]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/volume.c
revert previous bogus change...
[netatalk.git] / etc / afpd / volume.c
1 /*
2  * $Id: volume.c,v 1.51.2.7.2.8 2003-10-23 04:29:16 didg Exp $
3  *
4  * Copyright (c) 1990,1993 Regents of The University of Michigan.
5  * All Rights Reserved.  See COPYRIGHT.
6  */
7
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif /* HAVE_CONFIG_H */
11
12 #include <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 #if 0
355     LOG(log_debug, logtype_afpd, "Parsing volset %s", val);
356 #endif
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  * set volume creation date
1115  * avoid duplicate, well at least it tries
1116 */
1117 static void vol_setdate(u_int16_t id, struct adouble *adp, time_t date)
1118 {
1119     struct vol  *volume;
1120     struct vol  *vol = Volumes;
1121
1122     for ( volume = Volumes; volume; volume = volume->v_next ) {
1123         if (volume->v_vid == id) {
1124             vol = volume;
1125         }
1126         else if (AD_DATE_FROM_UNIX(date) == volume->v_ctime) {
1127             date = date+1;
1128             volume = Volumes; /* restart */
1129         }
1130     }
1131     vol->v_ctime = AD_DATE_FROM_UNIX(date);
1132     ad_setdate(adp, AD_DATE_CREATE | AD_DATE_UNIX, date);
1133 }
1134
1135 /* ----------------------- */
1136 static int getvolparams( bitmap, vol, st, buf, buflen )
1137 u_int16_t       bitmap;
1138 struct vol      *vol;
1139 struct stat     *st;
1140 char    *buf;
1141 int             *buflen;
1142 {
1143     struct adouble      ad;
1144     int                 bit = 0, isad = 1;
1145     u_int32_t           aint;
1146     u_short             ashort;
1147     u_int32_t           bfree, btotal, bsize;
1148     VolSpace            xbfree, xbtotal; /* extended bytes */
1149     char                *data, *nameoff = NULL;
1150     char                *slash;
1151
1152     /* courtesy of jallison@whistle.com:
1153      * For MacOS8.x support we need to create the
1154      * .Parent file here if it doesn't exist. */
1155
1156     ad_init(&ad, vol->v_adouble);
1157     if ( ad_open( vol->v_path, vol_noadouble(vol) |
1158                   ADFLAGS_HF|ADFLAGS_DIR, O_RDWR | O_CREAT,
1159                   0666, &ad) < 0 ) {
1160         isad = 0;
1161         vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1162
1163     } else if (ad_get_HF_flags( &ad ) & O_CREAT) {
1164         slash = strrchr( vol->v_path, '/' );
1165         if(slash)
1166             slash++;
1167         else
1168             slash = vol->v_path;
1169
1170         ad_setentrylen( &ad, ADEID_NAME, strlen( slash ));
1171         memcpy(ad_entry( &ad, ADEID_NAME ), slash,
1172                ad_getentrylen( &ad, ADEID_NAME ));
1173         vol_setdate(vol->v_vid, &ad, st->st_mtime);
1174         ad_flush(&ad, ADFLAGS_HF);
1175     }
1176     else {
1177         if (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0)
1178             vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1179         else 
1180             vol->v_ctime = aint;
1181     }
1182
1183     if (( bitmap & ( (1<<VOLPBIT_BFREE)|(1<<VOLPBIT_BTOTAL) |
1184                      (1<<VOLPBIT_XBFREE)|(1<<VOLPBIT_XBTOTAL) |
1185                      (1<<VOLPBIT_BSIZE)) ) != 0 ) {
1186         if ( getvolspace( vol, &bfree, &btotal, &xbfree, &xbtotal,
1187                           &bsize) < 0 ) {
1188             if ( isad ) {
1189                 ad_close( &ad, ADFLAGS_HF );
1190             }
1191             return( AFPERR_PARAM );
1192         }
1193     }
1194
1195     data = buf;
1196     while ( bitmap != 0 ) {
1197         while (( bitmap & 1 ) == 0 ) {
1198             bitmap = bitmap>>1;
1199             bit++;
1200         }
1201
1202         switch ( bit ) {
1203         case VOLPBIT_ATTR :
1204             ashort = 0;
1205             if (0 == (vol->v_flags & AFPVOL_NOFILEID) && vol->v_cdb != NULL &&
1206                            (vol->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1207                 ashort = VOLPBIT_ATTR_FILEID;
1208             }
1209             /* check for read-only.
1210              * NOTE: we don't actually set the read-only flag unless
1211              *       it's passed in that way as it's possible to mount
1212              *       a read-write filesystem under a read-only one. */
1213             if ((vol->v_flags & AFPVOL_RO) ||
1214                     ((utime(vol->v_path, NULL) < 0) && (errno == EROFS))) {
1215                 ashort |= VOLPBIT_ATTR_RO;
1216             }
1217             ashort |= VOLPBIT_ATTR_CATSEARCH;
1218             if (afp_version >= 30) {
1219                 ashort |= VOLPBIT_ATTR_UTF8;
1220                 if (vol->v_flags & AFPVOL_UNIX_PRIV)
1221                     ashort |= VOLPBIT_ATTR_UNIXPRIV;
1222             }
1223             ashort = htons(ashort);
1224             memcpy(data, &ashort, sizeof( ashort ));
1225             data += sizeof( ashort );
1226             break;
1227
1228         case VOLPBIT_SIG :
1229             ashort = htons( AFPVOLSIG_DEFAULT );
1230             memcpy(data, &ashort, sizeof( ashort ));
1231             data += sizeof( ashort );
1232             break;
1233
1234         case VOLPBIT_CDATE :
1235             aint = vol->v_ctime;
1236             memcpy(data, &aint, sizeof( aint ));
1237             data += sizeof( aint );
1238             break;
1239
1240         case VOLPBIT_MDATE :
1241             if ( st->st_mtime > vol->v_mtime ) {
1242                 vol->v_mtime = st->st_mtime;
1243             }
1244             aint = AD_DATE_FROM_UNIX(vol->v_mtime);
1245             memcpy(data, &aint, sizeof( aint ));
1246             data += sizeof( aint );
1247             break;
1248
1249         case VOLPBIT_BDATE :
1250             if (!isad ||  (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1251                 aint = AD_DATE_START;
1252             memcpy(data, &aint, sizeof( aint ));
1253             data += sizeof( aint );
1254             break;
1255
1256         case VOLPBIT_VID :
1257             memcpy(data, &vol->v_vid, sizeof( vol->v_vid ));
1258             data += sizeof( vol->v_vid );
1259             break;
1260
1261         case VOLPBIT_BFREE :
1262             bfree = htonl( bfree );
1263             memcpy(data, &bfree, sizeof( bfree ));
1264             data += sizeof( bfree );
1265             break;
1266
1267         case VOLPBIT_BTOTAL :
1268             btotal = htonl( btotal );
1269             memcpy(data, &btotal, sizeof( btotal ));
1270             data += sizeof( btotal );
1271             break;
1272
1273 #ifndef NO_LARGE_VOL_SUPPORT
1274         case VOLPBIT_XBFREE :
1275             xbfree = hton64( xbfree );
1276 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1277             bcopy(&xbfree, data, sizeof(xbfree));
1278 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1279             memcpy(data, &xbfree, sizeof( xbfree ));
1280 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1281             data += sizeof( xbfree );
1282             break;
1283
1284         case VOLPBIT_XBTOTAL :
1285             xbtotal = hton64( xbtotal );
1286 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1287             bcopy(&xbtotal, data, sizeof(xbtotal));
1288 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1289             memcpy(data, &xbtotal, sizeof( xbtotal ));
1290 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1291             data += sizeof( xbfree );
1292             break;
1293 #endif /* ! NO_LARGE_VOL_SUPPORT */
1294
1295         case VOLPBIT_NAME :
1296             nameoff = data;
1297             data += sizeof( u_int16_t );
1298             break;
1299
1300         case VOLPBIT_BSIZE:  /* block size */
1301             bsize = htonl(bsize);
1302             memcpy(data, &bsize, sizeof(bsize));
1303             data += sizeof(bsize);
1304             break;
1305
1306         default :
1307             if ( isad ) {
1308                 ad_close( &ad, ADFLAGS_HF );
1309             }
1310             return( AFPERR_BITMAP );
1311         }
1312         bitmap = bitmap>>1;
1313         bit++;
1314     }
1315     if ( nameoff ) {
1316         ashort = htons( data - buf );
1317         memcpy(nameoff, &ashort, sizeof( ashort ));
1318         aint = strlen( vol->v_name );
1319         *data++ = aint;
1320         memcpy(data, vol->v_name, aint );
1321         data += aint;
1322     }
1323     if ( isad ) {
1324         ad_close( &ad, ADFLAGS_HF );
1325     }
1326     *buflen = data - buf;
1327     return( AFP_OK );
1328 }
1329
1330 /* ------------------------- */
1331 int static stat_vol(u_int16_t bitmap, struct vol *vol, char *rbuf, int *rbuflen)
1332 {
1333     struct stat st;
1334     int         buflen, ret;
1335
1336     if ( stat( vol->v_path, &st ) < 0 ) {
1337         *rbuflen = 0;
1338         return( AFPERR_PARAM );
1339     }
1340
1341     buflen = *rbuflen - sizeof( bitmap );
1342     if (( ret = getvolparams( bitmap, vol, &st,
1343                               rbuf + sizeof( bitmap ), &buflen )) != AFP_OK ) {
1344         *rbuflen = 0;
1345         return( ret );
1346     }
1347     *rbuflen = buflen + sizeof( bitmap );
1348     bitmap = htons( bitmap );
1349     memcpy(rbuf, &bitmap, sizeof( bitmap ));
1350     return( AFP_OK );
1351
1352 }
1353
1354 /* ------------------------------- */
1355 void load_volumes(AFPObj *obj)
1356 {
1357     struct passwd       *pwent;
1358
1359     if (Volumes) {
1360         int changed = 0;
1361         
1362         /* check files date */
1363         if (obj->options.defaultvol.loaded) {
1364             changed = volfile_changed(&obj->options.defaultvol);
1365         }
1366         if (obj->options.systemvol.loaded) {
1367             changed |= volfile_changed(&obj->options.systemvol);
1368         }
1369         if (obj->options.uservol.loaded) {
1370             changed |= volfile_changed(&obj->options.uservol);
1371         }
1372         if (!changed)
1373             return;
1374         
1375         free_extmap();
1376         free_volumes();
1377     }
1378     
1379     pwent = getpwnam(obj->username);
1380     if ( (obj->options.flags & OPTION_USERVOLFIRST) == 0 ) {
1381         readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent);
1382     }
1383
1384     if ((*obj->username == '\0') || (obj->options.flags & OPTION_NOUSERVOL)) {
1385         readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent);
1386     } else if (pwent) {
1387         /*
1388         * Read user's AppleVolumes or .AppleVolumes file
1389         * If neither are readable, read the default volumes file. if
1390         * that doesn't work, create a user share.
1391         */
1392         obj->options.uservol.name = strdup(pwent->pw_dir);
1393         if ( readvolfile(obj, &obj->options.uservol,    "AppleVolumes", 1, pwent) < 0 &&
1394                 readvolfile(obj, &obj->options.uservol, ".AppleVolumes", 1, pwent) < 0 &&
1395                 readvolfile(obj, &obj->options.uservol, "applevolumes", 1, pwent) < 0 &&
1396                 readvolfile(obj, &obj->options.uservol, ".applevolumes", 1, pwent) < 0 &&
1397                 obj->options.defaultvol.name != NULL ) {
1398             if (readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent) < 0)
1399                 creatvol(obj, pwent, pwent->pw_dir, NULL, NULL, 1);
1400         }
1401     }
1402     if ( obj->options.flags & OPTION_USERVOLFIRST ) {
1403         readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent );
1404     }
1405 }
1406
1407 /* ------------------------------- */
1408 int afp_getsrvrparms(obj, ibuf, ibuflen, rbuf, rbuflen )
1409 AFPObj      *obj;
1410 char    *ibuf, *rbuf;
1411 int     ibuflen, *rbuflen;
1412 {
1413     struct timeval      tv;
1414     struct stat         st;
1415     struct vol          *volume;
1416     char        *data;
1417     int                 vcnt, len;
1418
1419     load_volumes(obj);
1420
1421     data = rbuf + 5;
1422     for ( vcnt = 0, volume = Volumes; volume; volume = volume->v_next ) {
1423         if (!(volume->v_flags & AFPVOL_NOSTAT)) {
1424             if ( stat( volume->v_path, &st ) < 0 ) {
1425                 LOG(log_info, logtype_afpd, "afp_getsrvrparms: stat %s: %s",
1426                         volume->v_path, strerror(errno) );
1427                 continue;               /* can't access directory */
1428             }
1429             if (!S_ISDIR(st.st_mode)) {
1430                 continue;               /* not a dir */
1431             }
1432         }
1433         if (volume->v_hide) {
1434             continue;           /* config file changed but the volume was mounted */
1435         }
1436         /* set password bit if there's a volume password */
1437         *data = (volume->v_password) ? AFPSRVR_PASSWD : 0;
1438
1439         /* Apple 2 clients running ProDOS-8 expect one volume to have
1440            bit 0 of this byte set.  They will not recognize anything
1441            on the server unless this is the case.  I have not
1442            completely worked this out, but it's related to booting
1443            from the server.  Support for that function is a ways
1444            off.. <shirsch@ibm.net> */
1445         *data |= (volume->v_flags & AFPVOL_A2VOL) ? AFPSRVR_CONFIGINFO : 0;
1446         *data++ |= 0; /* UNIX PRIVS BIT ..., OSX doesn't seem to use it, so we don't either */
1447         len = strlen( volume->v_name );
1448         *data++ = len;
1449         memcpy(data, volume->v_name, len );
1450         data += len;
1451         vcnt++;
1452     }
1453
1454     *rbuflen = data - rbuf;
1455     data = rbuf;
1456     if ( gettimeofday( &tv, 0 ) < 0 ) {
1457         LOG(log_error, logtype_afpd, "afp_getsrvrparms: gettimeofday: %s", strerror(errno) );
1458         *rbuflen = 0;
1459         return AFPERR_PARAM;
1460     }
1461     tv.tv_sec = AD_DATE_FROM_UNIX(tv.tv_sec);
1462     memcpy(data, &tv.tv_sec, sizeof( u_int32_t));
1463     data += sizeof( u_int32_t);
1464     *data = vcnt;
1465     return( AFP_OK );
1466 }
1467
1468 /* ------------------------- 
1469  * we are the user here
1470 */
1471 int afp_openvol(obj, ibuf, ibuflen, rbuf, rbuflen )
1472 AFPObj      *obj;
1473 char    *ibuf, *rbuf;
1474 int             ibuflen, *rbuflen;
1475 {
1476     struct stat st;
1477     char        *volname;
1478     char        *p;
1479     struct vol  *volume;
1480     struct dir  *dir;
1481     int         len, ret;
1482     u_int16_t   bitmap;
1483
1484     ibuf += 2;
1485     memcpy(&bitmap, ibuf, sizeof( bitmap ));
1486     bitmap = ntohs( bitmap );
1487     ibuf += sizeof( bitmap );
1488     if (( bitmap & (1<<VOLPBIT_VID)) == 0 ) {
1489         *rbuflen = 0;
1490         return AFPERR_BITMAP;
1491     }
1492
1493     len = (unsigned char)*ibuf++;
1494     volname = obj->oldtmp;
1495     memcpy(volname, ibuf, len );
1496     *(volname +  len) = '\0';
1497     ibuf += len;
1498     if ((len + 1) & 1) /* pad to an even boundary */
1499         ibuf++;
1500
1501     load_volumes(obj);
1502
1503     for ( volume = Volumes; volume; volume = volume->v_next ) {
1504         if ( strcasecmp( volname, volume->v_name ) == 0 ) {
1505             break;
1506         }
1507     }
1508
1509     if ( volume == NULL ) {
1510         *rbuflen = 0;
1511         return AFPERR_PARAM;
1512     }
1513
1514     /* check for a volume password */
1515     if (volume->v_password && strncmp(ibuf, volume->v_password, VOLPASSLEN)) {
1516         *rbuflen = 0;
1517         return AFPERR_ACCESS;
1518     }
1519
1520     if (( volume->v_flags & AFPVOL_OPEN  ) ) {
1521         /* the volume is already open */
1522 #ifdef FORCE_UIDGID
1523         set_uidgid ( volume );
1524 #endif
1525         return stat_vol(bitmap, volume, rbuf, rbuflen);
1526     }
1527
1528     /* initialize volume variables
1529      * FIXME file size
1530     */
1531     if (afp_version >= 30) {
1532         volume->max_filename = 255;
1533     }
1534     else {
1535         volume->max_filename = MACFILELEN;
1536     }
1537
1538     volume->v_dir = volume->v_root = NULL;
1539
1540     /* FIXME unix name != mac name */
1541     if ((dir = dirnew(volume->v_name, volume->v_name) ) == NULL) {
1542         LOG(log_error, logtype_afpd, "afp_openvol: malloc: %s", strerror(errno) );
1543         ret = AFPERR_MISC;
1544         goto openvol_err;
1545     }
1546
1547     dir->d_did = DIRDID_ROOT;
1548     dir->d_color = DIRTREE_COLOR_BLACK; /* root node is black */
1549     volume->v_dir = volume->v_root = dir;
1550     volume->v_flags |= AFPVOL_OPEN;
1551     volume->v_cdb = NULL;  
1552
1553     if (volume->v_root_preexec) {
1554         if ((ret = afprun(1, volume->v_root_preexec, NULL)) && volume->v_root_preexec_close) {
1555             LOG(log_error, logtype_afpd, "afp_openvol: root preexec : %d", ret );
1556             ret = AFPERR_MISC;
1557             goto openvol_err;
1558         }
1559     }
1560
1561 #ifdef FORCE_UIDGID
1562     set_uidgid ( volume );
1563 #endif
1564
1565     if (volume->v_preexec) {
1566         if ((ret = afprun(0, volume->v_preexec, NULL)) && volume->v_preexec_close) {
1567             LOG(log_error, logtype_afpd, "afp_openvol: preexec : %d", ret );
1568             ret = AFPERR_MISC;
1569             goto openvol_err;
1570         }
1571     }
1572
1573     if ( stat( volume->v_path, &st ) < 0 ) {
1574         ret = AFPERR_PARAM;
1575         goto openvol_err;
1576     }
1577
1578     if ( chdir( volume->v_path ) < 0 ) {
1579         ret = AFPERR_PARAM;
1580         goto openvol_err;
1581     }
1582     curdir = volume->v_dir;
1583     if (volume->v_cnidscheme == NULL) {
1584         volume->v_cnidscheme = strdup(DEFAULT_CNID_SCHEME);
1585         LOG(log_warning, logtype_afpd, "Warning: No CNID scheme for volume %s. Using default.",
1586                volume->v_path);
1587     }
1588     if (volume->v_dbpath)
1589         volume->v_cdb = cnid_open (volume->v_dbpath, volume->v_umask, volume->v_cnidscheme);
1590     if (volume->v_cdb == NULL)
1591         volume->v_cdb = cnid_open (volume->v_path, volume->v_umask, volume->v_cnidscheme);
1592     if (volume->v_cdb == NULL) {
1593         volume->v_cdb = cnid_open (volume->v_path, volume->v_umask, DEFAULT_CNID_SCHEME);
1594         LOG(log_error, logtype_afpd, "Error: invalid CNID backend for %s: %s", volume->v_path,
1595             volume->v_cnidscheme);
1596     }
1597     if (volume->v_cdb == NULL) {
1598         LOG(log_error, logtype_afpd, "Fatal error: cannot open CNID for %s", volume->v_path);
1599         ret = AFPERR_MISC;
1600         goto openvol_err;
1601     }
1602
1603     /* Codepages */
1604
1605     if (!volume->v_volcodepage)
1606         volume->v_volcodepage = strdup("UTF8");
1607
1608     if ( (charset_t) -1 == ( volume->v_volcharset = add_charset(volume->v_volcodepage)) ) {
1609         LOG (log_error, logtype_afpd, "Setting codepage %s as volume codepage failed", volume->v_volcodepage);
1610         ret = AFPERR_MISC;
1611         goto openvol_err;
1612     }
1613
1614     if ( NULL == ( volume->v_vol = find_charset_functions(volume->v_volcodepage)) || volume->v_vol->flags & CHARSET_ICONV ) {
1615         LOG (log_warning, logtype_afpd, "WARNING: volume encoding %s is *not* supported by netatalk, expect problems !!!!", volume->v_volcodepage);
1616     }   
1617
1618     if (!volume->v_maccodepage)
1619         volume->v_maccodepage = strdup(obj->options.maccodepage);
1620
1621     if ( (charset_t) -1 == ( volume->v_maccharset = add_charset(volume->v_maccodepage)) ) {
1622         LOG (log_error, logtype_afpd, "Setting codepage %s as mac codepage failed", volume->v_maccodepage);
1623         ret = AFPERR_MISC;
1624         goto openvol_err;
1625     }
1626
1627     if ( NULL == ( volume->v_mac = find_charset_functions(volume->v_maccodepage)) || ! (volume->v_mac->flags & CHARSET_CLIENT) ) {
1628         LOG (log_error, logtype_afpd, "Fatal error: mac charset %s not supported", volume->v_maccodepage);
1629         ret = AFPERR_MISC;
1630         goto openvol_err;
1631     }   
1632
1633     ret  = stat_vol(bitmap, volume, rbuf, rbuflen);
1634     if (ret == AFP_OK) {
1635
1636         handle_special_folders( volume );
1637
1638         /*
1639          * If you mount a volume twice, the second time the trash appears on
1640          * the desk-top.  That's because the Mac remembers the DID for the
1641          * trash (even for volumes in different zones, on different servers).
1642          * Just so this works better, we prime the DID cache with the trash,
1643          * fixing the trash at DID 17.
1644          * FIXME (RL): should it be done inside a CNID backend ? (always returning Trash DID when asked) ?
1645          */
1646         if ((volume->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1647
1648             /* FIXME find db time stamp */
1649             cnid_getstamp(volume->v_cdb, volume->v_stamp, sizeof(volume->v_stamp));
1650             p = Trash;
1651             cname( volume, volume->v_dir, &p );
1652         }
1653         return( AFP_OK );
1654     }
1655
1656 openvol_err:
1657     if (volume->v_dir) {
1658         dirfree( volume->v_dir );
1659         volume->v_dir = volume->v_root = NULL;
1660     }
1661
1662     volume->v_flags &= ~AFPVOL_OPEN;
1663     if (volume->v_cdb != NULL) {
1664         cnid_close(volume->v_cdb);
1665         volume->v_cdb = NULL;
1666     }
1667     *rbuflen = 0;
1668     return ret;
1669 }
1670
1671 /* ------------------------- */
1672 static void closevol(struct vol *vol)
1673 {
1674     if (!vol)
1675         return;
1676
1677     dirfree( vol->v_root );
1678     vol->v_dir = NULL;
1679     if (vol->v_cdb != NULL) {
1680         cnid_close(vol->v_cdb);
1681         vol->v_cdb = NULL;
1682     }
1683
1684     if (vol->v_postexec) {
1685         afprun(0, vol->v_postexec, NULL);
1686     }
1687     if (vol->v_root_postexec) {
1688         afprun(1, vol->v_root_postexec, NULL);
1689     }
1690 }
1691
1692 /* ------------------------- */
1693 void close_all_vol(void)
1694 {
1695     struct vol  *ovol;
1696     curdir = NULL;
1697     for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
1698         if ( ovol->v_flags & AFPVOL_OPEN ) {
1699             ovol->v_flags &= ~AFPVOL_OPEN;
1700             closevol(ovol);
1701         }
1702     }
1703 }
1704
1705 /* ------------------------- */
1706 int afp_closevol(obj, ibuf, ibuflen, rbuf, rbuflen )
1707 AFPObj      *obj;
1708 char    *ibuf, *rbuf;
1709 int             ibuflen, *rbuflen;
1710 {
1711     struct vol  *vol, *ovol;
1712     u_int16_t   vid;
1713
1714     *rbuflen = 0;
1715     ibuf += 2;
1716     memcpy(&vid, ibuf, sizeof( vid ));
1717     if (NULL == ( vol = getvolbyvid( vid )) ) {
1718         return( AFPERR_PARAM );
1719     }
1720
1721     vol->v_flags &= ~AFPVOL_OPEN;
1722     for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
1723         if ( ovol->v_flags & AFPVOL_OPEN ) {
1724             break;
1725         }
1726     }
1727     if ( ovol != NULL ) {
1728         /* Even if chdir fails, we can't say afp_closevol fails. */
1729         if ( chdir( ovol->v_path ) == 0 ) {
1730             curdir = ovol->v_dir;
1731         }
1732     }
1733
1734     closevol(vol);
1735     if (vol->v_deleted) {
1736         showvol(vol->v_name);
1737         volume_free(vol);
1738         volume_unlink(vol);
1739     }
1740     return( AFP_OK );
1741 }
1742
1743 /* ------------------------- */
1744 struct vol *getvolbyvid(const u_int16_t vid )
1745 {
1746     struct vol  *vol;
1747
1748     for ( vol = Volumes; vol; vol = vol->v_next ) {
1749         if ( vid == vol->v_vid ) {
1750             break;
1751         }
1752     }
1753     if ( vol == NULL || ( vol->v_flags & AFPVOL_OPEN ) == 0 ) {
1754         return( NULL );
1755     }
1756
1757 #ifdef FORCE_UIDGID
1758     set_uidgid ( vol );
1759 #endif /* FORCE_UIDGID */
1760
1761     return( vol );
1762 }
1763
1764 /* ------------------------ */
1765 static int ext_cmp_key(const void *key, const void *obj)
1766 {
1767     const char          *p = key;
1768     const struct extmap *em = obj;
1769     return strdiacasecmp(p, em->em_ext);
1770 }
1771 struct extmap *getextmap(const char *path)
1772 {
1773     char          *p;
1774     struct extmap *em;
1775
1776     if (NULL == ( p = strrchr( path, '.' )) ) {
1777         return( Defextmap );
1778     }
1779     p++;
1780     if (!*p || !Extmap_cnt) {
1781         return( Defextmap );
1782     }
1783     em = bsearch(p, Extmap, Extmap_cnt, sizeof(struct extmap), ext_cmp_key);
1784     if (em) {
1785         return( em );
1786     } else {
1787         return( Defextmap );
1788     }
1789 }
1790
1791 /* ------------------------- */
1792 struct extmap *getdefextmap(void)
1793 {
1794     return( Defextmap );
1795 }
1796
1797 /* --------------------------
1798    poll if a volume is changed by other processes.
1799 */
1800 int  pollvoltime(obj)
1801 AFPObj *obj;
1802 {
1803     struct vol       *vol;
1804     struct timeval   tv;
1805     struct stat      st;
1806     
1807     if (!(afp_version > 21 && obj->options.server_notif)) 
1808          return 0;
1809
1810     if ( gettimeofday( &tv, 0 ) < 0 ) 
1811          return 0;
1812
1813     for ( vol = Volumes; vol; vol = vol->v_next ) {
1814         if ( (vol->v_flags & AFPVOL_OPEN)  && vol->v_mtime + 30 < tv.tv_sec) {
1815             if ( !stat( vol->v_path, &st ) && vol->v_mtime != st.st_mtime ) {
1816                 vol->v_mtime = st.st_mtime;
1817                 if (!obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED))
1818                     return -1;
1819                 return 1;
1820             }
1821         }
1822     }
1823     return 0;
1824 }
1825
1826 /* ------------------------- */
1827 void setvoltime(obj, vol )
1828 AFPObj *obj;
1829 struct vol      *vol;
1830 {
1831     struct timeval      tv;
1832
1833     /* just looking at vol->v_mtime is broken seriously since updates
1834      * from other users afpd processes never are seen.
1835      * This is not the most elegant solution (a shared memory between
1836      * the afpd processes would come closer)
1837      * [RS] */
1838
1839     if ( gettimeofday( &tv, 0 ) < 0 ) {
1840         LOG(log_error, logtype_afpd, "setvoltime: gettimeofday: %s", strerror(errno) );
1841         return;
1842     }
1843     if( utime( vol->v_path, NULL ) < 0 ) {
1844         /* write of time failed ... probably a read only filesys,
1845          * where no other users can interfere, so there's no issue here
1846          */
1847     }
1848
1849     /* a little granularity */
1850     if (vol->v_mtime < tv.tv_sec) {
1851         vol->v_mtime = tv.tv_sec;
1852         if (afp_version > 21 && obj->options.server_notif) {
1853             obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED);
1854         }
1855     }
1856 }
1857
1858 /* ------------------------- */
1859 int afp_getvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1860 AFPObj      *obj;
1861 char    *ibuf, *rbuf;
1862 int             ibuflen, *rbuflen;
1863 {
1864     struct vol  *vol;
1865     u_int16_t   vid, bitmap;
1866
1867     ibuf += 2;
1868     memcpy(&vid, ibuf, sizeof( vid ));
1869     ibuf += sizeof( vid );
1870     memcpy(&bitmap, ibuf, sizeof( bitmap ));
1871     bitmap = ntohs( bitmap );
1872
1873     if (NULL == ( vol = getvolbyvid( vid )) ) {
1874         *rbuflen = 0;
1875         return( AFPERR_PARAM );
1876     }
1877
1878     return stat_vol(bitmap, vol, rbuf, rbuflen);
1879 }
1880
1881 /* ------------------------- */
1882 int afp_setvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1883 AFPObj      *obj;
1884 char    *ibuf, *rbuf;
1885 int             ibuflen, *rbuflen;
1886 {
1887     struct adouble ad;
1888     struct vol  *vol;
1889     u_int16_t   vid, bitmap;
1890     u_int32_t   aint;
1891
1892     ibuf += 2;
1893     *rbuflen = 0;
1894
1895     memcpy(&vid, ibuf, sizeof( vid ));
1896     ibuf += sizeof( vid );
1897     memcpy(&bitmap, ibuf, sizeof( bitmap ));
1898     bitmap = ntohs( bitmap );
1899     ibuf += sizeof(bitmap);
1900
1901     if (( vol = getvolbyvid( vid )) == NULL ) {
1902         return( AFPERR_PARAM );
1903     }
1904
1905     if (vol->v_flags & AFPVOL_RO)
1906         return AFPERR_VLOCK;
1907
1908     /* we can only set the backup date. */
1909     if (bitmap != (1 << VOLPBIT_BDATE))
1910         return AFPERR_BITMAP;
1911
1912     ad_init(&ad, vol->v_adouble);
1913     if ( ad_open( vol->v_path, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR,
1914                   0666, &ad) < 0 ) {
1915         if (errno == EROFS)
1916             return AFPERR_VLOCK;
1917
1918         return AFPERR_ACCESS;
1919     }
1920
1921     memcpy(&aint, ibuf, sizeof(aint));
1922     ad_setdate(&ad, AD_DATE_BACKUP, aint);
1923     ad_flush(&ad, ADFLAGS_HF);
1924     ad_close(&ad, ADFLAGS_HF);
1925     return( AFP_OK );
1926 }
1927
1928 /* ------------------------- */
1929 int wincheck(const struct vol *vol, const char *path)
1930 {
1931     int len;
1932
1933     if (!(vol->v_flags & AFPVOL_MSWINDOWS))
1934         return 1;
1935
1936     /* empty paths are not allowed */
1937     if ((len = strlen(path)) == 0)
1938         return 0;
1939
1940     /* leading or trailing whitespaces are not allowed, carriage returns
1941      * and probably other whitespace is okay, tabs are not allowed
1942      */
1943     if ((path[0] == ' ') || (path[len-1] == ' '))
1944         return 0;
1945
1946     /* certain characters are not allowed */
1947     if (strpbrk(path, MSWINDOWS_BADCHARS))
1948         return 0;
1949
1950     /* everything else is okay */
1951     return 1;
1952 }
1953
1954
1955 /*
1956  * precreate a folder 
1957  * this is only intended for folders in the volume root 
1958  * It will *not* work if the folder name contains extended characters 
1959  */
1960 static int create_special_folder (const struct vol *vol, const struct _special_folder *folder)
1961 {
1962         char            *p;
1963         struct adouble  ad;
1964         u_int16_t       attr;
1965         struct stat     st;
1966         int             ret;
1967
1968
1969         p = (char *) malloc ( strlen(vol->v_path)+strlen(folder->name)+2);
1970         if ( p == NULL) {
1971                 LOG(log_error, logtype_afpd,"malloc failed");
1972                 exit (-1);
1973         }
1974         strcpy(p, vol->v_path);
1975         strcat(p, "/");
1976         strcat(p, folder->name);
1977
1978         if ( (ret = stat( p, &st )) < 0 ) {
1979                 if (folder->precreate) {
1980                     if (ad_mkdir(p, folder->mode & ~vol->v_umask)) {
1981                         LOG(log_debug, logtype_afpd,"Creating '%s' failed", folder->name);
1982                         free(p);
1983                         return -1;
1984                     }
1985                     ret = 0;
1986                 }
1987         }
1988
1989         if ( !ret && folder->hide) {
1990                 /* Hide it */
1991                 ad_init(&ad, vol->v_adouble);
1992                 if (ad_open( p, vol_noadouble(vol) | ADFLAGS_HF|ADFLAGS_DIR,
1993                         O_RDWR|O_CREAT, 0666, &ad) < 0) {
1994                         free (p);
1995                         return (-1);
1996                 }
1997                 if ((ad_get_HF_flags( &ad ) & O_CREAT) ) {
1998                         ad_setentrylen( &ad, ADEID_NAME, strlen(folder->name));
1999                         memcpy(ad_entry( &ad, ADEID_NAME ), folder->name,
2000                         ad_getentrylen( &ad, ADEID_NAME ));
2001                 }
2002  
2003                 ad_getattr(&ad, &attr);
2004                 attr |= htons( ntohs( attr ) | ATTRBIT_INVISIBLE );
2005                 ad_setattr(&ad, attr);
2006     
2007                 ad_flush( &ad, ADFLAGS_HF );
2008                 ad_close( &ad, ADFLAGS_HF );
2009         }
2010         free(p);
2011         return 0;
2012 }
2013
2014 static void handle_special_folders (const struct vol * vol)
2015 {
2016         const _special_folder *p = &special_folders[0];
2017
2018         if (vol->v_flags & AFPVOL_RO)
2019                 return;
2020
2021         for (; p->name != NULL; p++) {
2022                 create_special_folder (vol, p);
2023         }
2024 }
2025