2 * $Id: volume.c,v 1.93 2009-10-14 02:24:05 didg 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"
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 (VOLOPT_DFLTPERM +1)
134 #define VOLOPT_NUM (VOLOPT_MAX + 1)
137 #define VOLOPT_DEFAULT ":DEFAULT:"
138 #define VOLOPT_DEFAULT_LEN 9
145 typedef struct _special_folder {
152 static const _special_folder special_folders[] = {
153 {"Network Trash Folder", 1, 0777, 1},
154 {"Temporary Items", 1, 0777, 1},
155 {".AppleDesktop", 1, 0777, 0},
157 {"TheFindByContentFolder", 0, 0, 1},
158 {"TheVolumeSettingsFolder", 0, 0, 1},
162 typedef struct _volopt_name {
163 const u_int32_t option;
168 /* Keep in sync with libatalk/util/volinfo.c ! */
169 static const _vol_opt_name vol_opt_names[] = {
170 {AFPVOL_A2VOL, "PRODOS"}, /* prodos volume */
171 {AFPVOL_CRLF, "CRLF"}, /* cr/lf translation */
172 {AFPVOL_NOADOUBLE, "NOADOUBLE"}, /* don't create .AppleDouble by default */
173 {AFPVOL_RO, "READONLY"}, /* read-only volume */
174 {AFPVOL_MSWINDOWS, "MSWINDOWS"}, /* deal with ms-windows yuckiness. this is going away. */
175 {AFPVOL_NOHEX, "NOHEX"}, /* don't do :hex translation */
176 {AFPVOL_USEDOTS, "USEDOTS"}, /* use real dots */
177 {AFPVOL_LIMITSIZE, "LIMITSIZE"}, /* limit size for older macs */
178 {AFPVOL_MAPASCII, "MAPASCII"}, /* map the ascii range as well */
179 {AFPVOL_DROPBOX, "DROPBOX"}, /* dropkludge dropbox support */
180 {AFPVOL_NOFILEID, "NOFILEID"}, /* don't advertise createid resolveid and deleteid calls */
181 {AFPVOL_NOSTAT, "NOSTAT"}, /* advertise the volume even if we can't stat() it
182 * maybe because it will be mounted later in preexec */
183 {AFPVOL_UNIX_PRIV, "UNIXPRIV"}, /* support unix privileges */
184 {AFPVOL_NODEV, "NODEV"}, /* always use 0 for device number in cnid calls */
185 {AFPVOL_CASEINSEN, "CASEINSENSITIVE"}, /* volume is case insensitive */
186 {AFPVOL_EILSEQ, "ILLEGALSEQ"}, /* encode illegal sequence */
187 {AFPVOL_CACHE, "CACHEID"}, /* Use adouble v2 CNID caching. Default: yes */
188 {AFPVOL_ACLS, "ACLS"}, /* Vol supports ACLs */
189 {AFPVOL_TM, "TM"}, /* Set "kSupportsTMLockSteal" is volume attributes */
193 static const _vol_opt_name vol_opt_casefold[] = {
194 {AFPVOL_MTOUUPPER, "MTOULOWER"},
195 {AFPVOL_MTOULOWER, "MTOULOWER"},
196 {AFPVOL_UTOMUPPER, "UTOMUPPER"},
197 {AFPVOL_UTOMLOWER, "UTOMLOWER"},
201 static void handle_special_folders (const struct vol *);
202 static int savevoloptions (const struct vol *);
203 static void deletevol(struct vol *vol);
204 static void volume_free(struct vol *vol);
206 static void volfree(struct vol_option *options,
207 const struct vol_option *save)
212 for (i = 0; i < VOLOPT_MAX; i++) {
213 if (options[i].c_value && (options[i].c_value != save[i].c_value))
214 free(options[i].c_value);
217 for (i = 0; i < VOLOPT_MAX; i++) {
218 if (options[i].c_value)
219 free(options[i].c_value);
225 /* handle variable substitutions. here's what we understand:
226 * $b -> basename of path
227 * $c -> client ip/appletalk address
228 * $d -> volume pathname on server
229 * $f -> full name (whatever's in the gecos field)
232 * $i -> client ip/appletalk address without port
233 * $s -> server name (hostname if it doesn't exist)
234 * $u -> username (guest is usually nobody)
235 * $v -> volume name or basename if null
236 * $z -> zone (may not exist)
241 #define is_var(a, b) (strncmp((a), (b), 2) == 0)
243 static char *volxlate(AFPObj *obj, char *dest, size_t destlen,
244 char *src, struct passwd *pwd, char *path, char *volname)
254 dest = calloc(destlen +1, 1);
260 strlcpy(dest, src, destlen +1);
261 if ((p = strchr(src, '$')) == NULL) /* nothing to do */
264 /* first part of the path. just forward to the next variable. */
265 len = MIN((size_t)(p - src), destlen);
271 while (p && destlen > 0) {
272 /* now figure out what the variable is */
274 if (is_var(p, "$b")) {
276 if ((q = strrchr(path, '/')) == NULL)
278 else if (*(q + 1) != '\0')
281 } else if (is_var(p, "$c")) {
282 if (obj->proto == AFPPROTO_ASP) {
283 ASP asp = obj->handle;
285 len = sprintf(dest, "%u.%u", ntohs(asp->asp_sat.sat_addr.s_net),
286 asp->asp_sat.sat_addr.s_node);
290 } else if (obj->proto == AFPPROTO_DSI) {
291 DSI *dsi = obj->handle;
293 len = sprintf(dest, "%s:%u", inet_ntoa(dsi->client.sin_addr),
294 ntohs(dsi->client.sin_port));
298 } else if (is_var(p, "$d")) {
300 } else if (is_var(p, "$f")) {
301 if ((q = strchr(pwd->pw_gecos, ',')))
304 } else if (is_var(p, "$g")) {
305 struct group *grp = getgrgid(pwd->pw_gid);
308 } else if (is_var(p, "$h")) {
309 q = obj->options.hostname;
310 } else if (is_var(p, "$i")) {
311 if (obj->proto == AFPPROTO_ASP) {
312 ASP asp = obj->handle;
314 len = sprintf(dest, "%u", ntohs(asp->asp_sat.sat_addr.s_net));
318 } else if (obj->proto == AFPPROTO_DSI) {
319 DSI *dsi = obj->handle;
321 q = inet_ntoa(dsi->client.sin_addr);
323 } else if (is_var(p, "$s")) {
326 else if (obj->options.server) {
327 q = obj->options.server;
329 q = obj->options.hostname;
330 } else if (is_var(p, "$u")) {
332 if ( obj->options.ntseparator && (sep = strchr(obj->username, obj->options.ntseparator[0])) != NULL)
336 } else if (is_var(p, "$v")) {
341 if ((q = strrchr(path, '/')) == NULL)
343 else if (*(q + 1) != '\0')
346 } else if (is_var(p, "$z")) {
348 } else if (is_var(p, "$$")) {
353 /* copy the stuff over. if we don't understand something that we
354 * should, just skip it over. */
356 len = MIN(p == q ? 2 : strlen(q), destlen);
357 strncpy(dest, q, len);
362 /* stuff up to next $ */
364 p = strchr(src, '$');
365 len = p ? MIN((size_t)(p - src), destlen) : destlen;
367 strncpy(dest, src, len);
375 /* to make sure that val is valid, make sure to select an opt that
377 static int optionok(const char *buf, const char *opt, const char *val)
379 if (!strstr(buf,opt))
387 /* -------------------- */
388 static void setoption(struct vol_option *options, struct vol_option *save, int opt, const char *val)
390 if (options[opt].c_value && (!save || options[opt].c_value != save[opt].c_value))
391 free(options[opt].c_value);
392 options[opt].c_value = strdup(val + 1);
395 /* ------------------------------------------
396 handle all the options. tmp can't be NULL. */
397 static void volset(struct vol_option *options, struct vol_option *save,
398 char *volname, int vlen,
403 val = strchr(tmp, ':');
405 /* we'll assume it's a volume name. */
406 strncpy(volname, tmp, vlen);
411 LOG(log_debug, logtype_afpd, "Parsing volset %s", val);
413 if (optionok(tmp, "allow:", val)) {
414 setoption(options, save, VOLOPT_ALLOW, val);
416 } else if (optionok(tmp, "deny:", val)) {
417 setoption(options, save, VOLOPT_DENY, val);
419 } else if (optionok(tmp, "rwlist:", val)) {
420 setoption(options, save, VOLOPT_RWLIST, val);
422 } else if (optionok(tmp, "rolist:", val)) {
423 setoption(options, save, VOLOPT_ROLIST, val);
425 } else if (optionok(tmp, "codepage:", val)) {
426 LOG (log_error, logtype_afpd, "The old codepage system has been removed. Please make sure to read the documentation !!!!");
427 /* Make sure we don't screw anything */
429 } else if (optionok(tmp, "volcharset:", val)) {
430 setoption(options, save, VOLOPT_ENCODING, val);
431 } else if (optionok(tmp, "maccharset:", val)) {
432 setoption(options, save, VOLOPT_MACCHARSET, val);
433 } else if (optionok(tmp, "veto:", val)) {
434 setoption(options, save, VOLOPT_VETO, val);
435 } else if (optionok(tmp, "cnidscheme:", val)) {
436 setoption(options, save, VOLOPT_CNIDSCHEME, val);
437 } else if (optionok(tmp, "casefold:", val)) {
438 if (strcasecmp(val + 1, "tolower") == 0)
439 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMLOWER;
440 else if (strcasecmp(val + 1, "toupper") == 0)
441 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMUPPER;
442 else if (strcasecmp(val + 1, "xlatelower") == 0)
443 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UUPPERMLOWER;
444 else if (strcasecmp(val + 1, "xlateupper") == 0)
445 options[VOLOPT_CASEFOLD].i_value = AFPVOL_ULOWERMUPPER;
446 } else if (optionok(tmp, "adouble:", val)) {
447 if (strcasecmp(val + 1, "v1") == 0)
448 options[VOLOPT_ADOUBLE].i_value = AD_VERSION1;
449 #if AD_VERSION == AD_VERSION2
450 else if (strcasecmp(val + 1, "v2") == 0)
451 options[VOLOPT_ADOUBLE].i_value = AD_VERSION2;
452 else if (strcasecmp(val + 1, "osx") == 0)
453 options[VOLOPT_ADOUBLE].i_value = AD_VERSION2_OSX;
454 else if (strcasecmp(val + 1, "sfm") == 0)
455 options[VOLOPT_ADOUBLE].i_value = AD_VERSION1_SFM;
457 } else if (optionok(tmp, "options:", val)) {
460 if ((p = strtok(val + 1, ",")) == NULL) /* nothing */
464 if (strcasecmp(p, "prodos") == 0)
465 options[VOLOPT_FLAGS].i_value |= AFPVOL_A2VOL;
466 else if (strcasecmp(p, "mswindows") == 0) {
467 options[VOLOPT_FLAGS].i_value |= AFPVOL_MSWINDOWS | AFPVOL_USEDOTS;
468 } else if (strcasecmp(p, "crlf") == 0)
469 options[VOLOPT_FLAGS].i_value |= AFPVOL_CRLF;
470 else if (strcasecmp(p, "noadouble") == 0)
471 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOADOUBLE;
472 else if (strcasecmp(p, "ro") == 0)
473 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
474 else if (strcasecmp(p, "nohex") == 0)
475 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOHEX;
476 else if (strcasecmp(p, "usedots") == 0)
477 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS;
478 else if (strcasecmp(p, "invisibledots") == 0)
479 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS | AFPVOL_INV_DOTS;
480 else if (strcasecmp(p, "limitsize") == 0)
481 options[VOLOPT_FLAGS].i_value |= AFPVOL_LIMITSIZE;
482 /* support for either "dropbox" or "dropkludge" */
483 else if (strcasecmp(p, "dropbox") == 0)
484 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
485 else if (strcasecmp(p, "dropkludge") == 0)
486 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
487 else if (strcasecmp(p, "nofileid") == 0)
488 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOFILEID;
489 else if (strcasecmp(p, "nostat") == 0)
490 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOSTAT;
491 else if (strcasecmp(p, "preexec_close") == 0)
492 options[VOLOPT_PREEXEC].i_value = 1;
493 else if (strcasecmp(p, "root_preexec_close") == 0)
494 options[VOLOPT_ROOTPREEXEC].i_value = 1;
495 else if (strcasecmp(p, "upriv") == 0)
496 options[VOLOPT_FLAGS].i_value |= AFPVOL_UNIX_PRIV;
497 else if (strcasecmp(p, "acls") == 0)
498 options[VOLOPT_FLAGS].i_value |= AFPVOL_ACLS;
499 else if (strcasecmp(p, "nodev") == 0)
500 options[VOLOPT_FLAGS].i_value |= AFPVOL_NODEV;
501 else if (strcasecmp(p, "caseinsensitive") == 0)
502 options[VOLOPT_FLAGS].i_value |= AFPVOL_CASEINSEN;
503 else if (strcasecmp(p, "illegalseq") == 0)
504 options[VOLOPT_FLAGS].i_value |= AFPVOL_EILSEQ;
505 else if (strcasecmp(p, "nocnidcache") == 0)
506 options[VOLOPT_FLAGS].i_value &= ~AFPVOL_CACHE;
507 else if (strcasecmp(p, "tm") == 0)
508 options[VOLOPT_FLAGS].i_value |= AFPVOL_TM;
510 p = strtok(NULL, ",");
513 } else if (optionok(tmp, "dbpath:", val)) {
514 setoption(options, save, VOLOPT_DBPATH, val);
516 } else if (optionok(tmp, "umask:", val)) {
517 options[VOLOPT_UMASK].i_value = (int)strtol(val +1, NULL, 8);
518 } else if (optionok(tmp, "dperm:", val)) {
519 options[VOLOPT_DPERM].i_value = (int)strtol(val+1, NULL, 8);
520 } else if (optionok(tmp, "fperm:", val)) {
521 options[VOLOPT_FPERM].i_value = (int)strtol(val+1, NULL, 8);
522 } else if (optionok(tmp, "perm:", val)) {
523 options[VOLOPT_DFLTPERM].i_value = (int)strtol(val+1, NULL, 8);
524 } else if (optionok(tmp, "mapchars:",val)) {
525 setoption(options, save, VOLOPT_MAPCHARS, val);
527 } else if (optionok(tmp, "password:", val)) {
528 setoption(options, save, VOLOPT_PASSWORD, val);
532 /* this code allows forced uid/gid per volume settings */
533 } else if (optionok(tmp, "forceuid:", val)) {
534 setoption(options, save, VOLOPT_FORCEUID, val);
535 } else if (optionok(tmp, "forcegid:", val)) {
536 setoption(options, save, VOLOPT_FORCEGID, val);
538 #endif /* FORCE_UIDGID */
539 } else if (optionok(tmp, "root_preexec:", val)) {
540 setoption(options, save, VOLOPT_ROOTPREEXEC, val);
542 } else if (optionok(tmp, "preexec:", val)) {
543 setoption(options, save, VOLOPT_PREEXEC, val);
545 } else if (optionok(tmp, "root_postexec:", val)) {
546 setoption(options, save, VOLOPT_ROOTPOSTEXEC, val);
548 } else if (optionok(tmp, "postexec:", val)) {
549 setoption(options, save, VOLOPT_POSTEXEC, val);
551 } else if (optionok(tmp, "allowed_hosts:", val)) {
552 setoption(options, save, VOLOPT_ALLOWED_HOSTS, val);
554 } else if (optionok(tmp, "denied_hosts:", val)) {
555 setoption(options, save, VOLOPT_DENIED_HOSTS, val);
557 } else if (optionok(tmp, "ea:", val)) {
558 if (strcasecmp(val + 1, "ad") == 0) /* the default anyway */
559 options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_AD;
560 else if (strcasecmp(val + 1, "solaris") == 0)
561 options[VOLOPT_EA_VFS].i_value = AFPVOL_EA_SOLARIS;
564 /* ignore unknown options */
565 LOG(log_debug, logtype_afpd, "ignoring unknown volume option: %s", tmp);
570 /* ----------------- */
571 static void showvol(const ucs2_t *name)
574 for ( volume = Volumes; volume; volume = volume->v_next ) {
575 if (volume->v_hide && !strcasecmp_w( volume->v_name, name ) ) {
582 /* ------------------------------- */
583 static int creatvol(AFPObj *obj, struct passwd *pwd,
584 char *path, char *name,
585 struct vol_option *options,
586 const int user /* user defined volume */
590 int suffixlen, vlen, tmpvlen, u8mvlen, macvlen;
592 char tmpname[AFPVOL_U8MNAMELEN+1];
593 ucs2_t u8mtmpname[(AFPVOL_U8MNAMELEN+1)*2], mactmpname[(AFPVOL_MACNAMELEN+1)*2];
594 char suffix[6]; /* max is #FFFF */
597 if ( name == NULL || *name == '\0' ) {
598 if ((name = strrchr( path, '/' )) == NULL) {
599 return -1; /* Obviously not a fully qualified path */
602 /* if you wish to share /, you need to specify a name. */
607 /* suffix for mangling use (lastvid + 1) */
608 /* because v_vid has not been decided yet. */
609 suffixlen = sprintf(suffix, "%c%X", MANGLE_CHAR, lastvid + 1 );
611 vlen = strlen( name );
613 /* Unicode Volume Name */
614 /* Firstly convert name from unixcharset to UTF8-MAC */
616 tmpvlen = convert_charset(obj->options.unixcharset, CH_UTF8_MAC, 0, name, vlen, tmpname, AFPVOL_U8MNAMELEN, &flags);
618 strcpy(tmpname, "???");
622 /* Do we have to mangle ? */
623 if ( (flags & CONV_REQMANGLE) || (tmpvlen > obj->options.volnamelen)) {
624 if (tmpvlen + suffixlen > obj->options.volnamelen) {
626 tmpvlen = convert_charset(obj->options.unixcharset, CH_UTF8_MAC, 0, name, vlen, tmpname, obj->options.volnamelen - suffixlen, &flags);
627 tmpname[tmpvlen != (size_t)-1 ? tmpvlen : 0] = 0;
629 strcat(tmpname, suffix);
630 tmpvlen = strlen(tmpname);
633 /* Secondly convert name from UTF8-MAC to UCS2 */
634 if ( 0 >= ( u8mvlen = convert_string(CH_UTF8_MAC, CH_UCS2, tmpname, tmpvlen, u8mtmpname, AFPVOL_U8MNAMELEN*2)) )
637 LOG(log_debug, logtype_afpd, "createvol: Volume '%s' -> UTF8-MAC Name: '%s'", name, tmpname);
639 /* Maccharset Volume Name */
640 /* Firsty convert name from unixcharset to maccharset */
642 tmpvlen = convert_charset(obj->options.unixcharset, obj->options.maccharset, 0, name, vlen, tmpname, AFPVOL_U8MNAMELEN, &flags);
644 strcpy(tmpname, "???");
648 /* Do we have to mangle ? */
649 if ( (flags & CONV_REQMANGLE) || (tmpvlen > AFPVOL_MACNAMELEN)) {
650 if (tmpvlen + suffixlen > AFPVOL_MACNAMELEN) {
652 tmpvlen = convert_charset(obj->options.unixcharset, obj->options.maccharset, 0, name, vlen, tmpname, AFPVOL_MACNAMELEN - suffixlen, &flags);
653 tmpname[tmpvlen != (size_t)-1 ? tmpvlen : 0] = 0;
655 strcat(tmpname, suffix);
656 tmpvlen = strlen(tmpname);
659 /* Secondly convert name from maccharset to UCS2 */
660 if ( 0 >= ( macvlen = convert_string(obj->options.maccharset, CH_UCS2, tmpname, tmpvlen, mactmpname, AFPVOL_U8MNAMELEN*2)) )
663 LOG(log_debug, logtype_afpd, "createvol: Volume '%s' -> Longname: '%s'", name, tmpname);
665 /* check duplicate */
666 for ( volume = Volumes; volume; volume = volume->v_next ) {
667 if (( strcasecmp_w( volume->v_u8mname, u8mtmpname ) == 0 ) || ( strcasecmp_w( volume->v_macname, mactmpname ) == 0 )){
668 if (volume->v_deleted) {
669 volume->v_new = hide = 1;
672 return -1; /* Won't be able to access it, anyway... */
677 if (!( volume = (struct vol *)calloc(1, sizeof( struct vol ))) ) {
678 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
681 if ( NULL == ( volume->v_localname = strdup(name))) {
682 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
687 if ( NULL == ( volume->v_u8mname = strdup_w(u8mtmpname))) {
688 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
693 if ( NULL == ( volume->v_macname = strdup_w(mactmpname))) {
694 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
699 if (!( volume->v_path = (char *)malloc( strlen( path ) + 1 )) ) {
700 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
706 volume->v_name = utf8_encoding()?volume->v_u8mname:volume->v_macname;
707 volume->v_hide = hide;
708 strcpy( volume->v_path, path );
712 #endif /* __svr4__ */
713 /* os X start at 1 and use network order ie. 1 2 3 */
714 volume->v_vid = ++lastvid;
715 volume->v_vid = htons(volume->v_vid);
716 volume->v_vfs_ea = AFPVOL_EA_AD;
720 /* should we casefold? */
721 volume->v_casefold = options[VOLOPT_CASEFOLD].i_value;
723 /* shift in some flags */
724 volume->v_flags = options[VOLOPT_FLAGS].i_value;
726 if (options[VOLOPT_EA_VFS].i_value != AFPVOL_EA_AD)
727 volume->v_vfs_ea = options[VOLOPT_EA_VFS].i_value;
729 volume->v_ad_options = 0;
730 if ((volume->v_flags & AFPVOL_NODEV))
731 volume->v_ad_options |= ADVOL_NODEV;
732 if ((volume->v_flags & AFPVOL_CACHE))
733 volume->v_ad_options |= ADVOL_CACHE;
734 if ((volume->v_flags & AFPVOL_UNIX_PRIV))
735 volume->v_ad_options |= ADVOL_UNIXPRIV;
736 if ((volume->v_flags & AFPVOL_INV_DOTS))
737 volume->v_ad_options |= ADVOL_INVDOTS;
739 if (options[VOLOPT_PASSWORD].c_value)
740 volume->v_password = strdup(options[VOLOPT_PASSWORD].c_value);
742 if (options[VOLOPT_VETO].c_value)
743 volume->v_veto = strdup(options[VOLOPT_VETO].c_value);
745 if (options[VOLOPT_ENCODING].c_value)
746 volume->v_volcodepage = strdup(options[VOLOPT_ENCODING].c_value);
748 if (options[VOLOPT_MACCHARSET].c_value)
749 volume->v_maccodepage = strdup(options[VOLOPT_MACCHARSET].c_value);
751 if (options[VOLOPT_DBPATH].c_value)
752 volume->v_dbpath = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_DBPATH].c_value, pwd, path, name);
754 if (options[VOLOPT_CNIDSCHEME].c_value)
755 volume->v_cnidscheme = strdup(options[VOLOPT_CNIDSCHEME].c_value);
757 if (options[VOLOPT_UMASK].i_value)
758 volume->v_umask = (mode_t)options[VOLOPT_UMASK].i_value;
760 if (options[VOLOPT_DPERM].i_value)
761 volume->v_dperm = (mode_t)options[VOLOPT_DPERM].i_value;
763 if (options[VOLOPT_FPERM].i_value)
764 volume->v_fperm = (mode_t)options[VOLOPT_FPERM].i_value;
766 if (options[VOLOPT_DFLTPERM].i_value)
767 volume->v_perm = (mode_t)options[VOLOPT_DFLTPERM].i_value;
769 if (options[VOLOPT_ADOUBLE].i_value)
770 volume->v_adouble = options[VOLOPT_ADOUBLE].i_value;
772 volume->v_adouble = AD_VERSION;
776 if (options[VOLOPT_FORCEUID].c_value) {
777 volume->v_forceuid = strdup(options[VOLOPT_FORCEUID].c_value);
779 volume->v_forceuid = NULL; /* set as null so as to return 0 later on */
782 if (options[VOLOPT_FORCEGID].c_value) {
783 volume->v_forcegid = strdup(options[VOLOPT_FORCEGID].c_value);
785 volume->v_forcegid = NULL; /* set as null so as to return 0 later on */
789 if (options[VOLOPT_PREEXEC].c_value)
790 volume->v_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_PREEXEC].c_value, pwd, path, name);
791 volume->v_preexec_close = options[VOLOPT_PREEXEC].i_value;
793 if (options[VOLOPT_POSTEXEC].c_value)
794 volume->v_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_POSTEXEC].c_value, pwd, path, name);
796 if (options[VOLOPT_ROOTPREEXEC].c_value)
797 volume->v_root_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPREEXEC].c_value, pwd, path, name);
798 volume->v_root_preexec_close = options[VOLOPT_ROOTPREEXEC].i_value;
800 if (options[VOLOPT_ROOTPOSTEXEC].c_value)
801 volume->v_root_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPOSTEXEC].c_value, pwd, path, name);
804 volume->v_dperm |= volume->v_perm;
805 volume->v_fperm |= volume->v_perm;
807 volume->v_next = Volumes;
812 /* ---------------- */
813 static char *myfgets( char *buf, int size, FILE *fp)
819 while ((EOF != ( c = getc( fp )) ) && ( size > 1 )) {
820 if ( c == '\n' || c == '\r' ) {
821 if (p != buf && *(p -1) == '\\') {
843 /* check access list. this function wants something of the following
845 * @group,name,name2,@group2,name3
847 * a NULL argument allows everybody to have access.
848 * we return three things:
850 * 0: list exists, but name isn't in it
854 #ifndef NO_REAL_USER_NAME
855 /* authentication is case insensitive
856 * FIXME should we do the same with group name?
858 #define access_strcmp strcasecmp
861 #define access_strcmp strcmp
865 static int accessvol(const char *args, const char *name)
867 char buf[MAXPATHLEN + 1], *p;
873 strlcpy(buf, args, sizeof(buf));
874 if ((p = strtok(buf, ",")) == NULL) /* nothing, return okay */
878 if (*p == '@') { /* it's a group */
879 if ((gr = getgrnam(p + 1)) && gmem(gr->gr_gid))
881 } else if (access_strcmp(p, name) == 0) /* it's a user name */
883 p = strtok(NULL, ",");
889 static int hostaccessvol(int type, char *volname, const char *args, const AFPObj *obj)
891 char buf[MAXPATHLEN + 1], *p, *b;
892 DSI *dsi = obj->handle;
897 strlcpy(buf, args, sizeof(buf));
898 if ((p = strtok_r(buf, ",", &b)) == NULL) /* nothing, return okay */
902 if (obj->proto == AFPPROTO_DSI) {
903 struct in_addr mask, net;
904 char *net_char, *mask_char;
907 net_char = strtok(p, "/");
908 mask_char = strtok(NULL,"/");
909 if (mask_char == NULL) {
912 mask_int = atoi(mask_char);
915 // convert the integer netmask to a bitmask in network order
916 mask.s_addr = htonl(-1 - ((1 << (32 - mask_int)) - 1));
917 net.s_addr = inet_addr(net_char) & mask.s_addr;
919 if ((dsi->client.sin_addr.s_addr & mask.s_addr) == net.s_addr) {
920 if (type == VOLOPT_DENIED_HOSTS)
921 LOG(log_info, logtype_afpd, "AFP access denied for client IP '%s' to volume '%s' by denied list",
922 inet_ntoa(dsi->client.sin_addr), volname);
926 p = strtok_r(NULL, ",", &b);
928 if (type == VOLOPT_ALLOWED_HOSTS)
929 LOG(log_info, logtype_afpd, "AFP access denied for client IP '%s' to volume '%s', not in allowed list",
930 inet_ntoa(dsi->client.sin_addr), volname);
934 static void setextmap(char *ext, char *type, char *creator, int user)
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(AFPObj *obj, struct afp_volume_name *p1, char *p2, int user, struct passwd *pwent)
1063 char path[ MAXPATHLEN + 1], tmp[ MAXPATHLEN + 1],
1064 volname[ AFPVOL_U8MNAMELEN + 1 ], buf[ BUFSIZ ],
1065 type[ 5 ], creator[ 5 ];
1068 struct vol_option options[VOLOPT_NUM], save_options[VOLOPT_NUM];
1076 strcpy( path, p1->name );
1078 strcat( path, "/" );
1080 if (p1->full_name) {
1081 free(p1->full_name);
1083 p1->full_name = strdup(path);
1086 if (NULL == ( fp = fopen( path, "r" )) ) {
1090 if (fd != -1 && !fstat( fd, &st) ) {
1091 p1->mtime = st.st_mtime;
1094 memset(save_options, 0, sizeof(save_options));
1096 /* Enable some default options for all volumes */
1097 save_options[VOLOPT_FLAGS].i_value |= AFPVOL_CACHE;
1099 while ( myfgets( buf, sizeof( buf ), fp ) != NULL ) {
1100 initline( strlen( buf ), buf );
1101 parseline( sizeof( path ) - 1, path );
1108 /* change the default options for this file */
1109 if (strncmp(path, VOLOPT_DEFAULT, VOLOPT_DEFAULT_LEN) == 0) {
1111 for (i = 0; i < VOLOPT_NUM; i++) {
1112 if (parseline( sizeof( path ) - VOLOPT_DEFAULT_LEN - 1,
1113 path + VOLOPT_DEFAULT_LEN) < 0)
1115 volset(save_options, NULL, tmp, sizeof(tmp) - 1,
1116 path + VOLOPT_DEFAULT_LEN);
1122 if (( p = strchr( path, '/' )) != NULL ) {
1130 if ( u == NULL || *u == '\0' || ( pw = getpwnam( u )) == NULL ) {
1133 strcpy( tmp, pw->pw_dir );
1134 if ( p != NULL && *p != '\0' ) {
1138 /* Tag a user's home directory with their umask. Note, this will
1139 * be overwritten if the user actually specifies a umask: option
1140 * for a '~' volume. */
1141 save_options[VOLOPT_UMASK].i_value = obj->options.save_mask;
1145 /* send path through variable substitution */
1146 if (*path != '~') /* need to copy path to tmp */
1149 pwent = getpwnam(obj->username);
1150 volxlate(obj, path, sizeof(path) - 1, tmp, pwent, NULL, NULL);
1152 /* this is sort of braindead. basically, i want to be
1153 * able to specify things in any order, but i don't want to
1154 * re-write everything.
1156 * currently we have options:
1164 * options:prodos,crlf,noadouble,ro...
1169 * namemask:x,y,!z (not implemented yet)
1171 memcpy(options, save_options, sizeof(options));
1174 /* read in up to VOLOP_NUM possible options */
1175 for (i = 0; i < VOLOPT_NUM; i++) {
1176 if (parseline( sizeof( tmp ) - 1, tmp ) < 0)
1179 volset(options, save_options, volname, sizeof(volname) - 1, tmp);
1182 /* check allow/deny lists:
1183 allow -> either no list (-1), or in list (1)
1184 deny -> either no list (-1), or not in list (0) */
1185 if (accessvol(options[VOLOPT_ALLOW].c_value, obj->username) &&
1186 (accessvol(options[VOLOPT_DENY].c_value, obj->username) < 1) &&
1187 hostaccessvol(VOLOPT_ALLOWED_HOSTS, volname, options[VOLOPT_ALLOWED_HOSTS].c_value, obj) &&
1188 (hostaccessvol(VOLOPT_DENIED_HOSTS, volname, options[VOLOPT_DENIED_HOSTS].c_value, obj) < 1)) {
1190 /* handle read-only behaviour. semantics:
1191 * 1) neither the rolist nor the rwlist exist -> rw
1192 * 2) rolist exists -> ro if user is in it.
1193 * 3) rwlist exists -> ro unless user is in it. */
1194 if (((options[VOLOPT_FLAGS].i_value & AFPVOL_RO) == 0) &&
1195 ((accessvol(options[VOLOPT_ROLIST].c_value,
1196 obj->username) == 1) ||
1197 !accessvol(options[VOLOPT_RWLIST].c_value,
1199 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
1201 /* do variable substitution for volname */
1202 volxlate(obj, tmp, sizeof(tmp) - 1, volname, pwent, path, NULL);
1203 creatvol(obj, pwent, path, tmp, options, p2 != NULL);
1205 volfree(options, save_options);
1209 parseline( sizeof( type ) - 1, type );
1210 parseline( sizeof( creator ) - 1, creator );
1211 setextmap( path, type, creator, user);
1218 volfree(save_options, NULL);
1220 if ( fclose( fp ) != 0 ) {
1221 LOG(log_error, logtype_afpd, "readvolfile: fclose: %s", strerror(errno) );
1227 /* ------------------------------- */
1228 static void volume_free(struct vol *vol)
1230 free(vol->v_localname);
1231 vol->v_localname = NULL;
1232 free(vol->v_u8mname);
1233 vol->v_u8mname = NULL;
1234 free(vol->v_macname);
1235 vol->v_macname = NULL;
1237 free(vol->v_password);
1239 free(vol->v_volcodepage);
1240 free(vol->v_maccodepage);
1241 free(vol->v_cnidscheme);
1242 free(vol->v_dbpath);
1245 free(vol->v_forceuid);
1246 free(vol->v_forcegid);
1247 #endif /* FORCE_UIDGID */
1250 /* ------------------------------- */
1251 static void free_volumes(void )
1254 struct vol *nvol, *ovol;
1256 for ( vol = Volumes; vol; vol = vol->v_next ) {
1257 if (( vol->v_flags & AFPVOL_OPEN ) ) {
1264 for ( vol = Volumes, ovol = NULL; vol; vol = nvol) {
1267 if (vol->v_localname == NULL) {
1268 if (Volumes == vol) {
1273 ovol->v_next = nvol;
1283 /* ------------------------------- */
1284 static void volume_unlink(struct vol *volume)
1286 struct vol *vol, *ovol, *nvol;
1288 if (volume == Volumes) {
1289 Volumes = Volumes->v_next;
1292 for ( vol = Volumes->v_next, ovol = Volumes; vol; vol = nvol) {
1295 if (vol == volume) {
1296 ovol->v_next = nvol;
1305 static int getvolspace(struct vol *vol,
1306 u_int32_t *bfree, u_int32_t *btotal,
1307 VolSpace *xbfree, VolSpace *xbtotal, u_int32_t *bsize)
1311 #ifndef NO_QUOTA_SUPPORT
1312 VolSpace qfree, qtotal;
1315 spaceflag = AFPVOL_GVSMASK & vol->v_flags;
1316 /* report up to 2GB if afp version is < 2.2 (4GB if not) */
1317 maxsize = (vol->v_flags & AFPVOL_A2VOL) ? 0x01fffe00 :
1318 (((afp_version < 22) || (vol->v_flags & AFPVOL_LIMITSIZE))
1319 ? 0x7fffffffL : 0xffffffffL);
1322 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_AFSGVS ) {
1323 if ( afs_getvolspace( vol, xbfree, xbtotal, bsize ) == AFP_OK ) {
1324 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_AFSGVS;
1325 goto getvolspace_done;
1330 if (( rc = ustatfs_getvolspace( vol, xbfree, xbtotal,
1331 bsize)) != AFP_OK ) {
1335 #define min(a,b) ((a)<(b)?(a):(b))
1336 #ifndef NO_QUOTA_SUPPORT
1337 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_UQUOTA ) {
1338 if ( uquota_getvolspace( vol, &qfree, &qtotal, *bsize ) == AFP_OK ) {
1339 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_UQUOTA;
1340 *xbfree = min(*xbfree, qfree);
1341 *xbtotal = min( *xbtotal, qtotal);
1342 goto getvolspace_done;
1346 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_USTATFS;
1349 *bfree = min( *xbfree, maxsize);
1350 *btotal = min( *xbtotal, maxsize);
1354 /* -----------------------
1355 * set volume creation date
1356 * avoid duplicate, well at least it tries
1358 static void vol_setdate(u_int16_t id, struct adouble *adp, time_t date)
1361 struct vol *vol = Volumes;
1363 for ( volume = Volumes; volume; volume = volume->v_next ) {
1364 if (volume->v_vid == id) {
1367 else if ((time_t)(AD_DATE_FROM_UNIX(date)) == volume->v_ctime) {
1369 volume = Volumes; /* restart */
1372 vol->v_ctime = AD_DATE_FROM_UNIX(date);
1373 ad_setdate(adp, AD_DATE_CREATE | AD_DATE_UNIX, date);
1376 /* ----------------------- */
1377 static int getvolparams( u_int16_t bitmap, struct vol *vol, struct stat *st, char *buf, int *buflen)
1380 int bit = 0, isad = 1;
1383 u_int32_t bfree, btotal, bsize;
1384 VolSpace xbfree, xbtotal; /* extended bytes */
1385 char *data, *nameoff = NULL;
1388 LOG(log_debug, logtype_afpd, "getvolparams: Volume '%s'", vol->v_localname);
1390 /* courtesy of jallison@whistle.com:
1391 * For MacOS8.x support we need to create the
1392 * .Parent file here if it doesn't exist. */
1394 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1395 if ( ad_open_metadata( vol->v_path, vol_noadouble(vol) | ADFLAGS_DIR, O_CREAT, &ad) < 0 ) {
1397 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1399 } else if (ad_get_MD_flags( &ad ) & O_CREAT) {
1400 slash = strrchr( vol->v_path, '/' );
1404 slash = vol->v_path;
1405 if (ad_getentryoff(&ad, ADEID_NAME)) {
1406 ad_setentrylen( &ad, ADEID_NAME, strlen( slash ));
1407 memcpy(ad_entry( &ad, ADEID_NAME ), slash,
1408 ad_getentrylen( &ad, ADEID_NAME ));
1410 vol_setdate(vol->v_vid, &ad, st->st_mtime);
1414 if (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0)
1415 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1417 vol->v_ctime = aint;
1420 if (( bitmap & ( (1<<VOLPBIT_BFREE)|(1<<VOLPBIT_BTOTAL) |
1421 (1<<VOLPBIT_XBFREE)|(1<<VOLPBIT_XBTOTAL) |
1422 (1<<VOLPBIT_BSIZE)) ) != 0 ) {
1423 if ( getvolspace( vol, &bfree, &btotal, &xbfree, &xbtotal,
1426 ad_close( &ad, ADFLAGS_HF );
1428 return( AFPERR_PARAM );
1433 while ( bitmap != 0 ) {
1434 while (( bitmap & 1 ) == 0 ) {
1442 if (0 == (vol->v_flags & AFPVOL_NOFILEID) && vol->v_cdb != NULL &&
1443 (vol->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1444 ashort = VOLPBIT_ATTR_FILEID;
1446 /* check for read-only.
1447 * NOTE: we don't actually set the read-only flag unless
1448 * it's passed in that way as it's possible to mount
1449 * a read-write filesystem under a read-only one. */
1450 if ((vol->v_flags & AFPVOL_RO) ||
1451 ((utime(vol->v_path, NULL) < 0) && (errno == EROFS))) {
1452 ashort |= VOLPBIT_ATTR_RO;
1454 ashort |= VOLPBIT_ATTR_CATSEARCH;
1455 ashort |= VOLPBIT_ATTR_EXT_ATTRS;
1456 if (afp_version >= 30) {
1457 ashort |= VOLPBIT_ATTR_UTF8;
1458 if (vol->v_flags & AFPVOL_UNIX_PRIV)
1459 ashort |= VOLPBIT_ATTR_UNIXPRIV;
1460 if (vol->v_flags & AFPVOL_TM)
1461 ashort |= VOLPBIT_ATTR_TM;
1463 if (afp_version >= 32) {
1464 if (vol->v_flags & AFPVOL_ACLS)
1465 ashort |= VOLPBIT_ATTR_ACLS;
1467 ashort = htons(ashort);
1468 memcpy(data, &ashort, sizeof( ashort ));
1469 data += sizeof( ashort );
1473 ashort = htons( AFPVOLSIG_DEFAULT );
1474 memcpy(data, &ashort, sizeof( ashort ));
1475 data += sizeof( ashort );
1478 case VOLPBIT_CDATE :
1479 aint = vol->v_ctime;
1480 memcpy(data, &aint, sizeof( aint ));
1481 data += sizeof( aint );
1484 case VOLPBIT_MDATE :
1485 if ( st->st_mtime > vol->v_mtime ) {
1486 vol->v_mtime = st->st_mtime;
1488 aint = AD_DATE_FROM_UNIX(vol->v_mtime);
1489 memcpy(data, &aint, sizeof( aint ));
1490 data += sizeof( aint );
1493 case VOLPBIT_BDATE :
1494 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1495 aint = AD_DATE_START;
1496 memcpy(data, &aint, sizeof( aint ));
1497 data += sizeof( aint );
1501 memcpy(data, &vol->v_vid, sizeof( vol->v_vid ));
1502 data += sizeof( vol->v_vid );
1505 case VOLPBIT_BFREE :
1506 bfree = htonl( bfree );
1507 memcpy(data, &bfree, sizeof( bfree ));
1508 data += sizeof( bfree );
1511 case VOLPBIT_BTOTAL :
1512 btotal = htonl( btotal );
1513 memcpy(data, &btotal, sizeof( btotal ));
1514 data += sizeof( btotal );
1517 #ifndef NO_LARGE_VOL_SUPPORT
1518 case VOLPBIT_XBFREE :
1519 xbfree = hton64( xbfree );
1520 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1521 bcopy(&xbfree, data, sizeof(xbfree));
1522 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1523 memcpy(data, &xbfree, sizeof( xbfree ));
1524 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1525 data += sizeof( xbfree );
1528 case VOLPBIT_XBTOTAL :
1529 xbtotal = hton64( xbtotal );
1530 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1531 bcopy(&xbtotal, data, sizeof(xbtotal));
1532 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1533 memcpy(data, &xbtotal, sizeof( xbtotal ));
1534 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1535 data += sizeof( xbfree );
1537 #endif /* ! NO_LARGE_VOL_SUPPORT */
1541 data += sizeof( u_int16_t );
1544 case VOLPBIT_BSIZE: /* block size */
1545 bsize = htonl(bsize);
1546 memcpy(data, &bsize, sizeof(bsize));
1547 data += sizeof(bsize);
1552 ad_close( &ad, ADFLAGS_HF );
1554 return( AFPERR_BITMAP );
1560 ashort = htons( data - buf );
1561 memcpy(nameoff, &ashort, sizeof( ashort ));
1562 /* name is always in mac charset */
1563 aint = ucs2_to_charset( vol->v_maccharset, vol->v_macname, data+1, AFPVOL_MACNAMELEN + 1);
1573 ad_close_metadata( &ad);
1575 *buflen = data - buf;
1579 /* ------------------------- */
1580 static int stat_vol(u_int16_t bitmap, struct vol *vol, char *rbuf, int *rbuflen)
1585 if ( stat( vol->v_path, &st ) < 0 ) {
1587 return( AFPERR_PARAM );
1589 /* save the volume device number */
1590 vol->v_dev = st.st_dev;
1592 buflen = *rbuflen - sizeof( bitmap );
1593 if (( ret = getvolparams( bitmap, vol, &st,
1594 rbuf + sizeof( bitmap ), &buflen )) != AFP_OK ) {
1598 *rbuflen = buflen + sizeof( bitmap );
1599 bitmap = htons( bitmap );
1600 memcpy(rbuf, &bitmap, sizeof( bitmap ));
1605 /* ------------------------------- */
1606 void load_volumes(AFPObj *obj)
1608 struct passwd *pwent;
1613 /* check files date */
1614 if (obj->options.defaultvol.loaded) {
1615 changed = volfile_changed(&obj->options.defaultvol);
1617 if (obj->options.systemvol.loaded) {
1618 changed |= volfile_changed(&obj->options.systemvol);
1620 if (obj->options.uservol.loaded) {
1621 changed |= volfile_changed(&obj->options.uservol);
1630 pwent = getpwnam(obj->username);
1631 if ( (obj->options.flags & OPTION_USERVOLFIRST) == 0 ) {
1632 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent);
1635 if ((*obj->username == '\0') || (obj->options.flags & OPTION_NOUSERVOL)) {
1636 readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent);
1639 * Read user's AppleVolumes or .AppleVolumes file
1640 * If neither are readable, read the default volumes file. if
1641 * that doesn't work, create a user share.
1643 if (obj->options.uservol.name) {
1644 free(obj->options.uservol.name);
1646 obj->options.uservol.name = strdup(pwent->pw_dir);
1647 if ( readvolfile(obj, &obj->options.uservol, "AppleVolumes", 1, pwent) < 0 &&
1648 readvolfile(obj, &obj->options.uservol, ".AppleVolumes", 1, pwent) < 0 &&
1649 readvolfile(obj, &obj->options.uservol, "applevolumes", 1, pwent) < 0 &&
1650 readvolfile(obj, &obj->options.uservol, ".applevolumes", 1, pwent) < 0 &&
1651 obj->options.defaultvol.name != NULL ) {
1652 if (readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent) < 0)
1653 creatvol(obj, pwent, pwent->pw_dir, NULL, NULL, 1);
1656 if ( obj->options.flags & OPTION_USERVOLFIRST ) {
1657 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent );
1660 if ( obj->options.closevol ) {
1663 for ( vol = Volumes; vol; vol = vol->v_next ) {
1664 if (vol->v_deleted && !vol->v_new ) {
1673 /* ------------------------------- */
1674 int afp_getsrvrparms(AFPObj *obj, char *ibuf _U_, int ibuflen _U_, char *rbuf, int *rbuflen)
1687 for ( vcnt = 0, volume = Volumes; volume; volume = volume->v_next ) {
1688 if (!(volume->v_flags & AFPVOL_NOSTAT)) {
1691 if ( stat( volume->v_path, &st ) < 0 ) {
1692 LOG(log_info, logtype_afpd, "afp_getsrvrparms(%s): stat: %s",
1693 volume->v_path, strerror(errno) );
1694 continue; /* can't access directory */
1696 if (!S_ISDIR(st.st_mode)) {
1697 continue; /* not a dir */
1699 accessmode(volume->v_path, &ma, NULL, &st);
1700 if ((ma.ma_user & (AR_UREAD | AR_USEARCH)) != (AR_UREAD | AR_USEARCH)) {
1701 continue; /* no r-x access */
1704 if (volume->v_hide) {
1705 continue; /* config file changed but the volume was mounted */
1708 if (utf8_encoding()) {
1709 len = ucs2_to_charset_allocate(CH_UTF8_MAC, &namebuf, volume->v_u8mname);
1711 len = ucs2_to_charset_allocate(obj->options.maccharset, &namebuf, volume->v_macname);
1714 if (len == (size_t)-1)
1717 /* set password bit if there's a volume password */
1718 *data = (volume->v_password) ? AFPSRVR_PASSWD : 0;
1720 /* Apple 2 clients running ProDOS-8 expect one volume to have
1721 bit 0 of this byte set. They will not recognize anything
1722 on the server unless this is the case. I have not
1723 completely worked this out, but it's related to booting
1724 from the server. Support for that function is a ways
1725 off.. <shirsch@ibm.net> */
1726 *data |= (volume->v_flags & AFPVOL_A2VOL) ? AFPSRVR_CONFIGINFO : 0;
1727 *data++ |= 0; /* UNIX PRIVS BIT ..., OSX doesn't seem to use it, so we don't either */
1729 memcpy(data, namebuf, len );
1735 *rbuflen = data - rbuf;
1737 if ( gettimeofday( &tv, NULL ) < 0 ) {
1738 LOG(log_error, logtype_afpd, "afp_getsrvrparms(%s): gettimeofday: %s", volume->v_path, strerror(errno) );
1740 return AFPERR_PARAM;
1742 tv.tv_sec = AD_DATE_FROM_UNIX(tv.tv_sec);
1743 memcpy(data, &tv.tv_sec, sizeof( u_int32_t));
1744 data += sizeof( u_int32_t);
1749 /* -------------------------
1750 * we are the user here
1752 int afp_openvol(AFPObj *obj, char *ibuf, int ibuflen _U_, char *rbuf, int *rbuflen)
1762 char path[ MAXPATHLEN + 1];
1768 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1769 bitmap = ntohs( bitmap );
1770 ibuf += sizeof( bitmap );
1771 if (( bitmap & (1<<VOLPBIT_VID)) == 0 ) {
1773 return AFPERR_BITMAP;
1776 len = (unsigned char)*ibuf++;
1777 volname = obj->oldtmp;
1779 if ((volname_tmp = strchr(volname,'+')) != NULL)
1780 volname = volname_tmp+1;
1782 if (utf8_encoding()) {
1783 namelen = convert_string(CH_UTF8_MAC, CH_UCS2, ibuf, len, volname, sizeof(obj->oldtmp));
1785 namelen = convert_string(obj->options.maccharset, CH_UCS2, ibuf, len, volname, sizeof(obj->oldtmp));
1790 return AFPERR_PARAM;
1794 if ((len + 1) & 1) /* pad to an even boundary */
1799 for ( volume = Volumes; volume; volume = volume->v_next ) {
1800 if ( strcasecmp_w( (ucs2_t*) volname, volume->v_name ) == 0 ) {
1805 if ( volume == NULL ) {
1807 return AFPERR_PARAM;
1810 /* check for a volume password */
1811 if (volume->v_password && strncmp(ibuf, volume->v_password, VOLPASSLEN)) {
1813 return AFPERR_ACCESS;
1816 if (( volume->v_flags & AFPVOL_OPEN ) ) {
1817 /* the volume is already open */
1819 set_uidgid ( volume );
1821 return stat_vol(bitmap, volume, rbuf, rbuflen);
1824 /* initialize volume variables
1827 if (utf8_encoding()) {
1828 volume->max_filename = 255;
1831 volume->max_filename = MACFILELEN;
1834 volume->v_dir = volume->v_root = NULL;
1835 volume->v_hash = NULL;
1837 volume->v_flags |= AFPVOL_OPEN;
1838 volume->v_cdb = NULL;
1840 if (volume->v_root_preexec) {
1841 if ((ret = afprun(1, volume->v_root_preexec, NULL)) && volume->v_root_preexec_close) {
1842 LOG(log_error, logtype_afpd, "afp_openvol(%s): root preexec : %d", volume->v_path, ret );
1849 set_uidgid ( volume );
1852 if (volume->v_preexec) {
1853 if ((ret = afprun(0, volume->v_preexec, NULL)) && volume->v_preexec_close) {
1854 LOG(log_error, logtype_afpd, "afp_openvol(%s): preexec : %d", volume->v_path, ret );
1860 if ( stat( volume->v_path, &st ) < 0 ) {
1865 if ( chdir( volume->v_path ) < 0 ) {
1870 if (utf8_encoding()) {
1871 len = convert_string_allocate(CH_UCS2, CH_UTF8_MAC, volume->v_u8mname, namelen, &vol_mname);
1873 len = convert_string_allocate(CH_UCS2, obj->options.maccharset, volume->v_macname, namelen, &vol_mname);
1875 if ( !vol_mname || len <= 0) {
1880 if ( NULL == getcwd(path, MAXPATHLEN)) {
1881 /* shouldn't be fatal but it will fail later */
1882 LOG(log_error, logtype_afpd, "afp_openvol(%s): volume pathlen too long", volume->v_path);
1887 if ((vol_uname = strrchr(path, '/')) == NULL)
1889 else if (*(vol_uname + 1) != '\0')
1892 if ((dir = dirnew(vol_mname, vol_uname) ) == NULL) {
1894 LOG(log_error, logtype_afpd, "afp_openvol(%s): malloc: %s", volume->v_path, strerror(errno) );
1900 dir->d_did = DIRDID_ROOT;
1901 dir->d_color = DIRTREE_COLOR_BLACK; /* root node is black */
1902 dir->d_m_name_ucs2 = strdup_w(volume->v_name);
1903 volume->v_dir = volume->v_root = dir;
1904 volume->v_hash = dirhash();
1906 curdir = volume->v_dir;
1907 if (volume->v_cnidscheme == NULL) {
1908 volume->v_cnidscheme = strdup(DEFAULT_CNID_SCHEME);
1909 LOG(log_info, logtype_afpd, "Volume %s use CNID scheme %s.", volume->v_path, volume->v_cnidscheme);
1911 if (volume->v_dbpath)
1912 volume->v_cdb = cnid_open (volume->v_dbpath, volume->v_umask, volume->v_cnidscheme, (volume->v_flags & AFPVOL_NODEV));
1914 volume->v_cdb = cnid_open (volume->v_path, volume->v_umask, volume->v_cnidscheme, (volume->v_flags & AFPVOL_NODEV));
1915 if (volume->v_cdb == NULL) {
1916 LOG(log_error, logtype_afpd, "Fatal error: cannot open CNID or invalid CNID backend for %s: %s",
1917 volume->v_path, volume->v_cnidscheme);
1924 if (!volume->v_volcodepage)
1925 volume->v_volcodepage = strdup("UTF8");
1927 if ( (charset_t) -1 == ( volume->v_volcharset = add_charset(volume->v_volcodepage)) ) {
1928 LOG (log_error, logtype_afpd, "Setting codepage %s as volume codepage failed", volume->v_volcodepage);
1933 if ( NULL == ( volume->v_vol = find_charset_functions(volume->v_volcodepage)) || volume->v_vol->flags & CHARSET_ICONV ) {
1934 LOG (log_warning, logtype_afpd, "WARNING: volume encoding %s is *not* supported by netatalk, expect problems !!!!", volume->v_volcodepage);
1937 if (!volume->v_maccodepage)
1938 volume->v_maccodepage = strdup(obj->options.maccodepage);
1940 if ( (charset_t) -1 == ( volume->v_maccharset = add_charset(volume->v_maccodepage)) ) {
1941 LOG (log_error, logtype_afpd, "Setting codepage %s as mac codepage failed", volume->v_maccodepage);
1946 if ( NULL == ( volume->v_mac = find_charset_functions(volume->v_maccodepage)) || ! (volume->v_mac->flags & CHARSET_CLIENT) ) {
1947 LOG (log_error, logtype_afpd, "Fatal error: mac charset %s not supported", volume->v_maccodepage);
1952 ret = stat_vol(bitmap, volume, rbuf, rbuflen);
1953 if (ret == AFP_OK) {
1955 if (!(volume->v_flags & AFPVOL_RO)) {
1956 handle_special_folders( volume );
1957 savevoloptions( volume);
1961 * If you mount a volume twice, the second time the trash appears on
1962 * the desk-top. That's because the Mac remembers the DID for the
1963 * trash (even for volumes in different zones, on different servers).
1964 * Just so this works better, we prime the DID cache with the trash,
1965 * fixing the trash at DID 17.
1966 * FIXME (RL): should it be done inside a CNID backend ? (always returning Trash DID when asked) ?
1968 if ((volume->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1970 /* FIXME find db time stamp */
1971 if (cnid_getstamp(volume->v_cdb, volume->v_stamp, sizeof(volume->v_stamp)) < 0) {
1972 LOG (log_error, logtype_afpd,
1973 "afp_openvol(%s): Fatal error: Unable to get stamp value from CNID backend",
1981 cname( volume, volume->v_dir, &p );
1987 if (volume->v_dir) {
1988 hash_free( volume->v_hash);
1989 dirfree( volume->v_dir );
1990 volume->v_dir = volume->v_root = NULL;
1993 volume->v_flags &= ~AFPVOL_OPEN;
1994 if (volume->v_cdb != NULL) {
1995 cnid_close(volume->v_cdb);
1996 volume->v_cdb = NULL;
2002 /* ------------------------- */
2003 static void closevol(struct vol *vol)
2008 hash_free( vol->v_hash);
2009 dirfree( vol->v_root );
2011 if (vol->v_cdb != NULL) {
2012 cnid_close(vol->v_cdb);
2016 if (vol->v_postexec) {
2017 afprun(0, vol->v_postexec, NULL);
2019 if (vol->v_root_postexec) {
2020 afprun(1, vol->v_root_postexec, NULL);
2024 /* ------------------------- */
2025 void close_all_vol(void)
2029 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
2030 if ( (ovol->v_flags & AFPVOL_OPEN) ) {
2031 ovol->v_flags &= ~AFPVOL_OPEN;
2037 /* ------------------------- */
2038 static void deletevol(struct vol *vol)
2042 vol->v_flags &= ~AFPVOL_OPEN;
2043 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
2044 if ( (ovol->v_flags & AFPVOL_OPEN) ) {
2048 if ( ovol != NULL ) {
2049 /* Even if chdir fails, we can't say afp_closevol fails. */
2050 if ( chdir( ovol->v_path ) == 0 ) {
2051 curdir = ovol->v_dir;
2056 if (vol->v_deleted) {
2057 showvol(vol->v_name);
2064 /* ------------------------- */
2065 int afp_closevol(AFPObj *obj _U_, char *ibuf, int ibuflen _U_, char *rbuf _U_, int *rbuflen)
2072 memcpy(&vid, ibuf, sizeof( vid ));
2073 if (NULL == ( vol = getvolbyvid( vid )) ) {
2074 return( AFPERR_PARAM );
2082 /* ------------------------- */
2083 struct vol *getvolbyvid(const u_int16_t vid )
2087 for ( vol = Volumes; vol; vol = vol->v_next ) {
2088 if ( vid == vol->v_vid ) {
2092 if ( vol == NULL || ( vol->v_flags & AFPVOL_OPEN ) == 0 ) {
2098 #endif /* FORCE_UIDGID */
2103 /* ------------------------ */
2104 static int ext_cmp_key(const void *key, const void *obj)
2106 const char *p = key;
2107 const struct extmap *em = obj;
2108 return strdiacasecmp(p, em->em_ext);
2110 struct extmap *getextmap(const char *path)
2115 if (NULL == ( p = strrchr( path, '.' )) ) {
2116 return( Defextmap );
2119 if (!*p || !Extmap_cnt) {
2120 return( Defextmap );
2122 em = bsearch(p, Extmap, Extmap_cnt, sizeof(struct extmap), ext_cmp_key);
2126 return( Defextmap );
2130 /* ------------------------- */
2131 struct extmap *getdefextmap(void)
2133 return( Defextmap );
2136 /* --------------------------
2137 poll if a volume is changed by other processes.
2139 int pollvoltime(AFPObj *obj)
2145 if (!(afp_version > 21 && obj->options.server_notif))
2148 if ( gettimeofday( &tv, NULL ) < 0 )
2151 for ( vol = Volumes; vol; vol = vol->v_next ) {
2152 if ( (vol->v_flags & AFPVOL_OPEN) && vol->v_mtime + 30 < tv.tv_sec) {
2153 if ( !stat( vol->v_path, &st ) && vol->v_mtime != st.st_mtime ) {
2154 vol->v_mtime = st.st_mtime;
2155 if (!obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED))
2164 /* ------------------------- */
2165 void setvoltime(AFPObj *obj, struct vol *vol)
2169 /* just looking at vol->v_mtime is broken seriously since updates
2170 * from other users afpd processes never are seen.
2171 * This is not the most elegant solution (a shared memory between
2172 * the afpd processes would come closer)
2175 if ( gettimeofday( &tv, NULL ) < 0 ) {
2176 LOG(log_error, logtype_afpd, "setvoltime(%s): gettimeofday: %s", vol->v_path, strerror(errno) );
2179 if( utime( vol->v_path, NULL ) < 0 ) {
2180 /* write of time failed ... probably a read only filesys,
2181 * where no other users can interfere, so there's no issue here
2185 /* a little granularity */
2186 if (vol->v_mtime < tv.tv_sec) {
2187 vol->v_mtime = tv.tv_sec;
2188 /* or finder doesn't update free space */
2189 if (afp_version > 21 && obj->options.server_notif) {
2190 obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED);
2195 /* ------------------------- */
2196 int afp_getvolparams(AFPObj *obj _U_, char *ibuf, int ibuflen _U_,char *rbuf, int *rbuflen)
2199 u_int16_t vid, bitmap;
2202 memcpy(&vid, ibuf, sizeof( vid ));
2203 ibuf += sizeof( vid );
2204 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2205 bitmap = ntohs( bitmap );
2207 if (NULL == ( vol = getvolbyvid( vid )) ) {
2209 return( AFPERR_PARAM );
2212 return stat_vol(bitmap, vol, rbuf, rbuflen);
2215 /* ------------------------- */
2216 int afp_setvolparams(AFPObj *obj _U_, char *ibuf, int ibuflen _U_, char *rbuf _U_, int *rbuflen)
2220 u_int16_t vid, bitmap;
2226 memcpy(&vid, ibuf, sizeof( vid ));
2227 ibuf += sizeof( vid );
2228 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2229 bitmap = ntohs( bitmap );
2230 ibuf += sizeof(bitmap);
2232 if (( vol = getvolbyvid( vid )) == NULL ) {
2233 return( AFPERR_PARAM );
2236 if ((vol->v_flags & AFPVOL_RO))
2237 return AFPERR_VLOCK;
2239 /* we can only set the backup date. */
2240 if (bitmap != (1 << VOLPBIT_BDATE))
2241 return AFPERR_BITMAP;
2243 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2244 if ( ad_open( vol->v_path, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR,
2247 return AFPERR_VLOCK;
2249 return AFPERR_ACCESS;
2252 memcpy(&aint, ibuf, sizeof(aint));
2253 ad_setdate(&ad, AD_DATE_BACKUP, aint);
2255 ad_close(&ad, ADFLAGS_HF);
2259 /* ------------------------- */
2260 int wincheck(const struct vol *vol, const char *path)
2264 if (!(vol->v_flags & AFPVOL_MSWINDOWS))
2267 /* empty paths are not allowed */
2268 if ((len = strlen(path)) == 0)
2271 /* leading or trailing whitespaces are not allowed, carriage returns
2272 * and probably other whitespace is okay, tabs are not allowed
2274 if ((path[0] == ' ') || (path[len-1] == ' '))
2277 /* certain characters are not allowed */
2278 if (strpbrk(path, MSWINDOWS_BADCHARS))
2281 /* everything else is okay */
2287 * precreate a folder
2288 * this is only intended for folders in the volume root
2289 * It will *not* work if the folder name contains extended characters
2291 static int create_special_folder (const struct vol *vol, const struct _special_folder *folder)
2300 p = (char *) malloc ( strlen(vol->v_path)+strlen(folder->name)+2);
2302 LOG(log_error, logtype_afpd,"malloc failed");
2306 q=strdup(folder->name);
2308 LOG(log_error, logtype_afpd,"malloc failed");
2312 strcpy(p, vol->v_path);
2317 if ((vol->v_casefold & AFPVOL_MTOUUPPER))
2319 else if ((vol->v_casefold & AFPVOL_MTOULOWER))
2325 if ( (ret = stat( p, &st )) < 0 ) {
2326 if (folder->precreate) {
2327 if (ad_mkdir(p, folder->mode)) {
2328 LOG(log_debug, logtype_afpd,"Creating '%s' failed in %s: %s", p, vol->v_path, strerror(errno));
2337 if ( !ret && folder->hide) {
2339 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2340 if (ad_open( p, vol_noadouble(vol) | ADFLAGS_HF|ADFLAGS_DIR,
2341 O_RDWR|O_CREAT, 0666, &ad) < 0) {
2346 if ((ad_get_HF_flags( &ad ) & O_CREAT) ) {
2347 if (ad_getentryoff(&ad, ADEID_NAME)) {
2348 ad_setentrylen( &ad, ADEID_NAME, strlen(folder->name));
2349 memcpy(ad_entry( &ad, ADEID_NAME ), folder->name,
2350 ad_getentrylen( &ad, ADEID_NAME ));
2354 ad_getattr(&ad, &attr);
2355 attr |= htons( ntohs( attr ) | ATTRBIT_INVISIBLE );
2356 ad_setattr(&ad, attr);
2358 /* do the same with the finder info */
2359 if (ad_entry(&ad, ADEID_FINDERI)) {
2360 memcpy(&attr, ad_entry(&ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF, sizeof(attr));
2361 attr |= htons(FINDERINFO_INVISIBLE);
2362 memcpy(ad_entry(&ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF,&attr, sizeof(attr));
2366 ad_close( &ad, ADFLAGS_HF );
2373 static void handle_special_folders (const struct vol * vol)
2375 const _special_folder *p = &special_folders[0];
2377 if ((vol->v_flags & AFPVOL_RO))
2380 for (; p->name != NULL; p++) {
2381 create_special_folder (vol, p);
2386 * Save the volume options to a file, used by
2388 * Writing the file everytime a volume is opened is
2389 * unnecessary, but it shouldn't hurt much.
2391 static int savevoloptions (const struct vol *vol)
2394 char item[MAXPATHLEN];
2398 const _vol_opt_name *op = &vol_opt_names[0];
2399 const _vol_opt_name *cf = &vol_opt_casefold[0];
2401 strlcpy (item, vol->v_path, sizeof(item));
2402 strlcat (item, "/.AppleDesktop/", sizeof(item));
2403 strlcat (item, VOLINFOFILE, sizeof(item));
2405 if ((fd = open( item, O_RDWR | O_CREAT , 0666)) <0 ) {
2406 LOG(log_debug, logtype_afpd,"Error opening %s: %s", item, strerror(errno));
2410 /* try to get a lock */
2412 lock.l_whence = SEEK_SET;
2414 lock.l_type = F_WRLCK;
2416 if (fcntl(fd, F_SETLK, &lock) < 0) {
2417 if (errno == EACCES || errno == EAGAIN) {
2418 /* ignore, other process already writing the file */
2421 LOG(log_error, logtype_cnid, "savevoloptions: cannot get lock: %s", strerror(errno));
2426 /* write volume options */
2427 snprintf(buf, sizeof(buf), "MAC_CHARSET:%s\n", vol->v_maccodepage);
2428 snprintf(item, sizeof(item), "VOL_CHARSET:%s\n", vol->v_volcodepage);
2429 strlcat(buf, item, sizeof(buf));
2431 switch (vol->v_adouble) {
2433 strlcat(buf, "ADOUBLE_VER:v1\n", sizeof(buf));
2436 strlcat(buf, "ADOUBLE_VER:v2\n", sizeof(buf));
2438 case AD_VERSION2_OSX:
2439 strlcat(buf, "ADOUBLE_VER:osx\n", sizeof(buf));
2441 case AD_VERSION1_SFM:
2442 strlcat(buf, "ADOUBLE_VER:sfm\n", sizeof(buf));
2446 strlcat(buf, "CNIDBACKEND:", sizeof(buf));
2447 strlcat(buf, vol->v_cnidscheme, sizeof(buf));
2448 strlcat(buf, "\n", sizeof(buf));
2450 strlcat(buf, "CNIDDBDHOST:", sizeof(buf));
2451 strlcat(buf, Cnid_srv, sizeof(buf));
2452 strlcat(buf, "\n", sizeof(buf));
2454 snprintf(item, sizeof(item), "CNIDDBDPORT:%u\n", Cnid_port);
2455 strlcat(buf, item, sizeof(buf));
2457 strcpy(item, "CNID_DBPATH:");
2459 strlcat(item, vol->v_dbpath, sizeof(item));
2461 strlcat(item, vol->v_path, sizeof(item));
2462 strlcat(item, "\n", sizeof(item));
2463 strlcat(buf, item, sizeof(buf));
2466 strcpy(item, "VOLUME_OPTS:");
2467 for (;op->name; op++) {
2468 if ( (vol->v_flags & op->option) ) {
2469 strlcat(item, op->name, sizeof(item));
2470 strlcat(item, " ", sizeof(item));
2473 strlcat(item, "\n", sizeof(item));
2474 strlcat(buf, item, sizeof(buf));
2476 /* casefold flags */
2477 strcpy(item, "VOLCASEFOLD:");
2478 for (;cf->name; cf++) {
2479 if ( (vol->v_casefold & cf->option) ) {
2480 strlcat(item, cf->name, sizeof(item));
2481 strlcat(item, " ", sizeof(item));
2484 strlcat(item, "\n", sizeof(item));
2485 strlcat(buf, item, sizeof(buf));
2487 if (strlen(buf) >= sizeof(buf)-1)
2488 LOG(log_debug, logtype_afpd,"Error writing .volinfo file: buffer too small, %s", buf);
2491 if (write( fd, buf, strlen(buf)) < 0 || ftruncate(fd, strlen(buf)) < 0 ) {
2492 LOG(log_debug, logtype_afpd,"Error writing .volinfo file: %s", strerror(errno));
2495 lock.l_type = F_UNLCK;
2496 fcntl(fd, F_SETLK, &lock);