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 <atalk/asp.h>
40 #include <atalk/dsi.h>
41 #include <atalk/adouble.h>
42 #include <atalk/afp.h>
43 #include <atalk/util.h>
44 #include <atalk/volinfo.h>
45 #include <atalk/logger.h>
46 #include <atalk/vfs.h>
47 #include <atalk/uuid.h>
50 #include <atalk/cnid.h>
54 #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
85 struct vol *current_vol; /* last volume from getvolbyvid() */
87 static struct vol *Volumes = NULL;
88 static u_int16_t lastvid = 0;
89 static char *Trash = "\02\024Network Trash Folder";
91 static struct extmap *Extmap = NULL, *Defextmap = NULL;
92 static int Extmap_cnt;
93 static void free_extmap(void);
95 #define VOLOPT_ALLOW 0 /* user allow list */
96 #define VOLOPT_DENY 1 /* user deny list */
97 #define VOLOPT_RWLIST 2 /* user rw list */
98 #define VOLOPT_ROLIST 3 /* user ro list */
99 #define VOLOPT_PASSWORD 4 /* volume password */
100 #define VOLOPT_CASEFOLD 5 /* character case mangling */
101 #define VOLOPT_FLAGS 6 /* various flags */
102 #define VOLOPT_DBPATH 7 /* path to database */
103 #define VOLOPT_LIMITSIZE 8 /* Limit the size of the volume */
105 #define VOLOPT_VETO 10 /* list of veto filespec */
106 #define VOLOPT_PREEXEC 11 /* preexec command */
107 #define VOLOPT_ROOTPREEXEC 12 /* root preexec command */
108 #define VOLOPT_POSTEXEC 13 /* postexec command */
109 #define VOLOPT_ROOTPOSTEXEC 14 /* root postexec command */
110 #define VOLOPT_ENCODING 15 /* mac encoding (pre OSX)*/
111 #define VOLOPT_MACCHARSET 16
112 #define VOLOPT_CNIDSCHEME 17
113 #define VOLOPT_ADOUBLE 18 /* adouble version */
119 #define VOLOPT_FORCEUID 19 /* force uid for username x */
120 #define VOLOPT_FORCEGID 20 /* force gid for group x */
121 #endif /* FORCE_UIDGID */
123 #define VOLOPT_UMASK 21
124 #define VOLOPT_ALLOWED_HOSTS 22
125 #define VOLOPT_DENIED_HOSTS 23
126 #define VOLOPT_DPERM 24 /* dperm default directories perms */
127 #define VOLOPT_FPERM 25 /* fperm default files perms */
128 #define VOLOPT_DFLTPERM 26 /* perm */
129 #define VOLOPT_EA_VFS 27 /* Extended Attributes vfs indirection */
130 #define VOLOPT_CNIDSERVER 28 /* CNID Server ip address*/
131 #define VOLOPT_CNIDPORT 30 /* CNID server tcp port */
133 #define VOLOPT_MAX 31 /* <== IMPORTANT !!!!!! */
134 #define VOLOPT_NUM (VOLOPT_MAX + 1)
137 #define VOLOPT_DEFAULT ":DEFAULT:"
138 #define VOLOPT_DEFAULT_LEN 9
145 typedef struct _special_folder {
152 static const _special_folder special_folders[] = {
153 {"Network Trash Folder", 1, 0777, 1},
154 {"Temporary Items", 1, 0777, 1},
155 {".AppleDesktop", 1, 0777, 0},
157 {"TheFindByContentFolder", 0, 0, 1},
158 {"TheVolumeSettingsFolder", 0, 0, 1},
162 /* Forward declarations */
163 static void handle_special_folders (const struct vol *);
164 static void deletevol(struct vol *vol);
165 static void volume_free(struct vol *vol);
166 static void check_ea_sys_support(struct vol *vol);
168 static void volfree(struct vol_option *options, const struct vol_option *save)
173 for (i = 0; i < VOLOPT_MAX; i++) {
174 if (options[i].c_value && (options[i].c_value != save[i].c_value))
175 free(options[i].c_value);
178 for (i = 0; i < VOLOPT_MAX; i++) {
179 if (options[i].c_value)
180 free(options[i].c_value);
186 #define is_var(a, b) (strncmp((a), (b), 2) == 0)
189 * Handle variable substitutions. here's what we understand:
190 * $b -> basename of path
191 * $c -> client ip/appletalk address
192 * $d -> volume pathname on server
193 * $f -> full name (whatever's in the gecos field)
196 * $i -> client ip/appletalk address without port
197 * $s -> server name (hostname if it doesn't exist)
198 * $u -> username (guest is usually nobody)
199 * $v -> volume name or basename if null
200 * $z -> zone (may not exist)
203 * This get's called from readvolfile with
204 * path = NULL, volname = NULL for xlating the volumes path
205 * path = path, volname = NULL for xlating the volumes name
206 * ... and from volumes options parsing code when xlating eg dbpath with
207 * path = path, volname = volname
209 * Using this information we can reject xlation of any variable depeninding on a login
210 * context which is not given in the afp master, where we must evaluate this whole stuff
211 * too for the Zeroconf announcements.
213 static char *volxlate(AFPObj *obj,
226 int xlatevolname = 0;
228 if (! ((DSI *)obj->handle)->child)
231 if (path && !volname)
239 dest = calloc(destlen +1, 1);
245 strlcpy(dest, src, destlen +1);
246 if ((p = strchr(src, '$')) == NULL) /* nothing to do */
249 /* first part of the path. just forward to the next variable. */
250 len = MIN((size_t)(p - src), destlen);
256 while (p && destlen > 0) {
257 /* now figure out what the variable is */
259 if (is_var(p, "$b")) {
260 if (afpmaster && xlatevolname)
263 if ((q = strrchr(path, '/')) == NULL)
265 else if (*(q + 1) != '\0')
268 } else if (is_var(p, "$c")) {
269 if (afpmaster && xlatevolname)
271 if (obj->proto == AFPPROTO_ASP) {
272 ASP asp = obj->handle;
274 len = sprintf(dest, "%u.%u", ntohs(asp->asp_sat.sat_addr.s_net),
275 asp->asp_sat.sat_addr.s_node);
279 } else if (obj->proto == AFPPROTO_DSI) {
280 DSI *dsi = obj->handle;
281 len = sprintf(dest, "%s:%u",
282 getip_string((struct sockaddr *)&dsi->client),
283 getip_port((struct sockaddr *)&dsi->client));
287 } else if (is_var(p, "$d")) {
288 if (afpmaster && xlatevolname)
291 } else if (pwd && is_var(p, "$f")) {
292 if (afpmaster && xlatevolname)
294 if ((r = strchr(pwd->pw_gecos, ',')))
297 } else if (pwd && is_var(p, "$g")) {
298 if (afpmaster && xlatevolname)
300 struct group *grp = getgrgid(pwd->pw_gid);
303 } else if (is_var(p, "$h")) {
304 q = obj->options.hostname;
305 } else if (is_var(p, "$i")) {
306 if (afpmaster && xlatevolname)
308 if (obj->proto == AFPPROTO_ASP) {
309 ASP asp = obj->handle;
311 len = sprintf(dest, "%u", ntohs(asp->asp_sat.sat_addr.s_net));
315 } else if (obj->proto == AFPPROTO_DSI) {
316 DSI *dsi = obj->handle;
317 q = getip_string((struct sockaddr *)&dsi->client);
319 } else if (is_var(p, "$s")) {
322 else if (obj->options.server) {
323 q = obj->options.server;
325 q = obj->options.hostname;
326 } else if (obj->username && is_var(p, "$u")) {
327 if (afpmaster && xlatevolname)
330 if ( obj->options.ntseparator && (sep = strchr(obj->username, obj->options.ntseparator[0])) != NULL)
334 } else if (is_var(p, "$v")) {
335 if (afpmaster && xlatevolname)
341 if ((q = strrchr(path, '/')) == NULL)
343 else if (*(q + 1) != '\0')
346 } else if (is_var(p, "$z")) {
348 } else if (is_var(p, "$$")) {
353 /* copy the stuff over. if we don't understand something that we
354 * should, just skip it over. */
356 len = MIN(p == q ? 2 : strlen(q), destlen);
357 strncpy(dest, q, len);
362 /* stuff up to next $ */
364 p = strchr(src, '$');
365 len = p ? MIN((size_t)(p - src), destlen) : destlen;
367 strncpy(dest, src, len);
375 /* to make sure that val is valid, make sure to select an opt that
377 static int optionok(const char *buf, const char *opt, const char *val)
379 if (!strstr(buf,opt))
387 /* -------------------- */
388 static void setoption(struct vol_option *options, struct vol_option *save, int opt, const char *val)
390 if (options[opt].c_value && (!save || options[opt].c_value != save[opt].c_value))
391 free(options[opt].c_value);
392 options[opt].c_value = strdup(val + 1);
395 /* ------------------------------------------
396 handle all the options. tmp can't be NULL. */
397 static void volset(struct vol_option *options, struct vol_option *save,
398 char *volname, int vlen,
403 val = strchr(tmp, ':');
405 /* we'll assume it's a volume name. */
406 strncpy(volname, tmp, vlen);
411 LOG(log_debug, logtype_afpd, "Parsing volset %s", val);
413 if (optionok(tmp, "allow:", val)) {
414 setoption(options, save, VOLOPT_ALLOW, val);
416 } else if (optionok(tmp, "deny:", val)) {
417 setoption(options, save, VOLOPT_DENY, val);
419 } else if (optionok(tmp, "rwlist:", val)) {
420 setoption(options, save, VOLOPT_RWLIST, val);
422 } else if (optionok(tmp, "rolist:", val)) {
423 setoption(options, save, VOLOPT_ROLIST, val);
425 } else if (optionok(tmp, "codepage:", val)) {
426 LOG (log_error, logtype_afpd, "The old codepage system has been removed. Please make sure to read the documentation !!!!");
427 /* Make sure we don't screw anything */
429 } else if (optionok(tmp, "volcharset:", val)) {
430 setoption(options, save, VOLOPT_ENCODING, val);
431 } else if (optionok(tmp, "maccharset:", val)) {
432 setoption(options, save, VOLOPT_MACCHARSET, val);
433 } else if (optionok(tmp, "veto:", val)) {
434 setoption(options, save, VOLOPT_VETO, val);
435 } else if (optionok(tmp, "cnidscheme:", val)) {
436 setoption(options, save, VOLOPT_CNIDSCHEME, val);
437 } else if (optionok(tmp, "casefold:", val)) {
438 if (strcasecmp(val + 1, "tolower") == 0)
439 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMLOWER;
440 else if (strcasecmp(val + 1, "toupper") == 0)
441 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMUPPER;
442 else if (strcasecmp(val + 1, "xlatelower") == 0)
443 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UUPPERMLOWER;
444 else if (strcasecmp(val + 1, "xlateupper") == 0)
445 options[VOLOPT_CASEFOLD].i_value = AFPVOL_ULOWERMUPPER;
446 } else if (optionok(tmp, "adouble:", val)) {
447 if (strcasecmp(val + 1, "v1") == 0)
448 options[VOLOPT_ADOUBLE].i_value = AD_VERSION1;
449 #if AD_VERSION == AD_VERSION2
450 else if (strcasecmp(val + 1, "v2") == 0)
451 options[VOLOPT_ADOUBLE].i_value = AD_VERSION2;
452 else if (strcasecmp(val + 1, "osx") == 0)
453 options[VOLOPT_ADOUBLE].i_value = AD_VERSION2_OSX;
454 else if (strcasecmp(val + 1, "sfm") == 0)
455 options[VOLOPT_ADOUBLE].i_value = AD_VERSION1_SFM;
457 } else if (optionok(tmp, "options:", val)) {
460 if ((p = strtok(val + 1, ",")) == NULL) /* nothing */
464 if (strcasecmp(p, "prodos") == 0)
465 options[VOLOPT_FLAGS].i_value |= AFPVOL_A2VOL;
466 else if (strcasecmp(p, "mswindows") == 0) {
467 options[VOLOPT_FLAGS].i_value |= AFPVOL_MSWINDOWS | AFPVOL_USEDOTS;
468 } else if (strcasecmp(p, "crlf") == 0)
469 options[VOLOPT_FLAGS].i_value |= AFPVOL_CRLF;
470 else if (strcasecmp(p, "noadouble") == 0)
471 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOADOUBLE;
472 else if (strcasecmp(p, "ro") == 0)
473 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
474 else if (strcasecmp(p, "nohex") == 0)
475 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOHEX;
476 else if (strcasecmp(p, "usedots") == 0)
477 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS;
478 else if (strcasecmp(p, "invisibledots") == 0)
479 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS | AFPVOL_INV_DOTS;
480 else if (strcasecmp(p, "limitsize") == 0)
481 options[VOLOPT_FLAGS].i_value |= AFPVOL_LIMITSIZE;
482 /* support for either "dropbox" or "dropkludge" */
483 else if (strcasecmp(p, "dropbox") == 0)
484 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
485 else if (strcasecmp(p, "dropkludge") == 0)
486 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
487 else if (strcasecmp(p, "nofileid") == 0)
488 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOFILEID;
489 else if (strcasecmp(p, "nostat") == 0)
490 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOSTAT;
491 else if (strcasecmp(p, "preexec_close") == 0)
492 options[VOLOPT_PREEXEC].i_value = 1;
493 else if (strcasecmp(p, "root_preexec_close") == 0)
494 options[VOLOPT_ROOTPREEXEC].i_value = 1;
495 else if (strcasecmp(p, "upriv") == 0)
496 options[VOLOPT_FLAGS].i_value |= AFPVOL_UNIX_PRIV;
497 else if (strcasecmp(p, "nodev") == 0)
498 options[VOLOPT_FLAGS].i_value |= AFPVOL_NODEV;
499 else if (strcasecmp(p, "caseinsensitive") == 0)
500 options[VOLOPT_FLAGS].i_value |= AFPVOL_CASEINSEN;
501 else if (strcasecmp(p, "illegalseq") == 0)
502 options[VOLOPT_FLAGS].i_value |= AFPVOL_EILSEQ;
503 else if (strcasecmp(p, "nocnidcache") == 0)
504 options[VOLOPT_FLAGS].i_value &= ~AFPVOL_CACHE;
505 else if (strcasecmp(p, "tm") == 0)
506 options[VOLOPT_FLAGS].i_value |= AFPVOL_TM;
507 /* Found this in branch dir-rewrite, maybe we want to use it sometimes */
509 else if (strcasecmp(p, "cdrom") == 0)
510 options[VOLOPT_FLAGS].i_value |= AFPVOL_CDROM | AFPVOL_RO;
512 p = strtok(NULL, ",");
515 } else if (optionok(tmp, "cnidserver:", val)) {
516 setoption(options, save, VOLOPT_CNIDSERVER, val);
518 char *p = strrchr(val + 1, ':');
521 setoption(options, save, VOLOPT_CNIDPORT, p);
524 LOG(log_debug, logtype_afpd, "CNID Server for volume '%s': %s:%s",
525 volname, val + 1, p ? p + 1 : Cnid_port);
527 } else if (optionok(tmp, "dbpath:", val)) {
528 setoption(options, save, VOLOPT_DBPATH, val);
530 } else if (optionok(tmp, "umask:", val)) {
531 options[VOLOPT_UMASK].i_value = (int)strtol(val +1, NULL, 8);
532 } else if (optionok(tmp, "dperm:", val)) {
533 options[VOLOPT_DPERM].i_value = (int)strtol(val+1, NULL, 8);
534 } else if (optionok(tmp, "fperm:", val)) {
535 options[VOLOPT_FPERM].i_value = (int)strtol(val+1, NULL, 8);
536 } else if (optionok(tmp, "perm:", val)) {
537 options[VOLOPT_DFLTPERM].i_value = (int)strtol(val+1, NULL, 8);
538 } else if (optionok(tmp, "password:", val)) {
539 setoption(options, save, VOLOPT_PASSWORD, val);
543 /* this code allows forced uid/gid per volume settings */
544 } else if (optionok(tmp, "forceuid:", val)) {
545 setoption(options, save, VOLOPT_FORCEUID, val);
546 } else if (optionok(tmp, "forcegid:", val)) {
547 setoption(options, save, VOLOPT_FORCEGID, val);
549 #endif /* FORCE_UIDGID */
550 } else if (optionok(tmp, "root_preexec:", val)) {
551 setoption(options, save, VOLOPT_ROOTPREEXEC, val);
553 } else if (optionok(tmp, "preexec:", val)) {
554 setoption(options, save, VOLOPT_PREEXEC, val);
556 } else if (optionok(tmp, "root_postexec:", val)) {
557 setoption(options, save, VOLOPT_ROOTPOSTEXEC, val);
559 } else if (optionok(tmp, "postexec:", val)) {
560 setoption(options, save, VOLOPT_POSTEXEC, val);
562 } else if (optionok(tmp, "allowed_hosts:", val)) {
563 setoption(options, save, VOLOPT_ALLOWED_HOSTS, val);
565 } else if (optionok(tmp, "denied_hosts:", val)) {
566 setoption(options, save, VOLOPT_DENIED_HOSTS, val);
568 } else if (optionok(tmp, "ea:", val)) {
569 if (strcasecmp(val + 1, "ad") == 0)
570 options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_AD;
571 else if (strcasecmp(val + 1, "sys") == 0)
572 options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_SYS;
573 else if (strcasecmp(val + 1, "none") == 0)
574 options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_NONE;
576 } else if (optionok(tmp, "volsizelimit:", val)) {
577 options[VOLOPT_LIMITSIZE].i_value = (uint32_t)strtoul(val + 1, NULL, 10);
580 /* ignore unknown options */
581 LOG(log_debug, logtype_afpd, "ignoring unknown volume option: %s", tmp);
586 /* ----------------- */
587 static void showvol(const ucs2_t *name)
590 for ( volume = Volumes; volume; volume = volume->v_next ) {
591 if (volume->v_hide && !strcasecmp_w( volume->v_name, name ) ) {
598 /* ------------------------------- */
599 static int creatvol(AFPObj *obj, struct passwd *pwd,
600 char *path, char *name,
601 struct vol_option *options,
602 const int user /* user defined volume */
606 int suffixlen, vlen, tmpvlen, u8mvlen, macvlen;
608 char tmpname[AFPVOL_U8MNAMELEN+1];
609 ucs2_t u8mtmpname[(AFPVOL_U8MNAMELEN+1)*2], mactmpname[(AFPVOL_MACNAMELEN+1)*2];
610 char suffix[6]; /* max is #FFFF */
613 LOG(log_debug, logtype_afpd, "createvol: Volume '%s'", name);
615 if ( name == NULL || *name == '\0' ) {
616 if ((name = strrchr( path, '/' )) == NULL) {
617 return -1; /* Obviously not a fully qualified path */
620 /* if you wish to share /, you need to specify a name. */
625 /* suffix for mangling use (lastvid + 1) */
626 /* because v_vid has not been decided yet. */
627 suffixlen = sprintf(suffix, "%c%X", MANGLE_CHAR, lastvid + 1 );
629 vlen = strlen( name );
631 /* Unicode Volume Name */
632 /* Firstly convert name from unixcharset to UTF8-MAC */
634 tmpvlen = convert_charset(obj->options.unixcharset, CH_UTF8_MAC, 0, name, vlen, tmpname, AFPVOL_U8MNAMELEN, &flags);
636 strcpy(tmpname, "???");
640 /* Do we have to mangle ? */
641 if ( (flags & CONV_REQMANGLE) || (tmpvlen > obj->options.volnamelen)) {
642 if (tmpvlen + suffixlen > obj->options.volnamelen) {
644 tmpvlen = convert_charset(obj->options.unixcharset, CH_UTF8_MAC, 0, name, vlen, tmpname, obj->options.volnamelen - suffixlen, &flags);
645 tmpname[tmpvlen >= 0 ? tmpvlen : 0] = 0;
647 strcat(tmpname, suffix);
648 tmpvlen = strlen(tmpname);
651 /* Secondly convert name from UTF8-MAC to UCS2 */
652 if ( 0 >= ( u8mvlen = convert_string(CH_UTF8_MAC, CH_UCS2, tmpname, tmpvlen, u8mtmpname, AFPVOL_U8MNAMELEN*2)) )
655 LOG(log_maxdebug, logtype_afpd, "createvol: Volume '%s' -> UTF8-MAC Name: '%s'", name, tmpname);
657 /* Maccharset Volume Name */
658 /* Firsty convert name from unixcharset to maccharset */
660 tmpvlen = convert_charset(obj->options.unixcharset, obj->options.maccharset, 0, name, vlen, tmpname, AFPVOL_U8MNAMELEN, &flags);
662 strcpy(tmpname, "???");
666 /* Do we have to mangle ? */
667 if ( (flags & CONV_REQMANGLE) || (tmpvlen > AFPVOL_MACNAMELEN)) {
668 if (tmpvlen + suffixlen > AFPVOL_MACNAMELEN) {
670 tmpvlen = convert_charset(obj->options.unixcharset, obj->options.maccharset, 0, name, vlen, tmpname, AFPVOL_MACNAMELEN - suffixlen, &flags);
671 tmpname[tmpvlen >= 0 ? tmpvlen : 0] = 0;
673 strcat(tmpname, suffix);
674 tmpvlen = strlen(tmpname);
677 /* Secondly convert name from maccharset to UCS2 */
678 if ( 0 >= ( macvlen = convert_string(obj->options.maccharset, CH_UCS2, tmpname, tmpvlen, mactmpname, AFPVOL_U8MNAMELEN*2)) )
681 LOG(log_maxdebug, logtype_afpd, "createvol: Volume '%s' -> Longname: '%s'", name, tmpname);
683 /* check duplicate */
684 for ( volume = Volumes; volume; volume = volume->v_next ) {
685 if (( strcasecmp_w( volume->v_u8mname, u8mtmpname ) == 0 ) || ( strcasecmp_w( volume->v_macname, mactmpname ) == 0 )){
686 if (volume->v_deleted) {
687 volume->v_new = hide = 1;
690 return -1; /* Won't be able to access it, anyway... */
695 if (!( volume = (struct vol *)calloc(1, sizeof( struct vol ))) ) {
696 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
699 if ( NULL == ( volume->v_localname = strdup(name))) {
700 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
705 if ( NULL == ( volume->v_u8mname = strdup_w(u8mtmpname))) {
706 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
711 if ( NULL == ( volume->v_macname = strdup_w(mactmpname))) {
712 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
717 if (!( volume->v_path = (char *)malloc( strlen( path ) + 1 )) ) {
718 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
724 volume->v_name = utf8_encoding()?volume->v_u8mname:volume->v_macname;
725 volume->v_hide = hide;
726 strcpy( volume->v_path, path );
730 #endif /* __svr4__ */
731 /* os X start at 1 and use network order ie. 1 2 3 */
732 volume->v_vid = ++lastvid;
733 volume->v_vid = htons(volume->v_vid);
735 if (check_vol_acl_support(volume))
736 volume->v_flags |= AFPVOL_ACLS;
741 volume->v_casefold = options[VOLOPT_CASEFOLD].i_value;
742 volume->v_flags |= options[VOLOPT_FLAGS].i_value;
744 if (options[VOLOPT_EA_VFS].i_value)
745 volume->v_vfs_ea = options[VOLOPT_EA_VFS].i_value;
747 volume->v_ad_options = 0;
748 if ((volume->v_flags & AFPVOL_NODEV))
749 volume->v_ad_options |= ADVOL_NODEV;
750 if ((volume->v_flags & AFPVOL_CACHE))
751 volume->v_ad_options |= ADVOL_CACHE;
752 if ((volume->v_flags & AFPVOL_UNIX_PRIV))
753 volume->v_ad_options |= ADVOL_UNIXPRIV;
754 if ((volume->v_flags & AFPVOL_INV_DOTS))
755 volume->v_ad_options |= ADVOL_INVDOTS;
756 if ((volume->v_flags & AFPVOL_NOADOUBLE))
757 volume->v_ad_options |= ADVOL_NOADOUBLE;
759 if (options[VOLOPT_PASSWORD].c_value)
760 volume->v_password = strdup(options[VOLOPT_PASSWORD].c_value);
762 if (options[VOLOPT_VETO].c_value)
763 volume->v_veto = strdup(options[VOLOPT_VETO].c_value);
765 if (options[VOLOPT_ENCODING].c_value)
766 volume->v_volcodepage = strdup(options[VOLOPT_ENCODING].c_value);
768 if (options[VOLOPT_MACCHARSET].c_value)
769 volume->v_maccodepage = strdup(options[VOLOPT_MACCHARSET].c_value);
771 if (options[VOLOPT_DBPATH].c_value)
772 volume->v_dbpath = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_DBPATH].c_value, pwd, path, name);
774 if (options[VOLOPT_CNIDSCHEME].c_value)
775 volume->v_cnidscheme = strdup(options[VOLOPT_CNIDSCHEME].c_value);
777 if (options[VOLOPT_CNIDSERVER].c_value)
778 volume->v_cnidserver = strdup(options[VOLOPT_CNIDSERVER].c_value);
780 if (options[VOLOPT_CNIDPORT].c_value)
781 volume->v_cnidport = strdup(options[VOLOPT_CNIDPORT].c_value);
783 if (options[VOLOPT_UMASK].i_value)
784 volume->v_umask = (mode_t)options[VOLOPT_UMASK].i_value;
786 if (options[VOLOPT_DPERM].i_value)
787 volume->v_dperm = (mode_t)options[VOLOPT_DPERM].i_value;
789 if (options[VOLOPT_FPERM].i_value)
790 volume->v_fperm = (mode_t)options[VOLOPT_FPERM].i_value;
792 if (options[VOLOPT_DFLTPERM].i_value)
793 volume->v_perm = (mode_t)options[VOLOPT_DFLTPERM].i_value;
795 if (options[VOLOPT_ADOUBLE].i_value)
796 volume->v_adouble = options[VOLOPT_ADOUBLE].i_value;
798 volume->v_adouble = AD_VERSION;
800 if (options[VOLOPT_LIMITSIZE].i_value)
801 volume->v_limitsize = options[VOLOPT_LIMITSIZE].i_value;
803 /* Mac to Unix conversion flags*/
804 volume->v_mtou_flags = 0;
805 if (!(volume->v_flags & AFPVOL_NOHEX))
806 volume->v_mtou_flags |= CONV_ESCAPEHEX;
807 if (!(volume->v_flags & AFPVOL_USEDOTS))
808 volume->v_mtou_flags |= CONV_ESCAPEDOTS;
809 if ((volume->v_flags & AFPVOL_EILSEQ))
810 volume->v_mtou_flags |= CONV__EILSEQ;
812 if ((volume->v_casefold & AFPVOL_MTOUUPPER))
813 volume->v_mtou_flags |= CONV_TOUPPER;
814 else if ((volume->v_casefold & AFPVOL_MTOULOWER))
815 volume->v_mtou_flags |= CONV_TOLOWER;
817 /* Unix to Mac conversion flags*/
818 volume->v_utom_flags = CONV_IGNORE | CONV_UNESCAPEHEX;
819 if ((volume->v_casefold & AFPVOL_UTOMUPPER))
820 volume->v_utom_flags |= CONV_TOUPPER;
821 else if ((volume->v_casefold & AFPVOL_UTOMLOWER))
822 volume->v_utom_flags |= CONV_TOLOWER;
824 if ((volume->v_flags & AFPVOL_EILSEQ))
825 volume->v_utom_flags |= CONV__EILSEQ;
828 if (options[VOLOPT_FORCEUID].c_value) {
829 volume->v_forceuid = strdup(options[VOLOPT_FORCEUID].c_value);
831 volume->v_forceuid = NULL; /* set as null so as to return 0 later on */
834 if (options[VOLOPT_FORCEGID].c_value) {
835 volume->v_forcegid = strdup(options[VOLOPT_FORCEGID].c_value);
837 volume->v_forcegid = NULL; /* set as null so as to return 0 later on */
841 if (options[VOLOPT_PREEXEC].c_value)
842 volume->v_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_PREEXEC].c_value, pwd, path, name);
843 volume->v_preexec_close = options[VOLOPT_PREEXEC].i_value;
845 if (options[VOLOPT_POSTEXEC].c_value)
846 volume->v_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_POSTEXEC].c_value, pwd, path, name);
848 if (options[VOLOPT_ROOTPREEXEC].c_value)
849 volume->v_root_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPREEXEC].c_value, pwd, path, name);
850 volume->v_root_preexec_close = options[VOLOPT_ROOTPREEXEC].i_value;
852 if (options[VOLOPT_ROOTPOSTEXEC].c_value)
853 volume->v_root_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPOSTEXEC].c_value, pwd, path, name);
856 volume->v_dperm |= volume->v_perm;
857 volume->v_fperm |= volume->v_perm;
859 /* Check EA support on volume */
860 if (volume->v_vfs_ea == AFPVOL_EA_AUTO)
861 check_ea_sys_support(volume);
864 /* get/store uuid from file */
865 if (volume->v_flags & AFPVOL_TM) {
866 char *uuid = get_uuid(obj, volume->v_localname);
868 LOG(log_error, logtype_afpd, "Volume '%s': couldn't get UUID",
869 volume->v_localname);
871 volume->v_uuid = uuid;
872 LOG(log_debug, logtype_afpd, "Volume '%s': UUID '%s'",
873 volume->v_localname, volume->v_uuid);
877 volume->v_next = Volumes;
882 /* ---------------- */
883 static char *myfgets( char *buf, int size, FILE *fp)
889 while ((EOF != ( c = getc( fp )) ) && ( size > 1 )) {
890 if ( c == '\n' || c == '\r' ) {
891 if (p != buf && *(p -1) == '\\') {
913 /* check access list. this function wants something of the following
915 * @group,name,name2,@group2,name3
917 * a NULL argument allows everybody to have access.
918 * we return three things:
920 * 0: list exists, but name isn't in it
924 #ifndef NO_REAL_USER_NAME
925 /* authentication is case insensitive
926 * FIXME should we do the same with group name?
928 #define access_strcmp strcasecmp
931 #define access_strcmp strcmp
935 static int accessvol(const char *args, const char *name)
937 char buf[MAXPATHLEN + 1], *p;
943 strlcpy(buf, args, sizeof(buf));
944 if ((p = strtok(buf, ",")) == NULL) /* nothing, return okay */
948 if (*p == '@') { /* it's a group */
949 if ((gr = getgrnam(p + 1)) && gmem(gr->gr_gid))
951 } else if (access_strcmp(p, name) == 0) /* it's a user name */
953 p = strtok(NULL, ",");
959 static int hostaccessvol(int type, const char *volname, const char *args, const AFPObj *obj)
962 char buf[MAXPATHLEN + 1], *p, *b;
963 DSI *dsi = obj->handle;
964 struct sockaddr_storage client;
969 strlcpy(buf, args, sizeof(buf));
970 if ((p = strtok_r(buf, ",", &b)) == NULL) /* nothing, return okay */
973 if (obj->proto != AFPPROTO_DSI)
978 char *ipaddr, *mask_char;
979 struct addrinfo hints, *ai;
981 ipaddr = strtok(p, "/");
982 mask_char = strtok(NULL,"/");
984 /* Get address from string with getaddrinfo */
985 memset(&hints, 0, sizeof hints);
986 hints.ai_family = AF_UNSPEC;
987 hints.ai_socktype = SOCK_STREAM;
988 if ((ret = getaddrinfo(ipaddr, NULL, &hints, &ai)) != 0) {
989 LOG(log_error, logtype_afpd, "hostaccessvol: getaddrinfo: %s\n", gai_strerror(ret));
994 if (mask_char != NULL)
995 mask_int = atoi(mask_char); /* apply_ip_mask does range checking on it */
997 if (ai->ai_family == AF_INET) /* IPv4 */
1003 /* Apply mask to addresses */
1004 client = dsi->client;
1005 apply_ip_mask((struct sockaddr *)&client, mask_int);
1006 apply_ip_mask(ai->ai_addr, mask_int);
1008 if (compare_ip((struct sockaddr *)&client, ai->ai_addr) == 0) {
1009 if (type == VOLOPT_DENIED_HOSTS)
1010 LOG(log_info, logtype_afpd, "AFP access denied for client IP '%s' to volume '%s' by denied list",
1011 getip_string((struct sockaddr *)&client), volname);
1018 p = strtok_r(NULL, ",", &b);
1021 if (type == VOLOPT_ALLOWED_HOSTS)
1022 LOG(log_info, logtype_afpd, "AFP access denied for client IP '%s' to volume '%s', not in allowed list",
1023 getip_string((struct sockaddr *)&dsi->client), volname);
1027 static void setextmap(char *ext, char *type, char *creator, int user)
1032 if (Extmap == NULL) {
1033 if (( Extmap = calloc(1, sizeof( struct extmap ))) == NULL ) {
1034 LOG(log_error, logtype_afpd, "setextmap: calloc: %s", strerror(errno) );
1039 for ( em = Extmap, cnt = 0; em->em_ext; em++, cnt++) {
1040 if ( (strdiacasecmp( em->em_ext, ext )) == 0 ) {
1045 if ( em->em_ext == NULL ) {
1046 if (!(Extmap = realloc( Extmap, sizeof( struct extmap ) * (cnt +2))) ) {
1047 LOG(log_error, logtype_afpd, "setextmap: realloc: %s", strerror(errno) );
1050 (Extmap +cnt +1)->em_ext = NULL;
1052 } else if ( !user ) {
1058 if (!(em->em_ext = strdup( ext))) {
1059 LOG(log_error, logtype_afpd, "setextmap: strdup: %s", strerror(errno) );
1063 if ( *type == '\0' ) {
1064 memcpy(em->em_type, "\0\0\0\0", sizeof( em->em_type ));
1066 memcpy(em->em_type, type, sizeof( em->em_type ));
1068 if ( *creator == '\0' ) {
1069 memcpy(em->em_creator, "\0\0\0\0", sizeof( em->em_creator ));
1071 memcpy(em->em_creator, creator, sizeof( em->em_creator ));
1075 /* -------------------------- */
1076 static int extmap_cmp(const void *map1, const void *map2)
1078 const struct extmap *em1 = map1;
1079 const struct extmap *em2 = map2;
1080 return strdiacasecmp(em1->em_ext, em2->em_ext);
1083 static void sortextmap( void)
1088 if ((em = Extmap) == NULL) {
1091 while (em->em_ext) {
1096 qsort(Extmap, Extmap_cnt, sizeof(struct extmap), extmap_cmp);
1097 if (*Extmap->em_ext == 0) {
1098 /* the first line is really "." the default entry,
1099 * we remove the leading '.' in setextmap
1106 /* ----------------------
1108 static void free_extmap( void)
1113 for ( em = Extmap; em->em_ext; em++) {
1123 /* ----------------------
1125 static int volfile_changed(struct afp_volume_name *p)
1131 name = p->full_name;
1135 if (!stat( name, &st) && st.st_mtime > p->mtime) {
1136 p->mtime = st.st_mtime;
1142 /* ----------------------
1143 * Read a volume configuration file and add the volumes contained within to
1144 * the global volume list. This gets called from the forked afpd childs.
1145 * The master now reads this too for Zeroconf announcements.
1147 * If p2 is non-NULL, the file that is opened is
1150 * Lines that begin with # and blank lines are ignored.
1151 * Volume lines are of the form:
1152 * <unix path> [<volume name>] [allow:<user>,<@group>,...] \
1153 * [codepage:<file>] [casefold:<num>]
1154 * <extension> TYPE [CREATOR]
1157 int readvolfile(AFPObj *obj, struct afp_volume_name *p1, char *p2, int user, struct passwd *pwent)
1160 char path[MAXPATHLEN + 1];
1161 char tmp[MAXPATHLEN + 1];
1162 char volname[AFPVOL_U8MNAMELEN + 1];
1164 char type[5], creator[5];
1169 struct vol_option save_options[VOLOPT_NUM];
1170 struct vol_option options[VOLOPT_NUM];
1176 strcpy( path, p1->name );
1178 strcat( path, "/" );
1180 if (p1->full_name) {
1181 free(p1->full_name);
1183 p1->full_name = strdup(path);
1186 if (NULL == ( fp = fopen( path, "r" )) ) {
1190 if (fd != -1 && !fstat( fd, &st) ) {
1191 p1->mtime = st.st_mtime;
1194 memset(save_options, 0, sizeof(save_options));
1196 /* Enable some default options for all volumes */
1197 save_options[VOLOPT_FLAGS].i_value |= AFPVOL_CACHE;
1198 save_options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_AUTO;
1199 LOG(log_maxdebug, logtype_afpd, "readvolfile: seeding default umask: %04o",
1200 obj->options.umask);
1201 save_options[VOLOPT_UMASK].i_value = obj->options.umask;
1203 LOG(log_debug, logtype_afpd, "readvolfile: \"%s\"", path);
1205 while ( myfgets( buf, sizeof( buf ), fp ) != NULL ) {
1206 initline( strlen( buf ), buf );
1207 parseline( sizeof( path ) - 1, path );
1214 /* change the default options for this file */
1215 if (strncmp(path, VOLOPT_DEFAULT, VOLOPT_DEFAULT_LEN) == 0) {
1217 for (i = 0; i < VOLOPT_NUM; i++) {
1218 if (parseline( sizeof( path ) - VOLOPT_DEFAULT_LEN - 1,
1219 path + VOLOPT_DEFAULT_LEN) < 0)
1221 volset(save_options, NULL, tmp, sizeof(tmp) - 1,
1222 path + VOLOPT_DEFAULT_LEN);
1228 if (( p = strchr( path, '/' )) != NULL ) {
1236 if ( u == NULL || *u == '\0' || ( pw = getpwnam( u )) == NULL ) {
1239 strcpy( tmp, pw->pw_dir );
1240 if ( p != NULL && *p != '\0' ) {
1247 /* send path through variable substitution */
1248 if (*path != '~') /* need to copy path to tmp */
1250 if (!pwent && obj->username)
1251 pwent = getpwnam(obj->username);
1253 if (volxlate(obj, path, sizeof(path) - 1, tmp, pwent, NULL, NULL) == NULL)
1256 /* this is sort of braindead. basically, i want to be
1257 * able to specify things in any order, but i don't want to
1258 * re-write everything. */
1260 memcpy(options, save_options, sizeof(options));
1263 /* read in up to VOLOP_NUM possible options */
1264 for (i = 0; i < VOLOPT_NUM; i++) {
1265 if (parseline( sizeof( tmp ) - 1, tmp ) < 0)
1268 volset(options, save_options, volname, sizeof(volname) - 1, tmp);
1271 /* check allow/deny lists (if not afpd master loading volumes for Zeroconf reg.):
1272 allow -> either no list (-1), or in list (1)
1273 deny -> either no list (-1), or not in list (0) */
1274 if (!((DSI *)obj->handle)->child
1276 (accessvol(options[VOLOPT_ALLOW].c_value, obj->username) &&
1277 (accessvol(options[VOLOPT_DENY].c_value, obj->username) < 1) &&
1278 hostaccessvol(VOLOPT_ALLOWED_HOSTS, volname, options[VOLOPT_ALLOWED_HOSTS].c_value, obj) &&
1279 (hostaccessvol(VOLOPT_DENIED_HOSTS, volname, options[VOLOPT_DENIED_HOSTS].c_value, obj) < 1))) {
1281 /* handle read-only behaviour. semantics:
1282 * 1) neither the rolist nor the rwlist exist -> rw
1283 * 2) rolist exists -> ro if user is in it.
1284 * 3) rwlist exists -> ro unless user is in it. */
1285 if (((DSI *)obj->handle)->child
1287 ((options[VOLOPT_FLAGS].i_value & AFPVOL_RO) == 0)
1289 ((accessvol(options[VOLOPT_ROLIST].c_value, obj->username) == 1) ||
1290 !accessvol(options[VOLOPT_RWLIST].c_value, obj->username)))
1291 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
1293 /* do variable substitution for volname */
1294 if (volxlate(obj, tmp, sizeof(tmp) - 1, volname, pwent, path, NULL) == NULL)
1297 creatvol(obj, pwent, path, tmp, options, p2 != NULL);
1299 volfree(options, save_options);
1303 parseline( sizeof( type ) - 1, type );
1304 parseline( sizeof( creator ) - 1, creator );
1305 setextmap( path, type, creator, user);
1312 volfree(save_options, NULL);
1314 if ( fclose( fp ) != 0 ) {
1315 LOG(log_error, logtype_afpd, "readvolfile: fclose: %s", strerror(errno) );
1321 /* ------------------------------- */
1322 static void volume_free(struct vol *vol)
1324 free(vol->v_localname);
1325 vol->v_localname = NULL;
1326 free(vol->v_u8mname);
1327 vol->v_u8mname = NULL;
1328 free(vol->v_macname);
1329 vol->v_macname = NULL;
1331 free(vol->v_password);
1333 free(vol->v_volcodepage);
1334 free(vol->v_maccodepage);
1335 free(vol->v_cnidscheme);
1336 free(vol->v_dbpath);
1339 free(vol->v_forceuid);
1340 free(vol->v_forcegid);
1341 #endif /* FORCE_UIDGID */
1346 /* ------------------------------- */
1347 static void free_volumes(void )
1350 struct vol *nvol, *ovol;
1352 for ( vol = Volumes; vol; vol = vol->v_next ) {
1353 if (( vol->v_flags & AFPVOL_OPEN ) ) {
1360 for ( vol = Volumes, ovol = NULL; vol; vol = nvol) {
1363 if (vol->v_localname == NULL) {
1364 if (Volumes == vol) {
1369 ovol->v_next = nvol;
1379 /* ------------------------------- */
1380 static void volume_unlink(struct vol *volume)
1382 struct vol *vol, *ovol, *nvol;
1384 if (volume == Volumes) {
1385 Volumes = Volumes->v_next;
1388 for ( vol = Volumes->v_next, ovol = Volumes; vol; vol = nvol) {
1391 if (vol == volume) {
1392 ovol->v_next = nvol;
1401 static int getvolspace(struct vol *vol,
1402 u_int32_t *bfree, u_int32_t *btotal,
1403 VolSpace *xbfree, VolSpace *xbtotal, u_int32_t *bsize)
1407 #ifndef NO_QUOTA_SUPPORT
1408 VolSpace qfree, qtotal;
1411 spaceflag = AFPVOL_GVSMASK & vol->v_flags;
1412 /* report up to 2GB if afp version is < 2.2 (4GB if not) */
1413 maxsize = (vol->v_flags & AFPVOL_A2VOL) ? 0x01fffe00 :
1414 (((afp_version < 22) || (vol->v_flags & AFPVOL_LIMITSIZE))
1415 ? 0x7fffffffL : 0xffffffffL);
1418 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_AFSGVS ) {
1419 if ( afs_getvolspace( vol, xbfree, xbtotal, bsize ) == AFP_OK ) {
1420 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_AFSGVS;
1421 goto getvolspace_done;
1426 if (( rc = ustatfs_getvolspace( vol, xbfree, xbtotal, bsize)) != AFP_OK ) {
1430 #define min(a,b) ((a)<(b)?(a):(b))
1431 #ifndef NO_QUOTA_SUPPORT
1432 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_UQUOTA ) {
1433 if ( uquota_getvolspace( vol, &qfree, &qtotal, *bsize ) == AFP_OK ) {
1434 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_UQUOTA;
1435 *xbfree = min(*xbfree, qfree);
1436 *xbtotal = min( *xbtotal, qtotal);
1437 goto getvolspace_done;
1441 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_USTATFS;
1444 if (vol->v_limitsize) {
1445 /* FIXME: Free could be limit minus (total minus used), */
1446 /* which will confuse the client less ? */
1447 *xbfree = min(*xbfree, (vol->v_limitsize * 1024 * 1024));
1448 *xbtotal = min(*xbtotal, (vol->v_limitsize * 1024 * 1024));
1451 *bfree = min( *xbfree, maxsize);
1452 *btotal = min( *xbtotal, maxsize);
1456 /* -----------------------
1457 * set volume creation date
1458 * avoid duplicate, well at least it tries
1460 static void vol_setdate(u_int16_t id, struct adouble *adp, time_t date)
1463 struct vol *vol = Volumes;
1465 for ( volume = Volumes; volume; volume = volume->v_next ) {
1466 if (volume->v_vid == id) {
1469 else if ((time_t)(AD_DATE_FROM_UNIX(date)) == volume->v_ctime) {
1471 volume = Volumes; /* restart */
1474 vol->v_ctime = AD_DATE_FROM_UNIX(date);
1475 ad_setdate(adp, AD_DATE_CREATE | AD_DATE_UNIX, date);
1478 /* ----------------------- */
1479 static int getvolparams( u_int16_t bitmap, struct vol *vol, struct stat *st, char *buf, size_t *buflen)
1482 int bit = 0, isad = 1;
1485 u_int32_t bfree, btotal, bsize;
1486 VolSpace xbfree, xbtotal; /* extended bytes */
1487 char *data, *nameoff = NULL;
1490 LOG(log_debug, logtype_afpd, "getvolparams: Volume '%s'", vol->v_localname);
1492 /* courtesy of jallison@whistle.com:
1493 * For MacOS8.x support we need to create the
1494 * .Parent file here if it doesn't exist. */
1496 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1497 if ( ad_open_metadata( vol->v_path, ADFLAGS_DIR, O_CREAT, &ad) < 0 ) {
1499 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1501 } else if (ad_get_MD_flags( &ad ) & O_CREAT) {
1502 slash = strrchr( vol->v_path, '/' );
1506 slash = vol->v_path;
1507 if (ad_getentryoff(&ad, ADEID_NAME)) {
1508 ad_setentrylen( &ad, ADEID_NAME, strlen( slash ));
1509 memcpy(ad_entry( &ad, ADEID_NAME ), slash,
1510 ad_getentrylen( &ad, ADEID_NAME ));
1512 vol_setdate(vol->v_vid, &ad, st->st_mtime);
1516 if (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0)
1517 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1519 vol->v_ctime = aint;
1522 if (( bitmap & ( (1<<VOLPBIT_BFREE)|(1<<VOLPBIT_BTOTAL) |
1523 (1<<VOLPBIT_XBFREE)|(1<<VOLPBIT_XBTOTAL) |
1524 (1<<VOLPBIT_BSIZE)) ) != 0 ) {
1525 if ( getvolspace( vol, &bfree, &btotal, &xbfree, &xbtotal,
1526 &bsize) != AFP_OK ) {
1528 ad_close( &ad, ADFLAGS_HF );
1530 return( AFPERR_PARAM );
1535 while ( bitmap != 0 ) {
1536 while (( bitmap & 1 ) == 0 ) {
1544 /* check for read-only.
1545 * NOTE: we don't actually set the read-only flag unless
1546 * it's passed in that way as it's possible to mount
1547 * a read-write filesystem under a read-only one. */
1548 if ((vol->v_flags & AFPVOL_RO) ||
1549 ((utime(vol->v_path, NULL) < 0) && (errno == EROFS))) {
1550 ashort |= VOLPBIT_ATTR_RO;
1552 /* prior 2.1 only VOLPBIT_ATTR_RO is defined */
1553 if (afp_version > 20) {
1554 if (0 == (vol->v_flags & AFPVOL_NOFILEID) && vol->v_cdb != NULL &&
1555 (vol->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1556 ashort |= VOLPBIT_ATTR_FILEID;
1558 ashort |= VOLPBIT_ATTR_CATSEARCH;
1560 if (afp_version >= 30) {
1561 ashort |= VOLPBIT_ATTR_UTF8;
1562 if (vol->v_flags & AFPVOL_UNIX_PRIV)
1563 ashort |= VOLPBIT_ATTR_UNIXPRIV;
1564 if (vol->v_flags & AFPVOL_TM)
1565 ashort |= VOLPBIT_ATTR_TM;
1567 if (afp_version >= 32) {
1569 ashort |= VOLPBIT_ATTR_EXT_ATTRS;
1570 if (vol->v_flags & AFPVOL_ACLS)
1571 ashort |= VOLPBIT_ATTR_ACLS;
1575 ashort = htons(ashort);
1576 memcpy(data, &ashort, sizeof( ashort ));
1577 data += sizeof( ashort );
1581 ashort = htons( AFPVOLSIG_DEFAULT );
1582 memcpy(data, &ashort, sizeof( ashort ));
1583 data += sizeof( ashort );
1586 case VOLPBIT_CDATE :
1587 aint = vol->v_ctime;
1588 memcpy(data, &aint, sizeof( aint ));
1589 data += sizeof( aint );
1592 case VOLPBIT_MDATE :
1593 if ( st->st_mtime > vol->v_mtime ) {
1594 vol->v_mtime = st->st_mtime;
1596 aint = AD_DATE_FROM_UNIX(vol->v_mtime);
1597 memcpy(data, &aint, sizeof( aint ));
1598 data += sizeof( aint );
1601 case VOLPBIT_BDATE :
1602 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1603 aint = AD_DATE_START;
1604 memcpy(data, &aint, sizeof( aint ));
1605 data += sizeof( aint );
1609 memcpy(data, &vol->v_vid, sizeof( vol->v_vid ));
1610 data += sizeof( vol->v_vid );
1613 case VOLPBIT_BFREE :
1614 bfree = htonl( bfree );
1615 memcpy(data, &bfree, sizeof( bfree ));
1616 data += sizeof( bfree );
1619 case VOLPBIT_BTOTAL :
1620 btotal = htonl( btotal );
1621 memcpy(data, &btotal, sizeof( btotal ));
1622 data += sizeof( btotal );
1625 #ifndef NO_LARGE_VOL_SUPPORT
1626 case VOLPBIT_XBFREE :
1627 xbfree = hton64( xbfree );
1628 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1629 bcopy(&xbfree, data, sizeof(xbfree));
1630 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1631 memcpy(data, &xbfree, sizeof( xbfree ));
1632 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1633 data += sizeof( xbfree );
1636 case VOLPBIT_XBTOTAL :
1637 xbtotal = hton64( xbtotal );
1638 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1639 bcopy(&xbtotal, data, sizeof(xbtotal));
1640 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1641 memcpy(data, &xbtotal, sizeof( xbtotal ));
1642 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1643 data += sizeof( xbfree );
1645 #endif /* ! NO_LARGE_VOL_SUPPORT */
1649 data += sizeof( u_int16_t );
1652 case VOLPBIT_BSIZE: /* block size */
1653 bsize = htonl(bsize);
1654 memcpy(data, &bsize, sizeof(bsize));
1655 data += sizeof(bsize);
1660 ad_close( &ad, ADFLAGS_HF );
1662 return( AFPERR_BITMAP );
1668 ashort = htons( data - buf );
1669 memcpy(nameoff, &ashort, sizeof( ashort ));
1670 /* name is always in mac charset */
1671 aint = ucs2_to_charset( vol->v_maccharset, vol->v_macname, data+1, AFPVOL_MACNAMELEN + 1);
1681 ad_close_metadata( &ad);
1683 *buflen = data - buf;
1687 /* ------------------------- */
1688 static int stat_vol(u_int16_t bitmap, struct vol *vol, char *rbuf, size_t *rbuflen)
1694 if ( stat( vol->v_path, &st ) < 0 ) {
1696 return( AFPERR_PARAM );
1698 /* save the volume device number */
1699 vol->v_dev = st.st_dev;
1701 buflen = *rbuflen - sizeof( bitmap );
1702 if (( ret = getvolparams( bitmap, vol, &st,
1703 rbuf + sizeof( bitmap ), &buflen )) != AFP_OK ) {
1707 *rbuflen = buflen + sizeof( bitmap );
1708 bitmap = htons( bitmap );
1709 memcpy(rbuf, &bitmap, sizeof( bitmap ));
1714 /* ------------------------------- */
1715 void load_volumes(AFPObj *obj)
1717 struct passwd *pwent;
1722 /* check files date */
1723 if (obj->options.defaultvol.loaded) {
1724 changed = volfile_changed(&obj->options.defaultvol);
1726 if (obj->options.systemvol.loaded) {
1727 changed |= volfile_changed(&obj->options.systemvol);
1729 if (obj->options.uservol.loaded) {
1730 changed |= volfile_changed(&obj->options.uservol);
1739 if (! ((DSI *)obj->handle)->child) {
1740 LOG(log_debug, logtype_afpd, "load_volumes: AFP MASTER");
1742 LOG(log_debug, logtype_afpd, "load_volumes: user: %s", obj->username);
1745 pwent = getpwnam(obj->username);
1746 if ( (obj->options.flags & OPTION_USERVOLFIRST) == 0 ) {
1747 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent);
1750 if ((*obj->username == '\0') || (obj->options.flags & OPTION_NOUSERVOL)) {
1751 readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent);
1754 * Read user's AppleVolumes or .AppleVolumes file
1755 * If neither are readable, read the default volumes file. if
1756 * that doesn't work, create a user share.
1758 if (obj->options.uservol.name) {
1759 free(obj->options.uservol.name);
1761 obj->options.uservol.name = strdup(pwent->pw_dir);
1762 if ( readvolfile(obj, &obj->options.uservol, "AppleVolumes", 1, pwent) < 0 &&
1763 readvolfile(obj, &obj->options.uservol, ".AppleVolumes", 1, pwent) < 0 &&
1764 readvolfile(obj, &obj->options.uservol, "applevolumes", 1, pwent) < 0 &&
1765 readvolfile(obj, &obj->options.uservol, ".applevolumes", 1, pwent) < 0 &&
1766 obj->options.defaultvol.name != NULL ) {
1767 if (readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent) < 0)
1768 creatvol(obj, pwent, pwent->pw_dir, NULL, NULL, 1);
1771 if ( obj->options.flags & OPTION_USERVOLFIRST ) {
1772 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent );
1775 if ( obj->options.closevol ) {
1778 for ( vol = Volumes; vol; vol = vol->v_next ) {
1779 if (vol->v_deleted && !vol->v_new ) {
1788 /* ------------------------------- */
1789 int afp_getsrvrparms(AFPObj *obj, char *ibuf _U_, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
1802 for ( vcnt = 0, volume = Volumes; volume; volume = volume->v_next ) {
1803 if (!(volume->v_flags & AFPVOL_NOSTAT)) {
1806 if ( stat( volume->v_path, &st ) < 0 ) {
1807 LOG(log_info, logtype_afpd, "afp_getsrvrparms(%s): stat: %s",
1808 volume->v_path, strerror(errno) );
1809 continue; /* can't access directory */
1811 if (!S_ISDIR(st.st_mode)) {
1812 continue; /* not a dir */
1814 accessmode(volume->v_path, &ma, NULL, &st);
1815 if ((ma.ma_user & (AR_UREAD | AR_USEARCH)) != (AR_UREAD | AR_USEARCH)) {
1816 continue; /* no r-x access */
1819 if (volume->v_hide) {
1820 continue; /* config file changed but the volume was mounted */
1823 if (utf8_encoding()) {
1824 len = ucs2_to_charset_allocate(CH_UTF8_MAC, &namebuf, volume->v_u8mname);
1826 len = ucs2_to_charset_allocate(obj->options.maccharset, &namebuf, volume->v_macname);
1829 if (len == (size_t)-1)
1832 /* set password bit if there's a volume password */
1833 *data = (volume->v_password) ? AFPSRVR_PASSWD : 0;
1835 /* Apple 2 clients running ProDOS-8 expect one volume to have
1836 bit 0 of this byte set. They will not recognize anything
1837 on the server unless this is the case. I have not
1838 completely worked this out, but it's related to booting
1839 from the server. Support for that function is a ways
1840 off.. <shirsch@ibm.net> */
1841 *data |= (volume->v_flags & AFPVOL_A2VOL) ? AFPSRVR_CONFIGINFO : 0;
1842 *data++ |= 0; /* UNIX PRIVS BIT ..., OSX doesn't seem to use it, so we don't either */
1844 memcpy(data, namebuf, len );
1850 *rbuflen = data - rbuf;
1852 if ( gettimeofday( &tv, NULL ) < 0 ) {
1853 LOG(log_error, logtype_afpd, "afp_getsrvrparms(%s): gettimeofday: %s", volume->v_path, strerror(errno) );
1855 return AFPERR_PARAM;
1857 tv.tv_sec = AD_DATE_FROM_UNIX(tv.tv_sec);
1858 memcpy(data, &tv.tv_sec, sizeof( u_int32_t));
1859 data += sizeof( u_int32_t);
1864 /* ------------------------- */
1865 static int volume_codepage(AFPObj *obj, struct vol *volume)
1867 struct charset_functions *charset;
1870 if (!volume->v_volcodepage)
1871 volume->v_volcodepage = strdup("UTF8");
1873 if ( (charset_t) -1 == ( volume->v_volcharset = add_charset(volume->v_volcodepage)) ) {
1874 LOG (log_error, logtype_afpd, "Setting codepage %s as volume codepage failed", volume->v_volcodepage);
1878 if ( NULL == (charset = find_charset_functions(volume->v_volcodepage)) || charset->flags & CHARSET_ICONV ) {
1879 LOG (log_warning, logtype_afpd, "WARNING: volume encoding %s is *not* supported by netatalk, expect problems !!!!", volume->v_volcodepage);
1882 if (!volume->v_maccodepage)
1883 volume->v_maccodepage = strdup(obj->options.maccodepage);
1885 if ( (charset_t) -1 == ( volume->v_maccharset = add_charset(volume->v_maccodepage)) ) {
1886 LOG (log_error, logtype_afpd, "Setting codepage %s as mac codepage failed", volume->v_maccodepage);
1890 if ( NULL == ( charset = find_charset_functions(volume->v_maccodepage)) || ! (charset->flags & CHARSET_CLIENT) ) {
1891 LOG (log_error, logtype_afpd, "Fatal error: mac charset %s not supported", volume->v_maccodepage);
1894 volume->v_kTextEncoding = htonl(charset->kTextEncoding);
1898 /* ------------------------- */
1899 static int volume_openDB(struct vol *volume)
1903 if ((volume->v_flags & AFPVOL_NODEV)) {
1904 flags |= CNID_FLAG_NODEV;
1907 if (volume->v_cnidscheme == NULL) {
1908 volume->v_cnidscheme = strdup(DEFAULT_CNID_SCHEME);
1909 LOG(log_info, logtype_afpd, "Volume %s use CNID scheme %s.",
1910 volume->v_path, volume->v_cnidscheme);
1913 LOG(log_info, logtype_afpd, "CNID server: %s:%s",
1914 volume->v_cnidserver ? volume->v_cnidserver : Cnid_srv,
1915 volume->v_cnidport ? volume->v_cnidport : Cnid_port);
1918 /* Found this in branch dir-rewrite, maybe we want to use it sometimes */
1920 /* Legacy pre 2.1 way of sharing eg CD-ROM */
1921 if (strcmp(volume->v_cnidscheme, "last") == 0) {
1922 /* "last" is gone. We support it by switching to in-memory "tdb" */
1923 volume->v_cnidscheme = strdup("tdb");
1924 flags |= CNID_FLAG_MEMORY;
1927 /* New way of sharing CD-ROM */
1928 if (volume->v_flags & AFPVOL_CDROM) {
1929 flags |= CNID_FLAG_MEMORY;
1930 if (strcmp(volume->v_cnidscheme, "tdb") != 0) {
1931 free(volume->v_cnidscheme);
1932 volume->v_cnidscheme = strdup("tdb");
1933 LOG(log_info, logtype_afpd, "Volume %s is ejectable, switching to scheme %s.",
1934 volume->v_path, volume->v_cnidscheme);
1939 volume->v_cdb = cnid_open(volume->v_dbpath ? volume->v_dbpath : volume->v_path,
1941 volume->v_cnidscheme,
1943 volume->v_cnidserver ? volume->v_cnidserver : Cnid_srv,
1944 volume->v_cnidport ? volume->v_cnidport : Cnid_port);
1946 if ( ! volume->v_cdb && ! (flags & CNID_FLAG_MEMORY)) {
1947 /* The first attempt failed and it wasn't yet an attempt to open in-memory */
1948 LOG(log_error, logtype_afpd, "Can't open volume \"%s\" CNID backend \"%s\" ",
1949 volume->v_path, volume->v_cnidscheme);
1950 LOG(log_error, logtype_afpd, "Reopen volume %s using in memory temporary CNID DB.",
1952 flags |= CNID_FLAG_MEMORY;
1953 volume->v_cdb = cnid_open (volume->v_path, volume->v_umask, "tdb", flags, NULL, NULL);
1955 /* kill ourself with SIGUSR2 aka msg pending */
1956 if (volume->v_cdb) {
1957 setmessage("Something wrong with the volume's CNID DB, using temporary CNID DB instead."
1958 "Check server messages for details!");
1959 kill(getpid(), SIGUSR2);
1960 /* deactivate cnid caching/storing in AppleDouble files */
1961 volume->v_flags &= ~AFPVOL_CACHE;
1966 return (!volume->v_cdb)?-1:0;
1970 Check if the underlying filesystem supports EAs for ea:sys volumes.
1971 If not, switch to ea:ad.
1972 As we can't check (requires write access) on ro-volumes, we switch ea:auto
1973 volumes that are options:ro to ea:none.
1975 static void check_ea_sys_support(struct vol *vol)
1977 uid_t process_uid = 0;
1978 char eaname[] = {"org.netatalk.supports-eas.XXXXXX"};
1979 const char *eacontent = "yes";
1981 if (vol->v_vfs_ea == AFPVOL_EA_AUTO) {
1983 if ((vol->v_flags & AFPVOL_RO) == AFPVOL_RO) {
1984 LOG(log_info, logtype_logger, "read-only volume '%s', can't test for EA support, disabling EAs", vol->v_localname);
1985 vol->v_vfs_ea = AFPVOL_EA_NONE;
1991 process_uid = geteuid();
1993 if (seteuid(0) == -1) {
1994 LOG(log_error, logtype_logger, "check_ea_sys_support: can't seteuid(0): %s", strerror(errno));
1998 if ((sys_setxattr(vol->v_path, eaname, eacontent, 4, 0)) == 0) {
1999 sys_removexattr(vol->v_path, eaname);
2000 vol->v_vfs_ea = AFPVOL_EA_SYS;
2002 LOG(log_warning, logtype_afpd, "volume \"%s\" does not support Extended Attributes, using ea:ad instead",
2004 vol->v_vfs_ea = AFPVOL_EA_AD;
2008 if (seteuid(process_uid) == -1) {
2009 LOG(log_error, logtype_logger, "can't seteuid back %s", strerror(errno));
2016 /* -------------------------
2017 * we are the user here
2019 int afp_openvol(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2030 char path[ MAXPATHLEN + 1];
2036 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2037 bitmap = ntohs( bitmap );
2038 ibuf += sizeof( bitmap );
2041 if (( bitmap & (1<<VOLPBIT_VID)) == 0 ) {
2042 return AFPERR_BITMAP;
2045 len = (unsigned char)*ibuf++;
2046 volname = obj->oldtmp;
2048 if ((volname_tmp = strchr(volname,'+')) != NULL)
2049 volname = volname_tmp+1;
2051 if (utf8_encoding()) {
2052 namelen = convert_string(CH_UTF8_MAC, CH_UCS2, ibuf, len, volname, sizeof(obj->oldtmp));
2054 namelen = convert_string(obj->options.maccharset, CH_UCS2, ibuf, len, volname, sizeof(obj->oldtmp));
2057 if ( namelen <= 0) {
2058 return AFPERR_PARAM;
2062 if ((len + 1) & 1) /* pad to an even boundary */
2067 for ( volume = Volumes; volume; volume = volume->v_next ) {
2068 if ( strcasecmp_w( (ucs2_t*) volname, volume->v_name ) == 0 ) {
2073 if ( volume == NULL ) {
2074 return AFPERR_PARAM;
2077 /* check for a volume password */
2078 if (volume->v_password && strncmp(ibuf, volume->v_password, VOLPASSLEN)) {
2079 return AFPERR_ACCESS;
2082 if (( volume->v_flags & AFPVOL_OPEN ) ) {
2083 /* the volume is already open */
2085 set_uidgid ( volume );
2087 return stat_vol(bitmap, volume, rbuf, rbuflen);
2090 if (volume->v_root_preexec) {
2091 if ((ret = afprun(1, volume->v_root_preexec, NULL)) && volume->v_root_preexec_close) {
2092 LOG(log_error, logtype_afpd, "afp_openvol(%s): root preexec : %d", volume->v_path, ret );
2098 set_uidgid ( volume );
2101 if (volume->v_preexec) {
2102 if ((ret = afprun(0, volume->v_preexec, NULL)) && volume->v_preexec_close) {
2103 LOG(log_error, logtype_afpd, "afp_openvol(%s): preexec : %d", volume->v_path, ret );
2108 if ( stat( volume->v_path, &st ) < 0 ) {
2109 return AFPERR_PARAM;
2112 if ( chdir( volume->v_path ) < 0 ) {
2113 return AFPERR_PARAM;
2116 if ( NULL == getcwd(path, MAXPATHLEN)) {
2117 /* shouldn't be fatal but it will fail later */
2118 LOG(log_error, logtype_afpd, "afp_openvol(%s): volume pathlen too long", volume->v_path);
2122 /* Normalize volume path */
2123 #ifdef REALPATH_TAKES_NULL
2124 if ((volume->v_path = realpath(path, NULL)) == NULL)
2127 if ((volume->v_path = malloc(MAXPATHLEN+1)) == NULL)
2129 if (realpath(path, volume->v_path) == NULL) {
2130 free(volume->v_path);
2133 /* Safe some memory */
2135 if ((tmp = strdup(volume->v_path)) == NULL) {
2136 free(volume->v_path);
2139 free(volume->v_path);
2140 volume->v_path = tmp;
2143 if (volume_codepage(obj, volume) < 0) {
2148 /* initialize volume variables
2151 if (utf8_encoding()) {
2152 volume->max_filename = 255;
2155 volume->max_filename = MACFILELEN;
2158 volume->v_flags |= AFPVOL_OPEN;
2159 volume->v_cdb = NULL;
2161 if (utf8_encoding()) {
2162 len = convert_string_allocate(CH_UCS2, CH_UTF8_MAC, volume->v_u8mname, namelen, &vol_mname);
2164 len = convert_string_allocate(CH_UCS2, obj->options.maccharset, volume->v_macname, namelen, &vol_mname);
2166 if ( !vol_mname || len <= 0) {
2171 if ((vol_uname = strrchr(path, '/')) == NULL)
2173 else if (*(vol_uname + 1) != '\0')
2176 if ((dir = dir_new(vol_mname,
2181 bfromcstr(volume->v_path),
2185 LOG(log_error, logtype_afpd, "afp_openvol(%s): malloc: %s", volume->v_path, strerror(errno) );
2190 volume->v_root = dir;
2193 if (volume_openDB(volume) < 0) {
2194 LOG(log_error, logtype_afpd, "Fatal error: cannot open CNID or invalid CNID backend for %s: %s",
2195 volume->v_path, volume->v_cnidscheme);
2200 ret = stat_vol(bitmap, volume, rbuf, rbuflen);
2201 if (ret == AFP_OK) {
2203 if (!(volume->v_flags & AFPVOL_RO)) {
2204 handle_special_folders( volume );
2206 volume->v_cnidserver ? volume->v_cnidserver : Cnid_srv,
2207 volume->v_cnidport ? volume->v_cnidport : Cnid_port);
2211 * If you mount a volume twice, the second time the trash appears on
2212 * the desk-top. That's because the Mac remembers the DID for the
2213 * trash (even for volumes in different zones, on different servers).
2214 * Just so this works better, we prime the DID cache with the trash,
2215 * fixing the trash at DID 17.
2216 * FIXME (RL): should it be done inside a CNID backend ? (always returning Trash DID when asked) ?
2218 if ((volume->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
2220 /* FIXME find db time stamp */
2221 if (cnid_getstamp(volume->v_cdb, volume->v_stamp, sizeof(volume->v_stamp)) < 0) {
2222 LOG (log_error, logtype_afpd,
2223 "afp_openvol(%s): Fatal error: Unable to get stamp value from CNID backend",
2231 cname( volume, volume->v_root, &p );
2237 if (volume->v_root) {
2238 dir_free( volume->v_root );
2239 volume->v_root = NULL;
2242 volume->v_flags &= ~AFPVOL_OPEN;
2243 if (volume->v_cdb != NULL) {
2244 cnid_close(volume->v_cdb);
2245 volume->v_cdb = NULL;
2251 /* ------------------------- */
2252 static void closevol(struct vol *vol)
2257 dir_free( vol->v_root );
2259 if (vol->v_cdb != NULL) {
2260 cnid_close(vol->v_cdb);
2264 if (vol->v_postexec) {
2265 afprun(0, vol->v_postexec, NULL);
2267 if (vol->v_root_postexec) {
2268 afprun(1, vol->v_root_postexec, NULL);
2272 /* ------------------------- */
2273 void close_all_vol(void)
2277 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
2278 if ( (ovol->v_flags & AFPVOL_OPEN) ) {
2279 ovol->v_flags &= ~AFPVOL_OPEN;
2285 /* ------------------------- */
2286 static void deletevol(struct vol *vol)
2290 vol->v_flags &= ~AFPVOL_OPEN;
2291 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
2292 if ( (ovol->v_flags & AFPVOL_OPEN) ) {
2296 if ( ovol != NULL ) {
2297 /* Even if chdir fails, we can't say afp_closevol fails. */
2298 if ( chdir( ovol->v_path ) == 0 ) {
2299 curdir = ovol->v_root;
2304 if (vol->v_deleted) {
2305 showvol(vol->v_name);
2312 /* ------------------------- */
2313 int afp_closevol(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2320 memcpy(&vid, ibuf, sizeof( vid ));
2321 if (NULL == ( vol = getvolbyvid( vid )) ) {
2322 return( AFPERR_PARAM );
2331 /* ------------------------- */
2332 struct vol *getvolbyvid(const u_int16_t vid )
2336 for ( vol = Volumes; vol; vol = vol->v_next ) {
2337 if ( vid == vol->v_vid ) {
2341 if ( vol == NULL || ( vol->v_flags & AFPVOL_OPEN ) == 0 ) {
2347 #endif /* FORCE_UIDGID */
2354 /* ------------------------ */
2355 static int ext_cmp_key(const void *key, const void *obj)
2357 const char *p = key;
2358 const struct extmap *em = obj;
2359 return strdiacasecmp(p, em->em_ext);
2361 struct extmap *getextmap(const char *path)
2366 if (!Extmap_cnt || NULL == ( p = strrchr( path, '.' )) ) {
2367 return( Defextmap );
2371 return( Defextmap );
2373 em = bsearch(p, Extmap, Extmap_cnt, sizeof(struct extmap), ext_cmp_key);
2377 return( Defextmap );
2381 /* ------------------------- */
2382 struct extmap *getdefextmap(void)
2384 return( Defextmap );
2387 /* --------------------------
2388 poll if a volume is changed by other processes.
2390 0 no attention msg sent
2391 1 attention msg sent
2392 -1 error (socket closed)
2394 Note: if attention return -1 no packet has been
2395 sent because the buffer is full, we don't care
2396 either there's no reader or there's a lot of
2397 traffic and another pollvoltime will follow
2399 int pollvoltime(AFPObj *obj)
2406 if (!(afp_version > 21 && obj->options.server_notif))
2409 if ( gettimeofday( &tv, NULL ) < 0 )
2412 for ( vol = Volumes; vol; vol = vol->v_next ) {
2413 if ( (vol->v_flags & AFPVOL_OPEN) && vol->v_mtime + 30 < tv.tv_sec) {
2414 if ( !stat( vol->v_path, &st ) && vol->v_mtime != st.st_mtime ) {
2415 vol->v_mtime = st.st_mtime;
2416 if (!obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED))
2425 /* ------------------------- */
2426 void setvoltime(AFPObj *obj, struct vol *vol)
2430 /* just looking at vol->v_mtime is broken seriously since updates
2431 * from other users afpd processes never are seen.
2432 * This is not the most elegant solution (a shared memory between
2433 * the afpd processes would come closer)
2436 if ( gettimeofday( &tv, NULL ) < 0 ) {
2437 LOG(log_error, logtype_afpd, "setvoltime(%s): gettimeofday: %s", vol->v_path, strerror(errno) );
2440 if( utime( vol->v_path, NULL ) < 0 ) {
2441 /* write of time failed ... probably a read only filesys,
2442 * where no other users can interfere, so there's no issue here
2446 /* a little granularity */
2447 if (vol->v_mtime < tv.tv_sec) {
2448 vol->v_mtime = tv.tv_sec;
2449 /* or finder doesn't update free space
2450 * AFP 3.2 and above clients seem to be ok without so many notification
2452 if (afp_version < 32 && obj->options.server_notif) {
2453 obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED);
2458 /* ------------------------- */
2459 int afp_getvolparams(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_,char *rbuf, size_t *rbuflen)
2462 u_int16_t vid, bitmap;
2465 memcpy(&vid, ibuf, sizeof( vid ));
2466 ibuf += sizeof( vid );
2467 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2468 bitmap = ntohs( bitmap );
2470 if (NULL == ( vol = getvolbyvid( vid )) ) {
2472 return( AFPERR_PARAM );
2475 return stat_vol(bitmap, vol, rbuf, rbuflen);
2478 /* ------------------------- */
2479 int afp_setvolparams(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2483 u_int16_t vid, bitmap;
2489 memcpy(&vid, ibuf, sizeof( vid ));
2490 ibuf += sizeof( vid );
2491 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2492 bitmap = ntohs( bitmap );
2493 ibuf += sizeof(bitmap);
2495 if (( vol = getvolbyvid( vid )) == NULL ) {
2496 return( AFPERR_PARAM );
2499 if ((vol->v_flags & AFPVOL_RO))
2500 return AFPERR_VLOCK;
2502 /* we can only set the backup date. */
2503 if (bitmap != (1 << VOLPBIT_BDATE))
2504 return AFPERR_BITMAP;
2506 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2507 if ( ad_open( vol->v_path, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR,
2510 return AFPERR_VLOCK;
2512 return AFPERR_ACCESS;
2515 memcpy(&aint, ibuf, sizeof(aint));
2516 ad_setdate(&ad, AD_DATE_BACKUP, aint);
2518 ad_close(&ad, ADFLAGS_HF);
2522 /* ------------------------- */
2523 int wincheck(const struct vol *vol, const char *path)
2527 if (!(vol->v_flags & AFPVOL_MSWINDOWS))
2530 /* empty paths are not allowed */
2531 if ((len = strlen(path)) == 0)
2534 /* leading or trailing whitespaces are not allowed, carriage returns
2535 * and probably other whitespace is okay, tabs are not allowed
2537 if ((path[0] == ' ') || (path[len-1] == ' '))
2540 /* certain characters are not allowed */
2541 if (strpbrk(path, MSWINDOWS_BADCHARS))
2544 /* everything else is okay */
2550 * precreate a folder
2551 * this is only intended for folders in the volume root
2552 * It will *not* work if the folder name contains extended characters
2554 static int create_special_folder (const struct vol *vol, const struct _special_folder *folder)
2563 p = (char *) malloc ( strlen(vol->v_path)+strlen(folder->name)+2);
2565 LOG(log_error, logtype_afpd,"malloc failed");
2569 q=strdup(folder->name);
2571 LOG(log_error, logtype_afpd,"malloc failed");
2575 strcpy(p, vol->v_path);
2580 if ((vol->v_casefold & AFPVOL_MTOUUPPER))
2582 else if ((vol->v_casefold & AFPVOL_MTOULOWER))
2588 if ( (ret = stat( p, &st )) < 0 ) {
2589 if (folder->precreate) {
2590 if (ad_mkdir(p, folder->mode)) {
2591 LOG(log_debug, logtype_afpd,"Creating '%s' failed in %s: %s", p, vol->v_path, strerror(errno));
2600 if ( !ret && folder->hide) {
2602 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2603 if (ad_open_metadata( p, ADFLAGS_DIR, O_CREAT, &ad) < 0) {
2609 ad_setname(&ad, folder->name);
2611 ad_getattr(&ad, &attr);
2612 attr |= htons( ntohs( attr ) | ATTRBIT_INVISIBLE );
2613 ad_setattr(&ad, attr);
2615 /* do the same with the finder info */
2616 if (ad_entry(&ad, ADEID_FINDERI)) {
2617 memcpy(&attr, ad_entry(&ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF, sizeof(attr));
2618 attr |= htons(FINDERINFO_INVISIBLE);
2619 memcpy(ad_entry(&ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF,&attr, sizeof(attr));
2623 ad_close_metadata( &ad);
2630 static void handle_special_folders (const struct vol * vol)
2632 const _special_folder *p = &special_folders[0];
2634 if ((vol->v_flags & AFPVOL_RO))
2637 for (; p->name != NULL; p++) {
2638 create_special_folder (vol, p);
2642 const struct vol *getvolumes(void)
2647 void unload_volumes_and_extmap(void)
2649 LOG(log_debug, logtype_afpd, "unload_volumes_and_extmap");
2655 * Get a volumes UUID from the config file.
2656 * If there is none, it is generated and stored there.
2658 * Returns pointer to allocated storage on success, NULL on error.
2660 char *get_uuid(const AFPObj *obj, const char *volname)
2663 char buf[1024], uuid[UUID_PRINTABLE_STRING_LENGTH], *p;
2665 struct stat tmpstat;
2668 if ((fp = fopen(obj->options.uuidconf, "r")) != NULL) { /* read open? */
2669 /* scan in the conf file */
2670 while (fgets(buf, sizeof(buf), fp) != NULL) {
2672 while (p && isblank(*p))
2674 if (!p || (*p == '#') || (*p == '\n'))
2675 continue; /* invalid line */
2678 if ((volname_conf = strtok( p, "\"" )) == NULL)
2679 continue; /* syntax error */
2681 if ((volname_conf = strtok( p, " \t" )) == NULL)
2682 continue; /* syntax error: invalid name */
2684 p = strchr(p, '\0');
2687 continue; /* syntax error */
2689 if (strcmp(volname, volname_conf) != 0)
2690 continue; /* another volume name */
2692 while (p && isblank(*p))
2695 if (sscanf(p, "%36s", uuid) == 1 ) {
2696 for (int i=0; uuid[i]; i++)
2697 uuid[i] = toupper(uuid[i]);
2698 LOG(log_debug, logtype_afpd, "get_uuid('%s'): UUID: '%s'", volname, uuid);
2700 return strdup(uuid);
2708 /* not found or no file, reopen in append mode */
2710 if (stat(obj->options.uuidconf, &tmpstat)) { /* no file */
2711 if (( fd = creat(obj->options.uuidconf, 0644 )) < 0 ) {
2712 LOG(log_error, logtype_atalkd, "ERROR: Cannot create %s (%s).",
2713 obj->options.uuidconf, strerror(errno));
2716 if (( fp = fdopen( fd, "w" )) == NULL ) {
2717 LOG(log_error, logtype_atalkd, "ERROR: Cannot fdopen %s (%s).",
2718 obj->options.uuidconf, strerror(errno));
2722 } else if ((fp = fopen(obj->options.uuidconf, "a+")) == NULL) { /* not found */
2723 LOG(log_error, logtype_afpd, "Cannot create or append to %s (%s).",
2724 obj->options.uuidconf, strerror(errno));
2727 fseek(fp, 0L, SEEK_END);
2728 if(ftell(fp) == 0) { /* size = 0 */
2729 fprintf(fp, "# DON'T TOUCH NOR COPY THOUGHTLESSLY!\n");
2730 fprintf(fp, "# This file is auto-generated by afpd\n");
2731 fprintf(fp, "# and stores UUIDs for TM volumes.\n\n");
2733 fseek(fp, -1L, SEEK_END);
2734 if(fgetc(fp) != '\n') fputc('\n', fp); /* last char is \n? */
2737 /* generate uuid and write to file */
2740 randombytes((void *)id, 16);
2741 cp = uuid_bin2string(id);
2743 LOG(log_debug, logtype_afpd, "get_uuid('%s'): generated UUID '%s'", volname, cp);
2745 fprintf(fp, "\"%s\"\t%36s\n", volname, cp);
2748 return strdup(uuid);