]> git.saurik.com Git - apple/libc.git/blob - darwin/_dirhelper.c
c25abd101eccea0c7d7731b93b1ec84aeeddc25a
[apple/libc.git] / darwin / _dirhelper.c
1 /*
2 * Copyright (c) 2006, 2007 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #include <mach/mach.h>
25 #include <mach/mach_error.h>
26 #include <servers/bootstrap.h>
27 #include <unistd.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <stdbool.h>
31 #include <membership.h>
32 #include <pthread.h>
33 #include <errno.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <sys/param.h>
37 #include <uuid/uuid.h>
38 #include <string.h>
39 #include <libkern/OSByteOrder.h>
40
41 #include "dirhelper.h"
42 #include "dirhelper_priv.h"
43
44 #define BUCKETLEN 2
45
46 #define MUTEX_LOCK(x) if(__is_threaded) pthread_mutex_lock(x)
47 #define MUTEX_UNLOCK(x) if(__is_threaded) pthread_mutex_unlock(x)
48
49 #define ENCODEBITS 6
50 #define ENCODEDSIZE ((8 * UUID_UID_SIZE + ENCODEBITS - 1) / ENCODEBITS)
51 #define UUID_UID_SIZE (sizeof(uuid_t) + sizeof(uid_t))
52
53 extern int __is_threaded;
54
55 static const mode_t modes[] = {
56 0, /* unused */
57 0700, /* temp */
58 0700, /* cache */
59 };
60
61 static const char *subdirs[] = {
62 DIRHELPER_TOP_STR,
63 DIRHELPER_TEMP_STR,
64 DIRHELPER_CACHE_STR,
65 };
66
67 static const char encode[] = "+-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
68
69 static void
70 encode_uuid_uid(uuid_t uuid, uid_t uid, char *str)
71 {
72 unsigned char buf[UUID_UID_SIZE + 1];
73 unsigned char *bp = buf;
74 int i = 0;
75 unsigned int n;
76
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) {
82 switch(i % 4) {
83 case 0:
84 n = *bp++;
85 *str++ = encode[n >> 2];
86 break;
87 case 1:
88 n = ((n & 0x3) << 8) | *bp++;
89 *str++ = encode[n >> 4];
90 break;
91 case 2:
92 n = ((n & 0xf) << 8) | *bp++;
93 *str++ = encode[n >> 6];
94 break;
95 case 3:
96 *str++ = encode[n & 0x3f];
97 break;
98 }
99 i++;
100 }
101 *str = 0;
102 }
103
104 char *
105 __user_local_dirname(uid_t uid, dirhelper_which_t which, char *path, size_t pathlen)
106 {
107 uuid_t uuid;
108 char str[ENCODEDSIZE + 1];
109 int res;
110
111 if(which < 0 || which > DIRHELPER_USER_LOCAL_LAST) {
112 errno = EINVAL;
113 return NULL;
114 }
115
116 res = mbr_uid_to_uuid(uid, uuid);
117 if(res != 0) {
118 errno = res;
119 return NULL;
120 }
121
122 //
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
127 //
128 encode_uuid_uid(uuid, uid, str);
129 res = snprintf(path, pathlen,
130 "%s%.*s/%s/%s",
131 VAR_FOLDERS_PATH, BUCKETLEN, str, str, subdirs[which]);
132 if(res >= pathlen) {
133 errno = EINVAL;
134 return NULL; /* buffer too small */
135 }
136 return path;
137 }
138
139 char *
140 __user_local_mkdir_p(char *path)
141 {
142 char *next;
143 int res;
144
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)
150 return NULL;
151 *next++ = '/'; // restore the slash and increment
152 }
153 return path;
154 }
155
156 __private_extern__ char *
157 _dirhelper(dirhelper_which_t which, char *path, size_t pathlen)
158 {
159 static char userdir[PATH_MAX];
160 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
161 int res;
162 struct stat sb;
163
164 if(which < 0 || which > DIRHELPER_USER_LOCAL_LAST) {
165 errno = EINVAL;
166 return NULL;
167 }
168
169 if(!*userdir) {
170 MUTEX_LOCK(&lock);
171 if (!*userdir) {
172
173 if(__user_local_dirname(geteuid(), DIRHELPER_USER_LOCAL, userdir, sizeof(userdir)) == NULL) {
174 MUTEX_UNLOCK(&lock);
175 return NULL;
176 }
177 /*
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).
182 */
183 if(stat(userdir, &sb) < 0) {
184 mach_port_t mp;
185
186 if(errno != ENOENT) { /* some unknown error */
187 *userdir = 0;
188 MUTEX_UNLOCK(&lock);
189 return NULL;
190 }
191 /*
192 * If we are root, lets do what dirhelper does for us.
193 */
194 if (geteuid() == 0) {
195 if (__user_local_mkdir_p(userdir) == NULL) {
196 *userdir = 0;
197 MUTEX_UNLOCK(&lock);
198 return NULL;
199 }
200 } else {
201 if(bootstrap_look_up(bootstrap_port, DIRHELPER_BOOTSTRAP_NAME, &mp) != KERN_SUCCESS) {
202 errno = EPERM;
203 server_error:
204 mach_port_deallocate(mach_task_self(), mp);
205 MUTEX_UNLOCK(&lock);
206 return NULL;
207 }
208 if(__dirhelper_create_user_local(mp) != KERN_SUCCESS) {
209 errno = EPERM;
210 goto server_error;
211 }
212 /* double check that the directory really got created */
213 if(stat(userdir, &sb) < 0) {
214 goto server_error;
215 }
216 mach_port_deallocate(mach_task_self(), mp);
217 }
218 }
219 }
220 MUTEX_UNLOCK(&lock);
221 }
222
223 if(pathlen < strlen(userdir) + strlen(subdirs[which]) + 1) {
224 errno = EINVAL;
225 return NULL; /* buffer too small */
226 }
227 strcpy(path, userdir);
228 strcat(path, subdirs[which]);
229
230 /*
231 * now for subdirectories, create it with the appropriate permissions
232 * if it doesn't already exist.
233 */
234 if(which != DIRHELPER_USER_LOCAL) {
235 res = mkdir(path, modes[which]);
236 if(res != 0 && errno != EEXIST)
237 return NULL;
238 }
239
240 return path;
241 }