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.
17 #include <sys/ioctl.h>
18 #include <sys/types.h>
19 #include <sys/socket.h>
38 #include <readline/readline.h>
39 #include <readline/history.h>
42 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
56 struct tls_config *tlsconf;
60 struct evbuffer *dirbuf;
64 volatile sig_atomic_t resized;
89 #define ASSERT_EMPTYBUF() assert(EVBUFFER_LENGTH(buf) == 0)
92 read_line(const char *prompt)
97 if ((line = readline(prompt)) == NULL)
99 /* XXX: trim spaces? */
110 tty_resized(int signo)
118 fprintf(stderr, "usage: %s [-c] host[:port] [path]\n",
120 fprintf(stderr, "kamid suite version " KAMID_VERSION "\n");
131 while (EVBUFFER_LENGTH(evb) != 0) {
132 buf = EVBUFFER_DATA(evb);
133 nbytes = EVBUFFER_LENGTH(evb);
136 r = write(sock, buf, nbytes);
137 if (r == 0 || r == -1)
140 r = tls_write(ctx, buf, nbytes);
141 if (r == TLS_WANT_POLLIN || r == TLS_WANT_POLLOUT)
144 errx(1, "tls: %s", tls_error(ctx));
147 evbuffer_drain(evb, r);
152 mustread(void *d, size_t len)
158 r = read(sock, d, len);
159 if (r == 0 || r == -1)
162 r = tls_read(ctx, d, len);
163 if (r == TLS_WANT_POLLIN || r == TLS_WANT_POLLOUT)
166 errx(1, "tls: %s", tls_error(ctx));
180 mustread(&len, sizeof(len));
182 if (len < HEADERSIZE)
183 errx(1, "read message of invalid length %d", len);
185 len -= 4; /* skip the length just read */
188 l = MIN(len, sizeof(tmp));
191 evbuffer_add(buf, tmp, l);
196 np_read64(struct evbuffer *buf)
200 evbuffer_remove(buf, &n, sizeof(n));
205 np_read32(struct evbuffer *buf)
209 evbuffer_remove(buf, &n, sizeof(n));
214 np_read16(struct evbuffer *buf)
218 evbuffer_remove(buf, &n, sizeof(n));
223 np_read8(struct evbuffer *buf)
227 evbuffer_remove(buf, &n, sizeof(n));
232 np_readstr(struct evbuffer *buf)
237 len = np_read16(buf);
238 assert(EVBUFFER_LENGTH(buf) >= len);
240 if ((str = calloc(1, len+1)) == NULL)
242 evbuffer_remove(buf, str, len);
247 np_read_qid(struct evbuffer *buf, struct qid *qid)
249 assert(EVBUFFER_LENGTH(buf) >= QIDSIZE);
251 qid->type = np_read8(buf);
252 qid->vers = np_read32(buf);
253 qid->path = np_read64(buf);
257 np_read_stat(struct evbuffer *buf, struct np_stat *st)
261 memset(st, 0, sizeof(*st));
263 size = np_read16(buf);
264 if (size > EVBUFFER_LENGTH(buf))
267 st->type = np_read16(buf);
268 st->dev = np_read32(buf);
269 np_read_qid(buf, &st->qid);
270 st->mode = np_read32(buf);
271 st->atime = np_read32(buf);
272 st->mtime = np_read32(buf);
273 st->length = np_read64(buf);
274 st->name = np_readstr(buf);
275 st->uid = np_readstr(buf);
276 st->gid = np_readstr(buf);
277 st->muid = np_readstr(buf);
297 err = np_readstr(buf);
298 errx(1, "expected %s, got error %s",
299 pp_msg_type(type), err);
302 errx(1, "expected %s, got msg type %s",
303 pp_msg_type(type), pp_msg_type(t));
307 expect2(uint8_t type, uint16_t tag)
317 errx(1, "expected tag 0x%x, got 0x%x", tag, t);
325 tversion(VERSION9P, MSIZE9P);
328 expect2(Rversion, NOTAG);
330 msize = np_read32(buf);
331 version = np_readstr(buf);
334 errx(1, "got unexpected msize: %d", msize);
335 if (strcmp(version, VERSION9P))
336 errx(1, "unexpected 9p version: %s", version);
343 do_attach(const char *path)
350 if ((user = getenv("USER")) == NULL)
353 tattach(pwdfid, NOFID, user, path);
356 expect2(Rattach, iota_tag);
357 np_read_qid(buf, &qid);
363 do_open(uint32_t fid, uint8_t mode)
371 expect2(Ropen, iota_tag);
373 np_read_qid(buf, &qid);
374 iounit = np_read32(buf);
382 do_clunk(uint32_t fid)
387 expect2(Rclunk, iota_tag);
393 dup_fid(int fid, int nfid)
397 twalk(fid, nfid, NULL, 0);
400 expect2(Rwalk, iota_tag);
402 nwqid = np_read16(buf);
409 walk_path(int fid, int newfid, const char *path, struct qid *qid)
411 char *wnames[MAXWELEM], *p, *t;
415 if ((p = strdup(path)) == NULL)
419 /* strip initial ./ */
420 if (t[0] == '.' && t[1] == '/')
423 for (nwname = 0; nwname < nitems(wnames) &&
424 (wnames[nwname] = strsep(&t, "/")) != NULL;) {
425 if (*wnames[nwname] != '\0')
429 twalk(fid, newfid, (const char **)wnames, nwname);
432 expect2(Rwalk, iota_tag);
434 nwqid = np_read16(buf);
435 assert(nwqid <= nwname);
437 /* consume all qids */
438 for (i = 0; i < nwname; ++i)
439 np_read_qid(buf, qid);
443 return nwqid == nwname;
447 do_stat(int fid, struct np_stat *st)
452 expect2(Rstat, iota_tag);
454 if (np_read_stat(buf, st) == -1)
455 errx(1, "invalid stat struct read");
461 do_read(int fid, uint64_t off, uint32_t count, void *data)
465 tread(fid, off, count);
468 expect2(Rread, iota_tag);
471 assert(r == EVBUFFER_LENGTH(buf));
473 evbuffer_remove(buf, data, r);
481 draw_progress(const char *pre, const struct progress *p)
487 perc = 100.0 * p->done / p->max;
489 fprintf(stderr, "%s: %d%%\n", pre, (int)perc);
496 if (ioctl(0, TIOCGWINSZ, &ws) == -1)
498 tty_width = ws.ws_col;
503 ((l = printf("\r%s ", pre)) == -1 || l >= w))
506 w -= l + 2 + 5; /* 2 for |, 5 for percentage + \n */
508 printf("%4d%%\n", (int)perc);
514 l = w * MIN(100.0, perc) / 100.0;
515 for (i = 0; i < l; i++)
519 printf("|%4d%%", (int)perc);
525 fetch_fid(int fid, const char *path)
527 struct progress p = {0};
534 do_open(fid, KOREAD);
536 if ((fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1) {
537 warn("can't open %s", path);
546 r = do_read(fid, p.done, sizeof(buf), buf);
551 for (off = 0; off < siz; off += nw)
552 if ((nw = write(fd, buf + off, siz - off)) == 0 ||
557 draw_progress(path, &p);
560 /* throttle, for debugging purpose */
562 struct timespec ts = { 0, 500000000 };
563 nanosleep(&ts, NULL);
576 do_tls_connect(const char *host, const char *port)
580 if ((tlsconf = tls_config_new()) == NULL)
581 fatalx("tls_config_new");
582 tls_config_insecure_noverifycert(tlsconf);
583 tls_config_insecure_noverifyname(tlsconf);
584 if (tls_config_set_keypair_file(tlsconf, crtpath, keypath) == -1)
585 fatalx("can't load certs (%s, %s)", crtpath, keypath);
587 if ((ctx = tls_client()) == NULL)
589 if (tls_configure(ctx, tlsconf) == -1)
590 fatalx("tls_configure: %s", tls_error(ctx));
592 if (tls_connect(ctx, host, port) == -1)
593 fatalx("can't connect to %s:%s: %s", host, port,
596 for (handshake = 0; !handshake;) {
597 switch (tls_handshake(ctx)) {
599 fatalx("tls_handshake: %s", tls_error(ctx));
608 do_ctxt_connect(const char *host, const char *port)
610 struct addrinfo hints, *res, *res0;
611 int error, saved_errno;
612 const char *cause = NULL;
614 memset(&hints, 0, sizeof(hints));
615 hints.ai_family = AF_UNSPEC;
616 hints.ai_socktype = SOCK_STREAM;
617 error = getaddrinfo(host, port, &hints, &res0);
619 errx(1, "%s", gai_strerror(error));
622 for (res = res0; res != NULL; res = res->ai_next) {
623 sock = socket(res->ai_family, res->ai_socktype,
630 if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
648 do_connect(const char *connspec, const char *path)
653 host = xstrdup(connspec);
654 if ((colon = strchr(host, ':')) != NULL) {
660 printf("connecting to %s:%s...", host, port);
664 do_tls_connect(host, port);
666 do_ctxt_connect(host, port);
677 cmd_bell(int argc, const char **argv)
682 puts("bell mode enabled");
684 puts("bell mode disabled");
691 if (!strcmp(*argv, "on")) {
693 puts("bell mode enabled");
697 if (!strcmp(*argv, "off")) {
699 puts("bell mode disabled");
704 printf("bell [on | off]\n");
708 cmd_bye(int argc, const char **argv)
715 cmd_cd(int argc, const char **argv)
721 printf("usage: cd remote-path\n");
726 if (walk_path(pwdfid, nfid, argv[0], &qid) == -1 ||
727 !(qid.type & QTDIR)) {
728 printf("can't cd %s\n", argv[0]);
737 cmd_get(int argc, const char **argv)
743 if (argc != 1 && argc != 2) {
744 printf("usage: get remote-file [local-file]\n");
750 else if ((l = strrchr(argv[0], '/')) != NULL)
756 if (walk_path(pwdfid, nfid, argv[0], &qid) == -1) {
757 printf("can't fetch %s\n", argv[0]);
762 printf("can't fetch %s\n", argv[0]);
771 cmd_lcd(int argc, const char **argv)
776 printf("lcd takes only one argument\n");
783 if (argc == 0 && (dir = getenv("HOME")) == NULL) {
784 printf("HOME is not defined\n");
788 if (chdir(dir) == -1)
789 printf("cd: %s: %s\n", dir, strerror(errno));
793 cmd_lpwd(int argc, const char **argv)
797 if (getcwd(path, sizeof(path)) == NULL) {
798 printf("lpwd: %s\n", strerror(errno));
802 printf("%s\n", path);
806 cmd_ls(int argc, const char **argv)
811 char fmt[FMT_SCALED_STRSIZE];
814 printf("ls don't take arguments (yet)\n");
821 evbuffer_drain(dirbuf, EVBUFFER_LENGTH(dirbuf));
824 tread(1, off, BUFSIZ);
827 expect2(Rread, iota_tag);
829 len = np_read32(buf);
833 evbuffer_add_buffer(dirbuf, buf);
839 while (EVBUFFER_LENGTH(dirbuf) != 0) {
840 if (np_read_stat(dirbuf, &st) == -1)
841 errx(1, "invalid stat struct read");
843 if (fmt_scaled(st.length, fmt) == -1)
844 strlcpy(fmt, "xxx", sizeof(fmt));
846 printf("%4s %8s %s\n", pp_qid_type(st.qid.type), fmt, st.name);
858 cmd_verbose(int argc, const char **argv)
861 log_setverbose(!log_getverbose());
862 if (log_getverbose())
863 puts("verbose mode enabled");
865 puts("verbose mode disabled");
872 if (!strcmp(*argv, "on")) {
874 puts("verbose mode enabled");
878 if (!strcmp(*argv, "off")) {
880 puts("verbose mode disabled");
885 printf("verbose [on | off]\n");
889 excmd(int argc, const char **argv)
893 void (*fn)(int, const char **);
903 {"verbose", cmd_verbose},
909 for (i = 0; i < nitems(cmds); ++i) {
910 if (!strcmp(cmds[i].name, *argv)) {
911 cmds[i].fn(argc-1, argv+1);
916 log_warnx("unknown command %s", *argv);
920 main(int argc, char **argv)
924 log_init(1, LOG_DAEMON);
926 log_procinit(getprogname());
928 while ((ch = getopt(argc, argv, "C:cK:")) != -1) {
952 signal(SIGWINCH, tty_resized);
955 if ((evb = evbuffer_new()) == NULL)
956 fatal("evbuffer_new");
958 if ((buf = evbuffer_new()) == NULL)
959 fatal("evbuffer_new");
961 if ((dirbuf = evbuffer_new()) == NULL)
962 fatal("evbuferr_new");
964 do_connect(argv[0], argv[1]);
968 char *line, *argv[16] = {0}, **ap;
970 if ((line = read_line("kamiftp> ")) == NULL)
973 for (argc = 0, ap = argv; ap < &argv[15] &&
974 (*ap = strsep(&line, " \t")) != NULL;) {
978 excmd(argc, (const char **)argv);