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