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