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 1cf719e9 2021-01-02 op if (sizeof(count) + count + HEADERSIZE >= msize) {
683 1cf719e9 2021-01-02 op np_error(tag, "Rread would overflow");
687 baa40504 2021-12-15 op np_header(sizeof(count) + count, Rread, tag);
688 5585f1c3 2021-12-16 op np_write32(evb, count);
689 5585f1c3 2021-12-16 op np_writebuf(evb, count, data);
694 2483be55 2021-12-16 op np_write(uint16_t tag, uint32_t count)
696 2483be55 2021-12-16 op np_header(sizeof(count), Rwrite, tag);
697 2483be55 2021-12-16 op np_write32(evb, count);
702 3a2c53f5 2021-12-18 op np_stat(uint16_t tag, uint32_t count, void *data)
704 1cf719e9 2021-01-02 op if (sizeof(count) + count + HEADERSIZE >= msize) {
705 1cf719e9 2021-01-02 op np_error(tag, "Rstat would overflow");
709 3a2c53f5 2021-12-18 op np_header(count, Rstat, tag);
710 3a2c53f5 2021-12-18 op np_writebuf(evb, count, data);
715 0592b956 2021-12-20 cage np_remove(uint16_t tag)
717 0592b956 2021-12-20 cage np_header(0, Rremove, tag);
721 0592b956 2021-12-20 cage static void
722 2ef72ade 2021-07-28 op np_error(uint16_t tag, const char *errstr)
726 2ef72ade 2021-07-28 op l = strlen(errstr);
728 9ebb95a7 2021-07-30 op np_header(sizeof(l) + l, Rerror, tag);
729 5585f1c3 2021-12-16 op np_string(evb, l, errstr);
734 c5d518da 2021-07-29 op np_errno(uint16_t tag)
736 9b088310 2021-07-30 op int saved_errno;
737 5c0ab198 2021-12-29 op char buf[NL_TEXTMAX] = {0};
739 9b088310 2021-07-30 op saved_errno = errno;
741 c5d518da 2021-07-29 op strerror_r(errno, buf, sizeof(buf));
742 c5d518da 2021-07-29 op np_error(tag, buf);
744 9b088310 2021-07-30 op errno = saved_errno;
748 48192874 2021-08-01 op np_read8(const char *t, const char *f, uint8_t *dst, const uint8_t **src,
751 48192874 2021-08-01 op if (*len < sizeof(*dst)) {
752 48192874 2021-08-01 op log_warnx("%s: wanted %zu bytes for the %s field but only "
753 48192874 2021-08-01 op "%zu are available.", t, sizeof(*dst), f, *len);
757 48192874 2021-08-01 op memcpy(dst, *src, sizeof(*dst));
758 48192874 2021-08-01 op *src += sizeof(*dst);
759 48192874 2021-08-01 op *len -= sizeof(*dst);
765 48192874 2021-08-01 op np_read16(const char *t, const char *f, uint16_t *dst, const uint8_t **src,
768 48192874 2021-08-01 op if (*len < sizeof(*dst)) {
769 48192874 2021-08-01 op log_warnx("%s: wanted %zu bytes for the %s field but only "
770 48192874 2021-08-01 op "%zu are available.", t, sizeof(*dst), f, *len);
774 48192874 2021-08-01 op memcpy(dst, *src, sizeof(*dst));
775 48192874 2021-08-01 op *src += sizeof(*dst);
776 48192874 2021-08-01 op *len -= sizeof(*dst);
777 48192874 2021-08-01 op *dst = le16toh(*dst);
783 48192874 2021-08-01 op np_read32(const char *t, const char *f, uint32_t *dst, const uint8_t **src,
786 48192874 2021-08-01 op if (*len < sizeof(*dst)) {
787 48192874 2021-08-01 op log_warnx("%s: wanted %zu bytes for the %s field but only "
788 48192874 2021-08-01 op "%zu are available.", t, sizeof(*dst), f, *len);
792 48192874 2021-08-01 op memcpy(dst, *src, sizeof(*dst));
793 48192874 2021-08-01 op *src += sizeof(*dst);
794 48192874 2021-08-01 op *len -= sizeof(*dst);
795 48192874 2021-08-01 op *dst = le32toh(*dst);
801 baa40504 2021-12-15 op np_read64(const char *t, const char *f, uint64_t *dst, const uint8_t **src,
804 baa40504 2021-12-15 op if (*len < sizeof(*dst)) {
805 baa40504 2021-12-15 op log_warnx("%s: wanted %zu bytes for the %s field but only "
806 baa40504 2021-12-15 op "%zu are available.", t, sizeof(*dst), f, *len);
810 baa40504 2021-12-15 op memcpy(dst, *src, sizeof(*dst));
811 baa40504 2021-12-15 op *src += sizeof(*dst);
812 baa40504 2021-12-15 op *len -= sizeof(*dst);
813 baa40504 2021-12-15 op *dst = le64toh(*dst);
819 48192874 2021-08-01 op np_readstr(const char *t, const char *f, char *res, size_t reslen,
820 48192874 2021-08-01 op const uint8_t **src, size_t *len)
823 48192874 2021-08-01 op char buf[32];
825 48192874 2021-08-01 op strlcpy(buf, f, sizeof(buf));
826 48192874 2021-08-01 op strlcat(buf, "-len", sizeof(buf));
828 48192874 2021-08-01 op if (!np_read16(t, buf, &sl, src, len))
829 48192874 2021-08-01 op return READSTRERR;
831 48192874 2021-08-01 op if (*len < sl) {
832 48192874 2021-08-01 op log_warnx("%s: wanted %d bytes for the %s field but only "
833 48192874 2021-08-01 op "%zu are available.", t, sl, f, *len);
834 48192874 2021-08-01 op return READSTRERR;
837 48192874 2021-08-01 op if (*len > reslen-1)
838 48192874 2021-08-01 op return READSTRTRUNC;
840 48192874 2021-08-01 op memcpy(res, *src, sl);
841 48192874 2021-08-01 op res[sl] = '\0';
849 e60f4e08 2021-07-30 op tversion(struct np_msg_header *hdr, const uint8_t *data, size_t len)
851 48192874 2021-08-01 op char *dot, version[32];
853 e60f4e08 2021-07-30 op if (handshaked)
856 3d20424d 2021-08-01 op /* msize[4] version[s] */
857 48192874 2021-08-01 op if (!NPREAD32("msize", &msize, &data, &len))
860 48192874 2021-08-01 op switch (NPREADSTR("version", version, sizeof(version), &data, &len)) {
861 48192874 2021-08-01 op case READSTRERR:
863 48192874 2021-08-01 op case READSTRTRUNC:
864 48192874 2021-08-01 op log_warnx("9P version string too long, truncated");
865 48192874 2021-08-01 op goto mismatch;
868 48192874 2021-08-01 op if ((dot = strchr(version, '.')) != NULL)
871 48192874 2021-08-01 op if (strcmp(version, VERSION9P) != 0 ||
873 48192874 2021-08-01 op goto mismatch;
875 48192874 2021-08-01 op /* version matched */
876 48192874 2021-08-01 op handshaked = 1;
877 423f02f5 2021-12-28 cage msize = MIN(msize, CLIENT_MSIZE);
878 e60f4e08 2021-07-30 op client_send_listener(IMSG_MSIZE, &msize, sizeof(msize));
879 e60f4e08 2021-07-30 op np_version(hdr->tag, msize, VERSION9P);
883 48192874 2021-08-01 op log_warnx("unknown 9P version string: \"%s\", want "VERSION9P,
885 48192874 2021-08-01 op np_version(hdr->tag, MSIZE9P, "unknown");
889 e60f4e08 2021-07-30 op client_send_listener(IMSG_CLOSE, NULL, 0);
890 e60f4e08 2021-07-30 op client_shutdown();
894 e60f4e08 2021-07-30 op tattach(struct np_msg_header *hdr, const uint8_t *data, size_t len)
896 c7f4e1bd 2021-12-24 op struct dir *dir;
897 e60f4e08 2021-07-30 op struct fid *f;
898 48192874 2021-08-01 op uint32_t fid, afid;
900 014adfc2 2021-07-30 op char aname[PATH_MAX];
902 e60f4e08 2021-07-30 op /* fid[4] afid[4] uname[s] aname[s] */
904 48192874 2021-08-01 op if (!NPREAD32("fid", &fid, &data, &len) ||
905 48192874 2021-08-01 op !NPREAD32("afid", &afid, &data, &len))
908 48192874 2021-08-01 op /* read the uname but don't actually use it */
909 48192874 2021-08-01 op switch (NPREADSTR("uname", aname, sizeof(aname), &data, &len)) {
910 48192874 2021-08-01 op case READSTRERR:
912 48192874 2021-08-01 op case READSTRTRUNC:
913 48192874 2021-08-01 op np_error(hdr->tag, "name too long");
917 48192874 2021-08-01 op switch (NPREADSTR("aname", aname, sizeof(aname), &data, &len)) {
918 48192874 2021-08-01 op case READSTRERR:
920 48192874 2021-08-01 op case READSTRTRUNC:
921 014adfc2 2021-07-30 op np_error(hdr->tag, "name too long");
925 cf758c33 2021-12-14 op if (fid_by_id(fid) != NULL || afid != NOFID) {
926 cf758c33 2021-12-14 op np_error(hdr->tag, "invalid fid or afid");
930 deb971f5 2021-08-12 op if ((fd = open(aname, O_RDONLY|O_DIRECTORY)) == -1)
933 c7f4e1bd 2021-12-24 op if ((dir = new_dir(fd)) == NULL)
936 cf758c33 2021-12-14 op log_debug("attached %s to %d", aname, fid);
938 c7f4e1bd 2021-12-24 op if ((f = new_fid(dir, fid, aname, NULL)) == NULL) {
939 c7f4e1bd 2021-12-24 op dir_decref(dir);
943 c7f4e1bd 2021-12-24 op np_attach(hdr->tag, &f->qid);
947 deb971f5 2021-08-12 op np_errno(hdr->tag);
948 deb971f5 2021-08-12 op log_warn("failed to attach %s", aname);
952 014adfc2 2021-07-30 op client_send_listener(IMSG_CLOSE, NULL, 0);
953 014adfc2 2021-07-30 op client_shutdown();
957 c377c1b9 2021-07-30 op tclunk(struct np_msg_header *hdr, const uint8_t *data, size_t len)
959 c377c1b9 2021-07-30 op struct fid *f;
960 c377c1b9 2021-07-30 op uint32_t fid;
963 48192874 2021-08-01 op if (!NPREAD32("fid", &fid, &data, &len)) {
964 c377c1b9 2021-07-30 op client_send_listener(IMSG_CLOSE, NULL, 0);
965 c377c1b9 2021-07-30 op client_shutdown();
969 c377c1b9 2021-07-30 op if ((f = fid_by_id(fid)) == NULL) {
970 c377c1b9 2021-07-30 op np_error(hdr->tag, "invalid fid");
975 c377c1b9 2021-07-30 op np_clunk(hdr->tag);
979 36b30273 2021-07-30 op tflush(struct np_msg_header *hdr, const uint8_t *data, size_t len)
981 36b30273 2021-07-30 op uint16_t oldtag;
984 36b30273 2021-07-30 op * We're doing only synchronous I/O. Tflush is implemented
985 36b30273 2021-07-30 op * only because it's illegal to reply with a Rerror.
988 36b30273 2021-07-30 op /* oldtag[2] */
989 36b30273 2021-07-30 op if (len != sizeof(oldtag)) {
990 0592b956 2021-12-20 cage log_warnx("Tflush with the wrong size: got %zu want %zu",
991 36b30273 2021-07-30 op len, sizeof(oldtag));
992 36b30273 2021-07-30 op client_send_listener(IMSG_CLOSE, NULL, 0);
993 36b30273 2021-07-30 op client_shutdown();
997 36b30273 2021-07-30 op np_flush(hdr->tag);
1001 1c08fc54 2021-08-01 op twalk(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1003 deb971f5 2021-08-12 op struct stat sb;
1004 c7f4e1bd 2021-12-24 op struct dir *dir;
1005 c7f4e1bd 2021-12-24 op struct qid wqid[MAXWELEM] = {0};
1006 1c08fc54 2021-08-01 op struct fid *f, *nf;
1007 1c08fc54 2021-08-01 op uint32_t fid, newfid;
1008 7ddddcfe 2021-08-01 op uint16_t nwname;
1009 deb971f5 2021-08-12 op int fd, oldfd, no, nwqid = 0;
1010 20c188ce 2021-12-16 op char wnam[PATH_MAX];
1012 48192874 2021-08-01 op if (!NPREAD32("fid", &fid, &data, &len) ||
1013 48192874 2021-08-01 op !NPREAD32("newfid", &newfid, &data, &len) ||
1014 48192874 2021-08-01 op !NPREAD16("nwname", &nwname, &data, &len))
1017 1c08fc54 2021-08-01 op if (nwname > MAXWELEM) {
1018 1c08fc54 2021-08-01 op log_warnx("Twalk: more than %d path elements: %d",
1019 1c08fc54 2021-08-01 op MAXWELEM, nwname);
1023 1c08fc54 2021-08-01 op if ((f = fid_by_id(fid)) == NULL) {
1024 1c08fc54 2021-08-01 op np_error(hdr->tag, "invalid fid");
1028 99a43590 2021-12-14 op if (f->fd != -1) {
1029 47c9b8b8 2021-12-02 op np_error(hdr->tag, "fid already opened for I/O");
1033 1c08fc54 2021-08-01 op if (fid == newfid)
1035 1c08fc54 2021-08-01 op else if ((nf = fid_by_id(newfid)) != NULL) {
1036 1c08fc54 2021-08-01 op np_error(hdr->tag, "newfid already in use");
1041 913eba5c 2021-08-01 op /* special case: fid duplication */
1042 22dfb5a0 2021-08-01 op if (nwname == 0) {
1044 1c08fc54 2021-08-01 op * TODO: should we forbid fids duplication when fid ==
1047 a1aa276f 2021-12-30 op if (nf == NULL &&
1048 c7f4e1bd 2021-12-24 op (nf = new_fid(f->dir, newfid, f->fpath, &f->qid)) == NULL)
1049 f987557c 2021-12-02 op fatal("new_fid duplication");
1051 a6036d0f 2021-12-21 op np_walk(hdr->tag, 0, NULL);
1055 c7f4e1bd 2021-12-24 op if (!(f->qid.type & QTDIR)) {
1056 053e2652 2021-08-01 op np_error(hdr->tag, "fid doesn't represent a directory");
1060 c7f4e1bd 2021-12-24 op oldfd = f->dir->fd;
1062 1c08fc54 2021-08-01 op for (nwqid = 0; nwqid < nwname; nwqid++) {
1063 053e2652 2021-08-01 op switch (NPREADSTR("wname", wnam, sizeof(wnam), &data, &len)) {
1064 48192874 2021-08-01 op case READSTRERR:
1066 48192874 2021-08-01 op case READSTRTRUNC:
1067 1c08fc54 2021-08-01 op np_error(hdr->tag, "wname too long");
1071 2532a087 2021-12-20 cage if (*wnam == '\0' ||
1072 054cd6b9 2021-12-14 op strchr(wnam, '/') != NULL ||
1073 054cd6b9 2021-12-14 op !strcmp(wnam, ".")) {
1074 054cd6b9 2021-12-14 op errno = EINVAL;
1075 054cd6b9 2021-12-14 op goto cantopen;
1078 deb971f5 2021-08-12 op if ((fd = openat(oldfd, wnam, O_RDONLY|O_DIRECTORY)) == -1 &&
1079 3242c0bc 2021-12-14 op errno != ENOTDIR)
1080 deb971f5 2021-08-12 op goto cantopen;
1082 deb971f5 2021-08-12 op if ((fd == -1 && fstatat(oldfd, wnam, &sb, 0) == -1) ||
1083 3242c0bc 2021-12-14 op (fd != -1 && fstat(fd, &sb) == -1))
1084 deb971f5 2021-08-12 op goto cantopen;
1086 deb971f5 2021-08-12 op qid_update_from_sb(&wqid[nwqid], &sb);
1088 deb971f5 2021-08-12 op /* reached a file but we still have other components */
1089 deb971f5 2021-08-12 op if (fd == -1 && nwqid+1 < nwname)
1090 deb971f5 2021-08-12 op goto cantopen;
1092 deb971f5 2021-08-12 op /* reached the end and found a file */
1093 deb971f5 2021-08-12 op if (fd == -1 && nwqid+1 == nwname)
1096 c7f4e1bd 2021-12-24 op if (oldfd != f->dir->fd)
1097 deb971f5 2021-08-12 op close(oldfd);
1102 1bf47a19 2021-12-23 op * If fd is -1 we've reached a file, otherwise we've just
1103 1bf47a19 2021-12-23 op * reached another directory. We must pay attention to what
1104 c7f4e1bd 2021-12-24 op * file descriptor we use to create the dir, because if we've
1105 c7f4e1bd 2021-12-24 op * reached a file and oldfd is f->dir->fd then we *must* share
1106 c7f4e1bd 2021-12-24 op * the same dir (it was a walk of one path from a directory to a
1107 1bf47a19 2021-12-23 op * file, otherwise fun is bound to happen as soon as the client
1108 1bf47a19 2021-12-23 op * closes the fid for the directory but keeps the one for the
1111 c7f4e1bd 2021-12-24 op if (fd == -1 && oldfd == f->dir->fd)
1112 c7f4e1bd 2021-12-24 op dir = f->dir;
1113 1bf47a19 2021-12-23 op else if (fd == -1)
1114 c7f4e1bd 2021-12-24 op dir = new_dir(oldfd);
1116 c7f4e1bd 2021-12-24 op dir = new_dir(fd);
1118 c7f4e1bd 2021-12-24 op if (dir == NULL)
1119 c7f4e1bd 2021-12-24 op fatal("new_dir");
1121 1c08fc54 2021-08-01 op if (nf == NULL) {
1122 c7f4e1bd 2021-12-24 op if ((nf = new_fid(dir, newfid, wnam, &wqid[nwqid-1])) == NULL)
1123 1bf47a19 2021-12-23 op fatal("new fid");
1125 c7f4e1bd 2021-12-24 op /* update the dir */
1126 c7f4e1bd 2021-12-24 op dir_decref(nf->dir);
1127 c7f4e1bd 2021-12-24 op nf->dir = dir_incref(dir);
1130 1c08fc54 2021-08-01 op np_walk(hdr->tag, nwqid, wqid);
1134 c7f4e1bd 2021-12-24 op if (oldfd != f->dir->fd)
1135 deb971f5 2021-08-12 op close(oldfd);
1137 deb971f5 2021-08-12 op if (nwqid == 0)
1138 deb971f5 2021-08-12 op np_error(hdr->tag, strerror(no));
1140 deb971f5 2021-08-12 op np_walk(hdr->tag, nwqid, wqid);
1144 1c08fc54 2021-08-01 op client_send_listener(IMSG_CLOSE, NULL, 0);
1145 1c08fc54 2021-08-01 op client_shutdown();
1148 021481ca 2021-12-13 op static inline int
1149 021481ca 2021-12-13 op npmode_to_unix(uint8_t mode, int *flags)
1151 021481ca 2021-12-13 op switch (mode & 0x0F) {
1152 021481ca 2021-12-13 op case KOREAD:
1153 021481ca 2021-12-13 op *flags = O_RDONLY;
1155 021481ca 2021-12-13 op case KOWRITE:
1156 021481ca 2021-12-13 op *flags = O_WRONLY;
1158 021481ca 2021-12-13 op case KORDWR:
1159 021481ca 2021-12-13 op *flags = O_RDWR;
1161 021481ca 2021-12-13 op case KOEXEC:
1162 021481ca 2021-12-13 op log_warnx("tried to open something with KOEXEC");
1163 021481ca 2021-12-13 op /* fallthrough */
1168 021481ca 2021-12-13 op if (mode & KOTRUNC)
1169 021481ca 2021-12-13 op *flags |= O_TRUNC;
1170 021481ca 2021-12-13 op if (mode & KORCLOSE)
1171 021481ca 2021-12-13 op *flags |= O_CLOEXEC;
1177 021481ca 2021-12-13 op topen(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1179 021481ca 2021-12-13 op struct stat sb;
1180 021481ca 2021-12-13 op struct qid qid;
1181 021481ca 2021-12-13 op struct fid *f;
1182 021481ca 2021-12-13 op uint32_t fid;
1183 021481ca 2021-12-13 op uint8_t mode;
1184 87a818e8 2021-12-14 op const char *path;
1186 021481ca 2021-12-13 op /* fid[4] mode[1] */
1187 021481ca 2021-12-13 op if (!NPREAD32("fid", &fid, &data, &len) ||
1188 021481ca 2021-12-13 op !NPREAD8("mode", &mode, &data, &len)) {
1189 021481ca 2021-12-13 op client_send_listener(IMSG_CLOSE, NULL, 0);
1190 021481ca 2021-12-13 op client_shutdown();
1194 021481ca 2021-12-13 op if ((f = fid_by_id(fid)) == NULL || f->fd != -1) {
1195 021481ca 2021-12-13 op np_error(hdr->tag, "invalid fid");
1199 d4e43815 2021-12-15 op if (npmode_to_unix(mode, &f->iomode) == -1) {
1200 d4e43815 2021-12-15 op np_error(hdr->tag, "invalid mode");
1204 1bf47a19 2021-12-23 op path = f->fpath;
1205 c7f4e1bd 2021-12-24 op if (f->qid.type & QTDIR)
1208 c7f4e1bd 2021-12-24 op if ((f->fd = openat(f->dir->fd, path, f->iomode)) == -1) {
1209 021481ca 2021-12-13 op np_error(hdr->tag, strerror(errno));
1213 021481ca 2021-12-13 op if (fstat(f->fd, &sb) == -1)
1214 021481ca 2021-12-13 op fatal("fstat");
1216 baa40504 2021-12-15 op if (S_ISDIR(sb.st_mode)) {
1217 c7f4e1bd 2021-12-24 op if ((f->d = fdopendir(f->fd)) == NULL) {
1218 baa40504 2021-12-15 op np_errno(hdr->tag);
1219 baa40504 2021-12-15 op close(f->fd);
1224 c0ae57b9 2021-12-15 op if ((f->evb = evbuffer_new()) == NULL) {
1225 c0ae57b9 2021-12-15 op np_errno(hdr->tag);
1226 c7f4e1bd 2021-12-24 op closedir(f->d);
1227 c7f4e1bd 2021-12-24 op f->d = NULL;
1232 baa40504 2021-12-15 op f->offset = 0;
1234 021481ca 2021-12-13 op qid_update_from_sb(&qid, &sb);
1235 021481ca 2021-12-13 op np_open(hdr->tag, &qid, sb.st_blksize);
1239 3162e55b 2021-12-20 op tcreate(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1241 3162e55b 2021-12-20 op struct stat sb;
1242 3162e55b 2021-12-20 op struct qid qid;
1243 3162e55b 2021-12-20 op struct fid *f;
1244 3162e55b 2021-12-20 op uint32_t fid, perm;
1245 3162e55b 2021-12-20 op uint8_t mode;
1246 3162e55b 2021-12-20 op char name[PATH_MAX];
1248 3162e55b 2021-12-20 op /* fid[4] name[s] perm[4] mode[1] */
1249 3162e55b 2021-12-20 op if (!NPREAD32("fid", &fid, &data, &len))
1251 3162e55b 2021-12-20 op switch (NPREADSTR("name", name, sizeof(name), &data, &len)) {
1252 3162e55b 2021-12-20 op case READSTRERR:
1254 3162e55b 2021-12-20 op case READSTRTRUNC:
1255 3162e55b 2021-12-20 op np_error(hdr->tag, "name too long");
1258 3162e55b 2021-12-20 op if (!NPREAD32("perm", &perm, &data, &len) ||
1259 3162e55b 2021-12-20 op !NPREAD8("mode", &mode, &data, &len))
1262 5547a835 2021-12-20 op if (!strcmp(name, ".") || !strcmp(name, "..") ||
1263 5547a835 2021-12-20 op strchr(name, '/') != NULL) {
1264 5547a835 2021-12-20 op np_error(hdr->tag, "invalid name");
1268 3162e55b 2021-12-20 op if ((f = fid_by_id(fid)) == NULL || f->fd != -1) {
1269 3162e55b 2021-12-20 op np_error(hdr->tag, "invalid fid");
1273 c7f4e1bd 2021-12-24 op if (!(f->qid.type & QTDIR)) {
1274 3162e55b 2021-12-20 op np_error(hdr->tag, "fid doesn't identify a directory");
1278 3162e55b 2021-12-20 op if (npmode_to_unix(mode, &f->iomode) == -1) {
1279 3162e55b 2021-12-20 op np_error(hdr->tag, "invalid mode");
1283 3162e55b 2021-12-20 op if (f->iomode & O_RDONLY) {
1284 3162e55b 2021-12-20 op np_error(hdr->tag, "can't create a read-only file");
1288 3162e55b 2021-12-20 op /* TODO: parse the mode */
1290 3162e55b 2021-12-20 op if (perm & 0x80000000) {
1291 3162e55b 2021-12-20 op /* create a directory */
1292 c7f4e1bd 2021-12-24 op f->fd = mkdirat(f->dir->fd, name, 0755);
1294 3162e55b 2021-12-20 op /* create a file */
1295 c7f4e1bd 2021-12-24 op f->fd = openat(f->dir->fd, name, f->iomode | O_CREAT | O_TRUNC,
1299 3162e55b 2021-12-20 op if (f->fd == -1) {
1300 3162e55b 2021-12-20 op np_errno(hdr->tag);
1304 3162e55b 2021-12-20 op if (fstat(f->fd, &sb) == -1)
1305 3162e55b 2021-12-20 op fatal("fstat");
1307 3162e55b 2021-12-20 op if (S_ISDIR(sb.st_mode)) {
1308 c7f4e1bd 2021-12-24 op if ((f->d = fdopendir(f->fd)) == NULL) {
1309 3162e55b 2021-12-20 op np_errno(hdr->tag);
1310 3162e55b 2021-12-20 op close(f->fd);
1315 3162e55b 2021-12-20 op if ((f->evb = evbuffer_new()) == NULL) {
1316 3162e55b 2021-12-20 op np_errno(hdr->tag);
1317 c7f4e1bd 2021-12-24 op closedir(f->d);
1318 c7f4e1bd 2021-12-24 op f->d = NULL;
1323 3162e55b 2021-12-20 op f->offset = 0;
1325 3162e55b 2021-12-20 op qid_update_from_sb(&qid, &sb);
1326 3162e55b 2021-12-20 op np_create(hdr->tag, &qid, sb.st_blksize);
1331 3162e55b 2021-12-20 op client_send_listener(IMSG_CLOSE, NULL, 0);
1332 3162e55b 2021-12-20 op client_shutdown();
1335 5585f1c3 2021-12-16 op static inline void
1336 3a2c53f5 2021-12-18 op serialize_stat(const char *fname, struct stat *sb, struct evbuffer *evb)
1338 5585f1c3 2021-12-16 op struct qid qid;
1339 5585f1c3 2021-12-16 op const char *uid, *gid, *muid;
1341 5585f1c3 2021-12-16 op uint16_t namlen, uidlen, gidlen, ulen;
1343 3a2c53f5 2021-12-18 op qid_update_from_sb(&qid, sb);
1345 5585f1c3 2021-12-16 op /* TODO: fill these fields */
1350 3a2c53f5 2021-12-18 op namlen = strlen(fname);
1351 5585f1c3 2021-12-16 op uidlen = strlen(uid);
1352 5585f1c3 2021-12-16 op gidlen = strlen(gid);
1353 5585f1c3 2021-12-16 op ulen = strlen(muid);
1355 5585f1c3 2021-12-16 op tot = NPSTATSIZ(namlen, uidlen, gidlen, ulen);
1356 5585f1c3 2021-12-16 op if (tot > UINT32_MAX) {
1357 5585f1c3 2021-12-16 op log_warnx("stat info for dir entry %s would overflow",
1362 5585f1c3 2021-12-16 op np_write16(evb, tot); /* size[2] */
1363 3a2c53f5 2021-12-18 op np_write16(evb, sb->st_rdev); /* type[2] */
1364 3a2c53f5 2021-12-18 op np_write32(evb, sb->st_dev); /* dev[4] */
1365 5585f1c3 2021-12-16 op np_qid(evb, &qid); /* qid[13] */
1367 5585f1c3 2021-12-16 op /* XXX: translate? */
1368 3a2c53f5 2021-12-18 op np_write32(evb, sb->st_mode); /* mode[4] */
1370 3a2c53f5 2021-12-18 op np_write32(evb, sb->st_atim.tv_sec); /* atime[4] */
1371 3a2c53f5 2021-12-18 op np_write32(evb, sb->st_mtim.tv_sec); /* mtime[4] */
1372 3a2c53f5 2021-12-18 op np_write64(evb, sb->st_size); /* length[8] */
1373 3a2c53f5 2021-12-18 op np_string(evb, namlen, fname); /* name[s] */
1374 5585f1c3 2021-12-16 op np_string(evb, uidlen, uid); /* uid[s] */
1375 5585f1c3 2021-12-16 op np_string(evb, gidlen, gid); /* gid[s] */
1376 5585f1c3 2021-12-16 op np_string(evb, ulen, muid); /* muid[s] */
1380 baa40504 2021-12-15 op tread(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1382 baa40504 2021-12-15 op struct fid *f;
1384 d8dd8bd7 2021-01-02 op size_t howmuch;
1385 baa40504 2021-12-15 op uint64_t off;
1386 baa40504 2021-12-15 op uint32_t fid, count;
1387 baa40504 2021-12-15 op char buf[2048];
1389 baa40504 2021-12-15 op /* fid[4] offset[8] count[4] */
1390 baa40504 2021-12-15 op if (!NPREAD32("fid", &fid, &data, &len) ||
1391 baa40504 2021-12-15 op !NPREAD64("offset", &off, &data, &len) ||
1392 baa40504 2021-12-15 op !NPREAD32("count", &count, &data, &len)) {
1393 baa40504 2021-12-15 op client_send_listener(IMSG_CLOSE, NULL, 0);
1394 baa40504 2021-12-15 op client_shutdown();
1398 5f362b7f 2021-12-15 op if ((f = fid_by_id(fid)) == NULL || f->fd == -1) {
1399 baa40504 2021-12-15 op np_error(hdr->tag, "invalid fid");
1403 baa40504 2021-12-15 op if (TYPE_OVERFLOW(off_t, off)) {
1404 423f02f5 2021-12-28 cage log_warnx("unexpected off_t size");
1405 baa40504 2021-12-15 op np_error(hdr->tag, "invalid offset");
1409 c7f4e1bd 2021-12-24 op if (f->d == NULL) {
1410 baa40504 2021-12-15 op /* read a file */
1411 d8dd8bd7 2021-01-02 op howmuch = MIN(sizeof(buf), count);
1412 d8dd8bd7 2021-01-02 op r = pread(f->fd, buf, howmuch, (off_t)off);
1413 baa40504 2021-12-15 op if (r == -1)
1414 baa40504 2021-12-15 op np_errno(hdr->tag);
1416 baa40504 2021-12-15 op np_read(hdr->tag, r, buf);
1418 fcf8ca15 2021-12-16 op if (off == 0 && f->offset != 0) {
1419 c7f4e1bd 2021-12-24 op rewinddir(f->d);
1420 fcf8ca15 2021-12-16 op f->offset = 0;
1421 423f02f5 2021-12-28 cage evbuffer_drain(f->evb, EVBUFFER_LENGTH(f->evb));
1424 5585f1c3 2021-12-16 op if (off != f->offset) {
1425 5585f1c3 2021-12-16 op np_error(hdr->tag, "can't seek in directories");
1429 5585f1c3 2021-12-16 op while (EVBUFFER_LENGTH(f->evb) < count) {
1430 5585f1c3 2021-12-16 op struct dirent *d;
1431 3a2c53f5 2021-12-18 op struct stat sb;
1433 c7f4e1bd 2021-12-24 op if ((d = readdir(f->d)) == NULL)
1435 3a2c53f5 2021-12-18 op if (fstatat(f->fd, d->d_name, &sb, 0) == -1) {
1436 3a2c53f5 2021-12-18 op warn("fstatat");
1439 3a2c53f5 2021-12-18 op serialize_stat(d->d_name, &sb, f->evb);
1442 5585f1c3 2021-12-16 op count = MIN(count, EVBUFFER_LENGTH(f->evb));
1443 5585f1c3 2021-12-16 op np_read(hdr->tag, count, EVBUFFER_DATA(f->evb));
1444 5585f1c3 2021-12-16 op evbuffer_drain(f->evb, count);
1446 5585f1c3 2021-12-16 op f->offset += count;
1451 2483be55 2021-12-16 op twrite(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1453 2483be55 2021-12-16 op struct fid *f;
1455 2483be55 2021-12-16 op uint64_t off;
1456 2483be55 2021-12-16 op uint32_t fid, count;
1458 2483be55 2021-12-16 op /* fid[4] offset[8] count[4] data[count] */
1459 2483be55 2021-12-16 op if (!NPREAD32("fid", &fid, &data, &len) ||
1460 2483be55 2021-12-16 op !NPREAD64("off", &off, &data, &len) ||
1461 2483be55 2021-12-16 op !NPREAD32("count", &count, &data, &len) ||
1462 2483be55 2021-12-16 op len != count) {
1463 2483be55 2021-12-16 op client_send_listener(IMSG_CLOSE, NULL, 0);
1464 2483be55 2021-12-16 op client_shutdown();
1468 2483be55 2021-12-16 op if ((f = fid_by_id(fid)) == NULL || f->fd == -1) {
1469 2483be55 2021-12-16 op np_error(hdr->tag, "invalid fid");
1473 2483be55 2021-12-16 op if (!(f->iomode & O_WRONLY) &&
1474 2483be55 2021-12-16 op !(f->iomode & O_RDWR)) {
1475 2483be55 2021-12-16 op np_error(hdr->tag, "fid not opened for writing");
1479 2483be55 2021-12-16 op if (TYPE_OVERFLOW(off_t, off)) {
1480 2483be55 2021-12-16 op log_warnx("unexpected off_t size");
1481 2483be55 2021-12-16 op np_error(hdr->tag, "invalid offset");
1485 6ace3b52 2021-12-17 op if ((r = pwrite(f->fd, data, len, off)) == -1)
1486 2483be55 2021-12-16 op np_errno(hdr->tag);
1488 2483be55 2021-12-16 op np_write(hdr->tag, r);
1492 3a2c53f5 2021-12-18 op tstat(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1494 3a2c53f5 2021-12-18 op struct evbuffer *evb;
1495 3a2c53f5 2021-12-18 op struct stat sb;
1496 3a2c53f5 2021-12-18 op struct fid *f;
1498 3a2c53f5 2021-12-18 op uint32_t fid;
1500 3a2c53f5 2021-12-18 op /* fid[4] */
1501 3a2c53f5 2021-12-18 op if (!NPREAD32("fid", &fid, &data, &len)) {
1502 3a2c53f5 2021-12-18 op client_send_listener(IMSG_CLOSE, NULL, 0);
1503 3a2c53f5 2021-12-18 op client_shutdown();
1508 3a2c53f5 2021-12-18 op * plan9' stat(9P) is not clear on whether the stat is allowed
1509 5129baa3 2021-12-30 op * on opened fids or not. We're allowing stat regardless of the
1510 5129baa3 2021-12-30 op * status of the fid.
1513 3a2c53f5 2021-12-18 op if ((f = fid_by_id(fid)) == NULL) {
1514 3a2c53f5 2021-12-18 op np_error(hdr->tag, "invalid fid");
1518 3a2c53f5 2021-12-18 op if ((evb = evbuffer_new()) == NULL)
1519 3a2c53f5 2021-12-18 op fatal("evbuffer_new");
1521 1bf47a19 2021-12-23 op if (f->fd != -1)
1522 1bf47a19 2021-12-23 op r = fstat(f->fd, &sb);
1523 e3962050 2021-12-30 op else if (f->qid.type & QTDIR)
1524 e3962050 2021-12-30 op r = fstat(f->dir->fd, &sb);
1526 c7f4e1bd 2021-12-24 op r = fstatat(f->dir->fd, f->fpath, &sb, 0);
1528 3a2c53f5 2021-12-18 op if (r == -1) {
1529 3a2c53f5 2021-12-18 op np_errno(hdr->tag);
1530 3a2c53f5 2021-12-18 op evbuffer_free(evb);
1534 1bf47a19 2021-12-23 op serialize_stat(f->fpath, &sb, evb);
1535 3a2c53f5 2021-12-18 op np_stat(hdr->tag, EVBUFFER_LENGTH(evb), EVBUFFER_DATA(evb));
1536 3a2c53f5 2021-12-18 op evbuffer_free(evb);
1539 0592b956 2021-12-20 cage static void
1540 0592b956 2021-12-20 cage tremove(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1542 0592b956 2021-12-20 cage struct fid *f;
1543 0592b956 2021-12-20 cage uint32_t fid;
1545 946136f3 2021-12-26 op char dirpath[PATH_MAX + 3];
1547 0592b956 2021-12-20 cage /* fid[4] */
1548 0592b956 2021-12-20 cage if (!NPREAD32("fid", &fid, &data, &len)) {
1549 0592b956 2021-12-20 cage client_send_listener(IMSG_CLOSE, NULL, 0);
1550 0592b956 2021-12-20 cage client_shutdown();
1554 0592b956 2021-12-20 cage if ((f = fid_by_id(fid)) == NULL) {
1555 0592b956 2021-12-20 cage np_error(hdr->tag, "invalid fid");
1559 946136f3 2021-12-26 op if (f->qid.type & QTDIR) { /* directory */
1560 946136f3 2021-12-26 op strlcpy(dirpath, "../", sizeof(dirpath));
1561 946136f3 2021-12-26 op strlcat(dirpath, f->fpath, sizeof(dirpath));
1562 946136f3 2021-12-26 op r = unlinkat(f->dir->fd, dirpath, AT_REMOVEDIR);
1563 946136f3 2021-12-26 op } else /* file */
1564 c7f4e1bd 2021-12-24 op r = unlinkat(f->dir->fd, f->fpath, 0);
1566 0592b956 2021-12-20 cage if (r == -1)
1567 0592b956 2021-12-20 cage np_errno(hdr->tag);
1569 0592b956 2021-12-20 cage np_remove(hdr->tag);
1571 0592b956 2021-12-20 cage free_fid(f);
1575 e60f4e08 2021-07-30 op handle_message(struct imsg *imsg, size_t len)
1577 e60f4e08 2021-07-30 op struct msg {
1578 e60f4e08 2021-07-30 op uint8_t type;
1579 e60f4e08 2021-07-30 op void (*fn)(struct np_msg_header *, const uint8_t *, size_t);
1580 e60f4e08 2021-07-30 op } msgs[] = {
1581 e60f4e08 2021-07-30 op {Tversion, tversion},
1582 e60f4e08 2021-07-30 op {Tattach, tattach},
1583 c377c1b9 2021-07-30 op {Tclunk, tclunk},
1584 36b30273 2021-07-30 op {Tflush, tflush},
1585 1c08fc54 2021-08-01 op {Twalk, twalk},
1586 021481ca 2021-12-13 op {Topen, topen},
1587 3162e55b 2021-12-20 op {Tcreate, tcreate},
1588 baa40504 2021-12-15 op {Tread, tread},
1589 2483be55 2021-12-16 op {Twrite, twrite},
1590 3a2c53f5 2021-12-18 op {Tstat, tstat},
1591 0592b956 2021-12-20 cage {Tremove, tremove},
1593 e60f4e08 2021-07-30 op struct np_msg_header hdr;
1595 e60f4e08 2021-07-30 op uint8_t *data;
1597 64c19d90 2021-07-30 op #if DEBUG_PACKETS
1598 78b94752 2021-08-01 op hexdump("incoming packet", imsg->data, len);
1601 e60f4e08 2021-07-30 op parse_message(imsg->data, len, &hdr, &data);
1602 e60f4e08 2021-07-30 op len -= HEADERSIZE;
1604 e60f4e08 2021-07-30 op log_debug("got request: len=%d type=%d[%s] tag=%d",
1605 e60f4e08 2021-07-30 op hdr.len, hdr.type, pp_msg_type(hdr.type), hdr.tag);
1607 e60f4e08 2021-07-30 op if (!handshaked && hdr.type != Tversion) {
1608 e60f4e08 2021-07-30 op client_send_listener(IMSG_CLOSE, NULL, 0);
1609 e60f4e08 2021-07-30 op client_shutdown();
1613 e60f4e08 2021-07-30 op for (i = 0; i < sizeof(msgs)/sizeof(msgs[0]); ++i) {
1614 e60f4e08 2021-07-30 op if (msgs[i].type != hdr.type)
1617 e60f4e08 2021-07-30 op msgs[i].fn(&hdr, data, len);
1621 e60f4e08 2021-07-30 op np_error(hdr.tag, "Not supported.");