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 baa40504 2021-12-15 op #include <assert.h>
22 baa40504 2021-12-15 op #include <dirent.h>
23 23e03c88 2021-07-26 op #include <endian.h>
24 8d1b399b 2021-07-22 op #include <errno.h>
25 c5d518da 2021-07-29 op #include <fcntl.h>
26 8d1b399b 2021-07-22 op #include <pwd.h>
27 8d1b399b 2021-07-22 op #include <signal.h>
28 8d1b399b 2021-07-22 op #include <stdlib.h>
29 8d1b399b 2021-07-22 op #include <string.h>
30 8d1b399b 2021-07-22 op #include <syslog.h>
31 8d1b399b 2021-07-22 op #include <unistd.h>
33 8d1b399b 2021-07-22 op #include "client.h"
34 8d1b399b 2021-07-22 op #include "kamid.h"
35 8d1b399b 2021-07-22 op #include "log.h"
36 8d1b399b 2021-07-22 op #include "sandbox.h"
37 23e03c88 2021-07-26 op #include "utils.h"
39 2adb951f 2021-08-01 op #define DEBUG_PACKETS 0
41 baa40504 2021-12-15 op /* straight outta /src/usr.bin/ssh/scp.c */
42 baa40504 2021-12-15 op #define TYPE_OVERFLOW(type, val) \
43 baa40504 2021-12-15 op ((sizeof(type) == 4 && (val) > INT32_MAX) || \
44 baa40504 2021-12-15 op (sizeof(type) == 8 && (val) > INT64_MAX) || \
45 baa40504 2021-12-15 op (sizeof(type) != 4 && sizeof(type) != 8))
47 c5d518da 2021-07-29 op STAILQ_HEAD(qidhead, qid) qids;
49 c5d518da 2021-07-29 op /* definition of a qid */
50 c5d518da 2021-07-29 op uint64_t path;
51 c5d518da 2021-07-29 op uint32_t vers;
57 deb971f5 2021-08-12 op char fpath[PATH_MAX+1];
59 c5d518da 2021-07-29 op STAILQ_ENTRY(qid) entries;
62 c5d518da 2021-07-29 op STAILQ_HEAD(fidhead, fid) fids;
67 29f1f582 2021-12-13 op * 0 when the fid was not yet opened for I/O otherwise set to
68 021481ca 2021-12-13 op * the flags passed to open(2). O_CLOEXEC means ORCLOSE, that
69 021481ca 2021-12-13 op * is to unlink the file upon Tclunk.
74 29f1f582 2021-12-13 op * if iomode is set, this fid was opened and fd represents its
75 29f1f582 2021-12-13 op * file descriptor.
79 baa40504 2021-12-15 op struct evbuffer *evb;
80 baa40504 2021-12-15 op uint64_t offset;
82 c5d518da 2021-07-29 op struct qid *qid;
83 c5d518da 2021-07-29 op STAILQ_ENTRY(fid) entries;
86 8d1b399b 2021-07-22 op static struct imsgev *iev_listener;
87 2ef72ade 2021-07-28 op static struct evbuffer *evb;
88 2ef72ade 2021-07-28 op static uint32_t peerid;
90 cf758c33 2021-12-14 op static int handshaked;
91 5c485996 2021-07-28 op uint32_t msize;
93 06a84967 2021-07-22 op static ATTR_DEAD void client_shutdown(void);
94 8d1b399b 2021-07-22 op static void client_sig_handler(int, short, void *);
95 8d1b399b 2021-07-22 op static void client_dispatch_listener(int, short, void *);
96 8d1b399b 2021-07-22 op static void client_privdrop(const char *, const char *);
98 2ef72ade 2021-07-28 op static int client_send_listener(int, const void *, uint16_t);
100 deb971f5 2021-08-12 op static void qid_update_from_sb(struct qid *, struct stat *);
101 deb971f5 2021-08-12 op static struct qid *qid_from_fd(int, const char *, struct stat *);
102 c377c1b9 2021-07-30 op static struct qid *qid_incref(struct qid *);
103 c377c1b9 2021-07-30 op static void qid_decref(struct qid *);
105 c5d518da 2021-07-29 op static struct fid *new_fid(struct qid *, uint32_t);
106 c377c1b9 2021-07-30 op static struct fid *fid_by_id(uint32_t);
107 c377c1b9 2021-07-30 op static void free_fid(struct fid *);
109 48192874 2021-08-01 op static void parse_message(const uint8_t *, size_t,
110 48192874 2021-08-01 op struct np_msg_header *, uint8_t **);
112 83f6b305 2021-08-01 op static void np_write16(uint16_t);
113 021481ca 2021-12-13 op static void np_write32(uint32_t);
114 2845cccb 2021-07-29 op static void np_header(uint32_t, uint8_t, uint16_t);
115 2845cccb 2021-07-29 op static void np_string(uint16_t, const char *);
116 c5d518da 2021-07-29 op static void np_qid(struct qid *);
117 84b10f04 2021-07-29 op static void do_send(void);
119 2845cccb 2021-07-29 op static void np_version(uint16_t, uint32_t, const char *);
120 c5d518da 2021-07-29 op static void np_attach(uint16_t, struct qid *);
121 c377c1b9 2021-07-30 op static void np_clunk(uint16_t);
122 36b30273 2021-07-30 op static void np_flush(uint16_t);
123 1c08fc54 2021-08-01 op static void np_walk(uint16_t, int, struct qid *);
124 021481ca 2021-12-13 op static void np_open(uint16_t, struct qid *, uint32_t);
125 baa40504 2021-12-15 op static void np_read(uint16_t, uint32_t, void *);
126 2ef72ade 2021-07-28 op static void np_error(uint16_t, const char *);
127 c5d518da 2021-07-29 op static void np_errno(uint16_t);
129 48192874 2021-08-01 op static int np_read8(const char *, const char *, uint8_t *,
130 48192874 2021-08-01 op const uint8_t **, size_t *);
131 48192874 2021-08-01 op static int np_read16(const char *, const char *, uint16_t *,
132 48192874 2021-08-01 op const uint8_t **, size_t *);
133 48192874 2021-08-01 op static int np_read32(const char *, const char *, uint32_t *,
134 48192874 2021-08-01 op const uint8_t **, size_t *);
135 baa40504 2021-12-15 op static int np_read64(const char *, const char *, uint64_t *,
136 baa40504 2021-12-15 op const uint8_t **, size_t *);
138 48192874 2021-08-01 op #define READSTRERR -1
139 48192874 2021-08-01 op #define READSTRTRUNC -2
140 48192874 2021-08-01 op static int np_readstr(const char *, const char *, char *, size_t,
141 48192874 2021-08-01 op const uint8_t **, size_t *);
143 48192874 2021-08-01 op #define NPREAD8(f, dst, src, len) np_read8(__func__, f, dst, src, len)
144 48192874 2021-08-01 op #define NPREAD16(f, dst, src, len) np_read16(__func__, f, dst, src, len)
145 48192874 2021-08-01 op #define NPREAD32(f, dst, src, len) np_read32(__func__, f, dst, src, len)
146 baa40504 2021-12-15 op #define NPREAD64(f, dst, src, len) np_read64(__func__, f, dst, src, len)
148 48192874 2021-08-01 op #define NPREADSTR(f, b, bl, src, len) np_readstr(__func__, f, b, bl, src, len)
150 e60f4e08 2021-07-30 op static void tversion(struct np_msg_header *, const uint8_t *, size_t);
151 e60f4e08 2021-07-30 op static void tattach(struct np_msg_header *, const uint8_t *, size_t);
152 c377c1b9 2021-07-30 op static void tclunk(struct np_msg_header *, const uint8_t *, size_t);
153 36b30273 2021-07-30 op static void tflush(struct np_msg_header *, const uint8_t *, size_t);
154 1c08fc54 2021-08-01 op static void twalk(struct np_msg_header *, const uint8_t *, size_t);
155 021481ca 2021-12-13 op static void topen(struct np_msg_header *, const uint8_t *, size_t);
156 baa40504 2021-12-15 op static void tread(struct np_msg_header *, const uint8_t *, size_t);
157 e60f4e08 2021-07-30 op static void handle_message(struct imsg *, size_t);
159 06a84967 2021-07-22 op ATTR_DEAD void
160 8d1b399b 2021-07-22 op client(int debug, int verbose)
162 8d1b399b 2021-07-22 op struct event ev_sigint, ev_sigterm;
164 8d1b399b 2021-07-22 op log_init(debug, LOG_DAEMON);
165 8d1b399b 2021-07-22 op log_setverbose(verbose);
167 8d1b399b 2021-07-22 op setproctitle("client");
168 8d1b399b 2021-07-22 op log_procinit("client");
170 8d1b399b 2021-07-22 op log_debug("warming up");
172 8d1b399b 2021-07-22 op event_init();
174 8d1b399b 2021-07-22 op /* Setup signal handlers */
175 8d1b399b 2021-07-22 op signal_set(&ev_sigint, SIGINT, client_sig_handler, NULL);
176 8d1b399b 2021-07-22 op signal_set(&ev_sigterm, SIGTERM, client_sig_handler, NULL);
178 8d1b399b 2021-07-22 op signal_add(&ev_sigint, NULL);
179 8d1b399b 2021-07-22 op signal_add(&ev_sigterm, NULL);
181 8d1b399b 2021-07-22 op signal(SIGPIPE, SIG_IGN);
182 8d1b399b 2021-07-22 op signal(SIGHUP, SIG_IGN);
184 8d1b399b 2021-07-22 op /* Setup pipe and event handler to the listener process */
185 8d1b399b 2021-07-22 op if ((iev_listener = malloc(sizeof(*iev_listener))) == NULL)
188 8d1b399b 2021-07-22 op imsg_init(&iev_listener->ibuf, 3);
189 8d1b399b 2021-07-22 op iev_listener->handler = client_dispatch_listener;
191 8d1b399b 2021-07-22 op /* Setup event handlers. */
192 8d1b399b 2021-07-22 op iev_listener->events = EV_READ;
193 8d1b399b 2021-07-22 op event_set(&iev_listener->ev, iev_listener->ibuf.fd,
194 8d1b399b 2021-07-22 op iev_listener->events, iev_listener->handler, iev_listener);
195 8d1b399b 2021-07-22 op event_add(&iev_listener->ev, NULL);
197 8d1b399b 2021-07-22 op event_dispatch();
198 8d1b399b 2021-07-22 op client_shutdown();
201 06a84967 2021-07-22 op static ATTR_DEAD void
202 8d1b399b 2021-07-22 op client_shutdown(void)
204 2ef72ade 2021-07-28 op if (evb != NULL)
205 2ef72ade 2021-07-28 op evbuffer_free(evb);
207 8d1b399b 2021-07-22 op msgbuf_clear(&iev_listener->ibuf.w);
208 8d1b399b 2021-07-22 op close(iev_listener->ibuf.fd);
210 8d1b399b 2021-07-22 op free(iev_listener);
212 223c9e73 2021-08-06 op log_debug("client exiting");
217 8d1b399b 2021-07-22 op client_sig_handler(int sig, short event, void *d)
220 8d1b399b 2021-07-22 op * Normal signal handler rules don't apply because libevent
221 8d1b399b 2021-07-22 op * decouples for us.
224 8d1b399b 2021-07-22 op switch (sig) {
226 8d1b399b 2021-07-22 op case SIGTERM:
227 8d1b399b 2021-07-22 op client_shutdown();
229 8d1b399b 2021-07-22 op fatalx("unexpected signal %d", sig);
233 8d1b399b 2021-07-22 op #define AUTH_NONE 0
234 8d1b399b 2021-07-22 op #define AUTH_USER 1
235 8d1b399b 2021-07-22 op #define AUTH_DONE 2
238 8d1b399b 2021-07-22 op client_dispatch_listener(int fd, short event, void *d)
240 8d1b399b 2021-07-22 op static int auth = AUTH_NONE;
241 8d1b399b 2021-07-22 op static char username[64] = {0};
242 8d1b399b 2021-07-22 op static char dir[PATH_MAX] = {0};
243 8d1b399b 2021-07-22 op struct imsg imsg;
244 8d1b399b 2021-07-22 op struct imsgev *iev = d;
245 8d1b399b 2021-07-22 op struct imsgbuf *ibuf;
247 8d1b399b 2021-07-22 op int shut = 0;
249 8d1b399b 2021-07-22 op ibuf = &iev->ibuf;
251 8d1b399b 2021-07-22 op if (event & EV_READ) {
252 8d1b399b 2021-07-22 op if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
253 8d1b399b 2021-07-22 op fatal("imsg_read error");
254 8d1b399b 2021-07-22 op if (n == 0) /* Connection closed */
257 8d1b399b 2021-07-22 op if (event & EV_WRITE) {
258 8d1b399b 2021-07-22 op if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
259 8d1b399b 2021-07-22 op fatal("msgbuf_write");
260 8d1b399b 2021-07-22 op if (n == 0) /* Connection closed */
265 8d1b399b 2021-07-22 op if ((n = imsg_get(ibuf, &imsg)) == -1)
266 8d1b399b 2021-07-22 op fatal("%s: imsg_get error", __func__);
267 8d1b399b 2021-07-22 op if (n == 0) /* No more messages. */
270 8d1b399b 2021-07-22 op switch (imsg.hdr.type) {
271 8d1b399b 2021-07-22 op case IMSG_AUTH:
272 2ef72ade 2021-07-28 op peerid = imsg.hdr.peerid;
274 8d1b399b 2021-07-22 op fatalx("%s: IMSG_AUTH already done", __func__);
275 8d1b399b 2021-07-22 op auth = AUTH_USER;
276 8d1b399b 2021-07-22 op ((char *)imsg.data)[IMSG_DATA_SIZE(imsg)-1] = '\0';
277 8d1b399b 2021-07-22 op strlcpy(username, imsg.data, sizeof(username));
279 8d1b399b 2021-07-22 op case IMSG_AUTH_DIR:
280 8d1b399b 2021-07-22 op if (auth != AUTH_USER)
281 8d1b399b 2021-07-22 op fatalx("%s: IMSG_AUTH_DIR not after IMSG_AUTH",
283 8d1b399b 2021-07-22 op auth = AUTH_DONE;
284 8d1b399b 2021-07-22 op ((char *)imsg.data)[IMSG_DATA_SIZE(imsg)-1] = '\0';
285 8d1b399b 2021-07-22 op strlcpy(dir, imsg.data, sizeof(dir));
286 8d1b399b 2021-07-22 op client_privdrop(username, dir);
287 8d1b399b 2021-07-22 op memset(username, 0, sizeof(username));
288 8d1b399b 2021-07-22 op memset(dir, 0, sizeof(username));
290 8d1b399b 2021-07-22 op case IMSG_BUF:
293 23e03c88 2021-07-26 op fatalx("%s: can't handle messages before"
294 23e03c88 2021-07-26 op " doing the auth", __func__);
295 23e03c88 2021-07-26 op handle_message(&imsg, IMSG_DATA_SIZE(imsg));
297 8d1b399b 2021-07-22 op case IMSG_CONN_GONE:
298 8d1b399b 2021-07-22 op log_debug("closing");
302 8d1b399b 2021-07-22 op log_debug("%s: unexpected imsg %d",
303 8d1b399b 2021-07-22 op __func__, imsg.hdr.type);
306 8d1b399b 2021-07-22 op imsg_free(&imsg);
310 8d1b399b 2021-07-22 op imsg_event_add(iev);
312 8d1b399b 2021-07-22 op /* This pipe is dead. Remove its event handler. */
313 8d1b399b 2021-07-22 op event_del(&iev->ev);
314 223c9e73 2021-08-06 op log_debug("pipe closed, shutting down...");
315 8d1b399b 2021-07-22 op event_loopexit(NULL);
320 8d1b399b 2021-07-22 op client_privdrop(const char *username, const char *dir)
322 8d1b399b 2021-07-22 op struct passwd *pw;
324 8d1b399b 2021-07-22 op setproctitle("client %s", username);
326 8d1b399b 2021-07-22 op if ((pw = getpwnam(username)) == NULL)
327 8d1b399b 2021-07-22 op fatalx("getpwnam(%s) failed", username);
329 8d1b399b 2021-07-22 op if (chroot(dir) == -1)
330 8d1b399b 2021-07-22 op fatal("chroot");
331 8d1b399b 2021-07-22 op if (chdir("/") == -1)
332 8d1b399b 2021-07-22 op fatal("chdir(\"/\")");
334 8d1b399b 2021-07-22 op if (setgroups(1, &pw->pw_gid) ||
335 8d1b399b 2021-07-22 op setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
336 8d1b399b 2021-07-22 op setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
337 8d1b399b 2021-07-22 op fatal("can't drop privileges");
339 8d1b399b 2021-07-22 op sandbox_client();
340 66a9a40a 2021-07-28 op log_debug("client ready; user=%s dir=%s", username, dir);
342 2ef72ade 2021-07-28 op if ((evb = evbuffer_new()) == NULL)
343 2ef72ade 2021-07-28 op fatal("evbuffer_new");
347 2ef72ade 2021-07-28 op client_send_listener(int type, const void *data, uint16_t len)
351 8d1b399b 2021-07-22 op if ((ret = imsg_compose(&iev_listener->ibuf, type, peerid, 0, -1,
352 8d1b399b 2021-07-22 op data, len)) != -1)
353 8d1b399b 2021-07-22 op imsg_event_add(iev_listener);
358 deb971f5 2021-08-12 op /* set qid fields from sb */
360 deb971f5 2021-08-12 op qid_update_from_sb(struct qid *qid, struct stat *sb)
362 deb971f5 2021-08-12 op qid->path = sb->st_ino;
365 1c08fc54 2021-08-01 op * Theoretically (and hopefully!) this should be a 64 bit
366 1c08fc54 2021-08-01 op * number. Unfortunately, 9P uses 32 bit timestamps.
368 deb971f5 2021-08-12 op qid->vers = sb->st_mtim.tv_sec;
370 deb971f5 2021-08-12 op if (S_ISREG(sb->st_mode))
371 1c08fc54 2021-08-01 op qid->type = QTFILE;
372 deb971f5 2021-08-12 op else if (S_ISDIR(sb->st_mode))
373 1c08fc54 2021-08-01 op qid->type = QTDIR;
374 deb971f5 2021-08-12 op else if (S_ISLNK(sb->st_mode))
375 1c08fc54 2021-08-01 op qid->type = QTSYMLINK;
378 c5d518da 2021-07-29 op /* creates a qid given a fd */
379 c5d518da 2021-07-29 op static struct qid *
380 deb971f5 2021-08-12 op qid_from_fd(int fd, const char *path, struct stat *s)
382 c5d518da 2021-07-29 op struct qid *qid;
383 deb971f5 2021-08-12 op struct stat sb;
386 c5d518da 2021-07-29 op if ((qid = calloc(1, sizeof(*qid))) == NULL)
389 deb971f5 2021-08-12 op if (path != NULL)
390 deb971f5 2021-08-12 op strlcpy(qid->fpath, path, sizeof(qid->fpath));
392 c5d518da 2021-07-29 op qid->fd = fd;
394 deb971f5 2021-08-12 op if (s == NULL) {
396 deb971f5 2021-08-12 op if (path == NULL)
397 deb971f5 2021-08-12 op r = fstat(fd, s);
399 deb971f5 2021-08-12 op r = fstatat(fd, path, s, 0);
400 deb971f5 2021-08-12 op if (r == -1) {
406 deb971f5 2021-08-12 op qid_update_from_sb(qid, s);
408 053e2652 2021-08-01 op STAILQ_INSERT_HEAD(&qids, qid, entries);
413 053e2652 2021-08-01 op static struct qid *
414 c377c1b9 2021-07-30 op qid_incref(struct qid *qid)
416 c377c1b9 2021-07-30 op qid->refcount++;
421 c377c1b9 2021-07-30 op qid_decref(struct qid *qid)
423 c377c1b9 2021-07-30 op if (--qid->refcount > 0)
426 c377c1b9 2021-07-30 op STAILQ_REMOVE(&qids, qid, qid, entries);
428 c377c1b9 2021-07-30 op close(qid->fd);
432 c5d518da 2021-07-29 op static struct fid *
433 c5d518da 2021-07-29 op new_fid(struct qid *qid, uint32_t fid)
435 c5d518da 2021-07-29 op struct fid *f;
437 c5d518da 2021-07-29 op if ((f = calloc(1, sizeof(*f))) == NULL)
440 c377c1b9 2021-07-30 op f->qid = qid_incref(qid);
441 c5d518da 2021-07-29 op f->fid = fid;
444 c5d518da 2021-07-29 op STAILQ_INSERT_HEAD(&fids, f, entries);
449 c377c1b9 2021-07-30 op static struct fid *
450 c377c1b9 2021-07-30 op fid_by_id(uint32_t fid)
452 c377c1b9 2021-07-30 op struct fid *f;
454 c377c1b9 2021-07-30 op STAILQ_FOREACH(f, &fids, entries) {
455 c377c1b9 2021-07-30 op if (f->fid == fid)
463 c377c1b9 2021-07-30 op free_fid(struct fid *f)
467 021481ca 2021-12-13 op if (f->fd != -1) {
468 baa40504 2021-12-15 op if (f->dir != NULL)
469 baa40504 2021-12-15 op r = closedir(f->dir);
471 baa40504 2021-12-15 op r = close(f->fd);
474 baa40504 2021-12-15 op fatal("can't close fid %d", f->fid);
476 baa40504 2021-12-15 op evbuffer_free(f->evb);
478 021481ca 2021-12-13 op /* try to honour ORCLOSE if requested */
479 021481ca 2021-12-13 op if (f->iomode & O_CLOEXEC)
480 021481ca 2021-12-13 op unlinkat(f->qid->fd, f->qid->fpath, 0);
483 c377c1b9 2021-07-30 op qid_decref(f->qid);
485 c377c1b9 2021-07-30 op STAILQ_REMOVE(&fids, f, fid, entries);
490 48192874 2021-08-01 op parse_message(const uint8_t *data, size_t len, struct np_msg_header *hdr,
491 5c485996 2021-07-28 op uint8_t **cnt)
493 48192874 2021-08-01 op size_t olen = len;
495 48192874 2021-08-01 op if (!NPREAD32("len", &hdr->len, &data, &len) ||
496 48192874 2021-08-01 op !NPREAD8("type", &hdr->type, &data, &len) ||
497 48192874 2021-08-01 op !NPREAD16("tag", &hdr->tag, &data, &len))
500 48192874 2021-08-01 op if (olen != hdr->len)
503 23e03c88 2021-07-26 op if (hdr->type < Tversion ||
504 23e03c88 2021-07-26 op hdr->type >= Tmax ||
505 23e03c88 2021-07-26 op hdr->type == Terror ||
506 23e03c88 2021-07-26 op (hdr->type & 0x1) != 0) /* cannot recv a R* */
509 23e03c88 2021-07-26 op hdr->tag = le32toh(hdr->tag);
511 48192874 2021-08-01 op *cnt = (uint8_t *)data;
515 23e03c88 2021-07-26 op /* TODO: send a proper message to terminate the connection. */
516 23e03c88 2021-07-26 op fatalx("got invalid message");
520 83f6b305 2021-08-01 op np_write16(uint16_t x)
522 83f6b305 2021-08-01 op x = htole16(x);
523 83f6b305 2021-08-01 op evbuffer_add(evb, &x, sizeof(x));
527 021481ca 2021-12-13 op np_write32(uint32_t x)
529 021481ca 2021-12-13 op x = htole32(x);
530 021481ca 2021-12-13 op evbuffer_add(evb, &x, sizeof(x));
534 baa40504 2021-12-15 op np_writebuf(size_t len, void *data)
536 baa40504 2021-12-15 op evbuffer_add(evb, data, len);
540 2ef72ade 2021-07-28 op np_header(uint32_t len, uint8_t type, uint16_t tag)
542 9ebb95a7 2021-07-30 op len += HEADERSIZE;
544 63f681aa 2021-07-28 op len = htole32(len);
545 63f681aa 2021-07-28 op tag = htole16(tag);
547 2ef72ade 2021-07-28 op evbuffer_add(evb, &len, sizeof(len));
548 2ef72ade 2021-07-28 op evbuffer_add(evb, &type, sizeof(type));
549 2ef72ade 2021-07-28 op evbuffer_add(evb, &tag, sizeof(tag));
553 5c485996 2021-07-28 op np_string(uint16_t len, const char *str)
555 5c485996 2021-07-28 op uint16_t l = len;
557 5c485996 2021-07-28 op len = htole16(len);
558 5c485996 2021-07-28 op evbuffer_add(evb, &len, sizeof(len));
559 5c485996 2021-07-28 op evbuffer_add(evb, str, l);
563 c5d518da 2021-07-29 op np_qid(struct qid *qid)
565 c5d518da 2021-07-29 op uint64_t path;
566 c5d518da 2021-07-29 op uint32_t vers;
568 c5d518da 2021-07-29 op path = htole64(qid->path);
569 c5d518da 2021-07-29 op vers = htole32(qid->vers);
571 c5d518da 2021-07-29 op evbuffer_add(evb, &qid->type, sizeof(qid->type));
572 8d448d55 2021-07-30 op evbuffer_add(evb, &vers, sizeof(vers));
573 8d448d55 2021-07-30 op evbuffer_add(evb, &path, sizeof(path));
577 2ef72ade 2021-07-28 op do_send(void)
582 2ef72ade 2021-07-28 op len = EVBUFFER_LENGTH(evb);
583 64c19d90 2021-07-30 op data = EVBUFFER_DATA(evb);
585 64c19d90 2021-07-30 op #if DEBUG_PACKETS
586 78b94752 2021-08-01 op hexdump("outgoing packet", data, len);
588 64c19d90 2021-07-30 op client_send_listener(IMSG_BUF, data, len);
589 2ef72ade 2021-07-28 op evbuffer_drain(evb, len);
593 5c485996 2021-07-28 op np_version(uint16_t tag, uint32_t msize, const char *version)
597 5c485996 2021-07-28 op l = strlen(version);
599 5c485996 2021-07-28 op msize = htole32(msize);
601 9ebb95a7 2021-07-30 op np_header(sizeof(msize) + sizeof(l) + l, Rversion, tag);
602 5c485996 2021-07-28 op evbuffer_add(evb, &msize, sizeof(msize));
603 5c485996 2021-07-28 op np_string(l, version);
608 c5d518da 2021-07-29 op np_attach(uint16_t tag, struct qid *qid)
610 9ebb95a7 2021-07-30 op np_header(QIDSIZE, Rattach, tag);
616 c377c1b9 2021-07-30 op np_clunk(uint16_t tag)
618 9ebb95a7 2021-07-30 op np_header(0, Rclunk, tag);
623 36b30273 2021-07-30 op np_flush(uint16_t tag)
625 9ebb95a7 2021-07-30 op np_header(0, Rflush, tag);
630 1c08fc54 2021-08-01 op np_walk(uint16_t tag, int nwqid, struct qid *wqid)
634 83f6b305 2021-08-01 op /* two bytes for the counter */
635 83f6b305 2021-08-01 op np_header(2 + QIDSIZE * nwqid, Rwalk, tag);
636 83f6b305 2021-08-01 op np_write16(nwqid);
637 1c08fc54 2021-08-01 op for (i = 0; i < nwqid; ++i)
638 1c08fc54 2021-08-01 op np_qid(wqid + i);
644 021481ca 2021-12-13 op np_open(uint16_t tag, struct qid *qid, uint32_t iounit)
646 021481ca 2021-12-13 op np_header(QIDSIZE + sizeof(iounit), Ropen, tag);
648 021481ca 2021-12-13 op np_write32(iounit);
653 baa40504 2021-12-15 op np_read(uint16_t tag, uint32_t count, void *data)
655 baa40504 2021-12-15 op np_header(sizeof(count) + count, Rread, tag);
656 baa40504 2021-12-15 op np_write32(count);
657 baa40504 2021-12-15 op np_writebuf(count, data);
662 2ef72ade 2021-07-28 op np_error(uint16_t tag, const char *errstr)
666 2ef72ade 2021-07-28 op l = strlen(errstr);
668 9ebb95a7 2021-07-30 op np_header(sizeof(l) + l, Rerror, tag);
669 5c485996 2021-07-28 op np_string(l, errstr);
674 c5d518da 2021-07-29 op np_errno(uint16_t tag)
676 9b088310 2021-07-30 op int saved_errno;
677 c5d518da 2021-07-29 op char buf[64];
679 9b088310 2021-07-30 op saved_errno = errno;
681 c5d518da 2021-07-29 op strerror_r(errno, buf, sizeof(buf));
682 c5d518da 2021-07-29 op np_error(tag, buf);
684 9b088310 2021-07-30 op errno = saved_errno;
688 48192874 2021-08-01 op np_read8(const char *t, const char *f, uint8_t *dst, const uint8_t **src,
691 48192874 2021-08-01 op if (*len < sizeof(*dst)) {
692 48192874 2021-08-01 op log_warnx("%s: wanted %zu bytes for the %s field but only "
693 48192874 2021-08-01 op "%zu are available.", t, sizeof(*dst), f, *len);
697 48192874 2021-08-01 op memcpy(dst, *src, sizeof(*dst));
698 48192874 2021-08-01 op *src += sizeof(*dst);
699 48192874 2021-08-01 op *len -= sizeof(*dst);
705 48192874 2021-08-01 op np_read16(const char *t, const char *f, uint16_t *dst, const uint8_t **src,
708 48192874 2021-08-01 op if (*len < sizeof(*dst)) {
709 48192874 2021-08-01 op log_warnx("%s: wanted %zu bytes for the %s field but only "
710 48192874 2021-08-01 op "%zu are available.", t, sizeof(*dst), f, *len);
714 48192874 2021-08-01 op memcpy(dst, *src, sizeof(*dst));
715 48192874 2021-08-01 op *src += sizeof(*dst);
716 48192874 2021-08-01 op *len -= sizeof(*dst);
717 48192874 2021-08-01 op *dst = le16toh(*dst);
723 48192874 2021-08-01 op np_read32(const char *t, const char *f, uint32_t *dst, const uint8_t **src,
726 48192874 2021-08-01 op if (*len < sizeof(*dst)) {
727 48192874 2021-08-01 op log_warnx("%s: wanted %zu bytes for the %s field but only "
728 48192874 2021-08-01 op "%zu are available.", t, sizeof(*dst), f, *len);
732 48192874 2021-08-01 op memcpy(dst, *src, sizeof(*dst));
733 48192874 2021-08-01 op *src += sizeof(*dst);
734 48192874 2021-08-01 op *len -= sizeof(*dst);
735 48192874 2021-08-01 op *dst = le32toh(*dst);
741 baa40504 2021-12-15 op np_read64(const char *t, const char *f, uint64_t *dst, const uint8_t **src,
744 baa40504 2021-12-15 op if (*len < sizeof(*dst)) {
745 baa40504 2021-12-15 op log_warnx("%s: wanted %zu bytes for the %s field but only "
746 baa40504 2021-12-15 op "%zu are available.", t, sizeof(*dst), f, *len);
750 baa40504 2021-12-15 op memcpy(dst, *src, sizeof(*dst));
751 baa40504 2021-12-15 op *src += sizeof(*dst);
752 baa40504 2021-12-15 op *len -= sizeof(*dst);
753 baa40504 2021-12-15 op *dst = le64toh(*dst);
759 48192874 2021-08-01 op np_readstr(const char *t, const char *f, char *res, size_t reslen,
760 48192874 2021-08-01 op const uint8_t **src, size_t *len)
763 48192874 2021-08-01 op char buf[32];
765 48192874 2021-08-01 op strlcpy(buf, f, sizeof(buf));
766 48192874 2021-08-01 op strlcat(buf, "-len", sizeof(buf));
768 48192874 2021-08-01 op if (!np_read16(t, buf, &sl, src, len))
769 48192874 2021-08-01 op return READSTRERR;
771 48192874 2021-08-01 op if (*len < sl) {
772 48192874 2021-08-01 op log_warnx("%s: wanted %d bytes for the %s field but only "
773 48192874 2021-08-01 op "%zu are available.", t, sl, f, *len);
774 48192874 2021-08-01 op return READSTRERR;
777 48192874 2021-08-01 op if (*len > reslen-1)
778 48192874 2021-08-01 op return READSTRTRUNC;
780 48192874 2021-08-01 op memcpy(res, *src, sl);
781 48192874 2021-08-01 op res[sl] = '\0';
789 e60f4e08 2021-07-30 op tversion(struct np_msg_header *hdr, const uint8_t *data, size_t len)
791 48192874 2021-08-01 op char *dot, version[32];
793 e60f4e08 2021-07-30 op if (handshaked)
796 3d20424d 2021-08-01 op /* msize[4] version[s] */
797 48192874 2021-08-01 op if (!NPREAD32("msize", &msize, &data, &len))
800 48192874 2021-08-01 op switch (NPREADSTR("version", version, sizeof(version), &data, &len)) {
801 48192874 2021-08-01 op case READSTRERR:
803 48192874 2021-08-01 op case READSTRTRUNC:
804 48192874 2021-08-01 op log_warnx("9P version string too long, truncated");
805 48192874 2021-08-01 op goto mismatch;
808 48192874 2021-08-01 op if ((dot = strchr(version, '.')) != NULL)
811 48192874 2021-08-01 op if (strcmp(version, VERSION9P) != 0 ||
813 48192874 2021-08-01 op goto mismatch;
815 48192874 2021-08-01 op /* version matched */
816 48192874 2021-08-01 op handshaked = 1;
817 e60f4e08 2021-07-30 op msize = MIN(msize, MSIZE9P);
818 e60f4e08 2021-07-30 op client_send_listener(IMSG_MSIZE, &msize, sizeof(msize));
819 e60f4e08 2021-07-30 op np_version(hdr->tag, msize, VERSION9P);
823 48192874 2021-08-01 op log_warnx("unknown 9P version string: \"%s\", want "VERSION9P,
825 48192874 2021-08-01 op np_version(hdr->tag, MSIZE9P, "unknown");
829 e60f4e08 2021-07-30 op client_send_listener(IMSG_CLOSE, NULL, 0);
830 e60f4e08 2021-07-30 op client_shutdown();
834 e60f4e08 2021-07-30 op tattach(struct np_msg_header *hdr, const uint8_t *data, size_t len)
836 e60f4e08 2021-07-30 op struct qid *qid;
837 e60f4e08 2021-07-30 op struct fid *f;
838 48192874 2021-08-01 op uint32_t fid, afid;
840 014adfc2 2021-07-30 op char aname[PATH_MAX];
842 e60f4e08 2021-07-30 op /* fid[4] afid[4] uname[s] aname[s] */
844 48192874 2021-08-01 op if (!NPREAD32("fid", &fid, &data, &len) ||
845 48192874 2021-08-01 op !NPREAD32("afid", &afid, &data, &len))
848 48192874 2021-08-01 op /* read the uname but don't actually use it */
849 48192874 2021-08-01 op switch (NPREADSTR("uname", aname, sizeof(aname), &data, &len)) {
850 48192874 2021-08-01 op case READSTRERR:
852 48192874 2021-08-01 op case READSTRTRUNC:
853 48192874 2021-08-01 op np_error(hdr->tag, "name too long");
857 48192874 2021-08-01 op switch (NPREADSTR("aname", aname, sizeof(aname), &data, &len)) {
858 48192874 2021-08-01 op case READSTRERR:
860 48192874 2021-08-01 op case READSTRTRUNC:
861 014adfc2 2021-07-30 op np_error(hdr->tag, "name too long");
865 cf758c33 2021-12-14 op if (fid_by_id(fid) != NULL || afid != NOFID) {
866 cf758c33 2021-12-14 op np_error(hdr->tag, "invalid fid or afid");
870 deb971f5 2021-08-12 op if ((fd = open(aname, O_RDONLY|O_DIRECTORY)) == -1)
873 deb971f5 2021-08-12 op if ((qid = qid_from_fd(fd, NULL, NULL)) == NULL)
876 cf758c33 2021-12-14 op log_debug("attached %s to %d", aname, fid);
878 e60f4e08 2021-07-30 op if ((f = new_fid(qid, fid)) == NULL) {
879 053e2652 2021-08-01 op qid_decref(qid);
883 e60f4e08 2021-07-30 op np_attach(hdr->tag, qid);
887 deb971f5 2021-08-12 op np_errno(hdr->tag);
888 deb971f5 2021-08-12 op log_warn("failed to attach %s", aname);
893 014adfc2 2021-07-30 op client_send_listener(IMSG_CLOSE, NULL, 0);
894 014adfc2 2021-07-30 op client_shutdown();
898 c377c1b9 2021-07-30 op tclunk(struct np_msg_header *hdr, const uint8_t *data, size_t len)
900 c377c1b9 2021-07-30 op struct fid *f;
901 c377c1b9 2021-07-30 op uint32_t fid;
904 48192874 2021-08-01 op if (!NPREAD32("fid", &fid, &data, &len)) {
905 c377c1b9 2021-07-30 op client_send_listener(IMSG_CLOSE, NULL, 0);
906 c377c1b9 2021-07-30 op client_shutdown();
910 c377c1b9 2021-07-30 op if ((f = fid_by_id(fid)) == NULL) {
911 c377c1b9 2021-07-30 op np_error(hdr->tag, "invalid fid");
916 c377c1b9 2021-07-30 op np_clunk(hdr->tag);
920 36b30273 2021-07-30 op tflush(struct np_msg_header *hdr, const uint8_t *data, size_t len)
922 36b30273 2021-07-30 op uint16_t oldtag;
925 36b30273 2021-07-30 op * We're doing only synchronous I/O. Tflush is implemented
926 36b30273 2021-07-30 op * only because it's illegal to reply with a Rerror.
929 36b30273 2021-07-30 op /* oldtag[2] */
930 36b30273 2021-07-30 op if (len != sizeof(oldtag)) {
931 36b30273 2021-07-30 op log_warnx("Tclunk with the wrong size: got %zu want %zu",
932 36b30273 2021-07-30 op len, sizeof(oldtag));
933 36b30273 2021-07-30 op client_send_listener(IMSG_CLOSE, NULL, 0);
934 36b30273 2021-07-30 op client_shutdown();
938 36b30273 2021-07-30 op np_flush(hdr->tag);
942 1c08fc54 2021-08-01 op twalk(struct np_msg_header *hdr, const uint8_t *data, size_t len)
944 deb971f5 2021-08-12 op struct stat sb;
945 053e2652 2021-08-01 op struct qid *qid, wqid[MAXWELEM] = {0};
946 1c08fc54 2021-08-01 op struct fid *f, *nf;
947 1c08fc54 2021-08-01 op uint32_t fid, newfid;
948 7ddddcfe 2021-08-01 op uint16_t nwname;
949 deb971f5 2021-08-12 op int fd, oldfd, no, nwqid = 0;
950 deb971f5 2021-08-12 op char wnam[PATH_MAX+1];
952 48192874 2021-08-01 op if (!NPREAD32("fid", &fid, &data, &len) ||
953 48192874 2021-08-01 op !NPREAD32("newfid", &newfid, &data, &len) ||
954 48192874 2021-08-01 op !NPREAD16("nwname", &nwname, &data, &len))
957 1c08fc54 2021-08-01 op if (nwname > MAXWELEM) {
958 1c08fc54 2021-08-01 op log_warnx("Twalk: more than %d path elements: %d",
959 1c08fc54 2021-08-01 op MAXWELEM, nwname);
963 1c08fc54 2021-08-01 op if ((f = fid_by_id(fid)) == NULL) {
964 1c08fc54 2021-08-01 op np_error(hdr->tag, "invalid fid");
968 99a43590 2021-12-14 op if (f->fd != -1) {
969 47c9b8b8 2021-12-02 op np_error(hdr->tag, "fid already opened for I/O");
973 1c08fc54 2021-08-01 op if (fid == newfid)
975 1c08fc54 2021-08-01 op else if ((nf = fid_by_id(newfid)) != NULL) {
976 1c08fc54 2021-08-01 op np_error(hdr->tag, "newfid already in use");
981 913eba5c 2021-08-01 op /* special case: fid duplication */
982 22dfb5a0 2021-08-01 op if (nwname == 0) {
984 1c08fc54 2021-08-01 op * TODO: should we forbid fids duplication when fid ==
987 f987557c 2021-12-02 op if (nf == NULL && (nf = new_fid(f->qid, newfid)) == NULL)
988 f987557c 2021-12-02 op fatal("new_fid duplication");
990 1c08fc54 2021-08-01 op np_walk(hdr->tag, 1, f->qid);
994 053e2652 2021-08-01 op if (f->qid->type != QTDIR) {
995 053e2652 2021-08-01 op np_error(hdr->tag, "fid doesn't represent a directory");
999 deb971f5 2021-08-12 op oldfd = f->qid->fd;
1001 1c08fc54 2021-08-01 op for (nwqid = 0; nwqid < nwname; nwqid++) {
1002 053e2652 2021-08-01 op switch (NPREADSTR("wname", wnam, sizeof(wnam), &data, &len)) {
1003 48192874 2021-08-01 op case READSTRERR:
1005 48192874 2021-08-01 op case READSTRTRUNC:
1006 1c08fc54 2021-08-01 op np_error(hdr->tag, "wname too long");
1010 054cd6b9 2021-12-14 op if (*wnam == '\0' ||
1011 054cd6b9 2021-12-14 op strchr(wnam, '/') != NULL ||
1012 054cd6b9 2021-12-14 op !strcmp(wnam, ".")) {
1013 054cd6b9 2021-12-14 op errno = EINVAL;
1014 054cd6b9 2021-12-14 op goto cantopen;
1017 deb971f5 2021-08-12 op if ((fd = openat(oldfd, wnam, O_RDONLY|O_DIRECTORY)) == -1 &&
1018 3242c0bc 2021-12-14 op errno != ENOTDIR)
1019 deb971f5 2021-08-12 op goto cantopen;
1021 deb971f5 2021-08-12 op if ((fd == -1 && fstatat(oldfd, wnam, &sb, 0) == -1) ||
1022 3242c0bc 2021-12-14 op (fd != -1 && fstat(fd, &sb) == -1))
1023 deb971f5 2021-08-12 op goto cantopen;
1025 deb971f5 2021-08-12 op qid_update_from_sb(&wqid[nwqid], &sb);
1027 deb971f5 2021-08-12 op /* reached a file but we still have other components */
1028 deb971f5 2021-08-12 op if (fd == -1 && nwqid+1 < nwname)
1029 deb971f5 2021-08-12 op goto cantopen;
1031 deb971f5 2021-08-12 op /* reached the end and found a file */
1032 deb971f5 2021-08-12 op if (fd == -1 && nwqid+1 == nwname)
1035 deb971f5 2021-08-12 op if (oldfd != f->qid->fd)
1036 deb971f5 2021-08-12 op close(oldfd);
1041 deb971f5 2021-08-12 op * There can be two possibilities: fd == -1 means that we've
1042 deb971f5 2021-08-12 op * reached a file and we should save BOTH the dirfd (oldfd)
1043 deb971f5 2021-08-12 op * and the path (wnam); or we just reached another directory,
1044 deb971f5 2021-08-12 op * in which case we can just create a new qid using fd.
1046 deb971f5 2021-08-12 op if (fd == -1)
1047 deb971f5 2021-08-12 op qid = qid_from_fd(oldfd, wnam, &sb);
1049 deb971f5 2021-08-12 op qid = qid_from_fd(oldfd, NULL, &sb);
1050 deb971f5 2021-08-12 op if (qid == NULL)
1051 eafb23b1 2021-08-01 op fatal("qid_from_fd");
1053 1c08fc54 2021-08-01 op if (nf == NULL) {
1054 1c08fc54 2021-08-01 op if ((nf = new_fid(qid, newfid)) == NULL)
1055 1c08fc54 2021-08-01 op fatal("new_fid");
1057 1c08fc54 2021-08-01 op /* swap qid */
1058 1c08fc54 2021-08-01 op qid_decref(nf->qid);
1059 1c08fc54 2021-08-01 op nf->qid = qid_incref(qid);
1062 1c08fc54 2021-08-01 op np_walk(hdr->tag, nwqid, wqid);
1066 deb971f5 2021-08-12 op if (oldfd != f->qid->fd)
1067 deb971f5 2021-08-12 op close(oldfd);
1069 deb971f5 2021-08-12 op if (nwqid == 0)
1070 deb971f5 2021-08-12 op np_error(hdr->tag, strerror(no));
1072 deb971f5 2021-08-12 op np_walk(hdr->tag, nwqid, wqid);
1076 1c08fc54 2021-08-01 op client_send_listener(IMSG_CLOSE, NULL, 0);
1077 1c08fc54 2021-08-01 op client_shutdown();
1080 021481ca 2021-12-13 op static inline int
1081 021481ca 2021-12-13 op npmode_to_unix(uint8_t mode, int *flags)
1083 021481ca 2021-12-13 op switch (mode & 0x0F) {
1084 021481ca 2021-12-13 op case KOREAD:
1085 021481ca 2021-12-13 op *flags = O_RDONLY;
1087 021481ca 2021-12-13 op case KOWRITE:
1088 021481ca 2021-12-13 op *flags = O_WRONLY;
1090 021481ca 2021-12-13 op case KORDWR:
1091 021481ca 2021-12-13 op *flags = O_RDWR;
1093 021481ca 2021-12-13 op case KOEXEC:
1094 021481ca 2021-12-13 op log_warnx("tried to open something with KOEXEC");
1095 021481ca 2021-12-13 op /* fallthrough */
1100 021481ca 2021-12-13 op if (mode & KOTRUNC)
1101 021481ca 2021-12-13 op *flags |= O_TRUNC;
1102 021481ca 2021-12-13 op if (mode & KORCLOSE)
1103 021481ca 2021-12-13 op *flags |= O_CLOEXEC;
1109 021481ca 2021-12-13 op topen(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1111 021481ca 2021-12-13 op struct stat sb;
1112 021481ca 2021-12-13 op struct qid qid;
1113 021481ca 2021-12-13 op struct fid *f;
1114 021481ca 2021-12-13 op uint32_t fid;
1115 021481ca 2021-12-13 op uint8_t mode;
1116 87a818e8 2021-12-14 op const char *path;
1118 021481ca 2021-12-13 op /* fid[4] mode[1] */
1119 021481ca 2021-12-13 op if (!NPREAD32("fid", &fid, &data, &len) ||
1120 021481ca 2021-12-13 op !NPREAD8("mode", &mode, &data, &len)) {
1121 021481ca 2021-12-13 op client_send_listener(IMSG_CLOSE, NULL, 0);
1122 021481ca 2021-12-13 op client_shutdown();
1126 021481ca 2021-12-13 op if ((f = fid_by_id(fid)) == NULL || f->fd != -1) {
1127 021481ca 2021-12-13 op np_error(hdr->tag, "invalid fid");
1131 d4e43815 2021-12-15 op if (npmode_to_unix(mode, &f->iomode) == -1) {
1132 d4e43815 2021-12-15 op np_error(hdr->tag, "invalid mode");
1136 d4e43815 2021-12-15 op if (f->qid->type & QTDIR &&
1137 d4e43815 2021-12-15 op (f->iomode & O_WRONLY || f->iomode & O_RDWR)) {
1138 d4e43815 2021-12-15 op np_error(hdr->tag, "can't open directory for writing");
1142 87a818e8 2021-12-14 op path = f->qid->fpath;
1143 87a818e8 2021-12-14 op if (*path == '\0')
1146 87a818e8 2021-12-14 op if ((f->fd = openat(f->qid->fd, path, f->iomode)) == -1) {
1147 021481ca 2021-12-13 op np_error(hdr->tag, strerror(errno));
1151 021481ca 2021-12-13 op if (fstat(f->fd, &sb) == -1)
1152 021481ca 2021-12-13 op fatal("fstat");
1154 baa40504 2021-12-15 op if (S_ISDIR(sb.st_mode)) {
1155 baa40504 2021-12-15 op assert(f->qid->type & QTDIR);
1156 baa40504 2021-12-15 op if ((f->dir = fdopendir(f->fd)) == NULL) {
1157 baa40504 2021-12-15 op np_errno(hdr->tag);
1158 baa40504 2021-12-15 op close(f->fd);
1163 c0ae57b9 2021-12-15 op if ((f->evb = evbuffer_new()) == NULL) {
1164 c0ae57b9 2021-12-15 op np_errno(hdr->tag);
1165 c0ae57b9 2021-12-15 op closedir(f->dir);
1166 c0ae57b9 2021-12-15 op f->dir = NULL;
1171 baa40504 2021-12-15 op f->offset = 0;
1173 021481ca 2021-12-13 op qid_update_from_sb(&qid, &sb);
1174 021481ca 2021-12-13 op np_open(hdr->tag, &qid, sb.st_blksize);
1178 baa40504 2021-12-15 op tread(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1180 baa40504 2021-12-15 op struct fid *f;
1182 baa40504 2021-12-15 op uint64_t off;
1183 baa40504 2021-12-15 op uint32_t fid, count;
1184 baa40504 2021-12-15 op char buf[2048];
1186 baa40504 2021-12-15 op /* fid[4] offset[8] count[4] */
1187 baa40504 2021-12-15 op if (!NPREAD32("fid", &fid, &data, &len) ||
1188 baa40504 2021-12-15 op !NPREAD64("offset", &off, &data, &len) ||
1189 baa40504 2021-12-15 op !NPREAD32("count", &count, &data, &len)) {
1190 baa40504 2021-12-15 op client_send_listener(IMSG_CLOSE, NULL, 0);
1191 baa40504 2021-12-15 op client_shutdown();
1195 5f362b7f 2021-12-15 op if ((f = fid_by_id(fid)) == NULL || f->fd == -1) {
1196 baa40504 2021-12-15 op np_error(hdr->tag, "invalid fid");
1200 baa40504 2021-12-15 op if (TYPE_OVERFLOW(off_t, off)) {
1201 baa40504 2021-12-15 op log_warnx("unexpected size_t size");
1202 baa40504 2021-12-15 op np_error(hdr->tag, "invalid offset");
1206 baa40504 2021-12-15 op if (f->dir == NULL) {
1207 baa40504 2021-12-15 op /* read a file */
1208 baa40504 2021-12-15 op r = pread(f->fd, buf, sizeof(buf), (off_t)off);
1209 baa40504 2021-12-15 op if (r == -1)
1210 baa40504 2021-12-15 op np_errno(hdr->tag);
1212 baa40504 2021-12-15 op np_read(hdr->tag, r, buf);
1214 baa40504 2021-12-15 op /* read dirents */
1215 baa40504 2021-12-15 op np_error(hdr->tag, "not implemented yet");
1220 e60f4e08 2021-07-30 op handle_message(struct imsg *imsg, size_t len)
1222 e60f4e08 2021-07-30 op struct msg {
1223 e60f4e08 2021-07-30 op uint8_t type;
1224 e60f4e08 2021-07-30 op void (*fn)(struct np_msg_header *, const uint8_t *, size_t);
1225 e60f4e08 2021-07-30 op } msgs[] = {
1226 e60f4e08 2021-07-30 op {Tversion, tversion},
1227 e60f4e08 2021-07-30 op {Tattach, tattach},
1228 c377c1b9 2021-07-30 op {Tclunk, tclunk},
1229 36b30273 2021-07-30 op {Tflush, tflush},
1230 1c08fc54 2021-08-01 op {Twalk, twalk},
1231 021481ca 2021-12-13 op {Topen, topen},
1232 baa40504 2021-12-15 op {Tread, tread},
1234 e60f4e08 2021-07-30 op struct np_msg_header hdr;
1236 e60f4e08 2021-07-30 op uint8_t *data;
1238 64c19d90 2021-07-30 op #if DEBUG_PACKETS
1239 78b94752 2021-08-01 op hexdump("incoming packet", imsg->data, len);
1242 e60f4e08 2021-07-30 op parse_message(imsg->data, len, &hdr, &data);
1243 e60f4e08 2021-07-30 op len -= HEADERSIZE;
1245 e60f4e08 2021-07-30 op log_debug("got request: len=%d type=%d[%s] tag=%d",
1246 e60f4e08 2021-07-30 op hdr.len, hdr.type, pp_msg_type(hdr.type), hdr.tag);
1248 e60f4e08 2021-07-30 op if (!handshaked && hdr.type != Tversion) {
1249 e60f4e08 2021-07-30 op client_send_listener(IMSG_CLOSE, NULL, 0);
1250 e60f4e08 2021-07-30 op client_shutdown();
1254 e60f4e08 2021-07-30 op for (i = 0; i < sizeof(msgs)/sizeof(msgs[0]); ++i) {
1255 e60f4e08 2021-07-30 op if (msgs[i].type != hdr.type)
1258 e60f4e08 2021-07-30 op msgs[i].fn(&hdr, data, len);
1262 e60f4e08 2021-07-30 op np_error(hdr.tag, "Not supported.");