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