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