2 dc8c1791 2022-01-10 op * Copyright (c) 2021, 2022 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>
22 9e7dd230 2022-01-12 op #include <sys/wait.h>
24 fb1a36c0 2022-01-09 op #include <assert.h>
25 fb1a36c0 2022-01-09 op #include <errno.h>
26 fb1a36c0 2022-01-09 op #include <fcntl.h>
27 fb1a36c0 2022-01-09 op #include <inttypes.h>
28 fb1a36c0 2022-01-09 op #include <netdb.h>
29 9e7dd230 2022-01-12 op #include <libgen.h>
30 fb1a36c0 2022-01-09 op #include <limits.h>
31 fb1a36c0 2022-01-09 op #include <signal.h>
32 fb1a36c0 2022-01-09 op #include <stdio.h>
33 fb1a36c0 2022-01-09 op #include <stdlib.h>
34 fb1a36c0 2022-01-09 op #include <string.h>
35 fb1a36c0 2022-01-09 op #include <syslog.h>
36 fb1a36c0 2022-01-09 op #include <tls.h>
37 fb1a36c0 2022-01-09 op #include <unistd.h>
39 91cfa92a 2022-01-10 op #ifdef HAVE_READLINE
40 fb1a36c0 2022-01-09 op #include <readline/readline.h>
41 fb1a36c0 2022-01-09 op #include <readline/history.h>
44 fb1a36c0 2022-01-09 op #ifndef nitems
45 fb1a36c0 2022-01-09 op #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
48 fb1a36c0 2022-01-09 op #include "9pclib.h"
49 fb1a36c0 2022-01-09 op #include "kami.h"
50 fb1a36c0 2022-01-09 op #include "utils.h"
51 fb1a36c0 2022-01-09 op #include "log.h"
55 fb1a36c0 2022-01-09 op const char *crtpath;
56 fb1a36c0 2022-01-09 op const char *keypath;
59 fb1a36c0 2022-01-09 op struct tls_config *tlsconf;
60 fb1a36c0 2022-01-09 op struct tls *ctx;
62 fb1a36c0 2022-01-09 op struct evbuffer *buf;
63 fb1a36c0 2022-01-09 op struct evbuffer *dirbuf;
64 fb1a36c0 2022-01-09 op uint32_t msize;
67 fb1a36c0 2022-01-09 op volatile sig_atomic_t resized;
69 fb1a36c0 2022-01-09 op int tty_width;
71 fb1a36c0 2022-01-09 op struct np_stat {
72 fb1a36c0 2022-01-09 op uint16_t type;
74 fb1a36c0 2022-01-09 op struct qid qid;
75 fb1a36c0 2022-01-09 op uint32_t mode;
76 fb1a36c0 2022-01-09 op uint32_t atime;
77 fb1a36c0 2022-01-09 op uint32_t mtime;
78 fb1a36c0 2022-01-09 op uint64_t length;
85 fb1a36c0 2022-01-09 op struct progress {
87 fb1a36c0 2022-01-09 op uint64_t done;
92 fb1a36c0 2022-01-09 op #define ASSERT_EMPTYBUF() assert(EVBUFFER_LENGTH(buf) == 0)
94 91cfa92a 2022-01-10 op #if !HAVE_READLINE
96 91cfa92a 2022-01-10 op readline(const char *prompt)
98 91cfa92a 2022-01-10 op char *ch, *line = NULL;
99 91cfa92a 2022-01-10 op size_t linesize = 0;
100 91cfa92a 2022-01-10 op ssize_t linelen;
102 91cfa92a 2022-01-10 op printf("%s", prompt);
103 91cfa92a 2022-01-10 op fflush(stdout);
105 91cfa92a 2022-01-10 op linelen = getline(&line, &linesize, stdin);
106 91cfa92a 2022-01-10 op if (linelen == -1)
109 91cfa92a 2022-01-10 op if ((ch = strchr(line, '\n')) != NULL)
115 91cfa92a 2022-01-10 op add_history(const char *line)
121 fb1a36c0 2022-01-09 op static char *
122 fb1a36c0 2022-01-09 op read_line(const char *prompt)
127 fb1a36c0 2022-01-09 op if ((line = readline(prompt)) == NULL)
129 fb1a36c0 2022-01-09 op /* XXX: trim spaces? */
130 fb1a36c0 2022-01-09 op if (*line == '\0') {
135 fb1a36c0 2022-01-09 op add_history(line);
140 9e7dd230 2022-01-12 op spawn(const char *argv0, ...)
145 9e7dd230 2022-01-12 op const char *argv[16], *last;
148 9e7dd230 2022-01-12 op memset(argv, 0, sizeof(argv));
150 9e7dd230 2022-01-12 op va_start(ap, argv0);
151 9e7dd230 2022-01-12 op argv[0] = argv0;
152 9e7dd230 2022-01-12 op for (i = 1; i < nitems(argv); ++i) {
153 9e7dd230 2022-01-12 op last = va_arg(ap, const char *);
154 9e7dd230 2022-01-12 op if (last == NULL)
156 9e7dd230 2022-01-12 op argv[i] = last;
160 9e7dd230 2022-01-12 op assert(last == NULL);
162 9e7dd230 2022-01-12 op switch (pid = fork()) {
164 9e7dd230 2022-01-12 op err(1, "fork");
165 9e7dd230 2022-01-12 op case 0: /* child */
166 9e7dd230 2022-01-12 op execvp(argv[0], (char *const *)argv);
167 9e7dd230 2022-01-12 op err(1, "execvp");
169 9e7dd230 2022-01-12 op waitpid(pid, &status, 0);
174 fb1a36c0 2022-01-09 op tty_resized(int signo)
179 fb1a36c0 2022-01-09 op static void __dead
180 fb1a36c0 2022-01-09 op usage(int ret)
182 dc8c1791 2022-01-10 op fprintf(stderr, "usage: %s [-c] [-C cert] [-K key] "
183 dc8c1791 2022-01-10 op "host[:port] [path]\n", getprogname());
184 fb1a36c0 2022-01-09 op fprintf(stderr, "kamid suite version " KAMID_VERSION "\n");
189 fb1a36c0 2022-01-09 op do_send(void)
191 fb1a36c0 2022-01-09 op const void *buf;
192 fb1a36c0 2022-01-09 op size_t nbytes;
195 fb1a36c0 2022-01-09 op while (EVBUFFER_LENGTH(evb) != 0) {
196 fb1a36c0 2022-01-09 op buf = EVBUFFER_DATA(evb);
197 fb1a36c0 2022-01-09 op nbytes = EVBUFFER_LENGTH(evb);
199 fb1a36c0 2022-01-09 op if (ctx == NULL) {
200 fb1a36c0 2022-01-09 op r = write(sock, buf, nbytes);
201 fb1a36c0 2022-01-09 op if (r == 0 || r == -1)
202 fb1a36c0 2022-01-09 op errx(1, "EOF");
204 fb1a36c0 2022-01-09 op r = tls_write(ctx, buf, nbytes);
205 fb1a36c0 2022-01-09 op if (r == TLS_WANT_POLLIN || r == TLS_WANT_POLLOUT)
208 fb1a36c0 2022-01-09 op errx(1, "tls: %s", tls_error(ctx));
211 fb1a36c0 2022-01-09 op evbuffer_drain(evb, r);
216 fb1a36c0 2022-01-09 op mustread(void *d, size_t len)
220 fb1a36c0 2022-01-09 op while (len != 0) {
221 fb1a36c0 2022-01-09 op if (ctx == NULL) {
222 fb1a36c0 2022-01-09 op r = read(sock, d, len);
223 fb1a36c0 2022-01-09 op if (r == 0 || r == -1)
224 fb1a36c0 2022-01-09 op errx(1, "EOF");
226 fb1a36c0 2022-01-09 op r = tls_read(ctx, d, len);
227 fb1a36c0 2022-01-09 op if (r == TLS_WANT_POLLIN || r == TLS_WANT_POLLOUT)
230 fb1a36c0 2022-01-09 op errx(1, "tls: %s", tls_error(ctx));
239 fb1a36c0 2022-01-09 op recv_msg(void)
241 fb1a36c0 2022-01-09 op uint32_t len, l;
242 fb1a36c0 2022-01-09 op char tmp[BUFSIZ];
244 fb1a36c0 2022-01-09 op mustread(&len, sizeof(len));
245 fb1a36c0 2022-01-09 op len = le32toh(len);
246 fb1a36c0 2022-01-09 op if (len < HEADERSIZE)
247 fb1a36c0 2022-01-09 op errx(1, "read message of invalid length %d", len);
249 fb1a36c0 2022-01-09 op len -= 4; /* skip the length just read */
251 fb1a36c0 2022-01-09 op while (len != 0) {
252 fb1a36c0 2022-01-09 op l = MIN(len, sizeof(tmp));
253 fb1a36c0 2022-01-09 op mustread(tmp, l);
255 fb1a36c0 2022-01-09 op evbuffer_add(buf, tmp, l);
259 fb1a36c0 2022-01-09 op static uint64_t
260 fb1a36c0 2022-01-09 op np_read64(struct evbuffer *buf)
264 fb1a36c0 2022-01-09 op evbuffer_remove(buf, &n, sizeof(n));
265 fb1a36c0 2022-01-09 op return le64toh(n);
268 fb1a36c0 2022-01-09 op static uint32_t
269 fb1a36c0 2022-01-09 op np_read32(struct evbuffer *buf)
273 fb1a36c0 2022-01-09 op evbuffer_remove(buf, &n, sizeof(n));
274 fb1a36c0 2022-01-09 op return le32toh(n);
277 fb1a36c0 2022-01-09 op static uint16_t
278 fb1a36c0 2022-01-09 op np_read16(struct evbuffer *buf)
282 fb1a36c0 2022-01-09 op evbuffer_remove(buf, &n, sizeof(n));
283 fb1a36c0 2022-01-09 op return le16toh(n);
286 fb1a36c0 2022-01-09 op static uint16_t
287 fb1a36c0 2022-01-09 op np_read8(struct evbuffer *buf)
291 fb1a36c0 2022-01-09 op evbuffer_remove(buf, &n, sizeof(n));
295 fb1a36c0 2022-01-09 op static char *
296 fb1a36c0 2022-01-09 op np_readstr(struct evbuffer *buf)
298 fb1a36c0 2022-01-09 op uint16_t len;
301 fb1a36c0 2022-01-09 op len = np_read16(buf);
302 fb1a36c0 2022-01-09 op assert(EVBUFFER_LENGTH(buf) >= len);
304 fb1a36c0 2022-01-09 op if ((str = calloc(1, len+1)) == NULL)
305 fb1a36c0 2022-01-09 op err(1, "calloc");
306 fb1a36c0 2022-01-09 op evbuffer_remove(buf, str, len);
311 fb1a36c0 2022-01-09 op np_read_qid(struct evbuffer *buf, struct qid *qid)
313 fb1a36c0 2022-01-09 op assert(EVBUFFER_LENGTH(buf) >= QIDSIZE);
315 fb1a36c0 2022-01-09 op qid->type = np_read8(buf);
316 fb1a36c0 2022-01-09 op qid->vers = np_read32(buf);
317 fb1a36c0 2022-01-09 op qid->path = np_read64(buf);
321 fb1a36c0 2022-01-09 op np_read_stat(struct evbuffer *buf, struct np_stat *st)
323 fb1a36c0 2022-01-09 op uint16_t size;
325 fb1a36c0 2022-01-09 op memset(st, 0, sizeof(*st));
327 fb1a36c0 2022-01-09 op size = np_read16(buf);
328 fb1a36c0 2022-01-09 op if (size > EVBUFFER_LENGTH(buf))
331 fb1a36c0 2022-01-09 op st->type = np_read16(buf);
332 fb1a36c0 2022-01-09 op st->dev = np_read32(buf);
333 fb1a36c0 2022-01-09 op np_read_qid(buf, &st->qid);
334 fb1a36c0 2022-01-09 op st->mode = np_read32(buf);
335 fb1a36c0 2022-01-09 op st->atime = np_read32(buf);
336 fb1a36c0 2022-01-09 op st->mtime = np_read32(buf);
337 fb1a36c0 2022-01-09 op st->length = np_read64(buf);
338 fb1a36c0 2022-01-09 op st->name = np_readstr(buf);
339 fb1a36c0 2022-01-09 op st->uid = np_readstr(buf);
340 fb1a36c0 2022-01-09 op st->gid = np_readstr(buf);
341 fb1a36c0 2022-01-09 op st->muid = np_readstr(buf);
347 fb1a36c0 2022-01-09 op expect(uint8_t type)
351 fb1a36c0 2022-01-09 op t = np_read8(buf);
352 fb1a36c0 2022-01-09 op if (t == type)
355 fb1a36c0 2022-01-09 op if (t == Rerror) {
358 fb1a36c0 2022-01-09 op /* skip tag */
359 fb1a36c0 2022-01-09 op np_read16(buf);
361 fb1a36c0 2022-01-09 op err = np_readstr(buf);
362 fb1a36c0 2022-01-09 op errx(1, "expected %s, got error %s",
363 fb1a36c0 2022-01-09 op pp_msg_type(type), err);
366 fb1a36c0 2022-01-09 op errx(1, "expected %s, got msg type %s",
367 fb1a36c0 2022-01-09 op pp_msg_type(type), pp_msg_type(t));
371 fb1a36c0 2022-01-09 op expect2(uint8_t type, uint16_t tag)
375 fb1a36c0 2022-01-09 op expect(type);
377 fb1a36c0 2022-01-09 op t = np_read16(buf);
378 fb1a36c0 2022-01-09 op if (t == tag)
381 fb1a36c0 2022-01-09 op errx(1, "expected tag 0x%x, got 0x%x", tag, t);
384 8f9f99f7 2022-01-16 op static char *
385 8f9f99f7 2022-01-16 op check(uint8_t type, uint16_t tag)
387 8f9f99f7 2022-01-16 op uint16_t rtag;
388 8f9f99f7 2022-01-16 op uint8_t rtype;
390 8f9f99f7 2022-01-16 op rtype = np_read8(buf);
391 8f9f99f7 2022-01-16 op rtag = np_read16(buf);
392 8f9f99f7 2022-01-16 op if (rtype == type) {
393 8f9f99f7 2022-01-16 op if (rtag != tag)
394 8f9f99f7 2022-01-16 op errx(1, "expected tag 0x%x, got 0x%x", tag, rtag);
398 8f9f99f7 2022-01-16 op if (rtype == Rerror)
399 8f9f99f7 2022-01-16 op return np_readstr(buf);
401 8f9f99f7 2022-01-16 op errx(1, "expected %s, got msg type %s",
402 8f9f99f7 2022-01-16 op pp_msg_type(type), pp_msg_type(rtype));
406 fb1a36c0 2022-01-09 op do_version(void)
408 fb1a36c0 2022-01-09 op char *version;
410 fb1a36c0 2022-01-09 op tversion(VERSION9P, MSIZE9P);
413 fb1a36c0 2022-01-09 op expect2(Rversion, NOTAG);
415 fb1a36c0 2022-01-09 op msize = np_read32(buf);
416 fb1a36c0 2022-01-09 op version = np_readstr(buf);
418 fb1a36c0 2022-01-09 op if (msize > MSIZE9P)
419 fb1a36c0 2022-01-09 op errx(1, "got unexpected msize: %d", msize);
420 fb1a36c0 2022-01-09 op if (strcmp(version, VERSION9P))
421 fb1a36c0 2022-01-09 op errx(1, "unexpected 9p version: %s", version);
423 fb1a36c0 2022-01-09 op free(version);
424 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
428 fb1a36c0 2022-01-09 op do_attach(const char *path)
430 fb1a36c0 2022-01-09 op const char *user;
431 fb1a36c0 2022-01-09 op struct qid qid;
433 fb1a36c0 2022-01-09 op if (path == NULL)
435 fb1a36c0 2022-01-09 op if ((user = getenv("USER")) == NULL)
436 fb1a36c0 2022-01-09 op user = "flan";
438 fb1a36c0 2022-01-09 op tattach(pwdfid, NOFID, user, path);
441 fb1a36c0 2022-01-09 op expect2(Rattach, iota_tag);
442 fb1a36c0 2022-01-09 op np_read_qid(buf, &qid);
444 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
447 fb1a36c0 2022-01-09 op static uint32_t
448 fb1a36c0 2022-01-09 op do_open(uint32_t fid, uint8_t mode)
450 fb1a36c0 2022-01-09 op struct qid qid;
451 fb1a36c0 2022-01-09 op uint32_t iounit;
453 fb1a36c0 2022-01-09 op topen(fid, mode);
456 fb1a36c0 2022-01-09 op expect2(Ropen, iota_tag);
458 fb1a36c0 2022-01-09 op np_read_qid(buf, &qid);
459 fb1a36c0 2022-01-09 op iounit = np_read32(buf);
461 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
463 fb1a36c0 2022-01-09 op return iounit;
467 fb1a36c0 2022-01-09 op do_clunk(uint32_t fid)
472 fb1a36c0 2022-01-09 op expect2(Rclunk, iota_tag);
474 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
478 fb1a36c0 2022-01-09 op dup_fid(int fid, int nfid)
480 fb1a36c0 2022-01-09 op uint16_t nwqid;
482 fb1a36c0 2022-01-09 op twalk(fid, nfid, NULL, 0);
485 fb1a36c0 2022-01-09 op expect2(Rwalk, iota_tag);
487 fb1a36c0 2022-01-09 op nwqid = np_read16(buf);
488 fb1a36c0 2022-01-09 op assert(nwqid == 0);
490 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
493 8f9f99f7 2022-01-16 op static char *
494 8f9f99f7 2022-01-16 op walk_path(int fid, int newfid, const char *path, int *missing,
495 8f9f99f7 2022-01-16 op struct qid *qid)
497 8f9f99f7 2022-01-16 op char *wnames[MAXWELEM], *p, *t, *errstr;
498 fb1a36c0 2022-01-09 op size_t nwname, i;
499 fb1a36c0 2022-01-09 op uint16_t nwqid;
501 fb1a36c0 2022-01-09 op if ((p = strdup(path)) == NULL)
502 fb1a36c0 2022-01-09 op err(1, "strdup");
505 fb1a36c0 2022-01-09 op /* strip initial ./ */
506 fb1a36c0 2022-01-09 op if (t[0] == '.' && t[1] == '/')
509 fb1a36c0 2022-01-09 op for (nwname = 0; nwname < nitems(wnames) &&
510 fb1a36c0 2022-01-09 op (wnames[nwname] = strsep(&t, "/")) != NULL;) {
511 fb1a36c0 2022-01-09 op if (*wnames[nwname] != '\0')
515 fb1a36c0 2022-01-09 op twalk(fid, newfid, (const char **)wnames, nwname);
519 8f9f99f7 2022-01-16 op *missing = nwname;
520 8f9f99f7 2022-01-16 op if ((errstr = check(Rwalk, iota_tag)) != NULL)
521 8f9f99f7 2022-01-16 op return errstr;
523 fb1a36c0 2022-01-09 op nwqid = np_read16(buf);
524 fb1a36c0 2022-01-09 op assert(nwqid <= nwname);
526 fb1a36c0 2022-01-09 op /* consume all qids */
527 fb1a36c0 2022-01-09 op for (i = 0; i < nwname; ++i)
528 fb1a36c0 2022-01-09 op np_read_qid(buf, qid);
532 8f9f99f7 2022-01-16 op *missing = nwname - nwqid;
537 fb1a36c0 2022-01-09 op do_stat(int fid, struct np_stat *st)
542 fb1a36c0 2022-01-09 op expect2(Rstat, iota_tag);
544 fb1a36c0 2022-01-09 op if (np_read_stat(buf, st) == -1)
545 fb1a36c0 2022-01-09 op errx(1, "invalid stat struct read");
547 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
550 fb1a36c0 2022-01-09 op static size_t
551 fb1a36c0 2022-01-09 op do_read(int fid, uint64_t off, uint32_t count, void *data)
555 fb1a36c0 2022-01-09 op tread(fid, off, count);
558 fb1a36c0 2022-01-09 op expect2(Rread, iota_tag);
560 fb1a36c0 2022-01-09 op r = np_read32(buf);
561 fb1a36c0 2022-01-09 op assert(r == EVBUFFER_LENGTH(buf));
562 fb1a36c0 2022-01-09 op assert(r <= count);
563 fb1a36c0 2022-01-09 op evbuffer_remove(buf, data, r);
565 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
571 fb1a36c0 2022-01-09 op draw_progress(const char *pre, const struct progress *p)
573 fb1a36c0 2022-01-09 op struct winsize ws;
577 fb1a36c0 2022-01-09 op perc = 100.0 * p->done / p->max;
578 fb1a36c0 2022-01-09 op if (!tty_p) {
579 fb1a36c0 2022-01-09 op fprintf(stderr, "%s: %d%%\n", pre, (int)perc);
583 fb1a36c0 2022-01-09 op if (resized) {
586 fb1a36c0 2022-01-09 op if (ioctl(0, TIOCGWINSZ, &ws) == -1)
588 fb1a36c0 2022-01-09 op tty_width = ws.ws_col;
590 fb1a36c0 2022-01-09 op w = tty_width;
592 fb1a36c0 2022-01-09 op if (pre == NULL ||
593 fb1a36c0 2022-01-09 op ((l = printf("\r%s ", pre)) == -1 || l >= w))
596 fb1a36c0 2022-01-09 op w -= l + 2 + 5; /* 2 for |, 5 for percentage + \n */
598 fb1a36c0 2022-01-09 op printf("%4d%%\n", (int)perc);
604 fb1a36c0 2022-01-09 op l = w * MIN(100.0, perc) / 100.0;
605 fb1a36c0 2022-01-09 op for (i = 0; i < l; i++)
607 fb1a36c0 2022-01-09 op for (; i < w; i++)
609 fb1a36c0 2022-01-09 op printf("|%4d%%", (int)perc);
611 fb1a36c0 2022-01-09 op fflush(stdout);
615 76521d26 2022-01-16 op fetch_fid(int fid, int fd, const char *name)
617 fb1a36c0 2022-01-09 op struct progress p = {0};
618 fb1a36c0 2022-01-09 op struct np_stat st;
620 fb1a36c0 2022-01-09 op char buf[BUFSIZ];
622 fb1a36c0 2022-01-09 op do_stat(fid, &st);
623 fb1a36c0 2022-01-09 op do_open(fid, KOREAD);
625 fb1a36c0 2022-01-09 op p.max = st.length;
630 fb1a36c0 2022-01-09 op r = do_read(fid, p.done, sizeof(buf), buf);
634 b2263c45 2022-01-16 op for (off = 0; off < r; off += nw)
635 b2263c45 2022-01-16 op if ((nw = write(fd, buf + off, r - off)) == 0 ||
637 fb1a36c0 2022-01-09 op err(1, "write");
640 9e7dd230 2022-01-12 op draw_progress(name, &p);
643 fb1a36c0 2022-01-09 op /* throttle, for debugging purpose */
645 fb1a36c0 2022-01-09 op struct timespec ts = { 0, 500000000 };
646 fb1a36c0 2022-01-09 op nanosleep(&ts, NULL);
651 fb1a36c0 2022-01-09 op putchar('\n');
653 fb1a36c0 2022-01-09 op do_clunk(fid);
657 fb1a36c0 2022-01-09 op do_tls_connect(const char *host, const char *port)
659 fb1a36c0 2022-01-09 op int handshake;
661 fb1a36c0 2022-01-09 op if ((tlsconf = tls_config_new()) == NULL)
662 fb1a36c0 2022-01-09 op fatalx("tls_config_new");
663 fb1a36c0 2022-01-09 op tls_config_insecure_noverifycert(tlsconf);
664 fb1a36c0 2022-01-09 op tls_config_insecure_noverifyname(tlsconf);
665 fb1a36c0 2022-01-09 op if (tls_config_set_keypair_file(tlsconf, crtpath, keypath) == -1)
666 fb1a36c0 2022-01-09 op fatalx("can't load certs (%s, %s)", crtpath, keypath);
668 fb1a36c0 2022-01-09 op if ((ctx = tls_client()) == NULL)
669 fb1a36c0 2022-01-09 op fatal("tls_client");
670 fb1a36c0 2022-01-09 op if (tls_configure(ctx, tlsconf) == -1)
671 fb1a36c0 2022-01-09 op fatalx("tls_configure: %s", tls_error(ctx));
673 fb1a36c0 2022-01-09 op if (tls_connect(ctx, host, port) == -1)
674 fb1a36c0 2022-01-09 op fatalx("can't connect to %s:%s: %s", host, port,
675 fb1a36c0 2022-01-09 op tls_error(ctx));
677 fb1a36c0 2022-01-09 op for (handshake = 0; !handshake;) {
678 fb1a36c0 2022-01-09 op switch (tls_handshake(ctx)) {
680 fb1a36c0 2022-01-09 op fatalx("tls_handshake: %s", tls_error(ctx));
682 fb1a36c0 2022-01-09 op handshake = 1;
689 fb1a36c0 2022-01-09 op do_ctxt_connect(const char *host, const char *port)
691 fb1a36c0 2022-01-09 op struct addrinfo hints, *res, *res0;
692 fb1a36c0 2022-01-09 op int error, saved_errno;
693 fb1a36c0 2022-01-09 op const char *cause = NULL;
695 fb1a36c0 2022-01-09 op memset(&hints, 0, sizeof(hints));
696 fb1a36c0 2022-01-09 op hints.ai_family = AF_UNSPEC;
697 fb1a36c0 2022-01-09 op hints.ai_socktype = SOCK_STREAM;
698 fb1a36c0 2022-01-09 op error = getaddrinfo(host, port, &hints, &res0);
700 fb1a36c0 2022-01-09 op errx(1, "%s", gai_strerror(error));
703 fb1a36c0 2022-01-09 op for (res = res0; res != NULL; res = res->ai_next) {
704 db066f1e 2022-01-12 op sock = socket(res->ai_family, res->ai_socktype|SOCK_CLOEXEC,
705 fb1a36c0 2022-01-09 op res->ai_protocol);
706 fb1a36c0 2022-01-09 op if (sock == -1) {
707 fb1a36c0 2022-01-09 op cause = "socket";
711 fb1a36c0 2022-01-09 op if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
712 fb1a36c0 2022-01-09 op cause = "connect";
713 fb1a36c0 2022-01-09 op saved_errno = errno;
715 fb1a36c0 2022-01-09 op errno = saved_errno;
723 fb1a36c0 2022-01-09 op if (sock == -1)
724 fb1a36c0 2022-01-09 op err(1, "%s", cause);
725 fb1a36c0 2022-01-09 op freeaddrinfo(res0);
729 fb1a36c0 2022-01-09 op do_connect(const char *connspec, const char *path)
731 fb1a36c0 2022-01-09 op char *host, *colon;
732 fb1a36c0 2022-01-09 op const char *port;
734 fb1a36c0 2022-01-09 op host = xstrdup(connspec);
735 fb1a36c0 2022-01-09 op if ((colon = strchr(host, ':')) != NULL) {
736 fb1a36c0 2022-01-09 op *colon = '\0';
737 fb1a36c0 2022-01-09 op port = ++colon;
739 fb1a36c0 2022-01-09 op port = "1337";
741 fb1a36c0 2022-01-09 op printf("connecting to %s:%s...", host, port);
742 fb1a36c0 2022-01-09 op fflush(stdout);
745 fb1a36c0 2022-01-09 op do_tls_connect(host, port);
747 fb1a36c0 2022-01-09 op do_ctxt_connect(host, port);
749 fb1a36c0 2022-01-09 op printf(" done!\n");
751 fb1a36c0 2022-01-09 op do_version();
752 fb1a36c0 2022-01-09 op do_attach(path);
758 fb1a36c0 2022-01-09 op cmd_bell(int argc, const char **argv)
760 fb1a36c0 2022-01-09 op if (argc == 0) {
761 fb1a36c0 2022-01-09 op bell = !bell;
763 fb1a36c0 2022-01-09 op puts("bell mode enabled");
765 fb1a36c0 2022-01-09 op puts("bell mode disabled");
769 fb1a36c0 2022-01-09 op if (argc != 1)
772 fb1a36c0 2022-01-09 op if (!strcmp(*argv, "on")) {
774 fb1a36c0 2022-01-09 op puts("bell mode enabled");
778 fb1a36c0 2022-01-09 op if (!strcmp(*argv, "off")) {
780 fb1a36c0 2022-01-09 op puts("bell mode disabled");
785 fb1a36c0 2022-01-09 op printf("bell [on | off]\n");
789 fb1a36c0 2022-01-09 op cmd_bye(int argc, const char **argv)
791 fb1a36c0 2022-01-09 op log_warnx("bye\n");
796 fb1a36c0 2022-01-09 op cmd_cd(int argc, const char **argv)
798 fb1a36c0 2022-01-09 op struct qid qid;
799 8f9f99f7 2022-01-16 op int nfid, miss;
800 8f9f99f7 2022-01-16 op char *errstr;
802 fb1a36c0 2022-01-09 op if (argc != 1) {
803 fb1a36c0 2022-01-09 op printf("usage: cd remote-path\n");
807 fb1a36c0 2022-01-09 op nfid = pwdfid+1;
808 8f9f99f7 2022-01-16 op errstr = walk_path(pwdfid, nfid, argv[0], &miss, &qid);
809 8f9f99f7 2022-01-16 op if (errstr != NULL) {
810 8f9f99f7 2022-01-16 op printf("%s: %s\n", argv[0], errstr);
811 8f9f99f7 2022-01-16 op free(errstr);
815 8f9f99f7 2022-01-16 op if (miss != 0 || !(qid.type & QTDIR)) {
816 8f9f99f7 2022-01-16 op printf("%s: not a directory\n", argv[0]);
817 fb1a36c0 2022-01-09 op do_clunk(nfid);
821 8f9f99f7 2022-01-16 op do_clunk(pwdfid);
822 8f9f99f7 2022-01-16 op pwdfid = nfid;
826 fb1a36c0 2022-01-09 op cmd_get(int argc, const char **argv)
828 fb1a36c0 2022-01-09 op struct qid qid;
829 fb1a36c0 2022-01-09 op const char *l;
830 8f9f99f7 2022-01-16 op char *errstr;
831 8f9f99f7 2022-01-16 op int nfid, fd, miss;
833 fb1a36c0 2022-01-09 op if (argc != 1 && argc != 2) {
834 fb1a36c0 2022-01-09 op printf("usage: get remote-file [local-file]\n");
838 fb1a36c0 2022-01-09 op if (argc == 2)
840 fb1a36c0 2022-01-09 op else if ((l = strrchr(argv[0], '/')) != NULL)
841 fb1a36c0 2022-01-09 op l++; /* skip / */
845 fb1a36c0 2022-01-09 op nfid = pwdfid+1;
846 8f9f99f7 2022-01-16 op errstr = walk_path(pwdfid, nfid, argv[0], &miss, &qid);
847 8f9f99f7 2022-01-16 op if (errstr != NULL) {
848 8f9f99f7 2022-01-16 op printf("%s: %s\n", argv[0], errstr);
849 8f9f99f7 2022-01-16 op free(errstr);
853 8f9f99f7 2022-01-16 op if (miss != 0 || qid.type != 0) {
854 8f9f99f7 2022-01-16 op printf("%s: not a file\n", argv[0]);
855 fb1a36c0 2022-01-09 op do_clunk(nfid);
859 76521d26 2022-01-16 op if ((fd = open(l, O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0644)) == -1) {
860 76521d26 2022-01-16 op warn("can't open %s", l);
861 76521d26 2022-01-16 op do_clunk(nfid);
865 76521d26 2022-01-16 op fetch_fid(nfid, fd, l);
870 fb1a36c0 2022-01-09 op cmd_lcd(int argc, const char **argv)
872 fb1a36c0 2022-01-09 op const char *dir;
874 fb1a36c0 2022-01-09 op if (argc > 1) {
875 fb1a36c0 2022-01-09 op printf("lcd takes only one argument\n");
879 fb1a36c0 2022-01-09 op if (argc == 1)
882 fb1a36c0 2022-01-09 op if (argc == 0 && (dir = getenv("HOME")) == NULL) {
883 fb1a36c0 2022-01-09 op printf("HOME is not defined\n");
887 fb1a36c0 2022-01-09 op if (chdir(dir) == -1)
888 fb1a36c0 2022-01-09 op printf("cd: %s: %s\n", dir, strerror(errno));
892 fb1a36c0 2022-01-09 op cmd_lpwd(int argc, const char **argv)
894 fb1a36c0 2022-01-09 op char path[PATH_MAX];
896 fb1a36c0 2022-01-09 op if (getcwd(path, sizeof(path)) == NULL) {
897 fb1a36c0 2022-01-09 op printf("lpwd: %s\n", strerror(errno));
901 fb1a36c0 2022-01-09 op printf("%s\n", path);
905 fb1a36c0 2022-01-09 op cmd_ls(int argc, const char **argv)
907 fb1a36c0 2022-01-09 op struct np_stat st;
908 fb1a36c0 2022-01-09 op uint64_t off = 0;
909 fb1a36c0 2022-01-09 op uint32_t len;
910 fb1a36c0 2022-01-09 op char fmt[FMT_SCALED_STRSIZE];
912 fb1a36c0 2022-01-09 op if (argc != 0) {
913 fb1a36c0 2022-01-09 op printf("ls don't take arguments (yet)\n");
917 fb1a36c0 2022-01-09 op dup_fid(pwdfid, 1);
918 fb1a36c0 2022-01-09 op do_open(1, KOREAD);
920 fb1a36c0 2022-01-09 op evbuffer_drain(dirbuf, EVBUFFER_LENGTH(dirbuf));
923 fb1a36c0 2022-01-09 op tread(1, off, BUFSIZ);
926 fb1a36c0 2022-01-09 op expect2(Rread, iota_tag);
928 fb1a36c0 2022-01-09 op len = np_read32(buf);
929 fb1a36c0 2022-01-09 op if (len == 0)
932 fb1a36c0 2022-01-09 op evbuffer_add_buffer(dirbuf, buf);
935 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
938 fb1a36c0 2022-01-09 op while (EVBUFFER_LENGTH(dirbuf) != 0) {
939 fb1a36c0 2022-01-09 op if (np_read_stat(dirbuf, &st) == -1)
940 fb1a36c0 2022-01-09 op errx(1, "invalid stat struct read");
942 fb1a36c0 2022-01-09 op if (fmt_scaled(st.length, fmt) == -1)
943 fb1a36c0 2022-01-09 op strlcpy(fmt, "xxx", sizeof(fmt));
945 fb1a36c0 2022-01-09 op printf("%4s %8s %s\n", pp_qid_type(st.qid.type), fmt, st.name);
947 fb1a36c0 2022-01-09 op free(st.name);
948 fb1a36c0 2022-01-09 op free(st.uid);
949 fb1a36c0 2022-01-09 op free(st.gid);
950 fb1a36c0 2022-01-09 op free(st.muid);
957 9e7dd230 2022-01-12 op cmd_page(int argc, const char **argv)
959 9e7dd230 2022-01-12 op struct qid qid;
960 8f9f99f7 2022-01-16 op int nfid, tmpfd, miss;
961 8f9f99f7 2022-01-16 op char sfn[24], p[PATH_MAX], *name, *errstr;
963 9e7dd230 2022-01-12 op if (argc != 1) {
964 9e7dd230 2022-01-12 op puts("usage: page file");
968 9e7dd230 2022-01-12 op nfid = pwdfid+1;
969 8f9f99f7 2022-01-16 op errstr = walk_path(pwdfid, nfid, *argv, &miss, &qid);
970 8f9f99f7 2022-01-16 op if (errstr != NULL) {
971 8f9f99f7 2022-01-16 op printf("%s: %s\n", *argv, errstr);
972 8f9f99f7 2022-01-16 op free(errstr);
976 8f9f99f7 2022-01-16 op if (miss != 0 || qid.type != 0) {
977 8f9f99f7 2022-01-16 op printf("%s: not a file\n", *argv);
978 9e7dd230 2022-01-12 op do_clunk(nfid);
982 9e7dd230 2022-01-12 op strlcpy(sfn, "/tmp/kamiftp.XXXXXXXXXX", sizeof(sfn));
983 9e7dd230 2022-01-12 op if ((tmpfd = mkstemp(sfn)) == -1) {
984 9e7dd230 2022-01-12 op warn("mkstemp %s", sfn);
985 9e7dd230 2022-01-12 op do_clunk(nfid);
989 9e7dd230 2022-01-12 op strlcpy(p, *argv, sizeof(p));
990 9e7dd230 2022-01-12 op name = basename(p);
991 76521d26 2022-01-16 op fetch_fid(nfid, tmpfd, name);
992 9e7dd230 2022-01-12 op close(tmpfd);
993 9e7dd230 2022-01-12 op spawn("less", sfn, NULL);
998 fb1a36c0 2022-01-09 op cmd_verbose(int argc, const char **argv)
1000 fb1a36c0 2022-01-09 op if (argc == 0) {
1001 fb1a36c0 2022-01-09 op log_setverbose(!log_getverbose());
1002 fb1a36c0 2022-01-09 op if (log_getverbose())
1003 fb1a36c0 2022-01-09 op puts("verbose mode enabled");
1005 fb1a36c0 2022-01-09 op puts("verbose mode disabled");
1009 fb1a36c0 2022-01-09 op if (argc != 1)
1012 fb1a36c0 2022-01-09 op if (!strcmp(*argv, "on")) {
1013 fb1a36c0 2022-01-09 op log_setverbose(1);
1014 fb1a36c0 2022-01-09 op puts("verbose mode enabled");
1018 fb1a36c0 2022-01-09 op if (!strcmp(*argv, "off")) {
1019 fb1a36c0 2022-01-09 op log_setverbose(0);
1020 fb1a36c0 2022-01-09 op puts("verbose mode disabled");
1025 fb1a36c0 2022-01-09 op printf("verbose [on | off]\n");
1029 fb1a36c0 2022-01-09 op excmd(int argc, const char **argv)
1031 fb1a36c0 2022-01-09 op struct cmd {
1032 fb1a36c0 2022-01-09 op const char *name;
1033 fb1a36c0 2022-01-09 op void (*fn)(int, const char **);
1034 fb1a36c0 2022-01-09 op } cmds[] = {
1035 fb1a36c0 2022-01-09 op {"bell", cmd_bell},
1036 fb1a36c0 2022-01-09 op {"bye", cmd_bye},
1037 fb1a36c0 2022-01-09 op {"cd", cmd_cd},
1038 fb1a36c0 2022-01-09 op {"get", cmd_get},
1039 fb1a36c0 2022-01-09 op {"lcd", cmd_lcd},
1040 fb1a36c0 2022-01-09 op {"lpwd", cmd_lpwd},
1041 fb1a36c0 2022-01-09 op {"ls", cmd_ls},
1042 9e7dd230 2022-01-12 op {"page", cmd_page},
1043 fb1a36c0 2022-01-09 op {"quit", cmd_bye},
1044 fb1a36c0 2022-01-09 op {"verbose", cmd_verbose},
1048 fb1a36c0 2022-01-09 op if (argc == 0)
1050 fb1a36c0 2022-01-09 op for (i = 0; i < nitems(cmds); ++i) {
1051 fb1a36c0 2022-01-09 op if (!strcmp(cmds[i].name, *argv)) {
1052 fb1a36c0 2022-01-09 op cmds[i].fn(argc-1, argv+1);
1057 fb1a36c0 2022-01-09 op log_warnx("unknown command %s", *argv);
1061 fb1a36c0 2022-01-09 op main(int argc, char **argv)
1065 fb1a36c0 2022-01-09 op log_init(1, LOG_DAEMON);
1066 fb1a36c0 2022-01-09 op log_setverbose(0);
1067 fb1a36c0 2022-01-09 op log_procinit(getprogname());
1069 fb1a36c0 2022-01-09 op while ((ch = getopt(argc, argv, "C:cK:")) != -1) {
1070 fb1a36c0 2022-01-09 op switch (ch) {
1072 fb1a36c0 2022-01-09 op crtpath = optarg;
1078 fb1a36c0 2022-01-09 op keypath = optarg;
1084 fb1a36c0 2022-01-09 op argc -= optind;
1085 fb1a36c0 2022-01-09 op argv += optind;
1087 fb1a36c0 2022-01-09 op if (argc == 0)
1090 fb1a36c0 2022-01-09 op if (isatty(1)) {
1092 fb1a36c0 2022-01-09 op resized = 1;
1093 fb1a36c0 2022-01-09 op signal(SIGWINCH, tty_resized);
1096 fb1a36c0 2022-01-09 op if ((evb = evbuffer_new()) == NULL)
1097 fb1a36c0 2022-01-09 op fatal("evbuffer_new");
1099 fb1a36c0 2022-01-09 op if ((buf = evbuffer_new()) == NULL)
1100 fb1a36c0 2022-01-09 op fatal("evbuffer_new");
1102 fb1a36c0 2022-01-09 op if ((dirbuf = evbuffer_new()) == NULL)
1103 fb1a36c0 2022-01-09 op fatal("evbuferr_new");
1105 fb1a36c0 2022-01-09 op do_connect(argv[0], argv[1]);
1108 fb1a36c0 2022-01-09 op int argc = 0;
1109 fb1a36c0 2022-01-09 op char *line, *argv[16] = {0}, **ap;
1111 fb1a36c0 2022-01-09 op if ((line = read_line("kamiftp> ")) == NULL)
1114 fb1a36c0 2022-01-09 op for (argc = 0, ap = argv; ap < &argv[15] &&
1115 fb1a36c0 2022-01-09 op (*ap = strsep(&line, " \t")) != NULL;) {
1116 fb1a36c0 2022-01-09 op if (**ap != '\0')
1117 fb1a36c0 2022-01-09 op ap++, argc++;
1119 fb1a36c0 2022-01-09 op excmd(argc, (const char **)argv);
1122 fb1a36c0 2022-01-09 op printf("\a");
1123 fb1a36c0 2022-01-09 op fflush(stdout);
1129 fb1a36c0 2022-01-09 op printf("\n");