2 * $Id: volume.c,v 1.51.2.7.2.31 2004-06-09 01:15:21 bfernhomberg 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"
57 extern int afprun(int root, char *cmd, int *outfd);
60 #define MIN(a, b) ((a) < (b) ? (a) : (b))
63 #ifndef NO_LARGE_VOL_SUPPORT
64 #if BYTE_ORDER == BIG_ENDIAN
67 #else /* BYTE_ORDER == BIG_ENDIAN */
68 #define hton64(x) ((u_int64_t) (htonl(((x) >> 32) & 0xffffffffLL)) | \
69 (u_int64_t) ((htonl(x) & 0xffffffffLL) << 32))
70 #define ntoh64(x) (hton64(x))
71 #endif /* BYTE_ORDER == BIG_ENDIAN */
72 #endif /* ! NO_LARGE_VOL_SUPPORT */
74 static struct vol *Volumes = NULL;
75 static u_int16_t lastvid = 0;
76 static char *Trash = "\02\024Network Trash Folder";
78 static struct extmap *Extmap = NULL, *Defextmap = NULL;
79 static int Extmap_cnt;
80 static void free_extmap(void);
82 #define VOLOPT_ALLOW 0 /* user allow list */
83 #define VOLOPT_DENY 1 /* user deny list */
84 #define VOLOPT_RWLIST 2 /* user rw list */
85 #define VOLOPT_ROLIST 3 /* user ro list */
86 #define VOLOPT_PASSWORD 4 /* volume password */
87 #define VOLOPT_CASEFOLD 5 /* character case mangling */
88 #define VOLOPT_FLAGS 6 /* various flags */
89 #define VOLOPT_DBPATH 7 /* path to database */
90 #define VOLOPT_MAPCHARS 8 /* does mtou and utom mappings. syntax:
91 m and u can be double-byte hex
96 !u -> make u illegal always
97 ~u -> make u illegal only as the first
98 part of a double-byte character.
100 #define VOLOPT_VETO 10 /* list of veto filespec */
101 #define VOLOPT_PREEXEC 11 /* preexec command */
102 #define VOLOPT_ROOTPREEXEC 12 /* root preexec command */
104 #define VOLOPT_POSTEXEC 13 /* postexec command */
105 #define VOLOPT_ROOTPOSTEXEC 14 /* root postexec command */
107 #define VOLOPT_ENCODING 15 /* mac encoding (pre OSX)*/
108 #define VOLOPT_MACCHARSET 16
109 #define VOLOPT_CNIDSCHEME 17
110 #define VOLOPT_ADOUBLE 18 /* adouble version */
115 #define VOLOPT_FORCEUID 19 /* force uid for username x */
116 #define VOLOPT_FORCEGID 20 /* force gid for group x */
117 #define VOLOPT_UMASK 21
119 #define VOLOPT_UMASK 19
120 #endif /* FORCE_UIDGID */
122 #define VOLOPT_MAX (VOLOPT_UMASK +1)
124 #define VOLOPT_NUM (VOLOPT_MAX + 1)
127 #define VOLOPT_DEFAULT ":DEFAULT:"
128 #define VOLOPT_DEFAULT_LEN 9
134 typedef struct _special_folder {
141 static const _special_folder special_folders[] = {
142 {"Network Trash Folder", 1, 0777, 1},
143 {"Temporary Items", 1, 0777, 1},
144 {".AppleDesktop", 1, 0777, 0},
146 {"TheFindByContentFolder", 0, 0, 1},
147 {"TheVolumeSettingsFolder", 0, 0, 1},
151 typedef struct _volopt_name {
152 const u_int32_t option;
156 static const _vol_opt_name vol_opt_names[] = {
157 {AFPVOL_A2VOL, "PRODOS"}, /* prodos volume */
158 {AFPVOL_CRLF, "CRLF"}, /* cr/lf translation */
159 {AFPVOL_NOADOUBLE, "NOADOUBLE"}, /* don't create .AppleDouble by default */
160 {AFPVOL_RO, "READONLY"}, /* read-only volume */
161 {AFPVOL_MSWINDOWS, "MSWINDOWS"}, /* deal with ms-windows yuckiness. this is going away. */
162 {AFPVOL_NOHEX, "NOHEX"}, /* don't do :hex translation */
163 {AFPVOL_USEDOTS, "USEDOTS"}, /* use real dots */
164 {AFPVOL_LIMITSIZE, "LIMITSIZE"}, /* limit size for older macs */
165 {AFPVOL_MAPASCII, "MAPASCII"}, /* map the ascii range as well */
166 {AFPVOL_DROPBOX, "DROPBOX"}, /* dropkludge dropbox support */
167 {AFPVOL_NOFILEID, "NOFILEID"}, /* don't advertise createid resolveid and deleteid calls */
168 {AFPVOL_NOSTAT, "NOSTAT"}, /* advertise the volume even if we can't stat() it
169 * maybe because it will be mounted later in preexec */
170 {AFPVOL_UNIX_PRIV, "UNIXPRIV"}, /* support unix privileges */
171 {AFPVOL_NODEV, "NODEV"}, /* always use 0 for device number in cnid calls */
175 static const _vol_opt_name vol_opt_casefold[] = {
176 {AFPVOL_MTOUUPPER, "MTOULOWER"},
177 {AFPVOL_MTOULOWER, "MTOULOWER"},
178 {AFPVOL_UTOMUPPER, "UTOMUPPER"},
179 {AFPVOL_UTOMLOWER, "UTOMLOWER"},
183 static void handle_special_folders (const struct vol *);
184 static int savevoloptions (const struct vol *);
186 static __inline__ void volfree(struct vol_option *options,
187 const struct vol_option *save)
192 for (i = 0; i < VOLOPT_MAX; i++) {
193 if (options[i].c_value && (options[i].c_value != save[i].c_value))
194 free(options[i].c_value);
197 for (i = 0; i < VOLOPT_MAX; i++) {
198 if (options[i].c_value)
199 free(options[i].c_value);
205 /* handle variable substitutions. here's what we understand:
206 * $b -> basename of path
207 * $c -> client ip/appletalk address
208 * $d -> volume pathname on server
209 * $f -> full name (whatever's in the gecos field)
212 * $i -> client ip/appletalk address without port
213 * $s -> server name (hostname if it doesn't exist)
214 * $u -> username (guest is usually nobody)
215 * $v -> volume name or basename if null
216 * $z -> zone (may not exist)
221 #define is_var(a, b) (strncmp((a), (b), 2) == 0)
223 static char *volxlate(AFPObj *obj, char *dest, size_t destlen,
224 char *src, struct passwd *pwd, char *path, char *volname)
234 dest = calloc(destlen +1, 1);
240 strlcpy(dest, src, destlen +1);
241 if ((p = strchr(src, '$')) == NULL) /* nothing to do */
244 /* first part of the path. just forward to the next variable. */
245 len = MIN(p - src, destlen);
251 while (p && destlen > 0) {
252 /* now figure out what the variable is */
254 if (is_var(p, "$b")) {
256 if ((q = strrchr(path, '/')) == NULL)
258 else if (*(q + 1) != '\0')
261 } else if (is_var(p, "$c")) {
262 if (obj->proto == AFPPROTO_ASP) {
263 ASP asp = obj->handle;
265 len = sprintf(dest, "%u.%u", ntohs(asp->asp_sat.sat_addr.s_net),
266 asp->asp_sat.sat_addr.s_node);
270 } else if (obj->proto == AFPPROTO_DSI) {
271 DSI *dsi = obj->handle;
273 len = sprintf(dest, "%s:%u", inet_ntoa(dsi->client.sin_addr),
274 ntohs(dsi->client.sin_port));
278 } else if (is_var(p, "$d")) {
280 } else if (is_var(p, "$f")) {
281 if ((q = strchr(pwd->pw_gecos, ',')))
284 } else if (is_var(p, "$g")) {
285 struct group *grp = getgrgid(pwd->pw_gid);
288 } else if (is_var(p, "$h")) {
289 q = obj->options.hostname;
290 } else if (is_var(p, "$i")) {
291 if (obj->proto == AFPPROTO_ASP) {
292 ASP asp = obj->handle;
294 len = sprintf(dest, "%u", ntohs(asp->asp_sat.sat_addr.s_net));
298 } else if (obj->proto == AFPPROTO_DSI) {
299 DSI *dsi = obj->handle;
301 q = inet_ntoa(dsi->client.sin_addr);
303 } else if (is_var(p, "$s")) {
306 else if (obj->options.server) {
307 q = obj->options.server;
309 q = obj->options.hostname;
310 } else if (is_var(p, "$u")) {
312 } else if (is_var(p, "$v")) {
317 if ((q = strrchr(path, '/')) == NULL)
319 else if (*(q + 1) != '\0')
322 } else if (is_var(p, "$z")) {
324 } else if (is_var(p, "$$")) {
329 /* copy the stuff over. if we don't understand something that we
330 * should, just skip it over. */
332 len = MIN(p == q ? 2 : strlen(q), destlen);
333 strncpy(dest, q, len);
338 /* stuff up to next $ */
340 p = strchr(src, '$');
341 len = p ? MIN(p - src, destlen) : destlen;
343 strncpy(dest, src, len);
351 /* to make sure that val is valid, make sure to select an opt that
353 static int optionok(const char *buf, const char *opt, const char *val)
355 if (!strstr(buf,opt))
363 /* -------------------- */
364 static void setoption(struct vol_option *options, struct vol_option *save, int opt, const char *val)
366 if (options[opt].c_value && (!save || options[opt].c_value != save[opt].c_value))
367 free(options[opt].c_value);
368 options[opt].c_value = strdup(val + 1);
371 /* ------------------------------------------
372 handle all the options. tmp can't be NULL. */
373 static void volset(struct vol_option *options, struct vol_option *save,
374 char *volname, int vlen,
379 val = strchr(tmp, ':');
381 /* we'll assume it's a volume name. */
382 strncpy(volname, tmp, vlen);
387 LOG(log_debug, logtype_afpd, "Parsing volset %s", val);
389 if (optionok(tmp, "allow:", val)) {
390 setoption(options, save, VOLOPT_ALLOW, val);
392 } else if (optionok(tmp, "deny:", val)) {
393 setoption(options, save, VOLOPT_DENY, val);
395 } else if (optionok(tmp, "rwlist:", val)) {
396 setoption(options, save, VOLOPT_RWLIST, val);
398 } else if (optionok(tmp, "rolist:", val)) {
399 setoption(options, save, VOLOPT_ROLIST, val);
401 } else if (optionok(tmp, "codepage:", val)) {
402 LOG (log_error, logtype_afpd, "The old codepage system has been removed. Please make sure to read the documentation !!!!");
403 /* Make sure we don't screw anything */
405 } else if (optionok(tmp, "volcharset:", val)) {
406 setoption(options, save, VOLOPT_ENCODING, val);
407 } else if (optionok(tmp, "maccharset:", val)) {
408 setoption(options, save, VOLOPT_MACCHARSET, val);
409 } else if (optionok(tmp, "veto:", val)) {
410 setoption(options, save, VOLOPT_VETO, val);
411 } else if (optionok(tmp, "cnidscheme:", val)) {
412 setoption(options, save, VOLOPT_CNIDSCHEME, val);
413 } else if (optionok(tmp, "casefold:", val)) {
414 if (strcasecmp(val + 1, "tolower") == 0)
415 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMLOWER;
416 else if (strcasecmp(val + 1, "toupper") == 0)
417 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMUPPER;
418 else if (strcasecmp(val + 1, "xlatelower") == 0)
419 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UUPPERMLOWER;
420 else if (strcasecmp(val + 1, "xlateupper") == 0)
421 options[VOLOPT_CASEFOLD].i_value = AFPVOL_ULOWERMUPPER;
422 } else if (optionok(tmp, "adouble:", val)) {
423 if (strcasecmp(val + 1, "v1") == 0)
424 options[VOLOPT_ADOUBLE].i_value = AD_VERSION1;
425 #if AD_VERSION == AD_VERSION2
426 else if (strcasecmp(val + 1, "v2") == 0)
427 options[VOLOPT_ADOUBLE].i_value = AD_VERSION2;
428 else if (strcasecmp(val + 1, "osx") == 0)
429 options[VOLOPT_ADOUBLE].i_value = AD_VERSION2_OSX;
431 } else if (optionok(tmp, "options:", val)) {
434 if ((p = strtok(val + 1, ",")) == NULL) /* nothing */
438 if (strcasecmp(p, "prodos") == 0)
439 options[VOLOPT_FLAGS].i_value |= AFPVOL_A2VOL;
440 else if (strcasecmp(p, "mswindows") == 0) {
441 options[VOLOPT_FLAGS].i_value |= AFPVOL_MSWINDOWS | AFPVOL_USEDOTS;
442 } else if (strcasecmp(p, "crlf") == 0)
443 options[VOLOPT_FLAGS].i_value |= AFPVOL_CRLF;
444 else if (strcasecmp(p, "noadouble") == 0)
445 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOADOUBLE;
446 else if (strcasecmp(p, "ro") == 0)
447 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
448 else if (strcasecmp(p, "nohex") == 0)
449 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOHEX;
450 else if (strcasecmp(p, "usedots") == 0)
451 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS;
452 else if (strcasecmp(p, "limitsize") == 0)
453 options[VOLOPT_FLAGS].i_value |= AFPVOL_LIMITSIZE;
454 /* support for either "dropbox" or "dropkludge" */
455 else if (strcasecmp(p, "dropbox") == 0)
456 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
457 else if (strcasecmp(p, "dropkludge") == 0)
458 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
459 else if (strcasecmp(p, "nofileid") == 0)
460 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOFILEID;
461 else if (strcasecmp(p, "nostat") == 0)
462 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOSTAT;
463 else if (strcasecmp(p, "preexec_close") == 0)
464 options[VOLOPT_PREEXEC].i_value = 1;
465 else if (strcasecmp(p, "root_preexec_close") == 0)
466 options[VOLOPT_ROOTPREEXEC].i_value = 1;
467 else if (strcasecmp(p, "upriv") == 0)
468 options[VOLOPT_FLAGS].i_value |= AFPVOL_UNIX_PRIV;
469 else if (strcasecmp(p, "nodev") == 0)
470 options[VOLOPT_FLAGS].i_value |= AFPVOL_NODEV;
472 p = strtok(NULL, ",");
475 } else if (optionok(tmp, "dbpath:", val)) {
476 setoption(options, save, VOLOPT_DBPATH, val);
478 } else if (optionok(tmp, "umask:", val)) {
479 options[VOLOPT_UMASK].i_value = (int)strtol(val, (char **)NULL, 8);
480 } else if (optionok(tmp, "mapchars:",val)) {
481 setoption(options, save, VOLOPT_MAPCHARS, val);
483 } else if (optionok(tmp, "password:", val)) {
484 setoption(options, save, VOLOPT_PASSWORD, val);
488 /* this code allows forced uid/gid per volume settings */
489 } else if (optionok(tmp, "forceuid:", val)) {
490 setoption(options, save, VOLOPT_FORCEUID, val);
491 } else if (optionok(tmp, "forcegid:", val)) {
492 setoption(options, save, VOLOPT_FORCEGID, val);
494 #endif /* FORCE_UIDGID */
495 } else if (optionok(tmp, "root_preexec:", val)) {
496 setoption(options, save, VOLOPT_ROOTPREEXEC, val);
498 } else if (optionok(tmp, "preexec:", val)) {
499 setoption(options, save, VOLOPT_PREEXEC, val);
501 } else if (optionok(tmp, "root_postexec:", val)) {
502 setoption(options, save, VOLOPT_ROOTPOSTEXEC, val);
504 } else if (optionok(tmp, "postexec:", val)) {
505 setoption(options, save, VOLOPT_POSTEXEC, val);
508 /* ignore unknown options */
509 LOG(log_debug, logtype_afpd, "ignoring unknown volume option: %s", tmp);
514 /* ----------------- */
515 static void showvol(const ucs2_t *name)
518 for ( volume = Volumes; volume; volume = volume->v_next ) {
519 if (volume->v_hide && !strcasecmp_w( volume->v_name, name ) ) {
527 * FIXME should be define elsewhere
529 static int validupath_adouble(const struct vol *vol, const char *name)
531 return (vol->v_flags & AFPVOL_USEDOTS) ? strncasecmp(name,".Apple", 6) && strcasecmp(name, ".Parent")
535 /* ----------------- */
536 static int validupath_osx(const struct vol *vol, const char *name)
538 return strncasecmp(name,".Apple", 6) && strncasecmp(name,"._", 2);
541 /* ---------------- */
542 static void initvoladouble(struct vol *vol)
544 if (vol->v_adouble == AD_VERSION2_OSX) {
545 vol->validupath = validupath_osx;
546 vol->ad_path = ad_path_osx;
549 vol->validupath = validupath_adouble;
550 vol->ad_path = ad_path;
554 /* ------------------------------- */
555 static int creatvol(AFPObj *obj, struct passwd *pwd,
556 char *path, char *name,
557 struct vol_option *options,
558 const int user /* user defined volume */
566 if ( name == NULL || *name == '\0' ) {
567 if ((name = strrchr( path, '/' )) == NULL) {
568 return -1; /* Obviously not a fully qualified path */
571 /* if you wish to share /, you need to specify a name. */
576 vlen = strlen( name );
577 if ( vlen > AFPVOL_NAMELEN ) {
578 vlen = AFPVOL_NAMELEN;
579 name[AFPVOL_NAMELEN] = '\0';
582 /* convert name to UCS2 first */
583 if ( 0 >= ( vlen = convert_string(obj->options.unixcharset, CH_UCS2, name, vlen, tmpname, 512)) )
586 for ( volume = Volumes; volume; volume = volume->v_next ) {
587 if ( strcasecmp_w( volume->v_name, tmpname ) == 0 ) {
588 if (volume->v_deleted) {
592 return -1; /* Won't be able to access it, anyway... */
598 if (!( volume = (struct vol *)calloc(1, sizeof( struct vol ))) ) {
599 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
602 if ( NULL == ( volume->v_name = strdup_w(tmpname))) {
603 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
607 if (!( volume->v_path = (char *)malloc( strlen( path ) + 1 )) ) {
608 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
609 free(volume->v_name);
613 volume->v_hide = hide;
614 strcpy( volume->v_path, path );
618 #endif /* __svr4__ */
619 /* os X start at 1 and use network order ie. 1 2 3 */
620 volume->v_vid = ++lastvid;
621 volume->v_vid = htons(volume->v_vid);
625 /* should we casefold? */
626 volume->v_casefold = options[VOLOPT_CASEFOLD].i_value;
628 /* shift in some flags */
629 volume->v_flags = options[VOLOPT_FLAGS].i_value;
631 if (options[VOLOPT_PASSWORD].c_value)
632 volume->v_password = strdup(options[VOLOPT_PASSWORD].c_value);
634 if (options[VOLOPT_VETO].c_value)
635 volume->v_veto = strdup(options[VOLOPT_VETO].c_value);
637 if (options[VOLOPT_ENCODING].c_value)
638 volume->v_volcodepage = strdup(options[VOLOPT_ENCODING].c_value);
640 if (options[VOLOPT_MACCHARSET].c_value)
641 volume->v_maccodepage = strdup(options[VOLOPT_MACCHARSET].c_value);
643 if (options[VOLOPT_DBPATH].c_value)
644 volume->v_dbpath = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_DBPATH].c_value, pwd, path, name);
646 if (options[VOLOPT_CNIDSCHEME].c_value)
647 volume->v_cnidscheme = strdup(options[VOLOPT_CNIDSCHEME].c_value);
649 if (options[VOLOPT_UMASK].i_value)
650 volume->v_umask = (mode_t)options[VOLOPT_UMASK].i_value;
652 if (options[VOLOPT_ADOUBLE].i_value)
653 volume->v_adouble = options[VOLOPT_ADOUBLE].i_value;
655 volume->v_adouble = AD_VERSION;
656 initvoladouble(volume);
658 if (options[VOLOPT_FORCEUID].c_value) {
659 volume->v_forceuid = strdup(options[VOLOPT_FORCEUID].c_value);
661 volume->v_forceuid = NULL; /* set as null so as to return 0 later on */
664 if (options[VOLOPT_FORCEGID].c_value) {
665 volume->v_forcegid = strdup(options[VOLOPT_FORCEGID].c_value);
667 volume->v_forcegid = NULL; /* set as null so as to return 0 later on */
671 if (options[VOLOPT_PREEXEC].c_value)
672 volume->v_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_PREEXEC].c_value, pwd, path, name);
673 volume->v_preexec_close = options[VOLOPT_PREEXEC].i_value;
675 if (options[VOLOPT_POSTEXEC].c_value)
676 volume->v_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_POSTEXEC].c_value, pwd, path, name);
678 if (options[VOLOPT_ROOTPREEXEC].c_value)
679 volume->v_root_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPREEXEC].c_value, pwd, path, name);
680 volume->v_root_preexec_close = options[VOLOPT_ROOTPREEXEC].i_value;
682 if (options[VOLOPT_ROOTPOSTEXEC].c_value)
683 volume->v_root_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPOSTEXEC].c_value, pwd, path, name);
687 volume->v_next = Volumes;
692 /* ---------------- */
693 static char *myfgets( buf, size, fp )
702 while ((EOF != ( c = getc( fp )) ) && ( size > 0 )) {
703 if ( c == '\n' || c == '\r' ) {
721 /* check access list. this function wants something of the following
723 * @group,name,name2,@group2,name3
725 * a NULL argument allows everybody to have access.
726 * we return three things:
728 * 0: list exists, but name isn't in it
732 #ifndef NO_REAL_USER_NAME
733 /* authentication is case insensitive
734 * FIXME should we do the same with group name?
736 #define access_strcmp strcasecmp
739 #define access_strcmp strcmp
743 static int accessvol(args, name)
747 char buf[MAXPATHLEN + 1], *p;
753 strlcpy(buf, args, sizeof(buf));
754 if ((p = strtok(buf, ",")) == NULL) /* nothing, return okay */
758 if (*p == '@') { /* it's a group */
759 if ((gr = getgrnam(p + 1)) && gmem(gr->gr_gid))
761 } else if (access_strcmp(p, name) == 0) /* it's a user name */
763 p = strtok(NULL, ",");
769 static void setextmap( ext, type, creator, user)
770 char *ext, *type, *creator;
776 if (Extmap == NULL) {
777 if (( Extmap = calloc(1, sizeof( struct extmap ))) == NULL ) {
778 LOG(log_error, logtype_afpd, "setextmap: calloc: %s", strerror(errno) );
783 for ( em = Extmap, cnt = 0; em->em_ext; em++, cnt++) {
784 if ( (strdiacasecmp( em->em_ext, ext )) == 0 ) {
789 if ( em->em_ext == NULL ) {
790 if (!(Extmap = realloc( Extmap, sizeof( struct extmap ) * (cnt +2))) ) {
791 LOG(log_error, logtype_afpd, "setextmap: realloc: %s", strerror(errno) );
794 (Extmap +cnt +1)->em_ext = NULL;
796 } else if ( !user ) {
802 if (!(em->em_ext = strdup( ext))) {
803 LOG(log_error, logtype_afpd, "setextmap: strdup: %s", strerror(errno) );
807 if ( *type == '\0' ) {
808 memcpy(em->em_type, "????", sizeof( em->em_type ));
810 memcpy(em->em_type, type, sizeof( em->em_type ));
812 if ( *creator == '\0' ) {
813 memcpy(em->em_creator, "UNIX", sizeof( em->em_creator ));
815 memcpy(em->em_creator, creator, sizeof( em->em_creator ));
819 /* -------------------------- */
820 static int extmap_cmp(const void *map1, const void *map2)
822 const struct extmap *em1 = map1;
823 const struct extmap *em2 = map2;
824 return strdiacasecmp(em1->em_ext, em2->em_ext);
827 static void sortextmap( void)
832 if ((em = Extmap) == NULL) {
840 qsort(Extmap, Extmap_cnt, sizeof(struct extmap), extmap_cmp);
845 /* ----------------------
847 static void free_extmap( void)
852 for ( em = Extmap; em->em_ext; em++) {
862 /* ----------------------
864 static int volfile_changed(struct afp_volume_name *p)
874 if (!stat( name, &st) && st.st_mtime > p->mtime) {
875 p->mtime = st.st_mtime;
881 /* ----------------------
882 * Read a volume configuration file and add the volumes contained within to
883 * the global volume list. If p2 is non-NULL, the file that is opened is
886 * Lines that begin with # and blank lines are ignored.
887 * Volume lines are of the form:
888 * <unix path> [<volume name>] [allow:<user>,<@group>,...] \
889 * [codepage:<file>] [casefold:<num>]
890 * <extension> TYPE [CREATOR]
892 static int readvolfile(obj, p1, p2, user, pwent)
894 struct afp_volume_name *p1;
897 struct passwd *pwent;
900 char path[ MAXPATHLEN + 1], tmp[ MAXPATHLEN + 1],
901 volname[ AFPVOL_NAMELEN + 1 ], buf[ BUFSIZ ],
902 type[ 5 ], creator[ 5 ];
905 struct vol_option options[VOLOPT_NUM], save_options[VOLOPT_NUM];
913 strcpy( path, p1->name );
920 p1->full_name = strdup(path);
923 if (NULL == ( fp = fopen( path, "r" )) ) {
927 if (fd != -1 && !fstat( fd, &st) ) {
928 p1->mtime = st.st_mtime;
931 memset(save_options, 0, sizeof(save_options));
932 while ( myfgets( buf, sizeof( buf ), fp ) != NULL ) {
933 initline( strlen( buf ), buf );
934 parseline( sizeof( path ) - 1, path );
941 /* change the default options for this file */
942 if (strncmp(path, VOLOPT_DEFAULT, VOLOPT_DEFAULT_LEN) == 0) {
944 for (i = 0; i < VOLOPT_NUM; i++) {
945 if (parseline( sizeof( path ) - VOLOPT_DEFAULT_LEN - 1,
946 path + VOLOPT_DEFAULT_LEN) < 0)
948 volset(save_options, NULL, tmp, sizeof(tmp) - 1,
949 path + VOLOPT_DEFAULT_LEN);
955 if (( p = strchr( path, '/' )) != NULL ) {
963 if ( u == NULL || *u == '\0' || ( pw = getpwnam( u )) == NULL ) {
966 strcpy( tmp, pw->pw_dir );
967 if ( p != NULL && *p != '\0' ) {
971 /* Tag a user's home directory with their umask. Note, this will
972 * be overwritten if the user actually specifies a umask: option
973 * for a '~' volume. */
974 save_options[VOLOPT_UMASK].i_value = obj->options.save_mask;
978 /* send path through variable substitution */
979 if (*path != '~') /* need to copy path to tmp */
982 pwent = getpwnam(obj->username);
983 volxlate(obj, path, sizeof(path) - 1, tmp, pwent, NULL, NULL);
985 /* this is sort of braindead. basically, i want to be
986 * able to specify things in any order, but i don't want to
987 * re-write everything.
989 * currently we have options:
997 * options:prodos,crlf,noadouble,ro...
1002 * namemask:x,y,!z (not implemented yet)
1004 memcpy(options, save_options, sizeof(options));
1007 /* read in up to VOLOP_NUM possible options */
1008 for (i = 0; i < VOLOPT_NUM; i++) {
1009 if (parseline( sizeof( tmp ) - 1, tmp ) < 0)
1012 volset(options, save_options, volname, sizeof(volname) - 1, tmp);
1015 /* check allow/deny lists:
1016 allow -> either no list (-1), or in list (1)
1017 deny -> either no list (-1), or not in list (0) */
1018 if (accessvol(options[VOLOPT_ALLOW].c_value, obj->username) &&
1019 (accessvol(options[VOLOPT_DENY].c_value, obj->username) < 1)) {
1021 /* handle read-only behaviour. semantics:
1022 * 1) neither the rolist nor the rwlist exist -> rw
1023 * 2) rolist exists -> ro if user is in it.
1024 * 3) rwlist exists -> ro unless user is in it. */
1025 if (((options[VOLOPT_FLAGS].i_value & AFPVOL_RO) == 0) &&
1026 ((accessvol(options[VOLOPT_ROLIST].c_value,
1027 obj->username) == 1) ||
1028 !accessvol(options[VOLOPT_RWLIST].c_value,
1030 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
1032 /* do variable substitution for volname */
1033 volxlate(obj, tmp, sizeof(tmp) - 1, volname, pwent, path, NULL);
1034 creatvol(obj, pwent, path, tmp, options, p2 != NULL);
1036 volfree(options, save_options);
1040 parseline( sizeof( type ) - 1, type );
1041 parseline( sizeof( creator ) - 1, creator );
1042 setextmap( path, type, creator, user);
1049 volfree(save_options, NULL);
1051 if ( fclose( fp ) != 0 ) {
1052 LOG(log_error, logtype_afpd, "readvolfile: fclose: %s", strerror(errno) );
1058 /* ------------------------------- */
1059 static void volume_free(struct vol *vol)
1064 free(vol->v_password);
1066 free(vol->v_volcodepage);
1067 free(vol->v_maccodepage);
1068 free(vol->v_cnidscheme);
1069 free(vol->v_dbpath);
1072 free(vol->v_forceuid);
1073 free(vol->v_forcegid);
1074 #endif /* FORCE_UIDGID */
1077 /* ------------------------------- */
1078 static void free_volumes(void )
1081 struct vol *nvol, *ovol;
1083 for ( vol = Volumes; vol; vol = vol->v_next ) {
1084 if (( vol->v_flags & AFPVOL_OPEN ) ) {
1091 for ( vol = Volumes, ovol = NULL; vol; vol = nvol) {
1094 if (vol->v_name == NULL) {
1095 if (Volumes == vol) {
1100 ovol->v_next = nvol;
1110 /* ------------------------------- */
1111 static void volume_unlink(struct vol *volume)
1113 struct vol *vol, *ovol, *nvol;
1115 if (volume == Volumes) {
1116 Volumes = Volumes->v_next;
1119 for ( vol = Volumes->v_next, ovol = Volumes; vol; vol = nvol) {
1122 if (vol == volume) {
1123 ovol->v_next = nvol;
1132 static int getvolspace( vol, bfree, btotal, xbfree, xbtotal, bsize )
1134 u_int32_t *bfree, *btotal, *bsize;
1135 VolSpace *xbfree, *xbtotal;
1139 #ifndef NO_QUOTA_SUPPORT
1140 VolSpace qfree, qtotal;
1143 spaceflag = AFPVOL_GVSMASK & vol->v_flags;
1144 /* report up to 2GB if afp version is < 2.2 (4GB if not) */
1145 maxsize = (vol->v_flags & AFPVOL_A2VOL) ? 0x01fffe00 :
1146 (((afp_version < 22) || (vol->v_flags & AFPVOL_LIMITSIZE))
1147 ? 0x7fffffffL : 0xffffffffL);
1150 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_AFSGVS ) {
1151 if ( afs_getvolspace( vol, xbfree, xbtotal, bsize ) == AFP_OK ) {
1152 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_AFSGVS;
1153 goto getvolspace_done;
1158 if (( rc = ustatfs_getvolspace( vol, xbfree, xbtotal,
1159 bsize)) != AFP_OK ) {
1163 #define min(a,b) ((a)<(b)?(a):(b))
1164 #ifndef NO_QUOTA_SUPPORT
1165 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_UQUOTA ) {
1166 if ( uquota_getvolspace( vol, &qfree, &qtotal, *bsize ) == AFP_OK ) {
1167 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_UQUOTA;
1168 *xbfree = min(*xbfree, qfree);
1169 *xbtotal = min( *xbtotal, qtotal);
1170 goto getvolspace_done;
1174 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_USTATFS;
1177 *bfree = min( *xbfree, maxsize);
1178 *btotal = min( *xbtotal, maxsize);
1182 /* -----------------------
1183 * set volume creation date
1184 * avoid duplicate, well at least it tries
1186 static void vol_setdate(u_int16_t id, struct adouble *adp, time_t date)
1189 struct vol *vol = Volumes;
1191 for ( volume = Volumes; volume; volume = volume->v_next ) {
1192 if (volume->v_vid == id) {
1195 else if (AD_DATE_FROM_UNIX(date) == volume->v_ctime) {
1197 volume = Volumes; /* restart */
1200 vol->v_ctime = AD_DATE_FROM_UNIX(date);
1201 ad_setdate(adp, AD_DATE_CREATE | AD_DATE_UNIX, date);
1204 /* ----------------------- */
1205 static int getvolparams( bitmap, vol, st, buf, buflen )
1213 int bit = 0, isad = 1;
1216 u_int32_t bfree, btotal, bsize;
1217 VolSpace xbfree, xbtotal; /* extended bytes */
1218 char *data, *nameoff = NULL;
1221 /* courtesy of jallison@whistle.com:
1222 * For MacOS8.x support we need to create the
1223 * .Parent file here if it doesn't exist. */
1225 ad_init(&ad, vol->v_adouble);
1226 if ( ad_open( vol->v_path, vol_noadouble(vol) |
1227 ADFLAGS_HF|ADFLAGS_DIR, O_RDWR | O_CREAT,
1230 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1232 } else if (ad_get_HF_flags( &ad ) & O_CREAT) {
1233 slash = strrchr( vol->v_path, '/' );
1237 slash = vol->v_path;
1238 if (ad_getentryoff(&ad, ADEID_NAME)) {
1239 ad_setentrylen( &ad, ADEID_NAME, strlen( slash ));
1240 memcpy(ad_entry( &ad, ADEID_NAME ), slash,
1241 ad_getentrylen( &ad, ADEID_NAME ));
1243 vol_setdate(vol->v_vid, &ad, st->st_mtime);
1244 ad_flush(&ad, ADFLAGS_HF);
1247 if (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0)
1248 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1250 vol->v_ctime = aint;
1253 if (( bitmap & ( (1<<VOLPBIT_BFREE)|(1<<VOLPBIT_BTOTAL) |
1254 (1<<VOLPBIT_XBFREE)|(1<<VOLPBIT_XBTOTAL) |
1255 (1<<VOLPBIT_BSIZE)) ) != 0 ) {
1256 if ( getvolspace( vol, &bfree, &btotal, &xbfree, &xbtotal,
1259 ad_close( &ad, ADFLAGS_HF );
1261 return( AFPERR_PARAM );
1266 while ( bitmap != 0 ) {
1267 while (( bitmap & 1 ) == 0 ) {
1275 if (0 == (vol->v_flags & AFPVOL_NOFILEID) && vol->v_cdb != NULL &&
1276 (vol->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1277 ashort = VOLPBIT_ATTR_FILEID;
1279 /* check for read-only.
1280 * NOTE: we don't actually set the read-only flag unless
1281 * it's passed in that way as it's possible to mount
1282 * a read-write filesystem under a read-only one. */
1283 if ((vol->v_flags & AFPVOL_RO) ||
1284 ((utime(vol->v_path, NULL) < 0) && (errno == EROFS))) {
1285 ashort |= VOLPBIT_ATTR_RO;
1287 ashort |= VOLPBIT_ATTR_CATSEARCH;
1288 if (afp_version >= 30) {
1289 ashort |= VOLPBIT_ATTR_UTF8;
1290 if (vol->v_flags & AFPVOL_UNIX_PRIV)
1291 ashort |= VOLPBIT_ATTR_UNIXPRIV;
1293 ashort = htons(ashort);
1294 memcpy(data, &ashort, sizeof( ashort ));
1295 data += sizeof( ashort );
1299 ashort = htons( AFPVOLSIG_DEFAULT );
1300 memcpy(data, &ashort, sizeof( ashort ));
1301 data += sizeof( ashort );
1304 case VOLPBIT_CDATE :
1305 aint = vol->v_ctime;
1306 memcpy(data, &aint, sizeof( aint ));
1307 data += sizeof( aint );
1310 case VOLPBIT_MDATE :
1311 if ( st->st_mtime > vol->v_mtime ) {
1312 vol->v_mtime = st->st_mtime;
1314 aint = AD_DATE_FROM_UNIX(vol->v_mtime);
1315 memcpy(data, &aint, sizeof( aint ));
1316 data += sizeof( aint );
1319 case VOLPBIT_BDATE :
1320 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1321 aint = AD_DATE_START;
1322 memcpy(data, &aint, sizeof( aint ));
1323 data += sizeof( aint );
1327 memcpy(data, &vol->v_vid, sizeof( vol->v_vid ));
1328 data += sizeof( vol->v_vid );
1331 case VOLPBIT_BFREE :
1332 bfree = htonl( bfree );
1333 memcpy(data, &bfree, sizeof( bfree ));
1334 data += sizeof( bfree );
1337 case VOLPBIT_BTOTAL :
1338 btotal = htonl( btotal );
1339 memcpy(data, &btotal, sizeof( btotal ));
1340 data += sizeof( btotal );
1343 #ifndef NO_LARGE_VOL_SUPPORT
1344 case VOLPBIT_XBFREE :
1345 xbfree = hton64( xbfree );
1346 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1347 bcopy(&xbfree, data, sizeof(xbfree));
1348 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1349 memcpy(data, &xbfree, sizeof( xbfree ));
1350 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1351 data += sizeof( xbfree );
1354 case VOLPBIT_XBTOTAL :
1355 xbtotal = hton64( xbtotal );
1356 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1357 bcopy(&xbtotal, data, sizeof(xbtotal));
1358 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1359 memcpy(data, &xbtotal, sizeof( xbtotal ));
1360 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1361 data += sizeof( xbfree );
1363 #endif /* ! NO_LARGE_VOL_SUPPORT */
1367 data += sizeof( u_int16_t );
1370 case VOLPBIT_BSIZE: /* block size */
1371 bsize = htonl(bsize);
1372 memcpy(data, &bsize, sizeof(bsize));
1373 data += sizeof(bsize);
1378 ad_close( &ad, ADFLAGS_HF );
1380 return( AFPERR_BITMAP );
1386 ashort = htons( data - buf );
1387 memcpy(nameoff, &ashort, sizeof( ashort ));
1388 aint = ucs2_to_charset( (utf8_encoding()?CH_UTF8_MAC:vol->v_maccharset), vol->v_name, data+1, 255);
1398 ad_close( &ad, ADFLAGS_HF );
1400 *buflen = data - buf;
1404 /* ------------------------- */
1405 int static stat_vol(u_int16_t bitmap, struct vol *vol, char *rbuf, int *rbuflen)
1410 if ( stat( vol->v_path, &st ) < 0 ) {
1412 return( AFPERR_PARAM );
1414 /* save the volume device number */
1415 vol->v_dev = st.st_dev;
1417 buflen = *rbuflen - sizeof( bitmap );
1418 if (( ret = getvolparams( bitmap, vol, &st,
1419 rbuf + sizeof( bitmap ), &buflen )) != AFP_OK ) {
1423 *rbuflen = buflen + sizeof( bitmap );
1424 bitmap = htons( bitmap );
1425 memcpy(rbuf, &bitmap, sizeof( bitmap ));
1430 /* ------------------------------- */
1431 void load_volumes(AFPObj *obj)
1433 struct passwd *pwent;
1438 /* check files date */
1439 if (obj->options.defaultvol.loaded) {
1440 changed = volfile_changed(&obj->options.defaultvol);
1442 if (obj->options.systemvol.loaded) {
1443 changed |= volfile_changed(&obj->options.systemvol);
1445 if (obj->options.uservol.loaded) {
1446 changed |= volfile_changed(&obj->options.uservol);
1455 pwent = getpwnam(obj->username);
1456 if ( (obj->options.flags & OPTION_USERVOLFIRST) == 0 ) {
1457 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent);
1460 if ((*obj->username == '\0') || (obj->options.flags & OPTION_NOUSERVOL)) {
1461 readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent);
1464 * Read user's AppleVolumes or .AppleVolumes file
1465 * If neither are readable, read the default volumes file. if
1466 * that doesn't work, create a user share.
1468 if (obj->options.uservol.name) {
1469 free(obj->options.uservol.name);
1471 obj->options.uservol.name = strdup(pwent->pw_dir);
1472 if ( readvolfile(obj, &obj->options.uservol, "AppleVolumes", 1, pwent) < 0 &&
1473 readvolfile(obj, &obj->options.uservol, ".AppleVolumes", 1, pwent) < 0 &&
1474 readvolfile(obj, &obj->options.uservol, "applevolumes", 1, pwent) < 0 &&
1475 readvolfile(obj, &obj->options.uservol, ".applevolumes", 1, pwent) < 0 &&
1476 obj->options.defaultvol.name != NULL ) {
1477 if (readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent) < 0)
1478 creatvol(obj, pwent, pwent->pw_dir, NULL, NULL, 1);
1481 if ( obj->options.flags & OPTION_USERVOLFIRST ) {
1482 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent );
1486 /* ------------------------------- */
1487 int afp_getsrvrparms(obj, ibuf, ibuflen, rbuf, rbuflen )
1490 int ibuflen, *rbuflen;
1503 for ( vcnt = 0, volume = Volumes; volume; volume = volume->v_next ) {
1504 if (!(volume->v_flags & AFPVOL_NOSTAT)) {
1505 if ( stat( volume->v_path, &st ) < 0 ) {
1506 LOG(log_info, logtype_afpd, "afp_getsrvrparms(%s): stat: %s",
1507 volume->v_path, strerror(errno) );
1508 continue; /* can't access directory */
1510 if (!S_ISDIR(st.st_mode)) {
1511 continue; /* not a dir */
1514 if (volume->v_hide) {
1515 continue; /* config file changed but the volume was mounted */
1517 len = ucs2_to_charset_allocate((utf8_encoding()?CH_UTF8_MAC:obj->options.maccharset),
1518 &namebuf, volume->v_name);
1519 if (len == (size_t)-1)
1522 /* set password bit if there's a volume password */
1523 *data = (volume->v_password) ? AFPSRVR_PASSWD : 0;
1525 /* Apple 2 clients running ProDOS-8 expect one volume to have
1526 bit 0 of this byte set. They will not recognize anything
1527 on the server unless this is the case. I have not
1528 completely worked this out, but it's related to booting
1529 from the server. Support for that function is a ways
1530 off.. <shirsch@ibm.net> */
1531 *data |= (volume->v_flags & AFPVOL_A2VOL) ? AFPSRVR_CONFIGINFO : 0;
1532 *data++ |= 0; /* UNIX PRIVS BIT ..., OSX doesn't seem to use it, so we don't either */
1534 memcpy(data, namebuf, len );
1540 *rbuflen = data - rbuf;
1542 if ( gettimeofday( &tv, 0 ) < 0 ) {
1543 LOG(log_error, logtype_afpd, "afp_getsrvrparms(%s): gettimeofday: %s", volume->v_path, strerror(errno) );
1545 return AFPERR_PARAM;
1547 tv.tv_sec = AD_DATE_FROM_UNIX(tv.tv_sec);
1548 memcpy(data, &tv.tv_sec, sizeof( u_int32_t));
1549 data += sizeof( u_int32_t);
1554 /* -------------------------
1555 * we are the user here
1557 int afp_openvol(obj, ibuf, ibuflen, rbuf, rbuflen )
1560 int ibuflen, *rbuflen;
1570 char path[ MAXPATHLEN + 1];
1575 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1576 bitmap = ntohs( bitmap );
1577 ibuf += sizeof( bitmap );
1578 if (( bitmap & (1<<VOLPBIT_VID)) == 0 ) {
1580 return AFPERR_BITMAP;
1583 len = (unsigned char)*ibuf++;
1584 volname = obj->oldtmp;
1585 namelen = convert_string( (utf8_encoding()?CH_UTF8_MAC:obj->options.maccharset), CH_UCS2,
1586 ibuf, len, volname, sizeof(obj->oldtmp));
1589 return AFPERR_PARAM;
1593 if ((len + 1) & 1) /* pad to an even boundary */
1598 for ( volume = Volumes; volume; volume = volume->v_next ) {
1599 if ( strcasecmp_w( (ucs2_t*) volname, volume->v_name ) == 0 ) {
1604 if ( volume == NULL ) {
1606 return AFPERR_PARAM;
1609 /* check for a volume password */
1610 if (volume->v_password && strncmp(ibuf, volume->v_password, VOLPASSLEN)) {
1612 return AFPERR_ACCESS;
1615 if (( volume->v_flags & AFPVOL_OPEN ) ) {
1616 /* the volume is already open */
1618 set_uidgid ( volume );
1620 return stat_vol(bitmap, volume, rbuf, rbuflen);
1623 /* initialize volume variables
1626 if (afp_version >= 30) {
1627 volume->max_filename = 255;
1630 volume->max_filename = MACFILELEN;
1633 volume->v_dir = volume->v_root = NULL;
1635 volume->v_flags |= AFPVOL_OPEN;
1636 volume->v_cdb = NULL;
1638 if (volume->v_root_preexec) {
1639 if ((ret = afprun(1, volume->v_root_preexec, NULL)) && volume->v_root_preexec_close) {
1640 LOG(log_error, logtype_afpd, "afp_openvol(%s): root preexec : %d", volume->v_path, ret );
1647 set_uidgid ( volume );
1650 if (volume->v_preexec) {
1651 if ((ret = afprun(0, volume->v_preexec, NULL)) && volume->v_preexec_close) {
1652 LOG(log_error, logtype_afpd, "afp_openvol(%s): preexec : %d", volume->v_path, ret );
1658 if ( stat( volume->v_path, &st ) < 0 ) {
1663 if ( chdir( volume->v_path ) < 0 ) {
1668 len = convert_string_allocate( CH_UCS2, (utf8_encoding()?CH_UTF8_MAC:obj->options.maccharset),
1669 volume->v_name, namelen, &vol_mname);
1670 if ( !vol_mname || len <= 0) {
1675 if ( NULL == getcwd(path, MAXPATHLEN)) {
1676 /* shouldn't be fatal but it will fail later */
1677 LOG(log_error, logtype_afpd, "afp_openvol(%s): volume pathlen too long", volume->v_path);
1682 if ((vol_uname = strrchr(path, '/')) == NULL)
1684 else if (*(vol_uname + 1) != '\0')
1687 if ((dir = dirnew(vol_mname, vol_uname) ) == NULL) {
1689 LOG(log_error, logtype_afpd, "afp_openvol(%s): malloc: %s", volume->v_path, strerror(errno) );
1695 dir->d_did = DIRDID_ROOT;
1696 dir->d_color = DIRTREE_COLOR_BLACK; /* root node is black */
1697 volume->v_dir = volume->v_root = dir;
1699 curdir = volume->v_dir;
1700 if (volume->v_cnidscheme == NULL) {
1701 volume->v_cnidscheme = strdup(DEFAULT_CNID_SCHEME);
1702 LOG(log_warning, logtype_afpd, "Warning: No CNID scheme for volume %s. Using default.",
1705 if (volume->v_dbpath)
1706 volume->v_cdb = cnid_open (volume->v_dbpath, volume->v_umask, volume->v_cnidscheme, (volume->v_flags & AFPVOL_NODEV));
1708 volume->v_cdb = cnid_open (volume->v_path, volume->v_umask, volume->v_cnidscheme, (volume->v_flags & AFPVOL_NODEV));
1709 if (volume->v_cdb == NULL) {
1710 LOG(log_error, logtype_afpd, "Fatal error: cannot open CNID or invalid CNID backend for %s: %s",
1711 volume->v_path, volume->v_cnidscheme);
1718 if (!volume->v_volcodepage)
1719 volume->v_volcodepage = strdup("UTF8");
1721 if ( (charset_t) -1 == ( volume->v_volcharset = add_charset(volume->v_volcodepage)) ) {
1722 LOG (log_error, logtype_afpd, "Setting codepage %s as volume codepage failed", volume->v_volcodepage);
1727 if ( NULL == ( volume->v_vol = find_charset_functions(volume->v_volcodepage)) || volume->v_vol->flags & CHARSET_ICONV ) {
1728 LOG (log_warning, logtype_afpd, "WARNING: volume encoding %s is *not* supported by netatalk, expect problems !!!!", volume->v_volcodepage);
1731 if (!volume->v_maccodepage)
1732 volume->v_maccodepage = strdup(obj->options.maccodepage);
1734 if ( (charset_t) -1 == ( volume->v_maccharset = add_charset(volume->v_maccodepage)) ) {
1735 LOG (log_error, logtype_afpd, "Setting codepage %s as mac codepage failed", volume->v_maccodepage);
1740 if ( NULL == ( volume->v_mac = find_charset_functions(volume->v_maccodepage)) || ! (volume->v_mac->flags & CHARSET_CLIENT) ) {
1741 LOG (log_error, logtype_afpd, "Fatal error: mac charset %s not supported", volume->v_maccodepage);
1746 ret = stat_vol(bitmap, volume, rbuf, rbuflen);
1747 if (ret == AFP_OK) {
1749 handle_special_folders( volume );
1750 savevoloptions( volume);
1753 * If you mount a volume twice, the second time the trash appears on
1754 * the desk-top. That's because the Mac remembers the DID for the
1755 * trash (even for volumes in different zones, on different servers).
1756 * Just so this works better, we prime the DID cache with the trash,
1757 * fixing the trash at DID 17.
1758 * FIXME (RL): should it be done inside a CNID backend ? (always returning Trash DID when asked) ?
1760 if ((volume->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1762 /* FIXME find db time stamp */
1763 if (cnid_getstamp(volume->v_cdb, volume->v_stamp, sizeof(volume->v_stamp)) < 0) {
1764 LOG (log_error, logtype_afpd,
1765 "afp_openvol(%s): Fatal error: Unable to get stamp value from CNID backend",
1772 cname( volume, volume->v_dir, &p );
1778 if (volume->v_dir) {
1779 dirfree( volume->v_dir );
1780 volume->v_dir = volume->v_root = NULL;
1783 volume->v_flags &= ~AFPVOL_OPEN;
1784 if (volume->v_cdb != NULL) {
1785 cnid_close(volume->v_cdb);
1786 volume->v_cdb = NULL;
1792 /* ------------------------- */
1793 static void closevol(struct vol *vol)
1798 dirfree( vol->v_root );
1800 if (vol->v_cdb != NULL) {
1801 cnid_close(vol->v_cdb);
1805 if (vol->v_postexec) {
1806 afprun(0, vol->v_postexec, NULL);
1808 if (vol->v_root_postexec) {
1809 afprun(1, vol->v_root_postexec, NULL);
1813 /* ------------------------- */
1814 void close_all_vol(void)
1818 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
1819 if ( ovol->v_flags & AFPVOL_OPEN ) {
1820 ovol->v_flags &= ~AFPVOL_OPEN;
1826 /* ------------------------- */
1827 int afp_closevol(obj, ibuf, ibuflen, rbuf, rbuflen )
1830 int ibuflen, *rbuflen;
1832 struct vol *vol, *ovol;
1837 memcpy(&vid, ibuf, sizeof( vid ));
1838 if (NULL == ( vol = getvolbyvid( vid )) ) {
1839 return( AFPERR_PARAM );
1842 vol->v_flags &= ~AFPVOL_OPEN;
1843 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
1844 if ( ovol->v_flags & AFPVOL_OPEN ) {
1848 if ( ovol != NULL ) {
1849 /* Even if chdir fails, we can't say afp_closevol fails. */
1850 if ( chdir( ovol->v_path ) == 0 ) {
1851 curdir = ovol->v_dir;
1856 if (vol->v_deleted) {
1857 showvol(vol->v_name);
1865 /* ------------------------- */
1866 struct vol *getvolbyvid(const u_int16_t vid )
1870 for ( vol = Volumes; vol; vol = vol->v_next ) {
1871 if ( vid == vol->v_vid ) {
1875 if ( vol == NULL || ( vol->v_flags & AFPVOL_OPEN ) == 0 ) {
1881 #endif /* FORCE_UIDGID */
1886 /* ------------------------ */
1887 static int ext_cmp_key(const void *key, const void *obj)
1889 const char *p = key;
1890 const struct extmap *em = obj;
1891 return strdiacasecmp(p, em->em_ext);
1893 struct extmap *getextmap(const char *path)
1898 if (NULL == ( p = strrchr( path, '.' )) ) {
1899 return( Defextmap );
1902 if (!*p || !Extmap_cnt) {
1903 return( Defextmap );
1905 em = bsearch(p, Extmap, Extmap_cnt, sizeof(struct extmap), ext_cmp_key);
1909 return( Defextmap );
1913 /* ------------------------- */
1914 struct extmap *getdefextmap(void)
1916 return( Defextmap );
1919 /* --------------------------
1920 poll if a volume is changed by other processes.
1922 int pollvoltime(obj)
1929 if (!(afp_version > 21 && obj->options.server_notif))
1932 if ( gettimeofday( &tv, 0 ) < 0 )
1935 for ( vol = Volumes; vol; vol = vol->v_next ) {
1936 if ( (vol->v_flags & AFPVOL_OPEN) && vol->v_mtime + 30 < tv.tv_sec) {
1937 if ( !stat( vol->v_path, &st ) && vol->v_mtime != st.st_mtime ) {
1938 vol->v_mtime = st.st_mtime;
1939 if (!obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED))
1948 /* ------------------------- */
1949 void setvoltime(obj, vol )
1955 /* just looking at vol->v_mtime is broken seriously since updates
1956 * from other users afpd processes never are seen.
1957 * This is not the most elegant solution (a shared memory between
1958 * the afpd processes would come closer)
1961 if ( gettimeofday( &tv, 0 ) < 0 ) {
1962 LOG(log_error, logtype_afpd, "setvoltime(%s): gettimeofday: %s", vol->v_path, strerror(errno) );
1965 if( utime( vol->v_path, NULL ) < 0 ) {
1966 /* write of time failed ... probably a read only filesys,
1967 * where no other users can interfere, so there's no issue here
1971 /* a little granularity */
1972 if (vol->v_mtime < tv.tv_sec) {
1973 vol->v_mtime = tv.tv_sec;
1974 /* or finder doesn't update free space */
1975 if (afp_version > 21 && obj->options.server_notif) {
1976 obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED);
1981 /* ------------------------- */
1982 int afp_getvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1985 int ibuflen, *rbuflen;
1988 u_int16_t vid, bitmap;
1991 memcpy(&vid, ibuf, sizeof( vid ));
1992 ibuf += sizeof( vid );
1993 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1994 bitmap = ntohs( bitmap );
1996 if (NULL == ( vol = getvolbyvid( vid )) ) {
1998 return( AFPERR_PARAM );
2001 return stat_vol(bitmap, vol, rbuf, rbuflen);
2004 /* ------------------------- */
2005 int afp_setvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
2008 int ibuflen, *rbuflen;
2012 u_int16_t vid, bitmap;
2018 memcpy(&vid, ibuf, sizeof( vid ));
2019 ibuf += sizeof( vid );
2020 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2021 bitmap = ntohs( bitmap );
2022 ibuf += sizeof(bitmap);
2024 if (( vol = getvolbyvid( vid )) == NULL ) {
2025 return( AFPERR_PARAM );
2028 if (vol->v_flags & AFPVOL_RO)
2029 return AFPERR_VLOCK;
2031 /* we can only set the backup date. */
2032 if (bitmap != (1 << VOLPBIT_BDATE))
2033 return AFPERR_BITMAP;
2035 ad_init(&ad, vol->v_adouble);
2036 if ( ad_open( vol->v_path, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR,
2039 return AFPERR_VLOCK;
2041 return AFPERR_ACCESS;
2044 memcpy(&aint, ibuf, sizeof(aint));
2045 ad_setdate(&ad, AD_DATE_BACKUP, aint);
2046 ad_flush(&ad, ADFLAGS_HF);
2047 ad_close(&ad, ADFLAGS_HF);
2051 /* ------------------------- */
2052 int wincheck(const struct vol *vol, const char *path)
2056 if (!(vol->v_flags & AFPVOL_MSWINDOWS))
2059 /* empty paths are not allowed */
2060 if ((len = strlen(path)) == 0)
2063 /* leading or trailing whitespaces are not allowed, carriage returns
2064 * and probably other whitespace is okay, tabs are not allowed
2066 if ((path[0] == ' ') || (path[len-1] == ' '))
2069 /* certain characters are not allowed */
2070 if (strpbrk(path, MSWINDOWS_BADCHARS))
2073 /* everything else is okay */
2079 * precreate a folder
2080 * this is only intended for folders in the volume root
2081 * It will *not* work if the folder name contains extended characters
2083 static int create_special_folder (const struct vol *vol, const struct _special_folder *folder)
2092 p = (char *) malloc ( strlen(vol->v_path)+strlen(folder->name)+2);
2094 LOG(log_error, logtype_afpd,"malloc failed");
2098 q=strdup(folder->name);
2100 LOG(log_error, logtype_afpd,"malloc failed");
2104 strcpy(p, vol->v_path);
2109 if ((vol->v_casefold & AFPVOL_MTOUUPPER))
2111 else if ((vol->v_casefold & AFPVOL_MTOULOWER))
2117 if ( (ret = stat( p, &st )) < 0 ) {
2118 if (folder->precreate) {
2119 if (ad_mkdir(p, folder->mode)) {
2120 LOG(log_debug, logtype_afpd,"Creating '%s' failed in %s: %s", p, vol->v_path, strerror(errno));
2129 if ( !ret && folder->hide) {
2131 ad_init(&ad, vol->v_adouble);
2132 if (ad_open( p, vol_noadouble(vol) | ADFLAGS_HF|ADFLAGS_DIR,
2133 O_RDWR|O_CREAT, 0666, &ad) < 0) {
2138 if ((ad_get_HF_flags( &ad ) & O_CREAT) ) {
2139 if (ad_getentryoff(&ad, ADEID_NAME)) {
2140 ad_setentrylen( &ad, ADEID_NAME, strlen(folder->name));
2141 memcpy(ad_entry( &ad, ADEID_NAME ), folder->name,
2142 ad_getentrylen( &ad, ADEID_NAME ));
2146 ad_getattr(&ad, &attr);
2147 attr |= htons( ntohs( attr ) | ATTRBIT_INVISIBLE );
2148 ad_setattr(&ad, attr);
2150 /* do the same with the finder info */
2151 if (ad_entry(&ad, ADEID_FINDERI)) {
2152 memcpy(&attr, ad_entry(&ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF, sizeof(attr));
2153 attr |= htons(FINDERINFO_INVISIBLE);
2154 memcpy(ad_entry(&ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF,&attr, sizeof(attr));
2157 ad_flush( &ad, ADFLAGS_HF );
2158 ad_close( &ad, ADFLAGS_HF );
2165 static void handle_special_folders (const struct vol * vol)
2167 const _special_folder *p = &special_folders[0];
2169 if (vol->v_flags & AFPVOL_RO)
2172 for (; p->name != NULL; p++) {
2173 create_special_folder (vol, p);
2178 * Save the volume options to a file, used by
2180 * Writing the file everytime a volume is opened is
2181 * unnecessary, but it shouldn't hurt much.
2183 static int savevoloptions (const struct vol *vol)
2186 char item[MAXPATHLEN];
2190 const _vol_opt_name *op = &vol_opt_names[0];
2191 const _vol_opt_name *cf = &vol_opt_casefold[0];
2193 strlcpy (item, vol->v_path, sizeof(item));
2194 strlcat (item, "/.AppleDesktop/", sizeof(item));
2195 strlcat (item, VOLINFOFILE, sizeof(item));
2197 if ((fd = open( item, O_RDWR | O_CREAT , 0666)) <0 ) {
2198 LOG(log_debug, logtype_afpd,"Error opening %s: %s", item, strerror(errno));
2202 /* try to get a lock */
2204 lock.l_whence = SEEK_SET;
2206 lock.l_type = F_WRLCK;
2208 if (fcntl(fd, F_SETLK, &lock) < 0) {
2209 if (errno == EACCES || errno == EAGAIN) {
2210 /* ignore, other process already writing the file */
2213 LOG(log_error, logtype_cnid, "savevoloptions: cannot get lock: %s", strerror(errno));
2218 /* write volume options */
2219 snprintf(buf, sizeof(buf), "MAC_CHARSET:%s\n", vol->v_maccodepage);
2220 snprintf(item, sizeof(item), "VOL_CHARSET:%s\n", vol->v_volcodepage);
2221 strlcat(buf, item, sizeof(buf));
2223 switch (vol->v_adouble) {
2225 strlcat(buf, "ADOUBLE_VER:v1\n", sizeof(buf));
2228 strlcat(buf, "ADOUBLE_VER:v2\n", sizeof(buf));
2230 case AD_VERSION2_OSX:
2231 strlcat(buf, "ADOUBLE_VER:osx\n", sizeof(buf));
2235 strlcat(buf, "CNIDBACKEND:", sizeof(buf));
2236 strlcat(buf, vol->v_cnidscheme, sizeof(buf));
2237 strlcat(buf, "\n", sizeof(buf));
2239 strlcat(buf, "CNIDDBDHOST:", sizeof(buf));
2240 strlcat(buf, Cnid_srv, sizeof(buf));
2241 strlcat(buf, "\n", sizeof(buf));
2243 snprintf(item, sizeof(item), "CNIDDBDPORT:%u\n", Cnid_port);
2244 strlcat(buf, item, sizeof(buf));
2246 strcpy(item, "CNID_DBPATH:");
2248 strlcat(item, vol->v_dbpath, sizeof(item));
2250 strlcat(item, vol->v_path, sizeof(item));
2251 strlcat(item, "\n", sizeof(item));
2252 strlcat(buf, item, sizeof(buf));
2255 strcpy(item, "VOLUME_OPTS:");
2256 for (;op->name; op++) {
2257 if ( vol->v_flags & op->option ) {
2258 strlcat(item, op->name, sizeof(item));
2259 strlcat(item, " ", sizeof(item));
2262 strlcat(item, "\n", sizeof(item));
2263 strlcat(buf, item, sizeof(buf));
2265 /* casefold flags */
2266 strcpy(item, "VOLCASEFOLD:");
2267 for (;cf->name; cf++) {
2268 if ( vol->v_casefold & cf->option ) {
2269 strlcat(item, cf->name, sizeof(item));
2270 strlcat(item, " ", sizeof(item));
2273 strlcat(item, "\n", sizeof(item));
2274 strlcat(buf, item, sizeof(buf));
2276 if (strlen(buf) >= sizeof(buf)-1)
2277 LOG(log_debug, logtype_afpd,"Error writing .volinfo file: buffer too small, %s", buf);
2280 if (write( fd, buf, strlen(buf)) < 0) {
2281 LOG(log_debug, logtype_afpd,"Error writing .volinfo file: %s", strerror(errno));
2284 ftruncate(fd, strlen(buf));
2287 lock.l_type = F_UNLCK;
2288 fcntl(fd, F_SETLK, &lock);