1 /* $NetBSD: svc_vc.c,v 1.7 2000/08/03 00:01:53 fvdl Exp $ */ 2 /* $FreeBSD$ */ 3 4 /* 5 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for 6 * unrestricted use provided that this legend is included on all tape 7 * media and as a part of the software program in whole or part. Users 8 * may copy or modify Sun RPC without charge, but are not authorized 9 * to license or distribute it to anyone else except as part of a product or 10 * program developed by the user. 11 * 12 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 13 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 14 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 15 * 16 * Sun RPC is provided with no support and without any obligation on the 17 * part of Sun Microsystems, Inc. to assist in its use, correction, 18 * modification or enhancement. 19 * 20 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 21 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 22 * OR ANY PART THEREOF. 23 * 24 * In no event will Sun Microsystems, Inc. be liable for any lost revenue 25 * or profits or other special, indirect and consequential damages, even if 26 * Sun has been advised of the possibility of such damages. 27 * 28 * Sun Microsystems, Inc. 29 * 2550 Garcia Avenue 30 * Mountain View, California 94043 31 */ 32 33 #include <sys/cdefs.h> 34 #if defined(LIBC_SCCS) && !defined(lint) 35 static char *sccsid = "@(#)svc_tcp.c 1.21 87/08/11 Copyr 1984 Sun Micro"; 36 static char *sccsid = "@(#)svc_tcp.c 2.2 88/08/01 4.0 RPCSRC"; 37 #endif 38 39 /* 40 * svc_vc.c, Server side for Connection Oriented based RPC. 41 * 42 * Actually implements two flavors of transporter - 43 * a tcp rendezvouser (a listner and connection establisher) 44 * and a record/tcp stream. 45 */ 46 47 #include "namespace.h" 48 #include "reentrant.h" 49 #include <sys/types.h> 50 #include <sys/param.h> 51 #include <sys/poll.h> 52 #include <sys/socket.h> 53 #include <sys/un.h> 54 #include <sys/uio.h> 55 #include <netinet/in.h> 56 #include <netinet/tcp.h> 57 58 #include <assert.h> 59 #include <err.h> 60 #include <errno.h> 61 #include <stdio.h> 62 #include <stdlib.h> 63 #include <string.h> 64 #include <unistd.h> 65 66 #include <rpc/rpc.h> 67 68 #include "rpc_com.h" 69 #include "un-namespace.h" 70 71 struct cmessage { 72 struct cmsghdr cmsg; 73 struct cmsgcred cmcred; 74 }; 75 76 static SVCXPRT *makefd_xprt __P((int, u_int, u_int)); 77 static bool_t rendezvous_request __P((SVCXPRT *, struct rpc_msg *)); 78 static enum xprt_stat rendezvous_stat __P((SVCXPRT *)); 79 static void svc_vc_destroy __P((SVCXPRT *)); 80 static int read_vc __P((caddr_t, caddr_t, int)); 81 static int write_vc __P((caddr_t, caddr_t, int)); 82 static enum xprt_stat svc_vc_stat __P((SVCXPRT *)); 83 static bool_t svc_vc_recv __P((SVCXPRT *, struct rpc_msg *)); 84 static bool_t svc_vc_getargs __P((SVCXPRT *, xdrproc_t, caddr_t)); 85 static bool_t svc_vc_freeargs __P((SVCXPRT *, xdrproc_t, caddr_t)); 86 static bool_t svc_vc_reply __P((SVCXPRT *, struct rpc_msg *)); 87 static void svc_vc_rendezvous_ops __P((SVCXPRT *)); 88 static void svc_vc_ops __P((SVCXPRT *)); 89 static bool_t svc_vc_control __P((SVCXPRT *xprt, const u_int rq, void *in)); 90 static int __msgread_withcred(int, void *, size_t, struct cmessage *); 91 static int __msgwrite(int, void *, size_t); 92 93 struct cf_rendezvous { /* kept in xprt->xp_p1 for rendezvouser */ 94 u_int sendsize; 95 u_int recvsize; 96 }; 97 98 struct cf_conn { /* kept in xprt->xp_p1 for actual connection */ 99 enum xprt_stat strm_stat; 100 u_int32_t x_id; 101 XDR xdrs; 102 char verf_body[MAX_AUTH_BYTES]; 103 }; 104 105 /* 106 * Usage: 107 * xprt = svc_vc_create(sock, send_buf_size, recv_buf_size); 108 * 109 * Creates, registers, and returns a (rpc) tcp based transporter. 110 * Once *xprt is initialized, it is registered as a transporter 111 * see (svc.h, xprt_register). This routine returns 112 * a NULL if a problem occurred. 113 * 114 * The filedescriptor passed in is expected to refer to a bound, but 115 * not yet connected socket. 116 * 117 * Since streams do buffered io similar to stdio, the caller can specify 118 * how big the send and receive buffers are via the second and third parms; 119 * 0 => use the system default. 120 */ 121 SVCXPRT * 122 svc_vc_create(fd, sendsize, recvsize) 123 int fd; 124 u_int sendsize; 125 u_int recvsize; 126 { 127 SVCXPRT *xprt; 128 struct cf_rendezvous *r = NULL; 129 struct __rpc_sockinfo si; 130 struct sockaddr_storage sslocal; 131 socklen_t slen; 132 int one = 1; 133 134 r = mem_alloc(sizeof(*r)); 135 if (r == NULL) { 136 warnx("svc_vc_create: out of memory"); 137 goto cleanup_svc_vc_create; 138 } 139 if (!__rpc_fd2sockinfo(fd, &si)) 140 return NULL; 141 r->sendsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsize); 142 r->recvsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsize); 143 xprt = mem_alloc(sizeof(SVCXPRT)); 144 if (xprt == NULL) { 145 warnx("svc_vc_create: out of memory"); 146 goto cleanup_svc_vc_create; 147 } 148 xprt->xp_tp = NULL; 149 xprt->xp_p1 = (caddr_t)(void *)r; 150 xprt->xp_p2 = NULL; 151 xprt->xp_p3 = NULL; 152 xprt->xp_verf = _null_auth; 153 svc_vc_rendezvous_ops(xprt); 154 xprt->xp_port = (u_short)-1; /* It is the rendezvouser */ 155 xprt->xp_fd = fd; 156 157 slen = sizeof (struct sockaddr_storage); 158 if (_getsockname(fd, (struct sockaddr *)(void *)&sslocal, &slen) < 0) { 159 warnx("svc_vc_create: could not retrieve local addr"); 160 goto cleanup_svc_vc_create; 161 } 162 163 xprt->xp_ltaddr.maxlen = xprt->xp_ltaddr.len = sslocal.ss_len; 164 xprt->xp_ltaddr.buf = mem_alloc((size_t)sslocal.ss_len); 165 if (xprt->xp_ltaddr.buf == NULL) { 166 warnx("svc_vc_create: no mem for local addr"); 167 goto cleanup_svc_vc_create; 168 } 169 memcpy(xprt->xp_ltaddr.buf, &sslocal, (size_t)sslocal.ss_len); 170 171 xprt->xp_rtaddr.maxlen = sizeof (struct sockaddr_storage); 172 xprt_register(xprt); 173 return (xprt); 174 cleanup_svc_vc_create: 175 if (r != NULL) 176 mem_free(r, sizeof(*r)); 177 return (NULL); 178 } 179 180 /* 181 * Like svtcp_create(), except the routine takes any *open* UNIX file 182 * descriptor as its first input. 183 */ 184 SVCXPRT * 185 svc_fd_create(fd, sendsize, recvsize) 186 int fd; 187 u_int sendsize; 188 u_int recvsize; 189 { 190 struct sockaddr_storage ss; 191 socklen_t slen; 192 SVCXPRT *ret; 193 194 assert(fd != -1); 195 196 ret = makefd_xprt(fd, sendsize, recvsize); 197 if (ret == NULL) 198 return NULL; 199 200 slen = sizeof (struct sockaddr_storage); 201 if (_getsockname(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) { 202 warnx("svc_fd_create: could not retrieve local addr"); 203 goto freedata; 204 } 205 ret->xp_ltaddr.maxlen = ret->xp_ltaddr.len = ss.ss_len; 206 ret->xp_ltaddr.buf = mem_alloc((size_t)ss.ss_len); 207 if (ret->xp_ltaddr.buf == NULL) { 208 warnx("svc_fd_create: no mem for local addr"); 209 goto freedata; 210 } 211 memcpy(ret->xp_ltaddr.buf, &ss, (size_t)ss.ss_len); 212 213 slen = sizeof (struct sockaddr_storage); 214 if (_getpeername(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) { 215 warnx("svc_fd_create: could not retrieve remote addr"); 216 goto freedata; 217 } 218 ret->xp_rtaddr.maxlen = ret->xp_rtaddr.len = ss.ss_len; 219 ret->xp_rtaddr.buf = mem_alloc((size_t)ss.ss_len); 220 if (ret->xp_rtaddr.buf == NULL) { 221 warnx("svc_fd_create: no mem for local addr"); 222 goto freedata; 223 } 224 memcpy(ret->xp_rtaddr.buf, &ss, (size_t)ss.ss_len); 225 #ifdef PORTMAP 226 if (ss.ss_family == AF_INET) { 227 ret->xp_raddr = *(struct sockaddr_in *)ret->xp_rtaddr.buf; 228 ret->xp_addrlen = sizeof (struct sockaddr_in); 229 } 230 #endif /* PORTMAP */ 231 232 return ret; 233 234 freedata: 235 if (ret->xp_ltaddr.buf != NULL) 236 mem_free(ret->xp_ltaddr.buf, rep->xp_ltaddr.maxlen); 237 238 return NULL; 239 } 240 241 static SVCXPRT * 242 makefd_xprt(fd, sendsize, recvsize) 243 int fd; 244 u_int sendsize; 245 u_int recvsize; 246 { 247 SVCXPRT *xprt; 248 struct cf_conn *cd; 249 const char *netid; 250 struct __rpc_sockinfo si; 251 252 assert(fd != -1); 253 254 xprt = mem_alloc(sizeof(SVCXPRT)); 255 if (xprt == NULL) { 256 warnx("svc_vc: makefd_xprt: out of memory"); 257 goto done; 258 } 259 memset(xprt, 0, sizeof *xprt); 260 cd = mem_alloc(sizeof(struct cf_conn)); 261 if (cd == NULL) { 262 warnx("svc_tcp: makefd_xprt: out of memory"); 263 mem_free(xprt, sizeof(SVCXPRT)); 264 xprt = NULL; 265 goto done; 266 } 267 cd->strm_stat = XPRT_IDLE; 268 xdrrec_create(&(cd->xdrs), sendsize, recvsize, 269 (caddr_t)(void *)xprt, read_vc, write_vc); 270 xprt->xp_p1 = (caddr_t)(void *)cd; 271 xprt->xp_verf.oa_base = cd->verf_body; 272 svc_vc_ops(xprt); /* truely deals with calls */ 273 xprt->xp_port = 0; /* this is a connection, not a rendezvouser */ 274 xprt->xp_fd = fd; 275 if (__rpc_fd2sockinfo(fd, &si) && __rpc_sockinfo2netid(&si, &netid)) 276 xprt->xp_netid = strdup(netid); 277 278 xprt_register(xprt); 279 done: 280 return (xprt); 281 } 282 283 /*ARGSUSED*/ 284 static bool_t 285 rendezvous_request(xprt, msg) 286 SVCXPRT *xprt; 287 struct rpc_msg *msg; 288 { 289 int sock; 290 struct cf_rendezvous *r; 291 struct sockaddr_storage addr; 292 socklen_t len; 293 struct __rpc_sockinfo si; 294 295 assert(xprt != NULL); 296 assert(msg != NULL); 297 298 r = (struct cf_rendezvous *)xprt->xp_p1; 299 again: 300 len = sizeof addr; 301 if ((sock = _accept(xprt->xp_fd, (struct sockaddr *)(void *)&addr, 302 &len)) < 0) { 303 if (errno == EINTR) 304 goto again; 305 return (FALSE); 306 } 307 /* 308 * make a new transporter (re-uses xprt) 309 */ 310 xprt = makefd_xprt(sock, r->sendsize, r->recvsize); 311 xprt->xp_rtaddr.buf = mem_alloc(len); 312 if (xprt->xp_rtaddr.buf == NULL) 313 return (FALSE); 314 memcpy(xprt->xp_rtaddr.buf, &addr, len); 315 xprt->xp_rtaddr.len = len; 316 #ifdef PORTMAP 317 if (addr.ss_family == AF_INET) { 318 xprt->xp_raddr = *(struct sockaddr_in *)xprt->xp_rtaddr.buf; 319 xprt->xp_addrlen = sizeof (struct sockaddr_in); 320 } 321 #endif /* PORTMAP */ 322 if (__rpc_fd2sockinfo(sock, &si) && si.si_proto == IPPROTO_TCP) { 323 len = 1; 324 /* XXX fvdl - is this useful? */ 325 _setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &len, sizeof (len)); 326 } 327 return (FALSE); /* there is never an rpc msg to be processed */ 328 } 329 330 /*ARGSUSED*/ 331 static enum xprt_stat 332 rendezvous_stat(xprt) 333 SVCXPRT *xprt; 334 { 335 336 return (XPRT_IDLE); 337 } 338 339 static void 340 svc_vc_destroy(xprt) 341 SVCXPRT *xprt; 342 { 343 struct cf_conn *cd; 344 struct cf_rendezvous *r; 345 346 assert(xprt != NULL); 347 348 cd = (struct cf_conn *)xprt->xp_p1; 349 350 xprt_unregister(xprt); 351 if (xprt->xp_fd != RPC_ANYFD) 352 (void)_close(xprt->xp_fd); 353 if (xprt->xp_port != 0) { 354 /* a rendezvouser socket */ 355 r = (struct cf_rendezvous *)xprt->xp_p1; 356 mem_free(r, sizeof (struct cf_rendezvous)); 357 xprt->xp_port = 0; 358 } else { 359 /* an actual connection socket */ 360 XDR_DESTROY(&(cd->xdrs)); 361 mem_free(cd, sizeof(struct cf_conn)); 362 } 363 if (xprt->xp_rtaddr.buf) 364 mem_free(xprt->xp_rtaddr.buf, xprt->xp_rtaddr.maxlen); 365 if (xprt->xp_ltaddr.buf) 366 mem_free(xprt->xp_ltaddr.buf, xprt->xp_ltaddr.maxlen); 367 if (xprt->xp_tp) 368 free(xprt->xp_tp); 369 if (xprt->xp_netid) 370 free(xprt->xp_netid); 371 mem_free(xprt, sizeof(SVCXPRT)); 372 } 373 374 /*ARGSUSED*/ 375 static bool_t 376 svc_vc_control(xprt, rq, in) 377 SVCXPRT *xprt; 378 const u_int rq; 379 void *in; 380 { 381 return (FALSE); 382 } 383 384 /* 385 * reads data from the tcp or uip connection. 386 * any error is fatal and the connection is closed. 387 * (And a read of zero bytes is a half closed stream => error.) 388 * All read operations timeout after 35 seconds. A timeout is 389 * fatal for the connection. 390 */ 391 static int 392 read_vc(xprtp, buf, len) 393 caddr_t xprtp; 394 caddr_t buf; 395 int len; 396 { 397 SVCXPRT *xprt; 398 int sock; 399 int milliseconds = 35 * 1000; 400 struct pollfd pollfd; 401 struct sockaddr *sa; 402 struct cmessage *cm; 403 struct cmsghdr *cmp; 404 struct sockcred *sc; 405 406 xprt = (SVCXPRT *)(void *)xprtp; 407 assert(xprt != NULL); 408 409 sock = xprt->xp_fd; 410 411 do { 412 pollfd.fd = sock; 413 pollfd.events = POLLIN; 414 pollfd.revents = 0; 415 switch (_poll(&pollfd, 1, milliseconds)) { 416 case -1: 417 if (errno == EINTR) 418 continue; 419 /*FALLTHROUGH*/ 420 case 0: 421 goto fatal_err; 422 423 default: 424 break; 425 } 426 } while ((pollfd.revents & POLLIN) == 0); 427 428 sa = (struct sockaddr *)xprt->xp_rtaddr.buf; 429 if (sa->sa_family == AF_LOCAL) { 430 cm = (struct cmessage *)xprt->xp_verf.oa_base; 431 if ((len = __msgread_withcred(sock, buf, len, cm)) > 0) { 432 cmp = &cm->cmsg; 433 sc = (struct sockcred *)(void *)CMSG_DATA(cmp); 434 xprt->xp_p2 = sc; 435 return (len); 436 } 437 } else { 438 if ((len = _read(sock, buf, (size_t)len)) > 0) 439 return (len); 440 } 441 442 fatal_err: 443 ((struct cf_conn *)(xprt->xp_p1))->strm_stat = XPRT_DIED; 444 return (-1); 445 } 446 447 /* 448 * writes data to the tcp connection. 449 * Any error is fatal and the connection is closed. 450 */ 451 static int 452 write_vc(xprtp, buf, len) 453 caddr_t xprtp; 454 caddr_t buf; 455 int len; 456 { 457 SVCXPRT *xprt; 458 int i, cnt; 459 struct sockaddr *sa; 460 461 xprt = (SVCXPRT *)(void *)xprtp; 462 assert(xprt != NULL); 463 464 sa = (struct sockaddr *)xprt->xp_rtaddr.buf; 465 if (sa->sa_family == AF_LOCAL) { 466 for (cnt = len; cnt > 0; cnt -= i, buf += i) { 467 if ((i = __msgwrite(xprt->xp_fd, buf, 468 (size_t)cnt)) < 0) { 469 ((struct cf_conn *)(xprt->xp_p1))->strm_stat = 470 XPRT_DIED; 471 return (-1); 472 } 473 } 474 } else { 475 for (cnt = len; cnt > 0; cnt -= i, buf += i) { 476 if ((i = _write(xprt->xp_fd, buf, 477 (size_t)cnt)) < 0) { 478 ((struct cf_conn *)(xprt->xp_p1))->strm_stat = 479 XPRT_DIED; 480 return (-1); 481 } 482 } 483 } 484 485 return (len); 486 } 487 488 static enum xprt_stat 489 svc_vc_stat(xprt) 490 SVCXPRT *xprt; 491 { 492 struct cf_conn *cd; 493 494 assert(xprt != NULL); 495 496 cd = (struct cf_conn *)(xprt->xp_p1); 497 498 if (cd->strm_stat == XPRT_DIED) 499 return (XPRT_DIED); 500 if (! xdrrec_eof(&(cd->xdrs))) 501 return (XPRT_MOREREQS); 502 return (XPRT_IDLE); 503 } 504 505 static bool_t 506 svc_vc_recv(xprt, msg) 507 SVCXPRT *xprt; 508 struct rpc_msg *msg; 509 { 510 struct cf_conn *cd; 511 XDR *xdrs; 512 513 assert(xprt != NULL); 514 assert(msg != NULL); 515 516 cd = (struct cf_conn *)(xprt->xp_p1); 517 xdrs = &(cd->xdrs); 518 519 xdrs->x_op = XDR_DECODE; 520 (void)xdrrec_skiprecord(xdrs); 521 if (xdr_callmsg(xdrs, msg)) { 522 cd->x_id = msg->rm_xid; 523 return (TRUE); 524 } 525 cd->strm_stat = XPRT_DIED; 526 return (FALSE); 527 } 528 529 static bool_t 530 svc_vc_getargs(xprt, xdr_args, args_ptr) 531 SVCXPRT *xprt; 532 xdrproc_t xdr_args; 533 caddr_t args_ptr; 534 { 535 536 assert(xprt != NULL); 537 /* args_ptr may be NULL */ 538 return ((*xdr_args)(&(((struct cf_conn *)(xprt->xp_p1))->xdrs), 539 args_ptr)); 540 } 541 542 static bool_t 543 svc_vc_freeargs(xprt, xdr_args, args_ptr) 544 SVCXPRT *xprt; 545 xdrproc_t xdr_args; 546 caddr_t args_ptr; 547 { 548 XDR *xdrs; 549 550 assert(xprt != NULL); 551 /* args_ptr may be NULL */ 552 553 xdrs = &(((struct cf_conn *)(xprt->xp_p1))->xdrs); 554 555 xdrs->x_op = XDR_FREE; 556 return ((*xdr_args)(xdrs, args_ptr)); 557 } 558 559 static bool_t 560 svc_vc_reply(xprt, msg) 561 SVCXPRT *xprt; 562 struct rpc_msg *msg; 563 { 564 struct cf_conn *cd; 565 XDR *xdrs; 566 bool_t stat; 567 568 assert(xprt != NULL); 569 assert(msg != NULL); 570 571 cd = (struct cf_conn *)(xprt->xp_p1); 572 xdrs = &(cd->xdrs); 573 574 xdrs->x_op = XDR_ENCODE; 575 msg->rm_xid = cd->x_id; 576 stat = xdr_replymsg(xdrs, msg); 577 (void)xdrrec_endofrecord(xdrs, TRUE); 578 return (stat); 579 } 580 581 static void 582 svc_vc_ops(xprt) 583 SVCXPRT *xprt; 584 { 585 static struct xp_ops ops; 586 static struct xp_ops2 ops2; 587 extern mutex_t ops_lock; 588 589 /* VARIABLES PROTECTED BY ops_lock: ops, ops2 */ 590 591 mutex_lock(&ops_lock); 592 if (ops.xp_recv == NULL) { 593 ops.xp_recv = svc_vc_recv; 594 ops.xp_stat = svc_vc_stat; 595 ops.xp_getargs = svc_vc_getargs; 596 ops.xp_reply = svc_vc_reply; 597 ops.xp_freeargs = svc_vc_freeargs; 598 ops.xp_destroy = svc_vc_destroy; 599 ops2.xp_control = svc_vc_control; 600 } 601 xprt->xp_ops = &ops; 602 xprt->xp_ops2 = &ops2; 603 mutex_unlock(&ops_lock); 604 } 605 606 static void 607 svc_vc_rendezvous_ops(xprt) 608 SVCXPRT *xprt; 609 { 610 static struct xp_ops ops; 611 static struct xp_ops2 ops2; 612 extern mutex_t ops_lock; 613 614 mutex_lock(&ops_lock); 615 if (ops.xp_recv == NULL) { 616 ops.xp_recv = rendezvous_request; 617 ops.xp_stat = rendezvous_stat; 618 ops.xp_getargs = 619 (bool_t (*) __P((SVCXPRT *, xdrproc_t, caddr_t)))abort; 620 ops.xp_reply = 621 (bool_t (*) __P((SVCXPRT *, struct rpc_msg *)))abort; 622 ops.xp_freeargs = 623 (bool_t (*) __P((SVCXPRT *, xdrproc_t, caddr_t)))abort, 624 ops.xp_destroy = svc_vc_destroy; 625 ops2.xp_control = svc_vc_control; 626 } 627 xprt->xp_ops = &ops; 628 xprt->xp_ops2 = &ops2; 629 mutex_unlock(&ops_lock); 630 } 631 632 int 633 __msgread_withcred(sock, buf, cnt, cmp) 634 int sock; 635 void *buf; 636 size_t cnt; 637 struct cmessage *cmp; 638 { 639 struct iovec iov[1]; 640 struct msghdr msg; 641 642 bzero(cmp, sizeof(*cmp)); 643 iov[0].iov_base = buf; 644 iov[0].iov_len = cnt; 645 646 msg.msg_iov = iov; 647 msg.msg_iovlen = 1; 648 msg.msg_name = NULL; 649 msg.msg_namelen = 0; 650 msg.msg_control = cmp; 651 msg.msg_controllen = sizeof(struct cmessage); 652 msg.msg_flags = 0; 653 654 return(_recvmsg(sock, &msg, 0)); 655 } 656 657 static int 658 __msgwrite(sock, buf, cnt) 659 int sock; 660 void *buf; 661 size_t cnt; 662 { 663 struct iovec iov[1]; 664 struct msghdr msg; 665 struct cmessage cm; 666 667 bzero((char *)&cm, sizeof(cm)); 668 iov[0].iov_base = buf; 669 iov[0].iov_len = cnt; 670 671 cm.cmsg.cmsg_type = SCM_CREDS; 672 cm.cmsg.cmsg_level = SOL_SOCKET; 673 cm.cmsg.cmsg_len = sizeof(struct cmessage); 674 675 msg.msg_iov = iov; 676 msg.msg_iovlen = 1; 677 msg.msg_name = NULL; 678 msg.msg_namelen = 0; 679 msg.msg_control = (caddr_t)&cm; 680 msg.msg_controllen = sizeof(struct cmessage); 681 msg.msg_flags = 0; 682 683 return(_sendmsg(sock, &msg, 0)); 684 } 685 686 /* 687 * Get the effective UID of the sending process. Used by rpcbind and keyserv 688 * (AF_LOCAL). 689 */ 690 int 691 __rpc_get_local_uid(SVCXPRT *transp, uid_t *uid) 692 { 693 struct cmsgcred *cmcred; 694 695 cmcred = __svc_getcallercreds(transp); 696 if (cmcred == NULL) 697 return(-1); 698 *uid = cmcred->cmcred_euid; 699 return(0); 700 } 701