getextattr.c revision 1.3.26.1 1 1.3.26.1 riz /* $NetBSD: getextattr.c,v 1.3.26.1 2011/08/09 15:55:55 riz 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.3.26.1 riz #include <fcntl.h>
54 1.3.26.1 riz #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.1 thorpej 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.3.26.1 riz 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.3.26.1 riz fprintf(stderr, "usage: %s [-fhnq] -i attrvalue_file "
74 1.3.26.1 riz "attrnamespace attrname filename ...\n",
75 1.3.26.1 riz 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.3.26.1 riz static int
112 1.3.26.1 riz parse_flag_vis(const char *opt)
113 1.3.26.1 riz {
114 1.3.26.1 riz if (strcmp(opt, "default") == 0)
115 1.3.26.1 riz return 0;
116 1.3.26.1 riz else if (strcmp(opt, "cstyle") == 0)
117 1.3.26.1 riz return VIS_CSTYLE;
118 1.3.26.1 riz else if (strcmp(opt, "octal") == 0)
119 1.3.26.1 riz return VIS_OCTAL;
120 1.3.26.1 riz else if (strcmp(opt, "httpstyle") == 0)
121 1.3.26.1 riz return VIS_HTTPSTYLE;
122 1.3.26.1 riz
123 1.3.26.1 riz /* Convenient aliases */
124 1.3.26.1 riz else if (strcmp(opt, "vis") == 0)
125 1.3.26.1 riz return 0;
126 1.3.26.1 riz else if (strcmp(opt, "c") == 0)
127 1.3.26.1 riz return VIS_CSTYLE;
128 1.3.26.1 riz else if (strcmp(opt, "http") == 0)
129 1.3.26.1 riz return VIS_HTTPSTYLE;
130 1.3.26.1 riz else
131 1.3.26.1 riz fprintf(stderr, "%s: invalid -s option \"%s\"",
132 1.3.26.1 riz getprogname(), opt);
133 1.3.26.1 riz
134 1.3.26.1 riz return -1;
135 1.3.26.1 riz }
136 1.3.26.1 riz
137 1.3.26.1 riz #define HEXDUMP_PRINT(x) ((uint8_t)x >= 32 && (uint8_t)x < 128) ? x : '.'
138 1.3.26.1 riz static void
139 1.3.26.1 riz hexdump(const char *addr, size_t len)
140 1.3.26.1 riz {
141 1.3.26.1 riz unsigned int i, j;
142 1.3.26.1 riz
143 1.3.26.1 riz for (i = 0; i < len; i += 16) {
144 1.3.26.1 riz printf(" %03x ", i);
145 1.3.26.1 riz for (j = 0; j < 16; j++) {
146 1.3.26.1 riz if (i + j > len)
147 1.3.26.1 riz printf(" ");
148 1.3.26.1 riz else
149 1.3.26.1 riz printf("%02x ", addr[i + j] & 0xff);
150 1.3.26.1 riz }
151 1.3.26.1 riz printf(" ");
152 1.3.26.1 riz for (j = 0; j < 16; j++) {
153 1.3.26.1 riz if (i + j > len)
154 1.3.26.1 riz printf(" ");
155 1.3.26.1 riz else
156 1.3.26.1 riz printf("%c", HEXDUMP_PRINT(addr[i + j]));
157 1.3.26.1 riz }
158 1.3.26.1 riz printf("\n");
159 1.3.26.1 riz }
160 1.3.26.1 riz }
161 1.3.26.1 riz #undef HEXDUMP_PRINT
162 1.3.26.1 riz
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.3.26.1 riz 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.3.26.1 riz int flag_vis = -1;
178 1.1 thorpej int flag_hex = 0;
179 1.3.26.1 riz 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.3.26.1 riz options = "fhqsxv:";
190 1.3.26.1 riz minargc = 2;
191 1.1 thorpej } else if (strcmp(p, "setextattr") == 0) {
192 1.1 thorpej what = EASET;
193 1.3.26.1 riz options = "fhnqi:";
194 1.3.26.1 riz 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.3.26.1 riz 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.3.26.1 riz flag_vis = VIS_SAFE | VIS_WHITE;
222 1.3.26.1 riz break;
223 1.3.26.1 riz case 'v':
224 1.3.26.1 riz 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.3.26.1 riz case 'i':
230 1.3.26.1 riz filename = optarg;
231 1.3.26.1 riz minargc--;
232 1.3.26.1 riz 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.3.26.1 riz /*
242 1.3.26.1 riz * Check for missing argument.
243 1.3.26.1 riz */
244 1.1 thorpej if (argc < minargc)
245 1.1 thorpej usage();
246 1.1 thorpej
247 1.3.26.1 riz /*
248 1.3.26.1 riz * Normal case "namespace attribute".
249 1.3.26.1 riz */
250 1.1 thorpej error = extattr_string_to_namespace(argv[0], &attrnamespace);
251 1.3.26.1 riz if (error == 0) {
252 1.3.26.1 riz /*
253 1.3.26.1 riz * Namespace was specified, so we need one more argument
254 1.3.26.1 riz * for the attribute (except for listing)
255 1.3.26.1 riz */
256 1.3.26.1 riz if ((what != EALS) && (argc < minargc + 1))
257 1.3.26.1 riz usage();
258 1.3.26.1 riz argc--; argv++;
259 1.3.26.1 riz } else {
260 1.3.26.1 riz /*
261 1.3.26.1 riz * The namespace was not valid. Perhaps it was omited.
262 1.3.26.1 riz * Try to guess a missing namespace by using
263 1.3.26.1 riz * linux layout "namespace.attribute". While
264 1.3.26.1 riz * we are here, also test the Linux namespaces.
265 1.3.26.1 riz */
266 1.3.26.1 riz if (strstr(argv[0], "user.") == argv[0]) {
267 1.3.26.1 riz attrnamespace = EXTATTR_NAMESPACE_USER;
268 1.3.26.1 riz } else
269 1.3.26.1 riz if ((strstr(argv[0], "system.") == argv[0]) ||
270 1.3.26.1 riz (strstr(argv[0], "trusted.") == argv[0]) ||
271 1.3.26.1 riz (strstr(argv[0], "security.") == argv[0])) {
272 1.3.26.1 riz attrnamespace = EXTATTR_NAMESPACE_SYSTEM;
273 1.3.26.1 riz } else {
274 1.3.26.1 riz err(1, "%s", argv[0]);
275 1.3.26.1 riz }
276 1.3.26.1 riz }
277 1.1 thorpej
278 1.1 thorpej if (what != EALS) {
279 1.1 thorpej attrname = argv[0];
280 1.1 thorpej argc--; argv++;
281 1.1 thorpej } else
282 1.1 thorpej attrname = NULL;
283 1.1 thorpej
284 1.1 thorpej if (what == EASET) {
285 1.3.26.1 riz /*
286 1.3.26.1 riz * Handle -i option, reading value from a file.
287 1.3.26.1 riz */
288 1.3.26.1 riz if (filename != NULL) {
289 1.3.26.1 riz int fd;
290 1.3.26.1 riz struct stat st;
291 1.3.26.1 riz ssize_t readen, remain;
292 1.3.26.1 riz
293 1.3.26.1 riz if ((fd = open(filename, O_RDONLY, 0)) == -1)
294 1.3.26.1 riz err(1, "%s: cannot open \"%s\"",
295 1.3.26.1 riz getprogname(), filename);
296 1.3.26.1 riz
297 1.3.26.1 riz if (fstat(fd, &st) != 0)
298 1.3.26.1 riz err(1, "%s: cannot stat \"%s\"",
299 1.3.26.1 riz getprogname(), filename);
300 1.3.26.1 riz
301 1.3.26.1 riz val_len = st.st_size;
302 1.3.26.1 riz mkbuf(&buf, &buflen, val_len);
303 1.3.26.1 riz
304 1.3.26.1 riz for (remain = val_len; remain > 0; remain -= readen) {
305 1.3.26.1 riz if ((readen = read(fd, buf, remain)) == -1)
306 1.3.26.1 riz err(1, "%s: cannot read \"%s\"",
307 1.3.26.1 riz getprogname(), filename);
308 1.3.26.1 riz }
309 1.3.26.1 riz
310 1.3.26.1 riz (void)close(fd);
311 1.3.26.1 riz } else {
312 1.3.26.1 riz val_len = strlen(argv[0]) + 1;
313 1.3.26.1 riz mkbuf(&buf, &buflen, val_len);
314 1.3.26.1 riz strcpy(buf, argv[0]); /* safe */
315 1.3.26.1 riz argc--; argv++;
316 1.3.26.1 riz }
317 1.1 thorpej }
318 1.1 thorpej
319 1.1 thorpej for (arg_counter = 0; arg_counter < argc; arg_counter++) {
320 1.1 thorpej switch (what) {
321 1.1 thorpej case EARM:
322 1.1 thorpej if (flag_nofollow)
323 1.1 thorpej error = extattr_delete_link(argv[arg_counter],
324 1.1 thorpej attrnamespace, attrname);
325 1.1 thorpej else
326 1.1 thorpej error = extattr_delete_file(argv[arg_counter],
327 1.1 thorpej attrnamespace, attrname);
328 1.1 thorpej if (error >= 0)
329 1.1 thorpej continue;
330 1.1 thorpej break;
331 1.1 thorpej case EASET:
332 1.1 thorpej if (flag_nofollow)
333 1.1 thorpej error = extattr_set_link(argv[arg_counter],
334 1.1 thorpej attrnamespace, attrname, buf,
335 1.3.26.1 riz val_len + flag_null);
336 1.1 thorpej else
337 1.1 thorpej error = extattr_set_file(argv[arg_counter],
338 1.1 thorpej attrnamespace, attrname, buf,
339 1.3.26.1 riz val_len + flag_null);
340 1.1 thorpej if (error >= 0)
341 1.1 thorpej continue;
342 1.1 thorpej break;
343 1.1 thorpej case EALS:
344 1.1 thorpej if (flag_nofollow)
345 1.1 thorpej error = extattr_list_link(argv[arg_counter],
346 1.1 thorpej attrnamespace, NULL, 0);
347 1.1 thorpej else
348 1.1 thorpej error = extattr_list_file(argv[arg_counter],
349 1.1 thorpej attrnamespace, NULL, 0);
350 1.1 thorpej if (error < 0)
351 1.1 thorpej break;
352 1.1 thorpej mkbuf(&buf, &buflen, error);
353 1.1 thorpej if (flag_nofollow)
354 1.1 thorpej error = extattr_list_link(argv[arg_counter],
355 1.1 thorpej attrnamespace, buf, buflen);
356 1.1 thorpej else
357 1.1 thorpej error = extattr_list_file(argv[arg_counter],
358 1.1 thorpej attrnamespace, buf, buflen);
359 1.1 thorpej if (error < 0)
360 1.1 thorpej break;
361 1.1 thorpej if (!flag_quiet)
362 1.1 thorpej printf("%s\t", argv[arg_counter]);
363 1.1 thorpej for (i = 0; i < error; i += buf[i] + 1)
364 1.1 thorpej printf("%s%*.*s", i ? "\t" : "",
365 1.1 thorpej buf[i], buf[i], buf + i + 1);
366 1.1 thorpej printf("\n");
367 1.1 thorpej continue;
368 1.1 thorpej case EAGET:
369 1.1 thorpej if (flag_nofollow)
370 1.1 thorpej error = extattr_get_link(argv[arg_counter],
371 1.1 thorpej attrnamespace, attrname, NULL, 0);
372 1.1 thorpej else
373 1.1 thorpej error = extattr_get_file(argv[arg_counter],
374 1.1 thorpej attrnamespace, attrname, NULL, 0);
375 1.1 thorpej if (error < 0)
376 1.1 thorpej break;
377 1.1 thorpej mkbuf(&buf, &buflen, error);
378 1.1 thorpej if (flag_nofollow)
379 1.1 thorpej error = extattr_get_link(argv[arg_counter],
380 1.1 thorpej attrnamespace, attrname, buf, buflen);
381 1.1 thorpej else
382 1.1 thorpej error = extattr_get_file(argv[arg_counter],
383 1.1 thorpej attrnamespace, attrname, buf, buflen);
384 1.1 thorpej if (error < 0)
385 1.1 thorpej break;
386 1.1 thorpej if (!flag_quiet)
387 1.1 thorpej printf("%s\t", argv[arg_counter]);
388 1.3.26.1 riz
389 1.3.26.1 riz /*
390 1.3.26.1 riz * Check for binary string and terminal output
391 1.3.26.1 riz */
392 1.3.26.1 riz #if 0
393 1.3.26.1 riz for (i = 0; i < error; i++)
394 1.3.26.1 riz if (!isprint((int)buf[i]))
395 1.3.26.1 riz err(1, "binary data, use -x flag");
396 1.3.26.1 riz #endif
397 1.3.26.1 riz
398 1.3.26.1 riz if (flag_vis != -1) {
399 1.1 thorpej mkbuf(&visbuf, &visbuflen, error * 4 + 1);
400 1.3.26.1 riz strvisx(visbuf, buf, error, flag_vis);
401 1.1 thorpej printf("\"%s\"\n", visbuf);
402 1.1 thorpej continue;
403 1.1 thorpej } else if (flag_hex) {
404 1.1 thorpej printf("\n");
405 1.3.26.1 riz hexdump(buf, error);
406 1.1 thorpej continue;
407 1.1 thorpej } else {
408 1.1 thorpej fwrite(buf, buflen, 1, stdout);
409 1.1 thorpej printf("\n");
410 1.1 thorpej continue;
411 1.1 thorpej }
412 1.1 thorpej default:
413 1.1 thorpej break;
414 1.1 thorpej }
415 1.1 thorpej if (!flag_quiet)
416 1.1 thorpej warn("%s: failed", argv[arg_counter]);
417 1.1 thorpej if (flag_force)
418 1.1 thorpej continue;
419 1.1 thorpej return(1);
420 1.1 thorpej }
421 1.1 thorpej return (0);
422 1.1 thorpej }
423