2 * $Id: volume.c,v 1.88 2009-07-20 23:23:02 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"
60 extern int afprun(int root, char *cmd, int *outfd);
63 #define MIN(a, b) ((a) < (b) ? (a) : (b))
66 #ifndef NO_LARGE_VOL_SUPPORT
67 #if BYTE_ORDER == BIG_ENDIAN
70 #else /* BYTE_ORDER == BIG_ENDIAN */
71 #define hton64(x) ((u_int64_t) (htonl(((x) >> 32) & 0xffffffffLL)) | \
72 (u_int64_t) ((htonl(x) & 0xffffffffLL) << 32))
73 #define ntoh64(x) (hton64(x))
74 #endif /* BYTE_ORDER == BIG_ENDIAN */
75 #endif /* ! NO_LARGE_VOL_SUPPORT */
77 static struct vol *Volumes = NULL;
78 static u_int16_t lastvid = 0;
79 static char *Trash = "\02\024Network Trash Folder";
81 static struct extmap *Extmap = NULL, *Defextmap = NULL;
82 static int Extmap_cnt;
83 static void free_extmap(void);
85 #define VOLOPT_ALLOW 0 /* user allow list */
86 #define VOLOPT_DENY 1 /* user deny list */
87 #define VOLOPT_RWLIST 2 /* user rw list */
88 #define VOLOPT_ROLIST 3 /* user ro list */
89 #define VOLOPT_PASSWORD 4 /* volume password */
90 #define VOLOPT_CASEFOLD 5 /* character case mangling */
91 #define VOLOPT_FLAGS 6 /* various flags */
92 #define VOLOPT_DBPATH 7 /* path to database */
93 #define VOLOPT_MAPCHARS 8 /* does mtou and utom mappings. syntax:
94 m and u can be double-byte hex
99 !u -> make u illegal always
100 ~u -> make u illegal only as the first
101 part of a double-byte character.
103 #define VOLOPT_VETO 10 /* list of veto filespec */
104 #define VOLOPT_PREEXEC 11 /* preexec command */
105 #define VOLOPT_ROOTPREEXEC 12 /* root preexec command */
107 #define VOLOPT_POSTEXEC 13 /* postexec command */
108 #define VOLOPT_ROOTPOSTEXEC 14 /* root postexec command */
110 #define VOLOPT_ENCODING 15 /* mac encoding (pre OSX)*/
111 #define VOLOPT_MACCHARSET 16
112 #define VOLOPT_CNIDSCHEME 17
113 #define VOLOPT_ADOUBLE 18 /* adouble version */
118 #define VOLOPT_FORCEUID 19 /* force uid for username x */
119 #define VOLOPT_FORCEGID 20 /* force gid for group x */
120 #endif /* FORCE_UIDGID */
122 #define VOLOPT_UMASK 21
123 #define VOLOPT_ALLOWED_HOSTS 22
124 #define VOLOPT_DENIED_HOSTS 23
125 #define VOLOPT_DPERM 24 /* dperm default directories perms */
126 #define VOLOPT_FPERM 25 /* fperm default files perms */
127 #define VOLOPT_DFLTPERM 26 /* perm */
129 #define VOLOPT_MAX (VOLOPT_DFLTPERM +1)
131 #define VOLOPT_NUM (VOLOPT_MAX + 1)
134 #define VOLOPT_DEFAULT ":DEFAULT:"
135 #define VOLOPT_DEFAULT_LEN 9
141 typedef struct _special_folder {
148 static const _special_folder special_folders[] = {
149 {"Network Trash Folder", 1, 0777, 1},
150 {"Temporary Items", 1, 0777, 1},
151 {".AppleDesktop", 1, 0777, 0},
153 {"TheFindByContentFolder", 0, 0, 1},
154 {"TheVolumeSettingsFolder", 0, 0, 1},
158 typedef struct _volopt_name {
159 const u_int32_t option;
164 /* Keep in sync with libatalk/util/volinfo.c ! */
165 static const _vol_opt_name vol_opt_names[] = {
166 {AFPVOL_A2VOL, "PRODOS"}, /* prodos volume */
167 {AFPVOL_CRLF, "CRLF"}, /* cr/lf translation */
168 {AFPVOL_NOADOUBLE, "NOADOUBLE"}, /* don't create .AppleDouble by default */
169 {AFPVOL_RO, "READONLY"}, /* read-only volume */
170 {AFPVOL_MSWINDOWS, "MSWINDOWS"}, /* deal with ms-windows yuckiness. this is going away. */
171 {AFPVOL_NOHEX, "NOHEX"}, /* don't do :hex translation */
172 {AFPVOL_USEDOTS, "USEDOTS"}, /* use real dots */
173 {AFPVOL_LIMITSIZE, "LIMITSIZE"}, /* limit size for older macs */
174 {AFPVOL_MAPASCII, "MAPASCII"}, /* map the ascii range as well */
175 {AFPVOL_DROPBOX, "DROPBOX"}, /* dropkludge dropbox support */
176 {AFPVOL_NOFILEID, "NOFILEID"}, /* don't advertise createid resolveid and deleteid calls */
177 {AFPVOL_NOSTAT, "NOSTAT"}, /* advertise the volume even if we can't stat() it
178 * maybe because it will be mounted later in preexec */
179 {AFPVOL_UNIX_PRIV, "UNIXPRIV"}, /* support unix privileges */
180 {AFPVOL_NODEV, "NODEV"}, /* always use 0 for device number in cnid calls */
181 {AFPVOL_CASEINSEN, "CASEINSENSITIVE"}, /* volume is case insensitive */
182 {AFPVOL_EILSEQ, "ILLEGALSEQ"}, /* encode illegal sequence */
183 {AFPVOL_CACHE, "CACHEID"}, /* Use adouble v2 CNID caching, default don't use it */
184 {AFPVOL_EXT_ATTRS, "EXT_ATTRS"}, /* Vol supports Extened Attributes */
185 {AFPVOL_ACLS, "ACLS"}, /* Vol supports ACLs */
189 static const _vol_opt_name vol_opt_casefold[] = {
190 {AFPVOL_MTOUUPPER, "MTOULOWER"},
191 {AFPVOL_MTOULOWER, "MTOULOWER"},
192 {AFPVOL_UTOMUPPER, "UTOMUPPER"},
193 {AFPVOL_UTOMLOWER, "UTOMLOWER"},
197 static void handle_special_folders (const struct vol *);
198 static int savevoloptions (const struct vol *);
199 static void deletevol(struct vol *vol);
200 static void volume_free(struct vol *vol);
202 static void volfree(struct vol_option *options,
203 const struct vol_option *save)
208 for (i = 0; i < VOLOPT_MAX; i++) {
209 if (options[i].c_value && (options[i].c_value != save[i].c_value))
210 free(options[i].c_value);
213 for (i = 0; i < VOLOPT_MAX; i++) {
214 if (options[i].c_value)
215 free(options[i].c_value);
221 /* handle variable substitutions. here's what we understand:
222 * $b -> basename of path
223 * $c -> client ip/appletalk address
224 * $d -> volume pathname on server
225 * $f -> full name (whatever's in the gecos field)
228 * $i -> client ip/appletalk address without port
229 * $s -> server name (hostname if it doesn't exist)
230 * $u -> username (guest is usually nobody)
231 * $v -> volume name or basename if null
232 * $z -> zone (may not exist)
237 #define is_var(a, b) (strncmp((a), (b), 2) == 0)
239 static char *volxlate(AFPObj *obj, char *dest, size_t destlen,
240 char *src, struct passwd *pwd, char *path, char *volname)
250 dest = calloc(destlen +1, 1);
256 strlcpy(dest, src, destlen +1);
257 if ((p = strchr(src, '$')) == NULL) /* nothing to do */
260 /* first part of the path. just forward to the next variable. */
261 len = MIN((size_t)(p - src), destlen);
267 while (p && destlen > 0) {
268 /* now figure out what the variable is */
270 if (is_var(p, "$b")) {
272 if ((q = strrchr(path, '/')) == NULL)
274 else if (*(q + 1) != '\0')
277 } else if (is_var(p, "$c")) {
278 if (obj->proto == AFPPROTO_ASP) {
279 ASP asp = obj->handle;
281 len = sprintf(dest, "%u.%u", ntohs(asp->asp_sat.sat_addr.s_net),
282 asp->asp_sat.sat_addr.s_node);
286 } else if (obj->proto == AFPPROTO_DSI) {
287 DSI *dsi = obj->handle;
289 len = sprintf(dest, "%s:%u", inet_ntoa(dsi->client.sin_addr),
290 ntohs(dsi->client.sin_port));
294 } else if (is_var(p, "$d")) {
296 } else if (is_var(p, "$f")) {
297 if ((q = strchr(pwd->pw_gecos, ',')))
300 } else if (is_var(p, "$g")) {
301 struct group *grp = getgrgid(pwd->pw_gid);
304 } else if (is_var(p, "$h")) {
305 q = obj->options.hostname;
306 } else if (is_var(p, "$i")) {
307 if (obj->proto == AFPPROTO_ASP) {
308 ASP asp = obj->handle;
310 len = sprintf(dest, "%u", ntohs(asp->asp_sat.sat_addr.s_net));
314 } else if (obj->proto == AFPPROTO_DSI) {
315 DSI *dsi = obj->handle;
317 q = inet_ntoa(dsi->client.sin_addr);
319 } else if (is_var(p, "$s")) {
322 else if (obj->options.server) {
323 q = obj->options.server;
325 q = obj->options.hostname;
326 } else if (is_var(p, "$u")) {
328 if ( obj->options.ntseparator && (sep = strchr(obj->username, obj->options.ntseparator[0])) != NULL)
332 } else if (is_var(p, "$v")) {
337 if ((q = strrchr(path, '/')) == NULL)
339 else if (*(q + 1) != '\0')
342 } else if (is_var(p, "$z")) {
344 } else if (is_var(p, "$$")) {
349 /* copy the stuff over. if we don't understand something that we
350 * should, just skip it over. */
352 len = MIN(p == q ? 2 : strlen(q), destlen);
353 strncpy(dest, q, len);
358 /* stuff up to next $ */
360 p = strchr(src, '$');
361 len = p ? MIN((size_t)(p - src), destlen) : destlen;
363 strncpy(dest, src, len);
371 /* to make sure that val is valid, make sure to select an opt that
373 static int optionok(const char *buf, const char *opt, const char *val)
375 if (!strstr(buf,opt))
383 /* -------------------- */
384 static void setoption(struct vol_option *options, struct vol_option *save, int opt, const char *val)
386 if (options[opt].c_value && (!save || options[opt].c_value != save[opt].c_value))
387 free(options[opt].c_value);
388 options[opt].c_value = strdup(val + 1);
391 /* ------------------------------------------
392 handle all the options. tmp can't be NULL. */
393 static void volset(struct vol_option *options, struct vol_option *save,
394 char *volname, int vlen,
399 val = strchr(tmp, ':');
401 /* we'll assume it's a volume name. */
402 strncpy(volname, tmp, vlen);
407 LOG(log_debug, logtype_afpd, "Parsing volset %s", val);
409 if (optionok(tmp, "allow:", val)) {
410 setoption(options, save, VOLOPT_ALLOW, val);
412 } else if (optionok(tmp, "deny:", val)) {
413 setoption(options, save, VOLOPT_DENY, val);
415 } else if (optionok(tmp, "rwlist:", val)) {
416 setoption(options, save, VOLOPT_RWLIST, val);
418 } else if (optionok(tmp, "rolist:", val)) {
419 setoption(options, save, VOLOPT_ROLIST, val);
421 } else if (optionok(tmp, "codepage:", val)) {
422 LOG (log_error, logtype_afpd, "The old codepage system has been removed. Please make sure to read the documentation !!!!");
423 /* Make sure we don't screw anything */
425 } else if (optionok(tmp, "volcharset:", val)) {
426 setoption(options, save, VOLOPT_ENCODING, val);
427 } else if (optionok(tmp, "maccharset:", val)) {
428 setoption(options, save, VOLOPT_MACCHARSET, val);
429 } else if (optionok(tmp, "veto:", val)) {
430 setoption(options, save, VOLOPT_VETO, val);
431 } else if (optionok(tmp, "cnidscheme:", val)) {
432 setoption(options, save, VOLOPT_CNIDSCHEME, val);
433 } else if (optionok(tmp, "casefold:", val)) {
434 if (strcasecmp(val + 1, "tolower") == 0)
435 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMLOWER;
436 else if (strcasecmp(val + 1, "toupper") == 0)
437 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMUPPER;
438 else if (strcasecmp(val + 1, "xlatelower") == 0)
439 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UUPPERMLOWER;
440 else if (strcasecmp(val + 1, "xlateupper") == 0)
441 options[VOLOPT_CASEFOLD].i_value = AFPVOL_ULOWERMUPPER;
442 } else if (optionok(tmp, "adouble:", val)) {
443 if (strcasecmp(val + 1, "v1") == 0)
444 options[VOLOPT_ADOUBLE].i_value = AD_VERSION1;
445 #if AD_VERSION == AD_VERSION2
446 else if (strcasecmp(val + 1, "v2") == 0)
447 options[VOLOPT_ADOUBLE].i_value = AD_VERSION2;
448 else if (strcasecmp(val + 1, "osx") == 0)
449 options[VOLOPT_ADOUBLE].i_value = AD_VERSION2_OSX;
450 else if (strcasecmp(val + 1, "sfm") == 0)
451 options[VOLOPT_ADOUBLE].i_value = AD_VERSION1_SFM;
453 } else if (optionok(tmp, "options:", val)) {
456 if ((p = strtok(val + 1, ",")) == NULL) /* nothing */
460 if (strcasecmp(p, "prodos") == 0)
461 options[VOLOPT_FLAGS].i_value |= AFPVOL_A2VOL;
462 else if (strcasecmp(p, "mswindows") == 0) {
463 options[VOLOPT_FLAGS].i_value |= AFPVOL_MSWINDOWS | AFPVOL_USEDOTS;
464 } else if (strcasecmp(p, "crlf") == 0)
465 options[VOLOPT_FLAGS].i_value |= AFPVOL_CRLF;
466 else if (strcasecmp(p, "noadouble") == 0)
467 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOADOUBLE;
468 else if (strcasecmp(p, "ro") == 0)
469 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
470 else if (strcasecmp(p, "nohex") == 0)
471 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOHEX;
472 else if (strcasecmp(p, "usedots") == 0)
473 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS;
474 else if (strcasecmp(p, "invisibledots") == 0)
475 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS | AFPVOL_INV_DOTS;
476 else if (strcasecmp(p, "limitsize") == 0)
477 options[VOLOPT_FLAGS].i_value |= AFPVOL_LIMITSIZE;
478 /* support for either "dropbox" or "dropkludge" */
479 else if (strcasecmp(p, "dropbox") == 0)
480 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
481 else if (strcasecmp(p, "dropkludge") == 0)
482 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
483 else if (strcasecmp(p, "nofileid") == 0)
484 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOFILEID;
485 else if (strcasecmp(p, "nostat") == 0)
486 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOSTAT;
487 else if (strcasecmp(p, "preexec_close") == 0)
488 options[VOLOPT_PREEXEC].i_value = 1;
489 else if (strcasecmp(p, "root_preexec_close") == 0)
490 options[VOLOPT_ROOTPREEXEC].i_value = 1;
491 else if (strcasecmp(p, "upriv") == 0)
492 options[VOLOPT_FLAGS].i_value |= AFPVOL_UNIX_PRIV;
493 else if (strcasecmp(p, "extattrs") == 0)
494 options[VOLOPT_FLAGS].i_value |= AFPVOL_EXT_ATTRS;
495 else if (strcasecmp(p, "acls") == 0)
496 options[VOLOPT_FLAGS].i_value |= AFPVOL_ACLS;
497 else if (strcasecmp(p, "nodev") == 0)
498 options[VOLOPT_FLAGS].i_value |= AFPVOL_NODEV;
499 else if (strcasecmp(p, "caseinsensitive") == 0)
500 options[VOLOPT_FLAGS].i_value |= AFPVOL_CASEINSEN;
501 else if (strcasecmp(p, "illegalseq") == 0)
502 options[VOLOPT_FLAGS].i_value |= AFPVOL_EILSEQ;
503 else if (strcasecmp(p, "cachecnid") == 0)
504 options[VOLOPT_FLAGS].i_value |= AFPVOL_CACHE;
506 p = strtok(NULL, ",");
509 } else if (optionok(tmp, "dbpath:", val)) {
510 setoption(options, save, VOLOPT_DBPATH, val);
512 } else if (optionok(tmp, "umask:", val)) {
513 options[VOLOPT_UMASK].i_value = (int)strtol(val +1, NULL, 8);
514 } else if (optionok(tmp, "dperm:", val)) {
515 options[VOLOPT_DPERM].i_value = (int)strtol(val+1, NULL, 8);
516 } else if (optionok(tmp, "fperm:", val)) {
517 options[VOLOPT_FPERM].i_value = (int)strtol(val+1, NULL, 8);
518 } else if (optionok(tmp, "perm:", val)) {
519 options[VOLOPT_DFLTPERM].i_value = (int)strtol(val+1, NULL, 8);
520 } else if (optionok(tmp, "mapchars:",val)) {
521 setoption(options, save, VOLOPT_MAPCHARS, val);
523 } else if (optionok(tmp, "password:", val)) {
524 setoption(options, save, VOLOPT_PASSWORD, val);
528 /* this code allows forced uid/gid per volume settings */
529 } else if (optionok(tmp, "forceuid:", val)) {
530 setoption(options, save, VOLOPT_FORCEUID, val);
531 } else if (optionok(tmp, "forcegid:", val)) {
532 setoption(options, save, VOLOPT_FORCEGID, val);
534 #endif /* FORCE_UIDGID */
535 } else if (optionok(tmp, "root_preexec:", val)) {
536 setoption(options, save, VOLOPT_ROOTPREEXEC, val);
538 } else if (optionok(tmp, "preexec:", val)) {
539 setoption(options, save, VOLOPT_PREEXEC, val);
541 } else if (optionok(tmp, "root_postexec:", val)) {
542 setoption(options, save, VOLOPT_ROOTPOSTEXEC, val);
544 } else if (optionok(tmp, "postexec:", val)) {
545 setoption(options, save, VOLOPT_POSTEXEC, val);
547 } else if (optionok(tmp, "allowed_hosts:", val)) {
548 setoption(options, save, VOLOPT_ALLOWED_HOSTS, val);
550 } else if (optionok(tmp, "denied_hosts:", val)) {
551 setoption(options, save, VOLOPT_DENIED_HOSTS, val);
554 /* ignore unknown options */
555 LOG(log_debug, logtype_afpd, "ignoring unknown volume option: %s", tmp);
560 /* ----------------- */
561 static void showvol(const ucs2_t *name)
564 for ( volume = Volumes; volume; volume = volume->v_next ) {
565 if (volume->v_hide && !strcasecmp_w( volume->v_name, name ) ) {
572 /* ------------------------------- */
573 static int creatvol(AFPObj *obj, struct passwd *pwd,
574 char *path, char *name,
575 struct vol_option *options,
576 const int user /* user defined volume */
580 int suffixlen, vlen, tmpvlen, u8mvlen, macvlen;
582 char tmpname[AFPVOL_U8MNAMELEN+1];
583 ucs2_t u8mtmpname[(AFPVOL_U8MNAMELEN+1)*2], mactmpname[(AFPVOL_MACNAMELEN+1)*2];
584 char suffix[6]; /* max is #FFFF */
587 if ( name == NULL || *name == '\0' ) {
588 if ((name = strrchr( path, '/' )) == NULL) {
589 return -1; /* Obviously not a fully qualified path */
592 /* if you wish to share /, you need to specify a name. */
597 /* suffix for mangling use (lastvid + 1) */
598 /* because v_vid has not been decided yet. */
599 suffixlen = sprintf(suffix, "%c%X", MANGLE_CHAR, lastvid + 1 );
601 vlen = strlen( name );
603 /* Unicode Volume Name */
604 /* Firstly convert name from unixcharset to UTF8-MAC */
606 tmpvlen = convert_charset(obj->options.unixcharset, CH_UTF8_MAC, 0, name, vlen, tmpname, AFPVOL_U8MNAMELEN, &flags);
608 strcpy(tmpname, "???");
612 /* Do we have to mangle ? */
613 if ( (flags & CONV_REQMANGLE) || (tmpvlen > obj->options.volnamelen)) {
614 if (tmpvlen + suffixlen > obj->options.volnamelen) {
616 tmpvlen = convert_charset(obj->options.unixcharset, CH_UTF8_MAC, 0, name, vlen, tmpname, obj->options.volnamelen - suffixlen, &flags);
617 tmpname[tmpvlen != (size_t)-1 ? tmpvlen : 0] = 0;
619 strcat(tmpname, suffix);
620 tmpvlen = strlen(tmpname);
623 /* Secondly convert name from UTF8-MAC to UCS2 */
624 if ( 0 >= ( u8mvlen = convert_string(CH_UTF8_MAC, CH_UCS2, tmpname, tmpvlen, u8mtmpname, AFPVOL_U8MNAMELEN*2)) )
627 LOG(log_debug, logtype_afpd, "createvol: Volume '%s' -> UTF8-MAC Name: '%s'", name, tmpname);
629 /* Maccharset Volume Name */
630 /* Firsty convert name from unixcharset to maccharset */
632 tmpvlen = convert_charset(obj->options.unixcharset, obj->options.maccharset, 0, name, vlen, tmpname, AFPVOL_U8MNAMELEN, &flags);
634 strcpy(tmpname, "???");
638 /* Do we have to mangle ? */
639 if ( (flags & CONV_REQMANGLE) || (tmpvlen > AFPVOL_MACNAMELEN)) {
640 if (tmpvlen + suffixlen > AFPVOL_MACNAMELEN) {
642 tmpvlen = convert_charset(obj->options.unixcharset, obj->options.maccharset, 0, name, vlen, tmpname, AFPVOL_MACNAMELEN - suffixlen, &flags);
643 tmpname[tmpvlen != (size_t)-1 ? tmpvlen : 0] = 0;
645 strcat(tmpname, suffix);
646 tmpvlen = strlen(tmpname);
649 /* Secondly convert name from maccharset to UCS2 */
650 if ( 0 >= ( macvlen = convert_string(obj->options.maccharset, CH_UCS2, tmpname, tmpvlen, mactmpname, AFPVOL_U8MNAMELEN*2)) )
653 LOG(log_debug, logtype_afpd, "createvol: Volume '%s' -> Longname: '%s'", name, tmpname);
655 /* check duplicate */
656 for ( volume = Volumes; volume; volume = volume->v_next ) {
657 if (( strcasecmp_w( volume->v_u8mname, u8mtmpname ) == 0 ) || ( strcasecmp_w( volume->v_macname, mactmpname ) == 0 )){
658 if (volume->v_deleted) {
659 volume->v_new = hide = 1;
662 return -1; /* Won't be able to access it, anyway... */
667 if (!( volume = (struct vol *)calloc(1, sizeof( struct vol ))) ) {
668 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
671 if ( NULL == ( volume->v_localname = strdup(name))) {
672 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
677 if ( NULL == ( volume->v_u8mname = strdup_w(u8mtmpname))) {
678 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
683 if ( NULL == ( volume->v_macname = strdup_w(mactmpname))) {
684 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
689 if (!( volume->v_path = (char *)malloc( strlen( path ) + 1 )) ) {
690 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
696 volume->v_name = utf8_encoding()?volume->v_u8mname:volume->v_macname;
697 volume->v_hide = hide;
698 strcpy( volume->v_path, path );
702 #endif /* __svr4__ */
703 /* os X start at 1 and use network order ie. 1 2 3 */
704 volume->v_vid = ++lastvid;
705 volume->v_vid = htons(volume->v_vid);
709 /* should we casefold? */
710 volume->v_casefold = options[VOLOPT_CASEFOLD].i_value;
712 /* shift in some flags */
713 volume->v_flags = options[VOLOPT_FLAGS].i_value;
715 volume->v_ad_options = 0;
716 if ((volume->v_flags & AFPVOL_NODEV))
717 volume->v_ad_options |= ADVOL_NODEV;
718 if ((volume->v_flags & AFPVOL_CACHE))
719 volume->v_ad_options |= ADVOL_CACHE;
720 if ((volume->v_flags & AFPVOL_UNIX_PRIV))
721 volume->v_ad_options |= ADVOL_UNIXPRIV;
722 if ((volume->v_flags & AFPVOL_INV_DOTS))
723 volume->v_ad_options |= ADVOL_INVDOTS;
725 if (options[VOLOPT_PASSWORD].c_value)
726 volume->v_password = strdup(options[VOLOPT_PASSWORD].c_value);
728 if (options[VOLOPT_VETO].c_value)
729 volume->v_veto = strdup(options[VOLOPT_VETO].c_value);
731 if (options[VOLOPT_ENCODING].c_value)
732 volume->v_volcodepage = strdup(options[VOLOPT_ENCODING].c_value);
734 if (options[VOLOPT_MACCHARSET].c_value)
735 volume->v_maccodepage = strdup(options[VOLOPT_MACCHARSET].c_value);
737 if (options[VOLOPT_DBPATH].c_value)
738 volume->v_dbpath = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_DBPATH].c_value, pwd, path, name);
740 if (options[VOLOPT_CNIDSCHEME].c_value)
741 volume->v_cnidscheme = strdup(options[VOLOPT_CNIDSCHEME].c_value);
743 if (options[VOLOPT_UMASK].i_value)
744 volume->v_umask = (mode_t)options[VOLOPT_UMASK].i_value;
746 if (options[VOLOPT_DPERM].i_value)
747 volume->v_dperm = (mode_t)options[VOLOPT_DPERM].i_value;
749 if (options[VOLOPT_FPERM].i_value)
750 volume->v_fperm = (mode_t)options[VOLOPT_FPERM].i_value;
752 if (options[VOLOPT_DFLTPERM].i_value)
753 volume->v_perm = (mode_t)options[VOLOPT_DFLTPERM].i_value;
755 if (options[VOLOPT_ADOUBLE].i_value)
756 volume->v_adouble = options[VOLOPT_ADOUBLE].i_value;
758 volume->v_adouble = AD_VERSION;
762 if (options[VOLOPT_FORCEUID].c_value) {
763 volume->v_forceuid = strdup(options[VOLOPT_FORCEUID].c_value);
765 volume->v_forceuid = NULL; /* set as null so as to return 0 later on */
768 if (options[VOLOPT_FORCEGID].c_value) {
769 volume->v_forcegid = strdup(options[VOLOPT_FORCEGID].c_value);
771 volume->v_forcegid = NULL; /* set as null so as to return 0 later on */
775 if (options[VOLOPT_PREEXEC].c_value)
776 volume->v_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_PREEXEC].c_value, pwd, path, name);
777 volume->v_preexec_close = options[VOLOPT_PREEXEC].i_value;
779 if (options[VOLOPT_POSTEXEC].c_value)
780 volume->v_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_POSTEXEC].c_value, pwd, path, name);
782 if (options[VOLOPT_ROOTPREEXEC].c_value)
783 volume->v_root_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPREEXEC].c_value, pwd, path, name);
784 volume->v_root_preexec_close = options[VOLOPT_ROOTPREEXEC].i_value;
786 if (options[VOLOPT_ROOTPOSTEXEC].c_value)
787 volume->v_root_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPOSTEXEC].c_value, pwd, path, name);
790 volume->v_dperm |= volume->v_perm;
791 volume->v_fperm |= volume->v_perm;
793 volume->v_next = Volumes;
798 /* ---------------- */
799 static char *myfgets( buf, size, fp )
808 while ((EOF != ( c = getc( fp )) ) && ( size > 1 )) {
809 if ( c == '\n' || c == '\r' ) {
810 if (p != buf && *(p -1) == '\\') {
832 /* check access list. this function wants something of the following
834 * @group,name,name2,@group2,name3
836 * a NULL argument allows everybody to have access.
837 * we return three things:
839 * 0: list exists, but name isn't in it
843 #ifndef NO_REAL_USER_NAME
844 /* authentication is case insensitive
845 * FIXME should we do the same with group name?
847 #define access_strcmp strcasecmp
850 #define access_strcmp strcmp
854 static int accessvol(args, name)
858 char buf[MAXPATHLEN + 1], *p;
864 strlcpy(buf, args, sizeof(buf));
865 if ((p = strtok(buf, ",")) == NULL) /* nothing, return okay */
869 if (*p == '@') { /* it's a group */
870 if ((gr = getgrnam(p + 1)) && gmem(gr->gr_gid))
872 } else if (access_strcmp(p, name) == 0) /* it's a user name */
874 p = strtok(NULL, ",");
880 static int hostaccessvol(type, volname, args, obj)
886 char buf[MAXPATHLEN + 1], *p, *b;
887 DSI *dsi = obj->handle;
892 strlcpy(buf, args, sizeof(buf));
893 if ((p = strtok_r(buf, ",", &b)) == NULL) /* nothing, return okay */
897 if (obj->proto == AFPPROTO_DSI) {
898 struct in_addr mask, net;
899 char *net_char, *mask_char;
902 net_char = strtok(p, "/");
903 mask_char = strtok(NULL,"/");
904 if (mask_char == NULL) {
907 mask_int = atoi(mask_char);
910 // convert the integer netmask to a bitmask in network order
911 mask.s_addr = htonl(-1 - ((1 << (32 - mask_int)) - 1));
912 net.s_addr = inet_addr(net_char) & mask.s_addr;
914 if ((dsi->client.sin_addr.s_addr & mask.s_addr) == net.s_addr) {
915 if (type == VOLOPT_DENIED_HOSTS)
916 LOG(log_info, logtype_afpd, "AFP access denied for client IP '%s' to volume '%s' by denied list",
917 inet_ntoa(dsi->client.sin_addr), volname);
921 p = strtok_r(NULL, ",", &b);
923 if (type == VOLOPT_ALLOWED_HOSTS)
924 LOG(log_info, logtype_afpd, "AFP access denied for client IP '%s' to volume '%s', not in allowed list",
925 inet_ntoa(dsi->client.sin_addr), volname);
929 static void setextmap( ext, type, creator, user)
930 char *ext, *type, *creator;
936 if (Extmap == NULL) {
937 if (( Extmap = calloc(1, sizeof( struct extmap ))) == NULL ) {
938 LOG(log_error, logtype_afpd, "setextmap: calloc: %s", strerror(errno) );
943 for ( em = Extmap, cnt = 0; em->em_ext; em++, cnt++) {
944 if ( (strdiacasecmp( em->em_ext, ext )) == 0 ) {
949 if ( em->em_ext == NULL ) {
950 if (!(Extmap = realloc( Extmap, sizeof( struct extmap ) * (cnt +2))) ) {
951 LOG(log_error, logtype_afpd, "setextmap: realloc: %s", strerror(errno) );
954 (Extmap +cnt +1)->em_ext = NULL;
956 } else if ( !user ) {
962 if (!(em->em_ext = strdup( ext))) {
963 LOG(log_error, logtype_afpd, "setextmap: strdup: %s", strerror(errno) );
967 if ( *type == '\0' ) {
968 memcpy(em->em_type, "????", sizeof( em->em_type ));
970 memcpy(em->em_type, type, sizeof( em->em_type ));
972 if ( *creator == '\0' ) {
973 memcpy(em->em_creator, "UNIX", sizeof( em->em_creator ));
975 memcpy(em->em_creator, creator, sizeof( em->em_creator ));
979 /* -------------------------- */
980 static int extmap_cmp(const void *map1, const void *map2)
982 const struct extmap *em1 = map1;
983 const struct extmap *em2 = map2;
984 return strdiacasecmp(em1->em_ext, em2->em_ext);
987 static void sortextmap( void)
992 if ((em = Extmap) == NULL) {
1000 qsort(Extmap, Extmap_cnt, sizeof(struct extmap), extmap_cmp);
1001 if (*Extmap->em_ext == 0) {
1002 /* the first line is really "." the default entry,
1003 * we remove the leading '.' in setextmap
1010 /* ----------------------
1012 static void free_extmap( void)
1017 for ( em = Extmap; em->em_ext; em++) {
1027 /* ----------------------
1029 static int volfile_changed(struct afp_volume_name *p)
1035 name = p->full_name;
1039 if (!stat( name, &st) && st.st_mtime > p->mtime) {
1040 p->mtime = st.st_mtime;
1046 /* ----------------------
1047 * Read a volume configuration file and add the volumes contained within to
1048 * the global volume list. If p2 is non-NULL, the file that is opened is
1051 * Lines that begin with # and blank lines are ignored.
1052 * Volume lines are of the form:
1053 * <unix path> [<volume name>] [allow:<user>,<@group>,...] \
1054 * [codepage:<file>] [casefold:<num>]
1055 * <extension> TYPE [CREATOR]
1057 static int readvolfile(obj, p1, p2, user, pwent)
1059 struct afp_volume_name *p1;
1062 struct passwd *pwent;
1065 char path[ MAXPATHLEN + 1], tmp[ MAXPATHLEN + 1],
1066 volname[ AFPVOL_U8MNAMELEN + 1 ], buf[ BUFSIZ ],
1067 type[ 5 ], creator[ 5 ];
1070 struct vol_option options[VOLOPT_NUM], save_options[VOLOPT_NUM];
1078 strcpy( path, p1->name );
1080 strcat( path, "/" );
1082 if (p1->full_name) {
1083 free(p1->full_name);
1085 p1->full_name = strdup(path);
1088 if (NULL == ( fp = fopen( path, "r" )) ) {
1092 if (fd != -1 && !fstat( fd, &st) ) {
1093 p1->mtime = st.st_mtime;
1096 memset(save_options, 0, sizeof(save_options));
1097 while ( myfgets( buf, sizeof( buf ), fp ) != NULL ) {
1098 initline( strlen( buf ), buf );
1099 parseline( sizeof( path ) - 1, path );
1106 /* change the default options for this file */
1107 if (strncmp(path, VOLOPT_DEFAULT, VOLOPT_DEFAULT_LEN) == 0) {
1109 for (i = 0; i < VOLOPT_NUM; i++) {
1110 if (parseline( sizeof( path ) - VOLOPT_DEFAULT_LEN - 1,
1111 path + VOLOPT_DEFAULT_LEN) < 0)
1113 volset(save_options, NULL, tmp, sizeof(tmp) - 1,
1114 path + VOLOPT_DEFAULT_LEN);
1120 if (( p = strchr( path, '/' )) != NULL ) {
1128 if ( u == NULL || *u == '\0' || ( pw = getpwnam( u )) == NULL ) {
1131 strcpy( tmp, pw->pw_dir );
1132 if ( p != NULL && *p != '\0' ) {
1136 /* Tag a user's home directory with their umask. Note, this will
1137 * be overwritten if the user actually specifies a umask: option
1138 * for a '~' volume. */
1139 save_options[VOLOPT_UMASK].i_value = obj->options.save_mask;
1143 /* send path through variable substitution */
1144 if (*path != '~') /* need to copy path to tmp */
1147 pwent = getpwnam(obj->username);
1148 volxlate(obj, path, sizeof(path) - 1, tmp, pwent, NULL, NULL);
1150 /* this is sort of braindead. basically, i want to be
1151 * able to specify things in any order, but i don't want to
1152 * re-write everything.
1154 * currently we have options:
1162 * options:prodos,crlf,noadouble,ro...
1167 * namemask:x,y,!z (not implemented yet)
1169 memcpy(options, save_options, sizeof(options));
1172 /* read in up to VOLOP_NUM possible options */
1173 for (i = 0; i < VOLOPT_NUM; i++) {
1174 if (parseline( sizeof( tmp ) - 1, tmp ) < 0)
1177 volset(options, save_options, volname, sizeof(volname) - 1, tmp);
1180 /* check allow/deny lists:
1181 allow -> either no list (-1), or in list (1)
1182 deny -> either no list (-1), or not in list (0) */
1183 if (accessvol(options[VOLOPT_ALLOW].c_value, obj->username) &&
1184 (accessvol(options[VOLOPT_DENY].c_value, obj->username) < 1) &&
1185 hostaccessvol(VOLOPT_ALLOWED_HOSTS, volname, options[VOLOPT_ALLOWED_HOSTS].c_value, obj) &&
1186 (hostaccessvol(VOLOPT_DENIED_HOSTS, volname, options[VOLOPT_DENIED_HOSTS].c_value, obj) < 1)) {
1188 /* handle read-only behaviour. semantics:
1189 * 1) neither the rolist nor the rwlist exist -> rw
1190 * 2) rolist exists -> ro if user is in it.
1191 * 3) rwlist exists -> ro unless user is in it. */
1192 if (((options[VOLOPT_FLAGS].i_value & AFPVOL_RO) == 0) &&
1193 ((accessvol(options[VOLOPT_ROLIST].c_value,
1194 obj->username) == 1) ||
1195 !accessvol(options[VOLOPT_RWLIST].c_value,
1197 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
1199 /* do variable substitution for volname */
1200 volxlate(obj, tmp, sizeof(tmp) - 1, volname, pwent, path, NULL);
1201 creatvol(obj, pwent, path, tmp, options, p2 != NULL);
1203 volfree(options, save_options);
1207 parseline( sizeof( type ) - 1, type );
1208 parseline( sizeof( creator ) - 1, creator );
1209 setextmap( path, type, creator, user);
1216 volfree(save_options, NULL);
1218 if ( fclose( fp ) != 0 ) {
1219 LOG(log_error, logtype_afpd, "readvolfile: fclose: %s", strerror(errno) );
1225 /* ------------------------------- */
1226 static void volume_free(struct vol *vol)
1228 free(vol->v_localname);
1229 vol->v_localname = NULL;
1230 free(vol->v_u8mname);
1231 vol->v_u8mname = NULL;
1232 free(vol->v_macname);
1233 vol->v_macname = NULL;
1235 free(vol->v_password);
1237 free(vol->v_volcodepage);
1238 free(vol->v_maccodepage);
1239 free(vol->v_cnidscheme);
1240 free(vol->v_dbpath);
1243 free(vol->v_forceuid);
1244 free(vol->v_forcegid);
1245 #endif /* FORCE_UIDGID */
1248 /* ------------------------------- */
1249 static void free_volumes(void )
1252 struct vol *nvol, *ovol;
1254 for ( vol = Volumes; vol; vol = vol->v_next ) {
1255 if (( vol->v_flags & AFPVOL_OPEN ) ) {
1262 for ( vol = Volumes, ovol = NULL; vol; vol = nvol) {
1265 if (vol->v_localname == NULL) {
1266 if (Volumes == vol) {
1271 ovol->v_next = nvol;
1281 /* ------------------------------- */
1282 static void volume_unlink(struct vol *volume)
1284 struct vol *vol, *ovol, *nvol;
1286 if (volume == Volumes) {
1287 Volumes = Volumes->v_next;
1290 for ( vol = Volumes->v_next, ovol = Volumes; vol; vol = nvol) {
1293 if (vol == volume) {
1294 ovol->v_next = nvol;
1303 static int getvolspace( vol, bfree, btotal, xbfree, xbtotal, bsize )
1305 u_int32_t *bfree, *btotal, *bsize;
1306 VolSpace *xbfree, *xbtotal;
1310 #ifndef NO_QUOTA_SUPPORT
1311 VolSpace qfree, qtotal;
1314 spaceflag = AFPVOL_GVSMASK & vol->v_flags;
1315 /* report up to 2GB if afp version is < 2.2 (4GB if not) */
1316 maxsize = (vol->v_flags & AFPVOL_A2VOL) ? 0x01fffe00 :
1317 (((afp_version < 22) || (vol->v_flags & AFPVOL_LIMITSIZE))
1318 ? 0x7fffffffL : 0xffffffffL);
1321 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_AFSGVS ) {
1322 if ( afs_getvolspace( vol, xbfree, xbtotal, bsize ) == AFP_OK ) {
1323 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_AFSGVS;
1324 goto getvolspace_done;
1329 if (( rc = ustatfs_getvolspace( vol, xbfree, xbtotal,
1330 bsize)) != AFP_OK ) {
1334 #define min(a,b) ((a)<(b)?(a):(b))
1335 #ifndef NO_QUOTA_SUPPORT
1336 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_UQUOTA ) {
1337 if ( uquota_getvolspace( vol, &qfree, &qtotal, *bsize ) == AFP_OK ) {
1338 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_UQUOTA;
1339 *xbfree = min(*xbfree, qfree);
1340 *xbtotal = min( *xbtotal, qtotal);
1341 goto getvolspace_done;
1345 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_USTATFS;
1348 *bfree = min( *xbfree, maxsize);
1349 *btotal = min( *xbtotal, maxsize);
1353 /* -----------------------
1354 * set volume creation date
1355 * avoid duplicate, well at least it tries
1357 static void vol_setdate(u_int16_t id, struct adouble *adp, time_t date)
1360 struct vol *vol = Volumes;
1362 for ( volume = Volumes; volume; volume = volume->v_next ) {
1363 if (volume->v_vid == id) {
1366 else if ((time_t)(AD_DATE_FROM_UNIX(date)) == volume->v_ctime) {
1368 volume = Volumes; /* restart */
1371 vol->v_ctime = AD_DATE_FROM_UNIX(date);
1372 ad_setdate(adp, AD_DATE_CREATE | AD_DATE_UNIX, date);
1375 /* ----------------------- */
1376 static int getvolparams( bitmap, vol, st, buf, buflen )
1384 int bit = 0, isad = 1;
1387 u_int32_t bfree, btotal, bsize;
1388 VolSpace xbfree, xbtotal; /* extended bytes */
1389 char *data, *nameoff = NULL;
1392 LOG(log_debug, logtype_afpd, "getvolparams: Volume '%s'", vol->v_localname);
1394 /* courtesy of jallison@whistle.com:
1395 * For MacOS8.x support we need to create the
1396 * .Parent file here if it doesn't exist. */
1398 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1399 if ( ad_open_metadata( vol->v_path, vol_noadouble(vol) | ADFLAGS_DIR, O_CREAT, &ad) < 0 ) {
1401 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1403 } else if (ad_get_MD_flags( &ad ) & O_CREAT) {
1404 slash = strrchr( vol->v_path, '/' );
1408 slash = vol->v_path;
1409 if (ad_getentryoff(&ad, ADEID_NAME)) {
1410 ad_setentrylen( &ad, ADEID_NAME, strlen( slash ));
1411 memcpy(ad_entry( &ad, ADEID_NAME ), slash,
1412 ad_getentrylen( &ad, ADEID_NAME ));
1414 vol_setdate(vol->v_vid, &ad, st->st_mtime);
1418 if (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0)
1419 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1421 vol->v_ctime = aint;
1424 if (( bitmap & ( (1<<VOLPBIT_BFREE)|(1<<VOLPBIT_BTOTAL) |
1425 (1<<VOLPBIT_XBFREE)|(1<<VOLPBIT_XBTOTAL) |
1426 (1<<VOLPBIT_BSIZE)) ) != 0 ) {
1427 if ( getvolspace( vol, &bfree, &btotal, &xbfree, &xbtotal,
1430 ad_close( &ad, ADFLAGS_HF );
1432 return( AFPERR_PARAM );
1437 while ( bitmap != 0 ) {
1438 while (( bitmap & 1 ) == 0 ) {
1446 if (0 == (vol->v_flags & AFPVOL_NOFILEID) && vol->v_cdb != NULL &&
1447 (vol->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1448 ashort = VOLPBIT_ATTR_FILEID;
1450 /* check for read-only.
1451 * NOTE: we don't actually set the read-only flag unless
1452 * it's passed in that way as it's possible to mount
1453 * a read-write filesystem under a read-only one. */
1454 if ((vol->v_flags & AFPVOL_RO) ||
1455 ((utime(vol->v_path, NULL) < 0) && (errno == EROFS))) {
1456 ashort |= VOLPBIT_ATTR_RO;
1458 ashort |= VOLPBIT_ATTR_CATSEARCH;
1459 if (afp_version >= 30) {
1460 ashort |= VOLPBIT_ATTR_UTF8;
1461 if (vol->v_flags & AFPVOL_UNIX_PRIV)
1462 ashort |= VOLPBIT_ATTR_UNIXPRIV;
1464 if (afp_version >= 32) {
1465 if (vol->v_flags & AFPVOL_EXT_ATTRS)
1466 ashort |= VOLPBIT_ATTR_EXT_ATTRS;
1467 if (vol->v_flags & AFPVOL_ACLS)
1468 ashort |= VOLPBIT_ATTR_ACLS;
1470 ashort = htons(ashort);
1471 memcpy(data, &ashort, sizeof( ashort ));
1472 data += sizeof( ashort );
1476 ashort = htons( AFPVOLSIG_DEFAULT );
1477 memcpy(data, &ashort, sizeof( ashort ));
1478 data += sizeof( ashort );
1481 case VOLPBIT_CDATE :
1482 aint = vol->v_ctime;
1483 memcpy(data, &aint, sizeof( aint ));
1484 data += sizeof( aint );
1487 case VOLPBIT_MDATE :
1488 if ( st->st_mtime > vol->v_mtime ) {
1489 vol->v_mtime = st->st_mtime;
1491 aint = AD_DATE_FROM_UNIX(vol->v_mtime);
1492 memcpy(data, &aint, sizeof( aint ));
1493 data += sizeof( aint );
1496 case VOLPBIT_BDATE :
1497 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1498 aint = AD_DATE_START;
1499 memcpy(data, &aint, sizeof( aint ));
1500 data += sizeof( aint );
1504 memcpy(data, &vol->v_vid, sizeof( vol->v_vid ));
1505 data += sizeof( vol->v_vid );
1508 case VOLPBIT_BFREE :
1509 bfree = htonl( bfree );
1510 memcpy(data, &bfree, sizeof( bfree ));
1511 data += sizeof( bfree );
1514 case VOLPBIT_BTOTAL :
1515 btotal = htonl( btotal );
1516 memcpy(data, &btotal, sizeof( btotal ));
1517 data += sizeof( btotal );
1520 #ifndef NO_LARGE_VOL_SUPPORT
1521 case VOLPBIT_XBFREE :
1522 xbfree = hton64( xbfree );
1523 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1524 bcopy(&xbfree, data, sizeof(xbfree));
1525 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1526 memcpy(data, &xbfree, sizeof( xbfree ));
1527 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1528 data += sizeof( xbfree );
1531 case VOLPBIT_XBTOTAL :
1532 xbtotal = hton64( xbtotal );
1533 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1534 bcopy(&xbtotal, data, sizeof(xbtotal));
1535 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1536 memcpy(data, &xbtotal, sizeof( xbtotal ));
1537 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1538 data += sizeof( xbfree );
1540 #endif /* ! NO_LARGE_VOL_SUPPORT */
1544 data += sizeof( u_int16_t );
1547 case VOLPBIT_BSIZE: /* block size */
1548 bsize = htonl(bsize);
1549 memcpy(data, &bsize, sizeof(bsize));
1550 data += sizeof(bsize);
1555 ad_close( &ad, ADFLAGS_HF );
1557 return( AFPERR_BITMAP );
1563 ashort = htons( data - buf );
1564 memcpy(nameoff, &ashort, sizeof( ashort ));
1565 /* name is always in mac charset */
1566 aint = ucs2_to_charset( vol->v_maccharset, vol->v_macname, data+1, AFPVOL_MACNAMELEN + 1);
1576 ad_close_metadata( &ad);
1578 *buflen = data - buf;
1582 /* ------------------------- */
1583 static int stat_vol(u_int16_t bitmap, struct vol *vol, char *rbuf, int *rbuflen)
1588 if ( stat( vol->v_path, &st ) < 0 ) {
1590 return( AFPERR_PARAM );
1592 /* save the volume device number */
1593 vol->v_dev = st.st_dev;
1595 buflen = *rbuflen - sizeof( bitmap );
1596 if (( ret = getvolparams( bitmap, vol, &st,
1597 rbuf + sizeof( bitmap ), &buflen )) != AFP_OK ) {
1601 *rbuflen = buflen + sizeof( bitmap );
1602 bitmap = htons( bitmap );
1603 memcpy(rbuf, &bitmap, sizeof( bitmap ));
1608 /* ------------------------------- */
1609 void load_volumes(AFPObj *obj)
1611 struct passwd *pwent;
1616 /* check files date */
1617 if (obj->options.defaultvol.loaded) {
1618 changed = volfile_changed(&obj->options.defaultvol);
1620 if (obj->options.systemvol.loaded) {
1621 changed |= volfile_changed(&obj->options.systemvol);
1623 if (obj->options.uservol.loaded) {
1624 changed |= volfile_changed(&obj->options.uservol);
1633 pwent = getpwnam(obj->username);
1634 if ( (obj->options.flags & OPTION_USERVOLFIRST) == 0 ) {
1635 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent);
1638 if ((*obj->username == '\0') || (obj->options.flags & OPTION_NOUSERVOL)) {
1639 readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent);
1642 * Read user's AppleVolumes or .AppleVolumes file
1643 * If neither are readable, read the default volumes file. if
1644 * that doesn't work, create a user share.
1646 if (obj->options.uservol.name) {
1647 free(obj->options.uservol.name);
1649 obj->options.uservol.name = strdup(pwent->pw_dir);
1650 if ( readvolfile(obj, &obj->options.uservol, "AppleVolumes", 1, pwent) < 0 &&
1651 readvolfile(obj, &obj->options.uservol, ".AppleVolumes", 1, pwent) < 0 &&
1652 readvolfile(obj, &obj->options.uservol, "applevolumes", 1, pwent) < 0 &&
1653 readvolfile(obj, &obj->options.uservol, ".applevolumes", 1, pwent) < 0 &&
1654 obj->options.defaultvol.name != NULL ) {
1655 if (readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent) < 0)
1656 creatvol(obj, pwent, pwent->pw_dir, NULL, NULL, 1);
1659 if ( obj->options.flags & OPTION_USERVOLFIRST ) {
1660 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent );
1663 if ( obj->options.closevol ) {
1666 for ( vol = Volumes; vol; vol = vol->v_next ) {
1667 if (vol->v_deleted && !vol->v_new ) {
1676 /* ------------------------------- */
1677 int afp_getsrvrparms(obj, ibuf, ibuflen, rbuf, rbuflen )
1679 char *ibuf _U_, *rbuf;
1680 int ibuflen _U_, *rbuflen;
1693 for ( vcnt = 0, volume = Volumes; volume; volume = volume->v_next ) {
1694 if (!(volume->v_flags & AFPVOL_NOSTAT)) {
1697 if ( stat( volume->v_path, &st ) < 0 ) {
1698 LOG(log_info, logtype_afpd, "afp_getsrvrparms(%s): stat: %s",
1699 volume->v_path, strerror(errno) );
1700 continue; /* can't access directory */
1702 if (!S_ISDIR(st.st_mode)) {
1703 continue; /* not a dir */
1705 accessmode(volume->v_path, &ma, NULL, &st);
1706 if ((ma.ma_user & (AR_UREAD | AR_USEARCH)) != (AR_UREAD | AR_USEARCH)) {
1707 continue; /* no r-x access */
1710 if (volume->v_hide) {
1711 continue; /* config file changed but the volume was mounted */
1714 if (utf8_encoding()) {
1715 len = ucs2_to_charset_allocate(CH_UTF8_MAC, &namebuf, volume->v_u8mname);
1717 len = ucs2_to_charset_allocate(obj->options.maccharset, &namebuf, volume->v_macname);
1720 if (len == (size_t)-1)
1723 /* set password bit if there's a volume password */
1724 *data = (volume->v_password) ? AFPSRVR_PASSWD : 0;
1726 /* Apple 2 clients running ProDOS-8 expect one volume to have
1727 bit 0 of this byte set. They will not recognize anything
1728 on the server unless this is the case. I have not
1729 completely worked this out, but it's related to booting
1730 from the server. Support for that function is a ways
1731 off.. <shirsch@ibm.net> */
1732 *data |= (volume->v_flags & AFPVOL_A2VOL) ? AFPSRVR_CONFIGINFO : 0;
1733 *data++ |= 0; /* UNIX PRIVS BIT ..., OSX doesn't seem to use it, so we don't either */
1735 memcpy(data, namebuf, len );
1741 *rbuflen = data - rbuf;
1743 if ( gettimeofday( &tv, 0 ) < 0 ) {
1744 LOG(log_error, logtype_afpd, "afp_getsrvrparms(%s): gettimeofday: %s", volume->v_path, strerror(errno) );
1746 return AFPERR_PARAM;
1748 tv.tv_sec = AD_DATE_FROM_UNIX(tv.tv_sec);
1749 memcpy(data, &tv.tv_sec, sizeof( u_int32_t));
1750 data += sizeof( u_int32_t);
1755 /* -------------------------
1756 * we are the user here
1758 int afp_openvol(obj, ibuf, ibuflen, rbuf, rbuflen )
1761 int ibuflen _U_, *rbuflen;
1771 char path[ MAXPATHLEN + 1];
1777 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1778 bitmap = ntohs( bitmap );
1779 ibuf += sizeof( bitmap );
1780 if (( bitmap & (1<<VOLPBIT_VID)) == 0 ) {
1782 return AFPERR_BITMAP;
1785 len = (unsigned char)*ibuf++;
1786 volname = obj->oldtmp;
1788 if ((volname_tmp = strchr(volname,'+')) != NULL)
1789 volname = volname_tmp+1;
1791 if (utf8_encoding()) {
1792 namelen = convert_string(CH_UTF8_MAC, CH_UCS2, ibuf, len, volname, sizeof(obj->oldtmp));
1794 namelen = convert_string(obj->options.maccharset, CH_UCS2, ibuf, len, volname, sizeof(obj->oldtmp));
1799 return AFPERR_PARAM;
1803 if ((len + 1) & 1) /* pad to an even boundary */
1808 for ( volume = Volumes; volume; volume = volume->v_next ) {
1809 if ( strcasecmp_w( (ucs2_t*) volname, volume->v_name ) == 0 ) {
1814 if ( volume == NULL ) {
1816 return AFPERR_PARAM;
1819 /* check for a volume password */
1820 if (volume->v_password && strncmp(ibuf, volume->v_password, VOLPASSLEN)) {
1822 return AFPERR_ACCESS;
1825 if (( volume->v_flags & AFPVOL_OPEN ) ) {
1826 /* the volume is already open */
1828 set_uidgid ( volume );
1830 return stat_vol(bitmap, volume, rbuf, rbuflen);
1833 /* initialize volume variables
1836 if (utf8_encoding()) {
1837 volume->max_filename = 255;
1840 volume->max_filename = MACFILELEN;
1843 volume->v_dir = volume->v_root = NULL;
1844 volume->v_hash = NULL;
1846 volume->v_flags |= AFPVOL_OPEN;
1847 volume->v_cdb = NULL;
1849 if (volume->v_root_preexec) {
1850 if ((ret = afprun(1, volume->v_root_preexec, NULL)) && volume->v_root_preexec_close) {
1851 LOG(log_error, logtype_afpd, "afp_openvol(%s): root preexec : %d", volume->v_path, ret );
1858 set_uidgid ( volume );
1861 if (volume->v_preexec) {
1862 if ((ret = afprun(0, volume->v_preexec, NULL)) && volume->v_preexec_close) {
1863 LOG(log_error, logtype_afpd, "afp_openvol(%s): preexec : %d", volume->v_path, ret );
1869 if ( stat( volume->v_path, &st ) < 0 ) {
1874 if ( chdir( volume->v_path ) < 0 ) {
1879 if (utf8_encoding()) {
1880 len = convert_string_allocate(CH_UCS2, CH_UTF8_MAC, volume->v_u8mname, namelen, &vol_mname);
1882 len = convert_string_allocate(CH_UCS2, obj->options.maccharset, volume->v_macname, namelen, &vol_mname);
1884 if ( !vol_mname || len <= 0) {
1889 if ( NULL == getcwd(path, MAXPATHLEN)) {
1890 /* shouldn't be fatal but it will fail later */
1891 LOG(log_error, logtype_afpd, "afp_openvol(%s): volume pathlen too long", volume->v_path);
1896 if ((vol_uname = strrchr(path, '/')) == NULL)
1898 else if (*(vol_uname + 1) != '\0')
1901 if ((dir = dirnew(vol_mname, vol_uname) ) == NULL) {
1903 LOG(log_error, logtype_afpd, "afp_openvol(%s): malloc: %s", volume->v_path, strerror(errno) );
1909 dir->d_did = DIRDID_ROOT;
1910 dir->d_color = DIRTREE_COLOR_BLACK; /* root node is black */
1911 dir->d_m_name_ucs2 = strdup_w(volume->v_name);
1912 volume->v_dir = volume->v_root = dir;
1913 volume->v_hash = dirhash();
1915 curdir = volume->v_dir;
1916 if (volume->v_cnidscheme == NULL) {
1917 volume->v_cnidscheme = strdup(DEFAULT_CNID_SCHEME);
1918 LOG(log_info, logtype_afpd, "Volume %s use CNID scheme %s.", volume->v_path, volume->v_cnidscheme);
1920 if (volume->v_dbpath)
1921 volume->v_cdb = cnid_open (volume->v_dbpath, volume->v_umask, volume->v_cnidscheme, (volume->v_flags & AFPVOL_NODEV));
1923 volume->v_cdb = cnid_open (volume->v_path, volume->v_umask, volume->v_cnidscheme, (volume->v_flags & AFPVOL_NODEV));
1924 if (volume->v_cdb == NULL) {
1925 LOG(log_error, logtype_afpd, "Fatal error: cannot open CNID or invalid CNID backend for %s: %s",
1926 volume->v_path, volume->v_cnidscheme);
1933 if (!volume->v_volcodepage)
1934 volume->v_volcodepage = strdup("UTF8");
1936 if ( (charset_t) -1 == ( volume->v_volcharset = add_charset(volume->v_volcodepage)) ) {
1937 LOG (log_error, logtype_afpd, "Setting codepage %s as volume codepage failed", volume->v_volcodepage);
1942 if ( NULL == ( volume->v_vol = find_charset_functions(volume->v_volcodepage)) || volume->v_vol->flags & CHARSET_ICONV ) {
1943 LOG (log_warning, logtype_afpd, "WARNING: volume encoding %s is *not* supported by netatalk, expect problems !!!!", volume->v_volcodepage);
1946 if (!volume->v_maccodepage)
1947 volume->v_maccodepage = strdup(obj->options.maccodepage);
1949 if ( (charset_t) -1 == ( volume->v_maccharset = add_charset(volume->v_maccodepage)) ) {
1950 LOG (log_error, logtype_afpd, "Setting codepage %s as mac codepage failed", volume->v_maccodepage);
1955 if ( NULL == ( volume->v_mac = find_charset_functions(volume->v_maccodepage)) || ! (volume->v_mac->flags & CHARSET_CLIENT) ) {
1956 LOG (log_error, logtype_afpd, "Fatal error: mac charset %s not supported", volume->v_maccodepage);
1961 ret = stat_vol(bitmap, volume, rbuf, rbuflen);
1962 if (ret == AFP_OK) {
1964 if (!(volume->v_flags & AFPVOL_RO)) {
1965 handle_special_folders( volume );
1966 savevoloptions( volume);
1970 * If you mount a volume twice, the second time the trash appears on
1971 * the desk-top. That's because the Mac remembers the DID for the
1972 * trash (even for volumes in different zones, on different servers).
1973 * Just so this works better, we prime the DID cache with the trash,
1974 * fixing the trash at DID 17.
1975 * FIXME (RL): should it be done inside a CNID backend ? (always returning Trash DID when asked) ?
1977 if ((volume->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1979 /* FIXME find db time stamp */
1980 if (cnid_getstamp(volume->v_cdb, volume->v_stamp, sizeof(volume->v_stamp)) < 0) {
1981 LOG (log_error, logtype_afpd,
1982 "afp_openvol(%s): Fatal error: Unable to get stamp value from CNID backend",
1990 cname( volume, volume->v_dir, &p );
1996 if (volume->v_dir) {
1997 hash_free( volume->v_hash);
1998 dirfree( volume->v_dir );
1999 volume->v_dir = volume->v_root = NULL;
2002 volume->v_flags &= ~AFPVOL_OPEN;
2003 if (volume->v_cdb != NULL) {
2004 cnid_close(volume->v_cdb);
2005 volume->v_cdb = NULL;
2011 /* ------------------------- */
2012 static void closevol(struct vol *vol)
2017 hash_free( vol->v_hash);
2018 dirfree( vol->v_root );
2020 if (vol->v_cdb != NULL) {
2021 cnid_close(vol->v_cdb);
2025 if (vol->v_postexec) {
2026 afprun(0, vol->v_postexec, NULL);
2028 if (vol->v_root_postexec) {
2029 afprun(1, vol->v_root_postexec, NULL);
2033 /* ------------------------- */
2034 void close_all_vol(void)
2038 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
2039 if ( (ovol->v_flags & AFPVOL_OPEN) ) {
2040 ovol->v_flags &= ~AFPVOL_OPEN;
2046 /* ------------------------- */
2047 static void deletevol(struct vol *vol)
2051 vol->v_flags &= ~AFPVOL_OPEN;
2052 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
2053 if ( (ovol->v_flags & AFPVOL_OPEN) ) {
2057 if ( ovol != NULL ) {
2058 /* Even if chdir fails, we can't say afp_closevol fails. */
2059 if ( chdir( ovol->v_path ) == 0 ) {
2060 curdir = ovol->v_dir;
2065 if (vol->v_deleted) {
2066 showvol(vol->v_name);
2073 /* ------------------------- */
2074 int afp_closevol(obj, ibuf, ibuflen, rbuf, rbuflen )
2076 char *ibuf, *rbuf _U_;
2077 int ibuflen _U_, *rbuflen;
2084 memcpy(&vid, ibuf, sizeof( vid ));
2085 if (NULL == ( vol = getvolbyvid( vid )) ) {
2086 return( AFPERR_PARAM );
2094 /* ------------------------- */
2095 struct vol *getvolbyvid(const u_int16_t vid )
2099 for ( vol = Volumes; vol; vol = vol->v_next ) {
2100 if ( vid == vol->v_vid ) {
2104 if ( vol == NULL || ( vol->v_flags & AFPVOL_OPEN ) == 0 ) {
2110 #endif /* FORCE_UIDGID */
2115 /* ------------------------ */
2116 static int ext_cmp_key(const void *key, const void *obj)
2118 const char *p = key;
2119 const struct extmap *em = obj;
2120 return strdiacasecmp(p, em->em_ext);
2122 struct extmap *getextmap(const char *path)
2127 if (NULL == ( p = strrchr( path, '.' )) ) {
2128 return( Defextmap );
2131 if (!*p || !Extmap_cnt) {
2132 return( Defextmap );
2134 em = bsearch(p, Extmap, Extmap_cnt, sizeof(struct extmap), ext_cmp_key);
2138 return( Defextmap );
2142 /* ------------------------- */
2143 struct extmap *getdefextmap(void)
2145 return( Defextmap );
2148 /* --------------------------
2149 poll if a volume is changed by other processes.
2151 int pollvoltime(obj)
2158 if (!(afp_version > 21 && obj->options.server_notif))
2161 if ( gettimeofday( &tv, 0 ) < 0 )
2164 for ( vol = Volumes; vol; vol = vol->v_next ) {
2165 if ( (vol->v_flags & AFPVOL_OPEN) && vol->v_mtime + 30 < tv.tv_sec) {
2166 if ( !stat( vol->v_path, &st ) && vol->v_mtime != st.st_mtime ) {
2167 vol->v_mtime = st.st_mtime;
2168 if (!obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED))
2177 /* ------------------------- */
2178 void setvoltime(obj, vol )
2184 /* just looking at vol->v_mtime is broken seriously since updates
2185 * from other users afpd processes never are seen.
2186 * This is not the most elegant solution (a shared memory between
2187 * the afpd processes would come closer)
2190 if ( gettimeofday( &tv, 0 ) < 0 ) {
2191 LOG(log_error, logtype_afpd, "setvoltime(%s): gettimeofday: %s", vol->v_path, strerror(errno) );
2194 if( utime( vol->v_path, NULL ) < 0 ) {
2195 /* write of time failed ... probably a read only filesys,
2196 * where no other users can interfere, so there's no issue here
2200 /* a little granularity */
2201 if (vol->v_mtime < tv.tv_sec) {
2202 vol->v_mtime = tv.tv_sec;
2203 /* or finder doesn't update free space */
2204 if (afp_version > 21 && obj->options.server_notif) {
2205 obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED);
2210 /* ------------------------- */
2211 int afp_getvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
2214 int ibuflen _U_, *rbuflen;
2217 u_int16_t vid, bitmap;
2220 memcpy(&vid, ibuf, sizeof( vid ));
2221 ibuf += sizeof( vid );
2222 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2223 bitmap = ntohs( bitmap );
2225 if (NULL == ( vol = getvolbyvid( vid )) ) {
2227 return( AFPERR_PARAM );
2230 return stat_vol(bitmap, vol, rbuf, rbuflen);
2233 /* ------------------------- */
2234 int afp_setvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
2236 char *ibuf, *rbuf _U_;
2237 int ibuflen _U_, *rbuflen;
2241 u_int16_t vid, bitmap;
2247 memcpy(&vid, ibuf, sizeof( vid ));
2248 ibuf += sizeof( vid );
2249 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2250 bitmap = ntohs( bitmap );
2251 ibuf += sizeof(bitmap);
2253 if (( vol = getvolbyvid( vid )) == NULL ) {
2254 return( AFPERR_PARAM );
2257 if ((vol->v_flags & AFPVOL_RO))
2258 return AFPERR_VLOCK;
2260 /* we can only set the backup date. */
2261 if (bitmap != (1 << VOLPBIT_BDATE))
2262 return AFPERR_BITMAP;
2264 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2265 if ( ad_open( vol->v_path, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR,
2268 return AFPERR_VLOCK;
2270 return AFPERR_ACCESS;
2273 memcpy(&aint, ibuf, sizeof(aint));
2274 ad_setdate(&ad, AD_DATE_BACKUP, aint);
2276 ad_close(&ad, ADFLAGS_HF);
2280 /* ------------------------- */
2281 int wincheck(const struct vol *vol, const char *path)
2285 if (!(vol->v_flags & AFPVOL_MSWINDOWS))
2288 /* empty paths are not allowed */
2289 if ((len = strlen(path)) == 0)
2292 /* leading or trailing whitespaces are not allowed, carriage returns
2293 * and probably other whitespace is okay, tabs are not allowed
2295 if ((path[0] == ' ') || (path[len-1] == ' '))
2298 /* certain characters are not allowed */
2299 if (strpbrk(path, MSWINDOWS_BADCHARS))
2302 /* everything else is okay */
2308 * precreate a folder
2309 * this is only intended for folders in the volume root
2310 * It will *not* work if the folder name contains extended characters
2312 static int create_special_folder (const struct vol *vol, const struct _special_folder *folder)
2321 p = (char *) malloc ( strlen(vol->v_path)+strlen(folder->name)+2);
2323 LOG(log_error, logtype_afpd,"malloc failed");
2327 q=strdup(folder->name);
2329 LOG(log_error, logtype_afpd,"malloc failed");
2333 strcpy(p, vol->v_path);
2338 if ((vol->v_casefold & AFPVOL_MTOUUPPER))
2340 else if ((vol->v_casefold & AFPVOL_MTOULOWER))
2346 if ( (ret = stat( p, &st )) < 0 ) {
2347 if (folder->precreate) {
2348 if (ad_mkdir(p, folder->mode)) {
2349 LOG(log_debug, logtype_afpd,"Creating '%s' failed in %s: %s", p, vol->v_path, strerror(errno));
2358 if ( !ret && folder->hide) {
2360 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2361 if (ad_open( p, vol_noadouble(vol) | ADFLAGS_HF|ADFLAGS_DIR,
2362 O_RDWR|O_CREAT, 0666, &ad) < 0) {
2367 if ((ad_get_HF_flags( &ad ) & O_CREAT) ) {
2368 if (ad_getentryoff(&ad, ADEID_NAME)) {
2369 ad_setentrylen( &ad, ADEID_NAME, strlen(folder->name));
2370 memcpy(ad_entry( &ad, ADEID_NAME ), folder->name,
2371 ad_getentrylen( &ad, ADEID_NAME ));
2375 ad_getattr(&ad, &attr);
2376 attr |= htons( ntohs( attr ) | ATTRBIT_INVISIBLE );
2377 ad_setattr(&ad, attr);
2379 /* do the same with the finder info */
2380 if (ad_entry(&ad, ADEID_FINDERI)) {
2381 memcpy(&attr, ad_entry(&ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF, sizeof(attr));
2382 attr |= htons(FINDERINFO_INVISIBLE);
2383 memcpy(ad_entry(&ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF,&attr, sizeof(attr));
2387 ad_close( &ad, ADFLAGS_HF );
2394 static void handle_special_folders (const struct vol * vol)
2396 const _special_folder *p = &special_folders[0];
2398 if ((vol->v_flags & AFPVOL_RO))
2401 for (; p->name != NULL; p++) {
2402 create_special_folder (vol, p);
2407 * Save the volume options to a file, used by
2409 * Writing the file everytime a volume is opened is
2410 * unnecessary, but it shouldn't hurt much.
2412 static int savevoloptions (const struct vol *vol)
2415 char item[MAXPATHLEN];
2419 const _vol_opt_name *op = &vol_opt_names[0];
2420 const _vol_opt_name *cf = &vol_opt_casefold[0];
2422 strlcpy (item, vol->v_path, sizeof(item));
2423 strlcat (item, "/.AppleDesktop/", sizeof(item));
2424 strlcat (item, VOLINFOFILE, sizeof(item));
2426 if ((fd = open( item, O_RDWR | O_CREAT , 0666)) <0 ) {
2427 LOG(log_debug, logtype_afpd,"Error opening %s: %s", item, strerror(errno));
2431 /* try to get a lock */
2433 lock.l_whence = SEEK_SET;
2435 lock.l_type = F_WRLCK;
2437 if (fcntl(fd, F_SETLK, &lock) < 0) {
2438 if (errno == EACCES || errno == EAGAIN) {
2439 /* ignore, other process already writing the file */
2442 LOG(log_error, logtype_cnid, "savevoloptions: cannot get lock: %s", strerror(errno));
2447 /* write volume options */
2448 snprintf(buf, sizeof(buf), "MAC_CHARSET:%s\n", vol->v_maccodepage);
2449 snprintf(item, sizeof(item), "VOL_CHARSET:%s\n", vol->v_volcodepage);
2450 strlcat(buf, item, sizeof(buf));
2452 switch (vol->v_adouble) {
2454 strlcat(buf, "ADOUBLE_VER:v1\n", sizeof(buf));
2457 strlcat(buf, "ADOUBLE_VER:v2\n", sizeof(buf));
2459 case AD_VERSION2_OSX:
2460 strlcat(buf, "ADOUBLE_VER:osx\n", sizeof(buf));
2462 case AD_VERSION1_SFM:
2463 strlcat(buf, "ADOUBLE_VER:sfm\n", sizeof(buf));
2467 strlcat(buf, "CNIDBACKEND:", sizeof(buf));
2468 strlcat(buf, vol->v_cnidscheme, sizeof(buf));
2469 strlcat(buf, "\n", sizeof(buf));
2471 strlcat(buf, "CNIDDBDHOST:", sizeof(buf));
2472 strlcat(buf, Cnid_srv, sizeof(buf));
2473 strlcat(buf, "\n", sizeof(buf));
2475 snprintf(item, sizeof(item), "CNIDDBDPORT:%u\n", Cnid_port);
2476 strlcat(buf, item, sizeof(buf));
2478 strcpy(item, "CNID_DBPATH:");
2480 strlcat(item, vol->v_dbpath, sizeof(item));
2482 strlcat(item, vol->v_path, sizeof(item));
2483 strlcat(item, "\n", sizeof(item));
2484 strlcat(buf, item, sizeof(buf));
2487 strcpy(item, "VOLUME_OPTS:");
2488 for (;op->name; op++) {
2489 if ( (vol->v_flags & op->option) ) {
2490 strlcat(item, op->name, sizeof(item));
2491 strlcat(item, " ", sizeof(item));
2494 strlcat(item, "\n", sizeof(item));
2495 strlcat(buf, item, sizeof(buf));
2497 /* casefold flags */
2498 strcpy(item, "VOLCASEFOLD:");
2499 for (;cf->name; cf++) {
2500 if ( (vol->v_casefold & cf->option) ) {
2501 strlcat(item, cf->name, sizeof(item));
2502 strlcat(item, " ", sizeof(item));
2505 strlcat(item, "\n", sizeof(item));
2506 strlcat(buf, item, sizeof(buf));
2508 if (strlen(buf) >= sizeof(buf)-1)
2509 LOG(log_debug, logtype_afpd,"Error writing .volinfo file: buffer too small, %s", buf);
2512 if (write( fd, buf, strlen(buf)) < 0 || ftruncate(fd, strlen(buf)) < 0 ) {
2513 LOG(log_debug, logtype_afpd,"Error writing .volinfo file: %s", strerror(errno));
2516 lock.l_type = F_UNLCK;
2517 fcntl(fd, F_SETLK, &lock);