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