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"
63 extern int afprun(int root, char *cmd, int *outfd);
66 #define MIN(a, b) ((a) < (b) ? (a) : (b))
69 #ifndef NO_LARGE_VOL_SUPPORT
70 #if BYTE_ORDER == BIG_ENDIAN
73 #else /* BYTE_ORDER == BIG_ENDIAN */
74 #define hton64(x) ((u_int64_t) (htonl(((x) >> 32) & 0xffffffffLL)) | \
75 (u_int64_t) ((htonl(x) & 0xffffffffLL) << 32))
76 #define ntoh64(x) (hton64(x))
77 #endif /* BYTE_ORDER == BIG_ENDIAN */
78 #endif /* ! NO_LARGE_VOL_SUPPORT */
80 #ifndef UUID_PRINTABLE_STRING_LENGTH
81 #define UUID_PRINTABLE_STRING_LENGTH 37
84 static struct vol *Volumes = NULL;
85 static u_int16_t lastvid = 0;
86 static char *Trash = "\02\024Network Trash Folder";
88 static struct extmap *Extmap = NULL, *Defextmap = NULL;
89 static int Extmap_cnt;
90 static void free_extmap(void);
92 #define VOLOPT_ALLOW 0 /* user allow list */
93 #define VOLOPT_DENY 1 /* user deny list */
94 #define VOLOPT_RWLIST 2 /* user rw list */
95 #define VOLOPT_ROLIST 3 /* user ro list */
96 #define VOLOPT_PASSWORD 4 /* volume password */
97 #define VOLOPT_CASEFOLD 5 /* character case mangling */
98 #define VOLOPT_FLAGS 6 /* various flags */
99 #define VOLOPT_DBPATH 7 /* path to database */
100 #define VOLOPT_LIMITSIZE 8 /* Limit the size of the volume */
102 #define VOLOPT_VETO 10 /* list of veto filespec */
103 #define VOLOPT_PREEXEC 11 /* preexec command */
104 #define VOLOPT_ROOTPREEXEC 12 /* root preexec command */
105 #define VOLOPT_POSTEXEC 13 /* postexec command */
106 #define VOLOPT_ROOTPOSTEXEC 14 /* root postexec command */
107 #define VOLOPT_ENCODING 15 /* mac encoding (pre OSX)*/
108 #define VOLOPT_MACCHARSET 16
109 #define VOLOPT_CNIDSCHEME 17
110 #define VOLOPT_ADOUBLE 18 /* adouble version */
116 #define VOLOPT_FORCEUID 19 /* force uid for username x */
117 #define VOLOPT_FORCEGID 20 /* force gid for group x */
118 #endif /* FORCE_UIDGID */
120 #define VOLOPT_UMASK 21
121 #define VOLOPT_ALLOWED_HOSTS 22
122 #define VOLOPT_DENIED_HOSTS 23
123 #define VOLOPT_DPERM 24 /* dperm default directories perms */
124 #define VOLOPT_FPERM 25 /* fperm default files perms */
125 #define VOLOPT_DFLTPERM 26 /* perm */
126 #define VOLOPT_EA_VFS 27 /* Extended Attributes vfs indirection */
127 #define VOLOPT_CNIDSERVER 28 /* CNID Server ip address*/
128 #define VOLOPT_CNIDPORT 30 /* CNID server tcp port */
130 #define VOLOPT_MAX 31 /* <== IMPORTANT !!!!!! */
131 #define VOLOPT_NUM (VOLOPT_MAX + 1)
134 #define VOLOPT_DEFAULT ":DEFAULT:"
135 #define VOLOPT_DEFAULT_LEN 9
142 typedef struct _special_folder {
149 static const _special_folder special_folders[] = {
150 {"Network Trash Folder", 1, 0777, 1},
151 {"Temporary Items", 1, 0777, 1},
152 {".AppleDesktop", 1, 0777, 0},
154 {"TheFindByContentFolder", 0, 0, 1},
155 {"TheVolumeSettingsFolder", 0, 0, 1},
159 /* Forward declarations */
160 static void handle_special_folders (const struct vol *);
161 static void deletevol(struct vol *vol);
162 static void volume_free(struct vol *vol);
163 static void check_ea_sys_support(struct vol *vol);
165 static void volfree(struct vol_option *options, const struct vol_option *save)
170 for (i = 0; i < VOLOPT_MAX; i++) {
171 if (options[i].c_value && (options[i].c_value != save[i].c_value))
172 free(options[i].c_value);
175 for (i = 0; i < VOLOPT_MAX; i++) {
176 if (options[i].c_value)
177 free(options[i].c_value);
183 #define is_var(a, b) (strncmp((a), (b), 2) == 0)
186 * Handle variable substitutions. here's what we understand:
187 * $b -> basename of path
188 * $c -> client ip/appletalk address
189 * $d -> volume pathname on server
190 * $f -> full name (whatever's in the gecos field)
193 * $i -> client ip/appletalk address without port
194 * $s -> server name (hostname if it doesn't exist)
195 * $u -> username (guest is usually nobody)
196 * $v -> volume name or basename if null
197 * $z -> zone (may not exist)
200 * This get's called from readvolfile with
201 * path = NULL, volname = NULL for xlating the volumes path
202 * path = path, volname = NULL for xlating the volumes name
203 * ... and from volumes options parsing code when xlating eg dbpath with
204 * path = path, volname = volname
206 * Using this information we can reject xlation of any variable depeninding on a login
207 * context which is not given in the afp master, where we must evaluate this whole stuff
208 * too for the Zeroconf announcements.
210 static char *volxlate(AFPObj *obj,
223 int xlatevolname = 0;
225 if (! ((DSI *)obj->handle)->child)
228 if (path && !volname)
236 dest = calloc(destlen +1, 1);
242 strlcpy(dest, src, destlen +1);
243 if ((p = strchr(src, '$')) == NULL) /* nothing to do */
246 /* first part of the path. just forward to the next variable. */
247 len = MIN((size_t)(p - src), destlen);
253 while (p && destlen > 0) {
254 /* now figure out what the variable is */
256 if (is_var(p, "$b")) {
257 if (afpmaster && xlatevolname)
260 if ((q = strrchr(path, '/')) == NULL)
262 else if (*(q + 1) != '\0')
265 } else if (is_var(p, "$c")) {
266 if (afpmaster && xlatevolname)
268 if (obj->proto == AFPPROTO_ASP) {
269 ASP asp = obj->handle;
271 len = sprintf(dest, "%u.%u", ntohs(asp->asp_sat.sat_addr.s_net),
272 asp->asp_sat.sat_addr.s_node);
276 } else if (obj->proto == AFPPROTO_DSI) {
277 DSI *dsi = obj->handle;
278 len = sprintf(dest, "%s:%u",
279 getip_string((struct sockaddr *)&dsi->client),
280 getip_port((struct sockaddr *)&dsi->client));
284 } else if (is_var(p, "$d")) {
285 if (afpmaster && xlatevolname)
288 } else if (pwd && is_var(p, "$f")) {
289 if (afpmaster && xlatevolname)
291 if ((r = strchr(pwd->pw_gecos, ',')))
294 } else if (pwd && is_var(p, "$g")) {
295 if (afpmaster && xlatevolname)
297 struct group *grp = getgrgid(pwd->pw_gid);
300 } else if (is_var(p, "$h")) {
301 q = obj->options.hostname;
302 } else if (is_var(p, "$i")) {
303 if (afpmaster && xlatevolname)
305 if (obj->proto == AFPPROTO_ASP) {
306 ASP asp = obj->handle;
308 len = sprintf(dest, "%u", ntohs(asp->asp_sat.sat_addr.s_net));
312 } else if (obj->proto == AFPPROTO_DSI) {
313 DSI *dsi = obj->handle;
314 q = getip_string((struct sockaddr *)&dsi->client);
316 } else if (is_var(p, "$s")) {
319 else if (obj->options.server) {
320 q = obj->options.server;
322 q = obj->options.hostname;
323 } else if (obj->username && is_var(p, "$u")) {
324 if (afpmaster && xlatevolname)
327 if ( obj->options.ntseparator && (sep = strchr(obj->username, obj->options.ntseparator[0])) != NULL)
331 } else if (is_var(p, "$v")) {
332 if (afpmaster && xlatevolname)
338 if ((q = strrchr(path, '/')) == NULL)
340 else if (*(q + 1) != '\0')
343 } else if (is_var(p, "$z")) {
345 } else if (is_var(p, "$$")) {
350 /* copy the stuff over. if we don't understand something that we
351 * should, just skip it over. */
353 len = MIN(p == q ? 2 : strlen(q), destlen);
354 strncpy(dest, q, len);
359 /* stuff up to next $ */
361 p = strchr(src, '$');
362 len = p ? MIN((size_t)(p - src), destlen) : destlen;
364 strncpy(dest, src, len);
372 /* to make sure that val is valid, make sure to select an opt that
374 static int optionok(const char *buf, const char *opt, const char *val)
376 if (!strstr(buf,opt))
384 /* -------------------- */
385 static void setoption(struct vol_option *options, struct vol_option *save, int opt, const char *val)
387 if (options[opt].c_value && (!save || options[opt].c_value != save[opt].c_value))
388 free(options[opt].c_value);
389 options[opt].c_value = strdup(val + 1);
392 /* ------------------------------------------
393 handle all the options. tmp can't be NULL. */
394 static void volset(struct vol_option *options, struct vol_option *save,
395 char *volname, int vlen,
400 val = strchr(tmp, ':');
402 /* we'll assume it's a volume name. */
403 strncpy(volname, tmp, vlen);
408 LOG(log_debug, logtype_afpd, "Parsing volset %s", val);
410 if (optionok(tmp, "allow:", val)) {
411 setoption(options, save, VOLOPT_ALLOW, val);
413 } else if (optionok(tmp, "deny:", val)) {
414 setoption(options, save, VOLOPT_DENY, val);
416 } else if (optionok(tmp, "rwlist:", val)) {
417 setoption(options, save, VOLOPT_RWLIST, val);
419 } else if (optionok(tmp, "rolist:", val)) {
420 setoption(options, save, VOLOPT_ROLIST, val);
422 } else if (optionok(tmp, "codepage:", val)) {
423 LOG (log_error, logtype_afpd, "The old codepage system has been removed. Please make sure to read the documentation !!!!");
424 /* Make sure we don't screw anything */
426 } else if (optionok(tmp, "volcharset:", val)) {
427 setoption(options, save, VOLOPT_ENCODING, val);
428 } else if (optionok(tmp, "maccharset:", val)) {
429 setoption(options, save, VOLOPT_MACCHARSET, val);
430 } else if (optionok(tmp, "veto:", val)) {
431 setoption(options, save, VOLOPT_VETO, val);
432 } else if (optionok(tmp, "cnidscheme:", val)) {
433 setoption(options, save, VOLOPT_CNIDSCHEME, val);
434 } else if (optionok(tmp, "casefold:", val)) {
435 if (strcasecmp(val + 1, "tolower") == 0)
436 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMLOWER;
437 else if (strcasecmp(val + 1, "toupper") == 0)
438 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMUPPER;
439 else if (strcasecmp(val + 1, "xlatelower") == 0)
440 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UUPPERMLOWER;
441 else if (strcasecmp(val + 1, "xlateupper") == 0)
442 options[VOLOPT_CASEFOLD].i_value = AFPVOL_ULOWERMUPPER;
443 } else if (optionok(tmp, "adouble:", val)) {
444 if (strcasecmp(val + 1, "v1") == 0)
445 options[VOLOPT_ADOUBLE].i_value = AD_VERSION1;
446 #if AD_VERSION == AD_VERSION2
447 else if (strcasecmp(val + 1, "v2") == 0)
448 options[VOLOPT_ADOUBLE].i_value = AD_VERSION2;
449 else if (strcasecmp(val + 1, "osx") == 0)
450 options[VOLOPT_ADOUBLE].i_value = AD_VERSION2_OSX;
451 else if (strcasecmp(val + 1, "sfm") == 0)
452 options[VOLOPT_ADOUBLE].i_value = AD_VERSION1_SFM;
454 } else if (optionok(tmp, "options:", val)) {
457 if ((p = strtok(val + 1, ",")) == NULL) /* nothing */
461 if (strcasecmp(p, "prodos") == 0)
462 options[VOLOPT_FLAGS].i_value |= AFPVOL_A2VOL;
463 else if (strcasecmp(p, "mswindows") == 0) {
464 options[VOLOPT_FLAGS].i_value |= AFPVOL_MSWINDOWS | AFPVOL_USEDOTS;
465 } else if (strcasecmp(p, "crlf") == 0)
466 options[VOLOPT_FLAGS].i_value |= AFPVOL_CRLF;
467 else if (strcasecmp(p, "noadouble") == 0)
468 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOADOUBLE;
469 else if (strcasecmp(p, "ro") == 0)
470 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
471 else if (strcasecmp(p, "nohex") == 0)
472 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOHEX;
473 else if (strcasecmp(p, "usedots") == 0)
474 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS;
475 else if (strcasecmp(p, "invisibledots") == 0)
476 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS | AFPVOL_INV_DOTS;
477 else if (strcasecmp(p, "limitsize") == 0)
478 options[VOLOPT_FLAGS].i_value |= AFPVOL_LIMITSIZE;
479 /* support for either "dropbox" or "dropkludge" */
480 else if (strcasecmp(p, "dropbox") == 0)
481 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
482 else if (strcasecmp(p, "dropkludge") == 0)
483 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
484 else if (strcasecmp(p, "nofileid") == 0)
485 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOFILEID;
486 else if (strcasecmp(p, "nostat") == 0)
487 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOSTAT;
488 else if (strcasecmp(p, "preexec_close") == 0)
489 options[VOLOPT_PREEXEC].i_value = 1;
490 else if (strcasecmp(p, "root_preexec_close") == 0)
491 options[VOLOPT_ROOTPREEXEC].i_value = 1;
492 else if (strcasecmp(p, "upriv") == 0)
493 options[VOLOPT_FLAGS].i_value |= AFPVOL_UNIX_PRIV;
494 else if (strcasecmp(p, "acls") == 0)
495 options[VOLOPT_FLAGS].i_value |= AFPVOL_ACLS;
496 else if (strcasecmp(p, "nodev") == 0)
497 options[VOLOPT_FLAGS].i_value |= AFPVOL_NODEV;
498 else if (strcasecmp(p, "caseinsensitive") == 0)
499 options[VOLOPT_FLAGS].i_value |= AFPVOL_CASEINSEN;
500 else if (strcasecmp(p, "illegalseq") == 0)
501 options[VOLOPT_FLAGS].i_value |= AFPVOL_EILSEQ;
502 else if (strcasecmp(p, "nocnidcache") == 0)
503 options[VOLOPT_FLAGS].i_value &= ~AFPVOL_CACHE;
504 else if (strcasecmp(p, "tm") == 0)
505 options[VOLOPT_FLAGS].i_value |= AFPVOL_TM;
506 /* Found this in branch dir-rewrite, maybe we want to use it sometimes */
508 else if (strcasecmp(p, "cdrom") == 0)
509 options[VOLOPT_FLAGS].i_value |= AFPVOL_CDROM | AFPVOL_RO;
511 p = strtok(NULL, ",");
514 } else if (optionok(tmp, "cnidserver:", val)) {
515 setoption(options, save, VOLOPT_CNIDSERVER, val);
517 char *p = strrchr(val + 1, ':');
520 setoption(options, save, VOLOPT_CNIDPORT, p);
523 LOG(log_debug, logtype_afpd, "CNID Server for volume '%s': %s:%s",
524 volname, val + 1, p ? p + 1 : Cnid_port);
526 } else if (optionok(tmp, "dbpath:", val)) {
527 setoption(options, save, VOLOPT_DBPATH, val);
529 } else if (optionok(tmp, "umask:", val)) {
530 options[VOLOPT_UMASK].i_value = (int)strtol(val +1, NULL, 8);
531 } else if (optionok(tmp, "dperm:", val)) {
532 options[VOLOPT_DPERM].i_value = (int)strtol(val+1, NULL, 8);
533 } else if (optionok(tmp, "fperm:", val)) {
534 options[VOLOPT_FPERM].i_value = (int)strtol(val+1, NULL, 8);
535 } else if (optionok(tmp, "perm:", val)) {
536 options[VOLOPT_DFLTPERM].i_value = (int)strtol(val+1, NULL, 8);
537 } else if (optionok(tmp, "password:", val)) {
538 setoption(options, save, VOLOPT_PASSWORD, val);
542 /* this code allows forced uid/gid per volume settings */
543 } else if (optionok(tmp, "forceuid:", val)) {
544 setoption(options, save, VOLOPT_FORCEUID, val);
545 } else if (optionok(tmp, "forcegid:", val)) {
546 setoption(options, save, VOLOPT_FORCEGID, val);
548 #endif /* FORCE_UIDGID */
549 } else if (optionok(tmp, "root_preexec:", val)) {
550 setoption(options, save, VOLOPT_ROOTPREEXEC, val);
552 } else if (optionok(tmp, "preexec:", val)) {
553 setoption(options, save, VOLOPT_PREEXEC, val);
555 } else if (optionok(tmp, "root_postexec:", val)) {
556 setoption(options, save, VOLOPT_ROOTPOSTEXEC, val);
558 } else if (optionok(tmp, "postexec:", val)) {
559 setoption(options, save, VOLOPT_POSTEXEC, val);
561 } else if (optionok(tmp, "allowed_hosts:", val)) {
562 setoption(options, save, VOLOPT_ALLOWED_HOSTS, val);
564 } else if (optionok(tmp, "denied_hosts:", val)) {
565 setoption(options, save, VOLOPT_DENIED_HOSTS, val);
567 } else if (optionok(tmp, "ea:", val)) {
568 if (strcasecmp(val + 1, "ad") == 0)
569 options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_AD;
570 else if (strcasecmp(val + 1, "sys") == 0)
571 options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_SYS;
572 else if (strcasecmp(val + 1, "none") == 0)
573 options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_NONE;
575 } else if (optionok(tmp, "volsizelimit:", val)) {
576 options[VOLOPT_LIMITSIZE].i_value = (uint32_t)strtoul(val + 1, NULL, 10);
579 /* ignore unknown options */
580 LOG(log_debug, logtype_afpd, "ignoring unknown volume option: %s", tmp);
585 /* ----------------- */
586 static void showvol(const ucs2_t *name)
589 for ( volume = Volumes; volume; volume = volume->v_next ) {
590 if (volume->v_hide && !strcasecmp_w( volume->v_name, name ) ) {
597 /* ------------------------------- */
598 static int creatvol(AFPObj *obj, struct passwd *pwd,
599 char *path, char *name,
600 struct vol_option *options,
601 const int user /* user defined volume */
605 int suffixlen, vlen, tmpvlen, u8mvlen, macvlen;
607 char tmpname[AFPVOL_U8MNAMELEN+1];
608 ucs2_t u8mtmpname[(AFPVOL_U8MNAMELEN+1)*2], mactmpname[(AFPVOL_MACNAMELEN+1)*2];
609 char suffix[6]; /* max is #FFFF */
612 if ( name == NULL || *name == '\0' ) {
613 if ((name = strrchr( path, '/' )) == NULL) {
614 return -1; /* Obviously not a fully qualified path */
617 /* if you wish to share /, you need to specify a name. */
622 /* suffix for mangling use (lastvid + 1) */
623 /* because v_vid has not been decided yet. */
624 suffixlen = sprintf(suffix, "%c%X", MANGLE_CHAR, lastvid + 1 );
626 vlen = strlen( name );
628 /* Unicode Volume Name */
629 /* Firstly convert name from unixcharset to UTF8-MAC */
631 tmpvlen = convert_charset(obj->options.unixcharset, CH_UTF8_MAC, 0, name, vlen, tmpname, AFPVOL_U8MNAMELEN, &flags);
633 strcpy(tmpname, "???");
637 /* Do we have to mangle ? */
638 if ( (flags & CONV_REQMANGLE) || (tmpvlen > obj->options.volnamelen)) {
639 if (tmpvlen + suffixlen > obj->options.volnamelen) {
641 tmpvlen = convert_charset(obj->options.unixcharset, CH_UTF8_MAC, 0, name, vlen, tmpname, obj->options.volnamelen - suffixlen, &flags);
642 tmpname[tmpvlen >= 0 ? tmpvlen : 0] = 0;
644 strcat(tmpname, suffix);
645 tmpvlen = strlen(tmpname);
648 /* Secondly convert name from UTF8-MAC to UCS2 */
649 if ( 0 >= ( u8mvlen = convert_string(CH_UTF8_MAC, CH_UCS2, tmpname, tmpvlen, u8mtmpname, AFPVOL_U8MNAMELEN*2)) )
652 LOG(log_debug, logtype_afpd, "createvol: Volume '%s' -> UTF8-MAC Name: '%s'", name, tmpname);
654 /* Maccharset Volume Name */
655 /* Firsty convert name from unixcharset to maccharset */
657 tmpvlen = convert_charset(obj->options.unixcharset, obj->options.maccharset, 0, name, vlen, tmpname, AFPVOL_U8MNAMELEN, &flags);
659 strcpy(tmpname, "???");
663 /* Do we have to mangle ? */
664 if ( (flags & CONV_REQMANGLE) || (tmpvlen > AFPVOL_MACNAMELEN)) {
665 if (tmpvlen + suffixlen > AFPVOL_MACNAMELEN) {
667 tmpvlen = convert_charset(obj->options.unixcharset, obj->options.maccharset, 0, name, vlen, tmpname, AFPVOL_MACNAMELEN - suffixlen, &flags);
668 tmpname[tmpvlen >= 0 ? tmpvlen : 0] = 0;
670 strcat(tmpname, suffix);
671 tmpvlen = strlen(tmpname);
674 /* Secondly convert name from maccharset to UCS2 */
675 if ( 0 >= ( macvlen = convert_string(obj->options.maccharset, CH_UCS2, tmpname, tmpvlen, mactmpname, AFPVOL_U8MNAMELEN*2)) )
678 LOG(log_debug, logtype_afpd, "createvol: Volume '%s' -> Longname: '%s'", name, tmpname);
680 /* check duplicate */
681 for ( volume = Volumes; volume; volume = volume->v_next ) {
682 if (( strcasecmp_w( volume->v_u8mname, u8mtmpname ) == 0 ) || ( strcasecmp_w( volume->v_macname, mactmpname ) == 0 )){
683 if (volume->v_deleted) {
684 volume->v_new = hide = 1;
687 return -1; /* Won't be able to access it, anyway... */
692 if (!( volume = (struct vol *)calloc(1, sizeof( struct vol ))) ) {
693 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
696 if ( NULL == ( volume->v_localname = strdup(name))) {
697 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
702 if ( NULL == ( volume->v_u8mname = strdup_w(u8mtmpname))) {
703 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
708 if ( NULL == ( volume->v_macname = strdup_w(mactmpname))) {
709 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
714 if (!( volume->v_path = (char *)malloc( strlen( path ) + 1 )) ) {
715 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
721 volume->v_name = utf8_encoding()?volume->v_u8mname:volume->v_macname;
722 volume->v_hide = hide;
723 strcpy( volume->v_path, path );
727 #endif /* __svr4__ */
728 /* os X start at 1 and use network order ie. 1 2 3 */
729 volume->v_vid = ++lastvid;
730 volume->v_vid = htons(volume->v_vid);
734 /* should we casefold? */
735 volume->v_casefold = options[VOLOPT_CASEFOLD].i_value;
737 /* shift in some flags */
738 volume->v_flags = options[VOLOPT_FLAGS].i_value;
740 if (options[VOLOPT_EA_VFS].i_value)
741 volume->v_vfs_ea = options[VOLOPT_EA_VFS].i_value;
743 volume->v_ad_options = 0;
744 if ((volume->v_flags & AFPVOL_NODEV))
745 volume->v_ad_options |= ADVOL_NODEV;
746 if ((volume->v_flags & AFPVOL_CACHE))
747 volume->v_ad_options |= ADVOL_CACHE;
748 if ((volume->v_flags & AFPVOL_UNIX_PRIV))
749 volume->v_ad_options |= ADVOL_UNIXPRIV;
750 if ((volume->v_flags & AFPVOL_INV_DOTS))
751 volume->v_ad_options |= ADVOL_INVDOTS;
752 if ((volume->v_flags & AFPVOL_NOADOUBLE))
753 volume->v_ad_options |= ADVOL_NOADOUBLE;
755 if (options[VOLOPT_PASSWORD].c_value)
756 volume->v_password = strdup(options[VOLOPT_PASSWORD].c_value);
758 if (options[VOLOPT_VETO].c_value)
759 volume->v_veto = strdup(options[VOLOPT_VETO].c_value);
761 if (options[VOLOPT_ENCODING].c_value)
762 volume->v_volcodepage = strdup(options[VOLOPT_ENCODING].c_value);
764 if (options[VOLOPT_MACCHARSET].c_value)
765 volume->v_maccodepage = strdup(options[VOLOPT_MACCHARSET].c_value);
767 if (options[VOLOPT_DBPATH].c_value)
768 volume->v_dbpath = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_DBPATH].c_value, pwd, path, name);
770 if (options[VOLOPT_CNIDSCHEME].c_value)
771 volume->v_cnidscheme = strdup(options[VOLOPT_CNIDSCHEME].c_value);
773 if (options[VOLOPT_CNIDSERVER].c_value)
774 volume->v_cnidserver = strdup(options[VOLOPT_CNIDSERVER].c_value);
776 if (options[VOLOPT_CNIDPORT].c_value)
777 volume->v_cnidport = strdup(options[VOLOPT_CNIDPORT].c_value);
779 if (options[VOLOPT_UMASK].i_value)
780 volume->v_umask = (mode_t)options[VOLOPT_UMASK].i_value;
782 if (options[VOLOPT_DPERM].i_value)
783 volume->v_dperm = (mode_t)options[VOLOPT_DPERM].i_value;
785 if (options[VOLOPT_FPERM].i_value)
786 volume->v_fperm = (mode_t)options[VOLOPT_FPERM].i_value;
788 if (options[VOLOPT_DFLTPERM].i_value)
789 volume->v_perm = (mode_t)options[VOLOPT_DFLTPERM].i_value;
791 if (options[VOLOPT_ADOUBLE].i_value)
792 volume->v_adouble = options[VOLOPT_ADOUBLE].i_value;
794 volume->v_adouble = AD_VERSION;
796 if (options[VOLOPT_LIMITSIZE].i_value)
797 volume->v_limitsize = options[VOLOPT_LIMITSIZE].i_value;
799 /* Mac to Unix conversion flags*/
800 volume->v_mtou_flags = 0;
801 if (!(volume->v_flags & AFPVOL_NOHEX))
802 volume->v_mtou_flags |= CONV_ESCAPEHEX;
803 if (!(volume->v_flags & AFPVOL_USEDOTS))
804 volume->v_mtou_flags |= CONV_ESCAPEDOTS;
805 if ((volume->v_flags & AFPVOL_EILSEQ))
806 volume->v_mtou_flags |= CONV__EILSEQ;
808 if ((volume->v_casefold & AFPVOL_MTOUUPPER))
809 volume->v_mtou_flags |= CONV_TOUPPER;
810 else if ((volume->v_casefold & AFPVOL_MTOULOWER))
811 volume->v_mtou_flags |= CONV_TOLOWER;
813 /* Unix to Mac conversion flags*/
814 volume->v_utom_flags = CONV_IGNORE | CONV_UNESCAPEHEX;
815 if ((volume->v_casefold & AFPVOL_UTOMUPPER))
816 volume->v_utom_flags |= CONV_TOUPPER;
817 else if ((volume->v_casefold & AFPVOL_UTOMLOWER))
818 volume->v_utom_flags |= CONV_TOLOWER;
820 if ((volume->v_flags & AFPVOL_EILSEQ))
821 volume->v_utom_flags |= CONV__EILSEQ;
824 if (options[VOLOPT_FORCEUID].c_value) {
825 volume->v_forceuid = strdup(options[VOLOPT_FORCEUID].c_value);
827 volume->v_forceuid = NULL; /* set as null so as to return 0 later on */
830 if (options[VOLOPT_FORCEGID].c_value) {
831 volume->v_forcegid = strdup(options[VOLOPT_FORCEGID].c_value);
833 volume->v_forcegid = NULL; /* set as null so as to return 0 later on */
837 if (options[VOLOPT_PREEXEC].c_value)
838 volume->v_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_PREEXEC].c_value, pwd, path, name);
839 volume->v_preexec_close = options[VOLOPT_PREEXEC].i_value;
841 if (options[VOLOPT_POSTEXEC].c_value)
842 volume->v_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_POSTEXEC].c_value, pwd, path, name);
844 if (options[VOLOPT_ROOTPREEXEC].c_value)
845 volume->v_root_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPREEXEC].c_value, pwd, path, name);
846 volume->v_root_preexec_close = options[VOLOPT_ROOTPREEXEC].i_value;
848 if (options[VOLOPT_ROOTPOSTEXEC].c_value)
849 volume->v_root_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPOSTEXEC].c_value, pwd, path, name);
852 volume->v_dperm |= volume->v_perm;
853 volume->v_fperm |= volume->v_perm;
855 /* Check EA support on volume */
856 if (volume->v_vfs_ea == AFPVOL_EA_AUTO)
857 check_ea_sys_support(volume);
860 /* get/store uuid from file */
861 if (volume->v_flags & AFPVOL_TM) {
862 char *uuid = get_uuid(obj, volume->v_localname);
864 LOG(log_error, logtype_afpd, "Volume '%s': couldn't get UUID",
865 volume->v_localname);
867 volume->v_uuid = uuid;
868 LOG(log_debug, logtype_afpd, "Volume '%s': UUID '%s'",
869 volume->v_localname, volume->v_uuid);
873 volume->v_next = Volumes;
878 /* ---------------- */
879 static char *myfgets( char *buf, int size, FILE *fp)
885 while ((EOF != ( c = getc( fp )) ) && ( size > 1 )) {
886 if ( c == '\n' || c == '\r' ) {
887 if (p != buf && *(p -1) == '\\') {
909 /* check access list. this function wants something of the following
911 * @group,name,name2,@group2,name3
913 * a NULL argument allows everybody to have access.
914 * we return three things:
916 * 0: list exists, but name isn't in it
920 #ifndef NO_REAL_USER_NAME
921 /* authentication is case insensitive
922 * FIXME should we do the same with group name?
924 #define access_strcmp strcasecmp
927 #define access_strcmp strcmp
931 static int accessvol(const char *args, const char *name)
933 char buf[MAXPATHLEN + 1], *p;
939 strlcpy(buf, args, sizeof(buf));
940 if ((p = strtok(buf, ",")) == NULL) /* nothing, return okay */
944 if (*p == '@') { /* it's a group */
945 if ((gr = getgrnam(p + 1)) && gmem(gr->gr_gid))
947 } else if (access_strcmp(p, name) == 0) /* it's a user name */
949 p = strtok(NULL, ",");
955 static int hostaccessvol(int type, const char *volname, const char *args, const AFPObj *obj)
958 char buf[MAXPATHLEN + 1], *p, *b;
959 DSI *dsi = obj->handle;
960 struct sockaddr_storage client;
965 strlcpy(buf, args, sizeof(buf));
966 if ((p = strtok_r(buf, ",", &b)) == NULL) /* nothing, return okay */
969 if (obj->proto != AFPPROTO_DSI)
974 char *ipaddr, *mask_char;
975 struct addrinfo hints, *ai;
977 ipaddr = strtok(p, "/");
978 mask_char = strtok(NULL,"/");
980 /* Get address from string with getaddrinfo */
981 memset(&hints, 0, sizeof hints);
982 hints.ai_family = AF_UNSPEC;
983 hints.ai_socktype = SOCK_STREAM;
984 if ((ret = getaddrinfo(ipaddr, NULL, &hints, &ai)) != 0) {
985 LOG(log_error, logtype_afpd, "hostaccessvol: getaddrinfo: %s\n", gai_strerror(ret));
990 if (mask_char != NULL)
991 mask_int = atoi(mask_char); /* apply_ip_mask does range checking on it */
993 if (ai->ai_family == AF_INET) /* IPv4 */
999 /* Apply mask to addresses */
1000 client = dsi->client;
1001 apply_ip_mask((struct sockaddr *)&client, mask_int);
1002 apply_ip_mask(ai->ai_addr, mask_int);
1004 if (compare_ip((struct sockaddr *)&client, ai->ai_addr) == 0) {
1005 if (type == VOLOPT_DENIED_HOSTS)
1006 LOG(log_info, logtype_afpd, "AFP access denied for client IP '%s' to volume '%s' by denied list",
1007 getip_string((struct sockaddr *)&client), volname);
1014 p = strtok_r(NULL, ",", &b);
1017 if (type == VOLOPT_ALLOWED_HOSTS)
1018 LOG(log_info, logtype_afpd, "AFP access denied for client IP '%s' to volume '%s', not in allowed list",
1019 getip_string((struct sockaddr *)&dsi->client), volname);
1023 static void setextmap(char *ext, char *type, char *creator, int user)
1028 if (Extmap == NULL) {
1029 if (( Extmap = calloc(1, sizeof( struct extmap ))) == NULL ) {
1030 LOG(log_error, logtype_afpd, "setextmap: calloc: %s", strerror(errno) );
1035 for ( em = Extmap, cnt = 0; em->em_ext; em++, cnt++) {
1036 if ( (strdiacasecmp( em->em_ext, ext )) == 0 ) {
1041 if ( em->em_ext == NULL ) {
1042 if (!(Extmap = realloc( Extmap, sizeof( struct extmap ) * (cnt +2))) ) {
1043 LOG(log_error, logtype_afpd, "setextmap: realloc: %s", strerror(errno) );
1046 (Extmap +cnt +1)->em_ext = NULL;
1048 } else if ( !user ) {
1054 if (!(em->em_ext = strdup( ext))) {
1055 LOG(log_error, logtype_afpd, "setextmap: strdup: %s", strerror(errno) );
1059 if ( *type == '\0' ) {
1060 memcpy(em->em_type, "\0\0\0\0", sizeof( em->em_type ));
1062 memcpy(em->em_type, type, sizeof( em->em_type ));
1064 if ( *creator == '\0' ) {
1065 memcpy(em->em_creator, "\0\0\0\0", sizeof( em->em_creator ));
1067 memcpy(em->em_creator, creator, sizeof( em->em_creator ));
1071 /* -------------------------- */
1072 static int extmap_cmp(const void *map1, const void *map2)
1074 const struct extmap *em1 = map1;
1075 const struct extmap *em2 = map2;
1076 return strdiacasecmp(em1->em_ext, em2->em_ext);
1079 static void sortextmap( void)
1084 if ((em = Extmap) == NULL) {
1087 while (em->em_ext) {
1092 qsort(Extmap, Extmap_cnt, sizeof(struct extmap), extmap_cmp);
1093 if (*Extmap->em_ext == 0) {
1094 /* the first line is really "." the default entry,
1095 * we remove the leading '.' in setextmap
1102 /* ----------------------
1104 static void free_extmap( void)
1109 for ( em = Extmap; em->em_ext; em++) {
1119 /* ----------------------
1121 static int volfile_changed(struct afp_volume_name *p)
1127 name = p->full_name;
1131 if (!stat( name, &st) && st.st_mtime > p->mtime) {
1132 p->mtime = st.st_mtime;
1138 /* ----------------------
1139 * Read a volume configuration file and add the volumes contained within to
1140 * the global volume list. This gets called from the forked afpd childs.
1141 * The master now reads this too for Zeroconf announcements.
1143 * If p2 is non-NULL, the file that is opened is
1146 * Lines that begin with # and blank lines are ignored.
1147 * Volume lines are of the form:
1148 * <unix path> [<volume name>] [allow:<user>,<@group>,...] \
1149 * [codepage:<file>] [casefold:<num>]
1150 * <extension> TYPE [CREATOR]
1153 int readvolfile(AFPObj *obj, struct afp_volume_name *p1, char *p2, int user, struct passwd *pwent)
1156 char path[MAXPATHLEN + 1];
1157 char tmp[MAXPATHLEN + 1];
1158 char volname[AFPVOL_U8MNAMELEN + 1];
1160 char type[5], creator[5];
1165 struct vol_option save_options[VOLOPT_NUM];
1166 struct vol_option options[VOLOPT_NUM];
1172 strcpy( path, p1->name );
1174 strcat( path, "/" );
1176 if (p1->full_name) {
1177 free(p1->full_name);
1179 p1->full_name = strdup(path);
1182 if (NULL == ( fp = fopen( path, "r" )) ) {
1186 if (fd != -1 && !fstat( fd, &st) ) {
1187 p1->mtime = st.st_mtime;
1190 memset(save_options, 0, sizeof(save_options));
1192 /* Enable some default options for all volumes */
1193 save_options[VOLOPT_FLAGS].i_value |= AFPVOL_CACHE;
1194 save_options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_AUTO;
1195 LOG(log_maxdebug, logtype_afpd, "readvolfile: seeding default umask: %04o",
1196 obj->options.umask);
1197 save_options[VOLOPT_UMASK].i_value = obj->options.umask;
1199 while ( myfgets( buf, sizeof( buf ), fp ) != NULL ) {
1200 initline( strlen( buf ), buf );
1201 parseline( sizeof( path ) - 1, path );
1208 /* change the default options for this file */
1209 if (strncmp(path, VOLOPT_DEFAULT, VOLOPT_DEFAULT_LEN) == 0) {
1211 for (i = 0; i < VOLOPT_NUM; i++) {
1212 if (parseline( sizeof( path ) - VOLOPT_DEFAULT_LEN - 1,
1213 path + VOLOPT_DEFAULT_LEN) < 0)
1215 volset(save_options, NULL, tmp, sizeof(tmp) - 1,
1216 path + VOLOPT_DEFAULT_LEN);
1222 if (( p = strchr( path, '/' )) != NULL ) {
1230 if ( u == NULL || *u == '\0' || ( pw = getpwnam( u )) == NULL ) {
1233 strcpy( tmp, pw->pw_dir );
1234 if ( p != NULL && *p != '\0' ) {
1241 /* send path through variable substitution */
1242 if (*path != '~') /* need to copy path to tmp */
1244 if (!pwent && obj->username)
1245 pwent = getpwnam(obj->username);
1247 if (volxlate(obj, path, sizeof(path) - 1, tmp, pwent, NULL, NULL) == NULL)
1250 /* this is sort of braindead. basically, i want to be
1251 * able to specify things in any order, but i don't want to
1252 * re-write everything. */
1254 memcpy(options, save_options, sizeof(options));
1257 /* read in up to VOLOP_NUM possible options */
1258 for (i = 0; i < VOLOPT_NUM; i++) {
1259 if (parseline( sizeof( tmp ) - 1, tmp ) < 0)
1262 volset(options, save_options, volname, sizeof(volname) - 1, tmp);
1265 /* check allow/deny lists:
1266 allow -> either no list (-1), or in list (1)
1267 deny -> either no list (-1), or not in list (0) */
1268 if (accessvol(options[VOLOPT_ALLOW].c_value, obj->username) &&
1269 (accessvol(options[VOLOPT_DENY].c_value, obj->username) < 1) &&
1270 hostaccessvol(VOLOPT_ALLOWED_HOSTS, volname, options[VOLOPT_ALLOWED_HOSTS].c_value, obj) &&
1271 (hostaccessvol(VOLOPT_DENIED_HOSTS, volname, options[VOLOPT_DENIED_HOSTS].c_value, obj) < 1)) {
1273 /* handle read-only behaviour. semantics:
1274 * 1) neither the rolist nor the rwlist exist -> rw
1275 * 2) rolist exists -> ro if user is in it.
1276 * 3) rwlist exists -> ro unless user is in it. */
1277 if (((options[VOLOPT_FLAGS].i_value & AFPVOL_RO) == 0) &&
1278 ((accessvol(options[VOLOPT_ROLIST].c_value,
1279 obj->username) == 1) ||
1280 !accessvol(options[VOLOPT_RWLIST].c_value,
1282 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
1284 /* do variable substitution for volname */
1285 if (volxlate(obj, tmp, sizeof(tmp) - 1, volname, pwent, path, NULL) == NULL)
1288 creatvol(obj, pwent, path, tmp, options, p2 != NULL);
1290 volfree(options, save_options);
1294 parseline( sizeof( type ) - 1, type );
1295 parseline( sizeof( creator ) - 1, creator );
1296 setextmap( path, type, creator, user);
1303 volfree(save_options, NULL);
1305 if ( fclose( fp ) != 0 ) {
1306 LOG(log_error, logtype_afpd, "readvolfile: fclose: %s", strerror(errno) );
1312 /* ------------------------------- */
1313 static void volume_free(struct vol *vol)
1315 free(vol->v_localname);
1316 vol->v_localname = NULL;
1317 free(vol->v_u8mname);
1318 vol->v_u8mname = NULL;
1319 free(vol->v_macname);
1320 vol->v_macname = NULL;
1322 free(vol->v_password);
1324 free(vol->v_volcodepage);
1325 free(vol->v_maccodepage);
1326 free(vol->v_cnidscheme);
1327 free(vol->v_dbpath);
1330 free(vol->v_forceuid);
1331 free(vol->v_forcegid);
1332 #endif /* FORCE_UIDGID */
1337 /* ------------------------------- */
1338 static void free_volumes(void )
1341 struct vol *nvol, *ovol;
1343 for ( vol = Volumes; vol; vol = vol->v_next ) {
1344 if (( vol->v_flags & AFPVOL_OPEN ) ) {
1351 for ( vol = Volumes, ovol = NULL; vol; vol = nvol) {
1354 if (vol->v_localname == NULL) {
1355 if (Volumes == vol) {
1360 ovol->v_next = nvol;
1370 /* ------------------------------- */
1371 static void volume_unlink(struct vol *volume)
1373 struct vol *vol, *ovol, *nvol;
1375 if (volume == Volumes) {
1376 Volumes = Volumes->v_next;
1379 for ( vol = Volumes->v_next, ovol = Volumes; vol; vol = nvol) {
1382 if (vol == volume) {
1383 ovol->v_next = nvol;
1392 static int getvolspace(struct vol *vol,
1393 u_int32_t *bfree, u_int32_t *btotal,
1394 VolSpace *xbfree, VolSpace *xbtotal, u_int32_t *bsize)
1398 #ifndef NO_QUOTA_SUPPORT
1399 VolSpace qfree, qtotal;
1402 spaceflag = AFPVOL_GVSMASK & vol->v_flags;
1403 /* report up to 2GB if afp version is < 2.2 (4GB if not) */
1404 maxsize = (vol->v_flags & AFPVOL_A2VOL) ? 0x01fffe00 :
1405 (((afp_version < 22) || (vol->v_flags & AFPVOL_LIMITSIZE))
1406 ? 0x7fffffffL : 0xffffffffL);
1409 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_AFSGVS ) {
1410 if ( afs_getvolspace( vol, xbfree, xbtotal, bsize ) == AFP_OK ) {
1411 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_AFSGVS;
1412 goto getvolspace_done;
1417 if (( rc = ustatfs_getvolspace( vol, xbfree, xbtotal, bsize)) != AFP_OK ) {
1421 #define min(a,b) ((a)<(b)?(a):(b))
1422 #ifndef NO_QUOTA_SUPPORT
1423 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_UQUOTA ) {
1424 if ( uquota_getvolspace( vol, &qfree, &qtotal, *bsize ) == AFP_OK ) {
1425 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_UQUOTA;
1426 *xbfree = min(*xbfree, qfree);
1427 *xbtotal = min( *xbtotal, qtotal);
1428 goto getvolspace_done;
1432 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_USTATFS;
1435 if (vol->v_limitsize) {
1436 /* FIXME: Free could be limit minus (total minus used), */
1437 /* which will confuse the client less ? */
1438 *xbfree = min(*xbfree, (vol->v_limitsize * 1024 * 1024));
1439 *xbtotal = min(*xbtotal, (vol->v_limitsize * 1024 * 1024));
1442 *bfree = min( *xbfree, maxsize);
1443 *btotal = min( *xbtotal, maxsize);
1447 /* -----------------------
1448 * set volume creation date
1449 * avoid duplicate, well at least it tries
1451 static void vol_setdate(u_int16_t id, struct adouble *adp, time_t date)
1454 struct vol *vol = Volumes;
1456 for ( volume = Volumes; volume; volume = volume->v_next ) {
1457 if (volume->v_vid == id) {
1460 else if ((time_t)(AD_DATE_FROM_UNIX(date)) == volume->v_ctime) {
1462 volume = Volumes; /* restart */
1465 vol->v_ctime = AD_DATE_FROM_UNIX(date);
1466 ad_setdate(adp, AD_DATE_CREATE | AD_DATE_UNIX, date);
1469 /* ----------------------- */
1470 static int getvolparams( u_int16_t bitmap, struct vol *vol, struct stat *st, char *buf, size_t *buflen)
1473 int bit = 0, isad = 1;
1476 u_int32_t bfree, btotal, bsize;
1477 VolSpace xbfree, xbtotal; /* extended bytes */
1478 char *data, *nameoff = NULL;
1481 LOG(log_debug, logtype_afpd, "getvolparams: Volume '%s'", vol->v_localname);
1483 /* courtesy of jallison@whistle.com:
1484 * For MacOS8.x support we need to create the
1485 * .Parent file here if it doesn't exist. */
1487 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1488 if ( ad_open_metadata( vol->v_path, ADFLAGS_DIR, O_CREAT, &ad) < 0 ) {
1490 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1492 } else if (ad_get_MD_flags( &ad ) & O_CREAT) {
1493 slash = strrchr( vol->v_path, '/' );
1497 slash = vol->v_path;
1498 if (ad_getentryoff(&ad, ADEID_NAME)) {
1499 ad_setentrylen( &ad, ADEID_NAME, strlen( slash ));
1500 memcpy(ad_entry( &ad, ADEID_NAME ), slash,
1501 ad_getentrylen( &ad, ADEID_NAME ));
1503 vol_setdate(vol->v_vid, &ad, st->st_mtime);
1507 if (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0)
1508 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1510 vol->v_ctime = aint;
1513 if (( bitmap & ( (1<<VOLPBIT_BFREE)|(1<<VOLPBIT_BTOTAL) |
1514 (1<<VOLPBIT_XBFREE)|(1<<VOLPBIT_XBTOTAL) |
1515 (1<<VOLPBIT_BSIZE)) ) != 0 ) {
1516 if ( getvolspace( vol, &bfree, &btotal, &xbfree, &xbtotal,
1517 &bsize) != AFP_OK ) {
1519 ad_close( &ad, ADFLAGS_HF );
1521 return( AFPERR_PARAM );
1526 while ( bitmap != 0 ) {
1527 while (( bitmap & 1 ) == 0 ) {
1535 /* check for read-only.
1536 * NOTE: we don't actually set the read-only flag unless
1537 * it's passed in that way as it's possible to mount
1538 * a read-write filesystem under a read-only one. */
1539 if ((vol->v_flags & AFPVOL_RO) ||
1540 ((utime(vol->v_path, NULL) < 0) && (errno == EROFS))) {
1541 ashort |= VOLPBIT_ATTR_RO;
1543 /* prior 2.1 only VOLPBIT_ATTR_RO is defined */
1544 if (afp_version > 20) {
1545 if (0 == (vol->v_flags & AFPVOL_NOFILEID) && vol->v_cdb != NULL &&
1546 (vol->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1547 ashort |= VOLPBIT_ATTR_FILEID;
1549 ashort |= VOLPBIT_ATTR_CATSEARCH;
1551 if (afp_version >= 30) {
1552 ashort |= VOLPBIT_ATTR_UTF8;
1553 if (vol->v_flags & AFPVOL_UNIX_PRIV)
1554 ashort |= VOLPBIT_ATTR_UNIXPRIV;
1555 if (vol->v_flags & AFPVOL_TM)
1556 ashort |= VOLPBIT_ATTR_TM;
1558 if (afp_version >= 32) {
1560 ashort |= VOLPBIT_ATTR_EXT_ATTRS;
1561 if (vol->v_flags & AFPVOL_ACLS)
1562 ashort |= VOLPBIT_ATTR_ACLS;
1566 ashort = htons(ashort);
1567 memcpy(data, &ashort, sizeof( ashort ));
1568 data += sizeof( ashort );
1572 ashort = htons( AFPVOLSIG_DEFAULT );
1573 memcpy(data, &ashort, sizeof( ashort ));
1574 data += sizeof( ashort );
1577 case VOLPBIT_CDATE :
1578 aint = vol->v_ctime;
1579 memcpy(data, &aint, sizeof( aint ));
1580 data += sizeof( aint );
1583 case VOLPBIT_MDATE :
1584 if ( st->st_mtime > vol->v_mtime ) {
1585 vol->v_mtime = st->st_mtime;
1587 aint = AD_DATE_FROM_UNIX(vol->v_mtime);
1588 memcpy(data, &aint, sizeof( aint ));
1589 data += sizeof( aint );
1592 case VOLPBIT_BDATE :
1593 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1594 aint = AD_DATE_START;
1595 memcpy(data, &aint, sizeof( aint ));
1596 data += sizeof( aint );
1600 memcpy(data, &vol->v_vid, sizeof( vol->v_vid ));
1601 data += sizeof( vol->v_vid );
1604 case VOLPBIT_BFREE :
1605 bfree = htonl( bfree );
1606 memcpy(data, &bfree, sizeof( bfree ));
1607 data += sizeof( bfree );
1610 case VOLPBIT_BTOTAL :
1611 btotal = htonl( btotal );
1612 memcpy(data, &btotal, sizeof( btotal ));
1613 data += sizeof( btotal );
1616 #ifndef NO_LARGE_VOL_SUPPORT
1617 case VOLPBIT_XBFREE :
1618 xbfree = hton64( xbfree );
1619 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1620 bcopy(&xbfree, data, sizeof(xbfree));
1621 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1622 memcpy(data, &xbfree, sizeof( xbfree ));
1623 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1624 data += sizeof( xbfree );
1627 case VOLPBIT_XBTOTAL :
1628 xbtotal = hton64( xbtotal );
1629 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1630 bcopy(&xbtotal, data, sizeof(xbtotal));
1631 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1632 memcpy(data, &xbtotal, sizeof( xbtotal ));
1633 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1634 data += sizeof( xbfree );
1636 #endif /* ! NO_LARGE_VOL_SUPPORT */
1640 data += sizeof( u_int16_t );
1643 case VOLPBIT_BSIZE: /* block size */
1644 bsize = htonl(bsize);
1645 memcpy(data, &bsize, sizeof(bsize));
1646 data += sizeof(bsize);
1651 ad_close( &ad, ADFLAGS_HF );
1653 return( AFPERR_BITMAP );
1659 ashort = htons( data - buf );
1660 memcpy(nameoff, &ashort, sizeof( ashort ));
1661 /* name is always in mac charset */
1662 aint = ucs2_to_charset( vol->v_maccharset, vol->v_macname, data+1, AFPVOL_MACNAMELEN + 1);
1672 ad_close_metadata( &ad);
1674 *buflen = data - buf;
1678 /* ------------------------- */
1679 static int stat_vol(u_int16_t bitmap, struct vol *vol, char *rbuf, size_t *rbuflen)
1685 if ( stat( vol->v_path, &st ) < 0 ) {
1687 return( AFPERR_PARAM );
1689 /* save the volume device number */
1690 vol->v_dev = st.st_dev;
1692 buflen = *rbuflen - sizeof( bitmap );
1693 if (( ret = getvolparams( bitmap, vol, &st,
1694 rbuf + sizeof( bitmap ), &buflen )) != AFP_OK ) {
1698 *rbuflen = buflen + sizeof( bitmap );
1699 bitmap = htons( bitmap );
1700 memcpy(rbuf, &bitmap, sizeof( bitmap ));
1705 /* ------------------------------- */
1706 void load_volumes(AFPObj *obj)
1708 struct passwd *pwent;
1713 /* check files date */
1714 if (obj->options.defaultvol.loaded) {
1715 changed = volfile_changed(&obj->options.defaultvol);
1717 if (obj->options.systemvol.loaded) {
1718 changed |= volfile_changed(&obj->options.systemvol);
1720 if (obj->options.uservol.loaded) {
1721 changed |= volfile_changed(&obj->options.uservol);
1730 if (! ((DSI *)obj->handle)->child) {
1731 LOG(log_debug, logtype_afpd, "load_volumes: AFP MASTER");
1733 LOG(log_debug, logtype_afpd, "load_volumes: user: %s", obj->username);
1736 pwent = getpwnam(obj->username);
1737 if ( (obj->options.flags & OPTION_USERVOLFIRST) == 0 ) {
1738 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent);
1741 if ((*obj->username == '\0') || (obj->options.flags & OPTION_NOUSERVOL)) {
1742 readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent);
1745 * Read user's AppleVolumes or .AppleVolumes file
1746 * If neither are readable, read the default volumes file. if
1747 * that doesn't work, create a user share.
1749 if (obj->options.uservol.name) {
1750 free(obj->options.uservol.name);
1752 obj->options.uservol.name = strdup(pwent->pw_dir);
1753 if ( readvolfile(obj, &obj->options.uservol, "AppleVolumes", 1, pwent) < 0 &&
1754 readvolfile(obj, &obj->options.uservol, ".AppleVolumes", 1, pwent) < 0 &&
1755 readvolfile(obj, &obj->options.uservol, "applevolumes", 1, pwent) < 0 &&
1756 readvolfile(obj, &obj->options.uservol, ".applevolumes", 1, pwent) < 0 &&
1757 obj->options.defaultvol.name != NULL ) {
1758 if (readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent) < 0)
1759 creatvol(obj, pwent, pwent->pw_dir, NULL, NULL, 1);
1762 if ( obj->options.flags & OPTION_USERVOLFIRST ) {
1763 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent );
1766 if ( obj->options.closevol ) {
1769 for ( vol = Volumes; vol; vol = vol->v_next ) {
1770 if (vol->v_deleted && !vol->v_new ) {
1779 /* ------------------------------- */
1780 int afp_getsrvrparms(AFPObj *obj, char *ibuf _U_, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
1793 for ( vcnt = 0, volume = Volumes; volume; volume = volume->v_next ) {
1794 if (!(volume->v_flags & AFPVOL_NOSTAT)) {
1797 if ( stat( volume->v_path, &st ) < 0 ) {
1798 LOG(log_info, logtype_afpd, "afp_getsrvrparms(%s): stat: %s",
1799 volume->v_path, strerror(errno) );
1800 continue; /* can't access directory */
1802 if (!S_ISDIR(st.st_mode)) {
1803 continue; /* not a dir */
1805 accessmode(volume->v_path, &ma, NULL, &st);
1806 if ((ma.ma_user & (AR_UREAD | AR_USEARCH)) != (AR_UREAD | AR_USEARCH)) {
1807 continue; /* no r-x access */
1810 if (volume->v_hide) {
1811 continue; /* config file changed but the volume was mounted */
1814 if (utf8_encoding()) {
1815 len = ucs2_to_charset_allocate(CH_UTF8_MAC, &namebuf, volume->v_u8mname);
1817 len = ucs2_to_charset_allocate(obj->options.maccharset, &namebuf, volume->v_macname);
1820 if (len == (size_t)-1)
1823 /* set password bit if there's a volume password */
1824 *data = (volume->v_password) ? AFPSRVR_PASSWD : 0;
1826 /* Apple 2 clients running ProDOS-8 expect one volume to have
1827 bit 0 of this byte set. They will not recognize anything
1828 on the server unless this is the case. I have not
1829 completely worked this out, but it's related to booting
1830 from the server. Support for that function is a ways
1831 off.. <shirsch@ibm.net> */
1832 *data |= (volume->v_flags & AFPVOL_A2VOL) ? AFPSRVR_CONFIGINFO : 0;
1833 *data++ |= 0; /* UNIX PRIVS BIT ..., OSX doesn't seem to use it, so we don't either */
1835 memcpy(data, namebuf, len );
1841 *rbuflen = data - rbuf;
1843 if ( gettimeofday( &tv, NULL ) < 0 ) {
1844 LOG(log_error, logtype_afpd, "afp_getsrvrparms(%s): gettimeofday: %s", volume->v_path, strerror(errno) );
1846 return AFPERR_PARAM;
1848 tv.tv_sec = AD_DATE_FROM_UNIX(tv.tv_sec);
1849 memcpy(data, &tv.tv_sec, sizeof( u_int32_t));
1850 data += sizeof( u_int32_t);
1855 /* ------------------------- */
1856 static int volume_codepage(AFPObj *obj, struct vol *volume)
1858 struct charset_functions *charset;
1861 if (!volume->v_volcodepage)
1862 volume->v_volcodepage = strdup("UTF8");
1864 if ( (charset_t) -1 == ( volume->v_volcharset = add_charset(volume->v_volcodepage)) ) {
1865 LOG (log_error, logtype_afpd, "Setting codepage %s as volume codepage failed", volume->v_volcodepage);
1869 if ( NULL == (charset = find_charset_functions(volume->v_volcodepage)) || charset->flags & CHARSET_ICONV ) {
1870 LOG (log_warning, logtype_afpd, "WARNING: volume encoding %s is *not* supported by netatalk, expect problems !!!!", volume->v_volcodepage);
1873 if (!volume->v_maccodepage)
1874 volume->v_maccodepage = strdup(obj->options.maccodepage);
1876 if ( (charset_t) -1 == ( volume->v_maccharset = add_charset(volume->v_maccodepage)) ) {
1877 LOG (log_error, logtype_afpd, "Setting codepage %s as mac codepage failed", volume->v_maccodepage);
1881 if ( NULL == ( charset = find_charset_functions(volume->v_maccodepage)) || ! (charset->flags & CHARSET_CLIENT) ) {
1882 LOG (log_error, logtype_afpd, "Fatal error: mac charset %s not supported", volume->v_maccodepage);
1885 volume->v_kTextEncoding = htonl(charset->kTextEncoding);
1889 /* ------------------------- */
1890 static int volume_openDB(struct vol *volume)
1894 if ((volume->v_flags & AFPVOL_NODEV)) {
1895 flags |= CNID_FLAG_NODEV;
1898 if (volume->v_cnidscheme == NULL) {
1899 volume->v_cnidscheme = strdup(DEFAULT_CNID_SCHEME);
1900 LOG(log_info, logtype_afpd, "Volume %s use CNID scheme %s.",
1901 volume->v_path, volume->v_cnidscheme);
1904 LOG(log_info, logtype_afpd, "CNID server: %s:%s",
1905 volume->v_cnidserver ? volume->v_cnidserver : Cnid_srv,
1906 volume->v_cnidport ? volume->v_cnidport : Cnid_port);
1909 /* Found this in branch dir-rewrite, maybe we want to use it sometimes */
1911 /* Legacy pre 2.1 way of sharing eg CD-ROM */
1912 if (strcmp(volume->v_cnidscheme, "last") == 0) {
1913 /* "last" is gone. We support it by switching to in-memory "tdb" */
1914 volume->v_cnidscheme = strdup("tdb");
1915 flags |= CNID_FLAG_MEMORY;
1918 /* New way of sharing CD-ROM */
1919 if (volume->v_flags & AFPVOL_CDROM) {
1920 flags |= CNID_FLAG_MEMORY;
1921 if (strcmp(volume->v_cnidscheme, "tdb") != 0) {
1922 free(volume->v_cnidscheme);
1923 volume->v_cnidscheme = strdup("tdb");
1924 LOG(log_info, logtype_afpd, "Volume %s is ejectable, switching to scheme %s.",
1925 volume->v_path, volume->v_cnidscheme);
1930 volume->v_cdb = cnid_open(volume->v_dbpath ? volume->v_dbpath : volume->v_path,
1932 volume->v_cnidscheme,
1934 volume->v_cnidserver ? volume->v_cnidserver : Cnid_srv,
1935 volume->v_cnidport ? volume->v_cnidport : Cnid_port);
1937 if ( ! volume->v_cdb && ! (flags & CNID_FLAG_MEMORY)) {
1938 /* The first attempt failed and it wasn't yet an attempt to open in-memory */
1939 LOG(log_error, logtype_afpd, "Can't open volume \"%s\" CNID backend \"%s\" ",
1940 volume->v_path, volume->v_cnidscheme);
1941 LOG(log_error, logtype_afpd, "Reopen volume %s using in memory temporary CNID DB.",
1943 flags |= CNID_FLAG_MEMORY;
1944 volume->v_cdb = cnid_open (volume->v_path, volume->v_umask, "tdb", flags, NULL, NULL);
1946 /* kill ourself with SIGUSR2 aka msg pending */
1947 if (volume->v_cdb) {
1948 setmessage("Something wrong with the volume's CNID DB, using temporary CNID DB instead."
1949 "Check server messages for details!");
1950 kill(getpid(), SIGUSR2);
1951 /* deactivate cnid caching/storing in AppleDouble files */
1952 volume->v_flags &= ~AFPVOL_CACHE;
1957 return (!volume->v_cdb)?-1:0;
1961 Check if the underlying filesystem supports EAs for ea:sys volumes.
1962 If not, switch to ea:ad.
1963 As we can't check (requires write access) on ro-volumes, we switch ea:auto
1964 volumes that are options:ro to ea:none.
1966 static void check_ea_sys_support(struct vol *vol)
1968 uid_t process_uid = 0;
1969 char eaname[] = {"org.netatalk.supports-eas.XXXXXX"};
1970 const char *eacontent = "yes";
1972 if (vol->v_vfs_ea == AFPVOL_EA_AUTO) {
1974 if ((vol->v_flags & AFPVOL_RO) == AFPVOL_RO) {
1975 LOG(log_info, logtype_logger, "read-only volume '%s', can't test for EA support, disabling EAs", vol->v_localname);
1976 vol->v_vfs_ea = AFPVOL_EA_NONE;
1982 process_uid = geteuid();
1984 if (seteuid(0) == -1) {
1985 LOG(log_error, logtype_logger, "check_ea_sys_support: can't seteuid(0): %s", strerror(errno));
1989 if ((sys_setxattr(vol->v_path, eaname, eacontent, 4, 0)) == 0) {
1990 sys_removexattr(vol->v_path, eaname);
1991 vol->v_vfs_ea = AFPVOL_EA_SYS;
1993 LOG(log_warning, logtype_afpd, "volume \"%s\" does not support Extended Attributes, using ea:ad instead",
1995 vol->v_vfs_ea = AFPVOL_EA_AD;
1999 if (seteuid(process_uid) == -1) {
2000 LOG(log_error, logtype_logger, "can't seteuid back %s", strerror(errno));
2007 /* -------------------------
2008 * we are the user here
2010 int afp_openvol(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2021 char path[ MAXPATHLEN + 1];
2027 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2028 bitmap = ntohs( bitmap );
2029 ibuf += sizeof( bitmap );
2032 if (( bitmap & (1<<VOLPBIT_VID)) == 0 ) {
2033 return AFPERR_BITMAP;
2036 len = (unsigned char)*ibuf++;
2037 volname = obj->oldtmp;
2039 if ((volname_tmp = strchr(volname,'+')) != NULL)
2040 volname = volname_tmp+1;
2042 if (utf8_encoding()) {
2043 namelen = convert_string(CH_UTF8_MAC, CH_UCS2, ibuf, len, volname, sizeof(obj->oldtmp));
2045 namelen = convert_string(obj->options.maccharset, CH_UCS2, ibuf, len, volname, sizeof(obj->oldtmp));
2048 if ( namelen <= 0) {
2049 return AFPERR_PARAM;
2053 if ((len + 1) & 1) /* pad to an even boundary */
2058 for ( volume = Volumes; volume; volume = volume->v_next ) {
2059 if ( strcasecmp_w( (ucs2_t*) volname, volume->v_name ) == 0 ) {
2064 if ( volume == NULL ) {
2065 return AFPERR_PARAM;
2068 /* check for a volume password */
2069 if (volume->v_password && strncmp(ibuf, volume->v_password, VOLPASSLEN)) {
2070 return AFPERR_ACCESS;
2073 if (( volume->v_flags & AFPVOL_OPEN ) ) {
2074 /* the volume is already open */
2076 set_uidgid ( volume );
2078 return stat_vol(bitmap, volume, rbuf, rbuflen);
2081 if (volume->v_root_preexec) {
2082 if ((ret = afprun(1, volume->v_root_preexec, NULL)) && volume->v_root_preexec_close) {
2083 LOG(log_error, logtype_afpd, "afp_openvol(%s): root preexec : %d", volume->v_path, ret );
2089 set_uidgid ( volume );
2092 if (volume->v_preexec) {
2093 if ((ret = afprun(0, volume->v_preexec, NULL)) && volume->v_preexec_close) {
2094 LOG(log_error, logtype_afpd, "afp_openvol(%s): preexec : %d", volume->v_path, ret );
2099 if ( stat( volume->v_path, &st ) < 0 ) {
2100 return AFPERR_PARAM;
2103 if ( chdir( volume->v_path ) < 0 ) {
2104 return AFPERR_PARAM;
2107 if ( NULL == getcwd(path, MAXPATHLEN)) {
2108 /* shouldn't be fatal but it will fail later */
2109 LOG(log_error, logtype_afpd, "afp_openvol(%s): volume pathlen too long", volume->v_path);
2113 /* Normalize volume path */
2114 #ifdef REALPATH_TAKES_NULL
2115 if ((volume->v_path = realpath(path, NULL)) == NULL)
2118 if ((volume->v_path = malloc(MAXPATHLEN+1)) == NULL)
2120 if (realpath(path, volume->v_path) == NULL) {
2121 free(volume->v_path);
2124 /* Safe some memory */
2126 if ((tmp = strdup(volume->v_path)) == NULL) {
2127 free(volume->v_path);
2130 free(volume->v_path);
2131 volume->v_path = tmp;
2134 if (volume_codepage(obj, volume) < 0) {
2139 /* initialize volume variables
2142 if (utf8_encoding()) {
2143 volume->max_filename = 255;
2146 volume->max_filename = MACFILELEN;
2149 volume->v_flags |= AFPVOL_OPEN;
2150 volume->v_cdb = NULL;
2152 if (utf8_encoding()) {
2153 len = convert_string_allocate(CH_UCS2, CH_UTF8_MAC, volume->v_u8mname, namelen, &vol_mname);
2155 len = convert_string_allocate(CH_UCS2, obj->options.maccharset, volume->v_macname, namelen, &vol_mname);
2157 if ( !vol_mname || len <= 0) {
2162 if ((vol_uname = strrchr(path, '/')) == NULL)
2164 else if (*(vol_uname + 1) != '\0')
2167 if ((dir = dir_new(vol_mname,
2172 bfromcstr(volume->v_path))
2175 LOG(log_error, logtype_afpd, "afp_openvol(%s): malloc: %s", volume->v_path, strerror(errno) );
2181 volume->v_root = dir;
2184 if (volume_openDB(volume) < 0) {
2185 LOG(log_error, logtype_afpd, "Fatal error: cannot open CNID or invalid CNID backend for %s: %s",
2186 volume->v_path, volume->v_cnidscheme);
2191 ret = stat_vol(bitmap, volume, rbuf, rbuflen);
2192 if (ret == AFP_OK) {
2194 if (!(volume->v_flags & AFPVOL_RO)) {
2195 handle_special_folders( volume );
2197 volume->v_cnidserver ? volume->v_cnidserver : Cnid_srv,
2198 volume->v_cnidport ? volume->v_cnidport : Cnid_port);
2202 * If you mount a volume twice, the second time the trash appears on
2203 * the desk-top. That's because the Mac remembers the DID for the
2204 * trash (even for volumes in different zones, on different servers).
2205 * Just so this works better, we prime the DID cache with the trash,
2206 * fixing the trash at DID 17.
2207 * FIXME (RL): should it be done inside a CNID backend ? (always returning Trash DID when asked) ?
2209 if ((volume->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
2211 /* FIXME find db time stamp */
2212 if (cnid_getstamp(volume->v_cdb, volume->v_stamp, sizeof(volume->v_stamp)) < 0) {
2213 LOG (log_error, logtype_afpd,
2214 "afp_openvol(%s): Fatal error: Unable to get stamp value from CNID backend",
2222 cname( volume, volume->v_root, &p );
2228 if (volume->v_root) {
2229 dir_free( volume->v_root );
2230 volume->v_root = NULL;
2233 volume->v_flags &= ~AFPVOL_OPEN;
2234 if (volume->v_cdb != NULL) {
2235 cnid_close(volume->v_cdb);
2236 volume->v_cdb = NULL;
2242 /* ------------------------- */
2243 static void closevol(struct vol *vol)
2248 dir_free( vol->v_root );
2250 if (vol->v_cdb != NULL) {
2251 cnid_close(vol->v_cdb);
2255 if (vol->v_postexec) {
2256 afprun(0, vol->v_postexec, NULL);
2258 if (vol->v_root_postexec) {
2259 afprun(1, vol->v_root_postexec, NULL);
2263 /* ------------------------- */
2264 void close_all_vol(void)
2268 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
2269 if ( (ovol->v_flags & AFPVOL_OPEN) ) {
2270 ovol->v_flags &= ~AFPVOL_OPEN;
2276 /* ------------------------- */
2277 static void deletevol(struct vol *vol)
2281 vol->v_flags &= ~AFPVOL_OPEN;
2282 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
2283 if ( (ovol->v_flags & AFPVOL_OPEN) ) {
2287 if ( ovol != NULL ) {
2288 /* Even if chdir fails, we can't say afp_closevol fails. */
2289 if ( chdir( ovol->v_path ) == 0 ) {
2290 curdir = ovol->v_root;
2295 if (vol->v_deleted) {
2296 showvol(vol->v_name);
2303 /* ------------------------- */
2304 int afp_closevol(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2311 memcpy(&vid, ibuf, sizeof( vid ));
2312 if (NULL == ( vol = getvolbyvid( vid )) ) {
2313 return( AFPERR_PARAM );
2321 /* ------------------------- */
2322 struct vol *getvolbyvid(const u_int16_t vid )
2326 for ( vol = Volumes; vol; vol = vol->v_next ) {
2327 if ( vid == vol->v_vid ) {
2331 if ( vol == NULL || ( vol->v_flags & AFPVOL_OPEN ) == 0 ) {
2337 #endif /* FORCE_UIDGID */
2342 /* ------------------------ */
2343 static int ext_cmp_key(const void *key, const void *obj)
2345 const char *p = key;
2346 const struct extmap *em = obj;
2347 return strdiacasecmp(p, em->em_ext);
2349 struct extmap *getextmap(const char *path)
2354 if (!Extmap_cnt || NULL == ( p = strrchr( path, '.' )) ) {
2355 return( Defextmap );
2359 return( Defextmap );
2361 em = bsearch(p, Extmap, Extmap_cnt, sizeof(struct extmap), ext_cmp_key);
2365 return( Defextmap );
2369 /* ------------------------- */
2370 struct extmap *getdefextmap(void)
2372 return( Defextmap );
2375 /* --------------------------
2376 poll if a volume is changed by other processes.
2378 0 no attention msg sent
2379 1 attention msg sent
2380 -1 error (socket closed)
2382 Note: if attention return -1 no packet has been
2383 sent because the buffer is full, we don't care
2384 either there's no reader or there's a lot of
2385 traffic and another pollvoltime will follow
2387 int pollvoltime(AFPObj *obj)
2394 if (!(afp_version > 21 && obj->options.server_notif))
2397 if ( gettimeofday( &tv, NULL ) < 0 )
2400 for ( vol = Volumes; vol; vol = vol->v_next ) {
2401 if ( (vol->v_flags & AFPVOL_OPEN) && vol->v_mtime + 30 < tv.tv_sec) {
2402 if ( !stat( vol->v_path, &st ) && vol->v_mtime != st.st_mtime ) {
2403 vol->v_mtime = st.st_mtime;
2404 if (!obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED))
2413 /* ------------------------- */
2414 void setvoltime(AFPObj *obj, struct vol *vol)
2418 /* just looking at vol->v_mtime is broken seriously since updates
2419 * from other users afpd processes never are seen.
2420 * This is not the most elegant solution (a shared memory between
2421 * the afpd processes would come closer)
2424 if ( gettimeofday( &tv, NULL ) < 0 ) {
2425 LOG(log_error, logtype_afpd, "setvoltime(%s): gettimeofday: %s", vol->v_path, strerror(errno) );
2428 if( utime( vol->v_path, NULL ) < 0 ) {
2429 /* write of time failed ... probably a read only filesys,
2430 * where no other users can interfere, so there's no issue here
2434 /* a little granularity */
2435 if (vol->v_mtime < tv.tv_sec) {
2436 vol->v_mtime = tv.tv_sec;
2437 /* or finder doesn't update free space
2438 * AFP 3.2 and above clients seem to be ok without so many notification
2440 if (afp_version < 32 && obj->options.server_notif) {
2441 obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED);
2446 /* ------------------------- */
2447 int afp_getvolparams(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_,char *rbuf, size_t *rbuflen)
2450 u_int16_t vid, bitmap;
2453 memcpy(&vid, ibuf, sizeof( vid ));
2454 ibuf += sizeof( vid );
2455 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2456 bitmap = ntohs( bitmap );
2458 if (NULL == ( vol = getvolbyvid( vid )) ) {
2460 return( AFPERR_PARAM );
2463 return stat_vol(bitmap, vol, rbuf, rbuflen);
2466 /* ------------------------- */
2467 int afp_setvolparams(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2471 u_int16_t vid, bitmap;
2477 memcpy(&vid, ibuf, sizeof( vid ));
2478 ibuf += sizeof( vid );
2479 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2480 bitmap = ntohs( bitmap );
2481 ibuf += sizeof(bitmap);
2483 if (( vol = getvolbyvid( vid )) == NULL ) {
2484 return( AFPERR_PARAM );
2487 if ((vol->v_flags & AFPVOL_RO))
2488 return AFPERR_VLOCK;
2490 /* we can only set the backup date. */
2491 if (bitmap != (1 << VOLPBIT_BDATE))
2492 return AFPERR_BITMAP;
2494 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2495 if ( ad_open( vol->v_path, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR,
2498 return AFPERR_VLOCK;
2500 return AFPERR_ACCESS;
2503 memcpy(&aint, ibuf, sizeof(aint));
2504 ad_setdate(&ad, AD_DATE_BACKUP, aint);
2506 ad_close(&ad, ADFLAGS_HF);
2510 /* ------------------------- */
2511 int wincheck(const struct vol *vol, const char *path)
2515 if (!(vol->v_flags & AFPVOL_MSWINDOWS))
2518 /* empty paths are not allowed */
2519 if ((len = strlen(path)) == 0)
2522 /* leading or trailing whitespaces are not allowed, carriage returns
2523 * and probably other whitespace is okay, tabs are not allowed
2525 if ((path[0] == ' ') || (path[len-1] == ' '))
2528 /* certain characters are not allowed */
2529 if (strpbrk(path, MSWINDOWS_BADCHARS))
2532 /* everything else is okay */
2538 * precreate a folder
2539 * this is only intended for folders in the volume root
2540 * It will *not* work if the folder name contains extended characters
2542 static int create_special_folder (const struct vol *vol, const struct _special_folder *folder)
2551 p = (char *) malloc ( strlen(vol->v_path)+strlen(folder->name)+2);
2553 LOG(log_error, logtype_afpd,"malloc failed");
2557 q=strdup(folder->name);
2559 LOG(log_error, logtype_afpd,"malloc failed");
2563 strcpy(p, vol->v_path);
2568 if ((vol->v_casefold & AFPVOL_MTOUUPPER))
2570 else if ((vol->v_casefold & AFPVOL_MTOULOWER))
2576 if ( (ret = stat( p, &st )) < 0 ) {
2577 if (folder->precreate) {
2578 if (ad_mkdir(p, folder->mode)) {
2579 LOG(log_debug, logtype_afpd,"Creating '%s' failed in %s: %s", p, vol->v_path, strerror(errno));
2588 if ( !ret && folder->hide) {
2590 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2591 if (ad_open_metadata( p, ADFLAGS_DIR, O_CREAT, &ad) < 0) {
2597 ad_setname(&ad, folder->name);
2599 ad_getattr(&ad, &attr);
2600 attr |= htons( ntohs( attr ) | ATTRBIT_INVISIBLE );
2601 ad_setattr(&ad, attr);
2603 /* do the same with the finder info */
2604 if (ad_entry(&ad, ADEID_FINDERI)) {
2605 memcpy(&attr, ad_entry(&ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF, sizeof(attr));
2606 attr |= htons(FINDERINFO_INVISIBLE);
2607 memcpy(ad_entry(&ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF,&attr, sizeof(attr));
2611 ad_close_metadata( &ad);
2618 static void handle_special_folders (const struct vol * vol)
2620 const _special_folder *p = &special_folders[0];
2622 if ((vol->v_flags & AFPVOL_RO))
2625 for (; p->name != NULL; p++) {
2626 create_special_folder (vol, p);
2630 const struct vol *getvolumes(void)
2635 void unload_volumes_and_extmap(void)
2637 LOG(log_debug, logtype_afpd, "unload_volumes_and_extmap");
2643 * Get a volumes UUID from the config file.
2644 * If there is none, it is generated and stored there.
2646 * Returns pointer to allocated storage on success, NULL on error.
2648 char *get_uuid(const AFPObj *obj, const char *volname)
2651 char buf[1024], uuid[UUID_PRINTABLE_STRING_LENGTH], *p;
2653 struct stat tmpstat;
2656 if ((fp = fopen(obj->options.uuidconf, "r")) != NULL) { /* read open? */
2657 /* scan in the conf file */
2658 while (fgets(buf, sizeof(buf), fp) != NULL) {
2660 while (p && isblank(*p))
2662 if (!p || (*p == '#') || (*p == '\n'))
2663 continue; /* invalid line */
2666 if ((volname_conf = strtok( p, "\"" )) == NULL)
2667 continue; /* syntax error */
2669 if ((volname_conf = strtok( p, " \t" )) == NULL)
2670 continue; /* syntax error: invalid name */
2672 p = strchr(p, '\0');
2675 continue; /* syntax error */
2677 if (strcmp(volname, volname_conf) != 0)
2678 continue; /* another volume name */
2680 while (p && isblank(*p))
2683 if (sscanf(p, "%36s", uuid) == 1 ) {
2684 for (int i=0; uuid[i]; i++)
2685 uuid[i] = toupper(uuid[i]);
2686 LOG(log_debug, logtype_afpd, "get_uuid('%s'): UUID: '%s'", volname, uuid);
2688 return strdup(uuid);
2696 /* not found or no file, reopen in append mode */
2698 if (stat(obj->options.uuidconf, &tmpstat)) { /* no file */
2699 if (( fd = creat(obj->options.uuidconf, 0644 )) < 0 ) {
2700 LOG(log_error, logtype_atalkd, "ERROR: Cannot create %s (%s).",
2701 obj->options.uuidconf, strerror(errno));
2704 if (( fp = fdopen( fd, "w" )) == NULL ) {
2705 LOG(log_error, logtype_atalkd, "ERROR: Cannot fdopen %s (%s).",
2706 obj->options.uuidconf, strerror(errno));
2710 } else if ((fp = fopen(obj->options.uuidconf, "a+")) == NULL) { /* not found */
2711 LOG(log_error, logtype_afpd, "Cannot create or append to %s (%s).",
2712 obj->options.uuidconf, strerror(errno));
2715 fseek(fp, 0L, SEEK_END);
2716 if(ftell(fp) == 0) { /* size = 0 */
2717 fprintf(fp, "# DON'T TOUCH NOR COPY THOUGHTLESSLY!\n");
2718 fprintf(fp, "# This file is auto-generated by afpd\n");
2719 fprintf(fp, "# and stores UUIDs for TM volumes.\n\n");
2721 fseek(fp, -1L, SEEK_END);
2722 if(fgetc(fp) != '\n') fputc('\n', fp); /* last char is \n? */
2725 /* generate uuid and write to file */
2728 uuid_unparse(id, uuid);
2729 for (int i=0; uuid[i]; i++)
2730 uuid[i] = toupper(uuid[i]);
2731 LOG(log_debug, logtype_afpd, "get_uuid('%s'): generated UUID '%s'", volname, uuid);
2733 fprintf(fp, "\"%s\"\t%36s\n", volname, uuid);
2736 return strdup(uuid);