2 This program is free software; you can redistribute it and/or modify
3 it under the terms of the GNU General Public License as published by
4 the Free Software Foundation; either version 2 of the License, or
5 (at your option) any later version.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
12 You should have received a copy of the GNU General Public License
13 along with this program; if not, write to the Free Software
14 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16 .volinfo file handling, command line utilities
17 copyright Bjoern Fernhomberg, 2004
22 #endif /* HAVE_CONFIG_H */
25 #include <sys/types.h>
32 #include <sys/param.h>
34 #include <atalk/adouble.h>
35 #include <atalk/util.h>
36 #include <atalk/logger.h>
37 #include <atalk/volinfo.h>
38 #include <atalk/volume.h>
39 #include <atalk/compat.h>
41 #include <atalk/cnid.h>
44 static const vol_opt_name_t vol_opt_names[] = {
45 {AFPVOL_A2VOL, "PRODOS"}, /* prodos volume */
46 {AFPVOL_CRLF, "CRLF"}, /* cr/lf translation */
47 {AFPVOL_NOADOUBLE, "NOADOUBLE"}, /* don't create .AppleDouble by default */
48 {AFPVOL_RO, "READONLY"}, /* read-only volume */
49 {AFPVOL_MSWINDOWS, "MSWINDOWS"}, /* deal with ms-windows yuckiness. this is going away. */
50 {AFPVOL_NOHEX, "NOHEX"}, /* don't do :hex translation */
51 {AFPVOL_USEDOTS, "USEDOTS"}, /* use real dots */
52 {AFPVOL_LIMITSIZE, "LIMITSIZE"}, /* limit size for older macs */
53 {AFPVOL_MAPASCII, "MAPASCII"}, /* map the ascii range as well */
54 {AFPVOL_NOFILEID, "NOFILEID"}, /* don't advertise createid resolveid and deleteid calls */
55 {AFPVOL_NOSTAT, "NOSTAT"}, /* advertise the volume even if we can't stat() it
56 * maybe because it will be mounted later in preexec */
57 {AFPVOL_UNIX_PRIV, "UNIXPRIV"}, /* support unix privileges */
58 {AFPVOL_NODEV, "NODEV"}, /* always use 0 for device number in cnid calls */
59 {AFPVOL_CASEINSEN, "CASEINSENSITIVE"}, /* volume is case insensitive */
60 {AFPVOL_EILSEQ, "ILLEGALSEQ"}, /* encode illegal sequence */
61 {AFPVOL_CACHE, "CACHEID"}, /* Use adouble v2 CNID caching, default don't use it */
62 {AFPVOL_INV_DOTS, "INVISIBLEDOTS"},
63 {AFPVOL_ACLS, "ACLS"}, /* Vol supports ACLs */
64 {AFPVOL_TM, "TM"}, /* Set "kSupportsTMLockSteal" is volume attributes */
68 static const vol_opt_name_t vol_opt_casefold[] = {
69 {AFPVOL_MTOUUPPER, "MTOULOWER"},
70 {AFPVOL_MTOULOWER, "MTOULOWER"},
71 {AFPVOL_UTOMUPPER, "UTOMUPPER"},
72 {AFPVOL_UTOMLOWER, "UTOMLOWER"},
92 static const info_option_t info_options[] = {
93 {"MAC_CHARSET", MAC_CHARSET},
94 {"VOL_CHARSET", VOL_CHARSET},
95 {"ADOUBLE_VER", ADOUBLE_VER},
96 {"CNIDBACKEND", CNIDBACKEND},
97 {"CNIDDBDHOST", CNIDDBDHOST},
98 {"CNIDDBDPORT", CNIDDBDPORT},
99 {"CNID_DBPATH", CNID_DBPATH},
100 {"VOLUME_OPTS", VOLUME_OPTS},
101 {"VOLCASEFOLD", VOLCASEFOLD},
102 {"EXTATTRTYPE", EXTATTRTYPE},
106 static char* find_in_path( char *path, char *subdir, size_t maxlen)
111 strlcat(path, subdir, maxlen);
112 pos = strrchr(path, '/');
114 while ( stat(path, &st) != 0) {
116 if ((pos = strrchr(path, '/'))) {
118 strlcat(path, subdir, maxlen);
125 path[pos-path] = '/';
126 path[pos-path+1] = 0;
131 static char * make_path_absolute(char *path, size_t bufsize)
134 char savecwd[MAXPATHLEN];
135 char abspath[MAXPATHLEN];
138 strlcpy(abspath, path, sizeof(abspath));
140 /* we might be called from `ad cp ...` with non existing target */
141 if (stat(abspath, &st) != 0) {
145 if (NULL == (p = strrchr(abspath, '/')) )
146 /* single component `ad cp SOURCEFILE TARGETFILE`, use "." instead */
147 strcpy(abspath, ".");
149 /* try without the last path element */
152 if (stat(abspath, &st) != 0) {
157 if (S_ISREG(st.st_mode)) {
158 /* single file copy SOURCE */
159 if (NULL == (p = strrchr(abspath, '/')) )
160 /* no path, use "." instead */
161 strcpy(abspath, ".");
163 /* try without the last path element */
167 if (!getcwd(savecwd, sizeof(savecwd)) || chdir(abspath) < 0)
170 if (!getcwd(abspath, sizeof(abspath)) || chdir (savecwd) < 0)
173 if (strlen(abspath) > bufsize)
176 strlcpy(path, abspath, bufsize);
180 static char * find_volumeroot(char *path, size_t maxlen)
182 char *volume = make_path_absolute(path, maxlen);
187 if (NULL == (find_in_path(volume, "/.AppleDesktop", maxlen)) )
193 int vol_load_charsets( struct volinfo *vol)
195 if ( (charset_t) -1 == ( vol->v_maccharset = add_charset(vol->v_maccodepage)) ) {
196 fprintf( stderr, "Setting codepage %s as Mac codepage failed", vol->v_maccodepage);
200 if ( (charset_t) -1 == ( vol->v_volcharset = add_charset(vol->v_volcodepage)) ) {
201 fprintf( stderr, "Setting codepage %s as volume codepage failed", vol->v_volcodepage);
208 static int parse_options (char *buf, int *flags, const vol_opt_name_t *options)
211 const vol_opt_name_t *op;
215 while ( *p != '\0') {
219 for (;op->name; op++) {
220 if ( !strcmp(op->name, q )) {
221 *flags |= op->option;
235 static int parseline ( char *buf, struct volinfo *vol)
240 const info_option_t *p = &info_options[0];
242 if (NULL == ( value = strchr(buf, ':')) )
248 if ( 0 == (len = strlen(value)) )
251 if (value[len-1] == '\n')
254 for (;p->name; p++) {
255 if ( !strcmp(p->name, buf )) {
263 if ((vol->v_maccodepage = strdup(value)) == NULL) {
264 fprintf (stderr, "strdup: %s", strerror(errno));
269 if ((vol->v_volcodepage = strdup(value)) == NULL) {
270 fprintf (stderr, "strdup: %s", strerror(errno));
275 if ((vol->v_cnidscheme = strdup(value)) == NULL) {
276 fprintf (stderr, "strdup: %s", strerror(errno));
281 if ((vol->v_dbd_host = strdup(value)) == NULL) {
282 fprintf (stderr, "strdup: %s", strerror(errno));
287 if ((vol->v_dbd_port = strdup(value)) == NULL) {
288 fprintf (stderr, "strdup: %s", strerror(errno));
293 if ((vol->v_dbpath = malloc(MAXPATHLEN+1)) == NULL)
295 strcpy(vol->v_dbpath, value);
298 if (strcasecmp(value, "v2") == 0) {
299 vol->ad_path = ad_path;
300 vol->v_adouble = AD_VERSION2;
301 } else if (strcasecmp(value, "ea") == 0) {
302 vol->ad_path = ad_path_ea;
303 vol->v_adouble = AD_VERSION_EA;
306 fprintf (stderr, "unknown adouble version: %s, %s", buf, value);
311 parse_options(value, &vol->v_flags, &vol_opt_names[0]);
314 parse_options(value, &vol->v_casefold, &vol_opt_casefold[0]);
317 if (strcasecmp(value, "AFPVOL_EA_AD") == 0)
318 vol->v_vfs_ea = AFPVOL_EA_AD;
319 else if (strcasecmp(value, "AFPVOL_EA_SYS") == 0)
320 vol->v_vfs_ea = AFPVOL_EA_SYS;
323 fprintf (stderr, "unknown volume information: %s, %s", buf, value);
332 int loadvolinfo (char *path, struct volinfo *vol)
335 char volinfofile[MAXPATHLEN];
336 char buf[MAXPATHLEN];
344 memset(vol, 0, sizeof(struct volinfo));
345 strlcpy(volinfofile, path, sizeof(volinfofile));
347 /* volinfo file is in .AppleDesktop */
348 if ( NULL == find_volumeroot(volinfofile, sizeof(volinfofile)))
351 if ((vol->v_path = strdup(volinfofile)) == NULL ) {
352 fprintf (stderr, "strdup: %s", strerror(errno));
355 /* Remove trailing slashes */
356 len = strlen(vol->v_path);
357 while (len && (vol->v_path[len-1] == '/')) {
358 vol->v_path[len-1] = 0;
362 strlcat(volinfofile, ".AppleDesktop/", sizeof(volinfofile));
363 strlcat(volinfofile, VOLINFOFILE, sizeof(volinfofile));
365 /* open the file read only */
366 if ( NULL == (fp = fopen( volinfofile, "r")) ) {
367 fprintf (stderr, "error opening volinfo (%s): %s", volinfofile, strerror(errno));
372 /* try to get a read lock */
374 lock.l_whence = SEEK_SET;
376 lock.l_type = F_RDLCK;
378 /* wait for read lock */
379 if (fcntl(fd, F_SETLKW, &lock) < 0) {
385 while (NULL != fgets(buf, sizeof(buf), fp)) {
390 lock.l_type = F_UNLCK;
391 fcntl(fd, F_SETLK, &lock);
393 /* Translate vol options to ad options like afp/volume.c does it */
394 vol->v_ad_options = 0;
395 if ((vol->v_flags & AFPVOL_NODEV))
396 vol->v_ad_options |= ADVOL_NODEV;
397 if ((vol->v_flags & AFPVOL_CACHE))
398 vol->v_ad_options |= ADVOL_CACHE;
399 if ((vol->v_flags & AFPVOL_UNIX_PRIV))
400 vol->v_ad_options |= ADVOL_UNIXPRIV;
401 if ((vol->v_flags & AFPVOL_INV_DOTS))
402 vol->v_ad_options |= ADVOL_INVDOTS;
404 vol->retaincount = 1;
411 * Allocate a struct volinfo object for refcounting usage with retain and close, and
412 * call loadvolinfo with it
414 struct volinfo *allocvolinfo(char *path)
416 struct volinfo *p = malloc(sizeof(struct volinfo));
420 if (loadvolinfo(path, p) == -1)
428 void retainvolinfo(struct volinfo *vol)
434 * Decrement retain count, free resources when retaincount reaches 0
436 int closevolinfo(struct volinfo *volinfo)
438 if (volinfo->retaincount <= 0)
441 volinfo->retaincount--;
443 if (volinfo->retaincount == 0) {
444 free(volinfo->v_name); volinfo->v_name = NULL;
445 free(volinfo->v_path); volinfo->v_path = NULL;
446 free(volinfo->v_cnidscheme); volinfo->v_cnidscheme = NULL;
447 free(volinfo->v_dbpath); volinfo->v_dbpath = NULL;
448 free(volinfo->v_volcodepage); volinfo->v_volcodepage = NULL;
449 free(volinfo->v_maccodepage); volinfo->v_maccodepage = NULL;
450 free(volinfo->v_dbd_host); volinfo->v_dbd_host = NULL;
451 free(volinfo->v_dbd_port); volinfo->v_dbd_port = NULL;
452 if (volinfo->malloced) {
453 volinfo->malloced = 0;
462 * Save the volume options to a file, used by shell utilities. Writing the file
463 * everytime a volume is opened is unnecessary, but it shouldn't hurt much.
465 int savevolinfo(const struct vol *vol, const char *Cnid_srv, const char *Cnid_port)
469 char item[MAXPATHLEN];
473 const vol_opt_name_t *op = &vol_opt_names[0];
474 const vol_opt_name_t *cf = &vol_opt_casefold[0];
476 strlcpy (item, vol->v_path, sizeof(item));
477 strlcat (item, "/.AppleDesktop/", sizeof(item));
478 strlcat (item, VOLINFOFILE, sizeof(item));
480 process_uid = geteuid();
482 if (seteuid(0) == -1) {
487 if ((fd = open(item, O_RDWR | O_CREAT , 0666)) <0 ) {
488 LOG(log_error, logtype_afpd,"Error opening %s: %s", item, strerror(errno));
493 if (seteuid(process_uid) == -1) {
494 LOG(log_error, logtype_logger, "can't seteuid back %s", strerror(errno));
499 /* try to get a lock */
501 lock.l_whence = SEEK_SET;
503 lock.l_type = F_WRLCK;
505 if (fcntl(fd, F_SETLK, &lock) < 0) {
506 if (errno == EACCES || errno == EAGAIN) {
507 /* ignore, other process already writing the file */
510 LOG(log_error, logtype_cnid, "savevoloptions: cannot get lock: %s", strerror(errno));
515 /* write volume options */
516 snprintf(buf, sizeof(buf), "MAC_CHARSET:%s\n", vol->v_maccodepage);
517 snprintf(item, sizeof(item), "VOL_CHARSET:%s\n", vol->v_volcodepage);
518 strlcat(buf, item, sizeof(buf));
520 switch (vol->v_adouble) {
522 strlcat(buf, "ADOUBLE_VER:v2\n", sizeof(buf));
525 strlcat(buf, "ADOUBLE_VER:ea\n", sizeof(buf));
529 strlcat(buf, "CNIDBACKEND:", sizeof(buf));
530 strlcat(buf, vol->v_cnidscheme, sizeof(buf));
531 strlcat(buf, "\n", sizeof(buf));
533 strlcat(buf, "CNIDDBDHOST:", sizeof(buf));
534 strlcat(buf, Cnid_srv, sizeof(buf));
535 strlcat(buf, "\n", sizeof(buf));
537 strlcat(buf, "CNIDDBDPORT:", sizeof(buf));
538 strlcat(buf, Cnid_port, sizeof(buf));
539 strlcat(buf, "\n", sizeof(buf));
541 strcpy(item, "CNID_DBPATH:");
543 strlcat(item, vol->v_dbpath, sizeof(item));
545 strlcat(item, vol->v_path, sizeof(item));
546 strlcat(item, "\n", sizeof(item));
547 strlcat(buf, item, sizeof(buf));
550 strcpy(item, "VOLUME_OPTS:");
551 for (;op->name; op++) {
552 if ( (vol->v_flags & op->option) ) {
553 strlcat(item, op->name, sizeof(item));
554 strlcat(item, " ", sizeof(item));
557 strlcat(item, "\n", sizeof(item));
558 strlcat(buf, item, sizeof(buf));
561 strcpy(item, "VOLCASEFOLD:");
562 for (;cf->name; cf++) {
563 if ( (vol->v_casefold & cf->option) ) {
564 strlcat(item, cf->name, sizeof(item));
565 strlcat(item, " ", sizeof(item));
568 strlcat(item, "\n", sizeof(item));
569 strlcat(buf, item, sizeof(buf));
571 /* ExtendedAttributes */
572 strcpy(item, "EXTATTRTYPE:");
573 switch (vol->v_vfs_ea) {
575 strlcat(item, "AFPVOL_EA_SYS\n", sizeof(item));
578 strlcat(item, "AFPVOL_EA_AD\n", sizeof(item));
581 strlcat(item, "AFPVOL_EA_NONE\n", sizeof(item));
584 strlcat(item, "AFPVOL_EA_UNKNOWN\n", sizeof(item));
587 strlcat(buf, item, sizeof(buf));
589 if (strlen(buf) >= sizeof(buf)-1)
590 LOG(log_debug, logtype_afpd,"Error writing .volinfo file: buffer too small, %s", buf);
591 if (write( fd, buf, strlen(buf)) < 0 || ftruncate(fd, strlen(buf)) < 0 ) {
592 LOG(log_debug, logtype_afpd,"Error writing .volinfo file: %s", strerror(errno));
595 lock.l_type = F_UNLCK;
596 fcntl(fd, F_SETLK, &lock);