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