ypset.c revision 1.1
1#include <sys/param.h>
2#include <sys/types.h>
3#include <sys/socket.h>
4#include <stdio.h>
5#include <netdb.h>
6#include <rpc/rpc.h>
7#include <rpc/xdr.h>
8#include <rpcsvc/yp_prot.h>
9#include <rpcsvc/ypclnt.h>
10
11extern bool_t xdr_domainname();
12
13usage()
14{
15	fprintf(stderr, "Usage:\n");
16	fprintf(stderr, "\typset [-h host ] [-d domain] server\n");
17	exit(1);
18}
19
20bind_tohost(sin, dom, server)
21struct sockaddr_in *sin;
22char *dom, *server;
23{
24	struct ypbind_setdom ypsd;
25	struct timeval tv;
26	CLIENT *client;
27	int sock, port;
28	int r;
29
30	if( (port=htons(getrpcport(server, YPPROG, YPPROC_NULL, IPPROTO_UDP))) == 0) {
31		fprintf(stderr, "%s not running ypserv.\n", server);
32		exit(1);
33	}
34
35	bzero(&ypsd, sizeof ypsd);
36	strncpy(ypsd.ypsetdom_domain, dom, sizeof ypsd.ypsetdom_domain);
37	ypsd.ypsetdom_addr = sin->sin_addr;
38	ypsd.ypsetdom_vers = YPVERS;
39	ypsd.ypsetdom_port = port;
40
41	tv.tv_sec = 15;
42	tv.tv_usec = 0;
43	sock = RPC_ANYSOCK;
44	client = clntudp_create(sin, YPBINDPROG, YPBINDVERS, &sock, 0, 0);
45	if (client==NULL) {
46		fprintf(stderr, "can't yp_bind: Reason: %s\n",
47			yperr_string(YPERR_YPBIND));
48		return YPERR_YPBIND;
49	}
50	client->cl_auth = authunix_create_default();
51
52	r = clnt_call(client, YPBINDPROC_SETDOM,
53		xdr_ypbind_setdom, &ypsd, xdr_void, NULL, tv);
54	if(r) {
55		fprintf(stderr, "Sorry, cannot ypset for domain %s on host.\n", dom);
56		clnt_destroy(client);
57		return YPERR_YPBIND;
58	}
59	clnt_destroy(client);
60	return 0;
61}
62
63int
64main(argc, argv)
65char **argv;
66{
67	struct sockaddr_in sin;
68	struct hostent *hent;
69	extern char *optarg;
70	extern int optind;
71	char *domainname;
72	int c;
73
74	yp_get_default_domain(&domainname);
75
76	bzero(&sin, sizeof sin);
77	sin.sin_family = AF_INET;
78	sin.sin_addr.s_addr = htonl(0x7f000001);
79
80	while( (c=getopt(argc, argv, "h:d:")) != -1)
81		switch(c) {
82		case 'd':
83			domainname = optarg;
84			break;
85		case 'h':
86			if( (sin.sin_addr.s_addr=inet_addr(optarg)) == -1) {
87				hent = gethostbyname(optarg);
88				if(hent==NULL) {
89					fprintf(stderr, "ypset: host %s unknown\n",
90						optarg);
91					exit(1);
92				}
93				bcopy(&hent->h_addr_list[0], &sin.sin_addr,
94					sizeof sin.sin_addr);
95			}
96			break;
97		default:
98			usage();
99		}
100
101	if(optind + 1 != argc )
102		usage();
103
104	if (bind_tohost(&sin, domainname, argv[optind]))
105		exit(1);
106	exit(0);
107}
108