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 #include <sys/socket.h>
36 #include <sys/param.h>
37 #include <arpa/inet.h>
38 #include <arpa/nameser.h>
40 #include <netinet/if_ether.h>
41 #include <netinet/in.h> /* for INET6_ADDRSTRLEN */
42 #include <rpc/rpcent.h>
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 netgroup(int, char *[]);
65 static int networks(int, char *[]);
66 static int passwd(int, char *[]);
67 static int protocols(int, char *[]);
68 static int rpc(int, char *[]);
69 static int services(int, char *[]);
70 static int shells(int, char *[]);
71 static int utmpx(int, char *[]);
80 static struct getentdb {
82 int (*callback)(int, char *[]);
84 { "ethers", ethers, },
87 { "netgroup", netgroup, },
88 { "networks", networks, },
89 { "passwd", passwd, },
90 { "protocols", protocols, },
92 { "services", services, },
93 { "shells", shells, },
100 main(int argc, char *argv[])
102 struct getentdb *curdb;
104 setprogname(argv[0]);
108 for (curdb = databases; curdb->name != NULL; curdb++) {
109 if (strcmp(curdb->name, argv[1]) == 0) {
110 exit(curdb->callback(argc, argv));
113 fprintf(stderr, "Unknown database: %s\n", argv[1]);
122 struct getentdb *curdb;
124 fprintf(stderr, "Usage: %s database [key ...]\n",
126 fprintf(stderr, " database may be one of:\n\t");
127 for (curdb = databases; curdb->name != NULL; curdb++) {
128 fprintf(stderr, " %s", curdb->name);
130 fprintf(stderr, "\n");
136 parsenum(const char *word, unsigned long *result)
141 assert(word != NULL);
142 assert(result != NULL);
144 if (!isdigit((unsigned char)word[0]))
147 num = strtoul(word, &ep, 10);
148 if (num == ULONG_MAX && errno == ERANGE)
158 * vprintf(format, ...),
159 * then the aliases (beginning with prefix, separated by sep),
163 printfmtstrings(char *strings[], const char *prefix, const char *sep,
164 const char *fmt, ...)
174 for (i = 0; strings[i] != NULL; i++) {
175 printf("%s%s", curpref, strings[i]);
186 ethers(int argc, char *argv[])
188 char hostname[MAXHOSTNAMELEN + 1], *hp;
189 struct ether_addr ea, *eap;
193 assert(argv != NULL);
195 #define ETHERSPRINT printf("%-17s %s\n", ether_ntoa(eap), hp)
199 fprintf(stderr, "Enumeration not supported on ethers\n");
202 for (i = 2; i < argc; i++) {
203 if ((eap = ether_aton(argv[i])) == NULL) {
206 if (ether_hostton(hp, eap) != 0) {
212 if (ether_ntohost(hp, eap) != 0) {
228 group(int argc, char *argv[])
235 assert(argv != NULL);
237 #define GROUPPRINT printfmtstrings(gr->gr_mem, ":", ",", "%s:%s:%u", \
238 gr->gr_name, gr->gr_passwd, gr->gr_gid)
243 while ((gr = getgrent()) != NULL)
246 for (i = 2; i < argc; i++) {
247 if (parsenum(argv[i], &id))
248 gr = getgrgid((gid_t)id);
250 gr = getgrnam(argv[i]);
269 hostsprint(const struct hostent *he)
271 char buf[INET6_ADDRSTRLEN];
274 if (inet_ntop(he->h_addrtype, he->h_addr, buf, sizeof(buf)) == NULL)
275 strlcpy(buf, "# unknown", sizeof(buf));
276 printfmtstrings(he->h_aliases, " ", " ", "%-16s %s", buf, he->h_name);
280 hosts(int argc, char *argv[])
282 struct hostent *he4, *he6;
283 char addr[IN6ADDRSZ];
287 assert(argv != NULL);
293 while ((he4 = gethostent()) != NULL)
296 for (i = 2; i < argc; i++) {
297 if (inet_pton(AF_INET6, argv[i], (void *)addr) > 0) {
298 he6 = gethostbyaddr(addr, IN6ADDRSZ, AF_INET6);
301 } else if (inet_pton(AF_INET, argv[i],
303 he4 = gethostbyaddr(addr, INADDRSZ, AF_INET);
307 he6 = gethostbyname2(argv[i], AF_INET6);
310 he4 = gethostbyname(argv[i]);
314 if ( he4 == NULL && he6 == NULL ) {
328 networksprint(const struct netent *ne)
330 char buf[INET6_ADDRSTRLEN];
331 struct in_addr ianet;
334 ianet = inet_makeaddr(ne->n_net, 0);
335 if (inet_ntop(ne->n_addrtype, &ianet, buf, sizeof(buf)) == NULL)
336 strlcpy(buf, "# unknown", sizeof(buf));
337 printfmtstrings(ne->n_aliases, " ", " ", "%-16s %s", ne->n_name, buf);
341 networks(int argc, char *argv[])
348 assert(argv != NULL);
353 while ((ne = getnetent()) != NULL)
356 for (i = 2; i < argc; i++) {
357 net = inet_network(argv[i]);
358 if (net != INADDR_NONE)
359 ne = getnetbyaddr(net, AF_INET);
361 ne = getnetbyname(argv[i]);
378 passwd(int argc, char *argv[])
385 assert(argv != NULL);
387 #define PASSWDPRINT printf("%s:%s:%u:%u:%s:%s:%s\n", \
388 pw->pw_name, pw->pw_passwd, pw->pw_uid, \
389 pw->pw_gid, pw->pw_gecos, pw->pw_dir, pw->pw_shell)
394 while ((pw = getpwent()) != NULL)
397 for (i = 2; i < argc; i++) {
398 if (parsenum(argv[i], &id))
399 pw = getpwuid((uid_t)id);
401 pw = getpwnam(argv[i]);
418 protocols(int argc, char *argv[])
425 assert(argv != NULL);
427 #define PROTOCOLSPRINT printfmtstrings(pe->p_aliases, " ", " ", \
428 "%-16s %5d", pe->p_name, pe->p_proto)
433 while ((pe = getprotoent()) != NULL)
436 for (i = 2; i < argc; i++) {
437 if (parsenum(argv[i], &id))
438 pe = getprotobynumber((int)id);
440 pe = getprotobyname(argv[i]);
457 rpc(int argc, char *argv[])
464 assert(argv != NULL);
466 #define RPCPRINT printfmtstrings(re->r_aliases, " ", " ", \
468 re->r_name, re->r_number)
473 while ((re = getrpcent()) != NULL)
476 for (i = 2; i < argc; i++) {
477 if (parsenum(argv[i], &id))
478 re = getrpcbynumber((int)id);
480 re = getrpcbyname(argv[i]);
497 services(int argc, char *argv[])
505 assert(argv != NULL);
507 #define SERVICESPRINT printfmtstrings(se->s_aliases, " ", " ", \
509 se->s_name, ntohs(se->s_port), se->s_proto)
514 while ((se = getservent()) != NULL)
517 for (i = 2; i < argc; i++) {
518 proto = strchr(argv[i], '/');
521 if (parsenum(argv[i], &id))
522 se = getservbyport(htons((u_short)id), proto);
524 se = getservbyname(argv[i], proto);
541 shells(int argc, char *argv[])
547 assert(argv != NULL);
549 #define SHELLSPRINT printf("%s\n", sh)
554 while ((sh = getusershell()) != NULL)
557 for (i = 2; i < argc; i++) {
559 while ((sh = getusershell()) != NULL) {
560 if (strcmp(sh, argv[i]) == 0) {
579 netgroup(int argc, char *argv[])
581 char *host, *user, *domain;
586 assert(argv != NULL);
588 #define NETGROUPPRINT(s) (((s) != NULL) ? (s) : "")
592 fprintf(stderr, "Enumeration not supported on netgroup\n");
595 for (i = 2; i < argc; i++) {
596 setnetgrent(argv[i]);
598 while (getnetgrent(&host, &user, &domain) != 0) {
601 (void)fputs(argv[i], stdout);
603 (void)printf(" (%s,%s,%s)",
606 NETGROUPPRINT(domain));
620 #define UTMPXPRINTID do { \
622 for (i = 0; i < sizeof ut->ut_id; i++) \
623 printf("%02hhx", ut->ut_id[i]); \
627 utmpxprint(const struct utmpx *ut)
630 if (ut->ut_type == EMPTY)
633 printf("[%jd.%06u -- %.24s] ",
634 (intmax_t)ut->ut_tv.tv_sec, (unsigned int)ut->ut_tv.tv_usec,
635 ctime(&ut->ut_tv.tv_sec));
637 switch (ut->ut_type) {
639 printf("system boot\n");
642 printf("system shutdown\n");
645 printf("old system time\n");
648 printf("new system time\n");
651 printf("user process: id=\"");
653 printf("\" pid=\"%d\" user=\"%s\" line=\"%s\" host=\"%s\"\n",
654 ut->ut_pid, ut->ut_user, ut->ut_line, ut->ut_host);
657 printf("init process: id=\"");
659 printf("\" pid=\"%d\"\n", ut->ut_pid);
662 printf("login process: id=\"");
664 printf("\" pid=\"%d\" user=\"%s\" line=\"%s\" host=\"%s\"\n",
665 ut->ut_pid, ut->ut_user, ut->ut_line, ut->ut_host);
668 printf("dead process: id=\"");
670 printf("\" pid=\"%d\"\n", ut->ut_pid);
673 printf("unknown record type %hu\n", ut->ut_type);
679 utmpx(int argc, char *argv[])
681 const struct utmpx *ut;
682 const char *file = NULL;
683 int rv = RV_OK, db = 0;
686 assert(argv != NULL);
688 if (argc == 3 || argc == 4) {
689 if (strcmp(argv[2], "active") == 0)
691 else if (strcmp(argv[2], "lastlogin") == 0)
692 db = UTXDB_LASTLOGIN;
693 else if (strcmp(argv[2], "log") == 0)
703 if (rv == RV_USAGE) {
705 "Usage: %s utmpx active | lastlogin | log [filename]\n",
707 } else if (rv == RV_OK) {
708 if (setutxdb(db, file) != 0)
709 return (RV_NOTFOUND);
710 while ((ut = getutxent()) != NULL)