2 * $Id: volume.c,v 1.51.2.7.2.33.2.10 2005-09-28 09:53:47 didg Exp $
4 * Copyright (c) 1990,1993 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
10 #endif /* HAVE_CONFIG_H */
26 #else /* STDC_HEADERS */
30 #endif /* HAVE_STRCHR */
31 char *strchr (), *strrchr ();
33 #define memcpy(d,s,n) bcopy ((s), (d), (n))
34 #define memmove(d,s,n) bcopy ((s), (d), (n))
35 #endif /* ! HAVE_MEMCPY */
36 #endif /* STDC_HEADERS */
37 #include <sys/param.h>
38 #include <sys/socket.h>
39 #include <netinet/in.h>
40 #include <arpa/inet.h>
41 #include <atalk/asp.h>
42 #include <atalk/dsi.h>
43 #include <atalk/adouble.h>
44 #include <atalk/afp.h>
45 #include <atalk/util.h>
46 #include <atalk/logger.h>
48 #include <atalk/cnid.h>
52 #include "directory.h"
57 extern int afprun(int root, char *cmd, int *outfd);
60 #define MIN(a, b) ((a) < (b) ? (a) : (b))
63 #ifndef NO_LARGE_VOL_SUPPORT
64 #if BYTE_ORDER == BIG_ENDIAN
67 #else /* BYTE_ORDER == BIG_ENDIAN */
68 #define hton64(x) ((u_int64_t) (htonl(((x) >> 32) & 0xffffffffLL)) | \
69 (u_int64_t) ((htonl(x) & 0xffffffffLL) << 32))
70 #define ntoh64(x) (hton64(x))
71 #endif /* BYTE_ORDER == BIG_ENDIAN */
72 #endif /* ! NO_LARGE_VOL_SUPPORT */
74 static struct vol *Volumes = NULL;
75 static u_int16_t lastvid = 0;
76 static char *Trash = "\02\024Network Trash Folder";
78 static struct extmap *Extmap = NULL, *Defextmap = NULL;
79 static int Extmap_cnt;
80 static void free_extmap(void);
82 #define VOLOPT_ALLOW 0 /* user allow list */
83 #define VOLOPT_DENY 1 /* user deny list */
84 #define VOLOPT_RWLIST 2 /* user rw list */
85 #define VOLOPT_ROLIST 3 /* user ro list */
86 #define VOLOPT_PASSWORD 4 /* volume password */
87 #define VOLOPT_CASEFOLD 5 /* character case mangling */
88 #define VOLOPT_FLAGS 6 /* various flags */
89 #define VOLOPT_DBPATH 7 /* path to database */
90 #define VOLOPT_MAPCHARS 8 /* does mtou and utom mappings. syntax:
91 m and u can be double-byte hex
96 !u -> make u illegal always
97 ~u -> make u illegal only as the first
98 part of a double-byte character.
100 #define VOLOPT_VETO 10 /* list of veto filespec */
101 #define VOLOPT_PREEXEC 11 /* preexec command */
102 #define VOLOPT_ROOTPREEXEC 12 /* root preexec command */
104 #define VOLOPT_POSTEXEC 13 /* postexec command */
105 #define VOLOPT_ROOTPOSTEXEC 14 /* root postexec command */
107 #define VOLOPT_ENCODING 15 /* mac encoding (pre OSX)*/
108 #define VOLOPT_MACCHARSET 16
109 #define VOLOPT_CNIDSCHEME 17
110 #define VOLOPT_ADOUBLE 18 /* adouble version */
115 #define VOLOPT_FORCEUID 19 /* force uid for username x */
116 #define VOLOPT_FORCEGID 20 /* force gid for group x */
117 #define VOLOPT_UMASK 21
118 #define VOLOPT_DFLTPERM 22
120 #define VOLOPT_UMASK 19
121 #define VOLOPT_DFLTPERM 20
122 #endif /* FORCE_UIDGID */
124 #define VOLOPT_MAX (VOLOPT_DFLTPERM +1)
126 #define VOLOPT_NUM (VOLOPT_MAX + 1)
129 #define VOLOPT_DEFAULT ":DEFAULT:"
130 #define VOLOPT_DEFAULT_LEN 9
136 typedef struct _special_folder {
143 static const _special_folder special_folders[] = {
144 {"Network Trash Folder", 1, 0777, 1},
145 {"Temporary Items", 1, 0777, 1},
146 {".AppleDesktop", 1, 0777, 0},
148 {"TheFindByContentFolder", 0, 0, 1},
149 {"TheVolumeSettingsFolder", 0, 0, 1},
153 typedef struct _volopt_name {
154 const u_int32_t option;
158 static const _vol_opt_name vol_opt_names[] = {
159 {AFPVOL_A2VOL, "PRODOS"}, /* prodos volume */
160 {AFPVOL_CRLF, "CRLF"}, /* cr/lf translation */
161 {AFPVOL_NOADOUBLE, "NOADOUBLE"}, /* don't create .AppleDouble by default */
162 {AFPVOL_RO, "READONLY"}, /* read-only volume */
163 {AFPVOL_MSWINDOWS, "MSWINDOWS"}, /* deal with ms-windows yuckiness. this is going away. */
164 {AFPVOL_NOHEX, "NOHEX"}, /* don't do :hex translation */
165 {AFPVOL_USEDOTS, "USEDOTS"}, /* use real dots */
166 {AFPVOL_LIMITSIZE, "LIMITSIZE"}, /* limit size for older macs */
167 {AFPVOL_MAPASCII, "MAPASCII"}, /* map the ascii range as well */
168 {AFPVOL_DROPBOX, "DROPBOX"}, /* dropkludge dropbox support */
169 {AFPVOL_NOFILEID, "NOFILEID"}, /* don't advertise createid resolveid and deleteid calls */
170 {AFPVOL_NOSTAT, "NOSTAT"}, /* advertise the volume even if we can't stat() it
171 * maybe because it will be mounted later in preexec */
172 {AFPVOL_UNIX_PRIV, "UNIXPRIV"}, /* support unix privileges */
173 {AFPVOL_NODEV, "NODEV"}, /* always use 0 for device number in cnid calls */
177 static const _vol_opt_name vol_opt_casefold[] = {
178 {AFPVOL_MTOUUPPER, "MTOULOWER"},
179 {AFPVOL_MTOULOWER, "MTOULOWER"},
180 {AFPVOL_UTOMUPPER, "UTOMUPPER"},
181 {AFPVOL_UTOMLOWER, "UTOMLOWER"},
185 static void handle_special_folders (const struct vol *);
186 static int savevoloptions (const struct vol *);
188 static __inline__ void volfree(struct vol_option *options,
189 const struct vol_option *save)
194 for (i = 0; i < VOLOPT_MAX; i++) {
195 if (options[i].c_value && (options[i].c_value != save[i].c_value))
196 free(options[i].c_value);
199 for (i = 0; i < VOLOPT_MAX; i++) {
200 if (options[i].c_value)
201 free(options[i].c_value);
207 /* handle variable substitutions. here's what we understand:
208 * $b -> basename of path
209 * $c -> client ip/appletalk address
210 * $d -> volume pathname on server
211 * $f -> full name (whatever's in the gecos field)
214 * $i -> client ip/appletalk address without port
215 * $s -> server name (hostname if it doesn't exist)
216 * $u -> username (guest is usually nobody)
217 * $v -> volume name or basename if null
218 * $z -> zone (may not exist)
223 #define is_var(a, b) (strncmp((a), (b), 2) == 0)
225 static char *volxlate(AFPObj *obj, char *dest, size_t destlen,
226 char *src, struct passwd *pwd, char *path, char *volname)
236 dest = calloc(destlen +1, 1);
242 strlcpy(dest, src, destlen +1);
243 if ((p = strchr(src, '$')) == NULL) /* nothing to do */
246 /* first part of the path. just forward to the next variable. */
247 len = MIN((size_t)(p - src), destlen);
253 while (p && destlen > 0) {
254 /* now figure out what the variable is */
256 if (is_var(p, "$b")) {
258 if ((q = strrchr(path, '/')) == NULL)
260 else if (*(q + 1) != '\0')
263 } else if (is_var(p, "$c")) {
264 if (obj->proto == AFPPROTO_ASP) {
265 ASP asp = obj->handle;
267 len = sprintf(dest, "%u.%u", ntohs(asp->asp_sat.sat_addr.s_net),
268 asp->asp_sat.sat_addr.s_node);
272 } else if (obj->proto == AFPPROTO_DSI) {
273 DSI *dsi = obj->handle;
275 len = sprintf(dest, "%s:%u", inet_ntoa(dsi->client.sin_addr),
276 ntohs(dsi->client.sin_port));
280 } else if (is_var(p, "$d")) {
282 } else if (is_var(p, "$f")) {
283 if ((q = strchr(pwd->pw_gecos, ',')))
286 } else if (is_var(p, "$g")) {
287 struct group *grp = getgrgid(pwd->pw_gid);
290 } else if (is_var(p, "$h")) {
291 q = obj->options.hostname;
292 } else if (is_var(p, "$i")) {
293 if (obj->proto == AFPPROTO_ASP) {
294 ASP asp = obj->handle;
296 len = sprintf(dest, "%u", ntohs(asp->asp_sat.sat_addr.s_net));
300 } else if (obj->proto == AFPPROTO_DSI) {
301 DSI *dsi = obj->handle;
303 q = inet_ntoa(dsi->client.sin_addr);
305 } else if (is_var(p, "$s")) {
308 else if (obj->options.server) {
309 q = obj->options.server;
311 q = obj->options.hostname;
312 } else if (is_var(p, "$u")) {
314 } else if (is_var(p, "$v")) {
319 if ((q = strrchr(path, '/')) == NULL)
321 else if (*(q + 1) != '\0')
324 } else if (is_var(p, "$z")) {
326 } else if (is_var(p, "$$")) {
331 /* copy the stuff over. if we don't understand something that we
332 * should, just skip it over. */
334 len = MIN(p == q ? 2 : strlen(q), destlen);
335 strncpy(dest, q, len);
340 /* stuff up to next $ */
342 p = strchr(src, '$');
343 len = p ? MIN((size_t)(p - src), destlen) : destlen;
345 strncpy(dest, src, len);
353 /* to make sure that val is valid, make sure to select an opt that
355 static int optionok(const char *buf, const char *opt, const char *val)
357 if (!strstr(buf,opt))
365 /* -------------------- */
366 static void setoption(struct vol_option *options, struct vol_option *save, int opt, const char *val)
368 if (options[opt].c_value && (!save || options[opt].c_value != save[opt].c_value))
369 free(options[opt].c_value);
370 options[opt].c_value = strdup(val + 1);
373 /* ------------------------------------------
374 handle all the options. tmp can't be NULL. */
375 static void volset(struct vol_option *options, struct vol_option *save,
376 char *volname, int vlen,
381 val = strchr(tmp, ':');
383 /* we'll assume it's a volume name. */
384 strncpy(volname, tmp, vlen);
389 LOG(log_debug, logtype_afpd, "Parsing volset %s", val);
391 if (optionok(tmp, "allow:", val)) {
392 setoption(options, save, VOLOPT_ALLOW, val);
394 } else if (optionok(tmp, "deny:", val)) {
395 setoption(options, save, VOLOPT_DENY, val);
397 } else if (optionok(tmp, "rwlist:", val)) {
398 setoption(options, save, VOLOPT_RWLIST, val);
400 } else if (optionok(tmp, "rolist:", val)) {
401 setoption(options, save, VOLOPT_ROLIST, val);
403 } else if (optionok(tmp, "codepage:", val)) {
404 LOG (log_error, logtype_afpd, "The old codepage system has been removed. Please make sure to read the documentation !!!!");
405 /* Make sure we don't screw anything */
407 } else if (optionok(tmp, "volcharset:", val)) {
408 setoption(options, save, VOLOPT_ENCODING, val);
409 } else if (optionok(tmp, "maccharset:", val)) {
410 setoption(options, save, VOLOPT_MACCHARSET, val);
411 } else if (optionok(tmp, "veto:", val)) {
412 setoption(options, save, VOLOPT_VETO, val);
413 } else if (optionok(tmp, "cnidscheme:", val)) {
414 setoption(options, save, VOLOPT_CNIDSCHEME, val);
415 } else if (optionok(tmp, "casefold:", val)) {
416 if (strcasecmp(val + 1, "tolower") == 0)
417 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMLOWER;
418 else if (strcasecmp(val + 1, "toupper") == 0)
419 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMUPPER;
420 else if (strcasecmp(val + 1, "xlatelower") == 0)
421 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UUPPERMLOWER;
422 else if (strcasecmp(val + 1, "xlateupper") == 0)
423 options[VOLOPT_CASEFOLD].i_value = AFPVOL_ULOWERMUPPER;
424 } else if (optionok(tmp, "adouble:", val)) {
425 if (strcasecmp(val + 1, "v1") == 0)
426 options[VOLOPT_ADOUBLE].i_value = AD_VERSION1;
427 #if AD_VERSION == AD_VERSION2
428 else if (strcasecmp(val + 1, "v2") == 0)
429 options[VOLOPT_ADOUBLE].i_value = AD_VERSION2;
430 else if (strcasecmp(val + 1, "osx") == 0)
431 options[VOLOPT_ADOUBLE].i_value = AD_VERSION2_OSX;
433 } else if (optionok(tmp, "options:", val)) {
436 if ((p = strtok(val + 1, ",")) == NULL) /* nothing */
440 if (strcasecmp(p, "prodos") == 0)
441 options[VOLOPT_FLAGS].i_value |= AFPVOL_A2VOL;
442 else if (strcasecmp(p, "mswindows") == 0) {
443 options[VOLOPT_FLAGS].i_value |= AFPVOL_MSWINDOWS | AFPVOL_USEDOTS;
444 } else if (strcasecmp(p, "crlf") == 0)
445 options[VOLOPT_FLAGS].i_value |= AFPVOL_CRLF;
446 else if (strcasecmp(p, "noadouble") == 0)
447 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOADOUBLE;
448 else if (strcasecmp(p, "ro") == 0)
449 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
450 else if (strcasecmp(p, "nohex") == 0)
451 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOHEX;
452 else if (strcasecmp(p, "usedots") == 0)
453 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS;
454 else if (strcasecmp(p, "limitsize") == 0)
455 options[VOLOPT_FLAGS].i_value |= AFPVOL_LIMITSIZE;
456 /* support for either "dropbox" or "dropkludge" */
457 else if (strcasecmp(p, "dropbox") == 0)
458 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
459 else if (strcasecmp(p, "dropkludge") == 0)
460 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
461 else if (strcasecmp(p, "nofileid") == 0)
462 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOFILEID;
463 else if (strcasecmp(p, "nostat") == 0)
464 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOSTAT;
465 else if (strcasecmp(p, "preexec_close") == 0)
466 options[VOLOPT_PREEXEC].i_value = 1;
467 else if (strcasecmp(p, "root_preexec_close") == 0)
468 options[VOLOPT_ROOTPREEXEC].i_value = 1;
469 else if (strcasecmp(p, "upriv") == 0)
470 options[VOLOPT_FLAGS].i_value |= AFPVOL_UNIX_PRIV;
471 else if (strcasecmp(p, "nodev") == 0)
472 options[VOLOPT_FLAGS].i_value |= AFPVOL_NODEV;
473 else if (strcasecmp(p, "cachecnid") == 0)
474 options[VOLOPT_FLAGS].i_value |= AFPVOL_CACHE;
476 p = strtok(NULL, ",");
479 } else if (optionok(tmp, "dbpath:", val)) {
480 setoption(options, save, VOLOPT_DBPATH, val);
482 } else if (optionok(tmp, "umask:", val)) {
483 options[VOLOPT_UMASK].i_value = (int)strtol(val +1, NULL, 8);
484 } else if (optionok(tmp, "perm:", val)) {
485 options[VOLOPT_DFLTPERM].i_value = (int)strtol(val+1, NULL, 8);
486 } else if (optionok(tmp, "mapchars:",val)) {
487 setoption(options, save, VOLOPT_MAPCHARS, val);
489 } else if (optionok(tmp, "password:", val)) {
490 setoption(options, save, VOLOPT_PASSWORD, val);
494 /* this code allows forced uid/gid per volume settings */
495 } else if (optionok(tmp, "forceuid:", val)) {
496 setoption(options, save, VOLOPT_FORCEUID, val);
497 } else if (optionok(tmp, "forcegid:", val)) {
498 setoption(options, save, VOLOPT_FORCEGID, val);
500 #endif /* FORCE_UIDGID */
501 } else if (optionok(tmp, "root_preexec:", val)) {
502 setoption(options, save, VOLOPT_ROOTPREEXEC, val);
504 } else if (optionok(tmp, "preexec:", val)) {
505 setoption(options, save, VOLOPT_PREEXEC, val);
507 } else if (optionok(tmp, "root_postexec:", val)) {
508 setoption(options, save, VOLOPT_ROOTPOSTEXEC, val);
510 } else if (optionok(tmp, "postexec:", val)) {
511 setoption(options, save, VOLOPT_POSTEXEC, val);
514 /* ignore unknown options */
515 LOG(log_debug, logtype_afpd, "ignoring unknown volume option: %s", tmp);
520 /* ----------------- */
521 static void showvol(const ucs2_t *name)
524 for ( volume = Volumes; volume; volume = volume->v_next ) {
525 if (volume->v_hide && !strcasecmp_w( volume->v_name, name ) ) {
533 * FIXME should be define elsewhere
535 static int validupath_adouble(const struct vol *vol, const char *name)
537 return (vol->v_flags & AFPVOL_USEDOTS) ? strncasecmp(name,".Apple", 6) && strcasecmp(name, ".Parent")
541 /* ----------------- */
542 static int validupath_osx(const struct vol *vol _U_, const char *name)
544 return strncasecmp(name,".Apple", 6) && strncasecmp(name,"._", 2);
547 /* ---------------- */
548 static void initvoladouble(struct vol *vol)
550 if (vol->v_adouble == AD_VERSION2_OSX) {
551 vol->validupath = validupath_osx;
552 vol->ad_path = ad_path_osx;
555 vol->validupath = validupath_adouble;
556 vol->ad_path = ad_path;
560 /* ------------------------------- */
561 static int creatvol(AFPObj *obj, struct passwd *pwd,
562 char *path, char *name,
563 struct vol_option *options,
564 const int user /* user defined volume */
572 if ( name == NULL || *name == '\0' ) {
573 if ((name = strrchr( path, '/' )) == NULL) {
574 return -1; /* Obviously not a fully qualified path */
577 /* if you wish to share /, you need to specify a name. */
582 vlen = strlen( name );
583 if ( vlen > AFPVOL_NAMELEN ) {
584 vlen = AFPVOL_NAMELEN;
585 name[AFPVOL_NAMELEN] = '\0';
588 /* convert name to UCS2 first */
589 if ( 0 >= ( vlen = convert_string(obj->options.unixcharset, CH_UCS2, name, vlen, tmpname, 512)) )
592 for ( volume = Volumes; volume; volume = volume->v_next ) {
593 if ( strcasecmp_w( volume->v_name, tmpname ) == 0 ) {
594 if (volume->v_deleted) {
598 return -1; /* Won't be able to access it, anyway... */
604 if (!( volume = (struct vol *)calloc(1, sizeof( struct vol ))) ) {
605 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
608 if ( NULL == ( volume->v_name = strdup_w(tmpname))) {
609 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
613 if (!( volume->v_path = (char *)malloc( strlen( path ) + 1 )) ) {
614 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
615 free(volume->v_name);
619 volume->v_hide = hide;
620 strcpy( volume->v_path, path );
624 #endif /* __svr4__ */
625 /* os X start at 1 and use network order ie. 1 2 3 */
626 volume->v_vid = ++lastvid;
627 volume->v_vid = htons(volume->v_vid);
631 /* should we casefold? */
632 volume->v_casefold = options[VOLOPT_CASEFOLD].i_value;
634 /* shift in some flags */
635 volume->v_flags = options[VOLOPT_FLAGS].i_value;
637 volume->v_ad_options = 0;
638 if ((volume->v_flags & AFPVOL_NODEV))
639 volume->v_ad_options |= ADVOL_NODEV;
640 if ((volume->v_flags & AFPVOL_CACHE))
641 volume->v_ad_options |= ADVOL_CACHE;
642 if ((volume->v_flags & AFPVOL_UNIX_PRIV))
643 volume->v_ad_options |= ADVOL_UNIXPRIV;
645 if (options[VOLOPT_PASSWORD].c_value)
646 volume->v_password = strdup(options[VOLOPT_PASSWORD].c_value);
648 if (options[VOLOPT_VETO].c_value)
649 volume->v_veto = strdup(options[VOLOPT_VETO].c_value);
651 if (options[VOLOPT_ENCODING].c_value)
652 volume->v_volcodepage = strdup(options[VOLOPT_ENCODING].c_value);
654 if (options[VOLOPT_MACCHARSET].c_value)
655 volume->v_maccodepage = strdup(options[VOLOPT_MACCHARSET].c_value);
657 if (options[VOLOPT_DBPATH].c_value)
658 volume->v_dbpath = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_DBPATH].c_value, pwd, path, name);
660 if (options[VOLOPT_CNIDSCHEME].c_value)
661 volume->v_cnidscheme = strdup(options[VOLOPT_CNIDSCHEME].c_value);
663 if (options[VOLOPT_UMASK].i_value)
664 volume->v_umask = (mode_t)options[VOLOPT_UMASK].i_value;
666 if (options[VOLOPT_DFLTPERM].i_value)
667 volume->v_perm = (mode_t)options[VOLOPT_DFLTPERM].i_value;
669 if (options[VOLOPT_ADOUBLE].i_value)
670 volume->v_adouble = options[VOLOPT_ADOUBLE].i_value;
672 volume->v_adouble = AD_VERSION;
674 if (options[VOLOPT_FORCEUID].c_value) {
675 volume->v_forceuid = strdup(options[VOLOPT_FORCEUID].c_value);
677 volume->v_forceuid = NULL; /* set as null so as to return 0 later on */
680 if (options[VOLOPT_FORCEGID].c_value) {
681 volume->v_forcegid = strdup(options[VOLOPT_FORCEGID].c_value);
683 volume->v_forcegid = NULL; /* set as null so as to return 0 later on */
687 if (options[VOLOPT_PREEXEC].c_value)
688 volume->v_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_PREEXEC].c_value, pwd, path, name);
689 volume->v_preexec_close = options[VOLOPT_PREEXEC].i_value;
691 if (options[VOLOPT_POSTEXEC].c_value)
692 volume->v_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_POSTEXEC].c_value, pwd, path, name);
694 if (options[VOLOPT_ROOTPREEXEC].c_value)
695 volume->v_root_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPREEXEC].c_value, pwd, path, name);
696 volume->v_root_preexec_close = options[VOLOPT_ROOTPREEXEC].i_value;
698 if (options[VOLOPT_ROOTPOSTEXEC].c_value)
699 volume->v_root_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPOSTEXEC].c_value, pwd, path, name);
703 initvoladouble(volume);
704 volume->v_next = Volumes;
709 /* ---------------- */
710 static char *myfgets( buf, size, fp )
719 while ((EOF != ( c = getc( fp )) ) && ( size > 0 )) {
720 if ( c == '\n' || c == '\r' ) {
738 /* check access list. this function wants something of the following
740 * @group,name,name2,@group2,name3
742 * a NULL argument allows everybody to have access.
743 * we return three things:
745 * 0: list exists, but name isn't in it
749 #ifndef NO_REAL_USER_NAME
750 /* authentication is case insensitive
751 * FIXME should we do the same with group name?
753 #define access_strcmp strcasecmp
756 #define access_strcmp strcmp
760 static int accessvol(args, name)
764 char buf[MAXPATHLEN + 1], *p;
770 strlcpy(buf, args, sizeof(buf));
771 if ((p = strtok(buf, ",")) == NULL) /* nothing, return okay */
775 if (*p == '@') { /* it's a group */
776 if ((gr = getgrnam(p + 1)) && gmem(gr->gr_gid))
778 } else if (access_strcmp(p, name) == 0) /* it's a user name */
780 p = strtok(NULL, ",");
786 static void setextmap( ext, type, creator, user)
787 char *ext, *type, *creator;
793 if (Extmap == NULL) {
794 if (( Extmap = calloc(1, sizeof( struct extmap ))) == NULL ) {
795 LOG(log_error, logtype_afpd, "setextmap: calloc: %s", strerror(errno) );
800 for ( em = Extmap, cnt = 0; em->em_ext; em++, cnt++) {
801 if ( (strdiacasecmp( em->em_ext, ext )) == 0 ) {
806 if ( em->em_ext == NULL ) {
807 if (!(Extmap = realloc( Extmap, sizeof( struct extmap ) * (cnt +2))) ) {
808 LOG(log_error, logtype_afpd, "setextmap: realloc: %s", strerror(errno) );
811 (Extmap +cnt +1)->em_ext = NULL;
813 } else if ( !user ) {
819 if (!(em->em_ext = strdup( ext))) {
820 LOG(log_error, logtype_afpd, "setextmap: strdup: %s", strerror(errno) );
824 if ( *type == '\0' ) {
825 memcpy(em->em_type, "????", sizeof( em->em_type ));
827 memcpy(em->em_type, type, sizeof( em->em_type ));
829 if ( *creator == '\0' ) {
830 memcpy(em->em_creator, "UNIX", sizeof( em->em_creator ));
832 memcpy(em->em_creator, creator, sizeof( em->em_creator ));
836 /* -------------------------- */
837 static int extmap_cmp(const void *map1, const void *map2)
839 const struct extmap *em1 = map1;
840 const struct extmap *em2 = map2;
841 return strdiacasecmp(em1->em_ext, em2->em_ext);
844 static void sortextmap( void)
849 if ((em = Extmap) == NULL) {
857 qsort(Extmap, Extmap_cnt, sizeof(struct extmap), extmap_cmp);
858 if (*Extmap->em_ext == 0) {
859 /* the first line is really "." the default entry,
860 * we remove the leading '.' in setextmap
867 /* ----------------------
869 static void free_extmap( void)
874 for ( em = Extmap; em->em_ext; em++) {
884 /* ----------------------
886 static int volfile_changed(struct afp_volume_name *p)
896 if (!stat( name, &st) && st.st_mtime > p->mtime) {
897 p->mtime = st.st_mtime;
903 /* ----------------------
904 * Read a volume configuration file and add the volumes contained within to
905 * the global volume list. If p2 is non-NULL, the file that is opened is
908 * Lines that begin with # and blank lines are ignored.
909 * Volume lines are of the form:
910 * <unix path> [<volume name>] [allow:<user>,<@group>,...] \
911 * [codepage:<file>] [casefold:<num>]
912 * <extension> TYPE [CREATOR]
914 static int readvolfile(obj, p1, p2, user, pwent)
916 struct afp_volume_name *p1;
919 struct passwd *pwent;
922 char path[ MAXPATHLEN + 1], tmp[ MAXPATHLEN + 1],
923 volname[ AFPVOL_NAMELEN + 1 ], buf[ BUFSIZ ],
924 type[ 5 ], creator[ 5 ];
927 struct vol_option options[VOLOPT_NUM], save_options[VOLOPT_NUM];
935 strcpy( path, p1->name );
942 p1->full_name = strdup(path);
945 if (NULL == ( fp = fopen( path, "r" )) ) {
949 if (fd != -1 && !fstat( fd, &st) ) {
950 p1->mtime = st.st_mtime;
953 memset(save_options, 0, sizeof(save_options));
954 while ( myfgets( buf, sizeof( buf ), fp ) != NULL ) {
955 initline( strlen( buf ), buf );
956 parseline( sizeof( path ) - 1, path );
963 /* change the default options for this file */
964 if (strncmp(path, VOLOPT_DEFAULT, VOLOPT_DEFAULT_LEN) == 0) {
966 for (i = 0; i < VOLOPT_NUM; i++) {
967 if (parseline( sizeof( path ) - VOLOPT_DEFAULT_LEN - 1,
968 path + VOLOPT_DEFAULT_LEN) < 0)
970 volset(save_options, NULL, tmp, sizeof(tmp) - 1,
971 path + VOLOPT_DEFAULT_LEN);
977 if (( p = strchr( path, '/' )) != NULL ) {
985 if ( u == NULL || *u == '\0' || ( pw = getpwnam( u )) == NULL ) {
988 strcpy( tmp, pw->pw_dir );
989 if ( p != NULL && *p != '\0' ) {
993 /* Tag a user's home directory with their umask. Note, this will
994 * be overwritten if the user actually specifies a umask: option
995 * for a '~' volume. */
996 save_options[VOLOPT_UMASK].i_value = obj->options.save_mask;
1000 /* send path through variable substitution */
1001 if (*path != '~') /* need to copy path to tmp */
1004 pwent = getpwnam(obj->username);
1005 volxlate(obj, path, sizeof(path) - 1, tmp, pwent, NULL, NULL);
1007 /* this is sort of braindead. basically, i want to be
1008 * able to specify things in any order, but i don't want to
1009 * re-write everything.
1011 * currently we have options:
1019 * options:prodos,crlf,noadouble,ro...
1024 * namemask:x,y,!z (not implemented yet)
1026 memcpy(options, save_options, sizeof(options));
1029 /* read in up to VOLOP_NUM possible options */
1030 for (i = 0; i < VOLOPT_NUM; i++) {
1031 if (parseline( sizeof( tmp ) - 1, tmp ) < 0)
1034 volset(options, save_options, volname, sizeof(volname) - 1, tmp);
1037 /* check allow/deny lists:
1038 allow -> either no list (-1), or in list (1)
1039 deny -> either no list (-1), or not in list (0) */
1040 if (accessvol(options[VOLOPT_ALLOW].c_value, obj->username) &&
1041 (accessvol(options[VOLOPT_DENY].c_value, obj->username) < 1)) {
1043 /* handle read-only behaviour. semantics:
1044 * 1) neither the rolist nor the rwlist exist -> rw
1045 * 2) rolist exists -> ro if user is in it.
1046 * 3) rwlist exists -> ro unless user is in it. */
1047 if (((options[VOLOPT_FLAGS].i_value & AFPVOL_RO) == 0) &&
1048 ((accessvol(options[VOLOPT_ROLIST].c_value,
1049 obj->username) == 1) ||
1050 !accessvol(options[VOLOPT_RWLIST].c_value,
1052 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
1054 /* do variable substitution for volname */
1055 volxlate(obj, tmp, sizeof(tmp) - 1, volname, pwent, path, NULL);
1056 creatvol(obj, pwent, path, tmp, options, p2 != NULL);
1058 volfree(options, save_options);
1062 parseline( sizeof( type ) - 1, type );
1063 parseline( sizeof( creator ) - 1, creator );
1064 setextmap( path, type, creator, user);
1071 volfree(save_options, NULL);
1073 if ( fclose( fp ) != 0 ) {
1074 LOG(log_error, logtype_afpd, "readvolfile: fclose: %s", strerror(errno) );
1080 /* ------------------------------- */
1081 static void volume_free(struct vol *vol)
1086 free(vol->v_password);
1088 free(vol->v_volcodepage);
1089 free(vol->v_maccodepage);
1090 free(vol->v_cnidscheme);
1091 free(vol->v_dbpath);
1094 free(vol->v_forceuid);
1095 free(vol->v_forcegid);
1096 #endif /* FORCE_UIDGID */
1099 /* ------------------------------- */
1100 static void free_volumes(void )
1103 struct vol *nvol, *ovol;
1105 for ( vol = Volumes; vol; vol = vol->v_next ) {
1106 if (( vol->v_flags & AFPVOL_OPEN ) ) {
1113 for ( vol = Volumes, ovol = NULL; vol; vol = nvol) {
1116 if (vol->v_name == NULL) {
1117 if (Volumes == vol) {
1122 ovol->v_next = nvol;
1132 /* ------------------------------- */
1133 static void volume_unlink(struct vol *volume)
1135 struct vol *vol, *ovol, *nvol;
1137 if (volume == Volumes) {
1138 Volumes = Volumes->v_next;
1141 for ( vol = Volumes->v_next, ovol = Volumes; vol; vol = nvol) {
1144 if (vol == volume) {
1145 ovol->v_next = nvol;
1154 static int getvolspace( vol, bfree, btotal, xbfree, xbtotal, bsize )
1156 u_int32_t *bfree, *btotal, *bsize;
1157 VolSpace *xbfree, *xbtotal;
1161 #ifndef NO_QUOTA_SUPPORT
1162 VolSpace qfree, qtotal;
1165 spaceflag = AFPVOL_GVSMASK & vol->v_flags;
1166 /* report up to 2GB if afp version is < 2.2 (4GB if not) */
1167 maxsize = (vol->v_flags & AFPVOL_A2VOL) ? 0x01fffe00 :
1168 (((afp_version < 22) || (vol->v_flags & AFPVOL_LIMITSIZE))
1169 ? 0x7fffffffL : 0xffffffffL);
1172 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_AFSGVS ) {
1173 if ( afs_getvolspace( vol, xbfree, xbtotal, bsize ) == AFP_OK ) {
1174 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_AFSGVS;
1175 goto getvolspace_done;
1180 if (( rc = ustatfs_getvolspace( vol, xbfree, xbtotal,
1181 bsize)) != AFP_OK ) {
1185 #define min(a,b) ((a)<(b)?(a):(b))
1186 #ifndef NO_QUOTA_SUPPORT
1187 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_UQUOTA ) {
1188 if ( uquota_getvolspace( vol, &qfree, &qtotal, *bsize ) == AFP_OK ) {
1189 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_UQUOTA;
1190 *xbfree = min(*xbfree, qfree);
1191 *xbtotal = min( *xbtotal, qtotal);
1192 goto getvolspace_done;
1196 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_USTATFS;
1199 *bfree = min( *xbfree, maxsize);
1200 *btotal = min( *xbtotal, maxsize);
1204 /* -----------------------
1205 * set volume creation date
1206 * avoid duplicate, well at least it tries
1208 static void vol_setdate(u_int16_t id, struct adouble *adp, time_t date)
1211 struct vol *vol = Volumes;
1213 for ( volume = Volumes; volume; volume = volume->v_next ) {
1214 if (volume->v_vid == id) {
1217 else if (AD_DATE_FROM_UNIX(date) == volume->v_ctime) {
1219 volume = Volumes; /* restart */
1222 vol->v_ctime = AD_DATE_FROM_UNIX(date);
1223 ad_setdate(adp, AD_DATE_CREATE | AD_DATE_UNIX, date);
1226 /* ----------------------- */
1227 static int getvolparams( bitmap, vol, st, buf, buflen )
1235 int bit = 0, isad = 1;
1238 u_int32_t bfree, btotal, bsize;
1239 VolSpace xbfree, xbtotal; /* extended bytes */
1240 char *data, *nameoff = NULL;
1243 /* courtesy of jallison@whistle.com:
1244 * For MacOS8.x support we need to create the
1245 * .Parent file here if it doesn't exist. */
1247 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1248 if ( ad_open( vol->v_path, vol_noadouble(vol) |
1249 ADFLAGS_HF|ADFLAGS_DIR, O_RDWR | O_CREAT,
1252 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1254 } else if (ad_get_HF_flags( &ad ) & O_CREAT) {
1255 slash = strrchr( vol->v_path, '/' );
1259 slash = vol->v_path;
1260 if (ad_getentryoff(&ad, ADEID_NAME)) {
1261 ad_setentrylen( &ad, ADEID_NAME, strlen( slash ));
1262 memcpy(ad_entry( &ad, ADEID_NAME ), slash,
1263 ad_getentrylen( &ad, ADEID_NAME ));
1265 vol_setdate(vol->v_vid, &ad, st->st_mtime);
1266 ad_flush(&ad, ADFLAGS_HF);
1269 if (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0)
1270 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1272 vol->v_ctime = aint;
1275 if (( bitmap & ( (1<<VOLPBIT_BFREE)|(1<<VOLPBIT_BTOTAL) |
1276 (1<<VOLPBIT_XBFREE)|(1<<VOLPBIT_XBTOTAL) |
1277 (1<<VOLPBIT_BSIZE)) ) != 0 ) {
1278 if ( getvolspace( vol, &bfree, &btotal, &xbfree, &xbtotal,
1281 ad_close( &ad, ADFLAGS_HF );
1283 return( AFPERR_PARAM );
1288 while ( bitmap != 0 ) {
1289 while (( bitmap & 1 ) == 0 ) {
1297 if (0 == (vol->v_flags & AFPVOL_NOFILEID) && vol->v_cdb != NULL &&
1298 (vol->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1299 ashort = VOLPBIT_ATTR_FILEID;
1301 /* check for read-only.
1302 * NOTE: we don't actually set the read-only flag unless
1303 * it's passed in that way as it's possible to mount
1304 * a read-write filesystem under a read-only one. */
1305 if ((vol->v_flags & AFPVOL_RO) ||
1306 ((utime(vol->v_path, NULL) < 0) && (errno == EROFS))) {
1307 ashort |= VOLPBIT_ATTR_RO;
1309 ashort |= VOLPBIT_ATTR_CATSEARCH;
1310 if (afp_version >= 30) {
1311 ashort |= VOLPBIT_ATTR_UTF8;
1312 if (vol->v_flags & AFPVOL_UNIX_PRIV)
1313 ashort |= VOLPBIT_ATTR_UNIXPRIV;
1315 ashort = htons(ashort);
1316 memcpy(data, &ashort, sizeof( ashort ));
1317 data += sizeof( ashort );
1321 ashort = htons( AFPVOLSIG_DEFAULT );
1322 memcpy(data, &ashort, sizeof( ashort ));
1323 data += sizeof( ashort );
1326 case VOLPBIT_CDATE :
1327 aint = vol->v_ctime;
1328 memcpy(data, &aint, sizeof( aint ));
1329 data += sizeof( aint );
1332 case VOLPBIT_MDATE :
1333 if ( st->st_mtime > vol->v_mtime ) {
1334 vol->v_mtime = st->st_mtime;
1336 aint = AD_DATE_FROM_UNIX(vol->v_mtime);
1337 memcpy(data, &aint, sizeof( aint ));
1338 data += sizeof( aint );
1341 case VOLPBIT_BDATE :
1342 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1343 aint = AD_DATE_START;
1344 memcpy(data, &aint, sizeof( aint ));
1345 data += sizeof( aint );
1349 memcpy(data, &vol->v_vid, sizeof( vol->v_vid ));
1350 data += sizeof( vol->v_vid );
1353 case VOLPBIT_BFREE :
1354 bfree = htonl( bfree );
1355 memcpy(data, &bfree, sizeof( bfree ));
1356 data += sizeof( bfree );
1359 case VOLPBIT_BTOTAL :
1360 btotal = htonl( btotal );
1361 memcpy(data, &btotal, sizeof( btotal ));
1362 data += sizeof( btotal );
1365 #ifndef NO_LARGE_VOL_SUPPORT
1366 case VOLPBIT_XBFREE :
1367 xbfree = hton64( xbfree );
1368 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1369 bcopy(&xbfree, data, sizeof(xbfree));
1370 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1371 memcpy(data, &xbfree, sizeof( xbfree ));
1372 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1373 data += sizeof( xbfree );
1376 case VOLPBIT_XBTOTAL :
1377 xbtotal = hton64( xbtotal );
1378 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1379 bcopy(&xbtotal, data, sizeof(xbtotal));
1380 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1381 memcpy(data, &xbtotal, sizeof( xbtotal ));
1382 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1383 data += sizeof( xbfree );
1385 #endif /* ! NO_LARGE_VOL_SUPPORT */
1389 data += sizeof( u_int16_t );
1392 case VOLPBIT_BSIZE: /* block size */
1393 bsize = htonl(bsize);
1394 memcpy(data, &bsize, sizeof(bsize));
1395 data += sizeof(bsize);
1400 ad_close( &ad, ADFLAGS_HF );
1402 return( AFPERR_BITMAP );
1408 ashort = htons( data - buf );
1409 memcpy(nameoff, &ashort, sizeof( ashort ));
1410 /* name is always in mac charset, FIXME mangle if length > 27 char */
1411 aint = ucs2_to_charset( vol->v_maccharset, vol->v_name, data+1, 255);
1421 ad_close( &ad, ADFLAGS_HF );
1423 *buflen = data - buf;
1427 /* ------------------------- */
1428 static int stat_vol(u_int16_t bitmap, struct vol *vol, char *rbuf, int *rbuflen)
1433 if ( stat( vol->v_path, &st ) < 0 ) {
1435 return( AFPERR_PARAM );
1437 /* save the volume device number */
1438 vol->v_dev = st.st_dev;
1440 buflen = *rbuflen - sizeof( bitmap );
1441 if (( ret = getvolparams( bitmap, vol, &st,
1442 rbuf + sizeof( bitmap ), &buflen )) != AFP_OK ) {
1446 *rbuflen = buflen + sizeof( bitmap );
1447 bitmap = htons( bitmap );
1448 memcpy(rbuf, &bitmap, sizeof( bitmap ));
1453 /* ------------------------------- */
1454 void load_volumes(AFPObj *obj)
1456 struct passwd *pwent;
1461 /* check files date */
1462 if (obj->options.defaultvol.loaded) {
1463 changed = volfile_changed(&obj->options.defaultvol);
1465 if (obj->options.systemvol.loaded) {
1466 changed |= volfile_changed(&obj->options.systemvol);
1468 if (obj->options.uservol.loaded) {
1469 changed |= volfile_changed(&obj->options.uservol);
1478 pwent = getpwnam(obj->username);
1479 if ( (obj->options.flags & OPTION_USERVOLFIRST) == 0 ) {
1480 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent);
1483 if ((*obj->username == '\0') || (obj->options.flags & OPTION_NOUSERVOL)) {
1484 readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent);
1487 * Read user's AppleVolumes or .AppleVolumes file
1488 * If neither are readable, read the default volumes file. if
1489 * that doesn't work, create a user share.
1491 if (obj->options.uservol.name) {
1492 free(obj->options.uservol.name);
1494 obj->options.uservol.name = strdup(pwent->pw_dir);
1495 if ( readvolfile(obj, &obj->options.uservol, "AppleVolumes", 1, pwent) < 0 &&
1496 readvolfile(obj, &obj->options.uservol, ".AppleVolumes", 1, pwent) < 0 &&
1497 readvolfile(obj, &obj->options.uservol, "applevolumes", 1, pwent) < 0 &&
1498 readvolfile(obj, &obj->options.uservol, ".applevolumes", 1, pwent) < 0 &&
1499 obj->options.defaultvol.name != NULL ) {
1500 if (readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent) < 0)
1501 creatvol(obj, pwent, pwent->pw_dir, NULL, NULL, 1);
1504 if ( obj->options.flags & OPTION_USERVOLFIRST ) {
1505 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent );
1509 /* ------------------------------- */
1510 int afp_getsrvrparms(obj, ibuf, ibuflen, rbuf, rbuflen )
1512 char *ibuf _U_, *rbuf;
1513 int ibuflen _U_, *rbuflen;
1526 for ( vcnt = 0, volume = Volumes; volume; volume = volume->v_next ) {
1527 if (!(volume->v_flags & AFPVOL_NOSTAT)) {
1530 if ( stat( volume->v_path, &st ) < 0 ) {
1531 LOG(log_info, logtype_afpd, "afp_getsrvrparms(%s): stat: %s",
1532 volume->v_path, strerror(errno) );
1533 continue; /* can't access directory */
1535 if (!S_ISDIR(st.st_mode)) {
1536 continue; /* not a dir */
1538 accessmode(volume->v_path, &ma, NULL, &st);
1539 if ((ma.ma_user & (AR_UREAD | AR_USEARCH)) != (AR_UREAD | AR_USEARCH)) {
1540 continue; /* no r-x access */
1543 if (volume->v_hide) {
1544 continue; /* config file changed but the volume was mounted */
1546 len = ucs2_to_charset_allocate((utf8_encoding()?CH_UTF8_MAC:obj->options.maccharset),
1547 &namebuf, volume->v_name);
1548 if (len == (size_t)-1)
1551 /* set password bit if there's a volume password */
1552 *data = (volume->v_password) ? AFPSRVR_PASSWD : 0;
1554 /* Apple 2 clients running ProDOS-8 expect one volume to have
1555 bit 0 of this byte set. They will not recognize anything
1556 on the server unless this is the case. I have not
1557 completely worked this out, but it's related to booting
1558 from the server. Support for that function is a ways
1559 off.. <shirsch@ibm.net> */
1560 *data |= (volume->v_flags & AFPVOL_A2VOL) ? AFPSRVR_CONFIGINFO : 0;
1561 *data++ |= 0; /* UNIX PRIVS BIT ..., OSX doesn't seem to use it, so we don't either */
1563 memcpy(data, namebuf, len );
1569 *rbuflen = data - rbuf;
1571 if ( gettimeofday( &tv, 0 ) < 0 ) {
1572 LOG(log_error, logtype_afpd, "afp_getsrvrparms(%s): gettimeofday: %s", volume->v_path, strerror(errno) );
1574 return AFPERR_PARAM;
1576 tv.tv_sec = AD_DATE_FROM_UNIX(tv.tv_sec);
1577 memcpy(data, &tv.tv_sec, sizeof( u_int32_t));
1578 data += sizeof( u_int32_t);
1583 /* -------------------------
1584 * we are the user here
1586 int afp_openvol(obj, ibuf, ibuflen, rbuf, rbuflen )
1589 int ibuflen _U_, *rbuflen;
1599 char path[ MAXPATHLEN + 1];
1604 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1605 bitmap = ntohs( bitmap );
1606 ibuf += sizeof( bitmap );
1607 if (( bitmap & (1<<VOLPBIT_VID)) == 0 ) {
1609 return AFPERR_BITMAP;
1612 len = (unsigned char)*ibuf++;
1613 volname = obj->oldtmp;
1614 namelen = convert_string( (utf8_encoding()?CH_UTF8_MAC:obj->options.maccharset), CH_UCS2,
1615 ibuf, len, volname, sizeof(obj->oldtmp));
1618 return AFPERR_PARAM;
1622 if ((len + 1) & 1) /* pad to an even boundary */
1627 for ( volume = Volumes; volume; volume = volume->v_next ) {
1628 if ( strcasecmp_w( (ucs2_t*) volname, volume->v_name ) == 0 ) {
1633 if ( volume == NULL ) {
1635 return AFPERR_PARAM;
1638 /* check for a volume password */
1639 if (volume->v_password && strncmp(ibuf, volume->v_password, VOLPASSLEN)) {
1641 return AFPERR_ACCESS;
1644 if (( volume->v_flags & AFPVOL_OPEN ) ) {
1645 /* the volume is already open */
1647 set_uidgid ( volume );
1649 return stat_vol(bitmap, volume, rbuf, rbuflen);
1652 /* initialize volume variables
1655 if (afp_version >= 30) {
1656 volume->max_filename = 255;
1659 volume->max_filename = MACFILELEN;
1662 volume->v_dir = volume->v_root = NULL;
1664 volume->v_flags |= AFPVOL_OPEN;
1665 volume->v_cdb = NULL;
1667 if (volume->v_root_preexec) {
1668 if ((ret = afprun(1, volume->v_root_preexec, NULL)) && volume->v_root_preexec_close) {
1669 LOG(log_error, logtype_afpd, "afp_openvol(%s): root preexec : %d", volume->v_path, ret );
1676 set_uidgid ( volume );
1679 if (volume->v_preexec) {
1680 if ((ret = afprun(0, volume->v_preexec, NULL)) && volume->v_preexec_close) {
1681 LOG(log_error, logtype_afpd, "afp_openvol(%s): preexec : %d", volume->v_path, ret );
1687 if ( stat( volume->v_path, &st ) < 0 ) {
1692 if ( chdir( volume->v_path ) < 0 ) {
1697 len = convert_string_allocate( CH_UCS2, (utf8_encoding()?CH_UTF8_MAC:obj->options.maccharset),
1698 volume->v_name, namelen, &vol_mname);
1699 if ( !vol_mname || len <= 0) {
1704 if ( NULL == getcwd(path, MAXPATHLEN)) {
1705 /* shouldn't be fatal but it will fail later */
1706 LOG(log_error, logtype_afpd, "afp_openvol(%s): volume pathlen too long", volume->v_path);
1711 if ((vol_uname = strrchr(path, '/')) == NULL)
1713 else if (*(vol_uname + 1) != '\0')
1716 if ((dir = dirnew(vol_mname, vol_uname) ) == NULL) {
1718 LOG(log_error, logtype_afpd, "afp_openvol(%s): malloc: %s", volume->v_path, strerror(errno) );
1724 dir->d_did = DIRDID_ROOT;
1725 dir->d_color = DIRTREE_COLOR_BLACK; /* root node is black */
1726 volume->v_dir = volume->v_root = dir;
1728 curdir = volume->v_dir;
1729 if (volume->v_cnidscheme == NULL) {
1730 volume->v_cnidscheme = strdup(DEFAULT_CNID_SCHEME);
1731 LOG(log_warning, logtype_afpd, "Warning: No CNID scheme for volume %s. Using default.",
1734 if (volume->v_dbpath)
1735 volume->v_cdb = cnid_open (volume->v_dbpath, volume->v_umask, volume->v_cnidscheme, (volume->v_flags & AFPVOL_NODEV));
1737 volume->v_cdb = cnid_open (volume->v_path, volume->v_umask, volume->v_cnidscheme, (volume->v_flags & AFPVOL_NODEV));
1738 if (volume->v_cdb == NULL) {
1739 LOG(log_error, logtype_afpd, "Fatal error: cannot open CNID or invalid CNID backend for %s: %s",
1740 volume->v_path, volume->v_cnidscheme);
1747 if (!volume->v_volcodepage)
1748 volume->v_volcodepage = strdup("UTF8");
1750 if ( (charset_t) -1 == ( volume->v_volcharset = add_charset(volume->v_volcodepage)) ) {
1751 LOG (log_error, logtype_afpd, "Setting codepage %s as volume codepage failed", volume->v_volcodepage);
1756 if ( NULL == ( volume->v_vol = find_charset_functions(volume->v_volcodepage)) || volume->v_vol->flags & CHARSET_ICONV ) {
1757 LOG (log_warning, logtype_afpd, "WARNING: volume encoding %s is *not* supported by netatalk, expect problems !!!!", volume->v_volcodepage);
1760 if (!volume->v_maccodepage)
1761 volume->v_maccodepage = strdup(obj->options.maccodepage);
1763 if ( (charset_t) -1 == ( volume->v_maccharset = add_charset(volume->v_maccodepage)) ) {
1764 LOG (log_error, logtype_afpd, "Setting codepage %s as mac codepage failed", volume->v_maccodepage);
1769 if ( NULL == ( volume->v_mac = find_charset_functions(volume->v_maccodepage)) || ! (volume->v_mac->flags & CHARSET_CLIENT) ) {
1770 LOG (log_error, logtype_afpd, "Fatal error: mac charset %s not supported", volume->v_maccodepage);
1775 ret = stat_vol(bitmap, volume, rbuf, rbuflen);
1776 if (ret == AFP_OK) {
1778 if (!(volume->v_flags & AFPVOL_RO)) {
1779 handle_special_folders( volume );
1780 savevoloptions( volume);
1784 * If you mount a volume twice, the second time the trash appears on
1785 * the desk-top. That's because the Mac remembers the DID for the
1786 * trash (even for volumes in different zones, on different servers).
1787 * Just so this works better, we prime the DID cache with the trash,
1788 * fixing the trash at DID 17.
1789 * FIXME (RL): should it be done inside a CNID backend ? (always returning Trash DID when asked) ?
1791 if ((volume->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1793 /* FIXME find db time stamp */
1794 if (cnid_getstamp(volume->v_cdb, volume->v_stamp, sizeof(volume->v_stamp)) < 0) {
1795 LOG (log_error, logtype_afpd,
1796 "afp_openvol(%s): Fatal error: Unable to get stamp value from CNID backend",
1804 cname( volume, volume->v_dir, &p );
1810 if (volume->v_dir) {
1811 dirfree( volume->v_dir );
1812 volume->v_dir = volume->v_root = NULL;
1815 volume->v_flags &= ~AFPVOL_OPEN;
1816 if (volume->v_cdb != NULL) {
1817 cnid_close(volume->v_cdb);
1818 volume->v_cdb = NULL;
1824 /* ------------------------- */
1825 static void closevol(struct vol *vol)
1830 dirfree( vol->v_root );
1832 if (vol->v_cdb != NULL) {
1833 cnid_close(vol->v_cdb);
1837 if (vol->v_postexec) {
1838 afprun(0, vol->v_postexec, NULL);
1840 if (vol->v_root_postexec) {
1841 afprun(1, vol->v_root_postexec, NULL);
1845 /* ------------------------- */
1846 void close_all_vol(void)
1850 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
1851 if ( (ovol->v_flags & AFPVOL_OPEN) ) {
1852 ovol->v_flags &= ~AFPVOL_OPEN;
1858 /* ------------------------- */
1859 int afp_closevol(obj, ibuf, ibuflen, rbuf, rbuflen )
1861 char *ibuf, *rbuf _U_;
1862 int ibuflen _U_, *rbuflen;
1864 struct vol *vol, *ovol;
1869 memcpy(&vid, ibuf, sizeof( vid ));
1870 if (NULL == ( vol = getvolbyvid( vid )) ) {
1871 return( AFPERR_PARAM );
1874 vol->v_flags &= ~AFPVOL_OPEN;
1875 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
1876 if ( (ovol->v_flags & AFPVOL_OPEN) ) {
1880 if ( ovol != NULL ) {
1881 /* Even if chdir fails, we can't say afp_closevol fails. */
1882 if ( chdir( ovol->v_path ) == 0 ) {
1883 curdir = ovol->v_dir;
1888 if (vol->v_deleted) {
1889 showvol(vol->v_name);
1897 /* ------------------------- */
1898 struct vol *getvolbyvid(const u_int16_t vid )
1902 for ( vol = Volumes; vol; vol = vol->v_next ) {
1903 if ( vid == vol->v_vid ) {
1907 if ( vol == NULL || ( vol->v_flags & AFPVOL_OPEN ) == 0 ) {
1913 #endif /* FORCE_UIDGID */
1918 /* ------------------------ */
1919 static int ext_cmp_key(const void *key, const void *obj)
1921 const char *p = key;
1922 const struct extmap *em = obj;
1923 return strdiacasecmp(p, em->em_ext);
1925 struct extmap *getextmap(const char *path)
1930 if (NULL == ( p = strrchr( path, '.' )) ) {
1931 return( Defextmap );
1934 if (!*p || !Extmap_cnt) {
1935 return( Defextmap );
1937 em = bsearch(p, Extmap, Extmap_cnt, sizeof(struct extmap), ext_cmp_key);
1941 return( Defextmap );
1945 /* ------------------------- */
1946 struct extmap *getdefextmap(void)
1948 return( Defextmap );
1951 /* --------------------------
1952 poll if a volume is changed by other processes.
1954 int pollvoltime(obj)
1961 if (!(afp_version > 21 && obj->options.server_notif))
1964 if ( gettimeofday( &tv, 0 ) < 0 )
1967 for ( vol = Volumes; vol; vol = vol->v_next ) {
1968 if ( (vol->v_flags & AFPVOL_OPEN) && vol->v_mtime + 30 < tv.tv_sec) {
1969 if ( !stat( vol->v_path, &st ) && vol->v_mtime != st.st_mtime ) {
1970 vol->v_mtime = st.st_mtime;
1971 if (!obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED))
1980 /* ------------------------- */
1981 void setvoltime(obj, vol )
1987 /* just looking at vol->v_mtime is broken seriously since updates
1988 * from other users afpd processes never are seen.
1989 * This is not the most elegant solution (a shared memory between
1990 * the afpd processes would come closer)
1993 if ( gettimeofday( &tv, 0 ) < 0 ) {
1994 LOG(log_error, logtype_afpd, "setvoltime(%s): gettimeofday: %s", vol->v_path, strerror(errno) );
1997 if( utime( vol->v_path, NULL ) < 0 ) {
1998 /* write of time failed ... probably a read only filesys,
1999 * where no other users can interfere, so there's no issue here
2003 /* a little granularity */
2004 if (vol->v_mtime < tv.tv_sec) {
2005 vol->v_mtime = tv.tv_sec;
2006 /* or finder doesn't update free space */
2007 if (afp_version > 21 && obj->options.server_notif) {
2008 obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED);
2013 /* ------------------------- */
2014 int afp_getvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
2017 int ibuflen _U_, *rbuflen;
2020 u_int16_t vid, bitmap;
2023 memcpy(&vid, ibuf, sizeof( vid ));
2024 ibuf += sizeof( vid );
2025 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2026 bitmap = ntohs( bitmap );
2028 if (NULL == ( vol = getvolbyvid( vid )) ) {
2030 return( AFPERR_PARAM );
2033 return stat_vol(bitmap, vol, rbuf, rbuflen);
2036 /* ------------------------- */
2037 int afp_setvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
2039 char *ibuf, *rbuf _U_;
2040 int ibuflen _U_, *rbuflen;
2044 u_int16_t vid, bitmap;
2050 memcpy(&vid, ibuf, sizeof( vid ));
2051 ibuf += sizeof( vid );
2052 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2053 bitmap = ntohs( bitmap );
2054 ibuf += sizeof(bitmap);
2056 if (( vol = getvolbyvid( vid )) == NULL ) {
2057 return( AFPERR_PARAM );
2060 if ((vol->v_flags & AFPVOL_RO))
2061 return AFPERR_VLOCK;
2063 /* we can only set the backup date. */
2064 if (bitmap != (1 << VOLPBIT_BDATE))
2065 return AFPERR_BITMAP;
2067 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2068 if ( ad_open( vol->v_path, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR,
2071 return AFPERR_VLOCK;
2073 return AFPERR_ACCESS;
2076 memcpy(&aint, ibuf, sizeof(aint));
2077 ad_setdate(&ad, AD_DATE_BACKUP, aint);
2078 ad_flush(&ad, ADFLAGS_HF);
2079 ad_close(&ad, ADFLAGS_HF);
2083 /* ------------------------- */
2084 int wincheck(const struct vol *vol, const char *path)
2088 if (!(vol->v_flags & AFPVOL_MSWINDOWS))
2091 /* empty paths are not allowed */
2092 if ((len = strlen(path)) == 0)
2095 /* leading or trailing whitespaces are not allowed, carriage returns
2096 * and probably other whitespace is okay, tabs are not allowed
2098 if ((path[0] == ' ') || (path[len-1] == ' '))
2101 /* certain characters are not allowed */
2102 if (strpbrk(path, MSWINDOWS_BADCHARS))
2105 /* everything else is okay */
2111 * precreate a folder
2112 * this is only intended for folders in the volume root
2113 * It will *not* work if the folder name contains extended characters
2115 static int create_special_folder (const struct vol *vol, const struct _special_folder *folder)
2124 p = (char *) malloc ( strlen(vol->v_path)+strlen(folder->name)+2);
2126 LOG(log_error, logtype_afpd,"malloc failed");
2130 q=strdup(folder->name);
2132 LOG(log_error, logtype_afpd,"malloc failed");
2136 strcpy(p, vol->v_path);
2141 if ((vol->v_casefold & AFPVOL_MTOUUPPER))
2143 else if ((vol->v_casefold & AFPVOL_MTOULOWER))
2149 if ( (ret = stat( p, &st )) < 0 ) {
2150 if (folder->precreate) {
2151 if (ad_mkdir(p, folder->mode)) {
2152 LOG(log_debug, logtype_afpd,"Creating '%s' failed in %s: %s", p, vol->v_path, strerror(errno));
2161 if ( !ret && folder->hide) {
2163 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2164 if (ad_open( p, vol_noadouble(vol) | ADFLAGS_HF|ADFLAGS_DIR,
2165 O_RDWR|O_CREAT, 0666, &ad) < 0) {
2170 if ((ad_get_HF_flags( &ad ) & O_CREAT) ) {
2171 if (ad_getentryoff(&ad, ADEID_NAME)) {
2172 ad_setentrylen( &ad, ADEID_NAME, strlen(folder->name));
2173 memcpy(ad_entry( &ad, ADEID_NAME ), folder->name,
2174 ad_getentrylen( &ad, ADEID_NAME ));
2178 ad_getattr(&ad, &attr);
2179 attr |= htons( ntohs( attr ) | ATTRBIT_INVISIBLE );
2180 ad_setattr(&ad, attr);
2182 /* do the same with the finder info */
2183 if (ad_entry(&ad, ADEID_FINDERI)) {
2184 memcpy(&attr, ad_entry(&ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF, sizeof(attr));
2185 attr |= htons(FINDERINFO_INVISIBLE);
2186 memcpy(ad_entry(&ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF,&attr, sizeof(attr));
2189 ad_flush( &ad, ADFLAGS_HF );
2190 ad_close( &ad, ADFLAGS_HF );
2197 static void handle_special_folders (const struct vol * vol)
2199 const _special_folder *p = &special_folders[0];
2201 if ((vol->v_flags & AFPVOL_RO))
2204 for (; p->name != NULL; p++) {
2205 create_special_folder (vol, p);
2210 * Save the volume options to a file, used by
2212 * Writing the file everytime a volume is opened is
2213 * unnecessary, but it shouldn't hurt much.
2215 static int savevoloptions (const struct vol *vol)
2218 char item[MAXPATHLEN];
2222 const _vol_opt_name *op = &vol_opt_names[0];
2223 const _vol_opt_name *cf = &vol_opt_casefold[0];
2225 strlcpy (item, vol->v_path, sizeof(item));
2226 strlcat (item, "/.AppleDesktop/", sizeof(item));
2227 strlcat (item, VOLINFOFILE, sizeof(item));
2229 if ((fd = open( item, O_RDWR | O_CREAT , 0666)) <0 ) {
2230 LOG(log_debug, logtype_afpd,"Error opening %s: %s", item, strerror(errno));
2234 /* try to get a lock */
2236 lock.l_whence = SEEK_SET;
2238 lock.l_type = F_WRLCK;
2240 if (fcntl(fd, F_SETLK, &lock) < 0) {
2241 if (errno == EACCES || errno == EAGAIN) {
2242 /* ignore, other process already writing the file */
2245 LOG(log_error, logtype_cnid, "savevoloptions: cannot get lock: %s", strerror(errno));
2250 /* write volume options */
2251 snprintf(buf, sizeof(buf), "MAC_CHARSET:%s\n", vol->v_maccodepage);
2252 snprintf(item, sizeof(item), "VOL_CHARSET:%s\n", vol->v_volcodepage);
2253 strlcat(buf, item, sizeof(buf));
2255 switch (vol->v_adouble) {
2257 strlcat(buf, "ADOUBLE_VER:v1\n", sizeof(buf));
2260 strlcat(buf, "ADOUBLE_VER:v2\n", sizeof(buf));
2262 case AD_VERSION2_OSX:
2263 strlcat(buf, "ADOUBLE_VER:osx\n", sizeof(buf));
2267 strlcat(buf, "CNIDBACKEND:", sizeof(buf));
2268 strlcat(buf, vol->v_cnidscheme, sizeof(buf));
2269 strlcat(buf, "\n", sizeof(buf));
2271 strlcat(buf, "CNIDDBDHOST:", sizeof(buf));
2272 strlcat(buf, Cnid_srv, sizeof(buf));
2273 strlcat(buf, "\n", sizeof(buf));
2275 snprintf(item, sizeof(item), "CNIDDBDPORT:%u\n", Cnid_port);
2276 strlcat(buf, item, sizeof(buf));
2278 strcpy(item, "CNID_DBPATH:");
2280 strlcat(item, vol->v_dbpath, sizeof(item));
2282 strlcat(item, vol->v_path, sizeof(item));
2283 strlcat(item, "\n", sizeof(item));
2284 strlcat(buf, item, sizeof(buf));
2287 strcpy(item, "VOLUME_OPTS:");
2288 for (;op->name; op++) {
2289 if ( (vol->v_flags & op->option) ) {
2290 strlcat(item, op->name, sizeof(item));
2291 strlcat(item, " ", sizeof(item));
2294 strlcat(item, "\n", sizeof(item));
2295 strlcat(buf, item, sizeof(buf));
2297 /* casefold flags */
2298 strcpy(item, "VOLCASEFOLD:");
2299 for (;cf->name; cf++) {
2300 if ( (vol->v_casefold & cf->option) ) {
2301 strlcat(item, cf->name, sizeof(item));
2302 strlcat(item, " ", sizeof(item));
2305 strlcat(item, "\n", sizeof(item));
2306 strlcat(buf, item, sizeof(buf));
2308 if (strlen(buf) >= sizeof(buf)-1)
2309 LOG(log_debug, logtype_afpd,"Error writing .volinfo file: buffer too small, %s", buf);
2312 if (write( fd, buf, strlen(buf)) < 0) {
2313 LOG(log_debug, logtype_afpd,"Error writing .volinfo file: %s", strerror(errno));
2316 ftruncate(fd, strlen(buf));
2319 lock.l_type = F_UNLCK;
2320 fcntl(fd, F_SETLK, &lock);