ypset.c revision 12862
11929Swollman/*
21929Swollman * Copyright (c) 1992/3 Theo de Raadt <deraadt@fsa.ca>
31929Swollman * All rights reserved.
41929Swollman *
51929Swollman * Redistribution and use in source and binary forms, with or without
61929Swollman * modification, are permitted provided that the following conditions
71929Swollman * are met:
81929Swollman * 1. Redistributions of source code must retain the above copyright
91929Swollman *    notice, this list of conditions and the following disclaimer.
101929Swollman * 2. Redistributions in binary form must reproduce the above copyright
111929Swollman *    notice, this list of conditions and the following disclaimer in the
121929Swollman *    documentation and/or other materials provided with the distribution.
131929Swollman * 3. The name of the author may not be used to endorse or promote
141929Swollman *    products derived from this software without specific prior written
151929Swollman *    permission.
161929Swollman *
171929Swollman * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
181929Swollman * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
191929Swollman * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201929Swollman * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
211929Swollman * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221929Swollman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231929Swollman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241929Swollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251929Swollman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261929Swollman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271929Swollman * SUCH DAMAGE.
281929Swollman */
291929Swollman
301929Swollman#ifndef LINT
311929Swollmanstatic char rcsid[] = "ypset.c,v 1.3 1993/06/12 00:02:37 deraadt Exp";
321929Swollman#endif
331929Swollman
341929Swollman#include <sys/param.h>
351929Swollman#include <sys/types.h>
361929Swollman#include <sys/socket.h>
371929Swollman#include <stdio.h>
381929Swollman#include <netdb.h>
391929Swollman#include <rpc/rpc.h>
401929Swollman#include <rpc/xdr.h>
4112862Swpaul#include <rpcsvc/yp.h>
4212862Swpaulstruct dom_binding{};
431929Swollman#include <rpcsvc/ypclnt.h>
441929Swollman#include <arpa/inet.h>
451929Swollman
461929Swollmanextern bool_t xdr_domainname();
471929Swollman
481929Swollmanusage()
491929Swollman{
501929Swollman	fprintf(stderr, "Usage:\n");
511929Swollman	fprintf(stderr, "\typset [-h host ] [-d domain] server\n");
521929Swollman	exit(1);
531929Swollman}
541929Swollman
551929Swollmanbind_tohost(sin, dom, server)
561929Swollmanstruct sockaddr_in *sin;
571929Swollmanchar *dom, *server;
581929Swollman{
591929Swollman	struct ypbind_setdom ypsd;
601929Swollman	struct timeval tv;
611929Swollman	struct hostent *hp;
621929Swollman	CLIENT *client;
631929Swollman	int sock, port;
641929Swollman	int r;
651929Swollman	unsigned long server_addr;
668857Srgrimes
671929Swollman	if( (port=htons(getrpcport(server, YPPROG, YPPROC_NULL, IPPROTO_UDP))) == 0) {
681929Swollman		fprintf(stderr, "%s not running ypserv.\n", server);
691929Swollman		exit(1);
701929Swollman	}
711929Swollman
721929Swollman	bzero(&ypsd, sizeof ypsd);
731929Swollman
741929Swollman	if( (hp = gethostbyname (server)) != NULL ) {
751929Swollman		/* is this the most compatible way?? */
7612862Swpaul		bcopy (hp->h_addr_list[0],
7712862Swpaul		       (u_long *)&ypsd.ypsetdom_binding.ypbind_binding_addr,
7812862Swpaul		       sizeof (unsigned long));
791929Swollman	} else if( (long)(server_addr = inet_addr (server)) == -1) {
801929Swollman		fprintf(stderr, "can't find address for %s\n", server);
811929Swollman		exit(1);
821929Swollman	} else
8312862Swpaul		bcopy (&server_addr,
8412862Swpaul		       *(u_long *)&ypsd.ypsetdom_binding.ypbind_binding_addr,
851929Swollman		       sizeof (server_addr));
861929Swollman
8712862Swpaul/*	strncpy(ypsd.ypsetdom_domain, dom, sizeof ypsd.ypsetdom_domain); */
8812862Swpaul	ypsd.ypsetdom_domain = dom;
8912862Swpaul	*(u_long *)&ypsd.ypsetdom_binding.ypbind_binding_port = port;
901929Swollman	ypsd.ypsetdom_vers = YPVERS;
918857Srgrimes
921929Swollman	tv.tv_sec = 15;
931929Swollman	tv.tv_usec = 0;
941929Swollman	sock = RPC_ANYSOCK;
951929Swollman	client = clntudp_create(sin, YPBINDPROG, YPBINDVERS, tv, &sock);
961929Swollman	if (client==NULL) {
971929Swollman		fprintf(stderr, "can't yp_bind: Reason: %s\n",
981929Swollman			yperr_string(YPERR_YPBIND));
991929Swollman		return YPERR_YPBIND;
1001929Swollman	}
1011929Swollman	client->cl_auth = authunix_create_default();
1021929Swollman
1031929Swollman	r = clnt_call(client, YPBINDPROC_SETDOM,
1041929Swollman		xdr_ypbind_setdom, &ypsd, xdr_void, NULL, tv);
1051929Swollman	if(r) {
1061929Swollman		fprintf(stderr, "Sorry, cannot ypset for domain %s on host.\n", dom);
1071929Swollman		clnt_destroy(client);
1081929Swollman		return YPERR_YPBIND;
1091929Swollman	}
1101929Swollman	clnt_destroy(client);
1111929Swollman	return 0;
1121929Swollman}
1131929Swollman
1141929Swollmanint
1151929Swollmanmain(argc, argv)
1161929Swollmanchar **argv;
1171929Swollman{
1181929Swollman	struct sockaddr_in sin;
1191929Swollman	struct hostent *hent;
1201929Swollman	extern char *optarg;
1211929Swollman	extern int optind;
1221929Swollman	char *domainname;
1231929Swollman	int c;
1241929Swollman
1251929Swollman	yp_get_default_domain(&domainname);
1261929Swollman
1271929Swollman	bzero(&sin, sizeof sin);
1281929Swollman	sin.sin_family = AF_INET;
1291929Swollman	sin.sin_addr.s_addr = htonl(0x7f000001);
1301929Swollman
1311929Swollman	while( (c=getopt(argc, argv, "h:d:")) != -1)
1321929Swollman		switch(c) {
1331929Swollman		case 'd':
1341929Swollman			domainname = optarg;
1351929Swollman			break;
1361929Swollman		case 'h':
1371929Swollman			if( (sin.sin_addr.s_addr=inet_addr(optarg)) == -1) {
1381929Swollman				hent = gethostbyname(optarg);
1391929Swollman				if(hent==NULL) {
1401929Swollman					fprintf(stderr, "ypset: host %s unknown\n",
1411929Swollman						optarg);
1421929Swollman					exit(1);
1431929Swollman				}
1441929Swollman				bcopy(&hent->h_addr_list[0], &sin.sin_addr,
1451929Swollman					sizeof sin.sin_addr);
1461929Swollman			}
1471929Swollman			break;
1481929Swollman		default:
1491929Swollman			usage();
1501929Swollman		}
1511929Swollman
1521929Swollman	if(optind + 1 != argc )
1531929Swollman		usage();
1541929Swollman
1551929Swollman	if (bind_tohost(&sin, domainname, argv[optind]))
1561929Swollman		exit(1);
1571929Swollman	exit(0);
1581929Swollman}
159