2 * $Id: volume.c,v 1.33 2002-08-30 19:32:41 didg 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 <atalk/logger.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 */
40 #else /* STDC_HEADERS */
44 #endif /* HAVE_STRCHR */
45 char *strchr (), *strrchr ();
47 #define memcpy(d,s,n) bcopy ((s), (d), (n))
48 #define memmove(d,s,n) bcopy ((s), (d), (n))
49 #endif /* ! HAVE_MEMCPY */
50 #endif /* STDC_HEADERS */
57 #include "directory.h"
64 #define MIN(a, b) ((a) < (b) ? (a) : (b))
67 #ifndef NO_LARGE_VOL_SUPPORT
68 #if BYTE_ORDER == BIG_ENDIAN
71 #else /* BYTE_ORDER == BIG_ENDIAN */
72 #define hton64(x) ((u_int64_t) (htonl(((x) >> 32) & 0xffffffffLL)) | \
73 (u_int64_t) ((htonl(x) & 0xffffffffLL) << 32))
74 #define ntoh64(x) (hton64(x))
75 #endif /* BYTE_ORDER == BIG_ENDIAN */
76 #endif /* ! NO_LARGE_VOL_SUPPORT */
78 static struct vol *volumes = NULL;
79 static int lastvid = 0;
81 static char *Trash = "\02\024Network Trash Folder";
83 static struct extmap *extmap = NULL, *defextmap = NULL;
85 #define VOLOPT_ALLOW 0 /* user allow list */
86 #define VOLOPT_DENY 1 /* user deny list */
87 #define VOLOPT_RWLIST 2 /* user rw list */
88 #define VOLOPT_ROLIST 3 /* user ro list */
89 #define VOLOPT_CODEPAGE 4 /* codepage */
90 #define VOLOPT_PASSWORD 5 /* volume password */
91 #define VOLOPT_CASEFOLD 6 /* character case mangling */
92 #define VOLOPT_FLAGS 7 /* various flags */
93 #define VOLOPT_DBPATH 8 /* path to database */
94 #define VOLOPT_MAPCHARS 9 /* does mtou and utom mappings. syntax:
95 m and u can be double-byte hex
100 !u -> make u illegal always
101 ~u -> make u illegal only as the first
102 part of a double-byte character.
104 #define VOLOPT_VETO 10 /* list of veto filespec */
110 #define VOLOPT_FORCEUID 11 /* force uid for username x */
111 #define VOLOPT_FORCEGID 12 /* force gid for group x */
112 #define VOLOPT_MAX 12
113 #else /* normally, there are only 9 possible options */
114 #define VOLOPT_MAX 10
115 #endif /* FORCE_UIDGID */
117 #define VOLOPT_NUM (VOLOPT_MAX + 1)
120 #define VOLOPT_DEFAULT ":DEFAULT:"
121 #define VOLOPT_DEFAULT_LEN 9
127 static __inline__ void volfree(struct vol_option *options,
128 const struct vol_option *save)
133 for (i = 0; i < VOLOPT_MAX; i++) {
134 if (options[i].c_value && (options[i].c_value != save[i].c_value))
135 free(options[i].c_value);
138 for (i = 0; i < VOLOPT_MAX; i++) {
139 if (options[i].c_value)
140 free(options[i].c_value);
146 /* handle variable substitutions. here's what we understand:
147 * $c -> client ip/appletalk address
148 * $f -> full name (whatever's in the gecos field)
151 * $s -> server name (hostname if it doesn't exist)
152 * $u -> username (guest is usually nobody)
153 * $v -> volume name (ADEID_NAME or basename)
154 * $z -> zone (may not exist)
157 #define is_var(a, b) (strncmp((a), (b), 2) == 0)
158 static void volxlate(AFPObj *obj, char *dest, int destlen,
159 char *src, struct passwd *pwd, char *path)
164 strncpy(dest, src, destlen);
165 if ((p = strchr(src, '$')) == NULL) /* nothing to do */
168 /* first part of the path. just forward to the next variable. */
169 len = MIN(p - src, destlen);
175 while (p && destlen > 0) {
176 /* now figure out what the variable is */
178 if (is_var(p, "$c")) {
179 if (obj->proto == AFPPROTO_ASP) {
180 ASP asp = obj->handle;
182 len = sprintf(dest, "%u.%u", ntohs(asp->asp_sat.sat_addr.s_net),
183 asp->asp_sat.sat_addr.s_node);
187 } else if (obj->proto == AFPPROTO_DSI) {
188 DSI *dsi = obj->handle;
190 len = sprintf(dest, "%s:%u", inet_ntoa(dsi->client.sin_addr),
191 ntohs(dsi->client.sin_port));
195 } else if (is_var(p, "$f")) {
196 if ((q = strchr(pwd->pw_gecos, ',')))
199 } else if (is_var(p, "$g")) {
200 struct group *grp = getgrgid(pwd->pw_gid);
203 } else if (is_var(p, "$h")) {
204 q = obj->options.hostname;
205 } else if (is_var(p, "$s")) {
208 else if (obj->options.server) {
209 q = obj->options.server;
211 q = obj->options.hostname;
212 } else if (is_var(p, "$u")) {
214 } else if (is_var(p, "$v")) {
218 memset(&ad, 0, sizeof(ad));
219 if (ad_open(path, ADFLAGS_HF, O_RDONLY, 0, &ad) < 0)
222 if ((len = MIN(ad_getentrylen(&ad, ADEID_NAME), destlen)) > 0) {
223 memcpy(dest, ad_entry(&ad, ADEID_NAME), len);
224 ad_close(&ad, ADFLAGS_HF);
228 ad_close(&ad, ADFLAGS_HF);
229 no_volname: /* simple basename */
230 if ((q = strrchr(path, '/')) == NULL)
232 else if (*(q + 1) != '\0')
236 } else if (is_var(p, "$z")) {
238 } else if (is_var(p, "$$")) {
243 /* copy the stuff over. if we don't understand something that we
244 * should, just skip it over. */
246 len = MIN(p == q ? 2 : strlen(q), destlen);
247 strncpy(dest, q, len);
252 /* stuff up to next $ */
254 p = strchr(src, '$');
255 len = p ? MIN(p - src, destlen) : destlen;
257 strncpy(dest, src, len);
264 /* to make sure that val is valid, make sure to select an opt that
266 #define optionok(buf,opt,val) (strstr((buf),(opt)) && ((val)[1] != '\0'))
268 static __inline__ char *get_codepage_path(const char *path, const char *name)
274 page = (char *) malloc((len = strlen(path)) + strlen(name) + 2);
277 if (path[len - 1] != '/') /* add a / */
285 /* debug: show which codepage directory we are using */
286 LOG(log_debug, logtype_afpd, "using codepage directory: %s", page);
291 /* handle all the options. tmp can't be NULL. */
292 static void volset(struct vol_option *options, char *volname, int vlen,
293 const char *nlspath, const char *tmp)
297 val = strchr(tmp, ':');
298 if (optionok(tmp, "allow:", val)) {
299 if (options[VOLOPT_ALLOW].c_value)
300 free(options[VOLOPT_ALLOW].c_value);
301 options[VOLOPT_ALLOW].c_value = strdup(val + 1);
303 } else if (optionok(tmp, "deny:", val)) {
304 if (options[VOLOPT_DENY].c_value)
305 free(options[VOLOPT_DENY].c_value);
306 options[VOLOPT_DENY].c_value = strdup(val + 1);
308 } else if (optionok(tmp, "rwlist:", val)) {
309 if (options[VOLOPT_RWLIST].c_value)
310 free(options[VOLOPT_RWLIST].c_value);
311 options[VOLOPT_RWLIST].c_value = strdup(val + 1);
313 } else if (optionok(tmp, "rolist:", val)) {
314 if (options[VOLOPT_ROLIST].c_value)
315 free(options[VOLOPT_ROLIST].c_value);
316 options[VOLOPT_ROLIST].c_value = strdup(val + 1);
318 } else if (optionok(tmp, "codepage:", val)) {
319 if (options[VOLOPT_CODEPAGE].c_value)
320 free(options[VOLOPT_CODEPAGE].c_value);
321 options[VOLOPT_CODEPAGE].c_value = get_codepage_path(nlspath, val + 1);
323 } else if (optionok(tmp, "veto:", val)) {
324 if (options[VOLOPT_VETO].c_value)
325 free(options[VOLOPT_VETO].c_value);
326 options[VOLOPT_VETO].c_value = strdup(val + 1);
328 } else if (optionok(tmp, "casefold:", val)) {
329 if (strcasecmp(val + 1, "tolower") == 0)
330 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMLOWER;
331 else if (strcasecmp(val + 1, "toupper") == 0)
332 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMUPPER;
333 else if (strcasecmp(val + 1, "xlatelower") == 0)
334 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UUPPERMLOWER;
335 else if (strcasecmp(val + 1, "xlateupper") == 0)
336 options[VOLOPT_CASEFOLD].i_value = AFPVOL_ULOWERMUPPER;
338 } else if (optionok(tmp, "options:", val)) {
341 if ((p = strtok(val + 1, ",")) == NULL) /* nothing */
345 if (strcasecmp(p, "prodos") == 0)
346 options[VOLOPT_FLAGS].i_value |= AFPVOL_A2VOL;
347 else if (strcasecmp(p, "mswindows") == 0) {
348 options[VOLOPT_FLAGS].i_value |= AFPVOL_MSWINDOWS;
349 if (!options[VOLOPT_CODEPAGE].c_value)
350 options[VOLOPT_CODEPAGE].c_value =
351 get_codepage_path(nlspath, MSWINDOWS_CODEPAGE);
353 } else if (strcasecmp(p, "crlf") == 0)
354 options[VOLOPT_FLAGS].i_value |= AFPVOL_CRLF;
355 else if (strcasecmp(p, "noadouble") == 0)
356 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOADOUBLE;
357 else if (strcasecmp(p, "ro") == 0)
358 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
359 else if (strcasecmp(p, "nohex") == 0)
360 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOHEX;
361 else if (strcasecmp(p, "usedots") == 0)
362 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS;
363 else if (strcasecmp(p, "limitsize") == 0)
364 options[VOLOPT_FLAGS].i_value |= AFPVOL_LIMITSIZE;
365 /* support for either "dropbox" or "dropkludge" */
366 else if (strcasecmp(p, "dropbox") == 0)
367 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
368 else if (strcasecmp(p, "dropkludge") == 0)
369 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
370 else if (strcasecmp(p, "nofileid") == 0)
371 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOFILEID;
373 p = strtok(NULL, ",");
377 } else if (optionok(tmp, "dbpath:", val)) {
378 if (options[VOLOPT_DBPATH].c_value)
379 free(options[VOLOPT_DBPATH].c_value);
381 options[VOLOPT_DBPATH].c_value = strdup(val + 1);
383 } else if (optionok(tmp, "mapchars:",val)) {
384 if (options[VOLOPT_MAPCHARS].c_value)
385 free(options[VOLOPT_MAPCHARS].c_value);
386 options[VOLOPT_MAPCHARS].c_value = strdup(val + 1);
388 } else if (optionok(tmp, "password:", val)) {
389 if (options[VOLOPT_PASSWORD].c_value)
390 free(options[VOLOPT_PASSWORD].c_value);
391 options[VOLOPT_PASSWORD].c_value = strdup(val + 1);
395 /* this code allows forced uid/gid per volume settings */
396 } else if (optionok(tmp, "forceuid:", val)) {
397 if (options[VOLOPT_FORCEUID].c_value)
398 free(options[VOLOPT_FORCEUID].c_value);
399 options[VOLOPT_FORCEUID].c_value = strdup(val + 1);
400 } else if (optionok(tmp, "forcegid:", val)) {
401 if (options[VOLOPT_FORCEGID].c_value)
402 free(options[VOLOPT_FORCEGID].c_value);
403 options[VOLOPT_FORCEGID].c_value = strdup(val + 1);
405 #endif /* FORCE_UIDGID */
408 /* ignore unknown options */
409 LOG(log_debug, logtype_afpd, "ignoring unknown volume option: %s", tmp);
412 /* we'll assume it's a volume name. */
413 strncpy(volname, tmp, vlen);
417 static int creatvol(const char *path, char *name, struct vol_option *options)
422 if ( name == NULL || *name == '\0' ) {
423 if ((name = strrchr( path, '/' )) == NULL) {
424 return -1; /* Obviously not a fully qualified path */
427 /* if you wish to share /, you need to specify a name. */
432 for ( volume = volumes; volume; volume = volume->v_next ) {
433 if ( strcasecmp( volume->v_name, name ) == 0 ) {
434 return -1; /* Won't be able to access it, anyway... */
438 vlen = strlen( name );
439 if ( vlen > AFPVOL_NAMELEN ) {
440 vlen = AFPVOL_NAMELEN;
441 name[AFPVOL_NAMELEN] = '\0';
445 (struct vol *)calloc(1, sizeof( struct vol ))) == NULL ) {
446 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
449 if (( volume->v_name =
450 (char *)malloc( vlen + 1 )) == NULL ) {
451 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
455 if (( volume->v_path =
456 (char *)malloc( strlen( path ) + 1 )) == NULL ) {
457 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
458 free(volume->v_name);
463 strcpy( volume->v_name, name);
464 strcpy( volume->v_path, path );
468 #endif /* __svr4__ */
469 volume->v_vid = lastvid++;
470 volume->v_lastdid = 17;
474 /* should we casefold? */
475 volume->v_casefold = options[VOLOPT_CASEFOLD].i_value;
477 /* shift in some flags */
478 volume->v_flags = options[VOLOPT_FLAGS].i_value;
480 /* read in the code pages */
481 if (options[VOLOPT_CODEPAGE].c_value)
482 codepage_read(volume, options[VOLOPT_CODEPAGE].c_value);
484 if (options[VOLOPT_PASSWORD].c_value)
485 volume->v_password = strdup(options[VOLOPT_PASSWORD].c_value);
487 if (options[VOLOPT_VETO].c_value)
488 volume->v_veto = strdup(options[VOLOPT_VETO].c_value);
491 if (options[VOLOPT_DBPATH].c_value)
492 volume->v_dbpath = strdup(options[VOLOPT_DBPATH].c_value);
497 if (options[VOLOPT_FORCEUID].c_value) {
498 volume->v_forceuid = strdup(options[VOLOPT_FORCEUID].c_value);
500 volume->v_forceuid = NULL; /* set as null so as to return 0 later on */
503 if (options[VOLOPT_FORCEGID].c_value) {
504 volume->v_forcegid = strdup(options[VOLOPT_FORCEGID].c_value);
506 volume->v_forcegid = NULL; /* set as null so as to return 0 later on */
509 #endif /* FORCE_UIDGID */
513 volume->v_next = volumes;
518 static char *myfgets( buf, size, fp )
527 while ((( c = getc( fp )) != EOF ) && ( size > 0 )) {
528 if ( c == '\n' || c == '\r' ) {
546 /* check access list. this function wants something of the following
548 * @group,name,name2,@group2,name3
550 * a NULL argument allows everybody to have access.
551 * we return three things:
553 * 0: list exists, but name isn't in it
556 static int accessvol(args, name)
560 char buf[MAXPATHLEN + 1], *p;
566 strncpy(buf, args, sizeof(buf));
567 if ((p = strtok(buf, ",")) == NULL) /* nothing, return okay */
571 if (*p == '@') { /* it's a group */
572 if ((gr = getgrnam(p + 1)) && gmem(gr->gr_gid))
574 } else if (strcmp(p, name) == 0) /* it's a user name */
576 p = strtok(NULL, ",");
582 static void setextmap( ext, type, creator, user)
583 char *ext, *type, *creator;
588 for ( em = extmap; em; em = em->em_next ) {
589 if ( strdiacasecmp( em->em_ext, ext ) == 0 ) {
596 (struct extmap *)malloc( sizeof( struct extmap ))) == NULL ) {
597 LOG(log_error, logtype_afpd, "setextmap: malloc: %s", strerror(errno) );
600 em->em_next = extmap;
602 } else if ( !user ) {
606 strcpy( em->em_ext, ext );
608 if ( *type == '\0' ) {
609 memcpy(em->em_type, "????", sizeof( em->em_type ));
611 memcpy(em->em_type, type, sizeof( em->em_type ));
613 if ( *creator == '\0' ) {
614 memcpy(em->em_creator, "UNIX", sizeof( em->em_creator ));
616 memcpy(em->em_creator, creator, sizeof( em->em_creator ));
619 if ( strcmp( ext, "." ) == 0 ) {
625 * Read a volume configuration file and add the volumes contained within to
626 * the global volume list. If p2 is non-NULL, the file that is opened is
629 * Lines that begin with # and blank lines are ignored.
630 * Volume lines are of the form:
631 * <unix path> [<volume name>] [allow:<user>,<@group>,...] \
632 * [codepage:<file>] [casefold:<num>]
633 * <extension> TYPE [CREATOR]
635 static int readvolfile(obj, p1, p2, user, pwent)
639 struct passwd *pwent;
642 char path[ MAXPATHLEN + 1], tmp[ MAXPATHLEN + 1],
643 volname[ AFPVOL_NAMELEN + 1 ], buf[ BUFSIZ ],
644 type[ 5 ], creator[ 5 ];
647 struct vol_option options[VOLOPT_NUM], save_options[VOLOPT_NUM];
659 if (( fp = fopen( path, "r" )) == NULL ) {
663 memset(save_options, 0, sizeof(save_options));
664 while ( myfgets( buf, sizeof( buf ), fp ) != NULL ) {
665 initline( strlen( buf ), buf );
666 parseline( sizeof( path ) - 1, path );
673 /* change the default options for this file */
674 if (strncmp(path, VOLOPT_DEFAULT, VOLOPT_DEFAULT_LEN) == 0) {
676 for (i = 0; i < VOLOPT_NUM; i++) {
677 if (parseline( sizeof( path ) - VOLOPT_DEFAULT_LEN - 1,
678 path + VOLOPT_DEFAULT_LEN) < 0)
680 volset(save_options, tmp, sizeof(tmp) - 1,
681 obj->options.nlspath, path + VOLOPT_DEFAULT_LEN);
687 if (( p = strchr( path, '/' )) != NULL ) {
695 if ( u == NULL || *u == '\0' || ( pw = getpwnam( u )) == NULL ) {
698 strcpy( tmp, pw->pw_dir );
699 if ( p != NULL && *p != '\0' ) {
706 /* send path through variable substitution */
707 if (*path != '~') /* need to copy path to tmp */
710 pwent = getpwnam(obj->username);
711 volxlate(obj, path, sizeof(path) - 1, tmp, pwent, NULL);
713 /* this is sort of braindead. basically, i want to be
714 * able to specify things in any order, but i don't want to
715 * re-write everything.
717 * currently we have 11 options:
725 * options:prodos,crlf,noadouble,ro
728 * namemask:x,y,!z (not implemented yet)
730 memcpy(options, save_options, sizeof(options));
733 /* read in up to 11 possible options */
734 for (i = 0; i < VOLOPT_NUM; i++) {
735 if (parseline( sizeof( tmp ) - 1, tmp ) < 0)
738 volset(options, volname, sizeof(volname) - 1,
739 obj->options.nlspath, tmp);
742 /* check allow/deny lists:
743 allow -> either no list (-1), or in list (1)
744 deny -> either no list (-1), or not in list (0) */
745 if (accessvol(options[VOLOPT_ALLOW].c_value, obj->username) &&
746 (accessvol(options[VOLOPT_DENY].c_value, obj->username) < 1)) {
748 /* handle read-only behaviour. semantics:
749 * 1) neither the rolist nor the rwlist exist -> rw
750 * 2) rolist exists -> ro if user is in it.
751 * 3) rwlist exists -> ro unless user is in it. */
752 if (((options[VOLOPT_FLAGS].i_value & AFPVOL_RO) == 0) &&
753 ((accessvol(options[VOLOPT_ROLIST].c_value,
754 obj->username) == 1) ||
755 !accessvol(options[VOLOPT_RWLIST].c_value,
757 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
759 /* do variable substitution */
760 volxlate(obj, tmp, sizeof(tmp) - 1, volname, pwent, path);
761 creatvol(path, tmp, options);
763 volfree(options, save_options);
767 parseline( sizeof( type ) - 1, type );
768 parseline( sizeof( creator ) - 1, creator );
769 setextmap( path, type, creator, user);
776 volfree(save_options, NULL);
777 if ( fclose( fp ) != 0 ) {
778 LOG(log_error, logtype_afpd, "readvolfile: fclose: %s", strerror(errno) );
784 static void load_volumes(AFPObj *obj)
786 struct passwd *pwent = getpwnam(obj->username);
788 if ( (obj->options.flags & OPTION_USERVOLFIRST) == 0 ) {
789 readvolfile(obj, obj->options.systemvol, NULL, 0, pwent);
792 if ((*obj->username == '\0') || (obj->options.flags & OPTION_NOUSERVOL)) {
793 readvolfile(obj, obj->options.defaultvol, NULL, 1, pwent);
796 * Read user's AppleVolumes or .AppleVolumes file
797 * If neither are readable, read the default volumes file. if
798 * that doesn't work, create a user share.
800 if ( readvolfile(obj, pwent->pw_dir, "AppleVolumes", 1, pwent) < 0 &&
801 readvolfile(obj, pwent->pw_dir, ".AppleVolumes", 1, pwent) < 0 &&
802 readvolfile(obj, pwent->pw_dir, "applevolumes", 1, pwent) < 0 &&
803 readvolfile(obj, pwent->pw_dir, ".applevolumes", 1, pwent) < 0 &&
804 obj->options.defaultvol != NULL ) {
805 if (readvolfile(obj, obj->options.defaultvol, NULL, 1, pwent) < 0)
806 creatvol(pwent->pw_dir, NULL, NULL);
809 if ( obj->options.flags & OPTION_USERVOLFIRST ) {
810 readvolfile(obj, obj->options.systemvol, NULL, 0, pwent );
814 static int getvolspace( vol, bfree, btotal, xbfree, xbtotal, bsize )
816 u_int32_t *bfree, *btotal, *bsize;
817 VolSpace *xbfree, *xbtotal;
821 #ifndef NO_QUOTA_SUPPORT
822 VolSpace qfree, qtotal;
823 #endif /* ! NO_QUOTA_SUPPORT */
825 spaceflag = AFPVOL_GVSMASK & vol->v_flags;
826 /* report up to 2GB if afp version is < 2.2 (4GB if not) */
827 maxsize = (vol->v_flags & AFPVOL_A2VOL) ? 0x01fffe00 :
828 (((afp_version < 22) || (vol->v_flags & AFPVOL_LIMITSIZE))
829 ? 0x7fffffffL : 0xffffffffL);
832 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_AFSGVS ) {
833 if ( afs_getvolspace( vol, xbfree, xbtotal, bsize ) == AFP_OK ) {
834 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_AFSGVS;
835 goto getvolspace_done;
840 if (( rc = ustatfs_getvolspace( vol, xbfree, xbtotal,
841 bsize)) != AFP_OK ) {
845 #define min(a,b) ((a)<(b)?(a):(b))
846 #ifndef NO_QUOTA_SUPPORT
847 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_UQUOTA ) {
848 if ( uquota_getvolspace( vol, &qfree, &qtotal, *bsize ) == AFP_OK ) {
849 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_UQUOTA;
850 *xbfree = min(*xbfree, qfree);
851 *xbtotal = min( *xbtotal, qtotal);
852 goto getvolspace_done;
855 #endif /* ! NO_QUOTA_SUPPORT */
856 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_USTATFS;
859 *bfree = min( *xbfree, maxsize);
860 *btotal = min( *xbtotal, maxsize);
864 static int getvolparams( bitmap, vol, st, buf, buflen )
872 int bit = 0, isad = 1;
875 u_int32_t bfree, btotal, bsize;
876 VolSpace xbfree, xbtotal; /* extended bytes */
877 char *data, *nameoff = NULL;
880 /* courtesy of jallison@whistle.com:
881 * For MacOS8.x support we need to create the
882 * .Parent file here if it doesn't exist. */
884 memset(&ad, 0, sizeof(ad));
885 if ( ad_open( vol->v_path, vol_noadouble(vol) |
886 ADFLAGS_HF|ADFLAGS_DIR, O_RDWR | O_CREAT,
890 } else if (ad_getoflags( &ad, ADFLAGS_HF ) & O_CREAT) {
891 slash = strrchr( vol->v_path, '/' );
897 ad_setentrylen( &ad, ADEID_NAME, strlen( slash ));
898 memcpy(ad_entry( &ad, ADEID_NAME ), slash,
899 ad_getentrylen( &ad, ADEID_NAME ));
900 ad_flush(&ad, ADFLAGS_HF);
903 if (( bitmap & ( (1<<VOLPBIT_BFREE)|(1<<VOLPBIT_BTOTAL) |
904 (1<<VOLPBIT_XBFREE)|(1<<VOLPBIT_XBTOTAL) |
905 (1<<VOLPBIT_BSIZE)) ) != 0 ) {
906 if ( getvolspace( vol, &bfree, &btotal, &xbfree, &xbtotal,
909 ad_close( &ad, ADFLAGS_HF );
911 return( AFPERR_PARAM );
916 while ( bitmap != 0 ) {
917 while (( bitmap & 1 ) == 0 ) {
926 if (0 == (vol->v_flags & AFPVOL_NOFILEID)) {
927 ashort = VOLPBIT_ATTR_FILEID;
930 /* check for read-only.
931 * NOTE: we don't actually set the read-only flag unless
932 * it's passed in that way as it's possible to mount
933 * a read-write filesystem under a read-only one. */
934 if ((vol->v_flags & AFPVOL_RO) ||
935 ((utime(vol->v_path, NULL) < 0) && (errno == EROFS)))
936 ashort |= VOLPBIT_ATTR_RO;
937 #ifdef WITH_CATSEARCH
938 ashort |= VOLPBIT_ATTR_CATSEARCH;
940 ashort = htons(ashort);
941 memcpy(data, &ashort, sizeof( ashort ));
942 data += sizeof( ashort );
946 ashort = htons( AFPVOLSIG_DEFAULT );
947 memcpy(data, &ashort, sizeof( ashort ));
948 data += sizeof( ashort );
952 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
953 aint = AD_DATE_FROM_UNIX(st->st_mtime);
954 memcpy(data, &aint, sizeof( aint ));
955 data += sizeof( aint );
959 if ( st->st_mtime > vol->v_time ) {
960 vol->v_time = st->st_mtime;
961 aint = AD_DATE_FROM_UNIX(st->st_mtime);
963 aint = AD_DATE_FROM_UNIX(vol->v_time);
965 memcpy(data, &aint, sizeof( aint ));
966 data += sizeof( aint );
970 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
971 aint = AD_DATE_START;
972 memcpy(data, &aint, sizeof( aint ));
973 data += sizeof( aint );
977 memcpy(data, &vol->v_vid, sizeof( vol->v_vid ));
978 data += sizeof( vol->v_vid );
982 bfree = htonl( bfree );
983 memcpy(data, &bfree, sizeof( bfree ));
984 data += sizeof( bfree );
987 case VOLPBIT_BTOTAL :
988 btotal = htonl( btotal );
989 memcpy(data, &btotal, sizeof( btotal ));
990 data += sizeof( btotal );
993 #ifndef NO_LARGE_VOL_SUPPORT
994 case VOLPBIT_XBFREE :
995 xbfree = hton64( xbfree );
996 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
997 bcopy(&xbfree, data, sizeof(xbfree));
998 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
999 memcpy(data, &xbfree, sizeof( xbfree ));
1000 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1001 data += sizeof( xbfree );
1004 case VOLPBIT_XBTOTAL :
1005 xbtotal = hton64( xbtotal );
1006 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1007 bcopy(&xbtotal, data, sizeof(xbtotal));
1008 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1009 memcpy(data, &xbtotal, sizeof( xbtotal ));
1010 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1011 data += sizeof( xbfree );
1013 #endif /* ! NO_LARGE_VOL_SUPPORT */
1017 data += sizeof( u_int16_t );
1020 case VOLPBIT_BSIZE: /* block size */
1021 bsize = htonl(bsize);
1022 memcpy(data, &bsize, sizeof(bsize));
1023 data += sizeof(bsize);
1028 ad_close( &ad, ADFLAGS_HF );
1030 return( AFPERR_BITMAP );
1036 ashort = htons( data - buf );
1037 memcpy(nameoff, &ashort, sizeof( ashort ));
1038 aint = strlen( vol->v_name );
1040 memcpy(data, vol->v_name, aint );
1044 ad_close( &ad, ADFLAGS_HF );
1046 *buflen = data - buf;
1052 int afp_getsrvrparms(obj, ibuf, ibuflen, rbuf, rbuflen )
1055 int ibuflen, *rbuflen;
1068 for ( vcnt = 0, volume = volumes; volume; volume = volume->v_next ) {
1069 if ( stat( volume->v_path, &st ) < 0 ) {
1070 LOG(log_info, logtype_afpd, "afp_getsrvrparms: stat %s: %s",
1071 volume->v_path, strerror(errno) );
1072 continue; /* can't access directory */
1074 if (!S_ISDIR(st.st_mode)) {
1075 continue; /* not a dir */
1078 /* set password bit if there's a volume password */
1079 *data = (volume->v_password) ? AFPSRVR_PASSWD : 0;
1081 /* Apple 2 clients running ProDOS-8 expect one volume to have
1082 bit 0 of this byte set. They will not recognize anything
1083 on the server unless this is the case. I have not
1084 completely worked this out, but it's related to booting
1085 from the server. Support for that function is a ways
1086 off.. <shirsch@ibm.net> */
1087 *data++ |= (volume->v_flags & AFPVOL_A2VOL) ? AFPSRVR_CONFIGINFO : 0;
1088 len = strlen( volume->v_name );
1090 memcpy(data, volume->v_name, len );
1095 *rbuflen = data - rbuf;
1097 if ( gettimeofday( &tv, 0 ) < 0 ) {
1098 LOG(log_error, logtype_afpd, "afp_getsrvrparms: gettimeofday: %s", strerror(errno) );
1100 return AFPERR_PARAM;
1102 tv.tv_sec = AD_DATE_FROM_UNIX(tv.tv_sec);
1103 memcpy(data, &tv.tv_sec, sizeof( u_int32_t));
1104 data += sizeof( u_int32_t);
1109 int afp_openvol(obj, ibuf, ibuflen, rbuf, rbuflen )
1112 int ibuflen, *rbuflen;
1118 #endif /* CNID_DB */
1121 int len, ret, buflen;
1125 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1126 bitmap = ntohs( bitmap );
1127 ibuf += sizeof( bitmap );
1128 if (( bitmap & (1<<VOLPBIT_VID)) == 0 ) {
1129 ret = AFPERR_BITMAP;
1133 len = (unsigned char)*ibuf++;
1134 volname = obj->oldtmp;
1135 memcpy(volname, ibuf, len );
1136 *(volname + len) = '\0';
1138 if ((len + 1) & 1) /* pad to an even boundary */
1144 for ( volume = volumes; volume; volume = volume->v_next ) {
1145 if ( strcasecmp( volname, volume->v_name ) == 0 ) {
1150 if ( volume == NULL ) {
1155 /* check for a volume password */
1156 if (volume->v_password &&
1157 strncmp(ibuf, volume->v_password, VOLPASSLEN)) {
1158 ret = AFPERR_ACCESS;
1162 if (( volume->v_flags & AFPVOL_OPEN ) == 0 ) {
1163 if ((dir = dirnew(strlen(volume->v_name) + 1)) == NULL) {
1164 LOG(log_error, logtype_afpd, "afp_openvol: malloc: %s", strerror(errno) );
1168 dir->d_did = DIRDID_ROOT;
1169 dir->d_color = DIRTREE_COLOR_BLACK; /* root node is black */
1170 strcpy( dir->d_name, volume->v_name );
1171 volume->v_dir = volume->v_root = dir;
1172 volume->v_flags |= AFPVOL_OPEN;
1175 set_uidgid ( volume );
1176 #endif /* FORCE_UIDGID */
1178 if ( stat( volume->v_path, &st ) < 0 ) {
1183 buflen = *rbuflen - sizeof( bitmap );
1184 if (( ret = getvolparams( bitmap, volume, &st,
1185 rbuf + sizeof(bitmap), &buflen )) != AFP_OK ) {
1188 *rbuflen = buflen + sizeof( bitmap );
1189 bitmap = htons( bitmap );
1190 memcpy(rbuf, &bitmap, sizeof( bitmap ));
1192 if ( chdir( volume->v_path ) < 0 ) {
1196 curdir = volume->v_dir;
1199 if (volume->v_dbpath)
1200 volume->v_db = cnid_open (volume->v_dbpath);
1201 if (volume->v_db == NULL)
1202 volume->v_db = cnid_open (volume->v_path);
1203 #endif /* CNID_DB */
1207 * If you mount a volume twice, the second time the trash appears on
1208 * the desk-top. That's because the Mac remembers the DID for the
1209 * trash (even for volumes in different zones, on different servers).
1210 * Just so this works better, we prime the DID cache with the trash,
1211 * fixing the trash at DID 17.
1214 cname( volume, volume->v_dir, &p );
1215 #endif /* CNID_DB */
1224 int afp_closevol(obj, ibuf, ibuflen, rbuf, rbuflen )
1227 int ibuflen, *rbuflen;
1229 struct vol *vol, *ovol;
1234 memcpy(&vid, ibuf, sizeof( vid ));
1235 if (( vol = getvolbyvid( vid )) == NULL ) {
1236 return( AFPERR_PARAM );
1239 vol->v_flags &= ~AFPVOL_OPEN;
1240 for ( ovol = volumes; ovol; ovol = ovol->v_next ) {
1241 if ( ovol->v_flags & AFPVOL_OPEN ) {
1245 if ( ovol != NULL ) {
1246 /* Even if chdir fails, we can't say afp_closevol fails. */
1247 if ( chdir( ovol->v_path ) == 0 ) {
1248 curdir = ovol->v_dir;
1252 dirfree( vol->v_root );
1255 cnid_close(vol->v_db);
1257 #endif /* CNID_DB */
1261 struct vol *getvolbyvid(const u_int16_t vid )
1265 for ( vol = volumes; vol; vol = vol->v_next ) {
1266 if ( vid == vol->v_vid ) {
1270 if ( vol == NULL || ( vol->v_flags & AFPVOL_OPEN ) == 0 ) {
1276 #endif /* FORCE_UIDGID */
1281 struct extmap *getextmap(const char *path)
1286 if (( p = strrchr( path, '.' )) == NULL ) {
1287 return( defextmap );
1290 for ( em = extmap; em; em = em->em_next ) {
1291 if ( strdiacasecmp( em->em_ext, p ) == 0 ) {
1296 return( defextmap );
1302 struct extmap *getdefextmap(void)
1304 return( defextmap );
1307 void setvoltime(obj, vol )
1313 /* just looking at vol->v_time is broken seriously since updates
1314 * from other users afpd processes never are seen.
1315 * This is not the most elegant solution (a shared memory between
1316 * the afpd processes would come closer)
1319 if ( gettimeofday( &tv, 0 ) < 0 ) {
1320 LOG(log_error, logtype_afpd, "setvoltime: gettimeofday: %s", strerror(errno) );
1323 if( utime( vol->v_path, NULL ) < 0 ) {
1324 /* write of time failed ... probably a read only filesys,
1325 * where no other users can interfere, so there's no issue here
1329 /* a little granularity */
1330 if (vol->v_time < tv.tv_sec) {
1331 vol->v_time = tv.tv_sec;
1332 if (afp_version > 21 && obj->options.server_notif) {
1333 obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED);
1338 int afp_getvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1341 int ibuflen, *rbuflen;
1346 u_int16_t vid, bitmap;
1349 memcpy(&vid, ibuf, sizeof( vid ));
1350 ibuf += sizeof( vid );
1351 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1352 bitmap = ntohs( bitmap );
1354 if (( vol = getvolbyvid( vid )) == NULL ) {
1356 return( AFPERR_PARAM );
1359 if ( stat( vol->v_path, &st ) < 0 ) {
1361 return( AFPERR_PARAM );
1364 buflen = *rbuflen - sizeof( bitmap );
1365 if (( ret = getvolparams( bitmap, vol, &st,
1366 rbuf + sizeof( bitmap ), &buflen )) != AFP_OK ) {
1370 *rbuflen = buflen + sizeof( bitmap );
1371 bitmap = htons( bitmap );
1372 memcpy(rbuf, &bitmap, sizeof( bitmap ));
1376 int afp_setvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1379 int ibuflen, *rbuflen;
1383 u_int16_t vid, bitmap;
1389 memcpy(&vid, ibuf, sizeof( vid ));
1390 ibuf += sizeof( vid );
1391 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1392 bitmap = ntohs( bitmap );
1393 ibuf += sizeof(bitmap);
1395 if (( vol = getvolbyvid( vid )) == NULL ) {
1396 return( AFPERR_PARAM );
1399 if (vol->v_flags & AFPVOL_RO)
1400 return AFPERR_VLOCK;
1402 /* we can only set the backup date. */
1403 if (bitmap != VOLPBIT_BDATE)
1404 return AFPERR_BITMAP;
1406 memset(&ad, 0, sizeof(ad));
1407 if ( ad_open( vol->v_path, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR,
1410 return AFPERR_VLOCK;
1412 return AFPERR_ACCESS;
1415 memcpy(&aint, ibuf, sizeof(aint));
1416 ad_setdate(&ad, AD_DATE_BACKUP, aint);
1417 ad_flush(&ad, ADFLAGS_HF);
1418 ad_close(&ad, ADFLAGS_HF);
1423 int wincheck(const struct vol *vol, const char *path)
1427 if (!(vol->v_flags & AFPVOL_MSWINDOWS))
1430 /* empty paths are not allowed */
1431 if ((len = strlen(path)) == 0)
1434 /* leading or trailing whitespaces are not allowed, carriage returns
1435 * and probably other whitespace is okay, tabs are not allowed
1437 if ((path[0] == ' ') || (path[len-1] == ' '))
1440 /* certain characters are not allowed */
1441 if (strpbrk(path, MSWINDOWS_BADCHARS))
1444 /* everything else is okay */