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