2 fb1a36c0 2022-01-09 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 fb1a36c0 2022-01-09 op * Permission to use, copy, modify, and distribute this software for any
5 fb1a36c0 2022-01-09 op * purpose with or without fee is hereby granted, provided that the above
6 fb1a36c0 2022-01-09 op * copyright notice and this permission notice appear in all copies.
8 fb1a36c0 2022-01-09 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 fb1a36c0 2022-01-09 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 fb1a36c0 2022-01-09 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 fb1a36c0 2022-01-09 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 fb1a36c0 2022-01-09 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 fb1a36c0 2022-01-09 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 fb1a36c0 2022-01-09 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 bbcba3ed 2022-01-10 op #include "compat.h"
19 fb1a36c0 2022-01-09 op #include <sys/types.h>
20 fb1a36c0 2022-01-09 op #include <sys/socket.h>
22 fb1a36c0 2022-01-09 op #include <netdb.h>
24 fb1a36c0 2022-01-09 op #include <assert.h>
25 fb1a36c0 2022-01-09 op #include <endian.h>
26 fb1a36c0 2022-01-09 op #include <errno.h>
27 fb1a36c0 2022-01-09 op #include <fcntl.h>
28 fb1a36c0 2022-01-09 op #include <inttypes.h>
29 fb1a36c0 2022-01-09 op #include <signal.h>
30 fb1a36c0 2022-01-09 op #include <stdio.h>
31 fb1a36c0 2022-01-09 op #include <stdlib.h>
32 fb1a36c0 2022-01-09 op #include <string.h>
33 fb1a36c0 2022-01-09 op #include <syslog.h>
34 fb1a36c0 2022-01-09 op #include <tls.h>
35 fb1a36c0 2022-01-09 op #include <unistd.h>
37 fb1a36c0 2022-01-09 op #include "9pclib.h"
38 fb1a36c0 2022-01-09 op #include "kami.h"
39 fb1a36c0 2022-01-09 op #include "log.h"
40 fb1a36c0 2022-01-09 op #include "utils.h"
42 fb1a36c0 2022-01-09 op #define DEBUG_PACKETS 0
44 fb1a36c0 2022-01-09 op #define PROMPT "=% "
49 fb1a36c0 2022-01-09 op const char *keypath;
50 fb1a36c0 2022-01-09 op const char *crtpath;
51 fb1a36c0 2022-01-09 op const char *host;
52 fb1a36c0 2022-01-09 op const char *port;
55 fb1a36c0 2022-01-09 op struct tls_config *tlsconf;
56 fb1a36c0 2022-01-09 op struct tls *ctx;
57 fb1a36c0 2022-01-09 op struct bufferevent *bev, *inbev;
59 fb1a36c0 2022-01-09 op static void __dead usage(int);
61 fb1a36c0 2022-01-09 op static void sig_handler(int, short, void *);
63 fb1a36c0 2022-01-09 op static int openconn(void);
64 fb1a36c0 2022-01-09 op static void mark_nonblock(int);
66 fb1a36c0 2022-01-09 op static void tls_readcb(int, short, void *);
67 fb1a36c0 2022-01-09 op static void tls_writecb(int, short, void *);
69 fb1a36c0 2022-01-09 op static void client_read(struct bufferevent *, void *);
70 fb1a36c0 2022-01-09 op static void client_write(struct bufferevent *, void *);
71 fb1a36c0 2022-01-09 op static void client_error(struct bufferevent *, short, void *);
73 fb1a36c0 2022-01-09 op static void repl_read(struct bufferevent *, void *);
74 fb1a36c0 2022-01-09 op static void repl_error(struct bufferevent *, short, void *);
76 fb1a36c0 2022-01-09 op static void excmd_version(const char **, int);
77 fb1a36c0 2022-01-09 op static void excmd_attach(const char **, int);
78 fb1a36c0 2022-01-09 op static void excmd_clunk(const char **, int);
79 fb1a36c0 2022-01-09 op static void excmd_flush(const char **, int);
80 fb1a36c0 2022-01-09 op static void excmd_walk(const char ** , int);
81 fb1a36c0 2022-01-09 op static void excmd_open(const char ** , int);
82 fb1a36c0 2022-01-09 op static void excmd_create(const char ** , int);
83 fb1a36c0 2022-01-09 op static void excmd_read(const char ** , int);
84 fb1a36c0 2022-01-09 op static void excmd_write(const char **, int);
85 fb1a36c0 2022-01-09 op static void excmd(const char **, int);
87 fb1a36c0 2022-01-09 op static void pp_qid(const uint8_t *, uint32_t);
88 fb1a36c0 2022-01-09 op static void pp_msg(uint32_t, uint8_t, uint16_t, const uint8_t *);
89 fb1a36c0 2022-01-09 op static void handle_9p(const uint8_t *, size_t);
90 fb1a36c0 2022-01-09 op static void clr(void);
91 fb1a36c0 2022-01-09 op static void prompt(void);
93 fb1a36c0 2022-01-09 op static void __dead
94 fb1a36c0 2022-01-09 op usage(int ret)
96 fb1a36c0 2022-01-09 op fprintf(stderr,
97 fb1a36c0 2022-01-09 op "usage: %s [-chv] [-C crt] [-K key] [-H host] [-P port]\n",
98 fb1a36c0 2022-01-09 op getprogname());
99 fb1a36c0 2022-01-09 op fprintf(stderr, "kamid suite version " KAMID_VERSION "\n");
104 fb1a36c0 2022-01-09 op sig_handler(int sig, short event, void *d)
107 fb1a36c0 2022-01-09 op * Normal signal handler rules don't apply because libevent
108 fb1a36c0 2022-01-09 op * decouples for us.
111 fb1a36c0 2022-01-09 op switch (sig) {
113 fb1a36c0 2022-01-09 op case SIGTERM:
115 fb1a36c0 2022-01-09 op log_warnx("Shutting down...");
116 fb1a36c0 2022-01-09 op event_loopbreak();
119 fb1a36c0 2022-01-09 op fatalx("unexpected signal %d", sig);
124 fb1a36c0 2022-01-09 op openconn(void)
126 fb1a36c0 2022-01-09 op struct addrinfo hints, *res, *res0;
128 fb1a36c0 2022-01-09 op int save_errno;
130 fb1a36c0 2022-01-09 op const char *cause = NULL;
132 fb1a36c0 2022-01-09 op memset(&hints, 0, sizeof(hints));
133 fb1a36c0 2022-01-09 op hints.ai_family = AF_UNSPEC;
134 fb1a36c0 2022-01-09 op hints.ai_socktype = SOCK_STREAM;
135 fb1a36c0 2022-01-09 op if ((error = getaddrinfo(host, port, &hints, &res0))) {
136 fb1a36c0 2022-01-09 op warnx("%s", gai_strerror(error));
141 fb1a36c0 2022-01-09 op for (res = res0; res; res = res->ai_next) {
142 fb1a36c0 2022-01-09 op s = socket(res->ai_family, res->ai_socktype,
143 fb1a36c0 2022-01-09 op res->ai_protocol);
144 fb1a36c0 2022-01-09 op if (s == -1) {
145 fb1a36c0 2022-01-09 op cause = "socket";
149 fb1a36c0 2022-01-09 op if (connect(s, res->ai_addr, res->ai_addrlen) == -1) {
150 fb1a36c0 2022-01-09 op cause = "connect";
151 fb1a36c0 2022-01-09 op save_errno = errno;
153 fb1a36c0 2022-01-09 op errno = save_errno;
161 fb1a36c0 2022-01-09 op freeaddrinfo(res0);
164 fb1a36c0 2022-01-09 op warn("%s", cause);
170 fb1a36c0 2022-01-09 op mark_nonblock(int fd)
174 fb1a36c0 2022-01-09 op if ((flags = fcntl(fd, F_GETFL)) == -1)
175 fb1a36c0 2022-01-09 op fatal("fcntl(F_GETFL)");
176 fb1a36c0 2022-01-09 op if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
177 fb1a36c0 2022-01-09 op fatal("fcntl(F_SETFL)");
181 fb1a36c0 2022-01-09 op tls_readcb(int fd, short event, void *d)
183 fb1a36c0 2022-01-09 op struct bufferevent *bufev = d;
184 fb1a36c0 2022-01-09 op char buf[IBUF_READ_SIZE];
185 fb1a36c0 2022-01-09 op int what = EVBUFFER_READ;
186 fb1a36c0 2022-01-09 op int howmuch = IBUF_READ_SIZE;
190 fb1a36c0 2022-01-09 op if (event == EV_TIMEOUT) {
191 fb1a36c0 2022-01-09 op what |= EVBUFFER_TIMEOUT;
195 fb1a36c0 2022-01-09 op if (bufev->wm_read.high != 0)
196 fb1a36c0 2022-01-09 op howmuch = MIN(sizeof(buf), bufev->wm_read.high);
198 fb1a36c0 2022-01-09 op switch (ret = tls_read(ctx, buf, howmuch)) {
199 fb1a36c0 2022-01-09 op case TLS_WANT_POLLIN:
200 fb1a36c0 2022-01-09 op case TLS_WANT_POLLOUT:
203 fb1a36c0 2022-01-09 op what |= EVBUFFER_ERROR;
208 fb1a36c0 2022-01-09 op if (len == 0) {
209 fb1a36c0 2022-01-09 op what |= EVBUFFER_EOF;
213 fb1a36c0 2022-01-09 op if (evbuffer_add(bufev->input, buf, len) == -1) {
214 fb1a36c0 2022-01-09 op what |= EVBUFFER_ERROR;
218 fb1a36c0 2022-01-09 op event_add(&bufev->ev_read, NULL);
220 fb1a36c0 2022-01-09 op len = EVBUFFER_LENGTH(bufev->input);
221 fb1a36c0 2022-01-09 op if (bufev->wm_read.low != 0 && len < bufev->wm_read.low)
223 fb1a36c0 2022-01-09 op if (bufev->readcb != NULL)
224 fb1a36c0 2022-01-09 op (*bufev->readcb)(bufev, bufev->cbarg);
228 fb1a36c0 2022-01-09 op event_add(&bufev->ev_read, NULL);
232 fb1a36c0 2022-01-09 op (*bufev->errorcb)(bufev, what, bufev->cbarg);
236 fb1a36c0 2022-01-09 op tls_writecb(int fd, short event, void *d)
238 fb1a36c0 2022-01-09 op struct bufferevent *bufev = d;
241 fb1a36c0 2022-01-09 op short what = EVBUFFER_WRITE;
244 fb1a36c0 2022-01-09 op if (event == EV_TIMEOUT) {
245 fb1a36c0 2022-01-09 op what |= EVBUFFER_TIMEOUT;
249 fb1a36c0 2022-01-09 op len = EVBUFFER_LENGTH(bufev->output);
250 fb1a36c0 2022-01-09 op if (len != 0) {
251 fb1a36c0 2022-01-09 op data = EVBUFFER_DATA(bufev->output);
253 fb1a36c0 2022-01-09 op #if DEBUG_PACKETS
254 fb1a36c0 2022-01-09 op hexdump("outgoing msg", data, len);
257 fb1a36c0 2022-01-09 op switch (ret = tls_write(ctx, data, len)) {
258 fb1a36c0 2022-01-09 op case TLS_WANT_POLLIN:
259 fb1a36c0 2022-01-09 op case TLS_WANT_POLLOUT:
262 fb1a36c0 2022-01-09 op what |= EVBUFFER_ERROR;
265 fb1a36c0 2022-01-09 op evbuffer_drain(bufev->output, ret);
268 fb1a36c0 2022-01-09 op if (EVBUFFER_LENGTH(bufev->output) != 0)
269 fb1a36c0 2022-01-09 op event_add(&bufev->ev_write, NULL);
271 fb1a36c0 2022-01-09 op if (bufev->writecb != NULL &&
272 fb1a36c0 2022-01-09 op EVBUFFER_LENGTH(bufev->output) <= bufev->wm_write.low)
273 fb1a36c0 2022-01-09 op (*bufev->writecb)(bufev, bufev->cbarg);
277 fb1a36c0 2022-01-09 op event_add(&bufev->ev_write, NULL);
280 fb1a36c0 2022-01-09 op (*bufev->errorcb)(bufev, what, bufev->cbarg);
284 fb1a36c0 2022-01-09 op client_read(struct bufferevent *bev, void *d)
286 fb1a36c0 2022-01-09 op struct evbuffer *src = EVBUFFER_INPUT(bev);
287 fb1a36c0 2022-01-09 op uint32_t len;
288 fb1a36c0 2022-01-09 op uint8_t *data;
291 fb1a36c0 2022-01-09 op if (EVBUFFER_LENGTH(src) < sizeof(len))
294 fb1a36c0 2022-01-09 op data = EVBUFFER_DATA(src);
296 fb1a36c0 2022-01-09 op memcpy(&len, data, sizeof(len));
297 fb1a36c0 2022-01-09 op len = le32toh(len);
299 fb1a36c0 2022-01-09 op if (len < HEADERSIZE)
300 fb1a36c0 2022-01-09 op fatal("incoming message is too small! (%d bytes)",
303 fb1a36c0 2022-01-09 op if (len > EVBUFFER_LENGTH(src))
306 fb1a36c0 2022-01-09 op #if DEBUG_PACKETS
307 fb1a36c0 2022-01-09 op hexdump("incoming msg", data, len);
310 fb1a36c0 2022-01-09 op handle_9p(data, len);
311 fb1a36c0 2022-01-09 op evbuffer_drain(src, len);
316 fb1a36c0 2022-01-09 op client_write(struct bufferevent *bev, void *data)
318 fb1a36c0 2022-01-09 op return; /* nothing to do */
322 fb1a36c0 2022-01-09 op client_error(struct bufferevent *bev, short err, void *data)
324 fb1a36c0 2022-01-09 op if (err & EVBUFFER_ERROR)
325 fb1a36c0 2022-01-09 op fatal("buffer event error");
327 fb1a36c0 2022-01-09 op if (err & EVBUFFER_EOF) {
329 fb1a36c0 2022-01-09 op log_info("EOF");
330 fb1a36c0 2022-01-09 op event_loopbreak();
335 fb1a36c0 2022-01-09 op log_warnx("unknown event error");
336 fb1a36c0 2022-01-09 op event_loopbreak();
340 fb1a36c0 2022-01-09 op repl_read(struct bufferevent *bev, void *d)
344 fb1a36c0 2022-01-09 op const char *argv[10], **ap;
347 fb1a36c0 2022-01-09 op line = evbuffer_readln(bev->input, &len, EVBUFFER_EOL_LF);
348 fb1a36c0 2022-01-09 op if (line == NULL)
351 fb1a36c0 2022-01-09 op for (argc = 0, ap = argv; ap < &argv[9] &&
352 fb1a36c0 2022-01-09 op (*ap = strsep(&line, " \t")) != NULL;) {
353 fb1a36c0 2022-01-09 op if (**ap != '\0')
354 fb1a36c0 2022-01-09 op ap++, argc++;
358 fb1a36c0 2022-01-09 op excmd(argv, argc);
365 fb1a36c0 2022-01-09 op repl_error(struct bufferevent *bev, short error, void *d)
367 fb1a36c0 2022-01-09 op fatalx("an error occurred");
370 fb1a36c0 2022-01-09 op static inline void
371 fb1a36c0 2022-01-09 op do_send(void)
373 fb1a36c0 2022-01-09 op bufferevent_write_buffer(bev, evb);
376 fb1a36c0 2022-01-09 op /* version [version-str] */
378 fb1a36c0 2022-01-09 op excmd_version(const char **argv, int argc)
380 fb1a36c0 2022-01-09 op const char *s;
382 fb1a36c0 2022-01-09 op s = VERSION9P;
383 fb1a36c0 2022-01-09 op if (argc == 2)
386 fb1a36c0 2022-01-09 op tversion(s, MSIZE9P);
390 fb1a36c0 2022-01-09 op /* attach fid uname aname */
392 fb1a36c0 2022-01-09 op excmd_attach(const char **argv, int argc)
394 fb1a36c0 2022-01-09 op uint32_t fid;
395 fb1a36c0 2022-01-09 op const char *errstr;
397 fb1a36c0 2022-01-09 op if (argc != 4)
400 fb1a36c0 2022-01-09 op fid = strtonum(argv[1], 0, UINT32_MAX, &errstr);
401 fb1a36c0 2022-01-09 op if (errstr != NULL) {
402 fb1a36c0 2022-01-09 op log_warnx("fid is %s: %s", errstr, argv[1]);
406 fb1a36c0 2022-01-09 op tattach(fid, NOFID, argv[2], argv[3]);
411 fb1a36c0 2022-01-09 op log_warnx("usage: attach fid uname aname");
414 fb1a36c0 2022-01-09 op /* clunk fid */
416 fb1a36c0 2022-01-09 op excmd_clunk(const char **argv, int argc)
418 fb1a36c0 2022-01-09 op uint32_t fid;
419 fb1a36c0 2022-01-09 op const char *errstr;
421 fb1a36c0 2022-01-09 op if (argc != 2)
424 fb1a36c0 2022-01-09 op fid = strtonum(argv[1], 0, UINT32_MAX, &errstr);
425 fb1a36c0 2022-01-09 op if (errstr != NULL) {
426 fb1a36c0 2022-01-09 op log_warnx("fid is %s: %s", errstr, argv[1]);
435 fb1a36c0 2022-01-09 op log_warnx("usage: clunk fid");
438 fb1a36c0 2022-01-09 op /* flush oldtag */
440 fb1a36c0 2022-01-09 op excmd_flush(const char **argv, int argc)
442 fb1a36c0 2022-01-09 op uint16_t oldtag;
443 fb1a36c0 2022-01-09 op const char *errstr;
445 fb1a36c0 2022-01-09 op if (argc != 2)
448 fb1a36c0 2022-01-09 op oldtag = strtonum(argv[1], 0, UINT16_MAX, &errstr);
449 fb1a36c0 2022-01-09 op if (errstr != NULL) {
450 fb1a36c0 2022-01-09 op log_warnx("oldtag is %s: %s", errstr, argv[1]);
454 fb1a36c0 2022-01-09 op tflush(oldtag);
459 fb1a36c0 2022-01-09 op log_warnx("usage: flush oldtag");
462 fb1a36c0 2022-01-09 op /* walk fid newfid wnames... */
464 fb1a36c0 2022-01-09 op excmd_walk(const char **argv, int argc)
466 fb1a36c0 2022-01-09 op uint32_t fid, newfid;
467 fb1a36c0 2022-01-09 op const char *errstr;
469 fb1a36c0 2022-01-09 op if (argc < 3)
472 fb1a36c0 2022-01-09 op fid = strtonum(argv[1], 0, UINT32_MAX, &errstr);
473 fb1a36c0 2022-01-09 op if (errstr != NULL) {
474 fb1a36c0 2022-01-09 op log_warnx("fid is %s: %s", errstr, argv[1]);
478 fb1a36c0 2022-01-09 op newfid = strtonum(argv[2], 0, UINT32_MAX, &errstr);
479 fb1a36c0 2022-01-09 op if (errstr != NULL) {
480 fb1a36c0 2022-01-09 op log_warnx("newfid is %s: %s", errstr, argv[1]);
484 fb1a36c0 2022-01-09 op twalk(fid, newfid, argv + 3, argc - 3);
489 fb1a36c0 2022-01-09 op log_warnx("usage: walk fid newfid wnames...");
492 fb1a36c0 2022-01-09 op /* open fid mode [flag] */
494 fb1a36c0 2022-01-09 op excmd_open(const char **argv, int argc)
496 fb1a36c0 2022-01-09 op const char *errstr;
497 fb1a36c0 2022-01-09 op uint32_t fid;
498 fb1a36c0 2022-01-09 op uint8_t mode = 0;
500 fb1a36c0 2022-01-09 op if (argc != 3 && argc != 4)
503 fb1a36c0 2022-01-09 op fid = strtonum(argv[1], 0, UINT32_MAX, &errstr);
504 fb1a36c0 2022-01-09 op if (errstr != NULL) {
505 fb1a36c0 2022-01-09 op log_warnx("fid is %s: %s", errstr, argv[1]);
509 fb1a36c0 2022-01-09 op /* parse mode */
510 fb1a36c0 2022-01-09 op if (!strcmp("read", argv[2]) || !strcmp("r", argv[2]))
511 fb1a36c0 2022-01-09 op mode = KOREAD;
512 fb1a36c0 2022-01-09 op else if (!strcmp("write", argv[2]) || !strcmp("w", argv[2]))
513 fb1a36c0 2022-01-09 op mode = KOWRITE;
514 fb1a36c0 2022-01-09 op else if (!strcmp("readwrite", argv[2]) || !strcmp("rw", argv[2]))
515 fb1a36c0 2022-01-09 op mode = KORDWR;
517 fb1a36c0 2022-01-09 op log_warnx("invalid mode %s", argv[2]);
521 fb1a36c0 2022-01-09 op /* parse flag */
522 fb1a36c0 2022-01-09 op if (argv[3] != NULL) {
523 fb1a36c0 2022-01-09 op if (!strcmp("trunc", argv[3]))
524 fb1a36c0 2022-01-09 op mode |= KOTRUNC;
525 fb1a36c0 2022-01-09 op else if (!strcmp("rclose", argv[3]))
526 fb1a36c0 2022-01-09 op mode |= KORCLOSE;
528 fb1a36c0 2022-01-09 op log_warnx("invalid flag %s", argv[3]);
533 fb1a36c0 2022-01-09 op topen(fid, mode);
538 fb1a36c0 2022-01-09 op log_warnx("usage: open fid mode [flag]");
541 fb1a36c0 2022-01-09 op /* create fid path perm mode */
543 fb1a36c0 2022-01-09 op excmd_create(const char **argv, int argc)
545 fb1a36c0 2022-01-09 op const char *errstr;
546 fb1a36c0 2022-01-09 op uint32_t fid;
547 fb1a36c0 2022-01-09 op uint8_t mode = 0;
549 fb1a36c0 2022-01-09 op if (argc != 5)
552 fb1a36c0 2022-01-09 op fid = strtonum(argv[1], 0, UINT32_MAX, &errstr);
553 fb1a36c0 2022-01-09 op if (errstr != NULL) {
554 fb1a36c0 2022-01-09 op log_warnx("fid is %s: %s", errstr, argv[1]);
558 fb1a36c0 2022-01-09 op /* parse mode */
559 fb1a36c0 2022-01-09 op if (!strcmp("write", argv[4]) || !strcmp("w", argv[4]))
560 fb1a36c0 2022-01-09 op mode = KOWRITE;
561 fb1a36c0 2022-01-09 op else if (!strcmp("readwrite", argv[4]) || !strcmp("rw", argv[4]))
562 fb1a36c0 2022-01-09 op mode = KORDWR;
564 fb1a36c0 2022-01-09 op log_warnx("invalid mode %s for create", argv[4]);
568 fb1a36c0 2022-01-09 op tcreate(fid, argv[2], 0, mode);
573 fb1a36c0 2022-01-09 op log_warnx("usage: create fid path perm mode ; perm is unused");
577 fb1a36c0 2022-01-09 op /* read fid offset count */
579 fb1a36c0 2022-01-09 op excmd_read(const char **argv, int argc)
581 fb1a36c0 2022-01-09 op uint64_t off;
582 fb1a36c0 2022-01-09 op uint32_t fid, count;
583 fb1a36c0 2022-01-09 op const char *errstr;
585 fb1a36c0 2022-01-09 op if (argc != 4)
588 fb1a36c0 2022-01-09 op fid = strtonum(argv[1], 0, UINT32_MAX, &errstr);
589 fb1a36c0 2022-01-09 op if (errstr != NULL) {
590 fb1a36c0 2022-01-09 op log_warnx("fid is %s: %s", errstr, argv[1]);
594 fb1a36c0 2022-01-09 op /* should really be UNT64_MAX but it fails... */
595 fb1a36c0 2022-01-09 op off = strtonum(argv[2], -1, UINT32_MAX, &errstr);
596 fb1a36c0 2022-01-09 op if (errstr != NULL) {
597 fb1a36c0 2022-01-09 op log_warnx("offset is %s: %s", errstr, argv[2]);
601 fb1a36c0 2022-01-09 op count = strtonum(argv[3], 0, UINT32_MAX, &errstr);
602 fb1a36c0 2022-01-09 op if (errstr != NULL) {
603 fb1a36c0 2022-01-09 op log_warnx("count is %s: %s", errstr, argv[3]);
607 fb1a36c0 2022-01-09 op tread(fid, off, count);
612 fb1a36c0 2022-01-09 op log_warnx("usage: read fid offset count");
615 fb1a36c0 2022-01-09 op /* write fid offset content */
617 fb1a36c0 2022-01-09 op excmd_write(const char **argv, int argc)
619 fb1a36c0 2022-01-09 op uint64_t off;
620 fb1a36c0 2022-01-09 op uint32_t fid, count;
621 fb1a36c0 2022-01-09 op const char *errstr;
623 fb1a36c0 2022-01-09 op if (argc != 4)
626 fb1a36c0 2022-01-09 op fid = strtonum(argv[1], 0, UINT32_MAX, &errstr);
627 fb1a36c0 2022-01-09 op if (errstr != NULL) {
628 fb1a36c0 2022-01-09 op log_warnx("fid is %s: %s", errstr, argv[1]);
632 fb1a36c0 2022-01-09 op /* should really be UINT64_MAX but... */
633 fb1a36c0 2022-01-09 op off = strtonum(argv[2], 0, UINT32_MAX, &errstr);
634 fb1a36c0 2022-01-09 op if (errstr != NULL) {
635 fb1a36c0 2022-01-09 op log_warnx("offset is %s: %s", errstr, argv[2]);
639 fb1a36c0 2022-01-09 op count = strlen(argv[3]);
640 fb1a36c0 2022-01-09 op twrite(fid, off, argv[3], count);
645 fb1a36c0 2022-01-09 op log_warnx("usage: write fid offset content");
648 fb1a36c0 2022-01-09 op /* remove fid */
650 fb1a36c0 2022-01-09 op excmd_remove(const char **argv, int argc)
652 fb1a36c0 2022-01-09 op const char *errstr;
653 fb1a36c0 2022-01-09 op uint32_t fid;
655 fb1a36c0 2022-01-09 op if (argc != 2)
658 fb1a36c0 2022-01-09 op fid = strtonum(argv[1], 0, UINT32_MAX, &errstr);
659 fb1a36c0 2022-01-09 op if (errstr != NULL) {
660 fb1a36c0 2022-01-09 op log_warnx("fid is %s: %s", errstr, argv[1]);
664 fb1a36c0 2022-01-09 op tremove(fid);
669 fb1a36c0 2022-01-09 op log_warnx("usage: remove fid");
673 fb1a36c0 2022-01-09 op excmd(const char **argv, int argc)
676 fb1a36c0 2022-01-09 op const char *name;
677 fb1a36c0 2022-01-09 op void (*fn)(const char **, int);
679 fb1a36c0 2022-01-09 op {"version", excmd_version},
680 fb1a36c0 2022-01-09 op {"attach", excmd_attach},
681 fb1a36c0 2022-01-09 op {"clunk", excmd_clunk},
682 fb1a36c0 2022-01-09 op {"flush", excmd_flush},
683 fb1a36c0 2022-01-09 op {"walk", excmd_walk},
684 fb1a36c0 2022-01-09 op {"open", excmd_open},
685 fb1a36c0 2022-01-09 op {"create", excmd_create},
686 fb1a36c0 2022-01-09 op {"read", excmd_read},
687 fb1a36c0 2022-01-09 op {"write", excmd_write},
688 fb1a36c0 2022-01-09 op /* TODO: stat */
689 fb1a36c0 2022-01-09 op {"remove", excmd_remove},
693 fb1a36c0 2022-01-09 op if (argc == 0)
696 fb1a36c0 2022-01-09 op for (i = 0; i < sizeof(cmds)/sizeof(cmds[0]); ++i) {
697 fb1a36c0 2022-01-09 op if (!strcmp(cmds[i].name, argv[0])) {
698 fb1a36c0 2022-01-09 op cmds[i].fn(argv, argc);
703 fb1a36c0 2022-01-09 op log_warnx("Unknown command %s", *argv);
707 fb1a36c0 2022-01-09 op pp_qid(const uint8_t *d, uint32_t len)
709 fb1a36c0 2022-01-09 op uint64_t path;
710 fb1a36c0 2022-01-09 op uint32_t vers;
711 fb1a36c0 2022-01-09 op uint8_t type;
713 fb1a36c0 2022-01-09 op if (len < 13) {
714 fb1a36c0 2022-01-09 op printf("invalid");
720 fb1a36c0 2022-01-09 op memcpy(&vers, d, sizeof(vers));
721 fb1a36c0 2022-01-09 op d += sizeof(vers);
722 fb1a36c0 2022-01-09 op vers = le64toh(vers);
724 fb1a36c0 2022-01-09 op memcpy(&path, d, sizeof(path));
725 fb1a36c0 2022-01-09 op d += sizeof(path);
726 fb1a36c0 2022-01-09 op path = le64toh(path);
728 fb1a36c0 2022-01-09 op printf("qid{path=%"PRIu64" version=%"PRIu32" type=0x%x\"%s\"}",
729 fb1a36c0 2022-01-09 op path, vers, type, pp_qid_type(type));
733 fb1a36c0 2022-01-09 op pp_msg(uint32_t len, uint8_t type, uint16_t tag, const uint8_t *d)
735 fb1a36c0 2022-01-09 op uint32_t msize, iounit, count;
736 fb1a36c0 2022-01-09 op uint16_t slen;
739 fb1a36c0 2022-01-09 op printf("len=%"PRIu32" type=%d[%s] tag=0x%x[%d] ", len,
740 fb1a36c0 2022-01-09 op type, pp_msg_type(type), tag, tag);
742 fb1a36c0 2022-01-09 op len -= HEADERSIZE;
744 fb1a36c0 2022-01-09 op switch (type) {
745 fb1a36c0 2022-01-09 op case Rversion:
746 fb1a36c0 2022-01-09 op if (len < 6) {
747 fb1a36c0 2022-01-09 op printf("invalid: not enough space for msize "
748 fb1a36c0 2022-01-09 op "and version provided.");
752 fb1a36c0 2022-01-09 op memcpy(&msize, d, sizeof(msize));
753 fb1a36c0 2022-01-09 op d += sizeof(msize);
754 fb1a36c0 2022-01-09 op len -= sizeof(msize);
755 fb1a36c0 2022-01-09 op msize = le32toh(msize);
757 fb1a36c0 2022-01-09 op memcpy(&slen, d, sizeof(slen));
758 fb1a36c0 2022-01-09 op d += sizeof(slen);
759 fb1a36c0 2022-01-09 op len -= sizeof(slen);
760 fb1a36c0 2022-01-09 op slen = le16toh(slen);
762 fb1a36c0 2022-01-09 op if (len != slen) {
763 fb1a36c0 2022-01-09 op printf("invalid: version string length doesn't "
764 fb1a36c0 2022-01-09 op "match. Got %d; want %d", slen, len);
768 fb1a36c0 2022-01-09 op printf("msize=%"PRIu32" version[%"PRIu16"]=\"",
769 fb1a36c0 2022-01-09 op msize, slen);
770 fb1a36c0 2022-01-09 op fwrite(d, 1, slen, stdout);
771 fb1a36c0 2022-01-09 op printf("\"");
775 fb1a36c0 2022-01-09 op case Rattach:
776 fb1a36c0 2022-01-09 op pp_qid(d, len);
781 fb1a36c0 2022-01-09 op case Rremove:
782 fb1a36c0 2022-01-09 op if (len != 0)
783 fb1a36c0 2022-01-09 op printf("invalid %s: %"PRIu32" extra bytes",
784 fb1a36c0 2022-01-09 op pp_msg_type(type), len);
788 fb1a36c0 2022-01-09 op if (len < 2) {
789 fb1a36c0 2022-01-09 op printf("invaild Rwalk: less than two bytes (%d)",
794 fb1a36c0 2022-01-09 op memcpy(&slen, d, sizeof(slen));
795 fb1a36c0 2022-01-09 op d += sizeof(slen);
796 fb1a36c0 2022-01-09 op len -= sizeof(slen);
797 fb1a36c0 2022-01-09 op slen = le16toh(slen);
799 fb1a36c0 2022-01-09 op if (len != QIDSIZE * slen) {
800 fb1a36c0 2022-01-09 op printf("invalid Rwalk: wanted %d bytes for %d qids "
801 fb1a36c0 2022-01-09 op "but got %"PRIu32" bytes instead",
802 fb1a36c0 2022-01-09 op QIDSIZE*slen, slen, len);
806 fb1a36c0 2022-01-09 op printf("nwqid=%"PRIu16, slen);
808 fb1a36c0 2022-01-09 op for (; slen != 0; slen--) {
810 fb1a36c0 2022-01-09 op pp_qid(d, len);
811 fb1a36c0 2022-01-09 op d += QIDSIZE;
812 fb1a36c0 2022-01-09 op len -= QIDSIZE;
818 fb1a36c0 2022-01-09 op case Rcreate:
819 fb1a36c0 2022-01-09 op if (len != QIDSIZE + 4) {
820 fb1a36c0 2022-01-09 op printf("invalid %s: expected %d bytes; "
821 fb1a36c0 2022-01-09 op "got %u\n", pp_msg_type(type), QIDSIZE + 4, len);
825 fb1a36c0 2022-01-09 op pp_qid(d, len);
826 fb1a36c0 2022-01-09 op d += QIDSIZE;
827 fb1a36c0 2022-01-09 op len -= QIDSIZE;
829 fb1a36c0 2022-01-09 op memcpy(&iounit, d, sizeof(iounit));
830 fb1a36c0 2022-01-09 op d += sizeof(iounit);
831 fb1a36c0 2022-01-09 op len -= sizeof(iounit);
832 fb1a36c0 2022-01-09 op iounit = le32toh(iounit);
833 fb1a36c0 2022-01-09 op printf(" iounit=%"PRIu32, iounit);
837 fb1a36c0 2022-01-09 op if (len < sizeof(count)) {
838 fb1a36c0 2022-01-09 op printf("invalid Rread: expected %zu bytes at least; "
839 fb1a36c0 2022-01-09 op "got %u\n", sizeof(count), len);
843 fb1a36c0 2022-01-09 op memcpy(&count, d, sizeof(count));
844 fb1a36c0 2022-01-09 op d += sizeof(count);
845 fb1a36c0 2022-01-09 op len -= sizeof(count);
846 fb1a36c0 2022-01-09 op count = le32toh(count);
848 fb1a36c0 2022-01-09 op if (len != count) {
849 fb1a36c0 2022-01-09 op printf("invalid Rread: expected %d data bytes; "
850 fb1a36c0 2022-01-09 op "got %u\n", count, len);
854 fb1a36c0 2022-01-09 op /* allocates three extra bytes, oh well... */
855 fb1a36c0 2022-01-09 op if ((v = calloc(count + 1, 4)) == NULL)
856 fb1a36c0 2022-01-09 op fatal("calloc");
857 fb1a36c0 2022-01-09 op strvisx(v, d, count, VIS_SAFE | VIS_TAB | VIS_NL | VIS_CSTYLE);
858 fb1a36c0 2022-01-09 op printf("data=%s", v);
864 fb1a36c0 2022-01-09 op if (len != sizeof(count)) {
865 fb1a36c0 2022-01-09 op printf("invalid Rwrite: expected %zu data bytes; "
866 fb1a36c0 2022-01-09 op "got %u\n", sizeof(count), len);
870 fb1a36c0 2022-01-09 op memcpy(&count, d, sizeof(count));
871 fb1a36c0 2022-01-09 op d += sizeof(count);
872 fb1a36c0 2022-01-09 op len -= sizeof(count);
873 fb1a36c0 2022-01-09 op count = le32toh(count);
875 fb1a36c0 2022-01-09 op printf("count=%d", count);
879 fb1a36c0 2022-01-09 op memcpy(&slen, d, sizeof(slen));
880 fb1a36c0 2022-01-09 op d += sizeof(slen);
881 fb1a36c0 2022-01-09 op len -= sizeof(slen);
882 fb1a36c0 2022-01-09 op slen = le16toh(slen);
884 fb1a36c0 2022-01-09 op if (slen != len) {
885 fb1a36c0 2022-01-09 op printf("invalid: error string length doesn't "
886 fb1a36c0 2022-01-09 op "match. Got %d; want %d", slen, len);
890 fb1a36c0 2022-01-09 op printf("error=\"");
891 fb1a36c0 2022-01-09 op fwrite(d, 1, slen, stdout);
892 fb1a36c0 2022-01-09 op printf("\"");
897 fb1a36c0 2022-01-09 op if ((v = calloc(len + 1, 4)) == NULL)
898 fb1a36c0 2022-01-09 op fatal("calloc");
899 fb1a36c0 2022-01-09 op strvisx(v, d, len, VIS_SAFE | VIS_TAB | VIS_NL | VIS_CSTYLE);
900 fb1a36c0 2022-01-09 op printf("body=%s", v);
904 fb1a36c0 2022-01-09 op printf("\n");
908 fb1a36c0 2022-01-09 op handle_9p(const uint8_t *data, size_t size)
910 fb1a36c0 2022-01-09 op uint32_t len;
911 fb1a36c0 2022-01-09 op uint16_t tag;
912 fb1a36c0 2022-01-09 op uint8_t type;
914 fb1a36c0 2022-01-09 op assert(size >= HEADERSIZE);
916 fb1a36c0 2022-01-09 op memcpy(&len, data, sizeof(len));
917 fb1a36c0 2022-01-09 op data += sizeof(len);
919 fb1a36c0 2022-01-09 op memcpy(&type, data, sizeof(type));
920 fb1a36c0 2022-01-09 op data += sizeof(type);
922 fb1a36c0 2022-01-09 op memcpy(&tag, data, sizeof(tag));
923 fb1a36c0 2022-01-09 op data += sizeof(tag);
925 fb1a36c0 2022-01-09 op len = le32toh(len);
926 fb1a36c0 2022-01-09 op /* type is one byte long, no endianness issues */
927 fb1a36c0 2022-01-09 op tag = le16toh(tag);
930 fb1a36c0 2022-01-09 op pp_msg(len, type, tag, data);
937 fb1a36c0 2022-01-09 op printf("\r");
938 fb1a36c0 2022-01-09 op fflush(stdout);
944 fb1a36c0 2022-01-09 op printf("%s", PROMPT);
945 fb1a36c0 2022-01-09 op fflush(stdout);
949 fb1a36c0 2022-01-09 op main(int argc, char **argv)
951 fb1a36c0 2022-01-09 op int ch, sock, handshake;
952 fb1a36c0 2022-01-09 op struct event ev_sigint, ev_sigterm;
954 fb1a36c0 2022-01-09 op signal(SIGPIPE, SIG_IGN);
956 fb1a36c0 2022-01-09 op while ((ch = getopt(argc, argv, "C:cH:hK:P:v")) != -1) {
957 fb1a36c0 2022-01-09 op switch (ch) {
959 fb1a36c0 2022-01-09 op crtpath = optarg;
965 fb1a36c0 2022-01-09 op host = optarg;
971 fb1a36c0 2022-01-09 op keypath = optarg;
974 fb1a36c0 2022-01-09 op port = optarg;
984 fb1a36c0 2022-01-09 op if (host == NULL)
985 fb1a36c0 2022-01-09 op host = "localhost";
986 fb1a36c0 2022-01-09 op if (port == NULL)
987 fb1a36c0 2022-01-09 op port = "1337";
989 fb1a36c0 2022-01-09 op argc -= optind;
990 fb1a36c0 2022-01-09 op argv += optind;
992 fb1a36c0 2022-01-09 op if (argc != 0)
994 fb1a36c0 2022-01-09 op /* if (!tls || (crtpath != NULL || keypath != NULL)) */
995 fb1a36c0 2022-01-09 op /* usage(1); */
997 fb1a36c0 2022-01-09 op errx(1, "must enable tls (for now)");
999 fb1a36c0 2022-01-09 op log_init(1, LOG_DAEMON);
1000 fb1a36c0 2022-01-09 op log_setverbose(verbose);
1001 fb1a36c0 2022-01-09 op log_procinit(getprogname());
1003 fb1a36c0 2022-01-09 op if ((tlsconf = tls_config_new()) == NULL)
1004 fb1a36c0 2022-01-09 op fatalx("tls_config_new");
1005 fb1a36c0 2022-01-09 op tls_config_insecure_noverifycert(tlsconf);
1006 fb1a36c0 2022-01-09 op tls_config_insecure_noverifyname(tlsconf);
1007 fb1a36c0 2022-01-09 op if (tls_config_set_keypair_file(tlsconf, crtpath, keypath) == -1)
1008 fb1a36c0 2022-01-09 op fatalx("can't load certs (%s, %s)", crtpath, keypath);
1010 fb1a36c0 2022-01-09 op if ((ctx = tls_client()) == NULL)
1011 fb1a36c0 2022-01-09 op fatal("tls_client");
1012 fb1a36c0 2022-01-09 op if (tls_configure(ctx, tlsconf) == -1)
1013 fb1a36c0 2022-01-09 op fatalx("tls_configure: %s", tls_error(ctx));
1015 fb1a36c0 2022-01-09 op log_info("connecting to %s:%s...", host, port);
1017 fb1a36c0 2022-01-09 op if ((sock = openconn()) == -1)
1018 fb1a36c0 2022-01-09 op fatalx("can't connect to %s:%s", host, port);
1020 fb1a36c0 2022-01-09 op if (tls_connect_socket(ctx, sock, host) == -1)
1021 fb1a36c0 2022-01-09 op fatalx("tls_connect_socket: %s", tls_error(ctx));
1023 fb1a36c0 2022-01-09 op for (handshake = 0; !handshake;) {
1024 fb1a36c0 2022-01-09 op switch (tls_handshake(ctx)) {
1026 fb1a36c0 2022-01-09 op fatalx("tls_handshake: %s", tls_error(ctx));
1028 fb1a36c0 2022-01-09 op handshake = 1;
1033 fb1a36c0 2022-01-09 op log_info("connected!");
1035 fb1a36c0 2022-01-09 op mark_nonblock(sock);
1037 fb1a36c0 2022-01-09 op event_init();
1039 fb1a36c0 2022-01-09 op /* initialize global evb */
1040 fb1a36c0 2022-01-09 op if ((evb = evbuffer_new()) == NULL)
1041 fb1a36c0 2022-01-09 op fatal("evbuffer_new");
1043 fb1a36c0 2022-01-09 op signal_set(&ev_sigint, SIGINT, sig_handler, NULL);
1044 fb1a36c0 2022-01-09 op signal_set(&ev_sigterm, SIGINT, sig_handler, NULL);
1046 fb1a36c0 2022-01-09 op signal_add(&ev_sigint, NULL);
1047 fb1a36c0 2022-01-09 op signal_add(&ev_sigterm, NULL);
1049 fb1a36c0 2022-01-09 op bev = bufferevent_new(sock, client_read, client_write, client_error,
1051 fb1a36c0 2022-01-09 op if (bev == NULL)
1052 fb1a36c0 2022-01-09 op fatal("bufferevent_new");
1054 fb1a36c0 2022-01-09 op /* setup tls/io */
1055 fb1a36c0 2022-01-09 op event_set(&bev->ev_read, sock, EV_READ, tls_readcb, bev);
1056 fb1a36c0 2022-01-09 op event_set(&bev->ev_write, sock, EV_WRITE, tls_writecb, bev);
1058 fb1a36c0 2022-01-09 op bufferevent_enable(bev, EV_READ|EV_WRITE);
1060 fb1a36c0 2022-01-09 op mark_nonblock(0);
1061 fb1a36c0 2022-01-09 op inbev = bufferevent_new(0, repl_read, NULL, repl_error, NULL);
1062 fb1a36c0 2022-01-09 op bufferevent_enable(inbev, EV_READ);
1065 fb1a36c0 2022-01-09 op event_dispatch();
1067 fb1a36c0 2022-01-09 op bufferevent_free(bev);
1068 fb1a36c0 2022-01-09 op tls_free(ctx);
1069 fb1a36c0 2022-01-09 op tls_config_free(tlsconf);
1070 fb1a36c0 2022-01-09 op close(sock);