2 * $Id: volume.c,v 1.26 2002-03-24 17:45:29 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 ) {
922 if (0 == (vol->v_flags & AFPVOL_NOFILEID)) {
923 ashort = VOLPBIT_ATTR_FILEID;
928 /* check for read-only.
929 * NOTE: we don't actually set the read-only flag unless
930 * it's passed in that way as it's possible to mount
931 * a read-write filesystem under a read-only one. */
932 if ((vol->v_flags & AFPVOL_RO) ||
933 ((utime(vol->v_path, NULL) < 0) && (errno == EROFS)))
934 ashort |= VOLPBIT_ATTR_RO;
935 ashort = htons(ashort);
936 memcpy(data, &ashort, sizeof( ashort ));
937 data += sizeof( ashort );
941 ashort = htons( AFPVOLSIG_DEFAULT );
942 memcpy(data, &ashort, sizeof( ashort ));
943 data += sizeof( ashort );
947 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
948 aint = AD_DATE_FROM_UNIX(st->st_mtime);
949 memcpy(data, &aint, sizeof( aint ));
950 data += sizeof( aint );
954 if ( st->st_mtime > vol->v_time ) {
955 vol->v_time = st->st_mtime;
956 aint = AD_DATE_FROM_UNIX(st->st_mtime);
958 aint = AD_DATE_FROM_UNIX(vol->v_time);
960 memcpy(data, &aint, sizeof( aint ));
961 data += sizeof( aint );
965 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
966 aint = AD_DATE_START;
967 memcpy(data, &aint, sizeof( aint ));
968 data += sizeof( aint );
972 memcpy(data, &vol->v_vid, sizeof( vol->v_vid ));
973 data += sizeof( vol->v_vid );
977 bfree = htonl( bfree );
978 memcpy(data, &bfree, sizeof( bfree ));
979 data += sizeof( bfree );
982 case VOLPBIT_BTOTAL :
983 btotal = htonl( btotal );
984 memcpy(data, &btotal, sizeof( btotal ));
985 data += sizeof( btotal );
988 #ifndef NO_LARGE_VOL_SUPPORT
989 case VOLPBIT_XBFREE :
990 xbfree = hton64( xbfree );
991 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
992 bcopy(&xbfree, data, sizeof(xbfree));
993 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
994 memcpy(data, &xbfree, sizeof( xbfree ));
995 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
996 data += sizeof( xbfree );
999 case VOLPBIT_XBTOTAL :
1000 xbtotal = hton64( xbtotal );
1001 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1002 bcopy(&xbtotal, data, sizeof(xbtotal));
1003 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1004 memcpy(data, &xbtotal, sizeof( xbtotal ));
1005 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1006 data += sizeof( xbfree );
1008 #endif /* ! NO_LARGE_VOL_SUPPORT */
1012 data += sizeof( u_int16_t );
1015 case VOLPBIT_BSIZE: /* block size */
1016 bsize = htonl(bsize);
1017 memcpy(data, &bsize, sizeof(bsize));
1018 data += sizeof(bsize);
1023 ad_close( &ad, ADFLAGS_HF );
1025 return( AFPERR_BITMAP );
1031 ashort = htons( data - buf );
1032 memcpy(nameoff, &ashort, sizeof( ashort ));
1033 aint = strlen( vol->v_name );
1035 memcpy(data, vol->v_name, aint );
1039 ad_close( &ad, ADFLAGS_HF );
1041 *buflen = data - buf;
1047 int afp_getsrvrparms(obj, ibuf, ibuflen, rbuf, rbuflen )
1050 int ibuflen, *rbuflen;
1063 for ( vcnt = 0, volume = volumes; volume; volume = volume->v_next ) {
1064 if ( stat( volume->v_path, &st ) < 0 ) {
1065 LOG(log_info, logtype_afpd, "afp_getsrvrparms: stat %s: %s",
1066 volume->v_path, strerror(errno) );
1067 continue; /* can't access directory */
1069 if (!S_ISDIR(st.st_mode)) {
1070 continue; /* not a dir */
1073 /* set password bit if there's a volume password */
1074 *data = (volume->v_password) ? AFPSRVR_PASSWD : 0;
1076 /* Apple 2 clients running ProDOS-8 expect one volume to have
1077 bit 0 of this byte set. They will not recognize anything
1078 on the server unless this is the case. I have not
1079 completely worked this out, but it's related to booting
1080 from the server. Support for that function is a ways
1081 off.. <shirsch@ibm.net> */
1082 *data++ |= (volume->v_flags & AFPVOL_A2VOL) ? AFPSRVR_CONFIGINFO : 0;
1083 len = strlen( volume->v_name );
1085 memcpy(data, volume->v_name, len );
1090 *rbuflen = data - rbuf;
1092 if ( gettimeofday( &tv, 0 ) < 0 ) {
1093 LOG(log_error, logtype_afpd, "afp_getsrvrparms: gettimeofday: %s", strerror(errno) );
1095 return AFPERR_PARAM;
1097 tv.tv_sec = AD_DATE_FROM_UNIX(tv.tv_sec);
1098 memcpy(data, &tv.tv_sec, sizeof( u_int32_t));
1099 data += sizeof( u_int32_t);
1104 int afp_openvol(obj, ibuf, ibuflen, rbuf, rbuflen )
1107 int ibuflen, *rbuflen;
1113 #endif /* CNID_DB */
1116 int len, ret, buflen;
1120 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1121 bitmap = ntohs( bitmap );
1122 ibuf += sizeof( bitmap );
1123 if (( bitmap & (1<<VOLPBIT_VID)) == 0 ) {
1124 ret = AFPERR_BITMAP;
1128 len = (unsigned char)*ibuf++;
1129 volname = obj->oldtmp;
1130 memcpy(volname, ibuf, len );
1131 *(volname + len) = '\0';
1133 if ((len + 1) & 1) /* pad to an even boundary */
1139 for ( volume = volumes; volume; volume = volume->v_next ) {
1140 if ( strcasecmp( volname, volume->v_name ) == 0 ) {
1145 if ( volume == NULL ) {
1150 /* check for a volume password */
1151 if (volume->v_password &&
1152 strncmp(ibuf, volume->v_password, VOLPASSLEN)) {
1153 ret = AFPERR_ACCESS;
1157 if (( volume->v_flags & AFPVOL_OPEN ) == 0 ) {
1158 if ((dir = dirnew(strlen(volume->v_name) + 1)) == NULL) {
1159 LOG(log_error, logtype_afpd, "afp_openvol: malloc: %s", strerror(errno) );
1163 dir->d_did = DIRDID_ROOT;
1164 dir->d_color = DIRTREE_COLOR_BLACK; /* root node is black */
1165 strcpy( dir->d_name, volume->v_name );
1166 volume->v_dir = volume->v_root = dir;
1167 volume->v_flags |= AFPVOL_OPEN;
1170 if ( stat( volume->v_path, &st ) < 0 ) {
1175 buflen = *rbuflen - sizeof( bitmap );
1176 if (( ret = getvolparams( bitmap, volume, &st,
1177 rbuf + sizeof(bitmap), &buflen )) != AFP_OK ) {
1180 *rbuflen = buflen + sizeof( bitmap );
1181 bitmap = htons( bitmap );
1182 memcpy(rbuf, &bitmap, sizeof( bitmap ));
1184 curdir = volume->v_dir;
1185 if ( chdir( volume->v_path ) < 0 ) {
1191 if (volume->v_dbpath)
1192 volume->v_db = cnid_open (volume->v_dbpath);
1193 if (volume->v_db == 0)
1194 volume->v_db = cnid_open (volume->v_path);
1195 #endif /* CNID_DB */
1199 * If you mount a volume twice, the second time the trash appears on
1200 * the desk-top. That's because the Mac remembers the DID for the
1201 * trash (even for volumes in different zones, on different servers).
1202 * Just so this works better, we prime the DID cache with the trash,
1203 * fixing the trash at DID 17.
1206 cname( volume, volume->v_dir, &p );
1207 #endif /* CNID_DB */
1216 int afp_closevol(obj, ibuf, ibuflen, rbuf, rbuflen )
1219 int ibuflen, *rbuflen;
1221 struct vol *vol, *ovol;
1226 memcpy(&vid, ibuf, sizeof( vid ));
1227 if (( vol = getvolbyvid( vid )) == NULL ) {
1228 return( AFPERR_PARAM );
1231 vol->v_flags &= ~AFPVOL_OPEN;
1232 for ( ovol = volumes; ovol; ovol = ovol->v_next ) {
1233 if ( ovol->v_flags & AFPVOL_OPEN ) {
1237 if ( ovol != NULL ) {
1238 curdir = ovol->v_dir;
1239 if ( chdir( ovol->v_path ) < 0 ) {
1240 return( AFPERR_PARAM );
1244 dirfree( vol->v_root );
1247 cnid_close(vol->v_db);
1249 #endif /* CNID_DB */
1253 struct vol *getvolbyvid(const u_int16_t vid )
1257 for ( vol = volumes; vol; vol = vol->v_next ) {
1258 if ( vid == vol->v_vid ) {
1262 if ( vol == NULL || ( vol->v_flags & AFPVOL_OPEN ) == 0 ) {
1269 struct extmap *getextmap(const char *path)
1274 if (( p = strrchr( path, '.' )) == NULL ) {
1275 return( defextmap );
1278 for ( em = extmap; em; em = em->em_next ) {
1279 if ( strdiacasecmp( em->em_ext, p ) == 0 ) {
1284 return( defextmap );
1290 void setvoltime(obj, vol )
1296 /* just looking at vol->v_time is broken seriously since updates
1297 * from other users afpd processes never are seen.
1298 * This is not the most elegant solution (a shared memory between
1299 * the afpd processes would come closer)
1302 if ( gettimeofday( &tv, 0 ) < 0 ) {
1303 LOG(log_error, logtype_afpd, "setvoltime: gettimeofday: %s", strerror(errno) );
1306 if( utime( vol->v_path, NULL ) < 0 ) {
1307 /* write of time failed ... probably a read only filesys,
1308 * where no other users can interfere, so there's no issue here
1312 /* a little granularity */
1313 if (vol->v_time < tv.tv_sec) {
1314 vol->v_time = tv.tv_sec;
1315 if (afp_version > 21 && obj->options.server_notif) {
1316 obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED);
1321 int afp_getvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1324 int ibuflen, *rbuflen;
1329 u_int16_t vid, bitmap;
1332 memcpy(&vid, ibuf, sizeof( vid ));
1333 ibuf += sizeof( vid );
1334 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1335 bitmap = ntohs( bitmap );
1337 if (( vol = getvolbyvid( vid )) == NULL ) {
1339 return( AFPERR_PARAM );
1342 if ( stat( vol->v_path, &st ) < 0 ) {
1344 return( AFPERR_PARAM );
1347 buflen = *rbuflen - sizeof( bitmap );
1348 if (( ret = getvolparams( bitmap, vol, &st,
1349 rbuf + sizeof( bitmap ), &buflen )) != AFP_OK ) {
1353 *rbuflen = buflen + sizeof( bitmap );
1354 bitmap = htons( bitmap );
1355 memcpy(rbuf, &bitmap, sizeof( bitmap ));
1359 int afp_setvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1362 int ibuflen, *rbuflen;
1366 u_int16_t vid, bitmap;
1372 memcpy(&vid, ibuf, sizeof( vid ));
1373 ibuf += sizeof( vid );
1374 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1375 bitmap = ntohs( bitmap );
1376 ibuf += sizeof(bitmap);
1378 if (( vol = getvolbyvid( vid )) == NULL ) {
1379 return( AFPERR_PARAM );
1382 if (vol->v_flags & AFPVOL_RO)
1383 return AFPERR_VLOCK;
1385 /* we can only set the backup date. */
1386 if (bitmap != VOLPBIT_BDATE)
1387 return AFPERR_BITMAP;
1389 memset(&ad, 0, sizeof(ad));
1390 if ( ad_open( vol->v_path, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR,
1393 return AFPERR_VLOCK;
1395 return AFPERR_ACCESS;
1398 memcpy(&aint, ibuf, sizeof(aint));
1399 ad_setdate(&ad, AD_DATE_BACKUP, aint);
1400 ad_flush(&ad, ADFLAGS_HF);
1401 ad_close(&ad, ADFLAGS_HF);
1406 int wincheck(struct vol *vol, const char *path)
1410 if (!(vol->v_flags & AFPVOL_MSWINDOWS))
1413 /* empty paths are not allowed */
1414 if ((len = strlen(path)) == 0)
1417 /* leading or trailing whitespaces are not allowed, carriage returns
1418 * and probably other whitespace is okay, tabs are not allowed
1420 if ((path[0] == ' ') || (path[len-1] == ' '))
1423 /* certain characters are not allowed */
1424 if (strpbrk(path, MSWINDOWS_BADCHARS))
1427 /* everything else is okay */