rpc_prot.c 7.1 KB

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