]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/volume.c
MacOS X Compatibility Fix: volume option mswindows => usedots.
[netatalk.git] / etc / afpd / volume.c
1 /*
2  * $Id: volume.c,v 1.57 2003-09-03 21:25:38 samnoble 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, AFPObj *obj,
297                    struct passwd *pwd)
298 {
299     char *val;
300
301     LOG(log_debug, logtype_afpd, "Parsing volset %s", tmp);
302
303     val = strchr(tmp, ':');
304     if (!val) {
305         /* we'll assume it's a volume name. */
306         strncpy(volname, tmp, vlen);
307
308     } else if (optionok(tmp, "allow:", val)) {
309         if (options[VOLOPT_ALLOW].c_value)
310             free(options[VOLOPT_ALLOW].c_value);
311         options[VOLOPT_ALLOW].c_value = strdup(val + 1);
312
313     } else if (optionok(tmp, "deny:", val)) {
314         if (options[VOLOPT_DENY].c_value)
315             free(options[VOLOPT_DENY].c_value);
316         options[VOLOPT_DENY].c_value = strdup(val + 1);
317
318     } else if (optionok(tmp, "rwlist:", val)) {
319         if (options[VOLOPT_RWLIST].c_value)
320             free(options[VOLOPT_RWLIST].c_value);
321         options[VOLOPT_RWLIST].c_value = strdup(val + 1);
322
323     } else if (optionok(tmp, "rolist:", val)) {
324         if (options[VOLOPT_ROLIST].c_value)
325             free(options[VOLOPT_ROLIST].c_value);
326         options[VOLOPT_ROLIST].c_value = strdup(val + 1);
327
328     } else if (optionok(tmp, "codepage:", val)) {
329         if (options[VOLOPT_CODEPAGE].c_value)
330             free(options[VOLOPT_CODEPAGE].c_value);
331         options[VOLOPT_CODEPAGE].c_value = get_codepage_path(nlspath, val + 1);
332
333     } else if (optionok(tmp, "veto:", val)) {
334         if (options[VOLOPT_VETO].c_value)
335             free(options[VOLOPT_VETO].c_value);
336         options[VOLOPT_VETO].c_value = strdup(val + 1);
337
338     } else if (optionok(tmp, "casefold:", val)) {
339         if (strcasecmp(val + 1, "tolower") == 0)
340             options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMLOWER;
341         else if (strcasecmp(val + 1, "toupper") == 0)
342             options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMUPPER;
343         else if (strcasecmp(val + 1, "xlatelower") == 0)
344             options[VOLOPT_CASEFOLD].i_value = AFPVOL_UUPPERMLOWER;
345         else if (strcasecmp(val + 1, "xlateupper") == 0)
346             options[VOLOPT_CASEFOLD].i_value = AFPVOL_ULOWERMUPPER;
347
348     } else if (optionok(tmp, "options:", val)) {
349         char *p;
350
351         if ((p = strtok(val + 1, ",")) == NULL) /* nothing */
352             return;
353
354         while (p) {
355             if (strcasecmp(p, "prodos") == 0)
356                 options[VOLOPT_FLAGS].i_value |= AFPVOL_A2VOL;
357             else if (strcasecmp(p, "mswindows") == 0) {
358                 options[VOLOPT_FLAGS].i_value |= (AFPVOL_MSWINDOWS | AFPVOL_USEDOTS);
359                 if (!options[VOLOPT_CODEPAGE].c_value)
360                     options[VOLOPT_CODEPAGE].c_value =
361                         get_codepage_path(nlspath, MSWINDOWS_CODEPAGE);
362
363             } else if (strcasecmp(p, "crlf") == 0)
364                 options[VOLOPT_FLAGS].i_value |= AFPVOL_CRLF;
365             else if (strcasecmp(p, "noadouble") == 0)
366                 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOADOUBLE;
367             else if (strcasecmp(p, "ro") == 0)
368                 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
369             else if (strcasecmp(p, "nohex") == 0)
370                 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOHEX;
371             else if (strcasecmp(p, "usedots") == 0)
372                 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS;
373             else if (strcasecmp(p, "limitsize") == 0)
374                 options[VOLOPT_FLAGS].i_value |= AFPVOL_LIMITSIZE;
375             /* support for either "dropbox" or "dropkludge" */
376             else if (strcasecmp(p, "dropbox") == 0)
377                 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
378             else if (strcasecmp(p, "dropkludge") == 0)
379                 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
380             else if (strcasecmp(p, "nofileid") == 0)
381                 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOFILEID;
382             else if (strcasecmp(p, "utf8") == 0)
383                 options[VOLOPT_FLAGS].i_value |= AFPVOL_UTF8;
384             else if (strcasecmp(p, "upriv") == 0)
385                 options[VOLOPT_FLAGS].i_value |= AFPVOL_UNIX_PRIV;
386
387             p = strtok(NULL, ",");
388         }
389
390 #ifdef CNID_DB
391     } else if (optionok(tmp, "dbpath:", val)) {
392         char t[MAXPATHLEN + 1];
393         if (options[VOLOPT_DBPATH].c_value)
394             free(options[VOLOPT_DBPATH].c_value);
395
396         volxlate(obj, t, MAXPATHLEN, val, pwd, NULL);
397         options[VOLOPT_DBPATH].c_value = strdup(t + 1);
398 #endif /* CNID_DB */
399     } else if (optionok(tmp, "umask:", val)) {
400         options[VOLOPT_UMASK].i_value = (int)strtol(val, (char **)NULL, 8);
401     } else if (optionok(tmp, "mapchars:",val)) {
402         if (options[VOLOPT_MAPCHARS].c_value)
403             free(options[VOLOPT_MAPCHARS].c_value);
404         options[VOLOPT_MAPCHARS].c_value = strdup(val + 1);
405
406     } else if (optionok(tmp, "password:", val)) {
407         if (options[VOLOPT_PASSWORD].c_value)
408             free(options[VOLOPT_PASSWORD].c_value);
409         options[VOLOPT_PASSWORD].c_value = strdup(val + 1);
410
411 #ifdef FORCE_UIDGID
412
413         /* this code allows forced uid/gid per volume settings */
414     } else if (optionok(tmp, "forceuid:", val)) {
415         if (options[VOLOPT_FORCEUID].c_value)
416             free(options[VOLOPT_FORCEUID].c_value);
417         options[VOLOPT_FORCEUID].c_value = strdup(val + 1);
418     } else if (optionok(tmp, "forcegid:", val)) {
419         if (options[VOLOPT_FORCEGID].c_value)
420             free(options[VOLOPT_FORCEGID].c_value);
421         options[VOLOPT_FORCEGID].c_value = strdup(val + 1);
422
423 #endif /* FORCE_UIDGID */
424
425     } else {
426         /* ignore unknown options */
427         LOG(log_debug, logtype_afpd, "ignoring unknown volume option: %s", tmp);
428
429     }
430 }
431
432 static int creatvol(const char *path, char *name, struct vol_option *options)
433 {
434     struct vol  *volume;
435     int         vlen;
436
437     if ( name == NULL || *name == '\0' ) {
438         if ((name = strrchr( path, '/' )) == NULL) {
439             return -1;  /* Obviously not a fully qualified path */
440         }
441
442         /* if you wish to share /, you need to specify a name. */
443         if (*++name == '\0')
444             return -1;
445     }
446
447     for ( volume = volumes; volume; volume = volume->v_next ) {
448         if ( strcasecmp( volume->v_name, name ) == 0 ) {
449             return -1;  /* Won't be able to access it, anyway... */
450         }
451     }
452
453     vlen = strlen( name );
454     if ( vlen > AFPVOL_NAMELEN ) {
455         vlen = AFPVOL_NAMELEN;
456         name[AFPVOL_NAMELEN] = '\0';
457     }
458
459     if (( volume =
460                 (struct vol *)calloc(1, sizeof( struct vol ))) == NULL ) {
461         LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
462         return -1;
463     }
464     if (( volume->v_name =
465                 (char *)malloc( vlen + 1 )) == NULL ) {
466         LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
467         free(volume);
468         return -1;
469     }
470     if (( volume->v_path =
471                 (char *)malloc( strlen( path ) + 1 )) == NULL ) {
472         LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
473         free(volume->v_name);
474         free(volume);
475         return -1;
476     }
477
478     strcpy( volume->v_name, name);
479     strcpy( volume->v_path, path );
480
481 #ifdef __svr4__
482     volume->v_qfd = -1;
483 #endif /* __svr4__ */
484     volume->v_vid = lastvid++;
485     volume->v_lastdid = 17;
486
487     /* handle options */
488     if (options) {
489         /* should we casefold? */
490         volume->v_casefold = options[VOLOPT_CASEFOLD].i_value;
491
492         /* shift in some flags */
493         volume->v_flags = options[VOLOPT_FLAGS].i_value;
494
495         /* read in the code pages */
496         if (options[VOLOPT_CODEPAGE].c_value)
497             codepage_read(volume, options[VOLOPT_CODEPAGE].c_value);
498
499         if (options[VOLOPT_PASSWORD].c_value)
500             volume->v_password = strdup(options[VOLOPT_PASSWORD].c_value);
501
502         if (options[VOLOPT_VETO].c_value)
503             volume->v_veto = strdup(options[VOLOPT_VETO].c_value);
504
505 #ifdef CNID_DB
506         if (options[VOLOPT_DBPATH].c_value)
507             volume->v_dbpath = strdup(options[VOLOPT_DBPATH].c_value);
508 #endif /* CNID_DB */
509
510         if (options[VOLOPT_UMASK].i_value)
511             volume->v_umask = (mode_t)options[VOLOPT_UMASK].i_value;
512
513 #ifdef FORCE_UIDGID
514
515         if (options[VOLOPT_FORCEUID].c_value) {
516             volume->v_forceuid = strdup(options[VOLOPT_FORCEUID].c_value);
517         } else {
518             volume->v_forceuid = NULL; /* set as null so as to return 0 later on */
519         }
520
521         if (options[VOLOPT_FORCEGID].c_value) {
522             volume->v_forcegid = strdup(options[VOLOPT_FORCEGID].c_value);
523         } else {
524             volume->v_forcegid = NULL; /* set as null so as to return 0 later on */
525         }
526
527 #endif /* FORCE_UIDGID */
528
529     }
530
531     volume->v_next = volumes;
532     volumes = volume;
533     return 0;
534 }
535
536 static char *myfgets( buf, size, fp )
537 char    *buf;
538 int             size;
539 FILE    *fp;
540 {
541     char        *p;
542     int         c;
543
544     p = buf;
545     while ((EOF != ( c = getc( fp )) ) && ( size > 0 )) {
546         if ( c == '\n' || c == '\r' ) {
547             *p++ = '\n';
548             break;
549         } else {
550             *p++ = c;
551         }
552         size--;
553     }
554
555     if ( p == buf ) {
556         return( NULL );
557     } else {
558         *p = '\0';
559         return( buf );
560     }
561 }
562
563
564 /* check access list. this function wants something of the following
565  * form:
566  *        @group,name,name2,@group2,name3
567  *
568  * a NULL argument allows everybody to have access.
569  * we return three things:
570  *     -1: no list
571  *      0: list exists, but name isn't in it
572  *      1: in list
573  */
574 static int accessvol(args, name)
575 const char *args;
576 const char *name;
577 {
578     char buf[MAXPATHLEN + 1], *p;
579     struct group *gr;
580
581     if (!args)
582         return -1;
583
584     strncpy(buf, args, sizeof(buf));
585     if ((p = strtok(buf, ",")) == NULL) /* nothing, return okay */
586         return -1;
587
588     while (p) {
589         if (*p == '@') { /* it's a group */
590             if ((gr = getgrnam(p + 1)) && gmem(gr->gr_gid))
591                 return 1;
592         } else if (strcmp(p, name) == 0) /* it's a user name */
593             return 1;
594         p = strtok(NULL, ",");
595     }
596
597     return 0;
598 }
599
600 static void setextmap( ext, type, creator, user)
601 char            *ext, *type, *creator;
602 int                     user;
603 {
604     struct extmap       *em;
605     int                 cnt;
606
607     if (extmap == NULL) {
608         if (( extmap = calloc(1, sizeof( struct extmap ))) == NULL ) {
609             LOG(log_error, logtype_afpd, "setextmap: calloc: %s", strerror(errno) );
610             return;
611         }
612     }
613     ext++;
614     for ( em = extmap, cnt = 0; em->em_ext; em++, cnt++) {
615         if ( (strdiacasecmp( em->em_ext, ext )) == 0 ) {
616             break;
617         }
618     }
619
620     if ( em->em_ext == NULL ) {
621         if (!(extmap  = realloc( extmap, sizeof( struct extmap ) * (cnt +2))) ) {
622             LOG(log_error, logtype_afpd, "setextmap: realloc: %s", strerror(errno) );
623             return;
624         }
625         (extmap +cnt +1)->em_ext = NULL;
626         em = extmap +cnt;
627     } else if ( !user ) {
628         return;
629     }
630     if (em->em_ext)
631         free(em->em_ext);
632
633     if (!(em->em_ext = strdup(  ext))) {
634         LOG(log_error, logtype_afpd, "setextmap: strdup: %s", strerror(errno) );
635         return;
636     }
637
638     if ( *type == '\0' ) {
639         memcpy(em->em_type, "????", sizeof( em->em_type ));
640     } else {
641         memcpy(em->em_type, type, sizeof( em->em_type ));
642     }
643     if ( *creator == '\0' ) {
644         memcpy(em->em_creator, "UNIX", sizeof( em->em_creator ));
645     } else {
646         memcpy(em->em_creator, creator, sizeof( em->em_creator ));
647     }
648 }
649
650 /* -------------------------- */
651 static int extmap_cmp(const void *map1, const void *map2)
652 {
653     const struct extmap *em1 = map1;
654     const struct extmap *em2 = map2;
655     return strdiacasecmp(em1->em_ext, em2->em_ext);
656 }
657
658 static void sortextmap( void)
659 {
660     struct extmap       *em;
661
662     extmap_cnt = 0;
663     if ((em = extmap) == NULL) {
664         return;
665     }
666     while (em->em_ext) {
667         em++;
668         extmap_cnt++;
669     }
670     if (extmap_cnt) {
671         qsort(extmap, extmap_cnt, sizeof(struct extmap), extmap_cmp);
672         defextmap = extmap;
673     }
674 }
675
676
677 /*
678  * Read a volume configuration file and add the volumes contained within to
679  * the global volume list.  If p2 is non-NULL, the file that is opened is
680  * p1/p2
681  *
682  * Lines that begin with # and blank lines are ignored.
683  * Volume lines are of the form:
684  *              <unix path> [<volume name>] [allow:<user>,<@group>,...] \
685  *                           [codepage:<file>] [casefold:<num>]
686  *              <extension> TYPE [CREATOR]
687  */
688 static int readvolfile(obj, p1, p2, user, pwent)
689 AFPObj      *obj;
690 char    *p1, *p2;
691 int             user;
692 struct passwd *pwent;
693 {
694     FILE                *fp;
695     char                path[ MAXPATHLEN + 1], tmp[ MAXPATHLEN + 1],
696     volname[ AFPVOL_NAMELEN + 1 ], buf[ BUFSIZ ],
697     type[ 5 ], creator[ 5 ];
698     char                *u, *p;
699     struct passwd       *pw;
700     struct vol_option   options[VOLOPT_NUM], save_options[VOLOPT_NUM];
701     int                 i;
702
703     if (!p1)
704         return -1;
705
706     strcpy( path, p1 );
707     if ( p2 != NULL ) {
708         strcat( path, "/" );
709         strcat( path, p2 );
710     }
711
712     if (NULL == ( fp = fopen( path, "r" )) ) {
713         return( -1 );
714     }
715
716     memset(save_options, 0, sizeof(save_options));
717     while ( myfgets( buf, sizeof( buf ), fp ) != NULL ) {
718         initline( strlen( buf ), buf );
719         parseline( sizeof( path ) - 1, path );
720         switch ( *path ) {
721         case '\0' :
722         case '#' :
723             continue;
724
725         case ':':
726             /* change the default options for this file */
727             if (strncmp(path, VOLOPT_DEFAULT, VOLOPT_DEFAULT_LEN) == 0) {
728                 *tmp = '\0';
729                 for (i = 0; i < VOLOPT_NUM; i++) {
730                     if (parseline( sizeof( path ) - VOLOPT_DEFAULT_LEN - 1,
731                                    path + VOLOPT_DEFAULT_LEN) < 0)
732                         break;
733                     volset(save_options, tmp, sizeof(tmp) - 1,
734                            obj->options.nlspath, path + VOLOPT_DEFAULT_LEN,
735                            obj, pwent);
736                 }
737             }
738             break;
739
740         case '~' :
741             if (( p = strchr( path, '/' )) != NULL ) {
742                 *p++ = '\0';
743             }
744             u = path;
745             u++;
746             if ( *u == '\0' ) {
747                 u = obj->username;
748             }
749             if ( u == NULL || *u == '\0' || ( pw = getpwnam( u )) == NULL ) {
750                 continue;
751             }
752             strcpy( tmp, pw->pw_dir );
753             if ( p != NULL && *p != '\0' ) {
754                 strcat( tmp, "/" );
755                 strcat( tmp, p );
756             }
757             /* Tag a user's home directory with their umask.  Note, this will
758              * be overwritten if the user actually specifies a umask: option
759              * for a '~' volume. */
760             save_options[VOLOPT_UMASK].i_value = obj->options.save_mask;
761             /* fall through */
762
763         case '/' :
764             /* send path through variable substitution */
765             if (*path != '~') /* need to copy path to tmp */
766                 strcpy(tmp, path);
767             if (!pwent)
768                 pwent = getpwnam(obj->username);
769             volxlate(obj, path, sizeof(path) - 1, tmp, pwent, NULL);
770
771             /* this is sort of braindead. basically, i want to be
772              * able to specify things in any order, but i don't want to 
773              * re-write everything. 
774              *
775              * currently we have 11 options: 
776              *   volname
777              *   codepage:x
778              *   casefold:x
779              *   allow:x,y,@z
780              *   deny:x,y,@z
781              *   rwlist:x,y,@z
782              *   rolist:x,y,@z
783              *   options:prodos,crlf,noadouble,ro
784              *   dbpath:x
785              *   password:x
786              *   namemask:x,y,!z  (not implemented yet)
787              */
788             memcpy(options, save_options, sizeof(options));
789             *volname = '\0';
790
791             /* read in up to 11 possible options */
792             for (i = 0; i < VOLOPT_NUM; i++) {
793                 if (parseline( sizeof( tmp ) - 1, tmp ) < 0)
794                     break;
795
796                 volset(options, volname, sizeof(volname) - 1,
797                        obj->options.nlspath, tmp, obj, pwent);
798             }
799
800             /* check allow/deny lists:
801                allow -> either no list (-1), or in list (1)
802                deny -> either no list (-1), or not in list (0) */
803             if (accessvol(options[VOLOPT_ALLOW].c_value, obj->username) &&
804                     (accessvol(options[VOLOPT_DENY].c_value, obj->username) < 1)) {
805
806                 /* handle read-only behaviour. semantics:
807                  * 1) neither the rolist nor the rwlist exist -> rw
808                  * 2) rolist exists -> ro if user is in it.
809                  * 3) rwlist exists -> ro unless user is in it. */
810                 if (((options[VOLOPT_FLAGS].i_value & AFPVOL_RO) == 0) &&
811                         ((accessvol(options[VOLOPT_ROLIST].c_value,
812                                     obj->username) == 1) ||
813                          !accessvol(options[VOLOPT_RWLIST].c_value,
814                                     obj->username)))
815                     options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
816
817                 /* do variable substitution */
818                 volxlate(obj, tmp, sizeof(tmp) - 1, volname, pwent, path);
819                 creatvol(path, tmp, options);
820             }
821             volfree(options, save_options);
822             break;
823
824         case '.' :
825             parseline( sizeof( type ) - 1, type );
826             parseline( sizeof( creator ) - 1, creator );
827             setextmap( path, type, creator, user);
828             break;
829
830         default :
831             break;
832         }
833     }
834     volfree(save_options, NULL);
835     sortextmap();
836     if ( fclose( fp ) != 0 ) {
837         LOG(log_error, logtype_afpd, "readvolfile: fclose: %s", strerror(errno) );
838     }
839     return( 0 );
840 }
841
842
843 static void load_volumes(AFPObj *obj)
844 {
845     struct passwd       *pwent = getpwnam(obj->username);
846
847     if ( (obj->options.flags & OPTION_USERVOLFIRST) == 0 ) {
848         readvolfile(obj, obj->options.systemvol, NULL, 0, pwent);
849     }
850
851     if ((*obj->username == '\0') || (obj->options.flags & OPTION_NOUSERVOL)) {
852         readvolfile(obj, obj->options.defaultvol, NULL, 1, pwent);
853     } else if (pwent) {
854         /*
855         * Read user's AppleVolumes or .AppleVolumes file
856         * If neither are readable, read the default volumes file. if
857         * that doesn't work, create a user share.
858         */
859         if ( readvolfile(obj, pwent->pw_dir, "AppleVolumes", 1, pwent) < 0 &&
860                 readvolfile(obj, pwent->pw_dir, ".AppleVolumes", 1, pwent) < 0 &&
861                 readvolfile(obj, pwent->pw_dir, "applevolumes", 1, pwent) < 0 &&
862                 readvolfile(obj, pwent->pw_dir, ".applevolumes", 1, pwent) < 0 &&
863                 obj->options.defaultvol != NULL ) {
864             if (readvolfile(obj, obj->options.defaultvol, NULL, 1, pwent) < 0)
865                 creatvol(pwent->pw_dir, NULL, NULL);
866         }
867     }
868     if ( obj->options.flags & OPTION_USERVOLFIRST ) {
869         readvolfile(obj, obj->options.systemvol, NULL, 0, pwent );
870     }
871 }
872
873 static int getvolspace( vol, bfree, btotal, xbfree, xbtotal, bsize )
874 struct vol      *vol;
875 u_int32_t       *bfree, *btotal, *bsize;
876 VolSpace    *xbfree, *xbtotal;
877 {
878     int         spaceflag, rc;
879     u_int32_t   maxsize;
880 #ifndef NO_QUOTA_SUPPORT
881     VolSpace    qfree, qtotal;
882 #endif /* ! NO_QUOTA_SUPPORT */
883
884     spaceflag = AFPVOL_GVSMASK & vol->v_flags;
885     /* report up to 2GB if afp version is < 2.2 (4GB if not) */
886     maxsize = (vol->v_flags & AFPVOL_A2VOL) ? 0x01fffe00 :
887               (((afp_version < 22) || (vol->v_flags & AFPVOL_LIMITSIZE))
888                ? 0x7fffffffL : 0xffffffffL);
889
890 #ifdef AFS
891     if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_AFSGVS ) {
892         if ( afs_getvolspace( vol, xbfree, xbtotal, bsize ) == AFP_OK ) {
893             vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_AFSGVS;
894             goto getvolspace_done;
895         }
896     }
897 #endif /* AFS */
898
899     if (( rc = ustatfs_getvolspace( vol, xbfree, xbtotal,
900                                     bsize)) != AFP_OK ) {
901         return( rc );
902     }
903
904 #define min(a,b)        ((a)<(b)?(a):(b))
905 #ifndef NO_QUOTA_SUPPORT
906     if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_UQUOTA ) {
907         if ( uquota_getvolspace( vol, &qfree, &qtotal, *bsize ) == AFP_OK ) {
908             vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_UQUOTA;
909             *xbfree = min(*xbfree, qfree);
910             *xbtotal = min( *xbtotal, qtotal);
911             goto getvolspace_done;
912         }
913     }
914 #endif /* ! NO_QUOTA_SUPPORT */
915     vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_USTATFS;
916
917 getvolspace_done:
918     *bfree = min( *xbfree, maxsize);
919     *btotal = min( *xbtotal, maxsize);
920     return( AFP_OK );
921 }
922
923 static int getvolparams( bitmap, vol, st, buf, buflen )
924 u_int16_t       bitmap;
925 struct vol      *vol;
926 struct stat     *st;
927 char    *buf;
928 int             *buflen;
929 {
930     struct adouble      ad;
931     int                 bit = 0, isad = 1;
932     u_int32_t           aint;
933     u_short             ashort;
934     u_int32_t           bfree, btotal, bsize;
935     VolSpace            xbfree, xbtotal; /* extended bytes */
936     char                *data, *nameoff = NULL;
937     char                *slash;
938
939     /* courtesy of jallison@whistle.com:
940      * For MacOS8.x support we need to create the
941      * .Parent file here if it doesn't exist. */
942
943     memset(&ad, 0, sizeof(ad));
944     if ( ad_open( vol->v_path, vol_noadouble(vol) |
945                   ADFLAGS_HF|ADFLAGS_DIR, O_RDWR | O_CREAT,
946                   0666, &ad) < 0 ) {
947         isad = 0;
948
949     } else if (ad_get_HF_flags( &ad ) & O_CREAT) {
950         slash = strrchr( vol->v_path, '/' );
951         if(slash)
952             slash++;
953         else
954             slash = vol->v_path;
955
956         ad_setentrylen( &ad, ADEID_NAME, strlen( slash ));
957         memcpy(ad_entry( &ad, ADEID_NAME ), slash,
958                ad_getentrylen( &ad, ADEID_NAME ));
959         ad_setdate(&ad, AD_DATE_CREATE | AD_DATE_UNIX, st->st_mtime);
960         ad_flush(&ad, ADFLAGS_HF);
961     }
962
963     if (( bitmap & ( (1<<VOLPBIT_BFREE)|(1<<VOLPBIT_BTOTAL) |
964                      (1<<VOLPBIT_XBFREE)|(1<<VOLPBIT_XBTOTAL) |
965                      (1<<VOLPBIT_BSIZE)) ) != 0 ) {
966         if ( getvolspace( vol, &bfree, &btotal, &xbfree, &xbtotal,
967                           &bsize) < 0 ) {
968             if ( isad ) {
969                 ad_close( &ad, ADFLAGS_HF );
970             }
971             return( AFPERR_PARAM );
972         }
973     }
974
975     data = buf;
976     while ( bitmap != 0 ) {
977         while (( bitmap & 1 ) == 0 ) {
978             bitmap = bitmap>>1;
979             bit++;
980         }
981
982         switch ( bit ) {
983         case VOLPBIT_ATTR :
984             ashort = 0;
985 #ifdef CNID_DB
986             if (0 == (vol->v_flags & AFPVOL_NOFILEID) && vol->v_db != NULL) {
987                 ashort = VOLPBIT_ATTR_FILEID;
988             }
989 #endif /* CNID_DB */
990             /* check for read-only.
991              * NOTE: we don't actually set the read-only flag unless
992              *       it's passed in that way as it's possible to mount
993              *       a read-write filesystem under a read-only one. */
994             if ((vol->v_flags & AFPVOL_RO) ||
995                     ((utime(vol->v_path, NULL) < 0) && (errno == EROFS))) {
996                 ashort |= VOLPBIT_ATTR_RO;
997             }
998             ashort |= VOLPBIT_ATTR_CATSEARCH;
999             if (afp_version >= 30) {
1000                 ashort |= VOLPBIT_ATTR_UTF8;
1001                 if (vol->v_flags & AFPVOL_UNIX_PRIV)
1002                     ashort |= VOLPBIT_ATTR_UNIXPRIV;
1003             }
1004             ashort = htons(ashort);
1005             memcpy(data, &ashort, sizeof( ashort ));
1006             data += sizeof( ashort );
1007             break;
1008
1009         case VOLPBIT_SIG :
1010             ashort = htons( AFPVOLSIG_DEFAULT );
1011             memcpy(data, &ashort, sizeof( ashort ));
1012             data += sizeof( ashort );
1013             break;
1014
1015         case VOLPBIT_CDATE :
1016             if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
1017                 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1018             memcpy(data, &aint, sizeof( aint ));
1019             data += sizeof( aint );
1020             break;
1021
1022         case VOLPBIT_MDATE :
1023             if ( st->st_mtime > vol->v_time ) {
1024                 vol->v_time = st->st_mtime;
1025                 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1026             } else {
1027                 aint = AD_DATE_FROM_UNIX(vol->v_time);
1028             }
1029             memcpy(data, &aint, sizeof( aint ));
1030             data += sizeof( aint );
1031             break;
1032
1033         case VOLPBIT_BDATE :
1034             if (!isad ||  (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1035                 aint = AD_DATE_START;
1036             memcpy(data, &aint, sizeof( aint ));
1037             data += sizeof( aint );
1038             break;
1039
1040         case VOLPBIT_VID :
1041             memcpy(data, &vol->v_vid, sizeof( vol->v_vid ));
1042             data += sizeof( vol->v_vid );
1043             break;
1044
1045         case VOLPBIT_BFREE :
1046             bfree = htonl( bfree );
1047             memcpy(data, &bfree, sizeof( bfree ));
1048             data += sizeof( bfree );
1049             break;
1050
1051         case VOLPBIT_BTOTAL :
1052             btotal = htonl( btotal );
1053             memcpy(data, &btotal, sizeof( btotal ));
1054             data += sizeof( btotal );
1055             break;
1056
1057 #ifndef NO_LARGE_VOL_SUPPORT
1058         case VOLPBIT_XBFREE :
1059             xbfree = hton64( xbfree );
1060 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1061             bcopy(&xbfree, data, sizeof(xbfree));
1062 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1063             memcpy(data, &xbfree, sizeof( xbfree ));
1064 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1065             data += sizeof( xbfree );
1066             break;
1067
1068         case VOLPBIT_XBTOTAL :
1069             xbtotal = hton64( xbtotal );
1070 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1071             bcopy(&xbtotal, data, sizeof(xbtotal));
1072 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1073             memcpy(data, &xbtotal, sizeof( xbtotal ));
1074 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1075             data += sizeof( xbfree );
1076             break;
1077 #endif /* ! NO_LARGE_VOL_SUPPORT */
1078
1079         case VOLPBIT_NAME :
1080             nameoff = data;
1081             data += sizeof( u_int16_t );
1082             break;
1083
1084         case VOLPBIT_BSIZE:  /* block size */
1085             bsize = htonl(bsize);
1086             memcpy(data, &bsize, sizeof(bsize));
1087             data += sizeof(bsize);
1088             break;
1089
1090         default :
1091             if ( isad ) {
1092                 ad_close( &ad, ADFLAGS_HF );
1093             }
1094             return( AFPERR_BITMAP );
1095         }
1096         bitmap = bitmap>>1;
1097         bit++;
1098     }
1099     if ( nameoff ) {
1100         ashort = htons( data - buf );
1101         memcpy(nameoff, &ashort, sizeof( ashort ));
1102         aint = strlen( vol->v_name );
1103         *data++ = aint;
1104         memcpy(data, vol->v_name, aint );
1105         data += aint;
1106     }
1107     if ( isad ) {
1108         ad_close( &ad, ADFLAGS_HF );
1109     }
1110     *buflen = data - buf;
1111     return( AFP_OK );
1112 }
1113
1114
1115
1116 int afp_getsrvrparms(obj, ibuf, ibuflen, rbuf, rbuflen )
1117 AFPObj      *obj;
1118 char    *ibuf, *rbuf;
1119 int     ibuflen, *rbuflen;
1120 {
1121     struct timeval      tv;
1122     struct stat         st;
1123     struct vol          *volume;
1124     char        *data;
1125     int                 vcnt, len;
1126
1127
1128     if (!volumes)
1129         load_volumes(obj);
1130
1131     data = rbuf + 5;
1132     for ( vcnt = 0, volume = volumes; volume; volume = volume->v_next ) {
1133         if ( stat( volume->v_path, &st ) < 0 ) {
1134             LOG(log_info, logtype_afpd, "afp_getsrvrparms: stat %s: %s",
1135                 volume->v_path, strerror(errno) );
1136             continue;           /* can't access directory */
1137         }
1138         if (!S_ISDIR(st.st_mode)) {
1139             continue;           /* not a dir */
1140         }
1141
1142         /* set password bit if there's a volume password */
1143         *data = (volume->v_password) ? AFPSRVR_PASSWD : 0;
1144
1145         /* Apple 2 clients running ProDOS-8 expect one volume to have
1146            bit 0 of this byte set.  They will not recognize anything
1147            on the server unless this is the case.  I have not
1148            completely worked this out, but it's related to booting
1149            from the server.  Support for that function is a ways
1150            off.. <shirsch@ibm.net> */
1151         *data |= (volume->v_flags & AFPVOL_A2VOL) ? AFPSRVR_CONFIGINFO : 0;
1152         *data++ |= 0; /* UNIX PRIVS BIT ..., OSX doesn't seem to use it, so we don't either */
1153         len = strlen( volume->v_name );
1154         *data++ = len;
1155         memcpy(data, volume->v_name, len );
1156         data += len;
1157         vcnt++;
1158     }
1159
1160     *rbuflen = data - rbuf;
1161     data = rbuf;
1162     if ( gettimeofday( &tv, 0 ) < 0 ) {
1163         LOG(log_error, logtype_afpd, "afp_getsrvrparms: gettimeofday: %s", strerror(errno) );
1164         *rbuflen = 0;
1165         return AFPERR_PARAM;
1166     }
1167     tv.tv_sec = AD_DATE_FROM_UNIX(tv.tv_sec);
1168     memcpy(data, &tv.tv_sec, sizeof( u_int32_t));
1169     data += sizeof( u_int32_t);
1170     *data = vcnt;
1171     return( AFP_OK );
1172 }
1173
1174 int afp_openvol(obj, ibuf, ibuflen, rbuf, rbuflen )
1175 AFPObj      *obj;
1176 char    *ibuf, *rbuf;
1177 int             ibuflen, *rbuflen;
1178 {
1179     struct stat st;
1180     char        *volname;
1181 #ifndef CNID_DB
1182     char        *p;
1183 #else
1184     int         opened = 0;
1185 #endif /* CNID_DB */
1186     struct vol  *volume;
1187     struct dir  *dir;
1188     int         len, ret, buflen;
1189     u_int16_t   bitmap;
1190
1191     ibuf += 2;
1192     memcpy(&bitmap, ibuf, sizeof( bitmap ));
1193     bitmap = ntohs( bitmap );
1194     ibuf += sizeof( bitmap );
1195     if (( bitmap & (1<<VOLPBIT_VID)) == 0 ) {
1196         ret = AFPERR_BITMAP;
1197         goto openvol_err;
1198     }
1199
1200     len = (unsigned char)*ibuf++;
1201     volname = obj->oldtmp;
1202     memcpy(volname, ibuf, len );
1203     *(volname +  len) = '\0';
1204     ibuf += len;
1205     if ((len + 1) & 1) /* pad to an even boundary */
1206         ibuf++;
1207
1208     if (!volumes)
1209         load_volumes(obj);
1210
1211     for ( volume = volumes; volume; volume = volume->v_next ) {
1212         if ( strcasecmp( volname, volume->v_name ) == 0 ) {
1213             break;
1214         }
1215     }
1216
1217     if ( volume == NULL ) {
1218         ret = AFPERR_PARAM;
1219         goto openvol_err;
1220     }
1221
1222     /* check for a volume password */
1223     if (volume->v_password &&
1224             strncmp(ibuf, volume->v_password, VOLPASSLEN)) {
1225         ret = AFPERR_ACCESS;
1226         goto openvol_err;
1227     }
1228     /* FIXME 
1229     */
1230     if (afp_version >= 30) {
1231         volume->max_filename = 255;
1232     }
1233     else {
1234         volume->max_filename = MACFILELEN;
1235     }
1236     if (( volume->v_flags & AFPVOL_OPEN  ) == 0 ) {
1237         /* FIXME unix name != mac name */
1238         if ((dir = dirnew(volume->v_name, volume->v_name) ) == NULL) {
1239             LOG(log_error, logtype_afpd, "afp_openvol: malloc: %s", strerror(errno) );
1240             ret = AFPERR_MISC;
1241             goto openvol_err;
1242         }
1243         dir->d_did = DIRDID_ROOT;
1244         dir->d_color = DIRTREE_COLOR_BLACK; /* root node is black */
1245         volume->v_dir = volume->v_root = dir;
1246         volume->v_flags |= AFPVOL_OPEN;
1247 #ifdef CNID_DB
1248         volume->v_db = NULL;
1249         opened = 1;
1250 #endif
1251     }
1252     else {
1253        /* FIXME */
1254     }    
1255 #ifdef FORCE_UIDGID
1256     set_uidgid ( volume );
1257 #endif /* FORCE_UIDGID */
1258
1259     if ( stat( volume->v_path, &st ) < 0 ) {
1260         ret = AFPERR_PARAM;
1261         goto openvol_err;
1262     }
1263
1264     if ( chdir( volume->v_path ) < 0 ) {
1265         ret = AFPERR_PARAM;
1266         goto openvol_err;
1267     }
1268     curdir = volume->v_dir;
1269
1270 #ifdef CNID_DB
1271     if (opened) {
1272         if (volume->v_dbpath)
1273             volume->v_db = cnid_open (volume->v_dbpath, volume->v_umask);
1274         if (volume->v_db == NULL)
1275             volume->v_db = cnid_open (volume->v_path, volume->v_umask);
1276         if (volume->v_db == NULL) {
1277            /* config option? 
1278             * - mount the volume readonly
1279             * - return an error
1280             * - read/write with other scheme
1281             */
1282         }
1283     }
1284 #endif /* CNID_DB */
1285
1286     buflen = *rbuflen - sizeof( bitmap );
1287     if (( ret = getvolparams( bitmap, volume, &st,
1288                               rbuf + sizeof(bitmap), &buflen )) != AFP_OK ) {
1289         goto openvol_err;
1290     }
1291 #ifdef AFP3x
1292     volume->v_utf8toucs2 = (iconv_t)(-1);
1293     volume->v_ucs2toutf8 = (iconv_t)(-1);
1294     volume->v_mactoutf8  = (iconv_t)(-1);
1295     volume->v_ucs2tomac  = (iconv_t)(-1);
1296     
1297     if (vol_utf8(volume)) {
1298         if ((iconv_t)(-1) == (volume->v_utf8toucs2 = iconv_open("UCS-2LE", "UTF-8"))) {
1299             LOG(log_error, logtype_afpd, "openvol: no UTF8 to UCS-2LE");
1300             goto openvol_err;
1301         }
1302         if ((iconv_t)(-1) == (volume->v_ucs2toutf8 = iconv_open("UTF-8", "UCS-2LE"))) {
1303             LOG(log_error, logtype_afpd, "openvol: no UCS-2LE to UTF-8");
1304             goto openvol_err_iconv;
1305         }
1306         if ((iconv_t)(-1) == (volume->v_mactoutf8 = iconv_open("UTF-8", "MAC"))) {
1307             LOG(log_error, logtype_afpd, "openvol: no MAC to UTF-8");
1308             goto openvol_err_iconv;
1309         }
1310         if ((iconv_t)(-1) == (volume->v_ucs2tomac = iconv_open("MAC", "UCS-2LE"))) {
1311             LOG(log_error, logtype_afpd, "openvol:  no UCS-2LE to MAC");
1312             goto openvol_err_iconv;
1313         }
1314     }
1315 #endif
1316     *rbuflen = buflen + sizeof( bitmap );
1317     bitmap = htons( bitmap );
1318     memcpy(rbuf, &bitmap, sizeof( bitmap ));
1319
1320 #ifndef CNID_DB
1321     /*
1322      * If you mount a volume twice, the second time the trash appears on
1323      * the desk-top.  That's because the Mac remembers the DID for the
1324      * trash (even for volumes in different zones, on different servers).
1325      * Just so this works better, we prime the DID cache with the trash,
1326      * fixing the trash at DID 17.
1327      */
1328     p = Trash;
1329     cname( volume, volume->v_dir, &p );
1330 #endif /* CNID_DB */
1331
1332     return( AFP_OK );
1333 #ifdef AFP3x
1334 openvol_err_iconv:
1335     if (volume->v_utf8toucs2 != (iconv_t)(-1))
1336         iconv_close(volume->v_utf8toucs2);
1337     if (volume->v_ucs2toutf8 != (iconv_t)(-1))
1338         iconv_close(volume->v_ucs2toutf8);
1339     if (volume->v_mactoutf8  != (iconv_t)(-1))
1340         iconv_close(volume->v_mactoutf8);    
1341     if (volume->v_ucs2tomac  != (iconv_t)(-1))
1342         iconv_close(volume->v_ucs2tomac);
1343 #endif        
1344 openvol_err:
1345 #ifdef CNID_DB
1346     if (opened && volume->v_db != NULL) {
1347         cnid_close(volume->v_db);
1348         volume->v_db = NULL;
1349     }
1350 #endif
1351     *rbuflen = 0;
1352     return ret;
1353 }
1354
1355 int afp_closevol(obj, ibuf, ibuflen, rbuf, rbuflen )
1356 AFPObj      *obj;
1357 char    *ibuf, *rbuf;
1358 int             ibuflen, *rbuflen;
1359 {
1360     struct vol  *vol, *ovol;
1361     u_int16_t   vid;
1362
1363     *rbuflen = 0;
1364     ibuf += 2;
1365     memcpy(&vid, ibuf, sizeof( vid ));
1366     if (NULL == ( vol = getvolbyvid( vid )) ) {
1367         return( AFPERR_PARAM );
1368     }
1369
1370     vol->v_flags &= ~AFPVOL_OPEN;
1371     for ( ovol = volumes; ovol; ovol = ovol->v_next ) {
1372         if ( ovol->v_flags & AFPVOL_OPEN ) {
1373             break;
1374         }
1375     }
1376     if ( ovol != NULL ) {
1377         /* Even if chdir fails, we can't say afp_closevol fails. */
1378         if ( chdir( ovol->v_path ) == 0 ) {
1379             curdir = ovol->v_dir;
1380         }
1381     }
1382     dirfree( vol->v_root );
1383     vol->v_dir = NULL;
1384 #ifdef CNID_DB
1385     cnid_close(vol->v_db);
1386     vol->v_db = NULL;
1387 #endif /* CNID_DB */
1388     return( AFP_OK );
1389 }
1390
1391 struct vol *getvolbyvid(const u_int16_t vid )
1392 {
1393     struct vol  *vol;
1394
1395     for ( vol = volumes; vol; vol = vol->v_next ) {
1396         if ( vid == vol->v_vid ) {
1397             break;
1398         }
1399     }
1400     if ( vol == NULL || ( vol->v_flags & AFPVOL_OPEN ) == 0 ) {
1401         return( NULL );
1402     }
1403
1404 #ifdef FORCE_UIDGID
1405     set_uidgid ( vol );
1406 #endif /* FORCE_UIDGID */
1407
1408     return( vol );
1409 }
1410
1411 /* ------------------------ */
1412 static int ext_cmp_key(const void *key, const void *obj)
1413 {
1414     const char          *p = key;
1415     const struct extmap *em = obj;
1416     return strdiacasecmp(p, em->em_ext);
1417 }
1418 struct extmap *getextmap(const char *path)
1419 {
1420     char          *p;
1421     struct extmap *em;
1422
1423     if (NULL == ( p = strrchr( path, '.' )) ) {
1424         return( defextmap );
1425     }
1426     p++;
1427     if (!*p || !extmap_cnt) {
1428         return( defextmap );
1429     }
1430     em = bsearch(p, extmap, extmap_cnt, sizeof(struct extmap), ext_cmp_key);
1431     if (em) {
1432         return( em );
1433     } else {
1434         return( defextmap );
1435     }
1436 }
1437
1438 struct extmap *getdefextmap(void)
1439 {
1440     return( defextmap );
1441 }
1442
1443 /*
1444    poll if a volume is changed by other processes.
1445 */
1446 int  pollvoltime(obj)
1447 AFPObj *obj;
1448 {
1449     struct vol       *vol;
1450     struct timeval   tv;
1451     struct stat      st;
1452     
1453     if (!(afp_version > 21 && obj->options.server_notif)) 
1454          return 0;
1455
1456     if ( gettimeofday( &tv, 0 ) < 0 ) 
1457          return 0;
1458
1459     for ( vol = volumes; vol; vol = vol->v_next ) {
1460         if ( (vol->v_flags & AFPVOL_OPEN)  && vol->v_time + 30 < tv.tv_sec) {
1461             if ( !stat( vol->v_path, &st ) && vol->v_time != st.st_mtime ) {
1462                 vol->v_time = st.st_mtime;
1463                 if (!obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED))
1464                     return -1;
1465                 return 1;
1466             }
1467         }
1468     }
1469     return 0;
1470 }
1471
1472 void setvoltime(obj, vol )
1473 AFPObj *obj;
1474 struct vol      *vol;
1475 {
1476     struct timeval      tv;
1477
1478     /* just looking at vol->v_time is broken seriously since updates
1479      * from other users afpd processes never are seen.
1480      * This is not the most elegant solution (a shared memory between
1481      * the afpd processes would come closer)
1482      * [RS] */
1483
1484     if ( gettimeofday( &tv, 0 ) < 0 ) {
1485         LOG(log_error, logtype_afpd, "setvoltime: gettimeofday: %s", strerror(errno) );
1486         return;
1487     }
1488     if( utime( vol->v_path, NULL ) < 0 ) {
1489         /* write of time failed ... probably a read only filesys,
1490          * where no other users can interfere, so there's no issue here
1491          */
1492     }
1493
1494     /* a little granularity */
1495     if (vol->v_time < tv.tv_sec) {
1496         vol->v_time = tv.tv_sec;
1497         if (afp_version > 21 && obj->options.server_notif) {
1498             obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED);
1499         }
1500     }
1501 }
1502
1503 int afp_getvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1504 AFPObj      *obj;
1505 char    *ibuf, *rbuf;
1506 int             ibuflen, *rbuflen;
1507 {
1508     struct stat st;
1509     struct vol  *vol;
1510     int         buflen, ret;
1511     u_int16_t   vid, bitmap;
1512
1513     ibuf += 2;
1514     memcpy(&vid, ibuf, sizeof( vid ));
1515     ibuf += sizeof( vid );
1516     memcpy(&bitmap, ibuf, sizeof( bitmap ));
1517     bitmap = ntohs( bitmap );
1518
1519     if (NULL == ( vol = getvolbyvid( vid )) ) {
1520         *rbuflen = 0;
1521         return( AFPERR_PARAM );
1522     }
1523
1524     if ( stat( vol->v_path, &st ) < 0 ) {
1525         *rbuflen = 0;
1526         return( AFPERR_PARAM );
1527     }
1528
1529     buflen = *rbuflen - sizeof( bitmap );
1530     if (( ret = getvolparams( bitmap, vol, &st,
1531                               rbuf + sizeof( bitmap ), &buflen )) != AFP_OK ) {
1532         *rbuflen = 0;
1533         return( ret );
1534     }
1535     *rbuflen = buflen + sizeof( bitmap );
1536     bitmap = htons( bitmap );
1537     memcpy(rbuf, &bitmap, sizeof( bitmap ));
1538     return( AFP_OK );
1539 }
1540
1541 int afp_setvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1542 AFPObj      *obj;
1543 char    *ibuf, *rbuf;
1544 int             ibuflen, *rbuflen;
1545 {
1546     struct adouble ad;
1547     struct vol  *vol;
1548     u_int16_t   vid, bitmap;
1549     u_int32_t   aint;
1550
1551     ibuf += 2;
1552     *rbuflen = 0;
1553
1554     memcpy(&vid, ibuf, sizeof( vid ));
1555     ibuf += sizeof( vid );
1556     memcpy(&bitmap, ibuf, sizeof( bitmap ));
1557     bitmap = ntohs( bitmap );
1558     ibuf += sizeof(bitmap);
1559
1560     if (( vol = getvolbyvid( vid )) == NULL ) {
1561         return( AFPERR_PARAM );
1562     }
1563
1564     if (vol->v_flags & AFPVOL_RO)
1565         return AFPERR_VLOCK;
1566
1567     /* we can only set the backup date. */
1568     if (bitmap != (1 << VOLPBIT_BDATE))
1569         return AFPERR_BITMAP;
1570
1571     memset(&ad, 0, sizeof(ad));
1572     if ( ad_open( vol->v_path, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR,
1573                   0666, &ad) < 0 ) {
1574         if (errno == EROFS)
1575             return AFPERR_VLOCK;
1576
1577         return AFPERR_ACCESS;
1578     }
1579
1580     memcpy(&aint, ibuf, sizeof(aint));
1581     ad_setdate(&ad, AD_DATE_BACKUP, aint);
1582     ad_flush(&ad, ADFLAGS_HF);
1583     ad_close(&ad, ADFLAGS_HF);
1584     return( AFP_OK );
1585 }
1586
1587
1588 int wincheck(const struct vol *vol, const char *path)
1589 {
1590     int len;
1591
1592     if (!(vol->v_flags & AFPVOL_MSWINDOWS))
1593         return 1;
1594
1595     /* empty paths are not allowed */
1596     if ((len = strlen(path)) == 0)
1597         return 0;
1598
1599     /* leading or trailing whitespaces are not allowed, carriage returns
1600      * and probably other whitespace is okay, tabs are not allowed
1601      */
1602     if ((path[0] == ' ') || (path[len-1] == ' '))
1603         return 0;
1604
1605     /* certain characters are not allowed */
1606     if (strpbrk(path, MSWINDOWS_BADCHARS))
1607         return 0;
1608
1609     /* everything else is okay */
1610     return 1;
1611 }
1612
1613
1614 void close_vols ()
1615 {
1616 #ifdef CNID_DB
1617     struct vol  *vol;
1618
1619     for ( vol = volumes; vol; vol = vol->v_next ) {
1620         if ( (vol->v_flags & AFPVOL_OPEN ) == 0 ) 
1621                 continue;
1622         if ( vol->v_db )
1623             cnid_close(vol->v_db);
1624         vol->v_db = NULL;
1625     }
1626 #endif
1627 }
1628