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