svc.c 13 KB

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