2 * $Id: volume.c,v 1.51.2.7.2.4 2003-09-25 12:23:53 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 u_int16_t 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_PASSWORD 4 /* volume password */
93 #define VOLOPT_CASEFOLD 5 /* character case mangling */
94 #define VOLOPT_FLAGS 6 /* various flags */
95 #define VOLOPT_DBPATH 7 /* path to database */
96 #define VOLOPT_MAPCHARS 8 /* does mtou and utom mappings. syntax:
97 m and u can be double-byte hex
102 !u -> make u illegal always
103 ~u -> make u illegal only as the first
104 part of a double-byte character.
106 #define VOLOPT_VETO 10 /* list of veto filespec */
107 #define VOLOPT_PREEXEC 11 /* preexec command */
108 #define VOLOPT_ROOTPREEXEC 12 /* root preexec command */
110 #define VOLOPT_POSTEXEC 13 /* postexec command */
111 #define VOLOPT_ROOTPOSTEXEC 14 /* root postexec command */
113 #define VOLOPT_ENCODING 15 /* mac encoding (pre OSX)*/
114 #define VOLOPT_MACCHARSET 16
115 #define VOLOPT_CNIDSCHEME 17
116 #define VOLOPT_ADOUBLE 18 /* adouble version */
121 #define VOLOPT_FORCEUID 19 /* force uid for username x */
122 #define VOLOPT_FORCEGID 20 /* force gid for group x */
123 #define VOLOPT_UMASK 21
125 #define VOLOPT_UMASK 19
126 #endif /* FORCE_UIDGID */
128 #define VOLOPT_MAX (VOLOPT_UMASK +1)
130 #define VOLOPT_NUM (VOLOPT_MAX + 1)
133 #define VOLOPT_DEFAULT ":DEFAULT:"
134 #define VOLOPT_DEFAULT_LEN 9
140 static __inline__ void volfree(struct vol_option *options,
141 const struct vol_option *save)
146 for (i = 0; i < VOLOPT_MAX; i++) {
147 if (options[i].c_value && (options[i].c_value != save[i].c_value))
148 free(options[i].c_value);
151 for (i = 0; i < VOLOPT_MAX; i++) {
152 if (options[i].c_value)
153 free(options[i].c_value);
159 /* handle variable substitutions. here's what we understand:
160 * $b -> basename of path
161 * $c -> client ip/appletalk address
162 * $d -> volume pathname on server
163 * $f -> full name (whatever's in the gecos field)
166 * $s -> server name (hostname if it doesn't exist)
167 * $u -> username (guest is usually nobody)
168 * $v -> volume name (ADEID_NAME or basename if ADEID_NAME is empty)
169 * $z -> zone (may not exist)
172 #define is_var(a, b) (strncmp((a), (b), 2) == 0)
174 static char *volxlate(AFPObj *obj, char *dest, size_t destlen,
175 char *src, struct passwd *pwd, char *path)
185 dest = calloc(destlen +1, 1);
191 strncpy(dest, src, destlen);
192 if ((p = strchr(src, '$')) == NULL) /* nothing to do */
195 /* first part of the path. just forward to the next variable. */
196 len = MIN(p - src, destlen);
202 while (p && destlen > 0) {
203 /* now figure out what the variable is */
205 if (is_var(p, "$b")) {
207 if ((q = strrchr(path, '/')) == NULL)
209 else if (*(q + 1) != '\0')
212 } else if (is_var(p, "$c")) {
213 if (obj->proto == AFPPROTO_ASP) {
214 ASP asp = obj->handle;
216 len = sprintf(dest, "%u.%u", ntohs(asp->asp_sat.sat_addr.s_net),
217 asp->asp_sat.sat_addr.s_node);
221 } else if (obj->proto == AFPPROTO_DSI) {
222 DSI *dsi = obj->handle;
224 len = sprintf(dest, "%s:%u", inet_ntoa(dsi->client.sin_addr),
225 ntohs(dsi->client.sin_port));
229 } else if (is_var(p, "$d")) {
231 } else if (is_var(p, "$f")) {
232 if ((q = strchr(pwd->pw_gecos, ',')))
235 } else if (is_var(p, "$g")) {
236 struct group *grp = getgrgid(pwd->pw_gid);
239 } else if (is_var(p, "$h")) {
240 q = obj->options.hostname;
241 } else if (is_var(p, "$s")) {
244 else if (obj->options.server) {
245 q = obj->options.server;
247 q = obj->options.hostname;
248 } else if (is_var(p, "$u")) {
250 } else if (is_var(p, "$v")) {
255 if (ad_open(path, ADFLAGS_HF, O_RDONLY, 0, &ad) < 0)
258 if ((len = MIN(ad_getentrylen(&ad, ADEID_NAME), destlen)) > 0) {
259 memcpy(dest, ad_entry(&ad, ADEID_NAME), len);
260 ad_close(&ad, ADFLAGS_HF);
264 ad_close(&ad, ADFLAGS_HF);
265 no_volname: /* simple basename */
266 if ((q = strrchr(path, '/')) == NULL)
268 else if (*(q + 1) != '\0')
272 } else if (is_var(p, "$z")) {
274 } else if (is_var(p, "$$")) {
279 /* copy the stuff over. if we don't understand something that we
280 * should, just skip it over. */
282 len = MIN(p == q ? 2 : strlen(q), destlen);
283 strncpy(dest, q, len);
288 /* stuff up to next $ */
290 p = strchr(src, '$');
291 len = p ? MIN(p - src, destlen) : destlen;
293 strncpy(dest, src, len);
301 /* to make sure that val is valid, make sure to select an opt that
303 static int optionok(const char *buf, const char *opt, const char *val)
305 if (!strstr(buf,opt))
313 /* -------------------- */
314 static void setoption(struct vol_option *options, struct vol_option *save, int opt, const char *val)
316 if (options[opt].c_value && (!save || options[opt].c_value != save[opt].c_value))
317 free(options[opt].c_value);
318 options[opt].c_value = strdup(val + 1);
321 /* ------------------------------------------
322 handle all the options. tmp can't be NULL. */
323 static void volset(struct vol_option *options, struct vol_option *save,
324 char *volname, int vlen,
329 val = strchr(tmp, ':');
331 /* we'll assume it's a volume name. */
332 strncpy(volname, tmp, vlen);
337 LOG(log_debug, logtype_afpd, "Parsing volset %s", val);
339 if (optionok(tmp, "allow:", val)) {
340 setoption(options, save, VOLOPT_ALLOW, val);
342 } else if (optionok(tmp, "deny:", val)) {
343 setoption(options, save, VOLOPT_DENY, val);
345 } else if (optionok(tmp, "rwlist:", val)) {
346 setoption(options, save, VOLOPT_RWLIST, val);
348 } else if (optionok(tmp, "rolist:", val)) {
349 setoption(options, save, VOLOPT_ROLIST, val);
351 } else if (optionok(tmp, "codepage:", val)) {
352 LOG (log_error, logtype_afpd, "The old codepage system has been removed. Please make sure to read the documentation !!!!");
353 /* Make sure we don't screw anything */
355 } else if (optionok(tmp, "volcharset:", val)) {
356 setoption(options, save, VOLOPT_ENCODING, val);
357 } else if (optionok(tmp, "maccharset:", val)) {
358 setoption(options, save, VOLOPT_MACCHARSET, val);
359 } else if (optionok(tmp, "veto:", val)) {
360 setoption(options, save, VOLOPT_VETO, val);
361 } else if (optionok(tmp, "cnidscheme:", val)) {
362 setoption(options, save, VOLOPT_CNIDSCHEME, val);
363 } else if (optionok(tmp, "casefold:", val)) {
364 if (strcasecmp(val + 1, "tolower") == 0)
365 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMLOWER;
366 else if (strcasecmp(val + 1, "toupper") == 0)
367 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMUPPER;
368 else if (strcasecmp(val + 1, "xlatelower") == 0)
369 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UUPPERMLOWER;
370 else if (strcasecmp(val + 1, "xlateupper") == 0)
371 options[VOLOPT_CASEFOLD].i_value = AFPVOL_ULOWERMUPPER;
372 } else if (optionok(tmp, "adouble:", val)) {
373 if (strcasecmp(val + 1, "v1") == 0)
374 options[VOLOPT_ADOUBLE].i_value = AD_VERSION1;
375 #if AD_VERSION == AD_VERSION2
376 else if (strcasecmp(val + 1, "v2") == 0)
377 options[VOLOPT_ADOUBLE].i_value = AD_VERSION2;
379 } else if (optionok(tmp, "options:", val)) {
382 if ((p = strtok(val + 1, ",")) == NULL) /* nothing */
386 if (strcasecmp(p, "prodos") == 0)
387 options[VOLOPT_FLAGS].i_value |= AFPVOL_A2VOL;
388 else if (strcasecmp(p, "mswindows") == 0) {
389 options[VOLOPT_FLAGS].i_value |= AFPVOL_MSWINDOWS;
390 } else if (strcasecmp(p, "crlf") == 0)
391 options[VOLOPT_FLAGS].i_value |= AFPVOL_CRLF;
392 else if (strcasecmp(p, "noadouble") == 0)
393 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOADOUBLE;
394 else if (strcasecmp(p, "ro") == 0)
395 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
396 else if (strcasecmp(p, "nohex") == 0)
397 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOHEX;
398 else if (strcasecmp(p, "usedots") == 0)
399 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS;
400 else if (strcasecmp(p, "limitsize") == 0)
401 options[VOLOPT_FLAGS].i_value |= AFPVOL_LIMITSIZE;
402 /* support for either "dropbox" or "dropkludge" */
403 else if (strcasecmp(p, "dropbox") == 0)
404 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
405 else if (strcasecmp(p, "dropkludge") == 0)
406 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
407 else if (strcasecmp(p, "nofileid") == 0)
408 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOFILEID;
409 else if (strcasecmp(p, "nostat") == 0)
410 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOSTAT;
411 else if (strcasecmp(p, "preexec_close") == 0)
412 options[VOLOPT_PREEXEC].i_value = 1;
413 else if (strcasecmp(p, "root_preexec_close") == 0)
414 options[VOLOPT_ROOTPREEXEC].i_value = 1;
415 else if (strcasecmp(p, "upriv") == 0)
416 options[VOLOPT_FLAGS].i_value |= AFPVOL_UNIX_PRIV;
418 p = strtok(NULL, ",");
421 } else if (optionok(tmp, "dbpath:", val)) {
422 setoption(options, save, VOLOPT_DBPATH, val);
424 } else if (optionok(tmp, "umask:", val)) {
425 options[VOLOPT_UMASK].i_value = (int)strtol(val, (char **)NULL, 8);
426 } else if (optionok(tmp, "mapchars:",val)) {
427 setoption(options, save, VOLOPT_MAPCHARS, val);
429 } else if (optionok(tmp, "password:", val)) {
430 setoption(options, save, VOLOPT_PASSWORD, val);
434 /* this code allows forced uid/gid per volume settings */
435 } else if (optionok(tmp, "forceuid:", val)) {
436 setoption(options, save, VOLOPT_FORCEUID, val);
437 } else if (optionok(tmp, "forcegid:", val)) {
438 setoption(options, save, VOLOPT_FORCEGID, val);
440 #endif /* FORCE_UIDGID */
441 } else if (optionok(tmp, "root_preexec:", val)) {
442 setoption(options, save, VOLOPT_ROOTPREEXEC, val);
444 } else if (optionok(tmp, "preexec:", val)) {
445 setoption(options, save, VOLOPT_PREEXEC, val);
447 } else if (optionok(tmp, "root_postexec:", val)) {
448 setoption(options, save, VOLOPT_ROOTPOSTEXEC, val);
450 } else if (optionok(tmp, "postexec:", val)) {
451 setoption(options, save, VOLOPT_POSTEXEC, val);
454 /* ignore unknown options */
455 LOG(log_debug, logtype_afpd, "ignoring unknown volume option: %s", tmp);
460 /* ----------------- */
461 static void showvol(const char *name)
464 for ( volume = Volumes; volume; volume = volume->v_next ) {
465 if ( !strcasecmp( volume->v_name, name ) && volume->v_hide) {
472 /* ------------------------------- */
473 static int creatvol(AFPObj *obj, struct passwd *pwd,
474 char *path, char *name,
475 struct vol_option *options,
476 const int user /* user defined volume */
483 if ( name == NULL || *name == '\0' ) {
484 if ((name = strrchr( path, '/' )) == NULL) {
485 return -1; /* Obviously not a fully qualified path */
488 /* if you wish to share /, you need to specify a name. */
493 for ( volume = Volumes; volume; volume = volume->v_next ) {
494 if ( strcasecmp( volume->v_name, name ) == 0 ) {
495 if (volume->v_deleted) {
499 return -1; /* Won't be able to access it, anyway... */
504 vlen = strlen( name );
505 if ( vlen > AFPVOL_NAMELEN ) {
506 vlen = AFPVOL_NAMELEN;
507 name[AFPVOL_NAMELEN] = '\0';
510 if (!( volume = (struct vol *)calloc(1, sizeof( struct vol ))) ) {
511 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
514 if (! ( volume->v_name = (char *)malloc( vlen + 1 ) ) ) {
515 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
519 if (!( volume->v_path = (char *)malloc( strlen( path ) + 1 )) ) {
520 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
521 free(volume->v_name);
525 volume->v_hide = hide;
526 strcpy( volume->v_name, name);
527 strcpy( volume->v_path, path );
531 #endif /* __svr4__ */
532 /* os X start at 1 and use network order ie. 1 2 3 */
533 volume->v_vid = ++lastvid;
534 volume->v_vid = htons(volume->v_vid);
538 /* should we casefold? */
539 volume->v_casefold = options[VOLOPT_CASEFOLD].i_value;
541 /* shift in some flags */
542 volume->v_flags = options[VOLOPT_FLAGS].i_value;
544 if (options[VOLOPT_PASSWORD].c_value)
545 volume->v_password = strdup(options[VOLOPT_PASSWORD].c_value);
547 if (options[VOLOPT_VETO].c_value)
548 volume->v_veto = strdup(options[VOLOPT_VETO].c_value);
550 if (options[VOLOPT_ENCODING].c_value)
551 volume->v_volcodepage = strdup(options[VOLOPT_ENCODING].c_value);
553 if (options[VOLOPT_MACCHARSET].c_value)
554 volume->v_maccodepage = strdup(options[VOLOPT_MACCHARSET].c_value);
556 if (options[VOLOPT_DBPATH].c_value)
557 volume->v_dbpath = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_DBPATH].c_value, pwd, path);
559 if (options[VOLOPT_CNIDSCHEME].c_value)
560 volume->v_cnidscheme = strdup(options[VOLOPT_CNIDSCHEME].c_value);
562 if (options[VOLOPT_UMASK].i_value)
563 volume->v_umask = (mode_t)options[VOLOPT_UMASK].i_value;
565 if (options[VOLOPT_ADOUBLE].i_value)
566 volume->v_adouble = options[VOLOPT_ADOUBLE].i_value;
568 volume->v_adouble = AD_VERSION;
570 if (options[VOLOPT_FORCEUID].c_value) {
571 volume->v_forceuid = strdup(options[VOLOPT_FORCEUID].c_value);
573 volume->v_forceuid = NULL; /* set as null so as to return 0 later on */
576 if (options[VOLOPT_FORCEGID].c_value) {
577 volume->v_forcegid = strdup(options[VOLOPT_FORCEGID].c_value);
579 volume->v_forcegid = NULL; /* set as null so as to return 0 later on */
583 if (options[VOLOPT_PREEXEC].c_value)
584 volume->v_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_PREEXEC].c_value, pwd, path);
585 volume->v_preexec_close = options[VOLOPT_PREEXEC].i_value;
587 if (options[VOLOPT_POSTEXEC].c_value)
588 volume->v_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_POSTEXEC].c_value, pwd, path);
590 if (options[VOLOPT_ROOTPREEXEC].c_value)
591 volume->v_root_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPREEXEC].c_value, pwd, path);
592 volume->v_root_preexec_close = options[VOLOPT_ROOTPREEXEC].i_value;
594 if (options[VOLOPT_ROOTPOSTEXEC].c_value)
595 volume->v_root_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPOSTEXEC].c_value, pwd, path);
599 volume->v_next = Volumes;
604 /* ---------------- */
605 static char *myfgets( buf, size, fp )
614 while ((EOF != ( c = getc( fp )) ) && ( size > 0 )) {
615 if ( c == '\n' || c == '\r' ) {
633 /* check access list. this function wants something of the following
635 * @group,name,name2,@group2,name3
637 * a NULL argument allows everybody to have access.
638 * we return three things:
640 * 0: list exists, but name isn't in it
644 #ifndef NO_REAL_USER_NAME
645 /* authentication is case insensitive
646 * FIXME should we do the same with group name?
648 #define access_strcmp strcasecmp
651 #define access_strcmp strcmp
655 static int accessvol(args, name)
659 char buf[MAXPATHLEN + 1], *p;
665 strncpy(buf, args, sizeof(buf));
666 if ((p = strtok(buf, ",")) == NULL) /* nothing, return okay */
670 if (*p == '@') { /* it's a group */
671 if ((gr = getgrnam(p + 1)) && gmem(gr->gr_gid))
673 } else if (access_strcmp(p, name) == 0) /* it's a user name */
675 p = strtok(NULL, ",");
681 static void setextmap( ext, type, creator, user)
682 char *ext, *type, *creator;
688 if (Extmap == NULL) {
689 if (( Extmap = calloc(1, sizeof( struct extmap ))) == NULL ) {
690 LOG(log_error, logtype_afpd, "setextmap: calloc: %s", strerror(errno) );
695 for ( em = Extmap, cnt = 0; em->em_ext; em++, cnt++) {
696 if ( (strdiacasecmp( em->em_ext, ext )) == 0 ) {
701 if ( em->em_ext == NULL ) {
702 if (!(Extmap = realloc( Extmap, sizeof( struct extmap ) * (cnt +2))) ) {
703 LOG(log_error, logtype_afpd, "setextmap: realloc: %s", strerror(errno) );
706 (Extmap +cnt +1)->em_ext = NULL;
708 } else if ( !user ) {
714 if (!(em->em_ext = strdup( ext))) {
715 LOG(log_error, logtype_afpd, "setextmap: strdup: %s", strerror(errno) );
719 if ( *type == '\0' ) {
720 memcpy(em->em_type, "????", sizeof( em->em_type ));
722 memcpy(em->em_type, type, sizeof( em->em_type ));
724 if ( *creator == '\0' ) {
725 memcpy(em->em_creator, "UNIX", sizeof( em->em_creator ));
727 memcpy(em->em_creator, creator, sizeof( em->em_creator ));
731 /* -------------------------- */
732 static int extmap_cmp(const void *map1, const void *map2)
734 const struct extmap *em1 = map1;
735 const struct extmap *em2 = map2;
736 return strdiacasecmp(em1->em_ext, em2->em_ext);
739 static void sortextmap( void)
744 if ((em = Extmap) == NULL) {
752 qsort(Extmap, Extmap_cnt, sizeof(struct extmap), extmap_cmp);
757 /* ----------------------
759 static void free_extmap( void)
769 /* ----------------------
771 static int volfile_changed(struct afp_volume_name *p)
781 if (!stat( name, &st) && st.st_mtime > p->mtime) {
782 p->mtime = st.st_mtime;
788 /* ----------------------
789 * Read a volume configuration file and add the volumes contained within to
790 * the global volume list. If p2 is non-NULL, the file that is opened is
793 * Lines that begin with # and blank lines are ignored.
794 * Volume lines are of the form:
795 * <unix path> [<volume name>] [allow:<user>,<@group>,...] \
796 * [codepage:<file>] [casefold:<num>]
797 * <extension> TYPE [CREATOR]
799 static int readvolfile(obj, p1, p2, user, pwent)
801 struct afp_volume_name *p1;
804 struct passwd *pwent;
807 char path[ MAXPATHLEN + 1], tmp[ MAXPATHLEN + 1],
808 volname[ AFPVOL_NAMELEN + 1 ], buf[ BUFSIZ ],
809 type[ 5 ], creator[ 5 ];
812 struct vol_option options[VOLOPT_NUM], save_options[VOLOPT_NUM];
820 strcpy( path, p1->name );
827 p1->full_name = strdup(path);
830 if (NULL == ( fp = fopen( path, "r" )) ) {
834 if (fd != -1 && !fstat( fd, &st) ) {
835 p1->mtime = st.st_mtime;
838 memset(save_options, 0, sizeof(save_options));
839 while ( myfgets( buf, sizeof( buf ), fp ) != NULL ) {
840 initline( strlen( buf ), buf );
841 parseline( sizeof( path ) - 1, path );
848 /* change the default options for this file */
849 if (strncmp(path, VOLOPT_DEFAULT, VOLOPT_DEFAULT_LEN) == 0) {
851 for (i = 0; i < VOLOPT_NUM; i++) {
852 if (parseline( sizeof( path ) - VOLOPT_DEFAULT_LEN - 1,
853 path + VOLOPT_DEFAULT_LEN) < 0)
855 volset(save_options, NULL, tmp, sizeof(tmp) - 1,
856 path + VOLOPT_DEFAULT_LEN);
862 if (( p = strchr( path, '/' )) != NULL ) {
870 if ( u == NULL || *u == '\0' || ( pw = getpwnam( u )) == NULL ) {
873 strcpy( tmp, pw->pw_dir );
874 if ( p != NULL && *p != '\0' ) {
878 /* Tag a user's home directory with their umask. Note, this will
879 * be overwritten if the user actually specifies a umask: option
880 * for a '~' volume. */
881 save_options[VOLOPT_UMASK].i_value = obj->options.save_mask;
885 /* send path through variable substitution */
886 if (*path != '~') /* need to copy path to tmp */
889 pwent = getpwnam(obj->username);
890 volxlate(obj, path, sizeof(path) - 1, tmp, pwent, NULL);
892 /* this is sort of braindead. basically, i want to be
893 * able to specify things in any order, but i don't want to
894 * re-write everything.
896 * currently we have options:
904 * options:prodos,crlf,noadouble,ro...
909 * namemask:x,y,!z (not implemented yet)
911 memcpy(options, save_options, sizeof(options));
914 /* read in up to VOLOP_NUM possible options */
915 for (i = 0; i < VOLOPT_NUM; i++) {
916 if (parseline( sizeof( tmp ) - 1, tmp ) < 0)
919 volset(options, save_options, volname, sizeof(volname) - 1, tmp);
922 /* check allow/deny lists:
923 allow -> either no list (-1), or in list (1)
924 deny -> either no list (-1), or not in list (0) */
925 if (accessvol(options[VOLOPT_ALLOW].c_value, obj->username) &&
926 (accessvol(options[VOLOPT_DENY].c_value, obj->username) < 1)) {
928 /* handle read-only behaviour. semantics:
929 * 1) neither the rolist nor the rwlist exist -> rw
930 * 2) rolist exists -> ro if user is in it.
931 * 3) rwlist exists -> ro unless user is in it. */
932 if (((options[VOLOPT_FLAGS].i_value & AFPVOL_RO) == 0) &&
933 ((accessvol(options[VOLOPT_ROLIST].c_value,
934 obj->username) == 1) ||
935 !accessvol(options[VOLOPT_RWLIST].c_value,
937 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
939 /* do variable substitution for volname */
940 volxlate(obj, tmp, sizeof(tmp) - 1, volname, pwent, path);
941 creatvol(obj, pwent, path, tmp, options, p2 != NULL);
943 volfree(options, save_options);
947 parseline( sizeof( type ) - 1, type );
948 parseline( sizeof( creator ) - 1, creator );
949 setextmap( path, type, creator, user);
956 volfree(save_options, NULL);
958 if ( fclose( fp ) != 0 ) {
959 LOG(log_error, logtype_afpd, "readvolfile: fclose: %s", strerror(errno) );
965 /* ------------------------------- */
966 static void volume_free(struct vol *vol)
971 free(vol->v_password);
973 free(vol->v_volcodepage);
974 free(vol->v_maccodepage);
977 free(vol->v_forceuid);
978 free(vol->v_forcegid);
979 #endif /* FORCE_UIDGID */
982 /* ------------------------------- */
983 static void free_volumes(void )
986 struct vol *nvol, *ovol;
988 for ( vol = Volumes; vol; vol = vol->v_next ) {
989 if (( vol->v_flags & AFPVOL_OPEN ) ) {
996 for ( vol = Volumes, ovol = NULL; vol; vol = nvol) {
999 if (vol->v_name == NULL) {
1000 if (Volumes == vol) {
1007 ovol->v_next = nvol;
1017 /* ------------------------------- */
1018 static void volume_unlink(struct vol *volume)
1020 struct vol *vol, *ovol, *nvol;
1022 if (volume == Volumes) {
1023 Volumes = Volumes->v_next;
1026 for ( vol = Volumes, ovol = NULL; vol; vol = nvol) {
1029 if (vol == volume) {
1034 ovol->v_next = nvol;
1045 static int getvolspace( vol, bfree, btotal, xbfree, xbtotal, bsize )
1047 u_int32_t *bfree, *btotal, *bsize;
1048 VolSpace *xbfree, *xbtotal;
1052 #ifndef NO_QUOTA_SUPPORT
1053 VolSpace qfree, qtotal;
1056 spaceflag = AFPVOL_GVSMASK & vol->v_flags;
1057 /* report up to 2GB if afp version is < 2.2 (4GB if not) */
1058 maxsize = (vol->v_flags & AFPVOL_A2VOL) ? 0x01fffe00 :
1059 (((afp_version < 22) || (vol->v_flags & AFPVOL_LIMITSIZE))
1060 ? 0x7fffffffL : 0xffffffffL);
1063 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_AFSGVS ) {
1064 if ( afs_getvolspace( vol, xbfree, xbtotal, bsize ) == AFP_OK ) {
1065 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_AFSGVS;
1066 goto getvolspace_done;
1071 if (( rc = ustatfs_getvolspace( vol, xbfree, xbtotal,
1072 bsize)) != AFP_OK ) {
1076 #define min(a,b) ((a)<(b)?(a):(b))
1077 #ifndef NO_QUOTA_SUPPORT
1078 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_UQUOTA ) {
1079 if ( uquota_getvolspace( vol, &qfree, &qtotal, *bsize ) == AFP_OK ) {
1080 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_UQUOTA;
1081 *xbfree = min(*xbfree, qfree);
1082 *xbtotal = min( *xbtotal, qtotal);
1083 goto getvolspace_done;
1087 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_USTATFS;
1090 *bfree = min( *xbfree, maxsize);
1091 *btotal = min( *xbtotal, maxsize);
1095 /* ----------------------- */
1096 static int getvolparams( bitmap, vol, st, buf, buflen )
1104 int bit = 0, isad = 1;
1107 u_int32_t bfree, btotal, bsize;
1108 VolSpace xbfree, xbtotal; /* extended bytes */
1109 char *data, *nameoff = NULL;
1112 /* courtesy of jallison@whistle.com:
1113 * For MacOS8.x support we need to create the
1114 * .Parent file here if it doesn't exist. */
1116 ad_init(&ad, vol->v_adouble);
1117 if ( ad_open( vol->v_path, vol_noadouble(vol) |
1118 ADFLAGS_HF|ADFLAGS_DIR, O_RDWR | O_CREAT,
1122 } else if (ad_get_HF_flags( &ad ) & O_CREAT) {
1123 slash = strrchr( vol->v_path, '/' );
1127 slash = vol->v_path;
1129 ad_setentrylen( &ad, ADEID_NAME, strlen( slash ));
1130 memcpy(ad_entry( &ad, ADEID_NAME ), slash,
1131 ad_getentrylen( &ad, ADEID_NAME ));
1132 ad_setdate(&ad, AD_DATE_CREATE | AD_DATE_UNIX, st->st_mtime);
1133 ad_flush(&ad, ADFLAGS_HF);
1136 if (( bitmap & ( (1<<VOLPBIT_BFREE)|(1<<VOLPBIT_BTOTAL) |
1137 (1<<VOLPBIT_XBFREE)|(1<<VOLPBIT_XBTOTAL) |
1138 (1<<VOLPBIT_BSIZE)) ) != 0 ) {
1139 if ( getvolspace( vol, &bfree, &btotal, &xbfree, &xbtotal,
1142 ad_close( &ad, ADFLAGS_HF );
1144 return( AFPERR_PARAM );
1149 while ( bitmap != 0 ) {
1150 while (( bitmap & 1 ) == 0 ) {
1158 if (0 == (vol->v_flags & AFPVOL_NOFILEID) && vol->v_cdb != NULL &&
1159 (vol->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1160 ashort = VOLPBIT_ATTR_FILEID;
1162 /* check for read-only.
1163 * NOTE: we don't actually set the read-only flag unless
1164 * it's passed in that way as it's possible to mount
1165 * a read-write filesystem under a read-only one. */
1166 if ((vol->v_flags & AFPVOL_RO) ||
1167 ((utime(vol->v_path, NULL) < 0) && (errno == EROFS))) {
1168 ashort |= VOLPBIT_ATTR_RO;
1170 ashort |= VOLPBIT_ATTR_CATSEARCH;
1171 if (afp_version >= 30) {
1172 ashort |= VOLPBIT_ATTR_UTF8;
1173 if (vol->v_flags & AFPVOL_UNIX_PRIV)
1174 ashort |= VOLPBIT_ATTR_UNIXPRIV;
1176 ashort = htons(ashort);
1177 memcpy(data, &ashort, sizeof( ashort ));
1178 data += sizeof( ashort );
1182 ashort = htons( AFPVOLSIG_DEFAULT );
1183 memcpy(data, &ashort, sizeof( ashort ));
1184 data += sizeof( ashort );
1187 case VOLPBIT_CDATE :
1188 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
1189 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1190 memcpy(data, &aint, sizeof( aint ));
1191 data += sizeof( aint );
1194 case VOLPBIT_MDATE :
1195 if ( st->st_mtime > vol->v_time ) {
1196 vol->v_time = st->st_mtime;
1197 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1199 aint = AD_DATE_FROM_UNIX(vol->v_time);
1201 memcpy(data, &aint, sizeof( aint ));
1202 data += sizeof( aint );
1205 case VOLPBIT_BDATE :
1206 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1207 aint = AD_DATE_START;
1208 memcpy(data, &aint, sizeof( aint ));
1209 data += sizeof( aint );
1213 memcpy(data, &vol->v_vid, sizeof( vol->v_vid ));
1214 data += sizeof( vol->v_vid );
1217 case VOLPBIT_BFREE :
1218 bfree = htonl( bfree );
1219 memcpy(data, &bfree, sizeof( bfree ));
1220 data += sizeof( bfree );
1223 case VOLPBIT_BTOTAL :
1224 btotal = htonl( btotal );
1225 memcpy(data, &btotal, sizeof( btotal ));
1226 data += sizeof( btotal );
1229 #ifndef NO_LARGE_VOL_SUPPORT
1230 case VOLPBIT_XBFREE :
1231 xbfree = hton64( xbfree );
1232 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1233 bcopy(&xbfree, data, sizeof(xbfree));
1234 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1235 memcpy(data, &xbfree, sizeof( xbfree ));
1236 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1237 data += sizeof( xbfree );
1240 case VOLPBIT_XBTOTAL :
1241 xbtotal = hton64( xbtotal );
1242 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1243 bcopy(&xbtotal, data, sizeof(xbtotal));
1244 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1245 memcpy(data, &xbtotal, sizeof( xbtotal ));
1246 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1247 data += sizeof( xbfree );
1249 #endif /* ! NO_LARGE_VOL_SUPPORT */
1253 data += sizeof( u_int16_t );
1256 case VOLPBIT_BSIZE: /* block size */
1257 bsize = htonl(bsize);
1258 memcpy(data, &bsize, sizeof(bsize));
1259 data += sizeof(bsize);
1264 ad_close( &ad, ADFLAGS_HF );
1266 return( AFPERR_BITMAP );
1272 ashort = htons( data - buf );
1273 memcpy(nameoff, &ashort, sizeof( ashort ));
1274 aint = strlen( vol->v_name );
1276 memcpy(data, vol->v_name, aint );
1280 ad_close( &ad, ADFLAGS_HF );
1282 *buflen = data - buf;
1286 /* ------------------------- */
1287 int static stat_vol(u_int16_t bitmap, struct vol *vol, char *rbuf, int *rbuflen)
1292 if ( stat( vol->v_path, &st ) < 0 ) {
1294 return( AFPERR_PARAM );
1297 buflen = *rbuflen - sizeof( bitmap );
1298 if (( ret = getvolparams( bitmap, vol, &st,
1299 rbuf + sizeof( bitmap ), &buflen )) != AFP_OK ) {
1303 *rbuflen = buflen + sizeof( bitmap );
1304 bitmap = htons( bitmap );
1305 memcpy(rbuf, &bitmap, sizeof( bitmap ));
1310 /* ------------------------------- */
1311 void load_volumes(AFPObj *obj)
1313 struct passwd *pwent;
1318 /* check files date */
1319 if (obj->options.defaultvol.loaded) {
1320 changed = volfile_changed(&obj->options.defaultvol);
1322 if (obj->options.systemvol.loaded) {
1323 changed |= volfile_changed(&obj->options.systemvol);
1325 if (obj->options.uservol.loaded) {
1326 changed |= volfile_changed(&obj->options.uservol);
1335 pwent = getpwnam(obj->username);
1336 if ( (obj->options.flags & OPTION_USERVOLFIRST) == 0 ) {
1337 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent);
1340 if ((*obj->username == '\0') || (obj->options.flags & OPTION_NOUSERVOL)) {
1341 readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent);
1344 * Read user's AppleVolumes or .AppleVolumes file
1345 * If neither are readable, read the default volumes file. if
1346 * that doesn't work, create a user share.
1348 obj->options.uservol.name = strdup(pwent->pw_dir);
1349 if ( readvolfile(obj, &obj->options.uservol, "AppleVolumes", 1, pwent) < 0 &&
1350 readvolfile(obj, &obj->options.uservol, ".AppleVolumes", 1, pwent) < 0 &&
1351 readvolfile(obj, &obj->options.uservol, "applevolumes", 1, pwent) < 0 &&
1352 readvolfile(obj, &obj->options.uservol, ".applevolumes", 1, pwent) < 0 &&
1353 obj->options.defaultvol.name != NULL ) {
1354 if (readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent) < 0)
1355 creatvol(obj, pwent, pwent->pw_dir, NULL, NULL, 1);
1358 if ( obj->options.flags & OPTION_USERVOLFIRST ) {
1359 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent );
1363 /* ------------------------------- */
1364 int afp_getsrvrparms(obj, ibuf, ibuflen, rbuf, rbuflen )
1367 int ibuflen, *rbuflen;
1378 for ( vcnt = 0, volume = Volumes; volume; volume = volume->v_next ) {
1379 if (!(volume->v_flags & AFPVOL_NOSTAT)) {
1380 if ( stat( volume->v_path, &st ) < 0 ) {
1381 LOG(log_info, logtype_afpd, "afp_getsrvrparms: stat %s: %s",
1382 volume->v_path, strerror(errno) );
1383 continue; /* can't access directory */
1385 if (!S_ISDIR(st.st_mode)) {
1386 continue; /* not a dir */
1389 if (volume->v_hide) {
1390 continue; /* config file changed but the volume was mounted */
1393 /* set password bit if there's a volume password */
1394 *data = (volume->v_password) ? AFPSRVR_PASSWD : 0;
1396 /* Apple 2 clients running ProDOS-8 expect one volume to have
1397 bit 0 of this byte set. They will not recognize anything
1398 on the server unless this is the case. I have not
1399 completely worked this out, but it's related to booting
1400 from the server. Support for that function is a ways
1401 off.. <shirsch@ibm.net> */
1402 *data |= (volume->v_flags & AFPVOL_A2VOL) ? AFPSRVR_CONFIGINFO : 0;
1403 *data++ |= 0; /* UNIX PRIVS BIT ..., OSX doesn't seem to use it, so we don't either */
1404 len = strlen( volume->v_name );
1406 memcpy(data, volume->v_name, len );
1411 *rbuflen = data - rbuf;
1413 if ( gettimeofday( &tv, 0 ) < 0 ) {
1414 LOG(log_error, logtype_afpd, "afp_getsrvrparms: gettimeofday: %s", strerror(errno) );
1416 return AFPERR_PARAM;
1418 tv.tv_sec = AD_DATE_FROM_UNIX(tv.tv_sec);
1419 memcpy(data, &tv.tv_sec, sizeof( u_int32_t));
1420 data += sizeof( u_int32_t);
1425 /* -------------------------
1426 * we are the user here
1428 int afp_openvol(obj, ibuf, ibuflen, rbuf, rbuflen )
1431 int ibuflen, *rbuflen;
1442 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1443 bitmap = ntohs( bitmap );
1444 ibuf += sizeof( bitmap );
1445 if (( bitmap & (1<<VOLPBIT_VID)) == 0 ) {
1447 return AFPERR_BITMAP;
1450 len = (unsigned char)*ibuf++;
1451 volname = obj->oldtmp;
1452 memcpy(volname, ibuf, len );
1453 *(volname + len) = '\0';
1455 if ((len + 1) & 1) /* pad to an even boundary */
1460 for ( volume = Volumes; volume; volume = volume->v_next ) {
1461 if ( strcasecmp( volname, volume->v_name ) == 0 ) {
1466 if ( volume == NULL ) {
1468 return AFPERR_PARAM;
1471 /* check for a volume password */
1472 if (volume->v_password && strncmp(ibuf, volume->v_password, VOLPASSLEN)) {
1474 return AFPERR_ACCESS;
1477 if (( volume->v_flags & AFPVOL_OPEN ) ) {
1478 /* the volume is already open */
1480 set_uidgid ( volume );
1482 return stat_vol(bitmap, volume, rbuf, rbuflen);
1485 /* initialize volume variables
1488 if (afp_version >= 30) {
1489 volume->max_filename = 255;
1492 volume->max_filename = MACFILELEN;
1495 volume->v_dir = volume->v_root = NULL;
1497 /* FIXME unix name != mac name */
1498 if ((dir = dirnew(volume->v_name, volume->v_name) ) == NULL) {
1499 LOG(log_error, logtype_afpd, "afp_openvol: malloc: %s", strerror(errno) );
1504 dir->d_did = DIRDID_ROOT;
1505 dir->d_color = DIRTREE_COLOR_BLACK; /* root node is black */
1506 volume->v_dir = volume->v_root = dir;
1507 volume->v_flags |= AFPVOL_OPEN;
1508 volume->v_cdb = NULL;
1510 if (volume->v_root_preexec) {
1511 if ((ret = afprun(1, volume->v_root_preexec, NULL)) && volume->v_root_preexec_close) {
1512 LOG(log_error, logtype_afpd, "afp_openvol: root preexec : %d", ret );
1519 set_uidgid ( volume );
1522 if (volume->v_preexec) {
1523 if ((ret = afprun(0, volume->v_preexec, NULL)) && volume->v_preexec_close) {
1524 LOG(log_error, logtype_afpd, "afp_openvol: preexec : %d", ret );
1530 if ( stat( volume->v_path, &st ) < 0 ) {
1535 if ( chdir( volume->v_path ) < 0 ) {
1539 curdir = volume->v_dir;
1540 if (volume->v_cnidscheme == NULL) {
1541 volume->v_cnidscheme = strdup(DEFAULT_CNID_SCHEME);
1542 LOG(log_warning, logtype_afpd, "Warning: No CNID scheme for volume %s. Using default.",
1545 if (volume->v_dbpath)
1546 volume->v_cdb = cnid_open (volume->v_dbpath, volume->v_umask, volume->v_cnidscheme);
1547 if (volume->v_cdb == NULL)
1548 volume->v_cdb = cnid_open (volume->v_path, volume->v_umask, volume->v_cnidscheme);
1549 if (volume->v_cdb == NULL) {
1550 volume->v_cdb = cnid_open (volume->v_path, volume->v_umask, DEFAULT_CNID_SCHEME);
1551 LOG(log_error, logtype_afpd, "Error: invalid CNID backend for %s: %s", volume->v_path,
1552 volume->v_cnidscheme);
1554 if (volume->v_cdb == NULL) {
1555 LOG(log_error, logtype_afpd, "Fatal error: cannot open CNID for %s", volume->v_path);
1562 if (!volume->v_volcodepage)
1563 volume->v_volcodepage = strdup("UTF8");
1565 if ( (charset_t) -1 == ( volume->v_volcharset = add_charset(volume->v_volcodepage)) ) {
1566 LOG (log_error, logtype_afpd, "Setting codepage %s as volume codepage failed", volume->v_volcodepage);
1571 if ( NULL == ( volume->v_vol = find_charset_functions(volume->v_volcodepage)) || volume->v_vol->flags & CHARSET_ICONV ) {
1572 LOG (log_warning, logtype_afpd, "WARNING: volume encoding %s is *not* supported by netatalk, expect problems !!!!", volume->v_volcodepage);
1575 if (!volume->v_maccodepage)
1576 volume->v_maccodepage = strdup(obj->options.maccodepage);
1578 if ( (charset_t) -1 == ( volume->v_maccharset = add_charset(volume->v_maccodepage)) ) {
1579 LOG (log_error, logtype_afpd, "Setting codepage %s as mac codepage failed", volume->v_maccodepage);
1584 if ( NULL == ( volume->v_mac = find_charset_functions(volume->v_maccodepage)) || ! (volume->v_mac->flags & CHARSET_CLIENT) ) {
1585 LOG (log_error, logtype_afpd, "Fatal error: mac charset %s not supported", volume->v_maccodepage);
1590 ret = stat_vol(bitmap, volume, rbuf, rbuflen);
1591 if (ret == AFP_OK) {
1593 * If you mount a volume twice, the second time the trash appears on
1594 * the desk-top. That's because the Mac remembers the DID for the
1595 * trash (even for volumes in different zones, on different servers).
1596 * Just so this works better, we prime the DID cache with the trash,
1597 * fixing the trash at DID 17.
1598 * FIXME (RL): should it be done inside a CNID backend ? (always returning Trash DID when asked) ?
1600 if ((volume->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1602 /* FIXME find db time stamp */
1603 cnid_getstamp(volume->v_cdb, volume->v_stamp, sizeof(volume->v_stamp));
1605 cname( volume, volume->v_dir, &p );
1611 if (volume->v_dir) {
1612 dirfree( volume->v_dir );
1613 volume->v_dir = volume->v_root = NULL;
1616 volume->v_flags &= ~AFPVOL_OPEN;
1617 if (volume->v_cdb != NULL) {
1618 cnid_close(volume->v_cdb);
1619 volume->v_cdb = NULL;
1625 /* ------------------------- */
1626 static void closevol(struct vol *vol)
1631 dirfree( vol->v_root );
1633 if (vol->v_cdb != NULL) {
1634 cnid_close(vol->v_cdb);
1638 if (vol->v_postexec) {
1639 afprun(0, vol->v_postexec, NULL);
1641 if (vol->v_root_postexec) {
1642 afprun(1, vol->v_root_postexec, NULL);
1646 /* ------------------------- */
1647 void close_all_vol(void)
1651 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
1652 if ( ovol->v_flags & AFPVOL_OPEN ) {
1653 ovol->v_flags &= ~AFPVOL_OPEN;
1659 /* ------------------------- */
1660 int afp_closevol(obj, ibuf, ibuflen, rbuf, rbuflen )
1663 int ibuflen, *rbuflen;
1665 struct vol *vol, *ovol;
1670 memcpy(&vid, ibuf, sizeof( vid ));
1671 if (NULL == ( vol = getvolbyvid( vid )) ) {
1672 return( AFPERR_PARAM );
1675 vol->v_flags &= ~AFPVOL_OPEN;
1676 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
1677 if ( ovol->v_flags & AFPVOL_OPEN ) {
1681 if ( ovol != NULL ) {
1682 /* Even if chdir fails, we can't say afp_closevol fails. */
1683 if ( chdir( ovol->v_path ) == 0 ) {
1684 curdir = ovol->v_dir;
1689 if (vol->v_deleted) {
1690 showvol(vol->v_name);
1697 /* ------------------------- */
1698 struct vol *getvolbyvid(const u_int16_t vid )
1702 for ( vol = Volumes; vol; vol = vol->v_next ) {
1703 if ( vid == vol->v_vid ) {
1707 if ( vol == NULL || ( vol->v_flags & AFPVOL_OPEN ) == 0 ) {
1713 #endif /* FORCE_UIDGID */
1718 /* ------------------------ */
1719 static int ext_cmp_key(const void *key, const void *obj)
1721 const char *p = key;
1722 const struct extmap *em = obj;
1723 return strdiacasecmp(p, em->em_ext);
1725 struct extmap *getextmap(const char *path)
1730 if (NULL == ( p = strrchr( path, '.' )) ) {
1731 return( Defextmap );
1734 if (!*p || !Extmap_cnt) {
1735 return( Defextmap );
1737 em = bsearch(p, Extmap, Extmap_cnt, sizeof(struct extmap), ext_cmp_key);
1741 return( Defextmap );
1745 /* ------------------------- */
1746 struct extmap *getdefextmap(void)
1748 return( Defextmap );
1751 /* --------------------------
1752 poll if a volume is changed by other processes.
1754 int pollvoltime(obj)
1761 if (!(afp_version > 21 && obj->options.server_notif))
1764 if ( gettimeofday( &tv, 0 ) < 0 )
1767 for ( vol = Volumes; vol; vol = vol->v_next ) {
1768 if ( (vol->v_flags & AFPVOL_OPEN) && vol->v_time + 30 < tv.tv_sec) {
1769 if ( !stat( vol->v_path, &st ) && vol->v_time != st.st_mtime ) {
1770 vol->v_time = st.st_mtime;
1771 if (!obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED))
1780 /* ------------------------- */
1781 void setvoltime(obj, vol )
1787 /* just looking at vol->v_time is broken seriously since updates
1788 * from other users afpd processes never are seen.
1789 * This is not the most elegant solution (a shared memory between
1790 * the afpd processes would come closer)
1793 if ( gettimeofday( &tv, 0 ) < 0 ) {
1794 LOG(log_error, logtype_afpd, "setvoltime: gettimeofday: %s", strerror(errno) );
1797 if( utime( vol->v_path, NULL ) < 0 ) {
1798 /* write of time failed ... probably a read only filesys,
1799 * where no other users can interfere, so there's no issue here
1803 /* a little granularity */
1804 if (vol->v_time < tv.tv_sec) {
1805 vol->v_time = tv.tv_sec;
1806 if (afp_version > 21 && obj->options.server_notif) {
1807 obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED);
1812 /* ------------------------- */
1813 int afp_getvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1816 int ibuflen, *rbuflen;
1819 u_int16_t vid, bitmap;
1822 memcpy(&vid, ibuf, sizeof( vid ));
1823 ibuf += sizeof( vid );
1824 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1825 bitmap = ntohs( bitmap );
1827 if (NULL == ( vol = getvolbyvid( vid )) ) {
1829 return( AFPERR_PARAM );
1832 return stat_vol(bitmap, vol, rbuf, rbuflen);
1835 /* ------------------------- */
1836 int afp_setvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1839 int ibuflen, *rbuflen;
1843 u_int16_t vid, bitmap;
1849 memcpy(&vid, ibuf, sizeof( vid ));
1850 ibuf += sizeof( vid );
1851 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1852 bitmap = ntohs( bitmap );
1853 ibuf += sizeof(bitmap);
1855 if (( vol = getvolbyvid( vid )) == NULL ) {
1856 return( AFPERR_PARAM );
1859 if (vol->v_flags & AFPVOL_RO)
1860 return AFPERR_VLOCK;
1862 /* we can only set the backup date. */
1863 if (bitmap != (1 << VOLPBIT_BDATE))
1864 return AFPERR_BITMAP;
1866 ad_init(&ad, vol->v_adouble);
1867 if ( ad_open( vol->v_path, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR,
1870 return AFPERR_VLOCK;
1872 return AFPERR_ACCESS;
1875 memcpy(&aint, ibuf, sizeof(aint));
1876 ad_setdate(&ad, AD_DATE_BACKUP, aint);
1877 ad_flush(&ad, ADFLAGS_HF);
1878 ad_close(&ad, ADFLAGS_HF);
1882 /* ------------------------- */
1883 int wincheck(const struct vol *vol, const char *path)
1887 if (!(vol->v_flags & AFPVOL_MSWINDOWS))
1890 /* empty paths are not allowed */
1891 if ((len = strlen(path)) == 0)
1894 /* leading or trailing whitespaces are not allowed, carriage returns
1895 * and probably other whitespace is okay, tabs are not allowed
1897 if ((path[0] == ' ') || (path[len-1] == ' '))
1900 /* certain characters are not allowed */
1901 if (strpbrk(path, MSWINDOWS_BADCHARS))
1904 /* everything else is okay */