2 * $Id: volume.c,v 1.5 2001-02-28 16:53:24 rufustfirefly Exp $
4 * Copyright (c) 1990,1993 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
13 #include <sys/syslog.h>
14 #include <sys/types.h>
16 #include <sys/param.h>
17 #include <netatalk/endian.h>
18 #include <atalk/asp.h>
19 #include <atalk/dsi.h>
20 #include <atalk/adouble.h>
21 #include <atalk/afp.h>
22 #include <atalk/util.h>
23 #include <atalk/cnid.h>
35 #include "directory.h"
42 #define MIN(a, b) ((a) < (b) ? (a) : (b))
45 #ifndef NO_LARGE_VOL_SUPPORT
46 #if BYTE_ORDER == BIG_ENDIAN
50 #define hton64(x) ((u_int64_t) (htonl(((x) >> 32) & 0xffffffffLL)) | \
51 (u_int64_t) ((htonl(x) & 0xffffffffLL) << 32))
52 #define ntoh64(x) (hton64(x))
56 static struct vol *volumes = NULL;
57 static int lastvid = 0;
58 #if AD_VERSION == AD_VERSION1
59 static char *Trash = "\02\024Network Trash Folder";
61 static struct extmap *extmap = NULL, *defextmap = NULL;
63 #define VOLOPT_ALLOW 0 /* user allow list */
64 #define VOLOPT_DENY 1 /* user deny list */
65 #define VOLOPT_RWLIST 2 /* user rw list */
66 #define VOLOPT_ROLIST 3 /* user ro list */
67 #define VOLOPT_CODEPAGE 4 /* codepage */
68 #define VOLOPT_PASSWORD 5 /* volume password */
69 #define VOLOPT_CASEFOLD 6 /* character case mangling */
70 #define VOLOPT_FLAGS 7 /* various flags */
71 #define VOLOPT_DBPATH 8 /* path to database */
72 #define VOLOPT_MAPCHARS 9 /* does mtou and utom mappings. syntax:
73 m and u can be double-byte hex
78 !u -> make u illegal always
79 ~u -> make u illegal only as the first
80 part of a double-byte character.
84 #define VOLOPT_FORCEUID 10 /* force uid for username x */
85 #define VOLOPT_FORCEGID 11 /* force gid for group x */
89 #define VOLOPT_NUM (VOLOPT_MAX + 1)
92 #define VOLOPT_DEFAULT ":DEFAULT:"
93 #define VOLOPT_DEFAULT_LEN 9
99 static __inline__ void volfree(struct vol_option *options,
100 const struct vol_option *save)
105 for (i = 0; i < VOLOPT_MAX; i++) {
106 if (options[i].c_value && (options[i].c_value != save[i].c_value))
107 free(options[i].c_value);
110 for (i = 0; i < VOLOPT_MAX; i++) {
111 if (options[i].c_value)
112 free(options[i].c_value);
118 /* handle variable substitutions. here's what we understand:
119 * $c -> client ip/appletalk address
120 * $f -> full name (whatever's in the gecos field)
123 * $s -> server name (hostname if it doesn't exist)
124 * $u -> username (guest is usually nobody)
125 * $v -> volume name (ADEID_NAME or basename)
126 * $z -> zone (may not exist)
129 #define is_var(a, b) (strncmp((a), (b), 2) == 0)
130 static void volxlate(AFPObj *obj, char *dest, int destlen,
131 char *src, struct passwd *pwd, char *path)
136 strncpy(dest, src, destlen);
137 if ((p = strchr(src, '$')) == NULL) /* nothing to do */
140 /* first part of the path. just forward to the next variable. */
141 len = MIN(p - src, destlen);
147 while (p && destlen > 0) {
148 /* now figure out what the variable is */
150 if (is_var(p, "$c")) {
151 if (obj->proto == AFPPROTO_ASP) {
152 ASP asp = obj->handle;
154 len = sprintf(dest, "%u.%u", ntohs(asp->asp_sat.sat_addr.s_net),
155 asp->asp_sat.sat_addr.s_node);
159 } else if (obj->proto == AFPPROTO_DSI) {
160 DSI *dsi = obj->handle;
162 len = sprintf(dest, "%s:%u", inet_ntoa(dsi->client.sin_addr),
163 ntohs(dsi->client.sin_port));
167 } else if (is_var(p, "$f")) {
168 if (q = strchr(pwd->pw_gecos, ','))
171 } else if (is_var(p, "$g")) {
172 struct group *grp = getgrgid(pwd->pw_gid);
175 } else if (is_var(p, "$h")) {
176 q = obj->options.hostname;
177 } else if (is_var(p, "$s")) {
180 else if (obj->options.server) {
181 q = obj->options.server;
183 q = obj->options.hostname;
184 } else if (is_var(p, "$u")) {
186 } else if (is_var(p, "$v")) {
190 memset(&ad, 0, sizeof(ad));
191 if (ad_open(path, ADFLAGS_HF, O_RDONLY, 0, &ad) < 0)
194 if ((len = MIN(ad_getentrylen(&ad, ADEID_NAME), destlen)) > 0) {
195 memcpy(dest, ad_entry(&ad, ADEID_NAME), len);
196 ad_close(&ad, ADFLAGS_HF);
200 ad_close(&ad, ADFLAGS_HF);
201 no_volname: /* simple basename */
202 if ((q = strrchr(path, '/')) == NULL)
204 else if (*(q + 1) != '\0')
208 } else if (is_var(p, "$z")) {
210 } else if (is_var(p, "$$")) {
215 /* copy the stuff over. if we don't understand something that we
216 * should, just skip it over. */
218 len = MIN(p == q ? 2 : strlen(q), destlen);
219 strncpy(dest, q, len);
224 /* stuff up to next $ */
226 p = strchr(src, '$');
227 len = p ? MIN(p - src, destlen) : destlen;
229 strncpy(dest, src, len);
236 /* to make sure that val is valid, make sure to select an opt that
238 #define optionok(buf,opt,val) (strstr((buf),(opt)) && ((val)[1] != '\0'))
240 static __inline__ char *get_codepage_path(const char *path, const char *name)
246 page = (char *) malloc((len = strlen(path)) + strlen(name) + 2);
249 if (path[len - 1] != '/') /* add a / */
257 /* debug: show which codepage directory we are using */
258 syslog(LOG_DEBUG, "using codepage directory: %s", page);
263 /* handle all the options. tmp can't be NULL. */
264 static void volset(struct vol_option *options, char *volname, int vlen,
265 const char *nlspath, const char *tmp)
269 val = strchr(tmp, ':');
270 if (optionok(tmp, "allow:", val)) {
271 if (options[VOLOPT_ALLOW].c_value)
272 free(options[VOLOPT_ALLOW].c_value);
273 options[VOLOPT_ALLOW].c_value = strdup(val + 1);
275 } else if (optionok(tmp, "deny:", val)) {
276 if (options[VOLOPT_DENY].c_value)
277 free(options[VOLOPT_DENY].c_value);
278 options[VOLOPT_DENY].c_value = strdup(val + 1);
280 } else if (optionok(tmp, "rwlist:", val)) {
281 if (options[VOLOPT_RWLIST].c_value)
282 free(options[VOLOPT_RWLIST].c_value);
283 options[VOLOPT_RWLIST].c_value = strdup(val + 1);
285 } else if (optionok(tmp, "rolist:", val)) {
286 if (options[VOLOPT_ROLIST].c_value)
287 free(options[VOLOPT_ROLIST].c_value);
288 options[VOLOPT_ROLIST].c_value = strdup(val + 1);
290 } else if (optionok(tmp, "codepage:", val)) {
291 if (options[VOLOPT_CODEPAGE].c_value)
292 free(options[VOLOPT_CODEPAGE].c_value);
293 options[VOLOPT_CODEPAGE].c_value = get_codepage_path(nlspath, val + 1);
295 } else if (optionok(tmp, "casefold:", val)) {
296 if (strcasecmp(val + 1, "tolower") == 0)
297 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMLOWER;
298 else if (strcasecmp(val + 1, "toupper") == 0)
299 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMUPPER;
300 else if (strcasecmp(val + 1, "xlatelower") == 0)
301 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UUPPERMLOWER;
302 else if (strcasecmp(val + 1, "xlateupper") == 0)
303 options[VOLOPT_CASEFOLD].i_value = AFPVOL_ULOWERMUPPER;
305 } else if (optionok(tmp, "options:", val)) {
308 if ((p = strtok(val + 1, ",")) == NULL) /* nothing */
312 if (strcasecmp(p, "prodos") == 0)
313 options[VOLOPT_FLAGS].i_value |= AFPVOL_A2VOL;
314 else if (strcasecmp(p, "mswindows") == 0) {
315 options[VOLOPT_FLAGS].i_value |= AFPVOL_MSWINDOWS;
316 if (!options[VOLOPT_CODEPAGE].c_value)
317 options[VOLOPT_CODEPAGE].c_value =
318 get_codepage_path(nlspath, MSWINDOWS_CODEPAGE);
320 } else if (strcasecmp(p, "crlf") == 0)
321 options[VOLOPT_FLAGS].i_value |= AFPVOL_CRLF;
322 else if (strcasecmp(p, "noadouble") == 0)
323 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOADOUBLE;
324 else if (strcasecmp(p, "ro") == 0)
325 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
326 else if (strcasecmp(p, "nohex") == 0)
327 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOHEX;
328 else if (strcasecmp(p, "usedots") == 0)
329 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS;
330 else if (strcasecmp(p, "limitsize") == 0)
331 options[VOLOPT_FLAGS].i_value |= AFPVOL_LIMITSIZE;
332 /* support for either "dropbox" or "dropkludge" */
333 else if (strcasecmp(p, "dropbox") == 0)
334 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
335 else if (strcasecmp(p, "dropkludge") == 0)
336 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
338 p = strtok(NULL, ",");
341 #if AD_VERSION > AD_VERSION1
342 } else if (optionok(tmp, "dbpath:", val)) {
343 if (options[VOLOPT_DBPATH].c_value)
344 free(options[VOLOPT_DBPATH].c_value);
346 options[VOLOPT_DBPATH].c_value = strdup(val + 1);
348 } else if (optionok(tmp, "mapchars:",val)) {
349 if (options[VOLOPT_MAPCHARS].c_value)
350 free(options[VOLOPT_MAPCHARS].c_value);
351 options[VOLOPT_MAPCHARS].c_value = strdup(val + 1);
353 } else if (optionok(tmp, "password:", val)) {
354 if (options[VOLOPT_PASSWORD].c_value)
355 free(options[VOLOPT_PASSWORD].c_value);
356 options[VOLOPT_PASSWORD].c_value = strdup(val + 1);
360 /* this code allows forced uid/gid per volume settings */
361 } else if (optionok(tmp, "forceuid:", val)) {
362 if (options[VOLOPT_FORCEUID].c_value)
363 free(options[VOLOPT_FORCEUID].c_value);
364 options[VOLOPT_FORCEUID].c_value = strdup(val + 1);
365 } else if (optionok(tmp, "forcegid:", val)) {
366 if (options[VOLOPT_FORCEGID].c_value)
367 free(options[VOLOPT_FORCEGID].c_value);
368 options[VOLOPT_FORCEGID].c_value = strdup(val + 1);
373 /* ignore unknown options */
374 syslog(LOG_DEBUG, "ignoring unknown volume option: %s", tmp);
377 /* we'll assume it's a volume name. */
378 strncpy(volname, tmp, vlen);
382 static int creatvol(const char *path, char *name, struct vol_option *options)
387 if ( name == NULL || *name == '\0' ) {
388 if ((name = strrchr( path, '/' )) == NULL) {
389 return -1; /* Obviously not a fully qualified path */
392 /* if you wish to share /, you need to specify a name. */
397 for ( volume = volumes; volume; volume = volume->v_next ) {
398 if ( strcasecmp( volume->v_name, name ) == 0 ) {
399 return -1; /* Won't be able to access it, anyway... */
403 vlen = strlen( name );
404 if ( vlen > AFPVOL_NAMELEN ) {
405 vlen = AFPVOL_NAMELEN;
406 name[AFPVOL_NAMELEN] = '\0';
410 (struct vol *)calloc(1, sizeof( struct vol ))) == NULL ) {
411 syslog( LOG_ERR, "creatvol: malloc: %m" );
414 if (( volume->v_name =
415 (char *)malloc( vlen + 1 )) == NULL ) {
416 syslog( LOG_ERR, "creatvol: malloc: %m" );
420 if (( volume->v_path =
421 (char *)malloc( strlen( path ) + 1 )) == NULL ) {
422 syslog( LOG_ERR, "creatvol: malloc: %m" );
423 free(volume->v_name);
428 strcpy( volume->v_name, name);
429 strcpy( volume->v_path, path );
434 volume->v_vid = lastvid++;
435 volume->v_lastdid = 3;
439 /* should we casefold? */
440 volume->v_casefold = options[VOLOPT_CASEFOLD].i_value;
442 /* shift in some flags */
443 volume->v_flags = options[VOLOPT_FLAGS].i_value;
445 /* read in the code pages */
446 if (options[VOLOPT_CODEPAGE].c_value)
447 codepage_read(volume, options[VOLOPT_CODEPAGE].c_value);
449 if (options[VOLOPT_PASSWORD].c_value)
450 volume->v_password = strdup(options[VOLOPT_PASSWORD].c_value);
452 #if AD_VERSION > AD_VERSION1
453 if (options[VOLOPT_DBPATH].c_value)
454 volume->v_dbpath = strdup(options[VOLOPT_DBPATH].c_value);
459 if (options[VOLOPT_FORCEUID].c_value) {
460 volume->v_forceuid = strdup(options[VOLOPT_FORCEUID].c_value);
462 volume->v_forceuid = NULL; /* set as null so as to return 0 later on */
465 if (options[VOLOPT_FORCEGID].c_value) {
466 volume->v_forcegid = strdup(options[VOLOPT_FORCEGID].c_value);
468 volume->v_forcegid = NULL; /* set as null so as to return 0 later on */
475 volume->v_next = volumes;
480 static char *myfgets( buf, size, fp )
489 while ((( c = getc( fp )) != EOF ) && ( size > 0 )) {
490 if ( c == '\n' || c == '\r' ) {
508 /* check access list. this function wants something of the following
510 * @group,name,name2,@group2,name3
512 * a NULL argument allows everybody to have access.
513 * we return three things:
515 * 0: list exists, but name isn't in it
518 static int accessvol(args, name)
522 char buf[MAXPATHLEN + 1], *p;
528 strncpy(buf, args, sizeof(buf));
529 if ((p = strtok(buf, ",")) == NULL) /* nothing, return okay */
533 if (*p == '@') { /* it's a group */
534 if ((gr = getgrnam(p + 1)) && gmem(gr->gr_gid))
536 } else if (strcmp(p, name) == 0) /* it's a user name */
538 p = strtok(NULL, ",");
544 static void setextmap( ext, type, creator, user)
545 char *ext, *type, *creator;
550 for ( em = extmap; em; em = em->em_next ) {
551 if ( strdiacasecmp( em->em_ext, ext ) == 0 ) {
558 (struct extmap *)malloc( sizeof( struct extmap ))) == NULL ) {
559 syslog( LOG_ERR, "setextmap: malloc: %m" );
562 em->em_next = extmap;
564 } else if ( !user ) {
568 strcpy( em->em_ext, ext );
570 if ( *type == '\0' ) {
571 memcpy(em->em_type, "????", sizeof( em->em_type ));
573 memcpy(em->em_type, type, sizeof( em->em_type ));
575 if ( *creator == '\0' ) {
576 memcpy(em->em_creator, "UNIX", sizeof( em->em_creator ));
578 memcpy(em->em_creator, creator, sizeof( em->em_creator ));
581 if ( strcmp( ext, "." ) == 0 ) {
587 * Read a volume configuration file and add the volumes contained within to
588 * the global volume list. If p2 is non-NULL, the file that is opened is
591 * Lines that begin with # and blank lines are ignored.
592 * Volume lines are of the form:
593 * <unix path> [<volume name>] [allow:<user>,<@group>,...] \
594 * [codepage:<file>] [casefold:<num>]
595 * <extension> TYPE [CREATOR]
597 static int readvolfile(obj, p1, p2, user, pwent)
601 struct passwd *pwent;
604 char path[ MAXPATHLEN + 1], tmp[ MAXPATHLEN + 1],
605 volname[ AFPVOL_NAMELEN + 1 ], buf[ BUFSIZ ],
606 type[ 5 ], creator[ 5 ];
609 struct vol_option options[VOLOPT_NUM], save_options[VOLOPT_NUM];
621 if (( fp = fopen( path, "r" )) == NULL ) {
625 memset(save_options, 0, sizeof(save_options));
626 while ( myfgets( buf, sizeof( buf ), fp ) != NULL ) {
627 initline( strlen( buf ), buf );
628 parseline( sizeof( path ) - 1, path );
635 /* change the default options for this file */
636 if (strncmp(path, VOLOPT_DEFAULT, VOLOPT_DEFAULT_LEN) == 0) {
638 for (i = 0; i < VOLOPT_NUM; i++) {
639 if (parseline( sizeof( path ) - VOLOPT_DEFAULT_LEN - 1,
640 path + VOLOPT_DEFAULT_LEN) < 0)
642 volset(save_options, tmp, sizeof(tmp) - 1,
643 obj->options.nlspath, path + VOLOPT_DEFAULT_LEN);
649 if (( p = strchr( path, '/' )) != NULL ) {
657 if ( u == NULL || *u == '\0' || ( pw = getpwnam( u )) == NULL ) {
660 strcpy( tmp, pw->pw_dir );
661 if ( p != NULL && *p != '\0' ) {
668 /* send path through variable substitution */
669 if (*path != '~') /* need to copy path to tmp */
672 pwent = getpwnam(obj->username);
673 volxlate(obj, path, sizeof(path) - 1, tmp, pwent, NULL);
675 /* this is sort of braindead. basically, i want to be
676 * able to specify things in any order, but i don't want to
677 * re-write everything.
679 * currently we have 11 options:
687 * options:prodos,crlf,noadouble,ro
690 * namemask:x,y,!z (not implemented yet)
692 memcpy(options, save_options, sizeof(options));
695 /* read in up to 11 possible options */
696 for (i = 0; i < VOLOPT_NUM; i++) {
697 if (parseline( sizeof( tmp ) - 1, tmp ) < 0)
700 volset(options, volname, sizeof(volname) - 1,
701 obj->options.nlspath, tmp);
704 /* check allow/deny lists:
705 allow -> either no list (-1), or in list (1)
706 deny -> either no list (-1), or not in list (0) */
707 if (accessvol(options[VOLOPT_ALLOW].c_value, obj->username) &&
708 (accessvol(options[VOLOPT_DENY].c_value, obj->username) < 1)) {
710 /* handle read-only behaviour. semantics:
711 * 1) neither the rolist nor the rwlist exist -> rw
712 * 2) rolist exists -> ro if user is in it.
713 * 3) rwlist exists -> ro unless user is in it. */
714 if (((options[VOLOPT_FLAGS].i_value & AFPVOL_RO) == 0) &&
715 ((accessvol(options[VOLOPT_ROLIST].c_value,
716 obj->username) == 1) ||
717 !accessvol(options[VOLOPT_RWLIST].c_value,
719 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
721 /* do variable substitution */
722 volxlate(obj, tmp, sizeof(tmp) - 1, volname, pwent, path);
723 creatvol(path, tmp, options);
725 volfree(options, save_options);
729 parseline( sizeof( type ) - 1, type );
730 parseline( sizeof( creator ) - 1, creator );
731 setextmap( path, type, creator, user);
738 volfree(save_options, NULL);
739 if ( fclose( fp ) != 0 ) {
740 syslog( LOG_ERR, "readvolfile: fclose: %m" );
746 static void load_volumes(AFPObj *obj)
748 struct passwd *pwent = getpwnam(obj->username);
750 if ( (obj->options.flags & OPTION_USERVOLFIRST) == 0 ) {
751 readvolfile(obj, obj->options.systemvol, 0, pwent);
754 if ((*obj->username == '\0') || (obj->options.flags & OPTION_NOUSERVOL)) {
755 readvolfile(obj, obj->options.defaultvol, NULL, 1, pwent);
758 * Read user's AppleVolumes or .AppleVolumes file
759 * If neither are readable, read the default volumes file. if
760 * that doesn't work, create a user share.
762 if ( readvolfile(obj, pwent->pw_dir, "AppleVolumes", 1, pwent) < 0 &&
763 readvolfile(obj, pwent->pw_dir, ".AppleVolumes", 1, pwent) < 0 &&
764 readvolfile(obj, pwent->pw_dir, "applevolumes", 1, pwent) < 0 &&
765 readvolfile(obj, pwent->pw_dir, ".applevolumes", 1, pwent) < 0 &&
766 obj->options.defaultvol != NULL ) {
767 if (readvolfile(obj, obj->options.defaultvol, NULL, 1, pwent) < 0)
768 creatvol(pwent->pw_dir, NULL, NULL);
771 if ( obj->options.flags & OPTION_USERVOLFIRST ) {
772 readvolfile(obj, obj->options.systemvol, NULL, 0, pwent );
776 static int getvolspace( vol, bfree, btotal, xbfree, xbtotal, bsize )
778 u_int32_t *bfree, *btotal, *bsize;
779 VolSpace *xbfree, *xbtotal;
783 #ifndef NO_QUOTA_SUPPORT
784 VolSpace qfree, qtotal;
787 spaceflag = AFPVOL_GVSMASK & vol->v_flags;
788 /* report up to 2GB if afp version is < 2.2 (4GB if not) */
789 maxsize = (vol->v_flags & AFPVOL_A2VOL) ? 0x01fffe00 :
790 (((afp_version < 22) || (vol->v_flags & AFPVOL_LIMITSIZE))
791 ? 0x7fffffffL : 0xffffffffL);
794 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_AFSGVS ) {
795 if ( afs_getvolspace( vol, xbfree, xbtotal, bsize ) == AFP_OK ) {
796 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_AFSGVS;
797 goto getvolspace_done;
802 if (( rc = ustatfs_getvolspace( vol, xbfree, xbtotal,
803 bsize)) != AFP_OK ) {
807 #define min(a,b) ((a)<(b)?(a):(b))
808 #ifndef NO_QUOTA_SUPPORT
809 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_UQUOTA ) {
810 if ( uquota_getvolspace( vol, &qfree, &qtotal, *bsize ) == AFP_OK ) {
811 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_UQUOTA;
812 *xbfree = min(*xbfree, qfree);
813 *xbtotal = min( *xbtotal, qtotal);
814 goto getvolspace_done;
818 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_USTATFS;
821 *bfree = min( *xbfree, maxsize);
822 *btotal = min( *xbtotal, maxsize);
826 static int getvolparams( bitmap, vol, st, buf, buflen )
834 int bit = 0, aint, isad = 1;
836 u_int32_t bfree, btotal, bsize;
837 VolSpace xbfree, xbtotal; /* extended bytes */
838 char *data, *nameoff = NULL;
841 /* courtesy of jallison@whistle.com:
842 * For MacOS8.x support we need to create the
843 * .Parent file here if it doesn't exist. */
845 memset(&ad, 0, sizeof(ad));
846 if ( ad_open( vol->v_path, vol_noadouble(vol) |
847 ADFLAGS_HF|ADFLAGS_DIR, O_RDWR | O_CREAT,
851 } else if (ad_getoflags( &ad, ADFLAGS_HF ) & O_CREAT) {
852 slash = strrchr( vol->v_path, '/' );
858 ad_setentrylen( &ad, ADEID_NAME, strlen( slash ));
859 memcpy(ad_entry( &ad, ADEID_NAME ), slash,
860 ad_getentrylen( &ad, ADEID_NAME ));
861 ad_flush(&ad, ADFLAGS_HF);
864 if (( bitmap & ( (1<<VOLPBIT_BFREE)|(1<<VOLPBIT_BTOTAL) |
865 (1<<VOLPBIT_XBFREE)|(1<<VOLPBIT_XBTOTAL) |
866 (1<<VOLPBIT_BSIZE)) ) != 0 ) {
867 if ( getvolspace( vol, &bfree, &btotal, &xbfree, &xbtotal,
870 ad_close( &ad, ADFLAGS_HF );
872 return( AFPERR_PARAM );
877 while ( bitmap != 0 ) {
878 while (( bitmap & 1 ) == 0 ) {
885 #if AD_VERSION > AD_VERSION1
886 ashort = VOLPBIT_ATTR_FILEID;
890 /* check for read-only.
891 * NOTE: we don't actually set the read-only flag unless
892 * it's passed in that way as it's possible to mount
893 * a read-write filesystem under a read-only one. */
894 if ((vol->v_flags & AFPVOL_RO) ||
895 ((utime(vol->v_path, NULL) < 0) && (errno == EROFS)))
896 ashort |= VOLPBIT_ATTR_RO;
897 ashort = htons(ashort);
898 memcpy(data, &ashort, sizeof( ashort ));
899 data += sizeof( ashort );
903 ashort = htons( AFPVOLSIG_DEFAULT );
904 memcpy(data, &ashort, sizeof( ashort ));
905 data += sizeof( ashort );
909 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
910 aint = AD_DATE_FROM_UNIX(st->st_mtime);
911 memcpy(data, &aint, sizeof( aint ));
912 data += sizeof( aint );
916 if ( st->st_mtime > vol->v_time ) {
917 vol->v_time = st->st_mtime;
918 aint = AD_DATE_FROM_UNIX(st->st_mtime);
920 aint = AD_DATE_FROM_UNIX(vol->v_time);
922 memcpy(data, &aint, sizeof( aint ));
923 data += sizeof( aint );
927 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
928 aint = AD_DATE_START;
929 memcpy(data, &aint, sizeof( aint ));
930 data += sizeof( aint );
934 memcpy(data, &vol->v_vid, sizeof( vol->v_vid ));
935 data += sizeof( vol->v_vid );
939 bfree = htonl( bfree );
940 memcpy(data, &bfree, sizeof( bfree ));
941 data += sizeof( bfree );
944 case VOLPBIT_BTOTAL :
945 btotal = htonl( btotal );
946 memcpy(data, &btotal, sizeof( btotal ));
947 data += sizeof( btotal );
950 #ifndef NO_LARGE_VOL_SUPPORT
951 case VOLPBIT_XBFREE :
952 xbfree = hton64( xbfree );
953 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
954 bcopy(&xbfree, data, sizeof(xbfree));
956 memcpy(data, &xbfree, sizeof( xbfree ));
958 data += sizeof( xbfree );
961 case VOLPBIT_XBTOTAL :
962 xbtotal = hton64( xbtotal );
963 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
964 bcopy(&xbtotal, data, sizeof(xbtotal));
966 memcpy(data, &xbtotal, sizeof( xbtotal ));
968 data += sizeof( xbfree );
974 data += sizeof( u_int16_t );
977 case VOLPBIT_BSIZE: /* block size */
978 bsize = htonl(bsize);
979 memcpy(data, &bsize, sizeof(bsize));
980 data += sizeof(bsize);
985 ad_close( &ad, ADFLAGS_HF );
987 return( AFPERR_BITMAP );
993 ashort = htons( data - buf );
994 memcpy(nameoff, &ashort, sizeof( ashort ));
995 aint = strlen( vol->v_name );
997 memcpy(data, vol->v_name, aint );
1001 ad_close( &ad, ADFLAGS_HF );
1003 *buflen = data - buf;
1009 int afp_getsrvrparms(obj, ibuf, ibuflen, rbuf, rbuflen )
1012 int ibuflen, *rbuflen;
1025 for ( vcnt = 0, volume = volumes; volume; volume = volume->v_next ) {
1026 if ( stat( volume->v_path, &st ) < 0 ) {
1027 syslog( LOG_INFO, "afp_getsrvrparms: stat %s: %m",
1029 continue; /* can't access directory */
1031 if (!S_ISDIR(st.st_mode)) {
1032 continue; /* not a dir */
1035 /* set password bit if there's a volume password */
1036 *data = (volume->v_password) ? AFPSRVR_PASSWD : 0;
1038 /* Apple 2 clients running ProDOS-8 expect one volume to have
1039 bit 0 of this byte set. They will not recognize anything
1040 on the server unless this is the case. I have not
1041 completely worked this out, but it's related to booting
1042 from the server. Support for that function is a ways
1043 off.. <shirsch@ibm.net> */
1044 *data++ |= (volume->v_flags & AFPVOL_A2VOL) ? AFPSRVR_CONFIGINFO : 0;
1045 len = strlen( volume->v_name );
1047 memcpy(data, volume->v_name, len );
1052 *rbuflen = data - rbuf;
1054 if ( gettimeofday( &tv, 0 ) < 0 ) {
1055 syslog( LOG_ERR, "afp_getsrvrparms: gettimeofday: %m" );
1057 return AFPERR_PARAM;
1059 tv.tv_sec = AD_DATE_FROM_UNIX(tv.tv_sec);
1060 memcpy(data, &tv.tv_sec, sizeof( u_int32_t));
1061 data += sizeof( u_int32_t);
1066 int afp_openvol(obj, ibuf, ibuflen, rbuf, rbuflen )
1069 int ibuflen, *rbuflen;
1073 #if AD_VERSION == AD_VERSION1
1078 int len, ret, buflen;
1082 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1083 bitmap = ntohs( bitmap );
1084 ibuf += sizeof( bitmap );
1085 if (( bitmap & (1<<VOLPBIT_VID)) == 0 ) {
1086 ret = AFPERR_BITMAP;
1090 len = (unsigned char)*ibuf++;
1091 volname = obj->oldtmp;
1092 memcpy(volname, ibuf, len );
1093 *(volname + len) = '\0';
1095 if ((len + 1) & 1) /* pad to an even boundary */
1101 for ( volume = volumes; volume; volume = volume->v_next ) {
1102 if ( strcasecmp( volname, volume->v_name ) == 0 ) {
1107 if ( volume == NULL ) {
1112 /* check for a volume password */
1113 if (volume->v_password &&
1114 strncmp(ibuf, volume->v_password, VOLPASSLEN)) {
1115 ret = AFPERR_ACCESS;
1119 if (( volume->v_flags & AFPVOL_OPEN ) == 0 ) {
1120 if ((dir = dirnew(strlen(volume->v_name) + 1)) == NULL) {
1121 syslog( LOG_ERR, "afp_openvol: malloc: %m" );
1125 dir->d_did = DIRDID_ROOT;
1126 dir->d_color = DIRTREE_COLOR_BLACK; /* root node is black */
1127 strcpy( dir->d_name, volume->v_name );
1128 volume->v_dir = volume->v_root = dir;
1129 volume->v_flags |= AFPVOL_OPEN;
1132 if ( stat( volume->v_path, &st ) < 0 ) {
1137 buflen = *rbuflen - sizeof( bitmap );
1138 if (( ret = getvolparams( bitmap, volume, &st,
1139 rbuf + sizeof(bitmap), &buflen )) != AFP_OK ) {
1142 *rbuflen = buflen + sizeof( bitmap );
1143 bitmap = htons( bitmap );
1144 memcpy(rbuf, &bitmap, sizeof( bitmap ));
1146 curdir = volume->v_dir;
1147 if ( chdir( volume->v_path ) < 0 ) {
1151 #if AD_VERSION == AD_VERSION1
1153 * If you mount a volume twice, the second time the trash appears on
1154 * the desk-top. That's because the Mac remembers the DID for the
1155 * trash (even for volumes in different zones, on different servers).
1156 * Just so this works better, we prime the DID cache with the trash,
1157 * fixing the trash at DID 3.
1160 cname( volume, volume->v_dir, &p );
1170 int afp_closevol(obj, ibuf, ibuflen, rbuf, rbuflen )
1173 int ibuflen, *rbuflen;
1175 struct vol *vol, *ovol;
1180 memcpy(&vid, ibuf, sizeof( vid ));
1181 if (( vol = getvolbyvid( vid )) == NULL ) {
1182 return( AFPERR_PARAM );
1185 vol->v_flags &= ~AFPVOL_OPEN;
1186 for ( ovol = volumes; ovol; ovol = ovol->v_next ) {
1187 if ( ovol->v_flags & AFPVOL_OPEN ) {
1191 if ( ovol != NULL ) {
1192 curdir = ovol->v_dir;
1193 if ( chdir( ovol->v_path ) < 0 ) {
1194 return( AFPERR_PARAM );
1198 dirfree( vol->v_root );
1200 #if AD_VERSION > AD_VERSION1
1201 cnid_close(vol->v_db);
1207 struct vol *getvolbyvid(const u_int16_t vid )
1211 for ( vol = volumes; vol; vol = vol->v_next ) {
1212 if ( vid == vol->v_vid ) {
1216 if ( vol == NULL || ( vol->v_flags & AFPVOL_OPEN ) == 0 ) {
1223 struct extmap *getextmap(const char *path)
1228 if (( p = strrchr( path, '.' )) == NULL ) {
1229 return( defextmap );
1232 for ( em = extmap; em; em = em->em_next ) {
1233 if ( strdiacasecmp( em->em_ext, p ) == 0 ) {
1238 return( defextmap );
1244 void setvoltime(obj, vol )
1250 /* just looking at vol->v_time is broken seriously since updates
1251 * from other users afpd processes never are seen.
1252 * This is not the most elegant solution (a shared memory between
1253 * the afpd processes would come closer)
1256 if ( gettimeofday( &tv, 0 ) < 0 ) {
1257 syslog( LOG_ERR, "setvoltime: gettimeofday: %m" );
1260 if( utime( vol->v_path, NULL ) < 0 ) {
1261 /* write of time failed ... probably a read only filesys,
1262 * where no other users can interfere, so there's no issue here
1266 /* a little granularity */
1267 if (vol->v_time < tv.tv_sec) {
1268 vol->v_time = tv.tv_sec;
1269 obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED);
1273 int afp_getvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1276 int ibuflen, *rbuflen;
1281 u_int16_t vid, bitmap;
1284 memcpy(&vid, ibuf, sizeof( vid ));
1285 ibuf += sizeof( vid );
1286 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1287 bitmap = ntohs( bitmap );
1289 if (( vol = getvolbyvid( vid )) == NULL ) {
1291 return( AFPERR_PARAM );
1294 if ( stat( vol->v_path, &st ) < 0 ) {
1296 return( AFPERR_PARAM );
1299 buflen = *rbuflen - sizeof( bitmap );
1300 if (( ret = getvolparams( bitmap, vol, &st,
1301 rbuf + sizeof( bitmap ), &buflen )) != AFP_OK ) {
1305 *rbuflen = buflen + sizeof( bitmap );
1306 bitmap = htons( bitmap );
1307 memcpy(rbuf, &bitmap, sizeof( bitmap ));
1311 int afp_setvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1314 int ibuflen, *rbuflen;
1318 u_int16_t vid, bitmap;
1324 memcpy(&vid, ibuf, sizeof( vid ));
1325 ibuf += sizeof( vid );
1326 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1327 bitmap = ntohs( bitmap );
1328 ibuf += sizeof(bitmap);
1330 if (( vol = getvolbyvid( vid )) == NULL ) {
1331 return( AFPERR_PARAM );
1334 if (vol->v_flags & AFPVOL_RO)
1335 return AFPERR_VLOCK;
1337 /* we can only set the backup date. */
1338 if (bitmap != VOLPBIT_BDATE)
1339 return AFPERR_BITMAP;
1341 memset(&ad, 0, sizeof(ad));
1342 if ( ad_open( vol->v_path, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR,
1345 return AFPERR_VLOCK;
1347 return AFPERR_ACCESS;
1350 memcpy(&aint, ibuf, sizeof(aint));
1351 ad_setdate(&ad, AD_DATE_BACKUP, aint);
1352 ad_flush(&ad, ADFLAGS_HF);
1353 ad_close(&ad, ADFLAGS_HF);