]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/volume.c
Sync volume options with volinfo stuff
[netatalk.git] / etc / afpd / volume.c
1 /*
2  * $Id: volume.c,v 1.85 2009-04-27 07:58:25 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/logger.h>
47 #ifdef CNID_DB
48 #include <atalk/cnid.h>
49 #endif /* CNID_DB*/
50
51 #include "globals.h"
52 #include "directory.h"
53 #include "file.h"
54 #include "volume.h"
55 #include "unix.h"
56 #include "mangle.h"
57 #include "fork.h"
58
59 extern int afprun(int root, char *cmd, int *outfd);
60
61 #ifndef MIN
62 #define MIN(a, b) ((a) < (b) ? (a) : (b))
63 #endif /* ! MIN */
64
65 #ifndef NO_LARGE_VOL_SUPPORT
66 #if BYTE_ORDER == BIG_ENDIAN
67 #define hton64(x)       (x)
68 #define ntoh64(x)       (x)
69 #else /* BYTE_ORDER == BIG_ENDIAN */
70 #define hton64(x)       ((u_int64_t) (htonl(((x) >> 32) & 0xffffffffLL)) | \
71                          (u_int64_t) ((htonl(x) & 0xffffffffLL) << 32))
72 #define ntoh64(x)       (hton64(x))
73 #endif /* BYTE_ORDER == BIG_ENDIAN */
74 #endif /* ! NO_LARGE_VOL_SUPPORT */
75
76 static struct vol *Volumes = NULL;
77 static u_int16_t        lastvid = 0;
78 static char             *Trash = "\02\024Network Trash Folder";
79
80 static struct extmap    *Extmap = NULL, *Defextmap = NULL;
81 static int              Extmap_cnt;
82 static void             free_extmap(void);
83
84 #define VOLOPT_ALLOW      0  /* user allow list */
85 #define VOLOPT_DENY       1  /* user deny list */
86 #define VOLOPT_RWLIST     2  /* user rw list */
87 #define VOLOPT_ROLIST     3  /* user ro list */
88 #define VOLOPT_PASSWORD   4  /* volume password */
89 #define VOLOPT_CASEFOLD   5  /* character case mangling */
90 #define VOLOPT_FLAGS      6  /* various flags */
91 #define VOLOPT_DBPATH     7  /* path to database */
92 #define VOLOPT_MAPCHARS   8  /* does mtou and utom mappings. syntax:
93 m and u can be double-byte hex
94 strings if necessary.
95 m=u -> map both ways
96   m>u -> map m to u
97   m<u -> map u to m
98   !u  -> make u illegal always
99   ~u  -> make u illegal only as the first
100   part of a double-byte character.
101   */
102 #define VOLOPT_VETO          10  /* list of veto filespec */
103 #define VOLOPT_PREEXEC       11  /* preexec command */
104 #define VOLOPT_ROOTPREEXEC   12  /* root preexec command */
105
106 #define VOLOPT_POSTEXEC      13  /* postexec command */
107 #define VOLOPT_ROOTPOSTEXEC  14  /* root postexec command */
108
109 #define VOLOPT_ENCODING      15  /* mac encoding (pre OSX)*/
110 #define VOLOPT_MACCHARSET    16
111 #define VOLOPT_CNIDSCHEME    17
112 #define VOLOPT_ADOUBLE       18  /* adouble version */
113 #ifdef FORCE_UIDGID
114 #warning UIDGID
115 #include "uid.h"
116
117 #define VOLOPT_FORCEUID  19  /* force uid for username x */
118 #define VOLOPT_FORCEGID  20  /* force gid for group x */
119 #endif /* FORCE_UIDGID */
120
121 #define VOLOPT_UMASK     21
122 #define VOLOPT_ALLOWED_HOSTS 22
123 #define VOLOPT_DENIED_HOSTS  23
124 #define VOLOPT_DPERM     24  /* dperm default directories perms */
125 #define VOLOPT_FPERM     25  /* fperm default files perms */
126 #define VOLOPT_DFLTPERM  26  /* perm */
127
128 #define VOLOPT_MAX       (VOLOPT_DFLTPERM +1)
129
130 #define VOLOPT_NUM        (VOLOPT_MAX + 1)
131
132 #define VOLPASSLEN  8
133 #define VOLOPT_DEFAULT     ":DEFAULT:"
134 #define VOLOPT_DEFAULT_LEN 9
135   struct vol_option {
136       char *c_value;
137       int i_value;
138   };
139
140 typedef struct _special_folder {
141         const char *name;
142         int precreate;
143         mode_t mode;
144         int hide;
145 } _special_folder;
146
147 static const _special_folder special_folders[] = {
148   {"Network Trash Folder",     1,  0777,  1},
149   {"Temporary Items",          1,  0777,  1},
150   {".AppleDesktop",            1,  0777,  0},
151 #if 0
152   {"TheFindByContentFolder",   0,     0,  1},
153   {"TheVolumeSettingsFolder",  0,     0,  1},
154 #endif
155   {NULL, 0, 0, 0}};
156
157 typedef struct _volopt_name {
158         const u_int32_t option;
159         const char      *name;
160 } _vol_opt_name;
161
162
163 /* Keep in sync with libatalk/util/volinfo.c ! */
164 static const _vol_opt_name vol_opt_names[] = {
165     {AFPVOL_A2VOL,      "PRODOS"},      /* prodos volume */
166     {AFPVOL_CRLF,       "CRLF"},        /* cr/lf translation */
167     {AFPVOL_NOADOUBLE,  "NOADOUBLE"},   /* don't create .AppleDouble by default */
168     {AFPVOL_RO,         "READONLY"},    /* read-only volume */
169     {AFPVOL_MSWINDOWS,  "MSWINDOWS"},   /* deal with ms-windows yuckiness. this is going away. */
170     {AFPVOL_NOHEX,      "NOHEX"},       /* don't do :hex translation */
171     {AFPVOL_USEDOTS,    "USEDOTS"},     /* use real dots */
172     {AFPVOL_LIMITSIZE,  "LIMITSIZE"},   /* limit size for older macs */
173     {AFPVOL_MAPASCII,   "MAPASCII"},    /* map the ascii range as well */
174     {AFPVOL_DROPBOX,    "DROPBOX"},     /* dropkludge dropbox support */
175     {AFPVOL_NOFILEID,   "NOFILEID"},    /* don't advertise createid resolveid and deleteid calls */
176     {AFPVOL_NOSTAT,     "NOSTAT"},      /* advertise the volume even if we can't stat() it
177                                          * maybe because it will be mounted later in preexec */
178     {AFPVOL_UNIX_PRIV,  "UNIXPRIV"},    /* support unix privileges */
179     {AFPVOL_NODEV,      "NODEV"},       /* always use 0 for device number in cnid calls */
180     {AFPVOL_CASEINSEN,  "CASEINSENSITIVE"}, /* volume is case insensitive */
181     {AFPVOL_EILSEQ,     "ILLEGALSEQ"},     /* encode illegal sequence */
182     {AFPVOL_CACHE,      "CACHEID"},     /* Use adouble v2 CNID caching, default don't use it */
183     {AFPVOL_EXT_ATTRS,  "EXT_ATTRS"},   /* Vol supports Extened Attributes */
184     {AFPVOL_ACLS,       "ACLS"},        /* Vol supports ACLs */
185     {0, NULL}
186 };
187
188 static const _vol_opt_name vol_opt_casefold[] = {
189     {AFPVOL_MTOUUPPER,  "MTOULOWER"},
190     {AFPVOL_MTOULOWER,  "MTOULOWER"},
191     {AFPVOL_UTOMUPPER,  "UTOMUPPER"},
192     {AFPVOL_UTOMLOWER,  "UTOMLOWER"},
193     {0, NULL}
194 };
195
196 static void handle_special_folders (const struct vol *);
197 static int savevoloptions (const struct vol *);
198 static void deletevol(struct vol *vol);
199 static void volume_free(struct vol *vol);
200
201 static void volfree(struct vol_option *options,
202                                const struct vol_option *save)
203 {
204     int i;
205
206     if (save) {
207         for (i = 0; i < VOLOPT_MAX; i++) {
208             if (options[i].c_value && (options[i].c_value != save[i].c_value))
209                 free(options[i].c_value);
210         }
211     } else {
212         for (i = 0; i < VOLOPT_MAX; i++) {
213             if (options[i].c_value)
214                 free(options[i].c_value);
215         }
216     }
217 }
218
219
220 /* handle variable substitutions. here's what we understand:
221  * $b   -> basename of path
222  * $c   -> client ip/appletalk address
223  * $d   -> volume pathname on server
224  * $f   -> full name (whatever's in the gecos field)
225  * $g   -> group
226  * $h   -> hostname 
227  * $i   -> client ip/appletalk address without port
228  * $s   -> server name (hostname if it doesn't exist)
229  * $u   -> username (guest is usually nobody)
230  * $v   -> volume name or basename if null
231  * $z   -> zone (may not exist)
232  * $$   -> $
233  *
234  *
235  */
236 #define is_var(a, b) (strncmp((a), (b), 2) == 0)
237
238 static char *volxlate(AFPObj *obj, char *dest, size_t destlen,
239                      char *src, struct passwd *pwd, char *path, char *volname)
240 {
241     char *p, *q;
242     int len;
243     char *ret;
244     
245     if (!src) {
246         return NULL;
247     }
248     if (!dest) {
249         dest = calloc(destlen +1, 1);
250     }
251     ret = dest;
252     if (!ret) {
253         return NULL;
254     }
255     strlcpy(dest, src, destlen +1);
256     if ((p = strchr(src, '$')) == NULL) /* nothing to do */
257         return ret;
258
259     /* first part of the path. just forward to the next variable. */
260     len = MIN((size_t)(p - src), destlen);
261     if (len > 0) {
262         destlen -= len;
263         dest += len;
264     }
265
266     while (p && destlen > 0) {
267         /* now figure out what the variable is */
268         q = NULL;
269         if (is_var(p, "$b")) {
270             if (path) {
271                 if ((q = strrchr(path, '/')) == NULL)
272                     q = path;
273                 else if (*(q + 1) != '\0')
274                     q++;
275             }
276         } else if (is_var(p, "$c")) {
277             if (obj->proto == AFPPROTO_ASP) {
278                 ASP asp = obj->handle;
279
280                 len = sprintf(dest, "%u.%u", ntohs(asp->asp_sat.sat_addr.s_net),
281                               asp->asp_sat.sat_addr.s_node);
282                 dest += len;
283                 destlen -= len;
284
285             } else if (obj->proto == AFPPROTO_DSI) {
286                 DSI *dsi = obj->handle;
287
288                 len = sprintf(dest, "%s:%u", inet_ntoa(dsi->client.sin_addr),
289                               ntohs(dsi->client.sin_port));
290                 dest += len;
291                 destlen -= len;
292             }
293         } else if (is_var(p, "$d")) {
294              q = path;
295         } else if (is_var(p, "$f")) {
296             if ((q = strchr(pwd->pw_gecos, ',')))
297                 *q = '\0';
298             q = pwd->pw_gecos;
299         } else if (is_var(p, "$g")) {
300             struct group *grp = getgrgid(pwd->pw_gid);
301             if (grp)
302                 q = grp->gr_name;
303         } else if (is_var(p, "$h")) {
304             q = obj->options.hostname;
305         } else if (is_var(p, "$i")) {
306             if (obj->proto == AFPPROTO_ASP) {
307                 ASP asp = obj->handle;
308  
309                 len = sprintf(dest, "%u", ntohs(asp->asp_sat.sat_addr.s_net));
310                 dest += len;
311                 destlen -= len;
312  
313             } else if (obj->proto == AFPPROTO_DSI) {
314                 DSI *dsi = obj->handle;
315  
316                 q = inet_ntoa(dsi->client.sin_addr);
317             }
318         } else if (is_var(p, "$s")) {
319             if (obj->Obj)
320                 q = obj->Obj;
321             else if (obj->options.server) {
322                 q = obj->options.server;
323             } else
324                 q = obj->options.hostname;
325         } else if (is_var(p, "$u")) {
326             char* sep = NULL;
327             if ( obj->options.ntseparator && (sep = strchr(obj->username, obj->options.ntseparator[0])) != NULL)
328                 q = sep+1;
329             else
330                 q = obj->username;
331         } else if (is_var(p, "$v")) {
332             if (volname) {
333                 q = volname;
334             }
335             else if (path) {
336                 if ((q = strrchr(path, '/')) == NULL)
337                     q = path;
338                 else if (*(q + 1) != '\0')
339                     q++;
340             }
341         } else if (is_var(p, "$z")) {
342             q = obj->Zone;
343         } else if (is_var(p, "$$")) {
344             q = "$";
345         } else
346             q = p;
347
348         /* copy the stuff over. if we don't understand something that we
349          * should, just skip it over. */
350         if (q) {
351             len = MIN(p == q ? 2 : strlen(q), destlen);
352             strncpy(dest, q, len);
353             dest += len;
354             destlen -= len;
355         }
356
357         /* stuff up to next $ */
358         src = p + 2;
359         p = strchr(src, '$');
360         len = p ? MIN((size_t)(p - src), destlen) : destlen;
361         if (len > 0) {
362             strncpy(dest, src, len);
363             dest += len;
364             destlen -= len;
365         }
366     }
367     return ret;
368 }
369
370 /* to make sure that val is valid, make sure to select an opt that
371    includes val */
372 static int optionok(const char *buf, const char *opt, const char *val) 
373 {
374     if (!strstr(buf,opt))
375         return 0;
376     if (!val[1])
377         return 0;
378     return 1;    
379 }
380
381
382 /* -------------------- */
383 static void setoption(struct vol_option *options, struct vol_option *save, int opt, const char *val)
384 {
385     if (options[opt].c_value && (!save || options[opt].c_value != save[opt].c_value))
386         free(options[opt].c_value);
387     options[opt].c_value = strdup(val + 1);
388 }
389
390 /* ------------------------------------------
391    handle all the options. tmp can't be NULL. */
392 static void volset(struct vol_option *options, struct vol_option *save, 
393                    char *volname, int vlen,
394                    const char *tmp)
395 {
396     char *val;
397
398     val = strchr(tmp, ':');
399     if (!val) {
400         /* we'll assume it's a volume name. */
401         strncpy(volname, tmp, vlen);
402         volname[vlen] = 0;
403         return;
404     }
405 #if 0
406     LOG(log_debug, logtype_afpd, "Parsing volset %s", val);
407 #endif
408     if (optionok(tmp, "allow:", val)) {
409         setoption(options, save, VOLOPT_ALLOW, val);
410
411     } else if (optionok(tmp, "deny:", val)) {
412         setoption(options, save, VOLOPT_DENY, val);
413
414     } else if (optionok(tmp, "rwlist:", val)) {
415         setoption(options, save, VOLOPT_RWLIST, val);
416
417     } else if (optionok(tmp, "rolist:", val)) {
418         setoption(options, save, VOLOPT_ROLIST, val);
419
420     } else if (optionok(tmp, "codepage:", val)) {
421         LOG (log_error, logtype_afpd, "The old codepage system has been removed. Please make sure to read the documentation !!!!");
422         /* Make sure we don't screw anything */
423         exit (EXITERR_CONF);
424     } else if (optionok(tmp, "volcharset:", val)) {
425         setoption(options, save, VOLOPT_ENCODING, val);
426     } else if (optionok(tmp, "maccharset:", val)) {
427         setoption(options, save, VOLOPT_MACCHARSET, val);
428     } else if (optionok(tmp, "veto:", val)) {
429         setoption(options, save, VOLOPT_VETO, val);
430     } else if (optionok(tmp, "cnidscheme:", val)) {
431         setoption(options, save, VOLOPT_CNIDSCHEME, val);
432     } else if (optionok(tmp, "casefold:", val)) {
433         if (strcasecmp(val + 1, "tolower") == 0)
434             options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMLOWER;
435         else if (strcasecmp(val + 1, "toupper") == 0)
436             options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMUPPER;
437         else if (strcasecmp(val + 1, "xlatelower") == 0)
438             options[VOLOPT_CASEFOLD].i_value = AFPVOL_UUPPERMLOWER;
439         else if (strcasecmp(val + 1, "xlateupper") == 0)
440             options[VOLOPT_CASEFOLD].i_value = AFPVOL_ULOWERMUPPER;
441     } else if (optionok(tmp, "adouble:", val)) {
442         if (strcasecmp(val + 1, "v1") == 0)
443             options[VOLOPT_ADOUBLE].i_value = AD_VERSION1;
444 #if AD_VERSION == AD_VERSION2            
445         else if (strcasecmp(val + 1, "v2") == 0)
446             options[VOLOPT_ADOUBLE].i_value = AD_VERSION2;
447         else if (strcasecmp(val + 1, "osx") == 0)
448             options[VOLOPT_ADOUBLE].i_value = AD_VERSION2_OSX;
449         else if (strcasecmp(val + 1, "ads") == 0)
450             options[VOLOPT_ADOUBLE].i_value = AD_VERSION1_ADS;
451         else if (strcasecmp(val + 1, "sfm") == 0)
452             options[VOLOPT_ADOUBLE].i_value = AD_VERSION1_SFM;
453 #endif
454     } else if (optionok(tmp, "options:", val)) {
455         char *p;
456
457         if ((p = strtok(val + 1, ",")) == NULL) /* nothing */
458             return;
459
460         while (p) {
461             if (strcasecmp(p, "prodos") == 0)
462                 options[VOLOPT_FLAGS].i_value |= AFPVOL_A2VOL;
463             else if (strcasecmp(p, "mswindows") == 0) {
464                 options[VOLOPT_FLAGS].i_value |= AFPVOL_MSWINDOWS | AFPVOL_USEDOTS;
465             } else if (strcasecmp(p, "crlf") == 0)
466                 options[VOLOPT_FLAGS].i_value |= AFPVOL_CRLF;
467             else if (strcasecmp(p, "noadouble") == 0)
468                 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOADOUBLE;
469             else if (strcasecmp(p, "ro") == 0)
470                 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
471             else if (strcasecmp(p, "nohex") == 0)
472                 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOHEX;
473             else if (strcasecmp(p, "usedots") == 0)
474                 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS;
475             else if (strcasecmp(p, "invisibledots") == 0)
476                 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS | AFPVOL_INV_DOTS;
477             else if (strcasecmp(p, "limitsize") == 0)
478                 options[VOLOPT_FLAGS].i_value |= AFPVOL_LIMITSIZE;
479             /* support for either "dropbox" or "dropkludge" */
480             else if (strcasecmp(p, "dropbox") == 0)
481                 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
482             else if (strcasecmp(p, "dropkludge") == 0)
483                 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
484             else if (strcasecmp(p, "nofileid") == 0)
485                 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOFILEID;
486             else if (strcasecmp(p, "nostat") == 0)
487                 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOSTAT;
488             else if (strcasecmp(p, "preexec_close") == 0)
489                 options[VOLOPT_PREEXEC].i_value = 1;
490             else if (strcasecmp(p, "root_preexec_close") == 0)
491                 options[VOLOPT_ROOTPREEXEC].i_value = 1;
492             else if (strcasecmp(p, "upriv") == 0)
493                 options[VOLOPT_FLAGS].i_value |= AFPVOL_UNIX_PRIV;
494             else if (strcasecmp(p, "extattrs") == 0)
495                 options[VOLOPT_FLAGS].i_value |= AFPVOL_EXT_ATTRS;
496             else if (strcasecmp(p, "acls") == 0)
497                 options[VOLOPT_FLAGS].i_value |= AFPVOL_ACLS;
498             else if (strcasecmp(p, "nodev") == 0)
499                 options[VOLOPT_FLAGS].i_value |= AFPVOL_NODEV;
500             else if (strcasecmp(p, "caseinsensitive") == 0)
501                 options[VOLOPT_FLAGS].i_value |= AFPVOL_CASEINSEN;
502             else if (strcasecmp(p, "illegalseq") == 0)
503                 options[VOLOPT_FLAGS].i_value |= AFPVOL_EILSEQ;
504             else if (strcasecmp(p, "cachecnid") == 0)
505                 options[VOLOPT_FLAGS].i_value |= AFPVOL_CACHE;
506
507             p = strtok(NULL, ",");
508         }
509
510     } else if (optionok(tmp, "dbpath:", val)) {
511         setoption(options, save, VOLOPT_DBPATH, val);
512
513     } else if (optionok(tmp, "umask:", val)) {
514         options[VOLOPT_UMASK].i_value = (int)strtol(val +1, NULL, 8);
515     } else if (optionok(tmp, "dperm:", val)) {
516         options[VOLOPT_DPERM].i_value = (int)strtol(val+1, NULL, 8);
517     } else if (optionok(tmp, "fperm:", val)) {
518         options[VOLOPT_FPERM].i_value = (int)strtol(val+1, NULL, 8);
519     } else if (optionok(tmp, "perm:", val)) {
520         options[VOLOPT_DFLTPERM].i_value = (int)strtol(val+1, NULL, 8);
521     } else if (optionok(tmp, "mapchars:",val)) {
522         setoption(options, save, VOLOPT_MAPCHARS, val);
523
524     } else if (optionok(tmp, "password:", val)) {
525         setoption(options, save, VOLOPT_PASSWORD, val);
526
527 #ifdef FORCE_UIDGID
528
529         /* this code allows forced uid/gid per volume settings */
530     } else if (optionok(tmp, "forceuid:", val)) {
531         setoption(options, save, VOLOPT_FORCEUID, val);
532     } else if (optionok(tmp, "forcegid:", val)) {
533         setoption(options, save, VOLOPT_FORCEGID, val);
534
535 #endif /* FORCE_UIDGID */
536     } else if (optionok(tmp, "root_preexec:", val)) {
537         setoption(options, save, VOLOPT_ROOTPREEXEC, val);
538
539     } else if (optionok(tmp, "preexec:", val)) {
540         setoption(options, save, VOLOPT_PREEXEC, val);
541
542     } else if (optionok(tmp, "root_postexec:", val)) {
543         setoption(options, save, VOLOPT_ROOTPOSTEXEC, val);
544
545     } else if (optionok(tmp, "postexec:", val)) {
546         setoption(options, save, VOLOPT_POSTEXEC, val);
547
548     } else if (optionok(tmp, "allowed_hosts:", val)) {
549         setoption(options, save, VOLOPT_ALLOWED_HOSTS, val);
550
551     } else if (optionok(tmp, "denied_hosts:", val)) {
552         setoption(options, save, VOLOPT_DENIED_HOSTS, val);
553
554     } else {
555         /* ignore unknown options */
556         LOG(log_debug, logtype_afpd, "ignoring unknown volume option: %s", tmp);
557
558     } 
559 }
560
561 /* ----------------- */
562 static void showvol(const ucs2_t *name)
563 {
564     struct vol  *volume;
565     for ( volume = Volumes; volume; volume = volume->v_next ) {
566       if (volume->v_hide && !strcasecmp_w( volume->v_name, name ) ) {
567             volume->v_hide = 0;
568             return;
569         }
570     }
571 }
572
573 /* ------------------------------- */
574 static int creatvol(AFPObj *obj, struct passwd *pwd, 
575                     char *path, char *name, 
576                     struct vol_option *options, 
577                     const int user /* user defined volume */
578                     )
579 {
580     struct vol  *volume;
581     int         suffixlen, vlen, tmpvlen, u8mvlen, macvlen;
582     int         hide = 0;
583     char        tmpname[AFPVOL_U8MNAMELEN+1];
584     ucs2_t      u8mtmpname[(AFPVOL_U8MNAMELEN+1)*2], mactmpname[(AFPVOL_MACNAMELEN+1)*2];
585     char        suffix[6]; /* max is #FFFF */
586     u_int16_t   flags;
587
588     if ( name == NULL || *name == '\0' ) {
589         if ((name = strrchr( path, '/' )) == NULL) {
590             return -1;  /* Obviously not a fully qualified path */
591         }
592
593         /* if you wish to share /, you need to specify a name. */
594         if (*++name == '\0')
595             return -1;
596     }
597
598     /* suffix for mangling use (lastvid + 1)   */
599     /* because v_vid has not been decided yet. */
600     suffixlen = sprintf(suffix, "%c%X", MANGLE_CHAR, lastvid + 1 );
601
602     vlen = strlen( name );
603
604     /* Unicode Volume Name */
605     /* Firstly convert name from unixcharset to UTF8-MAC */
606     flags = CONV_IGNORE;
607     tmpvlen = convert_charset(obj->options.unixcharset, CH_UTF8_MAC, 0, name, vlen, tmpname, AFPVOL_U8MNAMELEN, &flags);
608     if (tmpvlen <= 0) {
609         strcpy(tmpname, "???");
610         tmpvlen = 3;
611     }
612
613     /* Do we have to mangle ? */
614     if ( (flags & CONV_REQMANGLE) || (tmpvlen > obj->options.volnamelen)) {
615         if (tmpvlen + suffixlen > obj->options.volnamelen) {
616             flags = CONV_FORCE;
617             tmpvlen = convert_charset(obj->options.unixcharset, CH_UTF8_MAC, 0, name, vlen, tmpname, obj->options.volnamelen - suffixlen, &flags);
618             tmpname[tmpvlen != (size_t)-1 ? tmpvlen : 0] = 0;
619         }
620         strcat(tmpname, suffix);
621         tmpvlen = strlen(tmpname);
622     }
623
624     /* Secondly convert name from UTF8-MAC to UCS2 */
625     if ( 0 >= ( u8mvlen = convert_string(CH_UTF8_MAC, CH_UCS2, tmpname, tmpvlen, u8mtmpname, AFPVOL_U8MNAMELEN*2)) )
626         return -1;
627
628     LOG(log_debug, logtype_afpd, "createvol: Volume '%s' -> UTF8-MAC Name: '%s'", name, tmpname);
629
630     /* Maccharset Volume Name */
631     /* Firsty convert name from unixcharset to maccharset */
632     flags = CONV_IGNORE;
633     tmpvlen = convert_charset(obj->options.unixcharset, obj->options.maccharset, 0, name, vlen, tmpname, AFPVOL_U8MNAMELEN, &flags);
634     if (tmpvlen <= 0) {
635         strcpy(tmpname, "???");
636         tmpvlen = 3;
637     }
638
639     /* Do we have to mangle ? */
640     if ( (flags & CONV_REQMANGLE) || (tmpvlen > AFPVOL_MACNAMELEN)) {
641         if (tmpvlen + suffixlen > AFPVOL_MACNAMELEN) {
642             flags = CONV_FORCE;
643             tmpvlen = convert_charset(obj->options.unixcharset, obj->options.maccharset, 0, name, vlen, tmpname, AFPVOL_MACNAMELEN - suffixlen, &flags);
644             tmpname[tmpvlen != (size_t)-1 ? tmpvlen : 0] = 0;
645         }
646         strcat(tmpname, suffix);
647         tmpvlen = strlen(tmpname);
648     }
649
650     /* Secondly convert name from maccharset to UCS2 */
651     if ( 0 >= ( macvlen = convert_string(obj->options.maccharset, CH_UCS2, tmpname, tmpvlen, mactmpname, AFPVOL_U8MNAMELEN*2)) )
652         return -1;
653
654     LOG(log_debug, logtype_afpd, "createvol: Volume '%s' ->  Longname: '%s'", name, tmpname);
655
656     /* check duplicate */
657     for ( volume = Volumes; volume; volume = volume->v_next ) {
658       if (( strcasecmp_w( volume->v_u8mname, u8mtmpname ) == 0 ) || ( strcasecmp_w( volume->v_macname, mactmpname ) == 0 )){
659            if (volume->v_deleted) {
660                volume->v_new = hide = 1;
661            }
662            else {
663                return -1;       /* Won't be able to access it, anyway... */
664            }
665         }
666     }
667
668     if (!( volume = (struct vol *)calloc(1, sizeof( struct vol ))) ) {
669         LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
670         return -1;
671     }
672     if ( NULL == ( volume->v_localname = strdup(name))) {
673         LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
674         free(volume);
675         return -1;
676     }
677
678     if ( NULL == ( volume->v_u8mname = strdup_w(u8mtmpname))) {
679         LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
680         volume_free(volume);
681         free(volume);
682         return -1;
683     }
684     if ( NULL == ( volume->v_macname = strdup_w(mactmpname))) {
685         LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
686         volume_free(volume);
687         free(volume);
688         return -1;
689     }
690     if (!( volume->v_path = (char *)malloc( strlen( path ) + 1 )) ) {
691         LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
692         volume_free(volume);
693         free(volume);
694         return -1;
695     }
696
697     volume->v_name = utf8_encoding()?volume->v_u8mname:volume->v_macname;
698     volume->v_hide = hide;
699     strcpy( volume->v_path, path );
700
701 #ifdef __svr4__
702     volume->v_qfd = -1;
703 #endif /* __svr4__ */
704     /* os X start at 1 and use network order ie. 1 2 3 */
705     volume->v_vid = ++lastvid;
706     volume->v_vid = htons(volume->v_vid);
707
708     /* handle options */
709     if (options) {
710         /* should we casefold? */
711         volume->v_casefold = options[VOLOPT_CASEFOLD].i_value;
712
713         /* shift in some flags */
714         volume->v_flags = options[VOLOPT_FLAGS].i_value;
715
716         volume->v_ad_options = 0;
717         if ((volume->v_flags & AFPVOL_NODEV))
718             volume->v_ad_options |= ADVOL_NODEV;
719         if ((volume->v_flags & AFPVOL_CACHE))
720             volume->v_ad_options |= ADVOL_CACHE;
721         if ((volume->v_flags & AFPVOL_UNIX_PRIV))
722             volume->v_ad_options |= ADVOL_UNIXPRIV;
723         if ((volume->v_flags & AFPVOL_INV_DOTS))
724             volume->v_ad_options |= ADVOL_INVDOTS;
725
726         if (options[VOLOPT_PASSWORD].c_value)
727             volume->v_password = strdup(options[VOLOPT_PASSWORD].c_value);
728
729         if (options[VOLOPT_VETO].c_value)
730             volume->v_veto = strdup(options[VOLOPT_VETO].c_value);
731
732         if (options[VOLOPT_ENCODING].c_value)
733             volume->v_volcodepage = strdup(options[VOLOPT_ENCODING].c_value);
734
735         if (options[VOLOPT_MACCHARSET].c_value)
736             volume->v_maccodepage = strdup(options[VOLOPT_MACCHARSET].c_value);
737
738         if (options[VOLOPT_DBPATH].c_value)
739             volume->v_dbpath = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_DBPATH].c_value, pwd, path, name);
740
741        if (options[VOLOPT_CNIDSCHEME].c_value)
742            volume->v_cnidscheme = strdup(options[VOLOPT_CNIDSCHEME].c_value);
743
744         if (options[VOLOPT_UMASK].i_value)
745             volume->v_umask = (mode_t)options[VOLOPT_UMASK].i_value;
746
747         if (options[VOLOPT_DPERM].i_value)
748             volume->v_dperm = (mode_t)options[VOLOPT_DPERM].i_value;
749
750         if (options[VOLOPT_FPERM].i_value)
751             volume->v_fperm = (mode_t)options[VOLOPT_FPERM].i_value;
752
753         if (options[VOLOPT_DFLTPERM].i_value)
754             volume->v_perm = (mode_t)options[VOLOPT_DFLTPERM].i_value;
755
756         if (options[VOLOPT_ADOUBLE].i_value)
757             volume->v_adouble = options[VOLOPT_ADOUBLE].i_value;
758         else 
759             volume->v_adouble = AD_VERSION;
760
761         initvol_vfs(volume);
762 #ifdef FORCE_UIDGID
763         if (options[VOLOPT_FORCEUID].c_value) {
764             volume->v_forceuid = strdup(options[VOLOPT_FORCEUID].c_value);
765         } else {
766             volume->v_forceuid = NULL; /* set as null so as to return 0 later on */
767         }
768
769         if (options[VOLOPT_FORCEGID].c_value) {
770             volume->v_forcegid = strdup(options[VOLOPT_FORCEGID].c_value);
771         } else {
772             volume->v_forcegid = NULL; /* set as null so as to return 0 later on */
773         }
774 #endif
775         if (!user) {
776             if (options[VOLOPT_PREEXEC].c_value)
777                 volume->v_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_PREEXEC].c_value, pwd, path, name);
778             volume->v_preexec_close = options[VOLOPT_PREEXEC].i_value;
779
780             if (options[VOLOPT_POSTEXEC].c_value)
781                 volume->v_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_POSTEXEC].c_value, pwd, path, name);
782
783             if (options[VOLOPT_ROOTPREEXEC].c_value)
784                 volume->v_root_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPREEXEC].c_value, pwd, path,  name);
785             volume->v_root_preexec_close = options[VOLOPT_ROOTPREEXEC].i_value;
786
787             if (options[VOLOPT_ROOTPOSTEXEC].c_value)
788                 volume->v_root_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPOSTEXEC].c_value, pwd, path,  name);
789         }
790     }
791     volume->v_dperm |= volume->v_perm;
792     volume->v_fperm |= volume->v_perm;
793
794     volume->v_next = Volumes;
795     Volumes = volume;
796     return 0;
797 }
798
799 /* ---------------- */
800 static char *myfgets( buf, size, fp )
801 char    *buf;
802 int             size;
803 FILE    *fp;
804 {
805     char        *p;
806     int         c;
807
808     p = buf;
809     while ((EOF != ( c = getc( fp )) ) && ( size > 1 )) {
810         if ( c == '\n' || c == '\r' ) {
811             if (p != buf && *(p -1) == '\\') {
812                 p--;
813                 size++;
814                 continue;
815             }
816             *p++ = '\n';
817             break;
818         } else {
819             *p++ = c;
820         }
821         size--;
822     }
823
824     if ( p == buf ) {
825         return( NULL );
826     } else {
827         *p = '\0';
828         return( buf );
829     }
830 }
831
832
833 /* check access list. this function wants something of the following
834  * form:
835  *        @group,name,name2,@group2,name3
836  *
837  * a NULL argument allows everybody to have access.
838  * we return three things:
839  *     -1: no list
840  *      0: list exists, but name isn't in it
841  *      1: in list
842  */
843
844 #ifndef NO_REAL_USER_NAME
845 /* authentication is case insensitive 
846  * FIXME should we do the same with group name?
847 */
848 #define access_strcmp strcasecmp
849
850 #else
851 #define access_strcmp strcmp
852
853 #endif
854
855 static int accessvol(args, name)
856 const char *args;
857 const char *name;
858 {
859     char buf[MAXPATHLEN + 1], *p;
860     struct group *gr;
861
862     if (!args)
863         return -1;
864
865     strlcpy(buf, args, sizeof(buf));
866     if ((p = strtok(buf, ",")) == NULL) /* nothing, return okay */
867         return -1;
868
869     while (p) {
870         if (*p == '@') { /* it's a group */
871             if ((gr = getgrnam(p + 1)) && gmem(gr->gr_gid))
872                 return 1;
873         } else if (access_strcmp(p, name) == 0) /* it's a user name */
874             return 1;
875         p = strtok(NULL, ",");
876     }
877
878     return 0;
879 }
880
881 static int hostaccessvol(type, volname, args, obj)
882 int type;
883 char *volname;
884 const char *args;
885 const AFPObj *obj;
886 {
887     char buf[MAXPATHLEN + 1], *p, *b;
888     DSI *dsi = obj->handle;
889
890     if (!args)
891         return -1;
892
893     strlcpy(buf, args, sizeof(buf));
894     if ((p = strtok_r(buf, ",", &b)) == NULL) /* nothing, return okay */
895         return -1;
896
897     while (p) {
898         if (obj->proto == AFPPROTO_DSI) {
899             struct in_addr mask, net;
900             char *net_char, *mask_char;
901             int mask_int;
902
903             net_char = strtok(p, "/");
904             mask_char = strtok(NULL,"/");
905             if (mask_char == NULL) {
906                 mask_int = 32;
907             } else {
908                 mask_int = atoi(mask_char);
909             }
910            
911             // convert the integer netmask to a bitmask in network order
912             mask.s_addr = htonl(-1 - ((1 << (32 - mask_int)) - 1));
913             net.s_addr = inet_addr(net_char) & mask.s_addr;
914
915             if ((dsi->client.sin_addr.s_addr & mask.s_addr) == net.s_addr) {
916                     if (type == VOLOPT_DENIED_HOSTS)
917                         LOG(log_info, logtype_afpd, "AFP access denied for client IP '%s' to volume '%s' by denied list",
918                             inet_ntoa(dsi->client.sin_addr), volname);
919                     return 1;
920             }
921         }
922         p = strtok_r(NULL, ",", &b);
923     }
924     if (type == VOLOPT_ALLOWED_HOSTS)
925         LOG(log_info, logtype_afpd, "AFP access denied for client IP '%s' to volume '%s', not in allowed list",
926             inet_ntoa(dsi->client.sin_addr), volname);
927     return 0;
928 }
929
930 static void setextmap( ext, type, creator, user)
931 char            *ext, *type, *creator;
932 int                     user;
933 {
934     struct extmap       *em;
935     int                 cnt;
936
937     if (Extmap == NULL) {
938         if (( Extmap = calloc(1, sizeof( struct extmap ))) == NULL ) {
939             LOG(log_error, logtype_afpd, "setextmap: calloc: %s", strerror(errno) );
940             return;
941         }
942     }
943     ext++;
944     for ( em = Extmap, cnt = 0; em->em_ext; em++, cnt++) {
945         if ( (strdiacasecmp( em->em_ext, ext )) == 0 ) {
946             break;
947         }
948     }
949
950     if ( em->em_ext == NULL ) {
951         if (!(Extmap  = realloc( Extmap, sizeof( struct extmap ) * (cnt +2))) ) {
952             LOG(log_error, logtype_afpd, "setextmap: realloc: %s", strerror(errno) );
953             return;
954         }
955         (Extmap +cnt +1)->em_ext = NULL;
956         em = Extmap +cnt;
957     } else if ( !user ) {
958         return;
959     }
960     if (em->em_ext)
961         free(em->em_ext);
962
963     if (!(em->em_ext = strdup(  ext))) {
964         LOG(log_error, logtype_afpd, "setextmap: strdup: %s", strerror(errno) );
965         return;
966     }
967
968     if ( *type == '\0' ) {
969         memcpy(em->em_type, "????", sizeof( em->em_type ));
970     } else {
971         memcpy(em->em_type, type, sizeof( em->em_type ));
972     }
973     if ( *creator == '\0' ) {
974         memcpy(em->em_creator, "UNIX", sizeof( em->em_creator ));
975     } else {
976         memcpy(em->em_creator, creator, sizeof( em->em_creator ));
977     }
978 }
979
980 /* -------------------------- */
981 static int extmap_cmp(const void *map1, const void *map2)
982 {
983     const struct extmap *em1 = map1;
984     const struct extmap *em2 = map2;
985     return strdiacasecmp(em1->em_ext, em2->em_ext);
986 }
987
988 static void sortextmap( void)
989 {
990     struct extmap       *em;
991
992     Extmap_cnt = 0;
993     if ((em = Extmap) == NULL) {
994         return;
995     }
996     while (em->em_ext) {
997         em++;
998         Extmap_cnt++;
999     }
1000     if (Extmap_cnt) {
1001         qsort(Extmap, Extmap_cnt, sizeof(struct extmap), extmap_cmp);
1002         if (*Extmap->em_ext == 0) {
1003             /* the first line is really "." the default entry, 
1004              * we remove the leading '.' in setextmap
1005             */
1006             Defextmap = Extmap;
1007         }
1008     }
1009 }
1010
1011 /* ----------------------
1012 */
1013 static void free_extmap( void)
1014 {
1015     struct extmap       *em;
1016
1017     if (Extmap) {
1018         for ( em = Extmap; em->em_ext; em++) {
1019              free (em->em_ext);
1020         }
1021         free(Extmap);
1022         Extmap = NULL;
1023         Defextmap = Extmap;
1024         Extmap_cnt = 0;
1025     }
1026 }
1027
1028 /* ----------------------
1029 */
1030 static int volfile_changed(struct afp_volume_name *p) 
1031 {
1032     struct stat      st;
1033     char *name;
1034     
1035     if (p->full_name) 
1036         name = p->full_name;
1037     else
1038         name = p->name;
1039         
1040     if (!stat( name, &st) && st.st_mtime > p->mtime) {
1041         p->mtime = st.st_mtime;
1042         return 1;
1043     }
1044     return 0;
1045 }
1046
1047 /* ----------------------
1048  * Read a volume configuration file and add the volumes contained within to
1049  * the global volume list.  If p2 is non-NULL, the file that is opened is
1050  * p1/p2
1051  * 
1052  * Lines that begin with # and blank lines are ignored.
1053  * Volume lines are of the form:
1054  *              <unix path> [<volume name>] [allow:<user>,<@group>,...] \
1055  *                           [codepage:<file>] [casefold:<num>]
1056  *              <extension> TYPE [CREATOR]
1057  */
1058 static int readvolfile(obj, p1, p2, user, pwent)
1059 AFPObj      *obj;
1060 struct afp_volume_name  *p1;
1061 char        *p2;
1062 int             user;
1063 struct passwd *pwent;
1064 {
1065     FILE                *fp;
1066     char                path[ MAXPATHLEN + 1], tmp[ MAXPATHLEN + 1],
1067     volname[ AFPVOL_U8MNAMELEN + 1 ], buf[ BUFSIZ ],
1068     type[ 5 ], creator[ 5 ];
1069     char                *u, *p;
1070     struct passwd       *pw;
1071     struct vol_option   options[VOLOPT_NUM], save_options[VOLOPT_NUM];
1072     int                 i;
1073     struct stat         st;
1074     int                 fd;
1075
1076     if (!p1->name)
1077         return -1;
1078     p1->mtime = 0;
1079     strcpy( path, p1->name );
1080     if ( p2 != NULL ) {
1081         strcat( path, "/" );
1082         strcat( path, p2 );
1083         if (p1->full_name) {
1084             free(p1->full_name);
1085         }
1086         p1->full_name = strdup(path);
1087     }
1088
1089     if (NULL == ( fp = fopen( path, "r" )) ) {
1090         return( -1 );
1091     }
1092     fd = fileno(fp);
1093     if (fd != -1 && !fstat( fd, &st) ) {
1094         p1->mtime = st.st_mtime;
1095     }
1096
1097     memset(save_options, 0, sizeof(save_options));
1098     while ( myfgets( buf, sizeof( buf ), fp ) != NULL ) {
1099         initline( strlen( buf ), buf );
1100         parseline( sizeof( path ) - 1, path );
1101         switch ( *path ) {
1102         case '\0' :
1103         case '#' :
1104             continue;
1105
1106         case ':':
1107             /* change the default options for this file */
1108             if (strncmp(path, VOLOPT_DEFAULT, VOLOPT_DEFAULT_LEN) == 0) {
1109                 *tmp = '\0';
1110                 for (i = 0; i < VOLOPT_NUM; i++) {
1111                     if (parseline( sizeof( path ) - VOLOPT_DEFAULT_LEN - 1,
1112                                    path + VOLOPT_DEFAULT_LEN) < 0)
1113                         break;
1114                     volset(save_options, NULL, tmp, sizeof(tmp) - 1,
1115                            path + VOLOPT_DEFAULT_LEN);
1116                 }
1117             }
1118             break;
1119
1120         case '~' :
1121             if (( p = strchr( path, '/' )) != NULL ) {
1122                 *p++ = '\0';
1123             }
1124             u = path;
1125             u++;
1126             if ( *u == '\0' ) {
1127                 u = obj->username;
1128             }
1129             if ( u == NULL || *u == '\0' || ( pw = getpwnam( u )) == NULL ) {
1130                 continue;
1131             }
1132             strcpy( tmp, pw->pw_dir );
1133             if ( p != NULL && *p != '\0' ) {
1134                 strcat( tmp, "/" );
1135                 strcat( tmp, p );
1136             }
1137             /* Tag a user's home directory with their umask.  Note, this will
1138              * be overwritten if the user actually specifies a umask: option
1139              * for a '~' volume. */
1140             save_options[VOLOPT_UMASK].i_value = obj->options.save_mask;
1141             /* fall through */
1142
1143         case '/' :
1144             /* send path through variable substitution */
1145             if (*path != '~') /* need to copy path to tmp */
1146                 strcpy(tmp, path);
1147             if (!pwent)
1148                 pwent = getpwnam(obj->username);
1149             volxlate(obj, path, sizeof(path) - 1, tmp, pwent, NULL, NULL);
1150
1151             /* this is sort of braindead. basically, i want to be
1152              * able to specify things in any order, but i don't want to 
1153              * re-write everything. 
1154              *
1155              * currently we have options: 
1156              *   volname
1157              *   codepage:x
1158              *   casefold:x
1159              *   allow:x,y,@z
1160              *   deny:x,y,@z
1161              *   rwlist:x,y,@z
1162              *   rolist:x,y,@z
1163              *   options:prodos,crlf,noadouble,ro...
1164              *   dbpath:x
1165              *   password:x
1166              *   preexec:x
1167              *
1168              *   namemask:x,y,!z  (not implemented yet)
1169              */
1170             memcpy(options, save_options, sizeof(options));
1171             *volname = '\0';
1172
1173             /* read in up to VOLOP_NUM possible options */
1174             for (i = 0; i < VOLOPT_NUM; i++) {
1175                 if (parseline( sizeof( tmp ) - 1, tmp ) < 0)
1176                     break;
1177
1178                 volset(options, save_options, volname, sizeof(volname) - 1, tmp);
1179             }
1180
1181             /* check allow/deny lists:
1182                allow -> either no list (-1), or in list (1)
1183                deny -> either no list (-1), or not in list (0) */
1184             if (accessvol(options[VOLOPT_ALLOW].c_value, obj->username) &&
1185                 (accessvol(options[VOLOPT_DENY].c_value, obj->username) < 1) &&
1186                 hostaccessvol(VOLOPT_ALLOWED_HOSTS, volname, options[VOLOPT_ALLOWED_HOSTS].c_value, obj) &&
1187                 (hostaccessvol(VOLOPT_DENIED_HOSTS, volname, options[VOLOPT_DENIED_HOSTS].c_value, obj) < 1)) {
1188
1189                 /* handle read-only behaviour. semantics:
1190                  * 1) neither the rolist nor the rwlist exist -> rw
1191                  * 2) rolist exists -> ro if user is in it.
1192                  * 3) rwlist exists -> ro unless user is in it. */
1193                 if (((options[VOLOPT_FLAGS].i_value & AFPVOL_RO) == 0) &&
1194                         ((accessvol(options[VOLOPT_ROLIST].c_value,
1195                                     obj->username) == 1) ||
1196                          !accessvol(options[VOLOPT_RWLIST].c_value,
1197                                     obj->username)))
1198                     options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
1199
1200                 /* do variable substitution for volname */
1201                 volxlate(obj, tmp, sizeof(tmp) - 1, volname, pwent, path, NULL);
1202                 creatvol(obj, pwent, path, tmp, options, p2 != NULL);
1203             }
1204             volfree(options, save_options);
1205             break;
1206
1207         case '.' :
1208             parseline( sizeof( type ) - 1, type );
1209             parseline( sizeof( creator ) - 1, creator );
1210             setextmap( path, type, creator, user);
1211             break;
1212
1213         default :
1214             break;
1215         }
1216     }
1217     volfree(save_options, NULL);
1218     sortextmap();
1219     if ( fclose( fp ) != 0 ) {
1220         LOG(log_error, logtype_afpd, "readvolfile: fclose: %s", strerror(errno) );
1221     }
1222     p1->loaded = 1;
1223     return( 0 );
1224 }
1225
1226 /* ------------------------------- */
1227 static void volume_free(struct vol *vol)
1228 {
1229     free(vol->v_localname);
1230     vol->v_localname = NULL;
1231     free(vol->v_u8mname);
1232     vol->v_u8mname = NULL;
1233     free(vol->v_macname);
1234     vol->v_macname = NULL;
1235     free(vol->v_path);
1236     free(vol->v_password);
1237     free(vol->v_veto);
1238     free(vol->v_volcodepage);
1239     free(vol->v_maccodepage);
1240     free(vol->v_cnidscheme);
1241     free(vol->v_dbpath);
1242     free(vol->v_gvs);
1243 #ifdef FORCE_UIDGID
1244     free(vol->v_forceuid);
1245     free(vol->v_forcegid);
1246 #endif /* FORCE_UIDGID */
1247 }
1248
1249 /* ------------------------------- */
1250 static void free_volumes(void )
1251 {
1252     struct vol  *vol;
1253     struct vol  *nvol, *ovol;
1254
1255     for ( vol = Volumes; vol; vol = vol->v_next ) {
1256         if (( vol->v_flags & AFPVOL_OPEN ) ) {
1257             vol->v_deleted = 1;
1258             continue;
1259         }
1260         volume_free(vol);
1261     }
1262
1263     for ( vol = Volumes, ovol = NULL; vol; vol = nvol) {
1264         nvol = vol->v_next;
1265
1266         if (vol->v_localname == NULL) {
1267            if (Volumes == vol) {
1268                Volumes = nvol;
1269                ovol = Volumes;
1270            }
1271            else {
1272               ovol->v_next = nvol;
1273            }
1274            free(vol);
1275         }
1276         else {
1277            ovol = vol;
1278         }
1279     }
1280 }
1281
1282 /* ------------------------------- */
1283 static void volume_unlink(struct vol *volume)
1284 {
1285 struct vol *vol, *ovol, *nvol;
1286
1287     if (volume == Volumes) {
1288         Volumes = Volumes->v_next;
1289         return;
1290     }
1291     for ( vol = Volumes->v_next, ovol = Volumes; vol; vol = nvol) {
1292         nvol = vol->v_next;
1293
1294         if (vol == volume) {
1295             ovol->v_next = nvol;
1296             break;
1297         }
1298         else {
1299            ovol = vol;
1300         }
1301     }
1302 }
1303
1304 static int getvolspace( vol, bfree, btotal, xbfree, xbtotal, bsize )
1305 struct vol      *vol;
1306 u_int32_t       *bfree, *btotal, *bsize;
1307 VolSpace    *xbfree, *xbtotal;
1308 {
1309     int         spaceflag, rc;
1310     u_int32_t   maxsize;
1311 #ifndef NO_QUOTA_SUPPORT
1312     VolSpace    qfree, qtotal;
1313 #endif
1314
1315     spaceflag = AFPVOL_GVSMASK & vol->v_flags;
1316     /* report up to 2GB if afp version is < 2.2 (4GB if not) */
1317     maxsize = (vol->v_flags & AFPVOL_A2VOL) ? 0x01fffe00 :
1318               (((afp_version < 22) || (vol->v_flags & AFPVOL_LIMITSIZE))
1319                ? 0x7fffffffL : 0xffffffffL);
1320
1321 #ifdef AFS
1322     if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_AFSGVS ) {
1323         if ( afs_getvolspace( vol, xbfree, xbtotal, bsize ) == AFP_OK ) {
1324             vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_AFSGVS;
1325             goto getvolspace_done;
1326         }
1327     }
1328 #endif
1329
1330     if (( rc = ustatfs_getvolspace( vol, xbfree, xbtotal,
1331                                     bsize)) != AFP_OK ) {
1332         return( rc );
1333     }
1334
1335 #define min(a,b)        ((a)<(b)?(a):(b))
1336 #ifndef NO_QUOTA_SUPPORT
1337     if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_UQUOTA ) {
1338         if ( uquota_getvolspace( vol, &qfree, &qtotal, *bsize ) == AFP_OK ) {
1339             vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_UQUOTA;
1340             *xbfree = min(*xbfree, qfree);
1341             *xbtotal = min( *xbtotal, qtotal);
1342             goto getvolspace_done;
1343         }
1344     }
1345 #endif
1346     vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_USTATFS;
1347
1348 getvolspace_done:
1349     *bfree = min( *xbfree, maxsize);
1350     *btotal = min( *xbtotal, maxsize);
1351     return( AFP_OK );
1352 }
1353
1354 /* ----------------------- 
1355  * set volume creation date
1356  * avoid duplicate, well at least it tries
1357 */
1358 static void vol_setdate(u_int16_t id, struct adouble *adp, time_t date)
1359 {
1360     struct vol  *volume;
1361     struct vol  *vol = Volumes;
1362
1363     for ( volume = Volumes; volume; volume = volume->v_next ) {
1364         if (volume->v_vid == id) {
1365             vol = volume;
1366         }
1367         else if ((time_t)(AD_DATE_FROM_UNIX(date)) == volume->v_ctime) {
1368             date = date+1;
1369             volume = Volumes; /* restart */
1370         }
1371     }
1372     vol->v_ctime = AD_DATE_FROM_UNIX(date);
1373     ad_setdate(adp, AD_DATE_CREATE | AD_DATE_UNIX, date);
1374 }
1375
1376 /* ----------------------- */
1377 static int getvolparams( bitmap, vol, st, buf, buflen )
1378 u_int16_t       bitmap;
1379 struct vol      *vol;
1380 struct stat     *st;
1381 char    *buf;
1382 int             *buflen;
1383 {
1384     struct adouble      ad;
1385     int                 bit = 0, isad = 1;
1386     u_int32_t           aint;
1387     u_short             ashort;
1388     u_int32_t           bfree, btotal, bsize;
1389     VolSpace            xbfree, xbtotal; /* extended bytes */
1390     char                *data, *nameoff = NULL;
1391     char                *slash;
1392
1393     LOG(log_debug, logtype_afpd, "getvolparams: Volume '%s'", vol->v_localname);
1394
1395     /* courtesy of jallison@whistle.com:
1396      * For MacOS8.x support we need to create the
1397      * .Parent file here if it doesn't exist. */
1398
1399     ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1400     if ( ad_open_metadata( vol->v_path, vol_noadouble(vol) | ADFLAGS_DIR, O_CREAT, &ad) < 0 ) {
1401         isad = 0;
1402         vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1403
1404     } else if (ad_get_MD_flags( &ad ) & O_CREAT) {
1405         slash = strrchr( vol->v_path, '/' );
1406         if(slash)
1407             slash++;
1408         else
1409             slash = vol->v_path;
1410         if (ad_getentryoff(&ad, ADEID_NAME)) {
1411             ad_setentrylen( &ad, ADEID_NAME, strlen( slash ));
1412             memcpy(ad_entry( &ad, ADEID_NAME ), slash,
1413                ad_getentrylen( &ad, ADEID_NAME ));
1414         }
1415         vol_setdate(vol->v_vid, &ad, st->st_mtime);
1416         ad_flush(&ad);
1417     }
1418     else {
1419         if (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0)
1420             vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1421         else 
1422             vol->v_ctime = aint;
1423     }
1424
1425     if (( bitmap & ( (1<<VOLPBIT_BFREE)|(1<<VOLPBIT_BTOTAL) |
1426                      (1<<VOLPBIT_XBFREE)|(1<<VOLPBIT_XBTOTAL) |
1427                      (1<<VOLPBIT_BSIZE)) ) != 0 ) {
1428         if ( getvolspace( vol, &bfree, &btotal, &xbfree, &xbtotal,
1429                           &bsize) < 0 ) {
1430             if ( isad ) {
1431                 ad_close( &ad, ADFLAGS_HF );
1432             }
1433             return( AFPERR_PARAM );
1434         }
1435     }
1436
1437     data = buf;
1438     while ( bitmap != 0 ) {
1439         while (( bitmap & 1 ) == 0 ) {
1440             bitmap = bitmap>>1;
1441             bit++;
1442         }
1443
1444         switch ( bit ) {
1445         case VOLPBIT_ATTR :
1446             ashort = 0;
1447             if (0 == (vol->v_flags & AFPVOL_NOFILEID) && vol->v_cdb != NULL &&
1448                            (vol->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1449                 ashort = VOLPBIT_ATTR_FILEID;
1450             }
1451             /* check for read-only.
1452              * NOTE: we don't actually set the read-only flag unless
1453              *       it's passed in that way as it's possible to mount
1454              *       a read-write filesystem under a read-only one. */
1455             if ((vol->v_flags & AFPVOL_RO) ||
1456                     ((utime(vol->v_path, NULL) < 0) && (errno == EROFS))) {
1457                 ashort |= VOLPBIT_ATTR_RO;
1458             }
1459             ashort |= VOLPBIT_ATTR_CATSEARCH;
1460             if (afp_version >= 30) {
1461                 ashort |= VOLPBIT_ATTR_UTF8;
1462                 if (vol->v_flags & AFPVOL_UNIX_PRIV)
1463                     ashort |= VOLPBIT_ATTR_UNIXPRIV;
1464             }
1465             if (afp_version >= 32) {
1466                 if (vol->v_flags & AFPVOL_EXT_ATTRS)
1467                     ashort |= VOLPBIT_ATTR_EXT_ATTRS;
1468                 if (vol->v_flags & AFPVOL_ACLS)
1469                     ashort |= VOLPBIT_ATTR_ACLS;
1470             }
1471             ashort = htons(ashort);
1472             memcpy(data, &ashort, sizeof( ashort ));
1473             data += sizeof( ashort );
1474             break;
1475
1476         case VOLPBIT_SIG :
1477             ashort = htons( AFPVOLSIG_DEFAULT );
1478             memcpy(data, &ashort, sizeof( ashort ));
1479             data += sizeof( ashort );
1480             break;
1481
1482         case VOLPBIT_CDATE :
1483             aint = vol->v_ctime;
1484             memcpy(data, &aint, sizeof( aint ));
1485             data += sizeof( aint );
1486             break;
1487
1488         case VOLPBIT_MDATE :
1489             if ( st->st_mtime > vol->v_mtime ) {
1490                 vol->v_mtime = st->st_mtime;
1491             }
1492             aint = AD_DATE_FROM_UNIX(vol->v_mtime);
1493             memcpy(data, &aint, sizeof( aint ));
1494             data += sizeof( aint );
1495             break;
1496
1497         case VOLPBIT_BDATE :
1498             if (!isad ||  (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1499                 aint = AD_DATE_START;
1500             memcpy(data, &aint, sizeof( aint ));
1501             data += sizeof( aint );
1502             break;
1503
1504         case VOLPBIT_VID :
1505             memcpy(data, &vol->v_vid, sizeof( vol->v_vid ));
1506             data += sizeof( vol->v_vid );
1507             break;
1508
1509         case VOLPBIT_BFREE :
1510             bfree = htonl( bfree );
1511             memcpy(data, &bfree, sizeof( bfree ));
1512             data += sizeof( bfree );
1513             break;
1514
1515         case VOLPBIT_BTOTAL :
1516             btotal = htonl( btotal );
1517             memcpy(data, &btotal, sizeof( btotal ));
1518             data += sizeof( btotal );
1519             break;
1520
1521 #ifndef NO_LARGE_VOL_SUPPORT
1522         case VOLPBIT_XBFREE :
1523             xbfree = hton64( xbfree );
1524 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1525             bcopy(&xbfree, data, sizeof(xbfree));
1526 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1527             memcpy(data, &xbfree, sizeof( xbfree ));
1528 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1529             data += sizeof( xbfree );
1530             break;
1531
1532         case VOLPBIT_XBTOTAL :
1533             xbtotal = hton64( xbtotal );
1534 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1535             bcopy(&xbtotal, data, sizeof(xbtotal));
1536 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1537             memcpy(data, &xbtotal, sizeof( xbtotal ));
1538 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1539             data += sizeof( xbfree );
1540             break;
1541 #endif /* ! NO_LARGE_VOL_SUPPORT */
1542
1543         case VOLPBIT_NAME :
1544             nameoff = data;
1545             data += sizeof( u_int16_t );
1546             break;
1547
1548         case VOLPBIT_BSIZE:  /* block size */
1549             bsize = htonl(bsize);
1550             memcpy(data, &bsize, sizeof(bsize));
1551             data += sizeof(bsize);
1552             break;
1553
1554         default :
1555             if ( isad ) {
1556                 ad_close( &ad, ADFLAGS_HF );
1557             }
1558             return( AFPERR_BITMAP );
1559         }
1560         bitmap = bitmap>>1;
1561         bit++;
1562     }
1563     if ( nameoff ) {
1564         ashort = htons( data - buf );
1565         memcpy(nameoff, &ashort, sizeof( ashort ));
1566         /* name is always in mac charset */
1567         aint = ucs2_to_charset( vol->v_maccharset, vol->v_macname, data+1, AFPVOL_MACNAMELEN + 1);
1568         if ( aint <= 0 ) {
1569             *buflen = 0;
1570             return AFPERR_MISC;
1571         }
1572                 
1573         *data++ = aint;
1574         data += aint;
1575     }
1576     if ( isad ) {
1577         ad_close_metadata( &ad);
1578     }
1579     *buflen = data - buf;
1580     return( AFP_OK );
1581 }
1582
1583 /* ------------------------- */
1584 static int stat_vol(u_int16_t bitmap, struct vol *vol, char *rbuf, int *rbuflen)
1585 {
1586     struct stat st;
1587     int         buflen, ret;
1588
1589     if ( stat( vol->v_path, &st ) < 0 ) {
1590         *rbuflen = 0;
1591         return( AFPERR_PARAM );
1592     }
1593     /* save the volume device number */
1594     vol->v_dev = st.st_dev;
1595
1596     buflen = *rbuflen - sizeof( bitmap );
1597     if (( ret = getvolparams( bitmap, vol, &st,
1598                               rbuf + sizeof( bitmap ), &buflen )) != AFP_OK ) {
1599         *rbuflen = 0;
1600         return( ret );
1601     }
1602     *rbuflen = buflen + sizeof( bitmap );
1603     bitmap = htons( bitmap );
1604     memcpy(rbuf, &bitmap, sizeof( bitmap ));
1605     return( AFP_OK );
1606
1607 }
1608
1609 /* ------------------------------- */
1610 void load_volumes(AFPObj *obj)
1611 {
1612     struct passwd       *pwent;
1613
1614     if (Volumes) {
1615         int changed = 0;
1616         
1617         /* check files date */
1618         if (obj->options.defaultvol.loaded) {
1619             changed = volfile_changed(&obj->options.defaultvol);
1620         }
1621         if (obj->options.systemvol.loaded) {
1622             changed |= volfile_changed(&obj->options.systemvol);
1623         }
1624         if (obj->options.uservol.loaded) {
1625             changed |= volfile_changed(&obj->options.uservol);
1626         }
1627         if (!changed)
1628             return;
1629         
1630         free_extmap();
1631         free_volumes();
1632     }
1633     
1634     pwent = getpwnam(obj->username);
1635     if ( (obj->options.flags & OPTION_USERVOLFIRST) == 0 ) {
1636         readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent);
1637     }
1638
1639     if ((*obj->username == '\0') || (obj->options.flags & OPTION_NOUSERVOL)) {
1640         readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent);
1641     } else if (pwent) {
1642         /*
1643         * Read user's AppleVolumes or .AppleVolumes file
1644         * If neither are readable, read the default volumes file. if
1645         * that doesn't work, create a user share.
1646         */
1647         if (obj->options.uservol.name) {
1648             free(obj->options.uservol.name);
1649         }
1650         obj->options.uservol.name = strdup(pwent->pw_dir);
1651         if ( readvolfile(obj, &obj->options.uservol,    "AppleVolumes", 1, pwent) < 0 &&
1652                 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                 obj->options.defaultvol.name != NULL ) {
1656             if (readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent) < 0)
1657                 creatvol(obj, pwent, pwent->pw_dir, NULL, NULL, 1);
1658         }
1659     }
1660     if ( obj->options.flags & OPTION_USERVOLFIRST ) {
1661         readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent );
1662     }
1663     
1664     if ( obj->options.closevol ) {
1665         struct vol *vol;
1666         
1667         for ( vol = Volumes; vol; vol = vol->v_next ) {
1668             if (vol->v_deleted && !vol->v_new ) {
1669                 of_closevol(vol);
1670                 deletevol(vol);
1671                 vol = Volumes;
1672             }
1673         }
1674     }
1675 }
1676
1677 /* ------------------------------- */
1678 int afp_getsrvrparms(obj, ibuf, ibuflen, rbuf, rbuflen )
1679 AFPObj  *obj;
1680 char    *ibuf _U_, *rbuf;
1681 int     ibuflen _U_, *rbuflen;
1682 {
1683     struct timeval      tv;
1684     struct stat         st;
1685     struct vol          *volume;
1686     char        *data;
1687     char                *namebuf;
1688     int                 vcnt;
1689     size_t              len;
1690
1691     load_volumes(obj);
1692
1693     data = rbuf + 5;
1694     for ( vcnt = 0, volume = Volumes; volume; volume = volume->v_next ) {
1695         if (!(volume->v_flags & AFPVOL_NOSTAT)) {
1696             struct maccess ma;
1697
1698             if ( stat( volume->v_path, &st ) < 0 ) {
1699                 LOG(log_info, logtype_afpd, "afp_getsrvrparms(%s): stat: %s",
1700                         volume->v_path, strerror(errno) );
1701                 continue;               /* can't access directory */
1702             }
1703             if (!S_ISDIR(st.st_mode)) {
1704                 continue;               /* not a dir */
1705             }
1706             accessmode(volume->v_path, &ma, NULL, &st);
1707             if ((ma.ma_user & (AR_UREAD | AR_USEARCH)) != (AR_UREAD | AR_USEARCH)) {
1708                 continue;   /* no r-x access */
1709             }
1710         }
1711         if (volume->v_hide) {
1712             continue;           /* config file changed but the volume was mounted */
1713         }
1714
1715         if (utf8_encoding()) {
1716             len = ucs2_to_charset_allocate(CH_UTF8_MAC, &namebuf, volume->v_u8mname);
1717         } else {
1718             len = ucs2_to_charset_allocate(obj->options.maccharset, &namebuf, volume->v_macname);
1719         }
1720
1721         if (len == (size_t)-1)
1722                 continue;
1723
1724         /* set password bit if there's a volume password */
1725         *data = (volume->v_password) ? AFPSRVR_PASSWD : 0;
1726
1727         /* Apple 2 clients running ProDOS-8 expect one volume to have
1728            bit 0 of this byte set.  They will not recognize anything
1729            on the server unless this is the case.  I have not
1730            completely worked this out, but it's related to booting
1731            from the server.  Support for that function is a ways
1732            off.. <shirsch@ibm.net> */
1733         *data |= (volume->v_flags & AFPVOL_A2VOL) ? AFPSRVR_CONFIGINFO : 0;
1734         *data++ |= 0; /* UNIX PRIVS BIT ..., OSX doesn't seem to use it, so we don't either */
1735         *data++ = len;
1736         memcpy(data, namebuf, len );
1737         data += len;
1738         free(namebuf);
1739         vcnt++;
1740     }
1741
1742     *rbuflen = data - rbuf;
1743     data = rbuf;
1744     if ( gettimeofday( &tv, 0 ) < 0 ) {
1745         LOG(log_error, logtype_afpd, "afp_getsrvrparms(%s): gettimeofday: %s", volume->v_path, strerror(errno) );
1746         *rbuflen = 0;
1747         return AFPERR_PARAM;
1748     }
1749     tv.tv_sec = AD_DATE_FROM_UNIX(tv.tv_sec);
1750     memcpy(data, &tv.tv_sec, sizeof( u_int32_t));
1751     data += sizeof( u_int32_t);
1752     *data = vcnt;
1753     return( AFP_OK );
1754 }
1755
1756 /* ------------------------- 
1757  * we are the user here
1758 */
1759 int afp_openvol(obj, ibuf, ibuflen, rbuf, rbuflen )
1760 AFPObj  *obj;
1761 char    *ibuf, *rbuf;
1762 int     ibuflen _U_, *rbuflen;
1763 {
1764     struct stat st;
1765     char        *volname;
1766     char        *p;
1767     struct vol  *volume;
1768     struct dir  *dir;
1769     int         len, ret;
1770     size_t      namelen;
1771     u_int16_t   bitmap;
1772     char        path[ MAXPATHLEN + 1];
1773     char        *vol_uname;
1774     char        *vol_mname;
1775     char        *volname_tmp;
1776
1777     ibuf += 2;
1778     memcpy(&bitmap, ibuf, sizeof( bitmap ));
1779     bitmap = ntohs( bitmap );
1780     ibuf += sizeof( bitmap );
1781     if (( bitmap & (1<<VOLPBIT_VID)) == 0 ) {
1782         *rbuflen = 0;
1783         return AFPERR_BITMAP;
1784     }
1785
1786     len = (unsigned char)*ibuf++;
1787     volname = obj->oldtmp;
1788
1789     if ((volname_tmp = strchr(volname,'+')) != NULL)
1790         volname = volname_tmp+1;
1791
1792     if (utf8_encoding()) {
1793       namelen = convert_string(CH_UTF8_MAC, CH_UCS2, ibuf, len, volname, sizeof(obj->oldtmp));
1794     } else {
1795       namelen = convert_string(obj->options.maccharset, CH_UCS2, ibuf, len, volname, sizeof(obj->oldtmp));
1796     }
1797
1798     if ( namelen <= 0){
1799         *rbuflen = 0;
1800         return AFPERR_PARAM;
1801     }
1802
1803     ibuf += len;
1804     if ((len + 1) & 1) /* pad to an even boundary */
1805         ibuf++;
1806
1807     load_volumes(obj);
1808
1809     for ( volume = Volumes; volume; volume = volume->v_next ) {
1810         if ( strcasecmp_w( (ucs2_t*) volname, volume->v_name ) == 0 ) {
1811             break;
1812         }
1813     }
1814
1815     if ( volume == NULL ) {
1816         *rbuflen = 0;
1817         return AFPERR_PARAM;
1818     }
1819
1820     /* check for a volume password */
1821     if (volume->v_password && strncmp(ibuf, volume->v_password, VOLPASSLEN)) {
1822         *rbuflen = 0;
1823         return AFPERR_ACCESS;
1824     }
1825
1826     if (( volume->v_flags & AFPVOL_OPEN  ) ) {
1827         /* the volume is already open */
1828 #ifdef FORCE_UIDGID
1829         set_uidgid ( volume );
1830 #endif
1831         return stat_vol(bitmap, volume, rbuf, rbuflen);
1832     }
1833
1834     /* initialize volume variables
1835      * FIXME file size
1836     */
1837     if (utf8_encoding()) {
1838         volume->max_filename = 255;
1839     }
1840     else {
1841         volume->max_filename = MACFILELEN;
1842     }
1843
1844     volume->v_dir = volume->v_root = NULL;
1845     volume->v_hash = NULL;
1846
1847     volume->v_flags |= AFPVOL_OPEN;
1848     volume->v_cdb = NULL;  
1849
1850     if (volume->v_root_preexec) {
1851         if ((ret = afprun(1, volume->v_root_preexec, NULL)) && volume->v_root_preexec_close) {
1852             LOG(log_error, logtype_afpd, "afp_openvol(%s): root preexec : %d", volume->v_path, ret );
1853             ret = AFPERR_MISC;
1854             goto openvol_err;
1855         }
1856     }
1857
1858 #ifdef FORCE_UIDGID
1859     set_uidgid ( volume );
1860 #endif
1861
1862     if (volume->v_preexec) {
1863         if ((ret = afprun(0, volume->v_preexec, NULL)) && volume->v_preexec_close) {
1864             LOG(log_error, logtype_afpd, "afp_openvol(%s): preexec : %d", volume->v_path, ret );
1865             ret = AFPERR_MISC;
1866             goto openvol_err;
1867         }
1868     }
1869
1870     if ( stat( volume->v_path, &st ) < 0 ) {
1871         ret = AFPERR_PARAM;
1872         goto openvol_err;
1873     }
1874
1875     if ( chdir( volume->v_path ) < 0 ) {
1876         ret = AFPERR_PARAM;
1877         goto openvol_err;
1878     }
1879
1880     if (utf8_encoding()) {
1881         len = convert_string_allocate(CH_UCS2, CH_UTF8_MAC, volume->v_u8mname, namelen, &vol_mname);
1882     } else {
1883         len = convert_string_allocate(CH_UCS2, obj->options.maccharset, volume->v_macname, namelen, &vol_mname);
1884     }
1885     if ( !vol_mname || len <= 0) {
1886         ret = AFPERR_MISC;
1887         goto openvol_err;
1888     }
1889     
1890     if ( NULL == getcwd(path, MAXPATHLEN)) {
1891         /* shouldn't be fatal but it will fail later */
1892         LOG(log_error, logtype_afpd, "afp_openvol(%s): volume pathlen too long", volume->v_path);
1893         ret = AFPERR_MISC;
1894         goto openvol_err;
1895     }        
1896     
1897     if ((vol_uname = strrchr(path, '/')) == NULL)
1898          vol_uname = path;
1899     else if (*(vol_uname + 1) != '\0')
1900          vol_uname++;
1901         
1902     if ((dir = dirnew(vol_mname, vol_uname) ) == NULL) {
1903         free(vol_mname);
1904         LOG(log_error, logtype_afpd, "afp_openvol(%s): malloc: %s", volume->v_path, strerror(errno) );
1905         ret = AFPERR_MISC;
1906         goto openvol_err;
1907     }
1908     free(vol_mname);
1909
1910     dir->d_did = DIRDID_ROOT;
1911     dir->d_color = DIRTREE_COLOR_BLACK; /* root node is black */
1912     dir->d_m_name_ucs2 = strdup_w(volume->v_name);
1913     volume->v_dir = volume->v_root = dir;
1914     volume->v_hash = dirhash();
1915
1916     curdir = volume->v_dir;
1917     if (volume->v_cnidscheme == NULL) {
1918         volume->v_cnidscheme = strdup(DEFAULT_CNID_SCHEME);
1919         LOG(log_warning, logtype_afpd, "Warning: No CNID scheme for volume %s. Using default.",
1920                volume->v_path);
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 }