]> git.saurik.com Git - apple/security.git/blob - Keychain/DLDBListCFPref.h
Security-164.1.tar.gz
[apple/security.git] / Keychain / DLDBListCFPref.h
1 /*
2 * Copyright (c) 2000-2002 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 * DLDBListCFPref.h
21 */
22 #ifndef _SECURITY_DLDBLISTCFPREF_H_
23 #define _SECURITY_DLDBLISTCFPREF_H_
24
25 #include <Security/SecKeychain.h>
26 #include <Security/cfutilities.h>
27 #include <CoreFoundation/CFDictionary.h>
28 #include <CoreFoundation/CFPreferences.h>
29 #include <Security/DLDBList.h>
30 #include <Security/cssmdb.h>
31 #include <stdexcept>
32 #include <CoreFoundation/CFNumber.h>
33 #include <CoreFoundation/CFDate.h>
34
35 namespace Security
36 {
37
38 class PasswordDBLookup
39 {
40 protected:
41 string mDirectory;
42 string mName;
43 bool mValid;
44 uid_t mCurrent;
45 time_t mTime;
46
47 public:
48 PasswordDBLookup ();
49
50 void lookupInfoOnUID (uid_t uid);
51 const string& getDirectory () {return mDirectory;}
52 const string& getName () {return mName;}
53 };
54
55 class DLDbListCFPref
56 {
57 public:
58 DLDbListCFPref(SecPreferencesDomain domain = kSecPreferencesDomainUser);
59 ~DLDbListCFPref();
60
61 void set(SecPreferencesDomain domain);
62
63 void save();
64 vector<DLDbIdentifier>& list() { return mSearchList; }
65
66 static DLDbIdentifier cfDictionaryRefToDLDbIdentifier(CFDictionaryRef theDict);
67 static CFDictionaryRef dlDbIdentifierToCFDictionaryRef(const DLDbIdentifier& dldbIdentifier);
68 bool revert(bool force);
69
70 void add(const DLDbIdentifier &);
71 void remove(const DLDbIdentifier &);
72 const vector<DLDbIdentifier> &searchList();
73 void searchList(const vector<DLDbIdentifier> &);
74 void defaultDLDbIdentifier(const DLDbIdentifier &);
75 const DLDbIdentifier &defaultDLDbIdentifier();
76 void loginDLDbIdentifier(const DLDbIdentifier &);
77 const DLDbIdentifier &loginDLDbIdentifier();
78
79 DLDbIdentifier LoginDLDbIdentifier();
80 DLDbIdentifier JaguarLoginDLDbIdentifier();
81
82 static string ExpandTildesInPath(const string &inPath);
83 static string StripPathStuff(const string &inPath);
84 static string AbbreviatedPath(const string &inPath);
85
86 protected:
87 SecPreferencesDomain mDomain;
88 bool hasChanged() const { return mChanged; }
89 void changed(bool hasChanged) { mChanged = hasChanged; }
90
91 enum PwInfoType
92 {
93 kHomeDir,
94 kUsername
95 };
96
97 static PasswordDBLookup *mPdbLookup;
98 static string getPwInfo(PwInfoType type);
99 static void clearPWInfo ();
100
101 void resetCachedValues();
102 bool loadPropertyList(bool force);
103 void writePropertyList();
104
105
106 private:
107 CFAbsoluteTime mPrefsTimeStamp;
108 struct timespec mTimespec;
109 CFMutableDictionaryRef mPropertyList;
110
111 string mPrefsPath, mHomeDir, mUserName;
112 vector<DLDbIdentifier> mSearchList;
113 DLDbIdentifier mDefaultDLDbIdentifier;
114 DLDbIdentifier mLoginDLDbIdentifier;
115 bool mChanged, mSearchListSet, mDefaultDLDbIdentifierSet, mLoginDLDbIdentifierSet;
116 };
117
118 class CCFValue
119 {
120 public:
121 template <class T>
122 T cfref() const { return reinterpret_cast<T>(CFTypeRef(mRef)); }
123
124 CCFValue() {}
125 CCFValue(CFTypeRef ref) : mRef(ref) {}
126 CCFValue &operator =(CFTypeRef ref) { mRef = ref; return *this; }
127
128 CCFValue &operator = (bool value)
129 {
130 mRef = value?kCFBooleanTrue:kCFBooleanFalse;
131 return *this;
132 }
133
134 /*
135 CCFValue &operator = (const string &value) { string(value); return *this; }
136
137 void string(const string &value, CFStringEncoding encoding=kCFStringEncodingMacRoman)
138 {
139 mRef = CFStringCreate();
140 CFStringRef CFStringCreateWithBytes(CFAllocatorRef alloc, const UInt8 *bytes, CFIndex numBytes, CFStringEncoding encoding, Boolean isExternalRepresentation);
141 if (!mRef) throw std::bad_alloc;
142 CFRelease(mRef);
143 }
144 */
145
146 bool hasValue() const { return mRef; }
147
148 operator bool() const
149 {
150 if (!mRef) return false;
151 if (::CFGetTypeID(mRef) != ::CFBooleanGetTypeID())
152 throw std::logic_error("wrong type in property list");
153
154 return ::CFBooleanGetValue(cfref<CFBooleanRef>());
155 }
156
157 operator sint32() const
158 {
159 if (!mRef) return 0;
160 if (::CFGetTypeID(mRef) != ::CFNumberGetTypeID())
161 throw std::logic_error("wrong type in property list");
162
163 sint32 val;
164 ::CFNumberGetValue(cfref<CFNumberRef>(),kCFNumberSInt32Type,&val);
165 return val;
166 }
167
168 operator uint32() const { return uint32(sint32(*this)); }
169
170 operator const string() const { return getString(); }
171
172 const string getString(CFStringEncoding encoding=kCFStringEncodingMacRoman) const
173 {
174 if (!mRef)
175 throw std::logic_error("missing string in property list");
176 if (::CFGetTypeID(mRef) != ::CFStringGetTypeID())
177 throw std::logic_error("wrong type in property list");
178
179 const char *tmpStr=::CFStringGetCStringPtr(cfref<CFStringRef>(),encoding);
180 if (tmpStr == NULL)
181 {
182 CFIndex maxLen = CFStringGetMaximumSizeForEncoding(CFStringGetLength(cfref<CFStringRef>()), encoding);
183 auto_array<char> buffer(maxLen + 1);
184
185 if (!::CFStringGetCString(cfref<CFStringRef>(),buffer.get(),maxLen + 1,encoding))
186 throw std::logic_error("could not convert string from property list");
187
188 tmpStr=buffer.get();
189 return string(tmpStr?tmpStr:"");
190 }
191 return string(tmpStr?tmpStr:"");
192 }
193 private:
194 CFCopyRef<CFTypeRef>mRef;
195 };
196
197 } // end namespace Security
198
199 #endif /* !_SECURITY_DLDBLISTCFPREF_H_ */