Blame


1 8d1b399b 2021-07-22 op /*
2 8d1b399b 2021-07-22 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 8d1b399b 2021-07-22 op *
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.
7 8d1b399b 2021-07-22 op *
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.
15 8d1b399b 2021-07-22 op */
16 8d1b399b 2021-07-22 op
17 8d1b399b 2021-07-22 op #include "compat.h"
18 8d1b399b 2021-07-22 op
19 c5d518da 2021-07-29 op #include <sys/stat.h>
20 c5d518da 2021-07-29 op
21 baa40504 2021-12-15 op #include <assert.h>
22 baa40504 2021-12-15 op #include <dirent.h>
23 23e03c88 2021-07-26 op #include <endian.h>
24 8d1b399b 2021-07-22 op #include <errno.h>
25 c5d518da 2021-07-29 op #include <fcntl.h>
26 8d1b399b 2021-07-22 op #include <pwd.h>
27 8d1b399b 2021-07-22 op #include <signal.h>
28 8d1b399b 2021-07-22 op #include <stdlib.h>
29 8d1b399b 2021-07-22 op #include <string.h>
30 8d1b399b 2021-07-22 op #include <syslog.h>
31 8d1b399b 2021-07-22 op #include <unistd.h>
32 8d1b399b 2021-07-22 op
33 8d1b399b 2021-07-22 op #include "client.h"
34 8d1b399b 2021-07-22 op #include "kamid.h"
35 8d1b399b 2021-07-22 op #include "log.h"
36 8d1b399b 2021-07-22 op #include "sandbox.h"
37 23e03c88 2021-07-26 op #include "utils.h"
38 e0dce04c 2021-07-30 op
39 2adb951f 2021-08-01 op #define DEBUG_PACKETS 0
40 8d1b399b 2021-07-22 op
41 baa40504 2021-12-15 op /* straight outta /src/usr.bin/ssh/scp.c */
42 baa40504 2021-12-15 op #define TYPE_OVERFLOW(type, val) \
43 baa40504 2021-12-15 op ((sizeof(type) == 4 && (val) > INT32_MAX) || \
44 baa40504 2021-12-15 op (sizeof(type) == 8 && (val) > INT64_MAX) || \
45 baa40504 2021-12-15 op (sizeof(type) != 4 && sizeof(type) != 8))
46 baa40504 2021-12-15 op
47 c5d518da 2021-07-29 op STAILQ_HEAD(qidhead, qid) qids;
48 c5d518da 2021-07-29 op struct qid {
49 c5d518da 2021-07-29 op /* definition of a qid */
50 c5d518da 2021-07-29 op uint64_t path;
51 c5d518da 2021-07-29 op uint32_t vers;
52 c5d518da 2021-07-29 op uint8_t type;
53 c5d518da 2021-07-29 op
54 c377c1b9 2021-07-30 op int refcount;
55 c5d518da 2021-07-29 op
56 deb971f5 2021-08-12 op int fd;
57 20c188ce 2021-12-16 op char fpath[PATH_MAX];
58 deb971f5 2021-08-12 op
59 c5d518da 2021-07-29 op STAILQ_ENTRY(qid) entries;
60 c5d518da 2021-07-29 op };
61 c5d518da 2021-07-29 op
62 c5d518da 2021-07-29 op STAILQ_HEAD(fidhead, fid) fids;
63 c5d518da 2021-07-29 op struct fid {
64 c5d518da 2021-07-29 op uint32_t fid;
65 29f1f582 2021-12-13 op
66 29f1f582 2021-12-13 op /*
67 29f1f582 2021-12-13 op * 0 when the fid was not yet opened for I/O otherwise set to
68 021481ca 2021-12-13 op * the flags passed to open(2). O_CLOEXEC means ORCLOSE, that
69 021481ca 2021-12-13 op * is to unlink the file upon Tclunk.
70 29f1f582 2021-12-13 op */
71 1c08fc54 2021-08-01 op int iomode;
72 29f1f582 2021-12-13 op
73 29f1f582 2021-12-13 op /*
74 29f1f582 2021-12-13 op * if iomode is set, this fid was opened and fd represents its
75 29f1f582 2021-12-13 op * file descriptor.
76 29f1f582 2021-12-13 op */
77 29f1f582 2021-12-13 op int fd;
78 baa40504 2021-12-15 op DIR *dir;
79 baa40504 2021-12-15 op struct evbuffer *evb;
80 5585f1c3 2021-12-16 op
81 5585f1c3 2021-12-16 op /*
82 5585f1c3 2021-12-16 op * expected offset for Tread against a directory.
83 5585f1c3 2021-12-16 op */
84 baa40504 2021-12-15 op uint64_t offset;
85 29f1f582 2021-12-13 op
86 c5d518da 2021-07-29 op struct qid *qid;
87 c5d518da 2021-07-29 op STAILQ_ENTRY(fid) entries;
88 c5d518da 2021-07-29 op };
89 c5d518da 2021-07-29 op
90 8d1b399b 2021-07-22 op static struct imsgev *iev_listener;
91 2ef72ade 2021-07-28 op static struct evbuffer *evb;
92 2ef72ade 2021-07-28 op static uint32_t peerid;
93 8d1b399b 2021-07-22 op
94 cf758c33 2021-12-14 op static int handshaked;
95 5c485996 2021-07-28 op uint32_t msize;
96 5c485996 2021-07-28 op
97 06a84967 2021-07-22 op static ATTR_DEAD void client_shutdown(void);
98 8d1b399b 2021-07-22 op static void client_sig_handler(int, short, void *);
99 8d1b399b 2021-07-22 op static void client_dispatch_listener(int, short, void *);
100 8d1b399b 2021-07-22 op static void client_privdrop(const char *, const char *);
101 8d1b399b 2021-07-22 op
102 2ef72ade 2021-07-28 op static int client_send_listener(int, const void *, uint16_t);
103 5c485996 2021-07-28 op
104 deb971f5 2021-08-12 op static void qid_update_from_sb(struct qid *, struct stat *);
105 deb971f5 2021-08-12 op static struct qid *qid_from_fd(int, const char *, struct stat *);
106 c377c1b9 2021-07-30 op static struct qid *qid_incref(struct qid *);
107 c377c1b9 2021-07-30 op static void qid_decref(struct qid *);
108 c377c1b9 2021-07-30 op
109 c5d518da 2021-07-29 op static struct fid *new_fid(struct qid *, uint32_t);
110 c377c1b9 2021-07-30 op static struct fid *fid_by_id(uint32_t);
111 c377c1b9 2021-07-30 op static void free_fid(struct fid *);
112 c5d518da 2021-07-29 op
113 48192874 2021-08-01 op static void parse_message(const uint8_t *, size_t,
114 48192874 2021-08-01 op struct np_msg_header *, uint8_t **);
115 8d1b399b 2021-07-22 op
116 5585f1c3 2021-12-16 op static void np_write16(struct evbuffer *, uint16_t);
117 5585f1c3 2021-12-16 op static void np_write32(struct evbuffer *, uint32_t);
118 5585f1c3 2021-12-16 op static void np_write64(struct evbuffer *, uint64_t);
119 2845cccb 2021-07-29 op static void np_header(uint32_t, uint8_t, uint16_t);
120 5585f1c3 2021-12-16 op static void np_string(struct evbuffer *, uint16_t, const char *);
121 5585f1c3 2021-12-16 op static void np_qid(struct evbuffer *, struct qid *);
122 84b10f04 2021-07-29 op static void do_send(void);
123 2845cccb 2021-07-29 op
124 2845cccb 2021-07-29 op static void np_version(uint16_t, uint32_t, const char *);
125 c5d518da 2021-07-29 op static void np_attach(uint16_t, struct qid *);
126 c377c1b9 2021-07-30 op static void np_clunk(uint16_t);
127 36b30273 2021-07-30 op static void np_flush(uint16_t);
128 1c08fc54 2021-08-01 op static void np_walk(uint16_t, int, struct qid *);
129 021481ca 2021-12-13 op static void np_open(uint16_t, struct qid *, uint32_t);
130 baa40504 2021-12-15 op static void np_read(uint16_t, uint32_t, void *);
131 2483be55 2021-12-16 op static void np_write(uint16_t, uint32_t);
132 3a2c53f5 2021-12-18 op static void np_stat(uint16_t, uint32_t, void *);
133 2ef72ade 2021-07-28 op static void np_error(uint16_t, const char *);
134 c5d518da 2021-07-29 op static void np_errno(uint16_t);
135 2ef72ade 2021-07-28 op
136 48192874 2021-08-01 op static int np_read8(const char *, const char *, uint8_t *,
137 48192874 2021-08-01 op const uint8_t **, size_t *);
138 48192874 2021-08-01 op static int np_read16(const char *, const char *, uint16_t *,
139 48192874 2021-08-01 op const uint8_t **, size_t *);
140 48192874 2021-08-01 op static int np_read32(const char *, const char *, uint32_t *,
141 48192874 2021-08-01 op const uint8_t **, size_t *);
142 baa40504 2021-12-15 op static int np_read64(const char *, const char *, uint64_t *,
143 baa40504 2021-12-15 op const uint8_t **, size_t *);
144 48192874 2021-08-01 op
145 48192874 2021-08-01 op #define READSTRERR -1
146 48192874 2021-08-01 op #define READSTRTRUNC -2
147 48192874 2021-08-01 op static int np_readstr(const char *, const char *, char *, size_t,
148 48192874 2021-08-01 op const uint8_t **, size_t *);
149 48192874 2021-08-01 op
150 48192874 2021-08-01 op #define NPREAD8(f, dst, src, len) np_read8(__func__, f, dst, src, len)
151 48192874 2021-08-01 op #define NPREAD16(f, dst, src, len) np_read16(__func__, f, dst, src, len)
152 48192874 2021-08-01 op #define NPREAD32(f, dst, src, len) np_read32(__func__, f, dst, src, len)
153 baa40504 2021-12-15 op #define NPREAD64(f, dst, src, len) np_read64(__func__, f, dst, src, len)
154 48192874 2021-08-01 op
155 48192874 2021-08-01 op #define NPREADSTR(f, b, bl, src, len) np_readstr(__func__, f, b, bl, src, len)
156 48192874 2021-08-01 op
157 e60f4e08 2021-07-30 op static void tversion(struct np_msg_header *, const uint8_t *, size_t);
158 e60f4e08 2021-07-30 op static void tattach(struct np_msg_header *, const uint8_t *, size_t);
159 c377c1b9 2021-07-30 op static void tclunk(struct np_msg_header *, const uint8_t *, size_t);
160 36b30273 2021-07-30 op static void tflush(struct np_msg_header *, const uint8_t *, size_t);
161 1c08fc54 2021-08-01 op static void twalk(struct np_msg_header *, const uint8_t *, size_t);
162 021481ca 2021-12-13 op static void topen(struct np_msg_header *, const uint8_t *, size_t);
163 baa40504 2021-12-15 op static void tread(struct np_msg_header *, const uint8_t *, size_t);
164 2483be55 2021-12-16 op static void twrite(struct np_msg_header *, const uint8_t *, size_t);
165 3a2c53f5 2021-12-18 op static void tstat(struct np_msg_header *, const uint8_t *, size_t);
166 e60f4e08 2021-07-30 op static void handle_message(struct imsg *, size_t);
167 64c19d90 2021-07-30 op
168 06a84967 2021-07-22 op ATTR_DEAD void
169 8d1b399b 2021-07-22 op client(int debug, int verbose)
170 8d1b399b 2021-07-22 op {
171 8d1b399b 2021-07-22 op struct event ev_sigint, ev_sigterm;
172 8d1b399b 2021-07-22 op
173 8d1b399b 2021-07-22 op log_init(debug, LOG_DAEMON);
174 8d1b399b 2021-07-22 op log_setverbose(verbose);
175 8d1b399b 2021-07-22 op
176 8d1b399b 2021-07-22 op setproctitle("client");
177 8d1b399b 2021-07-22 op log_procinit("client");
178 8d1b399b 2021-07-22 op
179 8d1b399b 2021-07-22 op log_debug("warming up");
180 8d1b399b 2021-07-22 op
181 8d1b399b 2021-07-22 op event_init();
182 8d1b399b 2021-07-22 op
183 8d1b399b 2021-07-22 op /* Setup signal handlers */
184 8d1b399b 2021-07-22 op signal_set(&ev_sigint, SIGINT, client_sig_handler, NULL);
185 8d1b399b 2021-07-22 op signal_set(&ev_sigterm, SIGTERM, client_sig_handler, NULL);
186 8d1b399b 2021-07-22 op
187 8d1b399b 2021-07-22 op signal_add(&ev_sigint, NULL);
188 8d1b399b 2021-07-22 op signal_add(&ev_sigterm, NULL);
189 8d1b399b 2021-07-22 op
190 8d1b399b 2021-07-22 op signal(SIGPIPE, SIG_IGN);
191 8d1b399b 2021-07-22 op signal(SIGHUP, SIG_IGN);
192 8d1b399b 2021-07-22 op
193 8d1b399b 2021-07-22 op /* Setup pipe and event handler to the listener process */
194 8d1b399b 2021-07-22 op if ((iev_listener = malloc(sizeof(*iev_listener))) == NULL)
195 8d1b399b 2021-07-22 op fatal(NULL);
196 8d1b399b 2021-07-22 op
197 8d1b399b 2021-07-22 op imsg_init(&iev_listener->ibuf, 3);
198 8d1b399b 2021-07-22 op iev_listener->handler = client_dispatch_listener;
199 8d1b399b 2021-07-22 op
200 8d1b399b 2021-07-22 op /* Setup event handlers. */
201 8d1b399b 2021-07-22 op iev_listener->events = EV_READ;
202 8d1b399b 2021-07-22 op event_set(&iev_listener->ev, iev_listener->ibuf.fd,
203 8d1b399b 2021-07-22 op iev_listener->events, iev_listener->handler, iev_listener);
204 8d1b399b 2021-07-22 op event_add(&iev_listener->ev, NULL);
205 8d1b399b 2021-07-22 op
206 8d1b399b 2021-07-22 op event_dispatch();
207 8d1b399b 2021-07-22 op client_shutdown();
208 8d1b399b 2021-07-22 op }
209 8d1b399b 2021-07-22 op
210 06a84967 2021-07-22 op static ATTR_DEAD void
211 8d1b399b 2021-07-22 op client_shutdown(void)
212 8d1b399b 2021-07-22 op {
213 2ef72ade 2021-07-28 op if (evb != NULL)
214 2ef72ade 2021-07-28 op evbuffer_free(evb);
215 2ef72ade 2021-07-28 op
216 8d1b399b 2021-07-22 op msgbuf_clear(&iev_listener->ibuf.w);
217 8d1b399b 2021-07-22 op close(iev_listener->ibuf.fd);
218 8d1b399b 2021-07-22 op
219 8d1b399b 2021-07-22 op free(iev_listener);
220 8d1b399b 2021-07-22 op
221 223c9e73 2021-08-06 op log_debug("client exiting");
222 8d1b399b 2021-07-22 op exit(0);
223 8d1b399b 2021-07-22 op }
224 8d1b399b 2021-07-22 op
225 8d1b399b 2021-07-22 op static void
226 8d1b399b 2021-07-22 op client_sig_handler(int sig, short event, void *d)
227 8d1b399b 2021-07-22 op {
228 8d1b399b 2021-07-22 op /*
229 8d1b399b 2021-07-22 op * Normal signal handler rules don't apply because libevent
230 8d1b399b 2021-07-22 op * decouples for us.
231 8d1b399b 2021-07-22 op */
232 8d1b399b 2021-07-22 op
233 8d1b399b 2021-07-22 op switch (sig) {
234 8d1b399b 2021-07-22 op case SIGINT:
235 8d1b399b 2021-07-22 op case SIGTERM:
236 8d1b399b 2021-07-22 op client_shutdown();
237 8d1b399b 2021-07-22 op default:
238 8d1b399b 2021-07-22 op fatalx("unexpected signal %d", sig);
239 8d1b399b 2021-07-22 op }
240 8d1b399b 2021-07-22 op }
241 8d1b399b 2021-07-22 op
242 8d1b399b 2021-07-22 op #define AUTH_NONE 0
243 8d1b399b 2021-07-22 op #define AUTH_USER 1
244 8d1b399b 2021-07-22 op #define AUTH_DONE 2
245 8d1b399b 2021-07-22 op
246 8d1b399b 2021-07-22 op static void
247 8d1b399b 2021-07-22 op client_dispatch_listener(int fd, short event, void *d)
248 8d1b399b 2021-07-22 op {
249 8d1b399b 2021-07-22 op static int auth = AUTH_NONE;
250 8d1b399b 2021-07-22 op static char username[64] = {0};
251 8d1b399b 2021-07-22 op static char dir[PATH_MAX] = {0};
252 8d1b399b 2021-07-22 op struct imsg imsg;
253 8d1b399b 2021-07-22 op struct imsgev *iev = d;
254 8d1b399b 2021-07-22 op struct imsgbuf *ibuf;
255 8d1b399b 2021-07-22 op ssize_t n;
256 8d1b399b 2021-07-22 op int shut = 0;
257 8d1b399b 2021-07-22 op
258 8d1b399b 2021-07-22 op ibuf = &iev->ibuf;
259 8d1b399b 2021-07-22 op
260 8d1b399b 2021-07-22 op if (event & EV_READ) {
261 8d1b399b 2021-07-22 op if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
262 8d1b399b 2021-07-22 op fatal("imsg_read error");
263 8d1b399b 2021-07-22 op if (n == 0) /* Connection closed */
264 8d1b399b 2021-07-22 op shut = 1;
265 8d1b399b 2021-07-22 op }
266 8d1b399b 2021-07-22 op if (event & EV_WRITE) {
267 8d1b399b 2021-07-22 op if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
268 8d1b399b 2021-07-22 op fatal("msgbuf_write");
269 8d1b399b 2021-07-22 op if (n == 0) /* Connection closed */
270 8d1b399b 2021-07-22 op shut = 1;
271 8d1b399b 2021-07-22 op }
272 8d1b399b 2021-07-22 op
273 8d1b399b 2021-07-22 op for (;;) {
274 8d1b399b 2021-07-22 op if ((n = imsg_get(ibuf, &imsg)) == -1)
275 8d1b399b 2021-07-22 op fatal("%s: imsg_get error", __func__);
276 8d1b399b 2021-07-22 op if (n == 0) /* No more messages. */
277 8d1b399b 2021-07-22 op break;
278 8d1b399b 2021-07-22 op
279 8d1b399b 2021-07-22 op switch (imsg.hdr.type) {
280 8d1b399b 2021-07-22 op case IMSG_AUTH:
281 2ef72ade 2021-07-28 op peerid = imsg.hdr.peerid;
282 8d1b399b 2021-07-22 op if (auth)
283 8d1b399b 2021-07-22 op fatalx("%s: IMSG_AUTH already done", __func__);
284 8d1b399b 2021-07-22 op auth = AUTH_USER;
285 8d1b399b 2021-07-22 op ((char *)imsg.data)[IMSG_DATA_SIZE(imsg)-1] = '\0';
286 8d1b399b 2021-07-22 op strlcpy(username, imsg.data, sizeof(username));
287 8d1b399b 2021-07-22 op break;
288 8d1b399b 2021-07-22 op case IMSG_AUTH_DIR:
289 8d1b399b 2021-07-22 op if (auth != AUTH_USER)
290 8d1b399b 2021-07-22 op fatalx("%s: IMSG_AUTH_DIR not after IMSG_AUTH",
291 8d1b399b 2021-07-22 op __func__);
292 8d1b399b 2021-07-22 op auth = AUTH_DONE;
293 8d1b399b 2021-07-22 op ((char *)imsg.data)[IMSG_DATA_SIZE(imsg)-1] = '\0';
294 8d1b399b 2021-07-22 op strlcpy(dir, imsg.data, sizeof(dir));
295 8d1b399b 2021-07-22 op client_privdrop(username, dir);
296 8d1b399b 2021-07-22 op memset(username, 0, sizeof(username));
297 8d1b399b 2021-07-22 op memset(dir, 0, sizeof(username));
298 8d1b399b 2021-07-22 op break;
299 8d1b399b 2021-07-22 op case IMSG_BUF:
300 8d1b399b 2021-07-22 op /* echo! */
301 23e03c88 2021-07-26 op if (!auth)
302 23e03c88 2021-07-26 op fatalx("%s: can't handle messages before"
303 23e03c88 2021-07-26 op " doing the auth", __func__);
304 23e03c88 2021-07-26 op handle_message(&imsg, IMSG_DATA_SIZE(imsg));
305 8d1b399b 2021-07-22 op break;
306 8d1b399b 2021-07-22 op case IMSG_CONN_GONE:
307 8d1b399b 2021-07-22 op log_debug("closing");
308 8d1b399b 2021-07-22 op shut = 1;
309 8d1b399b 2021-07-22 op break;
310 8d1b399b 2021-07-22 op default:
311 8d1b399b 2021-07-22 op log_debug("%s: unexpected imsg %d",
312 8d1b399b 2021-07-22 op __func__, imsg.hdr.type);
313 8d1b399b 2021-07-22 op break;
314 8d1b399b 2021-07-22 op }
315 8d1b399b 2021-07-22 op imsg_free(&imsg);
316 8d1b399b 2021-07-22 op }
317 8d1b399b 2021-07-22 op
318 8d1b399b 2021-07-22 op if (!shut)
319 8d1b399b 2021-07-22 op imsg_event_add(iev);
320 8d1b399b 2021-07-22 op else {
321 8d1b399b 2021-07-22 op /* This pipe is dead. Remove its event handler. */
322 8d1b399b 2021-07-22 op event_del(&iev->ev);
323 223c9e73 2021-08-06 op log_debug("pipe closed, shutting down...");
324 8d1b399b 2021-07-22 op event_loopexit(NULL);
325 8d1b399b 2021-07-22 op }
326 8d1b399b 2021-07-22 op }
327 8d1b399b 2021-07-22 op
328 8d1b399b 2021-07-22 op static void
329 8d1b399b 2021-07-22 op client_privdrop(const char *username, const char *dir)
330 8d1b399b 2021-07-22 op {
331 8d1b399b 2021-07-22 op struct passwd *pw;
332 8d1b399b 2021-07-22 op
333 8d1b399b 2021-07-22 op setproctitle("client %s", username);
334 8d1b399b 2021-07-22 op
335 8d1b399b 2021-07-22 op if ((pw = getpwnam(username)) == NULL)
336 8d1b399b 2021-07-22 op fatalx("getpwnam(%s) failed", username);
337 8d1b399b 2021-07-22 op
338 8d1b399b 2021-07-22 op if (chroot(dir) == -1)
339 8d1b399b 2021-07-22 op fatal("chroot");
340 8d1b399b 2021-07-22 op if (chdir("/") == -1)
341 8d1b399b 2021-07-22 op fatal("chdir(\"/\")");
342 8d1b399b 2021-07-22 op
343 8d1b399b 2021-07-22 op if (setgroups(1, &pw->pw_gid) ||
344 8d1b399b 2021-07-22 op setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
345 8d1b399b 2021-07-22 op setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
346 8d1b399b 2021-07-22 op fatal("can't drop privileges");
347 8d1b399b 2021-07-22 op
348 8d1b399b 2021-07-22 op sandbox_client();
349 66a9a40a 2021-07-28 op log_debug("client ready; user=%s dir=%s", username, dir);
350 2ef72ade 2021-07-28 op
351 2ef72ade 2021-07-28 op if ((evb = evbuffer_new()) == NULL)
352 2ef72ade 2021-07-28 op fatal("evbuffer_new");
353 8d1b399b 2021-07-22 op }
354 8d1b399b 2021-07-22 op
355 8d1b399b 2021-07-22 op static int
356 2ef72ade 2021-07-28 op client_send_listener(int type, const void *data, uint16_t len)
357 8d1b399b 2021-07-22 op {
358 8d1b399b 2021-07-22 op int ret;
359 8d1b399b 2021-07-22 op
360 8d1b399b 2021-07-22 op if ((ret = imsg_compose(&iev_listener->ibuf, type, peerid, 0, -1,
361 8d1b399b 2021-07-22 op data, len)) != -1)
362 8d1b399b 2021-07-22 op imsg_event_add(iev_listener);
363 8d1b399b 2021-07-22 op
364 8d1b399b 2021-07-22 op return ret;
365 23e03c88 2021-07-26 op }
366 23e03c88 2021-07-26 op
367 deb971f5 2021-08-12 op /* set qid fields from sb */
368 053e2652 2021-08-01 op static void
369 deb971f5 2021-08-12 op qid_update_from_sb(struct qid *qid, struct stat *sb)
370 1c08fc54 2021-08-01 op {
371 deb971f5 2021-08-12 op qid->path = sb->st_ino;
372 1c08fc54 2021-08-01 op
373 1c08fc54 2021-08-01 op /*
374 1c08fc54 2021-08-01 op * Theoretically (and hopefully!) this should be a 64 bit
375 1c08fc54 2021-08-01 op * number. Unfortunately, 9P uses 32 bit timestamps.
376 1c08fc54 2021-08-01 op */
377 deb971f5 2021-08-12 op qid->vers = sb->st_mtim.tv_sec;
378 1c08fc54 2021-08-01 op
379 deb971f5 2021-08-12 op if (S_ISREG(sb->st_mode))
380 1c08fc54 2021-08-01 op qid->type = QTFILE;
381 deb971f5 2021-08-12 op else if (S_ISDIR(sb->st_mode))
382 1c08fc54 2021-08-01 op qid->type = QTDIR;
383 deb971f5 2021-08-12 op else if (S_ISLNK(sb->st_mode))
384 1c08fc54 2021-08-01 op qid->type = QTSYMLINK;
385 053e2652 2021-08-01 op }
386 1c08fc54 2021-08-01 op
387 c5d518da 2021-07-29 op /* creates a qid given a fd */
388 c5d518da 2021-07-29 op static struct qid *
389 deb971f5 2021-08-12 op qid_from_fd(int fd, const char *path, struct stat *s)
390 c5d518da 2021-07-29 op {
391 c5d518da 2021-07-29 op struct qid *qid;
392 deb971f5 2021-08-12 op struct stat sb;
393 deb971f5 2021-08-12 op int r;
394 c5d518da 2021-07-29 op
395 c5d518da 2021-07-29 op if ((qid = calloc(1, sizeof(*qid))) == NULL)
396 c5d518da 2021-07-29 op return NULL;
397 c5d518da 2021-07-29 op
398 deb971f5 2021-08-12 op if (path != NULL)
399 deb971f5 2021-08-12 op strlcpy(qid->fpath, path, sizeof(qid->fpath));
400 deb971f5 2021-08-12 op
401 c5d518da 2021-07-29 op qid->fd = fd;
402 c5d518da 2021-07-29 op
403 deb971f5 2021-08-12 op if (s == NULL) {
404 deb971f5 2021-08-12 op s = &sb;
405 deb971f5 2021-08-12 op if (path == NULL)
406 deb971f5 2021-08-12 op r = fstat(fd, s);
407 deb971f5 2021-08-12 op else
408 deb971f5 2021-08-12 op r = fstatat(fd, path, s, 0);
409 deb971f5 2021-08-12 op if (r == -1) {
410 deb971f5 2021-08-12 op free(qid);
411 deb971f5 2021-08-12 op return NULL;
412 deb971f5 2021-08-12 op }
413 053e2652 2021-08-01 op }
414 053e2652 2021-08-01 op
415 deb971f5 2021-08-12 op qid_update_from_sb(qid, s);
416 053e2652 2021-08-01 op
417 053e2652 2021-08-01 op STAILQ_INSERT_HEAD(&qids, qid, entries);
418 053e2652 2021-08-01 op
419 053e2652 2021-08-01 op return qid;
420 053e2652 2021-08-01 op }
421 053e2652 2021-08-01 op
422 053e2652 2021-08-01 op static struct qid *
423 c377c1b9 2021-07-30 op qid_incref(struct qid *qid)
424 c377c1b9 2021-07-30 op {
425 c377c1b9 2021-07-30 op qid->refcount++;
426 c377c1b9 2021-07-30 op return qid;
427 c377c1b9 2021-07-30 op }
428 c377c1b9 2021-07-30 op
429 c377c1b9 2021-07-30 op static void
430 c377c1b9 2021-07-30 op qid_decref(struct qid *qid)
431 c377c1b9 2021-07-30 op {
432 c377c1b9 2021-07-30 op if (--qid->refcount > 0)
433 c377c1b9 2021-07-30 op return;
434 c377c1b9 2021-07-30 op
435 c377c1b9 2021-07-30 op STAILQ_REMOVE(&qids, qid, qid, entries);
436 c377c1b9 2021-07-30 op
437 c377c1b9 2021-07-30 op close(qid->fd);
438 c377c1b9 2021-07-30 op free(qid);
439 c5d518da 2021-07-29 op }
440 c5d518da 2021-07-29 op
441 c5d518da 2021-07-29 op static struct fid *
442 c5d518da 2021-07-29 op new_fid(struct qid *qid, uint32_t fid)
443 c5d518da 2021-07-29 op {
444 c5d518da 2021-07-29 op struct fid *f;
445 c5d518da 2021-07-29 op
446 c5d518da 2021-07-29 op if ((f = calloc(1, sizeof(*f))) == NULL)
447 c5d518da 2021-07-29 op return NULL;
448 c5d518da 2021-07-29 op
449 c377c1b9 2021-07-30 op f->qid = qid_incref(qid);
450 c5d518da 2021-07-29 op f->fid = fid;
451 5b704257 2021-12-13 op f->fd = -1;
452 c5d518da 2021-07-29 op
453 c5d518da 2021-07-29 op STAILQ_INSERT_HEAD(&fids, f, entries);
454 c5d518da 2021-07-29 op
455 c5d518da 2021-07-29 op return f;
456 c5d518da 2021-07-29 op }
457 c5d518da 2021-07-29 op
458 c377c1b9 2021-07-30 op static struct fid *
459 c377c1b9 2021-07-30 op fid_by_id(uint32_t fid)
460 c377c1b9 2021-07-30 op {
461 c377c1b9 2021-07-30 op struct fid *f;
462 c377c1b9 2021-07-30 op
463 c377c1b9 2021-07-30 op STAILQ_FOREACH(f, &fids, entries) {
464 c377c1b9 2021-07-30 op if (f->fid == fid)
465 c377c1b9 2021-07-30 op return f;
466 c377c1b9 2021-07-30 op }
467 c377c1b9 2021-07-30 op
468 c377c1b9 2021-07-30 op return NULL;
469 c377c1b9 2021-07-30 op }
470 c377c1b9 2021-07-30 op
471 5c485996 2021-07-28 op static void
472 c377c1b9 2021-07-30 op free_fid(struct fid *f)
473 c377c1b9 2021-07-30 op {
474 baa40504 2021-12-15 op int r;
475 baa40504 2021-12-15 op
476 021481ca 2021-12-13 op if (f->fd != -1) {
477 baa40504 2021-12-15 op if (f->dir != NULL)
478 baa40504 2021-12-15 op r = closedir(f->dir);
479 baa40504 2021-12-15 op else
480 baa40504 2021-12-15 op r = close(f->fd);
481 baa40504 2021-12-15 op
482 baa40504 2021-12-15 op if (r == -1)
483 baa40504 2021-12-15 op fatal("can't close fid %d", f->fid);
484 baa40504 2021-12-15 op
485 1e8d72fd 2021-12-15 op if (f->evb != NULL)
486 1e8d72fd 2021-12-15 op evbuffer_free(f->evb);
487 baa40504 2021-12-15 op
488 021481ca 2021-12-13 op /* try to honour ORCLOSE if requested */
489 021481ca 2021-12-13 op if (f->iomode & O_CLOEXEC)
490 021481ca 2021-12-13 op unlinkat(f->qid->fd, f->qid->fpath, 0);
491 021481ca 2021-12-13 op }
492 021481ca 2021-12-13 op
493 c377c1b9 2021-07-30 op qid_decref(f->qid);
494 c377c1b9 2021-07-30 op
495 c377c1b9 2021-07-30 op STAILQ_REMOVE(&fids, f, fid, entries);
496 c377c1b9 2021-07-30 op free(f);
497 c377c1b9 2021-07-30 op }
498 c377c1b9 2021-07-30 op
499 c377c1b9 2021-07-30 op static void
500 48192874 2021-08-01 op parse_message(const uint8_t *data, size_t len, struct np_msg_header *hdr,
501 5c485996 2021-07-28 op uint8_t **cnt)
502 23e03c88 2021-07-26 op {
503 48192874 2021-08-01 op size_t olen = len;
504 12c6d699 2021-07-26 op
505 48192874 2021-08-01 op if (!NPREAD32("len", &hdr->len, &data, &len) ||
506 48192874 2021-08-01 op !NPREAD8("type", &hdr->type, &data, &len) ||
507 48192874 2021-08-01 op !NPREAD16("tag", &hdr->tag, &data, &len))
508 48192874 2021-08-01 op goto err;
509 63f681aa 2021-07-28 op
510 48192874 2021-08-01 op if (olen != hdr->len)
511 23e03c88 2021-07-26 op goto err;
512 23e03c88 2021-07-26 op
513 23e03c88 2021-07-26 op if (hdr->type < Tversion ||
514 23e03c88 2021-07-26 op hdr->type >= Tmax ||
515 23e03c88 2021-07-26 op hdr->type == Terror ||
516 23e03c88 2021-07-26 op (hdr->type & 0x1) != 0) /* cannot recv a R* */
517 23e03c88 2021-07-26 op goto err;
518 23e03c88 2021-07-26 op
519 23e03c88 2021-07-26 op hdr->tag = le32toh(hdr->tag);
520 23e03c88 2021-07-26 op
521 48192874 2021-08-01 op *cnt = (uint8_t *)data;
522 23e03c88 2021-07-26 op return;
523 23e03c88 2021-07-26 op
524 23e03c88 2021-07-26 op err:
525 23e03c88 2021-07-26 op /* TODO: send a proper message to terminate the connection. */
526 23e03c88 2021-07-26 op fatalx("got invalid message");
527 83f6b305 2021-08-01 op }
528 83f6b305 2021-08-01 op
529 83f6b305 2021-08-01 op static void
530 5585f1c3 2021-12-16 op np_write16(struct evbuffer *e, uint16_t x)
531 83f6b305 2021-08-01 op {
532 83f6b305 2021-08-01 op x = htole16(x);
533 5585f1c3 2021-12-16 op evbuffer_add(e, &x, sizeof(x));
534 2ef72ade 2021-07-28 op }
535 2ef72ade 2021-07-28 op
536 2845cccb 2021-07-29 op static void
537 5585f1c3 2021-12-16 op np_write32(struct evbuffer *e, uint32_t x)
538 021481ca 2021-12-13 op {
539 021481ca 2021-12-13 op x = htole32(x);
540 5585f1c3 2021-12-16 op evbuffer_add(e, &x, sizeof(x));
541 baa40504 2021-12-15 op }
542 baa40504 2021-12-15 op
543 baa40504 2021-12-15 op static void
544 5585f1c3 2021-12-16 op np_write64(struct evbuffer *e, uint64_t x)
545 baa40504 2021-12-15 op {
546 5585f1c3 2021-12-16 op x = htole64(x);
547 5585f1c3 2021-12-16 op evbuffer_add(e, &x, sizeof(x));
548 021481ca 2021-12-13 op }
549 021481ca 2021-12-13 op
550 021481ca 2021-12-13 op static void
551 5585f1c3 2021-12-16 op np_writebuf(struct evbuffer *e, size_t len, void *data)
552 5585f1c3 2021-12-16 op {
553 5585f1c3 2021-12-16 op evbuffer_add(e, data, len);
554 5585f1c3 2021-12-16 op }
555 5585f1c3 2021-12-16 op
556 5585f1c3 2021-12-16 op static void
557 2ef72ade 2021-07-28 op np_header(uint32_t len, uint8_t type, uint16_t tag)
558 2ef72ade 2021-07-28 op {
559 9ebb95a7 2021-07-30 op len += HEADERSIZE;
560 9ebb95a7 2021-07-30 op
561 63f681aa 2021-07-28 op len = htole32(len);
562 63f681aa 2021-07-28 op tag = htole16(tag);
563 63f681aa 2021-07-28 op
564 2ef72ade 2021-07-28 op evbuffer_add(evb, &len, sizeof(len));
565 2ef72ade 2021-07-28 op evbuffer_add(evb, &type, sizeof(type));
566 2ef72ade 2021-07-28 op evbuffer_add(evb, &tag, sizeof(tag));
567 5c485996 2021-07-28 op }
568 5c485996 2021-07-28 op
569 2845cccb 2021-07-29 op static void
570 5585f1c3 2021-12-16 op np_string(struct evbuffer *e, uint16_t len, const char *str)
571 5c485996 2021-07-28 op {
572 5c485996 2021-07-28 op uint16_t l = len;
573 5c485996 2021-07-28 op
574 5c485996 2021-07-28 op len = htole16(len);
575 5585f1c3 2021-12-16 op evbuffer_add(e, &len, sizeof(len));
576 5585f1c3 2021-12-16 op evbuffer_add(e, str, l);
577 8d1b399b 2021-07-22 op }
578 23e03c88 2021-07-26 op
579 84b10f04 2021-07-29 op static void
580 5585f1c3 2021-12-16 op np_qid(struct evbuffer *e, struct qid *qid)
581 c5d518da 2021-07-29 op {
582 c5d518da 2021-07-29 op uint64_t path;
583 c5d518da 2021-07-29 op uint32_t vers;
584 c5d518da 2021-07-29 op
585 c5d518da 2021-07-29 op path = htole64(qid->path);
586 c5d518da 2021-07-29 op vers = htole32(qid->vers);
587 c5d518da 2021-07-29 op
588 5585f1c3 2021-12-16 op evbuffer_add(e, &qid->type, sizeof(qid->type));
589 5585f1c3 2021-12-16 op evbuffer_add(e, &vers, sizeof(vers));
590 5585f1c3 2021-12-16 op evbuffer_add(e, &path, sizeof(path));
591 c5d518da 2021-07-29 op }
592 c5d518da 2021-07-29 op
593 c5d518da 2021-07-29 op static void
594 2ef72ade 2021-07-28 op do_send(void)
595 2ef72ade 2021-07-28 op {
596 64c19d90 2021-07-30 op size_t len;
597 64c19d90 2021-07-30 op void *data;
598 2ef72ade 2021-07-28 op
599 2ef72ade 2021-07-28 op len = EVBUFFER_LENGTH(evb);
600 64c19d90 2021-07-30 op data = EVBUFFER_DATA(evb);
601 64c19d90 2021-07-30 op
602 64c19d90 2021-07-30 op #if DEBUG_PACKETS
603 78b94752 2021-08-01 op hexdump("outgoing packet", data, len);
604 64c19d90 2021-07-30 op #endif
605 64c19d90 2021-07-30 op client_send_listener(IMSG_BUF, data, len);
606 2ef72ade 2021-07-28 op evbuffer_drain(evb, len);
607 2ef72ade 2021-07-28 op }
608 2ef72ade 2021-07-28 op
609 23e03c88 2021-07-26 op static void
610 5c485996 2021-07-28 op np_version(uint16_t tag, uint32_t msize, const char *version)
611 5c485996 2021-07-28 op {
612 5c485996 2021-07-28 op uint16_t l;
613 5c485996 2021-07-28 op
614 5c485996 2021-07-28 op l = strlen(version);
615 5c485996 2021-07-28 op
616 5c485996 2021-07-28 op msize = htole32(msize);
617 5c485996 2021-07-28 op
618 9ebb95a7 2021-07-30 op np_header(sizeof(msize) + sizeof(l) + l, Rversion, tag);
619 5c485996 2021-07-28 op evbuffer_add(evb, &msize, sizeof(msize));
620 5585f1c3 2021-12-16 op np_string(evb, l, version);
621 5c485996 2021-07-28 op do_send();
622 5c485996 2021-07-28 op }
623 5c485996 2021-07-28 op
624 5c485996 2021-07-28 op static void
625 c5d518da 2021-07-29 op np_attach(uint16_t tag, struct qid *qid)
626 c5d518da 2021-07-29 op {
627 9ebb95a7 2021-07-30 op np_header(QIDSIZE, Rattach, tag);
628 5585f1c3 2021-12-16 op np_qid(evb, qid);
629 c5d518da 2021-07-29 op do_send();
630 c5d518da 2021-07-29 op }
631 c5d518da 2021-07-29 op
632 c5d518da 2021-07-29 op static void
633 c377c1b9 2021-07-30 op np_clunk(uint16_t tag)
634 c377c1b9 2021-07-30 op {
635 9ebb95a7 2021-07-30 op np_header(0, Rclunk, tag);
636 c377c1b9 2021-07-30 op do_send();
637 c377c1b9 2021-07-30 op }
638 c377c1b9 2021-07-30 op
639 c377c1b9 2021-07-30 op static void
640 36b30273 2021-07-30 op np_flush(uint16_t tag)
641 36b30273 2021-07-30 op {
642 9ebb95a7 2021-07-30 op np_header(0, Rflush, tag);
643 36b30273 2021-07-30 op do_send();
644 36b30273 2021-07-30 op }
645 36b30273 2021-07-30 op
646 36b30273 2021-07-30 op static void
647 1c08fc54 2021-08-01 op np_walk(uint16_t tag, int nwqid, struct qid *wqid)
648 1c08fc54 2021-08-01 op {
649 1c08fc54 2021-08-01 op int i;
650 1c08fc54 2021-08-01 op
651 83f6b305 2021-08-01 op /* two bytes for the counter */
652 83f6b305 2021-08-01 op np_header(2 + QIDSIZE * nwqid, Rwalk, tag);
653 5585f1c3 2021-12-16 op np_write16(evb, nwqid);
654 1c08fc54 2021-08-01 op for (i = 0; i < nwqid; ++i)
655 5585f1c3 2021-12-16 op np_qid(evb, wqid + i);
656 1c08fc54 2021-08-01 op
657 1c08fc54 2021-08-01 op do_send();
658 1c08fc54 2021-08-01 op }
659 1c08fc54 2021-08-01 op
660 1c08fc54 2021-08-01 op static void
661 021481ca 2021-12-13 op np_open(uint16_t tag, struct qid *qid, uint32_t iounit)
662 021481ca 2021-12-13 op {
663 021481ca 2021-12-13 op np_header(QIDSIZE + sizeof(iounit), Ropen, tag);
664 5585f1c3 2021-12-16 op np_qid(evb, qid);
665 5585f1c3 2021-12-16 op np_write32(evb, iounit);
666 021481ca 2021-12-13 op do_send();
667 021481ca 2021-12-13 op }
668 021481ca 2021-12-13 op
669 021481ca 2021-12-13 op static void
670 baa40504 2021-12-15 op np_read(uint16_t tag, uint32_t count, void *data)
671 baa40504 2021-12-15 op {
672 baa40504 2021-12-15 op np_header(sizeof(count) + count, Rread, tag);
673 5585f1c3 2021-12-16 op np_write32(evb, count);
674 5585f1c3 2021-12-16 op np_writebuf(evb, count, data);
675 baa40504 2021-12-15 op do_send();
676 baa40504 2021-12-15 op }
677 baa40504 2021-12-15 op
678 baa40504 2021-12-15 op static void
679 2483be55 2021-12-16 op np_write(uint16_t tag, uint32_t count)
680 2483be55 2021-12-16 op {
681 2483be55 2021-12-16 op np_header(sizeof(count), Rwrite, tag);
682 2483be55 2021-12-16 op np_write32(evb, count);
683 3a2c53f5 2021-12-18 op do_send();
684 3a2c53f5 2021-12-18 op }
685 3a2c53f5 2021-12-18 op
686 3a2c53f5 2021-12-18 op static void
687 3a2c53f5 2021-12-18 op np_stat(uint16_t tag, uint32_t count, void *data)
688 3a2c53f5 2021-12-18 op {
689 3a2c53f5 2021-12-18 op np_header(count, Rstat, tag);
690 3a2c53f5 2021-12-18 op np_writebuf(evb, count, data);
691 2483be55 2021-12-16 op do_send();
692 2483be55 2021-12-16 op }
693 2483be55 2021-12-16 op
694 2483be55 2021-12-16 op static void
695 2ef72ade 2021-07-28 op np_error(uint16_t tag, const char *errstr)
696 2ef72ade 2021-07-28 op {
697 2ef72ade 2021-07-28 op uint16_t l;
698 2ef72ade 2021-07-28 op
699 2ef72ade 2021-07-28 op l = strlen(errstr);
700 2ef72ade 2021-07-28 op
701 9ebb95a7 2021-07-30 op np_header(sizeof(l) + l, Rerror, tag);
702 5585f1c3 2021-12-16 op np_string(evb, l, errstr);
703 2ef72ade 2021-07-28 op do_send();
704 2ef72ade 2021-07-28 op }
705 2ef72ade 2021-07-28 op
706 2ef72ade 2021-07-28 op static void
707 c5d518da 2021-07-29 op np_errno(uint16_t tag)
708 c5d518da 2021-07-29 op {
709 9b088310 2021-07-30 op int saved_errno;
710 c5d518da 2021-07-29 op char buf[64];
711 c5d518da 2021-07-29 op
712 9b088310 2021-07-30 op saved_errno = errno;
713 9b088310 2021-07-30 op
714 c5d518da 2021-07-29 op strerror_r(errno, buf, sizeof(buf));
715 c5d518da 2021-07-29 op np_error(tag, buf);
716 9b088310 2021-07-30 op
717 9b088310 2021-07-30 op errno = saved_errno;
718 48192874 2021-08-01 op }
719 48192874 2021-08-01 op
720 48192874 2021-08-01 op static int
721 48192874 2021-08-01 op np_read8(const char *t, const char *f, uint8_t *dst, const uint8_t **src,
722 48192874 2021-08-01 op size_t *len)
723 48192874 2021-08-01 op {
724 48192874 2021-08-01 op if (*len < sizeof(*dst)) {
725 48192874 2021-08-01 op log_warnx("%s: wanted %zu bytes for the %s field but only "
726 48192874 2021-08-01 op "%zu are available.", t, sizeof(*dst), f, *len);
727 48192874 2021-08-01 op return -1;
728 48192874 2021-08-01 op }
729 48192874 2021-08-01 op
730 48192874 2021-08-01 op memcpy(dst, *src, sizeof(*dst));
731 48192874 2021-08-01 op *src += sizeof(*dst);
732 48192874 2021-08-01 op *len -= sizeof(*dst);
733 48192874 2021-08-01 op
734 48192874 2021-08-01 op return 1;
735 48192874 2021-08-01 op }
736 48192874 2021-08-01 op
737 48192874 2021-08-01 op static int
738 48192874 2021-08-01 op np_read16(const char *t, const char *f, uint16_t *dst, const uint8_t **src,
739 48192874 2021-08-01 op size_t *len)
740 48192874 2021-08-01 op {
741 48192874 2021-08-01 op if (*len < sizeof(*dst)) {
742 48192874 2021-08-01 op log_warnx("%s: wanted %zu bytes for the %s field but only "
743 48192874 2021-08-01 op "%zu are available.", t, sizeof(*dst), f, *len);
744 48192874 2021-08-01 op return -1;
745 48192874 2021-08-01 op }
746 48192874 2021-08-01 op
747 48192874 2021-08-01 op memcpy(dst, *src, sizeof(*dst));
748 48192874 2021-08-01 op *src += sizeof(*dst);
749 48192874 2021-08-01 op *len -= sizeof(*dst);
750 48192874 2021-08-01 op *dst = le16toh(*dst);
751 48192874 2021-08-01 op
752 48192874 2021-08-01 op return 1;
753 c5d518da 2021-07-29 op }
754 c5d518da 2021-07-29 op
755 48192874 2021-08-01 op static int
756 48192874 2021-08-01 op np_read32(const char *t, const char *f, uint32_t *dst, const uint8_t **src,
757 48192874 2021-08-01 op size_t *len)
758 48192874 2021-08-01 op {
759 48192874 2021-08-01 op if (*len < sizeof(*dst)) {
760 48192874 2021-08-01 op log_warnx("%s: wanted %zu bytes for the %s field but only "
761 48192874 2021-08-01 op "%zu are available.", t, sizeof(*dst), f, *len);
762 48192874 2021-08-01 op return -1;
763 48192874 2021-08-01 op }
764 48192874 2021-08-01 op
765 48192874 2021-08-01 op memcpy(dst, *src, sizeof(*dst));
766 48192874 2021-08-01 op *src += sizeof(*dst);
767 48192874 2021-08-01 op *len -= sizeof(*dst);
768 48192874 2021-08-01 op *dst = le32toh(*dst);
769 baa40504 2021-12-15 op
770 baa40504 2021-12-15 op return 1;
771 baa40504 2021-12-15 op }
772 baa40504 2021-12-15 op
773 baa40504 2021-12-15 op static int
774 baa40504 2021-12-15 op np_read64(const char *t, const char *f, uint64_t *dst, const uint8_t **src,
775 baa40504 2021-12-15 op size_t *len)
776 baa40504 2021-12-15 op {
777 baa40504 2021-12-15 op if (*len < sizeof(*dst)) {
778 baa40504 2021-12-15 op log_warnx("%s: wanted %zu bytes for the %s field but only "
779 baa40504 2021-12-15 op "%zu are available.", t, sizeof(*dst), f, *len);
780 baa40504 2021-12-15 op return -1;
781 baa40504 2021-12-15 op }
782 baa40504 2021-12-15 op
783 baa40504 2021-12-15 op memcpy(dst, *src, sizeof(*dst));
784 baa40504 2021-12-15 op *src += sizeof(*dst);
785 baa40504 2021-12-15 op *len -= sizeof(*dst);
786 baa40504 2021-12-15 op *dst = le64toh(*dst);
787 48192874 2021-08-01 op
788 48192874 2021-08-01 op return 1;
789 48192874 2021-08-01 op }
790 48192874 2021-08-01 op
791 48192874 2021-08-01 op static int
792 48192874 2021-08-01 op np_readstr(const char *t, const char *f, char *res, size_t reslen,
793 48192874 2021-08-01 op const uint8_t **src, size_t *len)
794 48192874 2021-08-01 op {
795 48192874 2021-08-01 op uint16_t sl;
796 48192874 2021-08-01 op char buf[32];
797 48192874 2021-08-01 op
798 48192874 2021-08-01 op strlcpy(buf, f, sizeof(buf));
799 48192874 2021-08-01 op strlcat(buf, "-len", sizeof(buf));
800 48192874 2021-08-01 op
801 48192874 2021-08-01 op if (!np_read16(t, buf, &sl, src, len))
802 48192874 2021-08-01 op return READSTRERR;
803 48192874 2021-08-01 op
804 48192874 2021-08-01 op if (*len < sl) {
805 48192874 2021-08-01 op log_warnx("%s: wanted %d bytes for the %s field but only "
806 48192874 2021-08-01 op "%zu are available.", t, sl, f, *len);
807 48192874 2021-08-01 op return READSTRERR;
808 48192874 2021-08-01 op }
809 48192874 2021-08-01 op
810 48192874 2021-08-01 op if (*len > reslen-1)
811 48192874 2021-08-01 op return READSTRTRUNC;
812 48192874 2021-08-01 op
813 48192874 2021-08-01 op memcpy(res, *src, sl);
814 48192874 2021-08-01 op res[sl] = '\0';
815 48192874 2021-08-01 op *src += sl;
816 48192874 2021-08-01 op *len -= sl;
817 48192874 2021-08-01 op
818 48192874 2021-08-01 op return 0;
819 48192874 2021-08-01 op }
820 48192874 2021-08-01 op
821 c5d518da 2021-07-29 op static void
822 e60f4e08 2021-07-30 op tversion(struct np_msg_header *hdr, const uint8_t *data, size_t len)
823 23e03c88 2021-07-26 op {
824 48192874 2021-08-01 op char *dot, version[32];
825 23e03c88 2021-07-26 op
826 e60f4e08 2021-07-30 op if (handshaked)
827 5c485996 2021-07-28 op goto err;
828 5c485996 2021-07-28 op
829 3d20424d 2021-08-01 op /* msize[4] version[s] */
830 48192874 2021-08-01 op if (!NPREAD32("msize", &msize, &data, &len))
831 e60f4e08 2021-07-30 op goto err;
832 5c485996 2021-07-28 op
833 48192874 2021-08-01 op switch (NPREADSTR("version", version, sizeof(version), &data, &len)) {
834 48192874 2021-08-01 op case READSTRERR:
835 48192874 2021-08-01 op goto err;
836 48192874 2021-08-01 op case READSTRTRUNC:
837 48192874 2021-08-01 op log_warnx("9P version string too long, truncated");
838 48192874 2021-08-01 op goto mismatch;
839 e60f4e08 2021-07-30 op }
840 db8dca01 2021-07-29 op
841 48192874 2021-08-01 op if ((dot = strchr(version, '.')) != NULL)
842 48192874 2021-08-01 op *dot = '\0';
843 5c485996 2021-07-28 op
844 48192874 2021-08-01 op if (strcmp(version, VERSION9P) != 0 ||
845 48192874 2021-08-01 op msize == 0)
846 48192874 2021-08-01 op goto mismatch;
847 48192874 2021-08-01 op
848 48192874 2021-08-01 op /* version matched */
849 48192874 2021-08-01 op handshaked = 1;
850 e60f4e08 2021-07-30 op msize = MIN(msize, MSIZE9P);
851 e60f4e08 2021-07-30 op client_send_listener(IMSG_MSIZE, &msize, sizeof(msize));
852 e60f4e08 2021-07-30 op np_version(hdr->tag, msize, VERSION9P);
853 e60f4e08 2021-07-30 op return;
854 5c485996 2021-07-28 op
855 48192874 2021-08-01 op mismatch:
856 48192874 2021-08-01 op log_warnx("unknown 9P version string: \"%s\", want "VERSION9P,
857 48192874 2021-08-01 op version);
858 48192874 2021-08-01 op np_version(hdr->tag, MSIZE9P, "unknown");
859 48192874 2021-08-01 op return;
860 48192874 2021-08-01 op
861 e60f4e08 2021-07-30 op err:
862 e60f4e08 2021-07-30 op client_send_listener(IMSG_CLOSE, NULL, 0);
863 e60f4e08 2021-07-30 op client_shutdown();
864 e60f4e08 2021-07-30 op }
865 5c485996 2021-07-28 op
866 e60f4e08 2021-07-30 op static void
867 e60f4e08 2021-07-30 op tattach(struct np_msg_header *hdr, const uint8_t *data, size_t len)
868 e60f4e08 2021-07-30 op {
869 e60f4e08 2021-07-30 op struct qid *qid;
870 e60f4e08 2021-07-30 op struct fid *f;
871 48192874 2021-08-01 op uint32_t fid, afid;
872 deb971f5 2021-08-12 op int fd;
873 014adfc2 2021-07-30 op char aname[PATH_MAX];
874 c5d518da 2021-07-29 op
875 e60f4e08 2021-07-30 op /* fid[4] afid[4] uname[s] aname[s] */
876 c5d518da 2021-07-29 op
877 48192874 2021-08-01 op if (!NPREAD32("fid", &fid, &data, &len) ||
878 48192874 2021-08-01 op !NPREAD32("afid", &afid, &data, &len))
879 014adfc2 2021-07-30 op goto err;
880 014adfc2 2021-07-30 op
881 48192874 2021-08-01 op /* read the uname but don't actually use it */
882 48192874 2021-08-01 op switch (NPREADSTR("uname", aname, sizeof(aname), &data, &len)) {
883 48192874 2021-08-01 op case READSTRERR:
884 014adfc2 2021-07-30 op goto err;
885 48192874 2021-08-01 op case READSTRTRUNC:
886 48192874 2021-08-01 op np_error(hdr->tag, "name too long");
887 48192874 2021-08-01 op return;
888 3fe030c5 2021-07-30 op }
889 3fe030c5 2021-07-30 op
890 48192874 2021-08-01 op switch (NPREADSTR("aname", aname, sizeof(aname), &data, &len)) {
891 48192874 2021-08-01 op case READSTRERR:
892 48192874 2021-08-01 op goto err;
893 48192874 2021-08-01 op case READSTRTRUNC:
894 014adfc2 2021-07-30 op np_error(hdr->tag, "name too long");
895 cf758c33 2021-12-14 op return;
896 cf758c33 2021-12-14 op }
897 cf758c33 2021-12-14 op
898 cf758c33 2021-12-14 op if (fid_by_id(fid) != NULL || afid != NOFID) {
899 cf758c33 2021-12-14 op np_error(hdr->tag, "invalid fid or afid");
900 014adfc2 2021-07-30 op return;
901 014adfc2 2021-07-30 op }
902 014adfc2 2021-07-30 op
903 deb971f5 2021-08-12 op if ((fd = open(aname, O_RDONLY|O_DIRECTORY)) == -1)
904 deb971f5 2021-08-12 op goto fail;
905 deb971f5 2021-08-12 op
906 deb971f5 2021-08-12 op if ((qid = qid_from_fd(fd, NULL, NULL)) == NULL)
907 deb971f5 2021-08-12 op goto fail;
908 deb971f5 2021-08-12 op
909 cf758c33 2021-12-14 op log_debug("attached %s to %d", aname, fid);
910 c5d518da 2021-07-29 op
911 e60f4e08 2021-07-30 op if ((f = new_fid(qid, fid)) == NULL) {
912 053e2652 2021-08-01 op qid_decref(qid);
913 deb971f5 2021-08-12 op goto fail;
914 5c485996 2021-07-28 op }
915 5c485996 2021-07-28 op
916 e60f4e08 2021-07-30 op np_attach(hdr->tag, qid);
917 deb971f5 2021-08-12 op return;
918 deb971f5 2021-08-12 op
919 deb971f5 2021-08-12 op fail:
920 deb971f5 2021-08-12 op np_errno(hdr->tag);
921 deb971f5 2021-08-12 op log_warn("failed to attach %s", aname);
922 5c485996 2021-07-28 op return;
923 deb971f5 2021-08-12 op return;
924 014adfc2 2021-07-30 op
925 014adfc2 2021-07-30 op err:
926 014adfc2 2021-07-30 op client_send_listener(IMSG_CLOSE, NULL, 0);
927 014adfc2 2021-07-30 op client_shutdown();
928 c377c1b9 2021-07-30 op }
929 c377c1b9 2021-07-30 op
930 c377c1b9 2021-07-30 op static void
931 c377c1b9 2021-07-30 op tclunk(struct np_msg_header *hdr, const uint8_t *data, size_t len)
932 c377c1b9 2021-07-30 op {
933 c377c1b9 2021-07-30 op struct fid *f;
934 c377c1b9 2021-07-30 op uint32_t fid;
935 c377c1b9 2021-07-30 op
936 c377c1b9 2021-07-30 op /* fid[4] */
937 48192874 2021-08-01 op if (!NPREAD32("fid", &fid, &data, &len)) {
938 c377c1b9 2021-07-30 op client_send_listener(IMSG_CLOSE, NULL, 0);
939 c377c1b9 2021-07-30 op client_shutdown();
940 c377c1b9 2021-07-30 op return;
941 c377c1b9 2021-07-30 op }
942 c377c1b9 2021-07-30 op
943 c377c1b9 2021-07-30 op if ((f = fid_by_id(fid)) == NULL) {
944 c377c1b9 2021-07-30 op np_error(hdr->tag, "invalid fid");
945 c377c1b9 2021-07-30 op return;
946 c377c1b9 2021-07-30 op }
947 c377c1b9 2021-07-30 op
948 c377c1b9 2021-07-30 op free_fid(f);
949 c377c1b9 2021-07-30 op np_clunk(hdr->tag);
950 36b30273 2021-07-30 op }
951 36b30273 2021-07-30 op
952 36b30273 2021-07-30 op static void
953 36b30273 2021-07-30 op tflush(struct np_msg_header *hdr, const uint8_t *data, size_t len)
954 36b30273 2021-07-30 op {
955 36b30273 2021-07-30 op uint16_t oldtag;
956 36b30273 2021-07-30 op
957 36b30273 2021-07-30 op /*
958 36b30273 2021-07-30 op * We're doing only synchronous I/O. Tflush is implemented
959 36b30273 2021-07-30 op * only because it's illegal to reply with a Rerror.
960 36b30273 2021-07-30 op */
961 36b30273 2021-07-30 op
962 36b30273 2021-07-30 op /* oldtag[2] */
963 36b30273 2021-07-30 op if (len != sizeof(oldtag)) {
964 36b30273 2021-07-30 op log_warnx("Tclunk with the wrong size: got %zu want %zu",
965 36b30273 2021-07-30 op len, sizeof(oldtag));
966 36b30273 2021-07-30 op client_send_listener(IMSG_CLOSE, NULL, 0);
967 36b30273 2021-07-30 op client_shutdown();
968 36b30273 2021-07-30 op return;
969 36b30273 2021-07-30 op }
970 36b30273 2021-07-30 op
971 36b30273 2021-07-30 op np_flush(hdr->tag);
972 e60f4e08 2021-07-30 op }
973 5c485996 2021-07-28 op
974 e60f4e08 2021-07-30 op static void
975 1c08fc54 2021-08-01 op twalk(struct np_msg_header *hdr, const uint8_t *data, size_t len)
976 1c08fc54 2021-08-01 op {
977 deb971f5 2021-08-12 op struct stat sb;
978 053e2652 2021-08-01 op struct qid *qid, wqid[MAXWELEM] = {0};
979 1c08fc54 2021-08-01 op struct fid *f, *nf;
980 1c08fc54 2021-08-01 op uint32_t fid, newfid;
981 7ddddcfe 2021-08-01 op uint16_t nwname;
982 deb971f5 2021-08-12 op int fd, oldfd, no, nwqid = 0;
983 20c188ce 2021-12-16 op char wnam[PATH_MAX];
984 1c08fc54 2021-08-01 op
985 48192874 2021-08-01 op if (!NPREAD32("fid", &fid, &data, &len) ||
986 48192874 2021-08-01 op !NPREAD32("newfid", &newfid, &data, &len) ||
987 48192874 2021-08-01 op !NPREAD16("nwname", &nwname, &data, &len))
988 48192874 2021-08-01 op goto err;
989 1c08fc54 2021-08-01 op
990 1c08fc54 2021-08-01 op if (nwname > MAXWELEM) {
991 1c08fc54 2021-08-01 op log_warnx("Twalk: more than %d path elements: %d",
992 1c08fc54 2021-08-01 op MAXWELEM, nwname);
993 1c08fc54 2021-08-01 op goto err;
994 1c08fc54 2021-08-01 op }
995 1c08fc54 2021-08-01 op
996 1c08fc54 2021-08-01 op if ((f = fid_by_id(fid)) == NULL) {
997 1c08fc54 2021-08-01 op np_error(hdr->tag, "invalid fid");
998 47c9b8b8 2021-12-02 op return;
999 47c9b8b8 2021-12-02 op }
1000 47c9b8b8 2021-12-02 op
1001 99a43590 2021-12-14 op if (f->fd != -1) {
1002 47c9b8b8 2021-12-02 op np_error(hdr->tag, "fid already opened for I/O");
1003 1c08fc54 2021-08-01 op return;
1004 1c08fc54 2021-08-01 op }
1005 1c08fc54 2021-08-01 op
1006 1c08fc54 2021-08-01 op if (fid == newfid)
1007 1c08fc54 2021-08-01 op nf = f;
1008 1c08fc54 2021-08-01 op else if ((nf = fid_by_id(newfid)) != NULL) {
1009 1c08fc54 2021-08-01 op np_error(hdr->tag, "newfid already in use");
1010 1c08fc54 2021-08-01 op return;
1011 1c08fc54 2021-08-01 op } else
1012 1c08fc54 2021-08-01 op nf = NULL;
1013 1c08fc54 2021-08-01 op
1014 913eba5c 2021-08-01 op /* special case: fid duplication */
1015 22dfb5a0 2021-08-01 op if (nwname == 0) {
1016 1c08fc54 2021-08-01 op /*
1017 1c08fc54 2021-08-01 op * TODO: should we forbid fids duplication when fid ==
1018 1c08fc54 2021-08-01 op * newfid?
1019 1c08fc54 2021-08-01 op */
1020 f987557c 2021-12-02 op if (nf == NULL && (nf = new_fid(f->qid, newfid)) == NULL)
1021 f987557c 2021-12-02 op fatal("new_fid duplication");
1022 1c08fc54 2021-08-01 op
1023 1c08fc54 2021-08-01 op np_walk(hdr->tag, 1, f->qid);
1024 1c08fc54 2021-08-01 op return;
1025 1c08fc54 2021-08-01 op }
1026 1c08fc54 2021-08-01 op
1027 053e2652 2021-08-01 op if (f->qid->type != QTDIR) {
1028 053e2652 2021-08-01 op np_error(hdr->tag, "fid doesn't represent a directory");
1029 053e2652 2021-08-01 op return;
1030 053e2652 2021-08-01 op }
1031 053e2652 2021-08-01 op
1032 deb971f5 2021-08-12 op oldfd = f->qid->fd;
1033 053e2652 2021-08-01 op
1034 1c08fc54 2021-08-01 op for (nwqid = 0; nwqid < nwname; nwqid++) {
1035 053e2652 2021-08-01 op switch (NPREADSTR("wname", wnam, sizeof(wnam), &data, &len)) {
1036 48192874 2021-08-01 op case READSTRERR:
1037 1c08fc54 2021-08-01 op goto err;
1038 48192874 2021-08-01 op case READSTRTRUNC:
1039 1c08fc54 2021-08-01 op np_error(hdr->tag, "wname too long");
1040 1c08fc54 2021-08-01 op return;
1041 1c08fc54 2021-08-01 op }
1042 1c08fc54 2021-08-01 op
1043 054cd6b9 2021-12-14 op if (*wnam == '\0' ||
1044 054cd6b9 2021-12-14 op strchr(wnam, '/') != NULL ||
1045 054cd6b9 2021-12-14 op !strcmp(wnam, ".")) {
1046 054cd6b9 2021-12-14 op errno = EINVAL;
1047 054cd6b9 2021-12-14 op goto cantopen;
1048 054cd6b9 2021-12-14 op }
1049 054cd6b9 2021-12-14 op
1050 deb971f5 2021-08-12 op if ((fd = openat(oldfd, wnam, O_RDONLY|O_DIRECTORY)) == -1 &&
1051 3242c0bc 2021-12-14 op errno != ENOTDIR)
1052 deb971f5 2021-08-12 op goto cantopen;
1053 053e2652 2021-08-01 op
1054 deb971f5 2021-08-12 op if ((fd == -1 && fstatat(oldfd, wnam, &sb, 0) == -1) ||
1055 3242c0bc 2021-12-14 op (fd != -1 && fstat(fd, &sb) == -1))
1056 deb971f5 2021-08-12 op goto cantopen;
1057 b806d4d5 2021-08-01 op
1058 deb971f5 2021-08-12 op qid_update_from_sb(&wqid[nwqid], &sb);
1059 deb971f5 2021-08-12 op
1060 deb971f5 2021-08-12 op /* reached a file but we still have other components */
1061 deb971f5 2021-08-12 op if (fd == -1 && nwqid+1 < nwname)
1062 deb971f5 2021-08-12 op goto cantopen;
1063 deb971f5 2021-08-12 op
1064 deb971f5 2021-08-12 op /* reached the end and found a file */
1065 deb971f5 2021-08-12 op if (fd == -1 && nwqid+1 == nwname)
1066 deb971f5 2021-08-12 op continue;
1067 deb971f5 2021-08-12 op
1068 deb971f5 2021-08-12 op if (oldfd != f->qid->fd)
1069 deb971f5 2021-08-12 op close(oldfd);
1070 deb971f5 2021-08-12 op oldfd = fd;
1071 1c08fc54 2021-08-01 op }
1072 1c08fc54 2021-08-01 op
1073 deb971f5 2021-08-12 op /*
1074 deb971f5 2021-08-12 op * There can be two possibilities: fd == -1 means that we've
1075 deb971f5 2021-08-12 op * reached a file and we should save BOTH the dirfd (oldfd)
1076 deb971f5 2021-08-12 op * and the path (wnam); or we just reached another directory,
1077 deb971f5 2021-08-12 op * in which case we can just create a new qid using fd.
1078 deb971f5 2021-08-12 op */
1079 deb971f5 2021-08-12 op if (fd == -1)
1080 deb971f5 2021-08-12 op qid = qid_from_fd(oldfd, wnam, &sb);
1081 deb971f5 2021-08-12 op else
1082 deb971f5 2021-08-12 op qid = qid_from_fd(oldfd, NULL, &sb);
1083 deb971f5 2021-08-12 op if (qid == NULL)
1084 eafb23b1 2021-08-01 op fatal("qid_from_fd");
1085 1c08fc54 2021-08-01 op
1086 1c08fc54 2021-08-01 op if (nf == NULL) {
1087 1c08fc54 2021-08-01 op if ((nf = new_fid(qid, newfid)) == NULL)
1088 1c08fc54 2021-08-01 op fatal("new_fid");
1089 1c08fc54 2021-08-01 op } else {
1090 1c08fc54 2021-08-01 op /* swap qid */
1091 1c08fc54 2021-08-01 op qid_decref(nf->qid);
1092 1c08fc54 2021-08-01 op nf->qid = qid_incref(qid);
1093 1c08fc54 2021-08-01 op }
1094 1c08fc54 2021-08-01 op
1095 1c08fc54 2021-08-01 op np_walk(hdr->tag, nwqid, wqid);
1096 deb971f5 2021-08-12 op return;
1097 1c08fc54 2021-08-01 op
1098 deb971f5 2021-08-12 op cantopen:
1099 deb971f5 2021-08-12 op if (oldfd != f->qid->fd)
1100 deb971f5 2021-08-12 op close(oldfd);
1101 deb971f5 2021-08-12 op no = errno;
1102 deb971f5 2021-08-12 op if (nwqid == 0)
1103 deb971f5 2021-08-12 op np_error(hdr->tag, strerror(no));
1104 deb971f5 2021-08-12 op else
1105 deb971f5 2021-08-12 op np_walk(hdr->tag, nwqid, wqid);
1106 1c08fc54 2021-08-01 op return;
1107 1c08fc54 2021-08-01 op
1108 1c08fc54 2021-08-01 op err:
1109 1c08fc54 2021-08-01 op client_send_listener(IMSG_CLOSE, NULL, 0);
1110 1c08fc54 2021-08-01 op client_shutdown();
1111 021481ca 2021-12-13 op }
1112 021481ca 2021-12-13 op
1113 021481ca 2021-12-13 op static inline int
1114 021481ca 2021-12-13 op npmode_to_unix(uint8_t mode, int *flags)
1115 021481ca 2021-12-13 op {
1116 021481ca 2021-12-13 op switch (mode & 0x0F) {
1117 021481ca 2021-12-13 op case KOREAD:
1118 021481ca 2021-12-13 op *flags = O_RDONLY;
1119 021481ca 2021-12-13 op break;
1120 021481ca 2021-12-13 op case KOWRITE:
1121 021481ca 2021-12-13 op *flags = O_WRONLY;
1122 021481ca 2021-12-13 op break;
1123 021481ca 2021-12-13 op case KORDWR:
1124 021481ca 2021-12-13 op *flags = O_RDWR;
1125 021481ca 2021-12-13 op break;
1126 021481ca 2021-12-13 op case KOEXEC:
1127 021481ca 2021-12-13 op log_warnx("tried to open something with KOEXEC");
1128 021481ca 2021-12-13 op /* fallthrough */
1129 021481ca 2021-12-13 op default:
1130 021481ca 2021-12-13 op return -1;
1131 021481ca 2021-12-13 op }
1132 021481ca 2021-12-13 op
1133 021481ca 2021-12-13 op if (mode & KOTRUNC)
1134 021481ca 2021-12-13 op *flags |= O_TRUNC;
1135 021481ca 2021-12-13 op if (mode & KORCLOSE)
1136 021481ca 2021-12-13 op *flags |= O_CLOEXEC;
1137 021481ca 2021-12-13 op
1138 021481ca 2021-12-13 op return 0;
1139 1c08fc54 2021-08-01 op }
1140 1c08fc54 2021-08-01 op
1141 1c08fc54 2021-08-01 op static void
1142 021481ca 2021-12-13 op topen(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1143 021481ca 2021-12-13 op {
1144 021481ca 2021-12-13 op struct stat sb;
1145 021481ca 2021-12-13 op struct qid qid;
1146 021481ca 2021-12-13 op struct fid *f;
1147 021481ca 2021-12-13 op uint32_t fid;
1148 021481ca 2021-12-13 op uint8_t mode;
1149 87a818e8 2021-12-14 op const char *path;
1150 021481ca 2021-12-13 op
1151 021481ca 2021-12-13 op /* fid[4] mode[1] */
1152 021481ca 2021-12-13 op if (!NPREAD32("fid", &fid, &data, &len) ||
1153 021481ca 2021-12-13 op !NPREAD8("mode", &mode, &data, &len)) {
1154 021481ca 2021-12-13 op client_send_listener(IMSG_CLOSE, NULL, 0);
1155 021481ca 2021-12-13 op client_shutdown();
1156 021481ca 2021-12-13 op return;
1157 021481ca 2021-12-13 op }
1158 021481ca 2021-12-13 op
1159 021481ca 2021-12-13 op if ((f = fid_by_id(fid)) == NULL || f->fd != -1) {
1160 021481ca 2021-12-13 op np_error(hdr->tag, "invalid fid");
1161 021481ca 2021-12-13 op return;
1162 021481ca 2021-12-13 op }
1163 021481ca 2021-12-13 op
1164 d4e43815 2021-12-15 op if (npmode_to_unix(mode, &f->iomode) == -1) {
1165 d4e43815 2021-12-15 op np_error(hdr->tag, "invalid mode");
1166 021481ca 2021-12-13 op return;
1167 021481ca 2021-12-13 op }
1168 021481ca 2021-12-13 op
1169 d4e43815 2021-12-15 op if (f->qid->type & QTDIR &&
1170 d4e43815 2021-12-15 op (f->iomode & O_WRONLY || f->iomode & O_RDWR)) {
1171 d4e43815 2021-12-15 op np_error(hdr->tag, "can't open directory for writing");
1172 021481ca 2021-12-13 op return;
1173 021481ca 2021-12-13 op }
1174 021481ca 2021-12-13 op
1175 87a818e8 2021-12-14 op path = f->qid->fpath;
1176 87a818e8 2021-12-14 op if (*path == '\0')
1177 87a818e8 2021-12-14 op path = ".";
1178 87a818e8 2021-12-14 op
1179 87a818e8 2021-12-14 op if ((f->fd = openat(f->qid->fd, path, f->iomode)) == -1) {
1180 021481ca 2021-12-13 op np_error(hdr->tag, strerror(errno));
1181 021481ca 2021-12-13 op return;
1182 021481ca 2021-12-13 op }
1183 021481ca 2021-12-13 op
1184 021481ca 2021-12-13 op if (fstat(f->fd, &sb) == -1)
1185 021481ca 2021-12-13 op fatal("fstat");
1186 021481ca 2021-12-13 op
1187 baa40504 2021-12-15 op if (S_ISDIR(sb.st_mode)) {
1188 baa40504 2021-12-15 op assert(f->qid->type & QTDIR);
1189 baa40504 2021-12-15 op if ((f->dir = fdopendir(f->fd)) == NULL) {
1190 baa40504 2021-12-15 op np_errno(hdr->tag);
1191 baa40504 2021-12-15 op close(f->fd);
1192 baa40504 2021-12-15 op f->fd = -1;
1193 baa40504 2021-12-15 op return;
1194 baa40504 2021-12-15 op }
1195 baa40504 2021-12-15 op
1196 c0ae57b9 2021-12-15 op if ((f->evb = evbuffer_new()) == NULL) {
1197 c0ae57b9 2021-12-15 op np_errno(hdr->tag);
1198 c0ae57b9 2021-12-15 op closedir(f->dir);
1199 c0ae57b9 2021-12-15 op f->dir = NULL;
1200 c0ae57b9 2021-12-15 op f->fd = -1;
1201 c0ae57b9 2021-12-15 op }
1202 baa40504 2021-12-15 op }
1203 baa40504 2021-12-15 op
1204 baa40504 2021-12-15 op f->offset = 0;
1205 baa40504 2021-12-15 op
1206 021481ca 2021-12-13 op qid_update_from_sb(&qid, &sb);
1207 021481ca 2021-12-13 op np_open(hdr->tag, &qid, sb.st_blksize);
1208 021481ca 2021-12-13 op }
1209 021481ca 2021-12-13 op
1210 5585f1c3 2021-12-16 op static inline void
1211 3a2c53f5 2021-12-18 op serialize_stat(const char *fname, struct stat *sb, struct evbuffer *evb)
1212 5585f1c3 2021-12-16 op {
1213 5585f1c3 2021-12-16 op struct qid qid;
1214 5585f1c3 2021-12-16 op const char *uid, *gid, *muid;
1215 5585f1c3 2021-12-16 op size_t tot;
1216 5585f1c3 2021-12-16 op uint16_t namlen, uidlen, gidlen, ulen;
1217 5585f1c3 2021-12-16 op
1218 3a2c53f5 2021-12-18 op qid_update_from_sb(&qid, sb);
1219 5585f1c3 2021-12-16 op
1220 5585f1c3 2021-12-16 op /* TODO: fill these fields */
1221 5585f1c3 2021-12-16 op uid = "";
1222 5585f1c3 2021-12-16 op gid = "";
1223 5585f1c3 2021-12-16 op muid = "";
1224 5585f1c3 2021-12-16 op
1225 3a2c53f5 2021-12-18 op namlen = strlen(fname);
1226 5585f1c3 2021-12-16 op uidlen = strlen(uid);
1227 5585f1c3 2021-12-16 op gidlen = strlen(gid);
1228 5585f1c3 2021-12-16 op ulen = strlen(muid);
1229 5585f1c3 2021-12-16 op
1230 5585f1c3 2021-12-16 op tot = NPSTATSIZ(namlen, uidlen, gidlen, ulen);
1231 5585f1c3 2021-12-16 op if (tot > UINT32_MAX) {
1232 5585f1c3 2021-12-16 op log_warnx("stat info for dir entry %s would overflow",
1233 3a2c53f5 2021-12-18 op fname);
1234 5585f1c3 2021-12-16 op return;
1235 5585f1c3 2021-12-16 op }
1236 5585f1c3 2021-12-16 op
1237 5585f1c3 2021-12-16 op np_write16(evb, tot); /* size[2] */
1238 3a2c53f5 2021-12-18 op np_write16(evb, sb->st_rdev); /* type[2] */
1239 3a2c53f5 2021-12-18 op np_write32(evb, sb->st_dev); /* dev[4] */
1240 5585f1c3 2021-12-16 op np_qid(evb, &qid); /* qid[13] */
1241 5585f1c3 2021-12-16 op
1242 5585f1c3 2021-12-16 op /* XXX: translate? */
1243 3a2c53f5 2021-12-18 op np_write32(evb, sb->st_mode); /* mode[4] */
1244 5585f1c3 2021-12-16 op
1245 3a2c53f5 2021-12-18 op np_write32(evb, sb->st_atim.tv_sec); /* atime[4] */
1246 3a2c53f5 2021-12-18 op np_write32(evb, sb->st_mtim.tv_sec); /* mtime[4] */
1247 3a2c53f5 2021-12-18 op np_write64(evb, sb->st_size); /* length[8] */
1248 3a2c53f5 2021-12-18 op np_string(evb, namlen, fname); /* name[s] */
1249 5585f1c3 2021-12-16 op np_string(evb, uidlen, uid); /* uid[s] */
1250 5585f1c3 2021-12-16 op np_string(evb, gidlen, gid); /* gid[s] */
1251 5585f1c3 2021-12-16 op np_string(evb, ulen, muid); /* muid[s] */
1252 5585f1c3 2021-12-16 op }
1253 5585f1c3 2021-12-16 op
1254 021481ca 2021-12-13 op static void
1255 baa40504 2021-12-15 op tread(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1256 baa40504 2021-12-15 op {
1257 baa40504 2021-12-15 op struct fid *f;
1258 baa40504 2021-12-15 op ssize_t r;
1259 baa40504 2021-12-15 op uint64_t off;
1260 baa40504 2021-12-15 op uint32_t fid, count;
1261 baa40504 2021-12-15 op char buf[2048];
1262 baa40504 2021-12-15 op
1263 baa40504 2021-12-15 op /* fid[4] offset[8] count[4] */
1264 baa40504 2021-12-15 op if (!NPREAD32("fid", &fid, &data, &len) ||
1265 baa40504 2021-12-15 op !NPREAD64("offset", &off, &data, &len) ||
1266 baa40504 2021-12-15 op !NPREAD32("count", &count, &data, &len)) {
1267 baa40504 2021-12-15 op client_send_listener(IMSG_CLOSE, NULL, 0);
1268 baa40504 2021-12-15 op client_shutdown();
1269 baa40504 2021-12-15 op return;
1270 baa40504 2021-12-15 op }
1271 baa40504 2021-12-15 op
1272 5f362b7f 2021-12-15 op if ((f = fid_by_id(fid)) == NULL || f->fd == -1) {
1273 baa40504 2021-12-15 op np_error(hdr->tag, "invalid fid");
1274 baa40504 2021-12-15 op return;
1275 baa40504 2021-12-15 op }
1276 baa40504 2021-12-15 op
1277 baa40504 2021-12-15 op if (TYPE_OVERFLOW(off_t, off)) {
1278 baa40504 2021-12-15 op log_warnx("unexpected size_t size");
1279 baa40504 2021-12-15 op np_error(hdr->tag, "invalid offset");
1280 baa40504 2021-12-15 op return;
1281 baa40504 2021-12-15 op }
1282 baa40504 2021-12-15 op
1283 baa40504 2021-12-15 op if (f->dir == NULL) {
1284 baa40504 2021-12-15 op /* read a file */
1285 baa40504 2021-12-15 op r = pread(f->fd, buf, sizeof(buf), (off_t)off);
1286 baa40504 2021-12-15 op if (r == -1)
1287 baa40504 2021-12-15 op np_errno(hdr->tag);
1288 baa40504 2021-12-15 op else
1289 baa40504 2021-12-15 op np_read(hdr->tag, r, buf);
1290 baa40504 2021-12-15 op } else {
1291 fcf8ca15 2021-12-16 op if (off == 0 && f->offset != 0) {
1292 fcf8ca15 2021-12-16 op rewinddir(f->dir);
1293 fcf8ca15 2021-12-16 op f->offset = 0;
1294 fcf8ca15 2021-12-16 op }
1295 fcf8ca15 2021-12-16 op
1296 5585f1c3 2021-12-16 op if (off != f->offset) {
1297 5585f1c3 2021-12-16 op np_error(hdr->tag, "can't seek in directories");
1298 5585f1c3 2021-12-16 op return;
1299 5585f1c3 2021-12-16 op }
1300 5585f1c3 2021-12-16 op
1301 5585f1c3 2021-12-16 op while (EVBUFFER_LENGTH(f->evb) < count) {
1302 5585f1c3 2021-12-16 op struct dirent *d;
1303 3a2c53f5 2021-12-18 op struct stat sb;
1304 5585f1c3 2021-12-16 op
1305 5585f1c3 2021-12-16 op if ((d = readdir(f->dir)) == NULL)
1306 5585f1c3 2021-12-16 op break;
1307 3a2c53f5 2021-12-18 op if (fstatat(f->fd, d->d_name, &sb, 0) == -1) {
1308 3a2c53f5 2021-12-18 op warn("fstatat");
1309 3a2c53f5 2021-12-18 op continue;
1310 3a2c53f5 2021-12-18 op }
1311 3a2c53f5 2021-12-18 op serialize_stat(d->d_name, &sb, f->evb);
1312 5585f1c3 2021-12-16 op }
1313 5585f1c3 2021-12-16 op
1314 5585f1c3 2021-12-16 op count = MIN(count, EVBUFFER_LENGTH(f->evb));
1315 5585f1c3 2021-12-16 op np_read(hdr->tag, count, EVBUFFER_DATA(f->evb));
1316 5585f1c3 2021-12-16 op evbuffer_drain(f->evb, count);
1317 5585f1c3 2021-12-16 op
1318 5585f1c3 2021-12-16 op f->offset += count;
1319 2483be55 2021-12-16 op }
1320 2483be55 2021-12-16 op }
1321 2483be55 2021-12-16 op
1322 2483be55 2021-12-16 op static void
1323 2483be55 2021-12-16 op twrite(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1324 2483be55 2021-12-16 op {
1325 2483be55 2021-12-16 op struct fid *f;
1326 2483be55 2021-12-16 op ssize_t r;
1327 2483be55 2021-12-16 op uint64_t off;
1328 2483be55 2021-12-16 op uint32_t fid, count;
1329 2483be55 2021-12-16 op
1330 2483be55 2021-12-16 op /* fid[4] offset[8] count[4] data[count] */
1331 2483be55 2021-12-16 op if (!NPREAD32("fid", &fid, &data, &len) ||
1332 2483be55 2021-12-16 op !NPREAD64("off", &off, &data, &len) ||
1333 2483be55 2021-12-16 op !NPREAD32("count", &count, &data, &len) ||
1334 2483be55 2021-12-16 op len != count) {
1335 2483be55 2021-12-16 op client_send_listener(IMSG_CLOSE, NULL, 0);
1336 2483be55 2021-12-16 op client_shutdown();
1337 2483be55 2021-12-16 op return;
1338 baa40504 2021-12-15 op }
1339 2483be55 2021-12-16 op
1340 2483be55 2021-12-16 op if ((f = fid_by_id(fid)) == NULL || f->fd == -1) {
1341 2483be55 2021-12-16 op np_error(hdr->tag, "invalid fid");
1342 2483be55 2021-12-16 op return;
1343 2483be55 2021-12-16 op }
1344 2483be55 2021-12-16 op
1345 2483be55 2021-12-16 op if (!(f->iomode & O_WRONLY) &&
1346 2483be55 2021-12-16 op !(f->iomode & O_RDWR)) {
1347 2483be55 2021-12-16 op np_error(hdr->tag, "fid not opened for writing");
1348 2483be55 2021-12-16 op return;
1349 2483be55 2021-12-16 op }
1350 2483be55 2021-12-16 op
1351 2483be55 2021-12-16 op if (TYPE_OVERFLOW(off_t, off)) {
1352 2483be55 2021-12-16 op log_warnx("unexpected off_t size");
1353 2483be55 2021-12-16 op np_error(hdr->tag, "invalid offset");
1354 2483be55 2021-12-16 op return;
1355 2483be55 2021-12-16 op }
1356 2483be55 2021-12-16 op
1357 6ace3b52 2021-12-17 op if ((r = pwrite(f->fd, data, len, off)) == -1)
1358 2483be55 2021-12-16 op np_errno(hdr->tag);
1359 2483be55 2021-12-16 op else
1360 2483be55 2021-12-16 op np_write(hdr->tag, r);
1361 3a2c53f5 2021-12-18 op }
1362 3a2c53f5 2021-12-18 op
1363 3a2c53f5 2021-12-18 op static void
1364 3a2c53f5 2021-12-18 op tstat(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1365 3a2c53f5 2021-12-18 op {
1366 3a2c53f5 2021-12-18 op struct evbuffer *evb;
1367 3a2c53f5 2021-12-18 op struct stat sb;
1368 3a2c53f5 2021-12-18 op struct fid *f;
1369 3a2c53f5 2021-12-18 op const char *fname;
1370 3a2c53f5 2021-12-18 op int r;
1371 3a2c53f5 2021-12-18 op uint32_t fid;
1372 3a2c53f5 2021-12-18 op
1373 3a2c53f5 2021-12-18 op /* fid[4] */
1374 3a2c53f5 2021-12-18 op if (!NPREAD32("fid", &fid, &data, &len)) {
1375 3a2c53f5 2021-12-18 op client_send_listener(IMSG_CLOSE, NULL, 0);
1376 3a2c53f5 2021-12-18 op client_shutdown();
1377 3a2c53f5 2021-12-18 op return;
1378 3a2c53f5 2021-12-18 op }
1379 3a2c53f5 2021-12-18 op
1380 3a2c53f5 2021-12-18 op /*
1381 3a2c53f5 2021-12-18 op * plan9' stat(9P) is not clear on whether the stat is allowed
1382 3a2c53f5 2021-12-18 op * on opened fids or not.
1383 3a2c53f5 2021-12-18 op */
1384 3a2c53f5 2021-12-18 op if ((f = fid_by_id(fid)) == NULL) {
1385 3a2c53f5 2021-12-18 op np_error(hdr->tag, "invalid fid");
1386 3a2c53f5 2021-12-18 op return;
1387 3a2c53f5 2021-12-18 op }
1388 3a2c53f5 2021-12-18 op
1389 3a2c53f5 2021-12-18 op if ((evb = evbuffer_new()) == NULL)
1390 3a2c53f5 2021-12-18 op fatal("evbuffer_new");
1391 3a2c53f5 2021-12-18 op
1392 3a2c53f5 2021-12-18 op if (f->qid->type & QTDIR) {
1393 3a2c53f5 2021-12-18 op fname = ".";
1394 3a2c53f5 2021-12-18 op r = fstat(f->qid->fd, &sb);
1395 3a2c53f5 2021-12-18 op } else {
1396 3a2c53f5 2021-12-18 op fname = f->qid->fpath;
1397 3a2c53f5 2021-12-18 op r = fstatat(f->qid->fd, f->qid->fpath, &sb, 0);
1398 3a2c53f5 2021-12-18 op }
1399 3a2c53f5 2021-12-18 op
1400 3a2c53f5 2021-12-18 op if (r == -1) {
1401 3a2c53f5 2021-12-18 op np_errno(hdr->tag);
1402 3a2c53f5 2021-12-18 op evbuffer_free(evb);
1403 3a2c53f5 2021-12-18 op return;
1404 3a2c53f5 2021-12-18 op }
1405 3a2c53f5 2021-12-18 op
1406 3a2c53f5 2021-12-18 op serialize_stat(fname, &sb, evb);
1407 3a2c53f5 2021-12-18 op np_stat(hdr->tag, EVBUFFER_LENGTH(evb), EVBUFFER_DATA(evb));
1408 3a2c53f5 2021-12-18 op evbuffer_free(evb);
1409 baa40504 2021-12-15 op }
1410 baa40504 2021-12-15 op
1411 baa40504 2021-12-15 op static void
1412 e60f4e08 2021-07-30 op handle_message(struct imsg *imsg, size_t len)
1413 e60f4e08 2021-07-30 op {
1414 e60f4e08 2021-07-30 op struct msg {
1415 e60f4e08 2021-07-30 op uint8_t type;
1416 e60f4e08 2021-07-30 op void (*fn)(struct np_msg_header *, const uint8_t *, size_t);
1417 e60f4e08 2021-07-30 op } msgs[] = {
1418 e60f4e08 2021-07-30 op {Tversion, tversion},
1419 e60f4e08 2021-07-30 op {Tattach, tattach},
1420 c377c1b9 2021-07-30 op {Tclunk, tclunk},
1421 36b30273 2021-07-30 op {Tflush, tflush},
1422 1c08fc54 2021-08-01 op {Twalk, twalk},
1423 021481ca 2021-12-13 op {Topen, topen},
1424 baa40504 2021-12-15 op {Tread, tread},
1425 2483be55 2021-12-16 op {Twrite, twrite},
1426 3a2c53f5 2021-12-18 op {Tstat, tstat},
1427 e60f4e08 2021-07-30 op };
1428 e60f4e08 2021-07-30 op struct np_msg_header hdr;
1429 e60f4e08 2021-07-30 op size_t i;
1430 e60f4e08 2021-07-30 op uint8_t *data;
1431 64c19d90 2021-07-30 op
1432 64c19d90 2021-07-30 op #if DEBUG_PACKETS
1433 78b94752 2021-08-01 op hexdump("incoming packet", imsg->data, len);
1434 64c19d90 2021-07-30 op #endif
1435 e60f4e08 2021-07-30 op
1436 e60f4e08 2021-07-30 op parse_message(imsg->data, len, &hdr, &data);
1437 e60f4e08 2021-07-30 op len -= HEADERSIZE;
1438 e60f4e08 2021-07-30 op
1439 e60f4e08 2021-07-30 op log_debug("got request: len=%d type=%d[%s] tag=%d",
1440 e60f4e08 2021-07-30 op hdr.len, hdr.type, pp_msg_type(hdr.type), hdr.tag);
1441 e0dce04c 2021-07-30 op
1442 e60f4e08 2021-07-30 op if (!handshaked && hdr.type != Tversion) {
1443 e60f4e08 2021-07-30 op client_send_listener(IMSG_CLOSE, NULL, 0);
1444 e60f4e08 2021-07-30 op client_shutdown();
1445 e60f4e08 2021-07-30 op return;
1446 e60f4e08 2021-07-30 op }
1447 e60f4e08 2021-07-30 op
1448 e60f4e08 2021-07-30 op for (i = 0; i < sizeof(msgs)/sizeof(msgs[0]); ++i) {
1449 e60f4e08 2021-07-30 op if (msgs[i].type != hdr.type)
1450 e60f4e08 2021-07-30 op continue;
1451 e60f4e08 2021-07-30 op
1452 e60f4e08 2021-07-30 op msgs[i].fn(&hdr, data, len);
1453 e60f4e08 2021-07-30 op return;
1454 e60f4e08 2021-07-30 op }
1455 e60f4e08 2021-07-30 op
1456 e60f4e08 2021-07-30 op np_error(hdr.tag, "Not supported.");
1457 64c19d90 2021-07-30 op }