2 * $Id: volume.c,v 1.36.2.7 2003-11-18 21:49:41 bfernhomberg 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;
84 static int extmap_cnt;
86 #define VOLOPT_ALLOW 0 /* user allow list */
87 #define VOLOPT_DENY 1 /* user deny list */
88 #define VOLOPT_RWLIST 2 /* user rw list */
89 #define VOLOPT_ROLIST 3 /* user ro list */
90 #define VOLOPT_CODEPAGE 4 /* codepage */
91 #define VOLOPT_PASSWORD 5 /* volume password */
92 #define VOLOPT_CASEFOLD 6 /* character case mangling */
93 #define VOLOPT_FLAGS 7 /* various flags */
94 #define VOLOPT_DBPATH 8 /* path to database */
95 #define VOLOPT_MAPCHARS 9 /* does mtou and utom mappings. syntax:
96 m and u can be double-byte hex
101 !u -> make u illegal always
102 ~u -> make u illegal only as the first
103 part of a double-byte character.
105 #define VOLOPT_VETO 10 /* list of veto filespec */
111 #define VOLOPT_FORCEUID 11 /* force uid for username x */
112 #define VOLOPT_FORCEGID 12 /* force gid for group x */
113 #define VOLOPT_UMASK 13
114 #define VOLOPT_MAX 13
115 #else /* normally, there are only 9 possible options */
116 #define VOLOPT_UMASK 11
117 #define VOLOPT_MAX 11
118 #endif /* FORCE_UIDGID */
120 #define VOLOPT_NUM (VOLOPT_MAX + 1)
123 #define VOLOPT_DEFAULT ":DEFAULT:"
124 #define VOLOPT_DEFAULT_LEN 9
130 static __inline__ void volfree(struct vol_option *options,
131 const struct vol_option *save)
136 for (i = 0; i < VOLOPT_MAX; i++) {
137 if (options[i].c_value && (options[i].c_value != save[i].c_value))
138 free(options[i].c_value);
141 for (i = 0; i < VOLOPT_MAX; i++) {
142 if (options[i].c_value)
143 free(options[i].c_value);
149 /* handle variable substitutions. here's what we understand:
150 * $c -> client ip/appletalk address
151 * $f -> full name (whatever's in the gecos field)
154 * $s -> server name (hostname if it doesn't exist)
155 * $u -> username (guest is usually nobody)
156 * $v -> volume name (ADEID_NAME or basename)
157 * $z -> zone (may not exist)
160 #define is_var(a, b) (strncmp((a), (b), 2) == 0)
161 static void volxlate(AFPObj *obj, char *dest, int destlen,
162 char *src, struct passwd *pwd, char *path)
167 strncpy(dest, src, destlen);
168 if ((p = strchr(src, '$')) == NULL) /* nothing to do */
171 /* first part of the path. just forward to the next variable. */
172 len = MIN(p - src, destlen);
178 while (p && destlen > 0) {
179 /* now figure out what the variable is */
181 if (is_var(p, "$c")) {
182 if (obj->proto == AFPPROTO_ASP) {
183 ASP asp = obj->handle;
185 len = sprintf(dest, "%u.%u", ntohs(asp->asp_sat.sat_addr.s_net),
186 asp->asp_sat.sat_addr.s_node);
190 } else if (obj->proto == AFPPROTO_DSI) {
191 DSI *dsi = obj->handle;
193 len = sprintf(dest, "%s:%u", inet_ntoa(dsi->client.sin_addr),
194 ntohs(dsi->client.sin_port));
198 } else if (is_var(p, "$f")) {
199 if ((q = strchr(pwd->pw_gecos, ',')))
202 } else if (is_var(p, "$g")) {
203 struct group *grp = getgrgid(pwd->pw_gid);
206 } else if (is_var(p, "$h")) {
207 q = obj->options.hostname;
208 } else if (is_var(p, "$s")) {
211 else if (obj->options.server) {
212 q = obj->options.server;
214 q = obj->options.hostname;
215 } else if (is_var(p, "$u")) {
217 } else if (is_var(p, "$v")) {
221 memset(&ad, 0, sizeof(ad));
222 if (ad_open(path, ADFLAGS_HF, O_RDONLY, 0, &ad) < 0)
225 if ((len = MIN(ad_getentrylen(&ad, ADEID_NAME), destlen)) > 0) {
226 memcpy(dest, ad_entry(&ad, ADEID_NAME), len);
227 ad_close(&ad, ADFLAGS_HF);
231 ad_close(&ad, ADFLAGS_HF);
232 no_volname: /* simple basename */
233 if ((q = strrchr(path, '/')) == NULL)
235 else if (*(q + 1) != '\0')
239 } else if (is_var(p, "$z")) {
241 } else if (is_var(p, "$$")) {
246 /* copy the stuff over. if we don't understand something that we
247 * should, just skip it over. */
249 len = MIN(p == q ? 2 : strlen(q), destlen);
250 strncpy(dest, q, len);
255 /* stuff up to next $ */
257 p = strchr(src, '$');
258 len = p ? MIN(p - src, destlen) : destlen;
260 strncpy(dest, src, len);
267 /* to make sure that val is valid, make sure to select an opt that
269 #define optionok(buf,opt,val) (strstr((buf),(opt)) && ((val)[1] != '\0'))
271 static __inline__ char *get_codepage_path(const char *path, const char *name)
277 page = (char *) malloc((len = strlen(path)) + strlen(name) + 2);
280 if (path[len - 1] != '/') /* add a / */
288 /* debug: show which codepage directory we are using */
289 LOG(log_debug, logtype_afpd, "using codepage directory: %s", page);
294 /* handle all the options. tmp can't be NULL. */
295 static void volset(struct vol_option *options, char *volname, int vlen,
296 const char *nlspath, const char *tmp, AFPObj *obj,
301 LOG(log_debug, logtype_afpd, "Parsing volset %s", tmp);
303 val = strchr(tmp, ':');
305 /* we'll assume it's a volume name. */
306 strncpy(volname, tmp, vlen);
308 } else if (optionok(tmp, "allow:", val)) {
309 if (options[VOLOPT_ALLOW].c_value)
310 free(options[VOLOPT_ALLOW].c_value);
311 options[VOLOPT_ALLOW].c_value = strdup(val + 1);
313 } else if (optionok(tmp, "deny:", val)) {
314 if (options[VOLOPT_DENY].c_value)
315 free(options[VOLOPT_DENY].c_value);
316 options[VOLOPT_DENY].c_value = strdup(val + 1);
318 } else if (optionok(tmp, "rwlist:", val)) {
319 if (options[VOLOPT_RWLIST].c_value)
320 free(options[VOLOPT_RWLIST].c_value);
321 options[VOLOPT_RWLIST].c_value = strdup(val + 1);
323 } else if (optionok(tmp, "rolist:", val)) {
324 if (options[VOLOPT_ROLIST].c_value)
325 free(options[VOLOPT_ROLIST].c_value);
326 options[VOLOPT_ROLIST].c_value = strdup(val + 1);
328 } else if (optionok(tmp, "codepage:", val)) {
329 if (options[VOLOPT_CODEPAGE].c_value)
330 free(options[VOLOPT_CODEPAGE].c_value);
331 options[VOLOPT_CODEPAGE].c_value = get_codepage_path(nlspath, val + 1);
333 } else if (optionok(tmp, "veto:", val)) {
334 if (options[VOLOPT_VETO].c_value)
335 free(options[VOLOPT_VETO].c_value);
336 options[VOLOPT_VETO].c_value = strdup(val + 1);
338 } else if (optionok(tmp, "casefold:", val)) {
339 if (strcasecmp(val + 1, "tolower") == 0)
340 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMLOWER;
341 else if (strcasecmp(val + 1, "toupper") == 0)
342 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMUPPER;
343 else if (strcasecmp(val + 1, "xlatelower") == 0)
344 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UUPPERMLOWER;
345 else if (strcasecmp(val + 1, "xlateupper") == 0)
346 options[VOLOPT_CASEFOLD].i_value = AFPVOL_ULOWERMUPPER;
348 } else if (optionok(tmp, "options:", val)) {
351 if ((p = strtok(val + 1, ",")) == NULL) /* nothing */
355 if (strcasecmp(p, "prodos") == 0)
356 options[VOLOPT_FLAGS].i_value |= AFPVOL_A2VOL;
357 else if (strcasecmp(p, "mswindows") == 0) {
358 options[VOLOPT_FLAGS].i_value |= (AFPVOL_MSWINDOWS | AFPVOL_USEDOTS);
359 if (!options[VOLOPT_CODEPAGE].c_value)
360 options[VOLOPT_CODEPAGE].c_value =
361 get_codepage_path(nlspath, MSWINDOWS_CODEPAGE);
363 } else if (strcasecmp(p, "crlf") == 0)
364 options[VOLOPT_FLAGS].i_value |= AFPVOL_CRLF;
365 else if (strcasecmp(p, "noadouble") == 0)
366 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOADOUBLE;
367 else if (strcasecmp(p, "ro") == 0)
368 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
369 else if (strcasecmp(p, "nohex") == 0)
370 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOHEX;
371 else if (strcasecmp(p, "usedots") == 0)
372 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS;
373 else if (strcasecmp(p, "limitsize") == 0)
374 options[VOLOPT_FLAGS].i_value |= AFPVOL_LIMITSIZE;
375 /* support for either "dropbox" or "dropkludge" */
376 else if (strcasecmp(p, "dropbox") == 0)
377 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
378 else if (strcasecmp(p, "dropkludge") == 0)
379 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
380 else if (strcasecmp(p, "nofileid") == 0)
381 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOFILEID;
383 p = strtok(NULL, ",");
387 } else if (optionok(tmp, "dbpath:", val)) {
388 char t[MAXPATHLEN + 1];
389 if (options[VOLOPT_DBPATH].c_value)
390 free(options[VOLOPT_DBPATH].c_value);
392 volxlate(obj, t, MAXPATHLEN, val, pwd, NULL);
393 options[VOLOPT_DBPATH].c_value = strdup(t + 1);
395 } else if (optionok(tmp, "umask:", val)) {
396 options[VOLOPT_UMASK].i_value = (int)strtol(val, (char **)NULL, 8);
397 } else if (optionok(tmp, "mapchars:",val)) {
398 if (options[VOLOPT_MAPCHARS].c_value)
399 free(options[VOLOPT_MAPCHARS].c_value);
400 options[VOLOPT_MAPCHARS].c_value = strdup(val + 1);
402 } else if (optionok(tmp, "password:", val)) {
403 if (options[VOLOPT_PASSWORD].c_value)
404 free(options[VOLOPT_PASSWORD].c_value);
405 options[VOLOPT_PASSWORD].c_value = strdup(val + 1);
409 /* this code allows forced uid/gid per volume settings */
410 } else if (optionok(tmp, "forceuid:", val)) {
411 if (options[VOLOPT_FORCEUID].c_value)
412 free(options[VOLOPT_FORCEUID].c_value);
413 options[VOLOPT_FORCEUID].c_value = strdup(val + 1);
414 } else if (optionok(tmp, "forcegid:", val)) {
415 if (options[VOLOPT_FORCEGID].c_value)
416 free(options[VOLOPT_FORCEGID].c_value);
417 options[VOLOPT_FORCEGID].c_value = strdup(val + 1);
419 #endif /* FORCE_UIDGID */
422 /* ignore unknown options */
423 LOG(log_debug, logtype_afpd, "ignoring unknown volume option: %s", tmp);
428 static int creatvol(const char *path, char *name, struct vol_option *options)
433 if ( name == NULL || *name == '\0' ) {
434 if ((name = strrchr( path, '/' )) == NULL) {
435 return -1; /* Obviously not a fully qualified path */
438 /* if you wish to share /, you need to specify a name. */
443 for ( volume = volumes; volume; volume = volume->v_next ) {
444 if ( strcasecmp( volume->v_name, name ) == 0 ) {
445 return -1; /* Won't be able to access it, anyway... */
449 vlen = strlen( name );
450 if ( vlen > AFPVOL_NAMELEN ) {
451 vlen = AFPVOL_NAMELEN;
452 name[AFPVOL_NAMELEN] = '\0';
456 (struct vol *)calloc(1, sizeof( struct vol ))) == NULL ) {
457 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
460 if (( volume->v_name =
461 (char *)malloc( vlen + 1 )) == NULL ) {
462 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
466 if (( volume->v_path =
467 (char *)malloc( strlen( path ) + 1 )) == NULL ) {
468 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
469 free(volume->v_name);
474 strcpy( volume->v_name, name);
475 strcpy( volume->v_path, path );
479 #endif /* __svr4__ */
480 volume->v_vid = lastvid++;
481 volume->v_lastdid = 17;
485 /* should we casefold? */
486 volume->v_casefold = options[VOLOPT_CASEFOLD].i_value;
488 /* shift in some flags */
489 volume->v_flags = options[VOLOPT_FLAGS].i_value;
491 /* read in the code pages */
492 if (options[VOLOPT_CODEPAGE].c_value)
493 codepage_read(volume, options[VOLOPT_CODEPAGE].c_value);
495 if (options[VOLOPT_PASSWORD].c_value)
496 volume->v_password = strdup(options[VOLOPT_PASSWORD].c_value);
498 if (options[VOLOPT_VETO].c_value)
499 volume->v_veto = strdup(options[VOLOPT_VETO].c_value);
502 if (options[VOLOPT_DBPATH].c_value)
503 volume->v_dbpath = strdup(options[VOLOPT_DBPATH].c_value);
506 if (options[VOLOPT_UMASK].i_value)
507 volume->v_umask = (mode_t)options[VOLOPT_UMASK].i_value;
511 if (options[VOLOPT_FORCEUID].c_value) {
512 volume->v_forceuid = strdup(options[VOLOPT_FORCEUID].c_value);
514 volume->v_forceuid = NULL; /* set as null so as to return 0 later on */
517 if (options[VOLOPT_FORCEGID].c_value) {
518 volume->v_forcegid = strdup(options[VOLOPT_FORCEGID].c_value);
520 volume->v_forcegid = NULL; /* set as null so as to return 0 later on */
523 #endif /* FORCE_UIDGID */
527 volume->v_next = volumes;
532 static char *myfgets( buf, size, fp )
541 while ((EOF != ( c = getc( fp )) ) && ( size > 0 )) {
542 if ( c == '\n' || c == '\r' ) {
560 /* check access list. this function wants something of the following
562 * @group,name,name2,@group2,name3
564 * a NULL argument allows everybody to have access.
565 * we return three things:
567 * 0: list exists, but name isn't in it
570 static int accessvol(args, name)
574 char buf[MAXPATHLEN + 1], *p;
580 strncpy(buf, args, sizeof(buf));
581 if ((p = strtok(buf, ",")) == NULL) /* nothing, return okay */
585 if (*p == '@') { /* it's a group */
586 if ((gr = getgrnam(p + 1)) && gmem(gr->gr_gid))
588 } else if (strcmp(p, name) == 0) /* it's a user name */
590 p = strtok(NULL, ",");
596 static void setextmap( ext, type, creator, user)
597 char *ext, *type, *creator;
603 if (extmap == NULL) {
604 if (( extmap = calloc(1, sizeof( struct extmap ))) == NULL ) {
605 LOG(log_error, logtype_afpd, "setextmap: calloc: %s", strerror(errno) );
610 for ( em = extmap, cnt = 0; em->em_ext; em++, cnt++) {
611 if ( (strdiacasecmp( em->em_ext, ext )) == 0 ) {
616 if ( em->em_ext == NULL ) {
617 if (!(extmap = realloc( extmap, sizeof( struct extmap ) * (cnt +2))) ) {
618 LOG(log_error, logtype_afpd, "setextmap: realloc: %s", strerror(errno) );
621 (extmap +cnt +1)->em_ext = NULL;
623 } else if ( !user ) {
629 if (!(em->em_ext = strdup( ext))) {
630 LOG(log_error, logtype_afpd, "setextmap: strdup: %s", strerror(errno) );
634 if ( *type == '\0' ) {
635 memcpy(em->em_type, "????", sizeof( em->em_type ));
637 memcpy(em->em_type, type, sizeof( em->em_type ));
639 if ( *creator == '\0' ) {
640 memcpy(em->em_creator, "UNIX", sizeof( em->em_creator ));
642 memcpy(em->em_creator, creator, sizeof( em->em_creator ));
646 /* -------------------------- */
647 static int extmap_cmp(const void *map1, const void *map2)
649 const struct extmap *em1 = map1;
650 const struct extmap *em2 = map2;
651 return strdiacasecmp(em1->em_ext, em2->em_ext);
654 static void sortextmap( void)
659 if ((em = extmap) == NULL) {
667 qsort(extmap, extmap_cnt, sizeof(struct extmap), extmap_cmp);
674 * Read a volume configuration file and add the volumes contained within to
675 * the global volume list. If p2 is non-NULL, the file that is opened is
678 * Lines that begin with # and blank lines are ignored.
679 * Volume lines are of the form:
680 * <unix path> [<volume name>] [allow:<user>,<@group>,...] \
681 * [codepage:<file>] [casefold:<num>]
682 * <extension> TYPE [CREATOR]
684 static int readvolfile(obj, p1, p2, user, pwent)
688 struct passwd *pwent;
691 char path[ MAXPATHLEN + 1], tmp[ MAXPATHLEN + 1],
692 volname[ AFPVOL_NAMELEN + 1 ], buf[ BUFSIZ ],
693 type[ 5 ], creator[ 5 ];
696 struct vol_option options[VOLOPT_NUM], save_options[VOLOPT_NUM];
708 if (NULL == ( fp = fopen( path, "r" )) ) {
712 memset(save_options, 0, sizeof(save_options));
713 while ( myfgets( buf, sizeof( buf ), fp ) != NULL ) {
714 initline( strlen( buf ), buf );
715 parseline( sizeof( path ) - 1, path );
722 /* change the default options for this file */
723 if (strncmp(path, VOLOPT_DEFAULT, VOLOPT_DEFAULT_LEN) == 0) {
725 for (i = 0; i < VOLOPT_NUM; i++) {
726 if (parseline( sizeof( path ) - VOLOPT_DEFAULT_LEN - 1,
727 path + VOLOPT_DEFAULT_LEN) < 0)
729 volset(save_options, tmp, sizeof(tmp) - 1,
730 obj->options.nlspath, path + VOLOPT_DEFAULT_LEN,
737 if (( p = strchr( path, '/' )) != NULL ) {
745 if ( u == NULL || *u == '\0' || ( pw = getpwnam( u )) == NULL ) {
748 strcpy( tmp, pw->pw_dir );
749 if ( p != NULL && *p != '\0' ) {
753 /* Tag a user's home directory with their umask. Note, this will
754 * be overwritten if the user actually specifies a umask: option
755 * for a '~' volume. */
756 save_options[VOLOPT_UMASK].i_value = obj->options.save_mask;
760 /* send path through variable substitution */
761 if (*path != '~') /* need to copy path to tmp */
764 pwent = getpwnam(obj->username);
765 volxlate(obj, path, sizeof(path) - 1, tmp, pwent, NULL);
767 /* this is sort of braindead. basically, i want to be
768 * able to specify things in any order, but i don't want to
769 * re-write everything.
771 * currently we have 11 options:
779 * options:prodos,crlf,noadouble,ro
782 * namemask:x,y,!z (not implemented yet)
784 memcpy(options, save_options, sizeof(options));
787 /* read in up to 11 possible options */
788 for (i = 0; i < VOLOPT_NUM; i++) {
789 if (parseline( sizeof( tmp ) - 1, tmp ) < 0)
792 volset(options, volname, sizeof(volname) - 1,
793 obj->options.nlspath, tmp, obj, pwent);
796 /* check allow/deny lists:
797 allow -> either no list (-1), or in list (1)
798 deny -> either no list (-1), or not in list (0) */
799 if (accessvol(options[VOLOPT_ALLOW].c_value, obj->username) &&
800 (accessvol(options[VOLOPT_DENY].c_value, obj->username) < 1)) {
802 /* handle read-only behaviour. semantics:
803 * 1) neither the rolist nor the rwlist exist -> rw
804 * 2) rolist exists -> ro if user is in it.
805 * 3) rwlist exists -> ro unless user is in it. */
806 if (((options[VOLOPT_FLAGS].i_value & AFPVOL_RO) == 0) &&
807 ((accessvol(options[VOLOPT_ROLIST].c_value,
808 obj->username) == 1) ||
809 !accessvol(options[VOLOPT_RWLIST].c_value,
811 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
813 /* do variable substitution */
814 volxlate(obj, tmp, sizeof(tmp) - 1, volname, pwent, path);
815 creatvol(path, tmp, options);
817 volfree(options, save_options);
821 parseline( sizeof( type ) - 1, type );
822 parseline( sizeof( creator ) - 1, creator );
823 setextmap( path, type, creator, user);
830 volfree(save_options, NULL);
832 if ( fclose( fp ) != 0 ) {
833 LOG(log_error, logtype_afpd, "readvolfile: fclose: %s", strerror(errno) );
839 static void load_volumes(AFPObj *obj)
841 struct passwd *pwent = getpwnam(obj->username);
843 if ( (obj->options.flags & OPTION_USERVOLFIRST) == 0 ) {
844 readvolfile(obj, obj->options.systemvol, NULL, 0, pwent);
847 if ((*obj->username == '\0') || (obj->options.flags & OPTION_NOUSERVOL)) {
848 readvolfile(obj, obj->options.defaultvol, NULL, 1, pwent);
851 * Read user's AppleVolumes or .AppleVolumes file
852 * If neither are readable, read the default volumes file. if
853 * that doesn't work, create a user share.
855 if ( readvolfile(obj, pwent->pw_dir, "AppleVolumes", 1, pwent) < 0 &&
856 readvolfile(obj, pwent->pw_dir, ".AppleVolumes", 1, pwent) < 0 &&
857 readvolfile(obj, pwent->pw_dir, "applevolumes", 1, pwent) < 0 &&
858 readvolfile(obj, pwent->pw_dir, ".applevolumes", 1, pwent) < 0 &&
859 obj->options.defaultvol != NULL ) {
860 if (readvolfile(obj, obj->options.defaultvol, NULL, 1, pwent) < 0)
861 creatvol(pwent->pw_dir, NULL, NULL);
864 if ( obj->options.flags & OPTION_USERVOLFIRST ) {
865 readvolfile(obj, obj->options.systemvol, NULL, 0, pwent );
869 static int getvolspace( vol, bfree, btotal, xbfree, xbtotal, bsize )
871 u_int32_t *bfree, *btotal, *bsize;
872 VolSpace *xbfree, *xbtotal;
876 #ifndef NO_QUOTA_SUPPORT
877 VolSpace qfree, qtotal;
878 #endif /* ! NO_QUOTA_SUPPORT */
880 spaceflag = AFPVOL_GVSMASK & vol->v_flags;
881 /* report up to 2GB if afp version is < 2.2 (4GB if not) */
882 maxsize = (vol->v_flags & AFPVOL_A2VOL) ? 0x01fffe00 :
883 (((afp_version < 22) || (vol->v_flags & AFPVOL_LIMITSIZE))
884 ? 0x7fffffffL : 0xffffffffL);
887 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_AFSGVS ) {
888 if ( afs_getvolspace( vol, xbfree, xbtotal, bsize ) == AFP_OK ) {
889 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_AFSGVS;
890 goto getvolspace_done;
895 if (( rc = ustatfs_getvolspace( vol, xbfree, xbtotal,
896 bsize)) != AFP_OK ) {
900 #define min(a,b) ((a)<(b)?(a):(b))
901 #ifndef NO_QUOTA_SUPPORT
902 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_UQUOTA ) {
903 if ( uquota_getvolspace( vol, &qfree, &qtotal, *bsize ) == AFP_OK ) {
904 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_UQUOTA;
905 *xbfree = min(*xbfree, qfree);
906 *xbtotal = min( *xbtotal, qtotal);
907 goto getvolspace_done;
910 #endif /* ! NO_QUOTA_SUPPORT */
911 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_USTATFS;
914 *bfree = min( *xbfree, maxsize);
915 *btotal = min( *xbtotal, maxsize);
919 static int getvolparams( bitmap, vol, st, buf, buflen )
927 int bit = 0, isad = 1;
930 u_int32_t bfree, btotal, bsize;
931 VolSpace xbfree, xbtotal; /* extended bytes */
932 char *data, *nameoff = NULL;
935 /* courtesy of jallison@whistle.com:
936 * For MacOS8.x support we need to create the
937 * .Parent file here if it doesn't exist. */
939 memset(&ad, 0, sizeof(ad));
940 if ( ad_open( vol->v_path, vol_noadouble(vol) |
941 ADFLAGS_HF|ADFLAGS_DIR, O_RDWR | O_CREAT,
945 } else if (ad_getoflags( &ad, ADFLAGS_HF ) & O_CREAT) {
946 slash = strrchr( vol->v_path, '/' );
952 ad_setentrylen( &ad, ADEID_NAME, strlen( slash ));
953 memcpy(ad_entry( &ad, ADEID_NAME ), slash,
954 ad_getentrylen( &ad, ADEID_NAME ));
955 ad_setdate(&ad, AD_DATE_CREATE | AD_DATE_UNIX, st->st_mtime);
956 ad_flush(&ad, ADFLAGS_HF);
959 if (( bitmap & ( (1<<VOLPBIT_BFREE)|(1<<VOLPBIT_BTOTAL) |
960 (1<<VOLPBIT_XBFREE)|(1<<VOLPBIT_XBTOTAL) |
961 (1<<VOLPBIT_BSIZE)) ) != 0 ) {
962 if ( getvolspace( vol, &bfree, &btotal, &xbfree, &xbtotal,
965 ad_close( &ad, ADFLAGS_HF );
967 return( AFPERR_PARAM );
972 while ( bitmap != 0 ) {
973 while (( bitmap & 1 ) == 0 ) {
982 if (0 == (vol->v_flags & AFPVOL_NOFILEID)) {
983 ashort = VOLPBIT_ATTR_FILEID;
986 /* check for read-only.
987 * NOTE: we don't actually set the read-only flag unless
988 * it's passed in that way as it's possible to mount
989 * a read-write filesystem under a read-only one. */
990 if ((vol->v_flags & AFPVOL_RO) ||
991 ((utime(vol->v_path, NULL) < 0) && (errno == EROFS)))
992 ashort |= VOLPBIT_ATTR_RO;
993 ashort |= VOLPBIT_ATTR_CATSEARCH;
994 ashort = htons(ashort);
995 memcpy(data, &ashort, sizeof( ashort ));
996 data += sizeof( ashort );
1000 ashort = htons( AFPVOLSIG_DEFAULT );
1001 memcpy(data, &ashort, sizeof( ashort ));
1002 data += sizeof( ashort );
1005 case VOLPBIT_CDATE :
1006 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
1007 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1008 memcpy(data, &aint, sizeof( aint ));
1009 data += sizeof( aint );
1012 case VOLPBIT_MDATE :
1013 if ( st->st_mtime > vol->v_time ) {
1014 vol->v_time = st->st_mtime;
1015 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1017 aint = AD_DATE_FROM_UNIX(vol->v_time);
1019 memcpy(data, &aint, sizeof( aint ));
1020 data += sizeof( aint );
1023 case VOLPBIT_BDATE :
1024 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1025 aint = AD_DATE_START;
1026 memcpy(data, &aint, sizeof( aint ));
1027 data += sizeof( aint );
1031 memcpy(data, &vol->v_vid, sizeof( vol->v_vid ));
1032 data += sizeof( vol->v_vid );
1035 case VOLPBIT_BFREE :
1036 bfree = htonl( bfree );
1037 memcpy(data, &bfree, sizeof( bfree ));
1038 data += sizeof( bfree );
1041 case VOLPBIT_BTOTAL :
1042 btotal = htonl( btotal );
1043 memcpy(data, &btotal, sizeof( btotal ));
1044 data += sizeof( btotal );
1047 #ifndef NO_LARGE_VOL_SUPPORT
1048 case VOLPBIT_XBFREE :
1049 xbfree = hton64( xbfree );
1050 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1051 bcopy(&xbfree, data, sizeof(xbfree));
1052 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1053 memcpy(data, &xbfree, sizeof( xbfree ));
1054 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1055 data += sizeof( xbfree );
1058 case VOLPBIT_XBTOTAL :
1059 xbtotal = hton64( xbtotal );
1060 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1061 bcopy(&xbtotal, data, sizeof(xbtotal));
1062 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1063 memcpy(data, &xbtotal, sizeof( xbtotal ));
1064 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1065 data += sizeof( xbfree );
1067 #endif /* ! NO_LARGE_VOL_SUPPORT */
1071 data += sizeof( u_int16_t );
1074 case VOLPBIT_BSIZE: /* block size */
1075 bsize = htonl(bsize);
1076 memcpy(data, &bsize, sizeof(bsize));
1077 data += sizeof(bsize);
1082 ad_close( &ad, ADFLAGS_HF );
1084 return( AFPERR_BITMAP );
1090 ashort = htons( data - buf );
1091 memcpy(nameoff, &ashort, sizeof( ashort ));
1092 aint = strlen( vol->v_name );
1094 memcpy(data, vol->v_name, aint );
1098 ad_close( &ad, ADFLAGS_HF );
1100 *buflen = data - buf;
1106 int afp_getsrvrparms(obj, ibuf, ibuflen, rbuf, rbuflen )
1109 int ibuflen, *rbuflen;
1122 for ( vcnt = 0, volume = volumes; volume; volume = volume->v_next ) {
1123 if ( stat( volume->v_path, &st ) < 0 ) {
1124 LOG(log_info, logtype_afpd, "afp_getsrvrparms: stat %s: %s",
1125 volume->v_path, strerror(errno) );
1126 continue; /* can't access directory */
1128 if (!S_ISDIR(st.st_mode)) {
1129 continue; /* not a dir */
1132 /* set password bit if there's a volume password */
1133 *data = (volume->v_password) ? AFPSRVR_PASSWD : 0;
1135 /* Apple 2 clients running ProDOS-8 expect one volume to have
1136 bit 0 of this byte set. They will not recognize anything
1137 on the server unless this is the case. I have not
1138 completely worked this out, but it's related to booting
1139 from the server. Support for that function is a ways
1140 off.. <shirsch@ibm.net> */
1141 *data++ |= (volume->v_flags & AFPVOL_A2VOL) ? AFPSRVR_CONFIGINFO : 0;
1142 len = strlen( volume->v_name );
1144 memcpy(data, volume->v_name, len );
1149 *rbuflen = data - rbuf;
1151 if ( gettimeofday( &tv, 0 ) < 0 ) {
1152 LOG(log_error, logtype_afpd, "afp_getsrvrparms: gettimeofday: %s", strerror(errno) );
1154 return AFPERR_PARAM;
1156 tv.tv_sec = AD_DATE_FROM_UNIX(tv.tv_sec);
1157 memcpy(data, &tv.tv_sec, sizeof( u_int32_t));
1158 data += sizeof( u_int32_t);
1163 int afp_openvol(obj, ibuf, ibuflen, rbuf, rbuflen )
1166 int ibuflen, *rbuflen;
1172 #endif /* CNID_DB */
1175 int len, ret, buflen;
1179 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1180 bitmap = ntohs( bitmap );
1181 ibuf += sizeof( bitmap );
1182 if (( bitmap & (1<<VOLPBIT_VID)) == 0 ) {
1183 ret = AFPERR_BITMAP;
1187 len = (unsigned char)*ibuf++;
1188 volname = obj->oldtmp;
1189 memcpy(volname, ibuf, len );
1190 *(volname + len) = '\0';
1192 if ((len + 1) & 1) /* pad to an even boundary */
1198 for ( volume = volumes; volume; volume = volume->v_next ) {
1199 if ( strcasecmp( volname, volume->v_name ) == 0 ) {
1204 if ( volume == NULL ) {
1209 /* check for a volume password */
1210 if (volume->v_password &&
1211 strncmp(ibuf, volume->v_password, VOLPASSLEN)) {
1212 ret = AFPERR_ACCESS;
1216 if (( volume->v_flags & AFPVOL_OPEN ) == 0 ) {
1217 if ((dir = dirnew(strlen(volume->v_name) + 1)) == NULL) {
1218 LOG(log_error, logtype_afpd, "afp_openvol: malloc: %s", strerror(errno) );
1222 dir->d_did = DIRDID_ROOT;
1223 dir->d_color = DIRTREE_COLOR_BLACK; /* root node is black */
1224 strcpy( dir->d_name, volume->v_name );
1225 volume->v_dir = volume->v_root = dir;
1226 volume->v_flags |= AFPVOL_OPEN;
1229 set_uidgid ( volume );
1230 #endif /* FORCE_UIDGID */
1232 if ( stat( volume->v_path, &st ) < 0 ) {
1237 buflen = *rbuflen - sizeof( bitmap );
1238 if (( ret = getvolparams( bitmap, volume, &st,
1239 rbuf + sizeof(bitmap), &buflen )) != AFP_OK ) {
1242 *rbuflen = buflen + sizeof( bitmap );
1243 bitmap = htons( bitmap );
1244 memcpy(rbuf, &bitmap, sizeof( bitmap ));
1246 if ( chdir( volume->v_path ) < 0 ) {
1250 curdir = volume->v_dir;
1253 if (volume->v_dbpath)
1254 volume->v_db = cnid_open (volume->v_dbpath, volume->v_umask);
1255 if (volume->v_db == NULL)
1256 volume->v_db = cnid_open (volume->v_path, volume->v_umask);
1257 #endif /* CNID_DB */
1261 * If you mount a volume twice, the second time the trash appears on
1262 * the desk-top. That's because the Mac remembers the DID for the
1263 * trash (even for volumes in different zones, on different servers).
1264 * Just so this works better, we prime the DID cache with the trash,
1265 * fixing the trash at DID 17.
1268 cname( volume, volume->v_dir, &p );
1269 #endif /* CNID_DB */
1278 int afp_closevol(obj, ibuf, ibuflen, rbuf, rbuflen )
1281 int ibuflen, *rbuflen;
1283 struct vol *vol, *ovol;
1288 memcpy(&vid, ibuf, sizeof( vid ));
1289 if (NULL == ( vol = getvolbyvid( vid )) ) {
1290 return( AFPERR_PARAM );
1293 vol->v_flags &= ~AFPVOL_OPEN;
1294 for ( ovol = volumes; ovol; ovol = ovol->v_next ) {
1295 if ( ovol->v_flags & AFPVOL_OPEN ) {
1299 if ( ovol != NULL ) {
1300 /* Even if chdir fails, we can't say afp_closevol fails. */
1301 if ( chdir( ovol->v_path ) == 0 ) {
1302 curdir = ovol->v_dir;
1305 dirfree( vol->v_root );
1308 cnid_close(vol->v_db);
1310 #endif /* CNID_DB */
1314 struct vol *getvolbyvid(const u_int16_t vid )
1318 for ( vol = volumes; vol; vol = vol->v_next ) {
1319 if ( vid == vol->v_vid ) {
1323 if ( vol == NULL || ( vol->v_flags & AFPVOL_OPEN ) == 0 ) {
1329 #endif /* FORCE_UIDGID */
1334 /* ------------------------ */
1335 static int ext_cmp_key(const void *key, const void *obj)
1337 const char *p = key;
1338 const struct extmap *em = obj;
1339 return strdiacasecmp(p, em->em_ext);
1341 struct extmap *getextmap(const char *path)
1346 if (NULL == ( p = strrchr( path, '.' )) ) {
1347 return( defextmap );
1350 if (!*p || !extmap_cnt) {
1351 return( defextmap );
1353 em = bsearch(p, extmap, extmap_cnt, sizeof(struct extmap), ext_cmp_key);
1357 return( defextmap );
1361 struct extmap *getdefextmap(void)
1363 return( defextmap );
1367 poll if a volume is changed by other processes.
1369 int pollvoltime(obj)
1376 if (!(afp_version > 21 && obj->options.server_notif))
1379 if ( gettimeofday( &tv, 0 ) < 0 )
1382 for ( vol = volumes; vol; vol = vol->v_next ) {
1383 if ( (vol->v_flags & AFPVOL_OPEN) && vol->v_time + 30 < tv.tv_sec) {
1384 if ( !stat( vol->v_path, &st ) && vol->v_time != st.st_mtime ) {
1385 vol->v_time = st.st_mtime;
1386 obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED);
1394 void setvoltime(obj, vol )
1400 /* just looking at vol->v_time is broken seriously since updates
1401 * from other users afpd processes never are seen.
1402 * This is not the most elegant solution (a shared memory between
1403 * the afpd processes would come closer)
1406 if ( gettimeofday( &tv, 0 ) < 0 ) {
1407 LOG(log_error, logtype_afpd, "setvoltime: gettimeofday: %s", strerror(errno) );
1410 if( utime( vol->v_path, NULL ) < 0 ) {
1411 /* write of time failed ... probably a read only filesys,
1412 * where no other users can interfere, so there's no issue here
1416 /* a little granularity */
1417 if (vol->v_time < tv.tv_sec) {
1418 vol->v_time = tv.tv_sec;
1419 if (afp_version > 21 && obj->options.server_notif) {
1420 obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED);
1425 int afp_getvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1428 int ibuflen, *rbuflen;
1433 u_int16_t vid, bitmap;
1436 memcpy(&vid, ibuf, sizeof( vid ));
1437 ibuf += sizeof( vid );
1438 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1439 bitmap = ntohs( bitmap );
1441 if (NULL == ( vol = getvolbyvid( vid )) ) {
1443 return( AFPERR_PARAM );
1446 if ( stat( vol->v_path, &st ) < 0 ) {
1448 return( AFPERR_PARAM );
1451 buflen = *rbuflen - sizeof( bitmap );
1452 if (( ret = getvolparams( bitmap, vol, &st,
1453 rbuf + sizeof( bitmap ), &buflen )) != AFP_OK ) {
1457 *rbuflen = buflen + sizeof( bitmap );
1458 bitmap = htons( bitmap );
1459 memcpy(rbuf, &bitmap, sizeof( bitmap ));
1463 int afp_setvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1466 int ibuflen, *rbuflen;
1470 u_int16_t vid, bitmap;
1476 memcpy(&vid, ibuf, sizeof( vid ));
1477 ibuf += sizeof( vid );
1478 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1479 bitmap = ntohs( bitmap );
1480 ibuf += sizeof(bitmap);
1482 if (( vol = getvolbyvid( vid )) == NULL ) {
1483 return( AFPERR_PARAM );
1486 if (vol->v_flags & AFPVOL_RO)
1487 return AFPERR_VLOCK;
1489 /* we can only set the backup date. */
1490 if (bitmap != VOLPBIT_BDATE)
1491 return AFPERR_BITMAP;
1493 memset(&ad, 0, sizeof(ad));
1494 if ( ad_open( vol->v_path, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR,
1497 return AFPERR_VLOCK;
1499 return AFPERR_ACCESS;
1502 memcpy(&aint, ibuf, sizeof(aint));
1503 ad_setdate(&ad, AD_DATE_BACKUP, aint);
1504 ad_flush(&ad, ADFLAGS_HF);
1505 ad_close(&ad, ADFLAGS_HF);
1510 int wincheck(const struct vol *vol, const char *path)
1514 if (!(vol->v_flags & AFPVOL_MSWINDOWS))
1517 /* empty paths are not allowed */
1518 if ((len = strlen(path)) == 0)
1521 /* leading or trailing whitespaces are not allowed, carriage returns
1522 * and probably other whitespace is okay, tabs are not allowed
1524 if ((path[0] == ' ') || (path[len-1] == ' '))
1527 /* certain characters are not allowed */
1528 if (strpbrk(path, MSWINDOWS_BADCHARS))
1531 /* everything else is okay */