1 /* $NetBSD: getent.c,v 1.7 2005/08/24 14:31:02 ginsbach Exp $ */
4 * Copyright (c) 2004 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
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 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 *[]);
79 static struct getentdb {
81 int (*callback)(int, char *[]);
83 { "ethers", ethers, },
86 { "networks", networks, },
87 { "passwd", passwd, },
88 { "protocols", protocols, },
90 { "services", services, },
91 { "shells", shells, },
98 main(int argc, char *argv[])
100 struct getentdb *curdb;
102 setprogname(argv[0]);
106 for (curdb = databases; curdb->name != NULL; curdb++) {
107 if (strcmp(curdb->name, argv[1]) == 0) {
108 exit(curdb->callback(argc, argv));
111 fprintf(stderr, "Unknown database: %s\n", argv[1]);
120 struct getentdb *curdb;
122 fprintf(stderr, "Usage: %s database [key ...]\n",
124 fprintf(stderr, " database may be one of:\n\t");
125 for (curdb = databases; curdb->name != NULL; curdb++) {
126 fprintf(stderr, " %s", curdb->name);
128 fprintf(stderr, "\n");
134 parsenum(const char *word, unsigned long *result)
139 assert(word != NULL);
140 assert(result != NULL);
142 if (!isdigit((unsigned char)word[0]))
145 num = strtoul(word, &ep, 10);
146 if (num == ULONG_MAX && errno == ERANGE)
156 * vprintf(format, ...),
157 * then the aliases (beginning with prefix, separated by sep),
161 printfmtstrings(char *strings[], const char *prefix, const char *sep,
162 const char *fmt, ...)
172 for (i = 0; strings[i] != NULL; i++) {
173 printf("%s%s", curpref, strings[i]);
184 ethers(int argc, char *argv[])
186 char hostname[MAXHOSTNAMELEN + 1], *hp;
187 struct ether_addr ea, *eap;
191 assert(argv != NULL);
193 #define ETHERSPRINT printf("%-17s %s\n", ether_ntoa(eap), hp)
197 fprintf(stderr, "Enumeration not supported on ethers\n");
200 for (i = 2; i < argc; i++) {
201 if ((eap = ether_aton(argv[i])) == NULL) {
204 if (ether_hostton(hp, eap) != 0) {
210 if (ether_ntohost(hp, eap) != 0) {
226 group(int argc, char *argv[])
233 assert(argv != NULL);
235 #define GROUPPRINT printfmtstrings(gr->gr_mem, ":", ",", "%s:%s:%u", \
236 gr->gr_name, gr->gr_passwd, gr->gr_gid)
241 while ((gr = getgrent()) != NULL)
244 for (i = 2; i < argc; i++) {
245 if (parsenum(argv[i], &id))
246 gr = getgrgid((gid_t)id);
248 gr = getgrnam(argv[i]);
267 hostsprint(const struct hostent *he)
269 char buf[INET6_ADDRSTRLEN];
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);
278 hosts(int argc, char *argv[])
281 char addr[IN6ADDRSZ];
285 assert(argv != NULL);
290 while ((he = gethostent()) != NULL)
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);
299 he = gethostbyname(argv[i]);
316 networksprint(const struct netent *ne)
318 char buf[INET6_ADDRSTRLEN];
319 struct in_addr ianet;
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);
329 networks(int argc, char *argv[])
336 assert(argv != NULL);
341 while ((ne = getnetent()) != NULL)
344 for (i = 2; i < argc; i++) {
345 net = inet_network(argv[i]);
346 if (net != INADDR_NONE)
347 ne = getnetbyaddr(net, AF_INET);
349 ne = getnetbyname(argv[i]);
366 passwd(int argc, char *argv[])
373 assert(argv != NULL);
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)
382 while ((pw = getpwent()) != NULL)
385 for (i = 2; i < argc; i++) {
386 if (parsenum(argv[i], &id))
387 pw = getpwuid((uid_t)id);
389 pw = getpwnam(argv[i]);
406 protocols(int argc, char *argv[])
413 assert(argv != NULL);
415 #define PROTOCOLSPRINT printfmtstrings(pe->p_aliases, " ", " ", \
416 "%-16s %5d", pe->p_name, pe->p_proto)
421 while ((pe = getprotoent()) != NULL)
424 for (i = 2; i < argc; i++) {
425 if (parsenum(argv[i], &id))
426 pe = getprotobynumber((int)id);
428 pe = getprotobyname(argv[i]);
445 rpc(int argc, char *argv[])
452 assert(argv != NULL);
454 #define RPCPRINT printfmtstrings(re->r_aliases, " ", " ", \
456 re->r_name, re->r_number)
461 while ((re = getrpcent()) != NULL)
464 for (i = 2; i < argc; i++) {
465 if (parsenum(argv[i], &id))
466 re = getrpcbynumber((int)id);
468 re = getrpcbyname(argv[i]);
485 services(int argc, char *argv[])
493 assert(argv != NULL);
495 #define SERVICESPRINT printfmtstrings(se->s_aliases, " ", " ", \
497 se->s_name, ntohs(se->s_port), se->s_proto)
502 while ((se = getservent()) != NULL)
505 for (i = 2; i < argc; i++) {
506 proto = strchr(argv[i], '/');
509 if (parsenum(argv[i], &id))
510 se = getservbyport(htons((u_short)id), proto);
512 se = getservbyname(argv[i], proto);
529 shells(int argc, char *argv[])
535 assert(argv != NULL);
537 #define SHELLSPRINT printf("%s\n", sh)
542 while ((sh = getusershell()) != NULL)
545 for (i = 2; i < argc; i++) {
547 while ((sh = getusershell()) != NULL) {
548 if (strcmp(sh, argv[i]) == 0) {
567 #define UTMPXPRINTID do { \
569 for (i = 0; i < sizeof ut->ut_id; i++) \
570 printf("%02hhx", ut->ut_id[i]); \
574 utmpxprint(const struct utmpx *ut)
577 if (ut->ut_type == EMPTY)
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));
584 switch (ut->ut_type) {
586 printf("system boot\n");
589 printf("system shutdown\n");
592 printf("old system time\n");
595 printf("new system time\n");
598 printf("user process: id=\"");
600 printf("\" pid=\"%d\" user=\"%s\" line=\"%s\" host=\"%s\"\n",
601 ut->ut_pid, ut->ut_user, ut->ut_line, ut->ut_host);
604 printf("dead process: id=\"");
606 printf("\" pid=\"%d\"\n", ut->ut_pid);
609 printf("unknown record type\n");
615 utmpx(int argc, char *argv[])
617 const struct utmpx *ut;
618 const char *file = NULL;
619 int rv = RV_OK, db = 0;
622 assert(argv != NULL);
624 if (argc == 3 || argc == 4) {
625 if (strcmp(argv[2], "active") == 0)
627 else if (strcmp(argv[2], "lastlogin") == 0)
628 db = UTXDB_LASTLOGIN;
629 else if (strcmp(argv[2], "log") == 0)
639 if (rv == RV_USAGE) {
641 "Usage: %s utmpx active | lastlogin | log [filename]\n",
643 } else if (rv == RV_OK) {
644 if (setutxdb(db, file) != 0)
645 return (RV_NOTFOUND);
646 while ((ut = getutxent()) != NULL)