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