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