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