4 * Copyright (c) 1990,1993 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
10 #endif /* HAVE_CONFIG_H */
25 #else /* STDC_HEADERS */
29 #endif /* HAVE_STRCHR */
30 char *strchr (), *strrchr ();
32 #define memcpy(d,s,n) bcopy ((s), (d), (n))
33 #define memmove(d,s,n) bcopy ((s), (d), (n))
34 #endif /* ! HAVE_MEMCPY */
35 #endif /* STDC_HEADERS */
36 #include <sys/param.h>
37 #include <sys/socket.h>
38 #include <netinet/in.h>
39 #include <arpa/inet.h>
40 #include <atalk/asp.h>
41 #include <atalk/dsi.h>
42 #include <atalk/adouble.h>
43 #include <atalk/afp.h>
44 #include <atalk/util.h>
45 #include <atalk/volinfo.h>
46 #include <atalk/logger.h>
47 #include <atalk/vfs.h>
50 #include <atalk/cnid.h>
54 #include "directory.h"
62 extern int afprun(int root, char *cmd, int *outfd);
65 #define MIN(a, b) ((a) < (b) ? (a) : (b))
68 #ifndef NO_LARGE_VOL_SUPPORT
69 #if BYTE_ORDER == BIG_ENDIAN
72 #else /* BYTE_ORDER == BIG_ENDIAN */
73 #define hton64(x) ((u_int64_t) (htonl(((x) >> 32) & 0xffffffffLL)) | \
74 (u_int64_t) ((htonl(x) & 0xffffffffLL) << 32))
75 #define ntoh64(x) (hton64(x))
76 #endif /* BYTE_ORDER == BIG_ENDIAN */
77 #endif /* ! NO_LARGE_VOL_SUPPORT */
79 static struct vol *Volumes = NULL;
80 static u_int16_t lastvid = 0;
81 static char *Trash = "\02\024Network Trash Folder";
83 static struct extmap *Extmap = NULL, *Defextmap = NULL;
84 static int Extmap_cnt;
85 static void free_extmap(void);
87 #define VOLOPT_ALLOW 0 /* user allow list */
88 #define VOLOPT_DENY 1 /* user deny list */
89 #define VOLOPT_RWLIST 2 /* user rw list */
90 #define VOLOPT_ROLIST 3 /* user ro list */
91 #define VOLOPT_PASSWORD 4 /* volume password */
92 #define VOLOPT_CASEFOLD 5 /* character case mangling */
93 #define VOLOPT_FLAGS 6 /* various flags */
94 #define VOLOPT_DBPATH 7 /* path to database */
95 #define VOLOPT_LIMITSIZE 8 /* Limit the size of the volume */
97 #define VOLOPT_VETO 10 /* list of veto filespec */
98 #define VOLOPT_PREEXEC 11 /* preexec command */
99 #define VOLOPT_ROOTPREEXEC 12 /* root preexec command */
100 #define VOLOPT_POSTEXEC 13 /* postexec command */
101 #define VOLOPT_ROOTPOSTEXEC 14 /* root postexec command */
102 #define VOLOPT_ENCODING 15 /* mac encoding (pre OSX)*/
103 #define VOLOPT_MACCHARSET 16
104 #define VOLOPT_CNIDSCHEME 17
105 #define VOLOPT_ADOUBLE 18 /* adouble version */
111 #define VOLOPT_FORCEUID 19 /* force uid for username x */
112 #define VOLOPT_FORCEGID 20 /* force gid for group x */
113 #endif /* FORCE_UIDGID */
115 #define VOLOPT_UMASK 21
116 #define VOLOPT_ALLOWED_HOSTS 22
117 #define VOLOPT_DENIED_HOSTS 23
118 #define VOLOPT_DPERM 24 /* dperm default directories perms */
119 #define VOLOPT_FPERM 25 /* fperm default files perms */
120 #define VOLOPT_DFLTPERM 26 /* perm */
121 #define VOLOPT_EA_VFS 27 /* Extended Attributes vfs indirection */
122 #define VOLOPT_CNIDSERVER 28 /* CNID Server ip address*/
123 #define VOLOPT_CNIDPORT 30 /* CNID server tcp port */
125 #define VOLOPT_MAX 31 /* <== IMPORTANT !!!!!! */
126 #define VOLOPT_NUM (VOLOPT_MAX + 1)
129 #define VOLOPT_DEFAULT ":DEFAULT:"
130 #define VOLOPT_DEFAULT_LEN 9
137 typedef struct _special_folder {
144 static const _special_folder special_folders[] = {
145 {"Network Trash Folder", 1, 0777, 1},
146 {"Temporary Items", 1, 0777, 1},
147 {".AppleDesktop", 1, 0777, 0},
149 {"TheFindByContentFolder", 0, 0, 1},
150 {"TheVolumeSettingsFolder", 0, 0, 1},
154 /* Forward declarations */
155 static void handle_special_folders (const struct vol *);
156 static void deletevol(struct vol *vol);
157 static void volume_free(struct vol *vol);
158 static void check_ea_sys_support(struct vol *vol);
160 static void volfree(struct vol_option *options, const struct vol_option *save)
165 for (i = 0; i < VOLOPT_MAX; i++) {
166 if (options[i].c_value && (options[i].c_value != save[i].c_value))
167 free(options[i].c_value);
170 for (i = 0; i < VOLOPT_MAX; i++) {
171 if (options[i].c_value)
172 free(options[i].c_value);
178 /* handle variable substitutions. here's what we understand:
179 * $b -> basename of path
180 * $c -> client ip/appletalk address
181 * $d -> volume pathname on server
182 * $f -> full name (whatever's in the gecos field)
185 * $i -> client ip/appletalk address without port
186 * $s -> server name (hostname if it doesn't exist)
187 * $u -> username (guest is usually nobody)
188 * $v -> volume name or basename if null
189 * $z -> zone (may not exist)
194 #define is_var(a, b) (strncmp((a), (b), 2) == 0)
196 static char *volxlate(AFPObj *obj, char *dest, size_t destlen,
197 char *src, struct passwd *pwd, char *path, char *volname)
208 dest = calloc(destlen +1, 1);
214 strlcpy(dest, src, destlen +1);
215 if ((p = strchr(src, '$')) == NULL) /* nothing to do */
218 /* first part of the path. just forward to the next variable. */
219 len = MIN((size_t)(p - src), destlen);
225 while (p && destlen > 0) {
226 /* now figure out what the variable is */
228 if (is_var(p, "$b")) {
230 if ((q = strrchr(path, '/')) == NULL)
232 else if (*(q + 1) != '\0')
235 } else if (is_var(p, "$c")) {
236 if (obj->proto == AFPPROTO_ASP) {
237 ASP asp = obj->handle;
239 len = sprintf(dest, "%u.%u", ntohs(asp->asp_sat.sat_addr.s_net),
240 asp->asp_sat.sat_addr.s_node);
244 } else if (obj->proto == AFPPROTO_DSI) {
245 DSI *dsi = obj->handle;
246 len = sprintf(dest, "%s:%u",
247 getip_string((struct sockaddr *)&dsi->client),
248 getip_port((struct sockaddr *)&dsi->client));
252 } else if (is_var(p, "$d")) {
254 } else if (is_var(p, "$f")) {
255 if ((r = strchr(pwd->pw_gecos, ',')))
258 } else if (is_var(p, "$g")) {
259 struct group *grp = getgrgid(pwd->pw_gid);
262 } else if (is_var(p, "$h")) {
263 q = obj->options.hostname;
264 } else if (is_var(p, "$i")) {
265 if (obj->proto == AFPPROTO_ASP) {
266 ASP asp = obj->handle;
268 len = sprintf(dest, "%u", ntohs(asp->asp_sat.sat_addr.s_net));
272 } else if (obj->proto == AFPPROTO_DSI) {
273 DSI *dsi = obj->handle;
274 q = getip_string((struct sockaddr *)&dsi->client);
276 } else if (is_var(p, "$s")) {
279 else if (obj->options.server) {
280 q = obj->options.server;
282 q = obj->options.hostname;
283 } else if (is_var(p, "$u")) {
285 if ( obj->options.ntseparator && (sep = strchr(obj->username, obj->options.ntseparator[0])) != NULL)
289 } else if (is_var(p, "$v")) {
294 if ((q = strrchr(path, '/')) == NULL)
296 else if (*(q + 1) != '\0')
299 } else if (is_var(p, "$z")) {
301 } else if (is_var(p, "$$")) {
306 /* copy the stuff over. if we don't understand something that we
307 * should, just skip it over. */
309 len = MIN(p == q ? 2 : strlen(q), destlen);
310 strncpy(dest, q, len);
315 /* stuff up to next $ */
317 p = strchr(src, '$');
318 len = p ? MIN((size_t)(p - src), destlen) : destlen;
320 strncpy(dest, src, len);
328 /* to make sure that val is valid, make sure to select an opt that
330 static int optionok(const char *buf, const char *opt, const char *val)
332 if (!strstr(buf,opt))
340 /* -------------------- */
341 static void setoption(struct vol_option *options, struct vol_option *save, int opt, const char *val)
343 if (options[opt].c_value && (!save || options[opt].c_value != save[opt].c_value))
344 free(options[opt].c_value);
345 options[opt].c_value = strdup(val + 1);
348 /* ------------------------------------------
349 handle all the options. tmp can't be NULL. */
350 static void volset(struct vol_option *options, struct vol_option *save,
351 char *volname, int vlen,
356 val = strchr(tmp, ':');
358 /* we'll assume it's a volume name. */
359 strncpy(volname, tmp, vlen);
364 LOG(log_debug, logtype_afpd, "Parsing volset %s", val);
366 if (optionok(tmp, "allow:", val)) {
367 setoption(options, save, VOLOPT_ALLOW, val);
369 } else if (optionok(tmp, "deny:", val)) {
370 setoption(options, save, VOLOPT_DENY, val);
372 } else if (optionok(tmp, "rwlist:", val)) {
373 setoption(options, save, VOLOPT_RWLIST, val);
375 } else if (optionok(tmp, "rolist:", val)) {
376 setoption(options, save, VOLOPT_ROLIST, val);
378 } else if (optionok(tmp, "codepage:", val)) {
379 LOG (log_error, logtype_afpd, "The old codepage system has been removed. Please make sure to read the documentation !!!!");
380 /* Make sure we don't screw anything */
382 } else if (optionok(tmp, "volcharset:", val)) {
383 setoption(options, save, VOLOPT_ENCODING, val);
384 } else if (optionok(tmp, "maccharset:", val)) {
385 setoption(options, save, VOLOPT_MACCHARSET, val);
386 } else if (optionok(tmp, "veto:", val)) {
387 setoption(options, save, VOLOPT_VETO, val);
388 } else if (optionok(tmp, "cnidscheme:", val)) {
389 setoption(options, save, VOLOPT_CNIDSCHEME, val);
390 } else if (optionok(tmp, "casefold:", val)) {
391 if (strcasecmp(val + 1, "tolower") == 0)
392 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMLOWER;
393 else if (strcasecmp(val + 1, "toupper") == 0)
394 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMUPPER;
395 else if (strcasecmp(val + 1, "xlatelower") == 0)
396 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UUPPERMLOWER;
397 else if (strcasecmp(val + 1, "xlateupper") == 0)
398 options[VOLOPT_CASEFOLD].i_value = AFPVOL_ULOWERMUPPER;
399 } else if (optionok(tmp, "adouble:", val)) {
400 if (strcasecmp(val + 1, "v1") == 0)
401 options[VOLOPT_ADOUBLE].i_value = AD_VERSION1;
402 #if AD_VERSION == AD_VERSION2
403 else if (strcasecmp(val + 1, "v2") == 0)
404 options[VOLOPT_ADOUBLE].i_value = AD_VERSION2;
405 else if (strcasecmp(val + 1, "osx") == 0)
406 options[VOLOPT_ADOUBLE].i_value = AD_VERSION2_OSX;
407 else if (strcasecmp(val + 1, "sfm") == 0)
408 options[VOLOPT_ADOUBLE].i_value = AD_VERSION1_SFM;
410 } else if (optionok(tmp, "options:", val)) {
413 if ((p = strtok(val + 1, ",")) == NULL) /* nothing */
417 if (strcasecmp(p, "prodos") == 0)
418 options[VOLOPT_FLAGS].i_value |= AFPVOL_A2VOL;
419 else if (strcasecmp(p, "mswindows") == 0) {
420 options[VOLOPT_FLAGS].i_value |= AFPVOL_MSWINDOWS | AFPVOL_USEDOTS;
421 } else if (strcasecmp(p, "crlf") == 0)
422 options[VOLOPT_FLAGS].i_value |= AFPVOL_CRLF;
423 else if (strcasecmp(p, "noadouble") == 0)
424 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOADOUBLE;
425 else if (strcasecmp(p, "ro") == 0)
426 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
427 else if (strcasecmp(p, "nohex") == 0)
428 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOHEX;
429 else if (strcasecmp(p, "usedots") == 0)
430 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS;
431 else if (strcasecmp(p, "invisibledots") == 0)
432 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS | AFPVOL_INV_DOTS;
433 else if (strcasecmp(p, "limitsize") == 0)
434 options[VOLOPT_FLAGS].i_value |= AFPVOL_LIMITSIZE;
435 /* support for either "dropbox" or "dropkludge" */
436 else if (strcasecmp(p, "dropbox") == 0)
437 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
438 else if (strcasecmp(p, "dropkludge") == 0)
439 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
440 else if (strcasecmp(p, "nofileid") == 0)
441 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOFILEID;
442 else if (strcasecmp(p, "nostat") == 0)
443 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOSTAT;
444 else if (strcasecmp(p, "preexec_close") == 0)
445 options[VOLOPT_PREEXEC].i_value = 1;
446 else if (strcasecmp(p, "root_preexec_close") == 0)
447 options[VOLOPT_ROOTPREEXEC].i_value = 1;
448 else if (strcasecmp(p, "upriv") == 0)
449 options[VOLOPT_FLAGS].i_value |= AFPVOL_UNIX_PRIV;
450 else if (strcasecmp(p, "acls") == 0)
451 options[VOLOPT_FLAGS].i_value |= AFPVOL_ACLS;
452 else if (strcasecmp(p, "nodev") == 0)
453 options[VOLOPT_FLAGS].i_value |= AFPVOL_NODEV;
454 else if (strcasecmp(p, "caseinsensitive") == 0)
455 options[VOLOPT_FLAGS].i_value |= AFPVOL_CASEINSEN;
456 else if (strcasecmp(p, "illegalseq") == 0)
457 options[VOLOPT_FLAGS].i_value |= AFPVOL_EILSEQ;
458 else if (strcasecmp(p, "nocnidcache") == 0)
459 options[VOLOPT_FLAGS].i_value &= ~AFPVOL_CACHE;
460 else if (strcasecmp(p, "tm") == 0)
461 options[VOLOPT_FLAGS].i_value |= AFPVOL_TM;
462 /* Found this in branch dir-rewrite, maybe we want to use it sometimes */
464 else if (strcasecmp(p, "cdrom") == 0)
465 options[VOLOPT_FLAGS].i_value |= AFPVOL_CDROM | AFPVOL_RO;
467 p = strtok(NULL, ",");
470 } else if (optionok(tmp, "cnidserver:", val)) {
471 setoption(options, save, VOLOPT_CNIDSERVER, val);
473 char *p = strrchr(val + 1, ':');
476 setoption(options, save, VOLOPT_CNIDPORT, p);
479 LOG(log_debug, logtype_afpd, "CNID Server for volume '%s': %s:%s",
480 volname, val + 1, p ? p + 1 : Cnid_port);
482 } else if (optionok(tmp, "dbpath:", val)) {
483 setoption(options, save, VOLOPT_DBPATH, val);
485 } else if (optionok(tmp, "umask:", val)) {
486 options[VOLOPT_UMASK].i_value = (int)strtol(val +1, NULL, 8);
487 } else if (optionok(tmp, "dperm:", val)) {
488 options[VOLOPT_DPERM].i_value = (int)strtol(val+1, NULL, 8);
489 } else if (optionok(tmp, "fperm:", val)) {
490 options[VOLOPT_FPERM].i_value = (int)strtol(val+1, NULL, 8);
491 } else if (optionok(tmp, "perm:", val)) {
492 options[VOLOPT_DFLTPERM].i_value = (int)strtol(val+1, NULL, 8);
493 } else if (optionok(tmp, "password:", val)) {
494 setoption(options, save, VOLOPT_PASSWORD, val);
498 /* this code allows forced uid/gid per volume settings */
499 } else if (optionok(tmp, "forceuid:", val)) {
500 setoption(options, save, VOLOPT_FORCEUID, val);
501 } else if (optionok(tmp, "forcegid:", val)) {
502 setoption(options, save, VOLOPT_FORCEGID, val);
504 #endif /* FORCE_UIDGID */
505 } else if (optionok(tmp, "root_preexec:", val)) {
506 setoption(options, save, VOLOPT_ROOTPREEXEC, val);
508 } else if (optionok(tmp, "preexec:", val)) {
509 setoption(options, save, VOLOPT_PREEXEC, val);
511 } else if (optionok(tmp, "root_postexec:", val)) {
512 setoption(options, save, VOLOPT_ROOTPOSTEXEC, val);
514 } else if (optionok(tmp, "postexec:", val)) {
515 setoption(options, save, VOLOPT_POSTEXEC, val);
517 } else if (optionok(tmp, "allowed_hosts:", val)) {
518 setoption(options, save, VOLOPT_ALLOWED_HOSTS, val);
520 } else if (optionok(tmp, "denied_hosts:", val)) {
521 setoption(options, save, VOLOPT_DENIED_HOSTS, val);
523 } else if (optionok(tmp, "ea:", val)) {
524 if (strcasecmp(val + 1, "ad") == 0)
525 options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_AD;
526 else if (strcasecmp(val + 1, "sys") == 0)
527 options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_SYS;
528 else if (strcasecmp(val + 1, "none") == 0)
529 options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_NONE;
531 } else if (optionok(tmp, "volsizelimit:", val)) {
532 options[VOLOPT_LIMITSIZE].i_value = (uint32_t)strtoul(val + 1, NULL, 10);
535 /* ignore unknown options */
536 LOG(log_debug, logtype_afpd, "ignoring unknown volume option: %s", tmp);
541 /* ----------------- */
542 static void showvol(const ucs2_t *name)
545 for ( volume = Volumes; volume; volume = volume->v_next ) {
546 if (volume->v_hide && !strcasecmp_w( volume->v_name, name ) ) {
553 /* ------------------------------- */
554 static int creatvol(AFPObj *obj, struct passwd *pwd,
555 char *path, char *name,
556 struct vol_option *options,
557 const int user /* user defined volume */
561 int suffixlen, vlen, tmpvlen, u8mvlen, macvlen;
563 char tmpname[AFPVOL_U8MNAMELEN+1];
564 ucs2_t u8mtmpname[(AFPVOL_U8MNAMELEN+1)*2], mactmpname[(AFPVOL_MACNAMELEN+1)*2];
565 char suffix[6]; /* max is #FFFF */
568 if ( name == NULL || *name == '\0' ) {
569 if ((name = strrchr( path, '/' )) == NULL) {
570 return -1; /* Obviously not a fully qualified path */
573 /* if you wish to share /, you need to specify a name. */
578 /* suffix for mangling use (lastvid + 1) */
579 /* because v_vid has not been decided yet. */
580 suffixlen = sprintf(suffix, "%c%X", MANGLE_CHAR, lastvid + 1 );
582 vlen = strlen( name );
584 /* Unicode Volume Name */
585 /* Firstly convert name from unixcharset to UTF8-MAC */
587 tmpvlen = convert_charset(obj->options.unixcharset, CH_UTF8_MAC, 0, name, vlen, tmpname, AFPVOL_U8MNAMELEN, &flags);
589 strcpy(tmpname, "???");
593 /* Do we have to mangle ? */
594 if ( (flags & CONV_REQMANGLE) || (tmpvlen > obj->options.volnamelen)) {
595 if (tmpvlen + suffixlen > obj->options.volnamelen) {
597 tmpvlen = convert_charset(obj->options.unixcharset, CH_UTF8_MAC, 0, name, vlen, tmpname, obj->options.volnamelen - suffixlen, &flags);
598 tmpname[tmpvlen >= 0 ? tmpvlen : 0] = 0;
600 strcat(tmpname, suffix);
601 tmpvlen = strlen(tmpname);
604 /* Secondly convert name from UTF8-MAC to UCS2 */
605 if ( 0 >= ( u8mvlen = convert_string(CH_UTF8_MAC, CH_UCS2, tmpname, tmpvlen, u8mtmpname, AFPVOL_U8MNAMELEN*2)) )
608 LOG(log_debug, logtype_afpd, "createvol: Volume '%s' -> UTF8-MAC Name: '%s'", name, tmpname);
610 /* Maccharset Volume Name */
611 /* Firsty convert name from unixcharset to maccharset */
613 tmpvlen = convert_charset(obj->options.unixcharset, obj->options.maccharset, 0, name, vlen, tmpname, AFPVOL_U8MNAMELEN, &flags);
615 strcpy(tmpname, "???");
619 /* Do we have to mangle ? */
620 if ( (flags & CONV_REQMANGLE) || (tmpvlen > AFPVOL_MACNAMELEN)) {
621 if (tmpvlen + suffixlen > AFPVOL_MACNAMELEN) {
623 tmpvlen = convert_charset(obj->options.unixcharset, obj->options.maccharset, 0, name, vlen, tmpname, AFPVOL_MACNAMELEN - suffixlen, &flags);
624 tmpname[tmpvlen >= 0 ? tmpvlen : 0] = 0;
626 strcat(tmpname, suffix);
627 tmpvlen = strlen(tmpname);
630 /* Secondly convert name from maccharset to UCS2 */
631 if ( 0 >= ( macvlen = convert_string(obj->options.maccharset, CH_UCS2, tmpname, tmpvlen, mactmpname, AFPVOL_U8MNAMELEN*2)) )
634 LOG(log_debug, logtype_afpd, "createvol: Volume '%s' -> Longname: '%s'", name, tmpname);
636 /* check duplicate */
637 for ( volume = Volumes; volume; volume = volume->v_next ) {
638 if (( strcasecmp_w( volume->v_u8mname, u8mtmpname ) == 0 ) || ( strcasecmp_w( volume->v_macname, mactmpname ) == 0 )){
639 if (volume->v_deleted) {
640 volume->v_new = hide = 1;
643 return -1; /* Won't be able to access it, anyway... */
648 if (!( volume = (struct vol *)calloc(1, sizeof( struct vol ))) ) {
649 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
652 if ( NULL == ( volume->v_localname = strdup(name))) {
653 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
658 if ( NULL == ( volume->v_u8mname = strdup_w(u8mtmpname))) {
659 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
664 if ( NULL == ( volume->v_macname = strdup_w(mactmpname))) {
665 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
670 if (!( volume->v_path = (char *)malloc( strlen( path ) + 1 )) ) {
671 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
677 volume->v_name = utf8_encoding()?volume->v_u8mname:volume->v_macname;
678 volume->v_hide = hide;
679 strcpy( volume->v_path, path );
683 #endif /* __svr4__ */
684 /* os X start at 1 and use network order ie. 1 2 3 */
685 volume->v_vid = ++lastvid;
686 volume->v_vid = htons(volume->v_vid);
690 /* should we casefold? */
691 volume->v_casefold = options[VOLOPT_CASEFOLD].i_value;
693 /* shift in some flags */
694 volume->v_flags = options[VOLOPT_FLAGS].i_value;
696 if (options[VOLOPT_EA_VFS].i_value)
697 volume->v_vfs_ea = options[VOLOPT_EA_VFS].i_value;
699 volume->v_ad_options = 0;
700 if ((volume->v_flags & AFPVOL_NODEV))
701 volume->v_ad_options |= ADVOL_NODEV;
702 if ((volume->v_flags & AFPVOL_CACHE))
703 volume->v_ad_options |= ADVOL_CACHE;
704 if ((volume->v_flags & AFPVOL_UNIX_PRIV))
705 volume->v_ad_options |= ADVOL_UNIXPRIV;
706 if ((volume->v_flags & AFPVOL_INV_DOTS))
707 volume->v_ad_options |= ADVOL_INVDOTS;
708 if ((volume->v_flags & AFPVOL_NOADOUBLE))
709 volume->v_ad_options |= ADVOL_NOADOUBLE;
711 if (options[VOLOPT_PASSWORD].c_value)
712 volume->v_password = strdup(options[VOLOPT_PASSWORD].c_value);
714 if (options[VOLOPT_VETO].c_value)
715 volume->v_veto = strdup(options[VOLOPT_VETO].c_value);
717 if (options[VOLOPT_ENCODING].c_value)
718 volume->v_volcodepage = strdup(options[VOLOPT_ENCODING].c_value);
720 if (options[VOLOPT_MACCHARSET].c_value)
721 volume->v_maccodepage = strdup(options[VOLOPT_MACCHARSET].c_value);
723 if (options[VOLOPT_DBPATH].c_value)
724 volume->v_dbpath = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_DBPATH].c_value, pwd, path, name);
726 if (options[VOLOPT_CNIDSCHEME].c_value)
727 volume->v_cnidscheme = strdup(options[VOLOPT_CNIDSCHEME].c_value);
729 if (options[VOLOPT_CNIDSERVER].c_value)
730 volume->v_cnidserver = strdup(options[VOLOPT_CNIDSERVER].c_value);
732 if (options[VOLOPT_CNIDPORT].c_value)
733 volume->v_cnidport = strdup(options[VOLOPT_CNIDPORT].c_value);
735 if (options[VOLOPT_UMASK].i_value)
736 volume->v_umask = (mode_t)options[VOLOPT_UMASK].i_value;
738 if (options[VOLOPT_DPERM].i_value)
739 volume->v_dperm = (mode_t)options[VOLOPT_DPERM].i_value;
741 if (options[VOLOPT_FPERM].i_value)
742 volume->v_fperm = (mode_t)options[VOLOPT_FPERM].i_value;
744 if (options[VOLOPT_DFLTPERM].i_value)
745 volume->v_perm = (mode_t)options[VOLOPT_DFLTPERM].i_value;
747 if (options[VOLOPT_ADOUBLE].i_value)
748 volume->v_adouble = options[VOLOPT_ADOUBLE].i_value;
750 volume->v_adouble = AD_VERSION;
752 if (options[VOLOPT_LIMITSIZE].i_value)
753 volume->v_limitsize = options[VOLOPT_LIMITSIZE].i_value;
755 /* Mac to Unix conversion flags*/
756 volume->v_mtou_flags = 0;
757 if (!(volume->v_flags & AFPVOL_NOHEX))
758 volume->v_mtou_flags |= CONV_ESCAPEHEX;
759 if (!(volume->v_flags & AFPVOL_USEDOTS))
760 volume->v_mtou_flags |= CONV_ESCAPEDOTS;
761 if ((volume->v_flags & AFPVOL_EILSEQ))
762 volume->v_mtou_flags |= CONV__EILSEQ;
764 if ((volume->v_casefold & AFPVOL_MTOUUPPER))
765 volume->v_mtou_flags |= CONV_TOUPPER;
766 else if ((volume->v_casefold & AFPVOL_MTOULOWER))
767 volume->v_mtou_flags |= CONV_TOLOWER;
769 /* Unix to Mac conversion flags*/
770 volume->v_utom_flags = CONV_IGNORE | CONV_UNESCAPEHEX;
771 if ((volume->v_casefold & AFPVOL_UTOMUPPER))
772 volume->v_utom_flags |= CONV_TOUPPER;
773 else if ((volume->v_casefold & AFPVOL_UTOMLOWER))
774 volume->v_utom_flags |= CONV_TOLOWER;
776 if ((volume->v_flags & AFPVOL_EILSEQ))
777 volume->v_utom_flags |= CONV__EILSEQ;
780 if (options[VOLOPT_FORCEUID].c_value) {
781 volume->v_forceuid = strdup(options[VOLOPT_FORCEUID].c_value);
783 volume->v_forceuid = NULL; /* set as null so as to return 0 later on */
786 if (options[VOLOPT_FORCEGID].c_value) {
787 volume->v_forcegid = strdup(options[VOLOPT_FORCEGID].c_value);
789 volume->v_forcegid = NULL; /* set as null so as to return 0 later on */
793 if (options[VOLOPT_PREEXEC].c_value)
794 volume->v_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_PREEXEC].c_value, pwd, path, name);
795 volume->v_preexec_close = options[VOLOPT_PREEXEC].i_value;
797 if (options[VOLOPT_POSTEXEC].c_value)
798 volume->v_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_POSTEXEC].c_value, pwd, path, name);
800 if (options[VOLOPT_ROOTPREEXEC].c_value)
801 volume->v_root_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPREEXEC].c_value, pwd, path, name);
802 volume->v_root_preexec_close = options[VOLOPT_ROOTPREEXEC].i_value;
804 if (options[VOLOPT_ROOTPOSTEXEC].c_value)
805 volume->v_root_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPOSTEXEC].c_value, pwd, path, name);
808 volume->v_dperm |= volume->v_perm;
809 volume->v_fperm |= volume->v_perm;
811 /* Check EA support on volume */
812 if (volume->v_vfs_ea == AFPVOL_EA_AUTO)
813 check_ea_sys_support(volume);
816 volume->v_next = Volumes;
821 /* ---------------- */
822 static char *myfgets( char *buf, int size, FILE *fp)
828 while ((EOF != ( c = getc( fp )) ) && ( size > 1 )) {
829 if ( c == '\n' || c == '\r' ) {
830 if (p != buf && *(p -1) == '\\') {
852 /* check access list. this function wants something of the following
854 * @group,name,name2,@group2,name3
856 * a NULL argument allows everybody to have access.
857 * we return three things:
859 * 0: list exists, but name isn't in it
863 #ifndef NO_REAL_USER_NAME
864 /* authentication is case insensitive
865 * FIXME should we do the same with group name?
867 #define access_strcmp strcasecmp
870 #define access_strcmp strcmp
874 static int accessvol(const char *args, const char *name)
876 char buf[MAXPATHLEN + 1], *p;
882 strlcpy(buf, args, sizeof(buf));
883 if ((p = strtok(buf, ",")) == NULL) /* nothing, return okay */
887 if (*p == '@') { /* it's a group */
888 if ((gr = getgrnam(p + 1)) && gmem(gr->gr_gid))
890 } else if (access_strcmp(p, name) == 0) /* it's a user name */
892 p = strtok(NULL, ",");
898 static int hostaccessvol(int type, const char *volname, const char *args, const AFPObj *obj)
901 char buf[MAXPATHLEN + 1], *p, *b;
902 DSI *dsi = obj->handle;
903 struct sockaddr_storage client;
908 strlcpy(buf, args, sizeof(buf));
909 if ((p = strtok_r(buf, ",", &b)) == NULL) /* nothing, return okay */
912 if (obj->proto != AFPPROTO_DSI)
917 char *ipaddr, *mask_char;
918 struct addrinfo hints, *ai;
920 ipaddr = strtok(p, "/");
921 mask_char = strtok(NULL,"/");
923 /* Get address from string with getaddrinfo */
924 memset(&hints, 0, sizeof hints);
925 hints.ai_family = AF_UNSPEC;
926 hints.ai_socktype = SOCK_STREAM;
927 if ((ret = getaddrinfo(ipaddr, NULL, &hints, &ai)) != 0) {
928 LOG(log_error, logtype_afpd, "hostaccessvol: getaddrinfo: %s\n", gai_strerror(ret));
933 if (mask_char != NULL)
934 mask_int = atoi(mask_char); /* apply_ip_mask does range checking on it */
936 if (ai->ai_family == AF_INET) /* IPv4 */
942 /* Apply mask to addresses */
943 client = dsi->client;
944 apply_ip_mask((struct sockaddr *)&client, mask_int);
945 apply_ip_mask(ai->ai_addr, mask_int);
947 if (compare_ip((struct sockaddr *)&client, ai->ai_addr) == 0) {
948 if (type == VOLOPT_DENIED_HOSTS)
949 LOG(log_info, logtype_afpd, "AFP access denied for client IP '%s' to volume '%s' by denied list",
950 getip_string((struct sockaddr *)&client), volname);
957 p = strtok_r(NULL, ",", &b);
960 if (type == VOLOPT_ALLOWED_HOSTS)
961 LOG(log_info, logtype_afpd, "AFP access denied for client IP '%s' to volume '%s', not in allowed list",
962 getip_string((struct sockaddr *)&dsi->client), volname);
966 static void setextmap(char *ext, char *type, char *creator, int user)
971 if (Extmap == NULL) {
972 if (( Extmap = calloc(1, sizeof( struct extmap ))) == NULL ) {
973 LOG(log_error, logtype_afpd, "setextmap: calloc: %s", strerror(errno) );
978 for ( em = Extmap, cnt = 0; em->em_ext; em++, cnt++) {
979 if ( (strdiacasecmp( em->em_ext, ext )) == 0 ) {
984 if ( em->em_ext == NULL ) {
985 if (!(Extmap = realloc( Extmap, sizeof( struct extmap ) * (cnt +2))) ) {
986 LOG(log_error, logtype_afpd, "setextmap: realloc: %s", strerror(errno) );
989 (Extmap +cnt +1)->em_ext = NULL;
991 } else if ( !user ) {
997 if (!(em->em_ext = strdup( ext))) {
998 LOG(log_error, logtype_afpd, "setextmap: strdup: %s", strerror(errno) );
1002 if ( *type == '\0' ) {
1003 memcpy(em->em_type, "\0\0\0\0", sizeof( em->em_type ));
1005 memcpy(em->em_type, type, sizeof( em->em_type ));
1007 if ( *creator == '\0' ) {
1008 memcpy(em->em_creator, "\0\0\0\0", sizeof( em->em_creator ));
1010 memcpy(em->em_creator, creator, sizeof( em->em_creator ));
1014 /* -------------------------- */
1015 static int extmap_cmp(const void *map1, const void *map2)
1017 const struct extmap *em1 = map1;
1018 const struct extmap *em2 = map2;
1019 return strdiacasecmp(em1->em_ext, em2->em_ext);
1022 static void sortextmap( void)
1027 if ((em = Extmap) == NULL) {
1030 while (em->em_ext) {
1035 qsort(Extmap, Extmap_cnt, sizeof(struct extmap), extmap_cmp);
1036 if (*Extmap->em_ext == 0) {
1037 /* the first line is really "." the default entry,
1038 * we remove the leading '.' in setextmap
1045 /* ----------------------
1047 static void free_extmap( void)
1052 for ( em = Extmap; em->em_ext; em++) {
1062 /* ----------------------
1064 static int volfile_changed(struct afp_volume_name *p)
1070 name = p->full_name;
1074 if (!stat( name, &st) && st.st_mtime > p->mtime) {
1075 p->mtime = st.st_mtime;
1081 /* ----------------------
1082 * Read a volume configuration file and add the volumes contained within to
1083 * the global volume list. If p2 is non-NULL, the file that is opened is
1086 * Lines that begin with # and blank lines are ignored.
1087 * Volume lines are of the form:
1088 * <unix path> [<volume name>] [allow:<user>,<@group>,...] \
1089 * [codepage:<file>] [casefold:<num>]
1090 * <extension> TYPE [CREATOR]
1092 static int readvolfile(AFPObj *obj, struct afp_volume_name *p1, char *p2, int user, struct passwd *pwent)
1095 char path[MAXPATHLEN + 1];
1096 char tmp[MAXPATHLEN + 1];
1097 char volname[AFPVOL_U8MNAMELEN + 1];
1099 char type[5], creator[5];
1102 struct vol_option save_options[VOLOPT_NUM];
1103 struct vol_option options[VOLOPT_NUM];
1111 strcpy( path, p1->name );
1113 strcat( path, "/" );
1115 if (p1->full_name) {
1116 free(p1->full_name);
1118 p1->full_name = strdup(path);
1121 if (NULL == ( fp = fopen( path, "r" )) ) {
1125 if (fd != -1 && !fstat( fd, &st) ) {
1126 p1->mtime = st.st_mtime;
1129 memset(save_options, 0, sizeof(save_options));
1131 /* Enable some default options for all volumes */
1132 save_options[VOLOPT_FLAGS].i_value |= AFPVOL_CACHE;
1133 save_options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_AUTO;
1135 while ( myfgets( buf, sizeof( buf ), fp ) != NULL ) {
1136 initline( strlen( buf ), buf );
1137 parseline( sizeof( path ) - 1, path );
1144 /* change the default options for this file */
1145 if (strncmp(path, VOLOPT_DEFAULT, VOLOPT_DEFAULT_LEN) == 0) {
1147 for (i = 0; i < VOLOPT_NUM; i++) {
1148 if (parseline( sizeof( path ) - VOLOPT_DEFAULT_LEN - 1,
1149 path + VOLOPT_DEFAULT_LEN) < 0)
1151 volset(save_options, NULL, tmp, sizeof(tmp) - 1,
1152 path + VOLOPT_DEFAULT_LEN);
1158 if (( p = strchr( path, '/' )) != NULL ) {
1166 if ( u == NULL || *u == '\0' || ( pw = getpwnam( u )) == NULL ) {
1169 strcpy( tmp, pw->pw_dir );
1170 if ( p != NULL && *p != '\0' ) {
1174 /* Tag a user's home directory with their umask. Note, this will
1175 * be overwritten if the user actually specifies a umask: option
1176 * for a '~' volume. */
1177 save_options[VOLOPT_UMASK].i_value = obj->options.save_mask;
1181 /* send path through variable substitution */
1182 if (*path != '~') /* need to copy path to tmp */
1185 pwent = getpwnam(obj->username);
1186 volxlate(obj, path, sizeof(path) - 1, tmp, pwent, NULL, NULL);
1188 /* this is sort of braindead. basically, i want to be
1189 * able to specify things in any order, but i don't want to
1190 * re-write everything.
1192 * currently we have options:
1200 * options:prodos,crlf,noadouble,ro...
1205 * namemask:x,y,!z (not implemented yet)
1207 memcpy(options, save_options, sizeof(options));
1210 /* read in up to VOLOP_NUM possible options */
1211 for (i = 0; i < VOLOPT_NUM; i++) {
1212 if (parseline( sizeof( tmp ) - 1, tmp ) < 0)
1215 volset(options, save_options, volname, sizeof(volname) - 1, tmp);
1218 /* check allow/deny lists:
1219 allow -> either no list (-1), or in list (1)
1220 deny -> either no list (-1), or not in list (0) */
1221 if (accessvol(options[VOLOPT_ALLOW].c_value, obj->username) &&
1222 (accessvol(options[VOLOPT_DENY].c_value, obj->username) < 1) &&
1223 hostaccessvol(VOLOPT_ALLOWED_HOSTS, volname, options[VOLOPT_ALLOWED_HOSTS].c_value, obj) &&
1224 (hostaccessvol(VOLOPT_DENIED_HOSTS, volname, options[VOLOPT_DENIED_HOSTS].c_value, obj) < 1)) {
1226 /* handle read-only behaviour. semantics:
1227 * 1) neither the rolist nor the rwlist exist -> rw
1228 * 2) rolist exists -> ro if user is in it.
1229 * 3) rwlist exists -> ro unless user is in it. */
1230 if (((options[VOLOPT_FLAGS].i_value & AFPVOL_RO) == 0) &&
1231 ((accessvol(options[VOLOPT_ROLIST].c_value,
1232 obj->username) == 1) ||
1233 !accessvol(options[VOLOPT_RWLIST].c_value,
1235 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
1237 /* do variable substitution for volname */
1238 volxlate(obj, tmp, sizeof(tmp) - 1, volname, pwent, path, NULL);
1239 creatvol(obj, pwent, path, tmp, options, p2 != NULL);
1241 volfree(options, save_options);
1245 parseline( sizeof( type ) - 1, type );
1246 parseline( sizeof( creator ) - 1, creator );
1247 setextmap( path, type, creator, user);
1254 volfree(save_options, NULL);
1256 if ( fclose( fp ) != 0 ) {
1257 LOG(log_error, logtype_afpd, "readvolfile: fclose: %s", strerror(errno) );
1263 /* ------------------------------- */
1264 static void volume_free(struct vol *vol)
1266 free(vol->v_localname);
1267 vol->v_localname = NULL;
1268 free(vol->v_u8mname);
1269 vol->v_u8mname = NULL;
1270 free(vol->v_macname);
1271 vol->v_macname = NULL;
1273 free(vol->v_password);
1275 free(vol->v_volcodepage);
1276 free(vol->v_maccodepage);
1277 free(vol->v_cnidscheme);
1278 free(vol->v_dbpath);
1281 free(vol->v_forceuid);
1282 free(vol->v_forcegid);
1283 #endif /* FORCE_UIDGID */
1286 /* ------------------------------- */
1287 static void free_volumes(void )
1290 struct vol *nvol, *ovol;
1292 for ( vol = Volumes; vol; vol = vol->v_next ) {
1293 if (( vol->v_flags & AFPVOL_OPEN ) ) {
1300 for ( vol = Volumes, ovol = NULL; vol; vol = nvol) {
1303 if (vol->v_localname == NULL) {
1304 if (Volumes == vol) {
1309 ovol->v_next = nvol;
1319 /* ------------------------------- */
1320 static void volume_unlink(struct vol *volume)
1322 struct vol *vol, *ovol, *nvol;
1324 if (volume == Volumes) {
1325 Volumes = Volumes->v_next;
1328 for ( vol = Volumes->v_next, ovol = Volumes; vol; vol = nvol) {
1331 if (vol == volume) {
1332 ovol->v_next = nvol;
1341 static int getvolspace(struct vol *vol,
1342 u_int32_t *bfree, u_int32_t *btotal,
1343 VolSpace *xbfree, VolSpace *xbtotal, u_int32_t *bsize)
1347 #ifndef NO_QUOTA_SUPPORT
1348 VolSpace qfree, qtotal;
1351 spaceflag = AFPVOL_GVSMASK & vol->v_flags;
1352 /* report up to 2GB if afp version is < 2.2 (4GB if not) */
1353 maxsize = (vol->v_flags & AFPVOL_A2VOL) ? 0x01fffe00 :
1354 (((afp_version < 22) || (vol->v_flags & AFPVOL_LIMITSIZE))
1355 ? 0x7fffffffL : 0xffffffffL);
1358 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_AFSGVS ) {
1359 if ( afs_getvolspace( vol, xbfree, xbtotal, bsize ) == AFP_OK ) {
1360 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_AFSGVS;
1361 goto getvolspace_done;
1366 if (( rc = ustatfs_getvolspace( vol, xbfree, xbtotal, bsize)) != AFP_OK ) {
1370 #define min(a,b) ((a)<(b)?(a):(b))
1371 #ifndef NO_QUOTA_SUPPORT
1372 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_UQUOTA ) {
1373 if ( uquota_getvolspace( vol, &qfree, &qtotal, *bsize ) == AFP_OK ) {
1374 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_UQUOTA;
1375 *xbfree = min(*xbfree, qfree);
1376 *xbtotal = min( *xbtotal, qtotal);
1377 goto getvolspace_done;
1381 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_USTATFS;
1384 if (vol->v_limitsize) {
1385 /* FIXME: Free could be limit minus (total minus used), */
1386 /* which will confuse the client less ? */
1387 *xbfree = min(*xbfree, (vol->v_limitsize * 1024 * 1024));
1388 *xbtotal = min(*xbtotal, (vol->v_limitsize * 1024 * 1024));
1391 *bfree = min( *xbfree, maxsize);
1392 *btotal = min( *xbtotal, maxsize);
1396 /* -----------------------
1397 * set volume creation date
1398 * avoid duplicate, well at least it tries
1400 static void vol_setdate(u_int16_t id, struct adouble *adp, time_t date)
1403 struct vol *vol = Volumes;
1405 for ( volume = Volumes; volume; volume = volume->v_next ) {
1406 if (volume->v_vid == id) {
1409 else if ((time_t)(AD_DATE_FROM_UNIX(date)) == volume->v_ctime) {
1411 volume = Volumes; /* restart */
1414 vol->v_ctime = AD_DATE_FROM_UNIX(date);
1415 ad_setdate(adp, AD_DATE_CREATE | AD_DATE_UNIX, date);
1418 /* ----------------------- */
1419 static int getvolparams( u_int16_t bitmap, struct vol *vol, struct stat *st, char *buf, size_t *buflen)
1422 int bit = 0, isad = 1;
1425 u_int32_t bfree, btotal, bsize;
1426 VolSpace xbfree, xbtotal; /* extended bytes */
1427 char *data, *nameoff = NULL;
1430 LOG(log_debug, logtype_afpd, "getvolparams: Volume '%s'", vol->v_localname);
1432 /* courtesy of jallison@whistle.com:
1433 * For MacOS8.x support we need to create the
1434 * .Parent file here if it doesn't exist. */
1436 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1437 if ( ad_open_metadata( vol->v_path, ADFLAGS_DIR, O_CREAT, &ad) < 0 ) {
1439 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1441 } else if (ad_get_MD_flags( &ad ) & O_CREAT) {
1442 slash = strrchr( vol->v_path, '/' );
1446 slash = vol->v_path;
1447 if (ad_getentryoff(&ad, ADEID_NAME)) {
1448 ad_setentrylen( &ad, ADEID_NAME, strlen( slash ));
1449 memcpy(ad_entry( &ad, ADEID_NAME ), slash,
1450 ad_getentrylen( &ad, ADEID_NAME ));
1452 vol_setdate(vol->v_vid, &ad, st->st_mtime);
1456 if (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0)
1457 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1459 vol->v_ctime = aint;
1462 if (( bitmap & ( (1<<VOLPBIT_BFREE)|(1<<VOLPBIT_BTOTAL) |
1463 (1<<VOLPBIT_XBFREE)|(1<<VOLPBIT_XBTOTAL) |
1464 (1<<VOLPBIT_BSIZE)) ) != 0 ) {
1465 if ( getvolspace( vol, &bfree, &btotal, &xbfree, &xbtotal,
1466 &bsize) != AFP_OK ) {
1468 ad_close( &ad, ADFLAGS_HF );
1470 return( AFPERR_PARAM );
1475 while ( bitmap != 0 ) {
1476 while (( bitmap & 1 ) == 0 ) {
1484 /* check for read-only.
1485 * NOTE: we don't actually set the read-only flag unless
1486 * it's passed in that way as it's possible to mount
1487 * a read-write filesystem under a read-only one. */
1488 if ((vol->v_flags & AFPVOL_RO) ||
1489 ((utime(vol->v_path, NULL) < 0) && (errno == EROFS))) {
1490 ashort |= VOLPBIT_ATTR_RO;
1492 /* prior 2.1 only VOLPBIT_ATTR_RO is defined */
1493 if (afp_version > 20) {
1494 if (0 == (vol->v_flags & AFPVOL_NOFILEID) && vol->v_cdb != NULL &&
1495 (vol->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1496 ashort |= VOLPBIT_ATTR_FILEID;
1498 ashort |= VOLPBIT_ATTR_CATSEARCH;
1500 if (afp_version >= 30) {
1501 ashort |= VOLPBIT_ATTR_UTF8;
1502 if (vol->v_flags & AFPVOL_UNIX_PRIV)
1503 ashort |= VOLPBIT_ATTR_UNIXPRIV;
1504 if (vol->v_flags & AFPVOL_TM)
1505 ashort |= VOLPBIT_ATTR_TM;
1507 if (afp_version >= 32) {
1509 ashort |= VOLPBIT_ATTR_EXT_ATTRS;
1510 if (vol->v_flags & AFPVOL_ACLS)
1511 ashort |= VOLPBIT_ATTR_ACLS;
1515 ashort = htons(ashort);
1516 memcpy(data, &ashort, sizeof( ashort ));
1517 data += sizeof( ashort );
1521 ashort = htons( AFPVOLSIG_DEFAULT );
1522 memcpy(data, &ashort, sizeof( ashort ));
1523 data += sizeof( ashort );
1526 case VOLPBIT_CDATE :
1527 aint = vol->v_ctime;
1528 memcpy(data, &aint, sizeof( aint ));
1529 data += sizeof( aint );
1532 case VOLPBIT_MDATE :
1533 if ( st->st_mtime > vol->v_mtime ) {
1534 vol->v_mtime = st->st_mtime;
1536 aint = AD_DATE_FROM_UNIX(vol->v_mtime);
1537 memcpy(data, &aint, sizeof( aint ));
1538 data += sizeof( aint );
1541 case VOLPBIT_BDATE :
1542 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1543 aint = AD_DATE_START;
1544 memcpy(data, &aint, sizeof( aint ));
1545 data += sizeof( aint );
1549 memcpy(data, &vol->v_vid, sizeof( vol->v_vid ));
1550 data += sizeof( vol->v_vid );
1553 case VOLPBIT_BFREE :
1554 bfree = htonl( bfree );
1555 memcpy(data, &bfree, sizeof( bfree ));
1556 data += sizeof( bfree );
1559 case VOLPBIT_BTOTAL :
1560 btotal = htonl( btotal );
1561 memcpy(data, &btotal, sizeof( btotal ));
1562 data += sizeof( btotal );
1565 #ifndef NO_LARGE_VOL_SUPPORT
1566 case VOLPBIT_XBFREE :
1567 xbfree = hton64( xbfree );
1568 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1569 bcopy(&xbfree, data, sizeof(xbfree));
1570 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1571 memcpy(data, &xbfree, sizeof( xbfree ));
1572 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1573 data += sizeof( xbfree );
1576 case VOLPBIT_XBTOTAL :
1577 xbtotal = hton64( xbtotal );
1578 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1579 bcopy(&xbtotal, data, sizeof(xbtotal));
1580 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1581 memcpy(data, &xbtotal, sizeof( xbtotal ));
1582 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1583 data += sizeof( xbfree );
1585 #endif /* ! NO_LARGE_VOL_SUPPORT */
1589 data += sizeof( u_int16_t );
1592 case VOLPBIT_BSIZE: /* block size */
1593 bsize = htonl(bsize);
1594 memcpy(data, &bsize, sizeof(bsize));
1595 data += sizeof(bsize);
1600 ad_close( &ad, ADFLAGS_HF );
1602 return( AFPERR_BITMAP );
1608 ashort = htons( data - buf );
1609 memcpy(nameoff, &ashort, sizeof( ashort ));
1610 /* name is always in mac charset */
1611 aint = ucs2_to_charset( vol->v_maccharset, vol->v_macname, data+1, AFPVOL_MACNAMELEN + 1);
1621 ad_close_metadata( &ad);
1623 *buflen = data - buf;
1627 /* ------------------------- */
1628 static int stat_vol(u_int16_t bitmap, struct vol *vol, char *rbuf, size_t *rbuflen)
1634 if ( stat( vol->v_path, &st ) < 0 ) {
1636 return( AFPERR_PARAM );
1638 /* save the volume device number */
1639 vol->v_dev = st.st_dev;
1641 buflen = *rbuflen - sizeof( bitmap );
1642 if (( ret = getvolparams( bitmap, vol, &st,
1643 rbuf + sizeof( bitmap ), &buflen )) != AFP_OK ) {
1647 *rbuflen = buflen + sizeof( bitmap );
1648 bitmap = htons( bitmap );
1649 memcpy(rbuf, &bitmap, sizeof( bitmap ));
1654 /* ------------------------------- */
1655 void load_volumes(AFPObj *obj)
1657 struct passwd *pwent;
1662 /* check files date */
1663 if (obj->options.defaultvol.loaded) {
1664 changed = volfile_changed(&obj->options.defaultvol);
1666 if (obj->options.systemvol.loaded) {
1667 changed |= volfile_changed(&obj->options.systemvol);
1669 if (obj->options.uservol.loaded) {
1670 changed |= volfile_changed(&obj->options.uservol);
1679 pwent = getpwnam(obj->username);
1680 if ( (obj->options.flags & OPTION_USERVOLFIRST) == 0 ) {
1681 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent);
1684 if ((*obj->username == '\0') || (obj->options.flags & OPTION_NOUSERVOL)) {
1685 readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent);
1688 * Read user's AppleVolumes or .AppleVolumes file
1689 * If neither are readable, read the default volumes file. if
1690 * that doesn't work, create a user share.
1692 if (obj->options.uservol.name) {
1693 free(obj->options.uservol.name);
1695 obj->options.uservol.name = strdup(pwent->pw_dir);
1696 if ( readvolfile(obj, &obj->options.uservol, "AppleVolumes", 1, pwent) < 0 &&
1697 readvolfile(obj, &obj->options.uservol, ".AppleVolumes", 1, pwent) < 0 &&
1698 readvolfile(obj, &obj->options.uservol, "applevolumes", 1, pwent) < 0 &&
1699 readvolfile(obj, &obj->options.uservol, ".applevolumes", 1, pwent) < 0 &&
1700 obj->options.defaultvol.name != NULL ) {
1701 if (readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent) < 0)
1702 creatvol(obj, pwent, pwent->pw_dir, NULL, NULL, 1);
1705 if ( obj->options.flags & OPTION_USERVOLFIRST ) {
1706 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent );
1709 if ( obj->options.closevol ) {
1712 for ( vol = Volumes; vol; vol = vol->v_next ) {
1713 if (vol->v_deleted && !vol->v_new ) {
1722 /* ------------------------------- */
1723 int afp_getsrvrparms(AFPObj *obj, char *ibuf _U_, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
1736 for ( vcnt = 0, volume = Volumes; volume; volume = volume->v_next ) {
1737 if (!(volume->v_flags & AFPVOL_NOSTAT)) {
1740 if ( stat( volume->v_path, &st ) < 0 ) {
1741 LOG(log_info, logtype_afpd, "afp_getsrvrparms(%s): stat: %s",
1742 volume->v_path, strerror(errno) );
1743 continue; /* can't access directory */
1745 if (!S_ISDIR(st.st_mode)) {
1746 continue; /* not a dir */
1748 accessmode(volume->v_path, &ma, NULL, &st);
1749 if ((ma.ma_user & (AR_UREAD | AR_USEARCH)) != (AR_UREAD | AR_USEARCH)) {
1750 continue; /* no r-x access */
1753 if (volume->v_hide) {
1754 continue; /* config file changed but the volume was mounted */
1757 if (utf8_encoding()) {
1758 len = ucs2_to_charset_allocate(CH_UTF8_MAC, &namebuf, volume->v_u8mname);
1760 len = ucs2_to_charset_allocate(obj->options.maccharset, &namebuf, volume->v_macname);
1763 if (len == (size_t)-1)
1766 /* set password bit if there's a volume password */
1767 *data = (volume->v_password) ? AFPSRVR_PASSWD : 0;
1769 /* Apple 2 clients running ProDOS-8 expect one volume to have
1770 bit 0 of this byte set. They will not recognize anything
1771 on the server unless this is the case. I have not
1772 completely worked this out, but it's related to booting
1773 from the server. Support for that function is a ways
1774 off.. <shirsch@ibm.net> */
1775 *data |= (volume->v_flags & AFPVOL_A2VOL) ? AFPSRVR_CONFIGINFO : 0;
1776 *data++ |= 0; /* UNIX PRIVS BIT ..., OSX doesn't seem to use it, so we don't either */
1778 memcpy(data, namebuf, len );
1784 *rbuflen = data - rbuf;
1786 if ( gettimeofday( &tv, NULL ) < 0 ) {
1787 LOG(log_error, logtype_afpd, "afp_getsrvrparms(%s): gettimeofday: %s", volume->v_path, strerror(errno) );
1789 return AFPERR_PARAM;
1791 tv.tv_sec = AD_DATE_FROM_UNIX(tv.tv_sec);
1792 memcpy(data, &tv.tv_sec, sizeof( u_int32_t));
1793 data += sizeof( u_int32_t);
1798 /* ------------------------- */
1799 static int volume_codepage(AFPObj *obj, struct vol *volume)
1801 struct charset_functions *charset;
1804 if (!volume->v_volcodepage)
1805 volume->v_volcodepage = strdup("UTF8");
1807 if ( (charset_t) -1 == ( volume->v_volcharset = add_charset(volume->v_volcodepage)) ) {
1808 LOG (log_error, logtype_afpd, "Setting codepage %s as volume codepage failed", volume->v_volcodepage);
1812 if ( NULL == (charset = find_charset_functions(volume->v_volcodepage)) || charset->flags & CHARSET_ICONV ) {
1813 LOG (log_warning, logtype_afpd, "WARNING: volume encoding %s is *not* supported by netatalk, expect problems !!!!", volume->v_volcodepage);
1816 if (!volume->v_maccodepage)
1817 volume->v_maccodepage = strdup(obj->options.maccodepage);
1819 if ( (charset_t) -1 == ( volume->v_maccharset = add_charset(volume->v_maccodepage)) ) {
1820 LOG (log_error, logtype_afpd, "Setting codepage %s as mac codepage failed", volume->v_maccodepage);
1824 if ( NULL == ( charset = find_charset_functions(volume->v_maccodepage)) || ! (charset->flags & CHARSET_CLIENT) ) {
1825 LOG (log_error, logtype_afpd, "Fatal error: mac charset %s not supported", volume->v_maccodepage);
1828 volume->v_kTextEncoding = htonl(charset->kTextEncoding);
1832 /* ------------------------- */
1833 static int volume_openDB(struct vol *volume)
1837 if ((volume->v_flags & AFPVOL_NODEV)) {
1838 flags |= CNID_FLAG_NODEV;
1841 if (volume->v_cnidscheme == NULL) {
1842 volume->v_cnidscheme = strdup(DEFAULT_CNID_SCHEME);
1843 LOG(log_info, logtype_afpd, "Volume %s use CNID scheme %s.", volume->v_path, volume->v_cnidscheme);
1846 LOG(log_info, logtype_afpd, "%s:%s",
1847 volume->v_cnidserver ? volume->v_cnidserver : Cnid_srv,
1848 volume->v_cnidport ? volume->v_cnidport : Cnid_port);
1851 /* Found this in branch dir-rewrite, maybe we want to use it sometimes */
1853 /* Legacy pre 2.1 way of sharing eg CD-ROM */
1854 if (strcmp(volume->v_cnidscheme, "last") == 0) {
1855 /* "last" is gone. We support it by switching to in-memory "tdb" */
1856 volume->v_cnidscheme = strdup("tdb");
1857 flags |= CNID_FLAG_MEMORY;
1860 /* New way of sharing CD-ROM */
1861 if (volume->v_flags & AFPVOL_CDROM) {
1862 flags |= CNID_FLAG_MEMORY;
1863 if (strcmp(volume->v_cnidscheme, "tdb") != 0) {
1864 free(volume->v_cnidscheme);
1865 volume->v_cnidscheme = strdup("tdb");
1866 LOG(log_info, logtype_afpd, "Volume %s is ejectable, switching to scheme %s.",
1867 volume->v_path, volume->v_cnidscheme);
1872 volume->v_cdb = cnid_open(volume->v_dbpath ? volume->v_dbpath : volume->v_path,
1874 volume->v_cnidscheme,
1876 volume->v_cnidserver ? volume->v_cnidserver : Cnid_srv,
1877 volume->v_cnidport ? volume->v_cnidport : Cnid_port);
1880 if ( ! volume->v_cdb && ! (flags & CNID_FLAG_MEMORY)) {
1881 /* The first attempt failed and it wasn't yet an attempt to open in-memory */
1882 flags |= CNID_FLAG_MEMORY;
1883 LOG(log_error, logtype_afpd, "Reopen volume %s using in memory temporary CNID DB.", volume->v_path);
1884 volume->v_cdb = cnid_open (volume->v_path, volume->v_umask, "tdb", flags, NULL, NULL);
1886 /* kill ourself with SIGUSR2 aka msg pending */
1887 if (volume->v_cdb) {
1888 setmessage("Something wrong with the volume's DB ... FIXME with a better msg");
1889 kill(getpid(), SIGUSR2);
1890 /* XXX desactivate cachecnid ? */
1895 return (!volume->v_cdb)?-1:0;
1899 Check if the underlying filesystem supports EAs for ea:sys volumes.
1900 If not, switch to ea:ad.
1901 As we can't check (requires write access) on ro-volumes, we switch ea:auto
1902 volumes that are options:ro to ea:none.
1904 static void check_ea_sys_support(struct vol *vol)
1906 uid_t process_uid = 0;
1907 char eaname[] = {"org.netatalk.supports-eas.XXXXXX"};
1908 const char *eacontent = "yes";
1910 if (vol->v_vfs_ea == AFPVOL_EA_AUTO) {
1912 if ((vol->v_flags & AFPVOL_RO) == AFPVOL_RO) {
1913 LOG(log_info, logtype_logger, "read-only volume '%s', can't test for EA support, disabling EAs", vol->v_localname);
1914 vol->v_vfs_ea = AFPVOL_EA_NONE;
1920 process_uid = geteuid();
1922 if (seteuid(0) == -1) {
1923 LOG(log_error, logtype_logger, "check_ea_sys_support: can't seteuid(0): %s", strerror(errno));
1927 if ((sys_setxattr(vol->v_path, eaname, eacontent, 4, 0)) == 0) {
1928 sys_removexattr(vol->v_path, eaname);
1929 vol->v_vfs_ea = AFPVOL_EA_SYS;
1931 LOG(log_warning, logtype_afpd, "volume \"%s\" does not support Extended Attributes, using ea:ad instead",
1933 vol->v_vfs_ea = AFPVOL_EA_AD;
1937 if (seteuid(process_uid) == -1) {
1938 LOG(log_error, logtype_logger, "can't seteuid back %s", strerror(errno));
1945 /* -------------------------
1946 * we are the user here
1948 int afp_openvol(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
1959 char path[ MAXPATHLEN + 1];
1965 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1966 bitmap = ntohs( bitmap );
1967 ibuf += sizeof( bitmap );
1970 if (( bitmap & (1<<VOLPBIT_VID)) == 0 ) {
1971 return AFPERR_BITMAP;
1974 len = (unsigned char)*ibuf++;
1975 volname = obj->oldtmp;
1977 if ((volname_tmp = strchr(volname,'+')) != NULL)
1978 volname = volname_tmp+1;
1980 if (utf8_encoding()) {
1981 namelen = convert_string(CH_UTF8_MAC, CH_UCS2, ibuf, len, volname, sizeof(obj->oldtmp));
1983 namelen = convert_string(obj->options.maccharset, CH_UCS2, ibuf, len, volname, sizeof(obj->oldtmp));
1986 if ( namelen <= 0) {
1987 return AFPERR_PARAM;
1991 if ((len + 1) & 1) /* pad to an even boundary */
1996 for ( volume = Volumes; volume; volume = volume->v_next ) {
1997 if ( strcasecmp_w( (ucs2_t*) volname, volume->v_name ) == 0 ) {
2002 if ( volume == NULL ) {
2003 return AFPERR_PARAM;
2006 /* check for a volume password */
2007 if (volume->v_password && strncmp(ibuf, volume->v_password, VOLPASSLEN)) {
2008 return AFPERR_ACCESS;
2011 if (( volume->v_flags & AFPVOL_OPEN ) ) {
2012 /* the volume is already open */
2014 set_uidgid ( volume );
2016 return stat_vol(bitmap, volume, rbuf, rbuflen);
2019 if (volume->v_root_preexec) {
2020 if ((ret = afprun(1, volume->v_root_preexec, NULL)) && volume->v_root_preexec_close) {
2021 LOG(log_error, logtype_afpd, "afp_openvol(%s): root preexec : %d", volume->v_path, ret );
2027 set_uidgid ( volume );
2030 if (volume->v_preexec) {
2031 if ((ret = afprun(0, volume->v_preexec, NULL)) && volume->v_preexec_close) {
2032 LOG(log_error, logtype_afpd, "afp_openvol(%s): preexec : %d", volume->v_path, ret );
2037 if ( stat( volume->v_path, &st ) < 0 ) {
2038 return AFPERR_PARAM;
2041 if ( chdir( volume->v_path ) < 0 ) {
2042 return AFPERR_PARAM;
2045 if ( NULL == getcwd(path, MAXPATHLEN)) {
2046 /* shouldn't be fatal but it will fail later */
2047 LOG(log_error, logtype_afpd, "afp_openvol(%s): volume pathlen too long", volume->v_path);
2051 /* Normalize volume path */
2052 #ifdef REALPATH_TAKES_NULL
2053 if ((volume->v_path = realpath(path, NULL)) == NULL)
2056 if ((volume->v_path = malloc(MAXPATHLEN+1)) == NULL)
2058 if (realpath(path, volume->v_path) == NULL) {
2059 free(volume->v_path);
2062 /* Safe some memory */
2064 if ((tmp = strdup(volume->v_path)) == NULL) {
2065 free(volume->v_path);
2068 free(volume->v_path);
2069 volume->v_path = tmp;
2072 if (volume_codepage(obj, volume) < 0) {
2077 /* initialize volume variables
2080 if (utf8_encoding()) {
2081 volume->max_filename = 255;
2084 volume->max_filename = MACFILELEN;
2087 volume->v_flags |= AFPVOL_OPEN;
2088 volume->v_cdb = NULL;
2090 if (utf8_encoding()) {
2091 len = convert_string_allocate(CH_UCS2, CH_UTF8_MAC, volume->v_u8mname, namelen, &vol_mname);
2093 len = convert_string_allocate(CH_UCS2, obj->options.maccharset, volume->v_macname, namelen, &vol_mname);
2095 if ( !vol_mname || len <= 0) {
2100 if ((vol_uname = strrchr(path, '/')) == NULL)
2102 else if (*(vol_uname + 1) != '\0')
2105 if ((dir = dir_new(vol_mname,
2110 bfromcstr(volume->v_path))
2113 LOG(log_error, logtype_afpd, "afp_openvol(%s): malloc: %s", volume->v_path, strerror(errno) );
2119 volume->v_root = dir;
2122 if (volume_openDB(volume) < 0) {
2123 LOG(log_error, logtype_afpd, "Fatal error: cannot open CNID or invalid CNID backend for %s: %s",
2124 volume->v_path, volume->v_cnidscheme);
2129 ret = stat_vol(bitmap, volume, rbuf, rbuflen);
2130 if (ret == AFP_OK) {
2132 if (!(volume->v_flags & AFPVOL_RO)) {
2133 handle_special_folders( volume );
2135 volume->v_cnidserver ? volume->v_cnidserver : Cnid_srv,
2136 volume->v_cnidport ? volume->v_cnidport : Cnid_port);
2140 * If you mount a volume twice, the second time the trash appears on
2141 * the desk-top. That's because the Mac remembers the DID for the
2142 * trash (even for volumes in different zones, on different servers).
2143 * Just so this works better, we prime the DID cache with the trash,
2144 * fixing the trash at DID 17.
2145 * FIXME (RL): should it be done inside a CNID backend ? (always returning Trash DID when asked) ?
2147 if ((volume->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
2149 /* FIXME find db time stamp */
2150 if (cnid_getstamp(volume->v_cdb, volume->v_stamp, sizeof(volume->v_stamp)) < 0) {
2151 LOG (log_error, logtype_afpd,
2152 "afp_openvol(%s): Fatal error: Unable to get stamp value from CNID backend",
2160 cname( volume, volume->v_root, &p );
2166 if (volume->v_root) {
2167 dir_free( volume->v_root );
2168 volume->v_root = NULL;
2171 volume->v_flags &= ~AFPVOL_OPEN;
2172 if (volume->v_cdb != NULL) {
2173 cnid_close(volume->v_cdb);
2174 volume->v_cdb = NULL;
2180 /* ------------------------- */
2181 static void closevol(struct vol *vol)
2186 dir_free( vol->v_root );
2188 if (vol->v_cdb != NULL) {
2189 cnid_close(vol->v_cdb);
2193 if (vol->v_postexec) {
2194 afprun(0, vol->v_postexec, NULL);
2196 if (vol->v_root_postexec) {
2197 afprun(1, vol->v_root_postexec, NULL);
2201 /* ------------------------- */
2202 void close_all_vol(void)
2206 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
2207 if ( (ovol->v_flags & AFPVOL_OPEN) ) {
2208 ovol->v_flags &= ~AFPVOL_OPEN;
2214 /* ------------------------- */
2215 static void deletevol(struct vol *vol)
2219 vol->v_flags &= ~AFPVOL_OPEN;
2220 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
2221 if ( (ovol->v_flags & AFPVOL_OPEN) ) {
2225 if ( ovol != NULL ) {
2226 /* Even if chdir fails, we can't say afp_closevol fails. */
2227 if ( chdir( ovol->v_path ) == 0 ) {
2228 curdir = ovol->v_root;
2233 if (vol->v_deleted) {
2234 showvol(vol->v_name);
2241 /* ------------------------- */
2242 int afp_closevol(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2249 memcpy(&vid, ibuf, sizeof( vid ));
2250 if (NULL == ( vol = getvolbyvid( vid )) ) {
2251 return( AFPERR_PARAM );
2259 /* ------------------------- */
2260 struct vol *getvolbyvid(const u_int16_t vid )
2264 for ( vol = Volumes; vol; vol = vol->v_next ) {
2265 if ( vid == vol->v_vid ) {
2269 if ( vol == NULL || ( vol->v_flags & AFPVOL_OPEN ) == 0 ) {
2275 #endif /* FORCE_UIDGID */
2280 /* ------------------------ */
2281 static int ext_cmp_key(const void *key, const void *obj)
2283 const char *p = key;
2284 const struct extmap *em = obj;
2285 return strdiacasecmp(p, em->em_ext);
2287 struct extmap *getextmap(const char *path)
2292 if (!Extmap_cnt || NULL == ( p = strrchr( path, '.' )) ) {
2293 return( Defextmap );
2297 return( Defextmap );
2299 em = bsearch(p, Extmap, Extmap_cnt, sizeof(struct extmap), ext_cmp_key);
2303 return( Defextmap );
2307 /* ------------------------- */
2308 struct extmap *getdefextmap(void)
2310 return( Defextmap );
2313 /* --------------------------
2314 poll if a volume is changed by other processes.
2316 0 no attention msg sent
2317 1 attention msg sent
2318 -1 error (socket closed)
2320 Note: if attention return -1 no packet has been
2321 sent because the buffer is full, we don't care
2322 either there's no reader or there's a lot of
2323 traffic and another pollvoltime will follow
2325 int pollvoltime(AFPObj *obj)
2332 if (!(afp_version > 21 && obj->options.server_notif))
2335 if ( gettimeofday( &tv, NULL ) < 0 )
2338 for ( vol = Volumes; vol; vol = vol->v_next ) {
2339 if ( (vol->v_flags & AFPVOL_OPEN) && vol->v_mtime + 30 < tv.tv_sec) {
2340 if ( !stat( vol->v_path, &st ) && vol->v_mtime != st.st_mtime ) {
2341 vol->v_mtime = st.st_mtime;
2342 if (!obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED))
2351 /* ------------------------- */
2352 void setvoltime(AFPObj *obj, struct vol *vol)
2356 /* just looking at vol->v_mtime is broken seriously since updates
2357 * from other users afpd processes never are seen.
2358 * This is not the most elegant solution (a shared memory between
2359 * the afpd processes would come closer)
2362 if ( gettimeofday( &tv, NULL ) < 0 ) {
2363 LOG(log_error, logtype_afpd, "setvoltime(%s): gettimeofday: %s", vol->v_path, strerror(errno) );
2366 if( utime( vol->v_path, NULL ) < 0 ) {
2367 /* write of time failed ... probably a read only filesys,
2368 * where no other users can interfere, so there's no issue here
2372 /* a little granularity */
2373 if (vol->v_mtime < tv.tv_sec) {
2374 vol->v_mtime = tv.tv_sec;
2375 /* or finder doesn't update free space
2376 * AFP 3.2 and above clients seem to be ok without so many notification
2378 if (afp_version < 32 && obj->options.server_notif) {
2379 obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED);
2384 /* ------------------------- */
2385 int afp_getvolparams(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_,char *rbuf, size_t *rbuflen)
2388 u_int16_t vid, bitmap;
2391 memcpy(&vid, ibuf, sizeof( vid ));
2392 ibuf += sizeof( vid );
2393 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2394 bitmap = ntohs( bitmap );
2396 if (NULL == ( vol = getvolbyvid( vid )) ) {
2398 return( AFPERR_PARAM );
2401 return stat_vol(bitmap, vol, rbuf, rbuflen);
2404 /* ------------------------- */
2405 int afp_setvolparams(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2409 u_int16_t vid, bitmap;
2415 memcpy(&vid, ibuf, sizeof( vid ));
2416 ibuf += sizeof( vid );
2417 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2418 bitmap = ntohs( bitmap );
2419 ibuf += sizeof(bitmap);
2421 if (( vol = getvolbyvid( vid )) == NULL ) {
2422 return( AFPERR_PARAM );
2425 if ((vol->v_flags & AFPVOL_RO))
2426 return AFPERR_VLOCK;
2428 /* we can only set the backup date. */
2429 if (bitmap != (1 << VOLPBIT_BDATE))
2430 return AFPERR_BITMAP;
2432 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2433 if ( ad_open( vol->v_path, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR,
2436 return AFPERR_VLOCK;
2438 return AFPERR_ACCESS;
2441 memcpy(&aint, ibuf, sizeof(aint));
2442 ad_setdate(&ad, AD_DATE_BACKUP, aint);
2444 ad_close(&ad, ADFLAGS_HF);
2448 /* ------------------------- */
2449 int wincheck(const struct vol *vol, const char *path)
2453 if (!(vol->v_flags & AFPVOL_MSWINDOWS))
2456 /* empty paths are not allowed */
2457 if ((len = strlen(path)) == 0)
2460 /* leading or trailing whitespaces are not allowed, carriage returns
2461 * and probably other whitespace is okay, tabs are not allowed
2463 if ((path[0] == ' ') || (path[len-1] == ' '))
2466 /* certain characters are not allowed */
2467 if (strpbrk(path, MSWINDOWS_BADCHARS))
2470 /* everything else is okay */
2476 * precreate a folder
2477 * this is only intended for folders in the volume root
2478 * It will *not* work if the folder name contains extended characters
2480 static int create_special_folder (const struct vol *vol, const struct _special_folder *folder)
2489 p = (char *) malloc ( strlen(vol->v_path)+strlen(folder->name)+2);
2491 LOG(log_error, logtype_afpd,"malloc failed");
2495 q=strdup(folder->name);
2497 LOG(log_error, logtype_afpd,"malloc failed");
2501 strcpy(p, vol->v_path);
2506 if ((vol->v_casefold & AFPVOL_MTOUUPPER))
2508 else if ((vol->v_casefold & AFPVOL_MTOULOWER))
2514 if ( (ret = stat( p, &st )) < 0 ) {
2515 if (folder->precreate) {
2516 if (ad_mkdir(p, folder->mode)) {
2517 LOG(log_debug, logtype_afpd,"Creating '%s' failed in %s: %s", p, vol->v_path, strerror(errno));
2526 if ( !ret && folder->hide) {
2528 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2529 if (ad_open_metadata( p, ADFLAGS_DIR, O_CREAT, &ad) < 0) {
2535 ad_setname(&ad, folder->name);
2537 ad_getattr(&ad, &attr);
2538 attr |= htons( ntohs( attr ) | ATTRBIT_INVISIBLE );
2539 ad_setattr(&ad, attr);
2541 /* do the same with the finder info */
2542 if (ad_entry(&ad, ADEID_FINDERI)) {
2543 memcpy(&attr, ad_entry(&ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF, sizeof(attr));
2544 attr |= htons(FINDERINFO_INVISIBLE);
2545 memcpy(ad_entry(&ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF,&attr, sizeof(attr));
2549 ad_close_metadata( &ad);
2556 static void handle_special_folders (const struct vol * vol)
2558 const _special_folder *p = &special_folders[0];
2560 if ((vol->v_flags & AFPVOL_RO))
2563 for (; p->name != NULL; p++) {
2564 create_special_folder (vol, p);