2 * $Id: volume.c,v 1.51.2.7.2.6 2003-09-30 12:24:49 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 typedef struct _special_folder {
147 static const _special_folder special_folders[] = {
148 {"Network Trash Folder", 1, 0777, 1},
149 {"Temporary Items", 1, 0777, 1},
151 {"TheFindByContentFolder", 0, 0, 1},
152 {"TheVolumeSettingsFolder", 0, 0, 1},
156 static void handle_special_folders (const struct vol *);
158 static __inline__ void volfree(struct vol_option *options,
159 const struct vol_option *save)
164 for (i = 0; i < VOLOPT_MAX; i++) {
165 if (options[i].c_value && (options[i].c_value != save[i].c_value))
166 free(options[i].c_value);
169 for (i = 0; i < VOLOPT_MAX; i++) {
170 if (options[i].c_value)
171 free(options[i].c_value);
177 /* handle variable substitutions. here's what we understand:
178 * $b -> basename of path
179 * $c -> client ip/appletalk address
180 * $d -> volume pathname on server
181 * $f -> full name (whatever's in the gecos field)
184 * $s -> server name (hostname if it doesn't exist)
185 * $u -> username (guest is usually nobody)
186 * $v -> volume name (ADEID_NAME or basename if ADEID_NAME is empty)
187 * $z -> zone (may not exist)
190 #define is_var(a, b) (strncmp((a), (b), 2) == 0)
192 static char *volxlate(AFPObj *obj, char *dest, size_t destlen,
193 char *src, struct passwd *pwd, char *path)
203 dest = calloc(destlen +1, 1);
209 strncpy(dest, src, destlen);
210 if ((p = strchr(src, '$')) == NULL) /* nothing to do */
213 /* first part of the path. just forward to the next variable. */
214 len = MIN(p - src, destlen);
220 while (p && destlen > 0) {
221 /* now figure out what the variable is */
223 if (is_var(p, "$b")) {
225 if ((q = strrchr(path, '/')) == NULL)
227 else if (*(q + 1) != '\0')
230 } else if (is_var(p, "$c")) {
231 if (obj->proto == AFPPROTO_ASP) {
232 ASP asp = obj->handle;
234 len = sprintf(dest, "%u.%u", ntohs(asp->asp_sat.sat_addr.s_net),
235 asp->asp_sat.sat_addr.s_node);
239 } else if (obj->proto == AFPPROTO_DSI) {
240 DSI *dsi = obj->handle;
242 len = sprintf(dest, "%s:%u", inet_ntoa(dsi->client.sin_addr),
243 ntohs(dsi->client.sin_port));
247 } else if (is_var(p, "$d")) {
249 } else if (is_var(p, "$f")) {
250 if ((q = strchr(pwd->pw_gecos, ',')))
253 } else if (is_var(p, "$g")) {
254 struct group *grp = getgrgid(pwd->pw_gid);
257 } else if (is_var(p, "$h")) {
258 q = obj->options.hostname;
259 } else if (is_var(p, "$s")) {
262 else if (obj->options.server) {
263 q = obj->options.server;
265 q = obj->options.hostname;
266 } else if (is_var(p, "$u")) {
268 } else if (is_var(p, "$v")) {
273 if (ad_open(path, ADFLAGS_HF, O_RDONLY, 0, &ad) < 0)
276 if ((len = MIN(ad_getentrylen(&ad, ADEID_NAME), destlen)) > 0) {
277 memcpy(dest, ad_entry(&ad, ADEID_NAME), len);
278 ad_close(&ad, ADFLAGS_HF);
282 ad_close(&ad, ADFLAGS_HF);
283 no_volname: /* simple basename */
284 if ((q = strrchr(path, '/')) == NULL)
286 else if (*(q + 1) != '\0')
290 } else if (is_var(p, "$z")) {
292 } else if (is_var(p, "$$")) {
297 /* copy the stuff over. if we don't understand something that we
298 * should, just skip it over. */
300 len = MIN(p == q ? 2 : strlen(q), destlen);
301 strncpy(dest, q, len);
306 /* stuff up to next $ */
308 p = strchr(src, '$');
309 len = p ? MIN(p - src, destlen) : destlen;
311 strncpy(dest, src, len);
319 /* to make sure that val is valid, make sure to select an opt that
321 static int optionok(const char *buf, const char *opt, const char *val)
323 if (!strstr(buf,opt))
331 /* -------------------- */
332 static void setoption(struct vol_option *options, struct vol_option *save, int opt, const char *val)
334 if (options[opt].c_value && (!save || options[opt].c_value != save[opt].c_value))
335 free(options[opt].c_value);
336 options[opt].c_value = strdup(val + 1);
339 /* ------------------------------------------
340 handle all the options. tmp can't be NULL. */
341 static void volset(struct vol_option *options, struct vol_option *save,
342 char *volname, int vlen,
347 val = strchr(tmp, ':');
349 /* we'll assume it's a volume name. */
350 strncpy(volname, tmp, vlen);
355 LOG(log_debug, logtype_afpd, "Parsing volset %s", val);
357 if (optionok(tmp, "allow:", val)) {
358 setoption(options, save, VOLOPT_ALLOW, val);
360 } else if (optionok(tmp, "deny:", val)) {
361 setoption(options, save, VOLOPT_DENY, val);
363 } else if (optionok(tmp, "rwlist:", val)) {
364 setoption(options, save, VOLOPT_RWLIST, val);
366 } else if (optionok(tmp, "rolist:", val)) {
367 setoption(options, save, VOLOPT_ROLIST, val);
369 } else if (optionok(tmp, "codepage:", val)) {
370 LOG (log_error, logtype_afpd, "The old codepage system has been removed. Please make sure to read the documentation !!!!");
371 /* Make sure we don't screw anything */
373 } else if (optionok(tmp, "volcharset:", val)) {
374 setoption(options, save, VOLOPT_ENCODING, val);
375 } else if (optionok(tmp, "maccharset:", val)) {
376 setoption(options, save, VOLOPT_MACCHARSET, val);
377 } else if (optionok(tmp, "veto:", val)) {
378 setoption(options, save, VOLOPT_VETO, val);
379 } else if (optionok(tmp, "cnidscheme:", val)) {
380 setoption(options, save, VOLOPT_CNIDSCHEME, val);
381 } else if (optionok(tmp, "casefold:", val)) {
382 if (strcasecmp(val + 1, "tolower") == 0)
383 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMLOWER;
384 else if (strcasecmp(val + 1, "toupper") == 0)
385 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMUPPER;
386 else if (strcasecmp(val + 1, "xlatelower") == 0)
387 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UUPPERMLOWER;
388 else if (strcasecmp(val + 1, "xlateupper") == 0)
389 options[VOLOPT_CASEFOLD].i_value = AFPVOL_ULOWERMUPPER;
390 } else if (optionok(tmp, "adouble:", val)) {
391 if (strcasecmp(val + 1, "v1") == 0)
392 options[VOLOPT_ADOUBLE].i_value = AD_VERSION1;
393 #if AD_VERSION == AD_VERSION2
394 else if (strcasecmp(val + 1, "v2") == 0)
395 options[VOLOPT_ADOUBLE].i_value = AD_VERSION2;
397 } else if (optionok(tmp, "options:", val)) {
400 if ((p = strtok(val + 1, ",")) == NULL) /* nothing */
404 if (strcasecmp(p, "prodos") == 0)
405 options[VOLOPT_FLAGS].i_value |= AFPVOL_A2VOL;
406 else if (strcasecmp(p, "mswindows") == 0) {
407 options[VOLOPT_FLAGS].i_value |= AFPVOL_MSWINDOWS;
408 } else if (strcasecmp(p, "crlf") == 0)
409 options[VOLOPT_FLAGS].i_value |= AFPVOL_CRLF;
410 else if (strcasecmp(p, "noadouble") == 0)
411 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOADOUBLE;
412 else if (strcasecmp(p, "ro") == 0)
413 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
414 else if (strcasecmp(p, "nohex") == 0)
415 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOHEX;
416 else if (strcasecmp(p, "usedots") == 0)
417 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS;
418 else if (strcasecmp(p, "limitsize") == 0)
419 options[VOLOPT_FLAGS].i_value |= AFPVOL_LIMITSIZE;
420 /* support for either "dropbox" or "dropkludge" */
421 else if (strcasecmp(p, "dropbox") == 0)
422 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
423 else if (strcasecmp(p, "dropkludge") == 0)
424 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
425 else if (strcasecmp(p, "nofileid") == 0)
426 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOFILEID;
427 else if (strcasecmp(p, "nostat") == 0)
428 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOSTAT;
429 else if (strcasecmp(p, "preexec_close") == 0)
430 options[VOLOPT_PREEXEC].i_value = 1;
431 else if (strcasecmp(p, "root_preexec_close") == 0)
432 options[VOLOPT_ROOTPREEXEC].i_value = 1;
433 else if (strcasecmp(p, "upriv") == 0)
434 options[VOLOPT_FLAGS].i_value |= AFPVOL_UNIX_PRIV;
436 p = strtok(NULL, ",");
439 } else if (optionok(tmp, "dbpath:", val)) {
440 setoption(options, save, VOLOPT_DBPATH, val);
442 } else if (optionok(tmp, "umask:", val)) {
443 options[VOLOPT_UMASK].i_value = (int)strtol(val, (char **)NULL, 8);
444 } else if (optionok(tmp, "mapchars:",val)) {
445 setoption(options, save, VOLOPT_MAPCHARS, val);
447 } else if (optionok(tmp, "password:", val)) {
448 setoption(options, save, VOLOPT_PASSWORD, val);
452 /* this code allows forced uid/gid per volume settings */
453 } else if (optionok(tmp, "forceuid:", val)) {
454 setoption(options, save, VOLOPT_FORCEUID, val);
455 } else if (optionok(tmp, "forcegid:", val)) {
456 setoption(options, save, VOLOPT_FORCEGID, val);
458 #endif /* FORCE_UIDGID */
459 } else if (optionok(tmp, "root_preexec:", val)) {
460 setoption(options, save, VOLOPT_ROOTPREEXEC, val);
462 } else if (optionok(tmp, "preexec:", val)) {
463 setoption(options, save, VOLOPT_PREEXEC, val);
465 } else if (optionok(tmp, "root_postexec:", val)) {
466 setoption(options, save, VOLOPT_ROOTPOSTEXEC, val);
468 } else if (optionok(tmp, "postexec:", val)) {
469 setoption(options, save, VOLOPT_POSTEXEC, val);
472 /* ignore unknown options */
473 LOG(log_debug, logtype_afpd, "ignoring unknown volume option: %s", tmp);
478 /* ----------------- */
479 static void showvol(const char *name)
482 for ( volume = Volumes; volume; volume = volume->v_next ) {
483 if ( !strcasecmp( volume->v_name, name ) && volume->v_hide) {
490 /* ------------------------------- */
491 static int creatvol(AFPObj *obj, struct passwd *pwd,
492 char *path, char *name,
493 struct vol_option *options,
494 const int user /* user defined volume */
501 if ( name == NULL || *name == '\0' ) {
502 if ((name = strrchr( path, '/' )) == NULL) {
503 return -1; /* Obviously not a fully qualified path */
506 /* if you wish to share /, you need to specify a name. */
511 for ( volume = Volumes; volume; volume = volume->v_next ) {
512 if ( strcasecmp( volume->v_name, name ) == 0 ) {
513 if (volume->v_deleted) {
517 return -1; /* Won't be able to access it, anyway... */
522 vlen = strlen( name );
523 if ( vlen > AFPVOL_NAMELEN ) {
524 vlen = AFPVOL_NAMELEN;
525 name[AFPVOL_NAMELEN] = '\0';
528 if (!( volume = (struct vol *)calloc(1, sizeof( struct vol ))) ) {
529 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
532 if (! ( volume->v_name = (char *)malloc( vlen + 1 ) ) ) {
533 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
537 if (!( volume->v_path = (char *)malloc( strlen( path ) + 1 )) ) {
538 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
539 free(volume->v_name);
543 volume->v_hide = hide;
544 strcpy( volume->v_name, name);
545 strcpy( volume->v_path, path );
549 #endif /* __svr4__ */
550 /* os X start at 1 and use network order ie. 1 2 3 */
551 volume->v_vid = ++lastvid;
552 volume->v_vid = htons(volume->v_vid);
556 /* should we casefold? */
557 volume->v_casefold = options[VOLOPT_CASEFOLD].i_value;
559 /* shift in some flags */
560 volume->v_flags = options[VOLOPT_FLAGS].i_value;
562 if (options[VOLOPT_PASSWORD].c_value)
563 volume->v_password = strdup(options[VOLOPT_PASSWORD].c_value);
565 if (options[VOLOPT_VETO].c_value)
566 volume->v_veto = strdup(options[VOLOPT_VETO].c_value);
568 if (options[VOLOPT_ENCODING].c_value)
569 volume->v_volcodepage = strdup(options[VOLOPT_ENCODING].c_value);
571 if (options[VOLOPT_MACCHARSET].c_value)
572 volume->v_maccodepage = strdup(options[VOLOPT_MACCHARSET].c_value);
574 if (options[VOLOPT_DBPATH].c_value)
575 volume->v_dbpath = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_DBPATH].c_value, pwd, path);
577 if (options[VOLOPT_CNIDSCHEME].c_value)
578 volume->v_cnidscheme = strdup(options[VOLOPT_CNIDSCHEME].c_value);
580 if (options[VOLOPT_UMASK].i_value)
581 volume->v_umask = (mode_t)options[VOLOPT_UMASK].i_value;
583 if (options[VOLOPT_ADOUBLE].i_value)
584 volume->v_adouble = options[VOLOPT_ADOUBLE].i_value;
586 volume->v_adouble = AD_VERSION;
588 if (options[VOLOPT_FORCEUID].c_value) {
589 volume->v_forceuid = strdup(options[VOLOPT_FORCEUID].c_value);
591 volume->v_forceuid = NULL; /* set as null so as to return 0 later on */
594 if (options[VOLOPT_FORCEGID].c_value) {
595 volume->v_forcegid = strdup(options[VOLOPT_FORCEGID].c_value);
597 volume->v_forcegid = NULL; /* set as null so as to return 0 later on */
601 if (options[VOLOPT_PREEXEC].c_value)
602 volume->v_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_PREEXEC].c_value, pwd, path);
603 volume->v_preexec_close = options[VOLOPT_PREEXEC].i_value;
605 if (options[VOLOPT_POSTEXEC].c_value)
606 volume->v_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_POSTEXEC].c_value, pwd, path);
608 if (options[VOLOPT_ROOTPREEXEC].c_value)
609 volume->v_root_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPREEXEC].c_value, pwd, path);
610 volume->v_root_preexec_close = options[VOLOPT_ROOTPREEXEC].i_value;
612 if (options[VOLOPT_ROOTPOSTEXEC].c_value)
613 volume->v_root_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPOSTEXEC].c_value, pwd, path);
617 volume->v_next = Volumes;
622 /* ---------------- */
623 static char *myfgets( buf, size, fp )
632 while ((EOF != ( c = getc( fp )) ) && ( size > 0 )) {
633 if ( c == '\n' || c == '\r' ) {
651 /* check access list. this function wants something of the following
653 * @group,name,name2,@group2,name3
655 * a NULL argument allows everybody to have access.
656 * we return three things:
658 * 0: list exists, but name isn't in it
662 #ifndef NO_REAL_USER_NAME
663 /* authentication is case insensitive
664 * FIXME should we do the same with group name?
666 #define access_strcmp strcasecmp
669 #define access_strcmp strcmp
673 static int accessvol(args, name)
677 char buf[MAXPATHLEN + 1], *p;
683 strncpy(buf, args, sizeof(buf));
684 if ((p = strtok(buf, ",")) == NULL) /* nothing, return okay */
688 if (*p == '@') { /* it's a group */
689 if ((gr = getgrnam(p + 1)) && gmem(gr->gr_gid))
691 } else if (access_strcmp(p, name) == 0) /* it's a user name */
693 p = strtok(NULL, ",");
699 static void setextmap( ext, type, creator, user)
700 char *ext, *type, *creator;
706 if (Extmap == NULL) {
707 if (( Extmap = calloc(1, sizeof( struct extmap ))) == NULL ) {
708 LOG(log_error, logtype_afpd, "setextmap: calloc: %s", strerror(errno) );
713 for ( em = Extmap, cnt = 0; em->em_ext; em++, cnt++) {
714 if ( (strdiacasecmp( em->em_ext, ext )) == 0 ) {
719 if ( em->em_ext == NULL ) {
720 if (!(Extmap = realloc( Extmap, sizeof( struct extmap ) * (cnt +2))) ) {
721 LOG(log_error, logtype_afpd, "setextmap: realloc: %s", strerror(errno) );
724 (Extmap +cnt +1)->em_ext = NULL;
726 } else if ( !user ) {
732 if (!(em->em_ext = strdup( ext))) {
733 LOG(log_error, logtype_afpd, "setextmap: strdup: %s", strerror(errno) );
737 if ( *type == '\0' ) {
738 memcpy(em->em_type, "????", sizeof( em->em_type ));
740 memcpy(em->em_type, type, sizeof( em->em_type ));
742 if ( *creator == '\0' ) {
743 memcpy(em->em_creator, "UNIX", sizeof( em->em_creator ));
745 memcpy(em->em_creator, creator, sizeof( em->em_creator ));
749 /* -------------------------- */
750 static int extmap_cmp(const void *map1, const void *map2)
752 const struct extmap *em1 = map1;
753 const struct extmap *em2 = map2;
754 return strdiacasecmp(em1->em_ext, em2->em_ext);
757 static void sortextmap( void)
762 if ((em = Extmap) == NULL) {
770 qsort(Extmap, Extmap_cnt, sizeof(struct extmap), extmap_cmp);
775 /* ----------------------
777 static void free_extmap( void)
787 /* ----------------------
789 static int volfile_changed(struct afp_volume_name *p)
799 if (!stat( name, &st) && st.st_mtime > p->mtime) {
800 p->mtime = st.st_mtime;
806 /* ----------------------
807 * Read a volume configuration file and add the volumes contained within to
808 * the global volume list. If p2 is non-NULL, the file that is opened is
811 * Lines that begin with # and blank lines are ignored.
812 * Volume lines are of the form:
813 * <unix path> [<volume name>] [allow:<user>,<@group>,...] \
814 * [codepage:<file>] [casefold:<num>]
815 * <extension> TYPE [CREATOR]
817 static int readvolfile(obj, p1, p2, user, pwent)
819 struct afp_volume_name *p1;
822 struct passwd *pwent;
825 char path[ MAXPATHLEN + 1], tmp[ MAXPATHLEN + 1],
826 volname[ AFPVOL_NAMELEN + 1 ], buf[ BUFSIZ ],
827 type[ 5 ], creator[ 5 ];
830 struct vol_option options[VOLOPT_NUM], save_options[VOLOPT_NUM];
838 strcpy( path, p1->name );
845 p1->full_name = strdup(path);
848 if (NULL == ( fp = fopen( path, "r" )) ) {
852 if (fd != -1 && !fstat( fd, &st) ) {
853 p1->mtime = st.st_mtime;
856 memset(save_options, 0, sizeof(save_options));
857 while ( myfgets( buf, sizeof( buf ), fp ) != NULL ) {
858 initline( strlen( buf ), buf );
859 parseline( sizeof( path ) - 1, path );
866 /* change the default options for this file */
867 if (strncmp(path, VOLOPT_DEFAULT, VOLOPT_DEFAULT_LEN) == 0) {
869 for (i = 0; i < VOLOPT_NUM; i++) {
870 if (parseline( sizeof( path ) - VOLOPT_DEFAULT_LEN - 1,
871 path + VOLOPT_DEFAULT_LEN) < 0)
873 volset(save_options, NULL, tmp, sizeof(tmp) - 1,
874 path + VOLOPT_DEFAULT_LEN);
880 if (( p = strchr( path, '/' )) != NULL ) {
888 if ( u == NULL || *u == '\0' || ( pw = getpwnam( u )) == NULL ) {
891 strcpy( tmp, pw->pw_dir );
892 if ( p != NULL && *p != '\0' ) {
896 /* Tag a user's home directory with their umask. Note, this will
897 * be overwritten if the user actually specifies a umask: option
898 * for a '~' volume. */
899 save_options[VOLOPT_UMASK].i_value = obj->options.save_mask;
903 /* send path through variable substitution */
904 if (*path != '~') /* need to copy path to tmp */
907 pwent = getpwnam(obj->username);
908 volxlate(obj, path, sizeof(path) - 1, tmp, pwent, NULL);
910 /* this is sort of braindead. basically, i want to be
911 * able to specify things in any order, but i don't want to
912 * re-write everything.
914 * currently we have options:
922 * options:prodos,crlf,noadouble,ro...
927 * namemask:x,y,!z (not implemented yet)
929 memcpy(options, save_options, sizeof(options));
932 /* read in up to VOLOP_NUM possible options */
933 for (i = 0; i < VOLOPT_NUM; i++) {
934 if (parseline( sizeof( tmp ) - 1, tmp ) < 0)
937 volset(options, save_options, volname, sizeof(volname) - 1, tmp);
940 /* check allow/deny lists:
941 allow -> either no list (-1), or in list (1)
942 deny -> either no list (-1), or not in list (0) */
943 if (accessvol(options[VOLOPT_ALLOW].c_value, obj->username) &&
944 (accessvol(options[VOLOPT_DENY].c_value, obj->username) < 1)) {
946 /* handle read-only behaviour. semantics:
947 * 1) neither the rolist nor the rwlist exist -> rw
948 * 2) rolist exists -> ro if user is in it.
949 * 3) rwlist exists -> ro unless user is in it. */
950 if (((options[VOLOPT_FLAGS].i_value & AFPVOL_RO) == 0) &&
951 ((accessvol(options[VOLOPT_ROLIST].c_value,
952 obj->username) == 1) ||
953 !accessvol(options[VOLOPT_RWLIST].c_value,
955 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
957 /* do variable substitution for volname */
958 volxlate(obj, tmp, sizeof(tmp) - 1, volname, pwent, path);
959 creatvol(obj, pwent, path, tmp, options, p2 != NULL);
961 volfree(options, save_options);
965 parseline( sizeof( type ) - 1, type );
966 parseline( sizeof( creator ) - 1, creator );
967 setextmap( path, type, creator, user);
974 volfree(save_options, NULL);
976 if ( fclose( fp ) != 0 ) {
977 LOG(log_error, logtype_afpd, "readvolfile: fclose: %s", strerror(errno) );
983 /* ------------------------------- */
984 static void volume_free(struct vol *vol)
989 free(vol->v_password);
991 free(vol->v_volcodepage);
992 free(vol->v_maccodepage);
995 free(vol->v_forceuid);
996 free(vol->v_forcegid);
997 #endif /* FORCE_UIDGID */
1000 /* ------------------------------- */
1001 static void free_volumes(void )
1004 struct vol *nvol, *ovol;
1006 for ( vol = Volumes; vol; vol = vol->v_next ) {
1007 if (( vol->v_flags & AFPVOL_OPEN ) ) {
1014 for ( vol = Volumes, ovol = NULL; vol; vol = nvol) {
1017 if (vol->v_name == NULL) {
1018 if (Volumes == vol) {
1025 ovol->v_next = nvol;
1035 /* ------------------------------- */
1036 static void volume_unlink(struct vol *volume)
1038 struct vol *vol, *ovol, *nvol;
1040 if (volume == Volumes) {
1041 Volumes = Volumes->v_next;
1044 for ( vol = Volumes, ovol = NULL; vol; vol = nvol) {
1047 if (vol == volume) {
1052 ovol->v_next = nvol;
1063 static int getvolspace( vol, bfree, btotal, xbfree, xbtotal, bsize )
1065 u_int32_t *bfree, *btotal, *bsize;
1066 VolSpace *xbfree, *xbtotal;
1070 #ifndef NO_QUOTA_SUPPORT
1071 VolSpace qfree, qtotal;
1074 spaceflag = AFPVOL_GVSMASK & vol->v_flags;
1075 /* report up to 2GB if afp version is < 2.2 (4GB if not) */
1076 maxsize = (vol->v_flags & AFPVOL_A2VOL) ? 0x01fffe00 :
1077 (((afp_version < 22) || (vol->v_flags & AFPVOL_LIMITSIZE))
1078 ? 0x7fffffffL : 0xffffffffL);
1081 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_AFSGVS ) {
1082 if ( afs_getvolspace( vol, xbfree, xbtotal, bsize ) == AFP_OK ) {
1083 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_AFSGVS;
1084 goto getvolspace_done;
1089 if (( rc = ustatfs_getvolspace( vol, xbfree, xbtotal,
1090 bsize)) != AFP_OK ) {
1094 #define min(a,b) ((a)<(b)?(a):(b))
1095 #ifndef NO_QUOTA_SUPPORT
1096 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_UQUOTA ) {
1097 if ( uquota_getvolspace( vol, &qfree, &qtotal, *bsize ) == AFP_OK ) {
1098 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_UQUOTA;
1099 *xbfree = min(*xbfree, qfree);
1100 *xbtotal = min( *xbtotal, qtotal);
1101 goto getvolspace_done;
1105 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_USTATFS;
1108 *bfree = min( *xbfree, maxsize);
1109 *btotal = min( *xbtotal, maxsize);
1113 /* -----------------------
1114 * set volume creation date
1115 * avoid duplicate, well at least it tries
1117 static void vol_setdate(u_int16_t id, struct adouble *adp, time_t date)
1120 struct vol *vol = Volumes;
1122 for ( volume = Volumes; volume; volume = volume->v_next ) {
1123 if (volume->v_vid == id) {
1126 else if (AD_DATE_FROM_UNIX(date) == volume->v_ctime) {
1128 volume = Volumes; /* restart */
1131 vol->v_ctime = AD_DATE_FROM_UNIX(date);
1132 ad_setdate(adp, AD_DATE_CREATE | AD_DATE_UNIX, date);
1135 /* ----------------------- */
1136 static int getvolparams( bitmap, vol, st, buf, buflen )
1144 int bit = 0, isad = 1;
1147 u_int32_t bfree, btotal, bsize;
1148 VolSpace xbfree, xbtotal; /* extended bytes */
1149 char *data, *nameoff = NULL;
1152 /* courtesy of jallison@whistle.com:
1153 * For MacOS8.x support we need to create the
1154 * .Parent file here if it doesn't exist. */
1156 ad_init(&ad, vol->v_adouble);
1157 if ( ad_open( vol->v_path, vol_noadouble(vol) |
1158 ADFLAGS_HF|ADFLAGS_DIR, O_RDWR | O_CREAT,
1161 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1163 } else if (ad_get_HF_flags( &ad ) & O_CREAT) {
1164 slash = strrchr( vol->v_path, '/' );
1168 slash = vol->v_path;
1170 ad_setentrylen( &ad, ADEID_NAME, strlen( slash ));
1171 memcpy(ad_entry( &ad, ADEID_NAME ), slash,
1172 ad_getentrylen( &ad, ADEID_NAME ));
1173 vol_setdate(vol->v_vid, &ad, st->st_mtime);
1174 ad_flush(&ad, ADFLAGS_HF);
1177 if (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0)
1178 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1180 vol->v_ctime = aint;
1183 if (( bitmap & ( (1<<VOLPBIT_BFREE)|(1<<VOLPBIT_BTOTAL) |
1184 (1<<VOLPBIT_XBFREE)|(1<<VOLPBIT_XBTOTAL) |
1185 (1<<VOLPBIT_BSIZE)) ) != 0 ) {
1186 if ( getvolspace( vol, &bfree, &btotal, &xbfree, &xbtotal,
1189 ad_close( &ad, ADFLAGS_HF );
1191 return( AFPERR_PARAM );
1196 while ( bitmap != 0 ) {
1197 while (( bitmap & 1 ) == 0 ) {
1205 if (0 == (vol->v_flags & AFPVOL_NOFILEID) && vol->v_cdb != NULL &&
1206 (vol->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1207 ashort = VOLPBIT_ATTR_FILEID;
1209 /* check for read-only.
1210 * NOTE: we don't actually set the read-only flag unless
1211 * it's passed in that way as it's possible to mount
1212 * a read-write filesystem under a read-only one. */
1213 if ((vol->v_flags & AFPVOL_RO) ||
1214 ((utime(vol->v_path, NULL) < 0) && (errno == EROFS))) {
1215 ashort |= VOLPBIT_ATTR_RO;
1217 ashort |= VOLPBIT_ATTR_CATSEARCH;
1218 if (afp_version >= 30) {
1219 ashort |= VOLPBIT_ATTR_UTF8;
1220 if (vol->v_flags & AFPVOL_UNIX_PRIV)
1221 ashort |= VOLPBIT_ATTR_UNIXPRIV;
1223 ashort = htons(ashort);
1224 memcpy(data, &ashort, sizeof( ashort ));
1225 data += sizeof( ashort );
1229 ashort = htons( AFPVOLSIG_DEFAULT );
1230 memcpy(data, &ashort, sizeof( ashort ));
1231 data += sizeof( ashort );
1234 case VOLPBIT_CDATE :
1235 aint = vol->v_ctime;
1236 memcpy(data, &aint, sizeof( aint ));
1237 data += sizeof( aint );
1240 case VOLPBIT_MDATE :
1241 if ( st->st_mtime > vol->v_mtime ) {
1242 vol->v_mtime = st->st_mtime;
1244 aint = AD_DATE_FROM_UNIX(vol->v_mtime);
1245 memcpy(data, &aint, sizeof( aint ));
1246 data += sizeof( aint );
1249 case VOLPBIT_BDATE :
1250 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1251 aint = AD_DATE_START;
1252 memcpy(data, &aint, sizeof( aint ));
1253 data += sizeof( aint );
1257 memcpy(data, &vol->v_vid, sizeof( vol->v_vid ));
1258 data += sizeof( vol->v_vid );
1261 case VOLPBIT_BFREE :
1262 bfree = htonl( bfree );
1263 memcpy(data, &bfree, sizeof( bfree ));
1264 data += sizeof( bfree );
1267 case VOLPBIT_BTOTAL :
1268 btotal = htonl( btotal );
1269 memcpy(data, &btotal, sizeof( btotal ));
1270 data += sizeof( btotal );
1273 #ifndef NO_LARGE_VOL_SUPPORT
1274 case VOLPBIT_XBFREE :
1275 xbfree = hton64( xbfree );
1276 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1277 bcopy(&xbfree, data, sizeof(xbfree));
1278 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1279 memcpy(data, &xbfree, sizeof( xbfree ));
1280 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1281 data += sizeof( xbfree );
1284 case VOLPBIT_XBTOTAL :
1285 xbtotal = hton64( xbtotal );
1286 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1287 bcopy(&xbtotal, data, sizeof(xbtotal));
1288 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1289 memcpy(data, &xbtotal, sizeof( xbtotal ));
1290 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1291 data += sizeof( xbfree );
1293 #endif /* ! NO_LARGE_VOL_SUPPORT */
1297 data += sizeof( u_int16_t );
1300 case VOLPBIT_BSIZE: /* block size */
1301 bsize = htonl(bsize);
1302 memcpy(data, &bsize, sizeof(bsize));
1303 data += sizeof(bsize);
1308 ad_close( &ad, ADFLAGS_HF );
1310 return( AFPERR_BITMAP );
1316 ashort = htons( data - buf );
1317 memcpy(nameoff, &ashort, sizeof( ashort ));
1318 aint = strlen( vol->v_name );
1320 memcpy(data, vol->v_name, aint );
1324 ad_close( &ad, ADFLAGS_HF );
1326 *buflen = data - buf;
1330 /* ------------------------- */
1331 int static stat_vol(u_int16_t bitmap, struct vol *vol, char *rbuf, int *rbuflen)
1336 if ( stat( vol->v_path, &st ) < 0 ) {
1338 return( AFPERR_PARAM );
1341 buflen = *rbuflen - sizeof( bitmap );
1342 if (( ret = getvolparams( bitmap, vol, &st,
1343 rbuf + sizeof( bitmap ), &buflen )) != AFP_OK ) {
1347 *rbuflen = buflen + sizeof( bitmap );
1348 bitmap = htons( bitmap );
1349 memcpy(rbuf, &bitmap, sizeof( bitmap ));
1354 /* ------------------------------- */
1355 void load_volumes(AFPObj *obj)
1357 struct passwd *pwent;
1362 /* check files date */
1363 if (obj->options.defaultvol.loaded) {
1364 changed = volfile_changed(&obj->options.defaultvol);
1366 if (obj->options.systemvol.loaded) {
1367 changed |= volfile_changed(&obj->options.systemvol);
1369 if (obj->options.uservol.loaded) {
1370 changed |= volfile_changed(&obj->options.uservol);
1379 pwent = getpwnam(obj->username);
1380 if ( (obj->options.flags & OPTION_USERVOLFIRST) == 0 ) {
1381 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent);
1384 if ((*obj->username == '\0') || (obj->options.flags & OPTION_NOUSERVOL)) {
1385 readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent);
1388 * Read user's AppleVolumes or .AppleVolumes file
1389 * If neither are readable, read the default volumes file. if
1390 * that doesn't work, create a user share.
1392 obj->options.uservol.name = strdup(pwent->pw_dir);
1393 if ( readvolfile(obj, &obj->options.uservol, "AppleVolumes", 1, pwent) < 0 &&
1394 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 obj->options.defaultvol.name != NULL ) {
1398 if (readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent) < 0)
1399 creatvol(obj, pwent, pwent->pw_dir, NULL, NULL, 1);
1402 if ( obj->options.flags & OPTION_USERVOLFIRST ) {
1403 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent );
1407 /* ------------------------------- */
1408 int afp_getsrvrparms(obj, ibuf, ibuflen, rbuf, rbuflen )
1411 int ibuflen, *rbuflen;
1422 for ( vcnt = 0, volume = Volumes; volume; volume = volume->v_next ) {
1423 if ( stat( volume->v_path, &st ) < 0 ) {
1424 if ((volume->v_flags & AFPVOL_NOSTAT)) {
1427 LOG(log_info, logtype_afpd, "afp_getsrvrparms: stat %s: %s",
1428 volume->v_path, strerror(errno) );
1429 continue; /* can't access directory */
1431 if (!S_ISDIR(st.st_mode)) {
1432 continue; /* not a dir */
1435 if (volume->v_hide) {
1436 continue; /* config file changed but the volume was mounted */
1438 /* set password bit if there's a volume password */
1439 *data = (volume->v_password) ? AFPSRVR_PASSWD : 0;
1441 /* Apple 2 clients running ProDOS-8 expect one volume to have
1442 bit 0 of this byte set. They will not recognize anything
1443 on the server unless this is the case. I have not
1444 completely worked this out, but it's related to booting
1445 from the server. Support for that function is a ways
1446 off.. <shirsch@ibm.net> */
1447 *data |= (volume->v_flags & AFPVOL_A2VOL) ? AFPSRVR_CONFIGINFO : 0;
1448 *data++ |= 0; /* UNIX PRIVS BIT ..., OSX doesn't seem to use it, so we don't either */
1449 len = strlen( volume->v_name );
1451 memcpy(data, volume->v_name, len );
1456 *rbuflen = data - rbuf;
1458 if ( gettimeofday( &tv, 0 ) < 0 ) {
1459 LOG(log_error, logtype_afpd, "afp_getsrvrparms: gettimeofday: %s", strerror(errno) );
1461 return AFPERR_PARAM;
1463 tv.tv_sec = AD_DATE_FROM_UNIX(tv.tv_sec);
1464 memcpy(data, &tv.tv_sec, sizeof( u_int32_t));
1465 data += sizeof( u_int32_t);
1470 /* -------------------------
1471 * we are the user here
1473 int afp_openvol(obj, ibuf, ibuflen, rbuf, rbuflen )
1476 int ibuflen, *rbuflen;
1487 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1488 bitmap = ntohs( bitmap );
1489 ibuf += sizeof( bitmap );
1490 if (( bitmap & (1<<VOLPBIT_VID)) == 0 ) {
1492 return AFPERR_BITMAP;
1495 len = (unsigned char)*ibuf++;
1496 volname = obj->oldtmp;
1497 memcpy(volname, ibuf, len );
1498 *(volname + len) = '\0';
1500 if ((len + 1) & 1) /* pad to an even boundary */
1505 for ( volume = Volumes; volume; volume = volume->v_next ) {
1506 if ( strcasecmp( volname, volume->v_name ) == 0 ) {
1511 if ( volume == NULL ) {
1513 return AFPERR_PARAM;
1516 /* check for a volume password */
1517 if (volume->v_password && strncmp(ibuf, volume->v_password, VOLPASSLEN)) {
1519 return AFPERR_ACCESS;
1522 if (( volume->v_flags & AFPVOL_OPEN ) ) {
1523 /* the volume is already open */
1525 set_uidgid ( volume );
1527 return stat_vol(bitmap, volume, rbuf, rbuflen);
1530 /* initialize volume variables
1533 if (afp_version >= 30) {
1534 volume->max_filename = 255;
1537 volume->max_filename = MACFILELEN;
1540 volume->v_dir = volume->v_root = NULL;
1542 /* FIXME unix name != mac name */
1543 if ((dir = dirnew(volume->v_name, volume->v_name) ) == NULL) {
1544 LOG(log_error, logtype_afpd, "afp_openvol: malloc: %s", strerror(errno) );
1549 dir->d_did = DIRDID_ROOT;
1550 dir->d_color = DIRTREE_COLOR_BLACK; /* root node is black */
1551 volume->v_dir = volume->v_root = dir;
1552 volume->v_flags |= AFPVOL_OPEN;
1553 volume->v_cdb = NULL;
1555 if (volume->v_root_preexec) {
1556 if ((ret = afprun(1, volume->v_root_preexec, NULL)) && volume->v_root_preexec_close) {
1557 LOG(log_error, logtype_afpd, "afp_openvol: root preexec : %d", ret );
1564 set_uidgid ( volume );
1567 if (volume->v_preexec) {
1568 if ((ret = afprun(0, volume->v_preexec, NULL)) && volume->v_preexec_close) {
1569 LOG(log_error, logtype_afpd, "afp_openvol: preexec : %d", ret );
1575 if ( stat( volume->v_path, &st ) < 0 ) {
1580 if ( chdir( volume->v_path ) < 0 ) {
1584 curdir = volume->v_dir;
1585 if (volume->v_cnidscheme == NULL) {
1586 volume->v_cnidscheme = strdup(DEFAULT_CNID_SCHEME);
1587 LOG(log_warning, logtype_afpd, "Warning: No CNID scheme for volume %s. Using default.",
1590 if (volume->v_dbpath)
1591 volume->v_cdb = cnid_open (volume->v_dbpath, volume->v_umask, volume->v_cnidscheme);
1592 if (volume->v_cdb == NULL)
1593 volume->v_cdb = cnid_open (volume->v_path, volume->v_umask, volume->v_cnidscheme);
1594 if (volume->v_cdb == NULL) {
1595 volume->v_cdb = cnid_open (volume->v_path, volume->v_umask, DEFAULT_CNID_SCHEME);
1596 LOG(log_error, logtype_afpd, "Error: invalid CNID backend for %s: %s", volume->v_path,
1597 volume->v_cnidscheme);
1599 if (volume->v_cdb == NULL) {
1600 LOG(log_error, logtype_afpd, "Fatal error: cannot open CNID for %s", volume->v_path);
1607 if (!volume->v_volcodepage)
1608 volume->v_volcodepage = strdup("UTF8");
1610 if ( (charset_t) -1 == ( volume->v_volcharset = add_charset(volume->v_volcodepage)) ) {
1611 LOG (log_error, logtype_afpd, "Setting codepage %s as volume codepage failed", volume->v_volcodepage);
1616 if ( NULL == ( volume->v_vol = find_charset_functions(volume->v_volcodepage)) || volume->v_vol->flags & CHARSET_ICONV ) {
1617 LOG (log_warning, logtype_afpd, "WARNING: volume encoding %s is *not* supported by netatalk, expect problems !!!!", volume->v_volcodepage);
1620 if (!volume->v_maccodepage)
1621 volume->v_maccodepage = strdup(obj->options.maccodepage);
1623 if ( (charset_t) -1 == ( volume->v_maccharset = add_charset(volume->v_maccodepage)) ) {
1624 LOG (log_error, logtype_afpd, "Setting codepage %s as mac codepage failed", volume->v_maccodepage);
1629 if ( NULL == ( volume->v_mac = find_charset_functions(volume->v_maccodepage)) || ! (volume->v_mac->flags & CHARSET_CLIENT) ) {
1630 LOG (log_error, logtype_afpd, "Fatal error: mac charset %s not supported", volume->v_maccodepage);
1635 ret = stat_vol(bitmap, volume, rbuf, rbuflen);
1636 if (ret == AFP_OK) {
1638 handle_special_folders( volume );
1641 * If you mount a volume twice, the second time the trash appears on
1642 * the desk-top. That's because the Mac remembers the DID for the
1643 * trash (even for volumes in different zones, on different servers).
1644 * Just so this works better, we prime the DID cache with the trash,
1645 * fixing the trash at DID 17.
1646 * FIXME (RL): should it be done inside a CNID backend ? (always returning Trash DID when asked) ?
1648 if ((volume->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1650 /* FIXME find db time stamp */
1651 cnid_getstamp(volume->v_cdb, volume->v_stamp, sizeof(volume->v_stamp));
1653 cname( volume, volume->v_dir, &p );
1659 if (volume->v_dir) {
1660 dirfree( volume->v_dir );
1661 volume->v_dir = volume->v_root = NULL;
1664 volume->v_flags &= ~AFPVOL_OPEN;
1665 if (volume->v_cdb != NULL) {
1666 cnid_close(volume->v_cdb);
1667 volume->v_cdb = NULL;
1673 /* ------------------------- */
1674 static void closevol(struct vol *vol)
1679 dirfree( vol->v_root );
1681 if (vol->v_cdb != NULL) {
1682 cnid_close(vol->v_cdb);
1686 if (vol->v_postexec) {
1687 afprun(0, vol->v_postexec, NULL);
1689 if (vol->v_root_postexec) {
1690 afprun(1, vol->v_root_postexec, NULL);
1694 /* ------------------------- */
1695 void close_all_vol(void)
1699 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
1700 if ( ovol->v_flags & AFPVOL_OPEN ) {
1701 ovol->v_flags &= ~AFPVOL_OPEN;
1707 /* ------------------------- */
1708 int afp_closevol(obj, ibuf, ibuflen, rbuf, rbuflen )
1711 int ibuflen, *rbuflen;
1713 struct vol *vol, *ovol;
1718 memcpy(&vid, ibuf, sizeof( vid ));
1719 if (NULL == ( vol = getvolbyvid( vid )) ) {
1720 return( AFPERR_PARAM );
1723 vol->v_flags &= ~AFPVOL_OPEN;
1724 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
1725 if ( ovol->v_flags & AFPVOL_OPEN ) {
1729 if ( ovol != NULL ) {
1730 /* Even if chdir fails, we can't say afp_closevol fails. */
1731 if ( chdir( ovol->v_path ) == 0 ) {
1732 curdir = ovol->v_dir;
1737 if (vol->v_deleted) {
1738 showvol(vol->v_name);
1745 /* ------------------------- */
1746 struct vol *getvolbyvid(const u_int16_t vid )
1750 for ( vol = Volumes; vol; vol = vol->v_next ) {
1751 if ( vid == vol->v_vid ) {
1755 if ( vol == NULL || ( vol->v_flags & AFPVOL_OPEN ) == 0 ) {
1761 #endif /* FORCE_UIDGID */
1766 /* ------------------------ */
1767 static int ext_cmp_key(const void *key, const void *obj)
1769 const char *p = key;
1770 const struct extmap *em = obj;
1771 return strdiacasecmp(p, em->em_ext);
1773 struct extmap *getextmap(const char *path)
1778 if (NULL == ( p = strrchr( path, '.' )) ) {
1779 return( Defextmap );
1782 if (!*p || !Extmap_cnt) {
1783 return( Defextmap );
1785 em = bsearch(p, Extmap, Extmap_cnt, sizeof(struct extmap), ext_cmp_key);
1789 return( Defextmap );
1793 /* ------------------------- */
1794 struct extmap *getdefextmap(void)
1796 return( Defextmap );
1799 /* --------------------------
1800 poll if a volume is changed by other processes.
1802 int pollvoltime(obj)
1809 if (!(afp_version > 21 && obj->options.server_notif))
1812 if ( gettimeofday( &tv, 0 ) < 0 )
1815 for ( vol = Volumes; vol; vol = vol->v_next ) {
1816 if ( (vol->v_flags & AFPVOL_OPEN) && vol->v_mtime + 30 < tv.tv_sec) {
1817 if ( !stat( vol->v_path, &st ) && vol->v_mtime != st.st_mtime ) {
1818 vol->v_mtime = st.st_mtime;
1819 if (!obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED))
1828 /* ------------------------- */
1829 void setvoltime(obj, vol )
1835 /* just looking at vol->v_mtime is broken seriously since updates
1836 * from other users afpd processes never are seen.
1837 * This is not the most elegant solution (a shared memory between
1838 * the afpd processes would come closer)
1841 if ( gettimeofday( &tv, 0 ) < 0 ) {
1842 LOG(log_error, logtype_afpd, "setvoltime: gettimeofday: %s", strerror(errno) );
1845 if( utime( vol->v_path, NULL ) < 0 ) {
1846 /* write of time failed ... probably a read only filesys,
1847 * where no other users can interfere, so there's no issue here
1851 /* a little granularity */
1852 if (vol->v_mtime < tv.tv_sec) {
1853 vol->v_mtime = tv.tv_sec;
1854 if (afp_version > 21 && obj->options.server_notif) {
1855 obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED);
1860 /* ------------------------- */
1861 int afp_getvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1864 int ibuflen, *rbuflen;
1867 u_int16_t vid, bitmap;
1870 memcpy(&vid, ibuf, sizeof( vid ));
1871 ibuf += sizeof( vid );
1872 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1873 bitmap = ntohs( bitmap );
1875 if (NULL == ( vol = getvolbyvid( vid )) ) {
1877 return( AFPERR_PARAM );
1880 return stat_vol(bitmap, vol, rbuf, rbuflen);
1883 /* ------------------------- */
1884 int afp_setvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1887 int ibuflen, *rbuflen;
1891 u_int16_t vid, bitmap;
1897 memcpy(&vid, ibuf, sizeof( vid ));
1898 ibuf += sizeof( vid );
1899 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1900 bitmap = ntohs( bitmap );
1901 ibuf += sizeof(bitmap);
1903 if (( vol = getvolbyvid( vid )) == NULL ) {
1904 return( AFPERR_PARAM );
1907 if (vol->v_flags & AFPVOL_RO)
1908 return AFPERR_VLOCK;
1910 /* we can only set the backup date. */
1911 if (bitmap != (1 << VOLPBIT_BDATE))
1912 return AFPERR_BITMAP;
1914 ad_init(&ad, vol->v_adouble);
1915 if ( ad_open( vol->v_path, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR,
1918 return AFPERR_VLOCK;
1920 return AFPERR_ACCESS;
1923 memcpy(&aint, ibuf, sizeof(aint));
1924 ad_setdate(&ad, AD_DATE_BACKUP, aint);
1925 ad_flush(&ad, ADFLAGS_HF);
1926 ad_close(&ad, ADFLAGS_HF);
1930 /* ------------------------- */
1931 int wincheck(const struct vol *vol, const char *path)
1935 if (!(vol->v_flags & AFPVOL_MSWINDOWS))
1938 /* empty paths are not allowed */
1939 if ((len = strlen(path)) == 0)
1942 /* leading or trailing whitespaces are not allowed, carriage returns
1943 * and probably other whitespace is okay, tabs are not allowed
1945 if ((path[0] == ' ') || (path[len-1] == ' '))
1948 /* certain characters are not allowed */
1949 if (strpbrk(path, MSWINDOWS_BADCHARS))
1952 /* everything else is okay */
1958 * precreate a folder
1959 * this is only intended for folders in the volume root
1960 * It will *not* work if the folder name contains extended characters
1962 static int create_special_folder (const struct vol *vol, const struct _special_folder *folder)
1971 p = (char *) malloc ( strlen(vol->v_path)+strlen(folder->name)+2);
1973 LOG(log_error, logtype_afpd,"malloc failed");
1976 strcpy(p, vol->v_path);
1978 strcat(p, folder->name);
1980 if ( (ret = stat( p, &st )) < 0 ) {
1981 if (folder->precreate) {
1982 if (ad_mkdir(p, folder->mode & ~vol->v_umask)) {
1983 LOG(log_debug, logtype_afpd,"Creating '%s' failed", folder->name);
1991 if ( !ret && folder->hide) {
1993 ad_init(&ad, vol->v_adouble);
1994 if (ad_open( p, vol_noadouble(vol) | ADFLAGS_HF|ADFLAGS_DIR,
1995 O_RDWR|O_CREAT, 0666, &ad) < 0) {
1999 if ((ad_get_HF_flags( &ad ) & O_CREAT) ) {
2000 ad_setentrylen( &ad, ADEID_NAME, strlen(folder->name));
2001 memcpy(ad_entry( &ad, ADEID_NAME ), folder->name,
2002 ad_getentrylen( &ad, ADEID_NAME ));
2005 ad_getattr(&ad, &attr);
2006 attr |= htons( ntohs( attr ) | ATTRBIT_INVISIBLE );
2007 ad_setattr(&ad, attr);
2009 ad_flush( &ad, ADFLAGS_HF );
2010 ad_close( &ad, ADFLAGS_HF );
2016 static void handle_special_folders (const struct vol * vol)
2018 const _special_folder *p = &special_folders[0];
2020 if (vol->v_flags & AFPVOL_RO)
2023 for (; p->name != NULL; p++) {
2024 create_special_folder (vol, p);