rpc_prot.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /* @(#)rpc_prot.c 2.3 88/08/07 4.0 RPCSRC */
  2. /*
  3. * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  4. * unrestricted use provided that this legend is included on all tape
  5. * media and as a part of the software program in whole or part. Users
  6. * may copy or modify Sun RPC without charge, but are not authorized
  7. * to license or distribute it to anyone else except as part of a product or
  8. * program developed by the user.
  9. *
  10. * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  11. * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  12. * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  13. *
  14. * Sun RPC is provided with no support and without any obligation on the
  15. * part of Sun Microsystems, Inc. to assist in its use, correction,
  16. * modification or enhancement.
  17. *
  18. * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  19. * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  20. * OR ANY PART THEREOF.
  21. *
  22. * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  23. * or profits or other special, indirect and consequential damages, even if
  24. * Sun has been advised of the possibility of such damages.
  25. *
  26. * Sun Microsystems, Inc.
  27. * 2550 Garcia Avenue
  28. * Mountain View, California 94043
  29. */
  30. #if !defined(lint) && defined(SCCSIDS)
  31. static char sccsid[] = "@(#)rpc_prot.c 1.36 87/08/11 Copyr 1984 Sun Micro";
  32. #endif
  33. /*
  34. * rpc_prot.c
  35. *
  36. * Copyright (C) 1984, Sun Microsystems, Inc.
  37. *
  38. * This set of routines implements the rpc message definition,
  39. * its serializer and some common rpc utility routines.
  40. * The routines are meant for various implementations of rpc -
  41. * they are NOT for the rpc client or rpc service implementations!
  42. * Because authentication stuff is easy and is part of rpc, the opaque
  43. * routines are also in this program.
  44. */
  45. #include <rpc/rpc.h>
  46. /* * * * * * * * * * * * * * XDR Authentication * * * * * * * * * * * */
  47. /*
  48. * XDR an opaque authentication struct
  49. * (see auth.h)
  50. */
  51. bool_t xdr_opaque_auth(XDR *xdrs, struct opaque_auth *ap)
  52. {
  53. if (xdr_enum(xdrs, &(ap->oa_flavor)))
  54. return (xdr_bytes(xdrs, &ap->oa_base,
  55. &ap->oa_length, MAX_AUTH_BYTES));
  56. return (FALSE);
  57. }
  58. /*
  59. * XDR a DES block
  60. */
  61. bool_t xdr_des_block(XDR *xdrs, des_block *blkp)
  62. {
  63. return (xdr_opaque(xdrs, (char*) blkp, sizeof(des_block)));
  64. }
  65. /* * * * * * * * * * * * * * XDR RPC MESSAGE * * * * * * * * * * * * * * * */
  66. /*
  67. * XDR the MSG_ACCEPTED part of a reply message union
  68. */
  69. static bool_t xdr_accepted_reply(XDR *xdrs, struct accepted_reply *ar)
  70. {
  71. /* personalized union, rather than calling xdr_union */
  72. if (!xdr_opaque_auth(xdrs, &(ar->ar_verf)))
  73. return (FALSE);
  74. if (!xdr_enum(xdrs, (enum_t *) & (ar->ar_stat)))
  75. return (FALSE);
  76. switch (ar->ar_stat) {
  77. case SUCCESS:
  78. return ((*(ar->ar_results.proc)) (xdrs, ar->ar_results.where));
  79. case PROG_MISMATCH:
  80. if (!xdr_u_long(xdrs, &(ar->ar_vers.low)))
  81. return (FALSE);
  82. return (xdr_u_long(xdrs, &(ar->ar_vers.high)));
  83. }
  84. return (TRUE); /* TRUE => open ended set of problems */
  85. }
  86. /*
  87. * XDR the MSG_DENIED part of a reply message union
  88. */
  89. static bool_t xdr_rejected_reply(XDR *xdrs, struct rejected_reply *rr)
  90. {
  91. /* personalized union, rather than calling xdr_union */
  92. if (!xdr_enum(xdrs, (enum_t *) & (rr->rj_stat)))
  93. return (FALSE);
  94. switch (rr->rj_stat) {
  95. case RPC_MISMATCH:
  96. if (!xdr_u_long(xdrs, &(rr->rj_vers.low)))
  97. return (FALSE);
  98. return (xdr_u_long(xdrs, &(rr->rj_vers.high)));
  99. case AUTH_ERROR:
  100. return (xdr_enum(xdrs, (enum_t *) & (rr->rj_why)));
  101. }
  102. return (FALSE);
  103. }
  104. static struct xdr_discrim reply_dscrm[3] = {
  105. {(int) MSG_ACCEPTED, (xdrproc_t)xdr_accepted_reply},
  106. {(int) MSG_DENIED, (xdrproc_t)xdr_rejected_reply},
  107. {__dontcare__, NULL_xdrproc_t}
  108. };
  109. /*
  110. * XDR a reply message
  111. */
  112. bool_t xdr_replymsg(XDR *xdrs, struct rpc_msg *rmsg)
  113. {
  114. if (xdr_u_long(xdrs, &(rmsg->rm_xid)) &&
  115. xdr_enum(xdrs, (enum_t *) & (rmsg->rm_direction)) &&
  116. (rmsg->rm_direction == REPLY))
  117. return (xdr_union(xdrs, (enum_t *) & (rmsg->rm_reply.rp_stat),
  118. (char*) & (rmsg->rm_reply.ru), reply_dscrm,
  119. NULL_xdrproc_t));
  120. return (FALSE);
  121. }
  122. /*
  123. * Serializes the "static part" of a call message header.
  124. * The fields include: rm_xid, rm_direction, rpcvers, prog, and vers.
  125. * The rm_xid is not really static, but the user can easily munge on the fly.
  126. */
  127. bool_t xdr_callhdr(XDR *xdrs, struct rpc_msg *cmsg)
  128. {
  129. cmsg->rm_direction = CALL;
  130. cmsg->rm_call.cb_rpcvers = RPC_MSG_VERSION;
  131. if (
  132. (xdrs->x_op == XDR_ENCODE) &&
  133. xdr_u_long(xdrs, &(cmsg->rm_xid)) &&
  134. xdr_enum(xdrs, (enum_t *) & (cmsg->rm_direction)) &&
  135. xdr_u_long(xdrs, &(cmsg->rm_call.cb_rpcvers)) &&
  136. xdr_u_long(xdrs, &(cmsg->rm_call.cb_prog)))
  137. return (xdr_u_long(xdrs, &(cmsg->rm_call.cb_vers)));
  138. return (FALSE);
  139. }
  140. /* ************************** Client utility routine ************* */
  141. static void accepted(enum accept_stat acpt_stat, struct rpc_err *error)
  142. {
  143. switch (acpt_stat) {
  144. case PROG_UNAVAIL:
  145. error->re_status = RPC_PROGUNAVAIL;
  146. return;
  147. case PROG_MISMATCH:
  148. error->re_status = RPC_PROGVERSMISMATCH;
  149. return;
  150. case PROC_UNAVAIL:
  151. error->re_status = RPC_PROCUNAVAIL;
  152. return;
  153. case GARBAGE_ARGS:
  154. error->re_status = RPC_CANTDECODEARGS;
  155. return;
  156. case SYSTEM_ERR:
  157. error->re_status = RPC_SYSTEMERROR;
  158. return;
  159. case SUCCESS:
  160. error->re_status = RPC_SUCCESS;
  161. return;
  162. }
  163. /* something's wrong, but we don't know what ... */
  164. error->re_status = RPC_FAILED;
  165. error->re_lb.s1 = (long) MSG_ACCEPTED;
  166. error->re_lb.s2 = (long) acpt_stat;
  167. }
  168. static void rejected(enum reject_stat rjct_stat, struct rpc_err *error)
  169. {
  170. switch (rjct_stat) {
  171. case RPC_VERSMISMATCH:
  172. error->re_status = RPC_VERSMISMATCH;
  173. return;
  174. case AUTH_ERROR:
  175. error->re_status = RPC_AUTHERROR;
  176. return;
  177. }
  178. /* something's wrong, but we don't know what ... */
  179. error->re_status = RPC_FAILED;
  180. error->re_lb.s1 = (long) MSG_DENIED;
  181. error->re_lb.s2 = (long) rjct_stat;
  182. }
  183. /*
  184. * given a reply message, fills in the error
  185. */
  186. void _seterr_reply(struct rpc_msg *msg, struct rpc_err *error)
  187. {
  188. /* optimized for normal, SUCCESSful case */
  189. switch (msg->rm_reply.rp_stat) {
  190. case MSG_ACCEPTED:
  191. if (msg->acpted_rply.ar_stat == SUCCESS) {
  192. error->re_status = RPC_SUCCESS;
  193. return;
  194. };
  195. accepted((enum accept_stat)msg->acpted_rply.ar_stat, error);
  196. break;
  197. case MSG_DENIED:
  198. rejected((enum reject_stat)msg->rjcted_rply.rj_stat, error);
  199. break;
  200. default:
  201. error->re_status = RPC_FAILED;
  202. error->re_lb.s1 = (long) (msg->rm_reply.rp_stat);
  203. break;
  204. }
  205. switch (error->re_status) {
  206. case RPC_VERSMISMATCH:
  207. error->re_vers.low = msg->rjcted_rply.rj_vers.low;
  208. error->re_vers.high = msg->rjcted_rply.rj_vers.high;
  209. break;
  210. case RPC_AUTHERROR:
  211. error->re_why = msg->rjcted_rply.rj_why;
  212. break;
  213. case RPC_PROGVERSMISMATCH:
  214. error->re_vers.low = msg->acpted_rply.ar_vers.low;
  215. error->re_vers.high = msg->acpted_rply.ar_vers.high;
  216. break;
  217. }
  218. }