nineproto.c revision 1.1 1 /* $NetBSD: nineproto.c,v 1.1 2007/04/21 14:21:43 pooka 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.1 2007/04/21 14:21:43 pooka 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 p9pbuf *pb, struct qid9p *qid)
47 {
48
49 if (pb->remain < 13)
50 return 0;
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 1;
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 0; \
110 if (!(a(pb, b))) return 0; \
111 size -= unitsize; \
112 } while (/*CONSTCOND*/0)
113 #define GETSTR(val,strsize) \
114 do { \
115 if (!(p9pbuf_get_str(pb, val, strsize))) return 0; \
116 if (*strsize > size) return 0; \
117 size -= *strsize; \
118 } while (/*CONSTCOND*/0)
119 int
120 proto_getstat(struct p9pbuf *pb, struct vattr *vap, char **name, uint16_t *rs)
121 {
122 char *uid, *gid;
123 struct qid9p qid;
124 uint64_t flen;
125 uint32_t rdev, mode, atime, mtime;
126 uint16_t size, v16;
127
128 /* check size */
129 if (!p9pbuf_get_2(pb, &size))
130 return 0;
131 if (p9pbuf_remaining(pb) < size)
132 return 0;
133
134 if (rs)
135 *rs = size+2; /* compensate for size field itself */
136
137 GETFIELD(p9pbuf_get_2, &v16, 2);
138 if (v16)
139 printf("%d\n", v16);
140 GETFIELD(p9pbuf_get_4, &rdev, 4);
141 GETFIELD(proto_getqid, &qid, 13);
142 GETFIELD(p9pbuf_get_4, &mode, 4);
143 GETFIELD(p9pbuf_get_4, &atime, 4);
144 GETFIELD(p9pbuf_get_4, &mtime, 4);
145 GETFIELD(p9pbuf_get_8, &flen, 8);
146 GETSTR(name, &v16);
147 GETSTR(&uid, &v16);
148 GETSTR(&gid, &v16);
149
150 if (rdev)
151 printf("%d\n", rdev);
152 vap->va_rdev = rdev;
153 vap->va_mode = mode & 0777; /* may contain other uninteresting bits */
154 vap->va_atime.tv_sec = atime;
155 vap->va_mtime.tv_sec = mtime;
156 vap->va_ctime.tv_sec = mtime;
157 vap->va_atime.tv_nsec=vap->va_mtime.tv_nsec=vap->va_ctime.tv_nsec = 0;
158 vap->va_birthtime.tv_sec = vap->va_birthtime.tv_nsec = 0;
159 vap->va_size = vap->va_bytes = flen;
160 vap->va_uid = ustr2uid(uid);
161 vap->va_gid = gstr2gid(gid);
162 qid2vattr(vap, &qid);
163
164 /* some defaults */
165 if (vap->va_type == VDIR)
166 vap->va_nlink = 1906;
167 else
168 vap->va_nlink = 1;
169 vap->va_blocksize = 512;
170 vap->va_flags = vap->va_vaflags = 0;
171 vap->va_filerev = PUFFS_VNOVAL;
172
173 /* muid, not used */
174 GETSTR(NULL, &v16);
175
176 return 1;
177 }
178
179 int
180 proto_cc_dupfid(struct puffs_cc *pcc, p9pfid_t oldfid, p9pfid_t newfid)
181 {
182 struct puffs9p *p9p = puffs_cc_getspecific(pcc);
183 struct p9pbuf *pb;
184 p9ptag_t tag = NEXTTAG(p9p);
185 uint16_t qids;
186 int rv, error = 0;
187
188 pb = p9pbuf_make(p9p->maxreq, P9PB_OUT);
189 p9pbuf_put_1(pb, P9PROTO_T_WALK);
190 p9pbuf_put_2(pb, tag);
191 p9pbuf_put_4(pb, oldfid);
192 p9pbuf_put_4(pb, newfid);
193 p9pbuf_put_2(pb, 0);
194
195 outbuf_enqueue(p9p, pb, pcc, tag);
196 puffs_cc_yield(pcc);
197
198 rv = proto_expect_walk_nqids(pb, &qids);
199 if (rv)
200 error = rv;
201 if (qids != 0)
202 error = EPROTO;
203
204 p9pbuf_destroy(pb);
205 return error;
206 }
207
208 int
209 proto_cc_clunkfid(struct puffs_cc *pcc, p9pfid_t fid, int waitforit)
210 {
211 struct puffs9p *p9p = puffs_cc_getspecific(pcc);
212 struct p9pbuf *pb;
213 p9ptag_t tag = NEXTTAG(p9p);
214 int error = 0;
215
216 pb = p9pbuf_make(p9p->maxreq, P9PB_OUT);
217 p9pbuf_put_1(pb, P9PROTO_T_CLUNK);
218 p9pbuf_put_2(pb, tag);
219 p9pbuf_put_4(pb, fid);
220
221 if (waitforit) {
222 outbuf_enqueue(p9p, pb, pcc, tag);
223 puffs_cc_yield(pcc);
224 if (pb->type != P9PROTO_R_CLUNK)
225 error = EPROTO;
226 p9pbuf_destroy(pb);
227 } else {
228 outbuf_enqueue_nocc(p9p, pb, NULL, NULL, tag);
229 }
230
231 return error;
232 }
233
234 /*
235 * walk a new fid, then open it
236 */
237 int
238 proto_cc_open(struct puffs_cc *pcc, p9pfid_t fid, p9pfid_t newfid, int mode)
239 {
240 struct puffs9p *p9p = puffs_cc_getspecific(pcc);
241 struct p9pbuf *pb;
242 p9ptag_t tag = NEXTTAG(p9p);
243 int error;
244
245 error = proto_cc_dupfid(pcc, fid, newfid);
246 if (error)
247 return error;
248
249 pb = p9pbuf_make(p9p->maxreq, P9PB_OUT);
250 p9pbuf_put_1(pb, P9PROTO_T_OPEN);
251 p9pbuf_put_2(pb, tag);
252 p9pbuf_put_4(pb, newfid);
253 p9pbuf_put_1(pb, mode);
254 outbuf_enqueue(p9p, pb, pcc, tag);
255 puffs_cc_yield(pcc);
256 if (pb->type != P9PROTO_R_OPEN)
257 error = EPROTO;
258
259 p9pbuf_destroy(pb);
260 return error;
261 }
262
263 void
264 proto_make_stat(struct p9pbuf *pb, const struct vattr *vap,
265 const char *filename)
266 {
267 struct vattr fakeva;
268 uint32_t mode, atime, mtime;
269 uint64_t flen;
270 const char *owner, *group;
271 int startoff, curoff;
272
273 if (vap == NULL) {
274 puffs_vattr_null(&fakeva);
275 vap = &fakeva;
276 }
277
278 startoff = p9pbuf_tell(pb);
279 p9pbuf_seekset(pb, startoff + 2 + 2); /* stat[n], containing stat[2] */
280
281 if (vap->va_mode != (mode_t)PUFFS_VNOVAL)
282 mode = vap->va_mode;
283 else
284 mode = P9PROTO_STAT_NOVAL4;
285 if (vap->va_atime.tv_sec != (time_t)PUFFS_VNOVAL)
286 atime = vap->va_atime.tv_sec;
287 else
288 atime = P9PROTO_STAT_NOVAL4;
289 if (vap->va_mtime.tv_sec != (time_t)PUFFS_VNOVAL)
290 mtime = vap->va_mtime.tv_sec;
291 else
292 mtime = P9PROTO_STAT_NOVAL4;
293 if (vap->va_size != (u_quad_t)PUFFS_VNOVAL)
294 flen = vap->va_size;
295 else
296 flen = P9PROTO_STAT_NOVAL8;
297 if (vap->va_uid != (uid_t)PUFFS_VNOVAL)
298 owner = uid2ustr(vap->va_uid);
299 else
300 owner = "";
301 if (vap->va_gid != (gid_t)PUFFS_VNOVAL)
302 group = gid2gstr(vap->va_gid);
303 else
304 group = "";
305
306 p9pbuf_put_2(pb, P9PROTO_STAT_NOVAL2); /* kernel type */
307 p9pbuf_put_4(pb, P9PROTO_STAT_NOVAL4); /* dev */
308 p9pbuf_put_1(pb, P9PROTO_STAT_NOVAL1); /* type */
309 p9pbuf_put_4(pb, P9PROTO_STAT_NOVAL4); /* version */
310 p9pbuf_put_8(pb, P9PROTO_STAT_NOVAL8); /* path */
311 p9pbuf_put_4(pb, mode);
312 p9pbuf_put_4(pb, atime);
313 p9pbuf_put_4(pb, mtime);
314 p9pbuf_put_8(pb, flen);
315 p9pbuf_put_str(pb, filename ? filename : "");
316 p9pbuf_put_str(pb, owner);
317 p9pbuf_put_str(pb, group);
318 p9pbuf_put_str(pb, ""); /* muid */
319
320 curoff = p9pbuf_tell(pb);
321 p9pbuf_seekset(pb, startoff);
322 p9pbuf_put_2(pb, curoff-(startoff+2)); /* stat[n] size */
323 p9pbuf_put_2(pb, curoff-(startoff+4)); /* size[2] stat */
324
325 p9pbuf_seekset(pb, curoff);
326 }
327
328 int
329 proto_expect_walk_nqids(struct p9pbuf *pb, uint16_t *nqids)
330 {
331
332 if (pb->type != P9PROTO_R_WALK)
333 return EPROTO;
334 if (!p9pbuf_get_2(pb, nqids))
335 return EPROTO;
336
337 return 0;
338 }
339
340 int
341 proto_expect_qid(struct p9pbuf *pb, uint8_t op, struct qid9p *qid)
342 {
343
344 if (pb->type != op)
345 return EPROTO;
346 if (!proto_getqid(pb, qid))
347 return EPROTO;
348
349 return 0;
350 }
351
352 int
353 proto_expect_stat(struct p9pbuf *pb, struct vattr *va)
354 {
355 uint16_t dummy;
356
357 if (pb->type != P9PROTO_R_STAT)
358 return EPROTO;
359 if (!p9pbuf_get_2(pb, &dummy))
360 return EPROTO;
361 if (!proto_getstat(pb, va, NULL, NULL))
362 return EPROTO;
363
364 return 0;
365 }
366