]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/volume.c
Volname mangling. Fixes bug #2611981, from HAT. Also add new logger doc to afpd.conf...
[netatalk.git] / etc / afpd / volume.c
1 /*
2  * $Id: volume.c,v 1.81 2009-03-16 13:59:12 franklahm 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 <stdio.h>
13 #include <stdlib.h>
14 #include <ctype.h>
15 #include <dirent.h>
16 #include <pwd.h>
17 #include <grp.h>
18 #include <utime.h>
19 #include <errno.h>
20 #ifdef HAVE_STRINGS_H
21 #include <strings.h>
22 #endif
23 /* STDC check */
24 #if STDC_HEADERS
25 #include <string.h>
26 #else /* STDC_HEADERS */
27 #ifndef HAVE_STRCHR
28 #define strchr index
29 #define strrchr index
30 #endif /* HAVE_STRCHR */
31 char *strchr (), *strrchr ();
32 #ifndef HAVE_MEMCPY
33 #define memcpy(d,s,n) bcopy ((s), (d), (n))
34 #define memmove(d,s,n) bcopy ((s), (d), (n))
35 #endif /* ! HAVE_MEMCPY */
36 #endif /* STDC_HEADERS */
37 #include <sys/param.h>
38 #include <sys/socket.h>
39 #include <netinet/in.h>
40 #include <arpa/inet.h>
41 #include <atalk/asp.h>
42 #include <atalk/dsi.h>
43 #include <atalk/adouble.h>
44 #include <atalk/afp.h>
45 #include <atalk/util.h>
46 #include <atalk/logger.h>
47 #ifdef CNID_DB
48 #include <atalk/cnid.h>
49 #endif /* CNID_DB*/
50
51 #include "globals.h"
52 #include "directory.h"
53 #include "file.h"
54 #include "volume.h"
55 #include "unix.h"
56 #include "mangle.h"
57 #include "fork.h"
58
59 extern int afprun(int root, char *cmd, int *outfd);
60
61 #ifndef MIN
62 #define MIN(a, b) ((a) < (b) ? (a) : (b))
63 #endif /* ! MIN */
64
65 #ifndef NO_LARGE_VOL_SUPPORT
66 #if BYTE_ORDER == BIG_ENDIAN
67 #define hton64(x)       (x)
68 #define ntoh64(x)       (x)
69 #else /* BYTE_ORDER == BIG_ENDIAN */
70 #define hton64(x)       ((u_int64_t) (htonl(((x) >> 32) & 0xffffffffLL)) | \
71                          (u_int64_t) ((htonl(x) & 0xffffffffLL) << 32))
72 #define ntoh64(x)       (hton64(x))
73 #endif /* BYTE_ORDER == BIG_ENDIAN */
74 #endif /* ! NO_LARGE_VOL_SUPPORT */
75
76 static struct vol *Volumes = NULL;
77 static u_int16_t        lastvid = 0;
78 static char             *Trash = "\02\024Network Trash Folder";
79
80 static struct extmap    *Extmap = NULL, *Defextmap = NULL;
81 static int              Extmap_cnt;
82 static void             free_extmap(void);
83
84 #define VOLOPT_ALLOW      0  /* user allow list */
85 #define VOLOPT_DENY       1  /* user deny list */
86 #define VOLOPT_RWLIST     2  /* user rw list */
87 #define VOLOPT_ROLIST     3  /* user ro list */
88 #define VOLOPT_PASSWORD   4  /* volume password */
89 #define VOLOPT_CASEFOLD   5  /* character case mangling */
90 #define VOLOPT_FLAGS      6  /* various flags */
91 #define VOLOPT_DBPATH     7  /* path to database */
92 #define VOLOPT_MAPCHARS   8  /* does mtou and utom mappings. syntax:
93 m and u can be double-byte hex
94 strings if necessary.
95 m=u -> map both ways
96   m>u -> map m to u
97   m<u -> map u to m
98   !u  -> make u illegal always
99   ~u  -> make u illegal only as the first
100   part of a double-byte character.
101   */
102 #define VOLOPT_VETO          10  /* list of veto filespec */
103 #define VOLOPT_PREEXEC       11  /* preexec command */
104 #define VOLOPT_ROOTPREEXEC   12  /* root preexec command */
105
106 #define VOLOPT_POSTEXEC      13  /* postexec command */
107 #define VOLOPT_ROOTPOSTEXEC  14  /* root postexec command */
108
109 #define VOLOPT_ENCODING      15  /* mac encoding (pre OSX)*/
110 #define VOLOPT_MACCHARSET    16
111 #define VOLOPT_CNIDSCHEME    17
112 #define VOLOPT_ADOUBLE       18  /* adouble version */
113 #ifdef FORCE_UIDGID
114 #warning UIDGID
115 #include "uid.h"
116
117 #define VOLOPT_FORCEUID  19  /* force uid for username x */
118 #define VOLOPT_FORCEGID  20  /* force gid for group x */
119 #endif /* FORCE_UIDGID */
120
121 #define VOLOPT_UMASK     21
122 #define VOLOPT_DPERM     22  /* dperm default directories perms */
123 #define VOLOPT_FPERM     23  /* fperm default files perms */
124 #define VOLOPT_DFLTPERM  24  /* perm */
125
126 #define VOLOPT_MAX       (VOLOPT_DFLTPERM +1)
127
128 #define VOLOPT_NUM        (VOLOPT_MAX + 1)
129
130 #define VOLPASSLEN  8
131 #define VOLOPT_DEFAULT     ":DEFAULT:"
132 #define VOLOPT_DEFAULT_LEN 9
133   struct vol_option {
134       char *c_value;
135       int i_value;
136   };
137
138 typedef struct _special_folder {
139         const char *name;
140         int precreate;
141         mode_t mode;
142         int hide;
143 } _special_folder;
144
145 static const _special_folder special_folders[] = {
146   {"Network Trash Folder",     1,  0777,  1},
147   {"Temporary Items",          1,  0777,  1},
148   {".AppleDesktop",            1,  0777,  0},
149 #if 0
150   {"TheFindByContentFolder",   0,     0,  1},
151   {"TheVolumeSettingsFolder",  0,     0,  1},
152 #endif
153   {NULL, 0, 0, 0}};
154
155 typedef struct _volopt_name {
156         const u_int32_t option;
157         const char      *name;
158 } _vol_opt_name;
159
160 static const _vol_opt_name vol_opt_names[] = {
161     {AFPVOL_A2VOL,      "PRODOS"},      /* prodos volume */
162     {AFPVOL_CRLF,       "CRLF"},        /* cr/lf translation */
163     {AFPVOL_NOADOUBLE,  "NOADOUBLE"},   /* don't create .AppleDouble by default */
164     {AFPVOL_RO,         "READONLY"},    /* read-only volume */
165     {AFPVOL_MSWINDOWS,  "MSWINDOWS"},   /* deal with ms-windows yuckiness. this is going away. */
166     {AFPVOL_NOHEX,      "NOHEX"},       /* don't do :hex translation */
167     {AFPVOL_USEDOTS,    "USEDOTS"},     /* use real dots */
168     {AFPVOL_LIMITSIZE,  "LIMITSIZE"},   /* limit size for older macs */
169     {AFPVOL_MAPASCII,   "MAPASCII"},    /* map the ascii range as well */
170     {AFPVOL_DROPBOX,    "DROPBOX"},     /* dropkludge dropbox support */
171     {AFPVOL_NOFILEID,   "NOFILEID"},    /* don't advertise createid resolveid and deleteid calls */
172     {AFPVOL_NOSTAT,     "NOSTAT"},      /* advertise the volume even if we can't stat() it
173                                          * maybe because it will be mounted later in preexec */
174     {AFPVOL_UNIX_PRIV,  "UNIXPRIV"},    /* support unix privileges */
175     {AFPVOL_NODEV,      "NODEV"},       /* always use 0 for device number in cnid calls */
176     {AFPVOL_EILSEQ,     "ILLEGALSEQ"},     /* encode illegal sequence */
177     {AFPVOL_CACHE,      "CACHEID"},     /* Use adouble v2 CNID caching, default don't use it */
178     {AFPVOL_EXT_ATTRS,  "EXT_ATTRS"},   /* Vol supports Extened Attributes */
179     {AFPVOL_ACLS,       "ACLS"},        /* Vol supports ACLs */
180     {0, NULL}
181 };
182
183 static const _vol_opt_name vol_opt_casefold[] = {
184     {AFPVOL_MTOUUPPER,  "MTOULOWER"},
185     {AFPVOL_MTOULOWER,  "MTOULOWER"},
186     {AFPVOL_UTOMUPPER,  "UTOMUPPER"},
187     {AFPVOL_UTOMLOWER,  "UTOMLOWER"},
188     {0, NULL}
189 };
190
191 static void handle_special_folders (const struct vol *);
192 static int savevoloptions (const struct vol *);
193 static void deletevol(struct vol *vol);
194 static void volume_free(struct vol *vol);
195
196 static void volfree(struct vol_option *options,
197                                const struct vol_option *save)
198 {
199     int i;
200
201     if (save) {
202         for (i = 0; i < VOLOPT_MAX; i++) {
203             if (options[i].c_value && (options[i].c_value != save[i].c_value))
204                 free(options[i].c_value);
205         }
206     } else {
207         for (i = 0; i < VOLOPT_MAX; i++) {
208             if (options[i].c_value)
209                 free(options[i].c_value);
210         }
211     }
212 }
213
214
215 /* handle variable substitutions. here's what we understand:
216  * $b   -> basename of path
217  * $c   -> client ip/appletalk address
218  * $d   -> volume pathname on server
219  * $f   -> full name (whatever's in the gecos field)
220  * $g   -> group
221  * $h   -> hostname 
222  * $i   -> client ip/appletalk address without port
223  * $s   -> server name (hostname if it doesn't exist)
224  * $u   -> username (guest is usually nobody)
225  * $v   -> volume name or basename if null
226  * $z   -> zone (may not exist)
227  * $$   -> $
228  *
229  *
230  */
231 #define is_var(a, b) (strncmp((a), (b), 2) == 0)
232
233 static char *volxlate(AFPObj *obj, char *dest, size_t destlen,
234                      char *src, struct passwd *pwd, char *path, char *volname)
235 {
236     char *p, *q;
237     int len;
238     char *ret;
239     
240     if (!src) {
241         return NULL;
242     }
243     if (!dest) {
244         dest = calloc(destlen +1, 1);
245     }
246     ret = dest;
247     if (!ret) {
248         return NULL;
249     }
250     strlcpy(dest, src, destlen +1);
251     if ((p = strchr(src, '$')) == NULL) /* nothing to do */
252         return ret;
253
254     /* first part of the path. just forward to the next variable. */
255     len = MIN((size_t)(p - src), destlen);
256     if (len > 0) {
257         destlen -= len;
258         dest += len;
259     }
260
261     while (p && destlen > 0) {
262         /* now figure out what the variable is */
263         q = NULL;
264         if (is_var(p, "$b")) {
265             if (path) {
266                 if ((q = strrchr(path, '/')) == NULL)
267                     q = path;
268                 else if (*(q + 1) != '\0')
269                     q++;
270             }
271         } else if (is_var(p, "$c")) {
272             if (obj->proto == AFPPROTO_ASP) {
273                 ASP asp = obj->handle;
274
275                 len = sprintf(dest, "%u.%u", ntohs(asp->asp_sat.sat_addr.s_net),
276                               asp->asp_sat.sat_addr.s_node);
277                 dest += len;
278                 destlen -= len;
279
280             } else if (obj->proto == AFPPROTO_DSI) {
281                 DSI *dsi = obj->handle;
282
283                 len = sprintf(dest, "%s:%u", inet_ntoa(dsi->client.sin_addr),
284                               ntohs(dsi->client.sin_port));
285                 dest += len;
286                 destlen -= len;
287             }
288         } else if (is_var(p, "$d")) {
289              q = path;
290         } else if (is_var(p, "$f")) {
291             if ((q = strchr(pwd->pw_gecos, ',')))
292                 *q = '\0';
293             q = pwd->pw_gecos;
294         } else if (is_var(p, "$g")) {
295             struct group *grp = getgrgid(pwd->pw_gid);
296             if (grp)
297                 q = grp->gr_name;
298         } else if (is_var(p, "$h")) {
299             q = obj->options.hostname;
300         } else if (is_var(p, "$i")) {
301             if (obj->proto == AFPPROTO_ASP) {
302                 ASP asp = obj->handle;
303  
304                 len = sprintf(dest, "%u", ntohs(asp->asp_sat.sat_addr.s_net));
305                 dest += len;
306                 destlen -= len;
307  
308             } else if (obj->proto == AFPPROTO_DSI) {
309                 DSI *dsi = obj->handle;
310  
311                 q = inet_ntoa(dsi->client.sin_addr);
312             }
313         } else if (is_var(p, "$s")) {
314             if (obj->Obj)
315                 q = obj->Obj;
316             else if (obj->options.server) {
317                 q = obj->options.server;
318             } else
319                 q = obj->options.hostname;
320         } else if (is_var(p, "$u")) {
321             char* sep = NULL;
322             if ( obj->options.ntseparator && (sep = strchr(obj->username, obj->options.ntseparator[0])) != NULL)
323                 q = sep+1;
324             else
325                 q = obj->username;
326         } else if (is_var(p, "$v")) {
327             if (volname) {
328                 q = volname;
329             }
330             else if (path) {
331                 if ((q = strrchr(path, '/')) == NULL)
332                     q = path;
333                 else if (*(q + 1) != '\0')
334                     q++;
335             }
336         } else if (is_var(p, "$z")) {
337             q = obj->Zone;
338         } else if (is_var(p, "$$")) {
339             q = "$";
340         } else
341             q = p;
342
343         /* copy the stuff over. if we don't understand something that we
344          * should, just skip it over. */
345         if (q) {
346             len = MIN(p == q ? 2 : strlen(q), destlen);
347             strncpy(dest, q, len);
348             dest += len;
349             destlen -= len;
350         }
351
352         /* stuff up to next $ */
353         src = p + 2;
354         p = strchr(src, '$');
355         len = p ? MIN((size_t)(p - src), destlen) : destlen;
356         if (len > 0) {
357             strncpy(dest, src, len);
358             dest += len;
359             destlen -= len;
360         }
361     }
362     return ret;
363 }
364
365 /* to make sure that val is valid, make sure to select an opt that
366    includes val */
367 static int optionok(const char *buf, const char *opt, const char *val) 
368 {
369     if (!strstr(buf,opt))
370         return 0;
371     if (!val[1])
372         return 0;
373     return 1;    
374 }
375
376
377 /* -------------------- */
378 static void setoption(struct vol_option *options, struct vol_option *save, int opt, const char *val)
379 {
380     if (options[opt].c_value && (!save || options[opt].c_value != save[opt].c_value))
381         free(options[opt].c_value);
382     options[opt].c_value = strdup(val + 1);
383 }
384
385 /* ------------------------------------------
386    handle all the options. tmp can't be NULL. */
387 static void volset(struct vol_option *options, struct vol_option *save, 
388                    char *volname, int vlen,
389                    const char *tmp)
390 {
391     char *val;
392
393     val = strchr(tmp, ':');
394     if (!val) {
395         /* we'll assume it's a volume name. */
396         strncpy(volname, tmp, vlen);
397         volname[vlen] = 0;
398         return;
399     }
400 #if 0
401     LOG(log_debug, logtype_afpd, "Parsing volset %s", val);
402 #endif
403     if (optionok(tmp, "allow:", val)) {
404         setoption(options, save, VOLOPT_ALLOW, val);
405
406     } else if (optionok(tmp, "deny:", val)) {
407         setoption(options, save, VOLOPT_DENY, val);
408
409     } else if (optionok(tmp, "rwlist:", val)) {
410         setoption(options, save, VOLOPT_RWLIST, val);
411
412     } else if (optionok(tmp, "rolist:", val)) {
413         setoption(options, save, VOLOPT_ROLIST, val);
414
415     } else if (optionok(tmp, "codepage:", val)) {
416         LOG (log_error, logtype_afpd, "The old codepage system has been removed. Please make sure to read the documentation !!!!");
417         /* Make sure we don't screw anything */
418         exit (EXITERR_CONF);
419     } else if (optionok(tmp, "volcharset:", val)) {
420         setoption(options, save, VOLOPT_ENCODING, val);
421     } else if (optionok(tmp, "maccharset:", val)) {
422         setoption(options, save, VOLOPT_MACCHARSET, val);
423     } else if (optionok(tmp, "veto:", val)) {
424         setoption(options, save, VOLOPT_VETO, val);
425     } else if (optionok(tmp, "cnidscheme:", val)) {
426         setoption(options, save, VOLOPT_CNIDSCHEME, val);
427     } else if (optionok(tmp, "casefold:", val)) {
428         if (strcasecmp(val + 1, "tolower") == 0)
429             options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMLOWER;
430         else if (strcasecmp(val + 1, "toupper") == 0)
431             options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMUPPER;
432         else if (strcasecmp(val + 1, "xlatelower") == 0)
433             options[VOLOPT_CASEFOLD].i_value = AFPVOL_UUPPERMLOWER;
434         else if (strcasecmp(val + 1, "xlateupper") == 0)
435             options[VOLOPT_CASEFOLD].i_value = AFPVOL_ULOWERMUPPER;
436     } else if (optionok(tmp, "adouble:", val)) {
437         if (strcasecmp(val + 1, "v1") == 0)
438             options[VOLOPT_ADOUBLE].i_value = AD_VERSION1;
439 #if AD_VERSION == AD_VERSION2            
440         else if (strcasecmp(val + 1, "v2") == 0)
441             options[VOLOPT_ADOUBLE].i_value = AD_VERSION2;
442         else if (strcasecmp(val + 1, "osx") == 0)
443             options[VOLOPT_ADOUBLE].i_value = AD_VERSION2_OSX;
444         else if (strcasecmp(val + 1, "ads") == 0)
445             options[VOLOPT_ADOUBLE].i_value = AD_VERSION1_ADS;
446         else if (strcasecmp(val + 1, "sfm") == 0)
447             options[VOLOPT_ADOUBLE].i_value = AD_VERSION1_SFM;
448 #endif
449     } else if (optionok(tmp, "options:", val)) {
450         char *p;
451
452         if ((p = strtok(val + 1, ",")) == NULL) /* nothing */
453             return;
454
455         while (p) {
456             if (strcasecmp(p, "prodos") == 0)
457                 options[VOLOPT_FLAGS].i_value |= AFPVOL_A2VOL;
458             else if (strcasecmp(p, "mswindows") == 0) {
459                 options[VOLOPT_FLAGS].i_value |= AFPVOL_MSWINDOWS | AFPVOL_USEDOTS;
460             } else if (strcasecmp(p, "crlf") == 0)
461                 options[VOLOPT_FLAGS].i_value |= AFPVOL_CRLF;
462             else if (strcasecmp(p, "noadouble") == 0)
463                 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOADOUBLE;
464             else if (strcasecmp(p, "ro") == 0)
465                 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
466             else if (strcasecmp(p, "nohex") == 0)
467                 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOHEX;
468             else if (strcasecmp(p, "usedots") == 0)
469                 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS;
470             else if (strcasecmp(p, "invisibledots") == 0)
471                 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS | AFPVOL_INV_DOTS;
472             else if (strcasecmp(p, "limitsize") == 0)
473                 options[VOLOPT_FLAGS].i_value |= AFPVOL_LIMITSIZE;
474             /* support for either "dropbox" or "dropkludge" */
475             else if (strcasecmp(p, "dropbox") == 0)
476                 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
477             else if (strcasecmp(p, "dropkludge") == 0)
478                 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
479             else if (strcasecmp(p, "nofileid") == 0)
480                 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOFILEID;
481             else if (strcasecmp(p, "nostat") == 0)
482                 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOSTAT;
483             else if (strcasecmp(p, "preexec_close") == 0)
484                 options[VOLOPT_PREEXEC].i_value = 1;
485             else if (strcasecmp(p, "root_preexec_close") == 0)
486                 options[VOLOPT_ROOTPREEXEC].i_value = 1;
487             else if (strcasecmp(p, "upriv") == 0)
488                 options[VOLOPT_FLAGS].i_value |= AFPVOL_UNIX_PRIV;
489             else if (strcasecmp(p, "extattrs") == 0)
490                 options[VOLOPT_FLAGS].i_value |= AFPVOL_EXT_ATTRS;
491             else if (strcasecmp(p, "acls") == 0)
492                 options[VOLOPT_FLAGS].i_value |= AFPVOL_ACLS;
493             else if (strcasecmp(p, "nodev") == 0)
494                 options[VOLOPT_FLAGS].i_value |= AFPVOL_NODEV;
495             else if (strcasecmp(p, "illegalseq") == 0)
496                 options[VOLOPT_FLAGS].i_value |= AFPVOL_EILSEQ;
497             else if (strcasecmp(p, "cachecnid") == 0)
498                 options[VOLOPT_FLAGS].i_value |= AFPVOL_CACHE;
499
500             p = strtok(NULL, ",");
501         }
502
503     } else if (optionok(tmp, "dbpath:", val)) {
504         setoption(options, save, VOLOPT_DBPATH, val);
505
506     } else if (optionok(tmp, "umask:", val)) {
507         options[VOLOPT_UMASK].i_value = (int)strtol(val +1, NULL, 8);
508     } else if (optionok(tmp, "dperm:", val)) {
509         options[VOLOPT_DPERM].i_value = (int)strtol(val+1, NULL, 8);
510     } else if (optionok(tmp, "fperm:", val)) {
511         options[VOLOPT_FPERM].i_value = (int)strtol(val+1, NULL, 8);
512     } else if (optionok(tmp, "perm:", val)) {
513         options[VOLOPT_DFLTPERM].i_value = (int)strtol(val+1, NULL, 8);
514     } else if (optionok(tmp, "mapchars:",val)) {
515         setoption(options, save, VOLOPT_MAPCHARS, val);
516
517     } else if (optionok(tmp, "password:", val)) {
518         setoption(options, save, VOLOPT_PASSWORD, val);
519
520 #ifdef FORCE_UIDGID
521
522         /* this code allows forced uid/gid per volume settings */
523     } else if (optionok(tmp, "forceuid:", val)) {
524         setoption(options, save, VOLOPT_FORCEUID, val);
525     } else if (optionok(tmp, "forcegid:", val)) {
526         setoption(options, save, VOLOPT_FORCEGID, val);
527
528 #endif /* FORCE_UIDGID */
529     } else if (optionok(tmp, "root_preexec:", val)) {
530         setoption(options, save, VOLOPT_ROOTPREEXEC, val);
531
532     } else if (optionok(tmp, "preexec:", val)) {
533         setoption(options, save, VOLOPT_PREEXEC, val);
534
535     } else if (optionok(tmp, "root_postexec:", val)) {
536         setoption(options, save, VOLOPT_ROOTPOSTEXEC, val);
537
538     } else if (optionok(tmp, "postexec:", val)) {
539         setoption(options, save, VOLOPT_POSTEXEC, val);
540
541     } else {
542         /* ignore unknown options */
543         LOG(log_debug, logtype_afpd, "ignoring unknown volume option: %s", tmp);
544
545     } 
546 }
547
548 /* ----------------- */
549 static void showvol(const ucs2_t *name)
550 {
551     struct vol  *volume;
552     for ( volume = Volumes; volume; volume = volume->v_next ) {
553       if (volume->v_hide && !strcasecmp_w( (utf8_encoding()?volume->v_u8mname:volume->v_macname), name ) ) {
554             volume->v_hide = 0;
555             return;
556         }
557     }
558 }
559
560 /* ------------------------------- */
561 static int creatvol(AFPObj *obj, struct passwd *pwd, 
562                     char *path, char *name, 
563                     struct vol_option *options, 
564                     const int user /* user defined volume */
565                     )
566 {
567     struct vol  *volume;
568     int         suffixlen, vlen, tmpvlen, u8mvlen, macvlen;
569     int         hide = 0;
570     char        tmpname[AFPVOL_U8MNAMELEN+1];
571     ucs2_t      u8mtmpname[(AFPVOL_U8MNAMELEN+1)*2], mactmpname[(AFPVOL_MACNAMELEN+1)*2];
572     char        suffix[6]; /* max is #FFFF */
573     u_int16_t   flags;
574
575     if ( name == NULL || *name == '\0' ) {
576         if ((name = strrchr( path, '/' )) == NULL) {
577             return -1;  /* Obviously not a fully qualified path */
578         }
579
580         /* if you wish to share /, you need to specify a name. */
581         if (*++name == '\0')
582             return -1;
583     }
584
585     /* suffix for mangling use (lastvid + 1)   */
586     /* because v_vid has not been decided yet. */
587     suffixlen = sprintf(suffix, "%c%X", MANGLE_CHAR, lastvid + 1 );
588
589     vlen = strlen( name );
590
591     /* Unicode Volume Name */
592     /* Firstly convert name from unixcharset to UTF8-MAC */
593     flags = CONV_IGNORE;
594     tmpvlen = convert_charset(obj->options.unixcharset, CH_UTF8_MAC, 0, name, vlen, tmpname, AFPVOL_U8MNAMELEN, &flags);
595     if (tmpvlen <= 0) {
596         strcpy(tmpname, "???");
597         tmpvlen = 3;
598     }
599
600     /* Do we have to mangle ? */
601     if ( (flags & CONV_REQMANGLE) || (tmpvlen > obj->options.volnamelen)) {
602         if (tmpvlen + suffixlen > obj->options.volnamelen) {
603             flags = CONV_FORCE;
604             tmpvlen = convert_charset(obj->options.unixcharset, CH_UTF8_MAC, 0, name, vlen, tmpname, obj->options.volnamelen - suffixlen, &flags);
605             tmpname[tmpvlen != (size_t)-1 ? tmpvlen : 0] = 0;
606         }
607         strcat(tmpname, suffix);
608         tmpvlen = strlen(tmpname);
609     }
610
611     /* Secondly convert name from UTF8-MAC to UCS2 */
612     if ( 0 >= ( u8mvlen = convert_string(CH_UTF8_MAC, CH_UCS2, tmpname, tmpvlen, u8mtmpname, AFPVOL_U8MNAMELEN*2)) )
613         return -1;
614
615     LOG(log_debug, logtype_afpd, "createvol: Volume '%s' -> UTF8-MAC Name: '%s'", name, tmpname);
616
617     /* Maccharset Volume Name */
618     /* Firsty convert name from unixcharset to maccharset */
619     flags = CONV_IGNORE;
620     tmpvlen = convert_charset(obj->options.unixcharset, obj->options.maccharset, 0, name, vlen, tmpname, AFPVOL_U8MNAMELEN, &flags);
621     if (tmpvlen <= 0) {
622         strcpy(tmpname, "???");
623         tmpvlen = 3;
624     }
625
626     /* Do we have to mangle ? */
627     if ( (flags & CONV_REQMANGLE) || (tmpvlen > AFPVOL_MACNAMELEN)) {
628         if (tmpvlen + suffixlen > AFPVOL_MACNAMELEN) {
629             flags = CONV_FORCE;
630             tmpvlen = convert_charset(obj->options.unixcharset, obj->options.maccharset, 0, name, vlen, tmpname, AFPVOL_MACNAMELEN - suffixlen, &flags);
631             tmpname[tmpvlen != (size_t)-1 ? tmpvlen : 0] = 0;
632         }
633         if (*tmpname == 0) {
634             strcat(tmpname, "???");
635         }
636         strcat(tmpname, suffix);
637         tmpvlen = strlen(tmpname);
638     }
639
640     /* Secondly convert name from maccharset to UCS2 */
641     if ( 0 >= ( macvlen = convert_string(obj->options.maccharset, CH_UCS2, tmpname, tmpvlen, mactmpname, AFPVOL_U8MNAMELEN*2)) )
642         return -1;
643
644     LOG(log_debug, logtype_afpd, "createvol: Volume '%s' ->  Longname: '%s'", name, tmpname);
645
646     /* check duplicate */
647     for ( volume = Volumes; volume; volume = volume->v_next ) {
648       if (( strcasecmp_w( volume->v_u8mname, u8mtmpname ) == 0 ) || ( strcasecmp_w( volume->v_macname, mactmpname ) == 0 )){
649            if (volume->v_deleted) {
650                volume->v_new = hide = 1;
651            }
652            else {
653                return -1;       /* Won't be able to access it, anyway... */
654            }
655         }
656     }
657
658     if (!( volume = (struct vol *)calloc(1, sizeof( struct vol ))) ) {
659         LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
660         return -1;
661     }
662     if ( NULL == ( volume->v_localname = strdup(name))) {
663         LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
664         free(volume);
665         return -1;
666     }
667
668     if ( NULL == ( volume->v_u8mname = strdup_w(u8mtmpname))) {
669         LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
670         volume_free(volume);
671         free(volume);
672         return -1;
673     }
674     if ( NULL == ( volume->v_macname = strdup_w(mactmpname))) {
675         LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
676         volume_free(volume);
677         free(volume);
678         return -1;
679     }
680     if (!( volume->v_path = (char *)malloc( strlen( path ) + 1 )) ) {
681         LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
682         volume_free(volume);
683         free(volume);
684         return -1;
685     }
686     volume->v_hide = hide;
687     strcpy( volume->v_path, path );
688
689 #ifdef __svr4__
690     volume->v_qfd = -1;
691 #endif /* __svr4__ */
692     /* os X start at 1 and use network order ie. 1 2 3 */
693     volume->v_vid = ++lastvid;
694     volume->v_vid = htons(volume->v_vid);
695
696     /* handle options */
697     if (options) {
698         /* should we casefold? */
699         volume->v_casefold = options[VOLOPT_CASEFOLD].i_value;
700
701         /* shift in some flags */
702         volume->v_flags = options[VOLOPT_FLAGS].i_value;
703
704         volume->v_ad_options = 0;
705         if ((volume->v_flags & AFPVOL_NODEV))
706             volume->v_ad_options |= ADVOL_NODEV;
707         if ((volume->v_flags & AFPVOL_CACHE))
708             volume->v_ad_options |= ADVOL_CACHE;
709         if ((volume->v_flags & AFPVOL_UNIX_PRIV))
710             volume->v_ad_options |= ADVOL_UNIXPRIV;
711         if ((volume->v_flags & AFPVOL_INV_DOTS))
712             volume->v_ad_options |= ADVOL_INVDOTS;
713
714         if (options[VOLOPT_PASSWORD].c_value)
715             volume->v_password = strdup(options[VOLOPT_PASSWORD].c_value);
716
717         if (options[VOLOPT_VETO].c_value)
718             volume->v_veto = strdup(options[VOLOPT_VETO].c_value);
719
720         if (options[VOLOPT_ENCODING].c_value)
721             volume->v_volcodepage = strdup(options[VOLOPT_ENCODING].c_value);
722
723         if (options[VOLOPT_MACCHARSET].c_value)
724             volume->v_maccodepage = strdup(options[VOLOPT_MACCHARSET].c_value);
725
726         if (options[VOLOPT_DBPATH].c_value)
727             volume->v_dbpath = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_DBPATH].c_value, pwd, path, name);
728
729        if (options[VOLOPT_CNIDSCHEME].c_value)
730            volume->v_cnidscheme = strdup(options[VOLOPT_CNIDSCHEME].c_value);
731
732         if (options[VOLOPT_UMASK].i_value)
733             volume->v_umask = (mode_t)options[VOLOPT_UMASK].i_value;
734
735         if (options[VOLOPT_DPERM].i_value)
736             volume->v_dperm = (mode_t)options[VOLOPT_DPERM].i_value;
737
738         if (options[VOLOPT_FPERM].i_value)
739             volume->v_fperm = (mode_t)options[VOLOPT_FPERM].i_value;
740
741         if (options[VOLOPT_DFLTPERM].i_value)
742             volume->v_perm = (mode_t)options[VOLOPT_DFLTPERM].i_value;
743
744         if (options[VOLOPT_ADOUBLE].i_value)
745             volume->v_adouble = options[VOLOPT_ADOUBLE].i_value;
746         else 
747             volume->v_adouble = AD_VERSION;
748
749         initvol_vfs(volume);
750 #ifdef FORCE_UIDGID
751         if (options[VOLOPT_FORCEUID].c_value) {
752             volume->v_forceuid = strdup(options[VOLOPT_FORCEUID].c_value);
753         } else {
754             volume->v_forceuid = NULL; /* set as null so as to return 0 later on */
755         }
756
757         if (options[VOLOPT_FORCEGID].c_value) {
758             volume->v_forcegid = strdup(options[VOLOPT_FORCEGID].c_value);
759         } else {
760             volume->v_forcegid = NULL; /* set as null so as to return 0 later on */
761         }
762 #endif
763         if (!user) {
764             if (options[VOLOPT_PREEXEC].c_value)
765                 volume->v_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_PREEXEC].c_value, pwd, path, name);
766             volume->v_preexec_close = options[VOLOPT_PREEXEC].i_value;
767
768             if (options[VOLOPT_POSTEXEC].c_value)
769                 volume->v_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_POSTEXEC].c_value, pwd, path, name);
770
771             if (options[VOLOPT_ROOTPREEXEC].c_value)
772                 volume->v_root_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPREEXEC].c_value, pwd, path,  name);
773             volume->v_root_preexec_close = options[VOLOPT_ROOTPREEXEC].i_value;
774
775             if (options[VOLOPT_ROOTPOSTEXEC].c_value)
776                 volume->v_root_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPOSTEXEC].c_value, pwd, path,  name);
777         }
778     }
779     volume->v_dperm |= volume->v_perm;
780     volume->v_fperm |= volume->v_perm;
781
782     volume->v_next = Volumes;
783     Volumes = volume;
784     return 0;
785 }
786
787 /* ---------------- */
788 static char *myfgets( buf, size, fp )
789 char    *buf;
790 int             size;
791 FILE    *fp;
792 {
793     char        *p;
794     int         c;
795
796     p = buf;
797     while ((EOF != ( c = getc( fp )) ) && ( size > 1 )) {
798         if ( c == '\n' || c == '\r' ) {
799             if (p != buf && *(p -1) == '\\') {
800                 p--;
801                 size++;
802                 continue;
803             }
804             *p++ = '\n';
805             break;
806         } else {
807             *p++ = c;
808         }
809         size--;
810     }
811
812     if ( p == buf ) {
813         return( NULL );
814     } else {
815         *p = '\0';
816         return( buf );
817     }
818 }
819
820
821 /* check access list. this function wants something of the following
822  * form:
823  *        @group,name,name2,@group2,name3
824  *
825  * a NULL argument allows everybody to have access.
826  * we return three things:
827  *     -1: no list
828  *      0: list exists, but name isn't in it
829  *      1: in list
830  */
831
832 #ifndef NO_REAL_USER_NAME
833 /* authentication is case insensitive 
834  * FIXME should we do the same with group name?
835 */
836 #define access_strcmp strcasecmp
837
838 #else
839 #define access_strcmp strcmp
840
841 #endif
842
843 static int accessvol(args, name)
844 const char *args;
845 const char *name;
846 {
847     char buf[MAXPATHLEN + 1], *p;
848     struct group *gr;
849
850     if (!args)
851         return -1;
852
853     strlcpy(buf, args, sizeof(buf));
854     if ((p = strtok(buf, ",")) == NULL) /* nothing, return okay */
855         return -1;
856
857     while (p) {
858         if (*p == '@') { /* it's a group */
859             if ((gr = getgrnam(p + 1)) && gmem(gr->gr_gid))
860                 return 1;
861         } else if (access_strcmp(p, name) == 0) /* it's a user name */
862             return 1;
863         p = strtok(NULL, ",");
864     }
865
866     return 0;
867 }
868
869 static void setextmap( ext, type, creator, user)
870 char            *ext, *type, *creator;
871 int                     user;
872 {
873     struct extmap       *em;
874     int                 cnt;
875
876     if (Extmap == NULL) {
877         if (( Extmap = calloc(1, sizeof( struct extmap ))) == NULL ) {
878             LOG(log_error, logtype_afpd, "setextmap: calloc: %s", strerror(errno) );
879             return;
880         }
881     }
882     ext++;
883     for ( em = Extmap, cnt = 0; em->em_ext; em++, cnt++) {
884         if ( (strdiacasecmp( em->em_ext, ext )) == 0 ) {
885             break;
886         }
887     }
888
889     if ( em->em_ext == NULL ) {
890         if (!(Extmap  = realloc( Extmap, sizeof( struct extmap ) * (cnt +2))) ) {
891             LOG(log_error, logtype_afpd, "setextmap: realloc: %s", strerror(errno) );
892             return;
893         }
894         (Extmap +cnt +1)->em_ext = NULL;
895         em = Extmap +cnt;
896     } else if ( !user ) {
897         return;
898     }
899     if (em->em_ext)
900         free(em->em_ext);
901
902     if (!(em->em_ext = strdup(  ext))) {
903         LOG(log_error, logtype_afpd, "setextmap: strdup: %s", strerror(errno) );
904         return;
905     }
906
907     if ( *type == '\0' ) {
908         memcpy(em->em_type, "????", sizeof( em->em_type ));
909     } else {
910         memcpy(em->em_type, type, sizeof( em->em_type ));
911     }
912     if ( *creator == '\0' ) {
913         memcpy(em->em_creator, "UNIX", sizeof( em->em_creator ));
914     } else {
915         memcpy(em->em_creator, creator, sizeof( em->em_creator ));
916     }
917 }
918
919 /* -------------------------- */
920 static int extmap_cmp(const void *map1, const void *map2)
921 {
922     const struct extmap *em1 = map1;
923     const struct extmap *em2 = map2;
924     return strdiacasecmp(em1->em_ext, em2->em_ext);
925 }
926
927 static void sortextmap( void)
928 {
929     struct extmap       *em;
930
931     Extmap_cnt = 0;
932     if ((em = Extmap) == NULL) {
933         return;
934     }
935     while (em->em_ext) {
936         em++;
937         Extmap_cnt++;
938     }
939     if (Extmap_cnt) {
940         qsort(Extmap, Extmap_cnt, sizeof(struct extmap), extmap_cmp);
941         if (*Extmap->em_ext == 0) {
942             /* the first line is really "." the default entry, 
943              * we remove the leading '.' in setextmap
944             */
945             Defextmap = Extmap;
946         }
947     }
948 }
949
950 /* ----------------------
951 */
952 static void free_extmap( void)
953 {
954     struct extmap       *em;
955
956     if (Extmap) {
957         for ( em = Extmap; em->em_ext; em++) {
958              free (em->em_ext);
959         }
960         free(Extmap);
961         Extmap = NULL;
962         Defextmap = Extmap;
963         Extmap_cnt = 0;
964     }
965 }
966
967 /* ----------------------
968 */
969 static int volfile_changed(struct afp_volume_name *p) 
970 {
971     struct stat      st;
972     char *name;
973     
974     if (p->full_name) 
975         name = p->full_name;
976     else
977         name = p->name;
978         
979     if (!stat( name, &st) && st.st_mtime > p->mtime) {
980         p->mtime = st.st_mtime;
981         return 1;
982     }
983     return 0;
984 }
985
986 /* ----------------------
987  * Read a volume configuration file and add the volumes contained within to
988  * the global volume list.  If p2 is non-NULL, the file that is opened is
989  * p1/p2
990  * 
991  * Lines that begin with # and blank lines are ignored.
992  * Volume lines are of the form:
993  *              <unix path> [<volume name>] [allow:<user>,<@group>,...] \
994  *                           [codepage:<file>] [casefold:<num>]
995  *              <extension> TYPE [CREATOR]
996  */
997 static int readvolfile(obj, p1, p2, user, pwent)
998 AFPObj      *obj;
999 struct afp_volume_name  *p1;
1000 char        *p2;
1001 int             user;
1002 struct passwd *pwent;
1003 {
1004     FILE                *fp;
1005     char                path[ MAXPATHLEN + 1], tmp[ MAXPATHLEN + 1],
1006     volname[ AFPVOL_U8MNAMELEN + 1 ], buf[ BUFSIZ ],
1007     type[ 5 ], creator[ 5 ];
1008     char                *u, *p;
1009     struct passwd       *pw;
1010     struct vol_option   options[VOLOPT_NUM], save_options[VOLOPT_NUM];
1011     int                 i;
1012     struct stat         st;
1013     int                 fd;
1014
1015     if (!p1->name)
1016         return -1;
1017     p1->mtime = 0;
1018     strcpy( path, p1->name );
1019     if ( p2 != NULL ) {
1020         strcat( path, "/" );
1021         strcat( path, p2 );
1022         if (p1->full_name) {
1023             free(p1->full_name);
1024         }
1025         p1->full_name = strdup(path);
1026     }
1027
1028     if (NULL == ( fp = fopen( path, "r" )) ) {
1029         return( -1 );
1030     }
1031     fd = fileno(fp);
1032     if (fd != -1 && !fstat( fd, &st) ) {
1033         p1->mtime = st.st_mtime;
1034     }
1035
1036     memset(save_options, 0, sizeof(save_options));
1037     while ( myfgets( buf, sizeof( buf ), fp ) != NULL ) {
1038         initline( strlen( buf ), buf );
1039         parseline( sizeof( path ) - 1, path );
1040         switch ( *path ) {
1041         case '\0' :
1042         case '#' :
1043             continue;
1044
1045         case ':':
1046             /* change the default options for this file */
1047             if (strncmp(path, VOLOPT_DEFAULT, VOLOPT_DEFAULT_LEN) == 0) {
1048                 *tmp = '\0';
1049                 for (i = 0; i < VOLOPT_NUM; i++) {
1050                     if (parseline( sizeof( path ) - VOLOPT_DEFAULT_LEN - 1,
1051                                    path + VOLOPT_DEFAULT_LEN) < 0)
1052                         break;
1053                     volset(save_options, NULL, tmp, sizeof(tmp) - 1,
1054                            path + VOLOPT_DEFAULT_LEN);
1055                 }
1056             }
1057             break;
1058
1059         case '~' :
1060             if (( p = strchr( path, '/' )) != NULL ) {
1061                 *p++ = '\0';
1062             }
1063             u = path;
1064             u++;
1065             if ( *u == '\0' ) {
1066                 u = obj->username;
1067             }
1068             if ( u == NULL || *u == '\0' || ( pw = getpwnam( u )) == NULL ) {
1069                 continue;
1070             }
1071             strcpy( tmp, pw->pw_dir );
1072             if ( p != NULL && *p != '\0' ) {
1073                 strcat( tmp, "/" );
1074                 strcat( tmp, p );
1075             }
1076             /* Tag a user's home directory with their umask.  Note, this will
1077              * be overwritten if the user actually specifies a umask: option
1078              * for a '~' volume. */
1079             save_options[VOLOPT_UMASK].i_value = obj->options.save_mask;
1080             /* fall through */
1081
1082         case '/' :
1083             /* send path through variable substitution */
1084             if (*path != '~') /* need to copy path to tmp */
1085                 strcpy(tmp, path);
1086             if (!pwent)
1087                 pwent = getpwnam(obj->username);
1088             volxlate(obj, path, sizeof(path) - 1, tmp, pwent, NULL, NULL);
1089
1090             /* this is sort of braindead. basically, i want to be
1091              * able to specify things in any order, but i don't want to 
1092              * re-write everything. 
1093              *
1094              * currently we have options: 
1095              *   volname
1096              *   codepage:x
1097              *   casefold:x
1098              *   allow:x,y,@z
1099              *   deny:x,y,@z
1100              *   rwlist:x,y,@z
1101              *   rolist:x,y,@z
1102              *   options:prodos,crlf,noadouble,ro...
1103              *   dbpath:x
1104              *   password:x
1105              *   preexec:x
1106              *
1107              *   namemask:x,y,!z  (not implemented yet)
1108              */
1109             memcpy(options, save_options, sizeof(options));
1110             *volname = '\0';
1111
1112             /* read in up to VOLOP_NUM possible options */
1113             for (i = 0; i < VOLOPT_NUM; i++) {
1114                 if (parseline( sizeof( tmp ) - 1, tmp ) < 0)
1115                     break;
1116
1117                 volset(options, save_options, volname, sizeof(volname) - 1, tmp);
1118             }
1119
1120             /* check allow/deny lists:
1121                allow -> either no list (-1), or in list (1)
1122                deny -> either no list (-1), or not in list (0) */
1123             if (accessvol(options[VOLOPT_ALLOW].c_value, obj->username) &&
1124                     (accessvol(options[VOLOPT_DENY].c_value, obj->username) < 1)) {
1125
1126                 /* handle read-only behaviour. semantics:
1127                  * 1) neither the rolist nor the rwlist exist -> rw
1128                  * 2) rolist exists -> ro if user is in it.
1129                  * 3) rwlist exists -> ro unless user is in it. */
1130                 if (((options[VOLOPT_FLAGS].i_value & AFPVOL_RO) == 0) &&
1131                         ((accessvol(options[VOLOPT_ROLIST].c_value,
1132                                     obj->username) == 1) ||
1133                          !accessvol(options[VOLOPT_RWLIST].c_value,
1134                                     obj->username)))
1135                     options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
1136
1137                 /* do variable substitution for volname */
1138                 volxlate(obj, tmp, sizeof(tmp) - 1, volname, pwent, path, NULL);
1139                 creatvol(obj, pwent, path, tmp, options, p2 != NULL);
1140             }
1141             volfree(options, save_options);
1142             break;
1143
1144         case '.' :
1145             parseline( sizeof( type ) - 1, type );
1146             parseline( sizeof( creator ) - 1, creator );
1147             setextmap( path, type, creator, user);
1148             break;
1149
1150         default :
1151             break;
1152         }
1153     }
1154     volfree(save_options, NULL);
1155     sortextmap();
1156     if ( fclose( fp ) != 0 ) {
1157         LOG(log_error, logtype_afpd, "readvolfile: fclose: %s", strerror(errno) );
1158     }
1159     p1->loaded = 1;
1160     return( 0 );
1161 }
1162
1163 /* ------------------------------- */
1164 static void volume_free(struct vol *vol)
1165 {
1166     free(vol->v_localname);
1167     vol->v_localname = NULL;
1168     free(vol->v_u8mname);
1169     vol->v_u8mname = NULL;
1170     free(vol->v_macname);
1171     vol->v_macname = NULL;
1172     free(vol->v_path);
1173     free(vol->v_password);
1174     free(vol->v_veto);
1175     free(vol->v_volcodepage);
1176     free(vol->v_maccodepage);
1177     free(vol->v_cnidscheme);
1178     free(vol->v_dbpath);
1179     free(vol->v_gvs);
1180 #ifdef FORCE_UIDGID
1181     free(vol->v_forceuid);
1182     free(vol->v_forcegid);
1183 #endif /* FORCE_UIDGID */
1184 }
1185
1186 /* ------------------------------- */
1187 static void free_volumes(void )
1188 {
1189     struct vol  *vol;
1190     struct vol  *nvol, *ovol;
1191
1192     for ( vol = Volumes; vol; vol = vol->v_next ) {
1193         if (( vol->v_flags & AFPVOL_OPEN ) ) {
1194             vol->v_deleted = 1;
1195             continue;
1196         }
1197         volume_free(vol);
1198     }
1199
1200     for ( vol = Volumes, ovol = NULL; vol; vol = nvol) {
1201         nvol = vol->v_next;
1202
1203         if (vol->v_localname == NULL) {
1204            if (Volumes == vol) {
1205                Volumes = nvol;
1206                ovol = Volumes;
1207            }
1208            else {
1209               ovol->v_next = nvol;
1210            }
1211            free(vol);
1212         }
1213         else {
1214            ovol = vol;
1215         }
1216     }
1217 }
1218
1219 /* ------------------------------- */
1220 static void volume_unlink(struct vol *volume)
1221 {
1222 struct vol *vol, *ovol, *nvol;
1223
1224     if (volume == Volumes) {
1225         Volumes = Volumes->v_next;
1226         return;
1227     }
1228     for ( vol = Volumes->v_next, ovol = Volumes; vol; vol = nvol) {
1229         nvol = vol->v_next;
1230
1231         if (vol == volume) {
1232             ovol->v_next = nvol;
1233             break;
1234         }
1235         else {
1236            ovol = vol;
1237         }
1238     }
1239 }
1240
1241 static int getvolspace( vol, bfree, btotal, xbfree, xbtotal, bsize )
1242 struct vol      *vol;
1243 u_int32_t       *bfree, *btotal, *bsize;
1244 VolSpace    *xbfree, *xbtotal;
1245 {
1246     int         spaceflag, rc;
1247     u_int32_t   maxsize;
1248 #ifndef NO_QUOTA_SUPPORT
1249     VolSpace    qfree, qtotal;
1250 #endif
1251
1252     spaceflag = AFPVOL_GVSMASK & vol->v_flags;
1253     /* report up to 2GB if afp version is < 2.2 (4GB if not) */
1254     maxsize = (vol->v_flags & AFPVOL_A2VOL) ? 0x01fffe00 :
1255               (((afp_version < 22) || (vol->v_flags & AFPVOL_LIMITSIZE))
1256                ? 0x7fffffffL : 0xffffffffL);
1257
1258 #ifdef AFS
1259     if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_AFSGVS ) {
1260         if ( afs_getvolspace( vol, xbfree, xbtotal, bsize ) == AFP_OK ) {
1261             vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_AFSGVS;
1262             goto getvolspace_done;
1263         }
1264     }
1265 #endif
1266
1267     if (( rc = ustatfs_getvolspace( vol, xbfree, xbtotal,
1268                                     bsize)) != AFP_OK ) {
1269         return( rc );
1270     }
1271
1272 #define min(a,b)        ((a)<(b)?(a):(b))
1273 #ifndef NO_QUOTA_SUPPORT
1274     if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_UQUOTA ) {
1275         if ( uquota_getvolspace( vol, &qfree, &qtotal, *bsize ) == AFP_OK ) {
1276             vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_UQUOTA;
1277             *xbfree = min(*xbfree, qfree);
1278             *xbtotal = min( *xbtotal, qtotal);
1279             goto getvolspace_done;
1280         }
1281     }
1282 #endif
1283     vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_USTATFS;
1284
1285 getvolspace_done:
1286     *bfree = min( *xbfree, maxsize);
1287     *btotal = min( *xbtotal, maxsize);
1288     return( AFP_OK );
1289 }
1290
1291 /* ----------------------- 
1292  * set volume creation date
1293  * avoid duplicate, well at least it tries
1294 */
1295 static void vol_setdate(u_int16_t id, struct adouble *adp, time_t date)
1296 {
1297     struct vol  *volume;
1298     struct vol  *vol = Volumes;
1299
1300     for ( volume = Volumes; volume; volume = volume->v_next ) {
1301         if (volume->v_vid == id) {
1302             vol = volume;
1303         }
1304         else if ((time_t)(AD_DATE_FROM_UNIX(date)) == volume->v_ctime) {
1305             date = date+1;
1306             volume = Volumes; /* restart */
1307         }
1308     }
1309     vol->v_ctime = AD_DATE_FROM_UNIX(date);
1310     ad_setdate(adp, AD_DATE_CREATE | AD_DATE_UNIX, date);
1311 }
1312
1313 /* ----------------------- */
1314 static int getvolparams( bitmap, vol, st, buf, buflen )
1315 u_int16_t       bitmap;
1316 struct vol      *vol;
1317 struct stat     *st;
1318 char    *buf;
1319 int             *buflen;
1320 {
1321     struct adouble      ad;
1322     int                 bit = 0, isad = 1;
1323     u_int32_t           aint;
1324     u_short             ashort;
1325     u_int32_t           bfree, btotal, bsize;
1326     VolSpace            xbfree, xbtotal; /* extended bytes */
1327     char                *data, *nameoff = NULL;
1328     char                *slash;
1329
1330     LOG(log_debug, logtype_afpd, "getvolparams: Volume '%s'", vol->v_localname);
1331
1332     /* courtesy of jallison@whistle.com:
1333      * For MacOS8.x support we need to create the
1334      * .Parent file here if it doesn't exist. */
1335
1336     ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1337     if ( ad_open_metadata( vol->v_path, vol_noadouble(vol) | ADFLAGS_DIR, O_CREAT, &ad) < 0 ) {
1338         isad = 0;
1339         vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1340
1341     } else if (ad_get_MD_flags( &ad ) & O_CREAT) {
1342         slash = strrchr( vol->v_path, '/' );
1343         if(slash)
1344             slash++;
1345         else
1346             slash = vol->v_path;
1347         if (ad_getentryoff(&ad, ADEID_NAME)) {
1348             ad_setentrylen( &ad, ADEID_NAME, strlen( slash ));
1349             memcpy(ad_entry( &ad, ADEID_NAME ), slash,
1350                ad_getentrylen( &ad, ADEID_NAME ));
1351         }
1352         vol_setdate(vol->v_vid, &ad, st->st_mtime);
1353         ad_flush(&ad);
1354     }
1355     else {
1356         if (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0)
1357             vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1358         else 
1359             vol->v_ctime = aint;
1360     }
1361
1362     if (( bitmap & ( (1<<VOLPBIT_BFREE)|(1<<VOLPBIT_BTOTAL) |
1363                      (1<<VOLPBIT_XBFREE)|(1<<VOLPBIT_XBTOTAL) |
1364                      (1<<VOLPBIT_BSIZE)) ) != 0 ) {
1365         if ( getvolspace( vol, &bfree, &btotal, &xbfree, &xbtotal,
1366                           &bsize) < 0 ) {
1367             if ( isad ) {
1368                 ad_close( &ad, ADFLAGS_HF );
1369             }
1370             return( AFPERR_PARAM );
1371         }
1372     }
1373
1374     data = buf;
1375     while ( bitmap != 0 ) {
1376         while (( bitmap & 1 ) == 0 ) {
1377             bitmap = bitmap>>1;
1378             bit++;
1379         }
1380
1381         switch ( bit ) {
1382         case VOLPBIT_ATTR :
1383             ashort = 0;
1384             if (0 == (vol->v_flags & AFPVOL_NOFILEID) && vol->v_cdb != NULL &&
1385                            (vol->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1386                 ashort = VOLPBIT_ATTR_FILEID;
1387             }
1388             /* check for read-only.
1389              * NOTE: we don't actually set the read-only flag unless
1390              *       it's passed in that way as it's possible to mount
1391              *       a read-write filesystem under a read-only one. */
1392             if ((vol->v_flags & AFPVOL_RO) ||
1393                     ((utime(vol->v_path, NULL) < 0) && (errno == EROFS))) {
1394                 ashort |= VOLPBIT_ATTR_RO;
1395             }
1396             ashort |= VOLPBIT_ATTR_CATSEARCH;
1397             if (afp_version >= 30) {
1398                 ashort |= VOLPBIT_ATTR_UTF8;
1399                 if (vol->v_flags & AFPVOL_UNIX_PRIV)
1400                     ashort |= VOLPBIT_ATTR_UNIXPRIV;
1401             }
1402             if (afp_version >= 32) {
1403                 if (vol->v_flags & AFPVOL_EXT_ATTRS)
1404                     ashort |= VOLPBIT_ATTR_EXT_ATTRS;
1405                 if (vol->v_flags & AFPVOL_ACLS)
1406                     ashort |= VOLPBIT_ATTR_ACLS;
1407             }
1408             ashort = htons(ashort);
1409             memcpy(data, &ashort, sizeof( ashort ));
1410             data += sizeof( ashort );
1411             break;
1412
1413         case VOLPBIT_SIG :
1414             ashort = htons( AFPVOLSIG_DEFAULT );
1415             memcpy(data, &ashort, sizeof( ashort ));
1416             data += sizeof( ashort );
1417             break;
1418
1419         case VOLPBIT_CDATE :
1420             aint = vol->v_ctime;
1421             memcpy(data, &aint, sizeof( aint ));
1422             data += sizeof( aint );
1423             break;
1424
1425         case VOLPBIT_MDATE :
1426             if ( st->st_mtime > vol->v_mtime ) {
1427                 vol->v_mtime = st->st_mtime;
1428             }
1429             aint = AD_DATE_FROM_UNIX(vol->v_mtime);
1430             memcpy(data, &aint, sizeof( aint ));
1431             data += sizeof( aint );
1432             break;
1433
1434         case VOLPBIT_BDATE :
1435             if (!isad ||  (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1436                 aint = AD_DATE_START;
1437             memcpy(data, &aint, sizeof( aint ));
1438             data += sizeof( aint );
1439             break;
1440
1441         case VOLPBIT_VID :
1442             memcpy(data, &vol->v_vid, sizeof( vol->v_vid ));
1443             data += sizeof( vol->v_vid );
1444             break;
1445
1446         case VOLPBIT_BFREE :
1447             bfree = htonl( bfree );
1448             memcpy(data, &bfree, sizeof( bfree ));
1449             data += sizeof( bfree );
1450             break;
1451
1452         case VOLPBIT_BTOTAL :
1453             btotal = htonl( btotal );
1454             memcpy(data, &btotal, sizeof( btotal ));
1455             data += sizeof( btotal );
1456             break;
1457
1458 #ifndef NO_LARGE_VOL_SUPPORT
1459         case VOLPBIT_XBFREE :
1460             xbfree = hton64( xbfree );
1461 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1462             bcopy(&xbfree, data, sizeof(xbfree));
1463 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1464             memcpy(data, &xbfree, sizeof( xbfree ));
1465 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1466             data += sizeof( xbfree );
1467             break;
1468
1469         case VOLPBIT_XBTOTAL :
1470             xbtotal = hton64( xbtotal );
1471 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1472             bcopy(&xbtotal, data, sizeof(xbtotal));
1473 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1474             memcpy(data, &xbtotal, sizeof( xbtotal ));
1475 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1476             data += sizeof( xbfree );
1477             break;
1478 #endif /* ! NO_LARGE_VOL_SUPPORT */
1479
1480         case VOLPBIT_NAME :
1481             nameoff = data;
1482             data += sizeof( u_int16_t );
1483             break;
1484
1485         case VOLPBIT_BSIZE:  /* block size */
1486             bsize = htonl(bsize);
1487             memcpy(data, &bsize, sizeof(bsize));
1488             data += sizeof(bsize);
1489             break;
1490
1491         default :
1492             if ( isad ) {
1493                 ad_close( &ad, ADFLAGS_HF );
1494             }
1495             return( AFPERR_BITMAP );
1496         }
1497         bitmap = bitmap>>1;
1498         bit++;
1499     }
1500     if ( nameoff ) {
1501         ashort = htons( data - buf );
1502         memcpy(nameoff, &ashort, sizeof( ashort ));
1503         /* name is always in mac charset */
1504         aint = ucs2_to_charset( vol->v_maccharset, vol->v_macname, data+1, AFPVOL_MACNAMELEN + 1);
1505         if ( aint <= 0 ) {
1506             *buflen = 0;
1507             return AFPERR_MISC;
1508         }
1509                 
1510         *data++ = aint;
1511         data += aint;
1512     }
1513     if ( isad ) {
1514         ad_close_metadata( &ad);
1515     }
1516     *buflen = data - buf;
1517     return( AFP_OK );
1518 }
1519
1520 /* ------------------------- */
1521 static int stat_vol(u_int16_t bitmap, struct vol *vol, char *rbuf, int *rbuflen)
1522 {
1523     struct stat st;
1524     int         buflen, ret;
1525
1526     if ( stat( vol->v_path, &st ) < 0 ) {
1527         *rbuflen = 0;
1528         return( AFPERR_PARAM );
1529     }
1530     /* save the volume device number */
1531     vol->v_dev = st.st_dev;
1532
1533     buflen = *rbuflen - sizeof( bitmap );
1534     if (( ret = getvolparams( bitmap, vol, &st,
1535                               rbuf + sizeof( bitmap ), &buflen )) != AFP_OK ) {
1536         *rbuflen = 0;
1537         return( ret );
1538     }
1539     *rbuflen = buflen + sizeof( bitmap );
1540     bitmap = htons( bitmap );
1541     memcpy(rbuf, &bitmap, sizeof( bitmap ));
1542     return( AFP_OK );
1543
1544 }
1545
1546 /* ------------------------------- */
1547 void load_volumes(AFPObj *obj)
1548 {
1549     struct passwd       *pwent;
1550
1551     if (Volumes) {
1552         int changed = 0;
1553         
1554         /* check files date */
1555         if (obj->options.defaultvol.loaded) {
1556             changed = volfile_changed(&obj->options.defaultvol);
1557         }
1558         if (obj->options.systemvol.loaded) {
1559             changed |= volfile_changed(&obj->options.systemvol);
1560         }
1561         if (obj->options.uservol.loaded) {
1562             changed |= volfile_changed(&obj->options.uservol);
1563         }
1564         if (!changed)
1565             return;
1566         
1567         free_extmap();
1568         free_volumes();
1569     }
1570     
1571     pwent = getpwnam(obj->username);
1572     if ( (obj->options.flags & OPTION_USERVOLFIRST) == 0 ) {
1573         readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent);
1574     }
1575
1576     if ((*obj->username == '\0') || (obj->options.flags & OPTION_NOUSERVOL)) {
1577         readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent);
1578     } else if (pwent) {
1579         /*
1580         * Read user's AppleVolumes or .AppleVolumes file
1581         * If neither are readable, read the default volumes file. if
1582         * that doesn't work, create a user share.
1583         */
1584         if (obj->options.uservol.name) {
1585             free(obj->options.uservol.name);
1586         }
1587         obj->options.uservol.name = strdup(pwent->pw_dir);
1588         if ( readvolfile(obj, &obj->options.uservol,    "AppleVolumes", 1, pwent) < 0 &&
1589                 readvolfile(obj, &obj->options.uservol, ".AppleVolumes", 1, pwent) < 0 &&
1590                 readvolfile(obj, &obj->options.uservol, "applevolumes", 1, pwent) < 0 &&
1591                 readvolfile(obj, &obj->options.uservol, ".applevolumes", 1, pwent) < 0 &&
1592                 obj->options.defaultvol.name != NULL ) {
1593             if (readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent) < 0)
1594                 creatvol(obj, pwent, pwent->pw_dir, NULL, NULL, 1);
1595         }
1596     }
1597     if ( obj->options.flags & OPTION_USERVOLFIRST ) {
1598         readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent );
1599     }
1600     
1601     if ( obj->options.closevol ) {
1602         struct vol *vol;
1603         
1604         for ( vol = Volumes; vol; vol = vol->v_next ) {
1605             if (vol->v_deleted && !vol->v_new ) {
1606                 of_closevol(vol);
1607                 deletevol(vol);
1608                 vol = Volumes;
1609             }
1610         }
1611     }
1612 }
1613
1614 /* ------------------------------- */
1615 int afp_getsrvrparms(obj, ibuf, ibuflen, rbuf, rbuflen )
1616 AFPObj  *obj;
1617 char    *ibuf _U_, *rbuf;
1618 int     ibuflen _U_, *rbuflen;
1619 {
1620     struct timeval      tv;
1621     struct stat         st;
1622     struct vol          *volume;
1623     char        *data;
1624     char                *namebuf;
1625     int                 vcnt;
1626     size_t              len;
1627
1628     load_volumes(obj);
1629
1630     data = rbuf + 5;
1631     for ( vcnt = 0, volume = Volumes; volume; volume = volume->v_next ) {
1632         if (!(volume->v_flags & AFPVOL_NOSTAT)) {
1633             struct maccess ma;
1634
1635             if ( stat( volume->v_path, &st ) < 0 ) {
1636                 LOG(log_info, logtype_afpd, "afp_getsrvrparms(%s): stat: %s",
1637                         volume->v_path, strerror(errno) );
1638                 continue;               /* can't access directory */
1639             }
1640             if (!S_ISDIR(st.st_mode)) {
1641                 continue;               /* not a dir */
1642             }
1643             accessmode(volume->v_path, &ma, NULL, &st);
1644             if ((ma.ma_user & (AR_UREAD | AR_USEARCH)) != (AR_UREAD | AR_USEARCH)) {
1645                 continue;   /* no r-x access */
1646             }
1647         }
1648         if (volume->v_hide) {
1649             continue;           /* config file changed but the volume was mounted */
1650         }
1651
1652         if (utf8_encoding()) {
1653             len = ucs2_to_charset_allocate(CH_UTF8_MAC, &namebuf, volume->v_u8mname);
1654         } else {
1655             len = ucs2_to_charset_allocate(obj->options.maccharset, &namebuf, volume->v_macname);
1656         }
1657
1658         if (len == (size_t)-1)
1659                 continue;
1660
1661         /* set password bit if there's a volume password */
1662         *data = (volume->v_password) ? AFPSRVR_PASSWD : 0;
1663
1664         /* Apple 2 clients running ProDOS-8 expect one volume to have
1665            bit 0 of this byte set.  They will not recognize anything
1666            on the server unless this is the case.  I have not
1667            completely worked this out, but it's related to booting
1668            from the server.  Support for that function is a ways
1669            off.. <shirsch@ibm.net> */
1670         *data |= (volume->v_flags & AFPVOL_A2VOL) ? AFPSRVR_CONFIGINFO : 0;
1671         *data++ |= 0; /* UNIX PRIVS BIT ..., OSX doesn't seem to use it, so we don't either */
1672         *data++ = len;
1673         memcpy(data, namebuf, len );
1674         data += len;
1675         free(namebuf);
1676         vcnt++;
1677     }
1678
1679     *rbuflen = data - rbuf;
1680     data = rbuf;
1681     if ( gettimeofday( &tv, 0 ) < 0 ) {
1682         LOG(log_error, logtype_afpd, "afp_getsrvrparms(%s): gettimeofday: %s", volume->v_path, strerror(errno) );
1683         *rbuflen = 0;
1684         return AFPERR_PARAM;
1685     }
1686     tv.tv_sec = AD_DATE_FROM_UNIX(tv.tv_sec);
1687     memcpy(data, &tv.tv_sec, sizeof( u_int32_t));
1688     data += sizeof( u_int32_t);
1689     *data = vcnt;
1690     return( AFP_OK );
1691 }
1692
1693 /* ------------------------- 
1694  * we are the user here
1695 */
1696 int afp_openvol(obj, ibuf, ibuflen, rbuf, rbuflen )
1697 AFPObj  *obj;
1698 char    *ibuf, *rbuf;
1699 int     ibuflen _U_, *rbuflen;
1700 {
1701     struct stat st;
1702     char        *volname;
1703     char        *p;
1704     struct vol  *volume;
1705     struct dir  *dir;
1706     int         len, ret;
1707     size_t      namelen;
1708     u_int16_t   bitmap;
1709     char        path[ MAXPATHLEN + 1];
1710     char        *vol_uname;
1711     char        *vol_mname;
1712     char        *volname_tmp;
1713
1714     ibuf += 2;
1715     memcpy(&bitmap, ibuf, sizeof( bitmap ));
1716     bitmap = ntohs( bitmap );
1717     ibuf += sizeof( bitmap );
1718     if (( bitmap & (1<<VOLPBIT_VID)) == 0 ) {
1719         *rbuflen = 0;
1720         return AFPERR_BITMAP;
1721     }
1722
1723     len = (unsigned char)*ibuf++;
1724     volname = obj->oldtmp;
1725
1726     if ((volname_tmp = strchr(volname,'+')) != NULL)
1727         volname = volname_tmp+1;
1728
1729     if (utf8_encoding()) {
1730       namelen = convert_string(CH_UTF8_MAC, CH_UCS2, ibuf, len, volname, sizeof(obj->oldtmp));
1731     } else {
1732       namelen = convert_string(obj->options.maccharset, CH_UCS2, ibuf, len, volname, sizeof(obj->oldtmp));
1733     }
1734
1735     if ( namelen <= 0){
1736         *rbuflen = 0;
1737         return AFPERR_PARAM;
1738     }
1739
1740     ibuf += len;
1741     if ((len + 1) & 1) /* pad to an even boundary */
1742         ibuf++;
1743
1744     load_volumes(obj);
1745
1746     for ( volume = Volumes; volume; volume = volume->v_next ) {
1747         if ( strcasecmp_w( (ucs2_t*) volname, (utf8_encoding()?volume->v_u8mname:volume->v_macname) ) == 0 ) {
1748             break;
1749         }
1750     }
1751
1752     if ( volume == NULL ) {
1753         *rbuflen = 0;
1754         return AFPERR_PARAM;
1755     }
1756
1757     /* check for a volume password */
1758     if (volume->v_password && strncmp(ibuf, volume->v_password, VOLPASSLEN)) {
1759         *rbuflen = 0;
1760         return AFPERR_ACCESS;
1761     }
1762
1763     if (( volume->v_flags & AFPVOL_OPEN  ) ) {
1764         /* the volume is already open */
1765 #ifdef FORCE_UIDGID
1766         set_uidgid ( volume );
1767 #endif
1768         return stat_vol(bitmap, volume, rbuf, rbuflen);
1769     }
1770
1771     /* initialize volume variables
1772      * FIXME file size
1773     */
1774     if (utf8_encoding()) {
1775         volume->max_filename = 255;
1776     }
1777     else {
1778         volume->max_filename = MACFILELEN;
1779     }
1780
1781     volume->v_dir = volume->v_root = NULL;
1782     volume->v_hash = NULL;
1783
1784     volume->v_flags |= AFPVOL_OPEN;
1785     volume->v_cdb = NULL;  
1786
1787     if (volume->v_root_preexec) {
1788         if ((ret = afprun(1, volume->v_root_preexec, NULL)) && volume->v_root_preexec_close) {
1789             LOG(log_error, logtype_afpd, "afp_openvol(%s): root preexec : %d", volume->v_path, ret );
1790             ret = AFPERR_MISC;
1791             goto openvol_err;
1792         }
1793     }
1794
1795 #ifdef FORCE_UIDGID
1796     set_uidgid ( volume );
1797 #endif
1798
1799     if (volume->v_preexec) {
1800         if ((ret = afprun(0, volume->v_preexec, NULL)) && volume->v_preexec_close) {
1801             LOG(log_error, logtype_afpd, "afp_openvol(%s): preexec : %d", volume->v_path, ret );
1802             ret = AFPERR_MISC;
1803             goto openvol_err;
1804         }
1805     }
1806
1807     if ( stat( volume->v_path, &st ) < 0 ) {
1808         ret = AFPERR_PARAM;
1809         goto openvol_err;
1810     }
1811
1812     if ( chdir( volume->v_path ) < 0 ) {
1813         ret = AFPERR_PARAM;
1814         goto openvol_err;
1815     }
1816
1817     if (utf8_encoding()) {
1818         len = convert_string_allocate(CH_UCS2, CH_UTF8_MAC, volume->v_u8mname, namelen, &vol_mname);
1819     } else {
1820         len = convert_string_allocate(CH_UCS2, obj->options.maccharset, volume->v_macname, namelen, &vol_mname);
1821     }
1822     if ( !vol_mname || len <= 0) {
1823         ret = AFPERR_MISC;
1824         goto openvol_err;
1825     }
1826     
1827     if ( NULL == getcwd(path, MAXPATHLEN)) {
1828         /* shouldn't be fatal but it will fail later */
1829         LOG(log_error, logtype_afpd, "afp_openvol(%s): volume pathlen too long", volume->v_path);
1830         ret = AFPERR_MISC;
1831         goto openvol_err;
1832     }        
1833     
1834     if ((vol_uname = strrchr(path, '/')) == NULL)
1835          vol_uname = path;
1836     else if (*(vol_uname + 1) != '\0')
1837          vol_uname++;
1838         
1839     if ((dir = dirnew(vol_mname, vol_uname) ) == NULL) {
1840         free(vol_mname);
1841         LOG(log_error, logtype_afpd, "afp_openvol(%s): malloc: %s", volume->v_path, strerror(errno) );
1842         ret = AFPERR_MISC;
1843         goto openvol_err;
1844     }
1845     free(vol_mname);
1846
1847     dir->d_did = DIRDID_ROOT;
1848     dir->d_color = DIRTREE_COLOR_BLACK; /* root node is black */
1849     volume->v_dir = volume->v_root = dir;
1850     volume->v_hash = dirhash();
1851
1852     curdir = volume->v_dir;
1853     if (volume->v_cnidscheme == NULL) {
1854         volume->v_cnidscheme = strdup(DEFAULT_CNID_SCHEME);
1855         LOG(log_warning, logtype_afpd, "Warning: No CNID scheme for volume %s. Using default.",
1856                volume->v_path);
1857     }
1858     if (volume->v_dbpath)
1859         volume->v_cdb = cnid_open (volume->v_dbpath, volume->v_umask, volume->v_cnidscheme, (volume->v_flags & AFPVOL_NODEV));
1860     else
1861         volume->v_cdb = cnid_open (volume->v_path, volume->v_umask, volume->v_cnidscheme, (volume->v_flags & AFPVOL_NODEV));
1862     if (volume->v_cdb == NULL) {
1863         LOG(log_error, logtype_afpd, "Fatal error: cannot open CNID or invalid CNID backend for %s: %s", 
1864             volume->v_path, volume->v_cnidscheme);
1865         ret = AFPERR_MISC;
1866         goto openvol_err;
1867     }
1868
1869     /* Codepages */
1870
1871     if (!volume->v_volcodepage)
1872         volume->v_volcodepage = strdup("UTF8");
1873
1874     if ( (charset_t) -1 == ( volume->v_volcharset = add_charset(volume->v_volcodepage)) ) {
1875         LOG (log_error, logtype_afpd, "Setting codepage %s as volume codepage failed", volume->v_volcodepage);
1876         ret = AFPERR_MISC;
1877         goto openvol_err;
1878     }
1879
1880     if ( NULL == ( volume->v_vol = find_charset_functions(volume->v_volcodepage)) || volume->v_vol->flags & CHARSET_ICONV ) {
1881         LOG (log_warning, logtype_afpd, "WARNING: volume encoding %s is *not* supported by netatalk, expect problems !!!!", volume->v_volcodepage);
1882     }   
1883
1884     if (!volume->v_maccodepage)
1885         volume->v_maccodepage = strdup(obj->options.maccodepage);
1886
1887     if ( (charset_t) -1 == ( volume->v_maccharset = add_charset(volume->v_maccodepage)) ) {
1888         LOG (log_error, logtype_afpd, "Setting codepage %s as mac codepage failed", volume->v_maccodepage);
1889         ret = AFPERR_MISC;
1890         goto openvol_err;
1891     }
1892
1893     if ( NULL == ( volume->v_mac = find_charset_functions(volume->v_maccodepage)) || ! (volume->v_mac->flags & CHARSET_CLIENT) ) {
1894         LOG (log_error, logtype_afpd, "Fatal error: mac charset %s not supported", volume->v_maccodepage);
1895         ret = AFPERR_MISC;
1896         goto openvol_err;
1897     }   
1898
1899     ret  = stat_vol(bitmap, volume, rbuf, rbuflen);
1900     if (ret == AFP_OK) {
1901
1902         if (!(volume->v_flags & AFPVOL_RO)) {
1903             handle_special_folders( volume );
1904             savevoloptions( volume);
1905         }
1906
1907         /*
1908          * If you mount a volume twice, the second time the trash appears on
1909          * the desk-top.  That's because the Mac remembers the DID for the
1910          * trash (even for volumes in different zones, on different servers).
1911          * Just so this works better, we prime the DID cache with the trash,
1912          * fixing the trash at DID 17.
1913          * FIXME (RL): should it be done inside a CNID backend ? (always returning Trash DID when asked) ?
1914          */
1915         if ((volume->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1916
1917             /* FIXME find db time stamp */
1918             if (cnid_getstamp(volume->v_cdb, volume->v_stamp, sizeof(volume->v_stamp)) < 0) {
1919                 LOG (log_error, logtype_afpd, 
1920                       "afp_openvol(%s): Fatal error: Unable to get stamp value from CNID backend",
1921                       volume->v_path);
1922                 ret = AFPERR_MISC;
1923                 goto openvol_err;
1924             }
1925         }
1926         else {
1927             p = Trash;
1928             cname( volume, volume->v_dir, &p );
1929         }
1930         return( AFP_OK );
1931     }
1932
1933 openvol_err:
1934     if (volume->v_dir) {
1935         hash_free( volume->v_hash);
1936         dirfree( volume->v_dir );
1937         volume->v_dir = volume->v_root = NULL;
1938     }
1939
1940     volume->v_flags &= ~AFPVOL_OPEN;
1941     if (volume->v_cdb != NULL) {
1942         cnid_close(volume->v_cdb);
1943         volume->v_cdb = NULL;
1944     }
1945     *rbuflen = 0;
1946     return ret;
1947 }
1948
1949 /* ------------------------- */
1950 static void closevol(struct vol *vol)
1951 {
1952     if (!vol)
1953         return;
1954
1955     hash_free( vol->v_hash);
1956     dirfree( vol->v_root );
1957     vol->v_dir = NULL;
1958     if (vol->v_cdb != NULL) {
1959         cnid_close(vol->v_cdb);
1960         vol->v_cdb = NULL;
1961     }
1962
1963     if (vol->v_postexec) {
1964         afprun(0, vol->v_postexec, NULL);
1965     }
1966     if (vol->v_root_postexec) {
1967         afprun(1, vol->v_root_postexec, NULL);
1968     }
1969 }
1970
1971 /* ------------------------- */
1972 void close_all_vol(void)
1973 {
1974     struct vol  *ovol;
1975     curdir = NULL;
1976     for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
1977         if ( (ovol->v_flags & AFPVOL_OPEN) ) {
1978             ovol->v_flags &= ~AFPVOL_OPEN;
1979             closevol(ovol);
1980         }
1981     }
1982 }
1983
1984 /* ------------------------- */
1985 static void deletevol(struct vol *vol)
1986 {
1987     struct vol  *ovol;
1988
1989     vol->v_flags &= ~AFPVOL_OPEN;
1990     for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
1991         if ( (ovol->v_flags & AFPVOL_OPEN) ) {
1992             break;
1993         }
1994     }
1995     if ( ovol != NULL ) {
1996         /* Even if chdir fails, we can't say afp_closevol fails. */
1997         if ( chdir( ovol->v_path ) == 0 ) {
1998             curdir = ovol->v_dir;
1999         }
2000     }
2001
2002     closevol(vol);
2003     if (vol->v_deleted) {
2004       showvol(utf8_encoding()?vol->v_u8mname:vol->v_macname);
2005         volume_free(vol);
2006         volume_unlink(vol);
2007         free(vol);
2008     }
2009 }
2010
2011 /* ------------------------- */
2012 int afp_closevol(obj, ibuf, ibuflen, rbuf, rbuflen )
2013 AFPObj  *obj _U_;
2014 char    *ibuf, *rbuf _U_;
2015 int     ibuflen _U_, *rbuflen;
2016 {
2017     struct vol  *vol;
2018     u_int16_t   vid;
2019
2020     *rbuflen = 0;
2021     ibuf += 2;
2022     memcpy(&vid, ibuf, sizeof( vid ));
2023     if (NULL == ( vol = getvolbyvid( vid )) ) {
2024         return( AFPERR_PARAM );
2025     }
2026
2027     deletevol(vol);
2028
2029     return( AFP_OK );
2030 }
2031
2032 /* ------------------------- */
2033 struct vol *getvolbyvid(const u_int16_t vid )
2034 {
2035     struct vol  *vol;
2036
2037     for ( vol = Volumes; vol; vol = vol->v_next ) {
2038         if ( vid == vol->v_vid ) {
2039             break;
2040         }
2041     }
2042     if ( vol == NULL || ( vol->v_flags & AFPVOL_OPEN ) == 0 ) {
2043         return( NULL );
2044     }
2045
2046 #ifdef FORCE_UIDGID
2047     set_uidgid ( vol );
2048 #endif /* FORCE_UIDGID */
2049
2050     return( vol );
2051 }
2052
2053 /* ------------------------ */
2054 static int ext_cmp_key(const void *key, const void *obj)
2055 {
2056     const char          *p = key;
2057     const struct extmap *em = obj;
2058     return strdiacasecmp(p, em->em_ext);
2059 }
2060 struct extmap *getextmap(const char *path)
2061 {
2062     char          *p;
2063     struct extmap *em;
2064
2065     if (NULL == ( p = strrchr( path, '.' )) ) {
2066         return( Defextmap );
2067     }
2068     p++;
2069     if (!*p || !Extmap_cnt) {
2070         return( Defextmap );
2071     }
2072     em = bsearch(p, Extmap, Extmap_cnt, sizeof(struct extmap), ext_cmp_key);
2073     if (em) {
2074         return( em );
2075     } else {
2076         return( Defextmap );
2077     }
2078 }
2079
2080 /* ------------------------- */
2081 struct extmap *getdefextmap(void)
2082 {
2083     return( Defextmap );
2084 }
2085
2086 /* --------------------------
2087    poll if a volume is changed by other processes.
2088 */
2089 int  pollvoltime(obj)
2090 AFPObj *obj;
2091 {
2092     struct vol       *vol;
2093     struct timeval   tv;
2094     struct stat      st;
2095     
2096     if (!(afp_version > 21 && obj->options.server_notif)) 
2097          return 0;
2098
2099     if ( gettimeofday( &tv, 0 ) < 0 ) 
2100          return 0;
2101
2102     for ( vol = Volumes; vol; vol = vol->v_next ) {
2103         if ( (vol->v_flags & AFPVOL_OPEN)  && vol->v_mtime + 30 < tv.tv_sec) {
2104             if ( !stat( vol->v_path, &st ) && vol->v_mtime != st.st_mtime ) {
2105                 vol->v_mtime = st.st_mtime;
2106                 if (!obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED))
2107                     return -1;
2108                 return 1;
2109             }
2110         }
2111     }
2112     return 0;
2113 }
2114
2115 /* ------------------------- */
2116 void setvoltime(obj, vol )
2117 AFPObj *obj;
2118 struct vol      *vol;
2119 {
2120     struct timeval      tv;
2121
2122     /* just looking at vol->v_mtime is broken seriously since updates
2123      * from other users afpd processes never are seen.
2124      * This is not the most elegant solution (a shared memory between
2125      * the afpd processes would come closer)
2126      * [RS] */
2127
2128     if ( gettimeofday( &tv, 0 ) < 0 ) {
2129         LOG(log_error, logtype_afpd, "setvoltime(%s): gettimeofday: %s", vol->v_path, strerror(errno) );
2130         return;
2131     }
2132     if( utime( vol->v_path, NULL ) < 0 ) {
2133         /* write of time failed ... probably a read only filesys,
2134          * where no other users can interfere, so there's no issue here
2135          */
2136     }
2137
2138     /* a little granularity */
2139     if (vol->v_mtime < tv.tv_sec) {
2140         vol->v_mtime = tv.tv_sec;
2141         /* or finder doesn't update free space */
2142         if (afp_version > 21 && obj->options.server_notif) {
2143             obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED);
2144         }
2145     }
2146 }
2147
2148 /* ------------------------- */
2149 int afp_getvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
2150 AFPObj  *obj _U_;
2151 char    *ibuf, *rbuf;
2152 int     ibuflen _U_, *rbuflen;
2153 {
2154     struct vol  *vol;
2155     u_int16_t   vid, bitmap;
2156
2157     ibuf += 2;
2158     memcpy(&vid, ibuf, sizeof( vid ));
2159     ibuf += sizeof( vid );
2160     memcpy(&bitmap, ibuf, sizeof( bitmap ));
2161     bitmap = ntohs( bitmap );
2162
2163     if (NULL == ( vol = getvolbyvid( vid )) ) {
2164         *rbuflen = 0;
2165         return( AFPERR_PARAM );
2166     }
2167
2168     return stat_vol(bitmap, vol, rbuf, rbuflen);
2169 }
2170
2171 /* ------------------------- */
2172 int afp_setvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
2173 AFPObj  *obj _U_;
2174 char    *ibuf, *rbuf _U_;
2175 int     ibuflen _U_, *rbuflen;
2176 {
2177     struct adouble ad;
2178     struct vol  *vol;
2179     u_int16_t   vid, bitmap;
2180     u_int32_t   aint;
2181
2182     ibuf += 2;
2183     *rbuflen = 0;
2184
2185     memcpy(&vid, ibuf, sizeof( vid ));
2186     ibuf += sizeof( vid );
2187     memcpy(&bitmap, ibuf, sizeof( bitmap ));
2188     bitmap = ntohs( bitmap );
2189     ibuf += sizeof(bitmap);
2190
2191     if (( vol = getvolbyvid( vid )) == NULL ) {
2192         return( AFPERR_PARAM );
2193     }
2194
2195     if ((vol->v_flags & AFPVOL_RO))
2196         return AFPERR_VLOCK;
2197
2198     /* we can only set the backup date. */
2199     if (bitmap != (1 << VOLPBIT_BDATE))
2200         return AFPERR_BITMAP;
2201
2202     ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2203     if ( ad_open( vol->v_path, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR,
2204                   0666, &ad) < 0 ) {
2205         if (errno == EROFS)
2206             return AFPERR_VLOCK;
2207
2208         return AFPERR_ACCESS;
2209     }
2210
2211     memcpy(&aint, ibuf, sizeof(aint));
2212     ad_setdate(&ad, AD_DATE_BACKUP, aint);
2213     ad_flush(&ad);
2214     ad_close(&ad, ADFLAGS_HF);
2215     return( AFP_OK );
2216 }
2217
2218 /* ------------------------- */
2219 int wincheck(const struct vol *vol, const char *path)
2220 {
2221     int len;
2222
2223     if (!(vol->v_flags & AFPVOL_MSWINDOWS))
2224         return 1;
2225
2226     /* empty paths are not allowed */
2227     if ((len = strlen(path)) == 0)
2228         return 0;
2229
2230     /* leading or trailing whitespaces are not allowed, carriage returns
2231      * and probably other whitespace is okay, tabs are not allowed
2232      */
2233     if ((path[0] == ' ') || (path[len-1] == ' '))
2234         return 0;
2235
2236     /* certain characters are not allowed */
2237     if (strpbrk(path, MSWINDOWS_BADCHARS))
2238         return 0;
2239
2240     /* everything else is okay */
2241     return 1;
2242 }
2243
2244
2245 /*
2246  * precreate a folder 
2247  * this is only intended for folders in the volume root 
2248  * It will *not* work if the folder name contains extended characters 
2249  */
2250 static int create_special_folder (const struct vol *vol, const struct _special_folder *folder)
2251 {
2252         char            *p,*q,*r;
2253         struct adouble  ad;
2254         u_int16_t       attr;
2255         struct stat     st;
2256         int             ret;
2257
2258
2259         p = (char *) malloc ( strlen(vol->v_path)+strlen(folder->name)+2);
2260         if ( p == NULL) {
2261                 LOG(log_error, logtype_afpd,"malloc failed");
2262                 exit (EXITERR_SYS);
2263         }
2264
2265         q=strdup(folder->name);
2266         if ( q == NULL) {
2267                 LOG(log_error, logtype_afpd,"malloc failed");
2268                 exit (EXITERR_SYS);
2269         }
2270
2271         strcpy(p, vol->v_path);
2272         strcat(p, "/");
2273
2274         r=q;
2275         while (*r) {
2276                 if ((vol->v_casefold & AFPVOL_MTOUUPPER))
2277                         *r=toupper(*r);
2278                 else if ((vol->v_casefold & AFPVOL_MTOULOWER))
2279                         *r=tolower(*r);
2280                 r++;
2281         }
2282         strcat(p, q);
2283
2284         if ( (ret = stat( p, &st )) < 0 ) {
2285                 if (folder->precreate) {
2286                     if (ad_mkdir(p, folder->mode)) {
2287                         LOG(log_debug, logtype_afpd,"Creating '%s' failed in %s: %s", p, vol->v_path, strerror(errno));
2288                         free(p);
2289                         free(q);
2290                         return -1;
2291                     }
2292                     ret = 0;
2293                 }
2294         }
2295
2296         if ( !ret && folder->hide) {
2297                 /* Hide it */
2298                 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2299                 if (ad_open( p, vol_noadouble(vol) | ADFLAGS_HF|ADFLAGS_DIR,
2300                         O_RDWR|O_CREAT, 0666, &ad) < 0) {
2301                         free (p);
2302                         free(q);
2303                         return (-1);
2304                 }
2305                 if ((ad_get_HF_flags( &ad ) & O_CREAT) ) {
2306                     if (ad_getentryoff(&ad, ADEID_NAME)) {
2307                         ad_setentrylen( &ad, ADEID_NAME, strlen(folder->name));
2308                         memcpy(ad_entry( &ad, ADEID_NAME ), folder->name,
2309                                ad_getentrylen( &ad, ADEID_NAME ));
2310                     }
2311                 }
2312  
2313                 ad_getattr(&ad, &attr);
2314                 attr |= htons( ntohs( attr ) | ATTRBIT_INVISIBLE );
2315                 ad_setattr(&ad, attr);
2316
2317                 /* do the same with the finder info */
2318                 if (ad_entry(&ad, ADEID_FINDERI)) {
2319                         memcpy(&attr, ad_entry(&ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF, sizeof(attr));
2320                         attr   |= htons(FINDERINFO_INVISIBLE);
2321                         memcpy(ad_entry(&ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF,&attr, sizeof(attr));
2322                 }
2323
2324                 ad_flush( &ad );
2325                 ad_close( &ad, ADFLAGS_HF );
2326         }
2327         free(p);
2328         free(q);
2329         return 0;
2330 }
2331
2332 static void handle_special_folders (const struct vol * vol)
2333 {
2334         const _special_folder *p = &special_folders[0];
2335
2336         if ((vol->v_flags & AFPVOL_RO))
2337                 return;
2338
2339         for (; p->name != NULL; p++) {
2340                 create_special_folder (vol, p);
2341         }
2342 }
2343
2344 /*
2345  * Save the volume options to a file, used by
2346  * shell utilities.
2347  * Writing the file everytime a volume is opened is
2348  * unnecessary, but it shouldn't hurt much.
2349  */
2350 static int savevoloptions (const struct vol *vol)
2351 {
2352     char buf[16348];
2353     char item[MAXPATHLEN];
2354     int fd;
2355     int ret = 0;
2356     struct flock lock;
2357     const _vol_opt_name *op = &vol_opt_names[0];
2358     const _vol_opt_name *cf = &vol_opt_casefold[0];
2359
2360     strlcpy (item, vol->v_path, sizeof(item));
2361     strlcat (item, "/.AppleDesktop/", sizeof(item));
2362     strlcat (item, VOLINFOFILE, sizeof(item));
2363
2364     if ((fd = open( item, O_RDWR | O_CREAT , 0666)) <0 ) {
2365         LOG(log_debug, logtype_afpd,"Error opening %s: %s", item, strerror(errno));
2366         return (-1);
2367     }
2368
2369     /* try to get a lock */
2370     lock.l_start  = 0;
2371     lock.l_whence = SEEK_SET;
2372     lock.l_len    = 0;
2373     lock.l_type   = F_WRLCK;
2374
2375     if (fcntl(fd, F_SETLK, &lock) < 0) {
2376         if (errno == EACCES || errno == EAGAIN) {
2377             /* ignore, other process already writing the file */
2378             return 0;
2379         } else {
2380             LOG(log_error, logtype_cnid, "savevoloptions: cannot get lock: %s", strerror(errno));
2381             return (-1);
2382         }
2383     }
2384
2385     /* write volume options */
2386     snprintf(buf, sizeof(buf), "MAC_CHARSET:%s\n", vol->v_maccodepage);
2387     snprintf(item, sizeof(item), "VOL_CHARSET:%s\n", vol->v_volcodepage);
2388     strlcat(buf, item, sizeof(buf));
2389
2390     switch (vol->v_adouble) {
2391         case AD_VERSION1:
2392             strlcat(buf, "ADOUBLE_VER:v1\n", sizeof(buf));
2393             break;
2394         case AD_VERSION2:
2395             strlcat(buf, "ADOUBLE_VER:v2\n", sizeof(buf));
2396             break;
2397         case AD_VERSION2_OSX:
2398             strlcat(buf, "ADOUBLE_VER:osx\n", sizeof(buf));
2399             break;
2400         case AD_VERSION1_ADS:
2401             strlcat(buf, "ADOUBLE_VER:ads\n", sizeof(buf));
2402             break;
2403         case AD_VERSION1_SFM:
2404             strlcat(buf, "ADOUBLE_VER:sfm\n", sizeof(buf));
2405             break;
2406     }
2407
2408     strlcat(buf, "CNIDBACKEND:", sizeof(buf));
2409     strlcat(buf, vol->v_cnidscheme, sizeof(buf));
2410     strlcat(buf, "\n", sizeof(buf));
2411
2412     strlcat(buf, "CNIDDBDHOST:", sizeof(buf));
2413     strlcat(buf, Cnid_srv, sizeof(buf));
2414     strlcat(buf, "\n", sizeof(buf));
2415
2416     snprintf(item, sizeof(item), "CNIDDBDPORT:%u\n", Cnid_port);
2417     strlcat(buf, item, sizeof(buf));
2418
2419     strcpy(item, "CNID_DBPATH:");
2420     if (vol->v_dbpath)
2421         strlcat(item, vol->v_dbpath, sizeof(item));
2422     else
2423         strlcat(item, vol->v_path, sizeof(item));
2424     strlcat(item, "\n", sizeof(item));
2425     strlcat(buf, item, sizeof(buf));
2426
2427     /* volume flags */
2428     strcpy(item, "VOLUME_OPTS:");
2429     for (;op->name; op++) {
2430         if ( (vol->v_flags & op->option) ) {
2431             strlcat(item, op->name, sizeof(item));
2432             strlcat(item, " ", sizeof(item));
2433         }
2434     }
2435     strlcat(item, "\n", sizeof(item));
2436     strlcat(buf, item, sizeof(buf));
2437
2438     /* casefold flags */
2439     strcpy(item, "VOLCASEFOLD:");
2440     for (;cf->name; cf++) {
2441         if ( (vol->v_casefold & cf->option) ) {
2442             strlcat(item, cf->name, sizeof(item));
2443             strlcat(item, " ", sizeof(item));
2444         }
2445     }
2446     strlcat(item, "\n", sizeof(item));
2447     strlcat(buf, item, sizeof(buf));
2448
2449     if (strlen(buf) >= sizeof(buf)-1)
2450         LOG(log_debug, logtype_afpd,"Error writing .volinfo file: buffer too small, %s", buf);
2451
2452
2453    if (write( fd, buf, strlen(buf)) < 0 || ftruncate(fd, strlen(buf)) < 0 ) {
2454        LOG(log_debug, logtype_afpd,"Error writing .volinfo file: %s", strerror(errno));
2455    }
2456
2457    lock.l_type = F_UNLCK;
2458    fcntl(fd, F_SETLK, &lock);
2459    close (fd);
2460    return ret;
2461 }