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