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