rpc_prot.c 7.7 KB

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