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>
23 #include <atalk/dsi.h>
24 #include <atalk/adouble.h>
25 #include <atalk/afp.h>
26 #include <atalk/util.h>
27 #include <atalk/volinfo.h>
28 #include <atalk/logger.h>
29 #include <atalk/vfs.h>
30 #include <atalk/uuid.h>
32 #include <atalk/bstrlib.h>
33 #include <atalk/bstradd.h>
36 #include <atalk/cnid.h>
40 #include "directory.h"
49 extern int afprun(int root, char *cmd, int *outfd);
52 #define MIN(a, b) ((a) < (b) ? (a) : (b))
55 #ifndef NO_LARGE_VOL_SUPPORT
56 #if BYTE_ORDER == BIG_ENDIAN
59 #else /* BYTE_ORDER == BIG_ENDIAN */
60 #define hton64(x) ((u_int64_t) (htonl(((x) >> 32) & 0xffffffffLL)) | \
61 (u_int64_t) ((htonl(x) & 0xffffffffLL) << 32))
62 #define ntoh64(x) (hton64(x))
63 #endif /* BYTE_ORDER == BIG_ENDIAN */
64 #endif /* ! NO_LARGE_VOL_SUPPORT */
66 #ifndef UUID_PRINTABLE_STRING_LENGTH
67 #define UUID_PRINTABLE_STRING_LENGTH 37
71 struct vol *current_vol; /* last volume from getvolbyvid() */
73 static struct vol *Volumes = NULL;
74 static u_int16_t lastvid = 0;
75 static char *Trash = "\02\024Network Trash Folder";
77 static struct extmap *Extmap = NULL, *Defextmap = NULL;
78 static int Extmap_cnt;
79 static void free_extmap(void);
81 #define VOLOPT_ALLOW 0 /* user allow list */
82 #define VOLOPT_DENY 1 /* user deny list */
83 #define VOLOPT_RWLIST 2 /* user rw list */
84 #define VOLOPT_ROLIST 3 /* user ro list */
85 #define VOLOPT_PASSWORD 4 /* volume password */
86 #define VOLOPT_CASEFOLD 5 /* character case mangling */
87 #define VOLOPT_FLAGS 6 /* various flags */
88 #define VOLOPT_DBPATH 7 /* path to database */
89 #define VOLOPT_LIMITSIZE 8 /* Limit the size of the volume */
91 #define VOLOPT_VETO 10 /* list of veto filespec */
92 #define VOLOPT_PREEXEC 11 /* preexec command */
93 #define VOLOPT_ROOTPREEXEC 12 /* root preexec command */
94 #define VOLOPT_POSTEXEC 13 /* postexec command */
95 #define VOLOPT_ROOTPOSTEXEC 14 /* root postexec command */
96 #define VOLOPT_ENCODING 15 /* mac encoding (pre OSX)*/
97 #define VOLOPT_MACCHARSET 16
98 #define VOLOPT_CNIDSCHEME 17
99 #define VOLOPT_ADOUBLE 18 /* adouble version */
100 /* Usable slot: 19/20 */
101 #define VOLOPT_UMASK 21
102 #define VOLOPT_ALLOWED_HOSTS 22
103 #define VOLOPT_DENIED_HOSTS 23
104 #define VOLOPT_DPERM 24 /* dperm default directories perms */
105 #define VOLOPT_FPERM 25 /* fperm default files perms */
106 #define VOLOPT_DFLTPERM 26 /* perm */
107 #define VOLOPT_EA_VFS 27 /* Extended Attributes vfs indirection */
108 #define VOLOPT_CNIDSERVER 28 /* CNID Server ip address*/
109 #define VOLOPT_CNIDPORT 30 /* CNID server tcp port */
111 #define VOLOPT_MAX 31 /* <== IMPORTANT !!!!!! */
112 #define VOLOPT_NUM (VOLOPT_MAX + 1)
115 #define VOLOPT_DEFAULT ":DEFAULT:"
116 #define VOLOPT_DEFAULT_LEN 9
123 typedef struct _special_folder {
130 static const _special_folder special_folders[] = {
131 {"Network Trash Folder", 1, 0777, 1},
132 {".AppleDesktop", 1, 0777, 0},
135 /* Forward declarations */
136 static void handle_special_folders (const struct vol *);
137 static void deletevol(struct vol *vol);
138 static void volume_free(struct vol *vol);
139 static void check_ea_sys_support(struct vol *vol);
140 static char *get_vol_uuid(const AFPObj *obj, const char *volname);
141 static int readvolfile(AFPObj *obj, struct afp_volume_name *p1,char *p2, int user, struct passwd *pwent);
143 static void volfree(struct vol_option *options, const struct vol_option *save)
148 for (i = 0; i < VOLOPT_MAX; i++) {
149 if (options[i].c_value && (options[i].c_value != save[i].c_value))
150 free(options[i].c_value);
153 for (i = 0; i < VOLOPT_MAX; i++) {
154 if (options[i].c_value)
155 free(options[i].c_value);
161 #define is_var(a, b) (strncmp((a), (b), 2) == 0)
164 * Handle variable substitutions. here's what we understand:
165 * $b -> basename of path
166 * $c -> client ip/appletalk address
167 * $d -> volume pathname on server
168 * $f -> full name (whatever's in the gecos field)
171 * $i -> client ip/appletalk address without port
172 * $s -> server name (hostname if it doesn't exist)
173 * $u -> username (guest is usually nobody)
174 * $v -> volume name or basename if null
175 * $z -> zone (may not exist)
178 * This get's called from readvolfile with
179 * path = NULL, volname = NULL for xlating the volumes path
180 * path = path, volname = NULL for xlating the volumes name
181 * ... and from volumes options parsing code when xlating eg dbpath with
182 * path = path, volname = volname
184 * Using this information we can reject xlation of any variable depeninding on a login
185 * context which is not given in the afp master, where we must evaluate this whole stuff
186 * too for the Zeroconf announcements.
188 static char *volxlate(AFPObj *obj,
201 int xlatevolname = 0;
203 if (parent_or_child == 0)
206 if (path && !volname)
214 dest = calloc(destlen +1, 1);
220 strlcpy(dest, src, destlen +1);
221 if ((p = strchr(src, '$')) == NULL) /* nothing to do */
224 /* first part of the path. just forward to the next variable. */
225 len = MIN((size_t)(p - src), destlen);
231 while (p && destlen > 0) {
232 /* now figure out what the variable is */
234 if (is_var(p, "$b")) {
235 if (afpmaster && xlatevolname)
238 if ((q = strrchr(path, '/')) == NULL)
240 else if (*(q + 1) != '\0')
243 } else if (is_var(p, "$c")) {
244 if (afpmaster && xlatevolname)
246 DSI *dsi = obj->handle;
247 len = sprintf(dest, "%s:%u",
248 getip_string((struct sockaddr *)&dsi->client),
249 getip_port((struct sockaddr *)&dsi->client));
252 } else if (is_var(p, "$d")) {
253 if (afpmaster && xlatevolname)
256 } else if (pwd && is_var(p, "$f")) {
257 if (afpmaster && xlatevolname)
259 if ((r = strchr(pwd->pw_gecos, ',')))
262 } else if (pwd && is_var(p, "$g")) {
263 if (afpmaster && xlatevolname)
265 struct group *grp = getgrgid(pwd->pw_gid);
268 } else if (is_var(p, "$h")) {
269 q = obj->options.hostname;
270 } else if (is_var(p, "$i")) {
271 if (afpmaster && xlatevolname)
273 DSI *dsi = obj->handle;
274 q = getip_string((struct sockaddr *)&dsi->client);
275 } else if (is_var(p, "$s")) {
278 else if (obj->options.server) {
279 q = obj->options.server;
281 q = obj->options.hostname;
282 } else if (obj->username && is_var(p, "$u")) {
283 if (afpmaster && xlatevolname)
286 if ( obj->options.ntseparator && (sep = strchr(obj->username, obj->options.ntseparator[0])) != NULL)
290 } else if (is_var(p, "$v")) {
291 if (afpmaster && xlatevolname)
297 if ((q = strrchr(path, '/')) == NULL)
299 else if (*(q + 1) != '\0')
302 } else if (is_var(p, "$z")) {
304 } else if (is_var(p, "$$")) {
309 /* copy the stuff over. if we don't understand something that we
310 * should, just skip it over. */
312 len = MIN(p == q ? 2 : strlen(q), destlen);
313 strncpy(dest, q, len);
318 /* stuff up to next $ */
320 p = strchr(src, '$');
321 len = p ? MIN((size_t)(p - src), destlen) : destlen;
323 strncpy(dest, src, len);
331 /* to make sure that val is valid, make sure to select an opt that
333 static int optionok(const char *buf, const char *opt, const char *val)
335 if (!strstr(buf,opt))
343 /* -------------------- */
344 static void setoption(struct vol_option *options, struct vol_option *save, int opt, const char *val)
346 if (options[opt].c_value && (!save || options[opt].c_value != save[opt].c_value))
347 free(options[opt].c_value);
348 options[opt].c_value = strdup(val + 1);
351 /* ------------------------------------------
352 handle all the options. tmp can't be NULL. */
353 static void volset(struct vol_option *options, struct vol_option *save,
354 char *volname, int vlen,
359 val = strchr(tmp, ':');
361 /* we'll assume it's a volume name. */
362 strncpy(volname, tmp, vlen);
367 LOG(log_debug, logtype_afpd, "Parsing volset %s", val);
369 if (optionok(tmp, "allow:", val)) {
370 setoption(options, save, VOLOPT_ALLOW, val);
372 } else if (optionok(tmp, "deny:", val)) {
373 setoption(options, save, VOLOPT_DENY, val);
375 } else if (optionok(tmp, "rwlist:", val)) {
376 setoption(options, save, VOLOPT_RWLIST, val);
378 } else if (optionok(tmp, "rolist:", val)) {
379 setoption(options, save, VOLOPT_ROLIST, val);
381 } else if (optionok(tmp, "codepage:", val)) {
382 LOG (log_error, logtype_afpd, "The old codepage system has been removed. Please make sure to read the documentation !!!!");
383 /* Make sure we don't screw anything */
385 } else if (optionok(tmp, "volcharset:", val)) {
386 setoption(options, save, VOLOPT_ENCODING, val);
387 } else if (optionok(tmp, "maccharset:", val)) {
388 setoption(options, save, VOLOPT_MACCHARSET, val);
389 } else if (optionok(tmp, "veto:", val)) {
390 setoption(options, save, VOLOPT_VETO, val);
391 } else if (optionok(tmp, "cnidscheme:", val)) {
392 setoption(options, save, VOLOPT_CNIDSCHEME, val);
393 } else if (optionok(tmp, "casefold:", val)) {
394 if (strcasecmp(val + 1, "tolower") == 0)
395 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMLOWER;
396 else if (strcasecmp(val + 1, "toupper") == 0)
397 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMUPPER;
398 else if (strcasecmp(val + 1, "xlatelower") == 0)
399 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UUPPERMLOWER;
400 else if (strcasecmp(val + 1, "xlateupper") == 0)
401 options[VOLOPT_CASEFOLD].i_value = AFPVOL_ULOWERMUPPER;
402 } else if (optionok(tmp, "adouble:", val)) {
403 if (strcasecmp(val + 1, "v2") == 0)
404 options[VOLOPT_ADOUBLE].i_value = AD_VERSION2;
405 else if (strcasecmp(val + 1, "ea") == 0)
406 options[VOLOPT_ADOUBLE].i_value = AD_VERSION_EA;
407 } else if (optionok(tmp, "options:", val)) {
410 if ((p = strtok(val + 1, ",")) == NULL) /* nothing */
414 if (strcasecmp(p, "prodos") == 0)
415 options[VOLOPT_FLAGS].i_value |= AFPVOL_A2VOL;
416 else if (strcasecmp(p, "mswindows") == 0) {
417 options[VOLOPT_FLAGS].i_value |= AFPVOL_MSWINDOWS | AFPVOL_USEDOTS;
418 } else if (strcasecmp(p, "crlf") == 0)
419 options[VOLOPT_FLAGS].i_value |= AFPVOL_CRLF;
420 else if (strcasecmp(p, "noadouble") == 0)
421 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOADOUBLE;
422 else if (strcasecmp(p, "ro") == 0)
423 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
424 else if (strcasecmp(p, "nohex") == 0)
425 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOHEX;
426 else if (strcasecmp(p, "usedots") == 0)
427 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS;
428 else if (strcasecmp(p, "invisibledots") == 0)
429 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS | AFPVOL_INV_DOTS;
430 else if (strcasecmp(p, "limitsize") == 0)
431 options[VOLOPT_FLAGS].i_value |= AFPVOL_LIMITSIZE;
432 else if (strcasecmp(p, "nofileid") == 0)
433 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOFILEID;
434 else if (strcasecmp(p, "nostat") == 0)
435 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOSTAT;
436 else if (strcasecmp(p, "preexec_close") == 0)
437 options[VOLOPT_PREEXEC].i_value = 1;
438 else if (strcasecmp(p, "root_preexec_close") == 0)
439 options[VOLOPT_ROOTPREEXEC].i_value = 1;
440 else if (strcasecmp(p, "upriv") == 0)
441 options[VOLOPT_FLAGS].i_value |= AFPVOL_UNIX_PRIV;
442 else if (strcasecmp(p, "nodev") == 0)
443 options[VOLOPT_FLAGS].i_value |= AFPVOL_NODEV;
444 else if (strcasecmp(p, "caseinsensitive") == 0)
445 options[VOLOPT_FLAGS].i_value |= AFPVOL_CASEINSEN;
446 else if (strcasecmp(p, "illegalseq") == 0)
447 options[VOLOPT_FLAGS].i_value |= AFPVOL_EILSEQ;
448 else if (strcasecmp(p, "nocnidcache") == 0)
449 options[VOLOPT_FLAGS].i_value &= ~AFPVOL_CACHE;
450 else if (strcasecmp(p, "tm") == 0)
451 options[VOLOPT_FLAGS].i_value |= AFPVOL_TM;
452 else if (strcasecmp(p, "searchdb") == 0)
453 options[VOLOPT_FLAGS].i_value |= AFPVOL_SEARCHDB;
454 /* Found this in branch dir-rewrite, maybe we want to use it sometimes */
456 else if (strcasecmp(p, "cdrom") == 0)
457 options[VOLOPT_FLAGS].i_value |= AFPVOL_CDROM | AFPVOL_RO;
459 p = strtok(NULL, ",");
462 } else if (optionok(tmp, "cnidserver:", val)) {
463 setoption(options, save, VOLOPT_CNIDSERVER, val);
465 char *p = strrchr(val + 1, ':');
468 setoption(options, save, VOLOPT_CNIDPORT, p);
471 LOG(log_debug, logtype_afpd, "CNID Server for volume '%s': %s:%s",
472 volname, val + 1, p ? p + 1 : Cnid_port);
474 } else if (optionok(tmp, "dbpath:", val)) {
475 setoption(options, save, VOLOPT_DBPATH, val);
477 } else if (optionok(tmp, "umask:", val)) {
478 options[VOLOPT_UMASK].i_value = (int)strtol(val +1, NULL, 8);
479 } else if (optionok(tmp, "dperm:", val)) {
480 options[VOLOPT_DPERM].i_value = (int)strtol(val+1, NULL, 8);
481 } else if (optionok(tmp, "fperm:", val)) {
482 options[VOLOPT_FPERM].i_value = (int)strtol(val+1, NULL, 8);
483 } else if (optionok(tmp, "perm:", val)) {
484 options[VOLOPT_DFLTPERM].i_value = (int)strtol(val+1, NULL, 8);
485 } else if (optionok(tmp, "password:", val)) {
486 setoption(options, save, VOLOPT_PASSWORD, val);
487 } else if (optionok(tmp, "root_preexec:", val)) {
488 setoption(options, save, VOLOPT_ROOTPREEXEC, val);
490 } else if (optionok(tmp, "preexec:", val)) {
491 setoption(options, save, VOLOPT_PREEXEC, val);
493 } else if (optionok(tmp, "root_postexec:", val)) {
494 setoption(options, save, VOLOPT_ROOTPOSTEXEC, val);
496 } else if (optionok(tmp, "postexec:", val)) {
497 setoption(options, save, VOLOPT_POSTEXEC, val);
499 } else if (optionok(tmp, "allowed_hosts:", val)) {
500 setoption(options, save, VOLOPT_ALLOWED_HOSTS, val);
502 } else if (optionok(tmp, "denied_hosts:", val)) {
503 setoption(options, save, VOLOPT_DENIED_HOSTS, val);
505 } else if (optionok(tmp, "ea:", val)) {
506 if (strcasecmp(val + 1, "ad") == 0)
507 options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_AD;
508 else if (strcasecmp(val + 1, "sys") == 0)
509 options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_SYS;
510 else if (strcasecmp(val + 1, "none") == 0)
511 options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_NONE;
513 } else if (optionok(tmp, "volsizelimit:", val)) {
514 options[VOLOPT_LIMITSIZE].i_value = (uint32_t)strtoul(val + 1, NULL, 10);
517 /* ignore unknown options */
518 LOG(log_debug, logtype_afpd, "ignoring unknown volume option: %s", tmp);
523 /* ----------------- */
524 static void showvol(const ucs2_t *name)
527 for ( volume = Volumes; volume; volume = volume->v_next ) {
528 if (volume->v_hide && !strcasecmp_w( volume->v_name, name ) ) {
535 /* ------------------------------- */
536 static int creatvol(AFPObj *obj, struct passwd *pwd,
537 char *path, char *name,
538 struct vol_option *options,
539 const int user /* user defined volume */
543 int suffixlen, vlen, tmpvlen, u8mvlen, macvlen;
545 char tmpname[AFPVOL_U8MNAMELEN+1];
546 ucs2_t u8mtmpname[(AFPVOL_U8MNAMELEN+1)*2], mactmpname[(AFPVOL_MACNAMELEN+1)*2];
547 char suffix[6]; /* max is #FFFF */
550 LOG(log_debug, logtype_afpd, "createvol: Volume '%s'", name);
552 if ( name == NULL || *name == '\0' ) {
553 if ((name = strrchr( path, '/' )) == NULL) {
554 return -1; /* Obviously not a fully qualified path */
557 /* if you wish to share /, you need to specify a name. */
562 /* suffix for mangling use (lastvid + 1) */
563 /* because v_vid has not been decided yet. */
564 suffixlen = sprintf(suffix, "%c%X", MANGLE_CHAR, lastvid + 1 );
566 vlen = strlen( name );
568 /* Unicode Volume Name */
569 /* Firstly convert name from unixcharset to UTF8-MAC */
571 tmpvlen = convert_charset(obj->options.unixcharset, CH_UTF8_MAC, 0, name, vlen, tmpname, AFPVOL_U8MNAMELEN, &flags);
573 strcpy(tmpname, "???");
577 /* Do we have to mangle ? */
578 if ( (flags & CONV_REQMANGLE) || (tmpvlen > obj->options.volnamelen)) {
579 if (tmpvlen + suffixlen > obj->options.volnamelen) {
581 tmpvlen = convert_charset(obj->options.unixcharset, CH_UTF8_MAC, 0, name, vlen, tmpname, obj->options.volnamelen - suffixlen, &flags);
582 tmpname[tmpvlen >= 0 ? tmpvlen : 0] = 0;
584 strcat(tmpname, suffix);
585 tmpvlen = strlen(tmpname);
588 /* Secondly convert name from UTF8-MAC to UCS2 */
589 if ( 0 >= ( u8mvlen = convert_string(CH_UTF8_MAC, CH_UCS2, tmpname, tmpvlen, u8mtmpname, AFPVOL_U8MNAMELEN*2)) )
592 LOG(log_maxdebug, logtype_afpd, "createvol: Volume '%s' -> UTF8-MAC Name: '%s'", name, tmpname);
594 /* Maccharset Volume Name */
595 /* Firsty convert name from unixcharset to maccharset */
597 tmpvlen = convert_charset(obj->options.unixcharset, obj->options.maccharset, 0, name, vlen, tmpname, AFPVOL_U8MNAMELEN, &flags);
599 strcpy(tmpname, "???");
603 /* Do we have to mangle ? */
604 if ( (flags & CONV_REQMANGLE) || (tmpvlen > AFPVOL_MACNAMELEN)) {
605 if (tmpvlen + suffixlen > AFPVOL_MACNAMELEN) {
607 tmpvlen = convert_charset(obj->options.unixcharset, obj->options.maccharset, 0, name, vlen, tmpname, AFPVOL_MACNAMELEN - suffixlen, &flags);
608 tmpname[tmpvlen >= 0 ? tmpvlen : 0] = 0;
610 strcat(tmpname, suffix);
611 tmpvlen = strlen(tmpname);
614 /* Secondly convert name from maccharset to UCS2 */
615 if ( 0 >= ( macvlen = convert_string(obj->options.maccharset, CH_UCS2, tmpname, tmpvlen, mactmpname, AFPVOL_U8MNAMELEN*2)) )
618 LOG(log_maxdebug, logtype_afpd, "createvol: Volume '%s' -> Longname: '%s'", name, tmpname);
620 /* check duplicate */
621 for ( volume = Volumes; volume; volume = volume->v_next ) {
622 if (( strcasecmp_w( volume->v_u8mname, u8mtmpname ) == 0 ) || ( strcasecmp_w( volume->v_macname, mactmpname ) == 0 )){
623 LOG (log_error, logtype_afpd, "ERROR: Volume name is duplicated. Check AppleVolumes files.");
624 if (volume->v_deleted) {
625 volume->v_new = hide = 1;
628 return -1; /* Won't be able to access it, anyway... */
633 if (!( volume = (struct vol *)calloc(1, sizeof( struct vol ))) ) {
634 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
637 if ( NULL == ( volume->v_localname = strdup(name))) {
638 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
643 if ( NULL == ( volume->v_u8mname = strdup_w(u8mtmpname))) {
644 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
649 if ( NULL == ( volume->v_macname = strdup_w(mactmpname))) {
650 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
655 if (!( volume->v_path = (char *)malloc( strlen( path ) + 1 )) ) {
656 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
662 volume->v_name = utf8_encoding()?volume->v_u8mname:volume->v_macname;
663 volume->v_hide = hide;
664 strcpy( volume->v_path, path );
668 #endif /* __svr4__ */
669 /* os X start at 1 and use network order ie. 1 2 3 */
670 volume->v_vid = ++lastvid;
671 volume->v_vid = htons(volume->v_vid);
673 if (check_vol_acl_support(volume))
674 volume->v_flags |= AFPVOL_ACLS
680 volume->v_casefold = options[VOLOPT_CASEFOLD].i_value;
681 volume->v_flags |= options[VOLOPT_FLAGS].i_value;
683 if (options[VOLOPT_EA_VFS].i_value)
684 volume->v_vfs_ea = options[VOLOPT_EA_VFS].i_value;
686 volume->v_ad_options = 0;
687 if ((volume->v_flags & AFPVOL_NODEV))
688 volume->v_ad_options |= ADVOL_NODEV;
689 if ((volume->v_flags & AFPVOL_CACHE))
690 volume->v_ad_options |= ADVOL_CACHE;
691 if ((volume->v_flags & AFPVOL_UNIX_PRIV))
692 volume->v_ad_options |= ADVOL_UNIXPRIV;
693 if ((volume->v_flags & AFPVOL_INV_DOTS))
694 volume->v_ad_options |= ADVOL_INVDOTS;
695 if ((volume->v_flags & AFPVOL_NOADOUBLE))
696 volume->v_ad_options |= ADVOL_NOADOUBLE;
698 if (options[VOLOPT_PASSWORD].c_value)
699 volume->v_password = strdup(options[VOLOPT_PASSWORD].c_value);
701 if (options[VOLOPT_VETO].c_value)
702 volume->v_veto = strdup(options[VOLOPT_VETO].c_value);
704 if (options[VOLOPT_ENCODING].c_value)
705 volume->v_volcodepage = strdup(options[VOLOPT_ENCODING].c_value);
707 if (options[VOLOPT_MACCHARSET].c_value)
708 volume->v_maccodepage = strdup(options[VOLOPT_MACCHARSET].c_value);
710 if (options[VOLOPT_DBPATH].c_value)
711 volume->v_dbpath = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_DBPATH].c_value, pwd, path, name);
713 if (options[VOLOPT_CNIDSCHEME].c_value)
714 volume->v_cnidscheme = strdup(options[VOLOPT_CNIDSCHEME].c_value);
716 if (options[VOLOPT_CNIDSERVER].c_value)
717 volume->v_cnidserver = strdup(options[VOLOPT_CNIDSERVER].c_value);
719 if (options[VOLOPT_CNIDPORT].c_value)
720 volume->v_cnidport = strdup(options[VOLOPT_CNIDPORT].c_value);
722 if (options[VOLOPT_UMASK].i_value)
723 volume->v_umask = (mode_t)options[VOLOPT_UMASK].i_value;
725 if (options[VOLOPT_DPERM].i_value)
726 volume->v_dperm = (mode_t)options[VOLOPT_DPERM].i_value;
728 if (options[VOLOPT_FPERM].i_value)
729 volume->v_fperm = (mode_t)options[VOLOPT_FPERM].i_value;
731 if (options[VOLOPT_DFLTPERM].i_value)
732 volume->v_perm = (mode_t)options[VOLOPT_DFLTPERM].i_value;
734 if (options[VOLOPT_ADOUBLE].i_value)
735 volume->v_adouble = options[VOLOPT_ADOUBLE].i_value;
737 volume->v_adouble = AD_VERSION;
739 if (options[VOLOPT_LIMITSIZE].i_value)
740 volume->v_limitsize = options[VOLOPT_LIMITSIZE].i_value;
742 /* Mac to Unix conversion flags*/
743 volume->v_mtou_flags = 0;
744 if (!(volume->v_flags & AFPVOL_NOHEX))
745 volume->v_mtou_flags |= CONV_ESCAPEHEX;
746 if (!(volume->v_flags & AFPVOL_USEDOTS))
747 volume->v_mtou_flags |= CONV_ESCAPEDOTS;
748 if ((volume->v_flags & AFPVOL_EILSEQ))
749 volume->v_mtou_flags |= CONV__EILSEQ;
751 if ((volume->v_casefold & AFPVOL_MTOUUPPER))
752 volume->v_mtou_flags |= CONV_TOUPPER;
753 else if ((volume->v_casefold & AFPVOL_MTOULOWER))
754 volume->v_mtou_flags |= CONV_TOLOWER;
756 /* Unix to Mac conversion flags*/
757 volume->v_utom_flags = CONV_IGNORE | CONV_UNESCAPEHEX;
758 if ((volume->v_casefold & AFPVOL_UTOMUPPER))
759 volume->v_utom_flags |= CONV_TOUPPER;
760 else if ((volume->v_casefold & AFPVOL_UTOMLOWER))
761 volume->v_utom_flags |= CONV_TOLOWER;
763 if ((volume->v_flags & AFPVOL_EILSEQ))
764 volume->v_utom_flags |= CONV__EILSEQ;
767 if (options[VOLOPT_PREEXEC].c_value)
768 volume->v_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_PREEXEC].c_value, pwd, path, name);
769 volume->v_preexec_close = options[VOLOPT_PREEXEC].i_value;
771 if (options[VOLOPT_POSTEXEC].c_value)
772 volume->v_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_POSTEXEC].c_value, pwd, path, name);
774 if (options[VOLOPT_ROOTPREEXEC].c_value)
775 volume->v_root_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPREEXEC].c_value, pwd, path, name);
776 volume->v_root_preexec_close = options[VOLOPT_ROOTPREEXEC].i_value;
778 if (options[VOLOPT_ROOTPOSTEXEC].c_value)
779 volume->v_root_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPOSTEXEC].c_value, pwd, path, name);
782 volume->v_dperm |= volume->v_perm;
783 volume->v_fperm |= volume->v_perm;
785 /* Check EA support on volume */
786 if (volume->v_vfs_ea == AFPVOL_EA_AUTO)
787 check_ea_sys_support(volume);
790 /* get/store uuid from file */
791 if (volume->v_flags & AFPVOL_TM) {
792 char *uuid = get_vol_uuid(obj, volume->v_localname);
794 LOG(log_error, logtype_afpd, "Volume '%s': couldn't get UUID",
795 volume->v_localname);
797 volume->v_uuid = uuid;
798 LOG(log_debug, logtype_afpd, "Volume '%s': UUID '%s'",
799 volume->v_localname, volume->v_uuid);
803 volume->v_next = Volumes;
808 /* ---------------- */
809 static char *myfgets( char *buf, int size, FILE *fp)
815 while ((EOF != ( c = getc( fp )) ) && ( size > 1 )) {
816 if ( c == '\n' || c == '\r' ) {
817 if (p != buf && *(p -1) == '\\') {
839 /* check access list. this function wants something of the following
841 * @group,name,name2,@group2,name3
843 * a NULL argument allows everybody to have access.
844 * we return three things:
846 * 0: list exists, but name isn't in it
850 #ifndef NO_REAL_USER_NAME
851 /* authentication is case insensitive
852 * FIXME should we do the same with group name?
854 #define access_strcmp strcasecmp
857 #define access_strcmp strcmp
861 static int accessvol(const char *args, const char *name)
863 char buf[MAXPATHLEN + 1], *p;
869 strlcpy(buf, args, sizeof(buf));
870 if ((p = strtok(buf, ",")) == NULL) /* nothing, return okay */
874 if (*p == '@') { /* it's a group */
875 if ((gr = getgrnam(p + 1)) && gmem(gr->gr_gid))
877 } else if (access_strcmp(p, name) == 0) /* it's a user name */
879 p = strtok(NULL, ",");
885 static int hostaccessvol(int type, const char *volname, const char *args, const AFPObj *obj)
888 char buf[MAXPATHLEN + 1], *p, *b;
889 DSI *dsi = obj->handle;
890 struct sockaddr_storage client;
895 strlcpy(buf, args, sizeof(buf));
896 if ((p = strtok_r(buf, ",", &b)) == NULL) /* nothing, return okay */
899 if (obj->proto != AFPPROTO_DSI)
904 char *ipaddr, *mask_char;
905 struct addrinfo hints, *ai;
907 ipaddr = strtok(p, "/");
908 mask_char = strtok(NULL,"/");
910 /* Get address from string with getaddrinfo */
911 memset(&hints, 0, sizeof hints);
912 hints.ai_family = AF_UNSPEC;
913 hints.ai_socktype = SOCK_STREAM;
914 if ((ret = getaddrinfo(ipaddr, NULL, &hints, &ai)) != 0) {
915 LOG(log_error, logtype_afpd, "hostaccessvol: getaddrinfo: %s\n", gai_strerror(ret));
920 if (mask_char != NULL)
921 mask_int = atoi(mask_char); /* apply_ip_mask does range checking on it */
923 if (ai->ai_family == AF_INET) /* IPv4 */
929 /* Apply mask to addresses */
930 client = dsi->client;
931 apply_ip_mask((struct sockaddr *)&client, mask_int);
932 apply_ip_mask(ai->ai_addr, mask_int);
934 if (compare_ip((struct sockaddr *)&client, ai->ai_addr) == 0) {
935 if (type == VOLOPT_DENIED_HOSTS)
936 LOG(log_info, logtype_afpd, "AFP access denied for client IP '%s' to volume '%s' by denied list",
937 getip_string((struct sockaddr *)&client), volname);
944 p = strtok_r(NULL, ",", &b);
947 if (type == VOLOPT_ALLOWED_HOSTS)
948 LOG(log_info, logtype_afpd, "AFP access denied for client IP '%s' to volume '%s', not in allowed list",
949 getip_string((struct sockaddr *)&dsi->client), volname);
953 static void setextmap(char *ext, char *type, char *creator, int user)
958 if (Extmap == NULL) {
959 if (( Extmap = calloc(1, sizeof( struct extmap ))) == NULL ) {
960 LOG(log_error, logtype_afpd, "setextmap: calloc: %s", strerror(errno) );
965 for ( em = Extmap, cnt = 0; em->em_ext; em++, cnt++) {
966 if ( (strdiacasecmp( em->em_ext, ext )) == 0 ) {
971 if ( em->em_ext == NULL ) {
972 if (!(Extmap = realloc( Extmap, sizeof( struct extmap ) * (cnt +2))) ) {
973 LOG(log_error, logtype_afpd, "setextmap: realloc: %s", strerror(errno) );
976 (Extmap +cnt +1)->em_ext = NULL;
978 } else if ( !user ) {
984 if (!(em->em_ext = strdup( ext))) {
985 LOG(log_error, logtype_afpd, "setextmap: strdup: %s", strerror(errno) );
989 if ( *type == '\0' ) {
990 memcpy(em->em_type, "\0\0\0\0", sizeof( em->em_type ));
992 memcpy(em->em_type, type, sizeof( em->em_type ));
994 if ( *creator == '\0' ) {
995 memcpy(em->em_creator, "\0\0\0\0", sizeof( em->em_creator ));
997 memcpy(em->em_creator, creator, sizeof( em->em_creator ));
1001 /* -------------------------- */
1002 static int extmap_cmp(const void *map1, const void *map2)
1004 const struct extmap *em1 = map1;
1005 const struct extmap *em2 = map2;
1006 return strdiacasecmp(em1->em_ext, em2->em_ext);
1009 static void sortextmap( void)
1014 if ((em = Extmap) == NULL) {
1017 while (em->em_ext) {
1022 qsort(Extmap, Extmap_cnt, sizeof(struct extmap), extmap_cmp);
1023 if (*Extmap->em_ext == 0) {
1024 /* the first line is really "." the default entry,
1025 * we remove the leading '.' in setextmap
1032 /* ----------------------
1034 static void free_extmap( void)
1039 for ( em = Extmap; em->em_ext; em++) {
1049 /* ----------------------
1051 static int volfile_changed(struct afp_volume_name *p)
1057 name = p->full_name;
1061 if (!stat( name, &st) && st.st_mtime > p->mtime) {
1062 p->mtime = st.st_mtime;
1068 /* ----------------------
1069 * Read a volume configuration file and add the volumes contained within to
1070 * the global volume list. This gets called from the forked afpd childs.
1071 * The master now reads this too for Zeroconf announcements.
1073 * If p2 is non-NULL, the file that is opened is
1076 * Lines that begin with # and blank lines are ignored.
1077 * Volume lines are of the form:
1078 * <unix path> [<volume name>] [allow:<user>,<@group>,...] \
1079 * [codepage:<file>] [casefold:<num>]
1080 * <extension> TYPE [CREATOR]
1083 static int readvolfile(AFPObj *obj, struct afp_volume_name *p1, char *p2, int user, struct passwd *pwent)
1086 char path[MAXPATHLEN + 1];
1087 char tmp[MAXPATHLEN + 1];
1088 char volname[AFPVOL_U8MNAMELEN + 1];
1090 char type[5], creator[5];
1095 struct vol_option save_options[VOLOPT_NUM];
1096 struct vol_option options[VOLOPT_NUM];
1102 strcpy( path, p1->name );
1104 strcat( path, "/" );
1106 if (p1->full_name) {
1107 free(p1->full_name);
1109 p1->full_name = strdup(path);
1112 if (NULL == ( fp = fopen( path, "r" )) ) {
1116 if (fd != -1 && !fstat( fd, &st) ) {
1117 p1->mtime = st.st_mtime;
1120 if ((read_lock(fd, 0, SEEK_SET, 0)) != 0) {
1121 LOG(log_error, logtype_afpd, "readvolfile: can't lock volume file \"%s\"", path);
1122 if ( fclose( fp ) != 0 ) {
1123 LOG(log_error, logtype_afpd, "readvolfile: fclose: %s", strerror(errno) );
1128 memset(save_options, 0, sizeof(save_options));
1130 /* Enable some default options for all volumes */
1131 save_options[VOLOPT_FLAGS].i_value |= AFPVOL_CACHE;
1132 save_options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_AUTO;
1133 LOG(log_maxdebug, logtype_afpd, "readvolfile: seeding default umask: %04o",
1134 obj->options.umask);
1135 save_options[VOLOPT_UMASK].i_value = obj->options.umask;
1137 LOG(log_debug, logtype_afpd, "readvolfile: \"%s\"", path);
1139 while ( myfgets( buf, sizeof( buf ), fp ) != NULL ) {
1140 initline( strlen( buf ), buf );
1141 parseline( sizeof( path ) - 1, path );
1148 /* change the default options for this file */
1149 if (strncmp(path, VOLOPT_DEFAULT, VOLOPT_DEFAULT_LEN) == 0) {
1151 for (i = 0; i < VOLOPT_NUM; i++) {
1152 if (parseline( sizeof( path ) - VOLOPT_DEFAULT_LEN - 1,
1153 path + VOLOPT_DEFAULT_LEN) < 0)
1155 volset(save_options, NULL, tmp, sizeof(tmp) - 1,
1156 path + VOLOPT_DEFAULT_LEN);
1162 if (( p = strchr( path, '/' )) != NULL ) {
1170 if ( u == NULL || *u == '\0' || ( pw = getpwnam( u )) == NULL ) {
1173 strcpy( tmp, pw->pw_dir );
1174 if ( p != NULL && *p != '\0' ) {
1181 /* send path through variable substitution */
1182 if (*path != '~') /* need to copy path to tmp */
1184 if (!pwent && obj->username)
1185 pwent = getpwnam(obj->username);
1187 if (volxlate(obj, path, sizeof(path) - 1, tmp, pwent, NULL, NULL) == NULL)
1190 /* this is sort of braindead. basically, i want to be
1191 * able to specify things in any order, but i don't want to
1192 * re-write everything. */
1194 memcpy(options, save_options, sizeof(options));
1197 /* read in up to VOLOP_NUM possible options */
1198 for (i = 0; i < VOLOPT_NUM; i++) {
1199 if (parseline( sizeof( tmp ) - 1, tmp ) < 0)
1202 volset(options, save_options, volname, sizeof(volname) - 1, tmp);
1205 /* check allow/deny lists (if not afpd master loading volumes for Zeroconf reg.):
1206 allow -> either no list (-1), or in list (1)
1207 deny -> either no list (-1), or not in list (0) */
1208 if (parent_or_child == 0
1210 (accessvol(options[VOLOPT_ALLOW].c_value, obj->username) &&
1211 (accessvol(options[VOLOPT_DENY].c_value, obj->username) < 1) &&
1212 hostaccessvol(VOLOPT_ALLOWED_HOSTS, volname, options[VOLOPT_ALLOWED_HOSTS].c_value, obj) &&
1213 (hostaccessvol(VOLOPT_DENIED_HOSTS, volname, options[VOLOPT_DENIED_HOSTS].c_value, obj) < 1))) {
1215 /* handle read-only behaviour. semantics:
1216 * 1) neither the rolist nor the rwlist exist -> rw
1217 * 2) rolist exists -> ro if user is in it.
1218 * 3) rwlist exists -> ro unless user is in it. */
1219 if (parent_or_child == 1
1221 ((options[VOLOPT_FLAGS].i_value & AFPVOL_RO) == 0)
1223 ((accessvol(options[VOLOPT_ROLIST].c_value, obj->username) == 1) ||
1224 !accessvol(options[VOLOPT_RWLIST].c_value, obj->username)))
1225 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
1227 /* do variable substitution for volname */
1228 if (volxlate(obj, tmp, sizeof(tmp) - 1, volname, pwent, path, NULL) == NULL)
1231 creatvol(obj, pwent, path, tmp, options, p2 != NULL);
1233 volfree(options, save_options);
1237 parseline( sizeof( type ) - 1, type );
1238 parseline( sizeof( creator ) - 1, creator );
1239 setextmap( path, type, creator, user);
1246 volfree(save_options, NULL);
1248 if ( fclose( fp ) != 0 ) {
1249 LOG(log_error, logtype_afpd, "readvolfile: fclose: %s", strerror(errno) );
1255 /* ------------------------------- */
1256 static void volume_free(struct vol *vol)
1258 free(vol->v_localname);
1259 vol->v_localname = NULL;
1260 free(vol->v_u8mname);
1261 vol->v_u8mname = NULL;
1262 free(vol->v_macname);
1263 vol->v_macname = NULL;
1265 free(vol->v_password);
1267 free(vol->v_volcodepage);
1268 free(vol->v_maccodepage);
1269 free(vol->v_cnidscheme);
1270 free(vol->v_dbpath);
1276 /* ------------------------------- */
1277 static void free_volumes(void )
1280 struct vol *nvol, *ovol;
1282 for ( vol = Volumes; vol; vol = vol->v_next ) {
1283 if (( vol->v_flags & AFPVOL_OPEN ) ) {
1290 for ( vol = Volumes, ovol = NULL; vol; vol = nvol) {
1293 if (vol->v_localname == NULL) {
1294 if (Volumes == vol) {
1299 ovol->v_next = nvol;
1309 /* ------------------------------- */
1310 static void volume_unlink(struct vol *volume)
1312 struct vol *vol, *ovol, *nvol;
1314 if (volume == Volumes) {
1315 Volumes = Volumes->v_next;
1318 for ( vol = Volumes->v_next, ovol = Volumes; vol; vol = nvol) {
1321 if (vol == volume) {
1322 ovol->v_next = nvol;
1331 static int getvolspace(struct vol *vol,
1332 u_int32_t *bfree, u_int32_t *btotal,
1333 VolSpace *xbfree, VolSpace *xbtotal, u_int32_t *bsize)
1337 #ifndef NO_QUOTA_SUPPORT
1338 VolSpace qfree, qtotal;
1341 spaceflag = AFPVOL_GVSMASK & vol->v_flags;
1342 /* report up to 2GB if afp version is < 2.2 (4GB if not) */
1343 maxsize = (vol->v_flags & AFPVOL_A2VOL) ? 0x01fffe00 :
1344 (((afp_version < 22) || (vol->v_flags & AFPVOL_LIMITSIZE))
1345 ? 0x7fffffffL : 0xffffffffL);
1348 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_AFSGVS ) {
1349 if ( afs_getvolspace( vol, xbfree, xbtotal, bsize ) == AFP_OK ) {
1350 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_AFSGVS;
1351 goto getvolspace_done;
1356 if (( rc = ustatfs_getvolspace( vol, xbfree, xbtotal, bsize)) != AFP_OK ) {
1360 #define min(a,b) ((a)<(b)?(a):(b))
1361 #ifndef NO_QUOTA_SUPPORT
1362 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_UQUOTA ) {
1363 if ( uquota_getvolspace( vol, &qfree, &qtotal, *bsize ) == AFP_OK ) {
1364 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_UQUOTA;
1365 *xbfree = min(*xbfree, qfree);
1366 *xbtotal = min( *xbtotal, qtotal);
1367 goto getvolspace_done;
1371 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_USTATFS;
1374 if (vol->v_limitsize) {
1376 if ((cmdstr = bformat("du -sh \"%s\" 2> /dev/null | cut -f1", vol->v_path)) == NULL)
1379 FILE *cmd = popen(cfrombstr(cmdstr), "r");
1385 fgets(buf, 100, cmd);
1387 if (pclose(cmd) == -1)
1391 if (buf[strlen(buf) - 2] == 'G' || buf[strlen(buf) - 2] == 'g')
1393 multi = 1024 * 1024 * 1024;
1394 else if (buf[strlen(buf) - 2] == 'M' || buf[strlen(buf) - 2] == 'm')
1396 multi = 1024 * 1024;
1397 else if (buf[strlen(buf) - 2] == 'K' || buf[strlen(buf) - 2] == 'k')
1402 if (p = strchr(buf, ','))
1403 /* ignore fraction */
1406 /* remove G|M|K char */
1407 buf[strlen(buf) - 2] = 0;
1408 /* now buf contains only digits */
1409 long long used = atoll(buf) * multi;
1410 LOG(log_debug, logtype_afpd, "volparams: used on volume: %llu bytes", used);
1412 *xbtotal = min(*xbtotal, (vol->v_limitsize * 1024 * 1024));
1413 *xbfree = min(*xbfree, *xbtotal < used ? 0 : *xbtotal - used);
1416 *bfree = min( *xbfree, maxsize);
1417 *btotal = min( *xbtotal, maxsize);
1421 /* -----------------------
1422 * set volume creation date
1423 * avoid duplicate, well at least it tries
1425 static void vol_setdate(u_int16_t id, struct adouble *adp, time_t date)
1428 struct vol *vol = Volumes;
1430 for ( volume = Volumes; volume; volume = volume->v_next ) {
1431 if (volume->v_vid == id) {
1434 else if ((time_t)(AD_DATE_FROM_UNIX(date)) == volume->v_ctime) {
1436 volume = Volumes; /* restart */
1439 vol->v_ctime = AD_DATE_FROM_UNIX(date);
1440 ad_setdate(adp, AD_DATE_CREATE | AD_DATE_UNIX, date);
1443 /* ----------------------- */
1444 static int getvolparams( u_int16_t bitmap, struct vol *vol, struct stat *st, char *buf, size_t *buflen)
1447 int bit = 0, isad = 1;
1450 u_int32_t bfree, btotal, bsize;
1451 VolSpace xbfree, xbtotal; /* extended bytes */
1452 char *data, *nameoff = NULL;
1455 LOG(log_debug, logtype_afpd, "getvolparams: Volume '%s'", vol->v_localname);
1457 /* courtesy of jallison@whistle.com:
1458 * For MacOS8.x support we need to create the
1459 * .Parent file here if it doesn't exist. */
1461 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1462 if (ad_open(&ad, vol->v_path, ADFLAGS_HF | ADFLAGS_DIR, O_RDWR | O_CREAT, 0666) != 0 ) {
1464 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1466 } else if (ad_get_MD_flags( &ad ) & O_CREAT) {
1467 slash = strrchr( vol->v_path, '/' );
1471 slash = vol->v_path;
1472 if (ad_getentryoff(&ad, ADEID_NAME)) {
1473 ad_setentrylen( &ad, ADEID_NAME, strlen( slash ));
1474 memcpy(ad_entry( &ad, ADEID_NAME ), slash,
1475 ad_getentrylen( &ad, ADEID_NAME ));
1477 vol_setdate(vol->v_vid, &ad, st->st_mtime);
1481 if (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0)
1482 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1484 vol->v_ctime = aint;
1487 if (( bitmap & ( (1<<VOLPBIT_BFREE)|(1<<VOLPBIT_BTOTAL) |
1488 (1<<VOLPBIT_XBFREE)|(1<<VOLPBIT_XBTOTAL) |
1489 (1<<VOLPBIT_BSIZE)) ) != 0 ) {
1490 if ( getvolspace( vol, &bfree, &btotal, &xbfree, &xbtotal,
1491 &bsize) != AFP_OK ) {
1493 ad_close( &ad, ADFLAGS_HF );
1495 return( AFPERR_PARAM );
1500 while ( bitmap != 0 ) {
1501 while (( bitmap & 1 ) == 0 ) {
1509 /* check for read-only.
1510 * NOTE: we don't actually set the read-only flag unless
1511 * it's passed in that way as it's possible to mount
1512 * a read-write filesystem under a read-only one. */
1513 if ((vol->v_flags & AFPVOL_RO) ||
1514 ((utime(vol->v_path, NULL) < 0) && (errno == EROFS))) {
1515 ashort |= VOLPBIT_ATTR_RO;
1517 /* prior 2.1 only VOLPBIT_ATTR_RO is defined */
1518 if (afp_version > 20) {
1519 if (0 == (vol->v_flags & AFPVOL_NOFILEID) && vol->v_cdb != NULL &&
1520 (vol->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1521 ashort |= VOLPBIT_ATTR_FILEID;
1523 ashort |= VOLPBIT_ATTR_CATSEARCH;
1525 if (afp_version >= 30) {
1526 ashort |= VOLPBIT_ATTR_UTF8;
1527 if (vol->v_flags & AFPVOL_UNIX_PRIV)
1528 ashort |= VOLPBIT_ATTR_UNIXPRIV;
1529 if (vol->v_flags & AFPVOL_TM)
1530 ashort |= VOLPBIT_ATTR_TM;
1532 if (afp_version >= 32) {
1534 ashort |= VOLPBIT_ATTR_EXT_ATTRS;
1535 if (vol->v_flags & AFPVOL_ACLS)
1536 ashort |= VOLPBIT_ATTR_ACLS;
1540 ashort = htons(ashort);
1541 memcpy(data, &ashort, sizeof( ashort ));
1542 data += sizeof( ashort );
1546 ashort = htons( AFPVOLSIG_DEFAULT );
1547 memcpy(data, &ashort, sizeof( ashort ));
1548 data += sizeof( ashort );
1551 case VOLPBIT_CDATE :
1552 aint = vol->v_ctime;
1553 memcpy(data, &aint, sizeof( aint ));
1554 data += sizeof( aint );
1557 case VOLPBIT_MDATE :
1558 if ( st->st_mtime > vol->v_mtime ) {
1559 vol->v_mtime = st->st_mtime;
1561 aint = AD_DATE_FROM_UNIX(vol->v_mtime);
1562 memcpy(data, &aint, sizeof( aint ));
1563 data += sizeof( aint );
1566 case VOLPBIT_BDATE :
1567 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1568 aint = AD_DATE_START;
1569 memcpy(data, &aint, sizeof( aint ));
1570 data += sizeof( aint );
1574 memcpy(data, &vol->v_vid, sizeof( vol->v_vid ));
1575 data += sizeof( vol->v_vid );
1578 case VOLPBIT_BFREE :
1579 bfree = htonl( bfree );
1580 memcpy(data, &bfree, sizeof( bfree ));
1581 data += sizeof( bfree );
1584 case VOLPBIT_BTOTAL :
1585 btotal = htonl( btotal );
1586 memcpy(data, &btotal, sizeof( btotal ));
1587 data += sizeof( btotal );
1590 #ifndef NO_LARGE_VOL_SUPPORT
1591 case VOLPBIT_XBFREE :
1592 xbfree = hton64( xbfree );
1593 memcpy(data, &xbfree, sizeof( xbfree ));
1594 data += sizeof( xbfree );
1597 case VOLPBIT_XBTOTAL :
1598 xbtotal = hton64( xbtotal );
1599 memcpy(data, &xbtotal, sizeof( xbtotal ));
1600 data += sizeof( xbfree );
1602 #endif /* ! NO_LARGE_VOL_SUPPORT */
1606 data += sizeof( u_int16_t );
1609 case VOLPBIT_BSIZE: /* block size */
1610 bsize = htonl(bsize);
1611 memcpy(data, &bsize, sizeof(bsize));
1612 data += sizeof(bsize);
1617 ad_close( &ad, ADFLAGS_HF );
1619 return( AFPERR_BITMAP );
1625 ashort = htons( data - buf );
1626 memcpy(nameoff, &ashort, sizeof( ashort ));
1627 /* name is always in mac charset */
1628 aint = ucs2_to_charset( vol->v_maccharset, vol->v_macname, data+1, AFPVOL_MACNAMELEN + 1);
1638 ad_close_metadata( &ad);
1640 *buflen = data - buf;
1644 /* ------------------------- */
1645 static int stat_vol(u_int16_t bitmap, struct vol *vol, char *rbuf, size_t *rbuflen)
1651 if ( stat( vol->v_path, &st ) < 0 ) {
1653 return( AFPERR_PARAM );
1655 /* save the volume device number */
1656 vol->v_dev = st.st_dev;
1658 buflen = *rbuflen - sizeof( bitmap );
1659 if (( ret = getvolparams( bitmap, vol, &st,
1660 rbuf + sizeof( bitmap ), &buflen )) != AFP_OK ) {
1664 *rbuflen = buflen + sizeof( bitmap );
1665 bitmap = htons( bitmap );
1666 memcpy(rbuf, &bitmap, sizeof( bitmap ));
1671 /* ------------------------------- */
1672 void load_volumes(AFPObj *obj)
1674 struct passwd *pwent;
1679 /* check files date */
1680 if (obj->options.defaultvol.loaded) {
1681 changed = volfile_changed(&obj->options.defaultvol);
1683 if (obj->options.systemvol.loaded) {
1684 changed |= volfile_changed(&obj->options.systemvol);
1686 if (obj->options.uservol.loaded) {
1687 changed |= volfile_changed(&obj->options.uservol);
1696 if (parent_or_child == 0) {
1697 LOG(log_debug, logtype_afpd, "load_volumes: AFP MASTER");
1699 LOG(log_debug, logtype_afpd, "load_volumes: user: %s", obj->username);
1702 pwent = getpwnam(obj->username);
1703 if ( (obj->options.flags & OPTION_USERVOLFIRST) == 0 ) {
1704 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent);
1707 if ((*obj->username == '\0') || (obj->options.flags & OPTION_NOUSERVOL)) {
1708 readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent);
1711 * Read user's AppleVolumes or .AppleVolumes file
1712 * If neither are readable, read the default volumes file. if
1713 * that doesn't work, create a user share.
1715 if (obj->options.uservol.name) {
1716 free(obj->options.uservol.name);
1718 obj->options.uservol.name = strdup(pwent->pw_dir);
1719 if ( readvolfile(obj, &obj->options.uservol, "AppleVolumes", 1, pwent) < 0 &&
1720 readvolfile(obj, &obj->options.uservol, ".AppleVolumes", 1, pwent) < 0 &&
1721 readvolfile(obj, &obj->options.uservol, "applevolumes", 1, pwent) < 0 &&
1722 readvolfile(obj, &obj->options.uservol, ".applevolumes", 1, pwent) < 0 &&
1723 obj->options.defaultvol.name != NULL ) {
1724 if (readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent) < 0)
1725 creatvol(obj, pwent, pwent->pw_dir, NULL, NULL, 1);
1728 if ( obj->options.flags & OPTION_USERVOLFIRST ) {
1729 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent );
1732 if ( obj->options.closevol ) {
1735 for ( vol = Volumes; vol; vol = vol->v_next ) {
1736 if (vol->v_deleted && !vol->v_new ) {
1745 /* ------------------------------- */
1746 int afp_getsrvrparms(AFPObj *obj, char *ibuf _U_, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
1759 for ( vcnt = 0, volume = Volumes; volume; volume = volume->v_next ) {
1760 if (!(volume->v_flags & AFPVOL_NOSTAT)) {
1763 if ( stat( volume->v_path, &st ) < 0 ) {
1764 LOG(log_info, logtype_afpd, "afp_getsrvrparms(%s): stat: %s",
1765 volume->v_path, strerror(errno) );
1766 continue; /* can't access directory */
1768 if (!S_ISDIR(st.st_mode)) {
1769 continue; /* not a dir */
1771 accessmode(volume->v_path, &ma, NULL, &st);
1772 if ((ma.ma_user & (AR_UREAD | AR_USEARCH)) != (AR_UREAD | AR_USEARCH)) {
1773 continue; /* no r-x access */
1776 if (volume->v_hide) {
1777 continue; /* config file changed but the volume was mounted */
1780 if (utf8_encoding()) {
1781 len = ucs2_to_charset_allocate(CH_UTF8_MAC, &namebuf, volume->v_u8mname);
1783 len = ucs2_to_charset_allocate(obj->options.maccharset, &namebuf, volume->v_macname);
1786 if (len == (size_t)-1)
1789 /* set password bit if there's a volume password */
1790 *data = (volume->v_password) ? AFPSRVR_PASSWD : 0;
1792 /* Apple 2 clients running ProDOS-8 expect one volume to have
1793 bit 0 of this byte set. They will not recognize anything
1794 on the server unless this is the case. I have not
1795 completely worked this out, but it's related to booting
1796 from the server. Support for that function is a ways
1797 off.. <shirsch@ibm.net> */
1798 *data |= (volume->v_flags & AFPVOL_A2VOL) ? AFPSRVR_CONFIGINFO : 0;
1799 *data++ |= 0; /* UNIX PRIVS BIT ..., OSX doesn't seem to use it, so we don't either */
1801 memcpy(data, namebuf, len );
1807 *rbuflen = data - rbuf;
1809 if ( gettimeofday( &tv, NULL ) < 0 ) {
1810 LOG(log_error, logtype_afpd, "afp_getsrvrparms(%s): gettimeofday: %s", volume->v_path, strerror(errno) );
1812 return AFPERR_PARAM;
1814 tv.tv_sec = AD_DATE_FROM_UNIX(tv.tv_sec);
1815 memcpy(data, &tv.tv_sec, sizeof( u_int32_t));
1816 data += sizeof( u_int32_t);
1821 /* ------------------------- */
1822 static int volume_codepage(AFPObj *obj, struct vol *volume)
1824 struct charset_functions *charset;
1827 if (!volume->v_volcodepage)
1828 volume->v_volcodepage = strdup("UTF8");
1830 if ( (charset_t) -1 == ( volume->v_volcharset = add_charset(volume->v_volcodepage)) ) {
1831 LOG (log_error, logtype_afpd, "Setting codepage %s as volume codepage failed", volume->v_volcodepage);
1835 if ( NULL == (charset = find_charset_functions(volume->v_volcodepage)) || charset->flags & CHARSET_ICONV ) {
1836 LOG (log_warning, logtype_afpd, "WARNING: volume encoding %s is *not* supported by netatalk, expect problems !!!!", volume->v_volcodepage);
1839 if (!volume->v_maccodepage)
1840 volume->v_maccodepage = strdup(obj->options.maccodepage);
1842 if ( (charset_t) -1 == ( volume->v_maccharset = add_charset(volume->v_maccodepage)) ) {
1843 LOG (log_error, logtype_afpd, "Setting codepage %s as mac codepage failed", volume->v_maccodepage);
1847 if ( NULL == ( charset = find_charset_functions(volume->v_maccodepage)) || ! (charset->flags & CHARSET_CLIENT) ) {
1848 LOG (log_error, logtype_afpd, "Fatal error: mac charset %s not supported", volume->v_maccodepage);
1851 volume->v_kTextEncoding = htonl(charset->kTextEncoding);
1855 /* ------------------------- */
1856 static int volume_openDB(struct vol *volume)
1860 if ((volume->v_flags & AFPVOL_NODEV)) {
1861 flags |= CNID_FLAG_NODEV;
1864 if (volume->v_cnidscheme == NULL) {
1865 volume->v_cnidscheme = strdup(DEFAULT_CNID_SCHEME);
1866 LOG(log_info, logtype_afpd, "Volume %s use CNID scheme %s.",
1867 volume->v_path, volume->v_cnidscheme);
1870 LOG(log_info, logtype_afpd, "CNID server: %s:%s",
1871 volume->v_cnidserver ? volume->v_cnidserver : Cnid_srv,
1872 volume->v_cnidport ? volume->v_cnidport : Cnid_port);
1875 /* Found this in branch dir-rewrite, maybe we want to use it sometimes */
1877 /* Legacy pre 2.1 way of sharing eg CD-ROM */
1878 if (strcmp(volume->v_cnidscheme, "last") == 0) {
1879 /* "last" is gone. We support it by switching to in-memory "tdb" */
1880 volume->v_cnidscheme = strdup("tdb");
1881 flags |= CNID_FLAG_MEMORY;
1884 /* New way of sharing CD-ROM */
1885 if (volume->v_flags & AFPVOL_CDROM) {
1886 flags |= CNID_FLAG_MEMORY;
1887 if (strcmp(volume->v_cnidscheme, "tdb") != 0) {
1888 free(volume->v_cnidscheme);
1889 volume->v_cnidscheme = strdup("tdb");
1890 LOG(log_info, logtype_afpd, "Volume %s is ejectable, switching to scheme %s.",
1891 volume->v_path, volume->v_cnidscheme);
1896 volume->v_cdb = cnid_open(volume->v_path,
1898 volume->v_cnidscheme,
1900 volume->v_cnidserver ? volume->v_cnidserver : Cnid_srv,
1901 volume->v_cnidport ? volume->v_cnidport : Cnid_port);
1903 if ( ! volume->v_cdb && ! (flags & CNID_FLAG_MEMORY)) {
1904 /* The first attempt failed and it wasn't yet an attempt to open in-memory */
1905 LOG(log_error, logtype_afpd, "Can't open volume \"%s\" CNID backend \"%s\" ",
1906 volume->v_path, volume->v_cnidscheme);
1907 LOG(log_error, logtype_afpd, "Reopen volume %s using in memory temporary CNID DB.",
1909 flags |= CNID_FLAG_MEMORY;
1910 volume->v_cdb = cnid_open (volume->v_path, volume->v_umask, "tdb", flags, NULL, NULL);
1912 /* kill ourself with SIGUSR2 aka msg pending */
1913 if (volume->v_cdb) {
1914 setmessage("Something wrong with the volume's CNID DB, using temporary CNID DB instead."
1915 "Check server messages for details!");
1916 kill(getpid(), SIGUSR2);
1917 /* deactivate cnid caching/storing in AppleDouble files */
1918 volume->v_flags &= ~AFPVOL_CACHE;
1923 return (!volume->v_cdb)?-1:0;
1927 Check if the underlying filesystem supports EAs for ea:sys volumes.
1928 If not, switch to ea:ad.
1929 As we can't check (requires write access) on ro-volumes, we switch ea:auto
1930 volumes that are options:ro to ea:none.
1932 static void check_ea_sys_support(struct vol *vol)
1934 uid_t process_uid = 0;
1935 char eaname[] = {"org.netatalk.supports-eas.XXXXXX"};
1936 const char *eacontent = "yes";
1938 if (vol->v_vfs_ea == AFPVOL_EA_AUTO) {
1940 if ((vol->v_flags & AFPVOL_RO) == AFPVOL_RO) {
1941 LOG(log_info, logtype_afpd, "read-only volume '%s', can't test for EA support, disabling EAs", vol->v_localname);
1942 vol->v_vfs_ea = AFPVOL_EA_NONE;
1948 process_uid = geteuid();
1950 if (seteuid(0) == -1) {
1951 LOG(log_error, logtype_afpd, "check_ea_sys_support: can't seteuid(0): %s", strerror(errno));
1955 if ((sys_setxattr(vol->v_path, eaname, eacontent, 4, 0)) == 0) {
1956 sys_removexattr(vol->v_path, eaname);
1957 vol->v_vfs_ea = AFPVOL_EA_SYS;
1959 LOG(log_warning, logtype_afpd, "volume \"%s\" does not support Extended Attributes, using ea:ad instead",
1961 vol->v_vfs_ea = AFPVOL_EA_AD;
1965 if (seteuid(process_uid) == -1) {
1966 LOG(log_error, logtype_afpd, "can't seteuid back %s", strerror(errno));
1973 /* -------------------------
1974 * we are the user here
1976 int afp_openvol(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
1987 char path[ MAXPATHLEN + 1];
1993 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1994 bitmap = ntohs( bitmap );
1995 ibuf += sizeof( bitmap );
1998 if (( bitmap & (1<<VOLPBIT_VID)) == 0 ) {
1999 return AFPERR_BITMAP;
2002 len = (unsigned char)*ibuf++;
2003 volname = obj->oldtmp;
2005 if ((volname_tmp = strchr(volname,'+')) != NULL)
2006 volname = volname_tmp+1;
2008 if (utf8_encoding()) {
2009 namelen = convert_string(CH_UTF8_MAC, CH_UCS2, ibuf, len, volname, sizeof(obj->oldtmp));
2011 namelen = convert_string(obj->options.maccharset, CH_UCS2, ibuf, len, volname, sizeof(obj->oldtmp));
2014 if ( namelen <= 0) {
2015 return AFPERR_PARAM;
2019 if ((len + 1) & 1) /* pad to an even boundary */
2024 for ( volume = Volumes; volume; volume = volume->v_next ) {
2025 if ( strcasecmp_w( (ucs2_t*) volname, volume->v_name ) == 0 ) {
2030 if ( volume == NULL ) {
2031 return AFPERR_PARAM;
2034 /* check for a volume password */
2035 if (volume->v_password && strncmp(ibuf, volume->v_password, VOLPASSLEN)) {
2036 return AFPERR_ACCESS;
2039 if (( volume->v_flags & AFPVOL_OPEN ) ) {
2040 /* the volume is already open */
2041 return stat_vol(bitmap, volume, rbuf, rbuflen);
2044 if (volume->v_root_preexec) {
2045 if ((ret = afprun(1, volume->v_root_preexec, NULL)) && volume->v_root_preexec_close) {
2046 LOG(log_error, logtype_afpd, "afp_openvol(%s): root preexec : %d", volume->v_path, ret );
2051 if (volume->v_preexec) {
2052 if ((ret = afprun(0, volume->v_preexec, NULL)) && volume->v_preexec_close) {
2053 LOG(log_error, logtype_afpd, "afp_openvol(%s): preexec : %d", volume->v_path, ret );
2058 if ( stat( volume->v_path, &st ) < 0 ) {
2059 return AFPERR_PARAM;
2062 if ( chdir( volume->v_path ) < 0 ) {
2063 return AFPERR_PARAM;
2066 if ( NULL == getcwd(path, MAXPATHLEN)) {
2067 /* shouldn't be fatal but it will fail later */
2068 LOG(log_error, logtype_afpd, "afp_openvol(%s): volume pathlen too long", volume->v_path);
2072 /* Normalize volume path */
2073 #ifdef REALPATH_TAKES_NULL
2074 if ((volume->v_path = realpath(path, NULL)) == NULL)
2077 if ((volume->v_path = malloc(MAXPATHLEN+1)) == NULL)
2079 if (realpath(path, volume->v_path) == NULL) {
2080 free(volume->v_path);
2083 /* Safe some memory */
2085 if ((tmp = strdup(volume->v_path)) == NULL) {
2086 free(volume->v_path);
2089 free(volume->v_path);
2090 volume->v_path = tmp;
2093 if (volume_codepage(obj, volume) < 0) {
2098 /* initialize volume variables
2101 if (utf8_encoding()) {
2102 volume->max_filename = UTF8FILELEN_EARLY;
2105 volume->max_filename = MACFILELEN;
2108 volume->v_flags |= AFPVOL_OPEN;
2109 volume->v_cdb = NULL;
2111 if (utf8_encoding()) {
2112 len = convert_string_allocate(CH_UCS2, CH_UTF8_MAC, volume->v_u8mname, namelen, &vol_mname);
2114 len = convert_string_allocate(CH_UCS2, obj->options.maccharset, volume->v_macname, namelen, &vol_mname);
2116 if ( !vol_mname || len <= 0) {
2121 if ((vol_uname = strrchr(path, '/')) == NULL)
2123 else if (*(vol_uname + 1) != '\0')
2126 if ((dir = dir_new(vol_mname,
2131 bfromcstr(volume->v_path),
2135 LOG(log_error, logtype_afpd, "afp_openvol(%s): malloc: %s", volume->v_path, strerror(errno) );
2140 volume->v_root = dir;
2143 if (volume_openDB(volume) < 0) {
2144 LOG(log_error, logtype_afpd, "Fatal error: cannot open CNID or invalid CNID backend for %s: %s",
2145 volume->v_path, volume->v_cnidscheme);
2150 ret = stat_vol(bitmap, volume, rbuf, rbuflen);
2151 if (ret == AFP_OK) {
2153 if (!(volume->v_flags & AFPVOL_RO)) {
2154 handle_special_folders( volume );
2156 volume->v_cnidserver ? volume->v_cnidserver : Cnid_srv,
2157 volume->v_cnidport ? volume->v_cnidport : Cnid_port);
2161 * If you mount a volume twice, the second time the trash appears on
2162 * the desk-top. That's because the Mac remembers the DID for the
2163 * trash (even for volumes in different zones, on different servers).
2164 * Just so this works better, we prime the DID cache with the trash,
2165 * fixing the trash at DID 17.
2166 * FIXME (RL): should it be done inside a CNID backend ? (always returning Trash DID when asked) ?
2168 if ((volume->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
2170 /* FIXME find db time stamp */
2171 if (cnid_getstamp(volume->v_cdb, volume->v_stamp, sizeof(volume->v_stamp)) < 0) {
2172 LOG (log_error, logtype_afpd,
2173 "afp_openvol(%s): Fatal error: Unable to get stamp value from CNID backend",
2181 cname( volume, volume->v_root, &p );
2187 if (volume->v_root) {
2188 dir_free( volume->v_root );
2189 volume->v_root = NULL;
2192 volume->v_flags &= ~AFPVOL_OPEN;
2193 if (volume->v_cdb != NULL) {
2194 cnid_close(volume->v_cdb);
2195 volume->v_cdb = NULL;
2201 /* ------------------------- */
2202 static void closevol(struct vol *vol)
2207 dir_free( vol->v_root );
2209 if (vol->v_cdb != NULL) {
2210 cnid_close(vol->v_cdb);
2214 if (vol->v_postexec) {
2215 afprun(0, vol->v_postexec, NULL);
2217 if (vol->v_root_postexec) {
2218 afprun(1, vol->v_root_postexec, NULL);
2222 /* ------------------------- */
2223 void close_all_vol(void)
2227 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
2228 if ( (ovol->v_flags & AFPVOL_OPEN) ) {
2229 ovol->v_flags &= ~AFPVOL_OPEN;
2235 /* ------------------------- */
2236 static void deletevol(struct vol *vol)
2240 vol->v_flags &= ~AFPVOL_OPEN;
2241 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
2242 if ( (ovol->v_flags & AFPVOL_OPEN) ) {
2246 if ( ovol != NULL ) {
2247 /* Even if chdir fails, we can't say afp_closevol fails. */
2248 if ( chdir( ovol->v_path ) == 0 ) {
2249 curdir = ovol->v_root;
2254 if (vol->v_deleted) {
2255 showvol(vol->v_name);
2262 /* ------------------------- */
2263 int afp_closevol(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2270 memcpy(&vid, ibuf, sizeof( vid ));
2271 if (NULL == ( vol = getvolbyvid( vid )) ) {
2272 return( AFPERR_PARAM );
2281 /* ------------------------- */
2282 struct vol *getvolbyvid(const u_int16_t vid )
2286 for ( vol = Volumes; vol; vol = vol->v_next ) {
2287 if ( vid == vol->v_vid ) {
2291 if ( vol == NULL || ( vol->v_flags & AFPVOL_OPEN ) == 0 ) {
2300 /* ------------------------ */
2301 static int ext_cmp_key(const void *key, const void *obj)
2303 const char *p = key;
2304 const struct extmap *em = obj;
2305 return strdiacasecmp(p, em->em_ext);
2307 struct extmap *getextmap(const char *path)
2312 if (!Extmap_cnt || NULL == ( p = strrchr( path, '.' )) ) {
2313 return( Defextmap );
2317 return( Defextmap );
2319 em = bsearch(p, Extmap, Extmap_cnt, sizeof(struct extmap), ext_cmp_key);
2323 return( Defextmap );
2327 /* ------------------------- */
2328 struct extmap *getdefextmap(void)
2330 return( Defextmap );
2333 /* --------------------------
2334 poll if a volume is changed by other processes.
2336 0 no attention msg sent
2337 1 attention msg sent
2338 -1 error (socket closed)
2340 Note: if attention return -1 no packet has been
2341 sent because the buffer is full, we don't care
2342 either there's no reader or there's a lot of
2343 traffic and another pollvoltime will follow
2345 int pollvoltime(AFPObj *obj)
2352 if (!(afp_version > 21 && obj->options.server_notif))
2355 if ( gettimeofday( &tv, NULL ) < 0 )
2358 for ( vol = Volumes; vol; vol = vol->v_next ) {
2359 if ( (vol->v_flags & AFPVOL_OPEN) && vol->v_mtime + 30 < tv.tv_sec) {
2360 if ( !stat( vol->v_path, &st ) && vol->v_mtime != st.st_mtime ) {
2361 vol->v_mtime = st.st_mtime;
2362 if (!obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED))
2371 /* ------------------------- */
2372 void setvoltime(AFPObj *obj, struct vol *vol)
2376 /* just looking at vol->v_mtime is broken seriously since updates
2377 * from other users afpd processes never are seen.
2378 * This is not the most elegant solution (a shared memory between
2379 * the afpd processes would come closer)
2382 if ( gettimeofday( &tv, NULL ) < 0 ) {
2383 LOG(log_error, logtype_afpd, "setvoltime(%s): gettimeofday: %s", vol->v_path, strerror(errno) );
2386 if( utime( vol->v_path, NULL ) < 0 ) {
2387 /* write of time failed ... probably a read only filesys,
2388 * where no other users can interfere, so there's no issue here
2392 /* a little granularity */
2393 if (vol->v_mtime < tv.tv_sec) {
2394 vol->v_mtime = tv.tv_sec;
2395 /* or finder doesn't update free space
2396 * AFP 3.2 and above clients seem to be ok without so many notification
2398 if (afp_version < 32 && obj->options.server_notif) {
2399 obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED);
2404 /* ------------------------- */
2405 int afp_getvolparams(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_,char *rbuf, size_t *rbuflen)
2408 u_int16_t vid, bitmap;
2411 memcpy(&vid, ibuf, sizeof( vid ));
2412 ibuf += sizeof( vid );
2413 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2414 bitmap = ntohs( bitmap );
2416 if (NULL == ( vol = getvolbyvid( vid )) ) {
2418 return( AFPERR_PARAM );
2421 return stat_vol(bitmap, vol, rbuf, rbuflen);
2424 /* ------------------------- */
2425 int afp_setvolparams(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2429 u_int16_t vid, bitmap;
2435 memcpy(&vid, ibuf, sizeof( vid ));
2436 ibuf += sizeof( vid );
2437 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2438 bitmap = ntohs( bitmap );
2439 ibuf += sizeof(bitmap);
2441 if (( vol = getvolbyvid( vid )) == NULL ) {
2442 return( AFPERR_PARAM );
2445 if ((vol->v_flags & AFPVOL_RO))
2446 return AFPERR_VLOCK;
2448 /* we can only set the backup date. */
2449 if (bitmap != (1 << VOLPBIT_BDATE))
2450 return AFPERR_BITMAP;
2452 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2453 if ( ad_open(&ad, vol->v_path, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR) < 0 ) {
2455 return AFPERR_VLOCK;
2457 return AFPERR_ACCESS;
2460 memcpy(&aint, ibuf, sizeof(aint));
2461 ad_setdate(&ad, AD_DATE_BACKUP, aint);
2463 ad_close(&ad, ADFLAGS_HF);
2467 /* ------------------------- */
2468 int wincheck(const struct vol *vol, const char *path)
2472 if (!(vol->v_flags & AFPVOL_MSWINDOWS))
2475 /* empty paths are not allowed */
2476 if ((len = strlen(path)) == 0)
2479 /* leading or trailing whitespaces are not allowed, carriage returns
2480 * and probably other whitespace is okay, tabs are not allowed
2482 if ((path[0] == ' ') || (path[len-1] == ' '))
2485 /* certain characters are not allowed */
2486 if (strpbrk(path, MSWINDOWS_BADCHARS))
2489 /* everything else is okay */
2495 * precreate a folder
2496 * this is only intended for folders in the volume root
2497 * It will *not* work if the folder name contains extended characters
2499 static int create_special_folder (const struct vol *vol, const struct _special_folder *folder)
2508 p = (char *) malloc ( strlen(vol->v_path)+strlen(folder->name)+2);
2510 LOG(log_error, logtype_afpd,"malloc failed");
2514 q=strdup(folder->name);
2516 LOG(log_error, logtype_afpd,"malloc failed");
2520 strcpy(p, vol->v_path);
2525 if ((vol->v_casefold & AFPVOL_MTOUUPPER))
2527 else if ((vol->v_casefold & AFPVOL_MTOULOWER))
2533 if ( (ret = stat( p, &st )) < 0 ) {
2534 if (folder->precreate) {
2535 if (ad_mkdir(p, folder->mode)) {
2536 LOG(log_debug, logtype_afpd,"Creating '%s' failed in %s: %s", p, vol->v_path, strerror(errno));
2545 if ( !ret && folder->hide) {
2547 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2548 if (ad_open(&ad, p, ADFLAGS_HF | ADFLAGS_DIR, O_RDWR | O_CREAT, 0666) != 0) {
2554 ad_setname(&ad, folder->name);
2556 ad_getattr(&ad, &attr);
2557 attr |= htons( ntohs( attr ) | ATTRBIT_INVISIBLE );
2558 ad_setattr(&ad, attr);
2560 /* do the same with the finder info */
2561 if (ad_entry(&ad, ADEID_FINDERI)) {
2562 memcpy(&attr, ad_entry(&ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF, sizeof(attr));
2563 attr |= htons(FINDERINFO_INVISIBLE);
2564 memcpy(ad_entry(&ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF,&attr, sizeof(attr));
2568 ad_close_metadata( &ad);
2575 static void handle_special_folders (const struct vol * vol)
2577 const _special_folder *p = &special_folders[0];
2579 if ((vol->v_flags & AFPVOL_RO))
2582 for (; p->name != NULL; p++) {
2583 create_special_folder (vol, p);
2587 const struct vol *getvolumes(void)
2592 void unload_volumes_and_extmap(void)
2594 LOG(log_debug, logtype_afpd, "unload_volumes_and_extmap");
2600 * Get a volumes UUID from the config file.
2601 * If there is none, it is generated and stored there.
2603 * Returns pointer to allocated storage on success, NULL on error.
2605 static char *get_vol_uuid(const AFPObj *obj, const char *volname)
2608 char buf[1024], uuid[UUID_PRINTABLE_STRING_LENGTH], *p;
2610 struct stat tmpstat;
2613 if ((fp = fopen(obj->options.uuidconf, "r")) != NULL) { /* read open? */
2614 /* scan in the conf file */
2615 while (fgets(buf, sizeof(buf), fp) != NULL) {
2617 while (p && isblank(*p))
2619 if (!p || (*p == '#') || (*p == '\n'))
2620 continue; /* invalid line */
2623 if ((volname_conf = strtok( p, "\"" )) == NULL)
2624 continue; /* syntax error */
2626 if ((volname_conf = strtok( p, " \t" )) == NULL)
2627 continue; /* syntax error: invalid name */
2629 p = strchr(p, '\0');
2632 continue; /* syntax error */
2634 if (strcmp(volname, volname_conf) != 0)
2635 continue; /* another volume name */
2637 while (p && isblank(*p))
2640 if (sscanf(p, "%36s", uuid) == 1 ) {
2641 for (int i=0; uuid[i]; i++)
2642 uuid[i] = toupper(uuid[i]);
2643 LOG(log_debug, logtype_afpd, "get_uuid('%s'): UUID: '%s'", volname, uuid);
2645 return strdup(uuid);
2653 /* not found or no file, reopen in append mode */
2655 if (stat(obj->options.uuidconf, &tmpstat)) { /* no file */
2656 if (( fd = creat(obj->options.uuidconf, 0644 )) < 0 ) {
2657 LOG(log_error, logtype_afpd, "ERROR: Cannot create %s (%s).",
2658 obj->options.uuidconf, strerror(errno));
2661 if (( fp = fdopen( fd, "w" )) == NULL ) {
2662 LOG(log_error, logtype_afpd, "ERROR: Cannot fdopen %s (%s).",
2663 obj->options.uuidconf, strerror(errno));
2667 } else if ((fp = fopen(obj->options.uuidconf, "a+")) == NULL) { /* not found */
2668 LOG(log_error, logtype_afpd, "Cannot create or append to %s (%s).",
2669 obj->options.uuidconf, strerror(errno));
2672 fseek(fp, 0L, SEEK_END);
2673 if(ftell(fp) == 0) { /* size = 0 */
2674 fprintf(fp, "# DON'T TOUCH NOR COPY THOUGHTLESSLY!\n");
2675 fprintf(fp, "# This file is auto-generated by afpd\n");
2676 fprintf(fp, "# and stores UUIDs for TM volumes.\n\n");
2678 fseek(fp, -1L, SEEK_END);
2679 if(fgetc(fp) != '\n') fputc('\n', fp); /* last char is \n? */
2682 /* generate uuid and write to file */
2685 randombytes((void *)id, 16);
2686 cp = uuid_bin2string(id);
2688 LOG(log_debug, logtype_afpd, "get_uuid('%s'): generated UUID '%s'", volname, cp);
2690 fprintf(fp, "\"%s\"\t%36s\n", volname, cp);