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