2 * $Id: volume.c,v 1.51.2.7.2.1 2003-09-09 16:42:20 didg Exp $
4 * Copyright (c) 1990,1993 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
10 #endif /* HAVE_CONFIG_H */
13 #include <atalk/logger.h>
14 #include <sys/types.h>
16 #include <sys/param.h>
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
20 #include <netatalk/endian.h>
21 #include <atalk/asp.h>
22 #include <atalk/dsi.h>
23 #include <atalk/adouble.h>
24 #include <atalk/afp.h>
25 #include <atalk/util.h>
27 #include <atalk/cnid.h>
32 #endif /* HAVE_FCNTL_H */
40 #else /* STDC_HEADERS */
44 #endif /* HAVE_STRCHR */
45 char *strchr (), *strrchr ();
47 #define memcpy(d,s,n) bcopy ((s), (d), (n))
48 #define memmove(d,s,n) bcopy ((s), (d), (n))
49 #endif /* ! HAVE_MEMCPY */
50 #endif /* STDC_HEADERS */
57 #include "directory.h"
63 extern int afprun(int root, char *cmd, int *outfd);
66 #define MIN(a, b) ((a) < (b) ? (a) : (b))
69 #ifndef NO_LARGE_VOL_SUPPORT
70 #if BYTE_ORDER == BIG_ENDIAN
73 #else /* BYTE_ORDER == BIG_ENDIAN */
74 #define hton64(x) ((u_int64_t) (htonl(((x) >> 32) & 0xffffffffLL)) | \
75 (u_int64_t) ((htonl(x) & 0xffffffffLL) << 32))
76 #define ntoh64(x) (hton64(x))
77 #endif /* BYTE_ORDER == BIG_ENDIAN */
78 #endif /* ! NO_LARGE_VOL_SUPPORT */
80 static struct vol *Volumes = NULL;
81 static int lastvid = 0;
82 static char *Trash = "\02\024Network Trash Folder";
84 static struct extmap *Extmap = NULL, *Defextmap = NULL;
85 static int Extmap_cnt;
86 static void free_extmap(void);
88 #define VOLOPT_ALLOW 0 /* user allow list */
89 #define VOLOPT_DENY 1 /* user deny list */
90 #define VOLOPT_RWLIST 2 /* user rw list */
91 #define VOLOPT_ROLIST 3 /* user ro list */
92 #define VOLOPT_CODEPAGE 4 /* codepage */
93 #define VOLOPT_PASSWORD 5 /* volume password */
94 #define VOLOPT_CASEFOLD 6 /* character case mangling */
95 #define VOLOPT_FLAGS 7 /* various flags */
96 #define VOLOPT_DBPATH 8 /* path to database */
97 #define VOLOPT_MAPCHARS 9 /* does mtou and utom mappings. syntax:
98 m and u can be double-byte hex
103 !u -> make u illegal always
104 ~u -> make u illegal only as the first
105 part of a double-byte character.
107 #define VOLOPT_VETO 10 /* list of veto filespec */
108 #define VOLOPT_PREEXEC 11 /* preexec command */
109 #define VOLOPT_ROOTPREEXEC 12 /* root preexec command */
111 #define VOLOPT_POSTEXEC 13 /* postexec command */
112 #define VOLOPT_ROOTPOSTEXEC 14 /* root postexec command */
114 #define VOLOPT_ENCODING 15 /* mac encoding (pre OSX)*/
115 #define VOLOPT_MACCHARSET 16
116 #define VOLOPT_CNIDSCHEME 17
117 #define VOLOPT_ADOUBLE 18 /* adouble version */
122 #define VOLOPT_FORCEUID 19 /* force uid for username x */
123 #define VOLOPT_FORCEGID 20 /* force gid for group x */
124 #define VOLOPT_UMASK 21
126 #define VOLOPT_UMASK 19
127 #endif /* FORCE_UIDGID */
129 #define VOLOPT_MAX (VOLOPT_UMASK +1)
131 #define VOLOPT_NUM (VOLOPT_MAX + 1)
134 #define VOLOPT_DEFAULT ":DEFAULT:"
135 #define VOLOPT_DEFAULT_LEN 9
141 static __inline__ void volfree(struct vol_option *options,
142 const struct vol_option *save)
147 for (i = 0; i < VOLOPT_MAX; i++) {
148 if (options[i].c_value && (options[i].c_value != save[i].c_value))
149 free(options[i].c_value);
152 for (i = 0; i < VOLOPT_MAX; i++) {
153 if (options[i].c_value)
154 free(options[i].c_value);
160 /* handle variable substitutions. here's what we understand:
161 * $b -> basename of path
162 * $c -> client ip/appletalk address
163 * $d -> volume pathname on server
164 * $f -> full name (whatever's in the gecos field)
167 * $s -> server name (hostname if it doesn't exist)
168 * $u -> username (guest is usually nobody)
169 * $v -> volume name (ADEID_NAME or basename if ADEID_NAME is empty)
170 * $z -> zone (may not exist)
173 #define is_var(a, b) (strncmp((a), (b), 2) == 0)
175 static char *volxlate(AFPObj *obj, char *dest, size_t destlen,
176 char *src, struct passwd *pwd, char *path)
186 dest = calloc(destlen +1, 1);
192 strncpy(dest, src, destlen);
193 if ((p = strchr(src, '$')) == NULL) /* nothing to do */
196 /* first part of the path. just forward to the next variable. */
197 len = MIN(p - src, destlen);
203 while (p && destlen > 0) {
204 /* now figure out what the variable is */
206 if (is_var(p, "$b")) {
208 if ((q = strrchr(path, '/')) == NULL)
210 else if (*(q + 1) != '\0')
213 } else if (is_var(p, "$c")) {
214 if (obj->proto == AFPPROTO_ASP) {
215 ASP asp = obj->handle;
217 len = sprintf(dest, "%u.%u", ntohs(asp->asp_sat.sat_addr.s_net),
218 asp->asp_sat.sat_addr.s_node);
222 } else if (obj->proto == AFPPROTO_DSI) {
223 DSI *dsi = obj->handle;
225 len = sprintf(dest, "%s:%u", inet_ntoa(dsi->client.sin_addr),
226 ntohs(dsi->client.sin_port));
230 } else if (is_var(p, "$d")) {
232 } else if (is_var(p, "$f")) {
233 if ((q = strchr(pwd->pw_gecos, ',')))
236 } else if (is_var(p, "$g")) {
237 struct group *grp = getgrgid(pwd->pw_gid);
240 } else if (is_var(p, "$h")) {
241 q = obj->options.hostname;
242 } else if (is_var(p, "$s")) {
245 else if (obj->options.server) {
246 q = obj->options.server;
248 q = obj->options.hostname;
249 } else if (is_var(p, "$u")) {
251 } else if (is_var(p, "$v")) {
256 if (ad_open(path, ADFLAGS_HF, O_RDONLY, 0, &ad) < 0)
259 if ((len = MIN(ad_getentrylen(&ad, ADEID_NAME), destlen)) > 0) {
260 memcpy(dest, ad_entry(&ad, ADEID_NAME), len);
261 ad_close(&ad, ADFLAGS_HF);
265 ad_close(&ad, ADFLAGS_HF);
266 no_volname: /* simple basename */
267 if ((q = strrchr(path, '/')) == NULL)
269 else if (*(q + 1) != '\0')
273 } else if (is_var(p, "$z")) {
275 } else if (is_var(p, "$$")) {
280 /* copy the stuff over. if we don't understand something that we
281 * should, just skip it over. */
283 len = MIN(p == q ? 2 : strlen(q), destlen);
284 strncpy(dest, q, len);
289 /* stuff up to next $ */
291 p = strchr(src, '$');
292 len = p ? MIN(p - src, destlen) : destlen;
294 strncpy(dest, src, len);
302 /* to make sure that val is valid, make sure to select an opt that
304 static int optionok(const char *buf, const char *opt, const char *val)
306 if (!strstr(buf,opt))
313 static __inline__ char *get_codepage_path(const char *path, const char *name)
319 page = (char *) malloc((len = strlen(path)) + strlen(name) + 2);
322 if (path[len - 1] != '/') /* add a / */
330 /* debug: show which codepage directory we are using */
331 LOG(log_debug, logtype_afpd, "using codepage directory: %s", page);
336 /* -------------------- */
337 static void setoption(struct vol_option *options, struct vol_option *save, int opt, const char *val)
339 if (options[opt].c_value && (!save || options[opt].c_value != save[opt].c_value))
340 free(options[opt].c_value);
341 options[opt].c_value = strdup(val + 1);
344 /* ------------------------------------------
345 handle all the options. tmp can't be NULL. */
346 static void volset(struct vol_option *options, struct vol_option *save,
347 char *volname, int vlen,
348 const char *nlspath, const char *tmp)
352 val = strchr(tmp, ':');
354 /* we'll assume it's a volume name. */
355 strncpy(volname, tmp, vlen);
360 LOG(log_debug, logtype_afpd, "Parsing volset %s", val);
362 if (optionok(tmp, "allow:", val)) {
363 setoption(options, save, VOLOPT_ALLOW, val);
365 } else if (optionok(tmp, "deny:", val)) {
366 setoption(options, save, VOLOPT_DENY, val);
368 } else if (optionok(tmp, "rwlist:", val)) {
369 setoption(options, save, VOLOPT_RWLIST, val);
371 } else if (optionok(tmp, "rolist:", val)) {
372 setoption(options, save, VOLOPT_ROLIST, val);
374 } else if (optionok(tmp, "codepage:", val)) {
375 if (options[VOLOPT_CODEPAGE].c_value &&
376 (!save || options[VOLOPT_CODEPAGE].c_value != save[VOLOPT_CODEPAGE].c_value)) {
377 free(options[VOLOPT_CODEPAGE].c_value);
379 options[VOLOPT_CODEPAGE].c_value = get_codepage_path(nlspath, val + 1);
380 } else if (optionok(tmp, "volcharset:", val)) {
381 setoption(options, save, VOLOPT_ENCODING, val);
382 } else if (optionok(tmp, "maccharset:", val)) {
383 setoption(options, save, VOLOPT_MACCHARSET, val);
384 } else if (optionok(tmp, "veto:", val)) {
385 setoption(options, save, VOLOPT_VETO, val);
386 } else if (optionok(tmp, "cnidscheme:", val)) {
387 setoption(options, save, VOLOPT_CNIDSCHEME, val);
388 } else if (optionok(tmp, "casefold:", val)) {
389 if (strcasecmp(val + 1, "tolower") == 0)
390 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMLOWER;
391 else if (strcasecmp(val + 1, "toupper") == 0)
392 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMUPPER;
393 else if (strcasecmp(val + 1, "xlatelower") == 0)
394 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UUPPERMLOWER;
395 else if (strcasecmp(val + 1, "xlateupper") == 0)
396 options[VOLOPT_CASEFOLD].i_value = AFPVOL_ULOWERMUPPER;
397 } else if (optionok(tmp, "adouble:", val)) {
398 if (strcasecmp(val + 1, "v1") == 0)
399 options[VOLOPT_ADOUBLE].i_value = AD_VERSION1;
400 #if AD_VERSION == AD_VERSION2
401 else if (strcasecmp(val + 1, "v2") == 0)
402 options[VOLOPT_ADOUBLE].i_value = AD_VERSION2;
404 } else if (optionok(tmp, "options:", val)) {
407 if ((p = strtok(val + 1, ",")) == NULL) /* nothing */
411 if (strcasecmp(p, "prodos") == 0)
412 options[VOLOPT_FLAGS].i_value |= AFPVOL_A2VOL;
413 else if (strcasecmp(p, "mswindows") == 0) {
414 options[VOLOPT_FLAGS].i_value |= AFPVOL_MSWINDOWS;
415 if (!options[VOLOPT_CODEPAGE].c_value)
416 options[VOLOPT_CODEPAGE].c_value =
417 get_codepage_path(nlspath, MSWINDOWS_CODEPAGE);
419 } else if (strcasecmp(p, "crlf") == 0)
420 options[VOLOPT_FLAGS].i_value |= AFPVOL_CRLF;
421 else if (strcasecmp(p, "noadouble") == 0)
422 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOADOUBLE;
423 else if (strcasecmp(p, "ro") == 0)
424 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
425 else if (strcasecmp(p, "nohex") == 0)
426 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOHEX;
427 else if (strcasecmp(p, "usedots") == 0)
428 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS;
429 else if (strcasecmp(p, "limitsize") == 0)
430 options[VOLOPT_FLAGS].i_value |= AFPVOL_LIMITSIZE;
431 /* support for either "dropbox" or "dropkludge" */
432 else if (strcasecmp(p, "dropbox") == 0)
433 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
434 else if (strcasecmp(p, "dropkludge") == 0)
435 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
436 else if (strcasecmp(p, "nofileid") == 0)
437 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOFILEID;
438 else if (strcasecmp(p, "nostat") == 0)
439 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOSTAT;
440 else if (strcasecmp(p, "preexec_close") == 0)
441 options[VOLOPT_PREEXEC].i_value = 1;
442 else if (strcasecmp(p, "root_preexec_close") == 0)
443 options[VOLOPT_ROOTPREEXEC].i_value = 1;
444 else if (strcasecmp(p, "upriv") == 0)
445 options[VOLOPT_FLAGS].i_value |= AFPVOL_UNIX_PRIV;
447 p = strtok(NULL, ",");
450 } else if (optionok(tmp, "dbpath:", val)) {
451 setoption(options, save, VOLOPT_DBPATH, val);
453 } else if (optionok(tmp, "umask:", val)) {
454 options[VOLOPT_UMASK].i_value = (int)strtol(val, (char **)NULL, 8);
455 } else if (optionok(tmp, "mapchars:",val)) {
456 setoption(options, save, VOLOPT_MAPCHARS, val);
458 } else if (optionok(tmp, "password:", val)) {
459 setoption(options, save, VOLOPT_PASSWORD, val);
463 /* this code allows forced uid/gid per volume settings */
464 } else if (optionok(tmp, "forceuid:", val)) {
465 setoption(options, save, VOLOPT_FORCEUID, val);
466 } else if (optionok(tmp, "forcegid:", val)) {
467 setoption(options, save, VOLOPT_FORCEGID, val);
469 #endif /* FORCE_UIDGID */
470 } else if (optionok(tmp, "root_preexec:", val)) {
471 setoption(options, save, VOLOPT_ROOTPREEXEC, val);
473 } else if (optionok(tmp, "preexec:", val)) {
474 setoption(options, save, VOLOPT_PREEXEC, val);
476 } else if (optionok(tmp, "root_postexec:", val)) {
477 setoption(options, save, VOLOPT_ROOTPOSTEXEC, val);
479 } else if (optionok(tmp, "postexec:", val)) {
480 setoption(options, save, VOLOPT_POSTEXEC, val);
483 /* ignore unknown options */
484 LOG(log_debug, logtype_afpd, "ignoring unknown volume option: %s", tmp);
489 /* ----------------- */
490 static void showvol(const char *name)
493 for ( volume = Volumes; volume; volume = volume->v_next ) {
494 if ( !strcasecmp( volume->v_name, name ) && volume->v_hide) {
501 /* ------------------------------- */
502 static int creatvol(AFPObj *obj, struct passwd *pwd,
503 char *path, char *name,
504 struct vol_option *options,
505 const int user /* user defined volume */
512 if ( name == NULL || *name == '\0' ) {
513 if ((name = strrchr( path, '/' )) == NULL) {
514 return -1; /* Obviously not a fully qualified path */
517 /* if you wish to share /, you need to specify a name. */
522 for ( volume = Volumes; volume; volume = volume->v_next ) {
523 if ( strcasecmp( volume->v_name, name ) == 0 ) {
524 if (volume->v_deleted) {
528 return -1; /* Won't be able to access it, anyway... */
533 vlen = strlen( name );
534 if ( vlen > AFPVOL_NAMELEN ) {
535 vlen = AFPVOL_NAMELEN;
536 name[AFPVOL_NAMELEN] = '\0';
539 if (!( volume = (struct vol *)calloc(1, sizeof( struct vol ))) ) {
540 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
543 if (! ( volume->v_name = (char *)malloc( vlen + 1 ) ) ) {
544 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
548 if (!( volume->v_path = (char *)malloc( strlen( path ) + 1 )) ) {
549 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
550 free(volume->v_name);
554 volume->v_hide = hide;
555 strcpy( volume->v_name, name);
556 strcpy( volume->v_path, path );
560 #endif /* __svr4__ */
561 volume->v_vid = lastvid++;
565 /* should we casefold? */
566 volume->v_casefold = options[VOLOPT_CASEFOLD].i_value;
568 /* shift in some flags */
569 volume->v_flags = options[VOLOPT_FLAGS].i_value;
571 /* read in the code pages */
572 if (options[VOLOPT_CODEPAGE].c_value)
573 codepage_read(volume, options[VOLOPT_CODEPAGE].c_value);
575 if (options[VOLOPT_PASSWORD].c_value)
576 volume->v_password = strdup(options[VOLOPT_PASSWORD].c_value);
578 if (options[VOLOPT_VETO].c_value)
579 volume->v_veto = strdup(options[VOLOPT_VETO].c_value);
581 if (options[VOLOPT_ENCODING].c_value)
582 volume->v_volcodepage = strdup(options[VOLOPT_ENCODING].c_value);
584 if (options[VOLOPT_MACCHARSET].c_value)
585 volume->v_maccodepage = strdup(options[VOLOPT_MACCHARSET].c_value);
587 if (options[VOLOPT_DBPATH].c_value)
588 volume->v_dbpath = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_DBPATH].c_value, pwd, path);
590 if (options[VOLOPT_CNIDSCHEME].c_value)
591 volume->v_cnidscheme = strdup(options[VOLOPT_CNIDSCHEME].c_value);
593 if (options[VOLOPT_UMASK].i_value)
594 volume->v_umask = (mode_t)options[VOLOPT_UMASK].i_value;
596 if (options[VOLOPT_ADOUBLE].i_value)
597 volume->v_adouble = options[VOLOPT_ADOUBLE].i_value;
599 volume->v_adouble = AD_VERSION;
601 if (options[VOLOPT_FORCEUID].c_value) {
602 volume->v_forceuid = strdup(options[VOLOPT_FORCEUID].c_value);
604 volume->v_forceuid = NULL; /* set as null so as to return 0 later on */
607 if (options[VOLOPT_FORCEGID].c_value) {
608 volume->v_forcegid = strdup(options[VOLOPT_FORCEGID].c_value);
610 volume->v_forcegid = NULL; /* set as null so as to return 0 later on */
614 if (options[VOLOPT_PREEXEC].c_value)
615 volume->v_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_PREEXEC].c_value, pwd, path);
616 volume->v_preexec_close = options[VOLOPT_PREEXEC].i_value;
618 if (options[VOLOPT_POSTEXEC].c_value)
619 volume->v_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_POSTEXEC].c_value, pwd, path);
621 if (options[VOLOPT_ROOTPREEXEC].c_value)
622 volume->v_root_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPREEXEC].c_value, pwd, path);
623 volume->v_root_preexec_close = options[VOLOPT_ROOTPREEXEC].i_value;
625 if (options[VOLOPT_ROOTPOSTEXEC].c_value)
626 volume->v_root_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPOSTEXEC].c_value, pwd, path);
630 volume->v_next = Volumes;
635 /* ---------------- */
636 static char *myfgets( buf, size, fp )
645 while ((EOF != ( c = getc( fp )) ) && ( size > 0 )) {
646 if ( c == '\n' || c == '\r' ) {
664 /* check access list. this function wants something of the following
666 * @group,name,name2,@group2,name3
668 * a NULL argument allows everybody to have access.
669 * we return three things:
671 * 0: list exists, but name isn't in it
675 #ifndef NO_REAL_USER_NAME
676 /* authentication is case insensitive
677 * FIXME should we do the same with group name?
679 #define access_strcmp strcasecmp
682 #define access_strcmp strcmp
686 static int accessvol(args, name)
690 char buf[MAXPATHLEN + 1], *p;
696 strncpy(buf, args, sizeof(buf));
697 if ((p = strtok(buf, ",")) == NULL) /* nothing, return okay */
701 if (*p == '@') { /* it's a group */
702 if ((gr = getgrnam(p + 1)) && gmem(gr->gr_gid))
704 } else if (access_strcmp(p, name) == 0) /* it's a user name */
706 p = strtok(NULL, ",");
712 static void setextmap( ext, type, creator, user)
713 char *ext, *type, *creator;
719 if (Extmap == NULL) {
720 if (( Extmap = calloc(1, sizeof( struct extmap ))) == NULL ) {
721 LOG(log_error, logtype_afpd, "setextmap: calloc: %s", strerror(errno) );
726 for ( em = Extmap, cnt = 0; em->em_ext; em++, cnt++) {
727 if ( (strdiacasecmp( em->em_ext, ext )) == 0 ) {
732 if ( em->em_ext == NULL ) {
733 if (!(Extmap = realloc( Extmap, sizeof( struct extmap ) * (cnt +2))) ) {
734 LOG(log_error, logtype_afpd, "setextmap: realloc: %s", strerror(errno) );
737 (Extmap +cnt +1)->em_ext = NULL;
739 } else if ( !user ) {
745 if (!(em->em_ext = strdup( ext))) {
746 LOG(log_error, logtype_afpd, "setextmap: strdup: %s", strerror(errno) );
750 if ( *type == '\0' ) {
751 memcpy(em->em_type, "????", sizeof( em->em_type ));
753 memcpy(em->em_type, type, sizeof( em->em_type ));
755 if ( *creator == '\0' ) {
756 memcpy(em->em_creator, "UNIX", sizeof( em->em_creator ));
758 memcpy(em->em_creator, creator, sizeof( em->em_creator ));
762 /* -------------------------- */
763 static int extmap_cmp(const void *map1, const void *map2)
765 const struct extmap *em1 = map1;
766 const struct extmap *em2 = map2;
767 return strdiacasecmp(em1->em_ext, em2->em_ext);
770 static void sortextmap( void)
775 if ((em = Extmap) == NULL) {
783 qsort(Extmap, Extmap_cnt, sizeof(struct extmap), extmap_cmp);
788 /* ----------------------
790 static void free_extmap( void)
800 /* ----------------------
802 static int volfile_changed(struct afp_volume_name *p)
812 if (!stat( name, &st) && st.st_mtime > p->mtime) {
813 p->mtime = st.st_mtime;
819 /* ----------------------
820 * Read a volume configuration file and add the volumes contained within to
821 * the global volume list. If p2 is non-NULL, the file that is opened is
824 * Lines that begin with # and blank lines are ignored.
825 * Volume lines are of the form:
826 * <unix path> [<volume name>] [allow:<user>,<@group>,...] \
827 * [codepage:<file>] [casefold:<num>]
828 * <extension> TYPE [CREATOR]
830 static int readvolfile(obj, p1, p2, user, pwent)
832 struct afp_volume_name *p1;
835 struct passwd *pwent;
838 char path[ MAXPATHLEN + 1], tmp[ MAXPATHLEN + 1],
839 volname[ AFPVOL_NAMELEN + 1 ], buf[ BUFSIZ ],
840 type[ 5 ], creator[ 5 ];
843 struct vol_option options[VOLOPT_NUM], save_options[VOLOPT_NUM];
851 strcpy( path, p1->name );
858 p1->full_name = strdup(path);
861 if (NULL == ( fp = fopen( path, "r" )) ) {
865 if (fd != -1 && !fstat( fd, &st) ) {
866 p1->mtime = st.st_mtime;
869 memset(save_options, 0, sizeof(save_options));
870 while ( myfgets( buf, sizeof( buf ), fp ) != NULL ) {
871 initline( strlen( buf ), buf );
872 parseline( sizeof( path ) - 1, path );
879 /* change the default options for this file */
880 if (strncmp(path, VOLOPT_DEFAULT, VOLOPT_DEFAULT_LEN) == 0) {
882 for (i = 0; i < VOLOPT_NUM; i++) {
883 if (parseline( sizeof( path ) - VOLOPT_DEFAULT_LEN - 1,
884 path + VOLOPT_DEFAULT_LEN) < 0)
886 volset(save_options, NULL, tmp, sizeof(tmp) - 1,
887 obj->options.nlspath, path + VOLOPT_DEFAULT_LEN);
893 if (( p = strchr( path, '/' )) != NULL ) {
901 if ( u == NULL || *u == '\0' || ( pw = getpwnam( u )) == NULL ) {
904 strcpy( tmp, pw->pw_dir );
905 if ( p != NULL && *p != '\0' ) {
909 /* Tag a user's home directory with their umask. Note, this will
910 * be overwritten if the user actually specifies a umask: option
911 * for a '~' volume. */
912 save_options[VOLOPT_UMASK].i_value = obj->options.save_mask;
916 /* send path through variable substitution */
917 if (*path != '~') /* need to copy path to tmp */
920 pwent = getpwnam(obj->username);
921 volxlate(obj, path, sizeof(path) - 1, tmp, pwent, NULL);
923 /* this is sort of braindead. basically, i want to be
924 * able to specify things in any order, but i don't want to
925 * re-write everything.
927 * currently we have options:
935 * options:prodos,crlf,noadouble,ro...
940 * namemask:x,y,!z (not implemented yet)
942 memcpy(options, save_options, sizeof(options));
945 /* read in up to VOLOP_NUM possible options */
946 for (i = 0; i < VOLOPT_NUM; i++) {
947 if (parseline( sizeof( tmp ) - 1, tmp ) < 0)
950 volset(options, save_options, volname, sizeof(volname) - 1,obj->options.nlspath, tmp);
953 /* check allow/deny lists:
954 allow -> either no list (-1), or in list (1)
955 deny -> either no list (-1), or not in list (0) */
956 if (accessvol(options[VOLOPT_ALLOW].c_value, obj->username) &&
957 (accessvol(options[VOLOPT_DENY].c_value, obj->username) < 1)) {
959 /* handle read-only behaviour. semantics:
960 * 1) neither the rolist nor the rwlist exist -> rw
961 * 2) rolist exists -> ro if user is in it.
962 * 3) rwlist exists -> ro unless user is in it. */
963 if (((options[VOLOPT_FLAGS].i_value & AFPVOL_RO) == 0) &&
964 ((accessvol(options[VOLOPT_ROLIST].c_value,
965 obj->username) == 1) ||
966 !accessvol(options[VOLOPT_RWLIST].c_value,
968 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
970 /* do variable substitution for volname */
971 volxlate(obj, tmp, sizeof(tmp) - 1, volname, pwent, path);
972 creatvol(obj, pwent, path, tmp, options, p2 != NULL);
974 volfree(options, save_options);
978 parseline( sizeof( type ) - 1, type );
979 parseline( sizeof( creator ) - 1, creator );
980 setextmap( path, type, creator, user);
987 volfree(save_options, NULL);
989 if ( fclose( fp ) != 0 ) {
990 LOG(log_error, logtype_afpd, "readvolfile: fclose: %s", strerror(errno) );
996 /* ------------------------------- */
997 static void volume_free(struct vol *vol)
1003 free(vol->v_password);
1005 free(vol->v_volcodepage);
1006 free(vol->v_maccodepage);
1007 free(vol->v_dbpath);
1009 free(vol->v_forceuid);
1010 free(vol->v_forcegid);
1011 #endif /* FORCE_UIDGID */
1014 /* ------------------------------- */
1015 static void free_volumes(void )
1018 struct vol *nvol, *ovol;
1020 for ( vol = Volumes; vol; vol = vol->v_next ) {
1021 if (( vol->v_flags & AFPVOL_OPEN ) ) {
1028 for ( vol = Volumes, ovol = NULL; vol; vol = nvol) {
1031 if (vol->v_name == NULL) {
1032 if (Volumes == vol) {
1039 ovol->v_next = nvol;
1049 /* ------------------------------- */
1050 static void volume_unlink(struct vol *volume)
1052 struct vol *vol, *ovol, *nvol;
1054 if (volume == Volumes) {
1055 Volumes = Volumes->v_next;
1058 for ( vol = Volumes, ovol = NULL; vol; vol = nvol) {
1061 if (vol == volume) {
1066 ovol->v_next = nvol;
1077 static int getvolspace( vol, bfree, btotal, xbfree, xbtotal, bsize )
1079 u_int32_t *bfree, *btotal, *bsize;
1080 VolSpace *xbfree, *xbtotal;
1084 #ifndef NO_QUOTA_SUPPORT
1085 VolSpace qfree, qtotal;
1088 spaceflag = AFPVOL_GVSMASK & vol->v_flags;
1089 /* report up to 2GB if afp version is < 2.2 (4GB if not) */
1090 maxsize = (vol->v_flags & AFPVOL_A2VOL) ? 0x01fffe00 :
1091 (((afp_version < 22) || (vol->v_flags & AFPVOL_LIMITSIZE))
1092 ? 0x7fffffffL : 0xffffffffL);
1095 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_AFSGVS ) {
1096 if ( afs_getvolspace( vol, xbfree, xbtotal, bsize ) == AFP_OK ) {
1097 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_AFSGVS;
1098 goto getvolspace_done;
1103 if (( rc = ustatfs_getvolspace( vol, xbfree, xbtotal,
1104 bsize)) != AFP_OK ) {
1108 #define min(a,b) ((a)<(b)?(a):(b))
1109 #ifndef NO_QUOTA_SUPPORT
1110 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_UQUOTA ) {
1111 if ( uquota_getvolspace( vol, &qfree, &qtotal, *bsize ) == AFP_OK ) {
1112 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_UQUOTA;
1113 *xbfree = min(*xbfree, qfree);
1114 *xbtotal = min( *xbtotal, qtotal);
1115 goto getvolspace_done;
1119 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_USTATFS;
1122 *bfree = min( *xbfree, maxsize);
1123 *btotal = min( *xbtotal, maxsize);
1127 /* ----------------------- */
1128 static int getvolparams( bitmap, vol, st, buf, buflen )
1136 int bit = 0, isad = 1;
1139 u_int32_t bfree, btotal, bsize;
1140 VolSpace xbfree, xbtotal; /* extended bytes */
1141 char *data, *nameoff = NULL;
1144 /* courtesy of jallison@whistle.com:
1145 * For MacOS8.x support we need to create the
1146 * .Parent file here if it doesn't exist. */
1148 ad_init(&ad, vol->v_adouble);
1149 if ( ad_open( vol->v_path, vol_noadouble(vol) |
1150 ADFLAGS_HF|ADFLAGS_DIR, O_RDWR | O_CREAT,
1154 } else if (ad_get_HF_flags( &ad ) & O_CREAT) {
1155 slash = strrchr( vol->v_path, '/' );
1159 slash = vol->v_path;
1161 ad_setentrylen( &ad, ADEID_NAME, strlen( slash ));
1162 memcpy(ad_entry( &ad, ADEID_NAME ), slash,
1163 ad_getentrylen( &ad, ADEID_NAME ));
1164 ad_setdate(&ad, AD_DATE_CREATE | AD_DATE_UNIX, st->st_mtime);
1165 ad_flush(&ad, ADFLAGS_HF);
1168 if (( bitmap & ( (1<<VOLPBIT_BFREE)|(1<<VOLPBIT_BTOTAL) |
1169 (1<<VOLPBIT_XBFREE)|(1<<VOLPBIT_XBTOTAL) |
1170 (1<<VOLPBIT_BSIZE)) ) != 0 ) {
1171 if ( getvolspace( vol, &bfree, &btotal, &xbfree, &xbtotal,
1174 ad_close( &ad, ADFLAGS_HF );
1176 return( AFPERR_PARAM );
1181 while ( bitmap != 0 ) {
1182 while (( bitmap & 1 ) == 0 ) {
1190 if (0 == (vol->v_flags & AFPVOL_NOFILEID) && vol->v_cdb != NULL &&
1191 (vol->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1192 ashort = VOLPBIT_ATTR_FILEID;
1194 /* check for read-only.
1195 * NOTE: we don't actually set the read-only flag unless
1196 * it's passed in that way as it's possible to mount
1197 * a read-write filesystem under a read-only one. */
1198 if ((vol->v_flags & AFPVOL_RO) ||
1199 ((utime(vol->v_path, NULL) < 0) && (errno == EROFS))) {
1200 ashort |= VOLPBIT_ATTR_RO;
1202 ashort |= VOLPBIT_ATTR_CATSEARCH;
1203 if (afp_version >= 30) {
1204 ashort |= VOLPBIT_ATTR_UTF8;
1205 if (vol->v_flags & AFPVOL_UNIX_PRIV)
1206 ashort |= VOLPBIT_ATTR_UNIXPRIV;
1208 ashort = htons(ashort);
1209 memcpy(data, &ashort, sizeof( ashort ));
1210 data += sizeof( ashort );
1214 ashort = htons( AFPVOLSIG_DEFAULT );
1215 memcpy(data, &ashort, sizeof( ashort ));
1216 data += sizeof( ashort );
1219 case VOLPBIT_CDATE :
1220 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
1221 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1222 memcpy(data, &aint, sizeof( aint ));
1223 data += sizeof( aint );
1226 case VOLPBIT_MDATE :
1227 if ( st->st_mtime > vol->v_time ) {
1228 vol->v_time = st->st_mtime;
1229 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1231 aint = AD_DATE_FROM_UNIX(vol->v_time);
1233 memcpy(data, &aint, sizeof( aint ));
1234 data += sizeof( aint );
1237 case VOLPBIT_BDATE :
1238 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1239 aint = AD_DATE_START;
1240 memcpy(data, &aint, sizeof( aint ));
1241 data += sizeof( aint );
1245 memcpy(data, &vol->v_vid, sizeof( vol->v_vid ));
1246 data += sizeof( vol->v_vid );
1249 case VOLPBIT_BFREE :
1250 bfree = htonl( bfree );
1251 memcpy(data, &bfree, sizeof( bfree ));
1252 data += sizeof( bfree );
1255 case VOLPBIT_BTOTAL :
1256 btotal = htonl( btotal );
1257 memcpy(data, &btotal, sizeof( btotal ));
1258 data += sizeof( btotal );
1261 #ifndef NO_LARGE_VOL_SUPPORT
1262 case VOLPBIT_XBFREE :
1263 xbfree = hton64( xbfree );
1264 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1265 bcopy(&xbfree, data, sizeof(xbfree));
1266 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1267 memcpy(data, &xbfree, sizeof( xbfree ));
1268 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1269 data += sizeof( xbfree );
1272 case VOLPBIT_XBTOTAL :
1273 xbtotal = hton64( xbtotal );
1274 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1275 bcopy(&xbtotal, data, sizeof(xbtotal));
1276 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1277 memcpy(data, &xbtotal, sizeof( xbtotal ));
1278 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1279 data += sizeof( xbfree );
1281 #endif /* ! NO_LARGE_VOL_SUPPORT */
1285 data += sizeof( u_int16_t );
1288 case VOLPBIT_BSIZE: /* block size */
1289 bsize = htonl(bsize);
1290 memcpy(data, &bsize, sizeof(bsize));
1291 data += sizeof(bsize);
1296 ad_close( &ad, ADFLAGS_HF );
1298 return( AFPERR_BITMAP );
1304 ashort = htons( data - buf );
1305 memcpy(nameoff, &ashort, sizeof( ashort ));
1306 aint = strlen( vol->v_name );
1308 memcpy(data, vol->v_name, aint );
1312 ad_close( &ad, ADFLAGS_HF );
1314 *buflen = data - buf;
1318 /* ------------------------- */
1319 int static stat_vol(u_int16_t bitmap, struct vol *vol, char *rbuf, int *rbuflen)
1324 if ( stat( vol->v_path, &st ) < 0 ) {
1326 return( AFPERR_PARAM );
1329 buflen = *rbuflen - sizeof( bitmap );
1330 if (( ret = getvolparams( bitmap, vol, &st,
1331 rbuf + sizeof( bitmap ), &buflen )) != AFP_OK ) {
1335 *rbuflen = buflen + sizeof( bitmap );
1336 bitmap = htons( bitmap );
1337 memcpy(rbuf, &bitmap, sizeof( bitmap ));
1342 /* ------------------------------- */
1343 void load_volumes(AFPObj *obj)
1345 struct passwd *pwent;
1350 /* check files date */
1351 if (obj->options.defaultvol.loaded) {
1352 changed = volfile_changed(&obj->options.defaultvol);
1354 if (obj->options.systemvol.loaded) {
1355 changed |= volfile_changed(&obj->options.systemvol);
1357 if (obj->options.uservol.loaded) {
1358 changed |= volfile_changed(&obj->options.uservol);
1367 pwent = getpwnam(obj->username);
1368 if ( (obj->options.flags & OPTION_USERVOLFIRST) == 0 ) {
1369 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent);
1372 if ((*obj->username == '\0') || (obj->options.flags & OPTION_NOUSERVOL)) {
1373 readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent);
1376 * Read user's AppleVolumes or .AppleVolumes file
1377 * If neither are readable, read the default volumes file. if
1378 * that doesn't work, create a user share.
1380 obj->options.uservol.name = strdup(pwent->pw_dir);
1381 if ( readvolfile(obj, &obj->options.uservol, "AppleVolumes", 1, pwent) < 0 &&
1382 readvolfile(obj, &obj->options.uservol, ".AppleVolumes", 1, pwent) < 0 &&
1383 readvolfile(obj, &obj->options.uservol, "applevolumes", 1, pwent) < 0 &&
1384 readvolfile(obj, &obj->options.uservol, ".applevolumes", 1, pwent) < 0 &&
1385 obj->options.defaultvol.name != NULL ) {
1386 if (readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent) < 0)
1387 creatvol(obj, pwent, pwent->pw_dir, NULL, NULL, 1);
1390 if ( obj->options.flags & OPTION_USERVOLFIRST ) {
1391 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent );
1395 /* ------------------------------- */
1396 int afp_getsrvrparms(obj, ibuf, ibuflen, rbuf, rbuflen )
1399 int ibuflen, *rbuflen;
1411 for ( vcnt = 0, volume = Volumes; volume; volume = volume->v_next ) {
1412 if (!(volume->v_flags & AFPVOL_NOSTAT)) {
1413 if ( stat( volume->v_path, &st ) < 0 ) {
1414 LOG(log_info, logtype_afpd, "afp_getsrvrparms: stat %s: %s",
1415 volume->v_path, strerror(errno) );
1416 continue; /* can't access directory */
1418 if (!S_ISDIR(st.st_mode)) {
1419 continue; /* not a dir */
1422 if (volume->v_hide) {
1423 continue; /* config file changed but the volume was mounted */
1426 /* set password bit if there's a volume password */
1427 *data = (volume->v_password) ? AFPSRVR_PASSWD : 0;
1429 /* Apple 2 clients running ProDOS-8 expect one volume to have
1430 bit 0 of this byte set. They will not recognize anything
1431 on the server unless this is the case. I have not
1432 completely worked this out, but it's related to booting
1433 from the server. Support for that function is a ways
1434 off.. <shirsch@ibm.net> */
1435 *data |= (volume->v_flags & AFPVOL_A2VOL) ? AFPSRVR_CONFIGINFO : 0;
1436 *data++ |= 0; /* UNIX PRIVS BIT ..., OSX doesn't seem to use it, so we don't either */
1437 len = strlen( volume->v_name );
1439 memcpy(data, volume->v_name, len );
1444 *rbuflen = data - rbuf;
1446 if ( gettimeofday( &tv, 0 ) < 0 ) {
1447 LOG(log_error, logtype_afpd, "afp_getsrvrparms: gettimeofday: %s", strerror(errno) );
1449 return AFPERR_PARAM;
1451 tv.tv_sec = AD_DATE_FROM_UNIX(tv.tv_sec);
1452 memcpy(data, &tv.tv_sec, sizeof( u_int32_t));
1453 data += sizeof( u_int32_t);
1458 /* -------------------------
1459 * we are the user here
1461 int afp_openvol(obj, ibuf, ibuflen, rbuf, rbuflen )
1464 int ibuflen, *rbuflen;
1475 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1476 bitmap = ntohs( bitmap );
1477 ibuf += sizeof( bitmap );
1478 if (( bitmap & (1<<VOLPBIT_VID)) == 0 ) {
1480 return AFPERR_BITMAP;
1483 len = (unsigned char)*ibuf++;
1484 volname = obj->oldtmp;
1485 memcpy(volname, ibuf, len );
1486 *(volname + len) = '\0';
1488 if ((len + 1) & 1) /* pad to an even boundary */
1493 for ( volume = Volumes; volume; volume = volume->v_next ) {
1494 if ( strcasecmp( volname, volume->v_name ) == 0 ) {
1499 if ( volume == NULL ) {
1501 return AFPERR_PARAM;
1504 /* check for a volume password */
1505 if (volume->v_password && strncmp(ibuf, volume->v_password, VOLPASSLEN)) {
1507 return AFPERR_ACCESS;
1510 if (( volume->v_flags & AFPVOL_OPEN ) ) {
1511 /* the volume is already open */
1513 set_uidgid ( volume );
1515 return stat_vol(bitmap, volume, rbuf, rbuflen);
1518 /* initialize volume variables
1521 if (afp_version >= 30) {
1522 volume->max_filename = 255;
1525 volume->max_filename = MACFILELEN;
1528 volume->v_dir = volume->v_root = NULL;
1530 /* FIXME unix name != mac name */
1531 if ((dir = dirnew(volume->v_name, volume->v_name) ) == NULL) {
1532 LOG(log_error, logtype_afpd, "afp_openvol: malloc: %s", strerror(errno) );
1537 dir->d_did = DIRDID_ROOT;
1538 dir->d_color = DIRTREE_COLOR_BLACK; /* root node is black */
1539 volume->v_dir = volume->v_root = dir;
1540 volume->v_flags |= AFPVOL_OPEN;
1541 volume->v_cdb = NULL;
1543 if (volume->v_root_preexec) {
1544 if ((ret = afprun(1, volume->v_root_preexec, NULL)) && volume->v_root_preexec_close) {
1545 LOG(log_error, logtype_afpd, "afp_openvol: root preexec : %d", ret );
1552 set_uidgid ( volume );
1555 if (volume->v_preexec) {
1556 if ((ret = afprun(0, volume->v_preexec, NULL)) && volume->v_preexec_close) {
1557 LOG(log_error, logtype_afpd, "afp_openvol: preexec : %d", ret );
1563 if ( stat( volume->v_path, &st ) < 0 ) {
1568 if ( chdir( volume->v_path ) < 0 ) {
1572 curdir = volume->v_dir;
1573 if (volume->v_cnidscheme == NULL) {
1574 volume->v_cnidscheme = strdup(DEFAULT_CNID_SCHEME);
1575 LOG(log_warning, logtype_afpd, "Warning: No CNID scheme for volume %s. Using default.",
1578 if (volume->v_dbpath)
1579 volume->v_cdb = cnid_open (volume->v_dbpath, volume->v_umask, volume->v_cnidscheme);
1580 if (volume->v_cdb == NULL)
1581 volume->v_cdb = cnid_open (volume->v_path, volume->v_umask, volume->v_cnidscheme);
1582 if (volume->v_cdb == NULL) {
1583 volume->v_cdb = cnid_open (volume->v_path, volume->v_umask, DEFAULT_CNID_SCHEME);
1584 LOG(log_error, logtype_afpd, "Error: invalid CNID backend for %s: %s", volume->v_path,
1585 volume->v_cnidscheme);
1587 if (volume->v_cdb == NULL) {
1588 LOG(log_error, logtype_afpd, "Fatal error: cannot open CNID for %s", volume->v_path);
1595 if (!volume->v_volcodepage)
1596 volume->v_volcodepage = strdup("UTF8");
1598 if ( (charset_t) -1 == ( volume->v_volcharset = add_charset(volume->v_volcodepage)) ) {
1599 LOG (log_error, logtype_afpd, "Setting codepage %s as volume codepage failed", volume->v_volcodepage);
1604 if ( NULL == ( volume->v_vol = find_charset_functions(volume->v_volcodepage)) || volume->v_vol->flags & CHARSET_ICONV ) {
1605 LOG (log_warning, logtype_afpd, "WARNING: volume encoding %s is *not* supported by netatalk, expect problems !!!!", volume->v_volcodepage);
1608 if (!volume->v_maccodepage)
1609 volume->v_maccodepage = strdup(obj->options.maccodepage);
1611 if ( (charset_t) -1 == ( volume->v_maccharset = add_charset(volume->v_maccodepage)) ) {
1612 LOG (log_error, logtype_afpd, "Setting codepage %s as mac codepage failed", volume->v_maccodepage);
1617 if ( NULL == ( volume->v_mac = find_charset_functions(volume->v_maccodepage)) || ! (volume->v_mac->flags & CHARSET_CLIENT) ) {
1618 LOG (log_error, logtype_afpd, "Fatal error: mac charset %s not supported", volume->v_maccodepage);
1623 ret = stat_vol(bitmap, volume, rbuf, rbuflen);
1624 if (ret == AFP_OK) {
1626 * If you mount a volume twice, the second time the trash appears on
1627 * the desk-top. That's because the Mac remembers the DID for the
1628 * trash (even for volumes in different zones, on different servers).
1629 * Just so this works better, we prime the DID cache with the trash,
1630 * fixing the trash at DID 17.
1631 * FIXME (RL): should it be done inside a CNID backend ? (always returning Trash DID when asked) ?
1633 if ((volume->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1635 /* FIXME find db time stamp */
1636 if (!cnid_getstamp(volume->v_cdb, volume->v_stamp, sizeof(volume->v_stamp)) ) {
1637 LOG(log_error, logtype_afpd, "stamp %x", *(unsigned *)volume->v_stamp);
1641 cname( volume, volume->v_dir, &p );
1647 if (volume->v_dir) {
1648 dirfree( volume->v_dir );
1649 volume->v_dir = volume->v_root = NULL;
1652 volume->v_flags &= ~AFPVOL_OPEN;
1653 if (volume->v_cdb != NULL) {
1654 cnid_close(volume->v_cdb);
1655 volume->v_cdb = NULL;
1661 /* ------------------------- */
1662 static void closevol(struct vol *vol)
1667 dirfree( vol->v_root );
1669 if (vol->v_cdb != NULL) {
1670 cnid_close(vol->v_cdb);
1674 if (vol->v_postexec) {
1675 afprun(0, vol->v_postexec, NULL);
1677 if (vol->v_root_postexec) {
1678 afprun(1, vol->v_root_postexec, NULL);
1682 /* ------------------------- */
1683 void close_all_vol(void)
1687 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
1688 if ( ovol->v_flags & AFPVOL_OPEN ) {
1689 ovol->v_flags &= ~AFPVOL_OPEN;
1695 /* ------------------------- */
1696 int afp_closevol(obj, ibuf, ibuflen, rbuf, rbuflen )
1699 int ibuflen, *rbuflen;
1701 struct vol *vol, *ovol;
1706 memcpy(&vid, ibuf, sizeof( vid ));
1707 if (NULL == ( vol = getvolbyvid( vid )) ) {
1708 return( AFPERR_PARAM );
1711 vol->v_flags &= ~AFPVOL_OPEN;
1712 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
1713 if ( ovol->v_flags & AFPVOL_OPEN ) {
1717 if ( ovol != NULL ) {
1718 /* Even if chdir fails, we can't say afp_closevol fails. */
1719 if ( chdir( ovol->v_path ) == 0 ) {
1720 curdir = ovol->v_dir;
1725 if (vol->v_deleted) {
1726 showvol(vol->v_name);
1733 /* ------------------------- */
1734 struct vol *getvolbyvid(const u_int16_t vid )
1738 for ( vol = Volumes; vol; vol = vol->v_next ) {
1739 if ( vid == vol->v_vid ) {
1743 if ( vol == NULL || ( vol->v_flags & AFPVOL_OPEN ) == 0 ) {
1749 #endif /* FORCE_UIDGID */
1754 /* ------------------------ */
1755 static int ext_cmp_key(const void *key, const void *obj)
1757 const char *p = key;
1758 const struct extmap *em = obj;
1759 return strdiacasecmp(p, em->em_ext);
1761 struct extmap *getextmap(const char *path)
1766 if (NULL == ( p = strrchr( path, '.' )) ) {
1767 return( Defextmap );
1770 if (!*p || !Extmap_cnt) {
1771 return( Defextmap );
1773 em = bsearch(p, Extmap, Extmap_cnt, sizeof(struct extmap), ext_cmp_key);
1777 return( Defextmap );
1781 /* ------------------------- */
1782 struct extmap *getdefextmap(void)
1784 return( Defextmap );
1787 /* --------------------------
1788 poll if a volume is changed by other processes.
1790 int pollvoltime(obj)
1797 if (!(afp_version > 21 && obj->options.server_notif))
1800 if ( gettimeofday( &tv, 0 ) < 0 )
1803 for ( vol = Volumes; vol; vol = vol->v_next ) {
1804 if ( (vol->v_flags & AFPVOL_OPEN) && vol->v_time + 30 < tv.tv_sec) {
1805 if ( !stat( vol->v_path, &st ) && vol->v_time != st.st_mtime ) {
1806 vol->v_time = st.st_mtime;
1807 if (!obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED))
1816 /* ------------------------- */
1817 void setvoltime(obj, vol )
1823 /* just looking at vol->v_time is broken seriously since updates
1824 * from other users afpd processes never are seen.
1825 * This is not the most elegant solution (a shared memory between
1826 * the afpd processes would come closer)
1829 if ( gettimeofday( &tv, 0 ) < 0 ) {
1830 LOG(log_error, logtype_afpd, "setvoltime: gettimeofday: %s", strerror(errno) );
1833 if( utime( vol->v_path, NULL ) < 0 ) {
1834 /* write of time failed ... probably a read only filesys,
1835 * where no other users can interfere, so there's no issue here
1839 /* a little granularity */
1840 if (vol->v_time < tv.tv_sec) {
1841 vol->v_time = tv.tv_sec;
1842 if (afp_version > 21 && obj->options.server_notif) {
1843 obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED);
1848 /* ------------------------- */
1849 int afp_getvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1852 int ibuflen, *rbuflen;
1855 u_int16_t vid, bitmap;
1858 memcpy(&vid, ibuf, sizeof( vid ));
1859 ibuf += sizeof( vid );
1860 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1861 bitmap = ntohs( bitmap );
1863 if (NULL == ( vol = getvolbyvid( vid )) ) {
1865 return( AFPERR_PARAM );
1868 return stat_vol(bitmap, vol, rbuf, rbuflen);
1871 /* ------------------------- */
1872 int afp_setvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1875 int ibuflen, *rbuflen;
1879 u_int16_t vid, bitmap;
1885 memcpy(&vid, ibuf, sizeof( vid ));
1886 ibuf += sizeof( vid );
1887 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1888 bitmap = ntohs( bitmap );
1889 ibuf += sizeof(bitmap);
1891 if (( vol = getvolbyvid( vid )) == NULL ) {
1892 return( AFPERR_PARAM );
1895 if (vol->v_flags & AFPVOL_RO)
1896 return AFPERR_VLOCK;
1898 /* we can only set the backup date. */
1899 if (bitmap != (1 << VOLPBIT_BDATE))
1900 return AFPERR_BITMAP;
1902 ad_init(&ad, vol->v_adouble);
1903 if ( ad_open( vol->v_path, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR,
1906 return AFPERR_VLOCK;
1908 return AFPERR_ACCESS;
1911 memcpy(&aint, ibuf, sizeof(aint));
1912 ad_setdate(&ad, AD_DATE_BACKUP, aint);
1913 ad_flush(&ad, ADFLAGS_HF);
1914 ad_close(&ad, ADFLAGS_HF);
1918 /* ------------------------- */
1919 int wincheck(const struct vol *vol, const char *path)
1923 if (!(vol->v_flags & AFPVOL_MSWINDOWS))
1926 /* empty paths are not allowed */
1927 if ((len = strlen(path)) == 0)
1930 /* leading or trailing whitespaces are not allowed, carriage returns
1931 * and probably other whitespace is okay, tabs are not allowed
1933 if ((path[0] == ' ') || (path[len-1] == ' '))
1936 /* certain characters are not allowed */
1937 if (strpbrk(path, MSWINDOWS_BADCHARS))
1940 /* everything else is okay */