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