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