2 * Copyright (c) 2000-2002 Apple Computer, Inc. All Rights Reserved.
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
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.
22 #ifndef _SECURITY_DLDBLISTCFPREF_H_
23 #define _SECURITY_DLDBLISTCFPREF_H_
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>
32 #include <CoreFoundation/CFNumber.h>
33 #include <CoreFoundation/CFDate.h>
38 class PasswordDBLookup
50 void lookupInfoOnUID (uid_t uid
);
51 const string
& getDirectory () {return mDirectory
;}
52 const string
& getName () {return mName
;}
58 DLDbListCFPref(SecPreferencesDomain domain
= kSecPreferencesDomainUser
);
61 void set(SecPreferencesDomain domain
);
64 vector
<DLDbIdentifier
>& list() { return mSearchList
; }
66 static DLDbIdentifier
cfDictionaryRefToDLDbIdentifier(CFDictionaryRef theDict
);
67 static CFDictionaryRef
dlDbIdentifierToCFDictionaryRef(const DLDbIdentifier
& dldbIdentifier
);
68 bool revert(bool force
);
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();
79 DLDbIdentifier
LoginDLDbIdentifier();
80 DLDbIdentifier
JaguarLoginDLDbIdentifier();
82 static string
ExpandTildesInPath(const string
&inPath
);
83 static string
StripPathStuff(const string
&inPath
);
84 static string
AbbreviatedPath(const string
&inPath
);
87 SecPreferencesDomain mDomain
;
88 bool hasChanged() const { return mChanged
; }
89 void changed(bool hasChanged
) { mChanged
= hasChanged
; }
97 static PasswordDBLookup
*mPdbLookup
;
98 static string
getPwInfo(PwInfoType type
);
99 static void clearPWInfo ();
101 void resetCachedValues();
102 bool loadPropertyList(bool force
);
103 void writePropertyList();
107 CFAbsoluteTime mPrefsTimeStamp
;
108 struct timespec mTimespec
;
109 CFMutableDictionaryRef mPropertyList
;
111 string mPrefsPath
, mHomeDir
, mUserName
;
112 vector
<DLDbIdentifier
> mSearchList
;
113 DLDbIdentifier mDefaultDLDbIdentifier
;
114 DLDbIdentifier mLoginDLDbIdentifier
;
115 bool mChanged
, mSearchListSet
, mDefaultDLDbIdentifierSet
, mLoginDLDbIdentifierSet
;
122 T
cfref() const { return reinterpret_cast<T
>(CFTypeRef(mRef
)); }
125 CCFValue(CFTypeRef ref
) : mRef(ref
) {}
126 CCFValue
&operator =(CFTypeRef ref
) { mRef
= ref
; return *this; }
128 CCFValue
&operator = (bool value
)
130 mRef
= value
?kCFBooleanTrue
:kCFBooleanFalse
;
135 CCFValue &operator = (const string &value) { string(value); return *this; }
137 void string(const string &value, CFStringEncoding encoding=kCFStringEncodingMacRoman)
139 mRef = CFStringCreate();
140 CFStringRef CFStringCreateWithBytes(CFAllocatorRef alloc, const UInt8 *bytes, CFIndex numBytes, CFStringEncoding encoding, Boolean isExternalRepresentation);
141 if (!mRef) throw std::bad_alloc;
146 bool hasValue() const { return mRef
; }
148 operator bool() const
150 if (!mRef
) return false;
151 if (::CFGetTypeID(mRef
) != ::CFBooleanGetTypeID())
152 throw std::logic_error("wrong type in property list");
154 return ::CFBooleanGetValue(cfref
<CFBooleanRef
>());
157 operator sint32() const
160 if (::CFGetTypeID(mRef
) != ::CFNumberGetTypeID())
161 throw std::logic_error("wrong type in property list");
164 ::CFNumberGetValue(cfref
<CFNumberRef
>(),kCFNumberSInt32Type
,&val
);
168 operator uint32() const { return uint32(sint32(*this)); }
170 operator const string() const { return getString(); }
172 const string
getString(CFStringEncoding encoding
=kCFStringEncodingMacRoman
) const
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");
179 const char *tmpStr
=::CFStringGetCStringPtr(cfref
<CFStringRef
>(),encoding
);
182 CFIndex maxLen
= CFStringGetMaximumSizeForEncoding(CFStringGetLength(cfref
<CFStringRef
>()), encoding
);
183 auto_array
<char> buffer(maxLen
+ 1);
185 if (!::CFStringGetCString(cfref
<CFStringRef
>(),buffer
.get(),maxLen
+ 1,encoding
))
186 throw std::logic_error("could not convert string from property list");
189 return string(tmpStr
?tmpStr
:"");
191 return string(tmpStr
?tmpStr
:"");
194 CFCopyRef
<CFTypeRef
>mRef
;
197 } // end namespace Security
199 #endif /* !_SECURITY_DLDBLISTCFPREF_H_ */