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 fb1a36c0 2022-01-09 op #include <sys/ioctl.h>
18 fb1a36c0 2022-01-09 op #include <sys/types.h>
19 fb1a36c0 2022-01-09 op #include <sys/socket.h>
21 fb1a36c0 2022-01-09 op #include <assert.h>
22 fb1a36c0 2022-01-09 op #include <err.h>
23 fb1a36c0 2022-01-09 op #include <errno.h>
24 fb1a36c0 2022-01-09 op #include <event.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>
36 fb1a36c0 2022-01-09 op #include <util.h>
38 fb1a36c0 2022-01-09 op #if HAVE_LIBREADLINE
39 fb1a36c0 2022-01-09 op #include <readline/readline.h>
40 fb1a36c0 2022-01-09 op #include <readline/history.h>
43 fb1a36c0 2022-01-09 op #ifndef nitems
44 fb1a36c0 2022-01-09 op #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
47 fb1a36c0 2022-01-09 op #include "9pclib.h"
48 fb1a36c0 2022-01-09 op #include "kami.h"
49 fb1a36c0 2022-01-09 op #include "utils.h"
50 fb1a36c0 2022-01-09 op #include "log.h"
54 fb1a36c0 2022-01-09 op const char *crtpath;
55 fb1a36c0 2022-01-09 op const char *keypath;
58 fb1a36c0 2022-01-09 op struct tls_config *tlsconf;
59 fb1a36c0 2022-01-09 op struct tls *ctx;
61 fb1a36c0 2022-01-09 op struct evbuffer *buf;
62 fb1a36c0 2022-01-09 op struct evbuffer *dirbuf;
63 fb1a36c0 2022-01-09 op uint32_t msize;
66 fb1a36c0 2022-01-09 op volatile sig_atomic_t resized;
68 fb1a36c0 2022-01-09 op int tty_width;
70 fb1a36c0 2022-01-09 op struct np_stat {
71 fb1a36c0 2022-01-09 op uint16_t type;
73 fb1a36c0 2022-01-09 op struct qid qid;
74 fb1a36c0 2022-01-09 op uint32_t mode;
75 fb1a36c0 2022-01-09 op uint32_t atime;
76 fb1a36c0 2022-01-09 op uint32_t mtime;
77 fb1a36c0 2022-01-09 op uint64_t length;
84 fb1a36c0 2022-01-09 op struct progress {
86 fb1a36c0 2022-01-09 op uint64_t done;
91 fb1a36c0 2022-01-09 op #define ASSERT_EMPTYBUF() assert(EVBUFFER_LENGTH(buf) == 0)
93 fb1a36c0 2022-01-09 op #if HAVE_LIBREADLINE
95 fb1a36c0 2022-01-09 op read_line(const char *prompt)
100 fb1a36c0 2022-01-09 op if ((line = readline(prompt)) == NULL)
102 fb1a36c0 2022-01-09 op /* XXX: trim spaces? */
103 fb1a36c0 2022-01-09 op if (*line == '\0') {
108 fb1a36c0 2022-01-09 op add_history(line);
112 fb1a36c0 2022-01-09 op static char *
113 fb1a36c0 2022-01-09 op read_line(const char *prompt)
115 fb1a36c0 2022-01-09 op char *ch, *line = NULL;
116 fb1a36c0 2022-01-09 op size_t linesize = 0;
117 fb1a36c0 2022-01-09 op ssize_t linelen;
119 fb1a36c0 2022-01-09 op printf("%s", prompt);
120 fb1a36c0 2022-01-09 op fflush(stdout);
122 fb1a36c0 2022-01-09 op linelen = getline(&line, &linesize, stdin);
123 fb1a36c0 2022-01-09 op if (linelen == -1)
126 fb1a36c0 2022-01-09 op if ((ch = strchr(line, '\n')) != NULL)
133 fb1a36c0 2022-01-09 op tty_resized(int signo)
138 fb1a36c0 2022-01-09 op static void __dead
139 fb1a36c0 2022-01-09 op usage(int ret)
141 fb1a36c0 2022-01-09 op fprintf(stderr, "usage: %s [-c] host[:port] [path]\n",
142 fb1a36c0 2022-01-09 op getprogname());
143 fb1a36c0 2022-01-09 op fprintf(stderr, "kamid suite version " KAMID_VERSION "\n");
148 fb1a36c0 2022-01-09 op do_send(void)
150 fb1a36c0 2022-01-09 op const void *buf;
151 fb1a36c0 2022-01-09 op size_t nbytes;
154 fb1a36c0 2022-01-09 op while (EVBUFFER_LENGTH(evb) != 0) {
155 fb1a36c0 2022-01-09 op buf = EVBUFFER_DATA(evb);
156 fb1a36c0 2022-01-09 op nbytes = EVBUFFER_LENGTH(evb);
158 fb1a36c0 2022-01-09 op if (ctx == NULL) {
159 fb1a36c0 2022-01-09 op r = write(sock, buf, nbytes);
160 fb1a36c0 2022-01-09 op if (r == 0 || r == -1)
161 fb1a36c0 2022-01-09 op errx(1, "EOF");
163 fb1a36c0 2022-01-09 op r = tls_write(ctx, buf, nbytes);
164 fb1a36c0 2022-01-09 op if (r == TLS_WANT_POLLIN || r == TLS_WANT_POLLOUT)
167 fb1a36c0 2022-01-09 op errx(1, "tls: %s", tls_error(ctx));
170 fb1a36c0 2022-01-09 op evbuffer_drain(evb, r);
175 fb1a36c0 2022-01-09 op mustread(void *d, size_t len)
179 fb1a36c0 2022-01-09 op while (len != 0) {
180 fb1a36c0 2022-01-09 op if (ctx == NULL) {
181 fb1a36c0 2022-01-09 op r = read(sock, d, len);
182 fb1a36c0 2022-01-09 op if (r == 0 || r == -1)
183 fb1a36c0 2022-01-09 op errx(1, "EOF");
185 fb1a36c0 2022-01-09 op r = tls_read(ctx, d, len);
186 fb1a36c0 2022-01-09 op if (r == TLS_WANT_POLLIN || r == TLS_WANT_POLLOUT)
189 fb1a36c0 2022-01-09 op errx(1, "tls: %s", tls_error(ctx));
198 fb1a36c0 2022-01-09 op recv_msg(void)
200 fb1a36c0 2022-01-09 op uint32_t len, l;
201 fb1a36c0 2022-01-09 op char tmp[BUFSIZ];
203 fb1a36c0 2022-01-09 op mustread(&len, sizeof(len));
204 fb1a36c0 2022-01-09 op len = le32toh(len);
205 fb1a36c0 2022-01-09 op if (len < HEADERSIZE)
206 fb1a36c0 2022-01-09 op errx(1, "read message of invalid length %d", len);
208 fb1a36c0 2022-01-09 op len -= 4; /* skip the length just read */
210 fb1a36c0 2022-01-09 op while (len != 0) {
211 fb1a36c0 2022-01-09 op l = MIN(len, sizeof(tmp));
212 fb1a36c0 2022-01-09 op mustread(tmp, l);
214 fb1a36c0 2022-01-09 op evbuffer_add(buf, tmp, l);
218 fb1a36c0 2022-01-09 op static uint64_t
219 fb1a36c0 2022-01-09 op np_read64(struct evbuffer *buf)
223 fb1a36c0 2022-01-09 op evbuffer_remove(buf, &n, sizeof(n));
224 fb1a36c0 2022-01-09 op return le64toh(n);
227 fb1a36c0 2022-01-09 op static uint32_t
228 fb1a36c0 2022-01-09 op np_read32(struct evbuffer *buf)
232 fb1a36c0 2022-01-09 op evbuffer_remove(buf, &n, sizeof(n));
233 fb1a36c0 2022-01-09 op return le32toh(n);
236 fb1a36c0 2022-01-09 op static uint16_t
237 fb1a36c0 2022-01-09 op np_read16(struct evbuffer *buf)
241 fb1a36c0 2022-01-09 op evbuffer_remove(buf, &n, sizeof(n));
242 fb1a36c0 2022-01-09 op return le16toh(n);
245 fb1a36c0 2022-01-09 op static uint16_t
246 fb1a36c0 2022-01-09 op np_read8(struct evbuffer *buf)
250 fb1a36c0 2022-01-09 op evbuffer_remove(buf, &n, sizeof(n));
254 fb1a36c0 2022-01-09 op static char *
255 fb1a36c0 2022-01-09 op np_readstr(struct evbuffer *buf)
257 fb1a36c0 2022-01-09 op uint16_t len;
260 fb1a36c0 2022-01-09 op len = np_read16(buf);
261 fb1a36c0 2022-01-09 op assert(EVBUFFER_LENGTH(buf) >= len);
263 fb1a36c0 2022-01-09 op if ((str = calloc(1, len+1)) == NULL)
264 fb1a36c0 2022-01-09 op err(1, "calloc");
265 fb1a36c0 2022-01-09 op evbuffer_remove(buf, str, len);
270 fb1a36c0 2022-01-09 op np_read_qid(struct evbuffer *buf, struct qid *qid)
272 fb1a36c0 2022-01-09 op assert(EVBUFFER_LENGTH(buf) >= QIDSIZE);
274 fb1a36c0 2022-01-09 op qid->type = np_read8(buf);
275 fb1a36c0 2022-01-09 op qid->vers = np_read32(buf);
276 fb1a36c0 2022-01-09 op qid->path = np_read64(buf);
280 fb1a36c0 2022-01-09 op np_read_stat(struct evbuffer *buf, struct np_stat *st)
282 fb1a36c0 2022-01-09 op uint16_t size;
284 fb1a36c0 2022-01-09 op memset(st, 0, sizeof(*st));
286 fb1a36c0 2022-01-09 op size = np_read16(buf);
287 fb1a36c0 2022-01-09 op if (size > EVBUFFER_LENGTH(buf))
290 fb1a36c0 2022-01-09 op st->type = np_read16(buf);
291 fb1a36c0 2022-01-09 op st->dev = np_read32(buf);
292 fb1a36c0 2022-01-09 op np_read_qid(buf, &st->qid);
293 fb1a36c0 2022-01-09 op st->mode = np_read32(buf);
294 fb1a36c0 2022-01-09 op st->atime = np_read32(buf);
295 fb1a36c0 2022-01-09 op st->mtime = np_read32(buf);
296 fb1a36c0 2022-01-09 op st->length = np_read64(buf);
297 fb1a36c0 2022-01-09 op st->name = np_readstr(buf);
298 fb1a36c0 2022-01-09 op st->uid = np_readstr(buf);
299 fb1a36c0 2022-01-09 op st->gid = np_readstr(buf);
300 fb1a36c0 2022-01-09 op st->muid = np_readstr(buf);
306 fb1a36c0 2022-01-09 op expect(uint8_t type)
310 fb1a36c0 2022-01-09 op t = np_read8(buf);
311 fb1a36c0 2022-01-09 op if (t == type)
314 fb1a36c0 2022-01-09 op if (t == Rerror) {
317 fb1a36c0 2022-01-09 op /* skip tag */
318 fb1a36c0 2022-01-09 op np_read16(buf);
320 fb1a36c0 2022-01-09 op err = np_readstr(buf);
321 fb1a36c0 2022-01-09 op errx(1, "expected %s, got error %s",
322 fb1a36c0 2022-01-09 op pp_msg_type(type), err);
325 fb1a36c0 2022-01-09 op errx(1, "expected %s, got msg type %s",
326 fb1a36c0 2022-01-09 op pp_msg_type(type), pp_msg_type(t));
330 fb1a36c0 2022-01-09 op expect2(uint8_t type, uint16_t tag)
334 fb1a36c0 2022-01-09 op expect(type);
336 fb1a36c0 2022-01-09 op t = np_read16(buf);
337 fb1a36c0 2022-01-09 op if (t == tag)
340 fb1a36c0 2022-01-09 op errx(1, "expected tag 0x%x, got 0x%x", tag, t);
344 fb1a36c0 2022-01-09 op do_version(void)
346 fb1a36c0 2022-01-09 op char *version;
348 fb1a36c0 2022-01-09 op tversion(VERSION9P, MSIZE9P);
351 fb1a36c0 2022-01-09 op expect2(Rversion, NOTAG);
353 fb1a36c0 2022-01-09 op msize = np_read32(buf);
354 fb1a36c0 2022-01-09 op version = np_readstr(buf);
356 fb1a36c0 2022-01-09 op if (msize > MSIZE9P)
357 fb1a36c0 2022-01-09 op errx(1, "got unexpected msize: %d", msize);
358 fb1a36c0 2022-01-09 op if (strcmp(version, VERSION9P))
359 fb1a36c0 2022-01-09 op errx(1, "unexpected 9p version: %s", version);
361 fb1a36c0 2022-01-09 op free(version);
362 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
366 fb1a36c0 2022-01-09 op do_attach(const char *path)
368 fb1a36c0 2022-01-09 op const char *user;
369 fb1a36c0 2022-01-09 op struct qid qid;
371 fb1a36c0 2022-01-09 op if (path == NULL)
373 fb1a36c0 2022-01-09 op if ((user = getenv("USER")) == NULL)
374 fb1a36c0 2022-01-09 op user = "flan";
376 fb1a36c0 2022-01-09 op tattach(pwdfid, NOFID, user, path);
379 fb1a36c0 2022-01-09 op expect2(Rattach, iota_tag);
380 fb1a36c0 2022-01-09 op np_read_qid(buf, &qid);
382 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
385 fb1a36c0 2022-01-09 op static uint32_t
386 fb1a36c0 2022-01-09 op do_open(uint32_t fid, uint8_t mode)
388 fb1a36c0 2022-01-09 op struct qid qid;
389 fb1a36c0 2022-01-09 op uint32_t iounit;
391 fb1a36c0 2022-01-09 op topen(fid, mode);
394 fb1a36c0 2022-01-09 op expect2(Ropen, iota_tag);
396 fb1a36c0 2022-01-09 op np_read_qid(buf, &qid);
397 fb1a36c0 2022-01-09 op iounit = np_read32(buf);
399 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
401 fb1a36c0 2022-01-09 op return iounit;
405 fb1a36c0 2022-01-09 op do_clunk(uint32_t fid)
410 fb1a36c0 2022-01-09 op expect2(Rclunk, iota_tag);
412 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
416 fb1a36c0 2022-01-09 op dup_fid(int fid, int nfid)
418 fb1a36c0 2022-01-09 op uint16_t nwqid;
420 fb1a36c0 2022-01-09 op twalk(fid, nfid, NULL, 0);
423 fb1a36c0 2022-01-09 op expect2(Rwalk, iota_tag);
425 fb1a36c0 2022-01-09 op nwqid = np_read16(buf);
426 fb1a36c0 2022-01-09 op assert(nwqid == 0);
428 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
432 fb1a36c0 2022-01-09 op walk_path(int fid, int newfid, const char *path, struct qid *qid)
434 fb1a36c0 2022-01-09 op char *wnames[MAXWELEM], *p, *t;
435 fb1a36c0 2022-01-09 op size_t nwname, i;
436 fb1a36c0 2022-01-09 op uint16_t nwqid;
438 fb1a36c0 2022-01-09 op if ((p = strdup(path)) == NULL)
439 fb1a36c0 2022-01-09 op err(1, "strdup");
442 fb1a36c0 2022-01-09 op /* strip initial ./ */
443 fb1a36c0 2022-01-09 op if (t[0] == '.' && t[1] == '/')
446 fb1a36c0 2022-01-09 op for (nwname = 0; nwname < nitems(wnames) &&
447 fb1a36c0 2022-01-09 op (wnames[nwname] = strsep(&t, "/")) != NULL;) {
448 fb1a36c0 2022-01-09 op if (*wnames[nwname] != '\0')
452 fb1a36c0 2022-01-09 op twalk(fid, newfid, (const char **)wnames, nwname);
455 fb1a36c0 2022-01-09 op expect2(Rwalk, iota_tag);
457 fb1a36c0 2022-01-09 op nwqid = np_read16(buf);
458 fb1a36c0 2022-01-09 op assert(nwqid <= nwname);
460 fb1a36c0 2022-01-09 op /* consume all qids */
461 fb1a36c0 2022-01-09 op for (i = 0; i < nwname; ++i)
462 fb1a36c0 2022-01-09 op np_read_qid(buf, qid);
466 fb1a36c0 2022-01-09 op return nwqid == nwname;
470 fb1a36c0 2022-01-09 op do_stat(int fid, struct np_stat *st)
475 fb1a36c0 2022-01-09 op expect2(Rstat, iota_tag);
477 fb1a36c0 2022-01-09 op if (np_read_stat(buf, st) == -1)
478 fb1a36c0 2022-01-09 op errx(1, "invalid stat struct read");
480 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
483 fb1a36c0 2022-01-09 op static size_t
484 fb1a36c0 2022-01-09 op do_read(int fid, uint64_t off, uint32_t count, void *data)
488 fb1a36c0 2022-01-09 op tread(fid, off, count);
491 fb1a36c0 2022-01-09 op expect2(Rread, iota_tag);
493 fb1a36c0 2022-01-09 op r = np_read32(buf);
494 fb1a36c0 2022-01-09 op assert(r == EVBUFFER_LENGTH(buf));
495 fb1a36c0 2022-01-09 op assert(r <= count);
496 fb1a36c0 2022-01-09 op evbuffer_remove(buf, data, r);
498 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
504 fb1a36c0 2022-01-09 op draw_progress(const char *pre, const struct progress *p)
506 fb1a36c0 2022-01-09 op struct winsize ws;
510 fb1a36c0 2022-01-09 op perc = 100.0 * p->done / p->max;
511 fb1a36c0 2022-01-09 op if (!tty_p) {
512 fb1a36c0 2022-01-09 op fprintf(stderr, "%s: %d%%\n", pre, (int)perc);
516 fb1a36c0 2022-01-09 op if (resized) {
519 fb1a36c0 2022-01-09 op if (ioctl(0, TIOCGWINSZ, &ws) == -1)
521 fb1a36c0 2022-01-09 op tty_width = ws.ws_col;
523 fb1a36c0 2022-01-09 op w = tty_width;
525 fb1a36c0 2022-01-09 op if (pre == NULL ||
526 fb1a36c0 2022-01-09 op ((l = printf("\r%s ", pre)) == -1 || l >= w))
529 fb1a36c0 2022-01-09 op w -= l + 2 + 5; /* 2 for |, 5 for percentage + \n */
531 fb1a36c0 2022-01-09 op printf("%4d%%\n", (int)perc);
537 fb1a36c0 2022-01-09 op l = w * MIN(100.0, perc) / 100.0;
538 fb1a36c0 2022-01-09 op for (i = 0; i < l; i++)
540 fb1a36c0 2022-01-09 op for (; i < w; i++)
542 fb1a36c0 2022-01-09 op printf("|%4d%%", (int)perc);
544 fb1a36c0 2022-01-09 op fflush(stdout);
548 fb1a36c0 2022-01-09 op fetch_fid(int fid, const char *path)
550 fb1a36c0 2022-01-09 op struct progress p = {0};
551 fb1a36c0 2022-01-09 op struct np_stat st;
554 fb1a36c0 2022-01-09 op char buf[BUFSIZ];
556 fb1a36c0 2022-01-09 op do_stat(fid, &st);
557 fb1a36c0 2022-01-09 op do_open(fid, KOREAD);
559 fb1a36c0 2022-01-09 op if ((fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1) {
560 fb1a36c0 2022-01-09 op warn("can't open %s", path);
564 fb1a36c0 2022-01-09 op p.max = st.length;
566 fb1a36c0 2022-01-09 op size_t siz, off;
569 fb1a36c0 2022-01-09 op r = do_read(fid, p.done, sizeof(buf), buf);
573 fb1a36c0 2022-01-09 op siz = sizeof(buf);
574 fb1a36c0 2022-01-09 op for (off = 0; off < siz; off += nw)
575 fb1a36c0 2022-01-09 op if ((nw = write(fd, buf + off, siz - off)) == 0 ||
577 fb1a36c0 2022-01-09 op err(1, "write");
580 fb1a36c0 2022-01-09 op draw_progress(path, &p);
583 fb1a36c0 2022-01-09 op /* throttle, for debugging purpose */
585 fb1a36c0 2022-01-09 op struct timespec ts = { 0, 500000000 };
586 fb1a36c0 2022-01-09 op nanosleep(&ts, NULL);
591 fb1a36c0 2022-01-09 op putchar('\n');
594 fb1a36c0 2022-01-09 op do_clunk(fid);
599 fb1a36c0 2022-01-09 op do_tls_connect(const char *host, const char *port)
601 fb1a36c0 2022-01-09 op int handshake;
603 fb1a36c0 2022-01-09 op if ((tlsconf = tls_config_new()) == NULL)
604 fb1a36c0 2022-01-09 op fatalx("tls_config_new");
605 fb1a36c0 2022-01-09 op tls_config_insecure_noverifycert(tlsconf);
606 fb1a36c0 2022-01-09 op tls_config_insecure_noverifyname(tlsconf);
607 fb1a36c0 2022-01-09 op if (tls_config_set_keypair_file(tlsconf, crtpath, keypath) == -1)
608 fb1a36c0 2022-01-09 op fatalx("can't load certs (%s, %s)", crtpath, keypath);
610 fb1a36c0 2022-01-09 op if ((ctx = tls_client()) == NULL)
611 fb1a36c0 2022-01-09 op fatal("tls_client");
612 fb1a36c0 2022-01-09 op if (tls_configure(ctx, tlsconf) == -1)
613 fb1a36c0 2022-01-09 op fatalx("tls_configure: %s", tls_error(ctx));
615 fb1a36c0 2022-01-09 op if (tls_connect(ctx, host, port) == -1)
616 fb1a36c0 2022-01-09 op fatalx("can't connect to %s:%s: %s", host, port,
617 fb1a36c0 2022-01-09 op tls_error(ctx));
619 fb1a36c0 2022-01-09 op for (handshake = 0; !handshake;) {
620 fb1a36c0 2022-01-09 op switch (tls_handshake(ctx)) {
622 fb1a36c0 2022-01-09 op fatalx("tls_handshake: %s", tls_error(ctx));
624 fb1a36c0 2022-01-09 op handshake = 1;
631 fb1a36c0 2022-01-09 op do_ctxt_connect(const char *host, const char *port)
633 fb1a36c0 2022-01-09 op struct addrinfo hints, *res, *res0;
634 fb1a36c0 2022-01-09 op int error, saved_errno;
635 fb1a36c0 2022-01-09 op const char *cause = NULL;
637 fb1a36c0 2022-01-09 op memset(&hints, 0, sizeof(hints));
638 fb1a36c0 2022-01-09 op hints.ai_family = AF_UNSPEC;
639 fb1a36c0 2022-01-09 op hints.ai_socktype = SOCK_STREAM;
640 fb1a36c0 2022-01-09 op error = getaddrinfo(host, port, &hints, &res0);
642 fb1a36c0 2022-01-09 op errx(1, "%s", gai_strerror(error));
645 fb1a36c0 2022-01-09 op for (res = res0; res != NULL; res = res->ai_next) {
646 fb1a36c0 2022-01-09 op sock = socket(res->ai_family, res->ai_socktype,
647 fb1a36c0 2022-01-09 op res->ai_protocol);
648 fb1a36c0 2022-01-09 op if (sock == -1) {
649 fb1a36c0 2022-01-09 op cause = "socket";
653 fb1a36c0 2022-01-09 op if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
654 fb1a36c0 2022-01-09 op cause = "connect";
655 fb1a36c0 2022-01-09 op saved_errno = errno;
657 fb1a36c0 2022-01-09 op errno = saved_errno;
665 fb1a36c0 2022-01-09 op if (sock == -1)
666 fb1a36c0 2022-01-09 op err(1, "%s", cause);
667 fb1a36c0 2022-01-09 op freeaddrinfo(res0);
671 fb1a36c0 2022-01-09 op do_connect(const char *connspec, const char *path)
673 fb1a36c0 2022-01-09 op char *host, *colon;
674 fb1a36c0 2022-01-09 op const char *port;
676 fb1a36c0 2022-01-09 op host = xstrdup(connspec);
677 fb1a36c0 2022-01-09 op if ((colon = strchr(host, ':')) != NULL) {
678 fb1a36c0 2022-01-09 op *colon = '\0';
679 fb1a36c0 2022-01-09 op port = ++colon;
681 fb1a36c0 2022-01-09 op port = "1337";
683 fb1a36c0 2022-01-09 op printf("connecting to %s:%s...", host, port);
684 fb1a36c0 2022-01-09 op fflush(stdout);
687 fb1a36c0 2022-01-09 op do_tls_connect(host, port);
689 fb1a36c0 2022-01-09 op do_ctxt_connect(host, port);
691 fb1a36c0 2022-01-09 op printf(" done!\n");
693 fb1a36c0 2022-01-09 op do_version();
694 fb1a36c0 2022-01-09 op do_attach(path);
700 fb1a36c0 2022-01-09 op cmd_bell(int argc, const char **argv)
702 fb1a36c0 2022-01-09 op if (argc == 0) {
703 fb1a36c0 2022-01-09 op bell = !bell;
705 fb1a36c0 2022-01-09 op puts("bell mode enabled");
707 fb1a36c0 2022-01-09 op puts("bell mode disabled");
711 fb1a36c0 2022-01-09 op if (argc != 1)
714 fb1a36c0 2022-01-09 op if (!strcmp(*argv, "on")) {
716 fb1a36c0 2022-01-09 op puts("bell mode enabled");
720 fb1a36c0 2022-01-09 op if (!strcmp(*argv, "off")) {
722 fb1a36c0 2022-01-09 op puts("bell mode disabled");
727 fb1a36c0 2022-01-09 op printf("bell [on | off]\n");
731 fb1a36c0 2022-01-09 op cmd_bye(int argc, const char **argv)
733 fb1a36c0 2022-01-09 op log_warnx("bye\n");
738 fb1a36c0 2022-01-09 op cmd_cd(int argc, const char **argv)
740 fb1a36c0 2022-01-09 op struct qid qid;
743 fb1a36c0 2022-01-09 op if (argc != 1) {
744 fb1a36c0 2022-01-09 op printf("usage: cd remote-path\n");
748 fb1a36c0 2022-01-09 op nfid = pwdfid+1;
749 fb1a36c0 2022-01-09 op if (walk_path(pwdfid, nfid, argv[0], &qid) == -1 ||
750 fb1a36c0 2022-01-09 op !(qid.type & QTDIR)) {
751 fb1a36c0 2022-01-09 op printf("can't cd %s\n", argv[0]);
752 fb1a36c0 2022-01-09 op do_clunk(nfid);
754 fb1a36c0 2022-01-09 op do_clunk(pwdfid);
755 fb1a36c0 2022-01-09 op pwdfid = nfid;
760 fb1a36c0 2022-01-09 op cmd_get(int argc, const char **argv)
762 fb1a36c0 2022-01-09 op struct qid qid;
763 fb1a36c0 2022-01-09 op const char *l;
766 fb1a36c0 2022-01-09 op if (argc != 1 && argc != 2) {
767 fb1a36c0 2022-01-09 op printf("usage: get remote-file [local-file]\n");
771 fb1a36c0 2022-01-09 op if (argc == 2)
773 fb1a36c0 2022-01-09 op else if ((l = strrchr(argv[0], '/')) != NULL)
774 fb1a36c0 2022-01-09 op l++; /* skip / */
778 fb1a36c0 2022-01-09 op nfid = pwdfid+1;
779 fb1a36c0 2022-01-09 op if (walk_path(pwdfid, nfid, argv[0], &qid) == -1) {
780 fb1a36c0 2022-01-09 op printf("can't fetch %s\n", argv[0]);
784 fb1a36c0 2022-01-09 op if (qid.type != 0) {
785 fb1a36c0 2022-01-09 op printf("can't fetch %s\n", argv[0]);
786 fb1a36c0 2022-01-09 op do_clunk(nfid);
790 fb1a36c0 2022-01-09 op fetch_fid(nfid, l);
794 fb1a36c0 2022-01-09 op cmd_lcd(int argc, const char **argv)
796 fb1a36c0 2022-01-09 op const char *dir;
798 fb1a36c0 2022-01-09 op if (argc > 1) {
799 fb1a36c0 2022-01-09 op printf("lcd takes only one argument\n");
803 fb1a36c0 2022-01-09 op if (argc == 1)
806 fb1a36c0 2022-01-09 op if (argc == 0 && (dir = getenv("HOME")) == NULL) {
807 fb1a36c0 2022-01-09 op printf("HOME is not defined\n");
811 fb1a36c0 2022-01-09 op if (chdir(dir) == -1)
812 fb1a36c0 2022-01-09 op printf("cd: %s: %s\n", dir, strerror(errno));
816 fb1a36c0 2022-01-09 op cmd_lpwd(int argc, const char **argv)
818 fb1a36c0 2022-01-09 op char path[PATH_MAX];
820 fb1a36c0 2022-01-09 op if (getcwd(path, sizeof(path)) == NULL) {
821 fb1a36c0 2022-01-09 op printf("lpwd: %s\n", strerror(errno));
825 fb1a36c0 2022-01-09 op printf("%s\n", path);
829 fb1a36c0 2022-01-09 op cmd_ls(int argc, const char **argv)
831 fb1a36c0 2022-01-09 op struct np_stat st;
832 fb1a36c0 2022-01-09 op uint64_t off = 0;
833 fb1a36c0 2022-01-09 op uint32_t len;
834 fb1a36c0 2022-01-09 op char fmt[FMT_SCALED_STRSIZE];
836 fb1a36c0 2022-01-09 op if (argc != 0) {
837 fb1a36c0 2022-01-09 op printf("ls don't take arguments (yet)\n");
841 fb1a36c0 2022-01-09 op dup_fid(pwdfid, 1);
842 fb1a36c0 2022-01-09 op do_open(1, KOREAD);
844 fb1a36c0 2022-01-09 op evbuffer_drain(dirbuf, EVBUFFER_LENGTH(dirbuf));
847 fb1a36c0 2022-01-09 op tread(1, off, BUFSIZ);
850 fb1a36c0 2022-01-09 op expect2(Rread, iota_tag);
852 fb1a36c0 2022-01-09 op len = np_read32(buf);
853 fb1a36c0 2022-01-09 op if (len == 0)
856 fb1a36c0 2022-01-09 op evbuffer_add_buffer(dirbuf, buf);
859 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
862 fb1a36c0 2022-01-09 op while (EVBUFFER_LENGTH(dirbuf) != 0) {
863 fb1a36c0 2022-01-09 op if (np_read_stat(dirbuf, &st) == -1)
864 fb1a36c0 2022-01-09 op errx(1, "invalid stat struct read");
866 fb1a36c0 2022-01-09 op if (fmt_scaled(st.length, fmt) == -1)
867 fb1a36c0 2022-01-09 op strlcpy(fmt, "xxx", sizeof(fmt));
869 fb1a36c0 2022-01-09 op printf("%4s %8s %s\n", pp_qid_type(st.qid.type), fmt, st.name);
871 fb1a36c0 2022-01-09 op free(st.name);
872 fb1a36c0 2022-01-09 op free(st.uid);
873 fb1a36c0 2022-01-09 op free(st.gid);
874 fb1a36c0 2022-01-09 op free(st.muid);
881 fb1a36c0 2022-01-09 op cmd_verbose(int argc, const char **argv)
883 fb1a36c0 2022-01-09 op if (argc == 0) {
884 fb1a36c0 2022-01-09 op log_setverbose(!log_getverbose());
885 fb1a36c0 2022-01-09 op if (log_getverbose())
886 fb1a36c0 2022-01-09 op puts("verbose mode enabled");
888 fb1a36c0 2022-01-09 op puts("verbose mode disabled");
892 fb1a36c0 2022-01-09 op if (argc != 1)
895 fb1a36c0 2022-01-09 op if (!strcmp(*argv, "on")) {
896 fb1a36c0 2022-01-09 op log_setverbose(1);
897 fb1a36c0 2022-01-09 op puts("verbose mode enabled");
901 fb1a36c0 2022-01-09 op if (!strcmp(*argv, "off")) {
902 fb1a36c0 2022-01-09 op log_setverbose(0);
903 fb1a36c0 2022-01-09 op puts("verbose mode disabled");
908 fb1a36c0 2022-01-09 op printf("verbose [on | off]\n");
912 fb1a36c0 2022-01-09 op excmd(int argc, const char **argv)
915 fb1a36c0 2022-01-09 op const char *name;
916 fb1a36c0 2022-01-09 op void (*fn)(int, const char **);
918 fb1a36c0 2022-01-09 op {"bell", cmd_bell},
919 fb1a36c0 2022-01-09 op {"bye", cmd_bye},
920 fb1a36c0 2022-01-09 op {"cd", cmd_cd},
921 fb1a36c0 2022-01-09 op {"get", cmd_get},
922 fb1a36c0 2022-01-09 op {"lcd", cmd_lcd},
923 fb1a36c0 2022-01-09 op {"lpwd", cmd_lpwd},
924 fb1a36c0 2022-01-09 op {"ls", cmd_ls},
925 fb1a36c0 2022-01-09 op {"quit", cmd_bye},
926 fb1a36c0 2022-01-09 op {"verbose", cmd_verbose},
930 fb1a36c0 2022-01-09 op if (argc == 0)
932 fb1a36c0 2022-01-09 op for (i = 0; i < nitems(cmds); ++i) {
933 fb1a36c0 2022-01-09 op if (!strcmp(cmds[i].name, *argv)) {
934 fb1a36c0 2022-01-09 op cmds[i].fn(argc-1, argv+1);
939 fb1a36c0 2022-01-09 op log_warnx("unknown command %s", *argv);
943 fb1a36c0 2022-01-09 op main(int argc, char **argv)
947 fb1a36c0 2022-01-09 op log_init(1, LOG_DAEMON);
948 fb1a36c0 2022-01-09 op log_setverbose(0);
949 fb1a36c0 2022-01-09 op log_procinit(getprogname());
951 fb1a36c0 2022-01-09 op while ((ch = getopt(argc, argv, "C:cK:")) != -1) {
952 fb1a36c0 2022-01-09 op switch (ch) {
954 fb1a36c0 2022-01-09 op crtpath = optarg;
960 fb1a36c0 2022-01-09 op keypath = optarg;
966 fb1a36c0 2022-01-09 op argc -= optind;
967 fb1a36c0 2022-01-09 op argv += optind;
969 fb1a36c0 2022-01-09 op if (argc == 0)
972 fb1a36c0 2022-01-09 op if (isatty(1)) {
975 fb1a36c0 2022-01-09 op signal(SIGWINCH, tty_resized);
978 fb1a36c0 2022-01-09 op if ((evb = evbuffer_new()) == NULL)
979 fb1a36c0 2022-01-09 op fatal("evbuffer_new");
981 fb1a36c0 2022-01-09 op if ((buf = evbuffer_new()) == NULL)
982 fb1a36c0 2022-01-09 op fatal("evbuffer_new");
984 fb1a36c0 2022-01-09 op if ((dirbuf = evbuffer_new()) == NULL)
985 fb1a36c0 2022-01-09 op fatal("evbuferr_new");
987 fb1a36c0 2022-01-09 op do_connect(argv[0], argv[1]);
990 fb1a36c0 2022-01-09 op int argc = 0;
991 fb1a36c0 2022-01-09 op char *line, *argv[16] = {0}, **ap;
993 fb1a36c0 2022-01-09 op if ((line = read_line("kamiftp> ")) == NULL)
996 fb1a36c0 2022-01-09 op for (argc = 0, ap = argv; ap < &argv[15] &&
997 fb1a36c0 2022-01-09 op (*ap = strsep(&line, " \t")) != NULL;) {
998 fb1a36c0 2022-01-09 op if (**ap != '\0')
999 fb1a36c0 2022-01-09 op ap++, argc++;
1001 fb1a36c0 2022-01-09 op excmd(argc, (const char **)argv);
1004 fb1a36c0 2022-01-09 op printf("\a");
1005 fb1a36c0 2022-01-09 op fflush(stdout);
1011 fb1a36c0 2022-01-09 op printf("\n");