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/SystemConfiguration.h>
24 #include "SCPPrivate.h"
29 #include <sys/errno.h>
30 #include <sys/param.h>
33 static const struct scp_errmsg
{
37 { SCP_OK
, "Success!" },
38 { SCP_BUSY
, "Configuration daemon busy" },
39 { SCP_NEEDLOCK
, "Lock required for this operation" },
40 { SCP_EACCESS
, "Permission denied (must be root to obtain lock)" },
41 { SCP_ENOENT
, "Configuration file not found" },
42 { SCP_BADCF
, "Configuration file corrupt" },
43 { SCP_NOKEY
, "No such key" },
44 { SCP_NOLINK
, "No such link" },
45 { SCP_EXISTS
, "Key already defined" },
46 { SCP_STALE
, "Write attempted on stale version of object" },
47 { SCP_INVALIDARGUMENT
, "Invalid argument" },
48 { SCP_FAILED
, "Failed!" }
50 #define nSCP_ERRMSGS (sizeof(scp_errmsgs)/sizeof(struct scp_errmsg))
53 __private_extern__ CFDataRef
54 _SCPSignatureFromStatbuf(const struct stat
*statBuf
)
56 CFMutableDataRef signature
;
57 SCPSignatureDataRef sig
;
59 signature
= CFDataCreateMutable(NULL
, sizeof(SCPSignatureData
));
60 CFDataSetLength(signature
, sizeof(SCPSignatureData
));
61 sig
= (SCPSignatureDataRef
)CFDataGetBytePtr(signature
);
62 sig
->st_dev
= statBuf
->st_dev
;
63 sig
->st_ino
= statBuf
->st_ino
;
64 sig
->st_mtimespec
= statBuf
->st_mtimespec
;
65 sig
->st_size
= statBuf
->st_size
;
70 __private_extern__
char *
71 _SCPPrefsPath(CFStringRef prefsID
, boolean_t perUser
, CFStringRef user
)
73 CFStringRef path
= NULL
;
78 if (prefsID
== NULL
) {
79 /* no user prefsID specified */
81 } else if (CFStringHasPrefix(prefsID
, CFSTR("/"))) {
82 /* if absolute path */
83 path
= CFRetain(prefsID
);
86 * relative (to the user's preferences) path
88 char login
[MAXLOGNAME
+1];
91 bzero(&login
, sizeof(login
));
93 /* get current console user */
94 if (SCDConsoleUserGet(login
,
98 /* if could not get console user */
102 /* use specified user */
103 (void)CFStringGetBytes(user
,
104 CFRangeMake(0, CFStringGetLength(user
)),
105 kCFStringEncodingMacRoman
,
113 /* get password entry for user */
114 pwd
= getpwnam(login
);
116 /* if no home directory */
120 /* create prefs ID */
121 path
= CFStringCreateWithFormat(NULL
,
125 PREFS_DEFAULT_USER_DIR
,
129 if (prefsID
== NULL
) {
130 /* default preference ID */
131 path
= CFStringCreateWithFormat(NULL
,
135 PREFS_DEFAULT_CONFIG
);
136 } else if (CFStringHasPrefix(prefsID
, CFSTR("/"))) {
137 /* if absolute path */
138 path
= CFRetain(prefsID
);
141 path
= CFStringCreateWithFormat(NULL
,
150 * convert CFStringRef path to C-string path
152 pathLen
= CFStringGetLength(path
) + 1;
153 pathStr
= CFAllocatorAllocate(NULL
, pathLen
, 0);
154 if (!CFStringGetCString(path
,
157 kCFStringEncodingMacRoman
)) {
158 SCDLog(LOG_DEBUG
, CFSTR("could not convert path to C string"));
159 CFAllocatorDeallocate(NULL
, pathStr
);
169 SCPGetSignature(SCPSessionRef session
, CFDataRef
*signature
)
171 SCPSessionPrivateRef sessionPrivate
;
173 if (session
== NULL
) {
174 return SCP_FAILED
; /* you can't do anything with a closed session */
176 sessionPrivate
= (SCPSessionPrivateRef
)session
;
178 *signature
= sessionPrivate
->signature
;
183 __private_extern__ CFStringRef
184 _SCPNotificationKey(CFStringRef prefsID
,
189 CFStringRef key
= NULL
;
193 pathStr
= _SCPPrefsPath(prefsID
, perUser
, user
);
194 if (pathStr
== NULL
) {
198 /* create notification key */
213 key
= CFStringCreateWithFormat(NULL
,
220 CFAllocatorDeallocate(NULL
, pathStr
);
226 SCPNotificationKeyCreate(CFStringRef prefsID
, int keyType
)
228 return _SCPNotificationKey(prefsID
, FALSE
, NULL
, keyType
);
233 SCPUserNotificationKeyCreate(CFStringRef prefsID
, CFStringRef user
, int keyType
)
235 return _SCPNotificationKey(prefsID
, TRUE
, user
, keyType
);
240 SCPError(SCPStatus status
)
244 for (i
= 0; i
< nSCP_ERRMSGS
; i
++) {
245 if (scp_errmsgs
[i
].status
== status
) {
246 return scp_errmsgs
[i
].message
;
249 return "(unknown error)";