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