svc.c 11 KB

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