Home | History | Annotate | Line # | Download | only in extattr
getextattr.c revision 1.9.2.1
      1  1.9.2.1     yamt /*	$NetBSD: getextattr.c,v 1.9.2.1 2012/10/30 19:00:16 yamt Exp $	*/
      2      1.1  thorpej 
      3      1.1  thorpej /*-
      4      1.1  thorpej  * Copyright (c) 2002, 2003 Networks Associates Technology, Inc.
      5      1.1  thorpej  * Copyright (c) 2002 Poul-Henning Kamp.
      6      1.1  thorpej  * Copyright (c) 1999, 2000, 2001, 2002 Robert N. M. Watson
      7      1.1  thorpej  * All rights reserved.
      8      1.1  thorpej  *
      9      1.1  thorpej  * This software was developed for the FreeBSD Project by Poul-Henning
     10      1.1  thorpej  * Kamp and Network Associates Laboratories, the Security Research Division
     11      1.1  thorpej  * of Network Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
     12      1.1  thorpej  * ("CBOSS"), as part of the DARPA CHATS research program
     13      1.1  thorpej  *
     14      1.1  thorpej  * Redistribution and use in source and binary forms, with or without
     15      1.1  thorpej  * modification, are permitted provided that the following conditions
     16      1.1  thorpej  * are met:
     17      1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     18      1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     19      1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     20      1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     21      1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     22      1.1  thorpej  * 3. The names of the authors may not be used to endorse or promote
     23      1.1  thorpej  *    products derived from this software without specific prior written
     24      1.1  thorpej  *    permission.
     25      1.1  thorpej  *
     26      1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     27      1.1  thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28      1.1  thorpej  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29      1.1  thorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     30      1.1  thorpej  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31      1.1  thorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32      1.1  thorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33      1.1  thorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34      1.1  thorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35      1.1  thorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36      1.1  thorpej  * SUCH DAMAGE.
     37      1.1  thorpej  *
     38      1.1  thorpej  * FreeBSD: src/usr.sbin/extattr/rmextattr.c,v 1.6 2003/06/05 04:30:00 rwatson Exp
     39      1.1  thorpej  */
     40      1.1  thorpej 
     41      1.1  thorpej #include <sys/types.h>
     42      1.1  thorpej #include <sys/uio.h>
     43      1.1  thorpej #include <sys/extattr.h>
     44      1.1  thorpej 
     45      1.1  thorpej #include <err.h>
     46      1.1  thorpej #include <errno.h>
     47      1.1  thorpej //#include <libgen.h>
     48      1.1  thorpej #include <stdio.h>
     49      1.1  thorpej #include <stdlib.h>
     50      1.1  thorpej #include <string.h>
     51      1.1  thorpej #include <unistd.h>
     52      1.1  thorpej #include <vis.h>
     53      1.8     manu #include <fcntl.h>
     54      1.8     manu #include <sys/stat.h>
     55      1.1  thorpej //#include <util.h>
     56      1.1  thorpej 
     57      1.1  thorpej static enum { EADUNNO, EAGET, EASET, EARM, EALS } what = EADUNNO;
     58      1.1  thorpej 
     59      1.9    joerg __dead static void
     60      1.1  thorpej usage(void)
     61      1.1  thorpej {
     62      1.1  thorpej 
     63      1.1  thorpej 	switch (what) {
     64      1.1  thorpej 	case EAGET:
     65      1.8     manu 		fprintf(stderr, "usage: %s [-fhq] [-s | -x | -v style] "
     66      1.1  thorpej 		    "attrnamespace attrname filename ...\n", getprogname());
     67      1.1  thorpej 		exit(1);
     68      1.1  thorpej 
     69      1.1  thorpej 	case EASET:
     70      1.1  thorpej 		fprintf(stderr, "usage: %s [-fhnq] "
     71      1.1  thorpej 		    "attrnamespace attrname attrvalue filename ...\n",
     72      1.1  thorpej 		    getprogname());
     73      1.8     manu 		fprintf(stderr, "usage: %s [-fhnq] -i attrvalue_file "
     74      1.8     manu 		    "attrnamespace attrname filename ...\n",
     75      1.8     manu 		    getprogname());
     76      1.1  thorpej 		exit(1);
     77      1.1  thorpej 
     78      1.1  thorpej 	case EARM:
     79      1.1  thorpej 		fprintf(stderr, "usage: %s [-fhq] "
     80      1.1  thorpej 		    "attrnamespace attrname filename ...\n", getprogname());
     81      1.1  thorpej 		exit(1);
     82      1.1  thorpej 
     83      1.1  thorpej 	case EALS:
     84      1.1  thorpej 		fprintf(stderr, "usage: %s [-fhq] "
     85      1.1  thorpej 		    "attrnamespace filename ...\n", getprogname());
     86      1.1  thorpej 		exit(1);
     87      1.1  thorpej 
     88      1.1  thorpej 	case EADUNNO:
     89      1.1  thorpej 	default:
     90      1.1  thorpej 		fprintf(stderr,
     91      1.1  thorpej 		    "usage: (getextattr|lsextattr|rmextattr|setextattr)\n");
     92      1.1  thorpej 		exit (1);
     93      1.1  thorpej 	}
     94      1.1  thorpej }
     95      1.1  thorpej 
     96      1.1  thorpej static void
     97      1.1  thorpej mkbuf(char **buf, int *oldlen, int newlen)
     98      1.1  thorpej {
     99      1.1  thorpej 
    100      1.1  thorpej 	if (*oldlen >= newlen)
    101      1.1  thorpej 		return;
    102      1.1  thorpej 	if (*buf != NULL)
    103      1.1  thorpej 		free(*buf);
    104      1.1  thorpej 	*buf = malloc(newlen);
    105      1.1  thorpej 	if (*buf == NULL)
    106      1.1  thorpej 		err(1, "malloc");
    107      1.1  thorpej 	*oldlen = newlen;
    108      1.1  thorpej 	return;
    109      1.1  thorpej }
    110      1.1  thorpej 
    111      1.8     manu static int
    112      1.8     manu parse_flag_vis(const char *opt)
    113      1.8     manu {
    114      1.8     manu 	if (strcmp(opt, "default") == 0)
    115      1.8     manu 		return 0;
    116      1.8     manu 	else if (strcmp(opt, "cstyle") == 0)
    117      1.8     manu 		return VIS_CSTYLE;
    118      1.8     manu 	else if (strcmp(opt, "octal") == 0)
    119      1.8     manu 		return VIS_OCTAL;
    120      1.8     manu 	else if (strcmp(opt, "httpstyle") == 0)
    121      1.8     manu 		return VIS_HTTPSTYLE;
    122      1.8     manu 
    123      1.8     manu 	/* Convenient aliases */
    124      1.8     manu 	else if (strcmp(opt, "vis") == 0)
    125      1.8     manu 		return 0;
    126      1.8     manu 	else if (strcmp(opt, "c") == 0)
    127      1.8     manu 		return VIS_CSTYLE;
    128      1.8     manu 	else if (strcmp(opt, "http") == 0)
    129      1.8     manu 		return VIS_HTTPSTYLE;
    130      1.8     manu 	else
    131      1.8     manu 		fprintf(stderr, "%s: invalid -s option \"%s\"",
    132      1.8     manu 			getprogname(), opt);
    133      1.8     manu 
    134      1.8     manu 	return -1;
    135      1.8     manu }
    136      1.8     manu 
    137      1.8     manu #define HEXDUMP_PRINT(x) ((uint8_t)x >= 32 && (uint8_t)x < 128)  ? x : '.'
    138      1.8     manu static void
    139      1.8     manu hexdump(const char *addr, size_t len)
    140      1.8     manu {
    141      1.8     manu 	unsigned int i, j;
    142      1.8     manu 
    143      1.8     manu 	for (i = 0; i < len; i += 16) {
    144      1.8     manu 		printf("   %03x   ", i);
    145      1.8     manu 		for (j = 0; j < 16; j++) {
    146  1.9.2.1     yamt 			if (i + j >= len)
    147      1.8     manu 				printf("   ");
    148      1.8     manu 			else
    149      1.8     manu 				printf("%02x ", addr[i + j] & 0xff);
    150      1.8     manu 		}
    151      1.8     manu 		printf("   ");
    152      1.8     manu 		for (j = 0; j < 16; j++) {
    153  1.9.2.1     yamt 			if (i + j >= len)
    154      1.8     manu 				printf(" ");
    155      1.8     manu 			else
    156      1.8     manu 				printf("%c", HEXDUMP_PRINT(addr[i + j]));
    157      1.8     manu 		}
    158      1.8     manu 		printf("\n");
    159      1.8     manu 	}
    160      1.8     manu }
    161      1.8     manu #undef HEXDUMP_PRINT
    162      1.8     manu 
    163      1.1  thorpej int
    164      1.1  thorpej main(int argc, char *argv[])
    165      1.1  thorpej {
    166      1.1  thorpej 	char	*buf, *visbuf;
    167      1.1  thorpej 	const char *p;
    168      1.1  thorpej 
    169      1.1  thorpej 	const char *options, *attrname;
    170      1.1  thorpej 	int	 buflen, visbuflen, ch, error, i, arg_counter, attrnamespace,
    171      1.8     manu 		 minargc, val_len = 0;
    172      1.1  thorpej 
    173      1.1  thorpej 	int	flag_force = 0;
    174      1.1  thorpej 	int	flag_nofollow = 0;
    175      1.1  thorpej 	int	flag_null = 0;
    176      1.1  thorpej 	int	flag_quiet = 0;
    177      1.8     manu 	int	flag_vis = -1;
    178      1.1  thorpej 	int	flag_hex = 0;
    179      1.8     manu 	char	*filename = NULL;
    180      1.1  thorpej 
    181      1.2    lukem 	options = NULL;
    182      1.2    lukem 	minargc = 0;
    183      1.1  thorpej 	visbuflen = buflen = 0;
    184      1.1  thorpej 	visbuf = buf = NULL;
    185      1.1  thorpej 
    186      1.1  thorpej 	p = getprogname();
    187      1.1  thorpej 	if (strcmp(p, "getextattr") == 0) {
    188      1.1  thorpej 		what = EAGET;
    189      1.8     manu 		options = "fhqsxv:";
    190      1.8     manu 		minargc = 2;
    191      1.1  thorpej 	} else if (strcmp(p, "setextattr") == 0) {
    192      1.1  thorpej 		what = EASET;
    193      1.8     manu 		options = "fhnqi:";
    194      1.8     manu 		minargc = 3;
    195      1.1  thorpej 	} else if (strcmp(p, "rmextattr") == 0) {
    196      1.1  thorpej 		what = EARM;
    197      1.1  thorpej 		options = "fhq";
    198      1.8     manu 		minargc = 2;
    199      1.1  thorpej 	} else if (strcmp(p, "lsextattr") == 0) {
    200      1.1  thorpej 		what = EALS;
    201      1.1  thorpej 		options = "fhq";
    202      1.1  thorpej 		minargc = 2;
    203      1.1  thorpej 	} else
    204      1.1  thorpej 		usage();
    205      1.1  thorpej 
    206      1.1  thorpej 	while ((ch = getopt(argc, argv, options)) != -1) {
    207      1.1  thorpej 		switch (ch) {
    208      1.1  thorpej 		case 'f':
    209      1.1  thorpej 			flag_force = 1;
    210      1.1  thorpej 			break;
    211      1.1  thorpej 		case 'h':
    212      1.1  thorpej 			flag_nofollow = 1;
    213      1.1  thorpej 			break;
    214      1.1  thorpej 		case 'n':
    215      1.1  thorpej 			flag_null = 1;
    216      1.1  thorpej 			break;
    217      1.1  thorpej 		case 'q':
    218      1.1  thorpej 			flag_quiet = 1;
    219      1.1  thorpej 			break;
    220      1.1  thorpej 		case 's':
    221      1.8     manu 			flag_vis = VIS_SAFE | VIS_WHITE;
    222      1.8     manu 			break;
    223      1.8     manu 		case 'v':
    224      1.8     manu 			flag_vis = parse_flag_vis(optarg);
    225      1.1  thorpej 			break;
    226      1.1  thorpej 		case 'x':
    227      1.1  thorpej 			flag_hex = 1;
    228      1.1  thorpej 			break;
    229      1.8     manu 		case 'i':
    230      1.8     manu 			filename = optarg;
    231      1.8     manu 			minargc--;
    232      1.8     manu 			break;
    233      1.1  thorpej 		default:
    234      1.1  thorpej 			usage();
    235      1.1  thorpej 		}
    236      1.1  thorpej 	}
    237      1.1  thorpej 
    238      1.1  thorpej 	argc -= optind;
    239      1.1  thorpej 	argv += optind;
    240      1.1  thorpej 
    241      1.8     manu 	/*
    242      1.8     manu 	 * Check for missing argument.
    243      1.8     manu 	 */
    244      1.1  thorpej 	if (argc < minargc)
    245      1.1  thorpej 		usage();
    246      1.1  thorpej 
    247      1.8     manu 	/*
    248      1.8     manu 	 * Normal case "namespace attribute".
    249      1.8     manu 	 */
    250      1.1  thorpej 	error = extattr_string_to_namespace(argv[0], &attrnamespace);
    251      1.8     manu 	if (error == 0) {
    252      1.8     manu 		/*
    253      1.8     manu 		 * Namespace was specified, so we need one more argument
    254      1.8     manu 		 * for the attribute (except for listing)
    255      1.8     manu 		 */
    256      1.8     manu 		if ((what != EALS) && (argc < minargc + 1))
    257      1.8     manu 			usage();
    258      1.8     manu 		argc--; argv++;
    259      1.8     manu 	} else {
    260      1.8     manu 		/*
    261      1.8     manu 		 * The namespace was not valid. Perhaps it was omited.
    262      1.8     manu 		 * Try to guess a missing namespace by using
    263      1.8     manu 		 * linux layout "namespace.attribute". While
    264      1.8     manu 		 * we are here, also test the Linux namespaces.
    265      1.8     manu 		 */
    266      1.8     manu 		if (strstr(argv[0], "user.") == argv[0]) {
    267      1.8     manu 			attrnamespace = EXTATTR_NAMESPACE_USER;
    268      1.8     manu 		} else
    269      1.8     manu 		if ((strstr(argv[0], "system.") == argv[0]) ||
    270      1.8     manu 		    (strstr(argv[0], "trusted.") == argv[0]) ||
    271      1.8     manu 		    (strstr(argv[0], "security.") == argv[0])) {
    272      1.8     manu 			attrnamespace = EXTATTR_NAMESPACE_SYSTEM;
    273      1.8     manu 		} else {
    274      1.8     manu 			err(1, "%s", argv[0]);
    275      1.8     manu 		}
    276      1.8     manu 	}
    277      1.8     manu 
    278      1.1  thorpej 
    279      1.1  thorpej 	if (what != EALS) {
    280      1.1  thorpej 		attrname = argv[0];
    281      1.1  thorpej 		argc--; argv++;
    282      1.1  thorpej 	} else
    283      1.1  thorpej 		attrname = NULL;
    284      1.1  thorpej 
    285      1.1  thorpej 	if (what == EASET) {
    286      1.8     manu 		/*
    287      1.8     manu 		 * Handle -i option, reading value from a file.
    288      1.8     manu 		 */
    289      1.8     manu 		if (filename != NULL) {
    290      1.8     manu 			int fd;
    291      1.8     manu 			struct stat st;
    292      1.8     manu 			ssize_t readen, remain;
    293      1.8     manu 
    294      1.8     manu 			if ((fd = open(filename, O_RDONLY, 0)) == -1)
    295      1.8     manu 				err(1, "%s: cannot open \"%s\"",
    296      1.8     manu 				     getprogname(), filename);
    297      1.8     manu 
    298      1.8     manu 			if (fstat(fd, &st) != 0)
    299      1.8     manu 				err(1, "%s: cannot stat \"%s\"",
    300      1.8     manu 				     getprogname(), filename);
    301      1.8     manu 
    302      1.8     manu 			val_len = st.st_size;
    303      1.8     manu 			mkbuf(&buf, &buflen, val_len);
    304      1.8     manu 
    305      1.8     manu 			for (remain = val_len; remain > 0; remain -= readen) {
    306      1.8     manu 				if ((readen = read(fd, buf, remain)) == -1)
    307      1.8     manu 					err(1, "%s: cannot read \"%s\"",
    308      1.8     manu 					    getprogname(), filename);
    309      1.8     manu 			}
    310      1.8     manu 
    311      1.8     manu 			(void)close(fd);
    312      1.8     manu 		} else {
    313      1.8     manu 			val_len = strlen(argv[0]) + 1;
    314      1.8     manu 			mkbuf(&buf, &buflen, val_len);
    315      1.8     manu 			strcpy(buf, argv[0]); /* safe */
    316      1.8     manu 			argc--; argv++;
    317      1.8     manu 		}
    318      1.1  thorpej 	}
    319      1.1  thorpej 
    320      1.1  thorpej 	for (arg_counter = 0; arg_counter < argc; arg_counter++) {
    321      1.1  thorpej 		switch (what) {
    322      1.1  thorpej 		case EARM:
    323      1.1  thorpej 			if (flag_nofollow)
    324      1.1  thorpej 				error = extattr_delete_link(argv[arg_counter],
    325      1.1  thorpej 				    attrnamespace, attrname);
    326      1.1  thorpej 			else
    327      1.1  thorpej 				error = extattr_delete_file(argv[arg_counter],
    328      1.1  thorpej 				    attrnamespace, attrname);
    329      1.1  thorpej 			if (error >= 0)
    330      1.1  thorpej 				continue;
    331      1.1  thorpej 			break;
    332      1.1  thorpej 		case EASET:
    333      1.1  thorpej 			if (flag_nofollow)
    334      1.1  thorpej 				error = extattr_set_link(argv[arg_counter],
    335      1.1  thorpej 				    attrnamespace, attrname, buf,
    336      1.8     manu 				    val_len + flag_null);
    337      1.1  thorpej 			else
    338      1.1  thorpej 				error = extattr_set_file(argv[arg_counter],
    339      1.1  thorpej 				    attrnamespace, attrname, buf,
    340      1.8     manu 				    val_len + flag_null);
    341      1.1  thorpej 			if (error >= 0)
    342      1.1  thorpej 				continue;
    343      1.1  thorpej 			break;
    344      1.1  thorpej 		case EALS:
    345      1.1  thorpej 			if (flag_nofollow)
    346      1.1  thorpej 				error = extattr_list_link(argv[arg_counter],
    347      1.1  thorpej 				    attrnamespace, NULL, 0);
    348      1.1  thorpej 			else
    349      1.1  thorpej 				error = extattr_list_file(argv[arg_counter],
    350      1.1  thorpej 				    attrnamespace, NULL, 0);
    351      1.1  thorpej 			if (error < 0)
    352      1.1  thorpej 				break;
    353      1.1  thorpej 			mkbuf(&buf, &buflen, error);
    354      1.1  thorpej 			if (flag_nofollow)
    355      1.1  thorpej 				error = extattr_list_link(argv[arg_counter],
    356      1.1  thorpej 				    attrnamespace, buf, buflen);
    357      1.1  thorpej 			else
    358      1.1  thorpej 				error = extattr_list_file(argv[arg_counter],
    359      1.1  thorpej 				    attrnamespace, buf, buflen);
    360      1.1  thorpej 			if (error < 0)
    361      1.1  thorpej 				break;
    362      1.1  thorpej 			if (!flag_quiet)
    363      1.1  thorpej 				printf("%s\t", argv[arg_counter]);
    364      1.7     manu 			for (i = 0; i < error; i += buf[i] + 1)
    365      1.7     manu 			    printf("%s%*.*s", i ? "\t" : "",
    366      1.7     manu 				buf[i], buf[i], buf + i + 1);
    367      1.1  thorpej 			printf("\n");
    368      1.1  thorpej 			continue;
    369      1.1  thorpej 		case EAGET:
    370      1.1  thorpej 			if (flag_nofollow)
    371      1.1  thorpej 				error = extattr_get_link(argv[arg_counter],
    372      1.1  thorpej 				    attrnamespace, attrname, NULL, 0);
    373      1.1  thorpej 			else
    374      1.1  thorpej 				error = extattr_get_file(argv[arg_counter],
    375      1.1  thorpej 				    attrnamespace, attrname, NULL, 0);
    376      1.1  thorpej 			if (error < 0)
    377      1.1  thorpej 				break;
    378      1.1  thorpej 			mkbuf(&buf, &buflen, error);
    379      1.1  thorpej 			if (flag_nofollow)
    380      1.1  thorpej 				error = extattr_get_link(argv[arg_counter],
    381      1.1  thorpej 				    attrnamespace, attrname, buf, buflen);
    382      1.1  thorpej 			else
    383      1.1  thorpej 				error = extattr_get_file(argv[arg_counter],
    384      1.1  thorpej 				    attrnamespace, attrname, buf, buflen);
    385      1.1  thorpej 			if (error < 0)
    386      1.1  thorpej 				break;
    387      1.1  thorpej 			if (!flag_quiet)
    388      1.1  thorpej 				printf("%s\t", argv[arg_counter]);
    389      1.8     manu 
    390      1.8     manu 			/*
    391      1.8     manu 			 * Check for binary string and terminal output
    392      1.8     manu 			 */
    393      1.8     manu #if 0
    394      1.8     manu 			for (i = 0; i < error; i++)
    395      1.8     manu 				if (!isprint((int)buf[i]))
    396      1.8     manu 					err(1, "binary data, use -x flag");
    397      1.8     manu #endif
    398      1.8     manu 
    399      1.8     manu 			if (flag_vis != -1) {
    400      1.1  thorpej 				mkbuf(&visbuf, &visbuflen, error * 4 + 1);
    401      1.8     manu 				strvisx(visbuf, buf, error, flag_vis);
    402      1.1  thorpej 				printf("\"%s\"\n", visbuf);
    403      1.1  thorpej 				continue;
    404      1.1  thorpej 			} else if (flag_hex) {
    405      1.1  thorpej 				printf("\n");
    406      1.8     manu 				hexdump(buf, error);
    407      1.1  thorpej 				continue;
    408      1.1  thorpej 			} else {
    409      1.1  thorpej 				fwrite(buf, buflen, 1, stdout);
    410      1.1  thorpej 				printf("\n");
    411      1.1  thorpej 				continue;
    412      1.1  thorpej 			}
    413      1.1  thorpej 		default:
    414      1.1  thorpej 			break;
    415      1.1  thorpej 		}
    416      1.1  thorpej 		if (!flag_quiet)
    417      1.1  thorpej 			warn("%s: failed", argv[arg_counter]);
    418      1.1  thorpej 		if (flag_force)
    419      1.1  thorpej 			continue;
    420      1.1  thorpej 		return(1);
    421      1.1  thorpej 	}
    422      1.1  thorpej 	return (0);
    423      1.1  thorpej }
    424