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 __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_open(const char ** , int);
82 static void excmd_create(const char ** , int);
83 static void excmd_read(const char ** , int);
84 static void excmd_write(const char **, int);
85 static void excmd(const char **, int);
87 static const char *pp_qid_type(uint8_t);
88 static void pp_qid(const uint8_t *, uint32_t);
89 static void pp_msg(uint32_t, uint8_t, uint16_t, const uint8_t *);
90 static void handle_9p(const uint8_t *, size_t);
91 static void clr(void);
92 static void prompt(void);
98 "usage: %s [-chv] [-C crt] [-K key] [-H host] [-P port]\n",
100 fprintf(stderr, PACKAGE_NAME " suite version " PACKAGE_VERSION "\n");
105 sig_handler(int sig, short event, void *d)
108 * Normal signal handler rules don't apply because libevent
116 log_warnx("Shutting down...");
120 fatalx("unexpected signal %d", sig);
127 struct addrinfo hints, *res, *res0;
131 const char *cause = NULL;
133 memset(&hints, 0, sizeof(hints));
134 hints.ai_family = AF_UNSPEC;
135 hints.ai_socktype = SOCK_STREAM;
136 if ((error = getaddrinfo(host, port, &hints, &res0))) {
137 warnx("%s", gai_strerror(error));
142 for (res = res0; res; res = res->ai_next) {
143 s = socket(res->ai_family, res->ai_socktype,
150 if (connect(s, res->ai_addr, res->ai_addrlen) == -1) {
171 mark_nonblock(int fd)
175 if ((flags = fcntl(fd, F_GETFL)) == -1)
176 fatal("fcntl(F_GETFL)");
177 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
178 fatal("fcntl(F_SETFL)");
182 tls_readcb(int fd, short event, void *d)
184 struct bufferevent *bufev = d;
185 char buf[IBUF_READ_SIZE];
186 int what = EVBUFFER_READ;
187 int howmuch = IBUF_READ_SIZE;
191 if (event == EV_TIMEOUT) {
192 what |= EVBUFFER_TIMEOUT;
196 if (bufev->wm_read.high != 0)
197 howmuch = MIN(sizeof(buf), bufev->wm_read.high);
199 switch (ret = tls_read(ctx, buf, howmuch)) {
200 case TLS_WANT_POLLIN:
201 case TLS_WANT_POLLOUT:
204 what |= EVBUFFER_ERROR;
210 what |= EVBUFFER_EOF;
214 if (evbuffer_add(bufev->input, buf, len) == -1) {
215 what |= EVBUFFER_ERROR;
219 event_add(&bufev->ev_read, NULL);
221 len = EVBUFFER_LENGTH(bufev->input);
222 if (bufev->wm_read.low != 0 && len < bufev->wm_read.low)
224 if (bufev->readcb != NULL)
225 (*bufev->readcb)(bufev, bufev->cbarg);
229 event_add(&bufev->ev_read, NULL);
233 (*bufev->errorcb)(bufev, what, bufev->cbarg);
237 tls_writecb(int fd, short event, void *d)
239 struct bufferevent *bufev = d;
242 short what = EVBUFFER_WRITE;
245 if (event == EV_TIMEOUT) {
246 what |= EVBUFFER_TIMEOUT;
250 len = EVBUFFER_LENGTH(bufev->output);
252 data = EVBUFFER_DATA(bufev->output);
255 hexdump("outgoing msg", data, len);
258 switch (ret = tls_write(ctx, data, len)) {
259 case TLS_WANT_POLLIN:
260 case TLS_WANT_POLLOUT:
263 what |= EVBUFFER_ERROR;
266 evbuffer_drain(bufev->output, ret);
269 if (EVBUFFER_LENGTH(bufev->output) != 0)
270 event_add(&bufev->ev_write, NULL);
272 if (bufev->writecb != NULL &&
273 EVBUFFER_LENGTH(bufev->output) <= bufev->wm_write.low)
274 (*bufev->writecb)(bufev, bufev->cbarg);
278 event_add(&bufev->ev_write, NULL);
281 (*bufev->errorcb)(bufev, what, bufev->cbarg);
285 client_read(struct bufferevent *bev, void *d)
287 struct evbuffer *src = EVBUFFER_INPUT(bev);
292 if (EVBUFFER_LENGTH(src) < sizeof(len))
295 data = EVBUFFER_DATA(src);
297 memcpy(&len, data, sizeof(len));
300 if (len < HEADERSIZE)
301 fatal("incoming message is too small! (%d bytes)",
304 if (len > EVBUFFER_LENGTH(src))
308 hexdump("incoming msg", data, len);
311 handle_9p(data, len);
312 evbuffer_drain(src, len);
317 client_write(struct bufferevent *bev, void *data)
319 return; /* nothing to do */
323 client_error(struct bufferevent *bev, short err, void *data)
325 if (err & EVBUFFER_ERROR)
326 fatal("buffer event error");
328 if (err & EVBUFFER_EOF) {
336 log_warnx("unknown event error");
341 repl_read(struct bufferevent *bev, void *d)
345 const char *argv[10], **ap;
348 line = evbuffer_readln(bev->input, &len, EVBUFFER_EOL_LF);
352 for (argc = 0, ap = argv; ap < &argv[9] &&
353 (*ap = strsep(&line, " \t")) != NULL;) {
366 repl_error(struct bufferevent *bev, short error, void *d)
368 fatalx("an error occurred");
374 bufferevent_write_buffer(bev, evb);
377 /* version [version-str] */
379 excmd_version(const char **argv, int argc)
387 tversion(s, MSIZE9P);
391 /* attach fid uname aname */
393 excmd_attach(const char **argv, int argc)
401 fid = strtonum(argv[1], 0, UINT32_MAX, &errstr);
402 if (errstr != NULL) {
403 log_warnx("fid is %s: %s", errstr, argv[1]);
407 tattach(fid, NOFID, argv[2], argv[3]);
412 log_warnx("usage: attach fid uname aname");
417 excmd_clunk(const char **argv, int argc)
425 fid = strtonum(argv[1], 0, UINT32_MAX, &errstr);
426 if (errstr != NULL) {
427 log_warnx("fid is %s: %s", errstr, argv[1]);
436 log_warnx("usage: clunk fid");
441 excmd_flush(const char **argv, int argc)
449 oldtag = strtonum(argv[1], 0, UINT16_MAX, &errstr);
450 if (errstr != NULL) {
451 log_warnx("oldtag is %s: %s", errstr, argv[1]);
460 log_warnx("usage: flush oldtag");
463 /* walk fid newfid wnames... */
465 excmd_walk(const char **argv, int argc)
467 uint32_t fid, newfid;
473 fid = strtonum(argv[1], 0, UINT32_MAX, &errstr);
474 if (errstr != NULL) {
475 log_warnx("fid is %s: %s", errstr, argv[1]);
479 newfid = strtonum(argv[2], 0, UINT32_MAX, &errstr);
480 if (errstr != NULL) {
481 log_warnx("newfid is %s: %s", errstr, argv[1]);
485 twalk(fid, newfid, argv + 3, argc - 3);
490 log_warnx("usage: walk fid newfid wnames...");
493 /* open fid mode [flag] */
495 excmd_open(const char **argv, int argc)
501 if (argc != 3 && argc != 4)
504 fid = strtonum(argv[1], 0, UINT32_MAX, &errstr);
505 if (errstr != NULL) {
506 log_warnx("fid is %s: %s", errstr, argv[1]);
511 if (!strcmp("read", argv[2]) || !strcmp("r", argv[2]))
513 else if (!strcmp("write", argv[2]) || !strcmp("w", argv[2]))
515 else if (!strcmp("readwrite", argv[2]) || !strcmp("rw", argv[2]))
518 log_warnx("invalid mode %s", argv[2]);
523 if (argv[3] != NULL) {
524 if (!strcmp("trunc", argv[3]))
526 else if (!strcmp("rclose", argv[3]))
529 log_warnx("invalid flag %s", argv[3]);
539 log_warnx("usage: open fid mode [flag]");
542 /* create fid path perm mode */
544 excmd_create(const char **argv, int argc)
553 fid = strtonum(argv[1], 0, UINT32_MAX, &errstr);
554 if (errstr != NULL) {
555 log_warnx("fid is %s: %s", errstr, argv[1]);
560 if (!strcmp("write", argv[4]) || !strcmp("w", argv[4]))
562 else if (!strcmp("readwrite", argv[4]) || !strcmp("rw", argv[4]))
565 log_warnx("invalid mode %s for create", argv[4]);
569 tcreate(fid, argv[2], 0, mode);
574 log_warnx("usage: create fid path perm mode ; perm is unused");
578 /* read fid offset count */
580 excmd_read(const char **argv, int argc)
589 fid = strtonum(argv[1], 0, UINT32_MAX, &errstr);
590 if (errstr != NULL) {
591 log_warnx("fid is %s: %s", errstr, argv[1]);
595 /* should really be UNT64_MAX but it fails... */
596 off = strtonum(argv[2], -1, UINT32_MAX, &errstr);
597 if (errstr != NULL) {
598 log_warnx("offset is %s: %s", errstr, argv[2]);
602 count = strtonum(argv[3], 0, UINT32_MAX, &errstr);
603 if (errstr != NULL) {
604 log_warnx("count is %s: %s", errstr, argv[3]);
608 tread(fid, off, count);
613 log_warnx("usage: read fid offset count");
616 /* write fid offset content */
618 excmd_write(const char **argv, int argc)
627 fid = strtonum(argv[1], 0, UINT32_MAX, &errstr);
628 if (errstr != NULL) {
629 log_warnx("fid is %s: %s", errstr, argv[1]);
633 /* should really be UINT64_MAX but... */
634 off = strtonum(argv[2], 0, UINT32_MAX, &errstr);
635 if (errstr != NULL) {
636 log_warnx("offset is %s: %s", errstr, argv[2]);
640 count = strlen(argv[3]);
641 twrite(fid, off, argv[3], count);
646 log_warnx("usage: write fid offset content");
651 excmd_remove(const char **argv, int argc)
659 fid = strtonum(argv[1], 0, UINT32_MAX, &errstr);
660 if (errstr != NULL) {
661 log_warnx("fid is %s: %s", errstr, argv[1]);
670 log_warnx("usage: remove fid");
674 excmd(const char **argv, int argc)
678 void (*fn)(const char **, int);
680 {"version", excmd_version},
681 {"attach", excmd_attach},
682 {"clunk", excmd_clunk},
683 {"flush", excmd_flush},
684 {"walk", excmd_walk},
685 {"open", excmd_open},
686 {"create", excmd_create},
687 {"read", excmd_read},
688 {"write", excmd_write},
690 {"remove", excmd_remove},
697 for (i = 0; i < sizeof(cmds)/sizeof(cmds[0]); ++i) {
698 if (!strcmp(cmds[i].name, argv[0])) {
699 cmds[i].fn(argv, argc);
704 log_warnx("Unknown command %s", *argv);
708 pp_qid_type(uint8_t type)
711 case QTDIR: return "dir";
712 case QTAPPEND: return "append-only";
713 case QTEXCL: return "exclusive";
714 case QTMOUNT: return "mounted-channel";
715 case QTAUTH: return "authentication";
716 case QTTMP: return "non-backed-up";
717 case QTSYMLINK: return "symlink";
718 case QTFILE: return "file";
725 pp_qid(const uint8_t *d, uint32_t len)
738 memcpy(&vers, d, sizeof(vers));
740 vers = le64toh(vers);
742 memcpy(&path, d, sizeof(path));
744 path = le64toh(path);
746 printf("qid{path=%"PRIu64" version=%"PRIu32" type=0x%x\"%s\"}",
747 path, vers, type, pp_qid_type(type));
751 pp_msg(uint32_t len, uint8_t type, uint16_t tag, const uint8_t *d)
753 uint32_t msize, iounit, count;
757 printf("len=%"PRIu32" type=%d[%s] tag=0x%x[%d] ", len,
758 type, pp_msg_type(type), tag, tag);
765 printf("invalid: not enough space for msize "
766 "and version provided.");
770 memcpy(&msize, d, sizeof(msize));
772 len -= sizeof(msize);
773 msize = le32toh(msize);
775 memcpy(&slen, d, sizeof(slen));
778 slen = le16toh(slen);
781 printf("invalid: version string length doesn't "
782 "match. Got %d; want %d", slen, len);
786 printf("msize=%"PRIu32" version[%"PRIu16"]=\"",
788 fwrite(d, 1, slen, stdout);
801 printf("invalid %s: %"PRIu32" extra bytes",
802 pp_msg_type(type), len);
807 printf("invaild Rwalk: less than two bytes (%d)",
812 memcpy(&slen, d, sizeof(slen));
815 slen = le16toh(slen);
817 if (len != QIDSIZE * slen) {
818 printf("invalid Rwalk: wanted %d bytes for %d qids "
819 "but got %"PRIu32" bytes instead",
820 QIDSIZE*slen, slen, len);
824 printf("nwqid=%"PRIu16, slen);
826 for (; slen != 0; slen--) {
837 if (len != QIDSIZE + 4) {
838 printf("invalid %s: expected %d bytes; "
839 "got %u\n", pp_msg_type(type), QIDSIZE + 4, len);
847 memcpy(&iounit, d, sizeof(iounit));
849 len -= sizeof(iounit);
850 iounit = le32toh(iounit);
851 printf(" iounit=%"PRIu32, iounit);
855 if (len < sizeof(count)) {
856 printf("invalid Rread: expected %zu bytes at least; "
857 "got %u\n", sizeof(count), len);
861 memcpy(&count, d, sizeof(count));
863 len -= sizeof(count);
864 count = le32toh(count);
867 printf("invalid Rread: expected %d data bytes; "
868 "got %u\n", count, len);
872 /* allocates three extra bytes, oh well... */
873 if ((v = calloc(count + 1, 4)) == NULL)
875 strvisx(v, d, count, VIS_SAFE | VIS_TAB | VIS_NL | VIS_CSTYLE);
876 printf("data=%s", v);
882 if (len != sizeof(count)) {
883 printf("invalid Rwrite: expected %zu data bytes; "
884 "got %u\n", sizeof(count), len);
888 memcpy(&count, d, sizeof(count));
890 len -= sizeof(count);
891 count = le32toh(count);
893 printf("count=%d", count);
897 memcpy(&slen, d, sizeof(slen));
900 slen = le16toh(slen);
903 printf("invalid: error string length doesn't "
904 "match. Got %d; want %d", slen, len);
909 fwrite(d, 1, slen, stdout);
915 if ((v = calloc(len + 1, 4)) == NULL)
917 strvisx(v, d, len, VIS_SAFE | VIS_TAB | VIS_NL | VIS_CSTYLE);
918 printf("body=%s", v);
926 handle_9p(const uint8_t *data, size_t size)
932 assert(size >= HEADERSIZE);
934 memcpy(&len, data, sizeof(len));
937 memcpy(&type, data, sizeof(type));
938 data += sizeof(type);
940 memcpy(&tag, data, sizeof(tag));
944 /* type is one byte long, no endianness issues */
948 pp_msg(len, type, tag, data);
962 printf("%s", PROMPT);
967 main(int argc, char **argv)
969 int ch, sock, handshake;
970 struct event ev_sigint, ev_sigterm;
972 signal(SIGPIPE, SIG_IGN);
974 while ((ch = getopt(argc, argv, "C:cH:hK:P:v")) != -1) {
1012 /* if (!tls || (crtpath != NULL || keypath != NULL)) */
1015 errx(1, "must enable tls (for now)");
1017 log_init(1, LOG_DAEMON);
1018 log_setverbose(verbose);
1019 log_procinit(getprogname());
1021 if ((tlsconf = tls_config_new()) == NULL)
1022 fatalx("tls_config_new");
1023 tls_config_insecure_noverifycert(tlsconf);
1024 tls_config_insecure_noverifyname(tlsconf);
1025 if (tls_config_set_keypair_file(tlsconf, crtpath, keypath) == -1)
1026 fatalx("can't load certs (%s, %s)", crtpath, keypath);
1028 if ((ctx = tls_client()) == NULL)
1029 fatal("tls_client");
1030 if (tls_configure(ctx, tlsconf) == -1)
1031 fatalx("tls_configure: %s", tls_error(ctx));
1033 log_info("connecting to %s:%s...", host, port);
1035 if ((sock = openconn()) == -1)
1036 fatalx("can't connect to %s:%s", host, port);
1038 if (tls_connect_socket(ctx, sock, host) == -1)
1039 fatalx("tls_connect_socket: %s", tls_error(ctx));
1041 for (handshake = 0; !handshake;) {
1042 switch (tls_handshake(ctx)) {
1044 fatalx("tls_handshake: %s", tls_error(ctx));
1051 log_info("connected!");
1053 mark_nonblock(sock);
1057 /* initialize global evb */
1058 if ((evb = evbuffer_new()) == NULL)
1059 fatal("evbuffer_new");
1061 signal_set(&ev_sigint, SIGINT, sig_handler, NULL);
1062 signal_set(&ev_sigterm, SIGINT, sig_handler, NULL);
1064 signal_add(&ev_sigint, NULL);
1065 signal_add(&ev_sigterm, NULL);
1067 bev = bufferevent_new(sock, client_read, client_write, client_error,
1070 fatal("bufferevent_new");
1073 event_set(&bev->ev_read, sock, EV_READ, tls_readcb, bev);
1074 event_set(&bev->ev_write, sock, EV_WRITE, tls_writecb, bev);
1076 bufferevent_enable(bev, EV_READ|EV_WRITE);
1079 inbev = bufferevent_new(0, repl_read, NULL, repl_error, NULL);
1080 bufferevent_enable(inbev, EV_READ);
1085 bufferevent_free(bev);
1087 tls_config_free(tlsconf);