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 #include <readline/readline.h>
39 fb1a36c0 2022-01-09 op #include <readline/history.h>
41 fb1a36c0 2022-01-09 op #ifndef nitems
42 fb1a36c0 2022-01-09 op #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
45 fb1a36c0 2022-01-09 op #include "9pclib.h"
46 fb1a36c0 2022-01-09 op #include "kami.h"
47 fb1a36c0 2022-01-09 op #include "utils.h"
48 fb1a36c0 2022-01-09 op #include "log.h"
52 fb1a36c0 2022-01-09 op const char *crtpath;
53 fb1a36c0 2022-01-09 op const char *keypath;
56 fb1a36c0 2022-01-09 op struct tls_config *tlsconf;
57 fb1a36c0 2022-01-09 op struct tls *ctx;
59 fb1a36c0 2022-01-09 op struct evbuffer *buf;
60 fb1a36c0 2022-01-09 op struct evbuffer *dirbuf;
61 fb1a36c0 2022-01-09 op uint32_t msize;
64 fb1a36c0 2022-01-09 op volatile sig_atomic_t resized;
66 fb1a36c0 2022-01-09 op int tty_width;
68 fb1a36c0 2022-01-09 op struct np_stat {
69 fb1a36c0 2022-01-09 op uint16_t type;
71 fb1a36c0 2022-01-09 op struct qid qid;
72 fb1a36c0 2022-01-09 op uint32_t mode;
73 fb1a36c0 2022-01-09 op uint32_t atime;
74 fb1a36c0 2022-01-09 op uint32_t mtime;
75 fb1a36c0 2022-01-09 op uint64_t length;
82 fb1a36c0 2022-01-09 op struct progress {
84 fb1a36c0 2022-01-09 op uint64_t done;
89 fb1a36c0 2022-01-09 op #define ASSERT_EMPTYBUF() assert(EVBUFFER_LENGTH(buf) == 0)
92 fb1a36c0 2022-01-09 op read_line(const char *prompt)
97 fb1a36c0 2022-01-09 op if ((line = readline(prompt)) == NULL)
99 fb1a36c0 2022-01-09 op /* XXX: trim spaces? */
100 fb1a36c0 2022-01-09 op if (*line == '\0') {
105 fb1a36c0 2022-01-09 op add_history(line);
110 fb1a36c0 2022-01-09 op tty_resized(int signo)
115 fb1a36c0 2022-01-09 op static void __dead
116 fb1a36c0 2022-01-09 op usage(int ret)
118 fb1a36c0 2022-01-09 op fprintf(stderr, "usage: %s [-c] host[:port] [path]\n",
119 fb1a36c0 2022-01-09 op getprogname());
120 fb1a36c0 2022-01-09 op fprintf(stderr, "kamid suite version " KAMID_VERSION "\n");
125 fb1a36c0 2022-01-09 op do_send(void)
127 fb1a36c0 2022-01-09 op const void *buf;
128 fb1a36c0 2022-01-09 op size_t nbytes;
131 fb1a36c0 2022-01-09 op while (EVBUFFER_LENGTH(evb) != 0) {
132 fb1a36c0 2022-01-09 op buf = EVBUFFER_DATA(evb);
133 fb1a36c0 2022-01-09 op nbytes = EVBUFFER_LENGTH(evb);
135 fb1a36c0 2022-01-09 op if (ctx == NULL) {
136 fb1a36c0 2022-01-09 op r = write(sock, buf, nbytes);
137 fb1a36c0 2022-01-09 op if (r == 0 || r == -1)
138 fb1a36c0 2022-01-09 op errx(1, "EOF");
140 fb1a36c0 2022-01-09 op r = tls_write(ctx, buf, nbytes);
141 fb1a36c0 2022-01-09 op if (r == TLS_WANT_POLLIN || r == TLS_WANT_POLLOUT)
144 fb1a36c0 2022-01-09 op errx(1, "tls: %s", tls_error(ctx));
147 fb1a36c0 2022-01-09 op evbuffer_drain(evb, r);
152 fb1a36c0 2022-01-09 op mustread(void *d, size_t len)
156 fb1a36c0 2022-01-09 op while (len != 0) {
157 fb1a36c0 2022-01-09 op if (ctx == NULL) {
158 fb1a36c0 2022-01-09 op r = read(sock, d, len);
159 fb1a36c0 2022-01-09 op if (r == 0 || r == -1)
160 fb1a36c0 2022-01-09 op errx(1, "EOF");
162 fb1a36c0 2022-01-09 op r = tls_read(ctx, d, len);
163 fb1a36c0 2022-01-09 op if (r == TLS_WANT_POLLIN || r == TLS_WANT_POLLOUT)
166 fb1a36c0 2022-01-09 op errx(1, "tls: %s", tls_error(ctx));
175 fb1a36c0 2022-01-09 op recv_msg(void)
177 fb1a36c0 2022-01-09 op uint32_t len, l;
178 fb1a36c0 2022-01-09 op char tmp[BUFSIZ];
180 fb1a36c0 2022-01-09 op mustread(&len, sizeof(len));
181 fb1a36c0 2022-01-09 op len = le32toh(len);
182 fb1a36c0 2022-01-09 op if (len < HEADERSIZE)
183 fb1a36c0 2022-01-09 op errx(1, "read message of invalid length %d", len);
185 fb1a36c0 2022-01-09 op len -= 4; /* skip the length just read */
187 fb1a36c0 2022-01-09 op while (len != 0) {
188 fb1a36c0 2022-01-09 op l = MIN(len, sizeof(tmp));
189 fb1a36c0 2022-01-09 op mustread(tmp, l);
191 fb1a36c0 2022-01-09 op evbuffer_add(buf, tmp, l);
195 fb1a36c0 2022-01-09 op static uint64_t
196 fb1a36c0 2022-01-09 op np_read64(struct evbuffer *buf)
200 fb1a36c0 2022-01-09 op evbuffer_remove(buf, &n, sizeof(n));
201 fb1a36c0 2022-01-09 op return le64toh(n);
204 fb1a36c0 2022-01-09 op static uint32_t
205 fb1a36c0 2022-01-09 op np_read32(struct evbuffer *buf)
209 fb1a36c0 2022-01-09 op evbuffer_remove(buf, &n, sizeof(n));
210 fb1a36c0 2022-01-09 op return le32toh(n);
213 fb1a36c0 2022-01-09 op static uint16_t
214 fb1a36c0 2022-01-09 op np_read16(struct evbuffer *buf)
218 fb1a36c0 2022-01-09 op evbuffer_remove(buf, &n, sizeof(n));
219 fb1a36c0 2022-01-09 op return le16toh(n);
222 fb1a36c0 2022-01-09 op static uint16_t
223 fb1a36c0 2022-01-09 op np_read8(struct evbuffer *buf)
227 fb1a36c0 2022-01-09 op evbuffer_remove(buf, &n, sizeof(n));
231 fb1a36c0 2022-01-09 op static char *
232 fb1a36c0 2022-01-09 op np_readstr(struct evbuffer *buf)
234 fb1a36c0 2022-01-09 op uint16_t len;
237 fb1a36c0 2022-01-09 op len = np_read16(buf);
238 fb1a36c0 2022-01-09 op assert(EVBUFFER_LENGTH(buf) >= len);
240 fb1a36c0 2022-01-09 op if ((str = calloc(1, len+1)) == NULL)
241 fb1a36c0 2022-01-09 op err(1, "calloc");
242 fb1a36c0 2022-01-09 op evbuffer_remove(buf, str, len);
247 fb1a36c0 2022-01-09 op np_read_qid(struct evbuffer *buf, struct qid *qid)
249 fb1a36c0 2022-01-09 op assert(EVBUFFER_LENGTH(buf) >= QIDSIZE);
251 fb1a36c0 2022-01-09 op qid->type = np_read8(buf);
252 fb1a36c0 2022-01-09 op qid->vers = np_read32(buf);
253 fb1a36c0 2022-01-09 op qid->path = np_read64(buf);
257 fb1a36c0 2022-01-09 op np_read_stat(struct evbuffer *buf, struct np_stat *st)
259 fb1a36c0 2022-01-09 op uint16_t size;
261 fb1a36c0 2022-01-09 op memset(st, 0, sizeof(*st));
263 fb1a36c0 2022-01-09 op size = np_read16(buf);
264 fb1a36c0 2022-01-09 op if (size > EVBUFFER_LENGTH(buf))
267 fb1a36c0 2022-01-09 op st->type = np_read16(buf);
268 fb1a36c0 2022-01-09 op st->dev = np_read32(buf);
269 fb1a36c0 2022-01-09 op np_read_qid(buf, &st->qid);
270 fb1a36c0 2022-01-09 op st->mode = np_read32(buf);
271 fb1a36c0 2022-01-09 op st->atime = np_read32(buf);
272 fb1a36c0 2022-01-09 op st->mtime = np_read32(buf);
273 fb1a36c0 2022-01-09 op st->length = np_read64(buf);
274 fb1a36c0 2022-01-09 op st->name = np_readstr(buf);
275 fb1a36c0 2022-01-09 op st->uid = np_readstr(buf);
276 fb1a36c0 2022-01-09 op st->gid = np_readstr(buf);
277 fb1a36c0 2022-01-09 op st->muid = np_readstr(buf);
283 fb1a36c0 2022-01-09 op expect(uint8_t type)
287 fb1a36c0 2022-01-09 op t = np_read8(buf);
288 fb1a36c0 2022-01-09 op if (t == type)
291 fb1a36c0 2022-01-09 op if (t == Rerror) {
294 fb1a36c0 2022-01-09 op /* skip tag */
295 fb1a36c0 2022-01-09 op np_read16(buf);
297 fb1a36c0 2022-01-09 op err = np_readstr(buf);
298 fb1a36c0 2022-01-09 op errx(1, "expected %s, got error %s",
299 fb1a36c0 2022-01-09 op pp_msg_type(type), err);
302 fb1a36c0 2022-01-09 op errx(1, "expected %s, got msg type %s",
303 fb1a36c0 2022-01-09 op pp_msg_type(type), pp_msg_type(t));
307 fb1a36c0 2022-01-09 op expect2(uint8_t type, uint16_t tag)
311 fb1a36c0 2022-01-09 op expect(type);
313 fb1a36c0 2022-01-09 op t = np_read16(buf);
314 fb1a36c0 2022-01-09 op if (t == tag)
317 fb1a36c0 2022-01-09 op errx(1, "expected tag 0x%x, got 0x%x", tag, t);
321 fb1a36c0 2022-01-09 op do_version(void)
323 fb1a36c0 2022-01-09 op char *version;
325 fb1a36c0 2022-01-09 op tversion(VERSION9P, MSIZE9P);
328 fb1a36c0 2022-01-09 op expect2(Rversion, NOTAG);
330 fb1a36c0 2022-01-09 op msize = np_read32(buf);
331 fb1a36c0 2022-01-09 op version = np_readstr(buf);
333 fb1a36c0 2022-01-09 op if (msize > MSIZE9P)
334 fb1a36c0 2022-01-09 op errx(1, "got unexpected msize: %d", msize);
335 fb1a36c0 2022-01-09 op if (strcmp(version, VERSION9P))
336 fb1a36c0 2022-01-09 op errx(1, "unexpected 9p version: %s", version);
338 fb1a36c0 2022-01-09 op free(version);
339 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
343 fb1a36c0 2022-01-09 op do_attach(const char *path)
345 fb1a36c0 2022-01-09 op const char *user;
346 fb1a36c0 2022-01-09 op struct qid qid;
348 fb1a36c0 2022-01-09 op if (path == NULL)
350 fb1a36c0 2022-01-09 op if ((user = getenv("USER")) == NULL)
351 fb1a36c0 2022-01-09 op user = "flan";
353 fb1a36c0 2022-01-09 op tattach(pwdfid, NOFID, user, path);
356 fb1a36c0 2022-01-09 op expect2(Rattach, iota_tag);
357 fb1a36c0 2022-01-09 op np_read_qid(buf, &qid);
359 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
362 fb1a36c0 2022-01-09 op static uint32_t
363 fb1a36c0 2022-01-09 op do_open(uint32_t fid, uint8_t mode)
365 fb1a36c0 2022-01-09 op struct qid qid;
366 fb1a36c0 2022-01-09 op uint32_t iounit;
368 fb1a36c0 2022-01-09 op topen(fid, mode);
371 fb1a36c0 2022-01-09 op expect2(Ropen, iota_tag);
373 fb1a36c0 2022-01-09 op np_read_qid(buf, &qid);
374 fb1a36c0 2022-01-09 op iounit = np_read32(buf);
376 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
378 fb1a36c0 2022-01-09 op return iounit;
382 fb1a36c0 2022-01-09 op do_clunk(uint32_t fid)
387 fb1a36c0 2022-01-09 op expect2(Rclunk, iota_tag);
389 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
393 fb1a36c0 2022-01-09 op dup_fid(int fid, int nfid)
395 fb1a36c0 2022-01-09 op uint16_t nwqid;
397 fb1a36c0 2022-01-09 op twalk(fid, nfid, NULL, 0);
400 fb1a36c0 2022-01-09 op expect2(Rwalk, iota_tag);
402 fb1a36c0 2022-01-09 op nwqid = np_read16(buf);
403 fb1a36c0 2022-01-09 op assert(nwqid == 0);
405 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
409 fb1a36c0 2022-01-09 op walk_path(int fid, int newfid, const char *path, struct qid *qid)
411 fb1a36c0 2022-01-09 op char *wnames[MAXWELEM], *p, *t;
412 fb1a36c0 2022-01-09 op size_t nwname, i;
413 fb1a36c0 2022-01-09 op uint16_t nwqid;
415 fb1a36c0 2022-01-09 op if ((p = strdup(path)) == NULL)
416 fb1a36c0 2022-01-09 op err(1, "strdup");
419 fb1a36c0 2022-01-09 op /* strip initial ./ */
420 fb1a36c0 2022-01-09 op if (t[0] == '.' && t[1] == '/')
423 fb1a36c0 2022-01-09 op for (nwname = 0; nwname < nitems(wnames) &&
424 fb1a36c0 2022-01-09 op (wnames[nwname] = strsep(&t, "/")) != NULL;) {
425 fb1a36c0 2022-01-09 op if (*wnames[nwname] != '\0')
429 fb1a36c0 2022-01-09 op twalk(fid, newfid, (const char **)wnames, nwname);
432 fb1a36c0 2022-01-09 op expect2(Rwalk, iota_tag);
434 fb1a36c0 2022-01-09 op nwqid = np_read16(buf);
435 fb1a36c0 2022-01-09 op assert(nwqid <= nwname);
437 fb1a36c0 2022-01-09 op /* consume all qids */
438 fb1a36c0 2022-01-09 op for (i = 0; i < nwname; ++i)
439 fb1a36c0 2022-01-09 op np_read_qid(buf, qid);
443 fb1a36c0 2022-01-09 op return nwqid == nwname;
447 fb1a36c0 2022-01-09 op do_stat(int fid, struct np_stat *st)
452 fb1a36c0 2022-01-09 op expect2(Rstat, iota_tag);
454 fb1a36c0 2022-01-09 op if (np_read_stat(buf, st) == -1)
455 fb1a36c0 2022-01-09 op errx(1, "invalid stat struct read");
457 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
460 fb1a36c0 2022-01-09 op static size_t
461 fb1a36c0 2022-01-09 op do_read(int fid, uint64_t off, uint32_t count, void *data)
465 fb1a36c0 2022-01-09 op tread(fid, off, count);
468 fb1a36c0 2022-01-09 op expect2(Rread, iota_tag);
470 fb1a36c0 2022-01-09 op r = np_read32(buf);
471 fb1a36c0 2022-01-09 op assert(r == EVBUFFER_LENGTH(buf));
472 fb1a36c0 2022-01-09 op assert(r <= count);
473 fb1a36c0 2022-01-09 op evbuffer_remove(buf, data, r);
475 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
481 fb1a36c0 2022-01-09 op draw_progress(const char *pre, const struct progress *p)
483 fb1a36c0 2022-01-09 op struct winsize ws;
487 fb1a36c0 2022-01-09 op perc = 100.0 * p->done / p->max;
488 fb1a36c0 2022-01-09 op if (!tty_p) {
489 fb1a36c0 2022-01-09 op fprintf(stderr, "%s: %d%%\n", pre, (int)perc);
493 fb1a36c0 2022-01-09 op if (resized) {
496 fb1a36c0 2022-01-09 op if (ioctl(0, TIOCGWINSZ, &ws) == -1)
498 fb1a36c0 2022-01-09 op tty_width = ws.ws_col;
500 fb1a36c0 2022-01-09 op w = tty_width;
502 fb1a36c0 2022-01-09 op if (pre == NULL ||
503 fb1a36c0 2022-01-09 op ((l = printf("\r%s ", pre)) == -1 || l >= w))
506 fb1a36c0 2022-01-09 op w -= l + 2 + 5; /* 2 for |, 5 for percentage + \n */
508 fb1a36c0 2022-01-09 op printf("%4d%%\n", (int)perc);
514 fb1a36c0 2022-01-09 op l = w * MIN(100.0, perc) / 100.0;
515 fb1a36c0 2022-01-09 op for (i = 0; i < l; i++)
517 fb1a36c0 2022-01-09 op for (; i < w; i++)
519 fb1a36c0 2022-01-09 op printf("|%4d%%", (int)perc);
521 fb1a36c0 2022-01-09 op fflush(stdout);
525 fb1a36c0 2022-01-09 op fetch_fid(int fid, const char *path)
527 fb1a36c0 2022-01-09 op struct progress p = {0};
528 fb1a36c0 2022-01-09 op struct np_stat st;
531 fb1a36c0 2022-01-09 op char buf[BUFSIZ];
533 fb1a36c0 2022-01-09 op do_stat(fid, &st);
534 fb1a36c0 2022-01-09 op do_open(fid, KOREAD);
536 fb1a36c0 2022-01-09 op if ((fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1) {
537 fb1a36c0 2022-01-09 op warn("can't open %s", path);
541 fb1a36c0 2022-01-09 op p.max = st.length;
543 fb1a36c0 2022-01-09 op size_t siz, off;
546 fb1a36c0 2022-01-09 op r = do_read(fid, p.done, sizeof(buf), buf);
550 fb1a36c0 2022-01-09 op siz = sizeof(buf);
551 fb1a36c0 2022-01-09 op for (off = 0; off < siz; off += nw)
552 fb1a36c0 2022-01-09 op if ((nw = write(fd, buf + off, siz - off)) == 0 ||
554 fb1a36c0 2022-01-09 op err(1, "write");
557 fb1a36c0 2022-01-09 op draw_progress(path, &p);
560 fb1a36c0 2022-01-09 op /* throttle, for debugging purpose */
562 fb1a36c0 2022-01-09 op struct timespec ts = { 0, 500000000 };
563 fb1a36c0 2022-01-09 op nanosleep(&ts, NULL);
568 fb1a36c0 2022-01-09 op putchar('\n');
571 fb1a36c0 2022-01-09 op do_clunk(fid);
576 fb1a36c0 2022-01-09 op do_tls_connect(const char *host, const char *port)
578 fb1a36c0 2022-01-09 op int handshake;
580 fb1a36c0 2022-01-09 op if ((tlsconf = tls_config_new()) == NULL)
581 fb1a36c0 2022-01-09 op fatalx("tls_config_new");
582 fb1a36c0 2022-01-09 op tls_config_insecure_noverifycert(tlsconf);
583 fb1a36c0 2022-01-09 op tls_config_insecure_noverifyname(tlsconf);
584 fb1a36c0 2022-01-09 op if (tls_config_set_keypair_file(tlsconf, crtpath, keypath) == -1)
585 fb1a36c0 2022-01-09 op fatalx("can't load certs (%s, %s)", crtpath, keypath);
587 fb1a36c0 2022-01-09 op if ((ctx = tls_client()) == NULL)
588 fb1a36c0 2022-01-09 op fatal("tls_client");
589 fb1a36c0 2022-01-09 op if (tls_configure(ctx, tlsconf) == -1)
590 fb1a36c0 2022-01-09 op fatalx("tls_configure: %s", tls_error(ctx));
592 fb1a36c0 2022-01-09 op if (tls_connect(ctx, host, port) == -1)
593 fb1a36c0 2022-01-09 op fatalx("can't connect to %s:%s: %s", host, port,
594 fb1a36c0 2022-01-09 op tls_error(ctx));
596 fb1a36c0 2022-01-09 op for (handshake = 0; !handshake;) {
597 fb1a36c0 2022-01-09 op switch (tls_handshake(ctx)) {
599 fb1a36c0 2022-01-09 op fatalx("tls_handshake: %s", tls_error(ctx));
601 fb1a36c0 2022-01-09 op handshake = 1;
608 fb1a36c0 2022-01-09 op do_ctxt_connect(const char *host, const char *port)
610 fb1a36c0 2022-01-09 op struct addrinfo hints, *res, *res0;
611 fb1a36c0 2022-01-09 op int error, saved_errno;
612 fb1a36c0 2022-01-09 op const char *cause = NULL;
614 fb1a36c0 2022-01-09 op memset(&hints, 0, sizeof(hints));
615 fb1a36c0 2022-01-09 op hints.ai_family = AF_UNSPEC;
616 fb1a36c0 2022-01-09 op hints.ai_socktype = SOCK_STREAM;
617 fb1a36c0 2022-01-09 op error = getaddrinfo(host, port, &hints, &res0);
619 fb1a36c0 2022-01-09 op errx(1, "%s", gai_strerror(error));
622 fb1a36c0 2022-01-09 op for (res = res0; res != NULL; res = res->ai_next) {
623 fb1a36c0 2022-01-09 op sock = socket(res->ai_family, res->ai_socktype,
624 fb1a36c0 2022-01-09 op res->ai_protocol);
625 fb1a36c0 2022-01-09 op if (sock == -1) {
626 fb1a36c0 2022-01-09 op cause = "socket";
630 fb1a36c0 2022-01-09 op if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
631 fb1a36c0 2022-01-09 op cause = "connect";
632 fb1a36c0 2022-01-09 op saved_errno = errno;
634 fb1a36c0 2022-01-09 op errno = saved_errno;
642 fb1a36c0 2022-01-09 op if (sock == -1)
643 fb1a36c0 2022-01-09 op err(1, "%s", cause);
644 fb1a36c0 2022-01-09 op freeaddrinfo(res0);
648 fb1a36c0 2022-01-09 op do_connect(const char *connspec, const char *path)
650 fb1a36c0 2022-01-09 op char *host, *colon;
651 fb1a36c0 2022-01-09 op const char *port;
653 fb1a36c0 2022-01-09 op host = xstrdup(connspec);
654 fb1a36c0 2022-01-09 op if ((colon = strchr(host, ':')) != NULL) {
655 fb1a36c0 2022-01-09 op *colon = '\0';
656 fb1a36c0 2022-01-09 op port = ++colon;
658 fb1a36c0 2022-01-09 op port = "1337";
660 fb1a36c0 2022-01-09 op printf("connecting to %s:%s...", host, port);
661 fb1a36c0 2022-01-09 op fflush(stdout);
664 fb1a36c0 2022-01-09 op do_tls_connect(host, port);
666 fb1a36c0 2022-01-09 op do_ctxt_connect(host, port);
668 fb1a36c0 2022-01-09 op printf(" done!\n");
670 fb1a36c0 2022-01-09 op do_version();
671 fb1a36c0 2022-01-09 op do_attach(path);
677 fb1a36c0 2022-01-09 op cmd_bell(int argc, const char **argv)
679 fb1a36c0 2022-01-09 op if (argc == 0) {
680 fb1a36c0 2022-01-09 op bell = !bell;
682 fb1a36c0 2022-01-09 op puts("bell mode enabled");
684 fb1a36c0 2022-01-09 op puts("bell mode disabled");
688 fb1a36c0 2022-01-09 op if (argc != 1)
691 fb1a36c0 2022-01-09 op if (!strcmp(*argv, "on")) {
693 fb1a36c0 2022-01-09 op puts("bell mode enabled");
697 fb1a36c0 2022-01-09 op if (!strcmp(*argv, "off")) {
699 fb1a36c0 2022-01-09 op puts("bell mode disabled");
704 fb1a36c0 2022-01-09 op printf("bell [on | off]\n");
708 fb1a36c0 2022-01-09 op cmd_bye(int argc, const char **argv)
710 fb1a36c0 2022-01-09 op log_warnx("bye\n");
715 fb1a36c0 2022-01-09 op cmd_cd(int argc, const char **argv)
717 fb1a36c0 2022-01-09 op struct qid qid;
720 fb1a36c0 2022-01-09 op if (argc != 1) {
721 fb1a36c0 2022-01-09 op printf("usage: cd remote-path\n");
725 fb1a36c0 2022-01-09 op nfid = pwdfid+1;
726 fb1a36c0 2022-01-09 op if (walk_path(pwdfid, nfid, argv[0], &qid) == -1 ||
727 fb1a36c0 2022-01-09 op !(qid.type & QTDIR)) {
728 fb1a36c0 2022-01-09 op printf("can't cd %s\n", argv[0]);
729 fb1a36c0 2022-01-09 op do_clunk(nfid);
731 fb1a36c0 2022-01-09 op do_clunk(pwdfid);
732 fb1a36c0 2022-01-09 op pwdfid = nfid;
737 fb1a36c0 2022-01-09 op cmd_get(int argc, const char **argv)
739 fb1a36c0 2022-01-09 op struct qid qid;
740 fb1a36c0 2022-01-09 op const char *l;
743 fb1a36c0 2022-01-09 op if (argc != 1 && argc != 2) {
744 fb1a36c0 2022-01-09 op printf("usage: get remote-file [local-file]\n");
748 fb1a36c0 2022-01-09 op if (argc == 2)
750 fb1a36c0 2022-01-09 op else if ((l = strrchr(argv[0], '/')) != NULL)
751 fb1a36c0 2022-01-09 op l++; /* skip / */
755 fb1a36c0 2022-01-09 op nfid = pwdfid+1;
756 fb1a36c0 2022-01-09 op if (walk_path(pwdfid, nfid, argv[0], &qid) == -1) {
757 fb1a36c0 2022-01-09 op printf("can't fetch %s\n", argv[0]);
761 fb1a36c0 2022-01-09 op if (qid.type != 0) {
762 fb1a36c0 2022-01-09 op printf("can't fetch %s\n", argv[0]);
763 fb1a36c0 2022-01-09 op do_clunk(nfid);
767 fb1a36c0 2022-01-09 op fetch_fid(nfid, l);
771 fb1a36c0 2022-01-09 op cmd_lcd(int argc, const char **argv)
773 fb1a36c0 2022-01-09 op const char *dir;
775 fb1a36c0 2022-01-09 op if (argc > 1) {
776 fb1a36c0 2022-01-09 op printf("lcd takes only one argument\n");
780 fb1a36c0 2022-01-09 op if (argc == 1)
783 fb1a36c0 2022-01-09 op if (argc == 0 && (dir = getenv("HOME")) == NULL) {
784 fb1a36c0 2022-01-09 op printf("HOME is not defined\n");
788 fb1a36c0 2022-01-09 op if (chdir(dir) == -1)
789 fb1a36c0 2022-01-09 op printf("cd: %s: %s\n", dir, strerror(errno));
793 fb1a36c0 2022-01-09 op cmd_lpwd(int argc, const char **argv)
795 fb1a36c0 2022-01-09 op char path[PATH_MAX];
797 fb1a36c0 2022-01-09 op if (getcwd(path, sizeof(path)) == NULL) {
798 fb1a36c0 2022-01-09 op printf("lpwd: %s\n", strerror(errno));
802 fb1a36c0 2022-01-09 op printf("%s\n", path);
806 fb1a36c0 2022-01-09 op cmd_ls(int argc, const char **argv)
808 fb1a36c0 2022-01-09 op struct np_stat st;
809 fb1a36c0 2022-01-09 op uint64_t off = 0;
810 fb1a36c0 2022-01-09 op uint32_t len;
811 fb1a36c0 2022-01-09 op char fmt[FMT_SCALED_STRSIZE];
813 fb1a36c0 2022-01-09 op if (argc != 0) {
814 fb1a36c0 2022-01-09 op printf("ls don't take arguments (yet)\n");
818 fb1a36c0 2022-01-09 op dup_fid(pwdfid, 1);
819 fb1a36c0 2022-01-09 op do_open(1, KOREAD);
821 fb1a36c0 2022-01-09 op evbuffer_drain(dirbuf, EVBUFFER_LENGTH(dirbuf));
824 fb1a36c0 2022-01-09 op tread(1, off, BUFSIZ);
827 fb1a36c0 2022-01-09 op expect2(Rread, iota_tag);
829 fb1a36c0 2022-01-09 op len = np_read32(buf);
830 fb1a36c0 2022-01-09 op if (len == 0)
833 fb1a36c0 2022-01-09 op evbuffer_add_buffer(dirbuf, buf);
836 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
839 fb1a36c0 2022-01-09 op while (EVBUFFER_LENGTH(dirbuf) != 0) {
840 fb1a36c0 2022-01-09 op if (np_read_stat(dirbuf, &st) == -1)
841 fb1a36c0 2022-01-09 op errx(1, "invalid stat struct read");
843 fb1a36c0 2022-01-09 op if (fmt_scaled(st.length, fmt) == -1)
844 fb1a36c0 2022-01-09 op strlcpy(fmt, "xxx", sizeof(fmt));
846 fb1a36c0 2022-01-09 op printf("%4s %8s %s\n", pp_qid_type(st.qid.type), fmt, st.name);
848 fb1a36c0 2022-01-09 op free(st.name);
849 fb1a36c0 2022-01-09 op free(st.uid);
850 fb1a36c0 2022-01-09 op free(st.gid);
851 fb1a36c0 2022-01-09 op free(st.muid);
858 fb1a36c0 2022-01-09 op cmd_verbose(int argc, const char **argv)
860 fb1a36c0 2022-01-09 op if (argc == 0) {
861 fb1a36c0 2022-01-09 op log_setverbose(!log_getverbose());
862 fb1a36c0 2022-01-09 op if (log_getverbose())
863 fb1a36c0 2022-01-09 op puts("verbose mode enabled");
865 fb1a36c0 2022-01-09 op puts("verbose mode disabled");
869 fb1a36c0 2022-01-09 op if (argc != 1)
872 fb1a36c0 2022-01-09 op if (!strcmp(*argv, "on")) {
873 fb1a36c0 2022-01-09 op log_setverbose(1);
874 fb1a36c0 2022-01-09 op puts("verbose mode enabled");
878 fb1a36c0 2022-01-09 op if (!strcmp(*argv, "off")) {
879 fb1a36c0 2022-01-09 op log_setverbose(0);
880 fb1a36c0 2022-01-09 op puts("verbose mode disabled");
885 fb1a36c0 2022-01-09 op printf("verbose [on | off]\n");
889 fb1a36c0 2022-01-09 op excmd(int argc, const char **argv)
892 fb1a36c0 2022-01-09 op const char *name;
893 fb1a36c0 2022-01-09 op void (*fn)(int, const char **);
895 fb1a36c0 2022-01-09 op {"bell", cmd_bell},
896 fb1a36c0 2022-01-09 op {"bye", cmd_bye},
897 fb1a36c0 2022-01-09 op {"cd", cmd_cd},
898 fb1a36c0 2022-01-09 op {"get", cmd_get},
899 fb1a36c0 2022-01-09 op {"lcd", cmd_lcd},
900 fb1a36c0 2022-01-09 op {"lpwd", cmd_lpwd},
901 fb1a36c0 2022-01-09 op {"ls", cmd_ls},
902 fb1a36c0 2022-01-09 op {"quit", cmd_bye},
903 fb1a36c0 2022-01-09 op {"verbose", cmd_verbose},
907 fb1a36c0 2022-01-09 op if (argc == 0)
909 fb1a36c0 2022-01-09 op for (i = 0; i < nitems(cmds); ++i) {
910 fb1a36c0 2022-01-09 op if (!strcmp(cmds[i].name, *argv)) {
911 fb1a36c0 2022-01-09 op cmds[i].fn(argc-1, argv+1);
916 fb1a36c0 2022-01-09 op log_warnx("unknown command %s", *argv);
920 fb1a36c0 2022-01-09 op main(int argc, char **argv)
924 fb1a36c0 2022-01-09 op log_init(1, LOG_DAEMON);
925 fb1a36c0 2022-01-09 op log_setverbose(0);
926 fb1a36c0 2022-01-09 op log_procinit(getprogname());
928 fb1a36c0 2022-01-09 op while ((ch = getopt(argc, argv, "C:cK:")) != -1) {
929 fb1a36c0 2022-01-09 op switch (ch) {
931 fb1a36c0 2022-01-09 op crtpath = optarg;
937 fb1a36c0 2022-01-09 op keypath = optarg;
943 fb1a36c0 2022-01-09 op argc -= optind;
944 fb1a36c0 2022-01-09 op argv += optind;
946 fb1a36c0 2022-01-09 op if (argc == 0)
949 fb1a36c0 2022-01-09 op if (isatty(1)) {
952 fb1a36c0 2022-01-09 op signal(SIGWINCH, tty_resized);
955 fb1a36c0 2022-01-09 op if ((evb = evbuffer_new()) == NULL)
956 fb1a36c0 2022-01-09 op fatal("evbuffer_new");
958 fb1a36c0 2022-01-09 op if ((buf = evbuffer_new()) == NULL)
959 fb1a36c0 2022-01-09 op fatal("evbuffer_new");
961 fb1a36c0 2022-01-09 op if ((dirbuf = evbuffer_new()) == NULL)
962 fb1a36c0 2022-01-09 op fatal("evbuferr_new");
964 fb1a36c0 2022-01-09 op do_connect(argv[0], argv[1]);
967 fb1a36c0 2022-01-09 op int argc = 0;
968 fb1a36c0 2022-01-09 op char *line, *argv[16] = {0}, **ap;
970 fb1a36c0 2022-01-09 op if ((line = read_line("kamiftp> ")) == NULL)
973 fb1a36c0 2022-01-09 op for (argc = 0, ap = argv; ap < &argv[15] &&
974 fb1a36c0 2022-01-09 op (*ap = strsep(&line, " \t")) != NULL;) {
975 fb1a36c0 2022-01-09 op if (**ap != '\0')
976 fb1a36c0 2022-01-09 op ap++, argc++;
978 fb1a36c0 2022-01-09 op excmd(argc, (const char **)argv);
981 fb1a36c0 2022-01-09 op printf("\a");
982 fb1a36c0 2022-01-09 op fflush(stdout);
988 fb1a36c0 2022-01-09 op printf("\n");