2 * $Id: volume.c,v 1.61 2005-05-25 18:30:50 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"
58 extern int afprun(int root, char *cmd, int *outfd);
61 #define MIN(a, b) ((a) < (b) ? (a) : (b))
64 #ifndef NO_LARGE_VOL_SUPPORT
65 #if BYTE_ORDER == BIG_ENDIAN
68 #else /* BYTE_ORDER == BIG_ENDIAN */
69 #define hton64(x) ((u_int64_t) (htonl(((x) >> 32) & 0xffffffffLL)) | \
70 (u_int64_t) ((htonl(x) & 0xffffffffLL) << 32))
71 #define ntoh64(x) (hton64(x))
72 #endif /* BYTE_ORDER == BIG_ENDIAN */
73 #endif /* ! NO_LARGE_VOL_SUPPORT */
75 static struct vol *Volumes = NULL;
76 static u_int16_t lastvid = 0;
77 static char *Trash = "\02\024Network Trash Folder";
79 static struct extmap *Extmap = NULL, *Defextmap = NULL;
80 static int Extmap_cnt;
81 static void free_extmap(void);
83 #define VOLOPT_ALLOW 0 /* user allow list */
84 #define VOLOPT_DENY 1 /* user deny list */
85 #define VOLOPT_RWLIST 2 /* user rw list */
86 #define VOLOPT_ROLIST 3 /* user ro list */
87 #define VOLOPT_PASSWORD 4 /* volume password */
88 #define VOLOPT_CASEFOLD 5 /* character case mangling */
89 #define VOLOPT_FLAGS 6 /* various flags */
90 #define VOLOPT_DBPATH 7 /* path to database */
91 #define VOLOPT_MAPCHARS 8 /* does mtou and utom mappings. syntax:
92 m and u can be double-byte hex
97 !u -> make u illegal always
98 ~u -> make u illegal only as the first
99 part of a double-byte character.
101 #define VOLOPT_VETO 10 /* list of veto filespec */
102 #define VOLOPT_PREEXEC 11 /* preexec command */
103 #define VOLOPT_ROOTPREEXEC 12 /* root preexec command */
105 #define VOLOPT_POSTEXEC 13 /* postexec command */
106 #define VOLOPT_ROOTPOSTEXEC 14 /* root postexec command */
108 #define VOLOPT_ENCODING 15 /* mac encoding (pre OSX)*/
109 #define VOLOPT_MACCHARSET 16
110 #define VOLOPT_CNIDSCHEME 17
111 #define VOLOPT_ADOUBLE 18 /* adouble version */
116 #define VOLOPT_FORCEUID 19 /* force uid for username x */
117 #define VOLOPT_FORCEGID 20 /* force gid for group x */
118 #define VOLOPT_UMASK 21
120 #define VOLOPT_UMASK 19
121 #endif /* FORCE_UIDGID */
123 #define VOLOPT_MAX (VOLOPT_UMASK +1)
125 #define VOLOPT_NUM (VOLOPT_MAX + 1)
128 #define VOLOPT_DEFAULT ":DEFAULT:"
129 #define VOLOPT_DEFAULT_LEN 9
135 typedef struct _special_folder {
142 static const _special_folder special_folders[] = {
143 {"Network Trash Folder", 1, 0777, 1},
144 {"Temporary Items", 1, 0777, 1},
145 {".AppleDesktop", 1, 0777, 0},
147 {"TheFindByContentFolder", 0, 0, 1},
148 {"TheVolumeSettingsFolder", 0, 0, 1},
152 typedef struct _volopt_name {
153 const u_int32_t option;
157 static const _vol_opt_name vol_opt_names[] = {
158 {AFPVOL_A2VOL, "PRODOS"}, /* prodos volume */
159 {AFPVOL_CRLF, "CRLF"}, /* cr/lf translation */
160 {AFPVOL_NOADOUBLE, "NOADOUBLE"}, /* don't create .AppleDouble by default */
161 {AFPVOL_RO, "READONLY"}, /* read-only volume */
162 {AFPVOL_MSWINDOWS, "MSWINDOWS"}, /* deal with ms-windows yuckiness. this is going away. */
163 {AFPVOL_NOHEX, "NOHEX"}, /* don't do :hex translation */
164 {AFPVOL_USEDOTS, "USEDOTS"}, /* use real dots */
165 {AFPVOL_LIMITSIZE, "LIMITSIZE"}, /* limit size for older macs */
166 {AFPVOL_MAPASCII, "MAPASCII"}, /* map the ascii range as well */
167 {AFPVOL_DROPBOX, "DROPBOX"}, /* dropkludge dropbox support */
168 {AFPVOL_NOFILEID, "NOFILEID"}, /* don't advertise createid resolveid and deleteid calls */
169 {AFPVOL_NOSTAT, "NOSTAT"}, /* advertise the volume even if we can't stat() it
170 * maybe because it will be mounted later in preexec */
171 {AFPVOL_UNIX_PRIV, "UNIXPRIV"}, /* support unix privileges */
172 {AFPVOL_NODEV, "NODEV"}, /* always use 0 for device number in cnid calls */
173 {AFPVOL_CASEINSEN, "CASEINSENSITIVE"}, /* volume is case insensitive */
174 {AFPVOL_EILSEQ, "ILLEGALSEQ"}, /* encode illegal sequence */
175 {AFPVOL_CACHE, "CACHE ID"}, /* encode illegal sequence */
179 static const _vol_opt_name vol_opt_casefold[] = {
180 {AFPVOL_MTOUUPPER, "MTOULOWER"},
181 {AFPVOL_MTOULOWER, "MTOULOWER"},
182 {AFPVOL_UTOMUPPER, "UTOMUPPER"},
183 {AFPVOL_UTOMLOWER, "UTOMLOWER"},
187 static void handle_special_folders (const struct vol *);
188 static int savevoloptions (const struct vol *);
189 static void deletevol(struct vol *vol);
191 static __inline__ void volfree(struct vol_option *options,
192 const struct vol_option *save)
197 for (i = 0; i < VOLOPT_MAX; i++) {
198 if (options[i].c_value && (options[i].c_value != save[i].c_value))
199 free(options[i].c_value);
202 for (i = 0; i < VOLOPT_MAX; i++) {
203 if (options[i].c_value)
204 free(options[i].c_value);
210 /* handle variable substitutions. here's what we understand:
211 * $b -> basename of path
212 * $c -> client ip/appletalk address
213 * $d -> volume pathname on server
214 * $f -> full name (whatever's in the gecos field)
217 * $i -> client ip/appletalk address without port
218 * $s -> server name (hostname if it doesn't exist)
219 * $u -> username (guest is usually nobody)
220 * $v -> volume name or basename if null
221 * $z -> zone (may not exist)
226 #define is_var(a, b) (strncmp((a), (b), 2) == 0)
228 static char *volxlate(AFPObj *obj, char *dest, size_t destlen,
229 char *src, struct passwd *pwd, char *path, char *volname)
239 dest = calloc(destlen +1, 1);
245 strlcpy(dest, src, destlen +1);
246 if ((p = strchr(src, '$')) == NULL) /* nothing to do */
249 /* first part of the path. just forward to the next variable. */
250 len = MIN((size_t)(p - src), destlen);
256 while (p && destlen > 0) {
257 /* now figure out what the variable is */
259 if (is_var(p, "$b")) {
261 if ((q = strrchr(path, '/')) == NULL)
263 else if (*(q + 1) != '\0')
266 } else if (is_var(p, "$c")) {
267 if (obj->proto == AFPPROTO_ASP) {
268 ASP asp = obj->handle;
270 len = sprintf(dest, "%u.%u", ntohs(asp->asp_sat.sat_addr.s_net),
271 asp->asp_sat.sat_addr.s_node);
275 } else if (obj->proto == AFPPROTO_DSI) {
276 DSI *dsi = obj->handle;
278 len = sprintf(dest, "%s:%u", inet_ntoa(dsi->client.sin_addr),
279 ntohs(dsi->client.sin_port));
283 } else if (is_var(p, "$d")) {
285 } else if (is_var(p, "$f")) {
286 if ((q = strchr(pwd->pw_gecos, ',')))
289 } else if (is_var(p, "$g")) {
290 struct group *grp = getgrgid(pwd->pw_gid);
293 } else if (is_var(p, "$h")) {
294 q = obj->options.hostname;
295 } else if (is_var(p, "$i")) {
296 if (obj->proto == AFPPROTO_ASP) {
297 ASP asp = obj->handle;
299 len = sprintf(dest, "%u", ntohs(asp->asp_sat.sat_addr.s_net));
303 } else if (obj->proto == AFPPROTO_DSI) {
304 DSI *dsi = obj->handle;
306 q = inet_ntoa(dsi->client.sin_addr);
308 } else if (is_var(p, "$s")) {
311 else if (obj->options.server) {
312 q = obj->options.server;
314 q = obj->options.hostname;
315 } else if (is_var(p, "$u")) {
317 } else if (is_var(p, "$v")) {
322 if ((q = strrchr(path, '/')) == NULL)
324 else if (*(q + 1) != '\0')
327 } else if (is_var(p, "$z")) {
329 } else if (is_var(p, "$$")) {
334 /* copy the stuff over. if we don't understand something that we
335 * should, just skip it over. */
337 len = MIN(p == q ? 2 : strlen(q), destlen);
338 strncpy(dest, q, len);
343 /* stuff up to next $ */
345 p = strchr(src, '$');
346 len = p ? MIN((size_t)(p - src), destlen) : destlen;
348 strncpy(dest, src, len);
356 /* to make sure that val is valid, make sure to select an opt that
358 static int optionok(const char *buf, const char *opt, const char *val)
360 if (!strstr(buf,opt))
368 /* -------------------- */
369 static void setoption(struct vol_option *options, struct vol_option *save, int opt, const char *val)
371 if (options[opt].c_value && (!save || options[opt].c_value != save[opt].c_value))
372 free(options[opt].c_value);
373 options[opt].c_value = strdup(val + 1);
376 /* ------------------------------------------
377 handle all the options. tmp can't be NULL. */
378 static void volset(struct vol_option *options, struct vol_option *save,
379 char *volname, int vlen,
384 val = strchr(tmp, ':');
386 /* we'll assume it's a volume name. */
387 strncpy(volname, tmp, vlen);
392 LOG(log_debug, logtype_afpd, "Parsing volset %s", val);
394 if (optionok(tmp, "allow:", val)) {
395 setoption(options, save, VOLOPT_ALLOW, val);
397 } else if (optionok(tmp, "deny:", val)) {
398 setoption(options, save, VOLOPT_DENY, val);
400 } else if (optionok(tmp, "rwlist:", val)) {
401 setoption(options, save, VOLOPT_RWLIST, val);
403 } else if (optionok(tmp, "rolist:", val)) {
404 setoption(options, save, VOLOPT_ROLIST, val);
406 } else if (optionok(tmp, "codepage:", val)) {
407 LOG (log_error, logtype_afpd, "The old codepage system has been removed. Please make sure to read the documentation !!!!");
408 /* Make sure we don't screw anything */
410 } else if (optionok(tmp, "volcharset:", val)) {
411 setoption(options, save, VOLOPT_ENCODING, val);
412 } else if (optionok(tmp, "maccharset:", val)) {
413 setoption(options, save, VOLOPT_MACCHARSET, val);
414 } else if (optionok(tmp, "veto:", val)) {
415 setoption(options, save, VOLOPT_VETO, val);
416 } else if (optionok(tmp, "cnidscheme:", val)) {
417 setoption(options, save, VOLOPT_CNIDSCHEME, val);
418 } else if (optionok(tmp, "casefold:", val)) {
419 if (strcasecmp(val + 1, "tolower") == 0)
420 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMLOWER;
421 else if (strcasecmp(val + 1, "toupper") == 0)
422 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMUPPER;
423 else if (strcasecmp(val + 1, "xlatelower") == 0)
424 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UUPPERMLOWER;
425 else if (strcasecmp(val + 1, "xlateupper") == 0)
426 options[VOLOPT_CASEFOLD].i_value = AFPVOL_ULOWERMUPPER;
427 } else if (optionok(tmp, "adouble:", val)) {
428 if (strcasecmp(val + 1, "v1") == 0)
429 options[VOLOPT_ADOUBLE].i_value = AD_VERSION1;
430 #if AD_VERSION == AD_VERSION2
431 else if (strcasecmp(val + 1, "v2") == 0)
432 options[VOLOPT_ADOUBLE].i_value = AD_VERSION2;
433 else if (strcasecmp(val + 1, "osx") == 0)
434 options[VOLOPT_ADOUBLE].i_value = AD_VERSION2_OSX;
435 else if (strcasecmp(val + 1, "ads") == 0)
436 options[VOLOPT_ADOUBLE].i_value = AD_VERSION1_ADS;
438 } else if (optionok(tmp, "options:", val)) {
441 if ((p = strtok(val + 1, ",")) == NULL) /* nothing */
445 if (strcasecmp(p, "prodos") == 0)
446 options[VOLOPT_FLAGS].i_value |= AFPVOL_A2VOL;
447 else if (strcasecmp(p, "mswindows") == 0) {
448 options[VOLOPT_FLAGS].i_value |= AFPVOL_MSWINDOWS | AFPVOL_USEDOTS;
449 } else if (strcasecmp(p, "crlf") == 0)
450 options[VOLOPT_FLAGS].i_value |= AFPVOL_CRLF;
451 else if (strcasecmp(p, "noadouble") == 0)
452 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOADOUBLE;
453 else if (strcasecmp(p, "ro") == 0)
454 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
455 else if (strcasecmp(p, "nohex") == 0)
456 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOHEX;
457 else if (strcasecmp(p, "usedots") == 0)
458 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS;
459 else if (strcasecmp(p, "limitsize") == 0)
460 options[VOLOPT_FLAGS].i_value |= AFPVOL_LIMITSIZE;
461 /* support for either "dropbox" or "dropkludge" */
462 else if (strcasecmp(p, "dropbox") == 0)
463 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
464 else if (strcasecmp(p, "dropkludge") == 0)
465 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
466 else if (strcasecmp(p, "nofileid") == 0)
467 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOFILEID;
468 else if (strcasecmp(p, "nostat") == 0)
469 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOSTAT;
470 else if (strcasecmp(p, "preexec_close") == 0)
471 options[VOLOPT_PREEXEC].i_value = 1;
472 else if (strcasecmp(p, "root_preexec_close") == 0)
473 options[VOLOPT_ROOTPREEXEC].i_value = 1;
474 else if (strcasecmp(p, "upriv") == 0)
475 options[VOLOPT_FLAGS].i_value |= AFPVOL_UNIX_PRIV;
476 else if (strcasecmp(p, "nodev") == 0)
477 options[VOLOPT_FLAGS].i_value |= AFPVOL_NODEV;
478 else if (strcasecmp(p, "caseinsensitive") == 0)
479 options[VOLOPT_FLAGS].i_value |= AFPVOL_CASEINSEN;
480 else if (strcasecmp(p, "illegalseq") == 0)
481 options[VOLOPT_FLAGS].i_value |= AFPVOL_EILSEQ;
482 else if (strcasecmp(p, "cachecnid") == 0)
483 options[VOLOPT_FLAGS].i_value |= AFPVOL_CACHE;
485 p = strtok(NULL, ",");
488 } else if (optionok(tmp, "dbpath:", val)) {
489 setoption(options, save, VOLOPT_DBPATH, val);
491 } else if (optionok(tmp, "umask:", val)) {
492 options[VOLOPT_UMASK].i_value = (int)strtol(val +1, (char **)NULL, 8);
493 } else if (optionok(tmp, "mapchars:",val)) {
494 setoption(options, save, VOLOPT_MAPCHARS, val);
496 } else if (optionok(tmp, "password:", val)) {
497 setoption(options, save, VOLOPT_PASSWORD, val);
501 /* this code allows forced uid/gid per volume settings */
502 } else if (optionok(tmp, "forceuid:", val)) {
503 setoption(options, save, VOLOPT_FORCEUID, val);
504 } else if (optionok(tmp, "forcegid:", val)) {
505 setoption(options, save, VOLOPT_FORCEGID, val);
507 #endif /* FORCE_UIDGID */
508 } else if (optionok(tmp, "root_preexec:", val)) {
509 setoption(options, save, VOLOPT_ROOTPREEXEC, val);
511 } else if (optionok(tmp, "preexec:", val)) {
512 setoption(options, save, VOLOPT_PREEXEC, val);
514 } else if (optionok(tmp, "root_postexec:", val)) {
515 setoption(options, save, VOLOPT_ROOTPOSTEXEC, val);
517 } else if (optionok(tmp, "postexec:", val)) {
518 setoption(options, save, VOLOPT_POSTEXEC, val);
521 /* ignore unknown options */
522 LOG(log_debug, logtype_afpd, "ignoring unknown volume option: %s", tmp);
527 /* ----------------- */
528 static void showvol(const ucs2_t *name)
531 for ( volume = Volumes; volume; volume = volume->v_next ) {
532 if (volume->v_hide && !strcasecmp_w( volume->v_name, name ) ) {
539 /* ------------------------------- */
540 static int creatvol(AFPObj *obj, struct passwd *pwd,
541 char *path, char *name,
542 struct vol_option *options,
543 const int user /* user defined volume */
551 if ( name == NULL || *name == '\0' ) {
552 if ((name = strrchr( path, '/' )) == NULL) {
553 return -1; /* Obviously not a fully qualified path */
556 /* if you wish to share /, you need to specify a name. */
561 vlen = strlen( name );
562 if ( vlen > AFPVOL_NAMELEN ) {
563 vlen = AFPVOL_NAMELEN;
564 name[AFPVOL_NAMELEN] = '\0';
567 /* convert name to UCS2 first */
568 if ( 0 >= ( vlen = convert_string(obj->options.unixcharset, CH_UCS2, name, vlen, tmpname, 512)) )
571 for ( volume = Volumes; volume; volume = volume->v_next ) {
572 if ( strcasecmp_w( volume->v_name, tmpname ) == 0 ) {
573 if (volume->v_deleted) {
574 volume->v_new = hide = 1;
577 return -1; /* Won't be able to access it, anyway... */
583 if (!( volume = (struct vol *)calloc(1, sizeof( struct vol ))) ) {
584 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
587 if ( NULL == ( volume->v_name = strdup_w(tmpname))) {
588 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
592 if (!( volume->v_path = (char *)malloc( strlen( path ) + 1 )) ) {
593 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
594 free(volume->v_name);
598 volume->v_hide = hide;
599 strcpy( volume->v_path, path );
603 #endif /* __svr4__ */
604 /* os X start at 1 and use network order ie. 1 2 3 */
605 volume->v_vid = ++lastvid;
606 volume->v_vid = htons(volume->v_vid);
610 /* should we casefold? */
611 volume->v_casefold = options[VOLOPT_CASEFOLD].i_value;
613 /* shift in some flags */
614 volume->v_flags = options[VOLOPT_FLAGS].i_value;
616 volume->v_ad_options = 0;
617 if ((volume->v_flags & AFPVOL_NODEV))
618 volume->v_ad_options |= ADVOL_NODEV;
619 if ((volume->v_flags & AFPVOL_CACHE))
620 volume->v_ad_options |= ADVOL_CACHE;
621 if ((volume->v_flags & AFPVOL_UNIX_PRIV))
622 volume->v_ad_options |= ADVOL_UNIXPRIV;
624 if (options[VOLOPT_PASSWORD].c_value)
625 volume->v_password = strdup(options[VOLOPT_PASSWORD].c_value);
627 if (options[VOLOPT_VETO].c_value)
628 volume->v_veto = strdup(options[VOLOPT_VETO].c_value);
630 if (options[VOLOPT_ENCODING].c_value)
631 volume->v_volcodepage = strdup(options[VOLOPT_ENCODING].c_value);
633 if (options[VOLOPT_MACCHARSET].c_value)
634 volume->v_maccodepage = strdup(options[VOLOPT_MACCHARSET].c_value);
636 if (options[VOLOPT_DBPATH].c_value)
637 volume->v_dbpath = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_DBPATH].c_value, pwd, path, name);
639 if (options[VOLOPT_CNIDSCHEME].c_value)
640 volume->v_cnidscheme = strdup(options[VOLOPT_CNIDSCHEME].c_value);
642 if (options[VOLOPT_UMASK].i_value)
643 volume->v_umask = (mode_t)options[VOLOPT_UMASK].i_value;
645 if (options[VOLOPT_ADOUBLE].i_value)
646 volume->v_adouble = options[VOLOPT_ADOUBLE].i_value;
648 volume->v_adouble = AD_VERSION;
652 if (options[VOLOPT_FORCEUID].c_value) {
653 volume->v_forceuid = strdup(options[VOLOPT_FORCEUID].c_value);
655 volume->v_forceuid = NULL; /* set as null so as to return 0 later on */
658 if (options[VOLOPT_FORCEGID].c_value) {
659 volume->v_forcegid = strdup(options[VOLOPT_FORCEGID].c_value);
661 volume->v_forcegid = NULL; /* set as null so as to return 0 later on */
665 if (options[VOLOPT_PREEXEC].c_value)
666 volume->v_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_PREEXEC].c_value, pwd, path, name);
667 volume->v_preexec_close = options[VOLOPT_PREEXEC].i_value;
669 if (options[VOLOPT_POSTEXEC].c_value)
670 volume->v_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_POSTEXEC].c_value, pwd, path, name);
672 if (options[VOLOPT_ROOTPREEXEC].c_value)
673 volume->v_root_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPREEXEC].c_value, pwd, path, name);
674 volume->v_root_preexec_close = options[VOLOPT_ROOTPREEXEC].i_value;
676 if (options[VOLOPT_ROOTPOSTEXEC].c_value)
677 volume->v_root_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPOSTEXEC].c_value, pwd, path, name);
681 volume->v_next = Volumes;
686 /* ---------------- */
687 static char *myfgets( buf, size, fp )
696 while ((EOF != ( c = getc( fp )) ) && ( size > 0 )) {
697 if ( c == '\n' || c == '\r' ) {
715 /* check access list. this function wants something of the following
717 * @group,name,name2,@group2,name3
719 * a NULL argument allows everybody to have access.
720 * we return three things:
722 * 0: list exists, but name isn't in it
726 #ifndef NO_REAL_USER_NAME
727 /* authentication is case insensitive
728 * FIXME should we do the same with group name?
730 #define access_strcmp strcasecmp
733 #define access_strcmp strcmp
737 static int accessvol(args, name)
741 char buf[MAXPATHLEN + 1], *p;
747 strlcpy(buf, args, sizeof(buf));
748 if ((p = strtok(buf, ",")) == NULL) /* nothing, return okay */
752 if (*p == '@') { /* it's a group */
753 if ((gr = getgrnam(p + 1)) && gmem(gr->gr_gid))
755 } else if (access_strcmp(p, name) == 0) /* it's a user name */
757 p = strtok(NULL, ",");
763 static void setextmap( ext, type, creator, user)
764 char *ext, *type, *creator;
770 if (Extmap == NULL) {
771 if (( Extmap = calloc(1, sizeof( struct extmap ))) == NULL ) {
772 LOG(log_error, logtype_afpd, "setextmap: calloc: %s", strerror(errno) );
777 for ( em = Extmap, cnt = 0; em->em_ext; em++, cnt++) {
778 if ( (strdiacasecmp( em->em_ext, ext )) == 0 ) {
783 if ( em->em_ext == NULL ) {
784 if (!(Extmap = realloc( Extmap, sizeof( struct extmap ) * (cnt +2))) ) {
785 LOG(log_error, logtype_afpd, "setextmap: realloc: %s", strerror(errno) );
788 (Extmap +cnt +1)->em_ext = NULL;
790 } else if ( !user ) {
796 if (!(em->em_ext = strdup( ext))) {
797 LOG(log_error, logtype_afpd, "setextmap: strdup: %s", strerror(errno) );
801 if ( *type == '\0' ) {
802 memcpy(em->em_type, "????", sizeof( em->em_type ));
804 memcpy(em->em_type, type, sizeof( em->em_type ));
806 if ( *creator == '\0' ) {
807 memcpy(em->em_creator, "UNIX", sizeof( em->em_creator ));
809 memcpy(em->em_creator, creator, sizeof( em->em_creator ));
813 /* -------------------------- */
814 static int extmap_cmp(const void *map1, const void *map2)
816 const struct extmap *em1 = map1;
817 const struct extmap *em2 = map2;
818 return strdiacasecmp(em1->em_ext, em2->em_ext);
821 static void sortextmap( void)
826 if ((em = Extmap) == NULL) {
834 qsort(Extmap, Extmap_cnt, sizeof(struct extmap), extmap_cmp);
835 if (*Extmap->em_ext == 0) {
836 /* the first line is really "." the default entry,
837 * we remove the leading '.' in setextmap
844 /* ----------------------
846 static void free_extmap( void)
851 for ( em = Extmap; em->em_ext; em++) {
861 /* ----------------------
863 static int volfile_changed(struct afp_volume_name *p)
873 if (!stat( name, &st) && st.st_mtime > p->mtime) {
874 p->mtime = st.st_mtime;
880 /* ----------------------
881 * Read a volume configuration file and add the volumes contained within to
882 * the global volume list. If p2 is non-NULL, the file that is opened is
885 * Lines that begin with # and blank lines are ignored.
886 * Volume lines are of the form:
887 * <unix path> [<volume name>] [allow:<user>,<@group>,...] \
888 * [codepage:<file>] [casefold:<num>]
889 * <extension> TYPE [CREATOR]
891 static int readvolfile(obj, p1, p2, user, pwent)
893 struct afp_volume_name *p1;
896 struct passwd *pwent;
899 char path[ MAXPATHLEN + 1], tmp[ MAXPATHLEN + 1],
900 volname[ AFPVOL_NAMELEN + 1 ], buf[ BUFSIZ ],
901 type[ 5 ], creator[ 5 ];
904 struct vol_option options[VOLOPT_NUM], save_options[VOLOPT_NUM];
912 strcpy( path, p1->name );
919 p1->full_name = strdup(path);
922 if (NULL == ( fp = fopen( path, "r" )) ) {
926 if (fd != -1 && !fstat( fd, &st) ) {
927 p1->mtime = st.st_mtime;
930 memset(save_options, 0, sizeof(save_options));
931 while ( myfgets( buf, sizeof( buf ), fp ) != NULL ) {
932 initline( strlen( buf ), buf );
933 parseline( sizeof( path ) - 1, path );
940 /* change the default options for this file */
941 if (strncmp(path, VOLOPT_DEFAULT, VOLOPT_DEFAULT_LEN) == 0) {
943 for (i = 0; i < VOLOPT_NUM; i++) {
944 if (parseline( sizeof( path ) - VOLOPT_DEFAULT_LEN - 1,
945 path + VOLOPT_DEFAULT_LEN) < 0)
947 volset(save_options, NULL, tmp, sizeof(tmp) - 1,
948 path + VOLOPT_DEFAULT_LEN);
954 if (( p = strchr( path, '/' )) != NULL ) {
962 if ( u == NULL || *u == '\0' || ( pw = getpwnam( u )) == NULL ) {
965 strcpy( tmp, pw->pw_dir );
966 if ( p != NULL && *p != '\0' ) {
970 /* Tag a user's home directory with their umask. Note, this will
971 * be overwritten if the user actually specifies a umask: option
972 * for a '~' volume. */
973 save_options[VOLOPT_UMASK].i_value = obj->options.save_mask;
977 /* send path through variable substitution */
978 if (*path != '~') /* need to copy path to tmp */
981 pwent = getpwnam(obj->username);
982 volxlate(obj, path, sizeof(path) - 1, tmp, pwent, NULL, NULL);
984 /* this is sort of braindead. basically, i want to be
985 * able to specify things in any order, but i don't want to
986 * re-write everything.
988 * currently we have options:
996 * options:prodos,crlf,noadouble,ro...
1001 * namemask:x,y,!z (not implemented yet)
1003 memcpy(options, save_options, sizeof(options));
1006 /* read in up to VOLOP_NUM possible options */
1007 for (i = 0; i < VOLOPT_NUM; i++) {
1008 if (parseline( sizeof( tmp ) - 1, tmp ) < 0)
1011 volset(options, save_options, volname, sizeof(volname) - 1, tmp);
1014 /* check allow/deny lists:
1015 allow -> either no list (-1), or in list (1)
1016 deny -> either no list (-1), or not in list (0) */
1017 if (accessvol(options[VOLOPT_ALLOW].c_value, obj->username) &&
1018 (accessvol(options[VOLOPT_DENY].c_value, obj->username) < 1)) {
1020 /* handle read-only behaviour. semantics:
1021 * 1) neither the rolist nor the rwlist exist -> rw
1022 * 2) rolist exists -> ro if user is in it.
1023 * 3) rwlist exists -> ro unless user is in it. */
1024 if (((options[VOLOPT_FLAGS].i_value & AFPVOL_RO) == 0) &&
1025 ((accessvol(options[VOLOPT_ROLIST].c_value,
1026 obj->username) == 1) ||
1027 !accessvol(options[VOLOPT_RWLIST].c_value,
1029 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
1031 /* do variable substitution for volname */
1032 volxlate(obj, tmp, sizeof(tmp) - 1, volname, pwent, path, NULL);
1033 creatvol(obj, pwent, path, tmp, options, p2 != NULL);
1035 volfree(options, save_options);
1039 parseline( sizeof( type ) - 1, type );
1040 parseline( sizeof( creator ) - 1, creator );
1041 setextmap( path, type, creator, user);
1048 volfree(save_options, NULL);
1050 if ( fclose( fp ) != 0 ) {
1051 LOG(log_error, logtype_afpd, "readvolfile: fclose: %s", strerror(errno) );
1057 /* ------------------------------- */
1058 static void volume_free(struct vol *vol)
1063 free(vol->v_password);
1065 free(vol->v_volcodepage);
1066 free(vol->v_maccodepage);
1067 free(vol->v_cnidscheme);
1068 free(vol->v_dbpath);
1071 free(vol->v_forceuid);
1072 free(vol->v_forcegid);
1073 #endif /* FORCE_UIDGID */
1076 /* ------------------------------- */
1077 static void free_volumes(void )
1080 struct vol *nvol, *ovol;
1082 for ( vol = Volumes; vol; vol = vol->v_next ) {
1083 if (( vol->v_flags & AFPVOL_OPEN ) ) {
1090 for ( vol = Volumes, ovol = NULL; vol; vol = nvol) {
1093 if (vol->v_name == NULL) {
1094 if (Volumes == vol) {
1099 ovol->v_next = nvol;
1109 /* ------------------------------- */
1110 static void volume_unlink(struct vol *volume)
1112 struct vol *vol, *ovol, *nvol;
1114 if (volume == Volumes) {
1115 Volumes = Volumes->v_next;
1118 for ( vol = Volumes->v_next, ovol = Volumes; vol; vol = nvol) {
1121 if (vol == volume) {
1122 ovol->v_next = nvol;
1131 static int getvolspace( vol, bfree, btotal, xbfree, xbtotal, bsize )
1133 u_int32_t *bfree, *btotal, *bsize;
1134 VolSpace *xbfree, *xbtotal;
1138 #ifndef NO_QUOTA_SUPPORT
1139 VolSpace qfree, qtotal;
1142 spaceflag = AFPVOL_GVSMASK & vol->v_flags;
1143 /* report up to 2GB if afp version is < 2.2 (4GB if not) */
1144 maxsize = (vol->v_flags & AFPVOL_A2VOL) ? 0x01fffe00 :
1145 (((afp_version < 22) || (vol->v_flags & AFPVOL_LIMITSIZE))
1146 ? 0x7fffffffL : 0xffffffffL);
1149 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_AFSGVS ) {
1150 if ( afs_getvolspace( vol, xbfree, xbtotal, bsize ) == AFP_OK ) {
1151 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_AFSGVS;
1152 goto getvolspace_done;
1157 if (( rc = ustatfs_getvolspace( vol, xbfree, xbtotal,
1158 bsize)) != AFP_OK ) {
1162 #define min(a,b) ((a)<(b)?(a):(b))
1163 #ifndef NO_QUOTA_SUPPORT
1164 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_UQUOTA ) {
1165 if ( uquota_getvolspace( vol, &qfree, &qtotal, *bsize ) == AFP_OK ) {
1166 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_UQUOTA;
1167 *xbfree = min(*xbfree, qfree);
1168 *xbtotal = min( *xbtotal, qtotal);
1169 goto getvolspace_done;
1173 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_USTATFS;
1176 *bfree = min( *xbfree, maxsize);
1177 *btotal = min( *xbtotal, maxsize);
1181 /* -----------------------
1182 * set volume creation date
1183 * avoid duplicate, well at least it tries
1185 static void vol_setdate(u_int16_t id, struct adouble *adp, time_t date)
1188 struct vol *vol = Volumes;
1190 for ( volume = Volumes; volume; volume = volume->v_next ) {
1191 if (volume->v_vid == id) {
1194 else if ((time_t)(AD_DATE_FROM_UNIX(date)) == volume->v_ctime) {
1196 volume = Volumes; /* restart */
1199 vol->v_ctime = AD_DATE_FROM_UNIX(date);
1200 ad_setdate(adp, AD_DATE_CREATE | AD_DATE_UNIX, date);
1203 /* ----------------------- */
1204 static int getvolparams( bitmap, vol, st, buf, buflen )
1212 int bit = 0, isad = 1;
1215 u_int32_t bfree, btotal, bsize;
1216 VolSpace xbfree, xbtotal; /* extended bytes */
1217 char *data, *nameoff = NULL;
1220 /* courtesy of jallison@whistle.com:
1221 * For MacOS8.x support we need to create the
1222 * .Parent file here if it doesn't exist. */
1224 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1225 if ( ad_open_metadata( vol->v_path, vol_noadouble(vol) | ADFLAGS_DIR, O_CREAT, &ad) < 0 ) {
1227 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1229 } else if (ad_get_HF_flags( &ad ) & O_CREAT) {
1230 slash = strrchr( vol->v_path, '/' );
1234 slash = vol->v_path;
1235 if (ad_getentryoff(&ad, ADEID_NAME)) {
1236 ad_setentrylen( &ad, ADEID_NAME, strlen( slash ));
1237 memcpy(ad_entry( &ad, ADEID_NAME ), slash,
1238 ad_getentrylen( &ad, ADEID_NAME ));
1240 vol_setdate(vol->v_vid, &ad, st->st_mtime);
1241 ad_flush_metadata(&ad);
1244 if (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0)
1245 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1247 vol->v_ctime = aint;
1250 if (( bitmap & ( (1<<VOLPBIT_BFREE)|(1<<VOLPBIT_BTOTAL) |
1251 (1<<VOLPBIT_XBFREE)|(1<<VOLPBIT_XBTOTAL) |
1252 (1<<VOLPBIT_BSIZE)) ) != 0 ) {
1253 if ( getvolspace( vol, &bfree, &btotal, &xbfree, &xbtotal,
1256 ad_close( &ad, ADFLAGS_HF );
1258 return( AFPERR_PARAM );
1263 while ( bitmap != 0 ) {
1264 while (( bitmap & 1 ) == 0 ) {
1272 if (0 == (vol->v_flags & AFPVOL_NOFILEID) && vol->v_cdb != NULL &&
1273 (vol->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1274 ashort = VOLPBIT_ATTR_FILEID;
1276 /* check for read-only.
1277 * NOTE: we don't actually set the read-only flag unless
1278 * it's passed in that way as it's possible to mount
1279 * a read-write filesystem under a read-only one. */
1280 if ((vol->v_flags & AFPVOL_RO) ||
1281 ((utime(vol->v_path, NULL) < 0) && (errno == EROFS))) {
1282 ashort |= VOLPBIT_ATTR_RO;
1284 ashort |= VOLPBIT_ATTR_CATSEARCH;
1285 if (afp_version >= 30) {
1286 ashort |= VOLPBIT_ATTR_UTF8;
1287 if (vol->v_flags & AFPVOL_UNIX_PRIV)
1288 ashort |= VOLPBIT_ATTR_UNIXPRIV;
1290 ashort = htons(ashort);
1291 memcpy(data, &ashort, sizeof( ashort ));
1292 data += sizeof( ashort );
1296 ashort = htons( AFPVOLSIG_DEFAULT );
1297 memcpy(data, &ashort, sizeof( ashort ));
1298 data += sizeof( ashort );
1301 case VOLPBIT_CDATE :
1302 aint = vol->v_ctime;
1303 memcpy(data, &aint, sizeof( aint ));
1304 data += sizeof( aint );
1307 case VOLPBIT_MDATE :
1308 if ( st->st_mtime > vol->v_mtime ) {
1309 vol->v_mtime = st->st_mtime;
1311 aint = AD_DATE_FROM_UNIX(vol->v_mtime);
1312 memcpy(data, &aint, sizeof( aint ));
1313 data += sizeof( aint );
1316 case VOLPBIT_BDATE :
1317 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1318 aint = AD_DATE_START;
1319 memcpy(data, &aint, sizeof( aint ));
1320 data += sizeof( aint );
1324 memcpy(data, &vol->v_vid, sizeof( vol->v_vid ));
1325 data += sizeof( vol->v_vid );
1328 case VOLPBIT_BFREE :
1329 bfree = htonl( bfree );
1330 memcpy(data, &bfree, sizeof( bfree ));
1331 data += sizeof( bfree );
1334 case VOLPBIT_BTOTAL :
1335 btotal = htonl( btotal );
1336 memcpy(data, &btotal, sizeof( btotal ));
1337 data += sizeof( btotal );
1340 #ifndef NO_LARGE_VOL_SUPPORT
1341 case VOLPBIT_XBFREE :
1342 xbfree = hton64( xbfree );
1343 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1344 bcopy(&xbfree, data, sizeof(xbfree));
1345 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1346 memcpy(data, &xbfree, sizeof( xbfree ));
1347 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1348 data += sizeof( xbfree );
1351 case VOLPBIT_XBTOTAL :
1352 xbtotal = hton64( xbtotal );
1353 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1354 bcopy(&xbtotal, data, sizeof(xbtotal));
1355 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1356 memcpy(data, &xbtotal, sizeof( xbtotal ));
1357 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1358 data += sizeof( xbfree );
1360 #endif /* ! NO_LARGE_VOL_SUPPORT */
1364 data += sizeof( u_int16_t );
1367 case VOLPBIT_BSIZE: /* block size */
1368 bsize = htonl(bsize);
1369 memcpy(data, &bsize, sizeof(bsize));
1370 data += sizeof(bsize);
1375 ad_close( &ad, ADFLAGS_HF );
1377 return( AFPERR_BITMAP );
1383 ashort = htons( data - buf );
1384 memcpy(nameoff, &ashort, sizeof( ashort ));
1385 aint = ucs2_to_charset( (utf8_encoding()?CH_UTF8_MAC:vol->v_maccharset), vol->v_name, data+1, 255);
1395 ad_close_metadata( &ad);
1397 *buflen = data - buf;
1401 /* ------------------------- */
1402 static int stat_vol(u_int16_t bitmap, struct vol *vol, char *rbuf, int *rbuflen)
1407 if ( stat( vol->v_path, &st ) < 0 ) {
1409 return( AFPERR_PARAM );
1411 /* save the volume device number */
1412 vol->v_dev = st.st_dev;
1414 buflen = *rbuflen - sizeof( bitmap );
1415 if (( ret = getvolparams( bitmap, vol, &st,
1416 rbuf + sizeof( bitmap ), &buflen )) != AFP_OK ) {
1420 *rbuflen = buflen + sizeof( bitmap );
1421 bitmap = htons( bitmap );
1422 memcpy(rbuf, &bitmap, sizeof( bitmap ));
1427 /* ------------------------------- */
1428 void load_volumes(AFPObj *obj)
1430 struct passwd *pwent;
1435 /* check files date */
1436 if (obj->options.defaultvol.loaded) {
1437 changed = volfile_changed(&obj->options.defaultvol);
1439 if (obj->options.systemvol.loaded) {
1440 changed |= volfile_changed(&obj->options.systemvol);
1442 if (obj->options.uservol.loaded) {
1443 changed |= volfile_changed(&obj->options.uservol);
1452 pwent = getpwnam(obj->username);
1453 if ( (obj->options.flags & OPTION_USERVOLFIRST) == 0 ) {
1454 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent);
1457 if ((*obj->username == '\0') || (obj->options.flags & OPTION_NOUSERVOL)) {
1458 readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent);
1461 * Read user's AppleVolumes or .AppleVolumes file
1462 * If neither are readable, read the default volumes file. if
1463 * that doesn't work, create a user share.
1465 if (obj->options.uservol.name) {
1466 free(obj->options.uservol.name);
1468 obj->options.uservol.name = strdup(pwent->pw_dir);
1469 if ( readvolfile(obj, &obj->options.uservol, "AppleVolumes", 1, pwent) < 0 &&
1470 readvolfile(obj, &obj->options.uservol, ".AppleVolumes", 1, pwent) < 0 &&
1471 readvolfile(obj, &obj->options.uservol, "applevolumes", 1, pwent) < 0 &&
1472 readvolfile(obj, &obj->options.uservol, ".applevolumes", 1, pwent) < 0 &&
1473 obj->options.defaultvol.name != NULL ) {
1474 if (readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent) < 0)
1475 creatvol(obj, pwent, pwent->pw_dir, NULL, NULL, 1);
1478 if ( obj->options.flags & OPTION_USERVOLFIRST ) {
1479 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent );
1482 if ( obj->options.closevol ) {
1485 for ( vol = Volumes; vol; vol = vol->v_next ) {
1486 if (vol->v_deleted && !vol->v_new ) {
1495 /* ------------------------------- */
1496 int afp_getsrvrparms(obj, ibuf, ibuflen, rbuf, rbuflen )
1498 char *ibuf _U_, *rbuf;
1499 int ibuflen _U_, *rbuflen;
1512 for ( vcnt = 0, volume = Volumes; volume; volume = volume->v_next ) {
1513 if (!(volume->v_flags & AFPVOL_NOSTAT)) {
1514 if ( stat( volume->v_path, &st ) < 0 ) {
1515 LOG(log_info, logtype_afpd, "afp_getsrvrparms(%s): stat: %s",
1516 volume->v_path, strerror(errno) );
1517 continue; /* can't access directory */
1519 if (!S_ISDIR(st.st_mode)) {
1520 continue; /* not a dir */
1523 if (volume->v_hide) {
1524 continue; /* config file changed but the volume was mounted */
1526 len = ucs2_to_charset_allocate((utf8_encoding()?CH_UTF8_MAC:obj->options.maccharset),
1527 &namebuf, volume->v_name);
1528 if (len == (size_t)-1)
1531 /* set password bit if there's a volume password */
1532 *data = (volume->v_password) ? AFPSRVR_PASSWD : 0;
1534 /* Apple 2 clients running ProDOS-8 expect one volume to have
1535 bit 0 of this byte set. They will not recognize anything
1536 on the server unless this is the case. I have not
1537 completely worked this out, but it's related to booting
1538 from the server. Support for that function is a ways
1539 off.. <shirsch@ibm.net> */
1540 *data |= (volume->v_flags & AFPVOL_A2VOL) ? AFPSRVR_CONFIGINFO : 0;
1541 *data++ |= 0; /* UNIX PRIVS BIT ..., OSX doesn't seem to use it, so we don't either */
1543 memcpy(data, namebuf, len );
1549 *rbuflen = data - rbuf;
1551 if ( gettimeofday( &tv, 0 ) < 0 ) {
1552 LOG(log_error, logtype_afpd, "afp_getsrvrparms(%s): gettimeofday: %s", volume->v_path, strerror(errno) );
1554 return AFPERR_PARAM;
1556 tv.tv_sec = AD_DATE_FROM_UNIX(tv.tv_sec);
1557 memcpy(data, &tv.tv_sec, sizeof( u_int32_t));
1558 data += sizeof( u_int32_t);
1563 /* -------------------------
1564 * we are the user here
1566 int afp_openvol(obj, ibuf, ibuflen, rbuf, rbuflen )
1569 int ibuflen _U_, *rbuflen;
1579 char path[ MAXPATHLEN + 1];
1584 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1585 bitmap = ntohs( bitmap );
1586 ibuf += sizeof( bitmap );
1587 if (( bitmap & (1<<VOLPBIT_VID)) == 0 ) {
1589 return AFPERR_BITMAP;
1592 len = (unsigned char)*ibuf++;
1593 volname = obj->oldtmp;
1594 namelen = convert_string( (utf8_encoding()?CH_UTF8_MAC:obj->options.maccharset), CH_UCS2,
1595 ibuf, len, volname, sizeof(obj->oldtmp));
1598 return AFPERR_PARAM;
1602 if ((len + 1) & 1) /* pad to an even boundary */
1607 for ( volume = Volumes; volume; volume = volume->v_next ) {
1608 if ( strcasecmp_w( (ucs2_t*) volname, volume->v_name ) == 0 ) {
1613 if ( volume == NULL ) {
1615 return AFPERR_PARAM;
1618 /* check for a volume password */
1619 if (volume->v_password && strncmp(ibuf, volume->v_password, VOLPASSLEN)) {
1621 return AFPERR_ACCESS;
1624 if (( volume->v_flags & AFPVOL_OPEN ) ) {
1625 /* the volume is already open */
1627 set_uidgid ( volume );
1629 return stat_vol(bitmap, volume, rbuf, rbuflen);
1632 /* initialize volume variables
1635 if (afp_version >= 30) {
1636 volume->max_filename = 255;
1639 volume->max_filename = MACFILELEN;
1642 volume->v_dir = volume->v_root = NULL;
1643 volume->v_hash = NULL;
1645 volume->v_flags |= AFPVOL_OPEN;
1646 volume->v_cdb = NULL;
1648 if (volume->v_root_preexec) {
1649 if ((ret = afprun(1, volume->v_root_preexec, NULL)) && volume->v_root_preexec_close) {
1650 LOG(log_error, logtype_afpd, "afp_openvol(%s): root preexec : %d", volume->v_path, ret );
1657 set_uidgid ( volume );
1660 if (volume->v_preexec) {
1661 if ((ret = afprun(0, volume->v_preexec, NULL)) && volume->v_preexec_close) {
1662 LOG(log_error, logtype_afpd, "afp_openvol(%s): preexec : %d", volume->v_path, ret );
1668 if ( stat( volume->v_path, &st ) < 0 ) {
1673 if ( chdir( volume->v_path ) < 0 ) {
1678 len = convert_string_allocate( CH_UCS2, (utf8_encoding()?CH_UTF8_MAC:obj->options.maccharset),
1679 volume->v_name, namelen, &vol_mname);
1680 if ( !vol_mname || len <= 0) {
1685 if ( NULL == getcwd(path, MAXPATHLEN)) {
1686 /* shouldn't be fatal but it will fail later */
1687 LOG(log_error, logtype_afpd, "afp_openvol(%s): volume pathlen too long", volume->v_path);
1692 if ((vol_uname = strrchr(path, '/')) == NULL)
1694 else if (*(vol_uname + 1) != '\0')
1697 if ((dir = dirnew(vol_mname, vol_uname) ) == NULL) {
1699 LOG(log_error, logtype_afpd, "afp_openvol(%s): malloc: %s", volume->v_path, strerror(errno) );
1705 dir->d_did = DIRDID_ROOT;
1706 dir->d_color = DIRTREE_COLOR_BLACK; /* root node is black */
1707 dir->d_m_name_ucs2 = strdup_w(volume->v_name);
1708 volume->v_dir = volume->v_root = dir;
1709 volume->v_hash = dirhash();
1711 curdir = volume->v_dir;
1712 if (volume->v_cnidscheme == NULL) {
1713 volume->v_cnidscheme = strdup(DEFAULT_CNID_SCHEME);
1714 LOG(log_warning, logtype_afpd, "Warning: No CNID scheme for volume %s. Using default.",
1717 if (volume->v_dbpath)
1718 volume->v_cdb = cnid_open (volume->v_dbpath, volume->v_umask, volume->v_cnidscheme, (volume->v_flags & AFPVOL_NODEV));
1720 volume->v_cdb = cnid_open (volume->v_path, volume->v_umask, volume->v_cnidscheme, (volume->v_flags & AFPVOL_NODEV));
1721 if (volume->v_cdb == NULL) {
1722 LOG(log_error, logtype_afpd, "Fatal error: cannot open CNID or invalid CNID backend for %s: %s",
1723 volume->v_path, volume->v_cnidscheme);
1730 if (!volume->v_volcodepage)
1731 volume->v_volcodepage = strdup("UTF8");
1733 if ( (charset_t) -1 == ( volume->v_volcharset = add_charset(volume->v_volcodepage)) ) {
1734 LOG (log_error, logtype_afpd, "Setting codepage %s as volume codepage failed", volume->v_volcodepage);
1739 if ( NULL == ( volume->v_vol = find_charset_functions(volume->v_volcodepage)) || volume->v_vol->flags & CHARSET_ICONV ) {
1740 LOG (log_warning, logtype_afpd, "WARNING: volume encoding %s is *not* supported by netatalk, expect problems !!!!", volume->v_volcodepage);
1743 if (!volume->v_maccodepage)
1744 volume->v_maccodepage = strdup(obj->options.maccodepage);
1746 if ( (charset_t) -1 == ( volume->v_maccharset = add_charset(volume->v_maccodepage)) ) {
1747 LOG (log_error, logtype_afpd, "Setting codepage %s as mac codepage failed", volume->v_maccodepage);
1752 if ( NULL == ( volume->v_mac = find_charset_functions(volume->v_maccodepage)) || ! (volume->v_mac->flags & CHARSET_CLIENT) ) {
1753 LOG (log_error, logtype_afpd, "Fatal error: mac charset %s not supported", volume->v_maccodepage);
1758 ret = stat_vol(bitmap, volume, rbuf, rbuflen);
1759 if (ret == AFP_OK) {
1761 if (!(volume->v_flags & AFPVOL_RO)) {
1762 handle_special_folders( volume );
1763 savevoloptions( volume);
1767 * If you mount a volume twice, the second time the trash appears on
1768 * the desk-top. That's because the Mac remembers the DID for the
1769 * trash (even for volumes in different zones, on different servers).
1770 * Just so this works better, we prime the DID cache with the trash,
1771 * fixing the trash at DID 17.
1772 * FIXME (RL): should it be done inside a CNID backend ? (always returning Trash DID when asked) ?
1774 if ((volume->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1776 /* FIXME find db time stamp */
1777 if (cnid_getstamp(volume->v_cdb, volume->v_stamp, sizeof(volume->v_stamp)) < 0) {
1778 LOG (log_error, logtype_afpd,
1779 "afp_openvol(%s): Fatal error: Unable to get stamp value from CNID backend",
1787 cname( volume, volume->v_dir, &p );
1793 if (volume->v_dir) {
1794 hash_free( volume->v_hash);
1795 dirfree( volume->v_dir );
1796 volume->v_dir = volume->v_root = NULL;
1799 volume->v_flags &= ~AFPVOL_OPEN;
1800 if (volume->v_cdb != NULL) {
1801 cnid_close(volume->v_cdb);
1802 volume->v_cdb = NULL;
1808 /* ------------------------- */
1809 static void closevol(struct vol *vol)
1814 hash_free( vol->v_hash);
1815 dirfree( vol->v_root );
1817 if (vol->v_cdb != NULL) {
1818 cnid_close(vol->v_cdb);
1822 if (vol->v_postexec) {
1823 afprun(0, vol->v_postexec, NULL);
1825 if (vol->v_root_postexec) {
1826 afprun(1, vol->v_root_postexec, NULL);
1830 /* ------------------------- */
1831 void close_all_vol(void)
1835 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
1836 if ( (ovol->v_flags & AFPVOL_OPEN) ) {
1837 ovol->v_flags &= ~AFPVOL_OPEN;
1843 /* ------------------------- */
1844 static void deletevol(struct vol *vol)
1848 vol->v_flags &= ~AFPVOL_OPEN;
1849 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
1850 if ( (ovol->v_flags & AFPVOL_OPEN) ) {
1854 if ( ovol != NULL ) {
1855 /* Even if chdir fails, we can't say afp_closevol fails. */
1856 if ( chdir( ovol->v_path ) == 0 ) {
1857 curdir = ovol->v_dir;
1862 if (vol->v_deleted) {
1863 showvol(vol->v_name);
1870 /* ------------------------- */
1871 int afp_closevol(obj, ibuf, ibuflen, rbuf, rbuflen )
1873 char *ibuf, *rbuf _U_;
1874 int ibuflen _U_, *rbuflen;
1881 memcpy(&vid, ibuf, sizeof( vid ));
1882 if (NULL == ( vol = getvolbyvid( vid )) ) {
1883 return( AFPERR_PARAM );
1891 /* ------------------------- */
1892 struct vol *getvolbyvid(const u_int16_t vid )
1896 for ( vol = Volumes; vol; vol = vol->v_next ) {
1897 if ( vid == vol->v_vid ) {
1901 if ( vol == NULL || ( vol->v_flags & AFPVOL_OPEN ) == 0 ) {
1907 #endif /* FORCE_UIDGID */
1912 /* ------------------------ */
1913 static int ext_cmp_key(const void *key, const void *obj)
1915 const char *p = key;
1916 const struct extmap *em = obj;
1917 return strdiacasecmp(p, em->em_ext);
1919 struct extmap *getextmap(const char *path)
1924 if (NULL == ( p = strrchr( path, '.' )) ) {
1925 return( Defextmap );
1928 if (!*p || !Extmap_cnt) {
1929 return( Defextmap );
1931 em = bsearch(p, Extmap, Extmap_cnt, sizeof(struct extmap), ext_cmp_key);
1935 return( Defextmap );
1939 /* ------------------------- */
1940 struct extmap *getdefextmap(void)
1942 return( Defextmap );
1945 /* --------------------------
1946 poll if a volume is changed by other processes.
1948 int pollvoltime(obj)
1955 if (!(afp_version > 21 && obj->options.server_notif))
1958 if ( gettimeofday( &tv, 0 ) < 0 )
1961 for ( vol = Volumes; vol; vol = vol->v_next ) {
1962 if ( (vol->v_flags & AFPVOL_OPEN) && vol->v_mtime + 30 < tv.tv_sec) {
1963 if ( !stat( vol->v_path, &st ) && vol->v_mtime != st.st_mtime ) {
1964 vol->v_mtime = st.st_mtime;
1965 if (!obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED))
1974 /* ------------------------- */
1975 void setvoltime(obj, vol )
1981 /* just looking at vol->v_mtime is broken seriously since updates
1982 * from other users afpd processes never are seen.
1983 * This is not the most elegant solution (a shared memory between
1984 * the afpd processes would come closer)
1987 if ( gettimeofday( &tv, 0 ) < 0 ) {
1988 LOG(log_error, logtype_afpd, "setvoltime(%s): gettimeofday: %s", vol->v_path, strerror(errno) );
1991 if( utime( vol->v_path, NULL ) < 0 ) {
1992 /* write of time failed ... probably a read only filesys,
1993 * where no other users can interfere, so there's no issue here
1997 /* a little granularity */
1998 if (vol->v_mtime < tv.tv_sec) {
1999 vol->v_mtime = tv.tv_sec;
2000 /* or finder doesn't update free space */
2001 if (afp_version > 21 && obj->options.server_notif) {
2002 obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED);
2007 /* ------------------------- */
2008 int afp_getvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
2011 int ibuflen _U_, *rbuflen;
2014 u_int16_t vid, bitmap;
2017 memcpy(&vid, ibuf, sizeof( vid ));
2018 ibuf += sizeof( vid );
2019 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2020 bitmap = ntohs( bitmap );
2022 if (NULL == ( vol = getvolbyvid( vid )) ) {
2024 return( AFPERR_PARAM );
2027 return stat_vol(bitmap, vol, rbuf, rbuflen);
2030 /* ------------------------- */
2031 int afp_setvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
2033 char *ibuf, *rbuf _U_;
2034 int ibuflen _U_, *rbuflen;
2038 u_int16_t vid, bitmap;
2044 memcpy(&vid, ibuf, sizeof( vid ));
2045 ibuf += sizeof( vid );
2046 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2047 bitmap = ntohs( bitmap );
2048 ibuf += sizeof(bitmap);
2050 if (( vol = getvolbyvid( vid )) == NULL ) {
2051 return( AFPERR_PARAM );
2054 if ((vol->v_flags & AFPVOL_RO))
2055 return AFPERR_VLOCK;
2057 /* we can only set the backup date. */
2058 if (bitmap != (1 << VOLPBIT_BDATE))
2059 return AFPERR_BITMAP;
2061 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2062 if ( ad_open( vol->v_path, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR,
2065 return AFPERR_VLOCK;
2067 return AFPERR_ACCESS;
2070 memcpy(&aint, ibuf, sizeof(aint));
2071 ad_setdate(&ad, AD_DATE_BACKUP, aint);
2072 ad_flush(&ad, ADFLAGS_HF);
2073 ad_close(&ad, ADFLAGS_HF);
2077 /* ------------------------- */
2078 int wincheck(const struct vol *vol, const char *path)
2082 if (!(vol->v_flags & AFPVOL_MSWINDOWS))
2085 /* empty paths are not allowed */
2086 if ((len = strlen(path)) == 0)
2089 /* leading or trailing whitespaces are not allowed, carriage returns
2090 * and probably other whitespace is okay, tabs are not allowed
2092 if ((path[0] == ' ') || (path[len-1] == ' '))
2095 /* certain characters are not allowed */
2096 if (strpbrk(path, MSWINDOWS_BADCHARS))
2099 /* everything else is okay */
2105 * precreate a folder
2106 * this is only intended for folders in the volume root
2107 * It will *not* work if the folder name contains extended characters
2109 static int create_special_folder (const struct vol *vol, const struct _special_folder *folder)
2118 p = (char *) malloc ( strlen(vol->v_path)+strlen(folder->name)+2);
2120 LOG(log_error, logtype_afpd,"malloc failed");
2124 q=strdup(folder->name);
2126 LOG(log_error, logtype_afpd,"malloc failed");
2130 strcpy(p, vol->v_path);
2135 if ((vol->v_casefold & AFPVOL_MTOUUPPER))
2137 else if ((vol->v_casefold & AFPVOL_MTOULOWER))
2143 if ( (ret = stat( p, &st )) < 0 ) {
2144 if (folder->precreate) {
2145 if (ad_mkdir(p, folder->mode)) {
2146 LOG(log_debug, logtype_afpd,"Creating '%s' failed in %s: %s", p, vol->v_path, strerror(errno));
2155 if ( !ret && folder->hide) {
2157 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2158 if (ad_open( p, vol_noadouble(vol) | ADFLAGS_HF|ADFLAGS_DIR,
2159 O_RDWR|O_CREAT, 0666, &ad) < 0) {
2164 if ((ad_get_HF_flags( &ad ) & O_CREAT) ) {
2165 if (ad_getentryoff(&ad, ADEID_NAME)) {
2166 ad_setentrylen( &ad, ADEID_NAME, strlen(folder->name));
2167 memcpy(ad_entry( &ad, ADEID_NAME ), folder->name,
2168 ad_getentrylen( &ad, ADEID_NAME ));
2172 ad_getattr(&ad, &attr);
2173 attr |= htons( ntohs( attr ) | ATTRBIT_INVISIBLE );
2174 ad_setattr(&ad, attr);
2176 /* do the same with the finder info */
2177 if (ad_entry(&ad, ADEID_FINDERI)) {
2178 memcpy(&attr, ad_entry(&ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF, sizeof(attr));
2179 attr |= htons(FINDERINFO_INVISIBLE);
2180 memcpy(ad_entry(&ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF,&attr, sizeof(attr));
2183 ad_flush( &ad, ADFLAGS_HF );
2184 ad_close( &ad, ADFLAGS_HF );
2191 static void handle_special_folders (const struct vol * vol)
2193 const _special_folder *p = &special_folders[0];
2195 if ((vol->v_flags & AFPVOL_RO))
2198 for (; p->name != NULL; p++) {
2199 create_special_folder (vol, p);
2204 * Save the volume options to a file, used by
2206 * Writing the file everytime a volume is opened is
2207 * unnecessary, but it shouldn't hurt much.
2209 static int savevoloptions (const struct vol *vol)
2212 char item[MAXPATHLEN];
2216 const _vol_opt_name *op = &vol_opt_names[0];
2217 const _vol_opt_name *cf = &vol_opt_casefold[0];
2219 strlcpy (item, vol->v_path, sizeof(item));
2220 strlcat (item, "/.AppleDesktop/", sizeof(item));
2221 strlcat (item, VOLINFOFILE, sizeof(item));
2223 if ((fd = open( item, O_RDWR | O_CREAT , 0666)) <0 ) {
2224 LOG(log_debug, logtype_afpd,"Error opening %s: %s", item, strerror(errno));
2228 /* try to get a lock */
2230 lock.l_whence = SEEK_SET;
2232 lock.l_type = F_WRLCK;
2234 if (fcntl(fd, F_SETLK, &lock) < 0) {
2235 if (errno == EACCES || errno == EAGAIN) {
2236 /* ignore, other process already writing the file */
2239 LOG(log_error, logtype_cnid, "savevoloptions: cannot get lock: %s", strerror(errno));
2244 /* write volume options */
2245 snprintf(buf, sizeof(buf), "MAC_CHARSET:%s\n", vol->v_maccodepage);
2246 snprintf(item, sizeof(item), "VOL_CHARSET:%s\n", vol->v_volcodepage);
2247 strlcat(buf, item, sizeof(buf));
2249 switch (vol->v_adouble) {
2251 strlcat(buf, "ADOUBLE_VER:v1\n", sizeof(buf));
2254 strlcat(buf, "ADOUBLE_VER:v2\n", sizeof(buf));
2256 case AD_VERSION2_OSX:
2257 strlcat(buf, "ADOUBLE_VER:osx\n", sizeof(buf));
2259 case AD_VERSION1_ADS:
2260 strlcat(buf, "ADOUBLE_VER:ads\n", sizeof(buf));
2264 strlcat(buf, "CNIDBACKEND:", sizeof(buf));
2265 strlcat(buf, vol->v_cnidscheme, sizeof(buf));
2266 strlcat(buf, "\n", sizeof(buf));
2268 strlcat(buf, "CNIDDBDHOST:", sizeof(buf));
2269 strlcat(buf, Cnid_srv, sizeof(buf));
2270 strlcat(buf, "\n", sizeof(buf));
2272 snprintf(item, sizeof(item), "CNIDDBDPORT:%u\n", Cnid_port);
2273 strlcat(buf, item, sizeof(buf));
2275 strcpy(item, "CNID_DBPATH:");
2277 strlcat(item, vol->v_dbpath, sizeof(item));
2279 strlcat(item, vol->v_path, sizeof(item));
2280 strlcat(item, "\n", sizeof(item));
2281 strlcat(buf, item, sizeof(buf));
2284 strcpy(item, "VOLUME_OPTS:");
2285 for (;op->name; op++) {
2286 if ( (vol->v_flags & op->option) ) {
2287 strlcat(item, op->name, sizeof(item));
2288 strlcat(item, " ", sizeof(item));
2291 strlcat(item, "\n", sizeof(item));
2292 strlcat(buf, item, sizeof(buf));
2294 /* casefold flags */
2295 strcpy(item, "VOLCASEFOLD:");
2296 for (;cf->name; cf++) {
2297 if ( (vol->v_casefold & cf->option) ) {
2298 strlcat(item, cf->name, sizeof(item));
2299 strlcat(item, " ", sizeof(item));
2302 strlcat(item, "\n", sizeof(item));
2303 strlcat(buf, item, sizeof(buf));
2305 if (strlen(buf) >= sizeof(buf)-1)
2306 LOG(log_debug, logtype_afpd,"Error writing .volinfo file: buffer too small, %s", buf);
2309 if (write( fd, buf, strlen(buf)) < 0) {
2310 LOG(log_debug, logtype_afpd,"Error writing .volinfo file: %s", strerror(errno));
2313 ftruncate(fd, strlen(buf));
2316 lock.l_type = F_UNLCK;
2317 fcntl(fd, F_SETLK, &lock);