]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/volume.c
Fixes for new rebuild db
[netatalk.git] / etc / afpd / volume.c
1 /*
2  * Copyright (c) 1990,1993 Regents of The University of Michigan.
3  * All Rights Reserved.  See COPYRIGHT.
4  */
5
6 #ifdef HAVE_CONFIG_H
7 #include "config.h"
8 #endif /* HAVE_CONFIG_H */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <ctype.h>
13 #include <pwd.h>
14 #include <grp.h>
15 #include <utime.h>
16 #include <errno.h>
17 #ifdef HAVE_STRINGS_H
18 #include <strings.h>
19 #endif
20 /* STDC check */
21 #if STDC_HEADERS
22 #include <string.h>
23 #else /* STDC_HEADERS */
24 #ifndef HAVE_STRCHR
25 #define strchr index
26 #define strrchr index
27 #endif /* HAVE_STRCHR */
28 char *strchr (), *strrchr ();
29 #ifndef HAVE_MEMCPY
30 #define memcpy(d,s,n) bcopy ((s), (d), (n))
31 #define memmove(d,s,n) bcopy ((s), (d), (n))
32 #endif /* ! HAVE_MEMCPY */
33 #endif /* STDC_HEADERS */
34 #include <sys/param.h>
35 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
38 #include <atalk/asp.h>
39 #include <atalk/dsi.h>
40 #include <atalk/adouble.h>
41 #include <atalk/afp.h>
42 #include <atalk/util.h>
43 #include <atalk/volinfo.h>
44 #include <atalk/logger.h>
45 #include <atalk/vfs.h>
46 #include <atalk/ea.h>
47 #ifdef CNID_DB
48 #include <atalk/cnid.h>
49 #endif /* CNID_DB*/
50
51 #include "globals.h"
52 #include "directory.h"
53 #include "file.h"
54 #include "volume.h"
55 #include "unix.h"
56 #include "mangle.h"
57 #include "fork.h"
58 #include "hash.h"
59
60 extern int afprun(int root, char *cmd, int *outfd);
61
62 #ifndef MIN
63 #define MIN(a, b) ((a) < (b) ? (a) : (b))
64 #endif /* ! MIN */
65
66 #ifndef NO_LARGE_VOL_SUPPORT
67 #if BYTE_ORDER == BIG_ENDIAN
68 #define hton64(x)       (x)
69 #define ntoh64(x)       (x)
70 #else /* BYTE_ORDER == BIG_ENDIAN */
71 #define hton64(x)       ((u_int64_t) (htonl(((x) >> 32) & 0xffffffffLL)) | \
72                          (u_int64_t) ((htonl(x) & 0xffffffffLL) << 32))
73 #define ntoh64(x)       (hton64(x))
74 #endif /* BYTE_ORDER == BIG_ENDIAN */
75 #endif /* ! NO_LARGE_VOL_SUPPORT */
76
77 static struct vol *Volumes = NULL;
78 static u_int16_t    lastvid = 0;
79 static char     *Trash = "\02\024Network Trash Folder";
80
81 static struct extmap    *Extmap = NULL, *Defextmap = NULL;
82 static int              Extmap_cnt;
83 static void             free_extmap(void);
84
85 #define VOLOPT_ALLOW      0  /* user allow list */
86 #define VOLOPT_DENY       1  /* user deny list */
87 #define VOLOPT_RWLIST     2  /* user rw list */
88 #define VOLOPT_ROLIST     3  /* user ro list */
89 #define VOLOPT_PASSWORD   4  /* volume password */
90 #define VOLOPT_CASEFOLD   5  /* character case mangling */
91 #define VOLOPT_FLAGS      6  /* various flags */
92 #define VOLOPT_DBPATH     7  /* path to database */
93 #define VOLOPT_LIMITSIZE  8  /* Limit the size of the volume */
94 /* Usable slot: 9 */
95 #define VOLOPT_VETO          10  /* list of veto filespec */
96 #define VOLOPT_PREEXEC       11  /* preexec command */
97 #define VOLOPT_ROOTPREEXEC   12  /* root preexec command */
98 #define VOLOPT_POSTEXEC      13  /* postexec command */
99 #define VOLOPT_ROOTPOSTEXEC  14  /* root postexec command */
100 #define VOLOPT_ENCODING      15  /* mac encoding (pre OSX)*/
101 #define VOLOPT_MACCHARSET    16
102 #define VOLOPT_CNIDSCHEME    17
103 #define VOLOPT_ADOUBLE       18  /* adouble version */
104
105 #ifdef FORCE_UIDGID
106 #warning UIDGID
107 #include "uid.h"
108
109 #define VOLOPT_FORCEUID      19  /* force uid for username x */
110 #define VOLOPT_FORCEGID      20  /* force gid for group x */
111 #endif /* FORCE_UIDGID */
112
113 #define VOLOPT_UMASK         21
114 #define VOLOPT_ALLOWED_HOSTS 22
115 #define VOLOPT_DENIED_HOSTS  23
116 #define VOLOPT_DPERM         24  /* dperm default directories perms */
117 #define VOLOPT_FPERM         25  /* fperm default files perms */
118 #define VOLOPT_DFLTPERM      26  /* perm */
119 #define VOLOPT_EA_VFS        27  /* Extended Attributes vfs indirection */
120 #define VOLOPT_CNIDSERVER    28  /* CNID Server ip address*/
121 #define VOLOPT_CNIDPORT      30  /* CNID server tcp port */
122
123 #define VOLOPT_MAX           31  /* <== IMPORTANT !!!!!! */
124 #define VOLOPT_NUM           (VOLOPT_MAX + 1)
125
126 #define VOLPASSLEN  8
127 #define VOLOPT_DEFAULT     ":DEFAULT:"
128 #define VOLOPT_DEFAULT_LEN 9
129
130 struct vol_option {
131     char *c_value;
132     int i_value;
133 };
134
135 typedef struct _special_folder {
136     const char *name;
137     int precreate;
138     mode_t mode;
139     int hide;
140 } _special_folder;
141
142 static const _special_folder special_folders[] = {
143     {"Network Trash Folder",     1,  0777,  1},
144     {"Temporary Items",          1,  0777,  1},
145     {".AppleDesktop",            1,  0777,  0},
146 #if 0
147     {"TheFindByContentFolder",   0,     0,  1},
148     {"TheVolumeSettingsFolder",  0,     0,  1},
149 #endif
150     {NULL, 0, 0, 0}};
151
152 /* Forward declarations */
153 static void handle_special_folders (const struct vol *);
154 static void deletevol(struct vol *vol);
155 static void volume_free(struct vol *vol);
156 static void check_ea_sys_support(struct vol *vol);
157
158 static void volfree(struct vol_option *options, const struct vol_option *save)
159 {
160     int i;
161
162     if (save) {
163         for (i = 0; i < VOLOPT_MAX; i++) {
164             if (options[i].c_value && (options[i].c_value != save[i].c_value))
165                 free(options[i].c_value);
166         }
167     } else {
168         for (i = 0; i < VOLOPT_MAX; i++) {
169             if (options[i].c_value)
170                 free(options[i].c_value);
171         }
172     }
173 }
174
175
176 /* handle variable substitutions. here's what we understand:
177  * $b   -> basename of path
178  * $c   -> client ip/appletalk address
179  * $d   -> volume pathname on server
180  * $f   -> full name (whatever's in the gecos field)
181  * $g   -> group
182  * $h   -> hostname
183  * $i   -> client ip/appletalk address without port
184  * $s   -> server name (hostname if it doesn't exist)
185  * $u   -> username (guest is usually nobody)
186  * $v   -> volume name or basename if null
187  * $z   -> zone (may not exist)
188  * $$   -> $
189  *
190  *
191  */
192 #define is_var(a, b) (strncmp((a), (b), 2) == 0)
193
194 static char *volxlate(AFPObj *obj, char *dest, size_t destlen,
195                       char *src, struct passwd *pwd, char *path, char *volname)
196 {
197     char *p, *r;
198     const char *q;
199     int len;
200     char *ret;
201
202     if (!src) {
203         return NULL;
204     }
205     if (!dest) {
206         dest = calloc(destlen +1, 1);
207     }
208     ret = dest;
209     if (!ret) {
210         return NULL;
211     }
212     strlcpy(dest, src, destlen +1);
213     if ((p = strchr(src, '$')) == NULL) /* nothing to do */
214         return ret;
215
216     /* first part of the path. just forward to the next variable. */
217     len = MIN((size_t)(p - src), destlen);
218     if (len > 0) {
219         destlen -= len;
220         dest += len;
221     }
222
223     while (p && destlen > 0) {
224         /* now figure out what the variable is */
225         q = NULL;
226         if (is_var(p, "$b")) {
227             if (path) {
228                 if ((q = strrchr(path, '/')) == NULL)
229                     q = path;
230                 else if (*(q + 1) != '\0')
231                     q++;
232             }
233         } else if (is_var(p, "$c")) {
234             if (obj->proto == AFPPROTO_ASP) {
235                 ASP asp = obj->handle;
236
237                 len = sprintf(dest, "%u.%u", ntohs(asp->asp_sat.sat_addr.s_net),
238                               asp->asp_sat.sat_addr.s_node);
239                 dest += len;
240                 destlen -= len;
241
242             } else if (obj->proto == AFPPROTO_DSI) {
243                 DSI *dsi = obj->handle;
244                 len = sprintf(dest, "%s:%u",
245                               getip_string((struct sockaddr *)&dsi->client),
246                               getip_port((struct sockaddr *)&dsi->client));
247                 dest += len;
248                 destlen -= len;
249             }
250         } else if (is_var(p, "$d")) {
251             q = path;
252         } else if (is_var(p, "$f")) {
253             if ((r = strchr(pwd->pw_gecos, ',')))
254                 *r = '\0';
255             q = pwd->pw_gecos;
256         } else if (is_var(p, "$g")) {
257             struct group *grp = getgrgid(pwd->pw_gid);
258             if (grp)
259                 q = grp->gr_name;
260         } else if (is_var(p, "$h")) {
261             q = obj->options.hostname;
262         } else if (is_var(p, "$i")) {
263             if (obj->proto == AFPPROTO_ASP) {
264                 ASP asp = obj->handle;
265
266                 len = sprintf(dest, "%u", ntohs(asp->asp_sat.sat_addr.s_net));
267                 dest += len;
268                 destlen -= len;
269
270             } else if (obj->proto == AFPPROTO_DSI) {
271                 DSI *dsi = obj->handle;
272                 q = getip_string((struct sockaddr *)&dsi->client);
273             }
274         } else if (is_var(p, "$s")) {
275             if (obj->Obj)
276                 q = obj->Obj;
277             else if (obj->options.server) {
278                 q = obj->options.server;
279             } else
280                 q = obj->options.hostname;
281         } else if (is_var(p, "$u")) {
282             char* sep = NULL;
283             if ( obj->options.ntseparator && (sep = strchr(obj->username, obj->options.ntseparator[0])) != NULL)
284                 q = sep+1;
285             else
286                 q = obj->username;
287         } else if (is_var(p, "$v")) {
288             if (volname) {
289                 q = volname;
290             }
291             else if (path) {
292                 if ((q = strrchr(path, '/')) == NULL)
293                     q = path;
294                 else if (*(q + 1) != '\0')
295                     q++;
296             }
297         } else if (is_var(p, "$z")) {
298             q = obj->Zone;
299         } else if (is_var(p, "$$")) {
300             q = "$";
301         } else
302             q = p;
303
304         /* copy the stuff over. if we don't understand something that we
305          * should, just skip it over. */
306         if (q) {
307             len = MIN(p == q ? 2 : strlen(q), destlen);
308             strncpy(dest, q, len);
309             dest += len;
310             destlen -= len;
311         }
312
313         /* stuff up to next $ */
314         src = p + 2;
315         p = strchr(src, '$');
316         len = p ? MIN((size_t)(p - src), destlen) : destlen;
317         if (len > 0) {
318             strncpy(dest, src, len);
319             dest += len;
320             destlen -= len;
321         }
322     }
323     return ret;
324 }
325
326 /* to make sure that val is valid, make sure to select an opt that
327    includes val */
328 static int optionok(const char *buf, const char *opt, const char *val)
329 {
330     if (!strstr(buf,opt))
331         return 0;
332     if (!val[1])
333         return 0;
334     return 1;
335 }
336
337
338 /* -------------------- */
339 static void setoption(struct vol_option *options, struct vol_option *save, int opt, const char *val)
340 {
341     if (options[opt].c_value && (!save || options[opt].c_value != save[opt].c_value))
342         free(options[opt].c_value);
343     options[opt].c_value = strdup(val + 1);
344 }
345
346 /* ------------------------------------------
347    handle all the options. tmp can't be NULL. */
348 static void volset(struct vol_option *options, struct vol_option *save,
349                    char *volname, int vlen,
350                    const char *tmp)
351 {
352     char *val;
353
354     val = strchr(tmp, ':');
355     if (!val) {
356         /* we'll assume it's a volume name. */
357         strncpy(volname, tmp, vlen);
358         volname[vlen] = 0;
359         return;
360     }
361 #if 0
362     LOG(log_debug, logtype_afpd, "Parsing volset %s", val);
363 #endif
364     if (optionok(tmp, "allow:", val)) {
365         setoption(options, save, VOLOPT_ALLOW, val);
366
367     } else if (optionok(tmp, "deny:", val)) {
368         setoption(options, save, VOLOPT_DENY, val);
369
370     } else if (optionok(tmp, "rwlist:", val)) {
371         setoption(options, save, VOLOPT_RWLIST, val);
372
373     } else if (optionok(tmp, "rolist:", val)) {
374         setoption(options, save, VOLOPT_ROLIST, val);
375
376     } else if (optionok(tmp, "codepage:", val)) {
377         LOG (log_error, logtype_afpd, "The old codepage system has been removed. Please make sure to read the documentation !!!!");
378         /* Make sure we don't screw anything */
379         exit (EXITERR_CONF);
380     } else if (optionok(tmp, "volcharset:", val)) {
381         setoption(options, save, VOLOPT_ENCODING, val);
382     } else if (optionok(tmp, "maccharset:", val)) {
383         setoption(options, save, VOLOPT_MACCHARSET, val);
384     } else if (optionok(tmp, "veto:", val)) {
385         setoption(options, save, VOLOPT_VETO, val);
386     } else if (optionok(tmp, "cnidscheme:", val)) {
387         setoption(options, save, VOLOPT_CNIDSCHEME, val);
388     } else if (optionok(tmp, "casefold:", val)) {
389         if (strcasecmp(val + 1, "tolower") == 0)
390             options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMLOWER;
391         else if (strcasecmp(val + 1, "toupper") == 0)
392             options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMUPPER;
393         else if (strcasecmp(val + 1, "xlatelower") == 0)
394             options[VOLOPT_CASEFOLD].i_value = AFPVOL_UUPPERMLOWER;
395         else if (strcasecmp(val + 1, "xlateupper") == 0)
396             options[VOLOPT_CASEFOLD].i_value = AFPVOL_ULOWERMUPPER;
397     } else if (optionok(tmp, "adouble:", val)) {
398         if (strcasecmp(val + 1, "v1") == 0)
399             options[VOLOPT_ADOUBLE].i_value = AD_VERSION1;
400 #if AD_VERSION == AD_VERSION2
401         else if (strcasecmp(val + 1, "v2") == 0)
402             options[VOLOPT_ADOUBLE].i_value = AD_VERSION2;
403         else if (strcasecmp(val + 1, "osx") == 0)
404             options[VOLOPT_ADOUBLE].i_value = AD_VERSION2_OSX;
405         else if (strcasecmp(val + 1, "sfm") == 0)
406             options[VOLOPT_ADOUBLE].i_value = AD_VERSION1_SFM;
407 #endif
408     } else if (optionok(tmp, "options:", val)) {
409         char *p;
410
411         if ((p = strtok(val + 1, ",")) == NULL) /* nothing */
412             return;
413
414         while (p) {
415             if (strcasecmp(p, "prodos") == 0)
416                 options[VOLOPT_FLAGS].i_value |= AFPVOL_A2VOL;
417             else if (strcasecmp(p, "mswindows") == 0) {
418                 options[VOLOPT_FLAGS].i_value |= AFPVOL_MSWINDOWS | AFPVOL_USEDOTS;
419             } else if (strcasecmp(p, "crlf") == 0)
420                 options[VOLOPT_FLAGS].i_value |= AFPVOL_CRLF;
421             else if (strcasecmp(p, "noadouble") == 0)
422                 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOADOUBLE;
423             else if (strcasecmp(p, "ro") == 0)
424                 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
425             else if (strcasecmp(p, "nohex") == 0)
426                 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOHEX;
427             else if (strcasecmp(p, "usedots") == 0)
428                 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS;
429             else if (strcasecmp(p, "invisibledots") == 0)
430                 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS | AFPVOL_INV_DOTS;
431             else if (strcasecmp(p, "limitsize") == 0)
432                 options[VOLOPT_FLAGS].i_value |= AFPVOL_LIMITSIZE;
433             /* support for either "dropbox" or "dropkludge" */
434             else if (strcasecmp(p, "dropbox") == 0)
435                 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
436             else if (strcasecmp(p, "dropkludge") == 0)
437                 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
438             else if (strcasecmp(p, "nofileid") == 0)
439                 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOFILEID;
440             else if (strcasecmp(p, "nostat") == 0)
441                 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOSTAT;
442             else if (strcasecmp(p, "preexec_close") == 0)
443                 options[VOLOPT_PREEXEC].i_value = 1;
444             else if (strcasecmp(p, "root_preexec_close") == 0)
445                 options[VOLOPT_ROOTPREEXEC].i_value = 1;
446             else if (strcasecmp(p, "upriv") == 0)
447                 options[VOLOPT_FLAGS].i_value |= AFPVOL_UNIX_PRIV;
448             else if (strcasecmp(p, "acls") == 0)
449                 options[VOLOPT_FLAGS].i_value |= AFPVOL_ACLS;
450             else if (strcasecmp(p, "nodev") == 0)
451                 options[VOLOPT_FLAGS].i_value |= AFPVOL_NODEV;
452             else if (strcasecmp(p, "caseinsensitive") == 0)
453                 options[VOLOPT_FLAGS].i_value |= AFPVOL_CASEINSEN;
454             else if (strcasecmp(p, "illegalseq") == 0)
455                 options[VOLOPT_FLAGS].i_value |= AFPVOL_EILSEQ;
456             else if (strcasecmp(p, "nocnidcache") == 0)
457                 options[VOLOPT_FLAGS].i_value &= ~AFPVOL_CACHE;
458             else if (strcasecmp(p, "tm") == 0)
459                 options[VOLOPT_FLAGS].i_value |= AFPVOL_TM;
460
461             p = strtok(NULL, ",");
462         }
463
464     } else if (optionok(tmp, "cnidserver:", val)) {
465         setoption(options, save, VOLOPT_CNIDSERVER, val);
466
467         char *p = strrchr(val + 1, ':');
468         if (p) {
469             *p = 0;
470             setoption(options, save, VOLOPT_CNIDPORT, p);
471         }
472
473         LOG(log_debug, logtype_afpd, "CNID Server for volume '%s': %s:%s",
474             volname, val + 1, p ? p + 1 : Cnid_port);
475
476     } else if (optionok(tmp, "dbpath:", val)) {
477         setoption(options, save, VOLOPT_DBPATH, val);
478
479     } else if (optionok(tmp, "umask:", val)) {
480         options[VOLOPT_UMASK].i_value = (int)strtol(val +1, NULL, 8);
481     } else if (optionok(tmp, "dperm:", val)) {
482         options[VOLOPT_DPERM].i_value = (int)strtol(val+1, NULL, 8);
483     } else if (optionok(tmp, "fperm:", val)) {
484         options[VOLOPT_FPERM].i_value = (int)strtol(val+1, NULL, 8);
485     } else if (optionok(tmp, "perm:", val)) {
486         options[VOLOPT_DFLTPERM].i_value = (int)strtol(val+1, NULL, 8);
487     } else if (optionok(tmp, "password:", val)) {
488         setoption(options, save, VOLOPT_PASSWORD, val);
489
490 #ifdef FORCE_UIDGID
491
492         /* this code allows forced uid/gid per volume settings */
493     } else if (optionok(tmp, "forceuid:", val)) {
494         setoption(options, save, VOLOPT_FORCEUID, val);
495     } else if (optionok(tmp, "forcegid:", val)) {
496         setoption(options, save, VOLOPT_FORCEGID, val);
497
498 #endif /* FORCE_UIDGID */
499     } else if (optionok(tmp, "root_preexec:", val)) {
500         setoption(options, save, VOLOPT_ROOTPREEXEC, val);
501
502     } else if (optionok(tmp, "preexec:", val)) {
503         setoption(options, save, VOLOPT_PREEXEC, val);
504
505     } else if (optionok(tmp, "root_postexec:", val)) {
506         setoption(options, save, VOLOPT_ROOTPOSTEXEC, val);
507
508     } else if (optionok(tmp, "postexec:", val)) {
509         setoption(options, save, VOLOPT_POSTEXEC, val);
510
511     } else if (optionok(tmp, "allowed_hosts:", val)) {
512         setoption(options, save, VOLOPT_ALLOWED_HOSTS, val);
513
514     } else if (optionok(tmp, "denied_hosts:", val)) {
515         setoption(options, save, VOLOPT_DENIED_HOSTS, val);
516
517     } else if (optionok(tmp, "ea:", val)) {
518         if (strcasecmp(val + 1, "ad") == 0)
519             options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_AD;
520         else if (strcasecmp(val + 1, "sys") == 0)
521             options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_SYS;
522         else if (strcasecmp(val + 1, "none") == 0)
523             options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_NONE;
524
525     } else if (optionok(tmp, "volsizelimit:", val)) {
526         options[VOLOPT_LIMITSIZE].i_value = (uint32_t)strtoul(val + 1, NULL, 10);
527
528     } else {
529         /* ignore unknown options */
530         LOG(log_debug, logtype_afpd, "ignoring unknown volume option: %s", tmp);
531
532     }
533 }
534
535 /* ----------------- */
536 static void showvol(const ucs2_t *name)
537 {
538     struct vol  *volume;
539     for ( volume = Volumes; volume; volume = volume->v_next ) {
540         if (volume->v_hide && !strcasecmp_w( volume->v_name, name ) ) {
541             volume->v_hide = 0;
542             return;
543         }
544     }
545 }
546
547 /* ------------------------------- */
548 static int creatvol(AFPObj *obj, struct passwd *pwd,
549                     char *path, char *name,
550                     struct vol_option *options,
551                     const int user /* user defined volume */
552     )
553 {
554     struct vol  *volume;
555     int         suffixlen, vlen, tmpvlen, u8mvlen, macvlen;
556     int         hide = 0;
557     char        tmpname[AFPVOL_U8MNAMELEN+1];
558     ucs2_t      u8mtmpname[(AFPVOL_U8MNAMELEN+1)*2], mactmpname[(AFPVOL_MACNAMELEN+1)*2];
559     char        suffix[6]; /* max is #FFFF */
560     u_int16_t   flags;
561
562     if ( name == NULL || *name == '\0' ) {
563         if ((name = strrchr( path, '/' )) == NULL) {
564             return -1;  /* Obviously not a fully qualified path */
565         }
566
567         /* if you wish to share /, you need to specify a name. */
568         if (*++name == '\0')
569             return -1;
570     }
571
572     /* suffix for mangling use (lastvid + 1)   */
573     /* because v_vid has not been decided yet. */
574     suffixlen = sprintf(suffix, "%c%X", MANGLE_CHAR, lastvid + 1 );
575
576     vlen = strlen( name );
577
578     /* Unicode Volume Name */
579     /* Firstly convert name from unixcharset to UTF8-MAC */
580     flags = CONV_IGNORE;
581     tmpvlen = convert_charset(obj->options.unixcharset, CH_UTF8_MAC, 0, name, vlen, tmpname, AFPVOL_U8MNAMELEN, &flags);
582     if (tmpvlen <= 0) {
583         strcpy(tmpname, "???");
584         tmpvlen = 3;
585     }
586
587     /* Do we have to mangle ? */
588     if ( (flags & CONV_REQMANGLE) || (tmpvlen > obj->options.volnamelen)) {
589         if (tmpvlen + suffixlen > obj->options.volnamelen) {
590             flags = CONV_FORCE;
591             tmpvlen = convert_charset(obj->options.unixcharset, CH_UTF8_MAC, 0, name, vlen, tmpname, obj->options.volnamelen - suffixlen, &flags);
592             tmpname[tmpvlen >= 0 ? tmpvlen : 0] = 0;
593         }
594         strcat(tmpname, suffix);
595         tmpvlen = strlen(tmpname);
596     }
597
598     /* Secondly convert name from UTF8-MAC to UCS2 */
599     if ( 0 >= ( u8mvlen = convert_string(CH_UTF8_MAC, CH_UCS2, tmpname, tmpvlen, u8mtmpname, AFPVOL_U8MNAMELEN*2)) )
600         return -1;
601
602     LOG(log_debug, logtype_afpd, "createvol: Volume '%s' -> UTF8-MAC Name: '%s'", name, tmpname);
603
604     /* Maccharset Volume Name */
605     /* Firsty convert name from unixcharset to maccharset */
606     flags = CONV_IGNORE;
607     tmpvlen = convert_charset(obj->options.unixcharset, obj->options.maccharset, 0, name, vlen, tmpname, AFPVOL_U8MNAMELEN, &flags);
608     if (tmpvlen <= 0) {
609         strcpy(tmpname, "???");
610         tmpvlen = 3;
611     }
612
613     /* Do we have to mangle ? */
614     if ( (flags & CONV_REQMANGLE) || (tmpvlen > AFPVOL_MACNAMELEN)) {
615         if (tmpvlen + suffixlen > AFPVOL_MACNAMELEN) {
616             flags = CONV_FORCE;
617             tmpvlen = convert_charset(obj->options.unixcharset, obj->options.maccharset, 0, name, vlen, tmpname, AFPVOL_MACNAMELEN - suffixlen, &flags);
618             tmpname[tmpvlen >= 0 ? tmpvlen : 0] = 0;
619         }
620         strcat(tmpname, suffix);
621         tmpvlen = strlen(tmpname);
622     }
623
624     /* Secondly convert name from maccharset to UCS2 */
625     if ( 0 >= ( macvlen = convert_string(obj->options.maccharset, CH_UCS2, tmpname, tmpvlen, mactmpname, AFPVOL_U8MNAMELEN*2)) )
626         return -1;
627
628     LOG(log_debug, logtype_afpd, "createvol: Volume '%s' ->  Longname: '%s'", name, tmpname);
629
630     /* check duplicate */
631     for ( volume = Volumes; volume; volume = volume->v_next ) {
632         if (( strcasecmp_w( volume->v_u8mname, u8mtmpname ) == 0 ) || ( strcasecmp_w( volume->v_macname, mactmpname ) == 0 )){
633             if (volume->v_deleted) {
634                 volume->v_new = hide = 1;
635             }
636             else {
637                 return -1;  /* Won't be able to access it, anyway... */
638             }
639         }
640     }
641
642     if (!( volume = (struct vol *)calloc(1, sizeof( struct vol ))) ) {
643         LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
644         return -1;
645     }
646     if ( NULL == ( volume->v_localname = strdup(name))) {
647         LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
648         free(volume);
649         return -1;
650     }
651
652     if ( NULL == ( volume->v_u8mname = strdup_w(u8mtmpname))) {
653         LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
654         volume_free(volume);
655         free(volume);
656         return -1;
657     }
658     if ( NULL == ( volume->v_macname = strdup_w(mactmpname))) {
659         LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
660         volume_free(volume);
661         free(volume);
662         return -1;
663     }
664     if (!( volume->v_path = (char *)malloc( strlen( path ) + 1 )) ) {
665         LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
666         volume_free(volume);
667         free(volume);
668         return -1;
669     }
670
671     volume->v_name = utf8_encoding()?volume->v_u8mname:volume->v_macname;
672     volume->v_hide = hide;
673     strcpy( volume->v_path, path );
674
675 #ifdef __svr4__
676     volume->v_qfd = -1;
677 #endif /* __svr4__ */
678     /* os X start at 1 and use network order ie. 1 2 3 */
679     volume->v_vid = ++lastvid;
680     volume->v_vid = htons(volume->v_vid);
681
682     /* handle options */
683     if (options) {
684         /* should we casefold? */
685         volume->v_casefold = options[VOLOPT_CASEFOLD].i_value;
686
687         /* shift in some flags */
688         volume->v_flags = options[VOLOPT_FLAGS].i_value;
689
690         if (options[VOLOPT_EA_VFS].i_value)
691             volume->v_vfs_ea = options[VOLOPT_EA_VFS].i_value;
692
693         volume->v_ad_options = 0;
694         if ((volume->v_flags & AFPVOL_NODEV))
695             volume->v_ad_options |= ADVOL_NODEV;
696         if ((volume->v_flags & AFPVOL_CACHE))
697             volume->v_ad_options |= ADVOL_CACHE;
698         if ((volume->v_flags & AFPVOL_UNIX_PRIV))
699             volume->v_ad_options |= ADVOL_UNIXPRIV;
700         if ((volume->v_flags & AFPVOL_INV_DOTS))
701             volume->v_ad_options |= ADVOL_INVDOTS;
702         if ((volume->v_flags & AFPVOL_NOADOUBLE))
703             volume->v_ad_options |= ADVOL_NOADOUBLE;
704
705         if (options[VOLOPT_PASSWORD].c_value)
706             volume->v_password = strdup(options[VOLOPT_PASSWORD].c_value);
707
708         if (options[VOLOPT_VETO].c_value)
709             volume->v_veto = strdup(options[VOLOPT_VETO].c_value);
710
711         if (options[VOLOPT_ENCODING].c_value)
712             volume->v_volcodepage = strdup(options[VOLOPT_ENCODING].c_value);
713
714         if (options[VOLOPT_MACCHARSET].c_value)
715             volume->v_maccodepage = strdup(options[VOLOPT_MACCHARSET].c_value);
716
717         if (options[VOLOPT_DBPATH].c_value)
718             volume->v_dbpath = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_DBPATH].c_value, pwd, path, name);
719
720         if (options[VOLOPT_CNIDSCHEME].c_value)
721             volume->v_cnidscheme = strdup(options[VOLOPT_CNIDSCHEME].c_value);
722
723         if (options[VOLOPT_CNIDSERVER].c_value)
724             volume->v_cnidserver = strdup(options[VOLOPT_CNIDSERVER].c_value);
725
726         if (options[VOLOPT_CNIDPORT].c_value)
727             volume->v_cnidport = strdup(options[VOLOPT_CNIDPORT].c_value);
728
729         if (options[VOLOPT_UMASK].i_value)
730             volume->v_umask = (mode_t)options[VOLOPT_UMASK].i_value;
731
732         if (options[VOLOPT_DPERM].i_value)
733             volume->v_dperm = (mode_t)options[VOLOPT_DPERM].i_value;
734
735         if (options[VOLOPT_FPERM].i_value)
736             volume->v_fperm = (mode_t)options[VOLOPT_FPERM].i_value;
737
738         if (options[VOLOPT_DFLTPERM].i_value)
739             volume->v_perm = (mode_t)options[VOLOPT_DFLTPERM].i_value;
740
741         if (options[VOLOPT_ADOUBLE].i_value)
742             volume->v_adouble = options[VOLOPT_ADOUBLE].i_value;
743         else
744             volume->v_adouble = AD_VERSION;
745
746         if (options[VOLOPT_LIMITSIZE].i_value)
747             volume->v_limitsize = options[VOLOPT_LIMITSIZE].i_value;
748
749         /* Mac to Unix conversion flags*/
750         volume->v_mtou_flags = 0;
751         if (!(volume->v_flags & AFPVOL_NOHEX))
752             volume->v_mtou_flags |= CONV_ESCAPEHEX;
753         if (!(volume->v_flags & AFPVOL_USEDOTS))
754             volume->v_mtou_flags |= CONV_ESCAPEDOTS;
755         if ((volume->v_flags & AFPVOL_EILSEQ))
756             volume->v_mtou_flags |= CONV__EILSEQ;
757
758         if ((volume->v_casefold & AFPVOL_MTOUUPPER))
759             volume->v_mtou_flags |= CONV_TOUPPER;
760         else if ((volume->v_casefold & AFPVOL_MTOULOWER))
761             volume->v_mtou_flags |= CONV_TOLOWER;
762
763         /* Unix to Mac conversion flags*/
764         volume->v_utom_flags = CONV_IGNORE | CONV_UNESCAPEHEX;
765         if ((volume->v_casefold & AFPVOL_UTOMUPPER))
766             volume->v_utom_flags |= CONV_TOUPPER;
767         else if ((volume->v_casefold & AFPVOL_UTOMLOWER))
768             volume->v_utom_flags |= CONV_TOLOWER;
769
770         if ((volume->v_flags & AFPVOL_EILSEQ))
771             volume->v_utom_flags |= CONV__EILSEQ;
772
773 #ifdef FORCE_UIDGID
774         if (options[VOLOPT_FORCEUID].c_value) {
775             volume->v_forceuid = strdup(options[VOLOPT_FORCEUID].c_value);
776         } else {
777             volume->v_forceuid = NULL; /* set as null so as to return 0 later on */
778         }
779
780         if (options[VOLOPT_FORCEGID].c_value) {
781             volume->v_forcegid = strdup(options[VOLOPT_FORCEGID].c_value);
782         } else {
783             volume->v_forcegid = NULL; /* set as null so as to return 0 later on */
784         }
785 #endif
786         if (!user) {
787             if (options[VOLOPT_PREEXEC].c_value)
788                 volume->v_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_PREEXEC].c_value, pwd, path, name);
789             volume->v_preexec_close = options[VOLOPT_PREEXEC].i_value;
790
791             if (options[VOLOPT_POSTEXEC].c_value)
792                 volume->v_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_POSTEXEC].c_value, pwd, path, name);
793
794             if (options[VOLOPT_ROOTPREEXEC].c_value)
795                 volume->v_root_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPREEXEC].c_value, pwd, path,  name);
796             volume->v_root_preexec_close = options[VOLOPT_ROOTPREEXEC].i_value;
797
798             if (options[VOLOPT_ROOTPOSTEXEC].c_value)
799                 volume->v_root_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPOSTEXEC].c_value, pwd, path,  name);
800         }
801     }
802     volume->v_dperm |= volume->v_perm;
803     volume->v_fperm |= volume->v_perm;
804
805     /* Check EA support on volume */
806     if (volume->v_vfs_ea == AFPVOL_EA_AUTO)
807         check_ea_sys_support(volume);
808     initvol_vfs(volume);
809
810     volume->v_next = Volumes;
811     Volumes = volume;
812     return 0;
813 }
814
815 /* ---------------- */
816 static char *myfgets( char *buf, int size, FILE *fp)
817 {
818     char    *p;
819     int     c;
820
821     p = buf;
822     while ((EOF != ( c = getc( fp )) ) && ( size > 1 )) {
823         if ( c == '\n' || c == '\r' ) {
824             if (p != buf && *(p -1) == '\\') {
825                 p--;
826                 size++;
827                 continue;
828             }
829             *p++ = '\n';
830             break;
831         } else {
832             *p++ = c;
833         }
834         size--;
835     }
836
837     if ( p == buf ) {
838         return( NULL );
839     } else {
840         *p = '\0';
841         return( buf );
842     }
843 }
844
845
846 /* check access list. this function wants something of the following
847  * form:
848  *        @group,name,name2,@group2,name3
849  *
850  * a NULL argument allows everybody to have access.
851  * we return three things:
852  *     -1: no list
853  *      0: list exists, but name isn't in it
854  *      1: in list
855  */
856
857 #ifndef NO_REAL_USER_NAME
858 /* authentication is case insensitive
859  * FIXME should we do the same with group name?
860  */
861 #define access_strcmp strcasecmp
862
863 #else
864 #define access_strcmp strcmp
865
866 #endif
867
868 static int accessvol(const char *args, const char *name)
869 {
870     char buf[MAXPATHLEN + 1], *p;
871     struct group *gr;
872
873     if (!args)
874         return -1;
875
876     strlcpy(buf, args, sizeof(buf));
877     if ((p = strtok(buf, ",")) == NULL) /* nothing, return okay */
878         return -1;
879
880     while (p) {
881         if (*p == '@') { /* it's a group */
882             if ((gr = getgrnam(p + 1)) && gmem(gr->gr_gid))
883                 return 1;
884         } else if (access_strcmp(p, name) == 0) /* it's a user name */
885             return 1;
886         p = strtok(NULL, ",");
887     }
888
889     return 0;
890 }
891
892 static int hostaccessvol(int type, const char *volname, const char *args, const AFPObj *obj)
893 {
894     int mask_int;
895     char buf[MAXPATHLEN + 1], *p, *b;
896     DSI *dsi = obj->handle;
897     struct sockaddr_storage client;
898
899     if (!args)
900         return -1;
901
902     strlcpy(buf, args, sizeof(buf));
903     if ((p = strtok_r(buf, ",", &b)) == NULL) /* nothing, return okay */
904         return -1;
905
906     if (obj->proto != AFPPROTO_DSI)
907         return -1;
908
909     while (p) {
910         int ret;
911         char *ipaddr, *mask_char;
912         struct addrinfo hints, *ai;
913
914         ipaddr = strtok(p, "/");
915         mask_char = strtok(NULL,"/");
916
917         /* Get address from string with getaddrinfo */
918         memset(&hints, 0, sizeof hints);
919         hints.ai_family = AF_UNSPEC;
920         hints.ai_socktype = SOCK_STREAM;
921         if ((ret = getaddrinfo(ipaddr, NULL, &hints, &ai)) != 0) {
922             LOG(log_error, logtype_afpd, "hostaccessvol: getaddrinfo: %s\n", gai_strerror(ret));
923             continue;
924         }
925
926         /* netmask */
927         if (mask_char != NULL)
928             mask_int = atoi(mask_char); /* apply_ip_mask does range checking on it */
929         else {
930             if (ai->ai_family == AF_INET) /* IPv4 */
931                 mask_int = 32;
932             else                          /* IPv6 */
933                 mask_int = 128;
934         }
935
936         /* Apply mask to addresses */
937         client = dsi->client;
938         apply_ip_mask((struct sockaddr *)&client, mask_int);
939         apply_ip_mask(ai->ai_addr, mask_int);
940
941         if (compare_ip((struct sockaddr *)&client, ai->ai_addr) == 0) {
942             if (type == VOLOPT_DENIED_HOSTS)
943                 LOG(log_info, logtype_afpd, "AFP access denied for client IP '%s' to volume '%s' by denied list",
944                     getip_string((struct sockaddr *)&client), volname);
945             freeaddrinfo(ai);
946             return 1;
947         }
948
949         /* next address */
950         freeaddrinfo(ai);
951         p = strtok_r(NULL, ",", &b);
952     }
953
954     if (type == VOLOPT_ALLOWED_HOSTS)
955         LOG(log_info, logtype_afpd, "AFP access denied for client IP '%s' to volume '%s', not in allowed list",
956             getip_string((struct sockaddr *)&dsi->client), volname);
957     return 0;
958 }
959
960 static void setextmap(char *ext, char *type, char *creator, int user)
961 {
962     struct extmap   *em;
963     int                 cnt;
964
965     if (Extmap == NULL) {
966         if (( Extmap = calloc(1, sizeof( struct extmap ))) == NULL ) {
967             LOG(log_error, logtype_afpd, "setextmap: calloc: %s", strerror(errno) );
968             return;
969         }
970     }
971     ext++;
972     for ( em = Extmap, cnt = 0; em->em_ext; em++, cnt++) {
973         if ( (strdiacasecmp( em->em_ext, ext )) == 0 ) {
974             break;
975         }
976     }
977
978     if ( em->em_ext == NULL ) {
979         if (!(Extmap  = realloc( Extmap, sizeof( struct extmap ) * (cnt +2))) ) {
980             LOG(log_error, logtype_afpd, "setextmap: realloc: %s", strerror(errno) );
981             return;
982         }
983         (Extmap +cnt +1)->em_ext = NULL;
984         em = Extmap +cnt;
985     } else if ( !user ) {
986         return;
987     }
988     if (em->em_ext)
989         free(em->em_ext);
990
991     if (!(em->em_ext = strdup(  ext))) {
992         LOG(log_error, logtype_afpd, "setextmap: strdup: %s", strerror(errno) );
993         return;
994     }
995
996     if ( *type == '\0' ) {
997         memcpy(em->em_type, "\0\0\0\0", sizeof( em->em_type ));
998     } else {
999         memcpy(em->em_type, type, sizeof( em->em_type ));
1000     }
1001     if ( *creator == '\0' ) {
1002         memcpy(em->em_creator, "\0\0\0\0", sizeof( em->em_creator ));
1003     } else {
1004         memcpy(em->em_creator, creator, sizeof( em->em_creator ));
1005     }
1006 }
1007
1008 /* -------------------------- */
1009 static int extmap_cmp(const void *map1, const void *map2)
1010 {
1011     const struct extmap *em1 = map1;
1012     const struct extmap *em2 = map2;
1013     return strdiacasecmp(em1->em_ext, em2->em_ext);
1014 }
1015
1016 static void sortextmap( void)
1017 {
1018     struct extmap   *em;
1019
1020     Extmap_cnt = 0;
1021     if ((em = Extmap) == NULL) {
1022         return;
1023     }
1024     while (em->em_ext) {
1025         em++;
1026         Extmap_cnt++;
1027     }
1028     if (Extmap_cnt) {
1029         qsort(Extmap, Extmap_cnt, sizeof(struct extmap), extmap_cmp);
1030         if (*Extmap->em_ext == 0) {
1031             /* the first line is really "." the default entry,
1032              * we remove the leading '.' in setextmap
1033              */
1034             Defextmap = Extmap;
1035         }
1036     }
1037 }
1038
1039 /* ----------------------
1040  */
1041 static void free_extmap( void)
1042 {
1043     struct extmap   *em;
1044
1045     if (Extmap) {
1046         for ( em = Extmap; em->em_ext; em++) {
1047             free (em->em_ext);
1048         }
1049         free(Extmap);
1050         Extmap = NULL;
1051         Defextmap = Extmap;
1052         Extmap_cnt = 0;
1053     }
1054 }
1055
1056 /* ----------------------
1057  */
1058 static int volfile_changed(struct afp_volume_name *p)
1059 {
1060     struct stat      st;
1061     char *name;
1062
1063     if (p->full_name)
1064         name = p->full_name;
1065     else
1066         name = p->name;
1067
1068     if (!stat( name, &st) && st.st_mtime > p->mtime) {
1069         p->mtime = st.st_mtime;
1070         return 1;
1071     }
1072     return 0;
1073 }
1074
1075 /* ----------------------
1076  * Read a volume configuration file and add the volumes contained within to
1077  * the global volume list.  If p2 is non-NULL, the file that is opened is
1078  * p1/p2
1079  *
1080  * Lines that begin with # and blank lines are ignored.
1081  * Volume lines are of the form:
1082  *      <unix path> [<volume name>] [allow:<user>,<@group>,...] \
1083  *                           [codepage:<file>] [casefold:<num>]
1084  *      <extension> TYPE [CREATOR]
1085  */
1086 static int readvolfile(AFPObj *obj, struct afp_volume_name *p1, char *p2, int user, struct passwd *pwent)
1087 {
1088     FILE        *fp;
1089     char        path[MAXPATHLEN + 1];
1090     char        tmp[MAXPATHLEN + 1];
1091     char        volname[AFPVOL_U8MNAMELEN + 1];
1092     char        buf[BUFSIZ];
1093     char        type[5], creator[5];
1094     char        *u, *p;
1095     struct passwd   *pw;
1096     struct vol_option   save_options[VOLOPT_NUM];
1097     struct vol_option   options[VOLOPT_NUM];
1098     int                 i;
1099     struct stat         st;
1100     int                 fd;
1101
1102     if (!p1->name)
1103         return -1;
1104     p1->mtime = 0;
1105     strcpy( path, p1->name );
1106     if ( p2 != NULL ) {
1107         strcat( path, "/" );
1108         strcat( path, p2 );
1109         if (p1->full_name) {
1110             free(p1->full_name);
1111         }
1112         p1->full_name = strdup(path);
1113     }
1114
1115     if (NULL == ( fp = fopen( path, "r" )) ) {
1116         return( -1 );
1117     }
1118     fd = fileno(fp);
1119     if (fd != -1 && !fstat( fd, &st) ) {
1120         p1->mtime = st.st_mtime;
1121     }
1122
1123     memset(save_options, 0, sizeof(save_options));
1124
1125     /* Enable some default options for all volumes */
1126     save_options[VOLOPT_FLAGS].i_value |= AFPVOL_CACHE;
1127     save_options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_AUTO;
1128     LOG(log_maxdebug, logtype_afpd, "readvolfile: seeding default umask: %04o",
1129         obj->options.umask);
1130     save_options[VOLOPT_UMASK].i_value = obj->options.umask;
1131
1132     while ( myfgets( buf, sizeof( buf ), fp ) != NULL ) {
1133         initline( strlen( buf ), buf );
1134         parseline( sizeof( path ) - 1, path );
1135         switch ( *path ) {
1136         case '\0' :
1137         case '#' :
1138             continue;
1139
1140         case ':':
1141             /* change the default options for this file */
1142             if (strncmp(path, VOLOPT_DEFAULT, VOLOPT_DEFAULT_LEN) == 0) {
1143                 *tmp = '\0';
1144                 for (i = 0; i < VOLOPT_NUM; i++) {
1145                     if (parseline( sizeof( path ) - VOLOPT_DEFAULT_LEN - 1,
1146                                    path + VOLOPT_DEFAULT_LEN) < 0)
1147                         break;
1148                     volset(save_options, NULL, tmp, sizeof(tmp) - 1,
1149                            path + VOLOPT_DEFAULT_LEN);
1150                 }
1151             }
1152             break;
1153
1154         case '~' :
1155             if (( p = strchr( path, '/' )) != NULL ) {
1156                 *p++ = '\0';
1157             }
1158             u = path;
1159             u++;
1160             if ( *u == '\0' ) {
1161                 u = obj->username;
1162             }
1163             if ( u == NULL || *u == '\0' || ( pw = getpwnam( u )) == NULL ) {
1164                 continue;
1165             }
1166             strcpy( tmp, pw->pw_dir );
1167             if ( p != NULL && *p != '\0' ) {
1168                 strcat( tmp, "/" );
1169                 strcat( tmp, p );
1170             }
1171             /* fall through */
1172
1173         case '/' :
1174             /* send path through variable substitution */
1175             if (*path != '~') /* need to copy path to tmp */
1176                 strcpy(tmp, path);
1177             if (!pwent)
1178                 pwent = getpwnam(obj->username);
1179             volxlate(obj, path, sizeof(path) - 1, tmp, pwent, NULL, NULL);
1180
1181             /* this is sort of braindead. basically, i want to be
1182              * able to specify things in any order, but i don't want to
1183              * re-write everything.
1184              *
1185              * currently we have options:
1186              *   volname
1187              *   codepage:x
1188              *   casefold:x
1189              *   allow:x,y,@z
1190              *   deny:x,y,@z
1191              *   rwlist:x,y,@z
1192              *   rolist:x,y,@z
1193              *   options:prodos,crlf,noadouble,ro...
1194              *   dbpath:x
1195              *   password:x
1196              *   preexec:x
1197              *
1198              *   namemask:x,y,!z  (not implemented yet)
1199              */
1200             memcpy(options, save_options, sizeof(options));
1201             *volname = '\0';
1202
1203             /* read in up to VOLOP_NUM possible options */
1204             for (i = 0; i < VOLOPT_NUM; i++) {
1205                 if (parseline( sizeof( tmp ) - 1, tmp ) < 0)
1206                     break;
1207
1208                 volset(options, save_options, volname, sizeof(volname) - 1, tmp);
1209             }
1210
1211             /* check allow/deny lists:
1212                allow -> either no list (-1), or in list (1)
1213                deny -> either no list (-1), or not in list (0) */
1214             if (accessvol(options[VOLOPT_ALLOW].c_value, obj->username) &&
1215                 (accessvol(options[VOLOPT_DENY].c_value, obj->username) < 1) &&
1216                 hostaccessvol(VOLOPT_ALLOWED_HOSTS, volname, options[VOLOPT_ALLOWED_HOSTS].c_value, obj) &&
1217                 (hostaccessvol(VOLOPT_DENIED_HOSTS, volname, options[VOLOPT_DENIED_HOSTS].c_value, obj) < 1)) {
1218
1219                 /* handle read-only behaviour. semantics:
1220                  * 1) neither the rolist nor the rwlist exist -> rw
1221                  * 2) rolist exists -> ro if user is in it.
1222                  * 3) rwlist exists -> ro unless user is in it. */
1223                 if (((options[VOLOPT_FLAGS].i_value & AFPVOL_RO) == 0) &&
1224                     ((accessvol(options[VOLOPT_ROLIST].c_value,
1225                                 obj->username) == 1) ||
1226                      !accessvol(options[VOLOPT_RWLIST].c_value,
1227                                 obj->username)))
1228                     options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
1229
1230                 /* do variable substitution for volname */
1231                 volxlate(obj, tmp, sizeof(tmp) - 1, volname, pwent, path, NULL);
1232                 creatvol(obj, pwent, path, tmp, options, p2 != NULL);
1233             }
1234             volfree(options, save_options);
1235             break;
1236
1237         case '.' :
1238             parseline( sizeof( type ) - 1, type );
1239             parseline( sizeof( creator ) - 1, creator );
1240             setextmap( path, type, creator, user);
1241             break;
1242
1243         default :
1244             break;
1245         }
1246     }
1247     volfree(save_options, NULL);
1248     sortextmap();
1249     if ( fclose( fp ) != 0 ) {
1250         LOG(log_error, logtype_afpd, "readvolfile: fclose: %s", strerror(errno) );
1251     }
1252     p1->loaded = 1;
1253     return( 0 );
1254 }
1255
1256 /* ------------------------------- */
1257 static void volume_free(struct vol *vol)
1258 {
1259     free(vol->v_localname);
1260     vol->v_localname = NULL;
1261     free(vol->v_u8mname);
1262     vol->v_u8mname = NULL;
1263     free(vol->v_macname);
1264     vol->v_macname = NULL;
1265     free(vol->v_path);
1266     free(vol->v_password);
1267     free(vol->v_veto);
1268     free(vol->v_volcodepage);
1269     free(vol->v_maccodepage);
1270     free(vol->v_cnidscheme);
1271     free(vol->v_dbpath);
1272     free(vol->v_gvs);
1273 #ifdef FORCE_UIDGID
1274     free(vol->v_forceuid);
1275     free(vol->v_forcegid);
1276 #endif /* FORCE_UIDGID */
1277 }
1278
1279 /* ------------------------------- */
1280 static void free_volumes(void )
1281 {
1282     struct vol  *vol;
1283     struct vol  *nvol, *ovol;
1284
1285     for ( vol = Volumes; vol; vol = vol->v_next ) {
1286         if (( vol->v_flags & AFPVOL_OPEN ) ) {
1287             vol->v_deleted = 1;
1288             continue;
1289         }
1290         volume_free(vol);
1291     }
1292
1293     for ( vol = Volumes, ovol = NULL; vol; vol = nvol) {
1294         nvol = vol->v_next;
1295
1296         if (vol->v_localname == NULL) {
1297             if (Volumes == vol) {
1298                 Volumes = nvol;
1299                 ovol = Volumes;
1300             }
1301             else {
1302                 ovol->v_next = nvol;
1303             }
1304             free(vol);
1305         }
1306         else {
1307             ovol = vol;
1308         }
1309     }
1310 }
1311
1312 /* ------------------------------- */
1313 static void volume_unlink(struct vol *volume)
1314 {
1315     struct vol *vol, *ovol, *nvol;
1316
1317     if (volume == Volumes) {
1318         Volumes = Volumes->v_next;
1319         return;
1320     }
1321     for ( vol = Volumes->v_next, ovol = Volumes; vol; vol = nvol) {
1322         nvol = vol->v_next;
1323
1324         if (vol == volume) {
1325             ovol->v_next = nvol;
1326             break;
1327         }
1328         else {
1329             ovol = vol;
1330         }
1331     }
1332 }
1333
1334 static int getvolspace(struct vol *vol,
1335                        u_int32_t *bfree, u_int32_t *btotal,
1336                        VolSpace *xbfree, VolSpace *xbtotal, u_int32_t *bsize)
1337 {
1338     int         spaceflag, rc;
1339     u_int32_t   maxsize;
1340 #ifndef NO_QUOTA_SUPPORT
1341     VolSpace    qfree, qtotal;
1342 #endif
1343
1344     spaceflag = AFPVOL_GVSMASK & vol->v_flags;
1345     /* report up to 2GB if afp version is < 2.2 (4GB if not) */
1346     maxsize = (vol->v_flags & AFPVOL_A2VOL) ? 0x01fffe00 :
1347         (((afp_version < 22) || (vol->v_flags & AFPVOL_LIMITSIZE))
1348          ? 0x7fffffffL : 0xffffffffL);
1349
1350 #ifdef AFS
1351     if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_AFSGVS ) {
1352         if ( afs_getvolspace( vol, xbfree, xbtotal, bsize ) == AFP_OK ) {
1353             vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_AFSGVS;
1354             goto getvolspace_done;
1355         }
1356     }
1357 #endif
1358
1359     if (( rc = ustatfs_getvolspace( vol, xbfree, xbtotal, bsize)) != AFP_OK ) {
1360         return( rc );
1361     }
1362
1363 #define min(a,b)    ((a)<(b)?(a):(b))
1364 #ifndef NO_QUOTA_SUPPORT
1365     if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_UQUOTA ) {
1366         if ( uquota_getvolspace( vol, &qfree, &qtotal, *bsize ) == AFP_OK ) {
1367             vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_UQUOTA;
1368             *xbfree = min(*xbfree, qfree);
1369             *xbtotal = min( *xbtotal, qtotal);
1370             goto getvolspace_done;
1371         }
1372     }
1373 #endif
1374     vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_USTATFS;
1375
1376 getvolspace_done:
1377     if (vol->v_limitsize) {
1378         /* FIXME: Free could be limit minus (total minus used), */
1379         /* which will confuse the client less ? */
1380         *xbfree = min(*xbfree, (vol->v_limitsize * 1024 * 1024));
1381         *xbtotal = min(*xbtotal, (vol->v_limitsize * 1024 * 1024));
1382     }
1383
1384     *bfree = min( *xbfree, maxsize);
1385     *btotal = min( *xbtotal, maxsize);
1386     return( AFP_OK );
1387 }
1388
1389 /* -----------------------
1390  * set volume creation date
1391  * avoid duplicate, well at least it tries
1392  */
1393 static void vol_setdate(u_int16_t id, struct adouble *adp, time_t date)
1394 {
1395     struct vol  *volume;
1396     struct vol  *vol = Volumes;
1397
1398     for ( volume = Volumes; volume; volume = volume->v_next ) {
1399         if (volume->v_vid == id) {
1400             vol = volume;
1401         }
1402         else if ((time_t)(AD_DATE_FROM_UNIX(date)) == volume->v_ctime) {
1403             date = date+1;
1404             volume = Volumes; /* restart */
1405         }
1406     }
1407     vol->v_ctime = AD_DATE_FROM_UNIX(date);
1408     ad_setdate(adp, AD_DATE_CREATE | AD_DATE_UNIX, date);
1409 }
1410
1411 /* ----------------------- */
1412 static int getvolparams( u_int16_t bitmap, struct vol *vol, struct stat *st, char *buf, size_t *buflen)
1413 {
1414     struct adouble  ad;
1415     int         bit = 0, isad = 1;
1416     u_int32_t       aint;
1417     u_short     ashort;
1418     u_int32_t       bfree, btotal, bsize;
1419     VolSpace            xbfree, xbtotal; /* extended bytes */
1420     char        *data, *nameoff = NULL;
1421     char                *slash;
1422
1423     LOG(log_debug, logtype_afpd, "getvolparams: Volume '%s'", vol->v_localname);
1424
1425     /* courtesy of jallison@whistle.com:
1426      * For MacOS8.x support we need to create the
1427      * .Parent file here if it doesn't exist. */
1428
1429     ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1430     if ( ad_open_metadata( vol->v_path, ADFLAGS_DIR, O_CREAT, &ad) < 0 ) {
1431         isad = 0;
1432         vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1433
1434     } else if (ad_get_MD_flags( &ad ) & O_CREAT) {
1435         slash = strrchr( vol->v_path, '/' );
1436         if(slash)
1437             slash++;
1438         else
1439             slash = vol->v_path;
1440         if (ad_getentryoff(&ad, ADEID_NAME)) {
1441             ad_setentrylen( &ad, ADEID_NAME, strlen( slash ));
1442             memcpy(ad_entry( &ad, ADEID_NAME ), slash,
1443                    ad_getentrylen( &ad, ADEID_NAME ));
1444         }
1445         vol_setdate(vol->v_vid, &ad, st->st_mtime);
1446         ad_flush(&ad);
1447     }
1448     else {
1449         if (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0)
1450             vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1451         else
1452             vol->v_ctime = aint;
1453     }
1454
1455     if (( bitmap & ( (1<<VOLPBIT_BFREE)|(1<<VOLPBIT_BTOTAL) |
1456                      (1<<VOLPBIT_XBFREE)|(1<<VOLPBIT_XBTOTAL) |
1457                      (1<<VOLPBIT_BSIZE)) ) != 0 ) {
1458         if ( getvolspace( vol, &bfree, &btotal, &xbfree, &xbtotal,
1459                           &bsize) != AFP_OK ) {
1460             if ( isad ) {
1461                 ad_close( &ad, ADFLAGS_HF );
1462             }
1463             return( AFPERR_PARAM );
1464         }
1465     }
1466
1467     data = buf;
1468     while ( bitmap != 0 ) {
1469         while (( bitmap & 1 ) == 0 ) {
1470             bitmap = bitmap>>1;
1471             bit++;
1472         }
1473
1474         switch ( bit ) {
1475         case VOLPBIT_ATTR :
1476             ashort = 0;
1477             /* check for read-only.
1478              * NOTE: we don't actually set the read-only flag unless
1479              *       it's passed in that way as it's possible to mount
1480              *       a read-write filesystem under a read-only one. */
1481             if ((vol->v_flags & AFPVOL_RO) ||
1482                 ((utime(vol->v_path, NULL) < 0) && (errno == EROFS))) {
1483                 ashort |= VOLPBIT_ATTR_RO;
1484             }
1485             /* prior 2.1 only VOLPBIT_ATTR_RO is defined */
1486             if (afp_version > 20) {
1487                 if (0 == (vol->v_flags & AFPVOL_NOFILEID) && vol->v_cdb != NULL &&
1488                     (vol->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1489                     ashort |= VOLPBIT_ATTR_FILEID;
1490                 }
1491                 ashort |= VOLPBIT_ATTR_CATSEARCH;
1492
1493                 if (afp_version >= 30) {
1494                     ashort |= VOLPBIT_ATTR_UTF8;
1495                     if (vol->v_flags & AFPVOL_UNIX_PRIV)
1496                         ashort |= VOLPBIT_ATTR_UNIXPRIV;
1497                     if (vol->v_flags & AFPVOL_TM)
1498                         ashort |= VOLPBIT_ATTR_TM;
1499
1500                     if (afp_version >= 32) {
1501                         if (vol->v_vfs_ea)
1502                             ashort |= VOLPBIT_ATTR_EXT_ATTRS;
1503                         if (vol->v_flags & AFPVOL_ACLS)
1504                             ashort |= VOLPBIT_ATTR_ACLS;
1505                     }
1506                 }
1507             }
1508             ashort = htons(ashort);
1509             memcpy(data, &ashort, sizeof( ashort ));
1510             data += sizeof( ashort );
1511             break;
1512
1513         case VOLPBIT_SIG :
1514             ashort = htons( AFPVOLSIG_DEFAULT );
1515             memcpy(data, &ashort, sizeof( ashort ));
1516             data += sizeof( ashort );
1517             break;
1518
1519         case VOLPBIT_CDATE :
1520             aint = vol->v_ctime;
1521             memcpy(data, &aint, sizeof( aint ));
1522             data += sizeof( aint );
1523             break;
1524
1525         case VOLPBIT_MDATE :
1526             if ( st->st_mtime > vol->v_mtime ) {
1527                 vol->v_mtime = st->st_mtime;
1528             }
1529             aint = AD_DATE_FROM_UNIX(vol->v_mtime);
1530             memcpy(data, &aint, sizeof( aint ));
1531             data += sizeof( aint );
1532             break;
1533
1534         case VOLPBIT_BDATE :
1535             if (!isad ||  (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1536                 aint = AD_DATE_START;
1537             memcpy(data, &aint, sizeof( aint ));
1538             data += sizeof( aint );
1539             break;
1540
1541         case VOLPBIT_VID :
1542             memcpy(data, &vol->v_vid, sizeof( vol->v_vid ));
1543             data += sizeof( vol->v_vid );
1544             break;
1545
1546         case VOLPBIT_BFREE :
1547             bfree = htonl( bfree );
1548             memcpy(data, &bfree, sizeof( bfree ));
1549             data += sizeof( bfree );
1550             break;
1551
1552         case VOLPBIT_BTOTAL :
1553             btotal = htonl( btotal );
1554             memcpy(data, &btotal, sizeof( btotal ));
1555             data += sizeof( btotal );
1556             break;
1557
1558 #ifndef NO_LARGE_VOL_SUPPORT
1559         case VOLPBIT_XBFREE :
1560             xbfree = hton64( xbfree );
1561 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1562             bcopy(&xbfree, data, sizeof(xbfree));
1563 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1564             memcpy(data, &xbfree, sizeof( xbfree ));
1565 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1566             data += sizeof( xbfree );
1567             break;
1568
1569         case VOLPBIT_XBTOTAL :
1570             xbtotal = hton64( xbtotal );
1571 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1572             bcopy(&xbtotal, data, sizeof(xbtotal));
1573 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1574             memcpy(data, &xbtotal, sizeof( xbtotal ));
1575 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1576             data += sizeof( xbfree );
1577             break;
1578 #endif /* ! NO_LARGE_VOL_SUPPORT */
1579
1580         case VOLPBIT_NAME :
1581             nameoff = data;
1582             data += sizeof( u_int16_t );
1583             break;
1584
1585         case VOLPBIT_BSIZE:  /* block size */
1586             bsize = htonl(bsize);
1587             memcpy(data, &bsize, sizeof(bsize));
1588             data += sizeof(bsize);
1589             break;
1590
1591         default :
1592             if ( isad ) {
1593                 ad_close( &ad, ADFLAGS_HF );
1594             }
1595             return( AFPERR_BITMAP );
1596         }
1597         bitmap = bitmap>>1;
1598         bit++;
1599     }
1600     if ( nameoff ) {
1601         ashort = htons( data - buf );
1602         memcpy(nameoff, &ashort, sizeof( ashort ));
1603         /* name is always in mac charset */
1604         aint = ucs2_to_charset( vol->v_maccharset, vol->v_macname, data+1, AFPVOL_MACNAMELEN + 1);
1605         if ( aint <= 0 ) {
1606             *buflen = 0;
1607             return AFPERR_MISC;
1608         }
1609
1610         *data++ = aint;
1611         data += aint;
1612     }
1613     if ( isad ) {
1614         ad_close_metadata( &ad);
1615     }
1616     *buflen = data - buf;
1617     return( AFP_OK );
1618 }
1619
1620 /* ------------------------- */
1621 static int stat_vol(u_int16_t bitmap, struct vol *vol, char *rbuf, size_t *rbuflen)
1622 {
1623     struct stat st;
1624     int     ret;
1625     size_t  buflen;
1626
1627     if ( stat( vol->v_path, &st ) < 0 ) {
1628         *rbuflen = 0;
1629         return( AFPERR_PARAM );
1630     }
1631     /* save the volume device number */
1632     vol->v_dev = st.st_dev;
1633
1634     buflen = *rbuflen - sizeof( bitmap );
1635     if (( ret = getvolparams( bitmap, vol, &st,
1636                               rbuf + sizeof( bitmap ), &buflen )) != AFP_OK ) {
1637         *rbuflen = 0;
1638         return( ret );
1639     }
1640     *rbuflen = buflen + sizeof( bitmap );
1641     bitmap = htons( bitmap );
1642     memcpy(rbuf, &bitmap, sizeof( bitmap ));
1643     return( AFP_OK );
1644
1645 }
1646
1647 /* ------------------------------- */
1648 void load_volumes(AFPObj *obj)
1649 {
1650     struct passwd   *pwent;
1651
1652     if (Volumes) {
1653         int changed = 0;
1654
1655         /* check files date */
1656         if (obj->options.defaultvol.loaded) {
1657             changed = volfile_changed(&obj->options.defaultvol);
1658         }
1659         if (obj->options.systemvol.loaded) {
1660             changed |= volfile_changed(&obj->options.systemvol);
1661         }
1662         if (obj->options.uservol.loaded) {
1663             changed |= volfile_changed(&obj->options.uservol);
1664         }
1665         if (!changed)
1666             return;
1667
1668         free_extmap();
1669         free_volumes();
1670     }
1671
1672     pwent = getpwnam(obj->username);
1673     if ( (obj->options.flags & OPTION_USERVOLFIRST) == 0 ) {
1674         readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent);
1675     }
1676
1677     if ((*obj->username == '\0') || (obj->options.flags & OPTION_NOUSERVOL)) {
1678         readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent);
1679     } else if (pwent) {
1680         /*
1681          * Read user's AppleVolumes or .AppleVolumes file
1682          * If neither are readable, read the default volumes file. if
1683          * that doesn't work, create a user share.
1684          */
1685         if (obj->options.uservol.name) {
1686             free(obj->options.uservol.name);
1687         }
1688         obj->options.uservol.name = strdup(pwent->pw_dir);
1689         if ( readvolfile(obj, &obj->options.uservol,    "AppleVolumes", 1, pwent) < 0 &&
1690              readvolfile(obj, &obj->options.uservol, ".AppleVolumes", 1, pwent) < 0 &&
1691              readvolfile(obj, &obj->options.uservol, "applevolumes", 1, pwent) < 0 &&
1692              readvolfile(obj, &obj->options.uservol, ".applevolumes", 1, pwent) < 0 &&
1693              obj->options.defaultvol.name != NULL ) {
1694             if (readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent) < 0)
1695                 creatvol(obj, pwent, pwent->pw_dir, NULL, NULL, 1);
1696         }
1697     }
1698     if ( obj->options.flags & OPTION_USERVOLFIRST ) {
1699         readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent );
1700     }
1701
1702     if ( obj->options.closevol ) {
1703         struct vol *vol;
1704
1705         for ( vol = Volumes; vol; vol = vol->v_next ) {
1706             if (vol->v_deleted && !vol->v_new ) {
1707                 of_closevol(vol);
1708                 deletevol(vol);
1709                 vol = Volumes;
1710             }
1711         }
1712     }
1713 }
1714
1715 /* ------------------------------- */
1716 int afp_getsrvrparms(AFPObj *obj, char *ibuf _U_, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
1717 {
1718     struct timeval  tv;
1719     struct stat     st;
1720     struct vol      *volume;
1721     char    *data;
1722     char        *namebuf;
1723     int         vcnt;
1724     size_t      len;
1725
1726     load_volumes(obj);
1727
1728     data = rbuf + 5;
1729     for ( vcnt = 0, volume = Volumes; volume; volume = volume->v_next ) {
1730         if (!(volume->v_flags & AFPVOL_NOSTAT)) {
1731             struct maccess ma;
1732
1733             if ( stat( volume->v_path, &st ) < 0 ) {
1734                 LOG(log_info, logtype_afpd, "afp_getsrvrparms(%s): stat: %s",
1735                     volume->v_path, strerror(errno) );
1736                 continue;       /* can't access directory */
1737             }
1738             if (!S_ISDIR(st.st_mode)) {
1739                 continue;       /* not a dir */
1740             }
1741             accessmode(volume->v_path, &ma, NULL, &st);
1742             if ((ma.ma_user & (AR_UREAD | AR_USEARCH)) != (AR_UREAD | AR_USEARCH)) {
1743                 continue;   /* no r-x access */
1744             }
1745         }
1746         if (volume->v_hide) {
1747             continue;       /* config file changed but the volume was mounted */
1748         }
1749
1750         if (utf8_encoding()) {
1751             len = ucs2_to_charset_allocate(CH_UTF8_MAC, &namebuf, volume->v_u8mname);
1752         } else {
1753             len = ucs2_to_charset_allocate(obj->options.maccharset, &namebuf, volume->v_macname);
1754         }
1755
1756         if (len == (size_t)-1)
1757             continue;
1758
1759         /* set password bit if there's a volume password */
1760         *data = (volume->v_password) ? AFPSRVR_PASSWD : 0;
1761
1762         /* Apple 2 clients running ProDOS-8 expect one volume to have
1763            bit 0 of this byte set.  They will not recognize anything
1764            on the server unless this is the case.  I have not
1765            completely worked this out, but it's related to booting
1766            from the server.  Support for that function is a ways
1767            off.. <shirsch@ibm.net> */
1768         *data |= (volume->v_flags & AFPVOL_A2VOL) ? AFPSRVR_CONFIGINFO : 0;
1769         *data++ |= 0; /* UNIX PRIVS BIT ..., OSX doesn't seem to use it, so we don't either */
1770         *data++ = len;
1771         memcpy(data, namebuf, len );
1772         data += len;
1773         free(namebuf);
1774         vcnt++;
1775     }
1776
1777     *rbuflen = data - rbuf;
1778     data = rbuf;
1779     if ( gettimeofday( &tv, NULL ) < 0 ) {
1780         LOG(log_error, logtype_afpd, "afp_getsrvrparms(%s): gettimeofday: %s", volume->v_path, strerror(errno) );
1781         *rbuflen = 0;
1782         return AFPERR_PARAM;
1783     }
1784     tv.tv_sec = AD_DATE_FROM_UNIX(tv.tv_sec);
1785     memcpy(data, &tv.tv_sec, sizeof( u_int32_t));
1786     data += sizeof( u_int32_t);
1787     *data = vcnt;
1788     return( AFP_OK );
1789 }
1790
1791 /* ------------------------- */
1792 static int volume_codepage(AFPObj *obj, struct vol *volume)
1793 {
1794     struct charset_functions *charset;
1795     /* Codepages */
1796
1797     if (!volume->v_volcodepage)
1798         volume->v_volcodepage = strdup("UTF8");
1799
1800     if ( (charset_t) -1 == ( volume->v_volcharset = add_charset(volume->v_volcodepage)) ) {
1801         LOG (log_error, logtype_afpd, "Setting codepage %s as volume codepage failed", volume->v_volcodepage);
1802         return -1;
1803     }
1804
1805     if ( NULL == (charset = find_charset_functions(volume->v_volcodepage)) || charset->flags & CHARSET_ICONV ) {
1806         LOG (log_warning, logtype_afpd, "WARNING: volume encoding %s is *not* supported by netatalk, expect problems !!!!", volume->v_volcodepage);
1807     }
1808
1809     if (!volume->v_maccodepage)
1810         volume->v_maccodepage = strdup(obj->options.maccodepage);
1811
1812     if ( (charset_t) -1 == ( volume->v_maccharset = add_charset(volume->v_maccodepage)) ) {
1813         LOG (log_error, logtype_afpd, "Setting codepage %s as mac codepage failed", volume->v_maccodepage);
1814         return -1;
1815     }
1816
1817     if ( NULL == ( charset = find_charset_functions(volume->v_maccodepage)) || ! (charset->flags & CHARSET_CLIENT) ) {
1818         LOG (log_error, logtype_afpd, "Fatal error: mac charset %s not supported", volume->v_maccodepage);
1819         return -1;
1820     }
1821     volume->v_kTextEncoding = htonl(charset->kTextEncoding);
1822     return 0;
1823 }
1824
1825 /* ------------------------- */
1826 static int volume_openDB(struct vol *volume)
1827 {
1828     int flags = 0;
1829
1830     if ((volume->v_flags & AFPVOL_NODEV)) {
1831         flags |= CNID_FLAG_NODEV;
1832     }
1833
1834     if (volume->v_cnidscheme == NULL) {
1835         volume->v_cnidscheme = strdup(DEFAULT_CNID_SCHEME);
1836         LOG(log_info, logtype_afpd, "Volume %s use CNID scheme %s.",
1837             volume->v_path, volume->v_cnidscheme);
1838     }
1839
1840     LOG(log_info, logtype_afpd, "CNID server %s:%s",
1841         volume->v_cnidserver ? volume->v_cnidserver : Cnid_srv,
1842         volume->v_cnidport ? volume->v_cnidport : Cnid_port);
1843     
1844     volume->v_cdb = cnid_open(volume->v_dbpath ? volume->v_dbpath : volume->v_path,
1845                               volume->v_umask,
1846                               volume->v_cnidscheme,
1847                               flags,
1848                               volume->v_cnidserver ? volume->v_cnidserver : Cnid_srv,
1849                               volume->v_cnidport ? volume->v_cnidport : Cnid_port);
1850
1851     if (!volume->v_cdb) {
1852         LOG(log_error, logtype_afpd, "Can't open volume \"%s\" CNID backend \"%s\" ",
1853             volume->v_path, volume->v_cnidscheme);
1854         flags |= CNID_FLAG_MEMORY;
1855         LOG(log_error, logtype_afpd, "Reopen volume %s using in memory temporary CNID DB.",
1856             volume->v_path);
1857         volume->v_cdb = cnid_open (volume->v_path, volume->v_umask, "tdb", flags, NULL, NULL);
1858 #ifdef SERVERTEXT
1859         /* kill ourself with SIGUSR2 aka msg pending */
1860         if (volume->v_cdb) {
1861             setmessage("Something wrong with the volume's CNID DB, using temporary CNID DB instead."
1862                        "Check server messages for details!");
1863             kill(getpid(), SIGUSR2);
1864             /* deactivate cnid caching/storing in AppleDouble files */
1865             volume->v_flags &= ~AFPVOL_CACHE;
1866         }
1867 #endif
1868     }
1869
1870     return (!volume->v_cdb)?-1:0;
1871 }
1872
1873 /* 
1874    Check if the underlying filesystem supports EAs for ea:sys volumes.
1875    If not, switch to ea:ad.
1876    As we can't check (requires write access) on ro-volumes, we switch ea:auto
1877    volumes that are options:ro to ea:none.
1878 */
1879 static void check_ea_sys_support(struct vol *vol)
1880 {
1881     uid_t process_uid = 0;
1882     char eaname[] = {"org.netatalk.supports-eas.XXXXXX"};
1883     const char *eacontent = "yes";
1884
1885     if (vol->v_vfs_ea == AFPVOL_EA_AUTO) {
1886
1887         if ((vol->v_flags & AFPVOL_RO) == AFPVOL_RO) {
1888             LOG(log_info, logtype_logger, "read-only volume '%s', can't test for EA support, disabling EAs", vol->v_localname);
1889             vol->v_vfs_ea = AFPVOL_EA_NONE;
1890             return;
1891         }
1892
1893         mktemp(eaname);
1894
1895         process_uid = geteuid();
1896         if (process_uid)
1897             if (seteuid(0) == -1) {
1898                 LOG(log_error, logtype_logger, "check_ea_sys_support: can't seteuid(0): %s", strerror(errno));
1899                 exit(EXITERR_SYS);
1900             }
1901
1902         if ((sys_setxattr(vol->v_path, eaname, eacontent, 4, 0)) == 0) {
1903             sys_removexattr(vol->v_path, eaname);
1904             vol->v_vfs_ea = AFPVOL_EA_SYS;
1905         } else {
1906             LOG(log_warning, logtype_afpd, "volume \"%s\" does not support Extended Attributes, using ea:ad instead",
1907                 vol->v_localname);
1908             vol->v_vfs_ea = AFPVOL_EA_AD;
1909         }
1910
1911         if (process_uid) {
1912             if (seteuid(process_uid) == -1) {
1913                 LOG(log_error, logtype_logger, "can't seteuid back %s", strerror(errno));
1914                 exit(EXITERR_SYS);
1915             }
1916         }
1917     }
1918 }
1919
1920 /* -------------------------
1921  * we are the user here
1922  */
1923 int afp_openvol(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
1924 {
1925     struct stat st;
1926     char    *volname;
1927     char        *p;
1928
1929     struct vol  *volume;
1930     struct dir  *dir;
1931     int     len, ret;
1932     size_t  namelen;
1933     u_int16_t   bitmap;
1934     char        path[ MAXPATHLEN + 1];
1935     char        *vol_uname;
1936     char        *vol_mname;
1937     char        *volname_tmp;
1938
1939     ibuf += 2;
1940     memcpy(&bitmap, ibuf, sizeof( bitmap ));
1941     bitmap = ntohs( bitmap );
1942     ibuf += sizeof( bitmap );
1943
1944     *rbuflen = 0;
1945     if (( bitmap & (1<<VOLPBIT_VID)) == 0 ) {
1946         return AFPERR_BITMAP;
1947     }
1948
1949     len = (unsigned char)*ibuf++;
1950     volname = obj->oldtmp;
1951
1952     if ((volname_tmp = strchr(volname,'+')) != NULL)
1953         volname = volname_tmp+1;
1954
1955     if (utf8_encoding()) {
1956         namelen = convert_string(CH_UTF8_MAC, CH_UCS2, ibuf, len, volname, sizeof(obj->oldtmp));
1957     } else {
1958         namelen = convert_string(obj->options.maccharset, CH_UCS2, ibuf, len, volname, sizeof(obj->oldtmp));
1959     }
1960
1961     if ( namelen <= 0) {
1962         return AFPERR_PARAM;
1963     }
1964
1965     ibuf += len;
1966     if ((len + 1) & 1) /* pad to an even boundary */
1967         ibuf++;
1968
1969     load_volumes(obj);
1970
1971     for ( volume = Volumes; volume; volume = volume->v_next ) {
1972         if ( strcasecmp_w( (ucs2_t*) volname, volume->v_name ) == 0 ) {
1973             break;
1974         }
1975     }
1976
1977     if ( volume == NULL ) {
1978         return AFPERR_PARAM;
1979     }
1980
1981     /* check for a volume password */
1982     if (volume->v_password && strncmp(ibuf, volume->v_password, VOLPASSLEN)) {
1983         return AFPERR_ACCESS;
1984     }
1985
1986     if (( volume->v_flags & AFPVOL_OPEN  ) ) {
1987         /* the volume is already open */
1988 #ifdef FORCE_UIDGID
1989         set_uidgid ( volume );
1990 #endif
1991         return stat_vol(bitmap, volume, rbuf, rbuflen);
1992     }
1993
1994     if (volume->v_root_preexec) {
1995         if ((ret = afprun(1, volume->v_root_preexec, NULL)) && volume->v_root_preexec_close) {
1996             LOG(log_error, logtype_afpd, "afp_openvol(%s): root preexec : %d", volume->v_path, ret );
1997             return AFPERR_MISC;
1998         }
1999     }
2000
2001 #ifdef FORCE_UIDGID
2002     set_uidgid ( volume );
2003 #endif
2004
2005     if (volume->v_preexec) {
2006         if ((ret = afprun(0, volume->v_preexec, NULL)) && volume->v_preexec_close) {
2007             LOG(log_error, logtype_afpd, "afp_openvol(%s): preexec : %d", volume->v_path, ret );
2008             return AFPERR_MISC;
2009         }
2010     }
2011
2012     if ( stat( volume->v_path, &st ) < 0 ) {
2013         return AFPERR_PARAM;
2014     }
2015
2016     if ( chdir( volume->v_path ) < 0 ) {
2017         return AFPERR_PARAM;
2018     }
2019
2020     if ( NULL == getcwd(path, MAXPATHLEN)) {
2021         /* shouldn't be fatal but it will fail later */
2022         LOG(log_error, logtype_afpd, "afp_openvol(%s): volume pathlen too long", volume->v_path);
2023         return AFPERR_MISC;
2024     }
2025
2026     /* Normalize volume path */
2027 #ifdef REALPATH_TAKES_NULL
2028     if ((volume->v_path = realpath(path, NULL)) == NULL)
2029         return AFPERR_MISC;
2030 #else
2031     if ((volume->v_path = malloc(MAXPATHLEN+1)) == NULL)
2032         return AFPERR_MISC;
2033     if (realpath(path, volume->v_path) == NULL) {
2034         free(volume->v_path);
2035         return AFPERR_MISC;
2036     }
2037     /* Safe some memory */
2038     char *tmp;
2039     if ((tmp = strdup(volume->v_path)) == NULL) {
2040         free(volume->v_path);
2041         return AFPERR_MISC;
2042     } 
2043     free(volume->v_path);
2044     volume->v_path = tmp;
2045 #endif
2046
2047     if (volume_codepage(obj, volume) < 0) {
2048         ret = AFPERR_MISC;
2049         goto openvol_err;
2050     }
2051
2052     /* initialize volume variables
2053      * FIXME file size
2054      */
2055     if (utf8_encoding()) {
2056         volume->max_filename = 255;
2057     }
2058     else {
2059         volume->max_filename = MACFILELEN;
2060     }
2061
2062     volume->v_dir = volume->v_root = NULL;
2063     volume->v_hash = NULL;
2064
2065     volume->v_flags |= AFPVOL_OPEN;
2066     volume->v_cdb = NULL;
2067
2068     if (utf8_encoding()) {
2069         len = convert_string_allocate(CH_UCS2, CH_UTF8_MAC, volume->v_u8mname, namelen, &vol_mname);
2070     } else {
2071         len = convert_string_allocate(CH_UCS2, obj->options.maccharset, volume->v_macname, namelen, &vol_mname);
2072     }
2073     if ( !vol_mname || len <= 0) {
2074         ret = AFPERR_MISC;
2075         goto openvol_err;
2076     }
2077
2078     if ((vol_uname = strrchr(path, '/')) == NULL)
2079         vol_uname = path;
2080     else if (*(vol_uname + 1) != '\0')
2081         vol_uname++;
2082
2083     if ((dir = dirnew(vol_mname, vol_uname) ) == NULL) {
2084         free(vol_mname);
2085         LOG(log_error, logtype_afpd, "afp_openvol(%s): malloc: %s", volume->v_path, strerror(errno) );
2086         ret = AFPERR_MISC;
2087         goto openvol_err;
2088     }
2089     free(vol_mname);
2090
2091     dir->d_did = DIRDID_ROOT;
2092     dir->d_color = DIRTREE_COLOR_BLACK; /* root node is black */
2093     dir->d_m_name_ucs2 = strdup_w(volume->v_name);
2094     volume->v_dir = volume->v_root = dir;
2095     volume->v_curdir = NULL;
2096     volume->v_hash = dirhash();
2097
2098     curdir = volume->v_dir;
2099     if (volume_openDB(volume) < 0) {
2100         LOG(log_error, logtype_afpd, "Fatal error: cannot open CNID or invalid CNID backend for %s: %s",
2101             volume->v_path, volume->v_cnidscheme);
2102         ret = AFPERR_MISC;
2103         goto openvol_err;
2104     }
2105
2106     ret  = stat_vol(bitmap, volume, rbuf, rbuflen);
2107     if (ret == AFP_OK) {
2108
2109         if (!(volume->v_flags & AFPVOL_RO)) {
2110             handle_special_folders( volume );
2111             savevolinfo(volume,
2112                         volume->v_cnidserver ? volume->v_cnidserver : Cnid_srv,
2113                         volume->v_cnidport   ? volume->v_cnidport   : Cnid_port);
2114         }
2115
2116         /*
2117          * If you mount a volume twice, the second time the trash appears on
2118          * the desk-top.  That's because the Mac remembers the DID for the
2119          * trash (even for volumes in different zones, on different servers).
2120          * Just so this works better, we prime the DID cache with the trash,
2121          * fixing the trash at DID 17.
2122          * FIXME (RL): should it be done inside a CNID backend ? (always returning Trash DID when asked) ?
2123          */
2124         if ((volume->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
2125
2126             /* FIXME find db time stamp */
2127             if (cnid_getstamp(volume->v_cdb, volume->v_stamp, sizeof(volume->v_stamp)) < 0) {
2128                 LOG (log_error, logtype_afpd,
2129                      "afp_openvol(%s): Fatal error: Unable to get stamp value from CNID backend",
2130                      volume->v_path);
2131                 ret = AFPERR_MISC;
2132                 goto openvol_err;
2133             }
2134         }
2135         else {
2136             p = Trash;
2137             cname( volume, volume->v_dir, &p );
2138         }
2139         return( AFP_OK );
2140     }
2141
2142 openvol_err:
2143     if (volume->v_dir) {
2144         hash_free( volume->v_hash);
2145         dirfree( volume->v_dir );
2146         volume->v_dir = volume->v_root = NULL;
2147     }
2148
2149     volume->v_flags &= ~AFPVOL_OPEN;
2150     if (volume->v_cdb != NULL) {
2151         cnid_close(volume->v_cdb);
2152         volume->v_cdb = NULL;
2153     }
2154     *rbuflen = 0;
2155     return ret;
2156 }
2157
2158 /* ------------------------- */
2159 static void closevol(struct vol *vol)
2160 {
2161     if (!vol)
2162         return;
2163
2164     hash_free( vol->v_hash);
2165     dirfree( vol->v_root );
2166     vol->v_dir = NULL;
2167     if (vol->v_cdb != NULL) {
2168         cnid_close(vol->v_cdb);
2169         vol->v_cdb = NULL;
2170     }
2171
2172     if (vol->v_postexec) {
2173         afprun(0, vol->v_postexec, NULL);
2174     }
2175     if (vol->v_root_postexec) {
2176         afprun(1, vol->v_root_postexec, NULL);
2177     }
2178 }
2179
2180 /* ------------------------- */
2181 void close_all_vol(void)
2182 {
2183     struct vol  *ovol;
2184     curdir = NULL;
2185     for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
2186         if ( (ovol->v_flags & AFPVOL_OPEN) ) {
2187             ovol->v_flags &= ~AFPVOL_OPEN;
2188             closevol(ovol);
2189         }
2190     }
2191 }
2192
2193 /* ------------------------- */
2194 static void deletevol(struct vol *vol)
2195 {
2196     struct vol  *ovol;
2197
2198     vol->v_flags &= ~AFPVOL_OPEN;
2199     for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
2200         if ( (ovol->v_flags & AFPVOL_OPEN) ) {
2201             break;
2202         }
2203     }
2204     if ( ovol != NULL ) {
2205         /* Even if chdir fails, we can't say afp_closevol fails. */
2206         if ( chdir( ovol->v_path ) == 0 ) {
2207             curdir = ovol->v_dir;
2208         }
2209     }
2210
2211     closevol(vol);
2212     if (vol->v_deleted) {
2213         showvol(vol->v_name);
2214         volume_free(vol);
2215         volume_unlink(vol);
2216         free(vol);
2217     }
2218 }
2219
2220 /* ------------------------- */
2221 int afp_closevol(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2222 {
2223     struct vol  *vol;
2224     u_int16_t   vid;
2225
2226     *rbuflen = 0;
2227     ibuf += 2;
2228     memcpy(&vid, ibuf, sizeof( vid ));
2229     if (NULL == ( vol = getvolbyvid( vid )) ) {
2230         return( AFPERR_PARAM );
2231     }
2232
2233     deletevol(vol);
2234
2235     return( AFP_OK );
2236 }
2237
2238 /* ------------------------- */
2239 struct vol *getvolbyvid(const u_int16_t vid )
2240 {
2241     struct vol  *vol;
2242
2243     for ( vol = Volumes; vol; vol = vol->v_next ) {
2244         if ( vid == vol->v_vid ) {
2245             break;
2246         }
2247     }
2248     if ( vol == NULL || ( vol->v_flags & AFPVOL_OPEN ) == 0 ) {
2249         return( NULL );
2250     }
2251
2252 #ifdef FORCE_UIDGID
2253     set_uidgid ( vol );
2254 #endif /* FORCE_UIDGID */
2255
2256     return( vol );
2257 }
2258
2259 /* ------------------------ */
2260 static int ext_cmp_key(const void *key, const void *obj)
2261 {
2262     const char          *p = key;
2263     const struct extmap *em = obj;
2264     return strdiacasecmp(p, em->em_ext);
2265 }
2266 struct extmap *getextmap(const char *path)
2267 {
2268     char      *p;
2269     struct extmap *em;
2270
2271     if (!Extmap_cnt || NULL == ( p = strrchr( path, '.' )) ) {
2272         return( Defextmap );
2273     }
2274     p++;
2275     if (!*p) {
2276         return( Defextmap );
2277     }
2278     em = bsearch(p, Extmap, Extmap_cnt, sizeof(struct extmap), ext_cmp_key);
2279     if (em) {
2280         return( em );
2281     } else {
2282         return( Defextmap );
2283     }
2284 }
2285
2286 /* ------------------------- */
2287 struct extmap *getdefextmap(void)
2288 {
2289     return( Defextmap );
2290 }
2291
2292 /* --------------------------
2293    poll if a volume is changed by other processes.
2294    return
2295    0 no attention msg sent
2296    1 attention msg sent
2297    -1 error (socket closed)
2298
2299    Note: if attention return -1 no packet has been
2300    sent because the buffer is full, we don't care
2301    either there's no reader or there's a lot of
2302    traffic and another pollvoltime will follow
2303 */
2304 int  pollvoltime(AFPObj *obj)
2305
2306 {
2307     struct vol       *vol;
2308     struct timeval   tv;
2309     struct stat      st;
2310
2311     if (!(afp_version > 21 && obj->options.server_notif))
2312         return 0;
2313
2314     if ( gettimeofday( &tv, NULL ) < 0 )
2315         return 0;
2316
2317     for ( vol = Volumes; vol; vol = vol->v_next ) {
2318         if ( (vol->v_flags & AFPVOL_OPEN)  && vol->v_mtime + 30 < tv.tv_sec) {
2319             if ( !stat( vol->v_path, &st ) && vol->v_mtime != st.st_mtime ) {
2320                 vol->v_mtime = st.st_mtime;
2321                 if (!obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED))
2322                     return -1;
2323                 return 1;
2324             }
2325         }
2326     }
2327     return 0;
2328 }
2329
2330 /* ------------------------- */
2331 void setvoltime(AFPObj *obj, struct vol *vol)
2332 {
2333     struct timeval  tv;
2334
2335     /* just looking at vol->v_mtime is broken seriously since updates
2336      * from other users afpd processes never are seen.
2337      * This is not the most elegant solution (a shared memory between
2338      * the afpd processes would come closer)
2339      * [RS] */
2340
2341     if ( gettimeofday( &tv, NULL ) < 0 ) {
2342         LOG(log_error, logtype_afpd, "setvoltime(%s): gettimeofday: %s", vol->v_path, strerror(errno) );
2343         return;
2344     }
2345     if( utime( vol->v_path, NULL ) < 0 ) {
2346         /* write of time failed ... probably a read only filesys,
2347          * where no other users can interfere, so there's no issue here
2348          */
2349     }
2350
2351     /* a little granularity */
2352     if (vol->v_mtime < tv.tv_sec) {
2353         vol->v_mtime = tv.tv_sec;
2354         /* or finder doesn't update free space
2355          * AFP 3.2 and above clients seem to be ok without so many notification
2356          */
2357         if (afp_version < 32 && obj->options.server_notif) {
2358             obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED);
2359         }
2360     }
2361 }
2362
2363 /* ------------------------- */
2364 int afp_getvolparams(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_,char *rbuf, size_t *rbuflen)
2365 {
2366     struct vol  *vol;
2367     u_int16_t   vid, bitmap;
2368
2369     ibuf += 2;
2370     memcpy(&vid, ibuf, sizeof( vid ));
2371     ibuf += sizeof( vid );
2372     memcpy(&bitmap, ibuf, sizeof( bitmap ));
2373     bitmap = ntohs( bitmap );
2374
2375     if (NULL == ( vol = getvolbyvid( vid )) ) {
2376         *rbuflen = 0;
2377         return( AFPERR_PARAM );
2378     }
2379
2380     return stat_vol(bitmap, vol, rbuf, rbuflen);
2381 }
2382
2383 /* ------------------------- */
2384 int afp_setvolparams(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_,  size_t *rbuflen)
2385 {
2386     struct adouble ad;
2387     struct vol  *vol;
2388     u_int16_t   vid, bitmap;
2389     u_int32_t   aint;
2390
2391     ibuf += 2;
2392     *rbuflen = 0;
2393
2394     memcpy(&vid, ibuf, sizeof( vid ));
2395     ibuf += sizeof( vid );
2396     memcpy(&bitmap, ibuf, sizeof( bitmap ));
2397     bitmap = ntohs( bitmap );
2398     ibuf += sizeof(bitmap);
2399
2400     if (( vol = getvolbyvid( vid )) == NULL ) {
2401         return( AFPERR_PARAM );
2402     }
2403
2404     if ((vol->v_flags & AFPVOL_RO))
2405         return AFPERR_VLOCK;
2406
2407     /* we can only set the backup date. */
2408     if (bitmap != (1 << VOLPBIT_BDATE))
2409         return AFPERR_BITMAP;
2410
2411     ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2412     if ( ad_open( vol->v_path, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR,
2413                   0666, &ad) < 0 ) {
2414         if (errno == EROFS)
2415             return AFPERR_VLOCK;
2416
2417         return AFPERR_ACCESS;
2418     }
2419
2420     memcpy(&aint, ibuf, sizeof(aint));
2421     ad_setdate(&ad, AD_DATE_BACKUP, aint);
2422     ad_flush(&ad);
2423     ad_close(&ad, ADFLAGS_HF);
2424     return( AFP_OK );
2425 }
2426
2427 /* ------------------------- */
2428 int wincheck(const struct vol *vol, const char *path)
2429 {
2430     int len;
2431
2432     if (!(vol->v_flags & AFPVOL_MSWINDOWS))
2433         return 1;
2434
2435     /* empty paths are not allowed */
2436     if ((len = strlen(path)) == 0)
2437         return 0;
2438
2439     /* leading or trailing whitespaces are not allowed, carriage returns
2440      * and probably other whitespace is okay, tabs are not allowed
2441      */
2442     if ((path[0] == ' ') || (path[len-1] == ' '))
2443         return 0;
2444
2445     /* certain characters are not allowed */
2446     if (strpbrk(path, MSWINDOWS_BADCHARS))
2447         return 0;
2448
2449     /* everything else is okay */
2450     return 1;
2451 }
2452
2453
2454 /*
2455  * precreate a folder
2456  * this is only intended for folders in the volume root
2457  * It will *not* work if the folder name contains extended characters
2458  */
2459 static int create_special_folder (const struct vol *vol, const struct _special_folder *folder)
2460 {
2461     char        *p,*q,*r;
2462     struct adouble  ad;
2463     u_int16_t   attr;
2464     struct stat st;
2465     int     ret;
2466
2467
2468     p = (char *) malloc ( strlen(vol->v_path)+strlen(folder->name)+2);
2469     if ( p == NULL) {
2470         LOG(log_error, logtype_afpd,"malloc failed");
2471         exit (EXITERR_SYS);
2472     }
2473
2474     q=strdup(folder->name);
2475     if ( q == NULL) {
2476         LOG(log_error, logtype_afpd,"malloc failed");
2477         exit (EXITERR_SYS);
2478     }
2479
2480     strcpy(p, vol->v_path);
2481     strcat(p, "/");
2482
2483     r=q;
2484     while (*r) {
2485         if ((vol->v_casefold & AFPVOL_MTOUUPPER))
2486             *r=toupper(*r);
2487         else if ((vol->v_casefold & AFPVOL_MTOULOWER))
2488             *r=tolower(*r);
2489         r++;
2490     }
2491     strcat(p, q);
2492
2493     if ( (ret = stat( p, &st )) < 0 ) {
2494         if (folder->precreate) {
2495             if (ad_mkdir(p, folder->mode)) {
2496                 LOG(log_debug, logtype_afpd,"Creating '%s' failed in %s: %s", p, vol->v_path, strerror(errno));
2497                 free(p);
2498                 free(q);
2499                 return -1;
2500             }
2501             ret = 0;
2502         }
2503     }
2504
2505     if ( !ret && folder->hide) {
2506         /* Hide it */
2507         ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2508         if (ad_open_metadata( p, ADFLAGS_DIR, O_CREAT, &ad) < 0) {
2509             free (p);
2510             free(q);
2511             return (-1);
2512         }
2513         
2514         ad_setname(&ad, folder->name);
2515
2516         ad_getattr(&ad, &attr);
2517         attr |= htons( ntohs( attr ) | ATTRBIT_INVISIBLE );
2518         ad_setattr(&ad, attr);
2519
2520         /* do the same with the finder info */
2521         if (ad_entry(&ad, ADEID_FINDERI)) {
2522             memcpy(&attr, ad_entry(&ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF, sizeof(attr));
2523             attr   |= htons(FINDERINFO_INVISIBLE);
2524             memcpy(ad_entry(&ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF,&attr, sizeof(attr));
2525         }
2526
2527         ad_flush( &ad );
2528         ad_close_metadata( &ad);
2529     }
2530     free(p);
2531     free(q);
2532     return 0;
2533 }
2534
2535 static void handle_special_folders (const struct vol * vol)
2536 {
2537     const _special_folder *p = &special_folders[0];
2538
2539     if ((vol->v_flags & AFPVOL_RO))
2540         return;
2541
2542     for (; p->name != NULL; p++) {
2543         create_special_folder (vol, p);
2544     }
2545 }
2546