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