2 * $Id: volume.c,v 1.90 2009-09-11 09:14:16 franklahm Exp $
4 * Copyright (c) 1990,1993 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
10 #endif /* HAVE_CONFIG_H */
26 #else /* STDC_HEADERS */
30 #endif /* HAVE_STRCHR */
31 char *strchr (), *strrchr ();
33 #define memcpy(d,s,n) bcopy ((s), (d), (n))
34 #define memmove(d,s,n) bcopy ((s), (d), (n))
35 #endif /* ! HAVE_MEMCPY */
36 #endif /* STDC_HEADERS */
37 #include <sys/param.h>
38 #include <sys/socket.h>
39 #include <netinet/in.h>
40 #include <arpa/inet.h>
41 #include <atalk/asp.h>
42 #include <atalk/dsi.h>
43 #include <atalk/adouble.h>
44 #include <atalk/afp.h>
45 #include <atalk/util.h>
46 #include <atalk/volinfo.h>
47 #include <atalk/logger.h>
49 #include <atalk/cnid.h>
53 #include "directory.h"
60 extern int afprun(int root, char *cmd, int *outfd);
63 #define MIN(a, b) ((a) < (b) ? (a) : (b))
66 #ifndef NO_LARGE_VOL_SUPPORT
67 #if BYTE_ORDER == BIG_ENDIAN
70 #else /* BYTE_ORDER == BIG_ENDIAN */
71 #define hton64(x) ((u_int64_t) (htonl(((x) >> 32) & 0xffffffffLL)) | \
72 (u_int64_t) ((htonl(x) & 0xffffffffLL) << 32))
73 #define ntoh64(x) (hton64(x))
74 #endif /* BYTE_ORDER == BIG_ENDIAN */
75 #endif /* ! NO_LARGE_VOL_SUPPORT */
77 static struct vol *Volumes = NULL;
78 static u_int16_t lastvid = 0;
79 static char *Trash = "\02\024Network Trash Folder";
81 static struct extmap *Extmap = NULL, *Defextmap = NULL;
82 static int Extmap_cnt;
83 static void free_extmap(void);
85 #define VOLOPT_ALLOW 0 /* user allow list */
86 #define VOLOPT_DENY 1 /* user deny list */
87 #define VOLOPT_RWLIST 2 /* user rw list */
88 #define VOLOPT_ROLIST 3 /* user ro list */
89 #define VOLOPT_PASSWORD 4 /* volume password */
90 #define VOLOPT_CASEFOLD 5 /* character case mangling */
91 #define VOLOPT_FLAGS 6 /* various flags */
92 #define VOLOPT_DBPATH 7 /* path to database */
93 #define VOLOPT_MAPCHARS 8 /* does mtou and utom mappings. syntax:
94 m and u can be double-byte hex
99 !u -> make u illegal always
100 ~u -> make u illegal only as the first
101 part of a double-byte character.
103 #define VOLOPT_VETO 10 /* list of veto filespec */
104 #define VOLOPT_PREEXEC 11 /* preexec command */
105 #define VOLOPT_ROOTPREEXEC 12 /* root preexec command */
107 #define VOLOPT_POSTEXEC 13 /* postexec command */
108 #define VOLOPT_ROOTPOSTEXEC 14 /* root postexec command */
110 #define VOLOPT_ENCODING 15 /* mac encoding (pre OSX)*/
111 #define VOLOPT_MACCHARSET 16
112 #define VOLOPT_CNIDSCHEME 17
113 #define VOLOPT_ADOUBLE 18 /* adouble version */
118 #define VOLOPT_FORCEUID 19 /* force uid for username x */
119 #define VOLOPT_FORCEGID 20 /* force gid for group x */
120 #endif /* FORCE_UIDGID */
122 #define VOLOPT_UMASK 21
123 #define VOLOPT_ALLOWED_HOSTS 22
124 #define VOLOPT_DENIED_HOSTS 23
125 #define VOLOPT_DPERM 24 /* dperm default directories perms */
126 #define VOLOPT_FPERM 25 /* fperm default files perms */
127 #define VOLOPT_DFLTPERM 26 /* perm */
129 #define VOLOPT_MAX (VOLOPT_DFLTPERM +1)
131 #define VOLOPT_NUM (VOLOPT_MAX + 1)
134 #define VOLOPT_DEFAULT ":DEFAULT:"
135 #define VOLOPT_DEFAULT_LEN 9
141 typedef struct _special_folder {
148 static const _special_folder special_folders[] = {
149 {"Network Trash Folder", 1, 0777, 1},
150 {"Temporary Items", 1, 0777, 1},
151 {".AppleDesktop", 1, 0777, 0},
153 {"TheFindByContentFolder", 0, 0, 1},
154 {"TheVolumeSettingsFolder", 0, 0, 1},
158 typedef struct _volopt_name {
159 const u_int32_t option;
164 /* Keep in sync with libatalk/util/volinfo.c ! */
165 static const _vol_opt_name vol_opt_names[] = {
166 {AFPVOL_A2VOL, "PRODOS"}, /* prodos volume */
167 {AFPVOL_CRLF, "CRLF"}, /* cr/lf translation */
168 {AFPVOL_NOADOUBLE, "NOADOUBLE"}, /* don't create .AppleDouble by default */
169 {AFPVOL_RO, "READONLY"}, /* read-only volume */
170 {AFPVOL_MSWINDOWS, "MSWINDOWS"}, /* deal with ms-windows yuckiness. this is going away. */
171 {AFPVOL_NOHEX, "NOHEX"}, /* don't do :hex translation */
172 {AFPVOL_USEDOTS, "USEDOTS"}, /* use real dots */
173 {AFPVOL_LIMITSIZE, "LIMITSIZE"}, /* limit size for older macs */
174 {AFPVOL_MAPASCII, "MAPASCII"}, /* map the ascii range as well */
175 {AFPVOL_DROPBOX, "DROPBOX"}, /* dropkludge dropbox support */
176 {AFPVOL_NOFILEID, "NOFILEID"}, /* don't advertise createid resolveid and deleteid calls */
177 {AFPVOL_NOSTAT, "NOSTAT"}, /* advertise the volume even if we can't stat() it
178 * maybe because it will be mounted later in preexec */
179 {AFPVOL_UNIX_PRIV, "UNIXPRIV"}, /* support unix privileges */
180 {AFPVOL_NODEV, "NODEV"}, /* always use 0 for device number in cnid calls */
181 {AFPVOL_CASEINSEN, "CASEINSENSITIVE"}, /* volume is case insensitive */
182 {AFPVOL_EILSEQ, "ILLEGALSEQ"}, /* encode illegal sequence */
183 {AFPVOL_CACHE, "CACHEID"}, /* Use adouble v2 CNID caching. Default: yes */
184 {AFPVOL_EXT_ATTRS, "EXT_ATTRS"}, /* Vol supports Extened Attributes */
185 {AFPVOL_ACLS, "ACLS"}, /* Vol supports ACLs */
186 {AFPVOL_TM, "TM"}, /* Set "kSupportsTMLockSteal" is volume attributes */
190 static const _vol_opt_name vol_opt_casefold[] = {
191 {AFPVOL_MTOUUPPER, "MTOULOWER"},
192 {AFPVOL_MTOULOWER, "MTOULOWER"},
193 {AFPVOL_UTOMUPPER, "UTOMUPPER"},
194 {AFPVOL_UTOMLOWER, "UTOMLOWER"},
198 static void handle_special_folders (const struct vol *);
199 static int savevoloptions (const struct vol *);
200 static void deletevol(struct vol *vol);
201 static void volume_free(struct vol *vol);
203 static void volfree(struct vol_option *options,
204 const struct vol_option *save)
209 for (i = 0; i < VOLOPT_MAX; i++) {
210 if (options[i].c_value && (options[i].c_value != save[i].c_value))
211 free(options[i].c_value);
214 for (i = 0; i < VOLOPT_MAX; i++) {
215 if (options[i].c_value)
216 free(options[i].c_value);
222 /* handle variable substitutions. here's what we understand:
223 * $b -> basename of path
224 * $c -> client ip/appletalk address
225 * $d -> volume pathname on server
226 * $f -> full name (whatever's in the gecos field)
229 * $i -> client ip/appletalk address without port
230 * $s -> server name (hostname if it doesn't exist)
231 * $u -> username (guest is usually nobody)
232 * $v -> volume name or basename if null
233 * $z -> zone (may not exist)
238 #define is_var(a, b) (strncmp((a), (b), 2) == 0)
240 static char *volxlate(AFPObj *obj, char *dest, size_t destlen,
241 char *src, struct passwd *pwd, char *path, char *volname)
251 dest = calloc(destlen +1, 1);
257 strlcpy(dest, src, destlen +1);
258 if ((p = strchr(src, '$')) == NULL) /* nothing to do */
261 /* first part of the path. just forward to the next variable. */
262 len = MIN((size_t)(p - src), destlen);
268 while (p && destlen > 0) {
269 /* now figure out what the variable is */
271 if (is_var(p, "$b")) {
273 if ((q = strrchr(path, '/')) == NULL)
275 else if (*(q + 1) != '\0')
278 } else if (is_var(p, "$c")) {
279 if (obj->proto == AFPPROTO_ASP) {
280 ASP asp = obj->handle;
282 len = sprintf(dest, "%u.%u", ntohs(asp->asp_sat.sat_addr.s_net),
283 asp->asp_sat.sat_addr.s_node);
287 } else if (obj->proto == AFPPROTO_DSI) {
288 DSI *dsi = obj->handle;
290 len = sprintf(dest, "%s:%u", inet_ntoa(dsi->client.sin_addr),
291 ntohs(dsi->client.sin_port));
295 } else if (is_var(p, "$d")) {
297 } else if (is_var(p, "$f")) {
298 if ((q = strchr(pwd->pw_gecos, ',')))
301 } else if (is_var(p, "$g")) {
302 struct group *grp = getgrgid(pwd->pw_gid);
305 } else if (is_var(p, "$h")) {
306 q = obj->options.hostname;
307 } else if (is_var(p, "$i")) {
308 if (obj->proto == AFPPROTO_ASP) {
309 ASP asp = obj->handle;
311 len = sprintf(dest, "%u", ntohs(asp->asp_sat.sat_addr.s_net));
315 } else if (obj->proto == AFPPROTO_DSI) {
316 DSI *dsi = obj->handle;
318 q = inet_ntoa(dsi->client.sin_addr);
320 } else if (is_var(p, "$s")) {
323 else if (obj->options.server) {
324 q = obj->options.server;
326 q = obj->options.hostname;
327 } else if (is_var(p, "$u")) {
329 if ( obj->options.ntseparator && (sep = strchr(obj->username, obj->options.ntseparator[0])) != NULL)
333 } else if (is_var(p, "$v")) {
338 if ((q = strrchr(path, '/')) == NULL)
340 else if (*(q + 1) != '\0')
343 } else if (is_var(p, "$z")) {
345 } else if (is_var(p, "$$")) {
350 /* copy the stuff over. if we don't understand something that we
351 * should, just skip it over. */
353 len = MIN(p == q ? 2 : strlen(q), destlen);
354 strncpy(dest, q, len);
359 /* stuff up to next $ */
361 p = strchr(src, '$');
362 len = p ? MIN((size_t)(p - src), destlen) : destlen;
364 strncpy(dest, src, len);
372 /* to make sure that val is valid, make sure to select an opt that
374 static int optionok(const char *buf, const char *opt, const char *val)
376 if (!strstr(buf,opt))
384 /* -------------------- */
385 static void setoption(struct vol_option *options, struct vol_option *save, int opt, const char *val)
387 if (options[opt].c_value && (!save || options[opt].c_value != save[opt].c_value))
388 free(options[opt].c_value);
389 options[opt].c_value = strdup(val + 1);
392 /* ------------------------------------------
393 handle all the options. tmp can't be NULL. */
394 static void volset(struct vol_option *options, struct vol_option *save,
395 char *volname, int vlen,
400 val = strchr(tmp, ':');
402 /* we'll assume it's a volume name. */
403 strncpy(volname, tmp, vlen);
408 LOG(log_debug, logtype_afpd, "Parsing volset %s", val);
410 if (optionok(tmp, "allow:", val)) {
411 setoption(options, save, VOLOPT_ALLOW, val);
413 } else if (optionok(tmp, "deny:", val)) {
414 setoption(options, save, VOLOPT_DENY, val);
416 } else if (optionok(tmp, "rwlist:", val)) {
417 setoption(options, save, VOLOPT_RWLIST, val);
419 } else if (optionok(tmp, "rolist:", val)) {
420 setoption(options, save, VOLOPT_ROLIST, val);
422 } else if (optionok(tmp, "codepage:", val)) {
423 LOG (log_error, logtype_afpd, "The old codepage system has been removed. Please make sure to read the documentation !!!!");
424 /* Make sure we don't screw anything */
426 } else if (optionok(tmp, "volcharset:", val)) {
427 setoption(options, save, VOLOPT_ENCODING, val);
428 } else if (optionok(tmp, "maccharset:", val)) {
429 setoption(options, save, VOLOPT_MACCHARSET, val);
430 } else if (optionok(tmp, "veto:", val)) {
431 setoption(options, save, VOLOPT_VETO, val);
432 } else if (optionok(tmp, "cnidscheme:", val)) {
433 setoption(options, save, VOLOPT_CNIDSCHEME, val);
434 } else if (optionok(tmp, "casefold:", val)) {
435 if (strcasecmp(val + 1, "tolower") == 0)
436 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMLOWER;
437 else if (strcasecmp(val + 1, "toupper") == 0)
438 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMUPPER;
439 else if (strcasecmp(val + 1, "xlatelower") == 0)
440 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UUPPERMLOWER;
441 else if (strcasecmp(val + 1, "xlateupper") == 0)
442 options[VOLOPT_CASEFOLD].i_value = AFPVOL_ULOWERMUPPER;
443 } else if (optionok(tmp, "adouble:", val)) {
444 if (strcasecmp(val + 1, "v1") == 0)
445 options[VOLOPT_ADOUBLE].i_value = AD_VERSION1;
446 #if AD_VERSION == AD_VERSION2
447 else if (strcasecmp(val + 1, "v2") == 0)
448 options[VOLOPT_ADOUBLE].i_value = AD_VERSION2;
449 else if (strcasecmp(val + 1, "osx") == 0)
450 options[VOLOPT_ADOUBLE].i_value = AD_VERSION2_OSX;
451 else if (strcasecmp(val + 1, "sfm") == 0)
452 options[VOLOPT_ADOUBLE].i_value = AD_VERSION1_SFM;
454 } else if (optionok(tmp, "options:", val)) {
457 if ((p = strtok(val + 1, ",")) == NULL) /* nothing */
461 if (strcasecmp(p, "prodos") == 0)
462 options[VOLOPT_FLAGS].i_value |= AFPVOL_A2VOL;
463 else if (strcasecmp(p, "mswindows") == 0) {
464 options[VOLOPT_FLAGS].i_value |= AFPVOL_MSWINDOWS | AFPVOL_USEDOTS;
465 } else if (strcasecmp(p, "crlf") == 0)
466 options[VOLOPT_FLAGS].i_value |= AFPVOL_CRLF;
467 else if (strcasecmp(p, "noadouble") == 0)
468 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOADOUBLE;
469 else if (strcasecmp(p, "ro") == 0)
470 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
471 else if (strcasecmp(p, "nohex") == 0)
472 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOHEX;
473 else if (strcasecmp(p, "usedots") == 0)
474 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS;
475 else if (strcasecmp(p, "invisibledots") == 0)
476 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS | AFPVOL_INV_DOTS;
477 else if (strcasecmp(p, "limitsize") == 0)
478 options[VOLOPT_FLAGS].i_value |= AFPVOL_LIMITSIZE;
479 /* support for either "dropbox" or "dropkludge" */
480 else if (strcasecmp(p, "dropbox") == 0)
481 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
482 else if (strcasecmp(p, "dropkludge") == 0)
483 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
484 else if (strcasecmp(p, "nofileid") == 0)
485 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOFILEID;
486 else if (strcasecmp(p, "nostat") == 0)
487 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOSTAT;
488 else if (strcasecmp(p, "preexec_close") == 0)
489 options[VOLOPT_PREEXEC].i_value = 1;
490 else if (strcasecmp(p, "root_preexec_close") == 0)
491 options[VOLOPT_ROOTPREEXEC].i_value = 1;
492 else if (strcasecmp(p, "upriv") == 0)
493 options[VOLOPT_FLAGS].i_value |= AFPVOL_UNIX_PRIV;
494 else if (strcasecmp(p, "extattrs") == 0)
495 options[VOLOPT_FLAGS].i_value |= AFPVOL_EXT_ATTRS;
496 else if (strcasecmp(p, "acls") == 0)
497 options[VOLOPT_FLAGS].i_value |= AFPVOL_ACLS;
498 else if (strcasecmp(p, "nodev") == 0)
499 options[VOLOPT_FLAGS].i_value |= AFPVOL_NODEV;
500 else if (strcasecmp(p, "caseinsensitive") == 0)
501 options[VOLOPT_FLAGS].i_value |= AFPVOL_CASEINSEN;
502 else if (strcasecmp(p, "illegalseq") == 0)
503 options[VOLOPT_FLAGS].i_value |= AFPVOL_EILSEQ;
504 else if (strcasecmp(p, "nocnidcache") == 0)
505 options[VOLOPT_FLAGS].i_value &= ~AFPVOL_CACHE;
506 else if (strcasecmp(p, "tm") == 0)
507 options[VOLOPT_FLAGS].i_value |= AFPVOL_TM;
509 p = strtok(NULL, ",");
512 } else if (optionok(tmp, "dbpath:", val)) {
513 setoption(options, save, VOLOPT_DBPATH, val);
515 } else if (optionok(tmp, "umask:", val)) {
516 options[VOLOPT_UMASK].i_value = (int)strtol(val +1, NULL, 8);
517 } else if (optionok(tmp, "dperm:", val)) {
518 options[VOLOPT_DPERM].i_value = (int)strtol(val+1, NULL, 8);
519 } else if (optionok(tmp, "fperm:", val)) {
520 options[VOLOPT_FPERM].i_value = (int)strtol(val+1, NULL, 8);
521 } else if (optionok(tmp, "perm:", val)) {
522 options[VOLOPT_DFLTPERM].i_value = (int)strtol(val+1, NULL, 8);
523 } else if (optionok(tmp, "mapchars:",val)) {
524 setoption(options, save, VOLOPT_MAPCHARS, val);
526 } else if (optionok(tmp, "password:", val)) {
527 setoption(options, save, VOLOPT_PASSWORD, val);
531 /* this code allows forced uid/gid per volume settings */
532 } else if (optionok(tmp, "forceuid:", val)) {
533 setoption(options, save, VOLOPT_FORCEUID, val);
534 } else if (optionok(tmp, "forcegid:", val)) {
535 setoption(options, save, VOLOPT_FORCEGID, val);
537 #endif /* FORCE_UIDGID */
538 } else if (optionok(tmp, "root_preexec:", val)) {
539 setoption(options, save, VOLOPT_ROOTPREEXEC, val);
541 } else if (optionok(tmp, "preexec:", val)) {
542 setoption(options, save, VOLOPT_PREEXEC, val);
544 } else if (optionok(tmp, "root_postexec:", val)) {
545 setoption(options, save, VOLOPT_ROOTPOSTEXEC, val);
547 } else if (optionok(tmp, "postexec:", val)) {
548 setoption(options, save, VOLOPT_POSTEXEC, val);
550 } else if (optionok(tmp, "allowed_hosts:", val)) {
551 setoption(options, save, VOLOPT_ALLOWED_HOSTS, val);
553 } else if (optionok(tmp, "denied_hosts:", val)) {
554 setoption(options, save, VOLOPT_DENIED_HOSTS, val);
557 /* ignore unknown options */
558 LOG(log_debug, logtype_afpd, "ignoring unknown volume option: %s", tmp);
563 /* ----------------- */
564 static void showvol(const ucs2_t *name)
567 for ( volume = Volumes; volume; volume = volume->v_next ) {
568 if (volume->v_hide && !strcasecmp_w( volume->v_name, name ) ) {
575 /* ------------------------------- */
576 static int creatvol(AFPObj *obj, struct passwd *pwd,
577 char *path, char *name,
578 struct vol_option *options,
579 const int user /* user defined volume */
583 int suffixlen, vlen, tmpvlen, u8mvlen, macvlen;
585 char tmpname[AFPVOL_U8MNAMELEN+1];
586 ucs2_t u8mtmpname[(AFPVOL_U8MNAMELEN+1)*2], mactmpname[(AFPVOL_MACNAMELEN+1)*2];
587 char suffix[6]; /* max is #FFFF */
590 if ( name == NULL || *name == '\0' ) {
591 if ((name = strrchr( path, '/' )) == NULL) {
592 return -1; /* Obviously not a fully qualified path */
595 /* if you wish to share /, you need to specify a name. */
600 /* suffix for mangling use (lastvid + 1) */
601 /* because v_vid has not been decided yet. */
602 suffixlen = sprintf(suffix, "%c%X", MANGLE_CHAR, lastvid + 1 );
604 vlen = strlen( name );
606 /* Unicode Volume Name */
607 /* Firstly convert name from unixcharset to UTF8-MAC */
609 tmpvlen = convert_charset(obj->options.unixcharset, CH_UTF8_MAC, 0, name, vlen, tmpname, AFPVOL_U8MNAMELEN, &flags);
611 strcpy(tmpname, "???");
615 /* Do we have to mangle ? */
616 if ( (flags & CONV_REQMANGLE) || (tmpvlen > obj->options.volnamelen)) {
617 if (tmpvlen + suffixlen > obj->options.volnamelen) {
619 tmpvlen = convert_charset(obj->options.unixcharset, CH_UTF8_MAC, 0, name, vlen, tmpname, obj->options.volnamelen - suffixlen, &flags);
620 tmpname[tmpvlen != (size_t)-1 ? tmpvlen : 0] = 0;
622 strcat(tmpname, suffix);
623 tmpvlen = strlen(tmpname);
626 /* Secondly convert name from UTF8-MAC to UCS2 */
627 if ( 0 >= ( u8mvlen = convert_string(CH_UTF8_MAC, CH_UCS2, tmpname, tmpvlen, u8mtmpname, AFPVOL_U8MNAMELEN*2)) )
630 LOG(log_debug, logtype_afpd, "createvol: Volume '%s' -> UTF8-MAC Name: '%s'", name, tmpname);
632 /* Maccharset Volume Name */
633 /* Firsty convert name from unixcharset to maccharset */
635 tmpvlen = convert_charset(obj->options.unixcharset, obj->options.maccharset, 0, name, vlen, tmpname, AFPVOL_U8MNAMELEN, &flags);
637 strcpy(tmpname, "???");
641 /* Do we have to mangle ? */
642 if ( (flags & CONV_REQMANGLE) || (tmpvlen > AFPVOL_MACNAMELEN)) {
643 if (tmpvlen + suffixlen > AFPVOL_MACNAMELEN) {
645 tmpvlen = convert_charset(obj->options.unixcharset, obj->options.maccharset, 0, name, vlen, tmpname, AFPVOL_MACNAMELEN - suffixlen, &flags);
646 tmpname[tmpvlen != (size_t)-1 ? tmpvlen : 0] = 0;
648 strcat(tmpname, suffix);
649 tmpvlen = strlen(tmpname);
652 /* Secondly convert name from maccharset to UCS2 */
653 if ( 0 >= ( macvlen = convert_string(obj->options.maccharset, CH_UCS2, tmpname, tmpvlen, mactmpname, AFPVOL_U8MNAMELEN*2)) )
656 LOG(log_debug, logtype_afpd, "createvol: Volume '%s' -> Longname: '%s'", name, tmpname);
658 /* check duplicate */
659 for ( volume = Volumes; volume; volume = volume->v_next ) {
660 if (( strcasecmp_w( volume->v_u8mname, u8mtmpname ) == 0 ) || ( strcasecmp_w( volume->v_macname, mactmpname ) == 0 )){
661 if (volume->v_deleted) {
662 volume->v_new = hide = 1;
665 return -1; /* Won't be able to access it, anyway... */
670 if (!( volume = (struct vol *)calloc(1, sizeof( struct vol ))) ) {
671 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
674 if ( NULL == ( volume->v_localname = strdup(name))) {
675 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
680 if ( NULL == ( volume->v_u8mname = strdup_w(u8mtmpname))) {
681 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
686 if ( NULL == ( volume->v_macname = strdup_w(mactmpname))) {
687 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
692 if (!( volume->v_path = (char *)malloc( strlen( path ) + 1 )) ) {
693 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
699 volume->v_name = utf8_encoding()?volume->v_u8mname:volume->v_macname;
700 volume->v_hide = hide;
701 strcpy( volume->v_path, path );
705 #endif /* __svr4__ */
706 /* os X start at 1 and use network order ie. 1 2 3 */
707 volume->v_vid = ++lastvid;
708 volume->v_vid = htons(volume->v_vid);
712 /* should we casefold? */
713 volume->v_casefold = options[VOLOPT_CASEFOLD].i_value;
715 /* shift in some flags */
716 volume->v_flags = options[VOLOPT_FLAGS].i_value;
718 volume->v_ad_options = 0;
719 if ((volume->v_flags & AFPVOL_NODEV))
720 volume->v_ad_options |= ADVOL_NODEV;
721 if ((volume->v_flags & AFPVOL_CACHE))
722 volume->v_ad_options |= ADVOL_CACHE;
723 if ((volume->v_flags & AFPVOL_UNIX_PRIV))
724 volume->v_ad_options |= ADVOL_UNIXPRIV;
725 if ((volume->v_flags & AFPVOL_INV_DOTS))
726 volume->v_ad_options |= ADVOL_INVDOTS;
728 if (options[VOLOPT_PASSWORD].c_value)
729 volume->v_password = strdup(options[VOLOPT_PASSWORD].c_value);
731 if (options[VOLOPT_VETO].c_value)
732 volume->v_veto = strdup(options[VOLOPT_VETO].c_value);
734 if (options[VOLOPT_ENCODING].c_value)
735 volume->v_volcodepage = strdup(options[VOLOPT_ENCODING].c_value);
737 if (options[VOLOPT_MACCHARSET].c_value)
738 volume->v_maccodepage = strdup(options[VOLOPT_MACCHARSET].c_value);
740 if (options[VOLOPT_DBPATH].c_value)
741 volume->v_dbpath = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_DBPATH].c_value, pwd, path, name);
743 if (options[VOLOPT_CNIDSCHEME].c_value)
744 volume->v_cnidscheme = strdup(options[VOLOPT_CNIDSCHEME].c_value);
746 if (options[VOLOPT_UMASK].i_value)
747 volume->v_umask = (mode_t)options[VOLOPT_UMASK].i_value;
749 if (options[VOLOPT_DPERM].i_value)
750 volume->v_dperm = (mode_t)options[VOLOPT_DPERM].i_value;
752 if (options[VOLOPT_FPERM].i_value)
753 volume->v_fperm = (mode_t)options[VOLOPT_FPERM].i_value;
755 if (options[VOLOPT_DFLTPERM].i_value)
756 volume->v_perm = (mode_t)options[VOLOPT_DFLTPERM].i_value;
758 if (options[VOLOPT_ADOUBLE].i_value)
759 volume->v_adouble = options[VOLOPT_ADOUBLE].i_value;
761 volume->v_adouble = AD_VERSION;
765 if (options[VOLOPT_FORCEUID].c_value) {
766 volume->v_forceuid = strdup(options[VOLOPT_FORCEUID].c_value);
768 volume->v_forceuid = NULL; /* set as null so as to return 0 later on */
771 if (options[VOLOPT_FORCEGID].c_value) {
772 volume->v_forcegid = strdup(options[VOLOPT_FORCEGID].c_value);
774 volume->v_forcegid = NULL; /* set as null so as to return 0 later on */
778 if (options[VOLOPT_PREEXEC].c_value)
779 volume->v_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_PREEXEC].c_value, pwd, path, name);
780 volume->v_preexec_close = options[VOLOPT_PREEXEC].i_value;
782 if (options[VOLOPT_POSTEXEC].c_value)
783 volume->v_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_POSTEXEC].c_value, pwd, path, name);
785 if (options[VOLOPT_ROOTPREEXEC].c_value)
786 volume->v_root_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPREEXEC].c_value, pwd, path, name);
787 volume->v_root_preexec_close = options[VOLOPT_ROOTPREEXEC].i_value;
789 if (options[VOLOPT_ROOTPOSTEXEC].c_value)
790 volume->v_root_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPOSTEXEC].c_value, pwd, path, name);
793 volume->v_dperm |= volume->v_perm;
794 volume->v_fperm |= volume->v_perm;
796 volume->v_next = Volumes;
801 /* ---------------- */
802 static char *myfgets( buf, size, fp )
811 while ((EOF != ( c = getc( fp )) ) && ( size > 1 )) {
812 if ( c == '\n' || c == '\r' ) {
813 if (p != buf && *(p -1) == '\\') {
835 /* check access list. this function wants something of the following
837 * @group,name,name2,@group2,name3
839 * a NULL argument allows everybody to have access.
840 * we return three things:
842 * 0: list exists, but name isn't in it
846 #ifndef NO_REAL_USER_NAME
847 /* authentication is case insensitive
848 * FIXME should we do the same with group name?
850 #define access_strcmp strcasecmp
853 #define access_strcmp strcmp
857 static int accessvol(args, name)
861 char buf[MAXPATHLEN + 1], *p;
867 strlcpy(buf, args, sizeof(buf));
868 if ((p = strtok(buf, ",")) == NULL) /* nothing, return okay */
872 if (*p == '@') { /* it's a group */
873 if ((gr = getgrnam(p + 1)) && gmem(gr->gr_gid))
875 } else if (access_strcmp(p, name) == 0) /* it's a user name */
877 p = strtok(NULL, ",");
883 static int hostaccessvol(type, volname, args, obj)
889 char buf[MAXPATHLEN + 1], *p, *b;
890 DSI *dsi = obj->handle;
895 strlcpy(buf, args, sizeof(buf));
896 if ((p = strtok_r(buf, ",", &b)) == NULL) /* nothing, return okay */
900 if (obj->proto == AFPPROTO_DSI) {
901 struct in_addr mask, net;
902 char *net_char, *mask_char;
905 net_char = strtok(p, "/");
906 mask_char = strtok(NULL,"/");
907 if (mask_char == NULL) {
910 mask_int = atoi(mask_char);
913 // convert the integer netmask to a bitmask in network order
914 mask.s_addr = htonl(-1 - ((1 << (32 - mask_int)) - 1));
915 net.s_addr = inet_addr(net_char) & mask.s_addr;
917 if ((dsi->client.sin_addr.s_addr & mask.s_addr) == net.s_addr) {
918 if (type == VOLOPT_DENIED_HOSTS)
919 LOG(log_info, logtype_afpd, "AFP access denied for client IP '%s' to volume '%s' by denied list",
920 inet_ntoa(dsi->client.sin_addr), volname);
924 p = strtok_r(NULL, ",", &b);
926 if (type == VOLOPT_ALLOWED_HOSTS)
927 LOG(log_info, logtype_afpd, "AFP access denied for client IP '%s' to volume '%s', not in allowed list",
928 inet_ntoa(dsi->client.sin_addr), volname);
932 static void setextmap( ext, type, creator, user)
933 char *ext, *type, *creator;
939 if (Extmap == NULL) {
940 if (( Extmap = calloc(1, sizeof( struct extmap ))) == NULL ) {
941 LOG(log_error, logtype_afpd, "setextmap: calloc: %s", strerror(errno) );
946 for ( em = Extmap, cnt = 0; em->em_ext; em++, cnt++) {
947 if ( (strdiacasecmp( em->em_ext, ext )) == 0 ) {
952 if ( em->em_ext == NULL ) {
953 if (!(Extmap = realloc( Extmap, sizeof( struct extmap ) * (cnt +2))) ) {
954 LOG(log_error, logtype_afpd, "setextmap: realloc: %s", strerror(errno) );
957 (Extmap +cnt +1)->em_ext = NULL;
959 } else if ( !user ) {
965 if (!(em->em_ext = strdup( ext))) {
966 LOG(log_error, logtype_afpd, "setextmap: strdup: %s", strerror(errno) );
970 if ( *type == '\0' ) {
971 memcpy(em->em_type, "????", sizeof( em->em_type ));
973 memcpy(em->em_type, type, sizeof( em->em_type ));
975 if ( *creator == '\0' ) {
976 memcpy(em->em_creator, "UNIX", sizeof( em->em_creator ));
978 memcpy(em->em_creator, creator, sizeof( em->em_creator ));
982 /* -------------------------- */
983 static int extmap_cmp(const void *map1, const void *map2)
985 const struct extmap *em1 = map1;
986 const struct extmap *em2 = map2;
987 return strdiacasecmp(em1->em_ext, em2->em_ext);
990 static void sortextmap( void)
995 if ((em = Extmap) == NULL) {
1003 qsort(Extmap, Extmap_cnt, sizeof(struct extmap), extmap_cmp);
1004 if (*Extmap->em_ext == 0) {
1005 /* the first line is really "." the default entry,
1006 * we remove the leading '.' in setextmap
1013 /* ----------------------
1015 static void free_extmap( void)
1020 for ( em = Extmap; em->em_ext; em++) {
1030 /* ----------------------
1032 static int volfile_changed(struct afp_volume_name *p)
1038 name = p->full_name;
1042 if (!stat( name, &st) && st.st_mtime > p->mtime) {
1043 p->mtime = st.st_mtime;
1049 /* ----------------------
1050 * Read a volume configuration file and add the volumes contained within to
1051 * the global volume list. If p2 is non-NULL, the file that is opened is
1054 * Lines that begin with # and blank lines are ignored.
1055 * Volume lines are of the form:
1056 * <unix path> [<volume name>] [allow:<user>,<@group>,...] \
1057 * [codepage:<file>] [casefold:<num>]
1058 * <extension> TYPE [CREATOR]
1060 static int readvolfile(obj, p1, p2, user, pwent)
1062 struct afp_volume_name *p1;
1065 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( vol, bfree, btotal, xbfree, xbtotal, bsize )
1312 u_int32_t *bfree, *btotal, *bsize;
1313 VolSpace *xbfree, *xbtotal;
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( bitmap, vol, st, buf, buflen )
1391 int bit = 0, isad = 1;
1394 u_int32_t bfree, btotal, bsize;
1395 VolSpace xbfree, xbtotal; /* extended bytes */
1396 char *data, *nameoff = NULL;
1399 LOG(log_debug, logtype_afpd, "getvolparams: Volume '%s'", vol->v_localname);
1401 /* courtesy of jallison@whistle.com:
1402 * For MacOS8.x support we need to create the
1403 * .Parent file here if it doesn't exist. */
1405 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1406 if ( ad_open_metadata( vol->v_path, vol_noadouble(vol) | ADFLAGS_DIR, O_CREAT, &ad) < 0 ) {
1408 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1410 } else if (ad_get_MD_flags( &ad ) & O_CREAT) {
1411 slash = strrchr( vol->v_path, '/' );
1415 slash = vol->v_path;
1416 if (ad_getentryoff(&ad, ADEID_NAME)) {
1417 ad_setentrylen( &ad, ADEID_NAME, strlen( slash ));
1418 memcpy(ad_entry( &ad, ADEID_NAME ), slash,
1419 ad_getentrylen( &ad, ADEID_NAME ));
1421 vol_setdate(vol->v_vid, &ad, st->st_mtime);
1425 if (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0)
1426 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1428 vol->v_ctime = aint;
1431 if (( bitmap & ( (1<<VOLPBIT_BFREE)|(1<<VOLPBIT_BTOTAL) |
1432 (1<<VOLPBIT_XBFREE)|(1<<VOLPBIT_XBTOTAL) |
1433 (1<<VOLPBIT_BSIZE)) ) != 0 ) {
1434 if ( getvolspace( vol, &bfree, &btotal, &xbfree, &xbtotal,
1437 ad_close( &ad, ADFLAGS_HF );
1439 return( AFPERR_PARAM );
1444 while ( bitmap != 0 ) {
1445 while (( bitmap & 1 ) == 0 ) {
1453 if (0 == (vol->v_flags & AFPVOL_NOFILEID) && vol->v_cdb != NULL &&
1454 (vol->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1455 ashort = VOLPBIT_ATTR_FILEID;
1457 /* check for read-only.
1458 * NOTE: we don't actually set the read-only flag unless
1459 * it's passed in that way as it's possible to mount
1460 * a read-write filesystem under a read-only one. */
1461 if ((vol->v_flags & AFPVOL_RO) ||
1462 ((utime(vol->v_path, NULL) < 0) && (errno == EROFS))) {
1463 ashort |= VOLPBIT_ATTR_RO;
1465 ashort |= VOLPBIT_ATTR_CATSEARCH;
1466 if (afp_version >= 30) {
1467 ashort |= VOLPBIT_ATTR_UTF8;
1468 if (vol->v_flags & AFPVOL_UNIX_PRIV)
1469 ashort |= VOLPBIT_ATTR_UNIXPRIV;
1470 if (vol->v_flags & AFPVOL_TM)
1471 ashort |= VOLPBIT_ATTR_TM;
1473 if (afp_version >= 32) {
1474 if (vol->v_flags & AFPVOL_EXT_ATTRS)
1475 ashort |= VOLPBIT_ATTR_EXT_ATTRS;
1476 if (vol->v_flags & AFPVOL_ACLS)
1477 ashort |= VOLPBIT_ATTR_ACLS;
1479 ashort = htons(ashort);
1480 memcpy(data, &ashort, sizeof( ashort ));
1481 data += sizeof( ashort );
1485 ashort = htons( AFPVOLSIG_DEFAULT );
1486 memcpy(data, &ashort, sizeof( ashort ));
1487 data += sizeof( ashort );
1490 case VOLPBIT_CDATE :
1491 aint = vol->v_ctime;
1492 memcpy(data, &aint, sizeof( aint ));
1493 data += sizeof( aint );
1496 case VOLPBIT_MDATE :
1497 if ( st->st_mtime > vol->v_mtime ) {
1498 vol->v_mtime = st->st_mtime;
1500 aint = AD_DATE_FROM_UNIX(vol->v_mtime);
1501 memcpy(data, &aint, sizeof( aint ));
1502 data += sizeof( aint );
1505 case VOLPBIT_BDATE :
1506 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1507 aint = AD_DATE_START;
1508 memcpy(data, &aint, sizeof( aint ));
1509 data += sizeof( aint );
1513 memcpy(data, &vol->v_vid, sizeof( vol->v_vid ));
1514 data += sizeof( vol->v_vid );
1517 case VOLPBIT_BFREE :
1518 bfree = htonl( bfree );
1519 memcpy(data, &bfree, sizeof( bfree ));
1520 data += sizeof( bfree );
1523 case VOLPBIT_BTOTAL :
1524 btotal = htonl( btotal );
1525 memcpy(data, &btotal, sizeof( btotal ));
1526 data += sizeof( btotal );
1529 #ifndef NO_LARGE_VOL_SUPPORT
1530 case VOLPBIT_XBFREE :
1531 xbfree = hton64( xbfree );
1532 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1533 bcopy(&xbfree, data, sizeof(xbfree));
1534 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1535 memcpy(data, &xbfree, sizeof( xbfree ));
1536 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1537 data += sizeof( xbfree );
1540 case VOLPBIT_XBTOTAL :
1541 xbtotal = hton64( xbtotal );
1542 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1543 bcopy(&xbtotal, data, sizeof(xbtotal));
1544 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1545 memcpy(data, &xbtotal, sizeof( xbtotal ));
1546 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1547 data += sizeof( xbfree );
1549 #endif /* ! NO_LARGE_VOL_SUPPORT */
1553 data += sizeof( u_int16_t );
1556 case VOLPBIT_BSIZE: /* block size */
1557 bsize = htonl(bsize);
1558 memcpy(data, &bsize, sizeof(bsize));
1559 data += sizeof(bsize);
1564 ad_close( &ad, ADFLAGS_HF );
1566 return( AFPERR_BITMAP );
1572 ashort = htons( data - buf );
1573 memcpy(nameoff, &ashort, sizeof( ashort ));
1574 /* name is always in mac charset */
1575 aint = ucs2_to_charset( vol->v_maccharset, vol->v_macname, data+1, AFPVOL_MACNAMELEN + 1);
1585 ad_close_metadata( &ad);
1587 *buflen = data - buf;
1591 /* ------------------------- */
1592 static int stat_vol(u_int16_t bitmap, struct vol *vol, char *rbuf, int *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(obj, ibuf, ibuflen, rbuf, rbuflen )
1688 char *ibuf _U_, *rbuf;
1689 int ibuflen _U_, *rbuflen;
1702 for ( vcnt = 0, volume = Volumes; volume; volume = volume->v_next ) {
1703 if (!(volume->v_flags & AFPVOL_NOSTAT)) {
1706 if ( stat( volume->v_path, &st ) < 0 ) {
1707 LOG(log_info, logtype_afpd, "afp_getsrvrparms(%s): stat: %s",
1708 volume->v_path, strerror(errno) );
1709 continue; /* can't access directory */
1711 if (!S_ISDIR(st.st_mode)) {
1712 continue; /* not a dir */
1714 accessmode(volume->v_path, &ma, NULL, &st);
1715 if ((ma.ma_user & (AR_UREAD | AR_USEARCH)) != (AR_UREAD | AR_USEARCH)) {
1716 continue; /* no r-x access */
1719 if (volume->v_hide) {
1720 continue; /* config file changed but the volume was mounted */
1723 if (utf8_encoding()) {
1724 len = ucs2_to_charset_allocate(CH_UTF8_MAC, &namebuf, volume->v_u8mname);
1726 len = ucs2_to_charset_allocate(obj->options.maccharset, &namebuf, volume->v_macname);
1729 if (len == (size_t)-1)
1732 /* set password bit if there's a volume password */
1733 *data = (volume->v_password) ? AFPSRVR_PASSWD : 0;
1735 /* Apple 2 clients running ProDOS-8 expect one volume to have
1736 bit 0 of this byte set. They will not recognize anything
1737 on the server unless this is the case. I have not
1738 completely worked this out, but it's related to booting
1739 from the server. Support for that function is a ways
1740 off.. <shirsch@ibm.net> */
1741 *data |= (volume->v_flags & AFPVOL_A2VOL) ? AFPSRVR_CONFIGINFO : 0;
1742 *data++ |= 0; /* UNIX PRIVS BIT ..., OSX doesn't seem to use it, so we don't either */
1744 memcpy(data, namebuf, len );
1750 *rbuflen = data - rbuf;
1752 if ( gettimeofday( &tv, 0 ) < 0 ) {
1753 LOG(log_error, logtype_afpd, "afp_getsrvrparms(%s): gettimeofday: %s", volume->v_path, strerror(errno) );
1755 return AFPERR_PARAM;
1757 tv.tv_sec = AD_DATE_FROM_UNIX(tv.tv_sec);
1758 memcpy(data, &tv.tv_sec, sizeof( u_int32_t));
1759 data += sizeof( u_int32_t);
1764 /* -------------------------
1765 * we are the user here
1767 int afp_openvol(obj, ibuf, ibuflen, rbuf, rbuflen )
1770 int ibuflen _U_, *rbuflen;
1780 char path[ MAXPATHLEN + 1];
1786 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1787 bitmap = ntohs( bitmap );
1788 ibuf += sizeof( bitmap );
1789 if (( bitmap & (1<<VOLPBIT_VID)) == 0 ) {
1791 return AFPERR_BITMAP;
1794 len = (unsigned char)*ibuf++;
1795 volname = obj->oldtmp;
1797 if ((volname_tmp = strchr(volname,'+')) != NULL)
1798 volname = volname_tmp+1;
1800 if (utf8_encoding()) {
1801 namelen = convert_string(CH_UTF8_MAC, CH_UCS2, ibuf, len, volname, sizeof(obj->oldtmp));
1803 namelen = convert_string(obj->options.maccharset, CH_UCS2, ibuf, len, volname, sizeof(obj->oldtmp));
1808 return AFPERR_PARAM;
1812 if ((len + 1) & 1) /* pad to an even boundary */
1817 for ( volume = Volumes; volume; volume = volume->v_next ) {
1818 if ( strcasecmp_w( (ucs2_t*) volname, volume->v_name ) == 0 ) {
1823 if ( volume == NULL ) {
1825 return AFPERR_PARAM;
1828 /* check for a volume password */
1829 if (volume->v_password && strncmp(ibuf, volume->v_password, VOLPASSLEN)) {
1831 return AFPERR_ACCESS;
1834 if (( volume->v_flags & AFPVOL_OPEN ) ) {
1835 /* the volume is already open */
1837 set_uidgid ( volume );
1839 return stat_vol(bitmap, volume, rbuf, rbuflen);
1842 /* initialize volume variables
1845 if (utf8_encoding()) {
1846 volume->max_filename = 255;
1849 volume->max_filename = MACFILELEN;
1852 volume->v_dir = volume->v_root = NULL;
1853 volume->v_hash = NULL;
1855 volume->v_flags |= AFPVOL_OPEN;
1856 volume->v_cdb = NULL;
1858 if (volume->v_root_preexec) {
1859 if ((ret = afprun(1, volume->v_root_preexec, NULL)) && volume->v_root_preexec_close) {
1860 LOG(log_error, logtype_afpd, "afp_openvol(%s): root preexec : %d", volume->v_path, ret );
1867 set_uidgid ( volume );
1870 if (volume->v_preexec) {
1871 if ((ret = afprun(0, volume->v_preexec, NULL)) && volume->v_preexec_close) {
1872 LOG(log_error, logtype_afpd, "afp_openvol(%s): preexec : %d", volume->v_path, ret );
1878 if ( stat( volume->v_path, &st ) < 0 ) {
1883 if ( chdir( volume->v_path ) < 0 ) {
1888 if (utf8_encoding()) {
1889 len = convert_string_allocate(CH_UCS2, CH_UTF8_MAC, volume->v_u8mname, namelen, &vol_mname);
1891 len = convert_string_allocate(CH_UCS2, obj->options.maccharset, volume->v_macname, namelen, &vol_mname);
1893 if ( !vol_mname || len <= 0) {
1898 if ( NULL == getcwd(path, MAXPATHLEN)) {
1899 /* shouldn't be fatal but it will fail later */
1900 LOG(log_error, logtype_afpd, "afp_openvol(%s): volume pathlen too long", volume->v_path);
1905 if ((vol_uname = strrchr(path, '/')) == NULL)
1907 else if (*(vol_uname + 1) != '\0')
1910 if ((dir = dirnew(vol_mname, vol_uname) ) == NULL) {
1912 LOG(log_error, logtype_afpd, "afp_openvol(%s): malloc: %s", volume->v_path, strerror(errno) );
1918 dir->d_did = DIRDID_ROOT;
1919 dir->d_color = DIRTREE_COLOR_BLACK; /* root node is black */
1920 dir->d_m_name_ucs2 = strdup_w(volume->v_name);
1921 volume->v_dir = volume->v_root = dir;
1922 volume->v_hash = dirhash();
1924 curdir = volume->v_dir;
1925 if (volume->v_cnidscheme == NULL) {
1926 volume->v_cnidscheme = strdup(DEFAULT_CNID_SCHEME);
1927 LOG(log_info, logtype_afpd, "Volume %s use CNID scheme %s.", volume->v_path, volume->v_cnidscheme);
1929 if (volume->v_dbpath)
1930 volume->v_cdb = cnid_open (volume->v_dbpath, volume->v_umask, volume->v_cnidscheme, (volume->v_flags & AFPVOL_NODEV));
1932 volume->v_cdb = cnid_open (volume->v_path, volume->v_umask, volume->v_cnidscheme, (volume->v_flags & AFPVOL_NODEV));
1933 if (volume->v_cdb == NULL) {
1934 LOG(log_error, logtype_afpd, "Fatal error: cannot open CNID or invalid CNID backend for %s: %s",
1935 volume->v_path, volume->v_cnidscheme);
1942 if (!volume->v_volcodepage)
1943 volume->v_volcodepage = strdup("UTF8");
1945 if ( (charset_t) -1 == ( volume->v_volcharset = add_charset(volume->v_volcodepage)) ) {
1946 LOG (log_error, logtype_afpd, "Setting codepage %s as volume codepage failed", volume->v_volcodepage);
1951 if ( NULL == ( volume->v_vol = find_charset_functions(volume->v_volcodepage)) || volume->v_vol->flags & CHARSET_ICONV ) {
1952 LOG (log_warning, logtype_afpd, "WARNING: volume encoding %s is *not* supported by netatalk, expect problems !!!!", volume->v_volcodepage);
1955 if (!volume->v_maccodepage)
1956 volume->v_maccodepage = strdup(obj->options.maccodepage);
1958 if ( (charset_t) -1 == ( volume->v_maccharset = add_charset(volume->v_maccodepage)) ) {
1959 LOG (log_error, logtype_afpd, "Setting codepage %s as mac codepage failed", volume->v_maccodepage);
1964 if ( NULL == ( volume->v_mac = find_charset_functions(volume->v_maccodepage)) || ! (volume->v_mac->flags & CHARSET_CLIENT) ) {
1965 LOG (log_error, logtype_afpd, "Fatal error: mac charset %s not supported", volume->v_maccodepage);
1970 ret = stat_vol(bitmap, volume, rbuf, rbuflen);
1971 if (ret == AFP_OK) {
1973 if (!(volume->v_flags & AFPVOL_RO)) {
1974 handle_special_folders( volume );
1975 savevoloptions( volume);
1979 * If you mount a volume twice, the second time the trash appears on
1980 * the desk-top. That's because the Mac remembers the DID for the
1981 * trash (even for volumes in different zones, on different servers).
1982 * Just so this works better, we prime the DID cache with the trash,
1983 * fixing the trash at DID 17.
1984 * FIXME (RL): should it be done inside a CNID backend ? (always returning Trash DID when asked) ?
1986 if ((volume->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1988 /* FIXME find db time stamp */
1989 if (cnid_getstamp(volume->v_cdb, volume->v_stamp, sizeof(volume->v_stamp)) < 0) {
1990 LOG (log_error, logtype_afpd,
1991 "afp_openvol(%s): Fatal error: Unable to get stamp value from CNID backend",
1999 cname( volume, volume->v_dir, &p );
2005 if (volume->v_dir) {
2006 hash_free( volume->v_hash);
2007 dirfree( volume->v_dir );
2008 volume->v_dir = volume->v_root = NULL;
2011 volume->v_flags &= ~AFPVOL_OPEN;
2012 if (volume->v_cdb != NULL) {
2013 cnid_close(volume->v_cdb);
2014 volume->v_cdb = NULL;
2020 /* ------------------------- */
2021 static void closevol(struct vol *vol)
2026 hash_free( vol->v_hash);
2027 dirfree( vol->v_root );
2029 if (vol->v_cdb != NULL) {
2030 cnid_close(vol->v_cdb);
2034 if (vol->v_postexec) {
2035 afprun(0, vol->v_postexec, NULL);
2037 if (vol->v_root_postexec) {
2038 afprun(1, vol->v_root_postexec, NULL);
2042 /* ------------------------- */
2043 void close_all_vol(void)
2047 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
2048 if ( (ovol->v_flags & AFPVOL_OPEN) ) {
2049 ovol->v_flags &= ~AFPVOL_OPEN;
2055 /* ------------------------- */
2056 static void deletevol(struct vol *vol)
2060 vol->v_flags &= ~AFPVOL_OPEN;
2061 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
2062 if ( (ovol->v_flags & AFPVOL_OPEN) ) {
2066 if ( ovol != NULL ) {
2067 /* Even if chdir fails, we can't say afp_closevol fails. */
2068 if ( chdir( ovol->v_path ) == 0 ) {
2069 curdir = ovol->v_dir;
2074 if (vol->v_deleted) {
2075 showvol(vol->v_name);
2082 /* ------------------------- */
2083 int afp_closevol(obj, ibuf, ibuflen, rbuf, rbuflen )
2085 char *ibuf, *rbuf _U_;
2086 int ibuflen _U_, *rbuflen;
2093 memcpy(&vid, ibuf, sizeof( vid ));
2094 if (NULL == ( vol = getvolbyvid( vid )) ) {
2095 return( AFPERR_PARAM );
2103 /* ------------------------- */
2104 struct vol *getvolbyvid(const u_int16_t vid )
2108 for ( vol = Volumes; vol; vol = vol->v_next ) {
2109 if ( vid == vol->v_vid ) {
2113 if ( vol == NULL || ( vol->v_flags & AFPVOL_OPEN ) == 0 ) {
2119 #endif /* FORCE_UIDGID */
2124 /* ------------------------ */
2125 static int ext_cmp_key(const void *key, const void *obj)
2127 const char *p = key;
2128 const struct extmap *em = obj;
2129 return strdiacasecmp(p, em->em_ext);
2131 struct extmap *getextmap(const char *path)
2136 if (NULL == ( p = strrchr( path, '.' )) ) {
2137 return( Defextmap );
2140 if (!*p || !Extmap_cnt) {
2141 return( Defextmap );
2143 em = bsearch(p, Extmap, Extmap_cnt, sizeof(struct extmap), ext_cmp_key);
2147 return( Defextmap );
2151 /* ------------------------- */
2152 struct extmap *getdefextmap(void)
2154 return( Defextmap );
2157 /* --------------------------
2158 poll if a volume is changed by other processes.
2160 int pollvoltime(obj)
2167 if (!(afp_version > 21 && obj->options.server_notif))
2170 if ( gettimeofday( &tv, 0 ) < 0 )
2173 for ( vol = Volumes; vol; vol = vol->v_next ) {
2174 if ( (vol->v_flags & AFPVOL_OPEN) && vol->v_mtime + 30 < tv.tv_sec) {
2175 if ( !stat( vol->v_path, &st ) && vol->v_mtime != st.st_mtime ) {
2176 vol->v_mtime = st.st_mtime;
2177 if (!obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED))
2186 /* ------------------------- */
2187 void setvoltime(obj, vol )
2193 /* just looking at vol->v_mtime is broken seriously since updates
2194 * from other users afpd processes never are seen.
2195 * This is not the most elegant solution (a shared memory between
2196 * the afpd processes would come closer)
2199 if ( gettimeofday( &tv, 0 ) < 0 ) {
2200 LOG(log_error, logtype_afpd, "setvoltime(%s): gettimeofday: %s", vol->v_path, strerror(errno) );
2203 if( utime( vol->v_path, NULL ) < 0 ) {
2204 /* write of time failed ... probably a read only filesys,
2205 * where no other users can interfere, so there's no issue here
2209 /* a little granularity */
2210 if (vol->v_mtime < tv.tv_sec) {
2211 vol->v_mtime = tv.tv_sec;
2212 /* or finder doesn't update free space */
2213 if (afp_version > 21 && obj->options.server_notif) {
2214 obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED);
2219 /* ------------------------- */
2220 int afp_getvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
2223 int ibuflen _U_, *rbuflen;
2226 u_int16_t vid, bitmap;
2229 memcpy(&vid, ibuf, sizeof( vid ));
2230 ibuf += sizeof( vid );
2231 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2232 bitmap = ntohs( bitmap );
2234 if (NULL == ( vol = getvolbyvid( vid )) ) {
2236 return( AFPERR_PARAM );
2239 return stat_vol(bitmap, vol, rbuf, rbuflen);
2242 /* ------------------------- */
2243 int afp_setvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
2245 char *ibuf, *rbuf _U_;
2246 int ibuflen _U_, *rbuflen;
2250 u_int16_t vid, bitmap;
2256 memcpy(&vid, ibuf, sizeof( vid ));
2257 ibuf += sizeof( vid );
2258 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2259 bitmap = ntohs( bitmap );
2260 ibuf += sizeof(bitmap);
2262 if (( vol = getvolbyvid( vid )) == NULL ) {
2263 return( AFPERR_PARAM );
2266 if ((vol->v_flags & AFPVOL_RO))
2267 return AFPERR_VLOCK;
2269 /* we can only set the backup date. */
2270 if (bitmap != (1 << VOLPBIT_BDATE))
2271 return AFPERR_BITMAP;
2273 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2274 if ( ad_open( vol->v_path, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR,
2277 return AFPERR_VLOCK;
2279 return AFPERR_ACCESS;
2282 memcpy(&aint, ibuf, sizeof(aint));
2283 ad_setdate(&ad, AD_DATE_BACKUP, aint);
2285 ad_close(&ad, ADFLAGS_HF);
2289 /* ------------------------- */
2290 int wincheck(const struct vol *vol, const char *path)
2294 if (!(vol->v_flags & AFPVOL_MSWINDOWS))
2297 /* empty paths are not allowed */
2298 if ((len = strlen(path)) == 0)
2301 /* leading or trailing whitespaces are not allowed, carriage returns
2302 * and probably other whitespace is okay, tabs are not allowed
2304 if ((path[0] == ' ') || (path[len-1] == ' '))
2307 /* certain characters are not allowed */
2308 if (strpbrk(path, MSWINDOWS_BADCHARS))
2311 /* everything else is okay */
2317 * precreate a folder
2318 * this is only intended for folders in the volume root
2319 * It will *not* work if the folder name contains extended characters
2321 static int create_special_folder (const struct vol *vol, const struct _special_folder *folder)
2330 p = (char *) malloc ( strlen(vol->v_path)+strlen(folder->name)+2);
2332 LOG(log_error, logtype_afpd,"malloc failed");
2336 q=strdup(folder->name);
2338 LOG(log_error, logtype_afpd,"malloc failed");
2342 strcpy(p, vol->v_path);
2347 if ((vol->v_casefold & AFPVOL_MTOUUPPER))
2349 else if ((vol->v_casefold & AFPVOL_MTOULOWER))
2355 if ( (ret = stat( p, &st )) < 0 ) {
2356 if (folder->precreate) {
2357 if (ad_mkdir(p, folder->mode)) {
2358 LOG(log_debug, logtype_afpd,"Creating '%s' failed in %s: %s", p, vol->v_path, strerror(errno));
2367 if ( !ret && folder->hide) {
2369 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2370 if (ad_open( p, vol_noadouble(vol) | ADFLAGS_HF|ADFLAGS_DIR,
2371 O_RDWR|O_CREAT, 0666, &ad) < 0) {
2376 if ((ad_get_HF_flags( &ad ) & O_CREAT) ) {
2377 if (ad_getentryoff(&ad, ADEID_NAME)) {
2378 ad_setentrylen( &ad, ADEID_NAME, strlen(folder->name));
2379 memcpy(ad_entry( &ad, ADEID_NAME ), folder->name,
2380 ad_getentrylen( &ad, ADEID_NAME ));
2384 ad_getattr(&ad, &attr);
2385 attr |= htons( ntohs( attr ) | ATTRBIT_INVISIBLE );
2386 ad_setattr(&ad, attr);
2388 /* do the same with the finder info */
2389 if (ad_entry(&ad, ADEID_FINDERI)) {
2390 memcpy(&attr, ad_entry(&ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF, sizeof(attr));
2391 attr |= htons(FINDERINFO_INVISIBLE);
2392 memcpy(ad_entry(&ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF,&attr, sizeof(attr));
2396 ad_close( &ad, ADFLAGS_HF );
2403 static void handle_special_folders (const struct vol * vol)
2405 const _special_folder *p = &special_folders[0];
2407 if ((vol->v_flags & AFPVOL_RO))
2410 for (; p->name != NULL; p++) {
2411 create_special_folder (vol, p);
2416 * Save the volume options to a file, used by
2418 * Writing the file everytime a volume is opened is
2419 * unnecessary, but it shouldn't hurt much.
2421 static int savevoloptions (const struct vol *vol)
2424 char item[MAXPATHLEN];
2428 const _vol_opt_name *op = &vol_opt_names[0];
2429 const _vol_opt_name *cf = &vol_opt_casefold[0];
2431 strlcpy (item, vol->v_path, sizeof(item));
2432 strlcat (item, "/.AppleDesktop/", sizeof(item));
2433 strlcat (item, VOLINFOFILE, sizeof(item));
2435 if ((fd = open( item, O_RDWR | O_CREAT , 0666)) <0 ) {
2436 LOG(log_debug, logtype_afpd,"Error opening %s: %s", item, strerror(errno));
2440 /* try to get a lock */
2442 lock.l_whence = SEEK_SET;
2444 lock.l_type = F_WRLCK;
2446 if (fcntl(fd, F_SETLK, &lock) < 0) {
2447 if (errno == EACCES || errno == EAGAIN) {
2448 /* ignore, other process already writing the file */
2451 LOG(log_error, logtype_cnid, "savevoloptions: cannot get lock: %s", strerror(errno));
2456 /* write volume options */
2457 snprintf(buf, sizeof(buf), "MAC_CHARSET:%s\n", vol->v_maccodepage);
2458 snprintf(item, sizeof(item), "VOL_CHARSET:%s\n", vol->v_volcodepage);
2459 strlcat(buf, item, sizeof(buf));
2461 switch (vol->v_adouble) {
2463 strlcat(buf, "ADOUBLE_VER:v1\n", sizeof(buf));
2466 strlcat(buf, "ADOUBLE_VER:v2\n", sizeof(buf));
2468 case AD_VERSION2_OSX:
2469 strlcat(buf, "ADOUBLE_VER:osx\n", sizeof(buf));
2471 case AD_VERSION1_SFM:
2472 strlcat(buf, "ADOUBLE_VER:sfm\n", sizeof(buf));
2476 strlcat(buf, "CNIDBACKEND:", sizeof(buf));
2477 strlcat(buf, vol->v_cnidscheme, sizeof(buf));
2478 strlcat(buf, "\n", sizeof(buf));
2480 strlcat(buf, "CNIDDBDHOST:", sizeof(buf));
2481 strlcat(buf, Cnid_srv, sizeof(buf));
2482 strlcat(buf, "\n", sizeof(buf));
2484 snprintf(item, sizeof(item), "CNIDDBDPORT:%u\n", Cnid_port);
2485 strlcat(buf, item, sizeof(buf));
2487 strcpy(item, "CNID_DBPATH:");
2489 strlcat(item, vol->v_dbpath, sizeof(item));
2491 strlcat(item, vol->v_path, sizeof(item));
2492 strlcat(item, "\n", sizeof(item));
2493 strlcat(buf, item, sizeof(buf));
2496 strcpy(item, "VOLUME_OPTS:");
2497 for (;op->name; op++) {
2498 if ( (vol->v_flags & op->option) ) {
2499 strlcat(item, op->name, sizeof(item));
2500 strlcat(item, " ", sizeof(item));
2503 strlcat(item, "\n", sizeof(item));
2504 strlcat(buf, item, sizeof(buf));
2506 /* casefold flags */
2507 strcpy(item, "VOLCASEFOLD:");
2508 for (;cf->name; cf++) {
2509 if ( (vol->v_casefold & cf->option) ) {
2510 strlcat(item, cf->name, sizeof(item));
2511 strlcat(item, " ", sizeof(item));
2514 strlcat(item, "\n", sizeof(item));
2515 strlcat(buf, item, sizeof(buf));
2517 if (strlen(buf) >= sizeof(buf)-1)
2518 LOG(log_debug, logtype_afpd,"Error writing .volinfo file: buffer too small, %s", buf);
2521 if (write( fd, buf, strlen(buf)) < 0 || ftruncate(fd, strlen(buf)) < 0 ) {
2522 LOG(log_debug, logtype_afpd,"Error writing .volinfo file: %s", strerror(errno));
2525 lock.l_type = F_UNLCK;
2526 fcntl(fd, F_SETLK, &lock);