svc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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(xprt, prog, vers, dispatch, protocol)
  119. SVCXPRT *xprt;
  120. u_long prog;
  121. u_long vers;
  122. void (*dispatch) ();
  123. int protocol;
  124. {
  125. struct svc_callout *prev;
  126. register struct svc_callout *s;
  127. if ((s = svc_find(prog, vers, &prev)) != NULL_SVC) {
  128. if (s->sc_dispatch == dispatch)
  129. goto pmap_it; /* he is registering another xptr */
  130. return (FALSE);
  131. }
  132. s = (struct svc_callout *) mem_alloc(sizeof(struct svc_callout));
  133. if (s == (struct svc_callout *) 0) {
  134. return (FALSE);
  135. }
  136. s->sc_prog = prog;
  137. s->sc_vers = vers;
  138. s->sc_dispatch = dispatch;
  139. s->sc_next = svc_head;
  140. svc_head = s;
  141. pmap_it:
  142. /* now register the information with the local binder service */
  143. if (protocol) {
  144. return (pmap_set(prog, vers, protocol, xprt->xp_port));
  145. }
  146. return (TRUE);
  147. }
  148. /*
  149. * Remove a service program from the callout list.
  150. */
  151. void svc_unregister(prog, vers)
  152. u_long prog;
  153. u_long vers;
  154. {
  155. struct svc_callout *prev;
  156. register struct svc_callout *s;
  157. if ((s = svc_find(prog, vers, &prev)) == NULL_SVC)
  158. return;
  159. if (prev == NULL_SVC) {
  160. svc_head = s->sc_next;
  161. } else {
  162. prev->sc_next = s->sc_next;
  163. }
  164. s->sc_next = NULL_SVC;
  165. mem_free((char *) s, (u_int) sizeof(struct svc_callout));
  166. /* now unregister the information with the local binder service */
  167. (void) pmap_unset(prog, vers);
  168. }
  169. /*
  170. * Search the callout list for a program number, return the callout
  171. * struct.
  172. */
  173. static struct svc_callout *svc_find(prog, vers, prev)
  174. u_long prog;
  175. u_long vers;
  176. struct svc_callout **prev;
  177. {
  178. register struct svc_callout *s, *p;
  179. p = NULL_SVC;
  180. for (s = svc_head; s != NULL_SVC; s = s->sc_next) {
  181. if ((s->sc_prog == prog) && (s->sc_vers == vers))
  182. goto done;
  183. p = s;
  184. }
  185. done:
  186. *prev = p;
  187. return (s);
  188. }
  189. /* ******************* REPLY GENERATION ROUTINES ************ */
  190. /*
  191. * Send a reply to an rpc request
  192. */
  193. bool_t svc_sendreply(xprt, xdr_results, xdr_location)
  194. register SVCXPRT *xprt;
  195. xdrproc_t xdr_results;
  196. caddr_t xdr_location;
  197. {
  198. struct rpc_msg rply;
  199. rply.rm_direction = REPLY;
  200. rply.rm_reply.rp_stat = MSG_ACCEPTED;
  201. rply.acpted_rply.ar_verf = xprt->xp_verf;
  202. rply.acpted_rply.ar_stat = SUCCESS;
  203. rply.acpted_rply.ar_results.where = xdr_location;
  204. rply.acpted_rply.ar_results.proc = xdr_results;
  205. return (SVC_REPLY(xprt, &rply));
  206. }
  207. /*
  208. * No procedure error reply
  209. */
  210. void svcerr_noproc(xprt)
  211. register SVCXPRT *xprt;
  212. {
  213. struct rpc_msg rply;
  214. rply.rm_direction = REPLY;
  215. rply.rm_reply.rp_stat = MSG_ACCEPTED;
  216. rply.acpted_rply.ar_verf = xprt->xp_verf;
  217. rply.acpted_rply.ar_stat = PROC_UNAVAIL;
  218. SVC_REPLY(xprt, &rply);
  219. }
  220. /*
  221. * Can't decode args error reply
  222. */
  223. void svcerr_decode(xprt)
  224. 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 = GARBAGE_ARGS;
  231. SVC_REPLY(xprt, &rply);
  232. }
  233. /*
  234. * Some system error
  235. */
  236. void svcerr_systemerr(xprt)
  237. register SVCXPRT *xprt;
  238. {
  239. struct rpc_msg rply;
  240. rply.rm_direction = REPLY;
  241. rply.rm_reply.rp_stat = MSG_ACCEPTED;
  242. rply.acpted_rply.ar_verf = xprt->xp_verf;
  243. rply.acpted_rply.ar_stat = SYSTEM_ERR;
  244. SVC_REPLY(xprt, &rply);
  245. }
  246. /*
  247. * Authentication error reply
  248. */
  249. void svcerr_auth(xprt, why)
  250. SVCXPRT *xprt;
  251. 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. /*
  261. * Auth too weak error reply
  262. */
  263. void svcerr_weakauth(xprt)
  264. SVCXPRT *xprt;
  265. {
  266. svcerr_auth(xprt, AUTH_TOOWEAK);
  267. }
  268. /*
  269. * Program unavailable error reply
  270. */
  271. void svcerr_noprog(xprt)
  272. register SVCXPRT *xprt;
  273. {
  274. struct rpc_msg rply;
  275. rply.rm_direction = REPLY;
  276. rply.rm_reply.rp_stat = MSG_ACCEPTED;
  277. rply.acpted_rply.ar_verf = xprt->xp_verf;
  278. rply.acpted_rply.ar_stat = PROG_UNAVAIL;
  279. SVC_REPLY(xprt, &rply);
  280. }
  281. /*
  282. * Program version mismatch error reply
  283. */
  284. void svcerr_progvers(xprt, low_vers, high_vers)
  285. register SVCXPRT *xprt;
  286. u_long low_vers;
  287. u_long high_vers;
  288. {
  289. struct rpc_msg rply;
  290. rply.rm_direction = REPLY;
  291. rply.rm_reply.rp_stat = MSG_ACCEPTED;
  292. rply.acpted_rply.ar_verf = xprt->xp_verf;
  293. rply.acpted_rply.ar_stat = PROG_MISMATCH;
  294. rply.acpted_rply.ar_vers.low = low_vers;
  295. rply.acpted_rply.ar_vers.high = high_vers;
  296. SVC_REPLY(xprt, &rply);
  297. }
  298. /* ******************* SERVER INPUT STUFF ******************* */
  299. /*
  300. * Get server side input from some transport.
  301. *
  302. * Statement of authentication parameters management:
  303. * This function owns and manages all authentication parameters, specifically
  304. * the "raw" parameters (msg.rm_call.cb_cred and msg.rm_call.cb_verf) and
  305. * the "cooked" credentials (rqst->rq_clntcred).
  306. * However, this function does not know the structure of the cooked
  307. * credentials, so it make the following assumptions:
  308. * a) the structure is contiguous (no pointers), and
  309. * b) the cred structure size does not exceed RQCRED_SIZE bytes.
  310. * In all events, all three parameters are freed upon exit from this routine.
  311. * The storage is trivially management on the call stack in user land, but
  312. * is mallocated in kernel land.
  313. */
  314. void svc_getreq(rdfds)
  315. int rdfds;
  316. {
  317. #ifdef FD_SETSIZE
  318. fd_set readfds;
  319. FD_ZERO(&readfds);
  320. /*#ifdef __linux__*/
  321. #if 0
  322. readfds = rdfds;
  323. #else
  324. readfds.fds_bits[0] = rdfds;
  325. #endif
  326. svc_getreqset(&readfds);
  327. #else
  328. int readfds = rdfds & svc_fds;
  329. svc_getreqset(&readfds);
  330. #endif /* def FD_SETSIZE */
  331. }
  332. void svc_getreqset(readfds)
  333. #ifdef FD_SETSIZE
  334. fd_set *readfds;
  335. {
  336. #else
  337. int *readfds;
  338. {
  339. int readfds_local = *readfds;
  340. #endif /* def FD_SETSIZE */
  341. enum xprt_stat stat;
  342. struct rpc_msg msg;
  343. int prog_found;
  344. u_long low_vers;
  345. u_long high_vers;
  346. struct svc_req r;
  347. register SVCXPRT *xprt;
  348. register u_long mask;
  349. register int bit;
  350. register u_long *maskp;
  351. register int setsize;
  352. register int sock;
  353. char cred_area[2 * MAX_AUTH_BYTES + RQCRED_SIZE];
  354. msg.rm_call.cb_cred.oa_base = cred_area;
  355. msg.rm_call.cb_verf.oa_base = &(cred_area[MAX_AUTH_BYTES]);
  356. r.rq_clntcred = &(cred_area[2 * MAX_AUTH_BYTES]);
  357. #ifdef FD_SETSIZE
  358. setsize = _rpc_dtablesize();
  359. #ifdef __linux__
  360. /*#define NFDBITS 32*/
  361. maskp = (u_long *) readfds;
  362. #else
  363. maskp = (u_long *) readfds->fds_bits;
  364. #endif
  365. for (sock = 0; sock < setsize; sock += NFDBITS) {
  366. for (mask = *maskp++; bit = ffs(mask); mask ^= (1 << (bit - 1))) {
  367. /* sock has input waiting */
  368. xprt = xports[sock + bit - 1];
  369. #else
  370. for (sock = 0; readfds_local != 0; sock++, readfds_local >>= 1) {
  371. if ((readfds_local & 1) != 0) {
  372. /* sock has input waiting */
  373. xprt = xports[sock];
  374. #endif /* def FD_SETSIZE */
  375. /* now receive msgs from xprtprt (support batch calls) */
  376. do {
  377. if (SVC_RECV(xprt, &msg)) {
  378. /* now find the exported program and call it */
  379. register struct svc_callout *s;
  380. enum auth_stat why;
  381. r.rq_xprt = xprt;
  382. r.rq_prog = msg.rm_call.cb_prog;
  383. r.rq_vers = msg.rm_call.cb_vers;
  384. r.rq_proc = msg.rm_call.cb_proc;
  385. r.rq_cred = msg.rm_call.cb_cred;
  386. /* first authenticate the message */
  387. if ((why = _authenticate(&r, &msg)) != AUTH_OK) {
  388. svcerr_auth(xprt, why);
  389. goto call_done;
  390. }
  391. /* now match message with a registered service */
  392. prog_found = FALSE;
  393. low_vers = 0 - 1;
  394. high_vers = 0;
  395. for (s = svc_head; s != NULL_SVC; s = s->sc_next) {
  396. if (s->sc_prog == r.rq_prog) {
  397. if (s->sc_vers == r.rq_vers) {
  398. (*s->sc_dispatch) (&r, xprt);
  399. goto call_done;
  400. } /* found correct version */
  401. prog_found = TRUE;
  402. if (s->sc_vers < low_vers)
  403. low_vers = s->sc_vers;
  404. if (s->sc_vers > high_vers)
  405. high_vers = s->sc_vers;
  406. } /* found correct program */
  407. }
  408. /*
  409. * if we got here, the program or version
  410. * is not served ...
  411. */
  412. if (prog_found)
  413. svcerr_progvers(xprt, low_vers, high_vers);
  414. else
  415. svcerr_noprog(xprt);
  416. /* Fall through to ... */
  417. }
  418. call_done:
  419. if ((stat = SVC_STAT(xprt)) == XPRT_DIED) {
  420. SVC_DESTROY(xprt);
  421. break;
  422. }
  423. } while (stat == XPRT_MOREREQS);
  424. }
  425. }
  426. }