rpc_prot.c 7.7 KB

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