summaryrefslogtreecommitdiffstats
path: root/gnu/usr.bin/yppush/yppush.c
blob: 37410cfad0575bb2cc12c1e895420b38a31322d6 (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
/*
    YPS-0.2, NIS-Server for Linux
    Copyright (C) 1994  Tobias Reber

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

    Modified for use with FreeBSD 2.x by Bill Paul (wpaul@ctr.columbia.edu)

	$Id: yppush.c,v 1.5 1995/04/02 20:01:50 wpaul Exp $
*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <rpc/rpc.h>
#include <paths.h>
#include <rpcsvc/yp.h>
/*
 * ypclnt.h does not have a definition for struct dom_binding,
 * although it is used there. It is defined in yp_prot.h, but
 * we cannot use it here.
 */
struct dom_binding {
	void * m;
};
#include <rpcsvc/ypclnt.h>
#include <sys/param.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <ctype.h>
#include <netdb.h>
#include <errno.h>
#include <sys/types.h>
#include <fcntl.h>
#include <db.h>
#include <limits.h>
#include <sys/stat.h>

#ifndef _PATH_YP
#define _PATH_YP "/var/yp/"
#endif

char *progname;

#define PERM_SECURE (S_IRUSR|S_IWUSR)
HASHINFO openinfo = {
	4096,		/* bsize */
	32,		/* ffactor */
	256,		/* nelem */
	2048 * 1024,	/* cachesize */
	NULL,		/* hash */
	0		/* lorder */
};

#undef MIN
#define MIN(a,b) ((a)<(b)?(a):(b))

static char *DomainName=NULL;
static bool_t Verbose=FALSE;

static char *ThisHost=NULL;

static char *MapName;
static u_int MapOrderNum;

static CLIENT *PushClient=NULL;

static u_int CallbackTransid;
static u_int CallbackProg=0;
static SVCXPRT *CallbackXprt;

static inline char *
yppush_err_string(enum yppush_status y) {
	switch(y) {
	case YPPUSH_SUCC:	return "Success";
	case YPPUSH_AGE:	return "Master's version not newer";
	case YPPUSH_NOMAP:	return "Can't find server for map";
	case YPPUSH_NODOM:	return "Domain not supported";
	case YPPUSH_RSRC:	return "Local resource alloc failure";
	case YPPUSH_RPC:	return "RPC failure talking to server";
	case YPPUSH_MADDR:	return "Can't get master address";
	case YPPUSH_YPERR:	return "YP server/map db error";
	case YPPUSH_BADARGS:	return "Request arguments bad";
	case YPPUSH_DBM:	return "Local dbm operation failed";
	case YPPUSH_FILE:	return "Local file I/O operation failed";
	case YPPUSH_SKEW:	return "Map version skew during transfer";
	case YPPUSH_CLEAR:	return "Can't send \"Clear\" req to local ypserv";
	case YPPUSH_FORCE:	return "No local order number in map  use -f flag.";
	case YPPUSH_XFRERR:	return "ypxfr error";
	case YPPUSH_REFUSED:	return "Transfer request refused by ypserv";
	}
}

#define YPPUSH_XFRRESPPROG ((u_long)0x40000000)
#define YPPUSH_XFRRESPVERS ((u_long)1)

#define YPPUSHPROC_NULL ((u_long)0)
static inline void *
__yppushproc_null_1(void * req, struct svc_req * rqstp) {
	static int resp;
	return &resp;
}

#define YPPUSHPROC_XFRRESP ((u_long)1)
static inline void *
__yppushproc_xfrresp_1(yppushresp_xfr *req, struct svc_req * rqstp) {
	static int resp;

	if (req->status!=YPPUSH_SUCC)
		fprintf(stderr, "%s: %s\n", progname, yppush_err_string(req->status));
	return &resp;
}

void
__yppush_xfrrespprog_1( struct svc_req *rqstp, SVCXPRT *transp)
{
	union {
		int fill;
	} argument;
	char *result;
	bool_t (*xdr_argument)(XDR *, void *), (*xdr_result)(XDR *, void *);
	char *(*local)( void *, struct svc_req *);

	switch (rqstp->rq_proc) {
	case YPPUSHPROC_NULL:
		xdr_argument = (bool_t (*)(XDR *, void *))xdr_void;
		xdr_result = (bool_t (*)(XDR *, void *))xdr_void;
		local = (char *(*)( void *, struct svc_req *)) __yppushproc_null_1;
		break;

	case YPPUSHPROC_XFRRESP:
		xdr_argument = xdr_yppushresp_xfr;
		xdr_result = (bool_t (*)(XDR *, void *))xdr_void;
		local = (char *(*)( void *, struct svc_req *)) __yppushproc_xfrresp_1;
		break;

	default:
		svcerr_noproc(transp);
		exit(1);
	}
	bzero((char *)&argument, sizeof(argument));
	if (!svc_getargs(transp, xdr_argument, &argument)) {
		svcerr_decode(transp);
		exit(1);
	}
	result = (*local)(&argument, rqstp);
	if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
		svcerr_systemerr(transp);
	}
	if (!svc_freeargs(transp, xdr_argument, &argument)) {
		(void)fprintf(stderr, "%s: unable to free arguments\n", progname);
		exit(1);
	if (rqstp->rq_proc!=YPPUSHPROC_NULL)
		exit(0);
	}
	exit(0);
}

