2 * $Id: volume.c,v 1.108 2009-12-01 16:36:14 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>
49 #include <atalk/cnid.h>
53 #include "directory.h"
61 extern int afprun(int root, char *cmd, int *outfd);
64 #define MIN(a, b) ((a) < (b) ? (a) : (b))
67 #ifndef NO_LARGE_VOL_SUPPORT
68 #if BYTE_ORDER == BIG_ENDIAN
71 #else /* BYTE_ORDER == BIG_ENDIAN */
72 #define hton64(x) ((u_int64_t) (htonl(((x) >> 32) & 0xffffffffLL)) | \
73 (u_int64_t) ((htonl(x) & 0xffffffffLL) << 32))
74 #define ntoh64(x) (hton64(x))
75 #endif /* BYTE_ORDER == BIG_ENDIAN */
76 #endif /* ! NO_LARGE_VOL_SUPPORT */
78 static struct vol *Volumes = NULL;
79 static u_int16_t lastvid = 0;
80 static char *Trash = "\02\024Network Trash Folder";
82 static struct extmap *Extmap = NULL, *Defextmap = NULL;
83 static int Extmap_cnt;
84 static void free_extmap(void);
86 #define VOLOPT_ALLOW 0 /* user allow list */
87 #define VOLOPT_DENY 1 /* user deny list */
88 #define VOLOPT_RWLIST 2 /* user rw list */
89 #define VOLOPT_ROLIST 3 /* user ro list */
90 #define VOLOPT_PASSWORD 4 /* volume password */
91 #define VOLOPT_CASEFOLD 5 /* character case mangling */
92 #define VOLOPT_FLAGS 6 /* various flags */
93 #define VOLOPT_DBPATH 7 /* path to database */
94 #define VOLOPT_MAPCHARS 8 /* does mtou and utom mappings. syntax:
95 m and u can be double-byte hex
100 !u -> make u illegal always
101 ~u -> make u illegal only as the first
102 part of a double-byte character.
104 #define VOLOPT_VETO 10 /* list of veto filespec */
105 #define VOLOPT_PREEXEC 11 /* preexec command */
106 #define VOLOPT_ROOTPREEXEC 12 /* root preexec command */
108 #define VOLOPT_POSTEXEC 13 /* postexec command */
109 #define VOLOPT_ROOTPOSTEXEC 14 /* root postexec command */
111 #define VOLOPT_ENCODING 15 /* mac encoding (pre OSX)*/
112 #define VOLOPT_MACCHARSET 16
113 #define VOLOPT_CNIDSCHEME 17
114 #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 */
131 #define VOLOPT_MAX 28 /* <== IMPORTANT !!!!!! */
132 #define VOLOPT_NUM (VOLOPT_MAX + 1)
135 #define VOLOPT_DEFAULT ":DEFAULT:"
136 #define VOLOPT_DEFAULT_LEN 9
143 typedef struct _special_folder {
150 static const _special_folder special_folders[] = {
151 {"Network Trash Folder", 1, 0777, 1},
152 {"Temporary Items", 1, 0777, 1},
153 {".AppleDesktop", 1, 0777, 0},
155 {"TheFindByContentFolder", 0, 0, 1},
156 {"TheVolumeSettingsFolder", 0, 0, 1},
160 static void handle_special_folders (const struct vol *);
161 static void deletevol(struct vol *vol);
162 static void volume_free(struct vol *vol);
164 static void volfree(struct vol_option *options,
165 const struct vol_option *save)
170 for (i = 0; i < VOLOPT_MAX; i++) {
171 if (options[i].c_value && (options[i].c_value != save[i].c_value))
172 free(options[i].c_value);
175 for (i = 0; i < VOLOPT_MAX; i++) {
176 if (options[i].c_value)
177 free(options[i].c_value);
183 /* handle variable substitutions. here's what we understand:
184 * $b -> basename of path
185 * $c -> client ip/appletalk address
186 * $d -> volume pathname on server
187 * $f -> full name (whatever's in the gecos field)
190 * $i -> client ip/appletalk address without port
191 * $s -> server name (hostname if it doesn't exist)
192 * $u -> username (guest is usually nobody)
193 * $v -> volume name or basename if null
194 * $z -> zone (may not exist)
199 #define is_var(a, b) (strncmp((a), (b), 2) == 0)
201 static char *volxlate(AFPObj *obj, char *dest, size_t destlen,
202 char *src, struct passwd *pwd, char *path, char *volname)
213 dest = calloc(destlen +1, 1);
219 strlcpy(dest, src, destlen +1);
220 if ((p = strchr(src, '$')) == NULL) /* nothing to do */
223 /* first part of the path. just forward to the next variable. */
224 len = MIN((size_t)(p - src), destlen);
230 while (p && destlen > 0) {
231 /* now figure out what the variable is */
233 if (is_var(p, "$b")) {
235 if ((q = strrchr(path, '/')) == NULL)
237 else if (*(q + 1) != '\0')
240 } else if (is_var(p, "$c")) {
241 if (obj->proto == AFPPROTO_ASP) {
242 ASP asp = obj->handle;
244 len = sprintf(dest, "%u.%u", ntohs(asp->asp_sat.sat_addr.s_net),
245 asp->asp_sat.sat_addr.s_node);
249 } else if (obj->proto == AFPPROTO_DSI) {
250 DSI *dsi = obj->handle;
251 len = sprintf(dest, "%s:%u",
252 getip_string((struct sockaddr *)&dsi->client),
253 getip_port((struct sockaddr *)&dsi->client));
257 } else if (is_var(p, "$d")) {
259 } else if (is_var(p, "$f")) {
260 if ((r = strchr(pwd->pw_gecos, ',')))
263 } else if (is_var(p, "$g")) {
264 struct group *grp = getgrgid(pwd->pw_gid);
267 } else if (is_var(p, "$h")) {
268 q = obj->options.hostname;
269 } else if (is_var(p, "$i")) {
270 if (obj->proto == AFPPROTO_ASP) {
271 ASP asp = obj->handle;
273 len = sprintf(dest, "%u", ntohs(asp->asp_sat.sat_addr.s_net));
277 } else if (obj->proto == AFPPROTO_DSI) {
278 DSI *dsi = obj->handle;
279 q = getip_string((struct sockaddr *)&dsi->client);
281 } else if (is_var(p, "$s")) {
284 else if (obj->options.server) {
285 q = obj->options.server;
287 q = obj->options.hostname;
288 } else if (is_var(p, "$u")) {
290 if ( obj->options.ntseparator && (sep = strchr(obj->username, obj->options.ntseparator[0])) != NULL)
294 } else if (is_var(p, "$v")) {
299 if ((q = strrchr(path, '/')) == NULL)
301 else if (*(q + 1) != '\0')
304 } else if (is_var(p, "$z")) {
306 } else if (is_var(p, "$$")) {
311 /* copy the stuff over. if we don't understand something that we
312 * should, just skip it over. */
314 len = MIN(p == q ? 2 : strlen(q), destlen);
315 strncpy(dest, q, len);
320 /* stuff up to next $ */
322 p = strchr(src, '$');
323 len = p ? MIN((size_t)(p - src), destlen) : destlen;
325 strncpy(dest, src, len);
333 /* to make sure that val is valid, make sure to select an opt that
335 static int optionok(const char *buf, const char *opt, const char *val)
337 if (!strstr(buf,opt))
345 /* -------------------- */
346 static void setoption(struct vol_option *options, struct vol_option *save, int opt, const char *val)
348 if (options[opt].c_value && (!save || options[opt].c_value != save[opt].c_value))
349 free(options[opt].c_value);
350 options[opt].c_value = strdup(val + 1);
353 /* ------------------------------------------
354 handle all the options. tmp can't be NULL. */
355 static void volset(struct vol_option *options, struct vol_option *save,
356 char *volname, int vlen,
361 val = strchr(tmp, ':');
363 /* we'll assume it's a volume name. */
364 strncpy(volname, tmp, vlen);
369 LOG(log_debug, logtype_afpd, "Parsing volset %s", val);
371 if (optionok(tmp, "allow:", val)) {
372 setoption(options, save, VOLOPT_ALLOW, val);
374 } else if (optionok(tmp, "deny:", val)) {
375 setoption(options, save, VOLOPT_DENY, val);
377 } else if (optionok(tmp, "rwlist:", val)) {
378 setoption(options, save, VOLOPT_RWLIST, val);
380 } else if (optionok(tmp, "rolist:", val)) {
381 setoption(options, save, VOLOPT_ROLIST, val);
383 } else if (optionok(tmp, "codepage:", val)) {
384 LOG (log_error, logtype_afpd, "The old codepage system has been removed. Please make sure to read the documentation !!!!");
385 /* Make sure we don't screw anything */
387 } else if (optionok(tmp, "volcharset:", val)) {
388 setoption(options, save, VOLOPT_ENCODING, val);
389 } else if (optionok(tmp, "maccharset:", val)) {
390 setoption(options, save, VOLOPT_MACCHARSET, val);
391 } else if (optionok(tmp, "veto:", val)) {
392 setoption(options, save, VOLOPT_VETO, val);
393 } else if (optionok(tmp, "cnidscheme:", val)) {
394 setoption(options, save, VOLOPT_CNIDSCHEME, val);
395 } else if (optionok(tmp, "casefold:", val)) {
396 if (strcasecmp(val + 1, "tolower") == 0)
397 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMLOWER;
398 else if (strcasecmp(val + 1, "toupper") == 0)
399 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMUPPER;
400 else if (strcasecmp(val + 1, "xlatelower") == 0)
401 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UUPPERMLOWER;
402 else if (strcasecmp(val + 1, "xlateupper") == 0)
403 options[VOLOPT_CASEFOLD].i_value = AFPVOL_ULOWERMUPPER;
404 } else if (optionok(tmp, "adouble:", val)) {
405 if (strcasecmp(val + 1, "v1") == 0)
406 options[VOLOPT_ADOUBLE].i_value = AD_VERSION1;
407 #if AD_VERSION == AD_VERSION2
408 else if (strcasecmp(val + 1, "v2") == 0)
409 options[VOLOPT_ADOUBLE].i_value = AD_VERSION2;
410 else if (strcasecmp(val + 1, "osx") == 0)
411 options[VOLOPT_ADOUBLE].i_value = AD_VERSION2_OSX;
412 else if (strcasecmp(val + 1, "sfm") == 0)
413 options[VOLOPT_ADOUBLE].i_value = AD_VERSION1_SFM;
415 } else if (optionok(tmp, "options:", val)) {
418 if ((p = strtok(val + 1, ",")) == NULL) /* nothing */
422 if (strcasecmp(p, "prodos") == 0)
423 options[VOLOPT_FLAGS].i_value |= AFPVOL_A2VOL;
424 else if (strcasecmp(p, "mswindows") == 0) {
425 options[VOLOPT_FLAGS].i_value |= AFPVOL_MSWINDOWS | AFPVOL_USEDOTS;
426 } else if (strcasecmp(p, "crlf") == 0)
427 options[VOLOPT_FLAGS].i_value |= AFPVOL_CRLF;
428 else if (strcasecmp(p, "noadouble") == 0)
429 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOADOUBLE;
430 else if (strcasecmp(p, "ro") == 0)
431 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
432 else if (strcasecmp(p, "nohex") == 0)
433 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOHEX;
434 else if (strcasecmp(p, "usedots") == 0)
435 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS;
436 else if (strcasecmp(p, "invisibledots") == 0)
437 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS | AFPVOL_INV_DOTS;
438 else if (strcasecmp(p, "limitsize") == 0)
439 options[VOLOPT_FLAGS].i_value |= AFPVOL_LIMITSIZE;
440 /* support for either "dropbox" or "dropkludge" */
441 else if (strcasecmp(p, "dropbox") == 0)
442 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
443 else if (strcasecmp(p, "dropkludge") == 0)
444 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
445 else if (strcasecmp(p, "nofileid") == 0)
446 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOFILEID;
447 else if (strcasecmp(p, "nostat") == 0)
448 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOSTAT;
449 else if (strcasecmp(p, "preexec_close") == 0)
450 options[VOLOPT_PREEXEC].i_value = 1;
451 else if (strcasecmp(p, "root_preexec_close") == 0)
452 options[VOLOPT_ROOTPREEXEC].i_value = 1;
453 else if (strcasecmp(p, "upriv") == 0)
454 options[VOLOPT_FLAGS].i_value |= AFPVOL_UNIX_PRIV;
455 else if (strcasecmp(p, "acls") == 0)
456 options[VOLOPT_FLAGS].i_value |= AFPVOL_ACLS;
457 else if (strcasecmp(p, "nodev") == 0)
458 options[VOLOPT_FLAGS].i_value |= AFPVOL_NODEV;
459 else if (strcasecmp(p, "caseinsensitive") == 0)
460 options[VOLOPT_FLAGS].i_value |= AFPVOL_CASEINSEN;
461 else if (strcasecmp(p, "illegalseq") == 0)
462 options[VOLOPT_FLAGS].i_value |= AFPVOL_EILSEQ;
463 else if (strcasecmp(p, "nocnidcache") == 0)
464 options[VOLOPT_FLAGS].i_value &= ~AFPVOL_CACHE;
465 else if (strcasecmp(p, "tm") == 0)
466 options[VOLOPT_FLAGS].i_value |= AFPVOL_TM;
468 p = strtok(NULL, ",");
471 } else if (optionok(tmp, "dbpath:", val)) {
472 setoption(options, save, VOLOPT_DBPATH, val);
474 } else if (optionok(tmp, "umask:", val)) {
475 options[VOLOPT_UMASK].i_value = (int)strtol(val +1, NULL, 8);
476 } else if (optionok(tmp, "dperm:", val)) {
477 options[VOLOPT_DPERM].i_value = (int)strtol(val+1, NULL, 8);
478 } else if (optionok(tmp, "fperm:", val)) {
479 options[VOLOPT_FPERM].i_value = (int)strtol(val+1, NULL, 8);
480 } else if (optionok(tmp, "perm:", val)) {
481 options[VOLOPT_DFLTPERM].i_value = (int)strtol(val+1, NULL, 8);
482 } else if (optionok(tmp, "mapchars:",val)) {
483 setoption(options, save, VOLOPT_MAPCHARS, val);
485 } else if (optionok(tmp, "password:", val)) {
486 setoption(options, save, VOLOPT_PASSWORD, val);
490 /* this code allows forced uid/gid per volume settings */
491 } else if (optionok(tmp, "forceuid:", val)) {
492 setoption(options, save, VOLOPT_FORCEUID, val);
493 } else if (optionok(tmp, "forcegid:", val)) {
494 setoption(options, save, VOLOPT_FORCEGID, val);
496 #endif /* FORCE_UIDGID */
497 } else if (optionok(tmp, "root_preexec:", val)) {
498 setoption(options, save, VOLOPT_ROOTPREEXEC, val);
500 } else if (optionok(tmp, "preexec:", val)) {
501 setoption(options, save, VOLOPT_PREEXEC, val);
503 } else if (optionok(tmp, "root_postexec:", val)) {
504 setoption(options, save, VOLOPT_ROOTPOSTEXEC, val);
506 } else if (optionok(tmp, "postexec:", val)) {
507 setoption(options, save, VOLOPT_POSTEXEC, val);
509 } else if (optionok(tmp, "allowed_hosts:", val)) {
510 setoption(options, save, VOLOPT_ALLOWED_HOSTS, val);
512 } else if (optionok(tmp, "denied_hosts:", val)) {
513 setoption(options, save, VOLOPT_DENIED_HOSTS, val);
515 } else if (optionok(tmp, "ea:", val)) {
516 if (strcasecmp(val + 1, "ad") == 0) /* the default anyway */
517 options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_AD;
518 else if (strcasecmp(val + 1, "sys") == 0)
519 options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_SYS;
522 /* ignore unknown options */
523 LOG(log_debug, logtype_afpd, "ignoring unknown volume option: %s", tmp);
528 /* ----------------- */
529 static void showvol(const ucs2_t *name)
532 for ( volume = Volumes; volume; volume = volume->v_next ) {
533 if (volume->v_hide && !strcasecmp_w( volume->v_name, name ) ) {
540 /* ------------------------------- */
541 static int creatvol(AFPObj *obj, struct passwd *pwd,
542 char *path, char *name,
543 struct vol_option *options,
544 const int user /* user defined volume */
548 int suffixlen, vlen, tmpvlen, u8mvlen, macvlen;
550 char tmpname[AFPVOL_U8MNAMELEN+1];
551 ucs2_t u8mtmpname[(AFPVOL_U8MNAMELEN+1)*2], mactmpname[(AFPVOL_MACNAMELEN+1)*2];
552 char suffix[6]; /* max is #FFFF */
555 if ( name == NULL || *name == '\0' ) {
556 if ((name = strrchr( path, '/' )) == NULL) {
557 return -1; /* Obviously not a fully qualified path */
560 /* if you wish to share /, you need to specify a name. */
565 /* suffix for mangling use (lastvid + 1) */
566 /* because v_vid has not been decided yet. */
567 suffixlen = sprintf(suffix, "%c%X", MANGLE_CHAR, lastvid + 1 );
569 vlen = strlen( name );
571 /* Unicode Volume Name */
572 /* Firstly convert name from unixcharset to UTF8-MAC */
574 tmpvlen = convert_charset(obj->options.unixcharset, CH_UTF8_MAC, 0, name, vlen, tmpname, AFPVOL_U8MNAMELEN, &flags);
576 strcpy(tmpname, "???");
580 /* Do we have to mangle ? */
581 if ( (flags & CONV_REQMANGLE) || (tmpvlen > obj->options.volnamelen)) {
582 if (tmpvlen + suffixlen > obj->options.volnamelen) {
584 tmpvlen = convert_charset(obj->options.unixcharset, CH_UTF8_MAC, 0, name, vlen, tmpname, obj->options.volnamelen - suffixlen, &flags);
585 tmpname[tmpvlen >= 0 ? tmpvlen : 0] = 0;
587 strcat(tmpname, suffix);
588 tmpvlen = strlen(tmpname);
591 /* Secondly convert name from UTF8-MAC to UCS2 */
592 if ( 0 >= ( u8mvlen = convert_string(CH_UTF8_MAC, CH_UCS2, tmpname, tmpvlen, u8mtmpname, AFPVOL_U8MNAMELEN*2)) )
595 LOG(log_debug, logtype_afpd, "createvol: Volume '%s' -> UTF8-MAC Name: '%s'", name, tmpname);
597 /* Maccharset Volume Name */
598 /* Firsty convert name from unixcharset to maccharset */
600 tmpvlen = convert_charset(obj->options.unixcharset, obj->options.maccharset, 0, name, vlen, tmpname, AFPVOL_U8MNAMELEN, &flags);
602 strcpy(tmpname, "???");
606 /* Do we have to mangle ? */
607 if ( (flags & CONV_REQMANGLE) || (tmpvlen > AFPVOL_MACNAMELEN)) {
608 if (tmpvlen + suffixlen > AFPVOL_MACNAMELEN) {
610 tmpvlen = convert_charset(obj->options.unixcharset, obj->options.maccharset, 0, name, vlen, tmpname, AFPVOL_MACNAMELEN - suffixlen, &flags);
611 tmpname[tmpvlen >= 0 ? tmpvlen : 0] = 0;
613 strcat(tmpname, suffix);
614 tmpvlen = strlen(tmpname);
617 /* Secondly convert name from maccharset to UCS2 */
618 if ( 0 >= ( macvlen = convert_string(obj->options.maccharset, CH_UCS2, tmpname, tmpvlen, mactmpname, AFPVOL_U8MNAMELEN*2)) )
621 LOG(log_debug, logtype_afpd, "createvol: Volume '%s' -> Longname: '%s'", name, tmpname);
623 /* check duplicate */
624 for ( volume = Volumes; volume; volume = volume->v_next ) {
625 if (( strcasecmp_w( volume->v_u8mname, u8mtmpname ) == 0 ) || ( strcasecmp_w( volume->v_macname, mactmpname ) == 0 )){
626 if (volume->v_deleted) {
627 volume->v_new = hide = 1;
630 return -1; /* Won't be able to access it, anyway... */
635 if (!( volume = (struct vol *)calloc(1, sizeof( struct vol ))) ) {
636 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
639 if ( NULL == ( volume->v_localname = strdup(name))) {
640 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
645 if ( NULL == ( volume->v_u8mname = strdup_w(u8mtmpname))) {
646 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
651 if ( NULL == ( volume->v_macname = strdup_w(mactmpname))) {
652 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
657 if (!( volume->v_path = (char *)malloc( strlen( path ) + 1 )) ) {
658 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
664 volume->v_name = utf8_encoding()?volume->v_u8mname:volume->v_macname;
665 volume->v_hide = hide;
666 strcpy( volume->v_path, path );
670 #endif /* __svr4__ */
671 /* os X start at 1 and use network order ie. 1 2 3 */
672 volume->v_vid = ++lastvid;
673 volume->v_vid = htons(volume->v_vid);
674 volume->v_vfs_ea = AFPVOL_EA_AD;
678 /* should we casefold? */
679 volume->v_casefold = options[VOLOPT_CASEFOLD].i_value;
681 /* shift in some flags */
682 volume->v_flags = options[VOLOPT_FLAGS].i_value;
684 if (options[VOLOPT_EA_VFS].i_value != AFPVOL_EA_AD)
685 volume->v_vfs_ea = options[VOLOPT_EA_VFS].i_value;
687 volume->v_ad_options = 0;
688 if ((volume->v_flags & AFPVOL_NODEV))
689 volume->v_ad_options |= ADVOL_NODEV;
690 if ((volume->v_flags & AFPVOL_CACHE))
691 volume->v_ad_options |= ADVOL_CACHE;
692 if ((volume->v_flags & AFPVOL_UNIX_PRIV))
693 volume->v_ad_options |= ADVOL_UNIXPRIV;
694 if ((volume->v_flags & AFPVOL_INV_DOTS))
695 volume->v_ad_options |= ADVOL_INVDOTS;
697 if (options[VOLOPT_PASSWORD].c_value)
698 volume->v_password = strdup(options[VOLOPT_PASSWORD].c_value);
700 if (options[VOLOPT_VETO].c_value)
701 volume->v_veto = strdup(options[VOLOPT_VETO].c_value);
703 if (options[VOLOPT_ENCODING].c_value)
704 volume->v_volcodepage = strdup(options[VOLOPT_ENCODING].c_value);
706 if (options[VOLOPT_MACCHARSET].c_value)
707 volume->v_maccodepage = strdup(options[VOLOPT_MACCHARSET].c_value);
709 if (options[VOLOPT_DBPATH].c_value)
710 volume->v_dbpath = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_DBPATH].c_value, pwd, path, name);
712 if (options[VOLOPT_CNIDSCHEME].c_value)
713 volume->v_cnidscheme = strdup(options[VOLOPT_CNIDSCHEME].c_value);
715 if (options[VOLOPT_UMASK].i_value)
716 volume->v_umask = (mode_t)options[VOLOPT_UMASK].i_value;
718 if (options[VOLOPT_DPERM].i_value)
719 volume->v_dperm = (mode_t)options[VOLOPT_DPERM].i_value;
721 if (options[VOLOPT_FPERM].i_value)
722 volume->v_fperm = (mode_t)options[VOLOPT_FPERM].i_value;
724 if (options[VOLOPT_DFLTPERM].i_value)
725 volume->v_perm = (mode_t)options[VOLOPT_DFLTPERM].i_value;
727 if (options[VOLOPT_ADOUBLE].i_value)
728 volume->v_adouble = options[VOLOPT_ADOUBLE].i_value;
730 volume->v_adouble = AD_VERSION;
732 /* Mac to Unix conversion flags*/
733 volume->v_mtou_flags = 0;
734 if (!(volume->v_flags & AFPVOL_NOHEX))
735 volume->v_mtou_flags |= CONV_ESCAPEHEX;
736 if (!(volume->v_flags & AFPVOL_USEDOTS))
737 volume->v_mtou_flags |= CONV_ESCAPEDOTS;
738 if ((volume->v_flags & AFPVOL_EILSEQ))
739 volume->v_mtou_flags |= CONV__EILSEQ;
741 if ((volume->v_casefold & AFPVOL_MTOUUPPER))
742 volume->v_mtou_flags |= CONV_TOUPPER;
743 else if ((volume->v_casefold & AFPVOL_MTOULOWER))
744 volume->v_mtou_flags |= CONV_TOLOWER;
746 /* Unix to Mac conversion flags*/
747 volume->v_utom_flags = CONV_IGNORE | CONV_UNESCAPEHEX;
748 if ((volume->v_casefold & AFPVOL_UTOMUPPER))
749 volume->v_utom_flags |= CONV_TOUPPER;
750 else if ((volume->v_casefold & AFPVOL_UTOMLOWER))
751 volume->v_utom_flags |= CONV_TOLOWER;
753 if ((volume->v_flags & AFPVOL_EILSEQ))
754 volume->v_utom_flags |= CONV__EILSEQ;
759 if (options[VOLOPT_FORCEUID].c_value) {
760 volume->v_forceuid = strdup(options[VOLOPT_FORCEUID].c_value);
762 volume->v_forceuid = NULL; /* set as null so as to return 0 later on */
765 if (options[VOLOPT_FORCEGID].c_value) {
766 volume->v_forcegid = strdup(options[VOLOPT_FORCEGID].c_value);
768 volume->v_forcegid = NULL; /* set as null so as to return 0 later on */
772 if (options[VOLOPT_PREEXEC].c_value)
773 volume->v_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_PREEXEC].c_value, pwd, path, name);
774 volume->v_preexec_close = options[VOLOPT_PREEXEC].i_value;
776 if (options[VOLOPT_POSTEXEC].c_value)
777 volume->v_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_POSTEXEC].c_value, pwd, path, name);
779 if (options[VOLOPT_ROOTPREEXEC].c_value)
780 volume->v_root_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPREEXEC].c_value, pwd, path, name);
781 volume->v_root_preexec_close = options[VOLOPT_ROOTPREEXEC].i_value;
783 if (options[VOLOPT_ROOTPOSTEXEC].c_value)
784 volume->v_root_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPOSTEXEC].c_value, pwd, path, name);
787 volume->v_dperm |= volume->v_perm;
788 volume->v_fperm |= volume->v_perm;
790 volume->v_next = Volumes;
795 /* ---------------- */
796 static char *myfgets( char *buf, int size, FILE *fp)
802 while ((EOF != ( c = getc( fp )) ) && ( size > 1 )) {
803 if ( c == '\n' || c == '\r' ) {
804 if (p != buf && *(p -1) == '\\') {
826 /* check access list. this function wants something of the following
828 * @group,name,name2,@group2,name3
830 * a NULL argument allows everybody to have access.
831 * we return three things:
833 * 0: list exists, but name isn't in it
837 #ifndef NO_REAL_USER_NAME
838 /* authentication is case insensitive
839 * FIXME should we do the same with group name?
841 #define access_strcmp strcasecmp
844 #define access_strcmp strcmp
848 static int accessvol(const char *args, const char *name)
850 char buf[MAXPATHLEN + 1], *p;
856 strlcpy(buf, args, sizeof(buf));
857 if ((p = strtok(buf, ",")) == NULL) /* nothing, return okay */
861 if (*p == '@') { /* it's a group */
862 if ((gr = getgrnam(p + 1)) && gmem(gr->gr_gid))
864 } else if (access_strcmp(p, name) == 0) /* it's a user name */
866 p = strtok(NULL, ",");
872 static int hostaccessvol(int type, const char *volname, const char *args, const AFPObj *obj)
875 char buf[MAXPATHLEN + 1], *p, *b;
876 DSI *dsi = obj->handle;
877 struct sockaddr_storage client;
882 strlcpy(buf, args, sizeof(buf));
883 if ((p = strtok_r(buf, ",", &b)) == NULL) /* nothing, return okay */
886 if (obj->proto != AFPPROTO_DSI)
891 char *ipaddr, *mask_char;
892 struct addrinfo hints, *ai;
894 ipaddr = strtok(p, "/");
895 mask_char = strtok(NULL,"/");
897 /* Get address from string with getaddrinfo */
898 memset(&hints, 0, sizeof hints);
899 hints.ai_family = AF_UNSPEC;
900 hints.ai_socktype = SOCK_STREAM;
901 if ((ret = getaddrinfo(ipaddr, NULL, &hints, &ai)) != 0) {
902 LOG(log_error, logtype_afpd, "hostaccessvol: getaddrinfo: %s\n", gai_strerror(ret));
907 if (mask_char != NULL)
908 mask_int = atoi(mask_char); /* apply_ip_mask does range checking on it */
910 if (ai->ai_family == AF_INET) /* IPv4 */
916 /* Apply mask to addresses */
917 client = dsi->client;
918 apply_ip_mask((struct sockaddr *)&client, mask_int);
919 apply_ip_mask(ai->ai_addr, mask_int);
921 if (compare_ip((struct sockaddr *)&client, ai->ai_addr) == 0) {
922 if (type == VOLOPT_DENIED_HOSTS)
923 LOG(log_info, logtype_afpd, "AFP access denied for client IP '%s' to volume '%s' by denied list",
924 getip_string((struct sockaddr *)&client), volname);
931 p = strtok_r(NULL, ",", &b);
934 if (type == VOLOPT_ALLOWED_HOSTS)
935 LOG(log_info, logtype_afpd, "AFP access denied for client IP '%s' to volume '%s', not in allowed list",
936 getip_string((struct sockaddr *)&dsi->client), volname);
940 static void setextmap(char *ext, char *type, char *creator, int user)
945 if (Extmap == NULL) {
946 if (( Extmap = calloc(1, sizeof( struct extmap ))) == NULL ) {
947 LOG(log_error, logtype_afpd, "setextmap: calloc: %s", strerror(errno) );
952 for ( em = Extmap, cnt = 0; em->em_ext; em++, cnt++) {
953 if ( (strdiacasecmp( em->em_ext, ext )) == 0 ) {
958 if ( em->em_ext == NULL ) {
959 if (!(Extmap = realloc( Extmap, sizeof( struct extmap ) * (cnt +2))) ) {
960 LOG(log_error, logtype_afpd, "setextmap: realloc: %s", strerror(errno) );
963 (Extmap +cnt +1)->em_ext = NULL;
965 } else if ( !user ) {
971 if (!(em->em_ext = strdup( ext))) {
972 LOG(log_error, logtype_afpd, "setextmap: strdup: %s", strerror(errno) );
976 if ( *type == '\0' ) {
977 memcpy(em->em_type, "????", sizeof( em->em_type ));
979 memcpy(em->em_type, type, sizeof( em->em_type ));
981 if ( *creator == '\0' ) {
982 memcpy(em->em_creator, "UNIX", sizeof( em->em_creator ));
984 memcpy(em->em_creator, creator, sizeof( em->em_creator ));
988 /* -------------------------- */
989 static int extmap_cmp(const void *map1, const void *map2)
991 const struct extmap *em1 = map1;
992 const struct extmap *em2 = map2;
993 return strdiacasecmp(em1->em_ext, em2->em_ext);
996 static void sortextmap( void)
1001 if ((em = Extmap) == NULL) {
1004 while (em->em_ext) {
1009 qsort(Extmap, Extmap_cnt, sizeof(struct extmap), extmap_cmp);
1010 if (*Extmap->em_ext == 0) {
1011 /* the first line is really "." the default entry,
1012 * we remove the leading '.' in setextmap
1019 /* ----------------------
1021 static void free_extmap( void)
1026 for ( em = Extmap; em->em_ext; em++) {
1036 /* ----------------------
1038 static int volfile_changed(struct afp_volume_name *p)
1044 name = p->full_name;
1048 if (!stat( name, &st) && st.st_mtime > p->mtime) {
1049 p->mtime = st.st_mtime;
1055 /* ----------------------
1056 * Read a volume configuration file and add the volumes contained within to
1057 * the global volume list. If p2 is non-NULL, the file that is opened is
1060 * Lines that begin with # and blank lines are ignored.
1061 * Volume lines are of the form:
1062 * <unix path> [<volume name>] [allow:<user>,<@group>,...] \
1063 * [codepage:<file>] [casefold:<num>]
1064 * <extension> TYPE [CREATOR]
1066 static int readvolfile(AFPObj *obj, struct afp_volume_name *p1, char *p2, int user, struct passwd *pwent)
1069 char path[ MAXPATHLEN + 1], tmp[ MAXPATHLEN + 1],
1070 volname[ AFPVOL_U8MNAMELEN + 1 ], buf[ BUFSIZ ],
1071 type[ 5 ], creator[ 5 ];
1074 struct vol_option options[VOLOPT_NUM], save_options[VOLOPT_NUM];
1082 strcpy( path, p1->name );
1084 strcat( path, "/" );
1086 if (p1->full_name) {
1087 free(p1->full_name);
1089 p1->full_name = strdup(path);
1092 if (NULL == ( fp = fopen( path, "r" )) ) {
1096 if (fd != -1 && !fstat( fd, &st) ) {
1097 p1->mtime = st.st_mtime;
1100 memset(save_options, 0, sizeof(save_options));
1102 /* Enable some default options for all volumes */
1103 save_options[VOLOPT_FLAGS].i_value |= AFPVOL_CACHE;
1105 while ( myfgets( buf, sizeof( buf ), fp ) != NULL ) {
1106 initline( strlen( buf ), buf );
1107 parseline( sizeof( path ) - 1, path );
1114 /* change the default options for this file */
1115 if (strncmp(path, VOLOPT_DEFAULT, VOLOPT_DEFAULT_LEN) == 0) {
1117 for (i = 0; i < VOLOPT_NUM; i++) {
1118 if (parseline( sizeof( path ) - VOLOPT_DEFAULT_LEN - 1,
1119 path + VOLOPT_DEFAULT_LEN) < 0)
1121 volset(save_options, NULL, tmp, sizeof(tmp) - 1,
1122 path + VOLOPT_DEFAULT_LEN);
1128 if (( p = strchr( path, '/' )) != NULL ) {
1136 if ( u == NULL || *u == '\0' || ( pw = getpwnam( u )) == NULL ) {
1139 strcpy( tmp, pw->pw_dir );
1140 if ( p != NULL && *p != '\0' ) {
1144 /* Tag a user's home directory with their umask. Note, this will
1145 * be overwritten if the user actually specifies a umask: option
1146 * for a '~' volume. */
1147 save_options[VOLOPT_UMASK].i_value = obj->options.save_mask;
1151 /* send path through variable substitution */
1152 if (*path != '~') /* need to copy path to tmp */
1155 pwent = getpwnam(obj->username);
1156 volxlate(obj, path, sizeof(path) - 1, tmp, pwent, NULL, NULL);
1158 /* this is sort of braindead. basically, i want to be
1159 * able to specify things in any order, but i don't want to
1160 * re-write everything.
1162 * currently we have options:
1170 * options:prodos,crlf,noadouble,ro...
1175 * namemask:x,y,!z (not implemented yet)
1177 memcpy(options, save_options, sizeof(options));
1180 /* read in up to VOLOP_NUM possible options */
1181 for (i = 0; i < VOLOPT_NUM; i++) {
1182 if (parseline( sizeof( tmp ) - 1, tmp ) < 0)
1185 volset(options, save_options, volname, sizeof(volname) - 1, tmp);
1188 /* check allow/deny lists:
1189 allow -> either no list (-1), or in list (1)
1190 deny -> either no list (-1), or not in list (0) */
1191 if (accessvol(options[VOLOPT_ALLOW].c_value, obj->username) &&
1192 (accessvol(options[VOLOPT_DENY].c_value, obj->username) < 1) &&
1193 hostaccessvol(VOLOPT_ALLOWED_HOSTS, volname, options[VOLOPT_ALLOWED_HOSTS].c_value, obj) &&
1194 (hostaccessvol(VOLOPT_DENIED_HOSTS, volname, options[VOLOPT_DENIED_HOSTS].c_value, obj) < 1)) {
1196 /* handle read-only behaviour. semantics:
1197 * 1) neither the rolist nor the rwlist exist -> rw
1198 * 2) rolist exists -> ro if user is in it.
1199 * 3) rwlist exists -> ro unless user is in it. */
1200 if (((options[VOLOPT_FLAGS].i_value & AFPVOL_RO) == 0) &&
1201 ((accessvol(options[VOLOPT_ROLIST].c_value,
1202 obj->username) == 1) ||
1203 !accessvol(options[VOLOPT_RWLIST].c_value,
1205 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
1207 /* do variable substitution for volname */
1208 volxlate(obj, tmp, sizeof(tmp) - 1, volname, pwent, path, NULL);
1209 creatvol(obj, pwent, path, tmp, options, p2 != NULL);
1211 volfree(options, save_options);
1215 parseline( sizeof( type ) - 1, type );
1216 parseline( sizeof( creator ) - 1, creator );
1217 setextmap( path, type, creator, user);
1224 volfree(save_options, NULL);
1226 if ( fclose( fp ) != 0 ) {
1227 LOG(log_error, logtype_afpd, "readvolfile: fclose: %s", strerror(errno) );
1233 /* ------------------------------- */
1234 static void volume_free(struct vol *vol)
1236 free(vol->v_localname);
1237 vol->v_localname = NULL;
1238 free(vol->v_u8mname);
1239 vol->v_u8mname = NULL;
1240 free(vol->v_macname);
1241 vol->v_macname = NULL;
1243 free(vol->v_password);
1245 free(vol->v_volcodepage);
1246 free(vol->v_maccodepage);
1247 free(vol->v_cnidscheme);
1248 free(vol->v_dbpath);
1251 free(vol->v_forceuid);
1252 free(vol->v_forcegid);
1253 #endif /* FORCE_UIDGID */
1256 /* ------------------------------- */
1257 static void free_volumes(void )
1260 struct vol *nvol, *ovol;
1262 for ( vol = Volumes; vol; vol = vol->v_next ) {
1263 if (( vol->v_flags & AFPVOL_OPEN ) ) {
1270 for ( vol = Volumes, ovol = NULL; vol; vol = nvol) {
1273 if (vol->v_localname == NULL) {
1274 if (Volumes == vol) {
1279 ovol->v_next = nvol;
1289 /* ------------------------------- */
1290 static void volume_unlink(struct vol *volume)
1292 struct vol *vol, *ovol, *nvol;
1294 if (volume == Volumes) {
1295 Volumes = Volumes->v_next;
1298 for ( vol = Volumes->v_next, ovol = Volumes; vol; vol = nvol) {
1301 if (vol == volume) {
1302 ovol->v_next = nvol;
1311 static int getvolspace(struct vol *vol,
1312 u_int32_t *bfree, u_int32_t *btotal,
1313 VolSpace *xbfree, VolSpace *xbtotal, u_int32_t *bsize)
1317 #ifndef NO_QUOTA_SUPPORT
1318 VolSpace qfree, qtotal;
1321 spaceflag = AFPVOL_GVSMASK & vol->v_flags;
1322 /* report up to 2GB if afp version is < 2.2 (4GB if not) */
1323 maxsize = (vol->v_flags & AFPVOL_A2VOL) ? 0x01fffe00 :
1324 (((afp_version < 22) || (vol->v_flags & AFPVOL_LIMITSIZE))
1325 ? 0x7fffffffL : 0xffffffffL);
1328 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_AFSGVS ) {
1329 if ( afs_getvolspace( vol, xbfree, xbtotal, bsize ) == AFP_OK ) {
1330 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_AFSGVS;
1331 goto getvolspace_done;
1336 if (( rc = ustatfs_getvolspace( vol, xbfree, xbtotal,
1337 bsize)) != AFP_OK ) {
1341 #define min(a,b) ((a)<(b)?(a):(b))
1342 #ifndef NO_QUOTA_SUPPORT
1343 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_UQUOTA ) {
1344 if ( uquota_getvolspace( vol, &qfree, &qtotal, *bsize ) == AFP_OK ) {
1345 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_UQUOTA;
1346 *xbfree = min(*xbfree, qfree);
1347 *xbtotal = min( *xbtotal, qtotal);
1348 goto getvolspace_done;
1352 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_USTATFS;
1355 *bfree = min( *xbfree, maxsize);
1356 *btotal = min( *xbtotal, maxsize);
1360 /* -----------------------
1361 * set volume creation date
1362 * avoid duplicate, well at least it tries
1364 static void vol_setdate(u_int16_t id, struct adouble *adp, time_t date)
1367 struct vol *vol = Volumes;
1369 for ( volume = Volumes; volume; volume = volume->v_next ) {
1370 if (volume->v_vid == id) {
1373 else if ((time_t)(AD_DATE_FROM_UNIX(date)) == volume->v_ctime) {
1375 volume = Volumes; /* restart */
1378 vol->v_ctime = AD_DATE_FROM_UNIX(date);
1379 ad_setdate(adp, AD_DATE_CREATE | AD_DATE_UNIX, date);
1382 /* ----------------------- */
1383 static int getvolparams( u_int16_t bitmap, struct vol *vol, struct stat *st, char *buf, size_t *buflen)
1386 int bit = 0, isad = 1;
1389 u_int32_t bfree, btotal, bsize;
1390 VolSpace xbfree, xbtotal; /* extended bytes */
1391 char *data, *nameoff = NULL;
1394 LOG(log_debug, logtype_afpd, "getvolparams: Volume '%s'", vol->v_localname);
1396 /* courtesy of jallison@whistle.com:
1397 * For MacOS8.x support we need to create the
1398 * .Parent file here if it doesn't exist. */
1400 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1401 if ( ad_open_metadata( vol->v_path, vol_noadouble(vol) | ADFLAGS_DIR, O_CREAT, &ad) < 0 ) {
1403 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1405 } else if (ad_get_MD_flags( &ad ) & O_CREAT) {
1406 slash = strrchr( vol->v_path, '/' );
1410 slash = vol->v_path;
1411 if (ad_getentryoff(&ad, ADEID_NAME)) {
1412 ad_setentrylen( &ad, ADEID_NAME, strlen( slash ));
1413 memcpy(ad_entry( &ad, ADEID_NAME ), slash,
1414 ad_getentrylen( &ad, ADEID_NAME ));
1416 vol_setdate(vol->v_vid, &ad, st->st_mtime);
1420 if (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0)
1421 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1423 vol->v_ctime = aint;
1426 if (( bitmap & ( (1<<VOLPBIT_BFREE)|(1<<VOLPBIT_BTOTAL) |
1427 (1<<VOLPBIT_XBFREE)|(1<<VOLPBIT_XBTOTAL) |
1428 (1<<VOLPBIT_BSIZE)) ) != 0 ) {
1429 if ( getvolspace( vol, &bfree, &btotal, &xbfree, &xbtotal,
1430 &bsize) != AFP_OK ) {
1432 ad_close( &ad, ADFLAGS_HF );
1434 return( AFPERR_PARAM );
1439 while ( bitmap != 0 ) {
1440 while (( bitmap & 1 ) == 0 ) {
1448 /* check for read-only.
1449 * NOTE: we don't actually set the read-only flag unless
1450 * it's passed in that way as it's possible to mount
1451 * a read-write filesystem under a read-only one. */
1452 if ((vol->v_flags & AFPVOL_RO) ||
1453 ((utime(vol->v_path, NULL) < 0) && (errno == EROFS))) {
1454 ashort |= VOLPBIT_ATTR_RO;
1456 /* prior 2.1 only VOLPBIT_ATTR_RO is defined */
1457 if (afp_version > 20) {
1458 if (0 == (vol->v_flags & AFPVOL_NOFILEID) && vol->v_cdb != NULL &&
1459 (vol->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1460 ashort |= VOLPBIT_ATTR_FILEID;
1462 ashort |= VOLPBIT_ATTR_CATSEARCH;
1464 if (afp_version >= 30) {
1465 ashort |= VOLPBIT_ATTR_UTF8;
1466 if (vol->v_flags & AFPVOL_UNIX_PRIV)
1467 ashort |= VOLPBIT_ATTR_UNIXPRIV;
1468 if (vol->v_flags & AFPVOL_TM)
1469 ashort |= VOLPBIT_ATTR_TM;
1471 if (afp_version >= 32) {
1472 ashort |= VOLPBIT_ATTR_EXT_ATTRS;
1473 if (vol->v_flags & AFPVOL_ACLS)
1474 ashort |= VOLPBIT_ATTR_ACLS;
1478 ashort = htons(ashort);
1479 memcpy(data, &ashort, sizeof( ashort ));
1480 data += sizeof( ashort );
1484 ashort = htons( AFPVOLSIG_DEFAULT );
1485 memcpy(data, &ashort, sizeof( ashort ));
1486 data += sizeof( ashort );
1489 case VOLPBIT_CDATE :
1490 aint = vol->v_ctime;
1491 memcpy(data, &aint, sizeof( aint ));
1492 data += sizeof( aint );
1495 case VOLPBIT_MDATE :
1496 if ( st->st_mtime > vol->v_mtime ) {
1497 vol->v_mtime = st->st_mtime;
1499 aint = AD_DATE_FROM_UNIX(vol->v_mtime);
1500 memcpy(data, &aint, sizeof( aint ));
1501 data += sizeof( aint );
1504 case VOLPBIT_BDATE :
1505 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1506 aint = AD_DATE_START;
1507 memcpy(data, &aint, sizeof( aint ));
1508 data += sizeof( aint );
1512 memcpy(data, &vol->v_vid, sizeof( vol->v_vid ));
1513 data += sizeof( vol->v_vid );
1516 case VOLPBIT_BFREE :
1517 bfree = htonl( bfree );
1518 memcpy(data, &bfree, sizeof( bfree ));
1519 data += sizeof( bfree );
1522 case VOLPBIT_BTOTAL :
1523 btotal = htonl( btotal );
1524 memcpy(data, &btotal, sizeof( btotal ));
1525 data += sizeof( btotal );
1528 #ifndef NO_LARGE_VOL_SUPPORT
1529 case VOLPBIT_XBFREE :
1530 xbfree = hton64( xbfree );
1531 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1532 bcopy(&xbfree, data, sizeof(xbfree));
1533 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1534 memcpy(data, &xbfree, sizeof( xbfree ));
1535 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1536 data += sizeof( xbfree );
1539 case VOLPBIT_XBTOTAL :
1540 xbtotal = hton64( xbtotal );
1541 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1542 bcopy(&xbtotal, data, sizeof(xbtotal));
1543 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1544 memcpy(data, &xbtotal, sizeof( xbtotal ));
1545 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1546 data += sizeof( xbfree );
1548 #endif /* ! NO_LARGE_VOL_SUPPORT */
1552 data += sizeof( u_int16_t );
1555 case VOLPBIT_BSIZE: /* block size */
1556 bsize = htonl(bsize);
1557 memcpy(data, &bsize, sizeof(bsize));
1558 data += sizeof(bsize);
1563 ad_close( &ad, ADFLAGS_HF );
1565 return( AFPERR_BITMAP );
1571 ashort = htons( data - buf );
1572 memcpy(nameoff, &ashort, sizeof( ashort ));
1573 /* name is always in mac charset */
1574 aint = ucs2_to_charset( vol->v_maccharset, vol->v_macname, data+1, AFPVOL_MACNAMELEN + 1);
1584 ad_close_metadata( &ad);
1586 *buflen = data - buf;
1590 /* ------------------------- */
1591 static int stat_vol(u_int16_t bitmap, struct vol *vol, char *rbuf, size_t *rbuflen)
1597 if ( stat( vol->v_path, &st ) < 0 ) {
1599 return( AFPERR_PARAM );
1601 /* save the volume device number */
1602 vol->v_dev = st.st_dev;
1604 buflen = *rbuflen - sizeof( bitmap );
1605 if (( ret = getvolparams( bitmap, vol, &st,
1606 rbuf + sizeof( bitmap ), &buflen )) != AFP_OK ) {
1610 *rbuflen = buflen + sizeof( bitmap );
1611 bitmap = htons( bitmap );
1612 memcpy(rbuf, &bitmap, sizeof( bitmap ));
1617 /* ------------------------------- */
1618 void load_volumes(AFPObj *obj)
1620 struct passwd *pwent;
1625 /* check files date */
1626 if (obj->options.defaultvol.loaded) {
1627 changed = volfile_changed(&obj->options.defaultvol);
1629 if (obj->options.systemvol.loaded) {
1630 changed |= volfile_changed(&obj->options.systemvol);
1632 if (obj->options.uservol.loaded) {
1633 changed |= volfile_changed(&obj->options.uservol);
1642 pwent = getpwnam(obj->username);
1643 if ( (obj->options.flags & OPTION_USERVOLFIRST) == 0 ) {
1644 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent);
1647 if ((*obj->username == '\0') || (obj->options.flags & OPTION_NOUSERVOL)) {
1648 readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent);
1651 * Read user's AppleVolumes or .AppleVolumes file
1652 * If neither are readable, read the default volumes file. if
1653 * that doesn't work, create a user share.
1655 if (obj->options.uservol.name) {
1656 free(obj->options.uservol.name);
1658 obj->options.uservol.name = strdup(pwent->pw_dir);
1659 if ( readvolfile(obj, &obj->options.uservol, "AppleVolumes", 1, pwent) < 0 &&
1660 readvolfile(obj, &obj->options.uservol, ".AppleVolumes", 1, pwent) < 0 &&
1661 readvolfile(obj, &obj->options.uservol, "applevolumes", 1, pwent) < 0 &&
1662 readvolfile(obj, &obj->options.uservol, ".applevolumes", 1, pwent) < 0 &&
1663 obj->options.defaultvol.name != NULL ) {
1664 if (readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent) < 0)
1665 creatvol(obj, pwent, pwent->pw_dir, NULL, NULL, 1);
1668 if ( obj->options.flags & OPTION_USERVOLFIRST ) {
1669 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent );
1672 if ( obj->options.closevol ) {
1675 for ( vol = Volumes; vol; vol = vol->v_next ) {
1676 if (vol->v_deleted && !vol->v_new ) {
1685 /* ------------------------------- */
1686 int afp_getsrvrparms(AFPObj *obj, char *ibuf _U_, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
1699 for ( vcnt = 0, volume = Volumes; volume; volume = volume->v_next ) {
1700 if (!(volume->v_flags & AFPVOL_NOSTAT)) {
1703 if ( stat( volume->v_path, &st ) < 0 ) {
1704 LOG(log_info, logtype_afpd, "afp_getsrvrparms(%s): stat: %s",
1705 volume->v_path, strerror(errno) );
1706 continue; /* can't access directory */
1708 if (!S_ISDIR(st.st_mode)) {
1709 continue; /* not a dir */
1711 accessmode(volume->v_path, &ma, NULL, &st);
1712 if ((ma.ma_user & (AR_UREAD | AR_USEARCH)) != (AR_UREAD | AR_USEARCH)) {
1713 continue; /* no r-x access */
1716 if (volume->v_hide) {
1717 continue; /* config file changed but the volume was mounted */
1720 if (utf8_encoding()) {
1721 len = ucs2_to_charset_allocate(CH_UTF8_MAC, &namebuf, volume->v_u8mname);
1723 len = ucs2_to_charset_allocate(obj->options.maccharset, &namebuf, volume->v_macname);
1726 if (len == (size_t)-1)
1729 /* set password bit if there's a volume password */
1730 *data = (volume->v_password) ? AFPSRVR_PASSWD : 0;
1732 /* Apple 2 clients running ProDOS-8 expect one volume to have
1733 bit 0 of this byte set. They will not recognize anything
1734 on the server unless this is the case. I have not
1735 completely worked this out, but it's related to booting
1736 from the server. Support for that function is a ways
1737 off.. <shirsch@ibm.net> */
1738 *data |= (volume->v_flags & AFPVOL_A2VOL) ? AFPSRVR_CONFIGINFO : 0;
1739 *data++ |= 0; /* UNIX PRIVS BIT ..., OSX doesn't seem to use it, so we don't either */
1741 memcpy(data, namebuf, len );
1747 *rbuflen = data - rbuf;
1749 if ( gettimeofday( &tv, NULL ) < 0 ) {
1750 LOG(log_error, logtype_afpd, "afp_getsrvrparms(%s): gettimeofday: %s", volume->v_path, strerror(errno) );
1752 return AFPERR_PARAM;
1754 tv.tv_sec = AD_DATE_FROM_UNIX(tv.tv_sec);
1755 memcpy(data, &tv.tv_sec, sizeof( u_int32_t));
1756 data += sizeof( u_int32_t);
1761 /* ------------------------- */
1762 static int volume_codepage(AFPObj *obj, struct vol *volume)
1764 struct charset_functions *charset;
1767 if (!volume->v_volcodepage)
1768 volume->v_volcodepage = strdup("UTF8");
1770 if ( (charset_t) -1 == ( volume->v_volcharset = add_charset(volume->v_volcodepage)) ) {
1771 LOG (log_error, logtype_afpd, "Setting codepage %s as volume codepage failed", volume->v_volcodepage);
1775 if ( NULL == (charset = find_charset_functions(volume->v_volcodepage)) || charset->flags & CHARSET_ICONV ) {
1776 LOG (log_warning, logtype_afpd, "WARNING: volume encoding %s is *not* supported by netatalk, expect problems !!!!", volume->v_volcodepage);
1779 if (!volume->v_maccodepage)
1780 volume->v_maccodepage = strdup(obj->options.maccodepage);
1782 if ( (charset_t) -1 == ( volume->v_maccharset = add_charset(volume->v_maccodepage)) ) {
1783 LOG (log_error, logtype_afpd, "Setting codepage %s as mac codepage failed", volume->v_maccodepage);
1787 if ( NULL == ( charset = find_charset_functions(volume->v_maccodepage)) || ! (charset->flags & CHARSET_CLIENT) ) {
1788 LOG (log_error, logtype_afpd, "Fatal error: mac charset %s not supported", volume->v_maccodepage);
1791 volume->v_kTextEncoding = htonl(charset->kTextEncoding);
1795 /* ------------------------- */
1796 static int volume_openDB(struct vol *volume)
1800 if ((volume->v_flags & AFPVOL_NODEV)) {
1801 flags |= CNID_FLAG_NODEV;
1804 if (volume->v_cnidscheme == NULL) {
1805 volume->v_cnidscheme = strdup(DEFAULT_CNID_SCHEME);
1806 LOG(log_info, logtype_afpd, "Volume %s use CNID scheme %s.", volume->v_path, volume->v_cnidscheme);
1808 if (volume->v_dbpath)
1809 volume->v_cdb = cnid_open (volume->v_dbpath, volume->v_umask, volume->v_cnidscheme, flags);
1811 volume->v_cdb = cnid_open (volume->v_path, volume->v_umask, volume->v_cnidscheme, flags);
1813 if (!volume->v_cdb) {
1814 flags |= CNID_FLAG_MEMORY;
1815 LOG(log_error, logtype_afpd, "Reopen volume %s using in memory temporary CNID DB.", volume->v_path);
1816 volume->v_cdb = cnid_open (volume->v_path, volume->v_umask, "tdb", flags);
1818 /* kill ourself with SIGUSR2 aka msg pending */
1819 if (volume->v_cdb) {
1820 setmessage("Something wrong with the volume's DB ... FIXME with a better msg");
1821 kill(getpid(), SIGUSR2);
1822 /* XXX desactivate cachecnid ? */
1827 return (!volume->v_cdb)?-1:0;
1830 /* -------------------------
1831 * we are the user here
1833 int afp_openvol(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
1844 char path[ MAXPATHLEN + 1];
1850 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1851 bitmap = ntohs( bitmap );
1852 ibuf += sizeof( bitmap );
1855 if (( bitmap & (1<<VOLPBIT_VID)) == 0 ) {
1856 return AFPERR_BITMAP;
1859 len = (unsigned char)*ibuf++;
1860 volname = obj->oldtmp;
1862 if ((volname_tmp = strchr(volname,'+')) != NULL)
1863 volname = volname_tmp+1;
1865 if (utf8_encoding()) {
1866 namelen = convert_string(CH_UTF8_MAC, CH_UCS2, ibuf, len, volname, sizeof(obj->oldtmp));
1868 namelen = convert_string(obj->options.maccharset, CH_UCS2, ibuf, len, volname, sizeof(obj->oldtmp));
1871 if ( namelen <= 0) {
1872 return AFPERR_PARAM;
1876 if ((len + 1) & 1) /* pad to an even boundary */
1881 for ( volume = Volumes; volume; volume = volume->v_next ) {
1882 if ( strcasecmp_w( (ucs2_t*) volname, volume->v_name ) == 0 ) {
1887 if ( volume == NULL ) {
1888 return AFPERR_PARAM;
1891 /* check for a volume password */
1892 if (volume->v_password && strncmp(ibuf, volume->v_password, VOLPASSLEN)) {
1893 return AFPERR_ACCESS;
1896 if (( volume->v_flags & AFPVOL_OPEN ) ) {
1897 /* the volume is already open */
1899 set_uidgid ( volume );
1901 return stat_vol(bitmap, volume, rbuf, rbuflen);
1904 if (volume->v_root_preexec) {
1905 if ((ret = afprun(1, volume->v_root_preexec, NULL)) && volume->v_root_preexec_close) {
1906 LOG(log_error, logtype_afpd, "afp_openvol(%s): root preexec : %d", volume->v_path, ret );
1912 set_uidgid ( volume );
1915 if (volume->v_preexec) {
1916 if ((ret = afprun(0, volume->v_preexec, NULL)) && volume->v_preexec_close) {
1917 LOG(log_error, logtype_afpd, "afp_openvol(%s): preexec : %d", volume->v_path, ret );
1922 if ( stat( volume->v_path, &st ) < 0 ) {
1923 return AFPERR_PARAM;
1926 if ( chdir( volume->v_path ) < 0 ) {
1927 return AFPERR_PARAM;
1930 if ( NULL == getcwd(path, MAXPATHLEN)) {
1931 /* shouldn't be fatal but it will fail later */
1932 LOG(log_error, logtype_afpd, "afp_openvol(%s): volume pathlen too long", volume->v_path);
1936 if (volume_codepage(obj, volume) < 0) {
1941 /* initialize volume variables
1944 if (utf8_encoding()) {
1945 volume->max_filename = 255;
1948 volume->max_filename = MACFILELEN;
1951 volume->v_dir = volume->v_root = NULL;
1952 volume->v_hash = NULL;
1954 volume->v_flags |= AFPVOL_OPEN;
1955 volume->v_cdb = NULL;
1957 if (utf8_encoding()) {
1958 len = convert_string_allocate(CH_UCS2, CH_UTF8_MAC, volume->v_u8mname, namelen, &vol_mname);
1960 len = convert_string_allocate(CH_UCS2, obj->options.maccharset, volume->v_macname, namelen, &vol_mname);
1962 if ( !vol_mname || len <= 0) {
1967 if ((vol_uname = strrchr(path, '/')) == NULL)
1969 else if (*(vol_uname + 1) != '\0')
1972 if ((dir = dirnew(vol_mname, vol_uname) ) == NULL) {
1974 LOG(log_error, logtype_afpd, "afp_openvol(%s): malloc: %s", volume->v_path, strerror(errno) );
1980 dir->d_did = DIRDID_ROOT;
1981 dir->d_color = DIRTREE_COLOR_BLACK; /* root node is black */
1982 dir->d_m_name_ucs2 = strdup_w(volume->v_name);
1983 volume->v_dir = volume->v_root = dir;
1984 volume->v_curdir = NULL;
1985 volume->v_hash = dirhash();
1987 curdir = volume->v_dir;
1988 if (volume_openDB(volume) < 0) {
1989 LOG(log_error, logtype_afpd, "Fatal error: cannot open CNID or invalid CNID backend for %s: %s",
1990 volume->v_path, volume->v_cnidscheme);
1995 ret = stat_vol(bitmap, volume, rbuf, rbuflen);
1996 if (ret == AFP_OK) {
1998 if (!(volume->v_flags & AFPVOL_RO)) {
1999 handle_special_folders( volume );
2000 savevolinfo(volume, Cnid_srv, Cnid_port);
2004 * If you mount a volume twice, the second time the trash appears on
2005 * the desk-top. That's because the Mac remembers the DID for the
2006 * trash (even for volumes in different zones, on different servers).
2007 * Just so this works better, we prime the DID cache with the trash,
2008 * fixing the trash at DID 17.
2009 * FIXME (RL): should it be done inside a CNID backend ? (always returning Trash DID when asked) ?
2011 if ((volume->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
2013 /* FIXME find db time stamp */
2014 if (cnid_getstamp(volume->v_cdb, volume->v_stamp, sizeof(volume->v_stamp)) < 0) {
2015 LOG (log_error, logtype_afpd,
2016 "afp_openvol(%s): Fatal error: Unable to get stamp value from CNID backend",
2024 cname( volume, volume->v_dir, &p );
2030 if (volume->v_dir) {
2031 hash_free( volume->v_hash);
2032 dirfree( volume->v_dir );
2033 volume->v_dir = volume->v_root = NULL;
2036 volume->v_flags &= ~AFPVOL_OPEN;
2037 if (volume->v_cdb != NULL) {
2038 cnid_close(volume->v_cdb);
2039 volume->v_cdb = NULL;
2045 /* ------------------------- */
2046 static void closevol(struct vol *vol)
2051 hash_free( vol->v_hash);
2052 dirfree( vol->v_root );
2054 if (vol->v_cdb != NULL) {
2055 cnid_close(vol->v_cdb);
2059 if (vol->v_postexec) {
2060 afprun(0, vol->v_postexec, NULL);
2062 if (vol->v_root_postexec) {
2063 afprun(1, vol->v_root_postexec, NULL);
2067 /* ------------------------- */
2068 void close_all_vol(void)
2072 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
2073 if ( (ovol->v_flags & AFPVOL_OPEN) ) {
2074 ovol->v_flags &= ~AFPVOL_OPEN;
2080 /* ------------------------- */
2081 static void deletevol(struct vol *vol)
2085 vol->v_flags &= ~AFPVOL_OPEN;
2086 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
2087 if ( (ovol->v_flags & AFPVOL_OPEN) ) {
2091 if ( ovol != NULL ) {
2092 /* Even if chdir fails, we can't say afp_closevol fails. */
2093 if ( chdir( ovol->v_path ) == 0 ) {
2094 curdir = ovol->v_dir;
2099 if (vol->v_deleted) {
2100 showvol(vol->v_name);
2107 /* ------------------------- */
2108 int afp_closevol(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2115 memcpy(&vid, ibuf, sizeof( vid ));
2116 if (NULL == ( vol = getvolbyvid( vid )) ) {
2117 return( AFPERR_PARAM );
2125 /* ------------------------- */
2126 struct vol *getvolbyvid(const u_int16_t vid )
2130 for ( vol = Volumes; vol; vol = vol->v_next ) {
2131 if ( vid == vol->v_vid ) {
2135 if ( vol == NULL || ( vol->v_flags & AFPVOL_OPEN ) == 0 ) {
2141 #endif /* FORCE_UIDGID */
2146 /* ------------------------ */
2147 static int ext_cmp_key(const void *key, const void *obj)
2149 const char *p = key;
2150 const struct extmap *em = obj;
2151 return strdiacasecmp(p, em->em_ext);
2153 struct extmap *getextmap(const char *path)
2158 if (!Extmap_cnt || NULL == ( p = strrchr( path, '.' )) ) {
2159 return( Defextmap );
2163 return( Defextmap );
2165 em = bsearch(p, Extmap, Extmap_cnt, sizeof(struct extmap), ext_cmp_key);
2169 return( Defextmap );
2173 /* ------------------------- */
2174 struct extmap *getdefextmap(void)
2176 return( Defextmap );
2179 /* --------------------------
2180 poll if a volume is changed by other processes.
2182 0 no attention msg sent
2183 1 attention msg sent
2184 -1 error (socket closed)
2186 Note: if attention return -1 no packet has been
2187 sent because the buffer is full, we don't care
2188 either there's no reader or there's a lot of
2189 traffic and another pollvoltime will follow
2191 int pollvoltime(AFPObj *obj)
2198 if (!(afp_version > 21 && obj->options.server_notif))
2201 if ( gettimeofday( &tv, NULL ) < 0 )
2204 for ( vol = Volumes; vol; vol = vol->v_next ) {
2205 if ( (vol->v_flags & AFPVOL_OPEN) && vol->v_mtime + 30 < tv.tv_sec) {
2206 if ( !stat( vol->v_path, &st ) && vol->v_mtime != st.st_mtime ) {
2207 vol->v_mtime = st.st_mtime;
2208 if (!obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED))
2217 /* ------------------------- */
2218 void setvoltime(AFPObj *obj, struct vol *vol)
2222 /* just looking at vol->v_mtime is broken seriously since updates
2223 * from other users afpd processes never are seen.
2224 * This is not the most elegant solution (a shared memory between
2225 * the afpd processes would come closer)
2228 if ( gettimeofday( &tv, NULL ) < 0 ) {
2229 LOG(log_error, logtype_afpd, "setvoltime(%s): gettimeofday: %s", vol->v_path, strerror(errno) );
2232 if( utime( vol->v_path, NULL ) < 0 ) {
2233 /* write of time failed ... probably a read only filesys,
2234 * where no other users can interfere, so there's no issue here
2238 /* a little granularity */
2239 if (vol->v_mtime < tv.tv_sec) {
2240 vol->v_mtime = tv.tv_sec;
2241 /* or finder doesn't update free space
2242 * XXX is it still true with newer OSX?
2244 if (afp_version > 21 && obj->options.server_notif) {
2245 obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED);
2250 /* ------------------------- */
2251 int afp_getvolparams(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_,char *rbuf, size_t *rbuflen)
2254 u_int16_t vid, bitmap;
2257 memcpy(&vid, ibuf, sizeof( vid ));
2258 ibuf += sizeof( vid );
2259 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2260 bitmap = ntohs( bitmap );
2262 if (NULL == ( vol = getvolbyvid( vid )) ) {
2264 return( AFPERR_PARAM );
2267 return stat_vol(bitmap, vol, rbuf, rbuflen);
2270 /* ------------------------- */
2271 int afp_setvolparams(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2275 u_int16_t vid, bitmap;
2281 memcpy(&vid, ibuf, sizeof( vid ));
2282 ibuf += sizeof( vid );
2283 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2284 bitmap = ntohs( bitmap );
2285 ibuf += sizeof(bitmap);
2287 if (( vol = getvolbyvid( vid )) == NULL ) {
2288 return( AFPERR_PARAM );
2291 if ((vol->v_flags & AFPVOL_RO))
2292 return AFPERR_VLOCK;
2294 /* we can only set the backup date. */
2295 if (bitmap != (1 << VOLPBIT_BDATE))
2296 return AFPERR_BITMAP;
2298 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2299 if ( ad_open( vol->v_path, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR,
2302 return AFPERR_VLOCK;
2304 return AFPERR_ACCESS;
2307 memcpy(&aint, ibuf, sizeof(aint));
2308 ad_setdate(&ad, AD_DATE_BACKUP, aint);
2310 ad_close(&ad, ADFLAGS_HF);
2314 /* ------------------------- */
2315 int wincheck(const struct vol *vol, const char *path)
2319 if (!(vol->v_flags & AFPVOL_MSWINDOWS))
2322 /* empty paths are not allowed */
2323 if ((len = strlen(path)) == 0)
2326 /* leading or trailing whitespaces are not allowed, carriage returns
2327 * and probably other whitespace is okay, tabs are not allowed
2329 if ((path[0] == ' ') || (path[len-1] == ' '))
2332 /* certain characters are not allowed */
2333 if (strpbrk(path, MSWINDOWS_BADCHARS))
2336 /* everything else is okay */
2342 * precreate a folder
2343 * this is only intended for folders in the volume root
2344 * It will *not* work if the folder name contains extended characters
2346 static int create_special_folder (const struct vol *vol, const struct _special_folder *folder)
2355 p = (char *) malloc ( strlen(vol->v_path)+strlen(folder->name)+2);
2357 LOG(log_error, logtype_afpd,"malloc failed");
2361 q=strdup(folder->name);
2363 LOG(log_error, logtype_afpd,"malloc failed");
2367 strcpy(p, vol->v_path);
2372 if ((vol->v_casefold & AFPVOL_MTOUUPPER))
2374 else if ((vol->v_casefold & AFPVOL_MTOULOWER))
2380 if ( (ret = stat( p, &st )) < 0 ) {
2381 if (folder->precreate) {
2382 if (ad_mkdir(p, folder->mode)) {
2383 LOG(log_debug, logtype_afpd,"Creating '%s' failed in %s: %s", p, vol->v_path, strerror(errno));
2392 if ( !ret && folder->hide) {
2394 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2395 if (ad_open( p, vol_noadouble(vol) | ADFLAGS_HF|ADFLAGS_DIR,
2396 O_RDWR|O_CREAT, 0666, &ad) < 0) {
2401 if ((ad_get_HF_flags( &ad ) & O_CREAT) ) {
2402 if (ad_getentryoff(&ad, ADEID_NAME)) {
2403 ad_setentrylen( &ad, ADEID_NAME, strlen(folder->name));
2404 memcpy(ad_entry( &ad, ADEID_NAME ), folder->name,
2405 ad_getentrylen( &ad, ADEID_NAME ));
2409 ad_getattr(&ad, &attr);
2410 attr |= htons( ntohs( attr ) | ATTRBIT_INVISIBLE );
2411 ad_setattr(&ad, attr);
2413 /* do the same with the finder info */
2414 if (ad_entry(&ad, ADEID_FINDERI)) {
2415 memcpy(&attr, ad_entry(&ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF, sizeof(attr));
2416 attr |= htons(FINDERINFO_INVISIBLE);
2417 memcpy(ad_entry(&ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF,&attr, sizeof(attr));
2421 ad_close( &ad, ADFLAGS_HF );
2428 static void handle_special_folders (const struct vol * vol)
2430 const _special_folder *p = &special_folders[0];
2432 if ((vol->v_flags & AFPVOL_RO))
2435 for (; p->name != NULL; p++) {
2436 create_special_folder (vol, p);