1 /*
2  * decode_mountd.c
3  *
4  * RPC mountd.
5  *
6  * Outputs filehandle in nfsshell format. :-)
7  *
8  * Copyright (c) 2000 Dug Song <dugsong@monkey.org>
9  *
10  * $Id: decode_mountd.c,v 1.7 2001/03/15 08:33:01 dugsong Exp $
11  */
12 
13 #include "config.h"
14 
15 #include <sys/param.h>
16 #include <sys/types.h>
17 #include <rpc/rpc.h>
18 
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 
23 #include "buf.h"
24 #include "mount.h"
25 #include "rpc.h"
26 #include "decode.h"
27 
28 int
decode_mountd(u_char * buf,int len,u_char * obuf,int olen)29 decode_mountd(u_char *buf, int len, u_char *obuf, int olen)
30 {
31 	XDR xdrs;
32 	struct buf outbuf;
33 	struct rpc_msg msg;
34 	struct xid_map *xm;
35 	struct fhstatus fhstat;
36 	char *p, *dir;
37 	int i, hdrlen;
38 
39 	buf_init(&outbuf, obuf, olen);
40 
41 	if ((hdrlen = rpc_decode(buf, len, &msg)) == 0)
42 		return (0);
43 
44 	if (msg.rm_direction == CALL &&
45 	    msg.rm_call.cb_prog == MOUNTPROG &&
46 	    msg.rm_call.cb_proc == MOUNTPROC_MNT) {
47 		xdrmem_create(&xdrs, buf + hdrlen, len - hdrlen, XDR_DECODE);
48 		dir = NULL;
49 		if (xdr_string(&xdrs, &dir, MAXPATHLEN)) {
50 			xid_map_enter(msg.rm_xid, MOUNTPROG, MOUNTVERS,
51 				      MOUNTPROC_MNT, (void *) dir);
52 		}
53 		xdr_destroy(&xdrs);
54 	}
55 	else if (msg.rm_direction == REPLY &&
56 		 (xm = xid_map_find(msg.rm_xid)) != NULL) {
57 		if (msg.rm_reply.rp_stat == MSG_ACCEPTED &&
58 		    msg.acpted_rply.ar_stat == SUCCESS) {
59 			xdrmem_create(&xdrs, buf + hdrlen, len - hdrlen,
60 				      XDR_DECODE);
61 			if (xdr_fhstatus(&xdrs, &fhstat)) {
62 				if (fhstat.fhs_status == 0) {
63 					buf_putf(&outbuf, "%s [",
64 						 (char *)xm->data);
65 
66 					p = fhstat.fhstatus_u.fhs_fhandle;
67 
68 					for (i = 0; i < FHSIZE; i++) {
69 						buf_putf(&outbuf, "%.2x ",
70 							 p[i] & 0xff);
71 					}
72 					buf_put(&outbuf, "]\n", 2);
73 				}
74 			}
75 			xdr_destroy(&xdrs);
76 		}
77 		free(xm->data);
78 		memset(xm, 0, sizeof(*xm));
79 	}
80 	buf_end(&outbuf);
81 
82 	return (buf_len(&outbuf));
83 }
84