2 1e84c7b7 2022-01-10 op * Copyright (c) 2021, 2022 Omar Polo <op@omarpolo.com>
4 fb1a36c0 2022-01-09 op * Permission to use, copy, modify, and distribute this software for any
5 fb1a36c0 2022-01-09 op * purpose with or without fee is hereby granted, provided that the above
6 fb1a36c0 2022-01-09 op * copyright notice and this permission notice appear in all copies.
8 fb1a36c0 2022-01-09 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 fb1a36c0 2022-01-09 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 fb1a36c0 2022-01-09 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 fb1a36c0 2022-01-09 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 fb1a36c0 2022-01-09 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 fb1a36c0 2022-01-09 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 fb1a36c0 2022-01-09 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 bbcba3ed 2022-01-10 op #include "compat.h"
19 fb1a36c0 2022-01-09 op #include <sys/stat.h>
20 fb1a36c0 2022-01-09 op #include <sys/types.h>
21 fb1a36c0 2022-01-09 op #include <sys/uio.h>
23 fb1a36c0 2022-01-09 op #include <dirent.h>
24 fb1a36c0 2022-01-09 op #include <endian.h>
25 fb1a36c0 2022-01-09 op #include <errno.h>
26 fb1a36c0 2022-01-09 op #include <fcntl.h>
27 755d75bf 2022-01-17 op #include <inttypes.h>
28 755d75bf 2022-01-17 op #include <libgen.h>
29 fb1a36c0 2022-01-09 op #include <pwd.h>
30 fb1a36c0 2022-01-09 op #include <signal.h>
31 fb1a36c0 2022-01-09 op #include <stdint.h>
32 755d75bf 2022-01-17 op #include <stdio.h>
33 fb1a36c0 2022-01-09 op #include <stdlib.h>
34 fb1a36c0 2022-01-09 op #include <string.h>
35 fb1a36c0 2022-01-09 op #include <syslog.h>
36 fb1a36c0 2022-01-09 op #include <unistd.h>
38 fb1a36c0 2022-01-09 op #include "client.h"
39 fb1a36c0 2022-01-09 op #include "kami.h"
40 fb1a36c0 2022-01-09 op #include "kamid.h"
41 fb1a36c0 2022-01-09 op #include "log.h"
42 fb1a36c0 2022-01-09 op #include "sandbox.h"
43 fb1a36c0 2022-01-09 op #include "utils.h"
46 fb1a36c0 2022-01-09 op * XXX: atm is difficult to accept messages bigger than MAX_IMSGSIZE
47 fb1a36c0 2022-01-09 op * minus IMSG_HEADER_SIZE, we need something to split messages into
48 fb1a36c0 2022-01-09 op * chunks and receive them one by the other.
50 fb1a36c0 2022-01-09 op * CLIENT_MSIZE is thus the maximum message size we can handle now.
52 fb1a36c0 2022-01-09 op #define CLIENT_MSIZE (MAX_IMSGSIZE - IMSG_HEADER_SIZE)
54 fb1a36c0 2022-01-09 op #define DEBUG_PACKETS 0
56 fb1a36c0 2022-01-09 op /* straight outta /src/usr.bin/ssh/scp.c */
57 fb1a36c0 2022-01-09 op #define TYPE_OVERFLOW(type, val) \
58 fb1a36c0 2022-01-09 op ((sizeof(type) == 4 && (val) > INT32_MAX) || \
59 fb1a36c0 2022-01-09 op (sizeof(type) == 8 && (val) > INT64_MAX) || \
60 fb1a36c0 2022-01-09 op (sizeof(type) != 4 && sizeof(type) != 8))
62 fb1a36c0 2022-01-09 op STAILQ_HEAD(dirhead, dir) dirs;
66 fb1a36c0 2022-01-09 op STAILQ_ENTRY(dir) entries;
69 fb1a36c0 2022-01-09 op STAILQ_HEAD(fidhead, fid) fids;
73 46e4e74b 2022-01-17 op char fname[NAME_MAX];
76 fb1a36c0 2022-01-09 op * the flags passed to open(2). O_CLOEXEC means ORCLOSE, that
77 fb1a36c0 2022-01-09 op * is to unlink the file upon Tclunk.
82 fb1a36c0 2022-01-09 op * if fd is not -1 this fid was opened, fd represents its
83 fb1a36c0 2022-01-09 op * file descriptor and iomode the flags passed to open(2).
87 fb1a36c0 2022-01-09 op struct evbuffer *evb;
90 fb1a36c0 2022-01-09 op * expected offset for Tread against a directory.
92 fb1a36c0 2022-01-09 op uint64_t offset;
94 fb1a36c0 2022-01-09 op struct qid qid;
95 fb1a36c0 2022-01-09 op struct dir *dir;
96 fb1a36c0 2022-01-09 op STAILQ_ENTRY(fid) entries;
99 fb1a36c0 2022-01-09 op static struct imsgev *iev_listener;
100 fb1a36c0 2022-01-09 op static struct evbuffer *evb;
101 fb1a36c0 2022-01-09 op static uint32_t peerid;
103 fb1a36c0 2022-01-09 op static int handshaked;
104 fb1a36c0 2022-01-09 op uint32_t msize;
106 fb1a36c0 2022-01-09 op static __dead void client_shutdown(void);
107 fb1a36c0 2022-01-09 op static void client_sig_handler(int, short, void *);
108 fb1a36c0 2022-01-09 op static void client_dispatch_listener(int, short, void *);
109 fb1a36c0 2022-01-09 op static void client_privdrop(const char *, const char *);
111 fb1a36c0 2022-01-09 op static int client_send_listener(int, const void *, uint16_t);
113 fb1a36c0 2022-01-09 op static void qid_update_from_sb(struct qid *, struct stat *);
115 fb1a36c0 2022-01-09 op static struct dir *new_dir(int);
116 fb1a36c0 2022-01-09 op static struct dir *dir_incref(struct dir *);
117 fb1a36c0 2022-01-09 op static void dir_decref(struct dir *);
119 fb1a36c0 2022-01-09 op static struct fid *new_fid(struct dir *, uint32_t, const char *, struct qid *);
120 fb1a36c0 2022-01-09 op static struct fid *fid_by_id(uint32_t);
121 fb1a36c0 2022-01-09 op static void free_fid(struct fid *);
123 fb1a36c0 2022-01-09 op static void parse_message(const uint8_t *, size_t,
124 fb1a36c0 2022-01-09 op struct np_msg_header *, uint8_t **);
126 fb1a36c0 2022-01-09 op static void np_write16(struct evbuffer *, uint16_t);
127 fb1a36c0 2022-01-09 op static void np_write32(struct evbuffer *, uint32_t);
128 fb1a36c0 2022-01-09 op static void np_write64(struct evbuffer *, uint64_t);
129 fb1a36c0 2022-01-09 op static void np_header(uint32_t, uint8_t, uint16_t);
130 fb1a36c0 2022-01-09 op static void np_string(struct evbuffer *, uint16_t, const char *);
131 fb1a36c0 2022-01-09 op static void np_qid(struct evbuffer *, struct qid *);
132 fb1a36c0 2022-01-09 op static void do_send(void);
134 fb1a36c0 2022-01-09 op static void np_version(uint16_t, uint32_t, const char *);
135 fb1a36c0 2022-01-09 op static void np_attach(uint16_t, struct qid *);
136 fb1a36c0 2022-01-09 op static void np_clunk(uint16_t);
137 fb1a36c0 2022-01-09 op static void np_flush(uint16_t);
138 fb1a36c0 2022-01-09 op static void np_walk(uint16_t, int, struct qid *);
139 fb1a36c0 2022-01-09 op static void np_open(uint16_t, struct qid *, uint32_t);
140 fb1a36c0 2022-01-09 op static void np_create(uint16_t, struct qid *, uint32_t);
141 fb1a36c0 2022-01-09 op static void np_read(uint16_t, uint32_t, void *);
142 fb1a36c0 2022-01-09 op static void np_write(uint16_t, uint32_t);
143 fb1a36c0 2022-01-09 op static void np_stat(uint16_t, uint32_t, void *);
144 755d75bf 2022-01-17 op static void np_wstat(uint16_t);
145 fb1a36c0 2022-01-09 op static void np_remove(uint16_t);
146 fb1a36c0 2022-01-09 op static void np_error(uint16_t, const char *);
147 fb1a36c0 2022-01-09 op static void np_errno(uint16_t);
149 fb1a36c0 2022-01-09 op static int np_read8(const char *, const char *, uint8_t *,
150 fb1a36c0 2022-01-09 op const uint8_t **, size_t *);
151 fb1a36c0 2022-01-09 op static int np_read16(const char *, const char *, uint16_t *,
152 fb1a36c0 2022-01-09 op const uint8_t **, size_t *);
153 fb1a36c0 2022-01-09 op static int np_read32(const char *, const char *, uint32_t *,
154 fb1a36c0 2022-01-09 op const uint8_t **, size_t *);
155 fb1a36c0 2022-01-09 op static int np_read64(const char *, const char *, uint64_t *,
156 fb1a36c0 2022-01-09 op const uint8_t **, size_t *);
158 fb1a36c0 2022-01-09 op #define READSTRERR -1
159 fb1a36c0 2022-01-09 op #define READSTRTRUNC -2
160 fb1a36c0 2022-01-09 op static int np_readstr(const char *, const char *, char *, size_t,
161 fb1a36c0 2022-01-09 op const uint8_t **, size_t *);
163 755d75bf 2022-01-17 op static int np_readst(const char *, const char *, struct np_stat *,
164 755d75bf 2022-01-17 op char *, size_t, const uint8_t **, size_t *);
166 fb1a36c0 2022-01-09 op #define NPREAD8(f, dst, src, len) np_read8(__func__, f, dst, src, len)
167 fb1a36c0 2022-01-09 op #define NPREAD16(f, dst, src, len) np_read16(__func__, f, dst, src, len)
168 fb1a36c0 2022-01-09 op #define NPREAD32(f, dst, src, len) np_read32(__func__, f, dst, src, len)
169 fb1a36c0 2022-01-09 op #define NPREAD64(f, dst, src, len) np_read64(__func__, f, dst, src, len)
171 fb1a36c0 2022-01-09 op #define NPREADSTR(f, b, bl, src, len) np_readstr(__func__, f, b, bl, src, len)
173 755d75bf 2022-01-17 op #define NPREADST(f, dst, n, nl, src, len) np_readst(__func__, f, dst, n, nl, src, len)
175 fb1a36c0 2022-01-09 op static void tversion(struct np_msg_header *, const uint8_t *, size_t);
176 fb1a36c0 2022-01-09 op static void tattach(struct np_msg_header *, const uint8_t *, size_t);
177 fb1a36c0 2022-01-09 op static void tclunk(struct np_msg_header *, const uint8_t *, size_t);
178 fb1a36c0 2022-01-09 op static void tflush(struct np_msg_header *, const uint8_t *, size_t);
179 fb1a36c0 2022-01-09 op static void twalk(struct np_msg_header *, const uint8_t *, size_t);
180 fb1a36c0 2022-01-09 op static void topen(struct np_msg_header *, const uint8_t *, size_t);
181 fb1a36c0 2022-01-09 op static void tcreate(struct np_msg_header *, const uint8_t *, size_t);
182 fb1a36c0 2022-01-09 op static void tread(struct np_msg_header *, const uint8_t *, size_t);
183 fb1a36c0 2022-01-09 op static void twrite(struct np_msg_header *, const uint8_t *, size_t);
184 fb1a36c0 2022-01-09 op static void tstat(struct np_msg_header *, const uint8_t *, size_t);
185 755d75bf 2022-01-17 op static void twstat(struct np_msg_header *, const uint8_t *, size_t);
186 fb1a36c0 2022-01-09 op static void tremove(struct np_msg_header *, const uint8_t *, size_t);
187 fb1a36c0 2022-01-09 op static void handle_message(struct imsg *, size_t);
190 fb1a36c0 2022-01-09 op client(int debug, int verbose)
192 fb1a36c0 2022-01-09 op struct event ev_sigint, ev_sigterm;
194 fb1a36c0 2022-01-09 op log_init(debug, LOG_DAEMON);
195 fb1a36c0 2022-01-09 op log_setverbose(verbose);
197 fb1a36c0 2022-01-09 op setproctitle("client");
198 fb1a36c0 2022-01-09 op log_procinit("client");
200 fb1a36c0 2022-01-09 op log_debug("warming up");
202 fb1a36c0 2022-01-09 op event_init();
204 fb1a36c0 2022-01-09 op /* Setup signal handlers */
205 fb1a36c0 2022-01-09 op signal_set(&ev_sigint, SIGINT, client_sig_handler, NULL);
206 fb1a36c0 2022-01-09 op signal_set(&ev_sigterm, SIGTERM, client_sig_handler, NULL);
208 fb1a36c0 2022-01-09 op signal_add(&ev_sigint, NULL);
209 fb1a36c0 2022-01-09 op signal_add(&ev_sigterm, NULL);
211 fb1a36c0 2022-01-09 op signal(SIGPIPE, SIG_IGN);
212 fb1a36c0 2022-01-09 op signal(SIGHUP, SIG_IGN);
214 fb1a36c0 2022-01-09 op /* Setup pipe and event handler to the listener process */
215 fb1a36c0 2022-01-09 op if ((iev_listener = malloc(sizeof(*iev_listener))) == NULL)
218 fb1a36c0 2022-01-09 op imsg_init(&iev_listener->ibuf, 3);
219 fb1a36c0 2022-01-09 op iev_listener->handler = client_dispatch_listener;
221 fb1a36c0 2022-01-09 op /* Setup event handlers. */
222 fb1a36c0 2022-01-09 op iev_listener->events = EV_READ;
223 fb1a36c0 2022-01-09 op event_set(&iev_listener->ev, iev_listener->ibuf.fd,
224 fb1a36c0 2022-01-09 op iev_listener->events, iev_listener->handler, iev_listener);
225 fb1a36c0 2022-01-09 op event_add(&iev_listener->ev, NULL);
227 fb1a36c0 2022-01-09 op event_dispatch();
228 fb1a36c0 2022-01-09 op client_shutdown();
231 fb1a36c0 2022-01-09 op static __dead void
232 fb1a36c0 2022-01-09 op client_shutdown(void)
234 fb1a36c0 2022-01-09 op if (evb != NULL)
235 fb1a36c0 2022-01-09 op evbuffer_free(evb);
237 fb1a36c0 2022-01-09 op msgbuf_clear(&iev_listener->ibuf.w);
238 fb1a36c0 2022-01-09 op close(iev_listener->ibuf.fd);
240 fb1a36c0 2022-01-09 op free(iev_listener);
242 fb1a36c0 2022-01-09 op log_debug("client exiting");
247 fb1a36c0 2022-01-09 op client_sig_handler(int sig, short event, void *d)
250 fb1a36c0 2022-01-09 op * Normal signal handler rules don't apply because libevent
251 fb1a36c0 2022-01-09 op * decouples for us.
254 fb1a36c0 2022-01-09 op switch (sig) {
256 fb1a36c0 2022-01-09 op case SIGTERM:
257 fb1a36c0 2022-01-09 op client_shutdown();
259 fb1a36c0 2022-01-09 op fatalx("unexpected signal %d", sig);
264 fb1a36c0 2022-01-09 op client_dispatch_listener(int fd, short event, void *d)
266 1e84c7b7 2022-01-10 op static int auth = 0;
267 1e84c7b7 2022-01-10 op struct kd_auth_proc rauth;
268 fb1a36c0 2022-01-09 op struct imsg imsg;
269 fb1a36c0 2022-01-09 op struct imsgev *iev = d;
270 fb1a36c0 2022-01-09 op struct imsgbuf *ibuf;
272 fb1a36c0 2022-01-09 op int shut = 0;
274 fb1a36c0 2022-01-09 op ibuf = &iev->ibuf;
276 fb1a36c0 2022-01-09 op if (event & EV_READ) {
277 fb1a36c0 2022-01-09 op if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
278 fb1a36c0 2022-01-09 op fatal("imsg_read error");
279 fb1a36c0 2022-01-09 op if (n == 0) /* Connection closed */
282 fb1a36c0 2022-01-09 op if (event & EV_WRITE) {
283 fb1a36c0 2022-01-09 op if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
284 fb1a36c0 2022-01-09 op fatal("msgbuf_write");
285 fb1a36c0 2022-01-09 op if (n == 0) /* Connection closed */
290 fb1a36c0 2022-01-09 op if ((n = imsg_get(ibuf, &imsg)) == -1)
291 fb1a36c0 2022-01-09 op fatal("%s: imsg_get error", __func__);
292 fb1a36c0 2022-01-09 op if (n == 0) /* No more messages. */
295 fb1a36c0 2022-01-09 op switch (imsg.hdr.type) {
296 982f38dc 2022-01-23 op case IMSG_CTL_LOG_VERBOSE:
297 982f38dc 2022-01-23 op if (IMSG_DATA_SIZE(imsg) != sizeof(verbose))
298 982f38dc 2022-01-23 op fatalx("%s: IMSG_CTL_LOG_VERBOSE wrong size",
300 982f38dc 2022-01-23 op memcpy(&verbose, imsg.data, sizeof(verbose));
301 982f38dc 2022-01-23 op log_setverbose(verbose);
303 fb1a36c0 2022-01-09 op case IMSG_AUTH:
304 fb1a36c0 2022-01-09 op peerid = imsg.hdr.peerid;
306 fb1a36c0 2022-01-09 op fatalx("%s: IMSG_AUTH already done", __func__);
309 1e84c7b7 2022-01-10 op if (IMSG_DATA_SIZE(imsg) != sizeof(rauth))
310 1e84c7b7 2022-01-10 op fatalx("mismatching size for IMSG_AUTH");
311 1e84c7b7 2022-01-10 op memcpy(&rauth, imsg.data, sizeof(rauth));
312 1e84c7b7 2022-01-10 op if (rauth.uname[sizeof(rauth.uname)-1] != '\0' ||
313 1e84c7b7 2022-01-10 op rauth.dir[sizeof(rauth.dir)-1] != '\0')
314 1e84c7b7 2022-01-10 op fatalx("IMSG_AUTH strings not NUL-terminated");
316 1e84c7b7 2022-01-10 op client_privdrop(rauth.uname, rauth.dir);
317 1e84c7b7 2022-01-10 op explicit_bzero(&rauth, sizeof(rauth));
319 fb1a36c0 2022-01-09 op case IMSG_BUF:
321 fb1a36c0 2022-01-09 op fatalx("%s: can't handle messages before"
322 fb1a36c0 2022-01-09 op " doing the auth", __func__);
323 fb1a36c0 2022-01-09 op handle_message(&imsg, IMSG_DATA_SIZE(imsg));
325 fb1a36c0 2022-01-09 op case IMSG_CONN_GONE:
326 fb1a36c0 2022-01-09 op log_debug("closing");
330 fb1a36c0 2022-01-09 op log_debug("%s: unexpected imsg %d",
331 fb1a36c0 2022-01-09 op __func__, imsg.hdr.type);
334 fb1a36c0 2022-01-09 op imsg_free(&imsg);
338 fb1a36c0 2022-01-09 op imsg_event_add(iev);
340 fb1a36c0 2022-01-09 op /* This pipe is dead. Remove its event handler. */
341 fb1a36c0 2022-01-09 op event_del(&iev->ev);
342 fb1a36c0 2022-01-09 op log_debug("pipe closed, shutting down...");
343 fb1a36c0 2022-01-09 op event_loopexit(NULL);
348 fb1a36c0 2022-01-09 op client_privdrop(const char *username, const char *dir)
350 fb1a36c0 2022-01-09 op struct passwd *pw;
352 fb1a36c0 2022-01-09 op setproctitle("client %s", username);
354 fb1a36c0 2022-01-09 op if ((pw = getpwnam(username)) == NULL)
355 fb1a36c0 2022-01-09 op fatalx("getpwnam(%s) failed", username);
357 fb1a36c0 2022-01-09 op if (chroot(dir) == -1)
358 fb1a36c0 2022-01-09 op fatal("chroot");
359 fb1a36c0 2022-01-09 op if (chdir("/") == -1)
360 fb1a36c0 2022-01-09 op fatal("chdir(\"/\")");
362 fb1a36c0 2022-01-09 op if (setgroups(1, &pw->pw_gid) ||
363 fb1a36c0 2022-01-09 op setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
364 fb1a36c0 2022-01-09 op setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
365 fb1a36c0 2022-01-09 op fatal("can't drop privileges");
367 fb1a36c0 2022-01-09 op sandbox_client();
368 fb1a36c0 2022-01-09 op log_debug("client ready; user=%s dir=%s", username, dir);
370 fb1a36c0 2022-01-09 op if ((evb = evbuffer_new()) == NULL)
371 fb1a36c0 2022-01-09 op fatal("evbuffer_new");
375 fb1a36c0 2022-01-09 op client_send_listener(int type, const void *data, uint16_t len)
379 fb1a36c0 2022-01-09 op if ((ret = imsg_compose(&iev_listener->ibuf, type, peerid, 0, -1,
380 fb1a36c0 2022-01-09 op data, len)) != -1)
381 fb1a36c0 2022-01-09 op imsg_event_add(iev_listener);
386 fb1a36c0 2022-01-09 op /* set qid fields from sb */
388 fb1a36c0 2022-01-09 op qid_update_from_sb(struct qid *qid, struct stat *sb)
390 fb1a36c0 2022-01-09 op qid->path = sb->st_ino;
393 fb1a36c0 2022-01-09 op * Theoretically (and hopefully!) this should be a 64 bit
394 fb1a36c0 2022-01-09 op * number. Unfortunately, 9P uses 32 bit timestamps.
396 fb1a36c0 2022-01-09 op qid->vers = sb->st_mtim.tv_sec;
398 fb1a36c0 2022-01-09 op if (S_ISREG(sb->st_mode))
399 fb1a36c0 2022-01-09 op qid->type = QTFILE;
400 fb1a36c0 2022-01-09 op else if (S_ISDIR(sb->st_mode))
401 fb1a36c0 2022-01-09 op qid->type = QTDIR;
402 fb1a36c0 2022-01-09 op else if (S_ISLNK(sb->st_mode))
403 fb1a36c0 2022-01-09 op qid->type = QTSYMLINK;
406 fb1a36c0 2022-01-09 op /* creates a qid given a fd */
407 fb1a36c0 2022-01-09 op static struct dir *
408 fb1a36c0 2022-01-09 op new_dir(int fd)
410 fb1a36c0 2022-01-09 op struct dir *dir;
412 fb1a36c0 2022-01-09 op if ((dir = calloc(1, sizeof(*dir))) == NULL)
415 fb1a36c0 2022-01-09 op dir->fd = fd;
416 fb1a36c0 2022-01-09 op STAILQ_INSERT_HEAD(&dirs, dir, entries);
420 fb1a36c0 2022-01-09 op static struct dir *
421 fb1a36c0 2022-01-09 op dir_incref(struct dir *dir)
423 fb1a36c0 2022-01-09 op dir->refcount++;
428 fb1a36c0 2022-01-09 op dir_decref(struct dir *dir)
430 fb1a36c0 2022-01-09 op if (--dir->refcount > 0)
433 fb1a36c0 2022-01-09 op STAILQ_REMOVE(&dirs, dir, dir, entries);
435 fb1a36c0 2022-01-09 op close(dir->fd);
439 fb1a36c0 2022-01-09 op static struct fid *
440 fb1a36c0 2022-01-09 op new_fid(struct dir *dir, uint32_t fid, const char *path, struct qid *qid)
442 fb1a36c0 2022-01-09 op struct fid *f;
443 fb1a36c0 2022-01-09 op struct qid q;
444 fb1a36c0 2022-01-09 op struct stat sb;
446 fb1a36c0 2022-01-09 op if (qid == NULL) {
447 fb1a36c0 2022-01-09 op if (fstatat(dir->fd, path, &sb, 0)) {
448 fb1a36c0 2022-01-09 op log_warn("fstatat(%s)", path);
451 fb1a36c0 2022-01-09 op qid_update_from_sb(&q, &sb);
455 fb1a36c0 2022-01-09 op if ((f = calloc(1, sizeof(*f))) == NULL)
458 fb1a36c0 2022-01-09 op f->dir = dir_incref(dir);
459 fb1a36c0 2022-01-09 op f->fid = fid;
462 543fc013 2022-01-17 op strlcpy(f->fname, path, sizeof(f->fname));
464 fb1a36c0 2022-01-09 op memcpy(&f->qid, qid, sizeof(f->qid));
466 fb1a36c0 2022-01-09 op STAILQ_INSERT_HEAD(&fids, f, entries);
471 fb1a36c0 2022-01-09 op static struct fid *
472 fb1a36c0 2022-01-09 op fid_by_id(uint32_t fid)
474 fb1a36c0 2022-01-09 op struct fid *f;
476 fb1a36c0 2022-01-09 op STAILQ_FOREACH(f, &fids, entries) {
477 fb1a36c0 2022-01-09 op if (f->fid == fid)
485 fb1a36c0 2022-01-09 op free_fid(struct fid *f)
489 fb1a36c0 2022-01-09 op if (f->fd != -1) {
490 fb1a36c0 2022-01-09 op if (f->d != NULL)
491 fb1a36c0 2022-01-09 op r = closedir(f->d);
493 fb1a36c0 2022-01-09 op r = close(f->fd);
496 fb1a36c0 2022-01-09 op fatal("can't close fid %d", f->fid);
498 fb1a36c0 2022-01-09 op if (f->evb != NULL)
499 fb1a36c0 2022-01-09 op evbuffer_free(f->evb);
501 fb1a36c0 2022-01-09 op /* try to honour ORCLOSE if requested */
502 fb1a36c0 2022-01-09 op if (f->iomode & O_CLOEXEC)
503 543fc013 2022-01-17 op unlinkat(f->dir->fd, f->fname, 0);
506 fb1a36c0 2022-01-09 op dir_decref(f->dir);
508 fb1a36c0 2022-01-09 op STAILQ_REMOVE(&fids, f, fid, entries);
513 fb1a36c0 2022-01-09 op parse_message(const uint8_t *data, size_t len, struct np_msg_header *hdr,
514 fb1a36c0 2022-01-09 op uint8_t **cnt)
516 fb1a36c0 2022-01-09 op size_t olen = len;
518 fb1a36c0 2022-01-09 op if (!NPREAD32("len", &hdr->len, &data, &len) ||
519 fb1a36c0 2022-01-09 op !NPREAD8("type", &hdr->type, &data, &len) ||
520 fb1a36c0 2022-01-09 op !NPREAD16("tag", &hdr->tag, &data, &len))
523 fb1a36c0 2022-01-09 op if (olen != hdr->len)
526 fb1a36c0 2022-01-09 op if (hdr->type < Tversion ||
527 fb1a36c0 2022-01-09 op hdr->type >= Tmax ||
528 fb1a36c0 2022-01-09 op hdr->type == Terror ||
529 fb1a36c0 2022-01-09 op (hdr->type & 0x1) != 0) /* cannot recv a R* */
532 fb1a36c0 2022-01-09 op hdr->tag = le32toh(hdr->tag);
534 fb1a36c0 2022-01-09 op *cnt = (uint8_t *)data;
538 fb1a36c0 2022-01-09 op /* TODO: send a proper message to terminate the connection. */
539 fb1a36c0 2022-01-09 op fatalx("got invalid message");
543 fb1a36c0 2022-01-09 op np_write16(struct evbuffer *e, uint16_t x)
545 fb1a36c0 2022-01-09 op x = htole16(x);
546 fb1a36c0 2022-01-09 op evbuffer_add(e, &x, sizeof(x));
550 fb1a36c0 2022-01-09 op np_write32(struct evbuffer *e, uint32_t x)
552 fb1a36c0 2022-01-09 op x = htole32(x);
553 fb1a36c0 2022-01-09 op evbuffer_add(e, &x, sizeof(x));
557 fb1a36c0 2022-01-09 op np_write64(struct evbuffer *e, uint64_t x)
559 fb1a36c0 2022-01-09 op x = htole64(x);
560 fb1a36c0 2022-01-09 op evbuffer_add(e, &x, sizeof(x));
564 fb1a36c0 2022-01-09 op np_writebuf(struct evbuffer *e, size_t len, void *data)
566 fb1a36c0 2022-01-09 op evbuffer_add(e, data, len);
570 fb1a36c0 2022-01-09 op np_header(uint32_t len, uint8_t type, uint16_t tag)
572 fb1a36c0 2022-01-09 op len += HEADERSIZE;
574 fb1a36c0 2022-01-09 op len = htole32(len);
575 fb1a36c0 2022-01-09 op tag = htole16(tag);
577 fb1a36c0 2022-01-09 op evbuffer_add(evb, &len, sizeof(len));
578 fb1a36c0 2022-01-09 op evbuffer_add(evb, &type, sizeof(type));
579 fb1a36c0 2022-01-09 op evbuffer_add(evb, &tag, sizeof(tag));
583 fb1a36c0 2022-01-09 op np_string(struct evbuffer *e, uint16_t len, const char *str)
585 fb1a36c0 2022-01-09 op uint16_t l = len;
587 fb1a36c0 2022-01-09 op len = htole16(len);
588 fb1a36c0 2022-01-09 op evbuffer_add(e, &len, sizeof(len));
589 fb1a36c0 2022-01-09 op evbuffer_add(e, str, l);
593 fb1a36c0 2022-01-09 op np_qid(struct evbuffer *e, struct qid *qid)
595 fb1a36c0 2022-01-09 op uint64_t path;
596 fb1a36c0 2022-01-09 op uint32_t vers;
598 fb1a36c0 2022-01-09 op path = htole64(qid->path);
599 fb1a36c0 2022-01-09 op vers = htole32(qid->vers);
601 fb1a36c0 2022-01-09 op evbuffer_add(e, &qid->type, sizeof(qid->type));
602 fb1a36c0 2022-01-09 op evbuffer_add(e, &vers, sizeof(vers));
603 fb1a36c0 2022-01-09 op evbuffer_add(e, &path, sizeof(path));
607 fb1a36c0 2022-01-09 op do_send(void)
612 fb1a36c0 2022-01-09 op len = EVBUFFER_LENGTH(evb);
613 fb1a36c0 2022-01-09 op data = EVBUFFER_DATA(evb);
615 fb1a36c0 2022-01-09 op #if DEBUG_PACKETS
616 fb1a36c0 2022-01-09 op hexdump("outgoing packet", data, len);
618 fb1a36c0 2022-01-09 op client_send_listener(IMSG_BUF, data, len);
619 fb1a36c0 2022-01-09 op evbuffer_drain(evb, len);
623 fb1a36c0 2022-01-09 op np_version(uint16_t tag, uint32_t msize, const char *version)
627 fb1a36c0 2022-01-09 op l = strlen(version);
629 fb1a36c0 2022-01-09 op msize = htole32(msize);
631 fb1a36c0 2022-01-09 op np_header(sizeof(msize) + sizeof(l) + l, Rversion, tag);
632 fb1a36c0 2022-01-09 op evbuffer_add(evb, &msize, sizeof(msize));
633 fb1a36c0 2022-01-09 op np_string(evb, l, version);
638 fb1a36c0 2022-01-09 op np_attach(uint16_t tag, struct qid *qid)
640 fb1a36c0 2022-01-09 op np_header(QIDSIZE, Rattach, tag);
641 fb1a36c0 2022-01-09 op np_qid(evb, qid);
646 fb1a36c0 2022-01-09 op np_clunk(uint16_t tag)
648 fb1a36c0 2022-01-09 op np_header(0, Rclunk, tag);
653 fb1a36c0 2022-01-09 op np_flush(uint16_t tag)
655 fb1a36c0 2022-01-09 op np_header(0, Rflush, tag);
660 fb1a36c0 2022-01-09 op np_walk(uint16_t tag, int nwqid, struct qid *wqid)
664 fb1a36c0 2022-01-09 op /* two bytes for the counter */
665 fb1a36c0 2022-01-09 op np_header(2 + QIDSIZE * nwqid, Rwalk, tag);
666 fb1a36c0 2022-01-09 op np_write16(evb, nwqid);
667 fb1a36c0 2022-01-09 op for (i = 0; i < nwqid; ++i)
668 fb1a36c0 2022-01-09 op np_qid(evb, wqid + i);
674 fb1a36c0 2022-01-09 op np_open(uint16_t tag, struct qid *qid, uint32_t iounit)
676 fb1a36c0 2022-01-09 op np_header(QIDSIZE + sizeof(iounit), Ropen, tag);
677 fb1a36c0 2022-01-09 op np_qid(evb, qid);
678 fb1a36c0 2022-01-09 op np_write32(evb, iounit);
683 fb1a36c0 2022-01-09 op np_create(uint16_t tag, struct qid *qid, uint32_t iounit)
685 fb1a36c0 2022-01-09 op np_header(QIDSIZE + sizeof(iounit), Rcreate, tag);
686 fb1a36c0 2022-01-09 op np_qid(evb, qid);
687 fb1a36c0 2022-01-09 op np_write32(evb, iounit);
692 fb1a36c0 2022-01-09 op np_read(uint16_t tag, uint32_t count, void *data)
694 fb1a36c0 2022-01-09 op if (sizeof(count) + count + HEADERSIZE >= msize) {
695 fb1a36c0 2022-01-09 op np_error(tag, "Rread would overflow");
699 fb1a36c0 2022-01-09 op np_header(sizeof(count) + count, Rread, tag);
700 fb1a36c0 2022-01-09 op np_write32(evb, count);
701 fb1a36c0 2022-01-09 op np_writebuf(evb, count, data);
706 fb1a36c0 2022-01-09 op np_write(uint16_t tag, uint32_t count)
708 fb1a36c0 2022-01-09 op np_header(sizeof(count), Rwrite, tag);
709 fb1a36c0 2022-01-09 op np_write32(evb, count);
714 fb1a36c0 2022-01-09 op np_stat(uint16_t tag, uint32_t count, void *data)
716 fb1a36c0 2022-01-09 op if (sizeof(count) + count + HEADERSIZE >= msize) {
717 fb1a36c0 2022-01-09 op np_error(tag, "Rstat would overflow");
721 fb1a36c0 2022-01-09 op np_header(count, Rstat, tag);
722 fb1a36c0 2022-01-09 op np_writebuf(evb, count, data);
727 755d75bf 2022-01-17 op np_wstat(uint16_t tag)
729 755d75bf 2022-01-17 op np_header(0, Rwstat, tag);
734 fb1a36c0 2022-01-09 op np_remove(uint16_t tag)
736 fb1a36c0 2022-01-09 op np_header(0, Rremove, tag);
741 fb1a36c0 2022-01-09 op np_error(uint16_t tag, const char *errstr)
745 fb1a36c0 2022-01-09 op l = strlen(errstr);
747 fb1a36c0 2022-01-09 op np_header(sizeof(l) + l, Rerror, tag);
748 fb1a36c0 2022-01-09 op np_string(evb, l, errstr);
753 fb1a36c0 2022-01-09 op np_errno(uint16_t tag)
755 fb1a36c0 2022-01-09 op int saved_errno;
756 fb1a36c0 2022-01-09 op char buf[NL_TEXTMAX] = {0};
758 fb1a36c0 2022-01-09 op saved_errno = errno;
760 fb1a36c0 2022-01-09 op strerror_r(errno, buf, sizeof(buf));
761 fb1a36c0 2022-01-09 op np_error(tag, buf);
763 fb1a36c0 2022-01-09 op errno = saved_errno;
767 fb1a36c0 2022-01-09 op np_read8(const char *t, const char *f, uint8_t *dst, const uint8_t **src,
770 fb1a36c0 2022-01-09 op if (*len < sizeof(*dst)) {
771 fb1a36c0 2022-01-09 op log_warnx("%s: wanted %zu bytes for the %s field but only "
772 fb1a36c0 2022-01-09 op "%zu are available.", t, sizeof(*dst), f, *len);
776 fb1a36c0 2022-01-09 op memcpy(dst, *src, sizeof(*dst));
777 fb1a36c0 2022-01-09 op *src += sizeof(*dst);
778 fb1a36c0 2022-01-09 op *len -= sizeof(*dst);
784 fb1a36c0 2022-01-09 op np_read16(const char *t, const char *f, uint16_t *dst, const uint8_t **src,
787 fb1a36c0 2022-01-09 op if (*len < sizeof(*dst)) {
788 fb1a36c0 2022-01-09 op log_warnx("%s: wanted %zu bytes for the %s field but only "
789 fb1a36c0 2022-01-09 op "%zu are available.", t, sizeof(*dst), f, *len);
793 fb1a36c0 2022-01-09 op memcpy(dst, *src, sizeof(*dst));
794 fb1a36c0 2022-01-09 op *src += sizeof(*dst);
795 fb1a36c0 2022-01-09 op *len -= sizeof(*dst);
796 fb1a36c0 2022-01-09 op *dst = le16toh(*dst);
802 fb1a36c0 2022-01-09 op np_read32(const char *t, const char *f, uint32_t *dst, const uint8_t **src,
805 fb1a36c0 2022-01-09 op if (*len < sizeof(*dst)) {
806 fb1a36c0 2022-01-09 op log_warnx("%s: wanted %zu bytes for the %s field but only "
807 fb1a36c0 2022-01-09 op "%zu are available.", t, sizeof(*dst), f, *len);
811 fb1a36c0 2022-01-09 op memcpy(dst, *src, sizeof(*dst));
812 fb1a36c0 2022-01-09 op *src += sizeof(*dst);
813 fb1a36c0 2022-01-09 op *len -= sizeof(*dst);
814 fb1a36c0 2022-01-09 op *dst = le32toh(*dst);
820 fb1a36c0 2022-01-09 op np_read64(const char *t, const char *f, uint64_t *dst, const uint8_t **src,
823 fb1a36c0 2022-01-09 op if (*len < sizeof(*dst)) {
824 fb1a36c0 2022-01-09 op log_warnx("%s: wanted %zu bytes for the %s field but only "
825 fb1a36c0 2022-01-09 op "%zu are available.", t, sizeof(*dst), f, *len);
829 fb1a36c0 2022-01-09 op memcpy(dst, *src, sizeof(*dst));
830 fb1a36c0 2022-01-09 op *src += sizeof(*dst);
831 fb1a36c0 2022-01-09 op *len -= sizeof(*dst);
832 fb1a36c0 2022-01-09 op *dst = le64toh(*dst);
838 fb1a36c0 2022-01-09 op np_readstr(const char *t, const char *f, char *res, size_t reslen,
839 fb1a36c0 2022-01-09 op const uint8_t **src, size_t *len)
842 fb1a36c0 2022-01-09 op char buf[32];
844 fb1a36c0 2022-01-09 op strlcpy(buf, f, sizeof(buf));
845 fb1a36c0 2022-01-09 op strlcat(buf, "-len", sizeof(buf));
847 fb1a36c0 2022-01-09 op if (!np_read16(t, buf, &sl, src, len))
848 fb1a36c0 2022-01-09 op return READSTRERR;
850 fb1a36c0 2022-01-09 op if (*len < sl) {
851 fb1a36c0 2022-01-09 op log_warnx("%s: wanted %d bytes for the %s field but only "
852 fb1a36c0 2022-01-09 op "%zu are available.", t, sl, f, *len);
853 fb1a36c0 2022-01-09 op return READSTRERR;
856 fb1a36c0 2022-01-09 op if (*len > reslen-1)
857 fb1a36c0 2022-01-09 op return READSTRTRUNC;
859 fb1a36c0 2022-01-09 op memcpy(res, *src, sl);
860 fb1a36c0 2022-01-09 op res[sl] = '\0';
868 755d75bf 2022-01-17 op np_readst(const char *t, const char *f, struct np_stat *st,
869 755d75bf 2022-01-17 op char *name, size_t namelen, const uint8_t **src, size_t *len)
871 755d75bf 2022-01-17 op memset(st, 0, sizeof(*st));
873 9029ac6f 2022-01-19 op if (!np_read16(t, "stat.size", &st->size, src, len) ||
874 9029ac6f 2022-01-19 op !np_read16(t, "stat.type", &st->type, src, len) ||
875 755d75bf 2022-01-17 op !np_read32(t, "stat.dev", &st->dev, src, len) ||
876 755d75bf 2022-01-17 op !np_read64(t, "stat.qid.path", &st->qid.path, src, len) ||
877 755d75bf 2022-01-17 op !np_read32(t, "stat.qid.vers", &st->qid.vers, src, len) ||
878 755d75bf 2022-01-17 op !np_read8(t, "stat.qid.type", &st->qid.type, src, len) ||
879 755d75bf 2022-01-17 op !np_read32(t, "stat.mode", &st->mode, src, len) ||
880 755d75bf 2022-01-17 op !np_read32(t, "stat.atime", &st->atime, src, len) ||
881 755d75bf 2022-01-17 op !np_read32(t, "stat.mtime", &st->mtime, src, len) ||
882 755d75bf 2022-01-17 op !np_read64(t, "stat.length", &st->length, src, len))
883 755d75bf 2022-01-17 op return READSTRERR;
886 755d75bf 2022-01-17 op * ignore everything but the name, we don't support
887 755d75bf 2022-01-17 op * changing those fields anyway
889 755d75bf 2022-01-17 op st->name = name;
890 755d75bf 2022-01-17 op return np_readstr(t, "stat.name", name, namelen, src, len);
894 fb1a36c0 2022-01-09 op tversion(struct np_msg_header *hdr, const uint8_t *data, size_t len)
896 fb1a36c0 2022-01-09 op char *dot, version[32];
898 fb1a36c0 2022-01-09 op if (handshaked)
901 fb1a36c0 2022-01-09 op /* msize[4] version[s] */
902 fb1a36c0 2022-01-09 op if (!NPREAD32("msize", &msize, &data, &len))
905 fb1a36c0 2022-01-09 op switch (NPREADSTR("version", version, sizeof(version), &data, &len)) {
906 fb1a36c0 2022-01-09 op case READSTRERR:
908 fb1a36c0 2022-01-09 op case READSTRTRUNC:
909 fb1a36c0 2022-01-09 op log_warnx("9P version string too long, truncated");
910 fb1a36c0 2022-01-09 op goto mismatch;
913 bcdb46a7 2022-01-19 op if (len != 0)
916 fb1a36c0 2022-01-09 op if ((dot = strchr(version, '.')) != NULL)
919 fb1a36c0 2022-01-09 op if (strcmp(version, VERSION9P) != 0 ||
921 fb1a36c0 2022-01-09 op goto mismatch;
923 fb1a36c0 2022-01-09 op /* version matched */
924 fb1a36c0 2022-01-09 op handshaked = 1;
925 fb1a36c0 2022-01-09 op msize = MIN(msize, CLIENT_MSIZE);
926 fb1a36c0 2022-01-09 op client_send_listener(IMSG_MSIZE, &msize, sizeof(msize));
927 fb1a36c0 2022-01-09 op np_version(hdr->tag, msize, VERSION9P);
931 fb1a36c0 2022-01-09 op log_warnx("unknown 9P version string: \"%s\", want "VERSION9P,
933 fb1a36c0 2022-01-09 op np_version(hdr->tag, MSIZE9P, "unknown");
937 fb1a36c0 2022-01-09 op client_send_listener(IMSG_CLOSE, NULL, 0);
938 fb1a36c0 2022-01-09 op client_shutdown();
942 fb1a36c0 2022-01-09 op tattach(struct np_msg_header *hdr, const uint8_t *data, size_t len)
944 fb1a36c0 2022-01-09 op struct dir *dir;
945 fb1a36c0 2022-01-09 op struct fid *f;
946 fb1a36c0 2022-01-09 op uint32_t fid, afid;
948 fb1a36c0 2022-01-09 op char aname[PATH_MAX];
950 fb1a36c0 2022-01-09 op /* fid[4] afid[4] uname[s] aname[s] */
952 fb1a36c0 2022-01-09 op if (!NPREAD32("fid", &fid, &data, &len) ||
953 fb1a36c0 2022-01-09 op !NPREAD32("afid", &afid, &data, &len))
956 fb1a36c0 2022-01-09 op /* read the uname but don't actually use it */
957 fb1a36c0 2022-01-09 op switch (NPREADSTR("uname", aname, sizeof(aname), &data, &len)) {
958 fb1a36c0 2022-01-09 op case READSTRERR:
960 fb1a36c0 2022-01-09 op case READSTRTRUNC:
961 fb1a36c0 2022-01-09 op np_error(hdr->tag, "name too long");
965 fb1a36c0 2022-01-09 op switch (NPREADSTR("aname", aname, sizeof(aname), &data, &len)) {
966 fb1a36c0 2022-01-09 op case READSTRERR:
968 fb1a36c0 2022-01-09 op case READSTRTRUNC:
969 fb1a36c0 2022-01-09 op np_error(hdr->tag, "name too long");
973 bcdb46a7 2022-01-19 op if (len != 0)
976 fb1a36c0 2022-01-09 op if (fid_by_id(fid) != NULL || afid != NOFID) {
977 fb1a36c0 2022-01-09 op np_error(hdr->tag, "invalid fid or afid");
981 fb1a36c0 2022-01-09 op if ((fd = open(aname, O_RDONLY|O_DIRECTORY)) == -1)
984 fb1a36c0 2022-01-09 op if ((dir = new_dir(fd)) == NULL)
987 fb1a36c0 2022-01-09 op log_debug("attached %s to %d", aname, fid);
989 fb1a36c0 2022-01-09 op if ((f = new_fid(dir, fid, aname, NULL)) == NULL) {
990 fb1a36c0 2022-01-09 op dir_decref(dir);
994 fb1a36c0 2022-01-09 op np_attach(hdr->tag, &f->qid);
998 fb1a36c0 2022-01-09 op np_errno(hdr->tag);
999 fb1a36c0 2022-01-09 op log_warn("failed to attach %s", aname);
1003 fb1a36c0 2022-01-09 op client_send_listener(IMSG_CLOSE, NULL, 0);
1004 fb1a36c0 2022-01-09 op client_shutdown();
1008 fb1a36c0 2022-01-09 op tclunk(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1010 fb1a36c0 2022-01-09 op struct fid *f;
1011 fb1a36c0 2022-01-09 op uint32_t fid;
1013 fb1a36c0 2022-01-09 op /* fid[4] */
1014 bcdb46a7 2022-01-19 op if (!NPREAD32("fid", &fid, &data, &len) ||
1016 fb1a36c0 2022-01-09 op client_send_listener(IMSG_CLOSE, NULL, 0);
1017 fb1a36c0 2022-01-09 op client_shutdown();
1021 fb1a36c0 2022-01-09 op if ((f = fid_by_id(fid)) == NULL) {
1022 fb1a36c0 2022-01-09 op np_error(hdr->tag, "invalid fid");
1026 fb1a36c0 2022-01-09 op free_fid(f);
1027 fb1a36c0 2022-01-09 op np_clunk(hdr->tag);
1031 fb1a36c0 2022-01-09 op tflush(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1033 fb1a36c0 2022-01-09 op uint16_t oldtag;
1036 fb1a36c0 2022-01-09 op * We're doing only synchronous I/O. Tflush is implemented
1037 fb1a36c0 2022-01-09 op * only because it's illegal to reply with a Rerror.
1040 fb1a36c0 2022-01-09 op /* oldtag[2] */
1041 fb1a36c0 2022-01-09 op if (len != sizeof(oldtag)) {
1042 fb1a36c0 2022-01-09 op log_warnx("Tflush with the wrong size: got %zu want %zu",
1043 fb1a36c0 2022-01-09 op len, sizeof(oldtag));
1044 fb1a36c0 2022-01-09 op client_send_listener(IMSG_CLOSE, NULL, 0);
1045 fb1a36c0 2022-01-09 op client_shutdown();
1049 fb1a36c0 2022-01-09 op np_flush(hdr->tag);
1053 fb1a36c0 2022-01-09 op twalk(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1055 fb1a36c0 2022-01-09 op struct stat sb;
1056 fb1a36c0 2022-01-09 op struct dir *dir;
1057 fb1a36c0 2022-01-09 op struct qid wqid[MAXWELEM] = {0};
1058 fb1a36c0 2022-01-09 op struct fid *f, *nf;
1059 fb1a36c0 2022-01-09 op uint32_t fid, newfid;
1060 fb1a36c0 2022-01-09 op uint16_t nwname;
1061 fb1a36c0 2022-01-09 op int fd, oldfd, no, nwqid = 0;
1062 fb1a36c0 2022-01-09 op char wnam[PATH_MAX];
1064 fb1a36c0 2022-01-09 op if (!NPREAD32("fid", &fid, &data, &len) ||
1065 fb1a36c0 2022-01-09 op !NPREAD32("newfid", &newfid, &data, &len) ||
1066 fb1a36c0 2022-01-09 op !NPREAD16("nwname", &nwname, &data, &len))
1069 fb1a36c0 2022-01-09 op if (nwname > MAXWELEM) {
1070 fb1a36c0 2022-01-09 op log_warnx("Twalk: more than %d path elements: %d",
1071 fb1a36c0 2022-01-09 op MAXWELEM, nwname);
1075 fb1a36c0 2022-01-09 op if ((f = fid_by_id(fid)) == NULL) {
1076 fb1a36c0 2022-01-09 op np_error(hdr->tag, "invalid fid");
1080 fb1a36c0 2022-01-09 op if (f->fd != -1) {
1081 fb1a36c0 2022-01-09 op np_error(hdr->tag, "fid already opened for I/O");
1085 fb1a36c0 2022-01-09 op if (fid == newfid)
1087 fb1a36c0 2022-01-09 op else if ((nf = fid_by_id(newfid)) != NULL) {
1088 fb1a36c0 2022-01-09 op np_error(hdr->tag, "newfid already in use");
1093 fb1a36c0 2022-01-09 op /* special case: fid duplication */
1094 fb1a36c0 2022-01-09 op if (nwname == 0) {
1096 fb1a36c0 2022-01-09 op * TODO: should we forbid fids duplication when fid ==
1099 fb1a36c0 2022-01-09 op if (nf == NULL &&
1100 543fc013 2022-01-17 op (nf = new_fid(f->dir, newfid, f->fname, &f->qid)) == NULL)
1101 fb1a36c0 2022-01-09 op fatal("new_fid duplication");
1103 fb1a36c0 2022-01-09 op np_walk(hdr->tag, 0, NULL);
1107 fb1a36c0 2022-01-09 op if (!(f->qid.type & QTDIR)) {
1108 fb1a36c0 2022-01-09 op np_error(hdr->tag, "fid doesn't represent a directory");
1112 fb1a36c0 2022-01-09 op oldfd = f->dir->fd;
1114 fb1a36c0 2022-01-09 op for (nwqid = 0; nwqid < nwname; nwqid++) {
1115 fb1a36c0 2022-01-09 op switch (NPREADSTR("wname", wnam, sizeof(wnam), &data, &len)) {
1116 fb1a36c0 2022-01-09 op case READSTRERR:
1118 fb1a36c0 2022-01-09 op case READSTRTRUNC:
1119 fb1a36c0 2022-01-09 op np_error(hdr->tag, "wname too long");
1123 fb1a36c0 2022-01-09 op if (*wnam == '\0' ||
1124 fb1a36c0 2022-01-09 op strchr(wnam, '/') != NULL ||
1125 fb1a36c0 2022-01-09 op !strcmp(wnam, ".")) {
1126 fb1a36c0 2022-01-09 op errno = EINVAL;
1127 fb1a36c0 2022-01-09 op goto cantopen;
1130 fb1a36c0 2022-01-09 op if ((fd = openat(oldfd, wnam, O_RDONLY|O_DIRECTORY)) == -1 &&
1131 fb1a36c0 2022-01-09 op errno != ENOTDIR)
1132 fb1a36c0 2022-01-09 op goto cantopen;
1134 fb1a36c0 2022-01-09 op if ((fd == -1 && fstatat(oldfd, wnam, &sb, 0) == -1) ||
1135 fb1a36c0 2022-01-09 op (fd != -1 && fstat(fd, &sb) == -1))
1136 fb1a36c0 2022-01-09 op goto cantopen;
1138 fb1a36c0 2022-01-09 op qid_update_from_sb(&wqid[nwqid], &sb);
1140 fb1a36c0 2022-01-09 op /* reached a file but we still have other components */
1141 fb1a36c0 2022-01-09 op if (fd == -1 && nwqid+1 < nwname)
1142 fb1a36c0 2022-01-09 op goto cantopen;
1144 fb1a36c0 2022-01-09 op /* reached the end and found a file */
1145 fb1a36c0 2022-01-09 op if (fd == -1 && nwqid+1 == nwname)
1148 fb1a36c0 2022-01-09 op if (oldfd != f->dir->fd)
1149 fb1a36c0 2022-01-09 op close(oldfd);
1153 bcdb46a7 2022-01-19 op if (len != 0)
1157 fb1a36c0 2022-01-09 op * If fd is -1 we've reached a file, otherwise we've just
1158 fb1a36c0 2022-01-09 op * reached another directory. We must pay attention to what
1159 fb1a36c0 2022-01-09 op * file descriptor we use to create the dir, because if we've
1160 fb1a36c0 2022-01-09 op * reached a file and oldfd is f->dir->fd then we *must* share
1161 fb1a36c0 2022-01-09 op * the same dir (it was a walk of one path from a directory to a
1162 fb1a36c0 2022-01-09 op * file, otherwise fun is bound to happen as soon as the client
1163 fb1a36c0 2022-01-09 op * closes the fid for the directory but keeps the one for the
1166 fb1a36c0 2022-01-09 op if (fd == -1 && oldfd == f->dir->fd)
1167 fb1a36c0 2022-01-09 op dir = f->dir;
1168 fb1a36c0 2022-01-09 op else if (fd == -1)
1169 fb1a36c0 2022-01-09 op dir = new_dir(oldfd);
1171 fb1a36c0 2022-01-09 op dir = new_dir(fd);
1173 fb1a36c0 2022-01-09 op if (dir == NULL)
1174 fb1a36c0 2022-01-09 op fatal("new_dir");
1176 fb1a36c0 2022-01-09 op if (nf == NULL) {
1177 fb1a36c0 2022-01-09 op if ((nf = new_fid(dir, newfid, wnam, &wqid[nwqid-1])) == NULL)
1178 fb1a36c0 2022-01-09 op fatal("new fid");
1180 fb1a36c0 2022-01-09 op /* update the dir */
1181 fb1a36c0 2022-01-09 op dir_decref(nf->dir);
1182 fb1a36c0 2022-01-09 op nf->dir = dir_incref(dir);
1185 fb1a36c0 2022-01-09 op np_walk(hdr->tag, nwqid, wqid);
1189 fb1a36c0 2022-01-09 op if (oldfd != f->dir->fd)
1190 fb1a36c0 2022-01-09 op close(oldfd);
1192 fb1a36c0 2022-01-09 op if (nwqid == 0)
1193 fb1a36c0 2022-01-09 op np_error(hdr->tag, strerror(no));
1195 fb1a36c0 2022-01-09 op np_walk(hdr->tag, nwqid, wqid);
1199 fb1a36c0 2022-01-09 op client_send_listener(IMSG_CLOSE, NULL, 0);
1200 fb1a36c0 2022-01-09 op client_shutdown();
1203 fb1a36c0 2022-01-09 op static inline int
1204 fb1a36c0 2022-01-09 op npmode_to_unix(uint8_t mode, int *flags)
1206 fb1a36c0 2022-01-09 op switch (mode & 0x0F) {
1207 fb1a36c0 2022-01-09 op case KOREAD:
1208 fb1a36c0 2022-01-09 op *flags = O_RDONLY;
1210 fb1a36c0 2022-01-09 op case KOWRITE:
1211 fb1a36c0 2022-01-09 op *flags = O_WRONLY;
1213 fb1a36c0 2022-01-09 op case KORDWR:
1214 fb1a36c0 2022-01-09 op *flags = O_RDWR;
1216 fb1a36c0 2022-01-09 op case KOEXEC:
1217 fb1a36c0 2022-01-09 op log_warnx("tried to open something with KOEXEC");
1218 fb1a36c0 2022-01-09 op /* fallthrough */
1223 fb1a36c0 2022-01-09 op if (mode & KOTRUNC)
1224 fb1a36c0 2022-01-09 op *flags |= O_TRUNC;
1225 fb1a36c0 2022-01-09 op if (mode & KORCLOSE)
1226 fb1a36c0 2022-01-09 op *flags |= O_CLOEXEC;
1232 fb1a36c0 2022-01-09 op topen(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1234 fb1a36c0 2022-01-09 op struct stat sb;
1235 fb1a36c0 2022-01-09 op struct qid qid;
1236 fb1a36c0 2022-01-09 op struct fid *f;
1237 fb1a36c0 2022-01-09 op uint32_t fid;
1238 fb1a36c0 2022-01-09 op uint8_t mode;
1239 fb1a36c0 2022-01-09 op const char *path;
1241 fb1a36c0 2022-01-09 op /* fid[4] mode[1] */
1242 fb1a36c0 2022-01-09 op if (!NPREAD32("fid", &fid, &data, &len) ||
1243 bcdb46a7 2022-01-19 op !NPREAD8("mode", &mode, &data, &len) ||
1245 fb1a36c0 2022-01-09 op client_send_listener(IMSG_CLOSE, NULL, 0);
1246 fb1a36c0 2022-01-09 op client_shutdown();
1250 fb1a36c0 2022-01-09 op if ((f = fid_by_id(fid)) == NULL || f->fd != -1) {
1251 fb1a36c0 2022-01-09 op np_error(hdr->tag, "invalid fid");
1255 fb1a36c0 2022-01-09 op if (npmode_to_unix(mode, &f->iomode) == -1) {
1256 fb1a36c0 2022-01-09 op np_error(hdr->tag, "invalid mode");
1260 543fc013 2022-01-17 op path = f->fname;
1261 fb1a36c0 2022-01-09 op if (f->qid.type & QTDIR)
1264 fb1a36c0 2022-01-09 op if ((f->fd = openat(f->dir->fd, path, f->iomode)) == -1) {
1265 fb1a36c0 2022-01-09 op np_error(hdr->tag, strerror(errno));
1269 fb1a36c0 2022-01-09 op if (fstat(f->fd, &sb) == -1)
1270 fb1a36c0 2022-01-09 op fatal("fstat");
1272 fb1a36c0 2022-01-09 op if (S_ISDIR(sb.st_mode)) {
1273 fb1a36c0 2022-01-09 op if ((f->d = fdopendir(f->fd)) == NULL) {
1274 fb1a36c0 2022-01-09 op np_errno(hdr->tag);
1275 fb1a36c0 2022-01-09 op close(f->fd);
1280 fb1a36c0 2022-01-09 op if ((f->evb = evbuffer_new()) == NULL) {
1281 fb1a36c0 2022-01-09 op np_errno(hdr->tag);
1282 fb1a36c0 2022-01-09 op closedir(f->d);
1283 fb1a36c0 2022-01-09 op f->d = NULL;
1288 fb1a36c0 2022-01-09 op f->offset = 0;
1290 fb1a36c0 2022-01-09 op qid_update_from_sb(&qid, &sb);
1291 fb1a36c0 2022-01-09 op np_open(hdr->tag, &qid, sb.st_blksize);
1295 fb1a36c0 2022-01-09 op tcreate(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1297 fb1a36c0 2022-01-09 op struct stat sb;
1298 fb1a36c0 2022-01-09 op struct qid qid;
1299 fb1a36c0 2022-01-09 op struct fid *f;
1300 fb1a36c0 2022-01-09 op uint32_t fid, perm;
1301 fb1a36c0 2022-01-09 op uint8_t mode;
1302 fb1a36c0 2022-01-09 op char name[PATH_MAX];
1304 fb1a36c0 2022-01-09 op /* fid[4] name[s] perm[4] mode[1] */
1305 fb1a36c0 2022-01-09 op if (!NPREAD32("fid", &fid, &data, &len))
1307 fb1a36c0 2022-01-09 op switch (NPREADSTR("name", name, sizeof(name), &data, &len)) {
1308 fb1a36c0 2022-01-09 op case READSTRERR:
1310 fb1a36c0 2022-01-09 op case READSTRTRUNC:
1311 fb1a36c0 2022-01-09 op np_error(hdr->tag, "name too long");
1314 fb1a36c0 2022-01-09 op if (!NPREAD32("perm", &perm, &data, &len) ||
1315 bcdb46a7 2022-01-19 op !NPREAD8("mode", &mode, &data, &len) ||
1319 fb1a36c0 2022-01-09 op if (!strcmp(name, ".") || !strcmp(name, "..") ||
1320 fb1a36c0 2022-01-09 op strchr(name, '/') != NULL) {
1321 fb1a36c0 2022-01-09 op np_error(hdr->tag, "invalid name");
1325 fb1a36c0 2022-01-09 op if ((f = fid_by_id(fid)) == NULL || f->fd != -1) {
1326 fb1a36c0 2022-01-09 op np_error(hdr->tag, "invalid fid");
1330 fb1a36c0 2022-01-09 op if (!(f->qid.type & QTDIR)) {
1331 fb1a36c0 2022-01-09 op np_error(hdr->tag, "fid doesn't identify a directory");
1335 fb1a36c0 2022-01-09 op if (npmode_to_unix(mode, &f->iomode) == -1) {
1336 fb1a36c0 2022-01-09 op np_error(hdr->tag, "invalid mode");
1340 fb1a36c0 2022-01-09 op if (f->iomode & O_RDONLY) {
1341 fb1a36c0 2022-01-09 op np_error(hdr->tag, "can't create a read-only file");
1345 fb1a36c0 2022-01-09 op /* TODO: parse the mode */
1347 fb1a36c0 2022-01-09 op if (perm & 0x80000000) {
1348 fb1a36c0 2022-01-09 op /* create a directory */
1349 fb1a36c0 2022-01-09 op f->fd = mkdirat(f->dir->fd, name, 0755);
1351 fb1a36c0 2022-01-09 op /* create a file */
1352 fb1a36c0 2022-01-09 op f->fd = openat(f->dir->fd, name, f->iomode | O_CREAT | O_TRUNC,
1356 fb1a36c0 2022-01-09 op if (f->fd == -1) {
1357 fb1a36c0 2022-01-09 op np_errno(hdr->tag);
1361 fb1a36c0 2022-01-09 op if (fstat(f->fd, &sb) == -1)
1362 fb1a36c0 2022-01-09 op fatal("fstat");
1364 fb1a36c0 2022-01-09 op if (S_ISDIR(sb.st_mode)) {
1365 fb1a36c0 2022-01-09 op if ((f->d = fdopendir(f->fd)) == NULL) {
1366 fb1a36c0 2022-01-09 op np_errno(hdr->tag);
1367 fb1a36c0 2022-01-09 op close(f->fd);
1372 fb1a36c0 2022-01-09 op if ((f->evb = evbuffer_new()) == NULL) {
1373 fb1a36c0 2022-01-09 op np_errno(hdr->tag);
1374 fb1a36c0 2022-01-09 op closedir(f->d);
1375 fb1a36c0 2022-01-09 op f->d = NULL;
1380 fb1a36c0 2022-01-09 op f->offset = 0;
1382 fb1a36c0 2022-01-09 op qid_update_from_sb(&qid, &sb);
1383 fb1a36c0 2022-01-09 op np_create(hdr->tag, &qid, sb.st_blksize);
1388 fb1a36c0 2022-01-09 op client_send_listener(IMSG_CLOSE, NULL, 0);
1389 fb1a36c0 2022-01-09 op client_shutdown();
1392 fb1a36c0 2022-01-09 op static inline void
1393 fb1a36c0 2022-01-09 op serialize_stat(const char *fname, struct stat *sb, struct evbuffer *evb)
1395 fb1a36c0 2022-01-09 op struct qid qid;
1396 fb1a36c0 2022-01-09 op const char *uid, *gid, *muid;
1398 fb1a36c0 2022-01-09 op uint16_t namlen, uidlen, gidlen, ulen;
1400 fb1a36c0 2022-01-09 op qid_update_from_sb(&qid, sb);
1402 fb1a36c0 2022-01-09 op /* TODO: fill these fields */
1407 fb1a36c0 2022-01-09 op namlen = strlen(fname);
1408 fb1a36c0 2022-01-09 op uidlen = strlen(uid);
1409 fb1a36c0 2022-01-09 op gidlen = strlen(gid);
1410 fb1a36c0 2022-01-09 op ulen = strlen(muid);
1412 fb1a36c0 2022-01-09 op tot = NPSTATSIZ(namlen, uidlen, gidlen, ulen);
1413 fb1a36c0 2022-01-09 op if (tot > UINT32_MAX) {
1414 fb1a36c0 2022-01-09 op log_warnx("stat info for dir entry %s would overflow",
1419 fb1a36c0 2022-01-09 op np_write16(evb, tot); /* size[2] */
1420 fb1a36c0 2022-01-09 op np_write16(evb, sb->st_rdev); /* type[2] */
1421 fb1a36c0 2022-01-09 op np_write32(evb, sb->st_dev); /* dev[4] */
1422 fb1a36c0 2022-01-09 op np_qid(evb, &qid); /* qid[13] */
1424 fb1a36c0 2022-01-09 op /* XXX: translate? */
1425 fb1a36c0 2022-01-09 op np_write32(evb, sb->st_mode); /* mode[4] */
1427 fb1a36c0 2022-01-09 op np_write32(evb, sb->st_atim.tv_sec); /* atime[4] */
1428 fb1a36c0 2022-01-09 op np_write32(evb, sb->st_mtim.tv_sec); /* mtime[4] */
1430 68a7ef68 2022-01-16 op /* special case: directories have size 0 */
1431 68a7ef68 2022-01-16 op if (qid.type & QTDIR)
1432 68a7ef68 2022-01-16 op np_write64(evb, 0);
1434 68a7ef68 2022-01-16 op np_write64(evb, sb->st_size); /* length[8] */
1436 fb1a36c0 2022-01-09 op np_string(evb, namlen, fname); /* name[s] */
1437 fb1a36c0 2022-01-09 op np_string(evb, uidlen, uid); /* uid[s] */
1438 fb1a36c0 2022-01-09 op np_string(evb, gidlen, gid); /* gid[s] */
1439 fb1a36c0 2022-01-09 op np_string(evb, ulen, muid); /* muid[s] */
1443 fb1a36c0 2022-01-09 op tread(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1445 fb1a36c0 2022-01-09 op struct fid *f;
1447 fb1a36c0 2022-01-09 op size_t howmuch;
1448 fb1a36c0 2022-01-09 op uint64_t off;
1449 fb1a36c0 2022-01-09 op uint32_t fid, count;
1450 fb1a36c0 2022-01-09 op char buf[2048];
1452 fb1a36c0 2022-01-09 op /* fid[4] offset[8] count[4] */
1453 fb1a36c0 2022-01-09 op if (!NPREAD32("fid", &fid, &data, &len) ||
1454 fb1a36c0 2022-01-09 op !NPREAD64("offset", &off, &data, &len) ||
1455 bcdb46a7 2022-01-19 op !NPREAD32("count", &count, &data, &len) ||
1457 fb1a36c0 2022-01-09 op client_send_listener(IMSG_CLOSE, NULL, 0);
1458 fb1a36c0 2022-01-09 op client_shutdown();
1462 fb1a36c0 2022-01-09 op if ((f = fid_by_id(fid)) == NULL || f->fd == -1) {
1463 fb1a36c0 2022-01-09 op np_error(hdr->tag, "invalid fid");
1467 fb1a36c0 2022-01-09 op if (TYPE_OVERFLOW(off_t, off)) {
1468 fb1a36c0 2022-01-09 op log_warnx("unexpected off_t size");
1469 fb1a36c0 2022-01-09 op np_error(hdr->tag, "invalid offset");
1473 fb1a36c0 2022-01-09 op if (f->d == NULL) {
1474 fb1a36c0 2022-01-09 op /* read a file */
1475 fb1a36c0 2022-01-09 op howmuch = MIN(sizeof(buf), count);
1476 fb1a36c0 2022-01-09 op r = pread(f->fd, buf, howmuch, (off_t)off);
1477 fb1a36c0 2022-01-09 op if (r == -1)
1478 fb1a36c0 2022-01-09 op np_errno(hdr->tag);
1480 fb1a36c0 2022-01-09 op np_read(hdr->tag, r, buf);
1482 fb1a36c0 2022-01-09 op if (off == 0 && f->offset != 0) {
1483 fb1a36c0 2022-01-09 op rewinddir(f->d);
1484 fb1a36c0 2022-01-09 op f->offset = 0;
1485 fb1a36c0 2022-01-09 op evbuffer_drain(f->evb, EVBUFFER_LENGTH(f->evb));
1488 fb1a36c0 2022-01-09 op if (off != f->offset) {
1489 fb1a36c0 2022-01-09 op np_error(hdr->tag, "can't seek in directories");
1493 fb1a36c0 2022-01-09 op while (EVBUFFER_LENGTH(f->evb) < count) {
1494 fb1a36c0 2022-01-09 op struct dirent *d;
1495 fb1a36c0 2022-01-09 op struct stat sb;
1497 fb1a36c0 2022-01-09 op if ((d = readdir(f->d)) == NULL)
1499 fb1a36c0 2022-01-09 op if (fstatat(f->fd, d->d_name, &sb, 0) == -1) {
1500 fb1a36c0 2022-01-09 op warn("fstatat");
1503 fb1a36c0 2022-01-09 op serialize_stat(d->d_name, &sb, f->evb);
1506 fb1a36c0 2022-01-09 op count = MIN(count, EVBUFFER_LENGTH(f->evb));
1507 fb1a36c0 2022-01-09 op np_read(hdr->tag, count, EVBUFFER_DATA(f->evb));
1508 fb1a36c0 2022-01-09 op evbuffer_drain(f->evb, count);
1510 fb1a36c0 2022-01-09 op f->offset += count;
1515 fb1a36c0 2022-01-09 op twrite(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1517 fb1a36c0 2022-01-09 op struct fid *f;
1519 fb1a36c0 2022-01-09 op uint64_t off;
1520 fb1a36c0 2022-01-09 op uint32_t fid, count;
1522 fb1a36c0 2022-01-09 op /* fid[4] offset[8] count[4] data[count] */
1523 fb1a36c0 2022-01-09 op if (!NPREAD32("fid", &fid, &data, &len) ||
1524 fb1a36c0 2022-01-09 op !NPREAD64("off", &off, &data, &len) ||
1525 fb1a36c0 2022-01-09 op !NPREAD32("count", &count, &data, &len) ||
1526 fb1a36c0 2022-01-09 op len != count) {
1527 fb1a36c0 2022-01-09 op client_send_listener(IMSG_CLOSE, NULL, 0);
1528 fb1a36c0 2022-01-09 op client_shutdown();
1532 fb1a36c0 2022-01-09 op if ((f = fid_by_id(fid)) == NULL || f->fd == -1) {
1533 fb1a36c0 2022-01-09 op np_error(hdr->tag, "invalid fid");
1537 fb1a36c0 2022-01-09 op if (!(f->iomode & O_WRONLY) &&
1538 fb1a36c0 2022-01-09 op !(f->iomode & O_RDWR)) {
1539 fb1a36c0 2022-01-09 op np_error(hdr->tag, "fid not opened for writing");
1543 fb1a36c0 2022-01-09 op if (TYPE_OVERFLOW(off_t, off)) {
1544 fb1a36c0 2022-01-09 op log_warnx("unexpected off_t size");
1545 fb1a36c0 2022-01-09 op np_error(hdr->tag, "invalid offset");
1549 fb1a36c0 2022-01-09 op if ((r = pwrite(f->fd, data, len, off)) == -1)
1550 fb1a36c0 2022-01-09 op np_errno(hdr->tag);
1552 fb1a36c0 2022-01-09 op np_write(hdr->tag, r);
1556 fb1a36c0 2022-01-09 op tstat(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1558 fb1a36c0 2022-01-09 op struct evbuffer *evb;
1559 fb1a36c0 2022-01-09 op struct stat sb;
1560 fb1a36c0 2022-01-09 op struct fid *f;
1562 fb1a36c0 2022-01-09 op uint32_t fid;
1564 fb1a36c0 2022-01-09 op /* fid[4] */
1565 bcdb46a7 2022-01-19 op if (!NPREAD32("fid", &fid, &data, &len) ||
1567 fb1a36c0 2022-01-09 op client_send_listener(IMSG_CLOSE, NULL, 0);
1568 fb1a36c0 2022-01-09 op client_shutdown();
1573 fb1a36c0 2022-01-09 op * plan9' stat(9P) is not clear on whether the stat is allowed
1574 fb1a36c0 2022-01-09 op * on opened fids or not. We're allowing stat regardless of the
1575 fb1a36c0 2022-01-09 op * status of the fid.
1578 fb1a36c0 2022-01-09 op if ((f = fid_by_id(fid)) == NULL) {
1579 fb1a36c0 2022-01-09 op np_error(hdr->tag, "invalid fid");
1583 fb1a36c0 2022-01-09 op if ((evb = evbuffer_new()) == NULL)
1584 fb1a36c0 2022-01-09 op fatal("evbuffer_new");
1586 fb1a36c0 2022-01-09 op if (f->fd != -1)
1587 fb1a36c0 2022-01-09 op r = fstat(f->fd, &sb);
1588 fb1a36c0 2022-01-09 op else if (f->qid.type & QTDIR)
1589 fb1a36c0 2022-01-09 op r = fstat(f->dir->fd, &sb);
1591 543fc013 2022-01-17 op r = fstatat(f->dir->fd, f->fname, &sb, 0);
1593 fb1a36c0 2022-01-09 op if (r == -1) {
1594 fb1a36c0 2022-01-09 op np_errno(hdr->tag);
1595 fb1a36c0 2022-01-09 op evbuffer_free(evb);
1599 543fc013 2022-01-17 op serialize_stat(f->fname, &sb, evb);
1600 fb1a36c0 2022-01-09 op np_stat(hdr->tag, EVBUFFER_LENGTH(evb), EVBUFFER_DATA(evb));
1601 fb1a36c0 2022-01-09 op evbuffer_free(evb);
1605 755d75bf 2022-01-17 op twstat(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1607 755d75bf 2022-01-17 op struct timespec times[2];
1608 755d75bf 2022-01-17 op struct np_stat st;
1609 755d75bf 2022-01-17 op struct fid *f;
1611 755d75bf 2022-01-17 op uint32_t fid;
1612 755d75bf 2022-01-17 op char name[PATH_MAX];
1614 755d75bf 2022-01-17 op /* fid[4] stat[n] */
1615 755d75bf 2022-01-17 op if (!NPREAD32("fid", &fid, &data, &len))
1618 755d75bf 2022-01-17 op switch (NPREADST("stat", &st, name, sizeof(name), &data, &len)) {
1619 755d75bf 2022-01-17 op case READSTRERR:
1621 755d75bf 2022-01-17 op case READSTRTRUNC:
1622 755d75bf 2022-01-17 op log_warnx("wstat new name would overflow");
1623 755d75bf 2022-01-17 op np_error(hdr->tag, "new name too long");
1628 f4ad0146 2022-01-19 op * We skip the reading of some fields voluntarily because we
1629 f4ad0146 2022-01-19 op * don't support chown, so len will always be > 0!
1631 f4ad0146 2022-01-19 op #ifdef notyet
1632 bcdb46a7 2022-01-19 op if (len != 0)
1636 755d75bf 2022-01-17 op if ((f = fid_by_id(fid)) == NULL) {
1637 755d75bf 2022-01-17 op np_error(hdr->tag, "invalid fid");
1642 755d75bf 2022-01-17 op * 9P wants these edits to be done in an atomic fashion,
1643 755d75bf 2022-01-17 op * something that I don't think it's possible on UNIX without
1644 755d75bf 2022-01-17 op * some special kernel help. Changing atime or mtime,
1645 755d75bf 2022-01-17 op * permissions, or rename the file are different syscalls.
1647 755d75bf 2022-01-17 op * Also, silently ignore stuff we can't/don't want to modify.
1650 755d75bf 2022-01-17 op /* change the permissions */
1651 755d75bf 2022-01-17 op if (st.mode != (uint32_t)-1) {
1652 755d75bf 2022-01-17 op mode_t m = st.mode & 0x7F; /* silently truncate higer bits */
1653 755d75bf 2022-01-17 op if (f->fd != -1)
1654 755d75bf 2022-01-17 op r = fchmod(f->fd, m);
1656 543fc013 2022-01-17 op r = fchmodat(f->dir->fd, f->fname, m, 0);
1658 755d75bf 2022-01-17 op if (r == -1) {
1659 755d75bf 2022-01-17 op np_errno(hdr->tag);
1664 755d75bf 2022-01-17 op /* change the atime/mtime of the fid, opened or not */
1665 755d75bf 2022-01-17 op if (st.atime != (uint32_t)-1 || st.mtime != (uint32_t)-1) {
1666 755d75bf 2022-01-17 op times[0].tv_nsec = UTIME_OMIT;
1667 755d75bf 2022-01-17 op times[1].tv_nsec = UTIME_OMIT;
1669 755d75bf 2022-01-17 op if (st.atime != (uint32_t)-1) {
1670 755d75bf 2022-01-17 op times[0].tv_sec = st.atime;
1671 755d75bf 2022-01-17 op times[0].tv_nsec = 0;
1673 755d75bf 2022-01-17 op if (st.mtime != (uint32_t)-1) {
1674 755d75bf 2022-01-17 op times[1].tv_sec = st.mtime;
1675 755d75bf 2022-01-17 op times[1].tv_nsec = 0;
1678 755d75bf 2022-01-17 op if (f->fd != -1)
1679 755d75bf 2022-01-17 op r = futimens(f->fd, times);
1681 543fc013 2022-01-17 op r = utimensat(f->dir->fd, f->fname, times, 0);
1683 755d75bf 2022-01-17 op if (r == -1) {
1684 755d75bf 2022-01-17 op np_errno(hdr->tag);
1689 755d75bf 2022-01-17 op /* truncate */
1690 755d75bf 2022-01-17 op if (st.length != (uint64_t)-1) {
1691 755d75bf 2022-01-17 op if (f->fd == -1) {
1692 755d75bf 2022-01-17 op np_error(hdr->tag, "can't truncate a non-opened fid");
1696 755d75bf 2022-01-17 op if (f->qid.type & QTDIR && st.length != 0) {
1697 755d75bf 2022-01-17 op np_error(hdr->tag, "can't truncate directories");
1701 755d75bf 2022-01-17 op if (TYPE_OVERFLOW(off_t, st.length)) {
1702 755d75bf 2022-01-17 op log_warnx("truncate request overflows off_t: %"PRIu64,
1704 755d75bf 2022-01-17 op np_error(hdr->tag, "length overflows");
1708 6d3d71a9 2022-01-19 op if (f->qid.type == 0 &&
1709 755d75bf 2022-01-17 op ftruncate(f->fd, st.length) == -1) {
1710 755d75bf 2022-01-17 op np_errno(hdr->tag);
1715 755d75bf 2022-01-17 op /* rename (change the name entry) */
1716 755d75bf 2022-01-17 op if (*st.name != '\0') {
1717 755d75bf 2022-01-17 op struct stat sb;
1718 755d75bf 2022-01-17 op const char *newname;
1721 755d75bf 2022-01-17 op * XXX: 9P requires that we disallow changing the name
1722 755d75bf 2022-01-17 op * to that of an existing file. We can't do that
1723 755d75bf 2022-01-17 op * atomically (rename is happy about stealing names)
1724 755d75bf 2022-01-17 op * so this is just a best effort.
1726 755d75bf 2022-01-17 op if (fstatat(f->dir->fd, st.name, &sb, 0) == -1 &&
1727 755d75bf 2022-01-17 op errno != ENOENT) {
1728 755d75bf 2022-01-17 op np_error(hdr->tag, "target file exists");
1732 543fc013 2022-01-17 op r = renameat(f->dir->fd, f->fname, f->dir->fd, st.name);
1733 755d75bf 2022-01-17 op if (r == -1) {
1734 755d75bf 2022-01-17 op np_errno(hdr->tag);
1738 755d75bf 2022-01-17 op /* fix the fid so it points to the new file */
1740 755d75bf 2022-01-17 op if ((newname = strchr(st.name, '/')) != NULL)
1741 755d75bf 2022-01-17 op newname++; /* skip / */
1743 755d75bf 2022-01-17 op newname = st.name;
1744 543fc013 2022-01-17 op strlcpy(f->fname, newname, sizeof(f->fname));
1746 755d75bf 2022-01-17 op if (strchr(st.name, '/') != NULL) {
1747 755d75bf 2022-01-17 op struct dir *dir;
1748 755d75bf 2022-01-17 op char *dname;
1751 755d75bf 2022-01-17 op dname = dirname(st.name);
1752 755d75bf 2022-01-17 op fd = openat(f->dir->fd, dname, O_RDONLY|O_DIRECTORY);
1753 755d75bf 2022-01-17 op if (fd == -1) {
1754 755d75bf 2022-01-17 op np_errno(hdr->tag);
1755 755d75bf 2022-01-17 op free_fid(f);
1759 755d75bf 2022-01-17 op if ((dir = new_dir(fd)) == NULL) {
1760 755d75bf 2022-01-17 op np_errno(hdr->tag);
1761 755d75bf 2022-01-17 op free_fid(f);
1765 755d75bf 2022-01-17 op dir_decref(f->dir);
1766 755d75bf 2022-01-17 op f->dir = dir_incref(dir);
1770 755d75bf 2022-01-17 op np_wstat(hdr->tag);
1774 755d75bf 2022-01-17 op client_send_listener(IMSG_CLOSE, NULL, 0);
1775 755d75bf 2022-01-17 op client_shutdown();
1779 fb1a36c0 2022-01-09 op tremove(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1781 fb1a36c0 2022-01-09 op struct fid *f;
1782 fb1a36c0 2022-01-09 op uint32_t fid;
1784 fb1a36c0 2022-01-09 op char dirpath[PATH_MAX + 3];
1786 fb1a36c0 2022-01-09 op /* fid[4] */
1787 bcdb46a7 2022-01-19 op if (!NPREAD32("fid", &fid, &data, &len) ||
1789 fb1a36c0 2022-01-09 op client_send_listener(IMSG_CLOSE, NULL, 0);
1790 fb1a36c0 2022-01-09 op client_shutdown();
1794 fb1a36c0 2022-01-09 op if ((f = fid_by_id(fid)) == NULL) {
1795 fb1a36c0 2022-01-09 op np_error(hdr->tag, "invalid fid");
1799 fb1a36c0 2022-01-09 op if (f->qid.type & QTDIR) { /* directory */
1800 fb1a36c0 2022-01-09 op strlcpy(dirpath, "../", sizeof(dirpath));
1801 543fc013 2022-01-17 op strlcat(dirpath, f->fname, sizeof(dirpath));
1802 fb1a36c0 2022-01-09 op r = unlinkat(f->dir->fd, dirpath, AT_REMOVEDIR);
1803 fb1a36c0 2022-01-09 op } else /* file */
1804 543fc013 2022-01-17 op r = unlinkat(f->dir->fd, f->fname, 0);
1806 fb1a36c0 2022-01-09 op if (r == -1)
1807 fb1a36c0 2022-01-09 op np_errno(hdr->tag);
1809 fb1a36c0 2022-01-09 op np_remove(hdr->tag);
1811 fb1a36c0 2022-01-09 op free_fid(f);
1815 fb1a36c0 2022-01-09 op handle_message(struct imsg *imsg, size_t len)
1817 fb1a36c0 2022-01-09 op struct msg {
1818 fb1a36c0 2022-01-09 op uint8_t type;
1819 fb1a36c0 2022-01-09 op void (*fn)(struct np_msg_header *, const uint8_t *, size_t);
1820 fb1a36c0 2022-01-09 op } msgs[] = {
1821 fb1a36c0 2022-01-09 op {Tversion, tversion},
1822 fb1a36c0 2022-01-09 op {Tattach, tattach},
1823 fb1a36c0 2022-01-09 op {Tclunk, tclunk},
1824 fb1a36c0 2022-01-09 op {Tflush, tflush},
1825 fb1a36c0 2022-01-09 op {Twalk, twalk},
1826 fb1a36c0 2022-01-09 op {Topen, topen},
1827 fb1a36c0 2022-01-09 op {Tcreate, tcreate},
1828 fb1a36c0 2022-01-09 op {Tread, tread},
1829 fb1a36c0 2022-01-09 op {Twrite, twrite},
1830 fb1a36c0 2022-01-09 op {Tstat, tstat},
1831 755d75bf 2022-01-17 op {Twstat, twstat},
1832 fb1a36c0 2022-01-09 op {Tremove, tremove},
1834 fb1a36c0 2022-01-09 op struct np_msg_header hdr;
1836 fb1a36c0 2022-01-09 op uint8_t *data;
1838 fb1a36c0 2022-01-09 op #if DEBUG_PACKETS
1839 fb1a36c0 2022-01-09 op hexdump("incoming packet", imsg->data, len);
1842 fb1a36c0 2022-01-09 op parse_message(imsg->data, len, &hdr, &data);
1843 fb1a36c0 2022-01-09 op len -= HEADERSIZE;
1845 fb1a36c0 2022-01-09 op log_debug("got request: len=%d type=%d[%s] tag=%d",
1846 fb1a36c0 2022-01-09 op hdr.len, hdr.type, pp_msg_type(hdr.type), hdr.tag);
1848 fb1a36c0 2022-01-09 op if (!handshaked && hdr.type != Tversion) {
1849 fb1a36c0 2022-01-09 op client_send_listener(IMSG_CLOSE, NULL, 0);
1850 fb1a36c0 2022-01-09 op client_shutdown();
1854 fb1a36c0 2022-01-09 op for (i = 0; i < sizeof(msgs)/sizeof(msgs[0]); ++i) {
1855 fb1a36c0 2022-01-09 op if (msgs[i].type != hdr.type)
1858 fb1a36c0 2022-01-09 op msgs[i].fn(&hdr, data, len);
1862 fb1a36c0 2022-01-09 op np_error(hdr.tag, "Not supported.");