123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476 |
- #define __FORCE_GLIBC__
- #include <features.h>
- #define __USE_XOPEN
- #include <sys/errno.h>
- #include <rpc/rpc.h>
- #include <rpc/pmap_clnt.h>
- #ifdef __linux__
- #include <sys/types.h>
- #include <sys/select.h>
- #endif
- extern int errno;
- #ifdef FD_SETSIZE
- static SVCXPRT **xports;
- #else
- #define NOFILE 32
- static SVCXPRT *xports[NOFILE];
- #endif
- #define NULL_SVC ((struct svc_callout *)0)
- #define RQCRED_SIZE 400
- static struct svc_callout {
- struct svc_callout *sc_next;
- u_long sc_prog;
- u_long sc_vers;
- void (*sc_dispatch) ();
- } *svc_head;
- static struct svc_callout *svc_find();
- void xprt_register(xprt)
- SVCXPRT *xprt;
- {
- register int sock = xprt->xp_sock;
- #ifdef FD_SETSIZE
- if (xports == NULL) {
- xports = (SVCXPRT **)
- mem_alloc(FD_SETSIZE * sizeof(SVCXPRT *));
- }
- if (sock < _rpc_dtablesize()) {
- xports[sock] = xprt;
- FD_SET(sock, &svc_fdset);
- }
- #else
- if (sock < NOFILE) {
- xports[sock] = xprt;
- svc_fds |= (1 << sock);
- }
- #endif
- }
- void xprt_unregister(xprt)
- SVCXPRT *xprt;
- {
- register int sock = xprt->xp_sock;
- #ifdef FD_SETSIZE
- if ((sock < _rpc_dtablesize()) && (xports[sock] == xprt)) {
- xports[sock] = (SVCXPRT *) 0;
- FD_CLR(sock, &svc_fdset);
- }
- #else
- if ((sock < NOFILE) && (xports[sock] == xprt)) {
- xports[sock] = (SVCXPRT *) 0;
- svc_fds &= ~(1 << sock);
- }
- #endif
- }
- bool_t svc_register (SVCXPRT *xprt, u_long prog,
- u_long vers, __dispatch_fn_t dispatch, u_long protocol)
- {
- struct svc_callout *prev;
- register struct svc_callout *s;
- if ((s = svc_find(prog, vers, &prev)) != NULL_SVC) {
- if (s->sc_dispatch == dispatch)
- goto pmap_it;
- return (FALSE);
- }
- s = (struct svc_callout *) mem_alloc(sizeof(struct svc_callout));
- if (s == (struct svc_callout *) 0) {
- return (FALSE);
- }
- s->sc_prog = prog;
- s->sc_vers = vers;
- s->sc_dispatch = dispatch;
- s->sc_next = svc_head;
- svc_head = s;
- pmap_it:
-
- if (protocol) {
- return (pmap_set(prog, vers, protocol, xprt->xp_port));
- }
- return (TRUE);
- }
- void svc_unregister(prog, vers)
- u_long prog;
- u_long vers;
- {
- struct svc_callout *prev;
- register struct svc_callout *s;
- if ((s = svc_find(prog, vers, &prev)) == NULL_SVC)
- return;
- if (prev == NULL_SVC) {
- svc_head = s->sc_next;
- } else {
- prev->sc_next = s->sc_next;
- }
- s->sc_next = NULL_SVC;
- mem_free((char *) s, (u_int) sizeof(struct svc_callout));
-
- (void) pmap_unset(prog, vers);
- }
- static struct svc_callout *svc_find(prog, vers, prev)
- u_long prog;
- u_long vers;
- struct svc_callout **prev;
- {
- register struct svc_callout *s, *p;
- p = NULL_SVC;
- for (s = svc_head; s != NULL_SVC; s = s->sc_next) {
- if ((s->sc_prog == prog) && (s->sc_vers == vers))
- goto done;
- p = s;
- }
- done:
- *prev = p;
- return (s);
- }
- bool_t svc_sendreply(xprt, xdr_results, xdr_location)
- register SVCXPRT *xprt;
- xdrproc_t xdr_results;
- caddr_t xdr_location;
- {
- struct rpc_msg rply;
- rply.rm_direction = REPLY;
- rply.rm_reply.rp_stat = MSG_ACCEPTED;
- rply.acpted_rply.ar_verf = xprt->xp_verf;
- rply.acpted_rply.ar_stat = SUCCESS;
- rply.acpted_rply.ar_results.where = xdr_location;
- rply.acpted_rply.ar_results.proc = xdr_results;
- return (SVC_REPLY(xprt, &rply));
- }
- void svcerr_noproc(xprt)
- register SVCXPRT *xprt;
- {
- struct rpc_msg rply;
- rply.rm_direction = REPLY;
- rply.rm_reply.rp_stat = MSG_ACCEPTED;
- rply.acpted_rply.ar_verf = xprt->xp_verf;
- rply.acpted_rply.ar_stat = PROC_UNAVAIL;
- SVC_REPLY(xprt, &rply);
- }
- void svcerr_decode(xprt)
- register SVCXPRT *xprt;
- {
- struct rpc_msg rply;
- rply.rm_direction = REPLY;
- rply.rm_reply.rp_stat = MSG_ACCEPTED;
- rply.acpted_rply.ar_verf = xprt->xp_verf;
- rply.acpted_rply.ar_stat = GARBAGE_ARGS;
- SVC_REPLY(xprt, &rply);
- }
- void svcerr_systemerr(xprt)
- register SVCXPRT *xprt;
- {
- struct rpc_msg rply;
- rply.rm_direction = REPLY;
- rply.rm_reply.rp_stat = MSG_ACCEPTED;
- rply.acpted_rply.ar_verf = xprt->xp_verf;
- rply.acpted_rply.ar_stat = SYSTEM_ERR;
- SVC_REPLY(xprt, &rply);
- }
- void svcerr_auth(xprt, why)
- SVCXPRT *xprt;
- enum auth_stat why;
- {
- struct rpc_msg rply;
- rply.rm_direction = REPLY;
- rply.rm_reply.rp_stat = MSG_DENIED;
- rply.rjcted_rply.rj_stat = AUTH_ERROR;
- rply.rjcted_rply.rj_why = why;
- SVC_REPLY(xprt, &rply);
- }
- void svcerr_weakauth(xprt)
- SVCXPRT *xprt;
- {
- svcerr_auth(xprt, AUTH_TOOWEAK);
- }
- void svcerr_noprog(xprt)
- register SVCXPRT *xprt;
- {
- struct rpc_msg rply;
- rply.rm_direction = REPLY;
- rply.rm_reply.rp_stat = MSG_ACCEPTED;
- rply.acpted_rply.ar_verf = xprt->xp_verf;
- rply.acpted_rply.ar_stat = PROG_UNAVAIL;
- SVC_REPLY(xprt, &rply);
- }
- void svcerr_progvers(xprt, low_vers, high_vers)
- register SVCXPRT *xprt;
- u_long low_vers;
- u_long high_vers;
- {
- struct rpc_msg rply;
- rply.rm_direction = REPLY;
- rply.rm_reply.rp_stat = MSG_ACCEPTED;
- rply.acpted_rply.ar_verf = xprt->xp_verf;
- rply.acpted_rply.ar_stat = PROG_MISMATCH;
- rply.acpted_rply.ar_vers.low = low_vers;
- rply.acpted_rply.ar_vers.high = high_vers;
- SVC_REPLY(xprt, &rply);
- }
- void svc_getreq(rdfds)
- int rdfds;
- {
- #ifdef FD_SETSIZE
- fd_set readfds;
- FD_ZERO(&readfds);
- #if 0
- readfds = rdfds;
- #else
- readfds.fds_bits[0] = rdfds;
- #endif
- svc_getreqset(&readfds);
- #else
- int readfds = rdfds & svc_fds;
- svc_getreqset(&readfds);
- #endif
- }
- void svc_getreqset(readfds)
- #ifdef FD_SETSIZE
- fd_set *readfds;
- {
- #else
- int *readfds;
- {
- int readfds_local = *readfds;
- #endif
- enum xprt_stat stat;
- struct rpc_msg msg;
- int prog_found;
- u_long low_vers;
- u_long high_vers;
- struct svc_req r;
- register SVCXPRT *xprt;
- register u_long mask;
- register int bit;
- register u_long *maskp;
- register int setsize;
- register int sock;
- char cred_area[2 * MAX_AUTH_BYTES + RQCRED_SIZE];
- msg.rm_call.cb_cred.oa_base = cred_area;
- msg.rm_call.cb_verf.oa_base = &(cred_area[MAX_AUTH_BYTES]);
- r.rq_clntcred = &(cred_area[2 * MAX_AUTH_BYTES]);
- #ifdef FD_SETSIZE
- setsize = _rpc_dtablesize();
- #ifdef __linux__
- maskp = (u_long *) readfds;
- #else
- maskp = (u_long *) readfds->fds_bits;
- #endif
- for (sock = 0; sock < setsize; sock += NFDBITS) {
- for (mask = *maskp++; (bit = ffs(mask)); mask ^= (1 << (bit - 1))) {
-
- xprt = xports[sock + bit - 1];
- #else
- for (sock = 0; readfds_local != 0; sock++, readfds_local >>= 1) {
- if ((readfds_local & 1) != 0) {
-
- xprt = xports[sock];
- #endif
-
- do {
- if (SVC_RECV(xprt, &msg)) {
-
- register struct svc_callout *s;
- enum auth_stat why;
- r.rq_xprt = xprt;
- r.rq_prog = msg.rm_call.cb_prog;
- r.rq_vers = msg.rm_call.cb_vers;
- r.rq_proc = msg.rm_call.cb_proc;
- r.rq_cred = msg.rm_call.cb_cred;
-
- if ((why = _authenticate(&r, &msg)) != AUTH_OK) {
- svcerr_auth(xprt, why);
- goto call_done;
- }
-
- prog_found = FALSE;
- low_vers = 0 - 1;
- high_vers = 0;
- for (s = svc_head; s != NULL_SVC; s = s->sc_next) {
- if (s->sc_prog == r.rq_prog) {
- if (s->sc_vers == r.rq_vers) {
- (*s->sc_dispatch) (&r, xprt);
- goto call_done;
- }
- prog_found = TRUE;
- if (s->sc_vers < low_vers)
- low_vers = s->sc_vers;
- if (s->sc_vers > high_vers)
- high_vers = s->sc_vers;
- }
- }
-
- if (prog_found)
- svcerr_progvers(xprt, low_vers, high_vers);
- else
- svcerr_noprog(xprt);
-
- }
- call_done:
- if ((stat = SVC_STAT(xprt)) == XPRT_DIED) {
- SVC_DESTROY(xprt);
- break;
- }
- } while (stat == XPRT_MOREREQS);
- }
- }
- }
|