rpc_prot.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * rpc_prot.c
  3. *
  4. * Copyright (c) 2010, Oracle America, Inc.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following
  14. * disclaimer in the documentation and/or other materials
  15. * provided with the distribution.
  16. * * Neither the name of the "Oracle America, Inc." nor the names of its
  17. * contributors may be used to endorse or promote products derived
  18. * from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  25. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  27. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  28. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  29. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  30. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. *
  33. * This set of routines implements the rpc message definition,
  34. * its serializer and some common rpc utility routines.
  35. * The routines are meant for various implementations of rpc -
  36. * they are NOT for the rpc client or rpc service implementations!
  37. * Because authentication stuff is easy and is part of rpc, the opaque
  38. * routines are also in this program.
  39. */
  40. #include <sys/param.h>
  41. #include <rpc/rpc.h>
  42. #include <shlib-compat.h>
  43. /* * * * * * * * * * * * * * XDR Authentication * * * * * * * * * * * */
  44. /*
  45. * XDR an opaque authentication struct
  46. * (see auth.h)
  47. */
  48. bool_t
  49. xdr_opaque_auth (XDR *xdrs, struct opaque_auth *ap)
  50. {
  51. if (xdr_enum (xdrs, &(ap->oa_flavor)))
  52. return xdr_bytes (xdrs, &ap->oa_base,
  53. &ap->oa_length, MAX_AUTH_BYTES);
  54. return FALSE;
  55. }
  56. libc_hidden_nolink_sunrpc (xdr_opaque_auth, GLIBC_2_0)
  57. /*
  58. * XDR a DES block
  59. */
  60. bool_t
  61. xdr_des_block (XDR *xdrs, des_block *blkp)
  62. {
  63. return xdr_opaque (xdrs, (caddr_t) blkp, sizeof (des_block));
  64. }
  65. libc_hidden_nolink_sunrpc (xdr_des_block, GLIBC_2_0)
  66. /* * * * * * * * * * * * * * XDR RPC MESSAGE * * * * * * * * * * * * * * * */
  67. /*
  68. * XDR the MSG_ACCEPTED part of a reply message union
  69. */
  70. bool_t
  71. xdr_accepted_reply (XDR *xdrs, struct accepted_reply *ar)
  72. {
  73. /* personalized union, rather than calling xdr_union */
  74. if (!xdr_opaque_auth (xdrs, &(ar->ar_verf)))
  75. return FALSE;
  76. if (!xdr_enum (xdrs, (enum_t *) & (ar->ar_stat)))
  77. return FALSE;
  78. switch (ar->ar_stat)
  79. {
  80. case SUCCESS:
  81. return ((*(ar->ar_results.proc)) (xdrs, ar->ar_results.where));
  82. case PROG_MISMATCH:
  83. if (!xdr_u_long (xdrs, &(ar->ar_vers.low)))
  84. return FALSE;
  85. return (xdr_u_long (xdrs, &(ar->ar_vers.high)));
  86. default:
  87. return TRUE;
  88. }
  89. return TRUE; /* TRUE => open ended set of problems */
  90. }
  91. libc_hidden_nolink_sunrpc (xdr_accepted_reply, GLIBC_2_0)
  92. /*
  93. * XDR the MSG_DENIED part of a reply message union
  94. */
  95. bool_t
  96. xdr_rejected_reply (XDR *xdrs, struct rejected_reply *rr)
  97. {
  98. /* personalized union, rather than calling xdr_union */
  99. if (!xdr_enum (xdrs, (enum_t *) & (rr->rj_stat)))
  100. return FALSE;
  101. switch (rr->rj_stat)
  102. {
  103. case RPC_MISMATCH:
  104. if (!xdr_u_long (xdrs, &(rr->rj_vers.low)))
  105. return FALSE;
  106. return xdr_u_long (xdrs, &(rr->rj_vers.high));
  107. case AUTH_ERROR:
  108. return xdr_enum (xdrs, (enum_t *) & (rr->rj_why));
  109. }
  110. return FALSE;
  111. }
  112. libc_hidden_nolink_sunrpc (xdr_rejected_reply, GLIBC_2_0)
  113. static const struct xdr_discrim reply_dscrm[3] =
  114. {
  115. {(int) MSG_ACCEPTED, (xdrproc_t) xdr_accepted_reply},
  116. {(int) MSG_DENIED, (xdrproc_t) xdr_rejected_reply},
  117. {__dontcare__, NULL_xdrproc_t}};
  118. /*
  119. * XDR a reply message
  120. */
  121. bool_t
  122. xdr_replymsg (XDR *xdrs, struct rpc_msg *rmsg)
  123. {
  124. if (xdr_u_long (xdrs, &(rmsg->rm_xid)) &&
  125. xdr_enum (xdrs, (enum_t *) & (rmsg->rm_direction)) &&
  126. (rmsg->rm_direction == REPLY))
  127. return xdr_union (xdrs, (enum_t *) & (rmsg->rm_reply.rp_stat),
  128. (caddr_t) & (rmsg->rm_reply.ru), reply_dscrm,
  129. NULL_xdrproc_t);
  130. return FALSE;
  131. }
  132. libc_hidden_nolink_sunrpc (xdr_replymsg, GLIBC_2_0)
  133. /*
  134. * Serializes the "static part" of a call message header.
  135. * The fields include: rm_xid, rm_direction, rpcvers, prog, and vers.
  136. * The rm_xid is not really static, but the user can easily munge on the fly.
  137. */
  138. bool_t
  139. xdr_callhdr (XDR *xdrs, struct rpc_msg *cmsg)
  140. {
  141. cmsg->rm_direction = CALL;
  142. cmsg->rm_call.cb_rpcvers = RPC_MSG_VERSION;
  143. if (
  144. (xdrs->x_op == XDR_ENCODE) &&
  145. xdr_u_long (xdrs, &(cmsg->rm_xid)) &&
  146. xdr_enum (xdrs, (enum_t *) & (cmsg->rm_direction)) &&
  147. xdr_u_long (xdrs, &(cmsg->rm_call.cb_rpcvers)) &&
  148. xdr_u_long (xdrs, &(cmsg->rm_call.cb_prog)))
  149. return xdr_u_long (xdrs, &(cmsg->rm_call.cb_vers));
  150. return FALSE;
  151. }
  152. libc_hidden_nolink_sunrpc (xdr_callhdr, GLIBC_2_0)
  153. /* ************************** Client utility routine ************* */
  154. static void
  155. accepted (enum accept_stat acpt_stat,
  156. struct rpc_err *error)
  157. {
  158. switch (acpt_stat)
  159. {
  160. case PROG_UNAVAIL:
  161. error->re_status = RPC_PROGUNAVAIL;
  162. return;
  163. case PROG_MISMATCH:
  164. error->re_status = RPC_PROGVERSMISMATCH;
  165. return;
  166. case PROC_UNAVAIL:
  167. error->re_status = RPC_PROCUNAVAIL;
  168. return;
  169. case GARBAGE_ARGS:
  170. error->re_status = RPC_CANTDECODEARGS;
  171. return;
  172. case SYSTEM_ERR:
  173. error->re_status = RPC_SYSTEMERROR;
  174. return;
  175. case SUCCESS:
  176. error->re_status = RPC_SUCCESS;
  177. return;
  178. }
  179. /* something's wrong, but we don't know what ... */
  180. error->re_status = RPC_FAILED;
  181. error->re_lb.s1 = (long) MSG_ACCEPTED;
  182. error->re_lb.s2 = (long) acpt_stat;
  183. }
  184. static void
  185. rejected (enum reject_stat rjct_stat,
  186. struct rpc_err *error)
  187. {
  188. switch (rjct_stat)
  189. {
  190. case RPC_MISMATCH:
  191. error->re_status = RPC_VERSMISMATCH;
  192. return;
  193. case AUTH_ERROR:
  194. error->re_status = RPC_AUTHERROR;
  195. return;
  196. default:
  197. /* something's wrong, but we don't know what ... */
  198. error->re_status = RPC_FAILED;
  199. error->re_lb.s1 = (long) MSG_DENIED;
  200. error->re_lb.s2 = (long) rjct_stat;
  201. return;
  202. }
  203. }
  204. /*
  205. * given a reply message, fills in the error
  206. */
  207. void
  208. _seterr_reply (struct rpc_msg *msg,
  209. struct rpc_err *error)
  210. {
  211. /* optimized for normal, SUCCESSful case */
  212. switch (msg->rm_reply.rp_stat)
  213. {
  214. case MSG_ACCEPTED:
  215. if (msg->acpted_rply.ar_stat == SUCCESS)
  216. {
  217. error->re_status = RPC_SUCCESS;
  218. return;
  219. };
  220. accepted (msg->acpted_rply.ar_stat, error);
  221. break;
  222. case MSG_DENIED:
  223. rejected (msg->rjcted_rply.rj_stat, error);
  224. break;
  225. default:
  226. error->re_status = RPC_FAILED;
  227. error->re_lb.s1 = (long) (msg->rm_reply.rp_stat);
  228. break;
  229. }
  230. switch (error->re_status)
  231. {
  232. case RPC_VERSMISMATCH:
  233. error->re_vers.low = msg->rjcted_rply.rj_vers.low;
  234. error->re_vers.high = msg->rjcted_rply.rj_vers.high;
  235. break;
  236. case RPC_AUTHERROR:
  237. error->re_why = msg->rjcted_rply.rj_why;
  238. break;
  239. case RPC_PROGVERSMISMATCH:
  240. error->re_vers.low = msg->acpted_rply.ar_vers.low;
  241. error->re_vers.high = msg->acpted_rply.ar_vers.high;
  242. break;
  243. default:
  244. break;
  245. }
  246. }
  247. libc_hidden_nolink_sunrpc (_seterr_reply, GLIBC_2_0)