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