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