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