svc.c 11 KB

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