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.
20 #include <sys/types.h>
43 * XXX: atm is difficult to accept messages bigger than MAX_IMSGSIZE
44 * minus IMSG_HEADER_SIZE, we need something to split messages into
45 * chunks and receive them one by the other.
47 * CLIENT_MSIZE is thus the maximum message size we can handle now.
49 #define CLIENT_MSIZE (MAX_IMSGSIZE - IMSG_HEADER_SIZE)
51 #define DEBUG_PACKETS 0
53 /* straight outta /src/usr.bin/ssh/scp.c */
54 #define TYPE_OVERFLOW(type, val) \
55 ((sizeof(type) == 4 && (val) > INT32_MAX) || \
56 (sizeof(type) == 8 && (val) > INT64_MAX) || \
57 (sizeof(type) != 4 && sizeof(type) != 8))
59 STAILQ_HEAD(dirhead, dir) dirs;
63 STAILQ_ENTRY(dir) entries;
66 STAILQ_HEAD(fidhead, fid) fids;
73 * the flags passed to open(2). O_CLOEXEC means ORCLOSE, that
74 * is to unlink the file upon Tclunk.
79 * if fd is not -1 this fid was opened, fd represents its
80 * file descriptor and iomode the flags passed to open(2).
87 * expected offset for Tread against a directory.
93 STAILQ_ENTRY(fid) entries;
96 static struct imsgev *iev_listener;
97 static struct evbuffer *evb;
98 static uint32_t peerid;
100 static int handshaked;
103 static __dead void client_shutdown(void);
104 static void client_sig_handler(int, short, void *);
105 static void client_dispatch_listener(int, short, void *);
106 static void client_privdrop(const char *, const char *);
108 static int client_send_listener(int, const void *, uint16_t);
110 static void qid_update_from_sb(struct qid *, struct stat *);
112 static struct dir *new_dir(int);
113 static struct dir *dir_incref(struct dir *);
114 static void dir_decref(struct dir *);
116 static struct fid *new_fid(struct dir *, uint32_t, const char *, struct qid *);
117 static struct fid *fid_by_id(uint32_t);
118 static void free_fid(struct fid *);
120 static void parse_message(const uint8_t *, size_t,
121 struct np_msg_header *, uint8_t **);
123 static void np_write16(struct evbuffer *, uint16_t);
124 static void np_write32(struct evbuffer *, uint32_t);
125 static void np_write64(struct evbuffer *, uint64_t);
126 static void np_header(uint32_t, uint8_t, uint16_t);
127 static void np_string(struct evbuffer *, uint16_t, const char *);
128 static void np_qid(struct evbuffer *, struct qid *);
129 static void do_send(void);
131 static void np_version(uint16_t, uint32_t, const char *);
132 static void np_attach(uint16_t, struct qid *);
133 static void np_clunk(uint16_t);
134 static void np_flush(uint16_t);
135 static void np_walk(uint16_t, int, struct qid *);
136 static void np_open(uint16_t, struct qid *, uint32_t);
137 static void np_create(uint16_t, struct qid *, uint32_t);
138 static void np_read(uint16_t, uint32_t, void *);
139 static void np_write(uint16_t, uint32_t);
140 static void np_stat(uint16_t, uint32_t, void *);
141 static void np_remove(uint16_t);
142 static void np_error(uint16_t, const char *);
143 static void np_errno(uint16_t);
145 static int np_read8(const char *, const char *, uint8_t *,
146 const uint8_t **, size_t *);
147 static int np_read16(const char *, const char *, uint16_t *,
148 const uint8_t **, size_t *);
149 static int np_read32(const char *, const char *, uint32_t *,
150 const uint8_t **, size_t *);
151 static int np_read64(const char *, const char *, uint64_t *,
152 const uint8_t **, size_t *);
154 #define READSTRERR -1
155 #define READSTRTRUNC -2
156 static int np_readstr(const char *, const char *, char *, size_t,
157 const uint8_t **, size_t *);
159 #define NPREAD8(f, dst, src, len) np_read8(__func__, f, dst, src, len)
160 #define NPREAD16(f, dst, src, len) np_read16(__func__, f, dst, src, len)
161 #define NPREAD32(f, dst, src, len) np_read32(__func__, f, dst, src, len)
162 #define NPREAD64(f, dst, src, len) np_read64(__func__, f, dst, src, len)
164 #define NPREADSTR(f, b, bl, src, len) np_readstr(__func__, f, b, bl, src, len)
166 static void tversion(struct np_msg_header *, const uint8_t *, size_t);
167 static void tattach(struct np_msg_header *, const uint8_t *, size_t);
168 static void tclunk(struct np_msg_header *, const uint8_t *, size_t);
169 static void tflush(struct np_msg_header *, const uint8_t *, size_t);
170 static void twalk(struct np_msg_header *, const uint8_t *, size_t);
171 static void topen(struct np_msg_header *, const uint8_t *, size_t);
172 static void tcreate(struct np_msg_header *, const uint8_t *, size_t);
173 static void tread(struct np_msg_header *, const uint8_t *, size_t);
174 static void twrite(struct np_msg_header *, const uint8_t *, size_t);
175 static void tstat(struct np_msg_header *, const uint8_t *, size_t);
176 static void tremove(struct np_msg_header *, const uint8_t *, size_t);
177 static void handle_message(struct imsg *, size_t);
180 client(int debug, int verbose)
182 struct event ev_sigint, ev_sigterm;
184 log_init(debug, LOG_DAEMON);
185 log_setverbose(verbose);
187 setproctitle("client");
188 log_procinit("client");
190 log_debug("warming up");
194 /* Setup signal handlers */
195 signal_set(&ev_sigint, SIGINT, client_sig_handler, NULL);
196 signal_set(&ev_sigterm, SIGTERM, client_sig_handler, NULL);
198 signal_add(&ev_sigint, NULL);
199 signal_add(&ev_sigterm, NULL);
201 signal(SIGPIPE, SIG_IGN);
202 signal(SIGHUP, SIG_IGN);
204 /* Setup pipe and event handler to the listener process */
205 if ((iev_listener = malloc(sizeof(*iev_listener))) == NULL)
208 imsg_init(&iev_listener->ibuf, 3);
209 iev_listener->handler = client_dispatch_listener;
211 /* Setup event handlers. */
212 iev_listener->events = EV_READ;
213 event_set(&iev_listener->ev, iev_listener->ibuf.fd,
214 iev_listener->events, iev_listener->handler, iev_listener);
215 event_add(&iev_listener->ev, NULL);
222 client_shutdown(void)
227 msgbuf_clear(&iev_listener->ibuf.w);
228 close(iev_listener->ibuf.fd);
232 log_debug("client exiting");
237 client_sig_handler(int sig, short event, void *d)
240 * Normal signal handler rules don't apply because libevent
249 fatalx("unexpected signal %d", sig);
258 client_dispatch_listener(int fd, short event, void *d)
260 static int auth = AUTH_NONE;
261 static char username[64] = {0};
262 static char dir[PATH_MAX] = {0};
264 struct imsgev *iev = d;
265 struct imsgbuf *ibuf;
271 if (event & EV_READ) {
272 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
273 fatal("imsg_read error");
274 if (n == 0) /* Connection closed */
277 if (event & EV_WRITE) {
278 if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
279 fatal("msgbuf_write");
280 if (n == 0) /* Connection closed */
285 if ((n = imsg_get(ibuf, &imsg)) == -1)
286 fatal("%s: imsg_get error", __func__);
287 if (n == 0) /* No more messages. */
290 switch (imsg.hdr.type) {
292 peerid = imsg.hdr.peerid;
294 fatalx("%s: IMSG_AUTH already done", __func__);
296 ((char *)imsg.data)[IMSG_DATA_SIZE(imsg)-1] = '\0';
297 strlcpy(username, imsg.data, sizeof(username));
300 if (auth != AUTH_USER)
301 fatalx("%s: IMSG_AUTH_DIR not after IMSG_AUTH",
304 ((char *)imsg.data)[IMSG_DATA_SIZE(imsg)-1] = '\0';
305 strlcpy(dir, imsg.data, sizeof(dir));
306 client_privdrop(username, dir);
307 memset(username, 0, sizeof(username));
308 memset(dir, 0, sizeof(username));
313 fatalx("%s: can't handle messages before"
314 " doing the auth", __func__);
315 handle_message(&imsg, IMSG_DATA_SIZE(imsg));
318 log_debug("closing");
322 log_debug("%s: unexpected imsg %d",
323 __func__, imsg.hdr.type);
332 /* This pipe is dead. Remove its event handler. */
334 log_debug("pipe closed, shutting down...");
335 event_loopexit(NULL);
340 client_privdrop(const char *username, const char *dir)
344 setproctitle("client %s", username);
346 if ((pw = getpwnam(username)) == NULL)
347 fatalx("getpwnam(%s) failed", username);
349 if (chroot(dir) == -1)
351 if (chdir("/") == -1)
352 fatal("chdir(\"/\")");
354 if (setgroups(1, &pw->pw_gid) ||
355 setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
356 setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
357 fatal("can't drop privileges");
360 log_debug("client ready; user=%s dir=%s", username, dir);
362 if ((evb = evbuffer_new()) == NULL)
363 fatal("evbuffer_new");
367 client_send_listener(int type, const void *data, uint16_t len)
371 if ((ret = imsg_compose(&iev_listener->ibuf, type, peerid, 0, -1,
373 imsg_event_add(iev_listener);
378 /* set qid fields from sb */
380 qid_update_from_sb(struct qid *qid, struct stat *sb)
382 qid->path = sb->st_ino;
385 * Theoretically (and hopefully!) this should be a 64 bit
386 * number. Unfortunately, 9P uses 32 bit timestamps.
388 qid->vers = sb->st_mtim.tv_sec;
390 if (S_ISREG(sb->st_mode))
392 else if (S_ISDIR(sb->st_mode))
394 else if (S_ISLNK(sb->st_mode))
395 qid->type = QTSYMLINK;
398 /* creates a qid given a fd */
404 if ((dir = calloc(1, sizeof(*dir))) == NULL)
408 STAILQ_INSERT_HEAD(&dirs, dir, entries);
413 dir_incref(struct dir *dir)
420 dir_decref(struct dir *dir)
422 if (--dir->refcount > 0)
425 STAILQ_REMOVE(&dirs, dir, dir, entries);
432 new_fid(struct dir *dir, uint32_t fid, const char *path, struct qid *qid)
439 if (fstatat(dir->fd, path, &sb, 0)) {
440 log_warn("fstatat(%s)", path);
443 qid_update_from_sb(&q, &sb);
447 if ((f = calloc(1, sizeof(*f))) == NULL)
450 f->dir = dir_incref(dir);
454 strlcpy(f->fpath, path, sizeof(f->fpath));
456 memcpy(&f->qid, qid, sizeof(f->qid));
458 STAILQ_INSERT_HEAD(&fids, f, entries);
464 fid_by_id(uint32_t fid)
468 STAILQ_FOREACH(f, &fids, entries) {
477 free_fid(struct fid *f)
488 fatal("can't close fid %d", f->fid);
491 evbuffer_free(f->evb);
493 /* try to honour ORCLOSE if requested */
494 if (f->iomode & O_CLOEXEC)
495 unlinkat(f->dir->fd, f->fpath, 0);
500 STAILQ_REMOVE(&fids, f, fid, entries);
505 parse_message(const uint8_t *data, size_t len, struct np_msg_header *hdr,
510 if (!NPREAD32("len", &hdr->len, &data, &len) ||
511 !NPREAD8("type", &hdr->type, &data, &len) ||
512 !NPREAD16("tag", &hdr->tag, &data, &len))
515 if (olen != hdr->len)
518 if (hdr->type < Tversion ||
520 hdr->type == Terror ||
521 (hdr->type & 0x1) != 0) /* cannot recv a R* */
524 hdr->tag = le32toh(hdr->tag);
526 *cnt = (uint8_t *)data;
530 /* TODO: send a proper message to terminate the connection. */
531 fatalx("got invalid message");
535 np_write16(struct evbuffer *e, uint16_t x)
538 evbuffer_add(e, &x, sizeof(x));
542 np_write32(struct evbuffer *e, uint32_t x)
545 evbuffer_add(e, &x, sizeof(x));
549 np_write64(struct evbuffer *e, uint64_t x)
552 evbuffer_add(e, &x, sizeof(x));
556 np_writebuf(struct evbuffer *e, size_t len, void *data)
558 evbuffer_add(e, data, len);
562 np_header(uint32_t len, uint8_t type, uint16_t tag)
569 evbuffer_add(evb, &len, sizeof(len));
570 evbuffer_add(evb, &type, sizeof(type));
571 evbuffer_add(evb, &tag, sizeof(tag));
575 np_string(struct evbuffer *e, uint16_t len, const char *str)
580 evbuffer_add(e, &len, sizeof(len));
581 evbuffer_add(e, str, l);
585 np_qid(struct evbuffer *e, struct qid *qid)
590 path = htole64(qid->path);
591 vers = htole32(qid->vers);
593 evbuffer_add(e, &qid->type, sizeof(qid->type));
594 evbuffer_add(e, &vers, sizeof(vers));
595 evbuffer_add(e, &path, sizeof(path));
604 len = EVBUFFER_LENGTH(evb);
605 data = EVBUFFER_DATA(evb);
608 hexdump("outgoing packet", data, len);
610 client_send_listener(IMSG_BUF, data, len);
611 evbuffer_drain(evb, len);
615 np_version(uint16_t tag, uint32_t msize, const char *version)
621 msize = htole32(msize);
623 np_header(sizeof(msize) + sizeof(l) + l, Rversion, tag);
624 evbuffer_add(evb, &msize, sizeof(msize));
625 np_string(evb, l, version);
630 np_attach(uint16_t tag, struct qid *qid)
632 np_header(QIDSIZE, Rattach, tag);
638 np_clunk(uint16_t tag)
640 np_header(0, Rclunk, tag);
645 np_flush(uint16_t tag)
647 np_header(0, Rflush, tag);
652 np_walk(uint16_t tag, int nwqid, struct qid *wqid)
656 /* two bytes for the counter */
657 np_header(2 + QIDSIZE * nwqid, Rwalk, tag);
658 np_write16(evb, nwqid);
659 for (i = 0; i < nwqid; ++i)
660 np_qid(evb, wqid + i);
666 np_open(uint16_t tag, struct qid *qid, uint32_t iounit)
668 np_header(QIDSIZE + sizeof(iounit), Ropen, tag);
670 np_write32(evb, iounit);
675 np_create(uint16_t tag, struct qid *qid, uint32_t iounit)
677 np_header(QIDSIZE + sizeof(iounit), Rcreate, tag);
679 np_write32(evb, iounit);
684 np_read(uint16_t tag, uint32_t count, void *data)
686 if (sizeof(count) + count + HEADERSIZE >= msize) {
687 np_error(tag, "Rread would overflow");
691 np_header(sizeof(count) + count, Rread, tag);
692 np_write32(evb, count);
693 np_writebuf(evb, count, data);
698 np_write(uint16_t tag, uint32_t count)
700 np_header(sizeof(count), Rwrite, tag);
701 np_write32(evb, count);
706 np_stat(uint16_t tag, uint32_t count, void *data)
708 if (sizeof(count) + count + HEADERSIZE >= msize) {
709 np_error(tag, "Rstat would overflow");
713 np_header(count, Rstat, tag);
714 np_writebuf(evb, count, data);
719 np_remove(uint16_t tag)
721 np_header(0, Rremove, tag);
726 np_error(uint16_t tag, const char *errstr)
732 np_header(sizeof(l) + l, Rerror, tag);
733 np_string(evb, l, errstr);
738 np_errno(uint16_t tag)
741 char buf[NL_TEXTMAX] = {0};
745 strerror_r(errno, buf, sizeof(buf));
752 np_read8(const char *t, const char *f, uint8_t *dst, const uint8_t **src,
755 if (*len < sizeof(*dst)) {
756 log_warnx("%s: wanted %zu bytes for the %s field but only "
757 "%zu are available.", t, sizeof(*dst), f, *len);
761 memcpy(dst, *src, sizeof(*dst));
762 *src += sizeof(*dst);
763 *len -= sizeof(*dst);
769 np_read16(const char *t, const char *f, uint16_t *dst, const uint8_t **src,
772 if (*len < sizeof(*dst)) {
773 log_warnx("%s: wanted %zu bytes for the %s field but only "
774 "%zu are available.", t, sizeof(*dst), f, *len);
778 memcpy(dst, *src, sizeof(*dst));
779 *src += sizeof(*dst);
780 *len -= sizeof(*dst);
781 *dst = le16toh(*dst);
787 np_read32(const char *t, const char *f, uint32_t *dst, const uint8_t **src,
790 if (*len < sizeof(*dst)) {
791 log_warnx("%s: wanted %zu bytes for the %s field but only "
792 "%zu are available.", t, sizeof(*dst), f, *len);
796 memcpy(dst, *src, sizeof(*dst));
797 *src += sizeof(*dst);
798 *len -= sizeof(*dst);
799 *dst = le32toh(*dst);
805 np_read64(const char *t, const char *f, uint64_t *dst, const uint8_t **src,
808 if (*len < sizeof(*dst)) {
809 log_warnx("%s: wanted %zu bytes for the %s field but only "
810 "%zu are available.", t, sizeof(*dst), f, *len);
814 memcpy(dst, *src, sizeof(*dst));
815 *src += sizeof(*dst);
816 *len -= sizeof(*dst);
817 *dst = le64toh(*dst);
823 np_readstr(const char *t, const char *f, char *res, size_t reslen,
824 const uint8_t **src, size_t *len)
829 strlcpy(buf, f, sizeof(buf));
830 strlcat(buf, "-len", sizeof(buf));
832 if (!np_read16(t, buf, &sl, src, len))
836 log_warnx("%s: wanted %d bytes for the %s field but only "
837 "%zu are available.", t, sl, f, *len);
844 memcpy(res, *src, sl);
853 tversion(struct np_msg_header *hdr, const uint8_t *data, size_t len)
855 char *dot, version[32];
860 /* msize[4] version[s] */
861 if (!NPREAD32("msize", &msize, &data, &len))
864 switch (NPREADSTR("version", version, sizeof(version), &data, &len)) {
868 log_warnx("9P version string too long, truncated");
872 if ((dot = strchr(version, '.')) != NULL)
875 if (strcmp(version, VERSION9P) != 0 ||
879 /* version matched */
881 msize = MIN(msize, CLIENT_MSIZE);
882 client_send_listener(IMSG_MSIZE, &msize, sizeof(msize));
883 np_version(hdr->tag, msize, VERSION9P);
887 log_warnx("unknown 9P version string: \"%s\", want "VERSION9P,
889 np_version(hdr->tag, MSIZE9P, "unknown");
893 client_send_listener(IMSG_CLOSE, NULL, 0);
898 tattach(struct np_msg_header *hdr, const uint8_t *data, size_t len)
904 char aname[PATH_MAX];
906 /* fid[4] afid[4] uname[s] aname[s] */
908 if (!NPREAD32("fid", &fid, &data, &len) ||
909 !NPREAD32("afid", &afid, &data, &len))
912 /* read the uname but don't actually use it */
913 switch (NPREADSTR("uname", aname, sizeof(aname), &data, &len)) {
917 np_error(hdr->tag, "name too long");
921 switch (NPREADSTR("aname", aname, sizeof(aname), &data, &len)) {
925 np_error(hdr->tag, "name too long");
929 if (fid_by_id(fid) != NULL || afid != NOFID) {
930 np_error(hdr->tag, "invalid fid or afid");
934 if ((fd = open(aname, O_RDONLY|O_DIRECTORY)) == -1)
937 if ((dir = new_dir(fd)) == NULL)
940 log_debug("attached %s to %d", aname, fid);
942 if ((f = new_fid(dir, fid, aname, NULL)) == NULL) {
947 np_attach(hdr->tag, &f->qid);
952 log_warn("failed to attach %s", aname);
956 client_send_listener(IMSG_CLOSE, NULL, 0);
961 tclunk(struct np_msg_header *hdr, const uint8_t *data, size_t len)
967 if (!NPREAD32("fid", &fid, &data, &len)) {
968 client_send_listener(IMSG_CLOSE, NULL, 0);
973 if ((f = fid_by_id(fid)) == NULL) {
974 np_error(hdr->tag, "invalid fid");
983 tflush(struct np_msg_header *hdr, const uint8_t *data, size_t len)
988 * We're doing only synchronous I/O. Tflush is implemented
989 * only because it's illegal to reply with a Rerror.
993 if (len != sizeof(oldtag)) {
994 log_warnx("Tflush with the wrong size: got %zu want %zu",
995 len, sizeof(oldtag));
996 client_send_listener(IMSG_CLOSE, NULL, 0);
1005 twalk(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1009 struct qid wqid[MAXWELEM] = {0};
1011 uint32_t fid, newfid;
1013 int fd, oldfd, no, nwqid = 0;
1014 char wnam[PATH_MAX];
1016 if (!NPREAD32("fid", &fid, &data, &len) ||
1017 !NPREAD32("newfid", &newfid, &data, &len) ||
1018 !NPREAD16("nwname", &nwname, &data, &len))
1021 if (nwname > MAXWELEM) {
1022 log_warnx("Twalk: more than %d path elements: %d",
1027 if ((f = fid_by_id(fid)) == NULL) {
1028 np_error(hdr->tag, "invalid fid");
1033 np_error(hdr->tag, "fid already opened for I/O");
1039 else if ((nf = fid_by_id(newfid)) != NULL) {
1040 np_error(hdr->tag, "newfid already in use");
1045 /* special case: fid duplication */
1048 * TODO: should we forbid fids duplication when fid ==
1052 (nf = new_fid(f->dir, newfid, f->fpath, &f->qid)) == NULL)
1053 fatal("new_fid duplication");
1055 np_walk(hdr->tag, 0, NULL);
1059 if (!(f->qid.type & QTDIR)) {
1060 np_error(hdr->tag, "fid doesn't represent a directory");
1066 for (nwqid = 0; nwqid < nwname; nwqid++) {
1067 switch (NPREADSTR("wname", wnam, sizeof(wnam), &data, &len)) {
1071 np_error(hdr->tag, "wname too long");
1075 if (*wnam == '\0' ||
1076 strchr(wnam, '/') != NULL ||
1077 !strcmp(wnam, ".")) {
1082 if ((fd = openat(oldfd, wnam, O_RDONLY|O_DIRECTORY)) == -1 &&
1086 if ((fd == -1 && fstatat(oldfd, wnam, &sb, 0) == -1) ||
1087 (fd != -1 && fstat(fd, &sb) == -1))
1090 qid_update_from_sb(&wqid[nwqid], &sb);
1092 /* reached a file but we still have other components */
1093 if (fd == -1 && nwqid+1 < nwname)
1096 /* reached the end and found a file */
1097 if (fd == -1 && nwqid+1 == nwname)
1100 if (oldfd != f->dir->fd)
1106 * If fd is -1 we've reached a file, otherwise we've just
1107 * reached another directory. We must pay attention to what
1108 * file descriptor we use to create the dir, because if we've
1109 * reached a file and oldfd is f->dir->fd then we *must* share
1110 * the same dir (it was a walk of one path from a directory to a
1111 * file, otherwise fun is bound to happen as soon as the client
1112 * closes the fid for the directory but keeps the one for the
1115 if (fd == -1 && oldfd == f->dir->fd)
1118 dir = new_dir(oldfd);
1126 if ((nf = new_fid(dir, newfid, wnam, &wqid[nwqid-1])) == NULL)
1129 /* update the dir */
1130 dir_decref(nf->dir);
1131 nf->dir = dir_incref(dir);
1134 np_walk(hdr->tag, nwqid, wqid);
1138 if (oldfd != f->dir->fd)
1142 np_error(hdr->tag, strerror(no));
1144 np_walk(hdr->tag, nwqid, wqid);
1148 client_send_listener(IMSG_CLOSE, NULL, 0);
1153 npmode_to_unix(uint8_t mode, int *flags)
1155 switch (mode & 0x0F) {
1166 log_warnx("tried to open something with KOEXEC");
1174 if (mode & KORCLOSE)
1175 *flags |= O_CLOEXEC;
1181 topen(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1190 /* fid[4] mode[1] */
1191 if (!NPREAD32("fid", &fid, &data, &len) ||
1192 !NPREAD8("mode", &mode, &data, &len)) {
1193 client_send_listener(IMSG_CLOSE, NULL, 0);
1198 if ((f = fid_by_id(fid)) == NULL || f->fd != -1) {
1199 np_error(hdr->tag, "invalid fid");
1203 if (npmode_to_unix(mode, &f->iomode) == -1) {
1204 np_error(hdr->tag, "invalid mode");
1209 if (f->qid.type & QTDIR)
1212 if ((f->fd = openat(f->dir->fd, path, f->iomode)) == -1) {
1213 np_error(hdr->tag, strerror(errno));
1217 if (fstat(f->fd, &sb) == -1)
1220 if (S_ISDIR(sb.st_mode)) {
1221 if ((f->d = fdopendir(f->fd)) == NULL) {
1228 if ((f->evb = evbuffer_new()) == NULL) {
1238 qid_update_from_sb(&qid, &sb);
1239 np_open(hdr->tag, &qid, sb.st_blksize);
1243 tcreate(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1250 char name[PATH_MAX];
1252 /* fid[4] name[s] perm[4] mode[1] */
1253 if (!NPREAD32("fid", &fid, &data, &len))
1255 switch (NPREADSTR("name", name, sizeof(name), &data, &len)) {
1259 np_error(hdr->tag, "name too long");
1262 if (!NPREAD32("perm", &perm, &data, &len) ||
1263 !NPREAD8("mode", &mode, &data, &len))
1266 if (!strcmp(name, ".") || !strcmp(name, "..") ||
1267 strchr(name, '/') != NULL) {
1268 np_error(hdr->tag, "invalid name");
1272 if ((f = fid_by_id(fid)) == NULL || f->fd != -1) {
1273 np_error(hdr->tag, "invalid fid");
1277 if (!(f->qid.type & QTDIR)) {
1278 np_error(hdr->tag, "fid doesn't identify a directory");
1282 if (npmode_to_unix(mode, &f->iomode) == -1) {
1283 np_error(hdr->tag, "invalid mode");
1287 if (f->iomode & O_RDONLY) {
1288 np_error(hdr->tag, "can't create a read-only file");
1292 /* TODO: parse the mode */
1294 if (perm & 0x80000000) {
1295 /* create a directory */
1296 f->fd = mkdirat(f->dir->fd, name, 0755);
1299 f->fd = openat(f->dir->fd, name, f->iomode | O_CREAT | O_TRUNC,
1308 if (fstat(f->fd, &sb) == -1)
1311 if (S_ISDIR(sb.st_mode)) {
1312 if ((f->d = fdopendir(f->fd)) == NULL) {
1319 if ((f->evb = evbuffer_new()) == NULL) {
1329 qid_update_from_sb(&qid, &sb);
1330 np_create(hdr->tag, &qid, sb.st_blksize);
1335 client_send_listener(IMSG_CLOSE, NULL, 0);
1340 serialize_stat(const char *fname, struct stat *sb, struct evbuffer *evb)
1343 const char *uid, *gid, *muid;
1345 uint16_t namlen, uidlen, gidlen, ulen;
1347 qid_update_from_sb(&qid, sb);
1349 /* TODO: fill these fields */
1354 namlen = strlen(fname);
1355 uidlen = strlen(uid);
1356 gidlen = strlen(gid);
1357 ulen = strlen(muid);
1359 tot = NPSTATSIZ(namlen, uidlen, gidlen, ulen);
1360 if (tot > UINT32_MAX) {
1361 log_warnx("stat info for dir entry %s would overflow",
1366 np_write16(evb, tot); /* size[2] */
1367 np_write16(evb, sb->st_rdev); /* type[2] */
1368 np_write32(evb, sb->st_dev); /* dev[4] */
1369 np_qid(evb, &qid); /* qid[13] */
1371 /* XXX: translate? */
1372 np_write32(evb, sb->st_mode); /* mode[4] */
1374 np_write32(evb, sb->st_atim.tv_sec); /* atime[4] */
1375 np_write32(evb, sb->st_mtim.tv_sec); /* mtime[4] */
1376 np_write64(evb, sb->st_size); /* length[8] */
1377 np_string(evb, namlen, fname); /* name[s] */
1378 np_string(evb, uidlen, uid); /* uid[s] */
1379 np_string(evb, gidlen, gid); /* gid[s] */
1380 np_string(evb, ulen, muid); /* muid[s] */
1384 tread(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1390 uint32_t fid, count;
1393 /* fid[4] offset[8] count[4] */
1394 if (!NPREAD32("fid", &fid, &data, &len) ||
1395 !NPREAD64("offset", &off, &data, &len) ||
1396 !NPREAD32("count", &count, &data, &len)) {
1397 client_send_listener(IMSG_CLOSE, NULL, 0);
1402 if ((f = fid_by_id(fid)) == NULL || f->fd == -1) {
1403 np_error(hdr->tag, "invalid fid");
1407 if (TYPE_OVERFLOW(off_t, off)) {
1408 log_warnx("unexpected off_t size");
1409 np_error(hdr->tag, "invalid offset");
1415 howmuch = MIN(sizeof(buf), count);
1416 r = pread(f->fd, buf, howmuch, (off_t)off);
1420 np_read(hdr->tag, r, buf);
1422 if (off == 0 && f->offset != 0) {
1425 evbuffer_drain(f->evb, EVBUFFER_LENGTH(f->evb));
1428 if (off != f->offset) {
1429 np_error(hdr->tag, "can't seek in directories");
1433 while (EVBUFFER_LENGTH(f->evb) < count) {
1437 if ((d = readdir(f->d)) == NULL)
1439 if (fstatat(f->fd, d->d_name, &sb, 0) == -1) {
1443 serialize_stat(d->d_name, &sb, f->evb);
1446 count = MIN(count, EVBUFFER_LENGTH(f->evb));
1447 np_read(hdr->tag, count, EVBUFFER_DATA(f->evb));
1448 evbuffer_drain(f->evb, count);
1455 twrite(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1460 uint32_t fid, count;
1462 /* fid[4] offset[8] count[4] data[count] */
1463 if (!NPREAD32("fid", &fid, &data, &len) ||
1464 !NPREAD64("off", &off, &data, &len) ||
1465 !NPREAD32("count", &count, &data, &len) ||
1467 client_send_listener(IMSG_CLOSE, NULL, 0);
1472 if ((f = fid_by_id(fid)) == NULL || f->fd == -1) {
1473 np_error(hdr->tag, "invalid fid");
1477 if (!(f->iomode & O_WRONLY) &&
1478 !(f->iomode & O_RDWR)) {
1479 np_error(hdr->tag, "fid not opened for writing");
1483 if (TYPE_OVERFLOW(off_t, off)) {
1484 log_warnx("unexpected off_t size");
1485 np_error(hdr->tag, "invalid offset");
1489 if ((r = pwrite(f->fd, data, len, off)) == -1)
1492 np_write(hdr->tag, r);
1496 tstat(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1498 struct evbuffer *evb;
1505 if (!NPREAD32("fid", &fid, &data, &len)) {
1506 client_send_listener(IMSG_CLOSE, NULL, 0);
1512 * plan9' stat(9P) is not clear on whether the stat is allowed
1513 * on opened fids or not. We're allowing stat regardless of the
1514 * status of the fid.
1517 if ((f = fid_by_id(fid)) == NULL) {
1518 np_error(hdr->tag, "invalid fid");
1522 if ((evb = evbuffer_new()) == NULL)
1523 fatal("evbuffer_new");
1526 r = fstat(f->fd, &sb);
1527 else if (f->qid.type & QTDIR)
1528 r = fstat(f->dir->fd, &sb);
1530 r = fstatat(f->dir->fd, f->fpath, &sb, 0);
1538 serialize_stat(f->fpath, &sb, evb);
1539 np_stat(hdr->tag, EVBUFFER_LENGTH(evb), EVBUFFER_DATA(evb));
1544 tremove(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1549 char dirpath[PATH_MAX + 3];
1552 if (!NPREAD32("fid", &fid, &data, &len)) {
1553 client_send_listener(IMSG_CLOSE, NULL, 0);
1558 if ((f = fid_by_id(fid)) == NULL) {
1559 np_error(hdr->tag, "invalid fid");
1563 if (f->qid.type & QTDIR) { /* directory */
1564 strlcpy(dirpath, "../", sizeof(dirpath));
1565 strlcat(dirpath, f->fpath, sizeof(dirpath));
1566 r = unlinkat(f->dir->fd, dirpath, AT_REMOVEDIR);
1568 r = unlinkat(f->dir->fd, f->fpath, 0);
1573 np_remove(hdr->tag);
1579 handle_message(struct imsg *imsg, size_t len)
1583 void (*fn)(struct np_msg_header *, const uint8_t *, size_t);
1585 {Tversion, tversion},
1597 struct np_msg_header hdr;
1602 hexdump("incoming packet", imsg->data, len);
1605 parse_message(imsg->data, len, &hdr, &data);
1608 log_debug("got request: len=%d type=%d[%s] tag=%d",
1609 hdr.len, hdr.type, pp_msg_type(hdr.type), hdr.tag);
1611 if (!handshaked && hdr.type != Tversion) {
1612 client_send_listener(IMSG_CLOSE, NULL, 0);
1617 for (i = 0; i < sizeof(msgs)/sizeof(msgs[0]); ++i) {
1618 if (msgs[i].type != hdr.type)
1621 msgs[i].fn(&hdr, data, len);
1625 np_error(hdr.tag, "Not supported.");