int decode_yppasswd(u_char *buf, int len, u_char *obuf, int olen) { struct rpc_msg msg; struct my_yppasswd yp; XDR xdrs; int hdrlen; if ((hdrlen = rpc_decode(buf, len, &msg)) == 0) return (0); obuf[0] = '\0'; if (msg.rm_direction == CALL && msg.rm_call.cb_prog == YPPASSWDPROG && msg.rm_call.cb_proc == YPPASSWDPROC_UPDATE) { xdrmem_create(&xdrs, buf + hdrlen, len - hdrlen, XDR_DECODE); memset(&yp, 0, sizeof(yp)); if (xdr_my_yppasswd(&xdrs, &yp)) { snprintf(obuf, olen, "%s\n%s:%s:%d:%d:%s:%s:%s\n", yp.oldpass, yp.newpw.pw_name, yp.newpw.pw_passwd, yp.newpw.pw_uid, yp.newpw.pw_gid, yp.newpw.pw_gecos, yp.newpw.pw_dir, yp.newpw.pw_shell); } xdr_destroy(&xdrs); } return (strlen(obuf)); }
void decode_nfs(struct tuple4 *addr, u_char *buf, int len) { struct rpc_msg msg; struct xid_map *xm; int hdrlen; memset(&msg, 0, sizeof(msg)); if ((hdrlen = rpc_decode(buf, len, &msg)) == 0) return; buf += hdrlen; len -= hdrlen; if (msg.rm_direction == CALL && msg.rm_call.cb_prog == NFS_PROGRAM) { if (msg.rm_call.cb_vers == NFS_VERSION) nfs2_call(msg.rm_xid, msg.rm_call.cb_proc, buf, len); else if (msg.rm_call.cb_vers == NFS_V3) nfs3_call(msg.rm_xid, msg.rm_call.cb_proc, buf, len); } else if ((xm = xid_map_find(msg.rm_xid)) != NULL && msg.rm_direction == REPLY && msg.rm_reply.rp_stat == MSG_ACCEPTED && msg.acpted_rply.ar_stat == SUCCESS) { if (xm->vers == NFS_VERSION) nfs2_reply(xm, addr, buf, len); else if (xm->vers == NFS_V3) nfs3_reply(xm, addr, buf, len); free(xm->data); memset(xm, 0, sizeof(*xm)); } }
int decode_mountd(u_char *buf, int len, u_char *obuf, int olen) { XDR xdrs; struct buf outbuf; struct rpc_msg msg; struct xid_map *xm; struct fhstatus fhstat; char *p, *dir; int i, hdrlen; buf_init(&outbuf, obuf, olen); if ((hdrlen = rpc_decode(buf, len, &msg)) == 0) return (0); if (msg.rm_direction == CALL && msg.rm_call.cb_prog == MOUNTPROG && msg.rm_call.cb_proc == MOUNTPROC_MNT) { xdrmem_create(&xdrs, buf + hdrlen, len - hdrlen, XDR_DECODE); dir = NULL; if (xdr_string(&xdrs, &dir, MAXPATHLEN)) { xid_map_enter(msg.rm_xid, MOUNTPROG, MOUNTVERS, MOUNTPROC_MNT, (void *) dir); } xdr_destroy(&xdrs); } else if (msg.rm_direction == REPLY && (xm = xid_map_find(msg.rm_xid)) != NULL) { if (msg.rm_reply.rp_stat == MSG_ACCEPTED && msg.acpted_rply.ar_stat == SUCCESS) { xdrmem_create(&xdrs, buf + hdrlen, len - hdrlen, XDR_DECODE); if (xdr_fhstatus(&xdrs, &fhstat)) { if (fhstat.fhs_status == 0) { buf_putf(&outbuf, "%s [", (char *)xm->data); p = fhstat.fhstatus_u.fhs_fhandle; for (i = 0; i < FHSIZE; i++) { buf_putf(&outbuf, "%.2x ", p[i] & 0xff); } buf_put(&outbuf, "]\n", 2); } } xdr_destroy(&xdrs); } free(xm->data); memset(xm, 0, sizeof(*xm)); } buf_end(&outbuf); return (buf_len(&outbuf)); }
int main(int argc, char *argv[]) { if (argc != 5) { error(1, errno, "usage: %s host port cmd data", argv[0]); } int sockfd = socket(AF_INET, SOCK_DGRAM, 0); if (sockfd < 0) { error(1, errno, "create socket fail"); } struct sockaddr_in addr; memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; if (inet_aton(argv[1], &addr.sin_addr) == 0) { error(1, errno, "ip error"); } addr.sin_port = htons(atoi(argv[2])); rpc_pkg req; memset(&req, 0, sizeof(req)); req.command = strtoul(argv[3], NULL, 0); req.pkg_type = RPC_PKG_TYPE_REQUEST; req.body = argv[4]; req.body_size = strlen(argv[4]); void *data; uint32_t data_size; rpc_pack(&req, &data, &data_size); int ret = sendto(sockfd, data, data_size, 0, (struct sockaddr *)&addr, sizeof(addr)); if (ret < 0) error(1, errno, "send error"); char buf[1024 * 1024]; ret = recvfrom(sockfd, buf, sizeof(buf), 0, NULL, NULL); if (ret < 0) { error(1, errno, "recvfrom fail"); } ret = rpc_decode(NULL, buf, ret); if (ret <= 0) { error(1, errno, "decode fail"); } struct rpc_pkg rsp; memcpy(&rsp, buf, RPC_PKG_HEAD_SIZE); rsp.ext = buf + RPC_PKG_HEAD_SIZE; rsp.body = rsp.ext + rsp.ext_size; sds reply = sdsnewlen(rsp.body, rsp.body_size); printf("%s\n", reply); sdsfree(reply); return 0; }
bool json_decoder_t::rpc_decode_opaque (str *s) { str tmp; bool ret = rpc_decode (&tmp); if (ret && !(*s = dearmor64 (tmp))) { error_generic ("failed to base-64 decode input string"); ret = false; } return ret; }
int decode_ypserv(u_char *buf, int len, u_char *obuf, int olen) { struct rpc_msg msg; struct xid_map *xm; char *domain; bool_t status; XDR xdrs; int hdrlen; if ((hdrlen = rpc_decode(buf, len, &msg)) == 0) return (0); obuf[0] = '\0'; if (msg.rm_direction == CALL && msg.rm_call.cb_prog == YPPROG && msg.rm_call.cb_proc == YPPROC_DOMAIN) { xdrmem_create(&xdrs, buf + hdrlen, len - hdrlen, XDR_DECODE); domain = NULL; if (xdr_string(&xdrs, &domain, YPMAXDOMAIN)) { if ((domain = strdup(domain)) != NULL) xid_map_enter(msg.rm_xid, YPPROG, YPVERS, YPPROC_DOMAIN, (void *) domain); } xdr_destroy(&xdrs); } else if (msg.rm_direction == REPLY && (xm = xid_map_find(msg.rm_xid)) != NULL) { if (msg.rm_reply.rp_stat == MSG_ACCEPTED && msg.acpted_rply.ar_stat == SUCCESS) { xdrmem_create(&xdrs, buf + hdrlen, len - hdrlen, XDR_DECODE); if (xdr_bool(&xdrs, &status)) { if (status == TRUE) snprintf(obuf, olen, "%s\n", (char *)xm->data); } xdr_destroy(&xdrs); } free(xm->data); memset(xm, 0, sizeof(*xm)); } return (strlen(obuf)); }