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>
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>
49 #include <atalk/uuid.h>
50 #include <atalk/bstrlib.h>
51 #include <atalk/bstradd.h>
52 #include <atalk/ftw.h>
53 #include <atalk/globals.h>
56 #include <atalk/cnid.h>
59 #include "directory.h"
68 extern int afprun(int root, char *cmd, int *outfd);
71 #define MIN(a, b) ((a) < (b) ? (a) : (b))
74 #ifndef NO_LARGE_VOL_SUPPORT
75 #if BYTE_ORDER == BIG_ENDIAN
78 #else /* BYTE_ORDER == BIG_ENDIAN */
79 #define hton64(x) ((u_int64_t) (htonl(((x) >> 32) & 0xffffffffLL)) | \
80 (u_int64_t) ((htonl(x) & 0xffffffffLL) << 32))
81 #define ntoh64(x) (hton64(x))
82 #endif /* BYTE_ORDER == BIG_ENDIAN */
83 #endif /* ! NO_LARGE_VOL_SUPPORT */
85 #ifndef UUID_PRINTABLE_STRING_LENGTH
86 #define UUID_PRINTABLE_STRING_LENGTH 37
90 struct vol *current_vol; /* last volume from getvolbyvid() */
92 static struct vol *Volumes = NULL;
93 static u_int16_t lastvid = 0;
94 static char *Trash = "\02\024Network Trash Folder";
96 static struct extmap *Extmap = NULL, *Defextmap = NULL;
97 static int Extmap_cnt;
98 static void free_extmap(void);
100 #define VOLOPT_ALLOW 0 /* user allow list */
101 #define VOLOPT_DENY 1 /* user deny list */
102 #define VOLOPT_RWLIST 2 /* user rw list */
103 #define VOLOPT_ROLIST 3 /* user ro list */
104 #define VOLOPT_PASSWORD 4 /* volume password */
105 #define VOLOPT_CASEFOLD 5 /* character case mangling */
106 #define VOLOPT_FLAGS 6 /* various flags */
107 #define VOLOPT_DBPATH 7 /* path to database */
108 #define VOLOPT_LIMITSIZE 8 /* Limit the size of the volume */
110 #define VOLOPT_VETO 10 /* list of veto filespec */
111 #define VOLOPT_PREEXEC 11 /* preexec command */
112 #define VOLOPT_ROOTPREEXEC 12 /* root preexec command */
113 #define VOLOPT_POSTEXEC 13 /* postexec command */
114 #define VOLOPT_ROOTPOSTEXEC 14 /* root postexec command */
115 #define VOLOPT_ENCODING 15 /* mac encoding (pre OSX)*/
116 #define VOLOPT_MACCHARSET 16
117 #define VOLOPT_CNIDSCHEME 17
118 #define VOLOPT_ADOUBLE 18 /* adouble version */
124 #define VOLOPT_FORCEUID 19 /* force uid for username x */
125 #define VOLOPT_FORCEGID 20 /* force gid for group x */
126 #endif /* FORCE_UIDGID */
128 #define VOLOPT_UMASK 21
129 #define VOLOPT_ALLOWED_HOSTS 22
130 #define VOLOPT_DENIED_HOSTS 23
131 #define VOLOPT_DPERM 24 /* dperm default directories perms */
132 #define VOLOPT_FPERM 25 /* fperm default files perms */
133 #define VOLOPT_DFLTPERM 26 /* perm */
134 #define VOLOPT_EA_VFS 27 /* Extended Attributes vfs indirection */
135 #define VOLOPT_CNIDSERVER 28 /* CNID Server ip address*/
136 #define VOLOPT_CNIDPORT 30 /* CNID server tcp port */
138 #define VOLOPT_MAX 31 /* <== IMPORTANT !!!!!! */
139 #define VOLOPT_NUM (VOLOPT_MAX + 1)
142 #define VOLOPT_DEFAULT ":DEFAULT:"
143 #define VOLOPT_DEFAULT_LEN 9
150 typedef struct _special_folder {
157 static const _special_folder special_folders[] = {
158 {"Network Trash Folder", 1, 0777, 1},
159 {"Temporary Items", 1, 0777, 1},
160 {".AppleDesktop", 1, 0777, 0},
162 {"TheFindByContentFolder", 0, 0, 1},
163 {"TheVolumeSettingsFolder", 0, 0, 1},
167 /* Forward declarations */
168 static void handle_special_folders (const struct vol *);
169 static void deletevol(struct vol *vol);
170 static void volume_free(struct vol *vol);
171 static void check_ea_sys_support(struct vol *vol);
172 static char *get_vol_uuid(const AFPObj *obj, const char *volname);
173 static int readvolfile(AFPObj *obj, struct afp_volume_name *p1,char *p2, int user, struct passwd *pwent);
175 static void volfree(struct vol_option *options, const struct vol_option *save)
180 for (i = 0; i < VOLOPT_MAX; i++) {
181 if (options[i].c_value && (options[i].c_value != save[i].c_value))
182 free(options[i].c_value);
185 for (i = 0; i < VOLOPT_MAX; i++) {
186 if (options[i].c_value)
187 free(options[i].c_value);
193 #define is_var(a, b) (strncmp((a), (b), 2) == 0)
196 * Handle variable substitutions. here's what we understand:
197 * $b -> basename of path
198 * $c -> client ip/appletalk address
199 * $d -> volume pathname on server
200 * $f -> full name (whatever's in the gecos field)
203 * $i -> client ip/appletalk address without port
204 * $s -> server name (hostname if it doesn't exist)
205 * $u -> username (guest is usually nobody)
206 * $v -> volume name or basename if null
207 * $z -> zone (may not exist)
210 * This get's called from readvolfile with
211 * path = NULL, volname = NULL for xlating the volumes path
212 * path = path, volname = NULL for xlating the volumes name
213 * ... and from volumes options parsing code when xlating eg dbpath with
214 * path = path, volname = volname
216 * Using this information we can reject xlation of any variable depeninding on a login
217 * context which is not given in the afp master, where we must evaluate this whole stuff
218 * too for the Zeroconf announcements.
220 static char *volxlate(AFPObj *obj,
233 int xlatevolname = 0;
235 if (parent_or_child == 0)
238 if (path && !volname)
246 dest = calloc(destlen +1, 1);
252 strlcpy(dest, src, destlen +1);
253 if ((p = strchr(src, '$')) == NULL) /* nothing to do */
256 /* first part of the path. just forward to the next variable. */
257 len = MIN((size_t)(p - src), destlen);
263 while (p && destlen > 0) {
264 /* now figure out what the variable is */
266 if (is_var(p, "$b")) {
267 if (afpmaster && xlatevolname)
270 if ((q = strrchr(path, '/')) == NULL)
272 else if (*(q + 1) != '\0')
275 } else if (is_var(p, "$c")) {
276 if (afpmaster && xlatevolname)
278 if (obj->proto == AFPPROTO_ASP) {
279 ASP asp = obj->handle;
281 len = sprintf(dest, "%u.%u", ntohs(asp->asp_sat.sat_addr.s_net),
282 asp->asp_sat.sat_addr.s_node);
286 } else if (obj->proto == AFPPROTO_DSI) {
287 DSI *dsi = obj->handle;
288 len = sprintf(dest, "%s:%u",
289 getip_string((struct sockaddr *)&dsi->client),
290 getip_port((struct sockaddr *)&dsi->client));
294 } else if (is_var(p, "$d")) {
295 if (afpmaster && xlatevolname)
298 } else if (pwd && is_var(p, "$f")) {
299 if (afpmaster && xlatevolname)
301 if ((r = strchr(pwd->pw_gecos, ',')))
304 } else if (pwd && is_var(p, "$g")) {
305 if (afpmaster && xlatevolname)
307 struct group *grp = getgrgid(pwd->pw_gid);
310 } else if (is_var(p, "$h")) {
311 q = obj->options.hostname;
312 } else if (is_var(p, "$i")) {
313 if (afpmaster && xlatevolname)
315 if (obj->proto == AFPPROTO_ASP) {
316 ASP asp = obj->handle;
318 len = sprintf(dest, "%u", ntohs(asp->asp_sat.sat_addr.s_net));
322 } else if (obj->proto == AFPPROTO_DSI) {
323 DSI *dsi = obj->handle;
324 q = getip_string((struct sockaddr *)&dsi->client);
326 } else if (is_var(p, "$s")) {
329 else if (obj->options.server) {
330 q = obj->options.server;
332 q = obj->options.hostname;
333 } else if (obj->username && is_var(p, "$u")) {
334 if (afpmaster && xlatevolname)
337 if ( obj->options.ntseparator && (sep = strchr(obj->username, obj->options.ntseparator[0])) != NULL)
341 } else if (is_var(p, "$v")) {
342 if (afpmaster && xlatevolname)
348 if ((q = strrchr(path, '/')) == NULL)
350 else if (*(q + 1) != '\0')
353 } else if (is_var(p, "$z")) {
355 } else if (is_var(p, "$$")) {
360 /* copy the stuff over. if we don't understand something that we
361 * should, just skip it over. */
363 len = MIN(p == q ? 2 : strlen(q), destlen);
364 strncpy(dest, q, len);
369 /* stuff up to next $ */
371 p = strchr(src, '$');
372 len = p ? MIN((size_t)(p - src), destlen) : destlen;
374 strncpy(dest, src, len);
382 /* to make sure that val is valid, make sure to select an opt that
384 static int optionok(const char *buf, const char *opt, const char *val)
386 if (!strstr(buf,opt))
394 /* -------------------- */
395 static void setoption(struct vol_option *options, struct vol_option *save, int opt, const char *val)
397 if (options[opt].c_value && (!save || options[opt].c_value != save[opt].c_value))
398 free(options[opt].c_value);
399 options[opt].c_value = strdup(val + 1);
402 /* ------------------------------------------
403 handle all the options. tmp can't be NULL. */
404 static void volset(struct vol_option *options, struct vol_option *save,
405 char *volname, int vlen,
410 val = strchr(tmp, ':');
412 /* we'll assume it's a volume name. */
413 strncpy(volname, tmp, vlen);
418 LOG(log_debug, logtype_afpd, "Parsing volset %s", val);
420 if (optionok(tmp, "allow:", val)) {
421 setoption(options, save, VOLOPT_ALLOW, val);
423 } else if (optionok(tmp, "deny:", val)) {
424 setoption(options, save, VOLOPT_DENY, val);
426 } else if (optionok(tmp, "rwlist:", val)) {
427 setoption(options, save, VOLOPT_RWLIST, val);
429 } else if (optionok(tmp, "rolist:", val)) {
430 setoption(options, save, VOLOPT_ROLIST, val);
432 } else if (optionok(tmp, "codepage:", val)) {
433 LOG (log_error, logtype_afpd, "The old codepage system has been removed. Please make sure to read the documentation !!!!");
434 /* Make sure we don't screw anything */
436 } else if (optionok(tmp, "volcharset:", val)) {
437 setoption(options, save, VOLOPT_ENCODING, val);
438 } else if (optionok(tmp, "maccharset:", val)) {
439 setoption(options, save, VOLOPT_MACCHARSET, val);
440 } else if (optionok(tmp, "veto:", val)) {
441 setoption(options, save, VOLOPT_VETO, val);
442 } else if (optionok(tmp, "cnidscheme:", val)) {
443 setoption(options, save, VOLOPT_CNIDSCHEME, val);
444 } else if (optionok(tmp, "casefold:", val)) {
445 if (strcasecmp(val + 1, "tolower") == 0)
446 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMLOWER;
447 else if (strcasecmp(val + 1, "toupper") == 0)
448 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMUPPER;
449 else if (strcasecmp(val + 1, "xlatelower") == 0)
450 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UUPPERMLOWER;
451 else if (strcasecmp(val + 1, "xlateupper") == 0)
452 options[VOLOPT_CASEFOLD].i_value = AFPVOL_ULOWERMUPPER;
453 } else if (optionok(tmp, "adouble:", val)) {
454 if (strcasecmp(val + 1, "v1") == 0)
455 options[VOLOPT_ADOUBLE].i_value = AD_VERSION1;
456 #if AD_VERSION == AD_VERSION2
457 else if (strcasecmp(val + 1, "v2") == 0)
458 options[VOLOPT_ADOUBLE].i_value = AD_VERSION2;
459 else if (strcasecmp(val + 1, "osx") == 0)
460 options[VOLOPT_ADOUBLE].i_value = AD_VERSION2_OSX;
461 else if (strcasecmp(val + 1, "sfm") == 0)
462 options[VOLOPT_ADOUBLE].i_value = AD_VERSION1_SFM;
464 } else if (optionok(tmp, "options:", val)) {
467 if ((p = strtok(val + 1, ",")) == NULL) /* nothing */
471 if (strcasecmp(p, "prodos") == 0)
472 options[VOLOPT_FLAGS].i_value |= AFPVOL_A2VOL;
473 else if (strcasecmp(p, "mswindows") == 0) {
474 options[VOLOPT_FLAGS].i_value |= AFPVOL_MSWINDOWS | AFPVOL_USEDOTS;
475 } else if (strcasecmp(p, "crlf") == 0)
476 options[VOLOPT_FLAGS].i_value |= AFPVOL_CRLF;
477 else if (strcasecmp(p, "noadouble") == 0)
478 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOADOUBLE;
479 else if (strcasecmp(p, "ro") == 0)
480 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
481 else if (strcasecmp(p, "nohex") == 0)
482 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOHEX;
483 else if (strcasecmp(p, "usedots") == 0)
484 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS;
485 else if (strcasecmp(p, "invisibledots") == 0)
486 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS | AFPVOL_INV_DOTS;
487 else if (strcasecmp(p, "limitsize") == 0)
488 options[VOLOPT_FLAGS].i_value |= AFPVOL_LIMITSIZE;
489 /* support for either "dropbox" or "dropkludge" */
490 else if (strcasecmp(p, "dropbox") == 0)
491 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
492 else if (strcasecmp(p, "dropkludge") == 0)
493 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
494 else if (strcasecmp(p, "nofileid") == 0)
495 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOFILEID;
496 else if (strcasecmp(p, "nostat") == 0)
497 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOSTAT;
498 else if (strcasecmp(p, "preexec_close") == 0)
499 options[VOLOPT_PREEXEC].i_value = 1;
500 else if (strcasecmp(p, "root_preexec_close") == 0)
501 options[VOLOPT_ROOTPREEXEC].i_value = 1;
502 else if (strcasecmp(p, "upriv") == 0)
503 options[VOLOPT_FLAGS].i_value |= AFPVOL_UNIX_PRIV;
504 else if (strcasecmp(p, "nodev") == 0)
505 options[VOLOPT_FLAGS].i_value |= AFPVOL_NODEV;
506 else if (strcasecmp(p, "caseinsensitive") == 0)
507 options[VOLOPT_FLAGS].i_value |= AFPVOL_CASEINSEN;
508 else if (strcasecmp(p, "illegalseq") == 0)
509 options[VOLOPT_FLAGS].i_value |= AFPVOL_EILSEQ;
510 else if (strcasecmp(p, "nocnidcache") == 0)
511 options[VOLOPT_FLAGS].i_value &= ~AFPVOL_CACHE;
512 else if (strcasecmp(p, "tm") == 0)
513 options[VOLOPT_FLAGS].i_value |= AFPVOL_TM;
514 else if (strcasecmp(p, "searchdb") == 0)
515 options[VOLOPT_FLAGS].i_value |= AFPVOL_SEARCHDB;
516 else if (strcasecmp(p, "nonetids") == 0)
517 options[VOLOPT_FLAGS].i_value |= AFPVOL_NONETIDS;
518 p = strtok(NULL, ",");
521 } else if (optionok(tmp, "cnidserver:", val)) {
522 setoption(options, save, VOLOPT_CNIDSERVER, val);
524 char *p = strrchr(val + 1, ':');
527 setoption(options, save, VOLOPT_CNIDPORT, p);
530 LOG(log_debug, logtype_afpd, "CNID Server for volume '%s': %s:%s",
531 volname, val + 1, p ? p + 1 : Cnid_port);
533 } else if (optionok(tmp, "dbpath:", val)) {
534 setoption(options, save, VOLOPT_DBPATH, val);
536 } else if (optionok(tmp, "umask:", val)) {
537 options[VOLOPT_UMASK].i_value = (int)strtol(val +1, NULL, 8);
538 } else if (optionok(tmp, "dperm:", val)) {
539 options[VOLOPT_DPERM].i_value = (int)strtol(val+1, NULL, 8);
540 } else if (optionok(tmp, "fperm:", val)) {
541 options[VOLOPT_FPERM].i_value = (int)strtol(val+1, NULL, 8);
542 } else if (optionok(tmp, "perm:", val)) {
543 options[VOLOPT_DFLTPERM].i_value = (int)strtol(val+1, NULL, 8);
544 } else if (optionok(tmp, "password:", val)) {
545 setoption(options, save, VOLOPT_PASSWORD, val);
549 /* this code allows forced uid/gid per volume settings */
550 } else if (optionok(tmp, "forceuid:", val)) {
551 setoption(options, save, VOLOPT_FORCEUID, val);
552 } else if (optionok(tmp, "forcegid:", val)) {
553 setoption(options, save, VOLOPT_FORCEGID, val);
555 #endif /* FORCE_UIDGID */
556 } else if (optionok(tmp, "root_preexec:", val)) {
557 setoption(options, save, VOLOPT_ROOTPREEXEC, val);
559 } else if (optionok(tmp, "preexec:", val)) {
560 setoption(options, save, VOLOPT_PREEXEC, val);
562 } else if (optionok(tmp, "root_postexec:", val)) {
563 setoption(options, save, VOLOPT_ROOTPOSTEXEC, val);
565 } else if (optionok(tmp, "postexec:", val)) {
566 setoption(options, save, VOLOPT_POSTEXEC, val);
568 } else if (optionok(tmp, "allowed_hosts:", val)) {
569 setoption(options, save, VOLOPT_ALLOWED_HOSTS, val);
571 } else if (optionok(tmp, "denied_hosts:", val)) {
572 setoption(options, save, VOLOPT_DENIED_HOSTS, val);
574 } else if (optionok(tmp, "ea:", val)) {
575 if (strcasecmp(val + 1, "ad") == 0)
576 options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_AD;
577 else if (strcasecmp(val + 1, "sys") == 0)
578 options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_SYS;
579 else if (strcasecmp(val + 1, "none") == 0)
580 options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_NONE;
582 } else if (optionok(tmp, "volsizelimit:", val)) {
583 options[VOLOPT_LIMITSIZE].i_value = (uint32_t)strtoul(val + 1, NULL, 10);
586 /* ignore unknown options */
587 LOG(log_debug, logtype_afpd, "ignoring unknown volume option: %s", tmp);
592 /* ----------------- */
593 static void showvol(const ucs2_t *name)
596 for ( volume = Volumes; volume; volume = volume->v_next ) {
597 if (volume->v_hide && !strcasecmp_w( volume->v_name, name ) ) {
604 /* ------------------------------- */
605 static int creatvol(AFPObj *obj, struct passwd *pwd,
606 char *path, char *name,
607 struct vol_option *options,
608 const int user /* user defined volume */
612 int suffixlen, vlen, tmpvlen, u8mvlen, macvlen;
614 char tmpname[AFPVOL_U8MNAMELEN+1];
615 ucs2_t u8mtmpname[(AFPVOL_U8MNAMELEN+1)*2], mactmpname[(AFPVOL_MACNAMELEN+1)*2];
616 char suffix[6]; /* max is #FFFF */
619 LOG(log_debug, logtype_afpd, "createvol: Volume '%s'", name);
621 if ( name == NULL || *name == '\0' ) {
622 if ((name = strrchr( path, '/' )) == NULL) {
623 return -1; /* Obviously not a fully qualified path */
626 /* if you wish to share /, you need to specify a name. */
631 /* suffix for mangling use (lastvid + 1) */
632 /* because v_vid has not been decided yet. */
633 suffixlen = sprintf(suffix, "%c%X", MANGLE_CHAR, lastvid + 1 );
635 vlen = strlen( name );
637 /* Unicode Volume Name */
638 /* Firstly convert name from unixcharset to UTF8-MAC */
640 tmpvlen = convert_charset(obj->options.unixcharset, CH_UTF8_MAC, 0, name, vlen, tmpname, AFPVOL_U8MNAMELEN, &flags);
642 strcpy(tmpname, "???");
646 /* Do we have to mangle ? */
647 if ( (flags & CONV_REQMANGLE) || (tmpvlen > obj->options.volnamelen)) {
648 if (tmpvlen + suffixlen > obj->options.volnamelen) {
650 tmpvlen = convert_charset(obj->options.unixcharset, CH_UTF8_MAC, 0, name, vlen, tmpname, obj->options.volnamelen - suffixlen, &flags);
651 tmpname[tmpvlen >= 0 ? tmpvlen : 0] = 0;
653 strcat(tmpname, suffix);
654 tmpvlen = strlen(tmpname);
657 /* Secondly convert name from UTF8-MAC to UCS2 */
658 if ( 0 >= ( u8mvlen = convert_string(CH_UTF8_MAC, CH_UCS2, tmpname, tmpvlen, u8mtmpname, AFPVOL_U8MNAMELEN*2)) )
661 LOG(log_maxdebug, logtype_afpd, "createvol: Volume '%s' -> UTF8-MAC Name: '%s'", name, tmpname);
663 /* Maccharset Volume Name */
664 /* Firsty convert name from unixcharset to maccharset */
666 tmpvlen = convert_charset(obj->options.unixcharset, obj->options.maccharset, 0, name, vlen, tmpname, AFPVOL_U8MNAMELEN, &flags);
668 strcpy(tmpname, "???");
672 /* Do we have to mangle ? */
673 if ( (flags & CONV_REQMANGLE) || (tmpvlen > AFPVOL_MACNAMELEN)) {
674 if (tmpvlen + suffixlen > AFPVOL_MACNAMELEN) {
676 tmpvlen = convert_charset(obj->options.unixcharset,
677 obj->options.maccharset,
682 AFPVOL_MACNAMELEN - suffixlen,
684 tmpname[tmpvlen >= 0 ? tmpvlen : 0] = 0;
686 strcat(tmpname, suffix);
687 tmpvlen = strlen(tmpname);
690 /* Secondly convert name from maccharset to UCS2 */
691 if ( 0 >= ( macvlen = convert_string(obj->options.maccharset,
696 AFPVOL_U8MNAMELEN*2)) )
699 LOG(log_maxdebug, logtype_afpd, "createvol: Volume '%s' -> Longname: '%s'", name, tmpname);
701 /* check duplicate */
702 for ( volume = Volumes; volume; volume = volume->v_next ) {
703 if ((utf8_encoding() && (strcasecmp_w(volume->v_u8mname, u8mtmpname) == 0))
705 (!utf8_encoding() && (strcasecmp_w(volume->v_macname, mactmpname) == 0))) {
706 LOG (log_error, logtype_afpd,
707 "Duplicate volume name, check AppleVolumes files: previous: \"%s\", new: \"%s\"",
708 volume->v_localname, name);
709 if (volume->v_deleted) {
710 volume->v_new = hide = 1;
713 return -1; /* Won't be able to access it, anyway... */
718 if (!( volume = (struct vol *)calloc(1, sizeof( struct vol ))) ) {
719 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
722 if ( NULL == ( volume->v_localname = strdup(name))) {
723 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
728 if ( NULL == ( volume->v_u8mname = strdup_w(u8mtmpname))) {
729 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
734 if ( NULL == ( volume->v_macname = strdup_w(mactmpname))) {
735 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
740 if (!( volume->v_path = (char *)malloc( strlen( path ) + 1 )) ) {
741 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
747 volume->v_name = utf8_encoding()?volume->v_u8mname:volume->v_macname;
748 volume->v_hide = hide;
749 strcpy( volume->v_path, path );
753 #endif /* __svr4__ */
754 /* os X start at 1 and use network order ie. 1 2 3 */
755 volume->v_vid = ++lastvid;
756 volume->v_vid = htons(volume->v_vid);
758 if (check_vol_acl_support(volume))
759 volume->v_flags |= AFPVOL_ACLS
765 volume->v_casefold = options[VOLOPT_CASEFOLD].i_value;
766 volume->v_flags |= options[VOLOPT_FLAGS].i_value;
768 if (options[VOLOPT_EA_VFS].i_value)
769 volume->v_vfs_ea = options[VOLOPT_EA_VFS].i_value;
771 volume->v_ad_options = 0;
772 if ((volume->v_flags & AFPVOL_NODEV))
773 volume->v_ad_options |= ADVOL_NODEV;
774 if ((volume->v_flags & AFPVOL_CACHE))
775 volume->v_ad_options |= ADVOL_CACHE;
776 if ((volume->v_flags & AFPVOL_UNIX_PRIV))
777 volume->v_ad_options |= ADVOL_UNIXPRIV;
778 if ((volume->v_flags & AFPVOL_INV_DOTS))
779 volume->v_ad_options |= ADVOL_INVDOTS;
780 if ((volume->v_flags & AFPVOL_NOADOUBLE))
781 volume->v_ad_options |= ADVOL_NOADOUBLE;
783 if (options[VOLOPT_PASSWORD].c_value)
784 volume->v_password = strdup(options[VOLOPT_PASSWORD].c_value);
786 if (options[VOLOPT_VETO].c_value)
787 volume->v_veto = strdup(options[VOLOPT_VETO].c_value);
789 if (options[VOLOPT_ENCODING].c_value)
790 volume->v_volcodepage = strdup(options[VOLOPT_ENCODING].c_value);
792 if (options[VOLOPT_MACCHARSET].c_value)
793 volume->v_maccodepage = strdup(options[VOLOPT_MACCHARSET].c_value);
795 if (options[VOLOPT_DBPATH].c_value)
796 volume->v_dbpath = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_DBPATH].c_value, pwd, path, name);
798 if (options[VOLOPT_CNIDSCHEME].c_value)
799 volume->v_cnidscheme = strdup(options[VOLOPT_CNIDSCHEME].c_value);
801 if (options[VOLOPT_CNIDSERVER].c_value)
802 volume->v_cnidserver = strdup(options[VOLOPT_CNIDSERVER].c_value);
804 if (options[VOLOPT_CNIDPORT].c_value)
805 volume->v_cnidport = strdup(options[VOLOPT_CNIDPORT].c_value);
807 if (options[VOLOPT_UMASK].i_value)
808 volume->v_umask = (mode_t)options[VOLOPT_UMASK].i_value;
810 if (options[VOLOPT_DPERM].i_value)
811 volume->v_dperm = (mode_t)options[VOLOPT_DPERM].i_value;
813 if (options[VOLOPT_FPERM].i_value)
814 volume->v_fperm = (mode_t)options[VOLOPT_FPERM].i_value;
816 if (options[VOLOPT_DFLTPERM].i_value)
817 volume->v_perm = (mode_t)options[VOLOPT_DFLTPERM].i_value;
819 if (options[VOLOPT_ADOUBLE].i_value)
820 volume->v_adouble = options[VOLOPT_ADOUBLE].i_value;
822 volume->v_adouble = AD_VERSION;
824 if (options[VOLOPT_LIMITSIZE].i_value)
825 volume->v_limitsize = options[VOLOPT_LIMITSIZE].i_value;
827 /* Mac to Unix conversion flags*/
828 volume->v_mtou_flags = 0;
829 if (!(volume->v_flags & AFPVOL_NOHEX))
830 volume->v_mtou_flags |= CONV_ESCAPEHEX;
831 if (!(volume->v_flags & AFPVOL_USEDOTS))
832 volume->v_mtou_flags |= CONV_ESCAPEDOTS;
833 if ((volume->v_flags & AFPVOL_EILSEQ))
834 volume->v_mtou_flags |= CONV__EILSEQ;
836 if ((volume->v_casefold & AFPVOL_MTOUUPPER))
837 volume->v_mtou_flags |= CONV_TOUPPER;
838 else if ((volume->v_casefold & AFPVOL_MTOULOWER))
839 volume->v_mtou_flags |= CONV_TOLOWER;
841 /* Unix to Mac conversion flags*/
842 volume->v_utom_flags = CONV_IGNORE | CONV_UNESCAPEHEX;
843 if ((volume->v_casefold & AFPVOL_UTOMUPPER))
844 volume->v_utom_flags |= CONV_TOUPPER;
845 else if ((volume->v_casefold & AFPVOL_UTOMLOWER))
846 volume->v_utom_flags |= CONV_TOLOWER;
848 if ((volume->v_flags & AFPVOL_EILSEQ))
849 volume->v_utom_flags |= CONV__EILSEQ;
852 if (options[VOLOPT_FORCEUID].c_value) {
853 volume->v_forceuid = strdup(options[VOLOPT_FORCEUID].c_value);
855 volume->v_forceuid = NULL; /* set as null so as to return 0 later on */
858 if (options[VOLOPT_FORCEGID].c_value) {
859 volume->v_forcegid = strdup(options[VOLOPT_FORCEGID].c_value);
861 volume->v_forcegid = NULL; /* set as null so as to return 0 later on */
865 if (options[VOLOPT_PREEXEC].c_value)
866 volume->v_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_PREEXEC].c_value, pwd, path, name);
867 volume->v_preexec_close = options[VOLOPT_PREEXEC].i_value;
869 if (options[VOLOPT_POSTEXEC].c_value)
870 volume->v_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_POSTEXEC].c_value, pwd, path, name);
872 if (options[VOLOPT_ROOTPREEXEC].c_value)
873 volume->v_root_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPREEXEC].c_value, pwd, path, name);
874 volume->v_root_preexec_close = options[VOLOPT_ROOTPREEXEC].i_value;
876 if (options[VOLOPT_ROOTPOSTEXEC].c_value)
877 volume->v_root_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPOSTEXEC].c_value, pwd, path, name);
880 volume->v_dperm |= volume->v_perm;
881 volume->v_fperm |= volume->v_perm;
883 /* Check EA support on volume */
884 if (volume->v_vfs_ea == AFPVOL_EA_AUTO)
885 check_ea_sys_support(volume);
888 /* get/store uuid from file in afpd master*/
889 if ((parent_or_child == 0) && (volume->v_flags & AFPVOL_TM)) {
890 char *uuid = get_vol_uuid(obj, volume->v_localname);
892 LOG(log_error, logtype_afpd, "Volume '%s': couldn't get UUID",
893 volume->v_localname);
895 volume->v_uuid = uuid;
896 LOG(log_debug, logtype_afpd, "Volume '%s': UUID '%s'",
897 volume->v_localname, volume->v_uuid);
901 volume->v_next = Volumes;
906 /* ---------------- */
907 static char *myfgets( char *buf, int size, FILE *fp)
913 while ((EOF != ( c = getc( fp )) ) && ( size > 1 )) {
914 if ( c == '\n' || c == '\r' ) {
915 if (p != buf && *(p -1) == '\\') {
937 /* check access list. this function wants something of the following
939 * @group,name,name2,@group2,name3
941 * a NULL argument allows everybody to have access.
942 * we return three things:
944 * 0: list exists, but name isn't in it
948 #ifndef NO_REAL_USER_NAME
949 /* authentication is case insensitive
950 * FIXME should we do the same with group name?
952 #define access_strcmp strcasecmp
955 #define access_strcmp strcmp
959 static int accessvol(const char *args, const char *name)
961 char buf[MAXPATHLEN + 1], *p;
967 strlcpy(buf, args, sizeof(buf));
968 if ((p = strtok(buf, ",")) == NULL) /* nothing, return okay */
972 if (*p == '@') { /* it's a group */
973 if ((gr = getgrnam(p + 1)) && gmem(gr->gr_gid))
975 } else if (access_strcmp(p, name) == 0) /* it's a user name */
977 p = strtok(NULL, ",");
983 static int hostaccessvol(int type, const char *volname, const char *args, const AFPObj *obj)
986 char buf[MAXPATHLEN + 1], *p, *b;
987 DSI *dsi = obj->handle;
988 struct sockaddr_storage client;
993 strlcpy(buf, args, sizeof(buf));
994 if ((p = strtok_r(buf, ",", &b)) == NULL) /* nothing, return okay */
997 if (obj->proto != AFPPROTO_DSI)
1002 char *ipaddr, *mask_char;
1003 struct addrinfo hints, *ai;
1005 ipaddr = strtok(p, "/");
1006 mask_char = strtok(NULL,"/");
1008 /* Get address from string with getaddrinfo */
1009 memset(&hints, 0, sizeof hints);
1010 hints.ai_family = AF_UNSPEC;
1011 hints.ai_socktype = SOCK_STREAM;
1012 if ((ret = getaddrinfo(ipaddr, NULL, &hints, &ai)) != 0) {
1013 LOG(log_error, logtype_afpd, "hostaccessvol: getaddrinfo: %s\n", gai_strerror(ret));
1018 if (mask_char != NULL)
1019 mask_int = atoi(mask_char); /* apply_ip_mask does range checking on it */
1021 if (ai->ai_family == AF_INET) /* IPv4 */
1027 /* Apply mask to addresses */
1028 client = dsi->client;
1029 apply_ip_mask((struct sockaddr *)&client, mask_int);
1030 apply_ip_mask(ai->ai_addr, mask_int);
1032 if (compare_ip((struct sockaddr *)&client, ai->ai_addr) == 0) {
1033 if (type == VOLOPT_DENIED_HOSTS)
1034 LOG(log_info, logtype_afpd, "AFP access denied for client IP '%s' to volume '%s' by denied list",
1035 getip_string((struct sockaddr *)&client), volname);
1042 p = strtok_r(NULL, ",", &b);
1045 if (type == VOLOPT_ALLOWED_HOSTS)
1046 LOG(log_info, logtype_afpd, "AFP access denied for client IP '%s' to volume '%s', not in allowed list",
1047 getip_string((struct sockaddr *)&dsi->client), volname);
1051 static void setextmap(char *ext, char *type, char *creator, int user)
1056 if (Extmap == NULL) {
1057 if (( Extmap = calloc(1, sizeof( struct extmap ))) == NULL ) {
1058 LOG(log_error, logtype_afpd, "setextmap: calloc: %s", strerror(errno) );
1063 for ( em = Extmap, cnt = 0; em->em_ext; em++, cnt++) {
1064 if ( (strdiacasecmp( em->em_ext, ext )) == 0 ) {
1069 if ( em->em_ext == NULL ) {
1070 if (!(Extmap = realloc( Extmap, sizeof( struct extmap ) * (cnt +2))) ) {
1071 LOG(log_error, logtype_afpd, "setextmap: realloc: %s", strerror(errno) );
1074 (Extmap +cnt +1)->em_ext = NULL;
1076 } else if ( !user ) {
1082 if (!(em->em_ext = strdup( ext))) {
1083 LOG(log_error, logtype_afpd, "setextmap: strdup: %s", strerror(errno) );
1087 if ( *type == '\0' ) {
1088 memcpy(em->em_type, "\0\0\0\0", sizeof( em->em_type ));
1090 memcpy(em->em_type, type, sizeof( em->em_type ));
1092 if ( *creator == '\0' ) {
1093 memcpy(em->em_creator, "\0\0\0\0", sizeof( em->em_creator ));
1095 memcpy(em->em_creator, creator, sizeof( em->em_creator ));
1099 /* -------------------------- */
1100 static int extmap_cmp(const void *map1, const void *map2)
1102 const struct extmap *em1 = map1;
1103 const struct extmap *em2 = map2;
1104 return strdiacasecmp(em1->em_ext, em2->em_ext);
1107 static void sortextmap( void)
1112 if ((em = Extmap) == NULL) {
1115 while (em->em_ext) {
1120 qsort(Extmap, Extmap_cnt, sizeof(struct extmap), extmap_cmp);
1121 if (*Extmap->em_ext == 0) {
1122 /* the first line is really "." the default entry,
1123 * we remove the leading '.' in setextmap
1130 /* ----------------------
1132 static void free_extmap( void)
1137 for ( em = Extmap; em->em_ext; em++) {
1147 /* ----------------------
1149 static int volfile_changed(struct afp_volume_name *p)
1155 name = p->full_name;
1159 if (!stat( name, &st) && st.st_mtime > p->mtime) {
1160 p->mtime = st.st_mtime;
1166 /* ----------------------
1167 * Read a volume configuration file and add the volumes contained within to
1168 * the global volume list. This gets called from the forked afpd childs.
1169 * The master now reads this too for Zeroconf announcements.
1171 * If p2 is non-NULL, the file that is opened is
1174 * Lines that begin with # and blank lines are ignored.
1175 * Volume lines are of the form:
1176 * <unix path> [<volume name>] [allow:<user>,<@group>,...] \
1177 * [codepage:<file>] [casefold:<num>]
1178 * <extension> TYPE [CREATOR]
1181 static int readvolfile(AFPObj *obj, struct afp_volume_name *p1, char *p2, int user, struct passwd *pwent)
1184 char path[MAXPATHLEN + 1];
1185 char tmp[MAXPATHLEN + 1];
1186 char volname[AFPVOL_U8MNAMELEN + 1];
1188 char type[5], creator[5];
1193 struct vol_option save_options[VOLOPT_NUM];
1194 struct vol_option options[VOLOPT_NUM];
1200 strcpy( path, p1->name );
1202 strcat( path, "/" );
1204 if (p1->full_name) {
1205 free(p1->full_name);
1207 p1->full_name = strdup(path);
1210 if (NULL == ( fp = fopen( path, "r" )) ) {
1214 if (fd != -1 && !fstat( fd, &st) ) {
1215 p1->mtime = st.st_mtime;
1218 /* try putting a read lock on the volume file twice, sleep 1 second if first attempt fails */
1221 if ((read_lock(fd, 0, SEEK_SET, 0)) != 0) {
1224 LOG(log_error, logtype_afpd, "readvolfile: can't lock volume file \"%s\"", path);
1225 if ( fclose( fp ) != 0 ) {
1226 LOG(log_error, logtype_afpd, "readvolfile: fclose: %s", strerror(errno) );
1236 memset(save_options, 0, sizeof(save_options));
1238 /* Enable some default options for all volumes */
1239 save_options[VOLOPT_FLAGS].i_value |= AFPVOL_CACHE;
1240 save_options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_AUTO;
1241 LOG(log_maxdebug, logtype_afpd, "readvolfile: seeding default umask: %04o",
1242 obj->options.umask);
1243 save_options[VOLOPT_UMASK].i_value = obj->options.umask;
1245 LOG(log_debug, logtype_afpd, "readvolfile: \"%s\"", path);
1247 while ( myfgets( buf, sizeof( buf ), fp ) != NULL ) {
1248 initline( strlen( buf ), buf );
1249 parseline( sizeof( path ) - 1, path );
1256 /* change the default options for this file */
1257 if (strncmp(path, VOLOPT_DEFAULT, VOLOPT_DEFAULT_LEN) == 0) {
1259 for (i = 0; i < VOLOPT_NUM; i++) {
1260 if (parseline( sizeof( path ) - VOLOPT_DEFAULT_LEN - 1,
1261 path + VOLOPT_DEFAULT_LEN) < 0)
1263 volset(save_options, NULL, tmp, sizeof(tmp) - 1,
1264 path + VOLOPT_DEFAULT_LEN);
1270 if (( p = strchr( path, '/' )) != NULL ) {
1278 if ( u == NULL || *u == '\0' || ( pw = getpwnam( u )) == NULL ) {
1281 strcpy( tmp, pw->pw_dir );
1282 if ( p != NULL && *p != '\0' ) {
1289 /* send path through variable substitution */
1290 if (*path != '~') /* need to copy path to tmp */
1292 if (!pwent && obj->username)
1293 pwent = getpwnam(obj->username);
1295 if (volxlate(obj, path, sizeof(path) - 1, tmp, pwent, NULL, NULL) == NULL)
1298 /* this is sort of braindead. basically, i want to be
1299 * able to specify things in any order, but i don't want to
1300 * re-write everything. */
1302 memcpy(options, save_options, sizeof(options));
1305 /* read in up to VOLOP_NUM possible options */
1306 for (i = 0; i < VOLOPT_NUM; i++) {
1307 if (parseline( sizeof( tmp ) - 1, tmp ) < 0)
1310 volset(options, save_options, volname, sizeof(volname) - 1, tmp);
1313 /* check allow/deny lists (if not afpd master loading volumes for Zeroconf reg.):
1314 allow -> either no list (-1), or in list (1)
1315 deny -> either no list (-1), or not in list (0) */
1316 if (parent_or_child == 0
1318 (accessvol(options[VOLOPT_ALLOW].c_value, obj->username) &&
1319 (accessvol(options[VOLOPT_DENY].c_value, obj->username) < 1) &&
1320 hostaccessvol(VOLOPT_ALLOWED_HOSTS, volname, options[VOLOPT_ALLOWED_HOSTS].c_value, obj) &&
1321 (hostaccessvol(VOLOPT_DENIED_HOSTS, volname, options[VOLOPT_DENIED_HOSTS].c_value, obj) < 1))) {
1323 /* handle read-only behaviour. semantics:
1324 * 1) neither the rolist nor the rwlist exist -> rw
1325 * 2) rolist exists -> ro if user is in it.
1326 * 3) rwlist exists -> ro unless user is in it. */
1327 if (parent_or_child == 1
1329 ((options[VOLOPT_FLAGS].i_value & AFPVOL_RO) == 0)
1331 ((accessvol(options[VOLOPT_ROLIST].c_value, obj->username) == 1) ||
1332 !accessvol(options[VOLOPT_RWLIST].c_value, obj->username)))
1333 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
1335 /* do variable substitution for volname */
1336 if (volxlate(obj, tmp, sizeof(tmp) - 1, volname, pwent, path, NULL) == NULL)
1339 creatvol(obj, pwent, path, tmp, options, p2 != NULL);
1341 volfree(options, save_options);
1345 parseline( sizeof( type ) - 1, type );
1346 parseline( sizeof( creator ) - 1, creator );
1347 setextmap( path, type, creator, user);
1354 volfree(save_options, NULL);
1356 if ( fclose( fp ) != 0 ) {
1357 LOG(log_error, logtype_afpd, "readvolfile: fclose: %s", strerror(errno) );
1363 /* ------------------------------- */
1364 static void volume_free(struct vol *vol)
1366 free(vol->v_localname);
1367 vol->v_localname = NULL;
1368 free(vol->v_u8mname);
1369 vol->v_u8mname = NULL;
1370 free(vol->v_macname);
1371 vol->v_macname = NULL;
1373 free(vol->v_password);
1375 free(vol->v_volcodepage);
1376 free(vol->v_maccodepage);
1377 free(vol->v_cnidscheme);
1378 free(vol->v_dbpath);
1381 free(vol->v_forceuid);
1382 free(vol->v_forcegid);
1383 #endif /* FORCE_UIDGID */
1388 /* ------------------------------- */
1389 static void free_volumes(void )
1392 struct vol *nvol, *ovol;
1394 for ( vol = Volumes; vol; vol = vol->v_next ) {
1395 if (( vol->v_flags & AFPVOL_OPEN ) ) {
1402 for ( vol = Volumes, ovol = NULL; vol; vol = nvol) {
1405 if (vol->v_localname == NULL) {
1406 if (Volumes == vol) {
1411 ovol->v_next = nvol;
1421 /* ------------------------------- */
1422 static void volume_unlink(struct vol *volume)
1424 struct vol *vol, *ovol, *nvol;
1426 if (volume == Volumes) {
1427 Volumes = Volumes->v_next;
1430 for ( vol = Volumes->v_next, ovol = Volumes; vol; vol = nvol) {
1433 if (vol == volume) {
1434 ovol->v_next = nvol;
1443 static off_t getused_size; /* result of getused() */
1446 nftw callback for getused()
1448 static int getused_stat(const char *path,
1449 const struct stat *statp,
1455 if (tflag == FTW_F || tflag == FTW_D) {
1456 getused_size += statp->st_blocks * 512;
1462 #define GETUSED_CACHETIME 5
1464 * Calculate used size of a volume with nftw
1466 * The result is cached, we're try to avoid frequently calling nftw()
1468 * 1) Call nftw an refresh if:
1469 * 1a) - we're called the first time
1470 * 1b) - volume modification date is not yet set and the last time we've been called is
1471 * longer then 30 sec ago
1472 * 1c) - the last volume modification is less then 30 sec old
1474 * @param vol (r) volume to calculate
1476 static int getused(const struct vol *vol)
1478 static time_t vol_mtime = 0;
1480 time_t now = time(NULL);
1483 || (!vol->v_mtime && ((vol_mtime + GETUSED_CACHETIME) < now))
1484 || (vol->v_mtime && ((vol_mtime + GETUSED_CACHETIME) < vol->v_mtime))
1488 ret = nftw(vol->v_path, getused_stat, NULL, 20, FTW_PHYS); /* 2 */
1489 LOG(log_debug, logtype_afpd, "volparams: from nftw: %" PRIu64 " bytes",
1492 LOG(log_debug, logtype_afpd, "volparams: cached used: %" PRIu64 " bytes",
1499 static int getvolspace(struct vol *vol,
1500 u_int32_t *bfree, u_int32_t *btotal,
1501 VolSpace *xbfree, VolSpace *xbtotal, u_int32_t *bsize)
1506 #ifndef NO_QUOTA_SUPPORT
1507 VolSpace qfree, qtotal;
1510 spaceflag = AFPVOL_GVSMASK & vol->v_flags;
1511 /* report up to 2GB if afp version is < 2.2 (4GB if not) */
1512 maxsize = (vol->v_flags & AFPVOL_A2VOL) ? 0x01fffe00 :
1513 (((afp_version < 22) || (vol->v_flags & AFPVOL_LIMITSIZE))
1514 ? 0x7fffffffL : 0xffffffffL);
1517 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_AFSGVS ) {
1518 if ( afs_getvolspace( vol, xbfree, xbtotal, bsize ) == AFP_OK ) {
1519 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_AFSGVS;
1520 goto getvolspace_done;
1525 if (( rc = ustatfs_getvolspace( vol, xbfree, xbtotal, bsize)) != AFP_OK ) {
1529 #define min(a,b) ((a)<(b)?(a):(b))
1530 #ifndef NO_QUOTA_SUPPORT
1531 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_UQUOTA ) {
1532 if ( uquota_getvolspace( vol, &qfree, &qtotal, *bsize ) == AFP_OK ) {
1533 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_UQUOTA;
1534 *xbfree = min(*xbfree, qfree);
1535 *xbtotal = min( *xbtotal, qtotal);
1536 goto getvolspace_done;
1540 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_USTATFS;
1543 if (vol->v_limitsize) {
1544 if (getused(vol) != 0)
1546 LOG(log_debug, logtype_afpd, "volparams: used on volume: %" PRIu64 " bytes",
1549 *xbtotal = min(*xbtotal, (vol->v_limitsize * 1024 * 1024));
1550 *xbfree = min(*xbfree, *xbtotal < getused_size ? 0 : *xbtotal - getused_size);
1553 *bfree = min( *xbfree, maxsize);
1554 *btotal = min( *xbtotal, maxsize);
1558 /* -----------------------
1559 * set volume creation date
1560 * avoid duplicate, well at least it tries
1562 static void vol_setdate(u_int16_t id, struct adouble *adp, time_t date)
1565 struct vol *vol = Volumes;
1567 for ( volume = Volumes; volume; volume = volume->v_next ) {
1568 if (volume->v_vid == id) {
1571 else if ((time_t)(AD_DATE_FROM_UNIX(date)) == volume->v_ctime) {
1573 volume = Volumes; /* restart */
1576 vol->v_ctime = AD_DATE_FROM_UNIX(date);
1577 ad_setdate(adp, AD_DATE_CREATE | AD_DATE_UNIX, date);
1580 /* ----------------------- */
1581 static int getvolparams( u_int16_t bitmap, struct vol *vol, struct stat *st, char *buf, size_t *buflen)
1584 int bit = 0, isad = 1;
1587 u_int32_t bfree, btotal, bsize;
1588 VolSpace xbfree, xbtotal; /* extended bytes */
1589 char *data, *nameoff = NULL;
1592 LOG(log_debug, logtype_afpd, "getvolparams: Volume '%s'", vol->v_localname);
1594 /* courtesy of jallison@whistle.com:
1595 * For MacOS8.x support we need to create the
1596 * .Parent file here if it doesn't exist. */
1598 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1599 if ( ad_open_metadata( vol->v_path, ADFLAGS_DIR, O_CREAT, &ad) < 0 ) {
1601 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1603 } else if (ad_get_MD_flags( &ad ) & O_CREAT) {
1604 slash = strrchr( vol->v_path, '/' );
1608 slash = vol->v_path;
1609 if (ad_getentryoff(&ad, ADEID_NAME)) {
1610 ad_setentrylen( &ad, ADEID_NAME, strlen( slash ));
1611 memcpy(ad_entry( &ad, ADEID_NAME ), slash,
1612 ad_getentrylen( &ad, ADEID_NAME ));
1614 vol_setdate(vol->v_vid, &ad, st->st_mtime);
1618 if (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0)
1619 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1621 vol->v_ctime = aint;
1624 if (( bitmap & ( (1<<VOLPBIT_BFREE)|(1<<VOLPBIT_BTOTAL) |
1625 (1<<VOLPBIT_XBFREE)|(1<<VOLPBIT_XBTOTAL) |
1626 (1<<VOLPBIT_BSIZE)) ) != 0 ) {
1627 if ( getvolspace( vol, &bfree, &btotal, &xbfree, &xbtotal,
1628 &bsize) != AFP_OK ) {
1630 ad_close( &ad, ADFLAGS_HF );
1632 return( AFPERR_PARAM );
1637 while ( bitmap != 0 ) {
1638 while (( bitmap & 1 ) == 0 ) {
1646 /* check for read-only.
1647 * NOTE: we don't actually set the read-only flag unless
1648 * it's passed in that way as it's possible to mount
1649 * a read-write filesystem under a read-only one. */
1650 if ((vol->v_flags & AFPVOL_RO) ||
1651 ((utime(vol->v_path, NULL) < 0) && (errno == EROFS))) {
1652 ashort |= VOLPBIT_ATTR_RO;
1654 /* prior 2.1 only VOLPBIT_ATTR_RO is defined */
1655 if (afp_version > 20) {
1656 if (0 == (vol->v_flags & AFPVOL_NOFILEID) && vol->v_cdb != NULL &&
1657 (vol->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1658 ashort |= VOLPBIT_ATTR_FILEID;
1660 ashort |= VOLPBIT_ATTR_CATSEARCH;
1662 if (afp_version >= 30) {
1663 ashort |= VOLPBIT_ATTR_UTF8;
1664 if (vol->v_flags & AFPVOL_UNIX_PRIV)
1665 ashort |= VOLPBIT_ATTR_UNIXPRIV;
1666 if (vol->v_flags & AFPVOL_TM)
1667 ashort |= VOLPBIT_ATTR_TM;
1668 if (vol->v_flags & AFPVOL_NONETIDS)
1669 ashort |= VOLPBIT_ATTR_NONETIDS;
1670 if (afp_version >= 32) {
1672 ashort |= VOLPBIT_ATTR_EXT_ATTRS;
1673 if (vol->v_flags & AFPVOL_ACLS)
1674 ashort |= VOLPBIT_ATTR_ACLS;
1678 ashort = htons(ashort);
1679 memcpy(data, &ashort, sizeof( ashort ));
1680 data += sizeof( ashort );
1684 ashort = htons( AFPVOLSIG_DEFAULT );
1685 memcpy(data, &ashort, sizeof( ashort ));
1686 data += sizeof( ashort );
1689 case VOLPBIT_CDATE :
1690 aint = vol->v_ctime;
1691 memcpy(data, &aint, sizeof( aint ));
1692 data += sizeof( aint );
1695 case VOLPBIT_MDATE :
1696 if ( st->st_mtime > vol->v_mtime ) {
1697 vol->v_mtime = st->st_mtime;
1699 aint = AD_DATE_FROM_UNIX(vol->v_mtime);
1700 memcpy(data, &aint, sizeof( aint ));
1701 data += sizeof( aint );
1704 case VOLPBIT_BDATE :
1705 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1706 aint = AD_DATE_START;
1707 memcpy(data, &aint, sizeof( aint ));
1708 data += sizeof( aint );
1712 memcpy(data, &vol->v_vid, sizeof( vol->v_vid ));
1713 data += sizeof( vol->v_vid );
1716 case VOLPBIT_BFREE :
1717 bfree = htonl( bfree );
1718 memcpy(data, &bfree, sizeof( bfree ));
1719 data += sizeof( bfree );
1722 case VOLPBIT_BTOTAL :
1723 btotal = htonl( btotal );
1724 memcpy(data, &btotal, sizeof( btotal ));
1725 data += sizeof( btotal );
1728 #ifndef NO_LARGE_VOL_SUPPORT
1729 case VOLPBIT_XBFREE :
1730 xbfree = hton64( xbfree );
1731 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1732 bcopy(&xbfree, data, sizeof(xbfree));
1733 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1734 memcpy(data, &xbfree, sizeof( xbfree ));
1735 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1736 data += sizeof( xbfree );
1739 case VOLPBIT_XBTOTAL :
1740 xbtotal = hton64( xbtotal );
1741 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1742 bcopy(&xbtotal, data, sizeof(xbtotal));
1743 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1744 memcpy(data, &xbtotal, sizeof( xbtotal ));
1745 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1746 data += sizeof( xbfree );
1748 #endif /* ! NO_LARGE_VOL_SUPPORT */
1752 data += sizeof( u_int16_t );
1755 case VOLPBIT_BSIZE: /* block size */
1756 bsize = htonl(bsize);
1757 memcpy(data, &bsize, sizeof(bsize));
1758 data += sizeof(bsize);
1763 ad_close( &ad, ADFLAGS_HF );
1765 return( AFPERR_BITMAP );
1771 ashort = htons( data - buf );
1772 memcpy(nameoff, &ashort, sizeof( ashort ));
1773 /* name is always in mac charset */
1774 aint = ucs2_to_charset( vol->v_maccharset, vol->v_macname, data+1, AFPVOL_MACNAMELEN + 1);
1784 ad_close_metadata( &ad);
1786 *buflen = data - buf;
1790 /* ------------------------- */
1791 static int stat_vol(u_int16_t bitmap, struct vol *vol, char *rbuf, size_t *rbuflen)
1797 if ( stat( vol->v_path, &st ) < 0 ) {
1799 return( AFPERR_PARAM );
1801 /* save the volume device number */
1802 vol->v_dev = st.st_dev;
1804 buflen = *rbuflen - sizeof( bitmap );
1805 if (( ret = getvolparams( bitmap, vol, &st,
1806 rbuf + sizeof( bitmap ), &buflen )) != AFP_OK ) {
1810 *rbuflen = buflen + sizeof( bitmap );
1811 bitmap = htons( bitmap );
1812 memcpy(rbuf, &bitmap, sizeof( bitmap ));
1817 /* ------------------------------- */
1818 void load_volumes(AFPObj *obj)
1820 struct passwd *pwent;
1825 /* check files date */
1826 if (obj->options.defaultvol.loaded) {
1827 changed = volfile_changed(&obj->options.defaultvol);
1829 if (obj->options.systemvol.loaded) {
1830 changed |= volfile_changed(&obj->options.systemvol);
1832 if (obj->options.uservol.loaded) {
1833 changed |= volfile_changed(&obj->options.uservol);
1842 if (parent_or_child == 0) {
1843 LOG(log_debug, logtype_afpd, "load_volumes: AFP MASTER");
1845 LOG(log_debug, logtype_afpd, "load_volumes: user: %s", obj->username);
1848 pwent = getpwnam(obj->username);
1849 if ( (obj->options.flags & OPTION_USERVOLFIRST) == 0 ) {
1850 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent);
1853 if ((*obj->username == '\0') || (obj->options.flags & OPTION_NOUSERVOL)) {
1854 readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent);
1857 * Read user's AppleVolumes or .AppleVolumes file
1858 * If neither are readable, read the default volumes file. if
1859 * that doesn't work, create a user share.
1861 if (obj->options.uservol.name) {
1862 free(obj->options.uservol.name);
1864 obj->options.uservol.name = strdup(pwent->pw_dir);
1865 if ( readvolfile(obj, &obj->options.uservol, "AppleVolumes", 1, pwent) < 0 &&
1866 readvolfile(obj, &obj->options.uservol, ".AppleVolumes", 1, pwent) < 0 &&
1867 readvolfile(obj, &obj->options.uservol, "applevolumes", 1, pwent) < 0 &&
1868 readvolfile(obj, &obj->options.uservol, ".applevolumes", 1, pwent) < 0 &&
1869 obj->options.defaultvol.name != NULL ) {
1870 if (readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent) < 0)
1871 creatvol(obj, pwent, pwent->pw_dir, NULL, NULL, 1);
1874 if ( obj->options.flags & OPTION_USERVOLFIRST ) {
1875 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent );
1878 if ( obj->options.closevol ) {
1881 for ( vol = Volumes; vol; vol = vol->v_next ) {
1882 if (vol->v_deleted && !vol->v_new ) {
1891 /* ------------------------------- */
1892 int afp_getsrvrparms(AFPObj *obj, char *ibuf _U_, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
1905 for ( vcnt = 0, volume = Volumes; volume; volume = volume->v_next ) {
1906 if (!(volume->v_flags & AFPVOL_NOSTAT)) {
1909 if ( stat( volume->v_path, &st ) < 0 ) {
1910 LOG(log_info, logtype_afpd, "afp_getsrvrparms(%s): stat: %s",
1911 volume->v_path, strerror(errno) );
1912 continue; /* can't access directory */
1914 if (!S_ISDIR(st.st_mode)) {
1915 continue; /* not a dir */
1917 accessmode(volume->v_path, &ma, NULL, &st);
1918 if ((ma.ma_user & (AR_UREAD | AR_USEARCH)) != (AR_UREAD | AR_USEARCH)) {
1919 continue; /* no r-x access */
1922 if (volume->v_hide) {
1923 continue; /* config file changed but the volume was mounted */
1926 if (utf8_encoding()) {
1927 len = ucs2_to_charset_allocate(CH_UTF8_MAC, &namebuf, volume->v_u8mname);
1929 len = ucs2_to_charset_allocate(obj->options.maccharset, &namebuf, volume->v_macname);
1932 if (len == (size_t)-1)
1935 /* set password bit if there's a volume password */
1936 *data = (volume->v_password) ? AFPSRVR_PASSWD : 0;
1938 /* Apple 2 clients running ProDOS-8 expect one volume to have
1939 bit 0 of this byte set. They will not recognize anything
1940 on the server unless this is the case. I have not
1941 completely worked this out, but it's related to booting
1942 from the server. Support for that function is a ways
1943 off.. <shirsch@ibm.net> */
1944 *data |= (volume->v_flags & AFPVOL_A2VOL) ? AFPSRVR_CONFIGINFO : 0;
1945 *data++ |= 0; /* UNIX PRIVS BIT ..., OSX doesn't seem to use it, so we don't either */
1947 memcpy(data, namebuf, len );
1953 *rbuflen = data - rbuf;
1955 if ( gettimeofday( &tv, NULL ) < 0 ) {
1956 LOG(log_error, logtype_afpd, "afp_getsrvrparms(%s): gettimeofday: %s", volume->v_path, strerror(errno) );
1958 return AFPERR_PARAM;
1960 tv.tv_sec = AD_DATE_FROM_UNIX(tv.tv_sec);
1961 memcpy(data, &tv.tv_sec, sizeof( u_int32_t));
1962 data += sizeof( u_int32_t);
1967 /* ------------------------- */
1968 static int volume_codepage(AFPObj *obj, struct vol *volume)
1970 struct charset_functions *charset;
1973 if (!volume->v_volcodepage)
1974 volume->v_volcodepage = strdup("UTF8");
1976 if ( (charset_t) -1 == ( volume->v_volcharset = add_charset(volume->v_volcodepage)) ) {
1977 LOG (log_error, logtype_afpd, "Setting codepage %s as volume codepage failed", volume->v_volcodepage);
1981 if ( NULL == (charset = find_charset_functions(volume->v_volcodepage)) || charset->flags & CHARSET_ICONV ) {
1982 LOG (log_warning, logtype_afpd, "WARNING: volume encoding %s is *not* supported by netatalk, expect problems !!!!", volume->v_volcodepage);
1985 if (!volume->v_maccodepage)
1986 volume->v_maccodepage = strdup(obj->options.maccodepage);
1988 if ( (charset_t) -1 == ( volume->v_maccharset = add_charset(volume->v_maccodepage)) ) {
1989 LOG (log_error, logtype_afpd, "Setting codepage %s as mac codepage failed", volume->v_maccodepage);
1993 if ( NULL == ( charset = find_charset_functions(volume->v_maccodepage)) || ! (charset->flags & CHARSET_CLIENT) ) {
1994 LOG (log_error, logtype_afpd, "Fatal error: mac charset %s not supported", volume->v_maccodepage);
1997 volume->v_kTextEncoding = htonl(charset->kTextEncoding);
2001 /* ------------------------- */
2002 static int volume_openDB(struct vol *volume)
2006 if ((volume->v_flags & AFPVOL_NODEV)) {
2007 flags |= CNID_FLAG_NODEV;
2010 if (volume->v_cnidscheme == NULL) {
2011 volume->v_cnidscheme = strdup(DEFAULT_CNID_SCHEME);
2012 LOG(log_info, logtype_afpd, "Volume %s use CNID scheme %s.",
2013 volume->v_path, volume->v_cnidscheme);
2016 LOG(log_info, logtype_afpd, "CNID server: %s:%s",
2017 volume->v_cnidserver ? volume->v_cnidserver : Cnid_srv,
2018 volume->v_cnidport ? volume->v_cnidport : Cnid_port);
2021 /* Found this in branch dir-rewrite, maybe we want to use it sometimes */
2023 /* Legacy pre 2.1 way of sharing eg CD-ROM */
2024 if (strcmp(volume->v_cnidscheme, "last") == 0) {
2025 /* "last" is gone. We support it by switching to in-memory "tdb" */
2026 volume->v_cnidscheme = strdup("tdb");
2027 flags |= CNID_FLAG_MEMORY;
2030 /* New way of sharing CD-ROM */
2031 if (volume->v_flags & AFPVOL_CDROM) {
2032 flags |= CNID_FLAG_MEMORY;
2033 if (strcmp(volume->v_cnidscheme, "tdb") != 0) {
2034 free(volume->v_cnidscheme);
2035 volume->v_cnidscheme = strdup("tdb");
2036 LOG(log_info, logtype_afpd, "Volume %s is ejectable, switching to scheme %s.",
2037 volume->v_path, volume->v_cnidscheme);
2042 volume->v_cdb = cnid_open(volume->v_path,
2044 volume->v_cnidscheme,
2046 volume->v_cnidserver ? volume->v_cnidserver : Cnid_srv,
2047 volume->v_cnidport ? volume->v_cnidport : Cnid_port);
2049 if ( ! volume->v_cdb && ! (flags & CNID_FLAG_MEMORY)) {
2050 /* The first attempt failed and it wasn't yet an attempt to open in-memory */
2051 LOG(log_error, logtype_afpd, "Can't open volume \"%s\" CNID backend \"%s\" ",
2052 volume->v_path, volume->v_cnidscheme);
2053 LOG(log_error, logtype_afpd, "Reopen volume %s using in memory temporary CNID DB.",
2055 flags |= CNID_FLAG_MEMORY;
2056 volume->v_cdb = cnid_open (volume->v_path, volume->v_umask, "tdb", flags, NULL, NULL);
2058 /* kill ourself with SIGUSR2 aka msg pending */
2059 if (volume->v_cdb) {
2060 setmessage("Something wrong with the volume's CNID DB, using temporary CNID DB instead."
2061 "Check server messages for details!");
2062 kill(getpid(), SIGUSR2);
2063 /* deactivate cnid caching/storing in AppleDouble files */
2064 volume->v_flags &= ~AFPVOL_CACHE;
2069 return (!volume->v_cdb)?-1:0;
2073 Check if the underlying filesystem supports EAs for ea:sys volumes.
2074 If not, switch to ea:ad.
2075 As we can't check (requires write access) on ro-volumes, we switch ea:auto
2076 volumes that are options:ro to ea:none.
2078 static void check_ea_sys_support(struct vol *vol)
2080 uid_t process_uid = 0;
2081 char eaname[] = {"org.netatalk.supports-eas.XXXXXX"};
2082 const char *eacontent = "yes";
2084 if (vol->v_vfs_ea == AFPVOL_EA_AUTO) {
2086 if ((vol->v_flags & AFPVOL_RO) == AFPVOL_RO) {
2087 LOG(log_info, logtype_afpd, "read-only volume '%s', can't test for EA support, disabling EAs", vol->v_localname);
2088 vol->v_vfs_ea = AFPVOL_EA_NONE;
2094 process_uid = geteuid();
2096 if (seteuid(0) == -1) {
2097 LOG(log_error, logtype_afpd, "check_ea_sys_support: can't seteuid(0): %s", strerror(errno));
2101 if ((sys_setxattr(vol->v_path, eaname, eacontent, 4, 0)) == 0) {
2102 sys_removexattr(vol->v_path, eaname);
2103 vol->v_vfs_ea = AFPVOL_EA_SYS;
2105 LOG(log_warning, logtype_afpd, "volume \"%s\" does not support Extended Attributes, using ea:ad instead",
2107 vol->v_vfs_ea = AFPVOL_EA_AD;
2111 if (seteuid(process_uid) == -1) {
2112 LOG(log_error, logtype_afpd, "can't seteuid back %s", strerror(errno));
2119 /* -------------------------
2120 * we are the user here
2122 int afp_openvol(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2133 char path[ MAXPATHLEN + 1];
2139 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2140 bitmap = ntohs( bitmap );
2141 ibuf += sizeof( bitmap );
2144 if (( bitmap & (1<<VOLPBIT_VID)) == 0 ) {
2145 return AFPERR_BITMAP;
2148 len = (unsigned char)*ibuf++;
2149 volname = obj->oldtmp;
2151 if ((volname_tmp = strchr(volname,'+')) != NULL)
2152 volname = volname_tmp+1;
2154 if (utf8_encoding()) {
2155 namelen = convert_string(CH_UTF8_MAC, CH_UCS2, ibuf, len, volname, sizeof(obj->oldtmp));
2157 namelen = convert_string(obj->options.maccharset, CH_UCS2, ibuf, len, volname, sizeof(obj->oldtmp));
2160 if ( namelen <= 0) {
2161 return AFPERR_PARAM;
2165 if ((len + 1) & 1) /* pad to an even boundary */
2170 for ( volume = Volumes; volume; volume = volume->v_next ) {
2171 if ( strcasecmp_w( (ucs2_t*) volname, volume->v_name ) == 0 ) {
2176 if ( volume == NULL ) {
2177 return AFPERR_PARAM;
2180 /* check for a volume password */
2181 if (volume->v_password && strncmp(ibuf, volume->v_password, VOLPASSLEN)) {
2182 return AFPERR_ACCESS;
2185 if (( volume->v_flags & AFPVOL_OPEN ) ) {
2186 /* the volume is already open */
2188 set_uidgid ( volume );
2190 return stat_vol(bitmap, volume, rbuf, rbuflen);
2193 if (volume->v_root_preexec) {
2194 if ((ret = afprun(1, volume->v_root_preexec, NULL)) && volume->v_root_preexec_close) {
2195 LOG(log_error, logtype_afpd, "afp_openvol(%s): root preexec : %d", volume->v_path, ret );
2201 set_uidgid ( volume );
2204 if (volume->v_preexec) {
2205 if ((ret = afprun(0, volume->v_preexec, NULL)) && volume->v_preexec_close) {
2206 LOG(log_error, logtype_afpd, "afp_openvol(%s): preexec : %d", volume->v_path, ret );
2211 if ( stat( volume->v_path, &st ) < 0 ) {
2212 return AFPERR_PARAM;
2215 if ( chdir( volume->v_path ) < 0 ) {
2216 return AFPERR_PARAM;
2219 if ( NULL == getcwd(path, MAXPATHLEN)) {
2220 /* shouldn't be fatal but it will fail later */
2221 LOG(log_error, logtype_afpd, "afp_openvol(%s): volume pathlen too long", volume->v_path);
2225 /* Normalize volume path */
2226 #ifdef REALPATH_TAKES_NULL
2227 if ((volume->v_path = realpath(path, NULL)) == NULL)
2230 if ((volume->v_path = malloc(MAXPATHLEN+1)) == NULL)
2232 if (realpath(path, volume->v_path) == NULL) {
2233 free(volume->v_path);
2236 /* Safe some memory */
2238 if ((tmp = strdup(volume->v_path)) == NULL) {
2239 free(volume->v_path);
2242 free(volume->v_path);
2243 volume->v_path = tmp;
2246 if (volume_codepage(obj, volume) < 0) {
2251 /* initialize volume variables
2254 if (utf8_encoding()) {
2255 volume->max_filename = UTF8FILELEN_EARLY;
2258 volume->max_filename = MACFILELEN;
2261 volume->v_flags |= AFPVOL_OPEN;
2262 volume->v_cdb = NULL;
2264 if (utf8_encoding()) {
2265 len = convert_string_allocate(CH_UCS2, CH_UTF8_MAC, volume->v_u8mname, namelen, &vol_mname);
2267 len = convert_string_allocate(CH_UCS2, obj->options.maccharset, volume->v_macname, namelen, &vol_mname);
2269 if ( !vol_mname || len <= 0) {
2274 if ((vol_uname = strrchr(path, '/')) == NULL)
2276 else if (*(vol_uname + 1) != '\0')
2279 if ((dir = dir_new(vol_mname,
2284 bfromcstr(volume->v_path),
2288 LOG(log_error, logtype_afpd, "afp_openvol(%s): malloc: %s", volume->v_path, strerror(errno) );
2293 volume->v_root = dir;
2296 if (volume_openDB(volume) < 0) {
2297 LOG(log_error, logtype_afpd, "Fatal error: cannot open CNID or invalid CNID backend for %s: %s",
2298 volume->v_path, volume->v_cnidscheme);
2303 ret = stat_vol(bitmap, volume, rbuf, rbuflen);
2304 if (ret == AFP_OK) {
2306 if (!(volume->v_flags & AFPVOL_RO)) {
2307 handle_special_folders( volume );
2309 volume->v_cnidserver ? volume->v_cnidserver : Cnid_srv,
2310 volume->v_cnidport ? volume->v_cnidport : Cnid_port);
2314 * If you mount a volume twice, the second time the trash appears on
2315 * the desk-top. That's because the Mac remembers the DID for the
2316 * trash (even for volumes in different zones, on different servers).
2317 * Just so this works better, we prime the DID cache with the trash,
2318 * fixing the trash at DID 17.
2319 * FIXME (RL): should it be done inside a CNID backend ? (always returning Trash DID when asked) ?
2321 if ((volume->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
2323 /* FIXME find db time stamp */
2324 if (cnid_getstamp(volume->v_cdb, volume->v_stamp, sizeof(volume->v_stamp)) < 0) {
2325 LOG (log_error, logtype_afpd,
2326 "afp_openvol(%s): Fatal error: Unable to get stamp value from CNID backend",
2334 cname( volume, volume->v_root, &p );
2340 if (volume->v_root) {
2341 dir_free( volume->v_root );
2342 volume->v_root = NULL;
2345 volume->v_flags &= ~AFPVOL_OPEN;
2346 if (volume->v_cdb != NULL) {
2347 cnid_close(volume->v_cdb);
2348 volume->v_cdb = NULL;
2354 /* ------------------------- */
2355 static void closevol(struct vol *vol)
2360 dir_free( vol->v_root );
2362 if (vol->v_cdb != NULL) {
2363 cnid_close(vol->v_cdb);
2367 if (vol->v_postexec) {
2368 afprun(0, vol->v_postexec, NULL);
2370 if (vol->v_root_postexec) {
2371 afprun(1, vol->v_root_postexec, NULL);
2375 /* ------------------------- */
2376 void close_all_vol(void)
2380 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
2381 if ( (ovol->v_flags & AFPVOL_OPEN) ) {
2382 ovol->v_flags &= ~AFPVOL_OPEN;
2388 /* ------------------------- */
2389 static void deletevol(struct vol *vol)
2393 vol->v_flags &= ~AFPVOL_OPEN;
2394 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
2395 if ( (ovol->v_flags & AFPVOL_OPEN) ) {
2399 if ( ovol != NULL ) {
2400 /* Even if chdir fails, we can't say afp_closevol fails. */
2401 if ( chdir( ovol->v_path ) == 0 ) {
2402 curdir = ovol->v_root;
2407 if (vol->v_deleted) {
2408 showvol(vol->v_name);
2415 /* ------------------------- */
2416 int afp_closevol(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2423 memcpy(&vid, ibuf, sizeof( vid ));
2424 if (NULL == ( vol = getvolbyvid( vid )) ) {
2425 return( AFPERR_PARAM );
2434 /* ------------------------- */
2435 struct vol *getvolbyvid(const u_int16_t vid )
2439 for ( vol = Volumes; vol; vol = vol->v_next ) {
2440 if ( vid == vol->v_vid ) {
2444 if ( vol == NULL || ( vol->v_flags & AFPVOL_OPEN ) == 0 ) {
2450 #endif /* FORCE_UIDGID */
2457 /* ------------------------ */
2458 static int ext_cmp_key(const void *key, const void *obj)
2460 const char *p = key;
2461 const struct extmap *em = obj;
2462 return strdiacasecmp(p, em->em_ext);
2464 struct extmap *getextmap(const char *path)
2469 if (!Extmap_cnt || NULL == ( p = strrchr( path, '.' )) ) {
2470 return( Defextmap );
2474 return( Defextmap );
2476 em = bsearch(p, Extmap, Extmap_cnt, sizeof(struct extmap), ext_cmp_key);
2480 return( Defextmap );
2484 /* ------------------------- */
2485 struct extmap *getdefextmap(void)
2487 return( Defextmap );
2490 /* --------------------------
2491 poll if a volume is changed by other processes.
2493 0 no attention msg sent
2494 1 attention msg sent
2495 -1 error (socket closed)
2497 Note: if attention return -1 no packet has been
2498 sent because the buffer is full, we don't care
2499 either there's no reader or there's a lot of
2500 traffic and another pollvoltime will follow
2502 int pollvoltime(AFPObj *obj)
2509 if (!(afp_version > 21 && obj->options.server_notif))
2512 if ( gettimeofday( &tv, NULL ) < 0 )
2515 for ( vol = Volumes; vol; vol = vol->v_next ) {
2516 if ( (vol->v_flags & AFPVOL_OPEN) && vol->v_mtime + 30 < tv.tv_sec) {
2517 if ( !stat( vol->v_path, &st ) && vol->v_mtime != st.st_mtime ) {
2518 vol->v_mtime = st.st_mtime;
2519 if (!obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED))
2528 /* ------------------------- */
2529 void setvoltime(AFPObj *obj, struct vol *vol)
2533 if ( gettimeofday( &tv, NULL ) < 0 ) {
2534 LOG(log_error, logtype_afpd, "setvoltime(%s): gettimeofday: %s", vol->v_path, strerror(errno) );
2537 if( utime( vol->v_path, NULL ) < 0 ) {
2538 /* write of time failed ... probably a read only filesys,
2539 * where no other users can interfere, so there's no issue here
2543 /* a little granularity */
2544 if (vol->v_mtime < tv.tv_sec) {
2545 vol->v_mtime = tv.tv_sec;
2546 /* or finder doesn't update free space
2547 * AFP 3.2 and above clients seem to be ok without so many notification
2549 if (afp_version < 32 && obj->options.server_notif) {
2550 obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED);
2555 /* ------------------------- */
2556 int afp_getvolparams(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_,char *rbuf, size_t *rbuflen)
2559 u_int16_t vid, bitmap;
2562 memcpy(&vid, ibuf, sizeof( vid ));
2563 ibuf += sizeof( vid );
2564 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2565 bitmap = ntohs( bitmap );
2567 if (NULL == ( vol = getvolbyvid( vid )) ) {
2569 return( AFPERR_PARAM );
2572 return stat_vol(bitmap, vol, rbuf, rbuflen);
2575 /* ------------------------- */
2576 int afp_setvolparams(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2580 u_int16_t vid, bitmap;
2586 memcpy(&vid, ibuf, sizeof( vid ));
2587 ibuf += sizeof( vid );
2588 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2589 bitmap = ntohs( bitmap );
2590 ibuf += sizeof(bitmap);
2592 if (( vol = getvolbyvid( vid )) == NULL ) {
2593 return( AFPERR_PARAM );
2596 if ((vol->v_flags & AFPVOL_RO))
2597 return AFPERR_VLOCK;
2599 /* we can only set the backup date. */
2600 if (bitmap != (1 << VOLPBIT_BDATE))
2601 return AFPERR_BITMAP;
2603 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2604 if ( ad_open( vol->v_path, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR,
2607 return AFPERR_VLOCK;
2609 return AFPERR_ACCESS;
2612 memcpy(&aint, ibuf, sizeof(aint));
2613 ad_setdate(&ad, AD_DATE_BACKUP, aint);
2615 ad_close(&ad, ADFLAGS_HF);
2619 /* ------------------------- */
2620 int wincheck(const struct vol *vol, const char *path)
2624 if (!(vol->v_flags & AFPVOL_MSWINDOWS))
2627 /* empty paths are not allowed */
2628 if ((len = strlen(path)) == 0)
2631 /* leading or trailing whitespaces are not allowed, carriage returns
2632 * and probably other whitespace is okay, tabs are not allowed
2634 if ((path[0] == ' ') || (path[len-1] == ' '))
2637 /* certain characters are not allowed */
2638 if (strpbrk(path, MSWINDOWS_BADCHARS))
2641 /* everything else is okay */
2647 * precreate a folder
2648 * this is only intended for folders in the volume root
2649 * It will *not* work if the folder name contains extended characters
2651 static int create_special_folder (const struct vol *vol, const struct _special_folder *folder)
2660 p = (char *) malloc ( strlen(vol->v_path)+strlen(folder->name)+2);
2662 LOG(log_error, logtype_afpd,"malloc failed");
2666 q=strdup(folder->name);
2668 LOG(log_error, logtype_afpd,"malloc failed");
2672 strcpy(p, vol->v_path);
2677 if ((vol->v_casefold & AFPVOL_MTOUUPPER))
2679 else if ((vol->v_casefold & AFPVOL_MTOULOWER))
2685 if ( (ret = stat( p, &st )) < 0 ) {
2686 if (folder->precreate) {
2687 if (ad_mkdir(p, folder->mode)) {
2688 LOG(log_debug, logtype_afpd,"Creating '%s' failed in %s: %s", p, vol->v_path, strerror(errno));
2697 if ( !ret && folder->hide) {
2699 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2700 if (ad_open_metadata( p, ADFLAGS_DIR, O_CREAT, &ad) < 0) {
2706 ad_setname(&ad, folder->name);
2708 ad_getattr(&ad, &attr);
2709 attr |= htons( ntohs( attr ) | ATTRBIT_INVISIBLE );
2710 ad_setattr(&ad, attr);
2712 /* do the same with the finder info */
2713 if (ad_entry(&ad, ADEID_FINDERI)) {
2714 memcpy(&attr, ad_entry(&ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF, sizeof(attr));
2715 attr |= htons(FINDERINFO_INVISIBLE);
2716 memcpy(ad_entry(&ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF,&attr, sizeof(attr));
2720 ad_close_metadata( &ad);
2727 static void handle_special_folders (const struct vol * vol)
2729 const _special_folder *p = &special_folders[0];
2731 if ((vol->v_flags & AFPVOL_RO))
2734 for (; p->name != NULL; p++) {
2735 create_special_folder (vol, p);
2739 const struct vol *getvolumes(void)
2744 void unload_volumes_and_extmap(void)
2746 LOG(log_debug, logtype_afpd, "unload_volumes_and_extmap");
2752 * Get a volumes UUID from the config file.
2753 * If there is none, it is generated and stored there.
2755 * Returns pointer to allocated storage on success, NULL on error.
2757 static char *get_vol_uuid(const AFPObj *obj, const char *volname)
2760 char buf[1024], uuid[UUID_PRINTABLE_STRING_LENGTH], *p;
2762 struct stat tmpstat;
2765 if ((fp = fopen(obj->options.uuidconf, "r")) != NULL) { /* read open? */
2766 /* scan in the conf file */
2767 while (fgets(buf, sizeof(buf), fp) != NULL) {
2769 while (p && isblank(*p))
2771 if (!p || (*p == '#') || (*p == '\n'))
2772 continue; /* invalid line */
2775 if ((volname_conf = strtok( p, "\"" )) == NULL)
2776 continue; /* syntax error */
2778 if ((volname_conf = strtok( p, " \t" )) == NULL)
2779 continue; /* syntax error: invalid name */
2781 p = strchr(p, '\0');
2784 continue; /* syntax error */
2786 if (strcmp(volname, volname_conf) != 0)
2787 continue; /* another volume name */
2789 while (p && isblank(*p))
2792 if (sscanf(p, "%36s", uuid) == 1 ) {
2793 for (int i=0; uuid[i]; i++)
2794 uuid[i] = toupper(uuid[i]);
2795 LOG(log_debug, logtype_afpd, "get_uuid('%s'): UUID: '%s'", volname, uuid);
2797 return strdup(uuid);
2805 /* not found or no file, reopen in append mode */
2807 if (stat(obj->options.uuidconf, &tmpstat)) { /* no file */
2808 if (( fd = creat(obj->options.uuidconf, 0644 )) < 0 ) {
2809 LOG(log_error, logtype_afpd, "ERROR: Cannot create %s (%s).",
2810 obj->options.uuidconf, strerror(errno));
2813 if (( fp = fdopen( fd, "w" )) == NULL ) {
2814 LOG(log_error, logtype_afpd, "ERROR: Cannot fdopen %s (%s).",
2815 obj->options.uuidconf, strerror(errno));
2819 } else if ((fp = fopen(obj->options.uuidconf, "a+")) == NULL) { /* not found */
2820 LOG(log_error, logtype_afpd, "Cannot create or append to %s (%s).",
2821 obj->options.uuidconf, strerror(errno));
2824 fseek(fp, 0L, SEEK_END);
2825 if(ftell(fp) == 0) { /* size = 0 */
2826 fprintf(fp, "# DON'T TOUCH NOR COPY THOUGHTLESSLY!\n");
2827 fprintf(fp, "# This file is auto-generated by afpd\n");
2828 fprintf(fp, "# and stores UUIDs for TM volumes.\n\n");
2830 fseek(fp, -1L, SEEK_END);
2831 if(fgetc(fp) != '\n') fputc('\n', fp); /* last char is \n? */
2834 /* generate uuid and write to file */
2837 randombytes((void *)id, 16);
2838 cp = uuid_bin2string(id);
2840 LOG(log_debug, logtype_afpd, "get_uuid('%s'): generated UUID '%s'", volname, cp);
2842 fprintf(fp, "\"%s\"\t%36s\n", volname, cp);