2 * $Id: volume.c,v 1.125 2010-04-08 05:51:16 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 /* Usable slots: 8 and 9 */
96 #define VOLOPT_VETO 10 /* list of veto filespec */
97 #define VOLOPT_PREEXEC 11 /* preexec command */
98 #define VOLOPT_ROOTPREEXEC 12 /* root preexec command */
99 #define VOLOPT_POSTEXEC 13 /* postexec command */
100 #define VOLOPT_ROOTPOSTEXEC 14 /* root postexec command */
101 #define VOLOPT_ENCODING 15 /* mac encoding (pre OSX)*/
102 #define VOLOPT_MACCHARSET 16
103 #define VOLOPT_CNIDSCHEME 17
104 #define VOLOPT_ADOUBLE 18 /* adouble version */
110 #define VOLOPT_FORCEUID 19 /* force uid for username x */
111 #define VOLOPT_FORCEGID 20 /* force gid for group x */
112 #endif /* FORCE_UIDGID */
114 #define VOLOPT_UMASK 21
115 #define VOLOPT_ALLOWED_HOSTS 22
116 #define VOLOPT_DENIED_HOSTS 23
117 #define VOLOPT_DPERM 24 /* dperm default directories perms */
118 #define VOLOPT_FPERM 25 /* fperm default files perms */
119 #define VOLOPT_DFLTPERM 26 /* perm */
120 #define VOLOPT_EA_VFS 27 /* Extended Attributes vfs indirection */
121 #define VOLOPT_CNIDSERVER 28 /* CNID Server ip address*/
122 #define VOLOPT_CNIDPORT 30 /* CNID server tcp port */
124 #define VOLOPT_MAX 31 /* <== IMPORTANT !!!!!! */
125 #define VOLOPT_NUM (VOLOPT_MAX + 1)
128 #define VOLOPT_DEFAULT ":DEFAULT:"
129 #define VOLOPT_DEFAULT_LEN 9
136 typedef struct _special_folder {
143 static const _special_folder special_folders[] = {
144 {"Network Trash Folder", 1, 0777, 1},
145 {"Temporary Items", 1, 0777, 1},
146 {".AppleDesktop", 1, 0777, 0},
148 {"TheFindByContentFolder", 0, 0, 1},
149 {"TheVolumeSettingsFolder", 0, 0, 1},
153 /* Forward declarations */
154 static void handle_special_folders (const struct vol *);
155 static void deletevol(struct vol *vol);
156 static void volume_free(struct vol *vol);
157 static void check_ea_sys_support(struct vol *vol);
159 static void volfree(struct vol_option *options, const struct vol_option *save)
164 for (i = 0; i < VOLOPT_MAX; i++) {
165 if (options[i].c_value && (options[i].c_value != save[i].c_value))
166 free(options[i].c_value);
169 for (i = 0; i < VOLOPT_MAX; i++) {
170 if (options[i].c_value)
171 free(options[i].c_value);
177 /* handle variable substitutions. here's what we understand:
178 * $b -> basename of path
179 * $c -> client ip/appletalk address
180 * $d -> volume pathname on server
181 * $f -> full name (whatever's in the gecos field)
184 * $i -> client ip/appletalk address without port
185 * $s -> server name (hostname if it doesn't exist)
186 * $u -> username (guest is usually nobody)
187 * $v -> volume name or basename if null
188 * $z -> zone (may not exist)
193 #define is_var(a, b) (strncmp((a), (b), 2) == 0)
195 static char *volxlate(AFPObj *obj, char *dest, size_t destlen,
196 char *src, struct passwd *pwd, char *path, char *volname)
207 dest = calloc(destlen +1, 1);
213 strlcpy(dest, src, destlen +1);
214 if ((p = strchr(src, '$')) == NULL) /* nothing to do */
217 /* first part of the path. just forward to the next variable. */
218 len = MIN((size_t)(p - src), destlen);
224 while (p && destlen > 0) {
225 /* now figure out what the variable is */
227 if (is_var(p, "$b")) {
229 if ((q = strrchr(path, '/')) == NULL)
231 else if (*(q + 1) != '\0')
234 } else if (is_var(p, "$c")) {
235 if (obj->proto == AFPPROTO_ASP) {
236 ASP asp = obj->handle;
238 len = sprintf(dest, "%u.%u", ntohs(asp->asp_sat.sat_addr.s_net),
239 asp->asp_sat.sat_addr.s_node);
243 } else if (obj->proto == AFPPROTO_DSI) {
244 DSI *dsi = obj->handle;
245 len = sprintf(dest, "%s:%u",
246 getip_string((struct sockaddr *)&dsi->client),
247 getip_port((struct sockaddr *)&dsi->client));
251 } else if (is_var(p, "$d")) {
253 } else if (is_var(p, "$f")) {
254 if ((r = strchr(pwd->pw_gecos, ',')))
257 } else if (is_var(p, "$g")) {
258 struct group *grp = getgrgid(pwd->pw_gid);
261 } else if (is_var(p, "$h")) {
262 q = obj->options.hostname;
263 } else if (is_var(p, "$i")) {
264 if (obj->proto == AFPPROTO_ASP) {
265 ASP asp = obj->handle;
267 len = sprintf(dest, "%u", ntohs(asp->asp_sat.sat_addr.s_net));
271 } else if (obj->proto == AFPPROTO_DSI) {
272 DSI *dsi = obj->handle;
273 q = getip_string((struct sockaddr *)&dsi->client);
275 } else if (is_var(p, "$s")) {
278 else if (obj->options.server) {
279 q = obj->options.server;
281 q = obj->options.hostname;
282 } else if (is_var(p, "$u")) {
284 if ( obj->options.ntseparator && (sep = strchr(obj->username, obj->options.ntseparator[0])) != NULL)
288 } else if (is_var(p, "$v")) {
293 if ((q = strrchr(path, '/')) == NULL)
295 else if (*(q + 1) != '\0')
298 } else if (is_var(p, "$z")) {
300 } else if (is_var(p, "$$")) {
305 /* copy the stuff over. if we don't understand something that we
306 * should, just skip it over. */
308 len = MIN(p == q ? 2 : strlen(q), destlen);
309 strncpy(dest, q, len);
314 /* stuff up to next $ */
316 p = strchr(src, '$');
317 len = p ? MIN((size_t)(p - src), destlen) : destlen;
319 strncpy(dest, src, len);
327 /* to make sure that val is valid, make sure to select an opt that
329 static int optionok(const char *buf, const char *opt, const char *val)
331 if (!strstr(buf,opt))
339 /* -------------------- */
340 static void setoption(struct vol_option *options, struct vol_option *save, int opt, const char *val)
342 if (options[opt].c_value && (!save || options[opt].c_value != save[opt].c_value))
343 free(options[opt].c_value);
344 options[opt].c_value = strdup(val + 1);
347 /* ------------------------------------------
348 handle all the options. tmp can't be NULL. */
349 static void volset(struct vol_option *options, struct vol_option *save,
350 char *volname, int vlen,
355 val = strchr(tmp, ':');
357 /* we'll assume it's a volume name. */
358 strncpy(volname, tmp, vlen);
363 LOG(log_debug, logtype_afpd, "Parsing volset %s", val);
365 if (optionok(tmp, "allow:", val)) {
366 setoption(options, save, VOLOPT_ALLOW, val);
368 } else if (optionok(tmp, "deny:", val)) {
369 setoption(options, save, VOLOPT_DENY, val);
371 } else if (optionok(tmp, "rwlist:", val)) {
372 setoption(options, save, VOLOPT_RWLIST, val);
374 } else if (optionok(tmp, "rolist:", val)) {
375 setoption(options, save, VOLOPT_ROLIST, val);
377 } else if (optionok(tmp, "codepage:", val)) {
378 LOG (log_error, logtype_afpd, "The old codepage system has been removed. Please make sure to read the documentation !!!!");
379 /* Make sure we don't screw anything */
381 } else if (optionok(tmp, "volcharset:", val)) {
382 setoption(options, save, VOLOPT_ENCODING, val);
383 } else if (optionok(tmp, "maccharset:", val)) {
384 setoption(options, save, VOLOPT_MACCHARSET, val);
385 } else if (optionok(tmp, "veto:", val)) {
386 setoption(options, save, VOLOPT_VETO, val);
387 } else if (optionok(tmp, "cnidscheme:", val)) {
388 setoption(options, save, VOLOPT_CNIDSCHEME, val);
389 } else if (optionok(tmp, "casefold:", val)) {
390 if (strcasecmp(val + 1, "tolower") == 0)
391 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMLOWER;
392 else if (strcasecmp(val + 1, "toupper") == 0)
393 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMUPPER;
394 else if (strcasecmp(val + 1, "xlatelower") == 0)
395 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UUPPERMLOWER;
396 else if (strcasecmp(val + 1, "xlateupper") == 0)
397 options[VOLOPT_CASEFOLD].i_value = AFPVOL_ULOWERMUPPER;
398 } else if (optionok(tmp, "adouble:", val)) {
399 if (strcasecmp(val + 1, "v1") == 0)
400 options[VOLOPT_ADOUBLE].i_value = AD_VERSION1;
401 #if AD_VERSION == AD_VERSION2
402 else if (strcasecmp(val + 1, "v2") == 0)
403 options[VOLOPT_ADOUBLE].i_value = AD_VERSION2;
404 else if (strcasecmp(val + 1, "osx") == 0)
405 options[VOLOPT_ADOUBLE].i_value = AD_VERSION2_OSX;
406 else if (strcasecmp(val + 1, "sfm") == 0)
407 options[VOLOPT_ADOUBLE].i_value = AD_VERSION1_SFM;
409 } else if (optionok(tmp, "options:", val)) {
412 if ((p = strtok(val + 1, ",")) == NULL) /* nothing */
416 if (strcasecmp(p, "prodos") == 0)
417 options[VOLOPT_FLAGS].i_value |= AFPVOL_A2VOL;
418 else if (strcasecmp(p, "mswindows") == 0) {
419 options[VOLOPT_FLAGS].i_value |= AFPVOL_MSWINDOWS | AFPVOL_USEDOTS;
420 } else if (strcasecmp(p, "crlf") == 0)
421 options[VOLOPT_FLAGS].i_value |= AFPVOL_CRLF;
422 else if (strcasecmp(p, "noadouble") == 0)
423 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOADOUBLE;
424 else if (strcasecmp(p, "ro") == 0)
425 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
426 else if (strcasecmp(p, "nohex") == 0)
427 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOHEX;
428 else if (strcasecmp(p, "usedots") == 0)
429 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS;
430 else if (strcasecmp(p, "invisibledots") == 0)
431 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS | AFPVOL_INV_DOTS;
432 else if (strcasecmp(p, "limitsize") == 0)
433 options[VOLOPT_FLAGS].i_value |= AFPVOL_LIMITSIZE;
434 /* support for either "dropbox" or "dropkludge" */
435 else if (strcasecmp(p, "dropbox") == 0)
436 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
437 else if (strcasecmp(p, "dropkludge") == 0)
438 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
439 else if (strcasecmp(p, "nofileid") == 0)
440 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOFILEID;
441 else if (strcasecmp(p, "nostat") == 0)
442 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOSTAT;
443 else if (strcasecmp(p, "preexec_close") == 0)
444 options[VOLOPT_PREEXEC].i_value = 1;
445 else if (strcasecmp(p, "root_preexec_close") == 0)
446 options[VOLOPT_ROOTPREEXEC].i_value = 1;
447 else if (strcasecmp(p, "upriv") == 0)
448 options[VOLOPT_FLAGS].i_value |= AFPVOL_UNIX_PRIV;
449 else if (strcasecmp(p, "acls") == 0)
450 options[VOLOPT_FLAGS].i_value |= AFPVOL_ACLS;
451 else if (strcasecmp(p, "nodev") == 0)
452 options[VOLOPT_FLAGS].i_value |= AFPVOL_NODEV;
453 else if (strcasecmp(p, "caseinsensitive") == 0)
454 options[VOLOPT_FLAGS].i_value |= AFPVOL_CASEINSEN;
455 else if (strcasecmp(p, "illegalseq") == 0)
456 options[VOLOPT_FLAGS].i_value |= AFPVOL_EILSEQ;
457 else if (strcasecmp(p, "nocnidcache") == 0)
458 options[VOLOPT_FLAGS].i_value &= ~AFPVOL_CACHE;
459 else if (strcasecmp(p, "tm") == 0)
460 options[VOLOPT_FLAGS].i_value |= AFPVOL_TM;
462 p = strtok(NULL, ",");
465 } else if (optionok(tmp, "cnidserver:", val)) {
466 setoption(options, save, VOLOPT_CNIDSERVER, val);
468 char *p = strrchr(val + 1, ':');
471 setoption(options, save, VOLOPT_CNIDPORT, p);
474 LOG(log_debug, logtype_afpd, "CNID Server for volume '%s': %s:%s",
475 volname, val + 1, p ? p + 1 : Cnid_port);
477 } else if (optionok(tmp, "dbpath:", val)) {
478 setoption(options, save, VOLOPT_DBPATH, val);
480 } else if (optionok(tmp, "umask:", val)) {
481 options[VOLOPT_UMASK].i_value = (int)strtol(val +1, NULL, 8);
482 } else if (optionok(tmp, "dperm:", val)) {
483 options[VOLOPT_DPERM].i_value = (int)strtol(val+1, NULL, 8);
484 } else if (optionok(tmp, "fperm:", val)) {
485 options[VOLOPT_FPERM].i_value = (int)strtol(val+1, NULL, 8);
486 } else if (optionok(tmp, "perm:", val)) {
487 options[VOLOPT_DFLTPERM].i_value = (int)strtol(val+1, NULL, 8);
488 } else if (optionok(tmp, "password:", val)) {
489 setoption(options, save, VOLOPT_PASSWORD, val);
493 /* this code allows forced uid/gid per volume settings */
494 } else if (optionok(tmp, "forceuid:", val)) {
495 setoption(options, save, VOLOPT_FORCEUID, val);
496 } else if (optionok(tmp, "forcegid:", val)) {
497 setoption(options, save, VOLOPT_FORCEGID, val);
499 #endif /* FORCE_UIDGID */
500 } else if (optionok(tmp, "root_preexec:", val)) {
501 setoption(options, save, VOLOPT_ROOTPREEXEC, val);
503 } else if (optionok(tmp, "preexec:", val)) {
504 setoption(options, save, VOLOPT_PREEXEC, val);
506 } else if (optionok(tmp, "root_postexec:", val)) {
507 setoption(options, save, VOLOPT_ROOTPOSTEXEC, val);
509 } else if (optionok(tmp, "postexec:", val)) {
510 setoption(options, save, VOLOPT_POSTEXEC, val);
512 } else if (optionok(tmp, "allowed_hosts:", val)) {
513 setoption(options, save, VOLOPT_ALLOWED_HOSTS, val);
515 } else if (optionok(tmp, "denied_hosts:", val)) {
516 setoption(options, save, VOLOPT_DENIED_HOSTS, val);
518 } else if (optionok(tmp, "ea:", val)) {
519 if (strcasecmp(val + 1, "ad") == 0)
520 options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_AD;
521 else if (strcasecmp(val + 1, "sys") == 0)
522 options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_SYS;
523 else if (strcasecmp(val + 1, "none") == 0)
524 options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_NONE;
527 /* ignore unknown options */
528 LOG(log_debug, logtype_afpd, "ignoring unknown volume option: %s", tmp);
533 /* ----------------- */
534 static void showvol(const ucs2_t *name)
537 for ( volume = Volumes; volume; volume = volume->v_next ) {
538 if (volume->v_hide && !strcasecmp_w( volume->v_name, name ) ) {
545 /* ------------------------------- */
546 static int creatvol(AFPObj *obj, struct passwd *pwd,
547 char *path, char *name,
548 struct vol_option *options,
549 const int user /* user defined volume */
553 int suffixlen, vlen, tmpvlen, u8mvlen, macvlen;
555 char tmpname[AFPVOL_U8MNAMELEN+1];
556 ucs2_t u8mtmpname[(AFPVOL_U8MNAMELEN+1)*2], mactmpname[(AFPVOL_MACNAMELEN+1)*2];
557 char suffix[6]; /* max is #FFFF */
560 if ( name == NULL || *name == '\0' ) {
561 if ((name = strrchr( path, '/' )) == NULL) {
562 return -1; /* Obviously not a fully qualified path */
565 /* if you wish to share /, you need to specify a name. */
570 /* suffix for mangling use (lastvid + 1) */
571 /* because v_vid has not been decided yet. */
572 suffixlen = sprintf(suffix, "%c%X", MANGLE_CHAR, lastvid + 1 );
574 vlen = strlen( name );
576 /* Unicode Volume Name */
577 /* Firstly convert name from unixcharset to UTF8-MAC */
579 tmpvlen = convert_charset(obj->options.unixcharset, CH_UTF8_MAC, 0, name, vlen, tmpname, AFPVOL_U8MNAMELEN, &flags);
581 strcpy(tmpname, "???");
585 /* Do we have to mangle ? */
586 if ( (flags & CONV_REQMANGLE) || (tmpvlen > obj->options.volnamelen)) {
587 if (tmpvlen + suffixlen > obj->options.volnamelen) {
589 tmpvlen = convert_charset(obj->options.unixcharset, CH_UTF8_MAC, 0, name, vlen, tmpname, obj->options.volnamelen - suffixlen, &flags);
590 tmpname[tmpvlen >= 0 ? tmpvlen : 0] = 0;
592 strcat(tmpname, suffix);
593 tmpvlen = strlen(tmpname);
596 /* Secondly convert name from UTF8-MAC to UCS2 */
597 if ( 0 >= ( u8mvlen = convert_string(CH_UTF8_MAC, CH_UCS2, tmpname, tmpvlen, u8mtmpname, AFPVOL_U8MNAMELEN*2)) )
600 LOG(log_debug, logtype_afpd, "createvol: Volume '%s' -> UTF8-MAC Name: '%s'", name, tmpname);
602 /* Maccharset Volume Name */
603 /* Firsty convert name from unixcharset to maccharset */
605 tmpvlen = convert_charset(obj->options.unixcharset, obj->options.maccharset, 0, name, vlen, tmpname, AFPVOL_U8MNAMELEN, &flags);
607 strcpy(tmpname, "???");
611 /* Do we have to mangle ? */
612 if ( (flags & CONV_REQMANGLE) || (tmpvlen > AFPVOL_MACNAMELEN)) {
613 if (tmpvlen + suffixlen > AFPVOL_MACNAMELEN) {
615 tmpvlen = convert_charset(obj->options.unixcharset, obj->options.maccharset, 0, name, vlen, tmpname, AFPVOL_MACNAMELEN - suffixlen, &flags);
616 tmpname[tmpvlen >= 0 ? tmpvlen : 0] = 0;
618 strcat(tmpname, suffix);
619 tmpvlen = strlen(tmpname);
622 /* Secondly convert name from maccharset to UCS2 */
623 if ( 0 >= ( macvlen = convert_string(obj->options.maccharset, CH_UCS2, tmpname, tmpvlen, mactmpname, AFPVOL_U8MNAMELEN*2)) )
626 LOG(log_debug, logtype_afpd, "createvol: Volume '%s' -> Longname: '%s'", name, tmpname);
628 /* check duplicate */
629 for ( volume = Volumes; volume; volume = volume->v_next ) {
630 if (( strcasecmp_w( volume->v_u8mname, u8mtmpname ) == 0 ) || ( strcasecmp_w( volume->v_macname, mactmpname ) == 0 )){
631 if (volume->v_deleted) {
632 volume->v_new = hide = 1;
635 return -1; /* Won't be able to access it, anyway... */
640 if (!( volume = (struct vol *)calloc(1, sizeof( struct vol ))) ) {
641 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
644 if ( NULL == ( volume->v_localname = strdup(name))) {
645 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
650 if ( NULL == ( volume->v_u8mname = strdup_w(u8mtmpname))) {
651 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
656 if ( NULL == ( volume->v_macname = strdup_w(mactmpname))) {
657 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
662 if (!( volume->v_path = (char *)malloc( strlen( path ) + 1 )) ) {
663 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
669 volume->v_name = utf8_encoding()?volume->v_u8mname:volume->v_macname;
670 volume->v_hide = hide;
671 strcpy( volume->v_path, path );
675 #endif /* __svr4__ */
676 /* os X start at 1 and use network order ie. 1 2 3 */
677 volume->v_vid = ++lastvid;
678 volume->v_vid = htons(volume->v_vid);
682 /* should we casefold? */
683 volume->v_casefold = options[VOLOPT_CASEFOLD].i_value;
685 /* shift in some flags */
686 volume->v_flags = options[VOLOPT_FLAGS].i_value;
688 if (options[VOLOPT_EA_VFS].i_value)
689 volume->v_vfs_ea = options[VOLOPT_EA_VFS].i_value;
691 volume->v_ad_options = 0;
692 if ((volume->v_flags & AFPVOL_NODEV))
693 volume->v_ad_options |= ADVOL_NODEV;
694 if ((volume->v_flags & AFPVOL_CACHE))
695 volume->v_ad_options |= ADVOL_CACHE;
696 if ((volume->v_flags & AFPVOL_UNIX_PRIV))
697 volume->v_ad_options |= ADVOL_UNIXPRIV;
698 if ((volume->v_flags & AFPVOL_INV_DOTS))
699 volume->v_ad_options |= ADVOL_INVDOTS;
700 if ((volume->v_flags & AFPVOL_NOADOUBLE))
701 volume->v_ad_options |= ADVOL_NOADOUBLE;
703 if (options[VOLOPT_PASSWORD].c_value)
704 volume->v_password = strdup(options[VOLOPT_PASSWORD].c_value);
706 if (options[VOLOPT_VETO].c_value)
707 volume->v_veto = strdup(options[VOLOPT_VETO].c_value);
709 if (options[VOLOPT_ENCODING].c_value)
710 volume->v_volcodepage = strdup(options[VOLOPT_ENCODING].c_value);
712 if (options[VOLOPT_MACCHARSET].c_value)
713 volume->v_maccodepage = strdup(options[VOLOPT_MACCHARSET].c_value);
715 if (options[VOLOPT_DBPATH].c_value)
716 volume->v_dbpath = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_DBPATH].c_value, pwd, path, name);
718 if (options[VOLOPT_CNIDSCHEME].c_value)
719 volume->v_cnidscheme = strdup(options[VOLOPT_CNIDSCHEME].c_value);
721 if (options[VOLOPT_CNIDSERVER].c_value)
722 volume->v_cnidserver = strdup(options[VOLOPT_CNIDSERVER].c_value);
724 if (options[VOLOPT_CNIDPORT].c_value)
725 volume->v_cnidport = strdup(options[VOLOPT_CNIDPORT].c_value);
727 if (options[VOLOPT_UMASK].i_value)
728 volume->v_umask = (mode_t)options[VOLOPT_UMASK].i_value;
730 if (options[VOLOPT_DPERM].i_value)
731 volume->v_dperm = (mode_t)options[VOLOPT_DPERM].i_value;
733 if (options[VOLOPT_FPERM].i_value)
734 volume->v_fperm = (mode_t)options[VOLOPT_FPERM].i_value;
736 if (options[VOLOPT_DFLTPERM].i_value)
737 volume->v_perm = (mode_t)options[VOLOPT_DFLTPERM].i_value;
739 if (options[VOLOPT_ADOUBLE].i_value)
740 volume->v_adouble = options[VOLOPT_ADOUBLE].i_value;
742 volume->v_adouble = AD_VERSION;
744 /* Mac to Unix conversion flags*/
745 volume->v_mtou_flags = 0;
746 if (!(volume->v_flags & AFPVOL_NOHEX))
747 volume->v_mtou_flags |= CONV_ESCAPEHEX;
748 if (!(volume->v_flags & AFPVOL_USEDOTS))
749 volume->v_mtou_flags |= CONV_ESCAPEDOTS;
750 if ((volume->v_flags & AFPVOL_EILSEQ))
751 volume->v_mtou_flags |= CONV__EILSEQ;
753 if ((volume->v_casefold & AFPVOL_MTOUUPPER))
754 volume->v_mtou_flags |= CONV_TOUPPER;
755 else if ((volume->v_casefold & AFPVOL_MTOULOWER))
756 volume->v_mtou_flags |= CONV_TOLOWER;
758 /* Unix to Mac conversion flags*/
759 volume->v_utom_flags = CONV_IGNORE | CONV_UNESCAPEHEX;
760 if ((volume->v_casefold & AFPVOL_UTOMUPPER))
761 volume->v_utom_flags |= CONV_TOUPPER;
762 else if ((volume->v_casefold & AFPVOL_UTOMLOWER))
763 volume->v_utom_flags |= CONV_TOLOWER;
765 if ((volume->v_flags & AFPVOL_EILSEQ))
766 volume->v_utom_flags |= CONV__EILSEQ;
769 if (options[VOLOPT_FORCEUID].c_value) {
770 volume->v_forceuid = strdup(options[VOLOPT_FORCEUID].c_value);
772 volume->v_forceuid = NULL; /* set as null so as to return 0 later on */
775 if (options[VOLOPT_FORCEGID].c_value) {
776 volume->v_forcegid = strdup(options[VOLOPT_FORCEGID].c_value);
778 volume->v_forcegid = NULL; /* set as null so as to return 0 later on */
782 if (options[VOLOPT_PREEXEC].c_value)
783 volume->v_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_PREEXEC].c_value, pwd, path, name);
784 volume->v_preexec_close = options[VOLOPT_PREEXEC].i_value;
786 if (options[VOLOPT_POSTEXEC].c_value)
787 volume->v_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_POSTEXEC].c_value, pwd, path, name);
789 if (options[VOLOPT_ROOTPREEXEC].c_value)
790 volume->v_root_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPREEXEC].c_value, pwd, path, name);
791 volume->v_root_preexec_close = options[VOLOPT_ROOTPREEXEC].i_value;
793 if (options[VOLOPT_ROOTPOSTEXEC].c_value)
794 volume->v_root_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPOSTEXEC].c_value, pwd, path, name);
797 volume->v_dperm |= volume->v_perm;
798 volume->v_fperm |= volume->v_perm;
800 /* Check EA support on volume */
801 if (volume->v_vfs_ea == AFPVOL_EA_AUTO)
802 check_ea_sys_support(volume);
805 volume->v_next = Volumes;
810 /* ---------------- */
811 static char *myfgets( char *buf, int size, FILE *fp)
817 while ((EOF != ( c = getc( fp )) ) && ( size > 1 )) {
818 if ( c == '\n' || c == '\r' ) {
819 if (p != buf && *(p -1) == '\\') {
841 /* check access list. this function wants something of the following
843 * @group,name,name2,@group2,name3
845 * a NULL argument allows everybody to have access.
846 * we return three things:
848 * 0: list exists, but name isn't in it
852 #ifndef NO_REAL_USER_NAME
853 /* authentication is case insensitive
854 * FIXME should we do the same with group name?
856 #define access_strcmp strcasecmp
859 #define access_strcmp strcmp
863 static int accessvol(const char *args, const char *name)
865 char buf[MAXPATHLEN + 1], *p;
871 strlcpy(buf, args, sizeof(buf));
872 if ((p = strtok(buf, ",")) == NULL) /* nothing, return okay */
876 if (*p == '@') { /* it's a group */
877 if ((gr = getgrnam(p + 1)) && gmem(gr->gr_gid))
879 } else if (access_strcmp(p, name) == 0) /* it's a user name */
881 p = strtok(NULL, ",");
887 static int hostaccessvol(int type, const char *volname, const char *args, const AFPObj *obj)
890 char buf[MAXPATHLEN + 1], *p, *b;
891 DSI *dsi = obj->handle;
892 struct sockaddr_storage client;
897 strlcpy(buf, args, sizeof(buf));
898 if ((p = strtok_r(buf, ",", &b)) == NULL) /* nothing, return okay */
901 if (obj->proto != AFPPROTO_DSI)
906 char *ipaddr, *mask_char;
907 struct addrinfo hints, *ai;
909 ipaddr = strtok(p, "/");
910 mask_char = strtok(NULL,"/");
912 /* Get address from string with getaddrinfo */
913 memset(&hints, 0, sizeof hints);
914 hints.ai_family = AF_UNSPEC;
915 hints.ai_socktype = SOCK_STREAM;
916 if ((ret = getaddrinfo(ipaddr, NULL, &hints, &ai)) != 0) {
917 LOG(log_error, logtype_afpd, "hostaccessvol: getaddrinfo: %s\n", gai_strerror(ret));
922 if (mask_char != NULL)
923 mask_int = atoi(mask_char); /* apply_ip_mask does range checking on it */
925 if (ai->ai_family == AF_INET) /* IPv4 */
931 /* Apply mask to addresses */
932 client = dsi->client;
933 apply_ip_mask((struct sockaddr *)&client, mask_int);
934 apply_ip_mask(ai->ai_addr, mask_int);
936 if (compare_ip((struct sockaddr *)&client, ai->ai_addr) == 0) {
937 if (type == VOLOPT_DENIED_HOSTS)
938 LOG(log_info, logtype_afpd, "AFP access denied for client IP '%s' to volume '%s' by denied list",
939 getip_string((struct sockaddr *)&client), volname);
946 p = strtok_r(NULL, ",", &b);
949 if (type == VOLOPT_ALLOWED_HOSTS)
950 LOG(log_info, logtype_afpd, "AFP access denied for client IP '%s' to volume '%s', not in allowed list",
951 getip_string((struct sockaddr *)&dsi->client), volname);
955 static void setextmap(char *ext, char *type, char *creator, int user)
960 if (Extmap == NULL) {
961 if (( Extmap = calloc(1, sizeof( struct extmap ))) == NULL ) {
962 LOG(log_error, logtype_afpd, "setextmap: calloc: %s", strerror(errno) );
967 for ( em = Extmap, cnt = 0; em->em_ext; em++, cnt++) {
968 if ( (strdiacasecmp( em->em_ext, ext )) == 0 ) {
973 if ( em->em_ext == NULL ) {
974 if (!(Extmap = realloc( Extmap, sizeof( struct extmap ) * (cnt +2))) ) {
975 LOG(log_error, logtype_afpd, "setextmap: realloc: %s", strerror(errno) );
978 (Extmap +cnt +1)->em_ext = NULL;
980 } else if ( !user ) {
986 if (!(em->em_ext = strdup( ext))) {
987 LOG(log_error, logtype_afpd, "setextmap: strdup: %s", strerror(errno) );
991 if ( *type == '\0' ) {
992 memcpy(em->em_type, "\0\0\0\0", sizeof( em->em_type ));
994 memcpy(em->em_type, type, sizeof( em->em_type ));
996 if ( *creator == '\0' ) {
997 memcpy(em->em_creator, "\0\0\0\0", sizeof( em->em_creator ));
999 memcpy(em->em_creator, creator, sizeof( em->em_creator ));
1003 /* -------------------------- */
1004 static int extmap_cmp(const void *map1, const void *map2)
1006 const struct extmap *em1 = map1;
1007 const struct extmap *em2 = map2;
1008 return strdiacasecmp(em1->em_ext, em2->em_ext);
1011 static void sortextmap( void)
1016 if ((em = Extmap) == NULL) {
1019 while (em->em_ext) {
1024 qsort(Extmap, Extmap_cnt, sizeof(struct extmap), extmap_cmp);
1025 if (*Extmap->em_ext == 0) {
1026 /* the first line is really "." the default entry,
1027 * we remove the leading '.' in setextmap
1034 /* ----------------------
1036 static void free_extmap( void)
1041 for ( em = Extmap; em->em_ext; em++) {
1051 /* ----------------------
1053 static int volfile_changed(struct afp_volume_name *p)
1059 name = p->full_name;
1063 if (!stat( name, &st) && st.st_mtime > p->mtime) {
1064 p->mtime = st.st_mtime;
1070 /* ----------------------
1071 * Read a volume configuration file and add the volumes contained within to
1072 * the global volume list. If p2 is non-NULL, the file that is opened is
1075 * Lines that begin with # and blank lines are ignored.
1076 * Volume lines are of the form:
1077 * <unix path> [<volume name>] [allow:<user>,<@group>,...] \
1078 * [codepage:<file>] [casefold:<num>]
1079 * <extension> TYPE [CREATOR]
1081 static int readvolfile(AFPObj *obj, struct afp_volume_name *p1, char *p2, int user, struct passwd *pwent)
1084 char path[MAXPATHLEN + 1];
1085 char tmp[MAXPATHLEN + 1];
1086 char volname[AFPVOL_U8MNAMELEN + 1];
1088 char type[5], creator[5];
1091 struct vol_option save_options[VOLOPT_NUM];
1092 struct vol_option options[VOLOPT_NUM];
1100 strcpy( path, p1->name );
1102 strcat( path, "/" );
1104 if (p1->full_name) {
1105 free(p1->full_name);
1107 p1->full_name = strdup(path);
1110 if (NULL == ( fp = fopen( path, "r" )) ) {
1114 if (fd != -1 && !fstat( fd, &st) ) {
1115 p1->mtime = st.st_mtime;
1118 memset(save_options, 0, sizeof(save_options));
1120 /* Enable some default options for all volumes */
1121 save_options[VOLOPT_FLAGS].i_value |= AFPVOL_CACHE;
1122 save_options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_AUTO;
1124 while ( myfgets( buf, sizeof( buf ), fp ) != NULL ) {
1125 initline( strlen( buf ), buf );
1126 parseline( sizeof( path ) - 1, path );
1133 /* change the default options for this file */
1134 if (strncmp(path, VOLOPT_DEFAULT, VOLOPT_DEFAULT_LEN) == 0) {
1136 for (i = 0; i < VOLOPT_NUM; i++) {
1137 if (parseline( sizeof( path ) - VOLOPT_DEFAULT_LEN - 1,
1138 path + VOLOPT_DEFAULT_LEN) < 0)
1140 volset(save_options, NULL, tmp, sizeof(tmp) - 1,
1141 path + VOLOPT_DEFAULT_LEN);
1147 if (( p = strchr( path, '/' )) != NULL ) {
1155 if ( u == NULL || *u == '\0' || ( pw = getpwnam( u )) == NULL ) {
1158 strcpy( tmp, pw->pw_dir );
1159 if ( p != NULL && *p != '\0' ) {
1163 /* Tag a user's home directory with their umask. Note, this will
1164 * be overwritten if the user actually specifies a umask: option
1165 * for a '~' volume. */
1166 save_options[VOLOPT_UMASK].i_value = obj->options.save_mask;
1170 /* send path through variable substitution */
1171 if (*path != '~') /* need to copy path to tmp */
1174 pwent = getpwnam(obj->username);
1175 volxlate(obj, path, sizeof(path) - 1, tmp, pwent, NULL, NULL);
1177 /* this is sort of braindead. basically, i want to be
1178 * able to specify things in any order, but i don't want to
1179 * re-write everything.
1181 * currently we have options:
1189 * options:prodos,crlf,noadouble,ro...
1194 * namemask:x,y,!z (not implemented yet)
1196 memcpy(options, save_options, sizeof(options));
1199 /* read in up to VOLOP_NUM possible options */
1200 for (i = 0; i < VOLOPT_NUM; i++) {
1201 if (parseline( sizeof( tmp ) - 1, tmp ) < 0)
1204 volset(options, save_options, volname, sizeof(volname) - 1, tmp);
1207 /* check allow/deny lists:
1208 allow -> either no list (-1), or in list (1)
1209 deny -> either no list (-1), or not in list (0) */
1210 if (accessvol(options[VOLOPT_ALLOW].c_value, obj->username) &&
1211 (accessvol(options[VOLOPT_DENY].c_value, obj->username) < 1) &&
1212 hostaccessvol(VOLOPT_ALLOWED_HOSTS, volname, options[VOLOPT_ALLOWED_HOSTS].c_value, obj) &&
1213 (hostaccessvol(VOLOPT_DENIED_HOSTS, volname, options[VOLOPT_DENIED_HOSTS].c_value, obj) < 1)) {
1215 /* handle read-only behaviour. semantics:
1216 * 1) neither the rolist nor the rwlist exist -> rw
1217 * 2) rolist exists -> ro if user is in it.
1218 * 3) rwlist exists -> ro unless user is in it. */
1219 if (((options[VOLOPT_FLAGS].i_value & AFPVOL_RO) == 0) &&
1220 ((accessvol(options[VOLOPT_ROLIST].c_value,
1221 obj->username) == 1) ||
1222 !accessvol(options[VOLOPT_RWLIST].c_value,
1224 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
1226 /* do variable substitution for volname */
1227 volxlate(obj, tmp, sizeof(tmp) - 1, volname, pwent, path, NULL);
1228 creatvol(obj, pwent, path, tmp, options, p2 != NULL);
1230 volfree(options, save_options);
1234 parseline( sizeof( type ) - 1, type );
1235 parseline( sizeof( creator ) - 1, creator );
1236 setextmap( path, type, creator, user);
1243 volfree(save_options, NULL);
1245 if ( fclose( fp ) != 0 ) {
1246 LOG(log_error, logtype_afpd, "readvolfile: fclose: %s", strerror(errno) );
1252 /* ------------------------------- */
1253 static void volume_free(struct vol *vol)
1255 free(vol->v_localname);
1256 vol->v_localname = NULL;
1257 free(vol->v_u8mname);
1258 vol->v_u8mname = NULL;
1259 free(vol->v_macname);
1260 vol->v_macname = NULL;
1262 free(vol->v_password);
1264 free(vol->v_volcodepage);
1265 free(vol->v_maccodepage);
1266 free(vol->v_cnidscheme);
1267 free(vol->v_dbpath);
1270 free(vol->v_forceuid);
1271 free(vol->v_forcegid);
1272 #endif /* FORCE_UIDGID */
1275 /* ------------------------------- */
1276 static void free_volumes(void )
1279 struct vol *nvol, *ovol;
1281 for ( vol = Volumes; vol; vol = vol->v_next ) {
1282 if (( vol->v_flags & AFPVOL_OPEN ) ) {
1289 for ( vol = Volumes, ovol = NULL; vol; vol = nvol) {
1292 if (vol->v_localname == NULL) {
1293 if (Volumes == vol) {
1298 ovol->v_next = nvol;
1308 /* ------------------------------- */
1309 static void volume_unlink(struct vol *volume)
1311 struct vol *vol, *ovol, *nvol;
1313 if (volume == Volumes) {
1314 Volumes = Volumes->v_next;
1317 for ( vol = Volumes->v_next, ovol = Volumes; vol; vol = nvol) {
1320 if (vol == volume) {
1321 ovol->v_next = nvol;
1330 static int getvolspace(struct vol *vol,
1331 u_int32_t *bfree, u_int32_t *btotal,
1332 VolSpace *xbfree, VolSpace *xbtotal, u_int32_t *bsize)
1336 #ifndef NO_QUOTA_SUPPORT
1337 VolSpace qfree, qtotal;
1340 spaceflag = AFPVOL_GVSMASK & vol->v_flags;
1341 /* report up to 2GB if afp version is < 2.2 (4GB if not) */
1342 maxsize = (vol->v_flags & AFPVOL_A2VOL) ? 0x01fffe00 :
1343 (((afp_version < 22) || (vol->v_flags & AFPVOL_LIMITSIZE))
1344 ? 0x7fffffffL : 0xffffffffL);
1347 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_AFSGVS ) {
1348 if ( afs_getvolspace( vol, xbfree, xbtotal, bsize ) == AFP_OK ) {
1349 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_AFSGVS;
1350 goto getvolspace_done;
1355 if (( rc = ustatfs_getvolspace( vol, xbfree, xbtotal,
1356 bsize)) != AFP_OK ) {
1360 #define min(a,b) ((a)<(b)?(a):(b))
1361 #ifndef NO_QUOTA_SUPPORT
1362 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_UQUOTA ) {
1363 if ( uquota_getvolspace( vol, &qfree, &qtotal, *bsize ) == AFP_OK ) {
1364 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_UQUOTA;
1365 *xbfree = min(*xbfree, qfree);
1366 *xbtotal = min( *xbtotal, qtotal);
1367 goto getvolspace_done;
1371 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_USTATFS;
1374 *bfree = min( *xbfree, maxsize);
1375 *btotal = min( *xbtotal, maxsize);
1379 /* -----------------------
1380 * set volume creation date
1381 * avoid duplicate, well at least it tries
1383 static void vol_setdate(u_int16_t id, struct adouble *adp, time_t date)
1386 struct vol *vol = Volumes;
1388 for ( volume = Volumes; volume; volume = volume->v_next ) {
1389 if (volume->v_vid == id) {
1392 else if ((time_t)(AD_DATE_FROM_UNIX(date)) == volume->v_ctime) {
1394 volume = Volumes; /* restart */
1397 vol->v_ctime = AD_DATE_FROM_UNIX(date);
1398 ad_setdate(adp, AD_DATE_CREATE | AD_DATE_UNIX, date);
1401 /* ----------------------- */
1402 static int getvolparams( u_int16_t bitmap, struct vol *vol, struct stat *st, char *buf, size_t *buflen)
1405 int bit = 0, isad = 1;
1408 u_int32_t bfree, btotal, bsize;
1409 VolSpace xbfree, xbtotal; /* extended bytes */
1410 char *data, *nameoff = NULL;
1413 LOG(log_debug, logtype_afpd, "getvolparams: Volume '%s'", vol->v_localname);
1415 /* courtesy of jallison@whistle.com:
1416 * For MacOS8.x support we need to create the
1417 * .Parent file here if it doesn't exist. */
1419 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1420 if ( ad_open_metadata( vol->v_path, ADFLAGS_DIR, O_CREAT, &ad) < 0 ) {
1422 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1424 } else if (ad_get_MD_flags( &ad ) & O_CREAT) {
1425 slash = strrchr( vol->v_path, '/' );
1429 slash = vol->v_path;
1430 if (ad_getentryoff(&ad, ADEID_NAME)) {
1431 ad_setentrylen( &ad, ADEID_NAME, strlen( slash ));
1432 memcpy(ad_entry( &ad, ADEID_NAME ), slash,
1433 ad_getentrylen( &ad, ADEID_NAME ));
1435 vol_setdate(vol->v_vid, &ad, st->st_mtime);
1439 if (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0)
1440 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1442 vol->v_ctime = aint;
1445 if (( bitmap & ( (1<<VOLPBIT_BFREE)|(1<<VOLPBIT_BTOTAL) |
1446 (1<<VOLPBIT_XBFREE)|(1<<VOLPBIT_XBTOTAL) |
1447 (1<<VOLPBIT_BSIZE)) ) != 0 ) {
1448 if ( getvolspace( vol, &bfree, &btotal, &xbfree, &xbtotal,
1449 &bsize) != AFP_OK ) {
1451 ad_close( &ad, ADFLAGS_HF );
1453 return( AFPERR_PARAM );
1458 while ( bitmap != 0 ) {
1459 while (( bitmap & 1 ) == 0 ) {
1467 /* check for read-only.
1468 * NOTE: we don't actually set the read-only flag unless
1469 * it's passed in that way as it's possible to mount
1470 * a read-write filesystem under a read-only one. */
1471 if ((vol->v_flags & AFPVOL_RO) ||
1472 ((utime(vol->v_path, NULL) < 0) && (errno == EROFS))) {
1473 ashort |= VOLPBIT_ATTR_RO;
1475 /* prior 2.1 only VOLPBIT_ATTR_RO is defined */
1476 if (afp_version > 20) {
1477 if (0 == (vol->v_flags & AFPVOL_NOFILEID) && vol->v_cdb != NULL &&
1478 (vol->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1479 ashort |= VOLPBIT_ATTR_FILEID;
1481 ashort |= VOLPBIT_ATTR_CATSEARCH;
1483 if (afp_version >= 30) {
1484 ashort |= VOLPBIT_ATTR_UTF8;
1485 if (vol->v_flags & AFPVOL_UNIX_PRIV)
1486 ashort |= VOLPBIT_ATTR_UNIXPRIV;
1487 if (vol->v_flags & AFPVOL_TM)
1488 ashort |= VOLPBIT_ATTR_TM;
1490 if (afp_version >= 32) {
1492 ashort |= VOLPBIT_ATTR_EXT_ATTRS;
1493 if (vol->v_flags & AFPVOL_ACLS)
1494 ashort |= VOLPBIT_ATTR_ACLS;
1498 ashort = htons(ashort);
1499 memcpy(data, &ashort, sizeof( ashort ));
1500 data += sizeof( ashort );
1504 ashort = htons( AFPVOLSIG_DEFAULT );
1505 memcpy(data, &ashort, sizeof( ashort ));
1506 data += sizeof( ashort );
1509 case VOLPBIT_CDATE :
1510 aint = vol->v_ctime;
1511 memcpy(data, &aint, sizeof( aint ));
1512 data += sizeof( aint );
1515 case VOLPBIT_MDATE :
1516 if ( st->st_mtime > vol->v_mtime ) {
1517 vol->v_mtime = st->st_mtime;
1519 aint = AD_DATE_FROM_UNIX(vol->v_mtime);
1520 memcpy(data, &aint, sizeof( aint ));
1521 data += sizeof( aint );
1524 case VOLPBIT_BDATE :
1525 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1526 aint = AD_DATE_START;
1527 memcpy(data, &aint, sizeof( aint ));
1528 data += sizeof( aint );
1532 memcpy(data, &vol->v_vid, sizeof( vol->v_vid ));
1533 data += sizeof( vol->v_vid );
1536 case VOLPBIT_BFREE :
1537 bfree = htonl( bfree );
1538 memcpy(data, &bfree, sizeof( bfree ));
1539 data += sizeof( bfree );
1542 case VOLPBIT_BTOTAL :
1543 btotal = htonl( btotal );
1544 memcpy(data, &btotal, sizeof( btotal ));
1545 data += sizeof( btotal );
1548 #ifndef NO_LARGE_VOL_SUPPORT
1549 case VOLPBIT_XBFREE :
1550 xbfree = hton64( xbfree );
1551 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1552 bcopy(&xbfree, data, sizeof(xbfree));
1553 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1554 memcpy(data, &xbfree, sizeof( xbfree ));
1555 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1556 data += sizeof( xbfree );
1559 case VOLPBIT_XBTOTAL :
1560 xbtotal = hton64( xbtotal );
1561 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1562 bcopy(&xbtotal, data, sizeof(xbtotal));
1563 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1564 memcpy(data, &xbtotal, sizeof( xbtotal ));
1565 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1566 data += sizeof( xbfree );
1568 #endif /* ! NO_LARGE_VOL_SUPPORT */
1572 data += sizeof( u_int16_t );
1575 case VOLPBIT_BSIZE: /* block size */
1576 bsize = htonl(bsize);
1577 memcpy(data, &bsize, sizeof(bsize));
1578 data += sizeof(bsize);
1583 ad_close( &ad, ADFLAGS_HF );
1585 return( AFPERR_BITMAP );
1591 ashort = htons( data - buf );
1592 memcpy(nameoff, &ashort, sizeof( ashort ));
1593 /* name is always in mac charset */
1594 aint = ucs2_to_charset( vol->v_maccharset, vol->v_macname, data+1, AFPVOL_MACNAMELEN + 1);
1604 ad_close_metadata( &ad);
1606 *buflen = data - buf;
1610 /* ------------------------- */
1611 static int stat_vol(u_int16_t bitmap, struct vol *vol, char *rbuf, size_t *rbuflen)
1617 if ( stat( vol->v_path, &st ) < 0 ) {
1619 return( AFPERR_PARAM );
1621 /* save the volume device number */
1622 vol->v_dev = st.st_dev;
1624 buflen = *rbuflen - sizeof( bitmap );
1625 if (( ret = getvolparams( bitmap, vol, &st,
1626 rbuf + sizeof( bitmap ), &buflen )) != AFP_OK ) {
1630 *rbuflen = buflen + sizeof( bitmap );
1631 bitmap = htons( bitmap );
1632 memcpy(rbuf, &bitmap, sizeof( bitmap ));
1637 /* ------------------------------- */
1638 void load_volumes(AFPObj *obj)
1640 struct passwd *pwent;
1645 /* check files date */
1646 if (obj->options.defaultvol.loaded) {
1647 changed = volfile_changed(&obj->options.defaultvol);
1649 if (obj->options.systemvol.loaded) {
1650 changed |= volfile_changed(&obj->options.systemvol);
1652 if (obj->options.uservol.loaded) {
1653 changed |= volfile_changed(&obj->options.uservol);
1662 pwent = getpwnam(obj->username);
1663 if ( (obj->options.flags & OPTION_USERVOLFIRST) == 0 ) {
1664 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent);
1667 if ((*obj->username == '\0') || (obj->options.flags & OPTION_NOUSERVOL)) {
1668 readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent);
1671 * Read user's AppleVolumes or .AppleVolumes file
1672 * If neither are readable, read the default volumes file. if
1673 * that doesn't work, create a user share.
1675 if (obj->options.uservol.name) {
1676 free(obj->options.uservol.name);
1678 obj->options.uservol.name = strdup(pwent->pw_dir);
1679 if ( readvolfile(obj, &obj->options.uservol, "AppleVolumes", 1, pwent) < 0 &&
1680 readvolfile(obj, &obj->options.uservol, ".AppleVolumes", 1, pwent) < 0 &&
1681 readvolfile(obj, &obj->options.uservol, "applevolumes", 1, pwent) < 0 &&
1682 readvolfile(obj, &obj->options.uservol, ".applevolumes", 1, pwent) < 0 &&
1683 obj->options.defaultvol.name != NULL ) {
1684 if (readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent) < 0)
1685 creatvol(obj, pwent, pwent->pw_dir, NULL, NULL, 1);
1688 if ( obj->options.flags & OPTION_USERVOLFIRST ) {
1689 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent );
1692 if ( obj->options.closevol ) {
1695 for ( vol = Volumes; vol; vol = vol->v_next ) {
1696 if (vol->v_deleted && !vol->v_new ) {
1705 /* ------------------------------- */
1706 int afp_getsrvrparms(AFPObj *obj, char *ibuf _U_, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
1719 for ( vcnt = 0, volume = Volumes; volume; volume = volume->v_next ) {
1720 if (!(volume->v_flags & AFPVOL_NOSTAT)) {
1723 if ( stat( volume->v_path, &st ) < 0 ) {
1724 LOG(log_info, logtype_afpd, "afp_getsrvrparms(%s): stat: %s",
1725 volume->v_path, strerror(errno) );
1726 continue; /* can't access directory */
1728 if (!S_ISDIR(st.st_mode)) {
1729 continue; /* not a dir */
1731 accessmode(volume->v_path, &ma, NULL, &st);
1732 if ((ma.ma_user & (AR_UREAD | AR_USEARCH)) != (AR_UREAD | AR_USEARCH)) {
1733 continue; /* no r-x access */
1736 if (volume->v_hide) {
1737 continue; /* config file changed but the volume was mounted */
1740 if (utf8_encoding()) {
1741 len = ucs2_to_charset_allocate(CH_UTF8_MAC, &namebuf, volume->v_u8mname);
1743 len = ucs2_to_charset_allocate(obj->options.maccharset, &namebuf, volume->v_macname);
1746 if (len == (size_t)-1)
1749 /* set password bit if there's a volume password */
1750 *data = (volume->v_password) ? AFPSRVR_PASSWD : 0;
1752 /* Apple 2 clients running ProDOS-8 expect one volume to have
1753 bit 0 of this byte set. They will not recognize anything
1754 on the server unless this is the case. I have not
1755 completely worked this out, but it's related to booting
1756 from the server. Support for that function is a ways
1757 off.. <shirsch@ibm.net> */
1758 *data |= (volume->v_flags & AFPVOL_A2VOL) ? AFPSRVR_CONFIGINFO : 0;
1759 *data++ |= 0; /* UNIX PRIVS BIT ..., OSX doesn't seem to use it, so we don't either */
1761 memcpy(data, namebuf, len );
1767 *rbuflen = data - rbuf;
1769 if ( gettimeofday( &tv, NULL ) < 0 ) {
1770 LOG(log_error, logtype_afpd, "afp_getsrvrparms(%s): gettimeofday: %s", volume->v_path, strerror(errno) );
1772 return AFPERR_PARAM;
1774 tv.tv_sec = AD_DATE_FROM_UNIX(tv.tv_sec);
1775 memcpy(data, &tv.tv_sec, sizeof( u_int32_t));
1776 data += sizeof( u_int32_t);
1781 /* ------------------------- */
1782 static int volume_codepage(AFPObj *obj, struct vol *volume)
1784 struct charset_functions *charset;
1787 if (!volume->v_volcodepage)
1788 volume->v_volcodepage = strdup("UTF8");
1790 if ( (charset_t) -1 == ( volume->v_volcharset = add_charset(volume->v_volcodepage)) ) {
1791 LOG (log_error, logtype_afpd, "Setting codepage %s as volume codepage failed", volume->v_volcodepage);
1795 if ( NULL == (charset = find_charset_functions(volume->v_volcodepage)) || charset->flags & CHARSET_ICONV ) {
1796 LOG (log_warning, logtype_afpd, "WARNING: volume encoding %s is *not* supported by netatalk, expect problems !!!!", volume->v_volcodepage);
1799 if (!volume->v_maccodepage)
1800 volume->v_maccodepage = strdup(obj->options.maccodepage);
1802 if ( (charset_t) -1 == ( volume->v_maccharset = add_charset(volume->v_maccodepage)) ) {
1803 LOG (log_error, logtype_afpd, "Setting codepage %s as mac codepage failed", volume->v_maccodepage);
1807 if ( NULL == ( charset = find_charset_functions(volume->v_maccodepage)) || ! (charset->flags & CHARSET_CLIENT) ) {
1808 LOG (log_error, logtype_afpd, "Fatal error: mac charset %s not supported", volume->v_maccodepage);
1811 volume->v_kTextEncoding = htonl(charset->kTextEncoding);
1815 /* ------------------------- */
1816 static int volume_openDB(struct vol *volume)
1820 if ((volume->v_flags & AFPVOL_NODEV)) {
1821 flags |= CNID_FLAG_NODEV;
1824 if (volume->v_cnidscheme == NULL) {
1825 volume->v_cnidscheme = strdup(DEFAULT_CNID_SCHEME);
1826 LOG(log_info, logtype_afpd, "Volume %s use CNID scheme %s.", volume->v_path, volume->v_cnidscheme);
1829 LOG(log_info, logtype_afpd, "%s:%s", volume->v_cnidserver, volume->v_cnidport);
1831 volume->v_cdb = cnid_open(volume->v_dbpath ? volume->v_dbpath : volume->v_path,
1833 volume->v_cnidscheme,
1835 volume->v_cnidserver ? volume->v_cnidserver : Cnid_srv,
1836 volume->v_cnidport ? volume->v_cnidport : Cnid_port);
1838 if (!volume->v_cdb) {
1839 flags |= CNID_FLAG_MEMORY;
1840 LOG(log_error, logtype_afpd, "Reopen volume %s using in memory temporary CNID DB.", volume->v_path);
1841 volume->v_cdb = cnid_open (volume->v_path, volume->v_umask, "tdb", flags, NULL, NULL);
1843 /* kill ourself with SIGUSR2 aka msg pending */
1844 if (volume->v_cdb) {
1845 setmessage("Something wrong with the volume's DB ... FIXME with a better msg");
1846 kill(getpid(), SIGUSR2);
1847 /* XXX desactivate cachecnid ? */
1852 return (!volume->v_cdb)?-1:0;
1856 Check if the underlying filesystem supports EAs for ea:sys volumes.
1857 If not, switch to ea:ad.
1858 As we can't check (requires write access) on ro-volumes, we switch ea:auto
1859 volumes that are options:ro to ea:none.
1861 static void check_ea_sys_support(struct vol *vol)
1863 uid_t process_uid = 0;
1864 char eaname[] = {"org.netatalk.supports-eas.XXXXXX"};
1865 const char *eacontent = "yes";
1867 if (vol->v_vfs_ea == AFPVOL_EA_AUTO) {
1869 if ((vol->v_flags & AFPVOL_RO) == AFPVOL_RO) {
1870 LOG(log_info, logtype_logger, "read-only volume '%s', can't test for EA support, disabling EAs", vol->v_localname);
1871 vol->v_vfs_ea = AFPVOL_EA_NONE;
1877 process_uid = geteuid();
1879 if (seteuid(0) == -1) {
1880 LOG(log_error, logtype_logger, "check_ea_sys_support: can't seteuid(0): %s", strerror(errno));
1884 if ((sys_setxattr(vol->v_path, eaname, eacontent, 4, 0)) == 0) {
1885 sys_removexattr(vol->v_path, eaname);
1886 vol->v_vfs_ea = AFPVOL_EA_SYS;
1888 LOG(log_warning, logtype_afpd, "volume \"%s\" does not support Extended Attributes, using ea:ad instead",
1890 vol->v_vfs_ea = AFPVOL_EA_AD;
1894 if (seteuid(process_uid) == -1) {
1895 LOG(log_error, logtype_logger, "can't seteuid back %s", strerror(errno));
1902 /* -------------------------
1903 * we are the user here
1905 int afp_openvol(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
1916 char path[ MAXPATHLEN + 1];
1922 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1923 bitmap = ntohs( bitmap );
1924 ibuf += sizeof( bitmap );
1927 if (( bitmap & (1<<VOLPBIT_VID)) == 0 ) {
1928 return AFPERR_BITMAP;
1931 len = (unsigned char)*ibuf++;
1932 volname = obj->oldtmp;
1934 if ((volname_tmp = strchr(volname,'+')) != NULL)
1935 volname = volname_tmp+1;
1937 if (utf8_encoding()) {
1938 namelen = convert_string(CH_UTF8_MAC, CH_UCS2, ibuf, len, volname, sizeof(obj->oldtmp));
1940 namelen = convert_string(obj->options.maccharset, CH_UCS2, ibuf, len, volname, sizeof(obj->oldtmp));
1943 if ( namelen <= 0) {
1944 return AFPERR_PARAM;
1948 if ((len + 1) & 1) /* pad to an even boundary */
1953 for ( volume = Volumes; volume; volume = volume->v_next ) {
1954 if ( strcasecmp_w( (ucs2_t*) volname, volume->v_name ) == 0 ) {
1959 if ( volume == NULL ) {
1960 return AFPERR_PARAM;
1963 /* check for a volume password */
1964 if (volume->v_password && strncmp(ibuf, volume->v_password, VOLPASSLEN)) {
1965 return AFPERR_ACCESS;
1968 if (( volume->v_flags & AFPVOL_OPEN ) ) {
1969 /* the volume is already open */
1971 set_uidgid ( volume );
1973 return stat_vol(bitmap, volume, rbuf, rbuflen);
1976 if (volume->v_root_preexec) {
1977 if ((ret = afprun(1, volume->v_root_preexec, NULL)) && volume->v_root_preexec_close) {
1978 LOG(log_error, logtype_afpd, "afp_openvol(%s): root preexec : %d", volume->v_path, ret );
1984 set_uidgid ( volume );
1987 if (volume->v_preexec) {
1988 if ((ret = afprun(0, volume->v_preexec, NULL)) && volume->v_preexec_close) {
1989 LOG(log_error, logtype_afpd, "afp_openvol(%s): preexec : %d", volume->v_path, ret );
1994 if ( stat( volume->v_path, &st ) < 0 ) {
1995 return AFPERR_PARAM;
1998 if ( chdir( volume->v_path ) < 0 ) {
1999 return AFPERR_PARAM;
2002 if ( NULL == getcwd(path, MAXPATHLEN)) {
2003 /* shouldn't be fatal but it will fail later */
2004 LOG(log_error, logtype_afpd, "afp_openvol(%s): volume pathlen too long", volume->v_path);
2008 /* Normalize volume path */
2009 #ifdef REALPATH_TAKES_NULL
2010 if ((volume->v_path = realpath(path, NULL)) == NULL)
2013 if ((volume->v_path = malloc(MAXPATHLEN+1)) == NULL)
2015 if (realpath(path, volume->v_path) == NULL) {
2016 free(volume->v_path);
2019 /* Safe some memory */
2021 if ((tmp = strdup(volume->v_path)) == NULL) {
2022 free(volume->v_path);
2025 free(volume->v_path);
2026 volume->v_path = tmp;
2029 if (volume_codepage(obj, volume) < 0) {
2034 /* initialize volume variables
2037 if (utf8_encoding()) {
2038 volume->max_filename = 255;
2041 volume->max_filename = MACFILELEN;
2044 volume->v_dir = volume->v_root = NULL;
2045 volume->v_hash = NULL;
2047 volume->v_flags |= AFPVOL_OPEN;
2048 volume->v_cdb = NULL;
2050 if (utf8_encoding()) {
2051 len = convert_string_allocate(CH_UCS2, CH_UTF8_MAC, volume->v_u8mname, namelen, &vol_mname);
2053 len = convert_string_allocate(CH_UCS2, obj->options.maccharset, volume->v_macname, namelen, &vol_mname);
2055 if ( !vol_mname || len <= 0) {
2060 if ((vol_uname = strrchr(path, '/')) == NULL)
2062 else if (*(vol_uname + 1) != '\0')
2065 if ((dir = dirnew(vol_mname, vol_uname) ) == NULL) {
2067 LOG(log_error, logtype_afpd, "afp_openvol(%s): malloc: %s", volume->v_path, strerror(errno) );
2073 dir->d_did = DIRDID_ROOT;
2074 dir->d_color = DIRTREE_COLOR_BLACK; /* root node is black */
2075 dir->d_m_name_ucs2 = strdup_w(volume->v_name);
2076 volume->v_dir = volume->v_root = dir;
2077 volume->v_curdir = NULL;
2078 volume->v_hash = dirhash();
2080 curdir = volume->v_dir;
2081 if (volume_openDB(volume) < 0) {
2082 LOG(log_error, logtype_afpd, "Fatal error: cannot open CNID or invalid CNID backend for %s: %s",
2083 volume->v_path, volume->v_cnidscheme);
2088 ret = stat_vol(bitmap, volume, rbuf, rbuflen);
2089 if (ret == AFP_OK) {
2091 if (!(volume->v_flags & AFPVOL_RO)) {
2092 handle_special_folders( volume );
2094 volume->v_cnidserver ? volume->v_cnidserver : Cnid_srv,
2095 volume->v_cnidport ? volume->v_cnidport : Cnid_port);
2099 * If you mount a volume twice, the second time the trash appears on
2100 * the desk-top. That's because the Mac remembers the DID for the
2101 * trash (even for volumes in different zones, on different servers).
2102 * Just so this works better, we prime the DID cache with the trash,
2103 * fixing the trash at DID 17.
2104 * FIXME (RL): should it be done inside a CNID backend ? (always returning Trash DID when asked) ?
2106 if ((volume->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
2108 /* FIXME find db time stamp */
2109 if (cnid_getstamp(volume->v_cdb, volume->v_stamp, sizeof(volume->v_stamp)) < 0) {
2110 LOG (log_error, logtype_afpd,
2111 "afp_openvol(%s): Fatal error: Unable to get stamp value from CNID backend",
2119 cname( volume, volume->v_dir, &p );
2125 if (volume->v_dir) {
2126 hash_free( volume->v_hash);
2127 dirfree( volume->v_dir );
2128 volume->v_dir = volume->v_root = NULL;
2131 volume->v_flags &= ~AFPVOL_OPEN;
2132 if (volume->v_cdb != NULL) {
2133 cnid_close(volume->v_cdb);
2134 volume->v_cdb = NULL;
2140 /* ------------------------- */
2141 static void closevol(struct vol *vol)
2146 hash_free( vol->v_hash);
2147 dirfree( vol->v_root );
2149 if (vol->v_cdb != NULL) {
2150 cnid_close(vol->v_cdb);
2154 if (vol->v_postexec) {
2155 afprun(0, vol->v_postexec, NULL);
2157 if (vol->v_root_postexec) {
2158 afprun(1, vol->v_root_postexec, NULL);
2162 /* ------------------------- */
2163 void close_all_vol(void)
2167 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
2168 if ( (ovol->v_flags & AFPVOL_OPEN) ) {
2169 ovol->v_flags &= ~AFPVOL_OPEN;
2175 /* ------------------------- */
2176 static void deletevol(struct vol *vol)
2180 vol->v_flags &= ~AFPVOL_OPEN;
2181 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
2182 if ( (ovol->v_flags & AFPVOL_OPEN) ) {
2186 if ( ovol != NULL ) {
2187 /* Even if chdir fails, we can't say afp_closevol fails. */
2188 if ( chdir( ovol->v_path ) == 0 ) {
2189 curdir = ovol->v_dir;
2194 if (vol->v_deleted) {
2195 showvol(vol->v_name);
2202 /* ------------------------- */
2203 int afp_closevol(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2210 memcpy(&vid, ibuf, sizeof( vid ));
2211 if (NULL == ( vol = getvolbyvid( vid )) ) {
2212 return( AFPERR_PARAM );
2220 /* ------------------------- */
2221 struct vol *getvolbyvid(const u_int16_t vid )
2225 for ( vol = Volumes; vol; vol = vol->v_next ) {
2226 if ( vid == vol->v_vid ) {
2230 if ( vol == NULL || ( vol->v_flags & AFPVOL_OPEN ) == 0 ) {
2236 #endif /* FORCE_UIDGID */
2241 /* ------------------------ */
2242 static int ext_cmp_key(const void *key, const void *obj)
2244 const char *p = key;
2245 const struct extmap *em = obj;
2246 return strdiacasecmp(p, em->em_ext);
2248 struct extmap *getextmap(const char *path)
2253 if (!Extmap_cnt || NULL == ( p = strrchr( path, '.' )) ) {
2254 return( Defextmap );
2258 return( Defextmap );
2260 em = bsearch(p, Extmap, Extmap_cnt, sizeof(struct extmap), ext_cmp_key);
2264 return( Defextmap );
2268 /* ------------------------- */
2269 struct extmap *getdefextmap(void)
2271 return( Defextmap );
2274 /* --------------------------
2275 poll if a volume is changed by other processes.
2277 0 no attention msg sent
2278 1 attention msg sent
2279 -1 error (socket closed)
2281 Note: if attention return -1 no packet has been
2282 sent because the buffer is full, we don't care
2283 either there's no reader or there's a lot of
2284 traffic and another pollvoltime will follow
2286 int pollvoltime(AFPObj *obj)
2293 if (!(afp_version > 21 && obj->options.server_notif))
2296 if ( gettimeofday( &tv, NULL ) < 0 )
2299 for ( vol = Volumes; vol; vol = vol->v_next ) {
2300 if ( (vol->v_flags & AFPVOL_OPEN) && vol->v_mtime + 30 < tv.tv_sec) {
2301 if ( !stat( vol->v_path, &st ) && vol->v_mtime != st.st_mtime ) {
2302 vol->v_mtime = st.st_mtime;
2303 if (!obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED))
2312 /* ------------------------- */
2313 void setvoltime(AFPObj *obj, struct vol *vol)
2317 /* just looking at vol->v_mtime is broken seriously since updates
2318 * from other users afpd processes never are seen.
2319 * This is not the most elegant solution (a shared memory between
2320 * the afpd processes would come closer)
2323 if ( gettimeofday( &tv, NULL ) < 0 ) {
2324 LOG(log_error, logtype_afpd, "setvoltime(%s): gettimeofday: %s", vol->v_path, strerror(errno) );
2327 if( utime( vol->v_path, NULL ) < 0 ) {
2328 /* write of time failed ... probably a read only filesys,
2329 * where no other users can interfere, so there's no issue here
2333 /* a little granularity */
2334 if (vol->v_mtime < tv.tv_sec) {
2335 vol->v_mtime = tv.tv_sec;
2336 /* or finder doesn't update free space
2337 * AFP 3.2 and above clients seem to be ok without so many notification
2339 if (afp_version < 32 && obj->options.server_notif) {
2340 obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED);
2345 /* ------------------------- */
2346 int afp_getvolparams(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_,char *rbuf, size_t *rbuflen)
2349 u_int16_t vid, bitmap;
2352 memcpy(&vid, ibuf, sizeof( vid ));
2353 ibuf += sizeof( vid );
2354 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2355 bitmap = ntohs( bitmap );
2357 if (NULL == ( vol = getvolbyvid( vid )) ) {
2359 return( AFPERR_PARAM );
2362 return stat_vol(bitmap, vol, rbuf, rbuflen);
2365 /* ------------------------- */
2366 int afp_setvolparams(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2370 u_int16_t vid, bitmap;
2376 memcpy(&vid, ibuf, sizeof( vid ));
2377 ibuf += sizeof( vid );
2378 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2379 bitmap = ntohs( bitmap );
2380 ibuf += sizeof(bitmap);
2382 if (( vol = getvolbyvid( vid )) == NULL ) {
2383 return( AFPERR_PARAM );
2386 if ((vol->v_flags & AFPVOL_RO))
2387 return AFPERR_VLOCK;
2389 /* we can only set the backup date. */
2390 if (bitmap != (1 << VOLPBIT_BDATE))
2391 return AFPERR_BITMAP;
2393 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2394 if ( ad_open( vol->v_path, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR,
2397 return AFPERR_VLOCK;
2399 return AFPERR_ACCESS;
2402 memcpy(&aint, ibuf, sizeof(aint));
2403 ad_setdate(&ad, AD_DATE_BACKUP, aint);
2405 ad_close(&ad, ADFLAGS_HF);
2409 /* ------------------------- */
2410 int wincheck(const struct vol *vol, const char *path)
2414 if (!(vol->v_flags & AFPVOL_MSWINDOWS))
2417 /* empty paths are not allowed */
2418 if ((len = strlen(path)) == 0)
2421 /* leading or trailing whitespaces are not allowed, carriage returns
2422 * and probably other whitespace is okay, tabs are not allowed
2424 if ((path[0] == ' ') || (path[len-1] == ' '))
2427 /* certain characters are not allowed */
2428 if (strpbrk(path, MSWINDOWS_BADCHARS))
2431 /* everything else is okay */
2437 * precreate a folder
2438 * this is only intended for folders in the volume root
2439 * It will *not* work if the folder name contains extended characters
2441 static int create_special_folder (const struct vol *vol, const struct _special_folder *folder)
2450 p = (char *) malloc ( strlen(vol->v_path)+strlen(folder->name)+2);
2452 LOG(log_error, logtype_afpd,"malloc failed");
2456 q=strdup(folder->name);
2458 LOG(log_error, logtype_afpd,"malloc failed");
2462 strcpy(p, vol->v_path);
2467 if ((vol->v_casefold & AFPVOL_MTOUUPPER))
2469 else if ((vol->v_casefold & AFPVOL_MTOULOWER))
2475 if ( (ret = stat( p, &st )) < 0 ) {
2476 if (folder->precreate) {
2477 if (ad_mkdir(p, folder->mode)) {
2478 LOG(log_debug, logtype_afpd,"Creating '%s' failed in %s: %s", p, vol->v_path, strerror(errno));
2487 if ( !ret && folder->hide) {
2489 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2490 if (ad_open_metadata( p, ADFLAGS_DIR, O_CREAT, &ad) < 0) {
2496 ad_setname(&ad, folder->name);
2498 ad_getattr(&ad, &attr);
2499 attr |= htons( ntohs( attr ) | ATTRBIT_INVISIBLE );
2500 ad_setattr(&ad, attr);
2502 /* do the same with the finder info */
2503 if (ad_entry(&ad, ADEID_FINDERI)) {
2504 memcpy(&attr, ad_entry(&ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF, sizeof(attr));
2505 attr |= htons(FINDERINFO_INVISIBLE);
2506 memcpy(ad_entry(&ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF,&attr, sizeof(attr));
2510 ad_close_metadata( &ad);
2517 static void handle_special_folders (const struct vol * vol)
2519 const _special_folder *p = &special_folders[0];
2521 if ((vol->v_flags & AFPVOL_RO))
2524 for (; p->name != NULL; p++) {
2525 create_special_folder (vol, p);