2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
39 * XXX: atm is difficult to accept messages bigger than MAX_IMSGSIZE
40 * minus IMSG_HEADER_SIZE, we need something to split messages into
41 * chunks and receive them one by the other.
43 * CLIENT_MSIZE is thus the maximum message size we can handle now.
45 #define CLIENT_MSIZE (MAX_IMSGSIZE - IMSG_HEADER_SIZE)
47 #define DEBUG_PACKETS 0
49 /* straight outta /src/usr.bin/ssh/scp.c */
50 #define TYPE_OVERFLOW(type, val) \
51 ((sizeof(type) == 4 && (val) > INT32_MAX) || \
52 (sizeof(type) == 8 && (val) > INT64_MAX) || \
53 (sizeof(type) != 4 && sizeof(type) != 8))
61 STAILQ_HEAD(dirhead, dir) dirs;
65 STAILQ_ENTRY(dir) entries;
68 STAILQ_HEAD(fidhead, fid) fids;
75 * the flags passed to open(2). O_CLOEXEC means ORCLOSE, that
76 * is to unlink the file upon Tclunk.
81 * if fd is not -1 this fid was opened, fd represents its
82 * file descriptor and iomode the flags passed to open(2).
89 * expected offset for Tread against a directory.
95 STAILQ_ENTRY(fid) entries;
98 static struct imsgev *iev_listener;
99 static struct evbuffer *evb;
100 static uint32_t peerid;
102 static int handshaked;
105 static __dead void client_shutdown(void);
106 static void client_sig_handler(int, short, void *);
107 static void client_dispatch_listener(int, short, void *);
108 static void client_privdrop(const char *, const char *);
110 static int client_send_listener(int, const void *, uint16_t);
112 static void qid_update_from_sb(struct qid *, struct stat *);
114 static struct dir *new_dir(int);
115 static struct dir *dir_incref(struct dir *);
116 static void dir_decref(struct dir *);
118 static struct fid *new_fid(struct dir *, uint32_t, const char *, struct qid *);
119 static struct fid *fid_by_id(uint32_t);
120 static void free_fid(struct fid *);
122 static void parse_message(const uint8_t *, size_t,
123 struct np_msg_header *, uint8_t **);
125 static void np_write16(struct evbuffer *, uint16_t);
126 static void np_write32(struct evbuffer *, uint32_t);
127 static void np_write64(struct evbuffer *, uint64_t);
128 static void np_header(uint32_t, uint8_t, uint16_t);
129 static void np_string(struct evbuffer *, uint16_t, const char *);
130 static void np_qid(struct evbuffer *, struct qid *);
131 static void do_send(void);
133 static void np_version(uint16_t, uint32_t, const char *);
134 static void np_attach(uint16_t, struct qid *);
135 static void np_clunk(uint16_t);
136 static void np_flush(uint16_t);
137 static void np_walk(uint16_t, int, struct qid *);
138 static void np_open(uint16_t, struct qid *, uint32_t);
139 static void np_create(uint16_t, struct qid *, uint32_t);
140 static void np_read(uint16_t, uint32_t, void *);
141 static void np_write(uint16_t, uint32_t);
142 static void np_stat(uint16_t, uint32_t, void *);
143 static void np_remove(uint16_t);
144 static void np_error(uint16_t, const char *);
145 static void np_errno(uint16_t);
147 static int np_read8(const char *, const char *, uint8_t *,
148 const uint8_t **, size_t *);
149 static int np_read16(const char *, const char *, uint16_t *,
150 const uint8_t **, size_t *);
151 static int np_read32(const char *, const char *, uint32_t *,
152 const uint8_t **, size_t *);
153 static int np_read64(const char *, const char *, uint64_t *,
154 const uint8_t **, size_t *);
156 #define READSTRERR -1
157 #define READSTRTRUNC -2
158 static int np_readstr(const char *, const char *, char *, size_t,
159 const uint8_t **, size_t *);
161 #define NPREAD8(f, dst, src, len) np_read8(__func__, f, dst, src, len)
162 #define NPREAD16(f, dst, src, len) np_read16(__func__, f, dst, src, len)
163 #define NPREAD32(f, dst, src, len) np_read32(__func__, f, dst, src, len)
164 #define NPREAD64(f, dst, src, len) np_read64(__func__, f, dst, src, len)
166 #define NPREADSTR(f, b, bl, src, len) np_readstr(__func__, f, b, bl, src, len)
168 static void tversion(struct np_msg_header *, const uint8_t *, size_t);
169 static void tattach(struct np_msg_header *, const uint8_t *, size_t);
170 static void tclunk(struct np_msg_header *, const uint8_t *, size_t);
171 static void tflush(struct np_msg_header *, const uint8_t *, size_t);
172 static void twalk(struct np_msg_header *, const uint8_t *, size_t);
173 static void topen(struct np_msg_header *, const uint8_t *, size_t);
174 static void tcreate(struct np_msg_header *, const uint8_t *, size_t);
175 static void tread(struct np_msg_header *, const uint8_t *, size_t);
176 static void twrite(struct np_msg_header *, const uint8_t *, size_t);
177 static void tstat(struct np_msg_header *, const uint8_t *, size_t);
178 static void tremove(struct np_msg_header *, const uint8_t *, size_t);
179 static void handle_message(struct imsg *, size_t);
182 client(int debug, int verbose)
184 struct event ev_sigint, ev_sigterm;
186 log_init(debug, LOG_DAEMON);
187 log_setverbose(verbose);
189 setproctitle("client");
190 log_procinit("client");
192 log_debug("warming up");
196 /* Setup signal handlers */
197 signal_set(&ev_sigint, SIGINT, client_sig_handler, NULL);
198 signal_set(&ev_sigterm, SIGTERM, client_sig_handler, NULL);
200 signal_add(&ev_sigint, NULL);
201 signal_add(&ev_sigterm, NULL);
203 signal(SIGPIPE, SIG_IGN);
204 signal(SIGHUP, SIG_IGN);
206 /* Setup pipe and event handler to the listener process */
207 if ((iev_listener = malloc(sizeof(*iev_listener))) == NULL)
210 imsg_init(&iev_listener->ibuf, 3);
211 iev_listener->handler = client_dispatch_listener;
213 /* Setup event handlers. */
214 iev_listener->events = EV_READ;
215 event_set(&iev_listener->ev, iev_listener->ibuf.fd,
216 iev_listener->events, iev_listener->handler, iev_listener);
217 event_add(&iev_listener->ev, NULL);
224 client_shutdown(void)
229 msgbuf_clear(&iev_listener->ibuf.w);
230 close(iev_listener->ibuf.fd);
234 log_debug("client exiting");
239 client_sig_handler(int sig, short event, void *d)
242 * Normal signal handler rules don't apply because libevent
251 fatalx("unexpected signal %d", sig);
260 client_dispatch_listener(int fd, short event, void *d)
262 static int auth = AUTH_NONE;
263 static char username[64] = {0};
264 static char dir[PATH_MAX] = {0};
266 struct imsgev *iev = d;
267 struct imsgbuf *ibuf;
273 if (event & EV_READ) {
274 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
275 fatal("imsg_read error");
276 if (n == 0) /* Connection closed */
279 if (event & EV_WRITE) {
280 if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
281 fatal("msgbuf_write");
282 if (n == 0) /* Connection closed */
287 if ((n = imsg_get(ibuf, &imsg)) == -1)
288 fatal("%s: imsg_get error", __func__);
289 if (n == 0) /* No more messages. */
292 switch (imsg.hdr.type) {
294 peerid = imsg.hdr.peerid;
296 fatalx("%s: IMSG_AUTH already done", __func__);
298 ((char *)imsg.data)[IMSG_DATA_SIZE(imsg)-1] = '\0';
299 strlcpy(username, imsg.data, sizeof(username));
302 if (auth != AUTH_USER)
303 fatalx("%s: IMSG_AUTH_DIR not after IMSG_AUTH",
306 ((char *)imsg.data)[IMSG_DATA_SIZE(imsg)-1] = '\0';
307 strlcpy(dir, imsg.data, sizeof(dir));
308 client_privdrop(username, dir);
309 memset(username, 0, sizeof(username));
310 memset(dir, 0, sizeof(username));
315 fatalx("%s: can't handle messages before"
316 " doing the auth", __func__);
317 handle_message(&imsg, IMSG_DATA_SIZE(imsg));
320 log_debug("closing");
324 log_debug("%s: unexpected imsg %d",
325 __func__, imsg.hdr.type);
334 /* This pipe is dead. Remove its event handler. */
336 log_debug("pipe closed, shutting down...");
337 event_loopexit(NULL);
342 client_privdrop(const char *username, const char *dir)
346 setproctitle("client %s", username);
348 if ((pw = getpwnam(username)) == NULL)
349 fatalx("getpwnam(%s) failed", username);
351 if (chroot(dir) == -1)
353 if (chdir("/") == -1)
354 fatal("chdir(\"/\")");
356 if (setgroups(1, &pw->pw_gid) ||
357 setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
358 setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
359 fatal("can't drop privileges");
362 log_debug("client ready; user=%s dir=%s", username, dir);
364 if ((evb = evbuffer_new()) == NULL)
365 fatal("evbuffer_new");
369 client_send_listener(int type, const void *data, uint16_t len)
373 if ((ret = imsg_compose(&iev_listener->ibuf, type, peerid, 0, -1,
375 imsg_event_add(iev_listener);
380 /* set qid fields from sb */
382 qid_update_from_sb(struct qid *qid, struct stat *sb)
384 qid->path = sb->st_ino;
387 * Theoretically (and hopefully!) this should be a 64 bit
388 * number. Unfortunately, 9P uses 32 bit timestamps.
390 qid->vers = sb->st_mtim.tv_sec;
392 if (S_ISREG(sb->st_mode))
394 else if (S_ISDIR(sb->st_mode))
396 else if (S_ISLNK(sb->st_mode))
397 qid->type = QTSYMLINK;
400 /* creates a qid given a fd */
406 if ((dir = calloc(1, sizeof(*dir))) == NULL)
410 STAILQ_INSERT_HEAD(&dirs, dir, entries);
415 dir_incref(struct dir *dir)
422 dir_decref(struct dir *dir)
424 if (--dir->refcount > 0)
427 STAILQ_REMOVE(&dirs, dir, dir, entries);
434 new_fid(struct dir *dir, uint32_t fid, const char *path, struct qid *qid)
441 if (fstatat(dir->fd, path, &sb, 0)) {
442 log_warn("fstatat(%s)", path);
445 qid_update_from_sb(&q, &sb);
449 if ((f = calloc(1, sizeof(*f))) == NULL)
452 f->dir = dir_incref(dir);
456 strlcpy(f->fpath, path, sizeof(f->fpath));
458 memcpy(&f->qid, qid, sizeof(f->qid));
460 STAILQ_INSERT_HEAD(&fids, f, entries);
466 fid_by_id(uint32_t fid)
470 STAILQ_FOREACH(f, &fids, entries) {
479 free_fid(struct fid *f)
490 fatal("can't close fid %d", f->fid);
493 evbuffer_free(f->evb);
495 /* try to honour ORCLOSE if requested */
496 if (f->iomode & O_CLOEXEC)
497 unlinkat(f->dir->fd, f->fpath, 0);
502 STAILQ_REMOVE(&fids, f, fid, entries);
507 parse_message(const uint8_t *data, size_t len, struct np_msg_header *hdr,
512 if (!NPREAD32("len", &hdr->len, &data, &len) ||
513 !NPREAD8("type", &hdr->type, &data, &len) ||
514 !NPREAD16("tag", &hdr->tag, &data, &len))
517 if (olen != hdr->len)
520 if (hdr->type < Tversion ||
522 hdr->type == Terror ||
523 (hdr->type & 0x1) != 0) /* cannot recv a R* */
526 hdr->tag = le32toh(hdr->tag);
528 *cnt = (uint8_t *)data;
532 /* TODO: send a proper message to terminate the connection. */
533 fatalx("got invalid message");
537 np_write16(struct evbuffer *e, uint16_t x)
540 evbuffer_add(e, &x, sizeof(x));
544 np_write32(struct evbuffer *e, uint32_t x)
547 evbuffer_add(e, &x, sizeof(x));
551 np_write64(struct evbuffer *e, uint64_t x)
554 evbuffer_add(e, &x, sizeof(x));
558 np_writebuf(struct evbuffer *e, size_t len, void *data)
560 evbuffer_add(e, data, len);
564 np_header(uint32_t len, uint8_t type, uint16_t tag)
571 evbuffer_add(evb, &len, sizeof(len));
572 evbuffer_add(evb, &type, sizeof(type));
573 evbuffer_add(evb, &tag, sizeof(tag));
577 np_string(struct evbuffer *e, uint16_t len, const char *str)
582 evbuffer_add(e, &len, sizeof(len));
583 evbuffer_add(e, str, l);
587 np_qid(struct evbuffer *e, struct qid *qid)
592 path = htole64(qid->path);
593 vers = htole32(qid->vers);
595 evbuffer_add(e, &qid->type, sizeof(qid->type));
596 evbuffer_add(e, &vers, sizeof(vers));
597 evbuffer_add(e, &path, sizeof(path));
606 len = EVBUFFER_LENGTH(evb);
607 data = EVBUFFER_DATA(evb);
610 hexdump("outgoing packet", data, len);
612 client_send_listener(IMSG_BUF, data, len);
613 evbuffer_drain(evb, len);
617 np_version(uint16_t tag, uint32_t msize, const char *version)
623 msize = htole32(msize);
625 np_header(sizeof(msize) + sizeof(l) + l, Rversion, tag);
626 evbuffer_add(evb, &msize, sizeof(msize));
627 np_string(evb, l, version);
632 np_attach(uint16_t tag, struct qid *qid)
634 np_header(QIDSIZE, Rattach, tag);
640 np_clunk(uint16_t tag)
642 np_header(0, Rclunk, tag);
647 np_flush(uint16_t tag)
649 np_header(0, Rflush, tag);
654 np_walk(uint16_t tag, int nwqid, struct qid *wqid)
658 /* two bytes for the counter */
659 np_header(2 + QIDSIZE * nwqid, Rwalk, tag);
660 np_write16(evb, nwqid);
661 for (i = 0; i < nwqid; ++i)
662 np_qid(evb, wqid + i);
668 np_open(uint16_t tag, struct qid *qid, uint32_t iounit)
670 np_header(QIDSIZE + sizeof(iounit), Ropen, tag);
672 np_write32(evb, iounit);
677 np_create(uint16_t tag, struct qid *qid, uint32_t iounit)
679 np_header(QIDSIZE + sizeof(iounit), Rcreate, tag);
681 np_write32(evb, iounit);
686 np_read(uint16_t tag, uint32_t count, void *data)
688 np_header(sizeof(count) + count, Rread, tag);
689 np_write32(evb, count);
690 np_writebuf(evb, count, data);
695 np_write(uint16_t tag, uint32_t count)
697 np_header(sizeof(count), Rwrite, tag);
698 np_write32(evb, count);
703 np_stat(uint16_t tag, uint32_t count, void *data)
705 np_header(count, Rstat, tag);
706 np_writebuf(evb, count, data);
711 np_remove(uint16_t tag)
713 np_header(0, Rremove, tag);
718 np_error(uint16_t tag, const char *errstr)
724 np_header(sizeof(l) + l, Rerror, tag);
725 np_string(evb, l, errstr);
730 np_errno(uint16_t tag)
737 strerror_r(errno, buf, sizeof(buf));
744 np_read8(const char *t, const char *f, uint8_t *dst, const uint8_t **src,
747 if (*len < sizeof(*dst)) {
748 log_warnx("%s: wanted %zu bytes for the %s field but only "
749 "%zu are available.", t, sizeof(*dst), f, *len);
753 memcpy(dst, *src, sizeof(*dst));
754 *src += sizeof(*dst);
755 *len -= sizeof(*dst);
761 np_read16(const char *t, const char *f, uint16_t *dst, const uint8_t **src,
764 if (*len < sizeof(*dst)) {
765 log_warnx("%s: wanted %zu bytes for the %s field but only "
766 "%zu are available.", t, sizeof(*dst), f, *len);
770 memcpy(dst, *src, sizeof(*dst));
771 *src += sizeof(*dst);
772 *len -= sizeof(*dst);
773 *dst = le16toh(*dst);
779 np_read32(const char *t, const char *f, uint32_t *dst, const uint8_t **src,
782 if (*len < sizeof(*dst)) {
783 log_warnx("%s: wanted %zu bytes for the %s field but only "
784 "%zu are available.", t, sizeof(*dst), f, *len);
788 memcpy(dst, *src, sizeof(*dst));
789 *src += sizeof(*dst);
790 *len -= sizeof(*dst);
791 *dst = le32toh(*dst);
797 np_read64(const char *t, const char *f, uint64_t *dst, const uint8_t **src,
800 if (*len < sizeof(*dst)) {
801 log_warnx("%s: wanted %zu bytes for the %s field but only "
802 "%zu are available.", t, sizeof(*dst), f, *len);
806 memcpy(dst, *src, sizeof(*dst));
807 *src += sizeof(*dst);
808 *len -= sizeof(*dst);
809 *dst = le64toh(*dst);
815 np_readstr(const char *t, const char *f, char *res, size_t reslen,
816 const uint8_t **src, size_t *len)
821 strlcpy(buf, f, sizeof(buf));
822 strlcat(buf, "-len", sizeof(buf));
824 if (!np_read16(t, buf, &sl, src, len))
828 log_warnx("%s: wanted %d bytes for the %s field but only "
829 "%zu are available.", t, sl, f, *len);
836 memcpy(res, *src, sl);
845 tversion(struct np_msg_header *hdr, const uint8_t *data, size_t len)
847 char *dot, version[32];
852 /* msize[4] version[s] */
853 if (!NPREAD32("msize", &msize, &data, &len))
856 switch (NPREADSTR("version", version, sizeof(version), &data, &len)) {
860 log_warnx("9P version string too long, truncated");
864 if ((dot = strchr(version, '.')) != NULL)
867 if (strcmp(version, VERSION9P) != 0 ||
871 /* version matched */
873 msize = MIN(msize, CLIENT_MSIZE);
874 client_send_listener(IMSG_MSIZE, &msize, sizeof(msize));
875 np_version(hdr->tag, msize, VERSION9P);
879 log_warnx("unknown 9P version string: \"%s\", want "VERSION9P,
881 np_version(hdr->tag, MSIZE9P, "unknown");
885 client_send_listener(IMSG_CLOSE, NULL, 0);
890 tattach(struct np_msg_header *hdr, const uint8_t *data, size_t len)
896 char aname[PATH_MAX];
898 /* fid[4] afid[4] uname[s] aname[s] */
900 if (!NPREAD32("fid", &fid, &data, &len) ||
901 !NPREAD32("afid", &afid, &data, &len))
904 /* read the uname but don't actually use it */
905 switch (NPREADSTR("uname", aname, sizeof(aname), &data, &len)) {
909 np_error(hdr->tag, "name too long");
913 switch (NPREADSTR("aname", aname, sizeof(aname), &data, &len)) {
917 np_error(hdr->tag, "name too long");
921 if (fid_by_id(fid) != NULL || afid != NOFID) {
922 np_error(hdr->tag, "invalid fid or afid");
926 if ((fd = open(aname, O_RDONLY|O_DIRECTORY)) == -1)
929 if ((dir = new_dir(fd)) == NULL)
932 log_debug("attached %s to %d", aname, fid);
934 if ((f = new_fid(dir, fid, aname, NULL)) == NULL) {
939 np_attach(hdr->tag, &f->qid);
944 log_warn("failed to attach %s", aname);
948 client_send_listener(IMSG_CLOSE, NULL, 0);
953 tclunk(struct np_msg_header *hdr, const uint8_t *data, size_t len)
959 if (!NPREAD32("fid", &fid, &data, &len)) {
960 client_send_listener(IMSG_CLOSE, NULL, 0);
965 if ((f = fid_by_id(fid)) == NULL) {
966 np_error(hdr->tag, "invalid fid");
975 tflush(struct np_msg_header *hdr, const uint8_t *data, size_t len)
980 * We're doing only synchronous I/O. Tflush is implemented
981 * only because it's illegal to reply with a Rerror.
985 if (len != sizeof(oldtag)) {
986 log_warnx("Tflush with the wrong size: got %zu want %zu",
987 len, sizeof(oldtag));
988 client_send_listener(IMSG_CLOSE, NULL, 0);
997 twalk(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1001 struct qid wqid[MAXWELEM] = {0};
1003 uint32_t fid, newfid;
1005 int fd, oldfd, no, nwqid = 0;
1006 char wnam[PATH_MAX];
1008 if (!NPREAD32("fid", &fid, &data, &len) ||
1009 !NPREAD32("newfid", &newfid, &data, &len) ||
1010 !NPREAD16("nwname", &nwname, &data, &len))
1013 if (nwname > MAXWELEM) {
1014 log_warnx("Twalk: more than %d path elements: %d",
1019 if ((f = fid_by_id(fid)) == NULL) {
1020 np_error(hdr->tag, "invalid fid");
1025 np_error(hdr->tag, "fid already opened for I/O");
1031 else if ((nf = fid_by_id(newfid)) != NULL) {
1032 np_error(hdr->tag, "newfid already in use");
1037 /* special case: fid duplication */
1040 * TODO: should we forbid fids duplication when fid ==
1044 (nf = new_fid(f->dir, newfid, f->fpath, &f->qid)) == NULL)
1045 fatal("new_fid duplication");
1047 np_walk(hdr->tag, 0, NULL);
1051 if (!(f->qid.type & QTDIR)) {
1052 np_error(hdr->tag, "fid doesn't represent a directory");
1058 for (nwqid = 0; nwqid < nwname; nwqid++) {
1059 switch (NPREADSTR("wname", wnam, sizeof(wnam), &data, &len)) {
1063 np_error(hdr->tag, "wname too long");
1067 if (*wnam == '\0' ||
1068 strchr(wnam, '/') != NULL ||
1069 !strcmp(wnam, ".")) {
1074 if ((fd = openat(oldfd, wnam, O_RDONLY|O_DIRECTORY)) == -1 &&
1078 if ((fd == -1 && fstatat(oldfd, wnam, &sb, 0) == -1) ||
1079 (fd != -1 && fstat(fd, &sb) == -1))
1082 qid_update_from_sb(&wqid[nwqid], &sb);
1084 /* reached a file but we still have other components */
1085 if (fd == -1 && nwqid+1 < nwname)
1088 /* reached the end and found a file */
1089 if (fd == -1 && nwqid+1 == nwname)
1092 if (oldfd != f->dir->fd)
1098 * If fd is -1 we've reached a file, otherwise we've just
1099 * reached another directory. We must pay attention to what
1100 * file descriptor we use to create the dir, because if we've
1101 * reached a file and oldfd is f->dir->fd then we *must* share
1102 * the same dir (it was a walk of one path from a directory to a
1103 * file, otherwise fun is bound to happen as soon as the client
1104 * closes the fid for the directory but keeps the one for the
1107 if (fd == -1 && oldfd == f->dir->fd)
1110 dir = new_dir(oldfd);
1118 if ((nf = new_fid(dir, newfid, wnam, &wqid[nwqid-1])) == NULL)
1121 /* update the dir */
1122 dir_decref(nf->dir);
1123 nf->dir = dir_incref(dir);
1126 np_walk(hdr->tag, nwqid, wqid);
1130 if (oldfd != f->dir->fd)
1134 np_error(hdr->tag, strerror(no));
1136 np_walk(hdr->tag, nwqid, wqid);
1140 client_send_listener(IMSG_CLOSE, NULL, 0);
1145 npmode_to_unix(uint8_t mode, int *flags)
1147 switch (mode & 0x0F) {
1158 log_warnx("tried to open something with KOEXEC");
1166 if (mode & KORCLOSE)
1167 *flags |= O_CLOEXEC;
1173 topen(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1182 /* fid[4] mode[1] */
1183 if (!NPREAD32("fid", &fid, &data, &len) ||
1184 !NPREAD8("mode", &mode, &data, &len)) {
1185 client_send_listener(IMSG_CLOSE, NULL, 0);
1190 if ((f = fid_by_id(fid)) == NULL || f->fd != -1) {
1191 np_error(hdr->tag, "invalid fid");
1195 if (npmode_to_unix(mode, &f->iomode) == -1) {
1196 np_error(hdr->tag, "invalid mode");
1201 if (f->qid.type & QTDIR)
1204 if ((f->fd = openat(f->dir->fd, path, f->iomode)) == -1) {
1205 np_error(hdr->tag, strerror(errno));
1209 if (fstat(f->fd, &sb) == -1)
1212 if (S_ISDIR(sb.st_mode)) {
1213 if ((f->d = fdopendir(f->fd)) == NULL) {
1220 if ((f->evb = evbuffer_new()) == NULL) {
1230 qid_update_from_sb(&qid, &sb);
1231 np_open(hdr->tag, &qid, sb.st_blksize);
1235 tcreate(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1242 char name[PATH_MAX];
1244 /* fid[4] name[s] perm[4] mode[1] */
1245 if (!NPREAD32("fid", &fid, &data, &len))
1247 switch (NPREADSTR("name", name, sizeof(name), &data, &len)) {
1251 np_error(hdr->tag, "name too long");
1254 if (!NPREAD32("perm", &perm, &data, &len) ||
1255 !NPREAD8("mode", &mode, &data, &len))
1258 if (!strcmp(name, ".") || !strcmp(name, "..") ||
1259 strchr(name, '/') != NULL) {
1260 np_error(hdr->tag, "invalid name");
1264 if ((f = fid_by_id(fid)) == NULL || f->fd != -1) {
1265 np_error(hdr->tag, "invalid fid");
1269 if (!(f->qid.type & QTDIR)) {
1270 np_error(hdr->tag, "fid doesn't identify a directory");
1274 if (npmode_to_unix(mode, &f->iomode) == -1) {
1275 np_error(hdr->tag, "invalid mode");
1279 if (f->iomode & O_RDONLY) {
1280 np_error(hdr->tag, "can't create a read-only file");
1284 /* TODO: parse the mode */
1286 if (perm & 0x80000000) {
1287 /* create a directory */
1288 f->fd = mkdirat(f->dir->fd, name, 0755);
1291 f->fd = openat(f->dir->fd, name, f->iomode | O_CREAT | O_TRUNC,
1300 if (fstat(f->fd, &sb) == -1)
1303 if (S_ISDIR(sb.st_mode)) {
1304 if ((f->d = fdopendir(f->fd)) == NULL) {
1311 if ((f->evb = evbuffer_new()) == NULL) {
1321 qid_update_from_sb(&qid, &sb);
1322 np_create(hdr->tag, &qid, sb.st_blksize);
1327 client_send_listener(IMSG_CLOSE, NULL, 0);
1332 serialize_stat(const char *fname, struct stat *sb, struct evbuffer *evb)
1335 const char *uid, *gid, *muid;
1337 uint16_t namlen, uidlen, gidlen, ulen;
1339 qid_update_from_sb(&qid, sb);
1341 /* TODO: fill these fields */
1346 namlen = strlen(fname);
1347 uidlen = strlen(uid);
1348 gidlen = strlen(gid);
1349 ulen = strlen(muid);
1351 tot = NPSTATSIZ(namlen, uidlen, gidlen, ulen);
1352 if (tot > UINT32_MAX) {
1353 log_warnx("stat info for dir entry %s would overflow",
1358 np_write16(evb, tot); /* size[2] */
1359 np_write16(evb, sb->st_rdev); /* type[2] */
1360 np_write32(evb, sb->st_dev); /* dev[4] */
1361 np_qid(evb, &qid); /* qid[13] */
1363 /* XXX: translate? */
1364 np_write32(evb, sb->st_mode); /* mode[4] */
1366 np_write32(evb, sb->st_atim.tv_sec); /* atime[4] */
1367 np_write32(evb, sb->st_mtim.tv_sec); /* mtime[4] */
1368 np_write64(evb, sb->st_size); /* length[8] */
1369 np_string(evb, namlen, fname); /* name[s] */
1370 np_string(evb, uidlen, uid); /* uid[s] */
1371 np_string(evb, gidlen, gid); /* gid[s] */
1372 np_string(evb, ulen, muid); /* muid[s] */
1376 tread(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1381 uint32_t fid, count;
1384 /* fid[4] offset[8] count[4] */
1385 if (!NPREAD32("fid", &fid, &data, &len) ||
1386 !NPREAD64("offset", &off, &data, &len) ||
1387 !NPREAD32("count", &count, &data, &len)) {
1388 client_send_listener(IMSG_CLOSE, NULL, 0);
1393 if ((f = fid_by_id(fid)) == NULL || f->fd == -1) {
1394 np_error(hdr->tag, "invalid fid");
1398 if (TYPE_OVERFLOW(off_t, off)) {
1399 log_warnx("unexpected off_t size");
1400 np_error(hdr->tag, "invalid offset");
1406 r = pread(f->fd, buf, sizeof(buf), (off_t)off);
1410 np_read(hdr->tag, r, buf);
1412 if (off == 0 && f->offset != 0) {
1417 if (off != f->offset) {
1418 np_error(hdr->tag, "can't seek in directories");
1422 while (EVBUFFER_LENGTH(f->evb) < count) {
1426 if ((d = readdir(f->d)) == NULL)
1428 if (fstatat(f->fd, d->d_name, &sb, 0) == -1) {
1432 serialize_stat(d->d_name, &sb, f->evb);
1435 count = MIN(count, EVBUFFER_LENGTH(f->evb));
1436 np_read(hdr->tag, count, EVBUFFER_DATA(f->evb));
1437 evbuffer_drain(f->evb, count);
1444 twrite(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1449 uint32_t fid, count;
1451 /* fid[4] offset[8] count[4] data[count] */
1452 if (!NPREAD32("fid", &fid, &data, &len) ||
1453 !NPREAD64("off", &off, &data, &len) ||
1454 !NPREAD32("count", &count, &data, &len) ||
1456 client_send_listener(IMSG_CLOSE, NULL, 0);
1461 if ((f = fid_by_id(fid)) == NULL || f->fd == -1) {
1462 np_error(hdr->tag, "invalid fid");
1466 if (!(f->iomode & O_WRONLY) &&
1467 !(f->iomode & O_RDWR)) {
1468 np_error(hdr->tag, "fid not opened for writing");
1472 if (TYPE_OVERFLOW(off_t, off)) {
1473 log_warnx("unexpected off_t size");
1474 np_error(hdr->tag, "invalid offset");
1478 if ((r = pwrite(f->fd, data, len, off)) == -1)
1481 np_write(hdr->tag, r);
1485 tstat(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1487 struct evbuffer *evb;
1494 if (!NPREAD32("fid", &fid, &data, &len)) {
1495 client_send_listener(IMSG_CLOSE, NULL, 0);
1501 * plan9' stat(9P) is not clear on whether the stat is allowed
1502 * on opened fids or not.
1504 if ((f = fid_by_id(fid)) == NULL) {
1505 np_error(hdr->tag, "invalid fid");
1509 if ((evb = evbuffer_new()) == NULL)
1510 fatal("evbuffer_new");
1513 r = fstat(f->fd, &sb);
1515 r = fstatat(f->dir->fd, f->fpath, &sb, 0);
1523 serialize_stat(f->fpath, &sb, evb);
1524 np_stat(hdr->tag, EVBUFFER_LENGTH(evb), EVBUFFER_DATA(evb));
1529 tremove(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1534 char dirpath[PATH_MAX + 3];
1537 if (!NPREAD32("fid", &fid, &data, &len)) {
1538 client_send_listener(IMSG_CLOSE, NULL, 0);
1543 if ((f = fid_by_id(fid)) == NULL) {
1544 np_error(hdr->tag, "invalid fid");
1548 if (f->qid.type & QTDIR) { /* directory */
1549 strlcpy(dirpath, "../", sizeof(dirpath));
1550 strlcat(dirpath, f->fpath, sizeof(dirpath));
1551 r = unlinkat(f->dir->fd, dirpath, AT_REMOVEDIR);
1553 r = unlinkat(f->dir->fd, f->fpath, 0);
1558 np_remove(hdr->tag);
1564 handle_message(struct imsg *imsg, size_t len)
1568 void (*fn)(struct np_msg_header *, const uint8_t *, size_t);
1570 {Tversion, tversion},
1582 struct np_msg_header hdr;
1587 hexdump("incoming packet", imsg->data, len);
1590 parse_message(imsg->data, len, &hdr, &data);
1593 log_debug("got request: len=%d type=%d[%s] tag=%d",
1594 hdr.len, hdr.type, pp_msg_type(hdr.type), hdr.tag);
1596 if (!handshaked && hdr.type != Tversion) {
1597 client_send_listener(IMSG_CLOSE, NULL, 0);
1602 for (i = 0; i < sizeof(msgs)/sizeof(msgs[0]); ++i) {
1603 if (msgs[i].type != hdr.type)
1606 msgs[i].fn(&hdr, data, len);
1610 np_error(hdr.tag, "Not supported.");