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>
39 #include <readline/readline.h>
40 #include <readline/history.h>
44 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
58 struct tls_config *tlsconf;
62 struct evbuffer *dirbuf;
66 volatile sig_atomic_t resized;
91 #define ASSERT_EMPTYBUF() assert(EVBUFFER_LENGTH(buf) == 0)
95 read_line(const char *prompt)
100 if ((line = readline(prompt)) == NULL)
102 /* XXX: trim spaces? */
113 read_line(const char *prompt)
115 char *ch, *line = NULL;
119 printf("%s", prompt);
122 linelen = getline(&line, &linesize, stdin);
126 if ((ch = strchr(line, '\n')) != NULL)
133 tty_resized(int signo)
141 fprintf(stderr, "usage: %s [-c] host[:port] [path]\n",
143 fprintf(stderr, "kamid suite version " KAMID_VERSION "\n");
154 while (EVBUFFER_LENGTH(evb) != 0) {
155 buf = EVBUFFER_DATA(evb);
156 nbytes = EVBUFFER_LENGTH(evb);
159 r = write(sock, buf, nbytes);
160 if (r == 0 || r == -1)
163 r = tls_write(ctx, buf, nbytes);
164 if (r == TLS_WANT_POLLIN || r == TLS_WANT_POLLOUT)
167 errx(1, "tls: %s", tls_error(ctx));
170 evbuffer_drain(evb, r);
175 mustread(void *d, size_t len)
181 r = read(sock, d, len);
182 if (r == 0 || r == -1)
185 r = tls_read(ctx, d, len);
186 if (r == TLS_WANT_POLLIN || r == TLS_WANT_POLLOUT)
189 errx(1, "tls: %s", tls_error(ctx));
203 mustread(&len, sizeof(len));
205 if (len < HEADERSIZE)
206 errx(1, "read message of invalid length %d", len);
208 len -= 4; /* skip the length just read */
211 l = MIN(len, sizeof(tmp));
214 evbuffer_add(buf, tmp, l);
219 np_read64(struct evbuffer *buf)
223 evbuffer_remove(buf, &n, sizeof(n));
228 np_read32(struct evbuffer *buf)
232 evbuffer_remove(buf, &n, sizeof(n));
237 np_read16(struct evbuffer *buf)
241 evbuffer_remove(buf, &n, sizeof(n));
246 np_read8(struct evbuffer *buf)
250 evbuffer_remove(buf, &n, sizeof(n));
255 np_readstr(struct evbuffer *buf)
260 len = np_read16(buf);
261 assert(EVBUFFER_LENGTH(buf) >= len);
263 if ((str = calloc(1, len+1)) == NULL)
265 evbuffer_remove(buf, str, len);
270 np_read_qid(struct evbuffer *buf, struct qid *qid)
272 assert(EVBUFFER_LENGTH(buf) >= QIDSIZE);
274 qid->type = np_read8(buf);
275 qid->vers = np_read32(buf);
276 qid->path = np_read64(buf);
280 np_read_stat(struct evbuffer *buf, struct np_stat *st)
284 memset(st, 0, sizeof(*st));
286 size = np_read16(buf);
287 if (size > EVBUFFER_LENGTH(buf))
290 st->type = np_read16(buf);
291 st->dev = np_read32(buf);
292 np_read_qid(buf, &st->qid);
293 st->mode = np_read32(buf);
294 st->atime = np_read32(buf);
295 st->mtime = np_read32(buf);
296 st->length = np_read64(buf);
297 st->name = np_readstr(buf);
298 st->uid = np_readstr(buf);
299 st->gid = np_readstr(buf);
300 st->muid = np_readstr(buf);
320 err = np_readstr(buf);
321 errx(1, "expected %s, got error %s",
322 pp_msg_type(type), err);
325 errx(1, "expected %s, got msg type %s",
326 pp_msg_type(type), pp_msg_type(t));
330 expect2(uint8_t type, uint16_t tag)
340 errx(1, "expected tag 0x%x, got 0x%x", tag, t);
348 tversion(VERSION9P, MSIZE9P);
351 expect2(Rversion, NOTAG);
353 msize = np_read32(buf);
354 version = np_readstr(buf);
357 errx(1, "got unexpected msize: %d", msize);
358 if (strcmp(version, VERSION9P))
359 errx(1, "unexpected 9p version: %s", version);
366 do_attach(const char *path)
373 if ((user = getenv("USER")) == NULL)
376 tattach(pwdfid, NOFID, user, path);
379 expect2(Rattach, iota_tag);
380 np_read_qid(buf, &qid);
386 do_open(uint32_t fid, uint8_t mode)
394 expect2(Ropen, iota_tag);
396 np_read_qid(buf, &qid);
397 iounit = np_read32(buf);
405 do_clunk(uint32_t fid)
410 expect2(Rclunk, iota_tag);
416 dup_fid(int fid, int nfid)
420 twalk(fid, nfid, NULL, 0);
423 expect2(Rwalk, iota_tag);
425 nwqid = np_read16(buf);
432 walk_path(int fid, int newfid, const char *path, struct qid *qid)
434 char *wnames[MAXWELEM], *p, *t;
438 if ((p = strdup(path)) == NULL)
442 /* strip initial ./ */
443 if (t[0] == '.' && t[1] == '/')
446 for (nwname = 0; nwname < nitems(wnames) &&
447 (wnames[nwname] = strsep(&t, "/")) != NULL;) {
448 if (*wnames[nwname] != '\0')
452 twalk(fid, newfid, (const char **)wnames, nwname);
455 expect2(Rwalk, iota_tag);
457 nwqid = np_read16(buf);
458 assert(nwqid <= nwname);
460 /* consume all qids */
461 for (i = 0; i < nwname; ++i)
462 np_read_qid(buf, qid);
466 return nwqid == nwname;
470 do_stat(int fid, struct np_stat *st)
475 expect2(Rstat, iota_tag);
477 if (np_read_stat(buf, st) == -1)
478 errx(1, "invalid stat struct read");
484 do_read(int fid, uint64_t off, uint32_t count, void *data)
488 tread(fid, off, count);
491 expect2(Rread, iota_tag);
494 assert(r == EVBUFFER_LENGTH(buf));
496 evbuffer_remove(buf, data, r);
504 draw_progress(const char *pre, const struct progress *p)
510 perc = 100.0 * p->done / p->max;
512 fprintf(stderr, "%s: %d%%\n", pre, (int)perc);
519 if (ioctl(0, TIOCGWINSZ, &ws) == -1)
521 tty_width = ws.ws_col;
526 ((l = printf("\r%s ", pre)) == -1 || l >= w))
529 w -= l + 2 + 5; /* 2 for |, 5 for percentage + \n */
531 printf("%4d%%\n", (int)perc);
537 l = w * MIN(100.0, perc) / 100.0;
538 for (i = 0; i < l; i++)
542 printf("|%4d%%", (int)perc);
548 fetch_fid(int fid, const char *path)
550 struct progress p = {0};
557 do_open(fid, KOREAD);
559 if ((fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1) {
560 warn("can't open %s", path);
569 r = do_read(fid, p.done, sizeof(buf), buf);
574 for (off = 0; off < siz; off += nw)
575 if ((nw = write(fd, buf + off, siz - off)) == 0 ||
580 draw_progress(path, &p);
583 /* throttle, for debugging purpose */
585 struct timespec ts = { 0, 500000000 };
586 nanosleep(&ts, NULL);
599 do_tls_connect(const char *host, const char *port)
603 if ((tlsconf = tls_config_new()) == NULL)
604 fatalx("tls_config_new");
605 tls_config_insecure_noverifycert(tlsconf);
606 tls_config_insecure_noverifyname(tlsconf);
607 if (tls_config_set_keypair_file(tlsconf, crtpath, keypath) == -1)
608 fatalx("can't load certs (%s, %s)", crtpath, keypath);
610 if ((ctx = tls_client()) == NULL)
612 if (tls_configure(ctx, tlsconf) == -1)
613 fatalx("tls_configure: %s", tls_error(ctx));
615 if (tls_connect(ctx, host, port) == -1)
616 fatalx("can't connect to %s:%s: %s", host, port,
619 for (handshake = 0; !handshake;) {
620 switch (tls_handshake(ctx)) {
622 fatalx("tls_handshake: %s", tls_error(ctx));
631 do_ctxt_connect(const char *host, const char *port)
633 struct addrinfo hints, *res, *res0;
634 int error, saved_errno;
635 const char *cause = NULL;
637 memset(&hints, 0, sizeof(hints));
638 hints.ai_family = AF_UNSPEC;
639 hints.ai_socktype = SOCK_STREAM;
640 error = getaddrinfo(host, port, &hints, &res0);
642 errx(1, "%s", gai_strerror(error));
645 for (res = res0; res != NULL; res = res->ai_next) {
646 sock = socket(res->ai_family, res->ai_socktype,
653 if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
671 do_connect(const char *connspec, const char *path)
676 host = xstrdup(connspec);
677 if ((colon = strchr(host, ':')) != NULL) {
683 printf("connecting to %s:%s...", host, port);
687 do_tls_connect(host, port);
689 do_ctxt_connect(host, port);
700 cmd_bell(int argc, const char **argv)
705 puts("bell mode enabled");
707 puts("bell mode disabled");
714 if (!strcmp(*argv, "on")) {
716 puts("bell mode enabled");
720 if (!strcmp(*argv, "off")) {
722 puts("bell mode disabled");
727 printf("bell [on | off]\n");
731 cmd_bye(int argc, const char **argv)
738 cmd_cd(int argc, const char **argv)
744 printf("usage: cd remote-path\n");
749 if (walk_path(pwdfid, nfid, argv[0], &qid) == -1 ||
750 !(qid.type & QTDIR)) {
751 printf("can't cd %s\n", argv[0]);
760 cmd_get(int argc, const char **argv)
766 if (argc != 1 && argc != 2) {
767 printf("usage: get remote-file [local-file]\n");
773 else if ((l = strrchr(argv[0], '/')) != NULL)
779 if (walk_path(pwdfid, nfid, argv[0], &qid) == -1) {
780 printf("can't fetch %s\n", argv[0]);
785 printf("can't fetch %s\n", argv[0]);
794 cmd_lcd(int argc, const char **argv)
799 printf("lcd takes only one argument\n");
806 if (argc == 0 && (dir = getenv("HOME")) == NULL) {
807 printf("HOME is not defined\n");
811 if (chdir(dir) == -1)
812 printf("cd: %s: %s\n", dir, strerror(errno));
816 cmd_lpwd(int argc, const char **argv)
820 if (getcwd(path, sizeof(path)) == NULL) {
821 printf("lpwd: %s\n", strerror(errno));
825 printf("%s\n", path);
829 cmd_ls(int argc, const char **argv)
834 char fmt[FMT_SCALED_STRSIZE];
837 printf("ls don't take arguments (yet)\n");
844 evbuffer_drain(dirbuf, EVBUFFER_LENGTH(dirbuf));
847 tread(1, off, BUFSIZ);
850 expect2(Rread, iota_tag);
852 len = np_read32(buf);
856 evbuffer_add_buffer(dirbuf, buf);
862 while (EVBUFFER_LENGTH(dirbuf) != 0) {
863 if (np_read_stat(dirbuf, &st) == -1)
864 errx(1, "invalid stat struct read");
866 if (fmt_scaled(st.length, fmt) == -1)
867 strlcpy(fmt, "xxx", sizeof(fmt));
869 printf("%4s %8s %s\n", pp_qid_type(st.qid.type), fmt, st.name);
881 cmd_verbose(int argc, const char **argv)
884 log_setverbose(!log_getverbose());
885 if (log_getverbose())
886 puts("verbose mode enabled");
888 puts("verbose mode disabled");
895 if (!strcmp(*argv, "on")) {
897 puts("verbose mode enabled");
901 if (!strcmp(*argv, "off")) {
903 puts("verbose mode disabled");
908 printf("verbose [on | off]\n");
912 excmd(int argc, const char **argv)
916 void (*fn)(int, const char **);
926 {"verbose", cmd_verbose},
932 for (i = 0; i < nitems(cmds); ++i) {
933 if (!strcmp(cmds[i].name, *argv)) {
934 cmds[i].fn(argc-1, argv+1);
939 log_warnx("unknown command %s", *argv);
943 main(int argc, char **argv)
947 log_init(1, LOG_DAEMON);
949 log_procinit(getprogname());
951 while ((ch = getopt(argc, argv, "C:cK:")) != -1) {
975 signal(SIGWINCH, tty_resized);
978 if ((evb = evbuffer_new()) == NULL)
979 fatal("evbuffer_new");
981 if ((buf = evbuffer_new()) == NULL)
982 fatal("evbuffer_new");
984 if ((dirbuf = evbuffer_new()) == NULL)
985 fatal("evbuferr_new");
987 do_connect(argv[0], argv[1]);
991 char *line, *argv[16] = {0}, **ap;
993 if ((line = read_line("kamiftp> ")) == NULL)
996 for (argc = 0, ap = argv; ap < &argv[15] &&
997 (*ap = strsep(&line, " \t")) != NULL;) {
1001 excmd(argc, (const char **)argv);