1 /* 2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for 3 * unrestricted use provided that this legend is included on all tape 4 * media and as a part of the software program in whole or part. Users 5 * may copy or modify Sun RPC without charge, but are not authorized 6 * to license or distribute it to anyone else except as part of a product or 7 * program developed by the user. 8 * 9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 10 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 12 * 13 * Sun RPC is provided with no support and without any obligation on the 14 * part of Sun Microsystems, Inc. to assist in its use, correction, 15 * modification or enhancement. 16 * 17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 19 * OR ANY PART THEREOF. 20 * 21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue 22 * or profits or other special, indirect and consequential damages, even if 23 * Sun has been advised of the possibility of such damages. 24 * 25 * Sun Microsystems, Inc. 26 * 2550 Garcia Avenue 27 * Mountain View, California 94043 28 * 29 * @(#)svc.c 1.44 88/02/08 Copyr 1984 Sun Micro 30 * @(#)svc.c 2.4 88/08/11 4.0 RPCSRC 31 * $FreeBSD: src/lib/libc/rpc/svc.c,v 1.14.2.1 2001/03/05 10:50:36 obrien Exp $ 32 * $DragonFly: src/lib/libc/rpc/svc.c,v 1.4 2005/11/13 12:27:04 swildner Exp $ 33 */ 34 35 /* 36 * svc.c, Server-side remote procedure call interface. 37 * 38 * There are two sets of procedures here. The xprt routines are 39 * for handling transport handles. The svc routines handle the 40 * list of service routines. 41 * 42 * Copyright (C) 1984, Sun Microsystems, Inc. 43 */ 44 45 #include <string.h> 46 #include <stdlib.h> 47 #include <rpc/rpc.h> 48 #include <rpc/pmap_clnt.h> 49 50 static SVCXPRT **xports; 51 static int xportssize; 52 53 #define NULL_SVC ((struct svc_callout *)0) 54 #define RQCRED_SIZE 400 /* this size is excessive */ 55 56 #define max(a, b) (a > b ? a : b) 57 58 /* 59 * The services list 60 * Each entry represents a set of procedures (an rpc program). 61 * The dispatch routine takes request structs and runs the 62 * apropriate procedure. 63 */ 64 static struct svc_callout { 65 struct svc_callout *sc_next; 66 u_long sc_prog; 67 u_long sc_vers; 68 void (*sc_dispatch)(); 69 } *svc_head; 70 71 static struct svc_callout *svc_find(); 72 73 int __svc_fdsetsize = 0; 74 fd_set *__svc_fdset = NULL; 75 76 /* *************** SVCXPRT related stuff **************** */ 77 78 /* 79 * Activate a transport handle. 80 */ 81 void 82 xprt_register(SVCXPRT *xprt) 83 { 84 int sock = xprt->xp_sock; 85 86 if (sock + 1 > __svc_fdsetsize) { 87 int bytes = howmany(sock + 1, NFDBITS) * sizeof(fd_mask); 88 fd_set *fds; 89 90 fds = (fd_set *)malloc(bytes); 91 memset(fds, 0, bytes); 92 if (__svc_fdset) { 93 memcpy(fds, __svc_fdset, howmany(__svc_fdsetsize, 94 NFDBITS) * sizeof(fd_mask)); 95 free(__svc_fdset); 96 } 97 __svc_fdset = fds; 98 __svc_fdsetsize = howmany(sock+1, NFDBITS) * NFDBITS; 99 } 100 101 if (sock < FD_SETSIZE) 102 FD_SET(sock, &svc_fdset); 103 FD_SET(sock, __svc_fdset); 104 105 if (xports == NULL || sock + 1 > xportssize) { 106 SVCXPRT **xp; 107 int size = FD_SETSIZE; 108 109 if (sock + 1 > size) 110 size = sock + 1; 111 xp = (SVCXPRT **)mem_alloc(size * sizeof(SVCXPRT *)); 112 memset(xp, 0, size * sizeof(SVCXPRT *)); 113 if (xports) { 114 memcpy(xp, xports, xportssize * sizeof(SVCXPRT *)); 115 free(xports); 116 } 117 xportssize = size; 118 xports = xp; 119 } 120 xports[sock] = xprt; 121 svc_maxfd = max(svc_maxfd, sock); 122 } 123 124 /* 125 * De-activate a transport handle. 126 */ 127 void 128 xprt_unregister(SVCXPRT *xprt) 129 { 130 int sock = xprt->xp_sock; 131 132 if (xports[sock] == xprt) { 133 xports[sock] = (SVCXPRT *)0; 134 if (sock < FD_SETSIZE) 135 FD_CLR(sock, &svc_fdset); 136 FD_CLR(sock, __svc_fdset); 137 if (sock == svc_maxfd) { 138 for (svc_maxfd--; svc_maxfd >= 0; svc_maxfd--) 139 if (xports[svc_maxfd]) 140 break; 141 } 142 /* 143 * XXX could use svc_maxfd as a hint to 144 * decrease the size of __svc_fdset 145 */ 146 } 147 } 148 149 150 /* ********************** CALLOUT list related stuff ************* */ 151 152 /* 153 * Add a service program to the callout list. 154 * The dispatch routine will be called when a rpc request for this 155 * program number comes in. 156 */ 157 bool_t 158 svc_register(SVCXPRT *xprt, u_long prog, u_long vers, void (*dispatch)(), 159 int protocol) 160 { 161 struct svc_callout *prev; 162 struct svc_callout *s; 163 164 if ((s = svc_find(prog, vers, &prev)) != NULL_SVC) { 165 if (s->sc_dispatch == dispatch) 166 goto pmap_it; /* he is registering another xptr */ 167 return (FALSE); 168 } 169 s = (struct svc_callout *)mem_alloc(sizeof(struct svc_callout)); 170 if (s == (struct svc_callout *)0) { 171 return (FALSE); 172 } 173 s->sc_prog = prog; 174 s->sc_vers = vers; 175 s->sc_dispatch = dispatch; 176 s->sc_next = svc_head; 177 svc_head = s; 178 pmap_it: 179 /* now register the information with the local binder service */ 180 if (protocol) { 181 return (pmap_set(prog, vers, protocol, xprt->xp_port)); 182 } 183 return (TRUE); 184 } 185 186 /* 187 * Remove a service program from the callout list. 188 */ 189 void 190 svc_unregister(u_long prog, u_long vers) 191 { 192 struct svc_callout *prev; 193 struct svc_callout *s; 194 195 if ((s = svc_find(prog, vers, &prev)) == NULL_SVC) 196 return; 197 if (prev == NULL_SVC) { 198 svc_head = s->sc_next; 199 } else { 200 prev->sc_next = s->sc_next; 201 } 202 s->sc_next = NULL_SVC; 203 mem_free((char *) s, (u_int) sizeof(struct svc_callout)); 204 /* now unregister the information with the local binder service */ 205 pmap_unset(prog, vers); 206 } 207 208 /* 209 * Search the callout list for a program number, return the callout 210 * struct. 211 */ 212 static struct svc_callout * 213 svc_find(u_long prog, u_long vers, struct svc_callout **prev) 214 { 215 struct svc_callout *s, *p; 216 217 p = NULL_SVC; 218 for (s = svc_head; s != NULL_SVC; s = s->sc_next) { 219 if ((s->sc_prog == prog) && (s->sc_vers == vers)) 220 goto done; 221 p = s; 222 } 223 done: 224 *prev = p; 225 return (s); 226 } 227 228 /* ******************* REPLY GENERATION ROUTINES ************ */ 229 230 /* 231 * Send a reply to an rpc request 232 */ 233 bool_t 234 svc_sendreply(SVCXPRT *xprt, xdrproc_t xdr_results, caddr_t xdr_location) 235 { 236 struct rpc_msg rply; 237 238 rply.rm_direction = REPLY; 239 rply.rm_reply.rp_stat = MSG_ACCEPTED; 240 rply.acpted_rply.ar_verf = xprt->xp_verf; 241 rply.acpted_rply.ar_stat = SUCCESS; 242 rply.acpted_rply.ar_results.where = xdr_location; 243 rply.acpted_rply.ar_results.proc = xdr_results; 244 return (SVC_REPLY(xprt, &rply)); 245 } 246 247 /* 248 * No procedure error reply 249 */ 250 void 251 svcerr_noproc(SVCXPRT *xprt) 252 { 253 struct rpc_msg rply; 254 255 rply.rm_direction = REPLY; 256 rply.rm_reply.rp_stat = MSG_ACCEPTED; 257 rply.acpted_rply.ar_verf = xprt->xp_verf; 258 rply.acpted_rply.ar_stat = PROC_UNAVAIL; 259 SVC_REPLY(xprt, &rply); 260 } 261 262 /* 263 * Can't decode args error reply 264 */ 265 void 266 svcerr_decode(SVCXPRT *xprt) 267 { 268 struct rpc_msg rply; 269 270 rply.rm_direction = REPLY; 271 rply.rm_reply.rp_stat = MSG_ACCEPTED; 272 rply.acpted_rply.ar_verf = xprt->xp_verf; 273 rply.acpted_rply.ar_stat = GARBAGE_ARGS; 274 SVC_REPLY(xprt, &rply); 275 } 276 277 /* 278 * Some system error 279 */ 280 void 281 svcerr_systemerr(SVCXPRT *xprt) 282 { 283 struct rpc_msg rply; 284 285 rply.rm_direction = REPLY; 286 rply.rm_reply.rp_stat = MSG_ACCEPTED; 287 rply.acpted_rply.ar_verf = xprt->xp_verf; 288 rply.acpted_rply.ar_stat = SYSTEM_ERR; 289 SVC_REPLY(xprt, &rply); 290 } 291 292 /* 293 * Authentication error reply 294 */ 295 void 296 svcerr_auth(SVCXPRT *xprt, enum auth_stat why) 297 { 298 struct rpc_msg rply; 299 300 rply.rm_direction = REPLY; 301 rply.rm_reply.rp_stat = MSG_DENIED; 302 rply.rjcted_rply.rj_stat = AUTH_ERROR; 303 rply.rjcted_rply.rj_why = why; 304 SVC_REPLY(xprt, &rply); 305 } 306 307 /* 308 * Auth too weak error reply 309 */ 310 void 311 svcerr_weakauth(SVCXPRT *xprt) 312 { 313 314 svcerr_auth(xprt, AUTH_TOOWEAK); 315 } 316 317 /* 318 * Program unavailable error reply 319 */ 320 void 321 svcerr_noprog(SVCXPRT *xprt) 322 { 323 struct rpc_msg rply; 324 325 rply.rm_direction = REPLY; 326 rply.rm_reply.rp_stat = MSG_ACCEPTED; 327 rply.acpted_rply.ar_verf = xprt->xp_verf; 328 rply.acpted_rply.ar_stat = PROG_UNAVAIL; 329 SVC_REPLY(xprt, &rply); 330 } 331 332 /* 333 * Program version mismatch error reply 334 */ 335 void 336 svcerr_progvers(SVCXPRT *xprt, u_long low_vers, u_long high_vers) 337 { 338 struct rpc_msg rply; 339 340 rply.rm_direction = REPLY; 341 rply.rm_reply.rp_stat = MSG_ACCEPTED; 342 rply.acpted_rply.ar_verf = xprt->xp_verf; 343 rply.acpted_rply.ar_stat = PROG_MISMATCH; 344 rply.acpted_rply.ar_vers.low = low_vers; 345 rply.acpted_rply.ar_vers.high = high_vers; 346 SVC_REPLY(xprt, &rply); 347 } 348 349 /* ******************* SERVER INPUT STUFF ******************* */ 350 351 /* 352 * Get server side input from some transport. 353 * 354 * Statement of authentication parameters management: 355 * This function owns and manages all authentication parameters, specifically 356 * the "raw" parameters (msg.rm_call.cb_cred and msg.rm_call.cb_verf) and 357 * the "cooked" credentials (rqst->rq_clntcred). 358 * However, this function does not know the structure of the cooked 359 * credentials, so it make the following assumptions: 360 * a) the structure is contiguous (no pointers), and 361 * b) the cred structure size does not exceed RQCRED_SIZE bytes. 362 * In all events, all three parameters are freed upon exit from this routine. 363 * The storage is trivially management on the call stack in user land, but 364 * is mallocated in kernel land. 365 */ 366 367 void 368 svc_getreq(int rdfds) 369 { 370 fd_set readfds; 371 372 FD_ZERO(&readfds); 373 readfds.fds_bits[0] = rdfds; 374 svc_getreqset(&readfds); 375 } 376 377 void 378 svc_getreqset(fd_set *readfds) 379 { 380 svc_getreqset2(readfds, FD_SETSIZE); 381 } 382 383 void 384 svc_getreqset2(fd_set *readfds, int width) 385 { 386 enum xprt_stat stat; 387 struct rpc_msg msg; 388 int prog_found; 389 u_long low_vers; 390 u_long high_vers; 391 struct svc_req r; 392 SVCXPRT *xprt; 393 int bit; 394 int sock; 395 fd_mask mask, *maskp; 396 char cred_area[2*MAX_AUTH_BYTES + RQCRED_SIZE]; 397 msg.rm_call.cb_cred.oa_base = cred_area; 398 msg.rm_call.cb_verf.oa_base = &(cred_area[MAX_AUTH_BYTES]); 399 r.rq_clntcred = &(cred_area[2*MAX_AUTH_BYTES]); 400 401 402 maskp = readfds->fds_bits; 403 for (sock = 0; sock < width; sock += NFDBITS) { 404 for (mask = *maskp++; (bit = ffs(mask)); mask ^= (1 << (bit - 1))) { 405 /* sock has input waiting */ 406 xprt = xports[sock + bit - 1]; 407 if (xprt == NULL) 408 /* But do we control sock? */ 409 continue; 410 /* now receive msgs from xprtprt (support batch calls) */ 411 do { 412 if (SVC_RECV(xprt, &msg)) { 413 414 /* now find the exported program and call it */ 415 struct svc_callout *s; 416 enum auth_stat why; 417 418 r.rq_xprt = xprt; 419 r.rq_prog = msg.rm_call.cb_prog; 420 r.rq_vers = msg.rm_call.cb_vers; 421 r.rq_proc = msg.rm_call.cb_proc; 422 r.rq_cred = msg.rm_call.cb_cred; 423 /* first authenticate the message */ 424 if ((why= _authenticate(&r, &msg)) != AUTH_OK) { 425 svcerr_auth(xprt, why); 426 goto call_done; 427 } 428 /* now match message with a registered service*/ 429 prog_found = FALSE; 430 low_vers = (u_long) - 1; 431 high_vers = 0; 432 for (s = svc_head; s != NULL_SVC; s = s->sc_next) { 433 if (s->sc_prog == r.rq_prog) { 434 if (s->sc_vers == r.rq_vers) { 435 (*s->sc_dispatch)(&r, xprt); 436 goto call_done; 437 } /* found correct version */ 438 prog_found = TRUE; 439 if (s->sc_vers < low_vers) 440 low_vers = s->sc_vers; 441 if (s->sc_vers > high_vers) 442 high_vers = s->sc_vers; 443 } /* found correct program */ 444 } 445 /* 446 * if we got here, the program or version 447 * is not served ... 448 */ 449 if (prog_found) 450 svcerr_progvers(xprt, 451 low_vers, high_vers); 452 else 453 svcerr_noprog(xprt); 454 /* Fall through to ... */ 455 } 456 call_done: 457 if ((stat = SVC_STAT(xprt)) == XPRT_DIED){ 458 SVC_DESTROY(xprt); 459 break; 460 } 461 } while (stat == XPRT_MOREREQS); 462 } 463 } 464 } 465