2 8d1b399b 2021-07-22 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 8d1b399b 2021-07-22 op * Permission to use, copy, modify, and distribute this software for any
5 8d1b399b 2021-07-22 op * purpose with or without fee is hereby granted, provided that the above
6 8d1b399b 2021-07-22 op * copyright notice and this permission notice appear in all copies.
8 8d1b399b 2021-07-22 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 8d1b399b 2021-07-22 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 8d1b399b 2021-07-22 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 8d1b399b 2021-07-22 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 8d1b399b 2021-07-22 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 8d1b399b 2021-07-22 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 8d1b399b 2021-07-22 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 8d1b399b 2021-07-22 op #include "compat.h"
19 c5d518da 2021-07-29 op #include <sys/stat.h>
21 23e03c88 2021-07-26 op #include <endian.h>
22 8d1b399b 2021-07-22 op #include <errno.h>
23 c5d518da 2021-07-29 op #include <fcntl.h>
24 8d1b399b 2021-07-22 op #include <pwd.h>
25 8d1b399b 2021-07-22 op #include <signal.h>
26 8d1b399b 2021-07-22 op #include <stdlib.h>
27 8d1b399b 2021-07-22 op #include <string.h>
28 8d1b399b 2021-07-22 op #include <syslog.h>
29 8d1b399b 2021-07-22 op #include <unistd.h>
31 8d1b399b 2021-07-22 op #include "client.h"
32 8d1b399b 2021-07-22 op #include "kamid.h"
33 8d1b399b 2021-07-22 op #include "log.h"
34 8d1b399b 2021-07-22 op #include "sandbox.h"
35 23e03c88 2021-07-26 op #include "utils.h"
37 2adb951f 2021-08-01 op #define DEBUG_PACKETS 0
39 c5d518da 2021-07-29 op STAILQ_HEAD(qidhead, qid) qids;
41 c5d518da 2021-07-29 op /* definition of a qid */
42 c5d518da 2021-07-29 op uint64_t path;
43 c5d518da 2021-07-29 op uint32_t vers;
49 deb971f5 2021-08-12 op char fpath[PATH_MAX+1];
51 c5d518da 2021-07-29 op STAILQ_ENTRY(qid) entries;
54 c5d518da 2021-07-29 op STAILQ_HEAD(fidhead, fid) fids;
59 29f1f582 2021-12-13 op * 0 when the fid was not yet opened for I/O otherwise set to
60 021481ca 2021-12-13 op * the flags passed to open(2). O_CLOEXEC means ORCLOSE, that
61 021481ca 2021-12-13 op * is to unlink the file upon Tclunk.
66 29f1f582 2021-12-13 op * if iomode is set, this fid was opened and fd represents its
67 29f1f582 2021-12-13 op * file descriptor.
71 c5d518da 2021-07-29 op struct qid *qid;
72 c5d518da 2021-07-29 op STAILQ_ENTRY(fid) entries;
75 8d1b399b 2021-07-22 op static struct imsgev *iev_listener;
76 2ef72ade 2021-07-28 op static struct evbuffer *evb;
77 2ef72ade 2021-07-28 op static uint32_t peerid;
79 5c420a81 2021-07-30 op static int handshaked, attached;
80 5c485996 2021-07-28 op uint32_t msize;
82 06a84967 2021-07-22 op static ATTR_DEAD void client_shutdown(void);
83 8d1b399b 2021-07-22 op static void client_sig_handler(int, short, void *);
84 8d1b399b 2021-07-22 op static void client_dispatch_listener(int, short, void *);
85 8d1b399b 2021-07-22 op static void client_privdrop(const char *, const char *);
87 2ef72ade 2021-07-28 op static int client_send_listener(int, const void *, uint16_t);
89 deb971f5 2021-08-12 op static void qid_update_from_sb(struct qid *, struct stat *);
90 deb971f5 2021-08-12 op static struct qid *qid_from_fd(int, const char *, struct stat *);
91 c377c1b9 2021-07-30 op static struct qid *qid_incref(struct qid *);
92 c377c1b9 2021-07-30 op static void qid_decref(struct qid *);
94 c5d518da 2021-07-29 op static struct fid *new_fid(struct qid *, uint32_t);
95 c377c1b9 2021-07-30 op static struct fid *fid_by_id(uint32_t);
96 c377c1b9 2021-07-30 op static void free_fid(struct fid *);
98 48192874 2021-08-01 op static void parse_message(const uint8_t *, size_t,
99 48192874 2021-08-01 op struct np_msg_header *, uint8_t **);
101 83f6b305 2021-08-01 op static void np_write16(uint16_t);
102 021481ca 2021-12-13 op static void np_write32(uint32_t);
103 2845cccb 2021-07-29 op static void np_header(uint32_t, uint8_t, uint16_t);
104 2845cccb 2021-07-29 op static void np_string(uint16_t, const char *);
105 c5d518da 2021-07-29 op static void np_qid(struct qid *);
106 84b10f04 2021-07-29 op static void do_send(void);
108 2845cccb 2021-07-29 op static void np_version(uint16_t, uint32_t, const char *);
109 c5d518da 2021-07-29 op static void np_attach(uint16_t, struct qid *);
110 c377c1b9 2021-07-30 op static void np_clunk(uint16_t);
111 36b30273 2021-07-30 op static void np_flush(uint16_t);
112 1c08fc54 2021-08-01 op static void np_walk(uint16_t, int, struct qid *);
113 021481ca 2021-12-13 op static void np_open(uint16_t, struct qid *, uint32_t);
114 2ef72ade 2021-07-28 op static void np_error(uint16_t, const char *);
115 c5d518da 2021-07-29 op static void np_errno(uint16_t);
117 48192874 2021-08-01 op static int np_read8(const char *, const char *, uint8_t *,
118 48192874 2021-08-01 op const uint8_t **, size_t *);
119 48192874 2021-08-01 op static int np_read16(const char *, const char *, uint16_t *,
120 48192874 2021-08-01 op const uint8_t **, size_t *);
121 48192874 2021-08-01 op static int np_read32(const char *, const char *, uint32_t *,
122 48192874 2021-08-01 op const uint8_t **, size_t *);
124 48192874 2021-08-01 op #define READSTRERR -1
125 48192874 2021-08-01 op #define READSTRTRUNC -2
126 48192874 2021-08-01 op static int np_readstr(const char *, const char *, char *, size_t,
127 48192874 2021-08-01 op const uint8_t **, size_t *);
129 48192874 2021-08-01 op #define NPREAD8(f, dst, src, len) np_read8(__func__, f, dst, src, len)
130 48192874 2021-08-01 op #define NPREAD16(f, dst, src, len) np_read16(__func__, f, dst, src, len)
131 48192874 2021-08-01 op #define NPREAD32(f, dst, src, len) np_read32(__func__, f, dst, src, len)
133 48192874 2021-08-01 op #define NPREADSTR(f, b, bl, src, len) np_readstr(__func__, f, b, bl, src, len)
135 e60f4e08 2021-07-30 op static void tversion(struct np_msg_header *, const uint8_t *, size_t);
136 e60f4e08 2021-07-30 op static void tattach(struct np_msg_header *, const uint8_t *, size_t);
137 c377c1b9 2021-07-30 op static void tclunk(struct np_msg_header *, const uint8_t *, size_t);
138 36b30273 2021-07-30 op static void tflush(struct np_msg_header *, const uint8_t *, size_t);
139 1c08fc54 2021-08-01 op static void twalk(struct np_msg_header *, const uint8_t *, size_t);
140 021481ca 2021-12-13 op static void topen(struct np_msg_header *, const uint8_t *, size_t);
141 e60f4e08 2021-07-30 op static void handle_message(struct imsg *, size_t);
143 06a84967 2021-07-22 op ATTR_DEAD void
144 8d1b399b 2021-07-22 op client(int debug, int verbose)
146 8d1b399b 2021-07-22 op struct event ev_sigint, ev_sigterm;
148 8d1b399b 2021-07-22 op log_init(debug, LOG_DAEMON);
149 8d1b399b 2021-07-22 op log_setverbose(verbose);
151 8d1b399b 2021-07-22 op setproctitle("client");
152 8d1b399b 2021-07-22 op log_procinit("client");
154 8d1b399b 2021-07-22 op log_debug("warming up");
156 8d1b399b 2021-07-22 op event_init();
158 8d1b399b 2021-07-22 op /* Setup signal handlers */
159 8d1b399b 2021-07-22 op signal_set(&ev_sigint, SIGINT, client_sig_handler, NULL);
160 8d1b399b 2021-07-22 op signal_set(&ev_sigterm, SIGTERM, client_sig_handler, NULL);
162 8d1b399b 2021-07-22 op signal_add(&ev_sigint, NULL);
163 8d1b399b 2021-07-22 op signal_add(&ev_sigterm, NULL);
165 8d1b399b 2021-07-22 op signal(SIGPIPE, SIG_IGN);
166 8d1b399b 2021-07-22 op signal(SIGHUP, SIG_IGN);
168 8d1b399b 2021-07-22 op /* Setup pipe and event handler to the listener process */
169 8d1b399b 2021-07-22 op if ((iev_listener = malloc(sizeof(*iev_listener))) == NULL)
172 8d1b399b 2021-07-22 op imsg_init(&iev_listener->ibuf, 3);
173 8d1b399b 2021-07-22 op iev_listener->handler = client_dispatch_listener;
175 8d1b399b 2021-07-22 op /* Setup event handlers. */
176 8d1b399b 2021-07-22 op iev_listener->events = EV_READ;
177 8d1b399b 2021-07-22 op event_set(&iev_listener->ev, iev_listener->ibuf.fd,
178 8d1b399b 2021-07-22 op iev_listener->events, iev_listener->handler, iev_listener);
179 8d1b399b 2021-07-22 op event_add(&iev_listener->ev, NULL);
181 8d1b399b 2021-07-22 op event_dispatch();
182 8d1b399b 2021-07-22 op client_shutdown();
185 06a84967 2021-07-22 op static ATTR_DEAD void
186 8d1b399b 2021-07-22 op client_shutdown(void)
188 2ef72ade 2021-07-28 op if (evb != NULL)
189 2ef72ade 2021-07-28 op evbuffer_free(evb);
191 8d1b399b 2021-07-22 op msgbuf_clear(&iev_listener->ibuf.w);
192 8d1b399b 2021-07-22 op close(iev_listener->ibuf.fd);
194 8d1b399b 2021-07-22 op free(iev_listener);
196 223c9e73 2021-08-06 op log_debug("client exiting");
201 8d1b399b 2021-07-22 op client_sig_handler(int sig, short event, void *d)
204 8d1b399b 2021-07-22 op * Normal signal handler rules don't apply because libevent
205 8d1b399b 2021-07-22 op * decouples for us.
208 8d1b399b 2021-07-22 op switch (sig) {
210 8d1b399b 2021-07-22 op case SIGTERM:
211 8d1b399b 2021-07-22 op client_shutdown();
213 8d1b399b 2021-07-22 op fatalx("unexpected signal %d", sig);
217 8d1b399b 2021-07-22 op #define AUTH_NONE 0
218 8d1b399b 2021-07-22 op #define AUTH_USER 1
219 8d1b399b 2021-07-22 op #define AUTH_DONE 2
222 8d1b399b 2021-07-22 op client_dispatch_listener(int fd, short event, void *d)
224 8d1b399b 2021-07-22 op static int auth = AUTH_NONE;
225 8d1b399b 2021-07-22 op static char username[64] = {0};
226 8d1b399b 2021-07-22 op static char dir[PATH_MAX] = {0};
227 8d1b399b 2021-07-22 op struct imsg imsg;
228 8d1b399b 2021-07-22 op struct imsgev *iev = d;
229 8d1b399b 2021-07-22 op struct imsgbuf *ibuf;
231 8d1b399b 2021-07-22 op int shut = 0;
233 8d1b399b 2021-07-22 op ibuf = &iev->ibuf;
235 8d1b399b 2021-07-22 op if (event & EV_READ) {
236 8d1b399b 2021-07-22 op if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
237 8d1b399b 2021-07-22 op fatal("imsg_read error");
238 8d1b399b 2021-07-22 op if (n == 0) /* Connection closed */
241 8d1b399b 2021-07-22 op if (event & EV_WRITE) {
242 8d1b399b 2021-07-22 op if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
243 8d1b399b 2021-07-22 op fatal("msgbuf_write");
244 8d1b399b 2021-07-22 op if (n == 0) /* Connection closed */
249 8d1b399b 2021-07-22 op if ((n = imsg_get(ibuf, &imsg)) == -1)
250 8d1b399b 2021-07-22 op fatal("%s: imsg_get error", __func__);
251 8d1b399b 2021-07-22 op if (n == 0) /* No more messages. */
254 8d1b399b 2021-07-22 op switch (imsg.hdr.type) {
255 8d1b399b 2021-07-22 op case IMSG_AUTH:
256 2ef72ade 2021-07-28 op peerid = imsg.hdr.peerid;
258 8d1b399b 2021-07-22 op fatalx("%s: IMSG_AUTH already done", __func__);
259 8d1b399b 2021-07-22 op auth = AUTH_USER;
260 8d1b399b 2021-07-22 op ((char *)imsg.data)[IMSG_DATA_SIZE(imsg)-1] = '\0';
261 8d1b399b 2021-07-22 op strlcpy(username, imsg.data, sizeof(username));
263 8d1b399b 2021-07-22 op case IMSG_AUTH_DIR:
264 8d1b399b 2021-07-22 op if (auth != AUTH_USER)
265 8d1b399b 2021-07-22 op fatalx("%s: IMSG_AUTH_DIR not after IMSG_AUTH",
267 8d1b399b 2021-07-22 op auth = AUTH_DONE;
268 8d1b399b 2021-07-22 op ((char *)imsg.data)[IMSG_DATA_SIZE(imsg)-1] = '\0';
269 8d1b399b 2021-07-22 op strlcpy(dir, imsg.data, sizeof(dir));
270 8d1b399b 2021-07-22 op client_privdrop(username, dir);
271 8d1b399b 2021-07-22 op memset(username, 0, sizeof(username));
272 8d1b399b 2021-07-22 op memset(dir, 0, sizeof(username));
274 8d1b399b 2021-07-22 op case IMSG_BUF:
277 23e03c88 2021-07-26 op fatalx("%s: can't handle messages before"
278 23e03c88 2021-07-26 op " doing the auth", __func__);
279 23e03c88 2021-07-26 op handle_message(&imsg, IMSG_DATA_SIZE(imsg));
281 8d1b399b 2021-07-22 op case IMSG_CONN_GONE:
282 8d1b399b 2021-07-22 op log_debug("closing");
286 8d1b399b 2021-07-22 op log_debug("%s: unexpected imsg %d",
287 8d1b399b 2021-07-22 op __func__, imsg.hdr.type);
290 8d1b399b 2021-07-22 op imsg_free(&imsg);
294 8d1b399b 2021-07-22 op imsg_event_add(iev);
296 8d1b399b 2021-07-22 op /* This pipe is dead. Remove its event handler. */
297 8d1b399b 2021-07-22 op event_del(&iev->ev);
298 223c9e73 2021-08-06 op log_debug("pipe closed, shutting down...");
299 8d1b399b 2021-07-22 op event_loopexit(NULL);
304 8d1b399b 2021-07-22 op client_privdrop(const char *username, const char *dir)
306 8d1b399b 2021-07-22 op struct passwd *pw;
308 8d1b399b 2021-07-22 op setproctitle("client %s", username);
310 8d1b399b 2021-07-22 op if ((pw = getpwnam(username)) == NULL)
311 8d1b399b 2021-07-22 op fatalx("getpwnam(%s) failed", username);
313 8d1b399b 2021-07-22 op if (chroot(dir) == -1)
314 8d1b399b 2021-07-22 op fatal("chroot");
315 8d1b399b 2021-07-22 op if (chdir("/") == -1)
316 8d1b399b 2021-07-22 op fatal("chdir(\"/\")");
318 8d1b399b 2021-07-22 op if (setgroups(1, &pw->pw_gid) ||
319 8d1b399b 2021-07-22 op setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
320 8d1b399b 2021-07-22 op setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
321 8d1b399b 2021-07-22 op fatal("can't drop privileges");
323 8d1b399b 2021-07-22 op sandbox_client();
324 66a9a40a 2021-07-28 op log_debug("client ready; user=%s dir=%s", username, dir);
326 2ef72ade 2021-07-28 op if ((evb = evbuffer_new()) == NULL)
327 2ef72ade 2021-07-28 op fatal("evbuffer_new");
331 2ef72ade 2021-07-28 op client_send_listener(int type, const void *data, uint16_t len)
335 8d1b399b 2021-07-22 op if ((ret = imsg_compose(&iev_listener->ibuf, type, peerid, 0, -1,
336 8d1b399b 2021-07-22 op data, len)) != -1)
337 8d1b399b 2021-07-22 op imsg_event_add(iev_listener);
342 deb971f5 2021-08-12 op /* set qid fields from sb */
344 deb971f5 2021-08-12 op qid_update_from_sb(struct qid *qid, struct stat *sb)
346 deb971f5 2021-08-12 op qid->path = sb->st_ino;
349 1c08fc54 2021-08-01 op * Theoretically (and hopefully!) this should be a 64 bit
350 1c08fc54 2021-08-01 op * number. Unfortunately, 9P uses 32 bit timestamps.
352 deb971f5 2021-08-12 op qid->vers = sb->st_mtim.tv_sec;
354 deb971f5 2021-08-12 op if (S_ISREG(sb->st_mode))
355 1c08fc54 2021-08-01 op qid->type = QTFILE;
356 deb971f5 2021-08-12 op else if (S_ISDIR(sb->st_mode))
357 1c08fc54 2021-08-01 op qid->type = QTDIR;
358 deb971f5 2021-08-12 op else if (S_ISLNK(sb->st_mode))
359 1c08fc54 2021-08-01 op qid->type = QTSYMLINK;
362 c5d518da 2021-07-29 op /* creates a qid given a fd */
363 c5d518da 2021-07-29 op static struct qid *
364 deb971f5 2021-08-12 op qid_from_fd(int fd, const char *path, struct stat *s)
366 c5d518da 2021-07-29 op struct qid *qid;
367 deb971f5 2021-08-12 op struct stat sb;
370 c5d518da 2021-07-29 op if ((qid = calloc(1, sizeof(*qid))) == NULL)
373 deb971f5 2021-08-12 op if (path != NULL)
374 deb971f5 2021-08-12 op strlcpy(qid->fpath, path, sizeof(qid->fpath));
376 c5d518da 2021-07-29 op qid->fd = fd;
378 deb971f5 2021-08-12 op if (s == NULL) {
380 deb971f5 2021-08-12 op if (path == NULL)
381 deb971f5 2021-08-12 op r = fstat(fd, s);
383 deb971f5 2021-08-12 op r = fstatat(fd, path, s, 0);
384 deb971f5 2021-08-12 op if (r == -1) {
390 deb971f5 2021-08-12 op qid_update_from_sb(qid, s);
392 053e2652 2021-08-01 op STAILQ_INSERT_HEAD(&qids, qid, entries);
397 053e2652 2021-08-01 op static struct qid *
398 c377c1b9 2021-07-30 op qid_incref(struct qid *qid)
400 c377c1b9 2021-07-30 op qid->refcount++;
405 c377c1b9 2021-07-30 op qid_decref(struct qid *qid)
407 c377c1b9 2021-07-30 op if (--qid->refcount > 0)
410 c377c1b9 2021-07-30 op STAILQ_REMOVE(&qids, qid, qid, entries);
412 c377c1b9 2021-07-30 op close(qid->fd);
415 c377c1b9 2021-07-30 op if (STAILQ_EMPTY(&qids))
416 c377c1b9 2021-07-30 op attached = 0;
419 c5d518da 2021-07-29 op static struct fid *
420 c5d518da 2021-07-29 op new_fid(struct qid *qid, uint32_t fid)
422 c5d518da 2021-07-29 op struct fid *f;
424 c5d518da 2021-07-29 op if ((f = calloc(1, sizeof(*f))) == NULL)
427 c377c1b9 2021-07-30 op f->qid = qid_incref(qid);
428 c5d518da 2021-07-29 op f->fid = fid;
431 c5d518da 2021-07-29 op STAILQ_INSERT_HEAD(&fids, f, entries);
436 c377c1b9 2021-07-30 op static struct fid *
437 c377c1b9 2021-07-30 op fid_by_id(uint32_t fid)
439 c377c1b9 2021-07-30 op struct fid *f;
441 c377c1b9 2021-07-30 op STAILQ_FOREACH(f, &fids, entries) {
442 c377c1b9 2021-07-30 op if (f->fid == fid)
450 c377c1b9 2021-07-30 op free_fid(struct fid *f)
452 021481ca 2021-12-13 op if (f->fd != -1) {
453 021481ca 2021-12-13 op close(f->fd);
454 021481ca 2021-12-13 op /* try to honour ORCLOSE if requested */
455 021481ca 2021-12-13 op if (f->iomode & O_CLOEXEC)
456 021481ca 2021-12-13 op unlinkat(f->qid->fd, f->qid->fpath, 0);
459 c377c1b9 2021-07-30 op qid_decref(f->qid);
461 c377c1b9 2021-07-30 op STAILQ_REMOVE(&fids, f, fid, entries);
466 48192874 2021-08-01 op parse_message(const uint8_t *data, size_t len, struct np_msg_header *hdr,
467 5c485996 2021-07-28 op uint8_t **cnt)
469 48192874 2021-08-01 op size_t olen = len;
471 48192874 2021-08-01 op if (!NPREAD32("len", &hdr->len, &data, &len) ||
472 48192874 2021-08-01 op !NPREAD8("type", &hdr->type, &data, &len) ||
473 48192874 2021-08-01 op !NPREAD16("tag", &hdr->tag, &data, &len))
476 48192874 2021-08-01 op if (olen != hdr->len)
479 23e03c88 2021-07-26 op if (hdr->type < Tversion ||
480 23e03c88 2021-07-26 op hdr->type >= Tmax ||
481 23e03c88 2021-07-26 op hdr->type == Terror ||
482 23e03c88 2021-07-26 op (hdr->type & 0x1) != 0) /* cannot recv a R* */
485 23e03c88 2021-07-26 op hdr->tag = le32toh(hdr->tag);
487 48192874 2021-08-01 op *cnt = (uint8_t *)data;
491 23e03c88 2021-07-26 op /* TODO: send a proper message to terminate the connection. */
492 23e03c88 2021-07-26 op fatalx("got invalid message");
496 83f6b305 2021-08-01 op np_write16(uint16_t x)
498 83f6b305 2021-08-01 op x = htole16(x);
499 83f6b305 2021-08-01 op evbuffer_add(evb, &x, sizeof(x));
503 021481ca 2021-12-13 op np_write32(uint32_t x)
505 021481ca 2021-12-13 op x = htole32(x);
506 021481ca 2021-12-13 op evbuffer_add(evb, &x, sizeof(x));
510 2ef72ade 2021-07-28 op np_header(uint32_t len, uint8_t type, uint16_t tag)
512 9ebb95a7 2021-07-30 op len += HEADERSIZE;
514 63f681aa 2021-07-28 op len = htole32(len);
515 63f681aa 2021-07-28 op tag = htole16(tag);
517 2ef72ade 2021-07-28 op evbuffer_add(evb, &len, sizeof(len));
518 2ef72ade 2021-07-28 op evbuffer_add(evb, &type, sizeof(type));
519 2ef72ade 2021-07-28 op evbuffer_add(evb, &tag, sizeof(tag));
523 5c485996 2021-07-28 op np_string(uint16_t len, const char *str)
525 5c485996 2021-07-28 op uint16_t l = len;
527 5c485996 2021-07-28 op len = htole16(len);
528 5c485996 2021-07-28 op evbuffer_add(evb, &len, sizeof(len));
529 5c485996 2021-07-28 op evbuffer_add(evb, str, l);
533 c5d518da 2021-07-29 op np_qid(struct qid *qid)
535 c5d518da 2021-07-29 op uint64_t path;
536 c5d518da 2021-07-29 op uint32_t vers;
538 c5d518da 2021-07-29 op path = htole64(qid->path);
539 c5d518da 2021-07-29 op vers = htole32(qid->vers);
541 c5d518da 2021-07-29 op evbuffer_add(evb, &qid->type, sizeof(qid->type));
542 8d448d55 2021-07-30 op evbuffer_add(evb, &vers, sizeof(vers));
543 8d448d55 2021-07-30 op evbuffer_add(evb, &path, sizeof(path));
547 2ef72ade 2021-07-28 op do_send(void)
552 2ef72ade 2021-07-28 op len = EVBUFFER_LENGTH(evb);
553 64c19d90 2021-07-30 op data = EVBUFFER_DATA(evb);
555 64c19d90 2021-07-30 op #if DEBUG_PACKETS
556 78b94752 2021-08-01 op hexdump("outgoing packet", data, len);
558 64c19d90 2021-07-30 op client_send_listener(IMSG_BUF, data, len);
559 2ef72ade 2021-07-28 op evbuffer_drain(evb, len);
563 5c485996 2021-07-28 op np_version(uint16_t tag, uint32_t msize, const char *version)
567 5c485996 2021-07-28 op l = strlen(version);
569 5c485996 2021-07-28 op msize = htole32(msize);
571 9ebb95a7 2021-07-30 op np_header(sizeof(msize) + sizeof(l) + l, Rversion, tag);
572 5c485996 2021-07-28 op evbuffer_add(evb, &msize, sizeof(msize));
573 5c485996 2021-07-28 op np_string(l, version);
578 c5d518da 2021-07-29 op np_attach(uint16_t tag, struct qid *qid)
580 9ebb95a7 2021-07-30 op np_header(QIDSIZE, Rattach, tag);
586 c377c1b9 2021-07-30 op np_clunk(uint16_t tag)
588 9ebb95a7 2021-07-30 op np_header(0, Rclunk, tag);
593 36b30273 2021-07-30 op np_flush(uint16_t tag)
595 9ebb95a7 2021-07-30 op np_header(0, Rflush, tag);
600 1c08fc54 2021-08-01 op np_walk(uint16_t tag, int nwqid, struct qid *wqid)
604 83f6b305 2021-08-01 op /* two bytes for the counter */
605 83f6b305 2021-08-01 op np_header(2 + QIDSIZE * nwqid, Rwalk, tag);
606 83f6b305 2021-08-01 op np_write16(nwqid);
607 1c08fc54 2021-08-01 op for (i = 0; i < nwqid; ++i)
608 1c08fc54 2021-08-01 op np_qid(wqid + i);
614 021481ca 2021-12-13 op np_open(uint16_t tag, struct qid *qid, uint32_t iounit)
616 021481ca 2021-12-13 op np_header(QIDSIZE + sizeof(iounit), Ropen, tag);
618 021481ca 2021-12-13 op np_write32(iounit);
623 2ef72ade 2021-07-28 op np_error(uint16_t tag, const char *errstr)
627 2ef72ade 2021-07-28 op l = strlen(errstr);
629 9ebb95a7 2021-07-30 op np_header(sizeof(l) + l, Rerror, tag);
630 5c485996 2021-07-28 op np_string(l, errstr);
635 c5d518da 2021-07-29 op np_errno(uint16_t tag)
637 9b088310 2021-07-30 op int saved_errno;
638 c5d518da 2021-07-29 op char buf[64];
640 9b088310 2021-07-30 op saved_errno = errno;
642 c5d518da 2021-07-29 op strerror_r(errno, buf, sizeof(buf));
643 c5d518da 2021-07-29 op np_error(tag, buf);
645 9b088310 2021-07-30 op errno = saved_errno;
649 48192874 2021-08-01 op np_read8(const char *t, const char *f, uint8_t *dst, const uint8_t **src,
652 48192874 2021-08-01 op if (*len < sizeof(*dst)) {
653 48192874 2021-08-01 op log_warnx("%s: wanted %zu bytes for the %s field but only "
654 48192874 2021-08-01 op "%zu are available.", t, sizeof(*dst), f, *len);
658 48192874 2021-08-01 op memcpy(dst, *src, sizeof(*dst));
659 48192874 2021-08-01 op *src += sizeof(*dst);
660 48192874 2021-08-01 op *len -= sizeof(*dst);
666 48192874 2021-08-01 op np_read16(const char *t, const char *f, uint16_t *dst, const uint8_t **src,
669 48192874 2021-08-01 op if (*len < sizeof(*dst)) {
670 48192874 2021-08-01 op log_warnx("%s: wanted %zu bytes for the %s field but only "
671 48192874 2021-08-01 op "%zu are available.", t, sizeof(*dst), f, *len);
675 48192874 2021-08-01 op memcpy(dst, *src, sizeof(*dst));
676 48192874 2021-08-01 op *src += sizeof(*dst);
677 48192874 2021-08-01 op *len -= sizeof(*dst);
678 48192874 2021-08-01 op *dst = le16toh(*dst);
684 48192874 2021-08-01 op np_read32(const char *t, const char *f, uint32_t *dst, const uint8_t **src,
687 48192874 2021-08-01 op if (*len < sizeof(*dst)) {
688 48192874 2021-08-01 op log_warnx("%s: wanted %zu bytes for the %s field but only "
689 48192874 2021-08-01 op "%zu are available.", t, sizeof(*dst), f, *len);
693 48192874 2021-08-01 op memcpy(dst, *src, sizeof(*dst));
694 48192874 2021-08-01 op *src += sizeof(*dst);
695 48192874 2021-08-01 op *len -= sizeof(*dst);
696 48192874 2021-08-01 op *dst = le32toh(*dst);
702 48192874 2021-08-01 op np_readstr(const char *t, const char *f, char *res, size_t reslen,
703 48192874 2021-08-01 op const uint8_t **src, size_t *len)
706 48192874 2021-08-01 op char buf[32];
708 48192874 2021-08-01 op strlcpy(buf, f, sizeof(buf));
709 48192874 2021-08-01 op strlcat(buf, "-len", sizeof(buf));
711 48192874 2021-08-01 op if (!np_read16(t, buf, &sl, src, len))
712 48192874 2021-08-01 op return READSTRERR;
714 48192874 2021-08-01 op if (*len < sl) {
715 48192874 2021-08-01 op log_warnx("%s: wanted %d bytes for the %s field but only "
716 48192874 2021-08-01 op "%zu are available.", t, sl, f, *len);
717 48192874 2021-08-01 op return READSTRERR;
720 48192874 2021-08-01 op if (*len > reslen-1)
721 48192874 2021-08-01 op return READSTRTRUNC;
723 48192874 2021-08-01 op memcpy(res, *src, sl);
724 48192874 2021-08-01 op res[sl] = '\0';
732 e60f4e08 2021-07-30 op tversion(struct np_msg_header *hdr, const uint8_t *data, size_t len)
734 48192874 2021-08-01 op char *dot, version[32];
736 e60f4e08 2021-07-30 op if (handshaked)
739 3d20424d 2021-08-01 op /* msize[4] version[s] */
740 48192874 2021-08-01 op if (!NPREAD32("msize", &msize, &data, &len))
743 48192874 2021-08-01 op switch (NPREADSTR("version", version, sizeof(version), &data, &len)) {
744 48192874 2021-08-01 op case READSTRERR:
746 48192874 2021-08-01 op case READSTRTRUNC:
747 48192874 2021-08-01 op log_warnx("9P version string too long, truncated");
748 48192874 2021-08-01 op goto mismatch;
751 48192874 2021-08-01 op if ((dot = strchr(version, '.')) != NULL)
754 48192874 2021-08-01 op if (strcmp(version, VERSION9P) != 0 ||
756 48192874 2021-08-01 op goto mismatch;
758 48192874 2021-08-01 op /* version matched */
759 48192874 2021-08-01 op handshaked = 1;
760 e60f4e08 2021-07-30 op msize = MIN(msize, MSIZE9P);
761 e60f4e08 2021-07-30 op client_send_listener(IMSG_MSIZE, &msize, sizeof(msize));
762 e60f4e08 2021-07-30 op np_version(hdr->tag, msize, VERSION9P);
766 48192874 2021-08-01 op log_warnx("unknown 9P version string: \"%s\", want "VERSION9P,
768 48192874 2021-08-01 op np_version(hdr->tag, MSIZE9P, "unknown");
772 e60f4e08 2021-07-30 op client_send_listener(IMSG_CLOSE, NULL, 0);
773 e60f4e08 2021-07-30 op client_shutdown();
777 e60f4e08 2021-07-30 op tattach(struct np_msg_header *hdr, const uint8_t *data, size_t len)
779 e60f4e08 2021-07-30 op struct qid *qid;
780 e60f4e08 2021-07-30 op struct fid *f;
781 48192874 2021-08-01 op uint32_t fid, afid;
783 014adfc2 2021-07-30 op char aname[PATH_MAX];
785 e60f4e08 2021-07-30 op if (attached) {
786 e60f4e08 2021-07-30 op np_error(hdr->tag, "already attached");
790 e60f4e08 2021-07-30 op /* fid[4] afid[4] uname[s] aname[s] */
792 48192874 2021-08-01 op if (!NPREAD32("fid", &fid, &data, &len) ||
793 48192874 2021-08-01 op !NPREAD32("afid", &afid, &data, &len))
796 48192874 2021-08-01 op /* read the uname but don't actually use it */
797 48192874 2021-08-01 op switch (NPREADSTR("uname", aname, sizeof(aname), &data, &len)) {
798 48192874 2021-08-01 op case READSTRERR:
800 48192874 2021-08-01 op case READSTRTRUNC:
801 48192874 2021-08-01 op np_error(hdr->tag, "name too long");
805 48192874 2021-08-01 op switch (NPREADSTR("aname", aname, sizeof(aname), &data, &len)) {
806 48192874 2021-08-01 op case READSTRERR:
808 48192874 2021-08-01 op case READSTRTRUNC:
809 014adfc2 2021-07-30 op np_error(hdr->tag, "name too long");
813 deb971f5 2021-08-12 op if ((fd = open(aname, O_RDONLY|O_DIRECTORY)) == -1)
816 deb971f5 2021-08-12 op if ((qid = qid_from_fd(fd, NULL, NULL)) == NULL)
819 014adfc2 2021-07-30 op log_debug("attached %s", aname);
821 e60f4e08 2021-07-30 op if ((f = new_fid(qid, fid)) == NULL) {
822 053e2652 2021-08-01 op qid_decref(qid);
826 e60f4e08 2021-07-30 op np_attach(hdr->tag, qid);
827 e60f4e08 2021-07-30 op attached = 1;
831 deb971f5 2021-08-12 op np_errno(hdr->tag);
832 deb971f5 2021-08-12 op log_warn("failed to attach %s", aname);
837 014adfc2 2021-07-30 op client_send_listener(IMSG_CLOSE, NULL, 0);
838 014adfc2 2021-07-30 op client_shutdown();
842 c377c1b9 2021-07-30 op tclunk(struct np_msg_header *hdr, const uint8_t *data, size_t len)
844 c377c1b9 2021-07-30 op struct fid *f;
845 c377c1b9 2021-07-30 op uint32_t fid;
848 48192874 2021-08-01 op if (!NPREAD32("fid", &fid, &data, &len)) {
849 c377c1b9 2021-07-30 op client_send_listener(IMSG_CLOSE, NULL, 0);
850 c377c1b9 2021-07-30 op client_shutdown();
854 c377c1b9 2021-07-30 op if ((f = fid_by_id(fid)) == NULL) {
855 c377c1b9 2021-07-30 op np_error(hdr->tag, "invalid fid");
860 c377c1b9 2021-07-30 op np_clunk(hdr->tag);
864 36b30273 2021-07-30 op tflush(struct np_msg_header *hdr, const uint8_t *data, size_t len)
866 36b30273 2021-07-30 op uint16_t oldtag;
869 36b30273 2021-07-30 op * We're doing only synchronous I/O. Tflush is implemented
870 36b30273 2021-07-30 op * only because it's illegal to reply with a Rerror.
873 36b30273 2021-07-30 op /* oldtag[2] */
874 36b30273 2021-07-30 op if (len != sizeof(oldtag)) {
875 36b30273 2021-07-30 op log_warnx("Tclunk with the wrong size: got %zu want %zu",
876 36b30273 2021-07-30 op len, sizeof(oldtag));
877 36b30273 2021-07-30 op client_send_listener(IMSG_CLOSE, NULL, 0);
878 36b30273 2021-07-30 op client_shutdown();
882 36b30273 2021-07-30 op np_flush(hdr->tag);
886 1c08fc54 2021-08-01 op twalk(struct np_msg_header *hdr, const uint8_t *data, size_t len)
888 deb971f5 2021-08-12 op struct stat sb;
889 053e2652 2021-08-01 op struct qid *qid, wqid[MAXWELEM] = {0};
890 1c08fc54 2021-08-01 op struct fid *f, *nf;
891 1c08fc54 2021-08-01 op uint32_t fid, newfid;
892 7ddddcfe 2021-08-01 op uint16_t nwname;
893 deb971f5 2021-08-12 op int fd, oldfd, no, nwqid = 0;
894 deb971f5 2021-08-12 op char wnam[PATH_MAX+1];
896 48192874 2021-08-01 op if (!NPREAD32("fid", &fid, &data, &len) ||
897 48192874 2021-08-01 op !NPREAD32("newfid", &newfid, &data, &len) ||
898 48192874 2021-08-01 op !NPREAD16("nwname", &nwname, &data, &len))
901 1c08fc54 2021-08-01 op if (nwname > MAXWELEM) {
902 1c08fc54 2021-08-01 op log_warnx("Twalk: more than %d path elements: %d",
903 1c08fc54 2021-08-01 op MAXWELEM, nwname);
907 1c08fc54 2021-08-01 op if ((f = fid_by_id(fid)) == NULL) {
908 1c08fc54 2021-08-01 op np_error(hdr->tag, "invalid fid");
912 47c9b8b8 2021-12-02 op if (f->iomode != 0) {
913 47c9b8b8 2021-12-02 op np_error(hdr->tag, "fid already opened for I/O");
917 1c08fc54 2021-08-01 op if (fid == newfid)
919 1c08fc54 2021-08-01 op else if ((nf = fid_by_id(newfid)) != NULL) {
920 1c08fc54 2021-08-01 op np_error(hdr->tag, "newfid already in use");
925 913eba5c 2021-08-01 op /* special case: fid duplication */
926 22dfb5a0 2021-08-01 op if (nwname == 0) {
928 1c08fc54 2021-08-01 op * TODO: should we forbid fids duplication when fid ==
931 f987557c 2021-12-02 op if (nf == NULL && (nf = new_fid(f->qid, newfid)) == NULL)
932 f987557c 2021-12-02 op fatal("new_fid duplication");
934 1c08fc54 2021-08-01 op np_walk(hdr->tag, 1, f->qid);
938 053e2652 2021-08-01 op if (f->qid->type != QTDIR) {
939 053e2652 2021-08-01 op np_error(hdr->tag, "fid doesn't represent a directory");
943 deb971f5 2021-08-12 op oldfd = f->qid->fd;
945 1c08fc54 2021-08-01 op for (nwqid = 0; nwqid < nwname; nwqid++) {
946 053e2652 2021-08-01 op switch (NPREADSTR("wname", wnam, sizeof(wnam), &data, &len)) {
947 48192874 2021-08-01 op case READSTRERR:
949 48192874 2021-08-01 op case READSTRTRUNC:
950 1c08fc54 2021-08-01 op np_error(hdr->tag, "wname too long");
954 deb971f5 2021-08-12 op if ((fd = openat(oldfd, wnam, O_RDONLY|O_DIRECTORY)) == -1 &&
955 deb971f5 2021-08-12 op errno != ENOTDIR) {
957 deb971f5 2021-08-12 op goto cantopen;
960 deb971f5 2021-08-12 op if ((fd == -1 && fstatat(oldfd, wnam, &sb, 0) == -1) ||
961 deb971f5 2021-08-12 op (fd != -1 && fstat(fd, &sb) == -1)) {
963 deb971f5 2021-08-12 op goto cantopen;
966 deb971f5 2021-08-12 op qid_update_from_sb(&wqid[nwqid], &sb);
968 deb971f5 2021-08-12 op /* reached a file but we still have other components */
969 deb971f5 2021-08-12 op if (fd == -1 && nwqid+1 < nwname)
970 deb971f5 2021-08-12 op goto cantopen;
972 deb971f5 2021-08-12 op /* reached the end and found a file */
973 deb971f5 2021-08-12 op if (fd == -1 && nwqid+1 == nwname)
976 deb971f5 2021-08-12 op if (oldfd != f->qid->fd)
977 deb971f5 2021-08-12 op close(oldfd);
982 deb971f5 2021-08-12 op * There can be two possibilities: fd == -1 means that we've
983 deb971f5 2021-08-12 op * reached a file and we should save BOTH the dirfd (oldfd)
984 deb971f5 2021-08-12 op * and the path (wnam); or we just reached another directory,
985 deb971f5 2021-08-12 op * in which case we can just create a new qid using fd.
987 deb971f5 2021-08-12 op if (fd == -1)
988 deb971f5 2021-08-12 op qid = qid_from_fd(oldfd, wnam, &sb);
990 deb971f5 2021-08-12 op qid = qid_from_fd(oldfd, NULL, &sb);
991 deb971f5 2021-08-12 op if (qid == NULL)
992 eafb23b1 2021-08-01 op fatal("qid_from_fd");
994 1c08fc54 2021-08-01 op if (nf == NULL) {
995 1c08fc54 2021-08-01 op if ((nf = new_fid(qid, newfid)) == NULL)
996 1c08fc54 2021-08-01 op fatal("new_fid");
998 1c08fc54 2021-08-01 op /* swap qid */
999 1c08fc54 2021-08-01 op qid_decref(nf->qid);
1000 1c08fc54 2021-08-01 op nf->qid = qid_incref(qid);
1003 1c08fc54 2021-08-01 op np_walk(hdr->tag, nwqid, wqid);
1007 deb971f5 2021-08-12 op if (oldfd != f->qid->fd)
1008 deb971f5 2021-08-12 op close(oldfd);
1010 deb971f5 2021-08-12 op if (nwqid == 0)
1011 deb971f5 2021-08-12 op np_error(hdr->tag, strerror(no));
1013 deb971f5 2021-08-12 op np_walk(hdr->tag, nwqid, wqid);
1017 1c08fc54 2021-08-01 op client_send_listener(IMSG_CLOSE, NULL, 0);
1018 1c08fc54 2021-08-01 op client_shutdown();
1021 021481ca 2021-12-13 op static inline int
1022 021481ca 2021-12-13 op npmode_to_unix(uint8_t mode, int *flags)
1024 021481ca 2021-12-13 op switch (mode & 0x0F) {
1025 021481ca 2021-12-13 op case KOREAD:
1026 021481ca 2021-12-13 op *flags = O_RDONLY;
1028 021481ca 2021-12-13 op case KOWRITE:
1029 021481ca 2021-12-13 op *flags = O_WRONLY;
1031 021481ca 2021-12-13 op case KORDWR:
1032 021481ca 2021-12-13 op *flags = O_RDWR;
1034 021481ca 2021-12-13 op case KOEXEC:
1035 021481ca 2021-12-13 op log_warnx("tried to open something with KOEXEC");
1036 021481ca 2021-12-13 op /* fallthrough */
1041 021481ca 2021-12-13 op if (mode & KOTRUNC)
1042 021481ca 2021-12-13 op *flags |= O_TRUNC;
1043 021481ca 2021-12-13 op if (mode & KORCLOSE)
1044 021481ca 2021-12-13 op *flags |= O_CLOEXEC;
1050 021481ca 2021-12-13 op topen(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1052 021481ca 2021-12-13 op struct stat sb;
1053 021481ca 2021-12-13 op struct qid qid;
1054 021481ca 2021-12-13 op struct fid *f;
1055 021481ca 2021-12-13 op uint32_t fid;
1056 021481ca 2021-12-13 op uint8_t mode;
1058 021481ca 2021-12-13 op /* fid[4] mode[1] */
1059 021481ca 2021-12-13 op if (!NPREAD32("fid", &fid, &data, &len) ||
1060 021481ca 2021-12-13 op !NPREAD8("mode", &mode, &data, &len)) {
1061 021481ca 2021-12-13 op client_send_listener(IMSG_CLOSE, NULL, 0);
1062 021481ca 2021-12-13 op client_shutdown();
1066 021481ca 2021-12-13 op if ((f = fid_by_id(fid)) == NULL || f->fd != -1) {
1067 021481ca 2021-12-13 op np_error(hdr->tag, "invalid fid");
1071 021481ca 2021-12-13 op if (f->qid->type & QTDIR) {
1073 021481ca 2021-12-13 op * XXX: real 9P2000 uses reads on directories to list the
1074 021481ca 2021-12-13 op * files, but 9P2000.L (and possibly .U too) uses
1075 021481ca 2021-12-13 op * Treaddir. It's my intention to support the 9p-style
1076 021481ca 2021-12-13 op * read-on-dir, just not yet.
1078 021481ca 2021-12-13 op np_error(hdr->tag, "can't do I/O on directories yet");
1082 021481ca 2021-12-13 op if (npmode_to_unix(mode, &f->iomode) == -1) {
1083 021481ca 2021-12-13 op np_error(hdr->tag, "invalid mode");
1087 021481ca 2021-12-13 op if ((f->fd = openat(f->qid->fd, f->qid->fpath, f->iomode)) == -1) {
1088 021481ca 2021-12-13 op np_error(hdr->tag, strerror(errno));
1092 021481ca 2021-12-13 op if (fstat(f->fd, &sb) == -1)
1093 021481ca 2021-12-13 op fatal("fstat");
1095 021481ca 2021-12-13 op qid_update_from_sb(&qid, &sb);
1096 021481ca 2021-12-13 op np_open(hdr->tag, &qid, sb.st_blksize);
1100 e60f4e08 2021-07-30 op handle_message(struct imsg *imsg, size_t len)
1102 e60f4e08 2021-07-30 op struct msg {
1103 e60f4e08 2021-07-30 op uint8_t type;
1104 e60f4e08 2021-07-30 op void (*fn)(struct np_msg_header *, const uint8_t *, size_t);
1105 e60f4e08 2021-07-30 op } msgs[] = {
1106 e60f4e08 2021-07-30 op {Tversion, tversion},
1107 e60f4e08 2021-07-30 op {Tattach, tattach},
1108 c377c1b9 2021-07-30 op {Tclunk, tclunk},
1109 36b30273 2021-07-30 op {Tflush, tflush},
1110 1c08fc54 2021-08-01 op {Twalk, twalk},
1111 021481ca 2021-12-13 op {Topen, topen},
1113 e60f4e08 2021-07-30 op struct np_msg_header hdr;
1115 e60f4e08 2021-07-30 op uint8_t *data;
1117 64c19d90 2021-07-30 op #if DEBUG_PACKETS
1118 78b94752 2021-08-01 op hexdump("incoming packet", imsg->data, len);
1121 e60f4e08 2021-07-30 op parse_message(imsg->data, len, &hdr, &data);
1122 e60f4e08 2021-07-30 op len -= HEADERSIZE;
1124 e60f4e08 2021-07-30 op log_debug("got request: len=%d type=%d[%s] tag=%d",
1125 e60f4e08 2021-07-30 op hdr.len, hdr.type, pp_msg_type(hdr.type), hdr.tag);
1127 e60f4e08 2021-07-30 op if (!handshaked && hdr.type != Tversion) {
1128 e60f4e08 2021-07-30 op client_send_listener(IMSG_CLOSE, NULL, 0);
1129 e60f4e08 2021-07-30 op client_shutdown();
1133 e60f4e08 2021-07-30 op for (i = 0; i < sizeof(msgs)/sizeof(msgs[0]); ++i) {
1134 e60f4e08 2021-07-30 op if (msgs[i].type != hdr.type)
1137 e60f4e08 2021-07-30 op msgs[i].fn(&hdr, data, len);
1141 e60f4e08 2021-07-30 op np_error(hdr.tag, "Not supported.");