Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
/*	$NetBSD: ypwhich.c,v 1.21 2017/08/11 20:32:34 ginsbach Exp $	*/

/*
 * Copyright (c) 1997 Charles D. Cranor
 * 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.
 *
 * 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.
 */

/*
 * ypwhich
 * author: Chuck Cranor <chuck@netbsd>
 * date: 31-Oct-97
 *
 * notes: this is a full rewrite of Theo de Raadt's ypwhich.
 * this version allows you full control of which ypserv you
 * talk to for the "-m" command.
 */

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

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

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

#include <rpc/rpc.h>
#include <rpcsvc/yp_prot.h>
#include <rpcsvc/ypclnt.h>

#include "ypalias_init.h"

/*
 * ypwhich: query a host about its yp service
 *
 * usage:
 *   ypwhich [-d domain] [[-h] host]
 *	(who is host's ypserv?)
 *   ypwhich [-h host] [-d domain] [-f] [-t] -m [mapname]
 *	(who is the master of a map?)
 *   ypwhich -x
 *	(what nicknames do you use?)
 *
 *   -d: the domainname to ask about
 *   -f: for -m, force us to talk directly to ypserv on the specified host
 *       without going through ypbind.
 *   -h: specify a host to ask [default = localhost]
 *   -m: find master server for a specific map (no map means 'all maps')
 *   -t: inhibit nickname translation
 *   -T: use TCP instead of UDP
 *   -x: print list of yp map aliases and exit
 */

/*
 * prototypes
 */

static void find_mapmaster(const char *, const char *, const char *,
    int, int, int, const struct ypalias *);
static struct in_addr *find_server(const char *, const char *, int);
static CLIENT *mkclient(struct sockaddr_in *, unsigned long, unsigned long,
    int);
static void usage(void) __attribute__((__noreturn__));

/*
 * main
 */
int
main(int argc, char *argv[])

{
	const char   *targhost = "localhost";
	char   *ourdomain;
	int     inhibit = 0, force = 0, tcp = 0;
	char   *targmap = NULL;
	int     ch, saw_m;
	struct in_addr *inaddr;
	struct hostent *he;
	size_t i;
	const struct ypalias *ypaliases;

	/*
         * get default domainname and parse options
         */

	ypaliases = ypalias_init();
	(void)yp_get_default_domain(&ourdomain);
	saw_m = 0;
	while ((ch = getopt(argc, argv, "h:d:xtTfm")) != -1) {
		switch (ch) {
		case 'h':
			targhost = optarg;
			break;
		case 'd':
			ourdomain = optarg;
			break;
		case 'x':
			for (i = 0; ypaliases[i].alias; i++)
				(void)printf("Use \"%s\" for map \"%s\"\n",
				    ypaliases[i].alias, ypaliases[i].name);
			return 0;
		case 'f':
			force = 1;
			break;
		case 't':
			inhibit = 1;
			break;
		case 'T':
			tcp = 1;
			break;
		case 'm':
			if (optind < argc && argv[optind][0] != '-')
				targmap = argv[optind++];
			saw_m = 1;
			break;
		case '?':
		default:
			usage();
		}
	}
	argc -= optind;
	argv += optind;
	if (argc) {
		if (argc > 1)
			usage();
		targhost = argv[0];
	}
#ifdef DEBUG
	(void)printf("target_host=%s, domain=%s, inhibit=%d, saw_m=%d, map=%s, "
	    "force=%d, tcp=%d\n",
	    targhost, ourdomain, inhibit, saw_m, targmap, force, tcp);
#endif

	/*
         * need a valid domain
         */

	if (ourdomain == NULL)
		errx(1, "the domain hasn't been set on this machine.");

	/*
         * now do it
         */
	if (saw_m)
		find_mapmaster(targhost, ourdomain, targmap, inhibit, force,
		    tcp, ypaliases);
	else {
		inaddr = find_server(targhost, ourdomain, tcp);
		he = gethostbyaddr((void *)&inaddr->s_addr,
		    sizeof(inaddr->s_addr), AF_INET);
		if (he)
			(void)printf("%s\n", he->h_name);
		else
			(void)printf("%s\n", inet_ntoa(*inaddr));
	}
	return 0;
}

