]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/volume.c
4de618c5dc67c6d35188f07fceef4f90e7cb04c5
[netatalk.git] / etc / afpd / volume.c
1 /*
2  * $Id: volume.c,v 1.11 2001-08-18 13:20:30 uhees 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: %s", strerror(errno) );
421         return -1;
422     }
423     if (( volume->v_name =
424             (char *)malloc( vlen + 1 )) == NULL ) {
425         syslog( LOG_ERR, "creatvol: malloc: %s", strerror(errno) );
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: %s", strerror(errno) );
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: %s", strerror(errno) );
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: %s", strerror(errno) );
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, isad = 1;
844     u_int32_t           aint;
845     u_short             ashort;
846     u_int32_t           bfree, btotal, bsize;
847     VolSpace            xbfree, xbtotal; /* extended bytes */
848     char                *data, *nameoff = NULL;
849     char                *slash;
850
851     /* courtesy of jallison@whistle.com:
852      * For MacOS8.x support we need to create the
853      * .Parent file here if it doesn't exist. */
854     
855     memset(&ad, 0, sizeof(ad));
856     if ( ad_open( vol->v_path, vol_noadouble(vol) |
857                   ADFLAGS_HF|ADFLAGS_DIR, O_RDWR | O_CREAT, 
858                   0666, &ad) < 0 ) {
859           isad = 0;
860
861     } else if (ad_getoflags( &ad, ADFLAGS_HF ) & O_CREAT) {
862           slash = strrchr( vol->v_path, '/' );
863           if(slash)
864               slash++;
865           else
866               slash = vol->v_path;
867
868           ad_setentrylen( &ad, ADEID_NAME, strlen( slash ));
869           memcpy(ad_entry( &ad, ADEID_NAME ), slash, 
870                  ad_getentrylen( &ad, ADEID_NAME ));
871           ad_flush(&ad, ADFLAGS_HF);
872     }
873
874     if (( bitmap & ( (1<<VOLPBIT_BFREE)|(1<<VOLPBIT_BTOTAL) |
875                      (1<<VOLPBIT_XBFREE)|(1<<VOLPBIT_XBTOTAL) |
876                      (1<<VOLPBIT_BSIZE)) ) != 0 ) {
877         if ( getvolspace( vol, &bfree, &btotal, &xbfree, &xbtotal,
878                           &bsize) < 0 ) {
879             if ( isad ) {
880                 ad_close( &ad, ADFLAGS_HF );
881             }
882             return( AFPERR_PARAM );
883         }
884     }
885
886     data = buf;
887     while ( bitmap != 0 ) {
888         while (( bitmap & 1 ) == 0 ) {
889             bitmap = bitmap>>1;
890             bit++;
891         }
892
893         switch ( bit ) {
894         case VOLPBIT_ATTR :
895 #ifdef CNID_DB
896             ashort = VOLPBIT_ATTR_FILEID;
897 #else /* CNID_DB */
898             ashort = 0;
899 #endif /* CNID_DB */
900             /* check for read-only.
901              * NOTE: we don't actually set the read-only flag unless
902              *       it's passed in that way as it's possible to mount
903              *       a read-write filesystem under a read-only one. */
904             if ((vol->v_flags & AFPVOL_RO) ||
905                 ((utime(vol->v_path, NULL) < 0) && (errno == EROFS)))
906               ashort |= VOLPBIT_ATTR_RO;
907             ashort = htons(ashort);
908             memcpy(data, &ashort, sizeof( ashort ));
909             data += sizeof( ashort );
910             break;
911
912         case VOLPBIT_SIG :
913             ashort = htons( AFPVOLSIG_DEFAULT );
914             memcpy(data, &ashort, sizeof( ashort ));
915             data += sizeof( ashort );
916             break;
917
918         case VOLPBIT_CDATE :
919             if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
920                 aint = AD_DATE_FROM_UNIX(st->st_mtime);
921             memcpy(data, &aint, sizeof( aint ));
922             data += sizeof( aint );
923             break;
924
925         case VOLPBIT_MDATE :
926             if ( st->st_mtime > vol->v_time ) {
927                 vol->v_time = st->st_mtime;
928                 aint = AD_DATE_FROM_UNIX(st->st_mtime);
929             } else {
930                 aint = AD_DATE_FROM_UNIX(vol->v_time);
931             }
932             memcpy(data, &aint, sizeof( aint ));
933             data += sizeof( aint );
934             break;
935
936         case VOLPBIT_BDATE :
937             if (!isad ||  (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
938                 aint = AD_DATE_START;
939             memcpy(data, &aint, sizeof( aint ));
940             data += sizeof( aint );
941             break;
942
943         case VOLPBIT_VID :
944             memcpy(data, &vol->v_vid, sizeof( vol->v_vid ));
945             data += sizeof( vol->v_vid );
946             break;
947
948         case VOLPBIT_BFREE :
949             bfree = htonl( bfree );
950             memcpy(data, &bfree, sizeof( bfree ));
951             data += sizeof( bfree );
952             break;
953
954         case VOLPBIT_BTOTAL :
955             btotal = htonl( btotal );
956             memcpy(data, &btotal, sizeof( btotal ));
957             data += sizeof( btotal );
958             break;
959
960 #ifndef NO_LARGE_VOL_SUPPORT
961         case VOLPBIT_XBFREE :
962             xbfree = hton64( xbfree );
963 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
964             bcopy(&xbfree, data, sizeof(xbfree));
965 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
966             memcpy(data, &xbfree, sizeof( xbfree ));
967 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
968             data += sizeof( xbfree );
969             break;
970
971         case VOLPBIT_XBTOTAL :
972             xbtotal = hton64( xbtotal );
973 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
974             bcopy(&xbtotal, data, sizeof(xbtotal));
975 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
976             memcpy(data, &xbtotal, sizeof( xbtotal ));
977 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
978             data += sizeof( xbfree );
979             break;
980 #endif /* ! NO_LARGE_VOL_SUPPORT */
981
982         case VOLPBIT_NAME :
983             nameoff = data;
984             data += sizeof( u_int16_t );
985             break;
986
987         case VOLPBIT_BSIZE:  /* block size */
988             bsize = htonl(bsize);
989             memcpy(data, &bsize, sizeof(bsize));
990             data += sizeof(bsize);
991             break;
992
993         default :
994             if ( isad ) {
995                 ad_close( &ad, ADFLAGS_HF );
996             }
997             return( AFPERR_BITMAP );
998         }
999         bitmap = bitmap>>1;
1000         bit++;
1001     }
1002     if ( nameoff ) {
1003         ashort = htons( data - buf );
1004         memcpy(nameoff, &ashort, sizeof( ashort ));
1005         aint = strlen( vol->v_name );
1006         *data++ = aint;
1007         memcpy(data, vol->v_name, aint );
1008         data += aint;
1009     }
1010     if ( isad ) {
1011         ad_close( &ad, ADFLAGS_HF );
1012     }
1013     *buflen = data - buf;
1014     return( AFP_OK );
1015 }
1016
1017
1018
1019 int afp_getsrvrparms(obj, ibuf, ibuflen, rbuf, rbuflen )
1020     AFPObj      *obj;
1021     char        *ibuf, *rbuf;
1022     int         ibuflen, *rbuflen;
1023 {
1024     struct timeval      tv;
1025     struct stat         st;
1026     struct vol          *volume;
1027     char        *data;
1028     int                 vcnt, len;
1029
1030   
1031     if (!volumes)
1032       load_volumes(obj);
1033
1034     data = rbuf + 5;
1035     for ( vcnt = 0, volume = volumes; volume; volume = volume->v_next ) {
1036         if ( stat( volume->v_path, &st ) < 0 ) {
1037             syslog( LOG_INFO, "afp_getsrvrparms: stat %s: %s",
1038                     volume->v_path, strerror(errno) );
1039             continue;           /* can't access directory */
1040         }
1041         if (!S_ISDIR(st.st_mode)) {
1042             continue;           /* not a dir */
1043         }
1044
1045         /* set password bit if there's a volume password */
1046         *data = (volume->v_password) ? AFPSRVR_PASSWD : 0;
1047
1048         /* Apple 2 clients running ProDOS-8 expect one volume to have
1049            bit 0 of this byte set.  They will not recognize anything
1050            on the server unless this is the case.  I have not
1051            completely worked this out, but it's related to booting
1052            from the server.  Support for that function is a ways
1053            off.. <shirsch@ibm.net> */
1054         *data++ |= (volume->v_flags & AFPVOL_A2VOL) ? AFPSRVR_CONFIGINFO : 0;
1055         len = strlen( volume->v_name );
1056         *data++ = len;
1057         memcpy(data, volume->v_name, len );
1058         data += len;
1059         vcnt++;
1060     }
1061
1062     *rbuflen = data - rbuf;
1063     data = rbuf;
1064     if ( gettimeofday( &tv, 0 ) < 0 ) {
1065         syslog( LOG_ERR, "afp_getsrvrparms: gettimeofday: %s", strerror(errno) );
1066         *rbuflen = 0;
1067         return AFPERR_PARAM;
1068     }
1069     tv.tv_sec = AD_DATE_FROM_UNIX(tv.tv_sec);
1070     memcpy(data, &tv.tv_sec, sizeof( u_int32_t));
1071     data += sizeof( u_int32_t);
1072     *data = vcnt;
1073     return( AFP_OK );
1074 }
1075
1076 int afp_openvol(obj, ibuf, ibuflen, rbuf, rbuflen )
1077     AFPObj      *obj;
1078     char        *ibuf, *rbuf;
1079     int         ibuflen, *rbuflen;
1080 {
1081     struct stat st;
1082     char        *volname;
1083 #ifndef CNID_DB
1084     char *p;
1085 #endif /* CNID_DB */
1086     struct vol  *volume;
1087     struct dir  *dir;
1088     int         len, ret, buflen;
1089     u_int16_t   bitmap;
1090
1091     ibuf += 2;
1092     memcpy(&bitmap, ibuf, sizeof( bitmap ));
1093     bitmap = ntohs( bitmap );
1094     ibuf += sizeof( bitmap );
1095     if (( bitmap & (1<<VOLPBIT_VID)) == 0 ) {
1096         ret = AFPERR_BITMAP;
1097         goto openvol_err;
1098     }
1099
1100     len = (unsigned char)*ibuf++;
1101     volname = obj->oldtmp;
1102     memcpy(volname, ibuf, len );
1103     *(volname +  len) = '\0';
1104     ibuf += len;
1105     if ((len + 1) & 1) /* pad to an even boundary */
1106       ibuf++;
1107
1108     if (!volumes)
1109       load_volumes(obj);
1110
1111     for ( volume = volumes; volume; volume = volume->v_next ) {
1112         if ( strcasecmp( volname, volume->v_name ) == 0 ) {
1113             break;
1114         }
1115     }
1116
1117     if ( volume == NULL ) {
1118         ret = AFPERR_PARAM;
1119         goto openvol_err;
1120     }
1121
1122     /* check for a volume password */
1123     if (volume->v_password && 
1124         strncmp(ibuf, volume->v_password, VOLPASSLEN)) {
1125         ret = AFPERR_ACCESS;
1126         goto openvol_err;
1127     }
1128
1129     if (( volume->v_flags & AFPVOL_OPEN  ) == 0 ) {
1130         if ((dir = dirnew(strlen(volume->v_name) + 1)) == NULL) {
1131             syslog( LOG_ERR, "afp_openvol: malloc: %s", strerror(errno) );
1132             ret = AFPERR_MISC;
1133             goto openvol_err;
1134         }
1135         dir->d_did = DIRDID_ROOT;
1136         dir->d_color = DIRTREE_COLOR_BLACK; /* root node is black */
1137         strcpy( dir->d_name, volume->v_name );
1138         volume->v_dir = volume->v_root = dir;
1139         volume->v_flags |= AFPVOL_OPEN;
1140     }
1141
1142     if ( stat( volume->v_path, &st ) < 0 ) {
1143         ret = AFPERR_PARAM;
1144         goto openvol_err;
1145     }
1146
1147     buflen = *rbuflen - sizeof( bitmap );
1148     if (( ret = getvolparams( bitmap, volume, &st,
1149             rbuf + sizeof(bitmap), &buflen )) != AFP_OK ) {
1150         goto openvol_err;
1151     }
1152     *rbuflen = buflen + sizeof( bitmap );
1153     bitmap = htons( bitmap );
1154     memcpy(rbuf, &bitmap, sizeof( bitmap ));
1155
1156     curdir = volume->v_dir;
1157     if ( chdir( volume->v_path ) < 0 ) {
1158         ret = AFPERR_PARAM;
1159         goto openvol_err;
1160     }
1161
1162 #ifdef CNID_DB
1163         if (volume->v_dbpath)
1164                 volume->v_db = cnid_open (volume->v_dbpath);
1165         if (volume->v_db == 0)
1166                 volume->v_db = cnid_open (volume->v_path);
1167 #endif /* CNID_DB */
1168
1169 #ifndef CNID_DB
1170     /*
1171      * If you mount a volume twice, the second time the trash appears on
1172      * the desk-top.  That's because the Mac remembers the DID for the
1173      * trash (even for volumes in different zones, on different servers).
1174      * Just so this works better, we prime the DID cache with the trash,
1175      * fixing the trash at DID 3.
1176      */
1177     p = Trash;
1178     cname( volume, volume->v_dir, &p );
1179 #endif /* CNID_DB */
1180
1181     return( AFP_OK );
1182
1183 openvol_err:
1184     *rbuflen = 0;
1185     return ret;
1186 }
1187
1188 int afp_closevol(obj, ibuf, ibuflen, rbuf, rbuflen )
1189     AFPObj      *obj;
1190     char        *ibuf, *rbuf;
1191     int         ibuflen, *rbuflen;
1192 {
1193     struct vol  *vol, *ovol;
1194     u_int16_t   vid;
1195
1196     *rbuflen = 0;
1197     ibuf += 2;
1198     memcpy(&vid, ibuf, sizeof( vid ));
1199     if (( vol = getvolbyvid( vid )) == NULL ) {
1200         return( AFPERR_PARAM );
1201     }
1202
1203     vol->v_flags &= ~AFPVOL_OPEN;
1204     for ( ovol = volumes; ovol; ovol = ovol->v_next ) {
1205         if ( ovol->v_flags & AFPVOL_OPEN ) {
1206             break;
1207         }
1208     }
1209     if ( ovol != NULL ) {
1210         curdir = ovol->v_dir;
1211         if ( chdir( ovol->v_path ) < 0 ) {
1212             return( AFPERR_PARAM );
1213         }
1214     }
1215
1216     dirfree( vol->v_root );
1217     vol->v_dir = NULL;
1218 #ifdef CNID_DB
1219     cnid_close(vol->v_db);
1220     vol->v_db = NULL;
1221 #endif /* CNID_DB */
1222     return( AFP_OK );
1223 }
1224
1225 struct vol *getvolbyvid(const u_int16_t vid )
1226 {
1227     struct vol  *vol;
1228
1229     for ( vol = volumes; vol; vol = vol->v_next ) {
1230         if ( vid == vol->v_vid ) {
1231             break;
1232         }
1233     }
1234     if ( vol == NULL || ( vol->v_flags & AFPVOL_OPEN ) == 0 ) {
1235         return( NULL );
1236     }
1237
1238     return( vol );
1239 }
1240
1241 struct extmap *getextmap(const char *path)
1242 {
1243     char        *p;
1244     struct extmap       *em;
1245
1246     if (( p = strrchr( path, '.' )) == NULL ) {
1247         return( defextmap );
1248     }
1249
1250     for ( em = extmap; em; em = em->em_next ) {
1251         if ( strdiacasecmp( em->em_ext, p ) == 0 ) {
1252             break;
1253         }
1254     }
1255     if ( em == NULL ) {
1256         return( defextmap );
1257     } else {
1258         return( em );
1259     }
1260 }
1261
1262 void setvoltime(obj, vol )
1263     AFPObj *obj;
1264     struct vol  *vol;
1265 {
1266     struct timeval      tv;
1267
1268     /* just looking at vol->v_time is broken seriously since updates
1269      * from other users afpd processes never are seen.
1270      * This is not the most elegant solution (a shared memory between
1271      * the afpd processes would come closer)
1272      * [RS] */
1273
1274     if ( gettimeofday( &tv, 0 ) < 0 ) {
1275         syslog( LOG_ERR, "setvoltime: gettimeofday: %s", strerror(errno) );
1276         return;
1277     }
1278     if( utime( vol->v_path, NULL ) < 0 ) {
1279         /* write of time failed ... probably a read only filesys,
1280          * where no other users can interfere, so there's no issue here
1281          */
1282     }
1283
1284     /* a little granularity */
1285     if (vol->v_time < tv.tv_sec) {
1286       vol->v_time = tv.tv_sec;
1287       obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED);
1288     }
1289 }
1290
1291 int afp_getvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1292     AFPObj      *obj;
1293     char        *ibuf, *rbuf;
1294     int         ibuflen, *rbuflen;
1295 {
1296     struct stat st;
1297     struct vol  *vol;
1298     int         buflen, ret;
1299     u_int16_t   vid, bitmap;
1300
1301     ibuf += 2;
1302     memcpy(&vid, ibuf, sizeof( vid ));
1303     ibuf += sizeof( vid );
1304     memcpy(&bitmap, ibuf, sizeof( bitmap ));
1305     bitmap = ntohs( bitmap );
1306
1307     if (( vol = getvolbyvid( vid )) == NULL ) {
1308         *rbuflen = 0;
1309         return( AFPERR_PARAM );
1310     }
1311
1312     if ( stat( vol->v_path, &st ) < 0 ) {
1313         *rbuflen = 0;
1314         return( AFPERR_PARAM );
1315     }
1316
1317     buflen = *rbuflen - sizeof( bitmap );
1318     if (( ret = getvolparams( bitmap, vol, &st,
1319             rbuf + sizeof( bitmap ), &buflen )) != AFP_OK ) {
1320         *rbuflen = 0;
1321         return( ret );
1322     }
1323     *rbuflen = buflen + sizeof( bitmap );
1324     bitmap = htons( bitmap );
1325     memcpy(rbuf, &bitmap, sizeof( bitmap ));
1326     return( AFP_OK );
1327 }
1328
1329 int afp_setvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1330     AFPObj      *obj;
1331     char        *ibuf, *rbuf;
1332     int         ibuflen, *rbuflen;
1333 {
1334     struct adouble ad;
1335     struct vol  *vol;
1336     u_int16_t   vid, bitmap;
1337     u_int32_t   aint;
1338
1339     ibuf += 2;
1340     *rbuflen = 0;
1341
1342     memcpy(&vid, ibuf, sizeof( vid ));
1343     ibuf += sizeof( vid );
1344     memcpy(&bitmap, ibuf, sizeof( bitmap ));
1345     bitmap = ntohs( bitmap );
1346     ibuf += sizeof(bitmap);
1347
1348     if (( vol = getvolbyvid( vid )) == NULL ) {
1349         return( AFPERR_PARAM );
1350     }
1351
1352     if (vol->v_flags & AFPVOL_RO)
1353         return AFPERR_VLOCK;
1354
1355     /* we can only set the backup date. */
1356     if (bitmap != VOLPBIT_BDATE)
1357       return AFPERR_BITMAP;
1358
1359     memset(&ad, 0, sizeof(ad));
1360     if ( ad_open( vol->v_path, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR, 
1361                   0666, &ad) < 0 ) {
1362       if (errno == EROFS)
1363         return AFPERR_VLOCK;
1364
1365       return AFPERR_ACCESS;
1366     }
1367
1368     memcpy(&aint, ibuf, sizeof(aint));
1369     ad_setdate(&ad, AD_DATE_BACKUP, aint);
1370     ad_flush(&ad, ADFLAGS_HF);
1371     ad_close(&ad, ADFLAGS_HF);
1372     return( AFP_OK );
1373 }