]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/volume.c
change SIGHUP and SIGUSR1, SIGHUP reload config files, Volumes file are reloaded...
[netatalk.git] / etc / afpd / volume.c
1 /*
2  * $Id: volume.c,v 1.51.2.2 2003-05-26 11:17:25 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, "preexec_close") == 0)
427                 options[VOLOPT_PREEXEC].i_value = 1;
428             else if (strcasecmp(p, "root_preexec_close") == 0)
429                 options[VOLOPT_ROOTPREEXEC].i_value = 1;
430             p = strtok(NULL, ",");
431         }
432
433 #ifdef CNID_DB
434
435     } else if (optionok(tmp, "dbpath:", val)) {
436         setoption(options, save, VOLOPT_DBPATH, val);
437 #endif /* CNID_DB */
438
439     } else if (optionok(tmp, "umask:", val)) {
440         options[VOLOPT_UMASK].i_value = (int)strtol(val, (char **)NULL, 8);
441     } else if (optionok(tmp, "mapchars:",val)) {
442         setoption(options, save, VOLOPT_MAPCHARS, val);
443
444     } else if (optionok(tmp, "password:", val)) {
445         setoption(options, save, VOLOPT_PASSWORD, val);
446
447 #ifdef FORCE_UIDGID
448
449         /* this code allows forced uid/gid per volume settings */
450     } else if (optionok(tmp, "forceuid:", val)) {
451         setoption(options, save, VOLOPT_FORCEUID, val);
452     } else if (optionok(tmp, "forcegid:", val)) {
453         setoption(options, save, VOLOPT_FORCEGID, val);
454
455 #endif /* FORCE_UIDGID */
456     } else if (optionok(tmp, "root_preexec:", val)) {
457         setoption(options, save, VOLOPT_ROOTPREEXEC, val);
458
459     } else if (optionok(tmp, "preexec:", val)) {
460         setoption(options, save, VOLOPT_PREEXEC, val);
461
462     } else if (optionok(tmp, "root_postexec:", val)) {
463         setoption(options, save, VOLOPT_ROOTPOSTEXEC, val);
464
465     } else if (optionok(tmp, "postexec:", val)) {
466         setoption(options, save, VOLOPT_POSTEXEC, val);
467
468     } else {
469         /* ignore unknown options */
470         LOG(log_debug, logtype_afpd, "ignoring unknown volume option: %s", tmp);
471
472     } 
473 }
474
475 /* ----------------- */
476 static void showvol(const char *name)
477 {
478     struct vol  *volume;
479     for ( volume = Volumes; volume; volume = volume->v_next ) {
480         if ( !strcasecmp( volume->v_name, name ) && volume->v_hide) {
481             volume->v_hide = 0;
482             return;
483         }
484     }
485 }
486
487 /* ------------------------------- */
488 static int creatvol(AFPObj *obj, struct passwd *pwd, 
489                     char *path, char *name, 
490                     struct vol_option *options, 
491                     const int user /* user defined volume */
492                     )
493 {
494     struct vol  *volume;
495     int         vlen;
496     int         hide = 0;
497
498     if ( name == NULL || *name == '\0' ) {
499         if ((name = strrchr( path, '/' )) == NULL) {
500             return -1;  /* Obviously not a fully qualified path */
501         }
502
503         /* if you wish to share /, you need to specify a name. */
504         if (*++name == '\0')
505             return -1;
506     }
507
508     for ( volume = Volumes; volume; volume = volume->v_next ) {
509         if ( strcasecmp( volume->v_name, name ) == 0 ) {
510            if (volume->v_deleted) {
511                hide = 1;
512            }
513            else {
514                return -1;       /* Won't be able to access it, anyway... */
515            }
516         }
517     }
518
519     vlen = strlen( name );
520     if ( vlen > AFPVOL_NAMELEN ) {
521         vlen = AFPVOL_NAMELEN;
522         name[AFPVOL_NAMELEN] = '\0';
523     }
524
525     if (!( volume = (struct vol *)calloc(1, sizeof( struct vol ))) ) {
526         LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
527         return -1;
528     }
529     if (! ( volume->v_name = (char *)malloc( vlen + 1 ) ) ) {
530         LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
531         free(volume);
532         return -1;
533     }
534     if (!( volume->v_path = (char *)malloc( strlen( path ) + 1 )) ) {
535         LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
536         free(volume->v_name);
537         free(volume);
538         return -1;
539     }
540     volume->v_hide = hide;
541     strcpy( volume->v_name, name);
542     strcpy( volume->v_path, path );
543
544 #ifdef __svr4__
545     volume->v_qfd = -1;
546 #endif /* __svr4__ */
547     volume->v_vid = lastvid++;
548     volume->v_lastdid = 17;
549
550     /* handle options */
551     if (options) {
552         /* should we casefold? */
553         volume->v_casefold = options[VOLOPT_CASEFOLD].i_value;
554
555         /* shift in some flags */
556         volume->v_flags = options[VOLOPT_FLAGS].i_value;
557
558         /* read in the code pages */
559         if (options[VOLOPT_CODEPAGE].c_value)
560             codepage_read(volume, options[VOLOPT_CODEPAGE].c_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 #ifdef CNID_DB
569         if (options[VOLOPT_DBPATH].c_value)
570             volume->v_dbpath = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_DBPATH].c_value, pwd, path);
571 #endif
572
573         if (options[VOLOPT_UMASK].i_value)
574             volume->v_umask = (mode_t)options[VOLOPT_UMASK].i_value;
575
576 #ifdef FORCE_UIDGID
577         if (options[VOLOPT_FORCEUID].c_value) {
578             volume->v_forceuid = strdup(options[VOLOPT_FORCEUID].c_value);
579         } else {
580             volume->v_forceuid = NULL; /* set as null so as to return 0 later on */
581         }
582
583         if (options[VOLOPT_FORCEGID].c_value) {
584             volume->v_forcegid = strdup(options[VOLOPT_FORCEGID].c_value);
585         } else {
586             volume->v_forcegid = NULL; /* set as null so as to return 0 later on */
587         }
588 #endif
589         if (!user) {
590             if (options[VOLOPT_PREEXEC].c_value)
591                 volume->v_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_PREEXEC].c_value, pwd, path);
592             volume->v_preexec_close = options[VOLOPT_PREEXEC].i_value;
593
594             if (options[VOLOPT_POSTEXEC].c_value)
595                 volume->v_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_POSTEXEC].c_value, pwd, path);
596
597             if (options[VOLOPT_ROOTPREEXEC].c_value)
598                 volume->v_root_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPREEXEC].c_value, pwd, path);
599             volume->v_root_preexec_close = options[VOLOPT_ROOTPREEXEC].i_value;
600
601             if (options[VOLOPT_ROOTPOSTEXEC].c_value)
602                 volume->v_root_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPOSTEXEC].c_value, pwd, path);
603         }
604     }
605
606     volume->v_next = Volumes;
607     Volumes = volume;
608     return 0;
609 }
610
611 /* ---------------- */
612 static char *myfgets( buf, size, fp )
613 char    *buf;
614 int             size;
615 FILE    *fp;
616 {
617     char        *p;
618     int         c;
619
620     p = buf;
621     while ((EOF != ( c = getc( fp )) ) && ( size > 0 )) {
622         if ( c == '\n' || c == '\r' ) {
623             *p++ = '\n';
624             break;
625         } else {
626             *p++ = c;
627         }
628         size--;
629     }
630
631     if ( p == buf ) {
632         return( NULL );
633     } else {
634         *p = '\0';
635         return( buf );
636     }
637 }
638
639
640 /* check access list. this function wants something of the following
641  * form:
642  *        @group,name,name2,@group2,name3
643  *
644  * a NULL argument allows everybody to have access.
645  * we return three things:
646  *     -1: no list
647  *      0: list exists, but name isn't in it
648  *      1: in list
649  */
650 static int accessvol(args, name)
651 const char *args;
652 const char *name;
653 {
654     char buf[MAXPATHLEN + 1], *p;
655     struct group *gr;
656
657     if (!args)
658         return -1;
659
660     strncpy(buf, args, sizeof(buf));
661     if ((p = strtok(buf, ",")) == NULL) /* nothing, return okay */
662         return -1;
663
664     while (p) {
665         if (*p == '@') { /* it's a group */
666             if ((gr = getgrnam(p + 1)) && gmem(gr->gr_gid))
667                 return 1;
668         } else if (strcmp(p, name) == 0) /* it's a user name */
669             return 1;
670         p = strtok(NULL, ",");
671     }
672
673     return 0;
674 }
675
676 static void setextmap( ext, type, creator, user)
677 char            *ext, *type, *creator;
678 int                     user;
679 {
680     struct extmap       *em;
681     int                 cnt;
682
683     if (Extmap == NULL) {
684         if (( Extmap = calloc(1, sizeof( struct extmap ))) == NULL ) {
685             LOG(log_error, logtype_afpd, "setextmap: calloc: %s", strerror(errno) );
686             return;
687         }
688     }
689     ext++;
690     for ( em = Extmap, cnt = 0; em->em_ext; em++, cnt++) {
691         if ( (strdiacasecmp( em->em_ext, ext )) == 0 ) {
692             break;
693         }
694     }
695
696     if ( em->em_ext == NULL ) {
697         if (!(Extmap  = realloc( Extmap, sizeof( struct extmap ) * (cnt +2))) ) {
698             LOG(log_error, logtype_afpd, "setextmap: realloc: %s", strerror(errno) );
699             return;
700         }
701         (Extmap +cnt +1)->em_ext = NULL;
702         em = Extmap +cnt;
703     } else if ( !user ) {
704         return;
705     }
706     if (em->em_ext)
707         free(em->em_ext);
708
709     if (!(em->em_ext = strdup(  ext))) {
710         LOG(log_error, logtype_afpd, "setextmap: strdup: %s", strerror(errno) );
711         return;
712     }
713
714     if ( *type == '\0' ) {
715         memcpy(em->em_type, "????", sizeof( em->em_type ));
716     } else {
717         memcpy(em->em_type, type, sizeof( em->em_type ));
718     }
719     if ( *creator == '\0' ) {
720         memcpy(em->em_creator, "UNIX", sizeof( em->em_creator ));
721     } else {
722         memcpy(em->em_creator, creator, sizeof( em->em_creator ));
723     }
724 }
725
726 /* -------------------------- */
727 static int extmap_cmp(const void *map1, const void *map2)
728 {
729     const struct extmap *em1 = map1;
730     const struct extmap *em2 = map2;
731     return strdiacasecmp(em1->em_ext, em2->em_ext);
732 }
733
734 static void sortextmap( void)
735 {
736     struct extmap       *em;
737
738     Extmap_cnt = 0;
739     if ((em = Extmap) == NULL) {
740         return;
741     }
742     while (em->em_ext) {
743         em++;
744         Extmap_cnt++;
745     }
746     if (Extmap_cnt) {
747         qsort(Extmap, Extmap_cnt, sizeof(struct extmap), extmap_cmp);
748         Defextmap = Extmap;
749     }
750 }
751
752 /* ----------------------
753 */
754 static void free_extmap( void)
755 {
756     if (Extmap) {
757         free(Extmap);
758         Extmap = NULL;
759         Defextmap = Extmap;
760         Extmap_cnt = 0;
761     }
762 }
763
764 /* ----------------------
765 */
766 static int volfile_changed(struct afp_volume_name *p) 
767 {
768     struct stat      st;
769     char *name;
770     
771     if (p->full_name) 
772         name = p->full_name;
773     else
774         name = p->name;
775         
776     if (!stat( name, &st) && st.st_mtime > p->mtime) {
777         p->mtime = st.st_mtime;
778         return 1;
779     }
780     return 0;
781 }
782
783 /* ----------------------
784  * Read a volume configuration file and add the volumes contained within to
785  * the global volume list.  If p2 is non-NULL, the file that is opened is
786  * p1/p2
787  * 
788  * Lines that begin with # and blank lines are ignored.
789  * Volume lines are of the form:
790  *              <unix path> [<volume name>] [allow:<user>,<@group>,...] \
791  *                           [codepage:<file>] [casefold:<num>]
792  *              <extension> TYPE [CREATOR]
793  */
794 static int readvolfile(obj, p1, p2, user, pwent)
795 AFPObj      *obj;
796 struct afp_volume_name  *p1;
797 char        *p2;
798 int             user;
799 struct passwd *pwent;
800 {
801     FILE                *fp;
802     char                path[ MAXPATHLEN + 1], tmp[ MAXPATHLEN + 1],
803     volname[ AFPVOL_NAMELEN + 1 ], buf[ BUFSIZ ],
804     type[ 5 ], creator[ 5 ];
805     char                *u, *p;
806     struct passwd       *pw;
807     struct vol_option   options[VOLOPT_NUM], save_options[VOLOPT_NUM];
808     int                 i;
809     struct stat         st;
810     int                 fd;
811
812     if (!p1->name)
813         return -1;
814     p1->mtime = 0;
815     strcpy( path, p1->name );
816     if ( p2 != NULL ) {
817         strcat( path, "/" );
818         strcat( path, p2 );
819         if (p1->full_name) {
820             free(p1->full_name);
821         }
822         p1->full_name = strdup(path);
823     }
824
825     if (NULL == ( fp = fopen( path, "r" )) ) {
826         return( -1 );
827     }
828     fd = fileno(fp);
829     if (fd != -1 && !fstat( fd, &st) ) {
830         p1->mtime = st.st_mtime;
831     }
832
833     memset(save_options, 0, sizeof(save_options));
834     while ( myfgets( buf, sizeof( buf ), fp ) != NULL ) {
835         initline( strlen( buf ), buf );
836         parseline( sizeof( path ) - 1, path );
837         switch ( *path ) {
838         case '\0' :
839         case '#' :
840             continue;
841
842         case ':':
843             /* change the default options for this file */
844             if (strncmp(path, VOLOPT_DEFAULT, VOLOPT_DEFAULT_LEN) == 0) {
845                 *tmp = '\0';
846                 for (i = 0; i < VOLOPT_NUM; i++) {
847                     if (parseline( sizeof( path ) - VOLOPT_DEFAULT_LEN - 1,
848                                    path + VOLOPT_DEFAULT_LEN) < 0)
849                         break;
850                     volset(save_options, NULL, tmp, sizeof(tmp) - 1,
851                            obj->options.nlspath, path + VOLOPT_DEFAULT_LEN);
852                 }
853             }
854             break;
855
856         case '~' :
857             if (( p = strchr( path, '/' )) != NULL ) {
858                 *p++ = '\0';
859             }
860             u = path;
861             u++;
862             if ( *u == '\0' ) {
863                 u = obj->username;
864             }
865             if ( u == NULL || *u == '\0' || ( pw = getpwnam( u )) == NULL ) {
866                 continue;
867             }
868             strcpy( tmp, pw->pw_dir );
869             if ( p != NULL && *p != '\0' ) {
870                 strcat( tmp, "/" );
871                 strcat( tmp, p );
872             }
873             /* Tag a user's home directory with their umask.  Note, this will
874              * be overwritten if the user actually specifies a umask: option
875              * for a '~' volume. */
876             save_options[VOLOPT_UMASK].i_value = obj->options.save_mask;
877             /* fall through */
878
879         case '/' :
880             /* send path through variable substitution */
881             if (*path != '~') /* need to copy path to tmp */
882                 strcpy(tmp, path);
883             if (!pwent)
884                 pwent = getpwnam(obj->username);
885             volxlate(obj, path, sizeof(path) - 1, tmp, pwent, NULL);
886
887             /* this is sort of braindead. basically, i want to be
888              * able to specify things in any order, but i don't want to 
889              * re-write everything. 
890              *
891              * currently we have options: 
892              *   volname
893              *   codepage:x
894              *   casefold:x
895              *   allow:x,y,@z
896              *   deny:x,y,@z
897              *   rwlist:x,y,@z
898              *   rolist:x,y,@z
899              *   options:prodos,crlf,noadouble,ro...
900              *   dbpath:x
901              *   password:x
902              *   preexec:x
903              *
904              *   namemask:x,y,!z  (not implemented yet)
905              */
906             memcpy(options, save_options, sizeof(options));
907             *volname = '\0';
908
909             /* read in up to VOLOP_NUM possible options */
910             for (i = 0; i < VOLOPT_NUM; i++) {
911                 if (parseline( sizeof( tmp ) - 1, tmp ) < 0)
912                     break;
913
914                 volset(options, save_options, volname, sizeof(volname) - 1,obj->options.nlspath, tmp);
915             }
916
917             /* check allow/deny lists:
918                allow -> either no list (-1), or in list (1)
919                deny -> either no list (-1), or not in list (0) */
920             if (accessvol(options[VOLOPT_ALLOW].c_value, obj->username) &&
921                     (accessvol(options[VOLOPT_DENY].c_value, obj->username) < 1)) {
922
923                 /* handle read-only behaviour. semantics:
924                  * 1) neither the rolist nor the rwlist exist -> rw
925                  * 2) rolist exists -> ro if user is in it.
926                  * 3) rwlist exists -> ro unless user is in it. */
927                 if (((options[VOLOPT_FLAGS].i_value & AFPVOL_RO) == 0) &&
928                         ((accessvol(options[VOLOPT_ROLIST].c_value,
929                                     obj->username) == 1) ||
930                          !accessvol(options[VOLOPT_RWLIST].c_value,
931                                     obj->username)))
932                     options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
933
934                 /* do variable substitution for volname */
935                 volxlate(obj, tmp, sizeof(tmp) - 1, volname, pwent, path);
936                 creatvol(obj, pwent, path, tmp, options, p2 != NULL);
937             }
938             volfree(options, save_options);
939             break;
940
941         case '.' :
942             parseline( sizeof( type ) - 1, type );
943             parseline( sizeof( creator ) - 1, creator );
944             setextmap( path, type, creator, user);
945             break;
946
947         default :
948             break;
949         }
950     }
951     volfree(save_options, NULL);
952     sortextmap();
953     if ( fclose( fp ) != 0 ) {
954         LOG(log_error, logtype_afpd, "readvolfile: fclose: %s", strerror(errno) );
955     }
956     p1->loaded = 1;
957     return( 0 );
958 }
959
960 /* ------------------------------- */
961 static void volume_free(struct vol *vol)
962 {
963     free(vol->v_name);
964     vol->v_name = NULL;
965     free(vol->v_path);
966     codepage_free(vol);
967     free(vol->v_password);
968     free(vol->v_veto);
969 #ifdef CNID_DB
970     free(vol->v_dbpath);
971 #endif /* CNID_DB */
972 #ifdef FORCE_UIDGID
973     free(vol->v_forceuid);
974     free(vol->v_forcegid);
975 #endif /* FORCE_UIDGID */
976 }
977
978 /* ------------------------------- */
979 static void free_volumes(void )
980 {
981     struct vol  *vol;
982     struct vol  *nvol, *ovol;
983
984     for ( vol = Volumes; vol; vol = vol->v_next ) {
985         if (( vol->v_flags & AFPVOL_OPEN ) ) {
986             vol->v_deleted = 1;
987             continue;
988         }
989         volume_free(vol);
990     }
991
992     for ( vol = Volumes, ovol = NULL; vol; vol = nvol) {
993         nvol = vol->v_next;
994
995         if (vol->v_name == NULL) {
996            if (Volumes == vol) {
997                Volumes = nvol;
998            }
999            if (!ovol) {
1000                ovol = Volumes;
1001            }
1002            else {
1003               ovol->v_next = nvol;
1004            }
1005            free(vol);
1006         }
1007         else {
1008            ovol = vol;
1009         }
1010     }
1011 }
1012
1013 /* ------------------------------- */
1014 static void volume_unlink(struct vol *volume)
1015 {
1016 struct vol *vol, *ovol, *nvol;
1017
1018     if (volume == Volumes) {
1019         Volumes = Volumes->v_next;
1020         return;
1021     }
1022     for ( vol = Volumes, ovol = NULL; vol; vol = nvol) {
1023         nvol = vol->v_next;
1024
1025         if (vol == volume) {
1026            if (!ovol) {
1027                ovol = Volumes;
1028            }
1029            else {
1030               ovol->v_next = nvol;
1031            }
1032            break;
1033         }
1034         else {
1035            ovol = vol;
1036         }
1037     }
1038 }
1039
1040
1041 static int getvolspace( vol, bfree, btotal, xbfree, xbtotal, bsize )
1042 struct vol      *vol;
1043 u_int32_t       *bfree, *btotal, *bsize;
1044 VolSpace    *xbfree, *xbtotal;
1045 {
1046     int         spaceflag, rc;
1047     u_int32_t   maxsize;
1048 #ifndef NO_QUOTA_SUPPORT
1049     VolSpace    qfree, qtotal;
1050 #endif
1051
1052     spaceflag = AFPVOL_GVSMASK & vol->v_flags;
1053     /* report up to 2GB if afp version is < 2.2 (4GB if not) */
1054     maxsize = (vol->v_flags & AFPVOL_A2VOL) ? 0x01fffe00 :
1055               (((afp_version < 22) || (vol->v_flags & AFPVOL_LIMITSIZE))
1056                ? 0x7fffffffL : 0xffffffffL);
1057
1058 #ifdef AFS
1059     if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_AFSGVS ) {
1060         if ( afs_getvolspace( vol, xbfree, xbtotal, bsize ) == AFP_OK ) {
1061             vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_AFSGVS;
1062             goto getvolspace_done;
1063         }
1064     }
1065 #endif
1066
1067     if (( rc = ustatfs_getvolspace( vol, xbfree, xbtotal,
1068                                     bsize)) != AFP_OK ) {
1069         return( rc );
1070     }
1071
1072 #define min(a,b)        ((a)<(b)?(a):(b))
1073 #ifndef NO_QUOTA_SUPPORT
1074     if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_UQUOTA ) {
1075         if ( uquota_getvolspace( vol, &qfree, &qtotal, *bsize ) == AFP_OK ) {
1076             vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_UQUOTA;
1077             *xbfree = min(*xbfree, qfree);
1078             *xbtotal = min( *xbtotal, qtotal);
1079             goto getvolspace_done;
1080         }
1081     }
1082 #endif
1083     vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_USTATFS;
1084
1085 getvolspace_done:
1086     *bfree = min( *xbfree, maxsize);
1087     *btotal = min( *xbtotal, maxsize);
1088     return( AFP_OK );
1089 }
1090
1091 static int getvolparams( bitmap, vol, st, buf, buflen )
1092 u_int16_t       bitmap;
1093 struct vol      *vol;
1094 struct stat     *st;
1095 char    *buf;
1096 int             *buflen;
1097 {
1098     struct adouble      ad;
1099     int                 bit = 0, isad = 1;
1100     u_int32_t           aint;
1101     u_short             ashort;
1102     u_int32_t           bfree, btotal, bsize;
1103     VolSpace            xbfree, xbtotal; /* extended bytes */
1104     char                *data, *nameoff = NULL;
1105     char                *slash;
1106
1107     /* courtesy of jallison@whistle.com:
1108      * For MacOS8.x support we need to create the
1109      * .Parent file here if it doesn't exist. */
1110
1111     memset(&ad, 0, sizeof(ad));
1112     if ( ad_open( vol->v_path, vol_noadouble(vol) |
1113                   ADFLAGS_HF|ADFLAGS_DIR, O_RDWR | O_CREAT,
1114                   0666, &ad) < 0 ) {
1115         isad = 0;
1116
1117     } else if (ad_get_HF_flags( &ad ) & O_CREAT) {
1118         slash = strrchr( vol->v_path, '/' );
1119         if(slash)
1120             slash++;
1121         else
1122             slash = vol->v_path;
1123
1124         ad_setentrylen( &ad, ADEID_NAME, strlen( slash ));
1125         memcpy(ad_entry( &ad, ADEID_NAME ), slash,
1126                ad_getentrylen( &ad, ADEID_NAME ));
1127         ad_setdate(&ad, AD_DATE_CREATE | AD_DATE_UNIX, st->st_mtime);
1128         ad_flush(&ad, ADFLAGS_HF);
1129     }
1130
1131     if (( bitmap & ( (1<<VOLPBIT_BFREE)|(1<<VOLPBIT_BTOTAL) |
1132                      (1<<VOLPBIT_XBFREE)|(1<<VOLPBIT_XBTOTAL) |
1133                      (1<<VOLPBIT_BSIZE)) ) != 0 ) {
1134         if ( getvolspace( vol, &bfree, &btotal, &xbfree, &xbtotal,
1135                           &bsize) < 0 ) {
1136             if ( isad ) {
1137                 ad_close( &ad, ADFLAGS_HF );
1138             }
1139             return( AFPERR_PARAM );
1140         }
1141     }
1142
1143     data = buf;
1144     while ( bitmap != 0 ) {
1145         while (( bitmap & 1 ) == 0 ) {
1146             bitmap = bitmap>>1;
1147             bit++;
1148         }
1149
1150         switch ( bit ) {
1151         case VOLPBIT_ATTR :
1152             ashort = 0;
1153 #ifdef CNID_DB
1154             if (0 == (vol->v_flags & AFPVOL_NOFILEID) && vol->v_db != NULL) {
1155                 ashort = VOLPBIT_ATTR_FILEID;
1156             }
1157 #endif /* CNID_DB */
1158             /* check for read-only.
1159              * NOTE: we don't actually set the read-only flag unless
1160              *       it's passed in that way as it's possible to mount
1161              *       a read-write filesystem under a read-only one. */
1162             if ((vol->v_flags & AFPVOL_RO) ||
1163                     ((utime(vol->v_path, NULL) < 0) && (errno == EROFS))) {
1164                 ashort |= VOLPBIT_ATTR_RO;
1165             }
1166             ashort |= VOLPBIT_ATTR_CATSEARCH;
1167             if (afp_version >= 30) {
1168                 ashort |= VOLPBIT_ATTR_UTF8;
1169             }
1170             ashort = htons(ashort);
1171             memcpy(data, &ashort, sizeof( ashort ));
1172             data += sizeof( ashort );
1173             break;
1174
1175         case VOLPBIT_SIG :
1176             ashort = htons( AFPVOLSIG_DEFAULT );
1177             memcpy(data, &ashort, sizeof( ashort ));
1178             data += sizeof( ashort );
1179             break;
1180
1181         case VOLPBIT_CDATE :
1182             if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
1183                 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1184             memcpy(data, &aint, sizeof( aint ));
1185             data += sizeof( aint );
1186             break;
1187
1188         case VOLPBIT_MDATE :
1189             if ( st->st_mtime > vol->v_time ) {
1190                 vol->v_time = st->st_mtime;
1191                 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1192             } else {
1193                 aint = AD_DATE_FROM_UNIX(vol->v_time);
1194             }
1195             memcpy(data, &aint, sizeof( aint ));
1196             data += sizeof( aint );
1197             break;
1198
1199         case VOLPBIT_BDATE :
1200             if (!isad ||  (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1201                 aint = AD_DATE_START;
1202             memcpy(data, &aint, sizeof( aint ));
1203             data += sizeof( aint );
1204             break;
1205
1206         case VOLPBIT_VID :
1207             memcpy(data, &vol->v_vid, sizeof( vol->v_vid ));
1208             data += sizeof( vol->v_vid );
1209             break;
1210
1211         case VOLPBIT_BFREE :
1212             bfree = htonl( bfree );
1213             memcpy(data, &bfree, sizeof( bfree ));
1214             data += sizeof( bfree );
1215             break;
1216
1217         case VOLPBIT_BTOTAL :
1218             btotal = htonl( btotal );
1219             memcpy(data, &btotal, sizeof( btotal ));
1220             data += sizeof( btotal );
1221             break;
1222
1223 #ifndef NO_LARGE_VOL_SUPPORT
1224         case VOLPBIT_XBFREE :
1225             xbfree = hton64( xbfree );
1226 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1227             bcopy(&xbfree, data, sizeof(xbfree));
1228 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1229             memcpy(data, &xbfree, sizeof( xbfree ));
1230 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1231             data += sizeof( xbfree );
1232             break;
1233
1234         case VOLPBIT_XBTOTAL :
1235             xbtotal = hton64( xbtotal );
1236 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1237             bcopy(&xbtotal, data, sizeof(xbtotal));
1238 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1239             memcpy(data, &xbtotal, sizeof( xbtotal ));
1240 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1241             data += sizeof( xbfree );
1242             break;
1243 #endif /* ! NO_LARGE_VOL_SUPPORT */
1244
1245         case VOLPBIT_NAME :
1246             nameoff = data;
1247             data += sizeof( u_int16_t );
1248             break;
1249
1250         case VOLPBIT_BSIZE:  /* block size */
1251             bsize = htonl(bsize);
1252             memcpy(data, &bsize, sizeof(bsize));
1253             data += sizeof(bsize);
1254             break;
1255
1256         default :
1257             if ( isad ) {
1258                 ad_close( &ad, ADFLAGS_HF );
1259             }
1260             return( AFPERR_BITMAP );
1261         }
1262         bitmap = bitmap>>1;
1263         bit++;
1264     }
1265     if ( nameoff ) {
1266         ashort = htons( data - buf );
1267         memcpy(nameoff, &ashort, sizeof( ashort ));
1268         aint = strlen( vol->v_name );
1269         *data++ = aint;
1270         memcpy(data, vol->v_name, aint );
1271         data += aint;
1272     }
1273     if ( isad ) {
1274         ad_close( &ad, ADFLAGS_HF );
1275     }
1276     *buflen = data - buf;
1277     return( AFP_OK );
1278 }
1279
1280 /* ------------------------------- */
1281 void load_volumes(AFPObj *obj)
1282 {
1283     struct passwd       *pwent;
1284
1285     if (Volumes) {
1286         int changed = 0;
1287         
1288         /* check files date */
1289         if (obj->options.defaultvol.loaded) {
1290             changed = volfile_changed(&obj->options.defaultvol);
1291         }
1292         if (obj->options.systemvol.loaded) {
1293             changed |= volfile_changed(&obj->options.systemvol);
1294         }
1295         if (obj->options.uservol.loaded) {
1296             changed |= volfile_changed(&obj->options.uservol);
1297         }
1298         if (!changed)
1299             return;
1300         
1301         free_extmap();
1302         free_volumes();
1303     }
1304     
1305     pwent = getpwnam(obj->username);
1306     if ( (obj->options.flags & OPTION_USERVOLFIRST) == 0 ) {
1307         readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent);
1308     }
1309
1310     if ((*obj->username == '\0') || (obj->options.flags & OPTION_NOUSERVOL)) {
1311         readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent);
1312     } else if (pwent) {
1313         /*
1314         * Read user's AppleVolumes or .AppleVolumes file
1315         * If neither are readable, read the default volumes file. if
1316         * that doesn't work, create a user share.
1317         */
1318         obj->options.uservol.name = strdup(pwent->pw_dir);
1319         if ( readvolfile(obj, &obj->options.uservol,    "AppleVolumes", 1, pwent) < 0 &&
1320                 readvolfile(obj, &obj->options.uservol, ".AppleVolumes", 1, pwent) < 0 &&
1321                 readvolfile(obj, &obj->options.uservol, "applevolumes", 1, pwent) < 0 &&
1322                 readvolfile(obj, &obj->options.uservol, ".applevolumes", 1, pwent) < 0 &&
1323                 obj->options.defaultvol.name != NULL ) {
1324             if (readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent) < 0)
1325                 creatvol(pwent->pw_dir, NULL, NULL);
1326         }
1327     }
1328     if ( obj->options.flags & OPTION_USERVOLFIRST ) {
1329         readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent );
1330     }
1331 }
1332
1333 /* ------------------------------- */
1334 int afp_getsrvrparms(obj, ibuf, ibuflen, rbuf, rbuflen )
1335 AFPObj      *obj;
1336 char    *ibuf, *rbuf;
1337 int     ibuflen, *rbuflen;
1338 {
1339     struct timeval      tv;
1340     struct stat         st;
1341     struct vol          *volume;
1342     char        *data;
1343     int                 vcnt, len;
1344
1345
1346     load_volumes(obj);
1347
1348     data = rbuf + 5;
1349     for ( vcnt = 0, volume = Volumes; volume; volume = volume->v_next ) {
1350         if ( stat( volume->v_path, &st ) < 0 ) {
1351             LOG(log_info, logtype_afpd, "afp_getsrvrparms: stat %s: %s",
1352                 volume->v_path, strerror(errno) );
1353             continue;           /* can't access directory */
1354         }
1355         if (!S_ISDIR(st.st_mode)) {
1356             continue;           /* not a dir */
1357         }
1358         if (volume->v_hide) {
1359             continue;           /* config file changed but the volume was mounted */
1360         
1361         }
1362         /* set password bit if there's a volume password */
1363         *data = (volume->v_password) ? AFPSRVR_PASSWD : 0;
1364
1365         /* Apple 2 clients running ProDOS-8 expect one volume to have
1366            bit 0 of this byte set.  They will not recognize anything
1367            on the server unless this is the case.  I have not
1368            completely worked this out, but it's related to booting
1369            from the server.  Support for that function is a ways
1370            off.. <shirsch@ibm.net> */
1371         *data++ |= (volume->v_flags & AFPVOL_A2VOL) ? AFPSRVR_CONFIGINFO : 0;
1372         len = strlen( volume->v_name );
1373         *data++ = len;
1374         memcpy(data, volume->v_name, len );
1375         data += len;
1376         vcnt++;
1377     }
1378
1379     *rbuflen = data - rbuf;
1380     data = rbuf;
1381     if ( gettimeofday( &tv, 0 ) < 0 ) {
1382         LOG(log_error, logtype_afpd, "afp_getsrvrparms: gettimeofday: %s", strerror(errno) );
1383         *rbuflen = 0;
1384         return AFPERR_PARAM;
1385     }
1386     tv.tv_sec = AD_DATE_FROM_UNIX(tv.tv_sec);
1387     memcpy(data, &tv.tv_sec, sizeof( u_int32_t));
1388     data += sizeof( u_int32_t);
1389     *data = vcnt;
1390     return( AFP_OK );
1391 }
1392
1393 /* ------------------------- 
1394  * we are the user here
1395 */
1396 int afp_openvol(obj, ibuf, ibuflen, rbuf, rbuflen )
1397 AFPObj      *obj;
1398 char    *ibuf, *rbuf;
1399 int             ibuflen, *rbuflen;
1400 {
1401     struct stat st;
1402     char        *volname;
1403 #ifndef CNID_DB
1404     char        *p;
1405 #else
1406     int         opened = 0;
1407 #endif /* CNID_DB */
1408     struct vol  *volume;
1409     struct dir  *dir;
1410     int         len, ret, buflen;
1411     u_int16_t   bitmap;
1412
1413     ibuf += 2;
1414     memcpy(&bitmap, ibuf, sizeof( bitmap ));
1415     bitmap = ntohs( bitmap );
1416     ibuf += sizeof( bitmap );
1417     if (( bitmap & (1<<VOLPBIT_VID)) == 0 ) {
1418         ret = AFPERR_BITMAP;
1419         goto openvol_err;
1420     }
1421
1422     len = (unsigned char)*ibuf++;
1423     volname = obj->oldtmp;
1424     memcpy(volname, ibuf, len );
1425     *(volname +  len) = '\0';
1426     ibuf += len;
1427     if ((len + 1) & 1) /* pad to an even boundary */
1428         ibuf++;
1429
1430     load_volumes(obj);
1431
1432     for ( volume = Volumes; volume; volume = volume->v_next ) {
1433         if ( strcasecmp( volname, volume->v_name ) == 0 ) {
1434             break;
1435         }
1436     }
1437
1438     if ( volume == NULL ) {
1439         ret = AFPERR_PARAM;
1440         goto openvol_err;
1441     }
1442
1443     /* check for a volume password */
1444     if (volume->v_password && strncmp(ibuf, volume->v_password, VOLPASSLEN)) {
1445         ret = AFPERR_ACCESS;
1446         goto openvol_err;
1447     }
1448     /* FIXME 
1449     */
1450     if (afp_version >= 30) {
1451         volume->max_filename = 255;
1452     }
1453     else {
1454         volume->max_filename = MACFILELEN;
1455     }
1456     if (( volume->v_flags & AFPVOL_OPEN  ) == 0 ) {
1457         /* FIXME unix name != mac name */
1458         if ((dir = dirnew(volume->v_name, volume->v_name) ) == NULL) {
1459             LOG(log_error, logtype_afpd, "afp_openvol: malloc: %s", strerror(errno) );
1460             ret = AFPERR_MISC;
1461             goto openvol_err;
1462         }
1463         dir->d_did = DIRDID_ROOT;
1464         dir->d_color = DIRTREE_COLOR_BLACK; /* root node is black */
1465         volume->v_dir = volume->v_root = dir;
1466         volume->v_flags |= AFPVOL_OPEN;
1467 #ifdef CNID_DB
1468         volume->v_db = NULL;
1469         opened = 1;
1470 #endif
1471         if (volume->v_root_preexec) {
1472             if ((ret = afprun(1, volume->v_root_preexec, NULL)) && volume->v_root_preexec_close) {
1473                 LOG(log_error, logtype_afpd, "afp_openvol: root preexec : %d", ret );
1474                 ret = AFPERR_MISC;
1475                 goto openvol_err;
1476             }
1477         }
1478         if (volume->v_preexec) {
1479             if ((ret = afprun(0, volume->v_preexec, NULL)) && volume->v_preexec_close) {
1480                 LOG(log_error, logtype_afpd, "afp_openvol: preexec : %d", ret );
1481                 ret = AFPERR_MISC;
1482                 goto openvol_err;
1483             }
1484         }
1485     }
1486     else {
1487        /* FIXME */
1488     }    
1489 #ifdef FORCE_UIDGID
1490     set_uidgid ( volume );
1491 #endif /* FORCE_UIDGID */
1492
1493     if ( stat( volume->v_path, &st ) < 0 ) {
1494         ret = AFPERR_PARAM;
1495         goto openvol_err;
1496     }
1497
1498     if ( chdir( volume->v_path ) < 0 ) {
1499         ret = AFPERR_PARAM;
1500         goto openvol_err;
1501     }
1502     curdir = volume->v_dir;
1503
1504 #ifdef CNID_DB
1505     if (opened) {
1506         if (volume->v_dbpath)
1507             volume->v_db = cnid_open (volume->v_dbpath, volume->v_umask);
1508         if (volume->v_db == NULL)
1509             volume->v_db = cnid_open (volume->v_path, volume->v_umask);
1510         if (volume->v_db == NULL) {
1511            /* config option? 
1512             * - mount the volume readonly
1513             * - return an error
1514             * - read/write with other scheme
1515             */
1516         }
1517     }
1518 #endif /* CNID_DB */
1519
1520     buflen = *rbuflen - sizeof( bitmap );
1521     if (( ret = getvolparams( bitmap, volume, &st,
1522                               rbuf + sizeof(bitmap), &buflen )) != AFP_OK ) {
1523         goto openvol_err;
1524     }
1525 #ifdef AFP3x
1526     volume->v_utf8toucs2 = (iconv_t)(-1);
1527     volume->v_ucs2toutf8 = (iconv_t)(-1);
1528     volume->v_mactoutf8  = (iconv_t)(-1);
1529     volume->v_ucs2tomac  = (iconv_t)(-1);
1530     
1531     if (vol_utf8(volume)) {
1532         if ((iconv_t)(-1) == (volume->v_utf8toucs2 = iconv_open("UCS-2LE", "UTF-8"))) {
1533             LOG(log_error, logtype_afpd, "openvol: no UTF8 to UCS-2LE");
1534             goto openvol_err;
1535         }
1536         if ((iconv_t)(-1) == (volume->v_ucs2toutf8 = iconv_open("UTF-8", "UCS-2LE"))) {
1537             LOG(log_error, logtype_afpd, "openvol: no UCS-2LE to UTF-8");
1538             goto openvol_err_iconv;
1539         }
1540         if ((iconv_t)(-1) == (volume->v_mactoutf8 = iconv_open("UTF-8", "MAC"))) {
1541             LOG(log_error, logtype_afpd, "openvol: no MAC to UTF-8");
1542             goto openvol_err_iconv;
1543         }
1544         if ((iconv_t)(-1) == (volume->v_ucs2tomac = iconv_open("MAC", "UCS-2LE"))) {
1545             LOG(log_error, logtype_afpd, "openvol:  no UCS-2LE to MAC");
1546             goto openvol_err_iconv;
1547         }
1548     }
1549 #endif
1550     *rbuflen = buflen + sizeof( bitmap );
1551     bitmap = htons( bitmap );
1552     memcpy(rbuf, &bitmap, sizeof( bitmap ));
1553
1554 #ifndef CNID_DB
1555     /*
1556      * If you mount a volume twice, the second time the trash appears on
1557      * the desk-top.  That's because the Mac remembers the DID for the
1558      * trash (even for volumes in different zones, on different servers).
1559      * Just so this works better, we prime the DID cache with the trash,
1560      * fixing the trash at DID 17.
1561      */
1562     p = Trash;
1563     cname( volume, volume->v_dir, &p );
1564 #endif /* CNID_DB */
1565
1566     return( AFP_OK );
1567 #ifdef AFP3x
1568 openvol_err_iconv:
1569     if (volume->v_utf8toucs2 != (iconv_t)(-1))
1570         iconv_close(volume->v_utf8toucs2);
1571     if (volume->v_ucs2toutf8 != (iconv_t)(-1))
1572         iconv_close(volume->v_ucs2toutf8);
1573     if (volume->v_mactoutf8  != (iconv_t)(-1))
1574         iconv_close(volume->v_mactoutf8);    
1575     if (volume->v_ucs2tomac  != (iconv_t)(-1))
1576         iconv_close(volume->v_ucs2tomac);
1577 #endif        
1578 openvol_err:
1579 #ifdef CNID_DB
1580     if (opened && volume->v_db != NULL) {
1581         cnid_close(volume->v_db);
1582         volume->v_db = NULL;
1583     }
1584 #endif
1585     *rbuflen = 0;
1586     return ret;
1587 }
1588
1589 /* ------------------------- */
1590 static void closevol(struct vol *vol)
1591 {
1592     if (!vol)
1593         return;
1594
1595     dirfree( vol->v_root );
1596     vol->v_dir = NULL;
1597 #ifdef CNID_DB
1598     cnid_close(vol->v_db);
1599     vol->v_db = NULL;
1600 #endif /* CNID_DB */
1601     if (vol->v_postexec) {
1602         afprun(0, vol->v_postexec, NULL);
1603     }
1604     if (vol->v_root_postexec) {
1605         afprun(1, vol->v_root_postexec, NULL);
1606     }
1607 }
1608
1609 /* ------------------------- */
1610 void close_all_vol(void)
1611 {
1612     struct vol  *ovol;
1613     curdir = NULL;
1614     for ( ovol = volumes; ovol; ovol = ovol->v_next ) {
1615         if ( ovol->v_flags & AFPVOL_OPEN ) {
1616             ovol->v_flags &= ~AFPVOL_OPEN;
1617             closevol(ovol);
1618         }
1619     }
1620 }
1621
1622 /* ------------------------- */
1623 int afp_closevol(obj, ibuf, ibuflen, rbuf, rbuflen )
1624 AFPObj      *obj;
1625 char    *ibuf, *rbuf;
1626 int             ibuflen, *rbuflen;
1627 {
1628     struct vol  *vol, *ovol;
1629     u_int16_t   vid;
1630
1631     *rbuflen = 0;
1632     ibuf += 2;
1633     memcpy(&vid, ibuf, sizeof( vid ));
1634     if (NULL == ( vol = getvolbyvid( vid )) ) {
1635         return( AFPERR_PARAM );
1636     }
1637
1638     vol->v_flags &= ~AFPVOL_OPEN;
1639     for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
1640         if ( ovol->v_flags & AFPVOL_OPEN ) {
1641             break;
1642         }
1643     }
1644     if ( ovol != NULL ) {
1645         /* Even if chdir fails, we can't say afp_closevol fails. */
1646         if ( chdir( ovol->v_path ) == 0 ) {
1647             curdir = ovol->v_dir;
1648         }
1649     }
1650     dirfree( vol->v_root );
1651     vol->v_dir = NULL;
1652 #ifdef CNID_DB
1653     cnid_close(vol->v_db);
1654     vol->v_db = NULL;
1655 #endif /* CNID_DB */
1656
1657 #ifdef AFP3x
1658     if (vol->v_utf8toucs2 != (iconv_t)(-1))
1659         iconv_close(vol->v_utf8toucs2);
1660     if (vol->v_ucs2toutf8 != (iconv_t)(-1))
1661         iconv_close(vol->v_ucs2toutf8);
1662     if (vol->v_mactoutf8  != (iconv_t)(-1))
1663         iconv_close(vol->v_mactoutf8);    
1664     if (vol->v_ucs2tomac  != (iconv_t)(-1))
1665         iconv_close(vol->v_ucs2tomac);
1666 #endif        
1667
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 }