summaryrefslogtreecommitdiff
path: root/usr.sbin/ypserv/yppush/yppush.c
blob: 6807e2e444da574040bfe78027fbe242170aa4aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*	$OpenBSD: yppush.c,v 1.16 2001/11/19 09:01:43 deraadt Exp $ */

/*
 * Copyright (c) 1995 Mats O Jansson <moj@stacken.kth.se>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by Mats O Jansson
 * 4. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#ifndef lint
static char rcsid[] = "$OpenBSD: yppush.c,v 1.16 2001/11/19 09:01:43 deraadt Exp $";
#endif /* not lint */

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/resource.h>
#include <sys/wait.h>

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <ctype.h>

#include <rpc/rpc.h>
#include <rpc/xdr.h>
#include <rpcsvc/yp.h>
#include <rpcsvc/ypclnt.h>

#include <netdb.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>

#include "yplib_host.h"
#include "ypdef.h"
#include "ypdb.h"

int  Verbose = 0;
char Domain[MAXHOSTNAMELEN], Map[255];
u_int32_t OrderNum;
char *master;

extern void yppush_xfrrespprog_1(struct svc_req *request, SVCXPRT *xprt);
extern bool_t xdr_ypreq_xfr(XDR *, struct ypreq_xfr *);

void
usage()
{
	fprintf(stderr, "Usage:\n");
	fprintf(stderr, "\typpush [-d domainname] [-h host] [-v] mapname\n");
	exit(1);
}

void
_svc_run()
{
	fd_set *readfdsp = NULL;
	extern fd_set *__svc_fdset;
	extern int __svc_fdsetsize;
	struct timeval timeout;

	timeout.tv_sec = 60;
	timeout.tv_usec = 0;

	for (;;) {
		if (readfdsp)
			free(readfdsp);
		readfdsp = (fd_set *)calloc(howmany(__svc_fdsetsize, NFDBITS),
		    sizeof(fd_mask));
		if (readfdsp == NULL) {
			perror("calloc");
			return;
		}
		bcopy(__svc_fdset, readfdsp, howmany(__svc_fdsetsize, NFDBITS) *
		    sizeof(fd_mask));

		switch (select(svc_maxfd+1, readfdsp, NULL,
		    NULL, &timeout)) {
		case -1:
			if (errno == EINTR)
				continue;
			perror("yppush: _svc_run: select failed");
			return;
		case 0:
			fprintf(stderr, "yppush: Callback timed out.\n");
			exit(0);
		default:
			svc_getreqset2(readfdsp, svc_maxfd+1);
			break;
		}
	}
}

void
req_xfr(pid, prog, transp, host, client)
pid_t pid;
u_int prog;
SVCXPRT *transp;
char *host;
CLIENT *client;
{
	struct ypreq_xfr request;
	struct timeval tv;

	tv.tv_sec = 0;
	tv.tv_usec = 0;

	request.map_parms.domain=(char *)&Domain;
	request.map_parms.map=(char *)&Map;
	request.map_parms.peer=master;
	request.map_parms.ordernum=OrderNum;
	request.transid=(u_int)pid;
	request.prog=prog;
	request.port=transp->xp_port;

	if (Verbose)
		printf("%d: %s(%u@%s) -> %s@%s\n",
		    request.transid, request.map_parms.map,
		    request.map_parms.ordernum, host,
		    request.map_parms.peer, request.map_parms.domain);
	switch (clnt_call(client, YPPROC_XFR, xdr_ypreq_xfr, &request,
	    xdr_void, NULL, tv)) {
	case RPC_SUCCESS:
	case RPC_TIMEDOUT:
		break;
	default:
		clnt_perror(client, "yppush: Cannot call YPPROC_XFR");
		kill(pid, SIGTERM);
		break;
	}
}

void
push(inlen, indata)
int inlen;
char *indata;
{
	char host[MAXHOSTNAMELEN];
	CLIENT *client;
	SVCXPRT *transp;
	int sock = RPC_ANYSOCK;
	u_int prog;
	bool_t sts = 0;
	pid_t pid;
	int status;
	struct rusage res;

	snprintf(host, sizeof host, "%*.*s" ,inlen ,inlen, indata);

	client = clnt_create(host, YPPROG, YPVERS, "tcp");
	if (client == NULL) {
		if (Verbose)
			fprintf(stderr, "Target Host: %s\n",host);
		clnt_pcreateerror("yppush: Cannot create client");
		return;
	}

	transp = svcudp_create(sock);
	if (transp == NULL) {
		fprintf(stderr, "yppush: Cannot create callback transport.\n");
		return;
	}
	if (transp->xp_port >= IPPORT_RESERVED) {
		SVC_DESTROY(transp);
		fprintf(stderr, "yppush: Cannot allocate reserved port.\n");
		return;
	}

	for (prog=0x40000000; prog<0x5fffffff; prog++) {
		if ((sts = svc_register(transp, prog, 1,
		    yppush_xfrrespprog_1, IPPROTO_UDP)))
			break;
	}

	if (!sts) {
		fprintf(stderr, "yppush: Cannot register callback.\n");
		return;
	}

	switch(pid=fork()) {
	case -1:
		fprintf(stderr, "yppush: Cannot fork.\n");
		exit(1);
	case 0:
		_svc_run();
		exit(0);
	default:
		close(transp->xp_sock);
		transp->xp_sock = -1;
		req_xfr(pid, prog, transp, host, client);
		wait4(pid, &status, 0, &res);
		svc_unregister(prog, 1);
		if (client != NULL)
			clnt_destroy(client);
		/* XXX transp leak? */
	}

}

