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>
54 #include <atalk/fce_api.h>
57 #include <atalk/cnid.h>
60 #include "directory.h"
69 extern int afprun(int root, char *cmd, int *outfd);
72 #define MIN(a, b) ((a) < (b) ? (a) : (b))
75 #ifndef UUID_PRINTABLE_STRING_LENGTH
76 #define UUID_PRINTABLE_STRING_LENGTH 37
80 struct vol *current_vol; /* last volume from getvolbyvid() */
82 static struct vol *Volumes = NULL;
83 static u_int16_t lastvid = 0;
84 static char *Trash = "\02\024Network Trash Folder";
86 static struct extmap *Extmap = NULL, *Defextmap = NULL;
87 static int Extmap_cnt;
88 static void free_extmap(void);
90 #define VOLOPT_ALLOW 0 /* user allow list */
91 #define VOLOPT_DENY 1 /* user deny list */
92 #define VOLOPT_RWLIST 2 /* user rw list */
93 #define VOLOPT_ROLIST 3 /* user ro list */
94 #define VOLOPT_PASSWORD 4 /* volume password */
95 #define VOLOPT_CASEFOLD 5 /* character case mangling */
96 #define VOLOPT_FLAGS 6 /* various flags */
97 #define VOLOPT_DBPATH 7 /* path to database */
98 #define VOLOPT_LIMITSIZE 8 /* Limit the size of the volume */
100 #define VOLOPT_VETO 10 /* list of veto filespec */
101 #define VOLOPT_PREEXEC 11 /* preexec command */
102 #define VOLOPT_ROOTPREEXEC 12 /* root preexec command */
103 #define VOLOPT_POSTEXEC 13 /* postexec command */
104 #define VOLOPT_ROOTPOSTEXEC 14 /* root postexec command */
105 #define VOLOPT_ENCODING 15 /* mac encoding (pre OSX)*/
106 #define VOLOPT_MACCHARSET 16
107 #define VOLOPT_CNIDSCHEME 17
108 #define VOLOPT_ADOUBLE 18 /* adouble version */
114 #define VOLOPT_FORCEUID 19 /* force uid for username x */
115 #define VOLOPT_FORCEGID 20 /* force gid for group x */
116 #endif /* FORCE_UIDGID */
118 #define VOLOPT_UMASK 21
119 #define VOLOPT_ALLOWED_HOSTS 22
120 #define VOLOPT_DENIED_HOSTS 23
121 #define VOLOPT_DPERM 24 /* dperm default directories perms */
122 #define VOLOPT_FPERM 25 /* fperm default files perms */
123 #define VOLOPT_DFLTPERM 26 /* perm */
124 #define VOLOPT_EA_VFS 27 /* Extended Attributes vfs indirection */
125 #define VOLOPT_CNIDSERVER 28 /* CNID Server ip address*/
126 #define VOLOPT_CNIDPORT 30 /* CNID server tcp port */
128 #define VOLOPT_MAX 31 /* <== IMPORTANT !!!!!! */
129 #define VOLOPT_NUM (VOLOPT_MAX + 1)
132 #define VOLOPT_DEFAULT ":DEFAULT:"
133 #define VOLOPT_DEFAULT_LEN 9
140 typedef struct _special_folder {
147 static const _special_folder special_folders[] = {
148 {"Network Trash Folder", 1, 0777, 1},
149 {"Temporary Items", 1, 0777, 1},
150 {".AppleDesktop", 1, 0777, 0},
152 {"TheFindByContentFolder", 0, 0, 1},
153 {"TheVolumeSettingsFolder", 0, 0, 1},
157 /* Forward declarations */
158 static void handle_special_folders (const struct vol *);
159 static void deletevol(struct vol *vol);
160 static void volume_free(struct vol *vol);
161 static void check_ea_sys_support(struct vol *vol);
162 static char *get_vol_uuid(const AFPObj *obj, const char *volname);
163 static int readvolfile(AFPObj *obj, struct afp_volume_name *p1,char *p2, int user, struct passwd *pwent);
165 static void volfree(struct vol_option *options, const struct vol_option *save)
170 for (i = 0; i < VOLOPT_MAX; i++) {
171 if (options[i].c_value && (options[i].c_value != save[i].c_value))
172 free(options[i].c_value);
175 for (i = 0; i < VOLOPT_MAX; i++) {
176 if (options[i].c_value)
177 free(options[i].c_value);
183 #define is_var(a, b) (strncmp((a), (b), 2) == 0)
186 * Handle variable substitutions. here's what we understand:
187 * $b -> basename of path
188 * $c -> client ip/appletalk address
189 * $d -> volume pathname on server
190 * $f -> full name (whatever's in the gecos field)
193 * $i -> client ip/appletalk address without port
194 * $s -> server name (hostname if it doesn't exist)
195 * $u -> username (guest is usually nobody)
196 * $v -> volume name or basename if null
197 * $z -> zone (may not exist)
200 * This get's called from readvolfile with
201 * path = NULL, volname = NULL for xlating the volumes path
202 * path = path, volname = NULL for xlating the volumes name
203 * ... and from volumes options parsing code when xlating eg dbpath with
204 * path = path, volname = volname
206 * Using this information we can reject xlation of any variable depeninding on a login
207 * context which is not given in the afp master, where we must evaluate this whole stuff
208 * too for the Zeroconf announcements.
210 static char *volxlate(AFPObj *obj,
223 int xlatevolname = 0;
225 if (parent_or_child == 0)
228 if (path && !volname)
236 dest = calloc(destlen +1, 1);
242 strlcpy(dest, src, destlen +1);
243 if ((p = strchr(src, '$')) == NULL) /* nothing to do */
246 /* first part of the path. just forward to the next variable. */
247 len = MIN((size_t)(p - src), destlen);
253 while (p && destlen > 0) {
254 /* now figure out what the variable is */
256 if (is_var(p, "$b")) {
257 if (afpmaster && xlatevolname)
260 if ((q = strrchr(path, '/')) == NULL)
262 else if (*(q + 1) != '\0')
265 } else if (is_var(p, "$c")) {
266 if (afpmaster && xlatevolname)
269 if (obj->proto == AFPPROTO_ASP) {
270 ASP asp = obj->handle;
272 len = sprintf(dest, "%u.%u", ntohs(asp->asp_sat.sat_addr.s_net),
273 asp->asp_sat.sat_addr.s_node);
279 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)
309 if (obj->proto == AFPPROTO_ASP) {
310 ASP asp = obj->handle;
311 len = sprintf(dest, "%u", ntohs(asp->asp_sat.sat_addr.s_net));
317 if (obj->proto == AFPPROTO_DSI) {
318 DSI *dsi = obj->handle;
319 q = getip_string((struct sockaddr *)&dsi->client);
321 } else if (is_var(p, "$s")) {
324 else if (obj->options.server) {
325 q = obj->options.server;
327 q = obj->options.hostname;
328 } else if (obj->username && is_var(p, "$u")) {
329 if (afpmaster && xlatevolname)
332 if ( obj->options.ntseparator && (sep = strchr(obj->username, obj->options.ntseparator[0])) != NULL)
336 } else if (is_var(p, "$v")) {
337 if (afpmaster && xlatevolname)
343 if ((q = strrchr(path, '/')) == NULL)
345 else if (*(q + 1) != '\0')
348 } else if (is_var(p, "$z")) {
350 } else if (is_var(p, "$$")) {
355 /* copy the stuff over. if we don't understand something that we
356 * should, just skip it over. */
358 len = MIN(p == q ? 2 : strlen(q), destlen);
359 strncpy(dest, q, len);
364 /* stuff up to next $ */
366 p = strchr(src, '$');
367 len = p ? MIN((size_t)(p - src), destlen) : destlen;
369 strncpy(dest, src, len);
377 /* to make sure that val is valid, make sure to select an opt that
379 static int optionok(const char *buf, const char *opt, const char *val)
381 if (!strstr(buf,opt))
389 /* -------------------- */
390 static void setoption(struct vol_option *options, struct vol_option *save, int opt, const char *val)
392 if (options[opt].c_value && (!save || options[opt].c_value != save[opt].c_value))
393 free(options[opt].c_value);
394 options[opt].c_value = strdup(val + 1);
397 /* ------------------------------------------
398 handle all the options. tmp can't be NULL. */
399 static void volset(struct vol_option *options, struct vol_option *save,
400 char *volname, int vlen,
405 val = strchr(tmp, ':');
407 /* we'll assume it's a volume name. */
408 strncpy(volname, tmp, vlen);
413 LOG(log_debug, logtype_afpd, "Parsing volset %s", val);
415 if (optionok(tmp, "allow:", val)) {
416 setoption(options, save, VOLOPT_ALLOW, val);
418 } else if (optionok(tmp, "deny:", val)) {
419 setoption(options, save, VOLOPT_DENY, val);
421 } else if (optionok(tmp, "rwlist:", val)) {
422 setoption(options, save, VOLOPT_RWLIST, val);
424 } else if (optionok(tmp, "rolist:", val)) {
425 setoption(options, save, VOLOPT_ROLIST, val);
427 } else if (optionok(tmp, "codepage:", val)) {
428 LOG (log_error, logtype_afpd, "The old codepage system has been removed. Please make sure to read the documentation !!!!");
429 /* Make sure we don't screw anything */
431 } else if (optionok(tmp, "volcharset:", val)) {
432 setoption(options, save, VOLOPT_ENCODING, val);
433 } else if (optionok(tmp, "maccharset:", val)) {
434 setoption(options, save, VOLOPT_MACCHARSET, val);
435 } else if (optionok(tmp, "veto:", val)) {
436 setoption(options, save, VOLOPT_VETO, val);
437 } else if (optionok(tmp, "cnidscheme:", val)) {
438 setoption(options, save, VOLOPT_CNIDSCHEME, val);
439 } else if (optionok(tmp, "casefold:", val)) {
440 if (strcasecmp(val + 1, "tolower") == 0)
441 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMLOWER;
442 else if (strcasecmp(val + 1, "toupper") == 0)
443 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMUPPER;
444 else if (strcasecmp(val + 1, "xlatelower") == 0)
445 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UUPPERMLOWER;
446 else if (strcasecmp(val + 1, "xlateupper") == 0)
447 options[VOLOPT_CASEFOLD].i_value = AFPVOL_ULOWERMUPPER;
448 } else if (optionok(tmp, "adouble:", val)) {
449 if (strcasecmp(val + 1, "v1") == 0)
450 options[VOLOPT_ADOUBLE].i_value = AD_VERSION1;
451 #if AD_VERSION == AD_VERSION2
452 else if (strcasecmp(val + 1, "v2") == 0)
453 options[VOLOPT_ADOUBLE].i_value = AD_VERSION2;
454 else if (strcasecmp(val + 1, "osx") == 0)
455 options[VOLOPT_ADOUBLE].i_value = AD_VERSION2_OSX;
456 else if (strcasecmp(val + 1, "sfm") == 0)
457 options[VOLOPT_ADOUBLE].i_value = AD_VERSION1_SFM;
459 } else if (optionok(tmp, "options:", val)) {
462 if ((p = strtok(val + 1, ",")) == NULL) /* nothing */
466 if (strcasecmp(p, "prodos") == 0)
467 options[VOLOPT_FLAGS].i_value |= AFPVOL_A2VOL;
468 else if (strcasecmp(p, "mswindows") == 0) {
469 options[VOLOPT_FLAGS].i_value |= AFPVOL_MSWINDOWS | AFPVOL_USEDOTS;
470 } else if (strcasecmp(p, "crlf") == 0)
471 options[VOLOPT_FLAGS].i_value |= AFPVOL_CRLF;
472 else if (strcasecmp(p, "noadouble") == 0)
473 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOADOUBLE;
474 else if (strcasecmp(p, "ro") == 0)
475 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
476 else if (strcasecmp(p, "nohex") == 0)
477 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOHEX;
478 else if (strcasecmp(p, "usedots") == 0)
479 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS;
480 else if (strcasecmp(p, "invisibledots") == 0)
481 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS | AFPVOL_INV_DOTS;
482 else if (strcasecmp(p, "limitsize") == 0)
483 options[VOLOPT_FLAGS].i_value |= AFPVOL_LIMITSIZE;
484 /* support for either "dropbox" or "dropkludge" */
485 else if (strcasecmp(p, "dropbox") == 0)
486 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
487 else if (strcasecmp(p, "dropkludge") == 0)
488 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
489 else if (strcasecmp(p, "nofileid") == 0)
490 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOFILEID;
491 else if (strcasecmp(p, "nostat") == 0)
492 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOSTAT;
493 else if (strcasecmp(p, "preexec_close") == 0)
494 options[VOLOPT_PREEXEC].i_value = 1;
495 else if (strcasecmp(p, "root_preexec_close") == 0)
496 options[VOLOPT_ROOTPREEXEC].i_value = 1;
497 else if (strcasecmp(p, "upriv") == 0)
498 options[VOLOPT_FLAGS].i_value |= AFPVOL_UNIX_PRIV;
499 else if (strcasecmp(p, "nodev") == 0)
500 options[VOLOPT_FLAGS].i_value |= AFPVOL_NODEV;
501 else if (strcasecmp(p, "caseinsensitive") == 0)
502 options[VOLOPT_FLAGS].i_value |= AFPVOL_CASEINSEN;
503 else if (strcasecmp(p, "illegalseq") == 0)
504 options[VOLOPT_FLAGS].i_value |= AFPVOL_EILSEQ;
505 else if (strcasecmp(p, "nocnidcache") == 0)
506 options[VOLOPT_FLAGS].i_value &= ~AFPVOL_CACHE;
507 else if (strcasecmp(p, "tm") == 0)
508 options[VOLOPT_FLAGS].i_value |= AFPVOL_TM;
509 else if (strcasecmp(p, "searchdb") == 0)
510 options[VOLOPT_FLAGS].i_value |= AFPVOL_SEARCHDB;
511 else if (strcasecmp(p, "nonetids") == 0)
512 options[VOLOPT_FLAGS].i_value |= AFPVOL_NONETIDS;
513 else if (strcasecmp(p, "noacls") == 0)
514 options[VOLOPT_FLAGS].i_value &= ~AFPVOL_ACLS;
515 p = strtok(NULL, ",");
518 } else if (optionok(tmp, "cnidserver:", val)) {
519 setoption(options, save, VOLOPT_CNIDSERVER, val);
521 char *p = strrchr(val + 1, ':');
524 setoption(options, save, VOLOPT_CNIDPORT, p);
527 LOG(log_debug, logtype_afpd, "CNID Server for volume '%s': %s:%s",
528 volname, val + 1, p ? p + 1 : Cnid_port);
530 } else if (optionok(tmp, "dbpath:", val)) {
531 setoption(options, save, VOLOPT_DBPATH, val);
533 } else if (optionok(tmp, "umask:", val)) {
534 options[VOLOPT_UMASK].i_value = (int)strtol(val +1, NULL, 8);
535 } else if (optionok(tmp, "dperm:", val)) {
536 options[VOLOPT_DPERM].i_value = (int)strtol(val+1, NULL, 8);
537 } else if (optionok(tmp, "fperm:", val)) {
538 options[VOLOPT_FPERM].i_value = (int)strtol(val+1, NULL, 8);
539 } else if (optionok(tmp, "perm:", val)) {
540 options[VOLOPT_DFLTPERM].i_value = (int)strtol(val+1, NULL, 8);
541 } else if (optionok(tmp, "password:", val)) {
542 setoption(options, save, VOLOPT_PASSWORD, val);
546 /* this code allows forced uid/gid per volume settings */
547 } else if (optionok(tmp, "forceuid:", val)) {
548 setoption(options, save, VOLOPT_FORCEUID, val);
549 } else if (optionok(tmp, "forcegid:", val)) {
550 setoption(options, save, VOLOPT_FORCEGID, val);
552 #endif /* FORCE_UIDGID */
553 } else if (optionok(tmp, "root_preexec:", val)) {
554 setoption(options, save, VOLOPT_ROOTPREEXEC, val);
556 } else if (optionok(tmp, "preexec:", val)) {
557 setoption(options, save, VOLOPT_PREEXEC, val);
559 } else if (optionok(tmp, "root_postexec:", val)) {
560 setoption(options, save, VOLOPT_ROOTPOSTEXEC, val);
562 } else if (optionok(tmp, "postexec:", val)) {
563 setoption(options, save, VOLOPT_POSTEXEC, val);
565 } else if (optionok(tmp, "allowed_hosts:", val)) {
566 setoption(options, save, VOLOPT_ALLOWED_HOSTS, val);
568 } else if (optionok(tmp, "denied_hosts:", val)) {
569 setoption(options, save, VOLOPT_DENIED_HOSTS, val);
571 } else if (optionok(tmp, "ea:", val)) {
572 if (strcasecmp(val + 1, "ad") == 0)
573 options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_AD;
574 else if (strcasecmp(val + 1, "sys") == 0)
575 options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_SYS;
576 else if (strcasecmp(val + 1, "none") == 0)
577 options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_NONE;
579 } else if (optionok(tmp, "volsizelimit:", val)) {
580 options[VOLOPT_LIMITSIZE].i_value = (uint32_t)strtoul(val + 1, NULL, 10);
583 /* ignore unknown options */
584 LOG(log_debug, logtype_afpd, "ignoring unknown volume option: %s", tmp);
589 /* ----------------- */
590 static void showvol(const ucs2_t *name)
593 for ( volume = Volumes; volume; volume = volume->v_next ) {
594 if (volume->v_hide && !strcasecmp_w( volume->v_name, name ) ) {
601 /* ------------------------------- */
602 static int creatvol(AFPObj *obj, struct passwd *pwd,
603 char *path, char *name,
604 struct vol_option *options,
605 const int user /* user defined volume */
609 int suffixlen, vlen, tmpvlen, u8mvlen, macvlen;
611 char tmpname[AFPVOL_U8MNAMELEN+1];
612 ucs2_t u8mtmpname[(AFPVOL_U8MNAMELEN+1)*2], mactmpname[(AFPVOL_MACNAMELEN+1)*2];
613 char suffix[6]; /* max is #FFFF */
616 LOG(log_debug, logtype_afpd, "createvol: Volume '%s'", name);
618 if ( name == NULL || *name == '\0' ) {
619 if ((name = strrchr( path, '/' )) == NULL) {
620 return -1; /* Obviously not a fully qualified path */
623 /* if you wish to share /, you need to specify a name. */
628 /* suffix for mangling use (lastvid + 1) */
629 /* because v_vid has not been decided yet. */
630 suffixlen = sprintf(suffix, "%c%X", MANGLE_CHAR, lastvid + 1 );
632 vlen = strlen( name );
634 /* Unicode Volume Name */
635 /* Firstly convert name from unixcharset to UTF8-MAC */
637 tmpvlen = convert_charset(obj->options.unixcharset, CH_UTF8_MAC, 0, name, vlen, tmpname, AFPVOL_U8MNAMELEN, &flags);
639 strcpy(tmpname, "???");
643 /* Do we have to mangle ? */
644 if ( (flags & CONV_REQMANGLE) || (tmpvlen > obj->options.volnamelen)) {
645 if (tmpvlen + suffixlen > obj->options.volnamelen) {
647 tmpvlen = convert_charset(obj->options.unixcharset, CH_UTF8_MAC, 0, name, vlen, tmpname, obj->options.volnamelen - suffixlen, &flags);
648 tmpname[tmpvlen >= 0 ? tmpvlen : 0] = 0;
650 strcat(tmpname, suffix);
651 tmpvlen = strlen(tmpname);
654 /* Secondly convert name from UTF8-MAC to UCS2 */
655 if ( 0 >= ( u8mvlen = convert_string(CH_UTF8_MAC, CH_UCS2, tmpname, tmpvlen, u8mtmpname, AFPVOL_U8MNAMELEN*2)) )
658 LOG(log_maxdebug, logtype_afpd, "createvol: Volume '%s' -> UTF8-MAC Name: '%s'", name, tmpname);
660 /* Maccharset Volume Name */
661 /* Firsty convert name from unixcharset to maccharset */
663 tmpvlen = convert_charset(obj->options.unixcharset, obj->options.maccharset, 0, name, vlen, tmpname, AFPVOL_U8MNAMELEN, &flags);
665 strcpy(tmpname, "???");
669 /* Do we have to mangle ? */
670 if ( (flags & CONV_REQMANGLE) || (tmpvlen > AFPVOL_MACNAMELEN)) {
671 if (tmpvlen + suffixlen > AFPVOL_MACNAMELEN) {
673 tmpvlen = convert_charset(obj->options.unixcharset,
674 obj->options.maccharset,
679 AFPVOL_MACNAMELEN - suffixlen,
681 tmpname[tmpvlen >= 0 ? tmpvlen : 0] = 0;
683 strcat(tmpname, suffix);
684 tmpvlen = strlen(tmpname);
687 /* Secondly convert name from maccharset to UCS2 */
688 if ( 0 >= ( macvlen = convert_string(obj->options.maccharset,
693 AFPVOL_U8MNAMELEN*2)) )
696 LOG(log_maxdebug, logtype_afpd, "createvol: Volume '%s' -> Longname: '%s'", name, tmpname);
698 /* check duplicate */
699 for ( volume = Volumes; volume; volume = volume->v_next ) {
700 if ((utf8_encoding() && (strcasecmp_w(volume->v_u8mname, u8mtmpname) == 0))
702 (!utf8_encoding() && (strcasecmp_w(volume->v_macname, mactmpname) == 0))) {
703 LOG (log_error, logtype_afpd,
704 "Duplicate volume name, check AppleVolumes files: previous: \"%s\", new: \"%s\"",
705 volume->v_localname, name);
706 if (volume->v_deleted) {
707 volume->v_new = hide = 1;
710 return -1; /* Won't be able to access it, anyway... */
715 if (!( volume = (struct vol *)calloc(1, sizeof( struct vol ))) ) {
716 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
719 if ( NULL == ( volume->v_localname = strdup(name))) {
720 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
725 if ( NULL == ( volume->v_u8mname = strdup_w(u8mtmpname))) {
726 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
731 if ( NULL == ( volume->v_macname = strdup_w(mactmpname))) {
732 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
737 if (!( volume->v_path = (char *)malloc( strlen( path ) + 1 )) ) {
738 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
744 volume->v_name = utf8_encoding()?volume->v_u8mname:volume->v_macname;
745 volume->v_hide = hide;
746 strcpy( volume->v_path, path );
750 #endif /* __svr4__ */
751 /* os X start at 1 and use network order ie. 1 2 3 */
752 volume->v_vid = ++lastvid;
753 volume->v_vid = htons(volume->v_vid);
755 if (!check_vol_acl_support(volume)) {
756 LOG(log_debug, logtype_afpd, "creatvol(\"%s\"): disabling ACL support", volume->v_path);
757 options[VOLOPT_FLAGS].i_value &= ~AFPVOL_ACLS;
763 volume->v_casefold = options[VOLOPT_CASEFOLD].i_value;
764 volume->v_flags |= options[VOLOPT_FLAGS].i_value;
766 if (options[VOLOPT_EA_VFS].i_value)
767 volume->v_vfs_ea = options[VOLOPT_EA_VFS].i_value;
769 volume->v_ad_options = 0;
770 if ((volume->v_flags & AFPVOL_NODEV))
771 volume->v_ad_options |= ADVOL_NODEV;
772 if ((volume->v_flags & AFPVOL_CACHE))
773 volume->v_ad_options |= ADVOL_CACHE;
774 if ((volume->v_flags & AFPVOL_UNIX_PRIV))
775 volume->v_ad_options |= ADVOL_UNIXPRIV;
776 if ((volume->v_flags & AFPVOL_INV_DOTS))
777 volume->v_ad_options |= ADVOL_INVDOTS;
778 if ((volume->v_flags & AFPVOL_NOADOUBLE))
779 volume->v_ad_options |= ADVOL_NOADOUBLE;
781 if (options[VOLOPT_PASSWORD].c_value)
782 volume->v_password = strdup(options[VOLOPT_PASSWORD].c_value);
784 if (options[VOLOPT_VETO].c_value)
785 volume->v_veto = strdup(options[VOLOPT_VETO].c_value);
787 if (options[VOLOPT_ENCODING].c_value)
788 volume->v_volcodepage = strdup(options[VOLOPT_ENCODING].c_value);
790 if (options[VOLOPT_MACCHARSET].c_value)
791 volume->v_maccodepage = strdup(options[VOLOPT_MACCHARSET].c_value);
793 if (options[VOLOPT_DBPATH].c_value)
794 volume->v_dbpath = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_DBPATH].c_value, pwd, path, name);
796 if (options[VOLOPT_CNIDSCHEME].c_value)
797 volume->v_cnidscheme = strdup(options[VOLOPT_CNIDSCHEME].c_value);
799 if (options[VOLOPT_CNIDSERVER].c_value)
800 volume->v_cnidserver = strdup(options[VOLOPT_CNIDSERVER].c_value);
802 if (options[VOLOPT_CNIDPORT].c_value)
803 volume->v_cnidport = strdup(options[VOLOPT_CNIDPORT].c_value);
805 if (options[VOLOPT_UMASK].i_value)
806 volume->v_umask = (mode_t)options[VOLOPT_UMASK].i_value;
808 if (options[VOLOPT_DPERM].i_value)
809 volume->v_dperm = (mode_t)options[VOLOPT_DPERM].i_value;
811 if (options[VOLOPT_FPERM].i_value)
812 volume->v_fperm = (mode_t)options[VOLOPT_FPERM].i_value;
814 if (options[VOLOPT_DFLTPERM].i_value)
815 volume->v_perm = (mode_t)options[VOLOPT_DFLTPERM].i_value;
817 if (options[VOLOPT_ADOUBLE].i_value)
818 volume->v_adouble = options[VOLOPT_ADOUBLE].i_value;
820 volume->v_adouble = AD_VERSION;
822 if (options[VOLOPT_LIMITSIZE].i_value)
823 volume->v_limitsize = options[VOLOPT_LIMITSIZE].i_value;
825 /* Mac to Unix conversion flags*/
826 volume->v_mtou_flags = 0;
827 if (!(volume->v_flags & AFPVOL_NOHEX))
828 volume->v_mtou_flags |= CONV_ESCAPEHEX;
829 if (!(volume->v_flags & AFPVOL_USEDOTS))
830 volume->v_mtou_flags |= CONV_ESCAPEDOTS;
831 if ((volume->v_flags & AFPVOL_EILSEQ))
832 volume->v_mtou_flags |= CONV__EILSEQ;
834 if ((volume->v_casefold & AFPVOL_MTOUUPPER))
835 volume->v_mtou_flags |= CONV_TOUPPER;
836 else if ((volume->v_casefold & AFPVOL_MTOULOWER))
837 volume->v_mtou_flags |= CONV_TOLOWER;
839 /* Unix to Mac conversion flags*/
840 volume->v_utom_flags = CONV_IGNORE | CONV_UNESCAPEHEX;
841 if ((volume->v_casefold & AFPVOL_UTOMUPPER))
842 volume->v_utom_flags |= CONV_TOUPPER;
843 else if ((volume->v_casefold & AFPVOL_UTOMLOWER))
844 volume->v_utom_flags |= CONV_TOLOWER;
846 if ((volume->v_flags & AFPVOL_EILSEQ))
847 volume->v_utom_flags |= CONV__EILSEQ;
850 if (options[VOLOPT_FORCEUID].c_value) {
851 volume->v_forceuid = strdup(options[VOLOPT_FORCEUID].c_value);
853 volume->v_forceuid = NULL; /* set as null so as to return 0 later on */
856 if (options[VOLOPT_FORCEGID].c_value) {
857 volume->v_forcegid = strdup(options[VOLOPT_FORCEGID].c_value);
859 volume->v_forcegid = NULL; /* set as null so as to return 0 later on */
863 if (options[VOLOPT_PREEXEC].c_value)
864 volume->v_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_PREEXEC].c_value, pwd, path, name);
865 volume->v_preexec_close = options[VOLOPT_PREEXEC].i_value;
867 if (options[VOLOPT_POSTEXEC].c_value)
868 volume->v_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_POSTEXEC].c_value, pwd, path, name);
870 if (options[VOLOPT_ROOTPREEXEC].c_value)
871 volume->v_root_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPREEXEC].c_value, pwd, path, name);
872 volume->v_root_preexec_close = options[VOLOPT_ROOTPREEXEC].i_value;
874 if (options[VOLOPT_ROOTPOSTEXEC].c_value)
875 volume->v_root_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPOSTEXEC].c_value, pwd, path, name);
878 volume->v_dperm |= volume->v_perm;
879 volume->v_fperm |= volume->v_perm;
881 /* Check EA support on volume */
882 if (volume->v_vfs_ea == AFPVOL_EA_AUTO)
883 check_ea_sys_support(volume);
886 /* get/store uuid from file in afpd master*/
887 if ((parent_or_child == 0) && (volume->v_flags & AFPVOL_TM)) {
888 char *uuid = get_vol_uuid(obj, volume->v_localname);
890 LOG(log_error, logtype_afpd, "Volume '%s': couldn't get UUID",
891 volume->v_localname);
893 volume->v_uuid = uuid;
894 LOG(log_debug, logtype_afpd, "Volume '%s': UUID '%s'",
895 volume->v_localname, volume->v_uuid);
899 volume->v_next = Volumes;
904 /* ---------------- */
905 static char *myfgets( char *buf, int size, FILE *fp)
911 while ((EOF != ( c = getc( fp )) ) && ( size > 1 )) {
912 if ( c == '\n' || c == '\r' ) {
913 if (p != buf && *(p -1) == '\\') {
935 /* check access list. this function wants something of the following
937 * @group,name,name2,@group2,name3
939 * a NULL argument allows everybody to have access.
940 * we return three things:
942 * 0: list exists, but name isn't in it
946 #ifndef NO_REAL_USER_NAME
947 /* authentication is case insensitive
948 * FIXME should we do the same with group name?
950 #define access_strcmp strcasecmp
953 #define access_strcmp strcmp
957 static int accessvol(const char *args, const char *name)
959 char buf[MAXPATHLEN + 1], *p;
965 strlcpy(buf, args, sizeof(buf));
966 if ((p = strtok(buf, ",")) == NULL) /* nothing, return okay */
970 if (*p == '@') { /* it's a group */
971 if ((gr = getgrnam(p + 1)) && gmem(gr->gr_gid))
973 } else if (access_strcmp(p, name) == 0) /* it's a user name */
975 p = strtok(NULL, ",");
981 static int hostaccessvol(int type, const char *volname, const char *args, const AFPObj *obj)
984 char buf[MAXPATHLEN + 1], *p, *b;
985 DSI *dsi = obj->handle;
986 struct sockaddr_storage client;
991 strlcpy(buf, args, sizeof(buf));
992 if ((p = strtok_r(buf, ",", &b)) == NULL) /* nothing, return okay */
995 if (obj->proto != AFPPROTO_DSI)
1000 char *ipaddr, *mask_char;
1001 struct addrinfo hints, *ai;
1003 ipaddr = strtok(p, "/");
1004 mask_char = strtok(NULL,"/");
1006 /* Get address from string with getaddrinfo */
1007 memset(&hints, 0, sizeof hints);
1008 hints.ai_family = AF_UNSPEC;
1009 hints.ai_socktype = SOCK_STREAM;
1010 if ((ret = getaddrinfo(ipaddr, NULL, &hints, &ai)) != 0) {
1011 LOG(log_error, logtype_afpd, "hostaccessvol: getaddrinfo: %s\n", gai_strerror(ret));
1016 if (mask_char != NULL)
1017 mask_int = atoi(mask_char); /* apply_ip_mask does range checking on it */
1019 if (ai->ai_family == AF_INET) /* IPv4 */
1025 /* Apply mask to addresses */
1026 client = dsi->client;
1027 apply_ip_mask((struct sockaddr *)&client, mask_int);
1028 apply_ip_mask(ai->ai_addr, mask_int);
1030 if (compare_ip((struct sockaddr *)&client, ai->ai_addr) == 0) {
1031 if (type == VOLOPT_DENIED_HOSTS)
1032 LOG(log_info, logtype_afpd, "AFP access denied for client IP '%s' to volume '%s' by denied list",
1033 getip_string((struct sockaddr *)&client), volname);
1040 p = strtok_r(NULL, ",", &b);
1043 if (type == VOLOPT_ALLOWED_HOSTS)
1044 LOG(log_info, logtype_afpd, "AFP access denied for client IP '%s' to volume '%s', not in allowed list",
1045 getip_string((struct sockaddr *)&dsi->client), volname);
1049 static void setextmap(char *ext, char *type, char *creator, int user)
1054 if (Extmap == NULL) {
1055 if (( Extmap = calloc(1, sizeof( struct extmap ))) == NULL ) {
1056 LOG(log_error, logtype_afpd, "setextmap: calloc: %s", strerror(errno) );
1061 for ( em = Extmap, cnt = 0; em->em_ext; em++, cnt++) {
1062 if ( (strdiacasecmp( em->em_ext, ext )) == 0 ) {
1067 if ( em->em_ext == NULL ) {
1068 if (!(Extmap = realloc( Extmap, sizeof( struct extmap ) * (cnt +2))) ) {
1069 LOG(log_error, logtype_afpd, "setextmap: realloc: %s", strerror(errno) );
1072 (Extmap +cnt +1)->em_ext = NULL;
1074 } else if ( !user ) {
1080 if (!(em->em_ext = strdup( ext))) {
1081 LOG(log_error, logtype_afpd, "setextmap: strdup: %s", strerror(errno) );
1085 if ( *type == '\0' ) {
1086 memcpy(em->em_type, "\0\0\0\0", sizeof( em->em_type ));
1088 memcpy(em->em_type, type, sizeof( em->em_type ));
1090 if ( *creator == '\0' ) {
1091 memcpy(em->em_creator, "\0\0\0\0", sizeof( em->em_creator ));
1093 memcpy(em->em_creator, creator, sizeof( em->em_creator ));
1097 /* -------------------------- */
1098 static int extmap_cmp(const void *map1, const void *map2)
1100 const struct extmap *em1 = map1;
1101 const struct extmap *em2 = map2;
1102 return strdiacasecmp(em1->em_ext, em2->em_ext);
1105 static void sortextmap( void)
1110 if ((em = Extmap) == NULL) {
1113 while (em->em_ext) {
1118 qsort(Extmap, Extmap_cnt, sizeof(struct extmap), extmap_cmp);
1119 if (*Extmap->em_ext == 0) {
1120 /* the first line is really "." the default entry,
1121 * we remove the leading '.' in setextmap
1128 /* ----------------------
1130 static void free_extmap( void)
1135 for ( em = Extmap; em->em_ext; em++) {
1145 /* ----------------------
1147 static int volfile_changed(struct afp_volume_name *p)
1153 name = p->full_name;
1157 if (!stat( name, &st) && st.st_mtime > p->mtime) {
1158 p->mtime = st.st_mtime;
1164 /* ----------------------
1165 * Read a volume configuration file and add the volumes contained within to
1166 * the global volume list. This gets called from the forked afpd childs.
1167 * The master now reads this too for Zeroconf announcements.
1169 * If p2 is non-NULL, the file that is opened is
1172 * Lines that begin with # and blank lines are ignored.
1173 * Volume lines are of the form:
1174 * <unix path> [<volume name>] [allow:<user>,<@group>,...] \
1175 * [codepage:<file>] [casefold:<num>]
1176 * <extension> TYPE [CREATOR]
1179 static int readvolfile(AFPObj *obj, struct afp_volume_name *p1, char *p2, int user, struct passwd *pwent)
1182 char path[MAXPATHLEN + 1];
1183 char tmp[MAXPATHLEN + 1];
1184 char volname[AFPVOL_U8MNAMELEN + 1];
1186 char type[5], creator[5];
1191 struct vol_option save_options[VOLOPT_NUM];
1192 struct vol_option options[VOLOPT_NUM];
1198 strcpy( path, p1->name );
1200 strcat( path, "/" );
1202 if (p1->full_name) {
1203 free(p1->full_name);
1205 p1->full_name = strdup(path);
1208 if (NULL == ( fp = fopen( path, "r" )) ) {
1212 if (fd != -1 && !fstat( fd, &st) ) {
1213 p1->mtime = st.st_mtime;
1216 /* try putting a read lock on the volume file twice, sleep 1 second if first attempt fails */
1219 if ((read_lock(fd, 0, SEEK_SET, 0)) != 0) {
1222 LOG(log_error, logtype_afpd, "readvolfile: can't lock volume file \"%s\"", path);
1223 if ( fclose( fp ) != 0 ) {
1224 LOG(log_error, logtype_afpd, "readvolfile: fclose: %s", strerror(errno) );
1234 memset(save_options, 0, sizeof(save_options));
1236 /* Enable some default options for all volumes */
1237 save_options[VOLOPT_FLAGS].i_value |= AFPVOL_CACHE;
1239 save_options[VOLOPT_FLAGS].i_value |= AFPVOL_ACLS;
1241 save_options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_AUTO;
1242 LOG(log_maxdebug, logtype_afpd, "readvolfile: seeding default umask: %04o",
1243 obj->options.umask);
1244 save_options[VOLOPT_UMASK].i_value = obj->options.umask;
1246 LOG(log_debug, logtype_afpd, "readvolfile: \"%s\"", path);
1248 while ( myfgets( buf, sizeof( buf ), fp ) != NULL ) {
1249 initline( strlen( buf ), buf );
1250 parseline( sizeof( path ) - 1, path );
1257 /* change the default options for this file */
1258 if (strncmp(path, VOLOPT_DEFAULT, VOLOPT_DEFAULT_LEN) == 0) {
1260 for (i = 0; i < VOLOPT_NUM; i++) {
1261 if (parseline( sizeof( path ) - VOLOPT_DEFAULT_LEN - 1,
1262 path + VOLOPT_DEFAULT_LEN) < 0)
1264 volset(save_options, NULL, tmp, sizeof(tmp) - 1,
1265 path + VOLOPT_DEFAULT_LEN);
1271 if (( p = strchr( path, '/' )) != NULL ) {
1279 if ( u == NULL || *u == '\0' || ( pw = getpwnam( u )) == NULL ) {
1282 strcpy( tmp, pw->pw_dir );
1283 if ( p != NULL && *p != '\0' ) {
1290 /* send path through variable substitution */
1291 if (*path != '~') /* need to copy path to tmp */
1293 if (!pwent && obj->username)
1294 pwent = getpwnam(obj->username);
1296 if (volxlate(obj, path, sizeof(path) - 1, tmp, pwent, NULL, NULL) == NULL)
1299 /* this is sort of braindead. basically, i want to be
1300 * able to specify things in any order, but i don't want to
1301 * re-write everything. */
1303 memcpy(options, save_options, sizeof(options));
1306 /* read in up to VOLOP_NUM possible options */
1307 for (i = 0; i < VOLOPT_NUM; i++) {
1308 if (parseline( sizeof( tmp ) - 1, tmp ) < 0)
1311 volset(options, save_options, volname, sizeof(volname) - 1, tmp);
1314 /* check allow/deny lists (if not afpd master loading volumes for Zeroconf reg.):
1315 allow -> either no list (-1), or in list (1)
1316 deny -> either no list (-1), or not in list (0) */
1317 if (parent_or_child == 0
1319 (accessvol(options[VOLOPT_ALLOW].c_value, obj->username) &&
1320 (accessvol(options[VOLOPT_DENY].c_value, obj->username) < 1) &&
1321 hostaccessvol(VOLOPT_ALLOWED_HOSTS, volname, options[VOLOPT_ALLOWED_HOSTS].c_value, obj) &&
1322 (hostaccessvol(VOLOPT_DENIED_HOSTS, volname, options[VOLOPT_DENIED_HOSTS].c_value, obj) < 1))) {
1324 /* handle read-only behaviour. semantics:
1325 * 1) neither the rolist nor the rwlist exist -> rw
1326 * 2) rolist exists -> ro if user is in it.
1327 * 3) rwlist exists -> ro unless user is in it. */
1328 if (parent_or_child == 1
1330 ((options[VOLOPT_FLAGS].i_value & AFPVOL_RO) == 0)
1332 ((accessvol(options[VOLOPT_ROLIST].c_value, obj->username) == 1) ||
1333 !accessvol(options[VOLOPT_RWLIST].c_value, obj->username)))
1334 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
1336 /* do variable substitution for volname */
1337 if (volxlate(obj, tmp, sizeof(tmp) - 1, volname, pwent, path, NULL) == NULL)
1340 creatvol(obj, pwent, path, tmp, options, p2 != NULL);
1342 volfree(options, save_options);
1346 parseline( sizeof( type ) - 1, type );
1347 parseline( sizeof( creator ) - 1, creator );
1348 setextmap( path, type, creator, user);
1355 volfree(save_options, NULL);
1357 if ( fclose( fp ) != 0 ) {
1358 LOG(log_error, logtype_afpd, "readvolfile: fclose: %s", strerror(errno) );
1364 /* ------------------------------- */
1365 static void volume_free(struct vol *vol)
1367 free(vol->v_localname);
1368 vol->v_localname = NULL;
1369 free(vol->v_u8mname);
1370 vol->v_u8mname = NULL;
1371 free(vol->v_macname);
1372 vol->v_macname = NULL;
1374 free(vol->v_password);
1376 free(vol->v_volcodepage);
1377 free(vol->v_maccodepage);
1378 free(vol->v_cnidscheme);
1379 free(vol->v_dbpath);
1382 free(vol->v_forceuid);
1383 free(vol->v_forcegid);
1384 #endif /* FORCE_UIDGID */
1389 /* ------------------------------- */
1390 static void free_volumes(void )
1393 struct vol *nvol, *ovol;
1395 for ( vol = Volumes; vol; vol = vol->v_next ) {
1396 if (( vol->v_flags & AFPVOL_OPEN ) ) {
1403 for ( vol = Volumes, ovol = NULL; vol; vol = nvol) {
1406 if (vol->v_localname == NULL) {
1407 if (Volumes == vol) {
1412 ovol->v_next = nvol;
1422 /* ------------------------------- */
1423 static void volume_unlink(struct vol *volume)
1425 struct vol *vol, *ovol, *nvol;
1427 if (volume == Volumes) {
1428 Volumes = Volumes->v_next;
1431 for ( vol = Volumes->v_next, ovol = Volumes; vol; vol = nvol) {
1434 if (vol == volume) {
1435 ovol->v_next = nvol;
1444 static off_t getused_size; /* result of getused() */
1447 nftw callback for getused()
1449 static int getused_stat(const char *path,
1450 const struct stat *statp,
1456 if (tflag == FTW_F || tflag == FTW_D) {
1457 getused_size += statp->st_blocks * 512;
1463 #define GETUSED_CACHETIME 5
1465 * Calculate used size of a volume with nftw
1467 * The result is cached, we're try to avoid frequently calling nftw()
1469 * 1) Call nftw an refresh if:
1470 * 1a) - we're called the first time
1471 * 1b) - volume modification date is not yet set and the last time we've been called is
1472 * longer then 30 sec ago
1473 * 1c) - the last volume modification is less then 30 sec old
1475 * @param vol (rw) volume to calculate
1477 static int getused(struct vol *vol)
1479 static time_t vol_mtime = 0;
1481 time_t now = time(NULL);
1484 || (!vol->v_mtime && ((vol_mtime + GETUSED_CACHETIME) < now))
1485 || (vol->v_mtime && ((vol_mtime + GETUSED_CACHETIME) < vol->v_mtime))
1490 ret = nftw(vol->v_path, getused_stat, NULL, 20, FTW_PHYS); /* 2 */
1491 LOG(log_debug, logtype_afpd, "volparams: from nftw: %" PRIu64 " bytes",
1494 getused_size += vol->v_written;
1496 LOG(log_debug, logtype_afpd, "volparams: cached used: %" PRIu64 " bytes",
1503 static int getvolspace(struct vol *vol,
1504 u_int32_t *bfree, u_int32_t *btotal,
1505 VolSpace *xbfree, VolSpace *xbtotal, u_int32_t *bsize)
1510 #ifndef NO_QUOTA_SUPPORT
1511 VolSpace qfree, qtotal;
1514 spaceflag = AFPVOL_GVSMASK & vol->v_flags;
1515 /* report up to 2GB if afp version is < 2.2 (4GB if not) */
1516 maxsize = (vol->v_flags & AFPVOL_A2VOL) ? 0x01fffe00 :
1517 (((afp_version < 22) || (vol->v_flags & AFPVOL_LIMITSIZE))
1518 ? 0x7fffffffL : 0xffffffffL);
1521 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_AFSGVS ) {
1522 if ( afs_getvolspace( vol, xbfree, xbtotal, bsize ) == AFP_OK ) {
1523 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_AFSGVS;
1524 goto getvolspace_done;
1529 if (( rc = ustatfs_getvolspace( vol, xbfree, xbtotal, bsize)) != AFP_OK ) {
1533 #define min(a,b) ((a)<(b)?(a):(b))
1534 #ifndef NO_QUOTA_SUPPORT
1535 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_UQUOTA ) {
1536 if ( uquota_getvolspace( vol, &qfree, &qtotal, *bsize ) == AFP_OK ) {
1537 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_UQUOTA;
1538 *xbfree = min(*xbfree, qfree);
1539 *xbtotal = min( *xbtotal, qtotal);
1540 goto getvolspace_done;
1544 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_USTATFS;
1547 if (vol->v_limitsize) {
1548 if (getused(vol) != 0)
1550 LOG(log_debug, logtype_afpd, "volparams: used on volume: %" PRIu64 " bytes",
1552 vol->v_tm_used = getused_size;
1554 *xbtotal = min(*xbtotal, (vol->v_limitsize * 1024 * 1024));
1555 *xbfree = min(*xbfree, *xbtotal < getused_size ? 0 : *xbtotal - getused_size);
1558 *bfree = min( *xbfree, maxsize);
1559 *btotal = min( *xbtotal, maxsize);
1563 #define FCE_TM_DELTA 10 /* send notification every 10 seconds */
1564 void vol_fce_tm_event(void)
1567 time_t now = time(NULL);
1568 struct vol *vol = Volumes;
1570 if ((last + FCE_TM_DELTA) < now) {
1572 for ( ; vol; vol = vol->v_next ) {
1573 if (vol->v_flags & AFPVOL_TM)
1574 (void)fce_register_tm_size(vol->v_path, vol->v_tm_used + vol->v_written);
1579 /* -----------------------
1580 * set volume creation date
1581 * avoid duplicate, well at least it tries
1583 static void vol_setdate(u_int16_t id, struct adouble *adp, time_t date)
1586 struct vol *vol = Volumes;
1588 for ( volume = Volumes; volume; volume = volume->v_next ) {
1589 if (volume->v_vid == id) {
1592 else if ((time_t)(AD_DATE_FROM_UNIX(date)) == volume->v_ctime) {
1594 volume = Volumes; /* restart */
1597 vol->v_ctime = AD_DATE_FROM_UNIX(date);
1598 ad_setdate(adp, AD_DATE_CREATE | AD_DATE_UNIX, date);
1601 /* ----------------------- */
1602 static int getvolparams( u_int16_t bitmap, struct vol *vol, struct stat *st, char *buf, size_t *buflen)
1605 int bit = 0, isad = 1;
1608 u_int32_t bfree, btotal, bsize;
1609 VolSpace xbfree, xbtotal; /* extended bytes */
1610 char *data, *nameoff = NULL;
1613 LOG(log_debug, logtype_afpd, "getvolparams: Volume '%s'", vol->v_localname);
1615 /* courtesy of jallison@whistle.com:
1616 * For MacOS8.x support we need to create the
1617 * .Parent file here if it doesn't exist. */
1619 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1620 if ( ad_open_metadata( vol->v_path, ADFLAGS_DIR, O_CREAT, &ad) < 0 ) {
1622 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1624 } else if (ad_get_MD_flags( &ad ) & O_CREAT) {
1625 slash = strrchr( vol->v_path, '/' );
1629 slash = vol->v_path;
1630 if (ad_getentryoff(&ad, ADEID_NAME)) {
1631 ad_setentrylen( &ad, ADEID_NAME, strlen( slash ));
1632 memcpy(ad_entry( &ad, ADEID_NAME ), slash,
1633 ad_getentrylen( &ad, ADEID_NAME ));
1635 vol_setdate(vol->v_vid, &ad, st->st_mtime);
1639 if (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0)
1640 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1642 vol->v_ctime = aint;
1645 if (( bitmap & ( (1<<VOLPBIT_BFREE)|(1<<VOLPBIT_BTOTAL) |
1646 (1<<VOLPBIT_XBFREE)|(1<<VOLPBIT_XBTOTAL) |
1647 (1<<VOLPBIT_BSIZE)) ) != 0 ) {
1648 if ( getvolspace( vol, &bfree, &btotal, &xbfree, &xbtotal,
1649 &bsize) != AFP_OK ) {
1651 ad_close( &ad, ADFLAGS_HF );
1653 return( AFPERR_PARAM );
1658 while ( bitmap != 0 ) {
1659 while (( bitmap & 1 ) == 0 ) {
1667 /* check for read-only.
1668 * NOTE: we don't actually set the read-only flag unless
1669 * it's passed in that way as it's possible to mount
1670 * a read-write filesystem under a read-only one. */
1671 if ((vol->v_flags & AFPVOL_RO) ||
1672 ((utime(vol->v_path, NULL) < 0) && (errno == EROFS))) {
1673 ashort |= VOLPBIT_ATTR_RO;
1675 /* prior 2.1 only VOLPBIT_ATTR_RO is defined */
1676 if (afp_version > 20) {
1677 if (0 == (vol->v_flags & AFPVOL_NOFILEID) && vol->v_cdb != NULL &&
1678 (vol->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1679 ashort |= VOLPBIT_ATTR_FILEID;
1681 ashort |= VOLPBIT_ATTR_CATSEARCH;
1683 if (afp_version >= 30) {
1684 ashort |= VOLPBIT_ATTR_UTF8;
1685 if (vol->v_flags & AFPVOL_UNIX_PRIV)
1686 ashort |= VOLPBIT_ATTR_UNIXPRIV;
1687 if (vol->v_flags & AFPVOL_TM)
1688 ashort |= VOLPBIT_ATTR_TM;
1689 if (vol->v_flags & AFPVOL_NONETIDS)
1690 ashort |= VOLPBIT_ATTR_NONETIDS;
1691 if (afp_version >= 32) {
1693 ashort |= VOLPBIT_ATTR_EXT_ATTRS;
1694 if (vol->v_flags & AFPVOL_ACLS)
1695 ashort |= VOLPBIT_ATTR_ACLS;
1699 ashort = htons(ashort);
1700 memcpy(data, &ashort, sizeof( ashort ));
1701 data += sizeof( ashort );
1705 ashort = htons( AFPVOLSIG_DEFAULT );
1706 memcpy(data, &ashort, sizeof( ashort ));
1707 data += sizeof( ashort );
1710 case VOLPBIT_CDATE :
1711 aint = vol->v_ctime;
1712 memcpy(data, &aint, sizeof( aint ));
1713 data += sizeof( aint );
1716 case VOLPBIT_MDATE :
1717 if ( st->st_mtime > vol->v_mtime ) {
1718 vol->v_mtime = st->st_mtime;
1720 aint = AD_DATE_FROM_UNIX(vol->v_mtime);
1721 memcpy(data, &aint, sizeof( aint ));
1722 data += sizeof( aint );
1725 case VOLPBIT_BDATE :
1726 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1727 aint = AD_DATE_START;
1728 memcpy(data, &aint, sizeof( aint ));
1729 data += sizeof( aint );
1733 memcpy(data, &vol->v_vid, sizeof( vol->v_vid ));
1734 data += sizeof( vol->v_vid );
1737 case VOLPBIT_BFREE :
1738 bfree = htonl( bfree );
1739 memcpy(data, &bfree, sizeof( bfree ));
1740 data += sizeof( bfree );
1743 case VOLPBIT_BTOTAL :
1744 btotal = htonl( btotal );
1745 memcpy(data, &btotal, sizeof( btotal ));
1746 data += sizeof( btotal );
1749 #ifndef NO_LARGE_VOL_SUPPORT
1750 case VOLPBIT_XBFREE :
1751 xbfree = hton64( xbfree );
1752 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1753 bcopy(&xbfree, data, sizeof(xbfree));
1754 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1755 memcpy(data, &xbfree, sizeof( xbfree ));
1756 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1757 data += sizeof( xbfree );
1760 case VOLPBIT_XBTOTAL :
1761 xbtotal = hton64( xbtotal );
1762 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1763 bcopy(&xbtotal, data, sizeof(xbtotal));
1764 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1765 memcpy(data, &xbtotal, sizeof( xbtotal ));
1766 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1767 data += sizeof( xbfree );
1769 #endif /* ! NO_LARGE_VOL_SUPPORT */
1773 data += sizeof( u_int16_t );
1776 case VOLPBIT_BSIZE: /* block size */
1777 bsize = htonl(bsize);
1778 memcpy(data, &bsize, sizeof(bsize));
1779 data += sizeof(bsize);
1784 ad_close( &ad, ADFLAGS_HF );
1786 return( AFPERR_BITMAP );
1792 ashort = htons( data - buf );
1793 memcpy(nameoff, &ashort, sizeof( ashort ));
1794 /* name is always in mac charset */
1795 aint = ucs2_to_charset( vol->v_maccharset, vol->v_macname, data+1, AFPVOL_MACNAMELEN + 1);
1805 ad_close_metadata( &ad);
1807 *buflen = data - buf;
1811 /* ------------------------- */
1812 static int stat_vol(u_int16_t bitmap, struct vol *vol, char *rbuf, size_t *rbuflen)
1818 if ( stat( vol->v_path, &st ) < 0 ) {
1820 return( AFPERR_PARAM );
1822 /* save the volume device number */
1823 vol->v_dev = st.st_dev;
1825 buflen = *rbuflen - sizeof( bitmap );
1826 if (( ret = getvolparams( bitmap, vol, &st,
1827 rbuf + sizeof( bitmap ), &buflen )) != AFP_OK ) {
1831 *rbuflen = buflen + sizeof( bitmap );
1832 bitmap = htons( bitmap );
1833 memcpy(rbuf, &bitmap, sizeof( bitmap ));
1838 /* ------------------------------- */
1839 void load_volumes(AFPObj *obj)
1841 struct passwd *pwent;
1846 /* check files date */
1847 if (obj->options.defaultvol.loaded) {
1848 changed = volfile_changed(&obj->options.defaultvol);
1850 if (obj->options.systemvol.loaded) {
1851 changed |= volfile_changed(&obj->options.systemvol);
1853 if (obj->options.uservol.loaded) {
1854 changed |= volfile_changed(&obj->options.uservol);
1863 if (parent_or_child == 0) {
1864 LOG(log_debug, logtype_afpd, "load_volumes: AFP MASTER");
1866 LOG(log_debug, logtype_afpd, "load_volumes: user: %s", obj->username);
1869 pwent = getpwnam(obj->username);
1870 if ( (obj->options.flags & OPTION_USERVOLFIRST) == 0 ) {
1871 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent);
1874 if ((*obj->username == '\0') || (obj->options.flags & OPTION_NOUSERVOL)) {
1875 readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent);
1878 * Read user's AppleVolumes or .AppleVolumes file
1879 * If neither are readable, read the default volumes file. if
1880 * that doesn't work, create a user share.
1882 if (obj->options.uservol.name) {
1883 free(obj->options.uservol.name);
1885 obj->options.uservol.name = strdup(pwent->pw_dir);
1886 if ( readvolfile(obj, &obj->options.uservol, "AppleVolumes", 1, pwent) < 0 &&
1887 readvolfile(obj, &obj->options.uservol, ".AppleVolumes", 1, pwent) < 0 &&
1888 readvolfile(obj, &obj->options.uservol, "applevolumes", 1, pwent) < 0 &&
1889 readvolfile(obj, &obj->options.uservol, ".applevolumes", 1, pwent) < 0 &&
1890 obj->options.defaultvol.name != NULL ) {
1891 if (readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent) < 0)
1892 creatvol(obj, pwent, pwent->pw_dir, NULL, NULL, 1);
1895 if ( obj->options.flags & OPTION_USERVOLFIRST ) {
1896 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent );
1899 if ( obj->options.closevol ) {
1902 for ( vol = Volumes; vol; vol = vol->v_next ) {
1903 if (vol->v_deleted && !vol->v_new ) {
1912 /* ------------------------------- */
1913 int afp_getsrvrparms(AFPObj *obj, char *ibuf _U_, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
1926 for ( vcnt = 0, volume = Volumes; volume; volume = volume->v_next ) {
1927 if (!(volume->v_flags & AFPVOL_NOSTAT)) {
1930 if ( stat( volume->v_path, &st ) < 0 ) {
1931 LOG(log_info, logtype_afpd, "afp_getsrvrparms(%s): stat: %s",
1932 volume->v_path, strerror(errno) );
1933 continue; /* can't access directory */
1935 if (!S_ISDIR(st.st_mode)) {
1936 continue; /* not a dir */
1938 accessmode(volume->v_path, &ma, NULL, &st);
1939 if ((ma.ma_user & (AR_UREAD | AR_USEARCH)) != (AR_UREAD | AR_USEARCH)) {
1940 continue; /* no r-x access */
1943 if (volume->v_hide) {
1944 continue; /* config file changed but the volume was mounted */
1947 if (utf8_encoding()) {
1948 len = ucs2_to_charset_allocate(CH_UTF8_MAC, &namebuf, volume->v_u8mname);
1950 len = ucs2_to_charset_allocate(obj->options.maccharset, &namebuf, volume->v_macname);
1953 if (len == (size_t)-1)
1956 /* set password bit if there's a volume password */
1957 *data = (volume->v_password) ? AFPSRVR_PASSWD : 0;
1959 /* Apple 2 clients running ProDOS-8 expect one volume to have
1960 bit 0 of this byte set. They will not recognize anything
1961 on the server unless this is the case. I have not
1962 completely worked this out, but it's related to booting
1963 from the server. Support for that function is a ways
1964 off.. <shirsch@ibm.net> */
1965 *data |= (volume->v_flags & AFPVOL_A2VOL) ? AFPSRVR_CONFIGINFO : 0;
1966 *data++ |= 0; /* UNIX PRIVS BIT ..., OSX doesn't seem to use it, so we don't either */
1968 memcpy(data, namebuf, len );
1974 *rbuflen = data - rbuf;
1976 if ( gettimeofday( &tv, NULL ) < 0 ) {
1977 LOG(log_error, logtype_afpd, "afp_getsrvrparms(%s): gettimeofday: %s", volume->v_path, strerror(errno) );
1979 return AFPERR_PARAM;
1981 tv.tv_sec = AD_DATE_FROM_UNIX(tv.tv_sec);
1982 memcpy(data, &tv.tv_sec, sizeof( u_int32_t));
1983 data += sizeof( u_int32_t);
1988 /* ------------------------- */
1989 static int volume_codepage(AFPObj *obj, struct vol *volume)
1991 struct charset_functions *charset;
1994 if (!volume->v_volcodepage)
1995 volume->v_volcodepage = strdup("UTF8");
1997 if ( (charset_t) -1 == ( volume->v_volcharset = add_charset(volume->v_volcodepage)) ) {
1998 LOG (log_error, logtype_afpd, "Setting codepage %s as volume codepage failed", volume->v_volcodepage);
2002 if ( NULL == (charset = find_charset_functions(volume->v_volcodepage)) || charset->flags & CHARSET_ICONV ) {
2003 LOG (log_warning, logtype_afpd, "WARNING: volume encoding %s is *not* supported by netatalk, expect problems !!!!", volume->v_volcodepage);
2006 if (!volume->v_maccodepage)
2007 volume->v_maccodepage = strdup(obj->options.maccodepage);
2009 if ( (charset_t) -1 == ( volume->v_maccharset = add_charset(volume->v_maccodepage)) ) {
2010 LOG (log_error, logtype_afpd, "Setting codepage %s as mac codepage failed", volume->v_maccodepage);
2014 if ( NULL == ( charset = find_charset_functions(volume->v_maccodepage)) || ! (charset->flags & CHARSET_CLIENT) ) {
2015 LOG (log_error, logtype_afpd, "Fatal error: mac charset %s not supported", volume->v_maccodepage);
2018 volume->v_kTextEncoding = htonl(charset->kTextEncoding);
2022 /* ------------------------- */
2023 static int volume_openDB(struct vol *volume)
2027 if ((volume->v_flags & AFPVOL_NODEV)) {
2028 flags |= CNID_FLAG_NODEV;
2031 if (volume->v_cnidscheme == NULL) {
2032 volume->v_cnidscheme = strdup(DEFAULT_CNID_SCHEME);
2033 LOG(log_info, logtype_afpd, "Volume %s use CNID scheme %s.",
2034 volume->v_path, volume->v_cnidscheme);
2037 LOG(log_info, logtype_afpd, "CNID server: %s:%s",
2038 volume->v_cnidserver ? volume->v_cnidserver : Cnid_srv,
2039 volume->v_cnidport ? volume->v_cnidport : Cnid_port);
2042 /* Found this in branch dir-rewrite, maybe we want to use it sometimes */
2044 /* Legacy pre 2.1 way of sharing eg CD-ROM */
2045 if (strcmp(volume->v_cnidscheme, "last") == 0) {
2046 /* "last" is gone. We support it by switching to in-memory "tdb" */
2047 volume->v_cnidscheme = strdup("tdb");
2048 flags |= CNID_FLAG_MEMORY;
2051 /* New way of sharing CD-ROM */
2052 if (volume->v_flags & AFPVOL_CDROM) {
2053 flags |= CNID_FLAG_MEMORY;
2054 if (strcmp(volume->v_cnidscheme, "tdb") != 0) {
2055 free(volume->v_cnidscheme);
2056 volume->v_cnidscheme = strdup("tdb");
2057 LOG(log_info, logtype_afpd, "Volume %s is ejectable, switching to scheme %s.",
2058 volume->v_path, volume->v_cnidscheme);
2063 volume->v_cdb = cnid_open(volume->v_path,
2065 volume->v_cnidscheme,
2067 volume->v_cnidserver ? volume->v_cnidserver : Cnid_srv,
2068 volume->v_cnidport ? volume->v_cnidport : Cnid_port);
2070 if ( ! volume->v_cdb && ! (flags & CNID_FLAG_MEMORY)) {
2071 /* The first attempt failed and it wasn't yet an attempt to open in-memory */
2072 LOG(log_error, logtype_afpd, "Can't open volume \"%s\" CNID backend \"%s\" ",
2073 volume->v_path, volume->v_cnidscheme);
2074 LOG(log_error, logtype_afpd, "Reopen volume %s using in memory temporary CNID DB.",
2076 flags |= CNID_FLAG_MEMORY;
2077 volume->v_cdb = cnid_open (volume->v_path, volume->v_umask, "tdb", flags, NULL, NULL);
2079 /* kill ourself with SIGUSR2 aka msg pending */
2080 if (volume->v_cdb) {
2081 setmessage("Something wrong with the volume's CNID DB, using temporary CNID DB instead."
2082 "Check server messages for details!");
2083 kill(getpid(), SIGUSR2);
2084 /* deactivate cnid caching/storing in AppleDouble files */
2085 volume->v_flags &= ~AFPVOL_CACHE;
2090 return (!volume->v_cdb)?-1:0;
2094 Check if the underlying filesystem supports EAs for ea:sys volumes.
2095 If not, switch to ea:ad.
2096 As we can't check (requires write access) on ro-volumes, we switch ea:auto
2097 volumes that are options:ro to ea:none.
2099 static void check_ea_sys_support(struct vol *vol)
2101 uid_t process_uid = 0;
2102 char eaname[] = {"org.netatalk.supports-eas.XXXXXX"};
2103 const char *eacontent = "yes";
2105 if (vol->v_vfs_ea == AFPVOL_EA_AUTO) {
2107 if ((vol->v_flags & AFPVOL_RO) == AFPVOL_RO) {
2108 LOG(log_info, logtype_afpd, "read-only volume '%s', can't test for EA support, disabling EAs", vol->v_localname);
2109 vol->v_vfs_ea = AFPVOL_EA_NONE;
2115 process_uid = geteuid();
2117 if (seteuid(0) == -1) {
2118 LOG(log_error, logtype_afpd, "check_ea_sys_support: can't seteuid(0): %s", strerror(errno));
2122 if ((sys_setxattr(vol->v_path, eaname, eacontent, 4, 0)) == 0) {
2123 sys_removexattr(vol->v_path, eaname);
2124 vol->v_vfs_ea = AFPVOL_EA_SYS;
2126 LOG(log_warning, logtype_afpd, "volume \"%s\" does not support Extended Attributes, using ea:ad instead",
2128 vol->v_vfs_ea = AFPVOL_EA_AD;
2132 if (seteuid(process_uid) == -1) {
2133 LOG(log_error, logtype_afpd, "can't seteuid back %s", strerror(errno));
2140 /* -------------------------
2141 * we are the user here
2143 int afp_openvol(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2154 char path[ MAXPATHLEN + 1];
2160 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2161 bitmap = ntohs( bitmap );
2162 ibuf += sizeof( bitmap );
2165 if (( bitmap & (1<<VOLPBIT_VID)) == 0 ) {
2166 return AFPERR_BITMAP;
2169 len = (unsigned char)*ibuf++;
2170 volname = obj->oldtmp;
2172 if ((volname_tmp = strchr(volname,'+')) != NULL)
2173 volname = volname_tmp+1;
2175 if (utf8_encoding()) {
2176 namelen = convert_string(CH_UTF8_MAC, CH_UCS2, ibuf, len, volname, sizeof(obj->oldtmp));
2178 namelen = convert_string(obj->options.maccharset, CH_UCS2, ibuf, len, volname, sizeof(obj->oldtmp));
2181 if ( namelen <= 0) {
2182 return AFPERR_PARAM;
2186 if ((len + 1) & 1) /* pad to an even boundary */
2191 for ( volume = Volumes; volume; volume = volume->v_next ) {
2192 if ( strcasecmp_w( (ucs2_t*) volname, volume->v_name ) == 0 ) {
2197 if ( volume == NULL ) {
2198 return AFPERR_PARAM;
2201 /* check for a volume password */
2202 if (volume->v_password && strncmp(ibuf, volume->v_password, VOLPASSLEN)) {
2203 return AFPERR_ACCESS;
2206 if (( volume->v_flags & AFPVOL_OPEN ) ) {
2207 /* the volume is already open */
2209 set_uidgid ( volume );
2211 return stat_vol(bitmap, volume, rbuf, rbuflen);
2214 if (volume->v_root_preexec) {
2215 if ((ret = afprun(1, volume->v_root_preexec, NULL)) && volume->v_root_preexec_close) {
2216 LOG(log_error, logtype_afpd, "afp_openvol(%s): root preexec : %d", volume->v_path, ret );
2222 set_uidgid ( volume );
2225 if (volume->v_preexec) {
2226 if ((ret = afprun(0, volume->v_preexec, NULL)) && volume->v_preexec_close) {
2227 LOG(log_error, logtype_afpd, "afp_openvol(%s): preexec : %d", volume->v_path, ret );
2232 if ( stat( volume->v_path, &st ) < 0 ) {
2233 return AFPERR_PARAM;
2236 if ( chdir( volume->v_path ) < 0 ) {
2237 return AFPERR_PARAM;
2240 if ( NULL == getcwd(path, MAXPATHLEN)) {
2241 /* shouldn't be fatal but it will fail later */
2242 LOG(log_error, logtype_afpd, "afp_openvol(%s): volume pathlen too long", volume->v_path);
2246 /* Normalize volume path */
2247 #ifdef REALPATH_TAKES_NULL
2248 if ((volume->v_path = realpath(path, NULL)) == NULL)
2251 if ((volume->v_path = malloc(MAXPATHLEN+1)) == NULL)
2253 if (realpath(path, volume->v_path) == NULL) {
2254 free(volume->v_path);
2257 /* Safe some memory */
2259 if ((tmp = strdup(volume->v_path)) == NULL) {
2260 free(volume->v_path);
2263 free(volume->v_path);
2264 volume->v_path = tmp;
2267 if (volume_codepage(obj, volume) < 0) {
2272 /* initialize volume variables
2275 if (utf8_encoding()) {
2276 volume->max_filename = UTF8FILELEN_EARLY;
2279 volume->max_filename = MACFILELEN;
2282 volume->v_flags |= AFPVOL_OPEN;
2283 volume->v_cdb = NULL;
2285 if (utf8_encoding()) {
2286 len = convert_string_allocate(CH_UCS2, CH_UTF8_MAC, volume->v_u8mname, namelen, &vol_mname);
2288 len = convert_string_allocate(CH_UCS2, obj->options.maccharset, volume->v_macname, namelen, &vol_mname);
2290 if ( !vol_mname || len <= 0) {
2295 if ((vol_uname = strrchr(path, '/')) == NULL)
2297 else if (*(vol_uname + 1) != '\0')
2300 if ((dir = dir_new(vol_mname,
2305 bfromcstr(volume->v_path),
2309 LOG(log_error, logtype_afpd, "afp_openvol(%s): malloc: %s", volume->v_path, strerror(errno) );
2314 volume->v_root = dir;
2317 if (volume_openDB(volume) < 0) {
2318 LOG(log_error, logtype_afpd, "Fatal error: cannot open CNID or invalid CNID backend for %s: %s",
2319 volume->v_path, volume->v_cnidscheme);
2324 ret = stat_vol(bitmap, volume, rbuf, rbuflen);
2326 if (ret == AFP_OK) {
2327 handle_special_folders(volume);
2329 volume->v_cnidserver ? volume->v_cnidserver : Cnid_srv,
2330 volume->v_cnidport ? volume->v_cnidport : Cnid_port);
2334 * If you mount a volume twice, the second time the trash appears on
2335 * the desk-top. That's because the Mac remembers the DID for the
2336 * trash (even for volumes in different zones, on different servers).
2337 * Just so this works better, we prime the DID cache with the trash,
2338 * fixing the trash at DID 17.
2339 * FIXME (RL): should it be done inside a CNID backend ? (always returning Trash DID when asked) ?
2341 if ((volume->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
2343 /* FIXME find db time stamp */
2344 if (cnid_getstamp(volume->v_cdb, volume->v_stamp, sizeof(volume->v_stamp)) < 0) {
2345 LOG (log_error, logtype_afpd,
2346 "afp_openvol(%s): Fatal error: Unable to get stamp value from CNID backend",
2354 cname( volume, volume->v_root, &p );
2360 if (volume->v_root) {
2361 dir_free( volume->v_root );
2362 volume->v_root = NULL;
2365 volume->v_flags &= ~AFPVOL_OPEN;
2366 if (volume->v_cdb != NULL) {
2367 cnid_close(volume->v_cdb);
2368 volume->v_cdb = NULL;
2374 /* ------------------------- */
2375 static void closevol(struct vol *vol)
2380 dir_free( vol->v_root );
2382 if (vol->v_cdb != NULL) {
2383 cnid_close(vol->v_cdb);
2387 if (vol->v_postexec) {
2388 afprun(0, vol->v_postexec, NULL);
2390 if (vol->v_root_postexec) {
2391 afprun(1, vol->v_root_postexec, NULL);
2395 /* ------------------------- */
2396 void close_all_vol(void)
2400 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
2401 if ( (ovol->v_flags & AFPVOL_OPEN) ) {
2402 ovol->v_flags &= ~AFPVOL_OPEN;
2408 /* ------------------------- */
2409 static void deletevol(struct vol *vol)
2413 vol->v_flags &= ~AFPVOL_OPEN;
2414 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
2415 if ( (ovol->v_flags & AFPVOL_OPEN) ) {
2419 if ( ovol != NULL ) {
2420 /* Even if chdir fails, we can't say afp_closevol fails. */
2421 if ( chdir( ovol->v_path ) == 0 ) {
2422 curdir = ovol->v_root;
2427 if (vol->v_deleted) {
2428 showvol(vol->v_name);
2435 /* ------------------------- */
2436 int afp_closevol(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2443 memcpy(&vid, ibuf, sizeof( vid ));
2444 if (NULL == ( vol = getvolbyvid( vid )) ) {
2445 return( AFPERR_PARAM );
2454 /* ------------------------- */
2455 struct vol *getvolbyvid(const u_int16_t vid )
2459 for ( vol = Volumes; vol; vol = vol->v_next ) {
2460 if ( vid == vol->v_vid ) {
2464 if ( vol == NULL || ( vol->v_flags & AFPVOL_OPEN ) == 0 ) {
2470 #endif /* FORCE_UIDGID */
2477 /* ------------------------ */
2478 static int ext_cmp_key(const void *key, const void *obj)
2480 const char *p = key;
2481 const struct extmap *em = obj;
2482 return strdiacasecmp(p, em->em_ext);
2484 struct extmap *getextmap(const char *path)
2489 if (!Extmap_cnt || NULL == ( p = strrchr( path, '.' )) ) {
2490 return( Defextmap );
2494 return( Defextmap );
2496 em = bsearch(p, Extmap, Extmap_cnt, sizeof(struct extmap), ext_cmp_key);
2500 return( Defextmap );
2504 /* ------------------------- */
2505 struct extmap *getdefextmap(void)
2507 return( Defextmap );
2510 /* --------------------------
2511 poll if a volume is changed by other processes.
2513 0 no attention msg sent
2514 1 attention msg sent
2515 -1 error (socket closed)
2517 Note: if attention return -1 no packet has been
2518 sent because the buffer is full, we don't care
2519 either there's no reader or there's a lot of
2520 traffic and another pollvoltime will follow
2522 int pollvoltime(AFPObj *obj)
2529 if (!(afp_version > 21 && obj->options.server_notif))
2532 if ( gettimeofday( &tv, NULL ) < 0 )
2535 for ( vol = Volumes; vol; vol = vol->v_next ) {
2536 if ( (vol->v_flags & AFPVOL_OPEN) && vol->v_mtime + 30 < tv.tv_sec) {
2537 if ( !stat( vol->v_path, &st ) && vol->v_mtime != st.st_mtime ) {
2538 vol->v_mtime = st.st_mtime;
2539 if (!obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED))
2548 /* ------------------------- */
2549 void setvoltime(AFPObj *obj, struct vol *vol)
2553 if ( gettimeofday( &tv, NULL ) < 0 ) {
2554 LOG(log_error, logtype_afpd, "setvoltime(%s): gettimeofday: %s", vol->v_path, strerror(errno) );
2557 if( utime( vol->v_path, NULL ) < 0 ) {
2558 /* write of time failed ... probably a read only filesys,
2559 * where no other users can interfere, so there's no issue here
2563 /* a little granularity */
2564 if (vol->v_mtime < tv.tv_sec) {
2565 vol->v_mtime = tv.tv_sec;
2566 /* or finder doesn't update free space
2567 * AFP 3.2 and above clients seem to be ok without so many notification
2569 if (afp_version < 32 && obj->options.server_notif) {
2570 obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED);
2575 /* ------------------------- */
2576 int afp_getvolparams(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_,char *rbuf, size_t *rbuflen)
2579 u_int16_t vid, bitmap;
2582 memcpy(&vid, ibuf, sizeof( vid ));
2583 ibuf += sizeof( vid );
2584 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2585 bitmap = ntohs( bitmap );
2587 if (NULL == ( vol = getvolbyvid( vid )) ) {
2589 return( AFPERR_PARAM );
2592 return stat_vol(bitmap, vol, rbuf, rbuflen);
2595 /* ------------------------- */
2596 int afp_setvolparams(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2600 u_int16_t vid, bitmap;
2606 memcpy(&vid, ibuf, sizeof( vid ));
2607 ibuf += sizeof( vid );
2608 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2609 bitmap = ntohs( bitmap );
2610 ibuf += sizeof(bitmap);
2612 if (( vol = getvolbyvid( vid )) == NULL ) {
2613 return( AFPERR_PARAM );
2616 if ((vol->v_flags & AFPVOL_RO))
2617 return AFPERR_VLOCK;
2619 /* we can only set the backup date. */
2620 if (bitmap != (1 << VOLPBIT_BDATE))
2621 return AFPERR_BITMAP;
2623 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2624 if ( ad_open( vol->v_path, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR,
2627 return AFPERR_VLOCK;
2629 return AFPERR_ACCESS;
2632 memcpy(&aint, ibuf, sizeof(aint));
2633 ad_setdate(&ad, AD_DATE_BACKUP, aint);
2635 ad_close(&ad, ADFLAGS_HF);
2639 /* ------------------------- */
2640 int wincheck(const struct vol *vol, const char *path)
2644 if (!(vol->v_flags & AFPVOL_MSWINDOWS))
2647 /* empty paths are not allowed */
2648 if ((len = strlen(path)) == 0)
2651 /* leading or trailing whitespaces are not allowed, carriage returns
2652 * and probably other whitespace is okay, tabs are not allowed
2654 if ((path[0] == ' ') || (path[len-1] == ' '))
2657 /* certain characters are not allowed */
2658 if (strpbrk(path, MSWINDOWS_BADCHARS))
2661 /* everything else is okay */
2667 * precreate a folder
2668 * this is only intended for folders in the volume root
2669 * It will *not* work if the folder name contains extended characters
2671 static int create_special_folder (const struct vol *vol, const struct _special_folder *folder)
2680 p = (char *) malloc ( strlen(vol->v_path)+strlen(folder->name)+2);
2682 LOG(log_error, logtype_afpd,"malloc failed");
2686 q=strdup(folder->name);
2688 LOG(log_error, logtype_afpd,"malloc failed");
2692 strcpy(p, vol->v_path);
2697 if ((vol->v_casefold & AFPVOL_MTOUUPPER))
2699 else if ((vol->v_casefold & AFPVOL_MTOULOWER))
2705 if ( (ret = stat( p, &st )) < 0 ) {
2706 if (folder->precreate) {
2707 if (ad_mkdir(p, folder->mode)) {
2708 LOG(log_debug, logtype_afpd,"Creating '%s' failed in %s: %s", p, vol->v_path, strerror(errno));
2717 if ( !ret && folder->hide) {
2719 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2720 if (ad_open_metadata( p, ADFLAGS_DIR, O_CREAT, &ad) < 0) {
2726 ad_setname(&ad, folder->name);
2728 ad_getattr(&ad, &attr);
2729 attr |= htons( ntohs( attr ) | ATTRBIT_INVISIBLE );
2730 ad_setattr(&ad, attr);
2732 /* do the same with the finder info */
2733 if (ad_entry(&ad, ADEID_FINDERI)) {
2734 memcpy(&attr, ad_entry(&ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF, sizeof(attr));
2735 attr |= htons(FINDERINFO_INVISIBLE);
2736 memcpy(ad_entry(&ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF,&attr, sizeof(attr));
2740 ad_close_metadata( &ad);
2747 static void handle_special_folders (const struct vol * vol)
2749 const _special_folder *p = &special_folders[0];
2752 process_uid = geteuid();
2754 if (seteuid(0) == -1) {
2759 for (; p->name != NULL; p++) {
2760 create_special_folder (vol, p);
2764 if (seteuid(process_uid) == -1) {
2765 LOG(log_error, logtype_logger, "can't seteuid back %s", strerror(errno));
2771 const struct vol *getvolumes(void)
2776 void unload_volumes_and_extmap(void)
2778 LOG(log_debug, logtype_afpd, "unload_volumes_and_extmap");
2784 * Get a volumes UUID from the config file.
2785 * If there is none, it is generated and stored there.
2787 * Returns pointer to allocated storage on success, NULL on error.
2789 static char *get_vol_uuid(const AFPObj *obj, const char *volname)
2792 char buf[1024], uuid[UUID_PRINTABLE_STRING_LENGTH], *p;
2794 struct stat tmpstat;
2797 if ((fp = fopen(obj->options.uuidconf, "r")) != NULL) { /* read open? */
2798 /* scan in the conf file */
2799 while (fgets(buf, sizeof(buf), fp) != NULL) {
2801 while (p && isblank(*p))
2803 if (!p || (*p == '#') || (*p == '\n'))
2804 continue; /* invalid line */
2807 if ((volname_conf = strtok( p, "\"" )) == NULL)
2808 continue; /* syntax error */
2810 if ((volname_conf = strtok( p, " \t" )) == NULL)
2811 continue; /* syntax error: invalid name */
2813 p = strchr(p, '\0');
2816 continue; /* syntax error */
2818 if (strcmp(volname, volname_conf) != 0)
2819 continue; /* another volume name */
2821 while (p && isblank(*p))
2824 if (sscanf(p, "%36s", uuid) == 1 ) {
2825 for (int i=0; uuid[i]; i++)
2826 uuid[i] = toupper(uuid[i]);
2827 LOG(log_debug, logtype_afpd, "get_uuid('%s'): UUID: '%s'", volname, uuid);
2829 return strdup(uuid);
2837 /* not found or no file, reopen in append mode */
2839 if (stat(obj->options.uuidconf, &tmpstat)) { /* no file */
2840 if (( fd = creat(obj->options.uuidconf, 0644 )) < 0 ) {
2841 LOG(log_error, logtype_afpd, "ERROR: Cannot create %s (%s).",
2842 obj->options.uuidconf, strerror(errno));
2845 if (( fp = fdopen( fd, "w" )) == NULL ) {
2846 LOG(log_error, logtype_afpd, "ERROR: Cannot fdopen %s (%s).",
2847 obj->options.uuidconf, strerror(errno));
2851 } else if ((fp = fopen(obj->options.uuidconf, "a+")) == NULL) { /* not found */
2852 LOG(log_error, logtype_afpd, "Cannot create or append to %s (%s).",
2853 obj->options.uuidconf, strerror(errno));
2856 fseek(fp, 0L, SEEK_END);
2857 if(ftell(fp) == 0) { /* size = 0 */
2858 fprintf(fp, "# DON'T TOUCH NOR COPY THOUGHTLESSLY!\n");
2859 fprintf(fp, "# This file is auto-generated by afpd\n");
2860 fprintf(fp, "# and stores UUIDs for TM volumes.\n\n");
2862 fseek(fp, -1L, SEEK_END);
2863 if(fgetc(fp) != '\n') fputc('\n', fp); /* last char is \n? */
2866 /* generate uuid and write to file */
2869 randombytes((void *)id, 16);
2870 cp = uuid_bin2string(id);
2872 LOG(log_debug, logtype_afpd, "get_uuid('%s'): generated UUID '%s'", volname, cp);
2874 fprintf(fp, "\"%s\"\t%36s\n", volname, cp);