2 * Copyright (c) 1990,1993 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
8 #endif /* HAVE_CONFIG_H */
18 #include <sys/param.h>
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
25 #include <atalk/dsi.h>
26 #include <atalk/adouble.h>
27 #include <atalk/afp.h>
28 #include <atalk/util.h>
29 #include <atalk/volinfo.h>
30 #include <atalk/logger.h>
31 #include <atalk/vfs.h>
32 #include <atalk/uuid.h>
34 #include <atalk/bstrlib.h>
35 #include <atalk/bstradd.h>
36 #include <atalk/ftw.h>
37 #include <atalk/globals.h>
38 #include <atalk/fce_api.h>
39 #include <atalk/errchk.h>
42 #include <atalk/cnid.h>
45 #include "directory.h"
54 extern int afprun(int root, char *cmd, int *outfd);
57 #define MIN(a, b) ((a) < (b) ? (a) : (b))
60 #ifndef UUID_PRINTABLE_STRING_LENGTH
61 #define UUID_PRINTABLE_STRING_LENGTH 37
65 struct vol *current_vol; /* last volume from getvolbyvid() */
67 static struct vol *Volumes = NULL;
68 static uint16_t lastvid = 0;
69 static char *Trash = "\02\024Network Trash Folder";
71 static struct extmap *Extmap = NULL, *Defextmap = NULL;
72 static int Extmap_cnt;
73 static void free_extmap(void);
75 #define VOLOPT_ALLOW 0 /* user allow list */
76 #define VOLOPT_DENY 1 /* user deny list */
77 #define VOLOPT_RWLIST 2 /* user rw list */
78 #define VOLOPT_ROLIST 3 /* user ro list */
79 #define VOLOPT_PASSWORD 4 /* volume password */
80 #define VOLOPT_CASEFOLD 5 /* character case mangling */
81 #define VOLOPT_FLAGS 6 /* various flags */
82 #define VOLOPT_DBPATH 7 /* path to database */
83 #define VOLOPT_LIMITSIZE 8 /* Limit the size of the volume */
85 #define VOLOPT_VETO 10 /* list of veto filespec */
86 #define VOLOPT_PREEXEC 11 /* preexec command */
87 #define VOLOPT_ROOTPREEXEC 12 /* root preexec command */
88 #define VOLOPT_POSTEXEC 13 /* postexec command */
89 #define VOLOPT_ROOTPOSTEXEC 14 /* root postexec command */
90 #define VOLOPT_ENCODING 15 /* mac encoding (pre OSX)*/
91 #define VOLOPT_MACCHARSET 16
92 #define VOLOPT_CNIDSCHEME 17
93 #define VOLOPT_ADOUBLE 18 /* adouble version */
94 /* Usable slot: 19/20 */
95 #define VOLOPT_UMASK 21
96 #define VOLOPT_ALLOWED_HOSTS 22
97 #define VOLOPT_DENIED_HOSTS 23
98 #define VOLOPT_DPERM 24 /* dperm default directories perms */
99 #define VOLOPT_FPERM 25 /* fperm default files perms */
100 #define VOLOPT_DFLTPERM 26 /* perm */
101 #define VOLOPT_EA_VFS 27 /* Extended Attributes vfs indirection */
102 #define VOLOPT_CNIDSERVER 28 /* CNID Server ip address*/
103 #define VOLOPT_CNIDPORT 30 /* CNID server tcp port */
105 #define VOLOPT_MAX 31 /* <== IMPORTANT !!!!!! */
106 #define VOLOPT_NUM (VOLOPT_MAX + 1)
109 #define VOLOPT_DEFAULT ":DEFAULT:"
110 #define VOLOPT_DEFAULT_LEN 9
117 typedef struct _special_folder {
124 static const _special_folder special_folders[] = {
125 {"Network Trash Folder", 1, 0777, 1},
126 {".AppleDesktop", 1, 0777, 0},
129 /* Forward declarations */
130 static void handle_special_folders (const struct vol *);
131 static void deletevol(struct vol *vol);
132 static void volume_free(struct vol *vol);
133 static void check_ea_sys_support(struct vol *vol);
134 static char *get_vol_uuid(const AFPObj *obj, const char *volname);
135 static int readvolfile(AFPObj *obj, struct afp_volume_name *p1,char *p2, int user, struct passwd *pwent);
137 static void volfree(struct vol_option *options, const struct vol_option *save)
142 for (i = 0; i < VOLOPT_MAX; i++) {
143 if (options[i].c_value && (options[i].c_value != save[i].c_value))
144 free(options[i].c_value);
147 for (i = 0; i < VOLOPT_MAX; i++) {
148 if (options[i].c_value)
149 free(options[i].c_value);
155 #define is_var(a, b) (strncmp((a), (b), 2) == 0)
158 * Handle variable substitutions. here's what we understand:
159 * $b -> basename of path
160 * $c -> client ip/appletalk address
161 * $d -> volume pathname on server
162 * $f -> full name (whatever's in the gecos field)
165 * $i -> client ip/appletalk address without port
166 * $s -> server name (hostname if it doesn't exist)
167 * $u -> username (guest is usually nobody)
168 * $v -> volume name or basename if null
169 * $z -> zone (may not exist)
172 * This get's called from readvolfile with
173 * path = NULL, volname = NULL for xlating the volumes path
174 * path = path, volname = NULL for xlating the volumes name
175 * ... and from volumes options parsing code when xlating eg dbpath with
176 * path = path, volname = volname
178 * Using this information we can reject xlation of any variable depeninding on a login
179 * context which is not given in the afp master, where we must evaluate this whole stuff
180 * too for the Zeroconf announcements.
182 static char *volxlate(AFPObj *obj,
195 int xlatevolname = 0;
197 if (parent_or_child == 0)
200 if (path && !volname)
208 dest = calloc(destlen +1, 1);
214 strlcpy(dest, src, destlen +1);
215 if ((p = strchr(src, '$')) == NULL) /* nothing to do */
218 /* first part of the path. just forward to the next variable. */
219 len = MIN((size_t)(p - src), destlen);
225 while (p && destlen > 0) {
226 /* now figure out what the variable is */
228 if (is_var(p, "$b")) {
229 if (afpmaster && xlatevolname)
232 if ((q = strrchr(path, '/')) == NULL)
234 else if (*(q + 1) != '\0')
237 } else if (is_var(p, "$c")) {
238 if (afpmaster && xlatevolname)
240 DSI *dsi = obj->handle;
241 len = sprintf(dest, "%s:%u",
242 getip_string((struct sockaddr *)&dsi->client),
243 getip_port((struct sockaddr *)&dsi->client));
246 } else if (is_var(p, "$d")) {
247 if (afpmaster && xlatevolname)
250 } else if (pwd && is_var(p, "$f")) {
251 if (afpmaster && xlatevolname)
253 if ((r = strchr(pwd->pw_gecos, ',')))
256 } else if (pwd && is_var(p, "$g")) {
257 if (afpmaster && xlatevolname)
259 struct group *grp = getgrgid(pwd->pw_gid);
262 } else if (is_var(p, "$h")) {
263 q = obj->options.hostname;
264 } else if (is_var(p, "$i")) {
265 if (afpmaster && xlatevolname)
267 DSI *dsi = obj->handle;
268 q = getip_string((struct sockaddr *)&dsi->client);
269 } else if (is_var(p, "$s")) {
272 else if (obj->options.server) {
273 q = obj->options.server;
275 q = obj->options.hostname;
276 } else if (obj->username && is_var(p, "$u")) {
277 if (afpmaster && xlatevolname)
280 if ( obj->options.ntseparator && (sep = strchr(obj->username, obj->options.ntseparator[0])) != NULL)
284 } else if (is_var(p, "$v")) {
285 if (afpmaster && xlatevolname)
291 if ((q = strrchr(path, '/')) == NULL)
293 else if (*(q + 1) != '\0')
296 } else if (is_var(p, "$z")) {
298 } else if (is_var(p, "$$")) {
303 /* copy the stuff over. if we don't understand something that we
304 * should, just skip it over. */
306 len = MIN(p == q ? 2 : strlen(q), destlen);
307 strncpy(dest, q, len);
312 /* stuff up to next $ */
314 p = strchr(src, '$');
315 len = p ? MIN((size_t)(p - src), destlen) : destlen;
317 strncpy(dest, src, len);
325 /* to make sure that val is valid, make sure to select an opt that
327 static int optionok(const char *buf, const char *opt, const char *val)
329 if (!strstr(buf,opt))
337 /* -------------------- */
338 static void setoption(struct vol_option *options, struct vol_option *save, int opt, const char *val)
340 if (options[opt].c_value && (!save || options[opt].c_value != save[opt].c_value))
341 free(options[opt].c_value);
342 options[opt].c_value = strdup(val + 1);
345 /* ------------------------------------------
346 handle all the options. tmp can't be NULL. */
347 static void volset(struct vol_option *options, struct vol_option *save,
348 char *volname, int vlen,
353 val = strchr(tmp, ':');
355 /* we'll assume it's a volume name. */
356 strncpy(volname, tmp, vlen);
361 LOG(log_debug, logtype_afpd, "Parsing volset %s", val);
363 if (optionok(tmp, "allow:", val)) {
364 setoption(options, save, VOLOPT_ALLOW, val);
366 } else if (optionok(tmp, "deny:", val)) {
367 setoption(options, save, VOLOPT_DENY, val);
369 } else if (optionok(tmp, "rwlist:", val)) {
370 setoption(options, save, VOLOPT_RWLIST, val);
372 } else if (optionok(tmp, "rolist:", val)) {
373 setoption(options, save, VOLOPT_ROLIST, val);
375 } else if (optionok(tmp, "codepage:", val)) {
376 LOG (log_error, logtype_afpd, "The old codepage system has been removed. Please make sure to read the documentation !!!!");
377 /* Make sure we don't screw anything */
379 } else if (optionok(tmp, "volcharset:", val)) {
380 setoption(options, save, VOLOPT_ENCODING, val);
381 } else if (optionok(tmp, "maccharset:", val)) {
382 setoption(options, save, VOLOPT_MACCHARSET, val);
383 } else if (optionok(tmp, "veto:", val)) {
384 setoption(options, save, VOLOPT_VETO, val);
385 } else if (optionok(tmp, "cnidscheme:", val)) {
386 setoption(options, save, VOLOPT_CNIDSCHEME, val);
387 } else if (optionok(tmp, "casefold:", val)) {
388 if (strcasecmp(val + 1, "tolower") == 0)
389 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMLOWER;
390 else if (strcasecmp(val + 1, "toupper") == 0)
391 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMUPPER;
392 else if (strcasecmp(val + 1, "xlatelower") == 0)
393 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UUPPERMLOWER;
394 else if (strcasecmp(val + 1, "xlateupper") == 0)
395 options[VOLOPT_CASEFOLD].i_value = AFPVOL_ULOWERMUPPER;
396 } else if (optionok(tmp, "adouble:", val)) {
397 if (strcasecmp(val + 1, "v2") == 0)
398 options[VOLOPT_ADOUBLE].i_value = AD_VERSION2;
399 else if (strcasecmp(val + 1, "ea") == 0)
400 options[VOLOPT_ADOUBLE].i_value = AD_VERSION_EA;
401 } else if (optionok(tmp, "options:", val)) {
404 if ((p = strtok(val + 1, ",")) == NULL) /* nothing */
408 if (strcasecmp(p, "prodos") == 0)
409 options[VOLOPT_FLAGS].i_value |= AFPVOL_A2VOL;
410 else if (strcasecmp(p, "mswindows") == 0) {
411 options[VOLOPT_FLAGS].i_value |= AFPVOL_MSWINDOWS | AFPVOL_USEDOTS;
412 } else if (strcasecmp(p, "crlf") == 0)
413 options[VOLOPT_FLAGS].i_value |= AFPVOL_CRLF;
414 else if (strcasecmp(p, "noadouble") == 0)
415 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOADOUBLE;
416 else if (strcasecmp(p, "ro") == 0)
417 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
418 else if (strcasecmp(p, "nohex") == 0)
419 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOHEX;
420 else if (strcasecmp(p, "usedots") == 0)
421 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS;
422 else if (strcasecmp(p, "invisibledots") == 0)
423 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS | AFPVOL_INV_DOTS;
424 else if (strcasecmp(p, "limitsize") == 0)
425 options[VOLOPT_FLAGS].i_value |= AFPVOL_LIMITSIZE;
426 else if (strcasecmp(p, "nofileid") == 0)
427 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOFILEID;
428 else if (strcasecmp(p, "nostat") == 0)
429 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOSTAT;
430 else if (strcasecmp(p, "preexec_close") == 0)
431 options[VOLOPT_PREEXEC].i_value = 1;
432 else if (strcasecmp(p, "root_preexec_close") == 0)
433 options[VOLOPT_ROOTPREEXEC].i_value = 1;
434 else if (strcasecmp(p, "upriv") == 0)
435 options[VOLOPT_FLAGS].i_value |= AFPVOL_UNIX_PRIV;
436 else if (strcasecmp(p, "nodev") == 0)
437 options[VOLOPT_FLAGS].i_value |= AFPVOL_NODEV;
438 else if (strcasecmp(p, "caseinsensitive") == 0)
439 options[VOLOPT_FLAGS].i_value |= AFPVOL_CASEINSEN;
440 else if (strcasecmp(p, "illegalseq") == 0)
441 options[VOLOPT_FLAGS].i_value |= AFPVOL_EILSEQ;
442 else if (strcasecmp(p, "nocnidcache") == 0)
443 options[VOLOPT_FLAGS].i_value &= ~AFPVOL_CACHE;
444 else if (strcasecmp(p, "tm") == 0)
445 options[VOLOPT_FLAGS].i_value |= AFPVOL_TM;
446 else if (strcasecmp(p, "searchdb") == 0)
447 options[VOLOPT_FLAGS].i_value |= AFPVOL_SEARCHDB;
448 else if (strcasecmp(p, "nonetids") == 0)
449 options[VOLOPT_FLAGS].i_value |= AFPVOL_NONETIDS;
450 else if (strcasecmp(p, "noacls") == 0)
451 options[VOLOPT_FLAGS].i_value &= ~AFPVOL_ACLS;
452 p = strtok(NULL, ",");
455 } else if (optionok(tmp, "cnidserver:", val)) {
456 setoption(options, save, VOLOPT_CNIDSERVER, val);
458 char *p = strrchr(val + 1, ':');
461 setoption(options, save, VOLOPT_CNIDPORT, p);
464 LOG(log_debug, logtype_afpd, "CNID Server for volume '%s': %s:%s",
465 volname, val + 1, p ? p + 1 : Cnid_port);
467 } else if (optionok(tmp, "dbpath:", val)) {
468 setoption(options, save, VOLOPT_DBPATH, val);
470 } else if (optionok(tmp, "umask:", val)) {
471 options[VOLOPT_UMASK].i_value = (int)strtol(val +1, NULL, 8);
472 } else if (optionok(tmp, "dperm:", val)) {
473 options[VOLOPT_DPERM].i_value = (int)strtol(val+1, NULL, 8);
474 } else if (optionok(tmp, "fperm:", val)) {
475 options[VOLOPT_FPERM].i_value = (int)strtol(val+1, NULL, 8);
476 } else if (optionok(tmp, "perm:", val)) {
477 options[VOLOPT_DFLTPERM].i_value = (int)strtol(val+1, NULL, 8);
478 } else if (optionok(tmp, "password:", val)) {
479 setoption(options, save, VOLOPT_PASSWORD, val);
480 } else if (optionok(tmp, "root_preexec:", val)) {
481 setoption(options, save, VOLOPT_ROOTPREEXEC, val);
483 } else if (optionok(tmp, "preexec:", val)) {
484 setoption(options, save, VOLOPT_PREEXEC, val);
486 } else if (optionok(tmp, "root_postexec:", val)) {
487 setoption(options, save, VOLOPT_ROOTPOSTEXEC, val);
489 } else if (optionok(tmp, "postexec:", val)) {
490 setoption(options, save, VOLOPT_POSTEXEC, val);
492 } else if (optionok(tmp, "allowed_hosts:", val)) {
493 setoption(options, save, VOLOPT_ALLOWED_HOSTS, val);
495 } else if (optionok(tmp, "denied_hosts:", val)) {
496 setoption(options, save, VOLOPT_DENIED_HOSTS, val);
498 } else if (optionok(tmp, "ea:", val)) {
499 if (strcasecmp(val + 1, "ad") == 0)
500 options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_AD;
501 else if (strcasecmp(val + 1, "sys") == 0)
502 options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_SYS;
503 else if (strcasecmp(val + 1, "none") == 0)
504 options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_NONE;
506 } else if (optionok(tmp, "volsizelimit:", val)) {
507 options[VOLOPT_LIMITSIZE].i_value = (uint32_t)strtoul(val + 1, NULL, 10);
510 /* ignore unknown options */
511 LOG(log_debug, logtype_afpd, "ignoring unknown volume option: %s", tmp);
516 /* ----------------- */
517 static void showvol(const ucs2_t *name)
520 for ( volume = Volumes; volume; volume = volume->v_next ) {
521 if (volume->v_hide && !strcasecmp_w( volume->v_name, name ) ) {
528 /* ------------------------------- */
529 static int creatvol(AFPObj *obj, struct passwd *pwd,
530 char *path, char *name,
531 struct vol_option *options,
532 const int user /* user defined volume */
536 int suffixlen, vlen, tmpvlen, u8mvlen, macvlen;
538 char tmpname[AFPVOL_U8MNAMELEN+1];
539 ucs2_t u8mtmpname[(AFPVOL_U8MNAMELEN+1)*2], mactmpname[(AFPVOL_MACNAMELEN+1)*2];
540 char suffix[6]; /* max is #FFFF */
543 LOG(log_debug, logtype_afpd, "createvol: Volume '%s'", name);
545 if ( name == NULL || *name == '\0' ) {
546 if ((name = strrchr( path, '/' )) == NULL) {
547 return -1; /* Obviously not a fully qualified path */
550 /* if you wish to share /, you need to specify a name. */
555 /* suffix for mangling use (lastvid + 1) */
556 /* because v_vid has not been decided yet. */
557 suffixlen = sprintf(suffix, "%c%X", MANGLE_CHAR, lastvid + 1 );
559 vlen = strlen( name );
561 /* Unicode Volume Name */
562 /* Firstly convert name from unixcharset to UTF8-MAC */
564 tmpvlen = convert_charset(obj->options.unixcharset, CH_UTF8_MAC, 0, name, vlen, tmpname, AFPVOL_U8MNAMELEN, &flags);
566 strcpy(tmpname, "???");
570 /* Do we have to mangle ? */
571 if ( (flags & CONV_REQMANGLE) || (tmpvlen > obj->options.volnamelen)) {
572 if (tmpvlen + suffixlen > obj->options.volnamelen) {
574 tmpvlen = convert_charset(obj->options.unixcharset, CH_UTF8_MAC, 0, name, vlen, tmpname, obj->options.volnamelen - suffixlen, &flags);
575 tmpname[tmpvlen >= 0 ? tmpvlen : 0] = 0;
577 strcat(tmpname, suffix);
578 tmpvlen = strlen(tmpname);
581 /* Secondly convert name from UTF8-MAC to UCS2 */
582 if ( 0 >= ( u8mvlen = convert_string(CH_UTF8_MAC, CH_UCS2, tmpname, tmpvlen, u8mtmpname, AFPVOL_U8MNAMELEN*2)) )
585 LOG(log_maxdebug, logtype_afpd, "createvol: Volume '%s' -> UTF8-MAC Name: '%s'", name, tmpname);
587 /* Maccharset Volume Name */
588 /* Firsty convert name from unixcharset to maccharset */
590 tmpvlen = convert_charset(obj->options.unixcharset, obj->options.maccharset, 0, name, vlen, tmpname, AFPVOL_U8MNAMELEN, &flags);
592 strcpy(tmpname, "???");
596 /* Do we have to mangle ? */
597 if ( (flags & CONV_REQMANGLE) || (tmpvlen > AFPVOL_MACNAMELEN)) {
598 if (tmpvlen + suffixlen > AFPVOL_MACNAMELEN) {
600 tmpvlen = convert_charset(obj->options.unixcharset,
601 obj->options.maccharset,
606 AFPVOL_MACNAMELEN - suffixlen,
608 tmpname[tmpvlen >= 0 ? tmpvlen : 0] = 0;
610 strcat(tmpname, suffix);
611 tmpvlen = strlen(tmpname);
614 /* Secondly convert name from maccharset to UCS2 */
615 if ( 0 >= ( macvlen = convert_string(obj->options.maccharset,
620 AFPVOL_U8MNAMELEN*2)) )
623 LOG(log_maxdebug, logtype_afpd, "createvol: Volume '%s' -> Longname: '%s'", name, tmpname);
625 /* check duplicate */
626 for ( volume = Volumes; volume; volume = volume->v_next ) {
627 if ((utf8_encoding() && (strcasecmp_w(volume->v_u8mname, u8mtmpname) == 0))
629 (!utf8_encoding() && (strcasecmp_w(volume->v_macname, mactmpname) == 0))) {
630 LOG (log_error, logtype_afpd,
631 "Duplicate volume name, check AppleVolumes files: previous: \"%s\", new: \"%s\"",
632 volume->v_localname, name);
633 if (volume->v_deleted) {
634 volume->v_new = hide = 1;
637 return -1; /* Won't be able to access it, anyway... */
642 if (!( volume = (struct vol *)calloc(1, sizeof( struct vol ))) ) {
643 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
646 if ( NULL == ( volume->v_localname = strdup(name))) {
647 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
652 if ( NULL == ( volume->v_u8mname = strdup_w(u8mtmpname))) {
653 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
658 if ( NULL == ( volume->v_macname = strdup_w(mactmpname))) {
659 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
664 if (!( volume->v_path = (char *)malloc( strlen( path ) + 1 )) ) {
665 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
671 volume->v_name = utf8_encoding()?volume->v_u8mname:volume->v_macname;
672 volume->v_hide = hide;
673 strcpy( volume->v_path, path );
677 #endif /* __svr4__ */
678 /* os X start at 1 and use network order ie. 1 2 3 */
679 volume->v_vid = ++lastvid;
680 volume->v_vid = htons(volume->v_vid);
682 if (!check_vol_acl_support(volume)) {
683 LOG(log_debug, logtype_afpd, "creatvol(\"%s\"): disabling ACL support", volume->v_path);
684 options[VOLOPT_FLAGS].i_value &= ~AFPVOL_ACLS;
690 volume->v_casefold = options[VOLOPT_CASEFOLD].i_value;
691 volume->v_flags |= options[VOLOPT_FLAGS].i_value;
693 if (options[VOLOPT_EA_VFS].i_value)
694 volume->v_vfs_ea = options[VOLOPT_EA_VFS].i_value;
696 volume->v_ad_options = 0;
697 if ((volume->v_flags & AFPVOL_NODEV))
698 volume->v_ad_options |= ADVOL_NODEV;
699 if ((volume->v_flags & AFPVOL_CACHE))
700 volume->v_ad_options |= ADVOL_CACHE;
701 if ((volume->v_flags & AFPVOL_UNIX_PRIV))
702 volume->v_ad_options |= ADVOL_UNIXPRIV;
703 if ((volume->v_flags & AFPVOL_INV_DOTS))
704 volume->v_ad_options |= ADVOL_INVDOTS;
705 if ((volume->v_flags & AFPVOL_NOADOUBLE))
706 volume->v_ad_options |= ADVOL_NOADOUBLE;
708 if (options[VOLOPT_PASSWORD].c_value)
709 volume->v_password = strdup(options[VOLOPT_PASSWORD].c_value);
711 if (options[VOLOPT_VETO].c_value)
712 volume->v_veto = strdup(options[VOLOPT_VETO].c_value);
714 if (options[VOLOPT_ENCODING].c_value)
715 volume->v_volcodepage = strdup(options[VOLOPT_ENCODING].c_value);
717 if (options[VOLOPT_MACCHARSET].c_value)
718 volume->v_maccodepage = strdup(options[VOLOPT_MACCHARSET].c_value);
720 if (options[VOLOPT_DBPATH].c_value)
721 volume->v_dbpath = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_DBPATH].c_value, pwd, path, name);
723 if (options[VOLOPT_CNIDSCHEME].c_value)
724 volume->v_cnidscheme = strdup(options[VOLOPT_CNIDSCHEME].c_value);
726 if (options[VOLOPT_CNIDSERVER].c_value)
727 volume->v_cnidserver = strdup(options[VOLOPT_CNIDSERVER].c_value);
729 if (options[VOLOPT_CNIDPORT].c_value)
730 volume->v_cnidport = strdup(options[VOLOPT_CNIDPORT].c_value);
732 if (options[VOLOPT_UMASK].i_value)
733 volume->v_umask = (mode_t)options[VOLOPT_UMASK].i_value;
735 if (options[VOLOPT_DPERM].i_value)
736 volume->v_dperm = (mode_t)options[VOLOPT_DPERM].i_value;
738 if (options[VOLOPT_FPERM].i_value)
739 volume->v_fperm = (mode_t)options[VOLOPT_FPERM].i_value;
741 if (options[VOLOPT_DFLTPERM].i_value)
742 volume->v_perm = (mode_t)options[VOLOPT_DFLTPERM].i_value;
744 if (options[VOLOPT_ADOUBLE].i_value)
745 volume->v_adouble = options[VOLOPT_ADOUBLE].i_value;
747 volume->v_adouble = AD_VERSION;
749 if (options[VOLOPT_LIMITSIZE].i_value)
750 volume->v_limitsize = options[VOLOPT_LIMITSIZE].i_value;
752 /* Mac to Unix conversion flags*/
753 volume->v_mtou_flags = 0;
754 if (!(volume->v_flags & AFPVOL_NOHEX))
755 volume->v_mtou_flags |= CONV_ESCAPEHEX;
756 if (!(volume->v_flags & AFPVOL_USEDOTS))
757 volume->v_mtou_flags |= CONV_ESCAPEDOTS;
758 if ((volume->v_flags & AFPVOL_EILSEQ))
759 volume->v_mtou_flags |= CONV__EILSEQ;
761 if ((volume->v_casefold & AFPVOL_MTOUUPPER))
762 volume->v_mtou_flags |= CONV_TOUPPER;
763 else if ((volume->v_casefold & AFPVOL_MTOULOWER))
764 volume->v_mtou_flags |= CONV_TOLOWER;
766 /* Unix to Mac conversion flags*/
767 volume->v_utom_flags = CONV_IGNORE | CONV_UNESCAPEHEX;
768 if ((volume->v_casefold & AFPVOL_UTOMUPPER))
769 volume->v_utom_flags |= CONV_TOUPPER;
770 else if ((volume->v_casefold & AFPVOL_UTOMLOWER))
771 volume->v_utom_flags |= CONV_TOLOWER;
773 if ((volume->v_flags & AFPVOL_EILSEQ))
774 volume->v_utom_flags |= CONV__EILSEQ;
777 if (options[VOLOPT_PREEXEC].c_value)
778 volume->v_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_PREEXEC].c_value, pwd, path, name);
779 volume->v_preexec_close = options[VOLOPT_PREEXEC].i_value;
781 if (options[VOLOPT_POSTEXEC].c_value)
782 volume->v_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_POSTEXEC].c_value, pwd, path, name);
784 if (options[VOLOPT_ROOTPREEXEC].c_value)
785 volume->v_root_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPREEXEC].c_value, pwd, path, name);
786 volume->v_root_preexec_close = options[VOLOPT_ROOTPREEXEC].i_value;
788 if (options[VOLOPT_ROOTPOSTEXEC].c_value)
789 volume->v_root_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPOSTEXEC].c_value, pwd, path, name);
792 volume->v_dperm |= volume->v_perm;
793 volume->v_fperm |= volume->v_perm;
795 /* Check EA support on volume */
796 if (volume->v_vfs_ea == AFPVOL_EA_AUTO)
797 check_ea_sys_support(volume);
800 /* get/store uuid from file in afpd master*/
801 if ((parent_or_child == 0) && (volume->v_flags & AFPVOL_TM)) {
802 char *uuid = get_vol_uuid(obj, volume->v_localname);
804 LOG(log_error, logtype_afpd, "Volume '%s': couldn't get UUID",
805 volume->v_localname);
807 volume->v_uuid = uuid;
808 LOG(log_debug, logtype_afpd, "Volume '%s': UUID '%s'",
809 volume->v_localname, volume->v_uuid);
813 volume->v_next = Volumes;
818 /* ---------------- */
819 static char *myfgets( char *buf, int size, FILE *fp)
825 while ((EOF != ( c = getc( fp )) ) && ( size > 1 )) {
826 if ( c == '\n' || c == '\r' ) {
827 if (p != buf && *(p -1) == '\\') {
849 /* check access list. this function wants something of the following
851 * @group,name,name2,@group2,name3
853 * a NULL argument allows everybody to have access.
854 * we return three things:
856 * 0: list exists, but name isn't in it
860 #ifndef NO_REAL_USER_NAME
861 /* authentication is case insensitive
862 * FIXME should we do the same with group name?
864 #define access_strcmp strcasecmp
867 #define access_strcmp strcmp
871 static int accessvol(const char *args, const char *name)
873 char buf[MAXPATHLEN + 1], *p;
879 strlcpy(buf, args, sizeof(buf));
880 if ((p = strtok(buf, ",")) == NULL) /* nothing, return okay */
884 if (*p == '@') { /* it's a group */
885 if ((gr = getgrnam(p + 1)) && gmem(gr->gr_gid))
887 } else if (access_strcmp(p, name) == 0) /* it's a user name */
889 p = strtok(NULL, ",");
895 static int hostaccessvol(int type, const char *volname, const char *args, const AFPObj *obj)
898 char buf[MAXPATHLEN + 1], *p, *b;
899 DSI *dsi = obj->handle;
900 struct sockaddr_storage client;
905 strlcpy(buf, args, sizeof(buf));
906 if ((p = strtok_r(buf, ",", &b)) == NULL) /* nothing, return okay */
909 if (obj->proto != AFPPROTO_DSI)
914 char *ipaddr, *mask_char;
915 struct addrinfo hints, *ai;
917 ipaddr = strtok(p, "/");
918 mask_char = strtok(NULL,"/");
920 /* Get address from string with getaddrinfo */
921 memset(&hints, 0, sizeof hints);
922 hints.ai_family = AF_UNSPEC;
923 hints.ai_socktype = SOCK_STREAM;
924 if ((ret = getaddrinfo(ipaddr, NULL, &hints, &ai)) != 0) {
925 LOG(log_error, logtype_afpd, "hostaccessvol: getaddrinfo: %s\n", gai_strerror(ret));
930 if (mask_char != NULL)
931 mask_int = atoi(mask_char); /* apply_ip_mask does range checking on it */
933 if (ai->ai_family == AF_INET) /* IPv4 */
939 /* Apply mask to addresses */
940 client = dsi->client;
941 apply_ip_mask((struct sockaddr *)&client, mask_int);
942 apply_ip_mask(ai->ai_addr, mask_int);
944 if (compare_ip((struct sockaddr *)&client, ai->ai_addr) == 0) {
945 if (type == VOLOPT_DENIED_HOSTS)
946 LOG(log_info, logtype_afpd, "AFP access denied for client IP '%s' to volume '%s' by denied list",
947 getip_string((struct sockaddr *)&client), volname);
954 p = strtok_r(NULL, ",", &b);
957 if (type == VOLOPT_ALLOWED_HOSTS)
958 LOG(log_info, logtype_afpd, "AFP access denied for client IP '%s' to volume '%s', not in allowed list",
959 getip_string((struct sockaddr *)&dsi->client), volname);
963 static void setextmap(char *ext, char *type, char *creator, int user)
968 if (Extmap == NULL) {
969 if (( Extmap = calloc(1, sizeof( struct extmap ))) == NULL ) {
970 LOG(log_error, logtype_afpd, "setextmap: calloc: %s", strerror(errno) );
975 for ( em = Extmap, cnt = 0; em->em_ext; em++, cnt++) {
976 if ( (strdiacasecmp( em->em_ext, ext )) == 0 ) {
981 if ( em->em_ext == NULL ) {
982 if (!(Extmap = realloc( Extmap, sizeof( struct extmap ) * (cnt +2))) ) {
983 LOG(log_error, logtype_afpd, "setextmap: realloc: %s", strerror(errno) );
986 (Extmap +cnt +1)->em_ext = NULL;
988 } else if ( !user ) {
994 if (!(em->em_ext = strdup( ext))) {
995 LOG(log_error, logtype_afpd, "setextmap: strdup: %s", strerror(errno) );
999 if ( *type == '\0' ) {
1000 memcpy(em->em_type, "\0\0\0\0", sizeof( em->em_type ));
1002 memcpy(em->em_type, type, sizeof( em->em_type ));
1004 if ( *creator == '\0' ) {
1005 memcpy(em->em_creator, "\0\0\0\0", sizeof( em->em_creator ));
1007 memcpy(em->em_creator, creator, sizeof( em->em_creator ));
1011 /* -------------------------- */
1012 static int extmap_cmp(const void *map1, const void *map2)
1014 const struct extmap *em1 = map1;
1015 const struct extmap *em2 = map2;
1016 return strdiacasecmp(em1->em_ext, em2->em_ext);
1019 static void sortextmap( void)
1024 if ((em = Extmap) == NULL) {
1027 while (em->em_ext) {
1032 qsort(Extmap, Extmap_cnt, sizeof(struct extmap), extmap_cmp);
1033 if (*Extmap->em_ext == 0) {
1034 /* the first line is really "." the default entry,
1035 * we remove the leading '.' in setextmap
1042 /* ----------------------
1044 static void free_extmap( void)
1049 for ( em = Extmap; em->em_ext; em++) {
1059 /* ----------------------
1061 static int volfile_changed(struct afp_volume_name *p)
1067 name = p->full_name;
1071 if (!stat( name, &st) && st.st_mtime > p->mtime) {
1072 p->mtime = st.st_mtime;
1078 /* ----------------------
1079 * Read a volume configuration file and add the volumes contained within to
1080 * the global volume list. This gets called from the forked afpd childs.
1081 * The master now reads this too for Zeroconf announcements.
1083 * If p2 is non-NULL, the file that is opened is
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]
1093 static int readvolfile(AFPObj *obj, struct afp_volume_name *p1, char *p2, int user, struct passwd *pwent)
1096 char path[MAXPATHLEN + 1];
1097 char tmp[MAXPATHLEN + 1];
1098 char volname[AFPVOL_U8MNAMELEN + 1];
1100 char type[5], creator[5];
1105 struct vol_option save_options[VOLOPT_NUM];
1106 struct vol_option default_options[VOLOPT_NUM];
1107 struct vol_option options[VOLOPT_NUM];
1113 strcpy( path, p1->name );
1115 strcat( path, "/" );
1117 if (p1->full_name) {
1118 free(p1->full_name);
1120 p1->full_name = strdup(path);
1123 if (NULL == ( fp = fopen( path, "r" )) ) {
1127 if (fd != -1 && !fstat( fd, &st) ) {
1128 p1->mtime = st.st_mtime;
1131 /* try putting a read lock on the volume file twice, sleep 1 second if first attempt fails */
1134 if ((read_lock(fd, 0, SEEK_SET, 0)) != 0) {
1137 LOG(log_error, logtype_afpd, "readvolfile: can't lock volume file \"%s\"", path);
1138 if ( fclose( fp ) != 0 ) {
1139 LOG(log_error, logtype_afpd, "readvolfile: fclose: %s", strerror(errno) );
1149 memset(default_options, 0, sizeof(default_options));
1151 /* Enable some default options for all volumes */
1152 default_options[VOLOPT_FLAGS].i_value |= AFPVOL_CACHE;
1154 default_options[VOLOPT_FLAGS].i_value |= AFPVOL_ACLS;
1156 default_options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_AUTO;
1157 LOG(log_maxdebug, logtype_afpd, "readvolfile: seeding default umask: %04o",
1158 obj->options.umask);
1159 default_options[VOLOPT_UMASK].i_value = obj->options.umask;
1160 memcpy(save_options, default_options, sizeof(options));
1162 LOG(log_debug, logtype_afpd, "readvolfile: \"%s\"", path);
1164 while ( myfgets( buf, sizeof( buf ), fp ) != NULL ) {
1165 initline( strlen( buf ), buf );
1166 parseline( sizeof( path ) - 1, path );
1173 /* change the default options for this file */
1174 if (strncmp(path, VOLOPT_DEFAULT, VOLOPT_DEFAULT_LEN) == 0) {
1175 volfree(default_options, save_options);
1176 memcpy(default_options, save_options, sizeof(options));
1178 for (i = 0; i < VOLOPT_NUM; i++) {
1179 if (parseline( sizeof( path ) - VOLOPT_DEFAULT_LEN - 1,
1180 path + VOLOPT_DEFAULT_LEN) < 0)
1182 volset(default_options, NULL, tmp, sizeof(tmp) - 1,
1183 path + VOLOPT_DEFAULT_LEN);
1189 if (( p = strchr( path, '/' )) != NULL ) {
1197 if ( u == NULL || *u == '\0' || ( pw = getpwnam( u )) == NULL ) {
1200 strcpy( tmp, pw->pw_dir );
1201 if ( p != NULL && *p != '\0' ) {
1208 /* send path through variable substitution */
1209 if (*path != '~') /* need to copy path to tmp */
1211 if (!pwent && obj->username)
1212 pwent = getpwnam(obj->username);
1214 if (volxlate(obj, path, sizeof(path) - 1, tmp, pwent, NULL, NULL) == NULL)
1217 /* this is sort of braindead. basically, i want to be
1218 * able to specify things in any order, but i don't want to
1219 * re-write everything. */
1221 memcpy(options, default_options, sizeof(options));
1224 /* read in up to VOLOP_NUM possible options */
1225 for (i = 0; i < VOLOPT_NUM; i++) {
1226 if (parseline( sizeof( tmp ) - 1, tmp ) < 0)
1229 volset(options, default_options, volname, sizeof(volname) - 1, tmp);
1232 /* check allow/deny lists (if not afpd master loading volumes for Zeroconf reg.):
1233 allow -> either no list (-1), or in list (1)
1234 deny -> either no list (-1), or not in list (0) */
1235 if (parent_or_child == 0
1237 (accessvol(options[VOLOPT_ALLOW].c_value, obj->username) &&
1238 (accessvol(options[VOLOPT_DENY].c_value, obj->username) < 1) &&
1239 hostaccessvol(VOLOPT_ALLOWED_HOSTS, volname, options[VOLOPT_ALLOWED_HOSTS].c_value, obj) &&
1240 (hostaccessvol(VOLOPT_DENIED_HOSTS, volname, options[VOLOPT_DENIED_HOSTS].c_value, obj) < 1))) {
1242 /* handle read-only behaviour. semantics:
1243 * 1) neither the rolist nor the rwlist exist -> rw
1244 * 2) rolist exists -> ro if user is in it.
1245 * 3) rwlist exists -> ro unless user is in it. */
1246 if (parent_or_child == 1
1248 ((options[VOLOPT_FLAGS].i_value & AFPVOL_RO) == 0)
1250 ((accessvol(options[VOLOPT_ROLIST].c_value, obj->username) == 1) ||
1251 !accessvol(options[VOLOPT_RWLIST].c_value, obj->username)))
1252 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
1254 /* do variable substitution for volname */
1255 if (volxlate(obj, tmp, sizeof(tmp) - 1, volname, pwent, path, NULL) == NULL)
1258 creatvol(obj, pwent, path, tmp, options, p2 != NULL);
1260 volfree(options, default_options);
1264 parseline( sizeof( type ) - 1, type );
1265 parseline( sizeof( creator ) - 1, creator );
1266 setextmap( path, type, creator, user);
1273 volfree(save_options, NULL);
1275 if ( fclose( fp ) != 0 ) {
1276 LOG(log_error, logtype_afpd, "readvolfile: fclose: %s", strerror(errno) );
1282 /* ------------------------------- */
1283 static void volume_free(struct vol *vol)
1285 free(vol->v_localname);
1286 vol->v_localname = NULL;
1287 free(vol->v_u8mname);
1288 vol->v_u8mname = NULL;
1289 free(vol->v_macname);
1290 vol->v_macname = NULL;
1292 free(vol->v_password);
1294 free(vol->v_volcodepage);
1295 free(vol->v_maccodepage);
1296 free(vol->v_cnidscheme);
1297 free(vol->v_dbpath);
1303 /* ------------------------------- */
1304 static void free_volumes(void )
1307 struct vol *nvol, *ovol;
1309 for ( vol = Volumes; vol; vol = vol->v_next ) {
1310 if (( vol->v_flags & AFPVOL_OPEN ) ) {
1317 for ( vol = Volumes, ovol = NULL; vol; vol = nvol) {
1320 if (vol->v_localname == NULL) {
1321 if (Volumes == vol) {
1326 ovol->v_next = nvol;
1336 /* ------------------------------- */
1337 static void volume_unlink(struct vol *volume)
1339 struct vol *vol, *ovol, *nvol;
1341 if (volume == Volumes) {
1342 Volumes = Volumes->v_next;
1345 for ( vol = Volumes->v_next, ovol = Volumes; vol; vol = nvol) {
1348 if (vol == volume) {
1349 ovol->v_next = nvol;
1358 * Read band-size info from Info.plist XML file of an TM sparsebundle
1360 * @param path (r) path to Info.plist file
1361 * @return band-size in bytes, -1 on error
1363 static long long int get_tm_bandsize(const char *path)
1368 long long int bandsize = -1;
1370 EC_NULL_LOGSTR( file = fopen(path, "r"),
1371 "get_tm_bandsize(\"%s\"): %s",
1372 path, strerror(errno) );
1374 while (fgets(buf, sizeof(buf), file) != NULL) {
1375 if (strstr(buf, "band-size") == NULL)
1378 if (fscanf(file, " <integer>%lld</integer>", &bandsize) != 1) {
1379 LOG(log_error, logtype_afpd, "get_tm_bandsize(\"%s\"): can't parse band-size", path);
1388 LOG(log_debug, logtype_afpd, "get_tm_bandsize(\"%s\"): bandsize: %lld", path, bandsize);
1393 * Return number on entries in a directory
1395 * @param path (r) path to dir
1396 * @return number of entries, -1 on error
1398 static long long int get_tm_bands(const char *path)
1401 long long int count = 0;
1403 const struct dirent *entry;
1405 EC_NULL( dir = opendir(path) );
1407 while ((entry = readdir(dir)) != NULL)
1409 count -= 2; /* All OSens I'm aware of return "." and "..", so just substract them, avoiding string comparison in loop */
1420 * Calculate used size of a TimeMachine volume
1422 * This assumes that the volume is used only for TimeMachine.
1424 * 1) readdir(path of volume)
1425 * 2) for every element that matches regex "\(.*\)\.sparsebundle$" :
1426 * 3) parse "\1.sparsebundle/Info.plist" and read the band-size XML key integer value
1427 * 4) readdir "\1.sparsebundle/bands/" counting files
1428 * 5) calculate used size as: (file_count - 1) * band-size
1430 * The result of the calculation is returned in "volume->v_tm_used".
1431 * "volume->v_appended" gets reset to 0.
1432 * "volume->v_tm_cachetime" is updated with the current time from time(NULL).
1434 * "volume->v_tm_used" is cached for TM_USED_CACHETIME seconds and updated by
1435 * "volume->v_appended". The latter is increased by X every time the client
1436 * appends X bytes to a file (in fork.c).
1438 * @param vol (rw) volume to calculate
1439 * @return 0 on success, -1 on error
1441 #define TM_USED_CACHETIME 60 /* cache for 60 seconds */
1442 static int get_tm_used(struct vol * restrict vol)
1445 long long int bandsize;
1447 bstring infoplist = NULL;
1448 bstring bandsdir = NULL;
1450 const struct dirent *entry;
1454 time_t now = time(NULL);
1456 if (vol->v_tm_cachetime
1457 && ((vol->v_tm_cachetime + TM_USED_CACHETIME) >= now)) {
1458 if (vol->v_tm_used == -1)
1460 vol->v_tm_used += vol->v_appended;
1461 vol->v_appended = 0;
1462 LOG(log_debug, logtype_afpd, "getused(\"%s\"): cached: %" PRIu64 " bytes",
1463 vol->v_path, vol->v_tm_used);
1467 vol->v_tm_cachetime = now;
1469 EC_NULL( dir = opendir(vol->v_path) );
1471 while ((entry = readdir(dir)) != NULL) {
1472 if (((p = strstr(entry->d_name, "sparsebundle")) != NULL)
1473 && (strlen(entry->d_name) == (p + strlen("sparsebundle") - entry->d_name))) {
1475 EC_NULL_LOG( infoplist = bformat("%s/%s/%s", vol->v_path, entry->d_name, "Info.plist") );
1477 if ((bandsize = get_tm_bandsize(cfrombstr(infoplist))) == -1)
1480 EC_NULL_LOG( bandsdir = bformat("%s/%s/%s/", vol->v_path, entry->d_name, "bands") );
1482 if ((links = get_tm_bands(cfrombstr(bandsdir))) == -1)
1485 used += (links - 1) * bandsize;
1486 LOG(log_debug, logtype_afpd, "getused(\"%s\"): bands: %" PRIu64 " bytes",
1487 cfrombstr(bandsdir), used);
1491 vol->v_tm_used = used;
1495 bdestroy(infoplist);
1501 LOG(log_debug, logtype_afpd, "getused(\"%s\"): %" PRIu64 " bytes", vol->v_path, vol->v_tm_used);
1506 static int getvolspace(struct vol *vol,
1507 uint32_t *bfree, uint32_t *btotal,
1508 VolSpace *xbfree, VolSpace *xbtotal, uint32_t *bsize)
1513 #ifndef NO_QUOTA_SUPPORT
1514 VolSpace qfree, qtotal;
1517 spaceflag = AFPVOL_GVSMASK & vol->v_flags;
1518 /* report up to 2GB if afp version is < 2.2 (4GB if not) */
1519 maxsize = (vol->v_flags & AFPVOL_A2VOL) ? 0x01fffe00 :
1520 (((afp_version < 22) || (vol->v_flags & AFPVOL_LIMITSIZE))
1521 ? 0x7fffffffL : 0xffffffffL);
1524 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_AFSGVS ) {
1525 if ( afs_getvolspace( vol, xbfree, xbtotal, bsize ) == AFP_OK ) {
1526 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_AFSGVS;
1527 goto getvolspace_done;
1532 if (( rc = ustatfs_getvolspace( vol, xbfree, xbtotal, bsize)) != AFP_OK ) {
1536 #ifndef NO_QUOTA_SUPPORT
1537 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_UQUOTA ) {
1538 if ( uquota_getvolspace( vol, &qfree, &qtotal, *bsize ) == AFP_OK ) {
1539 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_UQUOTA;
1540 *xbfree = MIN(*xbfree, qfree);
1541 *xbtotal = MIN(*xbtotal, qtotal);
1542 goto getvolspace_done;
1546 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_USTATFS;
1549 if (vol->v_limitsize) {
1550 if (get_tm_used(vol) != 0)
1553 *xbtotal = MIN(*xbtotal, (vol->v_limitsize * 1024 * 1024));
1554 *xbfree = MIN(*xbfree, *xbtotal < vol->v_tm_used ? 0 : *xbtotal - vol->v_tm_used);
1556 LOG(log_debug, logtype_afpd,
1557 "volparams: total: %" PRIu64 ", used: %" PRIu64 ", free: %" PRIu64 " bytes",
1558 *xbtotal, vol->v_tm_used, *xbfree);
1561 *bfree = MIN(*xbfree, maxsize);
1562 *btotal = MIN(*xbtotal, maxsize);
1566 #define FCE_TM_DELTA 10 /* send notification every 10 seconds */
1567 void vol_fce_tm_event(void)
1570 time_t now = time(NULL);
1571 struct vol *vol = Volumes;
1573 if ((last + FCE_TM_DELTA) < now) {
1575 for ( ; vol; vol = vol->v_next ) {
1576 if (vol->v_flags & AFPVOL_TM)
1577 (void)fce_register_tm_size(vol->v_path, vol->v_tm_used + vol->v_appended);
1582 /* -----------------------
1583 * set volume creation date
1584 * avoid duplicate, well at least it tries
1586 static void vol_setdate(uint16_t id, struct adouble *adp, time_t date)
1589 struct vol *vol = Volumes;
1591 for ( volume = Volumes; volume; volume = volume->v_next ) {
1592 if (volume->v_vid == id) {
1595 else if ((time_t)(AD_DATE_FROM_UNIX(date)) == volume->v_ctime) {
1597 volume = Volumes; /* restart */
1600 vol->v_ctime = AD_DATE_FROM_UNIX(date);
1601 ad_setdate(adp, AD_DATE_CREATE | AD_DATE_UNIX, date);
1604 /* ----------------------- */
1605 static int getvolparams( uint16_t bitmap, struct vol *vol, struct stat *st, char *buf, size_t *buflen)
1608 int bit = 0, isad = 1;
1611 uint32_t bfree, btotal, bsize;
1612 VolSpace xbfree, xbtotal; /* extended bytes */
1613 char *data, *nameoff = NULL;
1616 LOG(log_debug, logtype_afpd, "getvolparams: Volume '%s'", vol->v_localname);
1618 /* courtesy of jallison@whistle.com:
1619 * For MacOS8.x support we need to create the
1620 * .Parent file here if it doesn't exist. */
1623 if (ad_open(&ad, vol->v_path, ADFLAGS_HF | ADFLAGS_DIR | ADFLAGS_RDWR | ADFLAGS_CREATE, 0666) != 0 ) {
1625 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1627 } else if (ad_get_MD_flags( &ad ) & O_CREAT) {
1628 slash = strrchr( vol->v_path, '/' );
1632 slash = vol->v_path;
1633 if (ad_getentryoff(&ad, ADEID_NAME)) {
1634 ad_setentrylen( &ad, ADEID_NAME, strlen( slash ));
1635 memcpy(ad_entry( &ad, ADEID_NAME ), slash,
1636 ad_getentrylen( &ad, ADEID_NAME ));
1638 vol_setdate(vol->v_vid, &ad, st->st_mtime);
1642 if (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0)
1643 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1645 vol->v_ctime = aint;
1648 if (( bitmap & ( (1<<VOLPBIT_BFREE)|(1<<VOLPBIT_BTOTAL) |
1649 (1<<VOLPBIT_XBFREE)|(1<<VOLPBIT_XBTOTAL) |
1650 (1<<VOLPBIT_BSIZE)) ) != 0 ) {
1651 if ( getvolspace( vol, &bfree, &btotal, &xbfree, &xbtotal,
1652 &bsize) != AFP_OK ) {
1654 ad_close( &ad, ADFLAGS_HF );
1656 return( AFPERR_PARAM );
1661 while ( bitmap != 0 ) {
1662 while (( bitmap & 1 ) == 0 ) {
1670 /* check for read-only.
1671 * NOTE: we don't actually set the read-only flag unless
1672 * it's passed in that way as it's possible to mount
1673 * a read-write filesystem under a read-only one. */
1674 if ((vol->v_flags & AFPVOL_RO) ||
1675 ((utime(vol->v_path, NULL) < 0) && (errno == EROFS))) {
1676 ashort |= VOLPBIT_ATTR_RO;
1678 /* prior 2.1 only VOLPBIT_ATTR_RO is defined */
1679 if (afp_version > 20) {
1680 if (0 == (vol->v_flags & AFPVOL_NOFILEID) && vol->v_cdb != NULL &&
1681 (vol->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1682 ashort |= VOLPBIT_ATTR_FILEID;
1684 ashort |= VOLPBIT_ATTR_CATSEARCH;
1686 if (afp_version >= 30) {
1687 ashort |= VOLPBIT_ATTR_UTF8;
1688 if (vol->v_flags & AFPVOL_UNIX_PRIV)
1689 ashort |= VOLPBIT_ATTR_UNIXPRIV;
1690 if (vol->v_flags & AFPVOL_TM)
1691 ashort |= VOLPBIT_ATTR_TM;
1692 if (vol->v_flags & AFPVOL_NONETIDS)
1693 ashort |= VOLPBIT_ATTR_NONETIDS;
1694 if (afp_version >= 32) {
1696 ashort |= VOLPBIT_ATTR_EXT_ATTRS;
1697 if (vol->v_flags & AFPVOL_ACLS)
1698 ashort |= VOLPBIT_ATTR_ACLS;
1702 ashort = htons(ashort);
1703 memcpy(data, &ashort, sizeof( ashort ));
1704 data += sizeof( ashort );
1708 ashort = htons( AFPVOLSIG_DEFAULT );
1709 memcpy(data, &ashort, sizeof( ashort ));
1710 data += sizeof( ashort );
1713 case VOLPBIT_CDATE :
1714 aint = vol->v_ctime;
1715 memcpy(data, &aint, sizeof( aint ));
1716 data += sizeof( aint );
1719 case VOLPBIT_MDATE :
1720 if ( st->st_mtime > vol->v_mtime ) {
1721 vol->v_mtime = st->st_mtime;
1723 aint = AD_DATE_FROM_UNIX(vol->v_mtime);
1724 memcpy(data, &aint, sizeof( aint ));
1725 data += sizeof( aint );
1728 case VOLPBIT_BDATE :
1729 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1730 aint = AD_DATE_START;
1731 memcpy(data, &aint, sizeof( aint ));
1732 data += sizeof( aint );
1736 memcpy(data, &vol->v_vid, sizeof( vol->v_vid ));
1737 data += sizeof( vol->v_vid );
1740 case VOLPBIT_BFREE :
1741 bfree = htonl( bfree );
1742 memcpy(data, &bfree, sizeof( bfree ));
1743 data += sizeof( bfree );
1746 case VOLPBIT_BTOTAL :
1747 btotal = htonl( btotal );
1748 memcpy(data, &btotal, sizeof( btotal ));
1749 data += sizeof( btotal );
1752 #ifndef NO_LARGE_VOL_SUPPORT
1753 case VOLPBIT_XBFREE :
1754 xbfree = hton64( xbfree );
1755 memcpy(data, &xbfree, sizeof( xbfree ));
1756 data += sizeof( xbfree );
1759 case VOLPBIT_XBTOTAL :
1760 xbtotal = hton64( xbtotal );
1761 memcpy(data, &xbtotal, sizeof( xbtotal ));
1762 data += sizeof( xbfree );
1764 #endif /* ! NO_LARGE_VOL_SUPPORT */
1768 data += sizeof( uint16_t );
1771 case VOLPBIT_BSIZE: /* block size */
1772 bsize = htonl(bsize);
1773 memcpy(data, &bsize, sizeof(bsize));
1774 data += sizeof(bsize);
1779 ad_close( &ad, ADFLAGS_HF );
1781 return( AFPERR_BITMAP );
1787 ashort = htons( data - buf );
1788 memcpy(nameoff, &ashort, sizeof( ashort ));
1789 /* name is always in mac charset */
1790 aint = ucs2_to_charset( vol->v_maccharset, vol->v_macname, data+1, AFPVOL_MACNAMELEN + 1);
1800 ad_close(&ad, ADFLAGS_HF);
1802 *buflen = data - buf;
1806 /* ------------------------- */
1807 static int stat_vol(uint16_t bitmap, struct vol *vol, char *rbuf, size_t *rbuflen)
1813 if ( stat( vol->v_path, &st ) < 0 ) {
1815 return( AFPERR_PARAM );
1817 /* save the volume device number */
1818 vol->v_dev = st.st_dev;
1820 buflen = *rbuflen - sizeof( bitmap );
1821 if (( ret = getvolparams( bitmap, vol, &st,
1822 rbuf + sizeof( bitmap ), &buflen )) != AFP_OK ) {
1826 *rbuflen = buflen + sizeof( bitmap );
1827 bitmap = htons( bitmap );
1828 memcpy(rbuf, &bitmap, sizeof( bitmap ));
1833 /* ------------------------------- */
1834 void load_volumes(AFPObj *obj)
1836 struct passwd *pwent;
1841 /* check files date */
1842 if (obj->options.defaultvol.loaded) {
1843 changed = volfile_changed(&obj->options.defaultvol);
1845 if (obj->options.systemvol.loaded) {
1846 changed |= volfile_changed(&obj->options.systemvol);
1848 if (obj->options.uservol.loaded) {
1849 changed |= volfile_changed(&obj->options.uservol);
1858 if (parent_or_child == 0) {
1859 LOG(log_debug, logtype_afpd, "load_volumes: AFP MASTER");
1861 LOG(log_debug, logtype_afpd, "load_volumes: user: %s", obj->username);
1864 pwent = getpwnam(obj->username);
1865 if ( (obj->options.flags & OPTION_USERVOLFIRST) == 0 ) {
1866 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent);
1869 if ((*obj->username == '\0') || (obj->options.flags & OPTION_NOUSERVOL)) {
1870 readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent);
1873 * Read user's AppleVolumes or .AppleVolumes file
1874 * If neither are readable, read the default volumes file. if
1875 * that doesn't work, create a user share.
1877 if (obj->options.uservol.name) {
1878 free(obj->options.uservol.name);
1880 obj->options.uservol.name = strdup(pwent->pw_dir);
1881 if ( readvolfile(obj, &obj->options.uservol, "AppleVolumes", 1, pwent) < 0 &&
1882 readvolfile(obj, &obj->options.uservol, ".AppleVolumes", 1, pwent) < 0 &&
1883 readvolfile(obj, &obj->options.uservol, "applevolumes", 1, pwent) < 0 &&
1884 readvolfile(obj, &obj->options.uservol, ".applevolumes", 1, pwent) < 0 &&
1885 obj->options.defaultvol.name != NULL ) {
1886 if (readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent) < 0)
1887 creatvol(obj, pwent, pwent->pw_dir, NULL, NULL, 1);
1890 if ( obj->options.flags & OPTION_USERVOLFIRST ) {
1891 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent );
1894 if ( obj->options.closevol ) {
1897 for ( vol = Volumes; vol; vol = vol->v_next ) {
1898 if (vol->v_deleted && !vol->v_new ) {
1907 /* ------------------------------- */
1908 int afp_getsrvrparms(AFPObj *obj, char *ibuf _U_, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
1921 for ( vcnt = 0, volume = Volumes; volume; volume = volume->v_next ) {
1922 if (!(volume->v_flags & AFPVOL_NOSTAT)) {
1925 if ( stat( volume->v_path, &st ) < 0 ) {
1926 LOG(log_info, logtype_afpd, "afp_getsrvrparms(%s): stat: %s",
1927 volume->v_path, strerror(errno) );
1928 continue; /* can't access directory */
1930 if (!S_ISDIR(st.st_mode)) {
1931 continue; /* not a dir */
1933 accessmode(volume, volume->v_path, &ma, NULL, &st);
1934 if ((ma.ma_user & (AR_UREAD | AR_USEARCH)) != (AR_UREAD | AR_USEARCH)) {
1935 continue; /* no r-x access */
1938 if (volume->v_hide) {
1939 continue; /* config file changed but the volume was mounted */
1942 if (utf8_encoding()) {
1943 len = ucs2_to_charset_allocate(CH_UTF8_MAC, &namebuf, volume->v_u8mname);
1945 len = ucs2_to_charset_allocate(obj->options.maccharset, &namebuf, volume->v_macname);
1948 if (len == (size_t)-1)
1951 /* set password bit if there's a volume password */
1952 *data = (volume->v_password) ? AFPSRVR_PASSWD : 0;
1954 /* Apple 2 clients running ProDOS-8 expect one volume to have
1955 bit 0 of this byte set. They will not recognize anything
1956 on the server unless this is the case. I have not
1957 completely worked this out, but it's related to booting
1958 from the server. Support for that function is a ways
1959 off.. <shirsch@ibm.net> */
1960 *data |= (volume->v_flags & AFPVOL_A2VOL) ? AFPSRVR_CONFIGINFO : 0;
1961 *data++ |= 0; /* UNIX PRIVS BIT ..., OSX doesn't seem to use it, so we don't either */
1963 memcpy(data, namebuf, len );
1969 *rbuflen = data - rbuf;
1971 if ( gettimeofday( &tv, NULL ) < 0 ) {
1972 LOG(log_error, logtype_afpd, "afp_getsrvrparms(%s): gettimeofday: %s", volume->v_path, strerror(errno) );
1974 return AFPERR_PARAM;
1976 tv.tv_sec = AD_DATE_FROM_UNIX(tv.tv_sec);
1977 memcpy(data, &tv.tv_sec, sizeof( uint32_t));
1978 data += sizeof( uint32_t);
1983 /* ------------------------- */
1984 static int volume_codepage(AFPObj *obj, struct vol *volume)
1986 struct charset_functions *charset;
1989 if (!volume->v_volcodepage)
1990 volume->v_volcodepage = strdup("UTF8");
1992 if ( (charset_t) -1 == ( volume->v_volcharset = add_charset(volume->v_volcodepage)) ) {
1993 LOG (log_error, logtype_afpd, "Setting codepage %s as volume codepage failed", volume->v_volcodepage);
1997 if ( NULL == (charset = find_charset_functions(volume->v_volcodepage)) || charset->flags & CHARSET_ICONV ) {
1998 LOG (log_warning, logtype_afpd, "WARNING: volume encoding %s is *not* supported by netatalk, expect problems !!!!", volume->v_volcodepage);
2001 if (!volume->v_maccodepage)
2002 volume->v_maccodepage = strdup(obj->options.maccodepage);
2004 if ( (charset_t) -1 == ( volume->v_maccharset = add_charset(volume->v_maccodepage)) ) {
2005 LOG (log_error, logtype_afpd, "Setting codepage %s as mac codepage failed", volume->v_maccodepage);
2009 if ( NULL == ( charset = find_charset_functions(volume->v_maccodepage)) || ! (charset->flags & CHARSET_CLIENT) ) {
2010 LOG (log_error, logtype_afpd, "Fatal error: mac charset %s not supported", volume->v_maccodepage);
2013 volume->v_kTextEncoding = htonl(charset->kTextEncoding);
2017 /* ------------------------- */
2018 static int volume_openDB(struct vol *volume)
2022 if ((volume->v_flags & AFPVOL_NODEV)) {
2023 flags |= CNID_FLAG_NODEV;
2026 if (volume->v_cnidscheme == NULL) {
2027 volume->v_cnidscheme = strdup(DEFAULT_CNID_SCHEME);
2028 LOG(log_info, logtype_afpd, "Volume %s use CNID scheme %s.",
2029 volume->v_path, volume->v_cnidscheme);
2032 LOG(log_info, logtype_afpd, "CNID server: %s:%s",
2033 volume->v_cnidserver ? volume->v_cnidserver : Cnid_srv,
2034 volume->v_cnidport ? volume->v_cnidport : Cnid_port);
2037 /* Found this in branch dir-rewrite, maybe we want to use it sometimes */
2039 /* Legacy pre 2.1 way of sharing eg CD-ROM */
2040 if (strcmp(volume->v_cnidscheme, "last") == 0) {
2041 /* "last" is gone. We support it by switching to in-memory "tdb" */
2042 volume->v_cnidscheme = strdup("tdb");
2043 flags |= CNID_FLAG_MEMORY;
2046 /* New way of sharing CD-ROM */
2047 if (volume->v_flags & AFPVOL_CDROM) {
2048 flags |= CNID_FLAG_MEMORY;
2049 if (strcmp(volume->v_cnidscheme, "tdb") != 0) {
2050 free(volume->v_cnidscheme);
2051 volume->v_cnidscheme = strdup("tdb");
2052 LOG(log_info, logtype_afpd, "Volume %s is ejectable, switching to scheme %s.",
2053 volume->v_path, volume->v_cnidscheme);
2058 volume->v_cdb = cnid_open(volume->v_path,
2060 volume->v_cnidscheme,
2062 volume->v_cnidserver ? volume->v_cnidserver : Cnid_srv,
2063 volume->v_cnidport ? volume->v_cnidport : Cnid_port);
2065 if ( ! volume->v_cdb && ! (flags & CNID_FLAG_MEMORY)) {
2066 /* The first attempt failed and it wasn't yet an attempt to open in-memory */
2067 LOG(log_error, logtype_afpd, "Can't open volume \"%s\" CNID backend \"%s\" ",
2068 volume->v_path, volume->v_cnidscheme);
2069 LOG(log_error, logtype_afpd, "Reopen volume %s using in memory temporary CNID DB.",
2071 flags |= CNID_FLAG_MEMORY;
2072 volume->v_cdb = cnid_open (volume->v_path, volume->v_umask, "tdb", flags, NULL, NULL);
2074 /* kill ourself with SIGUSR2 aka msg pending */
2075 if (volume->v_cdb) {
2076 setmessage("Something wrong with the volume's CNID DB, using temporary CNID DB instead."
2077 "Check server messages for details!");
2078 kill(getpid(), SIGUSR2);
2079 /* deactivate cnid caching/storing in AppleDouble files */
2080 volume->v_flags &= ~AFPVOL_CACHE;
2085 return (!volume->v_cdb)?-1:0;
2089 Check if the underlying filesystem supports EAs for ea:sys volumes.
2090 If not, switch to ea:ad.
2091 As we can't check (requires write access) on ro-volumes, we switch ea:auto
2092 volumes that are options:ro to ea:none.
2094 static void check_ea_sys_support(struct vol *vol)
2096 uid_t process_uid = 0;
2097 char eaname[] = {"org.netatalk.supports-eas.XXXXXX"};
2098 const char *eacontent = "yes";
2100 if (vol->v_vfs_ea == AFPVOL_EA_AUTO) {
2102 if ((vol->v_flags & AFPVOL_RO) == AFPVOL_RO) {
2103 LOG(log_info, logtype_afpd, "read-only volume '%s', can't test for EA support, disabling EAs", vol->v_localname);
2104 vol->v_vfs_ea = AFPVOL_EA_NONE;
2110 process_uid = geteuid();
2112 if (seteuid(0) == -1) {
2113 LOG(log_error, logtype_afpd, "check_ea_sys_support: can't seteuid(0): %s", strerror(errno));
2117 if ((sys_setxattr(vol->v_path, eaname, eacontent, 4, 0)) == 0) {
2118 sys_removexattr(vol->v_path, eaname);
2119 vol->v_vfs_ea = AFPVOL_EA_SYS;
2121 LOG(log_warning, logtype_afpd, "volume \"%s\" does not support Extended Attributes, using ea:ad instead",
2123 vol->v_vfs_ea = AFPVOL_EA_AD;
2127 if (seteuid(process_uid) == -1) {
2128 LOG(log_error, logtype_afpd, "can't seteuid back %s", strerror(errno));
2135 /* -------------------------
2136 * we are the user here
2138 int afp_openvol(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2149 char path[ MAXPATHLEN + 1];
2155 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2156 bitmap = ntohs( bitmap );
2157 ibuf += sizeof( bitmap );
2160 if (( bitmap & (1<<VOLPBIT_VID)) == 0 ) {
2161 return AFPERR_BITMAP;
2164 len = (unsigned char)*ibuf++;
2165 volname = obj->oldtmp;
2167 if ((volname_tmp = strchr(volname,'+')) != NULL)
2168 volname = volname_tmp+1;
2170 if (utf8_encoding()) {
2171 namelen = convert_string(CH_UTF8_MAC, CH_UCS2, ibuf, len, volname, sizeof(obj->oldtmp));
2173 namelen = convert_string(obj->options.maccharset, CH_UCS2, ibuf, len, volname, sizeof(obj->oldtmp));
2176 if ( namelen <= 0) {
2177 return AFPERR_PARAM;
2181 if ((len + 1) & 1) /* pad to an even boundary */
2186 for ( volume = Volumes; volume; volume = volume->v_next ) {
2187 if ( strcasecmp_w( (ucs2_t*) volname, volume->v_name ) == 0 ) {
2192 if ( volume == NULL ) {
2193 return AFPERR_PARAM;
2196 /* check for a volume password */
2197 if (volume->v_password && strncmp(ibuf, volume->v_password, VOLPASSLEN)) {
2198 return AFPERR_ACCESS;
2201 if (( volume->v_flags & AFPVOL_OPEN ) ) {
2202 /* the volume is already open */
2203 return stat_vol(bitmap, volume, rbuf, rbuflen);
2206 if (volume->v_root_preexec) {
2207 if ((ret = afprun(1, volume->v_root_preexec, NULL)) && volume->v_root_preexec_close) {
2208 LOG(log_error, logtype_afpd, "afp_openvol(%s): root preexec : %d", volume->v_path, ret );
2213 if (volume->v_preexec) {
2214 if ((ret = afprun(0, volume->v_preexec, NULL)) && volume->v_preexec_close) {
2215 LOG(log_error, logtype_afpd, "afp_openvol(%s): preexec : %d", volume->v_path, ret );
2220 if ( stat( volume->v_path, &st ) < 0 ) {
2221 return AFPERR_PARAM;
2224 if ( chdir( volume->v_path ) < 0 ) {
2225 return AFPERR_PARAM;
2228 if ( NULL == getcwd(path, MAXPATHLEN)) {
2229 /* shouldn't be fatal but it will fail later */
2230 LOG(log_error, logtype_afpd, "afp_openvol(%s): volume pathlen too long", volume->v_path);
2234 /* Normalize volume path */
2235 #ifdef REALPATH_TAKES_NULL
2236 if ((volume->v_path = realpath(path, NULL)) == NULL)
2239 if ((volume->v_path = malloc(MAXPATHLEN+1)) == NULL)
2241 if (realpath(path, volume->v_path) == NULL) {
2242 free(volume->v_path);
2245 /* Safe some memory */
2247 if ((tmp = strdup(volume->v_path)) == NULL) {
2248 free(volume->v_path);
2251 free(volume->v_path);
2252 volume->v_path = tmp;
2255 if (volume_codepage(obj, volume) < 0) {
2260 /* initialize volume variables
2263 if (utf8_encoding()) {
2264 volume->max_filename = UTF8FILELEN_EARLY;
2267 volume->max_filename = MACFILELEN;
2270 volume->v_flags |= AFPVOL_OPEN;
2271 volume->v_cdb = NULL;
2273 if (utf8_encoding()) {
2274 len = convert_string_allocate(CH_UCS2, CH_UTF8_MAC, volume->v_u8mname, namelen, &vol_mname);
2276 len = convert_string_allocate(CH_UCS2, obj->options.maccharset, volume->v_macname, namelen, &vol_mname);
2278 if ( !vol_mname || len <= 0) {
2283 if ((vol_uname = strrchr(path, '/')) == NULL)
2285 else if (*(vol_uname + 1) != '\0')
2288 if ((dir = dir_new(vol_mname,
2293 bfromcstr(volume->v_path),
2297 LOG(log_error, logtype_afpd, "afp_openvol(%s): malloc: %s", volume->v_path, strerror(errno) );
2302 volume->v_root = dir;
2305 if (volume_openDB(volume) < 0) {
2306 LOG(log_error, logtype_afpd, "Fatal error: cannot open CNID or invalid CNID backend for %s: %s",
2307 volume->v_path, volume->v_cnidscheme);
2312 ret = stat_vol(bitmap, volume, rbuf, rbuflen);
2314 if (ret == AFP_OK) {
2315 handle_special_folders(volume);
2317 volume->v_cnidserver ? volume->v_cnidserver : Cnid_srv,
2318 volume->v_cnidport ? volume->v_cnidport : Cnid_port);
2322 * If you mount a volume twice, the second time the trash appears on
2323 * the desk-top. That's because the Mac remembers the DID for the
2324 * trash (even for volumes in different zones, on different servers).
2325 * Just so this works better, we prime the DID cache with the trash,
2326 * fixing the trash at DID 17.
2327 * FIXME (RL): should it be done inside a CNID backend ? (always returning Trash DID when asked) ?
2329 if ((volume->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
2331 /* FIXME find db time stamp */
2332 if (cnid_getstamp(volume->v_cdb, volume->v_stamp, sizeof(volume->v_stamp)) < 0) {
2333 LOG (log_error, logtype_afpd,
2334 "afp_openvol(%s): Fatal error: Unable to get stamp value from CNID backend",
2342 cname( volume, volume->v_root, &p );
2348 if (volume->v_root) {
2349 dir_free( volume->v_root );
2350 volume->v_root = NULL;
2353 volume->v_flags &= ~AFPVOL_OPEN;
2354 if (volume->v_cdb != NULL) {
2355 cnid_close(volume->v_cdb);
2356 volume->v_cdb = NULL;
2362 /* ------------------------- */
2363 static void closevol(struct vol *vol)
2368 dir_free( vol->v_root );
2370 if (vol->v_cdb != NULL) {
2371 cnid_close(vol->v_cdb);
2375 if (vol->v_postexec) {
2376 afprun(0, vol->v_postexec, NULL);
2378 if (vol->v_root_postexec) {
2379 afprun(1, vol->v_root_postexec, NULL);
2383 /* ------------------------- */
2384 void close_all_vol(void)
2388 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
2389 if ( (ovol->v_flags & AFPVOL_OPEN) ) {
2390 ovol->v_flags &= ~AFPVOL_OPEN;
2396 /* ------------------------- */
2397 static void deletevol(struct vol *vol)
2401 vol->v_flags &= ~AFPVOL_OPEN;
2402 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
2403 if ( (ovol->v_flags & AFPVOL_OPEN) ) {
2407 if ( ovol != NULL ) {
2408 /* Even if chdir fails, we can't say afp_closevol fails. */
2409 if ( chdir( ovol->v_path ) == 0 ) {
2410 curdir = ovol->v_root;
2415 if (vol->v_deleted) {
2416 showvol(vol->v_name);
2423 /* ------------------------- */
2424 int afp_closevol(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2431 memcpy(&vid, ibuf, sizeof( vid ));
2432 if (NULL == ( vol = getvolbyvid( vid )) ) {
2433 return( AFPERR_PARAM );
2442 /* ------------------------- */
2443 struct vol *getvolbyvid(const uint16_t vid )
2447 for ( vol = Volumes; vol; vol = vol->v_next ) {
2448 if ( vid == vol->v_vid ) {
2452 if ( vol == NULL || ( vol->v_flags & AFPVOL_OPEN ) == 0 ) {
2461 /* ------------------------ */
2462 static int ext_cmp_key(const void *key, const void *obj)
2464 const char *p = key;
2465 const struct extmap *em = obj;
2466 return strdiacasecmp(p, em->em_ext);
2468 struct extmap *getextmap(const char *path)
2473 if (!Extmap_cnt || NULL == ( p = strrchr( path, '.' )) ) {
2474 return( Defextmap );
2478 return( Defextmap );
2480 em = bsearch(p, Extmap, Extmap_cnt, sizeof(struct extmap), ext_cmp_key);
2484 return( Defextmap );
2488 /* ------------------------- */
2489 struct extmap *getdefextmap(void)
2491 return( Defextmap );
2494 /* --------------------------
2495 poll if a volume is changed by other processes.
2497 0 no attention msg sent
2498 1 attention msg sent
2499 -1 error (socket closed)
2501 Note: if attention return -1 no packet has been
2502 sent because the buffer is full, we don't care
2503 either there's no reader or there's a lot of
2504 traffic and another pollvoltime will follow
2506 int pollvoltime(AFPObj *obj)
2513 if (!(afp_version > 21 && obj->options.server_notif))
2516 if ( gettimeofday( &tv, NULL ) < 0 )
2519 for ( vol = Volumes; vol; vol = vol->v_next ) {
2520 if ( (vol->v_flags & AFPVOL_OPEN) && vol->v_mtime + 30 < tv.tv_sec) {
2521 if ( !stat( vol->v_path, &st ) && vol->v_mtime != st.st_mtime ) {
2522 vol->v_mtime = st.st_mtime;
2523 if (!obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED))
2532 /* ------------------------- */
2533 void setvoltime(AFPObj *obj, struct vol *vol)
2537 if ( gettimeofday( &tv, NULL ) < 0 ) {
2538 LOG(log_error, logtype_afpd, "setvoltime(%s): gettimeofday: %s", vol->v_path, strerror(errno) );
2541 if( utime( vol->v_path, NULL ) < 0 ) {
2542 /* write of time failed ... probably a read only filesys,
2543 * where no other users can interfere, so there's no issue here
2547 /* a little granularity */
2548 if (vol->v_mtime < tv.tv_sec) {
2549 vol->v_mtime = tv.tv_sec;
2550 /* or finder doesn't update free space
2551 * AFP 3.2 and above clients seem to be ok without so many notification
2553 if (afp_version < 32 && obj->options.server_notif) {
2554 obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED);
2559 /* ------------------------- */
2560 int afp_getvolparams(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_,char *rbuf, size_t *rbuflen)
2563 uint16_t vid, bitmap;
2566 memcpy(&vid, ibuf, sizeof( vid ));
2567 ibuf += sizeof( vid );
2568 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2569 bitmap = ntohs( bitmap );
2571 if (NULL == ( vol = getvolbyvid( vid )) ) {
2573 return( AFPERR_PARAM );
2576 return stat_vol(bitmap, vol, rbuf, rbuflen);
2579 /* ------------------------- */
2580 int afp_setvolparams(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2584 uint16_t vid, bitmap;
2590 memcpy(&vid, ibuf, sizeof( vid ));
2591 ibuf += sizeof( vid );
2592 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2593 bitmap = ntohs( bitmap );
2594 ibuf += sizeof(bitmap);
2596 if (( vol = getvolbyvid( vid )) == NULL ) {
2597 return( AFPERR_PARAM );
2600 if ((vol->v_flags & AFPVOL_RO))
2601 return AFPERR_VLOCK;
2603 /* we can only set the backup date. */
2604 if (bitmap != (1 << VOLPBIT_BDATE))
2605 return AFPERR_BITMAP;
2608 if ( ad_open(&ad, vol->v_path, ADFLAGS_HF | ADFLAGS_DIR | ADFLAGS_RDWR) < 0 ) {
2610 return AFPERR_VLOCK;
2612 return AFPERR_ACCESS;
2615 memcpy(&aint, ibuf, sizeof(aint));
2616 ad_setdate(&ad, AD_DATE_BACKUP, aint);
2618 ad_close(&ad, ADFLAGS_HF);
2622 /* ------------------------- */
2623 int wincheck(const struct vol *vol, const char *path)
2627 if (!(vol->v_flags & AFPVOL_MSWINDOWS))
2630 /* empty paths are not allowed */
2631 if ((len = strlen(path)) == 0)
2634 /* leading or trailing whitespaces are not allowed, carriage returns
2635 * and probably other whitespace is okay, tabs are not allowed
2637 if ((path[0] == ' ') || (path[len-1] == ' '))
2640 /* certain characters are not allowed */
2641 if (strpbrk(path, MSWINDOWS_BADCHARS))
2644 /* everything else is okay */
2650 * precreate a folder
2651 * this is only intended for folders in the volume root
2652 * It will *not* work if the folder name contains extended characters
2654 static int create_special_folder (const struct vol *vol, const struct _special_folder *folder)
2663 p = (char *) malloc ( strlen(vol->v_path)+strlen(folder->name)+2);
2665 LOG(log_error, logtype_afpd,"malloc failed");
2669 q=strdup(folder->name);
2671 LOG(log_error, logtype_afpd,"malloc failed");
2675 strcpy(p, vol->v_path);
2680 if ((vol->v_casefold & AFPVOL_MTOUUPPER))
2682 else if ((vol->v_casefold & AFPVOL_MTOULOWER))
2688 if ( (ret = stat( p, &st )) < 0 ) {
2689 if (folder->precreate) {
2690 if (ad_mkdir(p, folder->mode)) {
2691 LOG(log_debug, logtype_afpd,"Creating '%s' failed in %s: %s", p, vol->v_path, strerror(errno));
2700 if ( !ret && folder->hide) {
2703 if (ad_open(&ad, p, ADFLAGS_HF | ADFLAGS_DIR | ADFLAGS_RDWR | ADFLAGS_CREATE, 0666) != 0) {
2709 ad_setname(&ad, folder->name);
2711 ad_getattr(&ad, &attr);
2712 attr |= htons( ntohs( attr ) | ATTRBIT_INVISIBLE );
2713 ad_setattr(&ad, attr);
2715 /* do the same with the finder info */
2716 if (ad_entry(&ad, ADEID_FINDERI)) {
2717 memcpy(&attr, ad_entry(&ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF, sizeof(attr));
2718 attr |= htons(FINDERINFO_INVISIBLE);
2719 memcpy(ad_entry(&ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF,&attr, sizeof(attr));
2723 ad_close(&ad, ADFLAGS_HF);
2730 static void handle_special_folders (const struct vol * vol)
2732 const _special_folder *p = &special_folders[0];
2735 process_uid = geteuid();
2737 if (seteuid(0) == -1) {
2742 for (; p->name != NULL; p++) {
2743 create_special_folder (vol, p);
2747 if (seteuid(process_uid) == -1) {
2748 LOG(log_error, logtype_logger, "can't seteuid back %s", strerror(errno));
2754 const struct vol *getvolumes(void)
2759 void unload_volumes_and_extmap(void)
2761 LOG(log_debug, logtype_afpd, "unload_volumes_and_extmap");
2767 * Get a volumes UUID from the config file.
2768 * If there is none, it is generated and stored there.
2770 * Returns pointer to allocated storage on success, NULL on error.
2772 static char *get_vol_uuid(const AFPObj *obj, const char *volname)
2775 char buf[1024], uuid[UUID_PRINTABLE_STRING_LENGTH], *p;
2777 struct stat tmpstat;
2780 if ((fp = fopen(obj->options.uuidconf, "r")) != NULL) { /* read open? */
2781 /* scan in the conf file */
2782 while (fgets(buf, sizeof(buf), fp) != NULL) {
2784 while (p && isblank(*p))
2786 if (!p || (*p == '#') || (*p == '\n'))
2787 continue; /* invalid line */
2790 if ((volname_conf = strtok( p, "\"" )) == NULL)
2791 continue; /* syntax error */
2793 if ((volname_conf = strtok( p, " \t" )) == NULL)
2794 continue; /* syntax error: invalid name */
2796 p = strchr(p, '\0');
2799 continue; /* syntax error */
2801 if (strcmp(volname, volname_conf) != 0)
2802 continue; /* another volume name */
2804 while (p && isblank(*p))
2807 if (sscanf(p, "%36s", uuid) == 1 ) {
2808 for (int i=0; uuid[i]; i++)
2809 uuid[i] = toupper(uuid[i]);
2810 LOG(log_debug, logtype_afpd, "get_uuid('%s'): UUID: '%s'", volname, uuid);
2812 return strdup(uuid);
2820 /* not found or no file, reopen in append mode */
2822 if (stat(obj->options.uuidconf, &tmpstat)) { /* no file */
2823 if (( fd = creat(obj->options.uuidconf, 0644 )) < 0 ) {
2824 LOG(log_error, logtype_afpd, "ERROR: Cannot create %s (%s).",
2825 obj->options.uuidconf, strerror(errno));
2828 if (( fp = fdopen( fd, "w" )) == NULL ) {
2829 LOG(log_error, logtype_afpd, "ERROR: Cannot fdopen %s (%s).",
2830 obj->options.uuidconf, strerror(errno));
2834 } else if ((fp = fopen(obj->options.uuidconf, "a+")) == NULL) { /* not found */
2835 LOG(log_error, logtype_afpd, "Cannot create or append to %s (%s).",
2836 obj->options.uuidconf, strerror(errno));
2839 fseek(fp, 0L, SEEK_END);
2840 if(ftell(fp) == 0) { /* size = 0 */
2841 fprintf(fp, "# DON'T TOUCH NOR COPY THOUGHTLESSLY!\n");
2842 fprintf(fp, "# This file is auto-generated by afpd\n");
2843 fprintf(fp, "# and stores UUIDs for TM volumes.\n\n");
2845 fseek(fp, -1L, SEEK_END);
2846 if(fgetc(fp) != '\n') fputc('\n', fp); /* last char is \n? */
2849 /* generate uuid and write to file */
2852 randombytes((void *)id, 16);
2853 cp = uuid_bin2string(id);
2855 LOG(log_debug, logtype_afpd, "get_uuid('%s'): generated UUID '%s'", volname, cp);
2857 fprintf(fp, "\"%s\"\t%36s\n", volname, cp);