2 * $Id: volume.c,v 1.122 2010-03-31 09:47:32 franklahm Exp $
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_MAPCHARS 8 /* does mtou and utom mappings. syntax:
96 m and u can be double-byte hex
101 !u -> make u illegal always
102 ~u -> make u illegal only as the first
103 part of a double-byte character.
105 #define VOLOPT_VETO 10 /* list of veto filespec */
106 #define VOLOPT_PREEXEC 11 /* preexec command */
107 #define VOLOPT_ROOTPREEXEC 12 /* root preexec command */
108 #define VOLOPT_POSTEXEC 13 /* postexec command */
109 #define VOLOPT_ROOTPOSTEXEC 14 /* root postexec command */
110 #define VOLOPT_ENCODING 15 /* mac encoding (pre OSX)*/
111 #define VOLOPT_MACCHARSET 16
112 #define VOLOPT_CNIDSCHEME 17
113 #define VOLOPT_ADOUBLE 18 /* adouble version */
119 #define VOLOPT_FORCEUID 19 /* force uid for username x */
120 #define VOLOPT_FORCEGID 20 /* force gid for group x */
121 #endif /* FORCE_UIDGID */
123 #define VOLOPT_UMASK 21
124 #define VOLOPT_ALLOWED_HOSTS 22
125 #define VOLOPT_DENIED_HOSTS 23
126 #define VOLOPT_DPERM 24 /* dperm default directories perms */
127 #define VOLOPT_FPERM 25 /* fperm default files perms */
128 #define VOLOPT_DFLTPERM 26 /* perm */
129 #define VOLOPT_EA_VFS 27 /* Extended Attributes vfs indirection */
130 #define VOLOPT_CNIDSERVER 28 /* CNID Server ip address*/
131 #define VOLOPT_CNIDPORT 30 /* CNID server tcp port */
133 #define VOLOPT_MAX 31 /* <== IMPORTANT !!!!!! */
134 #define VOLOPT_NUM (VOLOPT_MAX + 1)
137 #define VOLOPT_DEFAULT ":DEFAULT:"
138 #define VOLOPT_DEFAULT_LEN 9
145 typedef struct _special_folder {
152 static const _special_folder special_folders[] = {
153 {"Network Trash Folder", 1, 0777, 1},
154 {"Temporary Items", 1, 0777, 1},
155 {".AppleDesktop", 1, 0777, 0},
157 {"TheFindByContentFolder", 0, 0, 1},
158 {"TheVolumeSettingsFolder", 0, 0, 1},
162 /* Forward declarations */
163 static void handle_special_folders (const struct vol *);
164 static void deletevol(struct vol *vol);
165 static void volume_free(struct vol *vol);
166 static void check_ea_sys_support(struct vol *vol);
168 static void volfree(struct vol_option *options, const struct vol_option *save)
173 for (i = 0; i < VOLOPT_MAX; i++) {
174 if (options[i].c_value && (options[i].c_value != save[i].c_value))
175 free(options[i].c_value);
178 for (i = 0; i < VOLOPT_MAX; i++) {
179 if (options[i].c_value)
180 free(options[i].c_value);
186 /* handle variable substitutions. here's what we understand:
187 * $b -> basename of path
188 * $c -> client ip/appletalk address
189 * $d -> volume pathname on server
190 * $f -> full name (whatever's in the gecos field)
193 * $i -> client ip/appletalk address without port
194 * $s -> server name (hostname if it doesn't exist)
195 * $u -> username (guest is usually nobody)
196 * $v -> volume name or basename if null
197 * $z -> zone (may not exist)
202 #define is_var(a, b) (strncmp((a), (b), 2) == 0)
204 static char *volxlate(AFPObj *obj, char *dest, size_t destlen,
205 char *src, struct passwd *pwd, char *path, char *volname)
216 dest = calloc(destlen +1, 1);
222 strlcpy(dest, src, destlen +1);
223 if ((p = strchr(src, '$')) == NULL) /* nothing to do */
226 /* first part of the path. just forward to the next variable. */
227 len = MIN((size_t)(p - src), destlen);
233 while (p && destlen > 0) {
234 /* now figure out what the variable is */
236 if (is_var(p, "$b")) {
238 if ((q = strrchr(path, '/')) == NULL)
240 else if (*(q + 1) != '\0')
243 } else if (is_var(p, "$c")) {
244 if (obj->proto == AFPPROTO_ASP) {
245 ASP asp = obj->handle;
247 len = sprintf(dest, "%u.%u", ntohs(asp->asp_sat.sat_addr.s_net),
248 asp->asp_sat.sat_addr.s_node);
252 } else if (obj->proto == AFPPROTO_DSI) {
253 DSI *dsi = obj->handle;
254 len = sprintf(dest, "%s:%u",
255 getip_string((struct sockaddr *)&dsi->client),
256 getip_port((struct sockaddr *)&dsi->client));
260 } else if (is_var(p, "$d")) {
262 } else if (is_var(p, "$f")) {
263 if ((r = strchr(pwd->pw_gecos, ',')))
266 } else if (is_var(p, "$g")) {
267 struct group *grp = getgrgid(pwd->pw_gid);
270 } else if (is_var(p, "$h")) {
271 q = obj->options.hostname;
272 } else if (is_var(p, "$i")) {
273 if (obj->proto == AFPPROTO_ASP) {
274 ASP asp = obj->handle;
276 len = sprintf(dest, "%u", ntohs(asp->asp_sat.sat_addr.s_net));
280 } else if (obj->proto == AFPPROTO_DSI) {
281 DSI *dsi = obj->handle;
282 q = getip_string((struct sockaddr *)&dsi->client);
284 } else if (is_var(p, "$s")) {
287 else if (obj->options.server) {
288 q = obj->options.server;
290 q = obj->options.hostname;
291 } else if (is_var(p, "$u")) {
293 if ( obj->options.ntseparator && (sep = strchr(obj->username, obj->options.ntseparator[0])) != NULL)
297 } else if (is_var(p, "$v")) {
302 if ((q = strrchr(path, '/')) == NULL)
304 else if (*(q + 1) != '\0')
307 } else if (is_var(p, "$z")) {
309 } else if (is_var(p, "$$")) {
314 /* copy the stuff over. if we don't understand something that we
315 * should, just skip it over. */
317 len = MIN(p == q ? 2 : strlen(q), destlen);
318 strncpy(dest, q, len);
323 /* stuff up to next $ */
325 p = strchr(src, '$');
326 len = p ? MIN((size_t)(p - src), destlen) : destlen;
328 strncpy(dest, src, len);
336 /* to make sure that val is valid, make sure to select an opt that
338 static int optionok(const char *buf, const char *opt, const char *val)
340 if (!strstr(buf,opt))
348 /* -------------------- */
349 static void setoption(struct vol_option *options, struct vol_option *save, int opt, const char *val)
351 if (options[opt].c_value && (!save || options[opt].c_value != save[opt].c_value))
352 free(options[opt].c_value);
353 options[opt].c_value = strdup(val + 1);
356 /* ------------------------------------------
357 handle all the options. tmp can't be NULL. */
358 static void volset(struct vol_option *options, struct vol_option *save,
359 char *volname, int vlen,
364 val = strchr(tmp, ':');
366 /* we'll assume it's a volume name. */
367 strncpy(volname, tmp, vlen);
372 LOG(log_debug, logtype_afpd, "Parsing volset %s", val);
374 if (optionok(tmp, "allow:", val)) {
375 setoption(options, save, VOLOPT_ALLOW, val);
377 } else if (optionok(tmp, "deny:", val)) {
378 setoption(options, save, VOLOPT_DENY, val);
380 } else if (optionok(tmp, "rwlist:", val)) {
381 setoption(options, save, VOLOPT_RWLIST, val);
383 } else if (optionok(tmp, "rolist:", val)) {
384 setoption(options, save, VOLOPT_ROLIST, val);
386 } else if (optionok(tmp, "codepage:", val)) {
387 LOG (log_error, logtype_afpd, "The old codepage system has been removed. Please make sure to read the documentation !!!!");
388 /* Make sure we don't screw anything */
390 } else if (optionok(tmp, "volcharset:", val)) {
391 setoption(options, save, VOLOPT_ENCODING, val);
392 } else if (optionok(tmp, "maccharset:", val)) {
393 setoption(options, save, VOLOPT_MACCHARSET, val);
394 } else if (optionok(tmp, "veto:", val)) {
395 setoption(options, save, VOLOPT_VETO, val);
396 } else if (optionok(tmp, "cnidscheme:", val)) {
397 setoption(options, save, VOLOPT_CNIDSCHEME, val);
398 } else if (optionok(tmp, "casefold:", val)) {
399 if (strcasecmp(val + 1, "tolower") == 0)
400 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMLOWER;
401 else if (strcasecmp(val + 1, "toupper") == 0)
402 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMUPPER;
403 else if (strcasecmp(val + 1, "xlatelower") == 0)
404 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UUPPERMLOWER;
405 else if (strcasecmp(val + 1, "xlateupper") == 0)
406 options[VOLOPT_CASEFOLD].i_value = AFPVOL_ULOWERMUPPER;
407 } else if (optionok(tmp, "adouble:", val)) {
408 if (strcasecmp(val + 1, "v1") == 0)
409 options[VOLOPT_ADOUBLE].i_value = AD_VERSION1;
410 #if AD_VERSION == AD_VERSION2
411 else if (strcasecmp(val + 1, "v2") == 0)
412 options[VOLOPT_ADOUBLE].i_value = AD_VERSION2;
413 else if (strcasecmp(val + 1, "osx") == 0)
414 options[VOLOPT_ADOUBLE].i_value = AD_VERSION2_OSX;
415 else if (strcasecmp(val + 1, "sfm") == 0)
416 options[VOLOPT_ADOUBLE].i_value = AD_VERSION1_SFM;
418 } else if (optionok(tmp, "options:", val)) {
421 if ((p = strtok(val + 1, ",")) == NULL) /* nothing */
425 if (strcasecmp(p, "prodos") == 0)
426 options[VOLOPT_FLAGS].i_value |= AFPVOL_A2VOL;
427 else if (strcasecmp(p, "mswindows") == 0) {
428 options[VOLOPT_FLAGS].i_value |= AFPVOL_MSWINDOWS | AFPVOL_USEDOTS;
429 } else if (strcasecmp(p, "crlf") == 0)
430 options[VOLOPT_FLAGS].i_value |= AFPVOL_CRLF;
431 else if (strcasecmp(p, "noadouble") == 0)
432 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOADOUBLE;
433 else if (strcasecmp(p, "ro") == 0)
434 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
435 else if (strcasecmp(p, "nohex") == 0)
436 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOHEX;
437 else if (strcasecmp(p, "usedots") == 0)
438 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS;
439 else if (strcasecmp(p, "invisibledots") == 0)
440 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS | AFPVOL_INV_DOTS;
441 else if (strcasecmp(p, "limitsize") == 0)
442 options[VOLOPT_FLAGS].i_value |= AFPVOL_LIMITSIZE;
443 /* support for either "dropbox" or "dropkludge" */
444 else if (strcasecmp(p, "dropbox") == 0)
445 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
446 else if (strcasecmp(p, "dropkludge") == 0)
447 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
448 else if (strcasecmp(p, "nofileid") == 0)
449 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOFILEID;
450 else if (strcasecmp(p, "nostat") == 0)
451 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOSTAT;
452 else if (strcasecmp(p, "preexec_close") == 0)
453 options[VOLOPT_PREEXEC].i_value = 1;
454 else if (strcasecmp(p, "root_preexec_close") == 0)
455 options[VOLOPT_ROOTPREEXEC].i_value = 1;
456 else if (strcasecmp(p, "upriv") == 0)
457 options[VOLOPT_FLAGS].i_value |= AFPVOL_UNIX_PRIV;
458 else if (strcasecmp(p, "acls") == 0)
459 options[VOLOPT_FLAGS].i_value |= AFPVOL_ACLS;
460 else if (strcasecmp(p, "nodev") == 0)
461 options[VOLOPT_FLAGS].i_value |= AFPVOL_NODEV;
462 else if (strcasecmp(p, "caseinsensitive") == 0)
463 options[VOLOPT_FLAGS].i_value |= AFPVOL_CASEINSEN;
464 else if (strcasecmp(p, "illegalseq") == 0)
465 options[VOLOPT_FLAGS].i_value |= AFPVOL_EILSEQ;
466 else if (strcasecmp(p, "nocnidcache") == 0)
467 options[VOLOPT_FLAGS].i_value &= ~AFPVOL_CACHE;
468 else if (strcasecmp(p, "tm") == 0)
469 options[VOLOPT_FLAGS].i_value |= AFPVOL_TM;
471 p = strtok(NULL, ",");
474 } else if (optionok(tmp, "cnidserver:", val)) {
475 setoption(options, save, VOLOPT_CNIDSERVER, val);
477 char *p = strrchr(val + 1, ':');
480 setoption(options, save, VOLOPT_CNIDPORT, p);
483 LOG(log_debug, logtype_afpd, "CNID Server for volume '%s': %s:%s",
484 volname, val + 1, p ? p + 1 : Cnid_port);
486 } else if (optionok(tmp, "dbpath:", val)) {
487 setoption(options, save, VOLOPT_DBPATH, val);
489 } else if (optionok(tmp, "umask:", val)) {
490 options[VOLOPT_UMASK].i_value = (int)strtol(val +1, NULL, 8);
491 } else if (optionok(tmp, "dperm:", val)) {
492 options[VOLOPT_DPERM].i_value = (int)strtol(val+1, NULL, 8);
493 } else if (optionok(tmp, "fperm:", val)) {
494 options[VOLOPT_FPERM].i_value = (int)strtol(val+1, NULL, 8);
495 } else if (optionok(tmp, "perm:", val)) {
496 options[VOLOPT_DFLTPERM].i_value = (int)strtol(val+1, NULL, 8);
497 } else if (optionok(tmp, "mapchars:",val)) {
498 setoption(options, save, VOLOPT_MAPCHARS, val);
500 } else if (optionok(tmp, "password:", val)) {
501 setoption(options, save, VOLOPT_PASSWORD, val);
505 /* this code allows forced uid/gid per volume settings */
506 } else if (optionok(tmp, "forceuid:", val)) {
507 setoption(options, save, VOLOPT_FORCEUID, val);
508 } else if (optionok(tmp, "forcegid:", val)) {
509 setoption(options, save, VOLOPT_FORCEGID, val);
511 #endif /* FORCE_UIDGID */
512 } else if (optionok(tmp, "root_preexec:", val)) {
513 setoption(options, save, VOLOPT_ROOTPREEXEC, val);
515 } else if (optionok(tmp, "preexec:", val)) {
516 setoption(options, save, VOLOPT_PREEXEC, val);
518 } else if (optionok(tmp, "root_postexec:", val)) {
519 setoption(options, save, VOLOPT_ROOTPOSTEXEC, val);
521 } else if (optionok(tmp, "postexec:", val)) {
522 setoption(options, save, VOLOPT_POSTEXEC, val);
524 } else if (optionok(tmp, "allowed_hosts:", val)) {
525 setoption(options, save, VOLOPT_ALLOWED_HOSTS, val);
527 } else if (optionok(tmp, "denied_hosts:", val)) {
528 setoption(options, save, VOLOPT_DENIED_HOSTS, val);
530 } else if (optionok(tmp, "ea:", val)) {
531 if (strcasecmp(val + 1, "ad") == 0)
532 options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_AD;
533 else if (strcasecmp(val + 1, "sys") == 0)
534 options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_SYS;
535 else if (strcasecmp(val + 1, "none") == 0)
536 options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_NONE;
539 /* ignore unknown options */
540 LOG(log_debug, logtype_afpd, "ignoring unknown volume option: %s", tmp);
545 /* ----------------- */
546 static void showvol(const ucs2_t *name)
549 for ( volume = Volumes; volume; volume = volume->v_next ) {
550 if (volume->v_hide && !strcasecmp_w( volume->v_name, name ) ) {
557 /* ------------------------------- */
558 static int creatvol(AFPObj *obj, struct passwd *pwd,
559 char *path, char *name,
560 struct vol_option *options,
561 const int user /* user defined volume */
565 int suffixlen, vlen, tmpvlen, u8mvlen, macvlen;
567 char tmpname[AFPVOL_U8MNAMELEN+1];
568 ucs2_t u8mtmpname[(AFPVOL_U8MNAMELEN+1)*2], mactmpname[(AFPVOL_MACNAMELEN+1)*2];
569 char suffix[6]; /* max is #FFFF */
572 if ( name == NULL || *name == '\0' ) {
573 if ((name = strrchr( path, '/' )) == NULL) {
574 return -1; /* Obviously not a fully qualified path */
577 /* if you wish to share /, you need to specify a name. */
582 /* suffix for mangling use (lastvid + 1) */
583 /* because v_vid has not been decided yet. */
584 suffixlen = sprintf(suffix, "%c%X", MANGLE_CHAR, lastvid + 1 );
586 vlen = strlen( name );
588 /* Unicode Volume Name */
589 /* Firstly convert name from unixcharset to UTF8-MAC */
591 tmpvlen = convert_charset(obj->options.unixcharset, CH_UTF8_MAC, 0, name, vlen, tmpname, AFPVOL_U8MNAMELEN, &flags);
593 strcpy(tmpname, "???");
597 /* Do we have to mangle ? */
598 if ( (flags & CONV_REQMANGLE) || (tmpvlen > obj->options.volnamelen)) {
599 if (tmpvlen + suffixlen > obj->options.volnamelen) {
601 tmpvlen = convert_charset(obj->options.unixcharset, CH_UTF8_MAC, 0, name, vlen, tmpname, obj->options.volnamelen - suffixlen, &flags);
602 tmpname[tmpvlen >= 0 ? tmpvlen : 0] = 0;
604 strcat(tmpname, suffix);
605 tmpvlen = strlen(tmpname);
608 /* Secondly convert name from UTF8-MAC to UCS2 */
609 if ( 0 >= ( u8mvlen = convert_string(CH_UTF8_MAC, CH_UCS2, tmpname, tmpvlen, u8mtmpname, AFPVOL_U8MNAMELEN*2)) )
612 LOG(log_debug, logtype_afpd, "createvol: Volume '%s' -> UTF8-MAC Name: '%s'", name, tmpname);
614 /* Maccharset Volume Name */
615 /* Firsty convert name from unixcharset to maccharset */
617 tmpvlen = convert_charset(obj->options.unixcharset, obj->options.maccharset, 0, name, vlen, tmpname, AFPVOL_U8MNAMELEN, &flags);
619 strcpy(tmpname, "???");
623 /* Do we have to mangle ? */
624 if ( (flags & CONV_REQMANGLE) || (tmpvlen > AFPVOL_MACNAMELEN)) {
625 if (tmpvlen + suffixlen > AFPVOL_MACNAMELEN) {
627 tmpvlen = convert_charset(obj->options.unixcharset, obj->options.maccharset, 0, name, vlen, tmpname, AFPVOL_MACNAMELEN - suffixlen, &flags);
628 tmpname[tmpvlen >= 0 ? tmpvlen : 0] = 0;
630 strcat(tmpname, suffix);
631 tmpvlen = strlen(tmpname);
634 /* Secondly convert name from maccharset to UCS2 */
635 if ( 0 >= ( macvlen = convert_string(obj->options.maccharset, CH_UCS2, tmpname, tmpvlen, mactmpname, AFPVOL_U8MNAMELEN*2)) )
638 LOG(log_debug, logtype_afpd, "createvol: Volume '%s' -> Longname: '%s'", name, tmpname);
640 /* check duplicate */
641 for ( volume = Volumes; volume; volume = volume->v_next ) {
642 if (( strcasecmp_w( volume->v_u8mname, u8mtmpname ) == 0 ) || ( strcasecmp_w( volume->v_macname, mactmpname ) == 0 )){
643 if (volume->v_deleted) {
644 volume->v_new = hide = 1;
647 return -1; /* Won't be able to access it, anyway... */
652 if (!( volume = (struct vol *)calloc(1, sizeof( struct vol ))) ) {
653 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
656 if ( NULL == ( volume->v_localname = strdup(name))) {
657 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
662 if ( NULL == ( volume->v_u8mname = strdup_w(u8mtmpname))) {
663 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
668 if ( NULL == ( volume->v_macname = strdup_w(mactmpname))) {
669 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
674 if (!( volume->v_path = (char *)malloc( strlen( path ) + 1 )) ) {
675 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
681 volume->v_name = utf8_encoding()?volume->v_u8mname:volume->v_macname;
682 volume->v_hide = hide;
683 strcpy( volume->v_path, path );
687 #endif /* __svr4__ */
688 /* os X start at 1 and use network order ie. 1 2 3 */
689 volume->v_vid = ++lastvid;
690 volume->v_vid = htons(volume->v_vid);
694 /* should we casefold? */
695 volume->v_casefold = options[VOLOPT_CASEFOLD].i_value;
697 /* shift in some flags */
698 volume->v_flags = options[VOLOPT_FLAGS].i_value;
700 if (options[VOLOPT_EA_VFS].i_value)
701 volume->v_vfs_ea = options[VOLOPT_EA_VFS].i_value;
703 volume->v_ad_options = 0;
704 if ((volume->v_flags & AFPVOL_NODEV))
705 volume->v_ad_options |= ADVOL_NODEV;
706 if ((volume->v_flags & AFPVOL_CACHE))
707 volume->v_ad_options |= ADVOL_CACHE;
708 if ((volume->v_flags & AFPVOL_UNIX_PRIV))
709 volume->v_ad_options |= ADVOL_UNIXPRIV;
710 if ((volume->v_flags & AFPVOL_INV_DOTS))
711 volume->v_ad_options |= ADVOL_INVDOTS;
712 if ((volume->v_flags & AFPVOL_NOADOUBLE))
713 volume->v_ad_options |= ADVOL_NOADOUBLE;
715 if (options[VOLOPT_PASSWORD].c_value)
716 volume->v_password = strdup(options[VOLOPT_PASSWORD].c_value);
718 if (options[VOLOPT_VETO].c_value)
719 volume->v_veto = strdup(options[VOLOPT_VETO].c_value);
721 if (options[VOLOPT_ENCODING].c_value)
722 volume->v_volcodepage = strdup(options[VOLOPT_ENCODING].c_value);
724 if (options[VOLOPT_MACCHARSET].c_value)
725 volume->v_maccodepage = strdup(options[VOLOPT_MACCHARSET].c_value);
727 if (options[VOLOPT_DBPATH].c_value)
728 volume->v_dbpath = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_DBPATH].c_value, pwd, path, name);
730 if (options[VOLOPT_CNIDSCHEME].c_value)
731 volume->v_cnidscheme = strdup(options[VOLOPT_CNIDSCHEME].c_value);
733 if (options[VOLOPT_CNIDSERVER].c_value)
734 volume->v_cnidserver = strdup(options[VOLOPT_CNIDSERVER].c_value);
736 if (options[VOLOPT_CNIDPORT].c_value)
737 volume->v_cnidport = strdup(options[VOLOPT_CNIDPORT].c_value);
739 if (options[VOLOPT_UMASK].i_value)
740 volume->v_umask = (mode_t)options[VOLOPT_UMASK].i_value;
742 if (options[VOLOPT_DPERM].i_value)
743 volume->v_dperm = (mode_t)options[VOLOPT_DPERM].i_value;
745 if (options[VOLOPT_FPERM].i_value)
746 volume->v_fperm = (mode_t)options[VOLOPT_FPERM].i_value;
748 if (options[VOLOPT_DFLTPERM].i_value)
749 volume->v_perm = (mode_t)options[VOLOPT_DFLTPERM].i_value;
751 if (options[VOLOPT_ADOUBLE].i_value)
752 volume->v_adouble = options[VOLOPT_ADOUBLE].i_value;
754 volume->v_adouble = AD_VERSION;
756 /* Mac to Unix conversion flags*/
757 volume->v_mtou_flags = 0;
758 if (!(volume->v_flags & AFPVOL_NOHEX))
759 volume->v_mtou_flags |= CONV_ESCAPEHEX;
760 if (!(volume->v_flags & AFPVOL_USEDOTS))
761 volume->v_mtou_flags |= CONV_ESCAPEDOTS;
762 if ((volume->v_flags & AFPVOL_EILSEQ))
763 volume->v_mtou_flags |= CONV__EILSEQ;
765 if ((volume->v_casefold & AFPVOL_MTOUUPPER))
766 volume->v_mtou_flags |= CONV_TOUPPER;
767 else if ((volume->v_casefold & AFPVOL_MTOULOWER))
768 volume->v_mtou_flags |= CONV_TOLOWER;
770 /* Unix to Mac conversion flags*/
771 volume->v_utom_flags = CONV_IGNORE | CONV_UNESCAPEHEX;
772 if ((volume->v_casefold & AFPVOL_UTOMUPPER))
773 volume->v_utom_flags |= CONV_TOUPPER;
774 else if ((volume->v_casefold & AFPVOL_UTOMLOWER))
775 volume->v_utom_flags |= CONV_TOLOWER;
777 if ((volume->v_flags & AFPVOL_EILSEQ))
778 volume->v_utom_flags |= CONV__EILSEQ;
781 if (options[VOLOPT_FORCEUID].c_value) {
782 volume->v_forceuid = strdup(options[VOLOPT_FORCEUID].c_value);
784 volume->v_forceuid = NULL; /* set as null so as to return 0 later on */
787 if (options[VOLOPT_FORCEGID].c_value) {
788 volume->v_forcegid = strdup(options[VOLOPT_FORCEGID].c_value);
790 volume->v_forcegid = NULL; /* set as null so as to return 0 later on */
794 if (options[VOLOPT_PREEXEC].c_value)
795 volume->v_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_PREEXEC].c_value, pwd, path, name);
796 volume->v_preexec_close = options[VOLOPT_PREEXEC].i_value;
798 if (options[VOLOPT_POSTEXEC].c_value)
799 volume->v_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_POSTEXEC].c_value, pwd, path, name);
801 if (options[VOLOPT_ROOTPREEXEC].c_value)
802 volume->v_root_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPREEXEC].c_value, pwd, path, name);
803 volume->v_root_preexec_close = options[VOLOPT_ROOTPREEXEC].i_value;
805 if (options[VOLOPT_ROOTPOSTEXEC].c_value)
806 volume->v_root_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPOSTEXEC].c_value, pwd, path, name);
809 volume->v_dperm |= volume->v_perm;
810 volume->v_fperm |= volume->v_perm;
812 /* Check EA support on volume */
813 if (volume->v_vfs_ea == AFPVOL_EA_AUTO)
814 check_ea_sys_support(volume);
817 volume->v_next = Volumes;
822 /* ---------------- */
823 static char *myfgets( char *buf, int size, FILE *fp)
829 while ((EOF != ( c = getc( fp )) ) && ( size > 1 )) {
830 if ( c == '\n' || c == '\r' ) {
831 if (p != buf && *(p -1) == '\\') {
853 /* check access list. this function wants something of the following
855 * @group,name,name2,@group2,name3
857 * a NULL argument allows everybody to have access.
858 * we return three things:
860 * 0: list exists, but name isn't in it
864 #ifndef NO_REAL_USER_NAME
865 /* authentication is case insensitive
866 * FIXME should we do the same with group name?
868 #define access_strcmp strcasecmp
871 #define access_strcmp strcmp
875 static int accessvol(const char *args, const char *name)
877 char buf[MAXPATHLEN + 1], *p;
883 strlcpy(buf, args, sizeof(buf));
884 if ((p = strtok(buf, ",")) == NULL) /* nothing, return okay */
888 if (*p == '@') { /* it's a group */
889 if ((gr = getgrnam(p + 1)) && gmem(gr->gr_gid))
891 } else if (access_strcmp(p, name) == 0) /* it's a user name */
893 p = strtok(NULL, ",");
899 static int hostaccessvol(int type, const char *volname, const char *args, const AFPObj *obj)
902 char buf[MAXPATHLEN + 1], *p, *b;
903 DSI *dsi = obj->handle;
904 struct sockaddr_storage client;
909 strlcpy(buf, args, sizeof(buf));
910 if ((p = strtok_r(buf, ",", &b)) == NULL) /* nothing, return okay */
913 if (obj->proto != AFPPROTO_DSI)
918 char *ipaddr, *mask_char;
919 struct addrinfo hints, *ai;
921 ipaddr = strtok(p, "/");
922 mask_char = strtok(NULL,"/");
924 /* Get address from string with getaddrinfo */
925 memset(&hints, 0, sizeof hints);
926 hints.ai_family = AF_UNSPEC;
927 hints.ai_socktype = SOCK_STREAM;
928 if ((ret = getaddrinfo(ipaddr, NULL, &hints, &ai)) != 0) {
929 LOG(log_error, logtype_afpd, "hostaccessvol: getaddrinfo: %s\n", gai_strerror(ret));
934 if (mask_char != NULL)
935 mask_int = atoi(mask_char); /* apply_ip_mask does range checking on it */
937 if (ai->ai_family == AF_INET) /* IPv4 */
943 /* Apply mask to addresses */
944 client = dsi->client;
945 apply_ip_mask((struct sockaddr *)&client, mask_int);
946 apply_ip_mask(ai->ai_addr, mask_int);
948 if (compare_ip((struct sockaddr *)&client, ai->ai_addr) == 0) {
949 if (type == VOLOPT_DENIED_HOSTS)
950 LOG(log_info, logtype_afpd, "AFP access denied for client IP '%s' to volume '%s' by denied list",
951 getip_string((struct sockaddr *)&client), volname);
958 p = strtok_r(NULL, ",", &b);
961 if (type == VOLOPT_ALLOWED_HOSTS)
962 LOG(log_info, logtype_afpd, "AFP access denied for client IP '%s' to volume '%s', not in allowed list",
963 getip_string((struct sockaddr *)&dsi->client), volname);
967 static void setextmap(char *ext, char *type, char *creator, int user)
972 if (Extmap == NULL) {
973 if (( Extmap = calloc(1, sizeof( struct extmap ))) == NULL ) {
974 LOG(log_error, logtype_afpd, "setextmap: calloc: %s", strerror(errno) );
979 for ( em = Extmap, cnt = 0; em->em_ext; em++, cnt++) {
980 if ( (strdiacasecmp( em->em_ext, ext )) == 0 ) {
985 if ( em->em_ext == NULL ) {
986 if (!(Extmap = realloc( Extmap, sizeof( struct extmap ) * (cnt +2))) ) {
987 LOG(log_error, logtype_afpd, "setextmap: realloc: %s", strerror(errno) );
990 (Extmap +cnt +1)->em_ext = NULL;
992 } else if ( !user ) {
998 if (!(em->em_ext = strdup( ext))) {
999 LOG(log_error, logtype_afpd, "setextmap: strdup: %s", strerror(errno) );
1003 if ( *type == '\0' ) {
1004 memcpy(em->em_type, "\0\0\0\0", sizeof( em->em_type ));
1006 memcpy(em->em_type, type, sizeof( em->em_type ));
1008 if ( *creator == '\0' ) {
1009 memcpy(em->em_creator, "\0\0\0\0", sizeof( em->em_creator ));
1011 memcpy(em->em_creator, creator, sizeof( em->em_creator ));
1015 /* -------------------------- */
1016 static int extmap_cmp(const void *map1, const void *map2)
1018 const struct extmap *em1 = map1;
1019 const struct extmap *em2 = map2;
1020 return strdiacasecmp(em1->em_ext, em2->em_ext);
1023 static void sortextmap( void)
1028 if ((em = Extmap) == NULL) {
1031 while (em->em_ext) {
1036 qsort(Extmap, Extmap_cnt, sizeof(struct extmap), extmap_cmp);
1037 if (*Extmap->em_ext == 0) {
1038 /* the first line is really "." the default entry,
1039 * we remove the leading '.' in setextmap
1046 /* ----------------------
1048 static void free_extmap( void)
1053 for ( em = Extmap; em->em_ext; em++) {
1063 /* ----------------------
1065 static int volfile_changed(struct afp_volume_name *p)
1071 name = p->full_name;
1075 if (!stat( name, &st) && st.st_mtime > p->mtime) {
1076 p->mtime = st.st_mtime;
1082 /* ----------------------
1083 * Read a volume configuration file and add the volumes contained within to
1084 * the global volume list. If p2 is non-NULL, the file that is opened is
1087 * Lines that begin with # and blank lines are ignored.
1088 * Volume lines are of the form:
1089 * <unix path> [<volume name>] [allow:<user>,<@group>,...] \
1090 * [codepage:<file>] [casefold:<num>]
1091 * <extension> TYPE [CREATOR]
1093 static int readvolfile(AFPObj *obj, struct afp_volume_name *p1, char *p2, int user, struct passwd *pwent)
1096 char path[MAXPATHLEN + 1];
1097 char tmp[MAXPATHLEN + 1];
1098 char volname[AFPVOL_U8MNAMELEN + 1];
1100 char type[5], creator[5];
1103 struct vol_option save_options[VOLOPT_NUM];
1104 struct vol_option options[VOLOPT_NUM];
1112 strcpy( path, p1->name );
1114 strcat( path, "/" );
1116 if (p1->full_name) {
1117 free(p1->full_name);
1119 p1->full_name = strdup(path);
1122 if (NULL == ( fp = fopen( path, "r" )) ) {
1126 if (fd != -1 && !fstat( fd, &st) ) {
1127 p1->mtime = st.st_mtime;
1130 memset(save_options, 0, sizeof(save_options));
1132 /* Enable some default options for all volumes */
1133 save_options[VOLOPT_FLAGS].i_value |= AFPVOL_CACHE;
1134 save_options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_AUTO;
1136 while ( myfgets( buf, sizeof( buf ), fp ) != NULL ) {
1137 initline( strlen( buf ), buf );
1138 parseline( sizeof( path ) - 1, path );
1145 /* change the default options for this file */
1146 if (strncmp(path, VOLOPT_DEFAULT, VOLOPT_DEFAULT_LEN) == 0) {
1148 for (i = 0; i < VOLOPT_NUM; i++) {
1149 if (parseline( sizeof( path ) - VOLOPT_DEFAULT_LEN - 1,
1150 path + VOLOPT_DEFAULT_LEN) < 0)
1152 volset(save_options, NULL, tmp, sizeof(tmp) - 1,
1153 path + VOLOPT_DEFAULT_LEN);
1159 if (( p = strchr( path, '/' )) != NULL ) {
1167 if ( u == NULL || *u == '\0' || ( pw = getpwnam( u )) == NULL ) {
1170 strcpy( tmp, pw->pw_dir );
1171 if ( p != NULL && *p != '\0' ) {
1175 /* Tag a user's home directory with their umask. Note, this will
1176 * be overwritten if the user actually specifies a umask: option
1177 * for a '~' volume. */
1178 save_options[VOLOPT_UMASK].i_value = obj->options.save_mask;
1182 /* send path through variable substitution */
1183 if (*path != '~') /* need to copy path to tmp */
1186 pwent = getpwnam(obj->username);
1187 volxlate(obj, path, sizeof(path) - 1, tmp, pwent, NULL, NULL);
1189 /* this is sort of braindead. basically, i want to be
1190 * able to specify things in any order, but i don't want to
1191 * re-write everything.
1193 * currently we have options:
1201 * options:prodos,crlf,noadouble,ro...
1206 * namemask:x,y,!z (not implemented yet)
1208 memcpy(options, save_options, sizeof(options));
1211 /* read in up to VOLOP_NUM possible options */
1212 for (i = 0; i < VOLOPT_NUM; i++) {
1213 if (parseline( sizeof( tmp ) - 1, tmp ) < 0)
1216 volset(options, save_options, volname, sizeof(volname) - 1, tmp);
1219 /* check allow/deny lists:
1220 allow -> either no list (-1), or in list (1)
1221 deny -> either no list (-1), or not in list (0) */
1222 if (accessvol(options[VOLOPT_ALLOW].c_value, obj->username) &&
1223 (accessvol(options[VOLOPT_DENY].c_value, obj->username) < 1) &&
1224 hostaccessvol(VOLOPT_ALLOWED_HOSTS, volname, options[VOLOPT_ALLOWED_HOSTS].c_value, obj) &&
1225 (hostaccessvol(VOLOPT_DENIED_HOSTS, volname, options[VOLOPT_DENIED_HOSTS].c_value, obj) < 1)) {
1227 /* handle read-only behaviour. semantics:
1228 * 1) neither the rolist nor the rwlist exist -> rw
1229 * 2) rolist exists -> ro if user is in it.
1230 * 3) rwlist exists -> ro unless user is in it. */
1231 if (((options[VOLOPT_FLAGS].i_value & AFPVOL_RO) == 0) &&
1232 ((accessvol(options[VOLOPT_ROLIST].c_value,
1233 obj->username) == 1) ||
1234 !accessvol(options[VOLOPT_RWLIST].c_value,
1236 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
1238 /* do variable substitution for volname */
1239 volxlate(obj, tmp, sizeof(tmp) - 1, volname, pwent, path, NULL);
1240 creatvol(obj, pwent, path, tmp, options, p2 != NULL);
1242 volfree(options, save_options);
1246 parseline( sizeof( type ) - 1, type );
1247 parseline( sizeof( creator ) - 1, creator );
1248 setextmap( path, type, creator, user);
1255 volfree(save_options, NULL);
1257 if ( fclose( fp ) != 0 ) {
1258 LOG(log_error, logtype_afpd, "readvolfile: fclose: %s", strerror(errno) );
1264 /* ------------------------------- */
1265 static void volume_free(struct vol *vol)
1267 free(vol->v_localname);
1268 vol->v_localname = NULL;
1269 free(vol->v_u8mname);
1270 vol->v_u8mname = NULL;
1271 free(vol->v_macname);
1272 vol->v_macname = NULL;
1274 free(vol->v_password);
1276 free(vol->v_volcodepage);
1277 free(vol->v_maccodepage);
1278 free(vol->v_cnidscheme);
1279 free(vol->v_dbpath);
1282 free(vol->v_forceuid);
1283 free(vol->v_forcegid);
1284 #endif /* FORCE_UIDGID */
1287 /* ------------------------------- */
1288 static void free_volumes(void )
1291 struct vol *nvol, *ovol;
1293 for ( vol = Volumes; vol; vol = vol->v_next ) {
1294 if (( vol->v_flags & AFPVOL_OPEN ) ) {
1301 for ( vol = Volumes, ovol = NULL; vol; vol = nvol) {
1304 if (vol->v_localname == NULL) {
1305 if (Volumes == vol) {
1310 ovol->v_next = nvol;
1320 /* ------------------------------- */
1321 static void volume_unlink(struct vol *volume)
1323 struct vol *vol, *ovol, *nvol;
1325 if (volume == Volumes) {
1326 Volumes = Volumes->v_next;
1329 for ( vol = Volumes->v_next, ovol = Volumes; vol; vol = nvol) {
1332 if (vol == volume) {
1333 ovol->v_next = nvol;
1342 static int getvolspace(struct vol *vol,
1343 u_int32_t *bfree, u_int32_t *btotal,
1344 VolSpace *xbfree, VolSpace *xbtotal, u_int32_t *bsize)
1348 #ifndef NO_QUOTA_SUPPORT
1349 VolSpace qfree, qtotal;
1352 spaceflag = AFPVOL_GVSMASK & vol->v_flags;
1353 /* report up to 2GB if afp version is < 2.2 (4GB if not) */
1354 maxsize = (vol->v_flags & AFPVOL_A2VOL) ? 0x01fffe00 :
1355 (((afp_version < 22) || (vol->v_flags & AFPVOL_LIMITSIZE))
1356 ? 0x7fffffffL : 0xffffffffL);
1359 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_AFSGVS ) {
1360 if ( afs_getvolspace( vol, xbfree, xbtotal, bsize ) == AFP_OK ) {
1361 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_AFSGVS;
1362 goto getvolspace_done;
1367 if (( rc = ustatfs_getvolspace( vol, xbfree, xbtotal,
1368 bsize)) != AFP_OK ) {
1372 #define min(a,b) ((a)<(b)?(a):(b))
1373 #ifndef NO_QUOTA_SUPPORT
1374 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_UQUOTA ) {
1375 if ( uquota_getvolspace( vol, &qfree, &qtotal, *bsize ) == AFP_OK ) {
1376 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_UQUOTA;
1377 *xbfree = min(*xbfree, qfree);
1378 *xbtotal = min( *xbtotal, qtotal);
1379 goto getvolspace_done;
1383 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_USTATFS;
1386 *bfree = min( *xbfree, maxsize);
1387 *btotal = min( *xbtotal, maxsize);
1391 /* -----------------------
1392 * set volume creation date
1393 * avoid duplicate, well at least it tries
1395 static void vol_setdate(u_int16_t id, struct adouble *adp, time_t date)
1398 struct vol *vol = Volumes;
1400 for ( volume = Volumes; volume; volume = volume->v_next ) {
1401 if (volume->v_vid == id) {
1404 else if ((time_t)(AD_DATE_FROM_UNIX(date)) == volume->v_ctime) {
1406 volume = Volumes; /* restart */
1409 vol->v_ctime = AD_DATE_FROM_UNIX(date);
1410 ad_setdate(adp, AD_DATE_CREATE | AD_DATE_UNIX, date);
1413 /* ----------------------- */
1414 static int getvolparams( u_int16_t bitmap, struct vol *vol, struct stat *st, char *buf, size_t *buflen)
1417 int bit = 0, isad = 1;
1420 u_int32_t bfree, btotal, bsize;
1421 VolSpace xbfree, xbtotal; /* extended bytes */
1422 char *data, *nameoff = NULL;
1425 LOG(log_debug, logtype_afpd, "getvolparams: Volume '%s'", vol->v_localname);
1427 /* courtesy of jallison@whistle.com:
1428 * For MacOS8.x support we need to create the
1429 * .Parent file here if it doesn't exist. */
1431 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1432 if ( ad_open_metadata( vol->v_path, ADFLAGS_DIR, O_CREAT, &ad) < 0 ) {
1434 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1436 } else if (ad_get_MD_flags( &ad ) & O_CREAT) {
1437 slash = strrchr( vol->v_path, '/' );
1441 slash = vol->v_path;
1442 if (ad_getentryoff(&ad, ADEID_NAME)) {
1443 ad_setentrylen( &ad, ADEID_NAME, strlen( slash ));
1444 memcpy(ad_entry( &ad, ADEID_NAME ), slash,
1445 ad_getentrylen( &ad, ADEID_NAME ));
1447 vol_setdate(vol->v_vid, &ad, st->st_mtime);
1451 if (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0)
1452 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1454 vol->v_ctime = aint;
1457 if (( bitmap & ( (1<<VOLPBIT_BFREE)|(1<<VOLPBIT_BTOTAL) |
1458 (1<<VOLPBIT_XBFREE)|(1<<VOLPBIT_XBTOTAL) |
1459 (1<<VOLPBIT_BSIZE)) ) != 0 ) {
1460 if ( getvolspace( vol, &bfree, &btotal, &xbfree, &xbtotal,
1461 &bsize) != AFP_OK ) {
1463 ad_close( &ad, ADFLAGS_HF );
1465 return( AFPERR_PARAM );
1470 while ( bitmap != 0 ) {
1471 while (( bitmap & 1 ) == 0 ) {
1479 /* check for read-only.
1480 * NOTE: we don't actually set the read-only flag unless
1481 * it's passed in that way as it's possible to mount
1482 * a read-write filesystem under a read-only one. */
1483 if ((vol->v_flags & AFPVOL_RO) ||
1484 ((utime(vol->v_path, NULL) < 0) && (errno == EROFS))) {
1485 ashort |= VOLPBIT_ATTR_RO;
1487 /* prior 2.1 only VOLPBIT_ATTR_RO is defined */
1488 if (afp_version > 20) {
1489 if (0 == (vol->v_flags & AFPVOL_NOFILEID) && vol->v_cdb != NULL &&
1490 (vol->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1491 ashort |= VOLPBIT_ATTR_FILEID;
1493 ashort |= VOLPBIT_ATTR_CATSEARCH;
1495 if (afp_version >= 30) {
1496 ashort |= VOLPBIT_ATTR_UTF8;
1497 if (vol->v_flags & AFPVOL_UNIX_PRIV)
1498 ashort |= VOLPBIT_ATTR_UNIXPRIV;
1499 if (vol->v_flags & AFPVOL_TM)
1500 ashort |= VOLPBIT_ATTR_TM;
1502 if (afp_version >= 32) {
1504 ashort |= VOLPBIT_ATTR_EXT_ATTRS;
1505 if (vol->v_flags & AFPVOL_ACLS)
1506 ashort |= VOLPBIT_ATTR_ACLS;
1510 ashort = htons(ashort);
1511 memcpy(data, &ashort, sizeof( ashort ));
1512 data += sizeof( ashort );
1516 ashort = htons( AFPVOLSIG_DEFAULT );
1517 memcpy(data, &ashort, sizeof( ashort ));
1518 data += sizeof( ashort );
1521 case VOLPBIT_CDATE :
1522 aint = vol->v_ctime;
1523 memcpy(data, &aint, sizeof( aint ));
1524 data += sizeof( aint );
1527 case VOLPBIT_MDATE :
1528 if ( st->st_mtime > vol->v_mtime ) {
1529 vol->v_mtime = st->st_mtime;
1531 aint = AD_DATE_FROM_UNIX(vol->v_mtime);
1532 memcpy(data, &aint, sizeof( aint ));
1533 data += sizeof( aint );
1536 case VOLPBIT_BDATE :
1537 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1538 aint = AD_DATE_START;
1539 memcpy(data, &aint, sizeof( aint ));
1540 data += sizeof( aint );
1544 memcpy(data, &vol->v_vid, sizeof( vol->v_vid ));
1545 data += sizeof( vol->v_vid );
1548 case VOLPBIT_BFREE :
1549 bfree = htonl( bfree );
1550 memcpy(data, &bfree, sizeof( bfree ));
1551 data += sizeof( bfree );
1554 case VOLPBIT_BTOTAL :
1555 btotal = htonl( btotal );
1556 memcpy(data, &btotal, sizeof( btotal ));
1557 data += sizeof( btotal );
1560 #ifndef NO_LARGE_VOL_SUPPORT
1561 case VOLPBIT_XBFREE :
1562 xbfree = hton64( xbfree );
1563 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1564 bcopy(&xbfree, data, sizeof(xbfree));
1565 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1566 memcpy(data, &xbfree, sizeof( xbfree ));
1567 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1568 data += sizeof( xbfree );
1571 case VOLPBIT_XBTOTAL :
1572 xbtotal = hton64( xbtotal );
1573 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1574 bcopy(&xbtotal, data, sizeof(xbtotal));
1575 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1576 memcpy(data, &xbtotal, sizeof( xbtotal ));
1577 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1578 data += sizeof( xbfree );
1580 #endif /* ! NO_LARGE_VOL_SUPPORT */
1584 data += sizeof( u_int16_t );
1587 case VOLPBIT_BSIZE: /* block size */
1588 bsize = htonl(bsize);
1589 memcpy(data, &bsize, sizeof(bsize));
1590 data += sizeof(bsize);
1595 ad_close( &ad, ADFLAGS_HF );
1597 return( AFPERR_BITMAP );
1603 ashort = htons( data - buf );
1604 memcpy(nameoff, &ashort, sizeof( ashort ));
1605 /* name is always in mac charset */
1606 aint = ucs2_to_charset( vol->v_maccharset, vol->v_macname, data+1, AFPVOL_MACNAMELEN + 1);
1616 ad_close_metadata( &ad);
1618 *buflen = data - buf;
1622 /* ------------------------- */
1623 static int stat_vol(u_int16_t bitmap, struct vol *vol, char *rbuf, size_t *rbuflen)
1629 if ( stat( vol->v_path, &st ) < 0 ) {
1631 return( AFPERR_PARAM );
1633 /* save the volume device number */
1634 vol->v_dev = st.st_dev;
1636 buflen = *rbuflen - sizeof( bitmap );
1637 if (( ret = getvolparams( bitmap, vol, &st,
1638 rbuf + sizeof( bitmap ), &buflen )) != AFP_OK ) {
1642 *rbuflen = buflen + sizeof( bitmap );
1643 bitmap = htons( bitmap );
1644 memcpy(rbuf, &bitmap, sizeof( bitmap ));
1649 /* ------------------------------- */
1650 void load_volumes(AFPObj *obj)
1652 struct passwd *pwent;
1657 /* check files date */
1658 if (obj->options.defaultvol.loaded) {
1659 changed = volfile_changed(&obj->options.defaultvol);
1661 if (obj->options.systemvol.loaded) {
1662 changed |= volfile_changed(&obj->options.systemvol);
1664 if (obj->options.uservol.loaded) {
1665 changed |= volfile_changed(&obj->options.uservol);
1674 pwent = getpwnam(obj->username);
1675 if ( (obj->options.flags & OPTION_USERVOLFIRST) == 0 ) {
1676 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent);
1679 if ((*obj->username == '\0') || (obj->options.flags & OPTION_NOUSERVOL)) {
1680 readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent);
1683 * Read user's AppleVolumes or .AppleVolumes file
1684 * If neither are readable, read the default volumes file. if
1685 * that doesn't work, create a user share.
1687 if (obj->options.uservol.name) {
1688 free(obj->options.uservol.name);
1690 obj->options.uservol.name = strdup(pwent->pw_dir);
1691 if ( readvolfile(obj, &obj->options.uservol, "AppleVolumes", 1, pwent) < 0 &&
1692 readvolfile(obj, &obj->options.uservol, ".AppleVolumes", 1, pwent) < 0 &&
1693 readvolfile(obj, &obj->options.uservol, "applevolumes", 1, pwent) < 0 &&
1694 readvolfile(obj, &obj->options.uservol, ".applevolumes", 1, pwent) < 0 &&
1695 obj->options.defaultvol.name != NULL ) {
1696 if (readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent) < 0)
1697 creatvol(obj, pwent, pwent->pw_dir, NULL, NULL, 1);
1700 if ( obj->options.flags & OPTION_USERVOLFIRST ) {
1701 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent );
1704 if ( obj->options.closevol ) {
1707 for ( vol = Volumes; vol; vol = vol->v_next ) {
1708 if (vol->v_deleted && !vol->v_new ) {
1717 /* ------------------------------- */
1718 int afp_getsrvrparms(AFPObj *obj, char *ibuf _U_, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
1731 for ( vcnt = 0, volume = Volumes; volume; volume = volume->v_next ) {
1732 if (!(volume->v_flags & AFPVOL_NOSTAT)) {
1735 if ( stat( volume->v_path, &st ) < 0 ) {
1736 LOG(log_info, logtype_afpd, "afp_getsrvrparms(%s): stat: %s",
1737 volume->v_path, strerror(errno) );
1738 continue; /* can't access directory */
1740 if (!S_ISDIR(st.st_mode)) {
1741 continue; /* not a dir */
1743 accessmode(volume->v_path, &ma, NULL, &st);
1744 if ((ma.ma_user & (AR_UREAD | AR_USEARCH)) != (AR_UREAD | AR_USEARCH)) {
1745 continue; /* no r-x access */
1748 if (volume->v_hide) {
1749 continue; /* config file changed but the volume was mounted */
1752 if (utf8_encoding()) {
1753 len = ucs2_to_charset_allocate(CH_UTF8_MAC, &namebuf, volume->v_u8mname);
1755 len = ucs2_to_charset_allocate(obj->options.maccharset, &namebuf, volume->v_macname);
1758 if (len == (size_t)-1)
1761 /* set password bit if there's a volume password */
1762 *data = (volume->v_password) ? AFPSRVR_PASSWD : 0;
1764 /* Apple 2 clients running ProDOS-8 expect one volume to have
1765 bit 0 of this byte set. They will not recognize anything
1766 on the server unless this is the case. I have not
1767 completely worked this out, but it's related to booting
1768 from the server. Support for that function is a ways
1769 off.. <shirsch@ibm.net> */
1770 *data |= (volume->v_flags & AFPVOL_A2VOL) ? AFPSRVR_CONFIGINFO : 0;
1771 *data++ |= 0; /* UNIX PRIVS BIT ..., OSX doesn't seem to use it, so we don't either */
1773 memcpy(data, namebuf, len );
1779 *rbuflen = data - rbuf;
1781 if ( gettimeofday( &tv, NULL ) < 0 ) {
1782 LOG(log_error, logtype_afpd, "afp_getsrvrparms(%s): gettimeofday: %s", volume->v_path, strerror(errno) );
1784 return AFPERR_PARAM;
1786 tv.tv_sec = AD_DATE_FROM_UNIX(tv.tv_sec);
1787 memcpy(data, &tv.tv_sec, sizeof( u_int32_t));
1788 data += sizeof( u_int32_t);
1793 /* ------------------------- */
1794 static int volume_codepage(AFPObj *obj, struct vol *volume)
1796 struct charset_functions *charset;
1799 if (!volume->v_volcodepage)
1800 volume->v_volcodepage = strdup("UTF8");
1802 if ( (charset_t) -1 == ( volume->v_volcharset = add_charset(volume->v_volcodepage)) ) {
1803 LOG (log_error, logtype_afpd, "Setting codepage %s as volume codepage failed", volume->v_volcodepage);
1807 if ( NULL == (charset = find_charset_functions(volume->v_volcodepage)) || charset->flags & CHARSET_ICONV ) {
1808 LOG (log_warning, logtype_afpd, "WARNING: volume encoding %s is *not* supported by netatalk, expect problems !!!!", volume->v_volcodepage);
1811 if (!volume->v_maccodepage)
1812 volume->v_maccodepage = strdup(obj->options.maccodepage);
1814 if ( (charset_t) -1 == ( volume->v_maccharset = add_charset(volume->v_maccodepage)) ) {
1815 LOG (log_error, logtype_afpd, "Setting codepage %s as mac codepage failed", volume->v_maccodepage);
1819 if ( NULL == ( charset = find_charset_functions(volume->v_maccodepage)) || ! (charset->flags & CHARSET_CLIENT) ) {
1820 LOG (log_error, logtype_afpd, "Fatal error: mac charset %s not supported", volume->v_maccodepage);
1823 volume->v_kTextEncoding = htonl(charset->kTextEncoding);
1827 /* ------------------------- */
1828 static int volume_openDB(struct vol *volume)
1832 if ((volume->v_flags & AFPVOL_NODEV)) {
1833 flags |= CNID_FLAG_NODEV;
1836 if (volume->v_cnidscheme == NULL) {
1837 volume->v_cnidscheme = strdup(DEFAULT_CNID_SCHEME);
1838 LOG(log_info, logtype_afpd, "Volume %s use CNID scheme %s.", volume->v_path, volume->v_cnidscheme);
1841 LOG(log_info, logtype_afpd, "%s:%s", volume->v_cnidserver, volume->v_cnidport);
1843 volume->v_cdb = cnid_open(volume->v_dbpath ? volume->v_dbpath : volume->v_path,
1845 volume->v_cnidscheme,
1847 volume->v_cnidserver ? volume->v_cnidserver : Cnid_srv,
1848 volume->v_cnidport ? volume->v_cnidport : Cnid_port);
1850 if (!volume->v_cdb) {
1851 flags |= CNID_FLAG_MEMORY;
1852 LOG(log_error, logtype_afpd, "Reopen volume %s using in memory temporary CNID DB.", volume->v_path);
1853 volume->v_cdb = cnid_open (volume->v_path, volume->v_umask, "tdb", flags, NULL, NULL);
1855 /* kill ourself with SIGUSR2 aka msg pending */
1856 if (volume->v_cdb) {
1857 setmessage("Something wrong with the volume's DB ... FIXME with a better msg");
1858 kill(getpid(), SIGUSR2);
1859 /* XXX desactivate cachecnid ? */
1864 return (!volume->v_cdb)?-1:0;
1868 Check if the underlying filesystem supports EAs for ea:sys volumes.
1869 If not, switch to ea:ad.
1870 As we can't check (requires write access) on ro-volumes, we switch ea:auto
1871 volumes that are options:ro to ea:none.
1873 static void check_ea_sys_support(struct vol *vol)
1875 uid_t process_uid = 0;
1876 char eaname[] = {"org.netatalk.supports-eas.XXXXXX"};
1877 const char *eacontent = "yes";
1879 if (vol->v_vfs_ea == AFPVOL_EA_AUTO) {
1881 if ((vol->v_flags & AFPVOL_RO) == AFPVOL_RO) {
1882 LOG(log_info, logtype_logger, "read-only volume '%s', can't test for EA support, disabling EAs", vol->v_localname);
1883 vol->v_vfs_ea = AFPVOL_EA_NONE;
1889 process_uid = geteuid();
1891 if (seteuid(0) == -1) {
1892 LOG(log_error, logtype_logger, "check_ea_sys_support: can't seteuid(0): %s", strerror(errno));
1896 if ((sys_setxattr(vol->v_path, eaname, eacontent, 4, 0)) == 0) {
1897 sys_removexattr(vol->v_path, eaname);
1898 vol->v_vfs_ea = AFPVOL_EA_SYS;
1900 LOG(log_warning, logtype_afpd, "volume \"%s\" does not support Extended Attributes, using ea:ad instead",
1902 vol->v_vfs_ea = AFPVOL_EA_AD;
1906 if (seteuid(process_uid) == -1) {
1907 LOG(log_error, logtype_logger, "can't seteuid back %s", strerror(errno));
1914 /* -------------------------
1915 * we are the user here
1917 int afp_openvol(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
1928 char path[ MAXPATHLEN + 1];
1934 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1935 bitmap = ntohs( bitmap );
1936 ibuf += sizeof( bitmap );
1939 if (( bitmap & (1<<VOLPBIT_VID)) == 0 ) {
1940 return AFPERR_BITMAP;
1943 len = (unsigned char)*ibuf++;
1944 volname = obj->oldtmp;
1946 if ((volname_tmp = strchr(volname,'+')) != NULL)
1947 volname = volname_tmp+1;
1949 if (utf8_encoding()) {
1950 namelen = convert_string(CH_UTF8_MAC, CH_UCS2, ibuf, len, volname, sizeof(obj->oldtmp));
1952 namelen = convert_string(obj->options.maccharset, CH_UCS2, ibuf, len, volname, sizeof(obj->oldtmp));
1955 if ( namelen <= 0) {
1956 return AFPERR_PARAM;
1960 if ((len + 1) & 1) /* pad to an even boundary */
1965 for ( volume = Volumes; volume; volume = volume->v_next ) {
1966 if ( strcasecmp_w( (ucs2_t*) volname, volume->v_name ) == 0 ) {
1971 if ( volume == NULL ) {
1972 return AFPERR_PARAM;
1975 /* check for a volume password */
1976 if (volume->v_password && strncmp(ibuf, volume->v_password, VOLPASSLEN)) {
1977 return AFPERR_ACCESS;
1980 if (( volume->v_flags & AFPVOL_OPEN ) ) {
1981 /* the volume is already open */
1983 set_uidgid ( volume );
1985 return stat_vol(bitmap, volume, rbuf, rbuflen);
1988 if (volume->v_root_preexec) {
1989 if ((ret = afprun(1, volume->v_root_preexec, NULL)) && volume->v_root_preexec_close) {
1990 LOG(log_error, logtype_afpd, "afp_openvol(%s): root preexec : %d", volume->v_path, ret );
1996 set_uidgid ( volume );
1999 if (volume->v_preexec) {
2000 if ((ret = afprun(0, volume->v_preexec, NULL)) && volume->v_preexec_close) {
2001 LOG(log_error, logtype_afpd, "afp_openvol(%s): preexec : %d", volume->v_path, ret );
2006 if ( stat( volume->v_path, &st ) < 0 ) {
2007 return AFPERR_PARAM;
2010 if ( chdir( volume->v_path ) < 0 ) {
2011 return AFPERR_PARAM;
2014 if ( NULL == getcwd(path, MAXPATHLEN)) {
2015 /* shouldn't be fatal but it will fail later */
2016 LOG(log_error, logtype_afpd, "afp_openvol(%s): volume pathlen too long", volume->v_path);
2020 /* Normalize volume path */
2021 #ifdef REALPATH_TAKES_NULL
2022 if ((volume->v_path = realpath(path, NULL)) == NULL)
2025 if ((volume->v_path = malloc(MAXPATHLEN+1)) == NULL)
2027 if (realpath(path, volume->v_path) == NULL) {
2028 free(volume->v_path);
2031 /* Safe some memory */
2033 if ((tmp = strdup(volume->v_path)) == NULL) {
2034 free(volume->v_path);
2037 free(volume->v_path);
2038 volume->v_path = tmp;
2041 if (volume_codepage(obj, volume) < 0) {
2046 /* initialize volume variables
2049 if (utf8_encoding()) {
2050 volume->max_filename = 255;
2053 volume->max_filename = MACFILELEN;
2056 volume->v_dir = volume->v_root = NULL;
2057 volume->v_hash = NULL;
2059 volume->v_flags |= AFPVOL_OPEN;
2060 volume->v_cdb = NULL;
2062 if (utf8_encoding()) {
2063 len = convert_string_allocate(CH_UCS2, CH_UTF8_MAC, volume->v_u8mname, namelen, &vol_mname);
2065 len = convert_string_allocate(CH_UCS2, obj->options.maccharset, volume->v_macname, namelen, &vol_mname);
2067 if ( !vol_mname || len <= 0) {
2072 if ((vol_uname = strrchr(path, '/')) == NULL)
2074 else if (*(vol_uname + 1) != '\0')
2077 if ((dir = dirnew(vol_mname, vol_uname) ) == NULL) {
2079 LOG(log_error, logtype_afpd, "afp_openvol(%s): malloc: %s", volume->v_path, strerror(errno) );
2085 dir->d_did = DIRDID_ROOT;
2086 dir->d_color = DIRTREE_COLOR_BLACK; /* root node is black */
2087 dir->d_m_name_ucs2 = strdup_w(volume->v_name);
2088 volume->v_dir = volume->v_root = dir;
2089 volume->v_curdir = NULL;
2090 volume->v_hash = dirhash();
2092 curdir = volume->v_dir;
2093 if (volume_openDB(volume) < 0) {
2094 LOG(log_error, logtype_afpd, "Fatal error: cannot open CNID or invalid CNID backend for %s: %s",
2095 volume->v_path, volume->v_cnidscheme);
2100 ret = stat_vol(bitmap, volume, rbuf, rbuflen);
2101 if (ret == AFP_OK) {
2103 if (!(volume->v_flags & AFPVOL_RO)) {
2104 handle_special_folders( volume );
2106 volume->v_cnidserver ? volume->v_cnidserver : Cnid_srv,
2107 volume->v_cnidport ? volume->v_cnidport : Cnid_port);
2111 * If you mount a volume twice, the second time the trash appears on
2112 * the desk-top. That's because the Mac remembers the DID for the
2113 * trash (even for volumes in different zones, on different servers).
2114 * Just so this works better, we prime the DID cache with the trash,
2115 * fixing the trash at DID 17.
2116 * FIXME (RL): should it be done inside a CNID backend ? (always returning Trash DID when asked) ?
2118 if ((volume->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
2120 /* FIXME find db time stamp */
2121 if (cnid_getstamp(volume->v_cdb, volume->v_stamp, sizeof(volume->v_stamp)) < 0) {
2122 LOG (log_error, logtype_afpd,
2123 "afp_openvol(%s): Fatal error: Unable to get stamp value from CNID backend",
2131 cname( volume, volume->v_dir, &p );
2137 if (volume->v_dir) {
2138 hash_free( volume->v_hash);
2139 dirfree( volume->v_dir );
2140 volume->v_dir = volume->v_root = NULL;
2143 volume->v_flags &= ~AFPVOL_OPEN;
2144 if (volume->v_cdb != NULL) {
2145 cnid_close(volume->v_cdb);
2146 volume->v_cdb = NULL;
2152 /* ------------------------- */
2153 static void closevol(struct vol *vol)
2158 hash_free( vol->v_hash);
2159 dirfree( vol->v_root );
2161 if (vol->v_cdb != NULL) {
2162 cnid_close(vol->v_cdb);
2166 if (vol->v_postexec) {
2167 afprun(0, vol->v_postexec, NULL);
2169 if (vol->v_root_postexec) {
2170 afprun(1, vol->v_root_postexec, NULL);
2174 /* ------------------------- */
2175 void close_all_vol(void)
2179 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
2180 if ( (ovol->v_flags & AFPVOL_OPEN) ) {
2181 ovol->v_flags &= ~AFPVOL_OPEN;
2187 /* ------------------------- */
2188 static void deletevol(struct vol *vol)
2192 vol->v_flags &= ~AFPVOL_OPEN;
2193 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
2194 if ( (ovol->v_flags & AFPVOL_OPEN) ) {
2198 if ( ovol != NULL ) {
2199 /* Even if chdir fails, we can't say afp_closevol fails. */
2200 if ( chdir( ovol->v_path ) == 0 ) {
2201 curdir = ovol->v_dir;
2206 if (vol->v_deleted) {
2207 showvol(vol->v_name);
2214 /* ------------------------- */
2215 int afp_closevol(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2222 memcpy(&vid, ibuf, sizeof( vid ));
2223 if (NULL == ( vol = getvolbyvid( vid )) ) {
2224 return( AFPERR_PARAM );
2232 /* ------------------------- */
2233 struct vol *getvolbyvid(const u_int16_t vid )
2237 for ( vol = Volumes; vol; vol = vol->v_next ) {
2238 if ( vid == vol->v_vid ) {
2242 if ( vol == NULL || ( vol->v_flags & AFPVOL_OPEN ) == 0 ) {
2248 #endif /* FORCE_UIDGID */
2253 /* ------------------------ */
2254 static int ext_cmp_key(const void *key, const void *obj)
2256 const char *p = key;
2257 const struct extmap *em = obj;
2258 return strdiacasecmp(p, em->em_ext);
2260 struct extmap *getextmap(const char *path)
2265 if (!Extmap_cnt || NULL == ( p = strrchr( path, '.' )) ) {
2266 return( Defextmap );
2270 return( Defextmap );
2272 em = bsearch(p, Extmap, Extmap_cnt, sizeof(struct extmap), ext_cmp_key);
2276 return( Defextmap );
2280 /* ------------------------- */
2281 struct extmap *getdefextmap(void)
2283 return( Defextmap );
2286 /* --------------------------
2287 poll if a volume is changed by other processes.
2289 0 no attention msg sent
2290 1 attention msg sent
2291 -1 error (socket closed)
2293 Note: if attention return -1 no packet has been
2294 sent because the buffer is full, we don't care
2295 either there's no reader or there's a lot of
2296 traffic and another pollvoltime will follow
2298 int pollvoltime(AFPObj *obj)
2305 if (!(afp_version > 21 && obj->options.server_notif))
2308 if ( gettimeofday( &tv, NULL ) < 0 )
2311 for ( vol = Volumes; vol; vol = vol->v_next ) {
2312 if ( (vol->v_flags & AFPVOL_OPEN) && vol->v_mtime + 30 < tv.tv_sec) {
2313 if ( !stat( vol->v_path, &st ) && vol->v_mtime != st.st_mtime ) {
2314 vol->v_mtime = st.st_mtime;
2315 if (!obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED))
2324 /* ------------------------- */
2325 void setvoltime(AFPObj *obj, struct vol *vol)
2329 /* just looking at vol->v_mtime is broken seriously since updates
2330 * from other users afpd processes never are seen.
2331 * This is not the most elegant solution (a shared memory between
2332 * the afpd processes would come closer)
2335 if ( gettimeofday( &tv, NULL ) < 0 ) {
2336 LOG(log_error, logtype_afpd, "setvoltime(%s): gettimeofday: %s", vol->v_path, strerror(errno) );
2339 if( utime( vol->v_path, NULL ) < 0 ) {
2340 /* write of time failed ... probably a read only filesys,
2341 * where no other users can interfere, so there's no issue here
2345 /* a little granularity */
2346 if (vol->v_mtime < tv.tv_sec) {
2347 vol->v_mtime = tv.tv_sec;
2348 /* or finder doesn't update free space
2349 * AFP 3.2 and above clients seem to be ok without so many notification
2351 if (afp_version < 32 && obj->options.server_notif) {
2352 obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED);
2357 /* ------------------------- */
2358 int afp_getvolparams(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_,char *rbuf, size_t *rbuflen)
2361 u_int16_t vid, bitmap;
2364 memcpy(&vid, ibuf, sizeof( vid ));
2365 ibuf += sizeof( vid );
2366 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2367 bitmap = ntohs( bitmap );
2369 if (NULL == ( vol = getvolbyvid( vid )) ) {
2371 return( AFPERR_PARAM );
2374 return stat_vol(bitmap, vol, rbuf, rbuflen);
2377 /* ------------------------- */
2378 int afp_setvolparams(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2382 u_int16_t vid, bitmap;
2388 memcpy(&vid, ibuf, sizeof( vid ));
2389 ibuf += sizeof( vid );
2390 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2391 bitmap = ntohs( bitmap );
2392 ibuf += sizeof(bitmap);
2394 if (( vol = getvolbyvid( vid )) == NULL ) {
2395 return( AFPERR_PARAM );
2398 if ((vol->v_flags & AFPVOL_RO))
2399 return AFPERR_VLOCK;
2401 /* we can only set the backup date. */
2402 if (bitmap != (1 << VOLPBIT_BDATE))
2403 return AFPERR_BITMAP;
2405 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2406 if ( ad_open( vol->v_path, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR,
2409 return AFPERR_VLOCK;
2411 return AFPERR_ACCESS;
2414 memcpy(&aint, ibuf, sizeof(aint));
2415 ad_setdate(&ad, AD_DATE_BACKUP, aint);
2417 ad_close(&ad, ADFLAGS_HF);
2421 /* ------------------------- */
2422 int wincheck(const struct vol *vol, const char *path)
2426 if (!(vol->v_flags & AFPVOL_MSWINDOWS))
2429 /* empty paths are not allowed */
2430 if ((len = strlen(path)) == 0)
2433 /* leading or trailing whitespaces are not allowed, carriage returns
2434 * and probably other whitespace is okay, tabs are not allowed
2436 if ((path[0] == ' ') || (path[len-1] == ' '))
2439 /* certain characters are not allowed */
2440 if (strpbrk(path, MSWINDOWS_BADCHARS))
2443 /* everything else is okay */
2449 * precreate a folder
2450 * this is only intended for folders in the volume root
2451 * It will *not* work if the folder name contains extended characters
2453 static int create_special_folder (const struct vol *vol, const struct _special_folder *folder)
2462 p = (char *) malloc ( strlen(vol->v_path)+strlen(folder->name)+2);
2464 LOG(log_error, logtype_afpd,"malloc failed");
2468 q=strdup(folder->name);
2470 LOG(log_error, logtype_afpd,"malloc failed");
2474 strcpy(p, vol->v_path);
2479 if ((vol->v_casefold & AFPVOL_MTOUUPPER))
2481 else if ((vol->v_casefold & AFPVOL_MTOULOWER))
2487 if ( (ret = stat( p, &st )) < 0 ) {
2488 if (folder->precreate) {
2489 if (ad_mkdir(p, folder->mode)) {
2490 LOG(log_debug, logtype_afpd,"Creating '%s' failed in %s: %s", p, vol->v_path, strerror(errno));
2499 if ( !ret && folder->hide) {
2501 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2502 if (ad_open_metadata( p, ADFLAGS_DIR, O_CREAT, &ad) < 0) {
2508 ad_setname(&ad, folder->name);
2510 ad_getattr(&ad, &attr);
2511 attr |= htons( ntohs( attr ) | ATTRBIT_INVISIBLE );
2512 ad_setattr(&ad, attr);
2514 /* do the same with the finder info */
2515 if (ad_entry(&ad, ADEID_FINDERI)) {
2516 memcpy(&attr, ad_entry(&ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF, sizeof(attr));
2517 attr |= htons(FINDERINFO_INVISIBLE);
2518 memcpy(ad_entry(&ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF,&attr, sizeof(attr));
2522 ad_close_metadata( &ad);
2529 static void handle_special_folders (const struct vol * vol)
2531 const _special_folder *p = &special_folders[0];
2533 if ((vol->v_flags & AFPVOL_RO))
2536 for (; p->name != NULL; p++) {
2537 create_special_folder (vol, p);