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