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