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