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_create(uint16_t, struct qid *, uint32_t);
131 static void np_read(uint16_t, uint32_t, void *);
132 static void np_write(uint16_t, uint32_t);
133 static void np_stat(uint16_t, uint32_t, void *);
134 static void np_remove(uint16_t);
135 static void np_error(uint16_t, const char *);
136 static void np_errno(uint16_t);
138 static int np_read8(const char *, const char *, uint8_t *,
139 const uint8_t **, size_t *);
140 static int np_read16(const char *, const char *, uint16_t *,
141 const uint8_t **, size_t *);
142 static int np_read32(const char *, const char *, uint32_t *,
143 const uint8_t **, size_t *);
144 static int np_read64(const char *, const char *, uint64_t *,
145 const uint8_t **, size_t *);
147 #define READSTRERR -1
148 #define READSTRTRUNC -2
149 static int np_readstr(const char *, const char *, char *, size_t,
150 const uint8_t **, size_t *);
152 #define NPREAD8(f, dst, src, len) np_read8(__func__, f, dst, src, len)
153 #define NPREAD16(f, dst, src, len) np_read16(__func__, f, dst, src, len)
154 #define NPREAD32(f, dst, src, len) np_read32(__func__, f, dst, src, len)
155 #define NPREAD64(f, dst, src, len) np_read64(__func__, f, dst, src, len)
157 #define NPREADSTR(f, b, bl, src, len) np_readstr(__func__, f, b, bl, src, len)
159 static void tversion(struct np_msg_header *, const uint8_t *, size_t);
160 static void tattach(struct np_msg_header *, const uint8_t *, size_t);
161 static void tclunk(struct np_msg_header *, const uint8_t *, size_t);
162 static void tflush(struct np_msg_header *, const uint8_t *, size_t);
163 static void twalk(struct np_msg_header *, const uint8_t *, size_t);
164 static void topen(struct np_msg_header *, const uint8_t *, size_t);
165 static void tcreate(struct np_msg_header *, const uint8_t *, size_t);
166 static void tread(struct np_msg_header *, const uint8_t *, size_t);
167 static void twrite(struct np_msg_header *, const uint8_t *, size_t);
168 static void tstat(struct np_msg_header *, const uint8_t *, size_t);
169 static void tremove(struct np_msg_header *, const uint8_t *, size_t);
170 static void handle_message(struct imsg *, size_t);
173 client(int debug, int verbose)
175 struct event ev_sigint, ev_sigterm;
177 log_init(debug, LOG_DAEMON);
178 log_setverbose(verbose);
180 setproctitle("client");
181 log_procinit("client");
183 log_debug("warming up");
187 /* Setup signal handlers */
188 signal_set(&ev_sigint, SIGINT, client_sig_handler, NULL);
189 signal_set(&ev_sigterm, SIGTERM, client_sig_handler, NULL);
191 signal_add(&ev_sigint, NULL);
192 signal_add(&ev_sigterm, NULL);
194 signal(SIGPIPE, SIG_IGN);
195 signal(SIGHUP, SIG_IGN);
197 /* Setup pipe and event handler to the listener process */
198 if ((iev_listener = malloc(sizeof(*iev_listener))) == NULL)
201 imsg_init(&iev_listener->ibuf, 3);
202 iev_listener->handler = client_dispatch_listener;
204 /* Setup event handlers. */
205 iev_listener->events = EV_READ;
206 event_set(&iev_listener->ev, iev_listener->ibuf.fd,
207 iev_listener->events, iev_listener->handler, iev_listener);
208 event_add(&iev_listener->ev, NULL);
214 static ATTR_DEAD void
215 client_shutdown(void)
220 msgbuf_clear(&iev_listener->ibuf.w);
221 close(iev_listener->ibuf.fd);
225 log_debug("client exiting");
230 client_sig_handler(int sig, short event, void *d)
233 * Normal signal handler rules don't apply because libevent
242 fatalx("unexpected signal %d", sig);
251 client_dispatch_listener(int fd, short event, void *d)
253 static int auth = AUTH_NONE;
254 static char username[64] = {0};
255 static char dir[PATH_MAX] = {0};
257 struct imsgev *iev = d;
258 struct imsgbuf *ibuf;
264 if (event & EV_READ) {
265 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
266 fatal("imsg_read error");
267 if (n == 0) /* Connection closed */
270 if (event & EV_WRITE) {
271 if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
272 fatal("msgbuf_write");
273 if (n == 0) /* Connection closed */
278 if ((n = imsg_get(ibuf, &imsg)) == -1)
279 fatal("%s: imsg_get error", __func__);
280 if (n == 0) /* No more messages. */
283 switch (imsg.hdr.type) {
285 peerid = imsg.hdr.peerid;
287 fatalx("%s: IMSG_AUTH already done", __func__);
289 ((char *)imsg.data)[IMSG_DATA_SIZE(imsg)-1] = '\0';
290 strlcpy(username, imsg.data, sizeof(username));
293 if (auth != AUTH_USER)
294 fatalx("%s: IMSG_AUTH_DIR not after IMSG_AUTH",
297 ((char *)imsg.data)[IMSG_DATA_SIZE(imsg)-1] = '\0';
298 strlcpy(dir, imsg.data, sizeof(dir));
299 client_privdrop(username, dir);
300 memset(username, 0, sizeof(username));
301 memset(dir, 0, sizeof(username));
306 fatalx("%s: can't handle messages before"
307 " doing the auth", __func__);
308 handle_message(&imsg, IMSG_DATA_SIZE(imsg));
311 log_debug("closing");
315 log_debug("%s: unexpected imsg %d",
316 __func__, imsg.hdr.type);
325 /* This pipe is dead. Remove its event handler. */
327 log_debug("pipe closed, shutting down...");
328 event_loopexit(NULL);
333 client_privdrop(const char *username, const char *dir)
337 setproctitle("client %s", username);
339 if ((pw = getpwnam(username)) == NULL)
340 fatalx("getpwnam(%s) failed", username);
342 if (chroot(dir) == -1)
344 if (chdir("/") == -1)
345 fatal("chdir(\"/\")");
347 if (setgroups(1, &pw->pw_gid) ||
348 setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
349 setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
350 fatal("can't drop privileges");
353 log_debug("client ready; user=%s dir=%s", username, dir);
355 if ((evb = evbuffer_new()) == NULL)
356 fatal("evbuffer_new");
360 client_send_listener(int type, const void *data, uint16_t len)
364 if ((ret = imsg_compose(&iev_listener->ibuf, type, peerid, 0, -1,
366 imsg_event_add(iev_listener);
371 /* set qid fields from sb */
373 qid_update_from_sb(struct qid *qid, struct stat *sb)
375 qid->path = sb->st_ino;
378 * Theoretically (and hopefully!) this should be a 64 bit
379 * number. Unfortunately, 9P uses 32 bit timestamps.
381 qid->vers = sb->st_mtim.tv_sec;
383 if (S_ISREG(sb->st_mode))
385 else if (S_ISDIR(sb->st_mode))
387 else if (S_ISLNK(sb->st_mode))
388 qid->type = QTSYMLINK;
391 /* creates a qid given a fd */
393 qid_from_fd(int fd, const char *path, struct stat *s)
399 if ((qid = calloc(1, sizeof(*qid))) == NULL)
403 strlcpy(qid->fpath, path, sizeof(qid->fpath));
412 r = fstatat(fd, path, s, 0);
419 qid_update_from_sb(qid, s);
421 STAILQ_INSERT_HEAD(&qids, qid, entries);
427 qid_incref(struct qid *qid)
434 qid_decref(struct qid *qid)
436 if (--qid->refcount > 0)
439 STAILQ_REMOVE(&qids, qid, qid, entries);
446 new_fid(struct qid *qid, uint32_t fid)
450 if ((f = calloc(1, sizeof(*f))) == NULL)
453 f->qid = qid_incref(qid);
457 STAILQ_INSERT_HEAD(&fids, f, entries);
463 fid_by_id(uint32_t fid)
467 STAILQ_FOREACH(f, &fids, entries) {
476 free_fid(struct fid *f)
482 r = closedir(f->dir);
487 fatal("can't close fid %d", f->fid);
490 evbuffer_free(f->evb);
492 /* try to honour ORCLOSE if requested */
493 if (f->iomode & O_CLOEXEC)
494 unlinkat(f->qid->fd, f->qid->fpath, 0);
499 STAILQ_REMOVE(&fids, f, fid, entries);
504 parse_message(const uint8_t *data, size_t len, struct np_msg_header *hdr,
509 if (!NPREAD32("len", &hdr->len, &data, &len) ||
510 !NPREAD8("type", &hdr->type, &data, &len) ||
511 !NPREAD16("tag", &hdr->tag, &data, &len))
514 if (olen != hdr->len)
517 if (hdr->type < Tversion ||
519 hdr->type == Terror ||
520 (hdr->type & 0x1) != 0) /* cannot recv a R* */
523 hdr->tag = le32toh(hdr->tag);
525 *cnt = (uint8_t *)data;
529 /* TODO: send a proper message to terminate the connection. */
530 fatalx("got invalid message");
534 np_write16(struct evbuffer *e, uint16_t x)
537 evbuffer_add(e, &x, sizeof(x));
541 np_write32(struct evbuffer *e, uint32_t x)
544 evbuffer_add(e, &x, sizeof(x));
548 np_write64(struct evbuffer *e, uint64_t x)
551 evbuffer_add(e, &x, sizeof(x));
555 np_writebuf(struct evbuffer *e, size_t len, void *data)
557 evbuffer_add(e, data, len);
561 np_header(uint32_t len, uint8_t type, uint16_t tag)
568 evbuffer_add(evb, &len, sizeof(len));
569 evbuffer_add(evb, &type, sizeof(type));
570 evbuffer_add(evb, &tag, sizeof(tag));
574 np_string(struct evbuffer *e, uint16_t len, const char *str)
579 evbuffer_add(e, &len, sizeof(len));
580 evbuffer_add(e, str, l);
584 np_qid(struct evbuffer *e, struct qid *qid)
589 path = htole64(qid->path);
590 vers = htole32(qid->vers);
592 evbuffer_add(e, &qid->type, sizeof(qid->type));
593 evbuffer_add(e, &vers, sizeof(vers));
594 evbuffer_add(e, &path, sizeof(path));
603 len = EVBUFFER_LENGTH(evb);
604 data = EVBUFFER_DATA(evb);
607 hexdump("outgoing packet", data, len);
609 client_send_listener(IMSG_BUF, data, len);
610 evbuffer_drain(evb, len);
614 np_version(uint16_t tag, uint32_t msize, const char *version)
620 msize = htole32(msize);
622 np_header(sizeof(msize) + sizeof(l) + l, Rversion, tag);
623 evbuffer_add(evb, &msize, sizeof(msize));
624 np_string(evb, l, version);
629 np_attach(uint16_t tag, struct qid *qid)
631 np_header(QIDSIZE, Rattach, tag);
637 np_clunk(uint16_t tag)
639 np_header(0, Rclunk, tag);
644 np_flush(uint16_t tag)
646 np_header(0, Rflush, tag);
651 np_walk(uint16_t tag, int nwqid, struct qid *wqid)
655 /* two bytes for the counter */
656 np_header(2 + QIDSIZE * nwqid, Rwalk, tag);
657 np_write16(evb, nwqid);
658 for (i = 0; i < nwqid; ++i)
659 np_qid(evb, wqid + i);
665 np_open(uint16_t tag, struct qid *qid, uint32_t iounit)
667 np_header(QIDSIZE + sizeof(iounit), Ropen, tag);
669 np_write32(evb, iounit);
674 np_create(uint16_t tag, struct qid *qid, uint32_t iounit)
676 np_header(QIDSIZE + sizeof(iounit), Rcreate, tag);
678 np_write32(evb, iounit);
683 np_read(uint16_t tag, uint32_t count, void *data)
685 np_header(sizeof(count) + count, Rread, tag);
686 np_write32(evb, count);
687 np_writebuf(evb, count, data);
692 np_write(uint16_t tag, uint32_t count)
694 np_header(sizeof(count), Rwrite, tag);
695 np_write32(evb, count);
700 np_stat(uint16_t tag, uint32_t count, void *data)
702 np_header(count, Rstat, tag);
703 np_writebuf(evb, count, data);
708 np_remove(uint16_t tag)
710 np_header(0, Rremove, tag);
715 np_error(uint16_t tag, const char *errstr)
721 np_header(sizeof(l) + l, Rerror, tag);
722 np_string(evb, l, errstr);
727 np_errno(uint16_t tag)
734 strerror_r(errno, buf, sizeof(buf));
741 np_read8(const char *t, const char *f, uint8_t *dst, const uint8_t **src,
744 if (*len < sizeof(*dst)) {
745 log_warnx("%s: wanted %zu bytes for the %s field but only "
746 "%zu are available.", t, sizeof(*dst), f, *len);
750 memcpy(dst, *src, sizeof(*dst));
751 *src += sizeof(*dst);
752 *len -= sizeof(*dst);
758 np_read16(const char *t, const char *f, uint16_t *dst, const uint8_t **src,
761 if (*len < sizeof(*dst)) {
762 log_warnx("%s: wanted %zu bytes for the %s field but only "
763 "%zu are available.", t, sizeof(*dst), f, *len);
767 memcpy(dst, *src, sizeof(*dst));
768 *src += sizeof(*dst);
769 *len -= sizeof(*dst);
770 *dst = le16toh(*dst);
776 np_read32(const char *t, const char *f, uint32_t *dst, const uint8_t **src,
779 if (*len < sizeof(*dst)) {
780 log_warnx("%s: wanted %zu bytes for the %s field but only "
781 "%zu are available.", t, sizeof(*dst), f, *len);
785 memcpy(dst, *src, sizeof(*dst));
786 *src += sizeof(*dst);
787 *len -= sizeof(*dst);
788 *dst = le32toh(*dst);
794 np_read64(const char *t, const char *f, uint64_t *dst, const uint8_t **src,
797 if (*len < sizeof(*dst)) {
798 log_warnx("%s: wanted %zu bytes for the %s field but only "
799 "%zu are available.", t, sizeof(*dst), f, *len);
803 memcpy(dst, *src, sizeof(*dst));
804 *src += sizeof(*dst);
805 *len -= sizeof(*dst);
806 *dst = le64toh(*dst);
812 np_readstr(const char *t, const char *f, char *res, size_t reslen,
813 const uint8_t **src, size_t *len)
818 strlcpy(buf, f, sizeof(buf));
819 strlcat(buf, "-len", sizeof(buf));
821 if (!np_read16(t, buf, &sl, src, len))
825 log_warnx("%s: wanted %d bytes for the %s field but only "
826 "%zu are available.", t, sl, f, *len);
833 memcpy(res, *src, sl);
842 tversion(struct np_msg_header *hdr, const uint8_t *data, size_t len)
844 char *dot, version[32];
849 /* msize[4] version[s] */
850 if (!NPREAD32("msize", &msize, &data, &len))
853 switch (NPREADSTR("version", version, sizeof(version), &data, &len)) {
857 log_warnx("9P version string too long, truncated");
861 if ((dot = strchr(version, '.')) != NULL)
864 if (strcmp(version, VERSION9P) != 0 ||
868 /* version matched */
870 msize = MIN(msize, MSIZE9P);
871 client_send_listener(IMSG_MSIZE, &msize, sizeof(msize));
872 np_version(hdr->tag, msize, VERSION9P);
876 log_warnx("unknown 9P version string: \"%s\", want "VERSION9P,
878 np_version(hdr->tag, MSIZE9P, "unknown");
882 client_send_listener(IMSG_CLOSE, NULL, 0);
887 tattach(struct np_msg_header *hdr, const uint8_t *data, size_t len)
893 char aname[PATH_MAX];
895 /* fid[4] afid[4] uname[s] aname[s] */
897 if (!NPREAD32("fid", &fid, &data, &len) ||
898 !NPREAD32("afid", &afid, &data, &len))
901 /* read the uname but don't actually use it */
902 switch (NPREADSTR("uname", aname, sizeof(aname), &data, &len)) {
906 np_error(hdr->tag, "name too long");
910 switch (NPREADSTR("aname", aname, sizeof(aname), &data, &len)) {
914 np_error(hdr->tag, "name too long");
918 if (fid_by_id(fid) != NULL || afid != NOFID) {
919 np_error(hdr->tag, "invalid fid or afid");
923 if ((fd = open(aname, O_RDONLY|O_DIRECTORY)) == -1)
926 if ((qid = qid_from_fd(fd, NULL, NULL)) == NULL)
929 log_debug("attached %s to %d", aname, fid);
931 if ((f = new_fid(qid, fid)) == NULL) {
936 np_attach(hdr->tag, qid);
941 log_warn("failed to attach %s", aname);
946 client_send_listener(IMSG_CLOSE, NULL, 0);
951 tclunk(struct np_msg_header *hdr, const uint8_t *data, size_t len)
957 if (!NPREAD32("fid", &fid, &data, &len)) {
958 client_send_listener(IMSG_CLOSE, NULL, 0);
963 if ((f = fid_by_id(fid)) == NULL) {
964 np_error(hdr->tag, "invalid fid");
973 tflush(struct np_msg_header *hdr, const uint8_t *data, size_t len)
978 * We're doing only synchronous I/O. Tflush is implemented
979 * only because it's illegal to reply with a Rerror.
983 if (len != sizeof(oldtag)) {
984 log_warnx("Tflush with the wrong size: got %zu want %zu",
985 len, sizeof(oldtag));
986 client_send_listener(IMSG_CLOSE, NULL, 0);
995 twalk(struct np_msg_header *hdr, const uint8_t *data, size_t len)
998 struct qid *qid, wqid[MAXWELEM] = {0};
1000 uint32_t fid, newfid;
1002 int fd, oldfd, no, nwqid = 0;
1003 char wnam[PATH_MAX];
1005 if (!NPREAD32("fid", &fid, &data, &len) ||
1006 !NPREAD32("newfid", &newfid, &data, &len) ||
1007 !NPREAD16("nwname", &nwname, &data, &len))
1010 if (nwname > MAXWELEM) {
1011 log_warnx("Twalk: more than %d path elements: %d",
1016 if ((f = fid_by_id(fid)) == NULL) {
1017 np_error(hdr->tag, "invalid fid");
1022 np_error(hdr->tag, "fid already opened for I/O");
1028 else if ((nf = fid_by_id(newfid)) != NULL) {
1029 np_error(hdr->tag, "newfid already in use");
1034 /* special case: fid duplication */
1037 * TODO: should we forbid fids duplication when fid ==
1040 if (nf == NULL && (nf = new_fid(f->qid, newfid)) == NULL)
1041 fatal("new_fid duplication");
1043 np_walk(hdr->tag, 1, f->qid);
1047 if (f->qid->type != QTDIR) {
1048 np_error(hdr->tag, "fid doesn't represent a directory");
1054 for (nwqid = 0; nwqid < nwname; nwqid++) {
1055 switch (NPREADSTR("wname", wnam, sizeof(wnam), &data, &len)) {
1059 np_error(hdr->tag, "wname too long");
1063 if (*wnam == '\0' ||
1064 strchr(wnam, '/') != NULL ||
1065 !strcmp(wnam, ".")) {
1070 if ((fd = openat(oldfd, wnam, O_RDONLY|O_DIRECTORY)) == -1 &&
1074 if ((fd == -1 && fstatat(oldfd, wnam, &sb, 0) == -1) ||
1075 (fd != -1 && fstat(fd, &sb) == -1))
1078 qid_update_from_sb(&wqid[nwqid], &sb);
1080 /* reached a file but we still have other components */
1081 if (fd == -1 && nwqid+1 < nwname)
1084 /* reached the end and found a file */
1085 if (fd == -1 && nwqid+1 == nwname)
1088 if (oldfd != f->qid->fd)
1094 * There can be two possibilities: fd == -1 means that we've
1095 * reached a file and we should save BOTH the dirfd (oldfd)
1096 * and the path (wnam); or we just reached another directory,
1097 * in which case we can just create a new qid using fd.
1100 qid = qid_from_fd(oldfd, wnam, &sb);
1102 qid = qid_from_fd(oldfd, NULL, &sb);
1104 fatal("qid_from_fd");
1107 if ((nf = new_fid(qid, newfid)) == NULL)
1111 qid_decref(nf->qid);
1112 nf->qid = qid_incref(qid);
1115 np_walk(hdr->tag, nwqid, wqid);
1119 if (oldfd != f->qid->fd)
1123 np_error(hdr->tag, strerror(no));
1125 np_walk(hdr->tag, nwqid, wqid);
1129 client_send_listener(IMSG_CLOSE, NULL, 0);
1134 npmode_to_unix(uint8_t mode, int *flags)
1136 switch (mode & 0x0F) {
1147 log_warnx("tried to open something with KOEXEC");
1155 if (mode & KORCLOSE)
1156 *flags |= O_CLOEXEC;
1162 topen(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1171 /* fid[4] mode[1] */
1172 if (!NPREAD32("fid", &fid, &data, &len) ||
1173 !NPREAD8("mode", &mode, &data, &len)) {
1174 client_send_listener(IMSG_CLOSE, NULL, 0);
1179 if ((f = fid_by_id(fid)) == NULL || f->fd != -1) {
1180 np_error(hdr->tag, "invalid fid");
1184 if (npmode_to_unix(mode, &f->iomode) == -1) {
1185 np_error(hdr->tag, "invalid mode");
1189 if (f->qid->type & QTDIR &&
1190 (f->iomode & O_WRONLY || f->iomode & O_RDWR)) {
1191 np_error(hdr->tag, "can't open directory for writing");
1195 path = f->qid->fpath;
1199 if ((f->fd = openat(f->qid->fd, path, f->iomode)) == -1) {
1200 np_error(hdr->tag, strerror(errno));
1204 if (fstat(f->fd, &sb) == -1)
1207 if (S_ISDIR(sb.st_mode)) {
1208 assert(f->qid->type & QTDIR);
1209 if ((f->dir = fdopendir(f->fd)) == NULL) {
1216 if ((f->evb = evbuffer_new()) == NULL) {
1226 qid_update_from_sb(&qid, &sb);
1227 np_open(hdr->tag, &qid, sb.st_blksize);
1231 tcreate(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1238 char name[PATH_MAX];
1240 /* fid[4] name[s] perm[4] mode[1] */
1241 if (!NPREAD32("fid", &fid, &data, &len))
1243 switch (NPREADSTR("name", name, sizeof(name), &data, &len)) {
1247 np_error(hdr->tag, "name too long");
1250 if (!NPREAD32("perm", &perm, &data, &len) ||
1251 !NPREAD8("mode", &mode, &data, &len))
1254 if (!strcmp(name, ".") || !strcmp(name, "..") ||
1255 strchr(name, '/') != NULL) {
1256 np_error(hdr->tag, "invalid name");
1260 if ((f = fid_by_id(fid)) == NULL || f->fd != -1) {
1261 np_error(hdr->tag, "invalid fid");
1265 if (!(f->qid->type & QTDIR)) {
1266 np_error(hdr->tag, "fid doesn't identify a directory");
1270 if (npmode_to_unix(mode, &f->iomode) == -1) {
1271 np_error(hdr->tag, "invalid mode");
1275 if (f->iomode & O_RDONLY) {
1276 np_error(hdr->tag, "can't create a read-only file");
1280 /* TODO: parse the mode */
1282 if (perm & 0x80000000) {
1283 /* create a directory */
1284 f->fd = mkdirat(f->qid->fd, name, 0755);
1287 f->fd = openat(f->qid->fd, name, f->iomode | O_CREAT | O_TRUNC,
1296 if (fstat(f->fd, &sb) == -1)
1299 if (S_ISDIR(sb.st_mode)) {
1300 if ((f->dir = fdopendir(f->fd)) == NULL) {
1307 if ((f->evb = evbuffer_new()) == NULL) {
1317 qid_update_from_sb(&qid, &sb);
1318 np_create(hdr->tag, &qid, sb.st_blksize);
1323 client_send_listener(IMSG_CLOSE, NULL, 0);
1328 serialize_stat(const char *fname, struct stat *sb, struct evbuffer *evb)
1331 const char *uid, *gid, *muid;
1333 uint16_t namlen, uidlen, gidlen, ulen;
1335 qid_update_from_sb(&qid, sb);
1337 /* TODO: fill these fields */
1342 namlen = strlen(fname);
1343 uidlen = strlen(uid);
1344 gidlen = strlen(gid);
1345 ulen = strlen(muid);
1347 tot = NPSTATSIZ(namlen, uidlen, gidlen, ulen);
1348 if (tot > UINT32_MAX) {
1349 log_warnx("stat info for dir entry %s would overflow",
1354 np_write16(evb, tot); /* size[2] */
1355 np_write16(evb, sb->st_rdev); /* type[2] */
1356 np_write32(evb, sb->st_dev); /* dev[4] */
1357 np_qid(evb, &qid); /* qid[13] */
1359 /* XXX: translate? */
1360 np_write32(evb, sb->st_mode); /* mode[4] */
1362 np_write32(evb, sb->st_atim.tv_sec); /* atime[4] */
1363 np_write32(evb, sb->st_mtim.tv_sec); /* mtime[4] */
1364 np_write64(evb, sb->st_size); /* length[8] */
1365 np_string(evb, namlen, fname); /* name[s] */
1366 np_string(evb, uidlen, uid); /* uid[s] */
1367 np_string(evb, gidlen, gid); /* gid[s] */
1368 np_string(evb, ulen, muid); /* muid[s] */
1372 tread(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1377 uint32_t fid, count;
1380 /* fid[4] offset[8] count[4] */
1381 if (!NPREAD32("fid", &fid, &data, &len) ||
1382 !NPREAD64("offset", &off, &data, &len) ||
1383 !NPREAD32("count", &count, &data, &len)) {
1384 client_send_listener(IMSG_CLOSE, NULL, 0);
1389 if ((f = fid_by_id(fid)) == NULL || f->fd == -1) {
1390 np_error(hdr->tag, "invalid fid");
1394 if (TYPE_OVERFLOW(off_t, off)) {
1395 log_warnx("unexpected size_t size");
1396 np_error(hdr->tag, "invalid offset");
1400 if (f->dir == NULL) {
1402 r = pread(f->fd, buf, sizeof(buf), (off_t)off);
1406 np_read(hdr->tag, r, buf);
1408 if (off == 0 && f->offset != 0) {
1413 if (off != f->offset) {
1414 np_error(hdr->tag, "can't seek in directories");
1418 while (EVBUFFER_LENGTH(f->evb) < count) {
1422 if ((d = readdir(f->dir)) == NULL)
1424 if (fstatat(f->fd, d->d_name, &sb, 0) == -1) {
1428 serialize_stat(d->d_name, &sb, f->evb);
1431 count = MIN(count, EVBUFFER_LENGTH(f->evb));
1432 np_read(hdr->tag, count, EVBUFFER_DATA(f->evb));
1433 evbuffer_drain(f->evb, count);
1440 twrite(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1445 uint32_t fid, count;
1447 /* fid[4] offset[8] count[4] data[count] */
1448 if (!NPREAD32("fid", &fid, &data, &len) ||
1449 !NPREAD64("off", &off, &data, &len) ||
1450 !NPREAD32("count", &count, &data, &len) ||
1452 client_send_listener(IMSG_CLOSE, NULL, 0);
1457 if ((f = fid_by_id(fid)) == NULL || f->fd == -1) {
1458 np_error(hdr->tag, "invalid fid");
1462 if (!(f->iomode & O_WRONLY) &&
1463 !(f->iomode & O_RDWR)) {
1464 np_error(hdr->tag, "fid not opened for writing");
1468 if (TYPE_OVERFLOW(off_t, off)) {
1469 log_warnx("unexpected off_t size");
1470 np_error(hdr->tag, "invalid offset");
1474 if ((r = pwrite(f->fd, data, len, off)) == -1)
1477 np_write(hdr->tag, r);
1481 tstat(struct np_msg_header *hdr, const uint8_t *data, size_t len)
1483 struct evbuffer *evb;
1491 if (!NPREAD32("fid", &fid, &data, &len)) {
1492 client_send_listener(IMSG_CLOSE, NULL, 0);
1498 * plan9' stat(9P) is not clear on whether the stat is allowed
1499 * on opened fids or not.
1501 if ((f = fid_by_id(fid)) == NULL) {
1502 np_error(hdr->tag, "invalid fid");
1506 if ((evb = evbuffer_new()) == NULL)
1507 fatal("evbuffer_new");
1509 if (f->qid->type & QTDIR) {
1511 r = fstat(f->qid->fd, &sb);
1513 fname = f->qid->fpath;
1514 r = fstatat(f->qid->fd, f->qid->fpath, &sb, 0);
1523 serialize_stat(fname, &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)
1536 if (!NPREAD32("fid", &fid, &data, &len)) {
1537 client_send_listener(IMSG_CLOSE, NULL, 0);
1542 if ((f = fid_by_id(fid)) == NULL) {
1543 np_error(hdr->tag, "invalid fid");
1547 if (f->fd == -1 || f->dir == NULL) /* unlink file */
1548 r = unlinkat(f->qid->fd, f->qid->fpath, 0);
1549 else /* directory */
1550 r = unlinkat(f->qid->fd, f->qid->fpath, AT_REMOVEDIR);
1555 np_remove(hdr->tag);
1561 handle_message(struct imsg *imsg, size_t len)
1565 void (*fn)(struct np_msg_header *, const uint8_t *, size_t);
1567 {Tversion, tversion},
1579 struct np_msg_header hdr;
1584 hexdump("incoming packet", imsg->data, len);
1587 parse_message(imsg->data, len, &hdr, &data);
1590 log_debug("got request: len=%d type=%d[%s] tag=%d",
1591 hdr.len, hdr.type, pp_msg_type(hdr.type), hdr.tag);
1593 if (!handshaked && hdr.type != Tversion) {
1594 client_send_listener(IMSG_CLOSE, NULL, 0);
1599 for (i = 0; i < sizeof(msgs)/sizeof(msgs[0]); ++i) {
1600 if (msgs[i].type != hdr.type)
1603 msgs[i].fn(&hdr, data, len);
1607 np_error(hdr.tag, "Not supported.");