svc.c 13 KB

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