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>
41 #define DEBUG_PACKETS 0
54 struct tls_config *tlsconf;
56 struct bufferevent *bev, *inbev;
58 static void __dead usage(int);
60 static void sig_handler(int, short, void *);
62 static int openconn(void);
63 static void mark_nonblock(int);
65 static void tls_readcb(int, short, void *);
66 static void tls_writecb(int, short, void *);
68 static void client_read(struct bufferevent *, void *);
69 static void client_write(struct bufferevent *, void *);
70 static void client_error(struct bufferevent *, short, void *);
72 static void repl_read(struct bufferevent *, void *);
73 static void repl_error(struct bufferevent *, short, void *);
75 static void excmd_version(const char **, int);
76 static void excmd_attach(const char **, int);
77 static void excmd_clunk(const char **, int);
78 static void excmd_flush(const char **, int);
79 static void excmd_walk(const char ** , int);
80 static void excmd_open(const char ** , int);
81 static void excmd_create(const char ** , int);
82 static void excmd_read(const char ** , int);
83 static void excmd_write(const char **, int);
84 static void excmd(const char **, int);
86 static void pp_qid(const uint8_t *, uint32_t);
87 static void pp_msg(uint32_t, uint8_t, uint16_t, const uint8_t *);
88 static void handle_9p(const uint8_t *, size_t);
89 static void clr(void);
90 static void prompt(void);
96 "usage: %s [-chv] [-C crt] [-K key] [-H host] [-P port]\n",
98 fprintf(stderr, "kamid suite version " KAMID_VERSION "\n");
103 sig_handler(int sig, short event, void *d)
106 * Normal signal handler rules don't apply because libevent
114 log_warnx("Shutting down...");
118 fatalx("unexpected signal %d", sig);
125 struct addrinfo hints, *res, *res0;
129 const char *cause = NULL;
131 memset(&hints, 0, sizeof(hints));
132 hints.ai_family = AF_UNSPEC;
133 hints.ai_socktype = SOCK_STREAM;
134 if ((error = getaddrinfo(host, port, &hints, &res0))) {
135 warnx("%s", gai_strerror(error));
140 for (res = res0; res; res = res->ai_next) {
141 s = socket(res->ai_family, res->ai_socktype,
148 if (connect(s, res->ai_addr, res->ai_addrlen) == -1) {
169 mark_nonblock(int fd)
173 if ((flags = fcntl(fd, F_GETFL)) == -1)
174 fatal("fcntl(F_GETFL)");
175 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
176 fatal("fcntl(F_SETFL)");
180 tls_readcb(int fd, short event, void *d)
182 struct bufferevent *bufev = d;
183 char buf[IBUF_READ_SIZE];
184 int what = EVBUFFER_READ;
185 int howmuch = IBUF_READ_SIZE;
189 if (event == EV_TIMEOUT) {
190 what |= EVBUFFER_TIMEOUT;
194 if (bufev->wm_read.high != 0)
195 howmuch = MIN(sizeof(buf), bufev->wm_read.high);
197 switch (ret = tls_read(ctx, buf, howmuch)) {
198 case TLS_WANT_POLLIN:
199 case TLS_WANT_POLLOUT:
202 what |= EVBUFFER_ERROR;
208 what |= EVBUFFER_EOF;
212 if (evbuffer_add(bufev->input, buf, len) == -1) {
213 what |= EVBUFFER_ERROR;
217 event_add(&bufev->ev_read, NULL);
219 len = EVBUFFER_LENGTH(bufev->input);
220 if (bufev->wm_read.low != 0 && len < bufev->wm_read.low)
222 if (bufev->readcb != NULL)
223 (*bufev->readcb)(bufev, bufev->cbarg);
227 event_add(&bufev->ev_read, NULL);
231 (*bufev->errorcb)(bufev, what, bufev->cbarg);
235 tls_writecb(int fd, short event, void *d)
237 struct bufferevent *bufev = d;
240 short what = EVBUFFER_WRITE;
243 if (event == EV_TIMEOUT) {
244 what |= EVBUFFER_TIMEOUT;
248 len = EVBUFFER_LENGTH(bufev->output);
250 data = EVBUFFER_DATA(bufev->output);
253 hexdump("outgoing msg", data, len);
256 switch (ret = tls_write(ctx, data, len)) {
257 case TLS_WANT_POLLIN:
258 case TLS_WANT_POLLOUT:
261 what |= EVBUFFER_ERROR;
264 evbuffer_drain(bufev->output, ret);
267 if (EVBUFFER_LENGTH(bufev->output) != 0)
268 event_add(&bufev->ev_write, NULL);
270 if (bufev->writecb != NULL &&
271 EVBUFFER_LENGTH(bufev->output) <= bufev->wm_write.low)
272 (*bufev->writecb)(bufev, bufev->cbarg);
276 event_add(&bufev->ev_write, NULL);
279 (*bufev->errorcb)(bufev, what, bufev->cbarg);
283 client_read(struct bufferevent *bev, void *d)
285 struct evbuffer *src = EVBUFFER_INPUT(bev);
290 if (EVBUFFER_LENGTH(src) < sizeof(len))
293 data = EVBUFFER_DATA(src);
295 memcpy(&len, data, sizeof(len));
298 if (len < HEADERSIZE)
299 fatal("incoming message is too small! (%d bytes)",
302 if (len > EVBUFFER_LENGTH(src))
306 hexdump("incoming msg", data, len);
309 handle_9p(data, len);
310 evbuffer_drain(src, len);
315 client_write(struct bufferevent *bev, void *data)
317 return; /* nothing to do */
321 client_error(struct bufferevent *bev, short err, void *data)
323 if (err & EVBUFFER_ERROR)
324 fatal("buffer event error");
326 if (err & EVBUFFER_EOF) {
334 log_warnx("unknown event error");
339 repl_read(struct bufferevent *bev, void *d)
343 const char *argv[10], **ap;
346 line = evbuffer_readln(bev->input, &len, EVBUFFER_EOL_LF);
350 for (argc = 0, ap = argv; ap < &argv[9] &&
351 (*ap = strsep(&line, " \t")) != NULL;) {
364 repl_error(struct bufferevent *bev, short error, void *d)
366 fatalx("an error occurred");
372 bufferevent_write_buffer(bev, evb);
375 /* version [version-str] */
377 excmd_version(const char **argv, int argc)
385 tversion(s, MSIZE9P);
389 /* attach fid uname aname */
391 excmd_attach(const char **argv, int argc)
399 fid = strtonum(argv[1], 0, UINT32_MAX, &errstr);
400 if (errstr != NULL) {
401 log_warnx("fid is %s: %s", errstr, argv[1]);
405 tattach(fid, NOFID, argv[2], argv[3]);
410 log_warnx("usage: attach fid uname aname");
415 excmd_clunk(const char **argv, int argc)
423 fid = strtonum(argv[1], 0, UINT32_MAX, &errstr);
424 if (errstr != NULL) {
425 log_warnx("fid is %s: %s", errstr, argv[1]);
434 log_warnx("usage: clunk fid");
439 excmd_flush(const char **argv, int argc)
447 oldtag = strtonum(argv[1], 0, UINT16_MAX, &errstr);
448 if (errstr != NULL) {
449 log_warnx("oldtag is %s: %s", errstr, argv[1]);
458 log_warnx("usage: flush oldtag");
461 /* walk fid newfid wnames... */
463 excmd_walk(const char **argv, int argc)
465 uint32_t fid, newfid;
471 fid = strtonum(argv[1], 0, UINT32_MAX, &errstr);
472 if (errstr != NULL) {
473 log_warnx("fid is %s: %s", errstr, argv[1]);
477 newfid = strtonum(argv[2], 0, UINT32_MAX, &errstr);
478 if (errstr != NULL) {
479 log_warnx("newfid is %s: %s", errstr, argv[1]);
483 twalk(fid, newfid, argv + 3, argc - 3);
488 log_warnx("usage: walk fid newfid wnames...");
491 /* open fid mode [flag] */
493 excmd_open(const char **argv, int argc)
499 if (argc != 3 && argc != 4)
502 fid = strtonum(argv[1], 0, UINT32_MAX, &errstr);
503 if (errstr != NULL) {
504 log_warnx("fid is %s: %s", errstr, argv[1]);
509 if (!strcmp("read", argv[2]) || !strcmp("r", argv[2]))
511 else if (!strcmp("write", argv[2]) || !strcmp("w", argv[2]))
513 else if (!strcmp("readwrite", argv[2]) || !strcmp("rw", argv[2]))
516 log_warnx("invalid mode %s", argv[2]);
521 if (argv[3] != NULL) {
522 if (!strcmp("trunc", argv[3]))
524 else if (!strcmp("rclose", argv[3]))
527 log_warnx("invalid flag %s", argv[3]);
537 log_warnx("usage: open fid mode [flag]");
540 /* create fid path perm mode */
542 excmd_create(const char **argv, int argc)
551 fid = strtonum(argv[1], 0, UINT32_MAX, &errstr);
552 if (errstr != NULL) {
553 log_warnx("fid is %s: %s", errstr, argv[1]);
558 if (!strcmp("write", argv[4]) || !strcmp("w", argv[4]))
560 else if (!strcmp("readwrite", argv[4]) || !strcmp("rw", argv[4]))
563 log_warnx("invalid mode %s for create", argv[4]);
567 tcreate(fid, argv[2], 0, mode);
572 log_warnx("usage: create fid path perm mode ; perm is unused");
576 /* read fid offset count */
578 excmd_read(const char **argv, int argc)
587 fid = strtonum(argv[1], 0, UINT32_MAX, &errstr);
588 if (errstr != NULL) {
589 log_warnx("fid is %s: %s", errstr, argv[1]);
593 /* should really be UNT64_MAX but it fails... */
594 off = strtonum(argv[2], -1, UINT32_MAX, &errstr);
595 if (errstr != NULL) {
596 log_warnx("offset is %s: %s", errstr, argv[2]);
600 count = strtonum(argv[3], 0, UINT32_MAX, &errstr);
601 if (errstr != NULL) {
602 log_warnx("count is %s: %s", errstr, argv[3]);
606 tread(fid, off, count);
611 log_warnx("usage: read fid offset count");
614 /* write fid offset content */
616 excmd_write(const char **argv, int argc)
625 fid = strtonum(argv[1], 0, UINT32_MAX, &errstr);
626 if (errstr != NULL) {
627 log_warnx("fid is %s: %s", errstr, argv[1]);
631 /* should really be UINT64_MAX but... */
632 off = strtonum(argv[2], 0, UINT32_MAX, &errstr);
633 if (errstr != NULL) {
634 log_warnx("offset is %s: %s", errstr, argv[2]);
638 count = strlen(argv[3]);
639 twrite(fid, off, argv[3], count);
644 log_warnx("usage: write fid offset content");
649 excmd_remove(const char **argv, int argc)
657 fid = strtonum(argv[1], 0, UINT32_MAX, &errstr);
658 if (errstr != NULL) {
659 log_warnx("fid is %s: %s", errstr, argv[1]);
668 log_warnx("usage: remove fid");
672 excmd(const char **argv, int argc)
676 void (*fn)(const char **, int);
678 {"version", excmd_version},
679 {"attach", excmd_attach},
680 {"clunk", excmd_clunk},
681 {"flush", excmd_flush},
682 {"walk", excmd_walk},
683 {"open", excmd_open},
684 {"create", excmd_create},
685 {"read", excmd_read},
686 {"write", excmd_write},
688 {"remove", excmd_remove},
695 for (i = 0; i < sizeof(cmds)/sizeof(cmds[0]); ++i) {
696 if (!strcmp(cmds[i].name, argv[0])) {
697 cmds[i].fn(argv, argc);
702 log_warnx("Unknown command %s", *argv);
706 pp_qid(const uint8_t *d, uint32_t len)
719 memcpy(&vers, d, sizeof(vers));
721 vers = le64toh(vers);
723 memcpy(&path, d, sizeof(path));
725 path = le64toh(path);
727 printf("qid{path=%"PRIu64" version=%"PRIu32" type=0x%x\"%s\"}",
728 path, vers, type, pp_qid_type(type));
732 pp_msg(uint32_t len, uint8_t type, uint16_t tag, const uint8_t *d)
734 uint32_t msize, iounit, count;
738 printf("len=%"PRIu32" type=%d[%s] tag=0x%x[%d] ", len,
739 type, pp_msg_type(type), tag, tag);
746 printf("invalid: not enough space for msize "
747 "and version provided.");
751 memcpy(&msize, d, sizeof(msize));
753 len -= sizeof(msize);
754 msize = le32toh(msize);
756 memcpy(&slen, d, sizeof(slen));
759 slen = le16toh(slen);
762 printf("invalid: version string length doesn't "
763 "match. Got %d; want %d", slen, len);
767 printf("msize=%"PRIu32" version[%"PRIu16"]=\"",
769 fwrite(d, 1, slen, stdout);
782 printf("invalid %s: %"PRIu32" extra bytes",
783 pp_msg_type(type), len);
788 printf("invaild Rwalk: less than two bytes (%d)",
793 memcpy(&slen, d, sizeof(slen));
796 slen = le16toh(slen);
798 if (len != QIDSIZE * slen) {
799 printf("invalid Rwalk: wanted %d bytes for %d qids "
800 "but got %"PRIu32" bytes instead",
801 QIDSIZE*slen, slen, len);
805 printf("nwqid=%"PRIu16, slen);
807 for (; slen != 0; slen--) {
818 if (len != QIDSIZE + 4) {
819 printf("invalid %s: expected %d bytes; "
820 "got %u\n", pp_msg_type(type), QIDSIZE + 4, len);
828 memcpy(&iounit, d, sizeof(iounit));
830 len -= sizeof(iounit);
831 iounit = le32toh(iounit);
832 printf(" iounit=%"PRIu32, iounit);
836 if (len < sizeof(count)) {
837 printf("invalid Rread: expected %zu bytes at least; "
838 "got %u\n", sizeof(count), len);
842 memcpy(&count, d, sizeof(count));
844 len -= sizeof(count);
845 count = le32toh(count);
848 printf("invalid Rread: expected %d data bytes; "
849 "got %u\n", count, len);
853 /* allocates three extra bytes, oh well... */
854 if ((v = calloc(count + 1, 4)) == NULL)
856 strvisx(v, d, count, VIS_SAFE | VIS_TAB | VIS_NL | VIS_CSTYLE);
857 printf("data=%s", v);
863 if (len != sizeof(count)) {
864 printf("invalid Rwrite: expected %zu data bytes; "
865 "got %u\n", sizeof(count), len);
869 memcpy(&count, d, sizeof(count));
871 len -= sizeof(count);
872 count = le32toh(count);
874 printf("count=%d", count);
878 memcpy(&slen, d, sizeof(slen));
881 slen = le16toh(slen);
884 printf("invalid: error string length doesn't "
885 "match. Got %d; want %d", slen, len);
890 fwrite(d, 1, slen, stdout);
896 if ((v = calloc(len + 1, 4)) == NULL)
898 strvisx(v, d, len, VIS_SAFE | VIS_TAB | VIS_NL | VIS_CSTYLE);
899 printf("body=%s", v);
907 handle_9p(const uint8_t *data, size_t size)
913 assert(size >= HEADERSIZE);
915 memcpy(&len, data, sizeof(len));
918 memcpy(&type, data, sizeof(type));
919 data += sizeof(type);
921 memcpy(&tag, data, sizeof(tag));
925 /* type is one byte long, no endianness issues */
929 pp_msg(len, type, tag, data);
943 printf("%s", PROMPT);
948 main(int argc, char **argv)
950 int ch, sock, handshake;
951 struct event ev_sigint, ev_sigterm;
953 signal(SIGPIPE, SIG_IGN);
955 while ((ch = getopt(argc, argv, "C:cH:hK:P:v")) != -1) {
993 /* if (!tls || (crtpath != NULL || keypath != NULL)) */
996 errx(1, "must enable tls (for now)");
998 log_init(1, LOG_DAEMON);
999 log_setverbose(verbose);
1000 log_procinit(getprogname());
1002 if ((tlsconf = tls_config_new()) == NULL)
1003 fatalx("tls_config_new");
1004 tls_config_insecure_noverifycert(tlsconf);
1005 tls_config_insecure_noverifyname(tlsconf);
1006 if (tls_config_set_keypair_file(tlsconf, crtpath, keypath) == -1)
1007 fatalx("can't load certs (%s, %s)", crtpath, keypath);
1009 if ((ctx = tls_client()) == NULL)
1010 fatal("tls_client");
1011 if (tls_configure(ctx, tlsconf) == -1)
1012 fatalx("tls_configure: %s", tls_error(ctx));
1014 log_info("connecting to %s:%s...", host, port);
1016 if ((sock = openconn()) == -1)
1017 fatalx("can't connect to %s:%s", host, port);
1019 if (tls_connect_socket(ctx, sock, host) == -1)
1020 fatalx("tls_connect_socket: %s", tls_error(ctx));
1022 for (handshake = 0; !handshake;) {
1023 switch (tls_handshake(ctx)) {
1025 fatalx("tls_handshake: %s", tls_error(ctx));
1032 log_info("connected!");
1034 mark_nonblock(sock);
1038 /* initialize global evb */
1039 if ((evb = evbuffer_new()) == NULL)
1040 fatal("evbuffer_new");
1042 signal_set(&ev_sigint, SIGINT, sig_handler, NULL);
1043 signal_set(&ev_sigterm, SIGINT, sig_handler, NULL);
1045 signal_add(&ev_sigint, NULL);
1046 signal_add(&ev_sigterm, NULL);
1048 bev = bufferevent_new(sock, client_read, client_write, client_error,
1051 fatal("bufferevent_new");
1054 event_set(&bev->ev_read, sock, EV_READ, tls_readcb, bev);
1055 event_set(&bev->ev_write, sock, EV_WRITE, tls_writecb, bev);
1057 bufferevent_enable(bev, EV_READ|EV_WRITE);
1060 inbev = bufferevent_new(0, repl_read, NULL, repl_error, NULL);
1061 bufferevent_enable(inbev, EV_READ);
1066 bufferevent_free(bev);
1068 tls_config_free(tlsconf);