int
pushit(instatus, inkey, inkeylen, inval, invallen, indata)
int instatus;
char *inkey;
int inkeylen;
char *inval;
int invallen;
char *indata;
{
	if (instatus != YP_TRUE)
		return instatus;
	push(invallen, inval);
	return 0;
}

int
main(argc, argv)
int  argc;
char **argv;
{
	struct ypall_callback ypcb;
	extern char *optarg;
	extern int optind;
	char	*domain, *map, *hostname, *parallel, *timeout;
	int c, r, i;
	char *ypmap = "ypservers";
	CLIENT *client;
	static char map_path[MAXPATHLEN];
	struct stat finfo;
	DBM *yp_databas;
	char order_key[YP_LAST_LEN] = YP_LAST_KEY;
	datum o;

	yp_get_default_domain(&domain);
	hostname = NULL;
	while ((c=getopt(argc, argv, "d:h:v")) != -1)
		switch(c) {
		case 'd':
			domain = optarg;
			break;
		case 'h':
			hostname = optarg;
			break;
		case 'p':
			parallel = optarg;
			break;
		case 't':
			timeout = optarg;
			break;
		case 'v':
			Verbose = 1;
			break;
		default:
			usage();
			/*NOTREACHED*/
		}

	if (optind + 1 != argc )
		usage();

	map = argv[optind];

	strncpy(Domain, domain, sizeof(Domain)-1);
	Domain[sizeof(Domain)-1] = '\0';
	strncpy(Map, map, sizeof(Map)-1);
	Map[sizeof(Map)-1] = '\0';

	/* Check domain */
	snprintf(map_path, sizeof map_path, "%s/%s", YP_DB_PATH, domain);
	if (!((stat(map_path, &finfo) == 0) &&
	      ((finfo.st_mode & S_IFMT) == S_IFDIR))) {
	  	fprintf(stderr, "yppush: Map does not exist.\n");
		exit(1);
	}
		
	
	/* Check map */
	snprintf(map_path, sizeof map_path, "%s/%s/%s%s",
	    YP_DB_PATH, domain, Map, YPDB_SUFFIX);
	if (!(stat(map_path, &finfo) == 0)) {
		fprintf(stderr, "yppush: Map does not exist.\n");
		exit(1);
	}
		
	snprintf(map_path, sizeof map_path, "%s/%s/%s",
	    YP_DB_PATH, domain, Map);
	yp_databas = ypdb_open(map_path, 0, O_RDONLY);
	OrderNum=0xffffffff;
	if (yp_databas == 0) {
		fprintf(stderr, "yppush: %s%s: Cannot open database\n",
		    map_path, YPDB_SUFFIX);
	} else {
		o.dptr = (char *) &order_key;
		o.dsize = YP_LAST_LEN;
		o = ypdb_fetch(yp_databas, o);
		if (o.dptr == NULL) {
			fprintf(stderr,
			    "yppush: %s: Cannot determine order number\n",
			    Map);
		} else {
			OrderNum=0;
			for (i=0; i<o.dsize-1; i++) {
				if (!isdigit(o.dptr[i]))
					OrderNum=0xffffffff;
			}
			if (OrderNum != 0) {
				fprintf(stderr,
				    "yppush: %s: Invalid order number '%s'\n",
				    Map, o.dptr);
			} else {
				OrderNum = atoi(o.dptr);
			}
		}
	}
	

	yp_bind(Domain);

	r = yp_master(Domain, ypmap, &master);
	if (r != 0) {
		fprintf(stderr, "yppush: could not get ypservers map\n");
		exit(1);
	}

	if (hostname != NULL) {
		push(strlen(hostname), hostname);
	} else {
		if (Verbose) {
			printf("Contacting master for ypservers (%s).\n",
			    master);
		}

		client = yp_bind_host(master, YPPROG, YPVERS, 0, 1);

		ypcb.foreach = pushit;
		ypcb.data = NULL;
		r = yp_all_host(client, Domain, ypmap, &ypcb);
	}

	exit(0);
}