2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
20 #include <sys/socket.h>
42 #define DEBUG_PACKETS 0
55 struct tls_config *tlsconf;
57 struct bufferevent *bev, *inbev;
59 static void ATTR_DEAD usage(int);
61 static void sig_handler(int, short, void *);
63 static int openconn(void);
64 static void mark_nonblock(int);
66 static void tls_readcb(int, short, void *);
67 static void tls_writecb(int, short, void *);
69 static void client_read(struct bufferevent *, void *);
70 static void client_write(struct bufferevent *, void *);
71 static void client_error(struct bufferevent *, short, void *);
73 static void repl_read(struct bufferevent *, void *);
74 static void repl_error(struct bufferevent *, short, void *);
76 static void excmd_version(const char **, int);
77 static void excmd_attach(const char **, int);
78 static void excmd_clunk(const char **, int);
79 static void excmd_flush(const char **, int);
80 static void excmd_walk(const char ** , int);
81 static void excmd(const char **, int);
83 static const char *pp_qid_type(uint8_t);
84 static void pp_qid(const uint8_t *, uint32_t);
85 static void pp_msg(uint32_t, uint8_t, uint16_t, const uint8_t *);
86 static void handle_9p(const uint8_t *, size_t);
87 static void clr(void);
88 static void prompt(void);
94 "usage: %s [-chv] [-C crt] [-K key] [-H host] [-P port]\n",
96 fprintf(stderr, PACKAGE_NAME " suite version " PACKAGE_VERSION "\n");
101 sig_handler(int sig, short event, void *d)
104 * Normal signal handler rules don't apply because libevent
112 log_warnx("Shutting down...");
116 fatalx("unexpected signal %d", sig);
123 struct addrinfo hints, *res, *res0;
127 const char *cause = NULL;
129 memset(&hints, 0, sizeof(hints));
130 hints.ai_family = AF_UNSPEC;
131 hints.ai_socktype = SOCK_STREAM;
132 if ((error = getaddrinfo(host, port, &hints, &res0))) {
133 warnx("%s", gai_strerror(error));
138 for (res = res0; res; res = res->ai_next) {
139 s = socket(res->ai_family, res->ai_socktype,
146 if (connect(s, res->ai_addr, res->ai_addrlen) == -1) {
167 mark_nonblock(int fd)
171 if ((flags = fcntl(fd, F_GETFL)) == -1)
172 fatal("fcntl(F_GETFL)");
173 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
174 fatal("fcntl(F_SETFL)");
178 tls_readcb(int fd, short event, void *d)
180 struct bufferevent *bufev = d;
181 char buf[IBUF_READ_SIZE];
182 int what = EVBUFFER_READ;
183 int howmuch = IBUF_READ_SIZE;
187 if (event == EV_TIMEOUT) {
188 what |= EVBUFFER_TIMEOUT;
192 if (bufev->wm_read.high != 0)
193 howmuch = MIN(sizeof(buf), bufev->wm_read.high);
195 switch (ret = tls_read(ctx, buf, howmuch)) {
196 case TLS_WANT_POLLIN:
197 case TLS_WANT_POLLOUT:
200 what |= EVBUFFER_ERROR;
206 what |= EVBUFFER_EOF;
210 if (evbuffer_add(bufev->input, buf, len) == -1) {
211 what |= EVBUFFER_ERROR;
215 event_add(&bufev->ev_read, NULL);
217 len = EVBUFFER_LENGTH(bufev->input);
218 if (bufev->wm_read.low != 0 && len < bufev->wm_read.low)
220 if (bufev->readcb != NULL)
221 (*bufev->readcb)(bufev, bufev->cbarg);
225 event_add(&bufev->ev_read, NULL);
229 (*bufev->errorcb)(bufev, what, bufev->cbarg);
233 tls_writecb(int fd, short event, void *d)
235 struct bufferevent *bufev = d;
238 short what = EVBUFFER_WRITE;
241 if (event == EV_TIMEOUT) {
242 what |= EVBUFFER_TIMEOUT;
246 len = EVBUFFER_LENGTH(bufev->output);
248 data = EVBUFFER_DATA(bufev->output);
251 hexdump("outgoing msg", data, len);
254 switch (ret = tls_write(ctx, data, len)) {
255 case TLS_WANT_POLLIN:
256 case TLS_WANT_POLLOUT:
259 what |= EVBUFFER_ERROR;
262 evbuffer_drain(bufev->output, ret);
265 if (EVBUFFER_LENGTH(bufev->output) != 0)
266 event_add(&bufev->ev_write, NULL);
268 if (bufev->writecb != NULL &&
269 EVBUFFER_LENGTH(bufev->output) <= bufev->wm_write.low)
270 (*bufev->writecb)(bufev, bufev->cbarg);
274 event_add(&bufev->ev_write, NULL);
277 (*bufev->errorcb)(bufev, what, bufev->cbarg);
281 client_read(struct bufferevent *bev, void *d)
283 struct evbuffer *src = EVBUFFER_INPUT(bev);
288 if (EVBUFFER_LENGTH(src) < sizeof(len))
291 data = EVBUFFER_DATA(src);
293 memcpy(&len, data, sizeof(len));
296 if (len < HEADERSIZE)
297 fatal("incoming message is too small! (%d bytes)",
300 if (len > EVBUFFER_LENGTH(src))
304 hexdump("incoming msg", data, len);
307 handle_9p(data, len);
308 evbuffer_drain(src, len);
313 client_write(struct bufferevent *bev, void *data)
315 return; /* nothing to do */
319 client_error(struct bufferevent *bev, short err, void *data)
321 if (err & EVBUFFER_ERROR)
322 fatal("buffer event error");
324 if (err & EVBUFFER_EOF) {
332 log_warnx("unknown event error");
337 repl_read(struct bufferevent *bev, void *d)
341 const char *argv[10], **ap;
344 line = evbuffer_readln(bev->input, &len, EVBUFFER_EOL_LF);
348 for (argc = 0, ap = argv; ap < &argv[9] &&
349 (*ap = strsep(&line, " \t")) != NULL;) {
362 repl_error(struct bufferevent *bev, short error, void *d)
364 fatalx("an error occurred");
370 bufferevent_write_buffer(bev, evb);
373 /* version [version-str] */
375 excmd_version(const char **argv, int argc)
383 tversion(s, MSIZE9P);
387 /* attach fid uname aname */
389 excmd_attach(const char **argv, int argc)
397 fid = strtonum(argv[1], 0, UINT32_MAX, &errstr);
398 if (errstr != NULL) {
399 log_warnx("fid is %s: %s", errstr, argv[1]);
403 tattach(fid, NOFID, argv[2], argv[3]);
408 log_warnx("usage: attach fid uname aname");
413 excmd_clunk(const char **argv, int argc)
421 fid = strtonum(argv[1], 0, UINT32_MAX, &errstr);
422 if (errstr != NULL) {
423 log_warnx("fid is %s: %s", errstr, argv[1]);
432 log_warnx("usage: clunk fid");
437 excmd_flush(const char **argv, int argc)
445 oldtag = strtonum(argv[1], 0, UINT16_MAX, &errstr);
446 if (errstr != NULL) {
447 log_warnx("oldtag is %s: %s", errstr, argv[1]);
456 log_warnx("usage: flush oldtag");
459 /* walk fid newfid wnames... */
461 excmd_walk(const char **argv, int argc)
463 uint32_t fid, newfid;
469 fid = strtonum(argv[1], 0, UINT32_MAX, &errstr);
470 if (errstr != NULL) {
471 log_warnx("fid is %s: %s", errstr, argv[1]);
475 newfid = strtonum(argv[2], 0, UINT32_MAX, &errstr);
476 if (errstr != NULL) {
477 log_warnx("newfid is %s: %s", errstr, argv[1]);
481 twalk(fid, newfid, argv + 3, argc - 3);
486 log_warnx("usage: walk fid newfid wnames...");
490 excmd(const char **argv, int argc)
494 void (*fn)(const char **, int);
496 {"version", excmd_version},
497 {"attach", excmd_attach},
498 {"clunk", excmd_clunk},
499 {"flush", excmd_flush},
500 {"walk", excmd_walk},
507 for (i = 0; i < sizeof(cmds)/sizeof(cmds[0]); ++i) {
508 if (!strcmp(cmds[i].name, argv[0])) {
509 cmds[i].fn(argv, argc);
514 log_warnx("Unknown command %s", *argv);
518 pp_qid_type(uint8_t type)
521 case QTDIR: return "dir";
522 case QTAPPEND: return "append-only";
523 case QTEXCL: return "exclusive";
524 case QTMOUNT: return "mounted-channel";
525 case QTAUTH: return "authentication";
526 case QTTMP: return "non-backed-up";
527 case QTSYMLINK: return "symlink";
528 case QTFILE: return "file";
535 pp_qid(const uint8_t *d, uint32_t len)
548 memcpy(&vers, d, sizeof(vers));
550 vers = le64toh(vers);
552 memcpy(&path, d, sizeof(path));
554 path = le64toh(path);
556 printf("qid{path=%"PRIu64" version=%"PRIu32" type=0x%x\"%s\"}",
557 path, vers, type, pp_qid_type(type));
561 pp_msg(uint32_t len, uint8_t type, uint16_t tag, const uint8_t *d)
566 printf("len=%"PRIu32" type=%d[%s] tag=0x%x[%d] ", len,
567 type, pp_msg_type(type), tag, tag);
574 printf("invalid: not enough space for msize "
575 "and version provided.");
579 memcpy(&msize, d, sizeof(msize));
581 len -= sizeof(msize);
582 msize = le32toh(msize);
584 memcpy(&slen, d, sizeof(slen));
587 slen = le16toh(slen);
590 printf("invalid: version string length doesn't "
591 "match. Got %d; want %d", slen, len);
595 printf("msize=%"PRIu32" version[%"PRIu16"]=\"",
597 fwrite(d, 1, slen, stdout);
608 printf("invalid Rclunk: %"PRIu32" extra bytes", len);
613 printf("invalid Rflush: %"PRIu32" extra bytes", len);
618 printf("invaild Rwalk: less than two bytes (%d)",
623 memcpy(&slen, d, sizeof(slen));
626 slen = le16toh(slen);
628 if (len != QIDSIZE * slen) {
629 printf("invalid Rwalk: wanted %d bytes for %d qids "
630 "but got %"PRIu32" bytes instead",
631 QIDSIZE*slen, slen, len);
635 printf("nwqid=%"PRIu16, slen);
637 for (; slen != 0; slen--) {
647 memcpy(&slen, d, sizeof(slen));
650 slen = le16toh(slen);
653 printf("invalid: error string length doesn't "
654 "match. Got %d; want %d", slen, len);
659 fwrite(d, 1, slen, stdout);
665 printf("unknown command type");
672 handle_9p(const uint8_t *data, size_t size)
678 assert(size >= HEADERSIZE);
680 memcpy(&len, data, sizeof(len));
683 memcpy(&type, data, sizeof(type));
684 data += sizeof(type);
686 memcpy(&tag, data, sizeof(tag));
690 /* type is one byte long, no endianness issues */
694 pp_msg(len, type, tag, data);
708 printf("%s", PROMPT);
713 main(int argc, char **argv)
715 int ch, sock, handshake;
716 struct event ev_sigint, ev_sigterm;
718 signal(SIGPIPE, SIG_IGN);
720 while ((ch = getopt(argc, argv, "C:cH:hK:P:v")) != -1) {
758 /* if (!tls || (crtpath != NULL || keypath != NULL)) */
761 errx(1, "must enable tls (for now)");
763 log_init(1, LOG_DAEMON);
764 log_setverbose(verbose);
765 log_procinit(getprogname());
767 if ((tlsconf = tls_config_new()) == NULL)
768 fatalx("tls_config_new");
769 tls_config_insecure_noverifycert(tlsconf);
770 tls_config_insecure_noverifyname(tlsconf);
771 if (tls_config_set_keypair_file(tlsconf, crtpath, keypath) == -1)
772 fatalx("can't load certs (%s, %s)", crtpath, keypath);
774 if ((ctx = tls_client()) == NULL)
776 if (tls_configure(ctx, tlsconf) == -1)
777 fatalx("tls_configure: %s", tls_error(ctx));
779 log_info("connecting to %s:%s...", host, port);
781 if ((sock = openconn()) == -1)
782 fatalx("can't connect to %s:%s", host, port);
784 if (tls_connect_socket(ctx, sock, host) == -1)
785 fatalx("tls_connect_socket: %s", tls_error(ctx));
787 for (handshake = 0; !handshake;) {
788 switch (tls_handshake(ctx)) {
790 fatalx("tls_handshake: %s", tls_error(ctx));
797 log_info("connected!");
803 /* initialize global evb */
804 if ((evb = evbuffer_new()) == NULL)
805 fatal("evbuffer_new");
807 signal_set(&ev_sigint, SIGINT, sig_handler, NULL);
808 signal_set(&ev_sigterm, SIGINT, sig_handler, NULL);
810 signal_add(&ev_sigint, NULL);
811 signal_add(&ev_sigterm, NULL);
813 bev = bufferevent_new(sock, client_read, client_write, client_error,
816 fatal("bufferevent_new");
819 event_set(&bev->ev_read, sock, EV_READ, tls_readcb, bev);
820 event_set(&bev->ev_write, sock, EV_WRITE, tls_writecb, bev);
822 bufferevent_enable(bev, EV_READ|EV_WRITE);
825 inbev = bufferevent_new(0, repl_read, NULL, repl_error, NULL);
826 bufferevent_enable(inbev, EV_READ);
831 bufferevent_free(bev);
833 tls_config_free(tlsconf);