2 * $Id: volume.c,v 1.8 2001-06-20 18:33:04 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>
26 #include <atalk/cnid.h>
30 #endif /* HAVE_FCNTL_H */
40 #include "directory.h"
47 #define MIN(a, b) ((a) < (b) ? (a) : (b))
50 #ifndef NO_LARGE_VOL_SUPPORT
51 #if BYTE_ORDER == BIG_ENDIAN
54 #else /* BYTE_ORDER == BIG_ENDIAN */
55 #define hton64(x) ((u_int64_t) (htonl(((x) >> 32) & 0xffffffffLL)) | \
56 (u_int64_t) ((htonl(x) & 0xffffffffLL) << 32))
57 #define ntoh64(x) (hton64(x))
58 #endif /* BYTE_ORDER == BIG_ENDIAN */
59 #endif /* ! NO_LARGE_VOL_SUPPORT */
61 static struct vol *volumes = NULL;
62 static int lastvid = 0;
63 #if AD_VERSION == AD_VERSION1
64 static char *Trash = "\02\024Network Trash Folder";
65 #endif /* AD_VERSION == AD_VERSION1 */
66 static struct extmap *extmap = NULL, *defextmap = NULL;
68 #define VOLOPT_ALLOW 0 /* user allow list */
69 #define VOLOPT_DENY 1 /* user deny list */
70 #define VOLOPT_RWLIST 2 /* user rw list */
71 #define VOLOPT_ROLIST 3 /* user ro list */
72 #define VOLOPT_CODEPAGE 4 /* codepage */
73 #define VOLOPT_PASSWORD 5 /* volume password */
74 #define VOLOPT_CASEFOLD 6 /* character case mangling */
75 #define VOLOPT_FLAGS 7 /* various flags */
76 #define VOLOPT_DBPATH 8 /* path to database */
77 #define VOLOPT_MAPCHARS 9 /* does mtou and utom mappings. syntax:
78 m and u can be double-byte hex
83 !u -> make u illegal always
84 ~u -> make u illegal only as the first
85 part of a double-byte character.
89 #define VOLOPT_FORCEUID 10 /* force uid for username x */
90 #define VOLOPT_FORCEGID 11 /* force gid for group x */
92 #else /* normally, there are only 9 possible options */
94 #endif /* FORCE_UIDGID */
96 #define VOLOPT_NUM (VOLOPT_MAX + 1)
99 #define VOLOPT_DEFAULT ":DEFAULT:"
100 #define VOLOPT_DEFAULT_LEN 9
106 static __inline__ void volfree(struct vol_option *options,
107 const struct vol_option *save)
112 for (i = 0; i < VOLOPT_MAX; i++) {
113 if (options[i].c_value && (options[i].c_value != save[i].c_value))
114 free(options[i].c_value);
117 for (i = 0; i < VOLOPT_MAX; i++) {
118 if (options[i].c_value)
119 free(options[i].c_value);
125 /* handle variable substitutions. here's what we understand:
126 * $c -> client ip/appletalk address
127 * $f -> full name (whatever's in the gecos field)
130 * $s -> server name (hostname if it doesn't exist)
131 * $u -> username (guest is usually nobody)
132 * $v -> volume name (ADEID_NAME or basename)
133 * $z -> zone (may not exist)
136 #define is_var(a, b) (strncmp((a), (b), 2) == 0)
137 static void volxlate(AFPObj *obj, char *dest, int destlen,
138 char *src, struct passwd *pwd, char *path)
143 strncpy(dest, src, destlen);
144 if ((p = strchr(src, '$')) == NULL) /* nothing to do */
147 /* first part of the path. just forward to the next variable. */
148 len = MIN(p - src, destlen);
154 while (p && destlen > 0) {
155 /* now figure out what the variable is */
157 if (is_var(p, "$c")) {
158 if (obj->proto == AFPPROTO_ASP) {
159 ASP asp = obj->handle;
161 len = sprintf(dest, "%u.%u", ntohs(asp->asp_sat.sat_addr.s_net),
162 asp->asp_sat.sat_addr.s_node);
166 } else if (obj->proto == AFPPROTO_DSI) {
167 DSI *dsi = obj->handle;
169 len = sprintf(dest, "%s:%u", inet_ntoa(dsi->client.sin_addr),
170 ntohs(dsi->client.sin_port));
174 } else if (is_var(p, "$f")) {
175 if ((q = strchr(pwd->pw_gecos, ',')))
178 } else if (is_var(p, "$g")) {
179 struct group *grp = getgrgid(pwd->pw_gid);
182 } else if (is_var(p, "$h")) {
183 q = obj->options.hostname;
184 } else if (is_var(p, "$s")) {
187 else if (obj->options.server) {
188 q = obj->options.server;
190 q = obj->options.hostname;
191 } else if (is_var(p, "$u")) {
193 } else if (is_var(p, "$v")) {
197 memset(&ad, 0, sizeof(ad));
198 if (ad_open(path, ADFLAGS_HF, O_RDONLY, 0, &ad) < 0)
201 if ((len = MIN(ad_getentrylen(&ad, ADEID_NAME), destlen)) > 0) {
202 memcpy(dest, ad_entry(&ad, ADEID_NAME), len);
203 ad_close(&ad, ADFLAGS_HF);
207 ad_close(&ad, ADFLAGS_HF);
208 no_volname: /* simple basename */
209 if ((q = strrchr(path, '/')) == NULL)
211 else if (*(q + 1) != '\0')
215 } else if (is_var(p, "$z")) {
217 } else if (is_var(p, "$$")) {
222 /* copy the stuff over. if we don't understand something that we
223 * should, just skip it over. */
225 len = MIN(p == q ? 2 : strlen(q), destlen);
226 strncpy(dest, q, len);
231 /* stuff up to next $ */
233 p = strchr(src, '$');
234 len = p ? MIN(p - src, destlen) : destlen;
236 strncpy(dest, src, len);
243 /* to make sure that val is valid, make sure to select an opt that
245 #define optionok(buf,opt,val) (strstr((buf),(opt)) && ((val)[1] != '\0'))
247 static __inline__ char *get_codepage_path(const char *path, const char *name)
253 page = (char *) malloc((len = strlen(path)) + strlen(name) + 2);
256 if (path[len - 1] != '/') /* add a / */
264 /* debug: show which codepage directory we are using */
265 syslog(LOG_DEBUG, "using codepage directory: %s", page);
270 /* handle all the options. tmp can't be NULL. */
271 static void volset(struct vol_option *options, char *volname, int vlen,
272 const char *nlspath, const char *tmp)
276 val = strchr(tmp, ':');
277 if (optionok(tmp, "allow:", val)) {
278 if (options[VOLOPT_ALLOW].c_value)
279 free(options[VOLOPT_ALLOW].c_value);
280 options[VOLOPT_ALLOW].c_value = strdup(val + 1);
282 } else if (optionok(tmp, "deny:", val)) {
283 if (options[VOLOPT_DENY].c_value)
284 free(options[VOLOPT_DENY].c_value);
285 options[VOLOPT_DENY].c_value = strdup(val + 1);
287 } else if (optionok(tmp, "rwlist:", val)) {
288 if (options[VOLOPT_RWLIST].c_value)
289 free(options[VOLOPT_RWLIST].c_value);
290 options[VOLOPT_RWLIST].c_value = strdup(val + 1);
292 } else if (optionok(tmp, "rolist:", val)) {
293 if (options[VOLOPT_ROLIST].c_value)
294 free(options[VOLOPT_ROLIST].c_value);
295 options[VOLOPT_ROLIST].c_value = strdup(val + 1);
297 } else if (optionok(tmp, "codepage:", val)) {
298 if (options[VOLOPT_CODEPAGE].c_value)
299 free(options[VOLOPT_CODEPAGE].c_value);
300 options[VOLOPT_CODEPAGE].c_value = get_codepage_path(nlspath, val + 1);
302 } else if (optionok(tmp, "casefold:", val)) {
303 if (strcasecmp(val + 1, "tolower") == 0)
304 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMLOWER;
305 else if (strcasecmp(val + 1, "toupper") == 0)
306 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMUPPER;
307 else if (strcasecmp(val + 1, "xlatelower") == 0)
308 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UUPPERMLOWER;
309 else if (strcasecmp(val + 1, "xlateupper") == 0)
310 options[VOLOPT_CASEFOLD].i_value = AFPVOL_ULOWERMUPPER;
312 } else if (optionok(tmp, "options:", val)) {
315 if ((p = strtok(val + 1, ",")) == NULL) /* nothing */
319 if (strcasecmp(p, "prodos") == 0)
320 options[VOLOPT_FLAGS].i_value |= AFPVOL_A2VOL;
321 else if (strcasecmp(p, "mswindows") == 0) {
322 options[VOLOPT_FLAGS].i_value |= AFPVOL_MSWINDOWS;
323 if (!options[VOLOPT_CODEPAGE].c_value)
324 options[VOLOPT_CODEPAGE].c_value =
325 get_codepage_path(nlspath, MSWINDOWS_CODEPAGE);
327 } else if (strcasecmp(p, "crlf") == 0)
328 options[VOLOPT_FLAGS].i_value |= AFPVOL_CRLF;
329 else if (strcasecmp(p, "noadouble") == 0)
330 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOADOUBLE;
331 else if (strcasecmp(p, "ro") == 0)
332 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
333 else if (strcasecmp(p, "nohex") == 0)
334 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOHEX;
335 else if (strcasecmp(p, "usedots") == 0)
336 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS;
337 else if (strcasecmp(p, "limitsize") == 0)
338 options[VOLOPT_FLAGS].i_value |= AFPVOL_LIMITSIZE;
339 /* support for either "dropbox" or "dropkludge" */
340 else if (strcasecmp(p, "dropbox") == 0)
341 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
342 else if (strcasecmp(p, "dropkludge") == 0)
343 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
345 p = strtok(NULL, ",");
348 #if AD_VERSION > AD_VERSION1
349 } else if (optionok(tmp, "dbpath:", val)) {
350 if (options[VOLOPT_DBPATH].c_value)
351 free(options[VOLOPT_DBPATH].c_value);
353 options[VOLOPT_DBPATH].c_value = strdup(val + 1);
354 #endif /* AD_VERSION > AD_VERSION1 */
355 } else if (optionok(tmp, "mapchars:",val)) {
356 if (options[VOLOPT_MAPCHARS].c_value)
357 free(options[VOLOPT_MAPCHARS].c_value);
358 options[VOLOPT_MAPCHARS].c_value = strdup(val + 1);
360 } else if (optionok(tmp, "password:", val)) {
361 if (options[VOLOPT_PASSWORD].c_value)
362 free(options[VOLOPT_PASSWORD].c_value);
363 options[VOLOPT_PASSWORD].c_value = strdup(val + 1);
367 /* this code allows forced uid/gid per volume settings */
368 } else if (optionok(tmp, "forceuid:", val)) {
369 if (options[VOLOPT_FORCEUID].c_value)
370 free(options[VOLOPT_FORCEUID].c_value);
371 options[VOLOPT_FORCEUID].c_value = strdup(val + 1);
372 } else if (optionok(tmp, "forcegid:", val)) {
373 if (options[VOLOPT_FORCEGID].c_value)
374 free(options[VOLOPT_FORCEGID].c_value);
375 options[VOLOPT_FORCEGID].c_value = strdup(val + 1);
377 #endif /* FORCE_UIDGID */
380 /* ignore unknown options */
381 syslog(LOG_DEBUG, "ignoring unknown volume option: %s", tmp);
384 /* we'll assume it's a volume name. */
385 strncpy(volname, tmp, vlen);
389 static int creatvol(const char *path, char *name, struct vol_option *options)
394 if ( name == NULL || *name == '\0' ) {
395 if ((name = strrchr( path, '/' )) == NULL) {
396 return -1; /* Obviously not a fully qualified path */
399 /* if you wish to share /, you need to specify a name. */
404 for ( volume = volumes; volume; volume = volume->v_next ) {
405 if ( strcasecmp( volume->v_name, name ) == 0 ) {
406 return -1; /* Won't be able to access it, anyway... */
410 vlen = strlen( name );
411 if ( vlen > AFPVOL_NAMELEN ) {
412 vlen = AFPVOL_NAMELEN;
413 name[AFPVOL_NAMELEN] = '\0';
417 (struct vol *)calloc(1, sizeof( struct vol ))) == NULL ) {
418 syslog( LOG_ERR, "creatvol: malloc: %m" );
421 if (( volume->v_name =
422 (char *)malloc( vlen + 1 )) == NULL ) {
423 syslog( LOG_ERR, "creatvol: malloc: %m" );
427 if (( volume->v_path =
428 (char *)malloc( strlen( path ) + 1 )) == NULL ) {
429 syslog( LOG_ERR, "creatvol: malloc: %m" );
430 free(volume->v_name);
435 strcpy( volume->v_name, name);
436 strcpy( volume->v_path, path );
440 #endif /* __svr4__ */
441 volume->v_vid = lastvid++;
442 volume->v_lastdid = 3;
446 /* should we casefold? */
447 volume->v_casefold = options[VOLOPT_CASEFOLD].i_value;
449 /* shift in some flags */
450 volume->v_flags = options[VOLOPT_FLAGS].i_value;
452 /* read in the code pages */
453 if (options[VOLOPT_CODEPAGE].c_value)
454 codepage_read(volume, options[VOLOPT_CODEPAGE].c_value);
456 if (options[VOLOPT_PASSWORD].c_value)
457 volume->v_password = strdup(options[VOLOPT_PASSWORD].c_value);
459 #if AD_VERSION > AD_VERSION1
460 if (options[VOLOPT_DBPATH].c_value)
461 volume->v_dbpath = strdup(options[VOLOPT_DBPATH].c_value);
462 #endif /* AD_VERSION > AD_VERSION1 */
466 if (options[VOLOPT_FORCEUID].c_value) {
467 volume->v_forceuid = strdup(options[VOLOPT_FORCEUID].c_value);
469 volume->v_forceuid = NULL; /* set as null so as to return 0 later on */
472 if (options[VOLOPT_FORCEGID].c_value) {
473 volume->v_forcegid = strdup(options[VOLOPT_FORCEGID].c_value);
475 volume->v_forcegid = NULL; /* set as null so as to return 0 later on */
478 #endif /* FORCE_UIDGID */
482 volume->v_next = volumes;
487 static char *myfgets( buf, size, fp )
496 while ((( c = getc( fp )) != EOF ) && ( size > 0 )) {
497 if ( c == '\n' || c == '\r' ) {
515 /* check access list. this function wants something of the following
517 * @group,name,name2,@group2,name3
519 * a NULL argument allows everybody to have access.
520 * we return three things:
522 * 0: list exists, but name isn't in it
525 static int accessvol(args, name)
529 char buf[MAXPATHLEN + 1], *p;
535 strncpy(buf, args, sizeof(buf));
536 if ((p = strtok(buf, ",")) == NULL) /* nothing, return okay */
540 if (*p == '@') { /* it's a group */
541 if ((gr = getgrnam(p + 1)) && gmem(gr->gr_gid))
543 } else if (strcmp(p, name) == 0) /* it's a user name */
545 p = strtok(NULL, ",");
551 static void setextmap( ext, type, creator, user)
552 char *ext, *type, *creator;
557 for ( em = extmap; em; em = em->em_next ) {
558 if ( strdiacasecmp( em->em_ext, ext ) == 0 ) {
565 (struct extmap *)malloc( sizeof( struct extmap ))) == NULL ) {
566 syslog( LOG_ERR, "setextmap: malloc: %m" );
569 em->em_next = extmap;
571 } else if ( !user ) {
575 strcpy( em->em_ext, ext );
577 if ( *type == '\0' ) {
578 memcpy(em->em_type, "????", sizeof( em->em_type ));
580 memcpy(em->em_type, type, sizeof( em->em_type ));
582 if ( *creator == '\0' ) {
583 memcpy(em->em_creator, "UNIX", sizeof( em->em_creator ));
585 memcpy(em->em_creator, creator, sizeof( em->em_creator ));
588 if ( strcmp( ext, "." ) == 0 ) {
594 * Read a volume configuration file and add the volumes contained within to
595 * the global volume list. If p2 is non-NULL, the file that is opened is
598 * Lines that begin with # and blank lines are ignored.
599 * Volume lines are of the form:
600 * <unix path> [<volume name>] [allow:<user>,<@group>,...] \
601 * [codepage:<file>] [casefold:<num>]
602 * <extension> TYPE [CREATOR]
604 static int readvolfile(obj, p1, p2, user, pwent)
608 struct passwd *pwent;
611 char path[ MAXPATHLEN + 1], tmp[ MAXPATHLEN + 1],
612 volname[ AFPVOL_NAMELEN + 1 ], buf[ BUFSIZ ],
613 type[ 5 ], creator[ 5 ];
616 struct vol_option options[VOLOPT_NUM], save_options[VOLOPT_NUM];
628 if (( fp = fopen( path, "r" )) == NULL ) {
632 memset(save_options, 0, sizeof(save_options));
633 while ( myfgets( buf, sizeof( buf ), fp ) != NULL ) {
634 initline( strlen( buf ), buf );
635 parseline( sizeof( path ) - 1, path );
642 /* change the default options for this file */
643 if (strncmp(path, VOLOPT_DEFAULT, VOLOPT_DEFAULT_LEN) == 0) {
645 for (i = 0; i < VOLOPT_NUM; i++) {
646 if (parseline( sizeof( path ) - VOLOPT_DEFAULT_LEN - 1,
647 path + VOLOPT_DEFAULT_LEN) < 0)
649 volset(save_options, tmp, sizeof(tmp) - 1,
650 obj->options.nlspath, path + VOLOPT_DEFAULT_LEN);
656 if (( p = strchr( path, '/' )) != NULL ) {
664 if ( u == NULL || *u == '\0' || ( pw = getpwnam( u )) == NULL ) {
667 strcpy( tmp, pw->pw_dir );
668 if ( p != NULL && *p != '\0' ) {
675 /* send path through variable substitution */
676 if (*path != '~') /* need to copy path to tmp */
679 pwent = getpwnam(obj->username);
680 volxlate(obj, path, sizeof(path) - 1, tmp, pwent, NULL);
682 /* this is sort of braindead. basically, i want to be
683 * able to specify things in any order, but i don't want to
684 * re-write everything.
686 * currently we have 11 options:
694 * options:prodos,crlf,noadouble,ro
697 * namemask:x,y,!z (not implemented yet)
699 memcpy(options, save_options, sizeof(options));
702 /* read in up to 11 possible options */
703 for (i = 0; i < VOLOPT_NUM; i++) {
704 if (parseline( sizeof( tmp ) - 1, tmp ) < 0)
707 volset(options, volname, sizeof(volname) - 1,
708 obj->options.nlspath, tmp);
711 /* check allow/deny lists:
712 allow -> either no list (-1), or in list (1)
713 deny -> either no list (-1), or not in list (0) */
714 if (accessvol(options[VOLOPT_ALLOW].c_value, obj->username) &&
715 (accessvol(options[VOLOPT_DENY].c_value, obj->username) < 1)) {
717 /* handle read-only behaviour. semantics:
718 * 1) neither the rolist nor the rwlist exist -> rw
719 * 2) rolist exists -> ro if user is in it.
720 * 3) rwlist exists -> ro unless user is in it. */
721 if (((options[VOLOPT_FLAGS].i_value & AFPVOL_RO) == 0) &&
722 ((accessvol(options[VOLOPT_ROLIST].c_value,
723 obj->username) == 1) ||
724 !accessvol(options[VOLOPT_RWLIST].c_value,
726 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
728 /* do variable substitution */
729 volxlate(obj, tmp, sizeof(tmp) - 1, volname, pwent, path);
730 creatvol(path, tmp, options);
732 volfree(options, save_options);
736 parseline( sizeof( type ) - 1, type );
737 parseline( sizeof( creator ) - 1, creator );
738 setextmap( path, type, creator, user);
745 volfree(save_options, NULL);
746 if ( fclose( fp ) != 0 ) {
747 syslog( LOG_ERR, "readvolfile: fclose: %m" );
753 static void load_volumes(AFPObj *obj)
755 struct passwd *pwent = getpwnam(obj->username);
757 if ( (obj->options.flags & OPTION_USERVOLFIRST) == 0 ) {
758 readvolfile(obj, obj->options.systemvol, 0, pwent);
761 if ((*obj->username == '\0') || (obj->options.flags & OPTION_NOUSERVOL)) {
762 readvolfile(obj, obj->options.defaultvol, NULL, 1, pwent);
765 * Read user's AppleVolumes or .AppleVolumes file
766 * If neither are readable, read the default volumes file. if
767 * that doesn't work, create a user share.
769 if ( readvolfile(obj, pwent->pw_dir, "AppleVolumes", 1, pwent) < 0 &&
770 readvolfile(obj, pwent->pw_dir, ".AppleVolumes", 1, pwent) < 0 &&
771 readvolfile(obj, pwent->pw_dir, "applevolumes", 1, pwent) < 0 &&
772 readvolfile(obj, pwent->pw_dir, ".applevolumes", 1, pwent) < 0 &&
773 obj->options.defaultvol != NULL ) {
774 if (readvolfile(obj, obj->options.defaultvol, NULL, 1, pwent) < 0)
775 creatvol(pwent->pw_dir, NULL, NULL);
778 if ( obj->options.flags & OPTION_USERVOLFIRST ) {
779 readvolfile(obj, obj->options.systemvol, NULL, 0, pwent );
783 static int getvolspace( vol, bfree, btotal, xbfree, xbtotal, bsize )
785 u_int32_t *bfree, *btotal, *bsize;
786 VolSpace *xbfree, *xbtotal;
790 #ifndef NO_QUOTA_SUPPORT
791 VolSpace qfree, qtotal;
792 #endif /* ! NO_QUOTA_SUPPORT */
794 spaceflag = AFPVOL_GVSMASK & vol->v_flags;
795 /* report up to 2GB if afp version is < 2.2 (4GB if not) */
796 maxsize = (vol->v_flags & AFPVOL_A2VOL) ? 0x01fffe00 :
797 (((afp_version < 22) || (vol->v_flags & AFPVOL_LIMITSIZE))
798 ? 0x7fffffffL : 0xffffffffL);
801 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_AFSGVS ) {
802 if ( afs_getvolspace( vol, xbfree, xbtotal, bsize ) == AFP_OK ) {
803 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_AFSGVS;
804 goto getvolspace_done;
809 if (( rc = ustatfs_getvolspace( vol, xbfree, xbtotal,
810 bsize)) != AFP_OK ) {
814 #define min(a,b) ((a)<(b)?(a):(b))
815 #ifndef NO_QUOTA_SUPPORT
816 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_UQUOTA ) {
817 if ( uquota_getvolspace( vol, &qfree, &qtotal, *bsize ) == AFP_OK ) {
818 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_UQUOTA;
819 *xbfree = min(*xbfree, qfree);
820 *xbtotal = min( *xbtotal, qtotal);
821 goto getvolspace_done;
824 #endif /* ! NO_QUOTA_SUPPORT */
825 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_USTATFS;
828 *bfree = min( *xbfree, maxsize);
829 *btotal = min( *xbtotal, maxsize);
833 static int getvolparams( bitmap, vol, st, buf, buflen )
841 int bit = 0, aint, isad = 1;
843 u_int32_t bfree, btotal, bsize;
844 VolSpace xbfree, xbtotal; /* extended bytes */
845 char *data, *nameoff = NULL;
848 /* courtesy of jallison@whistle.com:
849 * For MacOS8.x support we need to create the
850 * .Parent file here if it doesn't exist. */
852 memset(&ad, 0, sizeof(ad));
853 if ( ad_open( vol->v_path, vol_noadouble(vol) |
854 ADFLAGS_HF|ADFLAGS_DIR, O_RDWR | O_CREAT,
858 } else if (ad_getoflags( &ad, ADFLAGS_HF ) & O_CREAT) {
859 slash = strrchr( vol->v_path, '/' );
865 ad_setentrylen( &ad, ADEID_NAME, strlen( slash ));
866 memcpy(ad_entry( &ad, ADEID_NAME ), slash,
867 ad_getentrylen( &ad, ADEID_NAME ));
868 ad_flush(&ad, ADFLAGS_HF);
871 if (( bitmap & ( (1<<VOLPBIT_BFREE)|(1<<VOLPBIT_BTOTAL) |
872 (1<<VOLPBIT_XBFREE)|(1<<VOLPBIT_XBTOTAL) |
873 (1<<VOLPBIT_BSIZE)) ) != 0 ) {
874 if ( getvolspace( vol, &bfree, &btotal, &xbfree, &xbtotal,
877 ad_close( &ad, ADFLAGS_HF );
879 return( AFPERR_PARAM );
884 while ( bitmap != 0 ) {
885 while (( bitmap & 1 ) == 0 ) {
892 #if AD_VERSION > AD_VERSION1
893 ashort = VOLPBIT_ATTR_FILEID;
894 #else /* AD_VERSION > AD_VERSION1 */
896 #endif /* AD_VERSION > AD_VERSION1 */
897 /* check for read-only.
898 * NOTE: we don't actually set the read-only flag unless
899 * it's passed in that way as it's possible to mount
900 * a read-write filesystem under a read-only one. */
901 if ((vol->v_flags & AFPVOL_RO) ||
902 ((utime(vol->v_path, NULL) < 0) && (errno == EROFS)))
903 ashort |= VOLPBIT_ATTR_RO;
904 ashort = htons(ashort);
905 memcpy(data, &ashort, sizeof( ashort ));
906 data += sizeof( ashort );
910 ashort = htons( AFPVOLSIG_DEFAULT );
911 memcpy(data, &ashort, sizeof( ashort ));
912 data += sizeof( ashort );
916 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
917 aint = AD_DATE_FROM_UNIX(st->st_mtime);
918 memcpy(data, &aint, sizeof( aint ));
919 data += sizeof( aint );
923 if ( st->st_mtime > vol->v_time ) {
924 vol->v_time = st->st_mtime;
925 aint = AD_DATE_FROM_UNIX(st->st_mtime);
927 aint = AD_DATE_FROM_UNIX(vol->v_time);
929 memcpy(data, &aint, sizeof( aint ));
930 data += sizeof( aint );
934 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
935 aint = AD_DATE_START;
936 memcpy(data, &aint, sizeof( aint ));
937 data += sizeof( aint );
941 memcpy(data, &vol->v_vid, sizeof( vol->v_vid ));
942 data += sizeof( vol->v_vid );
946 bfree = htonl( bfree );
947 memcpy(data, &bfree, sizeof( bfree ));
948 data += sizeof( bfree );
951 case VOLPBIT_BTOTAL :
952 btotal = htonl( btotal );
953 memcpy(data, &btotal, sizeof( btotal ));
954 data += sizeof( btotal );
957 #ifndef NO_LARGE_VOL_SUPPORT
958 case VOLPBIT_XBFREE :
959 xbfree = hton64( xbfree );
960 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
961 bcopy(&xbfree, data, sizeof(xbfree));
962 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
963 memcpy(data, &xbfree, sizeof( xbfree ));
964 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
965 data += sizeof( xbfree );
968 case VOLPBIT_XBTOTAL :
969 xbtotal = hton64( xbtotal );
970 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
971 bcopy(&xbtotal, data, sizeof(xbtotal));
972 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
973 memcpy(data, &xbtotal, sizeof( xbtotal ));
974 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
975 data += sizeof( xbfree );
977 #endif /* ! NO_LARGE_VOL_SUPPORT */
981 data += sizeof( u_int16_t );
984 case VOLPBIT_BSIZE: /* block size */
985 bsize = htonl(bsize);
986 memcpy(data, &bsize, sizeof(bsize));
987 data += sizeof(bsize);
992 ad_close( &ad, ADFLAGS_HF );
994 return( AFPERR_BITMAP );
1000 ashort = htons( data - buf );
1001 memcpy(nameoff, &ashort, sizeof( ashort ));
1002 aint = strlen( vol->v_name );
1004 memcpy(data, vol->v_name, aint );
1008 ad_close( &ad, ADFLAGS_HF );
1010 *buflen = data - buf;
1016 int afp_getsrvrparms(obj, ibuf, ibuflen, rbuf, rbuflen )
1019 int ibuflen, *rbuflen;
1032 for ( vcnt = 0, volume = volumes; volume; volume = volume->v_next ) {
1033 if ( stat( volume->v_path, &st ) < 0 ) {
1034 syslog( LOG_INFO, "afp_getsrvrparms: stat %s: %m",
1036 continue; /* can't access directory */
1038 if (!S_ISDIR(st.st_mode)) {
1039 continue; /* not a dir */
1042 /* set password bit if there's a volume password */
1043 *data = (volume->v_password) ? AFPSRVR_PASSWD : 0;
1045 /* Apple 2 clients running ProDOS-8 expect one volume to have
1046 bit 0 of this byte set. They will not recognize anything
1047 on the server unless this is the case. I have not
1048 completely worked this out, but it's related to booting
1049 from the server. Support for that function is a ways
1050 off.. <shirsch@ibm.net> */
1051 *data++ |= (volume->v_flags & AFPVOL_A2VOL) ? AFPSRVR_CONFIGINFO : 0;
1052 len = strlen( volume->v_name );
1054 memcpy(data, volume->v_name, len );
1059 *rbuflen = data - rbuf;
1061 if ( gettimeofday( &tv, 0 ) < 0 ) {
1062 syslog( LOG_ERR, "afp_getsrvrparms: gettimeofday: %m" );
1064 return AFPERR_PARAM;
1066 tv.tv_sec = AD_DATE_FROM_UNIX(tv.tv_sec);
1067 memcpy(data, &tv.tv_sec, sizeof( u_int32_t));
1068 data += sizeof( u_int32_t);
1073 int afp_openvol(obj, ibuf, ibuflen, rbuf, rbuflen )
1076 int ibuflen, *rbuflen;
1080 #if AD_VERSION == AD_VERSION1
1082 #endif /* AD_VERSION == AD_VERSION1 */
1085 int len, ret, buflen;
1089 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1090 bitmap = ntohs( bitmap );
1091 ibuf += sizeof( bitmap );
1092 if (( bitmap & (1<<VOLPBIT_VID)) == 0 ) {
1093 ret = AFPERR_BITMAP;
1097 len = (unsigned char)*ibuf++;
1098 volname = obj->oldtmp;
1099 memcpy(volname, ibuf, len );
1100 *(volname + len) = '\0';
1102 if ((len + 1) & 1) /* pad to an even boundary */
1108 for ( volume = volumes; volume; volume = volume->v_next ) {
1109 if ( strcasecmp( volname, volume->v_name ) == 0 ) {
1114 if ( volume == NULL ) {
1119 /* check for a volume password */
1120 if (volume->v_password &&
1121 strncmp(ibuf, volume->v_password, VOLPASSLEN)) {
1122 ret = AFPERR_ACCESS;
1126 if (( volume->v_flags & AFPVOL_OPEN ) == 0 ) {
1127 if ((dir = dirnew(strlen(volume->v_name) + 1)) == NULL) {
1128 syslog( LOG_ERR, "afp_openvol: malloc: %m" );
1132 dir->d_did = DIRDID_ROOT;
1133 dir->d_color = DIRTREE_COLOR_BLACK; /* root node is black */
1134 strcpy( dir->d_name, volume->v_name );
1135 volume->v_dir = volume->v_root = dir;
1136 volume->v_flags |= AFPVOL_OPEN;
1139 if ( stat( volume->v_path, &st ) < 0 ) {
1144 buflen = *rbuflen - sizeof( bitmap );
1145 if (( ret = getvolparams( bitmap, volume, &st,
1146 rbuf + sizeof(bitmap), &buflen )) != AFP_OK ) {
1149 *rbuflen = buflen + sizeof( bitmap );
1150 bitmap = htons( bitmap );
1151 memcpy(rbuf, &bitmap, sizeof( bitmap ));
1153 curdir = volume->v_dir;
1154 if ( chdir( volume->v_path ) < 0 ) {
1158 #if AD_VERSION == AD_VERSION1
1160 * If you mount a volume twice, the second time the trash appears on
1161 * the desk-top. That's because the Mac remembers the DID for the
1162 * trash (even for volumes in different zones, on different servers).
1163 * Just so this works better, we prime the DID cache with the trash,
1164 * fixing the trash at DID 3.
1167 cname( volume, volume->v_dir, &p );
1168 #endif /* AD_VERSION == AD_VERSION1 */
1177 int afp_closevol(obj, ibuf, ibuflen, rbuf, rbuflen )
1180 int ibuflen, *rbuflen;
1182 struct vol *vol, *ovol;
1187 memcpy(&vid, ibuf, sizeof( vid ));
1188 if (( vol = getvolbyvid( vid )) == NULL ) {
1189 return( AFPERR_PARAM );
1192 vol->v_flags &= ~AFPVOL_OPEN;
1193 for ( ovol = volumes; ovol; ovol = ovol->v_next ) {
1194 if ( ovol->v_flags & AFPVOL_OPEN ) {
1198 if ( ovol != NULL ) {
1199 curdir = ovol->v_dir;
1200 if ( chdir( ovol->v_path ) < 0 ) {
1201 return( AFPERR_PARAM );
1205 dirfree( vol->v_root );
1207 #if AD_VERSION > AD_VERSION1
1208 cnid_close(vol->v_db);
1210 #endif /* AD_VERSION > AD_VERSION1 */
1214 struct vol *getvolbyvid(const u_int16_t vid )
1218 for ( vol = volumes; vol; vol = vol->v_next ) {
1219 if ( vid == vol->v_vid ) {
1223 if ( vol == NULL || ( vol->v_flags & AFPVOL_OPEN ) == 0 ) {
1230 struct extmap *getextmap(const char *path)
1235 if (( p = strrchr( path, '.' )) == NULL ) {
1236 return( defextmap );
1239 for ( em = extmap; em; em = em->em_next ) {
1240 if ( strdiacasecmp( em->em_ext, p ) == 0 ) {
1245 return( defextmap );
1251 void setvoltime(obj, vol )
1257 /* just looking at vol->v_time is broken seriously since updates
1258 * from other users afpd processes never are seen.
1259 * This is not the most elegant solution (a shared memory between
1260 * the afpd processes would come closer)
1263 if ( gettimeofday( &tv, 0 ) < 0 ) {
1264 syslog( LOG_ERR, "setvoltime: gettimeofday: %m" );
1267 if( utime( vol->v_path, NULL ) < 0 ) {
1268 /* write of time failed ... probably a read only filesys,
1269 * where no other users can interfere, so there's no issue here
1273 /* a little granularity */
1274 if (vol->v_time < tv.tv_sec) {
1275 vol->v_time = tv.tv_sec;
1276 obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED);
1280 int afp_getvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1283 int ibuflen, *rbuflen;
1288 u_int16_t vid, bitmap;
1291 memcpy(&vid, ibuf, sizeof( vid ));
1292 ibuf += sizeof( vid );
1293 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1294 bitmap = ntohs( bitmap );
1296 if (( vol = getvolbyvid( vid )) == NULL ) {
1298 return( AFPERR_PARAM );
1301 if ( stat( vol->v_path, &st ) < 0 ) {
1303 return( AFPERR_PARAM );
1306 buflen = *rbuflen - sizeof( bitmap );
1307 if (( ret = getvolparams( bitmap, vol, &st,
1308 rbuf + sizeof( bitmap ), &buflen )) != AFP_OK ) {
1312 *rbuflen = buflen + sizeof( bitmap );
1313 bitmap = htons( bitmap );
1314 memcpy(rbuf, &bitmap, sizeof( bitmap ));
1318 int afp_setvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1321 int ibuflen, *rbuflen;
1325 u_int16_t vid, bitmap;
1331 memcpy(&vid, ibuf, sizeof( vid ));
1332 ibuf += sizeof( vid );
1333 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1334 bitmap = ntohs( bitmap );
1335 ibuf += sizeof(bitmap);
1337 if (( vol = getvolbyvid( vid )) == NULL ) {
1338 return( AFPERR_PARAM );
1341 if (vol->v_flags & AFPVOL_RO)
1342 return AFPERR_VLOCK;
1344 /* we can only set the backup date. */
1345 if (bitmap != VOLPBIT_BDATE)
1346 return AFPERR_BITMAP;
1348 memset(&ad, 0, sizeof(ad));
1349 if ( ad_open( vol->v_path, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR,
1352 return AFPERR_VLOCK;
1354 return AFPERR_ACCESS;
1357 memcpy(&aint, ibuf, sizeof(aint));
1358 ad_setdate(&ad, AD_DATE_BACKUP, aint);
1359 ad_flush(&ad, ADFLAGS_HF);
1360 ad_close(&ad, ADFLAGS_HF);