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