2 * $Id: volume.c,v 1.60 2005-05-14 12:54:53 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;
622 if (options[VOLOPT_PASSWORD].c_value)
623 volume->v_password = strdup(options[VOLOPT_PASSWORD].c_value);
625 if (options[VOLOPT_VETO].c_value)
626 volume->v_veto = strdup(options[VOLOPT_VETO].c_value);
628 if (options[VOLOPT_ENCODING].c_value)
629 volume->v_volcodepage = strdup(options[VOLOPT_ENCODING].c_value);
631 if (options[VOLOPT_MACCHARSET].c_value)
632 volume->v_maccodepage = strdup(options[VOLOPT_MACCHARSET].c_value);
634 if (options[VOLOPT_DBPATH].c_value)
635 volume->v_dbpath = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_DBPATH].c_value, pwd, path, name);
637 if (options[VOLOPT_CNIDSCHEME].c_value)
638 volume->v_cnidscheme = strdup(options[VOLOPT_CNIDSCHEME].c_value);
640 if (options[VOLOPT_UMASK].i_value)
641 volume->v_umask = (mode_t)options[VOLOPT_UMASK].i_value;
643 if (options[VOLOPT_ADOUBLE].i_value)
644 volume->v_adouble = options[VOLOPT_ADOUBLE].i_value;
646 volume->v_adouble = AD_VERSION;
650 if (options[VOLOPT_FORCEUID].c_value) {
651 volume->v_forceuid = strdup(options[VOLOPT_FORCEUID].c_value);
653 volume->v_forceuid = NULL; /* set as null so as to return 0 later on */
656 if (options[VOLOPT_FORCEGID].c_value) {
657 volume->v_forcegid = strdup(options[VOLOPT_FORCEGID].c_value);
659 volume->v_forcegid = NULL; /* set as null so as to return 0 later on */
663 if (options[VOLOPT_PREEXEC].c_value)
664 volume->v_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_PREEXEC].c_value, pwd, path, name);
665 volume->v_preexec_close = options[VOLOPT_PREEXEC].i_value;
667 if (options[VOLOPT_POSTEXEC].c_value)
668 volume->v_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_POSTEXEC].c_value, pwd, path, name);
670 if (options[VOLOPT_ROOTPREEXEC].c_value)
671 volume->v_root_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPREEXEC].c_value, pwd, path, name);
672 volume->v_root_preexec_close = options[VOLOPT_ROOTPREEXEC].i_value;
674 if (options[VOLOPT_ROOTPOSTEXEC].c_value)
675 volume->v_root_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPOSTEXEC].c_value, pwd, path, name);
679 volume->v_next = Volumes;
684 /* ---------------- */
685 static char *myfgets( buf, size, fp )
694 while ((EOF != ( c = getc( fp )) ) && ( size > 0 )) {
695 if ( c == '\n' || c == '\r' ) {
713 /* check access list. this function wants something of the following
715 * @group,name,name2,@group2,name3
717 * a NULL argument allows everybody to have access.
718 * we return three things:
720 * 0: list exists, but name isn't in it
724 #ifndef NO_REAL_USER_NAME
725 /* authentication is case insensitive
726 * FIXME should we do the same with group name?
728 #define access_strcmp strcasecmp
731 #define access_strcmp strcmp
735 static int accessvol(args, name)
739 char buf[MAXPATHLEN + 1], *p;
745 strlcpy(buf, args, sizeof(buf));
746 if ((p = strtok(buf, ",")) == NULL) /* nothing, return okay */
750 if (*p == '@') { /* it's a group */
751 if ((gr = getgrnam(p + 1)) && gmem(gr->gr_gid))
753 } else if (access_strcmp(p, name) == 0) /* it's a user name */
755 p = strtok(NULL, ",");
761 static void setextmap( ext, type, creator, user)
762 char *ext, *type, *creator;
768 if (Extmap == NULL) {
769 if (( Extmap = calloc(1, sizeof( struct extmap ))) == NULL ) {
770 LOG(log_error, logtype_afpd, "setextmap: calloc: %s", strerror(errno) );
775 for ( em = Extmap, cnt = 0; em->em_ext; em++, cnt++) {
776 if ( (strdiacasecmp( em->em_ext, ext )) == 0 ) {
781 if ( em->em_ext == NULL ) {
782 if (!(Extmap = realloc( Extmap, sizeof( struct extmap ) * (cnt +2))) ) {
783 LOG(log_error, logtype_afpd, "setextmap: realloc: %s", strerror(errno) );
786 (Extmap +cnt +1)->em_ext = NULL;
788 } else if ( !user ) {
794 if (!(em->em_ext = strdup( ext))) {
795 LOG(log_error, logtype_afpd, "setextmap: strdup: %s", strerror(errno) );
799 if ( *type == '\0' ) {
800 memcpy(em->em_type, "????", sizeof( em->em_type ));
802 memcpy(em->em_type, type, sizeof( em->em_type ));
804 if ( *creator == '\0' ) {
805 memcpy(em->em_creator, "UNIX", sizeof( em->em_creator ));
807 memcpy(em->em_creator, creator, sizeof( em->em_creator ));
811 /* -------------------------- */
812 static int extmap_cmp(const void *map1, const void *map2)
814 const struct extmap *em1 = map1;
815 const struct extmap *em2 = map2;
816 return strdiacasecmp(em1->em_ext, em2->em_ext);
819 static void sortextmap( void)
824 if ((em = Extmap) == NULL) {
832 qsort(Extmap, Extmap_cnt, sizeof(struct extmap), extmap_cmp);
833 if (*Extmap->em_ext == 0) {
834 /* the first line is really "." the default entry,
835 * we remove the leading '.' in setextmap
842 /* ----------------------
844 static void free_extmap( void)
849 for ( em = Extmap; em->em_ext; em++) {
859 /* ----------------------
861 static int volfile_changed(struct afp_volume_name *p)
871 if (!stat( name, &st) && st.st_mtime > p->mtime) {
872 p->mtime = st.st_mtime;
878 /* ----------------------
879 * Read a volume configuration file and add the volumes contained within to
880 * the global volume list. If p2 is non-NULL, the file that is opened is
883 * Lines that begin with # and blank lines are ignored.
884 * Volume lines are of the form:
885 * <unix path> [<volume name>] [allow:<user>,<@group>,...] \
886 * [codepage:<file>] [casefold:<num>]
887 * <extension> TYPE [CREATOR]
889 static int readvolfile(obj, p1, p2, user, pwent)
891 struct afp_volume_name *p1;
894 struct passwd *pwent;
897 char path[ MAXPATHLEN + 1], tmp[ MAXPATHLEN + 1],
898 volname[ AFPVOL_NAMELEN + 1 ], buf[ BUFSIZ ],
899 type[ 5 ], creator[ 5 ];
902 struct vol_option options[VOLOPT_NUM], save_options[VOLOPT_NUM];
910 strcpy( path, p1->name );
917 p1->full_name = strdup(path);
920 if (NULL == ( fp = fopen( path, "r" )) ) {
924 if (fd != -1 && !fstat( fd, &st) ) {
925 p1->mtime = st.st_mtime;
928 memset(save_options, 0, sizeof(save_options));
929 while ( myfgets( buf, sizeof( buf ), fp ) != NULL ) {
930 initline( strlen( buf ), buf );
931 parseline( sizeof( path ) - 1, path );
938 /* change the default options for this file */
939 if (strncmp(path, VOLOPT_DEFAULT, VOLOPT_DEFAULT_LEN) == 0) {
941 for (i = 0; i < VOLOPT_NUM; i++) {
942 if (parseline( sizeof( path ) - VOLOPT_DEFAULT_LEN - 1,
943 path + VOLOPT_DEFAULT_LEN) < 0)
945 volset(save_options, NULL, tmp, sizeof(tmp) - 1,
946 path + VOLOPT_DEFAULT_LEN);
952 if (( p = strchr( path, '/' )) != NULL ) {
960 if ( u == NULL || *u == '\0' || ( pw = getpwnam( u )) == NULL ) {
963 strcpy( tmp, pw->pw_dir );
964 if ( p != NULL && *p != '\0' ) {
968 /* Tag a user's home directory with their umask. Note, this will
969 * be overwritten if the user actually specifies a umask: option
970 * for a '~' volume. */
971 save_options[VOLOPT_UMASK].i_value = obj->options.save_mask;
975 /* send path through variable substitution */
976 if (*path != '~') /* need to copy path to tmp */
979 pwent = getpwnam(obj->username);
980 volxlate(obj, path, sizeof(path) - 1, tmp, pwent, NULL, NULL);
982 /* this is sort of braindead. basically, i want to be
983 * able to specify things in any order, but i don't want to
984 * re-write everything.
986 * currently we have options:
994 * options:prodos,crlf,noadouble,ro...
999 * namemask:x,y,!z (not implemented yet)
1001 memcpy(options, save_options, sizeof(options));
1004 /* read in up to VOLOP_NUM possible options */
1005 for (i = 0; i < VOLOPT_NUM; i++) {
1006 if (parseline( sizeof( tmp ) - 1, tmp ) < 0)
1009 volset(options, save_options, volname, sizeof(volname) - 1, tmp);
1012 /* check allow/deny lists:
1013 allow -> either no list (-1), or in list (1)
1014 deny -> either no list (-1), or not in list (0) */
1015 if (accessvol(options[VOLOPT_ALLOW].c_value, obj->username) &&
1016 (accessvol(options[VOLOPT_DENY].c_value, obj->username) < 1)) {
1018 /* handle read-only behaviour. semantics:
1019 * 1) neither the rolist nor the rwlist exist -> rw
1020 * 2) rolist exists -> ro if user is in it.
1021 * 3) rwlist exists -> ro unless user is in it. */
1022 if (((options[VOLOPT_FLAGS].i_value & AFPVOL_RO) == 0) &&
1023 ((accessvol(options[VOLOPT_ROLIST].c_value,
1024 obj->username) == 1) ||
1025 !accessvol(options[VOLOPT_RWLIST].c_value,
1027 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
1029 /* do variable substitution for volname */
1030 volxlate(obj, tmp, sizeof(tmp) - 1, volname, pwent, path, NULL);
1031 creatvol(obj, pwent, path, tmp, options, p2 != NULL);
1033 volfree(options, save_options);
1037 parseline( sizeof( type ) - 1, type );
1038 parseline( sizeof( creator ) - 1, creator );
1039 setextmap( path, type, creator, user);
1046 volfree(save_options, NULL);
1048 if ( fclose( fp ) != 0 ) {
1049 LOG(log_error, logtype_afpd, "readvolfile: fclose: %s", strerror(errno) );
1055 /* ------------------------------- */
1056 static void volume_free(struct vol *vol)
1061 free(vol->v_password);
1063 free(vol->v_volcodepage);
1064 free(vol->v_maccodepage);
1065 free(vol->v_cnidscheme);
1066 free(vol->v_dbpath);
1069 free(vol->v_forceuid);
1070 free(vol->v_forcegid);
1071 #endif /* FORCE_UIDGID */
1074 /* ------------------------------- */
1075 static void free_volumes(void )
1078 struct vol *nvol, *ovol;
1080 for ( vol = Volumes; vol; vol = vol->v_next ) {
1081 if (( vol->v_flags & AFPVOL_OPEN ) ) {
1088 for ( vol = Volumes, ovol = NULL; vol; vol = nvol) {
1091 if (vol->v_name == NULL) {
1092 if (Volumes == vol) {
1097 ovol->v_next = nvol;
1107 /* ------------------------------- */
1108 static void volume_unlink(struct vol *volume)
1110 struct vol *vol, *ovol, *nvol;
1112 if (volume == Volumes) {
1113 Volumes = Volumes->v_next;
1116 for ( vol = Volumes->v_next, ovol = Volumes; vol; vol = nvol) {
1119 if (vol == volume) {
1120 ovol->v_next = nvol;
1129 static int getvolspace( vol, bfree, btotal, xbfree, xbtotal, bsize )
1131 u_int32_t *bfree, *btotal, *bsize;
1132 VolSpace *xbfree, *xbtotal;
1136 #ifndef NO_QUOTA_SUPPORT
1137 VolSpace qfree, qtotal;
1140 spaceflag = AFPVOL_GVSMASK & vol->v_flags;
1141 /* report up to 2GB if afp version is < 2.2 (4GB if not) */
1142 maxsize = (vol->v_flags & AFPVOL_A2VOL) ? 0x01fffe00 :
1143 (((afp_version < 22) || (vol->v_flags & AFPVOL_LIMITSIZE))
1144 ? 0x7fffffffL : 0xffffffffL);
1147 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_AFSGVS ) {
1148 if ( afs_getvolspace( vol, xbfree, xbtotal, bsize ) == AFP_OK ) {
1149 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_AFSGVS;
1150 goto getvolspace_done;
1155 if (( rc = ustatfs_getvolspace( vol, xbfree, xbtotal,
1156 bsize)) != AFP_OK ) {
1160 #define min(a,b) ((a)<(b)?(a):(b))
1161 #ifndef NO_QUOTA_SUPPORT
1162 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_UQUOTA ) {
1163 if ( uquota_getvolspace( vol, &qfree, &qtotal, *bsize ) == AFP_OK ) {
1164 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_UQUOTA;
1165 *xbfree = min(*xbfree, qfree);
1166 *xbtotal = min( *xbtotal, qtotal);
1167 goto getvolspace_done;
1171 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_USTATFS;
1174 *bfree = min( *xbfree, maxsize);
1175 *btotal = min( *xbtotal, maxsize);
1179 /* -----------------------
1180 * set volume creation date
1181 * avoid duplicate, well at least it tries
1183 static void vol_setdate(u_int16_t id, struct adouble *adp, time_t date)
1186 struct vol *vol = Volumes;
1188 for ( volume = Volumes; volume; volume = volume->v_next ) {
1189 if (volume->v_vid == id) {
1192 else if ((time_t)(AD_DATE_FROM_UNIX(date)) == volume->v_ctime) {
1194 volume = Volumes; /* restart */
1197 vol->v_ctime = AD_DATE_FROM_UNIX(date);
1198 ad_setdate(adp, AD_DATE_CREATE | AD_DATE_UNIX, date);
1201 /* ----------------------- */
1202 static int getvolparams( bitmap, vol, st, buf, buflen )
1210 int bit = 0, isad = 1;
1213 u_int32_t bfree, btotal, bsize;
1214 VolSpace xbfree, xbtotal; /* extended bytes */
1215 char *data, *nameoff = NULL;
1218 /* courtesy of jallison@whistle.com:
1219 * For MacOS8.x support we need to create the
1220 * .Parent file here if it doesn't exist. */
1222 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1223 if ( ad_open_metadata( vol->v_path, vol_noadouble(vol) | ADFLAGS_DIR, O_CREAT, &ad) < 0 ) {
1225 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1227 } else if (ad_get_HF_flags( &ad ) & O_CREAT) {
1228 slash = strrchr( vol->v_path, '/' );
1232 slash = vol->v_path;
1233 if (ad_getentryoff(&ad, ADEID_NAME)) {
1234 ad_setentrylen( &ad, ADEID_NAME, strlen( slash ));
1235 memcpy(ad_entry( &ad, ADEID_NAME ), slash,
1236 ad_getentrylen( &ad, ADEID_NAME ));
1238 vol_setdate(vol->v_vid, &ad, st->st_mtime);
1239 ad_flush_metadata(&ad);
1242 if (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0)
1243 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1245 vol->v_ctime = aint;
1248 if (( bitmap & ( (1<<VOLPBIT_BFREE)|(1<<VOLPBIT_BTOTAL) |
1249 (1<<VOLPBIT_XBFREE)|(1<<VOLPBIT_XBTOTAL) |
1250 (1<<VOLPBIT_BSIZE)) ) != 0 ) {
1251 if ( getvolspace( vol, &bfree, &btotal, &xbfree, &xbtotal,
1254 ad_close( &ad, ADFLAGS_HF );
1256 return( AFPERR_PARAM );
1261 while ( bitmap != 0 ) {
1262 while (( bitmap & 1 ) == 0 ) {
1270 if (0 == (vol->v_flags & AFPVOL_NOFILEID) && vol->v_cdb != NULL &&
1271 (vol->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1272 ashort = VOLPBIT_ATTR_FILEID;
1274 /* check for read-only.
1275 * NOTE: we don't actually set the read-only flag unless
1276 * it's passed in that way as it's possible to mount
1277 * a read-write filesystem under a read-only one. */
1278 if ((vol->v_flags & AFPVOL_RO) ||
1279 ((utime(vol->v_path, NULL) < 0) && (errno == EROFS))) {
1280 ashort |= VOLPBIT_ATTR_RO;
1282 ashort |= VOLPBIT_ATTR_CATSEARCH;
1283 if (afp_version >= 30) {
1284 ashort |= VOLPBIT_ATTR_UTF8;
1285 if (vol->v_flags & AFPVOL_UNIX_PRIV)
1286 ashort |= VOLPBIT_ATTR_UNIXPRIV;
1288 ashort = htons(ashort);
1289 memcpy(data, &ashort, sizeof( ashort ));
1290 data += sizeof( ashort );
1294 ashort = htons( AFPVOLSIG_DEFAULT );
1295 memcpy(data, &ashort, sizeof( ashort ));
1296 data += sizeof( ashort );
1299 case VOLPBIT_CDATE :
1300 aint = vol->v_ctime;
1301 memcpy(data, &aint, sizeof( aint ));
1302 data += sizeof( aint );
1305 case VOLPBIT_MDATE :
1306 if ( st->st_mtime > vol->v_mtime ) {
1307 vol->v_mtime = st->st_mtime;
1309 aint = AD_DATE_FROM_UNIX(vol->v_mtime);
1310 memcpy(data, &aint, sizeof( aint ));
1311 data += sizeof( aint );
1314 case VOLPBIT_BDATE :
1315 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1316 aint = AD_DATE_START;
1317 memcpy(data, &aint, sizeof( aint ));
1318 data += sizeof( aint );
1322 memcpy(data, &vol->v_vid, sizeof( vol->v_vid ));
1323 data += sizeof( vol->v_vid );
1326 case VOLPBIT_BFREE :
1327 bfree = htonl( bfree );
1328 memcpy(data, &bfree, sizeof( bfree ));
1329 data += sizeof( bfree );
1332 case VOLPBIT_BTOTAL :
1333 btotal = htonl( btotal );
1334 memcpy(data, &btotal, sizeof( btotal ));
1335 data += sizeof( btotal );
1338 #ifndef NO_LARGE_VOL_SUPPORT
1339 case VOLPBIT_XBFREE :
1340 xbfree = hton64( xbfree );
1341 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1342 bcopy(&xbfree, data, sizeof(xbfree));
1343 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1344 memcpy(data, &xbfree, sizeof( xbfree ));
1345 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1346 data += sizeof( xbfree );
1349 case VOLPBIT_XBTOTAL :
1350 xbtotal = hton64( xbtotal );
1351 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1352 bcopy(&xbtotal, data, sizeof(xbtotal));
1353 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1354 memcpy(data, &xbtotal, sizeof( xbtotal ));
1355 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1356 data += sizeof( xbfree );
1358 #endif /* ! NO_LARGE_VOL_SUPPORT */
1362 data += sizeof( u_int16_t );
1365 case VOLPBIT_BSIZE: /* block size */
1366 bsize = htonl(bsize);
1367 memcpy(data, &bsize, sizeof(bsize));
1368 data += sizeof(bsize);
1373 ad_close( &ad, ADFLAGS_HF );
1375 return( AFPERR_BITMAP );
1381 ashort = htons( data - buf );
1382 memcpy(nameoff, &ashort, sizeof( ashort ));
1383 aint = ucs2_to_charset( (utf8_encoding()?CH_UTF8_MAC:vol->v_maccharset), vol->v_name, data+1, 255);
1393 ad_close_metadata( &ad);
1395 *buflen = data - buf;
1399 /* ------------------------- */
1400 static int stat_vol(u_int16_t bitmap, struct vol *vol, char *rbuf, int *rbuflen)
1405 if ( stat( vol->v_path, &st ) < 0 ) {
1407 return( AFPERR_PARAM );
1409 /* save the volume device number */
1410 vol->v_dev = st.st_dev;
1412 buflen = *rbuflen - sizeof( bitmap );
1413 if (( ret = getvolparams( bitmap, vol, &st,
1414 rbuf + sizeof( bitmap ), &buflen )) != AFP_OK ) {
1418 *rbuflen = buflen + sizeof( bitmap );
1419 bitmap = htons( bitmap );
1420 memcpy(rbuf, &bitmap, sizeof( bitmap ));
1425 /* ------------------------------- */
1426 void load_volumes(AFPObj *obj)
1428 struct passwd *pwent;
1433 /* check files date */
1434 if (obj->options.defaultvol.loaded) {
1435 changed = volfile_changed(&obj->options.defaultvol);
1437 if (obj->options.systemvol.loaded) {
1438 changed |= volfile_changed(&obj->options.systemvol);
1440 if (obj->options.uservol.loaded) {
1441 changed |= volfile_changed(&obj->options.uservol);
1450 pwent = getpwnam(obj->username);
1451 if ( (obj->options.flags & OPTION_USERVOLFIRST) == 0 ) {
1452 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent);
1455 if ((*obj->username == '\0') || (obj->options.flags & OPTION_NOUSERVOL)) {
1456 readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent);
1459 * Read user's AppleVolumes or .AppleVolumes file
1460 * If neither are readable, read the default volumes file. if
1461 * that doesn't work, create a user share.
1463 if (obj->options.uservol.name) {
1464 free(obj->options.uservol.name);
1466 obj->options.uservol.name = strdup(pwent->pw_dir);
1467 if ( readvolfile(obj, &obj->options.uservol, "AppleVolumes", 1, pwent) < 0 &&
1468 readvolfile(obj, &obj->options.uservol, ".AppleVolumes", 1, pwent) < 0 &&
1469 readvolfile(obj, &obj->options.uservol, "applevolumes", 1, pwent) < 0 &&
1470 readvolfile(obj, &obj->options.uservol, ".applevolumes", 1, pwent) < 0 &&
1471 obj->options.defaultvol.name != NULL ) {
1472 if (readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent) < 0)
1473 creatvol(obj, pwent, pwent->pw_dir, NULL, NULL, 1);
1476 if ( obj->options.flags & OPTION_USERVOLFIRST ) {
1477 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent );
1480 if ( obj->options.closevol ) {
1483 for ( vol = Volumes; vol; vol = vol->v_next ) {
1484 if (vol->v_deleted && !vol->v_new ) {
1493 /* ------------------------------- */
1494 int afp_getsrvrparms(obj, ibuf, ibuflen, rbuf, rbuflen )
1496 char *ibuf _U_, *rbuf;
1497 int ibuflen _U_, *rbuflen;
1510 for ( vcnt = 0, volume = Volumes; volume; volume = volume->v_next ) {
1511 if (!(volume->v_flags & AFPVOL_NOSTAT)) {
1512 if ( stat( volume->v_path, &st ) < 0 ) {
1513 LOG(log_info, logtype_afpd, "afp_getsrvrparms(%s): stat: %s",
1514 volume->v_path, strerror(errno) );
1515 continue; /* can't access directory */
1517 if (!S_ISDIR(st.st_mode)) {
1518 continue; /* not a dir */
1521 if (volume->v_hide) {
1522 continue; /* config file changed but the volume was mounted */
1524 len = ucs2_to_charset_allocate((utf8_encoding()?CH_UTF8_MAC:obj->options.maccharset),
1525 &namebuf, volume->v_name);
1526 if (len == (size_t)-1)
1529 /* set password bit if there's a volume password */
1530 *data = (volume->v_password) ? AFPSRVR_PASSWD : 0;
1532 /* Apple 2 clients running ProDOS-8 expect one volume to have
1533 bit 0 of this byte set. They will not recognize anything
1534 on the server unless this is the case. I have not
1535 completely worked this out, but it's related to booting
1536 from the server. Support for that function is a ways
1537 off.. <shirsch@ibm.net> */
1538 *data |= (volume->v_flags & AFPVOL_A2VOL) ? AFPSRVR_CONFIGINFO : 0;
1539 *data++ |= 0; /* UNIX PRIVS BIT ..., OSX doesn't seem to use it, so we don't either */
1541 memcpy(data, namebuf, len );
1547 *rbuflen = data - rbuf;
1549 if ( gettimeofday( &tv, 0 ) < 0 ) {
1550 LOG(log_error, logtype_afpd, "afp_getsrvrparms(%s): gettimeofday: %s", volume->v_path, strerror(errno) );
1552 return AFPERR_PARAM;
1554 tv.tv_sec = AD_DATE_FROM_UNIX(tv.tv_sec);
1555 memcpy(data, &tv.tv_sec, sizeof( u_int32_t));
1556 data += sizeof( u_int32_t);
1561 /* -------------------------
1562 * we are the user here
1564 int afp_openvol(obj, ibuf, ibuflen, rbuf, rbuflen )
1567 int ibuflen _U_, *rbuflen;
1577 char path[ MAXPATHLEN + 1];
1582 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1583 bitmap = ntohs( bitmap );
1584 ibuf += sizeof( bitmap );
1585 if (( bitmap & (1<<VOLPBIT_VID)) == 0 ) {
1587 return AFPERR_BITMAP;
1590 len = (unsigned char)*ibuf++;
1591 volname = obj->oldtmp;
1592 namelen = convert_string( (utf8_encoding()?CH_UTF8_MAC:obj->options.maccharset), CH_UCS2,
1593 ibuf, len, volname, sizeof(obj->oldtmp));
1596 return AFPERR_PARAM;
1600 if ((len + 1) & 1) /* pad to an even boundary */
1605 for ( volume = Volumes; volume; volume = volume->v_next ) {
1606 if ( strcasecmp_w( (ucs2_t*) volname, volume->v_name ) == 0 ) {
1611 if ( volume == NULL ) {
1613 return AFPERR_PARAM;
1616 /* check for a volume password */
1617 if (volume->v_password && strncmp(ibuf, volume->v_password, VOLPASSLEN)) {
1619 return AFPERR_ACCESS;
1622 if (( volume->v_flags & AFPVOL_OPEN ) ) {
1623 /* the volume is already open */
1625 set_uidgid ( volume );
1627 return stat_vol(bitmap, volume, rbuf, rbuflen);
1630 /* initialize volume variables
1633 if (afp_version >= 30) {
1634 volume->max_filename = 255;
1637 volume->max_filename = MACFILELEN;
1640 volume->v_dir = volume->v_root = NULL;
1641 volume->v_hash = NULL;
1643 volume->v_flags |= AFPVOL_OPEN;
1644 volume->v_cdb = NULL;
1646 if (volume->v_root_preexec) {
1647 if ((ret = afprun(1, volume->v_root_preexec, NULL)) && volume->v_root_preexec_close) {
1648 LOG(log_error, logtype_afpd, "afp_openvol(%s): root preexec : %d", volume->v_path, ret );
1655 set_uidgid ( volume );
1658 if (volume->v_preexec) {
1659 if ((ret = afprun(0, volume->v_preexec, NULL)) && volume->v_preexec_close) {
1660 LOG(log_error, logtype_afpd, "afp_openvol(%s): preexec : %d", volume->v_path, ret );
1666 if ( stat( volume->v_path, &st ) < 0 ) {
1671 if ( chdir( volume->v_path ) < 0 ) {
1676 len = convert_string_allocate( CH_UCS2, (utf8_encoding()?CH_UTF8_MAC:obj->options.maccharset),
1677 volume->v_name, namelen, &vol_mname);
1678 if ( !vol_mname || len <= 0) {
1683 if ( NULL == getcwd(path, MAXPATHLEN)) {
1684 /* shouldn't be fatal but it will fail later */
1685 LOG(log_error, logtype_afpd, "afp_openvol(%s): volume pathlen too long", volume->v_path);
1690 if ((vol_uname = strrchr(path, '/')) == NULL)
1692 else if (*(vol_uname + 1) != '\0')
1695 if ((dir = dirnew(vol_mname, vol_uname) ) == NULL) {
1697 LOG(log_error, logtype_afpd, "afp_openvol(%s): malloc: %s", volume->v_path, strerror(errno) );
1703 dir->d_did = DIRDID_ROOT;
1704 dir->d_color = DIRTREE_COLOR_BLACK; /* root node is black */
1705 dir->d_m_name_ucs2 = strdup_w(volume->v_name);
1706 volume->v_dir = volume->v_root = dir;
1707 volume->v_hash = dirhash();
1709 curdir = volume->v_dir;
1710 if (volume->v_cnidscheme == NULL) {
1711 volume->v_cnidscheme = strdup(DEFAULT_CNID_SCHEME);
1712 LOG(log_warning, logtype_afpd, "Warning: No CNID scheme for volume %s. Using default.",
1715 if (volume->v_dbpath)
1716 volume->v_cdb = cnid_open (volume->v_dbpath, volume->v_umask, volume->v_cnidscheme, (volume->v_flags & AFPVOL_NODEV));
1718 volume->v_cdb = cnid_open (volume->v_path, volume->v_umask, volume->v_cnidscheme, (volume->v_flags & AFPVOL_NODEV));
1719 if (volume->v_cdb == NULL) {
1720 LOG(log_error, logtype_afpd, "Fatal error: cannot open CNID or invalid CNID backend for %s: %s",
1721 volume->v_path, volume->v_cnidscheme);
1728 if (!volume->v_volcodepage)
1729 volume->v_volcodepage = strdup("UTF8");
1731 if ( (charset_t) -1 == ( volume->v_volcharset = add_charset(volume->v_volcodepage)) ) {
1732 LOG (log_error, logtype_afpd, "Setting codepage %s as volume codepage failed", volume->v_volcodepage);
1737 if ( NULL == ( volume->v_vol = find_charset_functions(volume->v_volcodepage)) || volume->v_vol->flags & CHARSET_ICONV ) {
1738 LOG (log_warning, logtype_afpd, "WARNING: volume encoding %s is *not* supported by netatalk, expect problems !!!!", volume->v_volcodepage);
1741 if (!volume->v_maccodepage)
1742 volume->v_maccodepage = strdup(obj->options.maccodepage);
1744 if ( (charset_t) -1 == ( volume->v_maccharset = add_charset(volume->v_maccodepage)) ) {
1745 LOG (log_error, logtype_afpd, "Setting codepage %s as mac codepage failed", volume->v_maccodepage);
1750 if ( NULL == ( volume->v_mac = find_charset_functions(volume->v_maccodepage)) || ! (volume->v_mac->flags & CHARSET_CLIENT) ) {
1751 LOG (log_error, logtype_afpd, "Fatal error: mac charset %s not supported", volume->v_maccodepage);
1756 ret = stat_vol(bitmap, volume, rbuf, rbuflen);
1757 if (ret == AFP_OK) {
1759 if (!(volume->v_flags & AFPVOL_RO)) {
1760 handle_special_folders( volume );
1761 savevoloptions( volume);
1765 * If you mount a volume twice, the second time the trash appears on
1766 * the desk-top. That's because the Mac remembers the DID for the
1767 * trash (even for volumes in different zones, on different servers).
1768 * Just so this works better, we prime the DID cache with the trash,
1769 * fixing the trash at DID 17.
1770 * FIXME (RL): should it be done inside a CNID backend ? (always returning Trash DID when asked) ?
1772 if ((volume->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1774 /* FIXME find db time stamp */
1775 if (cnid_getstamp(volume->v_cdb, volume->v_stamp, sizeof(volume->v_stamp)) < 0) {
1776 LOG (log_error, logtype_afpd,
1777 "afp_openvol(%s): Fatal error: Unable to get stamp value from CNID backend",
1785 cname( volume, volume->v_dir, &p );
1791 if (volume->v_dir) {
1792 hash_free( volume->v_hash);
1793 dirfree( volume->v_dir );
1794 volume->v_dir = volume->v_root = NULL;
1797 volume->v_flags &= ~AFPVOL_OPEN;
1798 if (volume->v_cdb != NULL) {
1799 cnid_close(volume->v_cdb);
1800 volume->v_cdb = NULL;
1806 /* ------------------------- */
1807 static void closevol(struct vol *vol)
1812 hash_free( vol->v_hash);
1813 dirfree( vol->v_root );
1815 if (vol->v_cdb != NULL) {
1816 cnid_close(vol->v_cdb);
1820 if (vol->v_postexec) {
1821 afprun(0, vol->v_postexec, NULL);
1823 if (vol->v_root_postexec) {
1824 afprun(1, vol->v_root_postexec, NULL);
1828 /* ------------------------- */
1829 void close_all_vol(void)
1833 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
1834 if ( (ovol->v_flags & AFPVOL_OPEN) ) {
1835 ovol->v_flags &= ~AFPVOL_OPEN;
1841 /* ------------------------- */
1842 static void deletevol(struct vol *vol)
1846 vol->v_flags &= ~AFPVOL_OPEN;
1847 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
1848 if ( (ovol->v_flags & AFPVOL_OPEN) ) {
1852 if ( ovol != NULL ) {
1853 /* Even if chdir fails, we can't say afp_closevol fails. */
1854 if ( chdir( ovol->v_path ) == 0 ) {
1855 curdir = ovol->v_dir;
1860 if (vol->v_deleted) {
1861 showvol(vol->v_name);
1868 /* ------------------------- */
1869 int afp_closevol(obj, ibuf, ibuflen, rbuf, rbuflen )
1871 char *ibuf, *rbuf _U_;
1872 int ibuflen _U_, *rbuflen;
1879 memcpy(&vid, ibuf, sizeof( vid ));
1880 if (NULL == ( vol = getvolbyvid( vid )) ) {
1881 return( AFPERR_PARAM );
1889 /* ------------------------- */
1890 struct vol *getvolbyvid(const u_int16_t vid )
1894 for ( vol = Volumes; vol; vol = vol->v_next ) {
1895 if ( vid == vol->v_vid ) {
1899 if ( vol == NULL || ( vol->v_flags & AFPVOL_OPEN ) == 0 ) {
1905 #endif /* FORCE_UIDGID */
1910 /* ------------------------ */
1911 static int ext_cmp_key(const void *key, const void *obj)
1913 const char *p = key;
1914 const struct extmap *em = obj;
1915 return strdiacasecmp(p, em->em_ext);
1917 struct extmap *getextmap(const char *path)
1922 if (NULL == ( p = strrchr( path, '.' )) ) {
1923 return( Defextmap );
1926 if (!*p || !Extmap_cnt) {
1927 return( Defextmap );
1929 em = bsearch(p, Extmap, Extmap_cnt, sizeof(struct extmap), ext_cmp_key);
1933 return( Defextmap );
1937 /* ------------------------- */
1938 struct extmap *getdefextmap(void)
1940 return( Defextmap );
1943 /* --------------------------
1944 poll if a volume is changed by other processes.
1946 int pollvoltime(obj)
1953 if (!(afp_version > 21 && obj->options.server_notif))
1956 if ( gettimeofday( &tv, 0 ) < 0 )
1959 for ( vol = Volumes; vol; vol = vol->v_next ) {
1960 if ( (vol->v_flags & AFPVOL_OPEN) && vol->v_mtime + 30 < tv.tv_sec) {
1961 if ( !stat( vol->v_path, &st ) && vol->v_mtime != st.st_mtime ) {
1962 vol->v_mtime = st.st_mtime;
1963 if (!obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED))
1972 /* ------------------------- */
1973 void setvoltime(obj, vol )
1979 /* just looking at vol->v_mtime is broken seriously since updates
1980 * from other users afpd processes never are seen.
1981 * This is not the most elegant solution (a shared memory between
1982 * the afpd processes would come closer)
1985 if ( gettimeofday( &tv, 0 ) < 0 ) {
1986 LOG(log_error, logtype_afpd, "setvoltime(%s): gettimeofday: %s", vol->v_path, strerror(errno) );
1989 if( utime( vol->v_path, NULL ) < 0 ) {
1990 /* write of time failed ... probably a read only filesys,
1991 * where no other users can interfere, so there's no issue here
1995 /* a little granularity */
1996 if (vol->v_mtime < tv.tv_sec) {
1997 vol->v_mtime = tv.tv_sec;
1998 /* or finder doesn't update free space */
1999 if (afp_version > 21 && obj->options.server_notif) {
2000 obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED);
2005 /* ------------------------- */
2006 int afp_getvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
2009 int ibuflen _U_, *rbuflen;
2012 u_int16_t vid, bitmap;
2015 memcpy(&vid, ibuf, sizeof( vid ));
2016 ibuf += sizeof( vid );
2017 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2018 bitmap = ntohs( bitmap );
2020 if (NULL == ( vol = getvolbyvid( vid )) ) {
2022 return( AFPERR_PARAM );
2025 return stat_vol(bitmap, vol, rbuf, rbuflen);
2028 /* ------------------------- */
2029 int afp_setvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
2031 char *ibuf, *rbuf _U_;
2032 int ibuflen _U_, *rbuflen;
2036 u_int16_t vid, bitmap;
2042 memcpy(&vid, ibuf, sizeof( vid ));
2043 ibuf += sizeof( vid );
2044 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2045 bitmap = ntohs( bitmap );
2046 ibuf += sizeof(bitmap);
2048 if (( vol = getvolbyvid( vid )) == NULL ) {
2049 return( AFPERR_PARAM );
2052 if ((vol->v_flags & AFPVOL_RO))
2053 return AFPERR_VLOCK;
2055 /* we can only set the backup date. */
2056 if (bitmap != (1 << VOLPBIT_BDATE))
2057 return AFPERR_BITMAP;
2059 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2060 if ( ad_open( vol->v_path, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR,
2063 return AFPERR_VLOCK;
2065 return AFPERR_ACCESS;
2068 memcpy(&aint, ibuf, sizeof(aint));
2069 ad_setdate(&ad, AD_DATE_BACKUP, aint);
2070 ad_flush(&ad, ADFLAGS_HF);
2071 ad_close(&ad, ADFLAGS_HF);
2075 /* ------------------------- */
2076 int wincheck(const struct vol *vol, const char *path)
2080 if (!(vol->v_flags & AFPVOL_MSWINDOWS))
2083 /* empty paths are not allowed */
2084 if ((len = strlen(path)) == 0)
2087 /* leading or trailing whitespaces are not allowed, carriage returns
2088 * and probably other whitespace is okay, tabs are not allowed
2090 if ((path[0] == ' ') || (path[len-1] == ' '))
2093 /* certain characters are not allowed */
2094 if (strpbrk(path, MSWINDOWS_BADCHARS))
2097 /* everything else is okay */
2103 * precreate a folder
2104 * this is only intended for folders in the volume root
2105 * It will *not* work if the folder name contains extended characters
2107 static int create_special_folder (const struct vol *vol, const struct _special_folder *folder)
2116 p = (char *) malloc ( strlen(vol->v_path)+strlen(folder->name)+2);
2118 LOG(log_error, logtype_afpd,"malloc failed");
2122 q=strdup(folder->name);
2124 LOG(log_error, logtype_afpd,"malloc failed");
2128 strcpy(p, vol->v_path);
2133 if ((vol->v_casefold & AFPVOL_MTOUUPPER))
2135 else if ((vol->v_casefold & AFPVOL_MTOULOWER))
2141 if ( (ret = stat( p, &st )) < 0 ) {
2142 if (folder->precreate) {
2143 if (ad_mkdir(p, folder->mode)) {
2144 LOG(log_debug, logtype_afpd,"Creating '%s' failed in %s: %s", p, vol->v_path, strerror(errno));
2153 if ( !ret && folder->hide) {
2155 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2156 if (ad_open( p, vol_noadouble(vol) | ADFLAGS_HF|ADFLAGS_DIR,
2157 O_RDWR|O_CREAT, 0666, &ad) < 0) {
2162 if ((ad_get_HF_flags( &ad ) & O_CREAT) ) {
2163 if (ad_getentryoff(&ad, ADEID_NAME)) {
2164 ad_setentrylen( &ad, ADEID_NAME, strlen(folder->name));
2165 memcpy(ad_entry( &ad, ADEID_NAME ), folder->name,
2166 ad_getentrylen( &ad, ADEID_NAME ));
2170 ad_getattr(&ad, &attr);
2171 attr |= htons( ntohs( attr ) | ATTRBIT_INVISIBLE );
2172 ad_setattr(&ad, attr);
2174 /* do the same with the finder info */
2175 if (ad_entry(&ad, ADEID_FINDERI)) {
2176 memcpy(&attr, ad_entry(&ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF, sizeof(attr));
2177 attr |= htons(FINDERINFO_INVISIBLE);
2178 memcpy(ad_entry(&ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF,&attr, sizeof(attr));
2181 ad_flush( &ad, ADFLAGS_HF );
2182 ad_close( &ad, ADFLAGS_HF );
2189 static void handle_special_folders (const struct vol * vol)
2191 const _special_folder *p = &special_folders[0];
2193 if ((vol->v_flags & AFPVOL_RO))
2196 for (; p->name != NULL; p++) {
2197 create_special_folder (vol, p);
2202 * Save the volume options to a file, used by
2204 * Writing the file everytime a volume is opened is
2205 * unnecessary, but it shouldn't hurt much.
2207 static int savevoloptions (const struct vol *vol)
2210 char item[MAXPATHLEN];
2214 const _vol_opt_name *op = &vol_opt_names[0];
2215 const _vol_opt_name *cf = &vol_opt_casefold[0];
2217 strlcpy (item, vol->v_path, sizeof(item));
2218 strlcat (item, "/.AppleDesktop/", sizeof(item));
2219 strlcat (item, VOLINFOFILE, sizeof(item));
2221 if ((fd = open( item, O_RDWR | O_CREAT , 0666)) <0 ) {
2222 LOG(log_debug, logtype_afpd,"Error opening %s: %s", item, strerror(errno));
2226 /* try to get a lock */
2228 lock.l_whence = SEEK_SET;
2230 lock.l_type = F_WRLCK;
2232 if (fcntl(fd, F_SETLK, &lock) < 0) {
2233 if (errno == EACCES || errno == EAGAIN) {
2234 /* ignore, other process already writing the file */
2237 LOG(log_error, logtype_cnid, "savevoloptions: cannot get lock: %s", strerror(errno));
2242 /* write volume options */
2243 snprintf(buf, sizeof(buf), "MAC_CHARSET:%s\n", vol->v_maccodepage);
2244 snprintf(item, sizeof(item), "VOL_CHARSET:%s\n", vol->v_volcodepage);
2245 strlcat(buf, item, sizeof(buf));
2247 switch (vol->v_adouble) {
2249 strlcat(buf, "ADOUBLE_VER:v1\n", sizeof(buf));
2252 strlcat(buf, "ADOUBLE_VER:v2\n", sizeof(buf));
2254 case AD_VERSION2_OSX:
2255 strlcat(buf, "ADOUBLE_VER:osx\n", sizeof(buf));
2257 case AD_VERSION1_ADS:
2258 strlcat(buf, "ADOUBLE_VER:ads\n", sizeof(buf));
2262 strlcat(buf, "CNIDBACKEND:", sizeof(buf));
2263 strlcat(buf, vol->v_cnidscheme, sizeof(buf));
2264 strlcat(buf, "\n", sizeof(buf));
2266 strlcat(buf, "CNIDDBDHOST:", sizeof(buf));
2267 strlcat(buf, Cnid_srv, sizeof(buf));
2268 strlcat(buf, "\n", sizeof(buf));
2270 snprintf(item, sizeof(item), "CNIDDBDPORT:%u\n", Cnid_port);
2271 strlcat(buf, item, sizeof(buf));
2273 strcpy(item, "CNID_DBPATH:");
2275 strlcat(item, vol->v_dbpath, sizeof(item));
2277 strlcat(item, vol->v_path, sizeof(item));
2278 strlcat(item, "\n", sizeof(item));
2279 strlcat(buf, item, sizeof(buf));
2282 strcpy(item, "VOLUME_OPTS:");
2283 for (;op->name; op++) {
2284 if ( (vol->v_flags & op->option) ) {
2285 strlcat(item, op->name, sizeof(item));
2286 strlcat(item, " ", sizeof(item));
2289 strlcat(item, "\n", sizeof(item));
2290 strlcat(buf, item, sizeof(buf));
2292 /* casefold flags */
2293 strcpy(item, "VOLCASEFOLD:");
2294 for (;cf->name; cf++) {
2295 if ( (vol->v_casefold & cf->option) ) {
2296 strlcat(item, cf->name, sizeof(item));
2297 strlcat(item, " ", sizeof(item));
2300 strlcat(item, "\n", sizeof(item));
2301 strlcat(buf, item, sizeof(buf));
2303 if (strlen(buf) >= sizeof(buf)-1)
2304 LOG(log_debug, logtype_afpd,"Error writing .volinfo file: buffer too small, %s", buf);
2307 if (write( fd, buf, strlen(buf)) < 0) {
2308 LOG(log_debug, logtype_afpd,"Error writing .volinfo file: %s", strerror(errno));
2311 ftruncate(fd, strlen(buf));
2314 lock.l_type = F_UNLCK;
2315 fcntl(fd, F_SETLK, &lock);