| 1 | /* |
| 2 | Copyright (C) 2010 by Ronnie Sahlberg <ronniesahlberg@gmail.com> |
| 3 | |
| 4 | This program is free software; you can redistribute it and/or modify |
| 5 | it under the terms of the GNU Lesser General Public License as published by |
| 6 | the Free Software Foundation; either version 2.1 of the License, or |
| 7 | (at your option) any later version. |
| 8 | |
| 9 | This program is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | GNU Lesser General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Lesser General Public License |
| 15 | along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | |
| 18 | #include <stdio.h> |
| 19 | #include <strings.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <rpc/rpc.h> |
| 22 | #include <rpc/xdr.h> |
| 23 | #include <rpc/rpc_msg.h> |
| 24 | #include "slist.h" |
| 25 | #include "libnfs.h" |
| 26 | #include "libnfs-raw.h" |
| 27 | #include "libnfs-private.h" |
| 28 | |
| 29 | struct rpc_pdu *rpc_allocate_pdu(struct rpc_context *rpc, int program, int version, int procedure, rpc_cb cb, void *private_data, xdrproc_t xdr_decode_fn, int xdr_decode_bufsize) |
| 30 | { |
| 31 | struct rpc_pdu *pdu; |
| 32 | struct rpc_msg msg; |
| 33 | |
| 34 | if (rpc == NULL) { |
| 35 | return NULL; |
| 36 | } |
| 37 | |
| 38 | pdu = malloc(sizeof(struct rpc_pdu)); |
| 39 | if (pdu == NULL) { |
| 40 | rpc_set_error(rpc, "Out of memory: Failed to allocate pdu structure"); |
| 41 | return NULL; |
| 42 | } |
| 43 | bzero(pdu, sizeof(struct rpc_pdu)); |
| 44 | pdu->xid = rpc->xid++; |
| 45 | pdu->cb = cb; |
| 46 | pdu->private_data = private_data; |
| 47 | pdu->xdr_decode_fn = xdr_decode_fn; |
| 48 | pdu->xdr_decode_bufsize = xdr_decode_bufsize; |
| 49 | |
| 50 | xdrmem_create(&pdu->xdr, rpc->encodebuf, rpc->encodebuflen, XDR_ENCODE); |
| 51 | xdr_setpos(&pdu->xdr, 4); /* skip past the record marker */ |
| 52 | |
| 53 | bzero(&msg, sizeof(struct rpc_msg)); |
| 54 | msg.rm_xid = pdu->xid; |
| 55 | msg.rm_direction = CALL; |
| 56 | msg.rm_call.cb_rpcvers = RPC_MSG_VERSION; |
| 57 | msg.rm_call.cb_prog = program; |
| 58 | msg.rm_call.cb_vers = version; |
| 59 | msg.rm_call.cb_proc = procedure; |
| 60 | msg.rm_call.cb_cred = rpc->auth->ah_cred; |
| 61 | msg.rm_call.cb_verf = rpc->auth->ah_verf; |
| 62 | |
| 63 | if (xdr_callmsg(&pdu->xdr, &msg) == 0) { |
| 64 | rpc_set_error(rpc, "xdr_callmsg failed"); |
| 65 | xdr_destroy(&pdu->xdr); |
| 66 | free(pdu); |
| 67 | return NULL; |
| 68 | } |
| 69 | |
| 70 | return pdu; |
| 71 | } |
| 72 | |
| 73 | void rpc_free_pdu(struct rpc_context *rpc _U_, struct rpc_pdu *pdu) |
| 74 | { |
| 75 | if (pdu->outdata.data != NULL) { |
| 76 | free(pdu->outdata.data); |
| 77 | pdu->outdata.data = NULL; |
| 78 | } |
| 79 | |
| 80 | if (pdu->xdr_decode_buf != NULL) { |
| 81 | xdr_free(pdu->xdr_decode_fn, pdu->xdr_decode_buf); |
| 82 | free(pdu->xdr_decode_buf); |
| 83 | pdu->xdr_decode_buf = NULL; |
| 84 | } |
| 85 | |
| 86 | xdr_destroy(&pdu->xdr); |
| 87 | free(pdu); |
| 88 | } |
| 89 | |
| 90 | |
| 91 | int rpc_queue_pdu(struct rpc_context *rpc, struct rpc_pdu *pdu) |
| 92 | { |
| 93 | int size, recordmarker; |
| 94 | |
| 95 | size = xdr_getpos(&pdu->xdr); |
| 96 | |
| 97 | /* write recordmarker */ |
| 98 | xdr_setpos(&pdu->xdr, 0); |
| 99 | recordmarker = (size - 4) | 0x80000000; |
| 100 | xdr_int(&pdu->xdr, &recordmarker); |
| 101 | |
| 102 | pdu->outdata.size = size; |
| 103 | pdu->outdata.data = malloc(pdu->outdata.size); |
| 104 | if (pdu->outdata.data == NULL) { |
| 105 | rpc_set_error(rpc, "Out of memory. Failed to allocate buffer for pdu\n"); |
| 106 | rpc_free_pdu(rpc, pdu); |
| 107 | return -1; |
| 108 | } |
| 109 | |
| 110 | memcpy(pdu->outdata.data, rpc->encodebuf, pdu->outdata.size); |
| 111 | SLIST_ADD_END(&rpc->outqueue, pdu); |
| 112 | |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | int rpc_get_pdu_size(char *buf) |
| 117 | { |
| 118 | uint32_t size; |
| 119 | |
| 120 | size = ntohl(*(uint32_t *)buf); |
| 121 | |
| 122 | if ((size & 0x80000000) == 0) { |
| 123 | /* cant handle oncrpc fragments */ |
| 124 | return -1; |
| 125 | } |
| 126 | |
| 127 | return (size & 0x7fffffff) + 4; |
| 128 | } |
| 129 | |
| 130 | static int rpc_process_reply(struct rpc_context *rpc, struct rpc_pdu *pdu, XDR *xdr) |
| 131 | { |
| 132 | struct rpc_msg msg; |
| 133 | |
| 134 | bzero(&msg, sizeof(struct rpc_msg)); |
| 135 | msg.acpted_rply.ar_verf = _null_auth; |
| 136 | if (pdu->xdr_decode_bufsize > 0) { |
| 137 | pdu->xdr_decode_buf = malloc(pdu->xdr_decode_bufsize); |
| 138 | if (pdu->xdr_decode_buf == NULL) { |
| 139 | rpc_set_error(rpc, "xdr_replymsg failed in portmap_getport_reply"); |
| 140 | pdu->cb(rpc, RPC_STATUS_ERROR, "Failed to allocate buffer for decoding of XDR reply", pdu->private_data); |
| 141 | return 0; |
| 142 | } |
| 143 | bzero(pdu->xdr_decode_buf, pdu->xdr_decode_bufsize); |
| 144 | } |
| 145 | msg.acpted_rply.ar_results.where = pdu->xdr_decode_buf; |
| 146 | msg.acpted_rply.ar_results.proc = pdu->xdr_decode_fn; |
| 147 | |
| 148 | if (xdr_replymsg(xdr, &msg) == 0) { |
| 149 | rpc_set_error(rpc, "xdr_replymsg failed in portmap_getport_reply"); |
| 150 | pdu->cb(rpc, RPC_STATUS_ERROR, "Message rejected by server", pdu->private_data); |
| 151 | if (pdu->xdr_decode_buf != NULL) { |
| 152 | free(pdu->xdr_decode_buf); |
| 153 | pdu->xdr_decode_buf = NULL; |
| 154 | } |
| 155 | return 0; |
| 156 | } |
| 157 | if (msg.rm_reply.rp_stat != MSG_ACCEPTED) { |
| 158 | pdu->cb(rpc, RPC_STATUS_ERROR, "RPC Packet not accepted by the server", pdu->private_data); |
| 159 | return 0; |
| 160 | } |
| 161 | switch (msg.rm_reply.rp_acpt.ar_stat) { |
| 162 | case SUCCESS: |
| 163 | pdu->cb(rpc, RPC_STATUS_SUCCESS, pdu->xdr_decode_buf, pdu->private_data); |
| 164 | break; |
| 165 | case PROG_UNAVAIL: |
| 166 | pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: Program not available", pdu->private_data); |
| 167 | break; |
| 168 | case PROG_MISMATCH: |
| 169 | pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: Program version mismatch", pdu->private_data); |
| 170 | break; |
| 171 | case PROC_UNAVAIL: |
| 172 | pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: Procedure not available", pdu->private_data); |
| 173 | break; |
| 174 | case GARBAGE_ARGS: |
| 175 | pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: Garbage arguments", pdu->private_data); |
| 176 | break; |
| 177 | case SYSTEM_ERR: |
| 178 | pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: System Error", pdu->private_data); |
| 179 | break; |
| 180 | default: |
| 181 | pdu->cb(rpc, RPC_STATUS_ERROR, "Unknown rpc response from server", pdu->private_data); |
| 182 | break; |
| 183 | } |
| 184 | |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | int rpc_process_pdu(struct rpc_context *rpc, char *buf, int size) |
| 189 | { |
| 190 | struct rpc_pdu *pdu; |
| 191 | XDR xdr; |
| 192 | int pos, recordmarker; |
| 193 | unsigned int xid; |
| 194 | |
| 195 | bzero(&xdr, sizeof(XDR)); |
| 196 | |
| 197 | xdrmem_create(&xdr, buf, size, XDR_DECODE); |
| 198 | if (xdr_int(&xdr, &recordmarker) == 0) { |
| 199 | rpc_set_error(rpc, "xdr_int reading recordmarker failed"); |
| 200 | xdr_destroy(&xdr); |
| 201 | return -1; |
| 202 | } |
| 203 | pos = xdr_getpos(&xdr); |
| 204 | if (xdr_int(&xdr, (int *)&xid) == 0) { |
| 205 | rpc_set_error(rpc, "xdr_int reading xid failed"); |
| 206 | xdr_destroy(&xdr); |
| 207 | return -1; |
| 208 | } |
| 209 | xdr_setpos(&xdr, pos); |
| 210 | |
| 211 | for (pdu=rpc->waitpdu; pdu; pdu=pdu->next) { |
| 212 | if (pdu->xid != xid) { |
| 213 | continue; |
| 214 | } |
| 215 | SLIST_REMOVE(&rpc->waitpdu, pdu); |
| 216 | if (rpc_process_reply(rpc, pdu, &xdr) != 0) { |
| 217 | rpc_set_error(rpc, "rpc_procdess_reply failed"); |
| 218 | } |
| 219 | xdr_destroy(&xdr); |
| 220 | rpc_free_pdu(rpc, pdu); |
| 221 | return 0; |
| 222 | } |
| 223 | rpc_set_error(rpc, "No matching pdu found for xid:%d", xid); |
| 224 | xdr_destroy(&xdr); |
| 225 | return -1; |
| 226 | } |
| 227 | |