/*
 * usage: print usage and exit
 */
static void
usage(void)
{
	const char *pname = getprogname();
	(void)fprintf(stderr, "Usage:\t%s [-T] [-d domain] [[-h] host]\n"
	    "\t%s [-fTt] [-d domain] [-h host] -m [mapname]\n"
	    "\t%s [-T] -x\n", pname, pname, pname);
	exit(1);
}

static CLIENT *
mkclient(struct sockaddr_in *sin, unsigned long prog, unsigned long vers,
    int tcp)
{
	static struct timeval tv = { 15, 0 };
	int fd = RPC_ANYSOCK;

	if (tcp)
		return clnttcp_create(sin, prog, vers, &fd, 0, 0);
	else
		return clntudp_create(sin, prog, vers, tv, &fd);
}

/*
 * find_server: ask a host's ypbind who its current ypserver is
 */
static struct in_addr *
find_server(const char *host, const char *domain, int tcp)
{
	static struct in_addr result;
	struct sockaddr_in sin;
	CLIENT *ypbind;
	struct timeval tv;
	enum clnt_stat retval;
	struct ypbind_resp ypbind_resp;

	/*
         * get address of host
         */
	(void)memset(&sin, 0, sizeof(sin));
	sin.sin_family = AF_INET;
	if (inet_aton(host, &sin.sin_addr) == 0) {
		struct hostent *he;

		he = gethostbyname(host);
		if (he == NULL)
			errx(1, "%s: %s", host, hstrerror(h_errno));
		(void)memmove(&sin.sin_addr, he->h_addr, sizeof(sin.sin_addr));
	}

	/*
         * establish connection to ypbind
         */
	ypbind = mkclient(&sin, YPBINDPROG, YPBINDVERS, tcp);
	if (ypbind == NULL)
		errx(1, "clnt%s_create: %s: %s", tcp ? "tcp" : "udp", host,
		    yperr_string(YPERR_YPBIND));

	/*
         * now call ypbind's "DOMAIN" procedure to get the server name
         */
	tv.tv_sec = 5;
	tv.tv_usec = 0;
	retval = clnt_call(ypbind, (unsigned int)YPBINDPROC_DOMAIN,
	    xdr_ypdomain_wrap_string, &domain, xdr_ypbind_resp, &ypbind_resp,
	    tv);
	clnt_destroy(ypbind);
	if (retval != RPC_SUCCESS)
		errx(1, "clnt_call: %s: %s", host, clnt_sperrno(retval));
	if (ypbind_resp.ypbind_status != YPBIND_SUCC_VAL)
		errx(1, "ypbind on %s for domain %s failed: %s", host, domain,
		    yperr_string(ypbind_resp.ypbind_status));

	/*
         * got it!
         */
	result.s_addr = ypbind_resp.ypbind_respbody.
	    ypbind_bindinfo.ypbind_binding_addr.s_addr;	/* love that name! */
	return (&result);
}

/*
 * find_mapmaster: ask a host's ypserver who its map's master is
 */
