2 * $Id: volume.c,v 1.83 2009-03-20 11:32:11 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/logger.h>
48 #include <atalk/cnid.h>
52 #include "directory.h"
59 extern int afprun(int root, char *cmd, int *outfd);
62 #define MIN(a, b) ((a) < (b) ? (a) : (b))
65 #ifndef NO_LARGE_VOL_SUPPORT
66 #if BYTE_ORDER == BIG_ENDIAN
69 #else /* BYTE_ORDER == BIG_ENDIAN */
70 #define hton64(x) ((u_int64_t) (htonl(((x) >> 32) & 0xffffffffLL)) | \
71 (u_int64_t) ((htonl(x) & 0xffffffffLL) << 32))
72 #define ntoh64(x) (hton64(x))
73 #endif /* BYTE_ORDER == BIG_ENDIAN */
74 #endif /* ! NO_LARGE_VOL_SUPPORT */
76 static struct vol *Volumes = NULL;
77 static u_int16_t lastvid = 0;
78 static char *Trash = "\02\024Network Trash Folder";
80 static struct extmap *Extmap = NULL, *Defextmap = NULL;
81 static int Extmap_cnt;
82 static void free_extmap(void);
84 #define VOLOPT_ALLOW 0 /* user allow list */
85 #define VOLOPT_DENY 1 /* user deny list */
86 #define VOLOPT_RWLIST 2 /* user rw list */
87 #define VOLOPT_ROLIST 3 /* user ro list */
88 #define VOLOPT_PASSWORD 4 /* volume password */
89 #define VOLOPT_CASEFOLD 5 /* character case mangling */
90 #define VOLOPT_FLAGS 6 /* various flags */
91 #define VOLOPT_DBPATH 7 /* path to database */
92 #define VOLOPT_MAPCHARS 8 /* does mtou and utom mappings. syntax:
93 m and u can be double-byte hex
98 !u -> make u illegal always
99 ~u -> make u illegal only as the first
100 part of a double-byte character.
102 #define VOLOPT_VETO 10 /* list of veto filespec */
103 #define VOLOPT_PREEXEC 11 /* preexec command */
104 #define VOLOPT_ROOTPREEXEC 12 /* root preexec command */
106 #define VOLOPT_POSTEXEC 13 /* postexec command */
107 #define VOLOPT_ROOTPOSTEXEC 14 /* root postexec command */
109 #define VOLOPT_ENCODING 15 /* mac encoding (pre OSX)*/
110 #define VOLOPT_MACCHARSET 16
111 #define VOLOPT_CNIDSCHEME 17
112 #define VOLOPT_ADOUBLE 18 /* adouble version */
117 #define VOLOPT_FORCEUID 19 /* force uid for username x */
118 #define VOLOPT_FORCEGID 20 /* force gid for group x */
119 #endif /* FORCE_UIDGID */
121 #define VOLOPT_UMASK 21
122 #define VOLOPT_ALLOWED_HOSTS 22
123 #define VOLOPT_DENIED_HOSTS 23
124 #define VOLOPT_DPERM 24 /* dperm default directories perms */
125 #define VOLOPT_FPERM 25 /* fperm default files perms */
126 #define VOLOPT_DFLTPERM 26 /* perm */
128 #define VOLOPT_MAX (VOLOPT_DFLTPERM +1)
130 #define VOLOPT_NUM (VOLOPT_MAX + 1)
133 #define VOLOPT_DEFAULT ":DEFAULT:"
134 #define VOLOPT_DEFAULT_LEN 9
140 typedef struct _special_folder {
147 static const _special_folder special_folders[] = {
148 {"Network Trash Folder", 1, 0777, 1},
149 {"Temporary Items", 1, 0777, 1},
150 {".AppleDesktop", 1, 0777, 0},
152 {"TheFindByContentFolder", 0, 0, 1},
153 {"TheVolumeSettingsFolder", 0, 0, 1},
157 typedef struct _volopt_name {
158 const u_int32_t option;
162 static const _vol_opt_name vol_opt_names[] = {
163 {AFPVOL_A2VOL, "PRODOS"}, /* prodos volume */
164 {AFPVOL_CRLF, "CRLF"}, /* cr/lf translation */
165 {AFPVOL_NOADOUBLE, "NOADOUBLE"}, /* don't create .AppleDouble by default */
166 {AFPVOL_RO, "READONLY"}, /* read-only volume */
167 {AFPVOL_MSWINDOWS, "MSWINDOWS"}, /* deal with ms-windows yuckiness. this is going away. */
168 {AFPVOL_NOHEX, "NOHEX"}, /* don't do :hex translation */
169 {AFPVOL_USEDOTS, "USEDOTS"}, /* use real dots */
170 {AFPVOL_LIMITSIZE, "LIMITSIZE"}, /* limit size for older macs */
171 {AFPVOL_MAPASCII, "MAPASCII"}, /* map the ascii range as well */
172 {AFPVOL_DROPBOX, "DROPBOX"}, /* dropkludge dropbox support */
173 {AFPVOL_NOFILEID, "NOFILEID"}, /* don't advertise createid resolveid and deleteid calls */
174 {AFPVOL_NOSTAT, "NOSTAT"}, /* advertise the volume even if we can't stat() it
175 * maybe because it will be mounted later in preexec */
176 {AFPVOL_UNIX_PRIV, "UNIXPRIV"}, /* support unix privileges */
177 {AFPVOL_NODEV, "NODEV"}, /* always use 0 for device number in cnid calls */
178 {AFPVOL_EILSEQ, "ILLEGALSEQ"}, /* encode illegal sequence */
179 {AFPVOL_CACHE, "CACHEID"}, /* Use adouble v2 CNID caching, default don't use it */
180 {AFPVOL_EXT_ATTRS, "EXT_ATTRS"}, /* Vol supports Extened Attributes */
181 {AFPVOL_ACLS, "ACLS"}, /* Vol supports ACLs */
185 static const _vol_opt_name vol_opt_casefold[] = {
186 {AFPVOL_MTOUUPPER, "MTOULOWER"},
187 {AFPVOL_MTOULOWER, "MTOULOWER"},
188 {AFPVOL_UTOMUPPER, "UTOMUPPER"},
189 {AFPVOL_UTOMLOWER, "UTOMLOWER"},
193 static void handle_special_folders (const struct vol *);
194 static int savevoloptions (const struct vol *);
195 static void deletevol(struct vol *vol);
196 static void volume_free(struct vol *vol);
198 static void volfree(struct vol_option *options,
199 const struct vol_option *save)
204 for (i = 0; i < VOLOPT_MAX; i++) {
205 if (options[i].c_value && (options[i].c_value != save[i].c_value))
206 free(options[i].c_value);
209 for (i = 0; i < VOLOPT_MAX; i++) {
210 if (options[i].c_value)
211 free(options[i].c_value);
217 /* handle variable substitutions. here's what we understand:
218 * $b -> basename of path
219 * $c -> client ip/appletalk address
220 * $d -> volume pathname on server
221 * $f -> full name (whatever's in the gecos field)
224 * $i -> client ip/appletalk address without port
225 * $s -> server name (hostname if it doesn't exist)
226 * $u -> username (guest is usually nobody)
227 * $v -> volume name or basename if null
228 * $z -> zone (may not exist)
233 #define is_var(a, b) (strncmp((a), (b), 2) == 0)
235 static char *volxlate(AFPObj *obj, char *dest, size_t destlen,
236 char *src, struct passwd *pwd, char *path, char *volname)
246 dest = calloc(destlen +1, 1);
252 strlcpy(dest, src, destlen +1);
253 if ((p = strchr(src, '$')) == NULL) /* nothing to do */
256 /* first part of the path. just forward to the next variable. */
257 len = MIN((size_t)(p - src), destlen);
263 while (p && destlen > 0) {
264 /* now figure out what the variable is */
266 if (is_var(p, "$b")) {
268 if ((q = strrchr(path, '/')) == NULL)
270 else if (*(q + 1) != '\0')
273 } else if (is_var(p, "$c")) {
274 if (obj->proto == AFPPROTO_ASP) {
275 ASP asp = obj->handle;
277 len = sprintf(dest, "%u.%u", ntohs(asp->asp_sat.sat_addr.s_net),
278 asp->asp_sat.sat_addr.s_node);
282 } else if (obj->proto == AFPPROTO_DSI) {
283 DSI *dsi = obj->handle;
285 len = sprintf(dest, "%s:%u", inet_ntoa(dsi->client.sin_addr),
286 ntohs(dsi->client.sin_port));
290 } else if (is_var(p, "$d")) {
292 } else if (is_var(p, "$f")) {
293 if ((q = strchr(pwd->pw_gecos, ',')))
296 } else if (is_var(p, "$g")) {
297 struct group *grp = getgrgid(pwd->pw_gid);
300 } else if (is_var(p, "$h")) {
301 q = obj->options.hostname;
302 } else if (is_var(p, "$i")) {
303 if (obj->proto == AFPPROTO_ASP) {
304 ASP asp = obj->handle;
306 len = sprintf(dest, "%u", ntohs(asp->asp_sat.sat_addr.s_net));
310 } else if (obj->proto == AFPPROTO_DSI) {
311 DSI *dsi = obj->handle;
313 q = inet_ntoa(dsi->client.sin_addr);
315 } else if (is_var(p, "$s")) {
318 else if (obj->options.server) {
319 q = obj->options.server;
321 q = obj->options.hostname;
322 } else if (is_var(p, "$u")) {
324 if ( obj->options.ntseparator && (sep = strchr(obj->username, obj->options.ntseparator[0])) != NULL)
328 } else if (is_var(p, "$v")) {
333 if ((q = strrchr(path, '/')) == NULL)
335 else if (*(q + 1) != '\0')
338 } else if (is_var(p, "$z")) {
340 } else if (is_var(p, "$$")) {
345 /* copy the stuff over. if we don't understand something that we
346 * should, just skip it over. */
348 len = MIN(p == q ? 2 : strlen(q), destlen);
349 strncpy(dest, q, len);
354 /* stuff up to next $ */
356 p = strchr(src, '$');
357 len = p ? MIN((size_t)(p - src), destlen) : destlen;
359 strncpy(dest, src, len);
367 /* to make sure that val is valid, make sure to select an opt that
369 static int optionok(const char *buf, const char *opt, const char *val)
371 if (!strstr(buf,opt))
379 /* -------------------- */
380 static void setoption(struct vol_option *options, struct vol_option *save, int opt, const char *val)
382 if (options[opt].c_value && (!save || options[opt].c_value != save[opt].c_value))
383 free(options[opt].c_value);
384 options[opt].c_value = strdup(val + 1);
387 /* ------------------------------------------
388 handle all the options. tmp can't be NULL. */
389 static void volset(struct vol_option *options, struct vol_option *save,
390 char *volname, int vlen,
395 val = strchr(tmp, ':');
397 /* we'll assume it's a volume name. */
398 strncpy(volname, tmp, vlen);
403 LOG(log_debug, logtype_afpd, "Parsing volset %s", val);
405 if (optionok(tmp, "allow:", val)) {
406 setoption(options, save, VOLOPT_ALLOW, val);
408 } else if (optionok(tmp, "deny:", val)) {
409 setoption(options, save, VOLOPT_DENY, val);
411 } else if (optionok(tmp, "rwlist:", val)) {
412 setoption(options, save, VOLOPT_RWLIST, val);
414 } else if (optionok(tmp, "rolist:", val)) {
415 setoption(options, save, VOLOPT_ROLIST, val);
417 } else if (optionok(tmp, "codepage:", val)) {
418 LOG (log_error, logtype_afpd, "The old codepage system has been removed. Please make sure to read the documentation !!!!");
419 /* Make sure we don't screw anything */
421 } else if (optionok(tmp, "volcharset:", val)) {
422 setoption(options, save, VOLOPT_ENCODING, val);
423 } else if (optionok(tmp, "maccharset:", val)) {
424 setoption(options, save, VOLOPT_MACCHARSET, val);
425 } else if (optionok(tmp, "veto:", val)) {
426 setoption(options, save, VOLOPT_VETO, val);
427 } else if (optionok(tmp, "cnidscheme:", val)) {
428 setoption(options, save, VOLOPT_CNIDSCHEME, val);
429 } else if (optionok(tmp, "casefold:", val)) {
430 if (strcasecmp(val + 1, "tolower") == 0)
431 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMLOWER;
432 else if (strcasecmp(val + 1, "toupper") == 0)
433 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMUPPER;
434 else if (strcasecmp(val + 1, "xlatelower") == 0)
435 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UUPPERMLOWER;
436 else if (strcasecmp(val + 1, "xlateupper") == 0)
437 options[VOLOPT_CASEFOLD].i_value = AFPVOL_ULOWERMUPPER;
438 } else if (optionok(tmp, "adouble:", val)) {
439 if (strcasecmp(val + 1, "v1") == 0)
440 options[VOLOPT_ADOUBLE].i_value = AD_VERSION1;
441 #if AD_VERSION == AD_VERSION2
442 else if (strcasecmp(val + 1, "v2") == 0)
443 options[VOLOPT_ADOUBLE].i_value = AD_VERSION2;
444 else if (strcasecmp(val + 1, "osx") == 0)
445 options[VOLOPT_ADOUBLE].i_value = AD_VERSION2_OSX;
446 else if (strcasecmp(val + 1, "ads") == 0)
447 options[VOLOPT_ADOUBLE].i_value = AD_VERSION1_ADS;
448 else if (strcasecmp(val + 1, "sfm") == 0)
449 options[VOLOPT_ADOUBLE].i_value = AD_VERSION1_SFM;
451 } else if (optionok(tmp, "options:", val)) {
454 if ((p = strtok(val + 1, ",")) == NULL) /* nothing */
458 if (strcasecmp(p, "prodos") == 0)
459 options[VOLOPT_FLAGS].i_value |= AFPVOL_A2VOL;
460 else if (strcasecmp(p, "mswindows") == 0) {
461 options[VOLOPT_FLAGS].i_value |= AFPVOL_MSWINDOWS | AFPVOL_USEDOTS;
462 } else if (strcasecmp(p, "crlf") == 0)
463 options[VOLOPT_FLAGS].i_value |= AFPVOL_CRLF;
464 else if (strcasecmp(p, "noadouble") == 0)
465 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOADOUBLE;
466 else if (strcasecmp(p, "ro") == 0)
467 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
468 else if (strcasecmp(p, "nohex") == 0)
469 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOHEX;
470 else if (strcasecmp(p, "usedots") == 0)
471 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS;
472 else if (strcasecmp(p, "invisibledots") == 0)
473 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS | AFPVOL_INV_DOTS;
474 else if (strcasecmp(p, "limitsize") == 0)
475 options[VOLOPT_FLAGS].i_value |= AFPVOL_LIMITSIZE;
476 /* support for either "dropbox" or "dropkludge" */
477 else if (strcasecmp(p, "dropbox") == 0)
478 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
479 else if (strcasecmp(p, "dropkludge") == 0)
480 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
481 else if (strcasecmp(p, "nofileid") == 0)
482 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOFILEID;
483 else if (strcasecmp(p, "nostat") == 0)
484 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOSTAT;
485 else if (strcasecmp(p, "preexec_close") == 0)
486 options[VOLOPT_PREEXEC].i_value = 1;
487 else if (strcasecmp(p, "root_preexec_close") == 0)
488 options[VOLOPT_ROOTPREEXEC].i_value = 1;
489 else if (strcasecmp(p, "upriv") == 0)
490 options[VOLOPT_FLAGS].i_value |= AFPVOL_UNIX_PRIV;
491 else if (strcasecmp(p, "extattrs") == 0)
492 options[VOLOPT_FLAGS].i_value |= AFPVOL_EXT_ATTRS;
493 else if (strcasecmp(p, "acls") == 0)
494 options[VOLOPT_FLAGS].i_value |= AFPVOL_ACLS;
495 else if (strcasecmp(p, "nodev") == 0)
496 options[VOLOPT_FLAGS].i_value |= AFPVOL_NODEV;
497 else if (strcasecmp(p, "illegalseq") == 0)
498 options[VOLOPT_FLAGS].i_value |= AFPVOL_EILSEQ;
499 else if (strcasecmp(p, "cachecnid") == 0)
500 options[VOLOPT_FLAGS].i_value |= AFPVOL_CACHE;
502 p = strtok(NULL, ",");
505 } else if (optionok(tmp, "dbpath:", val)) {
506 setoption(options, save, VOLOPT_DBPATH, val);
508 } else if (optionok(tmp, "umask:", val)) {
509 options[VOLOPT_UMASK].i_value = (int)strtol(val +1, NULL, 8);
510 } else if (optionok(tmp, "dperm:", val)) {
511 options[VOLOPT_DPERM].i_value = (int)strtol(val+1, NULL, 8);
512 } else if (optionok(tmp, "fperm:", val)) {
513 options[VOLOPT_FPERM].i_value = (int)strtol(val+1, NULL, 8);
514 } else if (optionok(tmp, "perm:", val)) {
515 options[VOLOPT_DFLTPERM].i_value = (int)strtol(val+1, NULL, 8);
516 } else if (optionok(tmp, "mapchars:",val)) {
517 setoption(options, save, VOLOPT_MAPCHARS, val);
519 } else if (optionok(tmp, "password:", val)) {
520 setoption(options, save, VOLOPT_PASSWORD, val);
524 /* this code allows forced uid/gid per volume settings */
525 } else if (optionok(tmp, "forceuid:", val)) {
526 setoption(options, save, VOLOPT_FORCEUID, val);
527 } else if (optionok(tmp, "forcegid:", val)) {
528 setoption(options, save, VOLOPT_FORCEGID, val);
530 #endif /* FORCE_UIDGID */
531 } else if (optionok(tmp, "root_preexec:", val)) {
532 setoption(options, save, VOLOPT_ROOTPREEXEC, val);
534 } else if (optionok(tmp, "preexec:", val)) {
535 setoption(options, save, VOLOPT_PREEXEC, val);
537 } else if (optionok(tmp, "root_postexec:", val)) {
538 setoption(options, save, VOLOPT_ROOTPOSTEXEC, val);
540 } else if (optionok(tmp, "postexec:", val)) {
541 setoption(options, save, VOLOPT_POSTEXEC, val);
543 } else if (optionok(tmp, "allowed_hosts:", val)) {
544 setoption(options, save, VOLOPT_ALLOWED_HOSTS, val);
546 } else if (optionok(tmp, "denied_hosts:", val)) {
547 setoption(options, save, VOLOPT_DENIED_HOSTS, val);
550 /* ignore unknown options */
551 LOG(log_debug, logtype_afpd, "ignoring unknown volume option: %s", tmp);
556 /* ----------------- */
557 static void showvol(const ucs2_t *name)
560 for ( volume = Volumes; volume; volume = volume->v_next ) {
561 if (volume->v_hide && !strcasecmp_w( (utf8_encoding()?volume->v_u8mname:volume->v_macname), name ) ) {
568 /* ------------------------------- */
569 static int creatvol(AFPObj *obj, struct passwd *pwd,
570 char *path, char *name,
571 struct vol_option *options,
572 const int user /* user defined volume */
576 int suffixlen, vlen, tmpvlen, u8mvlen, macvlen;
578 char tmpname[AFPVOL_U8MNAMELEN+1];
579 ucs2_t u8mtmpname[(AFPVOL_U8MNAMELEN+1)*2], mactmpname[(AFPVOL_MACNAMELEN+1)*2];
580 char suffix[6]; /* max is #FFFF */
583 if ( name == NULL || *name == '\0' ) {
584 if ((name = strrchr( path, '/' )) == NULL) {
585 return -1; /* Obviously not a fully qualified path */
588 /* if you wish to share /, you need to specify a name. */
593 /* suffix for mangling use (lastvid + 1) */
594 /* because v_vid has not been decided yet. */
595 suffixlen = sprintf(suffix, "%c%X", MANGLE_CHAR, lastvid + 1 );
597 vlen = strlen( name );
599 /* Unicode Volume Name */
600 /* Firstly convert name from unixcharset to UTF8-MAC */
602 tmpvlen = convert_charset(obj->options.unixcharset, CH_UTF8_MAC, 0, name, vlen, tmpname, AFPVOL_U8MNAMELEN, &flags);
604 strcpy(tmpname, "???");
608 /* Do we have to mangle ? */
609 if ( (flags & CONV_REQMANGLE) || (tmpvlen > obj->options.volnamelen)) {
610 if (tmpvlen + suffixlen > obj->options.volnamelen) {
612 tmpvlen = convert_charset(obj->options.unixcharset, CH_UTF8_MAC, 0, name, vlen, tmpname, obj->options.volnamelen - suffixlen, &flags);
613 tmpname[tmpvlen != (size_t)-1 ? tmpvlen : 0] = 0;
615 strcat(tmpname, suffix);
616 tmpvlen = strlen(tmpname);
619 /* Secondly convert name from UTF8-MAC to UCS2 */
620 if ( 0 >= ( u8mvlen = convert_string(CH_UTF8_MAC, CH_UCS2, tmpname, tmpvlen, u8mtmpname, AFPVOL_U8MNAMELEN*2)) )
623 LOG(log_debug, logtype_afpd, "createvol: Volume '%s' -> UTF8-MAC Name: '%s'", name, tmpname);
625 /* Maccharset Volume Name */
626 /* Firsty convert name from unixcharset to maccharset */
628 tmpvlen = convert_charset(obj->options.unixcharset, obj->options.maccharset, 0, name, vlen, tmpname, AFPVOL_U8MNAMELEN, &flags);
630 strcpy(tmpname, "???");
634 /* Do we have to mangle ? */
635 if ( (flags & CONV_REQMANGLE) || (tmpvlen > AFPVOL_MACNAMELEN)) {
636 if (tmpvlen + suffixlen > AFPVOL_MACNAMELEN) {
638 tmpvlen = convert_charset(obj->options.unixcharset, obj->options.maccharset, 0, name, vlen, tmpname, AFPVOL_MACNAMELEN - suffixlen, &flags);
639 tmpname[tmpvlen != (size_t)-1 ? tmpvlen : 0] = 0;
641 strcat(tmpname, suffix);
642 tmpvlen = strlen(tmpname);
645 /* Secondly convert name from maccharset to UCS2 */
646 if ( 0 >= ( macvlen = convert_string(obj->options.maccharset, CH_UCS2, tmpname, tmpvlen, mactmpname, AFPVOL_U8MNAMELEN*2)) )
649 LOG(log_debug, logtype_afpd, "createvol: Volume '%s' -> Longname: '%s'", name, tmpname);
651 /* check duplicate */
652 for ( volume = Volumes; volume; volume = volume->v_next ) {
653 if (( strcasecmp_w( volume->v_u8mname, u8mtmpname ) == 0 ) || ( strcasecmp_w( volume->v_macname, mactmpname ) == 0 )){
654 if (volume->v_deleted) {
655 volume->v_new = hide = 1;
658 return -1; /* Won't be able to access it, anyway... */
663 if (!( volume = (struct vol *)calloc(1, sizeof( struct vol ))) ) {
664 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
667 if ( NULL == ( volume->v_localname = strdup(name))) {
668 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
673 if ( NULL == ( volume->v_u8mname = strdup_w(u8mtmpname))) {
674 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
679 if ( NULL == ( volume->v_macname = strdup_w(mactmpname))) {
680 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
685 if (!( volume->v_path = (char *)malloc( strlen( path ) + 1 )) ) {
686 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
691 volume->v_hide = hide;
692 strcpy( volume->v_path, path );
696 #endif /* __svr4__ */
697 /* os X start at 1 and use network order ie. 1 2 3 */
698 volume->v_vid = ++lastvid;
699 volume->v_vid = htons(volume->v_vid);
703 /* should we casefold? */
704 volume->v_casefold = options[VOLOPT_CASEFOLD].i_value;
706 /* shift in some flags */
707 volume->v_flags = options[VOLOPT_FLAGS].i_value;
709 volume->v_ad_options = 0;
710 if ((volume->v_flags & AFPVOL_NODEV))
711 volume->v_ad_options |= ADVOL_NODEV;
712 if ((volume->v_flags & AFPVOL_CACHE))
713 volume->v_ad_options |= ADVOL_CACHE;
714 if ((volume->v_flags & AFPVOL_UNIX_PRIV))
715 volume->v_ad_options |= ADVOL_UNIXPRIV;
716 if ((volume->v_flags & AFPVOL_INV_DOTS))
717 volume->v_ad_options |= ADVOL_INVDOTS;
719 if (options[VOLOPT_PASSWORD].c_value)
720 volume->v_password = strdup(options[VOLOPT_PASSWORD].c_value);
722 if (options[VOLOPT_VETO].c_value)
723 volume->v_veto = strdup(options[VOLOPT_VETO].c_value);
725 if (options[VOLOPT_ENCODING].c_value)
726 volume->v_volcodepage = strdup(options[VOLOPT_ENCODING].c_value);
728 if (options[VOLOPT_MACCHARSET].c_value)
729 volume->v_maccodepage = strdup(options[VOLOPT_MACCHARSET].c_value);
731 if (options[VOLOPT_DBPATH].c_value)
732 volume->v_dbpath = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_DBPATH].c_value, pwd, path, name);
734 if (options[VOLOPT_CNIDSCHEME].c_value)
735 volume->v_cnidscheme = strdup(options[VOLOPT_CNIDSCHEME].c_value);
737 if (options[VOLOPT_UMASK].i_value)
738 volume->v_umask = (mode_t)options[VOLOPT_UMASK].i_value;
740 if (options[VOLOPT_DPERM].i_value)
741 volume->v_dperm = (mode_t)options[VOLOPT_DPERM].i_value;
743 if (options[VOLOPT_FPERM].i_value)
744 volume->v_fperm = (mode_t)options[VOLOPT_FPERM].i_value;
746 if (options[VOLOPT_DFLTPERM].i_value)
747 volume->v_perm = (mode_t)options[VOLOPT_DFLTPERM].i_value;
749 if (options[VOLOPT_ADOUBLE].i_value)
750 volume->v_adouble = options[VOLOPT_ADOUBLE].i_value;
752 volume->v_adouble = AD_VERSION;
756 if (options[VOLOPT_FORCEUID].c_value) {
757 volume->v_forceuid = strdup(options[VOLOPT_FORCEUID].c_value);
759 volume->v_forceuid = NULL; /* set as null so as to return 0 later on */
762 if (options[VOLOPT_FORCEGID].c_value) {
763 volume->v_forcegid = strdup(options[VOLOPT_FORCEGID].c_value);
765 volume->v_forcegid = NULL; /* set as null so as to return 0 later on */
769 if (options[VOLOPT_PREEXEC].c_value)
770 volume->v_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_PREEXEC].c_value, pwd, path, name);
771 volume->v_preexec_close = options[VOLOPT_PREEXEC].i_value;
773 if (options[VOLOPT_POSTEXEC].c_value)
774 volume->v_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_POSTEXEC].c_value, pwd, path, name);
776 if (options[VOLOPT_ROOTPREEXEC].c_value)
777 volume->v_root_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPREEXEC].c_value, pwd, path, name);
778 volume->v_root_preexec_close = options[VOLOPT_ROOTPREEXEC].i_value;
780 if (options[VOLOPT_ROOTPOSTEXEC].c_value)
781 volume->v_root_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPOSTEXEC].c_value, pwd, path, name);
784 volume->v_dperm |= volume->v_perm;
785 volume->v_fperm |= volume->v_perm;
787 volume->v_next = Volumes;
792 /* ---------------- */
793 static char *myfgets( buf, size, fp )
802 while ((EOF != ( c = getc( fp )) ) && ( size > 1 )) {
803 if ( c == '\n' || c == '\r' ) {
804 if (p != buf && *(p -1) == '\\') {
826 /* check access list. this function wants something of the following
828 * @group,name,name2,@group2,name3
830 * a NULL argument allows everybody to have access.
831 * we return three things:
833 * 0: list exists, but name isn't in it
837 #ifndef NO_REAL_USER_NAME
838 /* authentication is case insensitive
839 * FIXME should we do the same with group name?
841 #define access_strcmp strcasecmp
844 #define access_strcmp strcmp
848 static int accessvol(args, name)
852 char buf[MAXPATHLEN + 1], *p;
858 strlcpy(buf, args, sizeof(buf));
859 if ((p = strtok(buf, ",")) == NULL) /* nothing, return okay */
863 if (*p == '@') { /* it's a group */
864 if ((gr = getgrnam(p + 1)) && gmem(gr->gr_gid))
866 } else if (access_strcmp(p, name) == 0) /* it's a user name */
868 p = strtok(NULL, ",");
874 static int hostaccessvol(type, volname, args, obj)
880 char buf[MAXPATHLEN + 1], *p, *b;
881 DSI *dsi = obj->handle;
886 strlcpy(buf, args, sizeof(buf));
887 if ((p = strtok_r(buf, ",", &b)) == NULL) /* nothing, return okay */
891 if (obj->proto == AFPPROTO_DSI) {
892 struct in_addr mask, net;
893 char *net_char, *mask_char;
896 net_char = strtok(p, "/");
897 mask_char = strtok(NULL,"/");
898 if (mask_char == NULL) {
901 mask_int = atoi(mask_char);
904 // convert the integer netmask to a bitmask in network order
905 mask.s_addr = htonl(-1 - ((1 << (32 - mask_int)) - 1));
906 net.s_addr = inet_addr(net_char) & mask.s_addr;
908 if ((dsi->client.sin_addr.s_addr & mask.s_addr) == net.s_addr) {
909 if (type == VOLOPT_DENIED_HOSTS)
910 LOG(log_info, logtype_afpd, "AFP access denied for client IP '%s' to volume '%s' by denied list",
911 inet_ntoa(dsi->client.sin_addr), volname);
915 p = strtok_r(NULL, ",", &b);
917 if (type == VOLOPT_ALLOWED_HOSTS)
918 LOG(log_info, logtype_afpd, "AFP access denied for client IP '%s' to volume '%s', not in allowed list",
919 inet_ntoa(dsi->client.sin_addr), volname);
923 static void setextmap( ext, type, creator, user)
924 char *ext, *type, *creator;
930 if (Extmap == NULL) {
931 if (( Extmap = calloc(1, sizeof( struct extmap ))) == NULL ) {
932 LOG(log_error, logtype_afpd, "setextmap: calloc: %s", strerror(errno) );
937 for ( em = Extmap, cnt = 0; em->em_ext; em++, cnt++) {
938 if ( (strdiacasecmp( em->em_ext, ext )) == 0 ) {
943 if ( em->em_ext == NULL ) {
944 if (!(Extmap = realloc( Extmap, sizeof( struct extmap ) * (cnt +2))) ) {
945 LOG(log_error, logtype_afpd, "setextmap: realloc: %s", strerror(errno) );
948 (Extmap +cnt +1)->em_ext = NULL;
950 } else if ( !user ) {
956 if (!(em->em_ext = strdup( ext))) {
957 LOG(log_error, logtype_afpd, "setextmap: strdup: %s", strerror(errno) );
961 if ( *type == '\0' ) {
962 memcpy(em->em_type, "????", sizeof( em->em_type ));
964 memcpy(em->em_type, type, sizeof( em->em_type ));
966 if ( *creator == '\0' ) {
967 memcpy(em->em_creator, "UNIX", sizeof( em->em_creator ));
969 memcpy(em->em_creator, creator, sizeof( em->em_creator ));
973 /* -------------------------- */
974 static int extmap_cmp(const void *map1, const void *map2)
976 const struct extmap *em1 = map1;
977 const struct extmap *em2 = map2;
978 return strdiacasecmp(em1->em_ext, em2->em_ext);
981 static void sortextmap( void)
986 if ((em = Extmap) == NULL) {
994 qsort(Extmap, Extmap_cnt, sizeof(struct extmap), extmap_cmp);
995 if (*Extmap->em_ext == 0) {
996 /* the first line is really "." the default entry,
997 * we remove the leading '.' in setextmap
1004 /* ----------------------
1006 static void free_extmap( void)
1011 for ( em = Extmap; em->em_ext; em++) {
1021 /* ----------------------
1023 static int volfile_changed(struct afp_volume_name *p)
1029 name = p->full_name;
1033 if (!stat( name, &st) && st.st_mtime > p->mtime) {
1034 p->mtime = st.st_mtime;
1040 /* ----------------------
1041 * Read a volume configuration file and add the volumes contained within to
1042 * the global volume list. If p2 is non-NULL, the file that is opened is
1045 * Lines that begin with # and blank lines are ignored.
1046 * Volume lines are of the form:
1047 * <unix path> [<volume name>] [allow:<user>,<@group>,...] \
1048 * [codepage:<file>] [casefold:<num>]
1049 * <extension> TYPE [CREATOR]
1051 static int readvolfile(obj, p1, p2, user, pwent)
1053 struct afp_volume_name *p1;
1056 struct passwd *pwent;
1059 char path[ MAXPATHLEN + 1], tmp[ MAXPATHLEN + 1],
1060 volname[ AFPVOL_U8MNAMELEN + 1 ], buf[ BUFSIZ ],
1061 type[ 5 ], creator[ 5 ];
1064 struct vol_option options[VOLOPT_NUM], save_options[VOLOPT_NUM];
1072 strcpy( path, p1->name );
1074 strcat( path, "/" );
1076 if (p1->full_name) {
1077 free(p1->full_name);
1079 p1->full_name = strdup(path);
1082 if (NULL == ( fp = fopen( path, "r" )) ) {
1086 if (fd != -1 && !fstat( fd, &st) ) {
1087 p1->mtime = st.st_mtime;
1090 memset(save_options, 0, sizeof(save_options));
1091 while ( myfgets( buf, sizeof( buf ), fp ) != NULL ) {
1092 initline( strlen( buf ), buf );
1093 parseline( sizeof( path ) - 1, path );
1100 /* change the default options for this file */
1101 if (strncmp(path, VOLOPT_DEFAULT, VOLOPT_DEFAULT_LEN) == 0) {
1103 for (i = 0; i < VOLOPT_NUM; i++) {
1104 if (parseline( sizeof( path ) - VOLOPT_DEFAULT_LEN - 1,
1105 path + VOLOPT_DEFAULT_LEN) < 0)
1107 volset(save_options, NULL, tmp, sizeof(tmp) - 1,
1108 path + VOLOPT_DEFAULT_LEN);
1114 if (( p = strchr( path, '/' )) != NULL ) {
1122 if ( u == NULL || *u == '\0' || ( pw = getpwnam( u )) == NULL ) {
1125 strcpy( tmp, pw->pw_dir );
1126 if ( p != NULL && *p != '\0' ) {
1130 /* Tag a user's home directory with their umask. Note, this will
1131 * be overwritten if the user actually specifies a umask: option
1132 * for a '~' volume. */
1133 save_options[VOLOPT_UMASK].i_value = obj->options.save_mask;
1137 /* send path through variable substitution */
1138 if (*path != '~') /* need to copy path to tmp */
1141 pwent = getpwnam(obj->username);
1142 volxlate(obj, path, sizeof(path) - 1, tmp, pwent, NULL, NULL);
1144 /* this is sort of braindead. basically, i want to be
1145 * able to specify things in any order, but i don't want to
1146 * re-write everything.
1148 * currently we have options:
1156 * options:prodos,crlf,noadouble,ro...
1161 * namemask:x,y,!z (not implemented yet)
1163 memcpy(options, save_options, sizeof(options));
1166 /* read in up to VOLOP_NUM possible options */
1167 for (i = 0; i < VOLOPT_NUM; i++) {
1168 if (parseline( sizeof( tmp ) - 1, tmp ) < 0)
1171 volset(options, save_options, volname, sizeof(volname) - 1, tmp);
1174 /* check allow/deny lists:
1175 allow -> either no list (-1), or in list (1)
1176 deny -> either no list (-1), or not in list (0) */
1177 if (accessvol(options[VOLOPT_ALLOW].c_value, obj->username) &&
1178 (accessvol(options[VOLOPT_DENY].c_value, obj->username) < 1) &&
1179 hostaccessvol(VOLOPT_ALLOWED_HOSTS, volname, options[VOLOPT_ALLOWED_HOSTS].c_value, obj) &&
1180 (hostaccessvol(VOLOPT_DENIED_HOSTS, volname, options[VOLOPT_DENIED_HOSTS].c_value, obj) < 1)) {
1182 /* handle read-only behaviour. semantics:
1183 * 1) neither the rolist nor the rwlist exist -> rw
1184 * 2) rolist exists -> ro if user is in it.
1185 * 3) rwlist exists -> ro unless user is in it. */
1186 if (((options[VOLOPT_FLAGS].i_value & AFPVOL_RO) == 0) &&
1187 ((accessvol(options[VOLOPT_ROLIST].c_value,
1188 obj->username) == 1) ||
1189 !accessvol(options[VOLOPT_RWLIST].c_value,
1191 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
1193 /* do variable substitution for volname */
1194 volxlate(obj, tmp, sizeof(tmp) - 1, volname, pwent, path, NULL);
1195 creatvol(obj, pwent, path, tmp, options, p2 != NULL);
1197 volfree(options, save_options);
1201 parseline( sizeof( type ) - 1, type );
1202 parseline( sizeof( creator ) - 1, creator );
1203 setextmap( path, type, creator, user);
1210 volfree(save_options, NULL);
1212 if ( fclose( fp ) != 0 ) {
1213 LOG(log_error, logtype_afpd, "readvolfile: fclose: %s", strerror(errno) );
1219 /* ------------------------------- */
1220 static void volume_free(struct vol *vol)
1222 free(vol->v_localname);
1223 vol->v_localname = NULL;
1224 free(vol->v_u8mname);
1225 vol->v_u8mname = NULL;
1226 free(vol->v_macname);
1227 vol->v_macname = NULL;
1229 free(vol->v_password);
1231 free(vol->v_volcodepage);
1232 free(vol->v_maccodepage);
1233 free(vol->v_cnidscheme);
1234 free(vol->v_dbpath);
1237 free(vol->v_forceuid);
1238 free(vol->v_forcegid);
1239 #endif /* FORCE_UIDGID */
1242 /* ------------------------------- */
1243 static void free_volumes(void )
1246 struct vol *nvol, *ovol;
1248 for ( vol = Volumes; vol; vol = vol->v_next ) {
1249 if (( vol->v_flags & AFPVOL_OPEN ) ) {
1256 for ( vol = Volumes, ovol = NULL; vol; vol = nvol) {
1259 if (vol->v_localname == NULL) {
1260 if (Volumes == vol) {
1265 ovol->v_next = nvol;
1275 /* ------------------------------- */
1276 static void volume_unlink(struct vol *volume)
1278 struct vol *vol, *ovol, *nvol;
1280 if (volume == Volumes) {
1281 Volumes = Volumes->v_next;
1284 for ( vol = Volumes->v_next, ovol = Volumes; vol; vol = nvol) {
1287 if (vol == volume) {
1288 ovol->v_next = nvol;
1297 static int getvolspace( vol, bfree, btotal, xbfree, xbtotal, bsize )
1299 u_int32_t *bfree, *btotal, *bsize;
1300 VolSpace *xbfree, *xbtotal;
1304 #ifndef NO_QUOTA_SUPPORT
1305 VolSpace qfree, qtotal;
1308 spaceflag = AFPVOL_GVSMASK & vol->v_flags;
1309 /* report up to 2GB if afp version is < 2.2 (4GB if not) */
1310 maxsize = (vol->v_flags & AFPVOL_A2VOL) ? 0x01fffe00 :
1311 (((afp_version < 22) || (vol->v_flags & AFPVOL_LIMITSIZE))
1312 ? 0x7fffffffL : 0xffffffffL);
1315 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_AFSGVS ) {
1316 if ( afs_getvolspace( vol, xbfree, xbtotal, bsize ) == AFP_OK ) {
1317 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_AFSGVS;
1318 goto getvolspace_done;
1323 if (( rc = ustatfs_getvolspace( vol, xbfree, xbtotal,
1324 bsize)) != AFP_OK ) {
1328 #define min(a,b) ((a)<(b)?(a):(b))
1329 #ifndef NO_QUOTA_SUPPORT
1330 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_UQUOTA ) {
1331 if ( uquota_getvolspace( vol, &qfree, &qtotal, *bsize ) == AFP_OK ) {
1332 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_UQUOTA;
1333 *xbfree = min(*xbfree, qfree);
1334 *xbtotal = min( *xbtotal, qtotal);
1335 goto getvolspace_done;
1339 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_USTATFS;
1342 *bfree = min( *xbfree, maxsize);
1343 *btotal = min( *xbtotal, maxsize);
1347 /* -----------------------
1348 * set volume creation date
1349 * avoid duplicate, well at least it tries
1351 static void vol_setdate(u_int16_t id, struct adouble *adp, time_t date)
1354 struct vol *vol = Volumes;
1356 for ( volume = Volumes; volume; volume = volume->v_next ) {
1357 if (volume->v_vid == id) {
1360 else if ((time_t)(AD_DATE_FROM_UNIX(date)) == volume->v_ctime) {
1362 volume = Volumes; /* restart */
1365 vol->v_ctime = AD_DATE_FROM_UNIX(date);
1366 ad_setdate(adp, AD_DATE_CREATE | AD_DATE_UNIX, date);
1369 /* ----------------------- */
1370 static int getvolparams( bitmap, vol, st, buf, buflen )
1378 int bit = 0, isad = 1;
1381 u_int32_t bfree, btotal, bsize;
1382 VolSpace xbfree, xbtotal; /* extended bytes */
1383 char *data, *nameoff = NULL;
1386 LOG(log_debug, logtype_afpd, "getvolparams: Volume '%s'", vol->v_localname);
1388 /* courtesy of jallison@whistle.com:
1389 * For MacOS8.x support we need to create the
1390 * .Parent file here if it doesn't exist. */
1392 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1393 if ( ad_open_metadata( vol->v_path, vol_noadouble(vol) | ADFLAGS_DIR, O_CREAT, &ad) < 0 ) {
1395 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1397 } else if (ad_get_MD_flags( &ad ) & O_CREAT) {
1398 slash = strrchr( vol->v_path, '/' );
1402 slash = vol->v_path;
1403 if (ad_getentryoff(&ad, ADEID_NAME)) {
1404 ad_setentrylen( &ad, ADEID_NAME, strlen( slash ));
1405 memcpy(ad_entry( &ad, ADEID_NAME ), slash,
1406 ad_getentrylen( &ad, ADEID_NAME ));
1408 vol_setdate(vol->v_vid, &ad, st->st_mtime);
1412 if (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0)
1413 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1415 vol->v_ctime = aint;
1418 if (( bitmap & ( (1<<VOLPBIT_BFREE)|(1<<VOLPBIT_BTOTAL) |
1419 (1<<VOLPBIT_XBFREE)|(1<<VOLPBIT_XBTOTAL) |
1420 (1<<VOLPBIT_BSIZE)) ) != 0 ) {
1421 if ( getvolspace( vol, &bfree, &btotal, &xbfree, &xbtotal,
1424 ad_close( &ad, ADFLAGS_HF );
1426 return( AFPERR_PARAM );
1431 while ( bitmap != 0 ) {
1432 while (( bitmap & 1 ) == 0 ) {
1440 if (0 == (vol->v_flags & AFPVOL_NOFILEID) && vol->v_cdb != NULL &&
1441 (vol->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1442 ashort = VOLPBIT_ATTR_FILEID;
1444 /* check for read-only.
1445 * NOTE: we don't actually set the read-only flag unless
1446 * it's passed in that way as it's possible to mount
1447 * a read-write filesystem under a read-only one. */
1448 if ((vol->v_flags & AFPVOL_RO) ||
1449 ((utime(vol->v_path, NULL) < 0) && (errno == EROFS))) {
1450 ashort |= VOLPBIT_ATTR_RO;
1452 ashort |= VOLPBIT_ATTR_CATSEARCH;
1453 if (afp_version >= 30) {
1454 ashort |= VOLPBIT_ATTR_UTF8;
1455 if (vol->v_flags & AFPVOL_UNIX_PRIV)
1456 ashort |= VOLPBIT_ATTR_UNIXPRIV;
1458 if (afp_version >= 32) {
1459 if (vol->v_flags & AFPVOL_EXT_ATTRS)
1460 ashort |= VOLPBIT_ATTR_EXT_ATTRS;
1461 if (vol->v_flags & AFPVOL_ACLS)
1462 ashort |= VOLPBIT_ATTR_ACLS;
1464 ashort = htons(ashort);
1465 memcpy(data, &ashort, sizeof( ashort ));
1466 data += sizeof( ashort );
1470 ashort = htons( AFPVOLSIG_DEFAULT );
1471 memcpy(data, &ashort, sizeof( ashort ));
1472 data += sizeof( ashort );
1475 case VOLPBIT_CDATE :
1476 aint = vol->v_ctime;
1477 memcpy(data, &aint, sizeof( aint ));
1478 data += sizeof( aint );
1481 case VOLPBIT_MDATE :
1482 if ( st->st_mtime > vol->v_mtime ) {
1483 vol->v_mtime = st->st_mtime;
1485 aint = AD_DATE_FROM_UNIX(vol->v_mtime);
1486 memcpy(data, &aint, sizeof( aint ));
1487 data += sizeof( aint );
1490 case VOLPBIT_BDATE :
1491 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1492 aint = AD_DATE_START;
1493 memcpy(data, &aint, sizeof( aint ));
1494 data += sizeof( aint );
1498 memcpy(data, &vol->v_vid, sizeof( vol->v_vid ));
1499 data += sizeof( vol->v_vid );
1502 case VOLPBIT_BFREE :
1503 bfree = htonl( bfree );
1504 memcpy(data, &bfree, sizeof( bfree ));
1505 data += sizeof( bfree );
1508 case VOLPBIT_BTOTAL :
1509 btotal = htonl( btotal );
1510 memcpy(data, &btotal, sizeof( btotal ));
1511 data += sizeof( btotal );
1514 #ifndef NO_LARGE_VOL_SUPPORT
1515 case VOLPBIT_XBFREE :
1516 xbfree = hton64( xbfree );
1517 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1518 bcopy(&xbfree, data, sizeof(xbfree));
1519 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1520 memcpy(data, &xbfree, sizeof( xbfree ));
1521 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1522 data += sizeof( xbfree );
1525 case VOLPBIT_XBTOTAL :
1526 xbtotal = hton64( xbtotal );
1527 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1528 bcopy(&xbtotal, data, sizeof(xbtotal));
1529 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1530 memcpy(data, &xbtotal, sizeof( xbtotal ));
1531 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1532 data += sizeof( xbfree );
1534 #endif /* ! NO_LARGE_VOL_SUPPORT */
1538 data += sizeof( u_int16_t );
1541 case VOLPBIT_BSIZE: /* block size */
1542 bsize = htonl(bsize);
1543 memcpy(data, &bsize, sizeof(bsize));
1544 data += sizeof(bsize);
1549 ad_close( &ad, ADFLAGS_HF );
1551 return( AFPERR_BITMAP );
1557 ashort = htons( data - buf );
1558 memcpy(nameoff, &ashort, sizeof( ashort ));
1559 /* name is always in mac charset */
1560 aint = ucs2_to_charset( vol->v_maccharset, vol->v_macname, data+1, AFPVOL_MACNAMELEN + 1);
1570 ad_close_metadata( &ad);
1572 *buflen = data - buf;
1576 /* ------------------------- */
1577 static int stat_vol(u_int16_t bitmap, struct vol *vol, char *rbuf, int *rbuflen)
1582 if ( stat( vol->v_path, &st ) < 0 ) {
1584 return( AFPERR_PARAM );
1586 /* save the volume device number */
1587 vol->v_dev = st.st_dev;
1589 buflen = *rbuflen - sizeof( bitmap );
1590 if (( ret = getvolparams( bitmap, vol, &st,
1591 rbuf + sizeof( bitmap ), &buflen )) != AFP_OK ) {
1595 *rbuflen = buflen + sizeof( bitmap );
1596 bitmap = htons( bitmap );
1597 memcpy(rbuf, &bitmap, sizeof( bitmap ));
1602 /* ------------------------------- */
1603 void load_volumes(AFPObj *obj)
1605 struct passwd *pwent;
1610 /* check files date */
1611 if (obj->options.defaultvol.loaded) {
1612 changed = volfile_changed(&obj->options.defaultvol);
1614 if (obj->options.systemvol.loaded) {
1615 changed |= volfile_changed(&obj->options.systemvol);
1617 if (obj->options.uservol.loaded) {
1618 changed |= volfile_changed(&obj->options.uservol);
1627 pwent = getpwnam(obj->username);
1628 if ( (obj->options.flags & OPTION_USERVOLFIRST) == 0 ) {
1629 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent);
1632 if ((*obj->username == '\0') || (obj->options.flags & OPTION_NOUSERVOL)) {
1633 readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent);
1636 * Read user's AppleVolumes or .AppleVolumes file
1637 * If neither are readable, read the default volumes file. if
1638 * that doesn't work, create a user share.
1640 if (obj->options.uservol.name) {
1641 free(obj->options.uservol.name);
1643 obj->options.uservol.name = strdup(pwent->pw_dir);
1644 if ( readvolfile(obj, &obj->options.uservol, "AppleVolumes", 1, pwent) < 0 &&
1645 readvolfile(obj, &obj->options.uservol, ".AppleVolumes", 1, pwent) < 0 &&
1646 readvolfile(obj, &obj->options.uservol, "applevolumes", 1, pwent) < 0 &&
1647 readvolfile(obj, &obj->options.uservol, ".applevolumes", 1, pwent) < 0 &&
1648 obj->options.defaultvol.name != NULL ) {
1649 if (readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent) < 0)
1650 creatvol(obj, pwent, pwent->pw_dir, NULL, NULL, 1);
1653 if ( obj->options.flags & OPTION_USERVOLFIRST ) {
1654 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent );
1657 if ( obj->options.closevol ) {
1660 for ( vol = Volumes; vol; vol = vol->v_next ) {
1661 if (vol->v_deleted && !vol->v_new ) {
1670 /* ------------------------------- */
1671 int afp_getsrvrparms(obj, ibuf, ibuflen, rbuf, rbuflen )
1673 char *ibuf _U_, *rbuf;
1674 int ibuflen _U_, *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, 0 ) < 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(obj, ibuf, ibuflen, rbuf, rbuflen )
1755 int ibuflen _U_, *rbuflen;
1765 char path[ MAXPATHLEN + 1];
1771 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1772 bitmap = ntohs( bitmap );
1773 ibuf += sizeof( bitmap );
1774 if (( bitmap & (1<<VOLPBIT_VID)) == 0 ) {
1776 return AFPERR_BITMAP;
1779 len = (unsigned char)*ibuf++;
1780 volname = obj->oldtmp;
1782 if ((volname_tmp = strchr(volname,'+')) != NULL)
1783 volname = volname_tmp+1;
1785 if (utf8_encoding()) {
1786 namelen = convert_string(CH_UTF8_MAC, CH_UCS2, ibuf, len, volname, sizeof(obj->oldtmp));
1788 namelen = convert_string(obj->options.maccharset, CH_UCS2, ibuf, len, volname, sizeof(obj->oldtmp));
1793 return AFPERR_PARAM;
1797 if ((len + 1) & 1) /* pad to an even boundary */
1802 for ( volume = Volumes; volume; volume = volume->v_next ) {
1803 if ( strcasecmp_w( (ucs2_t*) volname, (utf8_encoding()?volume->v_u8mname:volume->v_macname) ) == 0 ) {
1808 if ( volume == NULL ) {
1810 return AFPERR_PARAM;
1813 /* check for a volume password */
1814 if (volume->v_password && strncmp(ibuf, volume->v_password, VOLPASSLEN)) {
1816 return AFPERR_ACCESS;
1819 if (( volume->v_flags & AFPVOL_OPEN ) ) {
1820 /* the volume is already open */
1822 set_uidgid ( volume );
1824 return stat_vol(bitmap, volume, rbuf, rbuflen);
1827 /* initialize volume variables
1830 if (utf8_encoding()) {
1831 volume->max_filename = 255;
1834 volume->max_filename = MACFILELEN;
1837 volume->v_dir = volume->v_root = NULL;
1838 volume->v_hash = NULL;
1840 volume->v_flags |= AFPVOL_OPEN;
1841 volume->v_cdb = NULL;
1843 if (volume->v_root_preexec) {
1844 if ((ret = afprun(1, volume->v_root_preexec, NULL)) && volume->v_root_preexec_close) {
1845 LOG(log_error, logtype_afpd, "afp_openvol(%s): root preexec : %d", volume->v_path, ret );
1852 set_uidgid ( volume );
1855 if (volume->v_preexec) {
1856 if ((ret = afprun(0, volume->v_preexec, NULL)) && volume->v_preexec_close) {
1857 LOG(log_error, logtype_afpd, "afp_openvol(%s): preexec : %d", volume->v_path, ret );
1863 if ( stat( volume->v_path, &st ) < 0 ) {
1868 if ( chdir( volume->v_path ) < 0 ) {
1873 if (utf8_encoding()) {
1874 len = convert_string_allocate(CH_UCS2, CH_UTF8_MAC, volume->v_u8mname, namelen, &vol_mname);
1876 len = convert_string_allocate(CH_UCS2, obj->options.maccharset, volume->v_macname, namelen, &vol_mname);
1878 if ( !vol_mname || len <= 0) {
1883 if ( NULL == getcwd(path, MAXPATHLEN)) {
1884 /* shouldn't be fatal but it will fail later */
1885 LOG(log_error, logtype_afpd, "afp_openvol(%s): volume pathlen too long", volume->v_path);
1890 if ((vol_uname = strrchr(path, '/')) == NULL)
1892 else if (*(vol_uname + 1) != '\0')
1895 if ((dir = dirnew(vol_mname, vol_uname) ) == NULL) {
1897 LOG(log_error, logtype_afpd, "afp_openvol(%s): malloc: %s", volume->v_path, strerror(errno) );
1903 dir->d_did = DIRDID_ROOT;
1904 dir->d_color = DIRTREE_COLOR_BLACK; /* root node is black */
1905 volume->v_dir = volume->v_root = dir;
1906 volume->v_hash = dirhash();
1908 curdir = volume->v_dir;
1909 if (volume->v_cnidscheme == NULL) {
1910 volume->v_cnidscheme = strdup(DEFAULT_CNID_SCHEME);
1911 LOG(log_warning, logtype_afpd, "Warning: No CNID scheme for volume %s. Using default.",
1914 if (volume->v_dbpath)
1915 volume->v_cdb = cnid_open (volume->v_dbpath, volume->v_umask, volume->v_cnidscheme, (volume->v_flags & AFPVOL_NODEV));
1917 volume->v_cdb = cnid_open (volume->v_path, volume->v_umask, volume->v_cnidscheme, (volume->v_flags & AFPVOL_NODEV));
1918 if (volume->v_cdb == NULL) {
1919 LOG(log_error, logtype_afpd, "Fatal error: cannot open CNID or invalid CNID backend for %s: %s",
1920 volume->v_path, volume->v_cnidscheme);
1927 if (!volume->v_volcodepage)
1928 volume->v_volcodepage = strdup("UTF8");
1930 if ( (charset_t) -1 == ( volume->v_volcharset = add_charset(volume->v_volcodepage)) ) {
1931 LOG (log_error, logtype_afpd, "Setting codepage %s as volume codepage failed", volume->v_volcodepage);
1936 if ( NULL == ( volume->v_vol = find_charset_functions(volume->v_volcodepage)) || volume->v_vol->flags & CHARSET_ICONV ) {
1937 LOG (log_warning, logtype_afpd, "WARNING: volume encoding %s is *not* supported by netatalk, expect problems !!!!", volume->v_volcodepage);
1940 if (!volume->v_maccodepage)
1941 volume->v_maccodepage = strdup(obj->options.maccodepage);
1943 if ( (charset_t) -1 == ( volume->v_maccharset = add_charset(volume->v_maccodepage)) ) {
1944 LOG (log_error, logtype_afpd, "Setting codepage %s as mac codepage failed", volume->v_maccodepage);
1949 if ( NULL == ( volume->v_mac = find_charset_functions(volume->v_maccodepage)) || ! (volume->v_mac->flags & CHARSET_CLIENT) ) {
1950 LOG (log_error, logtype_afpd, "Fatal error: mac charset %s not supported", volume->v_maccodepage);
1955 ret = stat_vol(bitmap, volume, rbuf, rbuflen);
1956 if (ret == AFP_OK) {
1958 if (!(volume->v_flags & AFPVOL_RO)) {
1959 handle_special_folders( volume );
1960 savevoloptions( volume);
1964 * If you mount a volume twice, the second time the trash appears on
1965 * the desk-top. That's because the Mac remembers the DID for the
1966 * trash (even for volumes in different zones, on different servers).
1967 * Just so this works better, we prime the DID cache with the trash,
1968 * fixing the trash at DID 17.
1969 * FIXME (RL): should it be done inside a CNID backend ? (always returning Trash DID when asked) ?
1971 if ((volume->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1973 /* FIXME find db time stamp */
1974 if (cnid_getstamp(volume->v_cdb, volume->v_stamp, sizeof(volume->v_stamp)) < 0) {
1975 LOG (log_error, logtype_afpd,
1976 "afp_openvol(%s): Fatal error: Unable to get stamp value from CNID backend",
1984 cname( volume, volume->v_dir, &p );
1990 if (volume->v_dir) {
1991 hash_free( volume->v_hash);
1992 dirfree( volume->v_dir );
1993 volume->v_dir = volume->v_root = NULL;
1996 volume->v_flags &= ~AFPVOL_OPEN;
1997 if (volume->v_cdb != NULL) {
1998 cnid_close(volume->v_cdb);
1999 volume->v_cdb = NULL;
2005 /* ------------------------- */
2006 static void closevol(struct vol *vol)
2011 hash_free( vol->v_hash);
2012 dirfree( vol->v_root );
2014 if (vol->v_cdb != NULL) {
2015 cnid_close(vol->v_cdb);
2019 if (vol->v_postexec) {
2020 afprun(0, vol->v_postexec, NULL);
2022 if (vol->v_root_postexec) {
2023 afprun(1, vol->v_root_postexec, NULL);
2027 /* ------------------------- */
2028 void close_all_vol(void)
2032 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
2033 if ( (ovol->v_flags & AFPVOL_OPEN) ) {
2034 ovol->v_flags &= ~AFPVOL_OPEN;
2040 /* ------------------------- */
2041 static void deletevol(struct vol *vol)
2045 vol->v_flags &= ~AFPVOL_OPEN;
2046 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
2047 if ( (ovol->v_flags & AFPVOL_OPEN) ) {
2051 if ( ovol != NULL ) {
2052 /* Even if chdir fails, we can't say afp_closevol fails. */
2053 if ( chdir( ovol->v_path ) == 0 ) {
2054 curdir = ovol->v_dir;
2059 if (vol->v_deleted) {
2060 showvol(utf8_encoding()?vol->v_u8mname:vol->v_macname);
2067 /* ------------------------- */
2068 int afp_closevol(obj, ibuf, ibuflen, rbuf, rbuflen )
2070 char *ibuf, *rbuf _U_;
2071 int ibuflen _U_, *rbuflen;
2078 memcpy(&vid, ibuf, sizeof( vid ));
2079 if (NULL == ( vol = getvolbyvid( vid )) ) {
2080 return( AFPERR_PARAM );
2088 /* ------------------------- */
2089 struct vol *getvolbyvid(const u_int16_t vid )
2093 for ( vol = Volumes; vol; vol = vol->v_next ) {
2094 if ( vid == vol->v_vid ) {
2098 if ( vol == NULL || ( vol->v_flags & AFPVOL_OPEN ) == 0 ) {
2104 #endif /* FORCE_UIDGID */
2109 /* ------------------------ */
2110 static int ext_cmp_key(const void *key, const void *obj)
2112 const char *p = key;
2113 const struct extmap *em = obj;
2114 return strdiacasecmp(p, em->em_ext);
2116 struct extmap *getextmap(const char *path)
2121 if (NULL == ( p = strrchr( path, '.' )) ) {
2122 return( Defextmap );
2125 if (!*p || !Extmap_cnt) {
2126 return( Defextmap );
2128 em = bsearch(p, Extmap, Extmap_cnt, sizeof(struct extmap), ext_cmp_key);
2132 return( Defextmap );
2136 /* ------------------------- */
2137 struct extmap *getdefextmap(void)
2139 return( Defextmap );
2142 /* --------------------------
2143 poll if a volume is changed by other processes.
2145 int pollvoltime(obj)
2152 if (!(afp_version > 21 && obj->options.server_notif))
2155 if ( gettimeofday( &tv, 0 ) < 0 )
2158 for ( vol = Volumes; vol; vol = vol->v_next ) {
2159 if ( (vol->v_flags & AFPVOL_OPEN) && vol->v_mtime + 30 < tv.tv_sec) {
2160 if ( !stat( vol->v_path, &st ) && vol->v_mtime != st.st_mtime ) {
2161 vol->v_mtime = st.st_mtime;
2162 if (!obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED))
2171 /* ------------------------- */
2172 void setvoltime(obj, vol )
2178 /* just looking at vol->v_mtime is broken seriously since updates
2179 * from other users afpd processes never are seen.
2180 * This is not the most elegant solution (a shared memory between
2181 * the afpd processes would come closer)
2184 if ( gettimeofday( &tv, 0 ) < 0 ) {
2185 LOG(log_error, logtype_afpd, "setvoltime(%s): gettimeofday: %s", vol->v_path, strerror(errno) );
2188 if( utime( vol->v_path, NULL ) < 0 ) {
2189 /* write of time failed ... probably a read only filesys,
2190 * where no other users can interfere, so there's no issue here
2194 /* a little granularity */
2195 if (vol->v_mtime < tv.tv_sec) {
2196 vol->v_mtime = tv.tv_sec;
2197 /* or finder doesn't update free space */
2198 if (afp_version > 21 && obj->options.server_notif) {
2199 obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED);
2204 /* ------------------------- */
2205 int afp_getvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
2208 int ibuflen _U_, *rbuflen;
2211 u_int16_t vid, bitmap;
2214 memcpy(&vid, ibuf, sizeof( vid ));
2215 ibuf += sizeof( vid );
2216 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2217 bitmap = ntohs( bitmap );
2219 if (NULL == ( vol = getvolbyvid( vid )) ) {
2221 return( AFPERR_PARAM );
2224 return stat_vol(bitmap, vol, rbuf, rbuflen);
2227 /* ------------------------- */
2228 int afp_setvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
2230 char *ibuf, *rbuf _U_;
2231 int ibuflen _U_, *rbuflen;
2235 u_int16_t vid, bitmap;
2241 memcpy(&vid, ibuf, sizeof( vid ));
2242 ibuf += sizeof( vid );
2243 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2244 bitmap = ntohs( bitmap );
2245 ibuf += sizeof(bitmap);
2247 if (( vol = getvolbyvid( vid )) == NULL ) {
2248 return( AFPERR_PARAM );
2251 if ((vol->v_flags & AFPVOL_RO))
2252 return AFPERR_VLOCK;
2254 /* we can only set the backup date. */
2255 if (bitmap != (1 << VOLPBIT_BDATE))
2256 return AFPERR_BITMAP;
2258 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2259 if ( ad_open( vol->v_path, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR,
2262 return AFPERR_VLOCK;
2264 return AFPERR_ACCESS;
2267 memcpy(&aint, ibuf, sizeof(aint));
2268 ad_setdate(&ad, AD_DATE_BACKUP, aint);
2270 ad_close(&ad, ADFLAGS_HF);
2274 /* ------------------------- */
2275 int wincheck(const struct vol *vol, const char *path)
2279 if (!(vol->v_flags & AFPVOL_MSWINDOWS))
2282 /* empty paths are not allowed */
2283 if ((len = strlen(path)) == 0)
2286 /* leading or trailing whitespaces are not allowed, carriage returns
2287 * and probably other whitespace is okay, tabs are not allowed
2289 if ((path[0] == ' ') || (path[len-1] == ' '))
2292 /* certain characters are not allowed */
2293 if (strpbrk(path, MSWINDOWS_BADCHARS))
2296 /* everything else is okay */
2302 * precreate a folder
2303 * this is only intended for folders in the volume root
2304 * It will *not* work if the folder name contains extended characters
2306 static int create_special_folder (const struct vol *vol, const struct _special_folder *folder)
2315 p = (char *) malloc ( strlen(vol->v_path)+strlen(folder->name)+2);
2317 LOG(log_error, logtype_afpd,"malloc failed");
2321 q=strdup(folder->name);
2323 LOG(log_error, logtype_afpd,"malloc failed");
2327 strcpy(p, vol->v_path);
2332 if ((vol->v_casefold & AFPVOL_MTOUUPPER))
2334 else if ((vol->v_casefold & AFPVOL_MTOULOWER))
2340 if ( (ret = stat( p, &st )) < 0 ) {
2341 if (folder->precreate) {
2342 if (ad_mkdir(p, folder->mode)) {
2343 LOG(log_debug, logtype_afpd,"Creating '%s' failed in %s: %s", p, vol->v_path, strerror(errno));
2352 if ( !ret && folder->hide) {
2354 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2355 if (ad_open( p, vol_noadouble(vol) | ADFLAGS_HF|ADFLAGS_DIR,
2356 O_RDWR|O_CREAT, 0666, &ad) < 0) {
2361 if ((ad_get_HF_flags( &ad ) & O_CREAT) ) {
2362 if (ad_getentryoff(&ad, ADEID_NAME)) {
2363 ad_setentrylen( &ad, ADEID_NAME, strlen(folder->name));
2364 memcpy(ad_entry( &ad, ADEID_NAME ), folder->name,
2365 ad_getentrylen( &ad, ADEID_NAME ));
2369 ad_getattr(&ad, &attr);
2370 attr |= htons( ntohs( attr ) | ATTRBIT_INVISIBLE );
2371 ad_setattr(&ad, attr);
2373 /* do the same with the finder info */
2374 if (ad_entry(&ad, ADEID_FINDERI)) {
2375 memcpy(&attr, ad_entry(&ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF, sizeof(attr));
2376 attr |= htons(FINDERINFO_INVISIBLE);
2377 memcpy(ad_entry(&ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF,&attr, sizeof(attr));
2381 ad_close( &ad, ADFLAGS_HF );
2388 static void handle_special_folders (const struct vol * vol)
2390 const _special_folder *p = &special_folders[0];
2392 if ((vol->v_flags & AFPVOL_RO))
2395 for (; p->name != NULL; p++) {
2396 create_special_folder (vol, p);
2401 * Save the volume options to a file, used by
2403 * Writing the file everytime a volume is opened is
2404 * unnecessary, but it shouldn't hurt much.
2406 static int savevoloptions (const struct vol *vol)
2409 char item[MAXPATHLEN];
2413 const _vol_opt_name *op = &vol_opt_names[0];
2414 const _vol_opt_name *cf = &vol_opt_casefold[0];
2416 strlcpy (item, vol->v_path, sizeof(item));
2417 strlcat (item, "/.AppleDesktop/", sizeof(item));
2418 strlcat (item, VOLINFOFILE, sizeof(item));
2420 if ((fd = open( item, O_RDWR | O_CREAT , 0666)) <0 ) {
2421 LOG(log_debug, logtype_afpd,"Error opening %s: %s", item, strerror(errno));
2425 /* try to get a lock */
2427 lock.l_whence = SEEK_SET;
2429 lock.l_type = F_WRLCK;
2431 if (fcntl(fd, F_SETLK, &lock) < 0) {
2432 if (errno == EACCES || errno == EAGAIN) {
2433 /* ignore, other process already writing the file */
2436 LOG(log_error, logtype_cnid, "savevoloptions: cannot get lock: %s", strerror(errno));
2441 /* write volume options */
2442 snprintf(buf, sizeof(buf), "MAC_CHARSET:%s\n", vol->v_maccodepage);
2443 snprintf(item, sizeof(item), "VOL_CHARSET:%s\n", vol->v_volcodepage);
2444 strlcat(buf, item, sizeof(buf));
2446 switch (vol->v_adouble) {
2448 strlcat(buf, "ADOUBLE_VER:v1\n", sizeof(buf));
2451 strlcat(buf, "ADOUBLE_VER:v2\n", sizeof(buf));
2453 case AD_VERSION2_OSX:
2454 strlcat(buf, "ADOUBLE_VER:osx\n", sizeof(buf));
2456 case AD_VERSION1_ADS:
2457 strlcat(buf, "ADOUBLE_VER:ads\n", sizeof(buf));
2459 case AD_VERSION1_SFM:
2460 strlcat(buf, "ADOUBLE_VER:sfm\n", sizeof(buf));
2464 strlcat(buf, "CNIDBACKEND:", sizeof(buf));
2465 strlcat(buf, vol->v_cnidscheme, sizeof(buf));
2466 strlcat(buf, "\n", sizeof(buf));
2468 strlcat(buf, "CNIDDBDHOST:", sizeof(buf));
2469 strlcat(buf, Cnid_srv, sizeof(buf));
2470 strlcat(buf, "\n", sizeof(buf));
2472 snprintf(item, sizeof(item), "CNIDDBDPORT:%u\n", Cnid_port);
2473 strlcat(buf, item, sizeof(buf));
2475 strcpy(item, "CNID_DBPATH:");
2477 strlcat(item, vol->v_dbpath, sizeof(item));
2479 strlcat(item, vol->v_path, sizeof(item));
2480 strlcat(item, "\n", sizeof(item));
2481 strlcat(buf, item, sizeof(buf));
2484 strcpy(item, "VOLUME_OPTS:");
2485 for (;op->name; op++) {
2486 if ( (vol->v_flags & op->option) ) {
2487 strlcat(item, op->name, sizeof(item));
2488 strlcat(item, " ", sizeof(item));
2491 strlcat(item, "\n", sizeof(item));
2492 strlcat(buf, item, sizeof(buf));
2494 /* casefold flags */
2495 strcpy(item, "VOLCASEFOLD:");
2496 for (;cf->name; cf++) {
2497 if ( (vol->v_casefold & cf->option) ) {
2498 strlcat(item, cf->name, sizeof(item));
2499 strlcat(item, " ", sizeof(item));
2502 strlcat(item, "\n", sizeof(item));
2503 strlcat(buf, item, sizeof(buf));
2505 if (strlen(buf) >= sizeof(buf)-1)
2506 LOG(log_debug, logtype_afpd,"Error writing .volinfo file: buffer too small, %s", buf);
2509 if (write( fd, buf, strlen(buf)) < 0 || ftruncate(fd, strlen(buf)) < 0 ) {
2510 LOG(log_debug, logtype_afpd,"Error writing .volinfo file: %s", strerror(errno));
2513 lock.l_type = F_UNLCK;
2514 fcntl(fd, F_SETLK, &lock);