summaryrefslogtreecommitdiff
path: root/usr.sbin/ypserv/ypxfr/ypxfr.c
blob: 6962080acb86929775fbc3015cf3f85b430e80ae (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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
/*	$OpenBSD: ypxfr.c,v 1.26 2001/11/07 19:01:29 deraadt Exp $ */

/*
 * Copyright (c) 1994 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: ypxfr.c,v 1.26 2001/11/07 19:01:29 deraadt Exp $";
#endif

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>

#include <netinet/in.h>
#include <arpa/inet.h>

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <netdb.h>

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

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

extern char *__progname;		/* from crt0.o */
DBM	*db;

extern bool_t xdr_ypresp_all_seq();

extern int (*ypresp_allfn)();
extern void *ypresp_data;

static int
ypxfr_foreach(status, keystr, keylen, valstr, vallen, data)
int status, keylen, vallen;
char *keystr, *valstr, *data;
{
	datum	key, val;

	if (status == YP_NOMORE)
		return(0);

	keystr[keylen] = '\0';
	valstr[vallen] = '\0';

	key.dptr = keystr;
	key.dsize = strlen(keystr);

	val.dptr = valstr;
	val.dsize = strlen(valstr);

	ypdb_store(db, key, val, YPDB_INSERT);

	return 0;
}

int
get_local_ordernum(domain, map, lordernum)
char *domain;
char *map;
u_int32_t *lordernum;
{
	char map_path[MAXPATHLEN];
	char order_key[] = YP_LAST_KEY;
	char order[MAX_LAST_LEN+1];
	struct stat finfo;
	DBM *db;
	datum k, v;
	int status;

	/* This routine returns YPPUSH_SUCC or YPPUSH_NODOM */

	status = YPPUSH_SUCC;

	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, "%s: domain %s not found locally\n",
		    __progname, domain);
		status = YPPUSH_NODOM;
	}

	if (status > 0) {
		snprintf(map_path, sizeof map_path, "%s/%s/%s%s",
		    YP_DB_PATH, domain, map, YPDB_SUFFIX);
		if (!(stat(map_path, &finfo) == 0))
			status = YPPUSH_NOMAP;
	}

	if (status > 0) {
		snprintf(map_path, sizeof map_path, "%s/%s/%s",
		    YP_DB_PATH, domain, map);
		db = ypdb_open(map_path, O_RDONLY, 0444);
		if (db == NULL)
			status = YPPUSH_DBM;
	}

	if (status > 0) {
		k.dptr = (char *)&order_key;
		k.dsize = YP_LAST_LEN;

		v = ypdb_fetch(db,k);
		ypdb_close(db);

		if (v.dptr == NULL) {
			*lordernum = 0;
		} else {
	        	strlcpy(order, v.dptr, sizeof order);
			*lordernum = (u_int32_t)atol(order);
		}
	}

	if (status == YPPUSH_NOMAP || status == YPPUSH_DBM) {
		*lordernum = 0;
		status = YPPUSH_SUCC;
	}
	return(status);

}

int
get_remote_ordernum(client, domain, map, lordernum, rordernum)
CLIENT *client;
char *domain;
char *map;
u_int32_t lordernum;
u_int32_t *rordernum;
{
	int status;

	status = yp_order_host(client, domain, map, rordernum);

	if (status == 0) {
		if (*rordernum <= lordernum)
			status = YPPUSH_AGE;
		else
			status = YPPUSH_SUCC;
	}
	return status;
}

int
get_map(client,domain,map,incallback)
CLIENT *client;
char *domain;
char *map;
struct ypall_callback *incallback;
{
	int	status;

	status = yp_all_host(client, domain, map, incallback);
	if ((status == 0) || (status == YPERR_NOMORE)) {
		status = YPPUSH_SUCC;
	} else {
		status = YPPUSH_YPERR;
	}

	return(status);
}

DBM *
create_db(domain,map,temp_map)
char *domain;
char *map;
char *temp_map;
{
	return ypdb_open_suf(temp_map, O_RDWR, 0444);
}

int
install_db(domain,map,temp_map)
char *domain;
char *map;
char *temp_map;
{
	char	db_name[MAXPATHLEN];

	snprintf(db_name, sizeof db_name, "%s/%s/%s%s",
	    YP_DB_PATH, domain, map, YPDB_SUFFIX);
	rename(temp_map, db_name);

	return YPPUSH_SUCC;
}

