rpc_prot.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. #define __FORCE_GLIBC__
  31. #include <features.h>
  32. /*
  33. * rpc_prot.c
  34. *
  35. * Copyright (C) 1984, Sun Microsystems, Inc.
  36. *
  37. * This set of routines implements the rpc message definition,
  38. * its serializer and some common rpc utility routines.
  39. * The routines are meant for various implementations of rpc -
  40. * they are NOT for the rpc client or rpc service implementations!
  41. * Because authentication stuff is easy and is part of rpc, the opaque
  42. * routines are also in this program.
  43. */
  44. #include <sys/param.h>
  45. #include <rpc/rpc.h>
  46. /* * * * * * * * * * * * * * XDR Authentication * * * * * * * * * * * */
  47. struct opaque_auth _null_auth;
  48. /*
  49. * XDR an opaque authentication struct
  50. * (see auth.h)
  51. */
  52. bool_t xdr_opaque_auth(xdrs, ap)
  53. register XDR *xdrs;
  54. register struct opaque_auth *ap;
  55. {
  56. if (xdr_enum(xdrs, &(ap->oa_flavor)))
  57. return (xdr_bytes(xdrs, &ap->oa_base,
  58. &ap->oa_length, MAX_AUTH_BYTES));
  59. return (FALSE);
  60. }
  61. /*
  62. * XDR a DES block
  63. */
  64. bool_t xdr_des_block(xdrs, blkp)
  65. register XDR *xdrs;
  66. register des_block *blkp;
  67. {
  68. return (xdr_opaque(xdrs, (caddr_t) blkp, sizeof(des_block)));
  69. }
  70. /* * * * * * * * * * * * * * XDR RPC MESSAGE * * * * * * * * * * * * * * * */
  71. /*
  72. * XDR the MSG_ACCEPTED part of a reply message union
  73. */
  74. bool_t xdr_accepted_reply(xdrs, ar)
  75. register XDR *xdrs;
  76. register struct accepted_reply *ar;
  77. {
  78. /* personalized union, rather than calling xdr_union */
  79. if (!xdr_opaque_auth(xdrs, &(ar->ar_verf)))
  80. return (FALSE);
  81. if (!xdr_enum(xdrs, (enum_t *) & (ar->ar_stat)))
  82. return (FALSE);
  83. switch (ar->ar_stat) {
  84. case SUCCESS:
  85. return ((*(ar->ar_results.proc)) (xdrs, ar->ar_results.where));
  86. case PROG_MISMATCH:
  87. if (!xdr_u_long(xdrs, &(ar->ar_vers.low)))
  88. return (FALSE);
  89. return (xdr_u_long(xdrs, &(ar->ar_vers.high)));
  90. }
  91. return (TRUE); /* TRUE => open ended set of problems */
  92. }
  93. /*
  94. * XDR the MSG_DENIED part of a reply message union
  95. */
  96. bool_t xdr_rejected_reply(xdrs, rr)
  97. register XDR *xdrs;
  98. register struct rejected_reply *rr;
  99. {
  100. /* personalized union, rather than calling xdr_union */
  101. if (!xdr_enum(xdrs, (enum_t *) & (rr->rj_stat)))
  102. return (FALSE);
  103. switch (rr->rj_stat) {
  104. case RPC_MISMATCH:
  105. if (!xdr_u_long(xdrs, &(rr->rj_vers.low)))
  106. return (FALSE);
  107. return (xdr_u_long(xdrs, &(rr->rj_vers.high)));
  108. case AUTH_ERROR:
  109. return (xdr_enum(xdrs, (enum_t *) & (rr->rj_why)));
  110. }
  111. return (FALSE);
  112. }
  113. static struct xdr_discrim reply_dscrm[3] = {
  114. {(int) MSG_ACCEPTED, xdr_accepted_reply},
  115. {(int) MSG_DENIED, xdr_rejected_reply},
  116. {__dontcare__, NULL_xdrproc_t}
  117. };
  118. /*
  119. * XDR a reply message
  120. */
  121. bool_t xdr_replymsg(xdrs, rmsg)
  122. register XDR *xdrs;
  123. register struct rpc_msg *rmsg;
  124. {
  125. if (xdr_u_long(xdrs, &(rmsg->rm_xid)) &&
  126. xdr_enum(xdrs, (enum_t *) & (rmsg->rm_direction)) &&
  127. (rmsg->rm_direction == REPLY))
  128. return (xdr_union(xdrs, (enum_t *) & (rmsg->rm_reply.rp_stat),
  129. (caddr_t) & (rmsg->rm_reply.ru), reply_dscrm,
  130. NULL_xdrproc_t));
  131. return (FALSE);
  132. }
  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 xdr_callhdr(xdrs, cmsg)
  139. register XDR *xdrs;
  140. register struct rpc_msg *cmsg;
  141. {
  142. cmsg->rm_direction = CALL;
  143. cmsg->rm_call.cb_rpcvers = RPC_MSG_VERSION;
  144. if (
  145. (xdrs->x_op == XDR_ENCODE) &&
  146. xdr_u_long(xdrs, &(cmsg->rm_xid)) &&
  147. xdr_enum(xdrs, (enum_t *) & (cmsg->rm_direction)) &&
  148. xdr_u_long(xdrs, &(cmsg->rm_call.cb_rpcvers)) &&
  149. xdr_u_long(xdrs, &(cmsg->rm_call.cb_prog)))
  150. return (xdr_u_long(xdrs, &(cmsg->rm_call.cb_vers)));
  151. return (FALSE);
  152. }
  153. /* ************************** Client utility routine ************* */
  154. static void accepted(acpt_stat, error)
  155. register enum accept_stat acpt_stat;
  156. register struct rpc_err *error;
  157. {
  158. switch (acpt_stat) {
  159. case PROG_UNAVAIL:
  160. error->re_status = RPC_PROGUNAVAIL;
  161. return;
  162. case PROG_MISMATCH:
  163. error->re_status = RPC_PROGVERSMISMATCH;
  164. return;
  165. case PROC_UNAVAIL:
  166. error->re_status = RPC_PROCUNAVAIL;
  167. return;
  168. case GARBAGE_ARGS:
  169. error->re_status = RPC_CANTDECODEARGS;
  170. return;
  171. case SYSTEM_ERR:
  172. error->re_status = RPC_SYSTEMERROR;
  173. return;
  174. case SUCCESS:
  175. error->re_status = RPC_SUCCESS;
  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_ACCEPTED;
  181. error->re_lb.s2 = (long) acpt_stat;
  182. }
  183. static void rejected(rjct_stat, error)
  184. register enum reject_stat rjct_stat;
  185. register struct rpc_err *error;
  186. {
  187. switch (rjct_stat) {
  188. case RPC_VERSMISMATCH:
  189. error->re_status = RPC_VERSMISMATCH;
  190. return;
  191. case AUTH_ERROR:
  192. error->re_status = RPC_AUTHERROR;
  193. return;
  194. }
  195. /* something's wrong, but we don't know what ... */
  196. error->re_status = RPC_FAILED;
  197. error->re_lb.s1 = (long) MSG_DENIED;
  198. error->re_lb.s2 = (long) rjct_stat;
  199. }
  200. /*
  201. * given a reply message, fills in the error
  202. */
  203. void _seterr_reply(msg, error)
  204. register struct rpc_msg *msg;
  205. register struct rpc_err *error;
  206. {
  207. /* optimized for normal, SUCCESSful case */
  208. switch (msg->rm_reply.rp_stat) {
  209. case MSG_ACCEPTED:
  210. if (msg->acpted_rply.ar_stat == SUCCESS) {
  211. error->re_status = RPC_SUCCESS;
  212. return;
  213. };
  214. accepted(msg->acpted_rply.ar_stat, error);
  215. break;
  216. case MSG_DENIED:
  217. rejected(msg->rjcted_rply.rj_stat, error);
  218. break;
  219. default:
  220. error->re_status = RPC_FAILED;
  221. error->re_lb.s1 = (long) (msg->rm_reply.rp_stat);
  222. break;
  223. }
  224. switch (error->re_status) {
  225. case RPC_VERSMISMATCH:
  226. error->re_vers.low = msg->rjcted_rply.rj_vers.low;
  227. error->re_vers.high = msg->rjcted_rply.rj_vers.high;
  228. break;
  229. case RPC_AUTHERROR:
  230. error->re_why = msg->rjcted_rply.rj_why;
  231. break;
  232. case RPC_PROGVERSMISMATCH:
  233. error->re_vers.low = msg->acpted_rply.ar_vers.low;
  234. error->re_vers.high = msg->acpted_rply.ar_vers.high;
  235. break;
  236. }
  237. }