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 #define DEBUG_PACKETS 0
41 /* straight outta /src/usr.bin/ssh/scp.c */
42 #define TYPE_OVERFLOW(type, val) \
43 ((sizeof(type) == 4 && (val) > INT32_MAX) || \
44 (sizeof(type) == 8 && (val) > INT64_MAX) || \
45 (sizeof(type) != 4 && sizeof(type) != 8))
47 STAILQ_HEAD(qidhead, qid) qids;
49 /* definition of a qid */
59 STAILQ_ENTRY(qid) entries;
62 STAILQ_HEAD(fidhead, fid) fids;
67 * 0 when the fid was not yet opened for I/O otherwise set to
68 * the flags passed to open(2). O_CLOEXEC means ORCLOSE, that
69 * is to unlink the file upon Tclunk.
74 * if iomode is set, this fid was opened and fd represents its
82 * expected offset for Tread against a directory.
87 STAILQ_ENTRY(fid) entries;
90 static struct imsgev *iev_listener;
91 static struct evbuffer *evb;
92 static uint32_t peerid;
94 static int handshaked;
97 static ATTR_DEAD void client_shutdown(void);
98 static void client_sig_handler(int, short, void *);
99 static void client_dispatch_listener(int, short, void *);
100 static void client_privdrop(const char *, const char *);
102 static int client_send_listener(int, const void *, uint16_t);
104 static void qid_update_from_sb(struct qid *, struct stat *);
105 static struct qid *qid_from_fd(int, const char *, struct stat *);
106 static struct qid *qid_incref(struct qid *);
107 static void qid_decref(struct qid *);
109 static struct fid *new_fid(struct qid *, uint32_t);
110 static struct fid *fid_by_id(uint32_t);
111 static void free_fid(struct fid *);
113 static void parse_message(const uint8_t *, size_t,
114 struct np_msg_header *, uint8_t **);
116 static void np_write16(struct evbuffer *, uint16_t);
117 static void np_write32(struct evbuffer *, uint32_t);
118 static void np_write64(struct evbuffer *, uint64_t);
119 static void np_header(uint32_t, uint8_t, uint16_t);
120 static void np_string(struct evbuffer *, uint16_t, const char *);
121 static void np_qid(struct evbuffer *, struct qid *);
122 static void do_send(void);
124 static void np_version(uint16_t, uint32_t, const char *);
125 static void np_attach(uint16_t, struct qid *);
126 static void np_clunk(uint16_t);
127 static void np_flush(uint16_t);
128 static void np_walk(uint16_t, int, struct qid *);
129 static void np_open(uint16_t, struct qid *, uint32_t);
130 static void np_read(uint16_t, uint32_t, void *);
131 static void np_write(uint16_t, uint32_t);
132 static void np_stat(uint16_t, uint32_t, void *);
133 static void np_error(uint16_t, const char *);
134 static void np_errno(uint16_t);
136 static int np_read8(const char *, const char *, uint8_t *,
137 const uint8_t **, size_t *);
138 static int np_read16(const char *, const char *, uint16_t *,
139 const uint8_t **, size_t *);
140 static int np_read32(const char *, const char *, uint32_t *,
141 const uint8_t **, size_t *);
142 static int np_read64(const char *, const char *, uint64_t *,
143 const uint8_t **, size_t *);
145 #define READSTRERR -1
146 #define READSTRTRUNC -2
147 static int np_readstr(const char *, const char *, char *, size_t,
148 const uint8_t **, size_t *);
150 #define NPREAD8(f, dst, src, len) np_read8(__func__, f, dst, src, len)
151 #define NPREAD16(f, dst, src, len) np_read16(__func__, f, dst, src, len)
152 #define NPREAD32(f, dst, src, len) np_read32(__func__, f, dst, src, len)
153 #define NPREAD64(f, dst, src, len) np_read64(__func__, f, dst, src, len)
155 #define NPREADSTR(f, b, bl, src, len) np_readstr(__func__, f, b, bl, src, len)
157 static void tversion(struct np_msg_header *, const uint8_t *, size_t);
158 static void tattach(struct np_msg_header *, const uint8_t *, size_t);
159 static void tclunk(struct np_msg_header *, const uint8_t *, size_t);
160 static void tflush(struct np_msg_header *, const uint8_t *, size_t);
161 static void twalk(struct np_msg_header *, const uint8_t *, size_t);
162 static void topen(struct np_msg_header *, const uint8_t *, size_t);
163 static void tread(struct np_msg_header *, const uint8_t *, size_t);
164 static void twrite(struct np_msg_header *, const uint8_t *, size_t);
165 static void tstat(struct np_msg_header *, const uint8_t *, size_t);
166 static void handle_message(struct imsg *, size_t);
169 client(int debug, int verbose)
171 struct event ev_sigint, ev_sigterm;
173 log_init(debug, LOG_DAEMON);
174 log_setverbose(verbose);
176 setproctitle("client");
177 log_procinit("client");
179 log_debug("warming up");
183 /* Setup signal handlers */
184 signal_set(&ev_sigint, SIGINT, client_sig_handler, NULL);
185 signal_set(&ev_sigterm, SIGTERM, client_sig_handler, NULL);
187 signal_add(&ev_sigint, NULL);
188 signal_add(&ev_sigterm, NULL);
190 signal(SIGPIPE, SIG_IGN);
191 signal(SIGHUP, SIG_IGN);
193 /* Setup pipe and event handler to the listener process */
194 if ((iev_listener = malloc(sizeof(*iev_listener))) == NULL)
197 imsg_init(&iev_listener->ibuf, 3);
198 iev_listener->handler = client_dispatch_listener;
200 /* Setup event handlers. */
201 iev_listener->events = EV_READ;
202 event_set(&iev_listener->ev, iev_listener->ibuf.fd,
203 iev_listener->events, iev_listener->handler, iev_listener);
204 event_add(&iev_listener->ev, NULL);
210 static ATTR_DEAD void
211 client_shutdown(void)
216 msgbuf_clear(&iev_listener->ibuf.w);
217 close(iev_listener->ibuf.fd);
221 log_debug("client exiting");
226 client_sig_handler(int sig, short event, void *d)
229 * Normal signal handler rules don't apply because libevent
238 fatalx("unexpected signal %d", sig);
247 client_dispatch_listener(int fd, short event, void *d)
249 static int auth = AUTH_NONE;
250 static char username[64] = {0};
251 static char dir[PATH_MAX] = {0};
253 struct imsgev *iev = d;
254 struct imsgbuf *ibuf;
260 if (event & EV_READ) {
261 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
262 fatal("imsg_read error");
263 if (n == 0) /* Connection closed */
266 if (event & EV_WRITE) {
267 if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
268 fatal("msgbuf_write");
269 if (n == 0) /* Connection closed */
274 if ((n = imsg_get(ibuf, &imsg)) == -1)
275 fatal("%s: imsg_get error", __func__);
276 if (n == 0) /* No more messages. */
279 switch (imsg.hdr.type) {
281 peerid = imsg.hdr.peerid;
283 fatalx("%s: IMSG_AUTH already done", __func__);
285 ((char *)imsg.data)[IMSG_DATA_SIZE(imsg)-1] = '\0';
286 strlcpy(username, imsg.data, sizeof(username));
289 if (auth != AUTH_USER)
290 fatalx("%s: IMSG_AUTH_DIR not after IMSG_AUTH",
293 ((char *)imsg.data)[IMSG_DATA_SIZE(imsg)-1] = '\0';
294 strlcpy(dir, imsg.data, sizeof(dir));
295 client_privdrop(username, dir);
296 memset(username, 0, sizeof(username));
297 memset(dir, 0, sizeof(username));
302 fatalx("%s: can't handle messages before"
303 " doing the auth", __func__);
304 handle_message(&imsg, IMSG_DATA_SIZE(imsg));
307 log_debug("closing");
311 log_debug("%s: unexpected imsg %d",
312 __func__, imsg.hdr.type);
321 /* This pipe is dead. Remove its event handler. */
323 log_debug("pipe closed, shutting down...");
324 event_loopexit(NULL);
329 client_privdrop(const char *username, const char *dir)
333 setproctitle("client %s", username);
335 if ((pw = getpwnam(username)) == NULL)
336 fatalx("getpwnam(%s) failed", username);
338 if (chroot(dir) == -1)
340 if (chdir("/") == -1)
341 fatal("chdir(\"/\")");
343 if (setgroups(1, &pw->pw_gid) ||
344 setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
345 setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
346 fatal("can't drop privileges");
349 log_debug("client ready; user=%s dir=%s", username, dir);
351 if ((evb = evbuffer_new()) == NULL)
352 fatal("evbuffer_new");
356 client_send_listener(int type, const void *data, uint16_t len)
360 if ((ret = imsg_compose(&iev_listener->ibuf, type, peerid, 0, -1,
362 imsg_event_add(iev_listener);
367 /* set qid fields from sb */
369 qid_update_from_sb(struct qid *qid, struct stat *sb)
371 qid->path = sb->st_ino;
374 * Theoretically (and hopefully!) this should be a 64 bit
375 * number. Unfortunately, 9P uses 32 bit timestamps.
377 qid->vers = sb->st_mtim.tv_sec;
379 if (S_ISREG(sb->st_mode))
381 else if (S_ISDIR(sb->st_mode))
383 else if (S_ISLNK(sb->st_mode))
384 qid->type = QTSYMLINK;
387 /* creates a qid given a fd */
389 qid_from_fd(int fd, const char *path, struct stat *s)
395 if ((qid = calloc(1, sizeof(*qid))) == NULL)
399 strlcpy(qid->fpath, path, sizeof(qid->fpath));
408 r = fstatat(fd, path, s, 0);
415 qid_update_from_sb(qid, s);
417 STAILQ_INSERT_HEAD(&qids, qid, entries);
423 qid_incref(struct qid *qid)
430 qid_decref(struct qid *qid)
432 if (--qid->refcount > 0)
435 STAILQ_REMOVE(&qids, qid, qid, entries);
442 new_fid(struct qid *qid, uint32_t fid)
446 if ((f = calloc(1, sizeof(*f))) == NULL)
449 f->qid = qid_incref(qid);
453 STAILQ_INSERT_HEAD(&fids, f, entries);
459 fid_by_id(uint32_t fid)
463 STAILQ_FOREACH(f, &fids, entries) {
472 free_fid(struct fid *f)
478 r = closedir(f->dir);
483 fatal("can't close fid %d", f->fid);
486 evbuffer_free(f->evb);
488 /* try to honour ORCLOSE if requested */
489 if (f->iomode & O_CLOEXEC)
490 unlinkat(f->qid->fd, f->qid->fpath, 0);
495 STAILQ_REMOVE(&fids, f, fid, entries);
500 parse_message(const uint8_t *data, size_t len, struct np_msg_header *hdr,
505 if (!NPREAD32("len", &hdr->len, &data, &len) ||
506 !NPREAD8("type", &hdr->type, &data, &len) ||
507 !NPREAD16("tag", &hdr->tag, &data, &len))
510 if (olen != hdr->len)
513 if (hdr->type < Tversion ||
515 hdr->type == Terror ||
516 (hdr->type & 0x1) != 0) /* cannot recv a R* */
519 hdr->tag = le32toh(hdr->tag);
521 *cnt = (uint8_t *)data;
525 /* TODO: send a proper message to terminate the connection. */
526 fatalx("got invalid message");
530 np_write16(struct evbuffer *e, uint16_t x)
533 evbuffer_add(e, &x, sizeof(x));
537 np_write32(struct evbuffer *e, uint32_t x)
540 evbuffer_add(e, &x, sizeof(x));
544 np_write64(struct evbuffer *e, uint64_t x)
547 evbuffer_add(e, &x, sizeof(x));
551 np_writebuf(struct evbuffer *e, size_t len, void *data)
553 evbuffer_add(e, data, len);
557 np_header(uint32_t len, uint8_t type, uint16_t tag)
564 evbuffer_add(evb, &len, sizeof(len));
565 evbuffer_add(evb, &type, sizeof(type));
566 evbuffer_add(evb, &tag, sizeof(tag));
570 np_string(struct evbuffer *e, uint16_t len, const char *str)
575 evbuffer_add(e, &len, sizeof(len));
576 evbuffer_add(e, str, l);
580 np_qid(struct evbuffer *e, struct qid *qid)
585 path = htole64(qid->path);
586 vers = htole32(qid->vers);
588 evbuffer_add(e, &qid->type, sizeof(qid->type));
589 evbuffer_add(e, &vers, sizeof(vers));
590 evbuffer_add(e, &path, sizeof(path));
599 len = EVBUFFER_LENGTH(evb);
600 data = EVBUFFER_DATA(evb);
603 hexdump("outgoing packet", data, len);
605 client_send_listener(IMSG_BUF, data, len);
606 evbuffer_drain(evb, len);
610 np_version(uint16_t tag, uint32_t msize, const char *version)
616 msize = htole32(msize);
618 np_header(sizeof(msize) + sizeof(l) + l, Rversion, tag);
619 evbuffer_add(evb, &msize, sizeof(msize));
620 np_string(evb, l, version);
625 np_attach(uint16_t tag, struct qid *qid)
627 np_header(QIDSIZE, Rattach, tag);
633 np_clunk(uint16_t tag)
635 np_header(0, Rclunk, tag);
640 np_flush(uint16_t tag)
642 np_header(0, Rflush, tag);
647 np_walk(uint16_t tag, int nwqid, struct qid *wqid)
651 /* two bytes for the counter */
652 np_header(2 + QIDSIZE * nwqid, Rwalk, tag);
653 np_write16(evb, nwqid);
654 for (i = 0; i < nwqid; ++i)
655 np_qid(evb, wqid + i);
661 np_open(uint16_t tag, struct qid *qid, uint32_t iounit)
663 np_header(QIDSIZE + sizeof(iounit), Ropen, tag);
665 np_write32(evb, iounit);
670 np_read(uint16_t tag, uint32_t count, void *data)
672 np_header(sizeof(count) + count, Rread, tag);
673 np_write32(evb, count);
674 np_writebuf(evb, count, data);
679 np_write(uint16_t tag, uint32_t count)
681 np_header(sizeof(count), Rwrite, tag);
682 np_write32(evb, count);
687 np_stat(uint16_t tag, uint32_t count, void *data)
689 np_header(count, Rstat, tag);
690 np_writebuf(evb, count, data);
695 np_error(uint16_t tag, const char *errstr)
701 np_header(sizeof(l) + l, Rerror, tag);
702 np_string(evb, l, errstr);
707 np_errno(uint16_t tag)
714 strerror_r(errno, buf, sizeof(buf));
721 np_read8(const char *t, const char *f, uint8_t *dst, const uint8_t **src,
724 if (*len < sizeof(*dst)) {
725 log_warnx("%s: wanted %zu bytes for the %s field but only "
726 "%zu are available.", t, sizeof(*dst), f, *len);
730 memcpy(dst, *src, sizeof(*dst));
731 *src += sizeof(*dst);
732 *len -= sizeof(*dst);
738 np_read16(const char *t, const char *f, uint16_t *dst, const uint8_t **src,
741 if (*len < sizeof(*dst)) {
742 log_warnx("%s: wanted %zu bytes for the %s field but only "
743 "%zu are available.", t, sizeof(*dst), f, *len);
747 memcpy(dst, *src, sizeof(*dst));
748 *src += sizeof(*dst);
749 *len -= sizeof(*dst);
750 *dst = le16toh(*dst);
756 np_read32(const char *t, const char *f, uint32_t *dst, const uint8_t **src,
759 if (*len < sizeof(*dst)) {
760 log_warnx("%s: wanted %zu bytes for the %s field but only "
761 "%zu are available.", t, sizeof(*dst), f, *len);
765 memcpy(dst, *src, sizeof(*dst));
766 *src += sizeof(*dst);
767 *len -= sizeof(*dst);
768 *dst = le32toh(*dst);
774 np_read64(const char *t, const char *f, uint64_t *dst, const uint8_t **src,
777 if (*len < sizeof(*dst)) {
778 log_warnx("%s: wanted %zu bytes for the %s field but only "
779 "%zu are available.", t, sizeof(*dst), f, *len);
783 memcpy(dst, *src, sizeof(*dst));
784 *src += sizeof(*dst);
785 *len -= sizeof(*dst);
786 *dst = le64toh(*dst);
792 np_readstr(const char *t, const char *f, char *res, size_t reslen,
793 const uint8_t **src, size_t *len)
798 strlcpy(buf, f, sizeof(buf));
799 strlcat(buf, "-len", sizeof(buf));
801 if (!np_read16(t, buf, &sl, src, len))
805 log_warnx("%s: wanted %d bytes for the %s field but only "
806 "%zu are available.", t, sl, f, *len);
813 memcpy(res, *src, sl);
822 tversion(struct np_msg_header *hdr, const uint8_t *data, size_t len)
824 char *dot, version[32];
829 /* msize[4] version[s] */
830 if (!NPREAD32("msize", &msize, &data, &len))
833 switch (NPREADSTR("version", version, sizeof(version), &data, &len)) {
837 log_warnx("9P version string too long, truncated");
841 if ((dot = strchr(version, '.')) != NULL)
844 if (strcmp(version, VERSION9P) != 0 ||
848 /* version matched */
850 msize = MIN(msize, MSIZE9P);
851 client_send_listener(IMSG_MSIZE, &msize, sizeof(msize));
852 np_version(hdr->tag, msize, VERSION9P);
856 log_warnx("unknown 9P version string: \"%s\", want "VERSION9P,
858 np_version(hdr->tag, MSIZE9P, "unknown");
862 client_send_listener(IMSG_CLOSE, NULL, 0);
867 tattach(struct np_msg_header *hdr, const uint8_t *data, size_t len)
873 char aname[PATH_MAX];
875 /* fid[4] afid[4] uname[s] aname[s] */
877 if (!NPREAD32("fid", &fid, &data, &len) ||
878 !NPREAD32("afid", &afid, &data, &len))
881 /* read the uname but don't actually use it */
882 switch (NPREADSTR("uname", aname, sizeof(aname), &data, &len)) {
886 np_error(hdr->tag, "name too long");
890 switch (NPREADSTR("aname", aname, sizeof(aname), &data, &len)) {
894 np_error(hdr->tag, "name too long");
898 if (fid_by_id(fid) != NULL || afid != NOFID) {
899 np_error(hdr->tag, "invalid fid or afid");
903 if ((fd = open(aname, O_RDONLY|O_DIRECTORY)) == -1)
906 if ((qid = qid_from_fd(fd, NULL, NULL)) == NULL)
909 log_debug("attached %s to %d", aname, fid);
911 if ((f = new_fid(qid, fid)) == NULL) {
916 np_attach(hdr->tag, qid);
921 log_warn("failed to attach %s", aname);
926 client_send_listener(IMSG_CLOSE, NULL, 0);
931 tclunk(struct np_msg_header *hdr, const uint8_t *data, size_t len)
937 if (!NPREAD32("fid", &fid, &data, &len)) {
938 client_send_listener(IMSG_CLOSE, NULL, 0);
943 if ((f = fid_by_id(fid)) == NULL) {
944 np_error(hdr->tag, "invalid fid");
953 tflush(struct np_msg_header *hdr, const uint8_t *data, size_t len)
958 * We're doing only synchronous I/O. Tflush is implemented
959 * only because it's illegal to reply with a Rerror.
963 if (len != sizeof(oldtag)) {
964 log_warnx("Tclunk with the wrong size: got %zu want %zu",
965 len, sizeof(oldtag));
966 client_send_listener(IMSG_CLOSE, NULL, 0);
975 twalk(struct np_msg_header *hdr, const uint8_t *data, size_t len)
978 struct qid *qid, wqid[MAXWELEM] = {0};
980 uint32_t fid, newfid;
982 int fd, oldfd, no, nwqid = 0;
985 if (!NPREAD32("fid", &fid, &data, &len) ||
986 !NPREAD32("newfid", &newfid, &data, &len) ||
987 !NPREAD16("nwname", &nwname, &data, &len))
990 if (nwname > MAXWELEM) {
991 log_warnx("Twalk: more than %d path elements: %d",
996 if ((f = fid_by_id(fid)) == NULL) {
997 np_error(hdr->tag, "invalid fid");
1002 np_error(hdr->tag, "fid already opened for I/O");
1008 else if ((nf = fid_by_id(newfid)) != NULL) {
1009 np_error(hdr->tag, "newfid already in use");
1014 /* special case: fid duplication */
1017 * TODO: should we forbid fids duplication when fid ==
1020 if (nf == NULL && (nf = new_fid(f->qid, newfid)) == NULL)
1021 fatal("new_fid duplication");
1023 np_walk(hdr->tag, 1, f->qid);
1027 if (f->qid->type != QTDIR) {
1028 np_error(hdr->tag, "fid doesn't represent a directory");
1034 for (nwqid = 0; nwqid < nwname; nwqid++) {
1035 switch (NPREADSTR("wname", wnam, sizeof(wnam), &data, &len)) {
1039 np_error(hdr->tag, "wname too long");
1043 if (*wnam == '\0' ||
1044 strchr(wnam, '/') != NULL ||
1045 !strcmp(wnam, ".")) {
1050 if ((fd = openat(oldfd, wnam, O_RDONLY|O_DIRECTORY)) == -1 &&
1054 if ((fd == -1 && fstatat(oldfd, wnam, &sb, 0) == -1) ||
1055 (fd != -1 && fstat(fd, &sb) == -1))
1058 qid_update_from_sb(&wqid[nwqid], &sb);
1060 /* reached a file but we still have other components */
1061 if (fd == -1 && nwqid+1 < nwname)
1064 /* reached the end and found a file */
1065 if (fd == -1 && nwqid+1 == nwname)
1068 if (oldfd != f->qid->fd)
1074 * There can be two possibilities: fd == -1 means that we've
1075 * reached a file and we should save BOTH the dirfd (oldfd)
1076 * and the path (wnam); or we just reached another directory,
1077 * in which case we can just create a new qid using fd.
1080 qid = qid_from_fd(oldfd, wnam, &sb);
1082 qid = qid_from_fd(oldfd, NULL, &sb);
1084 fatal("qid_from_fd");
1087 if ((nf = new_fid(qid, newfid)) == NULL)
1091 qid_decref(nf->qid);
1092 nf->qid = qid_incref(qid);
1095 np_walk(hdr->tag, nwqid, wqid);
1099 if (oldfd != f->qid->fd)
1103 np_error(hdr->tag, strerror(no));
1105 np_walk(hdr->tag, nwqid, wqid);
1109 client_send_listener(IMSG_CLOSE, NULL, 0);
1114 npmode_to_unix(uint8_t mode, int *flags)
1116 switch (mode & 0x0F) {
1127 log_warnx("tried to open something with KOEXEC");
1135 if (mode & KORCLOSE)
1136 *flags |= O_CLOEXEC;
1142 topen(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1151 /* fid[4] mode[1] */
1152 if (!NPREAD32("fid", &fid, &data, &len) ||
1153 !NPREAD8("mode", &mode, &data, &len)) {
1154 client_send_listener(IMSG_CLOSE, NULL, 0);
1159 if ((f = fid_by_id(fid)) == NULL || f->fd != -1) {
1160 np_error(hdr->tag, "invalid fid");
1164 if (npmode_to_unix(mode, &f->iomode) == -1) {
1165 np_error(hdr->tag, "invalid mode");
1169 if (f->qid->type & QTDIR &&
1170 (f->iomode & O_WRONLY || f->iomode & O_RDWR)) {
1171 np_error(hdr->tag, "can't open directory for writing");
1175 path = f->qid->fpath;
1179 if ((f->fd = openat(f->qid->fd, path, f->iomode)) == -1) {
1180 np_error(hdr->tag, strerror(errno));
1184 if (fstat(f->fd, &sb) == -1)
1187 if (S_ISDIR(sb.st_mode)) {
1188 assert(f->qid->type & QTDIR);
1189 if ((f->dir = fdopendir(f->fd)) == NULL) {
1196 if ((f->evb = evbuffer_new()) == NULL) {
1206 qid_update_from_sb(&qid, &sb);
1207 np_open(hdr->tag, &qid, sb.st_blksize);
1211 serialize_stat(const char *fname, struct stat *sb, struct evbuffer *evb)
1214 const char *uid, *gid, *muid;
1216 uint16_t namlen, uidlen, gidlen, ulen;
1218 qid_update_from_sb(&qid, sb);
1220 /* TODO: fill these fields */
1225 namlen = strlen(fname);
1226 uidlen = strlen(uid);
1227 gidlen = strlen(gid);
1228 ulen = strlen(muid);
1230 tot = NPSTATSIZ(namlen, uidlen, gidlen, ulen);
1231 if (tot > UINT32_MAX) {
1232 log_warnx("stat info for dir entry %s would overflow",
1237 np_write16(evb, tot); /* size[2] */
1238 np_write16(evb, sb->st_rdev); /* type[2] */
1239 np_write32(evb, sb->st_dev); /* dev[4] */
1240 np_qid(evb, &qid); /* qid[13] */
1242 /* XXX: translate? */
1243 np_write32(evb, sb->st_mode); /* mode[4] */
1245 np_write32(evb, sb->st_atim.tv_sec); /* atime[4] */
1246 np_write32(evb, sb->st_mtim.tv_sec); /* mtime[4] */
1247 np_write64(evb, sb->st_size); /* length[8] */
1248 np_string(evb, namlen, fname); /* name[s] */
1249 np_string(evb, uidlen, uid); /* uid[s] */
1250 np_string(evb, gidlen, gid); /* gid[s] */
1251 np_string(evb, ulen, muid); /* muid[s] */
1255 tread(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1260 uint32_t fid, count;
1263 /* fid[4] offset[8] count[4] */
1264 if (!NPREAD32("fid", &fid, &data, &len) ||
1265 !NPREAD64("offset", &off, &data, &len) ||
1266 !NPREAD32("count", &count, &data, &len)) {
1267 client_send_listener(IMSG_CLOSE, NULL, 0);
1272 if ((f = fid_by_id(fid)) == NULL || f->fd == -1) {
1273 np_error(hdr->tag, "invalid fid");
1277 if (TYPE_OVERFLOW(off_t, off)) {
1278 log_warnx("unexpected size_t size");
1279 np_error(hdr->tag, "invalid offset");
1283 if (f->dir == NULL) {
1285 r = pread(f->fd, buf, sizeof(buf), (off_t)off);
1289 np_read(hdr->tag, r, buf);
1291 if (off == 0 && f->offset != 0) {
1296 if (off != f->offset) {
1297 np_error(hdr->tag, "can't seek in directories");
1301 while (EVBUFFER_LENGTH(f->evb) < count) {
1305 if ((d = readdir(f->dir)) == NULL)
1307 if (fstatat(f->fd, d->d_name, &sb, 0) == -1) {
1311 serialize_stat(d->d_name, &sb, f->evb);
1314 count = MIN(count, EVBUFFER_LENGTH(f->evb));
1315 np_read(hdr->tag, count, EVBUFFER_DATA(f->evb));
1316 evbuffer_drain(f->evb, count);
1323 twrite(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1328 uint32_t fid, count;
1330 /* fid[4] offset[8] count[4] data[count] */
1331 if (!NPREAD32("fid", &fid, &data, &len) ||
1332 !NPREAD64("off", &off, &data, &len) ||
1333 !NPREAD32("count", &count, &data, &len) ||
1335 client_send_listener(IMSG_CLOSE, NULL, 0);
1340 if ((f = fid_by_id(fid)) == NULL || f->fd == -1) {
1341 np_error(hdr->tag, "invalid fid");
1345 if (!(f->iomode & O_WRONLY) &&
1346 !(f->iomode & O_RDWR)) {
1347 np_error(hdr->tag, "fid not opened for writing");
1351 if (TYPE_OVERFLOW(off_t, off)) {
1352 log_warnx("unexpected off_t size");
1353 np_error(hdr->tag, "invalid offset");
1357 if ((r = pwrite(f->fd, data, len, off)) == -1)
1360 np_write(hdr->tag, r);
1364 tstat(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1366 struct evbuffer *evb;
1374 if (!NPREAD32("fid", &fid, &data, &len)) {
1375 client_send_listener(IMSG_CLOSE, NULL, 0);
1381 * plan9' stat(9P) is not clear on whether the stat is allowed
1382 * on opened fids or not.
1384 if ((f = fid_by_id(fid)) == NULL) {
1385 np_error(hdr->tag, "invalid fid");
1389 if ((evb = evbuffer_new()) == NULL)
1390 fatal("evbuffer_new");
1392 if (f->qid->type & QTDIR) {
1394 r = fstat(f->qid->fd, &sb);
1396 fname = f->qid->fpath;
1397 r = fstatat(f->qid->fd, f->qid->fpath, &sb, 0);
1406 serialize_stat(fname, &sb, evb);
1407 np_stat(hdr->tag, EVBUFFER_LENGTH(evb), EVBUFFER_DATA(evb));
1412 handle_message(struct imsg *imsg, size_t len)
1416 void (*fn)(struct np_msg_header *, const uint8_t *, size_t);
1418 {Tversion, tversion},
1428 struct np_msg_header hdr;
1433 hexdump("incoming packet", imsg->data, len);
1436 parse_message(imsg->data, len, &hdr, &data);
1439 log_debug("got request: len=%d type=%d[%s] tag=%d",
1440 hdr.len, hdr.type, pp_msg_type(hdr.type), hdr.tag);
1442 if (!handshaked && hdr.type != Tversion) {
1443 client_send_listener(IMSG_CLOSE, NULL, 0);
1448 for (i = 0; i < sizeof(msgs)/sizeof(msgs[0]); ++i) {
1449 if (msgs[i].type != hdr.type)
1452 msgs[i].fn(&hdr, data, len);
1456 np_error(hdr.tag, "Not supported.");