2 * $Id: volume.c,v 1.51.2.7.2.9 2003-10-30 05:57:44 bfernhomberg Exp $
4 * Copyright (c) 1990,1993 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
10 #endif /* HAVE_CONFIG_H */
22 #endif /* HAVE_FCNTL_H */
29 #else /* STDC_HEADERS */
33 #endif /* HAVE_STRCHR */
34 char *strchr (), *strrchr ();
36 #define memcpy(d,s,n) bcopy ((s), (d), (n))
37 #define memmove(d,s,n) bcopy ((s), (d), (n))
38 #endif /* ! HAVE_MEMCPY */
39 #endif /* STDC_HEADERS */
41 #include <sys/types.h>
43 #include <sys/param.h>
44 #include <sys/socket.h>
45 #include <netinet/in.h>
46 #include <arpa/inet.h>
47 #include <netatalk/endian.h>
48 #include <atalk/asp.h>
49 #include <atalk/dsi.h>
50 #include <atalk/adouble.h>
51 #include <atalk/afp.h>
52 #include <atalk/util.h>
53 #include <atalk/logger.h>
55 #include <atalk/cnid.h>
59 #include "directory.h"
64 extern int afprun(int root, char *cmd, int *outfd);
67 #define MIN(a, b) ((a) < (b) ? (a) : (b))
70 #ifndef NO_LARGE_VOL_SUPPORT
71 #if BYTE_ORDER == BIG_ENDIAN
74 #else /* BYTE_ORDER == BIG_ENDIAN */
75 #define hton64(x) ((u_int64_t) (htonl(((x) >> 32) & 0xffffffffLL)) | \
76 (u_int64_t) ((htonl(x) & 0xffffffffLL) << 32))
77 #define ntoh64(x) (hton64(x))
78 #endif /* BYTE_ORDER == BIG_ENDIAN */
79 #endif /* ! NO_LARGE_VOL_SUPPORT */
81 static struct vol *Volumes = NULL;
82 static u_int16_t lastvid = 0;
83 static char *Trash = "\02\024Network Trash Folder";
85 static struct extmap *Extmap = NULL, *Defextmap = NULL;
86 static int Extmap_cnt;
87 static void free_extmap(void);
89 #define VOLOPT_ALLOW 0 /* user allow list */
90 #define VOLOPT_DENY 1 /* user deny list */
91 #define VOLOPT_RWLIST 2 /* user rw list */
92 #define VOLOPT_ROLIST 3 /* user ro list */
93 #define VOLOPT_PASSWORD 4 /* volume password */
94 #define VOLOPT_CASEFOLD 5 /* character case mangling */
95 #define VOLOPT_FLAGS 6 /* various flags */
96 #define VOLOPT_DBPATH 7 /* path to database */
97 #define VOLOPT_MAPCHARS 8 /* 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 typedef struct _special_folder {
148 static const _special_folder special_folders[] = {
149 {"Network Trash Folder", 1, 0777, 1},
150 {"Temporary Items", 1, 0777, 1},
152 {"TheFindByContentFolder", 0, 0, 1},
153 {"TheVolumeSettingsFolder", 0, 0, 1},
157 static void handle_special_folders (const struct vol *);
159 static __inline__ void volfree(struct vol_option *options,
160 const struct vol_option *save)
165 for (i = 0; i < VOLOPT_MAX; i++) {
166 if (options[i].c_value && (options[i].c_value != save[i].c_value))
167 free(options[i].c_value);
170 for (i = 0; i < VOLOPT_MAX; i++) {
171 if (options[i].c_value)
172 free(options[i].c_value);
178 /* handle variable substitutions. here's what we understand:
179 * $b -> basename of path
180 * $c -> client ip/appletalk address
181 * $d -> volume pathname on server
182 * $f -> full name (whatever's in the gecos field)
185 * $s -> server name (hostname if it doesn't exist)
186 * $u -> username (guest is usually nobody)
187 * $v -> volume name (ADEID_NAME or basename if ADEID_NAME is empty)
188 * $z -> zone (may not exist)
191 #define is_var(a, b) (strncmp((a), (b), 2) == 0)
193 static char *volxlate(AFPObj *obj, char *dest, size_t destlen,
194 char *src, struct passwd *pwd, char *path)
204 dest = calloc(destlen +1, 1);
210 strncpy(dest, src, destlen);
211 if ((p = strchr(src, '$')) == NULL) /* nothing to do */
214 /* first part of the path. just forward to the next variable. */
215 len = MIN(p - src, destlen);
221 while (p && destlen > 0) {
222 /* now figure out what the variable is */
224 if (is_var(p, "$b")) {
226 if ((q = strrchr(path, '/')) == NULL)
228 else if (*(q + 1) != '\0')
231 } else if (is_var(p, "$c")) {
232 if (obj->proto == AFPPROTO_ASP) {
233 ASP asp = obj->handle;
235 len = sprintf(dest, "%u.%u", ntohs(asp->asp_sat.sat_addr.s_net),
236 asp->asp_sat.sat_addr.s_node);
240 } else if (obj->proto == AFPPROTO_DSI) {
241 DSI *dsi = obj->handle;
243 len = sprintf(dest, "%s:%u", inet_ntoa(dsi->client.sin_addr),
244 ntohs(dsi->client.sin_port));
248 } else if (is_var(p, "$d")) {
250 } else if (is_var(p, "$f")) {
251 if ((q = strchr(pwd->pw_gecos, ',')))
254 } else if (is_var(p, "$g")) {
255 struct group *grp = getgrgid(pwd->pw_gid);
258 } else if (is_var(p, "$h")) {
259 q = obj->options.hostname;
260 } else if (is_var(p, "$s")) {
263 else if (obj->options.server) {
264 q = obj->options.server;
266 q = obj->options.hostname;
267 } else if (is_var(p, "$u")) {
269 } else if (is_var(p, "$v")) {
274 if (ad_open(path, ADFLAGS_HF, O_RDONLY, 0, &ad) < 0)
277 if ((len = MIN(ad_getentrylen(&ad, ADEID_NAME), destlen)) > 0) {
278 memcpy(dest, ad_entry(&ad, ADEID_NAME), len);
279 ad_close(&ad, ADFLAGS_HF);
283 ad_close(&ad, ADFLAGS_HF);
284 no_volname: /* simple basename */
285 if ((q = strrchr(path, '/')) == NULL)
287 else if (*(q + 1) != '\0')
291 } else if (is_var(p, "$z")) {
293 } else if (is_var(p, "$$")) {
298 /* copy the stuff over. if we don't understand something that we
299 * should, just skip it over. */
301 len = MIN(p == q ? 2 : strlen(q), destlen);
302 strncpy(dest, q, len);
307 /* stuff up to next $ */
309 p = strchr(src, '$');
310 len = p ? MIN(p - src, destlen) : destlen;
312 strncpy(dest, src, len);
320 /* to make sure that val is valid, make sure to select an opt that
322 static int optionok(const char *buf, const char *opt, const char *val)
324 if (!strstr(buf,opt))
332 /* -------------------- */
333 static void setoption(struct vol_option *options, struct vol_option *save, int opt, const char *val)
335 if (options[opt].c_value && (!save || options[opt].c_value != save[opt].c_value))
336 free(options[opt].c_value);
337 options[opt].c_value = strdup(val + 1);
340 /* ------------------------------------------
341 handle all the options. tmp can't be NULL. */
342 static void volset(struct vol_option *options, struct vol_option *save,
343 char *volname, int vlen,
348 val = strchr(tmp, ':');
350 /* we'll assume it's a volume name. */
351 strncpy(volname, tmp, vlen);
356 LOG(log_debug, logtype_afpd, "Parsing volset %s", val);
358 if (optionok(tmp, "allow:", val)) {
359 setoption(options, save, VOLOPT_ALLOW, val);
361 } else if (optionok(tmp, "deny:", val)) {
362 setoption(options, save, VOLOPT_DENY, val);
364 } else if (optionok(tmp, "rwlist:", val)) {
365 setoption(options, save, VOLOPT_RWLIST, val);
367 } else if (optionok(tmp, "rolist:", val)) {
368 setoption(options, save, VOLOPT_ROLIST, val);
370 } else if (optionok(tmp, "codepage:", val)) {
371 LOG (log_error, logtype_afpd, "The old codepage system has been removed. Please make sure to read the documentation !!!!");
372 /* Make sure we don't screw anything */
374 } else if (optionok(tmp, "volcharset:", val)) {
375 setoption(options, save, VOLOPT_ENCODING, val);
376 } else if (optionok(tmp, "maccharset:", val)) {
377 setoption(options, save, VOLOPT_MACCHARSET, val);
378 } else if (optionok(tmp, "veto:", val)) {
379 setoption(options, save, VOLOPT_VETO, val);
380 } else if (optionok(tmp, "cnidscheme:", val)) {
381 setoption(options, save, VOLOPT_CNIDSCHEME, val);
382 } else if (optionok(tmp, "casefold:", val)) {
383 if (strcasecmp(val + 1, "tolower") == 0)
384 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMLOWER;
385 else if (strcasecmp(val + 1, "toupper") == 0)
386 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMUPPER;
387 else if (strcasecmp(val + 1, "xlatelower") == 0)
388 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UUPPERMLOWER;
389 else if (strcasecmp(val + 1, "xlateupper") == 0)
390 options[VOLOPT_CASEFOLD].i_value = AFPVOL_ULOWERMUPPER;
391 } else if (optionok(tmp, "adouble:", val)) {
392 if (strcasecmp(val + 1, "v1") == 0)
393 options[VOLOPT_ADOUBLE].i_value = AD_VERSION1;
394 #if AD_VERSION == AD_VERSION2
395 else if (strcasecmp(val + 1, "v2") == 0)
396 options[VOLOPT_ADOUBLE].i_value = AD_VERSION2;
398 } else if (optionok(tmp, "options:", val)) {
401 if ((p = strtok(val + 1, ",")) == NULL) /* nothing */
405 if (strcasecmp(p, "prodos") == 0)
406 options[VOLOPT_FLAGS].i_value |= AFPVOL_A2VOL;
407 else if (strcasecmp(p, "mswindows") == 0) {
408 options[VOLOPT_FLAGS].i_value |= AFPVOL_MSWINDOWS;
409 } else if (strcasecmp(p, "crlf") == 0)
410 options[VOLOPT_FLAGS].i_value |= AFPVOL_CRLF;
411 else if (strcasecmp(p, "noadouble") == 0)
412 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOADOUBLE;
413 else if (strcasecmp(p, "ro") == 0)
414 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
415 else if (strcasecmp(p, "nohex") == 0)
416 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOHEX;
417 else if (strcasecmp(p, "usedots") == 0)
418 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS;
419 else if (strcasecmp(p, "limitsize") == 0)
420 options[VOLOPT_FLAGS].i_value |= AFPVOL_LIMITSIZE;
421 /* support for either "dropbox" or "dropkludge" */
422 else if (strcasecmp(p, "dropbox") == 0)
423 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
424 else if (strcasecmp(p, "dropkludge") == 0)
425 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
426 else if (strcasecmp(p, "nofileid") == 0)
427 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOFILEID;
428 else if (strcasecmp(p, "nostat") == 0)
429 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOSTAT;
430 else if (strcasecmp(p, "preexec_close") == 0)
431 options[VOLOPT_PREEXEC].i_value = 1;
432 else if (strcasecmp(p, "root_preexec_close") == 0)
433 options[VOLOPT_ROOTPREEXEC].i_value = 1;
434 else if (strcasecmp(p, "upriv") == 0)
435 options[VOLOPT_FLAGS].i_value |= AFPVOL_UNIX_PRIV;
437 p = strtok(NULL, ",");
440 } else if (optionok(tmp, "dbpath:", val)) {
441 setoption(options, save, VOLOPT_DBPATH, val);
443 } else if (optionok(tmp, "umask:", val)) {
444 options[VOLOPT_UMASK].i_value = (int)strtol(val, (char **)NULL, 8);
445 } else if (optionok(tmp, "mapchars:",val)) {
446 setoption(options, save, VOLOPT_MAPCHARS, val);
448 } else if (optionok(tmp, "password:", val)) {
449 setoption(options, save, VOLOPT_PASSWORD, val);
453 /* this code allows forced uid/gid per volume settings */
454 } else if (optionok(tmp, "forceuid:", val)) {
455 setoption(options, save, VOLOPT_FORCEUID, val);
456 } else if (optionok(tmp, "forcegid:", val)) {
457 setoption(options, save, VOLOPT_FORCEGID, val);
459 #endif /* FORCE_UIDGID */
460 } else if (optionok(tmp, "root_preexec:", val)) {
461 setoption(options, save, VOLOPT_ROOTPREEXEC, val);
463 } else if (optionok(tmp, "preexec:", val)) {
464 setoption(options, save, VOLOPT_PREEXEC, val);
466 } else if (optionok(tmp, "root_postexec:", val)) {
467 setoption(options, save, VOLOPT_ROOTPOSTEXEC, val);
469 } else if (optionok(tmp, "postexec:", val)) {
470 setoption(options, save, VOLOPT_POSTEXEC, val);
473 /* ignore unknown options */
474 LOG(log_debug, logtype_afpd, "ignoring unknown volume option: %s", tmp);
479 /* ----------------- */
480 static void showvol(const char *name)
483 for ( volume = Volumes; volume; volume = volume->v_next ) {
484 if ( !strcasecmp( volume->v_name, name ) && volume->v_hide) {
491 /* ------------------------------- */
492 static int creatvol(AFPObj *obj, struct passwd *pwd,
493 char *path, char *name,
494 struct vol_option *options,
495 const int user /* user defined volume */
502 if ( name == NULL || *name == '\0' ) {
503 if ((name = strrchr( path, '/' )) == NULL) {
504 return -1; /* Obviously not a fully qualified path */
507 /* if you wish to share /, you need to specify a name. */
512 for ( volume = Volumes; volume; volume = volume->v_next ) {
513 if ( strcasecmp( volume->v_name, name ) == 0 ) {
514 if (volume->v_deleted) {
518 return -1; /* Won't be able to access it, anyway... */
523 vlen = strlen( name );
524 if ( vlen > AFPVOL_NAMELEN ) {
525 vlen = AFPVOL_NAMELEN;
526 name[AFPVOL_NAMELEN] = '\0';
529 if (!( volume = (struct vol *)calloc(1, sizeof( struct vol ))) ) {
530 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
533 if (! ( volume->v_name = (char *)malloc( vlen + 1 ) ) ) {
534 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
538 if (!( volume->v_path = (char *)malloc( strlen( path ) + 1 )) ) {
539 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
540 free(volume->v_name);
544 volume->v_hide = hide;
545 strcpy( volume->v_name, name);
546 strcpy( volume->v_path, path );
550 #endif /* __svr4__ */
551 /* os X start at 1 and use network order ie. 1 2 3 */
552 volume->v_vid = ++lastvid;
553 volume->v_vid = htons(volume->v_vid);
557 /* should we casefold? */
558 volume->v_casefold = options[VOLOPT_CASEFOLD].i_value;
560 /* shift in some flags */
561 volume->v_flags = options[VOLOPT_FLAGS].i_value;
563 if (options[VOLOPT_PASSWORD].c_value)
564 volume->v_password = strdup(options[VOLOPT_PASSWORD].c_value);
566 if (options[VOLOPT_VETO].c_value)
567 volume->v_veto = strdup(options[VOLOPT_VETO].c_value);
569 if (options[VOLOPT_ENCODING].c_value)
570 volume->v_volcodepage = strdup(options[VOLOPT_ENCODING].c_value);
572 if (options[VOLOPT_MACCHARSET].c_value)
573 volume->v_maccodepage = strdup(options[VOLOPT_MACCHARSET].c_value);
575 if (options[VOLOPT_DBPATH].c_value)
576 volume->v_dbpath = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_DBPATH].c_value, pwd, path);
578 if (options[VOLOPT_CNIDSCHEME].c_value)
579 volume->v_cnidscheme = strdup(options[VOLOPT_CNIDSCHEME].c_value);
581 if (options[VOLOPT_UMASK].i_value)
582 volume->v_umask = (mode_t)options[VOLOPT_UMASK].i_value;
584 if (options[VOLOPT_ADOUBLE].i_value)
585 volume->v_adouble = options[VOLOPT_ADOUBLE].i_value;
587 volume->v_adouble = AD_VERSION;
589 if (options[VOLOPT_FORCEUID].c_value) {
590 volume->v_forceuid = strdup(options[VOLOPT_FORCEUID].c_value);
592 volume->v_forceuid = NULL; /* set as null so as to return 0 later on */
595 if (options[VOLOPT_FORCEGID].c_value) {
596 volume->v_forcegid = strdup(options[VOLOPT_FORCEGID].c_value);
598 volume->v_forcegid = NULL; /* set as null so as to return 0 later on */
602 if (options[VOLOPT_PREEXEC].c_value)
603 volume->v_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_PREEXEC].c_value, pwd, path);
604 volume->v_preexec_close = options[VOLOPT_PREEXEC].i_value;
606 if (options[VOLOPT_POSTEXEC].c_value)
607 volume->v_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_POSTEXEC].c_value, pwd, path);
609 if (options[VOLOPT_ROOTPREEXEC].c_value)
610 volume->v_root_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPREEXEC].c_value, pwd, path);
611 volume->v_root_preexec_close = options[VOLOPT_ROOTPREEXEC].i_value;
613 if (options[VOLOPT_ROOTPOSTEXEC].c_value)
614 volume->v_root_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPOSTEXEC].c_value, pwd, path);
618 volume->v_next = Volumes;
623 /* ---------------- */
624 static char *myfgets( buf, size, fp )
633 while ((EOF != ( c = getc( fp )) ) && ( size > 0 )) {
634 if ( c == '\n' || c == '\r' ) {
652 /* check access list. this function wants something of the following
654 * @group,name,name2,@group2,name3
656 * a NULL argument allows everybody to have access.
657 * we return three things:
659 * 0: list exists, but name isn't in it
663 #ifndef NO_REAL_USER_NAME
664 /* authentication is case insensitive
665 * FIXME should we do the same with group name?
667 #define access_strcmp strcasecmp
670 #define access_strcmp strcmp
674 static int accessvol(args, name)
678 char buf[MAXPATHLEN + 1], *p;
684 strncpy(buf, args, sizeof(buf));
685 if ((p = strtok(buf, ",")) == NULL) /* nothing, return okay */
689 if (*p == '@') { /* it's a group */
690 if ((gr = getgrnam(p + 1)) && gmem(gr->gr_gid))
692 } else if (access_strcmp(p, name) == 0) /* it's a user name */
694 p = strtok(NULL, ",");
700 static void setextmap( ext, type, creator, user)
701 char *ext, *type, *creator;
707 if (Extmap == NULL) {
708 if (( Extmap = calloc(1, sizeof( struct extmap ))) == NULL ) {
709 LOG(log_error, logtype_afpd, "setextmap: calloc: %s", strerror(errno) );
714 for ( em = Extmap, cnt = 0; em->em_ext; em++, cnt++) {
715 if ( (strdiacasecmp( em->em_ext, ext )) == 0 ) {
720 if ( em->em_ext == NULL ) {
721 if (!(Extmap = realloc( Extmap, sizeof( struct extmap ) * (cnt +2))) ) {
722 LOG(log_error, logtype_afpd, "setextmap: realloc: %s", strerror(errno) );
725 (Extmap +cnt +1)->em_ext = NULL;
727 } else if ( !user ) {
733 if (!(em->em_ext = strdup( ext))) {
734 LOG(log_error, logtype_afpd, "setextmap: strdup: %s", strerror(errno) );
738 if ( *type == '\0' ) {
739 memcpy(em->em_type, "????", sizeof( em->em_type ));
741 memcpy(em->em_type, type, sizeof( em->em_type ));
743 if ( *creator == '\0' ) {
744 memcpy(em->em_creator, "UNIX", sizeof( em->em_creator ));
746 memcpy(em->em_creator, creator, sizeof( em->em_creator ));
750 /* -------------------------- */
751 static int extmap_cmp(const void *map1, const void *map2)
753 const struct extmap *em1 = map1;
754 const struct extmap *em2 = map2;
755 return strdiacasecmp(em1->em_ext, em2->em_ext);
758 static void sortextmap( void)
763 if ((em = Extmap) == NULL) {
771 qsort(Extmap, Extmap_cnt, sizeof(struct extmap), extmap_cmp);
776 /* ----------------------
778 static void free_extmap( void)
788 /* ----------------------
790 static int volfile_changed(struct afp_volume_name *p)
800 if (!stat( name, &st) && st.st_mtime > p->mtime) {
801 p->mtime = st.st_mtime;
807 /* ----------------------
808 * Read a volume configuration file and add the volumes contained within to
809 * the global volume list. If p2 is non-NULL, the file that is opened is
812 * Lines that begin with # and blank lines are ignored.
813 * Volume lines are of the form:
814 * <unix path> [<volume name>] [allow:<user>,<@group>,...] \
815 * [codepage:<file>] [casefold:<num>]
816 * <extension> TYPE [CREATOR]
818 static int readvolfile(obj, p1, p2, user, pwent)
820 struct afp_volume_name *p1;
823 struct passwd *pwent;
826 char path[ MAXPATHLEN + 1], tmp[ MAXPATHLEN + 1],
827 volname[ AFPVOL_NAMELEN + 1 ], buf[ BUFSIZ ],
828 type[ 5 ], creator[ 5 ];
831 struct vol_option options[VOLOPT_NUM], save_options[VOLOPT_NUM];
839 strcpy( path, p1->name );
846 p1->full_name = strdup(path);
849 if (NULL == ( fp = fopen( path, "r" )) ) {
853 if (fd != -1 && !fstat( fd, &st) ) {
854 p1->mtime = st.st_mtime;
857 memset(save_options, 0, sizeof(save_options));
858 while ( myfgets( buf, sizeof( buf ), fp ) != NULL ) {
859 initline( strlen( buf ), buf );
860 parseline( sizeof( path ) - 1, path );
867 /* change the default options for this file */
868 if (strncmp(path, VOLOPT_DEFAULT, VOLOPT_DEFAULT_LEN) == 0) {
870 for (i = 0; i < VOLOPT_NUM; i++) {
871 if (parseline( sizeof( path ) - VOLOPT_DEFAULT_LEN - 1,
872 path + VOLOPT_DEFAULT_LEN) < 0)
874 volset(save_options, NULL, tmp, sizeof(tmp) - 1,
875 path + VOLOPT_DEFAULT_LEN);
881 if (( p = strchr( path, '/' )) != NULL ) {
889 if ( u == NULL || *u == '\0' || ( pw = getpwnam( u )) == NULL ) {
892 strcpy( tmp, pw->pw_dir );
893 if ( p != NULL && *p != '\0' ) {
897 /* Tag a user's home directory with their umask. Note, this will
898 * be overwritten if the user actually specifies a umask: option
899 * for a '~' volume. */
900 save_options[VOLOPT_UMASK].i_value = obj->options.save_mask;
904 /* send path through variable substitution */
905 if (*path != '~') /* need to copy path to tmp */
908 pwent = getpwnam(obj->username);
909 volxlate(obj, path, sizeof(path) - 1, tmp, pwent, NULL);
911 /* this is sort of braindead. basically, i want to be
912 * able to specify things in any order, but i don't want to
913 * re-write everything.
915 * currently we have options:
923 * options:prodos,crlf,noadouble,ro...
928 * namemask:x,y,!z (not implemented yet)
930 memcpy(options, save_options, sizeof(options));
933 /* read in up to VOLOP_NUM possible options */
934 for (i = 0; i < VOLOPT_NUM; i++) {
935 if (parseline( sizeof( tmp ) - 1, tmp ) < 0)
938 volset(options, save_options, volname, sizeof(volname) - 1, tmp);
941 /* check allow/deny lists:
942 allow -> either no list (-1), or in list (1)
943 deny -> either no list (-1), or not in list (0) */
944 if (accessvol(options[VOLOPT_ALLOW].c_value, obj->username) &&
945 (accessvol(options[VOLOPT_DENY].c_value, obj->username) < 1)) {
947 /* handle read-only behaviour. semantics:
948 * 1) neither the rolist nor the rwlist exist -> rw
949 * 2) rolist exists -> ro if user is in it.
950 * 3) rwlist exists -> ro unless user is in it. */
951 if (((options[VOLOPT_FLAGS].i_value & AFPVOL_RO) == 0) &&
952 ((accessvol(options[VOLOPT_ROLIST].c_value,
953 obj->username) == 1) ||
954 !accessvol(options[VOLOPT_RWLIST].c_value,
956 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
958 /* do variable substitution for volname */
959 volxlate(obj, tmp, sizeof(tmp) - 1, volname, pwent, path);
960 creatvol(obj, pwent, path, tmp, options, p2 != NULL);
962 volfree(options, save_options);
966 parseline( sizeof( type ) - 1, type );
967 parseline( sizeof( creator ) - 1, creator );
968 setextmap( path, type, creator, user);
975 volfree(save_options, NULL);
977 if ( fclose( fp ) != 0 ) {
978 LOG(log_error, logtype_afpd, "readvolfile: fclose: %s", strerror(errno) );
984 /* ------------------------------- */
985 static void volume_free(struct vol *vol)
990 free(vol->v_password);
992 free(vol->v_volcodepage);
993 free(vol->v_maccodepage);
996 free(vol->v_forceuid);
997 free(vol->v_forcegid);
998 #endif /* FORCE_UIDGID */
1001 /* ------------------------------- */
1002 static void free_volumes(void )
1005 struct vol *nvol, *ovol;
1007 for ( vol = Volumes; vol; vol = vol->v_next ) {
1008 if (( vol->v_flags & AFPVOL_OPEN ) ) {
1015 for ( vol = Volumes, ovol = NULL; vol; vol = nvol) {
1018 if (vol->v_name == NULL) {
1019 if (Volumes == vol) {
1026 ovol->v_next = nvol;
1036 /* ------------------------------- */
1037 static void volume_unlink(struct vol *volume)
1039 struct vol *vol, *ovol, *nvol;
1041 if (volume == Volumes) {
1042 Volumes = Volumes->v_next;
1045 for ( vol = Volumes, ovol = NULL; vol; vol = nvol) {
1048 if (vol == volume) {
1053 ovol->v_next = nvol;
1064 static int getvolspace( vol, bfree, btotal, xbfree, xbtotal, bsize )
1066 u_int32_t *bfree, *btotal, *bsize;
1067 VolSpace *xbfree, *xbtotal;
1071 #ifndef NO_QUOTA_SUPPORT
1072 VolSpace qfree, qtotal;
1075 spaceflag = AFPVOL_GVSMASK & vol->v_flags;
1076 /* report up to 2GB if afp version is < 2.2 (4GB if not) */
1077 maxsize = (vol->v_flags & AFPVOL_A2VOL) ? 0x01fffe00 :
1078 (((afp_version < 22) || (vol->v_flags & AFPVOL_LIMITSIZE))
1079 ? 0x7fffffffL : 0xffffffffL);
1082 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_AFSGVS ) {
1083 if ( afs_getvolspace( vol, xbfree, xbtotal, bsize ) == AFP_OK ) {
1084 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_AFSGVS;
1085 goto getvolspace_done;
1090 if (( rc = ustatfs_getvolspace( vol, xbfree, xbtotal,
1091 bsize)) != AFP_OK ) {
1095 #define min(a,b) ((a)<(b)?(a):(b))
1096 #ifndef NO_QUOTA_SUPPORT
1097 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_UQUOTA ) {
1098 if ( uquota_getvolspace( vol, &qfree, &qtotal, *bsize ) == AFP_OK ) {
1099 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_UQUOTA;
1100 *xbfree = min(*xbfree, qfree);
1101 *xbtotal = min( *xbtotal, qtotal);
1102 goto getvolspace_done;
1106 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_USTATFS;
1109 *bfree = min( *xbfree, maxsize);
1110 *btotal = min( *xbtotal, maxsize);
1114 /* -----------------------
1115 * set volume creation date
1116 * avoid duplicate, well at least it tries
1118 static void vol_setdate(u_int16_t id, struct adouble *adp, time_t date)
1121 struct vol *vol = Volumes;
1123 for ( volume = Volumes; volume; volume = volume->v_next ) {
1124 if (volume->v_vid == id) {
1127 else if (AD_DATE_FROM_UNIX(date) == volume->v_ctime) {
1129 volume = Volumes; /* restart */
1132 vol->v_ctime = AD_DATE_FROM_UNIX(date);
1133 ad_setdate(adp, AD_DATE_CREATE | AD_DATE_UNIX, date);
1136 /* ----------------------- */
1137 static int getvolparams( bitmap, vol, st, buf, buflen )
1145 int bit = 0, isad = 1;
1148 u_int32_t bfree, btotal, bsize;
1149 VolSpace xbfree, xbtotal; /* extended bytes */
1150 char *data, *nameoff = NULL;
1153 /* courtesy of jallison@whistle.com:
1154 * For MacOS8.x support we need to create the
1155 * .Parent file here if it doesn't exist. */
1157 ad_init(&ad, vol->v_adouble);
1158 if ( ad_open( vol->v_path, vol_noadouble(vol) |
1159 ADFLAGS_HF|ADFLAGS_DIR, O_RDWR | O_CREAT,
1162 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1164 } else if (ad_get_HF_flags( &ad ) & O_CREAT) {
1165 slash = strrchr( vol->v_path, '/' );
1169 slash = vol->v_path;
1171 ad_setentrylen( &ad, ADEID_NAME, strlen( slash ));
1172 memcpy(ad_entry( &ad, ADEID_NAME ), slash,
1173 ad_getentrylen( &ad, ADEID_NAME ));
1174 vol_setdate(vol->v_vid, &ad, st->st_mtime);
1175 ad_flush(&ad, ADFLAGS_HF);
1178 if (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0)
1179 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1181 vol->v_ctime = aint;
1184 if (( bitmap & ( (1<<VOLPBIT_BFREE)|(1<<VOLPBIT_BTOTAL) |
1185 (1<<VOLPBIT_XBFREE)|(1<<VOLPBIT_XBTOTAL) |
1186 (1<<VOLPBIT_BSIZE)) ) != 0 ) {
1187 if ( getvolspace( vol, &bfree, &btotal, &xbfree, &xbtotal,
1190 ad_close( &ad, ADFLAGS_HF );
1192 return( AFPERR_PARAM );
1197 while ( bitmap != 0 ) {
1198 while (( bitmap & 1 ) == 0 ) {
1206 if (0 == (vol->v_flags & AFPVOL_NOFILEID) && vol->v_cdb != NULL &&
1207 (vol->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1208 ashort = VOLPBIT_ATTR_FILEID;
1210 /* check for read-only.
1211 * NOTE: we don't actually set the read-only flag unless
1212 * it's passed in that way as it's possible to mount
1213 * a read-write filesystem under a read-only one. */
1214 if ((vol->v_flags & AFPVOL_RO) ||
1215 ((utime(vol->v_path, NULL) < 0) && (errno == EROFS))) {
1216 ashort |= VOLPBIT_ATTR_RO;
1218 ashort |= VOLPBIT_ATTR_CATSEARCH;
1219 if (afp_version >= 30) {
1220 ashort |= VOLPBIT_ATTR_UTF8;
1221 if (vol->v_flags & AFPVOL_UNIX_PRIV)
1222 ashort |= VOLPBIT_ATTR_UNIXPRIV;
1224 ashort = htons(ashort);
1225 memcpy(data, &ashort, sizeof( ashort ));
1226 data += sizeof( ashort );
1230 ashort = htons( AFPVOLSIG_DEFAULT );
1231 memcpy(data, &ashort, sizeof( ashort ));
1232 data += sizeof( ashort );
1235 case VOLPBIT_CDATE :
1236 aint = vol->v_ctime;
1237 memcpy(data, &aint, sizeof( aint ));
1238 data += sizeof( aint );
1241 case VOLPBIT_MDATE :
1242 if ( st->st_mtime > vol->v_mtime ) {
1243 vol->v_mtime = st->st_mtime;
1245 aint = AD_DATE_FROM_UNIX(vol->v_mtime);
1246 memcpy(data, &aint, sizeof( aint ));
1247 data += sizeof( aint );
1250 case VOLPBIT_BDATE :
1251 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1252 aint = AD_DATE_START;
1253 memcpy(data, &aint, sizeof( aint ));
1254 data += sizeof( aint );
1258 memcpy(data, &vol->v_vid, sizeof( vol->v_vid ));
1259 data += sizeof( vol->v_vid );
1262 case VOLPBIT_BFREE :
1263 bfree = htonl( bfree );
1264 memcpy(data, &bfree, sizeof( bfree ));
1265 data += sizeof( bfree );
1268 case VOLPBIT_BTOTAL :
1269 btotal = htonl( btotal );
1270 memcpy(data, &btotal, sizeof( btotal ));
1271 data += sizeof( btotal );
1274 #ifndef NO_LARGE_VOL_SUPPORT
1275 case VOLPBIT_XBFREE :
1276 xbfree = hton64( xbfree );
1277 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1278 bcopy(&xbfree, data, sizeof(xbfree));
1279 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1280 memcpy(data, &xbfree, sizeof( xbfree ));
1281 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1282 data += sizeof( xbfree );
1285 case VOLPBIT_XBTOTAL :
1286 xbtotal = hton64( xbtotal );
1287 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1288 bcopy(&xbtotal, data, sizeof(xbtotal));
1289 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1290 memcpy(data, &xbtotal, sizeof( xbtotal ));
1291 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1292 data += sizeof( xbfree );
1294 #endif /* ! NO_LARGE_VOL_SUPPORT */
1298 data += sizeof( u_int16_t );
1301 case VOLPBIT_BSIZE: /* block size */
1302 bsize = htonl(bsize);
1303 memcpy(data, &bsize, sizeof(bsize));
1304 data += sizeof(bsize);
1309 ad_close( &ad, ADFLAGS_HF );
1311 return( AFPERR_BITMAP );
1317 ashort = htons( data - buf );
1318 memcpy(nameoff, &ashort, sizeof( ashort ));
1319 aint = strlen( vol->v_name );
1321 memcpy(data, vol->v_name, aint );
1325 ad_close( &ad, ADFLAGS_HF );
1327 *buflen = data - buf;
1331 /* ------------------------- */
1332 int static stat_vol(u_int16_t bitmap, struct vol *vol, char *rbuf, int *rbuflen)
1337 if ( stat( vol->v_path, &st ) < 0 ) {
1339 return( AFPERR_PARAM );
1342 buflen = *rbuflen - sizeof( bitmap );
1343 if (( ret = getvolparams( bitmap, vol, &st,
1344 rbuf + sizeof( bitmap ), &buflen )) != AFP_OK ) {
1348 *rbuflen = buflen + sizeof( bitmap );
1349 bitmap = htons( bitmap );
1350 memcpy(rbuf, &bitmap, sizeof( bitmap ));
1355 /* ------------------------------- */
1356 void load_volumes(AFPObj *obj)
1358 struct passwd *pwent;
1363 /* check files date */
1364 if (obj->options.defaultvol.loaded) {
1365 changed = volfile_changed(&obj->options.defaultvol);
1367 if (obj->options.systemvol.loaded) {
1368 changed |= volfile_changed(&obj->options.systemvol);
1370 if (obj->options.uservol.loaded) {
1371 changed |= volfile_changed(&obj->options.uservol);
1380 pwent = getpwnam(obj->username);
1381 if ( (obj->options.flags & OPTION_USERVOLFIRST) == 0 ) {
1382 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent);
1385 if ((*obj->username == '\0') || (obj->options.flags & OPTION_NOUSERVOL)) {
1386 readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent);
1389 * Read user's AppleVolumes or .AppleVolumes file
1390 * If neither are readable, read the default volumes file. if
1391 * that doesn't work, create a user share.
1393 obj->options.uservol.name = strdup(pwent->pw_dir);
1394 if ( readvolfile(obj, &obj->options.uservol, "AppleVolumes", 1, pwent) < 0 &&
1395 readvolfile(obj, &obj->options.uservol, ".AppleVolumes", 1, pwent) < 0 &&
1396 readvolfile(obj, &obj->options.uservol, "applevolumes", 1, pwent) < 0 &&
1397 readvolfile(obj, &obj->options.uservol, ".applevolumes", 1, pwent) < 0 &&
1398 obj->options.defaultvol.name != NULL ) {
1399 if (readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent) < 0)
1400 creatvol(obj, pwent, pwent->pw_dir, NULL, NULL, 1);
1403 if ( obj->options.flags & OPTION_USERVOLFIRST ) {
1404 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent );
1408 /* ------------------------------- */
1409 int afp_getsrvrparms(obj, ibuf, ibuflen, rbuf, rbuflen )
1412 int ibuflen, *rbuflen;
1423 for ( vcnt = 0, volume = Volumes; volume; volume = volume->v_next ) {
1424 if (!(volume->v_flags & AFPVOL_NOSTAT)) {
1425 if ( stat( volume->v_path, &st ) < 0 ) {
1426 LOG(log_info, logtype_afpd, "afp_getsrvrparms: stat %s: %s",
1427 volume->v_path, strerror(errno) );
1428 continue; /* can't access directory */
1430 if (!S_ISDIR(st.st_mode)) {
1431 continue; /* not a dir */
1434 if (volume->v_hide) {
1435 continue; /* config file changed but the volume was mounted */
1437 /* set password bit if there's a volume password */
1438 *data = (volume->v_password) ? AFPSRVR_PASSWD : 0;
1440 /* Apple 2 clients running ProDOS-8 expect one volume to have
1441 bit 0 of this byte set. They will not recognize anything
1442 on the server unless this is the case. I have not
1443 completely worked this out, but it's related to booting
1444 from the server. Support for that function is a ways
1445 off.. <shirsch@ibm.net> */
1446 *data |= (volume->v_flags & AFPVOL_A2VOL) ? AFPSRVR_CONFIGINFO : 0;
1447 *data++ |= 0; /* UNIX PRIVS BIT ..., OSX doesn't seem to use it, so we don't either */
1448 len = strlen( volume->v_name );
1450 memcpy(data, volume->v_name, len );
1455 *rbuflen = data - rbuf;
1457 if ( gettimeofday( &tv, 0 ) < 0 ) {
1458 LOG(log_error, logtype_afpd, "afp_getsrvrparms: gettimeofday: %s", strerror(errno) );
1460 return AFPERR_PARAM;
1462 tv.tv_sec = AD_DATE_FROM_UNIX(tv.tv_sec);
1463 memcpy(data, &tv.tv_sec, sizeof( u_int32_t));
1464 data += sizeof( u_int32_t);
1469 /* -------------------------
1470 * we are the user here
1472 int afp_openvol(obj, ibuf, ibuflen, rbuf, rbuflen )
1475 int ibuflen, *rbuflen;
1486 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1487 bitmap = ntohs( bitmap );
1488 ibuf += sizeof( bitmap );
1489 if (( bitmap & (1<<VOLPBIT_VID)) == 0 ) {
1491 return AFPERR_BITMAP;
1494 len = (unsigned char)*ibuf++;
1495 volname = obj->oldtmp;
1496 memcpy(volname, ibuf, len );
1497 *(volname + len) = '\0';
1499 if ((len + 1) & 1) /* pad to an even boundary */
1504 for ( volume = Volumes; volume; volume = volume->v_next ) {
1505 if ( strcasecmp( volname, volume->v_name ) == 0 ) {
1510 if ( volume == NULL ) {
1512 return AFPERR_PARAM;
1515 /* check for a volume password */
1516 if (volume->v_password && strncmp(ibuf, volume->v_password, VOLPASSLEN)) {
1518 return AFPERR_ACCESS;
1521 if (( volume->v_flags & AFPVOL_OPEN ) ) {
1522 /* the volume is already open */
1524 set_uidgid ( volume );
1526 return stat_vol(bitmap, volume, rbuf, rbuflen);
1529 /* initialize volume variables
1532 if (afp_version >= 30) {
1533 volume->max_filename = 255;
1536 volume->max_filename = MACFILELEN;
1539 volume->v_dir = volume->v_root = NULL;
1541 /* FIXME unix name != mac name */
1542 if ((dir = dirnew(volume->v_name, volume->v_name) ) == NULL) {
1543 LOG(log_error, logtype_afpd, "afp_openvol: malloc: %s", strerror(errno) );
1548 dir->d_did = DIRDID_ROOT;
1549 dir->d_color = DIRTREE_COLOR_BLACK; /* root node is black */
1550 volume->v_dir = volume->v_root = dir;
1551 volume->v_flags |= AFPVOL_OPEN;
1552 volume->v_cdb = NULL;
1554 if (volume->v_root_preexec) {
1555 if ((ret = afprun(1, volume->v_root_preexec, NULL)) && volume->v_root_preexec_close) {
1556 LOG(log_error, logtype_afpd, "afp_openvol: root preexec : %d", ret );
1563 set_uidgid ( volume );
1566 if (volume->v_preexec) {
1567 if ((ret = afprun(0, volume->v_preexec, NULL)) && volume->v_preexec_close) {
1568 LOG(log_error, logtype_afpd, "afp_openvol: preexec : %d", ret );
1574 if ( stat( volume->v_path, &st ) < 0 ) {
1579 if ( chdir( volume->v_path ) < 0 ) {
1583 curdir = volume->v_dir;
1584 if (volume->v_cnidscheme == NULL) {
1585 volume->v_cnidscheme = strdup(DEFAULT_CNID_SCHEME);
1586 LOG(log_warning, logtype_afpd, "Warning: No CNID scheme for volume %s. Using default.",
1589 if (volume->v_dbpath)
1590 volume->v_cdb = cnid_open (volume->v_dbpath, volume->v_umask, volume->v_cnidscheme);
1591 if (volume->v_cdb == NULL)
1592 volume->v_cdb = cnid_open (volume->v_path, volume->v_umask, volume->v_cnidscheme);
1593 if (volume->v_cdb == NULL) {
1594 volume->v_cdb = cnid_open (volume->v_path, volume->v_umask, DEFAULT_CNID_SCHEME);
1595 LOG(log_error, logtype_afpd, "Error: invalid CNID backend for %s: %s", volume->v_path,
1596 volume->v_cnidscheme);
1598 if (volume->v_cdb == NULL) {
1599 LOG(log_error, logtype_afpd, "Fatal error: cannot open CNID for %s", volume->v_path);
1606 if (!volume->v_volcodepage)
1607 volume->v_volcodepage = strdup("UTF8");
1609 if ( (charset_t) -1 == ( volume->v_volcharset = add_charset(volume->v_volcodepage)) ) {
1610 LOG (log_error, logtype_afpd, "Setting codepage %s as volume codepage failed", volume->v_volcodepage);
1615 if ( NULL == ( volume->v_vol = find_charset_functions(volume->v_volcodepage)) || volume->v_vol->flags & CHARSET_ICONV ) {
1616 LOG (log_warning, logtype_afpd, "WARNING: volume encoding %s is *not* supported by netatalk, expect problems !!!!", volume->v_volcodepage);
1619 if (!volume->v_maccodepage)
1620 volume->v_maccodepage = strdup(obj->options.maccodepage);
1622 if ( (charset_t) -1 == ( volume->v_maccharset = add_charset(volume->v_maccodepage)) ) {
1623 LOG (log_error, logtype_afpd, "Setting codepage %s as mac codepage failed", volume->v_maccodepage);
1628 if ( NULL == ( volume->v_mac = find_charset_functions(volume->v_maccodepage)) || ! (volume->v_mac->flags & CHARSET_CLIENT) ) {
1629 LOG (log_error, logtype_afpd, "Fatal error: mac charset %s not supported", volume->v_maccodepage);
1634 ret = stat_vol(bitmap, volume, rbuf, rbuflen);
1635 if (ret == AFP_OK) {
1637 handle_special_folders( volume );
1640 * If you mount a volume twice, the second time the trash appears on
1641 * the desk-top. That's because the Mac remembers the DID for the
1642 * trash (even for volumes in different zones, on different servers).
1643 * Just so this works better, we prime the DID cache with the trash,
1644 * fixing the trash at DID 17.
1645 * FIXME (RL): should it be done inside a CNID backend ? (always returning Trash DID when asked) ?
1647 if ((volume->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1649 /* FIXME find db time stamp */
1650 cnid_getstamp(volume->v_cdb, volume->v_stamp, sizeof(volume->v_stamp));
1652 cname( volume, volume->v_dir, &p );
1658 if (volume->v_dir) {
1659 dirfree( volume->v_dir );
1660 volume->v_dir = volume->v_root = NULL;
1663 volume->v_flags &= ~AFPVOL_OPEN;
1664 if (volume->v_cdb != NULL) {
1665 cnid_close(volume->v_cdb);
1666 volume->v_cdb = NULL;
1672 /* ------------------------- */
1673 static void closevol(struct vol *vol)
1678 dirfree( vol->v_root );
1680 if (vol->v_cdb != NULL) {
1681 cnid_close(vol->v_cdb);
1685 if (vol->v_postexec) {
1686 afprun(0, vol->v_postexec, NULL);
1688 if (vol->v_root_postexec) {
1689 afprun(1, vol->v_root_postexec, NULL);
1693 /* ------------------------- */
1694 void close_all_vol(void)
1698 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
1699 if ( ovol->v_flags & AFPVOL_OPEN ) {
1700 ovol->v_flags &= ~AFPVOL_OPEN;
1706 /* ------------------------- */
1707 int afp_closevol(obj, ibuf, ibuflen, rbuf, rbuflen )
1710 int ibuflen, *rbuflen;
1712 struct vol *vol, *ovol;
1717 memcpy(&vid, ibuf, sizeof( vid ));
1718 if (NULL == ( vol = getvolbyvid( vid )) ) {
1719 return( AFPERR_PARAM );
1722 vol->v_flags &= ~AFPVOL_OPEN;
1723 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
1724 if ( ovol->v_flags & AFPVOL_OPEN ) {
1728 if ( ovol != NULL ) {
1729 /* Even if chdir fails, we can't say afp_closevol fails. */
1730 if ( chdir( ovol->v_path ) == 0 ) {
1731 curdir = ovol->v_dir;
1736 if (vol->v_deleted) {
1737 showvol(vol->v_name);
1744 /* ------------------------- */
1745 struct vol *getvolbyvid(const u_int16_t vid )
1749 for ( vol = Volumes; vol; vol = vol->v_next ) {
1750 if ( vid == vol->v_vid ) {
1754 if ( vol == NULL || ( vol->v_flags & AFPVOL_OPEN ) == 0 ) {
1760 #endif /* FORCE_UIDGID */
1765 /* ------------------------ */
1766 static int ext_cmp_key(const void *key, const void *obj)
1768 const char *p = key;
1769 const struct extmap *em = obj;
1770 return strdiacasecmp(p, em->em_ext);
1772 struct extmap *getextmap(const char *path)
1777 if (NULL == ( p = strrchr( path, '.' )) ) {
1778 return( Defextmap );
1781 if (!*p || !Extmap_cnt) {
1782 return( Defextmap );
1784 em = bsearch(p, Extmap, Extmap_cnt, sizeof(struct extmap), ext_cmp_key);
1788 return( Defextmap );
1792 /* ------------------------- */
1793 struct extmap *getdefextmap(void)
1795 return( Defextmap );
1798 /* --------------------------
1799 poll if a volume is changed by other processes.
1801 int pollvoltime(obj)
1808 if (!(afp_version > 21 && obj->options.server_notif))
1811 if ( gettimeofday( &tv, 0 ) < 0 )
1814 for ( vol = Volumes; vol; vol = vol->v_next ) {
1815 if ( (vol->v_flags & AFPVOL_OPEN) && vol->v_mtime + 30 < tv.tv_sec) {
1816 if ( !stat( vol->v_path, &st ) && vol->v_mtime != st.st_mtime ) {
1817 vol->v_mtime = st.st_mtime;
1818 if (!obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED))
1827 /* ------------------------- */
1828 void setvoltime(obj, vol )
1834 /* just looking at vol->v_mtime is broken seriously since updates
1835 * from other users afpd processes never are seen.
1836 * This is not the most elegant solution (a shared memory between
1837 * the afpd processes would come closer)
1840 if ( gettimeofday( &tv, 0 ) < 0 ) {
1841 LOG(log_error, logtype_afpd, "setvoltime: gettimeofday: %s", strerror(errno) );
1844 if( utime( vol->v_path, NULL ) < 0 ) {
1845 /* write of time failed ... probably a read only filesys,
1846 * where no other users can interfere, so there's no issue here
1850 /* a little granularity */
1851 if (vol->v_mtime < tv.tv_sec) {
1852 vol->v_mtime = tv.tv_sec;
1853 if (afp_version > 21 && obj->options.server_notif) {
1854 obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED);
1859 /* ------------------------- */
1860 int afp_getvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1863 int ibuflen, *rbuflen;
1866 u_int16_t vid, bitmap;
1869 memcpy(&vid, ibuf, sizeof( vid ));
1870 ibuf += sizeof( vid );
1871 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1872 bitmap = ntohs( bitmap );
1874 if (NULL == ( vol = getvolbyvid( vid )) ) {
1876 return( AFPERR_PARAM );
1879 return stat_vol(bitmap, vol, rbuf, rbuflen);
1882 /* ------------------------- */
1883 int afp_setvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1886 int ibuflen, *rbuflen;
1890 u_int16_t vid, bitmap;
1896 memcpy(&vid, ibuf, sizeof( vid ));
1897 ibuf += sizeof( vid );
1898 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1899 bitmap = ntohs( bitmap );
1900 ibuf += sizeof(bitmap);
1902 if (( vol = getvolbyvid( vid )) == NULL ) {
1903 return( AFPERR_PARAM );
1906 if (vol->v_flags & AFPVOL_RO)
1907 return AFPERR_VLOCK;
1909 /* we can only set the backup date. */
1910 if (bitmap != (1 << VOLPBIT_BDATE))
1911 return AFPERR_BITMAP;
1913 ad_init(&ad, vol->v_adouble);
1914 if ( ad_open( vol->v_path, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR,
1917 return AFPERR_VLOCK;
1919 return AFPERR_ACCESS;
1922 memcpy(&aint, ibuf, sizeof(aint));
1923 ad_setdate(&ad, AD_DATE_BACKUP, aint);
1924 ad_flush(&ad, ADFLAGS_HF);
1925 ad_close(&ad, ADFLAGS_HF);
1929 /* ------------------------- */
1930 int wincheck(const struct vol *vol, const char *path)
1934 if (!(vol->v_flags & AFPVOL_MSWINDOWS))
1937 /* empty paths are not allowed */
1938 if ((len = strlen(path)) == 0)
1941 /* leading or trailing whitespaces are not allowed, carriage returns
1942 * and probably other whitespace is okay, tabs are not allowed
1944 if ((path[0] == ' ') || (path[len-1] == ' '))
1947 /* certain characters are not allowed */
1948 if (strpbrk(path, MSWINDOWS_BADCHARS))
1951 /* everything else is okay */
1957 * precreate a folder
1958 * this is only intended for folders in the volume root
1959 * It will *not* work if the folder name contains extended characters
1961 static int create_special_folder (const struct vol *vol, const struct _special_folder *folder)
1970 p = (char *) malloc ( strlen(vol->v_path)+strlen(folder->name)+2);
1972 LOG(log_error, logtype_afpd,"malloc failed");
1975 strcpy(p, vol->v_path);
1977 strcat(p, folder->name);
1979 if ( (ret = stat( p, &st )) < 0 ) {
1980 if (folder->precreate) {
1981 if (ad_mkdir(p, folder->mode & ~vol->v_umask)) {
1982 LOG(log_debug, logtype_afpd,"Creating '%s' failed", folder->name);
1990 if ( !ret && folder->hide) {
1992 ad_init(&ad, vol->v_adouble);
1993 if (ad_open( p, vol_noadouble(vol) | ADFLAGS_HF|ADFLAGS_DIR,
1994 O_RDWR|O_CREAT, 0666, &ad) < 0) {
1998 if ((ad_get_HF_flags( &ad ) & O_CREAT) ) {
1999 ad_setentrylen( &ad, ADEID_NAME, strlen(folder->name));
2000 memcpy(ad_entry( &ad, ADEID_NAME ), folder->name,
2001 ad_getentrylen( &ad, ADEID_NAME ));
2004 ad_getattr(&ad, &attr);
2005 attr |= htons( ntohs( attr ) | ATTRBIT_INVISIBLE );
2006 ad_setattr(&ad, attr);
2008 ad_flush( &ad, ADFLAGS_HF );
2009 ad_close( &ad, ADFLAGS_HF );
2015 static void handle_special_folders (const struct vol * vol)
2017 const _special_folder *p = &special_folders[0];
2019 if (vol->v_flags & AFPVOL_RO)
2022 for (; p->name != NULL; p++) {
2023 create_special_folder (vol, p);