2 * $Id: volume.c,v 1.51.2.7.2.5 2003-09-27 03:05:41 bfernhomberg 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 static int getvolparams( bitmap, vol, st, buf, buflen )
1122 int bit = 0, isad = 1;
1125 u_int32_t bfree, btotal, bsize;
1126 VolSpace xbfree, xbtotal; /* extended bytes */
1127 char *data, *nameoff = NULL;
1130 /* courtesy of jallison@whistle.com:
1131 * For MacOS8.x support we need to create the
1132 * .Parent file here if it doesn't exist. */
1134 ad_init(&ad, vol->v_adouble);
1135 if ( ad_open( vol->v_path, vol_noadouble(vol) |
1136 ADFLAGS_HF|ADFLAGS_DIR, O_RDWR | O_CREAT,
1140 } else if (ad_get_HF_flags( &ad ) & O_CREAT) {
1141 slash = strrchr( vol->v_path, '/' );
1145 slash = vol->v_path;
1147 ad_setentrylen( &ad, ADEID_NAME, strlen( slash ));
1148 memcpy(ad_entry( &ad, ADEID_NAME ), slash,
1149 ad_getentrylen( &ad, ADEID_NAME ));
1150 ad_setdate(&ad, AD_DATE_CREATE | AD_DATE_UNIX, st->st_mtime);
1151 ad_flush(&ad, ADFLAGS_HF);
1154 if (( bitmap & ( (1<<VOLPBIT_BFREE)|(1<<VOLPBIT_BTOTAL) |
1155 (1<<VOLPBIT_XBFREE)|(1<<VOLPBIT_XBTOTAL) |
1156 (1<<VOLPBIT_BSIZE)) ) != 0 ) {
1157 if ( getvolspace( vol, &bfree, &btotal, &xbfree, &xbtotal,
1160 ad_close( &ad, ADFLAGS_HF );
1162 return( AFPERR_PARAM );
1167 while ( bitmap != 0 ) {
1168 while (( bitmap & 1 ) == 0 ) {
1176 if (0 == (vol->v_flags & AFPVOL_NOFILEID) && vol->v_cdb != NULL &&
1177 (vol->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1178 ashort = VOLPBIT_ATTR_FILEID;
1180 /* check for read-only.
1181 * NOTE: we don't actually set the read-only flag unless
1182 * it's passed in that way as it's possible to mount
1183 * a read-write filesystem under a read-only one. */
1184 if ((vol->v_flags & AFPVOL_RO) ||
1185 ((utime(vol->v_path, NULL) < 0) && (errno == EROFS))) {
1186 ashort |= VOLPBIT_ATTR_RO;
1188 ashort |= VOLPBIT_ATTR_CATSEARCH;
1189 if (afp_version >= 30) {
1190 ashort |= VOLPBIT_ATTR_UTF8;
1191 if (vol->v_flags & AFPVOL_UNIX_PRIV)
1192 ashort |= VOLPBIT_ATTR_UNIXPRIV;
1194 ashort = htons(ashort);
1195 memcpy(data, &ashort, sizeof( ashort ));
1196 data += sizeof( ashort );
1200 ashort = htons( AFPVOLSIG_DEFAULT );
1201 memcpy(data, &ashort, sizeof( ashort ));
1202 data += sizeof( ashort );
1205 case VOLPBIT_CDATE :
1206 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
1207 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1208 memcpy(data, &aint, sizeof( aint ));
1209 data += sizeof( aint );
1212 case VOLPBIT_MDATE :
1213 if ( st->st_mtime > vol->v_time ) {
1214 vol->v_time = st->st_mtime;
1215 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1217 aint = AD_DATE_FROM_UNIX(vol->v_time);
1219 memcpy(data, &aint, sizeof( aint ));
1220 data += sizeof( aint );
1223 case VOLPBIT_BDATE :
1224 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1225 aint = AD_DATE_START;
1226 memcpy(data, &aint, sizeof( aint ));
1227 data += sizeof( aint );
1231 memcpy(data, &vol->v_vid, sizeof( vol->v_vid ));
1232 data += sizeof( vol->v_vid );
1235 case VOLPBIT_BFREE :
1236 bfree = htonl( bfree );
1237 memcpy(data, &bfree, sizeof( bfree ));
1238 data += sizeof( bfree );
1241 case VOLPBIT_BTOTAL :
1242 btotal = htonl( btotal );
1243 memcpy(data, &btotal, sizeof( btotal ));
1244 data += sizeof( btotal );
1247 #ifndef NO_LARGE_VOL_SUPPORT
1248 case VOLPBIT_XBFREE :
1249 xbfree = hton64( xbfree );
1250 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1251 bcopy(&xbfree, data, sizeof(xbfree));
1252 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1253 memcpy(data, &xbfree, sizeof( xbfree ));
1254 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1255 data += sizeof( xbfree );
1258 case VOLPBIT_XBTOTAL :
1259 xbtotal = hton64( xbtotal );
1260 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1261 bcopy(&xbtotal, data, sizeof(xbtotal));
1262 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1263 memcpy(data, &xbtotal, sizeof( xbtotal ));
1264 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1265 data += sizeof( xbfree );
1267 #endif /* ! NO_LARGE_VOL_SUPPORT */
1271 data += sizeof( u_int16_t );
1274 case VOLPBIT_BSIZE: /* block size */
1275 bsize = htonl(bsize);
1276 memcpy(data, &bsize, sizeof(bsize));
1277 data += sizeof(bsize);
1282 ad_close( &ad, ADFLAGS_HF );
1284 return( AFPERR_BITMAP );
1290 ashort = htons( data - buf );
1291 memcpy(nameoff, &ashort, sizeof( ashort ));
1292 aint = strlen( vol->v_name );
1294 memcpy(data, vol->v_name, aint );
1298 ad_close( &ad, ADFLAGS_HF );
1300 *buflen = data - buf;
1304 /* ------------------------- */
1305 int static stat_vol(u_int16_t bitmap, struct vol *vol, char *rbuf, int *rbuflen)
1310 if ( stat( vol->v_path, &st ) < 0 ) {
1312 return( AFPERR_PARAM );
1315 buflen = *rbuflen - sizeof( bitmap );
1316 if (( ret = getvolparams( bitmap, vol, &st,
1317 rbuf + sizeof( bitmap ), &buflen )) != AFP_OK ) {
1321 *rbuflen = buflen + sizeof( bitmap );
1322 bitmap = htons( bitmap );
1323 memcpy(rbuf, &bitmap, sizeof( bitmap ));
1328 /* ------------------------------- */
1329 void load_volumes(AFPObj *obj)
1331 struct passwd *pwent;
1336 /* check files date */
1337 if (obj->options.defaultvol.loaded) {
1338 changed = volfile_changed(&obj->options.defaultvol);
1340 if (obj->options.systemvol.loaded) {
1341 changed |= volfile_changed(&obj->options.systemvol);
1343 if (obj->options.uservol.loaded) {
1344 changed |= volfile_changed(&obj->options.uservol);
1353 pwent = getpwnam(obj->username);
1354 if ( (obj->options.flags & OPTION_USERVOLFIRST) == 0 ) {
1355 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent);
1358 if ((*obj->username == '\0') || (obj->options.flags & OPTION_NOUSERVOL)) {
1359 readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent);
1362 * Read user's AppleVolumes or .AppleVolumes file
1363 * If neither are readable, read the default volumes file. if
1364 * that doesn't work, create a user share.
1366 obj->options.uservol.name = strdup(pwent->pw_dir);
1367 if ( readvolfile(obj, &obj->options.uservol, "AppleVolumes", 1, pwent) < 0 &&
1368 readvolfile(obj, &obj->options.uservol, ".AppleVolumes", 1, pwent) < 0 &&
1369 readvolfile(obj, &obj->options.uservol, "applevolumes", 1, pwent) < 0 &&
1370 readvolfile(obj, &obj->options.uservol, ".applevolumes", 1, pwent) < 0 &&
1371 obj->options.defaultvol.name != NULL ) {
1372 if (readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent) < 0)
1373 creatvol(obj, pwent, pwent->pw_dir, NULL, NULL, 1);
1376 if ( obj->options.flags & OPTION_USERVOLFIRST ) {
1377 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent );
1381 /* ------------------------------- */
1382 int afp_getsrvrparms(obj, ibuf, ibuflen, rbuf, rbuflen )
1385 int ibuflen, *rbuflen;
1396 for ( vcnt = 0, volume = Volumes; volume; volume = volume->v_next ) {
1397 if (!(volume->v_flags & AFPVOL_NOSTAT)) {
1398 if ( stat( volume->v_path, &st ) < 0 ) {
1399 LOG(log_info, logtype_afpd, "afp_getsrvrparms: stat %s: %s",
1400 volume->v_path, strerror(errno) );
1401 continue; /* can't access directory */
1403 if (!S_ISDIR(st.st_mode)) {
1404 continue; /* not a dir */
1407 if (volume->v_hide) {
1408 continue; /* config file changed but the volume was mounted */
1411 /* set password bit if there's a volume password */
1412 *data = (volume->v_password) ? AFPSRVR_PASSWD : 0;
1414 /* Apple 2 clients running ProDOS-8 expect one volume to have
1415 bit 0 of this byte set. They will not recognize anything
1416 on the server unless this is the case. I have not
1417 completely worked this out, but it's related to booting
1418 from the server. Support for that function is a ways
1419 off.. <shirsch@ibm.net> */
1420 *data |= (volume->v_flags & AFPVOL_A2VOL) ? AFPSRVR_CONFIGINFO : 0;
1421 *data++ |= 0; /* UNIX PRIVS BIT ..., OSX doesn't seem to use it, so we don't either */
1422 len = strlen( volume->v_name );
1424 memcpy(data, volume->v_name, len );
1429 *rbuflen = data - rbuf;
1431 if ( gettimeofday( &tv, 0 ) < 0 ) {
1432 LOG(log_error, logtype_afpd, "afp_getsrvrparms: gettimeofday: %s", strerror(errno) );
1434 return AFPERR_PARAM;
1436 tv.tv_sec = AD_DATE_FROM_UNIX(tv.tv_sec);
1437 memcpy(data, &tv.tv_sec, sizeof( u_int32_t));
1438 data += sizeof( u_int32_t);
1443 /* -------------------------
1444 * we are the user here
1446 int afp_openvol(obj, ibuf, ibuflen, rbuf, rbuflen )
1449 int ibuflen, *rbuflen;
1460 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1461 bitmap = ntohs( bitmap );
1462 ibuf += sizeof( bitmap );
1463 if (( bitmap & (1<<VOLPBIT_VID)) == 0 ) {
1465 return AFPERR_BITMAP;
1468 len = (unsigned char)*ibuf++;
1469 volname = obj->oldtmp;
1470 memcpy(volname, ibuf, len );
1471 *(volname + len) = '\0';
1473 if ((len + 1) & 1) /* pad to an even boundary */
1478 for ( volume = Volumes; volume; volume = volume->v_next ) {
1479 if ( strcasecmp( volname, volume->v_name ) == 0 ) {
1484 if ( volume == NULL ) {
1486 return AFPERR_PARAM;
1489 /* check for a volume password */
1490 if (volume->v_password && strncmp(ibuf, volume->v_password, VOLPASSLEN)) {
1492 return AFPERR_ACCESS;
1495 if (( volume->v_flags & AFPVOL_OPEN ) ) {
1496 /* the volume is already open */
1498 set_uidgid ( volume );
1500 return stat_vol(bitmap, volume, rbuf, rbuflen);
1503 /* initialize volume variables
1506 if (afp_version >= 30) {
1507 volume->max_filename = 255;
1510 volume->max_filename = MACFILELEN;
1513 volume->v_dir = volume->v_root = NULL;
1515 /* FIXME unix name != mac name */
1516 if ((dir = dirnew(volume->v_name, volume->v_name) ) == NULL) {
1517 LOG(log_error, logtype_afpd, "afp_openvol: malloc: %s", strerror(errno) );
1522 dir->d_did = DIRDID_ROOT;
1523 dir->d_color = DIRTREE_COLOR_BLACK; /* root node is black */
1524 volume->v_dir = volume->v_root = dir;
1525 volume->v_flags |= AFPVOL_OPEN;
1526 volume->v_cdb = NULL;
1528 if (volume->v_root_preexec) {
1529 if ((ret = afprun(1, volume->v_root_preexec, NULL)) && volume->v_root_preexec_close) {
1530 LOG(log_error, logtype_afpd, "afp_openvol: root preexec : %d", ret );
1537 set_uidgid ( volume );
1540 if (volume->v_preexec) {
1541 if ((ret = afprun(0, volume->v_preexec, NULL)) && volume->v_preexec_close) {
1542 LOG(log_error, logtype_afpd, "afp_openvol: preexec : %d", ret );
1548 if ( stat( volume->v_path, &st ) < 0 ) {
1553 if ( chdir( volume->v_path ) < 0 ) {
1557 curdir = volume->v_dir;
1558 if (volume->v_cnidscheme == NULL) {
1559 volume->v_cnidscheme = strdup(DEFAULT_CNID_SCHEME);
1560 LOG(log_warning, logtype_afpd, "Warning: No CNID scheme for volume %s. Using default.",
1563 if (volume->v_dbpath)
1564 volume->v_cdb = cnid_open (volume->v_dbpath, volume->v_umask, volume->v_cnidscheme);
1565 if (volume->v_cdb == NULL)
1566 volume->v_cdb = cnid_open (volume->v_path, volume->v_umask, volume->v_cnidscheme);
1567 if (volume->v_cdb == NULL) {
1568 volume->v_cdb = cnid_open (volume->v_path, volume->v_umask, DEFAULT_CNID_SCHEME);
1569 LOG(log_error, logtype_afpd, "Error: invalid CNID backend for %s: %s", volume->v_path,
1570 volume->v_cnidscheme);
1572 if (volume->v_cdb == NULL) {
1573 LOG(log_error, logtype_afpd, "Fatal error: cannot open CNID for %s", volume->v_path);
1580 if (!volume->v_volcodepage)
1581 volume->v_volcodepage = strdup("UTF8");
1583 if ( (charset_t) -1 == ( volume->v_volcharset = add_charset(volume->v_volcodepage)) ) {
1584 LOG (log_error, logtype_afpd, "Setting codepage %s as volume codepage failed", volume->v_volcodepage);
1589 if ( NULL == ( volume->v_vol = find_charset_functions(volume->v_volcodepage)) || volume->v_vol->flags & CHARSET_ICONV ) {
1590 LOG (log_warning, logtype_afpd, "WARNING: volume encoding %s is *not* supported by netatalk, expect problems !!!!", volume->v_volcodepage);
1593 if (!volume->v_maccodepage)
1594 volume->v_maccodepage = strdup(obj->options.maccodepage);
1596 if ( (charset_t) -1 == ( volume->v_maccharset = add_charset(volume->v_maccodepage)) ) {
1597 LOG (log_error, logtype_afpd, "Setting codepage %s as mac codepage failed", volume->v_maccodepage);
1602 if ( NULL == ( volume->v_mac = find_charset_functions(volume->v_maccodepage)) || ! (volume->v_mac->flags & CHARSET_CLIENT) ) {
1603 LOG (log_error, logtype_afpd, "Fatal error: mac charset %s not supported", volume->v_maccodepage);
1608 ret = stat_vol(bitmap, volume, rbuf, rbuflen);
1609 if (ret == AFP_OK) {
1611 handle_special_folders( volume );
1614 * If you mount a volume twice, the second time the trash appears on
1615 * the desk-top. That's because the Mac remembers the DID for the
1616 * trash (even for volumes in different zones, on different servers).
1617 * Just so this works better, we prime the DID cache with the trash,
1618 * fixing the trash at DID 17.
1619 * FIXME (RL): should it be done inside a CNID backend ? (always returning Trash DID when asked) ?
1621 if ((volume->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1623 /* FIXME find db time stamp */
1624 cnid_getstamp(volume->v_cdb, volume->v_stamp, sizeof(volume->v_stamp));
1626 cname( volume, volume->v_dir, &p );
1632 if (volume->v_dir) {
1633 dirfree( volume->v_dir );
1634 volume->v_dir = volume->v_root = NULL;
1637 volume->v_flags &= ~AFPVOL_OPEN;
1638 if (volume->v_cdb != NULL) {
1639 cnid_close(volume->v_cdb);
1640 volume->v_cdb = NULL;
1646 /* ------------------------- */
1647 static void closevol(struct vol *vol)
1652 dirfree( vol->v_root );
1654 if (vol->v_cdb != NULL) {
1655 cnid_close(vol->v_cdb);
1659 if (vol->v_postexec) {
1660 afprun(0, vol->v_postexec, NULL);
1662 if (vol->v_root_postexec) {
1663 afprun(1, vol->v_root_postexec, NULL);
1667 /* ------------------------- */
1668 void close_all_vol(void)
1672 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
1673 if ( ovol->v_flags & AFPVOL_OPEN ) {
1674 ovol->v_flags &= ~AFPVOL_OPEN;
1680 /* ------------------------- */
1681 int afp_closevol(obj, ibuf, ibuflen, rbuf, rbuflen )
1684 int ibuflen, *rbuflen;
1686 struct vol *vol, *ovol;
1691 memcpy(&vid, ibuf, sizeof( vid ));
1692 if (NULL == ( vol = getvolbyvid( vid )) ) {
1693 return( AFPERR_PARAM );
1696 vol->v_flags &= ~AFPVOL_OPEN;
1697 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
1698 if ( ovol->v_flags & AFPVOL_OPEN ) {
1702 if ( ovol != NULL ) {
1703 /* Even if chdir fails, we can't say afp_closevol fails. */
1704 if ( chdir( ovol->v_path ) == 0 ) {
1705 curdir = ovol->v_dir;
1710 if (vol->v_deleted) {
1711 showvol(vol->v_name);
1718 /* ------------------------- */
1719 struct vol *getvolbyvid(const u_int16_t vid )
1723 for ( vol = Volumes; vol; vol = vol->v_next ) {
1724 if ( vid == vol->v_vid ) {
1728 if ( vol == NULL || ( vol->v_flags & AFPVOL_OPEN ) == 0 ) {
1734 #endif /* FORCE_UIDGID */
1739 /* ------------------------ */
1740 static int ext_cmp_key(const void *key, const void *obj)
1742 const char *p = key;
1743 const struct extmap *em = obj;
1744 return strdiacasecmp(p, em->em_ext);
1746 struct extmap *getextmap(const char *path)
1751 if (NULL == ( p = strrchr( path, '.' )) ) {
1752 return( Defextmap );
1755 if (!*p || !Extmap_cnt) {
1756 return( Defextmap );
1758 em = bsearch(p, Extmap, Extmap_cnt, sizeof(struct extmap), ext_cmp_key);
1762 return( Defextmap );
1766 /* ------------------------- */
1767 struct extmap *getdefextmap(void)
1769 return( Defextmap );
1772 /* --------------------------
1773 poll if a volume is changed by other processes.
1775 int pollvoltime(obj)
1782 if (!(afp_version > 21 && obj->options.server_notif))
1785 if ( gettimeofday( &tv, 0 ) < 0 )
1788 for ( vol = Volumes; vol; vol = vol->v_next ) {
1789 if ( (vol->v_flags & AFPVOL_OPEN) && vol->v_time + 30 < tv.tv_sec) {
1790 if ( !stat( vol->v_path, &st ) && vol->v_time != st.st_mtime ) {
1791 vol->v_time = st.st_mtime;
1792 if (!obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED))
1801 /* ------------------------- */
1802 void setvoltime(obj, vol )
1808 /* just looking at vol->v_time is broken seriously since updates
1809 * from other users afpd processes never are seen.
1810 * This is not the most elegant solution (a shared memory between
1811 * the afpd processes would come closer)
1814 if ( gettimeofday( &tv, 0 ) < 0 ) {
1815 LOG(log_error, logtype_afpd, "setvoltime: gettimeofday: %s", strerror(errno) );
1818 if( utime( vol->v_path, NULL ) < 0 ) {
1819 /* write of time failed ... probably a read only filesys,
1820 * where no other users can interfere, so there's no issue here
1824 /* a little granularity */
1825 if (vol->v_time < tv.tv_sec) {
1826 vol->v_time = tv.tv_sec;
1827 if (afp_version > 21 && obj->options.server_notif) {
1828 obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED);
1833 /* ------------------------- */
1834 int afp_getvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1837 int ibuflen, *rbuflen;
1840 u_int16_t vid, bitmap;
1843 memcpy(&vid, ibuf, sizeof( vid ));
1844 ibuf += sizeof( vid );
1845 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1846 bitmap = ntohs( bitmap );
1848 if (NULL == ( vol = getvolbyvid( vid )) ) {
1850 return( AFPERR_PARAM );
1853 return stat_vol(bitmap, vol, rbuf, rbuflen);
1856 /* ------------------------- */
1857 int afp_setvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1860 int ibuflen, *rbuflen;
1864 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 );
1874 ibuf += sizeof(bitmap);
1876 if (( vol = getvolbyvid( vid )) == NULL ) {
1877 return( AFPERR_PARAM );
1880 if (vol->v_flags & AFPVOL_RO)
1881 return AFPERR_VLOCK;
1883 /* we can only set the backup date. */
1884 if (bitmap != (1 << VOLPBIT_BDATE))
1885 return AFPERR_BITMAP;
1887 ad_init(&ad, vol->v_adouble);
1888 if ( ad_open( vol->v_path, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR,
1891 return AFPERR_VLOCK;
1893 return AFPERR_ACCESS;
1896 memcpy(&aint, ibuf, sizeof(aint));
1897 ad_setdate(&ad, AD_DATE_BACKUP, aint);
1898 ad_flush(&ad, ADFLAGS_HF);
1899 ad_close(&ad, ADFLAGS_HF);
1903 /* ------------------------- */
1904 int wincheck(const struct vol *vol, const char *path)
1908 if (!(vol->v_flags & AFPVOL_MSWINDOWS))
1911 /* empty paths are not allowed */
1912 if ((len = strlen(path)) == 0)
1915 /* leading or trailing whitespaces are not allowed, carriage returns
1916 * and probably other whitespace is okay, tabs are not allowed
1918 if ((path[0] == ' ') || (path[len-1] == ' '))
1921 /* certain characters are not allowed */
1922 if (strpbrk(path, MSWINDOWS_BADCHARS))
1925 /* everything else is okay */
1931 * precreate a folder
1932 * this is only intended for folders in the volume root
1933 * It will *not* work if the folder name contains extended characters
1935 static int create_special_folder (const struct vol *vol, const struct _special_folder *folder)
1944 p = (char *) malloc ( strlen(vol->v_path)+strlen(folder->name)+2);
1946 LOG(log_error, logtype_afpd,"malloc failed");
1949 strcpy(p, vol->v_path);
1951 strcat(p, folder->name);
1953 if ( (ret = stat( p, &st )) < 0 ) {
1954 if (folder->precreate) {
1955 if (ad_mkdir(p, folder->mode & ~vol->v_umask)) {
1956 LOG(log_debug, logtype_afpd,"Creating '%s' failed", folder->name);
1964 if ( !ret && folder->hide) {
1966 ad_init(&ad, vol->v_adouble);
1967 if (ad_open( p, vol_noadouble(vol) | ADFLAGS_HF|ADFLAGS_DIR,
1968 O_RDWR|O_CREAT, 0666, &ad) < 0) {
1972 if ((ad_get_HF_flags( &ad ) & O_CREAT) ) {
1973 ad_setentrylen( &ad, ADEID_NAME, strlen(folder->name));
1974 memcpy(ad_entry( &ad, ADEID_NAME ), folder->name,
1975 ad_getentrylen( &ad, ADEID_NAME ));
1978 ad_getattr(&ad, &attr);
1979 attr |= htons( ntohs( attr ) | ATTRBIT_INVISIBLE );
1980 ad_setattr(&ad, attr);
1982 ad_flush( &ad, ADFLAGS_HF );
1983 ad_close( &ad, ADFLAGS_HF );
1989 static void handle_special_folders (const struct vol * vol)
1991 const _special_folder *p = &special_folders[0];
1993 if (vol->v_flags & AFPVOL_RO)
1996 for (; p->name != NULL; p++) {
1997 create_special_folder (vol, p);