2 * $Id: volume.c,v 1.51.2.7.2.11 2003-11-18 12:30:48 didg 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 * $i -> client ip/appletalk address without port
186 * $s -> server name (hostname if it doesn't exist)
187 * $u -> username (guest is usually nobody)
188 * $v -> volume name (ADEID_NAME or basename if ADEID_NAME is empty)
189 * $z -> zone (may not exist)
192 #define is_var(a, b) (strncmp((a), (b), 2) == 0)
194 static char *volxlate(AFPObj *obj, char *dest, size_t destlen,
195 char *src, struct passwd *pwd, char *path)
205 dest = calloc(destlen +1, 1);
211 strncpy(dest, src, destlen);
212 if ((p = strchr(src, '$')) == NULL) /* nothing to do */
215 /* first part of the path. just forward to the next variable. */
216 len = MIN(p - src, destlen);
222 while (p && destlen > 0) {
223 /* now figure out what the variable is */
225 if (is_var(p, "$b")) {
227 if ((q = strrchr(path, '/')) == NULL)
229 else if (*(q + 1) != '\0')
232 } else if (is_var(p, "$c")) {
233 if (obj->proto == AFPPROTO_ASP) {
234 ASP asp = obj->handle;
236 len = sprintf(dest, "%u.%u", ntohs(asp->asp_sat.sat_addr.s_net),
237 asp->asp_sat.sat_addr.s_node);
241 } else if (obj->proto == AFPPROTO_DSI) {
242 DSI *dsi = obj->handle;
244 len = sprintf(dest, "%s:%u", inet_ntoa(dsi->client.sin_addr),
245 ntohs(dsi->client.sin_port));
249 } else if (is_var(p, "$d")) {
251 } else if (is_var(p, "$f")) {
252 if ((q = strchr(pwd->pw_gecos, ',')))
255 } else if (is_var(p, "$g")) {
256 struct group *grp = getgrgid(pwd->pw_gid);
259 } else if (is_var(p, "$h")) {
260 q = obj->options.hostname;
261 } else if (is_var(p, "$i")) {
262 if (obj->proto == AFPPROTO_ASP) {
263 ASP asp = obj->handle;
265 len = sprintf(dest, "%u", ntohs(asp->asp_sat.sat_addr.s_net));
269 } else if (obj->proto == AFPPROTO_DSI) {
270 DSI *dsi = obj->handle;
272 q = inet_ntoa(dsi->client.sin_addr);
274 } else if (is_var(p, "$s")) {
277 else if (obj->options.server) {
278 q = obj->options.server;
280 q = obj->options.hostname;
281 } else if (is_var(p, "$u")) {
283 } else if (is_var(p, "$v")) {
288 if (ad_open(path, ADFLAGS_HF, O_RDONLY, 0, &ad) < 0)
291 if ((len = MIN(ad_getentrylen(&ad, ADEID_NAME), destlen)) > 0) {
292 memcpy(dest, ad_entry(&ad, ADEID_NAME), len);
293 ad_close(&ad, ADFLAGS_HF);
297 ad_close(&ad, ADFLAGS_HF);
298 no_volname: /* simple basename */
299 if ((q = strrchr(path, '/')) == NULL)
301 else if (*(q + 1) != '\0')
305 } else if (is_var(p, "$z")) {
307 } else if (is_var(p, "$$")) {
312 /* copy the stuff over. if we don't understand something that we
313 * should, just skip it over. */
315 len = MIN(p == q ? 2 : strlen(q), destlen);
316 strncpy(dest, q, len);
321 /* stuff up to next $ */
323 p = strchr(src, '$');
324 len = p ? MIN(p - src, destlen) : destlen;
326 strncpy(dest, src, len);
334 /* to make sure that val is valid, make sure to select an opt that
336 static int optionok(const char *buf, const char *opt, const char *val)
338 if (!strstr(buf,opt))
346 /* -------------------- */
347 static void setoption(struct vol_option *options, struct vol_option *save, int opt, const char *val)
349 if (options[opt].c_value && (!save || options[opt].c_value != save[opt].c_value))
350 free(options[opt].c_value);
351 options[opt].c_value = strdup(val + 1);
354 /* ------------------------------------------
355 handle all the options. tmp can't be NULL. */
356 static void volset(struct vol_option *options, struct vol_option *save,
357 char *volname, int vlen,
362 val = strchr(tmp, ':');
364 /* we'll assume it's a volume name. */
365 strncpy(volname, tmp, vlen);
370 LOG(log_debug, logtype_afpd, "Parsing volset %s", val);
372 if (optionok(tmp, "allow:", val)) {
373 setoption(options, save, VOLOPT_ALLOW, val);
375 } else if (optionok(tmp, "deny:", val)) {
376 setoption(options, save, VOLOPT_DENY, val);
378 } else if (optionok(tmp, "rwlist:", val)) {
379 setoption(options, save, VOLOPT_RWLIST, val);
381 } else if (optionok(tmp, "rolist:", val)) {
382 setoption(options, save, VOLOPT_ROLIST, val);
384 } else if (optionok(tmp, "codepage:", val)) {
385 LOG (log_error, logtype_afpd, "The old codepage system has been removed. Please make sure to read the documentation !!!!");
386 /* Make sure we don't screw anything */
388 } else if (optionok(tmp, "volcharset:", val)) {
389 setoption(options, save, VOLOPT_ENCODING, val);
390 } else if (optionok(tmp, "maccharset:", val)) {
391 setoption(options, save, VOLOPT_MACCHARSET, val);
392 } else if (optionok(tmp, "veto:", val)) {
393 setoption(options, save, VOLOPT_VETO, val);
394 } else if (optionok(tmp, "cnidscheme:", val)) {
395 setoption(options, save, VOLOPT_CNIDSCHEME, val);
396 } else if (optionok(tmp, "casefold:", val)) {
397 if (strcasecmp(val + 1, "tolower") == 0)
398 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMLOWER;
399 else if (strcasecmp(val + 1, "toupper") == 0)
400 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMUPPER;
401 else if (strcasecmp(val + 1, "xlatelower") == 0)
402 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UUPPERMLOWER;
403 else if (strcasecmp(val + 1, "xlateupper") == 0)
404 options[VOLOPT_CASEFOLD].i_value = AFPVOL_ULOWERMUPPER;
405 } else if (optionok(tmp, "adouble:", val)) {
406 if (strcasecmp(val + 1, "v1") == 0)
407 options[VOLOPT_ADOUBLE].i_value = AD_VERSION1;
408 #if AD_VERSION == AD_VERSION2
409 else if (strcasecmp(val + 1, "v2") == 0)
410 options[VOLOPT_ADOUBLE].i_value = AD_VERSION2;
412 } else if (optionok(tmp, "options:", val)) {
415 if ((p = strtok(val + 1, ",")) == NULL) /* nothing */
419 if (strcasecmp(p, "prodos") == 0)
420 options[VOLOPT_FLAGS].i_value |= AFPVOL_A2VOL;
421 else if (strcasecmp(p, "mswindows") == 0) {
422 options[VOLOPT_FLAGS].i_value |= AFPVOL_MSWINDOWS;
423 } else if (strcasecmp(p, "crlf") == 0)
424 options[VOLOPT_FLAGS].i_value |= AFPVOL_CRLF;
425 else if (strcasecmp(p, "noadouble") == 0)
426 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOADOUBLE;
427 else if (strcasecmp(p, "ro") == 0)
428 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
429 else if (strcasecmp(p, "nohex") == 0)
430 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOHEX;
431 else if (strcasecmp(p, "usedots") == 0)
432 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS;
433 else if (strcasecmp(p, "limitsize") == 0)
434 options[VOLOPT_FLAGS].i_value |= AFPVOL_LIMITSIZE;
435 /* support for either "dropbox" or "dropkludge" */
436 else if (strcasecmp(p, "dropbox") == 0)
437 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
438 else if (strcasecmp(p, "dropkludge") == 0)
439 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
440 else if (strcasecmp(p, "nofileid") == 0)
441 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOFILEID;
442 else if (strcasecmp(p, "nostat") == 0)
443 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOSTAT;
444 else if (strcasecmp(p, "preexec_close") == 0)
445 options[VOLOPT_PREEXEC].i_value = 1;
446 else if (strcasecmp(p, "root_preexec_close") == 0)
447 options[VOLOPT_ROOTPREEXEC].i_value = 1;
448 else if (strcasecmp(p, "upriv") == 0)
449 options[VOLOPT_FLAGS].i_value |= AFPVOL_UNIX_PRIV;
451 p = strtok(NULL, ",");
454 } else if (optionok(tmp, "dbpath:", val)) {
455 setoption(options, save, VOLOPT_DBPATH, val);
457 } else if (optionok(tmp, "umask:", val)) {
458 options[VOLOPT_UMASK].i_value = (int)strtol(val, (char **)NULL, 8);
459 } else if (optionok(tmp, "mapchars:",val)) {
460 setoption(options, save, VOLOPT_MAPCHARS, val);
462 } else if (optionok(tmp, "password:", val)) {
463 setoption(options, save, VOLOPT_PASSWORD, val);
467 /* this code allows forced uid/gid per volume settings */
468 } else if (optionok(tmp, "forceuid:", val)) {
469 setoption(options, save, VOLOPT_FORCEUID, val);
470 } else if (optionok(tmp, "forcegid:", val)) {
471 setoption(options, save, VOLOPT_FORCEGID, val);
473 #endif /* FORCE_UIDGID */
474 } else if (optionok(tmp, "root_preexec:", val)) {
475 setoption(options, save, VOLOPT_ROOTPREEXEC, val);
477 } else if (optionok(tmp, "preexec:", val)) {
478 setoption(options, save, VOLOPT_PREEXEC, val);
480 } else if (optionok(tmp, "root_postexec:", val)) {
481 setoption(options, save, VOLOPT_ROOTPOSTEXEC, val);
483 } else if (optionok(tmp, "postexec:", val)) {
484 setoption(options, save, VOLOPT_POSTEXEC, val);
487 /* ignore unknown options */
488 LOG(log_debug, logtype_afpd, "ignoring unknown volume option: %s", tmp);
493 /* ----------------- */
494 static void showvol(const ucs2_t *name)
497 for ( volume = Volumes; volume; volume = volume->v_next ) {
498 if ( !strcasecmp_w( volume->v_name, name ) && volume->v_hide) {
505 /* ------------------------------- */
506 static int creatvol(AFPObj *obj, struct passwd *pwd,
507 char *path, char *name,
508 struct vol_option *options,
509 const int user /* user defined volume */
517 if ( name == NULL || *name == '\0' ) {
518 if ((name = strrchr( path, '/' )) == NULL) {
519 return -1; /* Obviously not a fully qualified path */
522 /* if you wish to share /, you need to specify a name. */
527 vlen = strlen( name );
528 if ( vlen > AFPVOL_NAMELEN ) {
529 vlen = AFPVOL_NAMELEN;
530 name[AFPVOL_NAMELEN] = '\0';
533 /* convert name to UCS2 first */
534 if ( 0 >= ( vlen = convert_string(obj->options.unixcharset, CH_UCS2, name, vlen, tmpname, 512)) )
537 for ( volume = Volumes; volume; volume = volume->v_next ) {
538 if ( strcasecmp_w( volume->v_name, tmpname ) == 0 ) {
539 if (volume->v_deleted) {
543 return -1; /* Won't be able to access it, anyway... */
549 if (!( volume = (struct vol *)calloc(1, sizeof( struct vol ))) ) {
550 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
553 if ( NULL == ( volume->v_name = strdup_w(tmpname))) {
554 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
558 if (!( volume->v_path = (char *)malloc( strlen( path ) + 1 )) ) {
559 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
560 free(volume->v_name);
564 volume->v_hide = hide;
565 strcpy( volume->v_path, path );
569 #endif /* __svr4__ */
570 /* os X start at 1 and use network order ie. 1 2 3 */
571 volume->v_vid = ++lastvid;
572 volume->v_vid = htons(volume->v_vid);
576 /* should we casefold? */
577 volume->v_casefold = options[VOLOPT_CASEFOLD].i_value;
579 /* shift in some flags */
580 volume->v_flags = options[VOLOPT_FLAGS].i_value;
582 if (options[VOLOPT_PASSWORD].c_value)
583 volume->v_password = strdup(options[VOLOPT_PASSWORD].c_value);
585 if (options[VOLOPT_VETO].c_value)
586 volume->v_veto = strdup(options[VOLOPT_VETO].c_value);
588 if (options[VOLOPT_ENCODING].c_value)
589 volume->v_volcodepage = strdup(options[VOLOPT_ENCODING].c_value);
591 if (options[VOLOPT_MACCHARSET].c_value)
592 volume->v_maccodepage = strdup(options[VOLOPT_MACCHARSET].c_value);
594 if (options[VOLOPT_DBPATH].c_value)
595 volume->v_dbpath = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_DBPATH].c_value, pwd, path);
597 if (options[VOLOPT_CNIDSCHEME].c_value)
598 volume->v_cnidscheme = strdup(options[VOLOPT_CNIDSCHEME].c_value);
600 if (options[VOLOPT_UMASK].i_value)
601 volume->v_umask = (mode_t)options[VOLOPT_UMASK].i_value;
603 if (options[VOLOPT_ADOUBLE].i_value)
604 volume->v_adouble = options[VOLOPT_ADOUBLE].i_value;
606 volume->v_adouble = AD_VERSION;
608 if (options[VOLOPT_FORCEUID].c_value) {
609 volume->v_forceuid = strdup(options[VOLOPT_FORCEUID].c_value);
611 volume->v_forceuid = NULL; /* set as null so as to return 0 later on */
614 if (options[VOLOPT_FORCEGID].c_value) {
615 volume->v_forcegid = strdup(options[VOLOPT_FORCEGID].c_value);
617 volume->v_forcegid = NULL; /* set as null so as to return 0 later on */
621 if (options[VOLOPT_PREEXEC].c_value)
622 volume->v_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_PREEXEC].c_value, pwd, path);
623 volume->v_preexec_close = options[VOLOPT_PREEXEC].i_value;
625 if (options[VOLOPT_POSTEXEC].c_value)
626 volume->v_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_POSTEXEC].c_value, pwd, path);
628 if (options[VOLOPT_ROOTPREEXEC].c_value)
629 volume->v_root_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPREEXEC].c_value, pwd, path);
630 volume->v_root_preexec_close = options[VOLOPT_ROOTPREEXEC].i_value;
632 if (options[VOLOPT_ROOTPOSTEXEC].c_value)
633 volume->v_root_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPOSTEXEC].c_value, pwd, path);
637 volume->v_next = Volumes;
642 /* ---------------- */
643 static char *myfgets( buf, size, fp )
652 while ((EOF != ( c = getc( fp )) ) && ( size > 0 )) {
653 if ( c == '\n' || c == '\r' ) {
671 /* check access list. this function wants something of the following
673 * @group,name,name2,@group2,name3
675 * a NULL argument allows everybody to have access.
676 * we return three things:
678 * 0: list exists, but name isn't in it
682 #ifndef NO_REAL_USER_NAME
683 /* authentication is case insensitive
684 * FIXME should we do the same with group name?
686 #define access_strcmp strcasecmp
689 #define access_strcmp strcmp
693 static int accessvol(args, name)
697 char buf[MAXPATHLEN + 1], *p;
703 strncpy(buf, args, sizeof(buf));
704 if ((p = strtok(buf, ",")) == NULL) /* nothing, return okay */
708 if (*p == '@') { /* it's a group */
709 if ((gr = getgrnam(p + 1)) && gmem(gr->gr_gid))
711 } else if (access_strcmp(p, name) == 0) /* it's a user name */
713 p = strtok(NULL, ",");
719 static void setextmap( ext, type, creator, user)
720 char *ext, *type, *creator;
726 if (Extmap == NULL) {
727 if (( Extmap = calloc(1, sizeof( struct extmap ))) == NULL ) {
728 LOG(log_error, logtype_afpd, "setextmap: calloc: %s", strerror(errno) );
733 for ( em = Extmap, cnt = 0; em->em_ext; em++, cnt++) {
734 if ( (strdiacasecmp( em->em_ext, ext )) == 0 ) {
739 if ( em->em_ext == NULL ) {
740 if (!(Extmap = realloc( Extmap, sizeof( struct extmap ) * (cnt +2))) ) {
741 LOG(log_error, logtype_afpd, "setextmap: realloc: %s", strerror(errno) );
744 (Extmap +cnt +1)->em_ext = NULL;
746 } else if ( !user ) {
752 if (!(em->em_ext = strdup( ext))) {
753 LOG(log_error, logtype_afpd, "setextmap: strdup: %s", strerror(errno) );
757 if ( *type == '\0' ) {
758 memcpy(em->em_type, "????", sizeof( em->em_type ));
760 memcpy(em->em_type, type, sizeof( em->em_type ));
762 if ( *creator == '\0' ) {
763 memcpy(em->em_creator, "UNIX", sizeof( em->em_creator ));
765 memcpy(em->em_creator, creator, sizeof( em->em_creator ));
769 /* -------------------------- */
770 static int extmap_cmp(const void *map1, const void *map2)
772 const struct extmap *em1 = map1;
773 const struct extmap *em2 = map2;
774 return strdiacasecmp(em1->em_ext, em2->em_ext);
777 static void sortextmap( void)
782 if ((em = Extmap) == NULL) {
790 qsort(Extmap, Extmap_cnt, sizeof(struct extmap), extmap_cmp);
795 /* ----------------------
797 static void free_extmap( void)
807 /* ----------------------
809 static int volfile_changed(struct afp_volume_name *p)
819 if (!stat( name, &st) && st.st_mtime > p->mtime) {
820 p->mtime = st.st_mtime;
826 /* ----------------------
827 * Read a volume configuration file and add the volumes contained within to
828 * the global volume list. If p2 is non-NULL, the file that is opened is
831 * Lines that begin with # and blank lines are ignored.
832 * Volume lines are of the form:
833 * <unix path> [<volume name>] [allow:<user>,<@group>,...] \
834 * [codepage:<file>] [casefold:<num>]
835 * <extension> TYPE [CREATOR]
837 static int readvolfile(obj, p1, p2, user, pwent)
839 struct afp_volume_name *p1;
842 struct passwd *pwent;
845 char path[ MAXPATHLEN + 1], tmp[ MAXPATHLEN + 1],
846 volname[ AFPVOL_NAMELEN + 1 ], buf[ BUFSIZ ],
847 type[ 5 ], creator[ 5 ];
850 struct vol_option options[VOLOPT_NUM], save_options[VOLOPT_NUM];
858 strcpy( path, p1->name );
865 p1->full_name = strdup(path);
868 if (NULL == ( fp = fopen( path, "r" )) ) {
872 if (fd != -1 && !fstat( fd, &st) ) {
873 p1->mtime = st.st_mtime;
876 memset(save_options, 0, sizeof(save_options));
877 while ( myfgets( buf, sizeof( buf ), fp ) != NULL ) {
878 initline( strlen( buf ), buf );
879 parseline( sizeof( path ) - 1, path );
886 /* change the default options for this file */
887 if (strncmp(path, VOLOPT_DEFAULT, VOLOPT_DEFAULT_LEN) == 0) {
889 for (i = 0; i < VOLOPT_NUM; i++) {
890 if (parseline( sizeof( path ) - VOLOPT_DEFAULT_LEN - 1,
891 path + VOLOPT_DEFAULT_LEN) < 0)
893 volset(save_options, NULL, tmp, sizeof(tmp) - 1,
894 path + VOLOPT_DEFAULT_LEN);
900 if (( p = strchr( path, '/' )) != NULL ) {
908 if ( u == NULL || *u == '\0' || ( pw = getpwnam( u )) == NULL ) {
911 strcpy( tmp, pw->pw_dir );
912 if ( p != NULL && *p != '\0' ) {
916 /* Tag a user's home directory with their umask. Note, this will
917 * be overwritten if the user actually specifies a umask: option
918 * for a '~' volume. */
919 save_options[VOLOPT_UMASK].i_value = obj->options.save_mask;
923 /* send path through variable substitution */
924 if (*path != '~') /* need to copy path to tmp */
927 pwent = getpwnam(obj->username);
928 volxlate(obj, path, sizeof(path) - 1, tmp, pwent, NULL);
930 /* this is sort of braindead. basically, i want to be
931 * able to specify things in any order, but i don't want to
932 * re-write everything.
934 * currently we have options:
942 * options:prodos,crlf,noadouble,ro...
947 * namemask:x,y,!z (not implemented yet)
949 memcpy(options, save_options, sizeof(options));
952 /* read in up to VOLOP_NUM possible options */
953 for (i = 0; i < VOLOPT_NUM; i++) {
954 if (parseline( sizeof( tmp ) - 1, tmp ) < 0)
957 volset(options, save_options, volname, sizeof(volname) - 1, tmp);
960 /* check allow/deny lists:
961 allow -> either no list (-1), or in list (1)
962 deny -> either no list (-1), or not in list (0) */
963 if (accessvol(options[VOLOPT_ALLOW].c_value, obj->username) &&
964 (accessvol(options[VOLOPT_DENY].c_value, obj->username) < 1)) {
966 /* handle read-only behaviour. semantics:
967 * 1) neither the rolist nor the rwlist exist -> rw
968 * 2) rolist exists -> ro if user is in it.
969 * 3) rwlist exists -> ro unless user is in it. */
970 if (((options[VOLOPT_FLAGS].i_value & AFPVOL_RO) == 0) &&
971 ((accessvol(options[VOLOPT_ROLIST].c_value,
972 obj->username) == 1) ||
973 !accessvol(options[VOLOPT_RWLIST].c_value,
975 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
977 /* do variable substitution for volname */
978 volxlate(obj, tmp, sizeof(tmp) - 1, volname, pwent, path);
979 creatvol(obj, pwent, path, tmp, options, p2 != NULL);
981 volfree(options, save_options);
985 parseline( sizeof( type ) - 1, type );
986 parseline( sizeof( creator ) - 1, creator );
987 setextmap( path, type, creator, user);
994 volfree(save_options, NULL);
996 if ( fclose( fp ) != 0 ) {
997 LOG(log_error, logtype_afpd, "readvolfile: fclose: %s", strerror(errno) );
1003 /* ------------------------------- */
1004 static void volume_free(struct vol *vol)
1009 free(vol->v_password);
1011 free(vol->v_volcodepage);
1012 free(vol->v_maccodepage);
1013 free(vol->v_dbpath);
1015 free(vol->v_forceuid);
1016 free(vol->v_forcegid);
1017 #endif /* FORCE_UIDGID */
1020 /* ------------------------------- */
1021 static void free_volumes(void )
1024 struct vol *nvol, *ovol;
1026 for ( vol = Volumes; vol; vol = vol->v_next ) {
1027 if (( vol->v_flags & AFPVOL_OPEN ) ) {
1034 for ( vol = Volumes, ovol = NULL; vol; vol = nvol) {
1037 if (vol->v_name == NULL) {
1038 if (Volumes == vol) {
1045 ovol->v_next = nvol;
1055 /* ------------------------------- */
1056 static void volume_unlink(struct vol *volume)
1058 struct vol *vol, *ovol, *nvol;
1060 if (volume == Volumes) {
1061 Volumes = Volumes->v_next;
1064 for ( vol = Volumes, ovol = NULL; vol; vol = nvol) {
1067 if (vol == volume) {
1072 ovol->v_next = nvol;
1083 static int getvolspace( vol, bfree, btotal, xbfree, xbtotal, bsize )
1085 u_int32_t *bfree, *btotal, *bsize;
1086 VolSpace *xbfree, *xbtotal;
1090 #ifndef NO_QUOTA_SUPPORT
1091 VolSpace qfree, qtotal;
1094 spaceflag = AFPVOL_GVSMASK & vol->v_flags;
1095 /* report up to 2GB if afp version is < 2.2 (4GB if not) */
1096 maxsize = (vol->v_flags & AFPVOL_A2VOL) ? 0x01fffe00 :
1097 (((afp_version < 22) || (vol->v_flags & AFPVOL_LIMITSIZE))
1098 ? 0x7fffffffL : 0xffffffffL);
1101 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_AFSGVS ) {
1102 if ( afs_getvolspace( vol, xbfree, xbtotal, bsize ) == AFP_OK ) {
1103 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_AFSGVS;
1104 goto getvolspace_done;
1109 if (( rc = ustatfs_getvolspace( vol, xbfree, xbtotal,
1110 bsize)) != AFP_OK ) {
1114 #define min(a,b) ((a)<(b)?(a):(b))
1115 #ifndef NO_QUOTA_SUPPORT
1116 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_UQUOTA ) {
1117 if ( uquota_getvolspace( vol, &qfree, &qtotal, *bsize ) == AFP_OK ) {
1118 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_UQUOTA;
1119 *xbfree = min(*xbfree, qfree);
1120 *xbtotal = min( *xbtotal, qtotal);
1121 goto getvolspace_done;
1125 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_USTATFS;
1128 *bfree = min( *xbfree, maxsize);
1129 *btotal = min( *xbtotal, maxsize);
1133 /* -----------------------
1134 * set volume creation date
1135 * avoid duplicate, well at least it tries
1137 static void vol_setdate(u_int16_t id, struct adouble *adp, time_t date)
1140 struct vol *vol = Volumes;
1142 for ( volume = Volumes; volume; volume = volume->v_next ) {
1143 if (volume->v_vid == id) {
1146 else if (AD_DATE_FROM_UNIX(date) == volume->v_ctime) {
1148 volume = Volumes; /* restart */
1151 vol->v_ctime = AD_DATE_FROM_UNIX(date);
1152 ad_setdate(adp, AD_DATE_CREATE | AD_DATE_UNIX, date);
1155 /* ----------------------- */
1156 static int getvolparams( bitmap, vol, st, buf, buflen )
1164 int bit = 0, isad = 1;
1167 u_int32_t bfree, btotal, bsize;
1168 VolSpace xbfree, xbtotal; /* extended bytes */
1169 char *data, *nameoff = NULL;
1172 /* courtesy of jallison@whistle.com:
1173 * For MacOS8.x support we need to create the
1174 * .Parent file here if it doesn't exist. */
1176 ad_init(&ad, vol->v_adouble);
1177 if ( ad_open( vol->v_path, vol_noadouble(vol) |
1178 ADFLAGS_HF|ADFLAGS_DIR, O_RDWR | O_CREAT,
1181 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1183 } else if (ad_get_HF_flags( &ad ) & O_CREAT) {
1184 slash = strrchr( vol->v_path, '/' );
1188 slash = vol->v_path;
1190 ad_setentrylen( &ad, ADEID_NAME, strlen( slash ));
1191 memcpy(ad_entry( &ad, ADEID_NAME ), slash,
1192 ad_getentrylen( &ad, ADEID_NAME ));
1193 vol_setdate(vol->v_vid, &ad, st->st_mtime);
1194 ad_flush(&ad, ADFLAGS_HF);
1197 if (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0)
1198 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1200 vol->v_ctime = aint;
1203 if (( bitmap & ( (1<<VOLPBIT_BFREE)|(1<<VOLPBIT_BTOTAL) |
1204 (1<<VOLPBIT_XBFREE)|(1<<VOLPBIT_XBTOTAL) |
1205 (1<<VOLPBIT_BSIZE)) ) != 0 ) {
1206 if ( getvolspace( vol, &bfree, &btotal, &xbfree, &xbtotal,
1209 ad_close( &ad, ADFLAGS_HF );
1211 return( AFPERR_PARAM );
1216 while ( bitmap != 0 ) {
1217 while (( bitmap & 1 ) == 0 ) {
1225 if (0 == (vol->v_flags & AFPVOL_NOFILEID) && vol->v_cdb != NULL &&
1226 (vol->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1227 ashort = VOLPBIT_ATTR_FILEID;
1229 /* check for read-only.
1230 * NOTE: we don't actually set the read-only flag unless
1231 * it's passed in that way as it's possible to mount
1232 * a read-write filesystem under a read-only one. */
1233 if ((vol->v_flags & AFPVOL_RO) ||
1234 ((utime(vol->v_path, NULL) < 0) && (errno == EROFS))) {
1235 ashort |= VOLPBIT_ATTR_RO;
1237 ashort |= VOLPBIT_ATTR_CATSEARCH;
1238 if (afp_version >= 30) {
1239 ashort |= VOLPBIT_ATTR_UTF8;
1240 if (vol->v_flags & AFPVOL_UNIX_PRIV)
1241 ashort |= VOLPBIT_ATTR_UNIXPRIV;
1243 ashort = htons(ashort);
1244 memcpy(data, &ashort, sizeof( ashort ));
1245 data += sizeof( ashort );
1249 ashort = htons( AFPVOLSIG_DEFAULT );
1250 memcpy(data, &ashort, sizeof( ashort ));
1251 data += sizeof( ashort );
1254 case VOLPBIT_CDATE :
1255 aint = vol->v_ctime;
1256 memcpy(data, &aint, sizeof( aint ));
1257 data += sizeof( aint );
1260 case VOLPBIT_MDATE :
1261 if ( st->st_mtime > vol->v_mtime ) {
1262 vol->v_mtime = st->st_mtime;
1264 aint = AD_DATE_FROM_UNIX(vol->v_mtime);
1265 memcpy(data, &aint, sizeof( aint ));
1266 data += sizeof( aint );
1269 case VOLPBIT_BDATE :
1270 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1271 aint = AD_DATE_START;
1272 memcpy(data, &aint, sizeof( aint ));
1273 data += sizeof( aint );
1277 memcpy(data, &vol->v_vid, sizeof( vol->v_vid ));
1278 data += sizeof( vol->v_vid );
1281 case VOLPBIT_BFREE :
1282 bfree = htonl( bfree );
1283 memcpy(data, &bfree, sizeof( bfree ));
1284 data += sizeof( bfree );
1287 case VOLPBIT_BTOTAL :
1288 btotal = htonl( btotal );
1289 memcpy(data, &btotal, sizeof( btotal ));
1290 data += sizeof( btotal );
1293 #ifndef NO_LARGE_VOL_SUPPORT
1294 case VOLPBIT_XBFREE :
1295 xbfree = hton64( xbfree );
1296 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1297 bcopy(&xbfree, data, sizeof(xbfree));
1298 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1299 memcpy(data, &xbfree, sizeof( xbfree ));
1300 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1301 data += sizeof( xbfree );
1304 case VOLPBIT_XBTOTAL :
1305 xbtotal = hton64( xbtotal );
1306 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1307 bcopy(&xbtotal, data, sizeof(xbtotal));
1308 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1309 memcpy(data, &xbtotal, sizeof( xbtotal ));
1310 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1311 data += sizeof( xbfree );
1313 #endif /* ! NO_LARGE_VOL_SUPPORT */
1317 data += sizeof( u_int16_t );
1320 case VOLPBIT_BSIZE: /* block size */
1321 bsize = htonl(bsize);
1322 memcpy(data, &bsize, sizeof(bsize));
1323 data += sizeof(bsize);
1328 ad_close( &ad, ADFLAGS_HF );
1330 return( AFPERR_BITMAP );
1336 ashort = htons( data - buf );
1337 memcpy(nameoff, &ashort, sizeof( ashort ));
1338 aint = ucs2_to_charset( (utf8_encoding()?CH_UTF8_MAC:vol->v_maccharset), vol->v_name, data+1, 255);
1348 ad_close( &ad, ADFLAGS_HF );
1350 *buflen = data - buf;
1354 /* ------------------------- */
1355 int static stat_vol(u_int16_t bitmap, struct vol *vol, char *rbuf, int *rbuflen)
1360 if ( stat( vol->v_path, &st ) < 0 ) {
1362 return( AFPERR_PARAM );
1365 buflen = *rbuflen - sizeof( bitmap );
1366 if (( ret = getvolparams(bitmap, vol, &st,
1367 rbuf + sizeof( bitmap ), &buflen )) != AFP_OK ) {
1371 *rbuflen = buflen + sizeof( bitmap );
1372 bitmap = htons( bitmap );
1373 memcpy(rbuf, &bitmap, sizeof( bitmap ));
1378 /* ------------------------------- */
1379 void load_volumes(AFPObj *obj)
1381 struct passwd *pwent;
1386 /* check files date */
1387 if (obj->options.defaultvol.loaded) {
1388 changed = volfile_changed(&obj->options.defaultvol);
1390 if (obj->options.systemvol.loaded) {
1391 changed |= volfile_changed(&obj->options.systemvol);
1393 if (obj->options.uservol.loaded) {
1394 changed |= volfile_changed(&obj->options.uservol);
1403 pwent = getpwnam(obj->username);
1404 if ( (obj->options.flags & OPTION_USERVOLFIRST) == 0 ) {
1405 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent);
1408 if ((*obj->username == '\0') || (obj->options.flags & OPTION_NOUSERVOL)) {
1409 readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent);
1412 * Read user's AppleVolumes or .AppleVolumes file
1413 * If neither are readable, read the default volumes file. if
1414 * that doesn't work, create a user share.
1416 obj->options.uservol.name = strdup(pwent->pw_dir);
1417 if ( readvolfile(obj, &obj->options.uservol, "AppleVolumes", 1, pwent) < 0 &&
1418 readvolfile(obj, &obj->options.uservol, ".AppleVolumes", 1, pwent) < 0 &&
1419 readvolfile(obj, &obj->options.uservol, "applevolumes", 1, pwent) < 0 &&
1420 readvolfile(obj, &obj->options.uservol, ".applevolumes", 1, pwent) < 0 &&
1421 obj->options.defaultvol.name != NULL ) {
1422 if (readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent) < 0)
1423 creatvol(obj, pwent, pwent->pw_dir, NULL, NULL, 1);
1426 if ( obj->options.flags & OPTION_USERVOLFIRST ) {
1427 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent );
1431 /* ------------------------------- */
1432 int afp_getsrvrparms(obj, ibuf, ibuflen, rbuf, rbuflen )
1435 int ibuflen, *rbuflen;
1447 for ( vcnt = 0, volume = Volumes; volume; volume = volume->v_next ) {
1448 if (!(volume->v_flags & AFPVOL_NOSTAT)) {
1449 if ( stat( volume->v_path, &st ) < 0 ) {
1450 LOG(log_info, logtype_afpd, "afp_getsrvrparms: stat %s: %s",
1451 volume->v_path, strerror(errno) );
1452 continue; /* can't access directory */
1454 if (!S_ISDIR(st.st_mode)) {
1455 continue; /* not a dir */
1458 if (volume->v_hide) {
1459 continue; /* config file changed but the volume was mounted */
1461 /* set password bit if there's a volume password */
1462 *data = (volume->v_password) ? AFPSRVR_PASSWD : 0;
1464 /* Apple 2 clients running ProDOS-8 expect one volume to have
1465 bit 0 of this byte set. They will not recognize anything
1466 on the server unless this is the case. I have not
1467 completely worked this out, but it's related to booting
1468 from the server. Support for that function is a ways
1469 off.. <shirsch@ibm.net> */
1470 *data |= (volume->v_flags & AFPVOL_A2VOL) ? AFPSRVR_CONFIGINFO : 0;
1471 *data++ |= 0; /* UNIX PRIVS BIT ..., OSX doesn't seem to use it, so we don't either */
1473 len = ucs2_to_charset_allocate((utf8_encoding()?CH_UTF8_MAC:obj->options.maccharset),
1474 &namebuf, volume->v_name);
1478 memcpy(data, namebuf, len );
1484 *rbuflen = data - rbuf;
1486 if ( gettimeofday( &tv, 0 ) < 0 ) {
1487 LOG(log_error, logtype_afpd, "afp_getsrvrparms: gettimeofday: %s", strerror(errno) );
1489 return AFPERR_PARAM;
1491 tv.tv_sec = AD_DATE_FROM_UNIX(tv.tv_sec);
1492 memcpy(data, &tv.tv_sec, sizeof( u_int32_t));
1493 data += sizeof( u_int32_t);
1498 /* -------------------------
1499 * we are the user here
1501 int afp_openvol(obj, ibuf, ibuflen, rbuf, rbuflen )
1504 int ibuflen, *rbuflen;
1516 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1517 bitmap = ntohs( bitmap );
1518 ibuf += sizeof( bitmap );
1519 if (( bitmap & (1<<VOLPBIT_VID)) == 0 ) {
1521 return AFPERR_BITMAP;
1524 len = (unsigned char)*ibuf++;
1525 volname = obj->oldtmp;
1526 namelen = convert_string( (utf8_encoding()?CH_UTF8_MAC:obj->options.maccharset), CH_UCS2,
1527 ibuf, len, volname, sizeof(obj->oldtmp));
1530 return AFPERR_PARAM;
1534 if ((len + 1) & 1) /* pad to an even boundary */
1539 for ( volume = Volumes; volume; volume = volume->v_next ) {
1540 if ( strcasecmp_w( (ucs2_t*) volname, volume->v_name ) == 0 ) {
1545 if ( volume == NULL ) {
1547 return AFPERR_PARAM;
1550 /* check for a volume password */
1551 if (volume->v_password && strncmp(ibuf, volume->v_password, VOLPASSLEN)) {
1553 return AFPERR_ACCESS;
1556 if (( volume->v_flags & AFPVOL_OPEN ) ) {
1557 /* the volume is already open */
1559 set_uidgid ( volume );
1561 return stat_vol(bitmap, volume, rbuf, rbuflen);
1564 /* initialize volume variables
1567 if (afp_version >= 30) {
1568 volume->max_filename = 255;
1571 volume->max_filename = MACFILELEN;
1574 volume->v_dir = volume->v_root = NULL;
1576 /* FIXME unix name != mac name */
1580 len = convert_string_allocate( CH_UCS2, (utf8_encoding()?CH_UTF8_MAC:obj->options.maccharset),
1581 volume->v_name, namelen, &vol_mname);
1582 if ( !vol_mname || len <= 0) {
1587 len = convert_string_allocate( CH_UCS2 , volume->v_volcharset, volume->v_name, namelen, &vol_uname);
1589 if ( !vol_uname || len <= 0) {
1595 if ((dir = dirnew(vol_mname, vol_uname) ) == NULL) {
1598 LOG(log_error, logtype_afpd, "afp_openvol: malloc: %s", strerror(errno) );
1605 dir->d_did = DIRDID_ROOT;
1606 dir->d_color = DIRTREE_COLOR_BLACK; /* root node is black */
1607 volume->v_dir = volume->v_root = dir;
1608 volume->v_flags |= AFPVOL_OPEN;
1609 volume->v_cdb = NULL;
1611 if (volume->v_root_preexec) {
1612 if ((ret = afprun(1, volume->v_root_preexec, NULL)) && volume->v_root_preexec_close) {
1613 LOG(log_error, logtype_afpd, "afp_openvol: root preexec : %d", ret );
1620 set_uidgid ( volume );
1623 if (volume->v_preexec) {
1624 if ((ret = afprun(0, volume->v_preexec, NULL)) && volume->v_preexec_close) {
1625 LOG(log_error, logtype_afpd, "afp_openvol: preexec : %d", ret );
1631 if ( stat( volume->v_path, &st ) < 0 ) {
1636 if ( chdir( volume->v_path ) < 0 ) {
1640 curdir = volume->v_dir;
1641 if (volume->v_cnidscheme == NULL) {
1642 volume->v_cnidscheme = strdup(DEFAULT_CNID_SCHEME);
1643 LOG(log_warning, logtype_afpd, "Warning: No CNID scheme for volume %s. Using default.",
1646 if (volume->v_dbpath)
1647 volume->v_cdb = cnid_open (volume->v_dbpath, volume->v_umask, volume->v_cnidscheme);
1648 if (volume->v_cdb == NULL)
1649 volume->v_cdb = cnid_open (volume->v_path, volume->v_umask, volume->v_cnidscheme);
1650 if (volume->v_cdb == NULL) {
1651 volume->v_cdb = cnid_open (volume->v_path, volume->v_umask, DEFAULT_CNID_SCHEME);
1652 LOG(log_error, logtype_afpd, "Error: invalid CNID backend for %s: %s", volume->v_path,
1653 volume->v_cnidscheme);
1655 if (volume->v_cdb == NULL) {
1656 LOG(log_error, logtype_afpd, "Fatal error: cannot open CNID for %s", volume->v_path);
1663 if (!volume->v_volcodepage)
1664 volume->v_volcodepage = strdup("UTF8");
1666 if ( (charset_t) -1 == ( volume->v_volcharset = add_charset(volume->v_volcodepage)) ) {
1667 LOG (log_error, logtype_afpd, "Setting codepage %s as volume codepage failed", volume->v_volcodepage);
1672 if ( NULL == ( volume->v_vol = find_charset_functions(volume->v_volcodepage)) || volume->v_vol->flags & CHARSET_ICONV ) {
1673 LOG (log_warning, logtype_afpd, "WARNING: volume encoding %s is *not* supported by netatalk, expect problems !!!!", volume->v_volcodepage);
1676 if (!volume->v_maccodepage)
1677 volume->v_maccodepage = strdup(obj->options.maccodepage);
1679 if ( (charset_t) -1 == ( volume->v_maccharset = add_charset(volume->v_maccodepage)) ) {
1680 LOG (log_error, logtype_afpd, "Setting codepage %s as mac codepage failed", volume->v_maccodepage);
1685 if ( NULL == ( volume->v_mac = find_charset_functions(volume->v_maccodepage)) || ! (volume->v_mac->flags & CHARSET_CLIENT) ) {
1686 LOG (log_error, logtype_afpd, "Fatal error: mac charset %s not supported", volume->v_maccodepage);
1691 ret = stat_vol(bitmap, volume, rbuf, rbuflen);
1692 if (ret == AFP_OK) {
1694 handle_special_folders( volume );
1697 * If you mount a volume twice, the second time the trash appears on
1698 * the desk-top. That's because the Mac remembers the DID for the
1699 * trash (even for volumes in different zones, on different servers).
1700 * Just so this works better, we prime the DID cache with the trash,
1701 * fixing the trash at DID 17.
1702 * FIXME (RL): should it be done inside a CNID backend ? (always returning Trash DID when asked) ?
1704 if ((volume->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1706 /* FIXME find db time stamp */
1707 cnid_getstamp(volume->v_cdb, volume->v_stamp, sizeof(volume->v_stamp));
1711 cname( volume, volume->v_dir, &p );
1717 if (volume->v_dir) {
1718 dirfree( volume->v_dir );
1719 volume->v_dir = volume->v_root = NULL;
1722 volume->v_flags &= ~AFPVOL_OPEN;
1723 if (volume->v_cdb != NULL) {
1724 cnid_close(volume->v_cdb);
1725 volume->v_cdb = NULL;
1731 /* ------------------------- */
1732 static void closevol(struct vol *vol)
1737 dirfree( vol->v_root );
1739 if (vol->v_cdb != NULL) {
1740 cnid_close(vol->v_cdb);
1744 if (vol->v_postexec) {
1745 afprun(0, vol->v_postexec, NULL);
1747 if (vol->v_root_postexec) {
1748 afprun(1, vol->v_root_postexec, NULL);
1752 /* ------------------------- */
1753 void close_all_vol(void)
1757 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
1758 if ( ovol->v_flags & AFPVOL_OPEN ) {
1759 ovol->v_flags &= ~AFPVOL_OPEN;
1765 /* ------------------------- */
1766 int afp_closevol(obj, ibuf, ibuflen, rbuf, rbuflen )
1769 int ibuflen, *rbuflen;
1771 struct vol *vol, *ovol;
1776 memcpy(&vid, ibuf, sizeof( vid ));
1777 if (NULL == ( vol = getvolbyvid( vid )) ) {
1778 return( AFPERR_PARAM );
1781 vol->v_flags &= ~AFPVOL_OPEN;
1782 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
1783 if ( ovol->v_flags & AFPVOL_OPEN ) {
1787 if ( ovol != NULL ) {
1788 /* Even if chdir fails, we can't say afp_closevol fails. */
1789 if ( chdir( ovol->v_path ) == 0 ) {
1790 curdir = ovol->v_dir;
1795 if (vol->v_deleted) {
1796 showvol(vol->v_name);
1803 /* ------------------------- */
1804 struct vol *getvolbyvid(const u_int16_t vid )
1808 for ( vol = Volumes; vol; vol = vol->v_next ) {
1809 if ( vid == vol->v_vid ) {
1813 if ( vol == NULL || ( vol->v_flags & AFPVOL_OPEN ) == 0 ) {
1819 #endif /* FORCE_UIDGID */
1824 /* ------------------------ */
1825 static int ext_cmp_key(const void *key, const void *obj)
1827 const char *p = key;
1828 const struct extmap *em = obj;
1829 return strdiacasecmp(p, em->em_ext);
1831 struct extmap *getextmap(const char *path)
1836 if (NULL == ( p = strrchr( path, '.' )) ) {
1837 return( Defextmap );
1840 if (!*p || !Extmap_cnt) {
1841 return( Defextmap );
1843 em = bsearch(p, Extmap, Extmap_cnt, sizeof(struct extmap), ext_cmp_key);
1847 return( Defextmap );
1851 /* ------------------------- */
1852 struct extmap *getdefextmap(void)
1854 return( Defextmap );
1857 /* --------------------------
1858 poll if a volume is changed by other processes.
1860 int pollvoltime(obj)
1867 if (!(afp_version > 21 && obj->options.server_notif))
1870 if ( gettimeofday( &tv, 0 ) < 0 )
1873 for ( vol = Volumes; vol; vol = vol->v_next ) {
1874 if ( (vol->v_flags & AFPVOL_OPEN) && vol->v_mtime + 30 < tv.tv_sec) {
1875 if ( !stat( vol->v_path, &st ) && vol->v_mtime != st.st_mtime ) {
1876 vol->v_mtime = st.st_mtime;
1877 if (!obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED))
1886 /* ------------------------- */
1887 void setvoltime(obj, vol )
1893 /* just looking at vol->v_mtime is broken seriously since updates
1894 * from other users afpd processes never are seen.
1895 * This is not the most elegant solution (a shared memory between
1896 * the afpd processes would come closer)
1899 if ( gettimeofday( &tv, 0 ) < 0 ) {
1900 LOG(log_error, logtype_afpd, "setvoltime: gettimeofday: %s", strerror(errno) );
1903 if( utime( vol->v_path, NULL ) < 0 ) {
1904 /* write of time failed ... probably a read only filesys,
1905 * where no other users can interfere, so there's no issue here
1909 /* a little granularity */
1910 if (vol->v_mtime < tv.tv_sec) {
1911 vol->v_mtime = tv.tv_sec;
1913 /* if 0ed, we're sending too many */
1914 if (afp_version > 21 && obj->options.server_notif) {
1915 obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED);
1921 /* ------------------------- */
1922 int afp_getvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1925 int ibuflen, *rbuflen;
1928 u_int16_t vid, bitmap;
1931 memcpy(&vid, ibuf, sizeof( vid ));
1932 ibuf += sizeof( vid );
1933 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1934 bitmap = ntohs( bitmap );
1936 if (NULL == ( vol = getvolbyvid( vid )) ) {
1938 return( AFPERR_PARAM );
1941 return stat_vol(bitmap, vol, rbuf, rbuflen);
1944 /* ------------------------- */
1945 int afp_setvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1948 int ibuflen, *rbuflen;
1952 u_int16_t vid, bitmap;
1958 memcpy(&vid, ibuf, sizeof( vid ));
1959 ibuf += sizeof( vid );
1960 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1961 bitmap = ntohs( bitmap );
1962 ibuf += sizeof(bitmap);
1964 if (( vol = getvolbyvid( vid )) == NULL ) {
1965 return( AFPERR_PARAM );
1968 if (vol->v_flags & AFPVOL_RO)
1969 return AFPERR_VLOCK;
1971 /* we can only set the backup date. */
1972 if (bitmap != (1 << VOLPBIT_BDATE))
1973 return AFPERR_BITMAP;
1975 ad_init(&ad, vol->v_adouble);
1976 if ( ad_open( vol->v_path, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR,
1979 return AFPERR_VLOCK;
1981 return AFPERR_ACCESS;
1984 memcpy(&aint, ibuf, sizeof(aint));
1985 ad_setdate(&ad, AD_DATE_BACKUP, aint);
1986 ad_flush(&ad, ADFLAGS_HF);
1987 ad_close(&ad, ADFLAGS_HF);
1991 /* ------------------------- */
1992 int wincheck(const struct vol *vol, const char *path)
1996 if (!(vol->v_flags & AFPVOL_MSWINDOWS))
1999 /* empty paths are not allowed */
2000 if ((len = strlen(path)) == 0)
2003 /* leading or trailing whitespaces are not allowed, carriage returns
2004 * and probably other whitespace is okay, tabs are not allowed
2006 if ((path[0] == ' ') || (path[len-1] == ' '))
2009 /* certain characters are not allowed */
2010 if (strpbrk(path, MSWINDOWS_BADCHARS))
2013 /* everything else is okay */
2019 * precreate a folder
2020 * this is only intended for folders in the volume root
2021 * It will *not* work if the folder name contains extended characters
2023 static int create_special_folder (const struct vol *vol, const struct _special_folder *folder)
2032 p = (char *) malloc ( strlen(vol->v_path)+strlen(folder->name)+2);
2034 LOG(log_error, logtype_afpd,"malloc failed");
2037 strcpy(p, vol->v_path);
2039 strcat(p, folder->name);
2041 if ( (ret = stat( p, &st )) < 0 ) {
2042 if (folder->precreate) {
2043 if (ad_mkdir(p, folder->mode & ~vol->v_umask)) {
2044 LOG(log_debug, logtype_afpd,"Creating '%s' failed", folder->name);
2052 if ( !ret && folder->hide) {
2054 ad_init(&ad, vol->v_adouble);
2055 if (ad_open( p, vol_noadouble(vol) | ADFLAGS_HF|ADFLAGS_DIR,
2056 O_RDWR|O_CREAT, 0666, &ad) < 0) {
2060 if ((ad_get_HF_flags( &ad ) & O_CREAT) ) {
2061 ad_setentrylen( &ad, ADEID_NAME, strlen(folder->name));
2062 memcpy(ad_entry( &ad, ADEID_NAME ), folder->name,
2063 ad_getentrylen( &ad, ADEID_NAME ));
2066 ad_getattr(&ad, &attr);
2067 attr |= htons( ntohs( attr ) | ATTRBIT_INVISIBLE );
2068 ad_setattr(&ad, attr);
2070 ad_flush( &ad, ADFLAGS_HF );
2071 ad_close( &ad, ADFLAGS_HF );
2077 static void handle_special_folders (const struct vol * vol)
2079 const _special_folder *p = &special_folders[0];
2081 if (vol->v_flags & AFPVOL_RO)
2084 for (; p->name != NULL; p++) {
2085 create_special_folder (vol, p);