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