2 * $Id: volume.c,v 1.10 2001-08-15 01:37:34 srittau 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: %s", strerror(errno) );
423 if (( volume->v_name =
424 (char *)malloc( vlen + 1 )) == NULL ) {
425 syslog( LOG_ERR, "creatvol: malloc: %s", strerror(errno) );
429 if (( volume->v_path =
430 (char *)malloc( strlen( path ) + 1 )) == NULL ) {
431 syslog( LOG_ERR, "creatvol: malloc: %s", strerror(errno) );
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: %s", strerror(errno) );
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: %s", strerror(errno) );
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, isad = 1;
846 u_int32_t bfree, btotal, bsize;
847 VolSpace xbfree, xbtotal; /* extended bytes */
848 char *data, *nameoff = NULL;
851 /* courtesy of jallison@whistle.com:
852 * For MacOS8.x support we need to create the
853 * .Parent file here if it doesn't exist. */
855 memset(&ad, 0, sizeof(ad));
856 if ( ad_open( vol->v_path, vol_noadouble(vol) |
857 ADFLAGS_HF|ADFLAGS_DIR, O_RDWR | O_CREAT,
861 } else if (ad_getoflags( &ad, ADFLAGS_HF ) & O_CREAT) {
862 slash = strrchr( vol->v_path, '/' );
868 ad_setentrylen( &ad, ADEID_NAME, strlen( slash ));
869 memcpy(ad_entry( &ad, ADEID_NAME ), slash,
870 ad_getentrylen( &ad, ADEID_NAME ));
871 ad_flush(&ad, ADFLAGS_HF);
874 if (( bitmap & ( (1<<VOLPBIT_BFREE)|(1<<VOLPBIT_BTOTAL) |
875 (1<<VOLPBIT_XBFREE)|(1<<VOLPBIT_XBTOTAL) |
876 (1<<VOLPBIT_BSIZE)) ) != 0 ) {
877 if ( getvolspace( vol, &bfree, &btotal, &xbfree, &xbtotal,
880 ad_close( &ad, ADFLAGS_HF );
882 return( AFPERR_PARAM );
887 while ( bitmap != 0 ) {
888 while (( bitmap & 1 ) == 0 ) {
896 ashort = VOLPBIT_ATTR_FILEID;
900 /* check for read-only.
901 * NOTE: we don't actually set the read-only flag unless
902 * it's passed in that way as it's possible to mount
903 * a read-write filesystem under a read-only one. */
904 if ((vol->v_flags & AFPVOL_RO) ||
905 ((utime(vol->v_path, NULL) < 0) && (errno == EROFS)))
906 ashort |= VOLPBIT_ATTR_RO;
907 ashort = htons(ashort);
908 memcpy(data, &ashort, sizeof( ashort ));
909 data += sizeof( ashort );
913 ashort = htons( AFPVOLSIG_DEFAULT );
914 memcpy(data, &ashort, sizeof( ashort ));
915 data += sizeof( ashort );
919 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
920 aint = AD_DATE_FROM_UNIX(st->st_mtime);
921 memcpy(data, &aint, sizeof( aint ));
922 data += sizeof( aint );
926 if ( st->st_mtime > vol->v_time ) {
927 vol->v_time = st->st_mtime;
928 aint = AD_DATE_FROM_UNIX(st->st_mtime);
930 aint = AD_DATE_FROM_UNIX(vol->v_time);
932 memcpy(data, &aint, sizeof( aint ));
933 data += sizeof( aint );
937 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
938 aint = AD_DATE_START;
939 memcpy(data, &aint, sizeof( aint ));
940 data += sizeof( aint );
944 memcpy(data, &vol->v_vid, sizeof( vol->v_vid ));
945 data += sizeof( vol->v_vid );
949 bfree = htonl( bfree );
950 memcpy(data, &bfree, sizeof( bfree ));
951 data += sizeof( bfree );
954 case VOLPBIT_BTOTAL :
955 btotal = htonl( btotal );
956 memcpy(data, &btotal, sizeof( btotal ));
957 data += sizeof( btotal );
960 #ifndef NO_LARGE_VOL_SUPPORT
961 case VOLPBIT_XBFREE :
962 xbfree = hton64( xbfree );
963 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
964 bcopy(&xbfree, data, sizeof(xbfree));
965 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
966 memcpy(data, &xbfree, sizeof( xbfree ));
967 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
968 data += sizeof( xbfree );
971 case VOLPBIT_XBTOTAL :
972 xbtotal = hton64( xbtotal );
973 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
974 bcopy(&xbtotal, data, sizeof(xbtotal));
975 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
976 memcpy(data, &xbtotal, sizeof( xbtotal ));
977 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
978 data += sizeof( xbfree );
980 #endif /* ! NO_LARGE_VOL_SUPPORT */
984 data += sizeof( u_int16_t );
987 case VOLPBIT_BSIZE: /* block size */
988 bsize = htonl(bsize);
989 memcpy(data, &bsize, sizeof(bsize));
990 data += sizeof(bsize);
995 ad_close( &ad, ADFLAGS_HF );
997 return( AFPERR_BITMAP );
1003 ashort = htons( data - buf );
1004 memcpy(nameoff, &ashort, sizeof( ashort ));
1005 aint = strlen( vol->v_name );
1007 memcpy(data, vol->v_name, aint );
1011 ad_close( &ad, ADFLAGS_HF );
1013 *buflen = data - buf;
1019 int afp_getsrvrparms(obj, ibuf, ibuflen, rbuf, rbuflen )
1022 int ibuflen, *rbuflen;
1035 for ( vcnt = 0, volume = volumes; volume; volume = volume->v_next ) {
1036 if ( stat( volume->v_path, &st ) < 0 ) {
1037 syslog( LOG_INFO, "afp_getsrvrparms: stat %s: %s",
1038 volume->v_path, strerror(errno) );
1039 continue; /* can't access directory */
1041 if (!S_ISDIR(st.st_mode)) {
1042 continue; /* not a dir */
1045 /* set password bit if there's a volume password */
1046 *data = (volume->v_password) ? AFPSRVR_PASSWD : 0;
1048 /* Apple 2 clients running ProDOS-8 expect one volume to have
1049 bit 0 of this byte set. They will not recognize anything
1050 on the server unless this is the case. I have not
1051 completely worked this out, but it's related to booting
1052 from the server. Support for that function is a ways
1053 off.. <shirsch@ibm.net> */
1054 *data++ |= (volume->v_flags & AFPVOL_A2VOL) ? AFPSRVR_CONFIGINFO : 0;
1055 len = strlen( volume->v_name );
1057 memcpy(data, volume->v_name, len );
1062 *rbuflen = data - rbuf;
1064 if ( gettimeofday( &tv, 0 ) < 0 ) {
1065 syslog( LOG_ERR, "afp_getsrvrparms: gettimeofday: %s", strerror(errno) );
1067 return AFPERR_PARAM;
1069 tv.tv_sec = AD_DATE_FROM_UNIX(tv.tv_sec);
1070 memcpy(data, &tv.tv_sec, sizeof( u_int32_t));
1071 data += sizeof( u_int32_t);
1076 int afp_openvol(obj, ibuf, ibuflen, rbuf, rbuflen )
1079 int ibuflen, *rbuflen;
1085 #endif /* CNID_DB */
1088 int len, ret, buflen;
1092 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1093 bitmap = ntohs( bitmap );
1094 ibuf += sizeof( bitmap );
1095 if (( bitmap & (1<<VOLPBIT_VID)) == 0 ) {
1096 ret = AFPERR_BITMAP;
1100 len = (unsigned char)*ibuf++;
1101 volname = obj->oldtmp;
1102 memcpy(volname, ibuf, len );
1103 *(volname + len) = '\0';
1105 if ((len + 1) & 1) /* pad to an even boundary */
1111 for ( volume = volumes; volume; volume = volume->v_next ) {
1112 if ( strcasecmp( volname, volume->v_name ) == 0 ) {
1117 if ( volume == NULL ) {
1122 /* check for a volume password */
1123 if (volume->v_password &&
1124 strncmp(ibuf, volume->v_password, VOLPASSLEN)) {
1125 ret = AFPERR_ACCESS;
1129 if (( volume->v_flags & AFPVOL_OPEN ) == 0 ) {
1130 if ((dir = dirnew(strlen(volume->v_name) + 1)) == NULL) {
1131 syslog( LOG_ERR, "afp_openvol: malloc: %s", strerror(errno) );
1135 dir->d_did = DIRDID_ROOT;
1136 dir->d_color = DIRTREE_COLOR_BLACK; /* root node is black */
1137 strcpy( dir->d_name, volume->v_name );
1138 volume->v_dir = volume->v_root = dir;
1139 volume->v_flags |= AFPVOL_OPEN;
1142 if ( stat( volume->v_path, &st ) < 0 ) {
1147 buflen = *rbuflen - sizeof( bitmap );
1148 if (( ret = getvolparams( bitmap, volume, &st,
1149 rbuf + sizeof(bitmap), &buflen )) != AFP_OK ) {
1152 *rbuflen = buflen + sizeof( bitmap );
1153 bitmap = htons( bitmap );
1154 memcpy(rbuf, &bitmap, sizeof( bitmap ));
1156 curdir = volume->v_dir;
1157 if ( chdir( volume->v_path ) < 0 ) {
1163 if (volume->v_dbpath)
1164 volume->v_db = cnid_open (volume->v_dbpath);
1165 if (volume->v_db == 0)
1166 volume->v_db = cnid_open (volume->v_path);
1167 #endif /* CNID_DB */
1171 * If you mount a volume twice, the second time the trash appears on
1172 * the desk-top. That's because the Mac remembers the DID for the
1173 * trash (even for volumes in different zones, on different servers).
1174 * Just so this works better, we prime the DID cache with the trash,
1175 * fixing the trash at DID 3.
1178 cname( volume, volume->v_dir, &p );
1179 #endif /* CNID_DB */
1188 int afp_closevol(obj, ibuf, ibuflen, rbuf, rbuflen )
1191 int ibuflen, *rbuflen;
1193 struct vol *vol, *ovol;
1198 memcpy(&vid, ibuf, sizeof( vid ));
1199 if (( vol = getvolbyvid( vid )) == NULL ) {
1200 return( AFPERR_PARAM );
1203 vol->v_flags &= ~AFPVOL_OPEN;
1204 for ( ovol = volumes; ovol; ovol = ovol->v_next ) {
1205 if ( ovol->v_flags & AFPVOL_OPEN ) {
1209 if ( ovol != NULL ) {
1210 curdir = ovol->v_dir;
1211 if ( chdir( ovol->v_path ) < 0 ) {
1212 return( AFPERR_PARAM );
1216 dirfree( vol->v_root );
1219 cnid_close(vol->v_db);
1221 #endif /* AD_VERSION > AD_VERSION1 */
1225 struct vol *getvolbyvid(const u_int16_t vid )
1229 for ( vol = volumes; vol; vol = vol->v_next ) {
1230 if ( vid == vol->v_vid ) {
1234 if ( vol == NULL || ( vol->v_flags & AFPVOL_OPEN ) == 0 ) {
1241 struct extmap *getextmap(const char *path)
1246 if (( p = strrchr( path, '.' )) == NULL ) {
1247 return( defextmap );
1250 for ( em = extmap; em; em = em->em_next ) {
1251 if ( strdiacasecmp( em->em_ext, p ) == 0 ) {
1256 return( defextmap );
1262 void setvoltime(obj, vol )
1268 /* just looking at vol->v_time is broken seriously since updates
1269 * from other users afpd processes never are seen.
1270 * This is not the most elegant solution (a shared memory between
1271 * the afpd processes would come closer)
1274 if ( gettimeofday( &tv, 0 ) < 0 ) {
1275 syslog( LOG_ERR, "setvoltime: gettimeofday: %s", strerror(errno) );
1278 if( utime( vol->v_path, NULL ) < 0 ) {
1279 /* write of time failed ... probably a read only filesys,
1280 * where no other users can interfere, so there's no issue here
1284 /* a little granularity */
1285 if (vol->v_time < tv.tv_sec) {
1286 vol->v_time = tv.tv_sec;
1287 obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED);
1291 int afp_getvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1294 int ibuflen, *rbuflen;
1299 u_int16_t vid, bitmap;
1302 memcpy(&vid, ibuf, sizeof( vid ));
1303 ibuf += sizeof( vid );
1304 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1305 bitmap = ntohs( bitmap );
1307 if (( vol = getvolbyvid( vid )) == NULL ) {
1309 return( AFPERR_PARAM );
1312 if ( stat( vol->v_path, &st ) < 0 ) {
1314 return( AFPERR_PARAM );
1317 buflen = *rbuflen - sizeof( bitmap );
1318 if (( ret = getvolparams( bitmap, vol, &st,
1319 rbuf + sizeof( bitmap ), &buflen )) != AFP_OK ) {
1323 *rbuflen = buflen + sizeof( bitmap );
1324 bitmap = htons( bitmap );
1325 memcpy(rbuf, &bitmap, sizeof( bitmap ));
1329 int afp_setvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1332 int ibuflen, *rbuflen;
1336 u_int16_t vid, bitmap;
1342 memcpy(&vid, ibuf, sizeof( vid ));
1343 ibuf += sizeof( vid );
1344 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1345 bitmap = ntohs( bitmap );
1346 ibuf += sizeof(bitmap);
1348 if (( vol = getvolbyvid( vid )) == NULL ) {
1349 return( AFPERR_PARAM );
1352 if (vol->v_flags & AFPVOL_RO)
1353 return AFPERR_VLOCK;
1355 /* we can only set the backup date. */
1356 if (bitmap != VOLPBIT_BDATE)
1357 return AFPERR_BITMAP;
1359 memset(&ad, 0, sizeof(ad));
1360 if ( ad_open( vol->v_path, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR,
1363 return AFPERR_VLOCK;
1365 return AFPERR_ACCESS;
1368 memcpy(&aint, ibuf, sizeof(aint));
1369 ad_setdate(&ad, AD_DATE_BACKUP, aint);
1370 ad_flush(&ad, ADFLAGS_HF);
1371 ad_close(&ad, ADFLAGS_HF);