2 * $Id: volume.c,v 1.12 2001-09-04 13:52:45 rufustfirefly 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 <sys/syslog.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 */
42 #include "directory.h"
49 #define MIN(a, b) ((a) < (b) ? (a) : (b))
52 #ifndef NO_LARGE_VOL_SUPPORT
53 #if BYTE_ORDER == BIG_ENDIAN
56 #else /* BYTE_ORDER == BIG_ENDIAN */
57 #define hton64(x) ((u_int64_t) (htonl(((x) >> 32) & 0xffffffffLL)) | \
58 (u_int64_t) ((htonl(x) & 0xffffffffLL) << 32))
59 #define ntoh64(x) (hton64(x))
60 #endif /* BYTE_ORDER == BIG_ENDIAN */
61 #endif /* ! NO_LARGE_VOL_SUPPORT */
63 static struct vol *volumes = NULL;
64 static int lastvid = 0;
66 static char *Trash = "\02\024Network Trash Folder";
68 static struct extmap *extmap = NULL, *defextmap = NULL;
70 #define VOLOPT_ALLOW 0 /* user allow list */
71 #define VOLOPT_DENY 1 /* user deny list */
72 #define VOLOPT_RWLIST 2 /* user rw list */
73 #define VOLOPT_ROLIST 3 /* user ro list */
74 #define VOLOPT_CODEPAGE 4 /* codepage */
75 #define VOLOPT_PASSWORD 5 /* volume password */
76 #define VOLOPT_CASEFOLD 6 /* character case mangling */
77 #define VOLOPT_FLAGS 7 /* various flags */
78 #define VOLOPT_DBPATH 8 /* path to database */
79 #define VOLOPT_MAPCHARS 9 /* does mtou and utom mappings. syntax:
80 m and u can be double-byte hex
85 !u -> make u illegal always
86 ~u -> make u illegal only as the first
87 part of a double-byte character.
89 #define VOLOPT_VETO 10 /* list of veto filespec */
92 #define VOLOPT_FORCEUID 11 /* force uid for username x */
93 #define VOLOPT_FORCEGID 12 /* force gid for group x */
95 #else /* normally, there are only 9 possible options */
97 #endif /* FORCE_UIDGID */
99 #define VOLOPT_NUM (VOLOPT_MAX + 1)
102 #define VOLOPT_DEFAULT ":DEFAULT:"
103 #define VOLOPT_DEFAULT_LEN 9
109 static __inline__ void volfree(struct vol_option *options,
110 const struct vol_option *save)
115 for (i = 0; i < VOLOPT_MAX; i++) {
116 if (options[i].c_value && (options[i].c_value != save[i].c_value))
117 free(options[i].c_value);
120 for (i = 0; i < VOLOPT_MAX; i++) {
121 if (options[i].c_value)
122 free(options[i].c_value);
128 /* handle variable substitutions. here's what we understand:
129 * $c -> client ip/appletalk address
130 * $f -> full name (whatever's in the gecos field)
133 * $s -> server name (hostname if it doesn't exist)
134 * $u -> username (guest is usually nobody)
135 * $v -> volume name (ADEID_NAME or basename)
136 * $z -> zone (may not exist)
139 #define is_var(a, b) (strncmp((a), (b), 2) == 0)
140 static void volxlate(AFPObj *obj, char *dest, int destlen,
141 char *src, struct passwd *pwd, char *path)
146 strncpy(dest, src, destlen);
147 if ((p = strchr(src, '$')) == NULL) /* nothing to do */
150 /* first part of the path. just forward to the next variable. */
151 len = MIN(p - src, destlen);
157 while (p && destlen > 0) {
158 /* now figure out what the variable is */
160 if (is_var(p, "$c")) {
161 if (obj->proto == AFPPROTO_ASP) {
162 ASP asp = obj->handle;
164 len = sprintf(dest, "%u.%u", ntohs(asp->asp_sat.sat_addr.s_net),
165 asp->asp_sat.sat_addr.s_node);
169 } else if (obj->proto == AFPPROTO_DSI) {
170 DSI *dsi = obj->handle;
172 len = sprintf(dest, "%s:%u", inet_ntoa(dsi->client.sin_addr),
173 ntohs(dsi->client.sin_port));
177 } else if (is_var(p, "$f")) {
178 if ((q = strchr(pwd->pw_gecos, ',')))
181 } else if (is_var(p, "$g")) {
182 struct group *grp = getgrgid(pwd->pw_gid);
185 } else if (is_var(p, "$h")) {
186 q = obj->options.hostname;
187 } else if (is_var(p, "$s")) {
190 else if (obj->options.server) {
191 q = obj->options.server;
193 q = obj->options.hostname;
194 } else if (is_var(p, "$u")) {
196 } else if (is_var(p, "$v")) {
200 memset(&ad, 0, sizeof(ad));
201 if (ad_open(path, ADFLAGS_HF, O_RDONLY, 0, &ad) < 0)
204 if ((len = MIN(ad_getentrylen(&ad, ADEID_NAME), destlen)) > 0) {
205 memcpy(dest, ad_entry(&ad, ADEID_NAME), len);
206 ad_close(&ad, ADFLAGS_HF);
210 ad_close(&ad, ADFLAGS_HF);
211 no_volname: /* simple basename */
212 if ((q = strrchr(path, '/')) == NULL)
214 else if (*(q + 1) != '\0')
218 } else if (is_var(p, "$z")) {
220 } else if (is_var(p, "$$")) {
225 /* copy the stuff over. if we don't understand something that we
226 * should, just skip it over. */
228 len = MIN(p == q ? 2 : strlen(q), destlen);
229 strncpy(dest, q, len);
234 /* stuff up to next $ */
236 p = strchr(src, '$');
237 len = p ? MIN(p - src, destlen) : destlen;
239 strncpy(dest, src, len);
246 /* to make sure that val is valid, make sure to select an opt that
248 #define optionok(buf,opt,val) (strstr((buf),(opt)) && ((val)[1] != '\0'))
250 static __inline__ char *get_codepage_path(const char *path, const char *name)
256 page = (char *) malloc((len = strlen(path)) + strlen(name) + 2);
259 if (path[len - 1] != '/') /* add a / */
267 /* debug: show which codepage directory we are using */
268 syslog(LOG_DEBUG, "using codepage directory: %s", page);
273 /* handle all the options. tmp can't be NULL. */
274 static void volset(struct vol_option *options, char *volname, int vlen,
275 const char *nlspath, const char *tmp)
279 val = strchr(tmp, ':');
280 if (optionok(tmp, "allow:", val)) {
281 if (options[VOLOPT_ALLOW].c_value)
282 free(options[VOLOPT_ALLOW].c_value);
283 options[VOLOPT_ALLOW].c_value = strdup(val + 1);
285 } else if (optionok(tmp, "deny:", val)) {
286 if (options[VOLOPT_DENY].c_value)
287 free(options[VOLOPT_DENY].c_value);
288 options[VOLOPT_DENY].c_value = strdup(val + 1);
290 } else if (optionok(tmp, "rwlist:", val)) {
291 if (options[VOLOPT_RWLIST].c_value)
292 free(options[VOLOPT_RWLIST].c_value);
293 options[VOLOPT_RWLIST].c_value = strdup(val + 1);
295 } else if (optionok(tmp, "rolist:", val)) {
296 if (options[VOLOPT_ROLIST].c_value)
297 free(options[VOLOPT_ROLIST].c_value);
298 options[VOLOPT_ROLIST].c_value = strdup(val + 1);
300 } else if (optionok(tmp, "codepage:", val)) {
301 if (options[VOLOPT_CODEPAGE].c_value)
302 free(options[VOLOPT_CODEPAGE].c_value);
303 options[VOLOPT_CODEPAGE].c_value = get_codepage_path(nlspath, val + 1);
305 } else if (optionok(tmp, "veto:", val)) {
306 if (options[VOLOPT_VETO].c_value)
307 free(options[VOLOPT_VETO].c_value);
308 options[VOLOPT_VETO].c_value = strdup(val + 1);
310 } else if (optionok(tmp, "casefold:", val)) {
311 if (strcasecmp(val + 1, "tolower") == 0)
312 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMLOWER;
313 else if (strcasecmp(val + 1, "toupper") == 0)
314 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMUPPER;
315 else if (strcasecmp(val + 1, "xlatelower") == 0)
316 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UUPPERMLOWER;
317 else if (strcasecmp(val + 1, "xlateupper") == 0)
318 options[VOLOPT_CASEFOLD].i_value = AFPVOL_ULOWERMUPPER;
320 } else if (optionok(tmp, "options:", val)) {
323 if ((p = strtok(val + 1, ",")) == NULL) /* nothing */
327 if (strcasecmp(p, "prodos") == 0)
328 options[VOLOPT_FLAGS].i_value |= AFPVOL_A2VOL;
329 else if (strcasecmp(p, "mswindows") == 0) {
330 options[VOLOPT_FLAGS].i_value |= AFPVOL_MSWINDOWS;
331 if (!options[VOLOPT_CODEPAGE].c_value)
332 options[VOLOPT_CODEPAGE].c_value =
333 get_codepage_path(nlspath, MSWINDOWS_CODEPAGE);
335 } else if (strcasecmp(p, "crlf") == 0)
336 options[VOLOPT_FLAGS].i_value |= AFPVOL_CRLF;
337 else if (strcasecmp(p, "noadouble") == 0)
338 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOADOUBLE;
339 else if (strcasecmp(p, "ro") == 0)
340 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
341 else if (strcasecmp(p, "nohex") == 0)
342 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOHEX;
343 else if (strcasecmp(p, "usedots") == 0)
344 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS;
345 else if (strcasecmp(p, "limitsize") == 0)
346 options[VOLOPT_FLAGS].i_value |= AFPVOL_LIMITSIZE;
347 /* support for either "dropbox" or "dropkludge" */
348 else if (strcasecmp(p, "dropbox") == 0)
349 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
350 else if (strcasecmp(p, "dropkludge") == 0)
351 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
353 p = strtok(NULL, ",");
357 } else if (optionok(tmp, "dbpath:", val)) {
358 if (options[VOLOPT_DBPATH].c_value)
359 free(options[VOLOPT_DBPATH].c_value);
361 options[VOLOPT_DBPATH].c_value = strdup(val + 1);
363 } else if (optionok(tmp, "mapchars:",val)) {
364 if (options[VOLOPT_MAPCHARS].c_value)
365 free(options[VOLOPT_MAPCHARS].c_value);
366 options[VOLOPT_MAPCHARS].c_value = strdup(val + 1);
368 } else if (optionok(tmp, "password:", val)) {
369 if (options[VOLOPT_PASSWORD].c_value)
370 free(options[VOLOPT_PASSWORD].c_value);
371 options[VOLOPT_PASSWORD].c_value = strdup(val + 1);
375 /* this code allows forced uid/gid per volume settings */
376 } else if (optionok(tmp, "forceuid:", val)) {
377 if (options[VOLOPT_FORCEUID].c_value)
378 free(options[VOLOPT_FORCEUID].c_value);
379 options[VOLOPT_FORCEUID].c_value = strdup(val + 1);
380 } else if (optionok(tmp, "forcegid:", val)) {
381 if (options[VOLOPT_FORCEGID].c_value)
382 free(options[VOLOPT_FORCEGID].c_value);
383 options[VOLOPT_FORCEGID].c_value = strdup(val + 1);
385 #endif /* FORCE_UIDGID */
388 /* ignore unknown options */
389 syslog(LOG_DEBUG, "ignoring unknown volume option: %s", tmp);
392 /* we'll assume it's a volume name. */
393 strncpy(volname, tmp, vlen);
397 static int creatvol(const char *path, char *name, struct vol_option *options)
402 if ( name == NULL || *name == '\0' ) {
403 if ((name = strrchr( path, '/' )) == NULL) {
404 return -1; /* Obviously not a fully qualified path */
407 /* if you wish to share /, you need to specify a name. */
412 for ( volume = volumes; volume; volume = volume->v_next ) {
413 if ( strcasecmp( volume->v_name, name ) == 0 ) {
414 return -1; /* Won't be able to access it, anyway... */
418 vlen = strlen( name );
419 if ( vlen > AFPVOL_NAMELEN ) {
420 vlen = AFPVOL_NAMELEN;
421 name[AFPVOL_NAMELEN] = '\0';
425 (struct vol *)calloc(1, sizeof( struct vol ))) == NULL ) {
426 syslog( LOG_ERR, "creatvol: malloc: %s", strerror(errno) );
429 if (( volume->v_name =
430 (char *)malloc( vlen + 1 )) == NULL ) {
431 syslog( LOG_ERR, "creatvol: malloc: %s", strerror(errno) );
435 if (( volume->v_path =
436 (char *)malloc( strlen( path ) + 1 )) == NULL ) {
437 syslog( LOG_ERR, "creatvol: malloc: %s", strerror(errno) );
438 free(volume->v_name);
443 strcpy( volume->v_name, name);
444 strcpy( volume->v_path, path );
448 #endif /* __svr4__ */
449 volume->v_vid = lastvid++;
450 volume->v_lastdid = 3;
454 /* should we casefold? */
455 volume->v_casefold = options[VOLOPT_CASEFOLD].i_value;
457 /* shift in some flags */
458 volume->v_flags = options[VOLOPT_FLAGS].i_value;
460 /* read in the code pages */
461 if (options[VOLOPT_CODEPAGE].c_value)
462 codepage_read(volume, options[VOLOPT_CODEPAGE].c_value);
464 if (options[VOLOPT_PASSWORD].c_value)
465 volume->v_password = strdup(options[VOLOPT_PASSWORD].c_value);
467 if (options[VOLOPT_VETO].c_value)
468 volume->v_veto = strdup(options[VOLOPT_VETO].c_value);
471 if (options[VOLOPT_DBPATH].c_value)
472 volume->v_dbpath = strdup(options[VOLOPT_DBPATH].c_value);
477 if (options[VOLOPT_FORCEUID].c_value) {
478 volume->v_forceuid = strdup(options[VOLOPT_FORCEUID].c_value);
480 volume->v_forceuid = NULL; /* set as null so as to return 0 later on */
483 if (options[VOLOPT_FORCEGID].c_value) {
484 volume->v_forcegid = strdup(options[VOLOPT_FORCEGID].c_value);
486 volume->v_forcegid = NULL; /* set as null so as to return 0 later on */
489 #endif /* FORCE_UIDGID */
493 volume->v_next = volumes;
498 static char *myfgets( buf, size, fp )
507 while ((( c = getc( fp )) != EOF ) && ( size > 0 )) {
508 if ( c == '\n' || c == '\r' ) {
526 /* check access list. this function wants something of the following
528 * @group,name,name2,@group2,name3
530 * a NULL argument allows everybody to have access.
531 * we return three things:
533 * 0: list exists, but name isn't in it
536 static int accessvol(args, name)
540 char buf[MAXPATHLEN + 1], *p;
546 strncpy(buf, args, sizeof(buf));
547 if ((p = strtok(buf, ",")) == NULL) /* nothing, return okay */
551 if (*p == '@') { /* it's a group */
552 if ((gr = getgrnam(p + 1)) && gmem(gr->gr_gid))
554 } else if (strcmp(p, name) == 0) /* it's a user name */
556 p = strtok(NULL, ",");
562 static void setextmap( ext, type, creator, user)
563 char *ext, *type, *creator;
568 for ( em = extmap; em; em = em->em_next ) {
569 if ( strdiacasecmp( em->em_ext, ext ) == 0 ) {
576 (struct extmap *)malloc( sizeof( struct extmap ))) == NULL ) {
577 syslog( LOG_ERR, "setextmap: malloc: %s", strerror(errno) );
580 em->em_next = extmap;
582 } else if ( !user ) {
586 strcpy( em->em_ext, ext );
588 if ( *type == '\0' ) {
589 memcpy(em->em_type, "????", sizeof( em->em_type ));
591 memcpy(em->em_type, type, sizeof( em->em_type ));
593 if ( *creator == '\0' ) {
594 memcpy(em->em_creator, "UNIX", sizeof( em->em_creator ));
596 memcpy(em->em_creator, creator, sizeof( em->em_creator ));
599 if ( strcmp( ext, "." ) == 0 ) {
605 * Read a volume configuration file and add the volumes contained within to
606 * the global volume list. If p2 is non-NULL, the file that is opened is
609 * Lines that begin with # and blank lines are ignored.
610 * Volume lines are of the form:
611 * <unix path> [<volume name>] [allow:<user>,<@group>,...] \
612 * [codepage:<file>] [casefold:<num>]
613 * <extension> TYPE [CREATOR]
615 static int readvolfile(obj, p1, p2, user, pwent)
619 struct passwd *pwent;
622 char path[ MAXPATHLEN + 1], tmp[ MAXPATHLEN + 1],
623 volname[ AFPVOL_NAMELEN + 1 ], buf[ BUFSIZ ],
624 type[ 5 ], creator[ 5 ];
627 struct vol_option options[VOLOPT_NUM], save_options[VOLOPT_NUM];
639 if (( fp = fopen( path, "r" )) == NULL ) {
643 memset(save_options, 0, sizeof(save_options));
644 while ( myfgets( buf, sizeof( buf ), fp ) != NULL ) {
645 initline( strlen( buf ), buf );
646 parseline( sizeof( path ) - 1, path );
653 /* change the default options for this file */
654 if (strncmp(path, VOLOPT_DEFAULT, VOLOPT_DEFAULT_LEN) == 0) {
656 for (i = 0; i < VOLOPT_NUM; i++) {
657 if (parseline( sizeof( path ) - VOLOPT_DEFAULT_LEN - 1,
658 path + VOLOPT_DEFAULT_LEN) < 0)
660 volset(save_options, tmp, sizeof(tmp) - 1,
661 obj->options.nlspath, path + VOLOPT_DEFAULT_LEN);
667 if (( p = strchr( path, '/' )) != NULL ) {
675 if ( u == NULL || *u == '\0' || ( pw = getpwnam( u )) == NULL ) {
678 strcpy( tmp, pw->pw_dir );
679 if ( p != NULL && *p != '\0' ) {
686 /* send path through variable substitution */
687 if (*path != '~') /* need to copy path to tmp */
690 pwent = getpwnam(obj->username);
691 volxlate(obj, path, sizeof(path) - 1, tmp, pwent, NULL);
693 /* this is sort of braindead. basically, i want to be
694 * able to specify things in any order, but i don't want to
695 * re-write everything.
697 * currently we have 11 options:
705 * options:prodos,crlf,noadouble,ro
708 * namemask:x,y,!z (not implemented yet)
710 memcpy(options, save_options, sizeof(options));
713 /* read in up to 11 possible options */
714 for (i = 0; i < VOLOPT_NUM; i++) {
715 if (parseline( sizeof( tmp ) - 1, tmp ) < 0)
718 volset(options, volname, sizeof(volname) - 1,
719 obj->options.nlspath, tmp);
722 /* check allow/deny lists:
723 allow -> either no list (-1), or in list (1)
724 deny -> either no list (-1), or not in list (0) */
725 if (accessvol(options[VOLOPT_ALLOW].c_value, obj->username) &&
726 (accessvol(options[VOLOPT_DENY].c_value, obj->username) < 1)) {
728 /* handle read-only behaviour. semantics:
729 * 1) neither the rolist nor the rwlist exist -> rw
730 * 2) rolist exists -> ro if user is in it.
731 * 3) rwlist exists -> ro unless user is in it. */
732 if (((options[VOLOPT_FLAGS].i_value & AFPVOL_RO) == 0) &&
733 ((accessvol(options[VOLOPT_ROLIST].c_value,
734 obj->username) == 1) ||
735 !accessvol(options[VOLOPT_RWLIST].c_value,
737 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
739 /* do variable substitution */
740 volxlate(obj, tmp, sizeof(tmp) - 1, volname, pwent, path);
741 creatvol(path, tmp, options);
743 volfree(options, save_options);
747 parseline( sizeof( type ) - 1, type );
748 parseline( sizeof( creator ) - 1, creator );
749 setextmap( path, type, creator, user);
756 volfree(save_options, NULL);
757 if ( fclose( fp ) != 0 ) {
758 syslog( LOG_ERR, "readvolfile: fclose: %s", strerror(errno) );
764 static void load_volumes(AFPObj *obj)
766 struct passwd *pwent = getpwnam(obj->username);
768 if ( (obj->options.flags & OPTION_USERVOLFIRST) == 0 ) {
769 readvolfile(obj, obj->options.systemvol, 0, pwent);
772 if ((*obj->username == '\0') || (obj->options.flags & OPTION_NOUSERVOL)) {
773 readvolfile(obj, obj->options.defaultvol, NULL, 1, pwent);
776 * Read user's AppleVolumes or .AppleVolumes file
777 * If neither are readable, read the default volumes file. if
778 * that doesn't work, create a user share.
780 if ( readvolfile(obj, pwent->pw_dir, "AppleVolumes", 1, pwent) < 0 &&
781 readvolfile(obj, pwent->pw_dir, ".AppleVolumes", 1, pwent) < 0 &&
782 readvolfile(obj, pwent->pw_dir, "applevolumes", 1, pwent) < 0 &&
783 readvolfile(obj, pwent->pw_dir, ".applevolumes", 1, pwent) < 0 &&
784 obj->options.defaultvol != NULL ) {
785 if (readvolfile(obj, obj->options.defaultvol, NULL, 1, pwent) < 0)
786 creatvol(pwent->pw_dir, NULL, NULL);
789 if ( obj->options.flags & OPTION_USERVOLFIRST ) {
790 readvolfile(obj, obj->options.systemvol, NULL, 0, pwent );
794 static int getvolspace( vol, bfree, btotal, xbfree, xbtotal, bsize )
796 u_int32_t *bfree, *btotal, *bsize;
797 VolSpace *xbfree, *xbtotal;
801 #ifndef NO_QUOTA_SUPPORT
802 VolSpace qfree, qtotal;
803 #endif /* ! NO_QUOTA_SUPPORT */
805 spaceflag = AFPVOL_GVSMASK & vol->v_flags;
806 /* report up to 2GB if afp version is < 2.2 (4GB if not) */
807 maxsize = (vol->v_flags & AFPVOL_A2VOL) ? 0x01fffe00 :
808 (((afp_version < 22) || (vol->v_flags & AFPVOL_LIMITSIZE))
809 ? 0x7fffffffL : 0xffffffffL);
812 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_AFSGVS ) {
813 if ( afs_getvolspace( vol, xbfree, xbtotal, bsize ) == AFP_OK ) {
814 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_AFSGVS;
815 goto getvolspace_done;
820 if (( rc = ustatfs_getvolspace( vol, xbfree, xbtotal,
821 bsize)) != AFP_OK ) {
825 #define min(a,b) ((a)<(b)?(a):(b))
826 #ifndef NO_QUOTA_SUPPORT
827 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_UQUOTA ) {
828 if ( uquota_getvolspace( vol, &qfree, &qtotal, *bsize ) == AFP_OK ) {
829 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_UQUOTA;
830 *xbfree = min(*xbfree, qfree);
831 *xbtotal = min( *xbtotal, qtotal);
832 goto getvolspace_done;
835 #endif /* ! NO_QUOTA_SUPPORT */
836 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_USTATFS;
839 *bfree = min( *xbfree, maxsize);
840 *btotal = min( *xbtotal, maxsize);
844 static int getvolparams( bitmap, vol, st, buf, buflen )
852 int bit = 0, isad = 1;
855 u_int32_t bfree, btotal, bsize;
856 VolSpace xbfree, xbtotal; /* extended bytes */
857 char *data, *nameoff = NULL;
860 /* courtesy of jallison@whistle.com:
861 * For MacOS8.x support we need to create the
862 * .Parent file here if it doesn't exist. */
864 memset(&ad, 0, sizeof(ad));
865 if ( ad_open( vol->v_path, vol_noadouble(vol) |
866 ADFLAGS_HF|ADFLAGS_DIR, O_RDWR | O_CREAT,
870 } else if (ad_getoflags( &ad, ADFLAGS_HF ) & O_CREAT) {
871 slash = strrchr( vol->v_path, '/' );
877 ad_setentrylen( &ad, ADEID_NAME, strlen( slash ));
878 memcpy(ad_entry( &ad, ADEID_NAME ), slash,
879 ad_getentrylen( &ad, ADEID_NAME ));
880 ad_flush(&ad, ADFLAGS_HF);
883 if (( bitmap & ( (1<<VOLPBIT_BFREE)|(1<<VOLPBIT_BTOTAL) |
884 (1<<VOLPBIT_XBFREE)|(1<<VOLPBIT_XBTOTAL) |
885 (1<<VOLPBIT_BSIZE)) ) != 0 ) {
886 if ( getvolspace( vol, &bfree, &btotal, &xbfree, &xbtotal,
889 ad_close( &ad, ADFLAGS_HF );
891 return( AFPERR_PARAM );
896 while ( bitmap != 0 ) {
897 while (( bitmap & 1 ) == 0 ) {
905 ashort = VOLPBIT_ATTR_FILEID;
909 /* check for read-only.
910 * NOTE: we don't actually set the read-only flag unless
911 * it's passed in that way as it's possible to mount
912 * a read-write filesystem under a read-only one. */
913 if ((vol->v_flags & AFPVOL_RO) ||
914 ((utime(vol->v_path, NULL) < 0) && (errno == EROFS)))
915 ashort |= VOLPBIT_ATTR_RO;
916 ashort = htons(ashort);
917 memcpy(data, &ashort, sizeof( ashort ));
918 data += sizeof( ashort );
922 ashort = htons( AFPVOLSIG_DEFAULT );
923 memcpy(data, &ashort, sizeof( ashort ));
924 data += sizeof( ashort );
928 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
929 aint = AD_DATE_FROM_UNIX(st->st_mtime);
930 memcpy(data, &aint, sizeof( aint ));
931 data += sizeof( aint );
935 if ( st->st_mtime > vol->v_time ) {
936 vol->v_time = st->st_mtime;
937 aint = AD_DATE_FROM_UNIX(st->st_mtime);
939 aint = AD_DATE_FROM_UNIX(vol->v_time);
941 memcpy(data, &aint, sizeof( aint ));
942 data += sizeof( aint );
946 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
947 aint = AD_DATE_START;
948 memcpy(data, &aint, sizeof( aint ));
949 data += sizeof( aint );
953 memcpy(data, &vol->v_vid, sizeof( vol->v_vid ));
954 data += sizeof( vol->v_vid );
958 bfree = htonl( bfree );
959 memcpy(data, &bfree, sizeof( bfree ));
960 data += sizeof( bfree );
963 case VOLPBIT_BTOTAL :
964 btotal = htonl( btotal );
965 memcpy(data, &btotal, sizeof( btotal ));
966 data += sizeof( btotal );
969 #ifndef NO_LARGE_VOL_SUPPORT
970 case VOLPBIT_XBFREE :
971 xbfree = hton64( xbfree );
972 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
973 bcopy(&xbfree, data, sizeof(xbfree));
974 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
975 memcpy(data, &xbfree, sizeof( xbfree ));
976 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
977 data += sizeof( xbfree );
980 case VOLPBIT_XBTOTAL :
981 xbtotal = hton64( xbtotal );
982 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
983 bcopy(&xbtotal, data, sizeof(xbtotal));
984 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
985 memcpy(data, &xbtotal, sizeof( xbtotal ));
986 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
987 data += sizeof( xbfree );
989 #endif /* ! NO_LARGE_VOL_SUPPORT */
993 data += sizeof( u_int16_t );
996 case VOLPBIT_BSIZE: /* block size */
997 bsize = htonl(bsize);
998 memcpy(data, &bsize, sizeof(bsize));
999 data += sizeof(bsize);
1004 ad_close( &ad, ADFLAGS_HF );
1006 return( AFPERR_BITMAP );
1012 ashort = htons( data - buf );
1013 memcpy(nameoff, &ashort, sizeof( ashort ));
1014 aint = strlen( vol->v_name );
1016 memcpy(data, vol->v_name, aint );
1020 ad_close( &ad, ADFLAGS_HF );
1022 *buflen = data - buf;
1028 int afp_getsrvrparms(obj, ibuf, ibuflen, rbuf, rbuflen )
1031 int ibuflen, *rbuflen;
1044 for ( vcnt = 0, volume = volumes; volume; volume = volume->v_next ) {
1045 if ( stat( volume->v_path, &st ) < 0 ) {
1046 syslog( LOG_INFO, "afp_getsrvrparms: stat %s: %s",
1047 volume->v_path, strerror(errno) );
1048 continue; /* can't access directory */
1050 if (!S_ISDIR(st.st_mode)) {
1051 continue; /* not a dir */
1054 /* set password bit if there's a volume password */
1055 *data = (volume->v_password) ? AFPSRVR_PASSWD : 0;
1057 /* Apple 2 clients running ProDOS-8 expect one volume to have
1058 bit 0 of this byte set. They will not recognize anything
1059 on the server unless this is the case. I have not
1060 completely worked this out, but it's related to booting
1061 from the server. Support for that function is a ways
1062 off.. <shirsch@ibm.net> */
1063 *data++ |= (volume->v_flags & AFPVOL_A2VOL) ? AFPSRVR_CONFIGINFO : 0;
1064 len = strlen( volume->v_name );
1066 memcpy(data, volume->v_name, len );
1071 *rbuflen = data - rbuf;
1073 if ( gettimeofday( &tv, 0 ) < 0 ) {
1074 syslog( LOG_ERR, "afp_getsrvrparms: gettimeofday: %s", strerror(errno) );
1076 return AFPERR_PARAM;
1078 tv.tv_sec = AD_DATE_FROM_UNIX(tv.tv_sec);
1079 memcpy(data, &tv.tv_sec, sizeof( u_int32_t));
1080 data += sizeof( u_int32_t);
1085 int afp_openvol(obj, ibuf, ibuflen, rbuf, rbuflen )
1088 int ibuflen, *rbuflen;
1094 #endif /* CNID_DB */
1097 int len, ret, buflen;
1101 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1102 bitmap = ntohs( bitmap );
1103 ibuf += sizeof( bitmap );
1104 if (( bitmap & (1<<VOLPBIT_VID)) == 0 ) {
1105 ret = AFPERR_BITMAP;
1109 len = (unsigned char)*ibuf++;
1110 volname = obj->oldtmp;
1111 memcpy(volname, ibuf, len );
1112 *(volname + len) = '\0';
1114 if ((len + 1) & 1) /* pad to an even boundary */
1120 for ( volume = volumes; volume; volume = volume->v_next ) {
1121 if ( strcasecmp( volname, volume->v_name ) == 0 ) {
1126 if ( volume == NULL ) {
1131 /* check for a volume password */
1132 if (volume->v_password &&
1133 strncmp(ibuf, volume->v_password, VOLPASSLEN)) {
1134 ret = AFPERR_ACCESS;
1138 if (( volume->v_flags & AFPVOL_OPEN ) == 0 ) {
1139 if ((dir = dirnew(strlen(volume->v_name) + 1)) == NULL) {
1140 syslog( LOG_ERR, "afp_openvol: malloc: %s", strerror(errno) );
1144 dir->d_did = DIRDID_ROOT;
1145 dir->d_color = DIRTREE_COLOR_BLACK; /* root node is black */
1146 strcpy( dir->d_name, volume->v_name );
1147 volume->v_dir = volume->v_root = dir;
1148 volume->v_flags |= AFPVOL_OPEN;
1151 if ( stat( volume->v_path, &st ) < 0 ) {
1156 buflen = *rbuflen - sizeof( bitmap );
1157 if (( ret = getvolparams( bitmap, volume, &st,
1158 rbuf + sizeof(bitmap), &buflen )) != AFP_OK ) {
1161 *rbuflen = buflen + sizeof( bitmap );
1162 bitmap = htons( bitmap );
1163 memcpy(rbuf, &bitmap, sizeof( bitmap ));
1165 curdir = volume->v_dir;
1166 if ( chdir( volume->v_path ) < 0 ) {
1172 if (volume->v_dbpath)
1173 volume->v_db = cnid_open (volume->v_dbpath);
1174 if (volume->v_db == 0)
1175 volume->v_db = cnid_open (volume->v_path);
1176 #endif /* CNID_DB */
1180 * If you mount a volume twice, the second time the trash appears on
1181 * the desk-top. That's because the Mac remembers the DID for the
1182 * trash (even for volumes in different zones, on different servers).
1183 * Just so this works better, we prime the DID cache with the trash,
1184 * fixing the trash at DID 3.
1187 cname( volume, volume->v_dir, &p );
1188 #endif /* CNID_DB */
1197 int afp_closevol(obj, ibuf, ibuflen, rbuf, rbuflen )
1200 int ibuflen, *rbuflen;
1202 struct vol *vol, *ovol;
1207 memcpy(&vid, ibuf, sizeof( vid ));
1208 if (( vol = getvolbyvid( vid )) == NULL ) {
1209 return( AFPERR_PARAM );
1212 vol->v_flags &= ~AFPVOL_OPEN;
1213 for ( ovol = volumes; ovol; ovol = ovol->v_next ) {
1214 if ( ovol->v_flags & AFPVOL_OPEN ) {
1218 if ( ovol != NULL ) {
1219 curdir = ovol->v_dir;
1220 if ( chdir( ovol->v_path ) < 0 ) {
1221 return( AFPERR_PARAM );
1225 dirfree( vol->v_root );
1228 cnid_close(vol->v_db);
1230 #endif /* CNID_DB */
1234 struct vol *getvolbyvid(const u_int16_t vid )
1238 for ( vol = volumes; vol; vol = vol->v_next ) {
1239 if ( vid == vol->v_vid ) {
1243 if ( vol == NULL || ( vol->v_flags & AFPVOL_OPEN ) == 0 ) {
1250 struct extmap *getextmap(const char *path)
1255 if (( p = strrchr( path, '.' )) == NULL ) {
1256 return( defextmap );
1259 for ( em = extmap; em; em = em->em_next ) {
1260 if ( strdiacasecmp( em->em_ext, p ) == 0 ) {
1265 return( defextmap );
1271 void setvoltime(obj, vol )
1277 /* just looking at vol->v_time is broken seriously since updates
1278 * from other users afpd processes never are seen.
1279 * This is not the most elegant solution (a shared memory between
1280 * the afpd processes would come closer)
1283 if ( gettimeofday( &tv, 0 ) < 0 ) {
1284 syslog( LOG_ERR, "setvoltime: gettimeofday: %s", strerror(errno) );
1287 if( utime( vol->v_path, NULL ) < 0 ) {
1288 /* write of time failed ... probably a read only filesys,
1289 * where no other users can interfere, so there's no issue here
1293 /* a little granularity */
1294 if (vol->v_time < tv.tv_sec) {
1295 vol->v_time = tv.tv_sec;
1296 obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED);
1300 int afp_getvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1303 int ibuflen, *rbuflen;
1308 u_int16_t vid, bitmap;
1311 memcpy(&vid, ibuf, sizeof( vid ));
1312 ibuf += sizeof( vid );
1313 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1314 bitmap = ntohs( bitmap );
1316 if (( vol = getvolbyvid( vid )) == NULL ) {
1318 return( AFPERR_PARAM );
1321 if ( stat( vol->v_path, &st ) < 0 ) {
1323 return( AFPERR_PARAM );
1326 buflen = *rbuflen - sizeof( bitmap );
1327 if (( ret = getvolparams( bitmap, vol, &st,
1328 rbuf + sizeof( bitmap ), &buflen )) != AFP_OK ) {
1332 *rbuflen = buflen + sizeof( bitmap );
1333 bitmap = htons( bitmap );
1334 memcpy(rbuf, &bitmap, sizeof( bitmap ));
1338 int afp_setvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1341 int ibuflen, *rbuflen;
1345 u_int16_t vid, bitmap;
1351 memcpy(&vid, ibuf, sizeof( vid ));
1352 ibuf += sizeof( vid );
1353 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1354 bitmap = ntohs( bitmap );
1355 ibuf += sizeof(bitmap);
1357 if (( vol = getvolbyvid( vid )) == NULL ) {
1358 return( AFPERR_PARAM );
1361 if (vol->v_flags & AFPVOL_RO)
1362 return AFPERR_VLOCK;
1364 /* we can only set the backup date. */
1365 if (bitmap != VOLPBIT_BDATE)
1366 return AFPERR_BITMAP;
1368 memset(&ad, 0, sizeof(ad));
1369 if ( ad_open( vol->v_path, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR,
1372 return AFPERR_VLOCK;
1374 return AFPERR_ACCESS;
1377 memcpy(&aint, ibuf, sizeof(aint));
1378 ad_setdate(&ad, AD_DATE_BACKUP, aint);
1379 ad_flush(&ad, ADFLAGS_HF);
1380 ad_close(&ad, ADFLAGS_HF);