2 * $Id: volume.c,v 1.51.2.7.2.20 2004-02-14 15:47:20 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 or basename if null
189 * $z -> zone (may not exist)
194 #define is_var(a, b) (strncmp((a), (b), 2) == 0)
196 static char *volxlate(AFPObj *obj, char *dest, size_t destlen,
197 char *src, struct passwd *pwd, char *path, char *volname)
207 dest = calloc(destlen +1, 1);
213 strlcpy(dest, src, destlen +1);
214 if ((p = strchr(src, '$')) == NULL) /* nothing to do */
217 /* first part of the path. just forward to the next variable. */
218 len = MIN(p - src, destlen);
224 while (p && destlen > 0) {
225 /* now figure out what the variable is */
227 if (is_var(p, "$b")) {
229 if ((q = strrchr(path, '/')) == NULL)
231 else if (*(q + 1) != '\0')
234 } else if (is_var(p, "$c")) {
235 if (obj->proto == AFPPROTO_ASP) {
236 ASP asp = obj->handle;
238 len = sprintf(dest, "%u.%u", ntohs(asp->asp_sat.sat_addr.s_net),
239 asp->asp_sat.sat_addr.s_node);
243 } else if (obj->proto == AFPPROTO_DSI) {
244 DSI *dsi = obj->handle;
246 len = sprintf(dest, "%s:%u", inet_ntoa(dsi->client.sin_addr),
247 ntohs(dsi->client.sin_port));
251 } else if (is_var(p, "$d")) {
253 } else if (is_var(p, "$f")) {
254 if ((q = strchr(pwd->pw_gecos, ',')))
257 } else if (is_var(p, "$g")) {
258 struct group *grp = getgrgid(pwd->pw_gid);
261 } else if (is_var(p, "$h")) {
262 q = obj->options.hostname;
263 } else if (is_var(p, "$i")) {
264 if (obj->proto == AFPPROTO_ASP) {
265 ASP asp = obj->handle;
267 len = sprintf(dest, "%u", ntohs(asp->asp_sat.sat_addr.s_net));
271 } else if (obj->proto == AFPPROTO_DSI) {
272 DSI *dsi = obj->handle;
274 q = inet_ntoa(dsi->client.sin_addr);
276 } else if (is_var(p, "$s")) {
279 else if (obj->options.server) {
280 q = obj->options.server;
282 q = obj->options.hostname;
283 } else if (is_var(p, "$u")) {
285 } else if (is_var(p, "$v")) {
290 if ((q = strrchr(path, '/')) == NULL)
292 else if (*(q + 1) != '\0')
295 } else if (is_var(p, "$z")) {
297 } else if (is_var(p, "$$")) {
302 /* copy the stuff over. if we don't understand something that we
303 * should, just skip it over. */
305 len = MIN(p == q ? 2 : strlen(q), destlen);
306 strncpy(dest, q, len);
311 /* stuff up to next $ */
313 p = strchr(src, '$');
314 len = p ? MIN(p - src, destlen) : destlen;
316 strncpy(dest, src, len);
324 /* to make sure that val is valid, make sure to select an opt that
326 static int optionok(const char *buf, const char *opt, const char *val)
328 if (!strstr(buf,opt))
336 /* -------------------- */
337 static void setoption(struct vol_option *options, struct vol_option *save, int opt, const char *val)
339 if (options[opt].c_value && (!save || options[opt].c_value != save[opt].c_value))
340 free(options[opt].c_value);
341 options[opt].c_value = strdup(val + 1);
344 /* ------------------------------------------
345 handle all the options. tmp can't be NULL. */
346 static void volset(struct vol_option *options, struct vol_option *save,
347 char *volname, int vlen,
352 val = strchr(tmp, ':');
354 /* we'll assume it's a volume name. */
355 strncpy(volname, tmp, vlen);
360 LOG(log_debug, logtype_afpd, "Parsing volset %s", val);
362 if (optionok(tmp, "allow:", val)) {
363 setoption(options, save, VOLOPT_ALLOW, val);
365 } else if (optionok(tmp, "deny:", val)) {
366 setoption(options, save, VOLOPT_DENY, val);
368 } else if (optionok(tmp, "rwlist:", val)) {
369 setoption(options, save, VOLOPT_RWLIST, val);
371 } else if (optionok(tmp, "rolist:", val)) {
372 setoption(options, save, VOLOPT_ROLIST, val);
374 } else if (optionok(tmp, "codepage:", val)) {
375 LOG (log_error, logtype_afpd, "The old codepage system has been removed. Please make sure to read the documentation !!!!");
376 /* Make sure we don't screw anything */
378 } else if (optionok(tmp, "volcharset:", val)) {
379 setoption(options, save, VOLOPT_ENCODING, val);
380 } else if (optionok(tmp, "maccharset:", val)) {
381 setoption(options, save, VOLOPT_MACCHARSET, val);
382 } else if (optionok(tmp, "veto:", val)) {
383 setoption(options, save, VOLOPT_VETO, val);
384 } else if (optionok(tmp, "cnidscheme:", val)) {
385 setoption(options, save, VOLOPT_CNIDSCHEME, val);
386 } else if (optionok(tmp, "casefold:", val)) {
387 if (strcasecmp(val + 1, "tolower") == 0)
388 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMLOWER;
389 else if (strcasecmp(val + 1, "toupper") == 0)
390 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMUPPER;
391 else if (strcasecmp(val + 1, "xlatelower") == 0)
392 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UUPPERMLOWER;
393 else if (strcasecmp(val + 1, "xlateupper") == 0)
394 options[VOLOPT_CASEFOLD].i_value = AFPVOL_ULOWERMUPPER;
395 } else if (optionok(tmp, "adouble:", val)) {
396 if (strcasecmp(val + 1, "v1") == 0)
397 options[VOLOPT_ADOUBLE].i_value = AD_VERSION1;
398 #if AD_VERSION == AD_VERSION2
399 else if (strcasecmp(val + 1, "v2") == 0)
400 options[VOLOPT_ADOUBLE].i_value = AD_VERSION2;
402 } else if (optionok(tmp, "options:", val)) {
405 if ((p = strtok(val + 1, ",")) == NULL) /* nothing */
409 if (strcasecmp(p, "prodos") == 0)
410 options[VOLOPT_FLAGS].i_value |= AFPVOL_A2VOL;
411 else if (strcasecmp(p, "mswindows") == 0) {
412 options[VOLOPT_FLAGS].i_value |= AFPVOL_MSWINDOWS | AFPVOL_USEDOTS;
413 } else if (strcasecmp(p, "crlf") == 0)
414 options[VOLOPT_FLAGS].i_value |= AFPVOL_CRLF;
415 else if (strcasecmp(p, "noadouble") == 0)
416 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOADOUBLE;
417 else if (strcasecmp(p, "ro") == 0)
418 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
419 else if (strcasecmp(p, "nohex") == 0)
420 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOHEX;
421 else if (strcasecmp(p, "usedots") == 0)
422 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS;
423 else if (strcasecmp(p, "limitsize") == 0)
424 options[VOLOPT_FLAGS].i_value |= AFPVOL_LIMITSIZE;
425 /* support for either "dropbox" or "dropkludge" */
426 else if (strcasecmp(p, "dropbox") == 0)
427 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
428 else if (strcasecmp(p, "dropkludge") == 0)
429 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
430 else if (strcasecmp(p, "nofileid") == 0)
431 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOFILEID;
432 else if (strcasecmp(p, "nostat") == 0)
433 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOSTAT;
434 else if (strcasecmp(p, "preexec_close") == 0)
435 options[VOLOPT_PREEXEC].i_value = 1;
436 else if (strcasecmp(p, "root_preexec_close") == 0)
437 options[VOLOPT_ROOTPREEXEC].i_value = 1;
438 else if (strcasecmp(p, "upriv") == 0)
439 options[VOLOPT_FLAGS].i_value |= AFPVOL_UNIX_PRIV;
440 else if (strcasecmp(p, "nodev") == 0)
441 options[VOLOPT_FLAGS].i_value |= AFPVOL_NODEV;
443 p = strtok(NULL, ",");
446 } else if (optionok(tmp, "dbpath:", val)) {
447 setoption(options, save, VOLOPT_DBPATH, val);
449 } else if (optionok(tmp, "umask:", val)) {
450 options[VOLOPT_UMASK].i_value = (int)strtol(val, (char **)NULL, 8);
451 } else if (optionok(tmp, "mapchars:",val)) {
452 setoption(options, save, VOLOPT_MAPCHARS, val);
454 } else if (optionok(tmp, "password:", val)) {
455 setoption(options, save, VOLOPT_PASSWORD, val);
459 /* this code allows forced uid/gid per volume settings */
460 } else if (optionok(tmp, "forceuid:", val)) {
461 setoption(options, save, VOLOPT_FORCEUID, val);
462 } else if (optionok(tmp, "forcegid:", val)) {
463 setoption(options, save, VOLOPT_FORCEGID, val);
465 #endif /* FORCE_UIDGID */
466 } else if (optionok(tmp, "root_preexec:", val)) {
467 setoption(options, save, VOLOPT_ROOTPREEXEC, val);
469 } else if (optionok(tmp, "preexec:", val)) {
470 setoption(options, save, VOLOPT_PREEXEC, val);
472 } else if (optionok(tmp, "root_postexec:", val)) {
473 setoption(options, save, VOLOPT_ROOTPOSTEXEC, val);
475 } else if (optionok(tmp, "postexec:", val)) {
476 setoption(options, save, VOLOPT_POSTEXEC, val);
479 /* ignore unknown options */
480 LOG(log_debug, logtype_afpd, "ignoring unknown volume option: %s", tmp);
485 /* ----------------- */
486 static void showvol(const ucs2_t *name)
489 for ( volume = Volumes; volume; volume = volume->v_next ) {
490 if (volume->v_hide && !strcasecmp_w( volume->v_name, name ) ) {
497 /* ------------------------------- */
498 static int creatvol(AFPObj *obj, struct passwd *pwd,
499 char *path, char *name,
500 struct vol_option *options,
501 const int user /* user defined volume */
509 if ( name == NULL || *name == '\0' ) {
510 if ((name = strrchr( path, '/' )) == NULL) {
511 return -1; /* Obviously not a fully qualified path */
514 /* if you wish to share /, you need to specify a name. */
519 vlen = strlen( name );
520 if ( vlen > AFPVOL_NAMELEN ) {
521 vlen = AFPVOL_NAMELEN;
522 name[AFPVOL_NAMELEN] = '\0';
525 /* convert name to UCS2 first */
526 if ( 0 >= ( vlen = convert_string(obj->options.unixcharset, CH_UCS2, name, vlen, tmpname, 512)) )
529 for ( volume = Volumes; volume; volume = volume->v_next ) {
530 if ( strcasecmp_w( volume->v_name, tmpname ) == 0 ) {
531 if (volume->v_deleted) {
535 return -1; /* Won't be able to access it, anyway... */
541 if (!( volume = (struct vol *)calloc(1, sizeof( struct vol ))) ) {
542 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
545 if ( NULL == ( volume->v_name = strdup_w(tmpname))) {
546 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
550 if (!( volume->v_path = (char *)malloc( strlen( path ) + 1 )) ) {
551 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
552 free(volume->v_name);
556 volume->v_hide = hide;
557 strcpy( volume->v_path, path );
561 #endif /* __svr4__ */
562 /* os X start at 1 and use network order ie. 1 2 3 */
563 volume->v_vid = ++lastvid;
564 volume->v_vid = htons(volume->v_vid);
568 /* should we casefold? */
569 volume->v_casefold = options[VOLOPT_CASEFOLD].i_value;
571 /* shift in some flags */
572 volume->v_flags = options[VOLOPT_FLAGS].i_value;
574 if (options[VOLOPT_PASSWORD].c_value)
575 volume->v_password = strdup(options[VOLOPT_PASSWORD].c_value);
577 if (options[VOLOPT_VETO].c_value)
578 volume->v_veto = strdup(options[VOLOPT_VETO].c_value);
580 if (options[VOLOPT_ENCODING].c_value)
581 volume->v_volcodepage = strdup(options[VOLOPT_ENCODING].c_value);
583 if (options[VOLOPT_MACCHARSET].c_value)
584 volume->v_maccodepage = strdup(options[VOLOPT_MACCHARSET].c_value);
586 if (options[VOLOPT_DBPATH].c_value)
587 volume->v_dbpath = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_DBPATH].c_value, pwd, path, name);
589 if (options[VOLOPT_CNIDSCHEME].c_value)
590 volume->v_cnidscheme = strdup(options[VOLOPT_CNIDSCHEME].c_value);
592 if (options[VOLOPT_UMASK].i_value)
593 volume->v_umask = (mode_t)options[VOLOPT_UMASK].i_value;
595 if (options[VOLOPT_ADOUBLE].i_value)
596 volume->v_adouble = options[VOLOPT_ADOUBLE].i_value;
598 volume->v_adouble = AD_VERSION;
600 if (options[VOLOPT_FORCEUID].c_value) {
601 volume->v_forceuid = strdup(options[VOLOPT_FORCEUID].c_value);
603 volume->v_forceuid = NULL; /* set as null so as to return 0 later on */
606 if (options[VOLOPT_FORCEGID].c_value) {
607 volume->v_forcegid = strdup(options[VOLOPT_FORCEGID].c_value);
609 volume->v_forcegid = NULL; /* set as null so as to return 0 later on */
613 if (options[VOLOPT_PREEXEC].c_value)
614 volume->v_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_PREEXEC].c_value, pwd, path, name);
615 volume->v_preexec_close = options[VOLOPT_PREEXEC].i_value;
617 if (options[VOLOPT_POSTEXEC].c_value)
618 volume->v_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_POSTEXEC].c_value, pwd, path, name);
620 if (options[VOLOPT_ROOTPREEXEC].c_value)
621 volume->v_root_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPREEXEC].c_value, pwd, path, name);
622 volume->v_root_preexec_close = options[VOLOPT_ROOTPREEXEC].i_value;
624 if (options[VOLOPT_ROOTPOSTEXEC].c_value)
625 volume->v_root_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPOSTEXEC].c_value, pwd, path, name);
629 volume->v_next = Volumes;
634 /* ---------------- */
635 static char *myfgets( buf, size, fp )
644 while ((EOF != ( c = getc( fp )) ) && ( size > 0 )) {
645 if ( c == '\n' || c == '\r' ) {
663 /* check access list. this function wants something of the following
665 * @group,name,name2,@group2,name3
667 * a NULL argument allows everybody to have access.
668 * we return three things:
670 * 0: list exists, but name isn't in it
674 #ifndef NO_REAL_USER_NAME
675 /* authentication is case insensitive
676 * FIXME should we do the same with group name?
678 #define access_strcmp strcasecmp
681 #define access_strcmp strcmp
685 static int accessvol(args, name)
689 char buf[MAXPATHLEN + 1], *p;
695 strlcpy(buf, args, sizeof(buf));
696 if ((p = strtok(buf, ",")) == NULL) /* nothing, return okay */
700 if (*p == '@') { /* it's a group */
701 if ((gr = getgrnam(p + 1)) && gmem(gr->gr_gid))
703 } else if (access_strcmp(p, name) == 0) /* it's a user name */
705 p = strtok(NULL, ",");
711 static void setextmap( ext, type, creator, user)
712 char *ext, *type, *creator;
718 if (Extmap == NULL) {
719 if (( Extmap = calloc(1, sizeof( struct extmap ))) == NULL ) {
720 LOG(log_error, logtype_afpd, "setextmap: calloc: %s", strerror(errno) );
725 for ( em = Extmap, cnt = 0; em->em_ext; em++, cnt++) {
726 if ( (strdiacasecmp( em->em_ext, ext )) == 0 ) {
731 if ( em->em_ext == NULL ) {
732 if (!(Extmap = realloc( Extmap, sizeof( struct extmap ) * (cnt +2))) ) {
733 LOG(log_error, logtype_afpd, "setextmap: realloc: %s", strerror(errno) );
736 (Extmap +cnt +1)->em_ext = NULL;
738 } else if ( !user ) {
744 if (!(em->em_ext = strdup( ext))) {
745 LOG(log_error, logtype_afpd, "setextmap: strdup: %s", strerror(errno) );
749 if ( *type == '\0' ) {
750 memcpy(em->em_type, "????", sizeof( em->em_type ));
752 memcpy(em->em_type, type, sizeof( em->em_type ));
754 if ( *creator == '\0' ) {
755 memcpy(em->em_creator, "UNIX", sizeof( em->em_creator ));
757 memcpy(em->em_creator, creator, sizeof( em->em_creator ));
761 /* -------------------------- */
762 static int extmap_cmp(const void *map1, const void *map2)
764 const struct extmap *em1 = map1;
765 const struct extmap *em2 = map2;
766 return strdiacasecmp(em1->em_ext, em2->em_ext);
769 static void sortextmap( void)
774 if ((em = Extmap) == NULL) {
782 qsort(Extmap, Extmap_cnt, sizeof(struct extmap), extmap_cmp);
787 /* ----------------------
789 static void free_extmap( void)
794 for ( em = Extmap; em->em_ext; em++) {
804 /* ----------------------
806 static int volfile_changed(struct afp_volume_name *p)
816 if (!stat( name, &st) && st.st_mtime > p->mtime) {
817 p->mtime = st.st_mtime;
823 /* ----------------------
824 * Read a volume configuration file and add the volumes contained within to
825 * the global volume list. If p2 is non-NULL, the file that is opened is
828 * Lines that begin with # and blank lines are ignored.
829 * Volume lines are of the form:
830 * <unix path> [<volume name>] [allow:<user>,<@group>,...] \
831 * [codepage:<file>] [casefold:<num>]
832 * <extension> TYPE [CREATOR]
834 static int readvolfile(obj, p1, p2, user, pwent)
836 struct afp_volume_name *p1;
839 struct passwd *pwent;
842 char path[ MAXPATHLEN + 1], tmp[ MAXPATHLEN + 1],
843 volname[ AFPVOL_NAMELEN + 1 ], buf[ BUFSIZ ],
844 type[ 5 ], creator[ 5 ];
847 struct vol_option options[VOLOPT_NUM], save_options[VOLOPT_NUM];
855 strcpy( path, p1->name );
862 p1->full_name = strdup(path);
865 if (NULL == ( fp = fopen( path, "r" )) ) {
869 if (fd != -1 && !fstat( fd, &st) ) {
870 p1->mtime = st.st_mtime;
873 memset(save_options, 0, sizeof(save_options));
874 while ( myfgets( buf, sizeof( buf ), fp ) != NULL ) {
875 initline( strlen( buf ), buf );
876 parseline( sizeof( path ) - 1, path );
883 /* change the default options for this file */
884 if (strncmp(path, VOLOPT_DEFAULT, VOLOPT_DEFAULT_LEN) == 0) {
886 for (i = 0; i < VOLOPT_NUM; i++) {
887 if (parseline( sizeof( path ) - VOLOPT_DEFAULT_LEN - 1,
888 path + VOLOPT_DEFAULT_LEN) < 0)
890 volset(save_options, NULL, tmp, sizeof(tmp) - 1,
891 path + VOLOPT_DEFAULT_LEN);
897 if (( p = strchr( path, '/' )) != NULL ) {
905 if ( u == NULL || *u == '\0' || ( pw = getpwnam( u )) == NULL ) {
908 strcpy( tmp, pw->pw_dir );
909 if ( p != NULL && *p != '\0' ) {
913 /* Tag a user's home directory with their umask. Note, this will
914 * be overwritten if the user actually specifies a umask: option
915 * for a '~' volume. */
916 save_options[VOLOPT_UMASK].i_value = obj->options.save_mask;
920 /* send path through variable substitution */
921 if (*path != '~') /* need to copy path to tmp */
924 pwent = getpwnam(obj->username);
925 volxlate(obj, path, sizeof(path) - 1, tmp, pwent, NULL, NULL);
927 /* this is sort of braindead. basically, i want to be
928 * able to specify things in any order, but i don't want to
929 * re-write everything.
931 * currently we have options:
939 * options:prodos,crlf,noadouble,ro...
944 * namemask:x,y,!z (not implemented yet)
946 memcpy(options, save_options, sizeof(options));
949 /* read in up to VOLOP_NUM possible options */
950 for (i = 0; i < VOLOPT_NUM; i++) {
951 if (parseline( sizeof( tmp ) - 1, tmp ) < 0)
954 volset(options, save_options, volname, sizeof(volname) - 1, tmp);
957 /* check allow/deny lists:
958 allow -> either no list (-1), or in list (1)
959 deny -> either no list (-1), or not in list (0) */
960 if (accessvol(options[VOLOPT_ALLOW].c_value, obj->username) &&
961 (accessvol(options[VOLOPT_DENY].c_value, obj->username) < 1)) {
963 /* handle read-only behaviour. semantics:
964 * 1) neither the rolist nor the rwlist exist -> rw
965 * 2) rolist exists -> ro if user is in it.
966 * 3) rwlist exists -> ro unless user is in it. */
967 if (((options[VOLOPT_FLAGS].i_value & AFPVOL_RO) == 0) &&
968 ((accessvol(options[VOLOPT_ROLIST].c_value,
969 obj->username) == 1) ||
970 !accessvol(options[VOLOPT_RWLIST].c_value,
972 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
974 /* do variable substitution for volname */
975 volxlate(obj, tmp, sizeof(tmp) - 1, volname, pwent, path, NULL);
976 creatvol(obj, pwent, path, tmp, options, p2 != NULL);
978 volfree(options, save_options);
982 parseline( sizeof( type ) - 1, type );
983 parseline( sizeof( creator ) - 1, creator );
984 setextmap( path, type, creator, user);
991 volfree(save_options, NULL);
993 if ( fclose( fp ) != 0 ) {
994 LOG(log_error, logtype_afpd, "readvolfile: fclose: %s", strerror(errno) );
1000 /* ------------------------------- */
1001 static void volume_free(struct vol *vol)
1006 free(vol->v_password);
1008 free(vol->v_volcodepage);
1009 free(vol->v_maccodepage);
1010 free(vol->v_cnidscheme);
1011 free(vol->v_dbpath);
1014 free(vol->v_forceuid);
1015 free(vol->v_forcegid);
1016 #endif /* FORCE_UIDGID */
1019 /* ------------------------------- */
1020 static void free_volumes(void )
1023 struct vol *nvol, *ovol;
1025 for ( vol = Volumes; vol; vol = vol->v_next ) {
1026 if (( vol->v_flags & AFPVOL_OPEN ) ) {
1033 for ( vol = Volumes, ovol = NULL; vol; vol = nvol) {
1036 if (vol->v_name == NULL) {
1037 if (Volumes == vol) {
1042 ovol->v_next = nvol;
1052 /* ------------------------------- */
1053 static void volume_unlink(struct vol *volume)
1055 struct vol *vol, *ovol, *nvol;
1057 if (volume == Volumes) {
1058 Volumes = Volumes->v_next;
1061 for ( vol = Volumes->v_next, ovol = Volumes; vol; vol = nvol) {
1064 if (vol == volume) {
1065 ovol->v_next = nvol;
1074 static int getvolspace( vol, bfree, btotal, xbfree, xbtotal, bsize )
1076 u_int32_t *bfree, *btotal, *bsize;
1077 VolSpace *xbfree, *xbtotal;
1081 #ifndef NO_QUOTA_SUPPORT
1082 VolSpace qfree, qtotal;
1085 spaceflag = AFPVOL_GVSMASK & vol->v_flags;
1086 /* report up to 2GB if afp version is < 2.2 (4GB if not) */
1087 maxsize = (vol->v_flags & AFPVOL_A2VOL) ? 0x01fffe00 :
1088 (((afp_version < 22) || (vol->v_flags & AFPVOL_LIMITSIZE))
1089 ? 0x7fffffffL : 0xffffffffL);
1092 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_AFSGVS ) {
1093 if ( afs_getvolspace( vol, xbfree, xbtotal, bsize ) == AFP_OK ) {
1094 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_AFSGVS;
1095 goto getvolspace_done;
1100 if (( rc = ustatfs_getvolspace( vol, xbfree, xbtotal,
1101 bsize)) != AFP_OK ) {
1105 #define min(a,b) ((a)<(b)?(a):(b))
1106 #ifndef NO_QUOTA_SUPPORT
1107 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_UQUOTA ) {
1108 if ( uquota_getvolspace( vol, &qfree, &qtotal, *bsize ) == AFP_OK ) {
1109 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_UQUOTA;
1110 *xbfree = min(*xbfree, qfree);
1111 *xbtotal = min( *xbtotal, qtotal);
1112 goto getvolspace_done;
1116 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_USTATFS;
1119 *bfree = min( *xbfree, maxsize);
1120 *btotal = min( *xbtotal, maxsize);
1124 /* -----------------------
1125 * set volume creation date
1126 * avoid duplicate, well at least it tries
1128 static void vol_setdate(u_int16_t id, struct adouble *adp, time_t date)
1131 struct vol *vol = Volumes;
1133 for ( volume = Volumes; volume; volume = volume->v_next ) {
1134 if (volume->v_vid == id) {
1137 else if (AD_DATE_FROM_UNIX(date) == volume->v_ctime) {
1139 volume = Volumes; /* restart */
1142 vol->v_ctime = AD_DATE_FROM_UNIX(date);
1143 ad_setdate(adp, AD_DATE_CREATE | AD_DATE_UNIX, date);
1146 /* ----------------------- */
1147 static int getvolparams( bitmap, vol, st, buf, buflen )
1155 int bit = 0, isad = 1;
1158 u_int32_t bfree, btotal, bsize;
1159 VolSpace xbfree, xbtotal; /* extended bytes */
1160 char *data, *nameoff = NULL;
1163 /* courtesy of jallison@whistle.com:
1164 * For MacOS8.x support we need to create the
1165 * .Parent file here if it doesn't exist. */
1167 ad_init(&ad, vol->v_adouble);
1168 if ( ad_open( vol->v_path, vol_noadouble(vol) |
1169 ADFLAGS_HF|ADFLAGS_DIR, O_RDWR | O_CREAT,
1172 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1174 } else if (ad_get_HF_flags( &ad ) & O_CREAT) {
1175 slash = strrchr( vol->v_path, '/' );
1179 slash = vol->v_path;
1181 ad_setentrylen( &ad, ADEID_NAME, strlen( slash ));
1182 memcpy(ad_entry( &ad, ADEID_NAME ), slash,
1183 ad_getentrylen( &ad, ADEID_NAME ));
1184 vol_setdate(vol->v_vid, &ad, st->st_mtime);
1185 ad_flush(&ad, ADFLAGS_HF);
1188 if (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0)
1189 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1191 vol->v_ctime = aint;
1194 if (( bitmap & ( (1<<VOLPBIT_BFREE)|(1<<VOLPBIT_BTOTAL) |
1195 (1<<VOLPBIT_XBFREE)|(1<<VOLPBIT_XBTOTAL) |
1196 (1<<VOLPBIT_BSIZE)) ) != 0 ) {
1197 if ( getvolspace( vol, &bfree, &btotal, &xbfree, &xbtotal,
1200 ad_close( &ad, ADFLAGS_HF );
1202 return( AFPERR_PARAM );
1207 while ( bitmap != 0 ) {
1208 while (( bitmap & 1 ) == 0 ) {
1216 if (0 == (vol->v_flags & AFPVOL_NOFILEID) && vol->v_cdb != NULL &&
1217 (vol->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1218 ashort = VOLPBIT_ATTR_FILEID;
1220 /* check for read-only.
1221 * NOTE: we don't actually set the read-only flag unless
1222 * it's passed in that way as it's possible to mount
1223 * a read-write filesystem under a read-only one. */
1224 if ((vol->v_flags & AFPVOL_RO) ||
1225 ((utime(vol->v_path, NULL) < 0) && (errno == EROFS))) {
1226 ashort |= VOLPBIT_ATTR_RO;
1228 ashort |= VOLPBIT_ATTR_CATSEARCH;
1229 if (afp_version >= 30) {
1230 ashort |= VOLPBIT_ATTR_UTF8;
1231 if (vol->v_flags & AFPVOL_UNIX_PRIV)
1232 ashort |= VOLPBIT_ATTR_UNIXPRIV;
1234 ashort = htons(ashort);
1235 memcpy(data, &ashort, sizeof( ashort ));
1236 data += sizeof( ashort );
1240 ashort = htons( AFPVOLSIG_DEFAULT );
1241 memcpy(data, &ashort, sizeof( ashort ));
1242 data += sizeof( ashort );
1245 case VOLPBIT_CDATE :
1246 aint = vol->v_ctime;
1247 memcpy(data, &aint, sizeof( aint ));
1248 data += sizeof( aint );
1251 case VOLPBIT_MDATE :
1252 if ( st->st_mtime > vol->v_mtime ) {
1253 vol->v_mtime = st->st_mtime;
1255 aint = AD_DATE_FROM_UNIX(vol->v_mtime);
1256 memcpy(data, &aint, sizeof( aint ));
1257 data += sizeof( aint );
1260 case VOLPBIT_BDATE :
1261 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1262 aint = AD_DATE_START;
1263 memcpy(data, &aint, sizeof( aint ));
1264 data += sizeof( aint );
1268 memcpy(data, &vol->v_vid, sizeof( vol->v_vid ));
1269 data += sizeof( vol->v_vid );
1272 case VOLPBIT_BFREE :
1273 bfree = htonl( bfree );
1274 memcpy(data, &bfree, sizeof( bfree ));
1275 data += sizeof( bfree );
1278 case VOLPBIT_BTOTAL :
1279 btotal = htonl( btotal );
1280 memcpy(data, &btotal, sizeof( btotal ));
1281 data += sizeof( btotal );
1284 #ifndef NO_LARGE_VOL_SUPPORT
1285 case VOLPBIT_XBFREE :
1286 xbfree = hton64( xbfree );
1287 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1288 bcopy(&xbfree, data, sizeof(xbfree));
1289 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1290 memcpy(data, &xbfree, sizeof( xbfree ));
1291 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1292 data += sizeof( xbfree );
1295 case VOLPBIT_XBTOTAL :
1296 xbtotal = hton64( xbtotal );
1297 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1298 bcopy(&xbtotal, data, sizeof(xbtotal));
1299 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1300 memcpy(data, &xbtotal, sizeof( xbtotal ));
1301 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1302 data += sizeof( xbfree );
1304 #endif /* ! NO_LARGE_VOL_SUPPORT */
1308 data += sizeof( u_int16_t );
1311 case VOLPBIT_BSIZE: /* block size */
1312 bsize = htonl(bsize);
1313 memcpy(data, &bsize, sizeof(bsize));
1314 data += sizeof(bsize);
1319 ad_close( &ad, ADFLAGS_HF );
1321 return( AFPERR_BITMAP );
1327 ashort = htons( data - buf );
1328 memcpy(nameoff, &ashort, sizeof( ashort ));
1329 aint = ucs2_to_charset( (utf8_encoding()?CH_UTF8_MAC:vol->v_maccharset), vol->v_name, data+1, 255);
1339 ad_close( &ad, ADFLAGS_HF );
1341 *buflen = data - buf;
1345 /* ------------------------- */
1346 int static stat_vol(u_int16_t bitmap, struct vol *vol, char *rbuf, int *rbuflen)
1351 if ( stat( vol->v_path, &st ) < 0 ) {
1353 return( AFPERR_PARAM );
1355 /* save the volume device number */
1356 vol->v_dev = st.st_dev;
1358 buflen = *rbuflen - sizeof( bitmap );
1359 if (( ret = getvolparams( bitmap, vol, &st,
1360 rbuf + sizeof( bitmap ), &buflen )) != AFP_OK ) {
1364 *rbuflen = buflen + sizeof( bitmap );
1365 bitmap = htons( bitmap );
1366 memcpy(rbuf, &bitmap, sizeof( bitmap ));
1371 /* ------------------------------- */
1372 void load_volumes(AFPObj *obj)
1374 struct passwd *pwent;
1379 /* check files date */
1380 if (obj->options.defaultvol.loaded) {
1381 changed = volfile_changed(&obj->options.defaultvol);
1383 if (obj->options.systemvol.loaded) {
1384 changed |= volfile_changed(&obj->options.systemvol);
1386 if (obj->options.uservol.loaded) {
1387 changed |= volfile_changed(&obj->options.uservol);
1396 pwent = getpwnam(obj->username);
1397 if ( (obj->options.flags & OPTION_USERVOLFIRST) == 0 ) {
1398 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent);
1401 if ((*obj->username == '\0') || (obj->options.flags & OPTION_NOUSERVOL)) {
1402 readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent);
1405 * Read user's AppleVolumes or .AppleVolumes file
1406 * If neither are readable, read the default volumes file. if
1407 * that doesn't work, create a user share.
1409 if (obj->options.uservol.name) {
1410 free(obj->options.uservol.name);
1412 obj->options.uservol.name = strdup(pwent->pw_dir);
1413 if ( readvolfile(obj, &obj->options.uservol, "AppleVolumes", 1, pwent) < 0 &&
1414 readvolfile(obj, &obj->options.uservol, ".AppleVolumes", 1, pwent) < 0 &&
1415 readvolfile(obj, &obj->options.uservol, "applevolumes", 1, pwent) < 0 &&
1416 readvolfile(obj, &obj->options.uservol, ".applevolumes", 1, pwent) < 0 &&
1417 obj->options.defaultvol.name != NULL ) {
1418 if (readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent) < 0)
1419 creatvol(obj, pwent, pwent->pw_dir, NULL, NULL, 1);
1422 if ( obj->options.flags & OPTION_USERVOLFIRST ) {
1423 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent );
1427 /* ------------------------------- */
1428 int afp_getsrvrparms(obj, ibuf, ibuflen, rbuf, rbuflen )
1431 int ibuflen, *rbuflen;
1443 for ( vcnt = 0, volume = Volumes; volume; volume = volume->v_next ) {
1444 if (!(volume->v_flags & AFPVOL_NOSTAT)) {
1445 if ( stat( volume->v_path, &st ) < 0 ) {
1446 LOG(log_info, logtype_afpd, "afp_getsrvrparms: stat %s: %s",
1447 volume->v_path, strerror(errno) );
1448 continue; /* can't access directory */
1450 if (!S_ISDIR(st.st_mode)) {
1451 continue; /* not a dir */
1454 if (volume->v_hide) {
1455 continue; /* config file changed but the volume was mounted */
1457 /* set password bit if there's a volume password */
1458 *data = (volume->v_password) ? AFPSRVR_PASSWD : 0;
1460 /* Apple 2 clients running ProDOS-8 expect one volume to have
1461 bit 0 of this byte set. They will not recognize anything
1462 on the server unless this is the case. I have not
1463 completely worked this out, but it's related to booting
1464 from the server. Support for that function is a ways
1465 off.. <shirsch@ibm.net> */
1466 *data |= (volume->v_flags & AFPVOL_A2VOL) ? AFPSRVR_CONFIGINFO : 0;
1467 *data++ |= 0; /* UNIX PRIVS BIT ..., OSX doesn't seem to use it, so we don't either */
1469 len = ucs2_to_charset_allocate((utf8_encoding()?CH_UTF8_MAC:obj->options.maccharset),
1470 &namebuf, volume->v_name);
1474 memcpy(data, namebuf, len );
1480 *rbuflen = data - rbuf;
1482 if ( gettimeofday( &tv, 0 ) < 0 ) {
1483 LOG(log_error, logtype_afpd, "afp_getsrvrparms: gettimeofday: %s", strerror(errno) );
1485 return AFPERR_PARAM;
1487 tv.tv_sec = AD_DATE_FROM_UNIX(tv.tv_sec);
1488 memcpy(data, &tv.tv_sec, sizeof( u_int32_t));
1489 data += sizeof( u_int32_t);
1494 /* -------------------------
1495 * we are the user here
1497 int afp_openvol(obj, ibuf, ibuflen, rbuf, rbuflen )
1500 int ibuflen, *rbuflen;
1514 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1515 bitmap = ntohs( bitmap );
1516 ibuf += sizeof( bitmap );
1517 if (( bitmap & (1<<VOLPBIT_VID)) == 0 ) {
1519 return AFPERR_BITMAP;
1522 len = (unsigned char)*ibuf++;
1523 volname = obj->oldtmp;
1524 namelen = convert_string( (utf8_encoding()?CH_UTF8_MAC:obj->options.maccharset), CH_UCS2,
1525 ibuf, len, volname, sizeof(obj->oldtmp));
1528 return AFPERR_PARAM;
1532 if ((len + 1) & 1) /* pad to an even boundary */
1537 for ( volume = Volumes; volume; volume = volume->v_next ) {
1538 if ( strcasecmp_w( (ucs2_t*) volname, volume->v_name ) == 0 ) {
1543 if ( volume == NULL ) {
1545 return AFPERR_PARAM;
1548 /* check for a volume password */
1549 if (volume->v_password && strncmp(ibuf, volume->v_password, VOLPASSLEN)) {
1551 return AFPERR_ACCESS;
1554 if (( volume->v_flags & AFPVOL_OPEN ) ) {
1555 /* the volume is already open */
1557 set_uidgid ( volume );
1559 return stat_vol(bitmap, volume, rbuf, rbuflen);
1562 /* initialize volume variables
1565 if (afp_version >= 30) {
1566 volume->max_filename = 255;
1569 volume->max_filename = MACFILELEN;
1572 volume->v_dir = volume->v_root = NULL;
1574 /* FIXME unix name != mac name */
1576 len = convert_string_allocate( CH_UCS2, (utf8_encoding()?CH_UTF8_MAC:obj->options.maccharset),
1577 volume->v_name, namelen, &vol_mname);
1578 if ( !vol_mname || len <= 0) {
1583 len = convert_string_allocate( CH_UCS2 , volume->v_volcharset, volume->v_name, namelen, &vol_uname);
1585 if ( !vol_uname || len <= 0) {
1591 if ((dir = dirnew(vol_mname, vol_uname) ) == NULL) {
1594 LOG(log_error, logtype_afpd, "afp_openvol: malloc: %s", strerror(errno) );
1601 dir->d_did = DIRDID_ROOT;
1602 dir->d_color = DIRTREE_COLOR_BLACK; /* root node is black */
1603 volume->v_dir = volume->v_root = dir;
1604 volume->v_flags |= AFPVOL_OPEN;
1605 volume->v_cdb = NULL;
1607 if (volume->v_root_preexec) {
1608 if ((ret = afprun(1, volume->v_root_preexec, NULL)) && volume->v_root_preexec_close) {
1609 LOG(log_error, logtype_afpd, "afp_openvol: root preexec : %d", ret );
1616 set_uidgid ( volume );
1619 if (volume->v_preexec) {
1620 if ((ret = afprun(0, volume->v_preexec, NULL)) && volume->v_preexec_close) {
1621 LOG(log_error, logtype_afpd, "afp_openvol: preexec : %d", ret );
1627 if ( stat( volume->v_path, &st ) < 0 ) {
1632 if ( chdir( volume->v_path ) < 0 ) {
1636 curdir = volume->v_dir;
1637 if (volume->v_cnidscheme == NULL) {
1638 volume->v_cnidscheme = strdup(DEFAULT_CNID_SCHEME);
1639 LOG(log_warning, logtype_afpd, "Warning: No CNID scheme for volume %s. Using default.",
1642 if (volume->v_dbpath)
1643 volume->v_cdb = cnid_open (volume->v_dbpath, volume->v_umask, volume->v_cnidscheme, (volume->v_flags & AFPVOL_NODEV));
1645 volume->v_cdb = cnid_open (volume->v_path, volume->v_umask, volume->v_cnidscheme, (volume->v_flags & AFPVOL_NODEV));
1646 if (volume->v_cdb == NULL) {
1647 LOG(log_error, logtype_afpd, "Fatal error: cannot open CNID or invalid CNID backend for %s: %s",
1648 volume->v_path, volume->v_cnidscheme);
1655 if (!volume->v_volcodepage)
1656 volume->v_volcodepage = strdup("UTF8");
1658 if ( (charset_t) -1 == ( volume->v_volcharset = add_charset(volume->v_volcodepage)) ) {
1659 LOG (log_error, logtype_afpd, "Setting codepage %s as volume codepage failed", volume->v_volcodepage);
1664 if ( NULL == ( volume->v_vol = find_charset_functions(volume->v_volcodepage)) || volume->v_vol->flags & CHARSET_ICONV ) {
1665 LOG (log_warning, logtype_afpd, "WARNING: volume encoding %s is *not* supported by netatalk, expect problems !!!!", volume->v_volcodepage);
1668 if (!volume->v_maccodepage)
1669 volume->v_maccodepage = strdup(obj->options.maccodepage);
1671 if ( (charset_t) -1 == ( volume->v_maccharset = add_charset(volume->v_maccodepage)) ) {
1672 LOG (log_error, logtype_afpd, "Setting codepage %s as mac codepage failed", volume->v_maccodepage);
1677 if ( NULL == ( volume->v_mac = find_charset_functions(volume->v_maccodepage)) || ! (volume->v_mac->flags & CHARSET_CLIENT) ) {
1678 LOG (log_error, logtype_afpd, "Fatal error: mac charset %s not supported", volume->v_maccodepage);
1683 ret = stat_vol(bitmap, volume, rbuf, rbuflen);
1684 if (ret == AFP_OK) {
1686 handle_special_folders( volume );
1689 * If you mount a volume twice, the second time the trash appears on
1690 * the desk-top. That's because the Mac remembers the DID for the
1691 * trash (even for volumes in different zones, on different servers).
1692 * Just so this works better, we prime the DID cache with the trash,
1693 * fixing the trash at DID 17.
1694 * FIXME (RL): should it be done inside a CNID backend ? (always returning Trash DID when asked) ?
1696 if ((volume->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1698 /* FIXME find db time stamp */
1699 if (cnid_getstamp(volume->v_cdb, volume->v_stamp, sizeof(volume->v_stamp)) < 0) {
1700 LOG (log_error, logtype_afpd, "Fatal error: Unable to get stamp value from CNID backend");
1706 cname( volume, volume->v_dir, &p );
1712 if (volume->v_dir) {
1713 dirfree( volume->v_dir );
1714 volume->v_dir = volume->v_root = NULL;
1717 volume->v_flags &= ~AFPVOL_OPEN;
1718 if (volume->v_cdb != NULL) {
1719 cnid_close(volume->v_cdb);
1720 volume->v_cdb = NULL;
1726 /* ------------------------- */
1727 static void closevol(struct vol *vol)
1732 dirfree( vol->v_root );
1734 if (vol->v_cdb != NULL) {
1735 cnid_close(vol->v_cdb);
1739 if (vol->v_postexec) {
1740 afprun(0, vol->v_postexec, NULL);
1742 if (vol->v_root_postexec) {
1743 afprun(1, vol->v_root_postexec, NULL);
1747 /* ------------------------- */
1748 void close_all_vol(void)
1752 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
1753 if ( ovol->v_flags & AFPVOL_OPEN ) {
1754 ovol->v_flags &= ~AFPVOL_OPEN;
1760 /* ------------------------- */
1761 int afp_closevol(obj, ibuf, ibuflen, rbuf, rbuflen )
1764 int ibuflen, *rbuflen;
1766 struct vol *vol, *ovol;
1771 memcpy(&vid, ibuf, sizeof( vid ));
1772 if (NULL == ( vol = getvolbyvid( vid )) ) {
1773 return( AFPERR_PARAM );
1776 vol->v_flags &= ~AFPVOL_OPEN;
1777 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
1778 if ( ovol->v_flags & AFPVOL_OPEN ) {
1782 if ( ovol != NULL ) {
1783 /* Even if chdir fails, we can't say afp_closevol fails. */
1784 if ( chdir( ovol->v_path ) == 0 ) {
1785 curdir = ovol->v_dir;
1790 if (vol->v_deleted) {
1791 showvol(vol->v_name);
1799 /* ------------------------- */
1800 struct vol *getvolbyvid(const u_int16_t vid )
1804 for ( vol = Volumes; vol; vol = vol->v_next ) {
1805 if ( vid == vol->v_vid ) {
1809 if ( vol == NULL || ( vol->v_flags & AFPVOL_OPEN ) == 0 ) {
1815 #endif /* FORCE_UIDGID */
1820 /* ------------------------ */
1821 static int ext_cmp_key(const void *key, const void *obj)
1823 const char *p = key;
1824 const struct extmap *em = obj;
1825 return strdiacasecmp(p, em->em_ext);
1827 struct extmap *getextmap(const char *path)
1832 if (NULL == ( p = strrchr( path, '.' )) ) {
1833 return( Defextmap );
1836 if (!*p || !Extmap_cnt) {
1837 return( Defextmap );
1839 em = bsearch(p, Extmap, Extmap_cnt, sizeof(struct extmap), ext_cmp_key);
1843 return( Defextmap );
1847 /* ------------------------- */
1848 struct extmap *getdefextmap(void)
1850 return( Defextmap );
1853 /* --------------------------
1854 poll if a volume is changed by other processes.
1856 int pollvoltime(obj)
1863 if (!(afp_version > 21 && obj->options.server_notif))
1866 if ( gettimeofday( &tv, 0 ) < 0 )
1869 for ( vol = Volumes; vol; vol = vol->v_next ) {
1870 if ( (vol->v_flags & AFPVOL_OPEN) && vol->v_mtime + 30 < tv.tv_sec) {
1871 if ( !stat( vol->v_path, &st ) && vol->v_mtime != st.st_mtime ) {
1872 vol->v_mtime = st.st_mtime;
1873 if (!obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED))
1882 /* ------------------------- */
1883 void setvoltime(obj, vol )
1889 /* just looking at vol->v_mtime is broken seriously since updates
1890 * from other users afpd processes never are seen.
1891 * This is not the most elegant solution (a shared memory between
1892 * the afpd processes would come closer)
1895 if ( gettimeofday( &tv, 0 ) < 0 ) {
1896 LOG(log_error, logtype_afpd, "setvoltime: gettimeofday: %s", strerror(errno) );
1899 if( utime( vol->v_path, NULL ) < 0 ) {
1900 /* write of time failed ... probably a read only filesys,
1901 * where no other users can interfere, so there's no issue here
1905 /* a little granularity */
1906 if (vol->v_mtime < tv.tv_sec) {
1907 vol->v_mtime = tv.tv_sec;
1909 /* if 0ed, we're sending too many */
1910 if (afp_version > 21 && obj->options.server_notif) {
1911 obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED);
1917 /* ------------------------- */
1918 int afp_getvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1921 int ibuflen, *rbuflen;
1924 u_int16_t vid, bitmap;
1927 memcpy(&vid, ibuf, sizeof( vid ));
1928 ibuf += sizeof( vid );
1929 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1930 bitmap = ntohs( bitmap );
1932 if (NULL == ( vol = getvolbyvid( vid )) ) {
1934 return( AFPERR_PARAM );
1937 return stat_vol(bitmap, vol, rbuf, rbuflen);
1940 /* ------------------------- */
1941 int afp_setvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
1944 int ibuflen, *rbuflen;
1948 u_int16_t vid, bitmap;
1954 memcpy(&vid, ibuf, sizeof( vid ));
1955 ibuf += sizeof( vid );
1956 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1957 bitmap = ntohs( bitmap );
1958 ibuf += sizeof(bitmap);
1960 if (( vol = getvolbyvid( vid )) == NULL ) {
1961 return( AFPERR_PARAM );
1964 if (vol->v_flags & AFPVOL_RO)
1965 return AFPERR_VLOCK;
1967 /* we can only set the backup date. */
1968 if (bitmap != (1 << VOLPBIT_BDATE))
1969 return AFPERR_BITMAP;
1971 ad_init(&ad, vol->v_adouble);
1972 if ( ad_open( vol->v_path, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR,
1975 return AFPERR_VLOCK;
1977 return AFPERR_ACCESS;
1980 memcpy(&aint, ibuf, sizeof(aint));
1981 ad_setdate(&ad, AD_DATE_BACKUP, aint);
1982 ad_flush(&ad, ADFLAGS_HF);
1983 ad_close(&ad, ADFLAGS_HF);
1987 /* ------------------------- */
1988 int wincheck(const struct vol *vol, const char *path)
1992 if (!(vol->v_flags & AFPVOL_MSWINDOWS))
1995 /* empty paths are not allowed */
1996 if ((len = strlen(path)) == 0)
1999 /* leading or trailing whitespaces are not allowed, carriage returns
2000 * and probably other whitespace is okay, tabs are not allowed
2002 if ((path[0] == ' ') || (path[len-1] == ' '))
2005 /* certain characters are not allowed */
2006 if (strpbrk(path, MSWINDOWS_BADCHARS))
2009 /* everything else is okay */
2015 * precreate a folder
2016 * this is only intended for folders in the volume root
2017 * It will *not* work if the folder name contains extended characters
2019 static int create_special_folder (const struct vol *vol, const struct _special_folder *folder)
2028 p = (char *) malloc ( strlen(vol->v_path)+strlen(folder->name)+2);
2030 LOG(log_error, logtype_afpd,"malloc failed");
2033 strcpy(p, vol->v_path);
2035 strcat(p, folder->name);
2037 if ( (ret = stat( p, &st )) < 0 ) {
2038 if (folder->precreate) {
2039 if (ad_mkdir(p, folder->mode)) {
2040 LOG(log_debug, logtype_afpd,"Creating '%s' failed", folder->name);
2048 if ( !ret && folder->hide) {
2050 ad_init(&ad, vol->v_adouble);
2051 if (ad_open( p, vol_noadouble(vol) | ADFLAGS_HF|ADFLAGS_DIR,
2052 O_RDWR|O_CREAT, 0666, &ad) < 0) {
2056 if ((ad_get_HF_flags( &ad ) & O_CREAT) ) {
2057 ad_setentrylen( &ad, ADEID_NAME, strlen(folder->name));
2058 memcpy(ad_entry( &ad, ADEID_NAME ), folder->name,
2059 ad_getentrylen( &ad, ADEID_NAME ));
2062 ad_getattr(&ad, &attr);
2063 attr |= htons( ntohs( attr ) | ATTRBIT_INVISIBLE );
2064 ad_setattr(&ad, attr);
2066 ad_flush( &ad, ADFLAGS_HF );
2067 ad_close( &ad, ADFLAGS_HF );
2073 static void handle_special_folders (const struct vol * vol)
2075 const _special_folder *p = &special_folders[0];
2077 if (vol->v_flags & AFPVOL_RO)
2080 for (; p->name != NULL; p++) {
2081 create_special_folder (vol, p);