xref: /freebsd/usr.sbin/extattr/rmextattr.c (revision 68d75eff68281c1b445e3010bb975eae07aac225)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
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$
39  */
40 
41 #include <sys/types.h>
42 #include <sys/sbuf.h>
43 #include <sys/uio.h>
44 #include <sys/extattr.h>
45 
46 #include <libgen.h>
47 #include <libutil.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <unistd.h>
52 #include <vis.h>
53 #include <err.h>
54 #include <errno.h>
55 
56 static enum { EADUNNO, EAGET, EASET, EARM, EALS } what = EADUNNO;
57 
58 static void __dead2
59 usage(void)
60 {
61 
62 	switch (what) {
63 	case EAGET:
64 		fprintf(stderr, "usage: getextattr [-fhqsx] attrnamespace");
65 		fprintf(stderr, " attrname filename ...\n");
66 		exit(-1);
67 	case EASET:
68 		fprintf(stderr, "usage: setextattr [-fhnq] attrnamespace");
69 		fprintf(stderr, " attrname attrvalue filename ...\n");
70 		fprintf(stderr, "   or  setextattr -i [-fhnq] attrnamespace");
71 		fprintf(stderr, " attrname filename ...\n");
72 		exit(-1);
73 	case EARM:
74 		fprintf(stderr, "usage: rmextattr [-fhq] attrnamespace");
75 		fprintf(stderr, " attrname filename ...\n");
76 		exit(-1);
77 	case EALS:
78 		fprintf(stderr, "usage: lsextattr [-fhq] attrnamespace");
79 		fprintf(stderr, " filename ...\n");
80 		exit(-1);
81 	case EADUNNO:
82 	default:
83 		fprintf(stderr, "usage: (getextattr|lsextattr|rmextattr");
84 		fprintf(stderr, "|setextattr)\n");
85 		exit (-1);
86 	}
87 }
88 
89 static void
90 mkbuf(char **buf, int *oldlen, int newlen)
91 {
92 
93 	if (*oldlen >= newlen)
94 		return;
95 	if (*buf != NULL)
96 		free(*buf);
97 	*buf = malloc(newlen);
98 	if (*buf == NULL)
99 		err(1, "malloc");
100 	*oldlen = newlen;
101 	return;
102 }
103 
104 int
105 main(int argc, char *argv[])
106 {
107 #define STDIN_BUF_SZ 4096
108 	char	 stdin_data[STDIN_BUF_SZ];
109 	char	*p;
110 
111 	const char *options, *attrname;
112 	size_t	len;
113 	ssize_t	ret;
114 	int	 ch, error, i, arg_counter, attrnamespace, minargc;
115 
116 	char   *visbuf = NULL;
117 	int	visbuflen = 0;
118 	char   *buf = NULL;
119 	int	buflen = 0;
120 	struct	sbuf *attrvalue = NULL;
121 	int	flag_force = 0;
122 	int	flag_nofollow = 0;
123 	int	flag_null = 0;
124 	int	count_quiet = 0;
125 	int	flag_from_stdin = 0;
126 	int	flag_string = 0;
127 	int	flag_hex = 0;
128 
129 	p = basename(argv[0]);
130 	if (p == NULL)
131 		p = argv[0];
132 	if (!strcmp(p, "getextattr")) {
133 		what = EAGET;
134 		options = "fhqsx";
135 		minargc = 3;
136 	} else if (!strcmp(p, "setextattr")) {
137 		what = EASET;
138 		options = "fhinq";
139 		minargc = 3;
140 	} else if (!strcmp(p, "rmextattr")) {
141 		what = EARM;
142 		options = "fhq";
143 		minargc = 3;
144 	} else if (!strcmp(p, "lsextattr")) {
145 		what = EALS;
146 		options = "fhq";
147 		minargc = 2;
148 	} else {
149 		usage();
150 	}
151 
152 	while ((ch = getopt(argc, argv, options)) != -1) {
153 		switch (ch) {
154 		case 'f':
155 			flag_force = 1;
156 			break;
157 		case 'h':
158 			flag_nofollow = 1;
159 			break;
160 		case 'i':
161 			flag_from_stdin = 1;
162 			break;
163 		case 'n':
164 			flag_null = 1;
165 			break;
166 		case 'q':
167 			count_quiet += 1;
168 			break;
169 		case 's':
170 			flag_string = 1;
171 			break;
172 		case 'x':
173 			flag_hex = 1;
174 			break;
175 		case '?':
176 		default:
177 			usage();
178 		}
179 	}
180 
181 	argc -= optind;
182 	argv += optind;
183 
184 	if (what == EASET && flag_from_stdin == 0)
185 		minargc++;
186 
187 	if (argc < minargc)
188 		usage();
189 
190 	error = extattr_string_to_namespace(argv[0], &attrnamespace);
191 	if (error)
192 		err(-1, "%s", argv[0]);
193 	argc--; argv++;
194 
195 	if (what != EALS) {
196 		attrname = argv[0];
197 		argc--; argv++;
198 	} else
199 		attrname = NULL;
200 
201 	if (what == EASET) {
202 		attrvalue = sbuf_new_auto();
203 		if (flag_from_stdin) {
204 			while ((error = read(0, stdin_data, STDIN_BUF_SZ)) > 0)
205 				sbuf_bcat(attrvalue, stdin_data, error);
206 		} else {
207 			sbuf_cpy(attrvalue, argv[0]);
208 			argc--; argv++;
209 		}
210 		sbuf_finish(attrvalue);
211 	}
212 
213 	for (arg_counter = 0; arg_counter < argc; arg_counter++) {
214 		switch (what) {
215 		case EARM:
216 			if (flag_nofollow)
217 				error = extattr_delete_link(argv[arg_counter],
218 				    attrnamespace, attrname);
219 			else
220 				error = extattr_delete_file(argv[arg_counter],
221 				    attrnamespace, attrname);
222 			if (error >= 0)
223 				continue;
224 			break;
225 		case EASET:
226 			len = sbuf_len(attrvalue) + flag_null;
227 			if (flag_nofollow)
228 				ret = extattr_set_link(argv[arg_counter],
229 				    attrnamespace, attrname,
230 				    sbuf_data(attrvalue), len);
231 			else
232 				ret = extattr_set_file(argv[arg_counter],
233 				    attrnamespace, attrname,
234 				    sbuf_data(attrvalue), len);
235 			if (ret >= 0) {
236 				if ((size_t)ret != len && !count_quiet) {
237 					warnx("Set %zd bytes of %zu for %s",
238 					    ret, len, attrname);
239 				}
240 				continue;
241 			}
242 			break;
243 		case EALS:
244 			if (flag_nofollow)
245 				ret = extattr_list_link(argv[arg_counter],
246 				    attrnamespace, NULL, 0);
247 			else
248 				ret = extattr_list_file(argv[arg_counter],
249 				    attrnamespace, NULL, 0);
250 			if (ret < 0)
251 				break;
252 			mkbuf(&buf, &buflen, ret);
253 			if (flag_nofollow)
254 				ret = extattr_list_link(argv[arg_counter],
255 				    attrnamespace, buf, buflen);
256 			else
257 				ret = extattr_list_file(argv[arg_counter],
258 				    attrnamespace, buf, buflen);
259 			if (ret < 0)
260 				break;
261 			if (!count_quiet)
262 				printf("%s\t", argv[arg_counter]);
263 			for (i = 0; i < ret; i += ch + 1) {
264 			    /* The attribute name length is unsigned. */
265 			    ch = (unsigned char)buf[i];
266 			    printf("%s%*.*s", i ? "\t" : "",
267 				ch, ch, buf + i + 1);
268 			}
269 			if (!count_quiet || ret > 0)
270 				printf("\n");
271 			continue;
272 		case EAGET:
273 			if (flag_nofollow)
274 				ret = extattr_get_link(argv[arg_counter],
275 				    attrnamespace, attrname, NULL, 0);
276 			else
277 				ret = extattr_get_file(argv[arg_counter],
278 				    attrnamespace, attrname, NULL, 0);
279 			if (ret < 0)
280 				break;
281 			mkbuf(&buf, &buflen, ret);
282 			if (flag_nofollow)
283 				ret = extattr_get_link(argv[arg_counter],
284 				    attrnamespace, attrname, buf, buflen);
285 			else
286 				ret = extattr_get_file(argv[arg_counter],
287 				    attrnamespace, attrname, buf, buflen);
288 			if (ret < 0)
289 				break;
290 			if (!count_quiet)
291 				printf("%s\t", argv[arg_counter]);
292 			if (flag_string) {
293 				mkbuf(&visbuf, &visbuflen, ret * 4 + 1);
294 				strvisx(visbuf, buf, ret,
295 				    VIS_SAFE | VIS_WHITE);
296 				printf("\"%s\"", visbuf);
297 			} else if (flag_hex) {
298 				for (i = 0; i < ret; i++)
299 					printf("%s%02x", i ? " " : "",
300 							(unsigned char)buf[i]);
301 			} else {
302 				fwrite(buf, ret, 1, stdout);
303 			}
304 			if (count_quiet < 2)
305 				printf("\n");
306 			continue;
307 		default:
308 			break;
309 		}
310 		if (!count_quiet)
311 			warn("%s: failed", argv[arg_counter]);
312 		if (flag_force)
313 			continue;
314 		return(1);
315 	}
316 	return (0);
317 }
318