/*
 * This is the rpc server side idle loop
 * Wait for input, call server program.
 */

static void
_svc_run( void)
{
#ifdef FD_SETSIZE
	fd_set readfds;
#else
        int readfds;
#endif /* def FD_SETSIZE */
	extern int errno;
	struct timeval t;

	t.tv_sec=60; t.tv_usec=0;

	for (;;) {
#ifdef FD_SETSIZE
		readfds = svc_fdset;
#else
		readfds = svc_fds;
#endif /* def FD_SETSIZE */
		switch (select(_rpc_dtablesize(), &readfds, (void *)0,
			(void *)0, &t)) {
		case -1:
			if (errno == EINTR) {
				continue;
			}
			perror("svc_run: - select failed");
			return;
		case 0:
			fprintf(stderr, "%sh: callback timed out\n, progname");
			exit(0);
		default:
			svc_getreqset(&readfds);
		}
	}
}

static void
Usage(void)
{
	fprintf(stderr, "Usage: %s [ -d domain ] [ -v ] mapname ...\n",
								progname);
	exit(1);
}

static char *
getHostName( void)
{
	static char hostname[MAXHOSTNAMELEN+1];
	struct hostent *h;
	if (gethostname(hostname, sizeof hostname)!=0) {
		fprintf(stderr,"%s:", progname);
		perror("gethostname");
		return NULL;
	}
	h=gethostbyname(hostname);
	if (h!=NULL) {
		strncpy(hostname, h->h_name, sizeof (hostname)-1);
		hostname[sizeof (hostname)-1]='\0';
	}
	return hostname;
}

static u_int
getOrderNum( void)
{
	char mapPath[MAXPATHLEN];
	DB *db;
	DBT o,d;
	int i;

	strcpy(mapPath, _PATH_YP);
	strcat(mapPath, DomainName);
	strcat(mapPath, "/");
	strcat(mapPath, MapName);

	if ((db = dbopen(mapPath, O_RDWR|O_EXCL, PERM_SECURE, DB_HASH,
		&openinfo)) == NULL) {
		fprintf(stderr, "%s: %s: cannot open\n", progname, mapPath);
		return -1;
	}

	o.data="YP_LAST_MODIFIED"; o.size=strlen(o.data);
	(db->get)(db,&o,&d,0);
	if (d.data==NULL) {
		fprintf(stderr, "%s: %s: cannot determine order number\n",
			progname, MapName);
		return -1;
	}

	for (i=0; i<d.size; i++) {
		if (!isdigit(*((char *)d.data+i))) {
			fprintf(stderr, "%s: %s: Invalid order number '%s'\n",
				progname, MapName, d.data);
			return -1;
		}
	}
	(void)(db->close)(db);
	return atoi(d.data);
}

