2 * Copyright(c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1(the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 #include <SystemConfiguration/SCP.h>
24 #include <SystemConfiguration/SCPPath.h>
25 #include "SCPPrivate.h"
27 #include <SystemConfiguration/SCD.h>
31 #include <sys/errno.h>
35 _SCPOpen(SCPSessionRef
*session
,
43 SCPSessionPrivateRef newSession
;
46 CFMutableDataRef xmlData
;
49 newSession
= (SCPSessionPrivateRef
)CFAllocatorAllocate(NULL
, sizeof(SCPSessionPrivate
), 0);
50 newSession
->name
= NULL
;
51 newSession
->prefsID
= NULL
;
52 newSession
->perUser
= perUser
;
53 newSession
->user
= NULL
;
54 newSession
->path
= NULL
;
55 newSession
->signature
= NULL
;
56 newSession
->session
= NULL
;
57 newSession
->sessionKeyLock
= NULL
;
58 newSession
->sessionKeyCommit
= NULL
;
59 newSession
->sessionKeyApply
= NULL
;
60 newSession
->prefs
= NULL
;
61 newSession
->changed
= FALSE
;
62 newSession
->locked
= FALSE
;
63 newSession
->isRoot
= (geteuid() == 0);
66 * convert prefsID to path
68 newSession
->path
= _SCPPrefsPath(prefsID
, perUser
, user
);
69 if (newSession
->path
== NULL
) {
70 scp_status
= SCP_FAILED
;
77 fd
= open(newSession
->path
, O_RDONLY
, 0644);
79 char *errmsg
= strerror(errno
);
83 if (options
& kSCPOpenCreatePrefs
) {
84 bzero(&statBuf
, sizeof(statBuf
));
87 scp_status
= SCP_ENOENT
;
90 scp_status
= SCP_EACCESS
;
93 scp_status
= SCP_FAILED
;
95 SCDLog(LOG_DEBUG
, CFSTR("open() failed: %s"), errmsg
);
100 * check file, create signature
102 if (fstat(fd
, &statBuf
) == -1) {
103 SCDLog(LOG_DEBUG
, CFSTR("fstat() failed: %s"), strerror(errno
));
104 scp_status
= SCP_FAILED
;
110 newSession
->signature
= _SCPSignatureFromStatbuf(&statBuf
);
112 if (statBuf
.st_size
> 0) {
114 * extract property list
116 xmlData
= CFDataCreateMutable(NULL
, statBuf
.st_size
);
117 CFDataSetLength(xmlData
, statBuf
.st_size
);
118 if (read(fd
, (void *)CFDataGetBytePtr(xmlData
), statBuf
.st_size
) != statBuf
.st_size
) {
119 SCDLog(LOG_DEBUG
, CFSTR("_SCPOpen read(): could not load preference data."));
122 if (options
& kSCPOpenCreatePrefs
) {
125 scp_status
= SCP_BADCF
;
132 newSession
->prefs
= (CFMutableDictionaryRef
)
133 CFPropertyListCreateFromXMLData(NULL
,
135 kCFPropertyListMutableContainers
,
139 SCDLog(LOG_DEBUG
, CFSTR("_SCPOpen CFPropertyListCreateFromXMLData(): %s"), xmlError
);
140 if (options
& kSCPOpenCreatePrefs
) {
143 scp_status
= SCP_BADCF
;
148 * make sure that we've got a dictionary
150 if (CFGetTypeID(newSession
->prefs
) != CFDictionaryGetTypeID()) {
151 SCDLog(LOG_DEBUG
, CFSTR("_SCPOpen CFGetTypeID(): not a dictionary."));
152 CFRelease(newSession
->prefs
);
153 newSession
->prefs
= NULL
;
154 if (options
& kSCPOpenCreatePrefs
) {
157 scp_status
= SCP_BADCF
;
169 if (newSession
->prefs
== NULL
) {
171 * new file, create empty preferences
173 SCDLog(LOG_DEBUG
, CFSTR("_SCPOpen(): creating new dictionary."));
174 newSession
->prefs
= CFDictionaryCreateMutable(NULL
,
176 &kCFTypeDictionaryKeyCallBacks
,
177 &kCFTypeDictionaryValueCallBacks
);
178 newSession
->changed
= TRUE
;
184 newSession
->name
= CFRetain(name
);
186 newSession
->prefsID
= CFRetain(prefsID
);
188 newSession
->perUser
= perUser
;
190 newSession
->user
= CFRetain(user
);
192 *session
= (SCPSessionRef
)newSession
;
200 (void) SCPClose((SCPSessionRef
*)&newSession
);
206 SCPOpen(SCPSessionRef
*session
,
211 return _SCPOpen(session
, name
, prefsID
, FALSE
, NULL
, options
);
216 SCPUserOpen(SCPSessionRef
*session
,
222 return _SCPOpen(session
, name
, prefsID
, TRUE
, user
, options
);