2 * $Id: volume.c,v 1.73 2008-12-03 18:35:44 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_EILSEQ, "ILLEGALSEQ"}, /* encode illegal sequence */
176 {AFPVOL_CACHE, "CACHEID"}, /* Use adouble v2 CNID caching, default don't use it */
180 static const _vol_opt_name vol_opt_casefold[] = {
181 {AFPVOL_MTOUUPPER, "MTOULOWER"},
182 {AFPVOL_MTOULOWER, "MTOULOWER"},
183 {AFPVOL_UTOMUPPER, "UTOMUPPER"},
184 {AFPVOL_UTOMLOWER, "UTOMLOWER"},
188 static void handle_special_folders (const struct vol *);
189 static int savevoloptions (const struct vol *);
190 static void deletevol(struct vol *vol);
192 static void volfree(struct vol_option *options,
193 const struct vol_option *save)
198 for (i = 0; i < VOLOPT_MAX; i++) {
199 if (options[i].c_value && (options[i].c_value != save[i].c_value))
200 free(options[i].c_value);
203 for (i = 0; i < VOLOPT_MAX; i++) {
204 if (options[i].c_value)
205 free(options[i].c_value);
211 /* handle variable substitutions. here's what we understand:
212 * $b -> basename of path
213 * $c -> client ip/appletalk address
214 * $d -> volume pathname on server
215 * $f -> full name (whatever's in the gecos field)
218 * $i -> client ip/appletalk address without port
219 * $s -> server name (hostname if it doesn't exist)
220 * $u -> username (guest is usually nobody)
221 * $v -> volume name or basename if null
222 * $z -> zone (may not exist)
227 #define is_var(a, b) (strncmp((a), (b), 2) == 0)
229 static char *volxlate(AFPObj *obj, char *dest, size_t destlen,
230 char *src, struct passwd *pwd, char *path, char *volname)
240 dest = calloc(destlen +1, 1);
246 strlcpy(dest, src, destlen +1);
247 if ((p = strchr(src, '$')) == NULL) /* nothing to do */
250 /* first part of the path. just forward to the next variable. */
251 len = MIN((size_t)(p - src), destlen);
257 while (p && destlen > 0) {
258 /* now figure out what the variable is */
260 if (is_var(p, "$b")) {
262 if ((q = strrchr(path, '/')) == NULL)
264 else if (*(q + 1) != '\0')
267 } else if (is_var(p, "$c")) {
268 if (obj->proto == AFPPROTO_ASP) {
269 ASP asp = obj->handle;
271 len = sprintf(dest, "%u.%u", ntohs(asp->asp_sat.sat_addr.s_net),
272 asp->asp_sat.sat_addr.s_node);
276 } else if (obj->proto == AFPPROTO_DSI) {
277 DSI *dsi = obj->handle;
279 len = sprintf(dest, "%s:%u", inet_ntoa(dsi->client.sin_addr),
280 ntohs(dsi->client.sin_port));
284 } else if (is_var(p, "$d")) {
286 } else if (is_var(p, "$f")) {
287 if ((q = strchr(pwd->pw_gecos, ',')))
290 } else if (is_var(p, "$g")) {
291 struct group *grp = getgrgid(pwd->pw_gid);
294 } else if (is_var(p, "$h")) {
295 q = obj->options.hostname;
296 } else if (is_var(p, "$i")) {
297 if (obj->proto == AFPPROTO_ASP) {
298 ASP asp = obj->handle;
300 len = sprintf(dest, "%u", ntohs(asp->asp_sat.sat_addr.s_net));
304 } else if (obj->proto == AFPPROTO_DSI) {
305 DSI *dsi = obj->handle;
307 q = inet_ntoa(dsi->client.sin_addr);
309 } else if (is_var(p, "$s")) {
312 else if (obj->options.server) {
313 q = obj->options.server;
315 q = obj->options.hostname;
316 } else if (is_var(p, "$u")) {
318 if ( obj->options.ntseparator && (sep = strchr(obj->username, obj->options.ntseparator[0])) != NULL)
322 } else if (is_var(p, "$v")) {
327 if ((q = strrchr(path, '/')) == NULL)
329 else if (*(q + 1) != '\0')
332 } else if (is_var(p, "$z")) {
334 } else if (is_var(p, "$$")) {
339 /* copy the stuff over. if we don't understand something that we
340 * should, just skip it over. */
342 len = MIN(p == q ? 2 : strlen(q), destlen);
343 strncpy(dest, q, len);
348 /* stuff up to next $ */
350 p = strchr(src, '$');
351 len = p ? MIN((size_t)(p - src), destlen) : destlen;
353 strncpy(dest, src, len);
361 /* to make sure that val is valid, make sure to select an opt that
363 static int optionok(const char *buf, const char *opt, const char *val)
365 if (!strstr(buf,opt))
373 /* -------------------- */
374 static void setoption(struct vol_option *options, struct vol_option *save, int opt, const char *val)
376 if (options[opt].c_value && (!save || options[opt].c_value != save[opt].c_value))
377 free(options[opt].c_value);
378 options[opt].c_value = strdup(val + 1);
381 /* ------------------------------------------
382 handle all the options. tmp can't be NULL. */
383 static void volset(struct vol_option *options, struct vol_option *save,
384 char *volname, int vlen,
389 val = strchr(tmp, ':');
391 /* we'll assume it's a volume name. */
392 strncpy(volname, tmp, vlen);
397 LOG(log_debug, logtype_afpd, "Parsing volset %s", val);
399 if (optionok(tmp, "allow:", val)) {
400 setoption(options, save, VOLOPT_ALLOW, val);
402 } else if (optionok(tmp, "deny:", val)) {
403 setoption(options, save, VOLOPT_DENY, val);
405 } else if (optionok(tmp, "rwlist:", val)) {
406 setoption(options, save, VOLOPT_RWLIST, val);
408 } else if (optionok(tmp, "rolist:", val)) {
409 setoption(options, save, VOLOPT_ROLIST, val);
411 } else if (optionok(tmp, "codepage:", val)) {
412 LOG (log_error, logtype_afpd, "The old codepage system has been removed. Please make sure to read the documentation !!!!");
413 /* Make sure we don't screw anything */
415 } else if (optionok(tmp, "volcharset:", val)) {
416 setoption(options, save, VOLOPT_ENCODING, val);
417 } else if (optionok(tmp, "maccharset:", val)) {
418 setoption(options, save, VOLOPT_MACCHARSET, val);
419 } else if (optionok(tmp, "veto:", val)) {
420 setoption(options, save, VOLOPT_VETO, val);
421 } else if (optionok(tmp, "cnidscheme:", val)) {
422 setoption(options, save, VOLOPT_CNIDSCHEME, val);
423 } else if (optionok(tmp, "casefold:", val)) {
424 if (strcasecmp(val + 1, "tolower") == 0)
425 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMLOWER;
426 else if (strcasecmp(val + 1, "toupper") == 0)
427 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UMUPPER;
428 else if (strcasecmp(val + 1, "xlatelower") == 0)
429 options[VOLOPT_CASEFOLD].i_value = AFPVOL_UUPPERMLOWER;
430 else if (strcasecmp(val + 1, "xlateupper") == 0)
431 options[VOLOPT_CASEFOLD].i_value = AFPVOL_ULOWERMUPPER;
432 } else if (optionok(tmp, "adouble:", val)) {
433 if (strcasecmp(val + 1, "v1") == 0)
434 options[VOLOPT_ADOUBLE].i_value = AD_VERSION1;
435 #if AD_VERSION == AD_VERSION2
436 else if (strcasecmp(val + 1, "v2") == 0)
437 options[VOLOPT_ADOUBLE].i_value = AD_VERSION2;
438 else if (strcasecmp(val + 1, "osx") == 0)
439 options[VOLOPT_ADOUBLE].i_value = AD_VERSION2_OSX;
440 else if (strcasecmp(val + 1, "ads") == 0)
441 options[VOLOPT_ADOUBLE].i_value = AD_VERSION1_ADS;
442 else if (strcasecmp(val + 1, "sfm") == 0)
443 options[VOLOPT_ADOUBLE].i_value = AD_VERSION1_SFM;
445 } else if (optionok(tmp, "options:", val)) {
448 if ((p = strtok(val + 1, ",")) == NULL) /* nothing */
452 if (strcasecmp(p, "prodos") == 0)
453 options[VOLOPT_FLAGS].i_value |= AFPVOL_A2VOL;
454 else if (strcasecmp(p, "mswindows") == 0) {
455 options[VOLOPT_FLAGS].i_value |= AFPVOL_MSWINDOWS | AFPVOL_USEDOTS;
456 } else if (strcasecmp(p, "crlf") == 0)
457 options[VOLOPT_FLAGS].i_value |= AFPVOL_CRLF;
458 else if (strcasecmp(p, "noadouble") == 0)
459 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOADOUBLE;
460 else if (strcasecmp(p, "ro") == 0)
461 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
462 else if (strcasecmp(p, "nohex") == 0)
463 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOHEX;
464 else if (strcasecmp(p, "usedots") == 0)
465 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS;
466 else if (strcasecmp(p, "invisibledots") == 0)
467 options[VOLOPT_FLAGS].i_value |= AFPVOL_USEDOTS | AFPVOL_INV_DOTS;
468 else if (strcasecmp(p, "limitsize") == 0)
469 options[VOLOPT_FLAGS].i_value |= AFPVOL_LIMITSIZE;
470 /* support for either "dropbox" or "dropkludge" */
471 else if (strcasecmp(p, "dropbox") == 0)
472 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
473 else if (strcasecmp(p, "dropkludge") == 0)
474 options[VOLOPT_FLAGS].i_value |= AFPVOL_DROPBOX;
475 else if (strcasecmp(p, "nofileid") == 0)
476 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOFILEID;
477 else if (strcasecmp(p, "nostat") == 0)
478 options[VOLOPT_FLAGS].i_value |= AFPVOL_NOSTAT;
479 else if (strcasecmp(p, "preexec_close") == 0)
480 options[VOLOPT_PREEXEC].i_value = 1;
481 else if (strcasecmp(p, "root_preexec_close") == 0)
482 options[VOLOPT_ROOTPREEXEC].i_value = 1;
483 else if (strcasecmp(p, "upriv") == 0)
484 options[VOLOPT_FLAGS].i_value |= AFPVOL_UNIX_PRIV;
485 else if (strcasecmp(p, "nodev") == 0)
486 options[VOLOPT_FLAGS].i_value |= AFPVOL_NODEV;
487 else if (strcasecmp(p, "illegalseq") == 0)
488 options[VOLOPT_FLAGS].i_value |= AFPVOL_EILSEQ;
489 else if (strcasecmp(p, "cachecnid") == 0)
490 options[VOLOPT_FLAGS].i_value |= AFPVOL_CACHE;
492 p = strtok(NULL, ",");
495 } else if (optionok(tmp, "dbpath:", val)) {
496 setoption(options, save, VOLOPT_DBPATH, val);
498 } else if (optionok(tmp, "umask:", val)) {
499 options[VOLOPT_UMASK].i_value = (int)strtol(val +1, NULL, 8);
500 } else if (optionok(tmp, "perm:", val)) {
501 options[VOLOPT_DFLTPERM].i_value = (int)strtol(val+1, NULL, 8);
502 } else if (optionok(tmp, "mapchars:",val)) {
503 setoption(options, save, VOLOPT_MAPCHARS, val);
505 } else if (optionok(tmp, "password:", val)) {
506 setoption(options, save, VOLOPT_PASSWORD, val);
510 /* this code allows forced uid/gid per volume settings */
511 } else if (optionok(tmp, "forceuid:", val)) {
512 setoption(options, save, VOLOPT_FORCEUID, val);
513 } else if (optionok(tmp, "forcegid:", val)) {
514 setoption(options, save, VOLOPT_FORCEGID, val);
516 #endif /* FORCE_UIDGID */
517 } else if (optionok(tmp, "root_preexec:", val)) {
518 setoption(options, save, VOLOPT_ROOTPREEXEC, val);
520 } else if (optionok(tmp, "preexec:", val)) {
521 setoption(options, save, VOLOPT_PREEXEC, val);
523 } else if (optionok(tmp, "root_postexec:", val)) {
524 setoption(options, save, VOLOPT_ROOTPOSTEXEC, val);
526 } else if (optionok(tmp, "postexec:", val)) {
527 setoption(options, save, VOLOPT_POSTEXEC, val);
530 /* ignore unknown options */
531 LOG(log_debug, logtype_afpd, "ignoring unknown volume option: %s", tmp);
536 /* ----------------- */
537 static void showvol(const ucs2_t *name)
540 for ( volume = Volumes; volume; volume = volume->v_next ) {
541 if (volume->v_hide && !strcasecmp_w( volume->v_name, name ) ) {
548 /* ------------------------------- */
549 static int creatvol(AFPObj *obj, struct passwd *pwd,
550 char *path, char *name,
551 struct vol_option *options,
552 const int user /* user defined volume */
560 if ( name == NULL || *name == '\0' ) {
561 if ((name = strrchr( path, '/' )) == NULL) {
562 return -1; /* Obviously not a fully qualified path */
565 /* if you wish to share /, you need to specify a name. */
570 vlen = strlen( name );
571 if ( vlen > AFPVOL_NAMELEN ) {
572 vlen = AFPVOL_NAMELEN;
573 name[AFPVOL_NAMELEN] = '\0';
576 /* convert name to UCS2 first */
577 if ( 0 >= ( vlen = convert_string(obj->options.unixcharset, CH_UCS2, name, vlen, tmpname, 512)) )
580 for ( volume = Volumes; volume; volume = volume->v_next ) {
581 if ( strcasecmp_w( volume->v_name, tmpname ) == 0 ) {
582 if (volume->v_deleted) {
583 volume->v_new = hide = 1;
586 return -1; /* Won't be able to access it, anyway... */
592 if (!( volume = (struct vol *)calloc(1, sizeof( struct vol ))) ) {
593 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
596 if ( NULL == ( volume->v_name = strdup_w(tmpname))) {
597 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
601 if (!( volume->v_path = (char *)malloc( strlen( path ) + 1 )) ) {
602 LOG(log_error, logtype_afpd, "creatvol: malloc: %s", strerror(errno) );
603 free(volume->v_name);
607 volume->v_hide = hide;
608 strcpy( volume->v_path, path );
612 #endif /* __svr4__ */
613 /* os X start at 1 and use network order ie. 1 2 3 */
614 volume->v_vid = ++lastvid;
615 volume->v_vid = htons(volume->v_vid);
619 /* should we casefold? */
620 volume->v_casefold = options[VOLOPT_CASEFOLD].i_value;
622 /* shift in some flags */
623 volume->v_flags = options[VOLOPT_FLAGS].i_value;
625 volume->v_ad_options = 0;
626 if ((volume->v_flags & AFPVOL_NODEV))
627 volume->v_ad_options |= ADVOL_NODEV;
628 if ((volume->v_flags & AFPVOL_CACHE))
629 volume->v_ad_options |= ADVOL_CACHE;
630 if ((volume->v_flags & AFPVOL_UNIX_PRIV))
631 volume->v_ad_options |= ADVOL_UNIXPRIV;
632 if ((volume->v_flags & AFPVOL_INV_DOTS))
633 volume->v_ad_options |= ADVOL_INVDOTS;
635 if (options[VOLOPT_PASSWORD].c_value)
636 volume->v_password = strdup(options[VOLOPT_PASSWORD].c_value);
638 if (options[VOLOPT_VETO].c_value)
639 volume->v_veto = strdup(options[VOLOPT_VETO].c_value);
641 if (options[VOLOPT_ENCODING].c_value)
642 volume->v_volcodepage = strdup(options[VOLOPT_ENCODING].c_value);
644 if (options[VOLOPT_MACCHARSET].c_value)
645 volume->v_maccodepage = strdup(options[VOLOPT_MACCHARSET].c_value);
647 if (options[VOLOPT_DBPATH].c_value)
648 volume->v_dbpath = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_DBPATH].c_value, pwd, path, name);
650 if (options[VOLOPT_CNIDSCHEME].c_value)
651 volume->v_cnidscheme = strdup(options[VOLOPT_CNIDSCHEME].c_value);
653 if (options[VOLOPT_UMASK].i_value)
654 volume->v_umask = (mode_t)options[VOLOPT_UMASK].i_value;
656 if (options[VOLOPT_DFLTPERM].i_value)
657 volume->v_perm = (mode_t)options[VOLOPT_DFLTPERM].i_value;
659 if (options[VOLOPT_ADOUBLE].i_value)
660 volume->v_adouble = options[VOLOPT_ADOUBLE].i_value;
662 volume->v_adouble = AD_VERSION;
666 if (options[VOLOPT_FORCEUID].c_value) {
667 volume->v_forceuid = strdup(options[VOLOPT_FORCEUID].c_value);
669 volume->v_forceuid = NULL; /* set as null so as to return 0 later on */
672 if (options[VOLOPT_FORCEGID].c_value) {
673 volume->v_forcegid = strdup(options[VOLOPT_FORCEGID].c_value);
675 volume->v_forcegid = NULL; /* set as null so as to return 0 later on */
679 if (options[VOLOPT_PREEXEC].c_value)
680 volume->v_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_PREEXEC].c_value, pwd, path, name);
681 volume->v_preexec_close = options[VOLOPT_PREEXEC].i_value;
683 if (options[VOLOPT_POSTEXEC].c_value)
684 volume->v_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_POSTEXEC].c_value, pwd, path, name);
686 if (options[VOLOPT_ROOTPREEXEC].c_value)
687 volume->v_root_preexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPREEXEC].c_value, pwd, path, name);
688 volume->v_root_preexec_close = options[VOLOPT_ROOTPREEXEC].i_value;
690 if (options[VOLOPT_ROOTPOSTEXEC].c_value)
691 volume->v_root_postexec = volxlate(obj, NULL, MAXPATHLEN, options[VOLOPT_ROOTPOSTEXEC].c_value, pwd, path, name);
695 volume->v_next = Volumes;
700 /* ---------------- */
701 static char *myfgets( buf, size, fp )
710 while ((EOF != ( c = getc( fp )) ) && ( size > 0 )) {
711 if ( c == '\n' || c == '\r' ) {
729 /* check access list. this function wants something of the following
731 * @group,name,name2,@group2,name3
733 * a NULL argument allows everybody to have access.
734 * we return three things:
736 * 0: list exists, but name isn't in it
740 #ifndef NO_REAL_USER_NAME
741 /* authentication is case insensitive
742 * FIXME should we do the same with group name?
744 #define access_strcmp strcasecmp
747 #define access_strcmp strcmp
751 static int accessvol(args, name)
755 char buf[MAXPATHLEN + 1], *p;
761 strlcpy(buf, args, sizeof(buf));
762 if ((p = strtok(buf, ",")) == NULL) /* nothing, return okay */
766 if (*p == '@') { /* it's a group */
767 if ((gr = getgrnam(p + 1)) && gmem(gr->gr_gid))
769 } else if (access_strcmp(p, name) == 0) /* it's a user name */
771 p = strtok(NULL, ",");
777 static void setextmap( ext, type, creator, user)
778 char *ext, *type, *creator;
784 if (Extmap == NULL) {
785 if (( Extmap = calloc(1, sizeof( struct extmap ))) == NULL ) {
786 LOG(log_error, logtype_afpd, "setextmap: calloc: %s", strerror(errno) );
791 for ( em = Extmap, cnt = 0; em->em_ext; em++, cnt++) {
792 if ( (strdiacasecmp( em->em_ext, ext )) == 0 ) {
797 if ( em->em_ext == NULL ) {
798 if (!(Extmap = realloc( Extmap, sizeof( struct extmap ) * (cnt +2))) ) {
799 LOG(log_error, logtype_afpd, "setextmap: realloc: %s", strerror(errno) );
802 (Extmap +cnt +1)->em_ext = NULL;
804 } else if ( !user ) {
810 if (!(em->em_ext = strdup( ext))) {
811 LOG(log_error, logtype_afpd, "setextmap: strdup: %s", strerror(errno) );
815 if ( *type == '\0' ) {
816 memcpy(em->em_type, "????", sizeof( em->em_type ));
818 memcpy(em->em_type, type, sizeof( em->em_type ));
820 if ( *creator == '\0' ) {
821 memcpy(em->em_creator, "UNIX", sizeof( em->em_creator ));
823 memcpy(em->em_creator, creator, sizeof( em->em_creator ));
827 /* -------------------------- */
828 static int extmap_cmp(const void *map1, const void *map2)
830 const struct extmap *em1 = map1;
831 const struct extmap *em2 = map2;
832 return strdiacasecmp(em1->em_ext, em2->em_ext);
835 static void sortextmap( void)
840 if ((em = Extmap) == NULL) {
848 qsort(Extmap, Extmap_cnt, sizeof(struct extmap), extmap_cmp);
849 if (*Extmap->em_ext == 0) {
850 /* the first line is really "." the default entry,
851 * we remove the leading '.' in setextmap
858 /* ----------------------
860 static void free_extmap( void)
865 for ( em = Extmap; em->em_ext; em++) {
875 /* ----------------------
877 static int volfile_changed(struct afp_volume_name *p)
887 if (!stat( name, &st) && st.st_mtime > p->mtime) {
888 p->mtime = st.st_mtime;
894 /* ----------------------
895 * Read a volume configuration file and add the volumes contained within to
896 * the global volume list. If p2 is non-NULL, the file that is opened is
899 * Lines that begin with # and blank lines are ignored.
900 * Volume lines are of the form:
901 * <unix path> [<volume name>] [allow:<user>,<@group>,...] \
902 * [codepage:<file>] [casefold:<num>]
903 * <extension> TYPE [CREATOR]
905 static int readvolfile(obj, p1, p2, user, pwent)
907 struct afp_volume_name *p1;
910 struct passwd *pwent;
913 char path[ MAXPATHLEN + 1], tmp[ MAXPATHLEN + 1],
914 volname[ AFPVOL_NAMELEN + 1 ], buf[ BUFSIZ ],
915 type[ 5 ], creator[ 5 ];
918 struct vol_option options[VOLOPT_NUM], save_options[VOLOPT_NUM];
926 strcpy( path, p1->name );
933 p1->full_name = strdup(path);
936 if (NULL == ( fp = fopen( path, "r" )) ) {
940 if (fd != -1 && !fstat( fd, &st) ) {
941 p1->mtime = st.st_mtime;
944 memset(save_options, 0, sizeof(save_options));
945 while ( myfgets( buf, sizeof( buf ), fp ) != NULL ) {
946 initline( strlen( buf ), buf );
947 parseline( sizeof( path ) - 1, path );
954 /* change the default options for this file */
955 if (strncmp(path, VOLOPT_DEFAULT, VOLOPT_DEFAULT_LEN) == 0) {
957 for (i = 0; i < VOLOPT_NUM; i++) {
958 if (parseline( sizeof( path ) - VOLOPT_DEFAULT_LEN - 1,
959 path + VOLOPT_DEFAULT_LEN) < 0)
961 volset(save_options, NULL, tmp, sizeof(tmp) - 1,
962 path + VOLOPT_DEFAULT_LEN);
968 if (( p = strchr( path, '/' )) != NULL ) {
976 if ( u == NULL || *u == '\0' || ( pw = getpwnam( u )) == NULL ) {
979 strcpy( tmp, pw->pw_dir );
980 if ( p != NULL && *p != '\0' ) {
984 /* Tag a user's home directory with their umask. Note, this will
985 * be overwritten if the user actually specifies a umask: option
986 * for a '~' volume. */
987 save_options[VOLOPT_UMASK].i_value = obj->options.save_mask;
991 /* send path through variable substitution */
992 if (*path != '~') /* need to copy path to tmp */
995 pwent = getpwnam(obj->username);
996 volxlate(obj, path, sizeof(path) - 1, tmp, pwent, NULL, NULL);
998 /* this is sort of braindead. basically, i want to be
999 * able to specify things in any order, but i don't want to
1000 * re-write everything.
1002 * currently we have options:
1010 * options:prodos,crlf,noadouble,ro...
1015 * namemask:x,y,!z (not implemented yet)
1017 memcpy(options, save_options, sizeof(options));
1020 /* read in up to VOLOP_NUM possible options */
1021 for (i = 0; i < VOLOPT_NUM; i++) {
1022 if (parseline( sizeof( tmp ) - 1, tmp ) < 0)
1025 volset(options, save_options, volname, sizeof(volname) - 1, tmp);
1028 /* check allow/deny lists:
1029 allow -> either no list (-1), or in list (1)
1030 deny -> either no list (-1), or not in list (0) */
1031 if (accessvol(options[VOLOPT_ALLOW].c_value, obj->username) &&
1032 (accessvol(options[VOLOPT_DENY].c_value, obj->username) < 1)) {
1034 /* handle read-only behaviour. semantics:
1035 * 1) neither the rolist nor the rwlist exist -> rw
1036 * 2) rolist exists -> ro if user is in it.
1037 * 3) rwlist exists -> ro unless user is in it. */
1038 if (((options[VOLOPT_FLAGS].i_value & AFPVOL_RO) == 0) &&
1039 ((accessvol(options[VOLOPT_ROLIST].c_value,
1040 obj->username) == 1) ||
1041 !accessvol(options[VOLOPT_RWLIST].c_value,
1043 options[VOLOPT_FLAGS].i_value |= AFPVOL_RO;
1045 /* do variable substitution for volname */
1046 volxlate(obj, tmp, sizeof(tmp) - 1, volname, pwent, path, NULL);
1047 creatvol(obj, pwent, path, tmp, options, p2 != NULL);
1049 volfree(options, save_options);
1053 parseline( sizeof( type ) - 1, type );
1054 parseline( sizeof( creator ) - 1, creator );
1055 setextmap( path, type, creator, user);
1062 volfree(save_options, NULL);
1064 if ( fclose( fp ) != 0 ) {
1065 LOG(log_error, logtype_afpd, "readvolfile: fclose: %s", strerror(errno) );
1071 /* ------------------------------- */
1072 static void volume_free(struct vol *vol)
1077 free(vol->v_password);
1079 free(vol->v_volcodepage);
1080 free(vol->v_maccodepage);
1081 free(vol->v_cnidscheme);
1082 free(vol->v_dbpath);
1085 free(vol->v_forceuid);
1086 free(vol->v_forcegid);
1087 #endif /* FORCE_UIDGID */
1090 /* ------------------------------- */
1091 static void free_volumes(void )
1094 struct vol *nvol, *ovol;
1096 for ( vol = Volumes; vol; vol = vol->v_next ) {
1097 if (( vol->v_flags & AFPVOL_OPEN ) ) {
1104 for ( vol = Volumes, ovol = NULL; vol; vol = nvol) {
1107 if (vol->v_name == NULL) {
1108 if (Volumes == vol) {
1113 ovol->v_next = nvol;
1123 /* ------------------------------- */
1124 static void volume_unlink(struct vol *volume)
1126 struct vol *vol, *ovol, *nvol;
1128 if (volume == Volumes) {
1129 Volumes = Volumes->v_next;
1132 for ( vol = Volumes->v_next, ovol = Volumes; vol; vol = nvol) {
1135 if (vol == volume) {
1136 ovol->v_next = nvol;
1145 static int getvolspace( vol, bfree, btotal, xbfree, xbtotal, bsize )
1147 u_int32_t *bfree, *btotal, *bsize;
1148 VolSpace *xbfree, *xbtotal;
1152 #ifndef NO_QUOTA_SUPPORT
1153 VolSpace qfree, qtotal;
1156 spaceflag = AFPVOL_GVSMASK & vol->v_flags;
1157 /* report up to 2GB if afp version is < 2.2 (4GB if not) */
1158 maxsize = (vol->v_flags & AFPVOL_A2VOL) ? 0x01fffe00 :
1159 (((afp_version < 22) || (vol->v_flags & AFPVOL_LIMITSIZE))
1160 ? 0x7fffffffL : 0xffffffffL);
1163 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_AFSGVS ) {
1164 if ( afs_getvolspace( vol, xbfree, xbtotal, bsize ) == AFP_OK ) {
1165 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_AFSGVS;
1166 goto getvolspace_done;
1171 if (( rc = ustatfs_getvolspace( vol, xbfree, xbtotal,
1172 bsize)) != AFP_OK ) {
1176 #define min(a,b) ((a)<(b)?(a):(b))
1177 #ifndef NO_QUOTA_SUPPORT
1178 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_UQUOTA ) {
1179 if ( uquota_getvolspace( vol, &qfree, &qtotal, *bsize ) == AFP_OK ) {
1180 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_UQUOTA;
1181 *xbfree = min(*xbfree, qfree);
1182 *xbtotal = min( *xbtotal, qtotal);
1183 goto getvolspace_done;
1187 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_USTATFS;
1190 *bfree = min( *xbfree, maxsize);
1191 *btotal = min( *xbtotal, maxsize);
1195 /* -----------------------
1196 * set volume creation date
1197 * avoid duplicate, well at least it tries
1199 static void vol_setdate(u_int16_t id, struct adouble *adp, time_t date)
1202 struct vol *vol = Volumes;
1204 for ( volume = Volumes; volume; volume = volume->v_next ) {
1205 if (volume->v_vid == id) {
1208 else if ((time_t)(AD_DATE_FROM_UNIX(date)) == volume->v_ctime) {
1210 volume = Volumes; /* restart */
1213 vol->v_ctime = AD_DATE_FROM_UNIX(date);
1214 ad_setdate(adp, AD_DATE_CREATE | AD_DATE_UNIX, date);
1217 /* ----------------------- */
1218 static int getvolparams( bitmap, vol, st, buf, buflen )
1226 int bit = 0, isad = 1;
1229 u_int32_t bfree, btotal, bsize;
1230 VolSpace xbfree, xbtotal; /* extended bytes */
1231 char *data, *nameoff = NULL;
1234 /* courtesy of jallison@whistle.com:
1235 * For MacOS8.x support we need to create the
1236 * .Parent file here if it doesn't exist. */
1238 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1239 if ( ad_open_metadata( vol->v_path, vol_noadouble(vol) | ADFLAGS_DIR, O_CREAT, &ad) < 0 ) {
1241 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1243 } else if (ad_get_MD_flags( &ad ) & O_CREAT) {
1244 slash = strrchr( vol->v_path, '/' );
1248 slash = vol->v_path;
1249 if (ad_getentryoff(&ad, ADEID_NAME)) {
1250 ad_setentrylen( &ad, ADEID_NAME, strlen( slash ));
1251 memcpy(ad_entry( &ad, ADEID_NAME ), slash,
1252 ad_getentrylen( &ad, ADEID_NAME ));
1254 vol_setdate(vol->v_vid, &ad, st->st_mtime);
1258 if (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0)
1259 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
1261 vol->v_ctime = aint;
1264 if (( bitmap & ( (1<<VOLPBIT_BFREE)|(1<<VOLPBIT_BTOTAL) |
1265 (1<<VOLPBIT_XBFREE)|(1<<VOLPBIT_XBTOTAL) |
1266 (1<<VOLPBIT_BSIZE)) ) != 0 ) {
1267 if ( getvolspace( vol, &bfree, &btotal, &xbfree, &xbtotal,
1270 ad_close( &ad, ADFLAGS_HF );
1272 return( AFPERR_PARAM );
1277 while ( bitmap != 0 ) {
1278 while (( bitmap & 1 ) == 0 ) {
1286 if (0 == (vol->v_flags & AFPVOL_NOFILEID) && vol->v_cdb != NULL &&
1287 (vol->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1288 ashort = VOLPBIT_ATTR_FILEID;
1290 /* check for read-only.
1291 * NOTE: we don't actually set the read-only flag unless
1292 * it's passed in that way as it's possible to mount
1293 * a read-write filesystem under a read-only one. */
1294 if ((vol->v_flags & AFPVOL_RO) ||
1295 ((utime(vol->v_path, NULL) < 0) && (errno == EROFS))) {
1296 ashort |= VOLPBIT_ATTR_RO;
1298 ashort |= VOLPBIT_ATTR_CATSEARCH;
1299 if (afp_version >= 30) {
1300 ashort |= VOLPBIT_ATTR_UTF8;
1301 if (vol->v_flags & AFPVOL_UNIX_PRIV)
1302 ashort |= VOLPBIT_ATTR_UNIXPRIV;
1304 ashort = htons(ashort);
1305 memcpy(data, &ashort, sizeof( ashort ));
1306 data += sizeof( ashort );
1310 ashort = htons( AFPVOLSIG_DEFAULT );
1311 memcpy(data, &ashort, sizeof( ashort ));
1312 data += sizeof( ashort );
1315 case VOLPBIT_CDATE :
1316 aint = vol->v_ctime;
1317 memcpy(data, &aint, sizeof( aint ));
1318 data += sizeof( aint );
1321 case VOLPBIT_MDATE :
1322 if ( st->st_mtime > vol->v_mtime ) {
1323 vol->v_mtime = st->st_mtime;
1325 aint = AD_DATE_FROM_UNIX(vol->v_mtime);
1326 memcpy(data, &aint, sizeof( aint ));
1327 data += sizeof( aint );
1330 case VOLPBIT_BDATE :
1331 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1332 aint = AD_DATE_START;
1333 memcpy(data, &aint, sizeof( aint ));
1334 data += sizeof( aint );
1338 memcpy(data, &vol->v_vid, sizeof( vol->v_vid ));
1339 data += sizeof( vol->v_vid );
1342 case VOLPBIT_BFREE :
1343 bfree = htonl( bfree );
1344 memcpy(data, &bfree, sizeof( bfree ));
1345 data += sizeof( bfree );
1348 case VOLPBIT_BTOTAL :
1349 btotal = htonl( btotal );
1350 memcpy(data, &btotal, sizeof( btotal ));
1351 data += sizeof( btotal );
1354 #ifndef NO_LARGE_VOL_SUPPORT
1355 case VOLPBIT_XBFREE :
1356 xbfree = hton64( xbfree );
1357 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1358 bcopy(&xbfree, data, sizeof(xbfree));
1359 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1360 memcpy(data, &xbfree, sizeof( xbfree ));
1361 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1362 data += sizeof( xbfree );
1365 case VOLPBIT_XBTOTAL :
1366 xbtotal = hton64( xbtotal );
1367 #if defined(__GNUC__) && defined(HAVE_GCC_MEMCPY_BUG)
1368 bcopy(&xbtotal, data, sizeof(xbtotal));
1369 #else /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1370 memcpy(data, &xbtotal, sizeof( xbtotal ));
1371 #endif /* __GNUC__ && HAVE_GCC_MEMCPY_BUG */
1372 data += sizeof( xbfree );
1374 #endif /* ! NO_LARGE_VOL_SUPPORT */
1378 data += sizeof( u_int16_t );
1381 case VOLPBIT_BSIZE: /* block size */
1382 bsize = htonl(bsize);
1383 memcpy(data, &bsize, sizeof(bsize));
1384 data += sizeof(bsize);
1389 ad_close( &ad, ADFLAGS_HF );
1391 return( AFPERR_BITMAP );
1397 ashort = htons( data - buf );
1398 memcpy(nameoff, &ashort, sizeof( ashort ));
1399 /* name is always in mac charset, FIXME mangle if length > 27 char */
1400 aint = ucs2_to_charset( vol->v_maccharset, vol->v_name, data+1, 255);
1410 ad_close_metadata( &ad);
1412 *buflen = data - buf;
1416 /* ------------------------- */
1417 static int stat_vol(u_int16_t bitmap, struct vol *vol, char *rbuf, int *rbuflen)
1422 if ( stat( vol->v_path, &st ) < 0 ) {
1424 return( AFPERR_PARAM );
1426 /* save the volume device number */
1427 vol->v_dev = st.st_dev;
1429 buflen = *rbuflen - sizeof( bitmap );
1430 if (( ret = getvolparams( bitmap, vol, &st,
1431 rbuf + sizeof( bitmap ), &buflen )) != AFP_OK ) {
1435 *rbuflen = buflen + sizeof( bitmap );
1436 bitmap = htons( bitmap );
1437 memcpy(rbuf, &bitmap, sizeof( bitmap ));
1442 /* ------------------------------- */
1443 void load_volumes(AFPObj *obj)
1445 struct passwd *pwent;
1450 /* check files date */
1451 if (obj->options.defaultvol.loaded) {
1452 changed = volfile_changed(&obj->options.defaultvol);
1454 if (obj->options.systemvol.loaded) {
1455 changed |= volfile_changed(&obj->options.systemvol);
1457 if (obj->options.uservol.loaded) {
1458 changed |= volfile_changed(&obj->options.uservol);
1467 pwent = getpwnam(obj->username);
1468 if ( (obj->options.flags & OPTION_USERVOLFIRST) == 0 ) {
1469 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent);
1472 if ((*obj->username == '\0') || (obj->options.flags & OPTION_NOUSERVOL)) {
1473 readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent);
1476 * Read user's AppleVolumes or .AppleVolumes file
1477 * If neither are readable, read the default volumes file. if
1478 * that doesn't work, create a user share.
1480 if (obj->options.uservol.name) {
1481 free(obj->options.uservol.name);
1483 obj->options.uservol.name = strdup(pwent->pw_dir);
1484 if ( readvolfile(obj, &obj->options.uservol, "AppleVolumes", 1, pwent) < 0 &&
1485 readvolfile(obj, &obj->options.uservol, ".AppleVolumes", 1, pwent) < 0 &&
1486 readvolfile(obj, &obj->options.uservol, "applevolumes", 1, pwent) < 0 &&
1487 readvolfile(obj, &obj->options.uservol, ".applevolumes", 1, pwent) < 0 &&
1488 obj->options.defaultvol.name != NULL ) {
1489 if (readvolfile(obj, &obj->options.defaultvol, NULL, 1, pwent) < 0)
1490 creatvol(obj, pwent, pwent->pw_dir, NULL, NULL, 1);
1493 if ( obj->options.flags & OPTION_USERVOLFIRST ) {
1494 readvolfile(obj, &obj->options.systemvol, NULL, 0, pwent );
1497 if ( obj->options.closevol ) {
1500 for ( vol = Volumes; vol; vol = vol->v_next ) {
1501 if (vol->v_deleted && !vol->v_new ) {
1510 /* ------------------------------- */
1511 int afp_getsrvrparms(obj, ibuf, ibuflen, rbuf, rbuflen )
1513 char *ibuf _U_, *rbuf;
1514 int ibuflen _U_, *rbuflen;
1527 for ( vcnt = 0, volume = Volumes; volume; volume = volume->v_next ) {
1528 if (!(volume->v_flags & AFPVOL_NOSTAT)) {
1531 if ( stat( volume->v_path, &st ) < 0 ) {
1532 LOG(log_info, logtype_afpd, "afp_getsrvrparms(%s): stat: %s",
1533 volume->v_path, strerror(errno) );
1534 continue; /* can't access directory */
1536 if (!S_ISDIR(st.st_mode)) {
1537 continue; /* not a dir */
1539 accessmode(volume->v_path, &ma, NULL, &st);
1540 if ((ma.ma_user & (AR_UREAD | AR_USEARCH)) != (AR_UREAD | AR_USEARCH)) {
1541 continue; /* no r-x access */
1544 if (volume->v_hide) {
1545 continue; /* config file changed but the volume was mounted */
1547 len = ucs2_to_charset_allocate((utf8_encoding()?CH_UTF8_MAC:obj->options.maccharset),
1548 &namebuf, volume->v_name);
1549 if (len == (size_t)-1)
1552 /* set password bit if there's a volume password */
1553 *data = (volume->v_password) ? AFPSRVR_PASSWD : 0;
1555 /* Apple 2 clients running ProDOS-8 expect one volume to have
1556 bit 0 of this byte set. They will not recognize anything
1557 on the server unless this is the case. I have not
1558 completely worked this out, but it's related to booting
1559 from the server. Support for that function is a ways
1560 off.. <shirsch@ibm.net> */
1561 *data |= (volume->v_flags & AFPVOL_A2VOL) ? AFPSRVR_CONFIGINFO : 0;
1562 *data++ |= 0; /* UNIX PRIVS BIT ..., OSX doesn't seem to use it, so we don't either */
1564 memcpy(data, namebuf, len );
1570 *rbuflen = data - rbuf;
1572 if ( gettimeofday( &tv, 0 ) < 0 ) {
1573 LOG(log_error, logtype_afpd, "afp_getsrvrparms(%s): gettimeofday: %s", volume->v_path, strerror(errno) );
1575 return AFPERR_PARAM;
1577 tv.tv_sec = AD_DATE_FROM_UNIX(tv.tv_sec);
1578 memcpy(data, &tv.tv_sec, sizeof( u_int32_t));
1579 data += sizeof( u_int32_t);
1584 /* -------------------------
1585 * we are the user here
1587 int afp_openvol(obj, ibuf, ibuflen, rbuf, rbuflen )
1590 int ibuflen _U_, *rbuflen;
1600 char path[ MAXPATHLEN + 1];
1606 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1607 bitmap = ntohs( bitmap );
1608 ibuf += sizeof( bitmap );
1609 if (( bitmap & (1<<VOLPBIT_VID)) == 0 ) {
1611 return AFPERR_BITMAP;
1614 len = (unsigned char)*ibuf++;
1615 volname = obj->oldtmp;
1617 if ((volname_tmp = strchr(volname,'+')) != NULL)
1618 volname = volname_tmp+1;
1620 namelen = convert_string( (utf8_encoding()?CH_UTF8_MAC:obj->options.maccharset), CH_UCS2,
1621 ibuf, len, volname, sizeof(obj->oldtmp));
1624 return AFPERR_PARAM;
1628 if ((len + 1) & 1) /* pad to an even boundary */
1633 for ( volume = Volumes; volume; volume = volume->v_next ) {
1634 if ( strcasecmp_w( (ucs2_t*) volname, volume->v_name ) == 0 ) {
1639 if ( volume == NULL ) {
1641 return AFPERR_PARAM;
1644 /* check for a volume password */
1645 if (volume->v_password && strncmp(ibuf, volume->v_password, VOLPASSLEN)) {
1647 return AFPERR_ACCESS;
1650 if (( volume->v_flags & AFPVOL_OPEN ) ) {
1651 /* the volume is already open */
1653 set_uidgid ( volume );
1655 return stat_vol(bitmap, volume, rbuf, rbuflen);
1658 /* initialize volume variables
1661 if (afp_version >= 30) {
1662 volume->max_filename = 255;
1665 volume->max_filename = MACFILELEN;
1668 volume->v_dir = volume->v_root = NULL;
1669 volume->v_hash = NULL;
1671 volume->v_flags |= AFPVOL_OPEN;
1672 volume->v_cdb = NULL;
1674 if (volume->v_root_preexec) {
1675 if ((ret = afprun(1, volume->v_root_preexec, NULL)) && volume->v_root_preexec_close) {
1676 LOG(log_error, logtype_afpd, "afp_openvol(%s): root preexec : %d", volume->v_path, ret );
1683 set_uidgid ( volume );
1686 if (volume->v_preexec) {
1687 if ((ret = afprun(0, volume->v_preexec, NULL)) && volume->v_preexec_close) {
1688 LOG(log_error, logtype_afpd, "afp_openvol(%s): preexec : %d", volume->v_path, ret );
1694 if ( stat( volume->v_path, &st ) < 0 ) {
1699 if ( chdir( volume->v_path ) < 0 ) {
1704 len = convert_string_allocate( CH_UCS2, (utf8_encoding()?CH_UTF8_MAC:obj->options.maccharset),
1705 volume->v_name, namelen, &vol_mname);
1706 if ( !vol_mname || len <= 0) {
1711 if ( NULL == getcwd(path, MAXPATHLEN)) {
1712 /* shouldn't be fatal but it will fail later */
1713 LOG(log_error, logtype_afpd, "afp_openvol(%s): volume pathlen too long", volume->v_path);
1718 if ((vol_uname = strrchr(path, '/')) == NULL)
1720 else if (*(vol_uname + 1) != '\0')
1723 if ((dir = dirnew(vol_mname, vol_uname) ) == NULL) {
1725 LOG(log_error, logtype_afpd, "afp_openvol(%s): malloc: %s", volume->v_path, strerror(errno) );
1731 dir->d_did = DIRDID_ROOT;
1732 dir->d_color = DIRTREE_COLOR_BLACK; /* root node is black */
1733 volume->v_dir = volume->v_root = dir;
1734 volume->v_hash = dirhash();
1736 curdir = volume->v_dir;
1737 if (volume->v_cnidscheme == NULL) {
1738 volume->v_cnidscheme = strdup(DEFAULT_CNID_SCHEME);
1739 LOG(log_warning, logtype_afpd, "Warning: No CNID scheme for volume %s. Using default.",
1742 if (volume->v_dbpath)
1743 volume->v_cdb = cnid_open (volume->v_dbpath, volume->v_umask, volume->v_cnidscheme, (volume->v_flags & AFPVOL_NODEV));
1745 volume->v_cdb = cnid_open (volume->v_path, volume->v_umask, volume->v_cnidscheme, (volume->v_flags & AFPVOL_NODEV));
1746 if (volume->v_cdb == NULL) {
1747 LOG(log_error, logtype_afpd, "Fatal error: cannot open CNID or invalid CNID backend for %s: %s",
1748 volume->v_path, volume->v_cnidscheme);
1755 if (!volume->v_volcodepage)
1756 volume->v_volcodepage = strdup("UTF8");
1758 if ( (charset_t) -1 == ( volume->v_volcharset = add_charset(volume->v_volcodepage)) ) {
1759 LOG (log_error, logtype_afpd, "Setting codepage %s as volume codepage failed", volume->v_volcodepage);
1764 if ( NULL == ( volume->v_vol = find_charset_functions(volume->v_volcodepage)) || volume->v_vol->flags & CHARSET_ICONV ) {
1765 LOG (log_warning, logtype_afpd, "WARNING: volume encoding %s is *not* supported by netatalk, expect problems !!!!", volume->v_volcodepage);
1768 if (!volume->v_maccodepage)
1769 volume->v_maccodepage = strdup(obj->options.maccodepage);
1771 if ( (charset_t) -1 == ( volume->v_maccharset = add_charset(volume->v_maccodepage)) ) {
1772 LOG (log_error, logtype_afpd, "Setting codepage %s as mac codepage failed", volume->v_maccodepage);
1777 if ( NULL == ( volume->v_mac = find_charset_functions(volume->v_maccodepage)) || ! (volume->v_mac->flags & CHARSET_CLIENT) ) {
1778 LOG (log_error, logtype_afpd, "Fatal error: mac charset %s not supported", volume->v_maccodepage);
1783 ret = stat_vol(bitmap, volume, rbuf, rbuflen);
1784 if (ret == AFP_OK) {
1786 if (!(volume->v_flags & AFPVOL_RO)) {
1787 handle_special_folders( volume );
1788 savevoloptions( volume);
1792 * If you mount a volume twice, the second time the trash appears on
1793 * the desk-top. That's because the Mac remembers the DID for the
1794 * trash (even for volumes in different zones, on different servers).
1795 * Just so this works better, we prime the DID cache with the trash,
1796 * fixing the trash at DID 17.
1797 * FIXME (RL): should it be done inside a CNID backend ? (always returning Trash DID when asked) ?
1799 if ((volume->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1801 /* FIXME find db time stamp */
1802 if (cnid_getstamp(volume->v_cdb, volume->v_stamp, sizeof(volume->v_stamp)) < 0) {
1803 LOG (log_error, logtype_afpd,
1804 "afp_openvol(%s): Fatal error: Unable to get stamp value from CNID backend",
1812 cname( volume, volume->v_dir, &p );
1818 if (volume->v_dir) {
1819 hash_free( volume->v_hash);
1820 dirfree( volume->v_dir );
1821 volume->v_dir = volume->v_root = NULL;
1824 volume->v_flags &= ~AFPVOL_OPEN;
1825 if (volume->v_cdb != NULL) {
1826 cnid_close(volume->v_cdb);
1827 volume->v_cdb = NULL;
1833 /* ------------------------- */
1834 static void closevol(struct vol *vol)
1839 hash_free( vol->v_hash);
1840 dirfree( vol->v_root );
1842 if (vol->v_cdb != NULL) {
1843 cnid_close(vol->v_cdb);
1847 if (vol->v_postexec) {
1848 afprun(0, vol->v_postexec, NULL);
1850 if (vol->v_root_postexec) {
1851 afprun(1, vol->v_root_postexec, NULL);
1855 /* ------------------------- */
1856 void close_all_vol(void)
1860 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
1861 if ( (ovol->v_flags & AFPVOL_OPEN) ) {
1862 ovol->v_flags &= ~AFPVOL_OPEN;
1868 /* ------------------------- */
1869 static void deletevol(struct vol *vol)
1873 vol->v_flags &= ~AFPVOL_OPEN;
1874 for ( ovol = Volumes; ovol; ovol = ovol->v_next ) {
1875 if ( (ovol->v_flags & AFPVOL_OPEN) ) {
1879 if ( ovol != NULL ) {
1880 /* Even if chdir fails, we can't say afp_closevol fails. */
1881 if ( chdir( ovol->v_path ) == 0 ) {
1882 curdir = ovol->v_dir;
1887 if (vol->v_deleted) {
1888 showvol(vol->v_name);
1895 /* ------------------------- */
1896 int afp_closevol(obj, ibuf, ibuflen, rbuf, rbuflen )
1898 char *ibuf, *rbuf _U_;
1899 int ibuflen _U_, *rbuflen;
1906 memcpy(&vid, ibuf, sizeof( vid ));
1907 if (NULL == ( vol = getvolbyvid( vid )) ) {
1908 return( AFPERR_PARAM );
1916 /* ------------------------- */
1917 struct vol *getvolbyvid(const u_int16_t vid )
1921 for ( vol = Volumes; vol; vol = vol->v_next ) {
1922 if ( vid == vol->v_vid ) {
1926 if ( vol == NULL || ( vol->v_flags & AFPVOL_OPEN ) == 0 ) {
1932 #endif /* FORCE_UIDGID */
1937 /* ------------------------ */
1938 static int ext_cmp_key(const void *key, const void *obj)
1940 const char *p = key;
1941 const struct extmap *em = obj;
1942 return strdiacasecmp(p, em->em_ext);
1944 struct extmap *getextmap(const char *path)
1949 if (NULL == ( p = strrchr( path, '.' )) ) {
1950 return( Defextmap );
1953 if (!*p || !Extmap_cnt) {
1954 return( Defextmap );
1956 em = bsearch(p, Extmap, Extmap_cnt, sizeof(struct extmap), ext_cmp_key);
1960 return( Defextmap );
1964 /* ------------------------- */
1965 struct extmap *getdefextmap(void)
1967 return( Defextmap );
1970 /* --------------------------
1971 poll if a volume is changed by other processes.
1973 int pollvoltime(obj)
1980 if (!(afp_version > 21 && obj->options.server_notif))
1983 if ( gettimeofday( &tv, 0 ) < 0 )
1986 for ( vol = Volumes; vol; vol = vol->v_next ) {
1987 if ( (vol->v_flags & AFPVOL_OPEN) && vol->v_mtime + 30 < tv.tv_sec) {
1988 if ( !stat( vol->v_path, &st ) && vol->v_mtime != st.st_mtime ) {
1989 vol->v_mtime = st.st_mtime;
1990 if (!obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED))
1999 /* ------------------------- */
2000 void setvoltime(obj, vol )
2006 /* just looking at vol->v_mtime is broken seriously since updates
2007 * from other users afpd processes never are seen.
2008 * This is not the most elegant solution (a shared memory between
2009 * the afpd processes would come closer)
2012 if ( gettimeofday( &tv, 0 ) < 0 ) {
2013 LOG(log_error, logtype_afpd, "setvoltime(%s): gettimeofday: %s", vol->v_path, strerror(errno) );
2016 if( utime( vol->v_path, NULL ) < 0 ) {
2017 /* write of time failed ... probably a read only filesys,
2018 * where no other users can interfere, so there's no issue here
2022 /* a little granularity */
2023 if (vol->v_mtime < tv.tv_sec) {
2024 vol->v_mtime = tv.tv_sec;
2025 /* or finder doesn't update free space */
2026 if (afp_version > 21 && obj->options.server_notif) {
2027 obj->attention(obj->handle, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED);
2032 /* ------------------------- */
2033 int afp_getvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
2036 int ibuflen _U_, *rbuflen;
2039 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 );
2047 if (NULL == ( vol = getvolbyvid( vid )) ) {
2049 return( AFPERR_PARAM );
2052 return stat_vol(bitmap, vol, rbuf, rbuflen);
2055 /* ------------------------- */
2056 int afp_setvolparams(obj, ibuf, ibuflen, rbuf, rbuflen )
2058 char *ibuf, *rbuf _U_;
2059 int ibuflen _U_, *rbuflen;
2063 u_int16_t vid, bitmap;
2069 memcpy(&vid, ibuf, sizeof( vid ));
2070 ibuf += sizeof( vid );
2071 memcpy(&bitmap, ibuf, sizeof( bitmap ));
2072 bitmap = ntohs( bitmap );
2073 ibuf += sizeof(bitmap);
2075 if (( vol = getvolbyvid( vid )) == NULL ) {
2076 return( AFPERR_PARAM );
2079 if ((vol->v_flags & AFPVOL_RO))
2080 return AFPERR_VLOCK;
2082 /* we can only set the backup date. */
2083 if (bitmap != (1 << VOLPBIT_BDATE))
2084 return AFPERR_BITMAP;
2086 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2087 if ( ad_open( vol->v_path, ADFLAGS_HF|ADFLAGS_DIR, O_RDWR,
2090 return AFPERR_VLOCK;
2092 return AFPERR_ACCESS;
2095 memcpy(&aint, ibuf, sizeof(aint));
2096 ad_setdate(&ad, AD_DATE_BACKUP, aint);
2098 ad_close(&ad, ADFLAGS_HF);
2102 /* ------------------------- */
2103 int wincheck(const struct vol *vol, const char *path)
2107 if (!(vol->v_flags & AFPVOL_MSWINDOWS))
2110 /* empty paths are not allowed */
2111 if ((len = strlen(path)) == 0)
2114 /* leading or trailing whitespaces are not allowed, carriage returns
2115 * and probably other whitespace is okay, tabs are not allowed
2117 if ((path[0] == ' ') || (path[len-1] == ' '))
2120 /* certain characters are not allowed */
2121 if (strpbrk(path, MSWINDOWS_BADCHARS))
2124 /* everything else is okay */
2130 * precreate a folder
2131 * this is only intended for folders in the volume root
2132 * It will *not* work if the folder name contains extended characters
2134 static int create_special_folder (const struct vol *vol, const struct _special_folder *folder)
2143 p = (char *) malloc ( strlen(vol->v_path)+strlen(folder->name)+2);
2145 LOG(log_error, logtype_afpd,"malloc failed");
2149 q=strdup(folder->name);
2151 LOG(log_error, logtype_afpd,"malloc failed");
2155 strcpy(p, vol->v_path);
2160 if ((vol->v_casefold & AFPVOL_MTOUUPPER))
2162 else if ((vol->v_casefold & AFPVOL_MTOULOWER))
2168 if ( (ret = stat( p, &st )) < 0 ) {
2169 if (folder->precreate) {
2170 if (ad_mkdir(p, folder->mode)) {
2171 LOG(log_debug, logtype_afpd,"Creating '%s' failed in %s: %s", p, vol->v_path, strerror(errno));
2180 if ( !ret && folder->hide) {
2182 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2183 if (ad_open( p, vol_noadouble(vol) | ADFLAGS_HF|ADFLAGS_DIR,
2184 O_RDWR|O_CREAT, 0666, &ad) < 0) {
2189 if ((ad_get_HF_flags( &ad ) & O_CREAT) ) {
2190 if (ad_getentryoff(&ad, ADEID_NAME)) {
2191 ad_setentrylen( &ad, ADEID_NAME, strlen(folder->name));
2192 memcpy(ad_entry( &ad, ADEID_NAME ), folder->name,
2193 ad_getentrylen( &ad, ADEID_NAME ));
2197 ad_getattr(&ad, &attr);
2198 attr |= htons( ntohs( attr ) | ATTRBIT_INVISIBLE );
2199 ad_setattr(&ad, attr);
2201 /* do the same with the finder info */
2202 if (ad_entry(&ad, ADEID_FINDERI)) {
2203 memcpy(&attr, ad_entry(&ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF, sizeof(attr));
2204 attr |= htons(FINDERINFO_INVISIBLE);
2205 memcpy(ad_entry(&ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF,&attr, sizeof(attr));
2209 ad_close( &ad, ADFLAGS_HF );
2216 static void handle_special_folders (const struct vol * vol)
2218 const _special_folder *p = &special_folders[0];
2220 if ((vol->v_flags & AFPVOL_RO))
2223 for (; p->name != NULL; p++) {
2224 create_special_folder (vol, p);
2229 * Save the volume options to a file, used by
2231 * Writing the file everytime a volume is opened is
2232 * unnecessary, but it shouldn't hurt much.
2234 static int savevoloptions (const struct vol *vol)
2237 char item[MAXPATHLEN];
2241 const _vol_opt_name *op = &vol_opt_names[0];
2242 const _vol_opt_name *cf = &vol_opt_casefold[0];
2244 strlcpy (item, vol->v_path, sizeof(item));
2245 strlcat (item, "/.AppleDesktop/", sizeof(item));
2246 strlcat (item, VOLINFOFILE, sizeof(item));
2248 if ((fd = open( item, O_RDWR | O_CREAT , 0666)) <0 ) {
2249 LOG(log_debug, logtype_afpd,"Error opening %s: %s", item, strerror(errno));
2253 /* try to get a lock */
2255 lock.l_whence = SEEK_SET;
2257 lock.l_type = F_WRLCK;
2259 if (fcntl(fd, F_SETLK, &lock) < 0) {
2260 if (errno == EACCES || errno == EAGAIN) {
2261 /* ignore, other process already writing the file */
2264 LOG(log_error, logtype_cnid, "savevoloptions: cannot get lock: %s", strerror(errno));
2269 /* write volume options */
2270 snprintf(buf, sizeof(buf), "MAC_CHARSET:%s\n", vol->v_maccodepage);
2271 snprintf(item, sizeof(item), "VOL_CHARSET:%s\n", vol->v_volcodepage);
2272 strlcat(buf, item, sizeof(buf));
2274 switch (vol->v_adouble) {
2276 strlcat(buf, "ADOUBLE_VER:v1\n", sizeof(buf));
2279 strlcat(buf, "ADOUBLE_VER:v2\n", sizeof(buf));
2281 case AD_VERSION2_OSX:
2282 strlcat(buf, "ADOUBLE_VER:osx\n", sizeof(buf));
2284 case AD_VERSION1_ADS:
2285 strlcat(buf, "ADOUBLE_VER:ads\n", sizeof(buf));
2287 case AD_VERSION1_SFM:
2288 strlcat(buf, "ADOUBLE_VER:sfm\n", sizeof(buf));
2292 strlcat(buf, "CNIDBACKEND:", sizeof(buf));
2293 strlcat(buf, vol->v_cnidscheme, sizeof(buf));
2294 strlcat(buf, "\n", sizeof(buf));
2296 strlcat(buf, "CNIDDBDHOST:", sizeof(buf));
2297 strlcat(buf, Cnid_srv, sizeof(buf));
2298 strlcat(buf, "\n", sizeof(buf));
2300 snprintf(item, sizeof(item), "CNIDDBDPORT:%u\n", Cnid_port);
2301 strlcat(buf, item, sizeof(buf));
2303 strcpy(item, "CNID_DBPATH:");
2305 strlcat(item, vol->v_dbpath, sizeof(item));
2307 strlcat(item, vol->v_path, sizeof(item));
2308 strlcat(item, "\n", sizeof(item));
2309 strlcat(buf, item, sizeof(buf));
2312 strcpy(item, "VOLUME_OPTS:");
2313 for (;op->name; op++) {
2314 if ( (vol->v_flags & op->option) ) {
2315 strlcat(item, op->name, sizeof(item));
2316 strlcat(item, " ", sizeof(item));
2319 strlcat(item, "\n", sizeof(item));
2320 strlcat(buf, item, sizeof(buf));
2322 /* casefold flags */
2323 strcpy(item, "VOLCASEFOLD:");
2324 for (;cf->name; cf++) {
2325 if ( (vol->v_casefold & cf->option) ) {
2326 strlcat(item, cf->name, sizeof(item));
2327 strlcat(item, " ", sizeof(item));
2330 strlcat(item, "\n", sizeof(item));
2331 strlcat(buf, item, sizeof(buf));
2333 if (strlen(buf) >= sizeof(buf)-1)
2334 LOG(log_debug, logtype_afpd,"Error writing .volinfo file: buffer too small, %s", buf);
2337 if (write( fd, buf, strlen(buf)) < 0) {
2338 LOG(log_debug, logtype_afpd,"Error writing .volinfo file: %s", strerror(errno));
2341 ftruncate(fd, strlen(buf));
2344 lock.l_type = F_UNLCK;
2345 fcntl(fd, F_SETLK, &lock);