rpc_prot.c 7.4 KB

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