2 * Copyright (c) 1990,1993 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
8 #endif /* HAVE_CONFIG_H */
21 #include <sys/param.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
26 #include <atalk/asp.h>
27 #include <atalk/dsi.h>
28 #include <atalk/adouble.h>
29 #include <atalk/afp.h>
30 #include <atalk/util.h>
31 #include <atalk/volinfo.h>
32 #include <atalk/logger.h>
33 #include <atalk/vfs.h>
34 #include <atalk/uuid.h>
38 #include <atalk/cnid.h>
42 #include "directory.h"
51 extern int afprun(int root, char *cmd, int *outfd);
54 #define MIN(a, b) ((a) < (b) ? (a) : (b))
57 #ifndef NO_LARGE_VOL_SUPPORT
58 #if BYTE_ORDER == BIG_ENDIAN
61 #else /* BYTE_ORDER == BIG_ENDIAN */
62 #define hton64(x) ((u_int64_t) (htonl(((x) >> 32) & 0xffffffffLL)) | \
63 (u_int64_t) ((htonl(x) & 0xffffffffLL) << 32))
64 #define ntoh64(x) (hton64(x))
65 #endif /* BYTE_ORDER == BIG_ENDIAN */
66 #endif /* ! NO_LARGE_VOL_SUPPORT */
68 #ifndef UUID_PRINTABLE_STRING_LENGTH
69 #define UUID_PRINTABLE_STRING_LENGTH 37
73 struct vol *current_vol; /* last volume from getvolbyvid() */
75 static struct vol *Volumes = NULL;
76 static u_int16_t lastvid = 0;
77 static char *Trash = "\02\024Network Trash Folder";
79 static struct extmap *Extmap = NULL, *Defextmap = NULL;
80 static int Extmap_cnt;
81 static void free_extmap(void);
83 #define VOLOPT_ALLOW 0 /* user allow list */
84 #define VOLOPT_DENY 1 /* user deny list */
85 #define VOLOPT_RWLIST 2 /* user rw list */
86 #define VOLOPT_ROLIST 3 /* user ro list */
87 #define VOLOPT_PASSWORD 4 /* volume password */
88 #define VOLOPT_CASEFOLD 5 /* character case mangling */
89 #define VOLOPT_FLAGS 6 /* various flags */
90 #define VOLOPT_DBPATH 7 /* path to database */
91 #define VOLOPT_LIMITSIZE 8 /* Limit the size of the volume */
93 #define VOLOPT_VETO 10 /* list of veto filespec */
94 #define VOLOPT_PREEXEC 11 /* preexec command */
95 #define VOLOPT_ROOTPREEXEC 12 /* root preexec command */
96 #define VOLOPT_POSTEXEC 13 /* postexec command */
97 #define VOLOPT_ROOTPOSTEXEC 14 /* root postexec command */
98 #define VOLOPT_ENCODING 15 /* mac encoding (pre OSX)*/
99 #define VOLOPT_MACCHARSET 16
100 #define VOLOPT_CNIDSCHEME 17
101 #define VOLOPT_ADOUBLE 18 /* adouble version */
107 #define VOLOPT_FORCEUID 19 /* force uid for username x */
108 #define VOLOPT_FORCEGID 20 /* force gid for group x */
109 #endif /* FORCE_UIDGID */
111 #define VOLOPT_UMASK 21
112 #define VOLOPT_ALLOWED_HOSTS 22
113 #define VOLOPT_DENIED_HOSTS 23
114 #define VOLOPT_DPERM 24 /* dperm default directories perms */
115 #define VOLOPT_FPERM 25 /* fperm default files perms */
116 #define VOLOPT_DFLTPERM 26 /* perm */
117 #define VOLOPT_EA_VFS 27 /* Extended Attributes vfs indirection */
118 #define VOLOPT_CNIDSERVER 28 /* CNID Server ip address*/
119 #define VOLOPT_CNIDPORT 30 /* CNID server tcp port */
121 #define VOLOPT_MAX 31 /* <== IMPORTANT !!!!!! */
122 #define VOLOPT_NUM (VOLOPT_MAX + 1)
125 #define VOLOPT_DEFAULT ":DEFAULT:"
126 #define VOLOPT_DEFAULT_LEN 9
133 typedef struct _special_folder {
140 static const _special_folder special_folders[] = {
141 {"Network Trash Folder", 1, 0777, 1},
142 {".AppleDesktop", 1, 0777, 0},
145 /* Forward declarations */
146 static void handle_special_folders (const struct vol *);
147 static void deletevol(struct vol *vol);
148 static void volume_free(struct vol *vol);
149 static void check_ea_sys_support(struct vol *vol);
151 static void volfree(struct vol_option *options, const struct vol_option *save)
156 for (i = 0; i < VOLOPT_MAX; i++) {
157 if (options[i].c_value && (options[i].c_value != save[i].c_value))
158 free(options[i].c_value);
161 for (i = 0; i < VOLOPT_MAX; i++) {
162 if (options[i].c_value)
163 free(options[i].c_value);
169 #define is_var(a, b) (strncmp((a), (b), 2) == 0)
172 * Handle variable substitutions. here's what we understand:
173 * $b -> basename of path
174 * $c -> client ip/appletalk address
175 * $d -> volume pathname on server
176 * $f -> full name (whatever's in the gecos field)
179 * $i -> client ip/appletalk address without port
180 * $s -> server name (hostname if it doesn't exist)
181 * $u -> username (guest is usually nobody)
182 * $v -> volume name or basename if null
183 * $z -> zone (may not exist)
186 * This get's called from readvolfile with
187 * path = NULL, volname = NULL for xlating the volumes path
188 * path = path, volname = NULL for xlating the volumes name
189 * ... and from volumes options parsing code when xlating eg dbpath with
190 * path = path, volname = volname
192 * Using this information we can reject xlation of any variable depeninding on a login
193 * context which is not given in the afp master, where we must evaluate this whole stuff
194 * too for the Zeroconf announcements.
196 static char *volxlate(AFPObj *obj,
209 int xlatevolname = 0;
211 if (! ((DSI *)obj->handle)->child)
214 if (path && !volname)
222 dest = calloc(destlen +1, 1);
228 strlcpy(dest, src, destlen +1);
229 if ((p = strchr(src, '$')) == NULL) /* nothing to do */
232 /* first part of the path. just forward to the next variable. */
233 len = MIN((size_t)(p - src), destlen);
239 while (p && destlen > 0) {
240 /* now figure out what the variable is */
242 if (is_var(p, "$b")) {
243 if (afpmaster && xlatevolname)
246 if ((q = strrchr(path, '/')) == NULL)
248 else if (*(q + 1) != '\0')
251 } else if (is_var(p, "$c")) {
252 if (afpmaster && xlatevolname)
254 if (obj->proto == AFPPROTO_ASP) {
255 ASP asp = obj->handle;
257 len = sprintf(dest, "%u.%u", ntohs(asp->asp_sat.sat_addr.s_net),
258 asp->asp_sat.sat_addr.s_node);
262 } else if (obj->proto == AFPPROTO_DSI) {
263 DSI *dsi = obj->handle;
264 len = sprintf(dest, "%s:%u",
265 getip_string((struct sockaddr *)&dsi->client),
266 getip_port((struct sockaddr *)&dsi->client));
270 } else if (is_var(p, "$d")) {
271 if (afpmaster && xlatevolname)
274 } else if (pwd && is_var(p, "$f")) {
275 if (afpmaster && xlatevolname)
277 if ((r = strchr(pwd->pw_gecos, ',')))
280 } else if (pwd && is_var(p, "$g")) {
281 if (afpmaster && xlatevolname)
283 struct group *grp = getgrgid(pwd->pw_gid);
286 } else if (is_var(p, "$h")) {
287 q = obj->options.hostname;
288 } else if (is_var(p, "$i")) {
289 if (afpmaster && xlatevolname)
291 if (obj->proto == AFPPROTO_ASP) {
292 ASP asp = obj->handle;
294 len = sprintf(dest, "%u", ntohs(asp->asp_sat.sat_addr.s_net));
298 } else if (obj->proto == AFPPROTO_DSI) {
299 DSI *dsi = obj->handle;
300 q = getip_string((struct sockaddr *)&dsi->client);
302 } else if (is_var(p, "$s")) {
305 else if (obj->options.server) {
306 q = obj->options.server;
308 q = obj->options.hostname;
309 } else if (obj->username && is_var(p, "$u")) {
310 if (afpmaster && xlatevolname)
313 if ( obj->options.ntseparator && (sep = strchr(obj->username, obj->options.ntseparator[0])) != NULL)
317 } else if (is_var(p, "$v")) {
318 if (afpmaster && xlatevolname)
324 if ((q = strrchr(path, '/')) == NULL)
326 else if (*(q + 1) != '\0')
329 } else if (is_var(p, "$z")) {
331 } else if (is_var(p, "$$")) {
336 /* copy the stuff over. if we don't understand something that we
337 * should, just skip it over. */
339 len = MIN(p == q ? 2 : strlen(q), destlen);
340 strncpy(dest, q, len);
345 /* stuff up to next $ */
347 p = strchr(src, '$');
348 len = p ? MIN((size_t)(p - src), destlen) : destlen;
350 strncpy(dest, src, len);
358 /* to make sure that val is valid, make sure to select an opt that
360 static int optionok(const char *buf, const char *opt, const char *val)
362 if (!strstr(buf,opt))
370 /* -------------------- */
371 static void setoption(struct vol_option *options, struct vol_option *save, int opt, const char *val)
373 if (options[opt].c_value && (!save || options[opt].c_value != save[opt].c_value))
374 free(options[opt].c_value);
375 options[opt].c_value = strdup(val + 1);
378 /* ------------------------------------------
379 handle all the options. tmp can't be NULL. */
380 static void volset(struct vol_option *options, struct vol_option *save,
381 char *volname, int vlen,
386 val = strchr(tmp, ':');
388 /* we'll assume it's a volume name. */
389 strncpy(volname, tmp, vlen);
394 LOG(log_debug, logtype_afpd, "Parsing volset %s", val);
396 if (optionok(tmp, "allow:", val)) {
397 setoption(options, save, VOLOPT_ALLOW, val);
399 } else if (optionok(tmp, "deny:", val)) {
400 setoption(options, save, VOLOPT_DENY, val);
402 } else if (optionok(tmp, "rwlist:", val)) {
403 setoption(options, save, VOLOPT_RWLIST, val);
405 } else if (optionok(tmp, "rolist:", val)) {
406 setoption(options, save, VOLOPT_ROLIST, val);
408 } else if (optionok(tmp, "codepage:", val)) {
409 LOG (log_error, logtype_afpd, "The old codepage system has been removed. Please make sure to read the documentation !!!!");
410 /* Make sure we don't screw anything */
412 } else if (optionok(tmp, "volcharset:", val)) {
413 setoption(options, save, VOLOPT_ENCODING, val);
414 } else if (optionok(tmp, "maccharset:", val)) {
415 setoption(options, save, VOLOPT_MACCHARSET, val);
416 } else if (optionok(tmp, "veto:", val)) {
417 setoption(options, save, VOLOPT_VETO, val);
418 } else if (optionok(tmp, "cnidscheme:", val)) {
419 setoption(options, save, VOLOPT_CNIDSCHEME, val);
420 } else if (optionok(tmp, "casefold:", val)) {
421 if (strcasecmp(val + 1, "tolower") == 0)
422 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMLOWER;
423 else if (strcasecmp(val + 1, "toupper") == 0)
424 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMUPPER;
425 else if (strcasecmp(val + 1, "xlatelower") == 0)
426 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UUPPERMLOWER;
427 else if (strcasecmp(val + 1, "xlateupper") == 0)
428 options[VOLOPT_CASEFOLD].i_value = AFPVOL_ULOWERMUPPER;
429 } else if (optionok(tmp, "adouble:", val)) {
430 if (strcasecmp(val + 1, "v2") == 0)
431 options[VOLOPT_ADOUBLE].i_value = AD_VERSION2;
432 else if (strcasecmp(val + 1, "ea") == 0)
433 options[VOLOPT_ADOUBLE].i_value = AD_VERSION_EA;
434 } else if (optionok(tmp, "options:", val)) {
437 if ((p = strtok(val + 1, ",")) == NULL) /* nothing */
441 if (strcasecmp(p, "prodos") == 0)
442 options[VOLOPT_FLAGS].i_value |= AFPVOL_A2VOL;
443 else if (strcasecmp(p, "mswindows") == 0) {
444 options[VOLOPT_FLAGS].i_value |= AFPVOL_MSWINDOWS | AFPVOL_USEDOTS;
445 } else if (strcasecmp(p, "crlf") == 0)
446 options[VOLOPT_FLAGS].i_value |= AFPVOL_CRLF;
447 else if (strcasecmp(p, "noadouble") == 0)
448 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOADOUBLE;
449 else if (strcasecmp(p, "ro") == 0)
450 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
451 else if (strcasecmp(p, "nohex") == 0)
452 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOHEX;
453 else if (strcasecmp(p, "usedots") == 0)
454 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS;
455 else if (strcasecmp(p, "invisibledots") == 0)
456 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS | AFPVOL_INV_DOTS;
457 else if (strcasecmp(p, "limitsize") == 0)
458 options[VOLOPT_FLAGS].i_value |= AFPVOL_LIMITSIZE;
459 /* support for either "dropbox" or "dropkludge" */
460 else if (strcasecmp(p, "dropbox") == 0)
461 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
462 else if (strcasecmp(p, "dropkludge") == 0)
463 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
464 else if (strcasecmp(p, "nofileid") == 0)
465 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOFILEID;
466 else if (strcasecmp(p, "nostat") == 0)
467 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOSTAT;
468 else if (strcasecmp(p, "preexec_close") == 0)
469 options[VOLOPT_PREEXEC].i_value = 1;
470 else if (strcasecmp(p, "root_preexec_close") == 0)
471 options[VOLOPT_ROOTPREEXEC].i_value = 1;
472 else if (strcasecmp(p, "upriv") == 0)
473 options[VOLOPT_FLAGS].i_value |= AFPVOL_UNIX_PRIV;
474 else if (strcasecmp(p, "nodev") == 0)
475 options[VOLOPT_FLAGS].i_value |= AFPVOL_NODEV;
476 else if (strcasecmp(p, "caseinsensitive") == 0)
477 options[VOLOPT_FLAGS].i_value |= AFPVOL_CASEINSEN;
478 else if (strcasecmp(p, "illegalseq") == 0)
479 options[VOLOPT_FLAGS].i_value |= AFPVOL_EILSEQ;
480 else if (strcasecmp(p, "nocnidcache") == 0)
481 options[VOLOPT_FLAGS].i_value &= ~AFPVOL_CACHE;
482 else if (strcasecmp(p, "tm") == 0)
483 options[VOLOPT_FLAGS].i_value |= AFPVOL_TM;
484 else if (strcasecmp(p, "searchdb") == 0)
485 options[VOLOPT_FLAGS].i_value |= AFPVOL_SEARCHDB;
486 /* Found this in branch dir-rewrite, maybe we want to use it sometimes */
488 else if (strcasecmp(p, "cdrom") == 0)
489 options[VOLOPT_FLAGS].i_value |= AFPVOL_CDROM | AFPVOL_RO;
491 p = strtok(NULL, ",");
494 } else if (optionok(tmp, "cnidserver:", val)) {
495 setoption(options, save, VOLOPT_CNIDSERVER, val);
497 char *p = strrchr(val + 1, ':');
500 setoption(options, save, VOLOPT_CNIDPORT, p);
503 LOG(log_debug, logtype_afpd, "CNID Server for volume '%s': %s:%s",
504 volname, val + 1, p ? p + 1 : Cnid_port);
506 } else if (optionok(tmp, "dbpath:", val)) {
507 setoption(options, save, VOLOPT_DBPATH, val);
509 } else if (optionok(tmp, "umask:", val)) {
510 options[VOLOPT_UMASK].i_value = (int)strtol(val +1, NULL, 8);
511 } else if (optionok(tmp, "dperm:", val)) {
512 options[VOLOPT_DPERM].i_value = (int)strtol(val+1, NULL, 8);
513 } else if (optionok(tmp, "fperm:", val)) {
514 options[VOLOPT_FPERM].i_value = (int)strtol(val+1, NULL, 8);
515 } else if (optionok(tmp, "perm:", val)) {
516 options[VOLOPT_DFLTPERM].i_value = (int)strtol(val+1, NULL, 8);
517 } else if (optionok(tmp, "password:", val)) {
518 setoption(options, save, VOLOPT_PASSWORD, val);
522 /* this code allows forced uid/gid per volume settings */
523 } else if (optionok(tmp, "forceuid:", val)) {
524 setoption(options, save, VOLOPT_FORCEUID, val);
525 } else if (optionok(tmp, "forcegid:", val)) {
526 setoption(options, save, VOLOPT_FORCEGID, val);
528 #endif /* FORCE_UIDGID */
529 } else if (optionok(tmp, "root_preexec:", val)) {
530 setoption(options, save, VOLOPT_ROOTPREEXEC, val);
532 } else if (optionok(tmp, "preexec:", val)) {
533 setoption(options, save, VOLOPT_PREEXEC, val);
535 } else if (optionok(tmp, "root_postexec:", val)) {
536 setoption(options, save, VOLOPT_ROOTPOSTEXEC, val);
538 } else if (optionok(tmp, "postexec:", val)) {
539 setoption(options, save, VOLOPT_POSTEXEC, val);
541 } else if (optionok(tmp, "allowed_hosts:", val)) {
542 setoption(options, save, VOLOPT_ALLOWED_HOSTS, val);
544 } else if (optionok(tmp, "denied_hosts:", val)) {
545 setoption(options, save, VOLOPT_DENIED_HOSTS, val);
547 } else if (optionok(tmp, "ea:", val)) {
548 if (strcasecmp(val + 1, "ad") == 0)
549 options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_AD;
550 else if (strcasecmp(val + 1, "sys") == 0)
551 options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_SYS;
552 else if (strcasecmp(val + 1, "none") == 0)
553 options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_NONE;
555 } else if (optionok(tmp, "volsizelimit:", val)) {
556 options[VOLOPT_LIMITSIZE].i_value = (uint32_t)strtoul(val + 1, NULL, 10);
559 /* ignore unknown options */
560 LOG(log_debug, logtype_afpd, "ignoring unknown volume option: %s", tmp);
565 /* ----------------- */
566 static void showvol(const ucs2_t *name)
569 for ( volume = Volumes; volume; volume = volume->v_next ) {
570 if (volume->v_hide && !strcasecmp_w( volume->v_name, name ) ) {
577 /* ------------------------------- */
578 static int creatvol(AFPObj *obj, struct passwd *pwd,
579 char *path, char *name,
580 struct vol_option *options,
581 const int user /* user defined volume */
585 int suffixlen, vlen, tmpvlen, u8mvlen, macvlen;
587 char tmpname[AFPVOL_U8MNAMELEN+1];
588 ucs2_t u8mtmpname[(AFPVOL_U8MNAMELEN+1)*2], mactmpname[(AFPVOL_MACNAMELEN+1)*2];
589 char suffix[6]; /* max is #FFFF */
592 LOG(log_debug, logtype_afpd, "createvol: Volume '%s'", name);
594 if ( name == NULL || *name == '\0' ) {
595 if ((name = strrchr( path, '/' )) == NULL) {
596 return -1; /* Obviously not a fully qualified path */
599 /* if you wish to share /, you need to specify a name. */
604 /* suffix for mangling use (lastvid + 1) */
605 /* because v_vid has not been decided yet. */
606 suffixlen = sprintf(suffix, "%c%X", MANGLE_CHAR, lastvid + 1 );
608 vlen = strlen( name );
610 /* Unicode Volume Name */
611 /* Firstly convert name from unixcharset to UTF8-MAC */
613 tmpvlen = convert_charset(obj->options.unixcharset, CH_UTF8_MAC, 0, name, vlen, tmpname, AFPVOL_U8MNAMELEN, &flags);
615 strcpy(tmpname, "???");
619 /* Do we have to mangle ? */
620 if ( (flags & CONV_REQMANGLE) || (tmpvlen > obj->options.volnamelen)) {
621 if (tmpvlen + suffixlen > obj->options.volnamelen) {
623 tmpvlen = convert_charset(obj->options.unixcharset, CH_UTF8_MAC, 0, name, vlen, tmpname, obj->options.volnamelen - suffixlen, &flags);
624 tmpname[tmpvlen >= 0 ? tmpvlen : 0] = 0;
626 strcat(tmpname, suffix);
627 tmpvlen = strlen(tmpname);
630 /* Secondly convert name from UTF8-MAC to UCS2 */
631 if ( 0 >= ( u8mvlen = convert_string(CH_UTF8_MAC, CH_UCS2, tmpname, tmpvlen, u8mtmpname, AFPVOL_U8MNAMELEN*2)) )
634 LOG(log_maxdebug, logtype_afpd, "createvol: Volume '%s' -> UTF8-MAC Name: '%s'", name, tmpname);
636 /* Maccharset Volume Name */
637 /* Firsty convert name from unixcharset to maccharset */
639 tmpvlen = convert_charset(obj->options.unixcharset, obj->options.maccharset, 0, name, vlen, tmpname, AFPVOL_U8MNAMELEN, &flags);
641 strcpy(tmpname, "???");
645 /* Do we have to mangle ? */
646 if ( (flags & CONV_REQMANGLE) || (tmpvlen > AFPVOL_MACNAMELEN)) {
647 if (tmpvlen + suffixlen > AFPVOL_MACNAMELEN) {
649 tmpvlen = convert_charset(obj->options.unixcharset, obj->options.maccharset, 0, name, vlen, tmpname, AFPVOL_MACNAMELEN - suffixlen, &flags);
650 tmpname[tmpvlen >= 0 ? tmpvlen : 0] = 0;
652 strcat(tmpname, suffix);
653 tmpvlen = strlen(tmpname);
656 /* Secondly convert name from maccharset to UCS2 */
657 if ( 0 >= ( macvlen = convert_string(obj->options.maccharset, CH_UCS2, tmpname, tmpvlen, mactmpname, AFPVOL_U8MNAMELEN*2)) )
660 LOG(log_maxdebug, logtype_afpd, "createvol: Volume '%s' -> Longname: '%s'", name, tmpname);
662 /* check duplicate */
663 for ( volume = Volumes; volume; volume = volume->v_next ) {
664 if (( strcasecmp_w( volume->v_u8mname, u8mtmpname ) == 0 ) || ( strcasecmp_w( volume->v_macname, mactmpname ) == 0 )){
665 if (volume->v_deleted) {
666 volume->v_new = hide = 1;
669 return -1; /* Won't be able to access it, anyway... */
674 if (!( volume = (struct vol *)calloc(1, sizeof( struct vol ))) ) {
675 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
678 if ( NULL == ( volume->v_localname = strdup(name))) {
679 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
684 if ( NULL == ( volume->v_u8mname = strdup_w(u8mtmpname))) {
685 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
690 if ( NULL == ( volume->v_macname = strdup_w(mactmpname))) {
691 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
696 if (!( volume->v_path = (char *)malloc( strlen( path ) + 1 )) ) {
697 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
703 volume->v_name = utf8_encoding()?volume->v_u8mname:volume->v_macname;
704 volume->v_hide = hide;
705 strcpy( volume->v_path, path );
709 #endif /* __svr4__ */
710 /* os X start at 1 and use network order ie. 1 2 3 */
711 volume->v_vid = ++lastvid;
712 volume->v_vid = htons(volume->v_vid);
714 if (check_vol_acl_support(volume))
715 volume->v_flags |= AFPVOL_ACLS
721 volume->v_casefold = options[VOLOPT_CASEFOLD].i_value;
722 volume->v_flags |= options[VOLOPT_FLAGS].i_value;
724 if (options[VOLOPT_EA_VFS].i_value)
725 volume->v_vfs_ea = options[VOLOPT_EA_VFS].i_value;
727 volume->v_ad_options = 0;
728 if ((volume->v_flags & AFPVOL_NODEV))
729 volume->v_ad_options |= ADVOL_NODEV;
730 if ((volume->v_flags & AFPVOL_CACHE))
731 volume->v_ad_options |= ADVOL_CACHE;
732 if ((volume->v_flags & AFPVOL_UNIX_PRIV))
733 volume->v_ad_options |= ADVOL_UNIXPRIV;
734 if ((volume->v_flags & AFPVOL_INV_DOTS))
735 volume->v_ad_options |= ADVOL_INVDOTS;
736 if ((volume->v_flags & AFPVOL_NOADOUBLE))
737 volume->v_ad_options |= ADVOL_NOADOUBLE;
739 if (options[VOLOPT_PASSWORD].c_value)
740 volume->v_password = strdup(options[VOLOPT_PASSWORD].c_value);
742 if (options[VOLOPT_VETO].c_value)
743 volume->v_veto = strdup(options[VOLOPT_VETO].c_value);
745 if (options[VOLOPT_ENCODING].c_value)
746 volume->v_volcodepage = strdup(options[VOLOPT_ENCODING].c_value);
748 if (options[VOLOPT_MACCHARSET].c_value)
749 volume->v_maccodepage = strdup(options[VOLOPT_MACCHARSET].c_value);
751 if (options[VOLOPT_DBPATH].c_value)
752 volume->v_dbpath = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_DBPATH].c_value, pwd, path, name);
754 if (options[VOLOPT_CNIDSCHEME].c_value)
755 volume->v_cnidscheme = strdup(options[VOLOPT_CNIDSCHEME].c_value);
757 if (options[VOLOPT_CNIDSERVER].c_value)
758 volume->v_cnidserver = strdup(options[VOLOPT_CNIDSERVER].c_value);
760 if (options[VOLOPT_CNIDPORT].c_value)
761 volume->v_cnidport = strdup(options[VOLOPT_CNIDPORT].c_value);
763 if (options[VOLOPT_UMASK].i_value)
764 volume->v_umask = (mode_t)options[VOLOPT_UMASK].i_value;
766 if (options[VOLOPT_DPERM].i_value)
767 volume->v_dperm = (mode_t)options[VOLOPT_DPERM].i_value;
769 if (options[VOLOPT_FPERM].i_value)
770 volume->v_fperm = (mode_t)options[VOLOPT_FPERM].i_value;
772 if (options[VOLOPT_DFLTPERM].i_value)
773 volume->v_perm = (mode_t)options[VOLOPT_DFLTPERM].i_value;
775 if (options[VOLOPT_ADOUBLE].i_value)
776 volume->v_adouble = options[VOLOPT_ADOUBLE].i_value;
778 volume->v_adouble = AD_VERSION;
780 if (options[VOLOPT_LIMITSIZE].i_value)
781 volume->v_limitsize = options[VOLOPT_LIMITSIZE].i_value;
783 /* Mac to Unix conversion flags*/
784 volume->v_mtou_flags = 0;
785 if (!(volume->v_flags & AFPVOL_NOHEX))
786 volume->v_mtou_flags |= CONV_ESCAPEHEX;
787 if (!(volume->v_flags & AFPVOL_USEDOTS))
788 volume->v_mtou_flags |= CONV_ESCAPEDOTS;
789 if ((volume->v_flags & AFPVOL_EILSEQ))
790 volume->v_mtou_flags |= CONV__EILSEQ;
792 if ((volume->v_casefold & AFPVOL_MTOUUPPER))
793 volume->v_mtou_flags |= CONV_TOUPPER;
794 else if ((volume->v_casefold & AFPVOL_MTOULOWER))
795 volume->v_mtou_flags |= CONV_TOLOWER;
797 /* Unix to Mac conversion flags*/
798 volume->v_utom_flags = CONV_IGNORE | CONV_UNESCAPEHEX;
799 if ((volume->v_casefold & AFPVOL_UTOMUPPER))
800 volume->v_utom_flags |= CONV_TOUPPER;
801 else if ((volume->v_casefold & AFPVOL_UTOMLOWER))
802 volume->v_utom_flags |= CONV_TOLOWER;
804 if ((volume->v_flags & AFPVOL_EILSEQ))
805 volume->v_utom_flags |= CONV__EILSEQ;
808 if (options[VOLOPT_FORCEUID].c_value) {
809 volume->v_forceuid = strdup(options[VOLOPT_FORCEUID].c_value);
811 volume->v_forceuid = NULL; /* set as null so as to return 0 later on */
814 if (options[VOLOPT_FORCEGID].c_value) {
815 volume->v_forcegid = strdup(options[VOLOPT_FORCEGID].c_value);
817 volume->v_forcegid = NULL; /* set as null so as to return 0 later on */
821 if (options[VOLOPT_PREEXEC].c_value)
822 volume->v_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_PREEXEC].c_value, pwd, path, name);
823 volume->v_preexec_close = options[VOLOPT_PREEXEC].i_value;
825 if (options[VOLOPT_POSTEXEC].c_value)
826 volume->v_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_POSTEXEC].c_value, pwd, path, name);
828 if (options[VOLOPT_ROOTPREEXEC].c_value)
829 volume->v_root_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPREEXEC].c_value, pwd, path, name);
830 volume->v_root_preexec_close = options[VOLOPT_ROOTPREEXEC].i_value;
832 if (options[VOLOPT_ROOTPOSTEXEC].c_value)
833 volume->v_root_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPOSTEXEC].c_value, pwd, path, name);
836 volume->v_dperm |= volume->v_perm;
837 volume->v_fperm |= volume->v_perm;
839 /* Check EA support on volume */
840 if (volume->v_vfs_ea == AFPVOL_EA_AUTO)
841 check_ea_sys_support(volume);
844 /* get/store uuid from file */
845 if (volume->v_flags & AFPVOL_TM) {
846 char *uuid = get_uuid(obj, volume->v_localname);
848 LOG(log_error, logtype_afpd, "Volume '%s': couldn't get UUID",
849 volume->v_localname);
851 volume->v_uuid = uuid;
852 LOG(log_debug, logtype_afpd, "Volume '%s': UUID '%s'",
853 volume->v_localname, volume->v_uuid);
857 volume->v_next = Volumes;
862 /* ---------------- */
863 static char *myfgets( char *buf, int size, FILE *fp)
869 while ((EOF != ( c = getc( fp )) ) && ( size > 1 )) {
870 if ( c == '\n' || c == '\r' ) {
871 if (p != buf && *(p -1) == '\\') {
893 /* check access list. this function wants something of the following
895 * @group,name,name2,@group2,name3
897 * a NULL argument allows everybody to have access.
898 * we return three things:
900 * 0: list exists, but name isn't in it
904 #ifndef NO_REAL_USER_NAME
905 /* authentication is case insensitive
906 * FIXME should we do the same with group name?
908 #define access_strcmp strcasecmp
911 #define access_strcmp strcmp
915 static int accessvol(const char *args, const char *name)
917 char buf[MAXPATHLEN + 1], *p;
923 strlcpy(buf, args, sizeof(buf));
924 if ((p = strtok(buf, ",")) == NULL) /* nothing, return okay */
928 if (*p == '@') { /* it's a group */
929 if ((gr = getgrnam(p + 1)) && gmem(gr->gr_gid))
931 } else if (access_strcmp(p, name) == 0) /* it's a user name */
933 p = strtok(NULL, ",");
939 static int hostaccessvol(int type, const char *volname, const char *args, const AFPObj *obj)
942 char buf[MAXPATHLEN + 1], *p, *b;
943 DSI *dsi = obj->handle;
944 struct sockaddr_storage client;
949 strlcpy(buf, args, sizeof(buf));
950 if ((p = strtok_r(buf, ",", &b)) == NULL) /* nothing, return okay */
953 if (obj->proto != AFPPROTO_DSI)
958 char *ipaddr, *mask_char;
959 struct addrinfo hints, *ai;
961 ipaddr = strtok(p, "/");
962 mask_char = strtok(NULL,"/");
964 /* Get address from string with getaddrinfo */
965 memset(&hints, 0, sizeof hints);
966 hints.ai_family = AF_UNSPEC;
967 hints.ai_socktype = SOCK_STREAM;
968 if ((ret = getaddrinfo(ipaddr, NULL, &hints, &ai)) != 0) {
969 LOG(log_error, logtype_afpd, "hostaccessvol: getaddrinfo: %s\n", gai_strerror(ret));
974 if (mask_char != NULL)
975 mask_int = atoi(mask_char); /* apply_ip_mask does range checking on it */
977 if (ai->ai_family == AF_INET) /* IPv4 */
983 /* Apply mask to addresses */
984 client = dsi->client;
985 apply_ip_mask((struct sockaddr *)&client, mask_int);
986 apply_ip_mask(ai->ai_addr, mask_int);
988 if (compare_ip((struct sockaddr *)&client, ai->ai_addr) == 0) {
989 if (type == VOLOPT_DENIED_HOSTS)
990 LOG(log_info, logtype_afpd, "AFP access denied for client IP '%s' to volume '%s' by denied list",
991 getip_string((struct sockaddr *)&client), volname);
998 p = strtok_r(NULL, ",", &b);
1001 if (type == VOLOPT_ALLOWED_HOSTS)
1002 LOG(log_info, logtype_afpd, "AFP access denied for client IP '%s' to volume '%s', not in allowed list",
1003 getip_string((struct sockaddr *)&dsi->client), volname);
1007 static void setextmap(char *ext, char *type, char *creator, int user)
1012 if (Extmap == NULL) {
1013 if (( Extmap = calloc(1, sizeof( struct extmap ))) == NULL ) {
1014 LOG(log_error, logtype_afpd, "setextmap: calloc: %s", strerror(errno) );
1019 for ( em = Extmap, cnt = 0; em->em_ext; em++, cnt++) {
1020 if ( (strdiacasecmp( em->em_ext, ext )) == 0 ) {
1025 if ( em->em_ext == NULL ) {
1026 if (!(Extmap = realloc( Extmap, sizeof( struct extmap ) * (cnt +2))) ) {
1027 LOG(log_error, logtype_afpd, "setextmap: realloc: %s", strerror(errno) );
1030 (Extmap +cnt +1)->em_ext = NULL;
1032 } else if ( !user ) {
1038 if (!(em->em_ext = strdup( ext))) {
1039 LOG(log_error, logtype_afpd, "setextmap: strdup: %s", strerror(errno) );
1043 if ( *type == '\0' ) {
1044 memcpy(em->em_type, "\0\0\0\0", sizeof( em->em_type ));
1046 memcpy(em->em_type, type, sizeof( em->em_type ));
1048 if ( *creator == '\0' ) {
1049 memcpy(em->em_creator, "\0\0\0\0", sizeof( em->em_creator ));
1051 memcpy(em->em_creator, creator, sizeof( em->em_creator ));
1055 /* -------------------------- */
1056 static int extmap_cmp(const void *map1, const void *map2)
1058 const struct extmap *em1 = map1;
1059 const struct extmap *em2 = map2;
1060 return strdiacasecmp(em1->em_ext, em2->em_ext);
1063 static void sortextmap( void)
1068 if ((em = Extmap) == NULL) {
1071 while (em->em_ext) {
1076 qsort(Extmap, Extmap_cnt, sizeof(struct extmap), extmap_cmp);
1077 if (*Extmap->em_ext == 0) {
1078 /* the first line is really "." the default entry,
1079 * we remove the leading '.' in setextmap
1086 /* ----------------------
1088 static void free_extmap( void)
1093 for ( em = Extmap; em->em_ext; em++) {
1103 /* ----------------------
1105 static int volfile_changed(struct afp_volume_name *p)
1111 name = p->full_name;
1115 if (!stat( name, &st) && st.st_mtime > p->mtime) {
1116 p->mtime = st.st_mtime;
1122 /* ----------------------
1123 * Read a volume configuration file and add the volumes contained within to
1124 * the global volume list. This gets called from the forked afpd childs.
1125 * The master now reads this too for Zeroconf announcements.
1127 * If p2 is non-NULL, the file that is opened is
1130 * Lines that begin with # and blank lines are ignored.
1131 * Volume lines are of the form:
1132 * <unix path> [<volume name>] [allow:<user>,<@group>,...] \
1133 * [codepage:<file>] [casefold:<num>]
1134 * <extension> TYPE [CREATOR]
1137 int readvolfile(AFPObj *obj, struct afp_volume_name *p1, char *p2, int user, struct passwd *pwent)
1140 char path[MAXPATHLEN + 1];
1141 char tmp[MAXPATHLEN + 1];
1142 char volname[AFPVOL_U8MNAMELEN + 1];
1144 char type[5], creator[5];
1149 struct vol_option save_options[VOLOPT_NUM];
1150 struct vol_option options[VOLOPT_NUM];
1156 strcpy( path, p1->name );
1158 strcat( path, "/" );
1160 if (p1->full_name) {
1161 free(p1->full_name);
1163 p1->full_name = strdup(path);
1166 if (NULL == ( fp = fopen( path, "r" )) ) {
1170 if (fd != -1 && !fstat( fd, &st) ) {
1171 p1->mtime = st.st_mtime;
1174 memset(save_options, 0, sizeof(save_options));
1176 /* Enable some default options for all volumes */
1177 save_options[VOLOPT_FLAGS].i_value |= AFPVOL_CACHE;
1178 save_options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_AUTO;
1179 LOG(log_maxdebug, logtype_afpd, "readvolfile: seeding default umask: %04o",
1180 obj->options.umask);
1181 save_options[VOLOPT_UMASK].i_value = obj->options.umask;
1183 LOG(log_debug, logtype_afpd, "readvolfile: \"%s\"", path);
1185 while ( myfgets( buf, sizeof( buf ), fp ) != NULL ) {
1186 initline( strlen( buf ), buf );
1187 parseline( sizeof( path ) - 1, path );
1194 /* change the default options for this file */
1195 if (strncmp(path, VOLOPT_DEFAULT, VOLOPT_DEFAULT_LEN) == 0) {
1197 for (i = 0; i < VOLOPT_NUM; i++) {
1198 if (parseline( sizeof( path ) - VOLOPT_DEFAULT_LEN - 1,
1199 path + VOLOPT_DEFAULT_LEN) < 0)
1201 volset(save_options, NULL, tmp, sizeof(tmp) - 1,
1202 path + VOLOPT_DEFAULT_LEN);
1208 if (( p = strchr( path, '/' )) != NULL ) {
1216 if ( u == NULL || *u == '\0' || ( pw = getpwnam( u )) == NULL ) {
1219 strcpy( tmp, pw->pw_dir );
1220 if ( p != NULL && *p != '\0' ) {
1227 /* send path through variable substitution */
1228 if (*path != '~') /* need to copy path to tmp */
1230 if (!pwent && obj->username)
1231 pwent = getpwnam(obj->username);
1233 if (volxlate(obj, path, sizeof(path) - 1, tmp, pwent, NULL, NULL) == NULL)
1236 /* this is sort of braindead. basically, i want to be
1237 * able to specify things in any order, but i don't want to
1238 * re-write everything. */
1240 memcpy(options, save_options, sizeof(options));
1243 /* read in up to VOLOP_NUM possible options */
1244 for (i = 0; i < VOLOPT_NUM; i++) {
1245 if (parseline( sizeof( tmp ) - 1, tmp ) < 0)
1248 volset(options, save_options, volname, sizeof(volname) - 1, tmp);
1251 /* check allow/deny lists (if not afpd master loading volumes for Zeroconf reg.):
1252 allow -> either no list (-1), or in list (1)
1253 deny -> either no list (-1), or not in list (0) */
1254 if (!((DSI *)obj->handle)->child
1256 (accessvol(options[VOLOPT_ALLOW].c_value, obj->username) &&
1257 (accessvol(options[VOLOPT_DENY].c_value, obj->username) < 1) &&
1258 hostaccessvol(VOLOPT_ALLOWED_HOSTS, volname, options[VOLOPT_ALLOWED_HOSTS].c_value, obj) &&
1259 (hostaccessvol(VOLOPT_DENIED_HOSTS, volname, options[VOLOPT_DENIED_HOSTS].c_value, obj) < 1))) {
1261 /* handle read-only behaviour. semantics:
1262 * 1) neither the rolist nor the rwlist exist -> rw
1263 * 2) rolist exists -> ro if user is in it.
1264 * 3) rwlist exists -> ro unless user is in it. */
1265 if (((DSI *)obj->handle)->child
1267 ((options[VOLOPT_FLAGS].i_value & AFPVOL_RO) == 0)
1269 ((accessvol(options[VOLOPT_ROLIST].c_value, obj->username) == 1) ||
1270 !accessvol(options[VOLOPT_RWLIST].c_value, obj->username)))
1271 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
1273 /* do variable substitution for volname */
1274 if (volxlate(obj, tmp, sizeof(tmp) - 1, volname, pwent, path, NULL) == NULL)
1277 creatvol(obj, pwent, path, tmp, options, p2 != NULL);
1279 volfree(options, save_options);
1283 parseline( sizeof( type ) - 1, type );
1284 parseline( sizeof( creator ) - 1, creator );
1285 setextmap( path, type, creator, user);
1292 volfree(save_options, NULL);
1294 if ( fclose( fp ) != 0 ) {
1295 LOG(log_error, logtype_afpd, "readvolfile: fclose: %s", strerror(errno) );
1301 /* ------------------------------- */
1302 static void volume_free(struct vol *vol)
1304 free(vol->v_localname);
1305 vol->v_localname = NULL;
1306 free(vol->v_u8mname);
1307 vol->v_u8mname = NULL;
1308 free(vol->v_macname);
1309 vol->v_macname = NULL;
1311 free(vol->v_password);
1313 free(vol->v_volcodepage);
1314 free(vol->v_maccodepage);
1315 free(vol->v_cnidscheme);
1316 free(vol->v_dbpath);
1319 free(vol->v_forceuid);
1320 free(vol->v_forcegid);
1321 #endif /* FORCE_UIDGID */
1326 /* ------------------------------- */
1327 static void free_volumes(void )
1330 struct vol *nvol, *ovol;
1332 for ( vol = Volumes; vol; vol = vol->v_next ) {
1333 if (( vol->v_flags & AFPVOL_OPEN ) ) {
1340 for ( vol = Volumes, ovol = NULL; vol; vol = nvol) {
1343 if (vol->v_localname == NULL) {
1344 if (Volumes == vol) {
1349 ovol->v_next = nvol;
1359 /* ------------------------------- */
1360 static void volume_unlink(struct vol *volume)
1362 struct vol *vol, *ovol, *nvol;
1364 if (volume == Volumes) {
1365 Volumes = Volumes->v_next;
1368 for ( vol = Volumes->v_next, ovol = Volumes; vol; vol = nvol) {
1371 if (vol == volume) {
1372 ovol->v_next = nvol;
1381 static int getvolspace(struct vol *vol,
1382 u_int32_t *bfree, u_int32_t *btotal,
1383 VolSpace *xbfree, VolSpace *xbtotal, u_int32_t *bsize)
1387 #ifndef NO_QUOTA_SUPPORT
1388 VolSpace qfree, qtotal;
1391 spaceflag = AFPVOL_GVSMASK & vol->v_flags;
1392 /* report up to 2GB if afp version is < 2.2 (4GB if not) */
1393 maxsize = (vol->v_flags & AFPVOL_A2VOL) ? 0x01fffe00 :
1394 (((afp_version < 22) || (vol->v_flags & AFPVOL_LIMITSIZE))
1395 ? 0x7fffffffL : 0xffffffffL);
1398 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_AFSGVS ) {
1399 if ( afs_getvolspace( vol, xbfree, xbtotal, bsize ) == AFP_OK ) {
1400 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_AFSGVS;
1401 goto getvolspace_done;
1406 if (( rc = ustatfs_getvolspace( vol, xbfree, xbtotal, bsize)) != AFP_OK ) {
1410 #define min(a,b) ((a)<(b)?(a):(b))
1411 #ifndef NO_QUOTA_SUPPORT
1412 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_UQUOTA ) {
1413 if ( uquota_getvolspace( vol, &qfree, &qtotal, *bsize ) == AFP_OK ) {
1414 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_UQUOTA;
1415 *xbfree = min(*xbfree, qfree);
1416 *xbtotal = min( *xbtotal, qtotal);
1417 goto getvolspace_done;
1421 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_USTATFS;
1424 if (vol->v_limitsize) {
1425 /* FIXME: Free could be limit minus (total minus used), */
1426 /* which will confuse the client less ? */
1427 *xbfree = min(*xbfree, (vol->v_limitsize * 1024 * 1024));
1428 *xbtotal = min(*xbtotal, (vol->v_limitsize * 1024 * 1024));
1431 *bfree = min( *xbfree, maxsize);
1432 *btotal = min( *xbtotal, maxsize);
1436 /* -----------------------
1437 * set volume creation date
1438 * avoid duplicate, well at least it tries
1440 static void vol_setdate(u_int16_t id, struct adouble *adp, time_t date)
1443 struct vol *vol = Volumes;
1445 for ( volume = Volumes; volume; volume = volume->v_next ) {
1446 if (volume->v_vid == id) {
1449 else if ((time_t)(AD_DATE_FROM_UNIX(date)) == volume->v_ctime) {
1451 volume = Volumes; /* restart */
1454 vol->v_ctime = AD_DATE_FROM_UNIX(date);
1455 ad_setdate(adp, AD_DATE_CREATE | AD_DATE_UNIX, date);
1458 /* ----------------------- */
1459 static int getvolparams( u_int16_t bitmap, struct vol *vol, struct stat *st, char *buf, size_t *buflen)
1462 int bit = 0, isad = 1;
1465 u_int32_t bfree, btotal, bsize;
1466 VolSpace xbfree, xbtotal; /* extended bytes */
1467 char *data, *nameoff = NULL;
1470 LOG(log_debug, logtype_afpd, "getvolparams: Volume '%s'", vol->v_localname);
1472 /* courtesy of jallison@whistle.com:
1473 * For MacOS8.x support we need to create the
1474 * .Parent file here if it doesn't exist. */
1476 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1477 if (ad_open(&ad, vol->v_path, ADFLAGS_HF | ADFLAGS_DIR, O_RDWR | O_CREAT, 0666) != 0 ) {
1479 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1481 } else if (ad_get_MD_flags( &ad ) & O_CREAT) {
1482 slash = strrchr( vol->v_path, '/' );
1486 slash = vol->v_path;
1487 if (ad_getentryoff(&ad, ADEID_NAME)) {
1488 ad_setentrylen( &ad, ADEID_NAME, strlen( slash ));
1489 memcpy(ad_entry( &ad, ADEID_NAME ), slash,
1490 ad_getentrylen( &ad, ADEID_NAME ));
1492 vol_setdate(vol->v_vid, &ad, st->st_mtime);
1496 if (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0)
1497 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1499 vol->v_ctime = aint;
1502 if (( bitmap & ( (1<<VOLPBIT_BFREE)|(1<<VOLPBIT_BTOTAL) |
1503 (1<<VOLPBIT_XBFREE)|(1<<VOLPBIT_XBTOTAL) |
1504 (1<<VOLPBIT_BSIZE)) ) != 0 ) {
1505 if ( getvolspace( vol, &bfree, &btotal, &xbfree, &xbtotal,
1506 &bsize) != AFP_OK ) {
1508 ad_close( &ad, ADFLAGS_HF );
1510 return( AFPERR_PARAM );
1515 while ( bitmap != 0 ) {
1516 while (( bitmap & 1 ) == 0 ) {
1524 /* check for read-only.
1525 * NOTE: we don't actually set the read-only flag unless
1526 * it's passed in that way as it's possible to mount
1527 * a read-write filesystem under a read-only one. */
1528 if ((vol->v_flags & AFPVOL_RO) ||
1529 ((utime(vol->v_path, NULL) < 0) && (errno == EROFS))) {
1530 ashort |= VOLPBIT_ATTR_RO;
1532 /* prior 2.1 only VOLPBIT_ATTR_RO is defined */
1533 if (afp_version > 20) {
1534 if (0 == (vol->v_flags & AFPVOL_NOFILEID) && vol->v_cdb != NULL &&
1535 (vol->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1536 ashort |= VOLPBIT_ATTR_FILEID;
1538 ashort |= VOLPBIT_ATTR_CATSEARCH;
1540 if (afp_version >= 30) {
1541 ashort |= VOLPBIT_ATTR_UTF8;
1542 if (vol->v_flags & AFPVOL_UNIX_PRIV)
1543 ashort |= VOLPBIT_ATTR_UNIXPRIV;
1544 if (vol->v_flags & AFPVOL_TM)
1545 ashort |= VOLPBIT_ATTR_TM;
1547 if (afp_version >= 32) {
1549 ashort |= VOLPBIT_ATTR_EXT_ATTRS;
1550 if (vol->v_flags & AFPVOL_ACLS)
1551 ashort |= VOLPBIT_ATTR_ACLS;
1555 ashort = htons(ashort);
1556 memcpy(data, &ashort, sizeof( ashort ));
1557 data += sizeof( ashort );
1561 ashort = htons( AFPVOLSIG_DEFAULT );
1562 memcpy(data, &ashort, sizeof( ashort ));
1563 data += sizeof( ashort );
1566 case VOLPBIT_CDATE :
1567 aint = vol->v_ctime;
1568 memcpy(data, &aint, sizeof( aint ));
1569 data += sizeof( aint );
1572 case VOLPBIT_MDATE :
1573 if ( st->st_mtime > vol->v_mtime ) {
1574 vol->v_mtime = st->st_mtime;
1576 aint = AD_DATE_FROM_UNIX(vol->v_mtime);
1577 memcpy(data, &aint, sizeof( aint ));
1578 data += sizeof( aint );
1581 case VOLPBIT_BDATE :
1582 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1583 aint = AD_DATE_START;
1584 memcpy(data, &aint, sizeof( aint ));
1585 data += sizeof( aint );
1589 memcpy(data, &vol->v_vid, sizeof( vol->v_vid ));
1590 data += sizeof( vol->v_vid );
1593 case VOLPBIT_BFREE :
1594 bfree = htonl( bfree );
1595 memcpy(data, &bfree, sizeof( bfree ));
1596 data += sizeof( bfree );
1599 case VOLPBIT_BTOTAL :
1600 btotal = htonl( btotal );
1601 memcpy(data, &btotal, sizeof( btotal ));
1602 data += sizeof( btotal );
1605 #ifndef NO_LARGE_VOL_SUPPORT
1606 case VOLPBIT_XBFREE :
1607 xbfree = hton64( xbfree );
1608 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1609 bcopy(&xbfree, data, sizeof(xbfree));
1610 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1611 memcpy(data, &xbfree, sizeof( xbfree ));
1612 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1613 data += sizeof( xbfree );
1616 case VOLPBIT_XBTOTAL :
1617 xbtotal = hton64( xbtotal );
1618 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1619 bcopy(&xbtotal, data, sizeof(xbtotal));
1620 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1621 memcpy(data, &xbtotal, sizeof( xbtotal ));
1622 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1623 data += sizeof( xbfree );
1625 #endif /* ! NO_LARGE_VOL_SUPPORT */
1629 data += sizeof( u_int16_t );
1632 case VOLPBIT_BSIZE: /* block size */
1633 bsize = htonl(bsize);
1634 memcpy(data, &bsize, sizeof(bsize));
1635 data += sizeof(bsize);
1640 ad_close( &ad, ADFLAGS_HF );
1642 return( AFPERR_BITMAP );
1648 ashort = htons( data - buf );
1649 memcpy(nameoff, &ashort, sizeof( ashort ));
1650 /* name is always in mac charset */
1651 aint = ucs2_to_charset( vol->v_maccharset, vol->v_macname, data+1, AFPVOL_MACNAMELEN + 1);
1661 ad_close_metadata( &ad);
1663 *buflen = data - buf;
1667 /* ------------------------- */
1668 static int stat_vol(u_int16_t bitmap, struct vol *vol, char *rbuf, size_t *rbuflen)
1674 if ( stat( vol->v_path, &st ) < 0 ) {
1676 return( AFPERR_PARAM );
1678 /* save the volume device number */
1679 vol->v_dev = st.st_dev;
1681 buflen = *rbuflen - sizeof( bitmap );
1682 if (( ret = getvolparams( bitmap, vol, &st,
1683 rbuf + sizeof( bitmap ), &buflen )) != AFP_OK ) {
1687 *rbuflen = buflen + sizeof( bitmap );
1688 bitmap = htons( bitmap );
1689 memcpy(rbuf, &bitmap, sizeof( bitmap ));
1694 /* ------------------------------- */
1695 void load_volumes(AFPObj *obj)
1697 struct passwd *pwent;
1702 /* check files date */
1703 if (obj->options.defaultvol.loaded) {
1704 changed = volfile_changed(&obj->options.defaultvol);
1706 if (obj->options.systemvol.loaded) {
1707 changed |= volfile_changed(&obj->options.systemvol);
1709 if (obj->options.uservol.loaded) {
1710 changed |= volfile_changed(&obj->options.uservol);
1719 if (! ((DSI *)obj->handle)->child) {
1720 LOG(log_debug, logtype_afpd, "load_volumes: AFP MASTER");
1722 LOG(log_debug, logtype_afpd, "load_volumes: user: %s", obj->username);
1725 pwent = getpwnam(obj->username);
1726 if ( (obj->options.flags & OPTION_USERVOLFIRST) == 0 ) {
1727 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent);
1730 if ((*obj->username == '\0') || (obj->options.flags & OPTION_NOUSERVOL)) {
1731 readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent);
1734 * Read user's AppleVolumes or .AppleVolumes file
1735 * If neither are readable, read the default volumes file. if
1736 * that doesn't work, create a user share.
1738 if (obj->options.uservol.name) {
1739 free(obj->options.uservol.name);
1741 obj->options.uservol.name = strdup(pwent->pw_dir);
1742 if ( readvolfile(obj, &obj->options.uservol, "AppleVolumes", 1, pwent) < 0 &&
1743 readvolfile(obj, &obj->options.uservol, ".AppleVolumes", 1, pwent) < 0 &&
1744 readvolfile(obj, &obj->options.uservol, "applevolumes", 1, pwent) < 0 &&
1745 readvolfile(obj, &obj->options.uservol, ".applevolumes", 1, pwent) < 0 &&
1746 obj->options.defaultvol.name != NULL ) {
1747 if (readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent) < 0)
1748 creatvol(obj, pwent, pwent->pw_dir, NULL, NULL, 1);
1751 if ( obj->options.flags & OPTION_USERVOLFIRST ) {
1752 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent );
1755 if ( obj->options.closevol ) {
1758 for ( vol = Volumes; vol; vol = vol->v_next ) {
1759 if (vol->v_deleted && !vol->v_new ) {
1768 /* ------------------------------- */
1769 int afp_getsrvrparms(AFPObj *obj, char *ibuf _U_, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
1782 for ( vcnt = 0, volume = Volumes; volume; volume = volume->v_next ) {
1783 if (!(volume->v_flags & AFPVOL_NOSTAT)) {
1786 if ( stat( volume->v_path, &st ) < 0 ) {
1787 LOG(log_info, logtype_afpd, "afp_getsrvrparms(%s): stat: %s",
1788 volume->v_path, strerror(errno) );
1789 continue; /* can't access directory */
1791 if (!S_ISDIR(st.st_mode)) {
1792 continue; /* not a dir */
1794 accessmode(volume->v_path, &ma, NULL, &st);
1795 if ((ma.ma_user & (AR_UREAD | AR_USEARCH)) != (AR_UREAD | AR_USEARCH)) {
1796 continue; /* no r-x access */
1799 if (volume->v_hide) {
1800 continue; /* config file changed but the volume was mounted */
1803 if (utf8_encoding()) {
1804 len = ucs2_to_charset_allocate(CH_UTF8_MAC, &namebuf, volume->v_u8mname);
1806 len = ucs2_to_charset_allocate(obj->options.maccharset, &namebuf, volume->v_macname);
1809 if (len == (size_t)-1)
1812 /* set password bit if there's a volume password */
1813 *data = (volume->v_password) ? AFPSRVR_PASSWD : 0;
1815 /* Apple 2 clients running ProDOS-8 expect one volume to have
1816 bit 0 of this byte set. They will not recognize anything
1817 on the server unless this is the case. I have not
1818 completely worked this out, but it's related to booting
1819 from the server. Support for that function is a ways
1820 off.. <shirsch@ibm.net> */
1821 *data |= (volume->v_flags & AFPVOL_A2VOL) ? AFPSRVR_CONFIGINFO : 0;
1822 *data++ |= 0; /* UNIX PRIVS BIT ..., OSX doesn't seem to use it, so we don't either */
1824 memcpy(data, namebuf, len );
1830 *rbuflen = data - rbuf;
1832 if ( gettimeofday( &tv, NULL ) < 0 ) {
1833 LOG(log_error, logtype_afpd, "afp_getsrvrparms(%s): gettimeofday: %s", volume->v_path, strerror(errno) );
1835 return AFPERR_PARAM;
1837 tv.tv_sec = AD_DATE_FROM_UNIX(tv.tv_sec);
1838 memcpy(data, &tv.tv_sec, sizeof( u_int32_t));
1839 data += sizeof( u_int32_t);
1844 /* ------------------------- */
1845 static int volume_codepage(AFPObj *obj, struct vol *volume)
1847 struct charset_functions *charset;
1850 if (!volume->v_volcodepage)
1851 volume->v_volcodepage = strdup("UTF8");
1853 if ( (charset_t) -1 == ( volume->v_volcharset = add_charset(volume->v_volcodepage)) ) {
1854 LOG (log_error, logtype_afpd, "Setting codepage %s as volume codepage failed", volume->v_volcodepage);
1858 if ( NULL == (charset = find_charset_functions(volume->v_volcodepage)) || charset->flags & CHARSET_ICONV ) {
1859 LOG (log_warning, logtype_afpd, "WARNING: volume encoding %s is *not* supported by netatalk, expect problems !!!!", volume->v_volcodepage);
1862 if (!volume->v_maccodepage)
1863 volume->v_maccodepage = strdup(obj->options.maccodepage);
1865 if ( (charset_t) -1 == ( volume->v_maccharset = add_charset(volume->v_maccodepage)) ) {
1866 LOG (log_error, logtype_afpd, "Setting codepage %s as mac codepage failed", volume->v_maccodepage);
1870 if ( NULL == ( charset = find_charset_functions(volume->v_maccodepage)) || ! (charset->flags & CHARSET_CLIENT) ) {
1871 LOG (log_error, logtype_afpd, "Fatal error: mac charset %s not supported", volume->v_maccodepage);
1874 volume->v_kTextEncoding = htonl(charset->kTextEncoding);
1878 /* ------------------------- */
1879 static int volume_openDB(struct vol *volume)
1883 if ((volume->v_flags & AFPVOL_NODEV)) {
1884 flags |= CNID_FLAG_NODEV;
1887 if (volume->v_cnidscheme == NULL) {
1888 volume->v_cnidscheme = strdup(DEFAULT_CNID_SCHEME);
1889 LOG(log_info, logtype_afpd, "Volume %s use CNID scheme %s.",
1890 volume->v_path, volume->v_cnidscheme);
1893 LOG(log_info, logtype_afpd, "CNID server: %s:%s",
1894 volume->v_cnidserver ? volume->v_cnidserver : Cnid_srv,
1895 volume->v_cnidport ? volume->v_cnidport : Cnid_port);
1898 /* Found this in branch dir-rewrite, maybe we want to use it sometimes */
1900 /* Legacy pre 2.1 way of sharing eg CD-ROM */
1901 if (strcmp(volume->v_cnidscheme, "last") == 0) {
1902 /* "last" is gone. We support it by switching to in-memory "tdb" */
1903 volume->v_cnidscheme = strdup("tdb");
1904 flags |= CNID_FLAG_MEMORY;
1907 /* New way of sharing CD-ROM */
1908 if (volume->v_flags & AFPVOL_CDROM) {
1909 flags |= CNID_FLAG_MEMORY;
1910 if (strcmp(volume->v_cnidscheme, "tdb") != 0) {
1911 free(volume->v_cnidscheme);
1912 volume->v_cnidscheme = strdup("tdb");
1913 LOG(log_info, logtype_afpd, "Volume %s is ejectable, switching to scheme %s.",
1914 volume->v_path, volume->v_cnidscheme);
1919 volume->v_cdb = cnid_open(volume->v_path,
1921 volume->v_cnidscheme,
1923 volume->v_cnidserver ? volume->v_cnidserver : Cnid_srv,
1924 volume->v_cnidport ? volume->v_cnidport : Cnid_port);
1926 if ( ! volume->v_cdb && ! (flags & CNID_FLAG_MEMORY)) {
1927 /* The first attempt failed and it wasn't yet an attempt to open in-memory */
1928 LOG(log_error, logtype_afpd, "Can't open volume \"%s\" CNID backend \"%s\" ",
1929 volume->v_path, volume->v_cnidscheme);
1930 LOG(log_error, logtype_afpd, "Reopen volume %s using in memory temporary CNID DB.",
1932 flags |= CNID_FLAG_MEMORY;
1933 volume->v_cdb = cnid_open (volume->v_path, volume->v_umask, "tdb", flags, NULL, NULL);
1935 /* kill ourself with SIGUSR2 aka msg pending */
1936 if (volume->v_cdb) {
1937 setmessage("Something wrong with the volume's CNID DB, using temporary CNID DB instead."
1938 "Check server messages for details!");
1939 kill(getpid(), SIGUSR2);
1940 /* deactivate cnid caching/storing in AppleDouble files */
1941 volume->v_flags &= ~AFPVOL_CACHE;
1946 return (!volume->v_cdb)?-1:0;
1950 Check if the underlying filesystem supports EAs for ea:sys volumes.
1951 If not, switch to ea:ad.
1952 As we can't check (requires write access) on ro-volumes, we switch ea:auto
1953 volumes that are options:ro to ea:none.
1955 static void check_ea_sys_support(struct vol *vol)
1957 uid_t process_uid = 0;
1958 char eaname[] = {"org.netatalk.supports-eas.XXXXXX"};
1959 const char *eacontent = "yes";
1961 if (vol->v_vfs_ea == AFPVOL_EA_AUTO) {
1963 if ((vol->v_flags & AFPVOL_RO) == AFPVOL_RO) {
1964 LOG(log_info, logtype_logger, "read-only volume '%s', can't test for EA support, disabling EAs", vol->v_localname);
1965 vol->v_vfs_ea = AFPVOL_EA_NONE;
1971 process_uid = geteuid();
1973 if (seteuid(0) == -1) {
1974 LOG(log_error, logtype_logger, "check_ea_sys_support: can't seteuid(0): %s", strerror(errno));
1978 if ((sys_setxattr(vol->v_path, eaname, eacontent, 4, 0)) == 0) {
1979 sys_removexattr(vol->v_path, eaname);
1980 vol->v_vfs_ea = AFPVOL_EA_SYS;
1982 LOG(log_warning, logtype_afpd, "volume \"%s\" does not support Extended Attributes, using ea:ad instead",
1984 vol->v_vfs_ea = AFPVOL_EA_AD;
1988 if (seteuid(process_uid) == -1) {
1989 LOG(log_error, logtype_logger, "can't seteuid back %s", strerror(errno));
1996 /* -------------------------
1997 * we are the user here
1999 int afp_openvol(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2010 char path[ MAXPATHLEN + 1];
2016 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2017 bitmap = ntohs( bitmap );
2018 ibuf += sizeof( bitmap );
2021 if (( bitmap & (1<<VOLPBIT_VID)) == 0 ) {
2022 return AFPERR_BITMAP;
2025 len = (unsigned char)*ibuf++;
2026 volname = obj->oldtmp;
2028 if ((volname_tmp = strchr(volname,'+')) != NULL)
2029 volname = volname_tmp+1;
2031 if (utf8_encoding()) {
2032 namelen = convert_string(CH_UTF8_MAC, CH_UCS2, ibuf, len, volname, sizeof(obj->oldtmp));
2034 namelen = convert_string(obj->options.maccharset, CH_UCS2, ibuf, len, volname, sizeof(obj->oldtmp));
2037 if ( namelen <= 0) {
2038 return AFPERR_PARAM;
2042 if ((len + 1) & 1) /* pad to an even boundary */
2047 for ( volume = Volumes; volume; volume = volume->v_next ) {
2048 if ( strcasecmp_w( (ucs2_t*) volname, volume->v_name ) == 0 ) {
2053 if ( volume == NULL ) {
2054 return AFPERR_PARAM;
2057 /* check for a volume password */
2058 if (volume->v_password && strncmp(ibuf, volume->v_password, VOLPASSLEN)) {
2059 return AFPERR_ACCESS;
2062 if (( volume->v_flags & AFPVOL_OPEN ) ) {
2063 /* the volume is already open */
2065 set_uidgid ( volume );
2067 return stat_vol(bitmap, volume, rbuf, rbuflen);
2070 if (volume->v_root_preexec) {
2071 if ((ret = afprun(1, volume->v_root_preexec, NULL)) && volume->v_root_preexec_close) {
2072 LOG(log_error, logtype_afpd, "afp_openvol(%s): root preexec : %d", volume->v_path, ret );
2078 set_uidgid ( volume );
2081 if (volume->v_preexec) {
2082 if ((ret = afprun(0, volume->v_preexec, NULL)) && volume->v_preexec_close) {
2083 LOG(log_error, logtype_afpd, "afp_openvol(%s): preexec : %d", volume->v_path, ret );
2088 if ( stat( volume->v_path, &st ) < 0 ) {
2089 return AFPERR_PARAM;
2092 if ( chdir( volume->v_path ) < 0 ) {
2093 return AFPERR_PARAM;
2096 if ( NULL == getcwd(path, MAXPATHLEN)) {
2097 /* shouldn't be fatal but it will fail later */
2098 LOG(log_error, logtype_afpd, "afp_openvol(%s): volume pathlen too long", volume->v_path);
2102 /* Normalize volume path */
2103 #ifdef REALPATH_TAKES_NULL
2104 if ((volume->v_path = realpath(path, NULL)) == NULL)
2107 if ((volume->v_path = malloc(MAXPATHLEN+1)) == NULL)
2109 if (realpath(path, volume->v_path) == NULL) {
2110 free(volume->v_path);
2113 /* Safe some memory */
2115 if ((tmp = strdup(volume->v_path)) == NULL) {
2116 free(volume->v_path);
2119 free(volume->v_path);
2120 volume->v_path = tmp;
2123 if (volume_codepage(obj, volume) < 0) {
2128 /* initialize volume variables
2131 if (utf8_encoding()) {
2132 volume->max_filename = 255;
2135 volume->max_filename = MACFILELEN;
2138 volume->v_flags |= AFPVOL_OPEN;
2139 volume->v_cdb = NULL;
2141 if (utf8_encoding()) {
2142 len = convert_string_allocate(CH_UCS2, CH_UTF8_MAC, volume->v_u8mname, namelen, &vol_mname);
2144 len = convert_string_allocate(CH_UCS2, obj->options.maccharset, volume->v_macname, namelen, &vol_mname);
2146 if ( !vol_mname || len <= 0) {
2151 if ((vol_uname = strrchr(path, '/')) == NULL)
2153 else if (*(vol_uname + 1) != '\0')
2156 if ((dir = dir_new(vol_mname,
2161 bfromcstr(volume->v_path),
2165 LOG(log_error, logtype_afpd, "afp_openvol(%s): malloc: %s", volume->v_path, strerror(errno) );
2170 volume->v_root = dir;
2173 if (volume_openDB(volume) < 0) {
2174 LOG(log_error, logtype_afpd, "Fatal error: cannot open CNID or invalid CNID backend for %s: %s",
2175 volume->v_path, volume->v_cnidscheme);
2180 ret = stat_vol(bitmap, volume, rbuf, rbuflen);
2181 if (ret == AFP_OK) {
2183 if (!(volume->v_flags & AFPVOL_RO)) {
2184 handle_special_folders( volume );
2186 volume->v_cnidserver ? volume->v_cnidserver : Cnid_srv,
2187 volume->v_cnidport ? volume->v_cnidport : Cnid_port);
2191 * If you mount a volume twice, the second time the trash appears on
2192 * the desk-top. That's because the Mac remembers the DID for the
2193 * trash (even for volumes in different zones, on different servers).
2194 * Just so this works better, we prime the DID cache with the trash,
2195 * fixing the trash at DID 17.
2196 * FIXME (RL): should it be done inside a CNID backend ? (always returning Trash DID when asked) ?
2198 if ((volume->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
2200 /* FIXME find db time stamp */
2201 if (cnid_getstamp(volume->v_cdb, volume->v_stamp, sizeof(volume->v_stamp)) < 0) {
2202 LOG (log_error, logtype_afpd,
2203 "afp_openvol(%s): Fatal error: Unable to get stamp value from CNID backend",
2211 cname( volume, volume->v_root, &p );
2217 if (volume->v_root) {
2218 dir_free( volume->v_root );
2219 volume->v_root = NULL;
2222 volume->v_flags &= ~AFPVOL_OPEN;
2223 if (volume->v_cdb != NULL) {
2224 cnid_close(volume->v_cdb);
2225 volume->v_cdb = NULL;
2231 /* ------------------------- */
2232 static void closevol(struct vol *vol)
2237 dir_free( vol->v_root );
2239 if (vol->v_cdb != NULL) {
2240 cnid_close(vol->v_cdb);
2244 if (vol->v_postexec) {
2245 afprun(0, vol->v_postexec, NULL);
2247 if (vol->v_root_postexec) {
2248 afprun(1, vol->v_root_postexec, NULL);
2252 /* ------------------------- */
2253 void close_all_vol(void)
2257 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
2258 if ( (ovol->v_flags & AFPVOL_OPEN) ) {
2259 ovol->v_flags &= ~AFPVOL_OPEN;
2265 /* ------------------------- */
2266 static void deletevol(struct vol *vol)
2270 vol->v_flags &= ~AFPVOL_OPEN;
2271 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
2272 if ( (ovol->v_flags & AFPVOL_OPEN) ) {
2276 if ( ovol != NULL ) {
2277 /* Even if chdir fails, we can't say afp_closevol fails. */
2278 if ( chdir( ovol->v_path ) == 0 ) {
2279 curdir = ovol->v_root;
2284 if (vol->v_deleted) {
2285 showvol(vol->v_name);
2292 /* ------------------------- */
2293 int afp_closevol(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2300 memcpy(&vid, ibuf, sizeof( vid ));
2301 if (NULL == ( vol = getvolbyvid( vid )) ) {
2302 return( AFPERR_PARAM );
2311 /* ------------------------- */
2312 struct vol *getvolbyvid(const u_int16_t vid )
2316 for ( vol = Volumes; vol; vol = vol->v_next ) {
2317 if ( vid == vol->v_vid ) {
2321 if ( vol == NULL || ( vol->v_flags & AFPVOL_OPEN ) == 0 ) {
2327 #endif /* FORCE_UIDGID */
2334 /* ------------------------ */
2335 static int ext_cmp_key(const void *key, const void *obj)
2337 const char *p = key;
2338 const struct extmap *em = obj;
2339 return strdiacasecmp(p, em->em_ext);
2341 struct extmap *getextmap(const char *path)
2346 if (!Extmap_cnt || NULL == ( p = strrchr( path, '.' )) ) {
2347 return( Defextmap );
2351 return( Defextmap );
2353 em = bsearch(p, Extmap, Extmap_cnt, sizeof(struct extmap), ext_cmp_key);
2357 return( Defextmap );
2361 /* ------------------------- */
2362 struct extmap *getdefextmap(void)
2364 return( Defextmap );
2367 /* --------------------------
2368 poll if a volume is changed by other processes.
2370 0 no attention msg sent
2371 1 attention msg sent
2372 -1 error (socket closed)
2374 Note: if attention return -1 no packet has been
2375 sent because the buffer is full, we don't care
2376 either there's no reader or there's a lot of
2377 traffic and another pollvoltime will follow
2379 int pollvoltime(AFPObj *obj)
2386 if (!(afp_version > 21 && obj->options.server_notif))
2389 if ( gettimeofday( &tv, NULL ) < 0 )
2392 for ( vol = Volumes; vol; vol = vol->v_next ) {
2393 if ( (vol->v_flags & AFPVOL_OPEN) && vol->v_mtime + 30 < tv.tv_sec) {
2394 if ( !stat( vol->v_path, &st ) && vol->v_mtime != st.st_mtime ) {
2395 vol->v_mtime = st.st_mtime;
2396 if (!obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED))
2405 /* ------------------------- */
2406 void setvoltime(AFPObj *obj, struct vol *vol)
2410 /* just looking at vol->v_mtime is broken seriously since updates
2411 * from other users afpd processes never are seen.
2412 * This is not the most elegant solution (a shared memory between
2413 * the afpd processes would come closer)
2416 if ( gettimeofday( &tv, NULL ) < 0 ) {
2417 LOG(log_error, logtype_afpd, "setvoltime(%s): gettimeofday: %s", vol->v_path, strerror(errno) );
2420 if( utime( vol->v_path, NULL ) < 0 ) {
2421 /* write of time failed ... probably a read only filesys,
2422 * where no other users can interfere, so there's no issue here
2426 /* a little granularity */
2427 if (vol->v_mtime < tv.tv_sec) {
2428 vol->v_mtime = tv.tv_sec;
2429 /* or finder doesn't update free space
2430 * AFP 3.2 and above clients seem to be ok without so many notification
2432 if (afp_version < 32 && obj->options.server_notif) {
2433 obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED);
2438 /* ------------------------- */
2439 int afp_getvolparams(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_,char *rbuf, size_t *rbuflen)
2442 u_int16_t vid, bitmap;
2445 memcpy(&vid, ibuf, sizeof( vid ));
2446 ibuf += sizeof( vid );
2447 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2448 bitmap = ntohs( bitmap );
2450 if (NULL == ( vol = getvolbyvid( vid )) ) {
2452 return( AFPERR_PARAM );
2455 return stat_vol(bitmap, vol, rbuf, rbuflen);
2458 /* ------------------------- */
2459 int afp_setvolparams(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2463 u_int16_t vid, bitmap;
2469 memcpy(&vid, ibuf, sizeof( vid ));
2470 ibuf += sizeof( vid );
2471 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2472 bitmap = ntohs( bitmap );
2473 ibuf += sizeof(bitmap);
2475 if (( vol = getvolbyvid( vid )) == NULL ) {
2476 return( AFPERR_PARAM );
2479 if ((vol->v_flags & AFPVOL_RO))
2480 return AFPERR_VLOCK;
2482 /* we can only set the backup date. */
2483 if (bitmap != (1 << VOLPBIT_BDATE))
2484 return AFPERR_BITMAP;
2486 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2487 if ( ad_open(&ad, vol->v_path, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR) < 0 ) {
2489 return AFPERR_VLOCK;
2491 return AFPERR_ACCESS;
2494 memcpy(&aint, ibuf, sizeof(aint));
2495 ad_setdate(&ad, AD_DATE_BACKUP, aint);
2497 ad_close(&ad, ADFLAGS_HF);
2501 /* ------------------------- */
2502 int wincheck(const struct vol *vol, const char *path)
2506 if (!(vol->v_flags & AFPVOL_MSWINDOWS))
2509 /* empty paths are not allowed */
2510 if ((len = strlen(path)) == 0)
2513 /* leading or trailing whitespaces are not allowed, carriage returns
2514 * and probably other whitespace is okay, tabs are not allowed
2516 if ((path[0] == ' ') || (path[len-1] == ' '))
2519 /* certain characters are not allowed */
2520 if (strpbrk(path, MSWINDOWS_BADCHARS))
2523 /* everything else is okay */
2529 * precreate a folder
2530 * this is only intended for folders in the volume root
2531 * It will *not* work if the folder name contains extended characters
2533 static int create_special_folder (const struct vol *vol, const struct _special_folder *folder)
2542 p = (char *) malloc ( strlen(vol->v_path)+strlen(folder->name)+2);
2544 LOG(log_error, logtype_afpd,"malloc failed");
2548 q=strdup(folder->name);
2550 LOG(log_error, logtype_afpd,"malloc failed");
2554 strcpy(p, vol->v_path);
2559 if ((vol->v_casefold & AFPVOL_MTOUUPPER))
2561 else if ((vol->v_casefold & AFPVOL_MTOULOWER))
2567 if ( (ret = stat( p, &st )) < 0 ) {
2568 if (folder->precreate) {
2569 if (ad_mkdir(p, folder->mode)) {
2570 LOG(log_debug, logtype_afpd,"Creating '%s' failed in %s: %s", p, vol->v_path, strerror(errno));
2579 if ( !ret && folder->hide) {
2581 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2582 if (ad_open(&ad, p, ADFLAGS_HF | ADFLAGS_DIR, O_RDWR | O_CREAT, 0666) != 0) {
2588 ad_setname(&ad, folder->name);
2590 ad_getattr(&ad, &attr);
2591 attr |= htons( ntohs( attr ) | ATTRBIT_INVISIBLE );
2592 ad_setattr(&ad, attr);
2594 /* do the same with the finder info */
2595 if (ad_entry(&ad, ADEID_FINDERI)) {
2596 memcpy(&attr, ad_entry(&ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF, sizeof(attr));
2597 attr |= htons(FINDERINFO_INVISIBLE);
2598 memcpy(ad_entry(&ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF,&attr, sizeof(attr));
2602 ad_close_metadata( &ad);
2609 static void handle_special_folders (const struct vol * vol)
2611 const _special_folder *p = &special_folders[0];
2613 if ((vol->v_flags & AFPVOL_RO))
2616 for (; p->name != NULL; p++) {
2617 create_special_folder (vol, p);
2621 const struct vol *getvolumes(void)
2626 void unload_volumes_and_extmap(void)
2628 LOG(log_debug, logtype_afpd, "unload_volumes_and_extmap");
2634 * Get a volumes UUID from the config file.
2635 * If there is none, it is generated and stored there.
2637 * Returns pointer to allocated storage on success, NULL on error.
2639 char *get_uuid(const AFPObj *obj, const char *volname)
2642 char buf[1024], uuid[UUID_PRINTABLE_STRING_LENGTH], *p;
2644 struct stat tmpstat;
2647 if ((fp = fopen(obj->options.uuidconf, "r")) != NULL) { /* read open? */
2648 /* scan in the conf file */
2649 while (fgets(buf, sizeof(buf), fp) != NULL) {
2651 while (p && isblank(*p))
2653 if (!p || (*p == '#') || (*p == '\n'))
2654 continue; /* invalid line */
2657 if ((volname_conf = strtok( p, "\"" )) == NULL)
2658 continue; /* syntax error */
2660 if ((volname_conf = strtok( p, " \t" )) == NULL)
2661 continue; /* syntax error: invalid name */
2663 p = strchr(p, '\0');
2666 continue; /* syntax error */
2668 if (strcmp(volname, volname_conf) != 0)
2669 continue; /* another volume name */
2671 while (p && isblank(*p))
2674 if (sscanf(p, "%36s", uuid) == 1 ) {
2675 for (int i=0; uuid[i]; i++)
2676 uuid[i] = toupper(uuid[i]);
2677 LOG(log_debug, logtype_afpd, "get_uuid('%s'): UUID: '%s'", volname, uuid);
2679 return strdup(uuid);
2687 /* not found or no file, reopen in append mode */
2689 if (stat(obj->options.uuidconf, &tmpstat)) { /* no file */
2690 if (( fd = creat(obj->options.uuidconf, 0644 )) < 0 ) {
2691 LOG(log_error, logtype_atalkd, "ERROR: Cannot create %s (%s).",
2692 obj->options.uuidconf, strerror(errno));
2695 if (( fp = fdopen( fd, "w" )) == NULL ) {
2696 LOG(log_error, logtype_atalkd, "ERROR: Cannot fdopen %s (%s).",
2697 obj->options.uuidconf, strerror(errno));
2701 } else if ((fp = fopen(obj->options.uuidconf, "a+")) == NULL) { /* not found */
2702 LOG(log_error, logtype_afpd, "Cannot create or append to %s (%s).",
2703 obj->options.uuidconf, strerror(errno));
2706 fseek(fp, 0L, SEEK_END);
2707 if(ftell(fp) == 0) { /* size = 0 */
2708 fprintf(fp, "# DON'T TOUCH NOR COPY THOUGHTLESSLY!\n");
2709 fprintf(fp, "# This file is auto-generated by afpd\n");
2710 fprintf(fp, "# and stores UUIDs for TM volumes.\n\n");
2712 fseek(fp, -1L, SEEK_END);
2713 if(fgetc(fp) != '\n') fputc('\n', fp); /* last char is \n? */
2716 /* generate uuid and write to file */
2719 randombytes((void *)id, 16);
2720 cp = uuid_bin2string(id);
2722 LOG(log_debug, logtype_afpd, "get_uuid('%s'): generated UUID '%s'", volname, cp);
2724 fprintf(fp, "\"%s\"\t%36s\n", volname, cp);
2727 return strdup(uuid);