2 * $Id: volume.c,v 1.66 2006-09-15 00:05:51 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
119 #define VOLOPT_DFLTPERM 22
121 #define VOLOPT_UMASK 19
122 #define VOLOPT_DFLTPERM 20
123 #endif /* FORCE_UIDGID */
125 #define VOLOPT_MAX (VOLOPT_DFLTPERM +1)
127 #define VOLOPT_NUM (VOLOPT_MAX + 1)
130 #define VOLOPT_DEFAULT ":DEFAULT:"
131 #define VOLOPT_DEFAULT_LEN 9
137 typedef struct _special_folder {
144 static const _special_folder special_folders[] = {
145 {"Network Trash Folder", 1, 0777, 1},
146 {"Temporary Items", 1, 0777, 1},
147 {".AppleDesktop", 1, 0777, 0},
149 {"TheFindByContentFolder", 0, 0, 1},
150 {"TheVolumeSettingsFolder", 0, 0, 1},
154 typedef struct _volopt_name {
155 const u_int32_t option;
159 static const _vol_opt_name vol_opt_names[] = {
160 {AFPVOL_A2VOL, "PRODOS"}, /* prodos volume */
161 {AFPVOL_CRLF, "CRLF"}, /* cr/lf translation */
162 {AFPVOL_NOADOUBLE, "NOADOUBLE"}, /* don't create .AppleDouble by default */
163 {AFPVOL_RO, "READONLY"}, /* read-only volume */
164 {AFPVOL_MSWINDOWS, "MSWINDOWS"}, /* deal with ms-windows yuckiness. this is going away. */
165 {AFPVOL_NOHEX, "NOHEX"}, /* don't do :hex translation */
166 {AFPVOL_USEDOTS, "USEDOTS"}, /* use real dots */
167 {AFPVOL_LIMITSIZE, "LIMITSIZE"}, /* limit size for older macs */
168 {AFPVOL_MAPASCII, "MAPASCII"}, /* map the ascii range as well */
169 {AFPVOL_DROPBOX, "DROPBOX"}, /* dropkludge dropbox support */
170 {AFPVOL_NOFILEID, "NOFILEID"}, /* don't advertise createid resolveid and deleteid calls */
171 {AFPVOL_NOSTAT, "NOSTAT"}, /* advertise the volume even if we can't stat() it
172 * maybe because it will be mounted later in preexec */
173 {AFPVOL_UNIX_PRIV, "UNIXPRIV"}, /* support unix privileges */
174 {AFPVOL_NODEV, "NODEV"}, /* always use 0 for device number in cnid calls */
175 {AFPVOL_CASEINSEN, "CASEINSENSITIVE"}, /* volume is case insensitive */
176 {AFPVOL_EILSEQ, "ILLEGALSEQ"}, /* encode illegal sequence */
177 {AFPVOL_CACHE, "CACHEID"}, /* Use adouble v2 CNID caching, default don't use it */
181 static const _vol_opt_name vol_opt_casefold[] = {
182 {AFPVOL_MTOUUPPER, "MTOULOWER"},
183 {AFPVOL_MTOULOWER, "MTOULOWER"},
184 {AFPVOL_UTOMUPPER, "UTOMUPPER"},
185 {AFPVOL_UTOMLOWER, "UTOMLOWER"},
189 static void handle_special_folders (const struct vol *);
190 static int savevoloptions (const struct vol *);
191 static void deletevol(struct vol *vol);
193 static __inline__ void volfree(struct vol_option *options,
194 const struct vol_option *save)
199 for (i = 0; i < VOLOPT_MAX; i++) {
200 if (options[i].c_value && (options[i].c_value != save[i].c_value))
201 free(options[i].c_value);
204 for (i = 0; i < VOLOPT_MAX; i++) {
205 if (options[i].c_value)
206 free(options[i].c_value);
212 /* handle variable substitutions. here's what we understand:
213 * $b -> basename of path
214 * $c -> client ip/appletalk address
215 * $d -> volume pathname on server
216 * $f -> full name (whatever's in the gecos field)
219 * $i -> client ip/appletalk address without port
220 * $s -> server name (hostname if it doesn't exist)
221 * $u -> username (guest is usually nobody)
222 * $v -> volume name or basename if null
223 * $z -> zone (may not exist)
228 #define is_var(a, b) (strncmp((a), (b), 2) == 0)
230 static char *volxlate(AFPObj *obj, char *dest, size_t destlen,
231 char *src, struct passwd *pwd, char *path, char *volname)
241 dest = calloc(destlen +1, 1);
247 strlcpy(dest, src, destlen +1);
248 if ((p = strchr(src, '$')) == NULL) /* nothing to do */
251 /* first part of the path. just forward to the next variable. */
252 len = MIN((size_t)(p - src), destlen);
258 while (p && destlen > 0) {
259 /* now figure out what the variable is */
261 if (is_var(p, "$b")) {
263 if ((q = strrchr(path, '/')) == NULL)
265 else if (*(q + 1) != '\0')
268 } else if (is_var(p, "$c")) {
269 if (obj->proto == AFPPROTO_ASP) {
270 ASP asp = obj->handle;
272 len = sprintf(dest, "%u.%u", ntohs(asp->asp_sat.sat_addr.s_net),
273 asp->asp_sat.sat_addr.s_node);
277 } else if (obj->proto == AFPPROTO_DSI) {
278 DSI *dsi = obj->handle;
280 len = sprintf(dest, "%s:%u", inet_ntoa(dsi->client.sin_addr),
281 ntohs(dsi->client.sin_port));
285 } else if (is_var(p, "$d")) {
287 } else if (is_var(p, "$f")) {
288 if ((q = strchr(pwd->pw_gecos, ',')))
291 } else if (is_var(p, "$g")) {
292 struct group *grp = getgrgid(pwd->pw_gid);
295 } else if (is_var(p, "$h")) {
296 q = obj->options.hostname;
297 } else if (is_var(p, "$i")) {
298 if (obj->proto == AFPPROTO_ASP) {
299 ASP asp = obj->handle;
301 len = sprintf(dest, "%u", ntohs(asp->asp_sat.sat_addr.s_net));
305 } else if (obj->proto == AFPPROTO_DSI) {
306 DSI *dsi = obj->handle;
308 q = inet_ntoa(dsi->client.sin_addr);
310 } else if (is_var(p, "$s")) {
313 else if (obj->options.server) {
314 q = obj->options.server;
316 q = obj->options.hostname;
317 } else if (is_var(p, "$u")) {
319 } else if (is_var(p, "$v")) {
324 if ((q = strrchr(path, '/')) == NULL)
326 else if (*(q + 1) != '\0')
329 } else if (is_var(p, "$z")) {
331 } else if (is_var(p, "$$")) {
336 /* copy the stuff over. if we don't understand something that we
337 * should, just skip it over. */
339 len = MIN(p == q ? 2 : strlen(q), destlen);
340 strncpy(dest, q, len);
345 /* stuff up to next $ */
347 p = strchr(src, '$');
348 len = p ? MIN((size_t)(p - src), destlen) : destlen;
350 strncpy(dest, src, len);
358 /* to make sure that val is valid, make sure to select an opt that
360 static int optionok(const char *buf, const char *opt, const char *val)
362 if (!strstr(buf,opt))
370 /* -------------------- */
371 static void setoption(struct vol_option *options, struct vol_option *save, int opt, const char *val)
373 if (options[opt].c_value && (!save || options[opt].c_value != save[opt].c_value))
374 free(options[opt].c_value);
375 options[opt].c_value = strdup(val + 1);
378 /* ------------------------------------------
379 handle all the options. tmp can't be NULL. */
380 static void volset(struct vol_option *options, struct vol_option *save,
381 char *volname, int vlen,
386 val = strchr(tmp, ':');
388 /* we'll assume it's a volume name. */
389 strncpy(volname, tmp, vlen);
394 LOG(log_debug, logtype_afpd, "Parsing volset %s", val);
396 if (optionok(tmp, "allow:", val)) {
397 setoption(options, save, VOLOPT_ALLOW, val);
399 } else if (optionok(tmp, "deny:", val)) {
400 setoption(options, save, VOLOPT_DENY, val);
402 } else if (optionok(tmp, "rwlist:", val)) {
403 setoption(options, save, VOLOPT_RWLIST, val);
405 } else if (optionok(tmp, "rolist:", val)) {
406 setoption(options, save, VOLOPT_ROLIST, val);
408 } else if (optionok(tmp, "codepage:", val)) {
409 LOG (log_error, logtype_afpd, "The old codepage system has been removed. Please make sure to read the documentation !!!!");
410 /* Make sure we don't screw anything */
412 } else if (optionok(tmp, "volcharset:", val)) {
413 setoption(options, save, VOLOPT_ENCODING, val);
414 } else if (optionok(tmp, "maccharset:", val)) {
415 setoption(options, save, VOLOPT_MACCHARSET, val);
416 } else if (optionok(tmp, "veto:", val)) {
417 setoption(options, save, VOLOPT_VETO, val);
418 } else if (optionok(tmp, "cnidscheme:", val)) {
419 setoption(options, save, VOLOPT_CNIDSCHEME, val);
420 } else if (optionok(tmp, "casefold:", val)) {
421 if (strcasecmp(val + 1, "tolower") == 0)
422 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMLOWER;
423 else if (strcasecmp(val + 1, "toupper") == 0)
424 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMUPPER;
425 else if (strcasecmp(val + 1, "xlatelower") == 0)
426 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UUPPERMLOWER;
427 else if (strcasecmp(val + 1, "xlateupper") == 0)
428 options[VOLOPT_CASEFOLD].i_value = AFPVOL_ULOWERMUPPER;
429 } else if (optionok(tmp, "adouble:", val)) {
430 if (strcasecmp(val + 1, "v1") == 0)
431 options[VOLOPT_ADOUBLE].i_value = AD_VERSION1;
432 #if AD_VERSION == AD_VERSION2
433 else if (strcasecmp(val + 1, "v2") == 0)
434 options[VOLOPT_ADOUBLE].i_value = AD_VERSION2;
435 else if (strcasecmp(val + 1, "osx") == 0)
436 options[VOLOPT_ADOUBLE].i_value = AD_VERSION2_OSX;
437 else if (strcasecmp(val + 1, "ads") == 0)
438 options[VOLOPT_ADOUBLE].i_value = AD_VERSION1_ADS;
440 } else if (optionok(tmp, "options:", val)) {
443 if ((p = strtok(val + 1, ",")) == NULL) /* nothing */
447 if (strcasecmp(p, "prodos") == 0)
448 options[VOLOPT_FLAGS].i_value |= AFPVOL_A2VOL;
449 else if (strcasecmp(p, "mswindows") == 0) {
450 options[VOLOPT_FLAGS].i_value |= AFPVOL_MSWINDOWS | AFPVOL_USEDOTS;
451 } else if (strcasecmp(p, "crlf") == 0)
452 options[VOLOPT_FLAGS].i_value |= AFPVOL_CRLF;
453 else if (strcasecmp(p, "noadouble") == 0)
454 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOADOUBLE;
455 else if (strcasecmp(p, "ro") == 0)
456 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
457 else if (strcasecmp(p, "nohex") == 0)
458 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOHEX;
459 else if (strcasecmp(p, "usedots") == 0)
460 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS;
461 else if (strcasecmp(p, "limitsize") == 0)
462 options[VOLOPT_FLAGS].i_value |= AFPVOL_LIMITSIZE;
463 /* support for either "dropbox" or "dropkludge" */
464 else if (strcasecmp(p, "dropbox") == 0)
465 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
466 else if (strcasecmp(p, "dropkludge") == 0)
467 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
468 else if (strcasecmp(p, "nofileid") == 0)
469 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOFILEID;
470 else if (strcasecmp(p, "nostat") == 0)
471 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOSTAT;
472 else if (strcasecmp(p, "preexec_close") == 0)
473 options[VOLOPT_PREEXEC].i_value = 1;
474 else if (strcasecmp(p, "root_preexec_close") == 0)
475 options[VOLOPT_ROOTPREEXEC].i_value = 1;
476 else if (strcasecmp(p, "upriv") == 0)
477 options[VOLOPT_FLAGS].i_value |= AFPVOL_UNIX_PRIV;
478 else if (strcasecmp(p, "nodev") == 0)
479 options[VOLOPT_FLAGS].i_value |= AFPVOL_NODEV;
480 else if (strcasecmp(p, "caseinsensitive") == 0)
481 options[VOLOPT_FLAGS].i_value |= AFPVOL_CASEINSEN;
482 else if (strcasecmp(p, "illegalseq") == 0)
483 options[VOLOPT_FLAGS].i_value |= AFPVOL_EILSEQ;
484 else if (strcasecmp(p, "cachecnid") == 0)
485 options[VOLOPT_FLAGS].i_value |= AFPVOL_CACHE;
487 p = strtok(NULL, ",");
490 } else if (optionok(tmp, "dbpath:", val)) {
491 setoption(options, save, VOLOPT_DBPATH, val);
493 } else if (optionok(tmp, "umask:", val)) {
494 options[VOLOPT_UMASK].i_value = (int)strtol(val +1, NULL, 8);
495 } else if (optionok(tmp, "perm:", val)) {
496 options[VOLOPT_DFLTPERM].i_value = (int)strtol(val+1, NULL, 8);
497 } else if (optionok(tmp, "mapchars:",val)) {
498 setoption(options, save, VOLOPT_MAPCHARS, val);
500 } else if (optionok(tmp, "password:", val)) {
501 setoption(options, save, VOLOPT_PASSWORD, val);
505 /* this code allows forced uid/gid per volume settings */
506 } else if (optionok(tmp, "forceuid:", val)) {
507 setoption(options, save, VOLOPT_FORCEUID, val);
508 } else if (optionok(tmp, "forcegid:", val)) {
509 setoption(options, save, VOLOPT_FORCEGID, val);
511 #endif /* FORCE_UIDGID */
512 } else if (optionok(tmp, "root_preexec:", val)) {
513 setoption(options, save, VOLOPT_ROOTPREEXEC, val);
515 } else if (optionok(tmp, "preexec:", val)) {
516 setoption(options, save, VOLOPT_PREEXEC, val);
518 } else if (optionok(tmp, "root_postexec:", val)) {
519 setoption(options, save, VOLOPT_ROOTPOSTEXEC, val);
521 } else if (optionok(tmp, "postexec:", val)) {
522 setoption(options, save, VOLOPT_POSTEXEC, val);
525 /* ignore unknown options */
526 LOG(log_debug, logtype_afpd, "ignoring unknown volume option: %s", tmp);
531 /* ----------------- */
532 static void showvol(const ucs2_t *name)
535 for ( volume = Volumes; volume; volume = volume->v_next ) {
536 if (volume->v_hide && !strcasecmp_w( volume->v_name, name ) ) {
543 /* ------------------------------- */
544 static int creatvol(AFPObj *obj, struct passwd *pwd,
545 char *path, char *name,
546 struct vol_option *options,
547 const int user /* user defined volume */
555 if ( name == NULL || *name == '\0' ) {
556 if ((name = strrchr( path, '/' )) == NULL) {
557 return -1; /* Obviously not a fully qualified path */
560 /* if you wish to share /, you need to specify a name. */
565 vlen = strlen( name );
566 if ( vlen > AFPVOL_NAMELEN ) {
567 vlen = AFPVOL_NAMELEN;
568 name[AFPVOL_NAMELEN] = '\0';
571 /* convert name to UCS2 first */
572 if ( 0 >= ( vlen = convert_string(obj->options.unixcharset, CH_UCS2, name, vlen, tmpname, 512)) )
575 for ( volume = Volumes; volume; volume = volume->v_next ) {
576 if ( strcasecmp_w( volume->v_name, tmpname ) == 0 ) {
577 if (volume->v_deleted) {
578 volume->v_new = hide = 1;
581 return -1; /* Won't be able to access it, anyway... */
587 if (!( volume = (struct vol *)calloc(1, sizeof( struct vol ))) ) {
588 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
591 if ( NULL == ( volume->v_name = strdup_w(tmpname))) {
592 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
596 if (!( volume->v_path = (char *)malloc( strlen( path ) + 1 )) ) {
597 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
598 free(volume->v_name);
602 volume->v_hide = hide;
603 strcpy( volume->v_path, path );
607 #endif /* __svr4__ */
608 /* os X start at 1 and use network order ie. 1 2 3 */
609 volume->v_vid = ++lastvid;
610 volume->v_vid = htons(volume->v_vid);
614 /* should we casefold? */
615 volume->v_casefold = options[VOLOPT_CASEFOLD].i_value;
617 /* shift in some flags */
618 volume->v_flags = options[VOLOPT_FLAGS].i_value;
620 volume->v_ad_options = 0;
621 if ((volume->v_flags & AFPVOL_NODEV))
622 volume->v_ad_options |= ADVOL_NODEV;
623 if ((volume->v_flags & AFPVOL_CACHE))
624 volume->v_ad_options |= ADVOL_CACHE;
625 if ((volume->v_flags & AFPVOL_UNIX_PRIV))
626 volume->v_ad_options |= ADVOL_UNIXPRIV;
628 if (options[VOLOPT_PASSWORD].c_value)
629 volume->v_password = strdup(options[VOLOPT_PASSWORD].c_value);
631 if (options[VOLOPT_VETO].c_value)
632 volume->v_veto = strdup(options[VOLOPT_VETO].c_value);
634 if (options[VOLOPT_ENCODING].c_value)
635 volume->v_volcodepage = strdup(options[VOLOPT_ENCODING].c_value);
637 if (options[VOLOPT_MACCHARSET].c_value)
638 volume->v_maccodepage = strdup(options[VOLOPT_MACCHARSET].c_value);
640 if (options[VOLOPT_DBPATH].c_value)
641 volume->v_dbpath = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_DBPATH].c_value, pwd, path, name);
643 if (options[VOLOPT_CNIDSCHEME].c_value)
644 volume->v_cnidscheme = strdup(options[VOLOPT_CNIDSCHEME].c_value);
646 if (options[VOLOPT_UMASK].i_value)
647 volume->v_umask = (mode_t)options[VOLOPT_UMASK].i_value;
649 if (options[VOLOPT_DFLTPERM].i_value)
650 volume->v_perm = (mode_t)options[VOLOPT_DFLTPERM].i_value;
652 if (options[VOLOPT_ADOUBLE].i_value)
653 volume->v_adouble = options[VOLOPT_ADOUBLE].i_value;
655 volume->v_adouble = AD_VERSION;
659 if (options[VOLOPT_FORCEUID].c_value) {
660 volume->v_forceuid = strdup(options[VOLOPT_FORCEUID].c_value);
662 volume->v_forceuid = NULL; /* set as null so as to return 0 later on */
665 if (options[VOLOPT_FORCEGID].c_value) {
666 volume->v_forcegid = strdup(options[VOLOPT_FORCEGID].c_value);
668 volume->v_forcegid = NULL; /* set as null so as to return 0 later on */
672 if (options[VOLOPT_PREEXEC].c_value)
673 volume->v_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_PREEXEC].c_value, pwd, path, name);
674 volume->v_preexec_close = options[VOLOPT_PREEXEC].i_value;
676 if (options[VOLOPT_POSTEXEC].c_value)
677 volume->v_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_POSTEXEC].c_value, pwd, path, name);
679 if (options[VOLOPT_ROOTPREEXEC].c_value)
680 volume->v_root_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPREEXEC].c_value, pwd, path, name);
681 volume->v_root_preexec_close = options[VOLOPT_ROOTPREEXEC].i_value;
683 if (options[VOLOPT_ROOTPOSTEXEC].c_value)
684 volume->v_root_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPOSTEXEC].c_value, pwd, path, name);
688 volume->v_next = Volumes;
693 /* ---------------- */
694 static char *myfgets( buf, size, fp )
703 while ((EOF != ( c = getc( fp )) ) && ( size > 0 )) {
704 if ( c == '\n' || c == '\r' ) {
722 /* check access list. this function wants something of the following
724 * @group,name,name2,@group2,name3
726 * a NULL argument allows everybody to have access.
727 * we return three things:
729 * 0: list exists, but name isn't in it
733 #ifndef NO_REAL_USER_NAME
734 /* authentication is case insensitive
735 * FIXME should we do the same with group name?
737 #define access_strcmp strcasecmp
740 #define access_strcmp strcmp
744 static int accessvol(args, name)
748 char buf[MAXPATHLEN + 1], *p;
754 strlcpy(buf, args, sizeof(buf));
755 if ((p = strtok(buf, ",")) == NULL) /* nothing, return okay */
759 if (*p == '@') { /* it's a group */
760 if ((gr = getgrnam(p + 1)) && gmem(gr->gr_gid))
762 } else if (access_strcmp(p, name) == 0) /* it's a user name */
764 p = strtok(NULL, ",");
770 static void setextmap( ext, type, creator, user)
771 char *ext, *type, *creator;
777 if (Extmap == NULL) {
778 if (( Extmap = calloc(1, sizeof( struct extmap ))) == NULL ) {
779 LOG(log_error, logtype_afpd, "setextmap: calloc: %s", strerror(errno) );
784 for ( em = Extmap, cnt = 0; em->em_ext; em++, cnt++) {
785 if ( (strdiacasecmp( em->em_ext, ext )) == 0 ) {
790 if ( em->em_ext == NULL ) {
791 if (!(Extmap = realloc( Extmap, sizeof( struct extmap ) * (cnt +2))) ) {
792 LOG(log_error, logtype_afpd, "setextmap: realloc: %s", strerror(errno) );
795 (Extmap +cnt +1)->em_ext = NULL;
797 } else if ( !user ) {
803 if (!(em->em_ext = strdup( ext))) {
804 LOG(log_error, logtype_afpd, "setextmap: strdup: %s", strerror(errno) );
808 if ( *type == '\0' ) {
809 memcpy(em->em_type, "????", sizeof( em->em_type ));
811 memcpy(em->em_type, type, sizeof( em->em_type ));
813 if ( *creator == '\0' ) {
814 memcpy(em->em_creator, "UNIX", sizeof( em->em_creator ));
816 memcpy(em->em_creator, creator, sizeof( em->em_creator ));
820 /* -------------------------- */
821 static int extmap_cmp(const void *map1, const void *map2)
823 const struct extmap *em1 = map1;
824 const struct extmap *em2 = map2;
825 return strdiacasecmp(em1->em_ext, em2->em_ext);
828 static void sortextmap( void)
833 if ((em = Extmap) == NULL) {
841 qsort(Extmap, Extmap_cnt, sizeof(struct extmap), extmap_cmp);
842 if (*Extmap->em_ext == 0) {
843 /* the first line is really "." the default entry,
844 * we remove the leading '.' in setextmap
851 /* ----------------------
853 static void free_extmap( void)
858 for ( em = Extmap; em->em_ext; em++) {
868 /* ----------------------
870 static int volfile_changed(struct afp_volume_name *p)
880 if (!stat( name, &st) && st.st_mtime > p->mtime) {
881 p->mtime = st.st_mtime;
887 /* ----------------------
888 * Read a volume configuration file and add the volumes contained within to
889 * the global volume list. If p2 is non-NULL, the file that is opened is
892 * Lines that begin with # and blank lines are ignored.
893 * Volume lines are of the form:
894 * <unix path> [<volume name>] [allow:<user>,<@group>,...] \
895 * [codepage:<file>] [casefold:<num>]
896 * <extension> TYPE [CREATOR]
898 static int readvolfile(obj, p1, p2, user, pwent)
900 struct afp_volume_name *p1;
903 struct passwd *pwent;
906 char path[ MAXPATHLEN + 1], tmp[ MAXPATHLEN + 1],
907 volname[ AFPVOL_NAMELEN + 1 ], buf[ BUFSIZ ],
908 type[ 5 ], creator[ 5 ];
911 struct vol_option options[VOLOPT_NUM], save_options[VOLOPT_NUM];
919 strcpy( path, p1->name );
926 p1->full_name = strdup(path);
929 if (NULL == ( fp = fopen( path, "r" )) ) {
933 if (fd != -1 && !fstat( fd, &st) ) {
934 p1->mtime = st.st_mtime;
937 memset(save_options, 0, sizeof(save_options));
938 while ( myfgets( buf, sizeof( buf ), fp ) != NULL ) {
939 initline( strlen( buf ), buf );
940 parseline( sizeof( path ) - 1, path );
947 /* change the default options for this file */
948 if (strncmp(path, VOLOPT_DEFAULT, VOLOPT_DEFAULT_LEN) == 0) {
950 for (i = 0; i < VOLOPT_NUM; i++) {
951 if (parseline( sizeof( path ) - VOLOPT_DEFAULT_LEN - 1,
952 path + VOLOPT_DEFAULT_LEN) < 0)
954 volset(save_options, NULL, tmp, sizeof(tmp) - 1,
955 path + VOLOPT_DEFAULT_LEN);
961 if (( p = strchr( path, '/' )) != NULL ) {
969 if ( u == NULL || *u == '\0' || ( pw = getpwnam( u )) == NULL ) {
972 strcpy( tmp, pw->pw_dir );
973 if ( p != NULL && *p != '\0' ) {
977 /* Tag a user's home directory with their umask. Note, this will
978 * be overwritten if the user actually specifies a umask: option
979 * for a '~' volume. */
980 save_options[VOLOPT_UMASK].i_value = obj->options.save_mask;
984 /* send path through variable substitution */
985 if (*path != '~') /* need to copy path to tmp */
988 pwent = getpwnam(obj->username);
989 volxlate(obj, path, sizeof(path) - 1, tmp, pwent, NULL, NULL);
991 /* this is sort of braindead. basically, i want to be
992 * able to specify things in any order, but i don't want to
993 * re-write everything.
995 * currently we have options:
1003 * options:prodos,crlf,noadouble,ro...
1008 * namemask:x,y,!z (not implemented yet)
1010 memcpy(options, save_options, sizeof(options));
1013 /* read in up to VOLOP_NUM possible options */
1014 for (i = 0; i < VOLOPT_NUM; i++) {
1015 if (parseline( sizeof( tmp ) - 1, tmp ) < 0)
1018 volset(options, save_options, volname, sizeof(volname) - 1, tmp);
1021 /* check allow/deny lists:
1022 allow -> either no list (-1), or in list (1)
1023 deny -> either no list (-1), or not in list (0) */
1024 if (accessvol(options[VOLOPT_ALLOW].c_value, obj->username) &&
1025 (accessvol(options[VOLOPT_DENY].c_value, obj->username) < 1)) {
1027 /* handle read-only behaviour. semantics:
1028 * 1) neither the rolist nor the rwlist exist -> rw
1029 * 2) rolist exists -> ro if user is in it.
1030 * 3) rwlist exists -> ro unless user is in it. */
1031 if (((options[VOLOPT_FLAGS].i_value & AFPVOL_RO) == 0) &&
1032 ((accessvol(options[VOLOPT_ROLIST].c_value,
1033 obj->username) == 1) ||
1034 !accessvol(options[VOLOPT_RWLIST].c_value,
1036 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
1038 /* do variable substitution for volname */
1039 volxlate(obj, tmp, sizeof(tmp) - 1, volname, pwent, path, NULL);
1040 creatvol(obj, pwent, path, tmp, options, p2 != NULL);
1042 volfree(options, save_options);
1046 parseline( sizeof( type ) - 1, type );
1047 parseline( sizeof( creator ) - 1, creator );
1048 setextmap( path, type, creator, user);
1055 volfree(save_options, NULL);
1057 if ( fclose( fp ) != 0 ) {
1058 LOG(log_error, logtype_afpd, "readvolfile: fclose: %s", strerror(errno) );
1064 /* ------------------------------- */
1065 static void volume_free(struct vol *vol)
1070 free(vol->v_password);
1072 free(vol->v_volcodepage);
1073 free(vol->v_maccodepage);
1074 free(vol->v_cnidscheme);
1075 free(vol->v_dbpath);
1078 free(vol->v_forceuid);
1079 free(vol->v_forcegid);
1080 #endif /* FORCE_UIDGID */
1083 /* ------------------------------- */
1084 static void free_volumes(void )
1087 struct vol *nvol, *ovol;
1089 for ( vol = Volumes; vol; vol = vol->v_next ) {
1090 if (( vol->v_flags & AFPVOL_OPEN ) ) {
1097 for ( vol = Volumes, ovol = NULL; vol; vol = nvol) {
1100 if (vol->v_name == NULL) {
1101 if (Volumes == vol) {
1106 ovol->v_next = nvol;
1116 /* ------------------------------- */
1117 static void volume_unlink(struct vol *volume)
1119 struct vol *vol, *ovol, *nvol;
1121 if (volume == Volumes) {
1122 Volumes = Volumes->v_next;
1125 for ( vol = Volumes->v_next, ovol = Volumes; vol; vol = nvol) {
1128 if (vol == volume) {
1129 ovol->v_next = nvol;
1138 static int getvolspace( vol, bfree, btotal, xbfree, xbtotal, bsize )
1140 u_int32_t *bfree, *btotal, *bsize;
1141 VolSpace *xbfree, *xbtotal;
1145 #ifndef NO_QUOTA_SUPPORT
1146 VolSpace qfree, qtotal;
1149 spaceflag = AFPVOL_GVSMASK & vol->v_flags;
1150 /* report up to 2GB if afp version is < 2.2 (4GB if not) */
1151 maxsize = (vol->v_flags & AFPVOL_A2VOL) ? 0x01fffe00 :
1152 (((afp_version < 22) || (vol->v_flags & AFPVOL_LIMITSIZE))
1153 ? 0x7fffffffL : 0xffffffffL);
1156 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_AFSGVS ) {
1157 if ( afs_getvolspace( vol, xbfree, xbtotal, bsize ) == AFP_OK ) {
1158 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_AFSGVS;
1159 goto getvolspace_done;
1164 if (( rc = ustatfs_getvolspace( vol, xbfree, xbtotal,
1165 bsize)) != AFP_OK ) {
1169 #define min(a,b) ((a)<(b)?(a):(b))
1170 #ifndef NO_QUOTA_SUPPORT
1171 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_UQUOTA ) {
1172 if ( uquota_getvolspace( vol, &qfree, &qtotal, *bsize ) == AFP_OK ) {
1173 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_UQUOTA;
1174 *xbfree = min(*xbfree, qfree);
1175 *xbtotal = min( *xbtotal, qtotal);
1176 goto getvolspace_done;
1180 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_USTATFS;
1183 *bfree = min( *xbfree, maxsize);
1184 *btotal = min( *xbtotal, maxsize);
1188 /* -----------------------
1189 * set volume creation date
1190 * avoid duplicate, well at least it tries
1192 static void vol_setdate(u_int16_t id, struct adouble *adp, time_t date)
1195 struct vol *vol = Volumes;
1197 for ( volume = Volumes; volume; volume = volume->v_next ) {
1198 if (volume->v_vid == id) {
1201 else if ((time_t)(AD_DATE_FROM_UNIX(date)) == volume->v_ctime) {
1203 volume = Volumes; /* restart */
1206 vol->v_ctime = AD_DATE_FROM_UNIX(date);
1207 ad_setdate(adp, AD_DATE_CREATE | AD_DATE_UNIX, date);
1210 /* ----------------------- */
1211 static int getvolparams( bitmap, vol, st, buf, buflen )
1219 int bit = 0, isad = 1;
1222 u_int32_t bfree, btotal, bsize;
1223 VolSpace xbfree, xbtotal; /* extended bytes */
1224 char *data, *nameoff = NULL;
1227 /* courtesy of jallison@whistle.com:
1228 * For MacOS8.x support we need to create the
1229 * .Parent file here if it doesn't exist. */
1231 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1232 if ( ad_open_metadata( vol->v_path, vol_noadouble(vol) | ADFLAGS_DIR, O_CREAT, &ad) < 0 ) {
1234 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1236 } else if (ad_get_HF_flags( &ad ) & O_CREAT) {
1237 slash = strrchr( vol->v_path, '/' );
1241 slash = vol->v_path;
1242 if (ad_getentryoff(&ad, ADEID_NAME)) {
1243 ad_setentrylen( &ad, ADEID_NAME, strlen( slash ));
1244 memcpy(ad_entry( &ad, ADEID_NAME ), slash,
1245 ad_getentrylen( &ad, ADEID_NAME ));
1247 vol_setdate(vol->v_vid, &ad, st->st_mtime);
1248 ad_flush_metadata(&ad);
1251 if (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0)
1252 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1254 vol->v_ctime = aint;
1257 if (( bitmap & ( (1<<VOLPBIT_BFREE)|(1<<VOLPBIT_BTOTAL) |
1258 (1<<VOLPBIT_XBFREE)|(1<<VOLPBIT_XBTOTAL) |
1259 (1<<VOLPBIT_BSIZE)) ) != 0 ) {
1260 if ( getvolspace( vol, &bfree, &btotal, &xbfree, &xbtotal,
1263 ad_close( &ad, ADFLAGS_HF );
1265 return( AFPERR_PARAM );
1270 while ( bitmap != 0 ) {
1271 while (( bitmap & 1 ) == 0 ) {
1279 if (0 == (vol->v_flags & AFPVOL_NOFILEID) && vol->v_cdb != NULL &&
1280 (vol->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1281 ashort = VOLPBIT_ATTR_FILEID;
1283 /* check for read-only.
1284 * NOTE: we don't actually set the read-only flag unless
1285 * it's passed in that way as it's possible to mount
1286 * a read-write filesystem under a read-only one. */
1287 if ((vol->v_flags & AFPVOL_RO) ||
1288 ((utime(vol->v_path, NULL) < 0) && (errno == EROFS))) {
1289 ashort |= VOLPBIT_ATTR_RO;
1291 ashort |= VOLPBIT_ATTR_CATSEARCH;
1292 if (afp_version >= 30) {
1293 ashort |= VOLPBIT_ATTR_UTF8;
1294 if (vol->v_flags & AFPVOL_UNIX_PRIV)
1295 ashort |= VOLPBIT_ATTR_UNIXPRIV;
1297 ashort = htons(ashort);
1298 memcpy(data, &ashort, sizeof( ashort ));
1299 data += sizeof( ashort );
1303 ashort = htons( AFPVOLSIG_DEFAULT );
1304 memcpy(data, &ashort, sizeof( ashort ));
1305 data += sizeof( ashort );
1308 case VOLPBIT_CDATE :
1309 aint = vol->v_ctime;
1310 memcpy(data, &aint, sizeof( aint ));
1311 data += sizeof( aint );
1314 case VOLPBIT_MDATE :
1315 if ( st->st_mtime > vol->v_mtime ) {
1316 vol->v_mtime = st->st_mtime;
1318 aint = AD_DATE_FROM_UNIX(vol->v_mtime);
1319 memcpy(data, &aint, sizeof( aint ));
1320 data += sizeof( aint );
1323 case VOLPBIT_BDATE :
1324 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1325 aint = AD_DATE_START;
1326 memcpy(data, &aint, sizeof( aint ));
1327 data += sizeof( aint );
1331 memcpy(data, &vol->v_vid, sizeof( vol->v_vid ));
1332 data += sizeof( vol->v_vid );
1335 case VOLPBIT_BFREE :
1336 bfree = htonl( bfree );
1337 memcpy(data, &bfree, sizeof( bfree ));
1338 data += sizeof( bfree );
1341 case VOLPBIT_BTOTAL :
1342 btotal = htonl( btotal );
1343 memcpy(data, &btotal, sizeof( btotal ));
1344 data += sizeof( btotal );
1347 #ifndef NO_LARGE_VOL_SUPPORT
1348 case VOLPBIT_XBFREE :
1349 xbfree = hton64( xbfree );
1350 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1351 bcopy(&xbfree, data, sizeof(xbfree));
1352 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1353 memcpy(data, &xbfree, sizeof( xbfree ));
1354 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1355 data += sizeof( xbfree );
1358 case VOLPBIT_XBTOTAL :
1359 xbtotal = hton64( xbtotal );
1360 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1361 bcopy(&xbtotal, data, sizeof(xbtotal));
1362 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1363 memcpy(data, &xbtotal, sizeof( xbtotal ));
1364 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1365 data += sizeof( xbfree );
1367 #endif /* ! NO_LARGE_VOL_SUPPORT */
1371 data += sizeof( u_int16_t );
1374 case VOLPBIT_BSIZE: /* block size */
1375 bsize = htonl(bsize);
1376 memcpy(data, &bsize, sizeof(bsize));
1377 data += sizeof(bsize);
1382 ad_close( &ad, ADFLAGS_HF );
1384 return( AFPERR_BITMAP );
1390 ashort = htons( data - buf );
1391 memcpy(nameoff, &ashort, sizeof( ashort ));
1392 /* name is always in mac charset, FIXME mangle if length > 27 char */
1393 aint = ucs2_to_charset( vol->v_maccharset, vol->v_name, data+1, 255);
1403 ad_close_metadata( &ad);
1405 *buflen = data - buf;
1409 /* ------------------------- */
1410 static int stat_vol(u_int16_t bitmap, struct vol *vol, char *rbuf, int *rbuflen)
1415 if ( stat( vol->v_path, &st ) < 0 ) {
1417 return( AFPERR_PARAM );
1419 /* save the volume device number */
1420 vol->v_dev = st.st_dev;
1422 buflen = *rbuflen - sizeof( bitmap );
1423 if (( ret = getvolparams( bitmap, vol, &st,
1424 rbuf + sizeof( bitmap ), &buflen )) != AFP_OK ) {
1428 *rbuflen = buflen + sizeof( bitmap );
1429 bitmap = htons( bitmap );
1430 memcpy(rbuf, &bitmap, sizeof( bitmap ));
1435 /* ------------------------------- */
1436 void load_volumes(AFPObj *obj)
1438 struct passwd *pwent;
1443 /* check files date */
1444 if (obj->options.defaultvol.loaded) {
1445 changed = volfile_changed(&obj->options.defaultvol);
1447 if (obj->options.systemvol.loaded) {
1448 changed |= volfile_changed(&obj->options.systemvol);
1450 if (obj->options.uservol.loaded) {
1451 changed |= volfile_changed(&obj->options.uservol);
1460 pwent = getpwnam(obj->username);
1461 if ( (obj->options.flags & OPTION_USERVOLFIRST) == 0 ) {
1462 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent);
1465 if ((*obj->username == '\0') || (obj->options.flags & OPTION_NOUSERVOL)) {
1466 readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent);
1469 * Read user's AppleVolumes or .AppleVolumes file
1470 * If neither are readable, read the default volumes file. if
1471 * that doesn't work, create a user share.
1473 if (obj->options.uservol.name) {
1474 free(obj->options.uservol.name);
1476 obj->options.uservol.name = strdup(pwent->pw_dir);
1477 if ( readvolfile(obj, &obj->options.uservol, "AppleVolumes", 1, pwent) < 0 &&
1478 readvolfile(obj, &obj->options.uservol, ".AppleVolumes", 1, pwent) < 0 &&
1479 readvolfile(obj, &obj->options.uservol, "applevolumes", 1, pwent) < 0 &&
1480 readvolfile(obj, &obj->options.uservol, ".applevolumes", 1, pwent) < 0 &&
1481 obj->options.defaultvol.name != NULL ) {
1482 if (readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent) < 0)
1483 creatvol(obj, pwent, pwent->pw_dir, NULL, NULL, 1);
1486 if ( obj->options.flags & OPTION_USERVOLFIRST ) {
1487 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent );
1490 if ( obj->options.closevol ) {
1493 for ( vol = Volumes; vol; vol = vol->v_next ) {
1494 if (vol->v_deleted && !vol->v_new ) {
1503 /* ------------------------------- */
1504 int afp_getsrvrparms(obj, ibuf, ibuflen, rbuf, rbuflen )
1506 char *ibuf _U_, *rbuf;
1507 int ibuflen _U_, *rbuflen;
1520 for ( vcnt = 0, volume = Volumes; volume; volume = volume->v_next ) {
1521 if (!(volume->v_flags & AFPVOL_NOSTAT)) {
1524 if ( stat( volume->v_path, &st ) < 0 ) {
1525 LOG(log_info, logtype_afpd, "afp_getsrvrparms(%s): stat: %s",
1526 volume->v_path, strerror(errno) );
1527 continue; /* can't access directory */
1529 if (!S_ISDIR(st.st_mode)) {
1530 continue; /* not a dir */
1532 accessmode(volume->v_path, &ma, NULL, &st);
1533 if ((ma.ma_user & (AR_UREAD | AR_USEARCH)) != (AR_UREAD | AR_USEARCH)) {
1534 continue; /* no r-x access */
1537 if (volume->v_hide) {
1538 continue; /* config file changed but the volume was mounted */
1540 len = ucs2_to_charset_allocate((utf8_encoding()?CH_UTF8_MAC:obj->options.maccharset),
1541 &namebuf, volume->v_name);
1542 if (len == (size_t)-1)
1545 /* set password bit if there's a volume password */
1546 *data = (volume->v_password) ? AFPSRVR_PASSWD : 0;
1548 /* Apple 2 clients running ProDOS-8 expect one volume to have
1549 bit 0 of this byte set. They will not recognize anything
1550 on the server unless this is the case. I have not
1551 completely worked this out, but it's related to booting
1552 from the server. Support for that function is a ways
1553 off.. <shirsch@ibm.net> */
1554 *data |= (volume->v_flags & AFPVOL_A2VOL) ? AFPSRVR_CONFIGINFO : 0;
1555 *data++ |= 0; /* UNIX PRIVS BIT ..., OSX doesn't seem to use it, so we don't either */
1557 memcpy(data, namebuf, len );
1563 *rbuflen = data - rbuf;
1565 if ( gettimeofday( &tv, 0 ) < 0 ) {
1566 LOG(log_error, logtype_afpd, "afp_getsrvrparms(%s): gettimeofday: %s", volume->v_path, strerror(errno) );
1568 return AFPERR_PARAM;
1570 tv.tv_sec = AD_DATE_FROM_UNIX(tv.tv_sec);
1571 memcpy(data, &tv.tv_sec, sizeof( u_int32_t));
1572 data += sizeof( u_int32_t);
1577 /* -------------------------
1578 * we are the user here
1580 int afp_openvol(obj, ibuf, ibuflen, rbuf, rbuflen )
1583 int ibuflen _U_, *rbuflen;
1593 char path[ MAXPATHLEN + 1];
1598 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1599 bitmap = ntohs( bitmap );
1600 ibuf += sizeof( bitmap );
1601 if (( bitmap & (1<<VOLPBIT_VID)) == 0 ) {
1603 return AFPERR_BITMAP;
1606 len = (unsigned char)*ibuf++;
1607 volname = obj->oldtmp;
1608 namelen = convert_string( (utf8_encoding()?CH_UTF8_MAC:obj->options.maccharset), CH_UCS2,
1609 ibuf, len, volname, sizeof(obj->oldtmp));
1612 return AFPERR_PARAM;
1616 if ((len + 1) & 1) /* pad to an even boundary */
1621 for ( volume = Volumes; volume; volume = volume->v_next ) {
1622 if ( strcasecmp_w( (ucs2_t*) volname, volume->v_name ) == 0 ) {
1627 if ( volume == NULL ) {
1629 return AFPERR_PARAM;
1632 /* check for a volume password */
1633 if (volume->v_password && strncmp(ibuf, volume->v_password, VOLPASSLEN)) {
1635 return AFPERR_ACCESS;
1638 if (( volume->v_flags & AFPVOL_OPEN ) ) {
1639 /* the volume is already open */
1641 set_uidgid ( volume );
1643 return stat_vol(bitmap, volume, rbuf, rbuflen);
1646 /* initialize volume variables
1649 if (afp_version >= 30) {
1650 volume->max_filename = 255;
1653 volume->max_filename = MACFILELEN;
1656 volume->v_dir = volume->v_root = NULL;
1657 volume->v_hash = NULL;
1659 volume->v_flags |= AFPVOL_OPEN;
1660 volume->v_cdb = NULL;
1662 if (volume->v_root_preexec) {
1663 if ((ret = afprun(1, volume->v_root_preexec, NULL)) && volume->v_root_preexec_close) {
1664 LOG(log_error, logtype_afpd, "afp_openvol(%s): root preexec : %d", volume->v_path, ret );
1671 set_uidgid ( volume );
1674 if (volume->v_preexec) {
1675 if ((ret = afprun(0, volume->v_preexec, NULL)) && volume->v_preexec_close) {
1676 LOG(log_error, logtype_afpd, "afp_openvol(%s): preexec : %d", volume->v_path, ret );
1682 if ( stat( volume->v_path, &st ) < 0 ) {
1687 if ( chdir( volume->v_path ) < 0 ) {
1692 len = convert_string_allocate( CH_UCS2, (utf8_encoding()?CH_UTF8_MAC:obj->options.maccharset),
1693 volume->v_name, namelen, &vol_mname);
1694 if ( !vol_mname || len <= 0) {
1699 if ( NULL == getcwd(path, MAXPATHLEN)) {
1700 /* shouldn't be fatal but it will fail later */
1701 LOG(log_error, logtype_afpd, "afp_openvol(%s): volume pathlen too long", volume->v_path);
1706 if ((vol_uname = strrchr(path, '/')) == NULL)
1708 else if (*(vol_uname + 1) != '\0')
1711 if ((dir = dirnew(vol_mname, vol_uname) ) == NULL) {
1713 LOG(log_error, logtype_afpd, "afp_openvol(%s): malloc: %s", volume->v_path, strerror(errno) );
1719 dir->d_did = DIRDID_ROOT;
1720 dir->d_color = DIRTREE_COLOR_BLACK; /* root node is black */
1721 dir->d_m_name_ucs2 = strdup_w(volume->v_name);
1722 volume->v_dir = volume->v_root = dir;
1723 volume->v_hash = dirhash();
1725 curdir = volume->v_dir;
1726 if (volume->v_cnidscheme == NULL) {
1727 volume->v_cnidscheme = strdup(DEFAULT_CNID_SCHEME);
1728 LOG(log_warning, logtype_afpd, "Warning: No CNID scheme for volume %s. Using default.",
1731 if (volume->v_dbpath)
1732 volume->v_cdb = cnid_open (volume->v_dbpath, volume->v_umask, volume->v_cnidscheme, (volume->v_flags & AFPVOL_NODEV));
1734 volume->v_cdb = cnid_open (volume->v_path, volume->v_umask, volume->v_cnidscheme, (volume->v_flags & AFPVOL_NODEV));
1735 if (volume->v_cdb == NULL) {
1736 LOG(log_error, logtype_afpd, "Fatal error: cannot open CNID or invalid CNID backend for %s: %s",
1737 volume->v_path, volume->v_cnidscheme);
1744 if (!volume->v_volcodepage)
1745 volume->v_volcodepage = strdup("UTF8");
1747 if ( (charset_t) -1 == ( volume->v_volcharset = add_charset(volume->v_volcodepage)) ) {
1748 LOG (log_error, logtype_afpd, "Setting codepage %s as volume codepage failed", volume->v_volcodepage);
1753 if ( NULL == ( volume->v_vol = find_charset_functions(volume->v_volcodepage)) || volume->v_vol->flags & CHARSET_ICONV ) {
1754 LOG (log_warning, logtype_afpd, "WARNING: volume encoding %s is *not* supported by netatalk, expect problems !!!!", volume->v_volcodepage);
1757 if (!volume->v_maccodepage)
1758 volume->v_maccodepage = strdup(obj->options.maccodepage);
1760 if ( (charset_t) -1 == ( volume->v_maccharset = add_charset(volume->v_maccodepage)) ) {
1761 LOG (log_error, logtype_afpd, "Setting codepage %s as mac codepage failed", volume->v_maccodepage);
1766 if ( NULL == ( volume->v_mac = find_charset_functions(volume->v_maccodepage)) || ! (volume->v_mac->flags & CHARSET_CLIENT) ) {
1767 LOG (log_error, logtype_afpd, "Fatal error: mac charset %s not supported", volume->v_maccodepage);
1772 ret = stat_vol(bitmap, volume, rbuf, rbuflen);
1773 if (ret == AFP_OK) {
1775 if (!(volume->v_flags & AFPVOL_RO)) {
1776 handle_special_folders( volume );
1777 savevoloptions( volume);
1781 * If you mount a volume twice, the second time the trash appears on
1782 * the desk-top. That's because the Mac remembers the DID for the
1783 * trash (even for volumes in different zones, on different servers).
1784 * Just so this works better, we prime the DID cache with the trash,
1785 * fixing the trash at DID 17.
1786 * FIXME (RL): should it be done inside a CNID backend ? (always returning Trash DID when asked) ?
1788 if ((volume->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1790 /* FIXME find db time stamp */
1791 if (cnid_getstamp(volume->v_cdb, volume->v_stamp, sizeof(volume->v_stamp)) < 0) {
1792 LOG (log_error, logtype_afpd,
1793 "afp_openvol(%s): Fatal error: Unable to get stamp value from CNID backend",
1801 cname( volume, volume->v_dir, &p );
1807 if (volume->v_dir) {
1808 hash_free( volume->v_hash);
1809 dirfree( volume->v_dir );
1810 volume->v_dir = volume->v_root = NULL;
1813 volume->v_flags &= ~AFPVOL_OPEN;
1814 if (volume->v_cdb != NULL) {
1815 cnid_close(volume->v_cdb);
1816 volume->v_cdb = NULL;
1822 /* ------------------------- */
1823 static void closevol(struct vol *vol)
1828 hash_free( vol->v_hash);
1829 dirfree( vol->v_root );
1831 if (vol->v_cdb != NULL) {
1832 cnid_close(vol->v_cdb);
1836 if (vol->v_postexec) {
1837 afprun(0, vol->v_postexec, NULL);
1839 if (vol->v_root_postexec) {
1840 afprun(1, vol->v_root_postexec, NULL);
1844 /* ------------------------- */
1845 void close_all_vol(void)
1849 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
1850 if ( (ovol->v_flags & AFPVOL_OPEN) ) {
1851 ovol->v_flags &= ~AFPVOL_OPEN;
1857 /* ------------------------- */
1858 static void deletevol(struct vol *vol)
1862 vol->v_flags &= ~AFPVOL_OPEN;
1863 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
1864 if ( (ovol->v_flags & AFPVOL_OPEN) ) {
1868 if ( ovol != NULL ) {
1869 /* Even if chdir fails, we can't say afp_closevol fails. */
1870 if ( chdir( ovol->v_path ) == 0 ) {
1871 curdir = ovol->v_dir;
1876 if (vol->v_deleted) {
1877 showvol(vol->v_name);
1884 /* ------------------------- */
1885 int afp_closevol(obj, ibuf, ibuflen, rbuf, rbuflen )
1887 char *ibuf, *rbuf _U_;
1888 int ibuflen _U_, *rbuflen;
1895 memcpy(&vid, ibuf, sizeof( vid ));
1896 if (NULL == ( vol = getvolbyvid( vid )) ) {
1897 return( AFPERR_PARAM );
1905 /* ------------------------- */
1906 struct vol *getvolbyvid(const u_int16_t vid )
1910 for ( vol = Volumes; vol; vol = vol->v_next ) {
1911 if ( vid == vol->v_vid ) {
1915 if ( vol == NULL || ( vol->v_flags & AFPVOL_OPEN ) == 0 ) {
1921 #endif /* FORCE_UIDGID */
1926 /* ------------------------ */
1927 static int ext_cmp_key(const void *key, const void *obj)
1929 const char *p = key;
1930 const struct extmap *em = obj;
1931 return strdiacasecmp(p, em->em_ext);
1933 struct extmap *getextmap(const char *path)
1938 if (NULL == ( p = strrchr( path, '.' )) ) {
1939 return( Defextmap );
1942 if (!*p || !Extmap_cnt) {
1943 return( Defextmap );
1945 em = bsearch(p, Extmap, Extmap_cnt, sizeof(struct extmap), ext_cmp_key);
1949 return( Defextmap );
1953 /* ------------------------- */
1954 struct extmap *getdefextmap(void)
1956 return( Defextmap );
1959 /* --------------------------
1960 poll if a volume is changed by other processes.
1962 int pollvoltime(obj)
1969 if (!(afp_version > 21 && obj->options.server_notif))
1972 if ( gettimeofday( &tv, 0 ) < 0 )
1975 for ( vol = Volumes; vol; vol = vol->v_next ) {
1976 if ( (vol->v_flags & AFPVOL_OPEN) && vol->v_mtime + 30 < tv.tv_sec) {
1977 if ( !stat( vol->v_path, &st ) && vol->v_mtime != st.st_mtime ) {
1978 vol->v_mtime = st.st_mtime;
1979 if (!obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED))
1988 /* ------------------------- */
1989 void setvoltime(obj, vol )
1995 /* just looking at vol->v_mtime is broken seriously since updates
1996 * from other users afpd processes never are seen.
1997 * This is not the most elegant solution (a shared memory between
1998 * the afpd processes would come closer)
2001 if ( gettimeofday( &tv, 0 ) < 0 ) {
2002 LOG(log_error, logtype_afpd, "setvoltime(%s): gettimeofday: %s", vol->v_path, strerror(errno) );
2005 if( utime( vol->v_path, NULL ) < 0 ) {
2006 /* write of time failed ... probably a read only filesys,
2007 * where no other users can interfere, so there's no issue here
2011 /* a little granularity */
2012 if (vol->v_mtime < tv.tv_sec) {
2013 vol->v_mtime = tv.tv_sec;
2014 /* or finder doesn't update free space */
2015 if (afp_version > 21 && obj->options.server_notif) {
2016 obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED);
2021 /* ------------------------- */
2022 int afp_getvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
2025 int ibuflen _U_, *rbuflen;
2028 u_int16_t vid, bitmap;
2031 memcpy(&vid, ibuf, sizeof( vid ));
2032 ibuf += sizeof( vid );
2033 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2034 bitmap = ntohs( bitmap );
2036 if (NULL == ( vol = getvolbyvid( vid )) ) {
2038 return( AFPERR_PARAM );
2041 return stat_vol(bitmap, vol, rbuf, rbuflen);
2044 /* ------------------------- */
2045 int afp_setvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
2047 char *ibuf, *rbuf _U_;
2048 int ibuflen _U_, *rbuflen;
2052 u_int16_t vid, bitmap;
2058 memcpy(&vid, ibuf, sizeof( vid ));
2059 ibuf += sizeof( vid );
2060 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2061 bitmap = ntohs( bitmap );
2062 ibuf += sizeof(bitmap);
2064 if (( vol = getvolbyvid( vid )) == NULL ) {
2065 return( AFPERR_PARAM );
2068 if ((vol->v_flags & AFPVOL_RO))
2069 return AFPERR_VLOCK;
2071 /* we can only set the backup date. */
2072 if (bitmap != (1 << VOLPBIT_BDATE))
2073 return AFPERR_BITMAP;
2075 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2076 if ( ad_open( vol->v_path, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR,
2079 return AFPERR_VLOCK;
2081 return AFPERR_ACCESS;
2084 memcpy(&aint, ibuf, sizeof(aint));
2085 ad_setdate(&ad, AD_DATE_BACKUP, aint);
2086 ad_flush(&ad, ADFLAGS_HF);
2087 ad_close(&ad, ADFLAGS_HF);
2091 /* ------------------------- */
2092 int wincheck(const struct vol *vol, const char *path)
2096 if (!(vol->v_flags & AFPVOL_MSWINDOWS))
2099 /* empty paths are not allowed */
2100 if ((len = strlen(path)) == 0)
2103 /* leading or trailing whitespaces are not allowed, carriage returns
2104 * and probably other whitespace is okay, tabs are not allowed
2106 if ((path[0] == ' ') || (path[len-1] == ' '))
2109 /* certain characters are not allowed */
2110 if (strpbrk(path, MSWINDOWS_BADCHARS))
2113 /* everything else is okay */
2119 * precreate a folder
2120 * this is only intended for folders in the volume root
2121 * It will *not* work if the folder name contains extended characters
2123 static int create_special_folder (const struct vol *vol, const struct _special_folder *folder)
2132 p = (char *) malloc ( strlen(vol->v_path)+strlen(folder->name)+2);
2134 LOG(log_error, logtype_afpd,"malloc failed");
2138 q=strdup(folder->name);
2140 LOG(log_error, logtype_afpd,"malloc failed");
2144 strcpy(p, vol->v_path);
2149 if ((vol->v_casefold & AFPVOL_MTOUUPPER))
2151 else if ((vol->v_casefold & AFPVOL_MTOULOWER))
2157 if ( (ret = stat( p, &st )) < 0 ) {
2158 if (folder->precreate) {
2159 if (ad_mkdir(p, folder->mode)) {
2160 LOG(log_debug, logtype_afpd,"Creating '%s' failed in %s: %s", p, vol->v_path, strerror(errno));
2169 if ( !ret && folder->hide) {
2171 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2172 if (ad_open( p, vol_noadouble(vol) | ADFLAGS_HF|ADFLAGS_DIR,
2173 O_RDWR|O_CREAT, 0666, &ad) < 0) {
2178 if ((ad_get_HF_flags( &ad ) & O_CREAT) ) {
2179 if (ad_getentryoff(&ad, ADEID_NAME)) {
2180 ad_setentrylen( &ad, ADEID_NAME, strlen(folder->name));
2181 memcpy(ad_entry( &ad, ADEID_NAME ), folder->name,
2182 ad_getentrylen( &ad, ADEID_NAME ));
2186 ad_getattr(&ad, &attr);
2187 attr |= htons( ntohs( attr ) | ATTRBIT_INVISIBLE );
2188 ad_setattr(&ad, attr);
2190 /* do the same with the finder info */
2191 if (ad_entry(&ad, ADEID_FINDERI)) {
2192 memcpy(&attr, ad_entry(&ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF, sizeof(attr));
2193 attr |= htons(FINDERINFO_INVISIBLE);
2194 memcpy(ad_entry(&ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF,&attr, sizeof(attr));
2197 ad_flush( &ad, ADFLAGS_HF );
2198 ad_close( &ad, ADFLAGS_HF );
2205 static void handle_special_folders (const struct vol * vol)
2207 const _special_folder *p = &special_folders[0];
2209 if ((vol->v_flags & AFPVOL_RO))
2212 for (; p->name != NULL; p++) {
2213 create_special_folder (vol, p);
2218 * Save the volume options to a file, used by
2220 * Writing the file everytime a volume is opened is
2221 * unnecessary, but it shouldn't hurt much.
2223 static int savevoloptions (const struct vol *vol)
2226 char item[MAXPATHLEN];
2230 const _vol_opt_name *op = &vol_opt_names[0];
2231 const _vol_opt_name *cf = &vol_opt_casefold[0];
2233 strlcpy (item, vol->v_path, sizeof(item));
2234 strlcat (item, "/.AppleDesktop/", sizeof(item));
2235 strlcat (item, VOLINFOFILE, sizeof(item));
2237 if ((fd = open( item, O_RDWR | O_CREAT , 0666)) <0 ) {
2238 LOG(log_debug, logtype_afpd,"Error opening %s: %s", item, strerror(errno));
2242 /* try to get a lock */
2244 lock.l_whence = SEEK_SET;
2246 lock.l_type = F_WRLCK;
2248 if (fcntl(fd, F_SETLK, &lock) < 0) {
2249 if (errno == EACCES || errno == EAGAIN) {
2250 /* ignore, other process already writing the file */
2253 LOG(log_error, logtype_cnid, "savevoloptions: cannot get lock: %s", strerror(errno));
2258 /* write volume options */
2259 snprintf(buf, sizeof(buf), "MAC_CHARSET:%s\n", vol->v_maccodepage);
2260 snprintf(item, sizeof(item), "VOL_CHARSET:%s\n", vol->v_volcodepage);
2261 strlcat(buf, item, sizeof(buf));
2263 switch (vol->v_adouble) {
2265 strlcat(buf, "ADOUBLE_VER:v1\n", sizeof(buf));
2268 strlcat(buf, "ADOUBLE_VER:v2\n", sizeof(buf));
2270 case AD_VERSION2_OSX:
2271 strlcat(buf, "ADOUBLE_VER:osx\n", sizeof(buf));
2273 case AD_VERSION1_ADS:
2274 strlcat(buf, "ADOUBLE_VER:ads\n", sizeof(buf));
2278 strlcat(buf, "CNIDBACKEND:", sizeof(buf));
2279 strlcat(buf, vol->v_cnidscheme, sizeof(buf));
2280 strlcat(buf, "\n", sizeof(buf));
2282 strlcat(buf, "CNIDDBDHOST:", sizeof(buf));
2283 strlcat(buf, Cnid_srv, sizeof(buf));
2284 strlcat(buf, "\n", sizeof(buf));
2286 snprintf(item, sizeof(item), "CNIDDBDPORT:%u\n", Cnid_port);
2287 strlcat(buf, item, sizeof(buf));
2289 strcpy(item, "CNID_DBPATH:");
2291 strlcat(item, vol->v_dbpath, sizeof(item));
2293 strlcat(item, vol->v_path, sizeof(item));
2294 strlcat(item, "\n", sizeof(item));
2295 strlcat(buf, item, sizeof(buf));
2298 strcpy(item, "VOLUME_OPTS:");
2299 for (;op->name; op++) {
2300 if ( (vol->v_flags & op->option) ) {
2301 strlcat(item, op->name, sizeof(item));
2302 strlcat(item, " ", sizeof(item));
2305 strlcat(item, "\n", sizeof(item));
2306 strlcat(buf, item, sizeof(buf));
2308 /* casefold flags */
2309 strcpy(item, "VOLCASEFOLD:");
2310 for (;cf->name; cf++) {
2311 if ( (vol->v_casefold & cf->option) ) {
2312 strlcat(item, cf->name, sizeof(item));
2313 strlcat(item, " ", sizeof(item));
2316 strlcat(item, "\n", sizeof(item));
2317 strlcat(buf, item, sizeof(buf));
2319 if (strlen(buf) >= sizeof(buf)-1)
2320 LOG(log_debug, logtype_afpd,"Error writing .volinfo file: buffer too small, %s", buf);
2323 if (write( fd, buf, strlen(buf)) < 0) {
2324 LOG(log_debug, logtype_afpd,"Error writing .volinfo file: %s", strerror(errno));
2327 ftruncate(fd, strlen(buf));
2330 lock.l_type = F_UNLCK;
2331 fcntl(fd, F_SETLK, &lock);