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