2 fb1a36c0 2022-01-09 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 fb1a36c0 2022-01-09 op * Permission to use, copy, modify, and distribute this software for any
5 fb1a36c0 2022-01-09 op * purpose with or without fee is hereby granted, provided that the above
6 fb1a36c0 2022-01-09 op * copyright notice and this permission notice appear in all copies.
8 fb1a36c0 2022-01-09 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 fb1a36c0 2022-01-09 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 fb1a36c0 2022-01-09 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 fb1a36c0 2022-01-09 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 fb1a36c0 2022-01-09 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 fb1a36c0 2022-01-09 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 fb1a36c0 2022-01-09 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 bbcba3ed 2022-01-10 op #include "compat.h"
19 fb1a36c0 2022-01-09 op #include <sys/ioctl.h>
20 fb1a36c0 2022-01-09 op #include <sys/types.h>
21 fb1a36c0 2022-01-09 op #include <sys/socket.h>
23 fb1a36c0 2022-01-09 op #include <assert.h>
24 fb1a36c0 2022-01-09 op #include <errno.h>
25 fb1a36c0 2022-01-09 op #include <fcntl.h>
26 fb1a36c0 2022-01-09 op #include <inttypes.h>
27 fb1a36c0 2022-01-09 op #include <netdb.h>
28 fb1a36c0 2022-01-09 op #include <limits.h>
29 fb1a36c0 2022-01-09 op #include <signal.h>
30 fb1a36c0 2022-01-09 op #include <stdio.h>
31 fb1a36c0 2022-01-09 op #include <stdlib.h>
32 fb1a36c0 2022-01-09 op #include <string.h>
33 fb1a36c0 2022-01-09 op #include <syslog.h>
34 fb1a36c0 2022-01-09 op #include <tls.h>
35 fb1a36c0 2022-01-09 op #include <unistd.h>
37 91cfa92a 2022-01-10 op #ifdef HAVE_READLINE
38 fb1a36c0 2022-01-09 op #include <readline/readline.h>
39 fb1a36c0 2022-01-09 op #include <readline/history.h>
42 fb1a36c0 2022-01-09 op #ifndef nitems
43 fb1a36c0 2022-01-09 op #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
46 fb1a36c0 2022-01-09 op #include "9pclib.h"
47 fb1a36c0 2022-01-09 op #include "kami.h"
48 fb1a36c0 2022-01-09 op #include "utils.h"
49 fb1a36c0 2022-01-09 op #include "log.h"
53 fb1a36c0 2022-01-09 op const char *crtpath;
54 fb1a36c0 2022-01-09 op const char *keypath;
57 fb1a36c0 2022-01-09 op struct tls_config *tlsconf;
58 fb1a36c0 2022-01-09 op struct tls *ctx;
60 fb1a36c0 2022-01-09 op struct evbuffer *buf;
61 fb1a36c0 2022-01-09 op struct evbuffer *dirbuf;
62 fb1a36c0 2022-01-09 op uint32_t msize;
65 fb1a36c0 2022-01-09 op volatile sig_atomic_t resized;
67 fb1a36c0 2022-01-09 op int tty_width;
69 fb1a36c0 2022-01-09 op struct np_stat {
70 fb1a36c0 2022-01-09 op uint16_t type;
72 fb1a36c0 2022-01-09 op struct qid qid;
73 fb1a36c0 2022-01-09 op uint32_t mode;
74 fb1a36c0 2022-01-09 op uint32_t atime;
75 fb1a36c0 2022-01-09 op uint32_t mtime;
76 fb1a36c0 2022-01-09 op uint64_t length;
83 fb1a36c0 2022-01-09 op struct progress {
85 fb1a36c0 2022-01-09 op uint64_t done;
90 fb1a36c0 2022-01-09 op #define ASSERT_EMPTYBUF() assert(EVBUFFER_LENGTH(buf) == 0)
92 91cfa92a 2022-01-10 op #if !HAVE_READLINE
94 91cfa92a 2022-01-10 op readline(const char *prompt)
96 91cfa92a 2022-01-10 op char *ch, *line = NULL;
97 91cfa92a 2022-01-10 op size_t linesize = 0;
98 91cfa92a 2022-01-10 op ssize_t linelen;
100 91cfa92a 2022-01-10 op printf("%s", prompt);
101 91cfa92a 2022-01-10 op fflush(stdout);
103 91cfa92a 2022-01-10 op linelen = getline(&line, &linesize, stdin);
104 91cfa92a 2022-01-10 op if (linelen == -1)
107 91cfa92a 2022-01-10 op if ((ch = strchr(line, '\n')) != NULL)
113 91cfa92a 2022-01-10 op add_history(const char *line)
119 fb1a36c0 2022-01-09 op static char *
120 fb1a36c0 2022-01-09 op read_line(const char *prompt)
125 fb1a36c0 2022-01-09 op if ((line = readline(prompt)) == NULL)
127 fb1a36c0 2022-01-09 op /* XXX: trim spaces? */
128 fb1a36c0 2022-01-09 op if (*line == '\0') {
133 fb1a36c0 2022-01-09 op add_history(line);
138 fb1a36c0 2022-01-09 op tty_resized(int signo)
143 fb1a36c0 2022-01-09 op static void __dead
144 fb1a36c0 2022-01-09 op usage(int ret)
146 fb1a36c0 2022-01-09 op fprintf(stderr, "usage: %s [-c] host[:port] [path]\n",
147 fb1a36c0 2022-01-09 op getprogname());
148 fb1a36c0 2022-01-09 op fprintf(stderr, "kamid suite version " KAMID_VERSION "\n");
153 fb1a36c0 2022-01-09 op do_send(void)
155 fb1a36c0 2022-01-09 op const void *buf;
156 fb1a36c0 2022-01-09 op size_t nbytes;
159 fb1a36c0 2022-01-09 op while (EVBUFFER_LENGTH(evb) != 0) {
160 fb1a36c0 2022-01-09 op buf = EVBUFFER_DATA(evb);
161 fb1a36c0 2022-01-09 op nbytes = EVBUFFER_LENGTH(evb);
163 fb1a36c0 2022-01-09 op if (ctx == NULL) {
164 fb1a36c0 2022-01-09 op r = write(sock, buf, nbytes);
165 fb1a36c0 2022-01-09 op if (r == 0 || r == -1)
166 fb1a36c0 2022-01-09 op errx(1, "EOF");
168 fb1a36c0 2022-01-09 op r = tls_write(ctx, buf, nbytes);
169 fb1a36c0 2022-01-09 op if (r == TLS_WANT_POLLIN || r == TLS_WANT_POLLOUT)
172 fb1a36c0 2022-01-09 op errx(1, "tls: %s", tls_error(ctx));
175 fb1a36c0 2022-01-09 op evbuffer_drain(evb, r);
180 fb1a36c0 2022-01-09 op mustread(void *d, size_t len)
184 fb1a36c0 2022-01-09 op while (len != 0) {
185 fb1a36c0 2022-01-09 op if (ctx == NULL) {
186 fb1a36c0 2022-01-09 op r = read(sock, d, len);
187 fb1a36c0 2022-01-09 op if (r == 0 || r == -1)
188 fb1a36c0 2022-01-09 op errx(1, "EOF");
190 fb1a36c0 2022-01-09 op r = tls_read(ctx, d, len);
191 fb1a36c0 2022-01-09 op if (r == TLS_WANT_POLLIN || r == TLS_WANT_POLLOUT)
194 fb1a36c0 2022-01-09 op errx(1, "tls: %s", tls_error(ctx));
203 fb1a36c0 2022-01-09 op recv_msg(void)
205 fb1a36c0 2022-01-09 op uint32_t len, l;
206 fb1a36c0 2022-01-09 op char tmp[BUFSIZ];
208 fb1a36c0 2022-01-09 op mustread(&len, sizeof(len));
209 fb1a36c0 2022-01-09 op len = le32toh(len);
210 fb1a36c0 2022-01-09 op if (len < HEADERSIZE)
211 fb1a36c0 2022-01-09 op errx(1, "read message of invalid length %d", len);
213 fb1a36c0 2022-01-09 op len -= 4; /* skip the length just read */
215 fb1a36c0 2022-01-09 op while (len != 0) {
216 fb1a36c0 2022-01-09 op l = MIN(len, sizeof(tmp));
217 fb1a36c0 2022-01-09 op mustread(tmp, l);
219 fb1a36c0 2022-01-09 op evbuffer_add(buf, tmp, l);
223 fb1a36c0 2022-01-09 op static uint64_t
224 fb1a36c0 2022-01-09 op np_read64(struct evbuffer *buf)
228 fb1a36c0 2022-01-09 op evbuffer_remove(buf, &n, sizeof(n));
229 fb1a36c0 2022-01-09 op return le64toh(n);
232 fb1a36c0 2022-01-09 op static uint32_t
233 fb1a36c0 2022-01-09 op np_read32(struct evbuffer *buf)
237 fb1a36c0 2022-01-09 op evbuffer_remove(buf, &n, sizeof(n));
238 fb1a36c0 2022-01-09 op return le32toh(n);
241 fb1a36c0 2022-01-09 op static uint16_t
242 fb1a36c0 2022-01-09 op np_read16(struct evbuffer *buf)
246 fb1a36c0 2022-01-09 op evbuffer_remove(buf, &n, sizeof(n));
247 fb1a36c0 2022-01-09 op return le16toh(n);
250 fb1a36c0 2022-01-09 op static uint16_t
251 fb1a36c0 2022-01-09 op np_read8(struct evbuffer *buf)
255 fb1a36c0 2022-01-09 op evbuffer_remove(buf, &n, sizeof(n));
259 fb1a36c0 2022-01-09 op static char *
260 fb1a36c0 2022-01-09 op np_readstr(struct evbuffer *buf)
262 fb1a36c0 2022-01-09 op uint16_t len;
265 fb1a36c0 2022-01-09 op len = np_read16(buf);
266 fb1a36c0 2022-01-09 op assert(EVBUFFER_LENGTH(buf) >= len);
268 fb1a36c0 2022-01-09 op if ((str = calloc(1, len+1)) == NULL)
269 fb1a36c0 2022-01-09 op err(1, "calloc");
270 fb1a36c0 2022-01-09 op evbuffer_remove(buf, str, len);
275 fb1a36c0 2022-01-09 op np_read_qid(struct evbuffer *buf, struct qid *qid)
277 fb1a36c0 2022-01-09 op assert(EVBUFFER_LENGTH(buf) >= QIDSIZE);
279 fb1a36c0 2022-01-09 op qid->type = np_read8(buf);
280 fb1a36c0 2022-01-09 op qid->vers = np_read32(buf);
281 fb1a36c0 2022-01-09 op qid->path = np_read64(buf);
285 fb1a36c0 2022-01-09 op np_read_stat(struct evbuffer *buf, struct np_stat *st)
287 fb1a36c0 2022-01-09 op uint16_t size;
289 fb1a36c0 2022-01-09 op memset(st, 0, sizeof(*st));
291 fb1a36c0 2022-01-09 op size = np_read16(buf);
292 fb1a36c0 2022-01-09 op if (size > EVBUFFER_LENGTH(buf))
295 fb1a36c0 2022-01-09 op st->type = np_read16(buf);
296 fb1a36c0 2022-01-09 op st->dev = np_read32(buf);
297 fb1a36c0 2022-01-09 op np_read_qid(buf, &st->qid);
298 fb1a36c0 2022-01-09 op st->mode = np_read32(buf);
299 fb1a36c0 2022-01-09 op st->atime = np_read32(buf);
300 fb1a36c0 2022-01-09 op st->mtime = np_read32(buf);
301 fb1a36c0 2022-01-09 op st->length = np_read64(buf);
302 fb1a36c0 2022-01-09 op st->name = np_readstr(buf);
303 fb1a36c0 2022-01-09 op st->uid = np_readstr(buf);
304 fb1a36c0 2022-01-09 op st->gid = np_readstr(buf);
305 fb1a36c0 2022-01-09 op st->muid = np_readstr(buf);
311 fb1a36c0 2022-01-09 op expect(uint8_t type)
315 fb1a36c0 2022-01-09 op t = np_read8(buf);
316 fb1a36c0 2022-01-09 op if (t == type)
319 fb1a36c0 2022-01-09 op if (t == Rerror) {
322 fb1a36c0 2022-01-09 op /* skip tag */
323 fb1a36c0 2022-01-09 op np_read16(buf);
325 fb1a36c0 2022-01-09 op err = np_readstr(buf);
326 fb1a36c0 2022-01-09 op errx(1, "expected %s, got error %s",
327 fb1a36c0 2022-01-09 op pp_msg_type(type), err);
330 fb1a36c0 2022-01-09 op errx(1, "expected %s, got msg type %s",
331 fb1a36c0 2022-01-09 op pp_msg_type(type), pp_msg_type(t));
335 fb1a36c0 2022-01-09 op expect2(uint8_t type, uint16_t tag)
339 fb1a36c0 2022-01-09 op expect(type);
341 fb1a36c0 2022-01-09 op t = np_read16(buf);
342 fb1a36c0 2022-01-09 op if (t == tag)
345 fb1a36c0 2022-01-09 op errx(1, "expected tag 0x%x, got 0x%x", tag, t);
349 fb1a36c0 2022-01-09 op do_version(void)
351 fb1a36c0 2022-01-09 op char *version;
353 fb1a36c0 2022-01-09 op tversion(VERSION9P, MSIZE9P);
356 fb1a36c0 2022-01-09 op expect2(Rversion, NOTAG);
358 fb1a36c0 2022-01-09 op msize = np_read32(buf);
359 fb1a36c0 2022-01-09 op version = np_readstr(buf);
361 fb1a36c0 2022-01-09 op if (msize > MSIZE9P)
362 fb1a36c0 2022-01-09 op errx(1, "got unexpected msize: %d", msize);
363 fb1a36c0 2022-01-09 op if (strcmp(version, VERSION9P))
364 fb1a36c0 2022-01-09 op errx(1, "unexpected 9p version: %s", version);
366 fb1a36c0 2022-01-09 op free(version);
367 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
371 fb1a36c0 2022-01-09 op do_attach(const char *path)
373 fb1a36c0 2022-01-09 op const char *user;
374 fb1a36c0 2022-01-09 op struct qid qid;
376 fb1a36c0 2022-01-09 op if (path == NULL)
378 fb1a36c0 2022-01-09 op if ((user = getenv("USER")) == NULL)
379 fb1a36c0 2022-01-09 op user = "flan";
381 fb1a36c0 2022-01-09 op tattach(pwdfid, NOFID, user, path);
384 fb1a36c0 2022-01-09 op expect2(Rattach, iota_tag);
385 fb1a36c0 2022-01-09 op np_read_qid(buf, &qid);
387 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
390 fb1a36c0 2022-01-09 op static uint32_t
391 fb1a36c0 2022-01-09 op do_open(uint32_t fid, uint8_t mode)
393 fb1a36c0 2022-01-09 op struct qid qid;
394 fb1a36c0 2022-01-09 op uint32_t iounit;
396 fb1a36c0 2022-01-09 op topen(fid, mode);
399 fb1a36c0 2022-01-09 op expect2(Ropen, iota_tag);
401 fb1a36c0 2022-01-09 op np_read_qid(buf, &qid);
402 fb1a36c0 2022-01-09 op iounit = np_read32(buf);
404 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
406 fb1a36c0 2022-01-09 op return iounit;
410 fb1a36c0 2022-01-09 op do_clunk(uint32_t fid)
415 fb1a36c0 2022-01-09 op expect2(Rclunk, iota_tag);
417 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
421 fb1a36c0 2022-01-09 op dup_fid(int fid, int nfid)
423 fb1a36c0 2022-01-09 op uint16_t nwqid;
425 fb1a36c0 2022-01-09 op twalk(fid, nfid, NULL, 0);
428 fb1a36c0 2022-01-09 op expect2(Rwalk, iota_tag);
430 fb1a36c0 2022-01-09 op nwqid = np_read16(buf);
431 fb1a36c0 2022-01-09 op assert(nwqid == 0);
433 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
437 fb1a36c0 2022-01-09 op walk_path(int fid, int newfid, const char *path, struct qid *qid)
439 fb1a36c0 2022-01-09 op char *wnames[MAXWELEM], *p, *t;
440 fb1a36c0 2022-01-09 op size_t nwname, i;
441 fb1a36c0 2022-01-09 op uint16_t nwqid;
443 fb1a36c0 2022-01-09 op if ((p = strdup(path)) == NULL)
444 fb1a36c0 2022-01-09 op err(1, "strdup");
447 fb1a36c0 2022-01-09 op /* strip initial ./ */
448 fb1a36c0 2022-01-09 op if (t[0] == '.' && t[1] == '/')
451 fb1a36c0 2022-01-09 op for (nwname = 0; nwname < nitems(wnames) &&
452 fb1a36c0 2022-01-09 op (wnames[nwname] = strsep(&t, "/")) != NULL;) {
453 fb1a36c0 2022-01-09 op if (*wnames[nwname] != '\0')
457 fb1a36c0 2022-01-09 op twalk(fid, newfid, (const char **)wnames, nwname);
460 fb1a36c0 2022-01-09 op expect2(Rwalk, iota_tag);
462 fb1a36c0 2022-01-09 op nwqid = np_read16(buf);
463 fb1a36c0 2022-01-09 op assert(nwqid <= nwname);
465 fb1a36c0 2022-01-09 op /* consume all qids */
466 fb1a36c0 2022-01-09 op for (i = 0; i < nwname; ++i)
467 fb1a36c0 2022-01-09 op np_read_qid(buf, qid);
471 fb1a36c0 2022-01-09 op return nwqid == nwname;
475 fb1a36c0 2022-01-09 op do_stat(int fid, struct np_stat *st)
480 fb1a36c0 2022-01-09 op expect2(Rstat, iota_tag);
482 fb1a36c0 2022-01-09 op if (np_read_stat(buf, st) == -1)
483 fb1a36c0 2022-01-09 op errx(1, "invalid stat struct read");
485 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
488 fb1a36c0 2022-01-09 op static size_t
489 fb1a36c0 2022-01-09 op do_read(int fid, uint64_t off, uint32_t count, void *data)
493 fb1a36c0 2022-01-09 op tread(fid, off, count);
496 fb1a36c0 2022-01-09 op expect2(Rread, iota_tag);
498 fb1a36c0 2022-01-09 op r = np_read32(buf);
499 fb1a36c0 2022-01-09 op assert(r == EVBUFFER_LENGTH(buf));
500 fb1a36c0 2022-01-09 op assert(r <= count);
501 fb1a36c0 2022-01-09 op evbuffer_remove(buf, data, r);
503 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
509 fb1a36c0 2022-01-09 op draw_progress(const char *pre, const struct progress *p)
511 fb1a36c0 2022-01-09 op struct winsize ws;
515 fb1a36c0 2022-01-09 op perc = 100.0 * p->done / p->max;
516 fb1a36c0 2022-01-09 op if (!tty_p) {
517 fb1a36c0 2022-01-09 op fprintf(stderr, "%s: %d%%\n", pre, (int)perc);
521 fb1a36c0 2022-01-09 op if (resized) {
524 fb1a36c0 2022-01-09 op if (ioctl(0, TIOCGWINSZ, &ws) == -1)
526 fb1a36c0 2022-01-09 op tty_width = ws.ws_col;
528 fb1a36c0 2022-01-09 op w = tty_width;
530 fb1a36c0 2022-01-09 op if (pre == NULL ||
531 fb1a36c0 2022-01-09 op ((l = printf("\r%s ", pre)) == -1 || l >= w))
534 fb1a36c0 2022-01-09 op w -= l + 2 + 5; /* 2 for |, 5 for percentage + \n */
536 fb1a36c0 2022-01-09 op printf("%4d%%\n", (int)perc);
542 fb1a36c0 2022-01-09 op l = w * MIN(100.0, perc) / 100.0;
543 fb1a36c0 2022-01-09 op for (i = 0; i < l; i++)
545 fb1a36c0 2022-01-09 op for (; i < w; i++)
547 fb1a36c0 2022-01-09 op printf("|%4d%%", (int)perc);
549 fb1a36c0 2022-01-09 op fflush(stdout);
553 fb1a36c0 2022-01-09 op fetch_fid(int fid, const char *path)
555 fb1a36c0 2022-01-09 op struct progress p = {0};
556 fb1a36c0 2022-01-09 op struct np_stat st;
559 fb1a36c0 2022-01-09 op char buf[BUFSIZ];
561 fb1a36c0 2022-01-09 op do_stat(fid, &st);
562 fb1a36c0 2022-01-09 op do_open(fid, KOREAD);
564 fb1a36c0 2022-01-09 op if ((fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1) {
565 fb1a36c0 2022-01-09 op warn("can't open %s", path);
569 fb1a36c0 2022-01-09 op p.max = st.length;
571 fb1a36c0 2022-01-09 op size_t siz, off;
574 fb1a36c0 2022-01-09 op r = do_read(fid, p.done, sizeof(buf), buf);
578 fb1a36c0 2022-01-09 op siz = sizeof(buf);
579 fb1a36c0 2022-01-09 op for (off = 0; off < siz; off += nw)
580 fb1a36c0 2022-01-09 op if ((nw = write(fd, buf + off, siz - off)) == 0 ||
582 fb1a36c0 2022-01-09 op err(1, "write");
585 fb1a36c0 2022-01-09 op draw_progress(path, &p);
588 fb1a36c0 2022-01-09 op /* throttle, for debugging purpose */
590 fb1a36c0 2022-01-09 op struct timespec ts = { 0, 500000000 };
591 fb1a36c0 2022-01-09 op nanosleep(&ts, NULL);
596 fb1a36c0 2022-01-09 op putchar('\n');
599 fb1a36c0 2022-01-09 op do_clunk(fid);
604 fb1a36c0 2022-01-09 op do_tls_connect(const char *host, const char *port)
606 fb1a36c0 2022-01-09 op int handshake;
608 fb1a36c0 2022-01-09 op if ((tlsconf = tls_config_new()) == NULL)
609 fb1a36c0 2022-01-09 op fatalx("tls_config_new");
610 fb1a36c0 2022-01-09 op tls_config_insecure_noverifycert(tlsconf);
611 fb1a36c0 2022-01-09 op tls_config_insecure_noverifyname(tlsconf);
612 fb1a36c0 2022-01-09 op if (tls_config_set_keypair_file(tlsconf, crtpath, keypath) == -1)
613 fb1a36c0 2022-01-09 op fatalx("can't load certs (%s, %s)", crtpath, keypath);
615 fb1a36c0 2022-01-09 op if ((ctx = tls_client()) == NULL)
616 fb1a36c0 2022-01-09 op fatal("tls_client");
617 fb1a36c0 2022-01-09 op if (tls_configure(ctx, tlsconf) == -1)
618 fb1a36c0 2022-01-09 op fatalx("tls_configure: %s", tls_error(ctx));
620 fb1a36c0 2022-01-09 op if (tls_connect(ctx, host, port) == -1)
621 fb1a36c0 2022-01-09 op fatalx("can't connect to %s:%s: %s", host, port,
622 fb1a36c0 2022-01-09 op tls_error(ctx));
624 fb1a36c0 2022-01-09 op for (handshake = 0; !handshake;) {
625 fb1a36c0 2022-01-09 op switch (tls_handshake(ctx)) {
627 fb1a36c0 2022-01-09 op fatalx("tls_handshake: %s", tls_error(ctx));
629 fb1a36c0 2022-01-09 op handshake = 1;
636 fb1a36c0 2022-01-09 op do_ctxt_connect(const char *host, const char *port)
638 fb1a36c0 2022-01-09 op struct addrinfo hints, *res, *res0;
639 fb1a36c0 2022-01-09 op int error, saved_errno;
640 fb1a36c0 2022-01-09 op const char *cause = NULL;
642 fb1a36c0 2022-01-09 op memset(&hints, 0, sizeof(hints));
643 fb1a36c0 2022-01-09 op hints.ai_family = AF_UNSPEC;
644 fb1a36c0 2022-01-09 op hints.ai_socktype = SOCK_STREAM;
645 fb1a36c0 2022-01-09 op error = getaddrinfo(host, port, &hints, &res0);
647 fb1a36c0 2022-01-09 op errx(1, "%s", gai_strerror(error));
650 fb1a36c0 2022-01-09 op for (res = res0; res != NULL; res = res->ai_next) {
651 fb1a36c0 2022-01-09 op sock = socket(res->ai_family, res->ai_socktype,
652 fb1a36c0 2022-01-09 op res->ai_protocol);
653 fb1a36c0 2022-01-09 op if (sock == -1) {
654 fb1a36c0 2022-01-09 op cause = "socket";
658 fb1a36c0 2022-01-09 op if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
659 fb1a36c0 2022-01-09 op cause = "connect";
660 fb1a36c0 2022-01-09 op saved_errno = errno;
662 fb1a36c0 2022-01-09 op errno = saved_errno;
670 fb1a36c0 2022-01-09 op if (sock == -1)
671 fb1a36c0 2022-01-09 op err(1, "%s", cause);
672 fb1a36c0 2022-01-09 op freeaddrinfo(res0);
676 fb1a36c0 2022-01-09 op do_connect(const char *connspec, const char *path)
678 fb1a36c0 2022-01-09 op char *host, *colon;
679 fb1a36c0 2022-01-09 op const char *port;
681 fb1a36c0 2022-01-09 op host = xstrdup(connspec);
682 fb1a36c0 2022-01-09 op if ((colon = strchr(host, ':')) != NULL) {
683 fb1a36c0 2022-01-09 op *colon = '\0';
684 fb1a36c0 2022-01-09 op port = ++colon;
686 fb1a36c0 2022-01-09 op port = "1337";
688 fb1a36c0 2022-01-09 op printf("connecting to %s:%s...", host, port);
689 fb1a36c0 2022-01-09 op fflush(stdout);
692 fb1a36c0 2022-01-09 op do_tls_connect(host, port);
694 fb1a36c0 2022-01-09 op do_ctxt_connect(host, port);
696 fb1a36c0 2022-01-09 op printf(" done!\n");
698 fb1a36c0 2022-01-09 op do_version();
699 fb1a36c0 2022-01-09 op do_attach(path);
705 fb1a36c0 2022-01-09 op cmd_bell(int argc, const char **argv)
707 fb1a36c0 2022-01-09 op if (argc == 0) {
708 fb1a36c0 2022-01-09 op bell = !bell;
710 fb1a36c0 2022-01-09 op puts("bell mode enabled");
712 fb1a36c0 2022-01-09 op puts("bell mode disabled");
716 fb1a36c0 2022-01-09 op if (argc != 1)
719 fb1a36c0 2022-01-09 op if (!strcmp(*argv, "on")) {
721 fb1a36c0 2022-01-09 op puts("bell mode enabled");
725 fb1a36c0 2022-01-09 op if (!strcmp(*argv, "off")) {
727 fb1a36c0 2022-01-09 op puts("bell mode disabled");
732 fb1a36c0 2022-01-09 op printf("bell [on | off]\n");
736 fb1a36c0 2022-01-09 op cmd_bye(int argc, const char **argv)
738 fb1a36c0 2022-01-09 op log_warnx("bye\n");
743 fb1a36c0 2022-01-09 op cmd_cd(int argc, const char **argv)
745 fb1a36c0 2022-01-09 op struct qid qid;
748 fb1a36c0 2022-01-09 op if (argc != 1) {
749 fb1a36c0 2022-01-09 op printf("usage: cd remote-path\n");
753 fb1a36c0 2022-01-09 op nfid = pwdfid+1;
754 fb1a36c0 2022-01-09 op if (walk_path(pwdfid, nfid, argv[0], &qid) == -1 ||
755 fb1a36c0 2022-01-09 op !(qid.type & QTDIR)) {
756 fb1a36c0 2022-01-09 op printf("can't cd %s\n", argv[0]);
757 fb1a36c0 2022-01-09 op do_clunk(nfid);
759 fb1a36c0 2022-01-09 op do_clunk(pwdfid);
760 fb1a36c0 2022-01-09 op pwdfid = nfid;
765 fb1a36c0 2022-01-09 op cmd_get(int argc, const char **argv)
767 fb1a36c0 2022-01-09 op struct qid qid;
768 fb1a36c0 2022-01-09 op const char *l;
771 fb1a36c0 2022-01-09 op if (argc != 1 && argc != 2) {
772 fb1a36c0 2022-01-09 op printf("usage: get remote-file [local-file]\n");
776 fb1a36c0 2022-01-09 op if (argc == 2)
778 fb1a36c0 2022-01-09 op else if ((l = strrchr(argv[0], '/')) != NULL)
779 fb1a36c0 2022-01-09 op l++; /* skip / */
783 fb1a36c0 2022-01-09 op nfid = pwdfid+1;
784 fb1a36c0 2022-01-09 op if (walk_path(pwdfid, nfid, argv[0], &qid) == -1) {
785 fb1a36c0 2022-01-09 op printf("can't fetch %s\n", argv[0]);
789 fb1a36c0 2022-01-09 op if (qid.type != 0) {
790 fb1a36c0 2022-01-09 op printf("can't fetch %s\n", argv[0]);
791 fb1a36c0 2022-01-09 op do_clunk(nfid);
795 fb1a36c0 2022-01-09 op fetch_fid(nfid, l);
799 fb1a36c0 2022-01-09 op cmd_lcd(int argc, const char **argv)
801 fb1a36c0 2022-01-09 op const char *dir;
803 fb1a36c0 2022-01-09 op if (argc > 1) {
804 fb1a36c0 2022-01-09 op printf("lcd takes only one argument\n");
808 fb1a36c0 2022-01-09 op if (argc == 1)
811 fb1a36c0 2022-01-09 op if (argc == 0 && (dir = getenv("HOME")) == NULL) {
812 fb1a36c0 2022-01-09 op printf("HOME is not defined\n");
816 fb1a36c0 2022-01-09 op if (chdir(dir) == -1)
817 fb1a36c0 2022-01-09 op printf("cd: %s: %s\n", dir, strerror(errno));
821 fb1a36c0 2022-01-09 op cmd_lpwd(int argc, const char **argv)
823 fb1a36c0 2022-01-09 op char path[PATH_MAX];
825 fb1a36c0 2022-01-09 op if (getcwd(path, sizeof(path)) == NULL) {
826 fb1a36c0 2022-01-09 op printf("lpwd: %s\n", strerror(errno));
830 fb1a36c0 2022-01-09 op printf("%s\n", path);
834 fb1a36c0 2022-01-09 op cmd_ls(int argc, const char **argv)
836 fb1a36c0 2022-01-09 op struct np_stat st;
837 fb1a36c0 2022-01-09 op uint64_t off = 0;
838 fb1a36c0 2022-01-09 op uint32_t len;
839 fb1a36c0 2022-01-09 op char fmt[FMT_SCALED_STRSIZE];
841 fb1a36c0 2022-01-09 op if (argc != 0) {
842 fb1a36c0 2022-01-09 op printf("ls don't take arguments (yet)\n");
846 fb1a36c0 2022-01-09 op dup_fid(pwdfid, 1);
847 fb1a36c0 2022-01-09 op do_open(1, KOREAD);
849 fb1a36c0 2022-01-09 op evbuffer_drain(dirbuf, EVBUFFER_LENGTH(dirbuf));
852 fb1a36c0 2022-01-09 op tread(1, off, BUFSIZ);
855 fb1a36c0 2022-01-09 op expect2(Rread, iota_tag);
857 fb1a36c0 2022-01-09 op len = np_read32(buf);
858 fb1a36c0 2022-01-09 op if (len == 0)
861 fb1a36c0 2022-01-09 op evbuffer_add_buffer(dirbuf, buf);
864 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
867 fb1a36c0 2022-01-09 op while (EVBUFFER_LENGTH(dirbuf) != 0) {
868 fb1a36c0 2022-01-09 op if (np_read_stat(dirbuf, &st) == -1)
869 fb1a36c0 2022-01-09 op errx(1, "invalid stat struct read");
871 fb1a36c0 2022-01-09 op if (fmt_scaled(st.length, fmt) == -1)
872 fb1a36c0 2022-01-09 op strlcpy(fmt, "xxx", sizeof(fmt));
874 fb1a36c0 2022-01-09 op printf("%4s %8s %s\n", pp_qid_type(st.qid.type), fmt, st.name);
876 fb1a36c0 2022-01-09 op free(st.name);
877 fb1a36c0 2022-01-09 op free(st.uid);
878 fb1a36c0 2022-01-09 op free(st.gid);
879 fb1a36c0 2022-01-09 op free(st.muid);
886 fb1a36c0 2022-01-09 op cmd_verbose(int argc, const char **argv)
888 fb1a36c0 2022-01-09 op if (argc == 0) {
889 fb1a36c0 2022-01-09 op log_setverbose(!log_getverbose());
890 fb1a36c0 2022-01-09 op if (log_getverbose())
891 fb1a36c0 2022-01-09 op puts("verbose mode enabled");
893 fb1a36c0 2022-01-09 op puts("verbose mode disabled");
897 fb1a36c0 2022-01-09 op if (argc != 1)
900 fb1a36c0 2022-01-09 op if (!strcmp(*argv, "on")) {
901 fb1a36c0 2022-01-09 op log_setverbose(1);
902 fb1a36c0 2022-01-09 op puts("verbose mode enabled");
906 fb1a36c0 2022-01-09 op if (!strcmp(*argv, "off")) {
907 fb1a36c0 2022-01-09 op log_setverbose(0);
908 fb1a36c0 2022-01-09 op puts("verbose mode disabled");
913 fb1a36c0 2022-01-09 op printf("verbose [on | off]\n");
917 fb1a36c0 2022-01-09 op excmd(int argc, const char **argv)
920 fb1a36c0 2022-01-09 op const char *name;
921 fb1a36c0 2022-01-09 op void (*fn)(int, const char **);
923 fb1a36c0 2022-01-09 op {"bell", cmd_bell},
924 fb1a36c0 2022-01-09 op {"bye", cmd_bye},
925 fb1a36c0 2022-01-09 op {"cd", cmd_cd},
926 fb1a36c0 2022-01-09 op {"get", cmd_get},
927 fb1a36c0 2022-01-09 op {"lcd", cmd_lcd},
928 fb1a36c0 2022-01-09 op {"lpwd", cmd_lpwd},
929 fb1a36c0 2022-01-09 op {"ls", cmd_ls},
930 fb1a36c0 2022-01-09 op {"quit", cmd_bye},
931 fb1a36c0 2022-01-09 op {"verbose", cmd_verbose},
935 fb1a36c0 2022-01-09 op if (argc == 0)
937 fb1a36c0 2022-01-09 op for (i = 0; i < nitems(cmds); ++i) {
938 fb1a36c0 2022-01-09 op if (!strcmp(cmds[i].name, *argv)) {
939 fb1a36c0 2022-01-09 op cmds[i].fn(argc-1, argv+1);
944 fb1a36c0 2022-01-09 op log_warnx("unknown command %s", *argv);
948 fb1a36c0 2022-01-09 op main(int argc, char **argv)
952 fb1a36c0 2022-01-09 op log_init(1, LOG_DAEMON);
953 fb1a36c0 2022-01-09 op log_setverbose(0);
954 fb1a36c0 2022-01-09 op log_procinit(getprogname());
956 fb1a36c0 2022-01-09 op while ((ch = getopt(argc, argv, "C:cK:")) != -1) {
957 fb1a36c0 2022-01-09 op switch (ch) {
959 fb1a36c0 2022-01-09 op crtpath = optarg;
965 fb1a36c0 2022-01-09 op keypath = optarg;
971 fb1a36c0 2022-01-09 op argc -= optind;
972 fb1a36c0 2022-01-09 op argv += optind;
974 fb1a36c0 2022-01-09 op if (argc == 0)
977 fb1a36c0 2022-01-09 op if (isatty(1)) {
980 fb1a36c0 2022-01-09 op signal(SIGWINCH, tty_resized);
983 fb1a36c0 2022-01-09 op if ((evb = evbuffer_new()) == NULL)
984 fb1a36c0 2022-01-09 op fatal("evbuffer_new");
986 fb1a36c0 2022-01-09 op if ((buf = evbuffer_new()) == NULL)
987 fb1a36c0 2022-01-09 op fatal("evbuffer_new");
989 fb1a36c0 2022-01-09 op if ((dirbuf = evbuffer_new()) == NULL)
990 fb1a36c0 2022-01-09 op fatal("evbuferr_new");
992 fb1a36c0 2022-01-09 op do_connect(argv[0], argv[1]);
995 fb1a36c0 2022-01-09 op int argc = 0;
996 fb1a36c0 2022-01-09 op char *line, *argv[16] = {0}, **ap;
998 fb1a36c0 2022-01-09 op if ((line = read_line("kamiftp> ")) == NULL)
1001 fb1a36c0 2022-01-09 op for (argc = 0, ap = argv; ap < &argv[15] &&
1002 fb1a36c0 2022-01-09 op (*ap = strsep(&line, " \t")) != NULL;) {
1003 fb1a36c0 2022-01-09 op if (**ap != '\0')
1004 fb1a36c0 2022-01-09 op ap++, argc++;
1006 fb1a36c0 2022-01-09 op excmd(argc, (const char **)argv);
1009 fb1a36c0 2022-01-09 op printf("\a");
1010 fb1a36c0 2022-01-09 op fflush(stdout);
1016 fb1a36c0 2022-01-09 op printf("\n");