2 * $Id: volume.c,v 1.34 2002-08-31 05:35:10 jmarcus 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_UMASK 13
113 #define VOLOPT_MAX 13
114 #else /* normally, there are only 9 possible options */
115 #define VOLOPT_UMASK 11
116 #define VOLOPT_MAX 11
117 #endif /* FORCE_UIDGID */
119 #define VOLOPT_NUM (VOLOPT_MAX + 1)
122 #define VOLOPT_DEFAULT ":DEFAULT:"
123 #define VOLOPT_DEFAULT_LEN 9
129 static __inline__ void volfree(struct vol_option *options,
130 const struct vol_option *save)
135 for (i = 0; i < VOLOPT_MAX; i++) {
136 if (options[i].c_value && (options[i].c_value != save[i].c_value))
137 free(options[i].c_value);
140 for (i = 0; i < VOLOPT_MAX; i++) {
141 if (options[i].c_value)
142 free(options[i].c_value);
148 /* handle variable substitutions. here's what we understand:
149 * $c -> client ip/appletalk address
150 * $f -> full name (whatever's in the gecos field)
153 * $s -> server name (hostname if it doesn't exist)
154 * $u -> username (guest is usually nobody)
155 * $v -> volume name (ADEID_NAME or basename)
156 * $z -> zone (may not exist)
159 #define is_var(a, b) (strncmp((a), (b), 2) == 0)
160 static void volxlate(AFPObj *obj, char *dest, int destlen,
161 char *src, struct passwd *pwd, char *path)
166 strncpy(dest, src, destlen);
167 if ((p = strchr(src, '$')) == NULL) /* nothing to do */
170 /* first part of the path. just forward to the next variable. */
171 len = MIN(p - src, destlen);
177 while (p && destlen > 0) {
178 /* now figure out what the variable is */
180 if (is_var(p, "$c")) {
181 if (obj->proto == AFPPROTO_ASP) {
182 ASP asp = obj->handle;
184 len = sprintf(dest, "%u.%u", ntohs(asp->asp_sat.sat_addr.s_net),
185 asp->asp_sat.sat_addr.s_node);
189 } else if (obj->proto == AFPPROTO_DSI) {
190 DSI *dsi = obj->handle;
192 len = sprintf(dest, "%s:%u", inet_ntoa(dsi->client.sin_addr),
193 ntohs(dsi->client.sin_port));
197 } else if (is_var(p, "$f")) {
198 if ((q = strchr(pwd->pw_gecos, ',')))
201 } else if (is_var(p, "$g")) {
202 struct group *grp = getgrgid(pwd->pw_gid);
205 } else if (is_var(p, "$h")) {
206 q = obj->options.hostname;
207 } else if (is_var(p, "$s")) {
210 else if (obj->options.server) {
211 q = obj->options.server;
213 q = obj->options.hostname;
214 } else if (is_var(p, "$u")) {
216 } else if (is_var(p, "$v")) {
220 memset(&ad, 0, sizeof(ad));
221 if (ad_open(path, ADFLAGS_HF, O_RDONLY, 0, &ad) < 0)
224 if ((len = MIN(ad_getentrylen(&ad, ADEID_NAME), destlen)) > 0) {
225 memcpy(dest, ad_entry(&ad, ADEID_NAME), len);
226 ad_close(&ad, ADFLAGS_HF);
230 ad_close(&ad, ADFLAGS_HF);
231 no_volname: /* simple basename */
232 if ((q = strrchr(path, '/')) == NULL)
234 else if (*(q + 1) != '\0')
238 } else if (is_var(p, "$z")) {
240 } else if (is_var(p, "$$")) {
245 /* copy the stuff over. if we don't understand something that we
246 * should, just skip it over. */
248 len = MIN(p == q ? 2 : strlen(q), destlen);
249 strncpy(dest, q, len);
254 /* stuff up to next $ */
256 p = strchr(src, '$');
257 len = p ? MIN(p - src, destlen) : destlen;
259 strncpy(dest, src, len);
266 /* to make sure that val is valid, make sure to select an opt that
268 #define optionok(buf,opt,val) (strstr((buf),(opt)) && ((val)[1] != '\0'))
270 static __inline__ char *get_codepage_path(const char *path, const char *name)
276 page = (char *) malloc((len = strlen(path)) + strlen(name) + 2);
279 if (path[len - 1] != '/') /* add a / */
287 /* debug: show which codepage directory we are using */
288 LOG(log_debug, logtype_afpd, "using codepage directory: %s", page);
293 /* handle all the options. tmp can't be NULL. */
294 static void volset(struct vol_option *options, char *volname, int vlen,
295 const char *nlspath, const char *tmp)
299 val = strchr(tmp, ':');
300 if (optionok(tmp, "allow:", val)) {
301 if (options[VOLOPT_ALLOW].c_value)
302 free(options[VOLOPT_ALLOW].c_value);
303 options[VOLOPT_ALLOW].c_value = strdup(val + 1);
305 } else if (optionok(tmp, "deny:", val)) {
306 if (options[VOLOPT_DENY].c_value)
307 free(options[VOLOPT_DENY].c_value);
308 options[VOLOPT_DENY].c_value = strdup(val + 1);
310 } else if (optionok(tmp, "rwlist:", val)) {
311 if (options[VOLOPT_RWLIST].c_value)
312 free(options[VOLOPT_RWLIST].c_value);
313 options[VOLOPT_RWLIST].c_value = strdup(val + 1);
315 } else if (optionok(tmp, "rolist:", val)) {
316 if (options[VOLOPT_ROLIST].c_value)
317 free(options[VOLOPT_ROLIST].c_value);
318 options[VOLOPT_ROLIST].c_value = strdup(val + 1);
320 } else if (optionok(tmp, "codepage:", val)) {
321 if (options[VOLOPT_CODEPAGE].c_value)
322 free(options[VOLOPT_CODEPAGE].c_value);
323 options[VOLOPT_CODEPAGE].c_value = get_codepage_path(nlspath, val + 1);
325 } else if (optionok(tmp, "veto:", val)) {
326 if (options[VOLOPT_VETO].c_value)
327 free(options[VOLOPT_VETO].c_value);
328 options[VOLOPT_VETO].c_value = strdup(val + 1);
330 } else if (optionok(tmp, "casefold:", val)) {
331 if (strcasecmp(val + 1, "tolower") == 0)
332 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMLOWER;
333 else if (strcasecmp(val + 1, "toupper") == 0)
334 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMUPPER;
335 else if (strcasecmp(val + 1, "xlatelower") == 0)
336 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UUPPERMLOWER;
337 else if (strcasecmp(val + 1, "xlateupper") == 0)
338 options[VOLOPT_CASEFOLD].i_value = AFPVOL_ULOWERMUPPER;
340 } else if (optionok(tmp, "options:", val)) {
343 if ((p = strtok(val + 1, ",")) == NULL) /* nothing */
347 if (strcasecmp(p, "prodos") == 0)
348 options[VOLOPT_FLAGS].i_value |= AFPVOL_A2VOL;
349 else if (strcasecmp(p, "mswindows") == 0) {
350 options[VOLOPT_FLAGS].i_value |= AFPVOL_MSWINDOWS;
351 if (!options[VOLOPT_CODEPAGE].c_value)
352 options[VOLOPT_CODEPAGE].c_value =
353 get_codepage_path(nlspath, MSWINDOWS_CODEPAGE);
355 } else if (strcasecmp(p, "crlf") == 0)
356 options[VOLOPT_FLAGS].i_value |= AFPVOL_CRLF;
357 else if (strcasecmp(p, "noadouble") == 0)
358 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOADOUBLE;
359 else if (strcasecmp(p, "ro") == 0)
360 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
361 else if (strcasecmp(p, "nohex") == 0)
362 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOHEX;
363 else if (strcasecmp(p, "usedots") == 0)
364 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS;
365 else if (strcasecmp(p, "limitsize") == 0)
366 options[VOLOPT_FLAGS].i_value |= AFPVOL_LIMITSIZE;
367 /* support for either "dropbox" or "dropkludge" */
368 else if (strcasecmp(p, "dropbox") == 0)
369 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
370 else if (strcasecmp(p, "dropkludge") == 0)
371 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
372 else if (strcasecmp(p, "nofileid") == 0)
373 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOFILEID;
375 p = strtok(NULL, ",");
379 } else if (optionok(tmp, "dbpath:", val)) {
380 if (options[VOLOPT_DBPATH].c_value)
381 free(options[VOLOPT_DBPATH].c_value);
383 options[VOLOPT_DBPATH].c_value = strdup(val + 1);
385 } else if (optionok(tmp, "umask:", val)) {
386 options[VOLOPT_UMASK].i_value = (int)strtol(val, (char **)NULL, 8);
387 } else if (optionok(tmp, "mapchars:",val)) {
388 if (options[VOLOPT_MAPCHARS].c_value)
389 free(options[VOLOPT_MAPCHARS].c_value);
390 options[VOLOPT_MAPCHARS].c_value = strdup(val + 1);
392 } else if (optionok(tmp, "password:", val)) {
393 if (options[VOLOPT_PASSWORD].c_value)
394 free(options[VOLOPT_PASSWORD].c_value);
395 options[VOLOPT_PASSWORD].c_value = strdup(val + 1);
399 /* this code allows forced uid/gid per volume settings */
400 } else if (optionok(tmp, "forceuid:", val)) {
401 if (options[VOLOPT_FORCEUID].c_value)
402 free(options[VOLOPT_FORCEUID].c_value);
403 options[VOLOPT_FORCEUID].c_value = strdup(val + 1);
404 } else if (optionok(tmp, "forcegid:", val)) {
405 if (options[VOLOPT_FORCEGID].c_value)
406 free(options[VOLOPT_FORCEGID].c_value);
407 options[VOLOPT_FORCEGID].c_value = strdup(val + 1);
409 #endif /* FORCE_UIDGID */
412 /* ignore unknown options */
413 LOG(log_debug, logtype_afpd, "ignoring unknown volume option: %s", tmp);
416 /* we'll assume it's a volume name. */
417 strncpy(volname, tmp, vlen);
421 static int creatvol(const char *path, char *name, struct vol_option *options)
426 if ( name == NULL || *name == '\0' ) {
427 if ((name = strrchr( path, '/' )) == NULL) {
428 return -1; /* Obviously not a fully qualified path */
431 /* if you wish to share /, you need to specify a name. */
436 for ( volume = volumes; volume; volume = volume->v_next ) {
437 if ( strcasecmp( volume->v_name, name ) == 0 ) {
438 return -1; /* Won't be able to access it, anyway... */
442 vlen = strlen( name );
443 if ( vlen > AFPVOL_NAMELEN ) {
444 vlen = AFPVOL_NAMELEN;
445 name[AFPVOL_NAMELEN] = '\0';
449 (struct vol *)calloc(1, sizeof( struct vol ))) == NULL ) {
450 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
453 if (( volume->v_name =
454 (char *)malloc( vlen + 1 )) == NULL ) {
455 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
459 if (( volume->v_path =
460 (char *)malloc( strlen( path ) + 1 )) == NULL ) {
461 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
462 free(volume->v_name);
467 strcpy( volume->v_name, name);
468 strcpy( volume->v_path, path );
472 #endif /* __svr4__ */
473 volume->v_vid = lastvid++;
474 volume->v_lastdid = 17;
478 /* should we casefold? */
479 volume->v_casefold = options[VOLOPT_CASEFOLD].i_value;
481 /* shift in some flags */
482 volume->v_flags = options[VOLOPT_FLAGS].i_value;
484 /* read in the code pages */
485 if (options[VOLOPT_CODEPAGE].c_value)
486 codepage_read(volume, options[VOLOPT_CODEPAGE].c_value);
488 if (options[VOLOPT_PASSWORD].c_value)
489 volume->v_password = strdup(options[VOLOPT_PASSWORD].c_value);
491 if (options[VOLOPT_VETO].c_value)
492 volume->v_veto = strdup(options[VOLOPT_VETO].c_value);
495 if (options[VOLOPT_DBPATH].c_value)
496 volume->v_dbpath = strdup(options[VOLOPT_DBPATH].c_value);
499 if (options[VOLOPT_UMASK].i_value)
500 volume->v_umask = (mode_t)options[VOLOPT_UMASK].i_value;
504 if (options[VOLOPT_FORCEUID].c_value) {
505 volume->v_forceuid = strdup(options[VOLOPT_FORCEUID].c_value);
507 volume->v_forceuid = NULL; /* set as null so as to return 0 later on */
510 if (options[VOLOPT_FORCEGID].c_value) {
511 volume->v_forcegid = strdup(options[VOLOPT_FORCEGID].c_value);
513 volume->v_forcegid = NULL; /* set as null so as to return 0 later on */
516 #endif /* FORCE_UIDGID */
520 volume->v_next = volumes;
525 static char *myfgets( buf, size, fp )
534 while ((( c = getc( fp )) != EOF ) && ( size > 0 )) {
535 if ( c == '\n' || c == '\r' ) {
553 /* check access list. this function wants something of the following
555 * @group,name,name2,@group2,name3
557 * a NULL argument allows everybody to have access.
558 * we return three things:
560 * 0: list exists, but name isn't in it
563 static int accessvol(args, name)
567 char buf[MAXPATHLEN + 1], *p;
573 strncpy(buf, args, sizeof(buf));
574 if ((p = strtok(buf, ",")) == NULL) /* nothing, return okay */
578 if (*p == '@') { /* it's a group */
579 if ((gr = getgrnam(p + 1)) && gmem(gr->gr_gid))
581 } else if (strcmp(p, name) == 0) /* it's a user name */
583 p = strtok(NULL, ",");
589 static void setextmap( ext, type, creator, user)
590 char *ext, *type, *creator;
595 for ( em = extmap; em; em = em->em_next ) {
596 if ( strdiacasecmp( em->em_ext, ext ) == 0 ) {
603 (struct extmap *)malloc( sizeof( struct extmap ))) == NULL ) {
604 LOG(log_error, logtype_afpd, "setextmap: malloc: %s", strerror(errno) );
607 em->em_next = extmap;
609 } else if ( !user ) {
613 strcpy( em->em_ext, ext );
615 if ( *type == '\0' ) {
616 memcpy(em->em_type, "????", sizeof( em->em_type ));
618 memcpy(em->em_type, type, sizeof( em->em_type ));
620 if ( *creator == '\0' ) {
621 memcpy(em->em_creator, "UNIX", sizeof( em->em_creator ));
623 memcpy(em->em_creator, creator, sizeof( em->em_creator ));
626 if ( strcmp( ext, "." ) == 0 ) {
632 * Read a volume configuration file and add the volumes contained within to
633 * the global volume list. If p2 is non-NULL, the file that is opened is
636 * Lines that begin with # and blank lines are ignored.
637 * Volume lines are of the form:
638 * <unix path> [<volume name>] [allow:<user>,<@group>,...] \
639 * [codepage:<file>] [casefold:<num>]
640 * <extension> TYPE [CREATOR]
642 static int readvolfile(obj, p1, p2, user, pwent)
646 struct passwd *pwent;
649 char path[ MAXPATHLEN + 1], tmp[ MAXPATHLEN + 1],
650 volname[ AFPVOL_NAMELEN + 1 ], buf[ BUFSIZ ],
651 type[ 5 ], creator[ 5 ];
654 struct vol_option options[VOLOPT_NUM], save_options[VOLOPT_NUM];
666 if (( fp = fopen( path, "r" )) == NULL ) {
670 memset(save_options, 0, sizeof(save_options));
671 while ( myfgets( buf, sizeof( buf ), fp ) != NULL ) {
672 initline( strlen( buf ), buf );
673 parseline( sizeof( path ) - 1, path );
680 /* change the default options for this file */
681 if (strncmp(path, VOLOPT_DEFAULT, VOLOPT_DEFAULT_LEN) == 0) {
683 for (i = 0; i < VOLOPT_NUM; i++) {
684 if (parseline( sizeof( path ) - VOLOPT_DEFAULT_LEN - 1,
685 path + VOLOPT_DEFAULT_LEN) < 0)
687 volset(save_options, tmp, sizeof(tmp) - 1,
688 obj->options.nlspath, path + VOLOPT_DEFAULT_LEN);
694 if (( p = strchr( path, '/' )) != NULL ) {
702 if ( u == NULL || *u == '\0' || ( pw = getpwnam( u )) == NULL ) {
705 strcpy( tmp, pw->pw_dir );
706 if ( p != NULL && *p != '\0' ) {
710 /* Tag a user's home directory with their umask. Note, this will
711 * be overwritten if the user actually specifies a umask: option
712 * for a '~' volume. */
713 save_options[VOLOPT_UMASK].i_value = obj->options.save_mask;
717 /* send path through variable substitution */
718 if (*path != '~') /* need to copy path to tmp */
721 pwent = getpwnam(obj->username);
722 volxlate(obj, path, sizeof(path) - 1, tmp, pwent, NULL);
724 /* this is sort of braindead. basically, i want to be
725 * able to specify things in any order, but i don't want to
726 * re-write everything.
728 * currently we have 11 options:
736 * options:prodos,crlf,noadouble,ro
739 * namemask:x,y,!z (not implemented yet)
741 memcpy(options, save_options, sizeof(options));
744 /* read in up to 11 possible options */
745 for (i = 0; i < VOLOPT_NUM; i++) {
746 if (parseline( sizeof( tmp ) - 1, tmp ) < 0)
749 volset(options, volname, sizeof(volname) - 1,
750 obj->options.nlspath, tmp);
753 /* check allow/deny lists:
754 allow -> either no list (-1), or in list (1)
755 deny -> either no list (-1), or not in list (0) */
756 if (accessvol(options[VOLOPT_ALLOW].c_value, obj->username) &&
757 (accessvol(options[VOLOPT_DENY].c_value, obj->username) < 1)) {
759 /* handle read-only behaviour. semantics:
760 * 1) neither the rolist nor the rwlist exist -> rw
761 * 2) rolist exists -> ro if user is in it.
762 * 3) rwlist exists -> ro unless user is in it. */
763 if (((options[VOLOPT_FLAGS].i_value & AFPVOL_RO) == 0) &&
764 ((accessvol(options[VOLOPT_ROLIST].c_value,
765 obj->username) == 1) ||
766 !accessvol(options[VOLOPT_RWLIST].c_value,
768 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
770 /* do variable substitution */
771 volxlate(obj, tmp, sizeof(tmp) - 1, volname, pwent, path);
772 creatvol(path, tmp, options);
774 volfree(options, save_options);
778 parseline( sizeof( type ) - 1, type );
779 parseline( sizeof( creator ) - 1, creator );
780 setextmap( path, type, creator, user);
787 volfree(save_options, NULL);
788 if ( fclose( fp ) != 0 ) {
789 LOG(log_error, logtype_afpd, "readvolfile: fclose: %s", strerror(errno) );
795 static void load_volumes(AFPObj *obj)
797 struct passwd *pwent = getpwnam(obj->username);
799 if ( (obj->options.flags & OPTION_USERVOLFIRST) == 0 ) {
800 readvolfile(obj, obj->options.systemvol, NULL, 0, pwent);
803 if ((*obj->username == '\0') || (obj->options.flags & OPTION_NOUSERVOL)) {
804 readvolfile(obj, obj->options.defaultvol, NULL, 1, pwent);
807 * Read user's AppleVolumes or .AppleVolumes file
808 * If neither are readable, read the default volumes file. if
809 * that doesn't work, create a user share.
811 if ( readvolfile(obj, pwent->pw_dir, "AppleVolumes", 1, pwent) < 0 &&
812 readvolfile(obj, pwent->pw_dir, ".AppleVolumes", 1, pwent) < 0 &&
813 readvolfile(obj, pwent->pw_dir, "applevolumes", 1, pwent) < 0 &&
814 readvolfile(obj, pwent->pw_dir, ".applevolumes", 1, pwent) < 0 &&
815 obj->options.defaultvol != NULL ) {
816 if (readvolfile(obj, obj->options.defaultvol, NULL, 1, pwent) < 0)
817 creatvol(pwent->pw_dir, NULL, NULL);
820 if ( obj->options.flags & OPTION_USERVOLFIRST ) {
821 readvolfile(obj, obj->options.systemvol, NULL, 0, pwent );
825 static int getvolspace( vol, bfree, btotal, xbfree, xbtotal, bsize )
827 u_int32_t *bfree, *btotal, *bsize;
828 VolSpace *xbfree, *xbtotal;
832 #ifndef NO_QUOTA_SUPPORT
833 VolSpace qfree, qtotal;
834 #endif /* ! NO_QUOTA_SUPPORT */
836 spaceflag = AFPVOL_GVSMASK & vol->v_flags;
837 /* report up to 2GB if afp version is < 2.2 (4GB if not) */
838 maxsize = (vol->v_flags & AFPVOL_A2VOL) ? 0x01fffe00 :
839 (((afp_version < 22) || (vol->v_flags & AFPVOL_LIMITSIZE))
840 ? 0x7fffffffL : 0xffffffffL);
843 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_AFSGVS ) {
844 if ( afs_getvolspace( vol, xbfree, xbtotal, bsize ) == AFP_OK ) {
845 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_AFSGVS;
846 goto getvolspace_done;
851 if (( rc = ustatfs_getvolspace( vol, xbfree, xbtotal,
852 bsize)) != AFP_OK ) {
856 #define min(a,b) ((a)<(b)?(a):(b))
857 #ifndef NO_QUOTA_SUPPORT
858 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_UQUOTA ) {
859 if ( uquota_getvolspace( vol, &qfree, &qtotal, *bsize ) == AFP_OK ) {
860 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_UQUOTA;
861 *xbfree = min(*xbfree, qfree);
862 *xbtotal = min( *xbtotal, qtotal);
863 goto getvolspace_done;
866 #endif /* ! NO_QUOTA_SUPPORT */
867 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_USTATFS;
870 *bfree = min( *xbfree, maxsize);
871 *btotal = min( *xbtotal, maxsize);
875 static int getvolparams( bitmap, vol, st, buf, buflen )
883 int bit = 0, isad = 1;
886 u_int32_t bfree, btotal, bsize;
887 VolSpace xbfree, xbtotal; /* extended bytes */
888 char *data, *nameoff = NULL;
891 /* courtesy of jallison@whistle.com:
892 * For MacOS8.x support we need to create the
893 * .Parent file here if it doesn't exist. */
895 memset(&ad, 0, sizeof(ad));
896 if ( ad_open( vol->v_path, vol_noadouble(vol) |
897 ADFLAGS_HF|ADFLAGS_DIR, O_RDWR | O_CREAT,
901 } else if (ad_getoflags( &ad, ADFLAGS_HF ) & O_CREAT) {
902 slash = strrchr( vol->v_path, '/' );
908 ad_setentrylen( &ad, ADEID_NAME, strlen( slash ));
909 memcpy(ad_entry( &ad, ADEID_NAME ), slash,
910 ad_getentrylen( &ad, ADEID_NAME ));
911 ad_flush(&ad, ADFLAGS_HF);
914 if (( bitmap & ( (1<<VOLPBIT_BFREE)|(1<<VOLPBIT_BTOTAL) |
915 (1<<VOLPBIT_XBFREE)|(1<<VOLPBIT_XBTOTAL) |
916 (1<<VOLPBIT_BSIZE)) ) != 0 ) {
917 if ( getvolspace( vol, &bfree, &btotal, &xbfree, &xbtotal,
920 ad_close( &ad, ADFLAGS_HF );
922 return( AFPERR_PARAM );
927 while ( bitmap != 0 ) {
928 while (( bitmap & 1 ) == 0 ) {
937 if (0 == (vol->v_flags & AFPVOL_NOFILEID)) {
938 ashort = VOLPBIT_ATTR_FILEID;
941 /* check for read-only.
942 * NOTE: we don't actually set the read-only flag unless
943 * it's passed in that way as it's possible to mount
944 * a read-write filesystem under a read-only one. */
945 if ((vol->v_flags & AFPVOL_RO) ||
946 ((utime(vol->v_path, NULL) < 0) && (errno == EROFS)))
947 ashort |= VOLPBIT_ATTR_RO;
948 #ifdef WITH_CATSEARCH
949 ashort |= VOLPBIT_ATTR_CATSEARCH;
951 ashort = htons(ashort);
952 memcpy(data, &ashort, sizeof( ashort ));
953 data += sizeof( ashort );
957 ashort = htons( AFPVOLSIG_DEFAULT );
958 memcpy(data, &ashort, sizeof( ashort ));
959 data += sizeof( ashort );
963 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
964 aint = AD_DATE_FROM_UNIX(st->st_mtime);
965 memcpy(data, &aint, sizeof( aint ));
966 data += sizeof( aint );
970 if ( st->st_mtime > vol->v_time ) {
971 vol->v_time = st->st_mtime;
972 aint = AD_DATE_FROM_UNIX(st->st_mtime);
974 aint = AD_DATE_FROM_UNIX(vol->v_time);
976 memcpy(data, &aint, sizeof( aint ));
977 data += sizeof( aint );
981 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
982 aint = AD_DATE_START;
983 memcpy(data, &aint, sizeof( aint ));
984 data += sizeof( aint );
988 memcpy(data, &vol->v_vid, sizeof( vol->v_vid ));
989 data += sizeof( vol->v_vid );
993 bfree = htonl( bfree );
994 memcpy(data, &bfree, sizeof( bfree ));
995 data += sizeof( bfree );
998 case VOLPBIT_BTOTAL :
999 btotal = htonl( btotal );
1000 memcpy(data, &btotal, sizeof( btotal ));
1001 data += sizeof( btotal );
1004 #ifndef NO_LARGE_VOL_SUPPORT
1005 case VOLPBIT_XBFREE :
1006 xbfree = hton64( xbfree );
1007 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1008 bcopy(&xbfree, data, sizeof(xbfree));
1009 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1010 memcpy(data, &xbfree, sizeof( xbfree ));
1011 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1012 data += sizeof( xbfree );
1015 case VOLPBIT_XBTOTAL :
1016 xbtotal = hton64( xbtotal );
1017 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1018 bcopy(&xbtotal, data, sizeof(xbtotal));
1019 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1020 memcpy(data, &xbtotal, sizeof( xbtotal ));
1021 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1022 data += sizeof( xbfree );
1024 #endif /* ! NO_LARGE_VOL_SUPPORT */
1028 data += sizeof( u_int16_t );
1031 case VOLPBIT_BSIZE: /* block size */
1032 bsize = htonl(bsize);
1033 memcpy(data, &bsize, sizeof(bsize));
1034 data += sizeof(bsize);
1039 ad_close( &ad, ADFLAGS_HF );
1041 return( AFPERR_BITMAP );
1047 ashort = htons( data - buf );
1048 memcpy(nameoff, &ashort, sizeof( ashort ));
1049 aint = strlen( vol->v_name );
1051 memcpy(data, vol->v_name, aint );
1055 ad_close( &ad, ADFLAGS_HF );
1057 *buflen = data - buf;
1063 int afp_getsrvrparms(obj, ibuf, ibuflen, rbuf, rbuflen )
1066 int ibuflen, *rbuflen;
1079 for ( vcnt = 0, volume = volumes; volume; volume = volume->v_next ) {
1080 if ( stat( volume->v_path, &st ) < 0 ) {
1081 LOG(log_info, logtype_afpd, "afp_getsrvrparms: stat %s: %s",
1082 volume->v_path, strerror(errno) );
1083 continue; /* can't access directory */
1085 if (!S_ISDIR(st.st_mode)) {
1086 continue; /* not a dir */
1089 /* set password bit if there's a volume password */
1090 *data = (volume->v_password) ? AFPSRVR_PASSWD : 0;
1092 /* Apple 2 clients running ProDOS-8 expect one volume to have
1093 bit 0 of this byte set. They will not recognize anything
1094 on the server unless this is the case. I have not
1095 completely worked this out, but it's related to booting
1096 from the server. Support for that function is a ways
1097 off.. <shirsch@ibm.net> */
1098 *data++ |= (volume->v_flags & AFPVOL_A2VOL) ? AFPSRVR_CONFIGINFO : 0;
1099 len = strlen( volume->v_name );
1101 memcpy(data, volume->v_name, len );
1106 *rbuflen = data - rbuf;
1108 if ( gettimeofday( &tv, 0 ) < 0 ) {
1109 LOG(log_error, logtype_afpd, "afp_getsrvrparms: gettimeofday: %s", strerror(errno) );
1111 return AFPERR_PARAM;
1113 tv.tv_sec = AD_DATE_FROM_UNIX(tv.tv_sec);
1114 memcpy(data, &tv.tv_sec, sizeof( u_int32_t));
1115 data += sizeof( u_int32_t);
1120 int afp_openvol(obj, ibuf, ibuflen, rbuf, rbuflen )
1123 int ibuflen, *rbuflen;
1129 #endif /* CNID_DB */
1132 int len, ret, buflen;
1136 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1137 bitmap = ntohs( bitmap );
1138 ibuf += sizeof( bitmap );
1139 if (( bitmap & (1<<VOLPBIT_VID)) == 0 ) {
1140 ret = AFPERR_BITMAP;
1144 len = (unsigned char)*ibuf++;
1145 volname = obj->oldtmp;
1146 memcpy(volname, ibuf, len );
1147 *(volname + len) = '\0';
1149 if ((len + 1) & 1) /* pad to an even boundary */
1155 for ( volume = volumes; volume; volume = volume->v_next ) {
1156 if ( strcasecmp( volname, volume->v_name ) == 0 ) {
1161 if ( volume == NULL ) {
1166 /* check for a volume password */
1167 if (volume->v_password &&
1168 strncmp(ibuf, volume->v_password, VOLPASSLEN)) {
1169 ret = AFPERR_ACCESS;
1173 if (( volume->v_flags & AFPVOL_OPEN ) == 0 ) {
1174 if ((dir = dirnew(strlen(volume->v_name) + 1)) == NULL) {
1175 LOG(log_error, logtype_afpd, "afp_openvol: malloc: %s", strerror(errno) );
1179 dir->d_did = DIRDID_ROOT;
1180 dir->d_color = DIRTREE_COLOR_BLACK; /* root node is black */
1181 strcpy( dir->d_name, volume->v_name );
1182 volume->v_dir = volume->v_root = dir;
1183 volume->v_flags |= AFPVOL_OPEN;
1186 set_uidgid ( volume );
1187 #endif /* FORCE_UIDGID */
1189 if ( stat( volume->v_path, &st ) < 0 ) {
1194 buflen = *rbuflen - sizeof( bitmap );
1195 if (( ret = getvolparams( bitmap, volume, &st,
1196 rbuf + sizeof(bitmap), &buflen )) != AFP_OK ) {
1199 *rbuflen = buflen + sizeof( bitmap );
1200 bitmap = htons( bitmap );
1201 memcpy(rbuf, &bitmap, sizeof( bitmap ));
1203 if ( chdir( volume->v_path ) < 0 ) {
1207 curdir = volume->v_dir;
1210 if (volume->v_dbpath)
1211 volume->v_db = cnid_open (volume->v_dbpath, volume->v_umask);
1212 if (volume->v_db == NULL)
1213 volume->v_db = cnid_open (volume->v_path, volume->v_umask);
1214 #endif /* CNID_DB */
1218 * If you mount a volume twice, the second time the trash appears on
1219 * the desk-top. That's because the Mac remembers the DID for the
1220 * trash (even for volumes in different zones, on different servers).
1221 * Just so this works better, we prime the DID cache with the trash,
1222 * fixing the trash at DID 17.
1225 cname( volume, volume->v_dir, &p );
1226 #endif /* CNID_DB */
1235 int afp_closevol(obj, ibuf, ibuflen, rbuf, rbuflen )
1238 int ibuflen, *rbuflen;
1240 struct vol *vol, *ovol;
1245 memcpy(&vid, ibuf, sizeof( vid ));
1246 if (( vol = getvolbyvid( vid )) == NULL ) {
1247 return( AFPERR_PARAM );
1250 vol->v_flags &= ~AFPVOL_OPEN;
1251 for ( ovol = volumes; ovol; ovol = ovol->v_next ) {
1252 if ( ovol->v_flags & AFPVOL_OPEN ) {
1256 if ( ovol != NULL ) {
1257 /* Even if chdir fails, we can't say afp_closevol fails. */
1258 if ( chdir( ovol->v_path ) == 0 ) {
1259 curdir = ovol->v_dir;
1263 dirfree( vol->v_root );
1266 cnid_close(vol->v_db);
1268 #endif /* CNID_DB */
1272 struct vol *getvolbyvid(const u_int16_t vid )
1276 for ( vol = volumes; vol; vol = vol->v_next ) {
1277 if ( vid == vol->v_vid ) {
1281 if ( vol == NULL || ( vol->v_flags & AFPVOL_OPEN ) == 0 ) {
1287 #endif /* FORCE_UIDGID */
1292 struct extmap *getextmap(const char *path)
1297 if (( p = strrchr( path, '.' )) == NULL ) {
1298 return( defextmap );
1301 for ( em = extmap; em; em = em->em_next ) {
1302 if ( strdiacasecmp( em->em_ext, p ) == 0 ) {
1307 return( defextmap );
1313 struct extmap *getdefextmap(void)
1315 return( defextmap );
1318 void setvoltime(obj, vol )
1324 /* just looking at vol->v_time is broken seriously since updates
1325 * from other users afpd processes never are seen.
1326 * This is not the most elegant solution (a shared memory between
1327 * the afpd processes would come closer)
1330 if ( gettimeofday( &tv, 0 ) < 0 ) {
1331 LOG(log_error, logtype_afpd, "setvoltime: gettimeofday: %s", strerror(errno) );
1334 if( utime( vol->v_path, NULL ) < 0 ) {
1335 /* write of time failed ... probably a read only filesys,
1336 * where no other users can interfere, so there's no issue here
1340 /* a little granularity */
1341 if (vol->v_time < tv.tv_sec) {
1342 vol->v_time = tv.tv_sec;
1343 if (afp_version > 21 && obj->options.server_notif) {
1344 obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED);
1349 int afp_getvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1352 int ibuflen, *rbuflen;
1357 u_int16_t vid, bitmap;
1360 memcpy(&vid, ibuf, sizeof( vid ));
1361 ibuf += sizeof( vid );
1362 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1363 bitmap = ntohs( bitmap );
1365 if (( vol = getvolbyvid( vid )) == NULL ) {
1367 return( AFPERR_PARAM );
1370 if ( stat( vol->v_path, &st ) < 0 ) {
1372 return( AFPERR_PARAM );
1375 buflen = *rbuflen - sizeof( bitmap );
1376 if (( ret = getvolparams( bitmap, vol, &st,
1377 rbuf + sizeof( bitmap ), &buflen )) != AFP_OK ) {
1381 *rbuflen = buflen + sizeof( bitmap );
1382 bitmap = htons( bitmap );
1383 memcpy(rbuf, &bitmap, sizeof( bitmap ));
1387 int afp_setvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1390 int ibuflen, *rbuflen;
1394 u_int16_t vid, bitmap;
1400 memcpy(&vid, ibuf, sizeof( vid ));
1401 ibuf += sizeof( vid );
1402 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1403 bitmap = ntohs( bitmap );
1404 ibuf += sizeof(bitmap);
1406 if (( vol = getvolbyvid( vid )) == NULL ) {
1407 return( AFPERR_PARAM );
1410 if (vol->v_flags & AFPVOL_RO)
1411 return AFPERR_VLOCK;
1413 /* we can only set the backup date. */
1414 if (bitmap != VOLPBIT_BDATE)
1415 return AFPERR_BITMAP;
1417 memset(&ad, 0, sizeof(ad));
1418 if ( ad_open( vol->v_path, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR,
1421 return AFPERR_VLOCK;
1423 return AFPERR_ACCESS;
1426 memcpy(&aint, ibuf, sizeof(aint));
1427 ad_setdate(&ad, AD_DATE_BACKUP, aint);
1428 ad_flush(&ad, ADFLAGS_HF);
1429 ad_close(&ad, ADFLAGS_HF);
1434 int wincheck(const struct vol *vol, const char *path)
1438 if (!(vol->v_flags & AFPVOL_MSWINDOWS))
1441 /* empty paths are not allowed */
1442 if ((len = strlen(path)) == 0)
1445 /* leading or trailing whitespaces are not allowed, carriage returns
1446 * and probably other whitespace is okay, tabs are not allowed
1448 if ((path[0] == ' ') || (path[len-1] == ' '))
1451 /* certain characters are not allowed */
1452 if (strpbrk(path, MSWINDOWS_BADCHARS))
1455 /* everything else is okay */