svc.c 11 KB

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