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>
39 #include <uuid/uuid.h>
41 #include <atalk/asp.h>
42 #include <atalk/dsi.h>
43 #include <atalk/adouble.h>
44 #include <atalk/afp.h>
45 #include <atalk/util.h>
46 #include <atalk/volinfo.h>
47 #include <atalk/logger.h>
48 #include <atalk/vfs.h>
51 #include <atalk/cnid.h>
55 #include "directory.h"
64 extern int afprun(int root, char *cmd, int *outfd);
67 #define MIN(a, b) ((a) < (b) ? (a) : (b))
70 #ifndef NO_LARGE_VOL_SUPPORT
71 #if BYTE_ORDER == BIG_ENDIAN
74 #else /* BYTE_ORDER == BIG_ENDIAN */
75 #define hton64(x) ((u_int64_t) (htonl(((x) >> 32) & 0xffffffffLL)) | \
76 (u_int64_t) ((htonl(x) & 0xffffffffLL) << 32))
77 #define ntoh64(x) (hton64(x))
78 #endif /* BYTE_ORDER == BIG_ENDIAN */
79 #endif /* ! NO_LARGE_VOL_SUPPORT */
81 #ifndef UUID_PRINTABLE_STRING_LENGTH
82 #define UUID_PRINTABLE_STRING_LENGTH 37
86 struct vol *current_vol; /* last volume from getvolbyvid() */
88 static struct vol *Volumes = NULL;
89 static u_int16_t lastvid = 0;
90 static char *Trash = "\02\024Network Trash Folder";
92 static struct extmap *Extmap = NULL, *Defextmap = NULL;
93 static int Extmap_cnt;
94 static void free_extmap(void);
96 #define VOLOPT_ALLOW 0 /* user allow list */
97 #define VOLOPT_DENY 1 /* user deny list */
98 #define VOLOPT_RWLIST 2 /* user rw list */
99 #define VOLOPT_ROLIST 3 /* user ro list */
100 #define VOLOPT_PASSWORD 4 /* volume password */
101 #define VOLOPT_CASEFOLD 5 /* character case mangling */
102 #define VOLOPT_FLAGS 6 /* various flags */
103 #define VOLOPT_DBPATH 7 /* path to database */
104 #define VOLOPT_LIMITSIZE 8 /* Limit the size of the volume */
106 #define VOLOPT_VETO 10 /* list of veto filespec */
107 #define VOLOPT_PREEXEC 11 /* preexec command */
108 #define VOLOPT_ROOTPREEXEC 12 /* root preexec command */
109 #define VOLOPT_POSTEXEC 13 /* postexec command */
110 #define VOLOPT_ROOTPOSTEXEC 14 /* root postexec command */
111 #define VOLOPT_ENCODING 15 /* mac encoding (pre OSX)*/
112 #define VOLOPT_MACCHARSET 16
113 #define VOLOPT_CNIDSCHEME 17
114 #define VOLOPT_ADOUBLE 18 /* adouble version */
120 #define VOLOPT_FORCEUID 19 /* force uid for username x */
121 #define VOLOPT_FORCEGID 20 /* force gid for group x */
122 #endif /* FORCE_UIDGID */
124 #define VOLOPT_UMASK 21
125 #define VOLOPT_ALLOWED_HOSTS 22
126 #define VOLOPT_DENIED_HOSTS 23
127 #define VOLOPT_DPERM 24 /* dperm default directories perms */
128 #define VOLOPT_FPERM 25 /* fperm default files perms */
129 #define VOLOPT_DFLTPERM 26 /* perm */
130 #define VOLOPT_EA_VFS 27 /* Extended Attributes vfs indirection */
131 #define VOLOPT_CNIDSERVER 28 /* CNID Server ip address*/
132 #define VOLOPT_CNIDPORT 30 /* CNID server tcp port */
134 #define VOLOPT_MAX 31 /* <== IMPORTANT !!!!!! */
135 #define VOLOPT_NUM (VOLOPT_MAX + 1)
138 #define VOLOPT_DEFAULT ":DEFAULT:"
139 #define VOLOPT_DEFAULT_LEN 9
146 typedef struct _special_folder {
153 static const _special_folder special_folders[] = {
154 {"Network Trash Folder", 1, 0777, 1},
155 {"Temporary Items", 1, 0777, 1},
156 {".AppleDesktop", 1, 0777, 0},
158 {"TheFindByContentFolder", 0, 0, 1},
159 {"TheVolumeSettingsFolder", 0, 0, 1},
163 /* Forward declarations */
164 static void handle_special_folders (const struct vol *);
165 static void deletevol(struct vol *vol);
166 static void volume_free(struct vol *vol);
167 static void check_ea_sys_support(struct vol *vol);
169 static void volfree(struct vol_option *options, const struct vol_option *save)
174 for (i = 0; i < VOLOPT_MAX; i++) {
175 if (options[i].c_value && (options[i].c_value != save[i].c_value))
176 free(options[i].c_value);
179 for (i = 0; i < VOLOPT_MAX; i++) {
180 if (options[i].c_value)
181 free(options[i].c_value);
187 #define is_var(a, b) (strncmp((a), (b), 2) == 0)
190 * Handle variable substitutions. here's what we understand:
191 * $b -> basename of path
192 * $c -> client ip/appletalk address
193 * $d -> volume pathname on server
194 * $f -> full name (whatever's in the gecos field)
197 * $i -> client ip/appletalk address without port
198 * $s -> server name (hostname if it doesn't exist)
199 * $u -> username (guest is usually nobody)
200 * $v -> volume name or basename if null
201 * $z -> zone (may not exist)
204 * This get's called from readvolfile with
205 * path = NULL, volname = NULL for xlating the volumes path
206 * path = path, volname = NULL for xlating the volumes name
207 * ... and from volumes options parsing code when xlating eg dbpath with
208 * path = path, volname = volname
210 * Using this information we can reject xlation of any variable depeninding on a login
211 * context which is not given in the afp master, where we must evaluate this whole stuff
212 * too for the Zeroconf announcements.
214 static char *volxlate(AFPObj *obj,
227 int xlatevolname = 0;
229 if (! ((DSI *)obj->handle)->child)
232 if (path && !volname)
240 dest = calloc(destlen +1, 1);
246 strlcpy(dest, src, destlen +1);
247 if ((p = strchr(src, '$')) == NULL) /* nothing to do */
250 /* first part of the path. just forward to the next variable. */
251 len = MIN((size_t)(p - src), destlen);
257 while (p && destlen > 0) {
258 /* now figure out what the variable is */
260 if (is_var(p, "$b")) {
261 if (afpmaster && xlatevolname)
264 if ((q = strrchr(path, '/')) == NULL)
266 else if (*(q + 1) != '\0')
269 } else if (is_var(p, "$c")) {
270 if (afpmaster && xlatevolname)
272 if (obj->proto == AFPPROTO_ASP) {
273 ASP asp = obj->handle;
275 len = sprintf(dest, "%u.%u", ntohs(asp->asp_sat.sat_addr.s_net),
276 asp->asp_sat.sat_addr.s_node);
280 } else if (obj->proto == AFPPROTO_DSI) {
281 DSI *dsi = obj->handle;
282 len = sprintf(dest, "%s:%u",
283 getip_string((struct sockaddr *)&dsi->client),
284 getip_port((struct sockaddr *)&dsi->client));
288 } else if (is_var(p, "$d")) {
289 if (afpmaster && xlatevolname)
292 } else if (pwd && is_var(p, "$f")) {
293 if (afpmaster && xlatevolname)
295 if ((r = strchr(pwd->pw_gecos, ',')))
298 } else if (pwd && is_var(p, "$g")) {
299 if (afpmaster && xlatevolname)
301 struct group *grp = getgrgid(pwd->pw_gid);
304 } else if (is_var(p, "$h")) {
305 q = obj->options.hostname;
306 } else if (is_var(p, "$i")) {
307 if (afpmaster && xlatevolname)
309 if (obj->proto == AFPPROTO_ASP) {
310 ASP asp = obj->handle;
312 len = sprintf(dest, "%u", ntohs(asp->asp_sat.sat_addr.s_net));
316 } else if (obj->proto == AFPPROTO_DSI) {
317 DSI *dsi = obj->handle;
318 q = getip_string((struct sockaddr *)&dsi->client);
320 } else if (is_var(p, "$s")) {
323 else if (obj->options.server) {
324 q = obj->options.server;
326 q = obj->options.hostname;
327 } else if (obj->username && is_var(p, "$u")) {
328 if (afpmaster && xlatevolname)
331 if ( obj->options.ntseparator && (sep = strchr(obj->username, obj->options.ntseparator[0])) != NULL)
335 } else if (is_var(p, "$v")) {
336 if (afpmaster && xlatevolname)
342 if ((q = strrchr(path, '/')) == NULL)
344 else if (*(q + 1) != '\0')
347 } else if (is_var(p, "$z")) {
349 } else if (is_var(p, "$$")) {
354 /* copy the stuff over. if we don't understand something that we
355 * should, just skip it over. */
357 len = MIN(p == q ? 2 : strlen(q), destlen);
358 strncpy(dest, q, len);
363 /* stuff up to next $ */
365 p = strchr(src, '$');
366 len = p ? MIN((size_t)(p - src), destlen) : destlen;
368 strncpy(dest, src, len);
376 /* to make sure that val is valid, make sure to select an opt that
378 static int optionok(const char *buf, const char *opt, const char *val)
380 if (!strstr(buf,opt))
388 /* -------------------- */
389 static void setoption(struct vol_option *options, struct vol_option *save, int opt, const char *val)
391 if (options[opt].c_value && (!save || options[opt].c_value != save[opt].c_value))
392 free(options[opt].c_value);
393 options[opt].c_value = strdup(val + 1);
396 /* ------------------------------------------
397 handle all the options. tmp can't be NULL. */
398 static void volset(struct vol_option *options, struct vol_option *save,
399 char *volname, int vlen,
404 val = strchr(tmp, ':');
406 /* we'll assume it's a volume name. */
407 strncpy(volname, tmp, vlen);
412 LOG(log_debug, logtype_afpd, "Parsing volset %s", val);
414 if (optionok(tmp, "allow:", val)) {
415 setoption(options, save, VOLOPT_ALLOW, val);
417 } else if (optionok(tmp, "deny:", val)) {
418 setoption(options, save, VOLOPT_DENY, val);
420 } else if (optionok(tmp, "rwlist:", val)) {
421 setoption(options, save, VOLOPT_RWLIST, val);
423 } else if (optionok(tmp, "rolist:", val)) {
424 setoption(options, save, VOLOPT_ROLIST, val);
426 } else if (optionok(tmp, "codepage:", val)) {
427 LOG (log_error, logtype_afpd, "The old codepage system has been removed. Please make sure to read the documentation !!!!");
428 /* Make sure we don't screw anything */
430 } else if (optionok(tmp, "volcharset:", val)) {
431 setoption(options, save, VOLOPT_ENCODING, val);
432 } else if (optionok(tmp, "maccharset:", val)) {
433 setoption(options, save, VOLOPT_MACCHARSET, val);
434 } else if (optionok(tmp, "veto:", val)) {
435 setoption(options, save, VOLOPT_VETO, val);
436 } else if (optionok(tmp, "cnidscheme:", val)) {
437 setoption(options, save, VOLOPT_CNIDSCHEME, val);
438 } else if (optionok(tmp, "casefold:", val)) {
439 if (strcasecmp(val + 1, "tolower") == 0)
440 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMLOWER;
441 else if (strcasecmp(val + 1, "toupper") == 0)
442 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMUPPER;
443 else if (strcasecmp(val + 1, "xlatelower") == 0)
444 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UUPPERMLOWER;
445 else if (strcasecmp(val + 1, "xlateupper") == 0)
446 options[VOLOPT_CASEFOLD].i_value = AFPVOL_ULOWERMUPPER;
447 } else if (optionok(tmp, "adouble:", val)) {
448 if (strcasecmp(val + 1, "v1") == 0)
449 options[VOLOPT_ADOUBLE].i_value = AD_VERSION1;
450 #if AD_VERSION == AD_VERSION2
451 else if (strcasecmp(val + 1, "v2") == 0)
452 options[VOLOPT_ADOUBLE].i_value = AD_VERSION2;
453 else if (strcasecmp(val + 1, "osx") == 0)
454 options[VOLOPT_ADOUBLE].i_value = AD_VERSION2_OSX;
455 else if (strcasecmp(val + 1, "sfm") == 0)
456 options[VOLOPT_ADOUBLE].i_value = AD_VERSION1_SFM;
458 } else if (optionok(tmp, "options:", val)) {
461 if ((p = strtok(val + 1, ",")) == NULL) /* nothing */
465 if (strcasecmp(p, "prodos") == 0)
466 options[VOLOPT_FLAGS].i_value |= AFPVOL_A2VOL;
467 else if (strcasecmp(p, "mswindows") == 0) {
468 options[VOLOPT_FLAGS].i_value |= AFPVOL_MSWINDOWS | AFPVOL_USEDOTS;
469 } else if (strcasecmp(p, "crlf") == 0)
470 options[VOLOPT_FLAGS].i_value |= AFPVOL_CRLF;
471 else if (strcasecmp(p, "noadouble") == 0)
472 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOADOUBLE;
473 else if (strcasecmp(p, "ro") == 0)
474 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
475 else if (strcasecmp(p, "nohex") == 0)
476 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOHEX;
477 else if (strcasecmp(p, "usedots") == 0)
478 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS;
479 else if (strcasecmp(p, "invisibledots") == 0)
480 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS | AFPVOL_INV_DOTS;
481 else if (strcasecmp(p, "limitsize") == 0)
482 options[VOLOPT_FLAGS].i_value |= AFPVOL_LIMITSIZE;
483 /* support for either "dropbox" or "dropkludge" */
484 else if (strcasecmp(p, "dropbox") == 0)
485 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
486 else if (strcasecmp(p, "dropkludge") == 0)
487 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
488 else if (strcasecmp(p, "nofileid") == 0)
489 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOFILEID;
490 else if (strcasecmp(p, "nostat") == 0)
491 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOSTAT;
492 else if (strcasecmp(p, "preexec_close") == 0)
493 options[VOLOPT_PREEXEC].i_value = 1;
494 else if (strcasecmp(p, "root_preexec_close") == 0)
495 options[VOLOPT_ROOTPREEXEC].i_value = 1;
496 else if (strcasecmp(p, "upriv") == 0)
497 options[VOLOPT_FLAGS].i_value |= AFPVOL_UNIX_PRIV;
498 else if (strcasecmp(p, "nodev") == 0)
499 options[VOLOPT_FLAGS].i_value |= AFPVOL_NODEV;
500 else if (strcasecmp(p, "caseinsensitive") == 0)
501 options[VOLOPT_FLAGS].i_value |= AFPVOL_CASEINSEN;
502 else if (strcasecmp(p, "illegalseq") == 0)
503 options[VOLOPT_FLAGS].i_value |= AFPVOL_EILSEQ;
504 else if (strcasecmp(p, "nocnidcache") == 0)
505 options[VOLOPT_FLAGS].i_value &= ~AFPVOL_CACHE;
506 else if (strcasecmp(p, "tm") == 0)
507 options[VOLOPT_FLAGS].i_value |= AFPVOL_TM;
508 /* Found this in branch dir-rewrite, maybe we want to use it sometimes */
510 else if (strcasecmp(p, "cdrom") == 0)
511 options[VOLOPT_FLAGS].i_value |= AFPVOL_CDROM | AFPVOL_RO;
513 p = strtok(NULL, ",");
516 } else if (optionok(tmp, "cnidserver:", val)) {
517 setoption(options, save, VOLOPT_CNIDSERVER, val);
519 char *p = strrchr(val + 1, ':');
522 setoption(options, save, VOLOPT_CNIDPORT, p);
525 LOG(log_debug, logtype_afpd, "CNID Server for volume '%s': %s:%s",
526 volname, val + 1, p ? p + 1 : Cnid_port);
528 } else if (optionok(tmp, "dbpath:", val)) {
529 setoption(options, save, VOLOPT_DBPATH, val);
531 } else if (optionok(tmp, "umask:", val)) {
532 options[VOLOPT_UMASK].i_value = (int)strtol(val +1, NULL, 8);
533 } else if (optionok(tmp, "dperm:", val)) {
534 options[VOLOPT_DPERM].i_value = (int)strtol(val+1, NULL, 8);
535 } else if (optionok(tmp, "fperm:", val)) {
536 options[VOLOPT_FPERM].i_value = (int)strtol(val+1, NULL, 8);
537 } else if (optionok(tmp, "perm:", val)) {
538 options[VOLOPT_DFLTPERM].i_value = (int)strtol(val+1, NULL, 8);
539 } else if (optionok(tmp, "password:", val)) {
540 setoption(options, save, VOLOPT_PASSWORD, val);
544 /* this code allows forced uid/gid per volume settings */
545 } else if (optionok(tmp, "forceuid:", val)) {
546 setoption(options, save, VOLOPT_FORCEUID, val);
547 } else if (optionok(tmp, "forcegid:", val)) {
548 setoption(options, save, VOLOPT_FORCEGID, val);
550 #endif /* FORCE_UIDGID */
551 } else if (optionok(tmp, "root_preexec:", val)) {
552 setoption(options, save, VOLOPT_ROOTPREEXEC, val);
554 } else if (optionok(tmp, "preexec:", val)) {
555 setoption(options, save, VOLOPT_PREEXEC, val);
557 } else if (optionok(tmp, "root_postexec:", val)) {
558 setoption(options, save, VOLOPT_ROOTPOSTEXEC, val);
560 } else if (optionok(tmp, "postexec:", val)) {
561 setoption(options, save, VOLOPT_POSTEXEC, val);
563 } else if (optionok(tmp, "allowed_hosts:", val)) {
564 setoption(options, save, VOLOPT_ALLOWED_HOSTS, val);
566 } else if (optionok(tmp, "denied_hosts:", val)) {
567 setoption(options, save, VOLOPT_DENIED_HOSTS, val);
569 } else if (optionok(tmp, "ea:", val)) {
570 if (strcasecmp(val + 1, "ad") == 0)
571 options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_AD;
572 else if (strcasecmp(val + 1, "sys") == 0)
573 options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_SYS;
574 else if (strcasecmp(val + 1, "none") == 0)
575 options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_NONE;
577 } else if (optionok(tmp, "volsizelimit:", val)) {
578 options[VOLOPT_LIMITSIZE].i_value = (uint32_t)strtoul(val + 1, NULL, 10);
581 /* ignore unknown options */
582 LOG(log_debug, logtype_afpd, "ignoring unknown volume option: %s", tmp);
587 /* ----------------- */
588 static void showvol(const ucs2_t *name)
591 for ( volume = Volumes; volume; volume = volume->v_next ) {
592 if (volume->v_hide && !strcasecmp_w( volume->v_name, name ) ) {
599 /* ------------------------------- */
600 static int creatvol(AFPObj *obj, struct passwd *pwd,
601 char *path, char *name,
602 struct vol_option *options,
603 const int user /* user defined volume */
607 int suffixlen, vlen, tmpvlen, u8mvlen, macvlen;
609 char tmpname[AFPVOL_U8MNAMELEN+1];
610 ucs2_t u8mtmpname[(AFPVOL_U8MNAMELEN+1)*2], mactmpname[(AFPVOL_MACNAMELEN+1)*2];
611 char suffix[6]; /* max is #FFFF */
614 if ( name == NULL || *name == '\0' ) {
615 if ((name = strrchr( path, '/' )) == NULL) {
616 return -1; /* Obviously not a fully qualified path */
619 /* if you wish to share /, you need to specify a name. */
624 /* suffix for mangling use (lastvid + 1) */
625 /* because v_vid has not been decided yet. */
626 suffixlen = sprintf(suffix, "%c%X", MANGLE_CHAR, lastvid + 1 );
628 vlen = strlen( name );
630 /* Unicode Volume Name */
631 /* Firstly convert name from unixcharset to UTF8-MAC */
633 tmpvlen = convert_charset(obj->options.unixcharset, CH_UTF8_MAC, 0, name, vlen, tmpname, AFPVOL_U8MNAMELEN, &flags);
635 strcpy(tmpname, "???");
639 /* Do we have to mangle ? */
640 if ( (flags & CONV_REQMANGLE) || (tmpvlen > obj->options.volnamelen)) {
641 if (tmpvlen + suffixlen > obj->options.volnamelen) {
643 tmpvlen = convert_charset(obj->options.unixcharset, CH_UTF8_MAC, 0, name, vlen, tmpname, obj->options.volnamelen - suffixlen, &flags);
644 tmpname[tmpvlen >= 0 ? tmpvlen : 0] = 0;
646 strcat(tmpname, suffix);
647 tmpvlen = strlen(tmpname);
650 /* Secondly convert name from UTF8-MAC to UCS2 */
651 if ( 0 >= ( u8mvlen = convert_string(CH_UTF8_MAC, CH_UCS2, tmpname, tmpvlen, u8mtmpname, AFPVOL_U8MNAMELEN*2)) )
654 LOG(log_debug, logtype_afpd, "createvol: Volume '%s' -> UTF8-MAC Name: '%s'", name, tmpname);
656 /* Maccharset Volume Name */
657 /* Firsty convert name from unixcharset to maccharset */
659 tmpvlen = convert_charset(obj->options.unixcharset, obj->options.maccharset, 0, name, vlen, tmpname, AFPVOL_U8MNAMELEN, &flags);
661 strcpy(tmpname, "???");
665 /* Do we have to mangle ? */
666 if ( (flags & CONV_REQMANGLE) || (tmpvlen > AFPVOL_MACNAMELEN)) {
667 if (tmpvlen + suffixlen > AFPVOL_MACNAMELEN) {
669 tmpvlen = convert_charset(obj->options.unixcharset, obj->options.maccharset, 0, name, vlen, tmpname, AFPVOL_MACNAMELEN - suffixlen, &flags);
670 tmpname[tmpvlen >= 0 ? tmpvlen : 0] = 0;
672 strcat(tmpname, suffix);
673 tmpvlen = strlen(tmpname);
676 /* Secondly convert name from maccharset to UCS2 */
677 if ( 0 >= ( macvlen = convert_string(obj->options.maccharset, CH_UCS2, tmpname, tmpvlen, mactmpname, AFPVOL_U8MNAMELEN*2)) )
680 LOG(log_debug, logtype_afpd, "createvol: Volume '%s' -> Longname: '%s'", name, tmpname);
682 /* check duplicate */
683 for ( volume = Volumes; volume; volume = volume->v_next ) {
684 if (( strcasecmp_w( volume->v_u8mname, u8mtmpname ) == 0 ) || ( strcasecmp_w( volume->v_macname, mactmpname ) == 0 )){
685 if (volume->v_deleted) {
686 volume->v_new = hide = 1;
689 return -1; /* Won't be able to access it, anyway... */
694 if (!( volume = (struct vol *)calloc(1, sizeof( struct vol ))) ) {
695 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
698 if ( NULL == ( volume->v_localname = strdup(name))) {
699 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
704 if ( NULL == ( volume->v_u8mname = strdup_w(u8mtmpname))) {
705 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
710 if ( NULL == ( volume->v_macname = strdup_w(mactmpname))) {
711 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
716 if (!( volume->v_path = (char *)malloc( strlen( path ) + 1 )) ) {
717 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
723 volume->v_name = utf8_encoding()?volume->v_u8mname:volume->v_macname;
724 volume->v_hide = hide;
725 strcpy( volume->v_path, path );
729 #endif /* __svr4__ */
730 /* os X start at 1 and use network order ie. 1 2 3 */
731 volume->v_vid = ++lastvid;
732 volume->v_vid = htons(volume->v_vid);
734 if (check_vol_acl_support(volume))
735 volume->v_flags |= AFPVOL_ACLS;
740 volume->v_casefold = options[VOLOPT_CASEFOLD].i_value;
741 volume->v_flags |= options[VOLOPT_FLAGS].i_value;
743 if (options[VOLOPT_EA_VFS].i_value)
744 volume->v_vfs_ea = options[VOLOPT_EA_VFS].i_value;
746 volume->v_ad_options = 0;
747 if ((volume->v_flags & AFPVOL_NODEV))
748 volume->v_ad_options |= ADVOL_NODEV;
749 if ((volume->v_flags & AFPVOL_CACHE))
750 volume->v_ad_options |= ADVOL_CACHE;
751 if ((volume->v_flags & AFPVOL_UNIX_PRIV))
752 volume->v_ad_options |= ADVOL_UNIXPRIV;
753 if ((volume->v_flags & AFPVOL_INV_DOTS))
754 volume->v_ad_options |= ADVOL_INVDOTS;
755 if ((volume->v_flags & AFPVOL_NOADOUBLE))
756 volume->v_ad_options |= ADVOL_NOADOUBLE;
758 if (options[VOLOPT_PASSWORD].c_value)
759 volume->v_password = strdup(options[VOLOPT_PASSWORD].c_value);
761 if (options[VOLOPT_VETO].c_value)
762 volume->v_veto = strdup(options[VOLOPT_VETO].c_value);
764 if (options[VOLOPT_ENCODING].c_value)
765 volume->v_volcodepage = strdup(options[VOLOPT_ENCODING].c_value);
767 if (options[VOLOPT_MACCHARSET].c_value)
768 volume->v_maccodepage = strdup(options[VOLOPT_MACCHARSET].c_value);
770 if (options[VOLOPT_DBPATH].c_value)
771 volume->v_dbpath = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_DBPATH].c_value, pwd, path, name);
773 if (options[VOLOPT_CNIDSCHEME].c_value)
774 volume->v_cnidscheme = strdup(options[VOLOPT_CNIDSCHEME].c_value);
776 if (options[VOLOPT_CNIDSERVER].c_value)
777 volume->v_cnidserver = strdup(options[VOLOPT_CNIDSERVER].c_value);
779 if (options[VOLOPT_CNIDPORT].c_value)
780 volume->v_cnidport = strdup(options[VOLOPT_CNIDPORT].c_value);
782 if (options[VOLOPT_UMASK].i_value)
783 volume->v_umask = (mode_t)options[VOLOPT_UMASK].i_value;
785 if (options[VOLOPT_DPERM].i_value)
786 volume->v_dperm = (mode_t)options[VOLOPT_DPERM].i_value;
788 if (options[VOLOPT_FPERM].i_value)
789 volume->v_fperm = (mode_t)options[VOLOPT_FPERM].i_value;
791 if (options[VOLOPT_DFLTPERM].i_value)
792 volume->v_perm = (mode_t)options[VOLOPT_DFLTPERM].i_value;
794 if (options[VOLOPT_ADOUBLE].i_value)
795 volume->v_adouble = options[VOLOPT_ADOUBLE].i_value;
797 volume->v_adouble = AD_VERSION;
799 if (options[VOLOPT_LIMITSIZE].i_value)
800 volume->v_limitsize = options[VOLOPT_LIMITSIZE].i_value;
802 /* Mac to Unix conversion flags*/
803 volume->v_mtou_flags = 0;
804 if (!(volume->v_flags & AFPVOL_NOHEX))
805 volume->v_mtou_flags |= CONV_ESCAPEHEX;
806 if (!(volume->v_flags & AFPVOL_USEDOTS))
807 volume->v_mtou_flags |= CONV_ESCAPEDOTS;
808 if ((volume->v_flags & AFPVOL_EILSEQ))
809 volume->v_mtou_flags |= CONV__EILSEQ;
811 if ((volume->v_casefold & AFPVOL_MTOUUPPER))
812 volume->v_mtou_flags |= CONV_TOUPPER;
813 else if ((volume->v_casefold & AFPVOL_MTOULOWER))
814 volume->v_mtou_flags |= CONV_TOLOWER;
816 /* Unix to Mac conversion flags*/
817 volume->v_utom_flags = CONV_IGNORE | CONV_UNESCAPEHEX;
818 if ((volume->v_casefold & AFPVOL_UTOMUPPER))
819 volume->v_utom_flags |= CONV_TOUPPER;
820 else if ((volume->v_casefold & AFPVOL_UTOMLOWER))
821 volume->v_utom_flags |= CONV_TOLOWER;
823 if ((volume->v_flags & AFPVOL_EILSEQ))
824 volume->v_utom_flags |= CONV__EILSEQ;
827 if (options[VOLOPT_FORCEUID].c_value) {
828 volume->v_forceuid = strdup(options[VOLOPT_FORCEUID].c_value);
830 volume->v_forceuid = NULL; /* set as null so as to return 0 later on */
833 if (options[VOLOPT_FORCEGID].c_value) {
834 volume->v_forcegid = strdup(options[VOLOPT_FORCEGID].c_value);
836 volume->v_forcegid = NULL; /* set as null so as to return 0 later on */
840 if (options[VOLOPT_PREEXEC].c_value)
841 volume->v_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_PREEXEC].c_value, pwd, path, name);
842 volume->v_preexec_close = options[VOLOPT_PREEXEC].i_value;
844 if (options[VOLOPT_POSTEXEC].c_value)
845 volume->v_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_POSTEXEC].c_value, pwd, path, name);
847 if (options[VOLOPT_ROOTPREEXEC].c_value)
848 volume->v_root_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPREEXEC].c_value, pwd, path, name);
849 volume->v_root_preexec_close = options[VOLOPT_ROOTPREEXEC].i_value;
851 if (options[VOLOPT_ROOTPOSTEXEC].c_value)
852 volume->v_root_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPOSTEXEC].c_value, pwd, path, name);
855 volume->v_dperm |= volume->v_perm;
856 volume->v_fperm |= volume->v_perm;
858 /* Check EA support on volume */
859 if (volume->v_vfs_ea == AFPVOL_EA_AUTO)
860 check_ea_sys_support(volume);
863 /* get/store uuid from file */
864 if (volume->v_flags & AFPVOL_TM) {
865 char *uuid = get_uuid(obj, volume->v_localname);
867 LOG(log_error, logtype_afpd, "Volume '%s': couldn't get UUID",
868 volume->v_localname);
870 volume->v_uuid = uuid;
871 LOG(log_debug, logtype_afpd, "Volume '%s': UUID '%s'",
872 volume->v_localname, volume->v_uuid);
876 volume->v_next = Volumes;
881 /* ---------------- */
882 static char *myfgets( char *buf, int size, FILE *fp)
888 while ((EOF != ( c = getc( fp )) ) && ( size > 1 )) {
889 if ( c == '\n' || c == '\r' ) {
890 if (p != buf && *(p -1) == '\\') {
912 /* check access list. this function wants something of the following
914 * @group,name,name2,@group2,name3
916 * a NULL argument allows everybody to have access.
917 * we return three things:
919 * 0: list exists, but name isn't in it
923 #ifndef NO_REAL_USER_NAME
924 /* authentication is case insensitive
925 * FIXME should we do the same with group name?
927 #define access_strcmp strcasecmp
930 #define access_strcmp strcmp
934 static int accessvol(const char *args, const char *name)
936 char buf[MAXPATHLEN + 1], *p;
942 strlcpy(buf, args, sizeof(buf));
943 if ((p = strtok(buf, ",")) == NULL) /* nothing, return okay */
947 if (*p == '@') { /* it's a group */
948 if ((gr = getgrnam(p + 1)) && gmem(gr->gr_gid))
950 } else if (access_strcmp(p, name) == 0) /* it's a user name */
952 p = strtok(NULL, ",");
958 static int hostaccessvol(int type, const char *volname, const char *args, const AFPObj *obj)
961 char buf[MAXPATHLEN + 1], *p, *b;
962 DSI *dsi = obj->handle;
963 struct sockaddr_storage client;
968 strlcpy(buf, args, sizeof(buf));
969 if ((p = strtok_r(buf, ",", &b)) == NULL) /* nothing, return okay */
972 if (obj->proto != AFPPROTO_DSI)
977 char *ipaddr, *mask_char;
978 struct addrinfo hints, *ai;
980 ipaddr = strtok(p, "/");
981 mask_char = strtok(NULL,"/");
983 /* Get address from string with getaddrinfo */
984 memset(&hints, 0, sizeof hints);
985 hints.ai_family = AF_UNSPEC;
986 hints.ai_socktype = SOCK_STREAM;
987 if ((ret = getaddrinfo(ipaddr, NULL, &hints, &ai)) != 0) {
988 LOG(log_error, logtype_afpd, "hostaccessvol: getaddrinfo: %s\n", gai_strerror(ret));
993 if (mask_char != NULL)
994 mask_int = atoi(mask_char); /* apply_ip_mask does range checking on it */
996 if (ai->ai_family == AF_INET) /* IPv4 */
1002 /* Apply mask to addresses */
1003 client = dsi->client;
1004 apply_ip_mask((struct sockaddr *)&client, mask_int);
1005 apply_ip_mask(ai->ai_addr, mask_int);
1007 if (compare_ip((struct sockaddr *)&client, ai->ai_addr) == 0) {
1008 if (type == VOLOPT_DENIED_HOSTS)
1009 LOG(log_info, logtype_afpd, "AFP access denied for client IP '%s' to volume '%s' by denied list",
1010 getip_string((struct sockaddr *)&client), volname);
1017 p = strtok_r(NULL, ",", &b);
1020 if (type == VOLOPT_ALLOWED_HOSTS)
1021 LOG(log_info, logtype_afpd, "AFP access denied for client IP '%s' to volume '%s', not in allowed list",
1022 getip_string((struct sockaddr *)&dsi->client), volname);
1026 static void setextmap(char *ext, char *type, char *creator, int user)
1031 if (Extmap == NULL) {
1032 if (( Extmap = calloc(1, sizeof( struct extmap ))) == NULL ) {
1033 LOG(log_error, logtype_afpd, "setextmap: calloc: %s", strerror(errno) );
1038 for ( em = Extmap, cnt = 0; em->em_ext; em++, cnt++) {
1039 if ( (strdiacasecmp( em->em_ext, ext )) == 0 ) {
1044 if ( em->em_ext == NULL ) {
1045 if (!(Extmap = realloc( Extmap, sizeof( struct extmap ) * (cnt +2))) ) {
1046 LOG(log_error, logtype_afpd, "setextmap: realloc: %s", strerror(errno) );
1049 (Extmap +cnt +1)->em_ext = NULL;
1051 } else if ( !user ) {
1057 if (!(em->em_ext = strdup( ext))) {
1058 LOG(log_error, logtype_afpd, "setextmap: strdup: %s", strerror(errno) );
1062 if ( *type == '\0' ) {
1063 memcpy(em->em_type, "\0\0\0\0", sizeof( em->em_type ));
1065 memcpy(em->em_type, type, sizeof( em->em_type ));
1067 if ( *creator == '\0' ) {
1068 memcpy(em->em_creator, "\0\0\0\0", sizeof( em->em_creator ));
1070 memcpy(em->em_creator, creator, sizeof( em->em_creator ));
1074 /* -------------------------- */
1075 static int extmap_cmp(const void *map1, const void *map2)
1077 const struct extmap *em1 = map1;
1078 const struct extmap *em2 = map2;
1079 return strdiacasecmp(em1->em_ext, em2->em_ext);
1082 static void sortextmap( void)
1087 if ((em = Extmap) == NULL) {
1090 while (em->em_ext) {
1095 qsort(Extmap, Extmap_cnt, sizeof(struct extmap), extmap_cmp);
1096 if (*Extmap->em_ext == 0) {
1097 /* the first line is really "." the default entry,
1098 * we remove the leading '.' in setextmap
1105 /* ----------------------
1107 static void free_extmap( void)
1112 for ( em = Extmap; em->em_ext; em++) {
1122 /* ----------------------
1124 static int volfile_changed(struct afp_volume_name *p)
1130 name = p->full_name;
1134 if (!stat( name, &st) && st.st_mtime > p->mtime) {
1135 p->mtime = st.st_mtime;
1141 /* ----------------------
1142 * Read a volume configuration file and add the volumes contained within to
1143 * the global volume list. This gets called from the forked afpd childs.
1144 * The master now reads this too for Zeroconf announcements.
1146 * If p2 is non-NULL, the file that is opened is
1149 * Lines that begin with # and blank lines are ignored.
1150 * Volume lines are of the form:
1151 * <unix path> [<volume name>] [allow:<user>,<@group>,...] \
1152 * [codepage:<file>] [casefold:<num>]
1153 * <extension> TYPE [CREATOR]
1156 int readvolfile(AFPObj *obj, struct afp_volume_name *p1, char *p2, int user, struct passwd *pwent)
1159 char path[MAXPATHLEN + 1];
1160 char tmp[MAXPATHLEN + 1];
1161 char volname[AFPVOL_U8MNAMELEN + 1];
1163 char type[5], creator[5];
1168 struct vol_option save_options[VOLOPT_NUM];
1169 struct vol_option options[VOLOPT_NUM];
1175 strcpy( path, p1->name );
1177 strcat( path, "/" );
1179 if (p1->full_name) {
1180 free(p1->full_name);
1182 p1->full_name = strdup(path);
1185 if (NULL == ( fp = fopen( path, "r" )) ) {
1189 if (fd != -1 && !fstat( fd, &st) ) {
1190 p1->mtime = st.st_mtime;
1193 memset(save_options, 0, sizeof(save_options));
1195 /* Enable some default options for all volumes */
1196 save_options[VOLOPT_FLAGS].i_value |= AFPVOL_CACHE;
1197 save_options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_AUTO;
1198 LOG(log_maxdebug, logtype_afpd, "readvolfile: seeding default umask: %04o",
1199 obj->options.umask);
1200 save_options[VOLOPT_UMASK].i_value = obj->options.umask;
1202 while ( myfgets( buf, sizeof( buf ), fp ) != NULL ) {
1203 initline( strlen( buf ), buf );
1204 parseline( sizeof( path ) - 1, path );
1211 /* change the default options for this file */
1212 if (strncmp(path, VOLOPT_DEFAULT, VOLOPT_DEFAULT_LEN) == 0) {
1214 for (i = 0; i < VOLOPT_NUM; i++) {
1215 if (parseline( sizeof( path ) - VOLOPT_DEFAULT_LEN - 1,
1216 path + VOLOPT_DEFAULT_LEN) < 0)
1218 volset(save_options, NULL, tmp, sizeof(tmp) - 1,
1219 path + VOLOPT_DEFAULT_LEN);
1225 if (( p = strchr( path, '/' )) != NULL ) {
1233 if ( u == NULL || *u == '\0' || ( pw = getpwnam( u )) == NULL ) {
1236 strcpy( tmp, pw->pw_dir );
1237 if ( p != NULL && *p != '\0' ) {
1244 /* send path through variable substitution */
1245 if (*path != '~') /* need to copy path to tmp */
1247 if (!pwent && obj->username)
1248 pwent = getpwnam(obj->username);
1250 if (volxlate(obj, path, sizeof(path) - 1, tmp, pwent, NULL, NULL) == NULL)
1253 /* this is sort of braindead. basically, i want to be
1254 * able to specify things in any order, but i don't want to
1255 * re-write everything. */
1257 memcpy(options, save_options, sizeof(options));
1260 /* read in up to VOLOP_NUM possible options */
1261 for (i = 0; i < VOLOPT_NUM; i++) {
1262 if (parseline( sizeof( tmp ) - 1, tmp ) < 0)
1265 volset(options, save_options, volname, sizeof(volname) - 1, tmp);
1268 /* check allow/deny lists:
1269 allow -> either no list (-1), or in list (1)
1270 deny -> either no list (-1), or not in list (0) */
1271 if (accessvol(options[VOLOPT_ALLOW].c_value, obj->username) &&
1272 (accessvol(options[VOLOPT_DENY].c_value, obj->username) < 1) &&
1273 hostaccessvol(VOLOPT_ALLOWED_HOSTS, volname, options[VOLOPT_ALLOWED_HOSTS].c_value, obj) &&
1274 (hostaccessvol(VOLOPT_DENIED_HOSTS, volname, options[VOLOPT_DENIED_HOSTS].c_value, obj) < 1)) {
1276 /* handle read-only behaviour. semantics:
1277 * 1) neither the rolist nor the rwlist exist -> rw
1278 * 2) rolist exists -> ro if user is in it.
1279 * 3) rwlist exists -> ro unless user is in it. */
1280 if (((options[VOLOPT_FLAGS].i_value & AFPVOL_RO) == 0) &&
1281 ((accessvol(options[VOLOPT_ROLIST].c_value,
1282 obj->username) == 1) ||
1283 !accessvol(options[VOLOPT_RWLIST].c_value,
1285 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
1287 /* do variable substitution for volname */
1288 if (volxlate(obj, tmp, sizeof(tmp) - 1, volname, pwent, path, NULL) == NULL)
1291 creatvol(obj, pwent, path, tmp, options, p2 != NULL);
1293 volfree(options, save_options);
1297 parseline( sizeof( type ) - 1, type );
1298 parseline( sizeof( creator ) - 1, creator );
1299 setextmap( path, type, creator, user);
1306 volfree(save_options, NULL);
1308 if ( fclose( fp ) != 0 ) {
1309 LOG(log_error, logtype_afpd, "readvolfile: fclose: %s", strerror(errno) );
1315 /* ------------------------------- */
1316 static void volume_free(struct vol *vol)
1318 free(vol->v_localname);
1319 vol->v_localname = NULL;
1320 free(vol->v_u8mname);
1321 vol->v_u8mname = NULL;
1322 free(vol->v_macname);
1323 vol->v_macname = NULL;
1325 free(vol->v_password);
1327 free(vol->v_volcodepage);
1328 free(vol->v_maccodepage);
1329 free(vol->v_cnidscheme);
1330 free(vol->v_dbpath);
1333 free(vol->v_forceuid);
1334 free(vol->v_forcegid);
1335 #endif /* FORCE_UIDGID */
1340 /* ------------------------------- */
1341 static void free_volumes(void )
1344 struct vol *nvol, *ovol;
1346 for ( vol = Volumes; vol; vol = vol->v_next ) {
1347 if (( vol->v_flags & AFPVOL_OPEN ) ) {
1354 for ( vol = Volumes, ovol = NULL; vol; vol = nvol) {
1357 if (vol->v_localname == NULL) {
1358 if (Volumes == vol) {
1363 ovol->v_next = nvol;
1373 /* ------------------------------- */
1374 static void volume_unlink(struct vol *volume)
1376 struct vol *vol, *ovol, *nvol;
1378 if (volume == Volumes) {
1379 Volumes = Volumes->v_next;
1382 for ( vol = Volumes->v_next, ovol = Volumes; vol; vol = nvol) {
1385 if (vol == volume) {
1386 ovol->v_next = nvol;
1395 static int getvolspace(struct vol *vol,
1396 u_int32_t *bfree, u_int32_t *btotal,
1397 VolSpace *xbfree, VolSpace *xbtotal, u_int32_t *bsize)
1401 #ifndef NO_QUOTA_SUPPORT
1402 VolSpace qfree, qtotal;
1405 spaceflag = AFPVOL_GVSMASK & vol->v_flags;
1406 /* report up to 2GB if afp version is < 2.2 (4GB if not) */
1407 maxsize = (vol->v_flags & AFPVOL_A2VOL) ? 0x01fffe00 :
1408 (((afp_version < 22) || (vol->v_flags & AFPVOL_LIMITSIZE))
1409 ? 0x7fffffffL : 0xffffffffL);
1412 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_AFSGVS ) {
1413 if ( afs_getvolspace( vol, xbfree, xbtotal, bsize ) == AFP_OK ) {
1414 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_AFSGVS;
1415 goto getvolspace_done;
1420 if (( rc = ustatfs_getvolspace( vol, xbfree, xbtotal, bsize)) != AFP_OK ) {
1424 #define min(a,b) ((a)<(b)?(a):(b))
1425 #ifndef NO_QUOTA_SUPPORT
1426 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_UQUOTA ) {
1427 if ( uquota_getvolspace( vol, &qfree, &qtotal, *bsize ) == AFP_OK ) {
1428 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_UQUOTA;
1429 *xbfree = min(*xbfree, qfree);
1430 *xbtotal = min( *xbtotal, qtotal);
1431 goto getvolspace_done;
1435 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_USTATFS;
1438 if (vol->v_limitsize) {
1439 /* FIXME: Free could be limit minus (total minus used), */
1440 /* which will confuse the client less ? */
1441 *xbfree = min(*xbfree, (vol->v_limitsize * 1024 * 1024));
1442 *xbtotal = min(*xbtotal, (vol->v_limitsize * 1024 * 1024));
1445 *bfree = min( *xbfree, maxsize);
1446 *btotal = min( *xbtotal, maxsize);
1450 /* -----------------------
1451 * set volume creation date
1452 * avoid duplicate, well at least it tries
1454 static void vol_setdate(u_int16_t id, struct adouble *adp, time_t date)
1457 struct vol *vol = Volumes;
1459 for ( volume = Volumes; volume; volume = volume->v_next ) {
1460 if (volume->v_vid == id) {
1463 else if ((time_t)(AD_DATE_FROM_UNIX(date)) == volume->v_ctime) {
1465 volume = Volumes; /* restart */
1468 vol->v_ctime = AD_DATE_FROM_UNIX(date);
1469 ad_setdate(adp, AD_DATE_CREATE | AD_DATE_UNIX, date);
1472 /* ----------------------- */
1473 static int getvolparams( u_int16_t bitmap, struct vol *vol, struct stat *st, char *buf, size_t *buflen)
1476 int bit = 0, isad = 1;
1479 u_int32_t bfree, btotal, bsize;
1480 VolSpace xbfree, xbtotal; /* extended bytes */
1481 char *data, *nameoff = NULL;
1484 LOG(log_debug, logtype_afpd, "getvolparams: Volume '%s'", vol->v_localname);
1486 /* courtesy of jallison@whistle.com:
1487 * For MacOS8.x support we need to create the
1488 * .Parent file here if it doesn't exist. */
1490 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1491 if ( ad_open_metadata( vol->v_path, ADFLAGS_DIR, O_CREAT, &ad) < 0 ) {
1493 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1495 } else if (ad_get_MD_flags( &ad ) & O_CREAT) {
1496 slash = strrchr( vol->v_path, '/' );
1500 slash = vol->v_path;
1501 if (ad_getentryoff(&ad, ADEID_NAME)) {
1502 ad_setentrylen( &ad, ADEID_NAME, strlen( slash ));
1503 memcpy(ad_entry( &ad, ADEID_NAME ), slash,
1504 ad_getentrylen( &ad, ADEID_NAME ));
1506 vol_setdate(vol->v_vid, &ad, st->st_mtime);
1510 if (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0)
1511 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1513 vol->v_ctime = aint;
1516 if (( bitmap & ( (1<<VOLPBIT_BFREE)|(1<<VOLPBIT_BTOTAL) |
1517 (1<<VOLPBIT_XBFREE)|(1<<VOLPBIT_XBTOTAL) |
1518 (1<<VOLPBIT_BSIZE)) ) != 0 ) {
1519 if ( getvolspace( vol, &bfree, &btotal, &xbfree, &xbtotal,
1520 &bsize) != AFP_OK ) {
1522 ad_close( &ad, ADFLAGS_HF );
1524 return( AFPERR_PARAM );
1529 while ( bitmap != 0 ) {
1530 while (( bitmap & 1 ) == 0 ) {
1538 /* check for read-only.
1539 * NOTE: we don't actually set the read-only flag unless
1540 * it's passed in that way as it's possible to mount
1541 * a read-write filesystem under a read-only one. */
1542 if ((vol->v_flags & AFPVOL_RO) ||
1543 ((utime(vol->v_path, NULL) < 0) && (errno == EROFS))) {
1544 ashort |= VOLPBIT_ATTR_RO;
1546 /* prior 2.1 only VOLPBIT_ATTR_RO is defined */
1547 if (afp_version > 20) {
1548 if (0 == (vol->v_flags & AFPVOL_NOFILEID) && vol->v_cdb != NULL &&
1549 (vol->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1550 ashort |= VOLPBIT_ATTR_FILEID;
1552 ashort |= VOLPBIT_ATTR_CATSEARCH;
1554 if (afp_version >= 30) {
1555 ashort |= VOLPBIT_ATTR_UTF8;
1556 if (vol->v_flags & AFPVOL_UNIX_PRIV)
1557 ashort |= VOLPBIT_ATTR_UNIXPRIV;
1558 if (vol->v_flags & AFPVOL_TM)
1559 ashort |= VOLPBIT_ATTR_TM;
1561 if (afp_version >= 32) {
1563 ashort |= VOLPBIT_ATTR_EXT_ATTRS;
1564 if (vol->v_flags & AFPVOL_ACLS)
1565 ashort |= VOLPBIT_ATTR_ACLS;
1569 ashort = htons(ashort);
1570 memcpy(data, &ashort, sizeof( ashort ));
1571 data += sizeof( ashort );
1575 ashort = htons( AFPVOLSIG_DEFAULT );
1576 memcpy(data, &ashort, sizeof( ashort ));
1577 data += sizeof( ashort );
1580 case VOLPBIT_CDATE :
1581 aint = vol->v_ctime;
1582 memcpy(data, &aint, sizeof( aint ));
1583 data += sizeof( aint );
1586 case VOLPBIT_MDATE :
1587 if ( st->st_mtime > vol->v_mtime ) {
1588 vol->v_mtime = st->st_mtime;
1590 aint = AD_DATE_FROM_UNIX(vol->v_mtime);
1591 memcpy(data, &aint, sizeof( aint ));
1592 data += sizeof( aint );
1595 case VOLPBIT_BDATE :
1596 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1597 aint = AD_DATE_START;
1598 memcpy(data, &aint, sizeof( aint ));
1599 data += sizeof( aint );
1603 memcpy(data, &vol->v_vid, sizeof( vol->v_vid ));
1604 data += sizeof( vol->v_vid );
1607 case VOLPBIT_BFREE :
1608 bfree = htonl( bfree );
1609 memcpy(data, &bfree, sizeof( bfree ));
1610 data += sizeof( bfree );
1613 case VOLPBIT_BTOTAL :
1614 btotal = htonl( btotal );
1615 memcpy(data, &btotal, sizeof( btotal ));
1616 data += sizeof( btotal );
1619 #ifndef NO_LARGE_VOL_SUPPORT
1620 case VOLPBIT_XBFREE :
1621 xbfree = hton64( xbfree );
1622 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1623 bcopy(&xbfree, data, sizeof(xbfree));
1624 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1625 memcpy(data, &xbfree, sizeof( xbfree ));
1626 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1627 data += sizeof( xbfree );
1630 case VOLPBIT_XBTOTAL :
1631 xbtotal = hton64( xbtotal );
1632 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1633 bcopy(&xbtotal, data, sizeof(xbtotal));
1634 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1635 memcpy(data, &xbtotal, sizeof( xbtotal ));
1636 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1637 data += sizeof( xbfree );
1639 #endif /* ! NO_LARGE_VOL_SUPPORT */
1643 data += sizeof( u_int16_t );
1646 case VOLPBIT_BSIZE: /* block size */
1647 bsize = htonl(bsize);
1648 memcpy(data, &bsize, sizeof(bsize));
1649 data += sizeof(bsize);
1654 ad_close( &ad, ADFLAGS_HF );
1656 return( AFPERR_BITMAP );
1662 ashort = htons( data - buf );
1663 memcpy(nameoff, &ashort, sizeof( ashort ));
1664 /* name is always in mac charset */
1665 aint = ucs2_to_charset( vol->v_maccharset, vol->v_macname, data+1, AFPVOL_MACNAMELEN + 1);
1675 ad_close_metadata( &ad);
1677 *buflen = data - buf;
1681 /* ------------------------- */
1682 static int stat_vol(u_int16_t bitmap, struct vol *vol, char *rbuf, size_t *rbuflen)
1688 if ( stat( vol->v_path, &st ) < 0 ) {
1690 return( AFPERR_PARAM );
1692 /* save the volume device number */
1693 vol->v_dev = st.st_dev;
1695 buflen = *rbuflen - sizeof( bitmap );
1696 if (( ret = getvolparams( bitmap, vol, &st,
1697 rbuf + sizeof( bitmap ), &buflen )) != AFP_OK ) {
1701 *rbuflen = buflen + sizeof( bitmap );
1702 bitmap = htons( bitmap );
1703 memcpy(rbuf, &bitmap, sizeof( bitmap ));
1708 /* ------------------------------- */
1709 void load_volumes(AFPObj *obj)
1711 struct passwd *pwent;
1716 /* check files date */
1717 if (obj->options.defaultvol.loaded) {
1718 changed = volfile_changed(&obj->options.defaultvol);
1720 if (obj->options.systemvol.loaded) {
1721 changed |= volfile_changed(&obj->options.systemvol);
1723 if (obj->options.uservol.loaded) {
1724 changed |= volfile_changed(&obj->options.uservol);
1733 if (! ((DSI *)obj->handle)->child) {
1734 LOG(log_debug, logtype_afpd, "load_volumes: AFP MASTER");
1736 LOG(log_debug, logtype_afpd, "load_volumes: user: %s", obj->username);
1739 pwent = getpwnam(obj->username);
1740 if ( (obj->options.flags & OPTION_USERVOLFIRST) == 0 ) {
1741 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent);
1744 if ((*obj->username == '\0') || (obj->options.flags & OPTION_NOUSERVOL)) {
1745 readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent);
1748 * Read user's AppleVolumes or .AppleVolumes file
1749 * If neither are readable, read the default volumes file. if
1750 * that doesn't work, create a user share.
1752 if (obj->options.uservol.name) {
1753 free(obj->options.uservol.name);
1755 obj->options.uservol.name = strdup(pwent->pw_dir);
1756 if ( readvolfile(obj, &obj->options.uservol, "AppleVolumes", 1, pwent) < 0 &&
1757 readvolfile(obj, &obj->options.uservol, ".AppleVolumes", 1, pwent) < 0 &&
1758 readvolfile(obj, &obj->options.uservol, "applevolumes", 1, pwent) < 0 &&
1759 readvolfile(obj, &obj->options.uservol, ".applevolumes", 1, pwent) < 0 &&
1760 obj->options.defaultvol.name != NULL ) {
1761 if (readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent) < 0)
1762 creatvol(obj, pwent, pwent->pw_dir, NULL, NULL, 1);
1765 if ( obj->options.flags & OPTION_USERVOLFIRST ) {
1766 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent );
1769 if ( obj->options.closevol ) {
1772 for ( vol = Volumes; vol; vol = vol->v_next ) {
1773 if (vol->v_deleted && !vol->v_new ) {
1782 /* ------------------------------- */
1783 int afp_getsrvrparms(AFPObj *obj, char *ibuf _U_, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
1796 for ( vcnt = 0, volume = Volumes; volume; volume = volume->v_next ) {
1797 if (!(volume->v_flags & AFPVOL_NOSTAT)) {
1800 if ( stat( volume->v_path, &st ) < 0 ) {
1801 LOG(log_info, logtype_afpd, "afp_getsrvrparms(%s): stat: %s",
1802 volume->v_path, strerror(errno) );
1803 continue; /* can't access directory */
1805 if (!S_ISDIR(st.st_mode)) {
1806 continue; /* not a dir */
1808 accessmode(volume->v_path, &ma, NULL, &st);
1809 if ((ma.ma_user & (AR_UREAD | AR_USEARCH)) != (AR_UREAD | AR_USEARCH)) {
1810 continue; /* no r-x access */
1813 if (volume->v_hide) {
1814 continue; /* config file changed but the volume was mounted */
1817 if (utf8_encoding()) {
1818 len = ucs2_to_charset_allocate(CH_UTF8_MAC, &namebuf, volume->v_u8mname);
1820 len = ucs2_to_charset_allocate(obj->options.maccharset, &namebuf, volume->v_macname);
1823 if (len == (size_t)-1)
1826 /* set password bit if there's a volume password */
1827 *data = (volume->v_password) ? AFPSRVR_PASSWD : 0;
1829 /* Apple 2 clients running ProDOS-8 expect one volume to have
1830 bit 0 of this byte set. They will not recognize anything
1831 on the server unless this is the case. I have not
1832 completely worked this out, but it's related to booting
1833 from the server. Support for that function is a ways
1834 off.. <shirsch@ibm.net> */
1835 *data |= (volume->v_flags & AFPVOL_A2VOL) ? AFPSRVR_CONFIGINFO : 0;
1836 *data++ |= 0; /* UNIX PRIVS BIT ..., OSX doesn't seem to use it, so we don't either */
1838 memcpy(data, namebuf, len );
1844 *rbuflen = data - rbuf;
1846 if ( gettimeofday( &tv, NULL ) < 0 ) {
1847 LOG(log_error, logtype_afpd, "afp_getsrvrparms(%s): gettimeofday: %s", volume->v_path, strerror(errno) );
1849 return AFPERR_PARAM;
1851 tv.tv_sec = AD_DATE_FROM_UNIX(tv.tv_sec);
1852 memcpy(data, &tv.tv_sec, sizeof( u_int32_t));
1853 data += sizeof( u_int32_t);
1858 /* ------------------------- */
1859 static int volume_codepage(AFPObj *obj, struct vol *volume)
1861 struct charset_functions *charset;
1864 if (!volume->v_volcodepage)
1865 volume->v_volcodepage = strdup("UTF8");
1867 if ( (charset_t) -1 == ( volume->v_volcharset = add_charset(volume->v_volcodepage)) ) {
1868 LOG (log_error, logtype_afpd, "Setting codepage %s as volume codepage failed", volume->v_volcodepage);
1872 if ( NULL == (charset = find_charset_functions(volume->v_volcodepage)) || charset->flags & CHARSET_ICONV ) {
1873 LOG (log_warning, logtype_afpd, "WARNING: volume encoding %s is *not* supported by netatalk, expect problems !!!!", volume->v_volcodepage);
1876 if (!volume->v_maccodepage)
1877 volume->v_maccodepage = strdup(obj->options.maccodepage);
1879 if ( (charset_t) -1 == ( volume->v_maccharset = add_charset(volume->v_maccodepage)) ) {
1880 LOG (log_error, logtype_afpd, "Setting codepage %s as mac codepage failed", volume->v_maccodepage);
1884 if ( NULL == ( charset = find_charset_functions(volume->v_maccodepage)) || ! (charset->flags & CHARSET_CLIENT) ) {
1885 LOG (log_error, logtype_afpd, "Fatal error: mac charset %s not supported", volume->v_maccodepage);
1888 volume->v_kTextEncoding = htonl(charset->kTextEncoding);
1892 /* ------------------------- */
1893 static int volume_openDB(struct vol *volume)
1897 if ((volume->v_flags & AFPVOL_NODEV)) {
1898 flags |= CNID_FLAG_NODEV;
1901 if (volume->v_cnidscheme == NULL) {
1902 volume->v_cnidscheme = strdup(DEFAULT_CNID_SCHEME);
1903 LOG(log_info, logtype_afpd, "Volume %s use CNID scheme %s.",
1904 volume->v_path, volume->v_cnidscheme);
1907 LOG(log_info, logtype_afpd, "CNID server: %s:%s",
1908 volume->v_cnidserver ? volume->v_cnidserver : Cnid_srv,
1909 volume->v_cnidport ? volume->v_cnidport : Cnid_port);
1912 /* Found this in branch dir-rewrite, maybe we want to use it sometimes */
1914 /* Legacy pre 2.1 way of sharing eg CD-ROM */
1915 if (strcmp(volume->v_cnidscheme, "last") == 0) {
1916 /* "last" is gone. We support it by switching to in-memory "tdb" */
1917 volume->v_cnidscheme = strdup("tdb");
1918 flags |= CNID_FLAG_MEMORY;
1921 /* New way of sharing CD-ROM */
1922 if (volume->v_flags & AFPVOL_CDROM) {
1923 flags |= CNID_FLAG_MEMORY;
1924 if (strcmp(volume->v_cnidscheme, "tdb") != 0) {
1925 free(volume->v_cnidscheme);
1926 volume->v_cnidscheme = strdup("tdb");
1927 LOG(log_info, logtype_afpd, "Volume %s is ejectable, switching to scheme %s.",
1928 volume->v_path, volume->v_cnidscheme);
1933 volume->v_cdb = cnid_open(volume->v_dbpath ? volume->v_dbpath : volume->v_path,
1935 volume->v_cnidscheme,
1937 volume->v_cnidserver ? volume->v_cnidserver : Cnid_srv,
1938 volume->v_cnidport ? volume->v_cnidport : Cnid_port);
1940 if ( ! volume->v_cdb && ! (flags & CNID_FLAG_MEMORY)) {
1941 /* The first attempt failed and it wasn't yet an attempt to open in-memory */
1942 LOG(log_error, logtype_afpd, "Can't open volume \"%s\" CNID backend \"%s\" ",
1943 volume->v_path, volume->v_cnidscheme);
1944 LOG(log_error, logtype_afpd, "Reopen volume %s using in memory temporary CNID DB.",
1946 flags |= CNID_FLAG_MEMORY;
1947 volume->v_cdb = cnid_open (volume->v_path, volume->v_umask, "tdb", flags, NULL, NULL);
1949 /* kill ourself with SIGUSR2 aka msg pending */
1950 if (volume->v_cdb) {
1951 setmessage("Something wrong with the volume's CNID DB, using temporary CNID DB instead."
1952 "Check server messages for details!");
1953 kill(getpid(), SIGUSR2);
1954 /* deactivate cnid caching/storing in AppleDouble files */
1955 volume->v_flags &= ~AFPVOL_CACHE;
1960 return (!volume->v_cdb)?-1:0;
1964 Check if the underlying filesystem supports EAs for ea:sys volumes.
1965 If not, switch to ea:ad.
1966 As we can't check (requires write access) on ro-volumes, we switch ea:auto
1967 volumes that are options:ro to ea:none.
1969 static void check_ea_sys_support(struct vol *vol)
1971 uid_t process_uid = 0;
1972 char eaname[] = {"org.netatalk.supports-eas.XXXXXX"};
1973 const char *eacontent = "yes";
1975 if (vol->v_vfs_ea == AFPVOL_EA_AUTO) {
1977 if ((vol->v_flags & AFPVOL_RO) == AFPVOL_RO) {
1978 LOG(log_info, logtype_logger, "read-only volume '%s', can't test for EA support, disabling EAs", vol->v_localname);
1979 vol->v_vfs_ea = AFPVOL_EA_NONE;
1985 process_uid = geteuid();
1987 if (seteuid(0) == -1) {
1988 LOG(log_error, logtype_logger, "check_ea_sys_support: can't seteuid(0): %s", strerror(errno));
1992 if ((sys_setxattr(vol->v_path, eaname, eacontent, 4, 0)) == 0) {
1993 sys_removexattr(vol->v_path, eaname);
1994 vol->v_vfs_ea = AFPVOL_EA_SYS;
1996 LOG(log_warning, logtype_afpd, "volume \"%s\" does not support Extended Attributes, using ea:ad instead",
1998 vol->v_vfs_ea = AFPVOL_EA_AD;
2002 if (seteuid(process_uid) == -1) {
2003 LOG(log_error, logtype_logger, "can't seteuid back %s", strerror(errno));
2010 /* -------------------------
2011 * we are the user here
2013 int afp_openvol(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2024 char path[ MAXPATHLEN + 1];
2030 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2031 bitmap = ntohs( bitmap );
2032 ibuf += sizeof( bitmap );
2035 if (( bitmap & (1<<VOLPBIT_VID)) == 0 ) {
2036 return AFPERR_BITMAP;
2039 len = (unsigned char)*ibuf++;
2040 volname = obj->oldtmp;
2042 if ((volname_tmp = strchr(volname,'+')) != NULL)
2043 volname = volname_tmp+1;
2045 if (utf8_encoding()) {
2046 namelen = convert_string(CH_UTF8_MAC, CH_UCS2, ibuf, len, volname, sizeof(obj->oldtmp));
2048 namelen = convert_string(obj->options.maccharset, CH_UCS2, ibuf, len, volname, sizeof(obj->oldtmp));
2051 if ( namelen <= 0) {
2052 return AFPERR_PARAM;
2056 if ((len + 1) & 1) /* pad to an even boundary */
2061 for ( volume = Volumes; volume; volume = volume->v_next ) {
2062 if ( strcasecmp_w( (ucs2_t*) volname, volume->v_name ) == 0 ) {
2067 if ( volume == NULL ) {
2068 return AFPERR_PARAM;
2071 /* check for a volume password */
2072 if (volume->v_password && strncmp(ibuf, volume->v_password, VOLPASSLEN)) {
2073 return AFPERR_ACCESS;
2076 if (( volume->v_flags & AFPVOL_OPEN ) ) {
2077 /* the volume is already open */
2079 set_uidgid ( volume );
2081 return stat_vol(bitmap, volume, rbuf, rbuflen);
2084 if (volume->v_root_preexec) {
2085 if ((ret = afprun(1, volume->v_root_preexec, NULL)) && volume->v_root_preexec_close) {
2086 LOG(log_error, logtype_afpd, "afp_openvol(%s): root preexec : %d", volume->v_path, ret );
2092 set_uidgid ( volume );
2095 if (volume->v_preexec) {
2096 if ((ret = afprun(0, volume->v_preexec, NULL)) && volume->v_preexec_close) {
2097 LOG(log_error, logtype_afpd, "afp_openvol(%s): preexec : %d", volume->v_path, ret );
2102 if ( stat( volume->v_path, &st ) < 0 ) {
2103 return AFPERR_PARAM;
2106 if ( chdir( volume->v_path ) < 0 ) {
2107 return AFPERR_PARAM;
2110 if ( NULL == getcwd(path, MAXPATHLEN)) {
2111 /* shouldn't be fatal but it will fail later */
2112 LOG(log_error, logtype_afpd, "afp_openvol(%s): volume pathlen too long", volume->v_path);
2116 /* Normalize volume path */
2117 #ifdef REALPATH_TAKES_NULL
2118 if ((volume->v_path = realpath(path, NULL)) == NULL)
2121 if ((volume->v_path = malloc(MAXPATHLEN+1)) == NULL)
2123 if (realpath(path, volume->v_path) == NULL) {
2124 free(volume->v_path);
2127 /* Safe some memory */
2129 if ((tmp = strdup(volume->v_path)) == NULL) {
2130 free(volume->v_path);
2133 free(volume->v_path);
2134 volume->v_path = tmp;
2137 if (volume_codepage(obj, volume) < 0) {
2142 /* initialize volume variables
2145 if (utf8_encoding()) {
2146 volume->max_filename = 255;
2149 volume->max_filename = MACFILELEN;
2152 volume->v_flags |= AFPVOL_OPEN;
2153 volume->v_cdb = NULL;
2155 if (utf8_encoding()) {
2156 len = convert_string_allocate(CH_UCS2, CH_UTF8_MAC, volume->v_u8mname, namelen, &vol_mname);
2158 len = convert_string_allocate(CH_UCS2, obj->options.maccharset, volume->v_macname, namelen, &vol_mname);
2160 if ( !vol_mname || len <= 0) {
2165 if ((vol_uname = strrchr(path, '/')) == NULL)
2167 else if (*(vol_uname + 1) != '\0')
2170 if ((dir = dir_new(vol_mname,
2175 bfromcstr(volume->v_path),
2179 LOG(log_error, logtype_afpd, "afp_openvol(%s): malloc: %s", volume->v_path, strerror(errno) );
2184 volume->v_root = dir;
2187 if (volume_openDB(volume) < 0) {
2188 LOG(log_error, logtype_afpd, "Fatal error: cannot open CNID or invalid CNID backend for %s: %s",
2189 volume->v_path, volume->v_cnidscheme);
2194 ret = stat_vol(bitmap, volume, rbuf, rbuflen);
2195 if (ret == AFP_OK) {
2197 if (!(volume->v_flags & AFPVOL_RO)) {
2198 handle_special_folders( volume );
2200 volume->v_cnidserver ? volume->v_cnidserver : Cnid_srv,
2201 volume->v_cnidport ? volume->v_cnidport : Cnid_port);
2205 * If you mount a volume twice, the second time the trash appears on
2206 * the desk-top. That's because the Mac remembers the DID for the
2207 * trash (even for volumes in different zones, on different servers).
2208 * Just so this works better, we prime the DID cache with the trash,
2209 * fixing the trash at DID 17.
2210 * FIXME (RL): should it be done inside a CNID backend ? (always returning Trash DID when asked) ?
2212 if ((volume->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
2214 /* FIXME find db time stamp */
2215 if (cnid_getstamp(volume->v_cdb, volume->v_stamp, sizeof(volume->v_stamp)) < 0) {
2216 LOG (log_error, logtype_afpd,
2217 "afp_openvol(%s): Fatal error: Unable to get stamp value from CNID backend",
2225 cname( volume, volume->v_root, &p );
2231 if (volume->v_root) {
2232 dir_free( volume->v_root );
2233 volume->v_root = NULL;
2236 volume->v_flags &= ~AFPVOL_OPEN;
2237 if (volume->v_cdb != NULL) {
2238 cnid_close(volume->v_cdb);
2239 volume->v_cdb = NULL;
2245 /* ------------------------- */
2246 static void closevol(struct vol *vol)
2251 dir_free( vol->v_root );
2253 if (vol->v_cdb != NULL) {
2254 cnid_close(vol->v_cdb);
2258 if (vol->v_postexec) {
2259 afprun(0, vol->v_postexec, NULL);
2261 if (vol->v_root_postexec) {
2262 afprun(1, vol->v_root_postexec, NULL);
2266 /* ------------------------- */
2267 void close_all_vol(void)
2271 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
2272 if ( (ovol->v_flags & AFPVOL_OPEN) ) {
2273 ovol->v_flags &= ~AFPVOL_OPEN;
2279 /* ------------------------- */
2280 static void deletevol(struct vol *vol)
2284 vol->v_flags &= ~AFPVOL_OPEN;
2285 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
2286 if ( (ovol->v_flags & AFPVOL_OPEN) ) {
2290 if ( ovol != NULL ) {
2291 /* Even if chdir fails, we can't say afp_closevol fails. */
2292 if ( chdir( ovol->v_path ) == 0 ) {
2293 curdir = ovol->v_root;
2298 if (vol->v_deleted) {
2299 showvol(vol->v_name);
2306 /* ------------------------- */
2307 int afp_closevol(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2314 memcpy(&vid, ibuf, sizeof( vid ));
2315 if (NULL == ( vol = getvolbyvid( vid )) ) {
2316 return( AFPERR_PARAM );
2325 /* ------------------------- */
2326 struct vol *getvolbyvid(const u_int16_t vid )
2330 for ( vol = Volumes; vol; vol = vol->v_next ) {
2331 if ( vid == vol->v_vid ) {
2335 if ( vol == NULL || ( vol->v_flags & AFPVOL_OPEN ) == 0 ) {
2341 #endif /* FORCE_UIDGID */
2348 /* ------------------------ */
2349 static int ext_cmp_key(const void *key, const void *obj)
2351 const char *p = key;
2352 const struct extmap *em = obj;
2353 return strdiacasecmp(p, em->em_ext);
2355 struct extmap *getextmap(const char *path)
2360 if (!Extmap_cnt || NULL == ( p = strrchr( path, '.' )) ) {
2361 return( Defextmap );
2365 return( Defextmap );
2367 em = bsearch(p, Extmap, Extmap_cnt, sizeof(struct extmap), ext_cmp_key);
2371 return( Defextmap );
2375 /* ------------------------- */
2376 struct extmap *getdefextmap(void)
2378 return( Defextmap );
2381 /* --------------------------
2382 poll if a volume is changed by other processes.
2384 0 no attention msg sent
2385 1 attention msg sent
2386 -1 error (socket closed)
2388 Note: if attention return -1 no packet has been
2389 sent because the buffer is full, we don't care
2390 either there's no reader or there's a lot of
2391 traffic and another pollvoltime will follow
2393 int pollvoltime(AFPObj *obj)
2400 if (!(afp_version > 21 && obj->options.server_notif))
2403 if ( gettimeofday( &tv, NULL ) < 0 )
2406 for ( vol = Volumes; vol; vol = vol->v_next ) {
2407 if ( (vol->v_flags & AFPVOL_OPEN) && vol->v_mtime + 30 < tv.tv_sec) {
2408 if ( !stat( vol->v_path, &st ) && vol->v_mtime != st.st_mtime ) {
2409 vol->v_mtime = st.st_mtime;
2410 if (!obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED))
2419 /* ------------------------- */
2420 void setvoltime(AFPObj *obj, struct vol *vol)
2424 /* just looking at vol->v_mtime is broken seriously since updates
2425 * from other users afpd processes never are seen.
2426 * This is not the most elegant solution (a shared memory between
2427 * the afpd processes would come closer)
2430 if ( gettimeofday( &tv, NULL ) < 0 ) {
2431 LOG(log_error, logtype_afpd, "setvoltime(%s): gettimeofday: %s", vol->v_path, strerror(errno) );
2434 if( utime( vol->v_path, NULL ) < 0 ) {
2435 /* write of time failed ... probably a read only filesys,
2436 * where no other users can interfere, so there's no issue here
2440 /* a little granularity */
2441 if (vol->v_mtime < tv.tv_sec) {
2442 vol->v_mtime = tv.tv_sec;
2443 /* or finder doesn't update free space
2444 * AFP 3.2 and above clients seem to be ok without so many notification
2446 if (afp_version < 32 && obj->options.server_notif) {
2447 obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED);
2452 /* ------------------------- */
2453 int afp_getvolparams(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_,char *rbuf, size_t *rbuflen)
2456 u_int16_t vid, bitmap;
2459 memcpy(&vid, ibuf, sizeof( vid ));
2460 ibuf += sizeof( vid );
2461 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2462 bitmap = ntohs( bitmap );
2464 if (NULL == ( vol = getvolbyvid( vid )) ) {
2466 return( AFPERR_PARAM );
2469 return stat_vol(bitmap, vol, rbuf, rbuflen);
2472 /* ------------------------- */
2473 int afp_setvolparams(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2477 u_int16_t vid, bitmap;
2483 memcpy(&vid, ibuf, sizeof( vid ));
2484 ibuf += sizeof( vid );
2485 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2486 bitmap = ntohs( bitmap );
2487 ibuf += sizeof(bitmap);
2489 if (( vol = getvolbyvid( vid )) == NULL ) {
2490 return( AFPERR_PARAM );
2493 if ((vol->v_flags & AFPVOL_RO))
2494 return AFPERR_VLOCK;
2496 /* we can only set the backup date. */
2497 if (bitmap != (1 << VOLPBIT_BDATE))
2498 return AFPERR_BITMAP;
2500 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2501 if ( ad_open( vol->v_path, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR,
2504 return AFPERR_VLOCK;
2506 return AFPERR_ACCESS;
2509 memcpy(&aint, ibuf, sizeof(aint));
2510 ad_setdate(&ad, AD_DATE_BACKUP, aint);
2512 ad_close(&ad, ADFLAGS_HF);
2516 /* ------------------------- */
2517 int wincheck(const struct vol *vol, const char *path)
2521 if (!(vol->v_flags & AFPVOL_MSWINDOWS))
2524 /* empty paths are not allowed */
2525 if ((len = strlen(path)) == 0)
2528 /* leading or trailing whitespaces are not allowed, carriage returns
2529 * and probably other whitespace is okay, tabs are not allowed
2531 if ((path[0] == ' ') || (path[len-1] == ' '))
2534 /* certain characters are not allowed */
2535 if (strpbrk(path, MSWINDOWS_BADCHARS))
2538 /* everything else is okay */
2544 * precreate a folder
2545 * this is only intended for folders in the volume root
2546 * It will *not* work if the folder name contains extended characters
2548 static int create_special_folder (const struct vol *vol, const struct _special_folder *folder)
2557 p = (char *) malloc ( strlen(vol->v_path)+strlen(folder->name)+2);
2559 LOG(log_error, logtype_afpd,"malloc failed");
2563 q=strdup(folder->name);
2565 LOG(log_error, logtype_afpd,"malloc failed");
2569 strcpy(p, vol->v_path);
2574 if ((vol->v_casefold & AFPVOL_MTOUUPPER))
2576 else if ((vol->v_casefold & AFPVOL_MTOULOWER))
2582 if ( (ret = stat( p, &st )) < 0 ) {
2583 if (folder->precreate) {
2584 if (ad_mkdir(p, folder->mode)) {
2585 LOG(log_debug, logtype_afpd,"Creating '%s' failed in %s: %s", p, vol->v_path, strerror(errno));
2594 if ( !ret && folder->hide) {
2596 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2597 if (ad_open_metadata( p, ADFLAGS_DIR, O_CREAT, &ad) < 0) {
2603 ad_setname(&ad, folder->name);
2605 ad_getattr(&ad, &attr);
2606 attr |= htons( ntohs( attr ) | ATTRBIT_INVISIBLE );
2607 ad_setattr(&ad, attr);
2609 /* do the same with the finder info */
2610 if (ad_entry(&ad, ADEID_FINDERI)) {
2611 memcpy(&attr, ad_entry(&ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF, sizeof(attr));
2612 attr |= htons(FINDERINFO_INVISIBLE);
2613 memcpy(ad_entry(&ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF,&attr, sizeof(attr));
2617 ad_close_metadata( &ad);
2624 static void handle_special_folders (const struct vol * vol)
2626 const _special_folder *p = &special_folders[0];
2628 if ((vol->v_flags & AFPVOL_RO))
2631 for (; p->name != NULL; p++) {
2632 create_special_folder (vol, p);
2636 const struct vol *getvolumes(void)
2641 void unload_volumes_and_extmap(void)
2643 LOG(log_debug, logtype_afpd, "unload_volumes_and_extmap");
2649 * Get a volumes UUID from the config file.
2650 * If there is none, it is generated and stored there.
2652 * Returns pointer to allocated storage on success, NULL on error.
2654 char *get_uuid(const AFPObj *obj, const char *volname)
2657 char buf[1024], uuid[UUID_PRINTABLE_STRING_LENGTH], *p;
2659 struct stat tmpstat;
2662 if ((fp = fopen(obj->options.uuidconf, "r")) != NULL) { /* read open? */
2663 /* scan in the conf file */
2664 while (fgets(buf, sizeof(buf), fp) != NULL) {
2666 while (p && isblank(*p))
2668 if (!p || (*p == '#') || (*p == '\n'))
2669 continue; /* invalid line */
2672 if ((volname_conf = strtok( p, "\"" )) == NULL)
2673 continue; /* syntax error */
2675 if ((volname_conf = strtok( p, " \t" )) == NULL)
2676 continue; /* syntax error: invalid name */
2678 p = strchr(p, '\0');
2681 continue; /* syntax error */
2683 if (strcmp(volname, volname_conf) != 0)
2684 continue; /* another volume name */
2686 while (p && isblank(*p))
2689 if (sscanf(p, "%36s", uuid) == 1 ) {
2690 for (int i=0; uuid[i]; i++)
2691 uuid[i] = toupper(uuid[i]);
2692 LOG(log_debug, logtype_afpd, "get_uuid('%s'): UUID: '%s'", volname, uuid);
2694 return strdup(uuid);
2702 /* not found or no file, reopen in append mode */
2704 if (stat(obj->options.uuidconf, &tmpstat)) { /* no file */
2705 if (( fd = creat(obj->options.uuidconf, 0644 )) < 0 ) {
2706 LOG(log_error, logtype_atalkd, "ERROR: Cannot create %s (%s).",
2707 obj->options.uuidconf, strerror(errno));
2710 if (( fp = fdopen( fd, "w" )) == NULL ) {
2711 LOG(log_error, logtype_atalkd, "ERROR: Cannot fdopen %s (%s).",
2712 obj->options.uuidconf, strerror(errno));
2716 } else if ((fp = fopen(obj->options.uuidconf, "a+")) == NULL) { /* not found */
2717 LOG(log_error, logtype_afpd, "Cannot create or append to %s (%s).",
2718 obj->options.uuidconf, strerror(errno));
2721 fseek(fp, 0L, SEEK_END);
2722 if(ftell(fp) == 0) { /* size = 0 */
2723 fprintf(fp, "# DON'T TOUCH NOR COPY THOUGHTLESSLY!\n");
2724 fprintf(fp, "# This file is auto-generated by afpd\n");
2725 fprintf(fp, "# and stores UUIDs for TM volumes.\n\n");
2727 fseek(fp, -1L, SEEK_END);
2728 if(fgetc(fp) != '\n') fputc('\n', fp); /* last char is \n? */
2731 /* generate uuid and write to file */
2734 uuid_unparse(id, uuid);
2735 for (int i=0; uuid[i]; i++)
2736 uuid[i] = toupper(uuid[i]);
2737 LOG(log_debug, logtype_afpd, "get_uuid('%s'): generated UUID '%s'", volname, uuid);
2739 fprintf(fp, "\"%s\"\t%36s\n", volname, uuid);
2742 return strdup(uuid);