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