static void
find_mapmaster(const char *host, const char *domain, const char *map,
    int inhibit, int force, int tcp, const struct ypalias *ypaliases)
{
	struct in_addr *inaddr, faddr;
	struct hostent *he;
	struct sockaddr_in sin;
	CLIENT *ypserv;
	int     yperr;
	enum clnt_stat retval;
	struct timeval tv;
	struct ypresp_maplist yprespmlist;
	struct ypmaplist fakelist, *ypml;
	struct ypresp_master yprespmaster;
	struct ypreq_nokey ypreqkey;
	size_t i;

	/*
         * we can either ask the hosts ypbind where its ypserv is located,
         * or we can be forced to assume that ypserv is running on the host.
         */
	if (force) {
		if (inet_aton(host, &faddr) == 0) {
			he = gethostbyname(host);
			if (he == NULL)
				errx(1, "%s: %s", host, hstrerror(h_errno));
			(void)memmove(&faddr, he->h_addr, sizeof(faddr));
		}
		inaddr = &faddr;
	} else {
		/* ask host "host" who is currently serving its maps  */
		inaddr = find_server(host, domain, tcp);
	}

	/*
         * now translate nicknames [unless inhibited]
         */
	if (map && !inhibit) {
		for (i = 0; ypaliases[i].alias; i++) {
			if (strcmp(map, ypaliases[i].alias) == 0) {
				map = ypaliases[i].name;
				break;
			}
		}
#ifdef DEBUG
		(void)printf("translated map name = %s\n", map);
#endif
	}

	/*
         * now we try and connect to host's ypserv
         */
	(void)memset(&sin, 0, sizeof(sin));
	sin.sin_family = AF_INET;
	sin.sin_addr.s_addr = inaddr->s_addr;
	ypserv = mkclient(&sin, YPPROG, YPVERS, tcp);
	if (ypserv == NULL) {
		warnx("clnt%s_create: %s: %s", tcp ? "tcp" : "udp", host,
		    yperr_string(YPERR_YPSERV));
		goto error;
	}

	/*
         * did the user specify a map?
         */
	if (map == NULL) {
		/*
	         * if no map specified, we ask ypserv for a list of all maps
	         */
		(void)memset(&yprespmlist, 0, sizeof(yprespmlist));
		tv.tv_sec = 5;
		tv.tv_usec = 0;
		retval = clnt_call(ypserv, (unsigned int)YPPROC_MAPLIST,
		    xdr_ypdomain_wrap_string, &domain, xdr_ypresp_maplist,
		    &yprespmlist, tv);
		if (retval != RPC_SUCCESS) {
			warnx("clnt_call MAPLIST: %s: %s", host,
			    clnt_sperrno(retval));
			goto error;
		}
		yperr = ypprot_err(yprespmlist.status);
		if (yperr) {
			warnx("clnt_call: %s: %s", host, yperr_string(yperr));
			goto error;
		}
		ypml = yprespmlist.list;
	} else {
		/*
	         * build a fake "list" of maps containing only the list the user
	         * asked about in it.
	         */
		(void)memset(&fakelist, 0, sizeof(fakelist));
		(void)strlcpy(fakelist.ypml_name, map, sizeof(fakelist.ypml_name));
		fakelist.ypml_next = NULL;
		ypml = &fakelist;
	}

	/*
         * we now have a list of maps.   ask ypserv who is the master for
         * each map...
         */
	for ( /* null */ ; ypml != NULL; ypml = ypml->ypml_next) {
		ypreqkey.domain = domain;
		ypreqkey.map = ypml->ypml_name;
		(void)memset(&yprespmaster, 0, sizeof(yprespmaster));
		tv.tv_sec = 5;
		tv.tv_usec = 0;
		retval = clnt_call(ypserv, (unsigned int)YPPROC_MASTER,
		    xdr_ypreq_nokey, &ypreqkey, xdr_ypresp_master,
		    &yprespmaster, tv);
		if (retval != RPC_SUCCESS) {
			warnx("clnt_call MASTER: %s: %s", host,
			    clnt_sperrno(retval));
			goto error;
		}
		yperr = ypprot_err(yprespmaster.status);
		if (yperr) {
			warnx("clnt_call: %s: %s: %s", host, ypml->ypml_name,
			    yperr_string(yperr));
		} else {
			(void)printf("%s %s\n", ypml->ypml_name,
			    yprespmaster.master);
		}
		xdr_free((xdrproc_t)xdr_ypresp_master, (void *)&yprespmaster);
	}
	clnt_destroy(ypserv);

	/*
         * done
         */
	return;

error:
	/* print host's ypserv's IP address to prevent confusion */
	if (ypserv)
		clnt_destroy(ypserv);
	if (!force)
		(void)fprintf(stderr,
		    "\t[note %s's ypserv running on host %s]\n",
		    host, inet_ntoa(*inaddr));
	exit(1);
}