Tue, 25 Jun 2013 22:18:59 +0200
added keep-alive support
1 | 1 | /* |
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
3 | * | |
4 | * Copyright 2008 Sun Microsystems, Inc. All rights reserved. | |
5 | * | |
6 | * THE BSD LICENSE | |
7 | * | |
8 | * Redistribution and use in source and binary forms, with or without | |
9 | * modification, are permitted provided that the following conditions are met: | |
10 | * | |
11 | * Redistributions of source code must retain the above copyright notice, this | |
12 | * list of conditions and the following disclaimer. | |
13 | * Redistributions in binary form must reproduce the above copyright notice, | |
14 | * this list of conditions and the following disclaimer in the documentation | |
15 | * and/or other materials provided with the distribution. | |
16 | * | |
17 | * Neither the name of the nor the names of its contributors may be | |
18 | * used to endorse or promote products derived from this software without | |
19 | * specific prior written permission. | |
20 | * | |
21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
22 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
23 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
24 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER | |
25 | * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
26 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
27 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | |
28 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |
29 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | |
30 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | |
31 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
32 | */ | |
33 | ||
34 | /* | |
35 | * util.c: A hodge podge of utility functions and standard functions which | |
36 | * are unavailable on certain systems | |
37 | * | |
38 | * Rob McCool | |
39 | */ | |
40 | ||
41 | #ifdef XP_UNIX | |
42 | #include <sys/types.h> | |
43 | #include <sys/wait.h> | |
44 | #include <stdlib.h> | |
45 | #include <unistd.h> | |
46 | #include <limits.h> | |
47 | #include "prthread.h" | |
48 | #endif /* XP_UNIX */ | |
49 | ||
50 | ||
24
1a7853a4257e
removed some NSPR dependencies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
51 | //include "nspr.h" |
56
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
47
diff
changeset
|
52 | #include <errno.h> |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
47
diff
changeset
|
53 | |
24
1a7853a4257e
removed some NSPR dependencies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
54 | #include "../daemon/netsite.h" |
14
b8bf95b39952
New source folder layout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
55 | #include "../public/nsapi.h" |
1 | 56 | |
57 | #include "util.h" | |
58 | ||
62
c47e081b6c0f
added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
59
diff
changeset
|
59 | |
c47e081b6c0f
added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
59
diff
changeset
|
60 | |
c47e081b6c0f
added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
59
diff
changeset
|
61 | /* ------------------------------ _uudecode ------------------------------- */ |
c47e081b6c0f
added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
59
diff
changeset
|
62 | |
c47e081b6c0f
added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
59
diff
changeset
|
63 | static const unsigned char pr2six[256] = { |
c47e081b6c0f
added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
59
diff
changeset
|
64 | 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64, |
c47e081b6c0f
added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
59
diff
changeset
|
65 | 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,62,64,64,64,63, |
c47e081b6c0f
added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
59
diff
changeset
|
66 | 52,53,54,55,56,57,58,59,60,61,64,64,64,64,64,64,64,0,1,2,3,4,5,6,7,8,9, |
c47e081b6c0f
added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
59
diff
changeset
|
67 | 10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,64,64,64,64,64,64,26,27, |
c47e081b6c0f
added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
59
diff
changeset
|
68 | 28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51, |
c47e081b6c0f
added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
59
diff
changeset
|
69 | 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64, |
c47e081b6c0f
added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
59
diff
changeset
|
70 | 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64, |
c47e081b6c0f
added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
59
diff
changeset
|
71 | 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64, |
c47e081b6c0f
added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
59
diff
changeset
|
72 | 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64, |
c47e081b6c0f
added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
59
diff
changeset
|
73 | 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64, |
c47e081b6c0f
added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
59
diff
changeset
|
74 | 64,64,64,64,64,64,64,64,64,64,64,64,64 |
c47e081b6c0f
added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
59
diff
changeset
|
75 | }; |
c47e081b6c0f
added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
59
diff
changeset
|
76 | |
c47e081b6c0f
added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
59
diff
changeset
|
77 | /** you MUST reserve at least 2 additional bytes for bufout */ |
c47e081b6c0f
added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
59
diff
changeset
|
78 | size_t util_base64decode(char *bufcoded, size_t codedbytes, char *bufout) { |
c47e081b6c0f
added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
59
diff
changeset
|
79 | register char *bufin = bufcoded; |
c47e081b6c0f
added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
59
diff
changeset
|
80 | register int nprbytes; |
c47e081b6c0f
added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
59
diff
changeset
|
81 | size_t nbytesdecoded; |
c47e081b6c0f
added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
59
diff
changeset
|
82 | |
c47e081b6c0f
added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
59
diff
changeset
|
83 | /* Find the length */ |
c47e081b6c0f
added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
59
diff
changeset
|
84 | nprbytes = (int) codedbytes; |
c47e081b6c0f
added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
59
diff
changeset
|
85 | while(pr2six[(int)(bufin[nprbytes-1])] >= 64) { |
c47e081b6c0f
added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
59
diff
changeset
|
86 | nprbytes--; |
c47e081b6c0f
added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
59
diff
changeset
|
87 | } |
c47e081b6c0f
added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
59
diff
changeset
|
88 | nbytesdecoded = ((nprbytes+3)/4) * 3; |
c47e081b6c0f
added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
59
diff
changeset
|
89 | |
c47e081b6c0f
added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
59
diff
changeset
|
90 | while (nprbytes > 0) { |
c47e081b6c0f
added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
59
diff
changeset
|
91 | *(bufout++) = (unsigned char) |
c47e081b6c0f
added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
59
diff
changeset
|
92 | (pr2six[(int)(*bufin)] << 2 | pr2six[(int)bufin[1]] >> 4); |
c47e081b6c0f
added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
59
diff
changeset
|
93 | *(bufout++) = (unsigned char) |
c47e081b6c0f
added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
59
diff
changeset
|
94 | (pr2six[(int)bufin[1]] << 4 | pr2six[(int)bufin[2]] >> 2); |
c47e081b6c0f
added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
59
diff
changeset
|
95 | *(bufout++) = (unsigned char) |
c47e081b6c0f
added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
59
diff
changeset
|
96 | (pr2six[(int)bufin[2]] << 6 | pr2six[(int)bufin[3]]); |
c47e081b6c0f
added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
59
diff
changeset
|
97 | bufin += 4; |
c47e081b6c0f
added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
59
diff
changeset
|
98 | nprbytes -= 4; |
c47e081b6c0f
added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
59
diff
changeset
|
99 | } |
c47e081b6c0f
added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
59
diff
changeset
|
100 | |
c47e081b6c0f
added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
59
diff
changeset
|
101 | if(nprbytes & 03) { |
c47e081b6c0f
added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
59
diff
changeset
|
102 | if(pr2six[(int)bufin[-2]] > 63) |
c47e081b6c0f
added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
59
diff
changeset
|
103 | nbytesdecoded -= 2; |
c47e081b6c0f
added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
59
diff
changeset
|
104 | else |
c47e081b6c0f
added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
59
diff
changeset
|
105 | nbytesdecoded -= 1; |
c47e081b6c0f
added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
59
diff
changeset
|
106 | } |
c47e081b6c0f
added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
59
diff
changeset
|
107 | |
c47e081b6c0f
added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
59
diff
changeset
|
108 | return nbytesdecoded; |
c47e081b6c0f
added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
59
diff
changeset
|
109 | } |
c47e081b6c0f
added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
59
diff
changeset
|
110 | |
77 | 111 | |
112 | /* --------------------------- util_env_create ---------------------------- */ | |
113 | ||
114 | ||
115 | NSAPI_PUBLIC char **util_env_create(char **env, int n, int *pos) | |
116 | { | |
117 | int x; | |
118 | ||
119 | if(!env) { | |
120 | *pos = 0; | |
121 | return (char **) MALLOC((n + 1)*sizeof(char *)); | |
122 | } | |
123 | else { | |
124 | for(x = 0; (env[x]); x++); | |
125 | env = (char **) REALLOC(env, (n + x + 1)*(sizeof(char *))); | |
126 | *pos = x; | |
127 | return env; | |
128 | } | |
129 | } | |
130 | ||
131 | ||
132 | /* ---------------------------- util_env_free ----------------------------- */ | |
133 | ||
134 | ||
135 | NSAPI_PUBLIC void util_env_free(char **env) | |
136 | { | |
137 | register char **ep = env; | |
138 | ||
139 | for(ep = env; *ep; ep++) | |
140 | FREE(*ep); | |
141 | FREE(env); | |
142 | } | |
143 | ||
144 | /* ----------------------------- util_env_str ----------------------------- */ | |
145 | ||
146 | ||
147 | NSAPI_PUBLIC char *util_env_str(const char *name, const char *value) { | |
148 | char *t; | |
149 | ||
150 | t = (char *) MALLOC(strlen(name)+strlen(value)+2); /* 2: '=' and '\0' */ | |
151 | ||
152 | sprintf(t, "%s=%s", name, value); | |
153 | ||
154 | return t; | |
155 | } | |
156 | ||
157 | ||
158 | /* --------------------------- util_env_replace --------------------------- */ | |
159 | ||
160 | ||
161 | NSAPI_PUBLIC void util_env_replace(char **env, const char *name, const char *value) | |
162 | { | |
163 | int x, y, z; | |
164 | char *i; | |
165 | ||
166 | for(x = 0; env[x]; x++) { | |
167 | i = strchr(env[x], '='); | |
168 | *i = '\0'; | |
169 | if(!strcmp(env[x], name)) { | |
170 | y = strlen(env[x]); | |
171 | z = strlen(value); | |
172 | ||
173 | env[x] = (char *) REALLOC(env[x], y + z + 2); | |
174 | util_sprintf(&env[x][y], "=%s", value); | |
175 | return; | |
176 | } | |
177 | *i = '='; | |
1 | 178 | } |
77 | 179 | } |
180 | ||
181 | ||
182 | /* ---------------------------- util_env_find ----------------------------- */ | |
183 | ||
184 | ||
185 | NSAPI_PUBLIC char *util_env_find(char **env, const char *name) | |
186 | { | |
187 | char *i; | |
188 | int x, r; | |
189 | ||
190 | for(x = 0; env[x]; x++) { | |
191 | i = strchr(env[x], '='); | |
192 | *i = '\0'; | |
193 | r = !strcmp(env[x], name); | |
194 | *i = '='; | |
195 | if(r) | |
196 | return i + 1; | |
1 | 197 | } |
77 | 198 | return NULL; |
199 | } | |
200 | ||
201 | ||
202 | /* ---------------------------- util_env_copy ----------------------------- */ | |
203 | ||
204 | ||
205 | NSAPI_PUBLIC char **util_env_copy(char **src, char **dst) | |
206 | { | |
207 | char **src_ptr; | |
208 | int src_cnt; | |
209 | int index; | |
210 | ||
211 | if (!src) | |
212 | return NULL; | |
213 | ||
214 | for (src_cnt = 0, src_ptr = src; *src_ptr; src_ptr++, src_cnt++); | |
215 | ||
216 | if (!src_cnt) | |
217 | return NULL; | |
218 | ||
219 | dst = util_env_create(dst, src_cnt, &index); | |
220 | ||
221 | for (src_ptr = src; *src_ptr; index++, src_ptr++) | |
222 | dst[index] = STRDUP(*src_ptr); | |
223 | dst[index] = NULL; | |
224 | ||
225 | return dst; | |
1 | 226 | } |
77 | 227 | |
228 | /* ----------------------------- util_sprintf ----------------------------- */ | |
229 | ||
230 | NSAPI_PUBLIC int util_vsnprintf(char *s, int n, register const char *fmt, | |
231 | va_list args) | |
232 | { | |
233 | return vsnprintf(s, n, fmt, args); | |
234 | } | |
235 | ||
236 | NSAPI_PUBLIC int util_snprintf(char *s, int n, const char *fmt, ...) | |
237 | { | |
238 | va_list args; | |
239 | va_start(args, fmt); | |
240 | return vsnprintf(s, n, fmt, args); | |
241 | } | |
242 | ||
243 | NSAPI_PUBLIC int util_vsprintf(char *s, register const char *fmt, va_list args) | |
244 | { | |
245 | return vsprintf(s, fmt, args); | |
246 | } | |
247 | ||
248 | NSAPI_PUBLIC int util_sprintf(char *s, const char *fmt, ...) | |
249 | { | |
250 | va_list args; | |
251 | va_start(args, fmt); | |
252 | return vsprintf(s, fmt, args); | |
253 | } | |
254 | ||
255 | // TODO: asprintf | |
256 | ||
1 | 257 | |
258 | NSAPI_PUBLIC int INTutil_getboolean(const char *v, int def) { | |
259 | if(v[0] == 'T' || v[0] == 't') { | |
260 | return 1; | |
261 | } | |
262 | if(v[0] == 'F' || v[0] == 'f') { | |
263 | return 0; | |
264 | } | |
265 | return def; | |
266 | } | |
267 | ||
268 | ||
269 | /* ------------------------------ util_itoa ------------------------------- */ | |
270 | /* | |
271 | NSAPI_PUBLIC int util_itoa(int i, char *a) | |
272 | { | |
273 | int len = util_i64toa(i, a); | |
274 | ||
275 | PR_ASSERT(len < UTIL_ITOA_SIZE); | |
276 | ||
277 | return len; | |
278 | } | |
279 | */ | |
280 | NSAPI_PUBLIC int INTutil_itoa(int i, char *a) { | |
281 | return INTutil_i64toa(i, a); | |
282 | } | |
283 | ||
284 | ||
285 | /* ----------------------------- util_i64toa ------------------------------ */ | |
286 | ||
287 | /* | |
288 | * Assumption: Reversing the digits will be faster in the general case | |
289 | * than doing a log10 or some nasty trick to find the # of digits. | |
290 | */ | |
291 | ||
24
1a7853a4257e
removed some NSPR dependencies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
292 | NSAPI_PUBLIC int INTutil_i64toa(int64_t i, char *a) |
1 | 293 | { |
294 | register int x, y, p; | |
295 | register char c; | |
296 | int negative; | |
297 | ||
298 | negative = 0; | |
299 | if(i < 0) { | |
300 | *a++ = '-'; | |
301 | negative = 1; | |
302 | i = -i; | |
303 | } | |
304 | p = 0; | |
305 | while(i > 9) { | |
306 | a[p++] = (i%10) + '0'; | |
307 | i /= 10; | |
308 | } | |
309 | a[p++] = i + '0'; | |
310 | ||
311 | if(p > 1) { | |
312 | for(x = 0, y = p - 1; x < y; ++x, --y) { | |
313 | c = a[x]; | |
314 | a[x] = a[y]; | |
315 | a[y] = c; | |
316 | } | |
317 | } | |
318 | a[p] = '\0'; | |
319 | ||
320 | //PR_ASSERT(p + negative < UTIL_I64TOA_SIZE); | |
321 | ||
322 | return p + negative; | |
323 | } | |
47
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
24
diff
changeset
|
324 | |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
24
diff
changeset
|
325 | |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
24
diff
changeset
|
326 | |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
24
diff
changeset
|
327 | #ifndef XP_WIN32 |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
24
diff
changeset
|
328 | NSAPI_PUBLIC struct passwd * |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
24
diff
changeset
|
329 | util_getpwnam(const char *name, struct passwd *result, char *buffer, |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
24
diff
changeset
|
330 | int buflen) |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
24
diff
changeset
|
331 | { |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
24
diff
changeset
|
332 | struct passwd *rv; |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
24
diff
changeset
|
333 | |
69
4a10bc0ee80d
compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
63
diff
changeset
|
334 | #if defined(AIX) || defined(LINUX) || defined(HPUX) || defined(OSX) |
47
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
24
diff
changeset
|
335 | errno = getpwnam_r(name, result, buffer, buflen, &rv); |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
24
diff
changeset
|
336 | if (errno != 0) |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
24
diff
changeset
|
337 | rv = NULL; |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
24
diff
changeset
|
338 | #else |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
24
diff
changeset
|
339 | rv = getpwnam_r(name, result, buffer, buflen); |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
24
diff
changeset
|
340 | #endif |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
24
diff
changeset
|
341 | |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
24
diff
changeset
|
342 | return rv; |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
24
diff
changeset
|
343 | } |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
24
diff
changeset
|
344 | #endif |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
24
diff
changeset
|
345 | |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
24
diff
changeset
|
346 | |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
24
diff
changeset
|
347 | #ifndef XP_WIN32 |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
24
diff
changeset
|
348 | NSAPI_PUBLIC struct passwd * |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
24
diff
changeset
|
349 | util_getpwuid(uid_t uid, struct passwd *result, char *buffer, int buflen) |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
24
diff
changeset
|
350 | { |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
24
diff
changeset
|
351 | struct passwd *rv; |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
24
diff
changeset
|
352 | |
69
4a10bc0ee80d
compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
63
diff
changeset
|
353 | #if defined(AIX) || defined(LINUX) || defined(HPUX) || defined(OSX) |
47
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
24
diff
changeset
|
354 | errno = getpwuid_r(uid, result, buffer, buflen, &rv); |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
24
diff
changeset
|
355 | if (errno != 0) |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
24
diff
changeset
|
356 | rv = NULL; |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
24
diff
changeset
|
357 | #else |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
24
diff
changeset
|
358 | rv = getpwuid_r(uid, result, buffer, buflen); |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
24
diff
changeset
|
359 | #endif |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
24
diff
changeset
|
360 | |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
24
diff
changeset
|
361 | return rv; |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
24
diff
changeset
|
362 | } |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
24
diff
changeset
|
363 | #endif |
56
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
47
diff
changeset
|
364 | |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
47
diff
changeset
|
365 | |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
47
diff
changeset
|
366 | NSAPI_PUBLIC int util_errno2status(int errno_value) { |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
47
diff
changeset
|
367 | switch(errno_value) { |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
47
diff
changeset
|
368 | case 0: { |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
47
diff
changeset
|
369 | return 200; |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
47
diff
changeset
|
370 | } |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
47
diff
changeset
|
371 | case EACCES: { |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
47
diff
changeset
|
372 | return 403; |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
47
diff
changeset
|
373 | } |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
47
diff
changeset
|
374 | case ENOENT: { |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
47
diff
changeset
|
375 | return 404; |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
47
diff
changeset
|
376 | break; |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
47
diff
changeset
|
377 | } |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
47
diff
changeset
|
378 | } |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
47
diff
changeset
|
379 | return 500; |
c6cf20b09043
added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
47
diff
changeset
|
380 | } |
58
66c22e54aa90
webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
56
diff
changeset
|
381 | |
59
ab25c0a231d0
some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
58
diff
changeset
|
382 | NSAPI_PUBLIC int util_uri_unescape_strict(char *s) |
ab25c0a231d0
some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
58
diff
changeset
|
383 | { |
ab25c0a231d0
some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
58
diff
changeset
|
384 | char *t, *u, t1, t2; |
ab25c0a231d0
some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
58
diff
changeset
|
385 | int rv = 1; |
ab25c0a231d0
some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
58
diff
changeset
|
386 | |
ab25c0a231d0
some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
58
diff
changeset
|
387 | for(t = s, u = s; *t; ++t, ++u) { |
ab25c0a231d0
some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
58
diff
changeset
|
388 | if (*t == '%') { |
ab25c0a231d0
some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
58
diff
changeset
|
389 | t1 = t[1] & 0xdf; /* [a-f] -> [A-F] */ |
ab25c0a231d0
some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
58
diff
changeset
|
390 | if ((t1 < 'A' || t1 > 'F') && (t[1] < '0' || t[1] > '9')) |
ab25c0a231d0
some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
58
diff
changeset
|
391 | rv = 0; |
ab25c0a231d0
some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
58
diff
changeset
|
392 | |
ab25c0a231d0
some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
58
diff
changeset
|
393 | t2 = t[2] & 0xdf; /* [a-f] -> [A-F] */ |
ab25c0a231d0
some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
58
diff
changeset
|
394 | if ((t2 < 'A' || t2 > 'F') && (t[2] < '0' || t[2] > '9')) |
ab25c0a231d0
some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
58
diff
changeset
|
395 | rv = 0; |
ab25c0a231d0
some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
58
diff
changeset
|
396 | |
ab25c0a231d0
some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
58
diff
changeset
|
397 | *u = ((t[1] >= 'A' ? ((t[1] & 0xdf) - 'A')+10 : (t[1] - '0'))*16) + |
ab25c0a231d0
some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
58
diff
changeset
|
398 | (t[2] >= 'A' ? ((t[2] & 0xdf) - 'A')+10 : (t[2] - '0')); |
ab25c0a231d0
some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
58
diff
changeset
|
399 | t += 2; |
ab25c0a231d0
some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
58
diff
changeset
|
400 | } |
ab25c0a231d0
some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
58
diff
changeset
|
401 | else if (u != t) |
ab25c0a231d0
some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
58
diff
changeset
|
402 | *u = *t; |
ab25c0a231d0
some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
58
diff
changeset
|
403 | } |
ab25c0a231d0
some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
58
diff
changeset
|
404 | *u = *t; |
ab25c0a231d0
some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
58
diff
changeset
|
405 | |
ab25c0a231d0
some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
58
diff
changeset
|
406 | return rv; |
ab25c0a231d0
some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
58
diff
changeset
|
407 | } |
58
66c22e54aa90
webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
56
diff
changeset
|
408 | |
66c22e54aa90
webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
56
diff
changeset
|
409 | NSAPI_PUBLIC |
66c22e54aa90
webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
56
diff
changeset
|
410 | sstr_t util_path_append(pool_handle_t *pool, char *path, char *ch) { |
66c22e54aa90
webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
56
diff
changeset
|
411 | sstr_t parent = sstr(path); |
66c22e54aa90
webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
56
diff
changeset
|
412 | sstr_t child = sstr(ch); |
66c22e54aa90
webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
56
diff
changeset
|
413 | sstr_t newstr; |
66c22e54aa90
webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
56
diff
changeset
|
414 | sstr_t s; |
66c22e54aa90
webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
56
diff
changeset
|
415 | |
66c22e54aa90
webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
56
diff
changeset
|
416 | s.length = 0; |
66c22e54aa90
webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
56
diff
changeset
|
417 | s.ptr = NULL; |
66c22e54aa90
webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
56
diff
changeset
|
418 | newstr.length = parent.length + child.length; |
66c22e54aa90
webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
56
diff
changeset
|
419 | if(parent.ptr[parent.length - 1] != '/') { |
66c22e54aa90
webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
56
diff
changeset
|
420 | s = sstrn("/", 1); |
66c22e54aa90
webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
56
diff
changeset
|
421 | newstr.length++; |
66c22e54aa90
webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
56
diff
changeset
|
422 | } |
66c22e54aa90
webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
56
diff
changeset
|
423 | |
66c22e54aa90
webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
56
diff
changeset
|
424 | newstr.ptr = pool_malloc(pool, newstr.length + 1); |
66c22e54aa90
webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
56
diff
changeset
|
425 | if(!newstr.ptr) { |
66c22e54aa90
webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
56
diff
changeset
|
426 | // TODO: error |
59
ab25c0a231d0
some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
58
diff
changeset
|
427 | newstr.length = 0; |
58
66c22e54aa90
webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
56
diff
changeset
|
428 | return newstr; |
66c22e54aa90
webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
56
diff
changeset
|
429 | } |
66c22e54aa90
webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
56
diff
changeset
|
430 | if(s.length == 1) { |
66c22e54aa90
webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
56
diff
changeset
|
431 | newstr = sstrncat(3, newstr, parent, s, child); |
66c22e54aa90
webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
56
diff
changeset
|
432 | } else { |
66c22e54aa90
webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
56
diff
changeset
|
433 | newstr = sstrncat(2, newstr, parent, child); |
66c22e54aa90
webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
56
diff
changeset
|
434 | } |
66c22e54aa90
webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
56
diff
changeset
|
435 | newstr.ptr[newstr.length] = '\0'; |
66c22e54aa90
webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
56
diff
changeset
|
436 | |
66c22e54aa90
webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
56
diff
changeset
|
437 | return newstr; |
66c22e54aa90
webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
56
diff
changeset
|
438 | } |
63
66442f81f823
supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
62
diff
changeset
|
439 | |
66442f81f823
supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
62
diff
changeset
|
440 | sstr_t util_path_remove_last(sstr_t path) { |
66442f81f823
supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
62
diff
changeset
|
441 | int i; |
66442f81f823
supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
62
diff
changeset
|
442 | for(i=path.length-1;i>=0;i--) { |
66442f81f823
supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
62
diff
changeset
|
443 | char c = path.ptr[i]; |
66442f81f823
supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
62
diff
changeset
|
444 | if(c == '/') { |
66442f81f823
supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
62
diff
changeset
|
445 | path.ptr[i] = 0; |
66442f81f823
supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
62
diff
changeset
|
446 | path.length = i; |
66442f81f823
supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
62
diff
changeset
|
447 | break; |
66442f81f823
supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
62
diff
changeset
|
448 | } |
66442f81f823
supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
62
diff
changeset
|
449 | } |
66442f81f823
supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
62
diff
changeset
|
450 | if(i < 0) { |
66442f81f823
supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
62
diff
changeset
|
451 | path.ptr = NULL; |
66442f81f823
supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
62
diff
changeset
|
452 | path.length = 0; |
66442f81f823
supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
62
diff
changeset
|
453 | } |
66442f81f823
supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
62
diff
changeset
|
454 | return path; |
66442f81f823
supports file system ACLs on Solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
62
diff
changeset
|
455 | } |