1 /* $NetBSD: getent.c,v 1.19 2012/03/15 02:02:23 joerg Exp $ */
4 * Copyright (c) 2004-2006 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>
34 __RCSID("$NetBSD: getent.c,v 1.19 2012/03/15 02:02:23 joerg Exp $");
37 #include <sys/socket.h>
56 #include <arpa/inet.h>
57 #include <arpa/nameser.h>
60 #include <net/if_ether.h>
62 #include <netinet/in.h> /* for INET6_ADDRSTRLEN */
65 #include <rpc/rpcent.h>
66 #endif /* !defined(__minix) */
70 static int usage(void) __attribute__((__noreturn__
));
71 static int parsenum(const char *, unsigned long *);
72 static int disktab(int, char *[]);
73 static int gettytab(int, char *[]);
74 static int ethers(int, char *[]);
75 static int group(int, char *[]);
76 static int hosts(int, char *[]);
77 static int netgroup(int, char *[]);
78 static int networks(int, char *[]);
79 static int passwd(int, char *[]);
80 static int printcap(int, char *[]);
81 static int protocols(int, char *[]);
83 static int rpc(int, char *[]);
84 #endif /* !defined(__minix) */
85 static int services(int, char *[]);
86 static int shells(int, char *[]);
95 static struct getentdb
{
97 int (*callback
)(int, char *[]);
99 { "disktab", disktab
, },
100 { "ethers", ethers
, },
101 { "gettytab", gettytab
, },
104 { "netgroup", netgroup
, },
105 { "networks", networks
, },
106 { "passwd", passwd
, },
107 { "printcap", printcap
, },
108 { "protocols", protocols
, },
109 #if !defined(__minix)
111 #endif /* !defined(__minix) */
112 { "services", services
, },
113 { "shells", shells
, },
120 main(int argc
, char *argv
[])
122 struct getentdb
*curdb
;
124 setprogname(argv
[0]);
128 for (curdb
= databases
; curdb
->name
!= NULL
; curdb
++)
129 if (strcmp(curdb
->name
, argv
[1]) == 0)
130 return (*curdb
->callback
)(argc
, argv
);
132 warn("Unknown database `%s'", argv
[1]);
140 struct getentdb
*curdb
;
143 (void)fprintf(stderr
, "Usage: %s database [key ...]\n",
145 (void)fprintf(stderr
, "\tdatabase may be one of:");
146 for (i
= 0, curdb
= databases
; curdb
->name
!= NULL
; curdb
++, i
++) {
148 (void)fputs("\n\t\t", stderr
);
149 (void)fprintf(stderr
, "%s%s", i
% 7 == 0 ? "" : " ",
152 (void)fprintf(stderr
, "\n");
158 parsenum(const char *word
, unsigned long *result
)
163 assert(word
!= NULL
);
164 assert(result
!= NULL
);
166 if (!isdigit((unsigned char)word
[0]))
169 num
= strtoul(word
, &ep
, 10);
170 if (num
== ULONG_MAX
&& errno
== ERANGE
)
180 * vprintf(format, ...),
181 * then the aliases (beginning with prefix, separated by sep),
184 static __printflike(4, 5) void
185 printfmtstrings(char *strings
[], const char *prefix
, const char *sep
,
186 const char *fmt
, ...)
193 (void)vprintf(fmt
, ap
);
197 for (i
= 0; strings
[i
] != NULL
; i
++) {
198 (void)printf("%s%s", curpref
, strings
[i
]);
210 ethers(int argc
, char *argv
[])
212 char hostname
[MAXHOSTNAMELEN
+ 1], *hp
;
213 struct ether_addr ea
, *eap
;
217 assert(argv
!= NULL
);
219 #define ETHERSPRINT (void)printf("%-17s %s\n", ether_ntoa(eap), hp)
223 warnx("Enumeration not supported on ethers");
226 for (i
= 2; i
< argc
; i
++) {
227 if ((eap
= ether_aton(argv
[i
])) == NULL
) {
230 if (ether_hostton(hp
, eap
) != 0) {
236 if (ether_ntohost(hp
, eap
) != 0) {
252 group(int argc
, char *argv
[])
259 assert(argv
!= NULL
);
261 #define GROUPPRINT printfmtstrings(gr->gr_mem, ":", ",", "%s:%s:%u", \
262 gr->gr_name, gr->gr_passwd, gr->gr_gid)
264 (void)setgroupent(1);
267 while ((gr
= getgrent()) != NULL
)
270 for (i
= 2; i
< argc
; i
++) {
271 if (parsenum(argv
[i
], &id
))
272 gr
= getgrgid((gid_t
)id
);
274 gr
= getgrnam(argv
[i
]);
293 hostsprint(const struct hostent
*he
)
295 char buf
[INET6_ADDRSTRLEN
];
298 if (inet_ntop(he
->h_addrtype
, he
->h_addr
, buf
, sizeof(buf
)) == NULL
)
299 (void)strlcpy(buf
, "# unknown", sizeof(buf
));
300 printfmtstrings(he
->h_aliases
, " ", " ", "%-16s %s", buf
, he
->h_name
);
304 hosts(int argc
, char *argv
[])
307 char addr
[IN6ADDRSZ
];
311 assert(argv
!= NULL
);
316 while ((he
= gethostent()) != NULL
)
319 for (i
= 2; i
< argc
; i
++) {
320 if (inet_pton(AF_INET6
, argv
[i
], (void *)addr
) > 0)
321 he
= gethostbyaddr(addr
, IN6ADDRSZ
, AF_INET6
);
322 else if (inet_pton(AF_INET
, argv
[i
], (void *)addr
) > 0)
323 he
= gethostbyaddr(addr
, INADDRSZ
, AF_INET
);
325 he
= gethostbyname(argv
[i
]);
342 netgroup(int argc
, char *argv
[])
346 const char *host
, *user
, *domain
;
349 assert(argv
!= NULL
);
351 #define NETGROUPPRINT(s) (((s) != NULL) ? (s) : "")
355 warnx("Enumeration not supported on netgroup");
358 for (i
= 2; i
< argc
; i
++) {
359 setnetgrent(argv
[i
]);
361 while (getnetgrent(&host
, &user
, &domain
) != 0) {
364 (void)fputs(argv
[i
], stdout
);
366 (void)printf(" (%s,%s,%s)",
369 NETGROUPPRINT(domain
));
385 networksprint(const struct netent
*ne
)
387 char buf
[INET6_ADDRSTRLEN
];
388 struct in_addr ianet
;
391 ianet
= inet_makeaddr(ne
->n_net
, 0);
392 if (inet_ntop(ne
->n_addrtype
, &ianet
, buf
, sizeof(buf
)) == NULL
)
393 (void)strlcpy(buf
, "# unknown", sizeof(buf
));
394 printfmtstrings(ne
->n_aliases
, " ", " ", "%-16s %s", ne
->n_name
, buf
);
398 networks(int argc
, char *argv
[])
405 assert(argv
!= NULL
);
410 while ((ne
= getnetent()) != NULL
)
413 for (i
= 2; i
< argc
; i
++) {
414 net
= inet_network(argv
[i
]);
415 if (net
!= INADDR_NONE
)
416 ne
= getnetbyaddr(net
, AF_INET
);
418 ne
= getnetbyname(argv
[i
]);
437 passwd(int argc
, char *argv
[])
444 assert(argv
!= NULL
);
446 #define PASSWDPRINT (void)printf("%s:%s:%u:%u:%s:%s:%s\n", \
447 pw->pw_name, pw->pw_passwd, pw->pw_uid, \
448 pw->pw_gid, pw->pw_gecos, pw->pw_dir, pw->pw_shell)
453 while ((pw
= getpwent()) != NULL
)
456 for (i
= 2; i
< argc
; i
++) {
457 if (parsenum(argv
[i
], &id
))
458 pw
= getpwuid((uid_t
)id
);
460 pw
= getpwnam(argv
[i
]);
474 mygetent(const char * const * db_array
, const char *name
)
479 switch (error
= cgetent(&buf
, db_array
, name
)) {
481 warnx("tc= loop in record `%s' in `%s'", name
, db_array
[0]);
484 warn("system error fetching record `%s' in `%s'", name
,
491 warnx("tc= reference not found in record for `%s' in `%s'",
495 warnx("unknown error %d in record `%s' in `%s'", error
, name
,
503 mygetone(const char * const * db_array
, int first
)
508 switch (error
= (first
? cgetfirst
: cgetnext
)(&buf
, db_array
)) {
510 warnx("tc= loop in `%s'", db_array
[0]);
513 warn("system error fetching record in `%s'", db_array
[0]);
519 warnx("tc= reference not found in `%s'", db_array
[0]);
522 warnx("unknown error %d in `%s'", error
, db_array
[0]);
529 capprint(const char *cap
)
531 char *c
= strchr(cap
, ':');
534 (void)printf("true\n");
536 int l
= (int)(c
- cap
);
537 (void)printf("%*.*s\n", l
, l
, cap
);
540 (void)printf("%s\n", cap
);
553 if (len
<= TERMWIDTH
) {
560 for (s
= b
+ TERMWIDTH
; s
> b
&& *s
!= ':'; s
--)
576 handleone(const char * const *db_array
, char *b
, int recurse
, int pretty
,
587 if (!recurse
|| cgetstr(b
, "tc", &tc
) <= 0)
590 b
= mygetent(db_array
, tc
);
596 handleone(db_array
, b
, recurse
, pretty
, ++level
);
601 handlecap(const char *db
, int argc
, char *argv
[])
603 static const char sfx
[] = "=#:";
604 const char *db_array
[] = { db
, NULL
};
608 int expand
= 1, recurse
= 0, pretty
= 0;
611 assert(argv
!= NULL
);
615 while ((c
= getopt(argc
, argv
, "pnr")) != -1)
634 csetexpandtc(expand
);
637 for (b
= mygetone(db_array
, 1); b
; b
= mygetone(db_array
, 0)) {
638 handleone(db_array
, b
, recurse
, pretty
, 0);
642 if ((b
= mygetent(db_array
, argv
[0])) == NULL
)
645 handleone(db_array
, b
, recurse
, pretty
, 0);
647 for (i
= 2; i
< argc
; i
++) {
648 for (j
= 0; j
< sizeof(sfx
) - 1; j
++) {
649 cap
= cgetcap(b
, argv
[i
], sfx
[j
]);
655 if (j
== sizeof(sfx
) - 1)
669 gettytab(int argc
, char *argv
[])
671 return handlecap(_PATH_GETTYTAB
, argc
, argv
);
679 printcap(int argc
, char *argv
[])
681 return handlecap(_PATH_PRINTCAP
, argc
, argv
);
689 disktab(int argc
, char *argv
[])
691 return handlecap(_PATH_DISKTAB
, argc
, argv
);
699 protocols(int argc
, char *argv
[])
706 assert(argv
!= NULL
);
708 #define PROTOCOLSPRINT printfmtstrings(pe->p_aliases, " ", " ", \
709 "%-16s %5d", pe->p_name, pe->p_proto)
714 while ((pe
= getprotoent()) != NULL
)
717 for (i
= 2; i
< argc
; i
++) {
718 if (parsenum(argv
[i
], &id
))
719 pe
= getprotobynumber((int)id
);
721 pe
= getprotobyname(argv
[i
]);
734 #if !defined(__minix)
740 rpc(int argc
, char *argv
[])
747 assert(argv
!= NULL
);
749 #define RPCPRINT printfmtstrings(re->r_aliases, " ", " ", \
751 re->r_name, re->r_number)
756 while ((re
= getrpcent()) != NULL
)
759 for (i
= 2; i
< argc
; i
++) {
760 if (parsenum(argv
[i
], &id
))
761 re
= getrpcbynumber((int)id
);
763 re
= getrpcbyname(argv
[i
]);
775 #endif /* !defined(__minix) */
782 services(int argc
, char *argv
[])
790 assert(argv
!= NULL
);
792 #define SERVICESPRINT printfmtstrings(se->s_aliases, " ", " ", \
794 se->s_name, ntohs(se->s_port), se->s_proto)
799 while ((se
= getservent()) != NULL
)
802 for (i
= 2; i
< argc
; i
++) {
803 proto
= strchr(argv
[i
], '/');
806 if (parsenum(argv
[i
], &id
))
807 se
= getservbyport(htons(id
), proto
);
809 se
= getservbyname(argv
[i
], proto
);
828 shells(int argc
, char *argv
[])
834 assert(argv
!= NULL
);
836 #define SHELLSPRINT (void)printf("%s\n", sh)
841 while ((sh
= getusershell()) != NULL
)
844 for (i
= 2; i
< argc
; i
++) {
846 while ((sh
= getusershell()) != NULL
) {
847 if (strcmp(sh
, argv
[i
]) == 0) {