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 ATTR_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 *);
74 static void write_hdr(uint32_t, uint8_t, uint16_t);
75 static void write_hdr_auto(uint32_t, uint8_t);
76 static void write_str(uint16_t, const char *);
77 static void write_str_auto(const char *);
78 static void write_fid(uint32_t);
79 static void write_tag(uint16_t);
81 static void excmd_version(const char **, int);
82 static void excmd_attach(const char **, int);
83 static void excmd_clunk(const char **, int);
84 static void excmd_flush(const char **, int);
85 static void excmd_walk(const char ** , int);
86 static void excmd(const char **, int);
88 static const char *pp_qid_type(uint8_t);
89 static void pp_qid(const uint8_t *, uint32_t);
90 static void pp_msg(uint32_t, uint8_t, uint16_t, const uint8_t *);
91 static void handle_9p(const uint8_t *, size_t);
92 static void clr(void);
93 static void prompt(void);
99 "usage: %s [-chv] [-C crt] [-K key] [-H host] [-P port]\n",
101 fprintf(stderr, PACKAGE_NAME " suite version " PACKAGE_VERSION "\n");
106 sig_handler(int sig, short event, void *d)
109 * Normal signal handler rules don't apply because libevent
117 log_warnx("Shutting down...");
121 fatalx("unexpected signal %d", sig);
128 struct addrinfo hints, *res, *res0;
132 const char *cause = NULL;
134 memset(&hints, 0, sizeof(hints));
135 hints.ai_family = AF_UNSPEC;
136 hints.ai_socktype = SOCK_STREAM;
137 if ((error = getaddrinfo(host, port, &hints, &res0))) {
138 warnx("%s", gai_strerror(error));
143 for (res = res0; res; res = res->ai_next) {
144 s = socket(res->ai_family, res->ai_socktype,
151 if (connect(s, res->ai_addr, res->ai_addrlen) == -1) {
172 mark_nonblock(int fd)
176 if ((flags = fcntl(fd, F_GETFL)) == -1)
177 fatal("fcntl(F_GETFL)");
178 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
179 fatal("fcntl(F_SETFL)");
183 tls_readcb(int fd, short event, void *d)
185 struct bufferevent *bufev = d;
186 char buf[IBUF_READ_SIZE];
187 int what = EVBUFFER_READ;
188 int howmuch = IBUF_READ_SIZE;
192 if (event == EV_TIMEOUT) {
193 what |= EVBUFFER_TIMEOUT;
197 if (bufev->wm_read.high != 0)
198 howmuch = MIN(sizeof(buf), bufev->wm_read.high);
200 switch (ret = tls_read(ctx, buf, howmuch)) {
201 case TLS_WANT_POLLIN:
202 case TLS_WANT_POLLOUT:
205 what |= EVBUFFER_ERROR;
211 what |= EVBUFFER_EOF;
215 if (evbuffer_add(bufev->input, buf, len) == -1) {
216 what |= EVBUFFER_ERROR;
220 event_add(&bufev->ev_read, NULL);
222 len = EVBUFFER_LENGTH(bufev->input);
223 if (bufev->wm_read.low != 0 && len < bufev->wm_read.low)
225 if (bufev->readcb != NULL)
226 (*bufev->readcb)(bufev, bufev->cbarg);
230 event_add(&bufev->ev_read, NULL);
234 (*bufev->errorcb)(bufev, what, bufev->cbarg);
238 tls_writecb(int fd, short event, void *d)
240 struct bufferevent *bufev = d;
243 short what = EVBUFFER_WRITE;
246 if (event == EV_TIMEOUT) {
247 what |= EVBUFFER_TIMEOUT;
251 len = EVBUFFER_LENGTH(bufev->output);
253 data = EVBUFFER_DATA(bufev->output);
256 hexdump("outgoing msg", data, len);
259 switch (ret = tls_write(ctx, data, len)) {
260 case TLS_WANT_POLLIN:
261 case TLS_WANT_POLLOUT:
264 what |= EVBUFFER_ERROR;
267 evbuffer_drain(bufev->output, ret);
270 if (EVBUFFER_LENGTH(bufev->output) != 0)
271 event_add(&bufev->ev_write, NULL);
273 if (bufev->writecb != NULL &&
274 EVBUFFER_LENGTH(bufev->output) <= bufev->wm_write.low)
275 (*bufev->writecb)(bufev, bufev->cbarg);
279 event_add(&bufev->ev_write, NULL);
282 (*bufev->errorcb)(bufev, what, bufev->cbarg);
286 client_read(struct bufferevent *bev, void *d)
288 struct evbuffer *src = EVBUFFER_INPUT(bev);
293 if (EVBUFFER_LENGTH(src) < sizeof(len))
296 data = EVBUFFER_DATA(src);
298 memcpy(&len, data, sizeof(len));
301 if (len < HEADERSIZE)
302 fatal("incoming message is too small! (%d bytes)",
305 if (len > EVBUFFER_LENGTH(src))
309 hexdump("incoming msg", data, len);
312 handle_9p(data, len);
313 evbuffer_drain(src, len);
318 client_write(struct bufferevent *bev, void *data)
320 return; /* nothing to do */
324 client_error(struct bufferevent *bev, short err, void *data)
326 if (err & EVBUFFER_ERROR)
327 fatal("buffer event error");
329 if (err & EVBUFFER_EOF) {
337 log_warnx("unknown event error");
342 repl_read(struct bufferevent *bev, void *d)
346 const char *argv[10], **ap;
349 line = evbuffer_readln(bev->input, &len, EVBUFFER_EOL_LF);
353 for (argc = 0, ap = argv; ap < &argv[9] &&
354 (*ap = strsep(&line, " \t")) != NULL;) {
367 repl_error(struct bufferevent *bev, short error, void *d)
369 fatalx("an error occurred");
373 write_hdr(uint32_t len, uint8_t type, uint16_t tag)
377 log_debug("enqueuing a packet; len=%"PRIu32" type=%d[%s] tag=%d",
378 len, type, pp_msg_type(type), tag);
381 /* type is one byte, no endiannes issues */
384 bufferevent_write(bev, &len, sizeof(len));
385 bufferevent_write(bev, &type, sizeof(type));
386 bufferevent_write(bev, &tag, sizeof(tag));
390 write_hdr_auto(uint32_t len, uint8_t type)
392 static uint16_t tag = 0;
397 write_hdr(len, type, tag);
401 write_str(uint16_t len, const char *str)
406 bufferevent_write(bev, &len, sizeof(len));
407 bufferevent_write(bev, str, l);
411 write_str_auto(const char *str)
413 write_str(strlen(str), str);
417 write_fid(uint32_t fid)
420 bufferevent_write(bev, &fid, sizeof(fid));
424 write_tag(uint16_t tag)
427 bufferevent_write(bev, &tag, sizeof(tag));
430 /* version [version-str] */
432 excmd_version(const char **argv, int argc)
444 /* msize[4] version[s] */
445 len = 4 + sizeof(sl) + sl;
446 write_hdr(len, Tversion, NOTAG);
448 msize = htole32(MSIZE9P);
449 bufferevent_write(bev, &msize, sizeof(msize));
454 /* attach fid uname aname */
456 excmd_attach(const char **argv, int argc)
460 const char *s, *t, *errstr;
465 fid = strtonum(argv[1], 0, UINT32_MAX, &errstr);
466 if (errstr != NULL) {
467 log_warnx("fid is %s: %s", errstr, argv[1]);
476 /* fid[4] afid[4] uname[s] aname[s] */
477 len = 4 + 4 + sizeof(sl) + sl + sizeof(tl) + tl;
478 write_hdr_auto(len, Tattach);
487 log_warnx("usage: attach fid uname aname");
492 excmd_clunk(const char **argv, int argc)
500 fid = strtonum(argv[1], 0, UINT32_MAX, &errstr);
501 if (errstr != NULL) {
502 log_warnx("fid is %s: %s", errstr, argv[1]);
508 write_hdr_auto(len, Tclunk);
513 log_warnx("usage: clunk fid");
518 excmd_flush(const char **argv, int argc)
527 oldtag = strtonum(argv[1], 0, UINT16_MAX, &errstr);
528 if (errstr != NULL) {
529 log_warnx("oldtag is %s: %s", errstr, argv[1]);
534 len = sizeof(oldtag);
535 write_hdr_auto(len, Tflush);
540 log_warnx("usage: flush oldtag");
543 /* walk fid newfid wnames... */
545 excmd_walk(const char **argv, int argc)
548 uint32_t len, fid, newfid;
554 /* fid[4] newfid[4] nwname[2] nwname*(wname[s]) */
556 /* two bytes for wnames count */
557 len = sizeof(fid) + sizeof(newfid) + 2;
558 for (i = 3; i < argc; ++i)
559 len += 2 + strlen(argv[i]);
561 fid = strtonum(argv[1], 0, UINT32_MAX, &errstr);
562 if (errstr != NULL) {
563 log_warnx("fid is %s: %s", errstr, argv[1]);
567 newfid = strtonum(argv[2], 0, UINT32_MAX, &errstr);
568 if (errstr != NULL) {
569 log_warnx("newfid is %s: %s", errstr, argv[1]);
573 write_hdr_auto(len, Twalk);
577 for (i = 3; i < argc; ++i)
578 write_str_auto(argv[i]);
583 log_warnx("usage: walk fid newfid wnames...");
587 excmd(const char **argv, int argc)
591 void (*fn)(const char **, int);
593 {"version", excmd_version},
594 {"attach", excmd_attach},
595 {"clunk", excmd_clunk},
596 {"flush", excmd_flush},
597 {"walk", excmd_walk},
604 for (i = 0; i < sizeof(cmds)/sizeof(cmds[0]); ++i) {
605 if (!strcmp(cmds[i].name, argv[0])) {
606 cmds[i].fn(argv, argc);
611 log_warnx("Unknown command %s", *argv);
615 pp_qid_type(uint8_t type)
618 case QTDIR: return "dir";
619 case QTAPPEND: return "append-only";
620 case QTEXCL: return "exclusive";
621 case QTMOUNT: return "mounted-channel";
622 case QTAUTH: return "authentication";
623 case QTTMP: return "non-backed-up";
624 case QTSYMLINK: return "symlink";
625 case QTFILE: return "file";
632 pp_qid(const uint8_t *d, uint32_t len)
645 memcpy(&vers, d, sizeof(vers));
647 vers = le64toh(vers);
649 memcpy(&path, d, sizeof(path));
651 path = le64toh(path);
653 printf("qid{path=%"PRIu64" version=%"PRIu32" type=0x%x\"%s\"}",
654 path, vers, type, pp_qid_type(type));
658 pp_msg(uint32_t len, uint8_t type, uint16_t tag, const uint8_t *d)
663 printf("len=%"PRIu32" type=%d[%s] tag=0x%x[%d] ", len,
664 type, pp_msg_type(type), tag, tag);
671 printf("invalid: not enough space for msize "
672 "and version provided.");
676 memcpy(&msize, d, sizeof(msize));
678 len -= sizeof(msize);
679 msize = le32toh(msize);
681 memcpy(&slen, d, sizeof(slen));
684 slen = le16toh(slen);
687 printf("invalid: version string length doesn't "
688 "match. Got %d; want %d", slen, len);
692 printf("msize=%"PRIu32" version[%"PRIu16"]=\"",
694 fwrite(d, 1, slen, stdout);
705 printf("invalid Rclunk: %"PRIu32" extra bytes", len);
710 printf("invalid Rflush: %"PRIu32" extra bytes", len);
715 printf("invaild Rwalk: less than two bytes (%d)",
720 memcpy(&slen, d, sizeof(slen));
723 slen = le16toh(slen);
725 if (len != QIDSIZE * slen) {
726 printf("invalid Rwalk: wanted %d bytes for %d qids "
727 "but got %"PRIu32" bytes instead",
728 QIDSIZE*slen, slen, len);
732 printf("nwqid=%"PRIu16, slen);
734 for (; slen != 0; slen--) {
744 memcpy(&slen, d, sizeof(slen));
747 slen = le16toh(slen);
750 printf("invalid: error string length doesn't "
751 "match. Got %d; want %d", slen, len);
756 fwrite(d, 1, slen, stdout);
762 printf("unknown command type");
769 handle_9p(const uint8_t *data, size_t size)
775 assert(size >= HEADERSIZE);
777 memcpy(&len, data, sizeof(len));
780 memcpy(&type, data, sizeof(type));
781 data += sizeof(type);
783 memcpy(&tag, data, sizeof(tag));
787 /* type is one byte long, no endianness issues */
791 pp_msg(len, type, tag, data);
805 printf("%s", PROMPT);
810 main(int argc, char **argv)
812 int ch, sock, handshake;
813 struct event ev_sigint, ev_sigterm;
815 signal(SIGPIPE, SIG_IGN);
817 while ((ch = getopt(argc, argv, "C:cH:hK:P:v")) != -1) {
855 /* if (!tls || (crtpath != NULL || keypath != NULL)) */
858 errx(1, "must enable tls (for now)");
860 log_init(1, LOG_DAEMON);
861 log_setverbose(verbose);
862 log_procinit(getprogname());
864 if ((tlsconf = tls_config_new()) == NULL)
865 fatalx("tls_config_new");
866 tls_config_insecure_noverifycert(tlsconf);
867 tls_config_insecure_noverifyname(tlsconf);
868 if (tls_config_set_keypair_file(tlsconf, crtpath, keypath) == -1)
869 fatalx("can't load certs (%s, %s)", crtpath, keypath);
871 if ((ctx = tls_client()) == NULL)
873 if (tls_configure(ctx, tlsconf) == -1)
874 fatalx("tls_configure: %s", tls_error(ctx));
876 log_info("connecting to %s:%s...", host, port);
878 if ((sock = openconn()) == -1)
879 fatalx("can't connect to %s:%s", host, port);
881 if (tls_connect_socket(ctx, sock, host) == -1)
882 fatalx("tls_connect_socket: %s", tls_error(ctx));
884 for (handshake = 0; !handshake;) {
885 switch (tls_handshake(ctx)) {
887 fatalx("tls_handshake: %s", tls_error(ctx));
894 log_info("connected!");
900 signal_set(&ev_sigint, SIGINT, sig_handler, NULL);
901 signal_set(&ev_sigterm, SIGINT, sig_handler, NULL);
903 signal_add(&ev_sigint, NULL);
904 signal_add(&ev_sigterm, NULL);
906 bev = bufferevent_new(sock, client_read, client_write, client_error,
909 fatal("bufferevent_new");
912 event_set(&bev->ev_read, sock, EV_READ, tls_readcb, bev);
913 event_set(&bev->ev_write, sock, EV_WRITE, tls_writecb, bev);
915 bufferevent_enable(bev, EV_READ|EV_WRITE);
918 inbev = bufferevent_new(0, repl_read, NULL, repl_error, NULL);
919 bufferevent_enable(inbev, EV_READ);
924 bufferevent_free(bev);
926 tls_config_free(tlsconf);