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))
55 STAILQ_HEAD(dirhead, dir) dirs;
59 STAILQ_ENTRY(dir) entries;
62 STAILQ_HEAD(fidhead, fid) fids;
69 * the flags passed to open(2). O_CLOEXEC means ORCLOSE, that
70 * is to unlink the file upon Tclunk.
75 * if fd is not -1 this fid was opened, fd represents its
76 * file descriptor and iomode the flags passed to open(2).
83 * expected offset for Tread against a directory.
89 STAILQ_ENTRY(fid) entries;
92 static struct imsgev *iev_listener;
93 static struct evbuffer *evb;
94 static uint32_t peerid;
96 static int handshaked;
99 static __dead void client_shutdown(void);
100 static void client_sig_handler(int, short, void *);
101 static void client_dispatch_listener(int, short, void *);
102 static void client_privdrop(const char *, const char *);
104 static int client_send_listener(int, const void *, uint16_t);
106 static void qid_update_from_sb(struct qid *, struct stat *);
108 static struct dir *new_dir(int);
109 static struct dir *dir_incref(struct dir *);
110 static void dir_decref(struct dir *);
112 static struct fid *new_fid(struct dir *, uint32_t, const char *, struct qid *);
113 static struct fid *fid_by_id(uint32_t);
114 static void free_fid(struct fid *);
116 static void parse_message(const uint8_t *, size_t,
117 struct np_msg_header *, uint8_t **);
119 static void np_write16(struct evbuffer *, uint16_t);
120 static void np_write32(struct evbuffer *, uint32_t);
121 static void np_write64(struct evbuffer *, uint64_t);
122 static void np_header(uint32_t, uint8_t, uint16_t);
123 static void np_string(struct evbuffer *, uint16_t, const char *);
124 static void np_qid(struct evbuffer *, struct qid *);
125 static void do_send(void);
127 static void np_version(uint16_t, uint32_t, const char *);
128 static void np_attach(uint16_t, struct qid *);
129 static void np_clunk(uint16_t);
130 static void np_flush(uint16_t);
131 static void np_walk(uint16_t, int, struct qid *);
132 static void np_open(uint16_t, struct qid *, uint32_t);
133 static void np_create(uint16_t, struct qid *, uint32_t);
134 static void np_read(uint16_t, uint32_t, void *);
135 static void np_write(uint16_t, uint32_t);
136 static void np_stat(uint16_t, uint32_t, void *);
137 static void np_remove(uint16_t);
138 static void np_error(uint16_t, const char *);
139 static void np_errno(uint16_t);
141 static int np_read8(const char *, const char *, uint8_t *,
142 const uint8_t **, size_t *);
143 static int np_read16(const char *, const char *, uint16_t *,
144 const uint8_t **, size_t *);
145 static int np_read32(const char *, const char *, uint32_t *,
146 const uint8_t **, size_t *);
147 static int np_read64(const char *, const char *, uint64_t *,
148 const uint8_t **, size_t *);
150 #define READSTRERR -1
151 #define READSTRTRUNC -2
152 static int np_readstr(const char *, const char *, char *, size_t,
153 const uint8_t **, size_t *);
155 #define NPREAD8(f, dst, src, len) np_read8(__func__, f, dst, src, len)
156 #define NPREAD16(f, dst, src, len) np_read16(__func__, f, dst, src, len)
157 #define NPREAD32(f, dst, src, len) np_read32(__func__, f, dst, src, len)
158 #define NPREAD64(f, dst, src, len) np_read64(__func__, f, dst, src, len)
160 #define NPREADSTR(f, b, bl, src, len) np_readstr(__func__, f, b, bl, src, len)
162 static void tversion(struct np_msg_header *, const uint8_t *, size_t);
163 static void tattach(struct np_msg_header *, const uint8_t *, size_t);
164 static void tclunk(struct np_msg_header *, const uint8_t *, size_t);
165 static void tflush(struct np_msg_header *, const uint8_t *, size_t);
166 static void twalk(struct np_msg_header *, const uint8_t *, size_t);
167 static void topen(struct np_msg_header *, const uint8_t *, size_t);
168 static void tcreate(struct np_msg_header *, const uint8_t *, size_t);
169 static void tread(struct np_msg_header *, const uint8_t *, size_t);
170 static void twrite(struct np_msg_header *, const uint8_t *, size_t);
171 static void tstat(struct np_msg_header *, const uint8_t *, size_t);
172 static void tremove(struct np_msg_header *, const uint8_t *, size_t);
173 static void handle_message(struct imsg *, size_t);
176 client(int debug, int verbose)
178 struct event ev_sigint, ev_sigterm;
180 log_init(debug, LOG_DAEMON);
181 log_setverbose(verbose);
183 setproctitle("client");
184 log_procinit("client");
186 log_debug("warming up");
190 /* Setup signal handlers */
191 signal_set(&ev_sigint, SIGINT, client_sig_handler, NULL);
192 signal_set(&ev_sigterm, SIGTERM, client_sig_handler, NULL);
194 signal_add(&ev_sigint, NULL);
195 signal_add(&ev_sigterm, NULL);
197 signal(SIGPIPE, SIG_IGN);
198 signal(SIGHUP, SIG_IGN);
200 /* Setup pipe and event handler to the listener process */
201 if ((iev_listener = malloc(sizeof(*iev_listener))) == NULL)
204 imsg_init(&iev_listener->ibuf, 3);
205 iev_listener->handler = client_dispatch_listener;
207 /* Setup event handlers. */
208 iev_listener->events = EV_READ;
209 event_set(&iev_listener->ev, iev_listener->ibuf.fd,
210 iev_listener->events, iev_listener->handler, iev_listener);
211 event_add(&iev_listener->ev, NULL);
218 client_shutdown(void)
223 msgbuf_clear(&iev_listener->ibuf.w);
224 close(iev_listener->ibuf.fd);
228 log_debug("client exiting");
233 client_sig_handler(int sig, short event, void *d)
236 * Normal signal handler rules don't apply because libevent
245 fatalx("unexpected signal %d", sig);
254 client_dispatch_listener(int fd, short event, void *d)
256 static int auth = AUTH_NONE;
257 static char username[64] = {0};
258 static char dir[PATH_MAX] = {0};
260 struct imsgev *iev = d;
261 struct imsgbuf *ibuf;
267 if (event & EV_READ) {
268 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
269 fatal("imsg_read error");
270 if (n == 0) /* Connection closed */
273 if (event & EV_WRITE) {
274 if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
275 fatal("msgbuf_write");
276 if (n == 0) /* Connection closed */
281 if ((n = imsg_get(ibuf, &imsg)) == -1)
282 fatal("%s: imsg_get error", __func__);
283 if (n == 0) /* No more messages. */
286 switch (imsg.hdr.type) {
288 peerid = imsg.hdr.peerid;
290 fatalx("%s: IMSG_AUTH already done", __func__);
292 ((char *)imsg.data)[IMSG_DATA_SIZE(imsg)-1] = '\0';
293 strlcpy(username, imsg.data, sizeof(username));
296 if (auth != AUTH_USER)
297 fatalx("%s: IMSG_AUTH_DIR not after IMSG_AUTH",
300 ((char *)imsg.data)[IMSG_DATA_SIZE(imsg)-1] = '\0';
301 strlcpy(dir, imsg.data, sizeof(dir));
302 client_privdrop(username, dir);
303 memset(username, 0, sizeof(username));
304 memset(dir, 0, sizeof(username));
309 fatalx("%s: can't handle messages before"
310 " doing the auth", __func__);
311 handle_message(&imsg, IMSG_DATA_SIZE(imsg));
314 log_debug("closing");
318 log_debug("%s: unexpected imsg %d",
319 __func__, imsg.hdr.type);
328 /* This pipe is dead. Remove its event handler. */
330 log_debug("pipe closed, shutting down...");
331 event_loopexit(NULL);
336 client_privdrop(const char *username, const char *dir)
340 setproctitle("client %s", username);
342 if ((pw = getpwnam(username)) == NULL)
343 fatalx("getpwnam(%s) failed", username);
345 if (chroot(dir) == -1)
347 if (chdir("/") == -1)
348 fatal("chdir(\"/\")");
350 if (setgroups(1, &pw->pw_gid) ||
351 setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
352 setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
353 fatal("can't drop privileges");
356 log_debug("client ready; user=%s dir=%s", username, dir);
358 if ((evb = evbuffer_new()) == NULL)
359 fatal("evbuffer_new");
363 client_send_listener(int type, const void *data, uint16_t len)
367 if ((ret = imsg_compose(&iev_listener->ibuf, type, peerid, 0, -1,
369 imsg_event_add(iev_listener);
374 /* set qid fields from sb */
376 qid_update_from_sb(struct qid *qid, struct stat *sb)
378 qid->path = sb->st_ino;
381 * Theoretically (and hopefully!) this should be a 64 bit
382 * number. Unfortunately, 9P uses 32 bit timestamps.
384 qid->vers = sb->st_mtim.tv_sec;
386 if (S_ISREG(sb->st_mode))
388 else if (S_ISDIR(sb->st_mode))
390 else if (S_ISLNK(sb->st_mode))
391 qid->type = QTSYMLINK;
394 /* creates a qid given a fd */
400 if ((dir = calloc(1, sizeof(*dir))) == NULL)
404 STAILQ_INSERT_HEAD(&dirs, dir, entries);
409 dir_incref(struct dir *dir)
416 dir_decref(struct dir *dir)
418 if (--dir->refcount > 0)
421 STAILQ_REMOVE(&dirs, dir, dir, entries);
428 new_fid(struct dir *dir, uint32_t fid, const char *path, struct qid *qid)
435 if (fstatat(dir->fd, path, &sb, 0)) {
436 log_warn("fstatat(%s)", path);
439 qid_update_from_sb(&q, &sb);
443 if ((f = calloc(1, sizeof(*f))) == NULL)
446 f->dir = dir_incref(dir);
450 strlcpy(f->fpath, path, sizeof(f->fpath));
452 memcpy(&f->qid, qid, sizeof(f->qid));
454 STAILQ_INSERT_HEAD(&fids, f, entries);
460 fid_by_id(uint32_t fid)
464 STAILQ_FOREACH(f, &fids, entries) {
473 free_fid(struct fid *f)
484 fatal("can't close fid %d", f->fid);
487 evbuffer_free(f->evb);
489 /* try to honour ORCLOSE if requested */
490 if (f->iomode & O_CLOEXEC)
491 unlinkat(f->dir->fd, f->fpath, 0);
496 STAILQ_REMOVE(&fids, f, fid, entries);
501 parse_message(const uint8_t *data, size_t len, struct np_msg_header *hdr,
506 if (!NPREAD32("len", &hdr->len, &data, &len) ||
507 !NPREAD8("type", &hdr->type, &data, &len) ||
508 !NPREAD16("tag", &hdr->tag, &data, &len))
511 if (olen != hdr->len)
514 if (hdr->type < Tversion ||
516 hdr->type == Terror ||
517 (hdr->type & 0x1) != 0) /* cannot recv a R* */
520 hdr->tag = le32toh(hdr->tag);
522 *cnt = (uint8_t *)data;
526 /* TODO: send a proper message to terminate the connection. */
527 fatalx("got invalid message");
531 np_write16(struct evbuffer *e, uint16_t x)
534 evbuffer_add(e, &x, sizeof(x));
538 np_write32(struct evbuffer *e, uint32_t x)
541 evbuffer_add(e, &x, sizeof(x));
545 np_write64(struct evbuffer *e, uint64_t x)
548 evbuffer_add(e, &x, sizeof(x));
552 np_writebuf(struct evbuffer *e, size_t len, void *data)
554 evbuffer_add(e, data, len);
558 np_header(uint32_t len, uint8_t type, uint16_t tag)
565 evbuffer_add(evb, &len, sizeof(len));
566 evbuffer_add(evb, &type, sizeof(type));
567 evbuffer_add(evb, &tag, sizeof(tag));
571 np_string(struct evbuffer *e, uint16_t len, const char *str)
576 evbuffer_add(e, &len, sizeof(len));
577 evbuffer_add(e, str, l);
581 np_qid(struct evbuffer *e, struct qid *qid)
586 path = htole64(qid->path);
587 vers = htole32(qid->vers);
589 evbuffer_add(e, &qid->type, sizeof(qid->type));
590 evbuffer_add(e, &vers, sizeof(vers));
591 evbuffer_add(e, &path, sizeof(path));
600 len = EVBUFFER_LENGTH(evb);
601 data = EVBUFFER_DATA(evb);
604 hexdump("outgoing packet", data, len);
606 client_send_listener(IMSG_BUF, data, len);
607 evbuffer_drain(evb, len);
611 np_version(uint16_t tag, uint32_t msize, const char *version)
617 msize = htole32(msize);
619 np_header(sizeof(msize) + sizeof(l) + l, Rversion, tag);
620 evbuffer_add(evb, &msize, sizeof(msize));
621 np_string(evb, l, version);
626 np_attach(uint16_t tag, struct qid *qid)
628 np_header(QIDSIZE, Rattach, tag);
634 np_clunk(uint16_t tag)
636 np_header(0, Rclunk, tag);
641 np_flush(uint16_t tag)
643 np_header(0, Rflush, tag);
648 np_walk(uint16_t tag, int nwqid, struct qid *wqid)
652 /* two bytes for the counter */
653 np_header(2 + QIDSIZE * nwqid, Rwalk, tag);
654 np_write16(evb, nwqid);
655 for (i = 0; i < nwqid; ++i)
656 np_qid(evb, wqid + i);
662 np_open(uint16_t tag, struct qid *qid, uint32_t iounit)
664 np_header(QIDSIZE + sizeof(iounit), Ropen, tag);
666 np_write32(evb, iounit);
671 np_create(uint16_t tag, struct qid *qid, uint32_t iounit)
673 np_header(QIDSIZE + sizeof(iounit), Rcreate, tag);
675 np_write32(evb, iounit);
680 np_read(uint16_t tag, uint32_t count, void *data)
682 np_header(sizeof(count) + count, Rread, tag);
683 np_write32(evb, count);
684 np_writebuf(evb, count, data);
689 np_write(uint16_t tag, uint32_t count)
691 np_header(sizeof(count), Rwrite, tag);
692 np_write32(evb, count);
697 np_stat(uint16_t tag, uint32_t count, void *data)
699 np_header(count, Rstat, tag);
700 np_writebuf(evb, count, data);
705 np_remove(uint16_t tag)
707 np_header(0, Rremove, tag);
712 np_error(uint16_t tag, const char *errstr)
718 np_header(sizeof(l) + l, Rerror, tag);
719 np_string(evb, l, errstr);
724 np_errno(uint16_t tag)
731 strerror_r(errno, buf, sizeof(buf));
738 np_read8(const char *t, const char *f, uint8_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);
755 np_read16(const char *t, const char *f, uint16_t *dst, const uint8_t **src,
758 if (*len < sizeof(*dst)) {
759 log_warnx("%s: wanted %zu bytes for the %s field but only "
760 "%zu are available.", t, sizeof(*dst), f, *len);
764 memcpy(dst, *src, sizeof(*dst));
765 *src += sizeof(*dst);
766 *len -= sizeof(*dst);
767 *dst = le16toh(*dst);
773 np_read32(const char *t, const char *f, uint32_t *dst, const uint8_t **src,
776 if (*len < sizeof(*dst)) {
777 log_warnx("%s: wanted %zu bytes for the %s field but only "
778 "%zu are available.", t, sizeof(*dst), f, *len);
782 memcpy(dst, *src, sizeof(*dst));
783 *src += sizeof(*dst);
784 *len -= sizeof(*dst);
785 *dst = le32toh(*dst);
791 np_read64(const char *t, const char *f, uint64_t *dst, const uint8_t **src,
794 if (*len < sizeof(*dst)) {
795 log_warnx("%s: wanted %zu bytes for the %s field but only "
796 "%zu are available.", t, sizeof(*dst), f, *len);
800 memcpy(dst, *src, sizeof(*dst));
801 *src += sizeof(*dst);
802 *len -= sizeof(*dst);
803 *dst = le64toh(*dst);
809 np_readstr(const char *t, const char *f, char *res, size_t reslen,
810 const uint8_t **src, size_t *len)
815 strlcpy(buf, f, sizeof(buf));
816 strlcat(buf, "-len", sizeof(buf));
818 if (!np_read16(t, buf, &sl, src, len))
822 log_warnx("%s: wanted %d bytes for the %s field but only "
823 "%zu are available.", t, sl, f, *len);
830 memcpy(res, *src, sl);
839 tversion(struct np_msg_header *hdr, const uint8_t *data, size_t len)
841 char *dot, version[32];
846 /* msize[4] version[s] */
847 if (!NPREAD32("msize", &msize, &data, &len))
850 switch (NPREADSTR("version", version, sizeof(version), &data, &len)) {
854 log_warnx("9P version string too long, truncated");
858 if ((dot = strchr(version, '.')) != NULL)
861 if (strcmp(version, VERSION9P) != 0 ||
865 /* version matched */
867 msize = MIN(msize, CLIENT_MSIZE);
868 client_send_listener(IMSG_MSIZE, &msize, sizeof(msize));
869 np_version(hdr->tag, msize, VERSION9P);
873 log_warnx("unknown 9P version string: \"%s\", want "VERSION9P,
875 np_version(hdr->tag, MSIZE9P, "unknown");
879 client_send_listener(IMSG_CLOSE, NULL, 0);
884 tattach(struct np_msg_header *hdr, const uint8_t *data, size_t len)
890 char aname[PATH_MAX];
892 /* fid[4] afid[4] uname[s] aname[s] */
894 if (!NPREAD32("fid", &fid, &data, &len) ||
895 !NPREAD32("afid", &afid, &data, &len))
898 /* read the uname but don't actually use it */
899 switch (NPREADSTR("uname", aname, sizeof(aname), &data, &len)) {
903 np_error(hdr->tag, "name too long");
907 switch (NPREADSTR("aname", aname, sizeof(aname), &data, &len)) {
911 np_error(hdr->tag, "name too long");
915 if (fid_by_id(fid) != NULL || afid != NOFID) {
916 np_error(hdr->tag, "invalid fid or afid");
920 if ((fd = open(aname, O_RDONLY|O_DIRECTORY)) == -1)
923 if ((dir = new_dir(fd)) == NULL)
926 log_debug("attached %s to %d", aname, fid);
928 if ((f = new_fid(dir, fid, aname, NULL)) == NULL) {
933 np_attach(hdr->tag, &f->qid);
938 log_warn("failed to attach %s", aname);
942 client_send_listener(IMSG_CLOSE, NULL, 0);
947 tclunk(struct np_msg_header *hdr, const uint8_t *data, size_t len)
953 if (!NPREAD32("fid", &fid, &data, &len)) {
954 client_send_listener(IMSG_CLOSE, NULL, 0);
959 if ((f = fid_by_id(fid)) == NULL) {
960 np_error(hdr->tag, "invalid fid");
969 tflush(struct np_msg_header *hdr, const uint8_t *data, size_t len)
974 * We're doing only synchronous I/O. Tflush is implemented
975 * only because it's illegal to reply with a Rerror.
979 if (len != sizeof(oldtag)) {
980 log_warnx("Tflush with the wrong size: got %zu want %zu",
981 len, sizeof(oldtag));
982 client_send_listener(IMSG_CLOSE, NULL, 0);
991 twalk(struct np_msg_header *hdr, const uint8_t *data, size_t len)
995 struct qid wqid[MAXWELEM] = {0};
997 uint32_t fid, newfid;
999 int fd, oldfd, no, nwqid = 0;
1000 char wnam[PATH_MAX];
1002 if (!NPREAD32("fid", &fid, &data, &len) ||
1003 !NPREAD32("newfid", &newfid, &data, &len) ||
1004 !NPREAD16("nwname", &nwname, &data, &len))
1007 if (nwname > MAXWELEM) {
1008 log_warnx("Twalk: more than %d path elements: %d",
1013 if ((f = fid_by_id(fid)) == NULL) {
1014 np_error(hdr->tag, "invalid fid");
1019 np_error(hdr->tag, "fid already opened for I/O");
1025 else if ((nf = fid_by_id(newfid)) != NULL) {
1026 np_error(hdr->tag, "newfid already in use");
1031 /* special case: fid duplication */
1034 * TODO: should we forbid fids duplication when fid ==
1038 (nf = new_fid(f->dir, newfid, f->fpath, &f->qid)) == NULL)
1039 fatal("new_fid duplication");
1041 np_walk(hdr->tag, 0, NULL);
1045 if (!(f->qid.type & QTDIR)) {
1046 np_error(hdr->tag, "fid doesn't represent a directory");
1052 for (nwqid = 0; nwqid < nwname; nwqid++) {
1053 switch (NPREADSTR("wname", wnam, sizeof(wnam), &data, &len)) {
1057 np_error(hdr->tag, "wname too long");
1061 if (*wnam == '\0' ||
1062 strchr(wnam, '/') != NULL ||
1063 !strcmp(wnam, ".")) {
1068 if ((fd = openat(oldfd, wnam, O_RDONLY|O_DIRECTORY)) == -1 &&
1072 if ((fd == -1 && fstatat(oldfd, wnam, &sb, 0) == -1) ||
1073 (fd != -1 && fstat(fd, &sb) == -1))
1076 qid_update_from_sb(&wqid[nwqid], &sb);
1078 /* reached a file but we still have other components */
1079 if (fd == -1 && nwqid+1 < nwname)
1082 /* reached the end and found a file */
1083 if (fd == -1 && nwqid+1 == nwname)
1086 if (oldfd != f->dir->fd)
1092 * If fd is -1 we've reached a file, otherwise we've just
1093 * reached another directory. We must pay attention to what
1094 * file descriptor we use to create the dir, because if we've
1095 * reached a file and oldfd is f->dir->fd then we *must* share
1096 * the same dir (it was a walk of one path from a directory to a
1097 * file, otherwise fun is bound to happen as soon as the client
1098 * closes the fid for the directory but keeps the one for the
1101 if (fd == -1 && oldfd == f->dir->fd)
1104 dir = new_dir(oldfd);
1112 if ((nf = new_fid(dir, newfid, wnam, &wqid[nwqid-1])) == NULL)
1115 /* update the dir */
1116 dir_decref(nf->dir);
1117 nf->dir = dir_incref(dir);
1120 np_walk(hdr->tag, nwqid, wqid);
1124 if (oldfd != f->dir->fd)
1128 np_error(hdr->tag, strerror(no));
1130 np_walk(hdr->tag, nwqid, wqid);
1134 client_send_listener(IMSG_CLOSE, NULL, 0);
1139 npmode_to_unix(uint8_t mode, int *flags)
1141 switch (mode & 0x0F) {
1152 log_warnx("tried to open something with KOEXEC");
1160 if (mode & KORCLOSE)
1161 *flags |= O_CLOEXEC;
1167 topen(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1176 /* fid[4] mode[1] */
1177 if (!NPREAD32("fid", &fid, &data, &len) ||
1178 !NPREAD8("mode", &mode, &data, &len)) {
1179 client_send_listener(IMSG_CLOSE, NULL, 0);
1184 if ((f = fid_by_id(fid)) == NULL || f->fd != -1) {
1185 np_error(hdr->tag, "invalid fid");
1189 if (npmode_to_unix(mode, &f->iomode) == -1) {
1190 np_error(hdr->tag, "invalid mode");
1195 if (f->qid.type & QTDIR)
1198 if ((f->fd = openat(f->dir->fd, path, f->iomode)) == -1) {
1199 np_error(hdr->tag, strerror(errno));
1203 if (fstat(f->fd, &sb) == -1)
1206 if (S_ISDIR(sb.st_mode)) {
1207 if ((f->d = fdopendir(f->fd)) == NULL) {
1214 if ((f->evb = evbuffer_new()) == NULL) {
1224 qid_update_from_sb(&qid, &sb);
1225 np_open(hdr->tag, &qid, sb.st_blksize);
1229 tcreate(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1236 char name[PATH_MAX];
1238 /* fid[4] name[s] perm[4] mode[1] */
1239 if (!NPREAD32("fid", &fid, &data, &len))
1241 switch (NPREADSTR("name", name, sizeof(name), &data, &len)) {
1245 np_error(hdr->tag, "name too long");
1248 if (!NPREAD32("perm", &perm, &data, &len) ||
1249 !NPREAD8("mode", &mode, &data, &len))
1252 if (!strcmp(name, ".") || !strcmp(name, "..") ||
1253 strchr(name, '/') != NULL) {
1254 np_error(hdr->tag, "invalid name");
1258 if ((f = fid_by_id(fid)) == NULL || f->fd != -1) {
1259 np_error(hdr->tag, "invalid fid");
1263 if (!(f->qid.type & QTDIR)) {
1264 np_error(hdr->tag, "fid doesn't identify a directory");
1268 if (npmode_to_unix(mode, &f->iomode) == -1) {
1269 np_error(hdr->tag, "invalid mode");
1273 if (f->iomode & O_RDONLY) {
1274 np_error(hdr->tag, "can't create a read-only file");
1278 /* TODO: parse the mode */
1280 if (perm & 0x80000000) {
1281 /* create a directory */
1282 f->fd = mkdirat(f->dir->fd, name, 0755);
1285 f->fd = openat(f->dir->fd, name, f->iomode | O_CREAT | O_TRUNC,
1294 if (fstat(f->fd, &sb) == -1)
1297 if (S_ISDIR(sb.st_mode)) {
1298 if ((f->d = fdopendir(f->fd)) == NULL) {
1305 if ((f->evb = evbuffer_new()) == NULL) {
1315 qid_update_from_sb(&qid, &sb);
1316 np_create(hdr->tag, &qid, sb.st_blksize);
1321 client_send_listener(IMSG_CLOSE, NULL, 0);
1326 serialize_stat(const char *fname, struct stat *sb, struct evbuffer *evb)
1329 const char *uid, *gid, *muid;
1331 uint16_t namlen, uidlen, gidlen, ulen;
1333 qid_update_from_sb(&qid, sb);
1335 /* TODO: fill these fields */
1340 namlen = strlen(fname);
1341 uidlen = strlen(uid);
1342 gidlen = strlen(gid);
1343 ulen = strlen(muid);
1345 tot = NPSTATSIZ(namlen, uidlen, gidlen, ulen);
1346 if (tot > UINT32_MAX) {
1347 log_warnx("stat info for dir entry %s would overflow",
1352 np_write16(evb, tot); /* size[2] */
1353 np_write16(evb, sb->st_rdev); /* type[2] */
1354 np_write32(evb, sb->st_dev); /* dev[4] */
1355 np_qid(evb, &qid); /* qid[13] */
1357 /* XXX: translate? */
1358 np_write32(evb, sb->st_mode); /* mode[4] */
1360 np_write32(evb, sb->st_atim.tv_sec); /* atime[4] */
1361 np_write32(evb, sb->st_mtim.tv_sec); /* mtime[4] */
1362 np_write64(evb, sb->st_size); /* length[8] */
1363 np_string(evb, namlen, fname); /* name[s] */
1364 np_string(evb, uidlen, uid); /* uid[s] */
1365 np_string(evb, gidlen, gid); /* gid[s] */
1366 np_string(evb, ulen, muid); /* muid[s] */
1370 tread(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1375 uint32_t fid, count;
1378 /* fid[4] offset[8] count[4] */
1379 if (!NPREAD32("fid", &fid, &data, &len) ||
1380 !NPREAD64("offset", &off, &data, &len) ||
1381 !NPREAD32("count", &count, &data, &len)) {
1382 client_send_listener(IMSG_CLOSE, NULL, 0);
1387 if ((f = fid_by_id(fid)) == NULL || f->fd == -1) {
1388 np_error(hdr->tag, "invalid fid");
1392 if (TYPE_OVERFLOW(off_t, off)) {
1393 log_warnx("unexpected off_t size");
1394 np_error(hdr->tag, "invalid offset");
1400 r = pread(f->fd, buf, sizeof(buf), (off_t)off);
1404 np_read(hdr->tag, r, buf);
1406 if (off == 0 && f->offset != 0) {
1409 evbuffer_drain(f->evb, EVBUFFER_LENGTH(f->evb));
1412 if (off != f->offset) {
1413 np_error(hdr->tag, "can't seek in directories");
1417 while (EVBUFFER_LENGTH(f->evb) < count) {
1421 if ((d = readdir(f->d)) == NULL)
1423 if (fstatat(f->fd, d->d_name, &sb, 0) == -1) {
1427 serialize_stat(d->d_name, &sb, f->evb);
1430 count = MIN(count, EVBUFFER_LENGTH(f->evb));
1431 np_read(hdr->tag, count, EVBUFFER_DATA(f->evb));
1432 evbuffer_drain(f->evb, count);
1439 twrite(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1444 uint32_t fid, count;
1446 /* fid[4] offset[8] count[4] data[count] */
1447 if (!NPREAD32("fid", &fid, &data, &len) ||
1448 !NPREAD64("off", &off, &data, &len) ||
1449 !NPREAD32("count", &count, &data, &len) ||
1451 client_send_listener(IMSG_CLOSE, NULL, 0);
1456 if ((f = fid_by_id(fid)) == NULL || f->fd == -1) {
1457 np_error(hdr->tag, "invalid fid");
1461 if (!(f->iomode & O_WRONLY) &&
1462 !(f->iomode & O_RDWR)) {
1463 np_error(hdr->tag, "fid not opened for writing");
1467 if (TYPE_OVERFLOW(off_t, off)) {
1468 log_warnx("unexpected off_t size");
1469 np_error(hdr->tag, "invalid offset");
1473 if ((r = pwrite(f->fd, data, len, off)) == -1)
1476 np_write(hdr->tag, r);
1480 tstat(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1482 struct evbuffer *evb;
1489 if (!NPREAD32("fid", &fid, &data, &len)) {
1490 client_send_listener(IMSG_CLOSE, NULL, 0);
1496 * plan9' stat(9P) is not clear on whether the stat is allowed
1497 * on opened fids or not.
1499 if ((f = fid_by_id(fid)) == NULL) {
1500 np_error(hdr->tag, "invalid fid");
1504 if ((evb = evbuffer_new()) == NULL)
1505 fatal("evbuffer_new");
1508 r = fstat(f->fd, &sb);
1510 r = fstatat(f->dir->fd, f->fpath, &sb, 0);
1518 serialize_stat(f->fpath, &sb, evb);
1519 np_stat(hdr->tag, EVBUFFER_LENGTH(evb), EVBUFFER_DATA(evb));
1524 tremove(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1529 char dirpath[PATH_MAX + 3];
1532 if (!NPREAD32("fid", &fid, &data, &len)) {
1533 client_send_listener(IMSG_CLOSE, NULL, 0);
1538 if ((f = fid_by_id(fid)) == NULL) {
1539 np_error(hdr->tag, "invalid fid");
1543 if (f->qid.type & QTDIR) { /* directory */
1544 strlcpy(dirpath, "../", sizeof(dirpath));
1545 strlcat(dirpath, f->fpath, sizeof(dirpath));
1546 r = unlinkat(f->dir->fd, dirpath, AT_REMOVEDIR);
1548 r = unlinkat(f->dir->fd, f->fpath, 0);
1553 np_remove(hdr->tag);
1559 handle_message(struct imsg *imsg, size_t len)
1563 void (*fn)(struct np_msg_header *, const uint8_t *, size_t);
1565 {Tversion, tversion},
1577 struct np_msg_header hdr;
1582 hexdump("incoming packet", imsg->data, len);
1585 parse_message(imsg->data, len, &hdr, &data);
1588 log_debug("got request: len=%d type=%d[%s] tag=%d",
1589 hdr.len, hdr.type, pp_msg_type(hdr.type), hdr.tag);
1591 if (!handshaked && hdr.type != Tversion) {
1592 client_send_listener(IMSG_CLOSE, NULL, 0);
1597 for (i = 0; i < sizeof(msgs)/sizeof(msgs[0]); ++i) {
1598 if (msgs[i].type != hdr.type)
1601 msgs[i].fn(&hdr, data, len);
1605 np_error(hdr.tag, "Not supported.");