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