svc.c 13 KB

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