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