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