2 * $Id: volume.c,v 1.43 2002-11-15 10:59:11 srittau Exp $
4 * Copyright (c) 1990,1993 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
10 #endif /* HAVE_CONFIG_H */
13 #include <atalk/logger.h>
14 #include <sys/types.h>
16 #include <sys/param.h>
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
20 #include <netatalk/endian.h>
21 #include <atalk/asp.h>
22 #include <atalk/dsi.h>
23 #include <atalk/adouble.h>
24 #include <atalk/afp.h>
25 #include <atalk/util.h>
27 #include <atalk/cnid.h>
32 #endif /* HAVE_FCNTL_H */
40 #else /* STDC_HEADERS */
44 #endif /* HAVE_STRCHR */
45 char *strchr (), *strrchr ();
47 #define memcpy(d,s,n) bcopy ((s), (d), (n))
48 #define memmove(d,s,n) bcopy ((s), (d), (n))
49 #endif /* ! HAVE_MEMCPY */
50 #endif /* STDC_HEADERS */
57 #include "directory.h"
64 #define MIN(a, b) ((a) < (b) ? (a) : (b))
67 #ifndef NO_LARGE_VOL_SUPPORT
68 #if BYTE_ORDER == BIG_ENDIAN
71 #else /* BYTE_ORDER == BIG_ENDIAN */
72 #define hton64(x) ((u_int64_t) (htonl(((x) >> 32) & 0xffffffffLL)) | \
73 (u_int64_t) ((htonl(x) & 0xffffffffLL) << 32))
74 #define ntoh64(x) (hton64(x))
75 #endif /* BYTE_ORDER == BIG_ENDIAN */
76 #endif /* ! NO_LARGE_VOL_SUPPORT */
78 static struct vol *volumes = NULL;
79 static int lastvid = 0;
81 static char *Trash = "\02\024Network Trash Folder";
83 static struct extmap *extmap = NULL, *defextmap = NULL;
84 static int extmap_cnt;
86 #define VOLOPT_ALLOW 0 /* user allow list */
87 #define VOLOPT_DENY 1 /* user deny list */
88 #define VOLOPT_RWLIST 2 /* user rw list */
89 #define VOLOPT_ROLIST 3 /* user ro list */
90 #define VOLOPT_CODEPAGE 4 /* codepage */
91 #define VOLOPT_PASSWORD 5 /* volume password */
92 #define VOLOPT_CASEFOLD 6 /* character case mangling */
93 #define VOLOPT_FLAGS 7 /* various flags */
94 #define VOLOPT_DBPATH 8 /* path to database */
95 #define VOLOPT_MAPCHARS 9 /* does mtou and utom mappings. syntax:
96 m and u can be double-byte hex
101 !u -> make u illegal always
102 ~u -> make u illegal only as the first
103 part of a double-byte character.
105 #define VOLOPT_VETO 10 /* list of veto filespec */
111 #define VOLOPT_FORCEUID 11 /* force uid for username x */
112 #define VOLOPT_FORCEGID 12 /* force gid for group x */
113 #define VOLOPT_UMASK 13
114 #define VOLOPT_MAX 13
115 #else /* normally, there are only 9 possible options */
116 #define VOLOPT_UMASK 11
117 #define VOLOPT_MAX 11
118 #endif /* FORCE_UIDGID */
120 #define VOLOPT_NUM (VOLOPT_MAX + 1)
123 #define VOLOPT_DEFAULT ":DEFAULT:"
124 #define VOLOPT_DEFAULT_LEN 9
130 static __inline__ void volfree(struct vol_option *options,
131 const struct vol_option *save)
136 for (i = 0; i < VOLOPT_MAX; i++) {
137 if (options[i].c_value && (options[i].c_value != save[i].c_value))
138 free(options[i].c_value);
141 for (i = 0; i < VOLOPT_MAX; i++) {
142 if (options[i].c_value)
143 free(options[i].c_value);
149 /* handle variable substitutions. here's what we understand:
150 * $c -> client ip/appletalk address
151 * $f -> full name (whatever's in the gecos field)
154 * $s -> server name (hostname if it doesn't exist)
155 * $u -> username (guest is usually nobody)
156 * $v -> volume name (ADEID_NAME or basename)
157 * $z -> zone (may not exist)
160 #define is_var(a, b) (strncmp((a), (b), 2) == 0)
161 static void volxlate(AFPObj *obj, char *dest, int destlen,
162 char *src, struct passwd *pwd, char *path)
167 strncpy(dest, src, destlen);
168 if ((p = strchr(src, '$')) == NULL) /* nothing to do */
171 /* first part of the path. just forward to the next variable. */
172 len = MIN(p - src, destlen);
178 while (p && destlen > 0) {
179 /* now figure out what the variable is */
181 if (is_var(p, "$c")) {
182 if (obj->proto == AFPPROTO_ASP) {
183 ASP asp = obj->handle;
185 len = sprintf(dest, "%u.%u", ntohs(asp->asp_sat.sat_addr.s_net),
186 asp->asp_sat.sat_addr.s_node);
190 } else if (obj->proto == AFPPROTO_DSI) {
191 DSI *dsi = obj->handle;
193 len = sprintf(dest, "%s:%u", inet_ntoa(dsi->client.sin_addr),
194 ntohs(dsi->client.sin_port));
198 } else if (is_var(p, "$f")) {
199 if ((q = strchr(pwd->pw_gecos, ',')))
202 } else if (is_var(p, "$g")) {
203 struct group *grp = getgrgid(pwd->pw_gid);
206 } else if (is_var(p, "$h")) {
207 q = obj->options.hostname;
208 } else if (is_var(p, "$s")) {
211 else if (obj->options.server) {
212 q = obj->options.server;
214 q = obj->options.hostname;
215 } else if (is_var(p, "$u")) {
217 } else if (is_var(p, "$v")) {
221 memset(&ad, 0, sizeof(ad));
222 if (ad_open(path, ADFLAGS_HF, O_RDONLY, 0, &ad) < 0)
225 if ((len = MIN(ad_getentrylen(&ad, ADEID_NAME), destlen)) > 0) {
226 memcpy(dest, ad_entry(&ad, ADEID_NAME), len);
227 ad_close(&ad, ADFLAGS_HF);
231 ad_close(&ad, ADFLAGS_HF);
232 no_volname: /* simple basename */
233 if ((q = strrchr(path, '/')) == NULL)
235 else if (*(q + 1) != '\0')
239 } else if (is_var(p, "$z")) {
241 } else if (is_var(p, "$$")) {
246 /* copy the stuff over. if we don't understand something that we
247 * should, just skip it over. */
249 len = MIN(p == q ? 2 : strlen(q), destlen);
250 strncpy(dest, q, len);
255 /* stuff up to next $ */
257 p = strchr(src, '$');
258 len = p ? MIN(p - src, destlen) : destlen;
260 strncpy(dest, src, len);
267 /* to make sure that val is valid, make sure to select an opt that
269 #define optionok(buf,opt,val) (strstr((buf),(opt)) && ((val)[1] != '\0'))
271 static __inline__ char *get_codepage_path(const char *path, const char *name)
277 page = (char *) malloc((len = strlen(path)) + strlen(name) + 2);
280 if (path[len - 1] != '/') /* add a / */
288 /* debug: show which codepage directory we are using */
289 LOG(log_debug, logtype_afpd, "using codepage directory: %s", page);
294 /* handle all the options. tmp can't be NULL. */
295 static void volset(struct vol_option *options, char *volname, int vlen,
296 const char *nlspath, const char *tmp, AFPObj *obj,
301 val = strchr(tmp, ':');
302 LOG(log_debug, logtype_afpd, "Parsing volset %s", val);
304 if (optionok(tmp, "allow:", val)) {
305 if (options[VOLOPT_ALLOW].c_value)
306 free(options[VOLOPT_ALLOW].c_value);
307 options[VOLOPT_ALLOW].c_value = strdup(val + 1);
309 } else if (optionok(tmp, "deny:", val)) {
310 if (options[VOLOPT_DENY].c_value)
311 free(options[VOLOPT_DENY].c_value);
312 options[VOLOPT_DENY].c_value = strdup(val + 1);
314 } else if (optionok(tmp, "rwlist:", val)) {
315 if (options[VOLOPT_RWLIST].c_value)
316 free(options[VOLOPT_RWLIST].c_value);
317 options[VOLOPT_RWLIST].c_value = strdup(val + 1);
319 } else if (optionok(tmp, "rolist:", val)) {
320 if (options[VOLOPT_ROLIST].c_value)
321 free(options[VOLOPT_ROLIST].c_value);
322 options[VOLOPT_ROLIST].c_value = strdup(val + 1);
324 } else if (optionok(tmp, "codepage:", val)) {
325 if (options[VOLOPT_CODEPAGE].c_value)
326 free(options[VOLOPT_CODEPAGE].c_value);
327 options[VOLOPT_CODEPAGE].c_value = get_codepage_path(nlspath, val + 1);
329 } else if (optionok(tmp, "veto:", val)) {
330 if (options[VOLOPT_VETO].c_value)
331 free(options[VOLOPT_VETO].c_value);
332 options[VOLOPT_VETO].c_value = strdup(val + 1);
334 } else if (optionok(tmp, "casefold:", val)) {
335 if (strcasecmp(val + 1, "tolower") == 0)
336 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMLOWER;
337 else if (strcasecmp(val + 1, "toupper") == 0)
338 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMUPPER;
339 else if (strcasecmp(val + 1, "xlatelower") == 0)
340 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UUPPERMLOWER;
341 else if (strcasecmp(val + 1, "xlateupper") == 0)
342 options[VOLOPT_CASEFOLD].i_value = AFPVOL_ULOWERMUPPER;
344 } else if (optionok(tmp, "options:", val)) {
347 if ((p = strtok(val + 1, ",")) == NULL) /* nothing */
351 if (strcasecmp(p, "prodos") == 0)
352 options[VOLOPT_FLAGS].i_value |= AFPVOL_A2VOL;
353 else if (strcasecmp(p, "mswindows") == 0) {
354 options[VOLOPT_FLAGS].i_value |= AFPVOL_MSWINDOWS;
355 if (!options[VOLOPT_CODEPAGE].c_value)
356 options[VOLOPT_CODEPAGE].c_value =
357 get_codepage_path(nlspath, MSWINDOWS_CODEPAGE);
359 } else if (strcasecmp(p, "crlf") == 0)
360 options[VOLOPT_FLAGS].i_value |= AFPVOL_CRLF;
361 else if (strcasecmp(p, "noadouble") == 0)
362 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOADOUBLE;
363 else if (strcasecmp(p, "ro") == 0)
364 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
365 else if (strcasecmp(p, "nohex") == 0)
366 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOHEX;
367 else if (strcasecmp(p, "usedots") == 0)
368 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS;
369 else if (strcasecmp(p, "limitsize") == 0)
370 options[VOLOPT_FLAGS].i_value |= AFPVOL_LIMITSIZE;
371 /* support for either "dropbox" or "dropkludge" */
372 else if (strcasecmp(p, "dropbox") == 0)
373 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
374 else if (strcasecmp(p, "dropkludge") == 0)
375 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
376 else if (strcasecmp(p, "nofileid") == 0)
377 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOFILEID;
379 p = strtok(NULL, ",");
383 } else if (optionok(tmp, "dbpath:", val)) {
384 char t[MAXPATHLEN + 1];
385 if (options[VOLOPT_DBPATH].c_value)
386 free(options[VOLOPT_DBPATH].c_value);
388 volxlate(obj, t, MAXPATHLEN, val, pwd, NULL);
389 options[VOLOPT_DBPATH].c_value = strdup(t + 1);
391 } else if (optionok(tmp, "umask:", val)) {
392 options[VOLOPT_UMASK].i_value = (int)strtol(val, (char **)NULL, 8);
393 } else if (optionok(tmp, "mapchars:",val)) {
394 if (options[VOLOPT_MAPCHARS].c_value)
395 free(options[VOLOPT_MAPCHARS].c_value);
396 options[VOLOPT_MAPCHARS].c_value = strdup(val + 1);
398 } else if (optionok(tmp, "password:", val)) {
399 if (options[VOLOPT_PASSWORD].c_value)
400 free(options[VOLOPT_PASSWORD].c_value);
401 options[VOLOPT_PASSWORD].c_value = strdup(val + 1);
405 /* this code allows forced uid/gid per volume settings */
406 } else if (optionok(tmp, "forceuid:", val)) {
407 if (options[VOLOPT_FORCEUID].c_value)
408 free(options[VOLOPT_FORCEUID].c_value);
409 options[VOLOPT_FORCEUID].c_value = strdup(val + 1);
410 } else if (optionok(tmp, "forcegid:", val)) {
411 if (options[VOLOPT_FORCEGID].c_value)
412 free(options[VOLOPT_FORCEGID].c_value);
413 options[VOLOPT_FORCEGID].c_value = strdup(val + 1);
415 #endif /* FORCE_UIDGID */
418 /* ignore unknown options */
419 LOG(log_debug, logtype_afpd, "ignoring unknown volume option: %s", tmp);
422 /* we'll assume it's a volume name. */
423 strncpy(volname, tmp, vlen);
427 static int creatvol(const char *path, char *name, struct vol_option *options)
432 if ( name == NULL || *name == '\0' ) {
433 if ((name = strrchr( path, '/' )) == NULL) {
434 return -1; /* Obviously not a fully qualified path */
437 /* if you wish to share /, you need to specify a name. */
442 for ( volume = volumes; volume; volume = volume->v_next ) {
443 if ( strcasecmp( volume->v_name, name ) == 0 ) {
444 return -1; /* Won't be able to access it, anyway... */
448 vlen = strlen( name );
449 if ( vlen > AFPVOL_NAMELEN ) {
450 vlen = AFPVOL_NAMELEN;
451 name[AFPVOL_NAMELEN] = '\0';
455 (struct vol *)calloc(1, sizeof( struct vol ))) == NULL ) {
456 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
459 if (( volume->v_name =
460 (char *)malloc( vlen + 1 )) == NULL ) {
461 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
465 if (( volume->v_path =
466 (char *)malloc( strlen( path ) + 1 )) == NULL ) {
467 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
468 free(volume->v_name);
473 strcpy( volume->v_name, name);
474 strcpy( volume->v_path, path );
478 #endif /* __svr4__ */
479 volume->v_vid = lastvid++;
480 volume->v_lastdid = 17;
484 /* should we casefold? */
485 volume->v_casefold = options[VOLOPT_CASEFOLD].i_value;
487 /* shift in some flags */
488 volume->v_flags = options[VOLOPT_FLAGS].i_value;
490 /* read in the code pages */
491 if (options[VOLOPT_CODEPAGE].c_value)
492 codepage_read(volume, options[VOLOPT_CODEPAGE].c_value);
494 if (options[VOLOPT_PASSWORD].c_value)
495 volume->v_password = strdup(options[VOLOPT_PASSWORD].c_value);
497 if (options[VOLOPT_VETO].c_value)
498 volume->v_veto = strdup(options[VOLOPT_VETO].c_value);
501 if (options[VOLOPT_DBPATH].c_value)
502 volume->v_dbpath = strdup(options[VOLOPT_DBPATH].c_value);
505 if (options[VOLOPT_UMASK].i_value)
506 volume->v_umask = (mode_t)options[VOLOPT_UMASK].i_value;
510 if (options[VOLOPT_FORCEUID].c_value) {
511 volume->v_forceuid = strdup(options[VOLOPT_FORCEUID].c_value);
513 volume->v_forceuid = NULL; /* set as null so as to return 0 later on */
516 if (options[VOLOPT_FORCEGID].c_value) {
517 volume->v_forcegid = strdup(options[VOLOPT_FORCEGID].c_value);
519 volume->v_forcegid = NULL; /* set as null so as to return 0 later on */
522 #endif /* FORCE_UIDGID */
526 volume->v_next = volumes;
531 static char *myfgets( buf, size, fp )
540 while ((( c = getc( fp )) != EOF ) && ( size > 0 )) {
541 if ( c == '\n' || c == '\r' ) {
559 /* check access list. this function wants something of the following
561 * @group,name,name2,@group2,name3
563 * a NULL argument allows everybody to have access.
564 * we return three things:
566 * 0: list exists, but name isn't in it
569 static int accessvol(args, name)
573 char buf[MAXPATHLEN + 1], *p;
579 strncpy(buf, args, sizeof(buf));
580 if ((p = strtok(buf, ",")) == NULL) /* nothing, return okay */
584 if (*p == '@') { /* it's a group */
585 if ((gr = getgrnam(p + 1)) && gmem(gr->gr_gid))
587 } else if (strcmp(p, name) == 0) /* it's a user name */
589 p = strtok(NULL, ",");
595 static void setextmap( ext, type, creator, user)
596 char *ext, *type, *creator;
602 if (extmap == NULL) {
603 if (( extmap = calloc(1, sizeof( struct extmap ))) == NULL ) {
604 LOG(log_error, logtype_afpd, "setextmap: calloc: %s", strerror(errno) );
609 for ( em = extmap, cnt = 0; em->em_ext; em++, cnt++) {
610 if ( (strdiacasecmp( em->em_ext, ext )) == 0 ) {
615 if ( em->em_ext == NULL ) {
616 if (!(extmap = realloc( extmap, sizeof( struct extmap ) * (cnt +2))) ) {
617 LOG(log_error, logtype_afpd, "setextmap: realloc: %s", strerror(errno) );
620 (extmap +cnt +1)->em_ext = NULL;
622 } else if ( !user ) {
628 if (!(em->em_ext = strdup( ext))) {
629 LOG(log_error, logtype_afpd, "setextmap: strdup: %s", strerror(errno) );
633 if ( *type == '\0' ) {
634 memcpy(em->em_type, "????", sizeof( em->em_type ));
636 memcpy(em->em_type, type, sizeof( em->em_type ));
638 if ( *creator == '\0' ) {
639 memcpy(em->em_creator, "UNIX", sizeof( em->em_creator ));
641 memcpy(em->em_creator, creator, sizeof( em->em_creator ));
645 /* -------------------------- */
646 static int extmap_cmp(const void *map1, const void *map2)
648 const struct extmap *em1 = map1;
649 const struct extmap *em2 = map2;
650 return strdiacasecmp(em1->em_ext, em2->em_ext);
653 static void sortextmap( void)
658 if ((em = extmap) == NULL) {
666 qsort(extmap, extmap_cnt, sizeof(struct extmap), extmap_cmp);
673 * Read a volume configuration file and add the volumes contained within to
674 * the global volume list. If p2 is non-NULL, the file that is opened is
677 * Lines that begin with # and blank lines are ignored.
678 * Volume lines are of the form:
679 * <unix path> [<volume name>] [allow:<user>,<@group>,...] \
680 * [codepage:<file>] [casefold:<num>]
681 * <extension> TYPE [CREATOR]
683 static int readvolfile(obj, p1, p2, user, pwent)
687 struct passwd *pwent;
690 char path[ MAXPATHLEN + 1], tmp[ MAXPATHLEN + 1],
691 volname[ AFPVOL_NAMELEN + 1 ], buf[ BUFSIZ ],
692 type[ 5 ], creator[ 5 ];
695 struct vol_option options[VOLOPT_NUM], save_options[VOLOPT_NUM];
707 if (( fp = fopen( path, "r" )) == NULL ) {
711 memset(save_options, 0, sizeof(save_options));
712 while ( myfgets( buf, sizeof( buf ), fp ) != NULL ) {
713 initline( strlen( buf ), buf );
714 parseline( sizeof( path ) - 1, path );
721 /* change the default options for this file */
722 if (strncmp(path, VOLOPT_DEFAULT, VOLOPT_DEFAULT_LEN) == 0) {
724 for (i = 0; i < VOLOPT_NUM; i++) {
725 if (parseline( sizeof( path ) - VOLOPT_DEFAULT_LEN - 1,
726 path + VOLOPT_DEFAULT_LEN) < 0)
728 volset(save_options, tmp, sizeof(tmp) - 1,
729 obj->options.nlspath, path + VOLOPT_DEFAULT_LEN,
736 if (( p = strchr( path, '/' )) != NULL ) {
744 if ( u == NULL || *u == '\0' || ( pw = getpwnam( u )) == NULL ) {
747 strcpy( tmp, pw->pw_dir );
748 if ( p != NULL && *p != '\0' ) {
752 /* Tag a user's home directory with their umask. Note, this will
753 * be overwritten if the user actually specifies a umask: option
754 * for a '~' volume. */
755 save_options[VOLOPT_UMASK].i_value = obj->options.save_mask;
759 /* send path through variable substitution */
760 if (*path != '~') /* need to copy path to tmp */
763 pwent = getpwnam(obj->username);
764 volxlate(obj, path, sizeof(path) - 1, tmp, pwent, NULL);
766 /* this is sort of braindead. basically, i want to be
767 * able to specify things in any order, but i don't want to
768 * re-write everything.
770 * currently we have 11 options:
778 * options:prodos,crlf,noadouble,ro
781 * namemask:x,y,!z (not implemented yet)
783 memcpy(options, save_options, sizeof(options));
786 /* read in up to 11 possible options */
787 for (i = 0; i < VOLOPT_NUM; i++) {
788 if (parseline( sizeof( tmp ) - 1, tmp ) < 0)
791 volset(options, volname, sizeof(volname) - 1,
792 obj->options.nlspath, tmp, obj, pwent);
795 /* check allow/deny lists:
796 allow -> either no list (-1), or in list (1)
797 deny -> either no list (-1), or not in list (0) */
798 if (accessvol(options[VOLOPT_ALLOW].c_value, obj->username) &&
799 (accessvol(options[VOLOPT_DENY].c_value, obj->username) < 1)) {
801 /* handle read-only behaviour. semantics:
802 * 1) neither the rolist nor the rwlist exist -> rw
803 * 2) rolist exists -> ro if user is in it.
804 * 3) rwlist exists -> ro unless user is in it. */
805 if (((options[VOLOPT_FLAGS].i_value & AFPVOL_RO) == 0) &&
806 ((accessvol(options[VOLOPT_ROLIST].c_value,
807 obj->username) == 1) ||
808 !accessvol(options[VOLOPT_RWLIST].c_value,
810 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
812 /* do variable substitution */
813 volxlate(obj, tmp, sizeof(tmp) - 1, volname, pwent, path);
814 creatvol(path, tmp, options);
816 volfree(options, save_options);
820 parseline( sizeof( type ) - 1, type );
821 parseline( sizeof( creator ) - 1, creator );
822 setextmap( path, type, creator, user);
829 volfree(save_options, NULL);
831 if ( fclose( fp ) != 0 ) {
832 LOG(log_error, logtype_afpd, "readvolfile: fclose: %s", strerror(errno) );
838 static void load_volumes(AFPObj *obj)
840 struct passwd *pwent = getpwnam(obj->username);
842 if ( (obj->options.flags & OPTION_USERVOLFIRST) == 0 ) {
843 readvolfile(obj, obj->options.systemvol, NULL, 0, pwent);
846 if ((*obj->username == '\0') || (obj->options.flags & OPTION_NOUSERVOL)) {
847 readvolfile(obj, obj->options.defaultvol, NULL, 1, pwent);
850 * Read user's AppleVolumes or .AppleVolumes file
851 * If neither are readable, read the default volumes file. if
852 * that doesn't work, create a user share.
854 if ( readvolfile(obj, pwent->pw_dir, "AppleVolumes", 1, pwent) < 0 &&
855 readvolfile(obj, pwent->pw_dir, ".AppleVolumes", 1, pwent) < 0 &&
856 readvolfile(obj, pwent->pw_dir, "applevolumes", 1, pwent) < 0 &&
857 readvolfile(obj, pwent->pw_dir, ".applevolumes", 1, pwent) < 0 &&
858 obj->options.defaultvol != NULL ) {
859 if (readvolfile(obj, obj->options.defaultvol, NULL, 1, pwent) < 0)
860 creatvol(pwent->pw_dir, NULL, NULL);
863 if ( obj->options.flags & OPTION_USERVOLFIRST ) {
864 readvolfile(obj, obj->options.systemvol, NULL, 0, pwent );
868 static int getvolspace( vol, bfree, btotal, xbfree, xbtotal, bsize )
870 u_int32_t *bfree, *btotal, *bsize;
871 VolSpace *xbfree, *xbtotal;
875 #ifndef NO_QUOTA_SUPPORT
876 VolSpace qfree, qtotal;
877 #endif /* ! NO_QUOTA_SUPPORT */
879 spaceflag = AFPVOL_GVSMASK & vol->v_flags;
880 /* report up to 2GB if afp version is < 2.2 (4GB if not) */
881 maxsize = (vol->v_flags & AFPVOL_A2VOL) ? 0x01fffe00 :
882 (((afp_version < 22) || (vol->v_flags & AFPVOL_LIMITSIZE))
883 ? 0x7fffffffL : 0xffffffffL);
886 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_AFSGVS ) {
887 if ( afs_getvolspace( vol, xbfree, xbtotal, bsize ) == AFP_OK ) {
888 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_AFSGVS;
889 goto getvolspace_done;
894 if (( rc = ustatfs_getvolspace( vol, xbfree, xbtotal,
895 bsize)) != AFP_OK ) {
899 #define min(a,b) ((a)<(b)?(a):(b))
900 #ifndef NO_QUOTA_SUPPORT
901 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_UQUOTA ) {
902 if ( uquota_getvolspace( vol, &qfree, &qtotal, *bsize ) == AFP_OK ) {
903 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_UQUOTA;
904 *xbfree = min(*xbfree, qfree);
905 *xbtotal = min( *xbtotal, qtotal);
906 goto getvolspace_done;
909 #endif /* ! NO_QUOTA_SUPPORT */
910 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_USTATFS;
913 *bfree = min( *xbfree, maxsize);
914 *btotal = min( *xbtotal, maxsize);
918 static int getvolparams( bitmap, vol, st, buf, buflen )
926 int bit = 0, isad = 1;
929 u_int32_t bfree, btotal, bsize;
930 VolSpace xbfree, xbtotal; /* extended bytes */
931 char *data, *nameoff = NULL;
934 /* courtesy of jallison@whistle.com:
935 * For MacOS8.x support we need to create the
936 * .Parent file here if it doesn't exist. */
938 memset(&ad, 0, sizeof(ad));
939 if ( ad_open( vol->v_path, vol_noadouble(vol) |
940 ADFLAGS_HF|ADFLAGS_DIR, O_RDWR | O_CREAT,
944 } else if (ad_getoflags( &ad, ADFLAGS_HF ) & O_CREAT) {
945 slash = strrchr( vol->v_path, '/' );
951 ad_setentrylen( &ad, ADEID_NAME, strlen( slash ));
952 memcpy(ad_entry( &ad, ADEID_NAME ), slash,
953 ad_getentrylen( &ad, ADEID_NAME ));
954 ad_setdate(&ad, AD_DATE_CREATE | AD_DATE_UNIX, st->st_mtime);
955 ad_flush(&ad, ADFLAGS_HF);
958 if (( bitmap & ( (1<<VOLPBIT_BFREE)|(1<<VOLPBIT_BTOTAL) |
959 (1<<VOLPBIT_XBFREE)|(1<<VOLPBIT_XBTOTAL) |
960 (1<<VOLPBIT_BSIZE)) ) != 0 ) {
961 if ( getvolspace( vol, &bfree, &btotal, &xbfree, &xbtotal,
964 ad_close( &ad, ADFLAGS_HF );
966 return( AFPERR_PARAM );
971 while ( bitmap != 0 ) {
972 while (( bitmap & 1 ) == 0 ) {
981 if (0 == (vol->v_flags & AFPVOL_NOFILEID)) {
982 ashort = VOLPBIT_ATTR_FILEID;
985 /* check for read-only.
986 * NOTE: we don't actually set the read-only flag unless
987 * it's passed in that way as it's possible to mount
988 * a read-write filesystem under a read-only one. */
989 if ((vol->v_flags & AFPVOL_RO) ||
990 ((utime(vol->v_path, NULL) < 0) && (errno == EROFS))) {
991 ashort |= VOLPBIT_ATTR_RO;
993 ashort |= VOLPBIT_ATTR_CATSEARCH;
994 if (afp_version >= 30) {
995 ashort |= VOLPBIT_ATTR_UTF8;
997 ashort = htons(ashort);
998 memcpy(data, &ashort, sizeof( ashort ));
999 data += sizeof( ashort );
1003 ashort = htons( AFPVOLSIG_DEFAULT );
1004 memcpy(data, &ashort, sizeof( ashort ));
1005 data += sizeof( ashort );
1008 case VOLPBIT_CDATE :
1009 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
1010 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1011 memcpy(data, &aint, sizeof( aint ));
1012 data += sizeof( aint );
1015 case VOLPBIT_MDATE :
1016 if ( st->st_mtime > vol->v_time ) {
1017 vol->v_time = st->st_mtime;
1018 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1020 aint = AD_DATE_FROM_UNIX(vol->v_time);
1022 memcpy(data, &aint, sizeof( aint ));
1023 data += sizeof( aint );
1026 case VOLPBIT_BDATE :
1027 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1028 aint = AD_DATE_START;
1029 memcpy(data, &aint, sizeof( aint ));
1030 data += sizeof( aint );
1034 memcpy(data, &vol->v_vid, sizeof( vol->v_vid ));
1035 data += sizeof( vol->v_vid );
1038 case VOLPBIT_BFREE :
1039 bfree = htonl( bfree );
1040 memcpy(data, &bfree, sizeof( bfree ));
1041 data += sizeof( bfree );
1044 case VOLPBIT_BTOTAL :
1045 btotal = htonl( btotal );
1046 memcpy(data, &btotal, sizeof( btotal ));
1047 data += sizeof( btotal );
1050 #ifndef NO_LARGE_VOL_SUPPORT
1051 case VOLPBIT_XBFREE :
1052 xbfree = hton64( xbfree );
1053 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1054 bcopy(&xbfree, data, sizeof(xbfree));
1055 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1056 memcpy(data, &xbfree, sizeof( xbfree ));
1057 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1058 data += sizeof( xbfree );
1061 case VOLPBIT_XBTOTAL :
1062 xbtotal = hton64( xbtotal );
1063 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1064 bcopy(&xbtotal, data, sizeof(xbtotal));
1065 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1066 memcpy(data, &xbtotal, sizeof( xbtotal ));
1067 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1068 data += sizeof( xbfree );
1070 #endif /* ! NO_LARGE_VOL_SUPPORT */
1074 data += sizeof( u_int16_t );
1077 case VOLPBIT_BSIZE: /* block size */
1078 bsize = htonl(bsize);
1079 memcpy(data, &bsize, sizeof(bsize));
1080 data += sizeof(bsize);
1085 ad_close( &ad, ADFLAGS_HF );
1087 return( AFPERR_BITMAP );
1093 ashort = htons( data - buf );
1094 memcpy(nameoff, &ashort, sizeof( ashort ));
1095 aint = strlen( vol->v_name );
1097 memcpy(data, vol->v_name, aint );
1101 ad_close( &ad, ADFLAGS_HF );
1103 *buflen = data - buf;
1109 int afp_getsrvrparms(obj, ibuf, ibuflen, rbuf, rbuflen )
1112 int ibuflen, *rbuflen;
1125 for ( vcnt = 0, volume = volumes; volume; volume = volume->v_next ) {
1126 if ( stat( volume->v_path, &st ) < 0 ) {
1127 LOG(log_info, logtype_afpd, "afp_getsrvrparms: stat %s: %s",
1128 volume->v_path, strerror(errno) );
1129 continue; /* can't access directory */
1131 if (!S_ISDIR(st.st_mode)) {
1132 continue; /* not a dir */
1135 /* set password bit if there's a volume password */
1136 *data = (volume->v_password) ? AFPSRVR_PASSWD : 0;
1138 /* Apple 2 clients running ProDOS-8 expect one volume to have
1139 bit 0 of this byte set. They will not recognize anything
1140 on the server unless this is the case. I have not
1141 completely worked this out, but it's related to booting
1142 from the server. Support for that function is a ways
1143 off.. <shirsch@ibm.net> */
1144 *data++ |= (volume->v_flags & AFPVOL_A2VOL) ? AFPSRVR_CONFIGINFO : 0;
1145 len = strlen( volume->v_name );
1147 memcpy(data, volume->v_name, len );
1152 *rbuflen = data - rbuf;
1154 if ( gettimeofday( &tv, 0 ) < 0 ) {
1155 LOG(log_error, logtype_afpd, "afp_getsrvrparms: gettimeofday: %s", strerror(errno) );
1157 return AFPERR_PARAM;
1159 tv.tv_sec = AD_DATE_FROM_UNIX(tv.tv_sec);
1160 memcpy(data, &tv.tv_sec, sizeof( u_int32_t));
1161 data += sizeof( u_int32_t);
1166 int afp_openvol(obj, ibuf, ibuflen, rbuf, rbuflen )
1169 int ibuflen, *rbuflen;
1175 #endif /* CNID_DB */
1178 int len, ret, buflen;
1182 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1183 bitmap = ntohs( bitmap );
1184 ibuf += sizeof( bitmap );
1185 if (( bitmap & (1<<VOLPBIT_VID)) == 0 ) {
1186 ret = AFPERR_BITMAP;
1190 len = (unsigned char)*ibuf++;
1191 volname = obj->oldtmp;
1192 memcpy(volname, ibuf, len );
1193 *(volname + len) = '\0';
1195 if ((len + 1) & 1) /* pad to an even boundary */
1201 for ( volume = volumes; volume; volume = volume->v_next ) {
1202 if ( strcasecmp( volname, volume->v_name ) == 0 ) {
1207 if ( volume == NULL ) {
1212 /* check for a volume password */
1213 if (volume->v_password &&
1214 strncmp(ibuf, volume->v_password, VOLPASSLEN)) {
1215 ret = AFPERR_ACCESS;
1220 if (afp_version >= 30)
1221 volume->max_filename = 255;
1223 volume->max_filename = MACFILELEN;
1225 if (( volume->v_flags & AFPVOL_OPEN ) == 0 ) {
1226 /* FIXME unix name != mac name */
1227 if ((dir = dirnew(volume->v_name, volume->v_name) ) == NULL) {
1228 LOG(log_error, logtype_afpd, "afp_openvol: malloc: %s", strerror(errno) );
1232 dir->d_did = DIRDID_ROOT;
1233 dir->d_color = DIRTREE_COLOR_BLACK; /* root node is black */
1234 volume->v_dir = volume->v_root = dir;
1235 volume->v_flags |= AFPVOL_OPEN;
1238 set_uidgid ( volume );
1239 #endif /* FORCE_UIDGID */
1241 if ( stat( volume->v_path, &st ) < 0 ) {
1246 buflen = *rbuflen - sizeof( bitmap );
1247 if (( ret = getvolparams( bitmap, volume, &st,
1248 rbuf + sizeof(bitmap), &buflen )) != AFP_OK ) {
1251 *rbuflen = buflen + sizeof( bitmap );
1252 bitmap = htons( bitmap );
1253 memcpy(rbuf, &bitmap, sizeof( bitmap ));
1255 if ( chdir( volume->v_path ) < 0 ) {
1259 curdir = volume->v_dir;
1262 if (volume->v_dbpath)
1263 volume->v_db = cnid_open (volume->v_dbpath, volume->v_umask);
1264 if (volume->v_db == NULL)
1265 volume->v_db = cnid_open (volume->v_path, volume->v_umask);
1266 #endif /* CNID_DB */
1270 * If you mount a volume twice, the second time the trash appears on
1271 * the desk-top. That's because the Mac remembers the DID for the
1272 * trash (even for volumes in different zones, on different servers).
1273 * Just so this works better, we prime the DID cache with the trash,
1274 * fixing the trash at DID 17.
1277 cname( volume, volume->v_dir, &p );
1278 #endif /* CNID_DB */
1287 int afp_closevol(obj, ibuf, ibuflen, rbuf, rbuflen )
1290 int ibuflen, *rbuflen;
1292 struct vol *vol, *ovol;
1297 memcpy(&vid, ibuf, sizeof( vid ));
1298 if (( vol = getvolbyvid( vid )) == NULL ) {
1299 return( AFPERR_PARAM );
1302 vol->v_flags &= ~AFPVOL_OPEN;
1303 for ( ovol = volumes; ovol; ovol = ovol->v_next ) {
1304 if ( ovol->v_flags & AFPVOL_OPEN ) {
1308 if ( ovol != NULL ) {
1309 /* Even if chdir fails, we can't say afp_closevol fails. */
1310 if ( chdir( ovol->v_path ) == 0 ) {
1311 curdir = ovol->v_dir;
1315 dirfree( vol->v_root );
1318 cnid_close(vol->v_db);
1320 #endif /* CNID_DB */
1324 struct vol *getvolbyvid(const u_int16_t vid )
1328 for ( vol = volumes; vol; vol = vol->v_next ) {
1329 if ( vid == vol->v_vid ) {
1333 if ( vol == NULL || ( vol->v_flags & AFPVOL_OPEN ) == 0 ) {
1339 #endif /* FORCE_UIDGID */
1344 /* ------------------------ */
1345 static int ext_cmp_key(const void *key, const void *obj)
1347 const char *p = key;
1348 const struct extmap *em = obj;
1349 return strdiacasecmp(p, em->em_ext);
1351 struct extmap *getextmap(const char *path)
1356 if (( p = strrchr( path, '.' )) == NULL ) {
1357 return( defextmap );
1360 if (!*p || !extmap_cnt) {
1361 return( defextmap );
1363 em = bsearch(p, extmap, extmap_cnt, sizeof(struct extmap), ext_cmp_key);
1367 return( defextmap );
1371 struct extmap *getdefextmap(void)
1373 return( defextmap );
1377 poll if a volume is changed by other processes.
1379 int pollvoltime(obj)
1386 if (!(afp_version > 21 && obj->options.server_notif))
1389 if ( gettimeofday( &tv, 0 ) < 0 )
1392 for ( vol = volumes; vol; vol = vol->v_next ) {
1393 if ( (vol->v_flags & AFPVOL_OPEN) && vol->v_time + 30 < tv.tv_sec) {
1394 if ( !stat( vol->v_path, &st ) && vol->v_time != st.st_mtime ) {
1395 vol->v_time = st.st_mtime;
1396 obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED);
1404 void setvoltime(obj, vol )
1410 /* just looking at vol->v_time is broken seriously since updates
1411 * from other users afpd processes never are seen.
1412 * This is not the most elegant solution (a shared memory between
1413 * the afpd processes would come closer)
1416 if ( gettimeofday( &tv, 0 ) < 0 ) {
1417 LOG(log_error, logtype_afpd, "setvoltime: gettimeofday: %s", strerror(errno) );
1420 if( utime( vol->v_path, NULL ) < 0 ) {
1421 /* write of time failed ... probably a read only filesys,
1422 * where no other users can interfere, so there's no issue here
1426 /* a little granularity */
1427 if (vol->v_time < tv.tv_sec) {
1428 vol->v_time = tv.tv_sec;
1429 if (afp_version > 21 && obj->options.server_notif) {
1430 obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED);
1435 int afp_getvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1438 int ibuflen, *rbuflen;
1443 u_int16_t vid, bitmap;
1446 memcpy(&vid, ibuf, sizeof( vid ));
1447 ibuf += sizeof( vid );
1448 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1449 bitmap = ntohs( bitmap );
1451 if (( vol = getvolbyvid( vid )) == NULL ) {
1453 return( AFPERR_PARAM );
1456 if ( stat( vol->v_path, &st ) < 0 ) {
1458 return( AFPERR_PARAM );
1461 buflen = *rbuflen - sizeof( bitmap );
1462 if (( ret = getvolparams( bitmap, vol, &st,
1463 rbuf + sizeof( bitmap ), &buflen )) != AFP_OK ) {
1467 *rbuflen = buflen + sizeof( bitmap );
1468 bitmap = htons( bitmap );
1469 memcpy(rbuf, &bitmap, sizeof( bitmap ));
1473 int afp_setvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1476 int ibuflen, *rbuflen;
1480 u_int16_t vid, bitmap;
1486 memcpy(&vid, ibuf, sizeof( vid ));
1487 ibuf += sizeof( vid );
1488 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1489 bitmap = ntohs( bitmap );
1490 ibuf += sizeof(bitmap);
1492 if (( vol = getvolbyvid( vid )) == NULL ) {
1493 return( AFPERR_PARAM );
1496 if (vol->v_flags & AFPVOL_RO)
1497 return AFPERR_VLOCK;
1499 /* we can only set the backup date. */
1500 if (bitmap != VOLPBIT_BDATE)
1501 return AFPERR_BITMAP;
1503 memset(&ad, 0, sizeof(ad));
1504 if ( ad_open( vol->v_path, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR,
1507 return AFPERR_VLOCK;
1509 return AFPERR_ACCESS;
1512 memcpy(&aint, ibuf, sizeof(aint));
1513 ad_setdate(&ad, AD_DATE_BACKUP, aint);
1514 ad_flush(&ad, ADFLAGS_HF);
1515 ad_close(&ad, ADFLAGS_HF);
1520 int wincheck(const struct vol *vol, const char *path)
1524 if (!(vol->v_flags & AFPVOL_MSWINDOWS))
1527 /* empty paths are not allowed */
1528 if ((len = strlen(path)) == 0)
1531 /* leading or trailing whitespaces are not allowed, carriage returns
1532 * and probably other whitespace is okay, tabs are not allowed
1534 if ((path[0] == ' ') || (path[len-1] == ' '))
1537 /* certain characters are not allowed */
1538 if (strpbrk(path, MSWINDOWS_BADCHARS))
1541 /* everything else is okay */