xref: /freebsd/usr.bin/getent/getent.c (revision b1866dfea3f3f142697f8e325db49b8e668fba35)
1 /*	$NetBSD: getent.c,v 1.7 2005/08/24 14:31:02 ginsbach Exp $	*/
2 
3 /*-
4  * Copyright (c) 2004 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Luke Mewburn.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include <sys/socket.h>
36 #include <sys/param.h>
37 #include <arpa/inet.h>
38 #include <arpa/nameser.h>
39 #include <net/if.h>
40 #include <netinet/if_ether.h>
41 #include <netinet/in.h>		/* for INET6_ADDRSTRLEN */
42 #include <rpc/rpcent.h>
43 
44 #include <assert.h>
45 #include <ctype.h>
46 #include <errno.h>
47 #include <grp.h>
48 #include <limits.h>
49 #include <netdb.h>
50 #include <pwd.h>
51 #include <stdarg.h>
52 #include <stdint.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <unistd.h>
57 #include <utmpx.h>
58 
59 static int	usage(void);
60 static int	parsenum(const char *, unsigned long *);
61 static int	ethers(int, char *[]);
62 static int	group(int, char *[]);
63 static int	hosts(int, char *[]);
64 static int	networks(int, char *[]);
65 static int	passwd(int, char *[]);
66 static int	protocols(int, char *[]);
67 static int	rpc(int, char *[]);
68 static int	services(int, char *[]);
69 static int	shells(int, char *[]);
70 static int	utmpx(int, char *[]);
71 
72 enum {
73 	RV_OK		= 0,
74 	RV_USAGE	= 1,
75 	RV_NOTFOUND	= 2,
76 	RV_NOENUM	= 3
77 };
78 
79 static struct getentdb {
80 	const char	*name;
81 	int		(*callback)(int, char *[]);
82 } databases[] = {
83 	{	"ethers",	ethers,		},
84 	{	"group",	group,		},
85 	{	"hosts",	hosts,		},
86 	{	"networks",	networks,	},
87 	{	"passwd",	passwd,		},
88 	{	"protocols",	protocols,	},
89 	{	"rpc",		rpc,		},
90 	{	"services",	services,	},
91 	{	"shells",	shells,		},
92 	{	"utmpx",	utmpx,		},
93 
94 	{	NULL,		NULL,		},
95 };
96 
97 int
98 main(int argc, char *argv[])
99 {
100 	struct getentdb	*curdb;
101 
102 	setprogname(argv[0]);
103 
104 	if (argc < 2)
105 		usage();
106 	for (curdb = databases; curdb->name != NULL; curdb++) {
107 		if (strcmp(curdb->name, argv[1]) == 0) {
108 			exit(curdb->callback(argc, argv));
109 		}
110 	}
111 	fprintf(stderr, "Unknown database: %s\n", argv[1]);
112 	usage();
113 	/* NOTREACHED */
114 	return RV_USAGE;
115 }
116 
117 static int
118 usage(void)
119 {
120 	struct getentdb	*curdb;
121 
122 	fprintf(stderr, "Usage: %s database [key ...]\n",
123 	    getprogname());
124 	fprintf(stderr, "       database may be one of:\n\t");
125 	for (curdb = databases; curdb->name != NULL; curdb++) {
126 		fprintf(stderr, " %s", curdb->name);
127 	}
128 	fprintf(stderr, "\n");
129 	exit(RV_USAGE);
130 	/* NOTREACHED */
131 }
132 
133 static int
134 parsenum(const char *word, unsigned long *result)
135 {
136 	unsigned long	num;
137 	char		*ep;
138 
139 	assert(word != NULL);
140 	assert(result != NULL);
141 
142 	if (!isdigit((unsigned char)word[0]))
143 		return 0;
144 	errno = 0;
145 	num = strtoul(word, &ep, 10);
146 	if (num == ULONG_MAX && errno == ERANGE)
147 		return 0;
148 	if (*ep != '\0')
149 		return 0;
150 	*result = num;
151 	return 1;
152 }
153 
154 /*
155  * printfmtstrings --
156  *	vprintf(format, ...),
157  *	then the aliases (beginning with prefix, separated by sep),
158  *	then a newline
159  */
160 static void
161 printfmtstrings(char *strings[], const char *prefix, const char *sep,
162 	const char *fmt, ...)
163 {
164 	va_list		ap;
165 	const char	*curpref;
166 	int		i;
167 
168 	va_start(ap, fmt);
169 	vprintf(fmt, ap);
170 
171 	curpref = prefix;
172 	for (i = 0; strings[i] != NULL; i++) {
173 		printf("%s%s", curpref, strings[i]);
174 		curpref = sep;
175 	}
176 	printf("\n");
177 	va_end(ap);
178 }
179 
180 /*
181  * ethers
182  */
183 static int
184 ethers(int argc, char *argv[])
185 {
186 	char		hostname[MAXHOSTNAMELEN + 1], *hp;
187 	struct ether_addr ea, *eap;
188 	int		i, rv;
189 
190 	assert(argc > 1);
191 	assert(argv != NULL);
192 
193 #define ETHERSPRINT	printf("%-17s  %s\n", ether_ntoa(eap), hp)
194 
195 	rv = RV_OK;
196 	if (argc == 2) {
197 		fprintf(stderr, "Enumeration not supported on ethers\n");
198 		rv = RV_NOENUM;
199 	} else {
200 		for (i = 2; i < argc; i++) {
201 			if ((eap = ether_aton(argv[i])) == NULL) {
202 				eap = &ea;
203 				hp = argv[i];
204 				if (ether_hostton(hp, eap) != 0) {
205 					rv = RV_NOTFOUND;
206 					break;
207 				}
208 			} else {
209 				hp = hostname;
210 				if (ether_ntohost(hp, eap) != 0) {
211 					rv = RV_NOTFOUND;
212 					break;
213 				}
214 			}
215 			ETHERSPRINT;
216 		}
217 	}
218 	return rv;
219 }
220 
221 /*
222  * group
223  */
224 
225 static int
226 group(int argc, char *argv[])
227 {
228 	struct group	*gr;
229 	unsigned long	id;
230 	int		i, rv;
231 
232 	assert(argc > 1);
233 	assert(argv != NULL);
234 
235 #define GROUPPRINT	printfmtstrings(gr->gr_mem, ":", ",", "%s:%s:%u", \
236 			    gr->gr_name, gr->gr_passwd, gr->gr_gid)
237 
238 	setgroupent(1);
239 	rv = RV_OK;
240 	if (argc == 2) {
241 		while ((gr = getgrent()) != NULL)
242 			GROUPPRINT;
243 	} else {
244 		for (i = 2; i < argc; i++) {
245 			if (parsenum(argv[i], &id))
246 				gr = getgrgid((gid_t)id);
247 			else
248 				gr = getgrnam(argv[i]);
249 			if (gr != NULL)
250 				GROUPPRINT;
251 			else {
252 				rv = RV_NOTFOUND;
253 				break;
254 			}
255 		}
256 	}
257 	endgrent();
258 	return rv;
259 }
260 
261 
262 /*
263  * hosts
264  */
265 
266 static void
267 hostsprint(const struct hostent *he)
268 {
269 	char	buf[INET6_ADDRSTRLEN];
270 
271 	assert(he != NULL);
272 	if (inet_ntop(he->h_addrtype, he->h_addr, buf, sizeof(buf)) == NULL)
273 		strlcpy(buf, "# unknown", sizeof(buf));
274 	printfmtstrings(he->h_aliases, "  ", " ", "%-16s  %s", buf, he->h_name);
275 }
276 
277 static int
278 hosts(int argc, char *argv[])
279 {
280 	struct hostent	*he;
281 	char		addr[IN6ADDRSZ];
282 	int		i, rv;
283 
284 	assert(argc > 1);
285 	assert(argv != NULL);
286 
287 	sethostent(1);
288 	rv = RV_OK;
289 	if (argc == 2) {
290 		while ((he = gethostent()) != NULL)
291 			hostsprint(he);
292 	} else {
293 		for (i = 2; i < argc; i++) {
294 			if (inet_pton(AF_INET6, argv[i], (void *)addr) > 0)
295 				he = gethostbyaddr(addr, IN6ADDRSZ, AF_INET6);
296 			else if (inet_pton(AF_INET, argv[i], (void *)addr) > 0)
297 				he = gethostbyaddr(addr, INADDRSZ, AF_INET);
298 			else
299 				he = gethostbyname(argv[i]);
300 			if (he != NULL)
301 				hostsprint(he);
302 			else {
303 				rv = RV_NOTFOUND;
304 				break;
305 			}
306 		}
307 	}
308 	endhostent();
309 	return rv;
310 }
311 
312 /*
313  * networks
314  */
315 static void
316 networksprint(const struct netent *ne)
317 {
318 	char		buf[INET6_ADDRSTRLEN];
319 	struct	in_addr	ianet;
320 
321 	assert(ne != NULL);
322 	ianet = inet_makeaddr(ne->n_net, 0);
323 	if (inet_ntop(ne->n_addrtype, &ianet, buf, sizeof(buf)) == NULL)
324 		strlcpy(buf, "# unknown", sizeof(buf));
325 	printfmtstrings(ne->n_aliases, "  ", " ", "%-16s  %s", ne->n_name, buf);
326 }
327 
328 static int
329 networks(int argc, char *argv[])
330 {
331 	struct netent	*ne;
332 	in_addr_t	net;
333 	int		i, rv;
334 
335 	assert(argc > 1);
336 	assert(argv != NULL);
337 
338 	setnetent(1);
339 	rv = RV_OK;
340 	if (argc == 2) {
341 		while ((ne = getnetent()) != NULL)
342 			networksprint(ne);
343 	} else {
344 		for (i = 2; i < argc; i++) {
345 			net = inet_network(argv[i]);
346 			if (net != INADDR_NONE)
347 				ne = getnetbyaddr(net, AF_INET);
348 			else
349 				ne = getnetbyname(argv[i]);
350 			if (ne != NULL)
351 				networksprint(ne);
352 			else {
353 				rv = RV_NOTFOUND;
354 				break;
355 			}
356 		}
357 	}
358 	endnetent();
359 	return rv;
360 }
361 
362 /*
363  * passwd
364  */
365 static int
366 passwd(int argc, char *argv[])
367 {
368 	struct passwd	*pw;
369 	unsigned long	id;
370 	int		i, rv;
371 
372 	assert(argc > 1);
373 	assert(argv != NULL);
374 
375 #define PASSWDPRINT	printf("%s:%s:%u:%u:%s:%s:%s\n", \
376 			    pw->pw_name, pw->pw_passwd, pw->pw_uid, \
377 			    pw->pw_gid, pw->pw_gecos, pw->pw_dir, pw->pw_shell)
378 
379 	setpassent(1);
380 	rv = RV_OK;
381 	if (argc == 2) {
382 		while ((pw = getpwent()) != NULL)
383 			PASSWDPRINT;
384 	} else {
385 		for (i = 2; i < argc; i++) {
386 			if (parsenum(argv[i], &id))
387 				pw = getpwuid((uid_t)id);
388 			else
389 				pw = getpwnam(argv[i]);
390 			if (pw != NULL)
391 				PASSWDPRINT;
392 			else {
393 				rv = RV_NOTFOUND;
394 				break;
395 			}
396 		}
397 	}
398 	endpwent();
399 	return rv;
400 }
401 
402 /*
403  * protocols
404  */
405 static int
406 protocols(int argc, char *argv[])
407 {
408 	struct protoent	*pe;
409 	unsigned long	id;
410 	int		i, rv;
411 
412 	assert(argc > 1);
413 	assert(argv != NULL);
414 
415 #define PROTOCOLSPRINT	printfmtstrings(pe->p_aliases, "  ", " ", \
416 			    "%-16s  %5d", pe->p_name, pe->p_proto)
417 
418 	setprotoent(1);
419 	rv = RV_OK;
420 	if (argc == 2) {
421 		while ((pe = getprotoent()) != NULL)
422 			PROTOCOLSPRINT;
423 	} else {
424 		for (i = 2; i < argc; i++) {
425 			if (parsenum(argv[i], &id))
426 				pe = getprotobynumber((int)id);
427 			else
428 				pe = getprotobyname(argv[i]);
429 			if (pe != NULL)
430 				PROTOCOLSPRINT;
431 			else {
432 				rv = RV_NOTFOUND;
433 				break;
434 			}
435 		}
436 	}
437 	endprotoent();
438 	return rv;
439 }
440 
441 /*
442  * rpc
443  */
444 static int
445 rpc(int argc, char *argv[])
446 {
447 	struct rpcent	*re;
448 	unsigned long	id;
449 	int		i, rv;
450 
451 	assert(argc > 1);
452 	assert(argv != NULL);
453 
454 #define RPCPRINT	printfmtstrings(re->r_aliases, "  ", " ", \
455 				"%-16s  %6d", \
456 				re->r_name, re->r_number)
457 
458 	setrpcent(1);
459 	rv = RV_OK;
460 	if (argc == 2) {
461 		while ((re = getrpcent()) != NULL)
462 			RPCPRINT;
463 	} else {
464 		for (i = 2; i < argc; i++) {
465 			if (parsenum(argv[i], &id))
466 				re = getrpcbynumber((int)id);
467 			else
468 				re = getrpcbyname(argv[i]);
469 			if (re != NULL)
470 				RPCPRINT;
471 			else {
472 				rv = RV_NOTFOUND;
473 				break;
474 			}
475 		}
476 	}
477 	endrpcent();
478 	return rv;
479 }
480 
481 /*
482  * services
483  */
484 static int
485 services(int argc, char *argv[])
486 {
487 	struct servent	*se;
488 	unsigned long	id;
489 	char		*proto;
490 	int		i, rv;
491 
492 	assert(argc > 1);
493 	assert(argv != NULL);
494 
495 #define SERVICESPRINT	printfmtstrings(se->s_aliases, "  ", " ", \
496 			    "%-16s  %5d/%s", \
497 			    se->s_name, ntohs(se->s_port), se->s_proto)
498 
499 	setservent(1);
500 	rv = RV_OK;
501 	if (argc == 2) {
502 		while ((se = getservent()) != NULL)
503 			SERVICESPRINT;
504 	} else {
505 		for (i = 2; i < argc; i++) {
506 			proto = strchr(argv[i], '/');
507 			if (proto != NULL)
508 				*proto++ = '\0';
509 			if (parsenum(argv[i], &id))
510 				se = getservbyport(htons((u_short)id), proto);
511 			else
512 				se = getservbyname(argv[i], proto);
513 			if (se != NULL)
514 				SERVICESPRINT;
515 			else {
516 				rv = RV_NOTFOUND;
517 				break;
518 			}
519 		}
520 	}
521 	endservent();
522 	return rv;
523 }
524 
525 /*
526  * shells
527  */
528 static int
529 shells(int argc, char *argv[])
530 {
531 	const char	*sh;
532 	int		i, rv;
533 
534 	assert(argc > 1);
535 	assert(argv != NULL);
536 
537 #define SHELLSPRINT	printf("%s\n", sh)
538 
539 	setusershell();
540 	rv = RV_OK;
541 	if (argc == 2) {
542 		while ((sh = getusershell()) != NULL)
543 			SHELLSPRINT;
544 	} else {
545 		for (i = 2; i < argc; i++) {
546 			setusershell();
547 			while ((sh = getusershell()) != NULL) {
548 				if (strcmp(sh, argv[i]) == 0) {
549 					SHELLSPRINT;
550 					break;
551 				}
552 			}
553 			if (sh == NULL) {
554 				rv = RV_NOTFOUND;
555 				break;
556 			}
557 		}
558 	}
559 	endusershell();
560 	return rv;
561 }
562 
563 /*
564  * utmpx
565  */
566 
567 #define	UTMPXPRINTID do {			\
568 	size_t i;				\
569 	for (i = 0; i < sizeof ut->ut_id; i++)	\
570 		printf("%02hhx", ut->ut_id[i]);	\
571 } while (0)
572 
573 static void
574 utmpxprint(const struct utmpx *ut)
575 {
576 
577 	if (ut->ut_type == EMPTY)
578 		return;
579 
580 	printf("[%jd.%06u -- %.24s] ",
581 	    (intmax_t)ut->ut_tv.tv_sec, (unsigned int)ut->ut_tv.tv_usec,
582 	    ctime(&ut->ut_tv.tv_sec));
583 
584 	switch (ut->ut_type) {
585 	case BOOT_TIME:
586 		printf("system boot\n");
587 		return;
588 	case SHUTDOWN_TIME:
589 		printf("system shutdown\n");
590 		return;
591 	case OLD_TIME:
592 		printf("old system time\n");
593 		return;
594 	case NEW_TIME:
595 		printf("new system time\n");
596 		return;
597 	case USER_PROCESS:
598 		printf("user process: id=\"");
599 		UTMPXPRINTID;
600 		printf("\" pid=\"%d\" user=\"%s\" line=\"%s\" host=\"%s\"\n",
601 		    ut->ut_pid, ut->ut_user, ut->ut_line, ut->ut_host);
602 		break;
603 	case INIT_PROCESS:
604 		printf("init process: id=\"");
605 		UTMPXPRINTID;
606 		printf("\" pid=\"%d\"\n", ut->ut_pid);
607 		break;
608 	case LOGIN_PROCESS:
609 		printf("login process: id=\"");
610 		UTMPXPRINTID;
611 		printf("\" pid=\"%d\" user=\"%s\" line=\"%s\" host=\"%s\"\n",
612 		    ut->ut_pid, ut->ut_user, ut->ut_line, ut->ut_host);
613 		break;
614 	case DEAD_PROCESS:
615 		printf("dead process: id=\"");
616 		UTMPXPRINTID;
617 		printf("\" pid=\"%d\"\n", ut->ut_pid);
618 		break;
619 	default:
620 		printf("unknown record type %hu\n", ut->ut_type);
621 		break;
622 	}
623 }
624 
625 static int
626 utmpx(int argc, char *argv[])
627 {
628 	const struct utmpx *ut;
629 	const char *file = NULL;
630 	int rv = RV_OK, db = 0;
631 
632 	assert(argc > 1);
633 	assert(argv != NULL);
634 
635 	if (argc == 3 || argc == 4) {
636 		if (strcmp(argv[2], "active") == 0)
637 			db = UTXDB_ACTIVE;
638 		else if (strcmp(argv[2], "lastlogin") == 0)
639 			db = UTXDB_LASTLOGIN;
640 		else if (strcmp(argv[2], "log") == 0)
641 			db = UTXDB_LOG;
642 		else
643 			rv = RV_USAGE;
644 		if (argc == 4)
645 			file = argv[3];
646 	} else {
647 		rv = RV_USAGE;
648 	}
649 
650 	if (rv == RV_USAGE) {
651 		fprintf(stderr,
652 		    "Usage: %s utmpx active | lastlogin | log [filename]\n",
653 		    getprogname());
654 	} else if (rv == RV_OK) {
655 		if (setutxdb(db, file) != 0)
656 			return (RV_NOTFOUND);
657 		while ((ut = getutxent()) != NULL)
658 			utmpxprint(ut);
659 		endutxent();
660 	}
661 	return (rv);
662 }
663