int
add_order(db, ordernum)
DBM *db;
u_int32_t ordernum;
{
	char	datestr[11];
	datum	key, val;
	char	keystr[] = YP_LAST_KEY;
	int	status;

	snprintf(datestr, sizeof datestr, "%010u", ordernum);

	key.dptr = keystr;
	key.dsize = strlen(keystr);

	val.dptr = datestr;
	val.dsize = strlen(datestr);

	status = ypdb_store(db, key, val, YPDB_INSERT);
	if (status >= 0) {
		status = YPPUSH_SUCC;
	} else {
		status = YPPUSH_DBM;
	}
	return(status);
}

int
add_master(client, domain, map, db)
CLIENT *client;
char *domain;
char *map;
DBM *db;
{
	char	keystr[] = YP_MASTER_KEY;
	char	*master;
	int	status;
	datum	key, val;

	master = NULL;

	/* Get MASTER */

	status = yp_master_host(client, domain, map, &master);

	if (master != NULL) {
		key.dptr = keystr;
		key.dsize = strlen(keystr);

		val.dptr = master;
		val.dsize = strlen(master);

		status = ypdb_store(db, key, val, YPDB_INSERT);
		if (status >= 0) {
			status = YPPUSH_SUCC;
		} else {
			status = YPPUSH_DBM;
		}
	}
	return status;
}

int
add_interdomain(client, domain, map, db)
CLIENT *client;
char *domain;
char *map;
DBM *db;
{
	char	keystr[] = YP_INTERDOMAIN_KEY;
	char	*value;
	int	vallen;
	int	status;
	datum	k, v;

	/* Get INTERDOMAIN */

	k.dptr = keystr;
	k.dsize = strlen(keystr);

	status = yp_match_host(client, domain, map,
	    k.dptr, k.dsize, &value, &vallen);
	if (status == 0 && value) {
		v.dptr = value;
		v.dsize = vallen;

		if (v.dptr != NULL) {
			status = ypdb_store(db, k, v, YPDB_INSERT);
			if (status >= 0) {
				status = YPPUSH_SUCC;
			} else {
				status = YPPUSH_DBM;
			}
		}
	}

	return 1;
}

int
add_secure(client, domain, map, db)
CLIENT *client;
char *domain;
char *map;
DBM *db;
{
	char	keystr[] = YP_SECURE_KEY;
	char	*value;
	int	vallen;
	int	status;
	datum	k, v;

	/* Get SECURE */

	k.dptr = keystr;
	k.dsize = strlen(keystr);

	status = yp_match_host(client, domain, map,
	    k.dptr, k.dsize, &value, &vallen);
	if (status == 0 && value) {
		v.dptr = value;
		v.dsize = vallen;

		if (v.dptr != NULL) {
			status = ypdb_store(db, k, v, YPDB_INSERT);
			if (status >= 0) {
				status = YPPUSH_SUCC;
			} else {
				status = YPPUSH_DBM;
			}
		}
	}

	return status;

}

int
send_clear(client)
CLIENT *client;
{
	struct	timeval tv;
	int	r;
	int	status;

	status = YPPUSH_SUCC;

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

	/* Send CLEAR */
	r = clnt_call(client, YPPROC_CLEAR, xdr_void, 0, xdr_void, 0, tv);
	if (r != RPC_SUCCESS)
		clnt_perror(client, "yp_clear: clnt_call");
	return status;

}

int
send_reply(client,status,tid)
CLIENT *client;
u_long	status;
u_long  tid;
{
	struct	timeval tv;
	struct	ypresp_xfr resp;
	int	r;

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

	resp.transid = tid;
	resp.xfrstat = status;

	/* Send CLEAR */
	r = clnt_call(client, 1, xdr_ypresp_xfr, &resp, xdr_void, 0, tv);
	if (r != RPC_SUCCESS)
		clnt_perror(client, "yppushresp_xdr: clnt_call");
	return status;

}

