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