2 * $Id: volume.c,v 1.51.2.7.2.3 2003-09-21 09:25:17 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_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 volume->v_vid = lastvid++;
536 /* should we casefold? */
537 volume->v_casefold = options[VOLOPT_CASEFOLD].i_value;
539 /* shift in some flags */
540 volume->v_flags = options[VOLOPT_FLAGS].i_value;
542 if (options[VOLOPT_PASSWORD].c_value)
543 volume->v_password = strdup(options[VOLOPT_PASSWORD].c_value);
545 if (options[VOLOPT_VETO].c_value)
546 volume->v_veto = strdup(options[VOLOPT_VETO].c_value);
548 if (options[VOLOPT_ENCODING].c_value)
549 volume->v_volcodepage = strdup(options[VOLOPT_ENCODING].c_value);
551 if (options[VOLOPT_MACCHARSET].c_value)
552 volume->v_maccodepage = strdup(options[VOLOPT_MACCHARSET].c_value);
554 if (options[VOLOPT_DBPATH].c_value)
555 volume->v_dbpath = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_DBPATH].c_value, pwd, path);
557 if (options[VOLOPT_CNIDSCHEME].c_value)
558 volume->v_cnidscheme = strdup(options[VOLOPT_CNIDSCHEME].c_value);
560 if (options[VOLOPT_UMASK].i_value)
561 volume->v_umask = (mode_t)options[VOLOPT_UMASK].i_value;
563 if (options[VOLOPT_ADOUBLE].i_value)
564 volume->v_adouble = options[VOLOPT_ADOUBLE].i_value;
566 volume->v_adouble = AD_VERSION;
568 if (options[VOLOPT_FORCEUID].c_value) {
569 volume->v_forceuid = strdup(options[VOLOPT_FORCEUID].c_value);
571 volume->v_forceuid = NULL; /* set as null so as to return 0 later on */
574 if (options[VOLOPT_FORCEGID].c_value) {
575 volume->v_forcegid = strdup(options[VOLOPT_FORCEGID].c_value);
577 volume->v_forcegid = NULL; /* set as null so as to return 0 later on */
581 if (options[VOLOPT_PREEXEC].c_value)
582 volume->v_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_PREEXEC].c_value, pwd, path);
583 volume->v_preexec_close = options[VOLOPT_PREEXEC].i_value;
585 if (options[VOLOPT_POSTEXEC].c_value)
586 volume->v_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_POSTEXEC].c_value, pwd, path);
588 if (options[VOLOPT_ROOTPREEXEC].c_value)
589 volume->v_root_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPREEXEC].c_value, pwd, path);
590 volume->v_root_preexec_close = options[VOLOPT_ROOTPREEXEC].i_value;
592 if (options[VOLOPT_ROOTPOSTEXEC].c_value)
593 volume->v_root_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPOSTEXEC].c_value, pwd, path);
597 volume->v_next = Volumes;
602 /* ---------------- */
603 static char *myfgets( buf, size, fp )
612 while ((EOF != ( c = getc( fp )) ) && ( size > 0 )) {
613 if ( c == '\n' || c == '\r' ) {
631 /* check access list. this function wants something of the following
633 * @group,name,name2,@group2,name3
635 * a NULL argument allows everybody to have access.
636 * we return three things:
638 * 0: list exists, but name isn't in it
642 #ifndef NO_REAL_USER_NAME
643 /* authentication is case insensitive
644 * FIXME should we do the same with group name?
646 #define access_strcmp strcasecmp
649 #define access_strcmp strcmp
653 static int accessvol(args, name)
657 char buf[MAXPATHLEN + 1], *p;
663 strncpy(buf, args, sizeof(buf));
664 if ((p = strtok(buf, ",")) == NULL) /* nothing, return okay */
668 if (*p == '@') { /* it's a group */
669 if ((gr = getgrnam(p + 1)) && gmem(gr->gr_gid))
671 } else if (access_strcmp(p, name) == 0) /* it's a user name */
673 p = strtok(NULL, ",");
679 static void setextmap( ext, type, creator, user)
680 char *ext, *type, *creator;
686 if (Extmap == NULL) {
687 if (( Extmap = calloc(1, sizeof( struct extmap ))) == NULL ) {
688 LOG(log_error, logtype_afpd, "setextmap: calloc: %s", strerror(errno) );
693 for ( em = Extmap, cnt = 0; em->em_ext; em++, cnt++) {
694 if ( (strdiacasecmp( em->em_ext, ext )) == 0 ) {
699 if ( em->em_ext == NULL ) {
700 if (!(Extmap = realloc( Extmap, sizeof( struct extmap ) * (cnt +2))) ) {
701 LOG(log_error, logtype_afpd, "setextmap: realloc: %s", strerror(errno) );
704 (Extmap +cnt +1)->em_ext = NULL;
706 } else if ( !user ) {
712 if (!(em->em_ext = strdup( ext))) {
713 LOG(log_error, logtype_afpd, "setextmap: strdup: %s", strerror(errno) );
717 if ( *type == '\0' ) {
718 memcpy(em->em_type, "????", sizeof( em->em_type ));
720 memcpy(em->em_type, type, sizeof( em->em_type ));
722 if ( *creator == '\0' ) {
723 memcpy(em->em_creator, "UNIX", sizeof( em->em_creator ));
725 memcpy(em->em_creator, creator, sizeof( em->em_creator ));
729 /* -------------------------- */
730 static int extmap_cmp(const void *map1, const void *map2)
732 const struct extmap *em1 = map1;
733 const struct extmap *em2 = map2;
734 return strdiacasecmp(em1->em_ext, em2->em_ext);
737 static void sortextmap( void)
742 if ((em = Extmap) == NULL) {
750 qsort(Extmap, Extmap_cnt, sizeof(struct extmap), extmap_cmp);
755 /* ----------------------
757 static void free_extmap( void)
767 /* ----------------------
769 static int volfile_changed(struct afp_volume_name *p)
779 if (!stat( name, &st) && st.st_mtime > p->mtime) {
780 p->mtime = st.st_mtime;
786 /* ----------------------
787 * Read a volume configuration file and add the volumes contained within to
788 * the global volume list. If p2 is non-NULL, the file that is opened is
791 * Lines that begin with # and blank lines are ignored.
792 * Volume lines are of the form:
793 * <unix path> [<volume name>] [allow:<user>,<@group>,...] \
794 * [codepage:<file>] [casefold:<num>]
795 * <extension> TYPE [CREATOR]
797 static int readvolfile(obj, p1, p2, user, pwent)
799 struct afp_volume_name *p1;
802 struct passwd *pwent;
805 char path[ MAXPATHLEN + 1], tmp[ MAXPATHLEN + 1],
806 volname[ AFPVOL_NAMELEN + 1 ], buf[ BUFSIZ ],
807 type[ 5 ], creator[ 5 ];
810 struct vol_option options[VOLOPT_NUM], save_options[VOLOPT_NUM];
818 strcpy( path, p1->name );
825 p1->full_name = strdup(path);
828 if (NULL == ( fp = fopen( path, "r" )) ) {
832 if (fd != -1 && !fstat( fd, &st) ) {
833 p1->mtime = st.st_mtime;
836 memset(save_options, 0, sizeof(save_options));
837 while ( myfgets( buf, sizeof( buf ), fp ) != NULL ) {
838 initline( strlen( buf ), buf );
839 parseline( sizeof( path ) - 1, path );
846 /* change the default options for this file */
847 if (strncmp(path, VOLOPT_DEFAULT, VOLOPT_DEFAULT_LEN) == 0) {
849 for (i = 0; i < VOLOPT_NUM; i++) {
850 if (parseline( sizeof( path ) - VOLOPT_DEFAULT_LEN - 1,
851 path + VOLOPT_DEFAULT_LEN) < 0)
853 volset(save_options, NULL, tmp, sizeof(tmp) - 1,
854 path + VOLOPT_DEFAULT_LEN);
860 if (( p = strchr( path, '/' )) != NULL ) {
868 if ( u == NULL || *u == '\0' || ( pw = getpwnam( u )) == NULL ) {
871 strcpy( tmp, pw->pw_dir );
872 if ( p != NULL && *p != '\0' ) {
876 /* Tag a user's home directory with their umask. Note, this will
877 * be overwritten if the user actually specifies a umask: option
878 * for a '~' volume. */
879 save_options[VOLOPT_UMASK].i_value = obj->options.save_mask;
883 /* send path through variable substitution */
884 if (*path != '~') /* need to copy path to tmp */
887 pwent = getpwnam(obj->username);
888 volxlate(obj, path, sizeof(path) - 1, tmp, pwent, NULL);
890 /* this is sort of braindead. basically, i want to be
891 * able to specify things in any order, but i don't want to
892 * re-write everything.
894 * currently we have options:
902 * options:prodos,crlf,noadouble,ro...
907 * namemask:x,y,!z (not implemented yet)
909 memcpy(options, save_options, sizeof(options));
912 /* read in up to VOLOP_NUM possible options */
913 for (i = 0; i < VOLOPT_NUM; i++) {
914 if (parseline( sizeof( tmp ) - 1, tmp ) < 0)
917 volset(options, save_options, volname, sizeof(volname) - 1, tmp);
920 /* check allow/deny lists:
921 allow -> either no list (-1), or in list (1)
922 deny -> either no list (-1), or not in list (0) */
923 if (accessvol(options[VOLOPT_ALLOW].c_value, obj->username) &&
924 (accessvol(options[VOLOPT_DENY].c_value, obj->username) < 1)) {
926 /* handle read-only behaviour. semantics:
927 * 1) neither the rolist nor the rwlist exist -> rw
928 * 2) rolist exists -> ro if user is in it.
929 * 3) rwlist exists -> ro unless user is in it. */
930 if (((options[VOLOPT_FLAGS].i_value & AFPVOL_RO) == 0) &&
931 ((accessvol(options[VOLOPT_ROLIST].c_value,
932 obj->username) == 1) ||
933 !accessvol(options[VOLOPT_RWLIST].c_value,
935 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
937 /* do variable substitution for volname */
938 volxlate(obj, tmp, sizeof(tmp) - 1, volname, pwent, path);
939 creatvol(obj, pwent, path, tmp, options, p2 != NULL);
941 volfree(options, save_options);
945 parseline( sizeof( type ) - 1, type );
946 parseline( sizeof( creator ) - 1, creator );
947 setextmap( path, type, creator, user);
954 volfree(save_options, NULL);
956 if ( fclose( fp ) != 0 ) {
957 LOG(log_error, logtype_afpd, "readvolfile: fclose: %s", strerror(errno) );
963 /* ------------------------------- */
964 static void volume_free(struct vol *vol)
969 free(vol->v_password);
971 free(vol->v_volcodepage);
972 free(vol->v_maccodepage);
975 free(vol->v_forceuid);
976 free(vol->v_forcegid);
977 #endif /* FORCE_UIDGID */
980 /* ------------------------------- */
981 static void free_volumes(void )
984 struct vol *nvol, *ovol;
986 for ( vol = Volumes; vol; vol = vol->v_next ) {
987 if (( vol->v_flags & AFPVOL_OPEN ) ) {
994 for ( vol = Volumes, ovol = NULL; vol; vol = nvol) {
997 if (vol->v_name == NULL) {
998 if (Volumes == vol) {
1005 ovol->v_next = nvol;
1015 /* ------------------------------- */
1016 static void volume_unlink(struct vol *volume)
1018 struct vol *vol, *ovol, *nvol;
1020 if (volume == Volumes) {
1021 Volumes = Volumes->v_next;
1024 for ( vol = Volumes, ovol = NULL; vol; vol = nvol) {
1027 if (vol == volume) {
1032 ovol->v_next = nvol;
1043 static int getvolspace( vol, bfree, btotal, xbfree, xbtotal, bsize )
1045 u_int32_t *bfree, *btotal, *bsize;
1046 VolSpace *xbfree, *xbtotal;
1050 #ifndef NO_QUOTA_SUPPORT
1051 VolSpace qfree, qtotal;
1054 spaceflag = AFPVOL_GVSMASK & vol->v_flags;
1055 /* report up to 2GB if afp version is < 2.2 (4GB if not) */
1056 maxsize = (vol->v_flags & AFPVOL_A2VOL) ? 0x01fffe00 :
1057 (((afp_version < 22) || (vol->v_flags & AFPVOL_LIMITSIZE))
1058 ? 0x7fffffffL : 0xffffffffL);
1061 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_AFSGVS ) {
1062 if ( afs_getvolspace( vol, xbfree, xbtotal, bsize ) == AFP_OK ) {
1063 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_AFSGVS;
1064 goto getvolspace_done;
1069 if (( rc = ustatfs_getvolspace( vol, xbfree, xbtotal,
1070 bsize)) != AFP_OK ) {
1074 #define min(a,b) ((a)<(b)?(a):(b))
1075 #ifndef NO_QUOTA_SUPPORT
1076 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_UQUOTA ) {
1077 if ( uquota_getvolspace( vol, &qfree, &qtotal, *bsize ) == AFP_OK ) {
1078 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_UQUOTA;
1079 *xbfree = min(*xbfree, qfree);
1080 *xbtotal = min( *xbtotal, qtotal);
1081 goto getvolspace_done;
1085 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_USTATFS;
1088 *bfree = min( *xbfree, maxsize);
1089 *btotal = min( *xbtotal, maxsize);
1093 /* ----------------------- */
1094 static int getvolparams( bitmap, vol, st, buf, buflen )
1102 int bit = 0, isad = 1;
1105 u_int32_t bfree, btotal, bsize;
1106 VolSpace xbfree, xbtotal; /* extended bytes */
1107 char *data, *nameoff = NULL;
1110 /* courtesy of jallison@whistle.com:
1111 * For MacOS8.x support we need to create the
1112 * .Parent file here if it doesn't exist. */
1114 ad_init(&ad, vol->v_adouble);
1115 if ( ad_open( vol->v_path, vol_noadouble(vol) |
1116 ADFLAGS_HF|ADFLAGS_DIR, O_RDWR | O_CREAT,
1120 } else if (ad_get_HF_flags( &ad ) & O_CREAT) {
1121 slash = strrchr( vol->v_path, '/' );
1125 slash = vol->v_path;
1127 ad_setentrylen( &ad, ADEID_NAME, strlen( slash ));
1128 memcpy(ad_entry( &ad, ADEID_NAME ), slash,
1129 ad_getentrylen( &ad, ADEID_NAME ));
1130 ad_setdate(&ad, AD_DATE_CREATE | AD_DATE_UNIX, st->st_mtime);
1131 ad_flush(&ad, ADFLAGS_HF);
1134 if (( bitmap & ( (1<<VOLPBIT_BFREE)|(1<<VOLPBIT_BTOTAL) |
1135 (1<<VOLPBIT_XBFREE)|(1<<VOLPBIT_XBTOTAL) |
1136 (1<<VOLPBIT_BSIZE)) ) != 0 ) {
1137 if ( getvolspace( vol, &bfree, &btotal, &xbfree, &xbtotal,
1140 ad_close( &ad, ADFLAGS_HF );
1142 return( AFPERR_PARAM );
1147 while ( bitmap != 0 ) {
1148 while (( bitmap & 1 ) == 0 ) {
1156 if (0 == (vol->v_flags & AFPVOL_NOFILEID) && vol->v_cdb != NULL &&
1157 (vol->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1158 ashort = VOLPBIT_ATTR_FILEID;
1160 /* check for read-only.
1161 * NOTE: we don't actually set the read-only flag unless
1162 * it's passed in that way as it's possible to mount
1163 * a read-write filesystem under a read-only one. */
1164 if ((vol->v_flags & AFPVOL_RO) ||
1165 ((utime(vol->v_path, NULL) < 0) && (errno == EROFS))) {
1166 ashort |= VOLPBIT_ATTR_RO;
1168 ashort |= VOLPBIT_ATTR_CATSEARCH;
1169 if (afp_version >= 30) {
1170 ashort |= VOLPBIT_ATTR_UTF8;
1171 if (vol->v_flags & AFPVOL_UNIX_PRIV)
1172 ashort |= VOLPBIT_ATTR_UNIXPRIV;
1174 ashort = htons(ashort);
1175 memcpy(data, &ashort, sizeof( ashort ));
1176 data += sizeof( ashort );
1180 ashort = htons( AFPVOLSIG_DEFAULT );
1181 memcpy(data, &ashort, sizeof( ashort ));
1182 data += sizeof( ashort );
1185 case VOLPBIT_CDATE :
1186 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
1187 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1188 memcpy(data, &aint, sizeof( aint ));
1189 data += sizeof( aint );
1192 case VOLPBIT_MDATE :
1193 if ( st->st_mtime > vol->v_time ) {
1194 vol->v_time = st->st_mtime;
1195 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1197 aint = AD_DATE_FROM_UNIX(vol->v_time);
1199 memcpy(data, &aint, sizeof( aint ));
1200 data += sizeof( aint );
1203 case VOLPBIT_BDATE :
1204 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1205 aint = AD_DATE_START;
1206 memcpy(data, &aint, sizeof( aint ));
1207 data += sizeof( aint );
1211 memcpy(data, &vol->v_vid, sizeof( vol->v_vid ));
1212 data += sizeof( vol->v_vid );
1215 case VOLPBIT_BFREE :
1216 bfree = htonl( bfree );
1217 memcpy(data, &bfree, sizeof( bfree ));
1218 data += sizeof( bfree );
1221 case VOLPBIT_BTOTAL :
1222 btotal = htonl( btotal );
1223 memcpy(data, &btotal, sizeof( btotal ));
1224 data += sizeof( btotal );
1227 #ifndef NO_LARGE_VOL_SUPPORT
1228 case VOLPBIT_XBFREE :
1229 xbfree = hton64( xbfree );
1230 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1231 bcopy(&xbfree, data, sizeof(xbfree));
1232 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1233 memcpy(data, &xbfree, sizeof( xbfree ));
1234 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1235 data += sizeof( xbfree );
1238 case VOLPBIT_XBTOTAL :
1239 xbtotal = hton64( xbtotal );
1240 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1241 bcopy(&xbtotal, data, sizeof(xbtotal));
1242 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1243 memcpy(data, &xbtotal, sizeof( xbtotal ));
1244 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1245 data += sizeof( xbfree );
1247 #endif /* ! NO_LARGE_VOL_SUPPORT */
1251 data += sizeof( u_int16_t );
1254 case VOLPBIT_BSIZE: /* block size */
1255 bsize = htonl(bsize);
1256 memcpy(data, &bsize, sizeof(bsize));
1257 data += sizeof(bsize);
1262 ad_close( &ad, ADFLAGS_HF );
1264 return( AFPERR_BITMAP );
1270 ashort = htons( data - buf );
1271 memcpy(nameoff, &ashort, sizeof( ashort ));
1272 aint = strlen( vol->v_name );
1274 memcpy(data, vol->v_name, aint );
1278 ad_close( &ad, ADFLAGS_HF );
1280 *buflen = data - buf;
1284 /* ------------------------- */
1285 int static stat_vol(u_int16_t bitmap, struct vol *vol, char *rbuf, int *rbuflen)
1290 if ( stat( vol->v_path, &st ) < 0 ) {
1292 return( AFPERR_PARAM );
1295 buflen = *rbuflen - sizeof( bitmap );
1296 if (( ret = getvolparams( bitmap, vol, &st,
1297 rbuf + sizeof( bitmap ), &buflen )) != AFP_OK ) {
1301 *rbuflen = buflen + sizeof( bitmap );
1302 bitmap = htons( bitmap );
1303 memcpy(rbuf, &bitmap, sizeof( bitmap ));
1308 /* ------------------------------- */
1309 void load_volumes(AFPObj *obj)
1311 struct passwd *pwent;
1316 /* check files date */
1317 if (obj->options.defaultvol.loaded) {
1318 changed = volfile_changed(&obj->options.defaultvol);
1320 if (obj->options.systemvol.loaded) {
1321 changed |= volfile_changed(&obj->options.systemvol);
1323 if (obj->options.uservol.loaded) {
1324 changed |= volfile_changed(&obj->options.uservol);
1333 pwent = getpwnam(obj->username);
1334 if ( (obj->options.flags & OPTION_USERVOLFIRST) == 0 ) {
1335 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent);
1338 if ((*obj->username == '\0') || (obj->options.flags & OPTION_NOUSERVOL)) {
1339 readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent);
1342 * Read user's AppleVolumes or .AppleVolumes file
1343 * If neither are readable, read the default volumes file. if
1344 * that doesn't work, create a user share.
1346 obj->options.uservol.name = strdup(pwent->pw_dir);
1347 if ( readvolfile(obj, &obj->options.uservol, "AppleVolumes", 1, pwent) < 0 &&
1348 readvolfile(obj, &obj->options.uservol, ".AppleVolumes", 1, pwent) < 0 &&
1349 readvolfile(obj, &obj->options.uservol, "applevolumes", 1, pwent) < 0 &&
1350 readvolfile(obj, &obj->options.uservol, ".applevolumes", 1, pwent) < 0 &&
1351 obj->options.defaultvol.name != NULL ) {
1352 if (readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent) < 0)
1353 creatvol(obj, pwent, pwent->pw_dir, NULL, NULL, 1);
1356 if ( obj->options.flags & OPTION_USERVOLFIRST ) {
1357 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent );
1361 /* ------------------------------- */
1362 int afp_getsrvrparms(obj, ibuf, ibuflen, rbuf, rbuflen )
1365 int ibuflen, *rbuflen;
1376 for ( vcnt = 0, volume = Volumes; volume; volume = volume->v_next ) {
1377 if (!(volume->v_flags & AFPVOL_NOSTAT)) {
1378 if ( stat( volume->v_path, &st ) < 0 ) {
1379 LOG(log_info, logtype_afpd, "afp_getsrvrparms: stat %s: %s",
1380 volume->v_path, strerror(errno) );
1381 continue; /* can't access directory */
1383 if (!S_ISDIR(st.st_mode)) {
1384 continue; /* not a dir */
1387 if (volume->v_hide) {
1388 continue; /* config file changed but the volume was mounted */
1391 /* set password bit if there's a volume password */
1392 *data = (volume->v_password) ? AFPSRVR_PASSWD : 0;
1394 /* Apple 2 clients running ProDOS-8 expect one volume to have
1395 bit 0 of this byte set. They will not recognize anything
1396 on the server unless this is the case. I have not
1397 completely worked this out, but it's related to booting
1398 from the server. Support for that function is a ways
1399 off.. <shirsch@ibm.net> */
1400 *data |= (volume->v_flags & AFPVOL_A2VOL) ? AFPSRVR_CONFIGINFO : 0;
1401 *data++ |= 0; /* UNIX PRIVS BIT ..., OSX doesn't seem to use it, so we don't either */
1402 len = strlen( volume->v_name );
1404 memcpy(data, volume->v_name, len );
1409 *rbuflen = data - rbuf;
1411 if ( gettimeofday( &tv, 0 ) < 0 ) {
1412 LOG(log_error, logtype_afpd, "afp_getsrvrparms: gettimeofday: %s", strerror(errno) );
1414 return AFPERR_PARAM;
1416 tv.tv_sec = AD_DATE_FROM_UNIX(tv.tv_sec);
1417 memcpy(data, &tv.tv_sec, sizeof( u_int32_t));
1418 data += sizeof( u_int32_t);
1423 /* -------------------------
1424 * we are the user here
1426 int afp_openvol(obj, ibuf, ibuflen, rbuf, rbuflen )
1429 int ibuflen, *rbuflen;
1440 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1441 bitmap = ntohs( bitmap );
1442 ibuf += sizeof( bitmap );
1443 if (( bitmap & (1<<VOLPBIT_VID)) == 0 ) {
1445 return AFPERR_BITMAP;
1448 len = (unsigned char)*ibuf++;
1449 volname = obj->oldtmp;
1450 memcpy(volname, ibuf, len );
1451 *(volname + len) = '\0';
1453 if ((len + 1) & 1) /* pad to an even boundary */
1458 for ( volume = Volumes; volume; volume = volume->v_next ) {
1459 if ( strcasecmp( volname, volume->v_name ) == 0 ) {
1464 if ( volume == NULL ) {
1466 return AFPERR_PARAM;
1469 /* check for a volume password */
1470 if (volume->v_password && strncmp(ibuf, volume->v_password, VOLPASSLEN)) {
1472 return AFPERR_ACCESS;
1475 if (( volume->v_flags & AFPVOL_OPEN ) ) {
1476 /* the volume is already open */
1478 set_uidgid ( volume );
1480 return stat_vol(bitmap, volume, rbuf, rbuflen);
1483 /* initialize volume variables
1486 if (afp_version >= 30) {
1487 volume->max_filename = 255;
1490 volume->max_filename = MACFILELEN;
1493 volume->v_dir = volume->v_root = NULL;
1495 /* FIXME unix name != mac name */
1496 if ((dir = dirnew(volume->v_name, volume->v_name) ) == NULL) {
1497 LOG(log_error, logtype_afpd, "afp_openvol: malloc: %s", strerror(errno) );
1502 dir->d_did = DIRDID_ROOT;
1503 dir->d_color = DIRTREE_COLOR_BLACK; /* root node is black */
1504 volume->v_dir = volume->v_root = dir;
1505 volume->v_flags |= AFPVOL_OPEN;
1506 volume->v_cdb = NULL;
1508 if (volume->v_root_preexec) {
1509 if ((ret = afprun(1, volume->v_root_preexec, NULL)) && volume->v_root_preexec_close) {
1510 LOG(log_error, logtype_afpd, "afp_openvol: root preexec : %d", ret );
1517 set_uidgid ( volume );
1520 if (volume->v_preexec) {
1521 if ((ret = afprun(0, volume->v_preexec, NULL)) && volume->v_preexec_close) {
1522 LOG(log_error, logtype_afpd, "afp_openvol: preexec : %d", ret );
1528 if ( stat( volume->v_path, &st ) < 0 ) {
1533 if ( chdir( volume->v_path ) < 0 ) {
1537 curdir = volume->v_dir;
1538 if (volume->v_cnidscheme == NULL) {
1539 volume->v_cnidscheme = strdup(DEFAULT_CNID_SCHEME);
1540 LOG(log_warning, logtype_afpd, "Warning: No CNID scheme for volume %s. Using default.",
1543 if (volume->v_dbpath)
1544 volume->v_cdb = cnid_open (volume->v_dbpath, volume->v_umask, volume->v_cnidscheme);
1545 if (volume->v_cdb == NULL)
1546 volume->v_cdb = cnid_open (volume->v_path, volume->v_umask, volume->v_cnidscheme);
1547 if (volume->v_cdb == NULL) {
1548 volume->v_cdb = cnid_open (volume->v_path, volume->v_umask, DEFAULT_CNID_SCHEME);
1549 LOG(log_error, logtype_afpd, "Error: invalid CNID backend for %s: %s", volume->v_path,
1550 volume->v_cnidscheme);
1552 if (volume->v_cdb == NULL) {
1553 LOG(log_error, logtype_afpd, "Fatal error: cannot open CNID for %s", volume->v_path);
1560 if (!volume->v_volcodepage)
1561 volume->v_volcodepage = strdup("UTF8");
1563 if ( (charset_t) -1 == ( volume->v_volcharset = add_charset(volume->v_volcodepage)) ) {
1564 LOG (log_error, logtype_afpd, "Setting codepage %s as volume codepage failed", volume->v_volcodepage);
1569 if ( NULL == ( volume->v_vol = find_charset_functions(volume->v_volcodepage)) || volume->v_vol->flags & CHARSET_ICONV ) {
1570 LOG (log_warning, logtype_afpd, "WARNING: volume encoding %s is *not* supported by netatalk, expect problems !!!!", volume->v_volcodepage);
1573 if (!volume->v_maccodepage)
1574 volume->v_maccodepage = strdup(obj->options.maccodepage);
1576 if ( (charset_t) -1 == ( volume->v_maccharset = add_charset(volume->v_maccodepage)) ) {
1577 LOG (log_error, logtype_afpd, "Setting codepage %s as mac codepage failed", volume->v_maccodepage);
1582 if ( NULL == ( volume->v_mac = find_charset_functions(volume->v_maccodepage)) || ! (volume->v_mac->flags & CHARSET_CLIENT) ) {
1583 LOG (log_error, logtype_afpd, "Fatal error: mac charset %s not supported", volume->v_maccodepage);
1588 ret = stat_vol(bitmap, volume, rbuf, rbuflen);
1589 if (ret == AFP_OK) {
1591 * If you mount a volume twice, the second time the trash appears on
1592 * the desk-top. That's because the Mac remembers the DID for the
1593 * trash (even for volumes in different zones, on different servers).
1594 * Just so this works better, we prime the DID cache with the trash,
1595 * fixing the trash at DID 17.
1596 * FIXME (RL): should it be done inside a CNID backend ? (always returning Trash DID when asked) ?
1598 if ((volume->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1600 /* FIXME find db time stamp */
1601 cnid_getstamp(volume->v_cdb, volume->v_stamp, sizeof(volume->v_stamp));
1603 cname( volume, volume->v_dir, &p );
1609 if (volume->v_dir) {
1610 dirfree( volume->v_dir );
1611 volume->v_dir = volume->v_root = NULL;
1614 volume->v_flags &= ~AFPVOL_OPEN;
1615 if (volume->v_cdb != NULL) {
1616 cnid_close(volume->v_cdb);
1617 volume->v_cdb = NULL;
1623 /* ------------------------- */
1624 static void closevol(struct vol *vol)
1629 dirfree( vol->v_root );
1631 if (vol->v_cdb != NULL) {
1632 cnid_close(vol->v_cdb);
1636 if (vol->v_postexec) {
1637 afprun(0, vol->v_postexec, NULL);
1639 if (vol->v_root_postexec) {
1640 afprun(1, vol->v_root_postexec, NULL);
1644 /* ------------------------- */
1645 void close_all_vol(void)
1649 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
1650 if ( ovol->v_flags & AFPVOL_OPEN ) {
1651 ovol->v_flags &= ~AFPVOL_OPEN;
1657 /* ------------------------- */
1658 int afp_closevol(obj, ibuf, ibuflen, rbuf, rbuflen )
1661 int ibuflen, *rbuflen;
1663 struct vol *vol, *ovol;
1668 memcpy(&vid, ibuf, sizeof( vid ));
1669 if (NULL == ( vol = getvolbyvid( vid )) ) {
1670 return( AFPERR_PARAM );
1673 vol->v_flags &= ~AFPVOL_OPEN;
1674 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
1675 if ( ovol->v_flags & AFPVOL_OPEN ) {
1679 if ( ovol != NULL ) {
1680 /* Even if chdir fails, we can't say afp_closevol fails. */
1681 if ( chdir( ovol->v_path ) == 0 ) {
1682 curdir = ovol->v_dir;
1687 if (vol->v_deleted) {
1688 showvol(vol->v_name);
1695 /* ------------------------- */
1696 struct vol *getvolbyvid(const u_int16_t vid )
1700 for ( vol = Volumes; vol; vol = vol->v_next ) {
1701 if ( vid == vol->v_vid ) {
1705 if ( vol == NULL || ( vol->v_flags & AFPVOL_OPEN ) == 0 ) {
1711 #endif /* FORCE_UIDGID */
1716 /* ------------------------ */
1717 static int ext_cmp_key(const void *key, const void *obj)
1719 const char *p = key;
1720 const struct extmap *em = obj;
1721 return strdiacasecmp(p, em->em_ext);
1723 struct extmap *getextmap(const char *path)
1728 if (NULL == ( p = strrchr( path, '.' )) ) {
1729 return( Defextmap );
1732 if (!*p || !Extmap_cnt) {
1733 return( Defextmap );
1735 em = bsearch(p, Extmap, Extmap_cnt, sizeof(struct extmap), ext_cmp_key);
1739 return( Defextmap );
1743 /* ------------------------- */
1744 struct extmap *getdefextmap(void)
1746 return( Defextmap );
1749 /* --------------------------
1750 poll if a volume is changed by other processes.
1752 int pollvoltime(obj)
1759 if (!(afp_version > 21 && obj->options.server_notif))
1762 if ( gettimeofday( &tv, 0 ) < 0 )
1765 for ( vol = Volumes; vol; vol = vol->v_next ) {
1766 if ( (vol->v_flags & AFPVOL_OPEN) && vol->v_time + 30 < tv.tv_sec) {
1767 if ( !stat( vol->v_path, &st ) && vol->v_time != st.st_mtime ) {
1768 vol->v_time = st.st_mtime;
1769 if (!obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED))
1778 /* ------------------------- */
1779 void setvoltime(obj, vol )
1785 /* just looking at vol->v_time is broken seriously since updates
1786 * from other users afpd processes never are seen.
1787 * This is not the most elegant solution (a shared memory between
1788 * the afpd processes would come closer)
1791 if ( gettimeofday( &tv, 0 ) < 0 ) {
1792 LOG(log_error, logtype_afpd, "setvoltime: gettimeofday: %s", strerror(errno) );
1795 if( utime( vol->v_path, NULL ) < 0 ) {
1796 /* write of time failed ... probably a read only filesys,
1797 * where no other users can interfere, so there's no issue here
1801 /* a little granularity */
1802 if (vol->v_time < tv.tv_sec) {
1803 vol->v_time = tv.tv_sec;
1804 if (afp_version > 21 && obj->options.server_notif) {
1805 obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED);
1810 /* ------------------------- */
1811 int afp_getvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1814 int ibuflen, *rbuflen;
1817 u_int16_t vid, bitmap;
1820 memcpy(&vid, ibuf, sizeof( vid ));
1821 ibuf += sizeof( vid );
1822 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1823 bitmap = ntohs( bitmap );
1825 if (NULL == ( vol = getvolbyvid( vid )) ) {
1827 return( AFPERR_PARAM );
1830 return stat_vol(bitmap, vol, rbuf, rbuflen);
1833 /* ------------------------- */
1834 int afp_setvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1837 int ibuflen, *rbuflen;
1841 u_int16_t vid, bitmap;
1847 memcpy(&vid, ibuf, sizeof( vid ));
1848 ibuf += sizeof( vid );
1849 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1850 bitmap = ntohs( bitmap );
1851 ibuf += sizeof(bitmap);
1853 if (( vol = getvolbyvid( vid )) == NULL ) {
1854 return( AFPERR_PARAM );
1857 if (vol->v_flags & AFPVOL_RO)
1858 return AFPERR_VLOCK;
1860 /* we can only set the backup date. */
1861 if (bitmap != (1 << VOLPBIT_BDATE))
1862 return AFPERR_BITMAP;
1864 ad_init(&ad, vol->v_adouble);
1865 if ( ad_open( vol->v_path, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR,
1868 return AFPERR_VLOCK;
1870 return AFPERR_ACCESS;
1873 memcpy(&aint, ibuf, sizeof(aint));
1874 ad_setdate(&ad, AD_DATE_BACKUP, aint);
1875 ad_flush(&ad, ADFLAGS_HF);
1876 ad_close(&ad, ADFLAGS_HF);
1880 /* ------------------------- */
1881 int wincheck(const struct vol *vol, const char *path)
1885 if (!(vol->v_flags & AFPVOL_MSWINDOWS))
1888 /* empty paths are not allowed */
1889 if ((len = strlen(path)) == 0)
1892 /* leading or trailing whitespaces are not allowed, carriage returns
1893 * and probably other whitespace is okay, tabs are not allowed
1895 if ((path[0] == ' ') || (path[len-1] == ' '))
1898 /* certain characters are not allowed */
1899 if (strpbrk(path, MSWINDOWS_BADCHARS))
1902 /* everything else is okay */