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