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)
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)
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, "ro") == 0)
409 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
410 else if (strcasecmp(p, "nohex") == 0)
411 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOHEX;
412 else if (strcasecmp(p, "usedots") == 0)
413 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS;
414 else if (strcasecmp(p, "invisibledots") == 0)
415 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS | AFPVOL_INV_DOTS;
416 else if (strcasecmp(p, "nostat") == 0)
417 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOSTAT;
418 else if (strcasecmp(p, "preexec_close") == 0)
419 options[VOLOPT_PREEXEC].i_value = 1;
420 else if (strcasecmp(p, "root_preexec_close") == 0)
421 options[VOLOPT_ROOTPREEXEC].i_value = 1;
422 else if (strcasecmp(p, "upriv") == 0)
423 options[VOLOPT_FLAGS].i_value |= AFPVOL_UNIX_PRIV;
424 else if (strcasecmp(p, "nodev") == 0)
425 options[VOLOPT_FLAGS].i_value |= AFPVOL_NODEV;
426 else if (strcasecmp(p, "caseinsensitive") == 0)
427 options[VOLOPT_FLAGS].i_value |= AFPVOL_CASEINSEN;
428 else if (strcasecmp(p, "illegalseq") == 0)
429 options[VOLOPT_FLAGS].i_value |= AFPVOL_EILSEQ;
430 else if (strcasecmp(p, "tm") == 0)
431 options[VOLOPT_FLAGS].i_value |= AFPVOL_TM;
432 else if (strcasecmp(p, "searchdb") == 0)
433 options[VOLOPT_FLAGS].i_value |= AFPVOL_SEARCHDB;
434 else if (strcasecmp(p, "nonetids") == 0)
435 options[VOLOPT_FLAGS].i_value |= AFPVOL_NONETIDS;
436 else if (strcasecmp(p, "noacls") == 0)
437 options[VOLOPT_FLAGS].i_value &= ~AFPVOL_ACLS;
438 else if (strcasecmp(p, "nov2toeaconv") == 0)
439 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOV2TOEACONV;
440 p = strtok(NULL, ",");
443 } else if (optionok(tmp, "cnidserver:", val)) {
444 setoption(options, save, VOLOPT_CNIDSERVER, val);
446 char *p = strrchr(val + 1, ':');
449 setoption(options, save, VOLOPT_CNIDPORT, p);
452 LOG(log_debug, logtype_afpd, "CNID Server for volume '%s': %s:%s",
453 volname, val + 1, p ? p + 1 : Cnid_port);
455 } else if (optionok(tmp, "dbpath:", val)) {
456 setoption(options, save, VOLOPT_DBPATH, val);
458 } else if (optionok(tmp, "umask:", val)) {
459 options[VOLOPT_UMASK].i_value = (int)strtol(val +1, NULL, 8);
460 } else if (optionok(tmp, "dperm:", val)) {
461 options[VOLOPT_DPERM].i_value = (int)strtol(val+1, NULL, 8);
462 } else if (optionok(tmp, "fperm:", val)) {
463 options[VOLOPT_FPERM].i_value = (int)strtol(val+1, NULL, 8);
464 } else if (optionok(tmp, "perm:", val)) {
465 options[VOLOPT_DFLTPERM].i_value = (int)strtol(val+1, NULL, 8);
466 } else if (optionok(tmp, "password:", val)) {
467 setoption(options, save, VOLOPT_PASSWORD, val);
468 } else if (optionok(tmp, "root_preexec:", val)) {
469 setoption(options, save, VOLOPT_ROOTPREEXEC, val);
471 } else if (optionok(tmp, "preexec:", val)) {
472 setoption(options, save, VOLOPT_PREEXEC, val);
474 } else if (optionok(tmp, "root_postexec:", val)) {
475 setoption(options, save, VOLOPT_ROOTPOSTEXEC, val);
477 } else if (optionok(tmp, "postexec:", val)) {
478 setoption(options, save, VOLOPT_POSTEXEC, val);
480 } else if (optionok(tmp, "allowed_hosts:", val)) {
481 setoption(options, save, VOLOPT_ALLOWED_HOSTS, val);
483 } else if (optionok(tmp, "denied_hosts:", val)) {
484 setoption(options, save, VOLOPT_DENIED_HOSTS, val);
486 } else if (optionok(tmp, "ea:", val)) {
487 if (strcasecmp(val + 1, "ad") == 0)
488 options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_AD;
489 else if (strcasecmp(val + 1, "sys") == 0)
490 options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_SYS;
491 else if (strcasecmp(val + 1, "none") == 0)
492 options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_NONE;
494 } else if (optionok(tmp, "volsizelimit:", val)) {
495 options[VOLOPT_LIMITSIZE].i_value = (uint32_t)strtoul(val + 1, NULL, 10);
498 /* ignore unknown options */
499 LOG(log_debug, logtype_afpd, "ignoring unknown volume option: %s", tmp);
504 /* ----------------- */
505 static void showvol(const ucs2_t *name)
508 for ( volume = Volumes; volume; volume = volume->v_next ) {
509 if (volume->v_hide && !strcasecmp_w( volume->v_name, name ) ) {
516 /* ------------------------------- */
517 static int creatvol(AFPObj *obj, struct passwd *pwd,
518 char *path, char *name,
519 struct vol_option *options,
520 const int user /* user defined volume */
524 int suffixlen, vlen, tmpvlen, u8mvlen, macvlen;
526 char tmpname[AFPVOL_U8MNAMELEN+1];
527 ucs2_t u8mtmpname[(AFPVOL_U8MNAMELEN+1)*2], mactmpname[(AFPVOL_MACNAMELEN+1)*2];
528 char suffix[6]; /* max is #FFFF */
531 LOG(log_debug, logtype_afpd, "createvol: Volume '%s'", name);
533 if ( name == NULL || *name == '\0' ) {
534 if ((name = strrchr( path, '/' )) == NULL) {
535 return -1; /* Obviously not a fully qualified path */
538 /* if you wish to share /, you need to specify a name. */
543 /* suffix for mangling use (lastvid + 1) */
544 /* because v_vid has not been decided yet. */
545 suffixlen = sprintf(suffix, "%c%X", MANGLE_CHAR, lastvid + 1 );
547 vlen = strlen( name );
549 /* Unicode Volume Name */
550 /* Firstly convert name from unixcharset to UTF8-MAC */
552 tmpvlen = convert_charset(obj->options.unixcharset, CH_UTF8_MAC, 0, name, vlen, tmpname, AFPVOL_U8MNAMELEN, &flags);
554 strcpy(tmpname, "???");
558 /* Do we have to mangle ? */
559 if ( (flags & CONV_REQMANGLE) || (tmpvlen > obj->options.volnamelen)) {
560 if (tmpvlen + suffixlen > obj->options.volnamelen) {
562 tmpvlen = convert_charset(obj->options.unixcharset, CH_UTF8_MAC, 0, name, vlen, tmpname, obj->options.volnamelen - suffixlen, &flags);
563 tmpname[tmpvlen >= 0 ? tmpvlen : 0] = 0;
565 strcat(tmpname, suffix);
566 tmpvlen = strlen(tmpname);
569 /* Secondly convert name from UTF8-MAC to UCS2 */
570 if ( 0 >= ( u8mvlen = convert_string(CH_UTF8_MAC, CH_UCS2, tmpname, tmpvlen, u8mtmpname, AFPVOL_U8MNAMELEN*2)) )
573 LOG(log_maxdebug, logtype_afpd, "createvol: Volume '%s' -> UTF8-MAC Name: '%s'", name, tmpname);
575 /* Maccharset Volume Name */
576 /* Firsty convert name from unixcharset to maccharset */
578 tmpvlen = convert_charset(obj->options.unixcharset, obj->options.maccharset, 0, name, vlen, tmpname, AFPVOL_U8MNAMELEN, &flags);
580 strcpy(tmpname, "???");
584 /* Do we have to mangle ? */
585 if ( (flags & CONV_REQMANGLE) || (tmpvlen > AFPVOL_MACNAMELEN)) {
586 if (tmpvlen + suffixlen > AFPVOL_MACNAMELEN) {
588 tmpvlen = convert_charset(obj->options.unixcharset,
589 obj->options.maccharset,
594 AFPVOL_MACNAMELEN - suffixlen,
596 tmpname[tmpvlen >= 0 ? tmpvlen : 0] = 0;
598 strcat(tmpname, suffix);
599 tmpvlen = strlen(tmpname);
602 /* Secondly convert name from maccharset to UCS2 */
603 if ( 0 >= ( macvlen = convert_string(obj->options.maccharset,
608 AFPVOL_U8MNAMELEN*2)) )
611 LOG(log_maxdebug, logtype_afpd, "createvol: Volume '%s' -> Longname: '%s'", name, tmpname);
613 /* check duplicate */
614 for ( volume = Volumes; volume; volume = volume->v_next ) {
615 if ((utf8_encoding() && (strcasecmp_w(volume->v_u8mname, u8mtmpname) == 0))
617 (!utf8_encoding() && (strcasecmp_w(volume->v_macname, mactmpname) == 0))) {
618 LOG (log_error, logtype_afpd,
619 "Duplicate volume name, check AppleVolumes files: previous: \"%s\", new: \"%s\"",
620 volume->v_localname, name);
621 if (volume->v_deleted) {
622 volume->v_new = hide = 1;
625 return -1; /* Won't be able to access it, anyway... */
630 if (!( volume = (struct vol *)calloc(1, sizeof( struct vol ))) ) {
631 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
634 if ( NULL == ( volume->v_localname = strdup(name))) {
635 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
640 if ( NULL == ( volume->v_u8mname = strdup_w(u8mtmpname))) {
641 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
646 if ( NULL == ( volume->v_macname = strdup_w(mactmpname))) {
647 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
652 if (!( volume->v_path = (char *)malloc( strlen( path ) + 1 )) ) {
653 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
659 volume->v_name = utf8_encoding()?volume->v_u8mname:volume->v_macname;
660 volume->v_hide = hide;
661 strcpy( volume->v_path, path );
665 #endif /* __svr4__ */
666 /* os X start at 1 and use network order ie. 1 2 3 */
667 volume->v_vid = ++lastvid;
668 volume->v_vid = htons(volume->v_vid);
670 if (!check_vol_acl_support(volume)) {
671 LOG(log_debug, logtype_afpd, "creatvol(\"%s\"): disabling ACL support", volume->v_path);
672 options[VOLOPT_FLAGS].i_value &= ~AFPVOL_ACLS;
678 volume->v_casefold = options[VOLOPT_CASEFOLD].i_value;
679 volume->v_flags |= options[VOLOPT_FLAGS].i_value;
681 if (options[VOLOPT_EA_VFS].i_value)
682 volume->v_vfs_ea = options[VOLOPT_EA_VFS].i_value;
684 volume->v_ad_options = 0;
685 if ((volume->v_flags & AFPVOL_NODEV))
686 volume->v_ad_options |= ADVOL_NODEV;
687 if ((volume->v_flags & AFPVOL_UNIX_PRIV))
688 volume->v_ad_options |= ADVOL_UNIXPRIV;
689 if ((volume->v_flags & AFPVOL_INV_DOTS))
690 volume->v_ad_options |= ADVOL_INVDOTS;
692 if (options[VOLOPT_PASSWORD].c_value)
693 volume->v_password = strdup(options[VOLOPT_PASSWORD].c_value);
695 if (options[VOLOPT_VETO].c_value)
696 volume->v_veto = strdup(options[VOLOPT_VETO].c_value);
698 if (options[VOLOPT_ENCODING].c_value)
699 volume->v_volcodepage = strdup(options[VOLOPT_ENCODING].c_value);
701 if (options[VOLOPT_MACCHARSET].c_value)
702 volume->v_maccodepage = strdup(options[VOLOPT_MACCHARSET].c_value);
704 if (options[VOLOPT_DBPATH].c_value)
705 volume->v_dbpath = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_DBPATH].c_value, pwd, path, name);
707 if (options[VOLOPT_CNIDSCHEME].c_value)
708 volume->v_cnidscheme = strdup(options[VOLOPT_CNIDSCHEME].c_value);
710 if (options[VOLOPT_CNIDSERVER].c_value)
711 volume->v_cnidserver = strdup(options[VOLOPT_CNIDSERVER].c_value);
713 if (options[VOLOPT_CNIDPORT].c_value)
714 volume->v_cnidport = strdup(options[VOLOPT_CNIDPORT].c_value);
716 if (options[VOLOPT_UMASK].i_value)
717 volume->v_umask = (mode_t)options[VOLOPT_UMASK].i_value;
719 if (options[VOLOPT_DPERM].i_value)
720 volume->v_dperm = (mode_t)options[VOLOPT_DPERM].i_value;
722 if (options[VOLOPT_FPERM].i_value)
723 volume->v_fperm = (mode_t)options[VOLOPT_FPERM].i_value;
725 if (options[VOLOPT_DFLTPERM].i_value)
726 volume->v_perm = (mode_t)options[VOLOPT_DFLTPERM].i_value;
728 if (options[VOLOPT_ADOUBLE].i_value)
729 volume->v_adouble = options[VOLOPT_ADOUBLE].i_value;
731 volume->v_adouble = AD_VERSION;
733 if (options[VOLOPT_LIMITSIZE].i_value)
734 volume->v_limitsize = options[VOLOPT_LIMITSIZE].i_value;
736 /* Mac to Unix conversion flags*/
737 volume->v_mtou_flags = 0;
738 if (!(volume->v_flags & AFPVOL_NOHEX))
739 volume->v_mtou_flags |= CONV_ESCAPEHEX;
740 if (!(volume->v_flags & AFPVOL_USEDOTS))
741 volume->v_mtou_flags |= CONV_ESCAPEDOTS;
742 if ((volume->v_flags & AFPVOL_EILSEQ))
743 volume->v_mtou_flags |= CONV__EILSEQ;
745 if ((volume->v_casefold & AFPVOL_MTOUUPPER))
746 volume->v_mtou_flags |= CONV_TOUPPER;
747 else if ((volume->v_casefold & AFPVOL_MTOULOWER))
748 volume->v_mtou_flags |= CONV_TOLOWER;
750 /* Unix to Mac conversion flags*/
751 volume->v_utom_flags = CONV_IGNORE | CONV_UNESCAPEHEX;
752 if ((volume->v_casefold & AFPVOL_UTOMUPPER))
753 volume->v_utom_flags |= CONV_TOUPPER;
754 else if ((volume->v_casefold & AFPVOL_UTOMLOWER))
755 volume->v_utom_flags |= CONV_TOLOWER;
757 if ((volume->v_flags & AFPVOL_EILSEQ))
758 volume->v_utom_flags |= CONV__EILSEQ;
761 if (options[VOLOPT_PREEXEC].c_value)
762 volume->v_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_PREEXEC].c_value, pwd, path, name);
763 volume->v_preexec_close = options[VOLOPT_PREEXEC].i_value;
765 if (options[VOLOPT_POSTEXEC].c_value)
766 volume->v_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_POSTEXEC].c_value, pwd, path, name);
768 if (options[VOLOPT_ROOTPREEXEC].c_value)
769 volume->v_root_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPREEXEC].c_value, pwd, path, name);
770 volume->v_root_preexec_close = options[VOLOPT_ROOTPREEXEC].i_value;
772 if (options[VOLOPT_ROOTPOSTEXEC].c_value)
773 volume->v_root_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPOSTEXEC].c_value, pwd, path, name);
776 volume->v_dperm |= volume->v_perm;
777 volume->v_fperm |= volume->v_perm;
779 /* Check EA support on volume */
780 if (volume->v_vfs_ea == AFPVOL_EA_AUTO)
781 check_ea_sys_support(volume);
784 /* get/store uuid from file in afpd master*/
785 if ((parent_or_child == 0) && (volume->v_flags & AFPVOL_TM)) {
786 char *uuid = get_vol_uuid(obj, volume->v_localname);
788 LOG(log_error, logtype_afpd, "Volume '%s': couldn't get UUID",
789 volume->v_localname);
791 volume->v_uuid = uuid;
792 LOG(log_debug, logtype_afpd, "Volume '%s': UUID '%s'",
793 volume->v_localname, volume->v_uuid);
797 volume->v_next = Volumes;
802 /* ---------------- */
803 static char *myfgets( char *buf, int size, FILE *fp)
809 while ((EOF != ( c = getc( fp )) ) && ( size > 1 )) {
810 if ( c == '\n' || c == '\r' ) {
811 if (p != buf && *(p -1) == '\\') {
833 /* check access list. this function wants something of the following
835 * @group,name,name2,@group2,name3
837 * a NULL argument allows everybody to have access.
838 * we return three things:
840 * 0: list exists, but name isn't in it
844 #ifndef NO_REAL_USER_NAME
845 /* authentication is case insensitive
846 * FIXME should we do the same with group name?
848 #define access_strcmp strcasecmp
851 #define access_strcmp strcmp
855 static int accessvol(const char *args, const char *name)
857 char buf[MAXPATHLEN + 1], *p;
863 strlcpy(buf, args, sizeof(buf));
864 if ((p = strtok(buf, ",")) == NULL) /* nothing, return okay */
868 if (*p == '@') { /* it's a group */
869 if ((gr = getgrnam(p + 1)) && gmem(gr->gr_gid))
871 } else if (access_strcmp(p, name) == 0) /* it's a user name */
873 p = strtok(NULL, ",");
879 static int hostaccessvol(int type, const char *volname, const char *args, const AFPObj *obj)
882 char buf[MAXPATHLEN + 1], *p, *b;
884 struct sockaddr_storage client;
889 strlcpy(buf, args, sizeof(buf));
890 if ((p = strtok_r(buf, ",", &b)) == NULL) /* nothing, return okay */
893 if (obj->proto != AFPPROTO_DSI)
898 char *ipaddr, *mask_char;
899 struct addrinfo hints, *ai;
901 ipaddr = strtok(p, "/");
902 mask_char = strtok(NULL,"/");
904 /* Get address from string with getaddrinfo */
905 memset(&hints, 0, sizeof hints);
906 hints.ai_family = AF_UNSPEC;
907 hints.ai_socktype = SOCK_STREAM;
908 if ((ret = getaddrinfo(ipaddr, NULL, &hints, &ai)) != 0) {
909 LOG(log_error, logtype_afpd, "hostaccessvol: getaddrinfo: %s\n", gai_strerror(ret));
914 if (mask_char != NULL)
915 mask_int = atoi(mask_char); /* apply_ip_mask does range checking on it */
917 if (ai->ai_family == AF_INET) /* IPv4 */
923 /* Apply mask to addresses */
924 client = dsi->client;
925 apply_ip_mask((struct sockaddr *)&client, mask_int);
926 apply_ip_mask(ai->ai_addr, mask_int);
928 if (compare_ip((struct sockaddr *)&client, ai->ai_addr) == 0) {
929 if (type == VOLOPT_DENIED_HOSTS)
930 LOG(log_info, logtype_afpd, "AFP access denied for client IP '%s' to volume '%s' by denied list",
931 getip_string((struct sockaddr *)&client), volname);
938 p = strtok_r(NULL, ",", &b);
941 if (type == VOLOPT_ALLOWED_HOSTS)
942 LOG(log_info, logtype_afpd, "AFP access denied for client IP '%s' to volume '%s', not in allowed list",
943 getip_string((struct sockaddr *)&dsi->client), volname);
947 static void setextmap(char *ext, char *type, char *creator, int user)
952 if (Extmap == NULL) {
953 if (( Extmap = calloc(1, sizeof( struct extmap ))) == NULL ) {
954 LOG(log_error, logtype_afpd, "setextmap: calloc: %s", strerror(errno) );
959 for ( em = Extmap, cnt = 0; em->em_ext; em++, cnt++) {
960 if ( (strdiacasecmp( em->em_ext, ext )) == 0 ) {
965 if ( em->em_ext == NULL ) {
966 if (!(Extmap = realloc( Extmap, sizeof( struct extmap ) * (cnt +2))) ) {
967 LOG(log_error, logtype_afpd, "setextmap: realloc: %s", strerror(errno) );
970 (Extmap +cnt +1)->em_ext = NULL;
972 } else if ( !user ) {
978 if (!(em->em_ext = strdup( ext))) {
979 LOG(log_error, logtype_afpd, "setextmap: strdup: %s", strerror(errno) );
983 if ( *type == '\0' ) {
984 memcpy(em->em_type, "\0\0\0\0", sizeof( em->em_type ));
986 memcpy(em->em_type, type, sizeof( em->em_type ));
988 if ( *creator == '\0' ) {
989 memcpy(em->em_creator, "\0\0\0\0", sizeof( em->em_creator ));
991 memcpy(em->em_creator, creator, sizeof( em->em_creator ));
995 /* -------------------------- */
996 static int extmap_cmp(const void *map1, const void *map2)
998 const struct extmap *em1 = map1;
999 const struct extmap *em2 = map2;
1000 return strdiacasecmp(em1->em_ext, em2->em_ext);
1003 static void sortextmap( void)
1008 if ((em = Extmap) == NULL) {
1011 while (em->em_ext) {
1016 qsort(Extmap, Extmap_cnt, sizeof(struct extmap), extmap_cmp);
1017 if (*Extmap->em_ext == 0) {
1018 /* the first line is really "." the default entry,
1019 * we remove the leading '.' in setextmap
1026 /* ----------------------
1028 static void free_extmap( void)
1033 for ( em = Extmap; em->em_ext; em++) {
1043 /* ----------------------
1045 static int volfile_changed(struct afp_volume_name *p)
1051 name = p->full_name;
1055 if (!stat( name, &st) && st.st_mtime > p->mtime) {
1056 p->mtime = st.st_mtime;
1062 /* ----------------------
1063 * Read a volume configuration file and add the volumes contained within to
1064 * the global volume list. This gets called from the forked afpd childs.
1065 * The master now reads this too for Zeroconf announcements.
1067 * If p2 is non-NULL, the file that is opened is
1070 * Lines that begin with # and blank lines are ignored.
1071 * Volume lines are of the form:
1072 * <unix path> [<volume name>] [allow:<user>,<@group>,...] \
1073 * [codepage:<file>] [casefold:<num>]
1074 * <extension> TYPE [CREATOR]
1077 static int readvolfile(AFPObj *obj, struct afp_volume_name *p1, char *p2, int user, struct passwd *pwent)
1080 char path[MAXPATHLEN + 1];
1081 char tmp[MAXPATHLEN + 1];
1082 char volname[AFPVOL_U8MNAMELEN + 1];
1084 char type[5], creator[5];
1089 struct vol_option save_options[VOLOPT_NUM];
1090 struct vol_option default_options[VOLOPT_NUM];
1091 struct vol_option options[VOLOPT_NUM];
1097 strcpy( path, p1->name );
1099 strcat( path, "/" );
1101 if (p1->full_name) {
1102 free(p1->full_name);
1104 p1->full_name = strdup(path);
1107 if (NULL == ( fp = fopen( path, "r" )) ) {
1111 if (fd != -1 && !fstat( fd, &st) ) {
1112 p1->mtime = st.st_mtime;
1115 /* try putting a read lock on the volume file twice, sleep 1 second if first attempt fails */
1118 if ((read_lock(fd, 0, SEEK_SET, 0)) != 0) {
1121 LOG(log_error, logtype_afpd, "readvolfile: can't lock volume file \"%s\"", path);
1122 if ( fclose( fp ) != 0 ) {
1123 LOG(log_error, logtype_afpd, "readvolfile: fclose: %s", strerror(errno) );
1133 memset(default_options, 0, sizeof(default_options));
1135 /* Enable some default options for all volumes */
1137 default_options[VOLOPT_FLAGS].i_value |= AFPVOL_ACLS;
1139 default_options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_AUTO;
1140 LOG(log_maxdebug, logtype_afpd, "readvolfile: seeding default umask: %04o",
1141 obj->options.umask);
1142 default_options[VOLOPT_UMASK].i_value = obj->options.umask;
1143 memcpy(save_options, default_options, sizeof(options));
1145 LOG(log_debug, logtype_afpd, "readvolfile: \"%s\"", path);
1147 while ( myfgets( buf, sizeof( buf ), fp ) != NULL ) {
1148 initline( strlen( buf ), buf );
1149 parseline( sizeof( path ) - 1, path );
1156 /* change the default options for this file */
1157 if (strncmp(path, VOLOPT_DEFAULT, VOLOPT_DEFAULT_LEN) == 0) {
1158 volfree(default_options, save_options);
1159 memcpy(default_options, save_options, sizeof(options));
1161 for (i = 0; i < VOLOPT_NUM; i++) {
1162 if (parseline( sizeof( path ) - VOLOPT_DEFAULT_LEN - 1,
1163 path + VOLOPT_DEFAULT_LEN) < 0)
1165 volset(default_options, NULL, tmp, sizeof(tmp) - 1,
1166 path + VOLOPT_DEFAULT_LEN);
1172 if (( p = strchr( path, '/' )) != NULL ) {
1180 if ( u == NULL || *u == '\0' || ( pw = getpwnam( u )) == NULL ) {
1183 strcpy( tmp, pw->pw_dir );
1184 if ( p != NULL && *p != '\0' ) {
1191 /* send path through variable substitution */
1192 if (*path != '~') /* need to copy path to tmp */
1194 if (!pwent && obj->username)
1195 pwent = getpwnam(obj->username);
1197 if (volxlate(obj, path, sizeof(path) - 1, tmp, pwent, NULL, NULL) == NULL)
1200 /* this is sort of braindead. basically, i want to be
1201 * able to specify things in any order, but i don't want to
1202 * re-write everything. */
1204 memcpy(options, default_options, sizeof(options));
1207 /* read in up to VOLOP_NUM possible options */
1208 for (i = 0; i < VOLOPT_NUM; i++) {
1209 if (parseline( sizeof( tmp ) - 1, tmp ) < 0)
1212 volset(options, default_options, volname, sizeof(volname) - 1, tmp);
1215 /* check allow/deny lists (if not afpd master loading volumes for Zeroconf reg.):
1216 allow -> either no list (-1), or in list (1)
1217 deny -> either no list (-1), or not in list (0) */
1218 if (parent_or_child == 0
1220 (accessvol(options[VOLOPT_ALLOW].c_value, obj->username) &&
1221 (accessvol(options[VOLOPT_DENY].c_value, obj->username) < 1) &&
1222 hostaccessvol(VOLOPT_ALLOWED_HOSTS, volname, options[VOLOPT_ALLOWED_HOSTS].c_value, obj) &&
1223 (hostaccessvol(VOLOPT_DENIED_HOSTS, volname, options[VOLOPT_DENIED_HOSTS].c_value, obj) < 1))) {
1225 /* handle read-only behaviour. semantics:
1226 * 1) neither the rolist nor the rwlist exist -> rw
1227 * 2) rolist exists -> ro if user is in it.
1228 * 3) rwlist exists -> ro unless user is in it. */
1229 if (parent_or_child == 1
1231 ((options[VOLOPT_FLAGS].i_value & AFPVOL_RO) == 0)
1233 ((accessvol(options[VOLOPT_ROLIST].c_value, obj->username) == 1) ||
1234 !accessvol(options[VOLOPT_RWLIST].c_value, obj->username)))
1235 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
1237 /* do variable substitution for volname */
1238 if (volxlate(obj, tmp, sizeof(tmp) - 1, volname, pwent, path, NULL) == NULL)
1241 creatvol(obj, pwent, path, tmp, options, p2 != NULL);
1243 volfree(options, default_options);
1247 parseline( sizeof( type ) - 1, type );
1248 parseline( sizeof( creator ) - 1, creator );
1249 setextmap( path, type, creator, user);
1256 volfree(save_options, NULL);
1258 if ( fclose( fp ) != 0 ) {
1259 LOG(log_error, logtype_afpd, "readvolfile: fclose: %s", strerror(errno) );
1265 /* ------------------------------- */
1266 static void volume_free(struct vol *vol)
1268 free(vol->v_localname);
1269 vol->v_localname = NULL;
1270 free(vol->v_u8mname);
1271 vol->v_u8mname = NULL;
1272 free(vol->v_macname);
1273 vol->v_macname = NULL;
1275 free(vol->v_password);
1277 free(vol->v_volcodepage);
1278 free(vol->v_maccodepage);
1279 free(vol->v_cnidscheme);
1280 free(vol->v_dbpath);
1286 /* ------------------------------- */
1287 static void free_volumes(void )
1290 struct vol *nvol, *ovol;
1292 for ( vol = Volumes; vol; vol = vol->v_next ) {
1293 if (( vol->v_flags & AFPVOL_OPEN ) ) {
1300 for ( vol = Volumes, ovol = NULL; vol; vol = nvol) {
1303 if (vol->v_localname == NULL) {
1304 if (Volumes == vol) {
1309 ovol->v_next = nvol;
1319 /* ------------------------------- */
1320 static void volume_unlink(struct vol *volume)
1322 struct vol *vol, *ovol, *nvol;
1324 if (volume == Volumes) {
1325 Volumes = Volumes->v_next;
1328 for ( vol = Volumes->v_next, ovol = Volumes; vol; vol = nvol) {
1331 if (vol == volume) {
1332 ovol->v_next = nvol;
1341 * Read band-size info from Info.plist XML file of an TM sparsebundle
1343 * @param path (r) path to Info.plist file
1344 * @return band-size in bytes, -1 on error
1346 static long long int get_tm_bandsize(const char *path)
1351 long long int bandsize = -1;
1353 EC_NULL_LOGSTR( file = fopen(path, "r"),
1354 "get_tm_bandsize(\"%s\"): %s",
1355 path, strerror(errno) );
1357 while (fgets(buf, sizeof(buf), file) != NULL) {
1358 if (strstr(buf, "band-size") == NULL)
1361 if (fscanf(file, " <integer>%lld</integer>", &bandsize) != 1) {
1362 LOG(log_error, logtype_afpd, "get_tm_bandsize(\"%s\"): can't parse band-size", path);
1371 LOG(log_debug, logtype_afpd, "get_tm_bandsize(\"%s\"): bandsize: %lld", path, bandsize);
1376 * Return number on entries in a directory
1378 * @param path (r) path to dir
1379 * @return number of entries, -1 on error
1381 static long long int get_tm_bands(const char *path)
1384 long long int count = 0;
1386 const struct dirent *entry;
1388 EC_NULL( dir = opendir(path) );
1390 while ((entry = readdir(dir)) != NULL)
1392 count -= 2; /* All OSens I'm aware of return "." and "..", so just substract them, avoiding string comparison in loop */
1403 * Calculate used size of a TimeMachine volume
1405 * This assumes that the volume is used only for TimeMachine.
1407 * 1) readdir(path of volume)
1408 * 2) for every element that matches regex "\(.*\)\.sparsebundle$" :
1409 * 3) parse "\1.sparsebundle/Info.plist" and read the band-size XML key integer value
1410 * 4) readdir "\1.sparsebundle/bands/" counting files
1411 * 5) calculate used size as: (file_count - 1) * band-size
1413 * The result of the calculation is returned in "volume->v_tm_used".
1414 * "volume->v_appended" gets reset to 0.
1415 * "volume->v_tm_cachetime" is updated with the current time from time(NULL).
1417 * "volume->v_tm_used" is cached for TM_USED_CACHETIME seconds and updated by
1418 * "volume->v_appended". The latter is increased by X every time the client
1419 * appends X bytes to a file (in fork.c).
1421 * @param vol (rw) volume to calculate
1422 * @return 0 on success, -1 on error
1424 #define TM_USED_CACHETIME 60 /* cache for 60 seconds */
1425 static int get_tm_used(struct vol * restrict vol)
1428 long long int bandsize;
1430 bstring infoplist = NULL;
1431 bstring bandsdir = NULL;
1433 const struct dirent *entry;
1437 time_t now = time(NULL);
1439 if (vol->v_tm_cachetime
1440 && ((vol->v_tm_cachetime + TM_USED_CACHETIME) >= now)) {
1441 if (vol->v_tm_used == -1)
1443 vol->v_tm_used += vol->v_appended;
1444 vol->v_appended = 0;
1445 LOG(log_debug, logtype_afpd, "getused(\"%s\"): cached: %" PRIu64 " bytes",
1446 vol->v_path, vol->v_tm_used);
1450 vol->v_tm_cachetime = now;
1452 EC_NULL( dir = opendir(vol->v_path) );
1454 while ((entry = readdir(dir)) != NULL) {
1455 if (((p = strstr(entry->d_name, "sparsebundle")) != NULL)
1456 && (strlen(entry->d_name) == (p + strlen("sparsebundle") - entry->d_name))) {
1458 EC_NULL_LOG( infoplist = bformat("%s/%s/%s", vol->v_path, entry->d_name, "Info.plist") );
1460 if ((bandsize = get_tm_bandsize(cfrombstr(infoplist))) == -1)
1463 EC_NULL_LOG( bandsdir = bformat("%s/%s/%s/", vol->v_path, entry->d_name, "bands") );
1465 if ((links = get_tm_bands(cfrombstr(bandsdir))) == -1)
1468 used += (links - 1) * bandsize;
1469 LOG(log_debug, logtype_afpd, "getused(\"%s\"): bands: %" PRIu64 " bytes",
1470 cfrombstr(bandsdir), used);
1474 vol->v_tm_used = used;
1478 bdestroy(infoplist);
1484 LOG(log_debug, logtype_afpd, "getused(\"%s\"): %" PRIu64 " bytes", vol->v_path, vol->v_tm_used);
1489 static int getvolspace(struct vol *vol,
1490 uint32_t *bfree, uint32_t *btotal,
1491 VolSpace *xbfree, VolSpace *xbtotal, uint32_t *bsize)
1496 #ifndef NO_QUOTA_SUPPORT
1497 VolSpace qfree, qtotal;
1500 spaceflag = AFPVOL_GVSMASK & vol->v_flags;
1501 /* report up to 2GB if afp version is < 2.2 (4GB if not) */
1502 maxsize = (afp_version < 22) ? 0x7fffffffL : 0xffffffffL;
1505 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_AFSGVS ) {
1506 if ( afs_getvolspace( vol, xbfree, xbtotal, bsize ) == AFP_OK ) {
1507 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_AFSGVS;
1508 goto getvolspace_done;
1513 if (( rc = ustatfs_getvolspace( vol, xbfree, xbtotal, bsize)) != AFP_OK ) {
1517 #ifndef NO_QUOTA_SUPPORT
1518 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_UQUOTA ) {
1519 if ( uquota_getvolspace( vol, &qfree, &qtotal, *bsize ) == AFP_OK ) {
1520 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_UQUOTA;
1521 *xbfree = MIN(*xbfree, qfree);
1522 *xbtotal = MIN(*xbtotal, qtotal);
1523 goto getvolspace_done;
1527 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_USTATFS;
1530 if (vol->v_limitsize) {
1531 if (get_tm_used(vol) != 0)
1534 *xbtotal = MIN(*xbtotal, (vol->v_limitsize * 1024 * 1024));
1535 *xbfree = MIN(*xbfree, *xbtotal < vol->v_tm_used ? 0 : *xbtotal - vol->v_tm_used);
1537 LOG(log_debug, logtype_afpd,
1538 "volparams: total: %" PRIu64 ", used: %" PRIu64 ", free: %" PRIu64 " bytes",
1539 *xbtotal, vol->v_tm_used, *xbfree);
1542 *bfree = MIN(*xbfree, maxsize);
1543 *btotal = MIN(*xbtotal, maxsize);
1547 #define FCE_TM_DELTA 10 /* send notification every 10 seconds */
1548 void vol_fce_tm_event(void)
1551 time_t now = time(NULL);
1552 struct vol *vol = Volumes;
1554 if ((last + FCE_TM_DELTA) < now) {
1556 for ( ; vol; vol = vol->v_next ) {
1557 if (vol->v_flags & AFPVOL_TM)
1558 (void)fce_register_tm_size(vol->v_path, vol->v_tm_used + vol->v_appended);
1563 /* -----------------------
1564 * set volume creation date
1565 * avoid duplicate, well at least it tries
1567 static void vol_setdate(uint16_t id, struct adouble *adp, time_t date)
1570 struct vol *vol = Volumes;
1572 for ( volume = Volumes; volume; volume = volume->v_next ) {
1573 if (volume->v_vid == id) {
1576 else if ((time_t)(AD_DATE_FROM_UNIX(date)) == volume->v_ctime) {
1578 volume = Volumes; /* restart */
1581 vol->v_ctime = AD_DATE_FROM_UNIX(date);
1582 ad_setdate(adp, AD_DATE_CREATE | AD_DATE_UNIX, date);
1585 /* ----------------------- */
1586 static int getvolparams( uint16_t bitmap, struct vol *vol, struct stat *st, char *buf, size_t *buflen)
1589 int bit = 0, isad = 1;
1592 uint32_t bfree, btotal, bsize;
1593 VolSpace xbfree, xbtotal; /* extended bytes */
1594 char *data, *nameoff = NULL;
1597 LOG(log_debug, logtype_afpd, "getvolparams: Volume '%s'", vol->v_localname);
1599 /* courtesy of jallison@whistle.com:
1600 * For MacOS8.x support we need to create the
1601 * .Parent file here if it doesn't exist. */
1603 /* Convert adouble:v2 to adouble:ea on the fly */
1604 (void)ad_convert(vol->v_path, st, vol);
1607 if (ad_open(&ad, vol->v_path, ADFLAGS_HF | ADFLAGS_DIR | ADFLAGS_RDWR | ADFLAGS_CREATE, 0666) != 0 ) {
1609 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1611 } else if (ad_get_MD_flags( &ad ) & O_CREAT) {
1612 slash = strrchr( vol->v_path, '/' );
1616 slash = vol->v_path;
1617 if (ad_getentryoff(&ad, ADEID_NAME)) {
1618 ad_setentrylen( &ad, ADEID_NAME, strlen( slash ));
1619 memcpy(ad_entry( &ad, ADEID_NAME ), slash,
1620 ad_getentrylen( &ad, ADEID_NAME ));
1622 vol_setdate(vol->v_vid, &ad, st->st_mtime);
1626 if (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0)
1627 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1629 vol->v_ctime = aint;
1632 if (( bitmap & ( (1<<VOLPBIT_BFREE)|(1<<VOLPBIT_BTOTAL) |
1633 (1<<VOLPBIT_XBFREE)|(1<<VOLPBIT_XBTOTAL) |
1634 (1<<VOLPBIT_BSIZE)) ) != 0 ) {
1635 if ( getvolspace( vol, &bfree, &btotal, &xbfree, &xbtotal,
1636 &bsize) != AFP_OK ) {
1638 ad_close( &ad, ADFLAGS_HF );
1640 return( AFPERR_PARAM );
1645 while ( bitmap != 0 ) {
1646 while (( bitmap & 1 ) == 0 ) {
1654 /* check for read-only.
1655 * NOTE: we don't actually set the read-only flag unless
1656 * it's passed in that way as it's possible to mount
1657 * a read-write filesystem under a read-only one. */
1658 if ((vol->v_flags & AFPVOL_RO) ||
1659 ((utime(vol->v_path, NULL) < 0) && (errno == EROFS))) {
1660 ashort |= VOLPBIT_ATTR_RO;
1662 /* prior 2.1 only VOLPBIT_ATTR_RO is defined */
1663 if (afp_version > 20) {
1664 if (vol->v_cdb != NULL && (vol->v_cdb->flags & CNID_FLAG_PERSISTENT))
1665 ashort |= VOLPBIT_ATTR_FILEID;
1666 ashort |= VOLPBIT_ATTR_CATSEARCH;
1668 if (afp_version >= 30) {
1669 ashort |= VOLPBIT_ATTR_UTF8;
1670 if (vol->v_flags & AFPVOL_UNIX_PRIV)
1671 ashort |= VOLPBIT_ATTR_UNIXPRIV;
1672 if (vol->v_flags & AFPVOL_TM)
1673 ashort |= VOLPBIT_ATTR_TM;
1674 if (vol->v_flags & AFPVOL_NONETIDS)
1675 ashort |= VOLPBIT_ATTR_NONETIDS;
1676 if (afp_version >= 32) {
1678 ashort |= VOLPBIT_ATTR_EXT_ATTRS;
1679 if (vol->v_flags & AFPVOL_ACLS)
1680 ashort |= VOLPBIT_ATTR_ACLS;
1684 ashort = htons(ashort);
1685 memcpy(data, &ashort, sizeof( ashort ));
1686 data += sizeof( ashort );
1690 ashort = htons( AFPVOLSIG_DEFAULT );
1691 memcpy(data, &ashort, sizeof( ashort ));
1692 data += sizeof( ashort );
1695 case VOLPBIT_CDATE :
1696 aint = vol->v_ctime;
1697 memcpy(data, &aint, sizeof( aint ));
1698 data += sizeof( aint );
1701 case VOLPBIT_MDATE :
1702 if ( st->st_mtime > vol->v_mtime ) {
1703 vol->v_mtime = st->st_mtime;
1705 aint = AD_DATE_FROM_UNIX(vol->v_mtime);
1706 memcpy(data, &aint, sizeof( aint ));
1707 data += sizeof( aint );
1710 case VOLPBIT_BDATE :
1711 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1712 aint = AD_DATE_START;
1713 memcpy(data, &aint, sizeof( aint ));
1714 data += sizeof( aint );
1718 memcpy(data, &vol->v_vid, sizeof( vol->v_vid ));
1719 data += sizeof( vol->v_vid );
1722 case VOLPBIT_BFREE :
1723 bfree = htonl( bfree );
1724 memcpy(data, &bfree, sizeof( bfree ));
1725 data += sizeof( bfree );
1728 case VOLPBIT_BTOTAL :
1729 btotal = htonl( btotal );
1730 memcpy(data, &btotal, sizeof( btotal ));
1731 data += sizeof( btotal );
1734 #ifndef NO_LARGE_VOL_SUPPORT
1735 case VOLPBIT_XBFREE :
1736 xbfree = hton64( xbfree );
1737 memcpy(data, &xbfree, sizeof( xbfree ));
1738 data += sizeof( xbfree );
1741 case VOLPBIT_XBTOTAL :
1742 xbtotal = hton64( xbtotal );
1743 memcpy(data, &xbtotal, sizeof( xbtotal ));
1744 data += sizeof( xbfree );
1746 #endif /* ! NO_LARGE_VOL_SUPPORT */
1750 data += sizeof( uint16_t );
1753 case VOLPBIT_BSIZE: /* block size */
1754 bsize = htonl(bsize);
1755 memcpy(data, &bsize, sizeof(bsize));
1756 data += sizeof(bsize);
1761 ad_close( &ad, ADFLAGS_HF );
1763 return( AFPERR_BITMAP );
1769 ashort = htons( data - buf );
1770 memcpy(nameoff, &ashort, sizeof( ashort ));
1771 /* name is always in mac charset */
1772 aint = ucs2_to_charset( vol->v_maccharset, vol->v_macname, data+1, AFPVOL_MACNAMELEN + 1);
1782 ad_close(&ad, ADFLAGS_HF);
1784 *buflen = data - buf;
1788 /* ------------------------- */
1789 static int stat_vol(uint16_t bitmap, struct vol *vol, char *rbuf, size_t *rbuflen)
1795 if ( stat( vol->v_path, &st ) < 0 ) {
1797 return( AFPERR_PARAM );
1799 /* save the volume device number */
1800 vol->v_dev = st.st_dev;
1802 buflen = *rbuflen - sizeof( bitmap );
1803 if (( ret = getvolparams( bitmap, vol, &st,
1804 rbuf + sizeof( bitmap ), &buflen )) != AFP_OK ) {
1808 *rbuflen = buflen + sizeof( bitmap );
1809 bitmap = htons( bitmap );
1810 memcpy(rbuf, &bitmap, sizeof( bitmap ));
1815 /* ------------------------------- */
1816 void load_volumes(AFPObj *obj)
1818 struct passwd *pwent;
1823 /* check files date */
1824 if (obj->options.defaultvol.loaded) {
1825 changed = volfile_changed(&obj->options.defaultvol);
1827 if (obj->options.systemvol.loaded) {
1828 changed |= volfile_changed(&obj->options.systemvol);
1830 if (obj->options.uservol.loaded) {
1831 changed |= volfile_changed(&obj->options.uservol);
1840 if (parent_or_child == 0) {
1841 LOG(log_debug, logtype_afpd, "load_volumes: AFP MASTER");
1843 LOG(log_debug, logtype_afpd, "load_volumes: user: %s", obj->username);
1846 pwent = getpwnam(obj->username);
1847 if ( (obj->options.flags & OPTION_USERVOLFIRST) == 0 ) {
1848 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent);
1851 if ((*obj->username == '\0') || (obj->options.flags & OPTION_NOUSERVOL)) {
1852 readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent);
1855 * Read user's AppleVolumes or .AppleVolumes file
1856 * If neither are readable, read the default volumes file. if
1857 * that doesn't work, create a user share.
1859 if (obj->options.uservol.name) {
1860 free(obj->options.uservol.name);
1862 obj->options.uservol.name = strdup(pwent->pw_dir);
1863 if ( readvolfile(obj, &obj->options.uservol, "AppleVolumes", 1, pwent) < 0 &&
1864 readvolfile(obj, &obj->options.uservol, ".AppleVolumes", 1, pwent) < 0 &&
1865 readvolfile(obj, &obj->options.uservol, "applevolumes", 1, pwent) < 0 &&
1866 readvolfile(obj, &obj->options.uservol, ".applevolumes", 1, pwent) < 0 &&
1867 obj->options.defaultvol.name != NULL ) {
1868 if (readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent) < 0)
1869 creatvol(obj, pwent, pwent->pw_dir, NULL, NULL, 1);
1872 if ( obj->options.flags & OPTION_USERVOLFIRST ) {
1873 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent );
1876 if ( obj->options.closevol ) {
1879 for ( vol = Volumes; vol; vol = vol->v_next ) {
1880 if (vol->v_deleted && !vol->v_new ) {
1889 /* ------------------------------- */
1890 int afp_getsrvrparms(AFPObj *obj, char *ibuf _U_, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
1903 for ( vcnt = 0, volume = Volumes; volume; volume = volume->v_next ) {
1904 if (!(volume->v_flags & AFPVOL_NOSTAT)) {
1907 if ( stat( volume->v_path, &st ) < 0 ) {
1908 LOG(log_info, logtype_afpd, "afp_getsrvrparms(%s): stat: %s",
1909 volume->v_path, strerror(errno) );
1910 continue; /* can't access directory */
1912 if (!S_ISDIR(st.st_mode)) {
1913 continue; /* not a dir */
1915 accessmode(volume, volume->v_path, &ma, NULL, &st);
1916 if ((ma.ma_user & (AR_UREAD | AR_USEARCH)) != (AR_UREAD | AR_USEARCH)) {
1917 continue; /* no r-x access */
1920 if (volume->v_hide) {
1921 continue; /* config file changed but the volume was mounted */
1924 if (utf8_encoding()) {
1925 len = ucs2_to_charset_allocate(CH_UTF8_MAC, &namebuf, volume->v_u8mname);
1927 len = ucs2_to_charset_allocate(obj->options.maccharset, &namebuf, volume->v_macname);
1930 if (len == (size_t)-1)
1933 /* set password bit if there's a volume password */
1934 *data = (volume->v_password) ? AFPSRVR_PASSWD : 0;
1936 *data++ |= 0; /* UNIX PRIVS BIT ..., OSX doesn't seem to use it, so we don't either */
1938 memcpy(data, namebuf, len );
1944 *rbuflen = data - rbuf;
1946 if ( gettimeofday( &tv, NULL ) < 0 ) {
1947 LOG(log_error, logtype_afpd, "afp_getsrvrparms(%s): gettimeofday: %s", volume->v_path, strerror(errno) );
1949 return AFPERR_PARAM;
1951 tv.tv_sec = AD_DATE_FROM_UNIX(tv.tv_sec);
1952 memcpy(data, &tv.tv_sec, sizeof( uint32_t));
1953 data += sizeof( uint32_t);
1958 /* ------------------------- */
1959 static int volume_codepage(AFPObj *obj, struct vol *volume)
1961 struct charset_functions *charset;
1964 if (!volume->v_volcodepage)
1965 volume->v_volcodepage = strdup("UTF8");
1967 if ( (charset_t) -1 == ( volume->v_volcharset = add_charset(volume->v_volcodepage)) ) {
1968 LOG (log_error, logtype_afpd, "Setting codepage %s as volume codepage failed", volume->v_volcodepage);
1972 if ( NULL == (charset = find_charset_functions(volume->v_volcodepage)) || charset->flags & CHARSET_ICONV ) {
1973 LOG (log_warning, logtype_afpd, "WARNING: volume encoding %s is *not* supported by netatalk, expect problems !!!!", volume->v_volcodepage);
1976 if (!volume->v_maccodepage)
1977 volume->v_maccodepage = strdup(obj->options.maccodepage);
1979 if ( (charset_t) -1 == ( volume->v_maccharset = add_charset(volume->v_maccodepage)) ) {
1980 LOG (log_error, logtype_afpd, "Setting codepage %s as mac codepage failed", volume->v_maccodepage);
1984 if ( NULL == ( charset = find_charset_functions(volume->v_maccodepage)) || ! (charset->flags & CHARSET_CLIENT) ) {
1985 LOG (log_error, logtype_afpd, "Fatal error: mac charset %s not supported", volume->v_maccodepage);
1988 volume->v_kTextEncoding = htonl(charset->kTextEncoding);
1992 /* ------------------------- */
1993 static int volume_openDB(struct vol *volume)
1997 if ((volume->v_flags & AFPVOL_NODEV)) {
1998 flags |= CNID_FLAG_NODEV;
2001 if (volume->v_cnidscheme == NULL) {
2002 volume->v_cnidscheme = strdup(DEFAULT_CNID_SCHEME);
2003 LOG(log_info, logtype_afpd, "Volume %s use CNID scheme %s.",
2004 volume->v_path, volume->v_cnidscheme);
2007 LOG(log_info, logtype_afpd, "CNID server: %s:%s",
2008 volume->v_cnidserver ? volume->v_cnidserver : Cnid_srv,
2009 volume->v_cnidport ? volume->v_cnidport : Cnid_port);
2012 /* Found this in branch dir-rewrite, maybe we want to use it sometimes */
2014 /* Legacy pre 2.1 way of sharing eg CD-ROM */
2015 if (strcmp(volume->v_cnidscheme, "last") == 0) {
2016 /* "last" is gone. We support it by switching to in-memory "tdb" */
2017 volume->v_cnidscheme = strdup("tdb");
2018 flags |= CNID_FLAG_MEMORY;
2021 /* New way of sharing CD-ROM */
2022 if (volume->v_flags & AFPVOL_CDROM) {
2023 flags |= CNID_FLAG_MEMORY;
2024 if (strcmp(volume->v_cnidscheme, "tdb") != 0) {
2025 free(volume->v_cnidscheme);
2026 volume->v_cnidscheme = strdup("tdb");
2027 LOG(log_info, logtype_afpd, "Volume %s is ejectable, switching to scheme %s.",
2028 volume->v_path, volume->v_cnidscheme);
2033 volume->v_cdb = cnid_open(volume->v_path,
2035 volume->v_cnidscheme,
2037 volume->v_cnidserver ? volume->v_cnidserver : Cnid_srv,
2038 volume->v_cnidport ? volume->v_cnidport : Cnid_port);
2040 if ( ! volume->v_cdb && ! (flags & CNID_FLAG_MEMORY)) {
2041 /* The first attempt failed and it wasn't yet an attempt to open in-memory */
2042 LOG(log_error, logtype_afpd, "Can't open volume \"%s\" CNID backend \"%s\" ",
2043 volume->v_path, volume->v_cnidscheme);
2044 LOG(log_error, logtype_afpd, "Reopen volume %s using in memory temporary CNID DB.",
2046 flags |= CNID_FLAG_MEMORY;
2047 volume->v_cdb = cnid_open (volume->v_path, volume->v_umask, "tdb", flags, NULL, NULL);
2049 /* kill ourself with SIGUSR2 aka msg pending */
2050 if (volume->v_cdb) {
2051 setmessage("Something wrong with the volume's CNID DB, using temporary CNID DB instead."
2052 "Check server messages for details!");
2053 kill(getpid(), SIGUSR2);
2054 /* deactivate cnid caching/storing in AppleDouble files */
2059 return (!volume->v_cdb)?-1:0;
2063 Check if the underlying filesystem supports EAs for ea:sys volumes.
2064 If not, switch to ea:ad.
2065 As we can't check (requires write access) on ro-volumes, we switch ea:auto
2066 volumes that are options:ro to ea:none.
2068 static void check_ea_sys_support(struct vol *vol)
2070 uid_t process_uid = 0;
2071 char eaname[] = {"org.netatalk.supports-eas.XXXXXX"};
2072 const char *eacontent = "yes";
2074 if (vol->v_vfs_ea == AFPVOL_EA_AUTO) {
2076 if ((vol->v_flags & AFPVOL_RO) == AFPVOL_RO) {
2077 LOG(log_info, logtype_afpd, "read-only volume '%s', can't test for EA support, disabling EAs", vol->v_localname);
2078 vol->v_vfs_ea = AFPVOL_EA_NONE;
2084 process_uid = geteuid();
2086 if (seteuid(0) == -1) {
2087 LOG(log_error, logtype_afpd, "check_ea_sys_support: can't seteuid(0): %s", strerror(errno));
2091 if ((sys_setxattr(vol->v_path, eaname, eacontent, 4, 0)) == 0) {
2092 sys_removexattr(vol->v_path, eaname);
2093 vol->v_vfs_ea = AFPVOL_EA_SYS;
2095 LOG(log_warning, logtype_afpd, "volume \"%s\" does not support Extended Attributes, using ea:ad instead",
2097 vol->v_vfs_ea = AFPVOL_EA_AD;
2101 if (seteuid(process_uid) == -1) {
2102 LOG(log_error, logtype_afpd, "can't seteuid back %s", strerror(errno));
2109 /* -------------------------
2110 * we are the user here
2112 int afp_openvol(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2123 char path[ MAXPATHLEN + 1];
2129 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2130 bitmap = ntohs( bitmap );
2131 ibuf += sizeof( bitmap );
2134 if (( bitmap & (1<<VOLPBIT_VID)) == 0 ) {
2135 return AFPERR_BITMAP;
2138 len = (unsigned char)*ibuf++;
2139 volname = obj->oldtmp;
2141 if ((volname_tmp = strchr(volname,'+')) != NULL)
2142 volname = volname_tmp+1;
2144 if (utf8_encoding()) {
2145 namelen = convert_string(CH_UTF8_MAC, CH_UCS2, ibuf, len, volname, sizeof(obj->oldtmp));
2147 namelen = convert_string(obj->options.maccharset, CH_UCS2, ibuf, len, volname, sizeof(obj->oldtmp));
2150 if ( namelen <= 0) {
2151 return AFPERR_PARAM;
2155 if ((len + 1) & 1) /* pad to an even boundary */
2160 for ( volume = Volumes; volume; volume = volume->v_next ) {
2161 if ( strcasecmp_w( (ucs2_t*) volname, volume->v_name ) == 0 ) {
2166 if ( volume == NULL ) {
2167 return AFPERR_PARAM;
2170 /* check for a volume password */
2171 if (volume->v_password && strncmp(ibuf, volume->v_password, VOLPASSLEN)) {
2172 return AFPERR_ACCESS;
2175 if (( volume->v_flags & AFPVOL_OPEN ) ) {
2176 /* the volume is already open */
2177 return stat_vol(bitmap, volume, rbuf, rbuflen);
2180 if (volume->v_root_preexec) {
2181 if ((ret = afprun(1, volume->v_root_preexec, NULL)) && volume->v_root_preexec_close) {
2182 LOG(log_error, logtype_afpd, "afp_openvol(%s): root preexec : %d", volume->v_path, ret );
2187 if (volume->v_preexec) {
2188 if ((ret = afprun(0, volume->v_preexec, NULL)) && volume->v_preexec_close) {
2189 LOG(log_error, logtype_afpd, "afp_openvol(%s): preexec : %d", volume->v_path, ret );
2194 if ( stat( volume->v_path, &st ) < 0 ) {
2195 return AFPERR_PARAM;
2198 if ( chdir( volume->v_path ) < 0 ) {
2199 return AFPERR_PARAM;
2202 if ( NULL == getcwd(path, MAXPATHLEN)) {
2203 /* shouldn't be fatal but it will fail later */
2204 LOG(log_error, logtype_afpd, "afp_openvol(%s): volume pathlen too long", volume->v_path);
2208 /* Normalize volume path */
2209 #ifdef REALPATH_TAKES_NULL
2210 if ((volume->v_path = realpath(path, NULL)) == NULL)
2213 if ((volume->v_path = malloc(MAXPATHLEN+1)) == NULL)
2215 if (realpath(path, volume->v_path) == NULL) {
2216 free(volume->v_path);
2219 /* Safe some memory */
2221 if ((tmp = strdup(volume->v_path)) == NULL) {
2222 free(volume->v_path);
2225 free(volume->v_path);
2226 volume->v_path = tmp;
2229 if (volume_codepage(obj, volume) < 0) {
2234 /* initialize volume variables
2237 if (utf8_encoding()) {
2238 volume->max_filename = UTF8FILELEN_EARLY;
2241 volume->max_filename = MACFILELEN;
2244 volume->v_flags |= AFPVOL_OPEN;
2245 volume->v_cdb = NULL;
2247 if (utf8_encoding()) {
2248 len = convert_string_allocate(CH_UCS2, CH_UTF8_MAC, volume->v_u8mname, namelen, &vol_mname);
2250 len = convert_string_allocate(CH_UCS2, obj->options.maccharset, volume->v_macname, namelen, &vol_mname);
2252 if ( !vol_mname || len <= 0) {
2257 if ((vol_uname = strrchr(path, '/')) == NULL)
2259 else if (*(vol_uname + 1) != '\0')
2262 if ((dir = dir_new(vol_mname,
2267 bfromcstr(volume->v_path),
2271 LOG(log_error, logtype_afpd, "afp_openvol(%s): malloc: %s", volume->v_path, strerror(errno) );
2276 volume->v_root = dir;
2279 if (volume_openDB(volume) < 0) {
2280 LOG(log_error, logtype_afpd, "Fatal error: cannot open CNID or invalid CNID backend for %s: %s",
2281 volume->v_path, volume->v_cnidscheme);
2286 ret = stat_vol(bitmap, volume, rbuf, rbuflen);
2288 if (ret == AFP_OK) {
2289 handle_special_folders(volume);
2291 volume->v_cnidserver ? volume->v_cnidserver : Cnid_srv,
2292 volume->v_cnidport ? volume->v_cnidport : Cnid_port);
2296 * If you mount a volume twice, the second time the trash appears on
2297 * the desk-top. That's because the Mac remembers the DID for the
2298 * trash (even for volumes in different zones, on different servers).
2299 * Just so this works better, we prime the DID cache with the trash,
2300 * fixing the trash at DID 17.
2301 * FIXME (RL): should it be done inside a CNID backend ? (always returning Trash DID when asked) ?
2303 if ((volume->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
2305 /* FIXME find db time stamp */
2306 if (cnid_getstamp(volume->v_cdb, volume->v_stamp, sizeof(volume->v_stamp)) < 0) {
2307 LOG (log_error, logtype_afpd,
2308 "afp_openvol(%s): Fatal error: Unable to get stamp value from CNID backend",
2316 cname( volume, volume->v_root, &p );
2322 if (volume->v_root) {
2323 dir_free( volume->v_root );
2324 volume->v_root = NULL;
2327 volume->v_flags &= ~AFPVOL_OPEN;
2328 if (volume->v_cdb != NULL) {
2329 cnid_close(volume->v_cdb);
2330 volume->v_cdb = NULL;
2336 /* ------------------------- */
2337 static void closevol(struct vol *vol)
2342 dir_free( vol->v_root );
2344 if (vol->v_cdb != NULL) {
2345 cnid_close(vol->v_cdb);
2349 if (vol->v_postexec) {
2350 afprun(0, vol->v_postexec, NULL);
2352 if (vol->v_root_postexec) {
2353 afprun(1, vol->v_root_postexec, NULL);
2357 /* ------------------------- */
2358 void close_all_vol(void)
2362 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
2363 if ( (ovol->v_flags & AFPVOL_OPEN) ) {
2364 ovol->v_flags &= ~AFPVOL_OPEN;
2370 /* ------------------------- */
2371 static void deletevol(struct vol *vol)
2375 vol->v_flags &= ~AFPVOL_OPEN;
2376 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
2377 if ( (ovol->v_flags & AFPVOL_OPEN) ) {
2381 if ( ovol != NULL ) {
2382 /* Even if chdir fails, we can't say afp_closevol fails. */
2383 if ( chdir( ovol->v_path ) == 0 ) {
2384 curdir = ovol->v_root;
2389 if (vol->v_deleted) {
2390 showvol(vol->v_name);
2397 /* ------------------------- */
2398 int afp_closevol(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2405 memcpy(&vid, ibuf, sizeof( vid ));
2406 if (NULL == ( vol = getvolbyvid( vid )) ) {
2407 return( AFPERR_PARAM );
2416 /* ------------------------- */
2417 struct vol *getvolbyvid(const uint16_t vid )
2421 for ( vol = Volumes; vol; vol = vol->v_next ) {
2422 if ( vid == vol->v_vid ) {
2426 if ( vol == NULL || ( vol->v_flags & AFPVOL_OPEN ) == 0 ) {
2435 /* ------------------------ */
2436 static int ext_cmp_key(const void *key, const void *obj)
2438 const char *p = key;
2439 const struct extmap *em = obj;
2440 return strdiacasecmp(p, em->em_ext);
2442 struct extmap *getextmap(const char *path)
2447 if (!Extmap_cnt || NULL == ( p = strrchr( path, '.' )) ) {
2448 return( Defextmap );
2452 return( Defextmap );
2454 em = bsearch(p, Extmap, Extmap_cnt, sizeof(struct extmap), ext_cmp_key);
2458 return( Defextmap );
2462 /* ------------------------- */
2463 struct extmap *getdefextmap(void)
2465 return( Defextmap );
2468 /* --------------------------
2469 poll if a volume is changed by other processes.
2471 0 no attention msg sent
2472 1 attention msg sent
2473 -1 error (socket closed)
2475 Note: if attention return -1 no packet has been
2476 sent because the buffer is full, we don't care
2477 either there's no reader or there's a lot of
2478 traffic and another pollvoltime will follow
2480 int pollvoltime(AFPObj *obj)
2487 if (!(afp_version > 21 && obj->options.server_notif))
2490 if ( gettimeofday( &tv, NULL ) < 0 )
2493 for ( vol = Volumes; vol; vol = vol->v_next ) {
2494 if ( (vol->v_flags & AFPVOL_OPEN) && vol->v_mtime + 30 < tv.tv_sec) {
2495 if ( !stat( vol->v_path, &st ) && vol->v_mtime != st.st_mtime ) {
2496 vol->v_mtime = st.st_mtime;
2497 if (!obj->attention(obj->dsi, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED))
2506 /* ------------------------- */
2507 void setvoltime(AFPObj *obj, struct vol *vol)
2511 if ( gettimeofday( &tv, NULL ) < 0 ) {
2512 LOG(log_error, logtype_afpd, "setvoltime(%s): gettimeofday: %s", vol->v_path, strerror(errno) );
2515 if( utime( vol->v_path, NULL ) < 0 ) {
2516 /* write of time failed ... probably a read only filesys,
2517 * where no other users can interfere, so there's no issue here
2521 /* a little granularity */
2522 if (vol->v_mtime < tv.tv_sec) {
2523 vol->v_mtime = tv.tv_sec;
2524 /* or finder doesn't update free space
2525 * AFP 3.2 and above clients seem to be ok without so many notification
2527 if (afp_version < 32 && obj->options.server_notif) {
2528 obj->attention(obj->dsi, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED);
2533 /* ------------------------- */
2534 int afp_getvolparams(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_,char *rbuf, size_t *rbuflen)
2537 uint16_t vid, bitmap;
2540 memcpy(&vid, ibuf, sizeof( vid ));
2541 ibuf += sizeof( vid );
2542 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2543 bitmap = ntohs( bitmap );
2545 if (NULL == ( vol = getvolbyvid( vid )) ) {
2547 return( AFPERR_PARAM );
2550 return stat_vol(bitmap, vol, rbuf, rbuflen);
2553 /* ------------------------- */
2554 int afp_setvolparams(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2558 uint16_t vid, bitmap;
2564 memcpy(&vid, ibuf, sizeof( vid ));
2565 ibuf += sizeof( vid );
2566 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2567 bitmap = ntohs( bitmap );
2568 ibuf += sizeof(bitmap);
2570 if (( vol = getvolbyvid( vid )) == NULL ) {
2571 return( AFPERR_PARAM );
2574 if ((vol->v_flags & AFPVOL_RO))
2575 return AFPERR_VLOCK;
2577 /* we can only set the backup date. */
2578 if (bitmap != (1 << VOLPBIT_BDATE))
2579 return AFPERR_BITMAP;
2582 if ( ad_open(&ad, vol->v_path, ADFLAGS_HF | ADFLAGS_DIR | ADFLAGS_RDWR) < 0 ) {
2584 return AFPERR_VLOCK;
2586 return AFPERR_ACCESS;
2589 memcpy(&aint, ibuf, sizeof(aint));
2590 ad_setdate(&ad, AD_DATE_BACKUP, aint);
2592 ad_close(&ad, ADFLAGS_HF);
2597 * precreate a folder
2598 * this is only intended for folders in the volume root
2599 * It will *not* work if the folder name contains extended characters
2601 static int create_special_folder (const struct vol *vol, const struct _special_folder *folder)
2610 p = (char *) malloc ( strlen(vol->v_path)+strlen(folder->name)+2);
2612 LOG(log_error, logtype_afpd,"malloc failed");
2616 q=strdup(folder->name);
2618 LOG(log_error, logtype_afpd,"malloc failed");
2622 strcpy(p, vol->v_path);
2627 if ((vol->v_casefold & AFPVOL_MTOUUPPER))
2629 else if ((vol->v_casefold & AFPVOL_MTOULOWER))
2635 if ( (ret = stat( p, &st )) < 0 ) {
2636 if (folder->precreate) {
2637 if (ad_mkdir(p, folder->mode)) {
2638 LOG(log_debug, logtype_afpd,"Creating '%s' failed in %s: %s", p, vol->v_path, strerror(errno));
2647 if ( !ret && folder->hide) {
2650 if (ad_open(&ad, p, ADFLAGS_HF | ADFLAGS_DIR | ADFLAGS_RDWR | ADFLAGS_CREATE, 0666) != 0) {
2656 ad_setname(&ad, folder->name);
2658 ad_getattr(&ad, &attr);
2659 attr |= htons( ntohs( attr ) | ATTRBIT_INVISIBLE );
2660 ad_setattr(&ad, attr);
2662 /* do the same with the finder info */
2663 if (ad_entry(&ad, ADEID_FINDERI)) {
2664 memcpy(&attr, ad_entry(&ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF, sizeof(attr));
2665 attr |= htons(FINDERINFO_INVISIBLE);
2666 memcpy(ad_entry(&ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF,&attr, sizeof(attr));
2670 ad_close(&ad, ADFLAGS_HF);
2677 static void handle_special_folders (const struct vol * vol)
2679 const _special_folder *p = &special_folders[0];
2682 process_uid = geteuid();
2684 if (seteuid(0) == -1) {
2689 for (; p->name != NULL; p++) {
2690 create_special_folder (vol, p);
2694 if (seteuid(process_uid) == -1) {
2695 LOG(log_error, logtype_logger, "can't seteuid back %s", strerror(errno));
2701 const struct vol *getvolumes(void)
2706 void unload_volumes_and_extmap(void)
2708 LOG(log_debug, logtype_afpd, "unload_volumes_and_extmap");
2714 * Get a volumes UUID from the config file.
2715 * If there is none, it is generated and stored there.
2717 * Returns pointer to allocated storage on success, NULL on error.
2719 static char *get_vol_uuid(const AFPObj *obj, const char *volname)
2722 char buf[1024], uuid[UUID_PRINTABLE_STRING_LENGTH], *p;
2724 struct stat tmpstat;
2727 if ((fp = fopen(obj->options.uuidconf, "r")) != NULL) { /* read open? */
2728 /* scan in the conf file */
2729 while (fgets(buf, sizeof(buf), fp) != NULL) {
2731 while (p && isblank(*p))
2733 if (!p || (*p == '#') || (*p == '\n'))
2734 continue; /* invalid line */
2737 if ((volname_conf = strtok( p, "\"" )) == NULL)
2738 continue; /* syntax error */
2740 if ((volname_conf = strtok( p, " \t" )) == NULL)
2741 continue; /* syntax error: invalid name */
2743 p = strchr(p, '\0');
2746 continue; /* syntax error */
2748 if (strcmp(volname, volname_conf) != 0)
2749 continue; /* another volume name */
2751 while (p && isblank(*p))
2754 if (sscanf(p, "%36s", uuid) == 1 ) {
2755 for (int i=0; uuid[i]; i++)
2756 uuid[i] = toupper(uuid[i]);
2757 LOG(log_debug, logtype_afpd, "get_uuid('%s'): UUID: '%s'", volname, uuid);
2759 return strdup(uuid);
2767 /* not found or no file, reopen in append mode */
2769 if (stat(obj->options.uuidconf, &tmpstat)) { /* no file */
2770 if (( fd = creat(obj->options.uuidconf, 0644 )) < 0 ) {
2771 LOG(log_error, logtype_afpd, "ERROR: Cannot create %s (%s).",
2772 obj->options.uuidconf, strerror(errno));
2775 if (( fp = fdopen( fd, "w" )) == NULL ) {
2776 LOG(log_error, logtype_afpd, "ERROR: Cannot fdopen %s (%s).",
2777 obj->options.uuidconf, strerror(errno));
2781 } else if ((fp = fopen(obj->options.uuidconf, "a+")) == NULL) { /* not found */
2782 LOG(log_error, logtype_afpd, "Cannot create or append to %s (%s).",
2783 obj->options.uuidconf, strerror(errno));
2786 fseek(fp, 0L, SEEK_END);
2787 if(ftell(fp) == 0) { /* size = 0 */
2788 fprintf(fp, "# DON'T TOUCH NOR COPY THOUGHTLESSLY!\n");
2789 fprintf(fp, "# This file is auto-generated by afpd\n");
2790 fprintf(fp, "# and stores UUIDs for TM volumes.\n\n");
2792 fseek(fp, -1L, SEEK_END);
2793 if(fgetc(fp) != '\n') fputc('\n', fp); /* last char is \n? */
2796 /* generate uuid and write to file */
2799 randombytes((void *)id, 16);
2800 cp = uuid_bin2string(id);
2802 LOG(log_debug, logtype_afpd, "get_uuid('%s'): generated UUID '%s'", volname, cp);
2804 fprintf(fp, "\"%s\"\t%36s\n", volname, cp);