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 93c0742a 2021-12-27 op * XXX: atm is difficult to accept messages bigger than MAX_IMSGSIZE
40 93c0742a 2021-12-27 op * minus IMSG_HEADER_SIZE, we need something to split messages into
41 93c0742a 2021-12-27 op * chunks and receive them one by the other.
43 93c0742a 2021-12-27 op * CLIENT_MSIZE is thus the maximum message size we can handle now.
45 93c0742a 2021-12-27 op #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))
56 c5d518da 2021-07-29 op uint64_t path;
57 c5d518da 2021-07-29 op uint32_t vers;
61 c7f4e1bd 2021-12-24 op STAILQ_HEAD(dirhead, dir) dirs;
65 c7f4e1bd 2021-12-24 op STAILQ_ENTRY(dir) entries;
68 c5d518da 2021-07-29 op STAILQ_HEAD(fidhead, fid) fids;
72 1bf47a19 2021-12-23 op char fpath[PATH_MAX];
75 021481ca 2021-12-13 op * the flags passed to open(2). O_CLOEXEC means ORCLOSE, that
76 021481ca 2021-12-13 op * is to unlink the file upon Tclunk.
81 6842faaf 2021-12-24 op * if fd is not -1 this fid was opened, fd represents its
82 6842faaf 2021-12-24 op * file descriptor and iomode the flags passed to open(2).
86 baa40504 2021-12-15 op struct evbuffer *evb;
89 5585f1c3 2021-12-16 op * expected offset for Tread against a directory.
91 baa40504 2021-12-15 op uint64_t offset;
93 c7f4e1bd 2021-12-24 op struct qid qid;
94 c7f4e1bd 2021-12-24 op struct dir *dir;
95 c5d518da 2021-07-29 op STAILQ_ENTRY(fid) entries;
98 8d1b399b 2021-07-22 op static struct imsgev *iev_listener;
99 2ef72ade 2021-07-28 op static struct evbuffer *evb;
100 2ef72ade 2021-07-28 op static uint32_t peerid;
102 cf758c33 2021-12-14 op static int handshaked;
103 5c485996 2021-07-28 op uint32_t msize;
105 a97ec9eb 2021-12-23 op static __dead void client_shutdown(void);
106 8d1b399b 2021-07-22 op static void client_sig_handler(int, short, void *);
107 8d1b399b 2021-07-22 op static void client_dispatch_listener(int, short, void *);
108 8d1b399b 2021-07-22 op static void client_privdrop(const char *, const char *);
110 2ef72ade 2021-07-28 op static int client_send_listener(int, const void *, uint16_t);
112 deb971f5 2021-08-12 op static void qid_update_from_sb(struct qid *, struct stat *);
114 c7f4e1bd 2021-12-24 op static struct dir *new_dir(int);
115 c7f4e1bd 2021-12-24 op static struct dir *dir_incref(struct dir *);
116 c7f4e1bd 2021-12-24 op static void dir_decref(struct dir *);
118 c7f4e1bd 2021-12-24 op static struct fid *new_fid(struct dir *, uint32_t, const char *, struct qid *);
119 c377c1b9 2021-07-30 op static struct fid *fid_by_id(uint32_t);
120 c377c1b9 2021-07-30 op static void free_fid(struct fid *);
122 48192874 2021-08-01 op static void parse_message(const uint8_t *, size_t,
123 48192874 2021-08-01 op struct np_msg_header *, uint8_t **);
125 5585f1c3 2021-12-16 op static void np_write16(struct evbuffer *, uint16_t);
126 5585f1c3 2021-12-16 op static void np_write32(struct evbuffer *, uint32_t);
127 5585f1c3 2021-12-16 op static void np_write64(struct evbuffer *, uint64_t);
128 2845cccb 2021-07-29 op static void np_header(uint32_t, uint8_t, uint16_t);
129 5585f1c3 2021-12-16 op static void np_string(struct evbuffer *, uint16_t, const char *);
130 5585f1c3 2021-12-16 op static void np_qid(struct evbuffer *, struct qid *);
131 84b10f04 2021-07-29 op static void do_send(void);
133 2845cccb 2021-07-29 op static void np_version(uint16_t, uint32_t, const char *);
134 c5d518da 2021-07-29 op static void np_attach(uint16_t, struct qid *);
135 c377c1b9 2021-07-30 op static void np_clunk(uint16_t);
136 36b30273 2021-07-30 op static void np_flush(uint16_t);
137 1c08fc54 2021-08-01 op static void np_walk(uint16_t, int, struct qid *);
138 021481ca 2021-12-13 op static void np_open(uint16_t, struct qid *, uint32_t);
139 3162e55b 2021-12-20 op static void np_create(uint16_t, struct qid *, uint32_t);
140 baa40504 2021-12-15 op static void np_read(uint16_t, uint32_t, void *);
141 2483be55 2021-12-16 op static void np_write(uint16_t, uint32_t);
142 3a2c53f5 2021-12-18 op static void np_stat(uint16_t, uint32_t, void *);
143 0592b956 2021-12-20 cage static void np_remove(uint16_t);
144 2ef72ade 2021-07-28 op static void np_error(uint16_t, const char *);
145 c5d518da 2021-07-29 op static void np_errno(uint16_t);
147 48192874 2021-08-01 op static int np_read8(const char *, const char *, uint8_t *,
148 48192874 2021-08-01 op const uint8_t **, size_t *);
149 48192874 2021-08-01 op static int np_read16(const char *, const char *, uint16_t *,
150 48192874 2021-08-01 op const uint8_t **, size_t *);
151 48192874 2021-08-01 op static int np_read32(const char *, const char *, uint32_t *,
152 48192874 2021-08-01 op const uint8_t **, size_t *);
153 baa40504 2021-12-15 op static int np_read64(const char *, const char *, uint64_t *,
154 baa40504 2021-12-15 op const uint8_t **, size_t *);
156 48192874 2021-08-01 op #define READSTRERR -1
157 48192874 2021-08-01 op #define READSTRTRUNC -2
158 48192874 2021-08-01 op static int np_readstr(const char *, const char *, char *, size_t,
159 48192874 2021-08-01 op const uint8_t **, size_t *);
161 48192874 2021-08-01 op #define NPREAD8(f, dst, src, len) np_read8(__func__, f, dst, src, len)
162 48192874 2021-08-01 op #define NPREAD16(f, dst, src, len) np_read16(__func__, f, dst, src, len)
163 48192874 2021-08-01 op #define NPREAD32(f, dst, src, len) np_read32(__func__, f, dst, src, len)
164 baa40504 2021-12-15 op #define NPREAD64(f, dst, src, len) np_read64(__func__, f, dst, src, len)
166 48192874 2021-08-01 op #define NPREADSTR(f, b, bl, src, len) np_readstr(__func__, f, b, bl, src, len)
168 e60f4e08 2021-07-30 op static void tversion(struct np_msg_header *, const uint8_t *, size_t);
169 e60f4e08 2021-07-30 op static void tattach(struct np_msg_header *, const uint8_t *, size_t);
170 c377c1b9 2021-07-30 op static void tclunk(struct np_msg_header *, const uint8_t *, size_t);
171 36b30273 2021-07-30 op static void tflush(struct np_msg_header *, const uint8_t *, size_t);
172 1c08fc54 2021-08-01 op static void twalk(struct np_msg_header *, const uint8_t *, size_t);
173 021481ca 2021-12-13 op static void topen(struct np_msg_header *, const uint8_t *, size_t);
174 3162e55b 2021-12-20 op static void tcreate(struct np_msg_header *, const uint8_t *, size_t);
175 baa40504 2021-12-15 op static void tread(struct np_msg_header *, const uint8_t *, size_t);
176 2483be55 2021-12-16 op static void twrite(struct np_msg_header *, const uint8_t *, size_t);
177 3a2c53f5 2021-12-18 op static void tstat(struct np_msg_header *, const uint8_t *, size_t);
178 0592b956 2021-12-20 cage static void tremove(struct np_msg_header *, const uint8_t *, size_t);
179 e60f4e08 2021-07-30 op static void handle_message(struct imsg *, size_t);
182 8d1b399b 2021-07-22 op client(int debug, int verbose)
184 8d1b399b 2021-07-22 op struct event ev_sigint, ev_sigterm;
186 8d1b399b 2021-07-22 op log_init(debug, LOG_DAEMON);
187 8d1b399b 2021-07-22 op log_setverbose(verbose);
189 8d1b399b 2021-07-22 op setproctitle("client");
190 8d1b399b 2021-07-22 op log_procinit("client");
192 8d1b399b 2021-07-22 op log_debug("warming up");
194 8d1b399b 2021-07-22 op event_init();
196 8d1b399b 2021-07-22 op /* Setup signal handlers */
197 8d1b399b 2021-07-22 op signal_set(&ev_sigint, SIGINT, client_sig_handler, NULL);
198 8d1b399b 2021-07-22 op signal_set(&ev_sigterm, SIGTERM, client_sig_handler, NULL);
200 8d1b399b 2021-07-22 op signal_add(&ev_sigint, NULL);
201 8d1b399b 2021-07-22 op signal_add(&ev_sigterm, NULL);
203 8d1b399b 2021-07-22 op signal(SIGPIPE, SIG_IGN);
204 8d1b399b 2021-07-22 op signal(SIGHUP, SIG_IGN);
206 8d1b399b 2021-07-22 op /* Setup pipe and event handler to the listener process */
207 8d1b399b 2021-07-22 op if ((iev_listener = malloc(sizeof(*iev_listener))) == NULL)
210 8d1b399b 2021-07-22 op imsg_init(&iev_listener->ibuf, 3);
211 8d1b399b 2021-07-22 op iev_listener->handler = client_dispatch_listener;
213 8d1b399b 2021-07-22 op /* Setup event handlers. */
214 8d1b399b 2021-07-22 op iev_listener->events = EV_READ;
215 8d1b399b 2021-07-22 op event_set(&iev_listener->ev, iev_listener->ibuf.fd,
216 8d1b399b 2021-07-22 op iev_listener->events, iev_listener->handler, iev_listener);
217 8d1b399b 2021-07-22 op event_add(&iev_listener->ev, NULL);
219 8d1b399b 2021-07-22 op event_dispatch();
220 8d1b399b 2021-07-22 op client_shutdown();
223 a97ec9eb 2021-12-23 op static __dead void
224 8d1b399b 2021-07-22 op client_shutdown(void)
226 2ef72ade 2021-07-28 op if (evb != NULL)
227 2ef72ade 2021-07-28 op evbuffer_free(evb);
229 8d1b399b 2021-07-22 op msgbuf_clear(&iev_listener->ibuf.w);
230 8d1b399b 2021-07-22 op close(iev_listener->ibuf.fd);
232 8d1b399b 2021-07-22 op free(iev_listener);
234 223c9e73 2021-08-06 op log_debug("client exiting");
239 8d1b399b 2021-07-22 op client_sig_handler(int sig, short event, void *d)
242 8d1b399b 2021-07-22 op * Normal signal handler rules don't apply because libevent
243 8d1b399b 2021-07-22 op * decouples for us.
246 8d1b399b 2021-07-22 op switch (sig) {
248 8d1b399b 2021-07-22 op case SIGTERM:
249 8d1b399b 2021-07-22 op client_shutdown();
251 8d1b399b 2021-07-22 op fatalx("unexpected signal %d", sig);
255 8d1b399b 2021-07-22 op #define AUTH_NONE 0
256 8d1b399b 2021-07-22 op #define AUTH_USER 1
257 8d1b399b 2021-07-22 op #define AUTH_DONE 2
260 8d1b399b 2021-07-22 op client_dispatch_listener(int fd, short event, void *d)
262 8d1b399b 2021-07-22 op static int auth = AUTH_NONE;
263 8d1b399b 2021-07-22 op static char username[64] = {0};
264 8d1b399b 2021-07-22 op static char dir[PATH_MAX] = {0};
265 8d1b399b 2021-07-22 op struct imsg imsg;
266 8d1b399b 2021-07-22 op struct imsgev *iev = d;
267 8d1b399b 2021-07-22 op struct imsgbuf *ibuf;
269 8d1b399b 2021-07-22 op int shut = 0;
271 8d1b399b 2021-07-22 op ibuf = &iev->ibuf;
273 8d1b399b 2021-07-22 op if (event & EV_READ) {
274 8d1b399b 2021-07-22 op if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
275 8d1b399b 2021-07-22 op fatal("imsg_read error");
276 8d1b399b 2021-07-22 op if (n == 0) /* Connection closed */
279 8d1b399b 2021-07-22 op if (event & EV_WRITE) {
280 8d1b399b 2021-07-22 op if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
281 8d1b399b 2021-07-22 op fatal("msgbuf_write");
282 8d1b399b 2021-07-22 op if (n == 0) /* Connection closed */
287 8d1b399b 2021-07-22 op if ((n = imsg_get(ibuf, &imsg)) == -1)
288 8d1b399b 2021-07-22 op fatal("%s: imsg_get error", __func__);
289 8d1b399b 2021-07-22 op if (n == 0) /* No more messages. */
292 8d1b399b 2021-07-22 op switch (imsg.hdr.type) {
293 8d1b399b 2021-07-22 op case IMSG_AUTH:
294 2ef72ade 2021-07-28 op peerid = imsg.hdr.peerid;
296 8d1b399b 2021-07-22 op fatalx("%s: IMSG_AUTH already done", __func__);
297 8d1b399b 2021-07-22 op auth = AUTH_USER;
298 8d1b399b 2021-07-22 op ((char *)imsg.data)[IMSG_DATA_SIZE(imsg)-1] = '\0';
299 8d1b399b 2021-07-22 op strlcpy(username, imsg.data, sizeof(username));
301 8d1b399b 2021-07-22 op case IMSG_AUTH_DIR:
302 8d1b399b 2021-07-22 op if (auth != AUTH_USER)
303 8d1b399b 2021-07-22 op fatalx("%s: IMSG_AUTH_DIR not after IMSG_AUTH",
305 8d1b399b 2021-07-22 op auth = AUTH_DONE;
306 8d1b399b 2021-07-22 op ((char *)imsg.data)[IMSG_DATA_SIZE(imsg)-1] = '\0';
307 8d1b399b 2021-07-22 op strlcpy(dir, imsg.data, sizeof(dir));
308 8d1b399b 2021-07-22 op client_privdrop(username, dir);
309 8d1b399b 2021-07-22 op memset(username, 0, sizeof(username));
310 8d1b399b 2021-07-22 op memset(dir, 0, sizeof(username));
312 8d1b399b 2021-07-22 op case IMSG_BUF:
315 23e03c88 2021-07-26 op fatalx("%s: can't handle messages before"
316 23e03c88 2021-07-26 op " doing the auth", __func__);
317 23e03c88 2021-07-26 op handle_message(&imsg, IMSG_DATA_SIZE(imsg));
319 8d1b399b 2021-07-22 op case IMSG_CONN_GONE:
320 8d1b399b 2021-07-22 op log_debug("closing");
324 8d1b399b 2021-07-22 op log_debug("%s: unexpected imsg %d",
325 8d1b399b 2021-07-22 op __func__, imsg.hdr.type);
328 8d1b399b 2021-07-22 op imsg_free(&imsg);
332 8d1b399b 2021-07-22 op imsg_event_add(iev);
334 8d1b399b 2021-07-22 op /* This pipe is dead. Remove its event handler. */
335 8d1b399b 2021-07-22 op event_del(&iev->ev);
336 223c9e73 2021-08-06 op log_debug("pipe closed, shutting down...");
337 8d1b399b 2021-07-22 op event_loopexit(NULL);
342 8d1b399b 2021-07-22 op client_privdrop(const char *username, const char *dir)
344 8d1b399b 2021-07-22 op struct passwd *pw;
346 8d1b399b 2021-07-22 op setproctitle("client %s", username);
348 8d1b399b 2021-07-22 op if ((pw = getpwnam(username)) == NULL)
349 8d1b399b 2021-07-22 op fatalx("getpwnam(%s) failed", username);
351 8d1b399b 2021-07-22 op if (chroot(dir) == -1)
352 8d1b399b 2021-07-22 op fatal("chroot");
353 8d1b399b 2021-07-22 op if (chdir("/") == -1)
354 8d1b399b 2021-07-22 op fatal("chdir(\"/\")");
356 8d1b399b 2021-07-22 op if (setgroups(1, &pw->pw_gid) ||
357 8d1b399b 2021-07-22 op setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
358 8d1b399b 2021-07-22 op setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
359 8d1b399b 2021-07-22 op fatal("can't drop privileges");
361 8d1b399b 2021-07-22 op sandbox_client();
362 66a9a40a 2021-07-28 op log_debug("client ready; user=%s dir=%s", username, dir);
364 2ef72ade 2021-07-28 op if ((evb = evbuffer_new()) == NULL)
365 2ef72ade 2021-07-28 op fatal("evbuffer_new");
369 2ef72ade 2021-07-28 op client_send_listener(int type, const void *data, uint16_t len)
373 8d1b399b 2021-07-22 op if ((ret = imsg_compose(&iev_listener->ibuf, type, peerid, 0, -1,
374 8d1b399b 2021-07-22 op data, len)) != -1)
375 8d1b399b 2021-07-22 op imsg_event_add(iev_listener);
380 deb971f5 2021-08-12 op /* set qid fields from sb */
382 deb971f5 2021-08-12 op qid_update_from_sb(struct qid *qid, struct stat *sb)
384 deb971f5 2021-08-12 op qid->path = sb->st_ino;
387 1c08fc54 2021-08-01 op * Theoretically (and hopefully!) this should be a 64 bit
388 1c08fc54 2021-08-01 op * number. Unfortunately, 9P uses 32 bit timestamps.
390 deb971f5 2021-08-12 op qid->vers = sb->st_mtim.tv_sec;
392 deb971f5 2021-08-12 op if (S_ISREG(sb->st_mode))
393 1c08fc54 2021-08-01 op qid->type = QTFILE;
394 deb971f5 2021-08-12 op else if (S_ISDIR(sb->st_mode))
395 1c08fc54 2021-08-01 op qid->type = QTDIR;
396 deb971f5 2021-08-12 op else if (S_ISLNK(sb->st_mode))
397 1c08fc54 2021-08-01 op qid->type = QTSYMLINK;
400 c5d518da 2021-07-29 op /* creates a qid given a fd */
401 c7f4e1bd 2021-12-24 op static struct dir *
402 c7f4e1bd 2021-12-24 op new_dir(int fd)
404 c7f4e1bd 2021-12-24 op struct dir *dir;
406 c7f4e1bd 2021-12-24 op if ((dir = calloc(1, sizeof(*dir))) == NULL)
409 c7f4e1bd 2021-12-24 op dir->fd = fd;
410 c7f4e1bd 2021-12-24 op STAILQ_INSERT_HEAD(&dirs, dir, entries);
414 c7f4e1bd 2021-12-24 op static struct dir *
415 c7f4e1bd 2021-12-24 op dir_incref(struct dir *dir)
417 c7f4e1bd 2021-12-24 op dir->refcount++;
422 c7f4e1bd 2021-12-24 op dir_decref(struct dir *dir)
424 c7f4e1bd 2021-12-24 op if (--dir->refcount > 0)
427 c7f4e1bd 2021-12-24 op STAILQ_REMOVE(&dirs, dir, dir, entries);
429 c7f4e1bd 2021-12-24 op close(dir->fd);
433 c5d518da 2021-07-29 op static struct fid *
434 c7f4e1bd 2021-12-24 op new_fid(struct dir *dir, uint32_t fid, const char *path, struct qid *qid)
436 c7f4e1bd 2021-12-24 op struct fid *f;
437 c7f4e1bd 2021-12-24 op struct qid q;
438 c7f4e1bd 2021-12-24 op struct stat sb;
440 c7f4e1bd 2021-12-24 op if (qid == NULL) {
441 c7f4e1bd 2021-12-24 op if (fstatat(dir->fd, path, &sb, 0)) {
442 c7f4e1bd 2021-12-24 op log_warn("fstatat(%s)", path);
445 c7f4e1bd 2021-12-24 op qid_update_from_sb(&q, &sb);
449 c5d518da 2021-07-29 op if ((f = calloc(1, sizeof(*f))) == NULL)
452 c7f4e1bd 2021-12-24 op f->dir = dir_incref(dir);
453 c5d518da 2021-07-29 op f->fid = fid;
456 c7f4e1bd 2021-12-24 op strlcpy(f->fpath, path, sizeof(f->fpath));
458 c7f4e1bd 2021-12-24 op memcpy(&f->qid, qid, sizeof(f->qid));
460 c5d518da 2021-07-29 op STAILQ_INSERT_HEAD(&fids, f, entries);
465 c377c1b9 2021-07-30 op static struct fid *
466 c377c1b9 2021-07-30 op fid_by_id(uint32_t fid)
468 c377c1b9 2021-07-30 op struct fid *f;
470 c377c1b9 2021-07-30 op STAILQ_FOREACH(f, &fids, entries) {
471 c377c1b9 2021-07-30 op if (f->fid == fid)
479 c377c1b9 2021-07-30 op free_fid(struct fid *f)
483 021481ca 2021-12-13 op if (f->fd != -1) {
484 c7f4e1bd 2021-12-24 op if (f->d != NULL)
485 c7f4e1bd 2021-12-24 op r = closedir(f->d);
487 baa40504 2021-12-15 op r = close(f->fd);
490 baa40504 2021-12-15 op fatal("can't close fid %d", f->fid);
492 1e8d72fd 2021-12-15 op if (f->evb != NULL)
493 1e8d72fd 2021-12-15 op evbuffer_free(f->evb);
495 021481ca 2021-12-13 op /* try to honour ORCLOSE if requested */
496 021481ca 2021-12-13 op if (f->iomode & O_CLOEXEC)
497 c7f4e1bd 2021-12-24 op unlinkat(f->dir->fd, f->fpath, 0);
500 c7f4e1bd 2021-12-24 op dir_decref(f->dir);
502 c377c1b9 2021-07-30 op STAILQ_REMOVE(&fids, f, fid, entries);
507 48192874 2021-08-01 op parse_message(const uint8_t *data, size_t len, struct np_msg_header *hdr,
508 5c485996 2021-07-28 op uint8_t **cnt)
510 48192874 2021-08-01 op size_t olen = len;
512 48192874 2021-08-01 op if (!NPREAD32("len", &hdr->len, &data, &len) ||
513 48192874 2021-08-01 op !NPREAD8("type", &hdr->type, &data, &len) ||
514 48192874 2021-08-01 op !NPREAD16("tag", &hdr->tag, &data, &len))
517 48192874 2021-08-01 op if (olen != hdr->len)
520 23e03c88 2021-07-26 op if (hdr->type < Tversion ||
521 23e03c88 2021-07-26 op hdr->type >= Tmax ||
522 23e03c88 2021-07-26 op hdr->type == Terror ||
523 23e03c88 2021-07-26 op (hdr->type & 0x1) != 0) /* cannot recv a R* */
526 23e03c88 2021-07-26 op hdr->tag = le32toh(hdr->tag);
528 48192874 2021-08-01 op *cnt = (uint8_t *)data;
532 23e03c88 2021-07-26 op /* TODO: send a proper message to terminate the connection. */
533 23e03c88 2021-07-26 op fatalx("got invalid message");
537 5585f1c3 2021-12-16 op np_write16(struct evbuffer *e, uint16_t x)
539 83f6b305 2021-08-01 op x = htole16(x);
540 5585f1c3 2021-12-16 op evbuffer_add(e, &x, sizeof(x));
544 5585f1c3 2021-12-16 op np_write32(struct evbuffer *e, uint32_t x)
546 021481ca 2021-12-13 op x = htole32(x);
547 5585f1c3 2021-12-16 op evbuffer_add(e, &x, sizeof(x));
551 5585f1c3 2021-12-16 op np_write64(struct evbuffer *e, uint64_t x)
553 5585f1c3 2021-12-16 op x = htole64(x);
554 5585f1c3 2021-12-16 op evbuffer_add(e, &x, sizeof(x));
558 5585f1c3 2021-12-16 op np_writebuf(struct evbuffer *e, size_t len, void *data)
560 5585f1c3 2021-12-16 op evbuffer_add(e, data, len);
564 2ef72ade 2021-07-28 op np_header(uint32_t len, uint8_t type, uint16_t tag)
566 9ebb95a7 2021-07-30 op len += HEADERSIZE;
568 63f681aa 2021-07-28 op len = htole32(len);
569 63f681aa 2021-07-28 op tag = htole16(tag);
571 2ef72ade 2021-07-28 op evbuffer_add(evb, &len, sizeof(len));
572 2ef72ade 2021-07-28 op evbuffer_add(evb, &type, sizeof(type));
573 2ef72ade 2021-07-28 op evbuffer_add(evb, &tag, sizeof(tag));
577 5585f1c3 2021-12-16 op np_string(struct evbuffer *e, uint16_t len, const char *str)
579 5c485996 2021-07-28 op uint16_t l = len;
581 5c485996 2021-07-28 op len = htole16(len);
582 5585f1c3 2021-12-16 op evbuffer_add(e, &len, sizeof(len));
583 5585f1c3 2021-12-16 op evbuffer_add(e, str, l);
587 5585f1c3 2021-12-16 op np_qid(struct evbuffer *e, struct qid *qid)
589 c5d518da 2021-07-29 op uint64_t path;
590 c5d518da 2021-07-29 op uint32_t vers;
592 c5d518da 2021-07-29 op path = htole64(qid->path);
593 c5d518da 2021-07-29 op vers = htole32(qid->vers);
595 5585f1c3 2021-12-16 op evbuffer_add(e, &qid->type, sizeof(qid->type));
596 5585f1c3 2021-12-16 op evbuffer_add(e, &vers, sizeof(vers));
597 5585f1c3 2021-12-16 op evbuffer_add(e, &path, sizeof(path));
601 2ef72ade 2021-07-28 op do_send(void)
606 2ef72ade 2021-07-28 op len = EVBUFFER_LENGTH(evb);
607 64c19d90 2021-07-30 op data = EVBUFFER_DATA(evb);
609 64c19d90 2021-07-30 op #if DEBUG_PACKETS
610 78b94752 2021-08-01 op hexdump("outgoing packet", data, len);
612 64c19d90 2021-07-30 op client_send_listener(IMSG_BUF, data, len);
613 2ef72ade 2021-07-28 op evbuffer_drain(evb, len);
617 5c485996 2021-07-28 op np_version(uint16_t tag, uint32_t msize, const char *version)
621 5c485996 2021-07-28 op l = strlen(version);
623 5c485996 2021-07-28 op msize = htole32(msize);
625 9ebb95a7 2021-07-30 op np_header(sizeof(msize) + sizeof(l) + l, Rversion, tag);
626 5c485996 2021-07-28 op evbuffer_add(evb, &msize, sizeof(msize));
627 5585f1c3 2021-12-16 op np_string(evb, l, version);
632 c5d518da 2021-07-29 op np_attach(uint16_t tag, struct qid *qid)
634 9ebb95a7 2021-07-30 op np_header(QIDSIZE, Rattach, tag);
635 5585f1c3 2021-12-16 op np_qid(evb, qid);
640 c377c1b9 2021-07-30 op np_clunk(uint16_t tag)
642 9ebb95a7 2021-07-30 op np_header(0, Rclunk, tag);
647 36b30273 2021-07-30 op np_flush(uint16_t tag)
649 9ebb95a7 2021-07-30 op np_header(0, Rflush, tag);
654 1c08fc54 2021-08-01 op np_walk(uint16_t tag, int nwqid, struct qid *wqid)
658 83f6b305 2021-08-01 op /* two bytes for the counter */
659 83f6b305 2021-08-01 op np_header(2 + QIDSIZE * nwqid, Rwalk, tag);
660 5585f1c3 2021-12-16 op np_write16(evb, nwqid);
661 1c08fc54 2021-08-01 op for (i = 0; i < nwqid; ++i)
662 5585f1c3 2021-12-16 op np_qid(evb, wqid + i);
668 021481ca 2021-12-13 op np_open(uint16_t tag, struct qid *qid, uint32_t iounit)
670 021481ca 2021-12-13 op np_header(QIDSIZE + sizeof(iounit), Ropen, tag);
671 3162e55b 2021-12-20 op np_qid(evb, qid);
672 3162e55b 2021-12-20 op np_write32(evb, iounit);
677 3162e55b 2021-12-20 op np_create(uint16_t tag, struct qid *qid, uint32_t iounit)
679 3162e55b 2021-12-20 op np_header(QIDSIZE + sizeof(iounit), Rcreate, tag);
680 5585f1c3 2021-12-16 op np_qid(evb, qid);
681 5585f1c3 2021-12-16 op np_write32(evb, iounit);
686 baa40504 2021-12-15 op np_read(uint16_t tag, uint32_t count, void *data)
688 baa40504 2021-12-15 op np_header(sizeof(count) + count, Rread, tag);
689 5585f1c3 2021-12-16 op np_write32(evb, count);
690 5585f1c3 2021-12-16 op np_writebuf(evb, count, data);
695 2483be55 2021-12-16 op np_write(uint16_t tag, uint32_t count)
697 2483be55 2021-12-16 op np_header(sizeof(count), Rwrite, tag);
698 2483be55 2021-12-16 op np_write32(evb, count);
703 3a2c53f5 2021-12-18 op np_stat(uint16_t tag, uint32_t count, void *data)
705 3a2c53f5 2021-12-18 op np_header(count, Rstat, tag);
706 3a2c53f5 2021-12-18 op np_writebuf(evb, count, data);
711 0592b956 2021-12-20 cage np_remove(uint16_t tag)
713 0592b956 2021-12-20 cage np_header(0, Rremove, tag);
717 0592b956 2021-12-20 cage static void
718 2ef72ade 2021-07-28 op np_error(uint16_t tag, const char *errstr)
722 2ef72ade 2021-07-28 op l = strlen(errstr);
724 9ebb95a7 2021-07-30 op np_header(sizeof(l) + l, Rerror, tag);
725 5585f1c3 2021-12-16 op np_string(evb, l, errstr);
730 c5d518da 2021-07-29 op np_errno(uint16_t tag)
732 9b088310 2021-07-30 op int saved_errno;
733 c5d518da 2021-07-29 op char buf[64];
735 9b088310 2021-07-30 op saved_errno = errno;
737 c5d518da 2021-07-29 op strerror_r(errno, buf, sizeof(buf));
738 c5d518da 2021-07-29 op np_error(tag, buf);
740 9b088310 2021-07-30 op errno = saved_errno;
744 48192874 2021-08-01 op np_read8(const char *t, const char *f, uint8_t *dst, const uint8_t **src,
747 48192874 2021-08-01 op if (*len < sizeof(*dst)) {
748 48192874 2021-08-01 op log_warnx("%s: wanted %zu bytes for the %s field but only "
749 48192874 2021-08-01 op "%zu are available.", t, sizeof(*dst), f, *len);
753 48192874 2021-08-01 op memcpy(dst, *src, sizeof(*dst));
754 48192874 2021-08-01 op *src += sizeof(*dst);
755 48192874 2021-08-01 op *len -= sizeof(*dst);
761 48192874 2021-08-01 op np_read16(const char *t, const char *f, uint16_t *dst, const uint8_t **src,
764 48192874 2021-08-01 op if (*len < sizeof(*dst)) {
765 48192874 2021-08-01 op log_warnx("%s: wanted %zu bytes for the %s field but only "
766 48192874 2021-08-01 op "%zu are available.", t, sizeof(*dst), f, *len);
770 48192874 2021-08-01 op memcpy(dst, *src, sizeof(*dst));
771 48192874 2021-08-01 op *src += sizeof(*dst);
772 48192874 2021-08-01 op *len -= sizeof(*dst);
773 48192874 2021-08-01 op *dst = le16toh(*dst);
779 48192874 2021-08-01 op np_read32(const char *t, const char *f, uint32_t *dst, const uint8_t **src,
782 48192874 2021-08-01 op if (*len < sizeof(*dst)) {
783 48192874 2021-08-01 op log_warnx("%s: wanted %zu bytes for the %s field but only "
784 48192874 2021-08-01 op "%zu are available.", t, sizeof(*dst), f, *len);
788 48192874 2021-08-01 op memcpy(dst, *src, sizeof(*dst));
789 48192874 2021-08-01 op *src += sizeof(*dst);
790 48192874 2021-08-01 op *len -= sizeof(*dst);
791 48192874 2021-08-01 op *dst = le32toh(*dst);
797 baa40504 2021-12-15 op np_read64(const char *t, const char *f, uint64_t *dst, const uint8_t **src,
800 baa40504 2021-12-15 op if (*len < sizeof(*dst)) {
801 baa40504 2021-12-15 op log_warnx("%s: wanted %zu bytes for the %s field but only "
802 baa40504 2021-12-15 op "%zu are available.", t, sizeof(*dst), f, *len);
806 baa40504 2021-12-15 op memcpy(dst, *src, sizeof(*dst));
807 baa40504 2021-12-15 op *src += sizeof(*dst);
808 baa40504 2021-12-15 op *len -= sizeof(*dst);
809 baa40504 2021-12-15 op *dst = le64toh(*dst);
815 48192874 2021-08-01 op np_readstr(const char *t, const char *f, char *res, size_t reslen,
816 48192874 2021-08-01 op const uint8_t **src, size_t *len)
819 48192874 2021-08-01 op char buf[32];
821 48192874 2021-08-01 op strlcpy(buf, f, sizeof(buf));
822 48192874 2021-08-01 op strlcat(buf, "-len", sizeof(buf));
824 48192874 2021-08-01 op if (!np_read16(t, buf, &sl, src, len))
825 48192874 2021-08-01 op return READSTRERR;
827 48192874 2021-08-01 op if (*len < sl) {
828 48192874 2021-08-01 op log_warnx("%s: wanted %d bytes for the %s field but only "
829 48192874 2021-08-01 op "%zu are available.", t, sl, f, *len);
830 48192874 2021-08-01 op return READSTRERR;
833 48192874 2021-08-01 op if (*len > reslen-1)
834 48192874 2021-08-01 op return READSTRTRUNC;
836 48192874 2021-08-01 op memcpy(res, *src, sl);
837 48192874 2021-08-01 op res[sl] = '\0';
845 e60f4e08 2021-07-30 op tversion(struct np_msg_header *hdr, const uint8_t *data, size_t len)
847 48192874 2021-08-01 op char *dot, version[32];
849 e60f4e08 2021-07-30 op if (handshaked)
852 3d20424d 2021-08-01 op /* msize[4] version[s] */
853 48192874 2021-08-01 op if (!NPREAD32("msize", &msize, &data, &len))
856 48192874 2021-08-01 op switch (NPREADSTR("version", version, sizeof(version), &data, &len)) {
857 48192874 2021-08-01 op case READSTRERR:
859 48192874 2021-08-01 op case READSTRTRUNC:
860 48192874 2021-08-01 op log_warnx("9P version string too long, truncated");
861 48192874 2021-08-01 op goto mismatch;
864 48192874 2021-08-01 op if ((dot = strchr(version, '.')) != NULL)
867 48192874 2021-08-01 op if (strcmp(version, VERSION9P) != 0 ||
869 48192874 2021-08-01 op goto mismatch;
871 48192874 2021-08-01 op /* version matched */
872 48192874 2021-08-01 op handshaked = 1;
873 93c0742a 2021-12-27 op msize = MIN(msize, CLIENT_MSIZE);
874 e60f4e08 2021-07-30 op client_send_listener(IMSG_MSIZE, &msize, sizeof(msize));
875 e60f4e08 2021-07-30 op np_version(hdr->tag, msize, VERSION9P);
879 48192874 2021-08-01 op log_warnx("unknown 9P version string: \"%s\", want "VERSION9P,
881 48192874 2021-08-01 op np_version(hdr->tag, MSIZE9P, "unknown");
885 e60f4e08 2021-07-30 op client_send_listener(IMSG_CLOSE, NULL, 0);
886 e60f4e08 2021-07-30 op client_shutdown();
890 e60f4e08 2021-07-30 op tattach(struct np_msg_header *hdr, const uint8_t *data, size_t len)
892 c7f4e1bd 2021-12-24 op struct dir *dir;
893 e60f4e08 2021-07-30 op struct fid *f;
894 48192874 2021-08-01 op uint32_t fid, afid;
896 014adfc2 2021-07-30 op char aname[PATH_MAX];
898 e60f4e08 2021-07-30 op /* fid[4] afid[4] uname[s] aname[s] */
900 48192874 2021-08-01 op if (!NPREAD32("fid", &fid, &data, &len) ||
901 48192874 2021-08-01 op !NPREAD32("afid", &afid, &data, &len))
904 48192874 2021-08-01 op /* read the uname but don't actually use it */
905 48192874 2021-08-01 op switch (NPREADSTR("uname", aname, sizeof(aname), &data, &len)) {
906 48192874 2021-08-01 op case READSTRERR:
908 48192874 2021-08-01 op case READSTRTRUNC:
909 48192874 2021-08-01 op np_error(hdr->tag, "name too long");
913 48192874 2021-08-01 op switch (NPREADSTR("aname", aname, sizeof(aname), &data, &len)) {
914 48192874 2021-08-01 op case READSTRERR:
916 48192874 2021-08-01 op case READSTRTRUNC:
917 014adfc2 2021-07-30 op np_error(hdr->tag, "name too long");
921 cf758c33 2021-12-14 op if (fid_by_id(fid) != NULL || afid != NOFID) {
922 cf758c33 2021-12-14 op np_error(hdr->tag, "invalid fid or afid");
926 deb971f5 2021-08-12 op if ((fd = open(aname, O_RDONLY|O_DIRECTORY)) == -1)
929 c7f4e1bd 2021-12-24 op if ((dir = new_dir(fd)) == NULL)
932 cf758c33 2021-12-14 op log_debug("attached %s to %d", aname, fid);
934 c7f4e1bd 2021-12-24 op if ((f = new_fid(dir, fid, aname, NULL)) == NULL) {
935 c7f4e1bd 2021-12-24 op dir_decref(dir);
939 c7f4e1bd 2021-12-24 op np_attach(hdr->tag, &f->qid);
943 deb971f5 2021-08-12 op np_errno(hdr->tag);
944 deb971f5 2021-08-12 op log_warn("failed to attach %s", aname);
948 014adfc2 2021-07-30 op client_send_listener(IMSG_CLOSE, NULL, 0);
949 014adfc2 2021-07-30 op client_shutdown();
953 c377c1b9 2021-07-30 op tclunk(struct np_msg_header *hdr, const uint8_t *data, size_t len)
955 c377c1b9 2021-07-30 op struct fid *f;
956 c377c1b9 2021-07-30 op uint32_t fid;
959 48192874 2021-08-01 op if (!NPREAD32("fid", &fid, &data, &len)) {
960 c377c1b9 2021-07-30 op client_send_listener(IMSG_CLOSE, NULL, 0);
961 c377c1b9 2021-07-30 op client_shutdown();
965 c377c1b9 2021-07-30 op if ((f = fid_by_id(fid)) == NULL) {
966 c377c1b9 2021-07-30 op np_error(hdr->tag, "invalid fid");
971 c377c1b9 2021-07-30 op np_clunk(hdr->tag);
975 36b30273 2021-07-30 op tflush(struct np_msg_header *hdr, const uint8_t *data, size_t len)
977 36b30273 2021-07-30 op uint16_t oldtag;
980 36b30273 2021-07-30 op * We're doing only synchronous I/O. Tflush is implemented
981 36b30273 2021-07-30 op * only because it's illegal to reply with a Rerror.
984 36b30273 2021-07-30 op /* oldtag[2] */
985 36b30273 2021-07-30 op if (len != sizeof(oldtag)) {
986 0592b956 2021-12-20 cage log_warnx("Tflush with the wrong size: got %zu want %zu",
987 36b30273 2021-07-30 op len, sizeof(oldtag));
988 36b30273 2021-07-30 op client_send_listener(IMSG_CLOSE, NULL, 0);
989 36b30273 2021-07-30 op client_shutdown();
993 36b30273 2021-07-30 op np_flush(hdr->tag);
997 1c08fc54 2021-08-01 op twalk(struct np_msg_header *hdr, const uint8_t *data, size_t len)
999 deb971f5 2021-08-12 op struct stat sb;
1000 c7f4e1bd 2021-12-24 op struct dir *dir;
1001 c7f4e1bd 2021-12-24 op struct qid wqid[MAXWELEM] = {0};
1002 1c08fc54 2021-08-01 op struct fid *f, *nf;
1003 1c08fc54 2021-08-01 op uint32_t fid, newfid;
1004 7ddddcfe 2021-08-01 op uint16_t nwname;
1005 deb971f5 2021-08-12 op int fd, oldfd, no, nwqid = 0;
1006 20c188ce 2021-12-16 op char wnam[PATH_MAX];
1008 48192874 2021-08-01 op if (!NPREAD32("fid", &fid, &data, &len) ||
1009 48192874 2021-08-01 op !NPREAD32("newfid", &newfid, &data, &len) ||
1010 48192874 2021-08-01 op !NPREAD16("nwname", &nwname, &data, &len))
1013 1c08fc54 2021-08-01 op if (nwname > MAXWELEM) {
1014 1c08fc54 2021-08-01 op log_warnx("Twalk: more than %d path elements: %d",
1015 1c08fc54 2021-08-01 op MAXWELEM, nwname);
1019 1c08fc54 2021-08-01 op if ((f = fid_by_id(fid)) == NULL) {
1020 1c08fc54 2021-08-01 op np_error(hdr->tag, "invalid fid");
1024 99a43590 2021-12-14 op if (f->fd != -1) {
1025 47c9b8b8 2021-12-02 op np_error(hdr->tag, "fid already opened for I/O");
1029 1c08fc54 2021-08-01 op if (fid == newfid)
1031 1c08fc54 2021-08-01 op else if ((nf = fid_by_id(newfid)) != NULL) {
1032 1c08fc54 2021-08-01 op np_error(hdr->tag, "newfid already in use");
1037 913eba5c 2021-08-01 op /* special case: fid duplication */
1038 22dfb5a0 2021-08-01 op if (nwname == 0) {
1040 1c08fc54 2021-08-01 op * TODO: should we forbid fids duplication when fid ==
1043 1bf47a19 2021-12-23 op if (nf == NULL &&
1044 c7f4e1bd 2021-12-24 op (nf = new_fid(f->dir, newfid, f->fpath, &f->qid)) == NULL)
1045 f987557c 2021-12-02 op fatal("new_fid duplication");
1047 a6036d0f 2021-12-21 op np_walk(hdr->tag, 0, NULL);
1051 c7f4e1bd 2021-12-24 op if (!(f->qid.type & QTDIR)) {
1052 053e2652 2021-08-01 op np_error(hdr->tag, "fid doesn't represent a directory");
1056 c7f4e1bd 2021-12-24 op oldfd = f->dir->fd;
1058 1c08fc54 2021-08-01 op for (nwqid = 0; nwqid < nwname; nwqid++) {
1059 053e2652 2021-08-01 op switch (NPREADSTR("wname", wnam, sizeof(wnam), &data, &len)) {
1060 48192874 2021-08-01 op case READSTRERR:
1062 48192874 2021-08-01 op case READSTRTRUNC:
1063 1c08fc54 2021-08-01 op np_error(hdr->tag, "wname too long");
1067 2532a087 2021-12-20 cage if (*wnam == '\0' ||
1068 054cd6b9 2021-12-14 op strchr(wnam, '/') != NULL ||
1069 054cd6b9 2021-12-14 op !strcmp(wnam, ".")) {
1070 054cd6b9 2021-12-14 op errno = EINVAL;
1071 054cd6b9 2021-12-14 op goto cantopen;
1074 deb971f5 2021-08-12 op if ((fd = openat(oldfd, wnam, O_RDONLY|O_DIRECTORY)) == -1 &&
1075 3242c0bc 2021-12-14 op errno != ENOTDIR)
1076 deb971f5 2021-08-12 op goto cantopen;
1078 deb971f5 2021-08-12 op if ((fd == -1 && fstatat(oldfd, wnam, &sb, 0) == -1) ||
1079 3242c0bc 2021-12-14 op (fd != -1 && fstat(fd, &sb) == -1))
1080 deb971f5 2021-08-12 op goto cantopen;
1082 deb971f5 2021-08-12 op qid_update_from_sb(&wqid[nwqid], &sb);
1084 deb971f5 2021-08-12 op /* reached a file but we still have other components */
1085 deb971f5 2021-08-12 op if (fd == -1 && nwqid+1 < nwname)
1086 deb971f5 2021-08-12 op goto cantopen;
1088 deb971f5 2021-08-12 op /* reached the end and found a file */
1089 deb971f5 2021-08-12 op if (fd == -1 && nwqid+1 == nwname)
1092 c7f4e1bd 2021-12-24 op if (oldfd != f->dir->fd)
1093 deb971f5 2021-08-12 op close(oldfd);
1098 1bf47a19 2021-12-23 op * If fd is -1 we've reached a file, otherwise we've just
1099 1bf47a19 2021-12-23 op * reached another directory. We must pay attention to what
1100 c7f4e1bd 2021-12-24 op * file descriptor we use to create the dir, because if we've
1101 c7f4e1bd 2021-12-24 op * reached a file and oldfd is f->dir->fd then we *must* share
1102 c7f4e1bd 2021-12-24 op * the same dir (it was a walk of one path from a directory to a
1103 1bf47a19 2021-12-23 op * file, otherwise fun is bound to happen as soon as the client
1104 1bf47a19 2021-12-23 op * closes the fid for the directory but keeps the one for the
1107 c7f4e1bd 2021-12-24 op if (fd == -1 && oldfd == f->dir->fd)
1108 c7f4e1bd 2021-12-24 op dir = f->dir;
1109 1bf47a19 2021-12-23 op else if (fd == -1)
1110 c7f4e1bd 2021-12-24 op dir = new_dir(oldfd);
1112 c7f4e1bd 2021-12-24 op dir = new_dir(fd);
1114 c7f4e1bd 2021-12-24 op if (dir == NULL)
1115 c7f4e1bd 2021-12-24 op fatal("new_dir");
1117 1c08fc54 2021-08-01 op if (nf == NULL) {
1118 c7f4e1bd 2021-12-24 op if ((nf = new_fid(dir, newfid, wnam, &wqid[nwqid-1])) == NULL)
1119 1bf47a19 2021-12-23 op fatal("new fid");
1121 c7f4e1bd 2021-12-24 op /* update the dir */
1122 c7f4e1bd 2021-12-24 op dir_decref(nf->dir);
1123 c7f4e1bd 2021-12-24 op nf->dir = dir_incref(dir);
1126 1c08fc54 2021-08-01 op np_walk(hdr->tag, nwqid, wqid);
1130 c7f4e1bd 2021-12-24 op if (oldfd != f->dir->fd)
1131 deb971f5 2021-08-12 op close(oldfd);
1133 deb971f5 2021-08-12 op if (nwqid == 0)
1134 deb971f5 2021-08-12 op np_error(hdr->tag, strerror(no));
1136 deb971f5 2021-08-12 op np_walk(hdr->tag, nwqid, wqid);
1140 1c08fc54 2021-08-01 op client_send_listener(IMSG_CLOSE, NULL, 0);
1141 1c08fc54 2021-08-01 op client_shutdown();
1144 021481ca 2021-12-13 op static inline int
1145 021481ca 2021-12-13 op npmode_to_unix(uint8_t mode, int *flags)
1147 021481ca 2021-12-13 op switch (mode & 0x0F) {
1148 021481ca 2021-12-13 op case KOREAD:
1149 021481ca 2021-12-13 op *flags = O_RDONLY;
1151 021481ca 2021-12-13 op case KOWRITE:
1152 021481ca 2021-12-13 op *flags = O_WRONLY;
1154 021481ca 2021-12-13 op case KORDWR:
1155 021481ca 2021-12-13 op *flags = O_RDWR;
1157 021481ca 2021-12-13 op case KOEXEC:
1158 021481ca 2021-12-13 op log_warnx("tried to open something with KOEXEC");
1159 021481ca 2021-12-13 op /* fallthrough */
1164 021481ca 2021-12-13 op if (mode & KOTRUNC)
1165 021481ca 2021-12-13 op *flags |= O_TRUNC;
1166 021481ca 2021-12-13 op if (mode & KORCLOSE)
1167 021481ca 2021-12-13 op *flags |= O_CLOEXEC;
1173 021481ca 2021-12-13 op topen(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1175 021481ca 2021-12-13 op struct stat sb;
1176 021481ca 2021-12-13 op struct qid qid;
1177 021481ca 2021-12-13 op struct fid *f;
1178 021481ca 2021-12-13 op uint32_t fid;
1179 021481ca 2021-12-13 op uint8_t mode;
1180 87a818e8 2021-12-14 op const char *path;
1182 021481ca 2021-12-13 op /* fid[4] mode[1] */
1183 021481ca 2021-12-13 op if (!NPREAD32("fid", &fid, &data, &len) ||
1184 021481ca 2021-12-13 op !NPREAD8("mode", &mode, &data, &len)) {
1185 021481ca 2021-12-13 op client_send_listener(IMSG_CLOSE, NULL, 0);
1186 021481ca 2021-12-13 op client_shutdown();
1190 021481ca 2021-12-13 op if ((f = fid_by_id(fid)) == NULL || f->fd != -1) {
1191 021481ca 2021-12-13 op np_error(hdr->tag, "invalid fid");
1195 d4e43815 2021-12-15 op if (npmode_to_unix(mode, &f->iomode) == -1) {
1196 d4e43815 2021-12-15 op np_error(hdr->tag, "invalid mode");
1200 1bf47a19 2021-12-23 op path = f->fpath;
1201 c7f4e1bd 2021-12-24 op if (f->qid.type & QTDIR)
1204 c7f4e1bd 2021-12-24 op if ((f->fd = openat(f->dir->fd, path, f->iomode)) == -1) {
1205 021481ca 2021-12-13 op np_error(hdr->tag, strerror(errno));
1209 021481ca 2021-12-13 op if (fstat(f->fd, &sb) == -1)
1210 021481ca 2021-12-13 op fatal("fstat");
1212 baa40504 2021-12-15 op if (S_ISDIR(sb.st_mode)) {
1213 c7f4e1bd 2021-12-24 op if ((f->d = fdopendir(f->fd)) == NULL) {
1214 baa40504 2021-12-15 op np_errno(hdr->tag);
1215 baa40504 2021-12-15 op close(f->fd);
1220 c0ae57b9 2021-12-15 op if ((f->evb = evbuffer_new()) == NULL) {
1221 c0ae57b9 2021-12-15 op np_errno(hdr->tag);
1222 c7f4e1bd 2021-12-24 op closedir(f->d);
1223 c7f4e1bd 2021-12-24 op f->d = NULL;
1228 baa40504 2021-12-15 op f->offset = 0;
1230 021481ca 2021-12-13 op qid_update_from_sb(&qid, &sb);
1231 021481ca 2021-12-13 op np_open(hdr->tag, &qid, sb.st_blksize);
1235 3162e55b 2021-12-20 op tcreate(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1237 3162e55b 2021-12-20 op struct stat sb;
1238 3162e55b 2021-12-20 op struct qid qid;
1239 3162e55b 2021-12-20 op struct fid *f;
1240 3162e55b 2021-12-20 op uint32_t fid, perm;
1241 3162e55b 2021-12-20 op uint8_t mode;
1242 3162e55b 2021-12-20 op char name[PATH_MAX];
1244 3162e55b 2021-12-20 op /* fid[4] name[s] perm[4] mode[1] */
1245 3162e55b 2021-12-20 op if (!NPREAD32("fid", &fid, &data, &len))
1247 3162e55b 2021-12-20 op switch (NPREADSTR("name", name, sizeof(name), &data, &len)) {
1248 3162e55b 2021-12-20 op case READSTRERR:
1250 3162e55b 2021-12-20 op case READSTRTRUNC:
1251 3162e55b 2021-12-20 op np_error(hdr->tag, "name too long");
1254 3162e55b 2021-12-20 op if (!NPREAD32("perm", &perm, &data, &len) ||
1255 3162e55b 2021-12-20 op !NPREAD8("mode", &mode, &data, &len))
1258 5547a835 2021-12-20 op if (!strcmp(name, ".") || !strcmp(name, "..") ||
1259 5547a835 2021-12-20 op strchr(name, '/') != NULL) {
1260 5547a835 2021-12-20 op np_error(hdr->tag, "invalid name");
1264 3162e55b 2021-12-20 op if ((f = fid_by_id(fid)) == NULL || f->fd != -1) {
1265 3162e55b 2021-12-20 op np_error(hdr->tag, "invalid fid");
1269 c7f4e1bd 2021-12-24 op if (!(f->qid.type & QTDIR)) {
1270 3162e55b 2021-12-20 op np_error(hdr->tag, "fid doesn't identify a directory");
1274 3162e55b 2021-12-20 op if (npmode_to_unix(mode, &f->iomode) == -1) {
1275 3162e55b 2021-12-20 op np_error(hdr->tag, "invalid mode");
1279 3162e55b 2021-12-20 op if (f->iomode & O_RDONLY) {
1280 3162e55b 2021-12-20 op np_error(hdr->tag, "can't create a read-only file");
1284 3162e55b 2021-12-20 op /* TODO: parse the mode */
1286 3162e55b 2021-12-20 op if (perm & 0x80000000) {
1287 3162e55b 2021-12-20 op /* create a directory */
1288 c7f4e1bd 2021-12-24 op f->fd = mkdirat(f->dir->fd, name, 0755);
1290 3162e55b 2021-12-20 op /* create a file */
1291 c7f4e1bd 2021-12-24 op f->fd = openat(f->dir->fd, name, f->iomode | O_CREAT | O_TRUNC,
1295 3162e55b 2021-12-20 op if (f->fd == -1) {
1296 3162e55b 2021-12-20 op np_errno(hdr->tag);
1300 3162e55b 2021-12-20 op if (fstat(f->fd, &sb) == -1)
1301 3162e55b 2021-12-20 op fatal("fstat");
1303 3162e55b 2021-12-20 op if (S_ISDIR(sb.st_mode)) {
1304 c7f4e1bd 2021-12-24 op if ((f->d = fdopendir(f->fd)) == NULL) {
1305 3162e55b 2021-12-20 op np_errno(hdr->tag);
1306 3162e55b 2021-12-20 op close(f->fd);
1311 3162e55b 2021-12-20 op if ((f->evb = evbuffer_new()) == NULL) {
1312 3162e55b 2021-12-20 op np_errno(hdr->tag);
1313 c7f4e1bd 2021-12-24 op closedir(f->d);
1314 c7f4e1bd 2021-12-24 op f->d = NULL;
1319 3162e55b 2021-12-20 op f->offset = 0;
1321 3162e55b 2021-12-20 op qid_update_from_sb(&qid, &sb);
1322 3162e55b 2021-12-20 op np_create(hdr->tag, &qid, sb.st_blksize);
1327 3162e55b 2021-12-20 op client_send_listener(IMSG_CLOSE, NULL, 0);
1328 3162e55b 2021-12-20 op client_shutdown();
1331 5585f1c3 2021-12-16 op static inline void
1332 3a2c53f5 2021-12-18 op serialize_stat(const char *fname, struct stat *sb, struct evbuffer *evb)
1334 5585f1c3 2021-12-16 op struct qid qid;
1335 5585f1c3 2021-12-16 op const char *uid, *gid, *muid;
1337 5585f1c3 2021-12-16 op uint16_t namlen, uidlen, gidlen, ulen;
1339 3a2c53f5 2021-12-18 op qid_update_from_sb(&qid, sb);
1341 5585f1c3 2021-12-16 op /* TODO: fill these fields */
1346 3a2c53f5 2021-12-18 op namlen = strlen(fname);
1347 5585f1c3 2021-12-16 op uidlen = strlen(uid);
1348 5585f1c3 2021-12-16 op gidlen = strlen(gid);
1349 5585f1c3 2021-12-16 op ulen = strlen(muid);
1351 5585f1c3 2021-12-16 op tot = NPSTATSIZ(namlen, uidlen, gidlen, ulen);
1352 5585f1c3 2021-12-16 op if (tot > UINT32_MAX) {
1353 5585f1c3 2021-12-16 op log_warnx("stat info for dir entry %s would overflow",
1358 5585f1c3 2021-12-16 op np_write16(evb, tot); /* size[2] */
1359 3a2c53f5 2021-12-18 op np_write16(evb, sb->st_rdev); /* type[2] */
1360 3a2c53f5 2021-12-18 op np_write32(evb, sb->st_dev); /* dev[4] */
1361 5585f1c3 2021-12-16 op np_qid(evb, &qid); /* qid[13] */
1363 5585f1c3 2021-12-16 op /* XXX: translate? */
1364 3a2c53f5 2021-12-18 op np_write32(evb, sb->st_mode); /* mode[4] */
1366 3a2c53f5 2021-12-18 op np_write32(evb, sb->st_atim.tv_sec); /* atime[4] */
1367 3a2c53f5 2021-12-18 op np_write32(evb, sb->st_mtim.tv_sec); /* mtime[4] */
1368 3a2c53f5 2021-12-18 op np_write64(evb, sb->st_size); /* length[8] */
1369 3a2c53f5 2021-12-18 op np_string(evb, namlen, fname); /* name[s] */
1370 5585f1c3 2021-12-16 op np_string(evb, uidlen, uid); /* uid[s] */
1371 5585f1c3 2021-12-16 op np_string(evb, gidlen, gid); /* gid[s] */
1372 5585f1c3 2021-12-16 op np_string(evb, ulen, muid); /* muid[s] */
1376 baa40504 2021-12-15 op tread(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1378 baa40504 2021-12-15 op struct fid *f;
1380 baa40504 2021-12-15 op uint64_t off;
1381 baa40504 2021-12-15 op uint32_t fid, count;
1382 baa40504 2021-12-15 op char buf[2048];
1384 baa40504 2021-12-15 op /* fid[4] offset[8] count[4] */
1385 baa40504 2021-12-15 op if (!NPREAD32("fid", &fid, &data, &len) ||
1386 baa40504 2021-12-15 op !NPREAD64("offset", &off, &data, &len) ||
1387 baa40504 2021-12-15 op !NPREAD32("count", &count, &data, &len)) {
1388 baa40504 2021-12-15 op client_send_listener(IMSG_CLOSE, NULL, 0);
1389 baa40504 2021-12-15 op client_shutdown();
1393 5f362b7f 2021-12-15 op if ((f = fid_by_id(fid)) == NULL || f->fd == -1) {
1394 baa40504 2021-12-15 op np_error(hdr->tag, "invalid fid");
1398 baa40504 2021-12-15 op if (TYPE_OVERFLOW(off_t, off)) {
1399 2ef8253c 2021-12-27 op log_warnx("unexpected off_t size");
1400 baa40504 2021-12-15 op np_error(hdr->tag, "invalid offset");
1404 c7f4e1bd 2021-12-24 op if (f->d == NULL) {
1405 baa40504 2021-12-15 op /* read a file */
1406 baa40504 2021-12-15 op r = pread(f->fd, buf, sizeof(buf), (off_t)off);
1407 baa40504 2021-12-15 op if (r == -1)
1408 baa40504 2021-12-15 op np_errno(hdr->tag);
1410 baa40504 2021-12-15 op np_read(hdr->tag, r, buf);
1412 fcf8ca15 2021-12-16 op if (off == 0 && f->offset != 0) {
1413 c7f4e1bd 2021-12-24 op rewinddir(f->d);
1414 fcf8ca15 2021-12-16 op f->offset = 0;
1417 5585f1c3 2021-12-16 op if (off != f->offset) {
1418 5585f1c3 2021-12-16 op np_error(hdr->tag, "can't seek in directories");
1422 5585f1c3 2021-12-16 op while (EVBUFFER_LENGTH(f->evb) < count) {
1423 5585f1c3 2021-12-16 op struct dirent *d;
1424 3a2c53f5 2021-12-18 op struct stat sb;
1426 c7f4e1bd 2021-12-24 op if ((d = readdir(f->d)) == NULL)
1428 3a2c53f5 2021-12-18 op if (fstatat(f->fd, d->d_name, &sb, 0) == -1) {
1429 3a2c53f5 2021-12-18 op warn("fstatat");
1432 3a2c53f5 2021-12-18 op serialize_stat(d->d_name, &sb, f->evb);
1435 5585f1c3 2021-12-16 op count = MIN(count, EVBUFFER_LENGTH(f->evb));
1436 5585f1c3 2021-12-16 op np_read(hdr->tag, count, EVBUFFER_DATA(f->evb));
1437 5585f1c3 2021-12-16 op evbuffer_drain(f->evb, count);
1439 5585f1c3 2021-12-16 op f->offset += count;
1444 2483be55 2021-12-16 op twrite(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1446 2483be55 2021-12-16 op struct fid *f;
1448 2483be55 2021-12-16 op uint64_t off;
1449 2483be55 2021-12-16 op uint32_t fid, count;
1451 2483be55 2021-12-16 op /* fid[4] offset[8] count[4] data[count] */
1452 2483be55 2021-12-16 op if (!NPREAD32("fid", &fid, &data, &len) ||
1453 2483be55 2021-12-16 op !NPREAD64("off", &off, &data, &len) ||
1454 2483be55 2021-12-16 op !NPREAD32("count", &count, &data, &len) ||
1455 2483be55 2021-12-16 op len != count) {
1456 2483be55 2021-12-16 op client_send_listener(IMSG_CLOSE, NULL, 0);
1457 2483be55 2021-12-16 op client_shutdown();
1461 2483be55 2021-12-16 op if ((f = fid_by_id(fid)) == NULL || f->fd == -1) {
1462 2483be55 2021-12-16 op np_error(hdr->tag, "invalid fid");
1466 2483be55 2021-12-16 op if (!(f->iomode & O_WRONLY) &&
1467 2483be55 2021-12-16 op !(f->iomode & O_RDWR)) {
1468 2483be55 2021-12-16 op np_error(hdr->tag, "fid not opened for writing");
1472 2483be55 2021-12-16 op if (TYPE_OVERFLOW(off_t, off)) {
1473 2483be55 2021-12-16 op log_warnx("unexpected off_t size");
1474 2483be55 2021-12-16 op np_error(hdr->tag, "invalid offset");
1478 6ace3b52 2021-12-17 op if ((r = pwrite(f->fd, data, len, off)) == -1)
1479 2483be55 2021-12-16 op np_errno(hdr->tag);
1481 2483be55 2021-12-16 op np_write(hdr->tag, r);
1485 3a2c53f5 2021-12-18 op tstat(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1487 3a2c53f5 2021-12-18 op struct evbuffer *evb;
1488 3a2c53f5 2021-12-18 op struct stat sb;
1489 3a2c53f5 2021-12-18 op struct fid *f;
1491 3a2c53f5 2021-12-18 op uint32_t fid;
1493 3a2c53f5 2021-12-18 op /* fid[4] */
1494 3a2c53f5 2021-12-18 op if (!NPREAD32("fid", &fid, &data, &len)) {
1495 3a2c53f5 2021-12-18 op client_send_listener(IMSG_CLOSE, NULL, 0);
1496 3a2c53f5 2021-12-18 op client_shutdown();
1501 3a2c53f5 2021-12-18 op * plan9' stat(9P) is not clear on whether the stat is allowed
1502 3a2c53f5 2021-12-18 op * on opened fids or not.
1504 3a2c53f5 2021-12-18 op if ((f = fid_by_id(fid)) == NULL) {
1505 3a2c53f5 2021-12-18 op np_error(hdr->tag, "invalid fid");
1509 3a2c53f5 2021-12-18 op if ((evb = evbuffer_new()) == NULL)
1510 3a2c53f5 2021-12-18 op fatal("evbuffer_new");
1512 1bf47a19 2021-12-23 op if (f->fd != -1)
1513 1bf47a19 2021-12-23 op r = fstat(f->fd, &sb);
1515 c7f4e1bd 2021-12-24 op r = fstatat(f->dir->fd, f->fpath, &sb, 0);
1517 3a2c53f5 2021-12-18 op if (r == -1) {
1518 3a2c53f5 2021-12-18 op np_errno(hdr->tag);
1519 3a2c53f5 2021-12-18 op evbuffer_free(evb);
1523 1bf47a19 2021-12-23 op serialize_stat(f->fpath, &sb, evb);
1524 3a2c53f5 2021-12-18 op np_stat(hdr->tag, EVBUFFER_LENGTH(evb), EVBUFFER_DATA(evb));
1525 3a2c53f5 2021-12-18 op evbuffer_free(evb);
1528 0592b956 2021-12-20 cage static void
1529 0592b956 2021-12-20 cage tremove(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1531 0592b956 2021-12-20 cage struct fid *f;
1532 0592b956 2021-12-20 cage uint32_t fid;
1534 946136f3 2021-12-26 op char dirpath[PATH_MAX + 3];
1536 0592b956 2021-12-20 cage /* fid[4] */
1537 0592b956 2021-12-20 cage if (!NPREAD32("fid", &fid, &data, &len)) {
1538 0592b956 2021-12-20 cage client_send_listener(IMSG_CLOSE, NULL, 0);
1539 0592b956 2021-12-20 cage client_shutdown();
1543 0592b956 2021-12-20 cage if ((f = fid_by_id(fid)) == NULL) {
1544 0592b956 2021-12-20 cage np_error(hdr->tag, "invalid fid");
1548 946136f3 2021-12-26 op if (f->qid.type & QTDIR) { /* directory */
1549 946136f3 2021-12-26 op strlcpy(dirpath, "../", sizeof(dirpath));
1550 946136f3 2021-12-26 op strlcat(dirpath, f->fpath, sizeof(dirpath));
1551 946136f3 2021-12-26 op r = unlinkat(f->dir->fd, dirpath, AT_REMOVEDIR);
1552 946136f3 2021-12-26 op } else /* file */
1553 c7f4e1bd 2021-12-24 op r = unlinkat(f->dir->fd, f->fpath, 0);
1555 0592b956 2021-12-20 cage if (r == -1)
1556 0592b956 2021-12-20 cage np_errno(hdr->tag);
1558 0592b956 2021-12-20 cage np_remove(hdr->tag);
1560 0592b956 2021-12-20 cage free_fid(f);
1564 e60f4e08 2021-07-30 op handle_message(struct imsg *imsg, size_t len)
1566 e60f4e08 2021-07-30 op struct msg {
1567 e60f4e08 2021-07-30 op uint8_t type;
1568 e60f4e08 2021-07-30 op void (*fn)(struct np_msg_header *, const uint8_t *, size_t);
1569 e60f4e08 2021-07-30 op } msgs[] = {
1570 e60f4e08 2021-07-30 op {Tversion, tversion},
1571 e60f4e08 2021-07-30 op {Tattach, tattach},
1572 c377c1b9 2021-07-30 op {Tclunk, tclunk},
1573 36b30273 2021-07-30 op {Tflush, tflush},
1574 1c08fc54 2021-08-01 op {Twalk, twalk},
1575 021481ca 2021-12-13 op {Topen, topen},
1576 3162e55b 2021-12-20 op {Tcreate, tcreate},
1577 baa40504 2021-12-15 op {Tread, tread},
1578 2483be55 2021-12-16 op {Twrite, twrite},
1579 3a2c53f5 2021-12-18 op {Tstat, tstat},
1580 0592b956 2021-12-20 cage {Tremove, tremove},
1582 e60f4e08 2021-07-30 op struct np_msg_header hdr;
1584 e60f4e08 2021-07-30 op uint8_t *data;
1586 64c19d90 2021-07-30 op #if DEBUG_PACKETS
1587 78b94752 2021-08-01 op hexdump("incoming packet", imsg->data, len);
1590 e60f4e08 2021-07-30 op parse_message(imsg->data, len, &hdr, &data);
1591 e60f4e08 2021-07-30 op len -= HEADERSIZE;
1593 e60f4e08 2021-07-30 op log_debug("got request: len=%d type=%d[%s] tag=%d",
1594 e60f4e08 2021-07-30 op hdr.len, hdr.type, pp_msg_type(hdr.type), hdr.tag);
1596 e60f4e08 2021-07-30 op if (!handshaked && hdr.type != Tversion) {
1597 e60f4e08 2021-07-30 op client_send_listener(IMSG_CLOSE, NULL, 0);
1598 e60f4e08 2021-07-30 op client_shutdown();
1602 e60f4e08 2021-07-30 op for (i = 0; i < sizeof(msgs)/sizeof(msgs[0]); ++i) {
1603 e60f4e08 2021-07-30 op if (msgs[i].type != hdr.type)
1606 e60f4e08 2021-07-30 op msgs[i].fn(&hdr, data, len);
1610 e60f4e08 2021-07-30 op np_error(hdr.tag, "Not supported.");