2 * Copyright (c) 2000-2004,2011,2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
28 #ifndef _SECURITY_DLDBLISTCFPREF_H_
29 #define _SECURITY_DLDBLISTCFPREF_H_
31 #include <Security/SecKeychain.h>
32 #include <security_utilities/cfutilities.h>
33 #include <CoreFoundation/CFDictionary.h>
34 #include <security_cdsa_client/DLDBList.h>
35 #include <security_cdsa_utilities/cssmdb.h>
37 #include <CoreFoundation/CFNumber.h>
38 #include <CoreFoundation/CFDate.h>
43 class PasswordDBLookup
55 void lookupInfoOnUID (uid_t uid
);
56 const string
& getDirectory () {return mDirectory
;}
57 const string
& getName () {return mName
;}
63 DLDbListCFPref(SecPreferencesDomain domain
= kSecPreferencesDomainUser
);
66 void set(SecPreferencesDomain domain
);
69 vector
<DLDbIdentifier
>& list() { return mSearchList
; }
71 static DLDbIdentifier
makeDLDbIdentifier (const CSSM_GUID
&guid
, const CSSM_VERSION
&version
,
72 uint32 subserviceId
, CSSM_SERVICE_TYPE subserviceType
,
73 const char* dbName
, CSSM_NET_ADDRESS
*dbLocation
);
75 static DLDbIdentifier
cfDictionaryRefToDLDbIdentifier(CFDictionaryRef theDict
);
76 static CF_RETURNS_RETAINED CFDictionaryRef
dlDbIdentifierToCFDictionaryRef(const DLDbIdentifier
& dldbIdentifier
);
77 bool revert(bool force
);
79 void add(const DLDbIdentifier
&);
80 void remove(const DLDbIdentifier
&);
81 void rename(const DLDbIdentifier
&oldId
, const DLDbIdentifier
&newId
);
82 bool member(const DLDbIdentifier
&);
83 const vector
<DLDbIdentifier
> &searchList();
84 void searchList(const vector
<DLDbIdentifier
> &);
85 void defaultDLDbIdentifier(const DLDbIdentifier
&);
86 const DLDbIdentifier
&defaultDLDbIdentifier();
87 void loginDLDbIdentifier(const DLDbIdentifier
&);
88 const DLDbIdentifier
&loginDLDbIdentifier();
89 void forceUserSearchListReread ();
91 DLDbIdentifier
LoginDLDbIdentifier();
92 DLDbIdentifier
JaguarLoginDLDbIdentifier();
94 static string
ExpandTildesInPath(const string
&inPath
);
95 static string
StripPathStuff(const string
&inPath
);
96 static string
AbbreviatedPath(const string
&inPath
);
99 SecPreferencesDomain mDomain
;
100 bool hasChanged() const { return mChanged
; }
101 void changed(bool hasChanged
) { mChanged
= hasChanged
; }
109 static PasswordDBLookup
*mPdbLookup
;
110 static string
getPwInfo(PwInfoType type
);
111 static void clearPWInfo ();
113 void resetCachedValues();
114 bool loadPropertyList(bool force
);
115 void writePropertyList();
116 int testAndFixPropertyList();
126 void changeIdentity(ID_Direction
);
130 CFAbsoluteTime mPrefsTimeStamp
;
131 struct timespec mTimespec
;
132 CFMutableDictionaryRef mPropertyList
;
134 string mPrefsPath
, mHomeDir
, mUserName
;
135 vector
<DLDbIdentifier
> mSearchList
;
136 DLDbIdentifier mDefaultDLDbIdentifier
;
137 DLDbIdentifier mLoginDLDbIdentifier
;
138 bool mChanged
, mSearchListSet
, mDefaultDLDbIdentifierSet
, mLoginDLDbIdentifierSet
;
145 T
cfref() const { return reinterpret_cast<T
>(CFTypeRef(mRef
)); }
148 CCFValue(CFTypeRef ref
) : mRef(ref
) {}
149 CCFValue
&operator =(CFTypeRef ref
) { mRef
= ref
; return *this; }
151 CCFValue
&operator = (bool value
)
153 mRef
= value
?kCFBooleanTrue
:kCFBooleanFalse
;
158 CCFValue &operator = (const string &value) { string(value); return *this; }
160 void string(const string &value, CFStringEncoding encoding=kCFStringEncodingMacRoman)
162 mRef = CFStringCreate();
163 CFStringRef CFStringCreateWithBytes(CFAllocatorRef alloc, const UInt8 *bytes, CFIndex numBytes, CFStringEncoding encoding, Boolean isExternalRepresentation);
164 if (!mRef) throw std::bad_alloc;
169 bool hasValue() const { return mRef
; }
171 operator bool() const
173 if (!mRef
) return false;
174 if (::CFGetTypeID(mRef
) != ::CFBooleanGetTypeID())
175 throw std::logic_error("wrong type in property list");
177 return ::CFBooleanGetValue(cfref
<CFBooleanRef
>());
180 operator sint32() const
183 if (::CFGetTypeID(mRef
) != ::CFNumberGetTypeID())
184 throw std::logic_error("wrong type in property list");
187 ::CFNumberGetValue(cfref
<CFNumberRef
>(),kCFNumberSInt32Type
,&val
);
191 operator uint32() const { return uint32(sint32(*this)); }
193 operator const string() const { return getString(); }
195 const string
getString(CFStringEncoding encoding
=kCFStringEncodingUTF8
) const
198 throw std::logic_error("missing string in property list");
199 if (::CFGetTypeID(mRef
) != ::CFStringGetTypeID())
200 throw std::logic_error("wrong type in property list");
202 const char *tmpStr
=::CFStringGetCStringPtr(cfref
<CFStringRef
>(),encoding
);
205 CFIndex maxLen
= CFStringGetMaximumSizeForEncoding(CFStringGetLength(cfref
<CFStringRef
>()), encoding
);
206 auto_array
<char> buffer(maxLen
+ 1);
208 if (!::CFStringGetCString(cfref
<CFStringRef
>(),buffer
.get(),maxLen
+ 1,encoding
))
209 throw std::logic_error("could not convert string from property list");
212 return string(tmpStr
?tmpStr
:"");
214 return string(tmpStr
?tmpStr
:"");
217 CFCopyRef
<CFTypeRef
>mRef
;
220 } // end namespace Security
222 #endif /* !_SECURITY_DLDBLISTCFPREF_H_ */