int
main (argc,argv)
int argc;
char *argv[];
{
	int	 usage = 0;
	int	 cflag = 0;
	int	 fflag = 0;
	int	 Cflag = 0;
	int	 ch;
	extern	 char *optarg;
	char	 *domain;
	char	 *host = NULL;
	char	 *srcdomain = NULL;
	char	 *tid = NULL;
	char	 *prog = NULL;
	char	 *ipadd = NULL;
	char	 *port = NULL;
	char	 *map = NULL;
	u_int32_t ordernum, new_ordernum;
	struct	 ypall_callback callback;
	CLIENT   *client;
	int	 status, xfr_status;
	int	 srvport;

	status = YPPUSH_SUCC;
	client = NULL;

	yp_get_default_domain(&domain);

	while ((ch = getopt(argc, argv, "cd:fh:s:C:")) != -1)
		switch (ch) {
		case 'c':
			cflag++;
			break;
		case 'd':
			if (strchr(optarg, '/')) /* Ha ha, we are not listening */
				break;
			domain = optarg;
			break;
		case 'f':
			fflag++;
			break;
		case 'h':
			host = optarg;
			break;
		case 's':
			if (strchr(optarg, '/')) /* Ha ha, we are not listening */
				break;
			srcdomain = optarg;
			break;
		case 'C':
			if (optind + 3 >= argc) {
				usage++;
				optind = argc;
				break;
			}
			Cflag++;
			tid = optarg;
			prog = argv[optind++];
			ipadd = argv[optind++];
			port = argv[optind++];
			break;
		default:
			usage++;
			break;
		}

	if (optind + 1 != argc) {
		usage++;
	} else {
		map = argv[optind];
	}

	if (usage) {
		status = YPPUSH_BADARGS;
		fprintf(stderr, "usage: %s %s %s\n",
		    "[-cf] [-d domain] [-h host] [-s domain]",
		    "[-C tid prog ipadd port] mapname\n",
		    __progname);
	}

	if (status > 0) {
		ypopenlog();

		yplog("ypxfr: Arguments:");
		yplog("YP clear to local: %s", (cflag) ? "no" : "yes");
		yplog("   Force transfer: %s", (fflag) ? "yes" : "no");
		yplog("           domain: %s", domain);
		yplog("             host: %s", host);
		yplog("    source domain: %s", srcdomain);
		yplog("          transid: %s", tid);
		yplog("             prog: %s", prog);
		yplog("             port: %s", port);
		yplog("            ipadd: %s", ipadd);
		yplog("              map: %s", map);

		if (fflag != 0) {
			ordernum = 0;
		} else {
			status = get_local_ordernum(domain, map, &ordernum);
		}
	}

	if (status > 0) {
	        yplog("Get Master");

		if (host == NULL) {
			if (srcdomain == NULL) {
				status = yp_master(domain, map, &host);
		        } else {
				status = yp_master(srcdomain, map, &host);
			}
			if (status == 0) {
				status = YPPUSH_SUCC;
			} else {
				status = -status;
			}
		}
	}

	/* XXX this is raceable if portmap has holes! */
	if (status > 0) {
	        yplog("Check for reserved port on host: %s", host);

		srvport = getrpcport(host, YPPROG, YPVERS, IPPROTO_TCP);
		if (srvport >= IPPORT_RESERVED)
			status = YPPUSH_REFUSED;
	}

	if (status > 0) {
	        yplog("Connect host: %s", host);

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

		status = get_remote_ordernum(client, domain, map,
		    ordernum, &new_ordernum);
	}

	if (status == YPPUSH_SUCC) {
		char	tmpmapname[MAXPATHLEN];
		int	fd;

		/* Create temporary db */
		snprintf(tmpmapname, sizeof tmpmapname,
		    "%s/%s/ypdbXXXXXXXXXX", YP_DB_PATH, domain);
		fd = mkstemp(tmpmapname);
		if (fd == -1)
			status = YPPUSH_DBM;
		else
			close(fd);

		if (status > 0) {
			db = create_db(domain, map, tmpmapname);
			if (db == NULL)
				status = YPPUSH_DBM;
		}

	  	/* Add ORDER */
		if (status > 0)
			status = add_order(db, new_ordernum);

		/* Add MASTER */
		if (status > 0)
			status = add_master(client, domain, map, db);

	        /* Add INTERDOMAIN */
		if (status > 0)
			status = add_interdomain(client, domain, map, db);

	        /* Add SECURE */
		if (status > 0)
			status = add_secure(client, domain, map, db);

		if (status > 0) {
			callback.foreach = ypxfr_foreach;
			status = get_map(client, domain, map, &callback);
		}

		/* Close db */
		if (db != NULL)
			ypdb_close(db);

		/* Rename db */
		if (status > 0) {
			status = install_db(domain, map, tmpmapname);
		} else {
			unlink(tmpmapname);
			status = YPPUSH_SUCC;
		}
	}

	xfr_status = status;

	if (client != NULL)
		clnt_destroy(client);

	/* YP_CLEAR */

	if (!cflag) {
		client = yp_bind_local(YPPROG, YPVERS);
		status = send_clear(client);
		clnt_destroy(client);
	}

	if (Cflag > 0) {
		/* Send Response */
		client = yp_bind_host(ipadd, atoi(prog), 1, atoi(port), 0);
		status = send_reply(client, xfr_status, atoi(tid));
		clnt_destroy(client);
	}

	return(0);

}