2 * $Id: volume.c,v 1.102 2009-11-13 00:27:36 didg Exp $
4 * Copyright (c) 1990,1993 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
10 #endif /* HAVE_CONFIG_H */
25 #else /* STDC_HEADERS */
29 #endif /* HAVE_STRCHR */
30 char *strchr (), *strrchr ();
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>
49 #include <atalk/cnid.h>
53 #include "directory.h"
61 extern int afprun(int root, char *cmd, int *outfd);
64 #define MIN(a, b) ((a) < (b) ? (a) : (b))
67 #ifndef NO_LARGE_VOL_SUPPORT
68 #if BYTE_ORDER == BIG_ENDIAN
71 #else /* BYTE_ORDER == BIG_ENDIAN */
72 #define hton64(x) ((u_int64_t) (htonl(((x) >> 32) & 0xffffffffLL)) | \
73 (u_int64_t) ((htonl(x) & 0xffffffffLL) << 32))
74 #define ntoh64(x) (hton64(x))
75 #endif /* BYTE_ORDER == BIG_ENDIAN */
76 #endif /* ! NO_LARGE_VOL_SUPPORT */
78 static struct vol *Volumes = NULL;
79 static u_int16_t lastvid = 0;
80 static char *Trash = "\02\024Network Trash Folder";
82 static struct extmap *Extmap = NULL, *Defextmap = NULL;
83 static int Extmap_cnt;
84 static void free_extmap(void);
86 #define VOLOPT_ALLOW 0 /* user allow list */
87 #define VOLOPT_DENY 1 /* user deny list */
88 #define VOLOPT_RWLIST 2 /* user rw list */
89 #define VOLOPT_ROLIST 3 /* user ro list */
90 #define VOLOPT_PASSWORD 4 /* volume password */
91 #define VOLOPT_CASEFOLD 5 /* character case mangling */
92 #define VOLOPT_FLAGS 6 /* various flags */
93 #define VOLOPT_DBPATH 7 /* path to database */
94 #define VOLOPT_MAPCHARS 8 /* does mtou and utom mappings. syntax:
95 m and u can be double-byte hex
100 !u -> make u illegal always
101 ~u -> make u illegal only as the first
102 part of a double-byte character.
104 #define VOLOPT_VETO 10 /* list of veto filespec */
105 #define VOLOPT_PREEXEC 11 /* preexec command */
106 #define VOLOPT_ROOTPREEXEC 12 /* root preexec command */
108 #define VOLOPT_POSTEXEC 13 /* postexec command */
109 #define VOLOPT_ROOTPOSTEXEC 14 /* root postexec command */
111 #define VOLOPT_ENCODING 15 /* mac encoding (pre OSX)*/
112 #define VOLOPT_MACCHARSET 16
113 #define VOLOPT_CNIDSCHEME 17
114 #define VOLOPT_ADOUBLE 18 /* adouble version */
119 #define VOLOPT_FORCEUID 19 /* force uid for username x */
120 #define VOLOPT_FORCEGID 20 /* force gid for group x */
121 #endif /* FORCE_UIDGID */
123 #define VOLOPT_UMASK 21
124 #define VOLOPT_ALLOWED_HOSTS 22
125 #define VOLOPT_DENIED_HOSTS 23
126 #define VOLOPT_DPERM 24 /* dperm default directories perms */
127 #define VOLOPT_FPERM 25 /* fperm default files perms */
128 #define VOLOPT_DFLTPERM 26 /* perm */
129 #define VOLOPT_EA_VFS 27 /* Extended Attributes vfs indirection */
131 #define VOLOPT_MAX (VOLOPT_DFLTPERM +1)
133 #define VOLOPT_NUM (VOLOPT_MAX + 1)
136 #define VOLOPT_DEFAULT ":DEFAULT:"
137 #define VOLOPT_DEFAULT_LEN 9
144 typedef struct _special_folder {
151 static const _special_folder special_folders[] = {
152 {"Network Trash Folder", 1, 0777, 1},
153 {"Temporary Items", 1, 0777, 1},
154 {".AppleDesktop", 1, 0777, 0},
156 {"TheFindByContentFolder", 0, 0, 1},
157 {"TheVolumeSettingsFolder", 0, 0, 1},
161 typedef struct _volopt_name {
162 const u_int32_t option;
167 /* Keep in sync with libatalk/util/volinfo.c ! */
168 static const _vol_opt_name vol_opt_names[] = {
169 {AFPVOL_A2VOL, "PRODOS"}, /* prodos volume */
170 {AFPVOL_CRLF, "CRLF"}, /* cr/lf translation */
171 {AFPVOL_NOADOUBLE, "NOADOUBLE"}, /* don't create .AppleDouble by default */
172 {AFPVOL_RO, "READONLY"}, /* read-only volume */
173 {AFPVOL_MSWINDOWS, "MSWINDOWS"}, /* deal with ms-windows yuckiness. this is going away. */
174 {AFPVOL_NOHEX, "NOHEX"}, /* don't do :hex translation */
175 {AFPVOL_USEDOTS, "USEDOTS"}, /* use real dots */
176 {AFPVOL_LIMITSIZE, "LIMITSIZE"}, /* limit size for older macs */
177 {AFPVOL_MAPASCII, "MAPASCII"}, /* map the ascii range as well */
178 {AFPVOL_DROPBOX, "DROPBOX"}, /* dropkludge dropbox support */
179 {AFPVOL_NOFILEID, "NOFILEID"}, /* don't advertise createid resolveid and deleteid calls */
180 {AFPVOL_NOSTAT, "NOSTAT"}, /* advertise the volume even if we can't stat() it
181 * maybe because it will be mounted later in preexec */
182 {AFPVOL_UNIX_PRIV, "UNIXPRIV"}, /* support unix privileges */
183 {AFPVOL_NODEV, "NODEV"}, /* always use 0 for device number in cnid calls */
184 {AFPVOL_CASEINSEN, "CASEINSENSITIVE"}, /* volume is case insensitive */
185 {AFPVOL_EILSEQ, "ILLEGALSEQ"}, /* encode illegal sequence */
186 {AFPVOL_CACHE, "CACHEID"}, /* Use adouble v2 CNID caching. Default: yes */
187 {AFPVOL_ACLS, "ACLS"}, /* Vol supports ACLs */
188 {AFPVOL_TM, "TM"}, /* Set "kSupportsTMLockSteal" is volume attributes */
192 static const _vol_opt_name vol_opt_casefold[] = {
193 {AFPVOL_MTOUUPPER, "MTOULOWER"},
194 {AFPVOL_MTOULOWER, "MTOULOWER"},
195 {AFPVOL_UTOMUPPER, "UTOMUPPER"},
196 {AFPVOL_UTOMLOWER, "UTOMLOWER"},
200 static void handle_special_folders (const struct vol *);
201 static int savevoloptions (const struct vol *);
202 static void deletevol(struct vol *vol);
203 static void volume_free(struct vol *vol);
205 static void volfree(struct vol_option *options,
206 const struct vol_option *save)
211 for (i = 0; i < VOLOPT_MAX; i++) {
212 if (options[i].c_value && (options[i].c_value != save[i].c_value))
213 free(options[i].c_value);
216 for (i = 0; i < VOLOPT_MAX; i++) {
217 if (options[i].c_value)
218 free(options[i].c_value);
224 /* handle variable substitutions. here's what we understand:
225 * $b -> basename of path
226 * $c -> client ip/appletalk address
227 * $d -> volume pathname on server
228 * $f -> full name (whatever's in the gecos field)
231 * $i -> client ip/appletalk address without port
232 * $s -> server name (hostname if it doesn't exist)
233 * $u -> username (guest is usually nobody)
234 * $v -> volume name or basename if null
235 * $z -> zone (may not exist)
240 #define is_var(a, b) (strncmp((a), (b), 2) == 0)
242 static char *volxlate(AFPObj *obj, char *dest, size_t destlen,
243 char *src, struct passwd *pwd, char *path, char *volname)
254 dest = calloc(destlen +1, 1);
260 strlcpy(dest, src, destlen +1);
261 if ((p = strchr(src, '$')) == NULL) /* nothing to do */
264 /* first part of the path. just forward to the next variable. */
265 len = MIN((size_t)(p - src), destlen);
271 while (p && destlen > 0) {
272 /* now figure out what the variable is */
274 if (is_var(p, "$b")) {
276 if ((q = strrchr(path, '/')) == NULL)
278 else if (*(q + 1) != '\0')
281 } else if (is_var(p, "$c")) {
282 if (obj->proto == AFPPROTO_ASP) {
283 ASP asp = obj->handle;
285 len = sprintf(dest, "%u.%u", ntohs(asp->asp_sat.sat_addr.s_net),
286 asp->asp_sat.sat_addr.s_node);
290 } else if (obj->proto == AFPPROTO_DSI) {
291 DSI *dsi = obj->handle;
292 len = sprintf(dest, "%s:%u",
293 getip_string((struct sockaddr *)&dsi->client),
294 getip_port((struct sockaddr *)&dsi->client));
298 } else if (is_var(p, "$d")) {
300 } else if (is_var(p, "$f")) {
301 if ((r = strchr(pwd->pw_gecos, ',')))
304 } else if (is_var(p, "$g")) {
305 struct group *grp = getgrgid(pwd->pw_gid);
308 } else if (is_var(p, "$h")) {
309 q = obj->options.hostname;
310 } else if (is_var(p, "$i")) {
311 if (obj->proto == AFPPROTO_ASP) {
312 ASP asp = obj->handle;
314 len = sprintf(dest, "%u", ntohs(asp->asp_sat.sat_addr.s_net));
318 } else if (obj->proto == AFPPROTO_DSI) {
319 DSI *dsi = obj->handle;
320 q = getip_string((struct sockaddr *)&dsi->client);
322 } else if (is_var(p, "$s")) {
325 else if (obj->options.server) {
326 q = obj->options.server;
328 q = obj->options.hostname;
329 } else if (is_var(p, "$u")) {
331 if ( obj->options.ntseparator && (sep = strchr(obj->username, obj->options.ntseparator[0])) != NULL)
335 } else if (is_var(p, "$v")) {
340 if ((q = strrchr(path, '/')) == NULL)
342 else if (*(q + 1) != '\0')
345 } else if (is_var(p, "$z")) {
347 } else if (is_var(p, "$$")) {
352 /* copy the stuff over. if we don't understand something that we
353 * should, just skip it over. */
355 len = MIN(p == q ? 2 : strlen(q), destlen);
356 strncpy(dest, q, len);
361 /* stuff up to next $ */
363 p = strchr(src, '$');
364 len = p ? MIN((size_t)(p - src), destlen) : destlen;
366 strncpy(dest, src, len);
374 /* to make sure that val is valid, make sure to select an opt that
376 static int optionok(const char *buf, const char *opt, const char *val)
378 if (!strstr(buf,opt))
386 /* -------------------- */
387 static void setoption(struct vol_option *options, struct vol_option *save, int opt, const char *val)
389 if (options[opt].c_value && (!save || options[opt].c_value != save[opt].c_value))
390 free(options[opt].c_value);
391 options[opt].c_value = strdup(val + 1);
394 /* ------------------------------------------
395 handle all the options. tmp can't be NULL. */
396 static void volset(struct vol_option *options, struct vol_option *save,
397 char *volname, int vlen,
402 val = strchr(tmp, ':');
404 /* we'll assume it's a volume name. */
405 strncpy(volname, tmp, vlen);
410 LOG(log_debug, logtype_afpd, "Parsing volset %s", val);
412 if (optionok(tmp, "allow:", val)) {
413 setoption(options, save, VOLOPT_ALLOW, val);
415 } else if (optionok(tmp, "deny:", val)) {
416 setoption(options, save, VOLOPT_DENY, val);
418 } else if (optionok(tmp, "rwlist:", val)) {
419 setoption(options, save, VOLOPT_RWLIST, val);
421 } else if (optionok(tmp, "rolist:", val)) {
422 setoption(options, save, VOLOPT_ROLIST, val);
424 } else if (optionok(tmp, "codepage:", val)) {
425 LOG (log_error, logtype_afpd, "The old codepage system has been removed. Please make sure to read the documentation !!!!");
426 /* Make sure we don't screw anything */
428 } else if (optionok(tmp, "volcharset:", val)) {
429 setoption(options, save, VOLOPT_ENCODING, val);
430 } else if (optionok(tmp, "maccharset:", val)) {
431 setoption(options, save, VOLOPT_MACCHARSET, val);
432 } else if (optionok(tmp, "veto:", val)) {
433 setoption(options, save, VOLOPT_VETO, val);
434 } else if (optionok(tmp, "cnidscheme:", val)) {
435 setoption(options, save, VOLOPT_CNIDSCHEME, val);
436 } else if (optionok(tmp, "casefold:", val)) {
437 if (strcasecmp(val + 1, "tolower") == 0)
438 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMLOWER;
439 else if (strcasecmp(val + 1, "toupper") == 0)
440 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMUPPER;
441 else if (strcasecmp(val + 1, "xlatelower") == 0)
442 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UUPPERMLOWER;
443 else if (strcasecmp(val + 1, "xlateupper") == 0)
444 options[VOLOPT_CASEFOLD].i_value = AFPVOL_ULOWERMUPPER;
445 } else if (optionok(tmp, "adouble:", val)) {
446 if (strcasecmp(val + 1, "v1") == 0)
447 options[VOLOPT_ADOUBLE].i_value = AD_VERSION1;
448 #if AD_VERSION == AD_VERSION2
449 else if (strcasecmp(val + 1, "v2") == 0)
450 options[VOLOPT_ADOUBLE].i_value = AD_VERSION2;
451 else if (strcasecmp(val + 1, "osx") == 0)
452 options[VOLOPT_ADOUBLE].i_value = AD_VERSION2_OSX;
453 else if (strcasecmp(val + 1, "sfm") == 0)
454 options[VOLOPT_ADOUBLE].i_value = AD_VERSION1_SFM;
456 } else if (optionok(tmp, "options:", val)) {
459 if ((p = strtok(val + 1, ",")) == NULL) /* nothing */
463 if (strcasecmp(p, "prodos") == 0)
464 options[VOLOPT_FLAGS].i_value |= AFPVOL_A2VOL;
465 else if (strcasecmp(p, "mswindows") == 0) {
466 options[VOLOPT_FLAGS].i_value |= AFPVOL_MSWINDOWS | AFPVOL_USEDOTS;
467 } else if (strcasecmp(p, "crlf") == 0)
468 options[VOLOPT_FLAGS].i_value |= AFPVOL_CRLF;
469 else if (strcasecmp(p, "noadouble") == 0)
470 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOADOUBLE;
471 else if (strcasecmp(p, "ro") == 0)
472 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
473 else if (strcasecmp(p, "nohex") == 0)
474 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOHEX;
475 else if (strcasecmp(p, "usedots") == 0)
476 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS;
477 else if (strcasecmp(p, "invisibledots") == 0)
478 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS | AFPVOL_INV_DOTS;
479 else if (strcasecmp(p, "limitsize") == 0)
480 options[VOLOPT_FLAGS].i_value |= AFPVOL_LIMITSIZE;
481 /* support for either "dropbox" or "dropkludge" */
482 else if (strcasecmp(p, "dropbox") == 0)
483 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
484 else if (strcasecmp(p, "dropkludge") == 0)
485 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
486 else if (strcasecmp(p, "nofileid") == 0)
487 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOFILEID;
488 else if (strcasecmp(p, "nostat") == 0)
489 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOSTAT;
490 else if (strcasecmp(p, "preexec_close") == 0)
491 options[VOLOPT_PREEXEC].i_value = 1;
492 else if (strcasecmp(p, "root_preexec_close") == 0)
493 options[VOLOPT_ROOTPREEXEC].i_value = 1;
494 else if (strcasecmp(p, "upriv") == 0)
495 options[VOLOPT_FLAGS].i_value |= AFPVOL_UNIX_PRIV;
496 else if (strcasecmp(p, "acls") == 0)
497 options[VOLOPT_FLAGS].i_value |= AFPVOL_ACLS;
498 else if (strcasecmp(p, "nodev") == 0)
499 options[VOLOPT_FLAGS].i_value |= AFPVOL_NODEV;
500 else if (strcasecmp(p, "caseinsensitive") == 0)
501 options[VOLOPT_FLAGS].i_value |= AFPVOL_CASEINSEN;
502 else if (strcasecmp(p, "illegalseq") == 0)
503 options[VOLOPT_FLAGS].i_value |= AFPVOL_EILSEQ;
504 else if (strcasecmp(p, "nocnidcache") == 0)
505 options[VOLOPT_FLAGS].i_value &= ~AFPVOL_CACHE;
506 else if (strcasecmp(p, "tm") == 0)
507 options[VOLOPT_FLAGS].i_value |= AFPVOL_TM;
509 p = strtok(NULL, ",");
512 } else if (optionok(tmp, "dbpath:", val)) {
513 setoption(options, save, VOLOPT_DBPATH, val);
515 } else if (optionok(tmp, "umask:", val)) {
516 options[VOLOPT_UMASK].i_value = (int)strtol(val +1, NULL, 8);
517 } else if (optionok(tmp, "dperm:", val)) {
518 options[VOLOPT_DPERM].i_value = (int)strtol(val+1, NULL, 8);
519 } else if (optionok(tmp, "fperm:", val)) {
520 options[VOLOPT_FPERM].i_value = (int)strtol(val+1, NULL, 8);
521 } else if (optionok(tmp, "perm:", val)) {
522 options[VOLOPT_DFLTPERM].i_value = (int)strtol(val+1, NULL, 8);
523 } else if (optionok(tmp, "mapchars:",val)) {
524 setoption(options, save, VOLOPT_MAPCHARS, val);
526 } else if (optionok(tmp, "password:", val)) {
527 setoption(options, save, VOLOPT_PASSWORD, val);
531 /* this code allows forced uid/gid per volume settings */
532 } else if (optionok(tmp, "forceuid:", val)) {
533 setoption(options, save, VOLOPT_FORCEUID, val);
534 } else if (optionok(tmp, "forcegid:", val)) {
535 setoption(options, save, VOLOPT_FORCEGID, val);
537 #endif /* FORCE_UIDGID */
538 } else if (optionok(tmp, "root_preexec:", val)) {
539 setoption(options, save, VOLOPT_ROOTPREEXEC, val);
541 } else if (optionok(tmp, "preexec:", val)) {
542 setoption(options, save, VOLOPT_PREEXEC, val);
544 } else if (optionok(tmp, "root_postexec:", val)) {
545 setoption(options, save, VOLOPT_ROOTPOSTEXEC, val);
547 } else if (optionok(tmp, "postexec:", val)) {
548 setoption(options, save, VOLOPT_POSTEXEC, val);
550 } else if (optionok(tmp, "allowed_hosts:", val)) {
551 setoption(options, save, VOLOPT_ALLOWED_HOSTS, val);
553 } else if (optionok(tmp, "denied_hosts:", val)) {
554 setoption(options, save, VOLOPT_DENIED_HOSTS, val);
556 } else if (optionok(tmp, "ea:", val)) {
557 if (strcasecmp(val + 1, "ad") == 0) /* the default anyway */
558 options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_AD;
559 else if (strcasecmp(val + 1, "solaris") == 0)
560 options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_SOLARIS;
563 /* ignore unknown options */
564 LOG(log_debug, logtype_afpd, "ignoring unknown volume option: %s", tmp);
569 /* ----------------- */
570 static void showvol(const ucs2_t *name)
573 for ( volume = Volumes; volume; volume = volume->v_next ) {
574 if (volume->v_hide && !strcasecmp_w( volume->v_name, name ) ) {
581 /* ------------------------------- */
582 static int creatvol(AFPObj *obj, struct passwd *pwd,
583 char *path, char *name,
584 struct vol_option *options,
585 const int user /* user defined volume */
589 int suffixlen, vlen, tmpvlen, u8mvlen, macvlen;
591 char tmpname[AFPVOL_U8MNAMELEN+1];
592 ucs2_t u8mtmpname[(AFPVOL_U8MNAMELEN+1)*2], mactmpname[(AFPVOL_MACNAMELEN+1)*2];
593 char suffix[6]; /* max is #FFFF */
596 if ( name == NULL || *name == '\0' ) {
597 if ((name = strrchr( path, '/' )) == NULL) {
598 return -1; /* Obviously not a fully qualified path */
601 /* if you wish to share /, you need to specify a name. */
606 /* suffix for mangling use (lastvid + 1) */
607 /* because v_vid has not been decided yet. */
608 suffixlen = sprintf(suffix, "%c%X", MANGLE_CHAR, lastvid + 1 );
610 vlen = strlen( name );
612 /* Unicode Volume Name */
613 /* Firstly convert name from unixcharset to UTF8-MAC */
615 tmpvlen = convert_charset(obj->options.unixcharset, CH_UTF8_MAC, 0, name, vlen, tmpname, AFPVOL_U8MNAMELEN, &flags);
617 strcpy(tmpname, "???");
621 /* Do we have to mangle ? */
622 if ( (flags & CONV_REQMANGLE) || (tmpvlen > obj->options.volnamelen)) {
623 if (tmpvlen + suffixlen > obj->options.volnamelen) {
625 tmpvlen = convert_charset(obj->options.unixcharset, CH_UTF8_MAC, 0, name, vlen, tmpname, obj->options.volnamelen - suffixlen, &flags);
626 tmpname[tmpvlen != (size_t)-1 ? tmpvlen : 0] = 0;
628 strcat(tmpname, suffix);
629 tmpvlen = strlen(tmpname);
632 /* Secondly convert name from UTF8-MAC to UCS2 */
633 if ( 0 >= ( u8mvlen = convert_string(CH_UTF8_MAC, CH_UCS2, tmpname, tmpvlen, u8mtmpname, AFPVOL_U8MNAMELEN*2)) )
636 LOG(log_debug, logtype_afpd, "createvol: Volume '%s' -> UTF8-MAC Name: '%s'", name, tmpname);
638 /* Maccharset Volume Name */
639 /* Firsty convert name from unixcharset to maccharset */
641 tmpvlen = convert_charset(obj->options.unixcharset, obj->options.maccharset, 0, name, vlen, tmpname, AFPVOL_U8MNAMELEN, &flags);
643 strcpy(tmpname, "???");
647 /* Do we have to mangle ? */
648 if ( (flags & CONV_REQMANGLE) || (tmpvlen > AFPVOL_MACNAMELEN)) {
649 if (tmpvlen + suffixlen > AFPVOL_MACNAMELEN) {
651 tmpvlen = convert_charset(obj->options.unixcharset, obj->options.maccharset, 0, name, vlen, tmpname, AFPVOL_MACNAMELEN - suffixlen, &flags);
652 tmpname[tmpvlen != (size_t)-1 ? tmpvlen : 0] = 0;
654 strcat(tmpname, suffix);
655 tmpvlen = strlen(tmpname);
658 /* Secondly convert name from maccharset to UCS2 */
659 if ( 0 >= ( macvlen = convert_string(obj->options.maccharset, CH_UCS2, tmpname, tmpvlen, mactmpname, AFPVOL_U8MNAMELEN*2)) )
662 LOG(log_debug, logtype_afpd, "createvol: Volume '%s' -> Longname: '%s'", name, tmpname);
664 /* check duplicate */
665 for ( volume = Volumes; volume; volume = volume->v_next ) {
666 if (( strcasecmp_w( volume->v_u8mname, u8mtmpname ) == 0 ) || ( strcasecmp_w( volume->v_macname, mactmpname ) == 0 )){
667 if (volume->v_deleted) {
668 volume->v_new = hide = 1;
671 return -1; /* Won't be able to access it, anyway... */
676 if (!( volume = (struct vol *)calloc(1, sizeof( struct vol ))) ) {
677 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
680 if ( NULL == ( volume->v_localname = strdup(name))) {
681 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
686 if ( NULL == ( volume->v_u8mname = strdup_w(u8mtmpname))) {
687 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
692 if ( NULL == ( volume->v_macname = strdup_w(mactmpname))) {
693 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
698 if (!( volume->v_path = (char *)malloc( strlen( path ) + 1 )) ) {
699 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
705 volume->v_name = utf8_encoding()?volume->v_u8mname:volume->v_macname;
706 volume->v_hide = hide;
707 strcpy( volume->v_path, path );
711 #endif /* __svr4__ */
712 /* os X start at 1 and use network order ie. 1 2 3 */
713 volume->v_vid = ++lastvid;
714 volume->v_vid = htons(volume->v_vid);
715 volume->v_vfs_ea = AFPVOL_EA_AD;
719 /* should we casefold? */
720 volume->v_casefold = options[VOLOPT_CASEFOLD].i_value;
722 /* shift in some flags */
723 volume->v_flags = options[VOLOPT_FLAGS].i_value;
725 if (options[VOLOPT_EA_VFS].i_value != AFPVOL_EA_AD)
726 volume->v_vfs_ea = options[VOLOPT_EA_VFS].i_value;
728 volume->v_ad_options = 0;
729 if ((volume->v_flags & AFPVOL_NODEV))
730 volume->v_ad_options |= ADVOL_NODEV;
731 if ((volume->v_flags & AFPVOL_CACHE))
732 volume->v_ad_options |= ADVOL_CACHE;
733 if ((volume->v_flags & AFPVOL_UNIX_PRIV))
734 volume->v_ad_options |= ADVOL_UNIXPRIV;
735 if ((volume->v_flags & AFPVOL_INV_DOTS))
736 volume->v_ad_options |= ADVOL_INVDOTS;
738 if (options[VOLOPT_PASSWORD].c_value)
739 volume->v_password = strdup(options[VOLOPT_PASSWORD].c_value);
741 if (options[VOLOPT_VETO].c_value)
742 volume->v_veto = strdup(options[VOLOPT_VETO].c_value);
744 if (options[VOLOPT_ENCODING].c_value)
745 volume->v_volcodepage = strdup(options[VOLOPT_ENCODING].c_value);
747 if (options[VOLOPT_MACCHARSET].c_value)
748 volume->v_maccodepage = strdup(options[VOLOPT_MACCHARSET].c_value);
750 if (options[VOLOPT_DBPATH].c_value)
751 volume->v_dbpath = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_DBPATH].c_value, pwd, path, name);
753 if (options[VOLOPT_CNIDSCHEME].c_value)
754 volume->v_cnidscheme = strdup(options[VOLOPT_CNIDSCHEME].c_value);
756 if (options[VOLOPT_UMASK].i_value)
757 volume->v_umask = (mode_t)options[VOLOPT_UMASK].i_value;
759 if (options[VOLOPT_DPERM].i_value)
760 volume->v_dperm = (mode_t)options[VOLOPT_DPERM].i_value;
762 if (options[VOLOPT_FPERM].i_value)
763 volume->v_fperm = (mode_t)options[VOLOPT_FPERM].i_value;
765 if (options[VOLOPT_DFLTPERM].i_value)
766 volume->v_perm = (mode_t)options[VOLOPT_DFLTPERM].i_value;
768 if (options[VOLOPT_ADOUBLE].i_value)
769 volume->v_adouble = options[VOLOPT_ADOUBLE].i_value;
771 volume->v_adouble = AD_VERSION;
773 /* Mac to Unix conversion flags*/
774 volume->v_mtou_flags = 0;
775 if (!(volume->v_flags & AFPVOL_NOHEX))
776 volume->v_mtou_flags |= CONV_ESCAPEHEX;
777 if (!(volume->v_flags & AFPVOL_USEDOTS))
778 volume->v_mtou_flags |= CONV_ESCAPEDOTS;
779 if ((volume->v_flags & AFPVOL_EILSEQ))
780 volume->v_mtou_flags |= CONV__EILSEQ;
782 if ((volume->v_casefold & AFPVOL_MTOUUPPER))
783 volume->v_mtou_flags |= CONV_TOUPPER;
784 else if ((volume->v_casefold & AFPVOL_MTOULOWER))
785 volume->v_mtou_flags |= CONV_TOLOWER;
787 /* Unix to Mac conversion flags*/
788 volume->v_utom_flags = CONV_IGNORE | CONV_UNESCAPEHEX;
789 if ((volume->v_casefold & AFPVOL_UTOMUPPER))
790 volume->v_utom_flags |= CONV_TOUPPER;
791 else if ((volume->v_casefold & AFPVOL_UTOMLOWER))
792 volume->v_utom_flags |= CONV_TOLOWER;
794 if ((volume->v_flags & AFPVOL_EILSEQ))
795 volume->v_utom_flags |= CONV__EILSEQ;
800 if (options[VOLOPT_FORCEUID].c_value) {
801 volume->v_forceuid = strdup(options[VOLOPT_FORCEUID].c_value);
803 volume->v_forceuid = NULL; /* set as null so as to return 0 later on */
806 if (options[VOLOPT_FORCEGID].c_value) {
807 volume->v_forcegid = strdup(options[VOLOPT_FORCEGID].c_value);
809 volume->v_forcegid = NULL; /* set as null so as to return 0 later on */
813 if (options[VOLOPT_PREEXEC].c_value)
814 volume->v_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_PREEXEC].c_value, pwd, path, name);
815 volume->v_preexec_close = options[VOLOPT_PREEXEC].i_value;
817 if (options[VOLOPT_POSTEXEC].c_value)
818 volume->v_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_POSTEXEC].c_value, pwd, path, name);
820 if (options[VOLOPT_ROOTPREEXEC].c_value)
821 volume->v_root_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPREEXEC].c_value, pwd, path, name);
822 volume->v_root_preexec_close = options[VOLOPT_ROOTPREEXEC].i_value;
824 if (options[VOLOPT_ROOTPOSTEXEC].c_value)
825 volume->v_root_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPOSTEXEC].c_value, pwd, path, name);
828 volume->v_dperm |= volume->v_perm;
829 volume->v_fperm |= volume->v_perm;
831 volume->v_next = Volumes;
836 /* ---------------- */
837 static char *myfgets( char *buf, int size, FILE *fp)
843 while ((EOF != ( c = getc( fp )) ) && ( size > 1 )) {
844 if ( c == '\n' || c == '\r' ) {
845 if (p != buf && *(p -1) == '\\') {
867 /* check access list. this function wants something of the following
869 * @group,name,name2,@group2,name3
871 * a NULL argument allows everybody to have access.
872 * we return three things:
874 * 0: list exists, but name isn't in it
878 #ifndef NO_REAL_USER_NAME
879 /* authentication is case insensitive
880 * FIXME should we do the same with group name?
882 #define access_strcmp strcasecmp
885 #define access_strcmp strcmp
889 static int accessvol(const char *args, const char *name)
891 char buf[MAXPATHLEN + 1], *p;
897 strlcpy(buf, args, sizeof(buf));
898 if ((p = strtok(buf, ",")) == NULL) /* nothing, return okay */
902 if (*p == '@') { /* it's a group */
903 if ((gr = getgrnam(p + 1)) && gmem(gr->gr_gid))
905 } else if (access_strcmp(p, name) == 0) /* it's a user name */
907 p = strtok(NULL, ",");
913 static int hostaccessvol(int type, const char *volname, const char *args, const AFPObj *obj)
916 char buf[MAXPATHLEN + 1], *p, *b;
917 DSI *dsi = obj->handle;
918 struct sockaddr_storage client;
923 strlcpy(buf, args, sizeof(buf));
924 if ((p = strtok_r(buf, ",", &b)) == NULL) /* nothing, return okay */
927 if (obj->proto != AFPPROTO_DSI)
932 char *ipaddr, *mask_char;
933 struct addrinfo hints, *ai;
935 ipaddr = strtok(p, "/");
936 mask_char = strtok(NULL,"/");
938 /* Get address from string with getaddrinfo */
939 memset(&hints, 0, sizeof hints);
940 hints.ai_family = AF_UNSPEC;
941 hints.ai_socktype = SOCK_STREAM;
942 if ((ret = getaddrinfo(ipaddr, NULL, &hints, &ai)) != 0) {
943 LOG(log_error, logtype_afpd, "hostaccessvol: getaddrinfo: %s\n", gai_strerror(ret));
948 if (mask_char != NULL)
949 mask_int = atoi(mask_char); /* apply_ip_mask does range checking on it */
951 if (ai->ai_family == AF_INET) /* IPv4 */
957 /* Apply mask to addresses */
958 client = dsi->client;
959 apply_ip_mask((struct sockaddr *)&client, mask_int);
960 apply_ip_mask(ai->ai_addr, mask_int);
962 if (compare_ip((struct sockaddr *)&client, ai->ai_addr) == 0) {
963 if (type == VOLOPT_DENIED_HOSTS)
964 LOG(log_info, logtype_afpd, "AFP access denied for client IP '%s' to volume '%s' by denied list",
965 getip_string((struct sockaddr *)&client), volname);
972 p = strtok_r(NULL, ",", &b);
975 if (type == VOLOPT_ALLOWED_HOSTS)
976 LOG(log_info, logtype_afpd, "AFP access denied for client IP '%s' to volume '%s', not in allowed list",
977 getip_string((struct sockaddr *)&dsi->client), volname);
981 static void setextmap(char *ext, char *type, char *creator, int user)
986 if (Extmap == NULL) {
987 if (( Extmap = calloc(1, sizeof( struct extmap ))) == NULL ) {
988 LOG(log_error, logtype_afpd, "setextmap: calloc: %s", strerror(errno) );
993 for ( em = Extmap, cnt = 0; em->em_ext; em++, cnt++) {
994 if ( (strdiacasecmp( em->em_ext, ext )) == 0 ) {
999 if ( em->em_ext == NULL ) {
1000 if (!(Extmap = realloc( Extmap, sizeof( struct extmap ) * (cnt +2))) ) {
1001 LOG(log_error, logtype_afpd, "setextmap: realloc: %s", strerror(errno) );
1004 (Extmap +cnt +1)->em_ext = NULL;
1006 } else if ( !user ) {
1012 if (!(em->em_ext = strdup( ext))) {
1013 LOG(log_error, logtype_afpd, "setextmap: strdup: %s", strerror(errno) );
1017 if ( *type == '\0' ) {
1018 memcpy(em->em_type, "????", sizeof( em->em_type ));
1020 memcpy(em->em_type, type, sizeof( em->em_type ));
1022 if ( *creator == '\0' ) {
1023 memcpy(em->em_creator, "UNIX", sizeof( em->em_creator ));
1025 memcpy(em->em_creator, creator, sizeof( em->em_creator ));
1029 /* -------------------------- */
1030 static int extmap_cmp(const void *map1, const void *map2)
1032 const struct extmap *em1 = map1;
1033 const struct extmap *em2 = map2;
1034 return strdiacasecmp(em1->em_ext, em2->em_ext);
1037 static void sortextmap( void)
1042 if ((em = Extmap) == NULL) {
1045 while (em->em_ext) {
1050 qsort(Extmap, Extmap_cnt, sizeof(struct extmap), extmap_cmp);
1051 if (*Extmap->em_ext == 0) {
1052 /* the first line is really "." the default entry,
1053 * we remove the leading '.' in setextmap
1060 /* ----------------------
1062 static void free_extmap( void)
1067 for ( em = Extmap; em->em_ext; em++) {
1077 /* ----------------------
1079 static int volfile_changed(struct afp_volume_name *p)
1085 name = p->full_name;
1089 if (!stat( name, &st) && st.st_mtime > p->mtime) {
1090 p->mtime = st.st_mtime;
1096 /* ----------------------
1097 * Read a volume configuration file and add the volumes contained within to
1098 * the global volume list. If p2 is non-NULL, the file that is opened is
1101 * Lines that begin with # and blank lines are ignored.
1102 * Volume lines are of the form:
1103 * <unix path> [<volume name>] [allow:<user>,<@group>,...] \
1104 * [codepage:<file>] [casefold:<num>]
1105 * <extension> TYPE [CREATOR]
1107 static int readvolfile(AFPObj *obj, struct afp_volume_name *p1, char *p2, int user, struct passwd *pwent)
1110 char path[ MAXPATHLEN + 1], tmp[ MAXPATHLEN + 1],
1111 volname[ AFPVOL_U8MNAMELEN + 1 ], buf[ BUFSIZ ],
1112 type[ 5 ], creator[ 5 ];
1115 struct vol_option options[VOLOPT_NUM], save_options[VOLOPT_NUM];
1123 strcpy( path, p1->name );
1125 strcat( path, "/" );
1127 if (p1->full_name) {
1128 free(p1->full_name);
1130 p1->full_name = strdup(path);
1133 if (NULL == ( fp = fopen( path, "r" )) ) {
1137 if (fd != -1 && !fstat( fd, &st) ) {
1138 p1->mtime = st.st_mtime;
1141 memset(save_options, 0, sizeof(save_options));
1143 /* Enable some default options for all volumes */
1144 save_options[VOLOPT_FLAGS].i_value |= AFPVOL_CACHE;
1146 while ( myfgets( buf, sizeof( buf ), fp ) != NULL ) {
1147 initline( strlen( buf ), buf );
1148 parseline( sizeof( path ) - 1, path );
1155 /* change the default options for this file */
1156 if (strncmp(path, VOLOPT_DEFAULT, VOLOPT_DEFAULT_LEN) == 0) {
1158 for (i = 0; i < VOLOPT_NUM; i++) {
1159 if (parseline( sizeof( path ) - VOLOPT_DEFAULT_LEN - 1,
1160 path + VOLOPT_DEFAULT_LEN) < 0)
1162 volset(save_options, NULL, tmp, sizeof(tmp) - 1,
1163 path + VOLOPT_DEFAULT_LEN);
1169 if (( p = strchr( path, '/' )) != NULL ) {
1177 if ( u == NULL || *u == '\0' || ( pw = getpwnam( u )) == NULL ) {
1180 strcpy( tmp, pw->pw_dir );
1181 if ( p != NULL && *p != '\0' ) {
1185 /* Tag a user's home directory with their umask. Note, this will
1186 * be overwritten if the user actually specifies a umask: option
1187 * for a '~' volume. */
1188 save_options[VOLOPT_UMASK].i_value = obj->options.save_mask;
1192 /* send path through variable substitution */
1193 if (*path != '~') /* need to copy path to tmp */
1196 pwent = getpwnam(obj->username);
1197 volxlate(obj, path, sizeof(path) - 1, tmp, pwent, NULL, NULL);
1199 /* this is sort of braindead. basically, i want to be
1200 * able to specify things in any order, but i don't want to
1201 * re-write everything.
1203 * currently we have options:
1211 * options:prodos,crlf,noadouble,ro...
1216 * namemask:x,y,!z (not implemented yet)
1218 memcpy(options, save_options, sizeof(options));
1221 /* read in up to VOLOP_NUM possible options */
1222 for (i = 0; i < VOLOPT_NUM; i++) {
1223 if (parseline( sizeof( tmp ) - 1, tmp ) < 0)
1226 volset(options, save_options, volname, sizeof(volname) - 1, tmp);
1229 /* check allow/deny lists:
1230 allow -> either no list (-1), or in list (1)
1231 deny -> either no list (-1), or not in list (0) */
1232 if (accessvol(options[VOLOPT_ALLOW].c_value, obj->username) &&
1233 (accessvol(options[VOLOPT_DENY].c_value, obj->username) < 1) &&
1234 hostaccessvol(VOLOPT_ALLOWED_HOSTS, volname, options[VOLOPT_ALLOWED_HOSTS].c_value, obj) &&
1235 (hostaccessvol(VOLOPT_DENIED_HOSTS, volname, options[VOLOPT_DENIED_HOSTS].c_value, obj) < 1)) {
1237 /* handle read-only behaviour. semantics:
1238 * 1) neither the rolist nor the rwlist exist -> rw
1239 * 2) rolist exists -> ro if user is in it.
1240 * 3) rwlist exists -> ro unless user is in it. */
1241 if (((options[VOLOPT_FLAGS].i_value & AFPVOL_RO) == 0) &&
1242 ((accessvol(options[VOLOPT_ROLIST].c_value,
1243 obj->username) == 1) ||
1244 !accessvol(options[VOLOPT_RWLIST].c_value,
1246 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
1248 /* do variable substitution for volname */
1249 volxlate(obj, tmp, sizeof(tmp) - 1, volname, pwent, path, NULL);
1250 creatvol(obj, pwent, path, tmp, options, p2 != NULL);
1252 volfree(options, save_options);
1256 parseline( sizeof( type ) - 1, type );
1257 parseline( sizeof( creator ) - 1, creator );
1258 setextmap( path, type, creator, user);
1265 volfree(save_options, NULL);
1267 if ( fclose( fp ) != 0 ) {
1268 LOG(log_error, logtype_afpd, "readvolfile: fclose: %s", strerror(errno) );
1274 /* ------------------------------- */
1275 static void volume_free(struct vol *vol)
1277 free(vol->v_localname);
1278 vol->v_localname = NULL;
1279 free(vol->v_u8mname);
1280 vol->v_u8mname = NULL;
1281 free(vol->v_macname);
1282 vol->v_macname = NULL;
1284 free(vol->v_password);
1286 free(vol->v_volcodepage);
1287 free(vol->v_maccodepage);
1288 free(vol->v_cnidscheme);
1289 free(vol->v_dbpath);
1292 free(vol->v_forceuid);
1293 free(vol->v_forcegid);
1294 #endif /* FORCE_UIDGID */
1297 /* ------------------------------- */
1298 static void free_volumes(void )
1301 struct vol *nvol, *ovol;
1303 for ( vol = Volumes; vol; vol = vol->v_next ) {
1304 if (( vol->v_flags & AFPVOL_OPEN ) ) {
1311 for ( vol = Volumes, ovol = NULL; vol; vol = nvol) {
1314 if (vol->v_localname == NULL) {
1315 if (Volumes == vol) {
1320 ovol->v_next = nvol;
1330 /* ------------------------------- */
1331 static void volume_unlink(struct vol *volume)
1333 struct vol *vol, *ovol, *nvol;
1335 if (volume == Volumes) {
1336 Volumes = Volumes->v_next;
1339 for ( vol = Volumes->v_next, ovol = Volumes; vol; vol = nvol) {
1342 if (vol == volume) {
1343 ovol->v_next = nvol;
1352 static int getvolspace(struct vol *vol,
1353 u_int32_t *bfree, u_int32_t *btotal,
1354 VolSpace *xbfree, VolSpace *xbtotal, u_int32_t *bsize)
1358 #ifndef NO_QUOTA_SUPPORT
1359 VolSpace qfree, qtotal;
1362 spaceflag = AFPVOL_GVSMASK & vol->v_flags;
1363 /* report up to 2GB if afp version is < 2.2 (4GB if not) */
1364 maxsize = (vol->v_flags & AFPVOL_A2VOL) ? 0x01fffe00 :
1365 (((afp_version < 22) || (vol->v_flags & AFPVOL_LIMITSIZE))
1366 ? 0x7fffffffL : 0xffffffffL);
1369 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_AFSGVS ) {
1370 if ( afs_getvolspace( vol, xbfree, xbtotal, bsize ) == AFP_OK ) {
1371 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_AFSGVS;
1372 goto getvolspace_done;
1377 if (( rc = ustatfs_getvolspace( vol, xbfree, xbtotal,
1378 bsize)) != AFP_OK ) {
1382 #define min(a,b) ((a)<(b)?(a):(b))
1383 #ifndef NO_QUOTA_SUPPORT
1384 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_UQUOTA ) {
1385 if ( uquota_getvolspace( vol, &qfree, &qtotal, *bsize ) == AFP_OK ) {
1386 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_UQUOTA;
1387 *xbfree = min(*xbfree, qfree);
1388 *xbtotal = min( *xbtotal, qtotal);
1389 goto getvolspace_done;
1393 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_USTATFS;
1396 *bfree = min( *xbfree, maxsize);
1397 *btotal = min( *xbtotal, maxsize);
1401 /* -----------------------
1402 * set volume creation date
1403 * avoid duplicate, well at least it tries
1405 static void vol_setdate(u_int16_t id, struct adouble *adp, time_t date)
1408 struct vol *vol = Volumes;
1410 for ( volume = Volumes; volume; volume = volume->v_next ) {
1411 if (volume->v_vid == id) {
1414 else if ((time_t)(AD_DATE_FROM_UNIX(date)) == volume->v_ctime) {
1416 volume = Volumes; /* restart */
1419 vol->v_ctime = AD_DATE_FROM_UNIX(date);
1420 ad_setdate(adp, AD_DATE_CREATE | AD_DATE_UNIX, date);
1423 /* ----------------------- */
1424 static int getvolparams( u_int16_t bitmap, struct vol *vol, struct stat *st, char *buf, size_t *buflen)
1427 int bit = 0, isad = 1;
1430 u_int32_t bfree, btotal, bsize;
1431 VolSpace xbfree, xbtotal; /* extended bytes */
1432 char *data, *nameoff = NULL;
1435 LOG(log_debug, logtype_afpd, "getvolparams: Volume '%s'", vol->v_localname);
1437 /* courtesy of jallison@whistle.com:
1438 * For MacOS8.x support we need to create the
1439 * .Parent file here if it doesn't exist. */
1441 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1442 if ( ad_open_metadata( vol->v_path, vol_noadouble(vol) | ADFLAGS_DIR, O_CREAT, &ad) < 0 ) {
1444 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1446 } else if (ad_get_MD_flags( &ad ) & O_CREAT) {
1447 slash = strrchr( vol->v_path, '/' );
1451 slash = vol->v_path;
1452 if (ad_getentryoff(&ad, ADEID_NAME)) {
1453 ad_setentrylen( &ad, ADEID_NAME, strlen( slash ));
1454 memcpy(ad_entry( &ad, ADEID_NAME ), slash,
1455 ad_getentrylen( &ad, ADEID_NAME ));
1457 vol_setdate(vol->v_vid, &ad, st->st_mtime);
1461 if (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0)
1462 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1464 vol->v_ctime = aint;
1467 if (( bitmap & ( (1<<VOLPBIT_BFREE)|(1<<VOLPBIT_BTOTAL) |
1468 (1<<VOLPBIT_XBFREE)|(1<<VOLPBIT_XBTOTAL) |
1469 (1<<VOLPBIT_BSIZE)) ) != 0 ) {
1470 if ( getvolspace( vol, &bfree, &btotal, &xbfree, &xbtotal,
1471 &bsize) != AFP_OK ) {
1473 ad_close( &ad, ADFLAGS_HF );
1475 return( AFPERR_PARAM );
1480 while ( bitmap != 0 ) {
1481 while (( bitmap & 1 ) == 0 ) {
1489 if (0 == (vol->v_flags & AFPVOL_NOFILEID) && vol->v_cdb != NULL &&
1490 (vol->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1491 ashort = VOLPBIT_ATTR_FILEID;
1493 /* check for read-only.
1494 * NOTE: we don't actually set the read-only flag unless
1495 * it's passed in that way as it's possible to mount
1496 * a read-write filesystem under a read-only one. */
1497 if ((vol->v_flags & AFPVOL_RO) ||
1498 ((utime(vol->v_path, NULL) < 0) && (errno == EROFS))) {
1499 ashort |= VOLPBIT_ATTR_RO;
1501 ashort |= VOLPBIT_ATTR_CATSEARCH;
1502 ashort |= VOLPBIT_ATTR_EXT_ATTRS;
1503 if (afp_version >= 30) {
1504 ashort |= VOLPBIT_ATTR_UTF8;
1505 if (vol->v_flags & AFPVOL_UNIX_PRIV)
1506 ashort |= VOLPBIT_ATTR_UNIXPRIV;
1507 if (vol->v_flags & AFPVOL_TM)
1508 ashort |= VOLPBIT_ATTR_TM;
1510 if (afp_version >= 32) {
1511 if (vol->v_flags & AFPVOL_ACLS)
1512 ashort |= VOLPBIT_ATTR_ACLS;
1514 ashort = htons(ashort);
1515 memcpy(data, &ashort, sizeof( ashort ));
1516 data += sizeof( ashort );
1520 ashort = htons( AFPVOLSIG_DEFAULT );
1521 memcpy(data, &ashort, sizeof( ashort ));
1522 data += sizeof( ashort );
1525 case VOLPBIT_CDATE :
1526 aint = vol->v_ctime;
1527 memcpy(data, &aint, sizeof( aint ));
1528 data += sizeof( aint );
1531 case VOLPBIT_MDATE :
1532 if ( st->st_mtime > vol->v_mtime ) {
1533 vol->v_mtime = st->st_mtime;
1535 aint = AD_DATE_FROM_UNIX(vol->v_mtime);
1536 memcpy(data, &aint, sizeof( aint ));
1537 data += sizeof( aint );
1540 case VOLPBIT_BDATE :
1541 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1542 aint = AD_DATE_START;
1543 memcpy(data, &aint, sizeof( aint ));
1544 data += sizeof( aint );
1548 memcpy(data, &vol->v_vid, sizeof( vol->v_vid ));
1549 data += sizeof( vol->v_vid );
1552 case VOLPBIT_BFREE :
1553 bfree = htonl( bfree );
1554 memcpy(data, &bfree, sizeof( bfree ));
1555 data += sizeof( bfree );
1558 case VOLPBIT_BTOTAL :
1559 btotal = htonl( btotal );
1560 memcpy(data, &btotal, sizeof( btotal ));
1561 data += sizeof( btotal );
1564 #ifndef NO_LARGE_VOL_SUPPORT
1565 case VOLPBIT_XBFREE :
1566 xbfree = hton64( xbfree );
1567 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1568 bcopy(&xbfree, data, sizeof(xbfree));
1569 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1570 memcpy(data, &xbfree, sizeof( xbfree ));
1571 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1572 data += sizeof( xbfree );
1575 case VOLPBIT_XBTOTAL :
1576 xbtotal = hton64( xbtotal );
1577 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1578 bcopy(&xbtotal, data, sizeof(xbtotal));
1579 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1580 memcpy(data, &xbtotal, sizeof( xbtotal ));
1581 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1582 data += sizeof( xbfree );
1584 #endif /* ! NO_LARGE_VOL_SUPPORT */
1588 data += sizeof( u_int16_t );
1591 case VOLPBIT_BSIZE: /* block size */
1592 bsize = htonl(bsize);
1593 memcpy(data, &bsize, sizeof(bsize));
1594 data += sizeof(bsize);
1599 ad_close( &ad, ADFLAGS_HF );
1601 return( AFPERR_BITMAP );
1607 ashort = htons( data - buf );
1608 memcpy(nameoff, &ashort, sizeof( ashort ));
1609 /* name is always in mac charset */
1610 aint = ucs2_to_charset( vol->v_maccharset, vol->v_macname, data+1, AFPVOL_MACNAMELEN + 1);
1620 ad_close_metadata( &ad);
1622 *buflen = data - buf;
1626 /* ------------------------- */
1627 static int stat_vol(u_int16_t bitmap, struct vol *vol, char *rbuf, size_t *rbuflen)
1633 if ( stat( vol->v_path, &st ) < 0 ) {
1635 return( AFPERR_PARAM );
1637 /* save the volume device number */
1638 vol->v_dev = st.st_dev;
1640 buflen = *rbuflen - sizeof( bitmap );
1641 if (( ret = getvolparams( bitmap, vol, &st,
1642 rbuf + sizeof( bitmap ), &buflen )) != AFP_OK ) {
1646 *rbuflen = buflen + sizeof( bitmap );
1647 bitmap = htons( bitmap );
1648 memcpy(rbuf, &bitmap, sizeof( bitmap ));
1653 /* ------------------------------- */
1654 void load_volumes(AFPObj *obj)
1656 struct passwd *pwent;
1661 /* check files date */
1662 if (obj->options.defaultvol.loaded) {
1663 changed = volfile_changed(&obj->options.defaultvol);
1665 if (obj->options.systemvol.loaded) {
1666 changed |= volfile_changed(&obj->options.systemvol);
1668 if (obj->options.uservol.loaded) {
1669 changed |= volfile_changed(&obj->options.uservol);
1678 pwent = getpwnam(obj->username);
1679 if ( (obj->options.flags & OPTION_USERVOLFIRST) == 0 ) {
1680 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent);
1683 if ((*obj->username == '\0') || (obj->options.flags & OPTION_NOUSERVOL)) {
1684 readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent);
1687 * Read user's AppleVolumes or .AppleVolumes file
1688 * If neither are readable, read the default volumes file. if
1689 * that doesn't work, create a user share.
1691 if (obj->options.uservol.name) {
1692 free(obj->options.uservol.name);
1694 obj->options.uservol.name = strdup(pwent->pw_dir);
1695 if ( readvolfile(obj, &obj->options.uservol, "AppleVolumes", 1, pwent) < 0 &&
1696 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 obj->options.defaultvol.name != NULL ) {
1700 if (readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent) < 0)
1701 creatvol(obj, pwent, pwent->pw_dir, NULL, NULL, 1);
1704 if ( obj->options.flags & OPTION_USERVOLFIRST ) {
1705 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent );
1708 if ( obj->options.closevol ) {
1711 for ( vol = Volumes; vol; vol = vol->v_next ) {
1712 if (vol->v_deleted && !vol->v_new ) {
1721 /* ------------------------------- */
1722 int afp_getsrvrparms(AFPObj *obj, char *ibuf _U_, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
1735 for ( vcnt = 0, volume = Volumes; volume; volume = volume->v_next ) {
1736 if (!(volume->v_flags & AFPVOL_NOSTAT)) {
1739 if ( stat( volume->v_path, &st ) < 0 ) {
1740 LOG(log_info, logtype_afpd, "afp_getsrvrparms(%s): stat: %s",
1741 volume->v_path, strerror(errno) );
1742 continue; /* can't access directory */
1744 if (!S_ISDIR(st.st_mode)) {
1745 continue; /* not a dir */
1747 accessmode(volume->v_path, &ma, NULL, &st);
1748 if ((ma.ma_user & (AR_UREAD | AR_USEARCH)) != (AR_UREAD | AR_USEARCH)) {
1749 continue; /* no r-x access */
1752 if (volume->v_hide) {
1753 continue; /* config file changed but the volume was mounted */
1756 if (utf8_encoding()) {
1757 len = ucs2_to_charset_allocate(CH_UTF8_MAC, &namebuf, volume->v_u8mname);
1759 len = ucs2_to_charset_allocate(obj->options.maccharset, &namebuf, volume->v_macname);
1762 if (len == (size_t)-1)
1765 /* set password bit if there's a volume password */
1766 *data = (volume->v_password) ? AFPSRVR_PASSWD : 0;
1768 /* Apple 2 clients running ProDOS-8 expect one volume to have
1769 bit 0 of this byte set. They will not recognize anything
1770 on the server unless this is the case. I have not
1771 completely worked this out, but it's related to booting
1772 from the server. Support for that function is a ways
1773 off.. <shirsch@ibm.net> */
1774 *data |= (volume->v_flags & AFPVOL_A2VOL) ? AFPSRVR_CONFIGINFO : 0;
1775 *data++ |= 0; /* UNIX PRIVS BIT ..., OSX doesn't seem to use it, so we don't either */
1777 memcpy(data, namebuf, len );
1783 *rbuflen = data - rbuf;
1785 if ( gettimeofday( &tv, NULL ) < 0 ) {
1786 LOG(log_error, logtype_afpd, "afp_getsrvrparms(%s): gettimeofday: %s", volume->v_path, strerror(errno) );
1788 return AFPERR_PARAM;
1790 tv.tv_sec = AD_DATE_FROM_UNIX(tv.tv_sec);
1791 memcpy(data, &tv.tv_sec, sizeof( u_int32_t));
1792 data += sizeof( u_int32_t);
1797 /* -------------------------
1798 * we are the user here
1800 int afp_openvol(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
1810 char path[ MAXPATHLEN + 1];
1814 struct charset_functions *charset;
1817 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1818 bitmap = ntohs( bitmap );
1819 ibuf += sizeof( bitmap );
1820 if (( bitmap & (1<<VOLPBIT_VID)) == 0 ) {
1822 return AFPERR_BITMAP;
1825 len = (unsigned char)*ibuf++;
1826 volname = obj->oldtmp;
1828 if ((volname_tmp = strchr(volname,'+')) != NULL)
1829 volname = volname_tmp+1;
1831 if (utf8_encoding()) {
1832 namelen = convert_string(CH_UTF8_MAC, CH_UCS2, ibuf, len, volname, sizeof(obj->oldtmp));
1834 namelen = convert_string(obj->options.maccharset, CH_UCS2, ibuf, len, volname, sizeof(obj->oldtmp));
1839 return AFPERR_PARAM;
1843 if ((len + 1) & 1) /* pad to an even boundary */
1848 for ( volume = Volumes; volume; volume = volume->v_next ) {
1849 if ( strcasecmp_w( (ucs2_t*) volname, volume->v_name ) == 0 ) {
1854 if ( volume == NULL ) {
1856 return AFPERR_PARAM;
1859 /* check for a volume password */
1860 if (volume->v_password && strncmp(ibuf, volume->v_password, VOLPASSLEN)) {
1862 return AFPERR_ACCESS;
1865 if (( volume->v_flags & AFPVOL_OPEN ) ) {
1866 /* the volume is already open */
1868 set_uidgid ( volume );
1870 return stat_vol(bitmap, volume, rbuf, rbuflen);
1873 /* initialize volume variables
1876 if (utf8_encoding()) {
1877 volume->max_filename = 255;
1880 volume->max_filename = MACFILELEN;
1883 volume->v_dir = volume->v_root = NULL;
1884 volume->v_hash = NULL;
1886 volume->v_flags |= AFPVOL_OPEN;
1887 volume->v_cdb = NULL;
1889 if (volume->v_root_preexec) {
1890 if ((ret = afprun(1, volume->v_root_preexec, NULL)) && volume->v_root_preexec_close) {
1891 LOG(log_error, logtype_afpd, "afp_openvol(%s): root preexec : %d", volume->v_path, ret );
1898 set_uidgid ( volume );
1901 if (volume->v_preexec) {
1902 if ((ret = afprun(0, volume->v_preexec, NULL)) && volume->v_preexec_close) {
1903 LOG(log_error, logtype_afpd, "afp_openvol(%s): preexec : %d", volume->v_path, ret );
1909 if ( stat( volume->v_path, &st ) < 0 ) {
1914 if ( chdir( volume->v_path ) < 0 ) {
1919 if (utf8_encoding()) {
1920 len = convert_string_allocate(CH_UCS2, CH_UTF8_MAC, volume->v_u8mname, namelen, &vol_mname);
1922 len = convert_string_allocate(CH_UCS2, obj->options.maccharset, volume->v_macname, namelen, &vol_mname);
1924 if ( !vol_mname || len <= 0) {
1929 if ( NULL == getcwd(path, MAXPATHLEN)) {
1930 /* shouldn't be fatal but it will fail later */
1931 LOG(log_error, logtype_afpd, "afp_openvol(%s): volume pathlen too long", volume->v_path);
1936 if ((vol_uname = strrchr(path, '/')) == NULL)
1938 else if (*(vol_uname + 1) != '\0')
1941 if ((dir = dirnew(vol_mname, vol_uname) ) == NULL) {
1943 LOG(log_error, logtype_afpd, "afp_openvol(%s): malloc: %s", volume->v_path, strerror(errno) );
1949 dir->d_did = DIRDID_ROOT;
1950 dir->d_color = DIRTREE_COLOR_BLACK; /* root node is black */
1951 dir->d_m_name_ucs2 = strdup_w(volume->v_name);
1952 volume->v_dir = volume->v_root = dir;
1953 volume->v_curdir = NULL;
1954 volume->v_hash = dirhash();
1956 curdir = volume->v_dir;
1957 if (volume->v_cnidscheme == NULL) {
1958 volume->v_cnidscheme = strdup(DEFAULT_CNID_SCHEME);
1959 LOG(log_info, logtype_afpd, "Volume %s use CNID scheme %s.", volume->v_path, volume->v_cnidscheme);
1961 if (volume->v_dbpath)
1962 volume->v_cdb = cnid_open (volume->v_dbpath, volume->v_umask, volume->v_cnidscheme, (volume->v_flags & AFPVOL_NODEV));
1964 volume->v_cdb = cnid_open (volume->v_path, volume->v_umask, volume->v_cnidscheme, (volume->v_flags & AFPVOL_NODEV));
1965 if (volume->v_cdb == NULL) {
1966 LOG(log_error, logtype_afpd, "Fatal error: cannot open CNID or invalid CNID backend for %s: %s",
1967 volume->v_path, volume->v_cnidscheme);
1974 if (!volume->v_volcodepage)
1975 volume->v_volcodepage = strdup("UTF8");
1977 if ( (charset_t) -1 == ( volume->v_volcharset = add_charset(volume->v_volcodepage)) ) {
1978 LOG (log_error, logtype_afpd, "Setting codepage %s as volume codepage failed", volume->v_volcodepage);
1983 if ( NULL == (charset = find_charset_functions(volume->v_volcodepage)) || charset->flags & CHARSET_ICONV ) {
1984 LOG (log_warning, logtype_afpd, "WARNING: volume encoding %s is *not* supported by netatalk, expect problems !!!!", volume->v_volcodepage);
1987 if (!volume->v_maccodepage)
1988 volume->v_maccodepage = strdup(obj->options.maccodepage);
1990 if ( (charset_t) -1 == ( volume->v_maccharset = add_charset(volume->v_maccodepage)) ) {
1991 LOG (log_error, logtype_afpd, "Setting codepage %s as mac codepage failed", volume->v_maccodepage);
1996 if ( NULL == ( charset = find_charset_functions(volume->v_maccodepage)) || ! (charset->flags & CHARSET_CLIENT) ) {
1997 LOG (log_error, logtype_afpd, "Fatal error: mac charset %s not supported", volume->v_maccodepage);
2001 volume->v_kTextEncoding = htonl(charset->kTextEncoding);
2003 ret = stat_vol(bitmap, volume, rbuf, rbuflen);
2004 if (ret == AFP_OK) {
2006 if (!(volume->v_flags & AFPVOL_RO)) {
2007 handle_special_folders( volume );
2008 savevoloptions( volume);
2012 * If you mount a volume twice, the second time the trash appears on
2013 * the desk-top. That's because the Mac remembers the DID for the
2014 * trash (even for volumes in different zones, on different servers).
2015 * Just so this works better, we prime the DID cache with the trash,
2016 * fixing the trash at DID 17.
2017 * FIXME (RL): should it be done inside a CNID backend ? (always returning Trash DID when asked) ?
2019 if ((volume->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
2021 /* FIXME find db time stamp */
2022 if (cnid_getstamp(volume->v_cdb, volume->v_stamp, sizeof(volume->v_stamp)) < 0) {
2023 LOG (log_error, logtype_afpd,
2024 "afp_openvol(%s): Fatal error: Unable to get stamp value from CNID backend",
2032 cname( volume, volume->v_dir, &p );
2038 if (volume->v_dir) {
2039 hash_free( volume->v_hash);
2040 dirfree( volume->v_dir );
2041 volume->v_dir = volume->v_root = NULL;
2044 volume->v_flags &= ~AFPVOL_OPEN;
2045 if (volume->v_cdb != NULL) {
2046 cnid_close(volume->v_cdb);
2047 volume->v_cdb = NULL;
2053 /* ------------------------- */
2054 static void closevol(struct vol *vol)
2059 hash_free( vol->v_hash);
2060 dirfree( vol->v_root );
2062 if (vol->v_cdb != NULL) {
2063 cnid_close(vol->v_cdb);
2067 if (vol->v_postexec) {
2068 afprun(0, vol->v_postexec, NULL);
2070 if (vol->v_root_postexec) {
2071 afprun(1, vol->v_root_postexec, NULL);
2075 /* ------------------------- */
2076 void close_all_vol(void)
2080 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
2081 if ( (ovol->v_flags & AFPVOL_OPEN) ) {
2082 ovol->v_flags &= ~AFPVOL_OPEN;
2088 /* ------------------------- */
2089 static void deletevol(struct vol *vol)
2093 vol->v_flags &= ~AFPVOL_OPEN;
2094 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
2095 if ( (ovol->v_flags & AFPVOL_OPEN) ) {
2099 if ( ovol != NULL ) {
2100 /* Even if chdir fails, we can't say afp_closevol fails. */
2101 if ( chdir( ovol->v_path ) == 0 ) {
2102 curdir = ovol->v_dir;
2107 if (vol->v_deleted) {
2108 showvol(vol->v_name);
2115 /* ------------------------- */
2116 int afp_closevol(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2123 memcpy(&vid, ibuf, sizeof( vid ));
2124 if (NULL == ( vol = getvolbyvid( vid )) ) {
2125 return( AFPERR_PARAM );
2133 /* ------------------------- */
2134 struct vol *getvolbyvid(const u_int16_t vid )
2138 for ( vol = Volumes; vol; vol = vol->v_next ) {
2139 if ( vid == vol->v_vid ) {
2143 if ( vol == NULL || ( vol->v_flags & AFPVOL_OPEN ) == 0 ) {
2149 #endif /* FORCE_UIDGID */
2154 /* ------------------------ */
2155 static int ext_cmp_key(const void *key, const void *obj)
2157 const char *p = key;
2158 const struct extmap *em = obj;
2159 return strdiacasecmp(p, em->em_ext);
2161 struct extmap *getextmap(const char *path)
2166 if (!Extmap_cnt || NULL == ( p = strrchr( path, '.' )) ) {
2167 return( Defextmap );
2171 return( Defextmap );
2173 em = bsearch(p, Extmap, Extmap_cnt, sizeof(struct extmap), ext_cmp_key);
2177 return( Defextmap );
2181 /* ------------------------- */
2182 struct extmap *getdefextmap(void)
2184 return( Defextmap );
2187 /* --------------------------
2188 poll if a volume is changed by other processes.
2190 0 no attention msg sent
2191 1 attention msg sent
2192 -1 error (socket closed)
2194 Note: if attention return -1 no packet has been
2195 sent because the buffer is full, we don't care
2196 either there's no reader or there's a lot of
2197 traffic and another pollvoltime will follow
2199 int pollvoltime(AFPObj *obj)
2206 if (!(afp_version > 21 && obj->options.server_notif))
2209 if ( gettimeofday( &tv, NULL ) < 0 )
2212 for ( vol = Volumes; vol; vol = vol->v_next ) {
2213 if ( (vol->v_flags & AFPVOL_OPEN) && vol->v_mtime + 30 < tv.tv_sec) {
2214 if ( !stat( vol->v_path, &st ) && vol->v_mtime != st.st_mtime ) {
2215 vol->v_mtime = st.st_mtime;
2216 if (!obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED))
2225 /* ------------------------- */
2226 void setvoltime(AFPObj *obj, struct vol *vol)
2230 /* just looking at vol->v_mtime is broken seriously since updates
2231 * from other users afpd processes never are seen.
2232 * This is not the most elegant solution (a shared memory between
2233 * the afpd processes would come closer)
2236 if ( gettimeofday( &tv, NULL ) < 0 ) {
2237 LOG(log_error, logtype_afpd, "setvoltime(%s): gettimeofday: %s", vol->v_path, strerror(errno) );
2240 if( utime( vol->v_path, NULL ) < 0 ) {
2241 /* write of time failed ... probably a read only filesys,
2242 * where no other users can interfere, so there's no issue here
2246 /* a little granularity */
2247 if (vol->v_mtime < tv.tv_sec) {
2248 vol->v_mtime = tv.tv_sec;
2249 /* or finder doesn't update free space
2250 * XXX is it still true with newer OSX?
2252 if (afp_version > 21 && obj->options.server_notif) {
2253 obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED);
2258 /* ------------------------- */
2259 int afp_getvolparams(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_,char *rbuf, size_t *rbuflen)
2262 u_int16_t vid, bitmap;
2265 memcpy(&vid, ibuf, sizeof( vid ));
2266 ibuf += sizeof( vid );
2267 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2268 bitmap = ntohs( bitmap );
2270 if (NULL == ( vol = getvolbyvid( vid )) ) {
2272 return( AFPERR_PARAM );
2275 return stat_vol(bitmap, vol, rbuf, rbuflen);
2278 /* ------------------------- */
2279 int afp_setvolparams(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2283 u_int16_t vid, bitmap;
2289 memcpy(&vid, ibuf, sizeof( vid ));
2290 ibuf += sizeof( vid );
2291 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2292 bitmap = ntohs( bitmap );
2293 ibuf += sizeof(bitmap);
2295 if (( vol = getvolbyvid( vid )) == NULL ) {
2296 return( AFPERR_PARAM );
2299 if ((vol->v_flags & AFPVOL_RO))
2300 return AFPERR_VLOCK;
2302 /* we can only set the backup date. */
2303 if (bitmap != (1 << VOLPBIT_BDATE))
2304 return AFPERR_BITMAP;
2306 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2307 if ( ad_open( vol->v_path, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR,
2310 return AFPERR_VLOCK;
2312 return AFPERR_ACCESS;
2315 memcpy(&aint, ibuf, sizeof(aint));
2316 ad_setdate(&ad, AD_DATE_BACKUP, aint);
2318 ad_close(&ad, ADFLAGS_HF);
2322 /* ------------------------- */
2323 int wincheck(const struct vol *vol, const char *path)
2327 if (!(vol->v_flags & AFPVOL_MSWINDOWS))
2330 /* empty paths are not allowed */
2331 if ((len = strlen(path)) == 0)
2334 /* leading or trailing whitespaces are not allowed, carriage returns
2335 * and probably other whitespace is okay, tabs are not allowed
2337 if ((path[0] == ' ') || (path[len-1] == ' '))
2340 /* certain characters are not allowed */
2341 if (strpbrk(path, MSWINDOWS_BADCHARS))
2344 /* everything else is okay */
2350 * precreate a folder
2351 * this is only intended for folders in the volume root
2352 * It will *not* work if the folder name contains extended characters
2354 static int create_special_folder (const struct vol *vol, const struct _special_folder *folder)
2363 p = (char *) malloc ( strlen(vol->v_path)+strlen(folder->name)+2);
2365 LOG(log_error, logtype_afpd,"malloc failed");
2369 q=strdup(folder->name);
2371 LOG(log_error, logtype_afpd,"malloc failed");
2375 strcpy(p, vol->v_path);
2380 if ((vol->v_casefold & AFPVOL_MTOUUPPER))
2382 else if ((vol->v_casefold & AFPVOL_MTOULOWER))
2388 if ( (ret = stat( p, &st )) < 0 ) {
2389 if (folder->precreate) {
2390 if (ad_mkdir(p, folder->mode)) {
2391 LOG(log_debug, logtype_afpd,"Creating '%s' failed in %s: %s", p, vol->v_path, strerror(errno));
2400 if ( !ret && folder->hide) {
2402 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2403 if (ad_open( p, vol_noadouble(vol) | ADFLAGS_HF|ADFLAGS_DIR,
2404 O_RDWR|O_CREAT, 0666, &ad) < 0) {
2409 if ((ad_get_HF_flags( &ad ) & O_CREAT) ) {
2410 if (ad_getentryoff(&ad, ADEID_NAME)) {
2411 ad_setentrylen( &ad, ADEID_NAME, strlen(folder->name));
2412 memcpy(ad_entry( &ad, ADEID_NAME ), folder->name,
2413 ad_getentrylen( &ad, ADEID_NAME ));
2417 ad_getattr(&ad, &attr);
2418 attr |= htons( ntohs( attr ) | ATTRBIT_INVISIBLE );
2419 ad_setattr(&ad, attr);
2421 /* do the same with the finder info */
2422 if (ad_entry(&ad, ADEID_FINDERI)) {
2423 memcpy(&attr, ad_entry(&ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF, sizeof(attr));
2424 attr |= htons(FINDERINFO_INVISIBLE);
2425 memcpy(ad_entry(&ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF,&attr, sizeof(attr));
2429 ad_close( &ad, ADFLAGS_HF );
2436 static void handle_special_folders (const struct vol * vol)
2438 const _special_folder *p = &special_folders[0];
2440 if ((vol->v_flags & AFPVOL_RO))
2443 for (; p->name != NULL; p++) {
2444 create_special_folder (vol, p);
2449 * Save the volume options to a file, used by
2451 * Writing the file everytime a volume is opened is
2452 * unnecessary, but it shouldn't hurt much.
2454 static int savevoloptions (const struct vol *vol)
2457 char item[MAXPATHLEN];
2461 const _vol_opt_name *op = &vol_opt_names[0];
2462 const _vol_opt_name *cf = &vol_opt_casefold[0];
2464 strlcpy (item, vol->v_path, sizeof(item));
2465 strlcat (item, "/.AppleDesktop/", sizeof(item));
2466 strlcat (item, VOLINFOFILE, sizeof(item));
2468 if ((fd = open( item, O_RDWR | O_CREAT , 0666)) <0 ) {
2469 LOG(log_debug, logtype_afpd,"Error opening %s: %s", item, strerror(errno));
2473 /* try to get a lock */
2475 lock.l_whence = SEEK_SET;
2477 lock.l_type = F_WRLCK;
2479 if (fcntl(fd, F_SETLK, &lock) < 0) {
2480 if (errno == EACCES || errno == EAGAIN) {
2481 /* ignore, other process already writing the file */
2484 LOG(log_error, logtype_cnid, "savevoloptions: cannot get lock: %s", strerror(errno));
2489 /* write volume options */
2490 snprintf(buf, sizeof(buf), "MAC_CHARSET:%s\n", vol->v_maccodepage);
2491 snprintf(item, sizeof(item), "VOL_CHARSET:%s\n", vol->v_volcodepage);
2492 strlcat(buf, item, sizeof(buf));
2494 switch (vol->v_adouble) {
2496 strlcat(buf, "ADOUBLE_VER:v1\n", sizeof(buf));
2499 strlcat(buf, "ADOUBLE_VER:v2\n", sizeof(buf));
2501 case AD_VERSION2_OSX:
2502 strlcat(buf, "ADOUBLE_VER:osx\n", sizeof(buf));
2504 case AD_VERSION1_SFM:
2505 strlcat(buf, "ADOUBLE_VER:sfm\n", sizeof(buf));
2509 strlcat(buf, "CNIDBACKEND:", sizeof(buf));
2510 strlcat(buf, vol->v_cnidscheme, sizeof(buf));
2511 strlcat(buf, "\n", sizeof(buf));
2513 strlcat(buf, "CNIDDBDHOST:", sizeof(buf));
2514 strlcat(buf, Cnid_srv, sizeof(buf));
2515 strlcat(buf, "\n", sizeof(buf));
2517 strlcat(buf, "CNIDDBDPORT:", sizeof(buf));
2518 strlcat(buf, Cnid_port, sizeof(buf));
2519 strlcat(buf, "\n", sizeof(buf));
2521 strcpy(item, "CNID_DBPATH:");
2523 strlcat(item, vol->v_dbpath, sizeof(item));
2525 strlcat(item, vol->v_path, sizeof(item));
2526 strlcat(item, "\n", sizeof(item));
2527 strlcat(buf, item, sizeof(buf));
2530 strcpy(item, "VOLUME_OPTS:");
2531 for (;op->name; op++) {
2532 if ( (vol->v_flags & op->option) ) {
2533 strlcat(item, op->name, sizeof(item));
2534 strlcat(item, " ", sizeof(item));
2537 strlcat(item, "\n", sizeof(item));
2538 strlcat(buf, item, sizeof(buf));
2540 /* casefold flags */
2541 strcpy(item, "VOLCASEFOLD:");
2542 for (;cf->name; cf++) {
2543 if ( (vol->v_casefold & cf->option) ) {
2544 strlcat(item, cf->name, sizeof(item));
2545 strlcat(item, " ", sizeof(item));
2548 strlcat(item, "\n", sizeof(item));
2549 strlcat(buf, item, sizeof(buf));
2551 if (strlen(buf) >= sizeof(buf)-1)
2552 LOG(log_debug, logtype_afpd,"Error writing .volinfo file: buffer too small, %s", buf);
2555 if (write( fd, buf, strlen(buf)) < 0 || ftruncate(fd, strlen(buf)) < 0 ) {
2556 LOG(log_debug, logtype_afpd,"Error writing .volinfo file: %s", strerror(errno));
2559 lock.l_type = F_UNLCK;
2560 fcntl(fd, F_SETLK, &lock);