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