Blame


1 fb1a36c0 2022-01-09 op /*
2 1e84c7b7 2022-01-10 op * Copyright (c) 2021, 2022 Omar Polo <op@omarpolo.com>
3 fb1a36c0 2022-01-09 op *
4 fb1a36c0 2022-01-09 op * Permission to use, copy, modify, and distribute this software for any
5 fb1a36c0 2022-01-09 op * purpose with or without fee is hereby granted, provided that the above
6 fb1a36c0 2022-01-09 op * copyright notice and this permission notice appear in all copies.
7 fb1a36c0 2022-01-09 op *
8 fb1a36c0 2022-01-09 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 fb1a36c0 2022-01-09 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 fb1a36c0 2022-01-09 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 fb1a36c0 2022-01-09 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 fb1a36c0 2022-01-09 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 fb1a36c0 2022-01-09 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 fb1a36c0 2022-01-09 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 fb1a36c0 2022-01-09 op */
16 fb1a36c0 2022-01-09 op
17 bbcba3ed 2022-01-10 op #include "compat.h"
18 bbcba3ed 2022-01-10 op
19 fb1a36c0 2022-01-09 op #include <sys/stat.h>
20 fb1a36c0 2022-01-09 op #include <sys/types.h>
21 fb1a36c0 2022-01-09 op #include <sys/uio.h>
22 fb1a36c0 2022-01-09 op
23 fb1a36c0 2022-01-09 op #include <dirent.h>
24 fb1a36c0 2022-01-09 op #include <endian.h>
25 fb1a36c0 2022-01-09 op #include <errno.h>
26 fb1a36c0 2022-01-09 op #include <fcntl.h>
27 755d75bf 2022-01-17 op #include <inttypes.h>
28 755d75bf 2022-01-17 op #include <libgen.h>
29 fb1a36c0 2022-01-09 op #include <pwd.h>
30 fb1a36c0 2022-01-09 op #include <signal.h>
31 fb1a36c0 2022-01-09 op #include <stdint.h>
32 755d75bf 2022-01-17 op #include <stdio.h>
33 fb1a36c0 2022-01-09 op #include <stdlib.h>
34 fb1a36c0 2022-01-09 op #include <string.h>
35 fb1a36c0 2022-01-09 op #include <syslog.h>
36 fb1a36c0 2022-01-09 op #include <unistd.h>
37 fb1a36c0 2022-01-09 op
38 fb1a36c0 2022-01-09 op #include "client.h"
39 fb1a36c0 2022-01-09 op #include "kami.h"
40 fb1a36c0 2022-01-09 op #include "kamid.h"
41 fb1a36c0 2022-01-09 op #include "log.h"
42 fb1a36c0 2022-01-09 op #include "sandbox.h"
43 fb1a36c0 2022-01-09 op #include "utils.h"
44 fbeaf87b 2022-05-22 op
45 fbeaf87b 2022-05-22 op /*
46 fbeaf87b 2022-05-22 op * Max message size aviable via a single imsg.
47 fbeaf87b 2022-05-22 op */
48 fbeaf87b 2022-05-22 op #define IMSG_MAXSIZE (MAX_IMSGSIZE - IMSG_HEADER_SIZE)
49 fb1a36c0 2022-01-09 op
50 fb1a36c0 2022-01-09 op /*
51 6be59daa 2022-05-22 op * The minimum value allowed for the msize.
52 6be59daa 2022-05-22 op */
53 6be59daa 2022-05-22 op #define MIN_MSIZE 256
54 fb1a36c0 2022-01-09 op
55 fb1a36c0 2022-01-09 op #define DEBUG_PACKETS 0
56 fb1a36c0 2022-01-09 op
57 fb1a36c0 2022-01-09 op /* straight outta /src/usr.bin/ssh/scp.c */
58 fb1a36c0 2022-01-09 op #define TYPE_OVERFLOW(type, val) \
59 fb1a36c0 2022-01-09 op ((sizeof(type) == 4 && (val) > INT32_MAX) || \
60 fb1a36c0 2022-01-09 op (sizeof(type) == 8 && (val) > INT64_MAX) || \
61 fb1a36c0 2022-01-09 op (sizeof(type) != 4 && sizeof(type) != 8))
62 fb1a36c0 2022-01-09 op
63 fb1a36c0 2022-01-09 op STAILQ_HEAD(dirhead, dir) dirs;
64 fb1a36c0 2022-01-09 op struct dir {
65 fb1a36c0 2022-01-09 op int refcount;
66 fb1a36c0 2022-01-09 op int fd;
67 fb1a36c0 2022-01-09 op STAILQ_ENTRY(dir) entries;
68 fb1a36c0 2022-01-09 op };
69 fb1a36c0 2022-01-09 op
70 fb1a36c0 2022-01-09 op STAILQ_HEAD(fidhead, fid) fids;
71 fb1a36c0 2022-01-09 op struct fid {
72 fb1a36c0 2022-01-09 op uint32_t fid;
73 fb1a36c0 2022-01-09 op
74 46e4e74b 2022-01-17 op char fname[NAME_MAX];
75 fb1a36c0 2022-01-09 op
76 fb1a36c0 2022-01-09 op /*
77 fb1a36c0 2022-01-09 op * the flags passed to open(2). O_CLOEXEC means ORCLOSE, that
78 fb1a36c0 2022-01-09 op * is to unlink the file upon Tclunk.
79 fb1a36c0 2022-01-09 op */
80 fb1a36c0 2022-01-09 op int iomode;
81 fb1a36c0 2022-01-09 op
82 fb1a36c0 2022-01-09 op /*
83 fb1a36c0 2022-01-09 op * if fd is not -1 this fid was opened, fd represents its
84 fb1a36c0 2022-01-09 op * file descriptor and iomode the flags passed to open(2).
85 fb1a36c0 2022-01-09 op */
86 fb1a36c0 2022-01-09 op int fd;
87 fb1a36c0 2022-01-09 op DIR *d;
88 fb1a36c0 2022-01-09 op struct evbuffer *evb;
89 fb1a36c0 2022-01-09 op
90 fb1a36c0 2022-01-09 op /*
91 fb1a36c0 2022-01-09 op * expected offset for Tread against a directory.
92 fb1a36c0 2022-01-09 op */
93 fb1a36c0 2022-01-09 op uint64_t offset;
94 fb1a36c0 2022-01-09 op
95 fb1a36c0 2022-01-09 op struct qid qid;
96 fb1a36c0 2022-01-09 op struct dir *dir;
97 fb1a36c0 2022-01-09 op STAILQ_ENTRY(fid) entries;
98 fb1a36c0 2022-01-09 op };
99 fb1a36c0 2022-01-09 op
100 fb1a36c0 2022-01-09 op static struct imsgev *iev_listener;
101 fb1a36c0 2022-01-09 op static struct evbuffer *evb;
102 fb1a36c0 2022-01-09 op static uint32_t peerid;
103 fb1a36c0 2022-01-09 op
104 fb1a36c0 2022-01-09 op static int handshaked;
105 fb1a36c0 2022-01-09 op uint32_t msize;
106 fb1a36c0 2022-01-09 op
107 fb1a36c0 2022-01-09 op static __dead void client_shutdown(void);
108 fb1a36c0 2022-01-09 op static void client_sig_handler(int, short, void *);
109 fb1a36c0 2022-01-09 op static void client_dispatch_listener(int, short, void *);
110 fb1a36c0 2022-01-09 op static void client_privdrop(const char *, const char *);
111 fb1a36c0 2022-01-09 op
112 5c69b561 2022-01-29 op static int client_send_listenerp(int, uint32_t, const void *, uint16_t);
113 fb1a36c0 2022-01-09 op static int client_send_listener(int, const void *, uint16_t);
114 fb1a36c0 2022-01-09 op
115 fb1a36c0 2022-01-09 op static void qid_update_from_sb(struct qid *, struct stat *);
116 fb1a36c0 2022-01-09 op
117 fb1a36c0 2022-01-09 op static struct dir *new_dir(int);
118 fb1a36c0 2022-01-09 op static struct dir *dir_incref(struct dir *);
119 fb1a36c0 2022-01-09 op static void dir_decref(struct dir *);
120 fb1a36c0 2022-01-09 op
121 fb1a36c0 2022-01-09 op static struct fid *new_fid(struct dir *, uint32_t, const char *, struct qid *);
122 fb1a36c0 2022-01-09 op static struct fid *fid_by_id(uint32_t);
123 fb1a36c0 2022-01-09 op static void free_fid(struct fid *);
124 fb1a36c0 2022-01-09 op
125 fb1a36c0 2022-01-09 op static void parse_message(const uint8_t *, size_t,
126 fb1a36c0 2022-01-09 op struct np_msg_header *, uint8_t **);
127 fb1a36c0 2022-01-09 op
128 fb1a36c0 2022-01-09 op static void np_write16(struct evbuffer *, uint16_t);
129 fb1a36c0 2022-01-09 op static void np_write32(struct evbuffer *, uint32_t);
130 fb1a36c0 2022-01-09 op static void np_write64(struct evbuffer *, uint64_t);
131 fb1a36c0 2022-01-09 op static void np_header(uint32_t, uint8_t, uint16_t);
132 fb1a36c0 2022-01-09 op static void np_string(struct evbuffer *, uint16_t, const char *);
133 fb1a36c0 2022-01-09 op static void np_qid(struct evbuffer *, struct qid *);
134 fb1a36c0 2022-01-09 op static void do_send(void);
135 fb1a36c0 2022-01-09 op
136 fb1a36c0 2022-01-09 op static void np_version(uint16_t, uint32_t, const char *);
137 fb1a36c0 2022-01-09 op static void np_attach(uint16_t, struct qid *);
138 fb1a36c0 2022-01-09 op static void np_clunk(uint16_t);
139 fb1a36c0 2022-01-09 op static void np_flush(uint16_t);
140 fb1a36c0 2022-01-09 op static void np_walk(uint16_t, int, struct qid *);
141 fb1a36c0 2022-01-09 op static void np_open(uint16_t, struct qid *, uint32_t);
142 fb1a36c0 2022-01-09 op static void np_create(uint16_t, struct qid *, uint32_t);
143 fb1a36c0 2022-01-09 op static void np_read(uint16_t, uint32_t, void *);
144 fb1a36c0 2022-01-09 op static void np_write(uint16_t, uint32_t);
145 70540bb5 2022-02-02 op static void np_stat(uint16_t, uint16_t, void *);
146 755d75bf 2022-01-17 op static void np_wstat(uint16_t);
147 fb1a36c0 2022-01-09 op static void np_remove(uint16_t);
148 fb1a36c0 2022-01-09 op static void np_error(uint16_t, const char *);
149 fb1a36c0 2022-01-09 op static void np_errno(uint16_t);
150 fb1a36c0 2022-01-09 op
151 fb1a36c0 2022-01-09 op static int np_read8(const char *, const char *, uint8_t *,
152 fb1a36c0 2022-01-09 op const uint8_t **, size_t *);
153 fb1a36c0 2022-01-09 op static int np_read16(const char *, const char *, uint16_t *,
154 fb1a36c0 2022-01-09 op const uint8_t **, size_t *);
155 fb1a36c0 2022-01-09 op static int np_read32(const char *, const char *, uint32_t *,
156 fb1a36c0 2022-01-09 op const uint8_t **, size_t *);
157 fb1a36c0 2022-01-09 op static int np_read64(const char *, const char *, uint64_t *,
158 fb1a36c0 2022-01-09 op const uint8_t **, size_t *);
159 fb1a36c0 2022-01-09 op
160 fb1a36c0 2022-01-09 op #define READSTRERR -1
161 fb1a36c0 2022-01-09 op #define READSTRTRUNC -2
162 fb1a36c0 2022-01-09 op static int np_readstr(const char *, const char *, char *, size_t,
163 fb1a36c0 2022-01-09 op const uint8_t **, size_t *);
164 fb1a36c0 2022-01-09 op
165 755d75bf 2022-01-17 op static int np_readst(const char *, const char *, struct np_stat *,
166 755d75bf 2022-01-17 op char *, size_t, const uint8_t **, size_t *);
167 755d75bf 2022-01-17 op
168 fb1a36c0 2022-01-09 op #define NPREAD8(f, dst, src, len) np_read8(__func__, f, dst, src, len)
169 fb1a36c0 2022-01-09 op #define NPREAD16(f, dst, src, len) np_read16(__func__, f, dst, src, len)
170 fb1a36c0 2022-01-09 op #define NPREAD32(f, dst, src, len) np_read32(__func__, f, dst, src, len)
171 fb1a36c0 2022-01-09 op #define NPREAD64(f, dst, src, len) np_read64(__func__, f, dst, src, len)
172 fb1a36c0 2022-01-09 op
173 fb1a36c0 2022-01-09 op #define NPREADSTR(f, b, bl, src, len) np_readstr(__func__, f, b, bl, src, len)
174 fb1a36c0 2022-01-09 op
175 755d75bf 2022-01-17 op #define NPREADST(f, dst, n, nl, src, len) np_readst(__func__, f, dst, n, nl, src, len)
176 755d75bf 2022-01-17 op
177 fb1a36c0 2022-01-09 op static void tversion(struct np_msg_header *, const uint8_t *, size_t);
178 fb1a36c0 2022-01-09 op static void tattach(struct np_msg_header *, const uint8_t *, size_t);
179 fb1a36c0 2022-01-09 op static void tclunk(struct np_msg_header *, const uint8_t *, size_t);
180 fb1a36c0 2022-01-09 op static void tflush(struct np_msg_header *, const uint8_t *, size_t);
181 fb1a36c0 2022-01-09 op static void twalk(struct np_msg_header *, const uint8_t *, size_t);
182 fb1a36c0 2022-01-09 op static void topen(struct np_msg_header *, const uint8_t *, size_t);
183 fb1a36c0 2022-01-09 op static void tcreate(struct np_msg_header *, const uint8_t *, size_t);
184 fb1a36c0 2022-01-09 op static void tread(struct np_msg_header *, const uint8_t *, size_t);
185 8b0a5062 2022-05-22 op static void twrite(struct np_msg_header *, const uint8_t *, size_t, struct fid **, off_t *, size_t *, int *);
186 8b0a5062 2022-05-22 op static void twrite_cont(struct fid *, off_t *, size_t *, int *, uint16_t, const uint8_t *, size_t);
187 fb1a36c0 2022-01-09 op static void tstat(struct np_msg_header *, const uint8_t *, size_t);
188 755d75bf 2022-01-17 op static void twstat(struct np_msg_header *, const uint8_t *, size_t);
189 fb1a36c0 2022-01-09 op static void tremove(struct np_msg_header *, const uint8_t *, size_t);
190 8b0a5062 2022-05-22 op static void handle_message(struct imsg *, size_t, int);
191 fb1a36c0 2022-01-09 op
192 fb1a36c0 2022-01-09 op __dead void
193 fb1a36c0 2022-01-09 op client(int debug, int verbose)
194 fb1a36c0 2022-01-09 op {
195 fb1a36c0 2022-01-09 op struct event ev_sigint, ev_sigterm;
196 fb1a36c0 2022-01-09 op
197 fb1a36c0 2022-01-09 op log_init(debug, LOG_DAEMON);
198 fb1a36c0 2022-01-09 op log_setverbose(verbose);
199 fb1a36c0 2022-01-09 op
200 fb1a36c0 2022-01-09 op setproctitle("client");
201 fb1a36c0 2022-01-09 op log_procinit("client");
202 fb1a36c0 2022-01-09 op
203 fb1a36c0 2022-01-09 op log_debug("warming up");
204 fb1a36c0 2022-01-09 op
205 fb1a36c0 2022-01-09 op event_init();
206 fb1a36c0 2022-01-09 op
207 fb1a36c0 2022-01-09 op /* Setup signal handlers */
208 fb1a36c0 2022-01-09 op signal_set(&ev_sigint, SIGINT, client_sig_handler, NULL);
209 fb1a36c0 2022-01-09 op signal_set(&ev_sigterm, SIGTERM, client_sig_handler, NULL);
210 fb1a36c0 2022-01-09 op
211 fb1a36c0 2022-01-09 op signal_add(&ev_sigint, NULL);
212 fb1a36c0 2022-01-09 op signal_add(&ev_sigterm, NULL);
213 fb1a36c0 2022-01-09 op
214 fb1a36c0 2022-01-09 op signal(SIGPIPE, SIG_IGN);
215 fb1a36c0 2022-01-09 op signal(SIGHUP, SIG_IGN);
216 fb1a36c0 2022-01-09 op
217 fb1a36c0 2022-01-09 op /* Setup pipe and event handler to the listener process */
218 fb1a36c0 2022-01-09 op if ((iev_listener = malloc(sizeof(*iev_listener))) == NULL)
219 fb1a36c0 2022-01-09 op fatal(NULL);
220 fb1a36c0 2022-01-09 op
221 fb1a36c0 2022-01-09 op imsg_init(&iev_listener->ibuf, 3);
222 fb1a36c0 2022-01-09 op iev_listener->handler = client_dispatch_listener;
223 fb1a36c0 2022-01-09 op
224 fb1a36c0 2022-01-09 op /* Setup event handlers. */
225 fb1a36c0 2022-01-09 op iev_listener->events = EV_READ;
226 fb1a36c0 2022-01-09 op event_set(&iev_listener->ev, iev_listener->ibuf.fd,
227 fb1a36c0 2022-01-09 op iev_listener->events, iev_listener->handler, iev_listener);
228 fb1a36c0 2022-01-09 op event_add(&iev_listener->ev, NULL);
229 fb1a36c0 2022-01-09 op
230 fb1a36c0 2022-01-09 op event_dispatch();
231 fb1a36c0 2022-01-09 op client_shutdown();
232 fb1a36c0 2022-01-09 op }
233 fb1a36c0 2022-01-09 op
234 fb1a36c0 2022-01-09 op static __dead void
235 fb1a36c0 2022-01-09 op client_shutdown(void)
236 fb1a36c0 2022-01-09 op {
237 fb1a36c0 2022-01-09 op if (evb != NULL)
238 fb1a36c0 2022-01-09 op evbuffer_free(evb);
239 fb1a36c0 2022-01-09 op
240 fb1a36c0 2022-01-09 op msgbuf_clear(&iev_listener->ibuf.w);
241 fb1a36c0 2022-01-09 op close(iev_listener->ibuf.fd);
242 fb1a36c0 2022-01-09 op
243 fb1a36c0 2022-01-09 op free(iev_listener);
244 fb1a36c0 2022-01-09 op
245 fb1a36c0 2022-01-09 op log_debug("client exiting");
246 fb1a36c0 2022-01-09 op exit(0);
247 fb1a36c0 2022-01-09 op }
248 fb1a36c0 2022-01-09 op
249 fb1a36c0 2022-01-09 op static void
250 fb1a36c0 2022-01-09 op client_sig_handler(int sig, short event, void *d)
251 fb1a36c0 2022-01-09 op {
252 fb1a36c0 2022-01-09 op /*
253 fb1a36c0 2022-01-09 op * Normal signal handler rules don't apply because libevent
254 fb1a36c0 2022-01-09 op * decouples for us.
255 fb1a36c0 2022-01-09 op */
256 fb1a36c0 2022-01-09 op
257 fb1a36c0 2022-01-09 op switch (sig) {
258 fb1a36c0 2022-01-09 op case SIGINT:
259 fb1a36c0 2022-01-09 op case SIGTERM:
260 fb1a36c0 2022-01-09 op client_shutdown();
261 fb1a36c0 2022-01-09 op default:
262 fb1a36c0 2022-01-09 op fatalx("unexpected signal %d", sig);
263 fb1a36c0 2022-01-09 op }
264 fb1a36c0 2022-01-09 op }
265 fb1a36c0 2022-01-09 op
266 fb1a36c0 2022-01-09 op static void
267 fb1a36c0 2022-01-09 op client_dispatch_listener(int fd, short event, void *d)
268 fb1a36c0 2022-01-09 op {
269 1e84c7b7 2022-01-10 op static int auth = 0;
270 1e84c7b7 2022-01-10 op struct kd_auth_proc rauth;
271 5c69b561 2022-01-29 op struct kd_debug_info debug;
272 5c69b561 2022-01-29 op struct fid *f;
273 fb1a36c0 2022-01-09 op struct imsg imsg;
274 fb1a36c0 2022-01-09 op struct imsgev *iev = d;
275 fb1a36c0 2022-01-09 op struct imsgbuf *ibuf;
276 fb1a36c0 2022-01-09 op ssize_t n;
277 fb1a36c0 2022-01-09 op int shut = 0;
278 fb1a36c0 2022-01-09 op
279 fb1a36c0 2022-01-09 op ibuf = &iev->ibuf;
280 fb1a36c0 2022-01-09 op
281 fb1a36c0 2022-01-09 op if (event & EV_READ) {
282 fb1a36c0 2022-01-09 op if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
283 fb1a36c0 2022-01-09 op fatal("imsg_read error");
284 fb1a36c0 2022-01-09 op if (n == 0) /* Connection closed */
285 fb1a36c0 2022-01-09 op shut = 1;
286 fb1a36c0 2022-01-09 op }
287 fb1a36c0 2022-01-09 op if (event & EV_WRITE) {
288 fb1a36c0 2022-01-09 op if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
289 fb1a36c0 2022-01-09 op fatal("msgbuf_write");
290 fb1a36c0 2022-01-09 op if (n == 0) /* Connection closed */
291 fb1a36c0 2022-01-09 op shut = 1;
292 fb1a36c0 2022-01-09 op }
293 fb1a36c0 2022-01-09 op
294 fb1a36c0 2022-01-09 op for (;;) {
295 fb1a36c0 2022-01-09 op if ((n = imsg_get(ibuf, &imsg)) == -1)
296 fb1a36c0 2022-01-09 op fatal("%s: imsg_get error", __func__);
297 fb1a36c0 2022-01-09 op if (n == 0) /* No more messages. */
298 fb1a36c0 2022-01-09 op break;
299 fb1a36c0 2022-01-09 op
300 fb1a36c0 2022-01-09 op switch (imsg.hdr.type) {
301 982f38dc 2022-01-23 op case IMSG_CTL_LOG_VERBOSE:
302 982f38dc 2022-01-23 op if (IMSG_DATA_SIZE(imsg) != sizeof(verbose))
303 982f38dc 2022-01-23 op fatalx("%s: IMSG_CTL_LOG_VERBOSE wrong size",
304 982f38dc 2022-01-23 op __func__);
305 982f38dc 2022-01-23 op memcpy(&verbose, imsg.data, sizeof(verbose));
306 982f38dc 2022-01-23 op log_setverbose(verbose);
307 982f38dc 2022-01-23 op break;
308 5c69b561 2022-01-29 op case IMSG_CTL_DEBUG:
309 5c69b561 2022-01-29 op STAILQ_FOREACH(f, &fids, entries) {
310 5c69b561 2022-01-29 op memset(&debug, 0, sizeof(debug));
311 5c69b561 2022-01-29 op debug.fid = f->fid;
312 5c69b561 2022-01-29 op strlcpy(debug.path, f->fname,
313 5c69b561 2022-01-29 op sizeof(debug.path));
314 5c69b561 2022-01-29 op client_send_listenerp(IMSG_CTL_DEBUG_BACK,
315 5c69b561 2022-01-29 op imsg.hdr.peerid, &debug, sizeof(debug));
316 5c69b561 2022-01-29 op }
317 5c69b561 2022-01-29 op client_send_listenerp(IMSG_CTL_DEBUG_END,
318 5c69b561 2022-01-29 op imsg.hdr.peerid, NULL, 0);
319 5c69b561 2022-01-29 op break;
320 fb1a36c0 2022-01-09 op case IMSG_AUTH:
321 fb1a36c0 2022-01-09 op peerid = imsg.hdr.peerid;
322 fb1a36c0 2022-01-09 op if (auth)
323 fb1a36c0 2022-01-09 op fatalx("%s: IMSG_AUTH already done", __func__);
324 1e84c7b7 2022-01-10 op auth = 1;
325 1e84c7b7 2022-01-10 op
326 1e84c7b7 2022-01-10 op if (IMSG_DATA_SIZE(imsg) != sizeof(rauth))
327 1e84c7b7 2022-01-10 op fatalx("mismatching size for IMSG_AUTH");
328 1e84c7b7 2022-01-10 op memcpy(&rauth, imsg.data, sizeof(rauth));
329 1e84c7b7 2022-01-10 op if (rauth.uname[sizeof(rauth.uname)-1] != '\0' ||
330 1e84c7b7 2022-01-10 op rauth.dir[sizeof(rauth.dir)-1] != '\0')
331 1e84c7b7 2022-01-10 op fatalx("IMSG_AUTH strings not NUL-terminated");
332 1e84c7b7 2022-01-10 op
333 1e84c7b7 2022-01-10 op client_privdrop(rauth.uname, rauth.dir);
334 1e84c7b7 2022-01-10 op explicit_bzero(&rauth, sizeof(rauth));
335 fb1a36c0 2022-01-09 op break;
336 fb1a36c0 2022-01-09 op case IMSG_BUF:
337 8b0a5062 2022-05-22 op case IMSG_BUF_CONT:
338 fb1a36c0 2022-01-09 op if (!auth)
339 fb1a36c0 2022-01-09 op fatalx("%s: can't handle messages before"
340 fb1a36c0 2022-01-09 op " doing the auth", __func__);
341 8b0a5062 2022-05-22 op handle_message(&imsg, IMSG_DATA_SIZE(imsg),
342 8b0a5062 2022-05-22 op imsg.hdr.type == IMSG_BUF_CONT);
343 fb1a36c0 2022-01-09 op break;
344 fb1a36c0 2022-01-09 op case IMSG_CONN_GONE:
345 fb1a36c0 2022-01-09 op log_debug("closing");
346 fb1a36c0 2022-01-09 op shut = 1;
347 fb1a36c0 2022-01-09 op break;
348 fb1a36c0 2022-01-09 op default:
349 fb1a36c0 2022-01-09 op log_debug("%s: unexpected imsg %d",
350 fb1a36c0 2022-01-09 op __func__, imsg.hdr.type);
351 fb1a36c0 2022-01-09 op break;
352 fb1a36c0 2022-01-09 op }
353 fb1a36c0 2022-01-09 op imsg_free(&imsg);
354 fb1a36c0 2022-01-09 op }
355 fb1a36c0 2022-01-09 op
356 fb1a36c0 2022-01-09 op if (!shut)
357 fb1a36c0 2022-01-09 op imsg_event_add(iev);
358 fb1a36c0 2022-01-09 op else {
359 fb1a36c0 2022-01-09 op /* This pipe is dead. Remove its event handler. */
360 fb1a36c0 2022-01-09 op event_del(&iev->ev);
361 fb1a36c0 2022-01-09 op log_debug("pipe closed, shutting down...");
362 fb1a36c0 2022-01-09 op event_loopexit(NULL);
363 fb1a36c0 2022-01-09 op }
364 fb1a36c0 2022-01-09 op }
365 fb1a36c0 2022-01-09 op
366 fb1a36c0 2022-01-09 op static void
367 fb1a36c0 2022-01-09 op client_privdrop(const char *username, const char *dir)
368 fb1a36c0 2022-01-09 op {
369 fb1a36c0 2022-01-09 op struct passwd *pw;
370 fb1a36c0 2022-01-09 op
371 fb1a36c0 2022-01-09 op setproctitle("client %s", username);
372 fb1a36c0 2022-01-09 op
373 fb1a36c0 2022-01-09 op if ((pw = getpwnam(username)) == NULL)
374 fb1a36c0 2022-01-09 op fatalx("getpwnam(%s) failed", username);
375 fb1a36c0 2022-01-09 op
376 fb1a36c0 2022-01-09 op if (chroot(dir) == -1)
377 fb1a36c0 2022-01-09 op fatal("chroot");
378 fb1a36c0 2022-01-09 op if (chdir("/") == -1)
379 fb1a36c0 2022-01-09 op fatal("chdir(\"/\")");
380 fb1a36c0 2022-01-09 op
381 fb1a36c0 2022-01-09 op if (setgroups(1, &pw->pw_gid) ||
382 fb1a36c0 2022-01-09 op setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
383 fb1a36c0 2022-01-09 op setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
384 fb1a36c0 2022-01-09 op fatal("can't drop privileges");
385 fb1a36c0 2022-01-09 op
386 fb1a36c0 2022-01-09 op sandbox_client();
387 fb1a36c0 2022-01-09 op log_debug("client ready; user=%s dir=%s", username, dir);
388 fb1a36c0 2022-01-09 op
389 fb1a36c0 2022-01-09 op if ((evb = evbuffer_new()) == NULL)
390 fb1a36c0 2022-01-09 op fatal("evbuffer_new");
391 fb1a36c0 2022-01-09 op }
392 fb1a36c0 2022-01-09 op
393 fb1a36c0 2022-01-09 op static int
394 5c69b561 2022-01-29 op client_send_listenerp(int type, uint32_t peerid, const void *data, uint16_t len)
395 fb1a36c0 2022-01-09 op {
396 fb1a36c0 2022-01-09 op int ret;
397 fb1a36c0 2022-01-09 op
398 fb1a36c0 2022-01-09 op if ((ret = imsg_compose(&iev_listener->ibuf, type, peerid, 0, -1,
399 fb1a36c0 2022-01-09 op data, len)) != -1)
400 fb1a36c0 2022-01-09 op imsg_event_add(iev_listener);
401 fb1a36c0 2022-01-09 op
402 fb1a36c0 2022-01-09 op return ret;
403 fb1a36c0 2022-01-09 op }
404 fb1a36c0 2022-01-09 op
405 5c69b561 2022-01-29 op static int
406 5c69b561 2022-01-29 op client_send_listener(int type, const void *data, uint16_t len)
407 5c69b561 2022-01-29 op {
408 865dd323 2022-01-29 op return client_send_listenerp(type, peerid, data, len);
409 5c69b561 2022-01-29 op }
410 5c69b561 2022-01-29 op
411 fb1a36c0 2022-01-09 op /* set qid fields from sb */
412 fb1a36c0 2022-01-09 op static void
413 fb1a36c0 2022-01-09 op qid_update_from_sb(struct qid *qid, struct stat *sb)
414 fb1a36c0 2022-01-09 op {
415 fb1a36c0 2022-01-09 op qid->path = sb->st_ino;
416 fb1a36c0 2022-01-09 op
417 fb1a36c0 2022-01-09 op /*
418 fb1a36c0 2022-01-09 op * Theoretically (and hopefully!) this should be a 64 bit
419 fb1a36c0 2022-01-09 op * number. Unfortunately, 9P uses 32 bit timestamps.
420 fb1a36c0 2022-01-09 op */
421 fb1a36c0 2022-01-09 op qid->vers = sb->st_mtim.tv_sec;
422 fb1a36c0 2022-01-09 op
423 fb1a36c0 2022-01-09 op if (S_ISREG(sb->st_mode))
424 fb1a36c0 2022-01-09 op qid->type = QTFILE;
425 fb1a36c0 2022-01-09 op else if (S_ISDIR(sb->st_mode))
426 fb1a36c0 2022-01-09 op qid->type = QTDIR;
427 fb1a36c0 2022-01-09 op else if (S_ISLNK(sb->st_mode))
428 fb1a36c0 2022-01-09 op qid->type = QTSYMLINK;
429 fb1a36c0 2022-01-09 op }
430 fb1a36c0 2022-01-09 op
431 fb1a36c0 2022-01-09 op /* creates a qid given a fd */
432 fb1a36c0 2022-01-09 op static struct dir *
433 fb1a36c0 2022-01-09 op new_dir(int fd)
434 fb1a36c0 2022-01-09 op {
435 fb1a36c0 2022-01-09 op struct dir *dir;
436 fb1a36c0 2022-01-09 op
437 fb1a36c0 2022-01-09 op if ((dir = calloc(1, sizeof(*dir))) == NULL)
438 fb1a36c0 2022-01-09 op return NULL;
439 fb1a36c0 2022-01-09 op
440 fb1a36c0 2022-01-09 op dir->fd = fd;
441 fb1a36c0 2022-01-09 op STAILQ_INSERT_HEAD(&dirs, dir, entries);
442 fb1a36c0 2022-01-09 op return dir;
443 fb1a36c0 2022-01-09 op }
444 fb1a36c0 2022-01-09 op
445 fb1a36c0 2022-01-09 op static struct dir *
446 fb1a36c0 2022-01-09 op dir_incref(struct dir *dir)
447 fb1a36c0 2022-01-09 op {
448 fb1a36c0 2022-01-09 op dir->refcount++;
449 fb1a36c0 2022-01-09 op return dir;
450 fb1a36c0 2022-01-09 op }
451 fb1a36c0 2022-01-09 op
452 fb1a36c0 2022-01-09 op static void
453 fb1a36c0 2022-01-09 op dir_decref(struct dir *dir)
454 fb1a36c0 2022-01-09 op {
455 fb1a36c0 2022-01-09 op if (--dir->refcount > 0)
456 fb1a36c0 2022-01-09 op return;
457 fb1a36c0 2022-01-09 op
458 fb1a36c0 2022-01-09 op STAILQ_REMOVE(&dirs, dir, dir, entries);
459 fb1a36c0 2022-01-09 op
460 fb1a36c0 2022-01-09 op close(dir->fd);
461 fb1a36c0 2022-01-09 op free(dir);
462 fb1a36c0 2022-01-09 op }
463 fb1a36c0 2022-01-09 op
464 fb1a36c0 2022-01-09 op static struct fid *
465 fb1a36c0 2022-01-09 op new_fid(struct dir *dir, uint32_t fid, const char *path, struct qid *qid)
466 fb1a36c0 2022-01-09 op {
467 fb1a36c0 2022-01-09 op struct fid *f;
468 fb1a36c0 2022-01-09 op struct qid q;
469 fb1a36c0 2022-01-09 op struct stat sb;
470 fb1a36c0 2022-01-09 op
471 fb1a36c0 2022-01-09 op if (qid == NULL) {
472 fb1a36c0 2022-01-09 op if (fstatat(dir->fd, path, &sb, 0)) {
473 fb1a36c0 2022-01-09 op log_warn("fstatat(%s)", path);
474 fb1a36c0 2022-01-09 op return NULL;
475 fb1a36c0 2022-01-09 op }
476 fb1a36c0 2022-01-09 op qid_update_from_sb(&q, &sb);
477 fb1a36c0 2022-01-09 op qid = &q;
478 fb1a36c0 2022-01-09 op }
479 fb1a36c0 2022-01-09 op
480 fb1a36c0 2022-01-09 op if ((f = calloc(1, sizeof(*f))) == NULL)
481 fb1a36c0 2022-01-09 op return NULL;
482 fb1a36c0 2022-01-09 op
483 fb1a36c0 2022-01-09 op f->dir = dir_incref(dir);
484 fb1a36c0 2022-01-09 op f->fid = fid;
485 fb1a36c0 2022-01-09 op f->fd = -1;
486 fb1a36c0 2022-01-09 op
487 543fc013 2022-01-17 op strlcpy(f->fname, path, sizeof(f->fname));
488 fb1a36c0 2022-01-09 op
489 fb1a36c0 2022-01-09 op memcpy(&f->qid, qid, sizeof(f->qid));
490 fb1a36c0 2022-01-09 op
491 fb1a36c0 2022-01-09 op STAILQ_INSERT_HEAD(&fids, f, entries);
492 fb1a36c0 2022-01-09 op
493 fb1a36c0 2022-01-09 op return f;
494 fb1a36c0 2022-01-09 op }
495 fb1a36c0 2022-01-09 op
496 fb1a36c0 2022-01-09 op static struct fid *
497 fb1a36c0 2022-01-09 op fid_by_id(uint32_t fid)
498 fb1a36c0 2022-01-09 op {
499 fb1a36c0 2022-01-09 op struct fid *f;
500 fb1a36c0 2022-01-09 op
501 fb1a36c0 2022-01-09 op STAILQ_FOREACH(f, &fids, entries) {
502 fb1a36c0 2022-01-09 op if (f->fid == fid)
503 fb1a36c0 2022-01-09 op return f;
504 fb1a36c0 2022-01-09 op }
505 fb1a36c0 2022-01-09 op
506 fb1a36c0 2022-01-09 op return NULL;
507 fb1a36c0 2022-01-09 op }
508 fb1a36c0 2022-01-09 op
509 fb1a36c0 2022-01-09 op static void
510 fb1a36c0 2022-01-09 op free_fid(struct fid *f)
511 fb1a36c0 2022-01-09 op {
512 fb1a36c0 2022-01-09 op int r;
513 fb1a36c0 2022-01-09 op
514 fb1a36c0 2022-01-09 op if (f->fd != -1) {
515 fb1a36c0 2022-01-09 op if (f->d != NULL)
516 fb1a36c0 2022-01-09 op r = closedir(f->d);
517 fb1a36c0 2022-01-09 op else
518 fb1a36c0 2022-01-09 op r = close(f->fd);
519 fb1a36c0 2022-01-09 op
520 fb1a36c0 2022-01-09 op if (r == -1)
521 fb1a36c0 2022-01-09 op fatal("can't close fid %d", f->fid);
522 fb1a36c0 2022-01-09 op
523 fb1a36c0 2022-01-09 op if (f->evb != NULL)
524 fb1a36c0 2022-01-09 op evbuffer_free(f->evb);
525 fb1a36c0 2022-01-09 op
526 fb1a36c0 2022-01-09 op /* try to honour ORCLOSE if requested */
527 fb1a36c0 2022-01-09 op if (f->iomode & O_CLOEXEC)
528 543fc013 2022-01-17 op unlinkat(f->dir->fd, f->fname, 0);
529 fb1a36c0 2022-01-09 op }
530 fb1a36c0 2022-01-09 op
531 fb1a36c0 2022-01-09 op dir_decref(f->dir);
532 fb1a36c0 2022-01-09 op
533 fb1a36c0 2022-01-09 op STAILQ_REMOVE(&fids, f, fid, entries);
534 fb1a36c0 2022-01-09 op free(f);
535 fb1a36c0 2022-01-09 op }
536 fb1a36c0 2022-01-09 op
537 fb1a36c0 2022-01-09 op static void
538 fb1a36c0 2022-01-09 op parse_message(const uint8_t *data, size_t len, struct np_msg_header *hdr,
539 fb1a36c0 2022-01-09 op uint8_t **cnt)
540 fb1a36c0 2022-01-09 op {
541 fb1a36c0 2022-01-09 op size_t olen = len;
542 fb1a36c0 2022-01-09 op
543 fb1a36c0 2022-01-09 op if (!NPREAD32("len", &hdr->len, &data, &len) ||
544 fb1a36c0 2022-01-09 op !NPREAD8("type", &hdr->type, &data, &len) ||
545 fb1a36c0 2022-01-09 op !NPREAD16("tag", &hdr->tag, &data, &len))
546 fb1a36c0 2022-01-09 op goto err;
547 fb1a36c0 2022-01-09 op
548 fb1a36c0 2022-01-09 op if (olen != hdr->len)
549 fb1a36c0 2022-01-09 op goto err;
550 fb1a36c0 2022-01-09 op
551 fb1a36c0 2022-01-09 op if (hdr->type < Tversion ||
552 fb1a36c0 2022-01-09 op hdr->type >= Tmax ||
553 fb1a36c0 2022-01-09 op hdr->type == Terror ||
554 fb1a36c0 2022-01-09 op (hdr->type & 0x1) != 0) /* cannot recv a R* */
555 fb1a36c0 2022-01-09 op goto err;
556 fb1a36c0 2022-01-09 op
557 fb1a36c0 2022-01-09 op hdr->tag = le32toh(hdr->tag);
558 fb1a36c0 2022-01-09 op
559 fb1a36c0 2022-01-09 op *cnt = (uint8_t *)data;
560 fb1a36c0 2022-01-09 op return;
561 fb1a36c0 2022-01-09 op
562 fb1a36c0 2022-01-09 op err:
563 182b48dc 2022-05-22 op log_warnx("got invalid message: (%d, %d, %d)",
564 182b48dc 2022-05-22 op hdr->len, hdr->type, hdr->tag);
565 182b48dc 2022-05-22 op client_send_listener(IMSG_CLOSE, NULL, 0);
566 182b48dc 2022-05-22 op client_shutdown();
567 fb1a36c0 2022-01-09 op }
568 fb1a36c0 2022-01-09 op
569 fb1a36c0 2022-01-09 op static void
570 fb1a36c0 2022-01-09 op np_write16(struct evbuffer *e, uint16_t x)
571 fb1a36c0 2022-01-09 op {
572 fb1a36c0 2022-01-09 op x = htole16(x);
573 fb1a36c0 2022-01-09 op evbuffer_add(e, &x, sizeof(x));
574 fb1a36c0 2022-01-09 op }
575 fb1a36c0 2022-01-09 op
576 fb1a36c0 2022-01-09 op static void
577 fb1a36c0 2022-01-09 op np_write32(struct evbuffer *e, uint32_t x)
578 fb1a36c0 2022-01-09 op {
579 fb1a36c0 2022-01-09 op x = htole32(x);
580 fb1a36c0 2022-01-09 op evbuffer_add(e, &x, sizeof(x));
581 fb1a36c0 2022-01-09 op }
582 fb1a36c0 2022-01-09 op
583 fb1a36c0 2022-01-09 op static void
584 fb1a36c0 2022-01-09 op np_write64(struct evbuffer *e, uint64_t x)
585 fb1a36c0 2022-01-09 op {
586 fb1a36c0 2022-01-09 op x = htole64(x);
587 fb1a36c0 2022-01-09 op evbuffer_add(e, &x, sizeof(x));
588 fb1a36c0 2022-01-09 op }
589 fb1a36c0 2022-01-09 op
590 fb1a36c0 2022-01-09 op static void
591 fb1a36c0 2022-01-09 op np_writebuf(struct evbuffer *e, size_t len, void *data)
592 fb1a36c0 2022-01-09 op {
593 fb1a36c0 2022-01-09 op evbuffer_add(e, data, len);
594 fb1a36c0 2022-01-09 op }
595 fb1a36c0 2022-01-09 op
596 fb1a36c0 2022-01-09 op static void
597 fb1a36c0 2022-01-09 op np_header(uint32_t len, uint8_t type, uint16_t tag)
598 fb1a36c0 2022-01-09 op {
599 fb1a36c0 2022-01-09 op len += HEADERSIZE;
600 fb1a36c0 2022-01-09 op
601 fb1a36c0 2022-01-09 op len = htole32(len);
602 fb1a36c0 2022-01-09 op tag = htole16(tag);
603 fb1a36c0 2022-01-09 op
604 fb1a36c0 2022-01-09 op evbuffer_add(evb, &len, sizeof(len));
605 fb1a36c0 2022-01-09 op evbuffer_add(evb, &type, sizeof(type));
606 fb1a36c0 2022-01-09 op evbuffer_add(evb, &tag, sizeof(tag));
607 fb1a36c0 2022-01-09 op }
608 fb1a36c0 2022-01-09 op
609 fb1a36c0 2022-01-09 op static void
610 fb1a36c0 2022-01-09 op np_string(struct evbuffer *e, uint16_t len, const char *str)
611 fb1a36c0 2022-01-09 op {
612 fb1a36c0 2022-01-09 op uint16_t l = len;
613 fb1a36c0 2022-01-09 op
614 fb1a36c0 2022-01-09 op len = htole16(len);
615 fb1a36c0 2022-01-09 op evbuffer_add(e, &len, sizeof(len));
616 fb1a36c0 2022-01-09 op evbuffer_add(e, str, l);
617 fb1a36c0 2022-01-09 op }
618 fb1a36c0 2022-01-09 op
619 fb1a36c0 2022-01-09 op static void
620 fb1a36c0 2022-01-09 op np_qid(struct evbuffer *e, struct qid *qid)
621 fb1a36c0 2022-01-09 op {
622 fb1a36c0 2022-01-09 op uint64_t path;
623 fb1a36c0 2022-01-09 op uint32_t vers;
624 fb1a36c0 2022-01-09 op
625 fb1a36c0 2022-01-09 op path = htole64(qid->path);
626 fb1a36c0 2022-01-09 op vers = htole32(qid->vers);
627 fb1a36c0 2022-01-09 op
628 fb1a36c0 2022-01-09 op evbuffer_add(e, &qid->type, sizeof(qid->type));
629 fb1a36c0 2022-01-09 op evbuffer_add(e, &vers, sizeof(vers));
630 fb1a36c0 2022-01-09 op evbuffer_add(e, &path, sizeof(path));
631 fb1a36c0 2022-01-09 op }
632 fb1a36c0 2022-01-09 op
633 fb1a36c0 2022-01-09 op static void
634 fb1a36c0 2022-01-09 op do_send(void)
635 fb1a36c0 2022-01-09 op {
636 fb1a36c0 2022-01-09 op size_t len;
637 fb1a36c0 2022-01-09 op void *data;
638 fb1a36c0 2022-01-09 op
639 fb1a36c0 2022-01-09 op len = EVBUFFER_LENGTH(evb);
640 fb1a36c0 2022-01-09 op data = EVBUFFER_DATA(evb);
641 fb1a36c0 2022-01-09 op
642 fb1a36c0 2022-01-09 op #if DEBUG_PACKETS
643 fb1a36c0 2022-01-09 op hexdump("outgoing packet", data, len);
644 fb1a36c0 2022-01-09 op #endif
645 fb1a36c0 2022-01-09 op client_send_listener(IMSG_BUF, data, len);
646 fb1a36c0 2022-01-09 op evbuffer_drain(evb, len);
647 fb1a36c0 2022-01-09 op }
648 fb1a36c0 2022-01-09 op
649 fb1a36c0 2022-01-09 op static void
650 fb1a36c0 2022-01-09 op np_version(uint16_t tag, uint32_t msize, const char *version)
651 fb1a36c0 2022-01-09 op {
652 fb1a36c0 2022-01-09 op uint16_t l;
653 fb1a36c0 2022-01-09 op
654 fb1a36c0 2022-01-09 op l = strlen(version);
655 fb1a36c0 2022-01-09 op
656 fb1a36c0 2022-01-09 op msize = htole32(msize);
657 fb1a36c0 2022-01-09 op
658 fb1a36c0 2022-01-09 op np_header(sizeof(msize) + sizeof(l) + l, Rversion, tag);
659 fb1a36c0 2022-01-09 op evbuffer_add(evb, &msize, sizeof(msize));
660 fb1a36c0 2022-01-09 op np_string(evb, l, version);
661 fb1a36c0 2022-01-09 op do_send();
662 fb1a36c0 2022-01-09 op }
663 fb1a36c0 2022-01-09 op
664 fb1a36c0 2022-01-09 op static void
665 fb1a36c0 2022-01-09 op np_attach(uint16_t tag, struct qid *qid)
666 fb1a36c0 2022-01-09 op {
667 fb1a36c0 2022-01-09 op np_header(QIDSIZE, Rattach, tag);
668 fb1a36c0 2022-01-09 op np_qid(evb, qid);
669 fb1a36c0 2022-01-09 op do_send();
670 fb1a36c0 2022-01-09 op }
671 fb1a36c0 2022-01-09 op
672 fb1a36c0 2022-01-09 op static void
673 fb1a36c0 2022-01-09 op np_clunk(uint16_t tag)
674 fb1a36c0 2022-01-09 op {
675 fb1a36c0 2022-01-09 op np_header(0, Rclunk, tag);
676 fb1a36c0 2022-01-09 op do_send();
677 fb1a36c0 2022-01-09 op }
678 fb1a36c0 2022-01-09 op
679 fb1a36c0 2022-01-09 op static void
680 fb1a36c0 2022-01-09 op np_flush(uint16_t tag)
681 fb1a36c0 2022-01-09 op {
682 fb1a36c0 2022-01-09 op np_header(0, Rflush, tag);
683 fb1a36c0 2022-01-09 op do_send();
684 fb1a36c0 2022-01-09 op }
685 fb1a36c0 2022-01-09 op
686 fb1a36c0 2022-01-09 op static void
687 fb1a36c0 2022-01-09 op np_walk(uint16_t tag, int nwqid, struct qid *wqid)
688 fb1a36c0 2022-01-09 op {
689 fb1a36c0 2022-01-09 op int i;
690 fb1a36c0 2022-01-09 op
691 fb1a36c0 2022-01-09 op /* two bytes for the counter */
692 fb1a36c0 2022-01-09 op np_header(2 + QIDSIZE * nwqid, Rwalk, tag);
693 fb1a36c0 2022-01-09 op np_write16(evb, nwqid);
694 fb1a36c0 2022-01-09 op for (i = 0; i < nwqid; ++i)
695 fb1a36c0 2022-01-09 op np_qid(evb, wqid + i);
696 fb1a36c0 2022-01-09 op
697 fb1a36c0 2022-01-09 op do_send();
698 fb1a36c0 2022-01-09 op }
699 fb1a36c0 2022-01-09 op
700 fb1a36c0 2022-01-09 op static void
701 fb1a36c0 2022-01-09 op np_open(uint16_t tag, struct qid *qid, uint32_t iounit)
702 fb1a36c0 2022-01-09 op {
703 fb1a36c0 2022-01-09 op np_header(QIDSIZE + sizeof(iounit), Ropen, tag);
704 fb1a36c0 2022-01-09 op np_qid(evb, qid);
705 fb1a36c0 2022-01-09 op np_write32(evb, iounit);
706 fb1a36c0 2022-01-09 op do_send();
707 fb1a36c0 2022-01-09 op }
708 fb1a36c0 2022-01-09 op
709 fb1a36c0 2022-01-09 op static void
710 fb1a36c0 2022-01-09 op np_create(uint16_t tag, struct qid *qid, uint32_t iounit)
711 fb1a36c0 2022-01-09 op {
712 fb1a36c0 2022-01-09 op np_header(QIDSIZE + sizeof(iounit), Rcreate, tag);
713 fb1a36c0 2022-01-09 op np_qid(evb, qid);
714 fb1a36c0 2022-01-09 op np_write32(evb, iounit);
715 fb1a36c0 2022-01-09 op do_send();
716 fb1a36c0 2022-01-09 op }
717 fb1a36c0 2022-01-09 op
718 fb1a36c0 2022-01-09 op static void
719 fb1a36c0 2022-01-09 op np_read(uint16_t tag, uint32_t count, void *data)
720 fb1a36c0 2022-01-09 op {
721 fb1a36c0 2022-01-09 op if (sizeof(count) + count + HEADERSIZE >= msize) {
722 fb1a36c0 2022-01-09 op np_error(tag, "Rread would overflow");
723 fb1a36c0 2022-01-09 op return;
724 fb1a36c0 2022-01-09 op }
725 fb1a36c0 2022-01-09 op
726 fb1a36c0 2022-01-09 op np_header(sizeof(count) + count, Rread, tag);
727 fb1a36c0 2022-01-09 op np_write32(evb, count);
728 fb1a36c0 2022-01-09 op np_writebuf(evb, count, data);
729 fb1a36c0 2022-01-09 op do_send();
730 fb1a36c0 2022-01-09 op }
731 fb1a36c0 2022-01-09 op
732 fb1a36c0 2022-01-09 op static void
733 fb1a36c0 2022-01-09 op np_write(uint16_t tag, uint32_t count)
734 fb1a36c0 2022-01-09 op {
735 fb1a36c0 2022-01-09 op np_header(sizeof(count), Rwrite, tag);
736 fb1a36c0 2022-01-09 op np_write32(evb, count);
737 fb1a36c0 2022-01-09 op do_send();
738 fb1a36c0 2022-01-09 op }
739 fb1a36c0 2022-01-09 op
740 fb1a36c0 2022-01-09 op static void
741 70540bb5 2022-02-02 op np_stat(uint16_t tag, uint16_t count, void *data)
742 fb1a36c0 2022-01-09 op {
743 fb1a36c0 2022-01-09 op if (sizeof(count) + count + HEADERSIZE >= msize) {
744 fb1a36c0 2022-01-09 op np_error(tag, "Rstat would overflow");
745 fb1a36c0 2022-01-09 op return;
746 fb1a36c0 2022-01-09 op }
747 fb1a36c0 2022-01-09 op
748 70540bb5 2022-02-02 op np_header(sizeof(count) + count, Rstat, tag);
749 70540bb5 2022-02-02 op np_write16(evb, count);
750 fb1a36c0 2022-01-09 op np_writebuf(evb, count, data);
751 755d75bf 2022-01-17 op do_send();
752 755d75bf 2022-01-17 op }
753 755d75bf 2022-01-17 op
754 755d75bf 2022-01-17 op static void
755 755d75bf 2022-01-17 op np_wstat(uint16_t tag)
756 755d75bf 2022-01-17 op {
757 755d75bf 2022-01-17 op np_header(0, Rwstat, tag);
758 fb1a36c0 2022-01-09 op do_send();
759 fb1a36c0 2022-01-09 op }
760 fb1a36c0 2022-01-09 op
761 fb1a36c0 2022-01-09 op static void
762 fb1a36c0 2022-01-09 op np_remove(uint16_t tag)
763 fb1a36c0 2022-01-09 op {
764 fb1a36c0 2022-01-09 op np_header(0, Rremove, tag);
765 fb1a36c0 2022-01-09 op do_send();
766 fb1a36c0 2022-01-09 op }
767 fb1a36c0 2022-01-09 op
768 fb1a36c0 2022-01-09 op static void
769 fb1a36c0 2022-01-09 op np_error(uint16_t tag, const char *errstr)
770 fb1a36c0 2022-01-09 op {
771 fb1a36c0 2022-01-09 op uint16_t l;
772 fb1a36c0 2022-01-09 op
773 fb1a36c0 2022-01-09 op l = strlen(errstr);
774 fb1a36c0 2022-01-09 op
775 fb1a36c0 2022-01-09 op np_header(sizeof(l) + l, Rerror, tag);
776 fb1a36c0 2022-01-09 op np_string(evb, l, errstr);
777 fb1a36c0 2022-01-09 op do_send();
778 fb1a36c0 2022-01-09 op }
779 fb1a36c0 2022-01-09 op
780 fb1a36c0 2022-01-09 op static void
781 fb1a36c0 2022-01-09 op np_errno(uint16_t tag)
782 fb1a36c0 2022-01-09 op {
783 fb1a36c0 2022-01-09 op int saved_errno;
784 fb1a36c0 2022-01-09 op char buf[NL_TEXTMAX] = {0};
785 fb1a36c0 2022-01-09 op
786 fb1a36c0 2022-01-09 op saved_errno = errno;
787 fb1a36c0 2022-01-09 op
788 fb1a36c0 2022-01-09 op strerror_r(errno, buf, sizeof(buf));
789 fb1a36c0 2022-01-09 op np_error(tag, buf);
790 fb1a36c0 2022-01-09 op
791 fb1a36c0 2022-01-09 op errno = saved_errno;
792 fb1a36c0 2022-01-09 op }
793 fb1a36c0 2022-01-09 op
794 fb1a36c0 2022-01-09 op static int
795 fb1a36c0 2022-01-09 op np_read8(const char *t, const char *f, uint8_t *dst, const uint8_t **src,
796 fb1a36c0 2022-01-09 op size_t *len)
797 fb1a36c0 2022-01-09 op {
798 fb1a36c0 2022-01-09 op if (*len < sizeof(*dst)) {
799 fb1a36c0 2022-01-09 op log_warnx("%s: wanted %zu bytes for the %s field but only "
800 fb1a36c0 2022-01-09 op "%zu are available.", t, sizeof(*dst), f, *len);
801 ae6f257a 2022-01-19 op return 0;
802 fb1a36c0 2022-01-09 op }
803 fb1a36c0 2022-01-09 op
804 fb1a36c0 2022-01-09 op memcpy(dst, *src, sizeof(*dst));
805 fb1a36c0 2022-01-09 op *src += sizeof(*dst);
806 fb1a36c0 2022-01-09 op *len -= sizeof(*dst);
807 fb1a36c0 2022-01-09 op
808 fb1a36c0 2022-01-09 op return 1;
809 fb1a36c0 2022-01-09 op }
810 fb1a36c0 2022-01-09 op
811 fb1a36c0 2022-01-09 op static int
812 fb1a36c0 2022-01-09 op np_read16(const char *t, const char *f, uint16_t *dst, const uint8_t **src,
813 fb1a36c0 2022-01-09 op size_t *len)
814 fb1a36c0 2022-01-09 op {
815 fb1a36c0 2022-01-09 op if (*len < sizeof(*dst)) {
816 fb1a36c0 2022-01-09 op log_warnx("%s: wanted %zu bytes for the %s field but only "
817 fb1a36c0 2022-01-09 op "%zu are available.", t, sizeof(*dst), f, *len);
818 ae6f257a 2022-01-19 op return 0;
819 fb1a36c0 2022-01-09 op }
820 fb1a36c0 2022-01-09 op
821 fb1a36c0 2022-01-09 op memcpy(dst, *src, sizeof(*dst));
822 fb1a36c0 2022-01-09 op *src += sizeof(*dst);
823 fb1a36c0 2022-01-09 op *len -= sizeof(*dst);
824 fb1a36c0 2022-01-09 op *dst = le16toh(*dst);
825 fb1a36c0 2022-01-09 op
826 fb1a36c0 2022-01-09 op return 1;
827 fb1a36c0 2022-01-09 op }
828 fb1a36c0 2022-01-09 op
829 fb1a36c0 2022-01-09 op static int
830 fb1a36c0 2022-01-09 op np_read32(const char *t, const char *f, uint32_t *dst, const uint8_t **src,
831 fb1a36c0 2022-01-09 op size_t *len)
832 fb1a36c0 2022-01-09 op {
833 fb1a36c0 2022-01-09 op if (*len < sizeof(*dst)) {
834 fb1a36c0 2022-01-09 op log_warnx("%s: wanted %zu bytes for the %s field but only "
835 fb1a36c0 2022-01-09 op "%zu are available.", t, sizeof(*dst), f, *len);
836 ae6f257a 2022-01-19 op return 0;
837 fb1a36c0 2022-01-09 op }
838 fb1a36c0 2022-01-09 op
839 fb1a36c0 2022-01-09 op memcpy(dst, *src, sizeof(*dst));
840 fb1a36c0 2022-01-09 op *src += sizeof(*dst);
841 fb1a36c0 2022-01-09 op *len -= sizeof(*dst);
842 fb1a36c0 2022-01-09 op *dst = le32toh(*dst);
843 fb1a36c0 2022-01-09 op
844 fb1a36c0 2022-01-09 op return 1;
845 fb1a36c0 2022-01-09 op }
846 fb1a36c0 2022-01-09 op
847 fb1a36c0 2022-01-09 op static int
848 fb1a36c0 2022-01-09 op np_read64(const char *t, const char *f, uint64_t *dst, const uint8_t **src,
849 fb1a36c0 2022-01-09 op size_t *len)
850 fb1a36c0 2022-01-09 op {
851 fb1a36c0 2022-01-09 op if (*len < sizeof(*dst)) {
852 fb1a36c0 2022-01-09 op log_warnx("%s: wanted %zu bytes for the %s field but only "
853 fb1a36c0 2022-01-09 op "%zu are available.", t, sizeof(*dst), f, *len);
854 ae6f257a 2022-01-19 op return 0;
855 fb1a36c0 2022-01-09 op }
856 fb1a36c0 2022-01-09 op
857 fb1a36c0 2022-01-09 op memcpy(dst, *src, sizeof(*dst));
858 fb1a36c0 2022-01-09 op *src += sizeof(*dst);
859 fb1a36c0 2022-01-09 op *len -= sizeof(*dst);
860 fb1a36c0 2022-01-09 op *dst = le64toh(*dst);
861 fb1a36c0 2022-01-09 op
862 fb1a36c0 2022-01-09 op return 1;
863 fb1a36c0 2022-01-09 op }
864 fb1a36c0 2022-01-09 op
865 fb1a36c0 2022-01-09 op static int
866 fb1a36c0 2022-01-09 op np_readstr(const char *t, const char *f, char *res, size_t reslen,
867 fb1a36c0 2022-01-09 op const uint8_t **src, size_t *len)
868 fb1a36c0 2022-01-09 op {
869 fb1a36c0 2022-01-09 op uint16_t sl;
870 fb1a36c0 2022-01-09 op char buf[32];
871 fb1a36c0 2022-01-09 op
872 fb1a36c0 2022-01-09 op strlcpy(buf, f, sizeof(buf));
873 fb1a36c0 2022-01-09 op strlcat(buf, "-len", sizeof(buf));
874 fb1a36c0 2022-01-09 op
875 fb1a36c0 2022-01-09 op if (!np_read16(t, buf, &sl, src, len))
876 fb1a36c0 2022-01-09 op return READSTRERR;
877 fb1a36c0 2022-01-09 op
878 fb1a36c0 2022-01-09 op if (*len < sl) {
879 fb1a36c0 2022-01-09 op log_warnx("%s: wanted %d bytes for the %s field but only "
880 fb1a36c0 2022-01-09 op "%zu are available.", t, sl, f, *len);
881 fb1a36c0 2022-01-09 op return READSTRERR;
882 fb1a36c0 2022-01-09 op }
883 fb1a36c0 2022-01-09 op
884 fb1a36c0 2022-01-09 op if (*len > reslen-1)
885 fb1a36c0 2022-01-09 op return READSTRTRUNC;
886 fb1a36c0 2022-01-09 op
887 fb1a36c0 2022-01-09 op memcpy(res, *src, sl);
888 fb1a36c0 2022-01-09 op res[sl] = '\0';
889 fb1a36c0 2022-01-09 op *src += sl;
890 fb1a36c0 2022-01-09 op *len -= sl;
891 fb1a36c0 2022-01-09 op
892 fb1a36c0 2022-01-09 op return 0;
893 755d75bf 2022-01-17 op }
894 755d75bf 2022-01-17 op
895 755d75bf 2022-01-17 op static int
896 755d75bf 2022-01-17 op np_readst(const char *t, const char *f, struct np_stat *st,
897 755d75bf 2022-01-17 op char *name, size_t namelen, const uint8_t **src, size_t *len)
898 755d75bf 2022-01-17 op {
899 755d75bf 2022-01-17 op memset(st, 0, sizeof(*st));
900 755d75bf 2022-01-17 op
901 4e7aba8f 2022-02-02 op /* len is sent twice! */
902 4e7aba8f 2022-02-02 op if (!np_read16(t, "stat len", &st->size, src, len) ||
903 4e7aba8f 2022-02-02 op !np_read16(t, "stat.size", &st->size, src, len) ||
904 9029ac6f 2022-01-19 op !np_read16(t, "stat.type", &st->type, src, len) ||
905 755d75bf 2022-01-17 op !np_read32(t, "stat.dev", &st->dev, src, len) ||
906 755d75bf 2022-01-17 op !np_read64(t, "stat.qid.path", &st->qid.path, src, len) ||
907 755d75bf 2022-01-17 op !np_read32(t, "stat.qid.vers", &st->qid.vers, src, len) ||
908 755d75bf 2022-01-17 op !np_read8(t, "stat.qid.type", &st->qid.type, src, len) ||
909 755d75bf 2022-01-17 op !np_read32(t, "stat.mode", &st->mode, src, len) ||
910 755d75bf 2022-01-17 op !np_read32(t, "stat.atime", &st->atime, src, len) ||
911 755d75bf 2022-01-17 op !np_read32(t, "stat.mtime", &st->mtime, src, len) ||
912 755d75bf 2022-01-17 op !np_read64(t, "stat.length", &st->length, src, len))
913 755d75bf 2022-01-17 op return READSTRERR;
914 755d75bf 2022-01-17 op
915 755d75bf 2022-01-17 op /*
916 755d75bf 2022-01-17 op * ignore everything but the name, we don't support
917 755d75bf 2022-01-17 op * changing those fields anyway
918 755d75bf 2022-01-17 op */
919 755d75bf 2022-01-17 op st->name = name;
920 755d75bf 2022-01-17 op return np_readstr(t, "stat.name", name, namelen, src, len);
921 fb1a36c0 2022-01-09 op }
922 fb1a36c0 2022-01-09 op
923 fb1a36c0 2022-01-09 op static void
924 fb1a36c0 2022-01-09 op tversion(struct np_msg_header *hdr, const uint8_t *data, size_t len)
925 fb1a36c0 2022-01-09 op {
926 fb1a36c0 2022-01-09 op char *dot, version[32];
927 fb1a36c0 2022-01-09 op
928 fb1a36c0 2022-01-09 op if (handshaked)
929 fb1a36c0 2022-01-09 op goto err;
930 fb1a36c0 2022-01-09 op
931 fb1a36c0 2022-01-09 op /* msize[4] version[s] */
932 fb1a36c0 2022-01-09 op if (!NPREAD32("msize", &msize, &data, &len))
933 fb1a36c0 2022-01-09 op goto err;
934 fb1a36c0 2022-01-09 op
935 fb1a36c0 2022-01-09 op switch (NPREADSTR("version", version, sizeof(version), &data, &len)) {
936 fb1a36c0 2022-01-09 op case READSTRERR:
937 fb1a36c0 2022-01-09 op goto err;
938 fb1a36c0 2022-01-09 op case READSTRTRUNC:
939 fb1a36c0 2022-01-09 op log_warnx("9P version string too long, truncated");
940 6be59daa 2022-05-22 op np_version(hdr->tag, MSIZE9P, "unknown");
941 6be59daa 2022-05-22 op return;
942 fb1a36c0 2022-01-09 op }
943 fb1a36c0 2022-01-09 op
944 bcdb46a7 2022-01-19 op if (len != 0)
945 bcdb46a7 2022-01-19 op goto err;
946 bcdb46a7 2022-01-19 op
947 fb1a36c0 2022-01-09 op if ((dot = strchr(version, '.')) != NULL)
948 fb1a36c0 2022-01-09 op *dot = '\0';
949 fb1a36c0 2022-01-09 op
950 6be59daa 2022-05-22 op if (strcmp(version, VERSION9P) != 0) {
951 6be59daa 2022-05-22 op log_warnx("unknown 9P version \"%s\"; want "VERSION9P,
952 6be59daa 2022-05-22 op version);
953 6be59daa 2022-05-22 op np_version(hdr->tag, MSIZE9P, "unknown");
954 6be59daa 2022-05-22 op return;
955 6be59daa 2022-05-22 op }
956 6be59daa 2022-05-22 op
957 6be59daa 2022-05-22 op if (msize < MIN_MSIZE) {
958 6be59daa 2022-05-22 op log_warnx("msize too small: %"PRIu32"; want %d at least",
959 6be59daa 2022-05-22 op msize, MIN_MSIZE);
960 6be59daa 2022-05-22 op np_version(hdr->tag, MSIZE9P, "unknown");
961 6be59daa 2022-05-22 op return;
962 6be59daa 2022-05-22 op }
963 fb1a36c0 2022-01-09 op
964 fb1a36c0 2022-01-09 op /* version matched */
965 fb1a36c0 2022-01-09 op handshaked = 1;
966 8b0a5062 2022-05-22 op msize = MIN(msize, MSIZE9P);
967 fb1a36c0 2022-01-09 op client_send_listener(IMSG_MSIZE, &msize, sizeof(msize));
968 fb1a36c0 2022-01-09 op np_version(hdr->tag, msize, VERSION9P);
969 fb1a36c0 2022-01-09 op return;
970 fb1a36c0 2022-01-09 op
971 fb1a36c0 2022-01-09 op err:
972 fb1a36c0 2022-01-09 op client_send_listener(IMSG_CLOSE, NULL, 0);
973 fb1a36c0 2022-01-09 op client_shutdown();
974 fb1a36c0 2022-01-09 op }
975 fb1a36c0 2022-01-09 op
976 fb1a36c0 2022-01-09 op static void
977 fb1a36c0 2022-01-09 op tattach(struct np_msg_header *hdr, const uint8_t *data, size_t len)
978 fb1a36c0 2022-01-09 op {
979 fb1a36c0 2022-01-09 op struct dir *dir;
980 fb1a36c0 2022-01-09 op struct fid *f;
981 fb1a36c0 2022-01-09 op uint32_t fid, afid;
982 fb1a36c0 2022-01-09 op int fd;
983 fb1a36c0 2022-01-09 op char aname[PATH_MAX];
984 fb1a36c0 2022-01-09 op
985 fb1a36c0 2022-01-09 op /* fid[4] afid[4] uname[s] aname[s] */
986 fb1a36c0 2022-01-09 op
987 fb1a36c0 2022-01-09 op if (!NPREAD32("fid", &fid, &data, &len) ||
988 fb1a36c0 2022-01-09 op !NPREAD32("afid", &afid, &data, &len))
989 fb1a36c0 2022-01-09 op goto err;
990 fb1a36c0 2022-01-09 op
991 fb1a36c0 2022-01-09 op /* read the uname but don't actually use it */
992 fb1a36c0 2022-01-09 op switch (NPREADSTR("uname", aname, sizeof(aname), &data, &len)) {
993 fb1a36c0 2022-01-09 op case READSTRERR:
994 fb1a36c0 2022-01-09 op goto err;
995 fb1a36c0 2022-01-09 op case READSTRTRUNC:
996 fb1a36c0 2022-01-09 op np_error(hdr->tag, "name too long");
997 fb1a36c0 2022-01-09 op return;
998 fb1a36c0 2022-01-09 op }
999 fb1a36c0 2022-01-09 op
1000 fb1a36c0 2022-01-09 op switch (NPREADSTR("aname", aname, sizeof(aname), &data, &len)) {
1001 fb1a36c0 2022-01-09 op case READSTRERR:
1002 fb1a36c0 2022-01-09 op goto err;
1003 fb1a36c0 2022-01-09 op case READSTRTRUNC:
1004 fb1a36c0 2022-01-09 op np_error(hdr->tag, "name too long");
1005 fb1a36c0 2022-01-09 op return;
1006 fb1a36c0 2022-01-09 op }
1007 fb1a36c0 2022-01-09 op
1008 73df5ca2 2022-02-02 op if (*aname == '\0')
1009 73df5ca2 2022-02-02 op strlcpy(aname, "/", sizeof(aname));
1010 73df5ca2 2022-02-02 op
1011 bcdb46a7 2022-01-19 op if (len != 0)
1012 bcdb46a7 2022-01-19 op goto err;
1013 bcdb46a7 2022-01-19 op
1014 fb1a36c0 2022-01-09 op if (fid_by_id(fid) != NULL || afid != NOFID) {
1015 fb1a36c0 2022-01-09 op np_error(hdr->tag, "invalid fid or afid");
1016 fb1a36c0 2022-01-09 op return;
1017 fb1a36c0 2022-01-09 op }
1018 fb1a36c0 2022-01-09 op
1019 fb1a36c0 2022-01-09 op if ((fd = open(aname, O_RDONLY|O_DIRECTORY)) == -1)
1020 fb1a36c0 2022-01-09 op goto fail;
1021 fb1a36c0 2022-01-09 op
1022 fb1a36c0 2022-01-09 op if ((dir = new_dir(fd)) == NULL)
1023 fb1a36c0 2022-01-09 op goto fail;
1024 fb1a36c0 2022-01-09 op
1025 fb1a36c0 2022-01-09 op log_debug("attached %s to %d", aname, fid);
1026 fb1a36c0 2022-01-09 op
1027 fb1a36c0 2022-01-09 op if ((f = new_fid(dir, fid, aname, NULL)) == NULL) {
1028 fb1a36c0 2022-01-09 op dir_decref(dir);
1029 fb1a36c0 2022-01-09 op goto fail;
1030 fb1a36c0 2022-01-09 op }
1031 fb1a36c0 2022-01-09 op
1032 fb1a36c0 2022-01-09 op np_attach(hdr->tag, &f->qid);
1033 fb1a36c0 2022-01-09 op return;
1034 fb1a36c0 2022-01-09 op
1035 fb1a36c0 2022-01-09 op fail:
1036 fb1a36c0 2022-01-09 op np_errno(hdr->tag);
1037 fb1a36c0 2022-01-09 op log_warn("failed to attach %s", aname);
1038 fb1a36c0 2022-01-09 op return;
1039 fb1a36c0 2022-01-09 op
1040 fb1a36c0 2022-01-09 op err:
1041 fb1a36c0 2022-01-09 op client_send_listener(IMSG_CLOSE, NULL, 0);
1042 fb1a36c0 2022-01-09 op client_shutdown();
1043 fb1a36c0 2022-01-09 op }
1044 fb1a36c0 2022-01-09 op
1045 fb1a36c0 2022-01-09 op static void
1046 fb1a36c0 2022-01-09 op tclunk(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1047 fb1a36c0 2022-01-09 op {
1048 fb1a36c0 2022-01-09 op struct fid *f;
1049 fb1a36c0 2022-01-09 op uint32_t fid;
1050 fb1a36c0 2022-01-09 op
1051 fb1a36c0 2022-01-09 op /* fid[4] */
1052 bcdb46a7 2022-01-19 op if (!NPREAD32("fid", &fid, &data, &len) ||
1053 bcdb46a7 2022-01-19 op len != 0) {
1054 fb1a36c0 2022-01-09 op client_send_listener(IMSG_CLOSE, NULL, 0);
1055 fb1a36c0 2022-01-09 op client_shutdown();
1056 fb1a36c0 2022-01-09 op return;
1057 fb1a36c0 2022-01-09 op }
1058 fb1a36c0 2022-01-09 op
1059 fb1a36c0 2022-01-09 op if ((f = fid_by_id(fid)) == NULL) {
1060 fb1a36c0 2022-01-09 op np_error(hdr->tag, "invalid fid");
1061 fb1a36c0 2022-01-09 op return;
1062 fb1a36c0 2022-01-09 op }
1063 fb1a36c0 2022-01-09 op
1064 fb1a36c0 2022-01-09 op free_fid(f);
1065 fb1a36c0 2022-01-09 op np_clunk(hdr->tag);
1066 fb1a36c0 2022-01-09 op }
1067 fb1a36c0 2022-01-09 op
1068 fb1a36c0 2022-01-09 op static void
1069 fb1a36c0 2022-01-09 op tflush(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1070 fb1a36c0 2022-01-09 op {
1071 fb1a36c0 2022-01-09 op uint16_t oldtag;
1072 fb1a36c0 2022-01-09 op
1073 fb1a36c0 2022-01-09 op /*
1074 fb1a36c0 2022-01-09 op * We're doing only synchronous I/O. Tflush is implemented
1075 fb1a36c0 2022-01-09 op * only because it's illegal to reply with a Rerror.
1076 fb1a36c0 2022-01-09 op */
1077 fb1a36c0 2022-01-09 op
1078 fb1a36c0 2022-01-09 op /* oldtag[2] */
1079 fb1a36c0 2022-01-09 op if (len != sizeof(oldtag)) {
1080 fb1a36c0 2022-01-09 op log_warnx("Tflush with the wrong size: got %zu want %zu",
1081 fb1a36c0 2022-01-09 op len, sizeof(oldtag));
1082 fb1a36c0 2022-01-09 op client_send_listener(IMSG_CLOSE, NULL, 0);
1083 fb1a36c0 2022-01-09 op client_shutdown();
1084 fb1a36c0 2022-01-09 op return;
1085 fb1a36c0 2022-01-09 op }
1086 fb1a36c0 2022-01-09 op
1087 fb1a36c0 2022-01-09 op np_flush(hdr->tag);
1088 fb1a36c0 2022-01-09 op }
1089 fb1a36c0 2022-01-09 op
1090 fb1a36c0 2022-01-09 op static void
1091 fb1a36c0 2022-01-09 op twalk(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1092 fb1a36c0 2022-01-09 op {
1093 fb1a36c0 2022-01-09 op struct stat sb;
1094 fb1a36c0 2022-01-09 op struct dir *dir;
1095 fb1a36c0 2022-01-09 op struct qid wqid[MAXWELEM] = {0};
1096 fb1a36c0 2022-01-09 op struct fid *f, *nf;
1097 fb1a36c0 2022-01-09 op uint32_t fid, newfid;
1098 fb1a36c0 2022-01-09 op uint16_t nwname;
1099 fb1a36c0 2022-01-09 op int fd, oldfd, no, nwqid = 0;
1100 fb1a36c0 2022-01-09 op char wnam[PATH_MAX];
1101 fb1a36c0 2022-01-09 op
1102 fb1a36c0 2022-01-09 op if (!NPREAD32("fid", &fid, &data, &len) ||
1103 fb1a36c0 2022-01-09 op !NPREAD32("newfid", &newfid, &data, &len) ||
1104 fb1a36c0 2022-01-09 op !NPREAD16("nwname", &nwname, &data, &len))
1105 fb1a36c0 2022-01-09 op goto err;
1106 fb1a36c0 2022-01-09 op
1107 fb1a36c0 2022-01-09 op if (nwname > MAXWELEM) {
1108 fb1a36c0 2022-01-09 op log_warnx("Twalk: more than %d path elements: %d",
1109 fb1a36c0 2022-01-09 op MAXWELEM, nwname);
1110 fb1a36c0 2022-01-09 op goto err;
1111 fb1a36c0 2022-01-09 op }
1112 fb1a36c0 2022-01-09 op
1113 fb1a36c0 2022-01-09 op if ((f = fid_by_id(fid)) == NULL) {
1114 fb1a36c0 2022-01-09 op np_error(hdr->tag, "invalid fid");
1115 fb1a36c0 2022-01-09 op return;
1116 fb1a36c0 2022-01-09 op }
1117 fb1a36c0 2022-01-09 op
1118 fb1a36c0 2022-01-09 op if (f->fd != -1) {
1119 fb1a36c0 2022-01-09 op np_error(hdr->tag, "fid already opened for I/O");
1120 fb1a36c0 2022-01-09 op return;
1121 fb1a36c0 2022-01-09 op }
1122 fb1a36c0 2022-01-09 op
1123 fb1a36c0 2022-01-09 op if (fid == newfid)
1124 fb1a36c0 2022-01-09 op nf = f;
1125 fb1a36c0 2022-01-09 op else if ((nf = fid_by_id(newfid)) != NULL) {
1126 fb1a36c0 2022-01-09 op np_error(hdr->tag, "newfid already in use");
1127 fb1a36c0 2022-01-09 op return;
1128 fb1a36c0 2022-01-09 op } else
1129 fb1a36c0 2022-01-09 op nf = NULL;
1130 fb1a36c0 2022-01-09 op
1131 fb1a36c0 2022-01-09 op /* special case: fid duplication */
1132 fb1a36c0 2022-01-09 op if (nwname == 0) {
1133 fb1a36c0 2022-01-09 op /*
1134 fb1a36c0 2022-01-09 op * TODO: should we forbid fids duplication when fid ==
1135 fb1a36c0 2022-01-09 op * newfid?
1136 fb1a36c0 2022-01-09 op */
1137 fb1a36c0 2022-01-09 op if (nf == NULL &&
1138 543fc013 2022-01-17 op (nf = new_fid(f->dir, newfid, f->fname, &f->qid)) == NULL)
1139 fb1a36c0 2022-01-09 op fatal("new_fid duplication");
1140 fb1a36c0 2022-01-09 op
1141 fb1a36c0 2022-01-09 op np_walk(hdr->tag, 0, NULL);
1142 fb1a36c0 2022-01-09 op return;
1143 fb1a36c0 2022-01-09 op }
1144 fb1a36c0 2022-01-09 op
1145 fb1a36c0 2022-01-09 op if (!(f->qid.type & QTDIR)) {
1146 fb1a36c0 2022-01-09 op np_error(hdr->tag, "fid doesn't represent a directory");
1147 fb1a36c0 2022-01-09 op return;
1148 fb1a36c0 2022-01-09 op }
1149 fb1a36c0 2022-01-09 op
1150 fb1a36c0 2022-01-09 op oldfd = f->dir->fd;
1151 fb1a36c0 2022-01-09 op
1152 fb1a36c0 2022-01-09 op for (nwqid = 0; nwqid < nwname; nwqid++) {
1153 fb1a36c0 2022-01-09 op switch (NPREADSTR("wname", wnam, sizeof(wnam), &data, &len)) {
1154 fb1a36c0 2022-01-09 op case READSTRERR:
1155 fb1a36c0 2022-01-09 op goto err;
1156 fb1a36c0 2022-01-09 op case READSTRTRUNC:
1157 fb1a36c0 2022-01-09 op np_error(hdr->tag, "wname too long");
1158 fb1a36c0 2022-01-09 op return;
1159 fb1a36c0 2022-01-09 op }
1160 fb1a36c0 2022-01-09 op
1161 fb1a36c0 2022-01-09 op if (*wnam == '\0' ||
1162 fb1a36c0 2022-01-09 op strchr(wnam, '/') != NULL ||
1163 fb1a36c0 2022-01-09 op !strcmp(wnam, ".")) {
1164 fb1a36c0 2022-01-09 op errno = EINVAL;
1165 fb1a36c0 2022-01-09 op goto cantopen;
1166 fb1a36c0 2022-01-09 op }
1167 fb1a36c0 2022-01-09 op
1168 fb1a36c0 2022-01-09 op if ((fd = openat(oldfd, wnam, O_RDONLY|O_DIRECTORY)) == -1 &&
1169 fb1a36c0 2022-01-09 op errno != ENOTDIR)
1170 fb1a36c0 2022-01-09 op goto cantopen;
1171 fb1a36c0 2022-01-09 op
1172 fb1a36c0 2022-01-09 op if ((fd == -1 && fstatat(oldfd, wnam, &sb, 0) == -1) ||
1173 fb1a36c0 2022-01-09 op (fd != -1 && fstat(fd, &sb) == -1))
1174 fb1a36c0 2022-01-09 op goto cantopen;
1175 fb1a36c0 2022-01-09 op
1176 fb1a36c0 2022-01-09 op qid_update_from_sb(&wqid[nwqid], &sb);
1177 fb1a36c0 2022-01-09 op
1178 fb1a36c0 2022-01-09 op /* reached a file but we still have other components */
1179 fb1a36c0 2022-01-09 op if (fd == -1 && nwqid+1 < nwname)
1180 fb1a36c0 2022-01-09 op goto cantopen;
1181 fb1a36c0 2022-01-09 op
1182 fb1a36c0 2022-01-09 op /* reached the end and found a file */
1183 fb1a36c0 2022-01-09 op if (fd == -1 && nwqid+1 == nwname)
1184 fb1a36c0 2022-01-09 op continue;
1185 fb1a36c0 2022-01-09 op
1186 fb1a36c0 2022-01-09 op if (oldfd != f->dir->fd)
1187 fb1a36c0 2022-01-09 op close(oldfd);
1188 fb1a36c0 2022-01-09 op oldfd = fd;
1189 fb1a36c0 2022-01-09 op }
1190 fb1a36c0 2022-01-09 op
1191 bcdb46a7 2022-01-19 op if (len != 0)
1192 bcdb46a7 2022-01-19 op goto err;
1193 bcdb46a7 2022-01-19 op
1194 fb1a36c0 2022-01-09 op /*
1195 fb1a36c0 2022-01-09 op * If fd is -1 we've reached a file, otherwise we've just
1196 fb1a36c0 2022-01-09 op * reached another directory. We must pay attention to what
1197 fb1a36c0 2022-01-09 op * file descriptor we use to create the dir, because if we've
1198 fb1a36c0 2022-01-09 op * reached a file and oldfd is f->dir->fd then we *must* share
1199 fb1a36c0 2022-01-09 op * the same dir (it was a walk of one path from a directory to a
1200 fb1a36c0 2022-01-09 op * file, otherwise fun is bound to happen as soon as the client
1201 fb1a36c0 2022-01-09 op * closes the fid for the directory but keeps the one for the
1202 fb1a36c0 2022-01-09 op * file.
1203 fb1a36c0 2022-01-09 op */
1204 fb1a36c0 2022-01-09 op if (fd == -1 && oldfd == f->dir->fd)
1205 fb1a36c0 2022-01-09 op dir = f->dir;
1206 fb1a36c0 2022-01-09 op else if (fd == -1)
1207 fb1a36c0 2022-01-09 op dir = new_dir(oldfd);
1208 fb1a36c0 2022-01-09 op else
1209 fb1a36c0 2022-01-09 op dir = new_dir(fd);
1210 fb1a36c0 2022-01-09 op
1211 fb1a36c0 2022-01-09 op if (dir == NULL)
1212 fb1a36c0 2022-01-09 op fatal("new_dir");
1213 fb1a36c0 2022-01-09 op
1214 fb1a36c0 2022-01-09 op if (nf == NULL) {
1215 fb1a36c0 2022-01-09 op if ((nf = new_fid(dir, newfid, wnam, &wqid[nwqid-1])) == NULL)
1216 fb1a36c0 2022-01-09 op fatal("new fid");
1217 fb1a36c0 2022-01-09 op } else {
1218 fb1a36c0 2022-01-09 op /* update the dir */
1219 fb1a36c0 2022-01-09 op dir_decref(nf->dir);
1220 fb1a36c0 2022-01-09 op nf->dir = dir_incref(dir);
1221 fb1a36c0 2022-01-09 op }
1222 fb1a36c0 2022-01-09 op
1223 fb1a36c0 2022-01-09 op np_walk(hdr->tag, nwqid, wqid);
1224 fb1a36c0 2022-01-09 op return;
1225 fb1a36c0 2022-01-09 op
1226 fb1a36c0 2022-01-09 op cantopen:
1227 fb1a36c0 2022-01-09 op if (oldfd != f->dir->fd)
1228 fb1a36c0 2022-01-09 op close(oldfd);
1229 fb1a36c0 2022-01-09 op no = errno;
1230 fb1a36c0 2022-01-09 op if (nwqid == 0)
1231 fb1a36c0 2022-01-09 op np_error(hdr->tag, strerror(no));
1232 fb1a36c0 2022-01-09 op else
1233 fb1a36c0 2022-01-09 op np_walk(hdr->tag, nwqid, wqid);
1234 fb1a36c0 2022-01-09 op return;
1235 fb1a36c0 2022-01-09 op
1236 fb1a36c0 2022-01-09 op err:
1237 fb1a36c0 2022-01-09 op client_send_listener(IMSG_CLOSE, NULL, 0);
1238 fb1a36c0 2022-01-09 op client_shutdown();
1239 fb1a36c0 2022-01-09 op }
1240 fb1a36c0 2022-01-09 op
1241 fb1a36c0 2022-01-09 op static inline int
1242 fb1a36c0 2022-01-09 op npmode_to_unix(uint8_t mode, int *flags)
1243 fb1a36c0 2022-01-09 op {
1244 fb1a36c0 2022-01-09 op switch (mode & 0x0F) {
1245 fb1a36c0 2022-01-09 op case KOREAD:
1246 fb1a36c0 2022-01-09 op *flags = O_RDONLY;
1247 fb1a36c0 2022-01-09 op break;
1248 fb1a36c0 2022-01-09 op case KOWRITE:
1249 fb1a36c0 2022-01-09 op *flags = O_WRONLY;
1250 fb1a36c0 2022-01-09 op break;
1251 fb1a36c0 2022-01-09 op case KORDWR:
1252 fb1a36c0 2022-01-09 op *flags = O_RDWR;
1253 fb1a36c0 2022-01-09 op break;
1254 fb1a36c0 2022-01-09 op case KOEXEC:
1255 fb1a36c0 2022-01-09 op log_warnx("tried to open something with KOEXEC");
1256 fb1a36c0 2022-01-09 op /* fallthrough */
1257 fb1a36c0 2022-01-09 op default:
1258 fb1a36c0 2022-01-09 op return -1;
1259 fb1a36c0 2022-01-09 op }
1260 fb1a36c0 2022-01-09 op
1261 fb1a36c0 2022-01-09 op if (mode & KOTRUNC)
1262 fb1a36c0 2022-01-09 op *flags |= O_TRUNC;
1263 fb1a36c0 2022-01-09 op if (mode & KORCLOSE)
1264 fb1a36c0 2022-01-09 op *flags |= O_CLOEXEC;
1265 fb1a36c0 2022-01-09 op
1266 fb1a36c0 2022-01-09 op return 0;
1267 fb1a36c0 2022-01-09 op }
1268 fb1a36c0 2022-01-09 op
1269 fb1a36c0 2022-01-09 op static void
1270 fb1a36c0 2022-01-09 op topen(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1271 fb1a36c0 2022-01-09 op {
1272 fb1a36c0 2022-01-09 op struct stat sb;
1273 fb1a36c0 2022-01-09 op struct qid qid;
1274 fb1a36c0 2022-01-09 op struct fid *f;
1275 fb1a36c0 2022-01-09 op uint32_t fid;
1276 fb1a36c0 2022-01-09 op uint8_t mode;
1277 fb1a36c0 2022-01-09 op const char *path;
1278 fb1a36c0 2022-01-09 op
1279 fb1a36c0 2022-01-09 op /* fid[4] mode[1] */
1280 fb1a36c0 2022-01-09 op if (!NPREAD32("fid", &fid, &data, &len) ||
1281 bcdb46a7 2022-01-19 op !NPREAD8("mode", &mode, &data, &len) ||
1282 bcdb46a7 2022-01-19 op len != 0) {
1283 fb1a36c0 2022-01-09 op client_send_listener(IMSG_CLOSE, NULL, 0);
1284 fb1a36c0 2022-01-09 op client_shutdown();
1285 fb1a36c0 2022-01-09 op return;
1286 fb1a36c0 2022-01-09 op }
1287 fb1a36c0 2022-01-09 op
1288 fb1a36c0 2022-01-09 op if ((f = fid_by_id(fid)) == NULL || f->fd != -1) {
1289 fb1a36c0 2022-01-09 op np_error(hdr->tag, "invalid fid");
1290 fb1a36c0 2022-01-09 op return;
1291 fb1a36c0 2022-01-09 op }
1292 fb1a36c0 2022-01-09 op
1293 fb1a36c0 2022-01-09 op if (npmode_to_unix(mode, &f->iomode) == -1) {
1294 fb1a36c0 2022-01-09 op np_error(hdr->tag, "invalid mode");
1295 fb1a36c0 2022-01-09 op return;
1296 fb1a36c0 2022-01-09 op }
1297 fb1a36c0 2022-01-09 op
1298 543fc013 2022-01-17 op path = f->fname;
1299 fb1a36c0 2022-01-09 op if (f->qid.type & QTDIR)
1300 fb1a36c0 2022-01-09 op path = ".";
1301 fb1a36c0 2022-01-09 op
1302 fb1a36c0 2022-01-09 op if ((f->fd = openat(f->dir->fd, path, f->iomode)) == -1) {
1303 fb1a36c0 2022-01-09 op np_error(hdr->tag, strerror(errno));
1304 fb1a36c0 2022-01-09 op return;
1305 fb1a36c0 2022-01-09 op }
1306 fb1a36c0 2022-01-09 op
1307 fb1a36c0 2022-01-09 op if (fstat(f->fd, &sb) == -1)
1308 fb1a36c0 2022-01-09 op fatal("fstat");
1309 fb1a36c0 2022-01-09 op
1310 fb1a36c0 2022-01-09 op if (S_ISDIR(sb.st_mode)) {
1311 fb1a36c0 2022-01-09 op if ((f->d = fdopendir(f->fd)) == NULL) {
1312 fb1a36c0 2022-01-09 op np_errno(hdr->tag);
1313 fb1a36c0 2022-01-09 op close(f->fd);
1314 fb1a36c0 2022-01-09 op f->fd = -1;
1315 fb1a36c0 2022-01-09 op return;
1316 fb1a36c0 2022-01-09 op }
1317 fb1a36c0 2022-01-09 op
1318 fb1a36c0 2022-01-09 op if ((f->evb = evbuffer_new()) == NULL) {
1319 fb1a36c0 2022-01-09 op np_errno(hdr->tag);
1320 fb1a36c0 2022-01-09 op closedir(f->d);
1321 fb1a36c0 2022-01-09 op f->d = NULL;
1322 fb1a36c0 2022-01-09 op f->fd = -1;
1323 fb1a36c0 2022-01-09 op }
1324 fb1a36c0 2022-01-09 op }
1325 fb1a36c0 2022-01-09 op
1326 fb1a36c0 2022-01-09 op f->offset = 0;
1327 fb1a36c0 2022-01-09 op
1328 fb1a36c0 2022-01-09 op qid_update_from_sb(&qid, &sb);
1329 fb1a36c0 2022-01-09 op np_open(hdr->tag, &qid, sb.st_blksize);
1330 fb1a36c0 2022-01-09 op }
1331 fb1a36c0 2022-01-09 op
1332 fb1a36c0 2022-01-09 op static void
1333 fb1a36c0 2022-01-09 op tcreate(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1334 fb1a36c0 2022-01-09 op {
1335 fb1a36c0 2022-01-09 op struct stat sb;
1336 fb1a36c0 2022-01-09 op struct qid qid;
1337 fb1a36c0 2022-01-09 op struct fid *f;
1338 fb1a36c0 2022-01-09 op uint32_t fid, perm;
1339 fb1a36c0 2022-01-09 op uint8_t mode;
1340 fb1a36c0 2022-01-09 op char name[PATH_MAX];
1341 fb1a36c0 2022-01-09 op
1342 fb1a36c0 2022-01-09 op /* fid[4] name[s] perm[4] mode[1] */
1343 fb1a36c0 2022-01-09 op if (!NPREAD32("fid", &fid, &data, &len))
1344 fb1a36c0 2022-01-09 op goto err;
1345 fb1a36c0 2022-01-09 op switch (NPREADSTR("name", name, sizeof(name), &data, &len)) {
1346 fb1a36c0 2022-01-09 op case READSTRERR:
1347 fb1a36c0 2022-01-09 op goto err;
1348 fb1a36c0 2022-01-09 op case READSTRTRUNC:
1349 fb1a36c0 2022-01-09 op np_error(hdr->tag, "name too long");
1350 fb1a36c0 2022-01-09 op return;
1351 fb1a36c0 2022-01-09 op }
1352 fb1a36c0 2022-01-09 op if (!NPREAD32("perm", &perm, &data, &len) ||
1353 bcdb46a7 2022-01-19 op !NPREAD8("mode", &mode, &data, &len) ||
1354 bcdb46a7 2022-01-19 op len != 0)
1355 fb1a36c0 2022-01-09 op goto err;
1356 fb1a36c0 2022-01-09 op
1357 fb1a36c0 2022-01-09 op if (!strcmp(name, ".") || !strcmp(name, "..") ||
1358 fb1a36c0 2022-01-09 op strchr(name, '/') != NULL) {
1359 fb1a36c0 2022-01-09 op np_error(hdr->tag, "invalid name");
1360 fb1a36c0 2022-01-09 op return;
1361 fb1a36c0 2022-01-09 op }
1362 fb1a36c0 2022-01-09 op
1363 fb1a36c0 2022-01-09 op if ((f = fid_by_id(fid)) == NULL || f->fd != -1) {
1364 fb1a36c0 2022-01-09 op np_error(hdr->tag, "invalid fid");
1365 fb1a36c0 2022-01-09 op return;
1366 fb1a36c0 2022-01-09 op }
1367 fb1a36c0 2022-01-09 op
1368 fb1a36c0 2022-01-09 op if (!(f->qid.type & QTDIR)) {
1369 fb1a36c0 2022-01-09 op np_error(hdr->tag, "fid doesn't identify a directory");
1370 fb1a36c0 2022-01-09 op return;
1371 fb1a36c0 2022-01-09 op }
1372 fb1a36c0 2022-01-09 op
1373 fb1a36c0 2022-01-09 op if (npmode_to_unix(mode, &f->iomode) == -1) {
1374 fb1a36c0 2022-01-09 op np_error(hdr->tag, "invalid mode");
1375 fb1a36c0 2022-01-09 op return;
1376 fb1a36c0 2022-01-09 op }
1377 fb1a36c0 2022-01-09 op
1378 fb1a36c0 2022-01-09 op if (f->iomode & O_RDONLY) {
1379 fb1a36c0 2022-01-09 op np_error(hdr->tag, "can't create a read-only file");
1380 fb1a36c0 2022-01-09 op return;
1381 fb1a36c0 2022-01-09 op }
1382 fb1a36c0 2022-01-09 op
1383 fb1a36c0 2022-01-09 op /* TODO: parse the mode */
1384 fb1a36c0 2022-01-09 op
1385 fb1a36c0 2022-01-09 op if (perm & 0x80000000) {
1386 fb1a36c0 2022-01-09 op /* create a directory */
1387 fb1a36c0 2022-01-09 op f->fd = mkdirat(f->dir->fd, name, 0755);
1388 fb1a36c0 2022-01-09 op } else {
1389 fb1a36c0 2022-01-09 op /* create a file */
1390 fb1a36c0 2022-01-09 op f->fd = openat(f->dir->fd, name, f->iomode | O_CREAT | O_TRUNC,
1391 fb1a36c0 2022-01-09 op 0644);
1392 fb1a36c0 2022-01-09 op }
1393 fb1a36c0 2022-01-09 op
1394 fb1a36c0 2022-01-09 op if (f->fd == -1) {
1395 fb1a36c0 2022-01-09 op np_errno(hdr->tag);
1396 fb1a36c0 2022-01-09 op return;
1397 fb1a36c0 2022-01-09 op }
1398 fb1a36c0 2022-01-09 op
1399 fb1a36c0 2022-01-09 op if (fstat(f->fd, &sb) == -1)
1400 fb1a36c0 2022-01-09 op fatal("fstat");
1401 fb1a36c0 2022-01-09 op
1402 fb1a36c0 2022-01-09 op if (S_ISDIR(sb.st_mode)) {
1403 fb1a36c0 2022-01-09 op if ((f->d = fdopendir(f->fd)) == NULL) {
1404 fb1a36c0 2022-01-09 op np_errno(hdr->tag);
1405 fb1a36c0 2022-01-09 op close(f->fd);
1406 fb1a36c0 2022-01-09 op f->fd = -1;
1407 fb1a36c0 2022-01-09 op return;
1408 fb1a36c0 2022-01-09 op }
1409 fb1a36c0 2022-01-09 op
1410 fb1a36c0 2022-01-09 op if ((f->evb = evbuffer_new()) == NULL) {
1411 fb1a36c0 2022-01-09 op np_errno(hdr->tag);
1412 fb1a36c0 2022-01-09 op closedir(f->d);
1413 fb1a36c0 2022-01-09 op f->d = NULL;
1414 fb1a36c0 2022-01-09 op f->fd = -1;
1415 fb1a36c0 2022-01-09 op }
1416 fb1a36c0 2022-01-09 op }
1417 fb1a36c0 2022-01-09 op
1418 fb1a36c0 2022-01-09 op f->offset = 0;
1419 fb1a36c0 2022-01-09 op
1420 fb1a36c0 2022-01-09 op qid_update_from_sb(&qid, &sb);
1421 fb1a36c0 2022-01-09 op np_create(hdr->tag, &qid, sb.st_blksize);
1422 fb1a36c0 2022-01-09 op
1423 fb1a36c0 2022-01-09 op return;
1424 fb1a36c0 2022-01-09 op
1425 fb1a36c0 2022-01-09 op err:
1426 fb1a36c0 2022-01-09 op client_send_listener(IMSG_CLOSE, NULL, 0);
1427 fb1a36c0 2022-01-09 op client_shutdown();
1428 fb1a36c0 2022-01-09 op }
1429 fb1a36c0 2022-01-09 op
1430 91f48e25 2022-02-02 op static inline int
1431 fb1a36c0 2022-01-09 op serialize_stat(const char *fname, struct stat *sb, struct evbuffer *evb)
1432 fb1a36c0 2022-01-09 op {
1433 fb1a36c0 2022-01-09 op struct qid qid;
1434 fb1a36c0 2022-01-09 op const char *uid, *gid, *muid;
1435 fb1a36c0 2022-01-09 op size_t tot;
1436 bee0a9e3 2022-02-02 op uint32_t mode;
1437 fb1a36c0 2022-01-09 op uint16_t namlen, uidlen, gidlen, ulen;
1438 fb1a36c0 2022-01-09 op
1439 fb1a36c0 2022-01-09 op qid_update_from_sb(&qid, sb);
1440 fb1a36c0 2022-01-09 op
1441 fb1a36c0 2022-01-09 op /* TODO: fill these fields */
1442 fb1a36c0 2022-01-09 op uid = "";
1443 fb1a36c0 2022-01-09 op gid = "";
1444 fb1a36c0 2022-01-09 op muid = "";
1445 fb1a36c0 2022-01-09 op
1446 fb1a36c0 2022-01-09 op namlen = strlen(fname);
1447 fb1a36c0 2022-01-09 op uidlen = strlen(uid);
1448 fb1a36c0 2022-01-09 op gidlen = strlen(gid);
1449 fb1a36c0 2022-01-09 op ulen = strlen(muid);
1450 fb1a36c0 2022-01-09 op
1451 fb1a36c0 2022-01-09 op tot = NPSTATSIZ(namlen, uidlen, gidlen, ulen);
1452 91f48e25 2022-02-02 op if (tot > UINT16_MAX) {
1453 fb1a36c0 2022-01-09 op log_warnx("stat info for dir entry %s would overflow",
1454 fb1a36c0 2022-01-09 op fname);
1455 91f48e25 2022-02-02 op return -1;
1456 fb1a36c0 2022-01-09 op }
1457 bee0a9e3 2022-02-02 op
1458 bee0a9e3 2022-02-02 op mode = sb->st_mode & 0xFFFF;
1459 bee0a9e3 2022-02-02 op if (qid.type & QTDIR)
1460 bee0a9e3 2022-02-02 op mode |= 0x80000000;
1461 fb1a36c0 2022-01-09 op
1462 fb1a36c0 2022-01-09 op np_write16(evb, tot); /* size[2] */
1463 fb1a36c0 2022-01-09 op np_write16(evb, sb->st_rdev); /* type[2] */
1464 fb1a36c0 2022-01-09 op np_write32(evb, sb->st_dev); /* dev[4] */
1465 fb1a36c0 2022-01-09 op np_qid(evb, &qid); /* qid[13] */
1466 bee0a9e3 2022-02-02 op np_write32(evb, mode); /* mode[4] */
1467 fb1a36c0 2022-01-09 op np_write32(evb, sb->st_atim.tv_sec); /* atime[4] */
1468 fb1a36c0 2022-01-09 op np_write32(evb, sb->st_mtim.tv_sec); /* mtime[4] */
1469 68a7ef68 2022-01-16 op
1470 68a7ef68 2022-01-16 op /* special case: directories have size 0 */
1471 68a7ef68 2022-01-16 op if (qid.type & QTDIR)
1472 68a7ef68 2022-01-16 op np_write64(evb, 0);
1473 68a7ef68 2022-01-16 op else
1474 68a7ef68 2022-01-16 op np_write64(evb, sb->st_size); /* length[8] */
1475 68a7ef68 2022-01-16 op
1476 fb1a36c0 2022-01-09 op np_string(evb, namlen, fname); /* name[s] */
1477 fb1a36c0 2022-01-09 op np_string(evb, uidlen, uid); /* uid[s] */
1478 fb1a36c0 2022-01-09 op np_string(evb, gidlen, gid); /* gid[s] */
1479 fb1a36c0 2022-01-09 op np_string(evb, ulen, muid); /* muid[s] */
1480 91f48e25 2022-02-02 op
1481 91f48e25 2022-02-02 op return 0;
1482 fb1a36c0 2022-01-09 op }
1483 fb1a36c0 2022-01-09 op
1484 fb1a36c0 2022-01-09 op static void
1485 fb1a36c0 2022-01-09 op tread(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1486 fb1a36c0 2022-01-09 op {
1487 fb1a36c0 2022-01-09 op struct fid *f;
1488 fb1a36c0 2022-01-09 op ssize_t r;
1489 fb1a36c0 2022-01-09 op size_t howmuch;
1490 fb1a36c0 2022-01-09 op uint64_t off;
1491 fb1a36c0 2022-01-09 op uint32_t fid, count;
1492 fb1a36c0 2022-01-09 op char buf[2048];
1493 fb1a36c0 2022-01-09 op
1494 fb1a36c0 2022-01-09 op /* fid[4] offset[8] count[4] */
1495 fb1a36c0 2022-01-09 op if (!NPREAD32("fid", &fid, &data, &len) ||
1496 fb1a36c0 2022-01-09 op !NPREAD64("offset", &off, &data, &len) ||
1497 bcdb46a7 2022-01-19 op !NPREAD32("count", &count, &data, &len) ||
1498 bcdb46a7 2022-01-19 op len != 0) {
1499 fb1a36c0 2022-01-09 op client_send_listener(IMSG_CLOSE, NULL, 0);
1500 fb1a36c0 2022-01-09 op client_shutdown();
1501 fb1a36c0 2022-01-09 op return;
1502 fb1a36c0 2022-01-09 op }
1503 fb1a36c0 2022-01-09 op
1504 fb1a36c0 2022-01-09 op if ((f = fid_by_id(fid)) == NULL || f->fd == -1) {
1505 fb1a36c0 2022-01-09 op np_error(hdr->tag, "invalid fid");
1506 fb1a36c0 2022-01-09 op return;
1507 fb1a36c0 2022-01-09 op }
1508 fb1a36c0 2022-01-09 op
1509 fb1a36c0 2022-01-09 op if (TYPE_OVERFLOW(off_t, off)) {
1510 fb1a36c0 2022-01-09 op log_warnx("unexpected off_t size");
1511 fb1a36c0 2022-01-09 op np_error(hdr->tag, "invalid offset");
1512 fb1a36c0 2022-01-09 op return;
1513 fb1a36c0 2022-01-09 op }
1514 fb1a36c0 2022-01-09 op
1515 fb1a36c0 2022-01-09 op if (f->d == NULL) {
1516 fb1a36c0 2022-01-09 op /* read a file */
1517 fb1a36c0 2022-01-09 op howmuch = MIN(sizeof(buf), count);
1518 fb1a36c0 2022-01-09 op r = pread(f->fd, buf, howmuch, (off_t)off);
1519 fb1a36c0 2022-01-09 op if (r == -1)
1520 fb1a36c0 2022-01-09 op np_errno(hdr->tag);
1521 fb1a36c0 2022-01-09 op else
1522 fb1a36c0 2022-01-09 op np_read(hdr->tag, r, buf);
1523 fb1a36c0 2022-01-09 op } else {
1524 fb1a36c0 2022-01-09 op if (off == 0 && f->offset != 0) {
1525 fb1a36c0 2022-01-09 op rewinddir(f->d);
1526 fb1a36c0 2022-01-09 op f->offset = 0;
1527 fb1a36c0 2022-01-09 op evbuffer_drain(f->evb, EVBUFFER_LENGTH(f->evb));
1528 fb1a36c0 2022-01-09 op }
1529 fb1a36c0 2022-01-09 op
1530 fb1a36c0 2022-01-09 op if (off != f->offset) {
1531 fb1a36c0 2022-01-09 op np_error(hdr->tag, "can't seek in directories");
1532 fb1a36c0 2022-01-09 op return;
1533 fb1a36c0 2022-01-09 op }
1534 fb1a36c0 2022-01-09 op
1535 fb1a36c0 2022-01-09 op while (EVBUFFER_LENGTH(f->evb) < count) {
1536 fb1a36c0 2022-01-09 op struct dirent *d;
1537 fb1a36c0 2022-01-09 op struct stat sb;
1538 fb1a36c0 2022-01-09 op
1539 fb1a36c0 2022-01-09 op if ((d = readdir(f->d)) == NULL)
1540 fb1a36c0 2022-01-09 op break;
1541 ff6f19b1 2022-02-02 op if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
1542 ff6f19b1 2022-02-02 op continue;
1543 fb1a36c0 2022-01-09 op if (fstatat(f->fd, d->d_name, &sb, 0) == -1) {
1544 fb1a36c0 2022-01-09 op warn("fstatat");
1545 fb1a36c0 2022-01-09 op continue;
1546 fb1a36c0 2022-01-09 op }
1547 fb1a36c0 2022-01-09 op serialize_stat(d->d_name, &sb, f->evb);
1548 fb1a36c0 2022-01-09 op }
1549 fb1a36c0 2022-01-09 op
1550 fb1a36c0 2022-01-09 op count = MIN(count, EVBUFFER_LENGTH(f->evb));
1551 fb1a36c0 2022-01-09 op np_read(hdr->tag, count, EVBUFFER_DATA(f->evb));
1552 fb1a36c0 2022-01-09 op evbuffer_drain(f->evb, count);
1553 fb1a36c0 2022-01-09 op
1554 fb1a36c0 2022-01-09 op f->offset += count;
1555 fb1a36c0 2022-01-09 op }
1556 fb1a36c0 2022-01-09 op }
1557 fb1a36c0 2022-01-09 op
1558 fb1a36c0 2022-01-09 op static void
1559 8b0a5062 2022-05-22 op twrite(struct np_msg_header *hdr, const uint8_t *data, size_t len,
1560 8b0a5062 2022-05-22 op struct fid **writefid, off_t *writepos, size_t *writeleft, int *writeskip)
1561 fb1a36c0 2022-01-09 op {
1562 fb1a36c0 2022-01-09 op struct fid *f;
1563 fb1a36c0 2022-01-09 op ssize_t r;
1564 fb1a36c0 2022-01-09 op uint64_t off;
1565 fb1a36c0 2022-01-09 op uint32_t fid, count;
1566 fb1a36c0 2022-01-09 op
1567 fb1a36c0 2022-01-09 op /* fid[4] offset[8] count[4] data[count] */
1568 fb1a36c0 2022-01-09 op if (!NPREAD32("fid", &fid, &data, &len) ||
1569 fb1a36c0 2022-01-09 op !NPREAD64("off", &off, &data, &len) ||
1570 fb1a36c0 2022-01-09 op !NPREAD32("count", &count, &data, &len) ||
1571 8b0a5062 2022-05-22 op count < len) {
1572 fb1a36c0 2022-01-09 op client_send_listener(IMSG_CLOSE, NULL, 0);
1573 fb1a36c0 2022-01-09 op client_shutdown();
1574 fb1a36c0 2022-01-09 op return;
1575 fb1a36c0 2022-01-09 op }
1576 fb1a36c0 2022-01-09 op
1577 fb1a36c0 2022-01-09 op if ((f = fid_by_id(fid)) == NULL || f->fd == -1) {
1578 8b0a5062 2022-05-22 op *writeskip = 1;
1579 fb1a36c0 2022-01-09 op np_error(hdr->tag, "invalid fid");
1580 fb1a36c0 2022-01-09 op return;
1581 fb1a36c0 2022-01-09 op }
1582 fb1a36c0 2022-01-09 op
1583 fb1a36c0 2022-01-09 op if (!(f->iomode & O_WRONLY) &&
1584 fb1a36c0 2022-01-09 op !(f->iomode & O_RDWR)) {
1585 8b0a5062 2022-05-22 op *writeskip = 1;
1586 fb1a36c0 2022-01-09 op np_error(hdr->tag, "fid not opened for writing");
1587 fb1a36c0 2022-01-09 op return;
1588 fb1a36c0 2022-01-09 op }
1589 fb1a36c0 2022-01-09 op
1590 fb1a36c0 2022-01-09 op if (TYPE_OVERFLOW(off_t, off)) {
1591 8b0a5062 2022-05-22 op *writeskip = 1;
1592 fb1a36c0 2022-01-09 op log_warnx("unexpected off_t size");
1593 fb1a36c0 2022-01-09 op np_error(hdr->tag, "invalid offset");
1594 fb1a36c0 2022-01-09 op return;
1595 fb1a36c0 2022-01-09 op }
1596 fb1a36c0 2022-01-09 op
1597 8b0a5062 2022-05-22 op if ((r = pwrite(f->fd, data, len, off)) == -1) {
1598 8b0a5062 2022-05-22 op *writeskip = 1;
1599 8b0a5062 2022-05-22 op np_errno(hdr->tag);
1600 8b0a5062 2022-05-22 op } else if (count == len)
1601 fb1a36c0 2022-01-09 op np_write(hdr->tag, r);
1602 8b0a5062 2022-05-22 op
1603 8b0a5062 2022-05-22 op /* account for a continuated write */
1604 8b0a5062 2022-05-22 op if (count > len) {
1605 8b0a5062 2022-05-22 op *writefid = f;
1606 8b0a5062 2022-05-22 op *writepos = off + len;
1607 8b0a5062 2022-05-22 op *writeleft = count - len;
1608 8b0a5062 2022-05-22 op *writeskip = 0;
1609 8b0a5062 2022-05-22 op }
1610 fb1a36c0 2022-01-09 op }
1611 fb1a36c0 2022-01-09 op
1612 fb1a36c0 2022-01-09 op static void
1613 8b0a5062 2022-05-22 op twrite_cont(struct fid *f, off_t *writepos, size_t *writeleft, int *writeskip,
1614 8b0a5062 2022-05-22 op uint16_t tag, const uint8_t *data, size_t len)
1615 8b0a5062 2022-05-22 op {
1616 8b0a5062 2022-05-22 op ssize_t r;
1617 8b0a5062 2022-05-22 op
1618 8b0a5062 2022-05-22 op if (len > *writeleft) {
1619 8b0a5062 2022-05-22 op client_send_listener(IMSG_CLOSE, NULL, 0);
1620 8b0a5062 2022-05-22 op client_shutdown();
1621 8b0a5062 2022-05-22 op return;
1622 8b0a5062 2022-05-22 op }
1623 8b0a5062 2022-05-22 op
1624 8b0a5062 2022-05-22 op if ((r = pwrite(f->fd, data, len, *writepos)) == -1) {
1625 8b0a5062 2022-05-22 op *writeskip = 1;
1626 8b0a5062 2022-05-22 op np_errno(tag);
1627 8b0a5062 2022-05-22 op return;
1628 8b0a5062 2022-05-22 op }
1629 8b0a5062 2022-05-22 op
1630 8b0a5062 2022-05-22 op *writeleft -= len;
1631 8b0a5062 2022-05-22 op *writepos += len;
1632 8b0a5062 2022-05-22 op
1633 8b0a5062 2022-05-22 op if (*writeleft == 0)
1634 8b0a5062 2022-05-22 op np_write(tag, r);
1635 8b0a5062 2022-05-22 op }
1636 8b0a5062 2022-05-22 op
1637 8b0a5062 2022-05-22 op static void
1638 fb1a36c0 2022-01-09 op tstat(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1639 fb1a36c0 2022-01-09 op {
1640 fb1a36c0 2022-01-09 op struct evbuffer *evb;
1641 fb1a36c0 2022-01-09 op struct stat sb;
1642 fb1a36c0 2022-01-09 op struct fid *f;
1643 fb1a36c0 2022-01-09 op int r;
1644 fb1a36c0 2022-01-09 op uint32_t fid;
1645 fb1a36c0 2022-01-09 op
1646 fb1a36c0 2022-01-09 op /* fid[4] */
1647 bcdb46a7 2022-01-19 op if (!NPREAD32("fid", &fid, &data, &len) ||
1648 bcdb46a7 2022-01-19 op len != 0) {
1649 fb1a36c0 2022-01-09 op client_send_listener(IMSG_CLOSE, NULL, 0);
1650 fb1a36c0 2022-01-09 op client_shutdown();
1651 fb1a36c0 2022-01-09 op return;
1652 fb1a36c0 2022-01-09 op }
1653 fb1a36c0 2022-01-09 op
1654 fb1a36c0 2022-01-09 op /*
1655 fb1a36c0 2022-01-09 op * plan9' stat(9P) is not clear on whether the stat is allowed
1656 fb1a36c0 2022-01-09 op * on opened fids or not. We're allowing stat regardless of the
1657 fb1a36c0 2022-01-09 op * status of the fid.
1658 fb1a36c0 2022-01-09 op */
1659 fb1a36c0 2022-01-09 op
1660 fb1a36c0 2022-01-09 op if ((f = fid_by_id(fid)) == NULL) {
1661 fb1a36c0 2022-01-09 op np_error(hdr->tag, "invalid fid");
1662 fb1a36c0 2022-01-09 op return;
1663 fb1a36c0 2022-01-09 op }
1664 fb1a36c0 2022-01-09 op
1665 fb1a36c0 2022-01-09 op if ((evb = evbuffer_new()) == NULL)
1666 fb1a36c0 2022-01-09 op fatal("evbuffer_new");
1667 fb1a36c0 2022-01-09 op
1668 fb1a36c0 2022-01-09 op if (f->fd != -1)
1669 fb1a36c0 2022-01-09 op r = fstat(f->fd, &sb);
1670 fb1a36c0 2022-01-09 op else if (f->qid.type & QTDIR)
1671 fb1a36c0 2022-01-09 op r = fstat(f->dir->fd, &sb);
1672 fb1a36c0 2022-01-09 op else
1673 543fc013 2022-01-17 op r = fstatat(f->dir->fd, f->fname, &sb, 0);
1674 fb1a36c0 2022-01-09 op
1675 fb1a36c0 2022-01-09 op if (r == -1) {
1676 fb1a36c0 2022-01-09 op np_errno(hdr->tag);
1677 fb1a36c0 2022-01-09 op evbuffer_free(evb);
1678 fb1a36c0 2022-01-09 op return;
1679 fb1a36c0 2022-01-09 op }
1680 fb1a36c0 2022-01-09 op
1681 91f48e25 2022-02-02 op if (serialize_stat(f->fname, &sb, evb) == -1)
1682 91f48e25 2022-02-02 op np_error(hdr->tag, "stat would overflow");
1683 91f48e25 2022-02-02 op else
1684 91f48e25 2022-02-02 op np_stat(hdr->tag, EVBUFFER_LENGTH(evb), EVBUFFER_DATA(evb));
1685 fb1a36c0 2022-01-09 op evbuffer_free(evb);
1686 755d75bf 2022-01-17 op }
1687 755d75bf 2022-01-17 op
1688 755d75bf 2022-01-17 op static void
1689 755d75bf 2022-01-17 op twstat(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1690 755d75bf 2022-01-17 op {
1691 755d75bf 2022-01-17 op struct timespec times[2];
1692 755d75bf 2022-01-17 op struct np_stat st;
1693 755d75bf 2022-01-17 op struct fid *f;
1694 755d75bf 2022-01-17 op int r;
1695 755d75bf 2022-01-17 op uint32_t fid;
1696 755d75bf 2022-01-17 op char name[PATH_MAX];
1697 755d75bf 2022-01-17 op
1698 755d75bf 2022-01-17 op /* fid[4] stat[n] */
1699 755d75bf 2022-01-17 op if (!NPREAD32("fid", &fid, &data, &len))
1700 755d75bf 2022-01-17 op goto err;
1701 755d75bf 2022-01-17 op
1702 755d75bf 2022-01-17 op switch (NPREADST("stat", &st, name, sizeof(name), &data, &len)) {
1703 755d75bf 2022-01-17 op case READSTRERR:
1704 755d75bf 2022-01-17 op goto err;
1705 755d75bf 2022-01-17 op case READSTRTRUNC:
1706 755d75bf 2022-01-17 op log_warnx("wstat new name would overflow");
1707 755d75bf 2022-01-17 op np_error(hdr->tag, "new name too long");
1708 755d75bf 2022-01-17 op return;
1709 755d75bf 2022-01-17 op }
1710 755d75bf 2022-01-17 op
1711 f4ad0146 2022-01-19 op /*
1712 f4ad0146 2022-01-19 op * We skip the reading of some fields voluntarily because we
1713 f4ad0146 2022-01-19 op * don't support chown, so len will always be > 0!
1714 f4ad0146 2022-01-19 op */
1715 f4ad0146 2022-01-19 op #ifdef notyet
1716 bcdb46a7 2022-01-19 op if (len != 0)
1717 bcdb46a7 2022-01-19 op goto err;
1718 f4ad0146 2022-01-19 op #endif
1719 bcdb46a7 2022-01-19 op
1720 755d75bf 2022-01-17 op if ((f = fid_by_id(fid)) == NULL) {
1721 755d75bf 2022-01-17 op np_error(hdr->tag, "invalid fid");
1722 755d75bf 2022-01-17 op return;
1723 755d75bf 2022-01-17 op }
1724 755d75bf 2022-01-17 op
1725 755d75bf 2022-01-17 op /*
1726 755d75bf 2022-01-17 op * 9P wants these edits to be done in an atomic fashion,
1727 755d75bf 2022-01-17 op * something that I don't think it's possible on UNIX without
1728 755d75bf 2022-01-17 op * some special kernel help. Changing atime or mtime,
1729 755d75bf 2022-01-17 op * permissions, or rename the file are different syscalls.
1730 755d75bf 2022-01-17 op *
1731 755d75bf 2022-01-17 op * Also, silently ignore stuff we can't/don't want to modify.
1732 755d75bf 2022-01-17 op */
1733 755d75bf 2022-01-17 op
1734 755d75bf 2022-01-17 op /* change the permissions */
1735 755d75bf 2022-01-17 op if (st.mode != (uint32_t)-1) {
1736 b7c2d377 2022-02-13 op mode_t m = st.mode & 0x7FF; /* silently truncate higer bits */
1737 755d75bf 2022-01-17 op if (f->fd != -1)
1738 755d75bf 2022-01-17 op r = fchmod(f->fd, m);
1739 755d75bf 2022-01-17 op else
1740 543fc013 2022-01-17 op r = fchmodat(f->dir->fd, f->fname, m, 0);
1741 755d75bf 2022-01-17 op
1742 755d75bf 2022-01-17 op if (r == -1) {
1743 755d75bf 2022-01-17 op np_errno(hdr->tag);
1744 755d75bf 2022-01-17 op return;
1745 755d75bf 2022-01-17 op }
1746 755d75bf 2022-01-17 op }
1747 755d75bf 2022-01-17 op
1748 755d75bf 2022-01-17 op /* change the atime/mtime of the fid, opened or not */
1749 755d75bf 2022-01-17 op if (st.atime != (uint32_t)-1 || st.mtime != (uint32_t)-1) {
1750 755d75bf 2022-01-17 op times[0].tv_nsec = UTIME_OMIT;
1751 755d75bf 2022-01-17 op times[1].tv_nsec = UTIME_OMIT;
1752 755d75bf 2022-01-17 op
1753 755d75bf 2022-01-17 op if (st.atime != (uint32_t)-1) {
1754 755d75bf 2022-01-17 op times[0].tv_sec = st.atime;
1755 755d75bf 2022-01-17 op times[0].tv_nsec = 0;
1756 755d75bf 2022-01-17 op }
1757 755d75bf 2022-01-17 op if (st.mtime != (uint32_t)-1) {
1758 755d75bf 2022-01-17 op times[1].tv_sec = st.mtime;
1759 755d75bf 2022-01-17 op times[1].tv_nsec = 0;
1760 755d75bf 2022-01-17 op }
1761 755d75bf 2022-01-17 op
1762 755d75bf 2022-01-17 op if (f->fd != -1)
1763 755d75bf 2022-01-17 op r = futimens(f->fd, times);
1764 755d75bf 2022-01-17 op else
1765 543fc013 2022-01-17 op r = utimensat(f->dir->fd, f->fname, times, 0);
1766 755d75bf 2022-01-17 op
1767 755d75bf 2022-01-17 op if (r == -1) {
1768 755d75bf 2022-01-17 op np_errno(hdr->tag);
1769 755d75bf 2022-01-17 op return;
1770 755d75bf 2022-01-17 op }
1771 755d75bf 2022-01-17 op }
1772 755d75bf 2022-01-17 op
1773 755d75bf 2022-01-17 op /* truncate */
1774 755d75bf 2022-01-17 op if (st.length != (uint64_t)-1) {
1775 755d75bf 2022-01-17 op if (f->fd == -1) {
1776 755d75bf 2022-01-17 op np_error(hdr->tag, "can't truncate a non-opened fid");
1777 755d75bf 2022-01-17 op return;
1778 755d75bf 2022-01-17 op }
1779 755d75bf 2022-01-17 op
1780 755d75bf 2022-01-17 op if (f->qid.type & QTDIR && st.length != 0) {
1781 755d75bf 2022-01-17 op np_error(hdr->tag, "can't truncate directories");
1782 755d75bf 2022-01-17 op return;
1783 755d75bf 2022-01-17 op }
1784 755d75bf 2022-01-17 op
1785 755d75bf 2022-01-17 op if (TYPE_OVERFLOW(off_t, st.length)) {
1786 755d75bf 2022-01-17 op log_warnx("truncate request overflows off_t: %"PRIu64,
1787 755d75bf 2022-01-17 op st.length);
1788 755d75bf 2022-01-17 op np_error(hdr->tag, "length overflows");
1789 755d75bf 2022-01-17 op return;
1790 755d75bf 2022-01-17 op }
1791 755d75bf 2022-01-17 op
1792 6d3d71a9 2022-01-19 op if (f->qid.type == 0 &&
1793 755d75bf 2022-01-17 op ftruncate(f->fd, st.length) == -1) {
1794 755d75bf 2022-01-17 op np_errno(hdr->tag);
1795 755d75bf 2022-01-17 op return;
1796 755d75bf 2022-01-17 op }
1797 755d75bf 2022-01-17 op }
1798 755d75bf 2022-01-17 op
1799 755d75bf 2022-01-17 op /* rename (change the name entry) */
1800 755d75bf 2022-01-17 op if (*st.name != '\0') {
1801 755d75bf 2022-01-17 op struct stat sb;
1802 755d75bf 2022-01-17 op const char *newname;
1803 755d75bf 2022-01-17 op
1804 755d75bf 2022-01-17 op /*
1805 755d75bf 2022-01-17 op * XXX: 9P requires that we disallow changing the name
1806 755d75bf 2022-01-17 op * to that of an existing file. We can't do that
1807 755d75bf 2022-01-17 op * atomically (rename is happy about stealing names)
1808 755d75bf 2022-01-17 op * so this is just a best effort.
1809 755d75bf 2022-01-17 op */
1810 755d75bf 2022-01-17 op if (fstatat(f->dir->fd, st.name, &sb, 0) == -1 &&
1811 755d75bf 2022-01-17 op errno != ENOENT) {
1812 755d75bf 2022-01-17 op np_error(hdr->tag, "target file exists");
1813 755d75bf 2022-01-17 op return;
1814 755d75bf 2022-01-17 op }
1815 755d75bf 2022-01-17 op
1816 543fc013 2022-01-17 op r = renameat(f->dir->fd, f->fname, f->dir->fd, st.name);
1817 755d75bf 2022-01-17 op if (r == -1) {
1818 755d75bf 2022-01-17 op np_errno(hdr->tag);
1819 755d75bf 2022-01-17 op return;
1820 755d75bf 2022-01-17 op }
1821 755d75bf 2022-01-17 op
1822 755d75bf 2022-01-17 op /* fix the fid so it points to the new file */
1823 755d75bf 2022-01-17 op
1824 755d75bf 2022-01-17 op if ((newname = strchr(st.name, '/')) != NULL)
1825 755d75bf 2022-01-17 op newname++; /* skip / */
1826 755d75bf 2022-01-17 op else
1827 755d75bf 2022-01-17 op newname = st.name;
1828 543fc013 2022-01-17 op strlcpy(f->fname, newname, sizeof(f->fname));
1829 755d75bf 2022-01-17 op
1830 755d75bf 2022-01-17 op if (strchr(st.name, '/') != NULL) {
1831 755d75bf 2022-01-17 op struct dir *dir;
1832 755d75bf 2022-01-17 op char *dname;
1833 755d75bf 2022-01-17 op int fd;
1834 755d75bf 2022-01-17 op
1835 755d75bf 2022-01-17 op dname = dirname(st.name);
1836 755d75bf 2022-01-17 op fd = openat(f->dir->fd, dname, O_RDONLY|O_DIRECTORY);
1837 755d75bf 2022-01-17 op if (fd == -1) {
1838 755d75bf 2022-01-17 op np_errno(hdr->tag);
1839 755d75bf 2022-01-17 op free_fid(f);
1840 755d75bf 2022-01-17 op return;
1841 755d75bf 2022-01-17 op }
1842 755d75bf 2022-01-17 op
1843 755d75bf 2022-01-17 op if ((dir = new_dir(fd)) == NULL) {
1844 755d75bf 2022-01-17 op np_errno(hdr->tag);
1845 755d75bf 2022-01-17 op free_fid(f);
1846 755d75bf 2022-01-17 op return;
1847 755d75bf 2022-01-17 op }
1848 755d75bf 2022-01-17 op
1849 755d75bf 2022-01-17 op dir_decref(f->dir);
1850 755d75bf 2022-01-17 op f->dir = dir_incref(dir);
1851 755d75bf 2022-01-17 op }
1852 755d75bf 2022-01-17 op }
1853 755d75bf 2022-01-17 op
1854 755d75bf 2022-01-17 op np_wstat(hdr->tag);
1855 755d75bf 2022-01-17 op return;
1856 755d75bf 2022-01-17 op
1857 755d75bf 2022-01-17 op err:
1858 755d75bf 2022-01-17 op client_send_listener(IMSG_CLOSE, NULL, 0);
1859 755d75bf 2022-01-17 op client_shutdown();
1860 fb1a36c0 2022-01-09 op }
1861 fb1a36c0 2022-01-09 op
1862 fb1a36c0 2022-01-09 op static void
1863 fb1a36c0 2022-01-09 op tremove(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1864 fb1a36c0 2022-01-09 op {
1865 fb1a36c0 2022-01-09 op struct fid *f;
1866 fb1a36c0 2022-01-09 op uint32_t fid;
1867 fb1a36c0 2022-01-09 op int r;
1868 fb1a36c0 2022-01-09 op char dirpath[PATH_MAX + 3];
1869 fb1a36c0 2022-01-09 op
1870 fb1a36c0 2022-01-09 op /* fid[4] */
1871 bcdb46a7 2022-01-19 op if (!NPREAD32("fid", &fid, &data, &len) ||
1872 bcdb46a7 2022-01-19 op len != 0) {
1873 fb1a36c0 2022-01-09 op client_send_listener(IMSG_CLOSE, NULL, 0);
1874 fb1a36c0 2022-01-09 op client_shutdown();
1875 fb1a36c0 2022-01-09 op return;
1876 fb1a36c0 2022-01-09 op }
1877 fb1a36c0 2022-01-09 op
1878 fb1a36c0 2022-01-09 op if ((f = fid_by_id(fid)) == NULL) {
1879 fb1a36c0 2022-01-09 op np_error(hdr->tag, "invalid fid");
1880 fb1a36c0 2022-01-09 op return;
1881 fb1a36c0 2022-01-09 op }
1882 fb1a36c0 2022-01-09 op
1883 fb1a36c0 2022-01-09 op if (f->qid.type & QTDIR) { /* directory */
1884 fb1a36c0 2022-01-09 op strlcpy(dirpath, "../", sizeof(dirpath));
1885 543fc013 2022-01-17 op strlcat(dirpath, f->fname, sizeof(dirpath));
1886 fb1a36c0 2022-01-09 op r = unlinkat(f->dir->fd, dirpath, AT_REMOVEDIR);
1887 fb1a36c0 2022-01-09 op } else /* file */
1888 543fc013 2022-01-17 op r = unlinkat(f->dir->fd, f->fname, 0);
1889 fb1a36c0 2022-01-09 op
1890 fb1a36c0 2022-01-09 op if (r == -1)
1891 fb1a36c0 2022-01-09 op np_errno(hdr->tag);
1892 fb1a36c0 2022-01-09 op else
1893 fb1a36c0 2022-01-09 op np_remove(hdr->tag);
1894 fb1a36c0 2022-01-09 op
1895 fb1a36c0 2022-01-09 op free_fid(f);
1896 fb1a36c0 2022-01-09 op }
1897 fb1a36c0 2022-01-09 op
1898 fb1a36c0 2022-01-09 op static void
1899 8b0a5062 2022-05-22 op handle_message(struct imsg *imsg, size_t len, int cont)
1900 fb1a36c0 2022-01-09 op {
1901 8b0a5062 2022-05-22 op static struct fid *writefid;
1902 8b0a5062 2022-05-22 op static off_t writepos;
1903 8b0a5062 2022-05-22 op static size_t writeleft;
1904 8b0a5062 2022-05-22 op static int writeskip;
1905 8b0a5062 2022-05-22 op static uint16_t writetag;
1906 fb1a36c0 2022-01-09 op struct msg {
1907 fb1a36c0 2022-01-09 op uint8_t type;
1908 fb1a36c0 2022-01-09 op void (*fn)(struct np_msg_header *, const uint8_t *, size_t);
1909 fb1a36c0 2022-01-09 op } msgs[] = {
1910 fb1a36c0 2022-01-09 op {Tversion, tversion},
1911 fb1a36c0 2022-01-09 op {Tattach, tattach},
1912 fb1a36c0 2022-01-09 op {Tclunk, tclunk},
1913 fb1a36c0 2022-01-09 op {Tflush, tflush},
1914 fb1a36c0 2022-01-09 op {Twalk, twalk},
1915 fb1a36c0 2022-01-09 op {Topen, topen},
1916 fb1a36c0 2022-01-09 op {Tcreate, tcreate},
1917 fb1a36c0 2022-01-09 op {Tread, tread},
1918 8b0a5062 2022-05-22 op /* {Twrite, twrite}, */
1919 fb1a36c0 2022-01-09 op {Tstat, tstat},
1920 755d75bf 2022-01-17 op {Twstat, twstat},
1921 fb1a36c0 2022-01-09 op {Tremove, tremove},
1922 fb1a36c0 2022-01-09 op };
1923 fb1a36c0 2022-01-09 op struct np_msg_header hdr;
1924 fb1a36c0 2022-01-09 op size_t i;
1925 fb1a36c0 2022-01-09 op uint8_t *data;
1926 fb1a36c0 2022-01-09 op
1927 fb1a36c0 2022-01-09 op #if DEBUG_PACKETS
1928 fb1a36c0 2022-01-09 op hexdump("incoming packet", imsg->data, len);
1929 fb1a36c0 2022-01-09 op #endif
1930 fb1a36c0 2022-01-09 op
1931 8b0a5062 2022-05-22 op /*
1932 8b0a5062 2022-05-22 op * Twrite is special and can be "continued" to allow writing
1933 8b0a5062 2022-05-22 op * more than what the imsg framework would allow us to.
1934 8b0a5062 2022-05-22 op */
1935 8b0a5062 2022-05-22 op if (writeleft > 0 && !cont) {
1936 8b0a5062 2022-05-22 op log_warnx("received a non continuation message when still "
1937 8b0a5062 2022-05-22 op "missed %zu bytes to write", writeleft);
1938 8b0a5062 2022-05-22 op client_send_listener(IMSG_CLOSE, NULL, 0);
1939 8b0a5062 2022-05-22 op client_shutdown();
1940 8b0a5062 2022-05-22 op return;
1941 8b0a5062 2022-05-22 op }
1942 8b0a5062 2022-05-22 op
1943 8b0a5062 2022-05-22 op if (cont) {
1944 8b0a5062 2022-05-22 op if (writeskip)
1945 8b0a5062 2022-05-22 op return;
1946 8b0a5062 2022-05-22 op
1947 8b0a5062 2022-05-22 op if (writefid == NULL) {
1948 8b0a5062 2022-05-22 op log_warnx("received a continuation message without "
1949 8b0a5062 2022-05-22 op "seeing a Twrite");
1950 8b0a5062 2022-05-22 op client_send_listener(IMSG_CLOSE, NULL, 0);
1951 8b0a5062 2022-05-22 op client_shutdown();
1952 8b0a5062 2022-05-22 op return;
1953 8b0a5062 2022-05-22 op }
1954 8b0a5062 2022-05-22 op
1955 8b0a5062 2022-05-22 op log_warnx("continuing...");
1956 8b0a5062 2022-05-22 op twrite_cont(writefid, &writepos, &writeleft, &writeskip,
1957 8b0a5062 2022-05-22 op writetag, imsg->data, len);
1958 8b0a5062 2022-05-22 op return;
1959 8b0a5062 2022-05-22 op }
1960 8b0a5062 2022-05-22 op
1961 8b0a5062 2022-05-22 op writefid = NULL;
1962 8b0a5062 2022-05-22 op writepos = -1;
1963 8b0a5062 2022-05-22 op writeleft = 0;
1964 8b0a5062 2022-05-22 op writeskip = 0;
1965 8b0a5062 2022-05-22 op
1966 fb1a36c0 2022-01-09 op parse_message(imsg->data, len, &hdr, &data);
1967 fb1a36c0 2022-01-09 op len -= HEADERSIZE;
1968 fb1a36c0 2022-01-09 op
1969 fb1a36c0 2022-01-09 op log_debug("got request: len=%d type=%d[%s] tag=%d",
1970 fb1a36c0 2022-01-09 op hdr.len, hdr.type, pp_msg_type(hdr.type), hdr.tag);
1971 fb1a36c0 2022-01-09 op
1972 fb1a36c0 2022-01-09 op if (!handshaked && hdr.type != Tversion) {
1973 fb1a36c0 2022-01-09 op client_send_listener(IMSG_CLOSE, NULL, 0);
1974 fb1a36c0 2022-01-09 op client_shutdown();
1975 fb1a36c0 2022-01-09 op return;
1976 fb1a36c0 2022-01-09 op }
1977 fb1a36c0 2022-01-09 op
1978 8b0a5062 2022-05-22 op if (hdr.type == Twrite) {
1979 8b0a5062 2022-05-22 op writetag = hdr.tag;
1980 8b0a5062 2022-05-22 op twrite(&hdr, data, len, &writefid, &writepos, &writeleft,
1981 8b0a5062 2022-05-22 op &writeskip);
1982 fbeaf87b 2022-05-22 op return;
1983 fbeaf87b 2022-05-22 op }
1984 fbeaf87b 2022-05-22 op
1985 fbeaf87b 2022-05-22 op if (len > IMSG_MAXSIZE) {
1986 fbeaf87b 2022-05-22 op log_warnx("can't handle message: too long for its type");
1987 fbeaf87b 2022-05-22 op client_send_listener(IMSG_CLOSE, NULL, 0);
1988 fbeaf87b 2022-05-22 op client_shutdown();
1989 8b0a5062 2022-05-22 op return;
1990 8b0a5062 2022-05-22 op }
1991 8b0a5062 2022-05-22 op
1992 fb1a36c0 2022-01-09 op for (i = 0; i < sizeof(msgs)/sizeof(msgs[0]); ++i) {
1993 fb1a36c0 2022-01-09 op if (msgs[i].type != hdr.type)
1994 fb1a36c0 2022-01-09 op continue;
1995 fb1a36c0 2022-01-09 op
1996 fb1a36c0 2022-01-09 op msgs[i].fn(&hdr, data, len);
1997 fb1a36c0 2022-01-09 op return;
1998 fb1a36c0 2022-01-09 op }
1999 fb1a36c0 2022-01-09 op
2000 fb1a36c0 2022-01-09 op np_error(hdr.tag, "Not supported.");
2001 fb1a36c0 2022-01-09 op }