svc.c 11 KB

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