2 * Copyright (c) 1990,1993 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
11 #include <sys/syslog.h>
12 #include <sys/types.h>
14 #include <sys/param.h>
15 #include <netatalk/endian.h>
16 #include <atalk/asp.h>
17 #include <atalk/dsi.h>
18 #include <atalk/adouble.h>
19 #include <atalk/afp.h>
20 #include <atalk/util.h>
21 #include <atalk/cnid.h>
33 #include "directory.h"
40 #define MIN(a, b) ((a) < (b) ? (a) : (b))
43 #ifndef NO_LARGE_VOL_SUPPORT
44 #if BYTE_ORDER == BIG_ENDIAN
48 #define hton64(x) ((u_int64_t) (htonl(((x) >> 32) & 0xffffffffLL)) | \
49 (u_int64_t) ((htonl(x) & 0xffffffffLL) << 32))
50 #define ntoh64(x) (hton64(x))
54 static struct vol *volumes = NULL;
55 static int lastvid = 0;
56 #if AD_VERSION == AD_VERSION1
57 static char *Trash = "\02\024Network Trash Folder";
59 static struct extmap *extmap = NULL, *defextmap = NULL;
61 #define VOLOPT_ALLOW 0 /* user allow list */
62 #define VOLOPT_DENY 1 /* user deny list */
63 #define VOLOPT_RWLIST 2 /* user rw list */
64 #define VOLOPT_ROLIST 3 /* user ro list */
65 #define VOLOPT_CODEPAGE 4 /* codepage */
66 #define VOLOPT_PASSWORD 5 /* volume password */
67 #define VOLOPT_CASEFOLD 6 /* character case mangling */
68 #define VOLOPT_FLAGS 7 /* various flags */
69 #define VOLOPT_DBPATH 8 /* path to database */
70 #define VOLOPT_MAPCHARS 9 /* does mtou and utom mappings. syntax:
71 m and u can be double-byte hex
76 !u -> make u illegal always
77 ~u -> make u illegal only as the first
78 part of a double-byte character.
82 #define VOLOPT_FORCEUID 10 /* force uid for username x */
83 #define VOLOPT_FORCEGID 11 /* force gid for group x */
87 #define VOLOPT_NUM (VOLOPT_MAX + 1)
90 #define VOLOPT_DEFAULT ":DEFAULT:"
91 #define VOLOPT_DEFAULT_LEN 9
97 static __inline__ void volfree(struct vol_option *options,
98 const struct vol_option *save)
103 for (i = 0; i < VOLOPT_MAX; i++) {
104 if (options[i].c_value && (options[i].c_value != save[i].c_value))
105 free(options[i].c_value);
108 for (i = 0; i < VOLOPT_MAX; i++) {
109 if (options[i].c_value)
110 free(options[i].c_value);
116 /* handle variable substitutions. here's what we understand:
117 * $c -> client ip/appletalk address
118 * $f -> full name (whatever's in the gecos field)
121 * $s -> server name (hostname if it doesn't exist)
122 * $u -> username (guest is usually nobody)
123 * $v -> volume name (ADEID_NAME or basename)
124 * $z -> zone (may not exist)
127 #define is_var(a, b) (strncmp((a), (b), 2) == 0)
128 static void volxlate(AFPObj *obj, char *dest, int destlen,
129 char *src, struct passwd *pwd, char *path)
134 strncpy(dest, src, destlen);
135 if ((p = strchr(src, '$')) == NULL) /* nothing to do */
138 /* first part of the path. just forward to the next variable. */
139 len = MIN(p - src, destlen);
145 while (p && destlen > 0) {
146 /* now figure out what the variable is */
148 if (is_var(p, "$c")) {
149 if (obj->proto == AFPPROTO_ASP) {
150 ASP asp = obj->handle;
152 len = sprintf(dest, "%u.%u", ntohs(asp->asp_sat.sat_addr.s_net),
153 asp->asp_sat.sat_addr.s_node);
157 } else if (obj->proto == AFPPROTO_DSI) {
158 DSI *dsi = obj->handle;
160 len = sprintf(dest, "%s:%u", inet_ntoa(dsi->client.sin_addr),
161 ntohs(dsi->client.sin_port));
165 } else if (is_var(p, "$f")) {
166 if (q = strchr(pwd->pw_gecos, ','))
169 } else if (is_var(p, "$g")) {
170 struct group *grp = getgrgid(pwd->pw_gid);
173 } else if (is_var(p, "$h")) {
174 q = obj->options.hostname;
175 } else if (is_var(p, "$s")) {
178 else if (obj->options.server) {
179 q = obj->options.server;
181 q = obj->options.hostname;
182 } else if (is_var(p, "$u")) {
184 } else if (is_var(p, "$v")) {
188 memset(&ad, 0, sizeof(ad));
189 if (ad_open(path, ADFLAGS_HF, O_RDONLY, 0, &ad) < 0)
192 if ((len = MIN(ad_getentrylen(&ad, ADEID_NAME), destlen)) > 0) {
193 memcpy(dest, ad_entry(&ad, ADEID_NAME), len);
194 ad_close(&ad, ADFLAGS_HF);
198 ad_close(&ad, ADFLAGS_HF);
199 no_volname: /* simple basename */
200 if ((q = strrchr(path, '/')) == NULL)
202 else if (*(q + 1) != '\0')
206 } else if (is_var(p, "$z")) {
208 } else if (is_var(p, "$$")) {
213 /* copy the stuff over. if we don't understand something that we
214 * should, just skip it over. */
216 len = MIN(p == q ? 2 : strlen(q), destlen);
217 strncpy(dest, q, len);
222 /* stuff up to next $ */
224 p = strchr(src, '$');
225 len = p ? MIN(p - src, destlen) : destlen;
227 strncpy(dest, src, len);
234 /* to make sure that val is valid, make sure to select an opt that
236 #define optionok(buf,opt,val) (strstr((buf),(opt)) && ((val)[1] != '\0'))
238 static __inline__ char *get_codepage_path(const char *path, const char *name)
244 page = (char *) malloc((len = strlen(path)) + strlen(name) + 2);
247 if (path[len - 1] != '/') /* add a / */
255 /* debug: show which codepage directory we are using */
256 syslog(LOG_DEBUG, "using codepage directory: %s", page);
261 /* handle all the options. tmp can't be NULL. */
262 static void volset(struct vol_option *options, char *volname, int vlen,
263 const char *nlspath, const char *tmp)
267 val = strchr(tmp, ':');
268 if (optionok(tmp, "allow:", val)) {
269 if (options[VOLOPT_ALLOW].c_value)
270 free(options[VOLOPT_ALLOW].c_value);
271 options[VOLOPT_ALLOW].c_value = strdup(val + 1);
273 } else if (optionok(tmp, "deny:", val)) {
274 if (options[VOLOPT_DENY].c_value)
275 free(options[VOLOPT_DENY].c_value);
276 options[VOLOPT_DENY].c_value = strdup(val + 1);
278 } else if (optionok(tmp, "rwlist:", val)) {
279 if (options[VOLOPT_RWLIST].c_value)
280 free(options[VOLOPT_RWLIST].c_value);
281 options[VOLOPT_RWLIST].c_value = strdup(val + 1);
283 } else if (optionok(tmp, "rolist:", val)) {
284 if (options[VOLOPT_ROLIST].c_value)
285 free(options[VOLOPT_ROLIST].c_value);
286 options[VOLOPT_ROLIST].c_value = strdup(val + 1);
288 } else if (optionok(tmp, "codepage:", val)) {
289 if (options[VOLOPT_CODEPAGE].c_value)
290 free(options[VOLOPT_CODEPAGE].c_value);
291 options[VOLOPT_CODEPAGE].c_value = get_codepage_path(nlspath, val + 1);
293 } else if (optionok(tmp, "casefold:", val)) {
294 if (strcasecmp(val + 1, "tolower") == 0)
295 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMLOWER;
296 else if (strcasecmp(val + 1, "toupper") == 0)
297 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMUPPER;
298 else if (strcasecmp(val + 1, "xlatelower") == 0)
299 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UUPPERMLOWER;
300 else if (strcasecmp(val + 1, "xlateupper") == 0)
301 options[VOLOPT_CASEFOLD].i_value = AFPVOL_ULOWERMUPPER;
303 } else if (optionok(tmp, "options:", val)) {
306 if ((p = strtok(val + 1, ",")) == NULL) /* nothing */
310 if (strcasecmp(p, "prodos") == 0)
311 options[VOLOPT_FLAGS].i_value |= AFPVOL_A2VOL;
312 else if (strcasecmp(p, "mswindows") == 0) {
313 options[VOLOPT_FLAGS].i_value |= AFPVOL_MSWINDOWS;
314 if (!options[VOLOPT_CODEPAGE].c_value)
315 options[VOLOPT_CODEPAGE].c_value =
316 get_codepage_path(nlspath, MSWINDOWS_CODEPAGE);
318 } else if (strcasecmp(p, "crlf") == 0)
319 options[VOLOPT_FLAGS].i_value |= AFPVOL_CRLF;
320 else if (strcasecmp(p, "noadouble") == 0)
321 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOADOUBLE;
322 else if (strcasecmp(p, "ro") == 0)
323 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
324 else if (strcasecmp(p, "nohex") == 0)
325 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOHEX;
326 else if (strcasecmp(p, "usedots") == 0)
327 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS;
328 else if (strcasecmp(p, "limitsize") == 0)
329 options[VOLOPT_FLAGS].i_value |= AFPVOL_LIMITSIZE;
331 p = strtok(NULL, ",");
334 #if AD_VERSION > AD_VERSION1
335 } else if (optionok(tmp, "dbpath:", val)) {
336 if (options[VOLOPT_DBPATH].c_value)
337 free(options[VOLOPT_DBPATH].c_value);
339 options[VOLOPT_DBPATH].c_value = strdup(val + 1);
341 } else if (optionok(tmp, "mapchars:",val)) {
342 if (options[VOLOPT_MAPCHARS].c_value)
343 free(options[VOLOPT_MAPCHARS].c_value);
344 options[VOLOPT_MAPCHARS].c_value = strdup(val + 1);
346 } else if (optionok(tmp, "password:", val)) {
347 if (options[VOLOPT_PASSWORD].c_value)
348 free(options[VOLOPT_PASSWORD].c_value);
349 options[VOLOPT_PASSWORD].c_value = strdup(val + 1);
353 /* this code allows forced uid/gid per volume settings */
354 } else if (optionok(tmp, "forceuid:", val)) {
355 if (options[VOLOPT_FORCEUID].c_value)
356 free(options[VOLOPT_FORCEUID].c_value);
357 options[VOLOPT_FORCEUID].c_value = strdup(val + 1);
358 } else if (optionok(tmp, "forcegid:", val)) {
359 if (options[VOLOPT_FORCEGID].c_value)
360 free(options[VOLOPT_FORCEGID].c_value);
361 options[VOLOPT_FORCEGID].c_value = strdup(val + 1);
366 /* ignore unknown options */
367 syslog(LOG_DEBUG, "ignoring unknown volume option: %s", tmp);
370 /* we'll assume it's a volume name. */
371 strncpy(volname, tmp, vlen);
375 static int creatvol(const char *path, char *name, struct vol_option *options)
380 if ( name == NULL || *name == '\0' ) {
381 if ((name = strrchr( path, '/' )) == NULL) {
382 return -1; /* Obviously not a fully qualified path */
385 /* if you wish to share /, you need to specify a name. */
390 for ( volume = volumes; volume; volume = volume->v_next ) {
391 if ( strcasecmp( volume->v_name, name ) == 0 ) {
392 return -1; /* Won't be able to access it, anyway... */
396 vlen = strlen( name );
397 if ( vlen > AFPVOL_NAMELEN ) {
398 vlen = AFPVOL_NAMELEN;
399 name[AFPVOL_NAMELEN] = '\0';
403 (struct vol *)calloc(1, sizeof( struct vol ))) == NULL ) {
404 syslog( LOG_ERR, "creatvol: malloc: %m" );
407 if (( volume->v_name =
408 (char *)malloc( vlen + 1 )) == NULL ) {
409 syslog( LOG_ERR, "creatvol: malloc: %m" );
413 if (( volume->v_path =
414 (char *)malloc( strlen( path ) + 1 )) == NULL ) {
415 syslog( LOG_ERR, "creatvol: malloc: %m" );
416 free(volume->v_name);
421 strcpy( volume->v_name, name);
422 strcpy( volume->v_path, path );
427 volume->v_vid = lastvid++;
428 volume->v_lastdid = 3;
432 /* should we casefold? */
433 volume->v_casefold = options[VOLOPT_CASEFOLD].i_value;
435 /* shift in some flags */
436 volume->v_flags = options[VOLOPT_FLAGS].i_value;
438 /* read in the code pages */
439 if (options[VOLOPT_CODEPAGE].c_value)
440 codepage_read(volume, options[VOLOPT_CODEPAGE].c_value);
442 if (options[VOLOPT_PASSWORD].c_value)
443 volume->v_password = strdup(options[VOLOPT_PASSWORD].c_value);
445 #if AD_VERSION > AD_VERSION1
446 if (options[VOLOPT_DBPATH].c_value)
447 volume->v_dbpath = strdup(options[VOLOPT_DBPATH].c_value);
452 if (options[VOLOPT_FORCEUID].c_value) {
453 volume->v_forceuid = strdup(options[VOLOPT_FORCEUID].c_value);
455 volume->v_forceuid = NULL; /* set as null so as to return 0 later on */
458 if (options[VOLOPT_FORCEGID].c_value) {
459 volume->v_forcegid = strdup(options[VOLOPT_FORCEGID].c_value);
461 volume->v_forcegid = NULL; /* set as null so as to return 0 later on */
468 volume->v_next = volumes;
473 static char *myfgets( buf, size, fp )
482 while ((( c = getc( fp )) != EOF ) && ( size > 0 )) {
483 if ( c == '\n' || c == '\r' ) {
501 /* check access list. this function wants something of the following
503 * @group,name,name2,@group2,name3
505 * a NULL argument allows everybody to have access.
506 * we return three things:
508 * 0: list exists, but name isn't in it
511 static int accessvol(args, name)
515 char buf[MAXPATHLEN + 1], *p;
521 strncpy(buf, args, sizeof(buf));
522 if ((p = strtok(buf, ",")) == NULL) /* nothing, return okay */
526 if (*p == '@') { /* it's a group */
527 if ((gr = getgrnam(p + 1)) && gmem(gr->gr_gid))
529 } else if (strcmp(p, name) == 0) /* it's a user name */
531 p = strtok(NULL, ",");
537 static void setextmap( ext, type, creator, user)
538 char *ext, *type, *creator;
543 for ( em = extmap; em; em = em->em_next ) {
544 if ( strdiacasecmp( em->em_ext, ext ) == 0 ) {
551 (struct extmap *)malloc( sizeof( struct extmap ))) == NULL ) {
552 syslog( LOG_ERR, "setextmap: malloc: %m" );
555 em->em_next = extmap;
557 } else if ( !user ) {
561 strcpy( em->em_ext, ext );
563 if ( *type == '\0' ) {
564 memcpy(em->em_type, "????", sizeof( em->em_type ));
566 memcpy(em->em_type, type, sizeof( em->em_type ));
568 if ( *creator == '\0' ) {
569 memcpy(em->em_creator, "UNIX", sizeof( em->em_creator ));
571 memcpy(em->em_creator, creator, sizeof( em->em_creator ));
574 if ( strcmp( ext, "." ) == 0 ) {
580 * Read a volume configuration file and add the volumes contained within to
581 * the global volume list. If p2 is non-NULL, the file that is opened is
584 * Lines that begin with # and blank lines are ignored.
585 * Volume lines are of the form:
586 * <unix path> [<volume name>] [allow:<user>,<@group>,...] \
587 * [codepage:<file>] [casefold:<num>]
588 * <extension> TYPE [CREATOR]
590 static int readvolfile(obj, p1, p2, user, pwent)
594 struct passwd *pwent;
597 char path[ MAXPATHLEN + 1], tmp[ MAXPATHLEN + 1],
598 volname[ AFPVOL_NAMELEN + 1 ], buf[ BUFSIZ ],
599 type[ 5 ], creator[ 5 ];
602 struct vol_option options[VOLOPT_NUM], save_options[VOLOPT_NUM];
614 if (( fp = fopen( path, "r" )) == NULL ) {
618 memset(save_options, 0, sizeof(save_options));
619 while ( myfgets( buf, sizeof( buf ), fp ) != NULL ) {
620 initline( strlen( buf ), buf );
621 parseline( sizeof( path ) - 1, path );
628 /* change the default options for this file */
629 if (strncmp(path, VOLOPT_DEFAULT, VOLOPT_DEFAULT_LEN) == 0) {
631 for (i = 0; i < VOLOPT_NUM; i++) {
632 if (parseline( sizeof( path ) - VOLOPT_DEFAULT_LEN - 1,
633 path + VOLOPT_DEFAULT_LEN) < 0)
635 volset(save_options, tmp, sizeof(tmp) - 1,
636 obj->options.nlspath, path + VOLOPT_DEFAULT_LEN);
642 if (( p = strchr( path, '/' )) != NULL ) {
650 if ( u == NULL || *u == '\0' || ( pw = getpwnam( u )) == NULL ) {
653 strcpy( tmp, pw->pw_dir );
654 if ( p != NULL && *p != '\0' ) {
661 /* send path through variable substitution */
662 if (*path != '~') /* need to copy path to tmp */
665 pwent = getpwnam(obj->username);
666 volxlate(obj, path, sizeof(path) - 1, tmp, pwent, NULL);
668 /* this is sort of braindead. basically, i want to be
669 * able to specify things in any order, but i don't want to
670 * re-write everything.
672 * currently we have 11 options:
680 * options:prodos,crlf,noadouble,ro
683 * namemask:x,y,!z (not implemented yet)
685 memcpy(options, save_options, sizeof(options));
688 /* read in up to 11 possible options */
689 for (i = 0; i < VOLOPT_NUM; i++) {
690 if (parseline( sizeof( tmp ) - 1, tmp ) < 0)
693 volset(options, volname, sizeof(volname) - 1,
694 obj->options.nlspath, tmp);
697 /* check allow/deny lists:
698 allow -> either no list (-1), or in list (1)
699 deny -> either no list (-1), or not in list (0) */
700 if (accessvol(options[VOLOPT_ALLOW].c_value, obj->username) &&
701 (accessvol(options[VOLOPT_DENY].c_value, obj->username) < 1)) {
703 /* handle read-only behaviour. semantics:
704 * 1) neither the rolist nor the rwlist exist -> rw
705 * 2) rolist exists -> ro if user is in it.
706 * 3) rwlist exists -> ro unless user is in it. */
707 if (((options[VOLOPT_FLAGS].i_value & AFPVOL_RO) == 0) &&
708 ((accessvol(options[VOLOPT_ROLIST].c_value,
709 obj->username) == 1) ||
710 !accessvol(options[VOLOPT_RWLIST].c_value,
712 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
714 /* do variable substitution */
715 volxlate(obj, tmp, sizeof(tmp) - 1, volname, pwent, path);
716 creatvol(path, tmp, options);
718 volfree(options, save_options);
722 parseline( sizeof( type ) - 1, type );
723 parseline( sizeof( creator ) - 1, creator );
724 setextmap( path, type, creator, user);
731 volfree(save_options, NULL);
732 if ( fclose( fp ) != 0 ) {
733 syslog( LOG_ERR, "readvolfile: fclose: %m" );
739 static void load_volumes(AFPObj *obj)
741 struct passwd *pwent = getpwnam(obj->username);
743 if ( (obj->options.flags & OPTION_USERVOLFIRST) == 0 ) {
744 readvolfile(obj, obj->options.systemvol, 0, pwent);
747 if ((*obj->username == '\0') || (obj->options.flags & OPTION_NOUSERVOL)) {
748 readvolfile(obj, obj->options.defaultvol, NULL, 1, pwent);
751 * Read user's AppleVolumes or .AppleVolumes file
752 * If neither are readable, read the default volumes file. if
753 * that doesn't work, create a user share.
755 if ( readvolfile(obj, pwent->pw_dir, "AppleVolumes", 1, pwent) < 0 &&
756 readvolfile(obj, pwent->pw_dir, ".AppleVolumes", 1, pwent) < 0 &&
757 readvolfile(obj, pwent->pw_dir, "applevolumes", 1, pwent) < 0 &&
758 readvolfile(obj, pwent->pw_dir, ".applevolumes", 1, pwent) < 0 &&
759 obj->options.defaultvol != NULL ) {
760 if (readvolfile(obj, obj->options.defaultvol, NULL, 1, pwent) < 0)
761 creatvol(pwent->pw_dir, NULL, NULL);
764 if ( obj->options.flags & OPTION_USERVOLFIRST ) {
765 readvolfile(obj, obj->options.systemvol, NULL, 0, pwent );
769 static int getvolspace( vol, bfree, btotal, xbfree, xbtotal, bsize )
771 u_int32_t *bfree, *btotal, *bsize;
772 VolSpace *xbfree, *xbtotal;
776 #ifndef NO_QUOTA_SUPPORT
777 VolSpace qfree, qtotal;
780 spaceflag = AFPVOL_GVSMASK & vol->v_flags;
781 /* report up to 2GB if afp version is < 2.2 (4GB if not) */
782 maxsize = (vol->v_flags & AFPVOL_A2VOL) ? 0x01fffe00 :
783 (((afp_version < 22) || (vol->v_flags & AFPVOL_LIMITSIZE))
784 ? 0x7fffffffL : 0xffffffffL);
787 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_AFSGVS ) {
788 if ( afs_getvolspace( vol, xbfree, xbtotal, bsize ) == AFP_OK ) {
789 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_AFSGVS;
790 goto getvolspace_done;
795 if (( rc = ustatfs_getvolspace( vol, xbfree, xbtotal,
796 bsize)) != AFP_OK ) {
800 #define min(a,b) ((a)<(b)?(a):(b))
801 #ifndef NO_QUOTA_SUPPORT
802 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_UQUOTA ) {
803 if ( uquota_getvolspace( vol, &qfree, &qtotal, *bsize ) == AFP_OK ) {
804 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_UQUOTA;
805 *xbfree = min(*xbfree, qfree);
806 *xbtotal = min( *xbtotal, qtotal);
807 goto getvolspace_done;
811 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_USTATFS;
814 *bfree = min( *xbfree, maxsize);
815 *btotal = min( *xbtotal, maxsize);
819 static int getvolparams( bitmap, vol, st, buf, buflen )
827 int bit = 0, aint, isad = 1;
829 u_int32_t bfree, btotal, bsize;
830 VolSpace xbfree, xbtotal; /* extended bytes */
831 char *data, *nameoff = NULL;
834 /* courtesy of jallison@whistle.com:
835 * For MacOS8.x support we need to create the
836 * .Parent file here if it doesn't exist. */
838 memset(&ad, 0, sizeof(ad));
839 if ( ad_open( vol->v_path, vol_noadouble(vol) |
840 ADFLAGS_HF|ADFLAGS_DIR, O_RDWR | O_CREAT,
844 } else if (ad_getoflags( &ad, ADFLAGS_HF ) & O_CREAT) {
845 slash = strrchr( vol->v_path, '/' );
851 ad_setentrylen( &ad, ADEID_NAME, strlen( slash ));
852 memcpy(ad_entry( &ad, ADEID_NAME ), slash,
853 ad_getentrylen( &ad, ADEID_NAME ));
854 ad_flush(&ad, ADFLAGS_HF);
857 if (( bitmap & ( (1<<VOLPBIT_BFREE)|(1<<VOLPBIT_BTOTAL) |
858 (1<<VOLPBIT_XBFREE)|(1<<VOLPBIT_XBTOTAL) |
859 (1<<VOLPBIT_BSIZE)) ) != 0 ) {
860 if ( getvolspace( vol, &bfree, &btotal, &xbfree, &xbtotal,
863 ad_close( &ad, ADFLAGS_HF );
865 return( AFPERR_PARAM );
870 while ( bitmap != 0 ) {
871 while (( bitmap & 1 ) == 0 ) {
878 #if AD_VERSION > AD_VERSION1
879 ashort = VOLPBIT_ATTR_FILEID;
883 /* check for read-only.
884 * NOTE: we don't actually set the read-only flag unless
885 * it's passed in that way as it's possible to mount
886 * a read-write filesystem under a read-only one. */
887 if ((vol->v_flags & AFPVOL_RO) ||
888 ((utime(vol->v_path, NULL) < 0) && (errno == EROFS)))
889 ashort |= VOLPBIT_ATTR_RO;
890 ashort = htons(ashort);
891 memcpy(data, &ashort, sizeof( ashort ));
892 data += sizeof( ashort );
896 ashort = htons( AFPVOLSIG_DEFAULT );
897 memcpy(data, &ashort, sizeof( ashort ));
898 data += sizeof( ashort );
902 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
903 aint = AD_DATE_FROM_UNIX(st->st_mtime);
904 memcpy(data, &aint, sizeof( aint ));
905 data += sizeof( aint );
909 if ( st->st_mtime > vol->v_time ) {
910 vol->v_time = st->st_mtime;
911 aint = AD_DATE_FROM_UNIX(st->st_mtime);
913 aint = AD_DATE_FROM_UNIX(vol->v_time);
915 memcpy(data, &aint, sizeof( aint ));
916 data += sizeof( aint );
920 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
921 aint = AD_DATE_START;
922 memcpy(data, &aint, sizeof( aint ));
923 data += sizeof( aint );
927 memcpy(data, &vol->v_vid, sizeof( vol->v_vid ));
928 data += sizeof( vol->v_vid );
932 bfree = htonl( bfree );
933 memcpy(data, &bfree, sizeof( bfree ));
934 data += sizeof( bfree );
937 case VOLPBIT_BTOTAL :
938 btotal = htonl( btotal );
939 memcpy(data, &btotal, sizeof( btotal ));
940 data += sizeof( btotal );
943 #ifndef NO_LARGE_VOL_SUPPORT
944 case VOLPBIT_XBFREE :
945 xbfree = hton64( xbfree );
946 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
947 bcopy(&xbfree, data, sizeof(xbfree));
949 memcpy(data, &xbfree, sizeof( xbfree ));
951 data += sizeof( xbfree );
954 case VOLPBIT_XBTOTAL :
955 xbtotal = hton64( xbtotal );
956 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
957 bcopy(&xbtotal, data, sizeof(xbtotal));
959 memcpy(data, &xbtotal, sizeof( xbtotal ));
961 data += sizeof( xbfree );
967 data += sizeof( u_int16_t );
970 case VOLPBIT_BSIZE: /* block size */
971 bsize = htonl(bsize);
972 memcpy(data, &bsize, sizeof(bsize));
973 data += sizeof(bsize);
978 ad_close( &ad, ADFLAGS_HF );
980 return( AFPERR_BITMAP );
986 ashort = htons( data - buf );
987 memcpy(nameoff, &ashort, sizeof( ashort ));
988 aint = strlen( vol->v_name );
990 memcpy(data, vol->v_name, aint );
994 ad_close( &ad, ADFLAGS_HF );
996 *buflen = data - buf;
1002 int afp_getsrvrparms(obj, ibuf, ibuflen, rbuf, rbuflen )
1005 int ibuflen, *rbuflen;
1018 for ( vcnt = 0, volume = volumes; volume; volume = volume->v_next ) {
1019 if ( stat( volume->v_path, &st ) < 0 ) {
1020 syslog( LOG_INFO, "afp_getsrvrparms: stat %s: %m",
1022 continue; /* can't access directory */
1024 if (!S_ISDIR(st.st_mode)) {
1025 continue; /* not a dir */
1028 /* set password bit if there's a volume password */
1029 *data = (volume->v_password) ? AFPSRVR_PASSWD : 0;
1031 /* Apple 2 clients running ProDOS-8 expect one volume to have
1032 bit 0 of this byte set. They will not recognize anything
1033 on the server unless this is the case. I have not
1034 completely worked this out, but it's related to booting
1035 from the server. Support for that function is a ways
1036 off.. <shirsch@ibm.net> */
1037 *data++ |= (volume->v_flags & AFPVOL_A2VOL) ? AFPSRVR_CONFIGINFO : 0;
1038 len = strlen( volume->v_name );
1040 memcpy(data, volume->v_name, len );
1045 *rbuflen = data - rbuf;
1047 if ( gettimeofday( &tv, 0 ) < 0 ) {
1048 syslog( LOG_ERR, "afp_getsrvrparms: gettimeofday: %m" );
1050 return AFPERR_PARAM;
1052 tv.tv_sec = AD_DATE_FROM_UNIX(tv.tv_sec);
1053 memcpy(data, &tv.tv_sec, sizeof( u_int32_t));
1054 data += sizeof( u_int32_t);
1059 int afp_openvol(obj, ibuf, ibuflen, rbuf, rbuflen )
1062 int ibuflen, *rbuflen;
1066 #if AD_VERSION == AD_VERSION1
1071 int len, ret, buflen;
1075 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1076 bitmap = ntohs( bitmap );
1077 ibuf += sizeof( bitmap );
1078 if (( bitmap & (1<<VOLPBIT_VID)) == 0 ) {
1079 ret = AFPERR_BITMAP;
1083 len = (unsigned char)*ibuf++;
1084 volname = obj->oldtmp;
1085 memcpy(volname, ibuf, len );
1086 *(volname + len) = '\0';
1088 if ((len + 1) & 1) /* pad to an even boundary */
1094 for ( volume = volumes; volume; volume = volume->v_next ) {
1095 if ( strcasecmp( volname, volume->v_name ) == 0 ) {
1100 if ( volume == NULL ) {
1105 /* check for a volume password */
1106 if (volume->v_password &&
1107 strncmp(ibuf, volume->v_password, VOLPASSLEN)) {
1108 ret = AFPERR_ACCESS;
1112 if (( volume->v_flags & AFPVOL_OPEN ) == 0 ) {
1113 if ((dir = dirnew(strlen(volume->v_name) + 1)) == NULL) {
1114 syslog( LOG_ERR, "afp_openvol: malloc: %m" );
1118 dir->d_did = DIRDID_ROOT;
1119 dir->d_color = DIRTREE_COLOR_BLACK; /* root node is black */
1120 strcpy( dir->d_name, volume->v_name );
1121 volume->v_dir = volume->v_root = dir;
1122 volume->v_flags |= AFPVOL_OPEN;
1125 if ( stat( volume->v_path, &st ) < 0 ) {
1130 buflen = *rbuflen - sizeof( bitmap );
1131 if (( ret = getvolparams( bitmap, volume, &st,
1132 rbuf + sizeof(bitmap), &buflen )) != AFP_OK ) {
1135 *rbuflen = buflen + sizeof( bitmap );
1136 bitmap = htons( bitmap );
1137 memcpy(rbuf, &bitmap, sizeof( bitmap ));
1139 curdir = volume->v_dir;
1140 if ( chdir( volume->v_path ) < 0 ) {
1144 #if AD_VERSION == AD_VERSION1
1146 * If you mount a volume twice, the second time the trash appears on
1147 * the desk-top. That's because the Mac remembers the DID for the
1148 * trash (even for volumes in different zones, on different servers).
1149 * Just so this works better, we prime the DID cache with the trash,
1150 * fixing the trash at DID 3.
1153 cname( volume, volume->v_dir, &p );
1163 int afp_closevol(obj, ibuf, ibuflen, rbuf, rbuflen )
1166 int ibuflen, *rbuflen;
1168 struct vol *vol, *ovol;
1173 memcpy(&vid, ibuf, sizeof( vid ));
1174 if (( vol = getvolbyvid( vid )) == NULL ) {
1175 return( AFPERR_PARAM );
1178 vol->v_flags &= ~AFPVOL_OPEN;
1179 for ( ovol = volumes; ovol; ovol = ovol->v_next ) {
1180 if ( ovol->v_flags & AFPVOL_OPEN ) {
1184 if ( ovol != NULL ) {
1185 curdir = ovol->v_dir;
1186 if ( chdir( ovol->v_path ) < 0 ) {
1187 return( AFPERR_PARAM );
1191 dirfree( vol->v_root );
1193 #if AD_VERSION > AD_VERSION1
1194 cnid_close(vol->v_db);
1200 struct vol *getvolbyvid(const u_int16_t vid )
1204 for ( vol = volumes; vol; vol = vol->v_next ) {
1205 if ( vid == vol->v_vid ) {
1209 if ( vol == NULL || ( vol->v_flags & AFPVOL_OPEN ) == 0 ) {
1216 struct extmap *getextmap(const char *path)
1221 if (( p = strrchr( path, '.' )) == NULL ) {
1222 return( defextmap );
1225 for ( em = extmap; em; em = em->em_next ) {
1226 if ( strdiacasecmp( em->em_ext, p ) == 0 ) {
1231 return( defextmap );
1237 void setvoltime(obj, vol )
1243 /* just looking at vol->v_time is broken seriously since updates
1244 * from other users afpd processes never are seen.
1245 * This is not the most elegant solution (a shared memory between
1246 * the afpd processes would come closer)
1249 if ( gettimeofday( &tv, 0 ) < 0 ) {
1250 syslog( LOG_ERR, "setvoltime: gettimeofday: %m" );
1253 if( utime( vol->v_path, NULL ) < 0 ) {
1254 /* write of time failed ... probably a read only filesys,
1255 * where no other users can interfere, so there's no issue here
1259 /* a little granularity */
1260 if (vol->v_time < tv.tv_sec) {
1261 vol->v_time = tv.tv_sec;
1262 obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED);
1266 int afp_getvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1269 int ibuflen, *rbuflen;
1274 u_int16_t vid, bitmap;
1277 memcpy(&vid, ibuf, sizeof( vid ));
1278 ibuf += sizeof( vid );
1279 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1280 bitmap = ntohs( bitmap );
1282 if (( vol = getvolbyvid( vid )) == NULL ) {
1284 return( AFPERR_PARAM );
1287 if ( stat( vol->v_path, &st ) < 0 ) {
1289 return( AFPERR_PARAM );
1292 buflen = *rbuflen - sizeof( bitmap );
1293 if (( ret = getvolparams( bitmap, vol, &st,
1294 rbuf + sizeof( bitmap ), &buflen )) != AFP_OK ) {
1298 *rbuflen = buflen + sizeof( bitmap );
1299 bitmap = htons( bitmap );
1300 memcpy(rbuf, &bitmap, sizeof( bitmap ));
1304 int afp_setvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1307 int ibuflen, *rbuflen;
1311 u_int16_t vid, bitmap;
1317 memcpy(&vid, ibuf, sizeof( vid ));
1318 ibuf += sizeof( vid );
1319 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1320 bitmap = ntohs( bitmap );
1321 ibuf += sizeof(bitmap);
1323 if (( vol = getvolbyvid( vid )) == NULL ) {
1324 return( AFPERR_PARAM );
1327 if (vol->v_flags & AFPVOL_RO)
1328 return AFPERR_VLOCK;
1330 /* we can only set the backup date. */
1331 if (bitmap != VOLPBIT_BDATE)
1332 return AFPERR_BITMAP;
1334 memset(&ad, 0, sizeof(ad));
1335 if ( ad_open( vol->v_path, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR,
1338 return AFPERR_VLOCK;
1340 return AFPERR_ACCESS;
1343 memcpy(&aint, ibuf, sizeof(aint));
1344 ad_setdate(&ad, AD_DATE_BACKUP, aint);
1345 ad_flush(&ad, ADFLAGS_HF);
1346 ad_close(&ad, ADFLAGS_HF);