]> git.saurik.com Git - apple/security.git/blob - cdsa/cssm/cssmmds.cpp
Security-28.tar.gz
[apple/security.git] / cdsa / cssm / cssmmds.cpp
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
3 *
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
8 * using this file.
9 *
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
16 */
17
18
19 //
20 // cssmmds - MDS interface for CSSM & friends
21 //
22 #ifdef __MWERKS__
23 #define _CPP_CSSMMDS
24 #endif
25 #include "cssmmds.h"
26
27
28 ModuleNexus<MdsComponent::MDS> MdsComponent::mds;
29
30
31 MdsComponent::MdsComponent(const Guid &guid) : mMyGuid(guid)
32 {
33 mIsValid = false;
34 }
35
36 MdsComponent::~MdsComponent()
37 {
38 }
39
40
41 void MdsComponent::getInfo() const
42 {
43 rewind(mds().config);
44 char buffer[1024];
45 while (fgets(buffer, sizeof(buffer), mds().config)) {
46 static const char space[] = " \t\f";
47 char *p = buffer + strspn(buffer, space);
48 if (*p == '#' || *p == '\0' || *p == '\n')
49 continue; // comment or blank line
50
51 // field 1: guid
52 const char *guid = p; p += strcspn(p, space);
53 if (!*p) CssmError::throwMe(CSSM_ERRCODE_MDS_ERROR);
54 *p++ = '\0';
55 try {
56 if (Guid(guid) != mMyGuid)
57 continue; // no match this line
58 } catch (CssmCommonError &error) {
59 if (error.cssmError() == CSSM_ERRCODE_INVALID_GUID)
60 CssmError::throwMe(CSSM_ERRCODE_MDS_ERROR); // invalid file guid => MDS error
61 throw; // pass all other errors
62 }
63
64 // field 2: flags/options
65 p += strspn(p, space);
66 mServices = 0;
67 mIsThreadSafe = false;
68 while (*p && !isspace(*p)) {
69 switch (*p) {
70 case 'a': mServices |= CSSM_SERVICE_AC; break;
71 case 'c': mServices |= CSSM_SERVICE_CSP; break;
72 case 'd': mServices |= CSSM_SERVICE_DL; break;
73 case 'C': mServices |= CSSM_SERVICE_CL; break;
74 case 't': mServices |= CSSM_SERVICE_TP; break;
75 case 'm': mIsThreadSafe = true; break;
76 default:
77 CssmError::throwMe(CSSM_ERRCODE_MDS_ERROR);
78 }
79 p++;
80 }
81 if (!*p) CssmError::throwMe(CSSM_ERRCODE_MDS_ERROR);
82 p += strspn(p, space); // skip space
83 p[strlen(p)-1] = '\0'; // remove trailing newline
84 mPath = p; // rest of line is pathname, uninterpreted
85 mIsValid = true;
86 return;
87 }
88 // not found in file
89 CssmError::throwMe(CSSM_ERRCODE_INVALID_GUID); // @@@ should have "guid not found" error
90 }
91
92
93 #if !defined(_MDS_PATH)
94 # define _MDS_PATH "/System/Library/Frameworks/Security.framework/Resources/MDS"
95 #endif
96
97 MdsComponent::MDS::MDS()
98 {
99 const char *path = getenv("MDSPATH");
100 if (path == NULL)
101 path = getenv("MDSFILE");
102 if (path == NULL)
103 path = _MDS_PATH;
104 if (!(config = fopen(path, "r")))
105 CssmError::throwMe(CSSM_ERRCODE_MDS_ERROR);
106 }
107
108 MdsComponent::MDS::~MDS()
109 {
110 fclose(config);
111 }