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>
40 #include <atalk/iniparser.h>
43 #include <atalk/cnid.h>
46 #include "directory.h"
55 extern int afprun(int root, char *cmd, int *outfd);
58 struct vol *current_vol; /* last volume from getvolbyvid() */
60 static struct vol *Volumes = NULL;
61 static uint16_t lastvid = 0;
62 static char *Trash = "\02\024Network Trash Folder";
64 #define VOLOPT_ALLOW 0 /* user allow list */
65 #define VOLOPT_DENY 1 /* user deny list */
66 #define VOLOPT_RWLIST 2 /* user rw list */
67 #define VOLOPT_ROLIST 3 /* user ro list */
68 #define VOLOPT_PASSWORD 4 /* volume password */
69 #define VOLOPT_CASEFOLD 5 /* character case mangling */
70 #define VOLOPT_FLAGS 6 /* various flags */
71 #define VOLOPT_DBPATH 7 /* path to database */
72 #define VOLOPT_LIMITSIZE 8 /* Limit the size of the volume */
74 #define VOLOPT_VETO 10 /* list of veto filespec */
75 #define VOLOPT_PREEXEC 11 /* preexec command */
76 #define VOLOPT_ROOTPREEXEC 12 /* root preexec command */
77 #define VOLOPT_POSTEXEC 13 /* postexec command */
78 #define VOLOPT_ROOTPOSTEXEC 14 /* root postexec command */
79 #define VOLOPT_ENCODING 15 /* mac encoding (pre OSX)*/
80 #define VOLOPT_MACCHARSET 16
81 #define VOLOPT_CNIDSCHEME 17
82 #define VOLOPT_ADOUBLE 18 /* adouble version */
83 /* Usable slot: 19/20 */
84 #define VOLOPT_UMASK 21
85 #define VOLOPT_ALLOWED_HOSTS 22
86 #define VOLOPT_DENIED_HOSTS 23
87 #define VOLOPT_DPERM 24 /* dperm default directories perms */
88 #define VOLOPT_FPERM 25 /* fperm default files perms */
89 #define VOLOPT_DFLTPERM 26 /* perm */
90 #define VOLOPT_EA_VFS 27 /* Extended Attributes vfs indirection */
91 #define VOLOPT_CNIDSERVER 28 /* CNID Server ip address*/
92 #define VOLOPT_CNIDPORT 30 /* CNID server tcp port */
94 #define VOLOPT_MAX 31 /* <== IMPORTANT !!!!!! */
95 #define VOLOPT_NUM (VOLOPT_MAX + 1)
104 typedef struct _special_folder {
111 static const _special_folder special_folders[] = {
112 {"Network Trash Folder", 1, 0777, 1},
113 {".AppleDesktop", 1, 0777, 0},
116 /* Forward declarations */
117 static void handle_special_folders (const struct vol *);
118 static void deletevol(struct vol *vol);
119 static void volume_free(struct vol *vol);
120 static void check_ea_sys_support(struct vol *vol);
121 static char *get_vol_uuid(const AFPObj *obj, const char *volname);
123 static void volfree(struct vol_option *options, const struct vol_option *save)
128 for (i = 0; i < VOLOPT_MAX; i++) {
129 if (options[i].c_value && (options[i].c_value != save[i].c_value))
130 free(options[i].c_value);
133 for (i = 0; i < VOLOPT_MAX; i++) {
134 if (options[i].c_value)
135 free(options[i].c_value);
141 #define is_var(a, b) (strncmp((a), (b), 2) == 0)
144 * Handle variable substitutions. here's what we understand:
145 * $b -> basename of path
146 * $c -> client ip/appletalk address
147 * $d -> volume pathname on server
148 * $f -> full name (whatever's in the gecos field)
151 * $i -> client ip/appletalk address without port
152 * $s -> server name (hostname if it doesn't exist)
153 * $u -> username (guest is usually nobody)
154 * $v -> volume name or basename if null
157 * This get's called from readvolfile with
158 * path = NULL, volname = NULL for xlating the volumes path
159 * path = path, volname = NULL for xlating the volumes name
160 * ... and from volumes options parsing code when xlating eg dbpath with
161 * path = path, volname = volname
163 * Using this information we can reject xlation of any variable depeninding on a login
164 * context which is not given in the afp master, where we must evaluate this whole stuff
165 * too for the Zeroconf announcements.
167 static char *volxlate(AFPObj *obj,
180 int xlatevolname = 0;
182 if (parent_or_child == 0)
185 if (path && !volname)
193 dest = calloc(destlen +1, 1);
199 strlcpy(dest, src, destlen +1);
200 if ((p = strchr(src, '$')) == NULL) /* nothing to do */
203 /* first part of the path. just forward to the next variable. */
204 len = MIN((size_t)(p - src), destlen);
210 while (p && destlen > 0) {
211 /* now figure out what the variable is */
213 if (is_var(p, "$b")) {
214 if (afpmaster && xlatevolname)
217 if ((q = strrchr(path, '/')) == NULL)
219 else if (*(q + 1) != '\0')
222 } else if (is_var(p, "$c")) {
223 if (afpmaster && xlatevolname)
226 len = sprintf(dest, "%s:%u",
227 getip_string((struct sockaddr *)&dsi->client),
228 getip_port((struct sockaddr *)&dsi->client));
231 } else if (is_var(p, "$d")) {
232 if (afpmaster && xlatevolname)
235 } else if (pwd && is_var(p, "$f")) {
236 if (afpmaster && xlatevolname)
238 if ((r = strchr(pwd->pw_gecos, ',')))
241 } else if (pwd && is_var(p, "$g")) {
242 if (afpmaster && xlatevolname)
244 struct group *grp = getgrgid(pwd->pw_gid);
247 } else if (is_var(p, "$h")) {
248 q = obj->options.hostname;
249 } else if (is_var(p, "$i")) {
250 if (afpmaster && xlatevolname)
253 q = getip_string((struct sockaddr *)&dsi->client);
254 } else if (is_var(p, "$s")) {
255 q = obj->options.hostname;
256 } else if (obj->username && is_var(p, "$u")) {
257 if (afpmaster && xlatevolname)
260 if ( obj->options.ntseparator && (sep = strchr(obj->username, obj->options.ntseparator[0])) != NULL)
264 } else if (is_var(p, "$v")) {
265 if (afpmaster && xlatevolname)
271 if ((q = strrchr(path, '/')) == NULL)
273 else if (*(q + 1) != '\0')
276 } else if (is_var(p, "$$")) {
281 /* copy the stuff over. if we don't understand something that we
282 * should, just skip it over. */
284 len = MIN(p == q ? 2 : strlen(q), destlen);
285 strncpy(dest, q, len);
290 /* stuff up to next $ */
292 p = strchr(src, '$');
293 len = p ? MIN((size_t)(p - src), destlen) : destlen;
295 strncpy(dest, src, len);
303 /* -------------------- */
304 static void setoption(struct vol_option *options, const struct vol_option *save, int opt, const char *val)
306 if (options[opt].c_value && (!save || options[opt].c_value != save[opt].c_value))
307 free(options[opt].c_value);
308 options[opt].c_value = strdup(val + 1);
311 /* Parse iniconfig and initalize volume options */
312 static void volset(const dictionary *conf, const char *vol, struct vol_option *options, const struct vol_option *save)
317 if (val = iniparser_getstring(conf, vol, "allow", NULL))
318 setoption(options, save, VOLOPT_ALLOW, val);
320 if (val = iniparser_getstring(conf, vol, "deny", NULL))
321 setoption(options, save, VOLOPT_DENY, val);
323 if (val = iniparser_getstring(conf, vol, "rwlist", NULL))
324 setoption(options, save, VOLOPT_RWLIST, val);
326 if (val = iniparser_getstring(conf, vol, "rolist", NULL))
327 setoption(options, save, VOLOPT_ROLIST, val);
329 if (val = iniparser_getstring(conf, vol, "volcharset", NULL))
330 setoption(options, save, VOLOPT_ENCODING, val);
332 if (val = iniparser_getstring(conf, vol, "maccharset", NULL))
333 setoption(options, save, VOLOPT_MACCHARSET, val);
335 if (val = iniparser_getstring(conf, vol, "veto", NULL))
336 setoption(options, save, VOLOPT_VETO, val);
338 if (val = iniparser_getstring(conf, vol, "cnidscheme", NULL))
339 setoption(options, save, VOLOPT_CNIDSCHEME, val);
341 if (val = iniparser_getstring(conf, vol, "dbpath", NULL))
342 setoption(options, save, VOLOPT_DBPATH, val);
344 if (val = iniparser_getstring(conf, vol, "password", NULL))
345 setoption(options, save, VOLOPT_PASSWORD, val);
347 if (val = iniparser_getstring(conf, vol, "root_preexec", NULL))
348 setoption(options, save, VOLOPT_ROOTPREEXEC, val);
350 if (val = iniparser_getstring(conf, vol, "preexec", NULL))
351 setoption(options, save, VOLOPT_PREEXEC, val);
353 if (val = iniparser_getstring(conf, vol, "root_postexec", NULL))
354 setoption(options, save, VOLOPT_ROOTPOSTEXEC, val);
356 if (val = iniparser_getstring(conf, vol, "postexec", NULL))
357 setoption(options, save, VOLOPT_POSTEXEC, val);
359 if (val = iniparser_getstring(conf, vol, "allowed_hosts", NULL))
360 setoption(options, save, VOLOPT_ALLOWED_HOSTS, val);
362 if (val = iniparser_getstring(conf, vol, "denied_hosts", NULL))
363 setoption(options, save, VOLOPT_DENIED_HOSTS, val);
365 if (val = iniparser_getstring(conf, vol, "umask", NULL))
366 options[VOLOPT_UMASK].i_value = (int)strtol(val, NULL, 8);
368 if (val = iniparser_getstring(conf, vol, "dperm", NULL))
369 options[VOLOPT_DPERM].i_value = (int)strtol(val, NULL, 8);
371 if (val = iniparser_getstring(conf, vol, "fperm", NULL))
372 options[VOLOPT_FPERM].i_value = (int)strtol(val, NULL, 8);
374 if (val = iniparser_getstring(conf, vol, "perm", NULL))
375 options[VOLOPT_DFLTPERM].i_value = (int)strtol(val, NULL, 8);
377 if (val = iniparser_getstring(conf, vol, "volsizelimit", NULL))
378 options[VOLOPT_LIMITSIZE].i_value = (uint32_t)strtoul(val, NULL, 10);
380 if (val = iniparser_getstring(conf, vol, "casefold", NULL)) {
381 if (strcasecmp(val, "tolower") == 0)
382 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMLOWER;
383 else if (strcasecmp(val, "toupper") == 0)
384 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMUPPER;
385 else if (strcasecmp(val, "xlatelower") == 0)
386 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UUPPERMLOWER;
387 else if (strcasecmp(val, "xlateupper") == 0)
388 options[VOLOPT_CASEFOLD].i_value = AFPVOL_ULOWERMUPPER;
391 if (val = iniparser_getstring(conf, vol, "adouble", NULL)) {
392 if (strcasecmp(val, "v2") == 0)
393 options[VOLOPT_ADOUBLE].i_value = AD_VERSION2;
394 else if (strcasecmp(val, "ea") == 0)
395 options[VOLOPT_ADOUBLE].i_value = AD_VERSION_EA;
398 if (val = iniparser_getstring(conf, vol, "ea", NULL)) {
399 if (strcasecmp(val, "ad") == 0)
400 options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_AD;
401 else if (strcasecmp(val, "sys") == 0)
402 options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_SYS;
403 else if (strcasecmp(val, "none") == 0)
404 options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_NONE;
407 if (p = iniparser_getstrdup(conf, vol, "cnidserver", NULL)) {
408 if (q = strrchr(val, ':')) {
410 setoption(options, save, VOLOPT_CNIDPORT, q + 1);
412 setoption(options, save, VOLOPT_CNIDSERVER, p);
413 LOG(log_debug, logtype_afpd, "CNID Server for volume '%s': %s:%s",
414 vol, p, q ? q + 1 : "4700");
418 if (q = iniparser_getstrdup(conf, vol, "options", NULL)) {
419 if (p = strtok(q, ",")) {
421 if (strcasecmp(p, "ro") == 0)
422 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
423 else if (strcasecmp(p, "nohex") == 0)
424 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOHEX;
425 else if (strcasecmp(p, "usedots") == 0)
426 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS;
427 else if (strcasecmp(p, "invisibledots") == 0)
428 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS | AFPVOL_INV_DOTS;
429 else if (strcasecmp(p, "nostat") == 0)
430 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOSTAT;
431 else if (strcasecmp(p, "preexec_close") == 0)
432 options[VOLOPT_PREEXEC].i_value = 1;
433 else if (strcasecmp(p, "root_preexec_close") == 0)
434 options[VOLOPT_ROOTPREEXEC].i_value = 1;
435 else if (strcasecmp(p, "upriv") == 0)
436 options[VOLOPT_FLAGS].i_value |= AFPVOL_UNIX_PRIV;
437 else if (strcasecmp(p, "nodev") == 0)
438 options[VOLOPT_FLAGS].i_value |= AFPVOL_NODEV;
439 else if (strcasecmp(p, "caseinsensitive") == 0)
440 options[VOLOPT_FLAGS].i_value |= AFPVOL_CASEINSEN;
441 else if (strcasecmp(p, "illegalseq") == 0)
442 options[VOLOPT_FLAGS].i_value |= AFPVOL_EILSEQ;
443 else if (strcasecmp(p, "tm") == 0)
444 options[VOLOPT_FLAGS].i_value |= AFPVOL_TM;
445 else if (strcasecmp(p, "searchdb") == 0)
446 options[VOLOPT_FLAGS].i_value |= AFPVOL_SEARCHDB;
447 else if (strcasecmp(p, "nonetids") == 0)
448 options[VOLOPT_FLAGS].i_value |= AFPVOL_NONETIDS;
449 else if (strcasecmp(p, "noacls") == 0)
450 options[VOLOPT_FLAGS].i_value &= ~AFPVOL_ACLS;
451 else if (strcasecmp(p, "nov2toeaconv") == 0)
452 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOV2TOEACONV;
453 p = strtok(NULL, ",");
460 /* ----------------- */
461 static void showvol(const ucs2_t *name)
464 for ( volume = Volumes; volume; volume = volume->v_next ) {
465 if (volume->v_hide && !strcasecmp_w( volume->v_name, name ) ) {
472 /* ------------------------------- */
473 static int creatvol(AFPObj *obj, struct passwd *pwd,
474 char *path, char *name,
475 struct vol_option *options)
478 int suffixlen, vlen, tmpvlen, u8mvlen, macvlen;
480 char tmpname[AFPVOL_U8MNAMELEN+1];
481 ucs2_t u8mtmpname[(AFPVOL_U8MNAMELEN+1)*2], mactmpname[(AFPVOL_MACNAMELEN+1)*2];
482 char suffix[6]; /* max is #FFFF */
485 LOG(log_debug, logtype_afpd, "createvol: Volume '%s'", name);
487 if ( name == NULL || *name == '\0' ) {
488 if ((name = strrchr( path, '/' )) == NULL) {
489 return -1; /* Obviously not a fully qualified path */
492 /* if you wish to share /, you need to specify a name. */
497 /* suffix for mangling use (lastvid + 1) */
498 /* because v_vid has not been decided yet. */
499 suffixlen = sprintf(suffix, "%c%X", MANGLE_CHAR, lastvid + 1 );
501 vlen = strlen( name );
503 /* Unicode Volume Name */
504 /* Firstly convert name from unixcharset to UTF8-MAC */
506 tmpvlen = convert_charset(obj->options.unixcharset, CH_UTF8_MAC, 0, name, vlen, tmpname, AFPVOL_U8MNAMELEN, &flags);
508 strcpy(tmpname, "???");
512 /* Do we have to mangle ? */
513 if ( (flags & CONV_REQMANGLE) || (tmpvlen > obj->options.volnamelen)) {
514 if (tmpvlen + suffixlen > obj->options.volnamelen) {
516 tmpvlen = convert_charset(obj->options.unixcharset, CH_UTF8_MAC, 0, name, vlen, tmpname, obj->options.volnamelen - suffixlen, &flags);
517 tmpname[tmpvlen >= 0 ? tmpvlen : 0] = 0;
519 strcat(tmpname, suffix);
520 tmpvlen = strlen(tmpname);
523 /* Secondly convert name from UTF8-MAC to UCS2 */
524 if ( 0 >= ( u8mvlen = convert_string(CH_UTF8_MAC, CH_UCS2, tmpname, tmpvlen, u8mtmpname, AFPVOL_U8MNAMELEN*2)) )
527 LOG(log_maxdebug, logtype_afpd, "createvol: Volume '%s' -> UTF8-MAC Name: '%s'", name, tmpname);
529 /* Maccharset Volume Name */
530 /* Firsty convert name from unixcharset to maccharset */
532 tmpvlen = convert_charset(obj->options.unixcharset, obj->options.maccharset, 0, name, vlen, tmpname, AFPVOL_U8MNAMELEN, &flags);
534 strcpy(tmpname, "???");
538 /* Do we have to mangle ? */
539 if ( (flags & CONV_REQMANGLE) || (tmpvlen > AFPVOL_MACNAMELEN)) {
540 if (tmpvlen + suffixlen > AFPVOL_MACNAMELEN) {
542 tmpvlen = convert_charset(obj->options.unixcharset,
543 obj->options.maccharset,
548 AFPVOL_MACNAMELEN - suffixlen,
550 tmpname[tmpvlen >= 0 ? tmpvlen : 0] = 0;
552 strcat(tmpname, suffix);
553 tmpvlen = strlen(tmpname);
556 /* Secondly convert name from maccharset to UCS2 */
557 if ( 0 >= ( macvlen = convert_string(obj->options.maccharset,
562 AFPVOL_U8MNAMELEN*2)) )
565 LOG(log_maxdebug, logtype_afpd, "createvol: Volume '%s' -> Longname: '%s'", name, tmpname);
567 /* check duplicate */
568 for ( volume = Volumes; volume; volume = volume->v_next ) {
569 if ((utf8_encoding() && (strcasecmp_w(volume->v_u8mname, u8mtmpname) == 0))
571 (!utf8_encoding() && (strcasecmp_w(volume->v_macname, mactmpname) == 0))) {
572 LOG (log_error, logtype_afpd,
573 "Duplicate volume name, check AppleVolumes files: previous: \"%s\", new: \"%s\"",
574 volume->v_localname, name);
575 if (volume->v_deleted) {
576 volume->v_new = hide = 1;
579 return -1; /* Won't be able to access it, anyway... */
584 if (!( volume = (struct vol *)calloc(1, sizeof( struct vol ))) ) {
585 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
588 if ( NULL == ( volume->v_localname = strdup(name))) {
589 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
594 if ( NULL == ( volume->v_u8mname = strdup_w(u8mtmpname))) {
595 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
600 if ( NULL == ( volume->v_macname = strdup_w(mactmpname))) {
601 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
606 if (!( volume->v_path = (char *)malloc( strlen( path ) + 1 )) ) {
607 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
613 volume->v_name = utf8_encoding()?volume->v_u8mname:volume->v_macname;
614 volume->v_hide = hide;
615 strcpy( volume->v_path, path );
619 #endif /* __svr4__ */
620 /* os X start at 1 and use network order ie. 1 2 3 */
621 volume->v_vid = ++lastvid;
622 volume->v_vid = htons(volume->v_vid);
624 if (!check_vol_acl_support(volume)) {
625 LOG(log_debug, logtype_afpd, "creatvol(\"%s\"): disabling ACL support", volume->v_path);
626 options[VOLOPT_FLAGS].i_value &= ~AFPVOL_ACLS;
632 volume->v_casefold = options[VOLOPT_CASEFOLD].i_value;
633 volume->v_flags |= options[VOLOPT_FLAGS].i_value;
635 if (options[VOLOPT_EA_VFS].i_value)
636 volume->v_vfs_ea = options[VOLOPT_EA_VFS].i_value;
638 volume->v_ad_options = 0;
639 if ((volume->v_flags & AFPVOL_NODEV))
640 volume->v_ad_options |= ADVOL_NODEV;
641 if ((volume->v_flags & AFPVOL_UNIX_PRIV))
642 volume->v_ad_options |= ADVOL_UNIXPRIV;
643 if ((volume->v_flags & AFPVOL_INV_DOTS))
644 volume->v_ad_options |= ADVOL_INVDOTS;
646 if (options[VOLOPT_PASSWORD].c_value)
647 volume->v_password = strdup(options[VOLOPT_PASSWORD].c_value);
649 if (options[VOLOPT_VETO].c_value)
650 volume->v_veto = strdup(options[VOLOPT_VETO].c_value);
652 if (options[VOLOPT_ENCODING].c_value)
653 volume->v_volcodepage = strdup(options[VOLOPT_ENCODING].c_value);
655 if (options[VOLOPT_MACCHARSET].c_value)
656 volume->v_maccodepage = strdup(options[VOLOPT_MACCHARSET].c_value);
658 if (options[VOLOPT_DBPATH].c_value)
659 volume->v_dbpath = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_DBPATH].c_value, pwd, path, name);
661 if (options[VOLOPT_CNIDSCHEME].c_value)
662 volume->v_cnidscheme = strdup(options[VOLOPT_CNIDSCHEME].c_value);
664 if (options[VOLOPT_CNIDSERVER].c_value)
665 volume->v_cnidserver = strdup(options[VOLOPT_CNIDSERVER].c_value);
667 if (options[VOLOPT_CNIDPORT].c_value)
668 volume->v_cnidport = strdup(options[VOLOPT_CNIDPORT].c_value);
670 if (options[VOLOPT_UMASK].i_value)
671 volume->v_umask = (mode_t)options[VOLOPT_UMASK].i_value;
673 if (options[VOLOPT_DPERM].i_value)
674 volume->v_dperm = (mode_t)options[VOLOPT_DPERM].i_value;
676 if (options[VOLOPT_FPERM].i_value)
677 volume->v_fperm = (mode_t)options[VOLOPT_FPERM].i_value;
679 if (options[VOLOPT_DFLTPERM].i_value)
680 volume->v_perm = (mode_t)options[VOLOPT_DFLTPERM].i_value;
682 if (options[VOLOPT_ADOUBLE].i_value)
683 volume->v_adouble = options[VOLOPT_ADOUBLE].i_value;
685 volume->v_adouble = AD_VERSION;
687 if (options[VOLOPT_LIMITSIZE].i_value)
688 volume->v_limitsize = options[VOLOPT_LIMITSIZE].i_value;
690 /* Mac to Unix conversion flags*/
691 volume->v_mtou_flags = 0;
692 if (!(volume->v_flags & AFPVOL_NOHEX))
693 volume->v_mtou_flags |= CONV_ESCAPEHEX;
694 if (!(volume->v_flags & AFPVOL_USEDOTS))
695 volume->v_mtou_flags |= CONV_ESCAPEDOTS;
696 if ((volume->v_flags & AFPVOL_EILSEQ))
697 volume->v_mtou_flags |= CONV__EILSEQ;
699 if ((volume->v_casefold & AFPVOL_MTOUUPPER))
700 volume->v_mtou_flags |= CONV_TOUPPER;
701 else if ((volume->v_casefold & AFPVOL_MTOULOWER))
702 volume->v_mtou_flags |= CONV_TOLOWER;
704 /* Unix to Mac conversion flags*/
705 volume->v_utom_flags = CONV_IGNORE | CONV_UNESCAPEHEX;
706 if ((volume->v_casefold & AFPVOL_UTOMUPPER))
707 volume->v_utom_flags |= CONV_TOUPPER;
708 else if ((volume->v_casefold & AFPVOL_UTOMLOWER))
709 volume->v_utom_flags |= CONV_TOLOWER;
711 if ((volume->v_flags & AFPVOL_EILSEQ))
712 volume->v_utom_flags |= CONV__EILSEQ;
714 if (options[VOLOPT_PREEXEC].c_value)
715 volume->v_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_PREEXEC].c_value, pwd, path, name);
716 volume->v_preexec_close = options[VOLOPT_PREEXEC].i_value;
718 if (options[VOLOPT_POSTEXEC].c_value)
719 volume->v_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_POSTEXEC].c_value, pwd, path, name);
721 if (options[VOLOPT_ROOTPREEXEC].c_value)
722 volume->v_root_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPREEXEC].c_value, pwd, path, name);
723 volume->v_root_preexec_close = options[VOLOPT_ROOTPREEXEC].i_value;
725 if (options[VOLOPT_ROOTPOSTEXEC].c_value)
726 volume->v_root_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPOSTEXEC].c_value, pwd, path, name);
728 volume->v_dperm |= volume->v_perm;
729 volume->v_fperm |= volume->v_perm;
731 /* Check EA support on volume */
732 if (volume->v_vfs_ea == AFPVOL_EA_AUTO)
733 check_ea_sys_support(volume);
736 /* get/store uuid from file in afpd master*/
737 if ((parent_or_child == 0) && (volume->v_flags & AFPVOL_TM)) {
738 char *uuid = get_vol_uuid(obj, volume->v_localname);
740 LOG(log_error, logtype_afpd, "Volume '%s': couldn't get UUID",
741 volume->v_localname);
743 volume->v_uuid = uuid;
744 LOG(log_debug, logtype_afpd, "Volume '%s': UUID '%s'",
745 volume->v_localname, volume->v_uuid);
749 volume->v_next = Volumes;
754 /* ---------------- */
755 static char *myfgets( char *buf, int size, FILE *fp)
761 while ((EOF != ( c = getc( fp )) ) && ( size > 1 )) {
762 if ( c == '\n' || c == '\r' ) {
763 if (p != buf && *(p -1) == '\\') {
785 /* check access list. this function wants something of the following
787 * @group,name,name2,@group2,name3
789 * a NULL argument allows everybody to have access.
790 * we return three things:
792 * 0: list exists, but name isn't in it
796 #ifndef NO_REAL_USER_NAME
797 /* authentication is case insensitive
798 * FIXME should we do the same with group name?
800 #define access_strcmp strcasecmp
803 #define access_strcmp strcmp
807 static int accessvol(const char *args, const char *name)
809 char buf[MAXPATHLEN + 1], *p;
815 strlcpy(buf, args, sizeof(buf));
816 if ((p = strtok(buf, ",")) == NULL) /* nothing, return okay */
820 if (*p == '@') { /* it's a group */
821 if ((gr = getgrnam(p + 1)) && gmem(gr->gr_gid))
823 } else if (access_strcmp(p, name) == 0) /* it's a user name */
825 p = strtok(NULL, ",");
831 static int hostaccessvol(int type, const char *volname, const char *args, const AFPObj *obj)
834 char buf[MAXPATHLEN + 1], *p, *b;
836 struct sockaddr_storage client;
841 strlcpy(buf, args, sizeof(buf));
842 if ((p = strtok_r(buf, ",", &b)) == NULL) /* nothing, return okay */
847 char *ipaddr, *mask_char;
848 struct addrinfo hints, *ai;
850 ipaddr = strtok(p, "/");
851 mask_char = strtok(NULL,"/");
853 /* Get address from string with getaddrinfo */
854 memset(&hints, 0, sizeof hints);
855 hints.ai_family = AF_UNSPEC;
856 hints.ai_socktype = SOCK_STREAM;
857 if ((ret = getaddrinfo(ipaddr, NULL, &hints, &ai)) != 0) {
858 LOG(log_error, logtype_afpd, "hostaccessvol: getaddrinfo: %s\n", gai_strerror(ret));
863 if (mask_char != NULL)
864 mask_int = atoi(mask_char); /* apply_ip_mask does range checking on it */
866 if (ai->ai_family == AF_INET) /* IPv4 */
872 /* Apply mask to addresses */
873 client = dsi->client;
874 apply_ip_mask((struct sockaddr *)&client, mask_int);
875 apply_ip_mask(ai->ai_addr, mask_int);
877 if (compare_ip((struct sockaddr *)&client, ai->ai_addr) == 0) {
878 if (type == VOLOPT_DENIED_HOSTS)
879 LOG(log_info, logtype_afpd, "AFP access denied for client IP '%s' to volume '%s' by denied list",
880 getip_string((struct sockaddr *)&client), volname);
887 p = strtok_r(NULL, ",", &b);
890 if (type == VOLOPT_ALLOWED_HOSTS)
891 LOG(log_info, logtype_afpd, "AFP access denied for client IP '%s' to volume '%s', not in allowed list",
892 getip_string((struct sockaddr *)&dsi->client), volname);
896 /* ----------------------
898 static int volfile_changed(struct afp_options *p)
902 if (!stat(p->configfile, &st) && st.st_mtime > p->volfile.mtime) {
903 p->volfile.mtime = st.st_mtime;
909 static int vol_section(const char *sec)
911 if (STRCMP(sec, ==, INISEC_GLOBAL))
913 if (STRCMP(sec, ==, INISEC_AFP))
915 if (STRCMP(sec, ==, INISEC_CNID))
920 /* ----------------------
921 * Read volumes from iniconfig and add the volumes contained within to
922 * the global volume list. This gets called from the forked afpd childs.
923 * The master now reads this too for Zeroconf announcements.
925 static int readvolfile(AFPObj *obj, struct afp_volume_name *p1, struct passwd *pwent)
927 char path[MAXPATHLEN + 1];
928 char volname[AFPVOL_U8MNAMELEN + 1];
929 char tmp[MAXPATHLEN + 1];
935 struct vol_option save_options[VOLOPT_NUM];
936 struct vol_option default_options[VOLOPT_NUM];
937 struct vol_option options[VOLOPT_NUM];
939 LOG(log_debug, logtype_afpd, "readvolfile: BEGIN");
943 memset(default_options, 0, sizeof(default_options));
945 /* Enable some default options for all volumes */
947 default_options[VOLOPT_FLAGS].i_value |= AFPVOL_ACLS;
949 default_options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_AUTO;
950 default_options[VOLOPT_UMASK].i_value = obj->options.umask;
951 memcpy(save_options, default_options, sizeof(options));
953 int secnum = iniparser_getnsec(obj->iniconfig);
954 LOG(log_debug, logtype_afpd, "readvolfile: sections: %d", secnum);
957 for (i = 0; i < secnum; i++) {
958 secname = iniparser_getsecname(obj->iniconfig, i);
959 if (!vol_section(secname))
962 strlcpy(volname, secname, AFPVOL_U8MNAMELEN);
963 LOG(log_debug, logtype_afpd, "readvolfile: volume: %s", volname);
965 if ((p = iniparser_getstrdup(obj->iniconfig, secname, "path", NULL)) == NULL)
967 strlcpy(path, p, MAXPATHLEN);
971 if (!pwent && obj->username)
972 pwent = getpwnam(obj->username);
974 if (volxlate(obj, path, sizeof(path) - 1, tmp, pwent, NULL, NULL) == NULL)
977 memcpy(options, default_options, sizeof(options));
978 volset(obj->iniconfig, secname, options, default_options);
980 /* check allow/deny lists (if not afpd master loading volumes for Zeroconf reg.):
981 allow -> either no list (-1), or in list (1)
982 deny -> either no list (-1), or not in list (0) */
983 if (parent_or_child == 0
985 (accessvol(options[VOLOPT_ALLOW].c_value, obj->username) &&
986 (accessvol(options[VOLOPT_DENY].c_value, obj->username) < 1) &&
987 hostaccessvol(VOLOPT_ALLOWED_HOSTS, volname, options[VOLOPT_ALLOWED_HOSTS].c_value, obj) &&
988 (hostaccessvol(VOLOPT_DENIED_HOSTS, volname, options[VOLOPT_DENIED_HOSTS].c_value, obj) < 1))) {
990 /* handle read-only behaviour. semantics:
991 * 1) neither the rolist nor the rwlist exist -> rw
992 * 2) rolist exists -> ro if user is in it.
993 * 3) rwlist exists -> ro unless user is in it. */
994 if (parent_or_child == 1
996 ((options[VOLOPT_FLAGS].i_value & AFPVOL_RO) == 0)
998 ((accessvol(options[VOLOPT_ROLIST].c_value, obj->username) == 1) ||
999 !accessvol(options[VOLOPT_RWLIST].c_value, obj->username)))
1000 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
1002 /* do variable substitution for volname */
1003 if (volxlate(obj, tmp, sizeof(tmp) - 1, volname, pwent, path, NULL) == NULL) {
1004 volfree(options, default_options);
1008 creatvol(obj, pwent, path, tmp, options);
1010 volfree(options, default_options);
1012 volfree(save_options, NULL);
1017 /* ------------------------------- */
1018 static void volume_free(struct vol *vol)
1020 free(vol->v_localname);
1021 vol->v_localname = NULL;
1022 free(vol->v_u8mname);
1023 vol->v_u8mname = NULL;
1024 free(vol->v_macname);
1025 vol->v_macname = NULL;
1027 free(vol->v_password);
1029 free(vol->v_volcodepage);
1030 free(vol->v_maccodepage);
1031 free(vol->v_cnidscheme);
1032 free(vol->v_dbpath);
1038 /* ------------------------------- */
1039 static void free_volumes(void )
1042 struct vol *nvol, *ovol;
1044 for ( vol = Volumes; vol; vol = vol->v_next ) {
1045 if (( vol->v_flags & AFPVOL_OPEN ) ) {
1052 for ( vol = Volumes, ovol = NULL; vol; vol = nvol) {
1055 if (vol->v_localname == NULL) {
1056 if (Volumes == vol) {
1061 ovol->v_next = nvol;
1071 /* ------------------------------- */
1072 static void volume_unlink(struct vol *volume)
1074 struct vol *vol, *ovol, *nvol;
1076 if (volume == Volumes) {
1077 Volumes = Volumes->v_next;
1080 for ( vol = Volumes->v_next, ovol = Volumes; vol; vol = nvol) {
1083 if (vol == volume) {
1084 ovol->v_next = nvol;
1093 * Read band-size info from Info.plist XML file of an TM sparsebundle
1095 * @param path (r) path to Info.plist file
1096 * @return band-size in bytes, -1 on error
1098 static long long int get_tm_bandsize(const char *path)
1103 long long int bandsize = -1;
1105 EC_NULL_LOGSTR( file = fopen(path, "r"),
1106 "get_tm_bandsize(\"%s\"): %s",
1107 path, strerror(errno) );
1109 while (fgets(buf, sizeof(buf), file) != NULL) {
1110 if (strstr(buf, "band-size") == NULL)
1113 if (fscanf(file, " <integer>%lld</integer>", &bandsize) != 1) {
1114 LOG(log_error, logtype_afpd, "get_tm_bandsize(\"%s\"): can't parse band-size", path);
1123 LOG(log_debug, logtype_afpd, "get_tm_bandsize(\"%s\"): bandsize: %lld", path, bandsize);
1128 * Return number on entries in a directory
1130 * @param path (r) path to dir
1131 * @return number of entries, -1 on error
1133 static long long int get_tm_bands(const char *path)
1136 long long int count = 0;
1138 const struct dirent *entry;
1140 EC_NULL( dir = opendir(path) );
1142 while ((entry = readdir(dir)) != NULL)
1144 count -= 2; /* All OSens I'm aware of return "." and "..", so just substract them, avoiding string comparison in loop */
1155 * Calculate used size of a TimeMachine volume
1157 * This assumes that the volume is used only for TimeMachine.
1159 * 1) readdir(path of volume)
1160 * 2) for every element that matches regex "\(.*\)\.sparsebundle$" :
1161 * 3) parse "\1.sparsebundle/Info.plist" and read the band-size XML key integer value
1162 * 4) readdir "\1.sparsebundle/bands/" counting files
1163 * 5) calculate used size as: (file_count - 1) * band-size
1165 * The result of the calculation is returned in "volume->v_tm_used".
1166 * "volume->v_appended" gets reset to 0.
1167 * "volume->v_tm_cachetime" is updated with the current time from time(NULL).
1169 * "volume->v_tm_used" is cached for TM_USED_CACHETIME seconds and updated by
1170 * "volume->v_appended". The latter is increased by X every time the client
1171 * appends X bytes to a file (in fork.c).
1173 * @param vol (rw) volume to calculate
1174 * @return 0 on success, -1 on error
1176 #define TM_USED_CACHETIME 60 /* cache for 60 seconds */
1177 static int get_tm_used(struct vol * restrict vol)
1180 long long int bandsize;
1182 bstring infoplist = NULL;
1183 bstring bandsdir = NULL;
1185 const struct dirent *entry;
1189 time_t now = time(NULL);
1191 if (vol->v_tm_cachetime
1192 && ((vol->v_tm_cachetime + TM_USED_CACHETIME) >= now)) {
1193 if (vol->v_tm_used == -1)
1195 vol->v_tm_used += vol->v_appended;
1196 vol->v_appended = 0;
1197 LOG(log_debug, logtype_afpd, "getused(\"%s\"): cached: %" PRIu64 " bytes",
1198 vol->v_path, vol->v_tm_used);
1202 vol->v_tm_cachetime = now;
1204 EC_NULL( dir = opendir(vol->v_path) );
1206 while ((entry = readdir(dir)) != NULL) {
1207 if (((p = strstr(entry->d_name, "sparsebundle")) != NULL)
1208 && (strlen(entry->d_name) == (p + strlen("sparsebundle") - entry->d_name))) {
1210 EC_NULL_LOG( infoplist = bformat("%s/%s/%s", vol->v_path, entry->d_name, "Info.plist") );
1212 if ((bandsize = get_tm_bandsize(cfrombstr(infoplist))) == -1)
1215 EC_NULL_LOG( bandsdir = bformat("%s/%s/%s/", vol->v_path, entry->d_name, "bands") );
1217 if ((links = get_tm_bands(cfrombstr(bandsdir))) == -1)
1220 used += (links - 1) * bandsize;
1221 LOG(log_debug, logtype_afpd, "getused(\"%s\"): bands: %" PRIu64 " bytes",
1222 cfrombstr(bandsdir), used);
1226 vol->v_tm_used = used;
1230 bdestroy(infoplist);
1236 LOG(log_debug, logtype_afpd, "getused(\"%s\"): %" PRIu64 " bytes", vol->v_path, vol->v_tm_used);
1241 static int getvolspace(struct vol *vol,
1242 uint32_t *bfree, uint32_t *btotal,
1243 VolSpace *xbfree, VolSpace *xbtotal, uint32_t *bsize)
1248 #ifndef NO_QUOTA_SUPPORT
1249 VolSpace qfree, qtotal;
1252 spaceflag = AFPVOL_GVSMASK & vol->v_flags;
1253 /* report up to 2GB if afp version is < 2.2 (4GB if not) */
1254 maxsize = (afp_version < 22) ? 0x7fffffffL : 0xffffffffL;
1257 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_AFSGVS ) {
1258 if ( afs_getvolspace( vol, xbfree, xbtotal, bsize ) == AFP_OK ) {
1259 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_AFSGVS;
1260 goto getvolspace_done;
1265 if (( rc = ustatfs_getvolspace( vol, xbfree, xbtotal, bsize)) != AFP_OK ) {
1269 #ifndef NO_QUOTA_SUPPORT
1270 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_UQUOTA ) {
1271 if ( uquota_getvolspace( vol, &qfree, &qtotal, *bsize ) == AFP_OK ) {
1272 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_UQUOTA;
1273 *xbfree = MIN(*xbfree, qfree);
1274 *xbtotal = MIN(*xbtotal, qtotal);
1275 goto getvolspace_done;
1279 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_USTATFS;
1282 if (vol->v_limitsize) {
1283 if (get_tm_used(vol) != 0)
1286 *xbtotal = MIN(*xbtotal, (vol->v_limitsize * 1024 * 1024));
1287 *xbfree = MIN(*xbfree, *xbtotal < vol->v_tm_used ? 0 : *xbtotal - vol->v_tm_used);
1289 LOG(log_debug, logtype_afpd,
1290 "volparams: total: %" PRIu64 ", used: %" PRIu64 ", free: %" PRIu64 " bytes",
1291 *xbtotal, vol->v_tm_used, *xbfree);
1294 *bfree = MIN(*xbfree, maxsize);
1295 *btotal = MIN(*xbtotal, maxsize);
1299 #define FCE_TM_DELTA 10 /* send notification every 10 seconds */
1300 void vol_fce_tm_event(void)
1303 time_t now = time(NULL);
1304 struct vol *vol = Volumes;
1306 if ((last + FCE_TM_DELTA) < now) {
1308 for ( ; vol; vol = vol->v_next ) {
1309 if (vol->v_flags & AFPVOL_TM)
1310 (void)fce_register_tm_size(vol->v_path, vol->v_tm_used + vol->v_appended);
1315 /* -----------------------
1316 * set volume creation date
1317 * avoid duplicate, well at least it tries
1319 static void vol_setdate(uint16_t id, struct adouble *adp, time_t date)
1322 struct vol *vol = Volumes;
1324 for ( volume = Volumes; volume; volume = volume->v_next ) {
1325 if (volume->v_vid == id) {
1328 else if ((time_t)(AD_DATE_FROM_UNIX(date)) == volume->v_ctime) {
1330 volume = Volumes; /* restart */
1333 vol->v_ctime = AD_DATE_FROM_UNIX(date);
1334 ad_setdate(adp, AD_DATE_CREATE | AD_DATE_UNIX, date);
1337 /* ----------------------- */
1338 static int getvolparams( uint16_t bitmap, struct vol *vol, struct stat *st, char *buf, size_t *buflen)
1341 int bit = 0, isad = 1;
1344 uint32_t bfree, btotal, bsize;
1345 VolSpace xbfree, xbtotal; /* extended bytes */
1346 char *data, *nameoff = NULL;
1349 LOG(log_debug, logtype_afpd, "getvolparams: Volume '%s'", vol->v_localname);
1351 /* courtesy of jallison@whistle.com:
1352 * For MacOS8.x support we need to create the
1353 * .Parent file here if it doesn't exist. */
1355 /* Convert adouble:v2 to adouble:ea on the fly */
1356 (void)ad_convert(vol->v_path, st, vol);
1359 if (ad_open(&ad, vol->v_path, ADFLAGS_HF | ADFLAGS_DIR | ADFLAGS_RDWR | ADFLAGS_CREATE, 0666) != 0 ) {
1361 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1363 } else if (ad_get_MD_flags( &ad ) & O_CREAT) {
1364 slash = strrchr( vol->v_path, '/' );
1368 slash = vol->v_path;
1369 if (ad_getentryoff(&ad, ADEID_NAME)) {
1370 ad_setentrylen( &ad, ADEID_NAME, strlen( slash ));
1371 memcpy(ad_entry( &ad, ADEID_NAME ), slash,
1372 ad_getentrylen( &ad, ADEID_NAME ));
1374 vol_setdate(vol->v_vid, &ad, st->st_mtime);
1378 if (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0)
1379 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1381 vol->v_ctime = aint;
1384 if (( bitmap & ( (1<<VOLPBIT_BFREE)|(1<<VOLPBIT_BTOTAL) |
1385 (1<<VOLPBIT_XBFREE)|(1<<VOLPBIT_XBTOTAL) |
1386 (1<<VOLPBIT_BSIZE)) ) != 0 ) {
1387 if ( getvolspace( vol, &bfree, &btotal, &xbfree, &xbtotal,
1388 &bsize) != AFP_OK ) {
1390 ad_close( &ad, ADFLAGS_HF );
1392 return( AFPERR_PARAM );
1397 while ( bitmap != 0 ) {
1398 while (( bitmap & 1 ) == 0 ) {
1406 /* check for read-only.
1407 * NOTE: we don't actually set the read-only flag unless
1408 * it's passed in that way as it's possible to mount
1409 * a read-write filesystem under a read-only one. */
1410 if ((vol->v_flags & AFPVOL_RO) ||
1411 ((utime(vol->v_path, NULL) < 0) && (errno == EROFS))) {
1412 ashort |= VOLPBIT_ATTR_RO;
1414 /* prior 2.1 only VOLPBIT_ATTR_RO is defined */
1415 if (afp_version > 20) {
1416 if (vol->v_cdb != NULL && (vol->v_cdb->flags & CNID_FLAG_PERSISTENT))
1417 ashort |= VOLPBIT_ATTR_FILEID;
1418 ashort |= VOLPBIT_ATTR_CATSEARCH;
1420 if (afp_version >= 30) {
1421 ashort |= VOLPBIT_ATTR_UTF8;
1422 if (vol->v_flags & AFPVOL_UNIX_PRIV)
1423 ashort |= VOLPBIT_ATTR_UNIXPRIV;
1424 if (vol->v_flags & AFPVOL_TM)
1425 ashort |= VOLPBIT_ATTR_TM;
1426 if (vol->v_flags & AFPVOL_NONETIDS)
1427 ashort |= VOLPBIT_ATTR_NONETIDS;
1428 if (afp_version >= 32) {
1430 ashort |= VOLPBIT_ATTR_EXT_ATTRS;
1431 if (vol->v_flags & AFPVOL_ACLS)
1432 ashort |= VOLPBIT_ATTR_ACLS;
1436 ashort = htons(ashort);
1437 memcpy(data, &ashort, sizeof( ashort ));
1438 data += sizeof( ashort );
1442 ashort = htons( AFPVOLSIG_DEFAULT );
1443 memcpy(data, &ashort, sizeof( ashort ));
1444 data += sizeof( ashort );
1447 case VOLPBIT_CDATE :
1448 aint = vol->v_ctime;
1449 memcpy(data, &aint, sizeof( aint ));
1450 data += sizeof( aint );
1453 case VOLPBIT_MDATE :
1454 if ( st->st_mtime > vol->v_mtime ) {
1455 vol->v_mtime = st->st_mtime;
1457 aint = AD_DATE_FROM_UNIX(vol->v_mtime);
1458 memcpy(data, &aint, sizeof( aint ));
1459 data += sizeof( aint );
1462 case VOLPBIT_BDATE :
1463 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1464 aint = AD_DATE_START;
1465 memcpy(data, &aint, sizeof( aint ));
1466 data += sizeof( aint );
1470 memcpy(data, &vol->v_vid, sizeof( vol->v_vid ));
1471 data += sizeof( vol->v_vid );
1474 case VOLPBIT_BFREE :
1475 bfree = htonl( bfree );
1476 memcpy(data, &bfree, sizeof( bfree ));
1477 data += sizeof( bfree );
1480 case VOLPBIT_BTOTAL :
1481 btotal = htonl( btotal );
1482 memcpy(data, &btotal, sizeof( btotal ));
1483 data += sizeof( btotal );
1486 #ifndef NO_LARGE_VOL_SUPPORT
1487 case VOLPBIT_XBFREE :
1488 xbfree = hton64( xbfree );
1489 memcpy(data, &xbfree, sizeof( xbfree ));
1490 data += sizeof( xbfree );
1493 case VOLPBIT_XBTOTAL :
1494 xbtotal = hton64( xbtotal );
1495 memcpy(data, &xbtotal, sizeof( xbtotal ));
1496 data += sizeof( xbfree );
1498 #endif /* ! NO_LARGE_VOL_SUPPORT */
1502 data += sizeof( uint16_t );
1505 case VOLPBIT_BSIZE: /* block size */
1506 bsize = htonl(bsize);
1507 memcpy(data, &bsize, sizeof(bsize));
1508 data += sizeof(bsize);
1513 ad_close( &ad, ADFLAGS_HF );
1515 return( AFPERR_BITMAP );
1521 ashort = htons( data - buf );
1522 memcpy(nameoff, &ashort, sizeof( ashort ));
1523 /* name is always in mac charset */
1524 aint = ucs2_to_charset( vol->v_maccharset, vol->v_macname, data+1, AFPVOL_MACNAMELEN + 1);
1534 ad_close(&ad, ADFLAGS_HF);
1536 *buflen = data - buf;
1540 /* ------------------------- */
1541 static int stat_vol(uint16_t bitmap, struct vol *vol, char *rbuf, size_t *rbuflen)
1547 if ( stat( vol->v_path, &st ) < 0 ) {
1549 return( AFPERR_PARAM );
1551 /* save the volume device number */
1552 vol->v_dev = st.st_dev;
1554 buflen = *rbuflen - sizeof( bitmap );
1555 if (( ret = getvolparams( bitmap, vol, &st,
1556 rbuf + sizeof( bitmap ), &buflen )) != AFP_OK ) {
1560 *rbuflen = buflen + sizeof( bitmap );
1561 bitmap = htons( bitmap );
1562 memcpy(rbuf, &bitmap, sizeof( bitmap ));
1567 /* ------------------------------- */
1568 void load_volumes(AFPObj *obj)
1572 struct passwd *pwent;
1576 if (parent_or_child == 0) {
1577 LOG(log_debug, logtype_afpd, "load_volumes: AFP MASTER");
1579 LOG(log_debug, logtype_afpd, "load_volumes: user: %s", obj->username);
1583 if (!volfile_changed(&obj->options))
1588 /* try putting a read lock on the volume file twice, sleep 1 second if first attempt fails */
1590 fd = open(obj->options.configfile, O_RDWR);
1592 while (retries < 2) {
1593 if ((read_lock(fd, 0, SEEK_SET, 0)) != 0) {
1596 LOG(log_error, logtype_afpd, "readvolfile: can't lock configfile \"%s\"",
1597 obj->options.configfile);
1607 iniparser_freedict(obj->iniconfig);
1608 LOG(log_debug, logtype_afpd, "load_volumes: reloading: %s", obj->options.configfile);
1609 obj->iniconfig = iniparser_load(obj->options.configfile);
1612 pwent = getpwnam(obj->username);
1616 readvolfile(obj, &obj->options.volfile, pwent);
1618 if ( obj->options.flags & OPTION_CLOSEVOL) {
1620 for ( vol = Volumes; vol; vol = vol->v_next ) {
1621 if (vol->v_deleted && !vol->v_new ) {
1635 /* ------------------------------- */
1636 int afp_getsrvrparms(AFPObj *obj, char *ibuf _U_, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
1649 for ( vcnt = 0, volume = Volumes; volume; volume = volume->v_next ) {
1650 if (!(volume->v_flags & AFPVOL_NOSTAT)) {
1653 if ( stat( volume->v_path, &st ) < 0 ) {
1654 LOG(log_info, logtype_afpd, "afp_getsrvrparms(%s): stat: %s",
1655 volume->v_path, strerror(errno) );
1656 continue; /* can't access directory */
1658 if (!S_ISDIR(st.st_mode)) {
1659 continue; /* not a dir */
1661 accessmode(volume, volume->v_path, &ma, NULL, &st);
1662 if ((ma.ma_user & (AR_UREAD | AR_USEARCH)) != (AR_UREAD | AR_USEARCH)) {
1663 continue; /* no r-x access */
1666 if (volume->v_hide) {
1667 continue; /* config file changed but the volume was mounted */
1670 if (utf8_encoding()) {
1671 len = ucs2_to_charset_allocate(CH_UTF8_MAC, &namebuf, volume->v_u8mname);
1673 len = ucs2_to_charset_allocate(obj->options.maccharset, &namebuf, volume->v_macname);
1676 if (len == (size_t)-1)
1679 /* set password bit if there's a volume password */
1680 *data = (volume->v_password) ? AFPSRVR_PASSWD : 0;
1682 *data++ |= 0; /* UNIX PRIVS BIT ..., OSX doesn't seem to use it, so we don't either */
1684 memcpy(data, namebuf, len );
1690 *rbuflen = data - rbuf;
1692 if ( gettimeofday( &tv, NULL ) < 0 ) {
1693 LOG(log_error, logtype_afpd, "afp_getsrvrparms(%s): gettimeofday: %s", volume->v_path, strerror(errno) );
1695 return AFPERR_PARAM;
1697 tv.tv_sec = AD_DATE_FROM_UNIX(tv.tv_sec);
1698 memcpy(data, &tv.tv_sec, sizeof( uint32_t));
1699 data += sizeof( uint32_t);
1704 /* ------------------------- */
1705 static int volume_codepage(AFPObj *obj, struct vol *volume)
1707 struct charset_functions *charset;
1710 if (!volume->v_volcodepage)
1711 volume->v_volcodepage = strdup("UTF8");
1713 if ( (charset_t) -1 == ( volume->v_volcharset = add_charset(volume->v_volcodepage)) ) {
1714 LOG (log_error, logtype_afpd, "Setting codepage %s as volume codepage failed", volume->v_volcodepage);
1718 if ( NULL == (charset = find_charset_functions(volume->v_volcodepage)) || charset->flags & CHARSET_ICONV ) {
1719 LOG (log_warning, logtype_afpd, "WARNING: volume encoding %s is *not* supported by netatalk, expect problems !!!!", volume->v_volcodepage);
1722 if (!volume->v_maccodepage)
1723 volume->v_maccodepage = strdup(obj->options.maccodepage);
1725 if ( (charset_t) -1 == ( volume->v_maccharset = add_charset(volume->v_maccodepage)) ) {
1726 LOG (log_error, logtype_afpd, "Setting codepage %s as mac codepage failed", volume->v_maccodepage);
1730 if ( NULL == ( charset = find_charset_functions(volume->v_maccodepage)) || ! (charset->flags & CHARSET_CLIENT) ) {
1731 LOG (log_error, logtype_afpd, "Fatal error: mac charset %s not supported", volume->v_maccodepage);
1734 volume->v_kTextEncoding = htonl(charset->kTextEncoding);
1738 /* ------------------------- */
1739 static int volume_openDB(const AFPObj *obj, struct vol *volume)
1743 if ((volume->v_flags & AFPVOL_NODEV)) {
1744 flags |= CNID_FLAG_NODEV;
1747 if (volume->v_cnidscheme == NULL) {
1748 volume->v_cnidscheme = strdup(DEFAULT_CNID_SCHEME);
1749 LOG(log_info, logtype_afpd, "Volume %s use CNID scheme %s.",
1750 volume->v_path, volume->v_cnidscheme);
1753 LOG(log_info, logtype_afpd, "CNID server: %s:%s",
1754 volume->v_cnidserver ? volume->v_cnidserver : obj->options.Cnid_srv,
1755 volume->v_cnidport ? volume->v_cnidport : obj->options.Cnid_port);
1757 volume->v_cdb = cnid_open(volume->v_path,
1759 volume->v_cnidscheme,
1761 volume->v_cnidserver ? volume->v_cnidserver : obj->options.Cnid_srv,
1762 volume->v_cnidport ? volume->v_cnidport : obj->options.Cnid_port);
1764 if ( ! volume->v_cdb && ! (flags & CNID_FLAG_MEMORY)) {
1765 /* The first attempt failed and it wasn't yet an attempt to open in-memory */
1766 LOG(log_error, logtype_afpd, "Can't open volume \"%s\" CNID backend \"%s\" ",
1767 volume->v_path, volume->v_cnidscheme);
1768 LOG(log_error, logtype_afpd, "Reopen volume %s using in memory temporary CNID DB.",
1770 flags |= CNID_FLAG_MEMORY;
1771 volume->v_cdb = cnid_open (volume->v_path, volume->v_umask, "tdb", flags, NULL, NULL);
1773 /* kill ourself with SIGUSR2 aka msg pending */
1774 if (volume->v_cdb) {
1775 setmessage("Something wrong with the volume's CNID DB, using temporary CNID DB instead."
1776 "Check server messages for details!");
1777 kill(getpid(), SIGUSR2);
1778 /* deactivate cnid caching/storing in AppleDouble files */
1783 return (!volume->v_cdb)?-1:0;
1787 Check if the underlying filesystem supports EAs for ea:sys volumes.
1788 If not, switch to ea:ad.
1789 As we can't check (requires write access) on ro-volumes, we switch ea:auto
1790 volumes that are options:ro to ea:none.
1792 static void check_ea_sys_support(struct vol *vol)
1794 uid_t process_uid = 0;
1795 char eaname[] = {"org.netatalk.supports-eas.XXXXXX"};
1796 const char *eacontent = "yes";
1798 if (vol->v_vfs_ea == AFPVOL_EA_AUTO) {
1800 if ((vol->v_flags & AFPVOL_RO) == AFPVOL_RO) {
1801 LOG(log_info, logtype_afpd, "read-only volume '%s', can't test for EA support, disabling EAs", vol->v_localname);
1802 vol->v_vfs_ea = AFPVOL_EA_NONE;
1808 process_uid = geteuid();
1810 if (seteuid(0) == -1) {
1811 LOG(log_error, logtype_afpd, "check_ea_sys_support: can't seteuid(0): %s", strerror(errno));
1815 if ((sys_setxattr(vol->v_path, eaname, eacontent, 4, 0)) == 0) {
1816 sys_removexattr(vol->v_path, eaname);
1817 vol->v_vfs_ea = AFPVOL_EA_SYS;
1819 LOG(log_warning, logtype_afpd, "volume \"%s\" does not support Extended Attributes, using ea:ad instead",
1821 vol->v_vfs_ea = AFPVOL_EA_AD;
1825 if (seteuid(process_uid) == -1) {
1826 LOG(log_error, logtype_afpd, "can't seteuid back %s", strerror(errno));
1833 /* -------------------------
1834 * we are the user here
1836 int afp_openvol(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
1847 char path[ MAXPATHLEN + 1];
1853 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1854 bitmap = ntohs( bitmap );
1855 ibuf += sizeof( bitmap );
1858 if (( bitmap & (1<<VOLPBIT_VID)) == 0 ) {
1859 return AFPERR_BITMAP;
1862 len = (unsigned char)*ibuf++;
1863 volname = obj->oldtmp;
1865 if ((volname_tmp = strchr(volname,'+')) != NULL)
1866 volname = volname_tmp+1;
1868 if (utf8_encoding()) {
1869 namelen = convert_string(CH_UTF8_MAC, CH_UCS2, ibuf, len, volname, sizeof(obj->oldtmp));
1871 namelen = convert_string(obj->options.maccharset, CH_UCS2, ibuf, len, volname, sizeof(obj->oldtmp));
1874 if ( namelen <= 0) {
1875 return AFPERR_PARAM;
1879 if ((len + 1) & 1) /* pad to an even boundary */
1884 for ( volume = Volumes; volume; volume = volume->v_next ) {
1885 if ( strcasecmp_w( (ucs2_t*) volname, volume->v_name ) == 0 ) {
1890 if ( volume == NULL ) {
1891 return AFPERR_PARAM;
1894 /* check for a volume password */
1895 if (volume->v_password && strncmp(ibuf, volume->v_password, VOLPASSLEN)) {
1896 return AFPERR_ACCESS;
1899 if (( volume->v_flags & AFPVOL_OPEN ) ) {
1900 /* the volume is already open */
1901 return stat_vol(bitmap, volume, rbuf, rbuflen);
1904 if (volume->v_root_preexec) {
1905 if ((ret = afprun(1, volume->v_root_preexec, NULL)) && volume->v_root_preexec_close) {
1906 LOG(log_error, logtype_afpd, "afp_openvol(%s): root preexec : %d", volume->v_path, ret );
1911 if (volume->v_preexec) {
1912 if ((ret = afprun(0, volume->v_preexec, NULL)) && volume->v_preexec_close) {
1913 LOG(log_error, logtype_afpd, "afp_openvol(%s): preexec : %d", volume->v_path, ret );
1918 if ( stat( volume->v_path, &st ) < 0 ) {
1919 return AFPERR_PARAM;
1922 if ( chdir( volume->v_path ) < 0 ) {
1923 return AFPERR_PARAM;
1926 if ( NULL == getcwd(path, MAXPATHLEN)) {
1927 /* shouldn't be fatal but it will fail later */
1928 LOG(log_error, logtype_afpd, "afp_openvol(%s): volume pathlen too long", volume->v_path);
1932 /* Normalize volume path */
1933 #ifdef REALPATH_TAKES_NULL
1934 if ((volume->v_path = realpath(path, NULL)) == NULL)
1937 if ((volume->v_path = malloc(MAXPATHLEN+1)) == NULL)
1939 if (realpath(path, volume->v_path) == NULL) {
1940 free(volume->v_path);
1943 /* Safe some memory */
1945 if ((tmp = strdup(volume->v_path)) == NULL) {
1946 free(volume->v_path);
1949 free(volume->v_path);
1950 volume->v_path = tmp;
1953 if (volume_codepage(obj, volume) < 0) {
1958 /* initialize volume variables
1961 if (utf8_encoding()) {
1962 volume->max_filename = UTF8FILELEN_EARLY;
1965 volume->max_filename = MACFILELEN;
1968 volume->v_flags |= AFPVOL_OPEN;
1969 volume->v_cdb = NULL;
1971 if (utf8_encoding()) {
1972 len = convert_string_allocate(CH_UCS2, CH_UTF8_MAC, volume->v_u8mname, namelen, &vol_mname);
1974 len = convert_string_allocate(CH_UCS2, obj->options.maccharset, volume->v_macname, namelen, &vol_mname);
1976 if ( !vol_mname || len <= 0) {
1981 if ((vol_uname = strrchr(path, '/')) == NULL)
1983 else if (*(vol_uname + 1) != '\0')
1986 if ((dir = dir_new(vol_mname,
1991 bfromcstr(volume->v_path),
1995 LOG(log_error, logtype_afpd, "afp_openvol(%s): malloc: %s", volume->v_path, strerror(errno) );
2000 volume->v_root = dir;
2003 if (volume_openDB(obj, volume) < 0) {
2004 LOG(log_error, logtype_afpd, "Fatal error: cannot open CNID or invalid CNID backend for %s: %s",
2005 volume->v_path, volume->v_cnidscheme);
2010 ret = stat_vol(bitmap, volume, rbuf, rbuflen);
2012 if (ret == AFP_OK) {
2013 handle_special_folders(volume);
2015 volume->v_cnidserver ? volume->v_cnidserver : obj->options.Cnid_srv,
2016 volume->v_cnidport ? volume->v_cnidport : obj->options.Cnid_port);
2020 * If you mount a volume twice, the second time the trash appears on
2021 * the desk-top. That's because the Mac remembers the DID for the
2022 * trash (even for volumes in different zones, on different servers).
2023 * Just so this works better, we prime the DID cache with the trash,
2024 * fixing the trash at DID 17.
2025 * FIXME (RL): should it be done inside a CNID backend ? (always returning Trash DID when asked) ?
2027 if ((volume->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
2029 /* FIXME find db time stamp */
2030 if (cnid_getstamp(volume->v_cdb, volume->v_stamp, sizeof(volume->v_stamp)) < 0) {
2031 LOG (log_error, logtype_afpd,
2032 "afp_openvol(%s): Fatal error: Unable to get stamp value from CNID backend",
2040 cname( volume, volume->v_root, &p );
2046 if (volume->v_root) {
2047 dir_free( volume->v_root );
2048 volume->v_root = NULL;
2051 volume->v_flags &= ~AFPVOL_OPEN;
2052 if (volume->v_cdb != NULL) {
2053 cnid_close(volume->v_cdb);
2054 volume->v_cdb = NULL;
2060 /* ------------------------- */
2061 static void closevol(struct vol *vol)
2066 dir_free( vol->v_root );
2068 if (vol->v_cdb != NULL) {
2069 cnid_close(vol->v_cdb);
2073 if (vol->v_postexec) {
2074 afprun(0, vol->v_postexec, NULL);
2076 if (vol->v_root_postexec) {
2077 afprun(1, vol->v_root_postexec, NULL);
2081 /* ------------------------- */
2082 void close_all_vol(void)
2086 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
2087 if ( (ovol->v_flags & AFPVOL_OPEN) ) {
2088 ovol->v_flags &= ~AFPVOL_OPEN;
2094 /* ------------------------- */
2095 static void deletevol(struct vol *vol)
2099 vol->v_flags &= ~AFPVOL_OPEN;
2100 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
2101 if ( (ovol->v_flags & AFPVOL_OPEN) ) {
2105 if ( ovol != NULL ) {
2106 /* Even if chdir fails, we can't say afp_closevol fails. */
2107 if ( chdir( ovol->v_path ) == 0 ) {
2108 curdir = ovol->v_root;
2113 if (vol->v_deleted) {
2114 showvol(vol->v_name);
2121 /* ------------------------- */
2122 int afp_closevol(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2129 memcpy(&vid, ibuf, sizeof( vid ));
2130 if (NULL == ( vol = getvolbyvid( vid )) ) {
2131 return( AFPERR_PARAM );
2140 /* ------------------------- */
2141 struct vol *getvolbyvid(const uint16_t vid )
2145 for ( vol = Volumes; vol; vol = vol->v_next ) {
2146 if ( vid == vol->v_vid ) {
2150 if ( vol == NULL || ( vol->v_flags & AFPVOL_OPEN ) == 0 ) {
2159 /* --------------------------
2160 poll if a volume is changed by other processes.
2162 0 no attention msg sent
2163 1 attention msg sent
2164 -1 error (socket closed)
2166 Note: if attention return -1 no packet has been
2167 sent because the buffer is full, we don't care
2168 either there's no reader or there's a lot of
2169 traffic and another pollvoltime will follow
2171 int pollvoltime(AFPObj *obj)
2178 if (!(afp_version > 21 && obj->options.flags & OPTION_SERVERNOTIF))
2181 if ( gettimeofday( &tv, NULL ) < 0 )
2184 for ( vol = Volumes; vol; vol = vol->v_next ) {
2185 if ( (vol->v_flags & AFPVOL_OPEN) && vol->v_mtime + 30 < tv.tv_sec) {
2186 if ( !stat( vol->v_path, &st ) && vol->v_mtime != st.st_mtime ) {
2187 vol->v_mtime = st.st_mtime;
2188 if (!obj->attention(obj->dsi, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED))
2197 /* ------------------------- */
2198 void setvoltime(AFPObj *obj, struct vol *vol)
2202 if ( gettimeofday( &tv, NULL ) < 0 ) {
2203 LOG(log_error, logtype_afpd, "setvoltime(%s): gettimeofday: %s", vol->v_path, strerror(errno) );
2206 if( utime( vol->v_path, NULL ) < 0 ) {
2207 /* write of time failed ... probably a read only filesys,
2208 * where no other users can interfere, so there's no issue here
2212 /* a little granularity */
2213 if (vol->v_mtime < tv.tv_sec) {
2214 vol->v_mtime = tv.tv_sec;
2215 /* or finder doesn't update free space
2216 * AFP 3.2 and above clients seem to be ok without so many notification
2218 if (afp_version < 32 && obj->options.flags & OPTION_SERVERNOTIF) {
2219 obj->attention(obj->dsi, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED);
2224 /* ------------------------- */
2225 int afp_getvolparams(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_,char *rbuf, size_t *rbuflen)
2228 uint16_t vid, bitmap;
2231 memcpy(&vid, ibuf, sizeof( vid ));
2232 ibuf += sizeof( vid );
2233 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2234 bitmap = ntohs( bitmap );
2236 if (NULL == ( vol = getvolbyvid( vid )) ) {
2238 return( AFPERR_PARAM );
2241 return stat_vol(bitmap, vol, rbuf, rbuflen);
2244 /* ------------------------- */
2245 int afp_setvolparams(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2249 uint16_t vid, bitmap;
2255 memcpy(&vid, ibuf, sizeof( vid ));
2256 ibuf += sizeof( vid );
2257 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2258 bitmap = ntohs( bitmap );
2259 ibuf += sizeof(bitmap);
2261 if (( vol = getvolbyvid( vid )) == NULL ) {
2262 return( AFPERR_PARAM );
2265 if ((vol->v_flags & AFPVOL_RO))
2266 return AFPERR_VLOCK;
2268 /* we can only set the backup date. */
2269 if (bitmap != (1 << VOLPBIT_BDATE))
2270 return AFPERR_BITMAP;
2273 if ( ad_open(&ad, vol->v_path, ADFLAGS_HF | ADFLAGS_DIR | ADFLAGS_RDWR) < 0 ) {
2275 return AFPERR_VLOCK;
2277 return AFPERR_ACCESS;
2280 memcpy(&aint, ibuf, sizeof(aint));
2281 ad_setdate(&ad, AD_DATE_BACKUP, aint);
2283 ad_close(&ad, ADFLAGS_HF);
2288 * precreate a folder
2289 * this is only intended for folders in the volume root
2290 * It will *not* work if the folder name contains extended characters
2292 static int create_special_folder (const struct vol *vol, const struct _special_folder *folder)
2301 p = (char *) malloc ( strlen(vol->v_path)+strlen(folder->name)+2);
2303 LOG(log_error, logtype_afpd,"malloc failed");
2307 q=strdup(folder->name);
2309 LOG(log_error, logtype_afpd,"malloc failed");
2313 strcpy(p, vol->v_path);
2318 if ((vol->v_casefold & AFPVOL_MTOUUPPER))
2320 else if ((vol->v_casefold & AFPVOL_MTOULOWER))
2326 if ( (ret = stat( p, &st )) < 0 ) {
2327 if (folder->precreate) {
2328 if (ad_mkdir(p, folder->mode)) {
2329 LOG(log_debug, logtype_afpd,"Creating '%s' failed in %s: %s", p, vol->v_path, strerror(errno));
2338 if ( !ret && folder->hide) {
2341 if (ad_open(&ad, p, ADFLAGS_HF | ADFLAGS_DIR | ADFLAGS_RDWR | ADFLAGS_CREATE, 0666) != 0) {
2347 ad_setname(&ad, folder->name);
2349 ad_getattr(&ad, &attr);
2350 attr |= htons( ntohs( attr ) | ATTRBIT_INVISIBLE );
2351 ad_setattr(&ad, attr);
2353 /* do the same with the finder info */
2354 if (ad_entry(&ad, ADEID_FINDERI)) {
2355 memcpy(&attr, ad_entry(&ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF, sizeof(attr));
2356 attr |= htons(FINDERINFO_INVISIBLE);
2357 memcpy(ad_entry(&ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF,&attr, sizeof(attr));
2361 ad_close(&ad, ADFLAGS_HF);
2368 static void handle_special_folders (const struct vol * vol)
2370 const _special_folder *p = &special_folders[0];
2373 process_uid = geteuid();
2375 if (seteuid(0) == -1) {
2380 for (; p->name != NULL; p++) {
2381 create_special_folder (vol, p);
2385 if (seteuid(process_uid) == -1) {
2386 LOG(log_error, logtype_logger, "can't seteuid back %s", strerror(errno));
2392 const struct vol *getvolumes(void)
2397 void unload_volumes(void)
2399 LOG(log_debug, logtype_afpd, "unload_volumes_and_extmap");
2404 * Get a volumes UUID from the config file.
2405 * If there is none, it is generated and stored there.
2407 * Returns pointer to allocated storage on success, NULL on error.
2409 static char *get_vol_uuid(const AFPObj *obj, const char *volname)
2412 char buf[1024], uuid[UUID_PRINTABLE_STRING_LENGTH], *p;
2414 struct stat tmpstat;
2417 if ((fp = fopen(obj->options.uuidconf, "r")) != NULL) { /* read open? */
2418 /* scan in the conf file */
2419 while (fgets(buf, sizeof(buf), fp) != NULL) {
2421 while (p && isblank(*p))
2423 if (!p || (*p == '#') || (*p == '\n'))
2424 continue; /* invalid line */
2427 if ((volname_conf = strtok( p, "\"" )) == NULL)
2428 continue; /* syntax error */
2430 if ((volname_conf = strtok( p, " \t" )) == NULL)
2431 continue; /* syntax error: invalid name */
2433 p = strchr(p, '\0');
2436 continue; /* syntax error */
2438 if (strcmp(volname, volname_conf) != 0)
2439 continue; /* another volume name */
2441 while (p && isblank(*p))
2444 if (sscanf(p, "%36s", uuid) == 1 ) {
2445 for (int i=0; uuid[i]; i++)
2446 uuid[i] = toupper(uuid[i]);
2447 LOG(log_debug, logtype_afpd, "get_uuid('%s'): UUID: '%s'", volname, uuid);
2449 return strdup(uuid);
2457 /* not found or no file, reopen in append mode */
2459 if (stat(obj->options.uuidconf, &tmpstat)) { /* no file */
2460 if (( fd = creat(obj->options.uuidconf, 0644 )) < 0 ) {
2461 LOG(log_error, logtype_afpd, "ERROR: Cannot create %s (%s).",
2462 obj->options.uuidconf, strerror(errno));
2465 if (( fp = fdopen( fd, "w" )) == NULL ) {
2466 LOG(log_error, logtype_afpd, "ERROR: Cannot fdopen %s (%s).",
2467 obj->options.uuidconf, strerror(errno));
2471 } else if ((fp = fopen(obj->options.uuidconf, "a+")) == NULL) { /* not found */
2472 LOG(log_error, logtype_afpd, "Cannot create or append to %s (%s).",
2473 obj->options.uuidconf, strerror(errno));
2476 fseek(fp, 0L, SEEK_END);
2477 if(ftell(fp) == 0) { /* size = 0 */
2478 fprintf(fp, "# DON'T TOUCH NOR COPY THOUGHTLESSLY!\n");
2479 fprintf(fp, "# This file is auto-generated by afpd\n");
2480 fprintf(fp, "# and stores UUIDs for TM volumes.\n\n");
2482 fseek(fp, -1L, SEEK_END);
2483 if(fgetc(fp) != '\n') fputc('\n', fp); /* last char is \n? */
2486 /* generate uuid and write to file */
2489 randombytes((void *)id, 16);
2490 cp = uuid_bin2string(id);
2492 LOG(log_debug, logtype_afpd, "get_uuid('%s'): generated UUID '%s'", volname, cp);
2494 fprintf(fp, "\"%s\"\t%36s\n", volname, cp);