2 * $Id: volume.c,v 1.9 2001-08-14 14:00:10 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.
91 #define VOLOPT_FORCEUID 10 /* force uid for username x */
92 #define VOLOPT_FORCEGID 11 /* force gid for group x */
94 #else /* normally, there are only 9 possible options */
96 #endif /* FORCE_UIDGID */
98 #define VOLOPT_NUM (VOLOPT_MAX + 1)
101 #define VOLOPT_DEFAULT ":DEFAULT:"
102 #define VOLOPT_DEFAULT_LEN 9
108 static __inline__ void volfree(struct vol_option *options,
109 const struct vol_option *save)
114 for (i = 0; i < VOLOPT_MAX; i++) {
115 if (options[i].c_value && (options[i].c_value != save[i].c_value))
116 free(options[i].c_value);
119 for (i = 0; i < VOLOPT_MAX; i++) {
120 if (options[i].c_value)
121 free(options[i].c_value);
127 /* handle variable substitutions. here's what we understand:
128 * $c -> client ip/appletalk address
129 * $f -> full name (whatever's in the gecos field)
132 * $s -> server name (hostname if it doesn't exist)
133 * $u -> username (guest is usually nobody)
134 * $v -> volume name (ADEID_NAME or basename)
135 * $z -> zone (may not exist)
138 #define is_var(a, b) (strncmp((a), (b), 2) == 0)
139 static void volxlate(AFPObj *obj, char *dest, int destlen,
140 char *src, struct passwd *pwd, char *path)
145 strncpy(dest, src, destlen);
146 if ((p = strchr(src, '$')) == NULL) /* nothing to do */
149 /* first part of the path. just forward to the next variable. */
150 len = MIN(p - src, destlen);
156 while (p && destlen > 0) {
157 /* now figure out what the variable is */
159 if (is_var(p, "$c")) {
160 if (obj->proto == AFPPROTO_ASP) {
161 ASP asp = obj->handle;
163 len = sprintf(dest, "%u.%u", ntohs(asp->asp_sat.sat_addr.s_net),
164 asp->asp_sat.sat_addr.s_node);
168 } else if (obj->proto == AFPPROTO_DSI) {
169 DSI *dsi = obj->handle;
171 len = sprintf(dest, "%s:%u", inet_ntoa(dsi->client.sin_addr),
172 ntohs(dsi->client.sin_port));
176 } else if (is_var(p, "$f")) {
177 if ((q = strchr(pwd->pw_gecos, ',')))
180 } else if (is_var(p, "$g")) {
181 struct group *grp = getgrgid(pwd->pw_gid);
184 } else if (is_var(p, "$h")) {
185 q = obj->options.hostname;
186 } else if (is_var(p, "$s")) {
189 else if (obj->options.server) {
190 q = obj->options.server;
192 q = obj->options.hostname;
193 } else if (is_var(p, "$u")) {
195 } else if (is_var(p, "$v")) {
199 memset(&ad, 0, sizeof(ad));
200 if (ad_open(path, ADFLAGS_HF, O_RDONLY, 0, &ad) < 0)
203 if ((len = MIN(ad_getentrylen(&ad, ADEID_NAME), destlen)) > 0) {
204 memcpy(dest, ad_entry(&ad, ADEID_NAME), len);
205 ad_close(&ad, ADFLAGS_HF);
209 ad_close(&ad, ADFLAGS_HF);
210 no_volname: /* simple basename */
211 if ((q = strrchr(path, '/')) == NULL)
213 else if (*(q + 1) != '\0')
217 } else if (is_var(p, "$z")) {
219 } else if (is_var(p, "$$")) {
224 /* copy the stuff over. if we don't understand something that we
225 * should, just skip it over. */
227 len = MIN(p == q ? 2 : strlen(q), destlen);
228 strncpy(dest, q, len);
233 /* stuff up to next $ */
235 p = strchr(src, '$');
236 len = p ? MIN(p - src, destlen) : destlen;
238 strncpy(dest, src, len);
245 /* to make sure that val is valid, make sure to select an opt that
247 #define optionok(buf,opt,val) (strstr((buf),(opt)) && ((val)[1] != '\0'))
249 static __inline__ char *get_codepage_path(const char *path, const char *name)
255 page = (char *) malloc((len = strlen(path)) + strlen(name) + 2);
258 if (path[len - 1] != '/') /* add a / */
266 /* debug: show which codepage directory we are using */
267 syslog(LOG_DEBUG, "using codepage directory: %s", page);
272 /* handle all the options. tmp can't be NULL. */
273 static void volset(struct vol_option *options, char *volname, int vlen,
274 const char *nlspath, const char *tmp)
278 val = strchr(tmp, ':');
279 if (optionok(tmp, "allow:", val)) {
280 if (options[VOLOPT_ALLOW].c_value)
281 free(options[VOLOPT_ALLOW].c_value);
282 options[VOLOPT_ALLOW].c_value = strdup(val + 1);
284 } else if (optionok(tmp, "deny:", val)) {
285 if (options[VOLOPT_DENY].c_value)
286 free(options[VOLOPT_DENY].c_value);
287 options[VOLOPT_DENY].c_value = strdup(val + 1);
289 } else if (optionok(tmp, "rwlist:", val)) {
290 if (options[VOLOPT_RWLIST].c_value)
291 free(options[VOLOPT_RWLIST].c_value);
292 options[VOLOPT_RWLIST].c_value = strdup(val + 1);
294 } else if (optionok(tmp, "rolist:", val)) {
295 if (options[VOLOPT_ROLIST].c_value)
296 free(options[VOLOPT_ROLIST].c_value);
297 options[VOLOPT_ROLIST].c_value = strdup(val + 1);
299 } else if (optionok(tmp, "codepage:", val)) {
300 if (options[VOLOPT_CODEPAGE].c_value)
301 free(options[VOLOPT_CODEPAGE].c_value);
302 options[VOLOPT_CODEPAGE].c_value = get_codepage_path(nlspath, val + 1);
304 } else if (optionok(tmp, "casefold:", val)) {
305 if (strcasecmp(val + 1, "tolower") == 0)
306 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMLOWER;
307 else if (strcasecmp(val + 1, "toupper") == 0)
308 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMUPPER;
309 else if (strcasecmp(val + 1, "xlatelower") == 0)
310 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UUPPERMLOWER;
311 else if (strcasecmp(val + 1, "xlateupper") == 0)
312 options[VOLOPT_CASEFOLD].i_value = AFPVOL_ULOWERMUPPER;
314 } else if (optionok(tmp, "options:", val)) {
317 if ((p = strtok(val + 1, ",")) == NULL) /* nothing */
321 if (strcasecmp(p, "prodos") == 0)
322 options[VOLOPT_FLAGS].i_value |= AFPVOL_A2VOL;
323 else if (strcasecmp(p, "mswindows") == 0) {
324 options[VOLOPT_FLAGS].i_value |= AFPVOL_MSWINDOWS;
325 if (!options[VOLOPT_CODEPAGE].c_value)
326 options[VOLOPT_CODEPAGE].c_value =
327 get_codepage_path(nlspath, MSWINDOWS_CODEPAGE);
329 } else if (strcasecmp(p, "crlf") == 0)
330 options[VOLOPT_FLAGS].i_value |= AFPVOL_CRLF;
331 else if (strcasecmp(p, "noadouble") == 0)
332 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOADOUBLE;
333 else if (strcasecmp(p, "ro") == 0)
334 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
335 else if (strcasecmp(p, "nohex") == 0)
336 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOHEX;
337 else if (strcasecmp(p, "usedots") == 0)
338 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS;
339 else if (strcasecmp(p, "limitsize") == 0)
340 options[VOLOPT_FLAGS].i_value |= AFPVOL_LIMITSIZE;
341 /* support for either "dropbox" or "dropkludge" */
342 else if (strcasecmp(p, "dropbox") == 0)
343 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
344 else if (strcasecmp(p, "dropkludge") == 0)
345 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
347 p = strtok(NULL, ",");
351 } else if (optionok(tmp, "dbpath:", val)) {
352 if (options[VOLOPT_DBPATH].c_value)
353 free(options[VOLOPT_DBPATH].c_value);
355 options[VOLOPT_DBPATH].c_value = strdup(val + 1);
357 } else if (optionok(tmp, "mapchars:",val)) {
358 if (options[VOLOPT_MAPCHARS].c_value)
359 free(options[VOLOPT_MAPCHARS].c_value);
360 options[VOLOPT_MAPCHARS].c_value = strdup(val + 1);
362 } else if (optionok(tmp, "password:", val)) {
363 if (options[VOLOPT_PASSWORD].c_value)
364 free(options[VOLOPT_PASSWORD].c_value);
365 options[VOLOPT_PASSWORD].c_value = strdup(val + 1);
369 /* this code allows forced uid/gid per volume settings */
370 } else if (optionok(tmp, "forceuid:", val)) {
371 if (options[VOLOPT_FORCEUID].c_value)
372 free(options[VOLOPT_FORCEUID].c_value);
373 options[VOLOPT_FORCEUID].c_value = strdup(val + 1);
374 } else if (optionok(tmp, "forcegid:", val)) {
375 if (options[VOLOPT_FORCEGID].c_value)
376 free(options[VOLOPT_FORCEGID].c_value);
377 options[VOLOPT_FORCEGID].c_value = strdup(val + 1);
379 #endif /* FORCE_UIDGID */
382 /* ignore unknown options */
383 syslog(LOG_DEBUG, "ignoring unknown volume option: %s", tmp);
386 /* we'll assume it's a volume name. */
387 strncpy(volname, tmp, vlen);
391 static int creatvol(const char *path, char *name, struct vol_option *options)
396 if ( name == NULL || *name == '\0' ) {
397 if ((name = strrchr( path, '/' )) == NULL) {
398 return -1; /* Obviously not a fully qualified path */
401 /* if you wish to share /, you need to specify a name. */
406 for ( volume = volumes; volume; volume = volume->v_next ) {
407 if ( strcasecmp( volume->v_name, name ) == 0 ) {
408 return -1; /* Won't be able to access it, anyway... */
412 vlen = strlen( name );
413 if ( vlen > AFPVOL_NAMELEN ) {
414 vlen = AFPVOL_NAMELEN;
415 name[AFPVOL_NAMELEN] = '\0';
419 (struct vol *)calloc(1, sizeof( struct vol ))) == NULL ) {
420 syslog( LOG_ERR, "creatvol: malloc: %m" );
423 if (( volume->v_name =
424 (char *)malloc( vlen + 1 )) == NULL ) {
425 syslog( LOG_ERR, "creatvol: malloc: %m" );
429 if (( volume->v_path =
430 (char *)malloc( strlen( path ) + 1 )) == NULL ) {
431 syslog( LOG_ERR, "creatvol: malloc: %m" );
432 free(volume->v_name);
437 strcpy( volume->v_name, name);
438 strcpy( volume->v_path, path );
442 #endif /* __svr4__ */
443 volume->v_vid = lastvid++;
444 volume->v_lastdid = 3;
448 /* should we casefold? */
449 volume->v_casefold = options[VOLOPT_CASEFOLD].i_value;
451 /* shift in some flags */
452 volume->v_flags = options[VOLOPT_FLAGS].i_value;
454 /* read in the code pages */
455 if (options[VOLOPT_CODEPAGE].c_value)
456 codepage_read(volume, options[VOLOPT_CODEPAGE].c_value);
458 if (options[VOLOPT_PASSWORD].c_value)
459 volume->v_password = strdup(options[VOLOPT_PASSWORD].c_value);
462 if (options[VOLOPT_DBPATH].c_value)
463 volume->v_dbpath = strdup(options[VOLOPT_DBPATH].c_value);
468 if (options[VOLOPT_FORCEUID].c_value) {
469 volume->v_forceuid = strdup(options[VOLOPT_FORCEUID].c_value);
471 volume->v_forceuid = NULL; /* set as null so as to return 0 later on */
474 if (options[VOLOPT_FORCEGID].c_value) {
475 volume->v_forcegid = strdup(options[VOLOPT_FORCEGID].c_value);
477 volume->v_forcegid = NULL; /* set as null so as to return 0 later on */
480 #endif /* FORCE_UIDGID */
484 volume->v_next = volumes;
489 static char *myfgets( buf, size, fp )
498 while ((( c = getc( fp )) != EOF ) && ( size > 0 )) {
499 if ( c == '\n' || c == '\r' ) {
517 /* check access list. this function wants something of the following
519 * @group,name,name2,@group2,name3
521 * a NULL argument allows everybody to have access.
522 * we return three things:
524 * 0: list exists, but name isn't in it
527 static int accessvol(args, name)
531 char buf[MAXPATHLEN + 1], *p;
537 strncpy(buf, args, sizeof(buf));
538 if ((p = strtok(buf, ",")) == NULL) /* nothing, return okay */
542 if (*p == '@') { /* it's a group */
543 if ((gr = getgrnam(p + 1)) && gmem(gr->gr_gid))
545 } else if (strcmp(p, name) == 0) /* it's a user name */
547 p = strtok(NULL, ",");
553 static void setextmap( ext, type, creator, user)
554 char *ext, *type, *creator;
559 for ( em = extmap; em; em = em->em_next ) {
560 if ( strdiacasecmp( em->em_ext, ext ) == 0 ) {
567 (struct extmap *)malloc( sizeof( struct extmap ))) == NULL ) {
568 syslog( LOG_ERR, "setextmap: malloc: %m" );
571 em->em_next = extmap;
573 } else if ( !user ) {
577 strcpy( em->em_ext, ext );
579 if ( *type == '\0' ) {
580 memcpy(em->em_type, "????", sizeof( em->em_type ));
582 memcpy(em->em_type, type, sizeof( em->em_type ));
584 if ( *creator == '\0' ) {
585 memcpy(em->em_creator, "UNIX", sizeof( em->em_creator ));
587 memcpy(em->em_creator, creator, sizeof( em->em_creator ));
590 if ( strcmp( ext, "." ) == 0 ) {
596 * Read a volume configuration file and add the volumes contained within to
597 * the global volume list. If p2 is non-NULL, the file that is opened is
600 * Lines that begin with # and blank lines are ignored.
601 * Volume lines are of the form:
602 * <unix path> [<volume name>] [allow:<user>,<@group>,...] \
603 * [codepage:<file>] [casefold:<num>]
604 * <extension> TYPE [CREATOR]
606 static int readvolfile(obj, p1, p2, user, pwent)
610 struct passwd *pwent;
613 char path[ MAXPATHLEN + 1], tmp[ MAXPATHLEN + 1],
614 volname[ AFPVOL_NAMELEN + 1 ], buf[ BUFSIZ ],
615 type[ 5 ], creator[ 5 ];
618 struct vol_option options[VOLOPT_NUM], save_options[VOLOPT_NUM];
630 if (( fp = fopen( path, "r" )) == NULL ) {
634 memset(save_options, 0, sizeof(save_options));
635 while ( myfgets( buf, sizeof( buf ), fp ) != NULL ) {
636 initline( strlen( buf ), buf );
637 parseline( sizeof( path ) - 1, path );
644 /* change the default options for this file */
645 if (strncmp(path, VOLOPT_DEFAULT, VOLOPT_DEFAULT_LEN) == 0) {
647 for (i = 0; i < VOLOPT_NUM; i++) {
648 if (parseline( sizeof( path ) - VOLOPT_DEFAULT_LEN - 1,
649 path + VOLOPT_DEFAULT_LEN) < 0)
651 volset(save_options, tmp, sizeof(tmp) - 1,
652 obj->options.nlspath, path + VOLOPT_DEFAULT_LEN);
658 if (( p = strchr( path, '/' )) != NULL ) {
666 if ( u == NULL || *u == '\0' || ( pw = getpwnam( u )) == NULL ) {
669 strcpy( tmp, pw->pw_dir );
670 if ( p != NULL && *p != '\0' ) {
677 /* send path through variable substitution */
678 if (*path != '~') /* need to copy path to tmp */
681 pwent = getpwnam(obj->username);
682 volxlate(obj, path, sizeof(path) - 1, tmp, pwent, NULL);
684 /* this is sort of braindead. basically, i want to be
685 * able to specify things in any order, but i don't want to
686 * re-write everything.
688 * currently we have 11 options:
696 * options:prodos,crlf,noadouble,ro
699 * namemask:x,y,!z (not implemented yet)
701 memcpy(options, save_options, sizeof(options));
704 /* read in up to 11 possible options */
705 for (i = 0; i < VOLOPT_NUM; i++) {
706 if (parseline( sizeof( tmp ) - 1, tmp ) < 0)
709 volset(options, volname, sizeof(volname) - 1,
710 obj->options.nlspath, tmp);
713 /* check allow/deny lists:
714 allow -> either no list (-1), or in list (1)
715 deny -> either no list (-1), or not in list (0) */
716 if (accessvol(options[VOLOPT_ALLOW].c_value, obj->username) &&
717 (accessvol(options[VOLOPT_DENY].c_value, obj->username) < 1)) {
719 /* handle read-only behaviour. semantics:
720 * 1) neither the rolist nor the rwlist exist -> rw
721 * 2) rolist exists -> ro if user is in it.
722 * 3) rwlist exists -> ro unless user is in it. */
723 if (((options[VOLOPT_FLAGS].i_value & AFPVOL_RO) == 0) &&
724 ((accessvol(options[VOLOPT_ROLIST].c_value,
725 obj->username) == 1) ||
726 !accessvol(options[VOLOPT_RWLIST].c_value,
728 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
730 /* do variable substitution */
731 volxlate(obj, tmp, sizeof(tmp) - 1, volname, pwent, path);
732 creatvol(path, tmp, options);
734 volfree(options, save_options);
738 parseline( sizeof( type ) - 1, type );
739 parseline( sizeof( creator ) - 1, creator );
740 setextmap( path, type, creator, user);
747 volfree(save_options, NULL);
748 if ( fclose( fp ) != 0 ) {
749 syslog( LOG_ERR, "readvolfile: fclose: %m" );
755 static void load_volumes(AFPObj *obj)
757 struct passwd *pwent = getpwnam(obj->username);
759 if ( (obj->options.flags & OPTION_USERVOLFIRST) == 0 ) {
760 readvolfile(obj, obj->options.systemvol, 0, pwent);
763 if ((*obj->username == '\0') || (obj->options.flags & OPTION_NOUSERVOL)) {
764 readvolfile(obj, obj->options.defaultvol, NULL, 1, pwent);
767 * Read user's AppleVolumes or .AppleVolumes file
768 * If neither are readable, read the default volumes file. if
769 * that doesn't work, create a user share.
771 if ( readvolfile(obj, pwent->pw_dir, "AppleVolumes", 1, pwent) < 0 &&
772 readvolfile(obj, pwent->pw_dir, ".AppleVolumes", 1, pwent) < 0 &&
773 readvolfile(obj, pwent->pw_dir, "applevolumes", 1, pwent) < 0 &&
774 readvolfile(obj, pwent->pw_dir, ".applevolumes", 1, pwent) < 0 &&
775 obj->options.defaultvol != NULL ) {
776 if (readvolfile(obj, obj->options.defaultvol, NULL, 1, pwent) < 0)
777 creatvol(pwent->pw_dir, NULL, NULL);
780 if ( obj->options.flags & OPTION_USERVOLFIRST ) {
781 readvolfile(obj, obj->options.systemvol, NULL, 0, pwent );
785 static int getvolspace( vol, bfree, btotal, xbfree, xbtotal, bsize )
787 u_int32_t *bfree, *btotal, *bsize;
788 VolSpace *xbfree, *xbtotal;
792 #ifndef NO_QUOTA_SUPPORT
793 VolSpace qfree, qtotal;
794 #endif /* ! NO_QUOTA_SUPPORT */
796 spaceflag = AFPVOL_GVSMASK & vol->v_flags;
797 /* report up to 2GB if afp version is < 2.2 (4GB if not) */
798 maxsize = (vol->v_flags & AFPVOL_A2VOL) ? 0x01fffe00 :
799 (((afp_version < 22) || (vol->v_flags & AFPVOL_LIMITSIZE))
800 ? 0x7fffffffL : 0xffffffffL);
803 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_AFSGVS ) {
804 if ( afs_getvolspace( vol, xbfree, xbtotal, bsize ) == AFP_OK ) {
805 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_AFSGVS;
806 goto getvolspace_done;
811 if (( rc = ustatfs_getvolspace( vol, xbfree, xbtotal,
812 bsize)) != AFP_OK ) {
816 #define min(a,b) ((a)<(b)?(a):(b))
817 #ifndef NO_QUOTA_SUPPORT
818 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_UQUOTA ) {
819 if ( uquota_getvolspace( vol, &qfree, &qtotal, *bsize ) == AFP_OK ) {
820 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_UQUOTA;
821 *xbfree = min(*xbfree, qfree);
822 *xbtotal = min( *xbtotal, qtotal);
823 goto getvolspace_done;
826 #endif /* ! NO_QUOTA_SUPPORT */
827 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_USTATFS;
830 *bfree = min( *xbfree, maxsize);
831 *btotal = min( *xbtotal, maxsize);
835 static int getvolparams( bitmap, vol, st, buf, buflen )
843 int bit = 0, aint, isad = 1;
845 u_int32_t bfree, btotal, bsize;
846 VolSpace xbfree, xbtotal; /* extended bytes */
847 char *data, *nameoff = NULL;
850 /* courtesy of jallison@whistle.com:
851 * For MacOS8.x support we need to create the
852 * .Parent file here if it doesn't exist. */
854 memset(&ad, 0, sizeof(ad));
855 if ( ad_open( vol->v_path, vol_noadouble(vol) |
856 ADFLAGS_HF|ADFLAGS_DIR, O_RDWR | O_CREAT,
860 } else if (ad_getoflags( &ad, ADFLAGS_HF ) & O_CREAT) {
861 slash = strrchr( vol->v_path, '/' );
867 ad_setentrylen( &ad, ADEID_NAME, strlen( slash ));
868 memcpy(ad_entry( &ad, ADEID_NAME ), slash,
869 ad_getentrylen( &ad, ADEID_NAME ));
870 ad_flush(&ad, ADFLAGS_HF);
873 if (( bitmap & ( (1<<VOLPBIT_BFREE)|(1<<VOLPBIT_BTOTAL) |
874 (1<<VOLPBIT_XBFREE)|(1<<VOLPBIT_XBTOTAL) |
875 (1<<VOLPBIT_BSIZE)) ) != 0 ) {
876 if ( getvolspace( vol, &bfree, &btotal, &xbfree, &xbtotal,
879 ad_close( &ad, ADFLAGS_HF );
881 return( AFPERR_PARAM );
886 while ( bitmap != 0 ) {
887 while (( bitmap & 1 ) == 0 ) {
895 ashort = VOLPBIT_ATTR_FILEID;
899 /* check for read-only.
900 * NOTE: we don't actually set the read-only flag unless
901 * it's passed in that way as it's possible to mount
902 * a read-write filesystem under a read-only one. */
903 if ((vol->v_flags & AFPVOL_RO) ||
904 ((utime(vol->v_path, NULL) < 0) && (errno == EROFS)))
905 ashort |= VOLPBIT_ATTR_RO;
906 ashort = htons(ashort);
907 memcpy(data, &ashort, sizeof( ashort ));
908 data += sizeof( ashort );
912 ashort = htons( AFPVOLSIG_DEFAULT );
913 memcpy(data, &ashort, sizeof( ashort ));
914 data += sizeof( ashort );
918 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
919 aint = AD_DATE_FROM_UNIX(st->st_mtime);
920 memcpy(data, &aint, sizeof( aint ));
921 data += sizeof( aint );
925 if ( st->st_mtime > vol->v_time ) {
926 vol->v_time = st->st_mtime;
927 aint = AD_DATE_FROM_UNIX(st->st_mtime);
929 aint = AD_DATE_FROM_UNIX(vol->v_time);
931 memcpy(data, &aint, sizeof( aint ));
932 data += sizeof( aint );
936 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
937 aint = AD_DATE_START;
938 memcpy(data, &aint, sizeof( aint ));
939 data += sizeof( aint );
943 memcpy(data, &vol->v_vid, sizeof( vol->v_vid ));
944 data += sizeof( vol->v_vid );
948 bfree = htonl( bfree );
949 memcpy(data, &bfree, sizeof( bfree ));
950 data += sizeof( bfree );
953 case VOLPBIT_BTOTAL :
954 btotal = htonl( btotal );
955 memcpy(data, &btotal, sizeof( btotal ));
956 data += sizeof( btotal );
959 #ifndef NO_LARGE_VOL_SUPPORT
960 case VOLPBIT_XBFREE :
961 xbfree = hton64( xbfree );
962 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
963 bcopy(&xbfree, data, sizeof(xbfree));
964 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
965 memcpy(data, &xbfree, sizeof( xbfree ));
966 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
967 data += sizeof( xbfree );
970 case VOLPBIT_XBTOTAL :
971 xbtotal = hton64( xbtotal );
972 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
973 bcopy(&xbtotal, data, sizeof(xbtotal));
974 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
975 memcpy(data, &xbtotal, sizeof( xbtotal ));
976 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
977 data += sizeof( xbfree );
979 #endif /* ! NO_LARGE_VOL_SUPPORT */
983 data += sizeof( u_int16_t );
986 case VOLPBIT_BSIZE: /* block size */
987 bsize = htonl(bsize);
988 memcpy(data, &bsize, sizeof(bsize));
989 data += sizeof(bsize);
994 ad_close( &ad, ADFLAGS_HF );
996 return( AFPERR_BITMAP );
1002 ashort = htons( data - buf );
1003 memcpy(nameoff, &ashort, sizeof( ashort ));
1004 aint = strlen( vol->v_name );
1006 memcpy(data, vol->v_name, aint );
1010 ad_close( &ad, ADFLAGS_HF );
1012 *buflen = data - buf;
1018 int afp_getsrvrparms(obj, ibuf, ibuflen, rbuf, rbuflen )
1021 int ibuflen, *rbuflen;
1034 for ( vcnt = 0, volume = volumes; volume; volume = volume->v_next ) {
1035 if ( stat( volume->v_path, &st ) < 0 ) {
1036 syslog( LOG_INFO, "afp_getsrvrparms: stat %s: %m",
1038 continue; /* can't access directory */
1040 if (!S_ISDIR(st.st_mode)) {
1041 continue; /* not a dir */
1044 /* set password bit if there's a volume password */
1045 *data = (volume->v_password) ? AFPSRVR_PASSWD : 0;
1047 /* Apple 2 clients running ProDOS-8 expect one volume to have
1048 bit 0 of this byte set. They will not recognize anything
1049 on the server unless this is the case. I have not
1050 completely worked this out, but it's related to booting
1051 from the server. Support for that function is a ways
1052 off.. <shirsch@ibm.net> */
1053 *data++ |= (volume->v_flags & AFPVOL_A2VOL) ? AFPSRVR_CONFIGINFO : 0;
1054 len = strlen( volume->v_name );
1056 memcpy(data, volume->v_name, len );
1061 *rbuflen = data - rbuf;
1063 if ( gettimeofday( &tv, 0 ) < 0 ) {
1064 syslog( LOG_ERR, "afp_getsrvrparms: gettimeofday: %m" );
1066 return AFPERR_PARAM;
1068 tv.tv_sec = AD_DATE_FROM_UNIX(tv.tv_sec);
1069 memcpy(data, &tv.tv_sec, sizeof( u_int32_t));
1070 data += sizeof( u_int32_t);
1075 int afp_openvol(obj, ibuf, ibuflen, rbuf, rbuflen )
1078 int ibuflen, *rbuflen;
1084 #endif /* CNID_DB */
1087 int len, ret, buflen;
1091 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1092 bitmap = ntohs( bitmap );
1093 ibuf += sizeof( bitmap );
1094 if (( bitmap & (1<<VOLPBIT_VID)) == 0 ) {
1095 ret = AFPERR_BITMAP;
1099 len = (unsigned char)*ibuf++;
1100 volname = obj->oldtmp;
1101 memcpy(volname, ibuf, len );
1102 *(volname + len) = '\0';
1104 if ((len + 1) & 1) /* pad to an even boundary */
1110 for ( volume = volumes; volume; volume = volume->v_next ) {
1111 if ( strcasecmp( volname, volume->v_name ) == 0 ) {
1116 if ( volume == NULL ) {
1121 /* check for a volume password */
1122 if (volume->v_password &&
1123 strncmp(ibuf, volume->v_password, VOLPASSLEN)) {
1124 ret = AFPERR_ACCESS;
1128 if (( volume->v_flags & AFPVOL_OPEN ) == 0 ) {
1129 if ((dir = dirnew(strlen(volume->v_name) + 1)) == NULL) {
1130 syslog( LOG_ERR, "afp_openvol: malloc: %m" );
1134 dir->d_did = DIRDID_ROOT;
1135 dir->d_color = DIRTREE_COLOR_BLACK; /* root node is black */
1136 strcpy( dir->d_name, volume->v_name );
1137 volume->v_dir = volume->v_root = dir;
1138 volume->v_flags |= AFPVOL_OPEN;
1141 if ( stat( volume->v_path, &st ) < 0 ) {
1146 buflen = *rbuflen - sizeof( bitmap );
1147 if (( ret = getvolparams( bitmap, volume, &st,
1148 rbuf + sizeof(bitmap), &buflen )) != AFP_OK ) {
1151 *rbuflen = buflen + sizeof( bitmap );
1152 bitmap = htons( bitmap );
1153 memcpy(rbuf, &bitmap, sizeof( bitmap ));
1155 curdir = volume->v_dir;
1156 if ( chdir( volume->v_path ) < 0 ) {
1162 if (volume->v_dbpath)
1163 volume->v_db = cnid_open (volume->v_dbpath);
1164 if (volume->v_db == 0)
1165 volume->v_db = cnid_open (volume->v_path);
1166 #endif /* CNID_DB */
1170 * If you mount a volume twice, the second time the trash appears on
1171 * the desk-top. That's because the Mac remembers the DID for the
1172 * trash (even for volumes in different zones, on different servers).
1173 * Just so this works better, we prime the DID cache with the trash,
1174 * fixing the trash at DID 3.
1177 cname( volume, volume->v_dir, &p );
1178 #endif /* CNID_DB */
1187 int afp_closevol(obj, ibuf, ibuflen, rbuf, rbuflen )
1190 int ibuflen, *rbuflen;
1192 struct vol *vol, *ovol;
1197 memcpy(&vid, ibuf, sizeof( vid ));
1198 if (( vol = getvolbyvid( vid )) == NULL ) {
1199 return( AFPERR_PARAM );
1202 vol->v_flags &= ~AFPVOL_OPEN;
1203 for ( ovol = volumes; ovol; ovol = ovol->v_next ) {
1204 if ( ovol->v_flags & AFPVOL_OPEN ) {
1208 if ( ovol != NULL ) {
1209 curdir = ovol->v_dir;
1210 if ( chdir( ovol->v_path ) < 0 ) {
1211 return( AFPERR_PARAM );
1215 dirfree( vol->v_root );
1218 cnid_close(vol->v_db);
1220 #endif /* AD_VERSION > AD_VERSION1 */
1224 struct vol *getvolbyvid(const u_int16_t vid )
1228 for ( vol = volumes; vol; vol = vol->v_next ) {
1229 if ( vid == vol->v_vid ) {
1233 if ( vol == NULL || ( vol->v_flags & AFPVOL_OPEN ) == 0 ) {
1240 struct extmap *getextmap(const char *path)
1245 if (( p = strrchr( path, '.' )) == NULL ) {
1246 return( defextmap );
1249 for ( em = extmap; em; em = em->em_next ) {
1250 if ( strdiacasecmp( em->em_ext, p ) == 0 ) {
1255 return( defextmap );
1261 void setvoltime(obj, vol )
1267 /* just looking at vol->v_time is broken seriously since updates
1268 * from other users afpd processes never are seen.
1269 * This is not the most elegant solution (a shared memory between
1270 * the afpd processes would come closer)
1273 if ( gettimeofday( &tv, 0 ) < 0 ) {
1274 syslog( LOG_ERR, "setvoltime: gettimeofday: %m" );
1277 if( utime( vol->v_path, NULL ) < 0 ) {
1278 /* write of time failed ... probably a read only filesys,
1279 * where no other users can interfere, so there's no issue here
1283 /* a little granularity */
1284 if (vol->v_time < tv.tv_sec) {
1285 vol->v_time = tv.tv_sec;
1286 obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED);
1290 int afp_getvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1293 int ibuflen, *rbuflen;
1298 u_int16_t vid, bitmap;
1301 memcpy(&vid, ibuf, sizeof( vid ));
1302 ibuf += sizeof( vid );
1303 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1304 bitmap = ntohs( bitmap );
1306 if (( vol = getvolbyvid( vid )) == NULL ) {
1308 return( AFPERR_PARAM );
1311 if ( stat( vol->v_path, &st ) < 0 ) {
1313 return( AFPERR_PARAM );
1316 buflen = *rbuflen - sizeof( bitmap );
1317 if (( ret = getvolparams( bitmap, vol, &st,
1318 rbuf + sizeof( bitmap ), &buflen )) != AFP_OK ) {
1322 *rbuflen = buflen + sizeof( bitmap );
1323 bitmap = htons( bitmap );
1324 memcpy(rbuf, &bitmap, sizeof( bitmap ));
1328 int afp_setvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1331 int ibuflen, *rbuflen;
1335 u_int16_t vid, bitmap;
1341 memcpy(&vid, ibuf, sizeof( vid ));
1342 ibuf += sizeof( vid );
1343 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1344 bitmap = ntohs( bitmap );
1345 ibuf += sizeof(bitmap);
1347 if (( vol = getvolbyvid( vid )) == NULL ) {
1348 return( AFPERR_PARAM );
1351 if (vol->v_flags & AFPVOL_RO)
1352 return AFPERR_VLOCK;
1354 /* we can only set the backup date. */
1355 if (bitmap != VOLPBIT_BDATE)
1356 return AFPERR_BITMAP;
1358 memset(&ad, 0, sizeof(ad));
1359 if ( ad_open( vol->v_path, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR,
1362 return AFPERR_VLOCK;
1364 return AFPERR_ACCESS;
1367 memcpy(&aint, ibuf, sizeof(aint));
1368 ad_setdate(&ad, AD_DATE_BACKUP, aint);
1369 ad_flush(&ad, ADFLAGS_HF);
1370 ad_close(&ad, ADFLAGS_HF);