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 <dirent.h>
22 23e03c88 2021-07-26 op #include <endian.h>
23 8d1b399b 2021-07-22 op #include <errno.h>
24 c5d518da 2021-07-29 op #include <fcntl.h>
25 8d1b399b 2021-07-22 op #include <pwd.h>
26 8d1b399b 2021-07-22 op #include <signal.h>
27 8d1b399b 2021-07-22 op #include <stdlib.h>
28 8d1b399b 2021-07-22 op #include <string.h>
29 8d1b399b 2021-07-22 op #include <syslog.h>
30 8d1b399b 2021-07-22 op #include <unistd.h>
32 8d1b399b 2021-07-22 op #include "client.h"
33 8d1b399b 2021-07-22 op #include "kamid.h"
34 8d1b399b 2021-07-22 op #include "log.h"
35 8d1b399b 2021-07-22 op #include "sandbox.h"
36 23e03c88 2021-07-26 op #include "utils.h"
39 423f02f5 2021-12-28 cage * XXX: atm is difficult to accept messages bigger than MAX_IMSGSIZE
40 423f02f5 2021-12-28 cage * minus IMSG_HEADER_SIZE, we need something to split messages into
41 423f02f5 2021-12-28 cage * chunks and receive them one by the other.
43 423f02f5 2021-12-28 cage * CLIENT_MSIZE is thus the maximum message size we can handle now.
45 423f02f5 2021-12-28 cage #define CLIENT_MSIZE (MAX_IMSGSIZE - IMSG_HEADER_SIZE)
47 2adb951f 2021-08-01 op #define DEBUG_PACKETS 0
49 baa40504 2021-12-15 op /* straight outta /src/usr.bin/ssh/scp.c */
50 baa40504 2021-12-15 op #define TYPE_OVERFLOW(type, val) \
51 baa40504 2021-12-15 op ((sizeof(type) == 4 && (val) > INT32_MAX) || \
52 baa40504 2021-12-15 op (sizeof(type) == 8 && (val) > INT64_MAX) || \
53 baa40504 2021-12-15 op (sizeof(type) != 4 && sizeof(type) != 8))
55 c7f4e1bd 2021-12-24 op STAILQ_HEAD(dirhead, dir) dirs;
59 c7f4e1bd 2021-12-24 op STAILQ_ENTRY(dir) entries;
62 c5d518da 2021-07-29 op STAILQ_HEAD(fidhead, fid) fids;
66 1bf47a19 2021-12-23 op char fpath[PATH_MAX];
69 021481ca 2021-12-13 op * the flags passed to open(2). O_CLOEXEC means ORCLOSE, that
70 021481ca 2021-12-13 op * is to unlink the file upon Tclunk.
75 6842faaf 2021-12-24 op * if fd is not -1 this fid was opened, fd represents its
76 6842faaf 2021-12-24 op * file descriptor and iomode the flags passed to open(2).
80 baa40504 2021-12-15 op struct evbuffer *evb;
83 5585f1c3 2021-12-16 op * expected offset for Tread against a directory.
85 baa40504 2021-12-15 op uint64_t offset;
87 c7f4e1bd 2021-12-24 op struct qid qid;
88 c7f4e1bd 2021-12-24 op struct dir *dir;
89 c5d518da 2021-07-29 op STAILQ_ENTRY(fid) entries;
92 8d1b399b 2021-07-22 op static struct imsgev *iev_listener;
93 2ef72ade 2021-07-28 op static struct evbuffer *evb;
94 2ef72ade 2021-07-28 op static uint32_t peerid;
96 cf758c33 2021-12-14 op static int handshaked;
97 5c485996 2021-07-28 op uint32_t msize;
99 a97ec9eb 2021-12-23 op static __dead void client_shutdown(void);
100 8d1b399b 2021-07-22 op static void client_sig_handler(int, short, void *);
101 8d1b399b 2021-07-22 op static void client_dispatch_listener(int, short, void *);
102 8d1b399b 2021-07-22 op static void client_privdrop(const char *, const char *);
104 2ef72ade 2021-07-28 op static int client_send_listener(int, const void *, uint16_t);
106 deb971f5 2021-08-12 op static void qid_update_from_sb(struct qid *, struct stat *);
108 c7f4e1bd 2021-12-24 op static struct dir *new_dir(int);
109 c7f4e1bd 2021-12-24 op static struct dir *dir_incref(struct dir *);
110 c7f4e1bd 2021-12-24 op static void dir_decref(struct dir *);
112 c7f4e1bd 2021-12-24 op static struct fid *new_fid(struct dir *, uint32_t, const char *, struct qid *);
113 c377c1b9 2021-07-30 op static struct fid *fid_by_id(uint32_t);
114 c377c1b9 2021-07-30 op static void free_fid(struct fid *);
116 48192874 2021-08-01 op static void parse_message(const uint8_t *, size_t,
117 48192874 2021-08-01 op struct np_msg_header *, uint8_t **);
119 5585f1c3 2021-12-16 op static void np_write16(struct evbuffer *, uint16_t);
120 5585f1c3 2021-12-16 op static void np_write32(struct evbuffer *, uint32_t);
121 5585f1c3 2021-12-16 op static void np_write64(struct evbuffer *, uint64_t);
122 2845cccb 2021-07-29 op static void np_header(uint32_t, uint8_t, uint16_t);
123 5585f1c3 2021-12-16 op static void np_string(struct evbuffer *, uint16_t, const char *);
124 5585f1c3 2021-12-16 op static void np_qid(struct evbuffer *, struct qid *);
125 84b10f04 2021-07-29 op static void do_send(void);
127 2845cccb 2021-07-29 op static void np_version(uint16_t, uint32_t, const char *);
128 c5d518da 2021-07-29 op static void np_attach(uint16_t, struct qid *);
129 c377c1b9 2021-07-30 op static void np_clunk(uint16_t);
130 36b30273 2021-07-30 op static void np_flush(uint16_t);
131 1c08fc54 2021-08-01 op static void np_walk(uint16_t, int, struct qid *);
132 021481ca 2021-12-13 op static void np_open(uint16_t, struct qid *, uint32_t);
133 3162e55b 2021-12-20 op static void np_create(uint16_t, struct qid *, uint32_t);
134 baa40504 2021-12-15 op static void np_read(uint16_t, uint32_t, void *);
135 2483be55 2021-12-16 op static void np_write(uint16_t, uint32_t);
136 3a2c53f5 2021-12-18 op static void np_stat(uint16_t, uint32_t, void *);
137 0592b956 2021-12-20 cage static void np_remove(uint16_t);
138 2ef72ade 2021-07-28 op static void np_error(uint16_t, const char *);
139 c5d518da 2021-07-29 op static void np_errno(uint16_t);
141 48192874 2021-08-01 op static int np_read8(const char *, const char *, uint8_t *,
142 48192874 2021-08-01 op const uint8_t **, size_t *);
143 48192874 2021-08-01 op static int np_read16(const char *, const char *, uint16_t *,
144 48192874 2021-08-01 op const uint8_t **, size_t *);
145 48192874 2021-08-01 op static int np_read32(const char *, const char *, uint32_t *,
146 48192874 2021-08-01 op const uint8_t **, size_t *);
147 baa40504 2021-12-15 op static int np_read64(const char *, const char *, uint64_t *,
148 baa40504 2021-12-15 op const uint8_t **, size_t *);
150 48192874 2021-08-01 op #define READSTRERR -1
151 48192874 2021-08-01 op #define READSTRTRUNC -2
152 48192874 2021-08-01 op static int np_readstr(const char *, const char *, char *, size_t,
153 48192874 2021-08-01 op const uint8_t **, size_t *);
155 48192874 2021-08-01 op #define NPREAD8(f, dst, src, len) np_read8(__func__, f, dst, src, len)
156 48192874 2021-08-01 op #define NPREAD16(f, dst, src, len) np_read16(__func__, f, dst, src, len)
157 48192874 2021-08-01 op #define NPREAD32(f, dst, src, len) np_read32(__func__, f, dst, src, len)
158 baa40504 2021-12-15 op #define NPREAD64(f, dst, src, len) np_read64(__func__, f, dst, src, len)
160 48192874 2021-08-01 op #define NPREADSTR(f, b, bl, src, len) np_readstr(__func__, f, b, bl, src, len)
162 e60f4e08 2021-07-30 op static void tversion(struct np_msg_header *, const uint8_t *, size_t);
163 e60f4e08 2021-07-30 op static void tattach(struct np_msg_header *, const uint8_t *, size_t);
164 c377c1b9 2021-07-30 op static void tclunk(struct np_msg_header *, const uint8_t *, size_t);
165 36b30273 2021-07-30 op static void tflush(struct np_msg_header *, const uint8_t *, size_t);
166 1c08fc54 2021-08-01 op static void twalk(struct np_msg_header *, const uint8_t *, size_t);
167 021481ca 2021-12-13 op static void topen(struct np_msg_header *, const uint8_t *, size_t);
168 3162e55b 2021-12-20 op static void tcreate(struct np_msg_header *, const uint8_t *, size_t);
169 baa40504 2021-12-15 op static void tread(struct np_msg_header *, const uint8_t *, size_t);
170 2483be55 2021-12-16 op static void twrite(struct np_msg_header *, const uint8_t *, size_t);
171 3a2c53f5 2021-12-18 op static void tstat(struct np_msg_header *, const uint8_t *, size_t);
172 0592b956 2021-12-20 cage static void tremove(struct np_msg_header *, const uint8_t *, size_t);
173 e60f4e08 2021-07-30 op static void handle_message(struct imsg *, size_t);
176 8d1b399b 2021-07-22 op client(int debug, int verbose)
178 8d1b399b 2021-07-22 op struct event ev_sigint, ev_sigterm;
180 8d1b399b 2021-07-22 op log_init(debug, LOG_DAEMON);
181 8d1b399b 2021-07-22 op log_setverbose(verbose);
183 8d1b399b 2021-07-22 op setproctitle("client");
184 8d1b399b 2021-07-22 op log_procinit("client");
186 8d1b399b 2021-07-22 op log_debug("warming up");
188 8d1b399b 2021-07-22 op event_init();
190 8d1b399b 2021-07-22 op /* Setup signal handlers */
191 8d1b399b 2021-07-22 op signal_set(&ev_sigint, SIGINT, client_sig_handler, NULL);
192 8d1b399b 2021-07-22 op signal_set(&ev_sigterm, SIGTERM, client_sig_handler, NULL);
194 8d1b399b 2021-07-22 op signal_add(&ev_sigint, NULL);
195 8d1b399b 2021-07-22 op signal_add(&ev_sigterm, NULL);
197 8d1b399b 2021-07-22 op signal(SIGPIPE, SIG_IGN);
198 8d1b399b 2021-07-22 op signal(SIGHUP, SIG_IGN);
200 8d1b399b 2021-07-22 op /* Setup pipe and event handler to the listener process */
201 8d1b399b 2021-07-22 op if ((iev_listener = malloc(sizeof(*iev_listener))) == NULL)
204 8d1b399b 2021-07-22 op imsg_init(&iev_listener->ibuf, 3);
205 8d1b399b 2021-07-22 op iev_listener->handler = client_dispatch_listener;
207 8d1b399b 2021-07-22 op /* Setup event handlers. */
208 8d1b399b 2021-07-22 op iev_listener->events = EV_READ;
209 8d1b399b 2021-07-22 op event_set(&iev_listener->ev, iev_listener->ibuf.fd,
210 8d1b399b 2021-07-22 op iev_listener->events, iev_listener->handler, iev_listener);
211 8d1b399b 2021-07-22 op event_add(&iev_listener->ev, NULL);
213 8d1b399b 2021-07-22 op event_dispatch();
214 8d1b399b 2021-07-22 op client_shutdown();
217 a97ec9eb 2021-12-23 op static __dead void
218 8d1b399b 2021-07-22 op client_shutdown(void)
220 2ef72ade 2021-07-28 op if (evb != NULL)
221 2ef72ade 2021-07-28 op evbuffer_free(evb);
223 8d1b399b 2021-07-22 op msgbuf_clear(&iev_listener->ibuf.w);
224 8d1b399b 2021-07-22 op close(iev_listener->ibuf.fd);
226 8d1b399b 2021-07-22 op free(iev_listener);
228 223c9e73 2021-08-06 op log_debug("client exiting");
233 8d1b399b 2021-07-22 op client_sig_handler(int sig, short event, void *d)
236 8d1b399b 2021-07-22 op * Normal signal handler rules don't apply because libevent
237 8d1b399b 2021-07-22 op * decouples for us.
240 8d1b399b 2021-07-22 op switch (sig) {
242 8d1b399b 2021-07-22 op case SIGTERM:
243 8d1b399b 2021-07-22 op client_shutdown();
245 8d1b399b 2021-07-22 op fatalx("unexpected signal %d", sig);
249 8d1b399b 2021-07-22 op #define AUTH_NONE 0
250 8d1b399b 2021-07-22 op #define AUTH_USER 1
251 8d1b399b 2021-07-22 op #define AUTH_DONE 2
254 8d1b399b 2021-07-22 op client_dispatch_listener(int fd, short event, void *d)
256 8d1b399b 2021-07-22 op static int auth = AUTH_NONE;
257 8d1b399b 2021-07-22 op static char username[64] = {0};
258 8d1b399b 2021-07-22 op static char dir[PATH_MAX] = {0};
259 8d1b399b 2021-07-22 op struct imsg imsg;
260 8d1b399b 2021-07-22 op struct imsgev *iev = d;
261 8d1b399b 2021-07-22 op struct imsgbuf *ibuf;
263 8d1b399b 2021-07-22 op int shut = 0;
265 8d1b399b 2021-07-22 op ibuf = &iev->ibuf;
267 8d1b399b 2021-07-22 op if (event & EV_READ) {
268 8d1b399b 2021-07-22 op if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
269 8d1b399b 2021-07-22 op fatal("imsg_read error");
270 8d1b399b 2021-07-22 op if (n == 0) /* Connection closed */
273 8d1b399b 2021-07-22 op if (event & EV_WRITE) {
274 8d1b399b 2021-07-22 op if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
275 8d1b399b 2021-07-22 op fatal("msgbuf_write");
276 8d1b399b 2021-07-22 op if (n == 0) /* Connection closed */
281 8d1b399b 2021-07-22 op if ((n = imsg_get(ibuf, &imsg)) == -1)
282 8d1b399b 2021-07-22 op fatal("%s: imsg_get error", __func__);
283 8d1b399b 2021-07-22 op if (n == 0) /* No more messages. */
286 8d1b399b 2021-07-22 op switch (imsg.hdr.type) {
287 8d1b399b 2021-07-22 op case IMSG_AUTH:
288 2ef72ade 2021-07-28 op peerid = imsg.hdr.peerid;
290 8d1b399b 2021-07-22 op fatalx("%s: IMSG_AUTH already done", __func__);
291 8d1b399b 2021-07-22 op auth = AUTH_USER;
292 8d1b399b 2021-07-22 op ((char *)imsg.data)[IMSG_DATA_SIZE(imsg)-1] = '\0';
293 8d1b399b 2021-07-22 op strlcpy(username, imsg.data, sizeof(username));
295 8d1b399b 2021-07-22 op case IMSG_AUTH_DIR:
296 8d1b399b 2021-07-22 op if (auth != AUTH_USER)
297 8d1b399b 2021-07-22 op fatalx("%s: IMSG_AUTH_DIR not after IMSG_AUTH",
299 8d1b399b 2021-07-22 op auth = AUTH_DONE;
300 8d1b399b 2021-07-22 op ((char *)imsg.data)[IMSG_DATA_SIZE(imsg)-1] = '\0';
301 8d1b399b 2021-07-22 op strlcpy(dir, imsg.data, sizeof(dir));
302 8d1b399b 2021-07-22 op client_privdrop(username, dir);
303 8d1b399b 2021-07-22 op memset(username, 0, sizeof(username));
304 8d1b399b 2021-07-22 op memset(dir, 0, sizeof(username));
306 8d1b399b 2021-07-22 op case IMSG_BUF:
309 23e03c88 2021-07-26 op fatalx("%s: can't handle messages before"
310 23e03c88 2021-07-26 op " doing the auth", __func__);
311 23e03c88 2021-07-26 op handle_message(&imsg, IMSG_DATA_SIZE(imsg));
313 8d1b399b 2021-07-22 op case IMSG_CONN_GONE:
314 8d1b399b 2021-07-22 op log_debug("closing");
318 8d1b399b 2021-07-22 op log_debug("%s: unexpected imsg %d",
319 8d1b399b 2021-07-22 op __func__, imsg.hdr.type);
322 8d1b399b 2021-07-22 op imsg_free(&imsg);
326 8d1b399b 2021-07-22 op imsg_event_add(iev);
328 8d1b399b 2021-07-22 op /* This pipe is dead. Remove its event handler. */
329 8d1b399b 2021-07-22 op event_del(&iev->ev);
330 223c9e73 2021-08-06 op log_debug("pipe closed, shutting down...");
331 8d1b399b 2021-07-22 op event_loopexit(NULL);
336 8d1b399b 2021-07-22 op client_privdrop(const char *username, const char *dir)
338 8d1b399b 2021-07-22 op struct passwd *pw;
340 8d1b399b 2021-07-22 op setproctitle("client %s", username);
342 8d1b399b 2021-07-22 op if ((pw = getpwnam(username)) == NULL)
343 8d1b399b 2021-07-22 op fatalx("getpwnam(%s) failed", username);
345 8d1b399b 2021-07-22 op if (chroot(dir) == -1)
346 8d1b399b 2021-07-22 op fatal("chroot");
347 8d1b399b 2021-07-22 op if (chdir("/") == -1)
348 8d1b399b 2021-07-22 op fatal("chdir(\"/\")");
350 8d1b399b 2021-07-22 op if (setgroups(1, &pw->pw_gid) ||
351 8d1b399b 2021-07-22 op setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
352 8d1b399b 2021-07-22 op setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
353 8d1b399b 2021-07-22 op fatal("can't drop privileges");
355 8d1b399b 2021-07-22 op sandbox_client();
356 66a9a40a 2021-07-28 op log_debug("client ready; user=%s dir=%s", username, dir);
358 2ef72ade 2021-07-28 op if ((evb = evbuffer_new()) == NULL)
359 2ef72ade 2021-07-28 op fatal("evbuffer_new");
363 2ef72ade 2021-07-28 op client_send_listener(int type, const void *data, uint16_t len)
367 8d1b399b 2021-07-22 op if ((ret = imsg_compose(&iev_listener->ibuf, type, peerid, 0, -1,
368 8d1b399b 2021-07-22 op data, len)) != -1)
369 8d1b399b 2021-07-22 op imsg_event_add(iev_listener);
374 deb971f5 2021-08-12 op /* set qid fields from sb */
376 deb971f5 2021-08-12 op qid_update_from_sb(struct qid *qid, struct stat *sb)
378 deb971f5 2021-08-12 op qid->path = sb->st_ino;
381 1c08fc54 2021-08-01 op * Theoretically (and hopefully!) this should be a 64 bit
382 1c08fc54 2021-08-01 op * number. Unfortunately, 9P uses 32 bit timestamps.
384 deb971f5 2021-08-12 op qid->vers = sb->st_mtim.tv_sec;
386 deb971f5 2021-08-12 op if (S_ISREG(sb->st_mode))
387 1c08fc54 2021-08-01 op qid->type = QTFILE;
388 deb971f5 2021-08-12 op else if (S_ISDIR(sb->st_mode))
389 1c08fc54 2021-08-01 op qid->type = QTDIR;
390 deb971f5 2021-08-12 op else if (S_ISLNK(sb->st_mode))
391 1c08fc54 2021-08-01 op qid->type = QTSYMLINK;
394 c5d518da 2021-07-29 op /* creates a qid given a fd */
395 c7f4e1bd 2021-12-24 op static struct dir *
396 c7f4e1bd 2021-12-24 op new_dir(int fd)
398 c7f4e1bd 2021-12-24 op struct dir *dir;
400 c7f4e1bd 2021-12-24 op if ((dir = calloc(1, sizeof(*dir))) == NULL)
403 c7f4e1bd 2021-12-24 op dir->fd = fd;
404 c7f4e1bd 2021-12-24 op STAILQ_INSERT_HEAD(&dirs, dir, entries);
408 c7f4e1bd 2021-12-24 op static struct dir *
409 c7f4e1bd 2021-12-24 op dir_incref(struct dir *dir)
411 c7f4e1bd 2021-12-24 op dir->refcount++;
416 c7f4e1bd 2021-12-24 op dir_decref(struct dir *dir)
418 c7f4e1bd 2021-12-24 op if (--dir->refcount > 0)
421 c7f4e1bd 2021-12-24 op STAILQ_REMOVE(&dirs, dir, dir, entries);
423 c7f4e1bd 2021-12-24 op close(dir->fd);
427 c5d518da 2021-07-29 op static struct fid *
428 c7f4e1bd 2021-12-24 op new_fid(struct dir *dir, uint32_t fid, const char *path, struct qid *qid)
430 c7f4e1bd 2021-12-24 op struct fid *f;
431 c7f4e1bd 2021-12-24 op struct qid q;
432 c7f4e1bd 2021-12-24 op struct stat sb;
434 c7f4e1bd 2021-12-24 op if (qid == NULL) {
435 c7f4e1bd 2021-12-24 op if (fstatat(dir->fd, path, &sb, 0)) {
436 c7f4e1bd 2021-12-24 op log_warn("fstatat(%s)", path);
439 c7f4e1bd 2021-12-24 op qid_update_from_sb(&q, &sb);
443 c5d518da 2021-07-29 op if ((f = calloc(1, sizeof(*f))) == NULL)
446 c7f4e1bd 2021-12-24 op f->dir = dir_incref(dir);
447 c5d518da 2021-07-29 op f->fid = fid;
450 c7f4e1bd 2021-12-24 op strlcpy(f->fpath, path, sizeof(f->fpath));
452 c7f4e1bd 2021-12-24 op memcpy(&f->qid, qid, sizeof(f->qid));
454 c5d518da 2021-07-29 op STAILQ_INSERT_HEAD(&fids, f, entries);
459 c377c1b9 2021-07-30 op static struct fid *
460 c377c1b9 2021-07-30 op fid_by_id(uint32_t fid)
462 c377c1b9 2021-07-30 op struct fid *f;
464 c377c1b9 2021-07-30 op STAILQ_FOREACH(f, &fids, entries) {
465 c377c1b9 2021-07-30 op if (f->fid == fid)
473 c377c1b9 2021-07-30 op free_fid(struct fid *f)
477 021481ca 2021-12-13 op if (f->fd != -1) {
478 c7f4e1bd 2021-12-24 op if (f->d != NULL)
479 c7f4e1bd 2021-12-24 op r = closedir(f->d);
481 baa40504 2021-12-15 op r = close(f->fd);
484 baa40504 2021-12-15 op fatal("can't close fid %d", f->fid);
486 1e8d72fd 2021-12-15 op if (f->evb != NULL)
487 1e8d72fd 2021-12-15 op evbuffer_free(f->evb);
489 021481ca 2021-12-13 op /* try to honour ORCLOSE if requested */
490 021481ca 2021-12-13 op if (f->iomode & O_CLOEXEC)
491 c7f4e1bd 2021-12-24 op unlinkat(f->dir->fd, f->fpath, 0);
494 c7f4e1bd 2021-12-24 op dir_decref(f->dir);
496 c377c1b9 2021-07-30 op STAILQ_REMOVE(&fids, f, fid, entries);
501 48192874 2021-08-01 op parse_message(const uint8_t *data, size_t len, struct np_msg_header *hdr,
502 5c485996 2021-07-28 op uint8_t **cnt)
504 48192874 2021-08-01 op size_t olen = len;
506 48192874 2021-08-01 op if (!NPREAD32("len", &hdr->len, &data, &len) ||
507 48192874 2021-08-01 op !NPREAD8("type", &hdr->type, &data, &len) ||
508 48192874 2021-08-01 op !NPREAD16("tag", &hdr->tag, &data, &len))
511 48192874 2021-08-01 op if (olen != hdr->len)
514 23e03c88 2021-07-26 op if (hdr->type < Tversion ||
515 23e03c88 2021-07-26 op hdr->type >= Tmax ||
516 23e03c88 2021-07-26 op hdr->type == Terror ||
517 23e03c88 2021-07-26 op (hdr->type & 0x1) != 0) /* cannot recv a R* */
520 23e03c88 2021-07-26 op hdr->tag = le32toh(hdr->tag);
522 48192874 2021-08-01 op *cnt = (uint8_t *)data;
526 23e03c88 2021-07-26 op /* TODO: send a proper message to terminate the connection. */
527 23e03c88 2021-07-26 op fatalx("got invalid message");
531 5585f1c3 2021-12-16 op np_write16(struct evbuffer *e, uint16_t x)
533 83f6b305 2021-08-01 op x = htole16(x);
534 5585f1c3 2021-12-16 op evbuffer_add(e, &x, sizeof(x));
538 5585f1c3 2021-12-16 op np_write32(struct evbuffer *e, uint32_t x)
540 021481ca 2021-12-13 op x = htole32(x);
541 5585f1c3 2021-12-16 op evbuffer_add(e, &x, sizeof(x));
545 5585f1c3 2021-12-16 op np_write64(struct evbuffer *e, uint64_t x)
547 5585f1c3 2021-12-16 op x = htole64(x);
548 5585f1c3 2021-12-16 op evbuffer_add(e, &x, sizeof(x));
552 5585f1c3 2021-12-16 op np_writebuf(struct evbuffer *e, size_t len, void *data)
554 5585f1c3 2021-12-16 op evbuffer_add(e, data, len);
558 2ef72ade 2021-07-28 op np_header(uint32_t len, uint8_t type, uint16_t tag)
560 9ebb95a7 2021-07-30 op len += HEADERSIZE;
562 63f681aa 2021-07-28 op len = htole32(len);
563 63f681aa 2021-07-28 op tag = htole16(tag);
565 2ef72ade 2021-07-28 op evbuffer_add(evb, &len, sizeof(len));
566 2ef72ade 2021-07-28 op evbuffer_add(evb, &type, sizeof(type));
567 2ef72ade 2021-07-28 op evbuffer_add(evb, &tag, sizeof(tag));
571 5585f1c3 2021-12-16 op np_string(struct evbuffer *e, uint16_t len, const char *str)
573 5c485996 2021-07-28 op uint16_t l = len;
575 5c485996 2021-07-28 op len = htole16(len);
576 5585f1c3 2021-12-16 op evbuffer_add(e, &len, sizeof(len));
577 5585f1c3 2021-12-16 op evbuffer_add(e, str, l);
581 5585f1c3 2021-12-16 op np_qid(struct evbuffer *e, struct qid *qid)
583 c5d518da 2021-07-29 op uint64_t path;
584 c5d518da 2021-07-29 op uint32_t vers;
586 c5d518da 2021-07-29 op path = htole64(qid->path);
587 c5d518da 2021-07-29 op vers = htole32(qid->vers);
589 5585f1c3 2021-12-16 op evbuffer_add(e, &qid->type, sizeof(qid->type));
590 5585f1c3 2021-12-16 op evbuffer_add(e, &vers, sizeof(vers));
591 5585f1c3 2021-12-16 op evbuffer_add(e, &path, sizeof(path));
595 2ef72ade 2021-07-28 op do_send(void)
600 2ef72ade 2021-07-28 op len = EVBUFFER_LENGTH(evb);
601 64c19d90 2021-07-30 op data = EVBUFFER_DATA(evb);
603 64c19d90 2021-07-30 op #if DEBUG_PACKETS
604 78b94752 2021-08-01 op hexdump("outgoing packet", data, len);
606 64c19d90 2021-07-30 op client_send_listener(IMSG_BUF, data, len);
607 2ef72ade 2021-07-28 op evbuffer_drain(evb, len);
611 5c485996 2021-07-28 op np_version(uint16_t tag, uint32_t msize, const char *version)
615 5c485996 2021-07-28 op l = strlen(version);
617 5c485996 2021-07-28 op msize = htole32(msize);
619 9ebb95a7 2021-07-30 op np_header(sizeof(msize) + sizeof(l) + l, Rversion, tag);
620 5c485996 2021-07-28 op evbuffer_add(evb, &msize, sizeof(msize));
621 5585f1c3 2021-12-16 op np_string(evb, l, version);
626 c5d518da 2021-07-29 op np_attach(uint16_t tag, struct qid *qid)
628 9ebb95a7 2021-07-30 op np_header(QIDSIZE, Rattach, tag);
629 5585f1c3 2021-12-16 op np_qid(evb, qid);
634 c377c1b9 2021-07-30 op np_clunk(uint16_t tag)
636 9ebb95a7 2021-07-30 op np_header(0, Rclunk, tag);
641 36b30273 2021-07-30 op np_flush(uint16_t tag)
643 9ebb95a7 2021-07-30 op np_header(0, Rflush, tag);
648 1c08fc54 2021-08-01 op np_walk(uint16_t tag, int nwqid, struct qid *wqid)
652 83f6b305 2021-08-01 op /* two bytes for the counter */
653 83f6b305 2021-08-01 op np_header(2 + QIDSIZE * nwqid, Rwalk, tag);
654 5585f1c3 2021-12-16 op np_write16(evb, nwqid);
655 1c08fc54 2021-08-01 op for (i = 0; i < nwqid; ++i)
656 5585f1c3 2021-12-16 op np_qid(evb, wqid + i);
662 021481ca 2021-12-13 op np_open(uint16_t tag, struct qid *qid, uint32_t iounit)
664 021481ca 2021-12-13 op np_header(QIDSIZE + sizeof(iounit), Ropen, tag);
665 3162e55b 2021-12-20 op np_qid(evb, qid);
666 3162e55b 2021-12-20 op np_write32(evb, iounit);
671 3162e55b 2021-12-20 op np_create(uint16_t tag, struct qid *qid, uint32_t iounit)
673 3162e55b 2021-12-20 op np_header(QIDSIZE + sizeof(iounit), Rcreate, tag);
674 5585f1c3 2021-12-16 op np_qid(evb, qid);
675 5585f1c3 2021-12-16 op np_write32(evb, iounit);
680 baa40504 2021-12-15 op np_read(uint16_t tag, uint32_t count, void *data)
682 baa40504 2021-12-15 op np_header(sizeof(count) + count, Rread, tag);
683 5585f1c3 2021-12-16 op np_write32(evb, count);
684 5585f1c3 2021-12-16 op np_writebuf(evb, count, data);
689 2483be55 2021-12-16 op np_write(uint16_t tag, uint32_t count)
691 2483be55 2021-12-16 op np_header(sizeof(count), Rwrite, tag);
692 2483be55 2021-12-16 op np_write32(evb, count);
697 3a2c53f5 2021-12-18 op np_stat(uint16_t tag, uint32_t count, void *data)
699 3a2c53f5 2021-12-18 op np_header(count, Rstat, tag);
700 3a2c53f5 2021-12-18 op np_writebuf(evb, count, data);
705 0592b956 2021-12-20 cage np_remove(uint16_t tag)
707 0592b956 2021-12-20 cage np_header(0, Rremove, tag);
711 0592b956 2021-12-20 cage static void
712 2ef72ade 2021-07-28 op np_error(uint16_t tag, const char *errstr)
716 2ef72ade 2021-07-28 op l = strlen(errstr);
718 9ebb95a7 2021-07-30 op np_header(sizeof(l) + l, Rerror, tag);
719 5585f1c3 2021-12-16 op np_string(evb, l, errstr);
724 c5d518da 2021-07-29 op np_errno(uint16_t tag)
726 9b088310 2021-07-30 op int saved_errno;
727 c5d518da 2021-07-29 op char buf[64];
729 9b088310 2021-07-30 op saved_errno = errno;
731 c5d518da 2021-07-29 op strerror_r(errno, buf, sizeof(buf));
732 c5d518da 2021-07-29 op np_error(tag, buf);
734 9b088310 2021-07-30 op errno = saved_errno;
738 48192874 2021-08-01 op np_read8(const char *t, const char *f, uint8_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);
755 48192874 2021-08-01 op np_read16(const char *t, const char *f, uint16_t *dst, const uint8_t **src,
758 48192874 2021-08-01 op if (*len < sizeof(*dst)) {
759 48192874 2021-08-01 op log_warnx("%s: wanted %zu bytes for the %s field but only "
760 48192874 2021-08-01 op "%zu are available.", t, sizeof(*dst), f, *len);
764 48192874 2021-08-01 op memcpy(dst, *src, sizeof(*dst));
765 48192874 2021-08-01 op *src += sizeof(*dst);
766 48192874 2021-08-01 op *len -= sizeof(*dst);
767 48192874 2021-08-01 op *dst = le16toh(*dst);
773 48192874 2021-08-01 op np_read32(const char *t, const char *f, uint32_t *dst, const uint8_t **src,
776 48192874 2021-08-01 op if (*len < sizeof(*dst)) {
777 48192874 2021-08-01 op log_warnx("%s: wanted %zu bytes for the %s field but only "
778 48192874 2021-08-01 op "%zu are available.", t, sizeof(*dst), f, *len);
782 48192874 2021-08-01 op memcpy(dst, *src, sizeof(*dst));
783 48192874 2021-08-01 op *src += sizeof(*dst);
784 48192874 2021-08-01 op *len -= sizeof(*dst);
785 48192874 2021-08-01 op *dst = le32toh(*dst);
791 baa40504 2021-12-15 op np_read64(const char *t, const char *f, uint64_t *dst, const uint8_t **src,
794 baa40504 2021-12-15 op if (*len < sizeof(*dst)) {
795 baa40504 2021-12-15 op log_warnx("%s: wanted %zu bytes for the %s field but only "
796 baa40504 2021-12-15 op "%zu are available.", t, sizeof(*dst), f, *len);
800 baa40504 2021-12-15 op memcpy(dst, *src, sizeof(*dst));
801 baa40504 2021-12-15 op *src += sizeof(*dst);
802 baa40504 2021-12-15 op *len -= sizeof(*dst);
803 baa40504 2021-12-15 op *dst = le64toh(*dst);
809 48192874 2021-08-01 op np_readstr(const char *t, const char *f, char *res, size_t reslen,
810 48192874 2021-08-01 op const uint8_t **src, size_t *len)
813 48192874 2021-08-01 op char buf[32];
815 48192874 2021-08-01 op strlcpy(buf, f, sizeof(buf));
816 48192874 2021-08-01 op strlcat(buf, "-len", sizeof(buf));
818 48192874 2021-08-01 op if (!np_read16(t, buf, &sl, src, len))
819 48192874 2021-08-01 op return READSTRERR;
821 48192874 2021-08-01 op if (*len < sl) {
822 48192874 2021-08-01 op log_warnx("%s: wanted %d bytes for the %s field but only "
823 48192874 2021-08-01 op "%zu are available.", t, sl, f, *len);
824 48192874 2021-08-01 op return READSTRERR;
827 48192874 2021-08-01 op if (*len > reslen-1)
828 48192874 2021-08-01 op return READSTRTRUNC;
830 48192874 2021-08-01 op memcpy(res, *src, sl);
831 48192874 2021-08-01 op res[sl] = '\0';
839 e60f4e08 2021-07-30 op tversion(struct np_msg_header *hdr, const uint8_t *data, size_t len)
841 48192874 2021-08-01 op char *dot, version[32];
843 e60f4e08 2021-07-30 op if (handshaked)
846 3d20424d 2021-08-01 op /* msize[4] version[s] */
847 48192874 2021-08-01 op if (!NPREAD32("msize", &msize, &data, &len))
850 48192874 2021-08-01 op switch (NPREADSTR("version", version, sizeof(version), &data, &len)) {
851 48192874 2021-08-01 op case READSTRERR:
853 48192874 2021-08-01 op case READSTRTRUNC:
854 48192874 2021-08-01 op log_warnx("9P version string too long, truncated");
855 48192874 2021-08-01 op goto mismatch;
858 48192874 2021-08-01 op if ((dot = strchr(version, '.')) != NULL)
861 48192874 2021-08-01 op if (strcmp(version, VERSION9P) != 0 ||
863 48192874 2021-08-01 op goto mismatch;
865 48192874 2021-08-01 op /* version matched */
866 48192874 2021-08-01 op handshaked = 1;
867 423f02f5 2021-12-28 cage msize = MIN(msize, CLIENT_MSIZE);
868 e60f4e08 2021-07-30 op client_send_listener(IMSG_MSIZE, &msize, sizeof(msize));
869 e60f4e08 2021-07-30 op np_version(hdr->tag, msize, VERSION9P);
873 48192874 2021-08-01 op log_warnx("unknown 9P version string: \"%s\", want "VERSION9P,
875 48192874 2021-08-01 op np_version(hdr->tag, MSIZE9P, "unknown");
879 e60f4e08 2021-07-30 op client_send_listener(IMSG_CLOSE, NULL, 0);
880 e60f4e08 2021-07-30 op client_shutdown();
884 e60f4e08 2021-07-30 op tattach(struct np_msg_header *hdr, const uint8_t *data, size_t len)
886 c7f4e1bd 2021-12-24 op struct dir *dir;
887 e60f4e08 2021-07-30 op struct fid *f;
888 48192874 2021-08-01 op uint32_t fid, afid;
890 014adfc2 2021-07-30 op char aname[PATH_MAX];
892 e60f4e08 2021-07-30 op /* fid[4] afid[4] uname[s] aname[s] */
894 48192874 2021-08-01 op if (!NPREAD32("fid", &fid, &data, &len) ||
895 48192874 2021-08-01 op !NPREAD32("afid", &afid, &data, &len))
898 48192874 2021-08-01 op /* read the uname but don't actually use it */
899 48192874 2021-08-01 op switch (NPREADSTR("uname", aname, sizeof(aname), &data, &len)) {
900 48192874 2021-08-01 op case READSTRERR:
902 48192874 2021-08-01 op case READSTRTRUNC:
903 48192874 2021-08-01 op np_error(hdr->tag, "name too long");
907 48192874 2021-08-01 op switch (NPREADSTR("aname", aname, sizeof(aname), &data, &len)) {
908 48192874 2021-08-01 op case READSTRERR:
910 48192874 2021-08-01 op case READSTRTRUNC:
911 014adfc2 2021-07-30 op np_error(hdr->tag, "name too long");
915 cf758c33 2021-12-14 op if (fid_by_id(fid) != NULL || afid != NOFID) {
916 cf758c33 2021-12-14 op np_error(hdr->tag, "invalid fid or afid");
920 deb971f5 2021-08-12 op if ((fd = open(aname, O_RDONLY|O_DIRECTORY)) == -1)
923 c7f4e1bd 2021-12-24 op if ((dir = new_dir(fd)) == NULL)
926 cf758c33 2021-12-14 op log_debug("attached %s to %d", aname, fid);
928 c7f4e1bd 2021-12-24 op if ((f = new_fid(dir, fid, aname, NULL)) == NULL) {
929 c7f4e1bd 2021-12-24 op dir_decref(dir);
933 c7f4e1bd 2021-12-24 op np_attach(hdr->tag, &f->qid);
937 deb971f5 2021-08-12 op np_errno(hdr->tag);
938 deb971f5 2021-08-12 op log_warn("failed to attach %s", aname);
942 014adfc2 2021-07-30 op client_send_listener(IMSG_CLOSE, NULL, 0);
943 014adfc2 2021-07-30 op client_shutdown();
947 c377c1b9 2021-07-30 op tclunk(struct np_msg_header *hdr, const uint8_t *data, size_t len)
949 c377c1b9 2021-07-30 op struct fid *f;
950 c377c1b9 2021-07-30 op uint32_t fid;
953 48192874 2021-08-01 op if (!NPREAD32("fid", &fid, &data, &len)) {
954 c377c1b9 2021-07-30 op client_send_listener(IMSG_CLOSE, NULL, 0);
955 c377c1b9 2021-07-30 op client_shutdown();
959 c377c1b9 2021-07-30 op if ((f = fid_by_id(fid)) == NULL) {
960 c377c1b9 2021-07-30 op np_error(hdr->tag, "invalid fid");
965 c377c1b9 2021-07-30 op np_clunk(hdr->tag);
969 36b30273 2021-07-30 op tflush(struct np_msg_header *hdr, const uint8_t *data, size_t len)
971 36b30273 2021-07-30 op uint16_t oldtag;
974 36b30273 2021-07-30 op * We're doing only synchronous I/O. Tflush is implemented
975 36b30273 2021-07-30 op * only because it's illegal to reply with a Rerror.
978 36b30273 2021-07-30 op /* oldtag[2] */
979 36b30273 2021-07-30 op if (len != sizeof(oldtag)) {
980 0592b956 2021-12-20 cage log_warnx("Tflush with the wrong size: got %zu want %zu",
981 36b30273 2021-07-30 op len, sizeof(oldtag));
982 36b30273 2021-07-30 op client_send_listener(IMSG_CLOSE, NULL, 0);
983 36b30273 2021-07-30 op client_shutdown();
987 36b30273 2021-07-30 op np_flush(hdr->tag);
991 1c08fc54 2021-08-01 op twalk(struct np_msg_header *hdr, const uint8_t *data, size_t len)
993 deb971f5 2021-08-12 op struct stat sb;
994 c7f4e1bd 2021-12-24 op struct dir *dir;
995 c7f4e1bd 2021-12-24 op struct qid wqid[MAXWELEM] = {0};
996 1c08fc54 2021-08-01 op struct fid *f, *nf;
997 1c08fc54 2021-08-01 op uint32_t fid, newfid;
998 7ddddcfe 2021-08-01 op uint16_t nwname;
999 deb971f5 2021-08-12 op int fd, oldfd, no, nwqid = 0;
1000 20c188ce 2021-12-16 op char wnam[PATH_MAX];
1002 48192874 2021-08-01 op if (!NPREAD32("fid", &fid, &data, &len) ||
1003 48192874 2021-08-01 op !NPREAD32("newfid", &newfid, &data, &len) ||
1004 48192874 2021-08-01 op !NPREAD16("nwname", &nwname, &data, &len))
1007 1c08fc54 2021-08-01 op if (nwname > MAXWELEM) {
1008 1c08fc54 2021-08-01 op log_warnx("Twalk: more than %d path elements: %d",
1009 1c08fc54 2021-08-01 op MAXWELEM, nwname);
1013 1c08fc54 2021-08-01 op if ((f = fid_by_id(fid)) == NULL) {
1014 1c08fc54 2021-08-01 op np_error(hdr->tag, "invalid fid");
1018 99a43590 2021-12-14 op if (f->fd != -1) {
1019 47c9b8b8 2021-12-02 op np_error(hdr->tag, "fid already opened for I/O");
1023 1c08fc54 2021-08-01 op if (fid == newfid)
1025 1c08fc54 2021-08-01 op else if ((nf = fid_by_id(newfid)) != NULL) {
1026 1c08fc54 2021-08-01 op np_error(hdr->tag, "newfid already in use");
1031 913eba5c 2021-08-01 op /* special case: fid duplication */
1032 22dfb5a0 2021-08-01 op if (nwname == 0) {
1034 1c08fc54 2021-08-01 op * TODO: should we forbid fids duplication when fid ==
1037 1bf47a19 2021-12-23 op if (nf == NULL &&
1038 c7f4e1bd 2021-12-24 op (nf = new_fid(f->dir, newfid, f->fpath, &f->qid)) == NULL)
1039 f987557c 2021-12-02 op fatal("new_fid duplication");
1041 a6036d0f 2021-12-21 op np_walk(hdr->tag, 0, NULL);
1045 c7f4e1bd 2021-12-24 op if (!(f->qid.type & QTDIR)) {
1046 053e2652 2021-08-01 op np_error(hdr->tag, "fid doesn't represent a directory");
1050 c7f4e1bd 2021-12-24 op oldfd = f->dir->fd;
1052 1c08fc54 2021-08-01 op for (nwqid = 0; nwqid < nwname; nwqid++) {
1053 053e2652 2021-08-01 op switch (NPREADSTR("wname", wnam, sizeof(wnam), &data, &len)) {
1054 48192874 2021-08-01 op case READSTRERR:
1056 48192874 2021-08-01 op case READSTRTRUNC:
1057 1c08fc54 2021-08-01 op np_error(hdr->tag, "wname too long");
1061 2532a087 2021-12-20 cage if (*wnam == '\0' ||
1062 054cd6b9 2021-12-14 op strchr(wnam, '/') != NULL ||
1063 054cd6b9 2021-12-14 op !strcmp(wnam, ".")) {
1064 054cd6b9 2021-12-14 op errno = EINVAL;
1065 054cd6b9 2021-12-14 op goto cantopen;
1068 deb971f5 2021-08-12 op if ((fd = openat(oldfd, wnam, O_RDONLY|O_DIRECTORY)) == -1 &&
1069 3242c0bc 2021-12-14 op errno != ENOTDIR)
1070 deb971f5 2021-08-12 op goto cantopen;
1072 deb971f5 2021-08-12 op if ((fd == -1 && fstatat(oldfd, wnam, &sb, 0) == -1) ||
1073 3242c0bc 2021-12-14 op (fd != -1 && fstat(fd, &sb) == -1))
1074 deb971f5 2021-08-12 op goto cantopen;
1076 deb971f5 2021-08-12 op qid_update_from_sb(&wqid[nwqid], &sb);
1078 deb971f5 2021-08-12 op /* reached a file but we still have other components */
1079 deb971f5 2021-08-12 op if (fd == -1 && nwqid+1 < nwname)
1080 deb971f5 2021-08-12 op goto cantopen;
1082 deb971f5 2021-08-12 op /* reached the end and found a file */
1083 deb971f5 2021-08-12 op if (fd == -1 && nwqid+1 == nwname)
1086 c7f4e1bd 2021-12-24 op if (oldfd != f->dir->fd)
1087 deb971f5 2021-08-12 op close(oldfd);
1092 1bf47a19 2021-12-23 op * If fd is -1 we've reached a file, otherwise we've just
1093 1bf47a19 2021-12-23 op * reached another directory. We must pay attention to what
1094 c7f4e1bd 2021-12-24 op * file descriptor we use to create the dir, because if we've
1095 c7f4e1bd 2021-12-24 op * reached a file and oldfd is f->dir->fd then we *must* share
1096 c7f4e1bd 2021-12-24 op * the same dir (it was a walk of one path from a directory to a
1097 1bf47a19 2021-12-23 op * file, otherwise fun is bound to happen as soon as the client
1098 1bf47a19 2021-12-23 op * closes the fid for the directory but keeps the one for the
1101 c7f4e1bd 2021-12-24 op if (fd == -1 && oldfd == f->dir->fd)
1102 c7f4e1bd 2021-12-24 op dir = f->dir;
1103 1bf47a19 2021-12-23 op else if (fd == -1)
1104 c7f4e1bd 2021-12-24 op dir = new_dir(oldfd);
1106 c7f4e1bd 2021-12-24 op dir = new_dir(fd);
1108 c7f4e1bd 2021-12-24 op if (dir == NULL)
1109 c7f4e1bd 2021-12-24 op fatal("new_dir");
1111 1c08fc54 2021-08-01 op if (nf == NULL) {
1112 c7f4e1bd 2021-12-24 op if ((nf = new_fid(dir, newfid, wnam, &wqid[nwqid-1])) == NULL)
1113 1bf47a19 2021-12-23 op fatal("new fid");
1115 c7f4e1bd 2021-12-24 op /* update the dir */
1116 c7f4e1bd 2021-12-24 op dir_decref(nf->dir);
1117 c7f4e1bd 2021-12-24 op nf->dir = dir_incref(dir);
1120 1c08fc54 2021-08-01 op np_walk(hdr->tag, nwqid, wqid);
1124 c7f4e1bd 2021-12-24 op if (oldfd != f->dir->fd)
1125 deb971f5 2021-08-12 op close(oldfd);
1127 deb971f5 2021-08-12 op if (nwqid == 0)
1128 deb971f5 2021-08-12 op np_error(hdr->tag, strerror(no));
1130 deb971f5 2021-08-12 op np_walk(hdr->tag, nwqid, wqid);
1134 1c08fc54 2021-08-01 op client_send_listener(IMSG_CLOSE, NULL, 0);
1135 1c08fc54 2021-08-01 op client_shutdown();
1138 021481ca 2021-12-13 op static inline int
1139 021481ca 2021-12-13 op npmode_to_unix(uint8_t mode, int *flags)
1141 021481ca 2021-12-13 op switch (mode & 0x0F) {
1142 021481ca 2021-12-13 op case KOREAD:
1143 021481ca 2021-12-13 op *flags = O_RDONLY;
1145 021481ca 2021-12-13 op case KOWRITE:
1146 021481ca 2021-12-13 op *flags = O_WRONLY;
1148 021481ca 2021-12-13 op case KORDWR:
1149 021481ca 2021-12-13 op *flags = O_RDWR;
1151 021481ca 2021-12-13 op case KOEXEC:
1152 021481ca 2021-12-13 op log_warnx("tried to open something with KOEXEC");
1153 021481ca 2021-12-13 op /* fallthrough */
1158 021481ca 2021-12-13 op if (mode & KOTRUNC)
1159 021481ca 2021-12-13 op *flags |= O_TRUNC;
1160 021481ca 2021-12-13 op if (mode & KORCLOSE)
1161 021481ca 2021-12-13 op *flags |= O_CLOEXEC;
1167 021481ca 2021-12-13 op topen(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1169 021481ca 2021-12-13 op struct stat sb;
1170 021481ca 2021-12-13 op struct qid qid;
1171 021481ca 2021-12-13 op struct fid *f;
1172 021481ca 2021-12-13 op uint32_t fid;
1173 021481ca 2021-12-13 op uint8_t mode;
1174 87a818e8 2021-12-14 op const char *path;
1176 021481ca 2021-12-13 op /* fid[4] mode[1] */
1177 021481ca 2021-12-13 op if (!NPREAD32("fid", &fid, &data, &len) ||
1178 021481ca 2021-12-13 op !NPREAD8("mode", &mode, &data, &len)) {
1179 021481ca 2021-12-13 op client_send_listener(IMSG_CLOSE, NULL, 0);
1180 021481ca 2021-12-13 op client_shutdown();
1184 021481ca 2021-12-13 op if ((f = fid_by_id(fid)) == NULL || f->fd != -1) {
1185 021481ca 2021-12-13 op np_error(hdr->tag, "invalid fid");
1189 d4e43815 2021-12-15 op if (npmode_to_unix(mode, &f->iomode) == -1) {
1190 d4e43815 2021-12-15 op np_error(hdr->tag, "invalid mode");
1194 1bf47a19 2021-12-23 op path = f->fpath;
1195 c7f4e1bd 2021-12-24 op if (f->qid.type & QTDIR)
1198 c7f4e1bd 2021-12-24 op if ((f->fd = openat(f->dir->fd, path, f->iomode)) == -1) {
1199 021481ca 2021-12-13 op np_error(hdr->tag, strerror(errno));
1203 021481ca 2021-12-13 op if (fstat(f->fd, &sb) == -1)
1204 021481ca 2021-12-13 op fatal("fstat");
1206 baa40504 2021-12-15 op if (S_ISDIR(sb.st_mode)) {
1207 c7f4e1bd 2021-12-24 op if ((f->d = fdopendir(f->fd)) == NULL) {
1208 baa40504 2021-12-15 op np_errno(hdr->tag);
1209 baa40504 2021-12-15 op close(f->fd);
1214 c0ae57b9 2021-12-15 op if ((f->evb = evbuffer_new()) == NULL) {
1215 c0ae57b9 2021-12-15 op np_errno(hdr->tag);
1216 c7f4e1bd 2021-12-24 op closedir(f->d);
1217 c7f4e1bd 2021-12-24 op f->d = NULL;
1222 baa40504 2021-12-15 op f->offset = 0;
1224 021481ca 2021-12-13 op qid_update_from_sb(&qid, &sb);
1225 021481ca 2021-12-13 op np_open(hdr->tag, &qid, sb.st_blksize);
1229 3162e55b 2021-12-20 op tcreate(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1231 3162e55b 2021-12-20 op struct stat sb;
1232 3162e55b 2021-12-20 op struct qid qid;
1233 3162e55b 2021-12-20 op struct fid *f;
1234 3162e55b 2021-12-20 op uint32_t fid, perm;
1235 3162e55b 2021-12-20 op uint8_t mode;
1236 3162e55b 2021-12-20 op char name[PATH_MAX];
1238 3162e55b 2021-12-20 op /* fid[4] name[s] perm[4] mode[1] */
1239 3162e55b 2021-12-20 op if (!NPREAD32("fid", &fid, &data, &len))
1241 3162e55b 2021-12-20 op switch (NPREADSTR("name", name, sizeof(name), &data, &len)) {
1242 3162e55b 2021-12-20 op case READSTRERR:
1244 3162e55b 2021-12-20 op case READSTRTRUNC:
1245 3162e55b 2021-12-20 op np_error(hdr->tag, "name too long");
1248 3162e55b 2021-12-20 op if (!NPREAD32("perm", &perm, &data, &len) ||
1249 3162e55b 2021-12-20 op !NPREAD8("mode", &mode, &data, &len))
1252 5547a835 2021-12-20 op if (!strcmp(name, ".") || !strcmp(name, "..") ||
1253 5547a835 2021-12-20 op strchr(name, '/') != NULL) {
1254 5547a835 2021-12-20 op np_error(hdr->tag, "invalid name");
1258 3162e55b 2021-12-20 op if ((f = fid_by_id(fid)) == NULL || f->fd != -1) {
1259 3162e55b 2021-12-20 op np_error(hdr->tag, "invalid fid");
1263 c7f4e1bd 2021-12-24 op if (!(f->qid.type & QTDIR)) {
1264 3162e55b 2021-12-20 op np_error(hdr->tag, "fid doesn't identify a directory");
1268 3162e55b 2021-12-20 op if (npmode_to_unix(mode, &f->iomode) == -1) {
1269 3162e55b 2021-12-20 op np_error(hdr->tag, "invalid mode");
1273 3162e55b 2021-12-20 op if (f->iomode & O_RDONLY) {
1274 3162e55b 2021-12-20 op np_error(hdr->tag, "can't create a read-only file");
1278 3162e55b 2021-12-20 op /* TODO: parse the mode */
1280 3162e55b 2021-12-20 op if (perm & 0x80000000) {
1281 3162e55b 2021-12-20 op /* create a directory */
1282 c7f4e1bd 2021-12-24 op f->fd = mkdirat(f->dir->fd, name, 0755);
1284 3162e55b 2021-12-20 op /* create a file */
1285 c7f4e1bd 2021-12-24 op f->fd = openat(f->dir->fd, name, f->iomode | O_CREAT | O_TRUNC,
1289 3162e55b 2021-12-20 op if (f->fd == -1) {
1290 3162e55b 2021-12-20 op np_errno(hdr->tag);
1294 3162e55b 2021-12-20 op if (fstat(f->fd, &sb) == -1)
1295 3162e55b 2021-12-20 op fatal("fstat");
1297 3162e55b 2021-12-20 op if (S_ISDIR(sb.st_mode)) {
1298 c7f4e1bd 2021-12-24 op if ((f->d = fdopendir(f->fd)) == NULL) {
1299 3162e55b 2021-12-20 op np_errno(hdr->tag);
1300 3162e55b 2021-12-20 op close(f->fd);
1305 3162e55b 2021-12-20 op if ((f->evb = evbuffer_new()) == NULL) {
1306 3162e55b 2021-12-20 op np_errno(hdr->tag);
1307 c7f4e1bd 2021-12-24 op closedir(f->d);
1308 c7f4e1bd 2021-12-24 op f->d = NULL;
1313 3162e55b 2021-12-20 op f->offset = 0;
1315 3162e55b 2021-12-20 op qid_update_from_sb(&qid, &sb);
1316 3162e55b 2021-12-20 op np_create(hdr->tag, &qid, sb.st_blksize);
1321 3162e55b 2021-12-20 op client_send_listener(IMSG_CLOSE, NULL, 0);
1322 3162e55b 2021-12-20 op client_shutdown();
1325 5585f1c3 2021-12-16 op static inline void
1326 3a2c53f5 2021-12-18 op serialize_stat(const char *fname, struct stat *sb, struct evbuffer *evb)
1328 5585f1c3 2021-12-16 op struct qid qid;
1329 5585f1c3 2021-12-16 op const char *uid, *gid, *muid;
1331 5585f1c3 2021-12-16 op uint16_t namlen, uidlen, gidlen, ulen;
1333 3a2c53f5 2021-12-18 op qid_update_from_sb(&qid, sb);
1335 5585f1c3 2021-12-16 op /* TODO: fill these fields */
1340 3a2c53f5 2021-12-18 op namlen = strlen(fname);
1341 5585f1c3 2021-12-16 op uidlen = strlen(uid);
1342 5585f1c3 2021-12-16 op gidlen = strlen(gid);
1343 5585f1c3 2021-12-16 op ulen = strlen(muid);
1345 5585f1c3 2021-12-16 op tot = NPSTATSIZ(namlen, uidlen, gidlen, ulen);
1346 5585f1c3 2021-12-16 op if (tot > UINT32_MAX) {
1347 5585f1c3 2021-12-16 op log_warnx("stat info for dir entry %s would overflow",
1352 5585f1c3 2021-12-16 op np_write16(evb, tot); /* size[2] */
1353 3a2c53f5 2021-12-18 op np_write16(evb, sb->st_rdev); /* type[2] */
1354 3a2c53f5 2021-12-18 op np_write32(evb, sb->st_dev); /* dev[4] */
1355 5585f1c3 2021-12-16 op np_qid(evb, &qid); /* qid[13] */
1357 5585f1c3 2021-12-16 op /* XXX: translate? */
1358 3a2c53f5 2021-12-18 op np_write32(evb, sb->st_mode); /* mode[4] */
1360 3a2c53f5 2021-12-18 op np_write32(evb, sb->st_atim.tv_sec); /* atime[4] */
1361 3a2c53f5 2021-12-18 op np_write32(evb, sb->st_mtim.tv_sec); /* mtime[4] */
1362 3a2c53f5 2021-12-18 op np_write64(evb, sb->st_size); /* length[8] */
1363 3a2c53f5 2021-12-18 op np_string(evb, namlen, fname); /* name[s] */
1364 5585f1c3 2021-12-16 op np_string(evb, uidlen, uid); /* uid[s] */
1365 5585f1c3 2021-12-16 op np_string(evb, gidlen, gid); /* gid[s] */
1366 5585f1c3 2021-12-16 op np_string(evb, ulen, muid); /* muid[s] */
1370 baa40504 2021-12-15 op tread(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1372 baa40504 2021-12-15 op struct fid *f;
1374 baa40504 2021-12-15 op uint64_t off;
1375 baa40504 2021-12-15 op uint32_t fid, count;
1376 baa40504 2021-12-15 op char buf[2048];
1378 baa40504 2021-12-15 op /* fid[4] offset[8] count[4] */
1379 baa40504 2021-12-15 op if (!NPREAD32("fid", &fid, &data, &len) ||
1380 baa40504 2021-12-15 op !NPREAD64("offset", &off, &data, &len) ||
1381 baa40504 2021-12-15 op !NPREAD32("count", &count, &data, &len)) {
1382 baa40504 2021-12-15 op client_send_listener(IMSG_CLOSE, NULL, 0);
1383 baa40504 2021-12-15 op client_shutdown();
1387 5f362b7f 2021-12-15 op if ((f = fid_by_id(fid)) == NULL || f->fd == -1) {
1388 baa40504 2021-12-15 op np_error(hdr->tag, "invalid fid");
1392 baa40504 2021-12-15 op if (TYPE_OVERFLOW(off_t, off)) {
1393 423f02f5 2021-12-28 cage log_warnx("unexpected off_t size");
1394 baa40504 2021-12-15 op np_error(hdr->tag, "invalid offset");
1398 c7f4e1bd 2021-12-24 op if (f->d == NULL) {
1399 baa40504 2021-12-15 op /* read a file */
1400 baa40504 2021-12-15 op r = pread(f->fd, buf, sizeof(buf), (off_t)off);
1401 baa40504 2021-12-15 op if (r == -1)
1402 baa40504 2021-12-15 op np_errno(hdr->tag);
1404 baa40504 2021-12-15 op np_read(hdr->tag, r, buf);
1406 fcf8ca15 2021-12-16 op if (off == 0 && f->offset != 0) {
1407 c7f4e1bd 2021-12-24 op rewinddir(f->d);
1408 fcf8ca15 2021-12-16 op f->offset = 0;
1409 423f02f5 2021-12-28 cage evbuffer_drain(f->evb, EVBUFFER_LENGTH(f->evb));
1412 5585f1c3 2021-12-16 op if (off != f->offset) {
1413 5585f1c3 2021-12-16 op np_error(hdr->tag, "can't seek in directories");
1417 5585f1c3 2021-12-16 op while (EVBUFFER_LENGTH(f->evb) < count) {
1418 5585f1c3 2021-12-16 op struct dirent *d;
1419 3a2c53f5 2021-12-18 op struct stat sb;
1421 c7f4e1bd 2021-12-24 op if ((d = readdir(f->d)) == NULL)
1423 3a2c53f5 2021-12-18 op if (fstatat(f->fd, d->d_name, &sb, 0) == -1) {
1424 3a2c53f5 2021-12-18 op warn("fstatat");
1427 3a2c53f5 2021-12-18 op serialize_stat(d->d_name, &sb, f->evb);
1430 5585f1c3 2021-12-16 op count = MIN(count, EVBUFFER_LENGTH(f->evb));
1431 5585f1c3 2021-12-16 op np_read(hdr->tag, count, EVBUFFER_DATA(f->evb));
1432 5585f1c3 2021-12-16 op evbuffer_drain(f->evb, count);
1434 5585f1c3 2021-12-16 op f->offset += count;
1439 2483be55 2021-12-16 op twrite(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1441 2483be55 2021-12-16 op struct fid *f;
1443 2483be55 2021-12-16 op uint64_t off;
1444 2483be55 2021-12-16 op uint32_t fid, count;
1446 2483be55 2021-12-16 op /* fid[4] offset[8] count[4] data[count] */
1447 2483be55 2021-12-16 op if (!NPREAD32("fid", &fid, &data, &len) ||
1448 2483be55 2021-12-16 op !NPREAD64("off", &off, &data, &len) ||
1449 2483be55 2021-12-16 op !NPREAD32("count", &count, &data, &len) ||
1450 2483be55 2021-12-16 op len != count) {
1451 2483be55 2021-12-16 op client_send_listener(IMSG_CLOSE, NULL, 0);
1452 2483be55 2021-12-16 op client_shutdown();
1456 2483be55 2021-12-16 op if ((f = fid_by_id(fid)) == NULL || f->fd == -1) {
1457 2483be55 2021-12-16 op np_error(hdr->tag, "invalid fid");
1461 2483be55 2021-12-16 op if (!(f->iomode & O_WRONLY) &&
1462 2483be55 2021-12-16 op !(f->iomode & O_RDWR)) {
1463 2483be55 2021-12-16 op np_error(hdr->tag, "fid not opened for writing");
1467 2483be55 2021-12-16 op if (TYPE_OVERFLOW(off_t, off)) {
1468 2483be55 2021-12-16 op log_warnx("unexpected off_t size");
1469 2483be55 2021-12-16 op np_error(hdr->tag, "invalid offset");
1473 6ace3b52 2021-12-17 op if ((r = pwrite(f->fd, data, len, off)) == -1)
1474 2483be55 2021-12-16 op np_errno(hdr->tag);
1476 2483be55 2021-12-16 op np_write(hdr->tag, r);
1480 3a2c53f5 2021-12-18 op tstat(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1482 3a2c53f5 2021-12-18 op struct evbuffer *evb;
1483 3a2c53f5 2021-12-18 op struct stat sb;
1484 3a2c53f5 2021-12-18 op struct fid *f;
1486 3a2c53f5 2021-12-18 op uint32_t fid;
1488 3a2c53f5 2021-12-18 op /* fid[4] */
1489 3a2c53f5 2021-12-18 op if (!NPREAD32("fid", &fid, &data, &len)) {
1490 3a2c53f5 2021-12-18 op client_send_listener(IMSG_CLOSE, NULL, 0);
1491 3a2c53f5 2021-12-18 op client_shutdown();
1496 3a2c53f5 2021-12-18 op * plan9' stat(9P) is not clear on whether the stat is allowed
1497 3a2c53f5 2021-12-18 op * on opened fids or not.
1499 3a2c53f5 2021-12-18 op if ((f = fid_by_id(fid)) == NULL) {
1500 3a2c53f5 2021-12-18 op np_error(hdr->tag, "invalid fid");
1504 3a2c53f5 2021-12-18 op if ((evb = evbuffer_new()) == NULL)
1505 3a2c53f5 2021-12-18 op fatal("evbuffer_new");
1507 1bf47a19 2021-12-23 op if (f->fd != -1)
1508 1bf47a19 2021-12-23 op r = fstat(f->fd, &sb);
1510 c7f4e1bd 2021-12-24 op r = fstatat(f->dir->fd, f->fpath, &sb, 0);
1512 3a2c53f5 2021-12-18 op if (r == -1) {
1513 3a2c53f5 2021-12-18 op np_errno(hdr->tag);
1514 3a2c53f5 2021-12-18 op evbuffer_free(evb);
1518 1bf47a19 2021-12-23 op serialize_stat(f->fpath, &sb, evb);
1519 3a2c53f5 2021-12-18 op np_stat(hdr->tag, EVBUFFER_LENGTH(evb), EVBUFFER_DATA(evb));
1520 3a2c53f5 2021-12-18 op evbuffer_free(evb);
1523 0592b956 2021-12-20 cage static void
1524 0592b956 2021-12-20 cage tremove(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1526 0592b956 2021-12-20 cage struct fid *f;
1527 0592b956 2021-12-20 cage uint32_t fid;
1529 946136f3 2021-12-26 op char dirpath[PATH_MAX + 3];
1531 0592b956 2021-12-20 cage /* fid[4] */
1532 0592b956 2021-12-20 cage if (!NPREAD32("fid", &fid, &data, &len)) {
1533 0592b956 2021-12-20 cage client_send_listener(IMSG_CLOSE, NULL, 0);
1534 0592b956 2021-12-20 cage client_shutdown();
1538 0592b956 2021-12-20 cage if ((f = fid_by_id(fid)) == NULL) {
1539 0592b956 2021-12-20 cage np_error(hdr->tag, "invalid fid");
1543 946136f3 2021-12-26 op if (f->qid.type & QTDIR) { /* directory */
1544 946136f3 2021-12-26 op strlcpy(dirpath, "../", sizeof(dirpath));
1545 946136f3 2021-12-26 op strlcat(dirpath, f->fpath, sizeof(dirpath));
1546 946136f3 2021-12-26 op r = unlinkat(f->dir->fd, dirpath, AT_REMOVEDIR);
1547 946136f3 2021-12-26 op } else /* file */
1548 c7f4e1bd 2021-12-24 op r = unlinkat(f->dir->fd, f->fpath, 0);
1550 0592b956 2021-12-20 cage if (r == -1)
1551 0592b956 2021-12-20 cage np_errno(hdr->tag);
1553 0592b956 2021-12-20 cage np_remove(hdr->tag);
1555 0592b956 2021-12-20 cage free_fid(f);
1559 e60f4e08 2021-07-30 op handle_message(struct imsg *imsg, size_t len)
1561 e60f4e08 2021-07-30 op struct msg {
1562 e60f4e08 2021-07-30 op uint8_t type;
1563 e60f4e08 2021-07-30 op void (*fn)(struct np_msg_header *, const uint8_t *, size_t);
1564 e60f4e08 2021-07-30 op } msgs[] = {
1565 e60f4e08 2021-07-30 op {Tversion, tversion},
1566 e60f4e08 2021-07-30 op {Tattach, tattach},
1567 c377c1b9 2021-07-30 op {Tclunk, tclunk},
1568 36b30273 2021-07-30 op {Tflush, tflush},
1569 1c08fc54 2021-08-01 op {Twalk, twalk},
1570 021481ca 2021-12-13 op {Topen, topen},
1571 3162e55b 2021-12-20 op {Tcreate, tcreate},
1572 baa40504 2021-12-15 op {Tread, tread},
1573 2483be55 2021-12-16 op {Twrite, twrite},
1574 3a2c53f5 2021-12-18 op {Tstat, tstat},
1575 0592b956 2021-12-20 cage {Tremove, tremove},
1577 e60f4e08 2021-07-30 op struct np_msg_header hdr;
1579 e60f4e08 2021-07-30 op uint8_t *data;
1581 64c19d90 2021-07-30 op #if DEBUG_PACKETS
1582 78b94752 2021-08-01 op hexdump("incoming packet", imsg->data, len);
1585 e60f4e08 2021-07-30 op parse_message(imsg->data, len, &hdr, &data);
1586 e60f4e08 2021-07-30 op len -= HEADERSIZE;
1588 e60f4e08 2021-07-30 op log_debug("got request: len=%d type=%d[%s] tag=%d",
1589 e60f4e08 2021-07-30 op hdr.len, hdr.type, pp_msg_type(hdr.type), hdr.tag);
1591 e60f4e08 2021-07-30 op if (!handshaked && hdr.type != Tversion) {
1592 e60f4e08 2021-07-30 op client_send_listener(IMSG_CLOSE, NULL, 0);
1593 e60f4e08 2021-07-30 op client_shutdown();
1597 e60f4e08 2021-07-30 op for (i = 0; i < sizeof(msgs)/sizeof(msgs[0]); ++i) {
1598 e60f4e08 2021-07-30 op if (msgs[i].type != hdr.type)
1601 e60f4e08 2021-07-30 op msgs[i].fn(&hdr, data, len);
1605 e60f4e08 2021-07-30 op np_error(hdr.tag, "Not supported.");