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