2 * Copyright (c) 2006, 2007 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@
24 #include <mach/mach.h>
25 #include <mach/mach_error.h>
26 #include <servers/bootstrap.h>
31 #include <membership.h>
34 #include <sys/types.h>
36 #include <sys/param.h>
37 #include <uuid/uuid.h>
39 #include <libkern/OSByteOrder.h>
41 #include "dirhelper.h"
42 #include "dirhelper_priv.h"
46 #define MUTEX_LOCK(x) if(__is_threaded) pthread_mutex_lock(x)
47 #define MUTEX_UNLOCK(x) if(__is_threaded) pthread_mutex_unlock(x)
50 #define ENCODEDSIZE ((8 * UUID_UID_SIZE + ENCODEBITS - 1) / ENCODEBITS)
51 #define UUID_UID_SIZE (sizeof(uuid_t) + sizeof(uid_t))
53 extern int __is_threaded
;
55 static const mode_t modes
[] = {
61 static const char *subdirs
[] = {
67 static const char encode
[] = "+-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
70 encode_uuid_uid(uuid_t uuid
, uid_t uid
, char *str
)
72 unsigned char buf
[UUID_UID_SIZE
+ 1];
73 unsigned char *bp
= buf
;
77 memcpy(bp
, uuid
, sizeof(uuid_t
));
78 uid
= OSSwapHostToBigInt32(uid
);
79 memcpy(bp
+ sizeof(uuid_t
), &uid
, sizeof(uid_t
));
80 bp
[UUID_UID_SIZE
] = 0; // this ensures the last encoded byte will have trailing zeros
81 while(i
< ENCODEDSIZE
) {
85 *str
++ = encode
[n
>> 2];
88 n
= ((n
& 0x3) << 8) | *bp
++;
89 *str
++ = encode
[n
>> 4];
92 n
= ((n
& 0xf) << 8) | *bp
++;
93 *str
++ = encode
[n
>> 6];
96 *str
++ = encode
[n
& 0x3f];
105 __user_local_dirname(uid_t uid
, dirhelper_which_t which
, char *path
, size_t pathlen
)
108 char str
[ENCODEDSIZE
+ 1];
111 if(which
< 0 || which
> DIRHELPER_USER_LOCAL_LAST
) {
116 res
= mbr_uid_to_uuid(uid
, uuid
);
123 // We partition the namespace so that we don't end up with too
124 // many users in a single directory. With 4096 buckets, we
125 // could scale to 1,000,000 users while keeping the average
126 // number of files in a single directory below 250
128 encode_uuid_uid(uuid
, uid
, str
);
129 res
= snprintf(path
, pathlen
,
131 VAR_FOLDERS_PATH
, BUCKETLEN
, str
, str
, subdirs
[which
]);
134 return NULL
; /* buffer too small */
140 __user_local_mkdir_p(char *path
)
145 next
= path
+ strlen(VAR_FOLDERS_PATH
);
146 while ((next
= strchr(next
, '/')) != NULL
) {
147 *next
= 0; // temporarily truncate
148 res
= mkdir(path
, 0755);
149 if (res
!= 0 && errno
!= EEXIST
)
151 *next
++ = '/'; // restore the slash and increment
156 __private_extern__
char *
157 _dirhelper(dirhelper_which_t which
, char *path
, size_t pathlen
)
159 static char userdir
[PATH_MAX
];
160 static pthread_mutex_t lock
= PTHREAD_MUTEX_INITIALIZER
;
164 if(which
< 0 || which
> DIRHELPER_USER_LOCAL_LAST
) {
173 if(__user_local_dirname(geteuid(), DIRHELPER_USER_LOCAL
, userdir
, sizeof(userdir
)) == NULL
) {
178 * check if userdir exists, and if not, either do the work
179 * ourself if we are root, or call
180 * __dirhelper_create_user_local to create it (we have to
181 * check again afterwards).
183 if(stat(userdir
, &sb
) < 0) {
186 if(errno
!= ENOENT
) { /* some unknown error */
192 * If we are root, lets do what dirhelper does for us.
194 if (geteuid() == 0) {
195 if (__user_local_mkdir_p(userdir
) == NULL
) {
201 if(bootstrap_look_up(bootstrap_port
, DIRHELPER_BOOTSTRAP_NAME
, &mp
) != KERN_SUCCESS
) {
204 mach_port_deallocate(mach_task_self(), mp
);
208 if(__dirhelper_create_user_local(mp
) != KERN_SUCCESS
) {
212 /* double check that the directory really got created */
213 if(stat(userdir
, &sb
) < 0) {
216 mach_port_deallocate(mach_task_self(), mp
);
223 if(pathlen
< strlen(userdir
) + strlen(subdirs
[which
]) + 1) {
225 return NULL
; /* buffer too small */
227 strcpy(path
, userdir
);
228 strcat(path
, subdirs
[which
]);
231 * now for subdirectories, create it with the appropriate permissions
232 * if it doesn't already exist.
234 if(which
!= DIRHELPER_USER_LOCAL
) {
235 res
= mkdir(path
, modes
[which
]);
236 if(res
!= 0 && errno
!= EEXIST
)