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 */
139 __private_extern__
char *
140 _dirhelper(dirhelper_which_t which
, char *path
, size_t pathlen
)
142 static char userdir
[PATH_MAX
];
143 static pthread_mutex_t lock
= PTHREAD_MUTEX_INITIALIZER
;
147 if(which
< 0 || which
> DIRHELPER_USER_LOCAL_LAST
) {
157 if(bootstrap_look_up(bootstrap_port
, DIRHELPER_BOOTSTRAP_NAME
, &mp
) != KERN_SUCCESS
) {
162 if(__user_local_dirname(geteuid(), DIRHELPER_USER_LOCAL
, userdir
, sizeof(userdir
)) == NULL
) {
164 mach_port_deallocate(mach_task_self(), mp
);
169 * check if userdir exists, and if not, call
170 * __dirhelper_create_user_local to create it
171 * (we have to check again afterwards).
173 if(stat(userdir
, &sb
) < 0) {
174 if(errno
!= ENOENT
) { /* some unknown error */
178 if(__dirhelper_create_user_local(mp
) != KERN_SUCCESS
) {
183 /* double check that the directory really got created */
184 if(stat(userdir
, &sb
) < 0) {
189 mach_port_deallocate(mach_task_self(), mp
);
194 if(pathlen
< strlen(userdir
) + strlen(subdirs
[which
]) + 1) {
196 return NULL
; /* buffer too small */
198 strcpy(path
, userdir
);
199 strcat(path
, subdirs
[which
]);
202 * now for subdirectories, create it with the appropriate permissions
203 * if it doesn't already exist.
205 if(which
!= DIRHELPER_USER_LOCAL
) {
206 res
= mkdir(path
, modes
[which
]);
207 if(res
!= 0 && errno
!= EEXIST
)