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