]> git.saurik.com Git - apple/security.git/blob - cdsa/cssm/cssmmds.cpp
Security-54.1.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 #include <ctype.h>
27
28
29 ModuleNexus<MdsComponent::MDS> MdsComponent::mds;
30
31
32 MdsComponent::MdsComponent(const Guid &guid) : mMyGuid(guid)
33 {
34 mIsValid = false;
35 }
36
37 MdsComponent::~MdsComponent()
38 {
39 }
40
41
42 void MdsComponent::getInfo() const
43 {
44 rewind(mds().config);
45 char buffer[1024];
46 while (fgets(buffer, sizeof(buffer), mds().config)) {
47 static const char space[] = " \t\f";
48 char *p = buffer + strspn(buffer, space);
49 if (*p == '#' || *p == '\0' || *p == '\n')
50 continue; // comment or blank line
51
52 // field 1: guid
53 const char *guid = p; p += strcspn(p, space);
54 if (!*p) CssmError::throwMe(CSSM_ERRCODE_MDS_ERROR);
55 *p++ = '\0';
56 try {
57 if (Guid(guid) != mMyGuid)
58 continue; // no match this line
59 } catch (const CssmCommonError &error) {
60 if (error.cssmError() == CSSM_ERRCODE_INVALID_GUID)
61 CssmError::throwMe(CSSM_ERRCODE_MDS_ERROR); // invalid file guid => MDS error
62 throw; // pass all other errors
63 }
64
65 // field 2: flags/options
66 p += strspn(p, space);
67 mServices = 0;
68 mIsThreadSafe = false;
69 while (*p && !isspace(*p)) {
70 switch (*p) {
71 case 'a': mServices |= CSSM_SERVICE_AC; break;
72 case 'c': mServices |= CSSM_SERVICE_CSP; break;
73 case 'd': mServices |= CSSM_SERVICE_DL; break;
74 case 'C': mServices |= CSSM_SERVICE_CL; break;
75 case 't': mServices |= CSSM_SERVICE_TP; break;
76 case 'm': mIsThreadSafe = true; break;
77 default:
78 CssmError::throwMe(CSSM_ERRCODE_MDS_ERROR);
79 }
80 p++;
81 }
82 if (!*p) CssmError::throwMe(CSSM_ERRCODE_MDS_ERROR);
83 p += strspn(p, space); // skip space
84 p[strlen(p)-1] = '\0'; // remove trailing newline
85 mPath = p; // rest of line is pathname, uninterpreted
86 mIsValid = true;
87 return;
88 }
89 // not found in file
90 CssmError::throwMe(CSSM_ERRCODE_INVALID_GUID); // @@@ should have "guid not found" error
91 }
92
93
94 #if !defined(_MDS_PATH)
95 # define _MDS_PATH "/System/Library/Frameworks/Security.framework/Resources/MDS"
96 #endif
97
98 MdsComponent::MDS::MDS()
99 {
100 const char *path = getenv("MDSPATH");
101 if (path == NULL)
102 path = getenv("MDSFILE");
103 if (path == NULL)
104 path = _MDS_PATH;
105 if (!(config = fopen(path, "r")))
106 CssmError::throwMe(CSSM_ERRCODE_MDS_ERROR);
107 }
108
109 MdsComponent::MDS::~MDS()
110 {
111 fclose(config);
112 }