2 * $Id: volume.c,v 1.51.2.7.2.10 2003-11-15 00:00:35 bfernhomberg Exp $
4 * Copyright (c) 1990,1993 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
10 #endif /* HAVE_CONFIG_H */
22 #endif /* HAVE_FCNTL_H */
29 #else /* STDC_HEADERS */
33 #endif /* HAVE_STRCHR */
34 char *strchr (), *strrchr ();
36 #define memcpy(d,s,n) bcopy ((s), (d), (n))
37 #define memmove(d,s,n) bcopy ((s), (d), (n))
38 #endif /* ! HAVE_MEMCPY */
39 #endif /* STDC_HEADERS */
41 #include <sys/types.h>
43 #include <sys/param.h>
44 #include <sys/socket.h>
45 #include <netinet/in.h>
46 #include <arpa/inet.h>
47 #include <netatalk/endian.h>
48 #include <atalk/asp.h>
49 #include <atalk/dsi.h>
50 #include <atalk/adouble.h>
51 #include <atalk/afp.h>
52 #include <atalk/util.h>
53 #include <atalk/logger.h>
55 #include <atalk/cnid.h>
59 #include "directory.h"
64 extern int afprun(int root, char *cmd, int *outfd);
67 #define MIN(a, b) ((a) < (b) ? (a) : (b))
70 #ifndef NO_LARGE_VOL_SUPPORT
71 #if BYTE_ORDER == BIG_ENDIAN
74 #else /* BYTE_ORDER == BIG_ENDIAN */
75 #define hton64(x) ((u_int64_t) (htonl(((x) >> 32) & 0xffffffffLL)) | \
76 (u_int64_t) ((htonl(x) & 0xffffffffLL) << 32))
77 #define ntoh64(x) (hton64(x))
78 #endif /* BYTE_ORDER == BIG_ENDIAN */
79 #endif /* ! NO_LARGE_VOL_SUPPORT */
81 static struct vol *Volumes = NULL;
82 static u_int16_t lastvid = 0;
83 static char *Trash = "\02\024Network Trash Folder";
85 static struct extmap *Extmap = NULL, *Defextmap = NULL;
86 static int Extmap_cnt;
87 static void free_extmap(void);
89 #define VOLOPT_ALLOW 0 /* user allow list */
90 #define VOLOPT_DENY 1 /* user deny list */
91 #define VOLOPT_RWLIST 2 /* user rw list */
92 #define VOLOPT_ROLIST 3 /* user ro list */
93 #define VOLOPT_PASSWORD 4 /* volume password */
94 #define VOLOPT_CASEFOLD 5 /* character case mangling */
95 #define VOLOPT_FLAGS 6 /* various flags */
96 #define VOLOPT_DBPATH 7 /* path to database */
97 #define VOLOPT_MAPCHARS 8 /* does mtou and utom mappings. syntax:
98 m and u can be double-byte hex
103 !u -> make u illegal always
104 ~u -> make u illegal only as the first
105 part of a double-byte character.
107 #define VOLOPT_VETO 10 /* list of veto filespec */
108 #define VOLOPT_PREEXEC 11 /* preexec command */
109 #define VOLOPT_ROOTPREEXEC 12 /* root preexec command */
111 #define VOLOPT_POSTEXEC 13 /* postexec command */
112 #define VOLOPT_ROOTPOSTEXEC 14 /* root postexec command */
114 #define VOLOPT_ENCODING 15 /* mac encoding (pre OSX)*/
115 #define VOLOPT_MACCHARSET 16
116 #define VOLOPT_CNIDSCHEME 17
117 #define VOLOPT_ADOUBLE 18 /* adouble version */
122 #define VOLOPT_FORCEUID 19 /* force uid for username x */
123 #define VOLOPT_FORCEGID 20 /* force gid for group x */
124 #define VOLOPT_UMASK 21
126 #define VOLOPT_UMASK 19
127 #endif /* FORCE_UIDGID */
129 #define VOLOPT_MAX (VOLOPT_UMASK +1)
131 #define VOLOPT_NUM (VOLOPT_MAX + 1)
134 #define VOLOPT_DEFAULT ":DEFAULT:"
135 #define VOLOPT_DEFAULT_LEN 9
141 typedef struct _special_folder {
148 static const _special_folder special_folders[] = {
149 {"Network Trash Folder", 1, 0777, 1},
150 {"Temporary Items", 1, 0777, 1},
152 {"TheFindByContentFolder", 0, 0, 1},
153 {"TheVolumeSettingsFolder", 0, 0, 1},
157 static void handle_special_folders (const struct vol *);
159 static __inline__ void volfree(struct vol_option *options,
160 const struct vol_option *save)
165 for (i = 0; i < VOLOPT_MAX; i++) {
166 if (options[i].c_value && (options[i].c_value != save[i].c_value))
167 free(options[i].c_value);
170 for (i = 0; i < VOLOPT_MAX; i++) {
171 if (options[i].c_value)
172 free(options[i].c_value);
178 /* handle variable substitutions. here's what we understand:
179 * $b -> basename of path
180 * $c -> client ip/appletalk address
181 * $d -> volume pathname on server
182 * $f -> full name (whatever's in the gecos field)
185 * $s -> server name (hostname if it doesn't exist)
186 * $u -> username (guest is usually nobody)
187 * $v -> volume name (ADEID_NAME or basename if ADEID_NAME is empty)
188 * $z -> zone (may not exist)
191 #define is_var(a, b) (strncmp((a), (b), 2) == 0)
193 static char *volxlate(AFPObj *obj, char *dest, size_t destlen,
194 char *src, struct passwd *pwd, char *path)
204 dest = calloc(destlen +1, 1);
210 strncpy(dest, src, destlen);
211 if ((p = strchr(src, '$')) == NULL) /* nothing to do */
214 /* first part of the path. just forward to the next variable. */
215 len = MIN(p - src, destlen);
221 while (p && destlen > 0) {
222 /* now figure out what the variable is */
224 if (is_var(p, "$b")) {
226 if ((q = strrchr(path, '/')) == NULL)
228 else if (*(q + 1) != '\0')
231 } else if (is_var(p, "$c")) {
232 if (obj->proto == AFPPROTO_ASP) {
233 ASP asp = obj->handle;
235 len = sprintf(dest, "%u.%u", ntohs(asp->asp_sat.sat_addr.s_net),
236 asp->asp_sat.sat_addr.s_node);
240 } else if (obj->proto == AFPPROTO_DSI) {
241 DSI *dsi = obj->handle;
243 len = sprintf(dest, "%s:%u", inet_ntoa(dsi->client.sin_addr),
244 ntohs(dsi->client.sin_port));
248 } else if (is_var(p, "$d")) {
250 } else if (is_var(p, "$f")) {
251 if ((q = strchr(pwd->pw_gecos, ',')))
254 } else if (is_var(p, "$g")) {
255 struct group *grp = getgrgid(pwd->pw_gid);
258 } else if (is_var(p, "$h")) {
259 q = obj->options.hostname;
260 } else if (is_var(p, "$s")) {
263 else if (obj->options.server) {
264 q = obj->options.server;
266 q = obj->options.hostname;
267 } else if (is_var(p, "$u")) {
269 } else if (is_var(p, "$v")) {
274 if (ad_open(path, ADFLAGS_HF, O_RDONLY, 0, &ad) < 0)
277 if ((len = MIN(ad_getentrylen(&ad, ADEID_NAME), destlen)) > 0) {
278 memcpy(dest, ad_entry(&ad, ADEID_NAME), len);
279 ad_close(&ad, ADFLAGS_HF);
283 ad_close(&ad, ADFLAGS_HF);
284 no_volname: /* simple basename */
285 if ((q = strrchr(path, '/')) == NULL)
287 else if (*(q + 1) != '\0')
291 } else if (is_var(p, "$z")) {
293 } else if (is_var(p, "$$")) {
298 /* copy the stuff over. if we don't understand something that we
299 * should, just skip it over. */
301 len = MIN(p == q ? 2 : strlen(q), destlen);
302 strncpy(dest, q, len);
307 /* stuff up to next $ */
309 p = strchr(src, '$');
310 len = p ? MIN(p - src, destlen) : destlen;
312 strncpy(dest, src, len);
320 /* to make sure that val is valid, make sure to select an opt that
322 static int optionok(const char *buf, const char *opt, const char *val)
324 if (!strstr(buf,opt))
332 /* -------------------- */
333 static void setoption(struct vol_option *options, struct vol_option *save, int opt, const char *val)
335 if (options[opt].c_value && (!save || options[opt].c_value != save[opt].c_value))
336 free(options[opt].c_value);
337 options[opt].c_value = strdup(val + 1);
340 /* ------------------------------------------
341 handle all the options. tmp can't be NULL. */
342 static void volset(struct vol_option *options, struct vol_option *save,
343 char *volname, int vlen,
348 val = strchr(tmp, ':');
350 /* we'll assume it's a volume name. */
351 strncpy(volname, tmp, vlen);
356 LOG(log_debug, logtype_afpd, "Parsing volset %s", val);
358 if (optionok(tmp, "allow:", val)) {
359 setoption(options, save, VOLOPT_ALLOW, val);
361 } else if (optionok(tmp, "deny:", val)) {
362 setoption(options, save, VOLOPT_DENY, val);
364 } else if (optionok(tmp, "rwlist:", val)) {
365 setoption(options, save, VOLOPT_RWLIST, val);
367 } else if (optionok(tmp, "rolist:", val)) {
368 setoption(options, save, VOLOPT_ROLIST, val);
370 } else if (optionok(tmp, "codepage:", val)) {
371 LOG (log_error, logtype_afpd, "The old codepage system has been removed. Please make sure to read the documentation !!!!");
372 /* Make sure we don't screw anything */
374 } else if (optionok(tmp, "volcharset:", val)) {
375 setoption(options, save, VOLOPT_ENCODING, val);
376 } else if (optionok(tmp, "maccharset:", val)) {
377 setoption(options, save, VOLOPT_MACCHARSET, val);
378 } else if (optionok(tmp, "veto:", val)) {
379 setoption(options, save, VOLOPT_VETO, val);
380 } else if (optionok(tmp, "cnidscheme:", val)) {
381 setoption(options, save, VOLOPT_CNIDSCHEME, val);
382 } else if (optionok(tmp, "casefold:", val)) {
383 if (strcasecmp(val + 1, "tolower") == 0)
384 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMLOWER;
385 else if (strcasecmp(val + 1, "toupper") == 0)
386 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMUPPER;
387 else if (strcasecmp(val + 1, "xlatelower") == 0)
388 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UUPPERMLOWER;
389 else if (strcasecmp(val + 1, "xlateupper") == 0)
390 options[VOLOPT_CASEFOLD].i_value = AFPVOL_ULOWERMUPPER;
391 } else if (optionok(tmp, "adouble:", val)) {
392 if (strcasecmp(val + 1, "v1") == 0)
393 options[VOLOPT_ADOUBLE].i_value = AD_VERSION1;
394 #if AD_VERSION == AD_VERSION2
395 else if (strcasecmp(val + 1, "v2") == 0)
396 options[VOLOPT_ADOUBLE].i_value = AD_VERSION2;
398 } else if (optionok(tmp, "options:", val)) {
401 if ((p = strtok(val + 1, ",")) == NULL) /* nothing */
405 if (strcasecmp(p, "prodos") == 0)
406 options[VOLOPT_FLAGS].i_value |= AFPVOL_A2VOL;
407 else if (strcasecmp(p, "mswindows") == 0) {
408 options[VOLOPT_FLAGS].i_value |= AFPVOL_MSWINDOWS;
409 } else if (strcasecmp(p, "crlf") == 0)
410 options[VOLOPT_FLAGS].i_value |= AFPVOL_CRLF;
411 else if (strcasecmp(p, "noadouble") == 0)
412 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOADOUBLE;
413 else if (strcasecmp(p, "ro") == 0)
414 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
415 else if (strcasecmp(p, "nohex") == 0)
416 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOHEX;
417 else if (strcasecmp(p, "usedots") == 0)
418 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS;
419 else if (strcasecmp(p, "limitsize") == 0)
420 options[VOLOPT_FLAGS].i_value |= AFPVOL_LIMITSIZE;
421 /* support for either "dropbox" or "dropkludge" */
422 else if (strcasecmp(p, "dropbox") == 0)
423 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
424 else if (strcasecmp(p, "dropkludge") == 0)
425 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
426 else if (strcasecmp(p, "nofileid") == 0)
427 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOFILEID;
428 else if (strcasecmp(p, "nostat") == 0)
429 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOSTAT;
430 else if (strcasecmp(p, "preexec_close") == 0)
431 options[VOLOPT_PREEXEC].i_value = 1;
432 else if (strcasecmp(p, "root_preexec_close") == 0)
433 options[VOLOPT_ROOTPREEXEC].i_value = 1;
434 else if (strcasecmp(p, "upriv") == 0)
435 options[VOLOPT_FLAGS].i_value |= AFPVOL_UNIX_PRIV;
437 p = strtok(NULL, ",");
440 } else if (optionok(tmp, "dbpath:", val)) {
441 setoption(options, save, VOLOPT_DBPATH, val);
443 } else if (optionok(tmp, "umask:", val)) {
444 options[VOLOPT_UMASK].i_value = (int)strtol(val, (char **)NULL, 8);
445 } else if (optionok(tmp, "mapchars:",val)) {
446 setoption(options, save, VOLOPT_MAPCHARS, val);
448 } else if (optionok(tmp, "password:", val)) {
449 setoption(options, save, VOLOPT_PASSWORD, val);
453 /* this code allows forced uid/gid per volume settings */
454 } else if (optionok(tmp, "forceuid:", val)) {
455 setoption(options, save, VOLOPT_FORCEUID, val);
456 } else if (optionok(tmp, "forcegid:", val)) {
457 setoption(options, save, VOLOPT_FORCEGID, val);
459 #endif /* FORCE_UIDGID */
460 } else if (optionok(tmp, "root_preexec:", val)) {
461 setoption(options, save, VOLOPT_ROOTPREEXEC, val);
463 } else if (optionok(tmp, "preexec:", val)) {
464 setoption(options, save, VOLOPT_PREEXEC, val);
466 } else if (optionok(tmp, "root_postexec:", val)) {
467 setoption(options, save, VOLOPT_ROOTPOSTEXEC, val);
469 } else if (optionok(tmp, "postexec:", val)) {
470 setoption(options, save, VOLOPT_POSTEXEC, val);
473 /* ignore unknown options */
474 LOG(log_debug, logtype_afpd, "ignoring unknown volume option: %s", tmp);
479 /* ----------------- */
480 static void showvol(const ucs2_t *name)
483 for ( volume = Volumes; volume; volume = volume->v_next ) {
484 if ( !strcasecmp_w( volume->v_name, name ) && volume->v_hide) {
491 /* ------------------------------- */
492 static int creatvol(AFPObj *obj, struct passwd *pwd,
493 char *path, char *name,
494 struct vol_option *options,
495 const int user /* user defined volume */
503 if ( name == NULL || *name == '\0' ) {
504 if ((name = strrchr( path, '/' )) == NULL) {
505 return -1; /* Obviously not a fully qualified path */
508 /* if you wish to share /, you need to specify a name. */
513 vlen = strlen( name );
514 if ( vlen > AFPVOL_NAMELEN ) {
515 vlen = AFPVOL_NAMELEN;
516 name[AFPVOL_NAMELEN] = '\0';
519 /* convert name to UCS2 first */
520 if ( 0 >= ( vlen = convert_string(obj->options.unixcharset, CH_UCS2, name, vlen, tmpname, 512)) )
523 for ( volume = Volumes; volume; volume = volume->v_next ) {
524 if ( strcasecmp_w( volume->v_name, tmpname ) == 0 ) {
525 if (volume->v_deleted) {
529 return -1; /* Won't be able to access it, anyway... */
535 if (!( volume = (struct vol *)calloc(1, sizeof( struct vol ))) ) {
536 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
539 if ( NULL == ( volume->v_name = strdup_w(tmpname))) {
540 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
544 if (!( volume->v_path = (char *)malloc( strlen( path ) + 1 )) ) {
545 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
546 free(volume->v_name);
550 volume->v_hide = hide;
551 strcpy( volume->v_path, path );
555 #endif /* __svr4__ */
556 /* os X start at 1 and use network order ie. 1 2 3 */
557 volume->v_vid = ++lastvid;
558 volume->v_vid = htons(volume->v_vid);
562 /* should we casefold? */
563 volume->v_casefold = options[VOLOPT_CASEFOLD].i_value;
565 /* shift in some flags */
566 volume->v_flags = options[VOLOPT_FLAGS].i_value;
568 if (options[VOLOPT_PASSWORD].c_value)
569 volume->v_password = strdup(options[VOLOPT_PASSWORD].c_value);
571 if (options[VOLOPT_VETO].c_value)
572 volume->v_veto = strdup(options[VOLOPT_VETO].c_value);
574 if (options[VOLOPT_ENCODING].c_value)
575 volume->v_volcodepage = strdup(options[VOLOPT_ENCODING].c_value);
577 if (options[VOLOPT_MACCHARSET].c_value)
578 volume->v_maccodepage = strdup(options[VOLOPT_MACCHARSET].c_value);
580 if (options[VOLOPT_DBPATH].c_value)
581 volume->v_dbpath = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_DBPATH].c_value, pwd, path);
583 if (options[VOLOPT_CNIDSCHEME].c_value)
584 volume->v_cnidscheme = strdup(options[VOLOPT_CNIDSCHEME].c_value);
586 if (options[VOLOPT_UMASK].i_value)
587 volume->v_umask = (mode_t)options[VOLOPT_UMASK].i_value;
589 if (options[VOLOPT_ADOUBLE].i_value)
590 volume->v_adouble = options[VOLOPT_ADOUBLE].i_value;
592 volume->v_adouble = AD_VERSION;
594 if (options[VOLOPT_FORCEUID].c_value) {
595 volume->v_forceuid = strdup(options[VOLOPT_FORCEUID].c_value);
597 volume->v_forceuid = NULL; /* set as null so as to return 0 later on */
600 if (options[VOLOPT_FORCEGID].c_value) {
601 volume->v_forcegid = strdup(options[VOLOPT_FORCEGID].c_value);
603 volume->v_forcegid = NULL; /* set as null so as to return 0 later on */
607 if (options[VOLOPT_PREEXEC].c_value)
608 volume->v_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_PREEXEC].c_value, pwd, path);
609 volume->v_preexec_close = options[VOLOPT_PREEXEC].i_value;
611 if (options[VOLOPT_POSTEXEC].c_value)
612 volume->v_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_POSTEXEC].c_value, pwd, path);
614 if (options[VOLOPT_ROOTPREEXEC].c_value)
615 volume->v_root_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPREEXEC].c_value, pwd, path);
616 volume->v_root_preexec_close = options[VOLOPT_ROOTPREEXEC].i_value;
618 if (options[VOLOPT_ROOTPOSTEXEC].c_value)
619 volume->v_root_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPOSTEXEC].c_value, pwd, path);
623 volume->v_next = Volumes;
628 /* ---------------- */
629 static char *myfgets( buf, size, fp )
638 while ((EOF != ( c = getc( fp )) ) && ( size > 0 )) {
639 if ( c == '\n' || c == '\r' ) {
657 /* check access list. this function wants something of the following
659 * @group,name,name2,@group2,name3
661 * a NULL argument allows everybody to have access.
662 * we return three things:
664 * 0: list exists, but name isn't in it
668 #ifndef NO_REAL_USER_NAME
669 /* authentication is case insensitive
670 * FIXME should we do the same with group name?
672 #define access_strcmp strcasecmp
675 #define access_strcmp strcmp
679 static int accessvol(args, name)
683 char buf[MAXPATHLEN + 1], *p;
689 strncpy(buf, args, sizeof(buf));
690 if ((p = strtok(buf, ",")) == NULL) /* nothing, return okay */
694 if (*p == '@') { /* it's a group */
695 if ((gr = getgrnam(p + 1)) && gmem(gr->gr_gid))
697 } else if (access_strcmp(p, name) == 0) /* it's a user name */
699 p = strtok(NULL, ",");
705 static void setextmap( ext, type, creator, user)
706 char *ext, *type, *creator;
712 if (Extmap == NULL) {
713 if (( Extmap = calloc(1, sizeof( struct extmap ))) == NULL ) {
714 LOG(log_error, logtype_afpd, "setextmap: calloc: %s", strerror(errno) );
719 for ( em = Extmap, cnt = 0; em->em_ext; em++, cnt++) {
720 if ( (strdiacasecmp( em->em_ext, ext )) == 0 ) {
725 if ( em->em_ext == NULL ) {
726 if (!(Extmap = realloc( Extmap, sizeof( struct extmap ) * (cnt +2))) ) {
727 LOG(log_error, logtype_afpd, "setextmap: realloc: %s", strerror(errno) );
730 (Extmap +cnt +1)->em_ext = NULL;
732 } else if ( !user ) {
738 if (!(em->em_ext = strdup( ext))) {
739 LOG(log_error, logtype_afpd, "setextmap: strdup: %s", strerror(errno) );
743 if ( *type == '\0' ) {
744 memcpy(em->em_type, "????", sizeof( em->em_type ));
746 memcpy(em->em_type, type, sizeof( em->em_type ));
748 if ( *creator == '\0' ) {
749 memcpy(em->em_creator, "UNIX", sizeof( em->em_creator ));
751 memcpy(em->em_creator, creator, sizeof( em->em_creator ));
755 /* -------------------------- */
756 static int extmap_cmp(const void *map1, const void *map2)
758 const struct extmap *em1 = map1;
759 const struct extmap *em2 = map2;
760 return strdiacasecmp(em1->em_ext, em2->em_ext);
763 static void sortextmap( void)
768 if ((em = Extmap) == NULL) {
776 qsort(Extmap, Extmap_cnt, sizeof(struct extmap), extmap_cmp);
781 /* ----------------------
783 static void free_extmap( void)
793 /* ----------------------
795 static int volfile_changed(struct afp_volume_name *p)
805 if (!stat( name, &st) && st.st_mtime > p->mtime) {
806 p->mtime = st.st_mtime;
812 /* ----------------------
813 * Read a volume configuration file and add the volumes contained within to
814 * the global volume list. If p2 is non-NULL, the file that is opened is
817 * Lines that begin with # and blank lines are ignored.
818 * Volume lines are of the form:
819 * <unix path> [<volume name>] [allow:<user>,<@group>,...] \
820 * [codepage:<file>] [casefold:<num>]
821 * <extension> TYPE [CREATOR]
823 static int readvolfile(obj, p1, p2, user, pwent)
825 struct afp_volume_name *p1;
828 struct passwd *pwent;
831 char path[ MAXPATHLEN + 1], tmp[ MAXPATHLEN + 1],
832 volname[ AFPVOL_NAMELEN + 1 ], buf[ BUFSIZ ],
833 type[ 5 ], creator[ 5 ];
836 struct vol_option options[VOLOPT_NUM], save_options[VOLOPT_NUM];
844 strcpy( path, p1->name );
851 p1->full_name = strdup(path);
854 if (NULL == ( fp = fopen( path, "r" )) ) {
858 if (fd != -1 && !fstat( fd, &st) ) {
859 p1->mtime = st.st_mtime;
862 memset(save_options, 0, sizeof(save_options));
863 while ( myfgets( buf, sizeof( buf ), fp ) != NULL ) {
864 initline( strlen( buf ), buf );
865 parseline( sizeof( path ) - 1, path );
872 /* change the default options for this file */
873 if (strncmp(path, VOLOPT_DEFAULT, VOLOPT_DEFAULT_LEN) == 0) {
875 for (i = 0; i < VOLOPT_NUM; i++) {
876 if (parseline( sizeof( path ) - VOLOPT_DEFAULT_LEN - 1,
877 path + VOLOPT_DEFAULT_LEN) < 0)
879 volset(save_options, NULL, tmp, sizeof(tmp) - 1,
880 path + VOLOPT_DEFAULT_LEN);
886 if (( p = strchr( path, '/' )) != NULL ) {
894 if ( u == NULL || *u == '\0' || ( pw = getpwnam( u )) == NULL ) {
897 strcpy( tmp, pw->pw_dir );
898 if ( p != NULL && *p != '\0' ) {
902 /* Tag a user's home directory with their umask. Note, this will
903 * be overwritten if the user actually specifies a umask: option
904 * for a '~' volume. */
905 save_options[VOLOPT_UMASK].i_value = obj->options.save_mask;
909 /* send path through variable substitution */
910 if (*path != '~') /* need to copy path to tmp */
913 pwent = getpwnam(obj->username);
914 volxlate(obj, path, sizeof(path) - 1, tmp, pwent, NULL);
916 /* this is sort of braindead. basically, i want to be
917 * able to specify things in any order, but i don't want to
918 * re-write everything.
920 * currently we have options:
928 * options:prodos,crlf,noadouble,ro...
933 * namemask:x,y,!z (not implemented yet)
935 memcpy(options, save_options, sizeof(options));
938 /* read in up to VOLOP_NUM possible options */
939 for (i = 0; i < VOLOPT_NUM; i++) {
940 if (parseline( sizeof( tmp ) - 1, tmp ) < 0)
943 volset(options, save_options, volname, sizeof(volname) - 1, tmp);
946 /* check allow/deny lists:
947 allow -> either no list (-1), or in list (1)
948 deny -> either no list (-1), or not in list (0) */
949 if (accessvol(options[VOLOPT_ALLOW].c_value, obj->username) &&
950 (accessvol(options[VOLOPT_DENY].c_value, obj->username) < 1)) {
952 /* handle read-only behaviour. semantics:
953 * 1) neither the rolist nor the rwlist exist -> rw
954 * 2) rolist exists -> ro if user is in it.
955 * 3) rwlist exists -> ro unless user is in it. */
956 if (((options[VOLOPT_FLAGS].i_value & AFPVOL_RO) == 0) &&
957 ((accessvol(options[VOLOPT_ROLIST].c_value,
958 obj->username) == 1) ||
959 !accessvol(options[VOLOPT_RWLIST].c_value,
961 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
963 /* do variable substitution for volname */
964 volxlate(obj, tmp, sizeof(tmp) - 1, volname, pwent, path);
965 creatvol(obj, pwent, path, tmp, options, p2 != NULL);
967 volfree(options, save_options);
971 parseline( sizeof( type ) - 1, type );
972 parseline( sizeof( creator ) - 1, creator );
973 setextmap( path, type, creator, user);
980 volfree(save_options, NULL);
982 if ( fclose( fp ) != 0 ) {
983 LOG(log_error, logtype_afpd, "readvolfile: fclose: %s", strerror(errno) );
989 /* ------------------------------- */
990 static void volume_free(struct vol *vol)
995 free(vol->v_password);
997 free(vol->v_volcodepage);
998 free(vol->v_maccodepage);
1001 free(vol->v_forceuid);
1002 free(vol->v_forcegid);
1003 #endif /* FORCE_UIDGID */
1006 /* ------------------------------- */
1007 static void free_volumes(void )
1010 struct vol *nvol, *ovol;
1012 for ( vol = Volumes; vol; vol = vol->v_next ) {
1013 if (( vol->v_flags & AFPVOL_OPEN ) ) {
1020 for ( vol = Volumes, ovol = NULL; vol; vol = nvol) {
1023 if (vol->v_name == NULL) {
1024 if (Volumes == vol) {
1031 ovol->v_next = nvol;
1041 /* ------------------------------- */
1042 static void volume_unlink(struct vol *volume)
1044 struct vol *vol, *ovol, *nvol;
1046 if (volume == Volumes) {
1047 Volumes = Volumes->v_next;
1050 for ( vol = Volumes, ovol = NULL; vol; vol = nvol) {
1053 if (vol == volume) {
1058 ovol->v_next = nvol;
1069 static int getvolspace( vol, bfree, btotal, xbfree, xbtotal, bsize )
1071 u_int32_t *bfree, *btotal, *bsize;
1072 VolSpace *xbfree, *xbtotal;
1076 #ifndef NO_QUOTA_SUPPORT
1077 VolSpace qfree, qtotal;
1080 spaceflag = AFPVOL_GVSMASK & vol->v_flags;
1081 /* report up to 2GB if afp version is < 2.2 (4GB if not) */
1082 maxsize = (vol->v_flags & AFPVOL_A2VOL) ? 0x01fffe00 :
1083 (((afp_version < 22) || (vol->v_flags & AFPVOL_LIMITSIZE))
1084 ? 0x7fffffffL : 0xffffffffL);
1087 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_AFSGVS ) {
1088 if ( afs_getvolspace( vol, xbfree, xbtotal, bsize ) == AFP_OK ) {
1089 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_AFSGVS;
1090 goto getvolspace_done;
1095 if (( rc = ustatfs_getvolspace( vol, xbfree, xbtotal,
1096 bsize)) != AFP_OK ) {
1100 #define min(a,b) ((a)<(b)?(a):(b))
1101 #ifndef NO_QUOTA_SUPPORT
1102 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_UQUOTA ) {
1103 if ( uquota_getvolspace( vol, &qfree, &qtotal, *bsize ) == AFP_OK ) {
1104 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_UQUOTA;
1105 *xbfree = min(*xbfree, qfree);
1106 *xbtotal = min( *xbtotal, qtotal);
1107 goto getvolspace_done;
1111 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_USTATFS;
1114 *bfree = min( *xbfree, maxsize);
1115 *btotal = min( *xbtotal, maxsize);
1119 /* -----------------------
1120 * set volume creation date
1121 * avoid duplicate, well at least it tries
1123 static void vol_setdate(u_int16_t id, struct adouble *adp, time_t date)
1126 struct vol *vol = Volumes;
1128 for ( volume = Volumes; volume; volume = volume->v_next ) {
1129 if (volume->v_vid == id) {
1132 else if (AD_DATE_FROM_UNIX(date) == volume->v_ctime) {
1134 volume = Volumes; /* restart */
1137 vol->v_ctime = AD_DATE_FROM_UNIX(date);
1138 ad_setdate(adp, AD_DATE_CREATE | AD_DATE_UNIX, date);
1141 /* ----------------------- */
1142 static int getvolparams( bitmap, vol, st, buf, buflen )
1150 int bit = 0, isad = 1;
1153 u_int32_t bfree, btotal, bsize;
1154 VolSpace xbfree, xbtotal; /* extended bytes */
1155 char *data, *nameoff = NULL;
1158 /* courtesy of jallison@whistle.com:
1159 * For MacOS8.x support we need to create the
1160 * .Parent file here if it doesn't exist. */
1162 ad_init(&ad, vol->v_adouble);
1163 if ( ad_open( vol->v_path, vol_noadouble(vol) |
1164 ADFLAGS_HF|ADFLAGS_DIR, O_RDWR | O_CREAT,
1167 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1169 } else if (ad_get_HF_flags( &ad ) & O_CREAT) {
1170 slash = strrchr( vol->v_path, '/' );
1174 slash = vol->v_path;
1176 ad_setentrylen( &ad, ADEID_NAME, strlen( slash ));
1177 memcpy(ad_entry( &ad, ADEID_NAME ), slash,
1178 ad_getentrylen( &ad, ADEID_NAME ));
1179 vol_setdate(vol->v_vid, &ad, st->st_mtime);
1180 ad_flush(&ad, ADFLAGS_HF);
1183 if (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0)
1184 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1186 vol->v_ctime = aint;
1189 if (( bitmap & ( (1<<VOLPBIT_BFREE)|(1<<VOLPBIT_BTOTAL) |
1190 (1<<VOLPBIT_XBFREE)|(1<<VOLPBIT_XBTOTAL) |
1191 (1<<VOLPBIT_BSIZE)) ) != 0 ) {
1192 if ( getvolspace( vol, &bfree, &btotal, &xbfree, &xbtotal,
1195 ad_close( &ad, ADFLAGS_HF );
1197 return( AFPERR_PARAM );
1202 while ( bitmap != 0 ) {
1203 while (( bitmap & 1 ) == 0 ) {
1211 if (0 == (vol->v_flags & AFPVOL_NOFILEID) && vol->v_cdb != NULL &&
1212 (vol->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1213 ashort = VOLPBIT_ATTR_FILEID;
1215 /* check for read-only.
1216 * NOTE: we don't actually set the read-only flag unless
1217 * it's passed in that way as it's possible to mount
1218 * a read-write filesystem under a read-only one. */
1219 if ((vol->v_flags & AFPVOL_RO) ||
1220 ((utime(vol->v_path, NULL) < 0) && (errno == EROFS))) {
1221 ashort |= VOLPBIT_ATTR_RO;
1223 ashort |= VOLPBIT_ATTR_CATSEARCH;
1224 if (afp_version >= 30) {
1225 ashort |= VOLPBIT_ATTR_UTF8;
1226 if (vol->v_flags & AFPVOL_UNIX_PRIV)
1227 ashort |= VOLPBIT_ATTR_UNIXPRIV;
1229 ashort = htons(ashort);
1230 memcpy(data, &ashort, sizeof( ashort ));
1231 data += sizeof( ashort );
1235 ashort = htons( AFPVOLSIG_DEFAULT );
1236 memcpy(data, &ashort, sizeof( ashort ));
1237 data += sizeof( ashort );
1240 case VOLPBIT_CDATE :
1241 aint = vol->v_ctime;
1242 memcpy(data, &aint, sizeof( aint ));
1243 data += sizeof( aint );
1246 case VOLPBIT_MDATE :
1247 if ( st->st_mtime > vol->v_mtime ) {
1248 vol->v_mtime = st->st_mtime;
1250 aint = AD_DATE_FROM_UNIX(vol->v_mtime);
1251 memcpy(data, &aint, sizeof( aint ));
1252 data += sizeof( aint );
1255 case VOLPBIT_BDATE :
1256 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1257 aint = AD_DATE_START;
1258 memcpy(data, &aint, sizeof( aint ));
1259 data += sizeof( aint );
1263 memcpy(data, &vol->v_vid, sizeof( vol->v_vid ));
1264 data += sizeof( vol->v_vid );
1267 case VOLPBIT_BFREE :
1268 bfree = htonl( bfree );
1269 memcpy(data, &bfree, sizeof( bfree ));
1270 data += sizeof( bfree );
1273 case VOLPBIT_BTOTAL :
1274 btotal = htonl( btotal );
1275 memcpy(data, &btotal, sizeof( btotal ));
1276 data += sizeof( btotal );
1279 #ifndef NO_LARGE_VOL_SUPPORT
1280 case VOLPBIT_XBFREE :
1281 xbfree = hton64( xbfree );
1282 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1283 bcopy(&xbfree, data, sizeof(xbfree));
1284 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1285 memcpy(data, &xbfree, sizeof( xbfree ));
1286 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1287 data += sizeof( xbfree );
1290 case VOLPBIT_XBTOTAL :
1291 xbtotal = hton64( xbtotal );
1292 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1293 bcopy(&xbtotal, data, sizeof(xbtotal));
1294 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1295 memcpy(data, &xbtotal, sizeof( xbtotal ));
1296 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1297 data += sizeof( xbfree );
1299 #endif /* ! NO_LARGE_VOL_SUPPORT */
1303 data += sizeof( u_int16_t );
1306 case VOLPBIT_BSIZE: /* block size */
1307 bsize = htonl(bsize);
1308 memcpy(data, &bsize, sizeof(bsize));
1309 data += sizeof(bsize);
1314 ad_close( &ad, ADFLAGS_HF );
1316 return( AFPERR_BITMAP );
1322 ashort = htons( data - buf );
1323 memcpy(nameoff, &ashort, sizeof( ashort ));
1324 aint = ucs2_to_charset( (utf8_encoding()?CH_UTF8_MAC:vol->v_maccharset), vol->v_name, data+1, 255);
1334 ad_close( &ad, ADFLAGS_HF );
1336 *buflen = data - buf;
1340 /* ------------------------- */
1341 int static stat_vol(u_int16_t bitmap, struct vol *vol, char *rbuf, int *rbuflen)
1346 if ( stat( vol->v_path, &st ) < 0 ) {
1348 return( AFPERR_PARAM );
1351 buflen = *rbuflen - sizeof( bitmap );
1352 if (( ret = getvolparams(bitmap, vol, &st,
1353 rbuf + sizeof( bitmap ), &buflen )) != AFP_OK ) {
1357 *rbuflen = buflen + sizeof( bitmap );
1358 bitmap = htons( bitmap );
1359 memcpy(rbuf, &bitmap, sizeof( bitmap ));
1364 /* ------------------------------- */
1365 void load_volumes(AFPObj *obj)
1367 struct passwd *pwent;
1372 /* check files date */
1373 if (obj->options.defaultvol.loaded) {
1374 changed = volfile_changed(&obj->options.defaultvol);
1376 if (obj->options.systemvol.loaded) {
1377 changed |= volfile_changed(&obj->options.systemvol);
1379 if (obj->options.uservol.loaded) {
1380 changed |= volfile_changed(&obj->options.uservol);
1389 pwent = getpwnam(obj->username);
1390 if ( (obj->options.flags & OPTION_USERVOLFIRST) == 0 ) {
1391 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent);
1394 if ((*obj->username == '\0') || (obj->options.flags & OPTION_NOUSERVOL)) {
1395 readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent);
1398 * Read user's AppleVolumes or .AppleVolumes file
1399 * If neither are readable, read the default volumes file. if
1400 * that doesn't work, create a user share.
1402 obj->options.uservol.name = strdup(pwent->pw_dir);
1403 if ( readvolfile(obj, &obj->options.uservol, "AppleVolumes", 1, pwent) < 0 &&
1404 readvolfile(obj, &obj->options.uservol, ".AppleVolumes", 1, pwent) < 0 &&
1405 readvolfile(obj, &obj->options.uservol, "applevolumes", 1, pwent) < 0 &&
1406 readvolfile(obj, &obj->options.uservol, ".applevolumes", 1, pwent) < 0 &&
1407 obj->options.defaultvol.name != NULL ) {
1408 if (readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent) < 0)
1409 creatvol(obj, pwent, pwent->pw_dir, NULL, NULL, 1);
1412 if ( obj->options.flags & OPTION_USERVOLFIRST ) {
1413 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent );
1417 /* ------------------------------- */
1418 int afp_getsrvrparms(obj, ibuf, ibuflen, rbuf, rbuflen )
1421 int ibuflen, *rbuflen;
1433 for ( vcnt = 0, volume = Volumes; volume; volume = volume->v_next ) {
1434 if (!(volume->v_flags & AFPVOL_NOSTAT)) {
1435 if ( stat( volume->v_path, &st ) < 0 ) {
1436 LOG(log_info, logtype_afpd, "afp_getsrvrparms: stat %s: %s",
1437 volume->v_path, strerror(errno) );
1438 continue; /* can't access directory */
1440 if (!S_ISDIR(st.st_mode)) {
1441 continue; /* not a dir */
1444 if (volume->v_hide) {
1445 continue; /* config file changed but the volume was mounted */
1447 /* set password bit if there's a volume password */
1448 *data = (volume->v_password) ? AFPSRVR_PASSWD : 0;
1450 /* Apple 2 clients running ProDOS-8 expect one volume to have
1451 bit 0 of this byte set. They will not recognize anything
1452 on the server unless this is the case. I have not
1453 completely worked this out, but it's related to booting
1454 from the server. Support for that function is a ways
1455 off.. <shirsch@ibm.net> */
1456 *data |= (volume->v_flags & AFPVOL_A2VOL) ? AFPSRVR_CONFIGINFO : 0;
1457 *data++ |= 0; /* UNIX PRIVS BIT ..., OSX doesn't seem to use it, so we don't either */
1459 len = ucs2_to_charset_allocate((utf8_encoding()?CH_UTF8_MAC:obj->options.maccharset),
1460 &namebuf, volume->v_name);
1464 memcpy(data, namebuf, len );
1470 *rbuflen = data - rbuf;
1472 if ( gettimeofday( &tv, 0 ) < 0 ) {
1473 LOG(log_error, logtype_afpd, "afp_getsrvrparms: gettimeofday: %s", strerror(errno) );
1475 return AFPERR_PARAM;
1477 tv.tv_sec = AD_DATE_FROM_UNIX(tv.tv_sec);
1478 memcpy(data, &tv.tv_sec, sizeof( u_int32_t));
1479 data += sizeof( u_int32_t);
1484 /* -------------------------
1485 * we are the user here
1487 int afp_openvol(obj, ibuf, ibuflen, rbuf, rbuflen )
1490 int ibuflen, *rbuflen;
1502 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1503 bitmap = ntohs( bitmap );
1504 ibuf += sizeof( bitmap );
1505 if (( bitmap & (1<<VOLPBIT_VID)) == 0 ) {
1507 return AFPERR_BITMAP;
1510 len = (unsigned char)*ibuf++;
1511 volname = obj->oldtmp;
1512 namelen = convert_string( (utf8_encoding()?CH_UTF8_MAC:obj->options.maccharset), CH_UCS2,
1513 ibuf, len, volname, sizeof(obj->oldtmp));
1516 return AFPERR_PARAM;
1520 if ((len + 1) & 1) /* pad to an even boundary */
1525 for ( volume = Volumes; volume; volume = volume->v_next ) {
1526 if ( strcasecmp_w( (ucs2_t*) volname, volume->v_name ) == 0 ) {
1531 if ( volume == NULL ) {
1533 return AFPERR_PARAM;
1536 /* check for a volume password */
1537 if (volume->v_password && strncmp(ibuf, volume->v_password, VOLPASSLEN)) {
1539 return AFPERR_ACCESS;
1542 if (( volume->v_flags & AFPVOL_OPEN ) ) {
1543 /* the volume is already open */
1545 set_uidgid ( volume );
1547 return stat_vol(bitmap, volume, rbuf, rbuflen);
1550 /* initialize volume variables
1553 if (afp_version >= 30) {
1554 volume->max_filename = 255;
1557 volume->max_filename = MACFILELEN;
1560 volume->v_dir = volume->v_root = NULL;
1562 /* FIXME unix name != mac name */
1566 len = convert_string_allocate( CH_UCS2, (utf8_encoding()?CH_UTF8_MAC:obj->options.maccharset),
1567 volume->v_name, namelen, &vol_mname);
1568 if ( !vol_mname || len <= 0) {
1573 len = convert_string_allocate( CH_UCS2 , volume->v_volcharset, volume->v_name, namelen, &vol_uname);
1575 if ( !vol_uname || len <= 0) {
1581 if ((dir = dirnew(vol_mname, vol_uname) ) == NULL) {
1584 LOG(log_error, logtype_afpd, "afp_openvol: malloc: %s", strerror(errno) );
1591 dir->d_did = DIRDID_ROOT;
1592 dir->d_color = DIRTREE_COLOR_BLACK; /* root node is black */
1593 volume->v_dir = volume->v_root = dir;
1594 volume->v_flags |= AFPVOL_OPEN;
1595 volume->v_cdb = NULL;
1597 if (volume->v_root_preexec) {
1598 if ((ret = afprun(1, volume->v_root_preexec, NULL)) && volume->v_root_preexec_close) {
1599 LOG(log_error, logtype_afpd, "afp_openvol: root preexec : %d", ret );
1606 set_uidgid ( volume );
1609 if (volume->v_preexec) {
1610 if ((ret = afprun(0, volume->v_preexec, NULL)) && volume->v_preexec_close) {
1611 LOG(log_error, logtype_afpd, "afp_openvol: preexec : %d", ret );
1617 if ( stat( volume->v_path, &st ) < 0 ) {
1622 if ( chdir( volume->v_path ) < 0 ) {
1626 curdir = volume->v_dir;
1627 if (volume->v_cnidscheme == NULL) {
1628 volume->v_cnidscheme = strdup(DEFAULT_CNID_SCHEME);
1629 LOG(log_warning, logtype_afpd, "Warning: No CNID scheme for volume %s. Using default.",
1632 if (volume->v_dbpath)
1633 volume->v_cdb = cnid_open (volume->v_dbpath, volume->v_umask, volume->v_cnidscheme);
1634 if (volume->v_cdb == NULL)
1635 volume->v_cdb = cnid_open (volume->v_path, volume->v_umask, volume->v_cnidscheme);
1636 if (volume->v_cdb == NULL) {
1637 volume->v_cdb = cnid_open (volume->v_path, volume->v_umask, DEFAULT_CNID_SCHEME);
1638 LOG(log_error, logtype_afpd, "Error: invalid CNID backend for %s: %s", volume->v_path,
1639 volume->v_cnidscheme);
1641 if (volume->v_cdb == NULL) {
1642 LOG(log_error, logtype_afpd, "Fatal error: cannot open CNID for %s", volume->v_path);
1649 if (!volume->v_volcodepage)
1650 volume->v_volcodepage = strdup("UTF8");
1652 if ( (charset_t) -1 == ( volume->v_volcharset = add_charset(volume->v_volcodepage)) ) {
1653 LOG (log_error, logtype_afpd, "Setting codepage %s as volume codepage failed", volume->v_volcodepage);
1658 if ( NULL == ( volume->v_vol = find_charset_functions(volume->v_volcodepage)) || volume->v_vol->flags & CHARSET_ICONV ) {
1659 LOG (log_warning, logtype_afpd, "WARNING: volume encoding %s is *not* supported by netatalk, expect problems !!!!", volume->v_volcodepage);
1662 if (!volume->v_maccodepage)
1663 volume->v_maccodepage = strdup(obj->options.maccodepage);
1665 if ( (charset_t) -1 == ( volume->v_maccharset = add_charset(volume->v_maccodepage)) ) {
1666 LOG (log_error, logtype_afpd, "Setting codepage %s as mac codepage failed", volume->v_maccodepage);
1671 if ( NULL == ( volume->v_mac = find_charset_functions(volume->v_maccodepage)) || ! (volume->v_mac->flags & CHARSET_CLIENT) ) {
1672 LOG (log_error, logtype_afpd, "Fatal error: mac charset %s not supported", volume->v_maccodepage);
1677 ret = stat_vol(bitmap, volume, rbuf, rbuflen);
1678 if (ret == AFP_OK) {
1680 handle_special_folders( volume );
1683 * If you mount a volume twice, the second time the trash appears on
1684 * the desk-top. That's because the Mac remembers the DID for the
1685 * trash (even for volumes in different zones, on different servers).
1686 * Just so this works better, we prime the DID cache with the trash,
1687 * fixing the trash at DID 17.
1688 * FIXME (RL): should it be done inside a CNID backend ? (always returning Trash DID when asked) ?
1690 if ((volume->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1692 /* FIXME find db time stamp */
1693 cnid_getstamp(volume->v_cdb, volume->v_stamp, sizeof(volume->v_stamp));
1697 cname( volume, volume->v_dir, &p );
1703 if (volume->v_dir) {
1704 dirfree( volume->v_dir );
1705 volume->v_dir = volume->v_root = NULL;
1708 volume->v_flags &= ~AFPVOL_OPEN;
1709 if (volume->v_cdb != NULL) {
1710 cnid_close(volume->v_cdb);
1711 volume->v_cdb = NULL;
1717 /* ------------------------- */
1718 static void closevol(struct vol *vol)
1723 dirfree( vol->v_root );
1725 if (vol->v_cdb != NULL) {
1726 cnid_close(vol->v_cdb);
1730 if (vol->v_postexec) {
1731 afprun(0, vol->v_postexec, NULL);
1733 if (vol->v_root_postexec) {
1734 afprun(1, vol->v_root_postexec, NULL);
1738 /* ------------------------- */
1739 void close_all_vol(void)
1743 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
1744 if ( ovol->v_flags & AFPVOL_OPEN ) {
1745 ovol->v_flags &= ~AFPVOL_OPEN;
1751 /* ------------------------- */
1752 int afp_closevol(obj, ibuf, ibuflen, rbuf, rbuflen )
1755 int ibuflen, *rbuflen;
1757 struct vol *vol, *ovol;
1762 memcpy(&vid, ibuf, sizeof( vid ));
1763 if (NULL == ( vol = getvolbyvid( vid )) ) {
1764 return( AFPERR_PARAM );
1767 vol->v_flags &= ~AFPVOL_OPEN;
1768 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
1769 if ( ovol->v_flags & AFPVOL_OPEN ) {
1773 if ( ovol != NULL ) {
1774 /* Even if chdir fails, we can't say afp_closevol fails. */
1775 if ( chdir( ovol->v_path ) == 0 ) {
1776 curdir = ovol->v_dir;
1781 if (vol->v_deleted) {
1782 showvol(vol->v_name);
1789 /* ------------------------- */
1790 struct vol *getvolbyvid(const u_int16_t vid )
1794 for ( vol = Volumes; vol; vol = vol->v_next ) {
1795 if ( vid == vol->v_vid ) {
1799 if ( vol == NULL || ( vol->v_flags & AFPVOL_OPEN ) == 0 ) {
1805 #endif /* FORCE_UIDGID */
1810 /* ------------------------ */
1811 static int ext_cmp_key(const void *key, const void *obj)
1813 const char *p = key;
1814 const struct extmap *em = obj;
1815 return strdiacasecmp(p, em->em_ext);
1817 struct extmap *getextmap(const char *path)
1822 if (NULL == ( p = strrchr( path, '.' )) ) {
1823 return( Defextmap );
1826 if (!*p || !Extmap_cnt) {
1827 return( Defextmap );
1829 em = bsearch(p, Extmap, Extmap_cnt, sizeof(struct extmap), ext_cmp_key);
1833 return( Defextmap );
1837 /* ------------------------- */
1838 struct extmap *getdefextmap(void)
1840 return( Defextmap );
1843 /* --------------------------
1844 poll if a volume is changed by other processes.
1846 int pollvoltime(obj)
1853 if (!(afp_version > 21 && obj->options.server_notif))
1856 if ( gettimeofday( &tv, 0 ) < 0 )
1859 for ( vol = Volumes; vol; vol = vol->v_next ) {
1860 if ( (vol->v_flags & AFPVOL_OPEN) && vol->v_mtime + 30 < tv.tv_sec) {
1861 if ( !stat( vol->v_path, &st ) && vol->v_mtime != st.st_mtime ) {
1862 vol->v_mtime = st.st_mtime;
1863 if (!obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED))
1872 /* ------------------------- */
1873 void setvoltime(obj, vol )
1879 /* just looking at vol->v_mtime is broken seriously since updates
1880 * from other users afpd processes never are seen.
1881 * This is not the most elegant solution (a shared memory between
1882 * the afpd processes would come closer)
1885 if ( gettimeofday( &tv, 0 ) < 0 ) {
1886 LOG(log_error, logtype_afpd, "setvoltime: gettimeofday: %s", strerror(errno) );
1889 if( utime( vol->v_path, NULL ) < 0 ) {
1890 /* write of time failed ... probably a read only filesys,
1891 * where no other users can interfere, so there's no issue here
1895 /* a little granularity */
1896 if (vol->v_mtime < tv.tv_sec) {
1897 vol->v_mtime = tv.tv_sec;
1899 /* if 0ed, we're sending too many */
1900 if (afp_version > 21 && obj->options.server_notif) {
1901 obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED);
1907 /* ------------------------- */
1908 int afp_getvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1911 int ibuflen, *rbuflen;
1914 u_int16_t vid, bitmap;
1917 memcpy(&vid, ibuf, sizeof( vid ));
1918 ibuf += sizeof( vid );
1919 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1920 bitmap = ntohs( bitmap );
1922 if (NULL == ( vol = getvolbyvid( vid )) ) {
1924 return( AFPERR_PARAM );
1927 return stat_vol(bitmap, vol, rbuf, rbuflen);
1930 /* ------------------------- */
1931 int afp_setvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1934 int ibuflen, *rbuflen;
1938 u_int16_t vid, bitmap;
1944 memcpy(&vid, ibuf, sizeof( vid ));
1945 ibuf += sizeof( vid );
1946 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1947 bitmap = ntohs( bitmap );
1948 ibuf += sizeof(bitmap);
1950 if (( vol = getvolbyvid( vid )) == NULL ) {
1951 return( AFPERR_PARAM );
1954 if (vol->v_flags & AFPVOL_RO)
1955 return AFPERR_VLOCK;
1957 /* we can only set the backup date. */
1958 if (bitmap != (1 << VOLPBIT_BDATE))
1959 return AFPERR_BITMAP;
1961 ad_init(&ad, vol->v_adouble);
1962 if ( ad_open( vol->v_path, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR,
1965 return AFPERR_VLOCK;
1967 return AFPERR_ACCESS;
1970 memcpy(&aint, ibuf, sizeof(aint));
1971 ad_setdate(&ad, AD_DATE_BACKUP, aint);
1972 ad_flush(&ad, ADFLAGS_HF);
1973 ad_close(&ad, ADFLAGS_HF);
1977 /* ------------------------- */
1978 int wincheck(const struct vol *vol, const char *path)
1982 if (!(vol->v_flags & AFPVOL_MSWINDOWS))
1985 /* empty paths are not allowed */
1986 if ((len = strlen(path)) == 0)
1989 /* leading or trailing whitespaces are not allowed, carriage returns
1990 * and probably other whitespace is okay, tabs are not allowed
1992 if ((path[0] == ' ') || (path[len-1] == ' '))
1995 /* certain characters are not allowed */
1996 if (strpbrk(path, MSWINDOWS_BADCHARS))
1999 /* everything else is okay */
2005 * precreate a folder
2006 * this is only intended for folders in the volume root
2007 * It will *not* work if the folder name contains extended characters
2009 static int create_special_folder (const struct vol *vol, const struct _special_folder *folder)
2018 p = (char *) malloc ( strlen(vol->v_path)+strlen(folder->name)+2);
2020 LOG(log_error, logtype_afpd,"malloc failed");
2023 strcpy(p, vol->v_path);
2025 strcat(p, folder->name);
2027 if ( (ret = stat( p, &st )) < 0 ) {
2028 if (folder->precreate) {
2029 if (ad_mkdir(p, folder->mode & ~vol->v_umask)) {
2030 LOG(log_debug, logtype_afpd,"Creating '%s' failed", folder->name);
2038 if ( !ret && folder->hide) {
2040 ad_init(&ad, vol->v_adouble);
2041 if (ad_open( p, vol_noadouble(vol) | ADFLAGS_HF|ADFLAGS_DIR,
2042 O_RDWR|O_CREAT, 0666, &ad) < 0) {
2046 if ((ad_get_HF_flags( &ad ) & O_CREAT) ) {
2047 ad_setentrylen( &ad, ADEID_NAME, strlen(folder->name));
2048 memcpy(ad_entry( &ad, ADEID_NAME ), folder->name,
2049 ad_getentrylen( &ad, ADEID_NAME ));
2052 ad_getattr(&ad, &attr);
2053 attr |= htons( ntohs( attr ) | ATTRBIT_INVISIBLE );
2054 ad_setattr(&ad, attr);
2056 ad_flush( &ad, ADFLAGS_HF );
2057 ad_close( &ad, ADFLAGS_HF );
2063 static void handle_special_folders (const struct vol * vol)
2065 const _special_folder *p = &special_folders[0];
2067 if (vol->v_flags & AFPVOL_RO)
2070 for (; p->name != NULL; p++) {
2071 create_special_folder (vol, p);