2 * $Id: volume.c,v 1.29 2002-08-20 05:36:08 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 */
107 #define VOLOPT_FORCEUID 11 /* force uid for username x */
108 #define VOLOPT_FORCEGID 12 /* force gid for group x */
109 #define VOLOPT_MAX 12
110 #else /* normally, there are only 9 possible options */
111 #define VOLOPT_MAX 10
112 #endif /* FORCE_UIDGID */
114 #define VOLOPT_NUM (VOLOPT_MAX + 1)
117 #define VOLOPT_DEFAULT ":DEFAULT:"
118 #define VOLOPT_DEFAULT_LEN 9
124 static __inline__ void volfree(struct vol_option *options,
125 const struct vol_option *save)
130 for (i = 0; i < VOLOPT_MAX; i++) {
131 if (options[i].c_value && (options[i].c_value != save[i].c_value))
132 free(options[i].c_value);
135 for (i = 0; i < VOLOPT_MAX; i++) {
136 if (options[i].c_value)
137 free(options[i].c_value);
143 /* handle variable substitutions. here's what we understand:
144 * $c -> client ip/appletalk address
145 * $f -> full name (whatever's in the gecos field)
148 * $s -> server name (hostname if it doesn't exist)
149 * $u -> username (guest is usually nobody)
150 * $v -> volume name (ADEID_NAME or basename)
151 * $z -> zone (may not exist)
154 #define is_var(a, b) (strncmp((a), (b), 2) == 0)
155 static void volxlate(AFPObj *obj, char *dest, int destlen,
156 char *src, struct passwd *pwd, char *path)
161 strncpy(dest, src, destlen);
162 if ((p = strchr(src, '$')) == NULL) /* nothing to do */
165 /* first part of the path. just forward to the next variable. */
166 len = MIN(p - src, destlen);
172 while (p && destlen > 0) {
173 /* now figure out what the variable is */
175 if (is_var(p, "$c")) {
176 if (obj->proto == AFPPROTO_ASP) {
177 ASP asp = obj->handle;
179 len = sprintf(dest, "%u.%u", ntohs(asp->asp_sat.sat_addr.s_net),
180 asp->asp_sat.sat_addr.s_node);
184 } else if (obj->proto == AFPPROTO_DSI) {
185 DSI *dsi = obj->handle;
187 len = sprintf(dest, "%s:%u", inet_ntoa(dsi->client.sin_addr),
188 ntohs(dsi->client.sin_port));
192 } else if (is_var(p, "$f")) {
193 if ((q = strchr(pwd->pw_gecos, ',')))
196 } else if (is_var(p, "$g")) {
197 struct group *grp = getgrgid(pwd->pw_gid);
200 } else if (is_var(p, "$h")) {
201 q = obj->options.hostname;
202 } else if (is_var(p, "$s")) {
205 else if (obj->options.server) {
206 q = obj->options.server;
208 q = obj->options.hostname;
209 } else if (is_var(p, "$u")) {
211 } else if (is_var(p, "$v")) {
215 memset(&ad, 0, sizeof(ad));
216 if (ad_open(path, ADFLAGS_HF, O_RDONLY, 0, &ad) < 0)
219 if ((len = MIN(ad_getentrylen(&ad, ADEID_NAME), destlen)) > 0) {
220 memcpy(dest, ad_entry(&ad, ADEID_NAME), len);
221 ad_close(&ad, ADFLAGS_HF);
225 ad_close(&ad, ADFLAGS_HF);
226 no_volname: /* simple basename */
227 if ((q = strrchr(path, '/')) == NULL)
229 else if (*(q + 1) != '\0')
233 } else if (is_var(p, "$z")) {
235 } else if (is_var(p, "$$")) {
240 /* copy the stuff over. if we don't understand something that we
241 * should, just skip it over. */
243 len = MIN(p == q ? 2 : strlen(q), destlen);
244 strncpy(dest, q, len);
249 /* stuff up to next $ */
251 p = strchr(src, '$');
252 len = p ? MIN(p - src, destlen) : destlen;
254 strncpy(dest, src, len);
261 /* to make sure that val is valid, make sure to select an opt that
263 #define optionok(buf,opt,val) (strstr((buf),(opt)) && ((val)[1] != '\0'))
265 static __inline__ char *get_codepage_path(const char *path, const char *name)
271 page = (char *) malloc((len = strlen(path)) + strlen(name) + 2);
274 if (path[len - 1] != '/') /* add a / */
282 /* debug: show which codepage directory we are using */
283 LOG(log_debug, logtype_afpd, "using codepage directory: %s", page);
288 /* handle all the options. tmp can't be NULL. */
289 static void volset(struct vol_option *options, char *volname, int vlen,
290 const char *nlspath, const char *tmp)
294 val = strchr(tmp, ':');
295 if (optionok(tmp, "allow:", val)) {
296 if (options[VOLOPT_ALLOW].c_value)
297 free(options[VOLOPT_ALLOW].c_value);
298 options[VOLOPT_ALLOW].c_value = strdup(val + 1);
300 } else if (optionok(tmp, "deny:", val)) {
301 if (options[VOLOPT_DENY].c_value)
302 free(options[VOLOPT_DENY].c_value);
303 options[VOLOPT_DENY].c_value = strdup(val + 1);
305 } else if (optionok(tmp, "rwlist:", val)) {
306 if (options[VOLOPT_RWLIST].c_value)
307 free(options[VOLOPT_RWLIST].c_value);
308 options[VOLOPT_RWLIST].c_value = strdup(val + 1);
310 } else if (optionok(tmp, "rolist:", val)) {
311 if (options[VOLOPT_ROLIST].c_value)
312 free(options[VOLOPT_ROLIST].c_value);
313 options[VOLOPT_ROLIST].c_value = strdup(val + 1);
315 } else if (optionok(tmp, "codepage:", val)) {
316 if (options[VOLOPT_CODEPAGE].c_value)
317 free(options[VOLOPT_CODEPAGE].c_value);
318 options[VOLOPT_CODEPAGE].c_value = get_codepage_path(nlspath, val + 1);
320 } else if (optionok(tmp, "veto:", val)) {
321 if (options[VOLOPT_VETO].c_value)
322 free(options[VOLOPT_VETO].c_value);
323 options[VOLOPT_VETO].c_value = strdup(val + 1);
325 } else if (optionok(tmp, "casefold:", val)) {
326 if (strcasecmp(val + 1, "tolower") == 0)
327 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMLOWER;
328 else if (strcasecmp(val + 1, "toupper") == 0)
329 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMUPPER;
330 else if (strcasecmp(val + 1, "xlatelower") == 0)
331 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UUPPERMLOWER;
332 else if (strcasecmp(val + 1, "xlateupper") == 0)
333 options[VOLOPT_CASEFOLD].i_value = AFPVOL_ULOWERMUPPER;
335 } else if (optionok(tmp, "options:", val)) {
338 if ((p = strtok(val + 1, ",")) == NULL) /* nothing */
342 if (strcasecmp(p, "prodos") == 0)
343 options[VOLOPT_FLAGS].i_value |= AFPVOL_A2VOL;
344 else if (strcasecmp(p, "mswindows") == 0) {
345 options[VOLOPT_FLAGS].i_value |= AFPVOL_MSWINDOWS;
346 if (!options[VOLOPT_CODEPAGE].c_value)
347 options[VOLOPT_CODEPAGE].c_value =
348 get_codepage_path(nlspath, MSWINDOWS_CODEPAGE);
350 } else if (strcasecmp(p, "crlf") == 0)
351 options[VOLOPT_FLAGS].i_value |= AFPVOL_CRLF;
352 else if (strcasecmp(p, "noadouble") == 0)
353 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOADOUBLE;
354 else if (strcasecmp(p, "ro") == 0)
355 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
356 else if (strcasecmp(p, "nohex") == 0)
357 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOHEX;
358 else if (strcasecmp(p, "usedots") == 0)
359 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS;
360 else if (strcasecmp(p, "limitsize") == 0)
361 options[VOLOPT_FLAGS].i_value |= AFPVOL_LIMITSIZE;
362 /* support for either "dropbox" or "dropkludge" */
363 else if (strcasecmp(p, "dropbox") == 0)
364 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
365 else if (strcasecmp(p, "dropkludge") == 0)
366 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
367 else if (strcasecmp(p, "nofileid") == 0)
368 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOFILEID;
370 p = strtok(NULL, ",");
374 } else if (optionok(tmp, "dbpath:", val)) {
375 if (options[VOLOPT_DBPATH].c_value)
376 free(options[VOLOPT_DBPATH].c_value);
378 options[VOLOPT_DBPATH].c_value = strdup(val + 1);
380 } else if (optionok(tmp, "mapchars:",val)) {
381 if (options[VOLOPT_MAPCHARS].c_value)
382 free(options[VOLOPT_MAPCHARS].c_value);
383 options[VOLOPT_MAPCHARS].c_value = strdup(val + 1);
385 } else if (optionok(tmp, "password:", val)) {
386 if (options[VOLOPT_PASSWORD].c_value)
387 free(options[VOLOPT_PASSWORD].c_value);
388 options[VOLOPT_PASSWORD].c_value = strdup(val + 1);
392 /* this code allows forced uid/gid per volume settings */
393 } else if (optionok(tmp, "forceuid:", val)) {
394 if (options[VOLOPT_FORCEUID].c_value)
395 free(options[VOLOPT_FORCEUID].c_value);
396 options[VOLOPT_FORCEUID].c_value = strdup(val + 1);
397 } else if (optionok(tmp, "forcegid:", val)) {
398 if (options[VOLOPT_FORCEGID].c_value)
399 free(options[VOLOPT_FORCEGID].c_value);
400 options[VOLOPT_FORCEGID].c_value = strdup(val + 1);
402 #endif /* FORCE_UIDGID */
405 /* ignore unknown options */
406 LOG(log_debug, logtype_afpd, "ignoring unknown volume option: %s", tmp);
409 /* we'll assume it's a volume name. */
410 strncpy(volname, tmp, vlen);
414 static int creatvol(const char *path, char *name, struct vol_option *options)
419 if ( name == NULL || *name == '\0' ) {
420 if ((name = strrchr( path, '/' )) == NULL) {
421 return -1; /* Obviously not a fully qualified path */
424 /* if you wish to share /, you need to specify a name. */
429 for ( volume = volumes; volume; volume = volume->v_next ) {
430 if ( strcasecmp( volume->v_name, name ) == 0 ) {
431 return -1; /* Won't be able to access it, anyway... */
435 vlen = strlen( name );
436 if ( vlen > AFPVOL_NAMELEN ) {
437 vlen = AFPVOL_NAMELEN;
438 name[AFPVOL_NAMELEN] = '\0';
442 (struct vol *)calloc(1, sizeof( struct vol ))) == NULL ) {
443 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
446 if (( volume->v_name =
447 (char *)malloc( vlen + 1 )) == NULL ) {
448 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
452 if (( volume->v_path =
453 (char *)malloc( strlen( path ) + 1 )) == NULL ) {
454 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
455 free(volume->v_name);
460 strcpy( volume->v_name, name);
461 strcpy( volume->v_path, path );
465 #endif /* __svr4__ */
466 volume->v_vid = lastvid++;
467 volume->v_lastdid = 17;
471 /* should we casefold? */
472 volume->v_casefold = options[VOLOPT_CASEFOLD].i_value;
474 /* shift in some flags */
475 volume->v_flags = options[VOLOPT_FLAGS].i_value;
477 /* read in the code pages */
478 if (options[VOLOPT_CODEPAGE].c_value)
479 codepage_read(volume, options[VOLOPT_CODEPAGE].c_value);
481 if (options[VOLOPT_PASSWORD].c_value)
482 volume->v_password = strdup(options[VOLOPT_PASSWORD].c_value);
484 if (options[VOLOPT_VETO].c_value)
485 volume->v_veto = strdup(options[VOLOPT_VETO].c_value);
488 if (options[VOLOPT_DBPATH].c_value)
489 volume->v_dbpath = strdup(options[VOLOPT_DBPATH].c_value);
494 if (options[VOLOPT_FORCEUID].c_value) {
495 volume->v_forceuid = strdup(options[VOLOPT_FORCEUID].c_value);
497 volume->v_forceuid = NULL; /* set as null so as to return 0 later on */
500 if (options[VOLOPT_FORCEGID].c_value) {
501 volume->v_forcegid = strdup(options[VOLOPT_FORCEGID].c_value);
503 volume->v_forcegid = NULL; /* set as null so as to return 0 later on */
506 #endif /* FORCE_UIDGID */
510 volume->v_next = volumes;
515 static char *myfgets( buf, size, fp )
524 while ((( c = getc( fp )) != EOF ) && ( size > 0 )) {
525 if ( c == '\n' || c == '\r' ) {
543 /* check access list. this function wants something of the following
545 * @group,name,name2,@group2,name3
547 * a NULL argument allows everybody to have access.
548 * we return three things:
550 * 0: list exists, but name isn't in it
553 static int accessvol(args, name)
557 char buf[MAXPATHLEN + 1], *p;
563 strncpy(buf, args, sizeof(buf));
564 if ((p = strtok(buf, ",")) == NULL) /* nothing, return okay */
568 if (*p == '@') { /* it's a group */
569 if ((gr = getgrnam(p + 1)) && gmem(gr->gr_gid))
571 } else if (strcmp(p, name) == 0) /* it's a user name */
573 p = strtok(NULL, ",");
579 static void setextmap( ext, type, creator, user)
580 char *ext, *type, *creator;
585 for ( em = extmap; em; em = em->em_next ) {
586 if ( strdiacasecmp( em->em_ext, ext ) == 0 ) {
593 (struct extmap *)malloc( sizeof( struct extmap ))) == NULL ) {
594 LOG(log_error, logtype_afpd, "setextmap: malloc: %s", strerror(errno) );
597 em->em_next = extmap;
599 } else if ( !user ) {
603 strcpy( em->em_ext, ext );
605 if ( *type == '\0' ) {
606 memcpy(em->em_type, "????", sizeof( em->em_type ));
608 memcpy(em->em_type, type, sizeof( em->em_type ));
610 if ( *creator == '\0' ) {
611 memcpy(em->em_creator, "UNIX", sizeof( em->em_creator ));
613 memcpy(em->em_creator, creator, sizeof( em->em_creator ));
616 if ( strcmp( ext, "." ) == 0 ) {
622 * Read a volume configuration file and add the volumes contained within to
623 * the global volume list. If p2 is non-NULL, the file that is opened is
626 * Lines that begin with # and blank lines are ignored.
627 * Volume lines are of the form:
628 * <unix path> [<volume name>] [allow:<user>,<@group>,...] \
629 * [codepage:<file>] [casefold:<num>]
630 * <extension> TYPE [CREATOR]
632 static int readvolfile(obj, p1, p2, user, pwent)
636 struct passwd *pwent;
639 char path[ MAXPATHLEN + 1], tmp[ MAXPATHLEN + 1],
640 volname[ AFPVOL_NAMELEN + 1 ], buf[ BUFSIZ ],
641 type[ 5 ], creator[ 5 ];
644 struct vol_option options[VOLOPT_NUM], save_options[VOLOPT_NUM];
656 if (( fp = fopen( path, "r" )) == NULL ) {
660 memset(save_options, 0, sizeof(save_options));
661 while ( myfgets( buf, sizeof( buf ), fp ) != NULL ) {
662 initline( strlen( buf ), buf );
663 parseline( sizeof( path ) - 1, path );
670 /* change the default options for this file */
671 if (strncmp(path, VOLOPT_DEFAULT, VOLOPT_DEFAULT_LEN) == 0) {
673 for (i = 0; i < VOLOPT_NUM; i++) {
674 if (parseline( sizeof( path ) - VOLOPT_DEFAULT_LEN - 1,
675 path + VOLOPT_DEFAULT_LEN) < 0)
677 volset(save_options, tmp, sizeof(tmp) - 1,
678 obj->options.nlspath, path + VOLOPT_DEFAULT_LEN);
684 if (( p = strchr( path, '/' )) != NULL ) {
692 if ( u == NULL || *u == '\0' || ( pw = getpwnam( u )) == NULL ) {
695 strcpy( tmp, pw->pw_dir );
696 if ( p != NULL && *p != '\0' ) {
703 /* send path through variable substitution */
704 if (*path != '~') /* need to copy path to tmp */
707 pwent = getpwnam(obj->username);
708 volxlate(obj, path, sizeof(path) - 1, tmp, pwent, NULL);
710 /* this is sort of braindead. basically, i want to be
711 * able to specify things in any order, but i don't want to
712 * re-write everything.
714 * currently we have 11 options:
722 * options:prodos,crlf,noadouble,ro
725 * namemask:x,y,!z (not implemented yet)
727 memcpy(options, save_options, sizeof(options));
730 /* read in up to 11 possible options */
731 for (i = 0; i < VOLOPT_NUM; i++) {
732 if (parseline( sizeof( tmp ) - 1, tmp ) < 0)
735 volset(options, volname, sizeof(volname) - 1,
736 obj->options.nlspath, tmp);
739 /* check allow/deny lists:
740 allow -> either no list (-1), or in list (1)
741 deny -> either no list (-1), or not in list (0) */
742 if (accessvol(options[VOLOPT_ALLOW].c_value, obj->username) &&
743 (accessvol(options[VOLOPT_DENY].c_value, obj->username) < 1)) {
745 /* handle read-only behaviour. semantics:
746 * 1) neither the rolist nor the rwlist exist -> rw
747 * 2) rolist exists -> ro if user is in it.
748 * 3) rwlist exists -> ro unless user is in it. */
749 if (((options[VOLOPT_FLAGS].i_value & AFPVOL_RO) == 0) &&
750 ((accessvol(options[VOLOPT_ROLIST].c_value,
751 obj->username) == 1) ||
752 !accessvol(options[VOLOPT_RWLIST].c_value,
754 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
756 /* do variable substitution */
757 volxlate(obj, tmp, sizeof(tmp) - 1, volname, pwent, path);
758 creatvol(path, tmp, options);
760 volfree(options, save_options);
764 parseline( sizeof( type ) - 1, type );
765 parseline( sizeof( creator ) - 1, creator );
766 setextmap( path, type, creator, user);
773 volfree(save_options, NULL);
774 if ( fclose( fp ) != 0 ) {
775 LOG(log_error, logtype_afpd, "readvolfile: fclose: %s", strerror(errno) );
781 static void load_volumes(AFPObj *obj)
783 struct passwd *pwent = getpwnam(obj->username);
785 if ( (obj->options.flags & OPTION_USERVOLFIRST) == 0 ) {
786 readvolfile(obj, obj->options.systemvol, NULL, 0, pwent);
789 if ((*obj->username == '\0') || (obj->options.flags & OPTION_NOUSERVOL)) {
790 readvolfile(obj, obj->options.defaultvol, NULL, 1, pwent);
793 * Read user's AppleVolumes or .AppleVolumes file
794 * If neither are readable, read the default volumes file. if
795 * that doesn't work, create a user share.
797 if ( readvolfile(obj, pwent->pw_dir, "AppleVolumes", 1, pwent) < 0 &&
798 readvolfile(obj, pwent->pw_dir, ".AppleVolumes", 1, pwent) < 0 &&
799 readvolfile(obj, pwent->pw_dir, "applevolumes", 1, pwent) < 0 &&
800 readvolfile(obj, pwent->pw_dir, ".applevolumes", 1, pwent) < 0 &&
801 obj->options.defaultvol != NULL ) {
802 if (readvolfile(obj, obj->options.defaultvol, NULL, 1, pwent) < 0)
803 creatvol(pwent->pw_dir, NULL, NULL);
806 if ( obj->options.flags & OPTION_USERVOLFIRST ) {
807 readvolfile(obj, obj->options.systemvol, NULL, 0, pwent );
811 static int getvolspace( vol, bfree, btotal, xbfree, xbtotal, bsize )
813 u_int32_t *bfree, *btotal, *bsize;
814 VolSpace *xbfree, *xbtotal;
818 #ifndef NO_QUOTA_SUPPORT
819 VolSpace qfree, qtotal;
820 #endif /* ! NO_QUOTA_SUPPORT */
822 spaceflag = AFPVOL_GVSMASK & vol->v_flags;
823 /* report up to 2GB if afp version is < 2.2 (4GB if not) */
824 maxsize = (vol->v_flags & AFPVOL_A2VOL) ? 0x01fffe00 :
825 (((afp_version < 22) || (vol->v_flags & AFPVOL_LIMITSIZE))
826 ? 0x7fffffffL : 0xffffffffL);
829 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_AFSGVS ) {
830 if ( afs_getvolspace( vol, xbfree, xbtotal, bsize ) == AFP_OK ) {
831 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_AFSGVS;
832 goto getvolspace_done;
837 if (( rc = ustatfs_getvolspace( vol, xbfree, xbtotal,
838 bsize)) != AFP_OK ) {
842 #define min(a,b) ((a)<(b)?(a):(b))
843 #ifndef NO_QUOTA_SUPPORT
844 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_UQUOTA ) {
845 if ( uquota_getvolspace( vol, &qfree, &qtotal, *bsize ) == AFP_OK ) {
846 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_UQUOTA;
847 *xbfree = min(*xbfree, qfree);
848 *xbtotal = min( *xbtotal, qtotal);
849 goto getvolspace_done;
852 #endif /* ! NO_QUOTA_SUPPORT */
853 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_USTATFS;
856 *bfree = min( *xbfree, maxsize);
857 *btotal = min( *xbtotal, maxsize);
861 static int getvolparams( bitmap, vol, st, buf, buflen )
869 int bit = 0, isad = 1;
872 u_int32_t bfree, btotal, bsize;
873 VolSpace xbfree, xbtotal; /* extended bytes */
874 char *data, *nameoff = NULL;
877 /* courtesy of jallison@whistle.com:
878 * For MacOS8.x support we need to create the
879 * .Parent file here if it doesn't exist. */
881 memset(&ad, 0, sizeof(ad));
882 if ( ad_open( vol->v_path, vol_noadouble(vol) |
883 ADFLAGS_HF|ADFLAGS_DIR, O_RDWR | O_CREAT,
887 } else if (ad_getoflags( &ad, ADFLAGS_HF ) & O_CREAT) {
888 slash = strrchr( vol->v_path, '/' );
894 ad_setentrylen( &ad, ADEID_NAME, strlen( slash ));
895 memcpy(ad_entry( &ad, ADEID_NAME ), slash,
896 ad_getentrylen( &ad, ADEID_NAME ));
897 ad_flush(&ad, ADFLAGS_HF);
900 if (( bitmap & ( (1<<VOLPBIT_BFREE)|(1<<VOLPBIT_BTOTAL) |
901 (1<<VOLPBIT_XBFREE)|(1<<VOLPBIT_XBTOTAL) |
902 (1<<VOLPBIT_BSIZE)) ) != 0 ) {
903 if ( getvolspace( vol, &bfree, &btotal, &xbfree, &xbtotal,
906 ad_close( &ad, ADFLAGS_HF );
908 return( AFPERR_PARAM );
913 while ( bitmap != 0 ) {
914 while (( bitmap & 1 ) == 0 ) {
923 if (0 == (vol->v_flags & AFPVOL_NOFILEID)) {
924 ashort = VOLPBIT_ATTR_FILEID;
927 /* check for read-only.
928 * NOTE: we don't actually set the read-only flag unless
929 * it's passed in that way as it's possible to mount
930 * a read-write filesystem under a read-only one. */
931 if ((vol->v_flags & AFPVOL_RO) ||
932 ((utime(vol->v_path, NULL) < 0) && (errno == EROFS)))
933 ashort |= VOLPBIT_ATTR_RO;
934 ashort = htons(ashort);
935 memcpy(data, &ashort, sizeof( ashort ));
936 data += sizeof( ashort );
940 ashort = htons( AFPVOLSIG_DEFAULT );
941 memcpy(data, &ashort, sizeof( ashort ));
942 data += sizeof( ashort );
946 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
947 aint = AD_DATE_FROM_UNIX(st->st_mtime);
948 memcpy(data, &aint, sizeof( aint ));
949 data += sizeof( aint );
953 if ( st->st_mtime > vol->v_time ) {
954 vol->v_time = st->st_mtime;
955 aint = AD_DATE_FROM_UNIX(st->st_mtime);
957 aint = AD_DATE_FROM_UNIX(vol->v_time);
959 memcpy(data, &aint, sizeof( aint ));
960 data += sizeof( aint );
964 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
965 aint = AD_DATE_START;
966 memcpy(data, &aint, sizeof( aint ));
967 data += sizeof( aint );
971 memcpy(data, &vol->v_vid, sizeof( vol->v_vid ));
972 data += sizeof( vol->v_vid );
976 bfree = htonl( bfree );
977 memcpy(data, &bfree, sizeof( bfree ));
978 data += sizeof( bfree );
981 case VOLPBIT_BTOTAL :
982 btotal = htonl( btotal );
983 memcpy(data, &btotal, sizeof( btotal ));
984 data += sizeof( btotal );
987 #ifndef NO_LARGE_VOL_SUPPORT
988 case VOLPBIT_XBFREE :
989 xbfree = hton64( xbfree );
990 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
991 bcopy(&xbfree, data, sizeof(xbfree));
992 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
993 memcpy(data, &xbfree, sizeof( xbfree ));
994 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
995 data += sizeof( xbfree );
998 case VOLPBIT_XBTOTAL :
999 xbtotal = hton64( xbtotal );
1000 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1001 bcopy(&xbtotal, data, sizeof(xbtotal));
1002 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1003 memcpy(data, &xbtotal, sizeof( xbtotal ));
1004 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1005 data += sizeof( xbfree );
1007 #endif /* ! NO_LARGE_VOL_SUPPORT */
1011 data += sizeof( u_int16_t );
1014 case VOLPBIT_BSIZE: /* block size */
1015 bsize = htonl(bsize);
1016 memcpy(data, &bsize, sizeof(bsize));
1017 data += sizeof(bsize);
1022 ad_close( &ad, ADFLAGS_HF );
1024 return( AFPERR_BITMAP );
1030 ashort = htons( data - buf );
1031 memcpy(nameoff, &ashort, sizeof( ashort ));
1032 aint = strlen( vol->v_name );
1034 memcpy(data, vol->v_name, aint );
1038 ad_close( &ad, ADFLAGS_HF );
1040 *buflen = data - buf;
1046 int afp_getsrvrparms(obj, ibuf, ibuflen, rbuf, rbuflen )
1049 int ibuflen, *rbuflen;
1062 for ( vcnt = 0, volume = volumes; volume; volume = volume->v_next ) {
1063 if ( stat( volume->v_path, &st ) < 0 ) {
1064 LOG(log_info, logtype_afpd, "afp_getsrvrparms: stat %s: %s",
1065 volume->v_path, strerror(errno) );
1066 continue; /* can't access directory */
1068 if (!S_ISDIR(st.st_mode)) {
1069 continue; /* not a dir */
1072 /* set password bit if there's a volume password */
1073 *data = (volume->v_password) ? AFPSRVR_PASSWD : 0;
1075 /* Apple 2 clients running ProDOS-8 expect one volume to have
1076 bit 0 of this byte set. They will not recognize anything
1077 on the server unless this is the case. I have not
1078 completely worked this out, but it's related to booting
1079 from the server. Support for that function is a ways
1080 off.. <shirsch@ibm.net> */
1081 *data++ |= (volume->v_flags & AFPVOL_A2VOL) ? AFPSRVR_CONFIGINFO : 0;
1082 len = strlen( volume->v_name );
1084 memcpy(data, volume->v_name, len );
1089 *rbuflen = data - rbuf;
1091 if ( gettimeofday( &tv, 0 ) < 0 ) {
1092 LOG(log_error, logtype_afpd, "afp_getsrvrparms: gettimeofday: %s", strerror(errno) );
1094 return AFPERR_PARAM;
1096 tv.tv_sec = AD_DATE_FROM_UNIX(tv.tv_sec);
1097 memcpy(data, &tv.tv_sec, sizeof( u_int32_t));
1098 data += sizeof( u_int32_t);
1103 int afp_openvol(obj, ibuf, ibuflen, rbuf, rbuflen )
1106 int ibuflen, *rbuflen;
1112 #endif /* CNID_DB */
1115 int len, ret, buflen;
1119 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1120 bitmap = ntohs( bitmap );
1121 ibuf += sizeof( bitmap );
1122 if (( bitmap & (1<<VOLPBIT_VID)) == 0 ) {
1123 ret = AFPERR_BITMAP;
1127 len = (unsigned char)*ibuf++;
1128 volname = obj->oldtmp;
1129 memcpy(volname, ibuf, len );
1130 *(volname + len) = '\0';
1132 if ((len + 1) & 1) /* pad to an even boundary */
1138 for ( volume = volumes; volume; volume = volume->v_next ) {
1139 if ( strcasecmp( volname, volume->v_name ) == 0 ) {
1144 if ( volume == NULL ) {
1149 /* check for a volume password */
1150 if (volume->v_password &&
1151 strncmp(ibuf, volume->v_password, VOLPASSLEN)) {
1152 ret = AFPERR_ACCESS;
1156 if (( volume->v_flags & AFPVOL_OPEN ) == 0 ) {
1157 if ((dir = dirnew(strlen(volume->v_name) + 1)) == NULL) {
1158 LOG(log_error, logtype_afpd, "afp_openvol: malloc: %s", strerror(errno) );
1162 dir->d_did = DIRDID_ROOT;
1163 dir->d_color = DIRTREE_COLOR_BLACK; /* root node is black */
1164 strcpy( dir->d_name, volume->v_name );
1165 volume->v_dir = volume->v_root = dir;
1166 volume->v_flags |= AFPVOL_OPEN;
1169 if ( stat( volume->v_path, &st ) < 0 ) {
1174 buflen = *rbuflen - sizeof( bitmap );
1175 if (( ret = getvolparams( bitmap, volume, &st,
1176 rbuf + sizeof(bitmap), &buflen )) != AFP_OK ) {
1179 *rbuflen = buflen + sizeof( bitmap );
1180 bitmap = htons( bitmap );
1181 memcpy(rbuf, &bitmap, sizeof( bitmap ));
1183 if ( chdir( volume->v_path ) < 0 ) {
1187 curdir = volume->v_dir;
1190 if (volume->v_dbpath)
1191 volume->v_db = cnid_open (volume->v_dbpath);
1192 if (volume->v_db == NULL)
1193 volume->v_db = cnid_open (volume->v_path);
1194 #endif /* CNID_DB */
1198 * If you mount a volume twice, the second time the trash appears on
1199 * the desk-top. That's because the Mac remembers the DID for the
1200 * trash (even for volumes in different zones, on different servers).
1201 * Just so this works better, we prime the DID cache with the trash,
1202 * fixing the trash at DID 17.
1205 cname( volume, volume->v_dir, &p );
1206 #endif /* CNID_DB */
1215 int afp_closevol(obj, ibuf, ibuflen, rbuf, rbuflen )
1218 int ibuflen, *rbuflen;
1220 struct vol *vol, *ovol;
1225 memcpy(&vid, ibuf, sizeof( vid ));
1226 if (( vol = getvolbyvid( vid )) == NULL ) {
1227 return( AFPERR_PARAM );
1230 vol->v_flags &= ~AFPVOL_OPEN;
1231 for ( ovol = volumes; ovol; ovol = ovol->v_next ) {
1232 if ( ovol->v_flags & AFPVOL_OPEN ) {
1236 if ( ovol != NULL ) {
1237 /* Even if chdir fails, we can't say afp_closevol fails. */
1238 if ( chdir( ovol->v_path ) == 0 ) {
1239 curdir = ovol->v_dir;
1243 dirfree( vol->v_root );
1246 cnid_close(vol->v_db);
1248 #endif /* CNID_DB */
1252 struct vol *getvolbyvid(const u_int16_t vid )
1256 for ( vol = volumes; vol; vol = vol->v_next ) {
1257 if ( vid == vol->v_vid ) {
1261 if ( vol == NULL || ( vol->v_flags & AFPVOL_OPEN ) == 0 ) {
1268 struct extmap *getextmap(const char *path)
1273 if (( p = strrchr( path, '.' )) == NULL ) {
1274 return( defextmap );
1277 for ( em = extmap; em; em = em->em_next ) {
1278 if ( strdiacasecmp( em->em_ext, p ) == 0 ) {
1283 return( defextmap );
1289 void setvoltime(obj, vol )
1295 /* just looking at vol->v_time is broken seriously since updates
1296 * from other users afpd processes never are seen.
1297 * This is not the most elegant solution (a shared memory between
1298 * the afpd processes would come closer)
1301 if ( gettimeofday( &tv, 0 ) < 0 ) {
1302 LOG(log_error, logtype_afpd, "setvoltime: gettimeofday: %s", strerror(errno) );
1305 if( utime( vol->v_path, NULL ) < 0 ) {
1306 /* write of time failed ... probably a read only filesys,
1307 * where no other users can interfere, so there's no issue here
1311 /* a little granularity */
1312 if (vol->v_time < tv.tv_sec) {
1313 vol->v_time = tv.tv_sec;
1314 if (afp_version > 21 && obj->options.server_notif) {
1315 obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED);
1320 int afp_getvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1323 int ibuflen, *rbuflen;
1328 u_int16_t vid, bitmap;
1331 memcpy(&vid, ibuf, sizeof( vid ));
1332 ibuf += sizeof( vid );
1333 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1334 bitmap = ntohs( bitmap );
1336 if (( vol = getvolbyvid( vid )) == NULL ) {
1338 return( AFPERR_PARAM );
1341 if ( stat( vol->v_path, &st ) < 0 ) {
1343 return( AFPERR_PARAM );
1346 buflen = *rbuflen - sizeof( bitmap );
1347 if (( ret = getvolparams( bitmap, vol, &st,
1348 rbuf + sizeof( bitmap ), &buflen )) != AFP_OK ) {
1352 *rbuflen = buflen + sizeof( bitmap );
1353 bitmap = htons( bitmap );
1354 memcpy(rbuf, &bitmap, sizeof( bitmap ));
1358 int afp_setvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1361 int ibuflen, *rbuflen;
1365 u_int16_t vid, bitmap;
1371 memcpy(&vid, ibuf, sizeof( vid ));
1372 ibuf += sizeof( vid );
1373 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1374 bitmap = ntohs( bitmap );
1375 ibuf += sizeof(bitmap);
1377 if (( vol = getvolbyvid( vid )) == NULL ) {
1378 return( AFPERR_PARAM );
1381 if (vol->v_flags & AFPVOL_RO)
1382 return AFPERR_VLOCK;
1384 /* we can only set the backup date. */
1385 if (bitmap != VOLPBIT_BDATE)
1386 return AFPERR_BITMAP;
1388 memset(&ad, 0, sizeof(ad));
1389 if ( ad_open( vol->v_path, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR,
1392 return AFPERR_VLOCK;
1394 return AFPERR_ACCESS;
1397 memcpy(&aint, ibuf, sizeof(aint));
1398 ad_setdate(&ad, AD_DATE_BACKUP, aint);
1399 ad_flush(&ad, ADFLAGS_HF);
1400 ad_close(&ad, ADFLAGS_HF);
1405 int wincheck(struct vol *vol, const char *path)
1409 if (!(vol->v_flags & AFPVOL_MSWINDOWS))
1412 /* empty paths are not allowed */
1413 if ((len = strlen(path)) == 0)
1416 /* leading or trailing whitespaces are not allowed, carriage returns
1417 * and probably other whitespace is okay, tabs are not allowed
1419 if ((path[0] == ' ') || (path[len-1] == ' '))
1422 /* certain characters are not allowed */
1423 if (strpbrk(path, MSWINDOWS_BADCHARS))
1426 /* everything else is okay */