Path: blob/main/crypto/krb5/src/lib/rpc/pmap_clnt.c
39536 views
/* @(#)pmap_clnt.c 2.2 88/08/01 4.0 RPCSRC */1/*2* Copyright (c) 2010, Oracle America, Inc.3*4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions are met:8*9* * Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* * Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in14* the documentation and/or other materials provided with the15* distribution.16*17* * Neither the name of the "Oracle America, Inc." nor the names of18* its contributors may be used to endorse or promote products19* derived from this software without specific prior written permission.20*21* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS22* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED23* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A24* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT25* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,26* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED27* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR28* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF29* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING30* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS31* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.32*/33#if !defined(lint) && defined(SCCSIDS)34static char sccsid[] = "@(#)pmap_clnt.c 1.37 87/08/11 Copyr 1984 Sun Micro";35#endif3637/*38* pmap_clnt.c39* Client interface to pmap rpc service.40*/4142#include <unistd.h>43#include <gssrpc/rpc.h>44#include <gssrpc/pmap_prot.h>45#include <gssrpc/pmap_clnt.h>4647#if TARGET_OS_MAC48#include <sys/un.h>49#include <string.h>50#include <syslog.h>51#endif5253static struct timeval timeout = { 5, 0 };54static struct timeval tottimeout = { 60, 0 };5556/*57* Set a mapping between program,version and port.58* Calls the pmap service remotely to do the mapping.59*/60bool_t61pmap_set(62rpcprog_t program,63rpcvers_t version,64rpcprot_t protocol,65u_int port)66{67struct sockaddr_in myaddress;68int sock = -1;69CLIENT *client;70struct pmap parms;71bool_t rslt;7273get_myaddress(&myaddress);74client = clntudp_bufcreate(&myaddress, PMAPPROG, PMAPVERS,75timeout, &sock, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);76if (client == (CLIENT *)NULL)77return (FALSE);78parms.pm_prog = program;79parms.pm_vers = version;80parms.pm_prot = protocol;81parms.pm_port = port;82#if TARGET_OS_MAC83{84/*85* Poke launchd, then wait for portmap to start up.86*87* My impression is that the protocol involves getting88* something back from the server. So wait, briefly, to89* see if it's going to send us something. Then continue90* on, regardless. I don't actually check what the data91* is, because I have no idea what sort of validation, if92* any, is needed.93*94* However, for whatever reason, the socket seems to be95* mode 700 owner root on my system, so if you don't96* change its ownership or mode, and if the program isn't97* running as root, you still lose.98*/99#define TICKLER_SOCKET "/var/run/portmap.socket"100int tickle;101struct sockaddr_un a = {102.sun_family = AF_UNIX,103.sun_path = TICKLER_SOCKET,104.sun_len = (sizeof(TICKLER_SOCKET)105+ offsetof(struct sockaddr_un, sun_path)),106};107108if (sizeof(TICKLER_SOCKET) <= sizeof(a.sun_path)) {109tickle = socket(AF_UNIX, SOCK_STREAM, 0);110if (tickle >= 0) {111if (connect(tickle, (struct sockaddr *)&a, a.sun_len) == 0112&& tickle < FD_SETSIZE) {113fd_set readfds;114struct timeval tv;115116FD_ZERO(&readfds);117/* XXX Range check. */118FD_SET(tickle, &readfds);119tv.tv_sec = 5;120tv.tv_usec = 0;121(void) select(tickle+1, &readfds, 0, 0, &tv);122}123close(tickle);124}125}126}127#endif128if (CLNT_CALL(client, PMAPPROC_SET, (xdrproc_t)xdr_pmap, &parms,129(xdrproc_t)xdr_bool, &rslt,130tottimeout) != RPC_SUCCESS) {131clnt_perror(client, "Cannot register service");132return (FALSE);133}134CLNT_DESTROY(client);135(void)close(sock);136return (rslt);137}138139/*140* Remove the mapping between program,version and port.141* Calls the pmap service remotely to do the un-mapping.142*/143bool_t144pmap_unset(145rpcprog_t program,146rpcvers_t version)147{148struct sockaddr_in myaddress;149int sock = -1;150CLIENT *client;151struct pmap parms;152bool_t rslt;153154get_myaddress(&myaddress);155client = clntudp_bufcreate(&myaddress, PMAPPROG, PMAPVERS,156timeout, &sock, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);157if (client == (CLIENT *)NULL)158return (FALSE);159parms.pm_prog = program;160parms.pm_vers = version;161parms.pm_port = parms.pm_prot = 0;162CLNT_CALL(client, PMAPPROC_UNSET, (xdrproc_t)xdr_pmap, &parms,163(xdrproc_t)xdr_bool, &rslt, tottimeout);164CLNT_DESTROY(client);165(void)close(sock);166return (rslt);167}168169170