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