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/ioctl.h>
20 #include <sys/types.h>
21 #include <sys/socket.h>
38 #include <readline/readline.h>
39 #include <readline/history.h>
43 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
57 struct tls_config *tlsconf;
61 struct evbuffer *dirbuf;
65 volatile sig_atomic_t resized;
90 #define ASSERT_EMPTYBUF() assert(EVBUFFER_LENGTH(buf) == 0)
94 readline(const char *prompt)
96 char *ch, *line = NULL;
100 printf("%s", prompt);
103 linelen = getline(&line, &linesize, stdin);
107 if ((ch = strchr(line, '\n')) != NULL)
113 add_history(const char *line)
120 read_line(const char *prompt)
125 if ((line = readline(prompt)) == NULL)
127 /* XXX: trim spaces? */
138 tty_resized(int signo)
146 fprintf(stderr, "usage: %s [-c] host[:port] [path]\n",
148 fprintf(stderr, "kamid suite version " KAMID_VERSION "\n");
159 while (EVBUFFER_LENGTH(evb) != 0) {
160 buf = EVBUFFER_DATA(evb);
161 nbytes = EVBUFFER_LENGTH(evb);
164 r = write(sock, buf, nbytes);
165 if (r == 0 || r == -1)
168 r = tls_write(ctx, buf, nbytes);
169 if (r == TLS_WANT_POLLIN || r == TLS_WANT_POLLOUT)
172 errx(1, "tls: %s", tls_error(ctx));
175 evbuffer_drain(evb, r);
180 mustread(void *d, size_t len)
186 r = read(sock, d, len);
187 if (r == 0 || r == -1)
190 r = tls_read(ctx, d, len);
191 if (r == TLS_WANT_POLLIN || r == TLS_WANT_POLLOUT)
194 errx(1, "tls: %s", tls_error(ctx));
208 mustread(&len, sizeof(len));
210 if (len < HEADERSIZE)
211 errx(1, "read message of invalid length %d", len);
213 len -= 4; /* skip the length just read */
216 l = MIN(len, sizeof(tmp));
219 evbuffer_add(buf, tmp, l);
224 np_read64(struct evbuffer *buf)
228 evbuffer_remove(buf, &n, sizeof(n));
233 np_read32(struct evbuffer *buf)
237 evbuffer_remove(buf, &n, sizeof(n));
242 np_read16(struct evbuffer *buf)
246 evbuffer_remove(buf, &n, sizeof(n));
251 np_read8(struct evbuffer *buf)
255 evbuffer_remove(buf, &n, sizeof(n));
260 np_readstr(struct evbuffer *buf)
265 len = np_read16(buf);
266 assert(EVBUFFER_LENGTH(buf) >= len);
268 if ((str = calloc(1, len+1)) == NULL)
270 evbuffer_remove(buf, str, len);
275 np_read_qid(struct evbuffer *buf, struct qid *qid)
277 assert(EVBUFFER_LENGTH(buf) >= QIDSIZE);
279 qid->type = np_read8(buf);
280 qid->vers = np_read32(buf);
281 qid->path = np_read64(buf);
285 np_read_stat(struct evbuffer *buf, struct np_stat *st)
289 memset(st, 0, sizeof(*st));
291 size = np_read16(buf);
292 if (size > EVBUFFER_LENGTH(buf))
295 st->type = np_read16(buf);
296 st->dev = np_read32(buf);
297 np_read_qid(buf, &st->qid);
298 st->mode = np_read32(buf);
299 st->atime = np_read32(buf);
300 st->mtime = np_read32(buf);
301 st->length = np_read64(buf);
302 st->name = np_readstr(buf);
303 st->uid = np_readstr(buf);
304 st->gid = np_readstr(buf);
305 st->muid = np_readstr(buf);
325 err = np_readstr(buf);
326 errx(1, "expected %s, got error %s",
327 pp_msg_type(type), err);
330 errx(1, "expected %s, got msg type %s",
331 pp_msg_type(type), pp_msg_type(t));
335 expect2(uint8_t type, uint16_t tag)
345 errx(1, "expected tag 0x%x, got 0x%x", tag, t);
353 tversion(VERSION9P, MSIZE9P);
356 expect2(Rversion, NOTAG);
358 msize = np_read32(buf);
359 version = np_readstr(buf);
362 errx(1, "got unexpected msize: %d", msize);
363 if (strcmp(version, VERSION9P))
364 errx(1, "unexpected 9p version: %s", version);
371 do_attach(const char *path)
378 if ((user = getenv("USER")) == NULL)
381 tattach(pwdfid, NOFID, user, path);
384 expect2(Rattach, iota_tag);
385 np_read_qid(buf, &qid);
391 do_open(uint32_t fid, uint8_t mode)
399 expect2(Ropen, iota_tag);
401 np_read_qid(buf, &qid);
402 iounit = np_read32(buf);
410 do_clunk(uint32_t fid)
415 expect2(Rclunk, iota_tag);
421 dup_fid(int fid, int nfid)
425 twalk(fid, nfid, NULL, 0);
428 expect2(Rwalk, iota_tag);
430 nwqid = np_read16(buf);
437 walk_path(int fid, int newfid, const char *path, struct qid *qid)
439 char *wnames[MAXWELEM], *p, *t;
443 if ((p = strdup(path)) == NULL)
447 /* strip initial ./ */
448 if (t[0] == '.' && t[1] == '/')
451 for (nwname = 0; nwname < nitems(wnames) &&
452 (wnames[nwname] = strsep(&t, "/")) != NULL;) {
453 if (*wnames[nwname] != '\0')
457 twalk(fid, newfid, (const char **)wnames, nwname);
460 expect2(Rwalk, iota_tag);
462 nwqid = np_read16(buf);
463 assert(nwqid <= nwname);
465 /* consume all qids */
466 for (i = 0; i < nwname; ++i)
467 np_read_qid(buf, qid);
471 return nwqid == nwname;
475 do_stat(int fid, struct np_stat *st)
480 expect2(Rstat, iota_tag);
482 if (np_read_stat(buf, st) == -1)
483 errx(1, "invalid stat struct read");
489 do_read(int fid, uint64_t off, uint32_t count, void *data)
493 tread(fid, off, count);
496 expect2(Rread, iota_tag);
499 assert(r == EVBUFFER_LENGTH(buf));
501 evbuffer_remove(buf, data, r);
509 draw_progress(const char *pre, const struct progress *p)
515 perc = 100.0 * p->done / p->max;
517 fprintf(stderr, "%s: %d%%\n", pre, (int)perc);
524 if (ioctl(0, TIOCGWINSZ, &ws) == -1)
526 tty_width = ws.ws_col;
531 ((l = printf("\r%s ", pre)) == -1 || l >= w))
534 w -= l + 2 + 5; /* 2 for |, 5 for percentage + \n */
536 printf("%4d%%\n", (int)perc);
542 l = w * MIN(100.0, perc) / 100.0;
543 for (i = 0; i < l; i++)
547 printf("|%4d%%", (int)perc);
553 fetch_fid(int fid, const char *path)
555 struct progress p = {0};
562 do_open(fid, KOREAD);
564 if ((fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1) {
565 warn("can't open %s", path);
574 r = do_read(fid, p.done, sizeof(buf), buf);
579 for (off = 0; off < siz; off += nw)
580 if ((nw = write(fd, buf + off, siz - off)) == 0 ||
585 draw_progress(path, &p);
588 /* throttle, for debugging purpose */
590 struct timespec ts = { 0, 500000000 };
591 nanosleep(&ts, NULL);
604 do_tls_connect(const char *host, const char *port)
608 if ((tlsconf = tls_config_new()) == NULL)
609 fatalx("tls_config_new");
610 tls_config_insecure_noverifycert(tlsconf);
611 tls_config_insecure_noverifyname(tlsconf);
612 if (tls_config_set_keypair_file(tlsconf, crtpath, keypath) == -1)
613 fatalx("can't load certs (%s, %s)", crtpath, keypath);
615 if ((ctx = tls_client()) == NULL)
617 if (tls_configure(ctx, tlsconf) == -1)
618 fatalx("tls_configure: %s", tls_error(ctx));
620 if (tls_connect(ctx, host, port) == -1)
621 fatalx("can't connect to %s:%s: %s", host, port,
624 for (handshake = 0; !handshake;) {
625 switch (tls_handshake(ctx)) {
627 fatalx("tls_handshake: %s", tls_error(ctx));
636 do_ctxt_connect(const char *host, const char *port)
638 struct addrinfo hints, *res, *res0;
639 int error, saved_errno;
640 const char *cause = NULL;
642 memset(&hints, 0, sizeof(hints));
643 hints.ai_family = AF_UNSPEC;
644 hints.ai_socktype = SOCK_STREAM;
645 error = getaddrinfo(host, port, &hints, &res0);
647 errx(1, "%s", gai_strerror(error));
650 for (res = res0; res != NULL; res = res->ai_next) {
651 sock = socket(res->ai_family, res->ai_socktype,
658 if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
676 do_connect(const char *connspec, const char *path)
681 host = xstrdup(connspec);
682 if ((colon = strchr(host, ':')) != NULL) {
688 printf("connecting to %s:%s...", host, port);
692 do_tls_connect(host, port);
694 do_ctxt_connect(host, port);
705 cmd_bell(int argc, const char **argv)
710 puts("bell mode enabled");
712 puts("bell mode disabled");
719 if (!strcmp(*argv, "on")) {
721 puts("bell mode enabled");
725 if (!strcmp(*argv, "off")) {
727 puts("bell mode disabled");
732 printf("bell [on | off]\n");
736 cmd_bye(int argc, const char **argv)
743 cmd_cd(int argc, const char **argv)
749 printf("usage: cd remote-path\n");
754 if (walk_path(pwdfid, nfid, argv[0], &qid) == -1 ||
755 !(qid.type & QTDIR)) {
756 printf("can't cd %s\n", argv[0]);
765 cmd_get(int argc, const char **argv)
771 if (argc != 1 && argc != 2) {
772 printf("usage: get remote-file [local-file]\n");
778 else if ((l = strrchr(argv[0], '/')) != NULL)
784 if (walk_path(pwdfid, nfid, argv[0], &qid) == -1) {
785 printf("can't fetch %s\n", argv[0]);
790 printf("can't fetch %s\n", argv[0]);
799 cmd_lcd(int argc, const char **argv)
804 printf("lcd takes only one argument\n");
811 if (argc == 0 && (dir = getenv("HOME")) == NULL) {
812 printf("HOME is not defined\n");
816 if (chdir(dir) == -1)
817 printf("cd: %s: %s\n", dir, strerror(errno));
821 cmd_lpwd(int argc, const char **argv)
825 if (getcwd(path, sizeof(path)) == NULL) {
826 printf("lpwd: %s\n", strerror(errno));
830 printf("%s\n", path);
834 cmd_ls(int argc, const char **argv)
839 char fmt[FMT_SCALED_STRSIZE];
842 printf("ls don't take arguments (yet)\n");
849 evbuffer_drain(dirbuf, EVBUFFER_LENGTH(dirbuf));
852 tread(1, off, BUFSIZ);
855 expect2(Rread, iota_tag);
857 len = np_read32(buf);
861 evbuffer_add_buffer(dirbuf, buf);
867 while (EVBUFFER_LENGTH(dirbuf) != 0) {
868 if (np_read_stat(dirbuf, &st) == -1)
869 errx(1, "invalid stat struct read");
871 if (fmt_scaled(st.length, fmt) == -1)
872 strlcpy(fmt, "xxx", sizeof(fmt));
874 printf("%4s %8s %s\n", pp_qid_type(st.qid.type), fmt, st.name);
886 cmd_verbose(int argc, const char **argv)
889 log_setverbose(!log_getverbose());
890 if (log_getverbose())
891 puts("verbose mode enabled");
893 puts("verbose mode disabled");
900 if (!strcmp(*argv, "on")) {
902 puts("verbose mode enabled");
906 if (!strcmp(*argv, "off")) {
908 puts("verbose mode disabled");
913 printf("verbose [on | off]\n");
917 excmd(int argc, const char **argv)
921 void (*fn)(int, const char **);
931 {"verbose", cmd_verbose},
937 for (i = 0; i < nitems(cmds); ++i) {
938 if (!strcmp(cmds[i].name, *argv)) {
939 cmds[i].fn(argc-1, argv+1);
944 log_warnx("unknown command %s", *argv);
948 main(int argc, char **argv)
952 log_init(1, LOG_DAEMON);
954 log_procinit(getprogname());
956 while ((ch = getopt(argc, argv, "C:cK:")) != -1) {
980 signal(SIGWINCH, tty_resized);
983 if ((evb = evbuffer_new()) == NULL)
984 fatal("evbuffer_new");
986 if ((buf = evbuffer_new()) == NULL)
987 fatal("evbuffer_new");
989 if ((dirbuf = evbuffer_new()) == NULL)
990 fatal("evbuferr_new");
992 do_connect(argv[0], argv[1]);
996 char *line, *argv[16] = {0}, **ap;
998 if ((line = read_line("kamiftp> ")) == NULL)
1001 for (argc = 0, ap = argv; ap < &argv[15] &&
1002 (*ap = strsep(&line, " \t")) != NULL;) {
1006 excmd(argc, (const char **)argv);