svc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /*
  2. * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  3. * unrestricted use provided that this legend is included on all tape
  4. * media and as a part of the software program in whole or part. Users
  5. * may copy or modify Sun RPC without charge, but are not authorized
  6. * to license or distribute it to anyone else except as part of a product or
  7. * program developed by the user.
  8. *
  9. * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  10. * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  11. * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  12. *
  13. * Sun RPC is provided with no support and without any obligation on the
  14. * part of Sun Microsystems, Inc. to assist in its use, correction,
  15. * modification or enhancement.
  16. *
  17. * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  18. * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  19. * OR ANY PART THEREOF.
  20. *
  21. * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  22. * or profits or other special, indirect and consequential damages, even if
  23. * Sun has been advised of the possibility of such damages.
  24. *
  25. * Sun Microsystems, Inc.
  26. * 2550 Garcia Avenue
  27. * Mountain View, California 94043
  28. */
  29. /*
  30. * svc.c, Server-side remote procedure call interface.
  31. *
  32. * There are two sets of procedures here. The xprt routines are
  33. * for handling transport handles. The svc routines handle the
  34. * list of service routines.
  35. *
  36. * Copyright (C) 1984, Sun Microsystems, Inc.
  37. */
  38. #define __FORCE_GLIBC
  39. #define _GNU_SOURCE
  40. #include <features.h>
  41. #include <errno.h>
  42. #include <unistd.h>
  43. #include "rpc_private.h"
  44. #include <rpc/svc.h>
  45. #include <rpc/pmap_clnt.h>
  46. #include <sys/poll.h>
  47. #ifdef __UCLIBC_HAS_THREADS__
  48. #define xports ((SVCXPRT **)RPC_THREAD_VARIABLE(svc_xports_s))
  49. #else
  50. static SVCXPRT **xports;
  51. #endif
  52. #define NULL_SVC ((struct svc_callout *)0)
  53. #define RQCRED_SIZE 400 /* this size is excessive */
  54. /* The services list
  55. Each entry represents a set of procedures (an rpc program).
  56. The dispatch routine takes request structs and runs the
  57. appropriate procedure. */
  58. struct svc_callout {
  59. struct svc_callout *sc_next;
  60. rpcprog_t sc_prog;
  61. rpcvers_t sc_vers;
  62. void (*sc_dispatch) (struct svc_req *, SVCXPRT *);
  63. };
  64. #ifdef __UCLIBC_HAS_THREADS__
  65. #define svc_head ((struct svc_callout *)RPC_THREAD_VARIABLE(svc_head_s))
  66. #else
  67. static struct svc_callout *svc_head;
  68. #endif
  69. /* *************** SVCXPRT related stuff **************** */
  70. /* Activate a transport handle. */
  71. void
  72. xprt_register (SVCXPRT *xprt)
  73. {
  74. register int sock = xprt->xp_sock;
  75. register int i;
  76. if (xports == NULL)
  77. {
  78. xports = (SVCXPRT **) malloc (_rpc_dtablesize () * sizeof (SVCXPRT *));
  79. if (xports == NULL) /* Don´t add handle */
  80. return;
  81. }
  82. if (sock < _rpc_dtablesize ())
  83. {
  84. xports[sock] = xprt;
  85. if (sock < FD_SETSIZE)
  86. FD_SET (sock, &svc_fdset);
  87. /* Check if we have an empty slot */
  88. for (i = 0; i < svc_max_pollfd; ++i)
  89. if (svc_pollfd[i].fd == -1)
  90. {
  91. svc_pollfd[i].fd = sock;
  92. svc_pollfd[i].events = (POLLIN | POLLPRI |
  93. POLLRDNORM | POLLRDBAND);
  94. return;
  95. }
  96. ++svc_max_pollfd;
  97. svc_pollfd = realloc (svc_pollfd,
  98. sizeof (struct pollfd) * svc_max_pollfd);
  99. if (svc_pollfd == NULL) /* Out of memory */
  100. return;
  101. svc_pollfd[svc_max_pollfd - 1].fd = sock;
  102. svc_pollfd[svc_max_pollfd - 1].events = (POLLIN | POLLPRI |
  103. POLLRDNORM | POLLRDBAND);
  104. }
  105. }
  106. /* De-activate a transport handle. */
  107. void
  108. xprt_unregister (SVCXPRT *xprt)
  109. {
  110. register int sock = xprt->xp_sock;
  111. register int i;
  112. if ((sock < _rpc_dtablesize ()) && (xports[sock] == xprt))
  113. {
  114. xports[sock] = (SVCXPRT *) 0;
  115. if (sock < FD_SETSIZE)
  116. FD_CLR (sock, &svc_fdset);
  117. for (i = 0; i < svc_max_pollfd; ++i)
  118. if (svc_pollfd[i].fd == sock)
  119. svc_pollfd[i].fd = -1;
  120. }
  121. }
  122. /* ********************** CALLOUT list related stuff ************* */
  123. /* Search the callout list for a program number, return the callout
  124. struct. */
  125. static struct svc_callout *
  126. svc_find (rpcprog_t prog, rpcvers_t vers, struct svc_callout **prev)
  127. {
  128. register struct svc_callout *s, *p;
  129. p = NULL_SVC;
  130. for (s = svc_head; s != NULL_SVC; s = s->sc_next)
  131. {
  132. if ((s->sc_prog == prog) && (s->sc_vers == vers))
  133. goto done;
  134. p = s;
  135. }
  136. done:
  137. *prev = p;
  138. return s;
  139. }
  140. /* Add a service program to the callout list.
  141. The dispatch routine will be called when a rpc request for this
  142. program number comes in. */
  143. bool_t
  144. svc_register (SVCXPRT * xprt, rpcprog_t prog, rpcvers_t vers,
  145. void (*dispatch) (struct svc_req *, SVCXPRT *),
  146. rpcproc_t protocol)
  147. {
  148. struct svc_callout *prev;
  149. register struct svc_callout *s;
  150. if ((s = svc_find (prog, vers, &prev)) != NULL_SVC)
  151. {
  152. if (s->sc_dispatch == dispatch)
  153. goto pmap_it; /* he is registering another xptr */
  154. return FALSE;
  155. }
  156. s = (struct svc_callout *) mem_alloc (sizeof (struct svc_callout));
  157. if (s == (struct svc_callout *) 0)
  158. return FALSE;
  159. s->sc_prog = prog;
  160. s->sc_vers = vers;
  161. s->sc_dispatch = dispatch;
  162. s->sc_next = svc_head;
  163. svc_head = s;
  164. pmap_it:
  165. /* now register the information with the local binder service */
  166. if (protocol)
  167. return pmap_set (prog, vers, protocol, xprt->xp_port);
  168. return TRUE;
  169. }
  170. /* Remove a service program from the callout list. */
  171. void
  172. svc_unregister (rpcprog_t prog, rpcvers_t vers)
  173. {
  174. struct svc_callout *prev;
  175. register struct svc_callout *s;
  176. if ((s = svc_find (prog, vers, &prev)) == NULL_SVC)
  177. return;
  178. if (prev == NULL_SVC)
  179. svc_head = s->sc_next;
  180. else
  181. prev->sc_next = s->sc_next;
  182. s->sc_next = NULL_SVC;
  183. mem_free ((char *) s, (u_int) sizeof (struct svc_callout));
  184. /* now unregister the information with the local binder service */
  185. pmap_unset (prog, vers);
  186. }
  187. /* ******************* REPLY GENERATION ROUTINES ************ */
  188. /* Send a reply to an rpc request */
  189. bool_t
  190. svc_sendreply (register SVCXPRT *xprt, xdrproc_t xdr_results,
  191. caddr_t xdr_location)
  192. {
  193. struct rpc_msg rply;
  194. rply.rm_direction = REPLY;
  195. rply.rm_reply.rp_stat = MSG_ACCEPTED;
  196. rply.acpted_rply.ar_verf = xprt->xp_verf;
  197. rply.acpted_rply.ar_stat = SUCCESS;
  198. rply.acpted_rply.ar_results.where = xdr_location;
  199. rply.acpted_rply.ar_results.proc = xdr_results;
  200. return SVC_REPLY (xprt, &rply);
  201. }
  202. /* No procedure error reply */
  203. void
  204. svcerr_noproc (register SVCXPRT *xprt)
  205. {
  206. struct rpc_msg rply;
  207. rply.rm_direction = REPLY;
  208. rply.rm_reply.rp_stat = MSG_ACCEPTED;
  209. rply.acpted_rply.ar_verf = xprt->xp_verf;
  210. rply.acpted_rply.ar_stat = PROC_UNAVAIL;
  211. SVC_REPLY (xprt, &rply);
  212. }
  213. /* Can't decode args error reply */
  214. void
  215. svcerr_decode (register SVCXPRT *xprt)
  216. {
  217. struct rpc_msg rply;
  218. rply.rm_direction = REPLY;
  219. rply.rm_reply.rp_stat = MSG_ACCEPTED;
  220. rply.acpted_rply.ar_verf = xprt->xp_verf;
  221. rply.acpted_rply.ar_stat = GARBAGE_ARGS;
  222. SVC_REPLY (xprt, &rply);
  223. }
  224. /* Some system error */
  225. void
  226. svcerr_systemerr (register SVCXPRT *xprt)
  227. {
  228. struct rpc_msg rply;
  229. rply.rm_direction = REPLY;
  230. rply.rm_reply.rp_stat = MSG_ACCEPTED;
  231. rply.acpted_rply.ar_verf = xprt->xp_verf;
  232. rply.acpted_rply.ar_stat = SYSTEM_ERR;
  233. SVC_REPLY (xprt, &rply);
  234. }
  235. /* Authentication error reply */
  236. void
  237. svcerr_auth (SVCXPRT *xprt, enum auth_stat why)
  238. {
  239. struct rpc_msg rply;
  240. rply.rm_direction = REPLY;
  241. rply.rm_reply.rp_stat = MSG_DENIED;
  242. rply.rjcted_rply.rj_stat = AUTH_ERROR;
  243. rply.rjcted_rply.rj_why = why;
  244. SVC_REPLY (xprt, &rply);
  245. }
  246. /* Auth too weak error reply */
  247. void
  248. svcerr_weakauth (SVCXPRT *xprt)
  249. {
  250. svcerr_auth (xprt, AUTH_TOOWEAK);
  251. }
  252. /* Program unavailable error reply */
  253. void
  254. svcerr_noprog (register SVCXPRT *xprt)
  255. {
  256. struct rpc_msg rply;
  257. rply.rm_direction = REPLY;
  258. rply.rm_reply.rp_stat = MSG_ACCEPTED;
  259. rply.acpted_rply.ar_verf = xprt->xp_verf;
  260. rply.acpted_rply.ar_stat = PROG_UNAVAIL;
  261. SVC_REPLY (xprt, &rply);
  262. }
  263. /* Program version mismatch error reply */
  264. void
  265. svcerr_progvers (register SVCXPRT *xprt, rpcvers_t low_vers,
  266. rpcvers_t high_vers)
  267. {
  268. struct rpc_msg rply;
  269. rply.rm_direction = REPLY;
  270. rply.rm_reply.rp_stat = MSG_ACCEPTED;
  271. rply.acpted_rply.ar_verf = xprt->xp_verf;
  272. rply.acpted_rply.ar_stat = PROG_MISMATCH;
  273. rply.acpted_rply.ar_vers.low = low_vers;
  274. rply.acpted_rply.ar_vers.high = high_vers;
  275. SVC_REPLY (xprt, &rply);
  276. }
  277. /* ******************* SERVER INPUT STUFF ******************* */
  278. /*
  279. * Get server side input from some transport.
  280. *
  281. * Statement of authentication parameters management:
  282. * This function owns and manages all authentication parameters, specifically
  283. * the "raw" parameters (msg.rm_call.cb_cred and msg.rm_call.cb_verf) and
  284. * the "cooked" credentials (rqst->rq_clntcred).
  285. * However, this function does not know the structure of the cooked
  286. * credentials, so it make the following assumptions:
  287. * a) the structure is contiguous (no pointers), and
  288. * b) the cred structure size does not exceed RQCRED_SIZE bytes.
  289. * In all events, all three parameters are freed upon exit from this routine.
  290. * The storage is trivially management on the call stack in user land, but
  291. * is mallocated in kernel land.
  292. */
  293. void
  294. svc_getreq (int rdfds)
  295. {
  296. fd_set readfds;
  297. FD_ZERO (&readfds);
  298. readfds.fds_bits[0] = rdfds;
  299. svc_getreqset (&readfds);
  300. }
  301. void
  302. svc_getreqset (fd_set *readfds)
  303. {
  304. register u_int32_t mask;
  305. register u_int32_t *maskp;
  306. register int setsize;
  307. register int sock;
  308. register int bit;
  309. setsize = _rpc_dtablesize ();
  310. maskp = (u_int32_t *) readfds->fds_bits;
  311. for (sock = 0; sock < setsize; sock += 32)
  312. for (mask = *maskp++; (bit = ffs (mask)); mask ^= (1 << (bit - 1)))
  313. svc_getreq_common (sock + bit - 1);
  314. }
  315. void
  316. svc_getreq_poll (struct pollfd *pfdp, int pollretval)
  317. {
  318. register int i;
  319. register int fds_found;
  320. for (i = fds_found = 0; i < svc_max_pollfd && fds_found < pollretval; ++i)
  321. {
  322. register struct pollfd *p = &pfdp[i];
  323. if (p->fd != -1 && p->revents)
  324. {
  325. /* fd has input waiting */
  326. ++fds_found;
  327. if (p->revents & POLLNVAL)
  328. xprt_unregister (xports[p->fd]);
  329. else
  330. svc_getreq_common (p->fd);
  331. }
  332. }
  333. }
  334. void
  335. svc_getreq_common (const int fd)
  336. {
  337. enum xprt_stat stat;
  338. struct rpc_msg msg;
  339. register SVCXPRT *xprt;
  340. char cred_area[2 * MAX_AUTH_BYTES + RQCRED_SIZE];
  341. msg.rm_call.cb_cred.oa_base = cred_area;
  342. msg.rm_call.cb_verf.oa_base = &(cred_area[MAX_AUTH_BYTES]);
  343. xprt = xports[fd];
  344. /* Do we control fd? */
  345. if (xprt == NULL)
  346. return;
  347. /* now receive msgs from xprtprt (support batch calls) */
  348. do
  349. {
  350. if (SVC_RECV (xprt, &msg))
  351. {
  352. /* now find the exported program and call it */
  353. struct svc_callout *s;
  354. struct svc_req r;
  355. enum auth_stat why;
  356. rpcvers_t low_vers;
  357. rpcvers_t high_vers;
  358. int prog_found;
  359. r.rq_clntcred = &(cred_area[2 * MAX_AUTH_BYTES]);
  360. r.rq_xprt = xprt;
  361. r.rq_prog = msg.rm_call.cb_prog;
  362. r.rq_vers = msg.rm_call.cb_vers;
  363. r.rq_proc = msg.rm_call.cb_proc;
  364. r.rq_cred = msg.rm_call.cb_cred;
  365. /* first authenticate the message */
  366. /* Check for null flavor and bypass these calls if possible */
  367. if (msg.rm_call.cb_cred.oa_flavor == AUTH_NULL)
  368. {
  369. r.rq_xprt->xp_verf.oa_flavor = _null_auth.oa_flavor;
  370. r.rq_xprt->xp_verf.oa_length = 0;
  371. }
  372. else if ((why = _authenticate (&r, &msg)) != AUTH_OK)
  373. {
  374. svcerr_auth (xprt, why);
  375. goto call_done;
  376. }
  377. /* now match message with a registered service */
  378. prog_found = FALSE;
  379. low_vers = 0 - 1;
  380. high_vers = 0;
  381. for (s = svc_head; s != NULL_SVC; s = s->sc_next)
  382. {
  383. if (s->sc_prog == r.rq_prog)
  384. {
  385. if (s->sc_vers == r.rq_vers)
  386. {
  387. (*s->sc_dispatch) (&r, xprt);
  388. goto call_done;
  389. }
  390. /* found correct version */
  391. prog_found = TRUE;
  392. if (s->sc_vers < low_vers)
  393. low_vers = s->sc_vers;
  394. if (s->sc_vers > high_vers)
  395. high_vers = s->sc_vers;
  396. }
  397. /* found correct program */
  398. }
  399. /* if we got here, the program or version
  400. is not served ... */
  401. if (prog_found)
  402. svcerr_progvers (xprt, low_vers, high_vers);
  403. else
  404. svcerr_noprog (xprt);
  405. /* Fall through to ... */
  406. }
  407. call_done:
  408. if ((stat = SVC_STAT (xprt)) == XPRT_DIED)
  409. {
  410. SVC_DESTROY (xprt);
  411. break;
  412. }
  413. }
  414. while (stat == XPRT_MOREREQS);
  415. }
  416. #ifdef __UCLIBC_HAS_THREADS__
  417. void
  418. __rpc_thread_svc_cleanup (void)
  419. {
  420. struct svc_callout *svcp;
  421. while ((svcp = svc_head) != NULL)
  422. svc_unregister (svcp->sc_prog, svcp->sc_vers);
  423. }
  424. #endif /* __UCLIBC_HAS_THREADS__ */