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 20c188ce 2021-12-16 op char fpath[PATH_MAX];
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;
82 5585f1c3 2021-12-16 op * expected offset for Tread against a directory.
84 baa40504 2021-12-15 op uint64_t offset;
86 c5d518da 2021-07-29 op struct qid *qid;
87 c5d518da 2021-07-29 op STAILQ_ENTRY(fid) entries;
90 8d1b399b 2021-07-22 op static struct imsgev *iev_listener;
91 2ef72ade 2021-07-28 op static struct evbuffer *evb;
92 2ef72ade 2021-07-28 op static uint32_t peerid;
94 cf758c33 2021-12-14 op static int handshaked;
95 5c485996 2021-07-28 op uint32_t msize;
97 06a84967 2021-07-22 op static ATTR_DEAD void client_shutdown(void);
98 8d1b399b 2021-07-22 op static void client_sig_handler(int, short, void *);
99 8d1b399b 2021-07-22 op static void client_dispatch_listener(int, short, void *);
100 8d1b399b 2021-07-22 op static void client_privdrop(const char *, const char *);
102 2ef72ade 2021-07-28 op static int client_send_listener(int, const void *, uint16_t);
104 deb971f5 2021-08-12 op static void qid_update_from_sb(struct qid *, struct stat *);
105 deb971f5 2021-08-12 op static struct qid *qid_from_fd(int, const char *, struct stat *);
106 c377c1b9 2021-07-30 op static struct qid *qid_incref(struct qid *);
107 c377c1b9 2021-07-30 op static void qid_decref(struct qid *);
109 c5d518da 2021-07-29 op static struct fid *new_fid(struct qid *, uint32_t);
110 c377c1b9 2021-07-30 op static struct fid *fid_by_id(uint32_t);
111 c377c1b9 2021-07-30 op static void free_fid(struct fid *);
113 48192874 2021-08-01 op static void parse_message(const uint8_t *, size_t,
114 48192874 2021-08-01 op struct np_msg_header *, uint8_t **);
116 5585f1c3 2021-12-16 op static void np_write16(struct evbuffer *, uint16_t);
117 5585f1c3 2021-12-16 op static void np_write32(struct evbuffer *, uint32_t);
118 5585f1c3 2021-12-16 op static void np_write64(struct evbuffer *, uint64_t);
119 2845cccb 2021-07-29 op static void np_header(uint32_t, uint8_t, uint16_t);
120 5585f1c3 2021-12-16 op static void np_string(struct evbuffer *, uint16_t, const char *);
121 5585f1c3 2021-12-16 op static void np_qid(struct evbuffer *, struct qid *);
122 84b10f04 2021-07-29 op static void do_send(void);
124 2845cccb 2021-07-29 op static void np_version(uint16_t, uint32_t, const char *);
125 c5d518da 2021-07-29 op static void np_attach(uint16_t, struct qid *);
126 c377c1b9 2021-07-30 op static void np_clunk(uint16_t);
127 36b30273 2021-07-30 op static void np_flush(uint16_t);
128 1c08fc54 2021-08-01 op static void np_walk(uint16_t, int, struct qid *);
129 021481ca 2021-12-13 op static void np_open(uint16_t, struct qid *, uint32_t);
130 baa40504 2021-12-15 op static void np_read(uint16_t, uint32_t, void *);
131 2483be55 2021-12-16 op static void np_write(uint16_t, uint32_t);
132 3a2c53f5 2021-12-18 op static void np_stat(uint16_t, uint32_t, void *);
133 2ef72ade 2021-07-28 op static void np_error(uint16_t, const char *);
134 c5d518da 2021-07-29 op static void np_errno(uint16_t);
136 48192874 2021-08-01 op static int np_read8(const char *, const char *, uint8_t *,
137 48192874 2021-08-01 op const uint8_t **, size_t *);
138 48192874 2021-08-01 op static int np_read16(const char *, const char *, uint16_t *,
139 48192874 2021-08-01 op const uint8_t **, size_t *);
140 48192874 2021-08-01 op static int np_read32(const char *, const char *, uint32_t *,
141 48192874 2021-08-01 op const uint8_t **, size_t *);
142 baa40504 2021-12-15 op static int np_read64(const char *, const char *, uint64_t *,
143 baa40504 2021-12-15 op const uint8_t **, size_t *);
145 48192874 2021-08-01 op #define READSTRERR -1
146 48192874 2021-08-01 op #define READSTRTRUNC -2
147 48192874 2021-08-01 op static int np_readstr(const char *, const char *, char *, size_t,
148 48192874 2021-08-01 op const uint8_t **, size_t *);
150 48192874 2021-08-01 op #define NPREAD8(f, dst, src, len) np_read8(__func__, f, dst, src, len)
151 48192874 2021-08-01 op #define NPREAD16(f, dst, src, len) np_read16(__func__, f, dst, src, len)
152 48192874 2021-08-01 op #define NPREAD32(f, dst, src, len) np_read32(__func__, f, dst, src, len)
153 baa40504 2021-12-15 op #define NPREAD64(f, dst, src, len) np_read64(__func__, f, dst, src, len)
155 48192874 2021-08-01 op #define NPREADSTR(f, b, bl, src, len) np_readstr(__func__, f, b, bl, src, len)
157 e60f4e08 2021-07-30 op static void tversion(struct np_msg_header *, const uint8_t *, size_t);
158 e60f4e08 2021-07-30 op static void tattach(struct np_msg_header *, const uint8_t *, size_t);
159 c377c1b9 2021-07-30 op static void tclunk(struct np_msg_header *, const uint8_t *, size_t);
160 36b30273 2021-07-30 op static void tflush(struct np_msg_header *, const uint8_t *, size_t);
161 1c08fc54 2021-08-01 op static void twalk(struct np_msg_header *, const uint8_t *, size_t);
162 021481ca 2021-12-13 op static void topen(struct np_msg_header *, const uint8_t *, size_t);
163 baa40504 2021-12-15 op static void tread(struct np_msg_header *, const uint8_t *, size_t);
164 2483be55 2021-12-16 op static void twrite(struct np_msg_header *, const uint8_t *, size_t);
165 3a2c53f5 2021-12-18 op static void tstat(struct np_msg_header *, const uint8_t *, size_t);
166 e60f4e08 2021-07-30 op static void handle_message(struct imsg *, size_t);
168 06a84967 2021-07-22 op ATTR_DEAD void
169 8d1b399b 2021-07-22 op client(int debug, int verbose)
171 8d1b399b 2021-07-22 op struct event ev_sigint, ev_sigterm;
173 8d1b399b 2021-07-22 op log_init(debug, LOG_DAEMON);
174 8d1b399b 2021-07-22 op log_setverbose(verbose);
176 8d1b399b 2021-07-22 op setproctitle("client");
177 8d1b399b 2021-07-22 op log_procinit("client");
179 8d1b399b 2021-07-22 op log_debug("warming up");
181 8d1b399b 2021-07-22 op event_init();
183 8d1b399b 2021-07-22 op /* Setup signal handlers */
184 8d1b399b 2021-07-22 op signal_set(&ev_sigint, SIGINT, client_sig_handler, NULL);
185 8d1b399b 2021-07-22 op signal_set(&ev_sigterm, SIGTERM, client_sig_handler, NULL);
187 8d1b399b 2021-07-22 op signal_add(&ev_sigint, NULL);
188 8d1b399b 2021-07-22 op signal_add(&ev_sigterm, NULL);
190 8d1b399b 2021-07-22 op signal(SIGPIPE, SIG_IGN);
191 8d1b399b 2021-07-22 op signal(SIGHUP, SIG_IGN);
193 8d1b399b 2021-07-22 op /* Setup pipe and event handler to the listener process */
194 8d1b399b 2021-07-22 op if ((iev_listener = malloc(sizeof(*iev_listener))) == NULL)
197 8d1b399b 2021-07-22 op imsg_init(&iev_listener->ibuf, 3);
198 8d1b399b 2021-07-22 op iev_listener->handler = client_dispatch_listener;
200 8d1b399b 2021-07-22 op /* Setup event handlers. */
201 8d1b399b 2021-07-22 op iev_listener->events = EV_READ;
202 8d1b399b 2021-07-22 op event_set(&iev_listener->ev, iev_listener->ibuf.fd,
203 8d1b399b 2021-07-22 op iev_listener->events, iev_listener->handler, iev_listener);
204 8d1b399b 2021-07-22 op event_add(&iev_listener->ev, NULL);
206 8d1b399b 2021-07-22 op event_dispatch();
207 8d1b399b 2021-07-22 op client_shutdown();
210 06a84967 2021-07-22 op static ATTR_DEAD void
211 8d1b399b 2021-07-22 op client_shutdown(void)
213 2ef72ade 2021-07-28 op if (evb != NULL)
214 2ef72ade 2021-07-28 op evbuffer_free(evb);
216 8d1b399b 2021-07-22 op msgbuf_clear(&iev_listener->ibuf.w);
217 8d1b399b 2021-07-22 op close(iev_listener->ibuf.fd);
219 8d1b399b 2021-07-22 op free(iev_listener);
221 223c9e73 2021-08-06 op log_debug("client exiting");
226 8d1b399b 2021-07-22 op client_sig_handler(int sig, short event, void *d)
229 8d1b399b 2021-07-22 op * Normal signal handler rules don't apply because libevent
230 8d1b399b 2021-07-22 op * decouples for us.
233 8d1b399b 2021-07-22 op switch (sig) {
235 8d1b399b 2021-07-22 op case SIGTERM:
236 8d1b399b 2021-07-22 op client_shutdown();
238 8d1b399b 2021-07-22 op fatalx("unexpected signal %d", sig);
242 8d1b399b 2021-07-22 op #define AUTH_NONE 0
243 8d1b399b 2021-07-22 op #define AUTH_USER 1
244 8d1b399b 2021-07-22 op #define AUTH_DONE 2
247 8d1b399b 2021-07-22 op client_dispatch_listener(int fd, short event, void *d)
249 8d1b399b 2021-07-22 op static int auth = AUTH_NONE;
250 8d1b399b 2021-07-22 op static char username[64] = {0};
251 8d1b399b 2021-07-22 op static char dir[PATH_MAX] = {0};
252 8d1b399b 2021-07-22 op struct imsg imsg;
253 8d1b399b 2021-07-22 op struct imsgev *iev = d;
254 8d1b399b 2021-07-22 op struct imsgbuf *ibuf;
256 8d1b399b 2021-07-22 op int shut = 0;
258 8d1b399b 2021-07-22 op ibuf = &iev->ibuf;
260 8d1b399b 2021-07-22 op if (event & EV_READ) {
261 8d1b399b 2021-07-22 op if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
262 8d1b399b 2021-07-22 op fatal("imsg_read error");
263 8d1b399b 2021-07-22 op if (n == 0) /* Connection closed */
266 8d1b399b 2021-07-22 op if (event & EV_WRITE) {
267 8d1b399b 2021-07-22 op if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
268 8d1b399b 2021-07-22 op fatal("msgbuf_write");
269 8d1b399b 2021-07-22 op if (n == 0) /* Connection closed */
274 8d1b399b 2021-07-22 op if ((n = imsg_get(ibuf, &imsg)) == -1)
275 8d1b399b 2021-07-22 op fatal("%s: imsg_get error", __func__);
276 8d1b399b 2021-07-22 op if (n == 0) /* No more messages. */
279 8d1b399b 2021-07-22 op switch (imsg.hdr.type) {
280 8d1b399b 2021-07-22 op case IMSG_AUTH:
281 2ef72ade 2021-07-28 op peerid = imsg.hdr.peerid;
283 8d1b399b 2021-07-22 op fatalx("%s: IMSG_AUTH already done", __func__);
284 8d1b399b 2021-07-22 op auth = AUTH_USER;
285 8d1b399b 2021-07-22 op ((char *)imsg.data)[IMSG_DATA_SIZE(imsg)-1] = '\0';
286 8d1b399b 2021-07-22 op strlcpy(username, imsg.data, sizeof(username));
288 8d1b399b 2021-07-22 op case IMSG_AUTH_DIR:
289 8d1b399b 2021-07-22 op if (auth != AUTH_USER)
290 8d1b399b 2021-07-22 op fatalx("%s: IMSG_AUTH_DIR not after IMSG_AUTH",
292 8d1b399b 2021-07-22 op auth = AUTH_DONE;
293 8d1b399b 2021-07-22 op ((char *)imsg.data)[IMSG_DATA_SIZE(imsg)-1] = '\0';
294 8d1b399b 2021-07-22 op strlcpy(dir, imsg.data, sizeof(dir));
295 8d1b399b 2021-07-22 op client_privdrop(username, dir);
296 8d1b399b 2021-07-22 op memset(username, 0, sizeof(username));
297 8d1b399b 2021-07-22 op memset(dir, 0, sizeof(username));
299 8d1b399b 2021-07-22 op case IMSG_BUF:
302 23e03c88 2021-07-26 op fatalx("%s: can't handle messages before"
303 23e03c88 2021-07-26 op " doing the auth", __func__);
304 23e03c88 2021-07-26 op handle_message(&imsg, IMSG_DATA_SIZE(imsg));
306 8d1b399b 2021-07-22 op case IMSG_CONN_GONE:
307 8d1b399b 2021-07-22 op log_debug("closing");
311 8d1b399b 2021-07-22 op log_debug("%s: unexpected imsg %d",
312 8d1b399b 2021-07-22 op __func__, imsg.hdr.type);
315 8d1b399b 2021-07-22 op imsg_free(&imsg);
319 8d1b399b 2021-07-22 op imsg_event_add(iev);
321 8d1b399b 2021-07-22 op /* This pipe is dead. Remove its event handler. */
322 8d1b399b 2021-07-22 op event_del(&iev->ev);
323 223c9e73 2021-08-06 op log_debug("pipe closed, shutting down...");
324 8d1b399b 2021-07-22 op event_loopexit(NULL);
329 8d1b399b 2021-07-22 op client_privdrop(const char *username, const char *dir)
331 8d1b399b 2021-07-22 op struct passwd *pw;
333 8d1b399b 2021-07-22 op setproctitle("client %s", username);
335 8d1b399b 2021-07-22 op if ((pw = getpwnam(username)) == NULL)
336 8d1b399b 2021-07-22 op fatalx("getpwnam(%s) failed", username);
338 8d1b399b 2021-07-22 op if (chroot(dir) == -1)
339 8d1b399b 2021-07-22 op fatal("chroot");
340 8d1b399b 2021-07-22 op if (chdir("/") == -1)
341 8d1b399b 2021-07-22 op fatal("chdir(\"/\")");
343 8d1b399b 2021-07-22 op if (setgroups(1, &pw->pw_gid) ||
344 8d1b399b 2021-07-22 op setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
345 8d1b399b 2021-07-22 op setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
346 8d1b399b 2021-07-22 op fatal("can't drop privileges");
348 8d1b399b 2021-07-22 op sandbox_client();
349 66a9a40a 2021-07-28 op log_debug("client ready; user=%s dir=%s", username, dir);
351 2ef72ade 2021-07-28 op if ((evb = evbuffer_new()) == NULL)
352 2ef72ade 2021-07-28 op fatal("evbuffer_new");
356 2ef72ade 2021-07-28 op client_send_listener(int type, const void *data, uint16_t len)
360 8d1b399b 2021-07-22 op if ((ret = imsg_compose(&iev_listener->ibuf, type, peerid, 0, -1,
361 8d1b399b 2021-07-22 op data, len)) != -1)
362 8d1b399b 2021-07-22 op imsg_event_add(iev_listener);
367 deb971f5 2021-08-12 op /* set qid fields from sb */
369 deb971f5 2021-08-12 op qid_update_from_sb(struct qid *qid, struct stat *sb)
371 deb971f5 2021-08-12 op qid->path = sb->st_ino;
374 1c08fc54 2021-08-01 op * Theoretically (and hopefully!) this should be a 64 bit
375 1c08fc54 2021-08-01 op * number. Unfortunately, 9P uses 32 bit timestamps.
377 deb971f5 2021-08-12 op qid->vers = sb->st_mtim.tv_sec;
379 deb971f5 2021-08-12 op if (S_ISREG(sb->st_mode))
380 1c08fc54 2021-08-01 op qid->type = QTFILE;
381 deb971f5 2021-08-12 op else if (S_ISDIR(sb->st_mode))
382 1c08fc54 2021-08-01 op qid->type = QTDIR;
383 deb971f5 2021-08-12 op else if (S_ISLNK(sb->st_mode))
384 1c08fc54 2021-08-01 op qid->type = QTSYMLINK;
387 c5d518da 2021-07-29 op /* creates a qid given a fd */
388 c5d518da 2021-07-29 op static struct qid *
389 deb971f5 2021-08-12 op qid_from_fd(int fd, const char *path, struct stat *s)
391 c5d518da 2021-07-29 op struct qid *qid;
392 deb971f5 2021-08-12 op struct stat sb;
395 c5d518da 2021-07-29 op if ((qid = calloc(1, sizeof(*qid))) == NULL)
398 deb971f5 2021-08-12 op if (path != NULL)
399 deb971f5 2021-08-12 op strlcpy(qid->fpath, path, sizeof(qid->fpath));
401 c5d518da 2021-07-29 op qid->fd = fd;
403 deb971f5 2021-08-12 op if (s == NULL) {
405 deb971f5 2021-08-12 op if (path == NULL)
406 deb971f5 2021-08-12 op r = fstat(fd, s);
408 deb971f5 2021-08-12 op r = fstatat(fd, path, s, 0);
409 deb971f5 2021-08-12 op if (r == -1) {
415 deb971f5 2021-08-12 op qid_update_from_sb(qid, s);
417 053e2652 2021-08-01 op STAILQ_INSERT_HEAD(&qids, qid, entries);
422 053e2652 2021-08-01 op static struct qid *
423 c377c1b9 2021-07-30 op qid_incref(struct qid *qid)
425 c377c1b9 2021-07-30 op qid->refcount++;
430 c377c1b9 2021-07-30 op qid_decref(struct qid *qid)
432 c377c1b9 2021-07-30 op if (--qid->refcount > 0)
435 c377c1b9 2021-07-30 op STAILQ_REMOVE(&qids, qid, qid, entries);
437 c377c1b9 2021-07-30 op close(qid->fd);
441 c5d518da 2021-07-29 op static struct fid *
442 c5d518da 2021-07-29 op new_fid(struct qid *qid, uint32_t fid)
444 c5d518da 2021-07-29 op struct fid *f;
446 c5d518da 2021-07-29 op if ((f = calloc(1, sizeof(*f))) == NULL)
449 c377c1b9 2021-07-30 op f->qid = qid_incref(qid);
450 c5d518da 2021-07-29 op f->fid = fid;
453 c5d518da 2021-07-29 op STAILQ_INSERT_HEAD(&fids, f, entries);
458 c377c1b9 2021-07-30 op static struct fid *
459 c377c1b9 2021-07-30 op fid_by_id(uint32_t fid)
461 c377c1b9 2021-07-30 op struct fid *f;
463 c377c1b9 2021-07-30 op STAILQ_FOREACH(f, &fids, entries) {
464 c377c1b9 2021-07-30 op if (f->fid == fid)
472 c377c1b9 2021-07-30 op free_fid(struct fid *f)
476 021481ca 2021-12-13 op if (f->fd != -1) {
477 baa40504 2021-12-15 op if (f->dir != NULL)
478 baa40504 2021-12-15 op r = closedir(f->dir);
480 baa40504 2021-12-15 op r = close(f->fd);
483 baa40504 2021-12-15 op fatal("can't close fid %d", f->fid);
485 1e8d72fd 2021-12-15 op if (f->evb != NULL)
486 1e8d72fd 2021-12-15 op evbuffer_free(f->evb);
488 021481ca 2021-12-13 op /* try to honour ORCLOSE if requested */
489 021481ca 2021-12-13 op if (f->iomode & O_CLOEXEC)
490 021481ca 2021-12-13 op unlinkat(f->qid->fd, f->qid->fpath, 0);
493 c377c1b9 2021-07-30 op qid_decref(f->qid);
495 c377c1b9 2021-07-30 op STAILQ_REMOVE(&fids, f, fid, entries);
500 48192874 2021-08-01 op parse_message(const uint8_t *data, size_t len, struct np_msg_header *hdr,
501 5c485996 2021-07-28 op uint8_t **cnt)
503 48192874 2021-08-01 op size_t olen = len;
505 48192874 2021-08-01 op if (!NPREAD32("len", &hdr->len, &data, &len) ||
506 48192874 2021-08-01 op !NPREAD8("type", &hdr->type, &data, &len) ||
507 48192874 2021-08-01 op !NPREAD16("tag", &hdr->tag, &data, &len))
510 48192874 2021-08-01 op if (olen != hdr->len)
513 23e03c88 2021-07-26 op if (hdr->type < Tversion ||
514 23e03c88 2021-07-26 op hdr->type >= Tmax ||
515 23e03c88 2021-07-26 op hdr->type == Terror ||
516 23e03c88 2021-07-26 op (hdr->type & 0x1) != 0) /* cannot recv a R* */
519 23e03c88 2021-07-26 op hdr->tag = le32toh(hdr->tag);
521 48192874 2021-08-01 op *cnt = (uint8_t *)data;
525 23e03c88 2021-07-26 op /* TODO: send a proper message to terminate the connection. */
526 23e03c88 2021-07-26 op fatalx("got invalid message");
530 5585f1c3 2021-12-16 op np_write16(struct evbuffer *e, uint16_t x)
532 83f6b305 2021-08-01 op x = htole16(x);
533 5585f1c3 2021-12-16 op evbuffer_add(e, &x, sizeof(x));
537 5585f1c3 2021-12-16 op np_write32(struct evbuffer *e, uint32_t x)
539 021481ca 2021-12-13 op x = htole32(x);
540 5585f1c3 2021-12-16 op evbuffer_add(e, &x, sizeof(x));
544 5585f1c3 2021-12-16 op np_write64(struct evbuffer *e, uint64_t x)
546 5585f1c3 2021-12-16 op x = htole64(x);
547 5585f1c3 2021-12-16 op evbuffer_add(e, &x, sizeof(x));
551 5585f1c3 2021-12-16 op np_writebuf(struct evbuffer *e, size_t len, void *data)
553 5585f1c3 2021-12-16 op evbuffer_add(e, data, len);
557 2ef72ade 2021-07-28 op np_header(uint32_t len, uint8_t type, uint16_t tag)
559 9ebb95a7 2021-07-30 op len += HEADERSIZE;
561 63f681aa 2021-07-28 op len = htole32(len);
562 63f681aa 2021-07-28 op tag = htole16(tag);
564 2ef72ade 2021-07-28 op evbuffer_add(evb, &len, sizeof(len));
565 2ef72ade 2021-07-28 op evbuffer_add(evb, &type, sizeof(type));
566 2ef72ade 2021-07-28 op evbuffer_add(evb, &tag, sizeof(tag));
570 5585f1c3 2021-12-16 op np_string(struct evbuffer *e, uint16_t len, const char *str)
572 5c485996 2021-07-28 op uint16_t l = len;
574 5c485996 2021-07-28 op len = htole16(len);
575 5585f1c3 2021-12-16 op evbuffer_add(e, &len, sizeof(len));
576 5585f1c3 2021-12-16 op evbuffer_add(e, str, l);
580 5585f1c3 2021-12-16 op np_qid(struct evbuffer *e, struct qid *qid)
582 c5d518da 2021-07-29 op uint64_t path;
583 c5d518da 2021-07-29 op uint32_t vers;
585 c5d518da 2021-07-29 op path = htole64(qid->path);
586 c5d518da 2021-07-29 op vers = htole32(qid->vers);
588 5585f1c3 2021-12-16 op evbuffer_add(e, &qid->type, sizeof(qid->type));
589 5585f1c3 2021-12-16 op evbuffer_add(e, &vers, sizeof(vers));
590 5585f1c3 2021-12-16 op evbuffer_add(e, &path, sizeof(path));
594 2ef72ade 2021-07-28 op do_send(void)
599 2ef72ade 2021-07-28 op len = EVBUFFER_LENGTH(evb);
600 64c19d90 2021-07-30 op data = EVBUFFER_DATA(evb);
602 64c19d90 2021-07-30 op #if DEBUG_PACKETS
603 78b94752 2021-08-01 op hexdump("outgoing packet", data, len);
605 64c19d90 2021-07-30 op client_send_listener(IMSG_BUF, data, len);
606 2ef72ade 2021-07-28 op evbuffer_drain(evb, len);
610 5c485996 2021-07-28 op np_version(uint16_t tag, uint32_t msize, const char *version)
614 5c485996 2021-07-28 op l = strlen(version);
616 5c485996 2021-07-28 op msize = htole32(msize);
618 9ebb95a7 2021-07-30 op np_header(sizeof(msize) + sizeof(l) + l, Rversion, tag);
619 5c485996 2021-07-28 op evbuffer_add(evb, &msize, sizeof(msize));
620 5585f1c3 2021-12-16 op np_string(evb, l, version);
625 c5d518da 2021-07-29 op np_attach(uint16_t tag, struct qid *qid)
627 9ebb95a7 2021-07-30 op np_header(QIDSIZE, Rattach, tag);
628 5585f1c3 2021-12-16 op np_qid(evb, qid);
633 c377c1b9 2021-07-30 op np_clunk(uint16_t tag)
635 9ebb95a7 2021-07-30 op np_header(0, Rclunk, tag);
640 36b30273 2021-07-30 op np_flush(uint16_t tag)
642 9ebb95a7 2021-07-30 op np_header(0, Rflush, tag);
647 1c08fc54 2021-08-01 op np_walk(uint16_t tag, int nwqid, struct qid *wqid)
651 83f6b305 2021-08-01 op /* two bytes for the counter */
652 83f6b305 2021-08-01 op np_header(2 + QIDSIZE * nwqid, Rwalk, tag);
653 5585f1c3 2021-12-16 op np_write16(evb, nwqid);
654 1c08fc54 2021-08-01 op for (i = 0; i < nwqid; ++i)
655 5585f1c3 2021-12-16 op np_qid(evb, wqid + i);
661 021481ca 2021-12-13 op np_open(uint16_t tag, struct qid *qid, uint32_t iounit)
663 021481ca 2021-12-13 op np_header(QIDSIZE + sizeof(iounit), Ropen, tag);
664 5585f1c3 2021-12-16 op np_qid(evb, qid);
665 5585f1c3 2021-12-16 op np_write32(evb, iounit);
670 baa40504 2021-12-15 op np_read(uint16_t tag, uint32_t count, void *data)
672 baa40504 2021-12-15 op np_header(sizeof(count) + count, Rread, tag);
673 5585f1c3 2021-12-16 op np_write32(evb, count);
674 5585f1c3 2021-12-16 op np_writebuf(evb, count, data);
679 2483be55 2021-12-16 op np_write(uint16_t tag, uint32_t count)
681 2483be55 2021-12-16 op np_header(sizeof(count), Rwrite, tag);
682 2483be55 2021-12-16 op np_write32(evb, count);
687 3a2c53f5 2021-12-18 op np_stat(uint16_t tag, uint32_t count, void *data)
689 3a2c53f5 2021-12-18 op np_header(count, Rstat, tag);
690 3a2c53f5 2021-12-18 op np_writebuf(evb, count, data);
695 2ef72ade 2021-07-28 op np_error(uint16_t tag, const char *errstr)
699 2ef72ade 2021-07-28 op l = strlen(errstr);
701 9ebb95a7 2021-07-30 op np_header(sizeof(l) + l, Rerror, tag);
702 5585f1c3 2021-12-16 op np_string(evb, l, errstr);
707 c5d518da 2021-07-29 op np_errno(uint16_t tag)
709 9b088310 2021-07-30 op int saved_errno;
710 c5d518da 2021-07-29 op char buf[64];
712 9b088310 2021-07-30 op saved_errno = errno;
714 c5d518da 2021-07-29 op strerror_r(errno, buf, sizeof(buf));
715 c5d518da 2021-07-29 op np_error(tag, buf);
717 9b088310 2021-07-30 op errno = saved_errno;
721 48192874 2021-08-01 op np_read8(const char *t, const char *f, uint8_t *dst, const uint8_t **src,
724 48192874 2021-08-01 op if (*len < sizeof(*dst)) {
725 48192874 2021-08-01 op log_warnx("%s: wanted %zu bytes for the %s field but only "
726 48192874 2021-08-01 op "%zu are available.", t, sizeof(*dst), f, *len);
730 48192874 2021-08-01 op memcpy(dst, *src, sizeof(*dst));
731 48192874 2021-08-01 op *src += sizeof(*dst);
732 48192874 2021-08-01 op *len -= sizeof(*dst);
738 48192874 2021-08-01 op np_read16(const char *t, const char *f, uint16_t *dst, const uint8_t **src,
741 48192874 2021-08-01 op if (*len < sizeof(*dst)) {
742 48192874 2021-08-01 op log_warnx("%s: wanted %zu bytes for the %s field but only "
743 48192874 2021-08-01 op "%zu are available.", t, sizeof(*dst), f, *len);
747 48192874 2021-08-01 op memcpy(dst, *src, sizeof(*dst));
748 48192874 2021-08-01 op *src += sizeof(*dst);
749 48192874 2021-08-01 op *len -= sizeof(*dst);
750 48192874 2021-08-01 op *dst = le16toh(*dst);
756 48192874 2021-08-01 op np_read32(const char *t, const char *f, uint32_t *dst, const uint8_t **src,
759 48192874 2021-08-01 op if (*len < sizeof(*dst)) {
760 48192874 2021-08-01 op log_warnx("%s: wanted %zu bytes for the %s field but only "
761 48192874 2021-08-01 op "%zu are available.", t, sizeof(*dst), f, *len);
765 48192874 2021-08-01 op memcpy(dst, *src, sizeof(*dst));
766 48192874 2021-08-01 op *src += sizeof(*dst);
767 48192874 2021-08-01 op *len -= sizeof(*dst);
768 48192874 2021-08-01 op *dst = le32toh(*dst);
774 baa40504 2021-12-15 op np_read64(const char *t, const char *f, uint64_t *dst, const uint8_t **src,
777 baa40504 2021-12-15 op if (*len < sizeof(*dst)) {
778 baa40504 2021-12-15 op log_warnx("%s: wanted %zu bytes for the %s field but only "
779 baa40504 2021-12-15 op "%zu are available.", t, sizeof(*dst), f, *len);
783 baa40504 2021-12-15 op memcpy(dst, *src, sizeof(*dst));
784 baa40504 2021-12-15 op *src += sizeof(*dst);
785 baa40504 2021-12-15 op *len -= sizeof(*dst);
786 baa40504 2021-12-15 op *dst = le64toh(*dst);
792 48192874 2021-08-01 op np_readstr(const char *t, const char *f, char *res, size_t reslen,
793 48192874 2021-08-01 op const uint8_t **src, size_t *len)
796 48192874 2021-08-01 op char buf[32];
798 48192874 2021-08-01 op strlcpy(buf, f, sizeof(buf));
799 48192874 2021-08-01 op strlcat(buf, "-len", sizeof(buf));
801 48192874 2021-08-01 op if (!np_read16(t, buf, &sl, src, len))
802 48192874 2021-08-01 op return READSTRERR;
804 48192874 2021-08-01 op if (*len < sl) {
805 48192874 2021-08-01 op log_warnx("%s: wanted %d bytes for the %s field but only "
806 48192874 2021-08-01 op "%zu are available.", t, sl, f, *len);
807 48192874 2021-08-01 op return READSTRERR;
810 48192874 2021-08-01 op if (*len > reslen-1)
811 48192874 2021-08-01 op return READSTRTRUNC;
813 48192874 2021-08-01 op memcpy(res, *src, sl);
814 48192874 2021-08-01 op res[sl] = '\0';
822 e60f4e08 2021-07-30 op tversion(struct np_msg_header *hdr, const uint8_t *data, size_t len)
824 48192874 2021-08-01 op char *dot, version[32];
826 e60f4e08 2021-07-30 op if (handshaked)
829 3d20424d 2021-08-01 op /* msize[4] version[s] */
830 48192874 2021-08-01 op if (!NPREAD32("msize", &msize, &data, &len))
833 48192874 2021-08-01 op switch (NPREADSTR("version", version, sizeof(version), &data, &len)) {
834 48192874 2021-08-01 op case READSTRERR:
836 48192874 2021-08-01 op case READSTRTRUNC:
837 48192874 2021-08-01 op log_warnx("9P version string too long, truncated");
838 48192874 2021-08-01 op goto mismatch;
841 48192874 2021-08-01 op if ((dot = strchr(version, '.')) != NULL)
844 48192874 2021-08-01 op if (strcmp(version, VERSION9P) != 0 ||
846 48192874 2021-08-01 op goto mismatch;
848 48192874 2021-08-01 op /* version matched */
849 48192874 2021-08-01 op handshaked = 1;
850 e60f4e08 2021-07-30 op msize = MIN(msize, MSIZE9P);
851 e60f4e08 2021-07-30 op client_send_listener(IMSG_MSIZE, &msize, sizeof(msize));
852 e60f4e08 2021-07-30 op np_version(hdr->tag, msize, VERSION9P);
856 48192874 2021-08-01 op log_warnx("unknown 9P version string: \"%s\", want "VERSION9P,
858 48192874 2021-08-01 op np_version(hdr->tag, MSIZE9P, "unknown");
862 e60f4e08 2021-07-30 op client_send_listener(IMSG_CLOSE, NULL, 0);
863 e60f4e08 2021-07-30 op client_shutdown();
867 e60f4e08 2021-07-30 op tattach(struct np_msg_header *hdr, const uint8_t *data, size_t len)
869 e60f4e08 2021-07-30 op struct qid *qid;
870 e60f4e08 2021-07-30 op struct fid *f;
871 48192874 2021-08-01 op uint32_t fid, afid;
873 014adfc2 2021-07-30 op char aname[PATH_MAX];
875 e60f4e08 2021-07-30 op /* fid[4] afid[4] uname[s] aname[s] */
877 48192874 2021-08-01 op if (!NPREAD32("fid", &fid, &data, &len) ||
878 48192874 2021-08-01 op !NPREAD32("afid", &afid, &data, &len))
881 48192874 2021-08-01 op /* read the uname but don't actually use it */
882 48192874 2021-08-01 op switch (NPREADSTR("uname", aname, sizeof(aname), &data, &len)) {
883 48192874 2021-08-01 op case READSTRERR:
885 48192874 2021-08-01 op case READSTRTRUNC:
886 48192874 2021-08-01 op np_error(hdr->tag, "name too long");
890 48192874 2021-08-01 op switch (NPREADSTR("aname", aname, sizeof(aname), &data, &len)) {
891 48192874 2021-08-01 op case READSTRERR:
893 48192874 2021-08-01 op case READSTRTRUNC:
894 014adfc2 2021-07-30 op np_error(hdr->tag, "name too long");
898 cf758c33 2021-12-14 op if (fid_by_id(fid) != NULL || afid != NOFID) {
899 cf758c33 2021-12-14 op np_error(hdr->tag, "invalid fid or afid");
903 deb971f5 2021-08-12 op if ((fd = open(aname, O_RDONLY|O_DIRECTORY)) == -1)
906 deb971f5 2021-08-12 op if ((qid = qid_from_fd(fd, NULL, NULL)) == NULL)
909 cf758c33 2021-12-14 op log_debug("attached %s to %d", aname, fid);
911 e60f4e08 2021-07-30 op if ((f = new_fid(qid, fid)) == NULL) {
912 053e2652 2021-08-01 op qid_decref(qid);
916 e60f4e08 2021-07-30 op np_attach(hdr->tag, qid);
920 deb971f5 2021-08-12 op np_errno(hdr->tag);
921 deb971f5 2021-08-12 op log_warn("failed to attach %s", aname);
926 014adfc2 2021-07-30 op client_send_listener(IMSG_CLOSE, NULL, 0);
927 014adfc2 2021-07-30 op client_shutdown();
931 c377c1b9 2021-07-30 op tclunk(struct np_msg_header *hdr, const uint8_t *data, size_t len)
933 c377c1b9 2021-07-30 op struct fid *f;
934 c377c1b9 2021-07-30 op uint32_t fid;
937 48192874 2021-08-01 op if (!NPREAD32("fid", &fid, &data, &len)) {
938 c377c1b9 2021-07-30 op client_send_listener(IMSG_CLOSE, NULL, 0);
939 c377c1b9 2021-07-30 op client_shutdown();
943 c377c1b9 2021-07-30 op if ((f = fid_by_id(fid)) == NULL) {
944 c377c1b9 2021-07-30 op np_error(hdr->tag, "invalid fid");
949 c377c1b9 2021-07-30 op np_clunk(hdr->tag);
953 36b30273 2021-07-30 op tflush(struct np_msg_header *hdr, const uint8_t *data, size_t len)
955 36b30273 2021-07-30 op uint16_t oldtag;
958 36b30273 2021-07-30 op * We're doing only synchronous I/O. Tflush is implemented
959 36b30273 2021-07-30 op * only because it's illegal to reply with a Rerror.
962 36b30273 2021-07-30 op /* oldtag[2] */
963 36b30273 2021-07-30 op if (len != sizeof(oldtag)) {
964 36b30273 2021-07-30 op log_warnx("Tclunk with the wrong size: got %zu want %zu",
965 36b30273 2021-07-30 op len, sizeof(oldtag));
966 36b30273 2021-07-30 op client_send_listener(IMSG_CLOSE, NULL, 0);
967 36b30273 2021-07-30 op client_shutdown();
971 36b30273 2021-07-30 op np_flush(hdr->tag);
975 1c08fc54 2021-08-01 op twalk(struct np_msg_header *hdr, const uint8_t *data, size_t len)
977 deb971f5 2021-08-12 op struct stat sb;
978 053e2652 2021-08-01 op struct qid *qid, wqid[MAXWELEM] = {0};
979 1c08fc54 2021-08-01 op struct fid *f, *nf;
980 1c08fc54 2021-08-01 op uint32_t fid, newfid;
981 7ddddcfe 2021-08-01 op uint16_t nwname;
982 deb971f5 2021-08-12 op int fd, oldfd, no, nwqid = 0;
983 20c188ce 2021-12-16 op char wnam[PATH_MAX];
985 48192874 2021-08-01 op if (!NPREAD32("fid", &fid, &data, &len) ||
986 48192874 2021-08-01 op !NPREAD32("newfid", &newfid, &data, &len) ||
987 48192874 2021-08-01 op !NPREAD16("nwname", &nwname, &data, &len))
990 1c08fc54 2021-08-01 op if (nwname > MAXWELEM) {
991 1c08fc54 2021-08-01 op log_warnx("Twalk: more than %d path elements: %d",
992 1c08fc54 2021-08-01 op MAXWELEM, nwname);
996 1c08fc54 2021-08-01 op if ((f = fid_by_id(fid)) == NULL) {
997 1c08fc54 2021-08-01 op np_error(hdr->tag, "invalid fid");
1001 99a43590 2021-12-14 op if (f->fd != -1) {
1002 47c9b8b8 2021-12-02 op np_error(hdr->tag, "fid already opened for I/O");
1006 1c08fc54 2021-08-01 op if (fid == newfid)
1008 1c08fc54 2021-08-01 op else if ((nf = fid_by_id(newfid)) != NULL) {
1009 1c08fc54 2021-08-01 op np_error(hdr->tag, "newfid already in use");
1014 913eba5c 2021-08-01 op /* special case: fid duplication */
1015 22dfb5a0 2021-08-01 op if (nwname == 0) {
1017 1c08fc54 2021-08-01 op * TODO: should we forbid fids duplication when fid ==
1020 f987557c 2021-12-02 op if (nf == NULL && (nf = new_fid(f->qid, newfid)) == NULL)
1021 f987557c 2021-12-02 op fatal("new_fid duplication");
1023 1c08fc54 2021-08-01 op np_walk(hdr->tag, 1, f->qid);
1027 053e2652 2021-08-01 op if (f->qid->type != QTDIR) {
1028 053e2652 2021-08-01 op np_error(hdr->tag, "fid doesn't represent a directory");
1032 deb971f5 2021-08-12 op oldfd = f->qid->fd;
1034 1c08fc54 2021-08-01 op for (nwqid = 0; nwqid < nwname; nwqid++) {
1035 053e2652 2021-08-01 op switch (NPREADSTR("wname", wnam, sizeof(wnam), &data, &len)) {
1036 48192874 2021-08-01 op case READSTRERR:
1038 48192874 2021-08-01 op case READSTRTRUNC:
1039 1c08fc54 2021-08-01 op np_error(hdr->tag, "wname too long");
1043 054cd6b9 2021-12-14 op if (*wnam == '\0' ||
1044 054cd6b9 2021-12-14 op strchr(wnam, '/') != NULL ||
1045 054cd6b9 2021-12-14 op !strcmp(wnam, ".")) {
1046 054cd6b9 2021-12-14 op errno = EINVAL;
1047 054cd6b9 2021-12-14 op goto cantopen;
1050 deb971f5 2021-08-12 op if ((fd = openat(oldfd, wnam, O_RDONLY|O_DIRECTORY)) == -1 &&
1051 3242c0bc 2021-12-14 op errno != ENOTDIR)
1052 deb971f5 2021-08-12 op goto cantopen;
1054 deb971f5 2021-08-12 op if ((fd == -1 && fstatat(oldfd, wnam, &sb, 0) == -1) ||
1055 3242c0bc 2021-12-14 op (fd != -1 && fstat(fd, &sb) == -1))
1056 deb971f5 2021-08-12 op goto cantopen;
1058 deb971f5 2021-08-12 op qid_update_from_sb(&wqid[nwqid], &sb);
1060 deb971f5 2021-08-12 op /* reached a file but we still have other components */
1061 deb971f5 2021-08-12 op if (fd == -1 && nwqid+1 < nwname)
1062 deb971f5 2021-08-12 op goto cantopen;
1064 deb971f5 2021-08-12 op /* reached the end and found a file */
1065 deb971f5 2021-08-12 op if (fd == -1 && nwqid+1 == nwname)
1068 deb971f5 2021-08-12 op if (oldfd != f->qid->fd)
1069 deb971f5 2021-08-12 op close(oldfd);
1074 deb971f5 2021-08-12 op * There can be two possibilities: fd == -1 means that we've
1075 deb971f5 2021-08-12 op * reached a file and we should save BOTH the dirfd (oldfd)
1076 deb971f5 2021-08-12 op * and the path (wnam); or we just reached another directory,
1077 deb971f5 2021-08-12 op * in which case we can just create a new qid using fd.
1079 deb971f5 2021-08-12 op if (fd == -1)
1080 deb971f5 2021-08-12 op qid = qid_from_fd(oldfd, wnam, &sb);
1082 deb971f5 2021-08-12 op qid = qid_from_fd(oldfd, NULL, &sb);
1083 deb971f5 2021-08-12 op if (qid == NULL)
1084 eafb23b1 2021-08-01 op fatal("qid_from_fd");
1086 1c08fc54 2021-08-01 op if (nf == NULL) {
1087 1c08fc54 2021-08-01 op if ((nf = new_fid(qid, newfid)) == NULL)
1088 1c08fc54 2021-08-01 op fatal("new_fid");
1090 1c08fc54 2021-08-01 op /* swap qid */
1091 1c08fc54 2021-08-01 op qid_decref(nf->qid);
1092 1c08fc54 2021-08-01 op nf->qid = qid_incref(qid);
1095 1c08fc54 2021-08-01 op np_walk(hdr->tag, nwqid, wqid);
1099 deb971f5 2021-08-12 op if (oldfd != f->qid->fd)
1100 deb971f5 2021-08-12 op close(oldfd);
1102 deb971f5 2021-08-12 op if (nwqid == 0)
1103 deb971f5 2021-08-12 op np_error(hdr->tag, strerror(no));
1105 deb971f5 2021-08-12 op np_walk(hdr->tag, nwqid, wqid);
1109 1c08fc54 2021-08-01 op client_send_listener(IMSG_CLOSE, NULL, 0);
1110 1c08fc54 2021-08-01 op client_shutdown();
1113 021481ca 2021-12-13 op static inline int
1114 021481ca 2021-12-13 op npmode_to_unix(uint8_t mode, int *flags)
1116 021481ca 2021-12-13 op switch (mode & 0x0F) {
1117 021481ca 2021-12-13 op case KOREAD:
1118 021481ca 2021-12-13 op *flags = O_RDONLY;
1120 021481ca 2021-12-13 op case KOWRITE:
1121 021481ca 2021-12-13 op *flags = O_WRONLY;
1123 021481ca 2021-12-13 op case KORDWR:
1124 021481ca 2021-12-13 op *flags = O_RDWR;
1126 021481ca 2021-12-13 op case KOEXEC:
1127 021481ca 2021-12-13 op log_warnx("tried to open something with KOEXEC");
1128 021481ca 2021-12-13 op /* fallthrough */
1133 021481ca 2021-12-13 op if (mode & KOTRUNC)
1134 021481ca 2021-12-13 op *flags |= O_TRUNC;
1135 021481ca 2021-12-13 op if (mode & KORCLOSE)
1136 021481ca 2021-12-13 op *flags |= O_CLOEXEC;
1142 021481ca 2021-12-13 op topen(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1144 021481ca 2021-12-13 op struct stat sb;
1145 021481ca 2021-12-13 op struct qid qid;
1146 021481ca 2021-12-13 op struct fid *f;
1147 021481ca 2021-12-13 op uint32_t fid;
1148 021481ca 2021-12-13 op uint8_t mode;
1149 87a818e8 2021-12-14 op const char *path;
1151 021481ca 2021-12-13 op /* fid[4] mode[1] */
1152 021481ca 2021-12-13 op if (!NPREAD32("fid", &fid, &data, &len) ||
1153 021481ca 2021-12-13 op !NPREAD8("mode", &mode, &data, &len)) {
1154 021481ca 2021-12-13 op client_send_listener(IMSG_CLOSE, NULL, 0);
1155 021481ca 2021-12-13 op client_shutdown();
1159 021481ca 2021-12-13 op if ((f = fid_by_id(fid)) == NULL || f->fd != -1) {
1160 021481ca 2021-12-13 op np_error(hdr->tag, "invalid fid");
1164 d4e43815 2021-12-15 op if (npmode_to_unix(mode, &f->iomode) == -1) {
1165 d4e43815 2021-12-15 op np_error(hdr->tag, "invalid mode");
1169 d4e43815 2021-12-15 op if (f->qid->type & QTDIR &&
1170 d4e43815 2021-12-15 op (f->iomode & O_WRONLY || f->iomode & O_RDWR)) {
1171 d4e43815 2021-12-15 op np_error(hdr->tag, "can't open directory for writing");
1175 87a818e8 2021-12-14 op path = f->qid->fpath;
1176 87a818e8 2021-12-14 op if (*path == '\0')
1179 87a818e8 2021-12-14 op if ((f->fd = openat(f->qid->fd, path, f->iomode)) == -1) {
1180 021481ca 2021-12-13 op np_error(hdr->tag, strerror(errno));
1184 021481ca 2021-12-13 op if (fstat(f->fd, &sb) == -1)
1185 021481ca 2021-12-13 op fatal("fstat");
1187 baa40504 2021-12-15 op if (S_ISDIR(sb.st_mode)) {
1188 baa40504 2021-12-15 op assert(f->qid->type & QTDIR);
1189 baa40504 2021-12-15 op if ((f->dir = fdopendir(f->fd)) == NULL) {
1190 baa40504 2021-12-15 op np_errno(hdr->tag);
1191 baa40504 2021-12-15 op close(f->fd);
1196 c0ae57b9 2021-12-15 op if ((f->evb = evbuffer_new()) == NULL) {
1197 c0ae57b9 2021-12-15 op np_errno(hdr->tag);
1198 c0ae57b9 2021-12-15 op closedir(f->dir);
1199 c0ae57b9 2021-12-15 op f->dir = NULL;
1204 baa40504 2021-12-15 op f->offset = 0;
1206 021481ca 2021-12-13 op qid_update_from_sb(&qid, &sb);
1207 021481ca 2021-12-13 op np_open(hdr->tag, &qid, sb.st_blksize);
1210 5585f1c3 2021-12-16 op static inline void
1211 3a2c53f5 2021-12-18 op serialize_stat(const char *fname, struct stat *sb, struct evbuffer *evb)
1213 5585f1c3 2021-12-16 op struct qid qid;
1214 5585f1c3 2021-12-16 op const char *uid, *gid, *muid;
1216 5585f1c3 2021-12-16 op uint16_t namlen, uidlen, gidlen, ulen;
1218 3a2c53f5 2021-12-18 op qid_update_from_sb(&qid, sb);
1220 5585f1c3 2021-12-16 op /* TODO: fill these fields */
1225 3a2c53f5 2021-12-18 op namlen = strlen(fname);
1226 5585f1c3 2021-12-16 op uidlen = strlen(uid);
1227 5585f1c3 2021-12-16 op gidlen = strlen(gid);
1228 5585f1c3 2021-12-16 op ulen = strlen(muid);
1230 5585f1c3 2021-12-16 op tot = NPSTATSIZ(namlen, uidlen, gidlen, ulen);
1231 5585f1c3 2021-12-16 op if (tot > UINT32_MAX) {
1232 5585f1c3 2021-12-16 op log_warnx("stat info for dir entry %s would overflow",
1237 5585f1c3 2021-12-16 op np_write16(evb, tot); /* size[2] */
1238 3a2c53f5 2021-12-18 op np_write16(evb, sb->st_rdev); /* type[2] */
1239 3a2c53f5 2021-12-18 op np_write32(evb, sb->st_dev); /* dev[4] */
1240 5585f1c3 2021-12-16 op np_qid(evb, &qid); /* qid[13] */
1242 5585f1c3 2021-12-16 op /* XXX: translate? */
1243 3a2c53f5 2021-12-18 op np_write32(evb, sb->st_mode); /* mode[4] */
1245 3a2c53f5 2021-12-18 op np_write32(evb, sb->st_atim.tv_sec); /* atime[4] */
1246 3a2c53f5 2021-12-18 op np_write32(evb, sb->st_mtim.tv_sec); /* mtime[4] */
1247 3a2c53f5 2021-12-18 op np_write64(evb, sb->st_size); /* length[8] */
1248 3a2c53f5 2021-12-18 op np_string(evb, namlen, fname); /* name[s] */
1249 5585f1c3 2021-12-16 op np_string(evb, uidlen, uid); /* uid[s] */
1250 5585f1c3 2021-12-16 op np_string(evb, gidlen, gid); /* gid[s] */
1251 5585f1c3 2021-12-16 op np_string(evb, ulen, muid); /* muid[s] */
1255 baa40504 2021-12-15 op tread(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1257 baa40504 2021-12-15 op struct fid *f;
1259 baa40504 2021-12-15 op uint64_t off;
1260 baa40504 2021-12-15 op uint32_t fid, count;
1261 baa40504 2021-12-15 op char buf[2048];
1263 baa40504 2021-12-15 op /* fid[4] offset[8] count[4] */
1264 baa40504 2021-12-15 op if (!NPREAD32("fid", &fid, &data, &len) ||
1265 baa40504 2021-12-15 op !NPREAD64("offset", &off, &data, &len) ||
1266 baa40504 2021-12-15 op !NPREAD32("count", &count, &data, &len)) {
1267 baa40504 2021-12-15 op client_send_listener(IMSG_CLOSE, NULL, 0);
1268 baa40504 2021-12-15 op client_shutdown();
1272 5f362b7f 2021-12-15 op if ((f = fid_by_id(fid)) == NULL || f->fd == -1) {
1273 baa40504 2021-12-15 op np_error(hdr->tag, "invalid fid");
1277 baa40504 2021-12-15 op if (TYPE_OVERFLOW(off_t, off)) {
1278 baa40504 2021-12-15 op log_warnx("unexpected size_t size");
1279 baa40504 2021-12-15 op np_error(hdr->tag, "invalid offset");
1283 baa40504 2021-12-15 op if (f->dir == NULL) {
1284 baa40504 2021-12-15 op /* read a file */
1285 baa40504 2021-12-15 op r = pread(f->fd, buf, sizeof(buf), (off_t)off);
1286 baa40504 2021-12-15 op if (r == -1)
1287 baa40504 2021-12-15 op np_errno(hdr->tag);
1289 baa40504 2021-12-15 op np_read(hdr->tag, r, buf);
1291 fcf8ca15 2021-12-16 op if (off == 0 && f->offset != 0) {
1292 fcf8ca15 2021-12-16 op rewinddir(f->dir);
1293 fcf8ca15 2021-12-16 op f->offset = 0;
1296 5585f1c3 2021-12-16 op if (off != f->offset) {
1297 5585f1c3 2021-12-16 op np_error(hdr->tag, "can't seek in directories");
1301 5585f1c3 2021-12-16 op while (EVBUFFER_LENGTH(f->evb) < count) {
1302 5585f1c3 2021-12-16 op struct dirent *d;
1303 3a2c53f5 2021-12-18 op struct stat sb;
1305 5585f1c3 2021-12-16 op if ((d = readdir(f->dir)) == NULL)
1307 3a2c53f5 2021-12-18 op if (fstatat(f->fd, d->d_name, &sb, 0) == -1) {
1308 3a2c53f5 2021-12-18 op warn("fstatat");
1311 3a2c53f5 2021-12-18 op serialize_stat(d->d_name, &sb, f->evb);
1314 5585f1c3 2021-12-16 op count = MIN(count, EVBUFFER_LENGTH(f->evb));
1315 5585f1c3 2021-12-16 op np_read(hdr->tag, count, EVBUFFER_DATA(f->evb));
1316 5585f1c3 2021-12-16 op evbuffer_drain(f->evb, count);
1318 5585f1c3 2021-12-16 op f->offset += count;
1323 2483be55 2021-12-16 op twrite(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1325 2483be55 2021-12-16 op struct fid *f;
1327 2483be55 2021-12-16 op uint64_t off;
1328 2483be55 2021-12-16 op uint32_t fid, count;
1330 2483be55 2021-12-16 op /* fid[4] offset[8] count[4] data[count] */
1331 2483be55 2021-12-16 op if (!NPREAD32("fid", &fid, &data, &len) ||
1332 2483be55 2021-12-16 op !NPREAD64("off", &off, &data, &len) ||
1333 2483be55 2021-12-16 op !NPREAD32("count", &count, &data, &len) ||
1334 2483be55 2021-12-16 op len != count) {
1335 2483be55 2021-12-16 op client_send_listener(IMSG_CLOSE, NULL, 0);
1336 2483be55 2021-12-16 op client_shutdown();
1340 2483be55 2021-12-16 op if ((f = fid_by_id(fid)) == NULL || f->fd == -1) {
1341 2483be55 2021-12-16 op np_error(hdr->tag, "invalid fid");
1345 2483be55 2021-12-16 op if (!(f->iomode & O_WRONLY) &&
1346 2483be55 2021-12-16 op !(f->iomode & O_RDWR)) {
1347 2483be55 2021-12-16 op np_error(hdr->tag, "fid not opened for writing");
1351 2483be55 2021-12-16 op if (TYPE_OVERFLOW(off_t, off)) {
1352 2483be55 2021-12-16 op log_warnx("unexpected off_t size");
1353 2483be55 2021-12-16 op np_error(hdr->tag, "invalid offset");
1357 6ace3b52 2021-12-17 op if ((r = pwrite(f->fd, data, len, off)) == -1)
1358 2483be55 2021-12-16 op np_errno(hdr->tag);
1360 2483be55 2021-12-16 op np_write(hdr->tag, r);
1364 3a2c53f5 2021-12-18 op tstat(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1366 3a2c53f5 2021-12-18 op struct evbuffer *evb;
1367 3a2c53f5 2021-12-18 op struct stat sb;
1368 3a2c53f5 2021-12-18 op struct fid *f;
1369 3a2c53f5 2021-12-18 op const char *fname;
1371 3a2c53f5 2021-12-18 op uint32_t fid;
1373 3a2c53f5 2021-12-18 op /* fid[4] */
1374 3a2c53f5 2021-12-18 op if (!NPREAD32("fid", &fid, &data, &len)) {
1375 3a2c53f5 2021-12-18 op client_send_listener(IMSG_CLOSE, NULL, 0);
1376 3a2c53f5 2021-12-18 op client_shutdown();
1381 3a2c53f5 2021-12-18 op * plan9' stat(9P) is not clear on whether the stat is allowed
1382 3a2c53f5 2021-12-18 op * on opened fids or not.
1384 3a2c53f5 2021-12-18 op if ((f = fid_by_id(fid)) == NULL) {
1385 3a2c53f5 2021-12-18 op np_error(hdr->tag, "invalid fid");
1389 3a2c53f5 2021-12-18 op if ((evb = evbuffer_new()) == NULL)
1390 3a2c53f5 2021-12-18 op fatal("evbuffer_new");
1392 3a2c53f5 2021-12-18 op if (f->qid->type & QTDIR) {
1393 3a2c53f5 2021-12-18 op fname = ".";
1394 3a2c53f5 2021-12-18 op r = fstat(f->qid->fd, &sb);
1396 3a2c53f5 2021-12-18 op fname = f->qid->fpath;
1397 3a2c53f5 2021-12-18 op r = fstatat(f->qid->fd, f->qid->fpath, &sb, 0);
1400 3a2c53f5 2021-12-18 op if (r == -1) {
1401 3a2c53f5 2021-12-18 op np_errno(hdr->tag);
1402 3a2c53f5 2021-12-18 op evbuffer_free(evb);
1406 3a2c53f5 2021-12-18 op serialize_stat(fname, &sb, evb);
1407 3a2c53f5 2021-12-18 op np_stat(hdr->tag, EVBUFFER_LENGTH(evb), EVBUFFER_DATA(evb));
1408 3a2c53f5 2021-12-18 op evbuffer_free(evb);
1412 e60f4e08 2021-07-30 op handle_message(struct imsg *imsg, size_t len)
1414 e60f4e08 2021-07-30 op struct msg {
1415 e60f4e08 2021-07-30 op uint8_t type;
1416 e60f4e08 2021-07-30 op void (*fn)(struct np_msg_header *, const uint8_t *, size_t);
1417 e60f4e08 2021-07-30 op } msgs[] = {
1418 e60f4e08 2021-07-30 op {Tversion, tversion},
1419 e60f4e08 2021-07-30 op {Tattach, tattach},
1420 c377c1b9 2021-07-30 op {Tclunk, tclunk},
1421 36b30273 2021-07-30 op {Tflush, tflush},
1422 1c08fc54 2021-08-01 op {Twalk, twalk},
1423 021481ca 2021-12-13 op {Topen, topen},
1424 baa40504 2021-12-15 op {Tread, tread},
1425 2483be55 2021-12-16 op {Twrite, twrite},
1426 3a2c53f5 2021-12-18 op {Tstat, tstat},
1428 e60f4e08 2021-07-30 op struct np_msg_header hdr;
1430 e60f4e08 2021-07-30 op uint8_t *data;
1432 64c19d90 2021-07-30 op #if DEBUG_PACKETS
1433 78b94752 2021-08-01 op hexdump("incoming packet", imsg->data, len);
1436 e60f4e08 2021-07-30 op parse_message(imsg->data, len, &hdr, &data);
1437 e60f4e08 2021-07-30 op len -= HEADERSIZE;
1439 e60f4e08 2021-07-30 op log_debug("got request: len=%d type=%d[%s] tag=%d",
1440 e60f4e08 2021-07-30 op hdr.len, hdr.type, pp_msg_type(hdr.type), hdr.tag);
1442 e60f4e08 2021-07-30 op if (!handshaked && hdr.type != Tversion) {
1443 e60f4e08 2021-07-30 op client_send_listener(IMSG_CLOSE, NULL, 0);
1444 e60f4e08 2021-07-30 op client_shutdown();
1448 e60f4e08 2021-07-30 op for (i = 0; i < sizeof(msgs)/sizeof(msgs[0]); ++i) {
1449 e60f4e08 2021-07-30 op if (msgs[i].type != hdr.type)
1452 e60f4e08 2021-07-30 op msgs[i].fn(&hdr, data, len);
1456 e60f4e08 2021-07-30 op np_error(hdr.tag, "Not supported.");