static void
doPushClient( const char *targetHost)
{
	struct ypreq_xfr req;
	static struct timeval tv={0,0};

	req.map_parms.domain=DomainName;
	req.map_parms.map=(char *)MapName;
	req.map_parms.peer=ThisHost;
	req.map_parms.ordernum=MapOrderNum;
	req.transid=CallbackTransid;
	req.prog=CallbackProg;
	req.port=CallbackXprt->xp_port;

	if (Verbose)
		printf("%d: %s(%d@%s) -> %s@%s\n", req.transid,
			req.map_parms.map, req.map_parms.ordernum,
			req.map_parms.peer, targetHost,
			req.map_parms.domain);
	switch (clnt_call(PushClient, YPPROC_XFR, xdr_ypreq_xfr, &req,
		xdr_void, NULL, tv)) {
	case RPC_SUCCESS:
	case RPC_TIMEDOUT:
		break;
	default:
		clnt_perror(PushClient, "yppush: cannot call YPPROC_XFR");
		kill(CallbackTransid, SIGTERM);
	}
	return;
}

static bool_t
registerServer(void)
{
	int s;
	s=RPC_ANYSOCK;
	CallbackXprt=svcudp_create(s);
	if (CallbackXprt==NULL) {
		fprintf(stderr, "%s: Cannot create callback transport.\n",
								progname);
		return FALSE;
	}
	for (CallbackProg=0x40000000; CallbackProg<0x5fffffff; CallbackProg++) {
		if (svc_register(CallbackXprt, CallbackProg, 1,
			__yppush_xfrrespprog_1, IPPROTO_UDP))
			return TRUE;
	}
	return FALSE;
}

static bool_t
createClient(const char *targetHost)
{
	PushClient=clnt_create((char *)targetHost, YPPROG, YPVERS, "tcp");
	if (PushClient==NULL) {
		clnt_pcreateerror("yppush: cannot create client");
		return FALSE;
	}
	return TRUE;
}

static void
doPush( const char *targetHost)
{
	int s;
	struct rusage r;

	if (!createClient(targetHost))
		return;
	if (!registerServer())
		return;

	switch (CallbackTransid=fork()) {
	case -1:
		fprintf(stderr, "%s:", progname);
		perror("fork");
		exit(1);
	case 0:
		_svc_run();
		exit(0);
	default:
		close(CallbackXprt->xp_sock);
		doPushClient(targetHost);
		wait4(CallbackTransid, &s, 0, &r);
		svc_unregister(CallbackProg, 1);
		CallbackProg=0;
		if (PushClient!=NULL) {
			clnt_destroy(PushClient);
			PushClient=NULL;
		}
	}
}

static int
yppushForeach(int status, const char *inKey, int inKeylen,
	const char *inVal, int inVallen, const char *data)
{
	char targetHost[YPMAXPEER+1];

	if (status!=YP_TRUE) return 0;
	memcpy(targetHost, inKey, MIN(sizeof (targetHost)-1, inKeylen));
	targetHost[MIN(sizeof (targetHost)-1, inKeylen)]='\0';
	doPush(targetHost);
	return 0;
}

static void
intrHandler(int sig)
{
	if (CallbackProg!=0)
		svc_unregister(CallbackProg, 1);
	exit(1);
}

int
main(int argc, char **argv)
{
	char c;
	struct ypall_callback f;
	enum ypstat y;
	struct sigaction a;

	progname = argv[0];

	a.sa_handler=intrHandler;
	a.sa_mask=0;
	sigaction(SIGINT, &a, NULL);

	while((c=getopt(argc, argv, "d:v"))!=EOF) {
		switch(c) {
		case 'd':
			DomainName=optarg;
			break;
		case 'v':
			Verbose=TRUE;
			break;
		default:
			Usage();
		}
	}
	argc-=optind;
	argv+=optind;
	if (argc<1) Usage();

	if (DomainName==NULL) {
		if (yp_get_default_domain(&DomainName)!=0) {
			fprintf(stderr, "%s: cannot get default domain\n,
								progname");
			exit(1);
		}
	}

	ThisHost=getHostName();
	if (ThisHost==NULL) {
		fprintf(stderr, "%s: cannot determine local hostname\n,
								progname");
		exit(1);
	}

	while (*argv) {
		MapName=*argv++;
		MapOrderNum=getOrderNum();
		if (MapOrderNum==0xffffffff)
			continue;
		f.foreach=yppushForeach;
		y=yp_all(DomainName, "ypservers", &f);
		if (y && y!=YP_NOMORE) {
			fprintf(stderr, "%s: could not read ypservers map: %d %s\n",
				progname, y, yperr_string(y));
			exit(1);
		}
	}
	exit(0);
}
OpenPOWER on IntegriCloud