2 * Copyright (c) 1990,1993 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
8 #endif /* HAVE_CONFIG_H */
23 #else /* STDC_HEADERS */
27 #endif /* HAVE_STRCHR */
28 char *strchr (), *strrchr ();
30 #define memcpy(d,s,n) bcopy ((s), (d), (n))
31 #define memmove(d,s,n) bcopy ((s), (d), (n))
32 #endif /* ! HAVE_MEMCPY */
33 #endif /* STDC_HEADERS */
34 #include <sys/param.h>
35 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
39 #include <atalk/asp.h>
40 #include <atalk/dsi.h>
41 #include <atalk/adouble.h>
42 #include <atalk/afp.h>
43 #include <atalk/util.h>
44 #include <atalk/volinfo.h>
45 #include <atalk/logger.h>
46 #include <atalk/vfs.h>
47 #include <atalk/uuid.h>
48 #include <atalk/bstrlib.h>
49 #include <atalk/bstradd.h>
53 #include <atalk/cnid.h>
57 #include "directory.h"
66 extern int afprun(int root, char *cmd, int *outfd);
69 #define MIN(a, b) ((a) < (b) ? (a) : (b))
72 #ifndef NO_LARGE_VOL_SUPPORT
73 #if BYTE_ORDER == BIG_ENDIAN
76 #else /* BYTE_ORDER == BIG_ENDIAN */
77 #define hton64(x) ((u_int64_t) (htonl(((x) >> 32) & 0xffffffffLL)) | \
78 (u_int64_t) ((htonl(x) & 0xffffffffLL) << 32))
79 #define ntoh64(x) (hton64(x))
80 #endif /* BYTE_ORDER == BIG_ENDIAN */
81 #endif /* ! NO_LARGE_VOL_SUPPORT */
83 #ifndef UUID_PRINTABLE_STRING_LENGTH
84 #define UUID_PRINTABLE_STRING_LENGTH 37
88 struct vol *current_vol; /* last volume from getvolbyvid() */
90 static struct vol *Volumes = NULL;
91 static u_int16_t lastvid = 0;
92 static char *Trash = "\02\024Network Trash Folder";
94 static struct extmap *Extmap = NULL, *Defextmap = NULL;
95 static int Extmap_cnt;
96 static void free_extmap(void);
98 #define VOLOPT_ALLOW 0 /* user allow list */
99 #define VOLOPT_DENY 1 /* user deny list */
100 #define VOLOPT_RWLIST 2 /* user rw list */
101 #define VOLOPT_ROLIST 3 /* user ro list */
102 #define VOLOPT_PASSWORD 4 /* volume password */
103 #define VOLOPT_CASEFOLD 5 /* character case mangling */
104 #define VOLOPT_FLAGS 6 /* various flags */
105 #define VOLOPT_DBPATH 7 /* path to database */
106 #define VOLOPT_LIMITSIZE 8 /* Limit the size of the volume */
108 #define VOLOPT_VETO 10 /* list of veto filespec */
109 #define VOLOPT_PREEXEC 11 /* preexec command */
110 #define VOLOPT_ROOTPREEXEC 12 /* root preexec command */
111 #define VOLOPT_POSTEXEC 13 /* postexec command */
112 #define VOLOPT_ROOTPOSTEXEC 14 /* root postexec command */
113 #define VOLOPT_ENCODING 15 /* mac encoding (pre OSX)*/
114 #define VOLOPT_MACCHARSET 16
115 #define VOLOPT_CNIDSCHEME 17
116 #define VOLOPT_ADOUBLE 18 /* adouble version */
122 #define VOLOPT_FORCEUID 19 /* force uid for username x */
123 #define VOLOPT_FORCEGID 20 /* force gid for group x */
124 #endif /* FORCE_UIDGID */
126 #define VOLOPT_UMASK 21
127 #define VOLOPT_ALLOWED_HOSTS 22
128 #define VOLOPT_DENIED_HOSTS 23
129 #define VOLOPT_DPERM 24 /* dperm default directories perms */
130 #define VOLOPT_FPERM 25 /* fperm default files perms */
131 #define VOLOPT_DFLTPERM 26 /* perm */
132 #define VOLOPT_EA_VFS 27 /* Extended Attributes vfs indirection */
133 #define VOLOPT_CNIDSERVER 28 /* CNID Server ip address*/
134 #define VOLOPT_CNIDPORT 30 /* CNID server tcp port */
136 #define VOLOPT_MAX 31 /* <== IMPORTANT !!!!!! */
137 #define VOLOPT_NUM (VOLOPT_MAX + 1)
140 #define VOLOPT_DEFAULT ":DEFAULT:"
141 #define VOLOPT_DEFAULT_LEN 9
148 typedef struct _special_folder {
155 static const _special_folder special_folders[] = {
156 {"Network Trash Folder", 1, 0777, 1},
157 {"Temporary Items", 1, 0777, 1},
158 {".AppleDesktop", 1, 0777, 0},
160 {"TheFindByContentFolder", 0, 0, 1},
161 {"TheVolumeSettingsFolder", 0, 0, 1},
165 /* Forward declarations */
166 static void handle_special_folders (const struct vol *);
167 static void deletevol(struct vol *vol);
168 static void volume_free(struct vol *vol);
169 static void check_ea_sys_support(struct vol *vol);
171 static void volfree(struct vol_option *options, const struct vol_option *save)
176 for (i = 0; i < VOLOPT_MAX; i++) {
177 if (options[i].c_value && (options[i].c_value != save[i].c_value))
178 free(options[i].c_value);
181 for (i = 0; i < VOLOPT_MAX; i++) {
182 if (options[i].c_value)
183 free(options[i].c_value);
189 #define is_var(a, b) (strncmp((a), (b), 2) == 0)
192 * Handle variable substitutions. here's what we understand:
193 * $b -> basename of path
194 * $c -> client ip/appletalk address
195 * $d -> volume pathname on server
196 * $f -> full name (whatever's in the gecos field)
199 * $i -> client ip/appletalk address without port
200 * $s -> server name (hostname if it doesn't exist)
201 * $u -> username (guest is usually nobody)
202 * $v -> volume name or basename if null
203 * $z -> zone (may not exist)
206 * This get's called from readvolfile with
207 * path = NULL, volname = NULL for xlating the volumes path
208 * path = path, volname = NULL for xlating the volumes name
209 * ... and from volumes options parsing code when xlating eg dbpath with
210 * path = path, volname = volname
212 * Using this information we can reject xlation of any variable depeninding on a login
213 * context which is not given in the afp master, where we must evaluate this whole stuff
214 * too for the Zeroconf announcements.
216 static char *volxlate(AFPObj *obj,
229 int xlatevolname = 0;
231 if (parent_or_child == 0)
234 if (path && !volname)
242 dest = calloc(destlen +1, 1);
248 strlcpy(dest, src, destlen +1);
249 if ((p = strchr(src, '$')) == NULL) /* nothing to do */
252 /* first part of the path. just forward to the next variable. */
253 len = MIN((size_t)(p - src), destlen);
259 while (p && destlen > 0) {
260 /* now figure out what the variable is */
262 if (is_var(p, "$b")) {
263 if (afpmaster && xlatevolname)
266 if ((q = strrchr(path, '/')) == NULL)
268 else if (*(q + 1) != '\0')
271 } else if (is_var(p, "$c")) {
272 if (afpmaster && xlatevolname)
274 if (obj->proto == AFPPROTO_ASP) {
275 ASP asp = obj->handle;
277 len = sprintf(dest, "%u.%u", ntohs(asp->asp_sat.sat_addr.s_net),
278 asp->asp_sat.sat_addr.s_node);
282 } else if (obj->proto == AFPPROTO_DSI) {
283 DSI *dsi = obj->handle;
284 len = sprintf(dest, "%s:%u",
285 getip_string((struct sockaddr *)&dsi->client),
286 getip_port((struct sockaddr *)&dsi->client));
290 } else if (is_var(p, "$d")) {
291 if (afpmaster && xlatevolname)
294 } else if (pwd && is_var(p, "$f")) {
295 if (afpmaster && xlatevolname)
297 if ((r = strchr(pwd->pw_gecos, ',')))
300 } else if (pwd && is_var(p, "$g")) {
301 if (afpmaster && xlatevolname)
303 struct group *grp = getgrgid(pwd->pw_gid);
306 } else if (is_var(p, "$h")) {
307 q = obj->options.hostname;
308 } else if (is_var(p, "$i")) {
309 if (afpmaster && xlatevolname)
311 if (obj->proto == AFPPROTO_ASP) {
312 ASP asp = obj->handle;
314 len = sprintf(dest, "%u", ntohs(asp->asp_sat.sat_addr.s_net));
318 } else if (obj->proto == AFPPROTO_DSI) {
319 DSI *dsi = obj->handle;
320 q = getip_string((struct sockaddr *)&dsi->client);
322 } else if (is_var(p, "$s")) {
325 else if (obj->options.server) {
326 q = obj->options.server;
328 q = obj->options.hostname;
329 } else if (obj->username && is_var(p, "$u")) {
330 if (afpmaster && xlatevolname)
333 if ( obj->options.ntseparator && (sep = strchr(obj->username, obj->options.ntseparator[0])) != NULL)
337 } else if (is_var(p, "$v")) {
338 if (afpmaster && xlatevolname)
344 if ((q = strrchr(path, '/')) == NULL)
346 else if (*(q + 1) != '\0')
349 } else if (is_var(p, "$z")) {
351 } else if (is_var(p, "$$")) {
356 /* copy the stuff over. if we don't understand something that we
357 * should, just skip it over. */
359 len = MIN(p == q ? 2 : strlen(q), destlen);
360 strncpy(dest, q, len);
365 /* stuff up to next $ */
367 p = strchr(src, '$');
368 len = p ? MIN((size_t)(p - src), destlen) : destlen;
370 strncpy(dest, src, len);
378 /* to make sure that val is valid, make sure to select an opt that
380 static int optionok(const char *buf, const char *opt, const char *val)
382 if (!strstr(buf,opt))
390 /* -------------------- */
391 static void setoption(struct vol_option *options, struct vol_option *save, int opt, const char *val)
393 if (options[opt].c_value && (!save || options[opt].c_value != save[opt].c_value))
394 free(options[opt].c_value);
395 options[opt].c_value = strdup(val + 1);
398 /* ------------------------------------------
399 handle all the options. tmp can't be NULL. */
400 static void volset(struct vol_option *options, struct vol_option *save,
401 char *volname, int vlen,
406 val = strchr(tmp, ':');
408 /* we'll assume it's a volume name. */
409 strncpy(volname, tmp, vlen);
414 LOG(log_debug, logtype_afpd, "Parsing volset %s", val);
416 if (optionok(tmp, "allow:", val)) {
417 setoption(options, save, VOLOPT_ALLOW, val);
419 } else if (optionok(tmp, "deny:", val)) {
420 setoption(options, save, VOLOPT_DENY, val);
422 } else if (optionok(tmp, "rwlist:", val)) {
423 setoption(options, save, VOLOPT_RWLIST, val);
425 } else if (optionok(tmp, "rolist:", val)) {
426 setoption(options, save, VOLOPT_ROLIST, val);
428 } else if (optionok(tmp, "codepage:", val)) {
429 LOG (log_error, logtype_afpd, "The old codepage system has been removed. Please make sure to read the documentation !!!!");
430 /* Make sure we don't screw anything */
432 } else if (optionok(tmp, "volcharset:", val)) {
433 setoption(options, save, VOLOPT_ENCODING, val);
434 } else if (optionok(tmp, "maccharset:", val)) {
435 setoption(options, save, VOLOPT_MACCHARSET, val);
436 } else if (optionok(tmp, "veto:", val)) {
437 setoption(options, save, VOLOPT_VETO, val);
438 } else if (optionok(tmp, "cnidscheme:", val)) {
439 setoption(options, save, VOLOPT_CNIDSCHEME, val);
440 } else if (optionok(tmp, "casefold:", val)) {
441 if (strcasecmp(val + 1, "tolower") == 0)
442 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMLOWER;
443 else if (strcasecmp(val + 1, "toupper") == 0)
444 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMUPPER;
445 else if (strcasecmp(val + 1, "xlatelower") == 0)
446 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UUPPERMLOWER;
447 else if (strcasecmp(val + 1, "xlateupper") == 0)
448 options[VOLOPT_CASEFOLD].i_value = AFPVOL_ULOWERMUPPER;
449 } else if (optionok(tmp, "adouble:", val)) {
450 if (strcasecmp(val + 1, "v1") == 0)
451 options[VOLOPT_ADOUBLE].i_value = AD_VERSION1;
452 #if AD_VERSION == AD_VERSION2
453 else if (strcasecmp(val + 1, "v2") == 0)
454 options[VOLOPT_ADOUBLE].i_value = AD_VERSION2;
455 else if (strcasecmp(val + 1, "osx") == 0)
456 options[VOLOPT_ADOUBLE].i_value = AD_VERSION2_OSX;
457 else if (strcasecmp(val + 1, "sfm") == 0)
458 options[VOLOPT_ADOUBLE].i_value = AD_VERSION1_SFM;
460 } else if (optionok(tmp, "options:", val)) {
463 if ((p = strtok(val + 1, ",")) == NULL) /* nothing */
467 if (strcasecmp(p, "prodos") == 0)
468 options[VOLOPT_FLAGS].i_value |= AFPVOL_A2VOL;
469 else if (strcasecmp(p, "mswindows") == 0) {
470 options[VOLOPT_FLAGS].i_value |= AFPVOL_MSWINDOWS | AFPVOL_USEDOTS;
471 } else if (strcasecmp(p, "crlf") == 0)
472 options[VOLOPT_FLAGS].i_value |= AFPVOL_CRLF;
473 else if (strcasecmp(p, "noadouble") == 0)
474 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOADOUBLE;
475 else if (strcasecmp(p, "ro") == 0)
476 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
477 else if (strcasecmp(p, "nohex") == 0)
478 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOHEX;
479 else if (strcasecmp(p, "usedots") == 0)
480 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS;
481 else if (strcasecmp(p, "invisibledots") == 0)
482 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS | AFPVOL_INV_DOTS;
483 else if (strcasecmp(p, "limitsize") == 0)
484 options[VOLOPT_FLAGS].i_value |= AFPVOL_LIMITSIZE;
485 /* support for either "dropbox" or "dropkludge" */
486 else if (strcasecmp(p, "dropbox") == 0)
487 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
488 else if (strcasecmp(p, "dropkludge") == 0)
489 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
490 else if (strcasecmp(p, "nofileid") == 0)
491 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOFILEID;
492 else if (strcasecmp(p, "nostat") == 0)
493 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOSTAT;
494 else if (strcasecmp(p, "preexec_close") == 0)
495 options[VOLOPT_PREEXEC].i_value = 1;
496 else if (strcasecmp(p, "root_preexec_close") == 0)
497 options[VOLOPT_ROOTPREEXEC].i_value = 1;
498 else if (strcasecmp(p, "upriv") == 0)
499 options[VOLOPT_FLAGS].i_value |= AFPVOL_UNIX_PRIV;
500 else if (strcasecmp(p, "nodev") == 0)
501 options[VOLOPT_FLAGS].i_value |= AFPVOL_NODEV;
502 else if (strcasecmp(p, "caseinsensitive") == 0)
503 options[VOLOPT_FLAGS].i_value |= AFPVOL_CASEINSEN;
504 else if (strcasecmp(p, "illegalseq") == 0)
505 options[VOLOPT_FLAGS].i_value |= AFPVOL_EILSEQ;
506 else if (strcasecmp(p, "nocnidcache") == 0)
507 options[VOLOPT_FLAGS].i_value &= ~AFPVOL_CACHE;
508 else if (strcasecmp(p, "tm") == 0)
509 options[VOLOPT_FLAGS].i_value |= AFPVOL_TM;
510 else if (strcasecmp(p, "searchdb") == 0)
511 options[VOLOPT_FLAGS].i_value |= AFPVOL_SEARCHDB;
512 /* Found this in branch dir-rewrite, maybe we want to use it sometimes */
514 else if (strcasecmp(p, "cdrom") == 0)
515 options[VOLOPT_FLAGS].i_value |= AFPVOL_CDROM | AFPVOL_RO;
517 p = strtok(NULL, ",");
520 } else if (optionok(tmp, "cnidserver:", val)) {
521 setoption(options, save, VOLOPT_CNIDSERVER, val);
523 char *p = strrchr(val + 1, ':');
526 setoption(options, save, VOLOPT_CNIDPORT, p);
529 LOG(log_debug, logtype_afpd, "CNID Server for volume '%s': %s:%s",
530 volname, val + 1, p ? p + 1 : Cnid_port);
532 } else if (optionok(tmp, "dbpath:", val)) {
533 setoption(options, save, VOLOPT_DBPATH, val);
535 } else if (optionok(tmp, "umask:", val)) {
536 options[VOLOPT_UMASK].i_value = (int)strtol(val +1, NULL, 8);
537 } else if (optionok(tmp, "dperm:", val)) {
538 options[VOLOPT_DPERM].i_value = (int)strtol(val+1, NULL, 8);
539 } else if (optionok(tmp, "fperm:", val)) {
540 options[VOLOPT_FPERM].i_value = (int)strtol(val+1, NULL, 8);
541 } else if (optionok(tmp, "perm:", val)) {
542 options[VOLOPT_DFLTPERM].i_value = (int)strtol(val+1, NULL, 8);
543 } else if (optionok(tmp, "password:", val)) {
544 setoption(options, save, VOLOPT_PASSWORD, val);
548 /* this code allows forced uid/gid per volume settings */
549 } else if (optionok(tmp, "forceuid:", val)) {
550 setoption(options, save, VOLOPT_FORCEUID, val);
551 } else if (optionok(tmp, "forcegid:", val)) {
552 setoption(options, save, VOLOPT_FORCEGID, val);
554 #endif /* FORCE_UIDGID */
555 } else if (optionok(tmp, "root_preexec:", val)) {
556 setoption(options, save, VOLOPT_ROOTPREEXEC, val);
558 } else if (optionok(tmp, "preexec:", val)) {
559 setoption(options, save, VOLOPT_PREEXEC, val);
561 } else if (optionok(tmp, "root_postexec:", val)) {
562 setoption(options, save, VOLOPT_ROOTPOSTEXEC, val);
564 } else if (optionok(tmp, "postexec:", val)) {
565 setoption(options, save, VOLOPT_POSTEXEC, val);
567 } else if (optionok(tmp, "allowed_hosts:", val)) {
568 setoption(options, save, VOLOPT_ALLOWED_HOSTS, val);
570 } else if (optionok(tmp, "denied_hosts:", val)) {
571 setoption(options, save, VOLOPT_DENIED_HOSTS, val);
573 } else if (optionok(tmp, "ea:", val)) {
574 if (strcasecmp(val + 1, "ad") == 0)
575 options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_AD;
576 else if (strcasecmp(val + 1, "sys") == 0)
577 options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_SYS;
578 else if (strcasecmp(val + 1, "none") == 0)
579 options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_NONE;
581 } else if (optionok(tmp, "volsizelimit:", val)) {
582 options[VOLOPT_LIMITSIZE].i_value = (uint32_t)strtoul(val + 1, NULL, 10);
585 /* ignore unknown options */
586 LOG(log_debug, logtype_afpd, "ignoring unknown volume option: %s", tmp);
591 /* ----------------- */
592 static void showvol(const ucs2_t *name)
595 for ( volume = Volumes; volume; volume = volume->v_next ) {
596 if (volume->v_hide && !strcasecmp_w( volume->v_name, name ) ) {
603 /* ------------------------------- */
604 static int creatvol(AFPObj *obj, struct passwd *pwd,
605 char *path, char *name,
606 struct vol_option *options,
607 const int user /* user defined volume */
611 int suffixlen, vlen, tmpvlen, u8mvlen, macvlen;
613 char tmpname[AFPVOL_U8MNAMELEN+1];
614 ucs2_t u8mtmpname[(AFPVOL_U8MNAMELEN+1)*2], mactmpname[(AFPVOL_MACNAMELEN+1)*2];
615 char suffix[6]; /* max is #FFFF */
618 LOG(log_debug, logtype_afpd, "createvol: Volume '%s'", name);
620 if ( name == NULL || *name == '\0' ) {
621 if ((name = strrchr( path, '/' )) == NULL) {
622 return -1; /* Obviously not a fully qualified path */
625 /* if you wish to share /, you need to specify a name. */
630 /* suffix for mangling use (lastvid + 1) */
631 /* because v_vid has not been decided yet. */
632 suffixlen = sprintf(suffix, "%c%X", MANGLE_CHAR, lastvid + 1 );
634 vlen = strlen( name );
636 /* Unicode Volume Name */
637 /* Firstly convert name from unixcharset to UTF8-MAC */
639 tmpvlen = convert_charset(obj->options.unixcharset, CH_UTF8_MAC, 0, name, vlen, tmpname, AFPVOL_U8MNAMELEN, &flags);
641 strcpy(tmpname, "???");
645 /* Do we have to mangle ? */
646 if ( (flags & CONV_REQMANGLE) || (tmpvlen > obj->options.volnamelen)) {
647 if (tmpvlen + suffixlen > obj->options.volnamelen) {
649 tmpvlen = convert_charset(obj->options.unixcharset, CH_UTF8_MAC, 0, name, vlen, tmpname, obj->options.volnamelen - suffixlen, &flags);
650 tmpname[tmpvlen >= 0 ? tmpvlen : 0] = 0;
652 strcat(tmpname, suffix);
653 tmpvlen = strlen(tmpname);
656 /* Secondly convert name from UTF8-MAC to UCS2 */
657 if ( 0 >= ( u8mvlen = convert_string(CH_UTF8_MAC, CH_UCS2, tmpname, tmpvlen, u8mtmpname, AFPVOL_U8MNAMELEN*2)) )
660 LOG(log_maxdebug, logtype_afpd, "createvol: Volume '%s' -> UTF8-MAC Name: '%s'", name, tmpname);
662 /* Maccharset Volume Name */
663 /* Firsty convert name from unixcharset to maccharset */
665 tmpvlen = convert_charset(obj->options.unixcharset, obj->options.maccharset, 0, name, vlen, tmpname, AFPVOL_U8MNAMELEN, &flags);
667 strcpy(tmpname, "???");
671 /* Do we have to mangle ? */
672 if ( (flags & CONV_REQMANGLE) || (tmpvlen > AFPVOL_MACNAMELEN)) {
673 if (tmpvlen + suffixlen > AFPVOL_MACNAMELEN) {
675 tmpvlen = convert_charset(obj->options.unixcharset, obj->options.maccharset, 0, name, vlen, tmpname, AFPVOL_MACNAMELEN - suffixlen, &flags);
676 tmpname[tmpvlen >= 0 ? tmpvlen : 0] = 0;
678 strcat(tmpname, suffix);
679 tmpvlen = strlen(tmpname);
682 /* Secondly convert name from maccharset to UCS2 */
683 if ( 0 >= ( macvlen = convert_string(obj->options.maccharset, CH_UCS2, tmpname, tmpvlen, mactmpname, AFPVOL_U8MNAMELEN*2)) )
686 LOG(log_maxdebug, logtype_afpd, "createvol: Volume '%s' -> Longname: '%s'", name, tmpname);
688 /* check duplicate */
689 for ( volume = Volumes; volume; volume = volume->v_next ) {
690 if (( strcasecmp_w( volume->v_u8mname, u8mtmpname ) == 0 ) || ( strcasecmp_w( volume->v_macname, mactmpname ) == 0 )){
691 if (volume->v_deleted) {
692 volume->v_new = hide = 1;
695 return -1; /* Won't be able to access it, anyway... */
700 if (!( volume = (struct vol *)calloc(1, sizeof( struct vol ))) ) {
701 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
704 if ( NULL == ( volume->v_localname = strdup(name))) {
705 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
710 if ( NULL == ( volume->v_u8mname = strdup_w(u8mtmpname))) {
711 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
716 if ( NULL == ( volume->v_macname = strdup_w(mactmpname))) {
717 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
722 if (!( volume->v_path = (char *)malloc( strlen( path ) + 1 )) ) {
723 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
729 volume->v_name = utf8_encoding()?volume->v_u8mname:volume->v_macname;
730 volume->v_hide = hide;
731 strcpy( volume->v_path, path );
735 #endif /* __svr4__ */
736 /* os X start at 1 and use network order ie. 1 2 3 */
737 volume->v_vid = ++lastvid;
738 volume->v_vid = htons(volume->v_vid);
740 if (check_vol_acl_support(volume))
741 volume->v_flags |= AFPVOL_ACLS
747 volume->v_casefold = options[VOLOPT_CASEFOLD].i_value;
748 volume->v_flags |= options[VOLOPT_FLAGS].i_value;
750 if (options[VOLOPT_EA_VFS].i_value)
751 volume->v_vfs_ea = options[VOLOPT_EA_VFS].i_value;
753 volume->v_ad_options = 0;
754 if ((volume->v_flags & AFPVOL_NODEV))
755 volume->v_ad_options |= ADVOL_NODEV;
756 if ((volume->v_flags & AFPVOL_CACHE))
757 volume->v_ad_options |= ADVOL_CACHE;
758 if ((volume->v_flags & AFPVOL_UNIX_PRIV))
759 volume->v_ad_options |= ADVOL_UNIXPRIV;
760 if ((volume->v_flags & AFPVOL_INV_DOTS))
761 volume->v_ad_options |= ADVOL_INVDOTS;
762 if ((volume->v_flags & AFPVOL_NOADOUBLE))
763 volume->v_ad_options |= ADVOL_NOADOUBLE;
765 if (options[VOLOPT_PASSWORD].c_value)
766 volume->v_password = strdup(options[VOLOPT_PASSWORD].c_value);
768 if (options[VOLOPT_VETO].c_value)
769 volume->v_veto = strdup(options[VOLOPT_VETO].c_value);
771 if (options[VOLOPT_ENCODING].c_value)
772 volume->v_volcodepage = strdup(options[VOLOPT_ENCODING].c_value);
774 if (options[VOLOPT_MACCHARSET].c_value)
775 volume->v_maccodepage = strdup(options[VOLOPT_MACCHARSET].c_value);
777 if (options[VOLOPT_DBPATH].c_value)
778 volume->v_dbpath = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_DBPATH].c_value, pwd, path, name);
780 if (options[VOLOPT_CNIDSCHEME].c_value)
781 volume->v_cnidscheme = strdup(options[VOLOPT_CNIDSCHEME].c_value);
783 if (options[VOLOPT_CNIDSERVER].c_value)
784 volume->v_cnidserver = strdup(options[VOLOPT_CNIDSERVER].c_value);
786 if (options[VOLOPT_CNIDPORT].c_value)
787 volume->v_cnidport = strdup(options[VOLOPT_CNIDPORT].c_value);
789 if (options[VOLOPT_UMASK].i_value)
790 volume->v_umask = (mode_t)options[VOLOPT_UMASK].i_value;
792 if (options[VOLOPT_DPERM].i_value)
793 volume->v_dperm = (mode_t)options[VOLOPT_DPERM].i_value;
795 if (options[VOLOPT_FPERM].i_value)
796 volume->v_fperm = (mode_t)options[VOLOPT_FPERM].i_value;
798 if (options[VOLOPT_DFLTPERM].i_value)
799 volume->v_perm = (mode_t)options[VOLOPT_DFLTPERM].i_value;
801 if (options[VOLOPT_ADOUBLE].i_value)
802 volume->v_adouble = options[VOLOPT_ADOUBLE].i_value;
804 volume->v_adouble = AD_VERSION;
806 if (options[VOLOPT_LIMITSIZE].i_value)
807 volume->v_limitsize = options[VOLOPT_LIMITSIZE].i_value;
809 /* Mac to Unix conversion flags*/
810 volume->v_mtou_flags = 0;
811 if (!(volume->v_flags & AFPVOL_NOHEX))
812 volume->v_mtou_flags |= CONV_ESCAPEHEX;
813 if (!(volume->v_flags & AFPVOL_USEDOTS))
814 volume->v_mtou_flags |= CONV_ESCAPEDOTS;
815 if ((volume->v_flags & AFPVOL_EILSEQ))
816 volume->v_mtou_flags |= CONV__EILSEQ;
818 if ((volume->v_casefold & AFPVOL_MTOUUPPER))
819 volume->v_mtou_flags |= CONV_TOUPPER;
820 else if ((volume->v_casefold & AFPVOL_MTOULOWER))
821 volume->v_mtou_flags |= CONV_TOLOWER;
823 /* Unix to Mac conversion flags*/
824 volume->v_utom_flags = CONV_IGNORE | CONV_UNESCAPEHEX;
825 if ((volume->v_casefold & AFPVOL_UTOMUPPER))
826 volume->v_utom_flags |= CONV_TOUPPER;
827 else if ((volume->v_casefold & AFPVOL_UTOMLOWER))
828 volume->v_utom_flags |= CONV_TOLOWER;
830 if ((volume->v_flags & AFPVOL_EILSEQ))
831 volume->v_utom_flags |= CONV__EILSEQ;
834 if (options[VOLOPT_FORCEUID].c_value) {
835 volume->v_forceuid = strdup(options[VOLOPT_FORCEUID].c_value);
837 volume->v_forceuid = NULL; /* set as null so as to return 0 later on */
840 if (options[VOLOPT_FORCEGID].c_value) {
841 volume->v_forcegid = strdup(options[VOLOPT_FORCEGID].c_value);
843 volume->v_forcegid = NULL; /* set as null so as to return 0 later on */
847 if (options[VOLOPT_PREEXEC].c_value)
848 volume->v_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_PREEXEC].c_value, pwd, path, name);
849 volume->v_preexec_close = options[VOLOPT_PREEXEC].i_value;
851 if (options[VOLOPT_POSTEXEC].c_value)
852 volume->v_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_POSTEXEC].c_value, pwd, path, name);
854 if (options[VOLOPT_ROOTPREEXEC].c_value)
855 volume->v_root_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPREEXEC].c_value, pwd, path, name);
856 volume->v_root_preexec_close = options[VOLOPT_ROOTPREEXEC].i_value;
858 if (options[VOLOPT_ROOTPOSTEXEC].c_value)
859 volume->v_root_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPOSTEXEC].c_value, pwd, path, name);
862 volume->v_dperm |= volume->v_perm;
863 volume->v_fperm |= volume->v_perm;
865 /* Check EA support on volume */
866 if (volume->v_vfs_ea == AFPVOL_EA_AUTO)
867 check_ea_sys_support(volume);
870 /* get/store uuid from file */
871 if (volume->v_flags & AFPVOL_TM) {
872 char *uuid = get_uuid(obj, volume->v_localname);
874 LOG(log_error, logtype_afpd, "Volume '%s': couldn't get UUID",
875 volume->v_localname);
877 volume->v_uuid = uuid;
878 LOG(log_debug, logtype_afpd, "Volume '%s': UUID '%s'",
879 volume->v_localname, volume->v_uuid);
883 volume->v_next = Volumes;
888 /* ---------------- */
889 static char *myfgets( char *buf, int size, FILE *fp)
895 while ((EOF != ( c = getc( fp )) ) && ( size > 1 )) {
896 if ( c == '\n' || c == '\r' ) {
897 if (p != buf && *(p -1) == '\\') {
919 /* check access list. this function wants something of the following
921 * @group,name,name2,@group2,name3
923 * a NULL argument allows everybody to have access.
924 * we return three things:
926 * 0: list exists, but name isn't in it
930 #ifndef NO_REAL_USER_NAME
931 /* authentication is case insensitive
932 * FIXME should we do the same with group name?
934 #define access_strcmp strcasecmp
937 #define access_strcmp strcmp
941 static int accessvol(const char *args, const char *name)
943 char buf[MAXPATHLEN + 1], *p;
949 strlcpy(buf, args, sizeof(buf));
950 if ((p = strtok(buf, ",")) == NULL) /* nothing, return okay */
954 if (*p == '@') { /* it's a group */
955 if ((gr = getgrnam(p + 1)) && gmem(gr->gr_gid))
957 } else if (access_strcmp(p, name) == 0) /* it's a user name */
959 p = strtok(NULL, ",");
965 static int hostaccessvol(int type, const char *volname, const char *args, const AFPObj *obj)
968 char buf[MAXPATHLEN + 1], *p, *b;
969 DSI *dsi = obj->handle;
970 struct sockaddr_storage client;
975 strlcpy(buf, args, sizeof(buf));
976 if ((p = strtok_r(buf, ",", &b)) == NULL) /* nothing, return okay */
979 if (obj->proto != AFPPROTO_DSI)
984 char *ipaddr, *mask_char;
985 struct addrinfo hints, *ai;
987 ipaddr = strtok(p, "/");
988 mask_char = strtok(NULL,"/");
990 /* Get address from string with getaddrinfo */
991 memset(&hints, 0, sizeof hints);
992 hints.ai_family = AF_UNSPEC;
993 hints.ai_socktype = SOCK_STREAM;
994 if ((ret = getaddrinfo(ipaddr, NULL, &hints, &ai)) != 0) {
995 LOG(log_error, logtype_afpd, "hostaccessvol: getaddrinfo: %s\n", gai_strerror(ret));
1000 if (mask_char != NULL)
1001 mask_int = atoi(mask_char); /* apply_ip_mask does range checking on it */
1003 if (ai->ai_family == AF_INET) /* IPv4 */
1009 /* Apply mask to addresses */
1010 client = dsi->client;
1011 apply_ip_mask((struct sockaddr *)&client, mask_int);
1012 apply_ip_mask(ai->ai_addr, mask_int);
1014 if (compare_ip((struct sockaddr *)&client, ai->ai_addr) == 0) {
1015 if (type == VOLOPT_DENIED_HOSTS)
1016 LOG(log_info, logtype_afpd, "AFP access denied for client IP '%s' to volume '%s' by denied list",
1017 getip_string((struct sockaddr *)&client), volname);
1024 p = strtok_r(NULL, ",", &b);
1027 if (type == VOLOPT_ALLOWED_HOSTS)
1028 LOG(log_info, logtype_afpd, "AFP access denied for client IP '%s' to volume '%s', not in allowed list",
1029 getip_string((struct sockaddr *)&dsi->client), volname);
1033 static void setextmap(char *ext, char *type, char *creator, int user)
1038 if (Extmap == NULL) {
1039 if (( Extmap = calloc(1, sizeof( struct extmap ))) == NULL ) {
1040 LOG(log_error, logtype_afpd, "setextmap: calloc: %s", strerror(errno) );
1045 for ( em = Extmap, cnt = 0; em->em_ext; em++, cnt++) {
1046 if ( (strdiacasecmp( em->em_ext, ext )) == 0 ) {
1051 if ( em->em_ext == NULL ) {
1052 if (!(Extmap = realloc( Extmap, sizeof( struct extmap ) * (cnt +2))) ) {
1053 LOG(log_error, logtype_afpd, "setextmap: realloc: %s", strerror(errno) );
1056 (Extmap +cnt +1)->em_ext = NULL;
1058 } else if ( !user ) {
1064 if (!(em->em_ext = strdup( ext))) {
1065 LOG(log_error, logtype_afpd, "setextmap: strdup: %s", strerror(errno) );
1069 if ( *type == '\0' ) {
1070 memcpy(em->em_type, "\0\0\0\0", sizeof( em->em_type ));
1072 memcpy(em->em_type, type, sizeof( em->em_type ));
1074 if ( *creator == '\0' ) {
1075 memcpy(em->em_creator, "\0\0\0\0", sizeof( em->em_creator ));
1077 memcpy(em->em_creator, creator, sizeof( em->em_creator ));
1081 /* -------------------------- */
1082 static int extmap_cmp(const void *map1, const void *map2)
1084 const struct extmap *em1 = map1;
1085 const struct extmap *em2 = map2;
1086 return strdiacasecmp(em1->em_ext, em2->em_ext);
1089 static void sortextmap( void)
1094 if ((em = Extmap) == NULL) {
1097 while (em->em_ext) {
1102 qsort(Extmap, Extmap_cnt, sizeof(struct extmap), extmap_cmp);
1103 if (*Extmap->em_ext == 0) {
1104 /* the first line is really "." the default entry,
1105 * we remove the leading '.' in setextmap
1112 /* ----------------------
1114 static void free_extmap( void)
1119 for ( em = Extmap; em->em_ext; em++) {
1129 /* ----------------------
1131 static int volfile_changed(struct afp_volume_name *p)
1137 name = p->full_name;
1141 if (!stat( name, &st) && st.st_mtime > p->mtime) {
1142 p->mtime = st.st_mtime;
1148 /* ----------------------
1149 * Read a volume configuration file and add the volumes contained within to
1150 * the global volume list. This gets called from the forked afpd childs.
1151 * The master now reads this too for Zeroconf announcements.
1153 * If p2 is non-NULL, the file that is opened is
1156 * Lines that begin with # and blank lines are ignored.
1157 * Volume lines are of the form:
1158 * <unix path> [<volume name>] [allow:<user>,<@group>,...] \
1159 * [codepage:<file>] [casefold:<num>]
1160 * <extension> TYPE [CREATOR]
1163 int readvolfile(AFPObj *obj, struct afp_volume_name *p1, char *p2, int user, struct passwd *pwent)
1166 char path[MAXPATHLEN + 1];
1167 char tmp[MAXPATHLEN + 1];
1168 char volname[AFPVOL_U8MNAMELEN + 1];
1170 char type[5], creator[5];
1175 struct vol_option save_options[VOLOPT_NUM];
1176 struct vol_option options[VOLOPT_NUM];
1182 strcpy( path, p1->name );
1184 strcat( path, "/" );
1186 if (p1->full_name) {
1187 free(p1->full_name);
1189 p1->full_name = strdup(path);
1192 if (NULL == ( fp = fopen( path, "r" )) ) {
1196 if (fd != -1 && !fstat( fd, &st) ) {
1197 p1->mtime = st.st_mtime;
1200 memset(save_options, 0, sizeof(save_options));
1202 /* Enable some default options for all volumes */
1203 save_options[VOLOPT_FLAGS].i_value |= AFPVOL_CACHE;
1204 save_options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_AUTO;
1205 LOG(log_maxdebug, logtype_afpd, "readvolfile: seeding default umask: %04o",
1206 obj->options.umask);
1207 save_options[VOLOPT_UMASK].i_value = obj->options.umask;
1209 LOG(log_debug, logtype_afpd, "readvolfile: \"%s\"", path);
1211 while ( myfgets( buf, sizeof( buf ), fp ) != NULL ) {
1212 initline( strlen( buf ), buf );
1213 parseline( sizeof( path ) - 1, path );
1220 /* change the default options for this file */
1221 if (strncmp(path, VOLOPT_DEFAULT, VOLOPT_DEFAULT_LEN) == 0) {
1223 for (i = 0; i < VOLOPT_NUM; i++) {
1224 if (parseline( sizeof( path ) - VOLOPT_DEFAULT_LEN - 1,
1225 path + VOLOPT_DEFAULT_LEN) < 0)
1227 volset(save_options, NULL, tmp, sizeof(tmp) - 1,
1228 path + VOLOPT_DEFAULT_LEN);
1234 if (( p = strchr( path, '/' )) != NULL ) {
1242 if ( u == NULL || *u == '\0' || ( pw = getpwnam( u )) == NULL ) {
1245 strcpy( tmp, pw->pw_dir );
1246 if ( p != NULL && *p != '\0' ) {
1253 /* send path through variable substitution */
1254 if (*path != '~') /* need to copy path to tmp */
1256 if (!pwent && obj->username)
1257 pwent = getpwnam(obj->username);
1259 if (volxlate(obj, path, sizeof(path) - 1, tmp, pwent, NULL, NULL) == NULL)
1262 /* this is sort of braindead. basically, i want to be
1263 * able to specify things in any order, but i don't want to
1264 * re-write everything. */
1266 memcpy(options, save_options, sizeof(options));
1269 /* read in up to VOLOP_NUM possible options */
1270 for (i = 0; i < VOLOPT_NUM; i++) {
1271 if (parseline( sizeof( tmp ) - 1, tmp ) < 0)
1274 volset(options, save_options, volname, sizeof(volname) - 1, tmp);
1277 /* check allow/deny lists (if not afpd master loading volumes for Zeroconf reg.):
1278 allow -> either no list (-1), or in list (1)
1279 deny -> either no list (-1), or not in list (0) */
1280 if (parent_or_child == 0
1282 (accessvol(options[VOLOPT_ALLOW].c_value, obj->username) &&
1283 (accessvol(options[VOLOPT_DENY].c_value, obj->username) < 1) &&
1284 hostaccessvol(VOLOPT_ALLOWED_HOSTS, volname, options[VOLOPT_ALLOWED_HOSTS].c_value, obj) &&
1285 (hostaccessvol(VOLOPT_DENIED_HOSTS, volname, options[VOLOPT_DENIED_HOSTS].c_value, obj) < 1))) {
1287 /* handle read-only behaviour. semantics:
1288 * 1) neither the rolist nor the rwlist exist -> rw
1289 * 2) rolist exists -> ro if user is in it.
1290 * 3) rwlist exists -> ro unless user is in it. */
1291 if (parent_or_child == 1
1293 ((options[VOLOPT_FLAGS].i_value & AFPVOL_RO) == 0)
1295 ((accessvol(options[VOLOPT_ROLIST].c_value, obj->username) == 1) ||
1296 !accessvol(options[VOLOPT_RWLIST].c_value, obj->username)))
1297 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
1299 /* do variable substitution for volname */
1300 if (volxlate(obj, tmp, sizeof(tmp) - 1, volname, pwent, path, NULL) == NULL)
1303 creatvol(obj, pwent, path, tmp, options, p2 != NULL);
1305 volfree(options, save_options);
1309 parseline( sizeof( type ) - 1, type );
1310 parseline( sizeof( creator ) - 1, creator );
1311 setextmap( path, type, creator, user);
1318 volfree(save_options, NULL);
1320 if ( fclose( fp ) != 0 ) {
1321 LOG(log_error, logtype_afpd, "readvolfile: fclose: %s", strerror(errno) );
1327 /* ------------------------------- */
1328 static void volume_free(struct vol *vol)
1330 free(vol->v_localname);
1331 vol->v_localname = NULL;
1332 free(vol->v_u8mname);
1333 vol->v_u8mname = NULL;
1334 free(vol->v_macname);
1335 vol->v_macname = NULL;
1337 free(vol->v_password);
1339 free(vol->v_volcodepage);
1340 free(vol->v_maccodepage);
1341 free(vol->v_cnidscheme);
1342 free(vol->v_dbpath);
1345 free(vol->v_forceuid);
1346 free(vol->v_forcegid);
1347 #endif /* FORCE_UIDGID */
1352 /* ------------------------------- */
1353 static void free_volumes(void )
1356 struct vol *nvol, *ovol;
1358 for ( vol = Volumes; vol; vol = vol->v_next ) {
1359 if (( vol->v_flags & AFPVOL_OPEN ) ) {
1366 for ( vol = Volumes, ovol = NULL; vol; vol = nvol) {
1369 if (vol->v_localname == NULL) {
1370 if (Volumes == vol) {
1375 ovol->v_next = nvol;
1385 /* ------------------------------- */
1386 static void volume_unlink(struct vol *volume)
1388 struct vol *vol, *ovol, *nvol;
1390 if (volume == Volumes) {
1391 Volumes = Volumes->v_next;
1394 for ( vol = Volumes->v_next, ovol = Volumes; vol; vol = nvol) {
1397 if (vol == volume) {
1398 ovol->v_next = nvol;
1407 static int getvolspace(struct vol *vol,
1408 u_int32_t *bfree, u_int32_t *btotal,
1409 VolSpace *xbfree, VolSpace *xbtotal, u_int32_t *bsize)
1413 #ifndef NO_QUOTA_SUPPORT
1414 VolSpace qfree, qtotal;
1417 spaceflag = AFPVOL_GVSMASK & vol->v_flags;
1418 /* report up to 2GB if afp version is < 2.2 (4GB if not) */
1419 maxsize = (vol->v_flags & AFPVOL_A2VOL) ? 0x01fffe00 :
1420 (((afp_version < 22) || (vol->v_flags & AFPVOL_LIMITSIZE))
1421 ? 0x7fffffffL : 0xffffffffL);
1424 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_AFSGVS ) {
1425 if ( afs_getvolspace( vol, xbfree, xbtotal, bsize ) == AFP_OK ) {
1426 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_AFSGVS;
1427 goto getvolspace_done;
1432 if (( rc = ustatfs_getvolspace( vol, xbfree, xbtotal, bsize)) != AFP_OK ) {
1436 #define min(a,b) ((a)<(b)?(a):(b))
1437 #ifndef NO_QUOTA_SUPPORT
1438 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_UQUOTA ) {
1439 if ( uquota_getvolspace( vol, &qfree, &qtotal, *bsize ) == AFP_OK ) {
1440 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_UQUOTA;
1441 *xbfree = min(*xbfree, qfree);
1442 *xbtotal = min( *xbtotal, qtotal);
1443 goto getvolspace_done;
1447 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_USTATFS;
1450 if (vol->v_limitsize) {
1452 if ((cmdstr = bformat("du -sh \"%s\" 2> /dev/null | cut -f1", vol->v_path)) == NULL)
1455 FILE *cmd = popen(cfrombstr(cmdstr), "r");
1461 fgets(buf, 100, cmd);
1463 if (pclose(cmd) == -1)
1467 if (buf[strlen(buf) - 2] == 'G' || buf[strlen(buf) - 2] == 'g')
1469 multi = 1024 * 1024 * 1024;
1470 else if (buf[strlen(buf) - 2] == 'M' || buf[strlen(buf) - 2] == 'm')
1472 multi = 1024 * 1024;
1473 else if (buf[strlen(buf) - 2] == 'K' || buf[strlen(buf) - 2] == 'k')
1478 if (p = strchr(buf, ','))
1479 /* ignore fraction */
1482 /* remove G|M|K char */
1483 buf[strlen(buf) - 2] = 0;
1484 /* now buf contains only digits */
1485 long long used = atoll(buf) * multi;
1486 LOG(log_debug, logtype_afpd, "volparams: used on volume: %llu bytes", used);
1488 *xbtotal = min(*xbtotal, (vol->v_limitsize * 1024 * 1024));
1489 *xbfree = min(*xbfree, *xbtotal < used ? 0 : *xbtotal - used);
1492 *bfree = min( *xbfree, maxsize);
1493 *btotal = min( *xbtotal, maxsize);
1497 /* -----------------------
1498 * set volume creation date
1499 * avoid duplicate, well at least it tries
1501 static void vol_setdate(u_int16_t id, struct adouble *adp, time_t date)
1504 struct vol *vol = Volumes;
1506 for ( volume = Volumes; volume; volume = volume->v_next ) {
1507 if (volume->v_vid == id) {
1510 else if ((time_t)(AD_DATE_FROM_UNIX(date)) == volume->v_ctime) {
1512 volume = Volumes; /* restart */
1515 vol->v_ctime = AD_DATE_FROM_UNIX(date);
1516 ad_setdate(adp, AD_DATE_CREATE | AD_DATE_UNIX, date);
1519 /* ----------------------- */
1520 static int getvolparams( u_int16_t bitmap, struct vol *vol, struct stat *st, char *buf, size_t *buflen)
1523 int bit = 0, isad = 1;
1526 u_int32_t bfree, btotal, bsize;
1527 VolSpace xbfree, xbtotal; /* extended bytes */
1528 char *data, *nameoff = NULL;
1531 LOG(log_debug, logtype_afpd, "getvolparams: Volume '%s'", vol->v_localname);
1533 /* courtesy of jallison@whistle.com:
1534 * For MacOS8.x support we need to create the
1535 * .Parent file here if it doesn't exist. */
1537 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1538 if ( ad_open_metadata( vol->v_path, ADFLAGS_DIR, O_CREAT, &ad) < 0 ) {
1540 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1542 } else if (ad_get_MD_flags( &ad ) & O_CREAT) {
1543 slash = strrchr( vol->v_path, '/' );
1547 slash = vol->v_path;
1548 if (ad_getentryoff(&ad, ADEID_NAME)) {
1549 ad_setentrylen( &ad, ADEID_NAME, strlen( slash ));
1550 memcpy(ad_entry( &ad, ADEID_NAME ), slash,
1551 ad_getentrylen( &ad, ADEID_NAME ));
1553 vol_setdate(vol->v_vid, &ad, st->st_mtime);
1557 if (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0)
1558 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1560 vol->v_ctime = aint;
1563 if (( bitmap & ( (1<<VOLPBIT_BFREE)|(1<<VOLPBIT_BTOTAL) |
1564 (1<<VOLPBIT_XBFREE)|(1<<VOLPBIT_XBTOTAL) |
1565 (1<<VOLPBIT_BSIZE)) ) != 0 ) {
1566 if ( getvolspace( vol, &bfree, &btotal, &xbfree, &xbtotal,
1567 &bsize) != AFP_OK ) {
1569 ad_close( &ad, ADFLAGS_HF );
1571 return( AFPERR_PARAM );
1576 while ( bitmap != 0 ) {
1577 while (( bitmap & 1 ) == 0 ) {
1585 /* check for read-only.
1586 * NOTE: we don't actually set the read-only flag unless
1587 * it's passed in that way as it's possible to mount
1588 * a read-write filesystem under a read-only one. */
1589 if ((vol->v_flags & AFPVOL_RO) ||
1590 ((utime(vol->v_path, NULL) < 0) && (errno == EROFS))) {
1591 ashort |= VOLPBIT_ATTR_RO;
1593 /* prior 2.1 only VOLPBIT_ATTR_RO is defined */
1594 if (afp_version > 20) {
1595 if (0 == (vol->v_flags & AFPVOL_NOFILEID) && vol->v_cdb != NULL &&
1596 (vol->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1597 ashort |= VOLPBIT_ATTR_FILEID;
1599 ashort |= VOLPBIT_ATTR_CATSEARCH;
1601 if (afp_version >= 30) {
1602 ashort |= VOLPBIT_ATTR_UTF8;
1603 if (vol->v_flags & AFPVOL_UNIX_PRIV)
1604 ashort |= VOLPBIT_ATTR_UNIXPRIV;
1605 if (vol->v_flags & AFPVOL_TM)
1606 ashort |= VOLPBIT_ATTR_TM;
1608 if (afp_version >= 32) {
1610 ashort |= VOLPBIT_ATTR_EXT_ATTRS;
1611 if (vol->v_flags & AFPVOL_ACLS)
1612 ashort |= VOLPBIT_ATTR_ACLS;
1616 ashort = htons(ashort);
1617 memcpy(data, &ashort, sizeof( ashort ));
1618 data += sizeof( ashort );
1622 ashort = htons( AFPVOLSIG_DEFAULT );
1623 memcpy(data, &ashort, sizeof( ashort ));
1624 data += sizeof( ashort );
1627 case VOLPBIT_CDATE :
1628 aint = vol->v_ctime;
1629 memcpy(data, &aint, sizeof( aint ));
1630 data += sizeof( aint );
1633 case VOLPBIT_MDATE :
1634 if ( st->st_mtime > vol->v_mtime ) {
1635 vol->v_mtime = st->st_mtime;
1637 aint = AD_DATE_FROM_UNIX(vol->v_mtime);
1638 memcpy(data, &aint, sizeof( aint ));
1639 data += sizeof( aint );
1642 case VOLPBIT_BDATE :
1643 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1644 aint = AD_DATE_START;
1645 memcpy(data, &aint, sizeof( aint ));
1646 data += sizeof( aint );
1650 memcpy(data, &vol->v_vid, sizeof( vol->v_vid ));
1651 data += sizeof( vol->v_vid );
1654 case VOLPBIT_BFREE :
1655 bfree = htonl( bfree );
1656 memcpy(data, &bfree, sizeof( bfree ));
1657 data += sizeof( bfree );
1660 case VOLPBIT_BTOTAL :
1661 btotal = htonl( btotal );
1662 memcpy(data, &btotal, sizeof( btotal ));
1663 data += sizeof( btotal );
1666 #ifndef NO_LARGE_VOL_SUPPORT
1667 case VOLPBIT_XBFREE :
1668 xbfree = hton64( xbfree );
1669 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1670 bcopy(&xbfree, data, sizeof(xbfree));
1671 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1672 memcpy(data, &xbfree, sizeof( xbfree ));
1673 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1674 data += sizeof( xbfree );
1677 case VOLPBIT_XBTOTAL :
1678 xbtotal = hton64( xbtotal );
1679 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1680 bcopy(&xbtotal, data, sizeof(xbtotal));
1681 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1682 memcpy(data, &xbtotal, sizeof( xbtotal ));
1683 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1684 data += sizeof( xbfree );
1686 #endif /* ! NO_LARGE_VOL_SUPPORT */
1690 data += sizeof( u_int16_t );
1693 case VOLPBIT_BSIZE: /* block size */
1694 bsize = htonl(bsize);
1695 memcpy(data, &bsize, sizeof(bsize));
1696 data += sizeof(bsize);
1701 ad_close( &ad, ADFLAGS_HF );
1703 return( AFPERR_BITMAP );
1709 ashort = htons( data - buf );
1710 memcpy(nameoff, &ashort, sizeof( ashort ));
1711 /* name is always in mac charset */
1712 aint = ucs2_to_charset( vol->v_maccharset, vol->v_macname, data+1, AFPVOL_MACNAMELEN + 1);
1722 ad_close_metadata( &ad);
1724 *buflen = data - buf;
1728 /* ------------------------- */
1729 static int stat_vol(u_int16_t bitmap, struct vol *vol, char *rbuf, size_t *rbuflen)
1735 if ( stat( vol->v_path, &st ) < 0 ) {
1737 return( AFPERR_PARAM );
1739 /* save the volume device number */
1740 vol->v_dev = st.st_dev;
1742 buflen = *rbuflen - sizeof( bitmap );
1743 if (( ret = getvolparams( bitmap, vol, &st,
1744 rbuf + sizeof( bitmap ), &buflen )) != AFP_OK ) {
1748 *rbuflen = buflen + sizeof( bitmap );
1749 bitmap = htons( bitmap );
1750 memcpy(rbuf, &bitmap, sizeof( bitmap ));
1755 /* ------------------------------- */
1756 void load_volumes(AFPObj *obj)
1758 struct passwd *pwent;
1763 /* check files date */
1764 if (obj->options.defaultvol.loaded) {
1765 changed = volfile_changed(&obj->options.defaultvol);
1767 if (obj->options.systemvol.loaded) {
1768 changed |= volfile_changed(&obj->options.systemvol);
1770 if (obj->options.uservol.loaded) {
1771 changed |= volfile_changed(&obj->options.uservol);
1780 if (parent_or_child == 0) {
1781 LOG(log_debug, logtype_afpd, "load_volumes: AFP MASTER");
1783 LOG(log_debug, logtype_afpd, "load_volumes: user: %s", obj->username);
1786 pwent = getpwnam(obj->username);
1787 if ( (obj->options.flags & OPTION_USERVOLFIRST) == 0 ) {
1788 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent);
1791 if ((*obj->username == '\0') || (obj->options.flags & OPTION_NOUSERVOL)) {
1792 readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent);
1795 * Read user's AppleVolumes or .AppleVolumes file
1796 * If neither are readable, read the default volumes file. if
1797 * that doesn't work, create a user share.
1799 if (obj->options.uservol.name) {
1800 free(obj->options.uservol.name);
1802 obj->options.uservol.name = strdup(pwent->pw_dir);
1803 if ( readvolfile(obj, &obj->options.uservol, "AppleVolumes", 1, pwent) < 0 &&
1804 readvolfile(obj, &obj->options.uservol, ".AppleVolumes", 1, pwent) < 0 &&
1805 readvolfile(obj, &obj->options.uservol, "applevolumes", 1, pwent) < 0 &&
1806 readvolfile(obj, &obj->options.uservol, ".applevolumes", 1, pwent) < 0 &&
1807 obj->options.defaultvol.name != NULL ) {
1808 if (readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent) < 0)
1809 creatvol(obj, pwent, pwent->pw_dir, NULL, NULL, 1);
1812 if ( obj->options.flags & OPTION_USERVOLFIRST ) {
1813 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent );
1816 if ( obj->options.closevol ) {
1819 for ( vol = Volumes; vol; vol = vol->v_next ) {
1820 if (vol->v_deleted && !vol->v_new ) {
1829 /* ------------------------------- */
1830 int afp_getsrvrparms(AFPObj *obj, char *ibuf _U_, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
1843 for ( vcnt = 0, volume = Volumes; volume; volume = volume->v_next ) {
1844 if (!(volume->v_flags & AFPVOL_NOSTAT)) {
1847 if ( stat( volume->v_path, &st ) < 0 ) {
1848 LOG(log_info, logtype_afpd, "afp_getsrvrparms(%s): stat: %s",
1849 volume->v_path, strerror(errno) );
1850 continue; /* can't access directory */
1852 if (!S_ISDIR(st.st_mode)) {
1853 continue; /* not a dir */
1855 accessmode(volume->v_path, &ma, NULL, &st);
1856 if ((ma.ma_user & (AR_UREAD | AR_USEARCH)) != (AR_UREAD | AR_USEARCH)) {
1857 continue; /* no r-x access */
1860 if (volume->v_hide) {
1861 continue; /* config file changed but the volume was mounted */
1864 if (utf8_encoding()) {
1865 len = ucs2_to_charset_allocate(CH_UTF8_MAC, &namebuf, volume->v_u8mname);
1867 len = ucs2_to_charset_allocate(obj->options.maccharset, &namebuf, volume->v_macname);
1870 if (len == (size_t)-1)
1873 /* set password bit if there's a volume password */
1874 *data = (volume->v_password) ? AFPSRVR_PASSWD : 0;
1876 /* Apple 2 clients running ProDOS-8 expect one volume to have
1877 bit 0 of this byte set. They will not recognize anything
1878 on the server unless this is the case. I have not
1879 completely worked this out, but it's related to booting
1880 from the server. Support for that function is a ways
1881 off.. <shirsch@ibm.net> */
1882 *data |= (volume->v_flags & AFPVOL_A2VOL) ? AFPSRVR_CONFIGINFO : 0;
1883 *data++ |= 0; /* UNIX PRIVS BIT ..., OSX doesn't seem to use it, so we don't either */
1885 memcpy(data, namebuf, len );
1891 *rbuflen = data - rbuf;
1893 if ( gettimeofday( &tv, NULL ) < 0 ) {
1894 LOG(log_error, logtype_afpd, "afp_getsrvrparms(%s): gettimeofday: %s", volume->v_path, strerror(errno) );
1896 return AFPERR_PARAM;
1898 tv.tv_sec = AD_DATE_FROM_UNIX(tv.tv_sec);
1899 memcpy(data, &tv.tv_sec, sizeof( u_int32_t));
1900 data += sizeof( u_int32_t);
1905 /* ------------------------- */
1906 static int volume_codepage(AFPObj *obj, struct vol *volume)
1908 struct charset_functions *charset;
1911 if (!volume->v_volcodepage)
1912 volume->v_volcodepage = strdup("UTF8");
1914 if ( (charset_t) -1 == ( volume->v_volcharset = add_charset(volume->v_volcodepage)) ) {
1915 LOG (log_error, logtype_afpd, "Setting codepage %s as volume codepage failed", volume->v_volcodepage);
1919 if ( NULL == (charset = find_charset_functions(volume->v_volcodepage)) || charset->flags & CHARSET_ICONV ) {
1920 LOG (log_warning, logtype_afpd, "WARNING: volume encoding %s is *not* supported by netatalk, expect problems !!!!", volume->v_volcodepage);
1923 if (!volume->v_maccodepage)
1924 volume->v_maccodepage = strdup(obj->options.maccodepage);
1926 if ( (charset_t) -1 == ( volume->v_maccharset = add_charset(volume->v_maccodepage)) ) {
1927 LOG (log_error, logtype_afpd, "Setting codepage %s as mac codepage failed", volume->v_maccodepage);
1931 if ( NULL == ( charset = find_charset_functions(volume->v_maccodepage)) || ! (charset->flags & CHARSET_CLIENT) ) {
1932 LOG (log_error, logtype_afpd, "Fatal error: mac charset %s not supported", volume->v_maccodepage);
1935 volume->v_kTextEncoding = htonl(charset->kTextEncoding);
1939 /* ------------------------- */
1940 static int volume_openDB(struct vol *volume)
1944 if ((volume->v_flags & AFPVOL_NODEV)) {
1945 flags |= CNID_FLAG_NODEV;
1948 if (volume->v_cnidscheme == NULL) {
1949 volume->v_cnidscheme = strdup(DEFAULT_CNID_SCHEME);
1950 LOG(log_info, logtype_afpd, "Volume %s use CNID scheme %s.",
1951 volume->v_path, volume->v_cnidscheme);
1954 LOG(log_info, logtype_afpd, "CNID server: %s:%s",
1955 volume->v_cnidserver ? volume->v_cnidserver : Cnid_srv,
1956 volume->v_cnidport ? volume->v_cnidport : Cnid_port);
1959 /* Found this in branch dir-rewrite, maybe we want to use it sometimes */
1961 /* Legacy pre 2.1 way of sharing eg CD-ROM */
1962 if (strcmp(volume->v_cnidscheme, "last") == 0) {
1963 /* "last" is gone. We support it by switching to in-memory "tdb" */
1964 volume->v_cnidscheme = strdup("tdb");
1965 flags |= CNID_FLAG_MEMORY;
1968 /* New way of sharing CD-ROM */
1969 if (volume->v_flags & AFPVOL_CDROM) {
1970 flags |= CNID_FLAG_MEMORY;
1971 if (strcmp(volume->v_cnidscheme, "tdb") != 0) {
1972 free(volume->v_cnidscheme);
1973 volume->v_cnidscheme = strdup("tdb");
1974 LOG(log_info, logtype_afpd, "Volume %s is ejectable, switching to scheme %s.",
1975 volume->v_path, volume->v_cnidscheme);
1980 volume->v_cdb = cnid_open(volume->v_path,
1982 volume->v_cnidscheme,
1984 volume->v_cnidserver ? volume->v_cnidserver : Cnid_srv,
1985 volume->v_cnidport ? volume->v_cnidport : Cnid_port);
1987 if ( ! volume->v_cdb && ! (flags & CNID_FLAG_MEMORY)) {
1988 /* The first attempt failed and it wasn't yet an attempt to open in-memory */
1989 LOG(log_error, logtype_afpd, "Can't open volume \"%s\" CNID backend \"%s\" ",
1990 volume->v_path, volume->v_cnidscheme);
1991 LOG(log_error, logtype_afpd, "Reopen volume %s using in memory temporary CNID DB.",
1993 flags |= CNID_FLAG_MEMORY;
1994 volume->v_cdb = cnid_open (volume->v_path, volume->v_umask, "tdb", flags, NULL, NULL);
1996 /* kill ourself with SIGUSR2 aka msg pending */
1997 if (volume->v_cdb) {
1998 setmessage("Something wrong with the volume's CNID DB, using temporary CNID DB instead."
1999 "Check server messages for details!");
2000 kill(getpid(), SIGUSR2);
2001 /* deactivate cnid caching/storing in AppleDouble files */
2002 volume->v_flags &= ~AFPVOL_CACHE;
2007 return (!volume->v_cdb)?-1:0;
2011 Check if the underlying filesystem supports EAs for ea:sys volumes.
2012 If not, switch to ea:ad.
2013 As we can't check (requires write access) on ro-volumes, we switch ea:auto
2014 volumes that are options:ro to ea:none.
2016 static void check_ea_sys_support(struct vol *vol)
2018 uid_t process_uid = 0;
2019 char eaname[] = {"org.netatalk.supports-eas.XXXXXX"};
2020 const char *eacontent = "yes";
2022 if (vol->v_vfs_ea == AFPVOL_EA_AUTO) {
2024 if ((vol->v_flags & AFPVOL_RO) == AFPVOL_RO) {
2025 LOG(log_info, logtype_logger, "read-only volume '%s', can't test for EA support, disabling EAs", vol->v_localname);
2026 vol->v_vfs_ea = AFPVOL_EA_NONE;
2032 process_uid = geteuid();
2034 if (seteuid(0) == -1) {
2035 LOG(log_error, logtype_logger, "check_ea_sys_support: can't seteuid(0): %s", strerror(errno));
2039 if ((sys_setxattr(vol->v_path, eaname, eacontent, 4, 0)) == 0) {
2040 sys_removexattr(vol->v_path, eaname);
2041 vol->v_vfs_ea = AFPVOL_EA_SYS;
2043 LOG(log_warning, logtype_afpd, "volume \"%s\" does not support Extended Attributes, using ea:ad instead",
2045 vol->v_vfs_ea = AFPVOL_EA_AD;
2049 if (seteuid(process_uid) == -1) {
2050 LOG(log_error, logtype_logger, "can't seteuid back %s", strerror(errno));
2057 /* -------------------------
2058 * we are the user here
2060 int afp_openvol(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2071 char path[ MAXPATHLEN + 1];
2077 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2078 bitmap = ntohs( bitmap );
2079 ibuf += sizeof( bitmap );
2082 if (( bitmap & (1<<VOLPBIT_VID)) == 0 ) {
2083 return AFPERR_BITMAP;
2086 len = (unsigned char)*ibuf++;
2087 volname = obj->oldtmp;
2089 if ((volname_tmp = strchr(volname,'+')) != NULL)
2090 volname = volname_tmp+1;
2092 if (utf8_encoding()) {
2093 namelen = convert_string(CH_UTF8_MAC, CH_UCS2, ibuf, len, volname, sizeof(obj->oldtmp));
2095 namelen = convert_string(obj->options.maccharset, CH_UCS2, ibuf, len, volname, sizeof(obj->oldtmp));
2098 if ( namelen <= 0) {
2099 return AFPERR_PARAM;
2103 if ((len + 1) & 1) /* pad to an even boundary */
2108 for ( volume = Volumes; volume; volume = volume->v_next ) {
2109 if ( strcasecmp_w( (ucs2_t*) volname, volume->v_name ) == 0 ) {
2114 if ( volume == NULL ) {
2115 return AFPERR_PARAM;
2118 /* check for a volume password */
2119 if (volume->v_password && strncmp(ibuf, volume->v_password, VOLPASSLEN)) {
2120 return AFPERR_ACCESS;
2123 if (( volume->v_flags & AFPVOL_OPEN ) ) {
2124 /* the volume is already open */
2126 set_uidgid ( volume );
2128 return stat_vol(bitmap, volume, rbuf, rbuflen);
2131 if (volume->v_root_preexec) {
2132 if ((ret = afprun(1, volume->v_root_preexec, NULL)) && volume->v_root_preexec_close) {
2133 LOG(log_error, logtype_afpd, "afp_openvol(%s): root preexec : %d", volume->v_path, ret );
2139 set_uidgid ( volume );
2142 if (volume->v_preexec) {
2143 if ((ret = afprun(0, volume->v_preexec, NULL)) && volume->v_preexec_close) {
2144 LOG(log_error, logtype_afpd, "afp_openvol(%s): preexec : %d", volume->v_path, ret );
2149 if ( stat( volume->v_path, &st ) < 0 ) {
2150 return AFPERR_PARAM;
2153 if ( chdir( volume->v_path ) < 0 ) {
2154 return AFPERR_PARAM;
2157 if ( NULL == getcwd(path, MAXPATHLEN)) {
2158 /* shouldn't be fatal but it will fail later */
2159 LOG(log_error, logtype_afpd, "afp_openvol(%s): volume pathlen too long", volume->v_path);
2163 /* Normalize volume path */
2164 #ifdef REALPATH_TAKES_NULL
2165 if ((volume->v_path = realpath(path, NULL)) == NULL)
2168 if ((volume->v_path = malloc(MAXPATHLEN+1)) == NULL)
2170 if (realpath(path, volume->v_path) == NULL) {
2171 free(volume->v_path);
2174 /* Safe some memory */
2176 if ((tmp = strdup(volume->v_path)) == NULL) {
2177 free(volume->v_path);
2180 free(volume->v_path);
2181 volume->v_path = tmp;
2184 if (volume_codepage(obj, volume) < 0) {
2189 /* initialize volume variables
2192 if (utf8_encoding()) {
2193 volume->max_filename = 255;
2196 volume->max_filename = MACFILELEN;
2199 volume->v_flags |= AFPVOL_OPEN;
2200 volume->v_cdb = NULL;
2202 if (utf8_encoding()) {
2203 len = convert_string_allocate(CH_UCS2, CH_UTF8_MAC, volume->v_u8mname, namelen, &vol_mname);
2205 len = convert_string_allocate(CH_UCS2, obj->options.maccharset, volume->v_macname, namelen, &vol_mname);
2207 if ( !vol_mname || len <= 0) {
2212 if ((vol_uname = strrchr(path, '/')) == NULL)
2214 else if (*(vol_uname + 1) != '\0')
2217 if ((dir = dir_new(vol_mname,
2222 bfromcstr(volume->v_path),
2226 LOG(log_error, logtype_afpd, "afp_openvol(%s): malloc: %s", volume->v_path, strerror(errno) );
2231 volume->v_root = dir;
2234 if (volume_openDB(volume) < 0) {
2235 LOG(log_error, logtype_afpd, "Fatal error: cannot open CNID or invalid CNID backend for %s: %s",
2236 volume->v_path, volume->v_cnidscheme);
2241 ret = stat_vol(bitmap, volume, rbuf, rbuflen);
2242 if (ret == AFP_OK) {
2244 if (!(volume->v_flags & AFPVOL_RO)) {
2245 handle_special_folders( volume );
2247 volume->v_cnidserver ? volume->v_cnidserver : Cnid_srv,
2248 volume->v_cnidport ? volume->v_cnidport : Cnid_port);
2252 * If you mount a volume twice, the second time the trash appears on
2253 * the desk-top. That's because the Mac remembers the DID for the
2254 * trash (even for volumes in different zones, on different servers).
2255 * Just so this works better, we prime the DID cache with the trash,
2256 * fixing the trash at DID 17.
2257 * FIXME (RL): should it be done inside a CNID backend ? (always returning Trash DID when asked) ?
2259 if ((volume->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
2261 /* FIXME find db time stamp */
2262 if (cnid_getstamp(volume->v_cdb, volume->v_stamp, sizeof(volume->v_stamp)) < 0) {
2263 LOG (log_error, logtype_afpd,
2264 "afp_openvol(%s): Fatal error: Unable to get stamp value from CNID backend",
2272 cname( volume, volume->v_root, &p );
2278 if (volume->v_root) {
2279 dir_free( volume->v_root );
2280 volume->v_root = NULL;
2283 volume->v_flags &= ~AFPVOL_OPEN;
2284 if (volume->v_cdb != NULL) {
2285 cnid_close(volume->v_cdb);
2286 volume->v_cdb = NULL;
2292 /* ------------------------- */
2293 static void closevol(struct vol *vol)
2298 dir_free( vol->v_root );
2300 if (vol->v_cdb != NULL) {
2301 cnid_close(vol->v_cdb);
2305 if (vol->v_postexec) {
2306 afprun(0, vol->v_postexec, NULL);
2308 if (vol->v_root_postexec) {
2309 afprun(1, vol->v_root_postexec, NULL);
2313 /* ------------------------- */
2314 void close_all_vol(void)
2318 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
2319 if ( (ovol->v_flags & AFPVOL_OPEN) ) {
2320 ovol->v_flags &= ~AFPVOL_OPEN;
2326 /* ------------------------- */
2327 static void deletevol(struct vol *vol)
2331 vol->v_flags &= ~AFPVOL_OPEN;
2332 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
2333 if ( (ovol->v_flags & AFPVOL_OPEN) ) {
2337 if ( ovol != NULL ) {
2338 /* Even if chdir fails, we can't say afp_closevol fails. */
2339 if ( chdir( ovol->v_path ) == 0 ) {
2340 curdir = ovol->v_root;
2345 if (vol->v_deleted) {
2346 showvol(vol->v_name);
2353 /* ------------------------- */
2354 int afp_closevol(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2361 memcpy(&vid, ibuf, sizeof( vid ));
2362 if (NULL == ( vol = getvolbyvid( vid )) ) {
2363 return( AFPERR_PARAM );
2372 /* ------------------------- */
2373 struct vol *getvolbyvid(const u_int16_t vid )
2377 for ( vol = Volumes; vol; vol = vol->v_next ) {
2378 if ( vid == vol->v_vid ) {
2382 if ( vol == NULL || ( vol->v_flags & AFPVOL_OPEN ) == 0 ) {
2388 #endif /* FORCE_UIDGID */
2395 /* ------------------------ */
2396 static int ext_cmp_key(const void *key, const void *obj)
2398 const char *p = key;
2399 const struct extmap *em = obj;
2400 return strdiacasecmp(p, em->em_ext);
2402 struct extmap *getextmap(const char *path)
2407 if (!Extmap_cnt || NULL == ( p = strrchr( path, '.' )) ) {
2408 return( Defextmap );
2412 return( Defextmap );
2414 em = bsearch(p, Extmap, Extmap_cnt, sizeof(struct extmap), ext_cmp_key);
2418 return( Defextmap );
2422 /* ------------------------- */
2423 struct extmap *getdefextmap(void)
2425 return( Defextmap );
2428 /* --------------------------
2429 poll if a volume is changed by other processes.
2431 0 no attention msg sent
2432 1 attention msg sent
2433 -1 error (socket closed)
2435 Note: if attention return -1 no packet has been
2436 sent because the buffer is full, we don't care
2437 either there's no reader or there's a lot of
2438 traffic and another pollvoltime will follow
2440 int pollvoltime(AFPObj *obj)
2447 if (!(afp_version > 21 && obj->options.server_notif))
2450 if ( gettimeofday( &tv, NULL ) < 0 )
2453 for ( vol = Volumes; vol; vol = vol->v_next ) {
2454 if ( (vol->v_flags & AFPVOL_OPEN) && vol->v_mtime + 30 < tv.tv_sec) {
2455 if ( !stat( vol->v_path, &st ) && vol->v_mtime != st.st_mtime ) {
2456 vol->v_mtime = st.st_mtime;
2457 if (!obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED))
2466 /* ------------------------- */
2467 void setvoltime(AFPObj *obj, struct vol *vol)
2471 /* just looking at vol->v_mtime is broken seriously since updates
2472 * from other users afpd processes never are seen.
2473 * This is not the most elegant solution (a shared memory between
2474 * the afpd processes would come closer)
2477 if ( gettimeofday( &tv, NULL ) < 0 ) {
2478 LOG(log_error, logtype_afpd, "setvoltime(%s): gettimeofday: %s", vol->v_path, strerror(errno) );
2481 if( utime( vol->v_path, NULL ) < 0 ) {
2482 /* write of time failed ... probably a read only filesys,
2483 * where no other users can interfere, so there's no issue here
2487 /* a little granularity */
2488 if (vol->v_mtime < tv.tv_sec) {
2489 vol->v_mtime = tv.tv_sec;
2490 /* or finder doesn't update free space
2491 * AFP 3.2 and above clients seem to be ok without so many notification
2493 if (afp_version < 32 && obj->options.server_notif) {
2494 obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED);
2499 /* ------------------------- */
2500 int afp_getvolparams(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_,char *rbuf, size_t *rbuflen)
2503 u_int16_t vid, bitmap;
2506 memcpy(&vid, ibuf, sizeof( vid ));
2507 ibuf += sizeof( vid );
2508 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2509 bitmap = ntohs( bitmap );
2511 if (NULL == ( vol = getvolbyvid( vid )) ) {
2513 return( AFPERR_PARAM );
2516 return stat_vol(bitmap, vol, rbuf, rbuflen);
2519 /* ------------------------- */
2520 int afp_setvolparams(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2524 u_int16_t vid, bitmap;
2530 memcpy(&vid, ibuf, sizeof( vid ));
2531 ibuf += sizeof( vid );
2532 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2533 bitmap = ntohs( bitmap );
2534 ibuf += sizeof(bitmap);
2536 if (( vol = getvolbyvid( vid )) == NULL ) {
2537 return( AFPERR_PARAM );
2540 if ((vol->v_flags & AFPVOL_RO))
2541 return AFPERR_VLOCK;
2543 /* we can only set the backup date. */
2544 if (bitmap != (1 << VOLPBIT_BDATE))
2545 return AFPERR_BITMAP;
2547 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2548 if ( ad_open( vol->v_path, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR,
2551 return AFPERR_VLOCK;
2553 return AFPERR_ACCESS;
2556 memcpy(&aint, ibuf, sizeof(aint));
2557 ad_setdate(&ad, AD_DATE_BACKUP, aint);
2559 ad_close(&ad, ADFLAGS_HF);
2563 /* ------------------------- */
2564 int wincheck(const struct vol *vol, const char *path)
2568 if (!(vol->v_flags & AFPVOL_MSWINDOWS))
2571 /* empty paths are not allowed */
2572 if ((len = strlen(path)) == 0)
2575 /* leading or trailing whitespaces are not allowed, carriage returns
2576 * and probably other whitespace is okay, tabs are not allowed
2578 if ((path[0] == ' ') || (path[len-1] == ' '))
2581 /* certain characters are not allowed */
2582 if (strpbrk(path, MSWINDOWS_BADCHARS))
2585 /* everything else is okay */
2591 * precreate a folder
2592 * this is only intended for folders in the volume root
2593 * It will *not* work if the folder name contains extended characters
2595 static int create_special_folder (const struct vol *vol, const struct _special_folder *folder)
2604 p = (char *) malloc ( strlen(vol->v_path)+strlen(folder->name)+2);
2606 LOG(log_error, logtype_afpd,"malloc failed");
2610 q=strdup(folder->name);
2612 LOG(log_error, logtype_afpd,"malloc failed");
2616 strcpy(p, vol->v_path);
2621 if ((vol->v_casefold & AFPVOL_MTOUUPPER))
2623 else if ((vol->v_casefold & AFPVOL_MTOULOWER))
2629 if ( (ret = stat( p, &st )) < 0 ) {
2630 if (folder->precreate) {
2631 if (ad_mkdir(p, folder->mode)) {
2632 LOG(log_debug, logtype_afpd,"Creating '%s' failed in %s: %s", p, vol->v_path, strerror(errno));
2641 if ( !ret && folder->hide) {
2643 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2644 if (ad_open_metadata( p, ADFLAGS_DIR, O_CREAT, &ad) < 0) {
2650 ad_setname(&ad, folder->name);
2652 ad_getattr(&ad, &attr);
2653 attr |= htons( ntohs( attr ) | ATTRBIT_INVISIBLE );
2654 ad_setattr(&ad, attr);
2656 /* do the same with the finder info */
2657 if (ad_entry(&ad, ADEID_FINDERI)) {
2658 memcpy(&attr, ad_entry(&ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF, sizeof(attr));
2659 attr |= htons(FINDERINFO_INVISIBLE);
2660 memcpy(ad_entry(&ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF,&attr, sizeof(attr));
2664 ad_close_metadata( &ad);
2671 static void handle_special_folders (const struct vol * vol)
2673 const _special_folder *p = &special_folders[0];
2675 if ((vol->v_flags & AFPVOL_RO))
2678 for (; p->name != NULL; p++) {
2679 create_special_folder (vol, p);
2683 const struct vol *getvolumes(void)
2688 void unload_volumes_and_extmap(void)
2690 LOG(log_debug, logtype_afpd, "unload_volumes_and_extmap");
2696 * Get a volumes UUID from the config file.
2697 * If there is none, it is generated and stored there.
2699 * Returns pointer to allocated storage on success, NULL on error.
2701 char *get_uuid(const AFPObj *obj, const char *volname)
2704 char buf[1024], uuid[UUID_PRINTABLE_STRING_LENGTH], *p;
2706 struct stat tmpstat;
2709 if ((fp = fopen(obj->options.uuidconf, "r")) != NULL) { /* read open? */
2710 /* scan in the conf file */
2711 while (fgets(buf, sizeof(buf), fp) != NULL) {
2713 while (p && isblank(*p))
2715 if (!p || (*p == '#') || (*p == '\n'))
2716 continue; /* invalid line */
2719 if ((volname_conf = strtok( p, "\"" )) == NULL)
2720 continue; /* syntax error */
2722 if ((volname_conf = strtok( p, " \t" )) == NULL)
2723 continue; /* syntax error: invalid name */
2725 p = strchr(p, '\0');
2728 continue; /* syntax error */
2730 if (strcmp(volname, volname_conf) != 0)
2731 continue; /* another volume name */
2733 while (p && isblank(*p))
2736 if (sscanf(p, "%36s", uuid) == 1 ) {
2737 for (int i=0; uuid[i]; i++)
2738 uuid[i] = toupper(uuid[i]);
2739 LOG(log_debug, logtype_afpd, "get_uuid('%s'): UUID: '%s'", volname, uuid);
2741 return strdup(uuid);
2749 /* not found or no file, reopen in append mode */
2751 if (stat(obj->options.uuidconf, &tmpstat)) { /* no file */
2752 if (( fd = creat(obj->options.uuidconf, 0644 )) < 0 ) {
2753 LOG(log_error, logtype_atalkd, "ERROR: Cannot create %s (%s).",
2754 obj->options.uuidconf, strerror(errno));
2757 if (( fp = fdopen( fd, "w" )) == NULL ) {
2758 LOG(log_error, logtype_atalkd, "ERROR: Cannot fdopen %s (%s).",
2759 obj->options.uuidconf, strerror(errno));
2763 } else if ((fp = fopen(obj->options.uuidconf, "a+")) == NULL) { /* not found */
2764 LOG(log_error, logtype_afpd, "Cannot create or append to %s (%s).",
2765 obj->options.uuidconf, strerror(errno));
2768 fseek(fp, 0L, SEEK_END);
2769 if(ftell(fp) == 0) { /* size = 0 */
2770 fprintf(fp, "# DON'T TOUCH NOR COPY THOUGHTLESSLY!\n");
2771 fprintf(fp, "# This file is auto-generated by afpd\n");
2772 fprintf(fp, "# and stores UUIDs for TM volumes.\n\n");
2774 fseek(fp, -1L, SEEK_END);
2775 if(fgetc(fp) != '\n') fputc('\n', fp); /* last char is \n? */
2778 /* generate uuid and write to file */
2781 randombytes((void *)id, 16);
2782 cp = uuid_bin2string(id);
2784 LOG(log_debug, logtype_afpd, "get_uuid('%s'): generated UUID '%s'", volname, cp);
2786 fprintf(fp, "\"%s\"\t%36s\n", volname, cp);
2789 return strdup(uuid);