]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/volume.c
sendfile for LARGE_FILE. use sys_ftruncate in ad_open too, temporary fix with
[netatalk.git] / etc / afpd / volume.c
1 /*
2  * $Id: volume.c,v 1.51.2.7.2.7 2003-10-17 00:01:11 didg Exp $
3  *
4  * Copyright (c) 1990,1993 Regents of The University of Michigan.
5  * All Rights Reserved.  See COPYRIGHT.
6  */
7
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif /* HAVE_CONFIG_H */
11
12 #include <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 ( stat( volume->v_path, &st ) < 0 ) {
1424             if ((volume->v_flags & AFPVOL_NOSTAT)) {
1425                 continue;
1426             }
1427             LOG(log_info, logtype_afpd, "afp_getsrvrparms: stat %s: %s",
1428                     volume->v_path, strerror(errno) );
1429             continue;           /* can't access directory */
1430         }
1431         if (!S_ISDIR(st.st_mode)) {
1432             continue;           /* not a dir */
1433         }
1434
1435         if (volume->v_hide) {
1436             continue;           /* config file changed but the volume was mounted */
1437         }
1438         /* set password bit if there's a volume password */
1439         *data = (volume->v_password) ? AFPSRVR_PASSWD : 0;
1440
1441         /* Apple 2 clients running ProDOS-8 expect one volume to have
1442            bit 0 of this byte set.  They will not recognize anything
1443            on the server unless this is the case.  I have not
1444            completely worked this out, but it's related to booting
1445            from the server.  Support for that function is a ways
1446            off.. <shirsch@ibm.net> */
1447         *data |= (volume->v_flags & AFPVOL_A2VOL) ? AFPSRVR_CONFIGINFO : 0;
1448         *data++ |= 0; /* UNIX PRIVS BIT ..., OSX doesn't seem to use it, so we don't either */
1449         len = strlen( volume->v_name );
1450         *data++ = len;
1451         memcpy(data, volume->v_name, len );
1452         data += len;
1453         vcnt++;
1454     }
1455
1456     *rbuflen = data - rbuf;
1457     data = rbuf;
1458     if ( gettimeofday( &tv, 0 ) < 0 ) {
1459         LOG(log_error, logtype_afpd, "afp_getsrvrparms: gettimeofday: %s", strerror(errno) );
1460         *rbuflen = 0;
1461         return AFPERR_PARAM;
1462     }
1463     tv.tv_sec = AD_DATE_FROM_UNIX(tv.tv_sec);
1464     memcpy(data, &tv.tv_sec, sizeof( u_int32_t));
1465     data += sizeof( u_int32_t);
1466     *data = vcnt;
1467     return( AFP_OK );
1468 }
1469
1470 /* ------------------------- 
1471  * we are the user here
1472 */
1473 int afp_openvol(obj, ibuf, ibuflen, rbuf, rbuflen )
1474 AFPObj      *obj;
1475 char    *ibuf, *rbuf;
1476 int             ibuflen, *rbuflen;
1477 {
1478     struct stat st;
1479     char        *volname;
1480     char        *p;
1481     struct vol  *volume;
1482     struct dir  *dir;
1483     int         len, ret;
1484     u_int16_t   bitmap;
1485
1486     ibuf += 2;
1487     memcpy(&bitmap, ibuf, sizeof( bitmap ));
1488     bitmap = ntohs( bitmap );
1489     ibuf += sizeof( bitmap );
1490     if (( bitmap & (1<<VOLPBIT_VID)) == 0 ) {
1491         *rbuflen = 0;
1492         return AFPERR_BITMAP;
1493     }
1494
1495     len = (unsigned char)*ibuf++;
1496     volname = obj->oldtmp;
1497     memcpy(volname, ibuf, len );
1498     *(volname +  len) = '\0';
1499     ibuf += len;
1500     if ((len + 1) & 1) /* pad to an even boundary */
1501         ibuf++;
1502
1503     load_volumes(obj);
1504
1505     for ( volume = Volumes; volume; volume = volume->v_next ) {
1506         if ( strcasecmp( volname, volume->v_name ) == 0 ) {
1507             break;
1508         }
1509     }
1510
1511     if ( volume == NULL ) {
1512         *rbuflen = 0;
1513         return AFPERR_PARAM;
1514     }
1515
1516     /* check for a volume password */
1517     if (volume->v_password && strncmp(ibuf, volume->v_password, VOLPASSLEN)) {
1518         *rbuflen = 0;
1519         return AFPERR_ACCESS;
1520     }
1521
1522     if (( volume->v_flags & AFPVOL_OPEN  ) ) {
1523         /* the volume is already open */
1524 #ifdef FORCE_UIDGID
1525         set_uidgid ( volume );
1526 #endif
1527         return stat_vol(bitmap, volume, rbuf, rbuflen);
1528     }
1529
1530     /* initialize volume variables
1531      * FIXME file size
1532     */
1533     if (afp_version >= 30) {
1534         volume->max_filename = 255;
1535     }
1536     else {
1537         volume->max_filename = MACFILELEN;
1538     }
1539
1540     volume->v_dir = volume->v_root = NULL;
1541
1542     /* FIXME unix name != mac name */
1543     if ((dir = dirnew(volume->v_name, volume->v_name) ) == NULL) {
1544         LOG(log_error, logtype_afpd, "afp_openvol: malloc: %s", strerror(errno) );
1545         ret = AFPERR_MISC;
1546         goto openvol_err;
1547     }
1548
1549     dir->d_did = DIRDID_ROOT;
1550     dir->d_color = DIRTREE_COLOR_BLACK; /* root node is black */
1551     volume->v_dir = volume->v_root = dir;
1552     volume->v_flags |= AFPVOL_OPEN;
1553     volume->v_cdb = NULL;  
1554
1555     if (volume->v_root_preexec) {
1556         if ((ret = afprun(1, volume->v_root_preexec, NULL)) && volume->v_root_preexec_close) {
1557             LOG(log_error, logtype_afpd, "afp_openvol: root preexec : %d", ret );
1558             ret = AFPERR_MISC;
1559             goto openvol_err;
1560         }
1561     }
1562
1563 #ifdef FORCE_UIDGID
1564     set_uidgid ( volume );
1565 #endif
1566
1567     if (volume->v_preexec) {
1568         if ((ret = afprun(0, volume->v_preexec, NULL)) && volume->v_preexec_close) {
1569             LOG(log_error, logtype_afpd, "afp_openvol: preexec : %d", ret );
1570             ret = AFPERR_MISC;
1571             goto openvol_err;
1572         }
1573     }
1574
1575     if ( stat( volume->v_path, &st ) < 0 ) {
1576         ret = AFPERR_PARAM;
1577         goto openvol_err;
1578     }
1579
1580     if ( chdir( volume->v_path ) < 0 ) {
1581         ret = AFPERR_PARAM;
1582         goto openvol_err;
1583     }
1584     curdir = volume->v_dir;
1585     if (volume->v_cnidscheme == NULL) {
1586         volume->v_cnidscheme = strdup(DEFAULT_CNID_SCHEME);
1587         LOG(log_warning, logtype_afpd, "Warning: No CNID scheme for volume %s. Using default.",
1588                volume->v_path);
1589     }
1590     if (volume->v_dbpath)
1591         volume->v_cdb = cnid_open (volume->v_dbpath, volume->v_umask, volume->v_cnidscheme);
1592     if (volume->v_cdb == NULL)
1593         volume->v_cdb = cnid_open (volume->v_path, volume->v_umask, volume->v_cnidscheme);
1594     if (volume->v_cdb == NULL) {
1595         volume->v_cdb = cnid_open (volume->v_path, volume->v_umask, DEFAULT_CNID_SCHEME);
1596         LOG(log_error, logtype_afpd, "Error: invalid CNID backend for %s: %s", volume->v_path,
1597             volume->v_cnidscheme);
1598     }
1599     if (volume->v_cdb == NULL) {
1600         LOG(log_error, logtype_afpd, "Fatal error: cannot open CNID for %s", volume->v_path);
1601         ret = AFPERR_MISC;
1602         goto openvol_err;
1603     }
1604
1605     /* Codepages */
1606
1607     if (!volume->v_volcodepage)
1608         volume->v_volcodepage = strdup("UTF8");
1609
1610     if ( (charset_t) -1 == ( volume->v_volcharset = add_charset(volume->v_volcodepage)) ) {
1611         LOG (log_error, logtype_afpd, "Setting codepage %s as volume codepage failed", volume->v_volcodepage);
1612         ret = AFPERR_MISC;
1613         goto openvol_err;
1614     }
1615
1616     if ( NULL == ( volume->v_vol = find_charset_functions(volume->v_volcodepage)) || volume->v_vol->flags & CHARSET_ICONV ) {
1617         LOG (log_warning, logtype_afpd, "WARNING: volume encoding %s is *not* supported by netatalk, expect problems !!!!", volume->v_volcodepage);
1618     }   
1619
1620     if (!volume->v_maccodepage)
1621         volume->v_maccodepage = strdup(obj->options.maccodepage);
1622
1623     if ( (charset_t) -1 == ( volume->v_maccharset = add_charset(volume->v_maccodepage)) ) {
1624         LOG (log_error, logtype_afpd, "Setting codepage %s as mac codepage failed", volume->v_maccodepage);
1625         ret = AFPERR_MISC;
1626         goto openvol_err;
1627     }
1628
1629     if ( NULL == ( volume->v_mac = find_charset_functions(volume->v_maccodepage)) || ! (volume->v_mac->flags & CHARSET_CLIENT) ) {
1630         LOG (log_error, logtype_afpd, "Fatal error: mac charset %s not supported", volume->v_maccodepage);
1631         ret = AFPERR_MISC;
1632         goto openvol_err;
1633     }   
1634
1635     ret  = stat_vol(bitmap, volume, rbuf, rbuflen);
1636     if (ret == AFP_OK) {
1637
1638         handle_special_folders( volume );
1639
1640         /*
1641          * If you mount a volume twice, the second time the trash appears on
1642          * the desk-top.  That's because the Mac remembers the DID for the
1643          * trash (even for volumes in different zones, on different servers).
1644          * Just so this works better, we prime the DID cache with the trash,
1645          * fixing the trash at DID 17.
1646          * FIXME (RL): should it be done inside a CNID backend ? (always returning Trash DID when asked) ?
1647          */
1648         if ((volume->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1649
1650             /* FIXME find db time stamp */
1651             cnid_getstamp(volume->v_cdb, volume->v_stamp, sizeof(volume->v_stamp));
1652             p = Trash;
1653             cname( volume, volume->v_dir, &p );
1654         }
1655         return( AFP_OK );
1656     }
1657
1658 openvol_err:
1659     if (volume->v_dir) {
1660         dirfree( volume->v_dir );
1661         volume->v_dir = volume->v_root = NULL;
1662     }
1663
1664     volume->v_flags &= ~AFPVOL_OPEN;
1665     if (volume->v_cdb != NULL) {
1666         cnid_close(volume->v_cdb);
1667         volume->v_cdb = NULL;
1668     }
1669     *rbuflen = 0;
1670     return ret;
1671 }
1672
1673 /* ------------------------- */
1674 static void closevol(struct vol *vol)
1675 {
1676     if (!vol)
1677         return;
1678
1679     dirfree( vol->v_root );
1680     vol->v_dir = NULL;
1681     if (vol->v_cdb != NULL) {
1682         cnid_close(vol->v_cdb);
1683         vol->v_cdb = NULL;
1684     }
1685
1686     if (vol->v_postexec) {
1687         afprun(0, vol->v_postexec, NULL);
1688     }
1689     if (vol->v_root_postexec) {
1690         afprun(1, vol->v_root_postexec, NULL);
1691     }
1692 }
1693
1694 /* ------------------------- */
1695 void close_all_vol(void)
1696 {
1697     struct vol  *ovol;
1698     curdir = NULL;
1699     for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
1700         if ( ovol->v_flags & AFPVOL_OPEN ) {
1701             ovol->v_flags &= ~AFPVOL_OPEN;
1702             closevol(ovol);
1703         }
1704     }
1705 }
1706
1707 /* ------------------------- */
1708 int afp_closevol(obj, ibuf, ibuflen, rbuf, rbuflen )
1709 AFPObj      *obj;
1710 char    *ibuf, *rbuf;
1711 int             ibuflen, *rbuflen;
1712 {
1713     struct vol  *vol, *ovol;
1714     u_int16_t   vid;
1715
1716     *rbuflen = 0;
1717     ibuf += 2;
1718     memcpy(&vid, ibuf, sizeof( vid ));
1719     if (NULL == ( vol = getvolbyvid( vid )) ) {
1720         return( AFPERR_PARAM );
1721     }
1722
1723     vol->v_flags &= ~AFPVOL_OPEN;
1724     for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
1725         if ( ovol->v_flags & AFPVOL_OPEN ) {
1726             break;
1727         }
1728     }
1729     if ( ovol != NULL ) {
1730         /* Even if chdir fails, we can't say afp_closevol fails. */
1731         if ( chdir( ovol->v_path ) == 0 ) {
1732             curdir = ovol->v_dir;
1733         }
1734     }
1735
1736     closevol(vol);
1737     if (vol->v_deleted) {
1738         showvol(vol->v_name);
1739         volume_free(vol);
1740         volume_unlink(vol);
1741     }
1742     return( AFP_OK );
1743 }
1744
1745 /* ------------------------- */
1746 struct vol *getvolbyvid(const u_int16_t vid )
1747 {
1748     struct vol  *vol;
1749
1750     for ( vol = Volumes; vol; vol = vol->v_next ) {
1751         if ( vid == vol->v_vid ) {
1752             break;
1753         }
1754     }
1755     if ( vol == NULL || ( vol->v_flags & AFPVOL_OPEN ) == 0 ) {
1756         return( NULL );
1757     }
1758
1759 #ifdef FORCE_UIDGID
1760     set_uidgid ( vol );
1761 #endif /* FORCE_UIDGID */
1762
1763     return( vol );
1764 }
1765
1766 /* ------------------------ */
1767 static int ext_cmp_key(const void *key, const void *obj)
1768 {
1769     const char          *p = key;
1770     const struct extmap *em = obj;
1771     return strdiacasecmp(p, em->em_ext);
1772 }
1773 struct extmap *getextmap(const char *path)
1774 {
1775     char          *p;
1776     struct extmap *em;
1777
1778     if (NULL == ( p = strrchr( path, '.' )) ) {
1779         return( Defextmap );
1780     }
1781     p++;
1782     if (!*p || !Extmap_cnt) {
1783         return( Defextmap );
1784     }
1785     em = bsearch(p, Extmap, Extmap_cnt, sizeof(struct extmap), ext_cmp_key);
1786     if (em) {
1787         return( em );
1788     } else {
1789         return( Defextmap );
1790     }
1791 }
1792
1793 /* ------------------------- */
1794 struct extmap *getdefextmap(void)
1795 {
1796     return( Defextmap );
1797 }
1798
1799 /* --------------------------
1800    poll if a volume is changed by other processes.
1801 */
1802 int  pollvoltime(obj)
1803 AFPObj *obj;
1804 {
1805     struct vol       *vol;
1806     struct timeval   tv;
1807     struct stat      st;
1808     
1809     if (!(afp_version > 21 && obj->options.server_notif)) 
1810          return 0;
1811
1812     if ( gettimeofday( &tv, 0 ) < 0 ) 
1813          return 0;
1814
1815     for ( vol = Volumes; vol; vol = vol->v_next ) {
1816         if ( (vol->v_flags & AFPVOL_OPEN)  && vol->v_mtime + 30 < tv.tv_sec) {
1817             if ( !stat( vol->v_path, &st ) && vol->v_mtime != st.st_mtime ) {
1818                 vol->v_mtime = st.st_mtime;
1819                 if (!obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED))
1820                     return -1;
1821                 return 1;
1822             }
1823         }
1824     }
1825     return 0;
1826 }
1827
1828 /* ------------------------- */
1829 void setvoltime(obj, vol )
1830 AFPObj *obj;
1831 struct vol      *vol;
1832 {
1833     struct timeval      tv;
1834
1835     /* just looking at vol->v_mtime is broken seriously since updates
1836      * from other users afpd processes never are seen.
1837      * This is not the most elegant solution (a shared memory between
1838      * the afpd processes would come closer)
1839      * [RS] */
1840
1841     if ( gettimeofday( &tv, 0 ) < 0 ) {
1842         LOG(log_error, logtype_afpd, "setvoltime: gettimeofday: %s", strerror(errno) );
1843         return;
1844     }
1845     if( utime( vol->v_path, NULL ) < 0 ) {
1846         /* write of time failed ... probably a read only filesys,
1847          * where no other users can interfere, so there's no issue here
1848          */
1849     }
1850
1851     /* a little granularity */
1852     if (vol->v_mtime < tv.tv_sec) {
1853         vol->v_mtime = tv.tv_sec;
1854         if (afp_version > 21 && obj->options.server_notif) {
1855             obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED);
1856         }
1857     }
1858 }
1859
1860 /* ------------------------- */
1861 int afp_getvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1862 AFPObj      *obj;
1863 char    *ibuf, *rbuf;
1864 int             ibuflen, *rbuflen;
1865 {
1866     struct vol  *vol;
1867     u_int16_t   vid, bitmap;
1868
1869     ibuf += 2;
1870     memcpy(&vid, ibuf, sizeof( vid ));
1871     ibuf += sizeof( vid );
1872     memcpy(&bitmap, ibuf, sizeof( bitmap ));
1873     bitmap = ntohs( bitmap );
1874
1875     if (NULL == ( vol = getvolbyvid( vid )) ) {
1876         *rbuflen = 0;
1877         return( AFPERR_PARAM );
1878     }
1879
1880     return stat_vol(bitmap, vol, rbuf, rbuflen);
1881 }
1882
1883 /* ------------------------- */
1884 int afp_setvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1885 AFPObj      *obj;
1886 char    *ibuf, *rbuf;
1887 int             ibuflen, *rbuflen;
1888 {
1889     struct adouble ad;
1890     struct vol  *vol;
1891     u_int16_t   vid, bitmap;
1892     u_int32_t   aint;
1893
1894     ibuf += 2;
1895     *rbuflen = 0;
1896
1897     memcpy(&vid, ibuf, sizeof( vid ));
1898     ibuf += sizeof( vid );
1899     memcpy(&bitmap, ibuf, sizeof( bitmap ));
1900     bitmap = ntohs( bitmap );
1901     ibuf += sizeof(bitmap);
1902
1903     if (( vol = getvolbyvid( vid )) == NULL ) {
1904         return( AFPERR_PARAM );
1905     }
1906
1907     if (vol->v_flags & AFPVOL_RO)
1908         return AFPERR_VLOCK;
1909
1910     /* we can only set the backup date. */
1911     if (bitmap != (1 << VOLPBIT_BDATE))
1912         return AFPERR_BITMAP;
1913
1914     ad_init(&ad, vol->v_adouble);
1915     if ( ad_open( vol->v_path, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR,
1916                   0666, &ad) < 0 ) {
1917         if (errno == EROFS)
1918             return AFPERR_VLOCK;
1919
1920         return AFPERR_ACCESS;
1921     }
1922
1923     memcpy(&aint, ibuf, sizeof(aint));
1924     ad_setdate(&ad, AD_DATE_BACKUP, aint);
1925     ad_flush(&ad, ADFLAGS_HF);
1926     ad_close(&ad, ADFLAGS_HF);
1927     return( AFP_OK );
1928 }
1929
1930 /* ------------------------- */
1931 int wincheck(const struct vol *vol, const char *path)
1932 {
1933     int len;
1934
1935     if (!(vol->v_flags & AFPVOL_MSWINDOWS))
1936         return 1;
1937
1938     /* empty paths are not allowed */
1939     if ((len = strlen(path)) == 0)
1940         return 0;
1941
1942     /* leading or trailing whitespaces are not allowed, carriage returns
1943      * and probably other whitespace is okay, tabs are not allowed
1944      */
1945     if ((path[0] == ' ') || (path[len-1] == ' '))
1946         return 0;
1947
1948     /* certain characters are not allowed */
1949     if (strpbrk(path, MSWINDOWS_BADCHARS))
1950         return 0;
1951
1952     /* everything else is okay */
1953     return 1;
1954 }
1955
1956
1957 /*
1958  * precreate a folder 
1959  * this is only intended for folders in the volume root 
1960  * It will *not* work if the folder name contains extended characters 
1961  */
1962 static int create_special_folder (const struct vol *vol, const struct _special_folder *folder)
1963 {
1964         char            *p;
1965         struct adouble  ad;
1966         u_int16_t       attr;
1967         struct stat     st;
1968         int             ret;
1969
1970
1971         p = (char *) malloc ( strlen(vol->v_path)+strlen(folder->name)+2);
1972         if ( p == NULL) {
1973                 LOG(log_error, logtype_afpd,"malloc failed");
1974                 exit (-1);
1975         }
1976         strcpy(p, vol->v_path);
1977         strcat(p, "/");
1978         strcat(p, folder->name);
1979
1980         if ( (ret = stat( p, &st )) < 0 ) {
1981                 if (folder->precreate) {
1982                     if (ad_mkdir(p, folder->mode & ~vol->v_umask)) {
1983                         LOG(log_debug, logtype_afpd,"Creating '%s' failed", folder->name);
1984                         free(p);
1985                         return -1;
1986                     }
1987                     ret = 0;
1988                 }
1989         }
1990
1991         if ( !ret && folder->hide) {
1992                 /* Hide it */
1993                 ad_init(&ad, vol->v_adouble);
1994                 if (ad_open( p, vol_noadouble(vol) | ADFLAGS_HF|ADFLAGS_DIR,
1995                         O_RDWR|O_CREAT, 0666, &ad) < 0) {
1996                         free (p);
1997                         return (-1);
1998                 }
1999                 if ((ad_get_HF_flags( &ad ) & O_CREAT) ) {
2000                         ad_setentrylen( &ad, ADEID_NAME, strlen(folder->name));
2001                         memcpy(ad_entry( &ad, ADEID_NAME ), folder->name,
2002                         ad_getentrylen( &ad, ADEID_NAME ));
2003                 }
2004  
2005                 ad_getattr(&ad, &attr);
2006                 attr |= htons( ntohs( attr ) | ATTRBIT_INVISIBLE );
2007                 ad_setattr(&ad, attr);
2008     
2009                 ad_flush( &ad, ADFLAGS_HF );
2010                 ad_close( &ad, ADFLAGS_HF );
2011         }
2012         free(p);
2013         return 0;
2014 }
2015
2016 static void handle_special_folders (const struct vol * vol)
2017 {
2018         const _special_folder *p = &special_folders[0];
2019
2020         if (vol->v_flags & AFPVOL_RO)
2021                 return;
2022
2023         for (; p->name != NULL; p++) {
2024                 create_special_folder (vol, p);
2025         }
2026 }
2027