2 * $Id: volume.c,v 1.110 2009-12-11 13:26:44 franklahm Exp $
4 * Copyright (c) 1990,1993 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
10 #endif /* HAVE_CONFIG_H */
25 #else /* STDC_HEADERS */
29 #endif /* HAVE_STRCHR */
30 char *strchr (), *strrchr ();
32 #define memcpy(d,s,n) bcopy ((s), (d), (n))
33 #define memmove(d,s,n) bcopy ((s), (d), (n))
34 #endif /* ! HAVE_MEMCPY */
35 #endif /* STDC_HEADERS */
36 #include <sys/param.h>
37 #include <sys/socket.h>
38 #include <netinet/in.h>
39 #include <arpa/inet.h>
40 #include <atalk/asp.h>
41 #include <atalk/dsi.h>
42 #include <atalk/adouble.h>
43 #include <atalk/afp.h>
44 #include <atalk/util.h>
45 #include <atalk/volinfo.h>
46 #include <atalk/logger.h>
47 #include <atalk/vfs.h>
50 #include <atalk/cnid.h>
54 #include "directory.h"
62 extern int afprun(int root, char *cmd, int *outfd);
65 #define MIN(a, b) ((a) < (b) ? (a) : (b))
68 #ifndef NO_LARGE_VOL_SUPPORT
69 #if BYTE_ORDER == BIG_ENDIAN
72 #else /* BYTE_ORDER == BIG_ENDIAN */
73 #define hton64(x) ((u_int64_t) (htonl(((x) >> 32) & 0xffffffffLL)) | \
74 (u_int64_t) ((htonl(x) & 0xffffffffLL) << 32))
75 #define ntoh64(x) (hton64(x))
76 #endif /* BYTE_ORDER == BIG_ENDIAN */
77 #endif /* ! NO_LARGE_VOL_SUPPORT */
79 static struct vol *Volumes = NULL;
80 static u_int16_t lastvid = 0;
81 static char *Trash = "\02\024Network Trash Folder";
83 static struct extmap *Extmap = NULL, *Defextmap = NULL;
84 static int Extmap_cnt;
85 static void free_extmap(void);
87 #define VOLOPT_ALLOW 0 /* user allow list */
88 #define VOLOPT_DENY 1 /* user deny list */
89 #define VOLOPT_RWLIST 2 /* user rw list */
90 #define VOLOPT_ROLIST 3 /* user ro list */
91 #define VOLOPT_PASSWORD 4 /* volume password */
92 #define VOLOPT_CASEFOLD 5 /* character case mangling */
93 #define VOLOPT_FLAGS 6 /* various flags */
94 #define VOLOPT_DBPATH 7 /* path to database */
95 #define VOLOPT_MAPCHARS 8 /* does mtou and utom mappings. syntax:
96 m and u can be double-byte hex
101 !u -> make u illegal always
102 ~u -> make u illegal only as the first
103 part of a double-byte character.
105 #define VOLOPT_VETO 10 /* list of veto filespec */
106 #define VOLOPT_PREEXEC 11 /* preexec command */
107 #define VOLOPT_ROOTPREEXEC 12 /* root preexec command */
109 #define VOLOPT_POSTEXEC 13 /* postexec command */
110 #define VOLOPT_ROOTPOSTEXEC 14 /* root postexec command */
112 #define VOLOPT_ENCODING 15 /* mac encoding (pre OSX)*/
113 #define VOLOPT_MACCHARSET 16
114 #define VOLOPT_CNIDSCHEME 17
115 #define VOLOPT_ADOUBLE 18 /* adouble version */
120 #define VOLOPT_FORCEUID 19 /* force uid for username x */
121 #define VOLOPT_FORCEGID 20 /* force gid for group x */
122 #endif /* FORCE_UIDGID */
124 #define VOLOPT_UMASK 21
125 #define VOLOPT_ALLOWED_HOSTS 22
126 #define VOLOPT_DENIED_HOSTS 23
127 #define VOLOPT_DPERM 24 /* dperm default directories perms */
128 #define VOLOPT_FPERM 25 /* fperm default files perms */
129 #define VOLOPT_DFLTPERM 26 /* perm */
130 #define VOLOPT_EA_VFS 27 /* Extended Attributes vfs indirection */
132 #define VOLOPT_MAX 28 /* <== IMPORTANT !!!!!! */
133 #define VOLOPT_NUM (VOLOPT_MAX + 1)
136 #define VOLOPT_DEFAULT ":DEFAULT:"
137 #define VOLOPT_DEFAULT_LEN 9
144 typedef struct _special_folder {
151 static const _special_folder special_folders[] = {
152 {"Network Trash Folder", 1, 0777, 1},
153 {"Temporary Items", 1, 0777, 1},
154 {".AppleDesktop", 1, 0777, 0},
156 {"TheFindByContentFolder", 0, 0, 1},
157 {"TheVolumeSettingsFolder", 0, 0, 1},
161 static void handle_special_folders (const struct vol *);
162 static void deletevol(struct vol *vol);
163 static void volume_free(struct vol *vol);
165 static void volfree(struct vol_option *options,
166 const struct vol_option *save)
171 for (i = 0; i < VOLOPT_MAX; i++) {
172 if (options[i].c_value && (options[i].c_value != save[i].c_value))
173 free(options[i].c_value);
176 for (i = 0; i < VOLOPT_MAX; i++) {
177 if (options[i].c_value)
178 free(options[i].c_value);
184 /* handle variable substitutions. here's what we understand:
185 * $b -> basename of path
186 * $c -> client ip/appletalk address
187 * $d -> volume pathname on server
188 * $f -> full name (whatever's in the gecos field)
191 * $i -> client ip/appletalk address without port
192 * $s -> server name (hostname if it doesn't exist)
193 * $u -> username (guest is usually nobody)
194 * $v -> volume name or basename if null
195 * $z -> zone (may not exist)
200 #define is_var(a, b) (strncmp((a), (b), 2) == 0)
202 static char *volxlate(AFPObj *obj, char *dest, size_t destlen,
203 char *src, struct passwd *pwd, char *path, char *volname)
214 dest = calloc(destlen +1, 1);
220 strlcpy(dest, src, destlen +1);
221 if ((p = strchr(src, '$')) == NULL) /* nothing to do */
224 /* first part of the path. just forward to the next variable. */
225 len = MIN((size_t)(p - src), destlen);
231 while (p && destlen > 0) {
232 /* now figure out what the variable is */
234 if (is_var(p, "$b")) {
236 if ((q = strrchr(path, '/')) == NULL)
238 else if (*(q + 1) != '\0')
241 } else if (is_var(p, "$c")) {
242 if (obj->proto == AFPPROTO_ASP) {
243 ASP asp = obj->handle;
245 len = sprintf(dest, "%u.%u", ntohs(asp->asp_sat.sat_addr.s_net),
246 asp->asp_sat.sat_addr.s_node);
250 } else if (obj->proto == AFPPROTO_DSI) {
251 DSI *dsi = obj->handle;
252 len = sprintf(dest, "%s:%u",
253 getip_string((struct sockaddr *)&dsi->client),
254 getip_port((struct sockaddr *)&dsi->client));
258 } else if (is_var(p, "$d")) {
260 } else if (is_var(p, "$f")) {
261 if ((r = strchr(pwd->pw_gecos, ',')))
264 } else if (is_var(p, "$g")) {
265 struct group *grp = getgrgid(pwd->pw_gid);
268 } else if (is_var(p, "$h")) {
269 q = obj->options.hostname;
270 } else if (is_var(p, "$i")) {
271 if (obj->proto == AFPPROTO_ASP) {
272 ASP asp = obj->handle;
274 len = sprintf(dest, "%u", ntohs(asp->asp_sat.sat_addr.s_net));
278 } else if (obj->proto == AFPPROTO_DSI) {
279 DSI *dsi = obj->handle;
280 q = getip_string((struct sockaddr *)&dsi->client);
282 } else if (is_var(p, "$s")) {
285 else if (obj->options.server) {
286 q = obj->options.server;
288 q = obj->options.hostname;
289 } else if (is_var(p, "$u")) {
291 if ( obj->options.ntseparator && (sep = strchr(obj->username, obj->options.ntseparator[0])) != NULL)
295 } else if (is_var(p, "$v")) {
300 if ((q = strrchr(path, '/')) == NULL)
302 else if (*(q + 1) != '\0')
305 } else if (is_var(p, "$z")) {
307 } else if (is_var(p, "$$")) {
312 /* copy the stuff over. if we don't understand something that we
313 * should, just skip it over. */
315 len = MIN(p == q ? 2 : strlen(q), destlen);
316 strncpy(dest, q, len);
321 /* stuff up to next $ */
323 p = strchr(src, '$');
324 len = p ? MIN((size_t)(p - src), destlen) : destlen;
326 strncpy(dest, src, len);
334 /* to make sure that val is valid, make sure to select an opt that
336 static int optionok(const char *buf, const char *opt, const char *val)
338 if (!strstr(buf,opt))
346 /* -------------------- */
347 static void setoption(struct vol_option *options, struct vol_option *save, int opt, const char *val)
349 if (options[opt].c_value && (!save || options[opt].c_value != save[opt].c_value))
350 free(options[opt].c_value);
351 options[opt].c_value = strdup(val + 1);
354 /* ------------------------------------------
355 handle all the options. tmp can't be NULL. */
356 static void volset(struct vol_option *options, struct vol_option *save,
357 char *volname, int vlen,
362 val = strchr(tmp, ':');
364 /* we'll assume it's a volume name. */
365 strncpy(volname, tmp, vlen);
370 LOG(log_debug, logtype_afpd, "Parsing volset %s", val);
372 if (optionok(tmp, "allow:", val)) {
373 setoption(options, save, VOLOPT_ALLOW, val);
375 } else if (optionok(tmp, "deny:", val)) {
376 setoption(options, save, VOLOPT_DENY, val);
378 } else if (optionok(tmp, "rwlist:", val)) {
379 setoption(options, save, VOLOPT_RWLIST, val);
381 } else if (optionok(tmp, "rolist:", val)) {
382 setoption(options, save, VOLOPT_ROLIST, val);
384 } else if (optionok(tmp, "codepage:", val)) {
385 LOG (log_error, logtype_afpd, "The old codepage system has been removed. Please make sure to read the documentation !!!!");
386 /* Make sure we don't screw anything */
388 } else if (optionok(tmp, "volcharset:", val)) {
389 setoption(options, save, VOLOPT_ENCODING, val);
390 } else if (optionok(tmp, "maccharset:", val)) {
391 setoption(options, save, VOLOPT_MACCHARSET, val);
392 } else if (optionok(tmp, "veto:", val)) {
393 setoption(options, save, VOLOPT_VETO, val);
394 } else if (optionok(tmp, "cnidscheme:", val)) {
395 setoption(options, save, VOLOPT_CNIDSCHEME, val);
396 } else if (optionok(tmp, "casefold:", val)) {
397 if (strcasecmp(val + 1, "tolower") == 0)
398 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMLOWER;
399 else if (strcasecmp(val + 1, "toupper") == 0)
400 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMUPPER;
401 else if (strcasecmp(val + 1, "xlatelower") == 0)
402 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UUPPERMLOWER;
403 else if (strcasecmp(val + 1, "xlateupper") == 0)
404 options[VOLOPT_CASEFOLD].i_value = AFPVOL_ULOWERMUPPER;
405 } else if (optionok(tmp, "adouble:", val)) {
406 if (strcasecmp(val + 1, "v1") == 0)
407 options[VOLOPT_ADOUBLE].i_value = AD_VERSION1;
408 #if AD_VERSION == AD_VERSION2
409 else if (strcasecmp(val + 1, "v2") == 0)
410 options[VOLOPT_ADOUBLE].i_value = AD_VERSION2;
411 else if (strcasecmp(val + 1, "osx") == 0)
412 options[VOLOPT_ADOUBLE].i_value = AD_VERSION2_OSX;
413 else if (strcasecmp(val + 1, "sfm") == 0)
414 options[VOLOPT_ADOUBLE].i_value = AD_VERSION1_SFM;
416 } else if (optionok(tmp, "options:", val)) {
419 if ((p = strtok(val + 1, ",")) == NULL) /* nothing */
423 if (strcasecmp(p, "prodos") == 0)
424 options[VOLOPT_FLAGS].i_value |= AFPVOL_A2VOL;
425 else if (strcasecmp(p, "mswindows") == 0) {
426 options[VOLOPT_FLAGS].i_value |= AFPVOL_MSWINDOWS | AFPVOL_USEDOTS;
427 } else if (strcasecmp(p, "crlf") == 0)
428 options[VOLOPT_FLAGS].i_value |= AFPVOL_CRLF;
429 else if (strcasecmp(p, "noadouble") == 0)
430 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOADOUBLE;
431 else if (strcasecmp(p, "ro") == 0)
432 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
433 else if (strcasecmp(p, "nohex") == 0)
434 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOHEX;
435 else if (strcasecmp(p, "usedots") == 0)
436 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS;
437 else if (strcasecmp(p, "invisibledots") == 0)
438 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS | AFPVOL_INV_DOTS;
439 else if (strcasecmp(p, "limitsize") == 0)
440 options[VOLOPT_FLAGS].i_value |= AFPVOL_LIMITSIZE;
441 /* support for either "dropbox" or "dropkludge" */
442 else if (strcasecmp(p, "dropbox") == 0)
443 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
444 else if (strcasecmp(p, "dropkludge") == 0)
445 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
446 else if (strcasecmp(p, "nofileid") == 0)
447 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOFILEID;
448 else if (strcasecmp(p, "nostat") == 0)
449 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOSTAT;
450 else if (strcasecmp(p, "preexec_close") == 0)
451 options[VOLOPT_PREEXEC].i_value = 1;
452 else if (strcasecmp(p, "root_preexec_close") == 0)
453 options[VOLOPT_ROOTPREEXEC].i_value = 1;
454 else if (strcasecmp(p, "upriv") == 0)
455 options[VOLOPT_FLAGS].i_value |= AFPVOL_UNIX_PRIV;
456 else if (strcasecmp(p, "acls") == 0)
457 options[VOLOPT_FLAGS].i_value |= AFPVOL_ACLS;
458 else if (strcasecmp(p, "nodev") == 0)
459 options[VOLOPT_FLAGS].i_value |= AFPVOL_NODEV;
460 else if (strcasecmp(p, "caseinsensitive") == 0)
461 options[VOLOPT_FLAGS].i_value |= AFPVOL_CASEINSEN;
462 else if (strcasecmp(p, "illegalseq") == 0)
463 options[VOLOPT_FLAGS].i_value |= AFPVOL_EILSEQ;
464 else if (strcasecmp(p, "nocnidcache") == 0)
465 options[VOLOPT_FLAGS].i_value &= ~AFPVOL_CACHE;
466 else if (strcasecmp(p, "tm") == 0)
467 options[VOLOPT_FLAGS].i_value |= AFPVOL_TM;
469 p = strtok(NULL, ",");
472 } else if (optionok(tmp, "dbpath:", val)) {
473 setoption(options, save, VOLOPT_DBPATH, val);
475 } else if (optionok(tmp, "umask:", val)) {
476 options[VOLOPT_UMASK].i_value = (int)strtol(val +1, NULL, 8);
477 } else if (optionok(tmp, "dperm:", val)) {
478 options[VOLOPT_DPERM].i_value = (int)strtol(val+1, NULL, 8);
479 } else if (optionok(tmp, "fperm:", val)) {
480 options[VOLOPT_FPERM].i_value = (int)strtol(val+1, NULL, 8);
481 } else if (optionok(tmp, "perm:", val)) {
482 options[VOLOPT_DFLTPERM].i_value = (int)strtol(val+1, NULL, 8);
483 } else if (optionok(tmp, "mapchars:",val)) {
484 setoption(options, save, VOLOPT_MAPCHARS, val);
486 } else if (optionok(tmp, "password:", val)) {
487 setoption(options, save, VOLOPT_PASSWORD, val);
491 /* this code allows forced uid/gid per volume settings */
492 } else if (optionok(tmp, "forceuid:", val)) {
493 setoption(options, save, VOLOPT_FORCEUID, val);
494 } else if (optionok(tmp, "forcegid:", val)) {
495 setoption(options, save, VOLOPT_FORCEGID, val);
497 #endif /* FORCE_UIDGID */
498 } else if (optionok(tmp, "root_preexec:", val)) {
499 setoption(options, save, VOLOPT_ROOTPREEXEC, val);
501 } else if (optionok(tmp, "preexec:", val)) {
502 setoption(options, save, VOLOPT_PREEXEC, val);
504 } else if (optionok(tmp, "root_postexec:", val)) {
505 setoption(options, save, VOLOPT_ROOTPOSTEXEC, val);
507 } else if (optionok(tmp, "postexec:", val)) {
508 setoption(options, save, VOLOPT_POSTEXEC, val);
510 } else if (optionok(tmp, "allowed_hosts:", val)) {
511 setoption(options, save, VOLOPT_ALLOWED_HOSTS, val);
513 } else if (optionok(tmp, "denied_hosts:", val)) {
514 setoption(options, save, VOLOPT_DENIED_HOSTS, val);
516 } else if (optionok(tmp, "ea:", val)) {
517 if (strcasecmp(val + 1, "ad") == 0)
518 options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_AD;
521 /* ignore unknown options */
522 LOG(log_debug, logtype_afpd, "ignoring unknown volume option: %s", tmp);
527 /* ----------------- */
528 static void showvol(const ucs2_t *name)
531 for ( volume = Volumes; volume; volume = volume->v_next ) {
532 if (volume->v_hide && !strcasecmp_w( volume->v_name, name ) ) {
539 /* ------------------------------- */
540 static int creatvol(AFPObj *obj, struct passwd *pwd,
541 char *path, char *name,
542 struct vol_option *options,
543 const int user /* user defined volume */
547 int suffixlen, vlen, tmpvlen, u8mvlen, macvlen;
549 char tmpname[AFPVOL_U8MNAMELEN+1];
550 ucs2_t u8mtmpname[(AFPVOL_U8MNAMELEN+1)*2], mactmpname[(AFPVOL_MACNAMELEN+1)*2];
551 char suffix[6]; /* max is #FFFF */
554 if ( name == NULL || *name == '\0' ) {
555 if ((name = strrchr( path, '/' )) == NULL) {
556 return -1; /* Obviously not a fully qualified path */
559 /* if you wish to share /, you need to specify a name. */
564 /* suffix for mangling use (lastvid + 1) */
565 /* because v_vid has not been decided yet. */
566 suffixlen = sprintf(suffix, "%c%X", MANGLE_CHAR, lastvid + 1 );
568 vlen = strlen( name );
570 /* Unicode Volume Name */
571 /* Firstly convert name from unixcharset to UTF8-MAC */
573 tmpvlen = convert_charset(obj->options.unixcharset, CH_UTF8_MAC, 0, name, vlen, tmpname, AFPVOL_U8MNAMELEN, &flags);
575 strcpy(tmpname, "???");
579 /* Do we have to mangle ? */
580 if ( (flags & CONV_REQMANGLE) || (tmpvlen > obj->options.volnamelen)) {
581 if (tmpvlen + suffixlen > obj->options.volnamelen) {
583 tmpvlen = convert_charset(obj->options.unixcharset, CH_UTF8_MAC, 0, name, vlen, tmpname, obj->options.volnamelen - suffixlen, &flags);
584 tmpname[tmpvlen >= 0 ? tmpvlen : 0] = 0;
586 strcat(tmpname, suffix);
587 tmpvlen = strlen(tmpname);
590 /* Secondly convert name from UTF8-MAC to UCS2 */
591 if ( 0 >= ( u8mvlen = convert_string(CH_UTF8_MAC, CH_UCS2, tmpname, tmpvlen, u8mtmpname, AFPVOL_U8MNAMELEN*2)) )
594 LOG(log_debug, logtype_afpd, "createvol: Volume '%s' -> UTF8-MAC Name: '%s'", name, tmpname);
596 /* Maccharset Volume Name */
597 /* Firsty convert name from unixcharset to maccharset */
599 tmpvlen = convert_charset(obj->options.unixcharset, obj->options.maccharset, 0, name, vlen, tmpname, AFPVOL_U8MNAMELEN, &flags);
601 strcpy(tmpname, "???");
605 /* Do we have to mangle ? */
606 if ( (flags & CONV_REQMANGLE) || (tmpvlen > AFPVOL_MACNAMELEN)) {
607 if (tmpvlen + suffixlen > AFPVOL_MACNAMELEN) {
609 tmpvlen = convert_charset(obj->options.unixcharset, obj->options.maccharset, 0, name, vlen, tmpname, AFPVOL_MACNAMELEN - suffixlen, &flags);
610 tmpname[tmpvlen >= 0 ? tmpvlen : 0] = 0;
612 strcat(tmpname, suffix);
613 tmpvlen = strlen(tmpname);
616 /* Secondly convert name from maccharset to UCS2 */
617 if ( 0 >= ( macvlen = convert_string(obj->options.maccharset, CH_UCS2, tmpname, tmpvlen, mactmpname, AFPVOL_U8MNAMELEN*2)) )
620 LOG(log_debug, logtype_afpd, "createvol: Volume '%s' -> Longname: '%s'", name, tmpname);
622 /* check duplicate */
623 for ( volume = Volumes; volume; volume = volume->v_next ) {
624 if (( strcasecmp_w( volume->v_u8mname, u8mtmpname ) == 0 ) || ( strcasecmp_w( volume->v_macname, mactmpname ) == 0 )){
625 if (volume->v_deleted) {
626 volume->v_new = hide = 1;
629 return -1; /* Won't be able to access it, anyway... */
634 if (!( volume = (struct vol *)calloc(1, sizeof( struct vol ))) ) {
635 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
638 if ( NULL == ( volume->v_localname = strdup(name))) {
639 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
644 if ( NULL == ( volume->v_u8mname = strdup_w(u8mtmpname))) {
645 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
650 if ( NULL == ( volume->v_macname = strdup_w(mactmpname))) {
651 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
656 if (!( volume->v_path = (char *)malloc( strlen( path ) + 1 )) ) {
657 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
663 volume->v_name = utf8_encoding()?volume->v_u8mname:volume->v_macname;
664 volume->v_hide = hide;
665 strcpy( volume->v_path, path );
669 #endif /* __svr4__ */
670 /* os X start at 1 and use network order ie. 1 2 3 */
671 volume->v_vid = ++lastvid;
672 volume->v_vid = htons(volume->v_vid);
673 volume->v_vfs_ea = AFPVOL_EA_SYS;
677 /* should we casefold? */
678 volume->v_casefold = options[VOLOPT_CASEFOLD].i_value;
680 /* shift in some flags */
681 volume->v_flags = options[VOLOPT_FLAGS].i_value;
683 if (options[VOLOPT_EA_VFS].i_value)
684 volume->v_vfs_ea = options[VOLOPT_EA_VFS].i_value;
686 volume->v_ad_options = 0;
687 if ((volume->v_flags & AFPVOL_NODEV))
688 volume->v_ad_options |= ADVOL_NODEV;
689 if ((volume->v_flags & AFPVOL_CACHE))
690 volume->v_ad_options |= ADVOL_CACHE;
691 if ((volume->v_flags & AFPVOL_UNIX_PRIV))
692 volume->v_ad_options |= ADVOL_UNIXPRIV;
693 if ((volume->v_flags & AFPVOL_INV_DOTS))
694 volume->v_ad_options |= ADVOL_INVDOTS;
696 if (options[VOLOPT_PASSWORD].c_value)
697 volume->v_password = strdup(options[VOLOPT_PASSWORD].c_value);
699 if (options[VOLOPT_VETO].c_value)
700 volume->v_veto = strdup(options[VOLOPT_VETO].c_value);
702 if (options[VOLOPT_ENCODING].c_value)
703 volume->v_volcodepage = strdup(options[VOLOPT_ENCODING].c_value);
705 if (options[VOLOPT_MACCHARSET].c_value)
706 volume->v_maccodepage = strdup(options[VOLOPT_MACCHARSET].c_value);
708 if (options[VOLOPT_DBPATH].c_value)
709 volume->v_dbpath = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_DBPATH].c_value, pwd, path, name);
711 if (options[VOLOPT_CNIDSCHEME].c_value)
712 volume->v_cnidscheme = strdup(options[VOLOPT_CNIDSCHEME].c_value);
714 if (options[VOLOPT_UMASK].i_value)
715 volume->v_umask = (mode_t)options[VOLOPT_UMASK].i_value;
717 if (options[VOLOPT_DPERM].i_value)
718 volume->v_dperm = (mode_t)options[VOLOPT_DPERM].i_value;
720 if (options[VOLOPT_FPERM].i_value)
721 volume->v_fperm = (mode_t)options[VOLOPT_FPERM].i_value;
723 if (options[VOLOPT_DFLTPERM].i_value)
724 volume->v_perm = (mode_t)options[VOLOPT_DFLTPERM].i_value;
726 if (options[VOLOPT_ADOUBLE].i_value)
727 volume->v_adouble = options[VOLOPT_ADOUBLE].i_value;
729 volume->v_adouble = AD_VERSION;
731 /* Mac to Unix conversion flags*/
732 volume->v_mtou_flags = 0;
733 if (!(volume->v_flags & AFPVOL_NOHEX))
734 volume->v_mtou_flags |= CONV_ESCAPEHEX;
735 if (!(volume->v_flags & AFPVOL_USEDOTS))
736 volume->v_mtou_flags |= CONV_ESCAPEDOTS;
737 if ((volume->v_flags & AFPVOL_EILSEQ))
738 volume->v_mtou_flags |= CONV__EILSEQ;
740 if ((volume->v_casefold & AFPVOL_MTOUUPPER))
741 volume->v_mtou_flags |= CONV_TOUPPER;
742 else if ((volume->v_casefold & AFPVOL_MTOULOWER))
743 volume->v_mtou_flags |= CONV_TOLOWER;
745 /* Unix to Mac conversion flags*/
746 volume->v_utom_flags = CONV_IGNORE | CONV_UNESCAPEHEX;
747 if ((volume->v_casefold & AFPVOL_UTOMUPPER))
748 volume->v_utom_flags |= CONV_TOUPPER;
749 else if ((volume->v_casefold & AFPVOL_UTOMLOWER))
750 volume->v_utom_flags |= CONV_TOLOWER;
752 if ((volume->v_flags & AFPVOL_EILSEQ))
753 volume->v_utom_flags |= CONV__EILSEQ;
758 if (options[VOLOPT_FORCEUID].c_value) {
759 volume->v_forceuid = strdup(options[VOLOPT_FORCEUID].c_value);
761 volume->v_forceuid = NULL; /* set as null so as to return 0 later on */
764 if (options[VOLOPT_FORCEGID].c_value) {
765 volume->v_forcegid = strdup(options[VOLOPT_FORCEGID].c_value);
767 volume->v_forcegid = NULL; /* set as null so as to return 0 later on */
771 if (options[VOLOPT_PREEXEC].c_value)
772 volume->v_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_PREEXEC].c_value, pwd, path, name);
773 volume->v_preexec_close = options[VOLOPT_PREEXEC].i_value;
775 if (options[VOLOPT_POSTEXEC].c_value)
776 volume->v_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_POSTEXEC].c_value, pwd, path, name);
778 if (options[VOLOPT_ROOTPREEXEC].c_value)
779 volume->v_root_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPREEXEC].c_value, pwd, path, name);
780 volume->v_root_preexec_close = options[VOLOPT_ROOTPREEXEC].i_value;
782 if (options[VOLOPT_ROOTPOSTEXEC].c_value)
783 volume->v_root_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPOSTEXEC].c_value, pwd, path, name);
786 volume->v_dperm |= volume->v_perm;
787 volume->v_fperm |= volume->v_perm;
789 volume->v_next = Volumes;
794 /* ---------------- */
795 static char *myfgets( char *buf, int size, FILE *fp)
801 while ((EOF != ( c = getc( fp )) ) && ( size > 1 )) {
802 if ( c == '\n' || c == '\r' ) {
803 if (p != buf && *(p -1) == '\\') {
825 /* check access list. this function wants something of the following
827 * @group,name,name2,@group2,name3
829 * a NULL argument allows everybody to have access.
830 * we return three things:
832 * 0: list exists, but name isn't in it
836 #ifndef NO_REAL_USER_NAME
837 /* authentication is case insensitive
838 * FIXME should we do the same with group name?
840 #define access_strcmp strcasecmp
843 #define access_strcmp strcmp
847 static int accessvol(const char *args, const char *name)
849 char buf[MAXPATHLEN + 1], *p;
855 strlcpy(buf, args, sizeof(buf));
856 if ((p = strtok(buf, ",")) == NULL) /* nothing, return okay */
860 if (*p == '@') { /* it's a group */
861 if ((gr = getgrnam(p + 1)) && gmem(gr->gr_gid))
863 } else if (access_strcmp(p, name) == 0) /* it's a user name */
865 p = strtok(NULL, ",");
871 static int hostaccessvol(int type, const char *volname, const char *args, const AFPObj *obj)
874 char buf[MAXPATHLEN + 1], *p, *b;
875 DSI *dsi = obj->handle;
876 struct sockaddr_storage client;
881 strlcpy(buf, args, sizeof(buf));
882 if ((p = strtok_r(buf, ",", &b)) == NULL) /* nothing, return okay */
885 if (obj->proto != AFPPROTO_DSI)
890 char *ipaddr, *mask_char;
891 struct addrinfo hints, *ai;
893 ipaddr = strtok(p, "/");
894 mask_char = strtok(NULL,"/");
896 /* Get address from string with getaddrinfo */
897 memset(&hints, 0, sizeof hints);
898 hints.ai_family = AF_UNSPEC;
899 hints.ai_socktype = SOCK_STREAM;
900 if ((ret = getaddrinfo(ipaddr, NULL, &hints, &ai)) != 0) {
901 LOG(log_error, logtype_afpd, "hostaccessvol: getaddrinfo: %s\n", gai_strerror(ret));
906 if (mask_char != NULL)
907 mask_int = atoi(mask_char); /* apply_ip_mask does range checking on it */
909 if (ai->ai_family == AF_INET) /* IPv4 */
915 /* Apply mask to addresses */
916 client = dsi->client;
917 apply_ip_mask((struct sockaddr *)&client, mask_int);
918 apply_ip_mask(ai->ai_addr, mask_int);
920 if (compare_ip((struct sockaddr *)&client, ai->ai_addr) == 0) {
921 if (type == VOLOPT_DENIED_HOSTS)
922 LOG(log_info, logtype_afpd, "AFP access denied for client IP '%s' to volume '%s' by denied list",
923 getip_string((struct sockaddr *)&client), volname);
930 p = strtok_r(NULL, ",", &b);
933 if (type == VOLOPT_ALLOWED_HOSTS)
934 LOG(log_info, logtype_afpd, "AFP access denied for client IP '%s' to volume '%s', not in allowed list",
935 getip_string((struct sockaddr *)&dsi->client), volname);
939 static void setextmap(char *ext, char *type, char *creator, int user)
944 if (Extmap == NULL) {
945 if (( Extmap = calloc(1, sizeof( struct extmap ))) == NULL ) {
946 LOG(log_error, logtype_afpd, "setextmap: calloc: %s", strerror(errno) );
951 for ( em = Extmap, cnt = 0; em->em_ext; em++, cnt++) {
952 if ( (strdiacasecmp( em->em_ext, ext )) == 0 ) {
957 if ( em->em_ext == NULL ) {
958 if (!(Extmap = realloc( Extmap, sizeof( struct extmap ) * (cnt +2))) ) {
959 LOG(log_error, logtype_afpd, "setextmap: realloc: %s", strerror(errno) );
962 (Extmap +cnt +1)->em_ext = NULL;
964 } else if ( !user ) {
970 if (!(em->em_ext = strdup( ext))) {
971 LOG(log_error, logtype_afpd, "setextmap: strdup: %s", strerror(errno) );
975 if ( *type == '\0' ) {
976 memcpy(em->em_type, "????", sizeof( em->em_type ));
978 memcpy(em->em_type, type, sizeof( em->em_type ));
980 if ( *creator == '\0' ) {
981 memcpy(em->em_creator, "UNIX", sizeof( em->em_creator ));
983 memcpy(em->em_creator, creator, sizeof( em->em_creator ));
987 /* -------------------------- */
988 static int extmap_cmp(const void *map1, const void *map2)
990 const struct extmap *em1 = map1;
991 const struct extmap *em2 = map2;
992 return strdiacasecmp(em1->em_ext, em2->em_ext);
995 static void sortextmap( void)
1000 if ((em = Extmap) == NULL) {
1003 while (em->em_ext) {
1008 qsort(Extmap, Extmap_cnt, sizeof(struct extmap), extmap_cmp);
1009 if (*Extmap->em_ext == 0) {
1010 /* the first line is really "." the default entry,
1011 * we remove the leading '.' in setextmap
1018 /* ----------------------
1020 static void free_extmap( void)
1025 for ( em = Extmap; em->em_ext; em++) {
1035 /* ----------------------
1037 static int volfile_changed(struct afp_volume_name *p)
1043 name = p->full_name;
1047 if (!stat( name, &st) && st.st_mtime > p->mtime) {
1048 p->mtime = st.st_mtime;
1054 /* ----------------------
1055 * Read a volume configuration file and add the volumes contained within to
1056 * the global volume list. If p2 is non-NULL, the file that is opened is
1059 * Lines that begin with # and blank lines are ignored.
1060 * Volume lines are of the form:
1061 * <unix path> [<volume name>] [allow:<user>,<@group>,...] \
1062 * [codepage:<file>] [casefold:<num>]
1063 * <extension> TYPE [CREATOR]
1065 static int readvolfile(AFPObj *obj, struct afp_volume_name *p1, char *p2, int user, struct passwd *pwent)
1068 char path[ MAXPATHLEN + 1], tmp[ MAXPATHLEN + 1],
1069 volname[ AFPVOL_U8MNAMELEN + 1 ], buf[ BUFSIZ ],
1070 type[ 5 ], creator[ 5 ];
1073 struct vol_option options[VOLOPT_NUM], save_options[VOLOPT_NUM];
1081 strcpy( path, p1->name );
1083 strcat( path, "/" );
1085 if (p1->full_name) {
1086 free(p1->full_name);
1088 p1->full_name = strdup(path);
1091 if (NULL == ( fp = fopen( path, "r" )) ) {
1095 if (fd != -1 && !fstat( fd, &st) ) {
1096 p1->mtime = st.st_mtime;
1099 memset(save_options, 0, sizeof(save_options));
1101 /* Enable some default options for all volumes */
1102 save_options[VOLOPT_FLAGS].i_value |= AFPVOL_CACHE;
1104 while ( myfgets( buf, sizeof( buf ), fp ) != NULL ) {
1105 initline( strlen( buf ), buf );
1106 parseline( sizeof( path ) - 1, path );
1113 /* change the default options for this file */
1114 if (strncmp(path, VOLOPT_DEFAULT, VOLOPT_DEFAULT_LEN) == 0) {
1116 for (i = 0; i < VOLOPT_NUM; i++) {
1117 if (parseline( sizeof( path ) - VOLOPT_DEFAULT_LEN - 1,
1118 path + VOLOPT_DEFAULT_LEN) < 0)
1120 volset(save_options, NULL, tmp, sizeof(tmp) - 1,
1121 path + VOLOPT_DEFAULT_LEN);
1127 if (( p = strchr( path, '/' )) != NULL ) {
1135 if ( u == NULL || *u == '\0' || ( pw = getpwnam( u )) == NULL ) {
1138 strcpy( tmp, pw->pw_dir );
1139 if ( p != NULL && *p != '\0' ) {
1143 /* Tag a user's home directory with their umask. Note, this will
1144 * be overwritten if the user actually specifies a umask: option
1145 * for a '~' volume. */
1146 save_options[VOLOPT_UMASK].i_value = obj->options.save_mask;
1150 /* send path through variable substitution */
1151 if (*path != '~') /* need to copy path to tmp */
1154 pwent = getpwnam(obj->username);
1155 volxlate(obj, path, sizeof(path) - 1, tmp, pwent, NULL, NULL);
1157 /* this is sort of braindead. basically, i want to be
1158 * able to specify things in any order, but i don't want to
1159 * re-write everything.
1161 * currently we have options:
1169 * options:prodos,crlf,noadouble,ro...
1174 * namemask:x,y,!z (not implemented yet)
1176 memcpy(options, save_options, sizeof(options));
1179 /* read in up to VOLOP_NUM possible options */
1180 for (i = 0; i < VOLOPT_NUM; i++) {
1181 if (parseline( sizeof( tmp ) - 1, tmp ) < 0)
1184 volset(options, save_options, volname, sizeof(volname) - 1, tmp);
1187 /* check allow/deny lists:
1188 allow -> either no list (-1), or in list (1)
1189 deny -> either no list (-1), or not in list (0) */
1190 if (accessvol(options[VOLOPT_ALLOW].c_value, obj->username) &&
1191 (accessvol(options[VOLOPT_DENY].c_value, obj->username) < 1) &&
1192 hostaccessvol(VOLOPT_ALLOWED_HOSTS, volname, options[VOLOPT_ALLOWED_HOSTS].c_value, obj) &&
1193 (hostaccessvol(VOLOPT_DENIED_HOSTS, volname, options[VOLOPT_DENIED_HOSTS].c_value, obj) < 1)) {
1195 /* handle read-only behaviour. semantics:
1196 * 1) neither the rolist nor the rwlist exist -> rw
1197 * 2) rolist exists -> ro if user is in it.
1198 * 3) rwlist exists -> ro unless user is in it. */
1199 if (((options[VOLOPT_FLAGS].i_value & AFPVOL_RO) == 0) &&
1200 ((accessvol(options[VOLOPT_ROLIST].c_value,
1201 obj->username) == 1) ||
1202 !accessvol(options[VOLOPT_RWLIST].c_value,
1204 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
1206 /* do variable substitution for volname */
1207 volxlate(obj, tmp, sizeof(tmp) - 1, volname, pwent, path, NULL);
1208 creatvol(obj, pwent, path, tmp, options, p2 != NULL);
1210 volfree(options, save_options);
1214 parseline( sizeof( type ) - 1, type );
1215 parseline( sizeof( creator ) - 1, creator );
1216 setextmap( path, type, creator, user);
1223 volfree(save_options, NULL);
1225 if ( fclose( fp ) != 0 ) {
1226 LOG(log_error, logtype_afpd, "readvolfile: fclose: %s", strerror(errno) );
1232 /* ------------------------------- */
1233 static void volume_free(struct vol *vol)
1235 free(vol->v_localname);
1236 vol->v_localname = NULL;
1237 free(vol->v_u8mname);
1238 vol->v_u8mname = NULL;
1239 free(vol->v_macname);
1240 vol->v_macname = NULL;
1242 free(vol->v_password);
1244 free(vol->v_volcodepage);
1245 free(vol->v_maccodepage);
1246 free(vol->v_cnidscheme);
1247 free(vol->v_dbpath);
1250 free(vol->v_forceuid);
1251 free(vol->v_forcegid);
1252 #endif /* FORCE_UIDGID */
1255 /* ------------------------------- */
1256 static void free_volumes(void )
1259 struct vol *nvol, *ovol;
1261 for ( vol = Volumes; vol; vol = vol->v_next ) {
1262 if (( vol->v_flags & AFPVOL_OPEN ) ) {
1269 for ( vol = Volumes, ovol = NULL; vol; vol = nvol) {
1272 if (vol->v_localname == NULL) {
1273 if (Volumes == vol) {
1278 ovol->v_next = nvol;
1288 /* ------------------------------- */
1289 static void volume_unlink(struct vol *volume)
1291 struct vol *vol, *ovol, *nvol;
1293 if (volume == Volumes) {
1294 Volumes = Volumes->v_next;
1297 for ( vol = Volumes->v_next, ovol = Volumes; vol; vol = nvol) {
1300 if (vol == volume) {
1301 ovol->v_next = nvol;
1310 static int getvolspace(struct vol *vol,
1311 u_int32_t *bfree, u_int32_t *btotal,
1312 VolSpace *xbfree, VolSpace *xbtotal, u_int32_t *bsize)
1316 #ifndef NO_QUOTA_SUPPORT
1317 VolSpace qfree, qtotal;
1320 spaceflag = AFPVOL_GVSMASK & vol->v_flags;
1321 /* report up to 2GB if afp version is < 2.2 (4GB if not) */
1322 maxsize = (vol->v_flags & AFPVOL_A2VOL) ? 0x01fffe00 :
1323 (((afp_version < 22) || (vol->v_flags & AFPVOL_LIMITSIZE))
1324 ? 0x7fffffffL : 0xffffffffL);
1327 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_AFSGVS ) {
1328 if ( afs_getvolspace( vol, xbfree, xbtotal, bsize ) == AFP_OK ) {
1329 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_AFSGVS;
1330 goto getvolspace_done;
1335 if (( rc = ustatfs_getvolspace( vol, xbfree, xbtotal,
1336 bsize)) != AFP_OK ) {
1340 #define min(a,b) ((a)<(b)?(a):(b))
1341 #ifndef NO_QUOTA_SUPPORT
1342 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_UQUOTA ) {
1343 if ( uquota_getvolspace( vol, &qfree, &qtotal, *bsize ) == AFP_OK ) {
1344 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_UQUOTA;
1345 *xbfree = min(*xbfree, qfree);
1346 *xbtotal = min( *xbtotal, qtotal);
1347 goto getvolspace_done;
1351 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_USTATFS;
1354 *bfree = min( *xbfree, maxsize);
1355 *btotal = min( *xbtotal, maxsize);
1359 /* -----------------------
1360 * set volume creation date
1361 * avoid duplicate, well at least it tries
1363 static void vol_setdate(u_int16_t id, struct adouble *adp, time_t date)
1366 struct vol *vol = Volumes;
1368 for ( volume = Volumes; volume; volume = volume->v_next ) {
1369 if (volume->v_vid == id) {
1372 else if ((time_t)(AD_DATE_FROM_UNIX(date)) == volume->v_ctime) {
1374 volume = Volumes; /* restart */
1377 vol->v_ctime = AD_DATE_FROM_UNIX(date);
1378 ad_setdate(adp, AD_DATE_CREATE | AD_DATE_UNIX, date);
1381 /* ----------------------- */
1382 static int getvolparams( u_int16_t bitmap, struct vol *vol, struct stat *st, char *buf, size_t *buflen)
1385 int bit = 0, isad = 1;
1388 u_int32_t bfree, btotal, bsize;
1389 VolSpace xbfree, xbtotal; /* extended bytes */
1390 char *data, *nameoff = NULL;
1393 LOG(log_debug, logtype_afpd, "getvolparams: Volume '%s'", vol->v_localname);
1395 /* courtesy of jallison@whistle.com:
1396 * For MacOS8.x support we need to create the
1397 * .Parent file here if it doesn't exist. */
1399 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1400 if ( ad_open_metadata( vol->v_path, vol_noadouble(vol) | ADFLAGS_DIR, O_CREAT, &ad) < 0 ) {
1402 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1404 } else if (ad_get_MD_flags( &ad ) & O_CREAT) {
1405 slash = strrchr( vol->v_path, '/' );
1409 slash = vol->v_path;
1410 if (ad_getentryoff(&ad, ADEID_NAME)) {
1411 ad_setentrylen( &ad, ADEID_NAME, strlen( slash ));
1412 memcpy(ad_entry( &ad, ADEID_NAME ), slash,
1413 ad_getentrylen( &ad, ADEID_NAME ));
1415 vol_setdate(vol->v_vid, &ad, st->st_mtime);
1419 if (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0)
1420 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1422 vol->v_ctime = aint;
1425 if (( bitmap & ( (1<<VOLPBIT_BFREE)|(1<<VOLPBIT_BTOTAL) |
1426 (1<<VOLPBIT_XBFREE)|(1<<VOLPBIT_XBTOTAL) |
1427 (1<<VOLPBIT_BSIZE)) ) != 0 ) {
1428 if ( getvolspace( vol, &bfree, &btotal, &xbfree, &xbtotal,
1429 &bsize) != AFP_OK ) {
1431 ad_close( &ad, ADFLAGS_HF );
1433 return( AFPERR_PARAM );
1438 while ( bitmap != 0 ) {
1439 while (( bitmap & 1 ) == 0 ) {
1447 /* check for read-only.
1448 * NOTE: we don't actually set the read-only flag unless
1449 * it's passed in that way as it's possible to mount
1450 * a read-write filesystem under a read-only one. */
1451 if ((vol->v_flags & AFPVOL_RO) ||
1452 ((utime(vol->v_path, NULL) < 0) && (errno == EROFS))) {
1453 ashort |= VOLPBIT_ATTR_RO;
1455 /* prior 2.1 only VOLPBIT_ATTR_RO is defined */
1456 if (afp_version > 20) {
1457 if (0 == (vol->v_flags & AFPVOL_NOFILEID) && vol->v_cdb != NULL &&
1458 (vol->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1459 ashort |= VOLPBIT_ATTR_FILEID;
1461 ashort |= VOLPBIT_ATTR_CATSEARCH;
1463 if (afp_version >= 30) {
1464 ashort |= VOLPBIT_ATTR_UTF8;
1465 if (vol->v_flags & AFPVOL_UNIX_PRIV)
1466 ashort |= VOLPBIT_ATTR_UNIXPRIV;
1467 if (vol->v_flags & AFPVOL_TM)
1468 ashort |= VOLPBIT_ATTR_TM;
1470 if (afp_version >= 32) {
1471 ashort |= VOLPBIT_ATTR_EXT_ATTRS;
1472 if (vol->v_flags & AFPVOL_ACLS)
1473 ashort |= VOLPBIT_ATTR_ACLS;
1477 ashort = htons(ashort);
1478 memcpy(data, &ashort, sizeof( ashort ));
1479 data += sizeof( ashort );
1483 ashort = htons( AFPVOLSIG_DEFAULT );
1484 memcpy(data, &ashort, sizeof( ashort ));
1485 data += sizeof( ashort );
1488 case VOLPBIT_CDATE :
1489 aint = vol->v_ctime;
1490 memcpy(data, &aint, sizeof( aint ));
1491 data += sizeof( aint );
1494 case VOLPBIT_MDATE :
1495 if ( st->st_mtime > vol->v_mtime ) {
1496 vol->v_mtime = st->st_mtime;
1498 aint = AD_DATE_FROM_UNIX(vol->v_mtime);
1499 memcpy(data, &aint, sizeof( aint ));
1500 data += sizeof( aint );
1503 case VOLPBIT_BDATE :
1504 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1505 aint = AD_DATE_START;
1506 memcpy(data, &aint, sizeof( aint ));
1507 data += sizeof( aint );
1511 memcpy(data, &vol->v_vid, sizeof( vol->v_vid ));
1512 data += sizeof( vol->v_vid );
1515 case VOLPBIT_BFREE :
1516 bfree = htonl( bfree );
1517 memcpy(data, &bfree, sizeof( bfree ));
1518 data += sizeof( bfree );
1521 case VOLPBIT_BTOTAL :
1522 btotal = htonl( btotal );
1523 memcpy(data, &btotal, sizeof( btotal ));
1524 data += sizeof( btotal );
1527 #ifndef NO_LARGE_VOL_SUPPORT
1528 case VOLPBIT_XBFREE :
1529 xbfree = hton64( xbfree );
1530 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1531 bcopy(&xbfree, data, sizeof(xbfree));
1532 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1533 memcpy(data, &xbfree, sizeof( xbfree ));
1534 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1535 data += sizeof( xbfree );
1538 case VOLPBIT_XBTOTAL :
1539 xbtotal = hton64( xbtotal );
1540 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1541 bcopy(&xbtotal, data, sizeof(xbtotal));
1542 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1543 memcpy(data, &xbtotal, sizeof( xbtotal ));
1544 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1545 data += sizeof( xbfree );
1547 #endif /* ! NO_LARGE_VOL_SUPPORT */
1551 data += sizeof( u_int16_t );
1554 case VOLPBIT_BSIZE: /* block size */
1555 bsize = htonl(bsize);
1556 memcpy(data, &bsize, sizeof(bsize));
1557 data += sizeof(bsize);
1562 ad_close( &ad, ADFLAGS_HF );
1564 return( AFPERR_BITMAP );
1570 ashort = htons( data - buf );
1571 memcpy(nameoff, &ashort, sizeof( ashort ));
1572 /* name is always in mac charset */
1573 aint = ucs2_to_charset( vol->v_maccharset, vol->v_macname, data+1, AFPVOL_MACNAMELEN + 1);
1583 ad_close_metadata( &ad);
1585 *buflen = data - buf;
1589 /* ------------------------- */
1590 static int stat_vol(u_int16_t bitmap, struct vol *vol, char *rbuf, size_t *rbuflen)
1596 if ( stat( vol->v_path, &st ) < 0 ) {
1598 return( AFPERR_PARAM );
1600 /* save the volume device number */
1601 vol->v_dev = st.st_dev;
1603 buflen = *rbuflen - sizeof( bitmap );
1604 if (( ret = getvolparams( bitmap, vol, &st,
1605 rbuf + sizeof( bitmap ), &buflen )) != AFP_OK ) {
1609 *rbuflen = buflen + sizeof( bitmap );
1610 bitmap = htons( bitmap );
1611 memcpy(rbuf, &bitmap, sizeof( bitmap ));
1616 /* ------------------------------- */
1617 void load_volumes(AFPObj *obj)
1619 struct passwd *pwent;
1624 /* check files date */
1625 if (obj->options.defaultvol.loaded) {
1626 changed = volfile_changed(&obj->options.defaultvol);
1628 if (obj->options.systemvol.loaded) {
1629 changed |= volfile_changed(&obj->options.systemvol);
1631 if (obj->options.uservol.loaded) {
1632 changed |= volfile_changed(&obj->options.uservol);
1641 pwent = getpwnam(obj->username);
1642 if ( (obj->options.flags & OPTION_USERVOLFIRST) == 0 ) {
1643 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent);
1646 if ((*obj->username == '\0') || (obj->options.flags & OPTION_NOUSERVOL)) {
1647 readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent);
1650 * Read user's AppleVolumes or .AppleVolumes file
1651 * If neither are readable, read the default volumes file. if
1652 * that doesn't work, create a user share.
1654 if (obj->options.uservol.name) {
1655 free(obj->options.uservol.name);
1657 obj->options.uservol.name = strdup(pwent->pw_dir);
1658 if ( readvolfile(obj, &obj->options.uservol, "AppleVolumes", 1, pwent) < 0 &&
1659 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 obj->options.defaultvol.name != NULL ) {
1663 if (readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent) < 0)
1664 creatvol(obj, pwent, pwent->pw_dir, NULL, NULL, 1);
1667 if ( obj->options.flags & OPTION_USERVOLFIRST ) {
1668 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent );
1671 if ( obj->options.closevol ) {
1674 for ( vol = Volumes; vol; vol = vol->v_next ) {
1675 if (vol->v_deleted && !vol->v_new ) {
1684 /* ------------------------------- */
1685 int afp_getsrvrparms(AFPObj *obj, char *ibuf _U_, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
1698 for ( vcnt = 0, volume = Volumes; volume; volume = volume->v_next ) {
1699 if (!(volume->v_flags & AFPVOL_NOSTAT)) {
1702 if ( stat( volume->v_path, &st ) < 0 ) {
1703 LOG(log_info, logtype_afpd, "afp_getsrvrparms(%s): stat: %s",
1704 volume->v_path, strerror(errno) );
1705 continue; /* can't access directory */
1707 if (!S_ISDIR(st.st_mode)) {
1708 continue; /* not a dir */
1710 accessmode(volume->v_path, &ma, NULL, &st);
1711 if ((ma.ma_user & (AR_UREAD | AR_USEARCH)) != (AR_UREAD | AR_USEARCH)) {
1712 continue; /* no r-x access */
1715 if (volume->v_hide) {
1716 continue; /* config file changed but the volume was mounted */
1719 if (utf8_encoding()) {
1720 len = ucs2_to_charset_allocate(CH_UTF8_MAC, &namebuf, volume->v_u8mname);
1722 len = ucs2_to_charset_allocate(obj->options.maccharset, &namebuf, volume->v_macname);
1725 if (len == (size_t)-1)
1728 /* set password bit if there's a volume password */
1729 *data = (volume->v_password) ? AFPSRVR_PASSWD : 0;
1731 /* Apple 2 clients running ProDOS-8 expect one volume to have
1732 bit 0 of this byte set. They will not recognize anything
1733 on the server unless this is the case. I have not
1734 completely worked this out, but it's related to booting
1735 from the server. Support for that function is a ways
1736 off.. <shirsch@ibm.net> */
1737 *data |= (volume->v_flags & AFPVOL_A2VOL) ? AFPSRVR_CONFIGINFO : 0;
1738 *data++ |= 0; /* UNIX PRIVS BIT ..., OSX doesn't seem to use it, so we don't either */
1740 memcpy(data, namebuf, len );
1746 *rbuflen = data - rbuf;
1748 if ( gettimeofday( &tv, NULL ) < 0 ) {
1749 LOG(log_error, logtype_afpd, "afp_getsrvrparms(%s): gettimeofday: %s", volume->v_path, strerror(errno) );
1751 return AFPERR_PARAM;
1753 tv.tv_sec = AD_DATE_FROM_UNIX(tv.tv_sec);
1754 memcpy(data, &tv.tv_sec, sizeof( u_int32_t));
1755 data += sizeof( u_int32_t);
1760 /* ------------------------- */
1761 static int volume_codepage(AFPObj *obj, struct vol *volume)
1763 struct charset_functions *charset;
1766 if (!volume->v_volcodepage)
1767 volume->v_volcodepage = strdup("UTF8");
1769 if ( (charset_t) -1 == ( volume->v_volcharset = add_charset(volume->v_volcodepage)) ) {
1770 LOG (log_error, logtype_afpd, "Setting codepage %s as volume codepage failed", volume->v_volcodepage);
1774 if ( NULL == (charset = find_charset_functions(volume->v_volcodepage)) || charset->flags & CHARSET_ICONV ) {
1775 LOG (log_warning, logtype_afpd, "WARNING: volume encoding %s is *not* supported by netatalk, expect problems !!!!", volume->v_volcodepage);
1778 if (!volume->v_maccodepage)
1779 volume->v_maccodepage = strdup(obj->options.maccodepage);
1781 if ( (charset_t) -1 == ( volume->v_maccharset = add_charset(volume->v_maccodepage)) ) {
1782 LOG (log_error, logtype_afpd, "Setting codepage %s as mac codepage failed", volume->v_maccodepage);
1786 if ( NULL == ( charset = find_charset_functions(volume->v_maccodepage)) || ! (charset->flags & CHARSET_CLIENT) ) {
1787 LOG (log_error, logtype_afpd, "Fatal error: mac charset %s not supported", volume->v_maccodepage);
1790 volume->v_kTextEncoding = htonl(charset->kTextEncoding);
1794 /* ------------------------- */
1795 static int volume_openDB(struct vol *volume)
1799 if ((volume->v_flags & AFPVOL_NODEV)) {
1800 flags |= CNID_FLAG_NODEV;
1803 if (volume->v_cnidscheme == NULL) {
1804 volume->v_cnidscheme = strdup(DEFAULT_CNID_SCHEME);
1805 LOG(log_info, logtype_afpd, "Volume %s use CNID scheme %s.", volume->v_path, volume->v_cnidscheme);
1807 if (volume->v_dbpath)
1808 volume->v_cdb = cnid_open (volume->v_dbpath, volume->v_umask, volume->v_cnidscheme, flags);
1810 volume->v_cdb = cnid_open (volume->v_path, volume->v_umask, volume->v_cnidscheme, flags);
1812 if (!volume->v_cdb) {
1813 flags |= CNID_FLAG_MEMORY;
1814 LOG(log_error, logtype_afpd, "Reopen volume %s using in memory temporary CNID DB.", volume->v_path);
1815 volume->v_cdb = cnid_open (volume->v_path, volume->v_umask, "tdb", flags);
1817 /* kill ourself with SIGUSR2 aka msg pending */
1818 if (volume->v_cdb) {
1819 setmessage("Something wrong with the volume's DB ... FIXME with a better msg");
1820 kill(getpid(), SIGUSR2);
1821 /* XXX desactivate cachecnid ? */
1826 return (!volume->v_cdb)?-1:0;
1830 Check if the underlying filesystem supports EAs for ea:sys volumes.
1831 If not, switch to ea:ad.
1833 static void check_ea_sys_support(struct vol *vol)
1835 char buf[ATTRNAMEBUFSIZ];
1837 if (vol->v_vfs_ea == AFPVOL_EA_SYS) {
1838 if ((sys_llistxattr(".AppleDesktop", buf, ATTRNAMEBUFSIZ)) == -1
1840 LOG(log_warning, logtype_afpd, "Volume \"%s\" does not support Extended Attributes. Using ad format instead.",
1842 vol->v_vfs_ea = AFPVOL_EA_AD;
1848 /* -------------------------
1849 * we are the user here
1851 int afp_openvol(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
1862 char path[ MAXPATHLEN + 1];
1868 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1869 bitmap = ntohs( bitmap );
1870 ibuf += sizeof( bitmap );
1873 if (( bitmap & (1<<VOLPBIT_VID)) == 0 ) {
1874 return AFPERR_BITMAP;
1877 len = (unsigned char)*ibuf++;
1878 volname = obj->oldtmp;
1880 if ((volname_tmp = strchr(volname,'+')) != NULL)
1881 volname = volname_tmp+1;
1883 if (utf8_encoding()) {
1884 namelen = convert_string(CH_UTF8_MAC, CH_UCS2, ibuf, len, volname, sizeof(obj->oldtmp));
1886 namelen = convert_string(obj->options.maccharset, CH_UCS2, ibuf, len, volname, sizeof(obj->oldtmp));
1889 if ( namelen <= 0) {
1890 return AFPERR_PARAM;
1894 if ((len + 1) & 1) /* pad to an even boundary */
1899 for ( volume = Volumes; volume; volume = volume->v_next ) {
1900 if ( strcasecmp_w( (ucs2_t*) volname, volume->v_name ) == 0 ) {
1905 if ( volume == NULL ) {
1906 return AFPERR_PARAM;
1909 /* check for a volume password */
1910 if (volume->v_password && strncmp(ibuf, volume->v_password, VOLPASSLEN)) {
1911 return AFPERR_ACCESS;
1914 if (( volume->v_flags & AFPVOL_OPEN ) ) {
1915 /* the volume is already open */
1917 set_uidgid ( volume );
1919 return stat_vol(bitmap, volume, rbuf, rbuflen);
1922 if (volume->v_root_preexec) {
1923 if ((ret = afprun(1, volume->v_root_preexec, NULL)) && volume->v_root_preexec_close) {
1924 LOG(log_error, logtype_afpd, "afp_openvol(%s): root preexec : %d", volume->v_path, ret );
1930 set_uidgid ( volume );
1933 if (volume->v_preexec) {
1934 if ((ret = afprun(0, volume->v_preexec, NULL)) && volume->v_preexec_close) {
1935 LOG(log_error, logtype_afpd, "afp_openvol(%s): preexec : %d", volume->v_path, ret );
1940 if ( stat( volume->v_path, &st ) < 0 ) {
1941 return AFPERR_PARAM;
1944 if ( chdir( volume->v_path ) < 0 ) {
1945 return AFPERR_PARAM;
1948 if ( NULL == getcwd(path, MAXPATHLEN)) {
1949 /* shouldn't be fatal but it will fail later */
1950 LOG(log_error, logtype_afpd, "afp_openvol(%s): volume pathlen too long", volume->v_path);
1954 if (volume_codepage(obj, volume) < 0) {
1959 /* initialize volume variables
1962 if (utf8_encoding()) {
1963 volume->max_filename = 255;
1966 volume->max_filename = MACFILELEN;
1969 volume->v_dir = volume->v_root = NULL;
1970 volume->v_hash = NULL;
1972 volume->v_flags |= AFPVOL_OPEN;
1973 volume->v_cdb = NULL;
1975 if (utf8_encoding()) {
1976 len = convert_string_allocate(CH_UCS2, CH_UTF8_MAC, volume->v_u8mname, namelen, &vol_mname);
1978 len = convert_string_allocate(CH_UCS2, obj->options.maccharset, volume->v_macname, namelen, &vol_mname);
1980 if ( !vol_mname || len <= 0) {
1985 if ((vol_uname = strrchr(path, '/')) == NULL)
1987 else if (*(vol_uname + 1) != '\0')
1990 if ((dir = dirnew(vol_mname, vol_uname) ) == NULL) {
1992 LOG(log_error, logtype_afpd, "afp_openvol(%s): malloc: %s", volume->v_path, strerror(errno) );
1998 dir->d_did = DIRDID_ROOT;
1999 dir->d_color = DIRTREE_COLOR_BLACK; /* root node is black */
2000 dir->d_m_name_ucs2 = strdup_w(volume->v_name);
2001 volume->v_dir = volume->v_root = dir;
2002 volume->v_curdir = NULL;
2003 volume->v_hash = dirhash();
2005 curdir = volume->v_dir;
2006 if (volume_openDB(volume) < 0) {
2007 LOG(log_error, logtype_afpd, "Fatal error: cannot open CNID or invalid CNID backend for %s: %s",
2008 volume->v_path, volume->v_cnidscheme);
2013 ret = stat_vol(bitmap, volume, rbuf, rbuflen);
2014 if (ret == AFP_OK) {
2016 if (!(volume->v_flags & AFPVOL_RO)) {
2017 handle_special_folders( volume );
2018 check_ea_sys_support(volume);
2019 savevolinfo(volume, Cnid_srv, Cnid_port);
2023 * If you mount a volume twice, the second time the trash appears on
2024 * the desk-top. That's because the Mac remembers the DID for the
2025 * trash (even for volumes in different zones, on different servers).
2026 * Just so this works better, we prime the DID cache with the trash,
2027 * fixing the trash at DID 17.
2028 * FIXME (RL): should it be done inside a CNID backend ? (always returning Trash DID when asked) ?
2030 if ((volume->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
2032 /* FIXME find db time stamp */
2033 if (cnid_getstamp(volume->v_cdb, volume->v_stamp, sizeof(volume->v_stamp)) < 0) {
2034 LOG (log_error, logtype_afpd,
2035 "afp_openvol(%s): Fatal error: Unable to get stamp value from CNID backend",
2043 cname( volume, volume->v_dir, &p );
2049 if (volume->v_dir) {
2050 hash_free( volume->v_hash);
2051 dirfree( volume->v_dir );
2052 volume->v_dir = volume->v_root = NULL;
2055 volume->v_flags &= ~AFPVOL_OPEN;
2056 if (volume->v_cdb != NULL) {
2057 cnid_close(volume->v_cdb);
2058 volume->v_cdb = NULL;
2064 /* ------------------------- */
2065 static void closevol(struct vol *vol)
2070 hash_free( vol->v_hash);
2071 dirfree( vol->v_root );
2073 if (vol->v_cdb != NULL) {
2074 cnid_close(vol->v_cdb);
2078 if (vol->v_postexec) {
2079 afprun(0, vol->v_postexec, NULL);
2081 if (vol->v_root_postexec) {
2082 afprun(1, vol->v_root_postexec, NULL);
2086 /* ------------------------- */
2087 void close_all_vol(void)
2091 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
2092 if ( (ovol->v_flags & AFPVOL_OPEN) ) {
2093 ovol->v_flags &= ~AFPVOL_OPEN;
2099 /* ------------------------- */
2100 static void deletevol(struct vol *vol)
2104 vol->v_flags &= ~AFPVOL_OPEN;
2105 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
2106 if ( (ovol->v_flags & AFPVOL_OPEN) ) {
2110 if ( ovol != NULL ) {
2111 /* Even if chdir fails, we can't say afp_closevol fails. */
2112 if ( chdir( ovol->v_path ) == 0 ) {
2113 curdir = ovol->v_dir;
2118 if (vol->v_deleted) {
2119 showvol(vol->v_name);
2126 /* ------------------------- */
2127 int afp_closevol(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2134 memcpy(&vid, ibuf, sizeof( vid ));
2135 if (NULL == ( vol = getvolbyvid( vid )) ) {
2136 return( AFPERR_PARAM );
2144 /* ------------------------- */
2145 struct vol *getvolbyvid(const u_int16_t vid )
2149 for ( vol = Volumes; vol; vol = vol->v_next ) {
2150 if ( vid == vol->v_vid ) {
2154 if ( vol == NULL || ( vol->v_flags & AFPVOL_OPEN ) == 0 ) {
2160 #endif /* FORCE_UIDGID */
2165 /* ------------------------ */
2166 static int ext_cmp_key(const void *key, const void *obj)
2168 const char *p = key;
2169 const struct extmap *em = obj;
2170 return strdiacasecmp(p, em->em_ext);
2172 struct extmap *getextmap(const char *path)
2177 if (!Extmap_cnt || NULL == ( p = strrchr( path, '.' )) ) {
2178 return( Defextmap );
2182 return( Defextmap );
2184 em = bsearch(p, Extmap, Extmap_cnt, sizeof(struct extmap), ext_cmp_key);
2188 return( Defextmap );
2192 /* ------------------------- */
2193 struct extmap *getdefextmap(void)
2195 return( Defextmap );
2198 /* --------------------------
2199 poll if a volume is changed by other processes.
2201 0 no attention msg sent
2202 1 attention msg sent
2203 -1 error (socket closed)
2205 Note: if attention return -1 no packet has been
2206 sent because the buffer is full, we don't care
2207 either there's no reader or there's a lot of
2208 traffic and another pollvoltime will follow
2210 int pollvoltime(AFPObj *obj)
2217 if (!(afp_version > 21 && obj->options.server_notif))
2220 if ( gettimeofday( &tv, NULL ) < 0 )
2223 for ( vol = Volumes; vol; vol = vol->v_next ) {
2224 if ( (vol->v_flags & AFPVOL_OPEN) && vol->v_mtime + 30 < tv.tv_sec) {
2225 if ( !stat( vol->v_path, &st ) && vol->v_mtime != st.st_mtime ) {
2226 vol->v_mtime = st.st_mtime;
2227 if (!obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED))
2236 /* ------------------------- */
2237 void setvoltime(AFPObj *obj, struct vol *vol)
2241 /* just looking at vol->v_mtime is broken seriously since updates
2242 * from other users afpd processes never are seen.
2243 * This is not the most elegant solution (a shared memory between
2244 * the afpd processes would come closer)
2247 if ( gettimeofday( &tv, NULL ) < 0 ) {
2248 LOG(log_error, logtype_afpd, "setvoltime(%s): gettimeofday: %s", vol->v_path, strerror(errno) );
2251 if( utime( vol->v_path, NULL ) < 0 ) {
2252 /* write of time failed ... probably a read only filesys,
2253 * where no other users can interfere, so there's no issue here
2257 /* a little granularity */
2258 if (vol->v_mtime < tv.tv_sec) {
2259 vol->v_mtime = tv.tv_sec;
2260 /* or finder doesn't update free space
2261 * XXX is it still true with newer OSX?
2263 if (afp_version > 21 && obj->options.server_notif) {
2264 obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED);
2269 /* ------------------------- */
2270 int afp_getvolparams(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_,char *rbuf, size_t *rbuflen)
2273 u_int16_t vid, bitmap;
2276 memcpy(&vid, ibuf, sizeof( vid ));
2277 ibuf += sizeof( vid );
2278 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2279 bitmap = ntohs( bitmap );
2281 if (NULL == ( vol = getvolbyvid( vid )) ) {
2283 return( AFPERR_PARAM );
2286 return stat_vol(bitmap, vol, rbuf, rbuflen);
2289 /* ------------------------- */
2290 int afp_setvolparams(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2294 u_int16_t vid, bitmap;
2300 memcpy(&vid, ibuf, sizeof( vid ));
2301 ibuf += sizeof( vid );
2302 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2303 bitmap = ntohs( bitmap );
2304 ibuf += sizeof(bitmap);
2306 if (( vol = getvolbyvid( vid )) == NULL ) {
2307 return( AFPERR_PARAM );
2310 if ((vol->v_flags & AFPVOL_RO))
2311 return AFPERR_VLOCK;
2313 /* we can only set the backup date. */
2314 if (bitmap != (1 << VOLPBIT_BDATE))
2315 return AFPERR_BITMAP;
2317 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2318 if ( ad_open( vol->v_path, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR,
2321 return AFPERR_VLOCK;
2323 return AFPERR_ACCESS;
2326 memcpy(&aint, ibuf, sizeof(aint));
2327 ad_setdate(&ad, AD_DATE_BACKUP, aint);
2329 ad_close(&ad, ADFLAGS_HF);
2333 /* ------------------------- */
2334 int wincheck(const struct vol *vol, const char *path)
2338 if (!(vol->v_flags & AFPVOL_MSWINDOWS))
2341 /* empty paths are not allowed */
2342 if ((len = strlen(path)) == 0)
2345 /* leading or trailing whitespaces are not allowed, carriage returns
2346 * and probably other whitespace is okay, tabs are not allowed
2348 if ((path[0] == ' ') || (path[len-1] == ' '))
2351 /* certain characters are not allowed */
2352 if (strpbrk(path, MSWINDOWS_BADCHARS))
2355 /* everything else is okay */
2361 * precreate a folder
2362 * this is only intended for folders in the volume root
2363 * It will *not* work if the folder name contains extended characters
2365 static int create_special_folder (const struct vol *vol, const struct _special_folder *folder)
2374 p = (char *) malloc ( strlen(vol->v_path)+strlen(folder->name)+2);
2376 LOG(log_error, logtype_afpd,"malloc failed");
2380 q=strdup(folder->name);
2382 LOG(log_error, logtype_afpd,"malloc failed");
2386 strcpy(p, vol->v_path);
2391 if ((vol->v_casefold & AFPVOL_MTOUUPPER))
2393 else if ((vol->v_casefold & AFPVOL_MTOULOWER))
2399 if ( (ret = stat( p, &st )) < 0 ) {
2400 if (folder->precreate) {
2401 if (ad_mkdir(p, folder->mode)) {
2402 LOG(log_debug, logtype_afpd,"Creating '%s' failed in %s: %s", p, vol->v_path, strerror(errno));
2411 if ( !ret && folder->hide) {
2413 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2414 if (ad_open( p, vol_noadouble(vol) | ADFLAGS_HF|ADFLAGS_DIR,
2415 O_RDWR|O_CREAT, 0666, &ad) < 0) {
2420 if ((ad_get_HF_flags( &ad ) & O_CREAT) ) {
2421 if (ad_getentryoff(&ad, ADEID_NAME)) {
2422 ad_setentrylen( &ad, ADEID_NAME, strlen(folder->name));
2423 memcpy(ad_entry( &ad, ADEID_NAME ), folder->name,
2424 ad_getentrylen( &ad, ADEID_NAME ));
2428 ad_getattr(&ad, &attr);
2429 attr |= htons( ntohs( attr ) | ATTRBIT_INVISIBLE );
2430 ad_setattr(&ad, attr);
2432 /* do the same with the finder info */
2433 if (ad_entry(&ad, ADEID_FINDERI)) {
2434 memcpy(&attr, ad_entry(&ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF, sizeof(attr));
2435 attr |= htons(FINDERINFO_INVISIBLE);
2436 memcpy(ad_entry(&ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF,&attr, sizeof(attr));
2440 ad_close( &ad, ADFLAGS_HF );
2447 static void handle_special_folders (const struct vol * vol)
2449 const _special_folder *p = &special_folders[0];
2451 if ((vol->v_flags & AFPVOL_RO))
2454 for (; p->name != NULL; p++) {
2455 create_special_folder (vol, p);