2 * Copyright (c) 1990,1993 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
8 #endif /* HAVE_CONFIG_H */
23 #else /* STDC_HEADERS */
27 #endif /* HAVE_STRCHR */
28 char *strchr (), *strrchr ();
30 #define memcpy(d,s,n) bcopy ((s), (d), (n))
31 #define memmove(d,s,n) bcopy ((s), (d), (n))
32 #endif /* ! HAVE_MEMCPY */
33 #endif /* STDC_HEADERS */
34 #include <sys/param.h>
35 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
40 #include <atalk/asp.h>
41 #include <atalk/dsi.h>
42 #include <atalk/adouble.h>
43 #include <atalk/afp.h>
44 #include <atalk/util.h>
45 #include <atalk/volinfo.h>
46 #include <atalk/logger.h>
47 #include <atalk/vfs.h>
48 #include <atalk/uuid.h>
49 #include <atalk/bstrlib.h>
50 #include <atalk/bstradd.h>
51 #include <atalk/ftw.h>
54 #include <atalk/cnid.h>
58 #include "directory.h"
67 extern int afprun(int root, char *cmd, int *outfd);
70 #define MIN(a, b) ((a) < (b) ? (a) : (b))
73 #ifndef NO_LARGE_VOL_SUPPORT
74 #if BYTE_ORDER == BIG_ENDIAN
77 #else /* BYTE_ORDER == BIG_ENDIAN */
78 #define hton64(x) ((u_int64_t) (htonl(((x) >> 32) & 0xffffffffLL)) | \
79 (u_int64_t) ((htonl(x) & 0xffffffffLL) << 32))
80 #define ntoh64(x) (hton64(x))
81 #endif /* BYTE_ORDER == BIG_ENDIAN */
82 #endif /* ! NO_LARGE_VOL_SUPPORT */
84 #ifndef UUID_PRINTABLE_STRING_LENGTH
85 #define UUID_PRINTABLE_STRING_LENGTH 37
89 struct vol *current_vol; /* last volume from getvolbyvid() */
91 static struct vol *Volumes = NULL;
92 static u_int16_t lastvid = 0;
93 static char *Trash = "\02\024Network Trash Folder";
95 static struct extmap *Extmap = NULL, *Defextmap = NULL;
96 static int Extmap_cnt;
97 static void free_extmap(void);
99 #define VOLOPT_ALLOW 0 /* user allow list */
100 #define VOLOPT_DENY 1 /* user deny list */
101 #define VOLOPT_RWLIST 2 /* user rw list */
102 #define VOLOPT_ROLIST 3 /* user ro list */
103 #define VOLOPT_PASSWORD 4 /* volume password */
104 #define VOLOPT_CASEFOLD 5 /* character case mangling */
105 #define VOLOPT_FLAGS 6 /* various flags */
106 #define VOLOPT_DBPATH 7 /* path to database */
107 #define VOLOPT_LIMITSIZE 8 /* Limit the size of the volume */
109 #define VOLOPT_VETO 10 /* list of veto filespec */
110 #define VOLOPT_PREEXEC 11 /* preexec command */
111 #define VOLOPT_ROOTPREEXEC 12 /* root preexec command */
112 #define VOLOPT_POSTEXEC 13 /* postexec command */
113 #define VOLOPT_ROOTPOSTEXEC 14 /* root postexec command */
114 #define VOLOPT_ENCODING 15 /* mac encoding (pre OSX)*/
115 #define VOLOPT_MACCHARSET 16
116 #define VOLOPT_CNIDSCHEME 17
117 #define VOLOPT_ADOUBLE 18 /* adouble version */
123 #define VOLOPT_FORCEUID 19 /* force uid for username x */
124 #define VOLOPT_FORCEGID 20 /* force gid for group x */
125 #endif /* FORCE_UIDGID */
127 #define VOLOPT_UMASK 21
128 #define VOLOPT_ALLOWED_HOSTS 22
129 #define VOLOPT_DENIED_HOSTS 23
130 #define VOLOPT_DPERM 24 /* dperm default directories perms */
131 #define VOLOPT_FPERM 25 /* fperm default files perms */
132 #define VOLOPT_DFLTPERM 26 /* perm */
133 #define VOLOPT_EA_VFS 27 /* Extended Attributes vfs indirection */
134 #define VOLOPT_CNIDSERVER 28 /* CNID Server ip address*/
135 #define VOLOPT_CNIDPORT 30 /* CNID server tcp port */
137 #define VOLOPT_MAX 31 /* <== IMPORTANT !!!!!! */
138 #define VOLOPT_NUM (VOLOPT_MAX + 1)
141 #define VOLOPT_DEFAULT ":DEFAULT:"
142 #define VOLOPT_DEFAULT_LEN 9
149 typedef struct _special_folder {
156 static const _special_folder special_folders[] = {
157 {"Network Trash Folder", 1, 0777, 1},
158 {"Temporary Items", 1, 0777, 1},
159 {".AppleDesktop", 1, 0777, 0},
161 {"TheFindByContentFolder", 0, 0, 1},
162 {"TheVolumeSettingsFolder", 0, 0, 1},
166 /* Forward declarations */
167 static void handle_special_folders (const struct vol *);
168 static void deletevol(struct vol *vol);
169 static void volume_free(struct vol *vol);
170 static void check_ea_sys_support(struct vol *vol);
171 static char *get_vol_uuid(const AFPObj *obj, const char *volname);
172 static int readvolfile(AFPObj *obj, struct afp_volume_name *p1,char *p2, int user, struct passwd *pwent);
174 static void volfree(struct vol_option *options, const struct vol_option *save)
179 for (i = 0; i < VOLOPT_MAX; i++) {
180 if (options[i].c_value && (options[i].c_value != save[i].c_value))
181 free(options[i].c_value);
184 for (i = 0; i < VOLOPT_MAX; i++) {
185 if (options[i].c_value)
186 free(options[i].c_value);
192 #define is_var(a, b) (strncmp((a), (b), 2) == 0)
195 * Handle variable substitutions. here's what we understand:
196 * $b -> basename of path
197 * $c -> client ip/appletalk address
198 * $d -> volume pathname on server
199 * $f -> full name (whatever's in the gecos field)
202 * $i -> client ip/appletalk address without port
203 * $s -> server name (hostname if it doesn't exist)
204 * $u -> username (guest is usually nobody)
205 * $v -> volume name or basename if null
206 * $z -> zone (may not exist)
209 * This get's called from readvolfile with
210 * path = NULL, volname = NULL for xlating the volumes path
211 * path = path, volname = NULL for xlating the volumes name
212 * ... and from volumes options parsing code when xlating eg dbpath with
213 * path = path, volname = volname
215 * Using this information we can reject xlation of any variable depeninding on a login
216 * context which is not given in the afp master, where we must evaluate this whole stuff
217 * too for the Zeroconf announcements.
219 static char *volxlate(AFPObj *obj,
232 int xlatevolname = 0;
234 if (parent_or_child == 0)
237 if (path && !volname)
245 dest = calloc(destlen +1, 1);
251 strlcpy(dest, src, destlen +1);
252 if ((p = strchr(src, '$')) == NULL) /* nothing to do */
255 /* first part of the path. just forward to the next variable. */
256 len = MIN((size_t)(p - src), destlen);
262 while (p && destlen > 0) {
263 /* now figure out what the variable is */
265 if (is_var(p, "$b")) {
266 if (afpmaster && xlatevolname)
269 if ((q = strrchr(path, '/')) == NULL)
271 else if (*(q + 1) != '\0')
274 } else if (is_var(p, "$c")) {
275 if (afpmaster && xlatevolname)
277 if (obj->proto == AFPPROTO_ASP) {
278 ASP asp = obj->handle;
280 len = sprintf(dest, "%u.%u", ntohs(asp->asp_sat.sat_addr.s_net),
281 asp->asp_sat.sat_addr.s_node);
285 } else if (obj->proto == AFPPROTO_DSI) {
286 DSI *dsi = obj->handle;
287 len = sprintf(dest, "%s:%u",
288 getip_string((struct sockaddr *)&dsi->client),
289 getip_port((struct sockaddr *)&dsi->client));
293 } else if (is_var(p, "$d")) {
294 if (afpmaster && xlatevolname)
297 } else if (pwd && is_var(p, "$f")) {
298 if (afpmaster && xlatevolname)
300 if ((r = strchr(pwd->pw_gecos, ',')))
303 } else if (pwd && is_var(p, "$g")) {
304 if (afpmaster && xlatevolname)
306 struct group *grp = getgrgid(pwd->pw_gid);
309 } else if (is_var(p, "$h")) {
310 q = obj->options.hostname;
311 } else if (is_var(p, "$i")) {
312 if (afpmaster && xlatevolname)
314 if (obj->proto == AFPPROTO_ASP) {
315 ASP asp = obj->handle;
317 len = sprintf(dest, "%u", ntohs(asp->asp_sat.sat_addr.s_net));
321 } else if (obj->proto == AFPPROTO_DSI) {
322 DSI *dsi = obj->handle;
323 q = getip_string((struct sockaddr *)&dsi->client);
325 } else if (is_var(p, "$s")) {
328 else if (obj->options.server) {
329 q = obj->options.server;
331 q = obj->options.hostname;
332 } else if (obj->username && is_var(p, "$u")) {
333 if (afpmaster && xlatevolname)
336 if ( obj->options.ntseparator && (sep = strchr(obj->username, obj->options.ntseparator[0])) != NULL)
340 } else if (is_var(p, "$v")) {
341 if (afpmaster && xlatevolname)
347 if ((q = strrchr(path, '/')) == NULL)
349 else if (*(q + 1) != '\0')
352 } else if (is_var(p, "$z")) {
354 } else if (is_var(p, "$$")) {
359 /* copy the stuff over. if we don't understand something that we
360 * should, just skip it over. */
362 len = MIN(p == q ? 2 : strlen(q), destlen);
363 strncpy(dest, q, len);
368 /* stuff up to next $ */
370 p = strchr(src, '$');
371 len = p ? MIN((size_t)(p - src), destlen) : destlen;
373 strncpy(dest, src, len);
381 /* to make sure that val is valid, make sure to select an opt that
383 static int optionok(const char *buf, const char *opt, const char *val)
385 if (!strstr(buf,opt))
393 /* -------------------- */
394 static void setoption(struct vol_option *options, struct vol_option *save, int opt, const char *val)
396 if (options[opt].c_value && (!save || options[opt].c_value != save[opt].c_value))
397 free(options[opt].c_value);
398 options[opt].c_value = strdup(val + 1);
401 /* ------------------------------------------
402 handle all the options. tmp can't be NULL. */
403 static void volset(struct vol_option *options, struct vol_option *save,
404 char *volname, int vlen,
409 val = strchr(tmp, ':');
411 /* we'll assume it's a volume name. */
412 strncpy(volname, tmp, vlen);
417 LOG(log_debug, logtype_afpd, "Parsing volset %s", val);
419 if (optionok(tmp, "allow:", val)) {
420 setoption(options, save, VOLOPT_ALLOW, val);
422 } else if (optionok(tmp, "deny:", val)) {
423 setoption(options, save, VOLOPT_DENY, val);
425 } else if (optionok(tmp, "rwlist:", val)) {
426 setoption(options, save, VOLOPT_RWLIST, val);
428 } else if (optionok(tmp, "rolist:", val)) {
429 setoption(options, save, VOLOPT_ROLIST, val);
431 } else if (optionok(tmp, "codepage:", val)) {
432 LOG (log_error, logtype_afpd, "The old codepage system has been removed. Please make sure to read the documentation !!!!");
433 /* Make sure we don't screw anything */
435 } else if (optionok(tmp, "volcharset:", val)) {
436 setoption(options, save, VOLOPT_ENCODING, val);
437 } else if (optionok(tmp, "maccharset:", val)) {
438 setoption(options, save, VOLOPT_MACCHARSET, val);
439 } else if (optionok(tmp, "veto:", val)) {
440 setoption(options, save, VOLOPT_VETO, val);
441 } else if (optionok(tmp, "cnidscheme:", val)) {
442 setoption(options, save, VOLOPT_CNIDSCHEME, val);
443 } else if (optionok(tmp, "casefold:", val)) {
444 if (strcasecmp(val + 1, "tolower") == 0)
445 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMLOWER;
446 else if (strcasecmp(val + 1, "toupper") == 0)
447 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMUPPER;
448 else if (strcasecmp(val + 1, "xlatelower") == 0)
449 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UUPPERMLOWER;
450 else if (strcasecmp(val + 1, "xlateupper") == 0)
451 options[VOLOPT_CASEFOLD].i_value = AFPVOL_ULOWERMUPPER;
452 } else if (optionok(tmp, "adouble:", val)) {
453 if (strcasecmp(val + 1, "v1") == 0)
454 options[VOLOPT_ADOUBLE].i_value = AD_VERSION1;
455 #if AD_VERSION == AD_VERSION2
456 else if (strcasecmp(val + 1, "v2") == 0)
457 options[VOLOPT_ADOUBLE].i_value = AD_VERSION2;
458 else if (strcasecmp(val + 1, "osx") == 0)
459 options[VOLOPT_ADOUBLE].i_value = AD_VERSION2_OSX;
460 else if (strcasecmp(val + 1, "sfm") == 0)
461 options[VOLOPT_ADOUBLE].i_value = AD_VERSION1_SFM;
463 } else if (optionok(tmp, "options:", val)) {
466 if ((p = strtok(val + 1, ",")) == NULL) /* nothing */
470 if (strcasecmp(p, "prodos") == 0)
471 options[VOLOPT_FLAGS].i_value |= AFPVOL_A2VOL;
472 else if (strcasecmp(p, "mswindows") == 0) {
473 options[VOLOPT_FLAGS].i_value |= AFPVOL_MSWINDOWS | AFPVOL_USEDOTS;
474 } else if (strcasecmp(p, "crlf") == 0)
475 options[VOLOPT_FLAGS].i_value |= AFPVOL_CRLF;
476 else if (strcasecmp(p, "noadouble") == 0)
477 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOADOUBLE;
478 else if (strcasecmp(p, "ro") == 0)
479 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
480 else if (strcasecmp(p, "nohex") == 0)
481 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOHEX;
482 else if (strcasecmp(p, "usedots") == 0)
483 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS;
484 else if (strcasecmp(p, "invisibledots") == 0)
485 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS | AFPVOL_INV_DOTS;
486 else if (strcasecmp(p, "limitsize") == 0)
487 options[VOLOPT_FLAGS].i_value |= AFPVOL_LIMITSIZE;
488 /* support for either "dropbox" or "dropkludge" */
489 else if (strcasecmp(p, "dropbox") == 0)
490 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
491 else if (strcasecmp(p, "dropkludge") == 0)
492 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
493 else if (strcasecmp(p, "nofileid") == 0)
494 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOFILEID;
495 else if (strcasecmp(p, "nostat") == 0)
496 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOSTAT;
497 else if (strcasecmp(p, "preexec_close") == 0)
498 options[VOLOPT_PREEXEC].i_value = 1;
499 else if (strcasecmp(p, "root_preexec_close") == 0)
500 options[VOLOPT_ROOTPREEXEC].i_value = 1;
501 else if (strcasecmp(p, "upriv") == 0)
502 options[VOLOPT_FLAGS].i_value |= AFPVOL_UNIX_PRIV;
503 else if (strcasecmp(p, "nodev") == 0)
504 options[VOLOPT_FLAGS].i_value |= AFPVOL_NODEV;
505 else if (strcasecmp(p, "caseinsensitive") == 0)
506 options[VOLOPT_FLAGS].i_value |= AFPVOL_CASEINSEN;
507 else if (strcasecmp(p, "illegalseq") == 0)
508 options[VOLOPT_FLAGS].i_value |= AFPVOL_EILSEQ;
509 else if (strcasecmp(p, "nocnidcache") == 0)
510 options[VOLOPT_FLAGS].i_value &= ~AFPVOL_CACHE;
511 else if (strcasecmp(p, "tm") == 0)
512 options[VOLOPT_FLAGS].i_value |= AFPVOL_TM;
513 else if (strcasecmp(p, "searchdb") == 0)
514 options[VOLOPT_FLAGS].i_value |= AFPVOL_SEARCHDB;
515 /* Found this in branch dir-rewrite, maybe we want to use it sometimes */
517 else if (strcasecmp(p, "cdrom") == 0)
518 options[VOLOPT_FLAGS].i_value |= AFPVOL_CDROM | AFPVOL_RO;
520 p = strtok(NULL, ",");
523 } else if (optionok(tmp, "cnidserver:", val)) {
524 setoption(options, save, VOLOPT_CNIDSERVER, val);
526 char *p = strrchr(val + 1, ':');
529 setoption(options, save, VOLOPT_CNIDPORT, p);
532 LOG(log_debug, logtype_afpd, "CNID Server for volume '%s': %s:%s",
533 volname, val + 1, p ? p + 1 : Cnid_port);
535 } else if (optionok(tmp, "dbpath:", val)) {
536 setoption(options, save, VOLOPT_DBPATH, val);
538 } else if (optionok(tmp, "umask:", val)) {
539 options[VOLOPT_UMASK].i_value = (int)strtol(val +1, NULL, 8);
540 } else if (optionok(tmp, "dperm:", val)) {
541 options[VOLOPT_DPERM].i_value = (int)strtol(val+1, NULL, 8);
542 } else if (optionok(tmp, "fperm:", val)) {
543 options[VOLOPT_FPERM].i_value = (int)strtol(val+1, NULL, 8);
544 } else if (optionok(tmp, "perm:", val)) {
545 options[VOLOPT_DFLTPERM].i_value = (int)strtol(val+1, NULL, 8);
546 } else if (optionok(tmp, "password:", val)) {
547 setoption(options, save, VOLOPT_PASSWORD, val);
551 /* this code allows forced uid/gid per volume settings */
552 } else if (optionok(tmp, "forceuid:", val)) {
553 setoption(options, save, VOLOPT_FORCEUID, val);
554 } else if (optionok(tmp, "forcegid:", val)) {
555 setoption(options, save, VOLOPT_FORCEGID, val);
557 #endif /* FORCE_UIDGID */
558 } else if (optionok(tmp, "root_preexec:", val)) {
559 setoption(options, save, VOLOPT_ROOTPREEXEC, val);
561 } else if (optionok(tmp, "preexec:", val)) {
562 setoption(options, save, VOLOPT_PREEXEC, val);
564 } else if (optionok(tmp, "root_postexec:", val)) {
565 setoption(options, save, VOLOPT_ROOTPOSTEXEC, val);
567 } else if (optionok(tmp, "postexec:", val)) {
568 setoption(options, save, VOLOPT_POSTEXEC, val);
570 } else if (optionok(tmp, "allowed_hosts:", val)) {
571 setoption(options, save, VOLOPT_ALLOWED_HOSTS, val);
573 } else if (optionok(tmp, "denied_hosts:", val)) {
574 setoption(options, save, VOLOPT_DENIED_HOSTS, val);
576 } else if (optionok(tmp, "ea:", val)) {
577 if (strcasecmp(val + 1, "ad") == 0)
578 options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_AD;
579 else if (strcasecmp(val + 1, "sys") == 0)
580 options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_SYS;
581 else if (strcasecmp(val + 1, "none") == 0)
582 options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_NONE;
584 } else if (optionok(tmp, "volsizelimit:", val)) {
585 options[VOLOPT_LIMITSIZE].i_value = (uint32_t)strtoul(val + 1, NULL, 10);
588 /* ignore unknown options */
589 LOG(log_debug, logtype_afpd, "ignoring unknown volume option: %s", tmp);
594 /* ----------------- */
595 static void showvol(const ucs2_t *name)
598 for ( volume = Volumes; volume; volume = volume->v_next ) {
599 if (volume->v_hide && !strcasecmp_w( volume->v_name, name ) ) {
606 /* ------------------------------- */
607 static int creatvol(AFPObj *obj, struct passwd *pwd,
608 char *path, char *name,
609 struct vol_option *options,
610 const int user /* user defined volume */
614 int suffixlen, vlen, tmpvlen, u8mvlen, macvlen;
616 char tmpname[AFPVOL_U8MNAMELEN+1];
617 ucs2_t u8mtmpname[(AFPVOL_U8MNAMELEN+1)*2], mactmpname[(AFPVOL_MACNAMELEN+1)*2];
618 char suffix[6]; /* max is #FFFF */
621 LOG(log_debug, logtype_afpd, "createvol: Volume '%s'", name);
623 if ( name == NULL || *name == '\0' ) {
624 if ((name = strrchr( path, '/' )) == NULL) {
625 return -1; /* Obviously not a fully qualified path */
628 /* if you wish to share /, you need to specify a name. */
633 /* suffix for mangling use (lastvid + 1) */
634 /* because v_vid has not been decided yet. */
635 suffixlen = sprintf(suffix, "%c%X", MANGLE_CHAR, lastvid + 1 );
637 vlen = strlen( name );
639 /* Unicode Volume Name */
640 /* Firstly convert name from unixcharset to UTF8-MAC */
642 tmpvlen = convert_charset(obj->options.unixcharset, CH_UTF8_MAC, 0, name, vlen, tmpname, AFPVOL_U8MNAMELEN, &flags);
644 strcpy(tmpname, "???");
648 /* Do we have to mangle ? */
649 if ( (flags & CONV_REQMANGLE) || (tmpvlen > obj->options.volnamelen)) {
650 if (tmpvlen + suffixlen > obj->options.volnamelen) {
652 tmpvlen = convert_charset(obj->options.unixcharset, CH_UTF8_MAC, 0, name, vlen, tmpname, obj->options.volnamelen - suffixlen, &flags);
653 tmpname[tmpvlen >= 0 ? tmpvlen : 0] = 0;
655 strcat(tmpname, suffix);
656 tmpvlen = strlen(tmpname);
659 /* Secondly convert name from UTF8-MAC to UCS2 */
660 if ( 0 >= ( u8mvlen = convert_string(CH_UTF8_MAC, CH_UCS2, tmpname, tmpvlen, u8mtmpname, AFPVOL_U8MNAMELEN*2)) )
663 LOG(log_maxdebug, logtype_afpd, "createvol: Volume '%s' -> UTF8-MAC Name: '%s'", name, tmpname);
665 /* Maccharset Volume Name */
666 /* Firsty convert name from unixcharset to maccharset */
668 tmpvlen = convert_charset(obj->options.unixcharset, obj->options.maccharset, 0, name, vlen, tmpname, AFPVOL_U8MNAMELEN, &flags);
670 strcpy(tmpname, "???");
674 /* Do we have to mangle ? */
675 if ( (flags & CONV_REQMANGLE) || (tmpvlen > AFPVOL_MACNAMELEN)) {
676 if (tmpvlen + suffixlen > AFPVOL_MACNAMELEN) {
678 tmpvlen = convert_charset(obj->options.unixcharset,
679 obj->options.maccharset,
684 AFPVOL_MACNAMELEN - suffixlen,
686 tmpname[tmpvlen >= 0 ? tmpvlen : 0] = 0;
688 strcat(tmpname, suffix);
689 tmpvlen = strlen(tmpname);
692 /* Secondly convert name from maccharset to UCS2 */
693 if ( 0 >= ( macvlen = convert_string(obj->options.maccharset,
698 AFPVOL_U8MNAMELEN*2)) )
701 LOG(log_maxdebug, logtype_afpd, "createvol: Volume '%s' -> Longname: '%s'", name, tmpname);
703 /* check duplicate */
704 for ( volume = Volumes; volume; volume = volume->v_next ) {
705 if ((utf8_encoding() && (strcasecmp_w(volume->v_u8mname, u8mtmpname) == 0))
707 (!utf8_encoding() && (strcasecmp_w(volume->v_macname, mactmpname) == 0))) {
708 LOG (log_error, logtype_afpd,
709 "Duplicate volume name, check AppleVolumes files: previous: \"%s\", new: \"%s\"",
710 volume->v_localname, name);
711 if (volume->v_deleted) {
712 volume->v_new = hide = 1;
715 return -1; /* Won't be able to access it, anyway... */
720 if (!( volume = (struct vol *)calloc(1, sizeof( struct vol ))) ) {
721 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
724 if ( NULL == ( volume->v_localname = strdup(name))) {
725 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
730 if ( NULL == ( volume->v_u8mname = strdup_w(u8mtmpname))) {
731 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
736 if ( NULL == ( volume->v_macname = strdup_w(mactmpname))) {
737 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
742 if (!( volume->v_path = (char *)malloc( strlen( path ) + 1 )) ) {
743 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
749 volume->v_name = utf8_encoding()?volume->v_u8mname:volume->v_macname;
750 volume->v_hide = hide;
751 strcpy( volume->v_path, path );
755 #endif /* __svr4__ */
756 /* os X start at 1 and use network order ie. 1 2 3 */
757 volume->v_vid = ++lastvid;
758 volume->v_vid = htons(volume->v_vid);
760 if (check_vol_acl_support(volume))
761 volume->v_flags |= AFPVOL_ACLS
767 volume->v_casefold = options[VOLOPT_CASEFOLD].i_value;
768 volume->v_flags |= options[VOLOPT_FLAGS].i_value;
770 if (options[VOLOPT_EA_VFS].i_value)
771 volume->v_vfs_ea = options[VOLOPT_EA_VFS].i_value;
773 volume->v_ad_options = 0;
774 if ((volume->v_flags & AFPVOL_NODEV))
775 volume->v_ad_options |= ADVOL_NODEV;
776 if ((volume->v_flags & AFPVOL_CACHE))
777 volume->v_ad_options |= ADVOL_CACHE;
778 if ((volume->v_flags & AFPVOL_UNIX_PRIV))
779 volume->v_ad_options |= ADVOL_UNIXPRIV;
780 if ((volume->v_flags & AFPVOL_INV_DOTS))
781 volume->v_ad_options |= ADVOL_INVDOTS;
782 if ((volume->v_flags & AFPVOL_NOADOUBLE))
783 volume->v_ad_options |= ADVOL_NOADOUBLE;
785 if (options[VOLOPT_PASSWORD].c_value)
786 volume->v_password = strdup(options[VOLOPT_PASSWORD].c_value);
788 if (options[VOLOPT_VETO].c_value)
789 volume->v_veto = strdup(options[VOLOPT_VETO].c_value);
791 if (options[VOLOPT_ENCODING].c_value)
792 volume->v_volcodepage = strdup(options[VOLOPT_ENCODING].c_value);
794 if (options[VOLOPT_MACCHARSET].c_value)
795 volume->v_maccodepage = strdup(options[VOLOPT_MACCHARSET].c_value);
797 if (options[VOLOPT_DBPATH].c_value)
798 volume->v_dbpath = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_DBPATH].c_value, pwd, path, name);
800 if (options[VOLOPT_CNIDSCHEME].c_value)
801 volume->v_cnidscheme = strdup(options[VOLOPT_CNIDSCHEME].c_value);
803 if (options[VOLOPT_CNIDSERVER].c_value)
804 volume->v_cnidserver = strdup(options[VOLOPT_CNIDSERVER].c_value);
806 if (options[VOLOPT_CNIDPORT].c_value)
807 volume->v_cnidport = strdup(options[VOLOPT_CNIDPORT].c_value);
809 if (options[VOLOPT_UMASK].i_value)
810 volume->v_umask = (mode_t)options[VOLOPT_UMASK].i_value;
812 if (options[VOLOPT_DPERM].i_value)
813 volume->v_dperm = (mode_t)options[VOLOPT_DPERM].i_value;
815 if (options[VOLOPT_FPERM].i_value)
816 volume->v_fperm = (mode_t)options[VOLOPT_FPERM].i_value;
818 if (options[VOLOPT_DFLTPERM].i_value)
819 volume->v_perm = (mode_t)options[VOLOPT_DFLTPERM].i_value;
821 if (options[VOLOPT_ADOUBLE].i_value)
822 volume->v_adouble = options[VOLOPT_ADOUBLE].i_value;
824 volume->v_adouble = AD_VERSION;
826 if (options[VOLOPT_LIMITSIZE].i_value)
827 volume->v_limitsize = options[VOLOPT_LIMITSIZE].i_value;
829 /* Mac to Unix conversion flags*/
830 volume->v_mtou_flags = 0;
831 if (!(volume->v_flags & AFPVOL_NOHEX))
832 volume->v_mtou_flags |= CONV_ESCAPEHEX;
833 if (!(volume->v_flags & AFPVOL_USEDOTS))
834 volume->v_mtou_flags |= CONV_ESCAPEDOTS;
835 if ((volume->v_flags & AFPVOL_EILSEQ))
836 volume->v_mtou_flags |= CONV__EILSEQ;
838 if ((volume->v_casefold & AFPVOL_MTOUUPPER))
839 volume->v_mtou_flags |= CONV_TOUPPER;
840 else if ((volume->v_casefold & AFPVOL_MTOULOWER))
841 volume->v_mtou_flags |= CONV_TOLOWER;
843 /* Unix to Mac conversion flags*/
844 volume->v_utom_flags = CONV_IGNORE | CONV_UNESCAPEHEX;
845 if ((volume->v_casefold & AFPVOL_UTOMUPPER))
846 volume->v_utom_flags |= CONV_TOUPPER;
847 else if ((volume->v_casefold & AFPVOL_UTOMLOWER))
848 volume->v_utom_flags |= CONV_TOLOWER;
850 if ((volume->v_flags & AFPVOL_EILSEQ))
851 volume->v_utom_flags |= CONV__EILSEQ;
854 if (options[VOLOPT_FORCEUID].c_value) {
855 volume->v_forceuid = strdup(options[VOLOPT_FORCEUID].c_value);
857 volume->v_forceuid = NULL; /* set as null so as to return 0 later on */
860 if (options[VOLOPT_FORCEGID].c_value) {
861 volume->v_forcegid = strdup(options[VOLOPT_FORCEGID].c_value);
863 volume->v_forcegid = NULL; /* set as null so as to return 0 later on */
867 if (options[VOLOPT_PREEXEC].c_value)
868 volume->v_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_PREEXEC].c_value, pwd, path, name);
869 volume->v_preexec_close = options[VOLOPT_PREEXEC].i_value;
871 if (options[VOLOPT_POSTEXEC].c_value)
872 volume->v_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_POSTEXEC].c_value, pwd, path, name);
874 if (options[VOLOPT_ROOTPREEXEC].c_value)
875 volume->v_root_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPREEXEC].c_value, pwd, path, name);
876 volume->v_root_preexec_close = options[VOLOPT_ROOTPREEXEC].i_value;
878 if (options[VOLOPT_ROOTPOSTEXEC].c_value)
879 volume->v_root_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPOSTEXEC].c_value, pwd, path, name);
882 volume->v_dperm |= volume->v_perm;
883 volume->v_fperm |= volume->v_perm;
885 /* Check EA support on volume */
886 if (volume->v_vfs_ea == AFPVOL_EA_AUTO)
887 check_ea_sys_support(volume);
890 /* get/store uuid from file in afpd master*/
891 if ((parent_or_child == 0) && (volume->v_flags & AFPVOL_TM)) {
892 char *uuid = get_vol_uuid(obj, volume->v_localname);
894 LOG(log_error, logtype_afpd, "Volume '%s': couldn't get UUID",
895 volume->v_localname);
897 volume->v_uuid = uuid;
898 LOG(log_debug, logtype_afpd, "Volume '%s': UUID '%s'",
899 volume->v_localname, volume->v_uuid);
903 volume->v_next = Volumes;
908 /* ---------------- */
909 static char *myfgets( char *buf, int size, FILE *fp)
915 while ((EOF != ( c = getc( fp )) ) && ( size > 1 )) {
916 if ( c == '\n' || c == '\r' ) {
917 if (p != buf && *(p -1) == '\\') {
939 /* check access list. this function wants something of the following
941 * @group,name,name2,@group2,name3
943 * a NULL argument allows everybody to have access.
944 * we return three things:
946 * 0: list exists, but name isn't in it
950 #ifndef NO_REAL_USER_NAME
951 /* authentication is case insensitive
952 * FIXME should we do the same with group name?
954 #define access_strcmp strcasecmp
957 #define access_strcmp strcmp
961 static int accessvol(const char *args, const char *name)
963 char buf[MAXPATHLEN + 1], *p;
969 strlcpy(buf, args, sizeof(buf));
970 if ((p = strtok(buf, ",")) == NULL) /* nothing, return okay */
974 if (*p == '@') { /* it's a group */
975 if ((gr = getgrnam(p + 1)) && gmem(gr->gr_gid))
977 } else if (access_strcmp(p, name) == 0) /* it's a user name */
979 p = strtok(NULL, ",");
985 static int hostaccessvol(int type, const char *volname, const char *args, const AFPObj *obj)
988 char buf[MAXPATHLEN + 1], *p, *b;
989 DSI *dsi = obj->handle;
990 struct sockaddr_storage client;
995 strlcpy(buf, args, sizeof(buf));
996 if ((p = strtok_r(buf, ",", &b)) == NULL) /* nothing, return okay */
999 if (obj->proto != AFPPROTO_DSI)
1004 char *ipaddr, *mask_char;
1005 struct addrinfo hints, *ai;
1007 ipaddr = strtok(p, "/");
1008 mask_char = strtok(NULL,"/");
1010 /* Get address from string with getaddrinfo */
1011 memset(&hints, 0, sizeof hints);
1012 hints.ai_family = AF_UNSPEC;
1013 hints.ai_socktype = SOCK_STREAM;
1014 if ((ret = getaddrinfo(ipaddr, NULL, &hints, &ai)) != 0) {
1015 LOG(log_error, logtype_afpd, "hostaccessvol: getaddrinfo: %s\n", gai_strerror(ret));
1020 if (mask_char != NULL)
1021 mask_int = atoi(mask_char); /* apply_ip_mask does range checking on it */
1023 if (ai->ai_family == AF_INET) /* IPv4 */
1029 /* Apply mask to addresses */
1030 client = dsi->client;
1031 apply_ip_mask((struct sockaddr *)&client, mask_int);
1032 apply_ip_mask(ai->ai_addr, mask_int);
1034 if (compare_ip((struct sockaddr *)&client, ai->ai_addr) == 0) {
1035 if (type == VOLOPT_DENIED_HOSTS)
1036 LOG(log_info, logtype_afpd, "AFP access denied for client IP '%s' to volume '%s' by denied list",
1037 getip_string((struct sockaddr *)&client), volname);
1044 p = strtok_r(NULL, ",", &b);
1047 if (type == VOLOPT_ALLOWED_HOSTS)
1048 LOG(log_info, logtype_afpd, "AFP access denied for client IP '%s' to volume '%s', not in allowed list",
1049 getip_string((struct sockaddr *)&dsi->client), volname);
1053 static void setextmap(char *ext, char *type, char *creator, int user)
1058 if (Extmap == NULL) {
1059 if (( Extmap = calloc(1, sizeof( struct extmap ))) == NULL ) {
1060 LOG(log_error, logtype_afpd, "setextmap: calloc: %s", strerror(errno) );
1065 for ( em = Extmap, cnt = 0; em->em_ext; em++, cnt++) {
1066 if ( (strdiacasecmp( em->em_ext, ext )) == 0 ) {
1071 if ( em->em_ext == NULL ) {
1072 if (!(Extmap = realloc( Extmap, sizeof( struct extmap ) * (cnt +2))) ) {
1073 LOG(log_error, logtype_afpd, "setextmap: realloc: %s", strerror(errno) );
1076 (Extmap +cnt +1)->em_ext = NULL;
1078 } else if ( !user ) {
1084 if (!(em->em_ext = strdup( ext))) {
1085 LOG(log_error, logtype_afpd, "setextmap: strdup: %s", strerror(errno) );
1089 if ( *type == '\0' ) {
1090 memcpy(em->em_type, "\0\0\0\0", sizeof( em->em_type ));
1092 memcpy(em->em_type, type, sizeof( em->em_type ));
1094 if ( *creator == '\0' ) {
1095 memcpy(em->em_creator, "\0\0\0\0", sizeof( em->em_creator ));
1097 memcpy(em->em_creator, creator, sizeof( em->em_creator ));
1101 /* -------------------------- */
1102 static int extmap_cmp(const void *map1, const void *map2)
1104 const struct extmap *em1 = map1;
1105 const struct extmap *em2 = map2;
1106 return strdiacasecmp(em1->em_ext, em2->em_ext);
1109 static void sortextmap( void)
1114 if ((em = Extmap) == NULL) {
1117 while (em->em_ext) {
1122 qsort(Extmap, Extmap_cnt, sizeof(struct extmap), extmap_cmp);
1123 if (*Extmap->em_ext == 0) {
1124 /* the first line is really "." the default entry,
1125 * we remove the leading '.' in setextmap
1132 /* ----------------------
1134 static void free_extmap( void)
1139 for ( em = Extmap; em->em_ext; em++) {
1149 /* ----------------------
1151 static int volfile_changed(struct afp_volume_name *p)
1157 name = p->full_name;
1161 if (!stat( name, &st) && st.st_mtime > p->mtime) {
1162 p->mtime = st.st_mtime;
1168 /* ----------------------
1169 * Read a volume configuration file and add the volumes contained within to
1170 * the global volume list. This gets called from the forked afpd childs.
1171 * The master now reads this too for Zeroconf announcements.
1173 * If p2 is non-NULL, the file that is opened is
1176 * Lines that begin with # and blank lines are ignored.
1177 * Volume lines are of the form:
1178 * <unix path> [<volume name>] [allow:<user>,<@group>,...] \
1179 * [codepage:<file>] [casefold:<num>]
1180 * <extension> TYPE [CREATOR]
1183 static int readvolfile(AFPObj *obj, struct afp_volume_name *p1, char *p2, int user, struct passwd *pwent)
1186 char path[MAXPATHLEN + 1];
1187 char tmp[MAXPATHLEN + 1];
1188 char volname[AFPVOL_U8MNAMELEN + 1];
1190 char type[5], creator[5];
1195 struct vol_option save_options[VOLOPT_NUM];
1196 struct vol_option options[VOLOPT_NUM];
1202 strcpy( path, p1->name );
1204 strcat( path, "/" );
1206 if (p1->full_name) {
1207 free(p1->full_name);
1209 p1->full_name = strdup(path);
1212 if (NULL == ( fp = fopen( path, "r" )) ) {
1216 if (fd != -1 && !fstat( fd, &st) ) {
1217 p1->mtime = st.st_mtime;
1220 if ((read_lock(fd, 0, SEEK_SET, 0)) != 0) {
1221 LOG(log_error, logtype_afpd, "readvolfile: can't lock volume file \"%s\"", path);
1222 if ( fclose( fp ) != 0 ) {
1223 LOG(log_error, logtype_afpd, "readvolfile: fclose: %s", strerror(errno) );
1228 memset(save_options, 0, sizeof(save_options));
1230 /* Enable some default options for all volumes */
1231 save_options[VOLOPT_FLAGS].i_value |= AFPVOL_CACHE;
1232 save_options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_AUTO;
1233 LOG(log_maxdebug, logtype_afpd, "readvolfile: seeding default umask: %04o",
1234 obj->options.umask);
1235 save_options[VOLOPT_UMASK].i_value = obj->options.umask;
1237 LOG(log_debug, logtype_afpd, "readvolfile: \"%s\"", path);
1239 while ( myfgets( buf, sizeof( buf ), fp ) != NULL ) {
1240 initline( strlen( buf ), buf );
1241 parseline( sizeof( path ) - 1, path );
1248 /* change the default options for this file */
1249 if (strncmp(path, VOLOPT_DEFAULT, VOLOPT_DEFAULT_LEN) == 0) {
1251 for (i = 0; i < VOLOPT_NUM; i++) {
1252 if (parseline( sizeof( path ) - VOLOPT_DEFAULT_LEN - 1,
1253 path + VOLOPT_DEFAULT_LEN) < 0)
1255 volset(save_options, NULL, tmp, sizeof(tmp) - 1,
1256 path + VOLOPT_DEFAULT_LEN);
1262 if (( p = strchr( path, '/' )) != NULL ) {
1270 if ( u == NULL || *u == '\0' || ( pw = getpwnam( u )) == NULL ) {
1273 strcpy( tmp, pw->pw_dir );
1274 if ( p != NULL && *p != '\0' ) {
1281 /* send path through variable substitution */
1282 if (*path != '~') /* need to copy path to tmp */
1284 if (!pwent && obj->username)
1285 pwent = getpwnam(obj->username);
1287 if (volxlate(obj, path, sizeof(path) - 1, tmp, pwent, NULL, NULL) == NULL)
1290 /* this is sort of braindead. basically, i want to be
1291 * able to specify things in any order, but i don't want to
1292 * re-write everything. */
1294 memcpy(options, save_options, sizeof(options));
1297 /* read in up to VOLOP_NUM possible options */
1298 for (i = 0; i < VOLOPT_NUM; i++) {
1299 if (parseline( sizeof( tmp ) - 1, tmp ) < 0)
1302 volset(options, save_options, volname, sizeof(volname) - 1, tmp);
1305 /* check allow/deny lists (if not afpd master loading volumes for Zeroconf reg.):
1306 allow -> either no list (-1), or in list (1)
1307 deny -> either no list (-1), or not in list (0) */
1308 if (parent_or_child == 0
1310 (accessvol(options[VOLOPT_ALLOW].c_value, obj->username) &&
1311 (accessvol(options[VOLOPT_DENY].c_value, obj->username) < 1) &&
1312 hostaccessvol(VOLOPT_ALLOWED_HOSTS, volname, options[VOLOPT_ALLOWED_HOSTS].c_value, obj) &&
1313 (hostaccessvol(VOLOPT_DENIED_HOSTS, volname, options[VOLOPT_DENIED_HOSTS].c_value, obj) < 1))) {
1315 /* handle read-only behaviour. semantics:
1316 * 1) neither the rolist nor the rwlist exist -> rw
1317 * 2) rolist exists -> ro if user is in it.
1318 * 3) rwlist exists -> ro unless user is in it. */
1319 if (parent_or_child == 1
1321 ((options[VOLOPT_FLAGS].i_value & AFPVOL_RO) == 0)
1323 ((accessvol(options[VOLOPT_ROLIST].c_value, obj->username) == 1) ||
1324 !accessvol(options[VOLOPT_RWLIST].c_value, obj->username)))
1325 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
1327 /* do variable substitution for volname */
1328 if (volxlate(obj, tmp, sizeof(tmp) - 1, volname, pwent, path, NULL) == NULL)
1331 creatvol(obj, pwent, path, tmp, options, p2 != NULL);
1333 volfree(options, save_options);
1337 parseline( sizeof( type ) - 1, type );
1338 parseline( sizeof( creator ) - 1, creator );
1339 setextmap( path, type, creator, user);
1346 volfree(save_options, NULL);
1348 if ( fclose( fp ) != 0 ) {
1349 LOG(log_error, logtype_afpd, "readvolfile: fclose: %s", strerror(errno) );
1355 /* ------------------------------- */
1356 static void volume_free(struct vol *vol)
1358 free(vol->v_localname);
1359 vol->v_localname = NULL;
1360 free(vol->v_u8mname);
1361 vol->v_u8mname = NULL;
1362 free(vol->v_macname);
1363 vol->v_macname = NULL;
1365 free(vol->v_password);
1367 free(vol->v_volcodepage);
1368 free(vol->v_maccodepage);
1369 free(vol->v_cnidscheme);
1370 free(vol->v_dbpath);
1373 free(vol->v_forceuid);
1374 free(vol->v_forcegid);
1375 #endif /* FORCE_UIDGID */
1380 /* ------------------------------- */
1381 static void free_volumes(void )
1384 struct vol *nvol, *ovol;
1386 for ( vol = Volumes; vol; vol = vol->v_next ) {
1387 if (( vol->v_flags & AFPVOL_OPEN ) ) {
1394 for ( vol = Volumes, ovol = NULL; vol; vol = nvol) {
1397 if (vol->v_localname == NULL) {
1398 if (Volumes == vol) {
1403 ovol->v_next = nvol;
1413 /* ------------------------------- */
1414 static void volume_unlink(struct vol *volume)
1416 struct vol *vol, *ovol, *nvol;
1418 if (volume == Volumes) {
1419 Volumes = Volumes->v_next;
1422 for ( vol = Volumes->v_next, ovol = Volumes; vol; vol = nvol) {
1425 if (vol == volume) {
1426 ovol->v_next = nvol;
1435 static off_t getused_size; /* result of getused() */
1438 nftw callback for getused()
1440 static int getused_stat(const char *path,
1441 const struct stat *statp,
1445 if (tflag == FTW_F) {
1446 getused_size += statp->st_size;
1452 * Calculate used size of a volume with nftw
1454 * 1) Check if the volume has been modified or the v_mtime has not
1456 * 2) Call nftw if yes
1458 * @param vol (r) volume to calculate
1460 static int getused(const struct vol *vol)
1462 static time_t vol_mtime = 0;
1465 if (!vol->v_mtime || (vol_mtime < vol->v_mtime)) { /* 1 */
1466 vol_mtime = vol->v_mtime;
1468 ret = nftw(vol->v_path, getused_stat, NULL, 20, FTW_PHYS); /* 2 */
1470 LOG(log_note, logtype_afpd, "volparams: cached used: %" PRIu64 " bytes",
1479 static int getvolspace(struct vol *vol,
1480 u_int32_t *bfree, u_int32_t *btotal,
1481 VolSpace *xbfree, VolSpace *xbtotal, u_int32_t *bsize)
1486 #ifndef NO_QUOTA_SUPPORT
1487 VolSpace qfree, qtotal;
1490 spaceflag = AFPVOL_GVSMASK & vol->v_flags;
1491 /* report up to 2GB if afp version is < 2.2 (4GB if not) */
1492 maxsize = (vol->v_flags & AFPVOL_A2VOL) ? 0x01fffe00 :
1493 (((afp_version < 22) || (vol->v_flags & AFPVOL_LIMITSIZE))
1494 ? 0x7fffffffL : 0xffffffffL);
1497 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_AFSGVS ) {
1498 if ( afs_getvolspace( vol, xbfree, xbtotal, bsize ) == AFP_OK ) {
1499 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_AFSGVS;
1500 goto getvolspace_done;
1505 if (( rc = ustatfs_getvolspace( vol, xbfree, xbtotal, bsize)) != AFP_OK ) {
1509 #define min(a,b) ((a)<(b)?(a):(b))
1510 #ifndef NO_QUOTA_SUPPORT
1511 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_UQUOTA ) {
1512 if ( uquota_getvolspace( vol, &qfree, &qtotal, *bsize ) == AFP_OK ) {
1513 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_UQUOTA;
1514 *xbfree = min(*xbfree, qfree);
1515 *xbtotal = min( *xbtotal, qtotal);
1516 goto getvolspace_done;
1520 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_USTATFS;
1523 if (vol->v_limitsize) {
1524 if (getused(vol) != 0)
1526 LOG(log_note, logtype_afpd, "volparams: used on volume: %" PRIu64 " bytes",
1529 *xbtotal = min(*xbtotal, (vol->v_limitsize * 1024 * 1024));
1530 *xbfree = min(*xbfree, *xbtotal < getused_size ? 0 : *xbtotal - getused_size);
1533 *bfree = min( *xbfree, maxsize);
1534 *btotal = min( *xbtotal, maxsize);
1538 /* -----------------------
1539 * set volume creation date
1540 * avoid duplicate, well at least it tries
1542 static void vol_setdate(u_int16_t id, struct adouble *adp, time_t date)
1545 struct vol *vol = Volumes;
1547 for ( volume = Volumes; volume; volume = volume->v_next ) {
1548 if (volume->v_vid == id) {
1551 else if ((time_t)(AD_DATE_FROM_UNIX(date)) == volume->v_ctime) {
1553 volume = Volumes; /* restart */
1556 vol->v_ctime = AD_DATE_FROM_UNIX(date);
1557 ad_setdate(adp, AD_DATE_CREATE | AD_DATE_UNIX, date);
1560 /* ----------------------- */
1561 static int getvolparams( u_int16_t bitmap, struct vol *vol, struct stat *st, char *buf, size_t *buflen)
1564 int bit = 0, isad = 1;
1567 u_int32_t bfree, btotal, bsize;
1568 VolSpace xbfree, xbtotal; /* extended bytes */
1569 char *data, *nameoff = NULL;
1572 LOG(log_debug, logtype_afpd, "getvolparams: Volume '%s'", vol->v_localname);
1574 /* courtesy of jallison@whistle.com:
1575 * For MacOS8.x support we need to create the
1576 * .Parent file here if it doesn't exist. */
1578 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1579 if ( ad_open_metadata( vol->v_path, ADFLAGS_DIR, O_CREAT, &ad) < 0 ) {
1581 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1583 } else if (ad_get_MD_flags( &ad ) & O_CREAT) {
1584 slash = strrchr( vol->v_path, '/' );
1588 slash = vol->v_path;
1589 if (ad_getentryoff(&ad, ADEID_NAME)) {
1590 ad_setentrylen( &ad, ADEID_NAME, strlen( slash ));
1591 memcpy(ad_entry( &ad, ADEID_NAME ), slash,
1592 ad_getentrylen( &ad, ADEID_NAME ));
1594 vol_setdate(vol->v_vid, &ad, st->st_mtime);
1598 if (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0)
1599 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1601 vol->v_ctime = aint;
1604 if (( bitmap & ( (1<<VOLPBIT_BFREE)|(1<<VOLPBIT_BTOTAL) |
1605 (1<<VOLPBIT_XBFREE)|(1<<VOLPBIT_XBTOTAL) |
1606 (1<<VOLPBIT_BSIZE)) ) != 0 ) {
1607 if ( getvolspace( vol, &bfree, &btotal, &xbfree, &xbtotal,
1608 &bsize) != AFP_OK ) {
1610 ad_close( &ad, ADFLAGS_HF );
1612 return( AFPERR_PARAM );
1617 while ( bitmap != 0 ) {
1618 while (( bitmap & 1 ) == 0 ) {
1626 /* check for read-only.
1627 * NOTE: we don't actually set the read-only flag unless
1628 * it's passed in that way as it's possible to mount
1629 * a read-write filesystem under a read-only one. */
1630 if ((vol->v_flags & AFPVOL_RO) ||
1631 ((utime(vol->v_path, NULL) < 0) && (errno == EROFS))) {
1632 ashort |= VOLPBIT_ATTR_RO;
1634 /* prior 2.1 only VOLPBIT_ATTR_RO is defined */
1635 if (afp_version > 20) {
1636 if (0 == (vol->v_flags & AFPVOL_NOFILEID) && vol->v_cdb != NULL &&
1637 (vol->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1638 ashort |= VOLPBIT_ATTR_FILEID;
1640 ashort |= VOLPBIT_ATTR_CATSEARCH;
1642 if (afp_version >= 30) {
1643 ashort |= VOLPBIT_ATTR_UTF8;
1644 if (vol->v_flags & AFPVOL_UNIX_PRIV)
1645 ashort |= VOLPBIT_ATTR_UNIXPRIV;
1646 if (vol->v_flags & AFPVOL_TM)
1647 ashort |= VOLPBIT_ATTR_TM;
1649 if (afp_version >= 32) {
1651 ashort |= VOLPBIT_ATTR_EXT_ATTRS;
1652 if (vol->v_flags & AFPVOL_ACLS)
1653 ashort |= VOLPBIT_ATTR_ACLS;
1657 ashort = htons(ashort);
1658 memcpy(data, &ashort, sizeof( ashort ));
1659 data += sizeof( ashort );
1663 ashort = htons( AFPVOLSIG_DEFAULT );
1664 memcpy(data, &ashort, sizeof( ashort ));
1665 data += sizeof( ashort );
1668 case VOLPBIT_CDATE :
1669 aint = vol->v_ctime;
1670 memcpy(data, &aint, sizeof( aint ));
1671 data += sizeof( aint );
1674 case VOLPBIT_MDATE :
1675 if ( st->st_mtime > vol->v_mtime ) {
1676 vol->v_mtime = st->st_mtime;
1678 aint = AD_DATE_FROM_UNIX(vol->v_mtime);
1679 memcpy(data, &aint, sizeof( aint ));
1680 data += sizeof( aint );
1683 case VOLPBIT_BDATE :
1684 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1685 aint = AD_DATE_START;
1686 memcpy(data, &aint, sizeof( aint ));
1687 data += sizeof( aint );
1691 memcpy(data, &vol->v_vid, sizeof( vol->v_vid ));
1692 data += sizeof( vol->v_vid );
1695 case VOLPBIT_BFREE :
1696 bfree = htonl( bfree );
1697 memcpy(data, &bfree, sizeof( bfree ));
1698 data += sizeof( bfree );
1701 case VOLPBIT_BTOTAL :
1702 btotal = htonl( btotal );
1703 memcpy(data, &btotal, sizeof( btotal ));
1704 data += sizeof( btotal );
1707 #ifndef NO_LARGE_VOL_SUPPORT
1708 case VOLPBIT_XBFREE :
1709 xbfree = hton64( xbfree );
1710 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1711 bcopy(&xbfree, data, sizeof(xbfree));
1712 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1713 memcpy(data, &xbfree, sizeof( xbfree ));
1714 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1715 data += sizeof( xbfree );
1718 case VOLPBIT_XBTOTAL :
1719 xbtotal = hton64( xbtotal );
1720 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1721 bcopy(&xbtotal, data, sizeof(xbtotal));
1722 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1723 memcpy(data, &xbtotal, sizeof( xbtotal ));
1724 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1725 data += sizeof( xbfree );
1727 #endif /* ! NO_LARGE_VOL_SUPPORT */
1731 data += sizeof( u_int16_t );
1734 case VOLPBIT_BSIZE: /* block size */
1735 bsize = htonl(bsize);
1736 memcpy(data, &bsize, sizeof(bsize));
1737 data += sizeof(bsize);
1742 ad_close( &ad, ADFLAGS_HF );
1744 return( AFPERR_BITMAP );
1750 ashort = htons( data - buf );
1751 memcpy(nameoff, &ashort, sizeof( ashort ));
1752 /* name is always in mac charset */
1753 aint = ucs2_to_charset( vol->v_maccharset, vol->v_macname, data+1, AFPVOL_MACNAMELEN + 1);
1763 ad_close_metadata( &ad);
1765 *buflen = data - buf;
1769 /* ------------------------- */
1770 static int stat_vol(u_int16_t bitmap, struct vol *vol, char *rbuf, size_t *rbuflen)
1776 if ( stat( vol->v_path, &st ) < 0 ) {
1778 return( AFPERR_PARAM );
1780 /* save the volume device number */
1781 vol->v_dev = st.st_dev;
1783 buflen = *rbuflen - sizeof( bitmap );
1784 if (( ret = getvolparams( bitmap, vol, &st,
1785 rbuf + sizeof( bitmap ), &buflen )) != AFP_OK ) {
1789 *rbuflen = buflen + sizeof( bitmap );
1790 bitmap = htons( bitmap );
1791 memcpy(rbuf, &bitmap, sizeof( bitmap ));
1796 /* ------------------------------- */
1797 void load_volumes(AFPObj *obj)
1799 struct passwd *pwent;
1804 /* check files date */
1805 if (obj->options.defaultvol.loaded) {
1806 changed = volfile_changed(&obj->options.defaultvol);
1808 if (obj->options.systemvol.loaded) {
1809 changed |= volfile_changed(&obj->options.systemvol);
1811 if (obj->options.uservol.loaded) {
1812 changed |= volfile_changed(&obj->options.uservol);
1821 if (parent_or_child == 0) {
1822 LOG(log_debug, logtype_afpd, "load_volumes: AFP MASTER");
1824 LOG(log_debug, logtype_afpd, "load_volumes: user: %s", obj->username);
1827 pwent = getpwnam(obj->username);
1828 if ( (obj->options.flags & OPTION_USERVOLFIRST) == 0 ) {
1829 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent);
1832 if ((*obj->username == '\0') || (obj->options.flags & OPTION_NOUSERVOL)) {
1833 readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent);
1836 * Read user's AppleVolumes or .AppleVolumes file
1837 * If neither are readable, read the default volumes file. if
1838 * that doesn't work, create a user share.
1840 if (obj->options.uservol.name) {
1841 free(obj->options.uservol.name);
1843 obj->options.uservol.name = strdup(pwent->pw_dir);
1844 if ( readvolfile(obj, &obj->options.uservol, "AppleVolumes", 1, pwent) < 0 &&
1845 readvolfile(obj, &obj->options.uservol, ".AppleVolumes", 1, pwent) < 0 &&
1846 readvolfile(obj, &obj->options.uservol, "applevolumes", 1, pwent) < 0 &&
1847 readvolfile(obj, &obj->options.uservol, ".applevolumes", 1, pwent) < 0 &&
1848 obj->options.defaultvol.name != NULL ) {
1849 if (readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent) < 0)
1850 creatvol(obj, pwent, pwent->pw_dir, NULL, NULL, 1);
1853 if ( obj->options.flags & OPTION_USERVOLFIRST ) {
1854 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent );
1857 if ( obj->options.closevol ) {
1860 for ( vol = Volumes; vol; vol = vol->v_next ) {
1861 if (vol->v_deleted && !vol->v_new ) {
1870 /* ------------------------------- */
1871 int afp_getsrvrparms(AFPObj *obj, char *ibuf _U_, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
1884 for ( vcnt = 0, volume = Volumes; volume; volume = volume->v_next ) {
1885 if (!(volume->v_flags & AFPVOL_NOSTAT)) {
1888 if ( stat( volume->v_path, &st ) < 0 ) {
1889 LOG(log_info, logtype_afpd, "afp_getsrvrparms(%s): stat: %s",
1890 volume->v_path, strerror(errno) );
1891 continue; /* can't access directory */
1893 if (!S_ISDIR(st.st_mode)) {
1894 continue; /* not a dir */
1896 accessmode(volume->v_path, &ma, NULL, &st);
1897 if ((ma.ma_user & (AR_UREAD | AR_USEARCH)) != (AR_UREAD | AR_USEARCH)) {
1898 continue; /* no r-x access */
1901 if (volume->v_hide) {
1902 continue; /* config file changed but the volume was mounted */
1905 if (utf8_encoding()) {
1906 len = ucs2_to_charset_allocate(CH_UTF8_MAC, &namebuf, volume->v_u8mname);
1908 len = ucs2_to_charset_allocate(obj->options.maccharset, &namebuf, volume->v_macname);
1911 if (len == (size_t)-1)
1914 /* set password bit if there's a volume password */
1915 *data = (volume->v_password) ? AFPSRVR_PASSWD : 0;
1917 /* Apple 2 clients running ProDOS-8 expect one volume to have
1918 bit 0 of this byte set. They will not recognize anything
1919 on the server unless this is the case. I have not
1920 completely worked this out, but it's related to booting
1921 from the server. Support for that function is a ways
1922 off.. <shirsch@ibm.net> */
1923 *data |= (volume->v_flags & AFPVOL_A2VOL) ? AFPSRVR_CONFIGINFO : 0;
1924 *data++ |= 0; /* UNIX PRIVS BIT ..., OSX doesn't seem to use it, so we don't either */
1926 memcpy(data, namebuf, len );
1932 *rbuflen = data - rbuf;
1934 if ( gettimeofday( &tv, NULL ) < 0 ) {
1935 LOG(log_error, logtype_afpd, "afp_getsrvrparms(%s): gettimeofday: %s", volume->v_path, strerror(errno) );
1937 return AFPERR_PARAM;
1939 tv.tv_sec = AD_DATE_FROM_UNIX(tv.tv_sec);
1940 memcpy(data, &tv.tv_sec, sizeof( u_int32_t));
1941 data += sizeof( u_int32_t);
1946 /* ------------------------- */
1947 static int volume_codepage(AFPObj *obj, struct vol *volume)
1949 struct charset_functions *charset;
1952 if (!volume->v_volcodepage)
1953 volume->v_volcodepage = strdup("UTF8");
1955 if ( (charset_t) -1 == ( volume->v_volcharset = add_charset(volume->v_volcodepage)) ) {
1956 LOG (log_error, logtype_afpd, "Setting codepage %s as volume codepage failed", volume->v_volcodepage);
1960 if ( NULL == (charset = find_charset_functions(volume->v_volcodepage)) || charset->flags & CHARSET_ICONV ) {
1961 LOG (log_warning, logtype_afpd, "WARNING: volume encoding %s is *not* supported by netatalk, expect problems !!!!", volume->v_volcodepage);
1964 if (!volume->v_maccodepage)
1965 volume->v_maccodepage = strdup(obj->options.maccodepage);
1967 if ( (charset_t) -1 == ( volume->v_maccharset = add_charset(volume->v_maccodepage)) ) {
1968 LOG (log_error, logtype_afpd, "Setting codepage %s as mac codepage failed", volume->v_maccodepage);
1972 if ( NULL == ( charset = find_charset_functions(volume->v_maccodepage)) || ! (charset->flags & CHARSET_CLIENT) ) {
1973 LOG (log_error, logtype_afpd, "Fatal error: mac charset %s not supported", volume->v_maccodepage);
1976 volume->v_kTextEncoding = htonl(charset->kTextEncoding);
1980 /* ------------------------- */
1981 static int volume_openDB(struct vol *volume)
1985 if ((volume->v_flags & AFPVOL_NODEV)) {
1986 flags |= CNID_FLAG_NODEV;
1989 if (volume->v_cnidscheme == NULL) {
1990 volume->v_cnidscheme = strdup(DEFAULT_CNID_SCHEME);
1991 LOG(log_info, logtype_afpd, "Volume %s use CNID scheme %s.",
1992 volume->v_path, volume->v_cnidscheme);
1995 LOG(log_info, logtype_afpd, "CNID server: %s:%s",
1996 volume->v_cnidserver ? volume->v_cnidserver : Cnid_srv,
1997 volume->v_cnidport ? volume->v_cnidport : Cnid_port);
2000 /* Found this in branch dir-rewrite, maybe we want to use it sometimes */
2002 /* Legacy pre 2.1 way of sharing eg CD-ROM */
2003 if (strcmp(volume->v_cnidscheme, "last") == 0) {
2004 /* "last" is gone. We support it by switching to in-memory "tdb" */
2005 volume->v_cnidscheme = strdup("tdb");
2006 flags |= CNID_FLAG_MEMORY;
2009 /* New way of sharing CD-ROM */
2010 if (volume->v_flags & AFPVOL_CDROM) {
2011 flags |= CNID_FLAG_MEMORY;
2012 if (strcmp(volume->v_cnidscheme, "tdb") != 0) {
2013 free(volume->v_cnidscheme);
2014 volume->v_cnidscheme = strdup("tdb");
2015 LOG(log_info, logtype_afpd, "Volume %s is ejectable, switching to scheme %s.",
2016 volume->v_path, volume->v_cnidscheme);
2021 volume->v_cdb = cnid_open(volume->v_path,
2023 volume->v_cnidscheme,
2025 volume->v_cnidserver ? volume->v_cnidserver : Cnid_srv,
2026 volume->v_cnidport ? volume->v_cnidport : Cnid_port);
2028 if ( ! volume->v_cdb && ! (flags & CNID_FLAG_MEMORY)) {
2029 /* The first attempt failed and it wasn't yet an attempt to open in-memory */
2030 LOG(log_error, logtype_afpd, "Can't open volume \"%s\" CNID backend \"%s\" ",
2031 volume->v_path, volume->v_cnidscheme);
2032 LOG(log_error, logtype_afpd, "Reopen volume %s using in memory temporary CNID DB.",
2034 flags |= CNID_FLAG_MEMORY;
2035 volume->v_cdb = cnid_open (volume->v_path, volume->v_umask, "tdb", flags, NULL, NULL);
2037 /* kill ourself with SIGUSR2 aka msg pending */
2038 if (volume->v_cdb) {
2039 setmessage("Something wrong with the volume's CNID DB, using temporary CNID DB instead."
2040 "Check server messages for details!");
2041 kill(getpid(), SIGUSR2);
2042 /* deactivate cnid caching/storing in AppleDouble files */
2043 volume->v_flags &= ~AFPVOL_CACHE;
2048 return (!volume->v_cdb)?-1:0;
2052 Check if the underlying filesystem supports EAs for ea:sys volumes.
2053 If not, switch to ea:ad.
2054 As we can't check (requires write access) on ro-volumes, we switch ea:auto
2055 volumes that are options:ro to ea:none.
2057 static void check_ea_sys_support(struct vol *vol)
2059 uid_t process_uid = 0;
2060 char eaname[] = {"org.netatalk.supports-eas.XXXXXX"};
2061 const char *eacontent = "yes";
2063 if (vol->v_vfs_ea == AFPVOL_EA_AUTO) {
2065 if ((vol->v_flags & AFPVOL_RO) == AFPVOL_RO) {
2066 LOG(log_info, logtype_afpd, "read-only volume '%s', can't test for EA support, disabling EAs", vol->v_localname);
2067 vol->v_vfs_ea = AFPVOL_EA_NONE;
2073 process_uid = geteuid();
2075 if (seteuid(0) == -1) {
2076 LOG(log_error, logtype_afpd, "check_ea_sys_support: can't seteuid(0): %s", strerror(errno));
2080 if ((sys_setxattr(vol->v_path, eaname, eacontent, 4, 0)) == 0) {
2081 sys_removexattr(vol->v_path, eaname);
2082 vol->v_vfs_ea = AFPVOL_EA_SYS;
2084 LOG(log_warning, logtype_afpd, "volume \"%s\" does not support Extended Attributes, using ea:ad instead",
2086 vol->v_vfs_ea = AFPVOL_EA_AD;
2090 if (seteuid(process_uid) == -1) {
2091 LOG(log_error, logtype_afpd, "can't seteuid back %s", strerror(errno));
2098 /* -------------------------
2099 * we are the user here
2101 int afp_openvol(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2112 char path[ MAXPATHLEN + 1];
2118 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2119 bitmap = ntohs( bitmap );
2120 ibuf += sizeof( bitmap );
2123 if (( bitmap & (1<<VOLPBIT_VID)) == 0 ) {
2124 return AFPERR_BITMAP;
2127 len = (unsigned char)*ibuf++;
2128 volname = obj->oldtmp;
2130 if ((volname_tmp = strchr(volname,'+')) != NULL)
2131 volname = volname_tmp+1;
2133 if (utf8_encoding()) {
2134 namelen = convert_string(CH_UTF8_MAC, CH_UCS2, ibuf, len, volname, sizeof(obj->oldtmp));
2136 namelen = convert_string(obj->options.maccharset, CH_UCS2, ibuf, len, volname, sizeof(obj->oldtmp));
2139 if ( namelen <= 0) {
2140 return AFPERR_PARAM;
2144 if ((len + 1) & 1) /* pad to an even boundary */
2149 for ( volume = Volumes; volume; volume = volume->v_next ) {
2150 if ( strcasecmp_w( (ucs2_t*) volname, volume->v_name ) == 0 ) {
2155 if ( volume == NULL ) {
2156 return AFPERR_PARAM;
2159 /* check for a volume password */
2160 if (volume->v_password && strncmp(ibuf, volume->v_password, VOLPASSLEN)) {
2161 return AFPERR_ACCESS;
2164 if (( volume->v_flags & AFPVOL_OPEN ) ) {
2165 /* the volume is already open */
2167 set_uidgid ( volume );
2169 return stat_vol(bitmap, volume, rbuf, rbuflen);
2172 if (volume->v_root_preexec) {
2173 if ((ret = afprun(1, volume->v_root_preexec, NULL)) && volume->v_root_preexec_close) {
2174 LOG(log_error, logtype_afpd, "afp_openvol(%s): root preexec : %d", volume->v_path, ret );
2180 set_uidgid ( volume );
2183 if (volume->v_preexec) {
2184 if ((ret = afprun(0, volume->v_preexec, NULL)) && volume->v_preexec_close) {
2185 LOG(log_error, logtype_afpd, "afp_openvol(%s): preexec : %d", volume->v_path, ret );
2190 if ( stat( volume->v_path, &st ) < 0 ) {
2191 return AFPERR_PARAM;
2194 if ( chdir( volume->v_path ) < 0 ) {
2195 return AFPERR_PARAM;
2198 if ( NULL == getcwd(path, MAXPATHLEN)) {
2199 /* shouldn't be fatal but it will fail later */
2200 LOG(log_error, logtype_afpd, "afp_openvol(%s): volume pathlen too long", volume->v_path);
2204 /* Normalize volume path */
2205 #ifdef REALPATH_TAKES_NULL
2206 if ((volume->v_path = realpath(path, NULL)) == NULL)
2209 if ((volume->v_path = malloc(MAXPATHLEN+1)) == NULL)
2211 if (realpath(path, volume->v_path) == NULL) {
2212 free(volume->v_path);
2215 /* Safe some memory */
2217 if ((tmp = strdup(volume->v_path)) == NULL) {
2218 free(volume->v_path);
2221 free(volume->v_path);
2222 volume->v_path = tmp;
2225 if (volume_codepage(obj, volume) < 0) {
2230 /* initialize volume variables
2233 if (utf8_encoding()) {
2234 volume->max_filename = UTF8FILELEN_EARLY;
2237 volume->max_filename = MACFILELEN;
2240 volume->v_flags |= AFPVOL_OPEN;
2241 volume->v_cdb = NULL;
2243 if (utf8_encoding()) {
2244 len = convert_string_allocate(CH_UCS2, CH_UTF8_MAC, volume->v_u8mname, namelen, &vol_mname);
2246 len = convert_string_allocate(CH_UCS2, obj->options.maccharset, volume->v_macname, namelen, &vol_mname);
2248 if ( !vol_mname || len <= 0) {
2253 if ((vol_uname = strrchr(path, '/')) == NULL)
2255 else if (*(vol_uname + 1) != '\0')
2258 if ((dir = dir_new(vol_mname,
2263 bfromcstr(volume->v_path),
2267 LOG(log_error, logtype_afpd, "afp_openvol(%s): malloc: %s", volume->v_path, strerror(errno) );
2272 volume->v_root = dir;
2275 if (volume_openDB(volume) < 0) {
2276 LOG(log_error, logtype_afpd, "Fatal error: cannot open CNID or invalid CNID backend for %s: %s",
2277 volume->v_path, volume->v_cnidscheme);
2282 ret = stat_vol(bitmap, volume, rbuf, rbuflen);
2283 if (ret == AFP_OK) {
2285 if (!(volume->v_flags & AFPVOL_RO)) {
2286 handle_special_folders( volume );
2288 volume->v_cnidserver ? volume->v_cnidserver : Cnid_srv,
2289 volume->v_cnidport ? volume->v_cnidport : Cnid_port);
2293 * If you mount a volume twice, the second time the trash appears on
2294 * the desk-top. That's because the Mac remembers the DID for the
2295 * trash (even for volumes in different zones, on different servers).
2296 * Just so this works better, we prime the DID cache with the trash,
2297 * fixing the trash at DID 17.
2298 * FIXME (RL): should it be done inside a CNID backend ? (always returning Trash DID when asked) ?
2300 if ((volume->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
2302 /* FIXME find db time stamp */
2303 if (cnid_getstamp(volume->v_cdb, volume->v_stamp, sizeof(volume->v_stamp)) < 0) {
2304 LOG (log_error, logtype_afpd,
2305 "afp_openvol(%s): Fatal error: Unable to get stamp value from CNID backend",
2313 cname( volume, volume->v_root, &p );
2319 if (volume->v_root) {
2320 dir_free( volume->v_root );
2321 volume->v_root = NULL;
2324 volume->v_flags &= ~AFPVOL_OPEN;
2325 if (volume->v_cdb != NULL) {
2326 cnid_close(volume->v_cdb);
2327 volume->v_cdb = NULL;
2333 /* ------------------------- */
2334 static void closevol(struct vol *vol)
2339 dir_free( vol->v_root );
2341 if (vol->v_cdb != NULL) {
2342 cnid_close(vol->v_cdb);
2346 if (vol->v_postexec) {
2347 afprun(0, vol->v_postexec, NULL);
2349 if (vol->v_root_postexec) {
2350 afprun(1, vol->v_root_postexec, NULL);
2354 /* ------------------------- */
2355 void close_all_vol(void)
2359 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
2360 if ( (ovol->v_flags & AFPVOL_OPEN) ) {
2361 ovol->v_flags &= ~AFPVOL_OPEN;
2367 /* ------------------------- */
2368 static void deletevol(struct vol *vol)
2372 vol->v_flags &= ~AFPVOL_OPEN;
2373 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
2374 if ( (ovol->v_flags & AFPVOL_OPEN) ) {
2378 if ( ovol != NULL ) {
2379 /* Even if chdir fails, we can't say afp_closevol fails. */
2380 if ( chdir( ovol->v_path ) == 0 ) {
2381 curdir = ovol->v_root;
2386 if (vol->v_deleted) {
2387 showvol(vol->v_name);
2394 /* ------------------------- */
2395 int afp_closevol(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2402 memcpy(&vid, ibuf, sizeof( vid ));
2403 if (NULL == ( vol = getvolbyvid( vid )) ) {
2404 return( AFPERR_PARAM );
2413 /* ------------------------- */
2414 struct vol *getvolbyvid(const u_int16_t vid )
2418 for ( vol = Volumes; vol; vol = vol->v_next ) {
2419 if ( vid == vol->v_vid ) {
2423 if ( vol == NULL || ( vol->v_flags & AFPVOL_OPEN ) == 0 ) {
2429 #endif /* FORCE_UIDGID */
2436 /* ------------------------ */
2437 static int ext_cmp_key(const void *key, const void *obj)
2439 const char *p = key;
2440 const struct extmap *em = obj;
2441 return strdiacasecmp(p, em->em_ext);
2443 struct extmap *getextmap(const char *path)
2448 if (!Extmap_cnt || NULL == ( p = strrchr( path, '.' )) ) {
2449 return( Defextmap );
2453 return( Defextmap );
2455 em = bsearch(p, Extmap, Extmap_cnt, sizeof(struct extmap), ext_cmp_key);
2459 return( Defextmap );
2463 /* ------------------------- */
2464 struct extmap *getdefextmap(void)
2466 return( Defextmap );
2469 /* --------------------------
2470 poll if a volume is changed by other processes.
2472 0 no attention msg sent
2473 1 attention msg sent
2474 -1 error (socket closed)
2476 Note: if attention return -1 no packet has been
2477 sent because the buffer is full, we don't care
2478 either there's no reader or there's a lot of
2479 traffic and another pollvoltime will follow
2481 int pollvoltime(AFPObj *obj)
2488 if (!(afp_version > 21 && obj->options.server_notif))
2491 if ( gettimeofday( &tv, NULL ) < 0 )
2494 for ( vol = Volumes; vol; vol = vol->v_next ) {
2495 if ( (vol->v_flags & AFPVOL_OPEN) && vol->v_mtime + 30 < tv.tv_sec) {
2496 if ( !stat( vol->v_path, &st ) && vol->v_mtime != st.st_mtime ) {
2497 vol->v_mtime = st.st_mtime;
2498 if (!obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED))
2507 /* ------------------------- */
2508 void setvoltime(AFPObj *obj, struct vol *vol)
2512 if ( gettimeofday( &tv, NULL ) < 0 ) {
2513 LOG(log_error, logtype_afpd, "setvoltime(%s): gettimeofday: %s", vol->v_path, strerror(errno) );
2516 if( utime( vol->v_path, NULL ) < 0 ) {
2517 /* write of time failed ... probably a read only filesys,
2518 * where no other users can interfere, so there's no issue here
2522 /* a little granularity */
2523 if (vol->v_mtime < tv.tv_sec) {
2524 vol->v_mtime = tv.tv_sec;
2525 /* or finder doesn't update free space
2526 * AFP 3.2 and above clients seem to be ok without so many notification
2528 if (afp_version < 32 && obj->options.server_notif) {
2529 obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED);
2534 /* ------------------------- */
2535 int afp_getvolparams(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_,char *rbuf, size_t *rbuflen)
2538 u_int16_t vid, bitmap;
2541 memcpy(&vid, ibuf, sizeof( vid ));
2542 ibuf += sizeof( vid );
2543 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2544 bitmap = ntohs( bitmap );
2546 if (NULL == ( vol = getvolbyvid( vid )) ) {
2548 return( AFPERR_PARAM );
2551 return stat_vol(bitmap, vol, rbuf, rbuflen);
2554 /* ------------------------- */
2555 int afp_setvolparams(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2559 u_int16_t vid, bitmap;
2565 memcpy(&vid, ibuf, sizeof( vid ));
2566 ibuf += sizeof( vid );
2567 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2568 bitmap = ntohs( bitmap );
2569 ibuf += sizeof(bitmap);
2571 if (( vol = getvolbyvid( vid )) == NULL ) {
2572 return( AFPERR_PARAM );
2575 if ((vol->v_flags & AFPVOL_RO))
2576 return AFPERR_VLOCK;
2578 /* we can only set the backup date. */
2579 if (bitmap != (1 << VOLPBIT_BDATE))
2580 return AFPERR_BITMAP;
2582 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2583 if ( ad_open( vol->v_path, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR,
2586 return AFPERR_VLOCK;
2588 return AFPERR_ACCESS;
2591 memcpy(&aint, ibuf, sizeof(aint));
2592 ad_setdate(&ad, AD_DATE_BACKUP, aint);
2594 ad_close(&ad, ADFLAGS_HF);
2598 /* ------------------------- */
2599 int wincheck(const struct vol *vol, const char *path)
2603 if (!(vol->v_flags & AFPVOL_MSWINDOWS))
2606 /* empty paths are not allowed */
2607 if ((len = strlen(path)) == 0)
2610 /* leading or trailing whitespaces are not allowed, carriage returns
2611 * and probably other whitespace is okay, tabs are not allowed
2613 if ((path[0] == ' ') || (path[len-1] == ' '))
2616 /* certain characters are not allowed */
2617 if (strpbrk(path, MSWINDOWS_BADCHARS))
2620 /* everything else is okay */
2626 * precreate a folder
2627 * this is only intended for folders in the volume root
2628 * It will *not* work if the folder name contains extended characters
2630 static int create_special_folder (const struct vol *vol, const struct _special_folder *folder)
2639 p = (char *) malloc ( strlen(vol->v_path)+strlen(folder->name)+2);
2641 LOG(log_error, logtype_afpd,"malloc failed");
2645 q=strdup(folder->name);
2647 LOG(log_error, logtype_afpd,"malloc failed");
2651 strcpy(p, vol->v_path);
2656 if ((vol->v_casefold & AFPVOL_MTOUUPPER))
2658 else if ((vol->v_casefold & AFPVOL_MTOULOWER))
2664 if ( (ret = stat( p, &st )) < 0 ) {
2665 if (folder->precreate) {
2666 if (ad_mkdir(p, folder->mode)) {
2667 LOG(log_debug, logtype_afpd,"Creating '%s' failed in %s: %s", p, vol->v_path, strerror(errno));
2676 if ( !ret && folder->hide) {
2678 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2679 if (ad_open_metadata( p, ADFLAGS_DIR, O_CREAT, &ad) < 0) {
2685 ad_setname(&ad, folder->name);
2687 ad_getattr(&ad, &attr);
2688 attr |= htons( ntohs( attr ) | ATTRBIT_INVISIBLE );
2689 ad_setattr(&ad, attr);
2691 /* do the same with the finder info */
2692 if (ad_entry(&ad, ADEID_FINDERI)) {
2693 memcpy(&attr, ad_entry(&ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF, sizeof(attr));
2694 attr |= htons(FINDERINFO_INVISIBLE);
2695 memcpy(ad_entry(&ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF,&attr, sizeof(attr));
2699 ad_close_metadata( &ad);
2706 static void handle_special_folders (const struct vol * vol)
2708 const _special_folder *p = &special_folders[0];
2710 if ((vol->v_flags & AFPVOL_RO))
2713 for (; p->name != NULL; p++) {
2714 create_special_folder (vol, p);
2718 const struct vol *getvolumes(void)
2723 void unload_volumes_and_extmap(void)
2725 LOG(log_debug, logtype_afpd, "unload_volumes_and_extmap");
2731 * Get a volumes UUID from the config file.
2732 * If there is none, it is generated and stored there.
2734 * Returns pointer to allocated storage on success, NULL on error.
2736 static char *get_vol_uuid(const AFPObj *obj, const char *volname)
2739 char buf[1024], uuid[UUID_PRINTABLE_STRING_LENGTH], *p;
2741 struct stat tmpstat;
2744 if ((fp = fopen(obj->options.uuidconf, "r")) != NULL) { /* read open? */
2745 /* scan in the conf file */
2746 while (fgets(buf, sizeof(buf), fp) != NULL) {
2748 while (p && isblank(*p))
2750 if (!p || (*p == '#') || (*p == '\n'))
2751 continue; /* invalid line */
2754 if ((volname_conf = strtok( p, "\"" )) == NULL)
2755 continue; /* syntax error */
2757 if ((volname_conf = strtok( p, " \t" )) == NULL)
2758 continue; /* syntax error: invalid name */
2760 p = strchr(p, '\0');
2763 continue; /* syntax error */
2765 if (strcmp(volname, volname_conf) != 0)
2766 continue; /* another volume name */
2768 while (p && isblank(*p))
2771 if (sscanf(p, "%36s", uuid) == 1 ) {
2772 for (int i=0; uuid[i]; i++)
2773 uuid[i] = toupper(uuid[i]);
2774 LOG(log_debug, logtype_afpd, "get_uuid('%s'): UUID: '%s'", volname, uuid);
2776 return strdup(uuid);
2784 /* not found or no file, reopen in append mode */
2786 if (stat(obj->options.uuidconf, &tmpstat)) { /* no file */
2787 if (( fd = creat(obj->options.uuidconf, 0644 )) < 0 ) {
2788 LOG(log_error, logtype_afpd, "ERROR: Cannot create %s (%s).",
2789 obj->options.uuidconf, strerror(errno));
2792 if (( fp = fdopen( fd, "w" )) == NULL ) {
2793 LOG(log_error, logtype_afpd, "ERROR: Cannot fdopen %s (%s).",
2794 obj->options.uuidconf, strerror(errno));
2798 } else if ((fp = fopen(obj->options.uuidconf, "a+")) == NULL) { /* not found */
2799 LOG(log_error, logtype_afpd, "Cannot create or append to %s (%s).",
2800 obj->options.uuidconf, strerror(errno));
2803 fseek(fp, 0L, SEEK_END);
2804 if(ftell(fp) == 0) { /* size = 0 */
2805 fprintf(fp, "# DON'T TOUCH NOR COPY THOUGHTLESSLY!\n");
2806 fprintf(fp, "# This file is auto-generated by afpd\n");
2807 fprintf(fp, "# and stores UUIDs for TM volumes.\n\n");
2809 fseek(fp, -1L, SEEK_END);
2810 if(fgetc(fp) != '\n') fputc('\n', fp); /* last char is \n? */
2813 /* generate uuid and write to file */
2816 randombytes((void *)id, 16);
2817 cp = uuid_bin2string(id);
2819 LOG(log_debug, logtype_afpd, "get_uuid('%s'): generated UUID '%s'", volname, cp);
2821 fprintf(fp, "\"%s\"\t%36s\n", volname, cp);