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