xref: /netbsd-src/usr.sbin/extattrctl/extattrctl.c (revision bec77c5f430f4d289623bb3bbdd1fe59434fd314)
1*bec77c5fSjoerg /*	$NetBSD: extattrctl.c,v 1.4 2011/08/31 13:32:36 joerg Exp $	*/
2e1afed9cSthorpej 
3e1afed9cSthorpej /*-
4e1afed9cSthorpej  * Copyright (c) 1999-2002 Robert N. M. Watson
5e1afed9cSthorpej  * All rights reserved.
6e1afed9cSthorpej  *
7e1afed9cSthorpej  * This software was developed by Robert Watson for the TrustedBSD Project.
8e1afed9cSthorpej  *
9e1afed9cSthorpej  * Redistribution and use in source and binary forms, with or without
10e1afed9cSthorpej  * modification, are permitted provided that the following conditions
11e1afed9cSthorpej  * are met:
12e1afed9cSthorpej  * 1. Redistributions of source code must retain the above copyright
13e1afed9cSthorpej  *    notice, this list of conditions and the following disclaimer.
14e1afed9cSthorpej  * 2. Redistributions in binary form must reproduce the above copyright
15e1afed9cSthorpej  *    notice, this list of conditions and the following disclaimer in the
16e1afed9cSthorpej  *    documentation and/or other materials provided with the distribution.
17e1afed9cSthorpej  *
18e1afed9cSthorpej  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19e1afed9cSthorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20e1afed9cSthorpej  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21e1afed9cSthorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22e1afed9cSthorpej  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23e1afed9cSthorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24e1afed9cSthorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25e1afed9cSthorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26e1afed9cSthorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27e1afed9cSthorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28e1afed9cSthorpej  * SUCH DAMAGE.
29e1afed9cSthorpej  *
30e1afed9cSthorpej  * $FreeBSD: src/usr.sbin/extattrctl/extattrctl.c,v 1.19 2002/04/19 01:42:55 rwatson Exp $
31e1afed9cSthorpej  */
32e1afed9cSthorpej 
33e1afed9cSthorpej /*
34e1afed9cSthorpej  * Developed by the TrustedBSD Project.
35e1afed9cSthorpej  * Support for file system extended attribute.
36e1afed9cSthorpej  */
37e1afed9cSthorpej 
38e1afed9cSthorpej #include <sys/types.h>
39e1afed9cSthorpej #include <sys/uio.h>
40e1afed9cSthorpej #include <sys/extattr.h>
41e1afed9cSthorpej #include <sys/param.h>
42e1afed9cSthorpej #include <sys/mount.h>
43e1afed9cSthorpej 
44e1afed9cSthorpej #include <ufs/ufs/extattr.h>
45e1afed9cSthorpej 
46e1afed9cSthorpej #include <err.h>
47e1afed9cSthorpej #include <fcntl.h>
48e1afed9cSthorpej #include <stdio.h>
49e1afed9cSthorpej #include <stdlib.h>
50e1afed9cSthorpej #include <string.h>
51e1afed9cSthorpej #include <unistd.h>
52e1afed9cSthorpej #include <util.h>
53e1afed9cSthorpej 
54e1afed9cSthorpej #include <machine/bswap.h>
55e1afed9cSthorpej #include <machine/endian.h>
56e1afed9cSthorpej 
57e1afed9cSthorpej static int needswap;
58e1afed9cSthorpej 
59e1afed9cSthorpej static uint32_t
rw32(uint32_t v)60e1afed9cSthorpej rw32(uint32_t v)
61e1afed9cSthorpej {
62e1afed9cSthorpej 	if (needswap)
63e1afed9cSthorpej 		return (bswap32(v));
64e1afed9cSthorpej 	return (v);
65e1afed9cSthorpej }
66e1afed9cSthorpej 
67*bec77c5fSjoerg __dead static void
usage(void)68e1afed9cSthorpej usage(void)
69e1afed9cSthorpej {
70e1afed9cSthorpej 
71e1afed9cSthorpej 	fprintf(stderr,
72e1afed9cSthorpej 	    "usage:\n"
73e1afed9cSthorpej 	    "  %s start path\n"
74e1afed9cSthorpej 	    "  %s stop path\n"
75e1afed9cSthorpej 	    "  %s initattr [-f] [-p path] attrsize attrfile\n"
76e1afed9cSthorpej 	    "  %s showattr attrfile\n"
77e1afed9cSthorpej 	    "  %s enable path attrnamespace attrname attrfile\n"
78e1afed9cSthorpej 	    "  %s disable path attrnamespace attrname\n",
79e1afed9cSthorpej 	    getprogname(), getprogname(), getprogname(),
80e1afed9cSthorpej 	    getprogname(), getprogname(), getprogname());
81e1afed9cSthorpej 	exit(1);
82e1afed9cSthorpej }
83e1afed9cSthorpej 
84e1afed9cSthorpej static uint64_t
num_inodes_by_path(const char * path)85e1afed9cSthorpej num_inodes_by_path(const char *path)
86e1afed9cSthorpej {
87e1afed9cSthorpej 	struct statvfs buf;
88e1afed9cSthorpej 
89e1afed9cSthorpej 	if (statvfs(path, &buf) == -1) {
90e1afed9cSthorpej 		warn("statvfs(%s)", path);
91e1afed9cSthorpej 		return (-1);
92e1afed9cSthorpej 	}
93e1afed9cSthorpej 
94e1afed9cSthorpej 	return (buf.f_files);
95e1afed9cSthorpej }
96e1afed9cSthorpej 
97e1afed9cSthorpej static const char zero_buf[8192];
98e1afed9cSthorpej 
99e1afed9cSthorpej static int
initattr(int argc,char * argv[])100e1afed9cSthorpej initattr(int argc, char *argv[])
101e1afed9cSthorpej {
102e1afed9cSthorpej 	struct ufs_extattr_fileheader uef;
103e1afed9cSthorpej 	char *fs_path = NULL;
104e1afed9cSthorpej 	int ch, i, error, flags;
105e1afed9cSthorpej 	ssize_t wlen;
106e1afed9cSthorpej 	size_t easize;
107e1afed9cSthorpej 
108e1afed9cSthorpej 	flags = O_CREAT | O_WRONLY | O_TRUNC | O_EXCL;
109e1afed9cSthorpej 	optind = 0;
110e1afed9cSthorpej 	while ((ch = getopt(argc, argv, "fp:r:w:")) != -1) {
111e1afed9cSthorpej 		switch (ch) {
112e1afed9cSthorpej 		case 'f':
113e1afed9cSthorpej 			flags &= ~O_EXCL;
114e1afed9cSthorpej 			break;
115e1afed9cSthorpej 		case 'p':
116e1afed9cSthorpej 			fs_path = optarg;
117e1afed9cSthorpej 			break;
118e1afed9cSthorpej 		case 'B':
119e1afed9cSthorpej #if BYTE_ORDER == LITTLE_ENDIAN
120e1afed9cSthorpej 			if (strcmp(optarg, "le") == 0)
121e1afed9cSthorpej 				needswap = 0;
122e1afed9cSthorpej 			else if (strcmp(optarg, "be") == 0)
123e1afed9cSthorpej 				needswap = 1;
124e1afed9cSthorpej 			else
125e1afed9cSthorpej 				usage();
126e1afed9cSthorpej #else
127e1afed9cSthorpej 			if (strcmp(optarg, "be") == 0)
128e1afed9cSthorpej 				needswap = 0;
129e1afed9cSthorpej 			else if (strcmp(optarg, "le") == 0)
130e1afed9cSthorpej 				needswap = 1;
131e1afed9cSthorpej 			else
132e1afed9cSthorpej 				usage();
133e1afed9cSthorpej #endif
134e1afed9cSthorpej 			break;
135e1afed9cSthorpej 		case '?':
136e1afed9cSthorpej 		default:
137e1afed9cSthorpej 			usage();
138e1afed9cSthorpej 		}
139e1afed9cSthorpej 	}
140e1afed9cSthorpej 
141e1afed9cSthorpej 	argc -= optind;
142e1afed9cSthorpej 	argv += optind;
143e1afed9cSthorpej 
144e1afed9cSthorpej 	if (argc != 2)
145e1afed9cSthorpej 		usage();
146e1afed9cSthorpej 
147e1afed9cSthorpej 	error = 0;
148e1afed9cSthorpej 	if ((i = open(argv[1], flags, 0600)) == -1) {
149e1afed9cSthorpej 		warn("open(%s)", argv[1]);
150e1afed9cSthorpej 		return (-1);
151e1afed9cSthorpej 	}
152e1afed9cSthorpej 	uef.uef_magic = rw32(UFS_EXTATTR_MAGIC);
153e1afed9cSthorpej 	uef.uef_version = rw32(UFS_EXTATTR_VERSION);
154e1afed9cSthorpej 	uef.uef_size = rw32(atoi(argv[0]));
155e1afed9cSthorpej 	if (write(i, &uef, sizeof(uef)) != sizeof(uef)) {
156f5e448fdSwiz 		warn("unable to write attribute file header");
157e1afed9cSthorpej 		error = -1;
158e1afed9cSthorpej 	} else if (fs_path != NULL) {
159e1afed9cSthorpej 		easize = (sizeof(uef) + uef.uef_size) *
160e1afed9cSthorpej 		    num_inodes_by_path(fs_path);
161e1afed9cSthorpej 		while (easize > 0) {
162e1afed9cSthorpej 			size_t x = (easize > sizeof(zero_buf)) ?
163e1afed9cSthorpej 			    sizeof(zero_buf) : easize;
164e1afed9cSthorpej 			wlen = write(i, zero_buf, x);
16598e270d7Slukem 			if ((size_t)wlen != x) {
166e1afed9cSthorpej 				warn("unable to write attribute file");
167e1afed9cSthorpej 				error = -1;
168e1afed9cSthorpej 				break;
169e1afed9cSthorpej 			}
170e1afed9cSthorpej 			easize -= wlen;
171e1afed9cSthorpej 		}
172e1afed9cSthorpej 	}
173f5e448fdSwiz 	close(i);
174e1afed9cSthorpej 	if (error == -1) {
175e1afed9cSthorpej 		unlink(argv[1]);
176e1afed9cSthorpej 		return (-1);
177e1afed9cSthorpej 	}
178e1afed9cSthorpej 
179e1afed9cSthorpej 	return (0);
180e1afed9cSthorpej }
181e1afed9cSthorpej 
182e1afed9cSthorpej static int
showattr(int argc,char * argv[])183e1afed9cSthorpej showattr(int argc, char *argv[])
184e1afed9cSthorpej {
185e1afed9cSthorpej 	struct ufs_extattr_fileheader uef;
186e1afed9cSthorpej 	int i, fd;
187e1afed9cSthorpej 	const char *bo;
188e1afed9cSthorpej 
189e1afed9cSthorpej 	if (argc != 1)
190e1afed9cSthorpej 		usage();
191e1afed9cSthorpej 
192e1afed9cSthorpej 	fd = open(argv[0], O_RDONLY);
193e1afed9cSthorpej 	if (fd == -1) {
194e1afed9cSthorpej 		warn("open(%s)", argv[0]);
195e1afed9cSthorpej 		return (-1);
196e1afed9cSthorpej 	}
197e1afed9cSthorpej 
198e1afed9cSthorpej 	i = read(fd, &uef, sizeof(uef));
199e1afed9cSthorpej 	if (i != sizeof(uef)) {
200e1afed9cSthorpej 		warn("unable to read attribute file header");
201f5e448fdSwiz 		(void)close(fd);
202e1afed9cSthorpej 		return (-1);
203e1afed9cSthorpej 	}
204e1afed9cSthorpej 
205e1afed9cSthorpej 	if (rw32(uef.uef_magic) != UFS_EXTATTR_MAGIC) {
206e1afed9cSthorpej 		needswap = 1;
207e1afed9cSthorpej 		if (rw32(uef.uef_magic) != UFS_EXTATTR_MAGIC) {
208e1afed9cSthorpej 			fprintf(stderr, "%s: bad magic\n", argv[0]);
209f5e448fdSwiz 			(void)close(fd);
210e1afed9cSthorpej 			return (-1);
211e1afed9cSthorpej 		}
212e1afed9cSthorpej 	}
213e1afed9cSthorpej 
214e1afed9cSthorpej #if BYTE_ORDER == LITTLE_ENDIAN
215e1afed9cSthorpej 	bo = needswap ? "big-endian" : "little-endian";
216e1afed9cSthorpej #else
217e1afed9cSthorpej 	bo = needswap ? "little-endian" : "big-endian";
218e1afed9cSthorpej #endif
219e1afed9cSthorpej 
220e1afed9cSthorpej 	printf("%s: version %u, size %u, byte-order: %s\n",
221e1afed9cSthorpej 	    argv[0], rw32(uef.uef_version), rw32(uef.uef_size), bo);
222e1afed9cSthorpej 
223f5e448fdSwiz 	close(fd);
224e1afed9cSthorpej 	return (0);
225e1afed9cSthorpej }
226e1afed9cSthorpej 
227e1afed9cSthorpej int
main(int argc,char * argv[])228e1afed9cSthorpej main(int argc, char *argv[])
229e1afed9cSthorpej {
230e1afed9cSthorpej 	int error = 0, attrnamespace;
231e1afed9cSthorpej 
232e1afed9cSthorpej 	if (argc < 2)
233e1afed9cSthorpej 		usage();
234e1afed9cSthorpej 
235e1afed9cSthorpej 	if (!strcmp(argv[1], "start")) {
236e1afed9cSthorpej 		if (argc != 3)
237e1afed9cSthorpej 			usage();
238e1afed9cSthorpej 		error = extattrctl(argv[2], UFS_EXTATTR_CMD_START, NULL, 0,
239e1afed9cSthorpej 		    NULL);
240e1afed9cSthorpej 		if (error)
241e1afed9cSthorpej 			err(1, "start");
242e1afed9cSthorpej 	} else if (!strcmp(argv[1], "stop")) {
243e1afed9cSthorpej 		if (argc != 3)
244e1afed9cSthorpej 			usage();
245e1afed9cSthorpej 		error = extattrctl(argv[2], UFS_EXTATTR_CMD_STOP, NULL, 0,
246e1afed9cSthorpej 		   NULL);
247e1afed9cSthorpej 		if (error)
248e1afed9cSthorpej 			err(1, "stop");
249e1afed9cSthorpej 	} else if (!strcmp(argv[1], "enable")) {
250e1afed9cSthorpej 		if (argc != 6)
251e1afed9cSthorpej 			usage();
252e1afed9cSthorpej 		error = extattr_string_to_namespace(argv[3], &attrnamespace);
253e1afed9cSthorpej 		if (error)
254e1afed9cSthorpej 			errx(1, "bad namespace: %s", argv[3]);
255e1afed9cSthorpej 		error = extattrctl(argv[2], UFS_EXTATTR_CMD_ENABLE, argv[5],
256e1afed9cSthorpej 		    attrnamespace, argv[4]);
257e1afed9cSthorpej 		if (error)
258e1afed9cSthorpej 			err(1, "enable");
259e1afed9cSthorpej 	} else if (!strcmp(argv[1], "disable")) {
260e1afed9cSthorpej 		if (argc != 5)
261e1afed9cSthorpej 			usage();
262e1afed9cSthorpej 		error = extattr_string_to_namespace(argv[3], &attrnamespace);
263e1afed9cSthorpej 		if (error)
264e1afed9cSthorpej 			errx(1, "bad namespace: %s", argv[3]);
265e1afed9cSthorpej 		error = extattrctl(argv[2], UFS_EXTATTR_CMD_DISABLE, NULL,
266e1afed9cSthorpej 		    attrnamespace, argv[4]);
267e1afed9cSthorpej 		if (error)
268e1afed9cSthorpej 			err(1, "disable");
269e1afed9cSthorpej 	} else if (!strcmp(argv[1], "initattr")) {
270e1afed9cSthorpej 		argc -= 2;
271e1afed9cSthorpej 		argv += 2;
272e1afed9cSthorpej 		error = initattr(argc, argv);
273e1afed9cSthorpej 		if (error)
274e1afed9cSthorpej 			return (1);
275e1afed9cSthorpej 	} else if (!strcmp(argv[1], "showattr")) {
276e1afed9cSthorpej 		argc -= 2;
277e1afed9cSthorpej 		argv += 2;
278e1afed9cSthorpej 		error = showattr(argc, argv);
279e1afed9cSthorpej 		if (error)
280e1afed9cSthorpej 			return (1);
281e1afed9cSthorpej 	} else
282e1afed9cSthorpej 		usage();
283e1afed9cSthorpej 
284e1afed9cSthorpej 	return (0);
285e1afed9cSthorpej }
286