nineproto.c revision 1.11 1 /* $NetBSD: nineproto.c,v 1.11 2019/06/07 05:34:34 ozaki-r Exp $ */
2
3 /*
4 * Copyright (c) 2007 Antti Kantee. All Rights Reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include <sys/cdefs.h>
29 #ifndef lint
30 __RCSID("$NetBSD: nineproto.c,v 1.11 2019/06/07 05:34:34 ozaki-r Exp $");
31 #endif /* !lint */
32
33 #include <sys/types.h>
34
35 #include <errno.h>
36 #include <grp.h>
37 #include <pwd.h>
38 #include <puffs.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41
42 #include "ninepuffs.h"
43 #include "nineproto.h"
44
45 int
46 proto_getqid(struct puffs_framebuf *pb, struct qid9p *qid)
47 {
48
49 if (puffs_framebuf_remaining(pb) < 1+4+8)
50 return ENOBUFS;
51
52 p9pbuf_get_1(pb, &qid->qidtype);
53 p9pbuf_get_4(pb, &qid->qidvers);
54 p9pbuf_get_8(pb, &qid->qidpath);
55
56 return 0;
57 }
58
59 static uid_t
60 ustr2uid(char *uid)
61 {
62 struct passwd *pw;
63
64 pw = getpwnam(uid);
65 if (pw == NULL)
66 return 0; /* XXXXX */
67
68 return pw->pw_uid;
69 }
70
71 static gid_t
72 gstr2gid(char *gid)
73 {
74 struct group *grr;
75
76 grr = getgrnam(gid);
77 if (grr == NULL)
78 return 0; /* more XXXX */
79
80 return grr->gr_gid;
81 }
82
83 static const char *
84 uid2ustr(uid_t uid)
85 {
86 struct passwd *pw;
87
88 pw = getpwuid(uid);
89 if (pw == NULL)
90 return "root"; /* XXXXX */
91
92 return pw->pw_name;
93 }
94
95 static const char *
96 gid2gstr(gid_t gid)
97 {
98 struct group *grr;
99
100 grr = getgrgid(gid);
101 if (grr == NULL)
102 return "wheel"; /* XXXXXX */
103
104 return grr->gr_name;
105 }
106
107 #define GETFIELD(a,b,unitsize) \
108 do { \
109 if (size < unitsize) return EPROTO; \
110 if ((rv = (a(pb, b)))) return rv; \
111 size -= unitsize; \
112 } while (/*CONSTCOND*/0)
113 #define GETSTR(val,strsize) \
114 do { \
115 if ((rv = p9pbuf_get_str(pb, val, strsize))) return rv; \
116 if (*strsize > size) return EPROTO; \
117 size -= *strsize; \
118 } while (/*CONSTCOND*/0)
119 int
120 proto_getstat(struct puffs_usermount *pu, struct puffs_framebuf *pb, struct vattr *vap,
121 char **name, uint16_t *rs)
122 {
123 struct puffs9p *p9p = puffs_getspecific(pu);
124 char *uid, *gid;
125 struct qid9p qid;
126 uint64_t flen;
127 uint32_t rdev, mode, atime, mtime;
128 uint16_t size, v16;
129 int rv;
130
131 /* check size */
132 if ((rv = p9pbuf_get_2(pb, &size)))
133 return rv;
134 if (puffs_framebuf_remaining(pb) < size)
135 return ENOBUFS;
136
137 if (rs)
138 *rs = size+2; /* compensate for size field itself */
139
140 GETFIELD(p9pbuf_get_2, &v16, 2);
141 if (v16)
142 printf("%d\n", v16);
143 GETFIELD(p9pbuf_get_4, &rdev, 4);
144 GETFIELD(proto_getqid, &qid, 13);
145 GETFIELD(p9pbuf_get_4, &mode, 4);
146 GETFIELD(p9pbuf_get_4, &atime, 4);
147 GETFIELD(p9pbuf_get_4, &mtime, 4);
148 GETFIELD(p9pbuf_get_8, &flen, 8);
149 GETSTR(name, &v16);
150 GETSTR(&uid, &v16);
151 GETSTR(&gid, &v16);
152
153 if (rdev)
154 printf("%d\n", rdev);
155 vap->va_rdev = rdev;
156 vap->va_mode = mode & 0777; /* may contain other uninteresting bits */
157 vap->va_atime.tv_sec = atime;
158 vap->va_mtime.tv_sec = mtime;
159 vap->va_ctime.tv_sec = mtime;
160 vap->va_atime.tv_nsec=vap->va_mtime.tv_nsec=vap->va_ctime.tv_nsec = 0;
161 vap->va_birthtime.tv_sec = vap->va_birthtime.tv_nsec = 0;
162 vap->va_size = vap->va_bytes = flen;
163 vap->va_uid = ustr2uid(uid);
164 vap->va_gid = gstr2gid(gid);
165 free(uid);
166 free(gid);
167 qid2vattr(vap, &qid);
168
169 /* some defaults */
170 if (vap->va_type == VDIR)
171 vap->va_nlink = 1906;
172 else
173 vap->va_nlink = 1;
174 vap->va_blocksize = 512;
175 vap->va_flags = vap->va_vaflags = 0;
176 vap->va_filerev = PUFFS_VNOVAL;
177
178 /* muid, not used */
179 GETSTR(NULL, &v16);
180 if (p9p->protover == P9PROTO_VERSION_U) {
181 uint32_t dummy;
182 GETSTR(NULL, &v16); /* extention[s], not used */
183 GETFIELD(p9pbuf_get_4, &dummy, 4); /* n_uid[4], not used */
184 GETFIELD(p9pbuf_get_4, &dummy, 4); /* n_gid[4], not used */
185 GETFIELD(p9pbuf_get_4, &dummy, 4); /* n_muid[4], not used */
186 }
187
188 return 0;
189 }
190
191 static int
192 proto_rerror(struct puffs_usermount *pu, struct puffs_framebuf *pb,
193 uint32_t *_errno)
194 {
195 struct puffs9p *p9p = puffs_getspecific(pu);
196 uint16_t size;
197 int rv;
198 char *name;
199
200 /* Skip size[4] Rerror tag[2] */
201 rv = puffs_framebuf_seekset(pb,
202 sizeof(uint32_t) + sizeof(uint8_t) + sizeof(uint16_t));
203 if (rv == -1)
204 return EPROTO;
205
206 rv = p9pbuf_get_str(pb, &name, &size);
207 if (rv != 0)
208 return rv;
209 if (p9p->protover == P9PROTO_VERSION_U) {
210 rv = p9pbuf_get_4(pb, _errno);
211 } else {
212 /* TODO Convert error string to errno */
213 rv = EPROTO;
214 }
215
216 return rv;
217 }
218
219 int
220 proto_handle_rerror(struct puffs_usermount *pu, struct puffs_framebuf *pb)
221 {
222 int rv;
223 uint32_t _errno;
224
225 if (p9pbuf_get_type(pb) != P9PROTO_R_ERROR)
226 return EPROTO;
227
228 rv = proto_rerror(pu, pb, &_errno);
229 if (rv == 0)
230 rv = _errno;
231 return rv;
232 }
233
234 int
235 proto_cc_dupfid(struct puffs_usermount *pu, p9pfid_t oldfid, p9pfid_t newfid)
236 {
237 struct puffs_cc *pcc = puffs_cc_getcc(pu);
238 struct puffs9p *p9p = puffs_getspecific(pu);
239 struct puffs_framebuf *pb;
240 p9ptag_t tag = NEXTTAG(p9p);
241 uint16_t qids;
242 int rv = 0;
243
244 pb = p9pbuf_makeout();
245 p9pbuf_put_1(pb, P9PROTO_T_WALK);
246 p9pbuf_put_2(pb, tag);
247 p9pbuf_put_4(pb, oldfid);
248 p9pbuf_put_4(pb, newfid);
249 p9pbuf_put_2(pb, 0);
250 GETRESPONSE(pb);
251
252 rv = proto_expect_walk_nqids(pu, pb, &qids);
253 if (rv == 0 && qids != 0)
254 rv = EPROTO;
255
256 out:
257 puffs_framebuf_destroy(pb);
258 return rv;
259 }
260
261 int
262 proto_cc_clunkfid(struct puffs_usermount *pu, p9pfid_t fid, int waitforit)
263 {
264 struct puffs_cc *pcc = puffs_cc_getcc(pu);
265 struct puffs9p *p9p = puffs_getspecific(pu);
266 struct puffs_framebuf *pb;
267 p9ptag_t tag = NEXTTAG(p9p);
268 int rv = 0;
269
270 pb = p9pbuf_makeout();
271 p9pbuf_put_1(pb, P9PROTO_T_CLUNK);
272 p9pbuf_put_2(pb, tag);
273 p9pbuf_put_4(pb, fid);
274
275 if (waitforit) {
276 if (puffs_framev_enqueue_cc(pcc, p9p->servsock, pb, 0) == 0) {
277 if (p9pbuf_get_type(pb) != P9PROTO_R_CLUNK)
278 rv = proto_handle_rerror(pu, pb);
279 } else {
280 rv = errno;
281 }
282 puffs_framebuf_destroy(pb);
283 } else {
284 JUSTSEND(pb);
285 }
286
287 out:
288 return rv;
289 }
290
291 /*
292 * walk a new fid, then open it
293 */
294 int
295 proto_cc_open(struct puffs_usermount *pu, p9pfid_t fid,
296 p9pfid_t newfid, int mode)
297 {
298 struct puffs_cc *pcc = puffs_cc_getcc(pu);
299 struct puffs9p *p9p = puffs_getspecific(pu);
300 struct puffs_framebuf *pb;
301 p9ptag_t tag = NEXTTAG(p9p);
302 int rv;
303
304 rv = proto_cc_dupfid(pu, fid, newfid);
305 if (rv)
306 return rv;
307
308 pb = p9pbuf_makeout();
309 p9pbuf_put_1(pb, P9PROTO_T_OPEN);
310 p9pbuf_put_2(pb, tag);
311 p9pbuf_put_4(pb, newfid);
312 p9pbuf_put_1(pb, mode);
313 GETRESPONSE(pb);
314 if (p9pbuf_get_type(pb) != P9PROTO_R_OPEN)
315 rv = proto_handle_rerror(pu, pb);
316
317 out:
318 puffs_framebuf_destroy(pb);
319 return rv;
320 }
321
322 void
323 proto_make_stat(struct puffs_usermount *pu, struct puffs_framebuf *pb,
324 const struct vattr *vap, const char *filename, enum vtype vt)
325 {
326 struct puffs9p *p9p = puffs_getspecific(pu);
327 struct vattr fakeva;
328 uint32_t mode, atime, mtime;
329 uint64_t flen;
330 const char *owner, *group;
331 int startoff, curoff;
332
333 if (vap == NULL) {
334 puffs_vattr_null(&fakeva);
335 vap = &fakeva;
336 }
337
338 startoff = puffs_framebuf_telloff(pb);
339 puffs_framebuf_seekset(pb, startoff + 2+2); /* stat[n] incl. stat[2] */
340
341 if (vap->va_mode != (mode_t)PUFFS_VNOVAL)
342 mode = vap->va_mode | (vt == VDIR ? P9PROTO_CPERM_DIR : 0);
343 else
344 mode = P9PROTO_STAT_NOVAL4;
345 if (vap->va_atime.tv_sec != (time_t)PUFFS_VNOVAL)
346 atime = vap->va_atime.tv_sec;
347 else
348 atime = P9PROTO_STAT_NOVAL4;
349 if (vap->va_mtime.tv_sec != (time_t)PUFFS_VNOVAL)
350 mtime = vap->va_mtime.tv_sec;
351 else
352 mtime = P9PROTO_STAT_NOVAL4;
353 if (vap->va_size != (u_quad_t)PUFFS_VNOVAL)
354 flen = vap->va_size;
355 else
356 flen = P9PROTO_STAT_NOVAL8;
357 if (vap->va_uid != (uid_t)PUFFS_VNOVAL)
358 owner = uid2ustr(vap->va_uid);
359 else
360 owner = "";
361 if (vap->va_gid != (gid_t)PUFFS_VNOVAL)
362 group = gid2gstr(vap->va_gid);
363 else
364 group = "";
365
366 p9pbuf_put_2(pb, P9PROTO_STAT_NOVAL2); /* kernel type */
367 p9pbuf_put_4(pb, P9PROTO_STAT_NOVAL4); /* dev */
368 p9pbuf_put_1(pb, P9PROTO_STAT_NOVAL1); /* type */
369 p9pbuf_put_4(pb, P9PROTO_STAT_NOVAL4); /* version */
370 p9pbuf_put_8(pb, P9PROTO_STAT_NOVAL8); /* path */
371 p9pbuf_put_4(pb, mode);
372 p9pbuf_put_4(pb, atime);
373 p9pbuf_put_4(pb, mtime);
374 p9pbuf_put_8(pb, flen);
375 p9pbuf_put_str(pb, filename ? filename : "");
376 p9pbuf_put_str(pb, owner);
377 p9pbuf_put_str(pb, group);
378 p9pbuf_put_str(pb, ""); /* muid */
379 if (p9p->protover == P9PROTO_VERSION_U) {
380 p9pbuf_put_str(pb, P9PROTO_STAT_NOSTR); /* extentions[s] */
381 p9pbuf_put_4(pb, P9PROTO_STAT_NOVAL4); /* n_uid[4] */
382 p9pbuf_put_4(pb, P9PROTO_STAT_NOVAL4); /* n_gid[4] */
383 p9pbuf_put_4(pb, P9PROTO_STAT_NOVAL4); /* n_muid[4] */
384 }
385
386 curoff = puffs_framebuf_telloff(pb);
387 puffs_framebuf_seekset(pb, startoff);
388 p9pbuf_put_2(pb, curoff-(startoff+2)); /* stat[n] size */
389 p9pbuf_put_2(pb, curoff-(startoff+4)); /* size[2] stat */
390
391 puffs_framebuf_seekset(pb, curoff);
392 }
393
394 int
395 proto_expect_walk_nqids(struct puffs_usermount *pu, struct puffs_framebuf *pb,
396 uint16_t *nqids)
397 {
398
399 if (p9pbuf_get_type(pb) != P9PROTO_R_WALK)
400 return proto_handle_rerror(pu, pb);
401 return p9pbuf_get_2(pb, nqids);
402 }
403
404 int
405 proto_expect_qid(struct puffs_usermount *pu, struct puffs_framebuf *pb,
406 uint8_t op, struct qid9p *qid)
407 {
408
409 if (p9pbuf_get_type(pb) != op)
410 return proto_handle_rerror(pu, pb);
411 return proto_getqid(pb, qid);
412 }
413
414 int
415 proto_expect_stat(struct puffs_usermount *pu, struct puffs_framebuf *pb,
416 struct vattr *va)
417 {
418 uint16_t dummy;
419 int rv;
420
421 if (p9pbuf_get_type(pb) != P9PROTO_R_STAT)
422 return proto_handle_rerror(pu, pb);
423 if ((rv = p9pbuf_get_2(pb, &dummy)))
424 return rv;
425 return proto_getstat(pu, pb, va, NULL, NULL);
426 }
427