summaryrefslogtreecommitdiff
path: root/usr.sbin/hostctl/hostctl.c
blob: dbaf1eb0d6ce3cab14bfaa124cb7e96cef17e61b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*	$OpenBSD: hostctl.c,v 1.4 2018/07/03 16:42:51 sf Exp $	*/

/*
 * Copyright (c) 2016 Reyk Floeter <reyk@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/ioctl.h>
#include <sys/types.h>

#include <dev/pv/pvvar.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <err.h>
#include <vis.h>

#define KVBUFSZ	 4096				/* arbitrary value */

char		*path_pvbus = "/dev/pvbus0";	/* the first hv interface */
int		 qflag = 0;			/* quiet */
int		 tflag = 0;			/* show type */

__dead void	 usage(void);
int		 kvsetstr(char *, const char *, size_t);
int		 kvsetfile(char *, const char *, size_t);

__dead void
usage(void)
{
	extern char	*__progname;
	fprintf(stderr, "usage: %s [-qt] [-f device] "
	    "[-i input] [-o output] key [value]\n", __progname);
	exit(1);
}

int
kvsetstr(char *dst, const char *src, size_t dstlen)
{
	size_t	sz;

	/* Sanitize the string before sending it to the kernel and host */
	if ((sz = strnvis(dst, src, dstlen, VIS_SAFE | VIS_CSTYLE)) >= dstlen)
		return (-1);

	/* Remove trailing newline */
	dst[strcspn(dst, "\n")] = '\0';

	return (0);
}

int
kvsetfile(char *dst, const char *input, size_t dstlen)
{
	char	*buf = NULL;
	int	 ret = -1;
	FILE	*fp;

	if (strcmp("-", input) == 0)
		fp = stdin;
	else if ((fp = fopen(input, "r")) == NULL)
		return (-1);

	if ((buf = calloc(1, dstlen)) == NULL)
		goto done;
	if (fread(buf, 1, dstlen - 1, fp) == 0)
		goto done;
	if (kvsetstr(dst, buf, dstlen) == -1)
		goto done;

	ret = 0;
 done:
	free(buf);
	if (fp != stdin)
		fclose(fp);
	return (ret);
}

int
main(int argc, char *argv[])
{
	const char		*key, *value, *in = NULL, *out = NULL;
	FILE			*outfp = stdout;
	int			fd, ret;
	struct pvbus_req	pvr;
	int			ch;
	unsigned long		cmd = 0;
	char			*str;

	while ((ch = getopt(argc, argv, "f:i:o:qt")) != -1) {
		switch (ch) {
		case 'f':
			path_pvbus = optarg;
			break;
		case 'i':
			in = optarg;
			break;
		case 'o':
			out = optarg;
			break;
		case 'q':
			qflag++;
			break;
		case 't':
			tflag++;
			break;
		default:
			usage();
		}
	}
	argc -= optind;
	argv += optind;

	if ((fd = open(path_pvbus, O_RDONLY)) == -1)
		err(1, "open: %s", path_pvbus);

	if (out != NULL) {
		if (strcmp("-", out) == 0)
			outfp = stdout;
		else if ((outfp = fopen(out, "w")) == NULL)
			err(1, "fopen: %s", out);
	}

	memset(&pvr, 0, sizeof(pvr));
	pvr.pvr_keylen = pvr.pvr_valuelen = KVBUFSZ;
	if ((pvr.pvr_key = calloc(1, pvr.pvr_keylen)) == NULL ||
	    (pvr.pvr_value = calloc(1, pvr.pvr_valuelen)) == NULL)
		err(1, "calloc");

	if (tflag) {
		if (ioctl(fd, PVBUSIOC_TYPE, &pvr, sizeof(pvr)) != 0)
			err(1, "ioctl");

		/* The returned type should be a simple single-line key */
		if (stravis(&str, pvr.pvr_key,
		    VIS_WHITE | VIS_DQ | VIS_CSTYLE) == -1)
			err(1, "stravis");
		fprintf(outfp, "%s: %s\n", path_pvbus, str);
		free(str);
		goto done;
	}

	if (argc < 1)
		usage();
	key = argv[0];

	if (kvsetstr(pvr.pvr_key, key, pvr.pvr_keylen) == -1)
		errx(1, "key too long");

	/* Validate command line options for reading or writing */
	if (argc == 2 && in == NULL) {
		cmd = PVBUSIOC_KVWRITE;
		value = argv[1];
		if (kvsetstr(pvr.pvr_value, value, pvr.pvr_valuelen) == -1)
			errx(1, "value too long");
	} else if (argc == 1 && in != NULL) {
		cmd = PVBUSIOC_KVWRITE;
		if (kvsetfile(pvr.pvr_value, in, pvr.pvr_valuelen) == -1)
			errx(1, "input file");
	} else if (argc == 1) {
		cmd = cmd == 0 ? PVBUSIOC_KVREAD : cmd;
	} else
		usage();

	/* Re-open read-writable */
	if (cmd == PVBUSIOC_KVWRITE) {
		close(fd);
		if ((fd = open(path_pvbus, O_RDWR)) == -1)
			err(1, "open: %s", path_pvbus);
	}

	if ((ret = ioctl(fd, cmd, &pvr, sizeof(pvr))) != 0)
		err(1, "ioctl");

	if (!qflag && strlen(pvr.pvr_value)) {
		/*
		 * The value can contain newlines and basically anything;
		 * only encode the unsafe characters that could perform
		 * unexpected functions on the terminal.
		 */
		if (stravis(&str, pvr.pvr_value, VIS_SAFE | VIS_CSTYLE) == -1)
			err(1, "stravis");
		fprintf(outfp, "%s\n", str);
		free(str);
	}

 done:
	if (outfp != stdout)
		fclose(outfp);
	free(pvr.pvr_value);
	free(pvr.pvr_key);
	close(fd);

	return (0);
}