1 /* $NetBSD: getent.c,v 1.7 2005/08/24 14:31:02 ginsbach Exp $ */
4 * SPDX-License-Identifier: BSD-2-Clause
6 * Copyright (c) 2004 The NetBSD Foundation, Inc.
9 * This code is derived from software contributed to The NetBSD Foundation
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/socket.h>
38 #include <sys/param.h>
39 #include <arpa/inet.h>
40 #include <arpa/nameser.h>
42 #include <netinet/if_ether.h>
43 #include <netinet/in.h> /* for INET6_ADDRSTRLEN */
44 #include <rpc/rpcent.h>
61 static int usage(void);
62 static int parsenum(const char *, unsigned long *);
63 static int ethers(int, char *[]);
64 static int group(int, char *[]);
65 static int hosts(int, char *[]);
66 static int netgroup(int, char *[]);
67 static int networks(int, char *[]);
68 static int passwd(int, char *[]);
69 static int protocols(int, char *[]);
70 static int rpc(int, char *[]);
71 static int services(int, char *[]);
72 static int shells(int, char *[]);
73 static int utmpx(int, char *[]);
82 static struct getentdb {
84 int (*callback)(int, char *[]);
86 { "ethers", ethers, },
89 { "netgroup", netgroup, },
90 { "networks", networks, },
91 { "passwd", passwd, },
92 { "protocols", protocols, },
94 { "services", services, },
95 { "shells", shells, },
102 main(int argc, char *argv[])
104 struct getentdb *curdb;
106 setprogname(argv[0]);
110 for (curdb = databases; curdb->name != NULL; curdb++) {
111 if (strcmp(curdb->name, argv[1]) == 0) {
112 exit(curdb->callback(argc, argv));
115 fprintf(stderr, "Unknown database: %s\n", argv[1]);
124 struct getentdb *curdb;
126 fprintf(stderr, "Usage: %s database [key ...]\n",
128 fprintf(stderr, " database may be one of:\n\t");
129 for (curdb = databases; curdb->name != NULL; curdb++) {
130 fprintf(stderr, " %s", curdb->name);
132 fprintf(stderr, "\n");
138 parsenum(const char *word, unsigned long *result)
143 assert(word != NULL);
144 assert(result != NULL);
146 if (!isdigit((unsigned char)word[0]))
149 num = strtoul(word, &ep, 10);
150 if (num == ULONG_MAX && errno == ERANGE)
160 * vprintf(format, ...),
161 * then the aliases (beginning with prefix, separated by sep),
165 printfmtstrings(char *strings[], const char *prefix, const char *sep,
166 const char *fmt, ...)
176 for (i = 0; strings[i] != NULL; i++) {
177 printf("%s%s", curpref, strings[i]);
188 ethers(int argc, char *argv[])
190 char hostname[MAXHOSTNAMELEN + 1], *hp;
191 struct ether_addr ea, *eap;
195 assert(argv != NULL);
197 #define ETHERSPRINT printf("%-17s %s\n", ether_ntoa(eap), hp)
201 fprintf(stderr, "Enumeration not supported on ethers\n");
204 for (i = 2; i < argc; i++) {
205 if ((eap = ether_aton(argv[i])) == NULL) {
208 if (ether_hostton(hp, eap) != 0) {
214 if (ether_ntohost(hp, eap) != 0) {
230 group(int argc, char *argv[])
237 assert(argv != NULL);
239 #define GROUPPRINT printfmtstrings(gr->gr_mem, ":", ",", "%s:%s:%u", \
240 gr->gr_name, gr->gr_passwd, gr->gr_gid)
245 while ((gr = getgrent()) != NULL)
248 for (i = 2; i < argc; i++) {
249 if (parsenum(argv[i], &id))
250 gr = getgrgid((gid_t)id);
252 gr = getgrnam(argv[i]);
271 hostsprint(const struct hostent *he)
273 char buf[INET6_ADDRSTRLEN];
276 if (inet_ntop(he->h_addrtype, he->h_addr, buf, sizeof(buf)) == NULL)
277 strlcpy(buf, "# unknown", sizeof(buf));
278 printfmtstrings(he->h_aliases, " ", " ", "%-16s %s", buf, he->h_name);
282 hosts(int argc, char *argv[])
284 struct hostent *he4, *he6;
285 char addr[IN6ADDRSZ];
289 assert(argv != NULL);
295 while ((he4 = gethostent()) != NULL)
298 for (i = 2; i < argc; i++) {
299 if (inet_pton(AF_INET6, argv[i], (void *)addr) > 0) {
300 he6 = gethostbyaddr(addr, IN6ADDRSZ, AF_INET6);
303 } else if (inet_pton(AF_INET, argv[i],
305 he4 = gethostbyaddr(addr, INADDRSZ, AF_INET);
309 he6 = gethostbyname2(argv[i], AF_INET6);
312 he4 = gethostbyname(argv[i]);
316 if ( he4 == NULL && he6 == NULL ) {
330 networksprint(const struct netent *ne)
332 char buf[INET6_ADDRSTRLEN];
333 struct in_addr ianet;
336 ianet = inet_makeaddr(ne->n_net, 0);
337 if (inet_ntop(ne->n_addrtype, &ianet, buf, sizeof(buf)) == NULL)
338 strlcpy(buf, "# unknown", sizeof(buf));
339 printfmtstrings(ne->n_aliases, " ", " ", "%-16s %s", ne->n_name, buf);
343 networks(int argc, char *argv[])
350 assert(argv != NULL);
355 while ((ne = getnetent()) != NULL)
358 for (i = 2; i < argc; i++) {
359 net = inet_network(argv[i]);
360 if (net != INADDR_NONE)
361 ne = getnetbyaddr(net, AF_INET);
363 ne = getnetbyname(argv[i]);
380 passwd(int argc, char *argv[])
387 assert(argv != NULL);
389 #define PASSWDPRINT printf("%s:%s:%u:%u:%s:%s:%s\n", \
390 pw->pw_name, pw->pw_passwd, pw->pw_uid, \
391 pw->pw_gid, pw->pw_gecos, pw->pw_dir, pw->pw_shell)
396 while ((pw = getpwent()) != NULL)
399 for (i = 2; i < argc; i++) {
400 if (parsenum(argv[i], &id))
401 pw = getpwuid((uid_t)id);
403 pw = getpwnam(argv[i]);
420 protocols(int argc, char *argv[])
427 assert(argv != NULL);
429 #define PROTOCOLSPRINT printfmtstrings(pe->p_aliases, " ", " ", \
430 "%-16s %5d", pe->p_name, pe->p_proto)
435 while ((pe = getprotoent()) != NULL)
438 for (i = 2; i < argc; i++) {
439 if (parsenum(argv[i], &id))
440 pe = getprotobynumber((int)id);
442 pe = getprotobyname(argv[i]);
459 rpc(int argc, char *argv[])
466 assert(argv != NULL);
468 #define RPCPRINT printfmtstrings(re->r_aliases, " ", " ", \
470 re->r_name, re->r_number)
475 while ((re = getrpcent()) != NULL)
478 for (i = 2; i < argc; i++) {
479 if (parsenum(argv[i], &id))
480 re = getrpcbynumber((int)id);
482 re = getrpcbyname(argv[i]);
499 services(int argc, char *argv[])
507 assert(argv != NULL);
509 #define SERVICESPRINT printfmtstrings(se->s_aliases, " ", " ", \
511 se->s_name, ntohs(se->s_port), se->s_proto)
516 while ((se = getservent()) != NULL)
519 for (i = 2; i < argc; i++) {
520 proto = strchr(argv[i], '/');
523 if (parsenum(argv[i], &id))
524 se = getservbyport(htons((u_short)id), proto);
526 se = getservbyname(argv[i], proto);
543 shells(int argc, char *argv[])
549 assert(argv != NULL);
551 #define SHELLSPRINT printf("%s\n", sh)
556 while ((sh = getusershell()) != NULL)
559 for (i = 2; i < argc; i++) {
561 while ((sh = getusershell()) != NULL) {
562 if (strcmp(sh, argv[i]) == 0) {
581 netgroup(int argc, char *argv[])
583 char *host, *user, *domain;
588 assert(argv != NULL);
590 #define NETGROUPPRINT(s) (((s) != NULL) ? (s) : "")
594 fprintf(stderr, "Enumeration not supported on netgroup\n");
597 for (i = 2; i < argc; i++) {
598 setnetgrent(argv[i]);
600 while (getnetgrent(&host, &user, &domain) != 0) {
603 (void)fputs(argv[i], stdout);
605 (void)printf(" (%s,%s,%s)",
608 NETGROUPPRINT(domain));
622 #define UTMPXPRINTID do { \
624 for (i = 0; i < sizeof ut->ut_id; i++) \
625 printf("%02hhx", ut->ut_id[i]); \
629 utmpxprint(const struct utmpx *ut)
632 if (ut->ut_type == EMPTY)
635 printf("[%jd.%06u -- %.24s] ",
636 (intmax_t)ut->ut_tv.tv_sec, (unsigned int)ut->ut_tv.tv_usec,
637 ctime(&ut->ut_tv.tv_sec));
639 switch (ut->ut_type) {
641 printf("system boot\n");
644 printf("system shutdown\n");
647 printf("old system time\n");
650 printf("new system time\n");
653 printf("user process: id=\"");
655 printf("\" pid=\"%d\" user=\"%s\" line=\"%s\" host=\"%s\"\n",
656 ut->ut_pid, ut->ut_user, ut->ut_line, ut->ut_host);
659 printf("init process: id=\"");
661 printf("\" pid=\"%d\"\n", ut->ut_pid);
664 printf("login process: id=\"");
666 printf("\" pid=\"%d\" user=\"%s\" line=\"%s\" host=\"%s\"\n",
667 ut->ut_pid, ut->ut_user, ut->ut_line, ut->ut_host);
670 printf("dead process: id=\"");
672 printf("\" pid=\"%d\"\n", ut->ut_pid);
675 printf("unknown record type %hu\n", ut->ut_type);
681 utmpx(int argc, char *argv[])
683 const struct utmpx *ut;
684 const char *file = NULL;
685 int rv = RV_OK, db = 0;
688 assert(argv != NULL);
690 if (argc == 3 || argc == 4) {
691 if (strcmp(argv[2], "active") == 0)
693 else if (strcmp(argv[2], "lastlogin") == 0)
694 db = UTXDB_LASTLOGIN;
695 else if (strcmp(argv[2], "log") == 0)
705 if (rv == RV_USAGE) {
707 "Usage: %s utmpx active | lastlogin | log [filename]\n",
709 } else if (rv == RV_OK) {
710 if (setutxdb(db, file) != 0)
711 return (RV_NOTFOUND);
712 while ((ut = getutxent()) != NULL)