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 void pp_qid(const uint8_t *, uint32_t);
88 static void pp_msg(uint32_t, uint8_t, uint16_t, const uint8_t *);
89 static void handle_9p(const uint8_t *, size_t);
90 static void clr(void);
91 static void prompt(void);
97 "usage: %s [-chv] [-C crt] [-K key] [-H host] [-P port]\n",
99 fprintf(stderr, "kamid suite version " KAMID_VERSION "\n");
104 sig_handler(int sig, short event, void *d)
107 * Normal signal handler rules don't apply because libevent
115 log_warnx("Shutting down...");
119 fatalx("unexpected signal %d", sig);
126 struct addrinfo hints, *res, *res0;
130 const char *cause = NULL;
132 memset(&hints, 0, sizeof(hints));
133 hints.ai_family = AF_UNSPEC;
134 hints.ai_socktype = SOCK_STREAM;
135 if ((error = getaddrinfo(host, port, &hints, &res0))) {
136 warnx("%s", gai_strerror(error));
141 for (res = res0; res; res = res->ai_next) {
142 s = socket(res->ai_family, res->ai_socktype,
149 if (connect(s, res->ai_addr, res->ai_addrlen) == -1) {
170 mark_nonblock(int fd)
174 if ((flags = fcntl(fd, F_GETFL)) == -1)
175 fatal("fcntl(F_GETFL)");
176 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
177 fatal("fcntl(F_SETFL)");
181 tls_readcb(int fd, short event, void *d)
183 struct bufferevent *bufev = d;
184 char buf[IBUF_READ_SIZE];
185 int what = EVBUFFER_READ;
186 int howmuch = IBUF_READ_SIZE;
190 if (event == EV_TIMEOUT) {
191 what |= EVBUFFER_TIMEOUT;
195 if (bufev->wm_read.high != 0)
196 howmuch = MIN(sizeof(buf), bufev->wm_read.high);
198 switch (ret = tls_read(ctx, buf, howmuch)) {
199 case TLS_WANT_POLLIN:
200 case TLS_WANT_POLLOUT:
203 what |= EVBUFFER_ERROR;
209 what |= EVBUFFER_EOF;
213 if (evbuffer_add(bufev->input, buf, len) == -1) {
214 what |= EVBUFFER_ERROR;
218 event_add(&bufev->ev_read, NULL);
220 len = EVBUFFER_LENGTH(bufev->input);
221 if (bufev->wm_read.low != 0 && len < bufev->wm_read.low)
223 if (bufev->readcb != NULL)
224 (*bufev->readcb)(bufev, bufev->cbarg);
228 event_add(&bufev->ev_read, NULL);
232 (*bufev->errorcb)(bufev, what, bufev->cbarg);
236 tls_writecb(int fd, short event, void *d)
238 struct bufferevent *bufev = d;
241 short what = EVBUFFER_WRITE;
244 if (event == EV_TIMEOUT) {
245 what |= EVBUFFER_TIMEOUT;
249 len = EVBUFFER_LENGTH(bufev->output);
251 data = EVBUFFER_DATA(bufev->output);
254 hexdump("outgoing msg", data, len);
257 switch (ret = tls_write(ctx, data, len)) {
258 case TLS_WANT_POLLIN:
259 case TLS_WANT_POLLOUT:
262 what |= EVBUFFER_ERROR;
265 evbuffer_drain(bufev->output, ret);
268 if (EVBUFFER_LENGTH(bufev->output) != 0)
269 event_add(&bufev->ev_write, NULL);
271 if (bufev->writecb != NULL &&
272 EVBUFFER_LENGTH(bufev->output) <= bufev->wm_write.low)
273 (*bufev->writecb)(bufev, bufev->cbarg);
277 event_add(&bufev->ev_write, NULL);
280 (*bufev->errorcb)(bufev, what, bufev->cbarg);
284 client_read(struct bufferevent *bev, void *d)
286 struct evbuffer *src = EVBUFFER_INPUT(bev);
291 if (EVBUFFER_LENGTH(src) < sizeof(len))
294 data = EVBUFFER_DATA(src);
296 memcpy(&len, data, sizeof(len));
299 if (len < HEADERSIZE)
300 fatal("incoming message is too small! (%d bytes)",
303 if (len > EVBUFFER_LENGTH(src))
307 hexdump("incoming msg", data, len);
310 handle_9p(data, len);
311 evbuffer_drain(src, len);
316 client_write(struct bufferevent *bev, void *data)
318 return; /* nothing to do */
322 client_error(struct bufferevent *bev, short err, void *data)
324 if (err & EVBUFFER_ERROR)
325 fatal("buffer event error");
327 if (err & EVBUFFER_EOF) {
335 log_warnx("unknown event error");
340 repl_read(struct bufferevent *bev, void *d)
344 const char *argv[10], **ap;
347 line = evbuffer_readln(bev->input, &len, EVBUFFER_EOL_LF);
351 for (argc = 0, ap = argv; ap < &argv[9] &&
352 (*ap = strsep(&line, " \t")) != NULL;) {
365 repl_error(struct bufferevent *bev, short error, void *d)
367 fatalx("an error occurred");
373 bufferevent_write_buffer(bev, evb);
376 /* version [version-str] */
378 excmd_version(const char **argv, int argc)
386 tversion(s, MSIZE9P);
390 /* attach fid uname aname */
392 excmd_attach(const char **argv, int argc)
400 fid = strtonum(argv[1], 0, UINT32_MAX, &errstr);
401 if (errstr != NULL) {
402 log_warnx("fid is %s: %s", errstr, argv[1]);
406 tattach(fid, NOFID, argv[2], argv[3]);
411 log_warnx("usage: attach fid uname aname");
416 excmd_clunk(const char **argv, int argc)
424 fid = strtonum(argv[1], 0, UINT32_MAX, &errstr);
425 if (errstr != NULL) {
426 log_warnx("fid is %s: %s", errstr, argv[1]);
435 log_warnx("usage: clunk fid");
440 excmd_flush(const char **argv, int argc)
448 oldtag = strtonum(argv[1], 0, UINT16_MAX, &errstr);
449 if (errstr != NULL) {
450 log_warnx("oldtag is %s: %s", errstr, argv[1]);
459 log_warnx("usage: flush oldtag");
462 /* walk fid newfid wnames... */
464 excmd_walk(const char **argv, int argc)
466 uint32_t fid, newfid;
472 fid = strtonum(argv[1], 0, UINT32_MAX, &errstr);
473 if (errstr != NULL) {
474 log_warnx("fid is %s: %s", errstr, argv[1]);
478 newfid = strtonum(argv[2], 0, UINT32_MAX, &errstr);
479 if (errstr != NULL) {
480 log_warnx("newfid is %s: %s", errstr, argv[1]);
484 twalk(fid, newfid, argv + 3, argc - 3);
489 log_warnx("usage: walk fid newfid wnames...");
492 /* open fid mode [flag] */
494 excmd_open(const char **argv, int argc)
500 if (argc != 3 && argc != 4)
503 fid = strtonum(argv[1], 0, UINT32_MAX, &errstr);
504 if (errstr != NULL) {
505 log_warnx("fid is %s: %s", errstr, argv[1]);
510 if (!strcmp("read", argv[2]) || !strcmp("r", argv[2]))
512 else if (!strcmp("write", argv[2]) || !strcmp("w", argv[2]))
514 else if (!strcmp("readwrite", argv[2]) || !strcmp("rw", argv[2]))
517 log_warnx("invalid mode %s", argv[2]);
522 if (argv[3] != NULL) {
523 if (!strcmp("trunc", argv[3]))
525 else if (!strcmp("rclose", argv[3]))
528 log_warnx("invalid flag %s", argv[3]);
538 log_warnx("usage: open fid mode [flag]");
541 /* create fid path perm mode */
543 excmd_create(const char **argv, int argc)
552 fid = strtonum(argv[1], 0, UINT32_MAX, &errstr);
553 if (errstr != NULL) {
554 log_warnx("fid is %s: %s", errstr, argv[1]);
559 if (!strcmp("write", argv[4]) || !strcmp("w", argv[4]))
561 else if (!strcmp("readwrite", argv[4]) || !strcmp("rw", argv[4]))
564 log_warnx("invalid mode %s for create", argv[4]);
568 tcreate(fid, argv[2], 0, mode);
573 log_warnx("usage: create fid path perm mode ; perm is unused");
577 /* read fid offset count */
579 excmd_read(const char **argv, int argc)
588 fid = strtonum(argv[1], 0, UINT32_MAX, &errstr);
589 if (errstr != NULL) {
590 log_warnx("fid is %s: %s", errstr, argv[1]);
594 /* should really be UNT64_MAX but it fails... */
595 off = strtonum(argv[2], -1, UINT32_MAX, &errstr);
596 if (errstr != NULL) {
597 log_warnx("offset is %s: %s", errstr, argv[2]);
601 count = strtonum(argv[3], 0, UINT32_MAX, &errstr);
602 if (errstr != NULL) {
603 log_warnx("count is %s: %s", errstr, argv[3]);
607 tread(fid, off, count);
612 log_warnx("usage: read fid offset count");
615 /* write fid offset content */
617 excmd_write(const char **argv, int argc)
626 fid = strtonum(argv[1], 0, UINT32_MAX, &errstr);
627 if (errstr != NULL) {
628 log_warnx("fid is %s: %s", errstr, argv[1]);
632 /* should really be UINT64_MAX but... */
633 off = strtonum(argv[2], 0, UINT32_MAX, &errstr);
634 if (errstr != NULL) {
635 log_warnx("offset is %s: %s", errstr, argv[2]);
639 count = strlen(argv[3]);
640 twrite(fid, off, argv[3], count);
645 log_warnx("usage: write fid offset content");
650 excmd_remove(const char **argv, int argc)
658 fid = strtonum(argv[1], 0, UINT32_MAX, &errstr);
659 if (errstr != NULL) {
660 log_warnx("fid is %s: %s", errstr, argv[1]);
669 log_warnx("usage: remove fid");
673 excmd(const char **argv, int argc)
677 void (*fn)(const char **, int);
679 {"version", excmd_version},
680 {"attach", excmd_attach},
681 {"clunk", excmd_clunk},
682 {"flush", excmd_flush},
683 {"walk", excmd_walk},
684 {"open", excmd_open},
685 {"create", excmd_create},
686 {"read", excmd_read},
687 {"write", excmd_write},
689 {"remove", excmd_remove},
696 for (i = 0; i < sizeof(cmds)/sizeof(cmds[0]); ++i) {
697 if (!strcmp(cmds[i].name, argv[0])) {
698 cmds[i].fn(argv, argc);
703 log_warnx("Unknown command %s", *argv);
707 pp_qid(const uint8_t *d, uint32_t len)
720 memcpy(&vers, d, sizeof(vers));
722 vers = le64toh(vers);
724 memcpy(&path, d, sizeof(path));
726 path = le64toh(path);
728 printf("qid{path=%"PRIu64" version=%"PRIu32" type=0x%x\"%s\"}",
729 path, vers, type, pp_qid_type(type));
733 pp_msg(uint32_t len, uint8_t type, uint16_t tag, const uint8_t *d)
735 uint32_t msize, iounit, count;
739 printf("len=%"PRIu32" type=%d[%s] tag=0x%x[%d] ", len,
740 type, pp_msg_type(type), tag, tag);
747 printf("invalid: not enough space for msize "
748 "and version provided.");
752 memcpy(&msize, d, sizeof(msize));
754 len -= sizeof(msize);
755 msize = le32toh(msize);
757 memcpy(&slen, d, sizeof(slen));
760 slen = le16toh(slen);
763 printf("invalid: version string length doesn't "
764 "match. Got %d; want %d", slen, len);
768 printf("msize=%"PRIu32" version[%"PRIu16"]=\"",
770 fwrite(d, 1, slen, stdout);
783 printf("invalid %s: %"PRIu32" extra bytes",
784 pp_msg_type(type), len);
789 printf("invaild Rwalk: less than two bytes (%d)",
794 memcpy(&slen, d, sizeof(slen));
797 slen = le16toh(slen);
799 if (len != QIDSIZE * slen) {
800 printf("invalid Rwalk: wanted %d bytes for %d qids "
801 "but got %"PRIu32" bytes instead",
802 QIDSIZE*slen, slen, len);
806 printf("nwqid=%"PRIu16, slen);
808 for (; slen != 0; slen--) {
819 if (len != QIDSIZE + 4) {
820 printf("invalid %s: expected %d bytes; "
821 "got %u\n", pp_msg_type(type), QIDSIZE + 4, len);
829 memcpy(&iounit, d, sizeof(iounit));
831 len -= sizeof(iounit);
832 iounit = le32toh(iounit);
833 printf(" iounit=%"PRIu32, iounit);
837 if (len < sizeof(count)) {
838 printf("invalid Rread: expected %zu bytes at least; "
839 "got %u\n", sizeof(count), len);
843 memcpy(&count, d, sizeof(count));
845 len -= sizeof(count);
846 count = le32toh(count);
849 printf("invalid Rread: expected %d data bytes; "
850 "got %u\n", count, len);
854 /* allocates three extra bytes, oh well... */
855 if ((v = calloc(count + 1, 4)) == NULL)
857 strvisx(v, d, count, VIS_SAFE | VIS_TAB | VIS_NL | VIS_CSTYLE);
858 printf("data=%s", v);
864 if (len != sizeof(count)) {
865 printf("invalid Rwrite: expected %zu data bytes; "
866 "got %u\n", sizeof(count), len);
870 memcpy(&count, d, sizeof(count));
872 len -= sizeof(count);
873 count = le32toh(count);
875 printf("count=%d", count);
879 memcpy(&slen, d, sizeof(slen));
882 slen = le16toh(slen);
885 printf("invalid: error string length doesn't "
886 "match. Got %d; want %d", slen, len);
891 fwrite(d, 1, slen, stdout);
897 if ((v = calloc(len + 1, 4)) == NULL)
899 strvisx(v, d, len, VIS_SAFE | VIS_TAB | VIS_NL | VIS_CSTYLE);
900 printf("body=%s", v);
908 handle_9p(const uint8_t *data, size_t size)
914 assert(size >= HEADERSIZE);
916 memcpy(&len, data, sizeof(len));
919 memcpy(&type, data, sizeof(type));
920 data += sizeof(type);
922 memcpy(&tag, data, sizeof(tag));
926 /* type is one byte long, no endianness issues */
930 pp_msg(len, type, tag, data);
944 printf("%s", PROMPT);
949 main(int argc, char **argv)
951 int ch, sock, handshake;
952 struct event ev_sigint, ev_sigterm;
954 signal(SIGPIPE, SIG_IGN);
956 while ((ch = getopt(argc, argv, "C:cH:hK:P:v")) != -1) {
994 /* if (!tls || (crtpath != NULL || keypath != NULL)) */
997 errx(1, "must enable tls (for now)");
999 log_init(1, LOG_DAEMON);
1000 log_setverbose(verbose);
1001 log_procinit(getprogname());
1003 if ((tlsconf = tls_config_new()) == NULL)
1004 fatalx("tls_config_new");
1005 tls_config_insecure_noverifycert(tlsconf);
1006 tls_config_insecure_noverifyname(tlsconf);
1007 if (tls_config_set_keypair_file(tlsconf, crtpath, keypath) == -1)
1008 fatalx("can't load certs (%s, %s)", crtpath, keypath);
1010 if ((ctx = tls_client()) == NULL)
1011 fatal("tls_client");
1012 if (tls_configure(ctx, tlsconf) == -1)
1013 fatalx("tls_configure: %s", tls_error(ctx));
1015 log_info("connecting to %s:%s...", host, port);
1017 if ((sock = openconn()) == -1)
1018 fatalx("can't connect to %s:%s", host, port);
1020 if (tls_connect_socket(ctx, sock, host) == -1)
1021 fatalx("tls_connect_socket: %s", tls_error(ctx));
1023 for (handshake = 0; !handshake;) {
1024 switch (tls_handshake(ctx)) {
1026 fatalx("tls_handshake: %s", tls_error(ctx));
1033 log_info("connected!");
1035 mark_nonblock(sock);
1039 /* initialize global evb */
1040 if ((evb = evbuffer_new()) == NULL)
1041 fatal("evbuffer_new");
1043 signal_set(&ev_sigint, SIGINT, sig_handler, NULL);
1044 signal_set(&ev_sigterm, SIGINT, sig_handler, NULL);
1046 signal_add(&ev_sigint, NULL);
1047 signal_add(&ev_sigterm, NULL);
1049 bev = bufferevent_new(sock, client_read, client_write, client_error,
1052 fatal("bufferevent_new");
1055 event_set(&bev->ev_read, sock, EV_READ, tls_readcb, bev);
1056 event_set(&bev->ev_write, sock, EV_WRITE, tls_writecb, bev);
1058 bufferevent_enable(bev, EV_READ|EV_WRITE);
1061 inbev = bufferevent_new(0, repl_read, NULL, repl_error, NULL);
1062 bufferevent_enable(inbev, EV_READ);
1067 bufferevent_free(bev);
1069 tls_config_free(tlsconf);