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