1 /* $NetBSD: getent.c,v 1.15 2009/02/24 06:10:52 yamt 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.15 2009/02/24 06:10:52 yamt 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 */
64 #include <rpc/rpcent.h>
68 static int usage(void) __attribute__((__noreturn__
));
69 static int parsenum(const char *, unsigned long *);
70 static int disktab(int, char *[]);
71 static int gettytab(int, char *[]);
72 static int ethers(int, char *[]);
73 static int group(int, char *[]);
74 static int hosts(int, char *[]);
75 static int netgroup(int, char *[]);
76 static int networks(int, char *[]);
77 static int passwd(int, char *[]);
78 static int printcap(int, char *[]);
79 static int protocols(int, char *[]);
80 static int rpc(int, char *[]);
81 static int services(int, char *[]);
82 static int shells(int, char *[]);
83 static int termcap(int, char *[]);
92 static struct getentdb
{
94 int (*callback
)(int, char *[]);
96 { "disktab", disktab
, },
97 { "ethers", ethers
, },
98 { "gettytab", gettytab
, },
101 { "netgroup", netgroup
, },
102 { "networks", networks
, },
103 { "passwd", passwd
, },
104 { "printcap", printcap
, },
105 { "protocols", protocols
, },
107 { "services", services
, },
108 { "shells", shells
, },
109 { "termcap", termcap
, },
116 main(int argc
, char *argv
[])
118 struct getentdb
*curdb
;
120 setprogname(argv
[0]);
124 for (curdb
= databases
; curdb
->name
!= NULL
; curdb
++)
125 if (strcmp(curdb
->name
, argv
[1]) == 0)
126 return (*curdb
->callback
)(argc
, argv
);
128 warn("Unknown database `%s'", argv
[1]);
136 struct getentdb
*curdb
;
138 (void)fprintf(stderr
, "Usage: %s database [key ...]\n",
140 (void)fprintf(stderr
, " database may be one of:\n\t");
141 for (curdb
= databases
; curdb
->name
!= NULL
; curdb
++)
142 (void)fprintf(stderr
, " %s", curdb
->name
);
143 (void)fprintf(stderr
, "\n");
149 parsenum(const char *word
, unsigned long *result
)
154 assert(word
!= NULL
);
155 assert(result
!= NULL
);
157 if (!isdigit((unsigned char)word
[0]))
160 num
= strtoul(word
, &ep
, 10);
161 if (num
== ULONG_MAX
&& errno
== ERANGE
)
171 * vprintf(format, ...),
172 * then the aliases (beginning with prefix, separated by sep),
176 printfmtstrings(char *strings
[], const char *prefix
, const char *sep
,
177 const char *fmt
, ...)
184 (void)vprintf(fmt
, ap
);
188 for (i
= 0; strings
[i
] != NULL
; i
++) {
189 (void)printf("%s%s", curpref
, strings
[i
]);
201 ethers(int argc
, char *argv
[])
203 char hostname
[MAXHOSTNAMELEN
+ 1], *hp
;
204 struct ether_addr ea
, *eap
;
208 assert(argv
!= NULL
);
210 #define ETHERSPRINT (void)printf("%-17s %s\n", ether_ntoa(eap), hp)
214 warnx("Enumeration not supported on ethers");
217 for (i
= 2; i
< argc
; i
++) {
218 if ((eap
= ether_aton(argv
[i
])) == NULL
) {
221 if (ether_hostton(hp
, eap
) != 0) {
227 if (ether_ntohost(hp
, eap
) != 0) {
243 group(int argc
, char *argv
[])
250 assert(argv
!= NULL
);
252 #define GROUPPRINT printfmtstrings(gr->gr_mem, ":", ",", "%s:%s:%u", \
253 gr->gr_name, gr->gr_passwd, gr->gr_gid)
255 (void)setgroupent(1);
258 while ((gr
= getgrent()) != NULL
)
261 for (i
= 2; i
< argc
; i
++) {
262 if (parsenum(argv
[i
], &id
))
263 gr
= getgrgid((gid_t
)id
);
265 gr
= getgrnam(argv
[i
]);
284 hostsprint(const struct hostent
*he
)
286 char buf
[INET6_ADDRSTRLEN
];
289 if (inet_ntop(he
->h_addrtype
, he
->h_addr
, buf
, sizeof(buf
)) == NULL
)
290 (void)strlcpy(buf
, "# unknown", sizeof(buf
));
291 printfmtstrings(he
->h_aliases
, " ", " ", "%-16s %s", buf
, he
->h_name
);
295 hosts(int argc
, char *argv
[])
298 char addr
[IN6ADDRSZ
];
302 assert(argv
!= NULL
);
307 while ((he
= gethostent()) != NULL
)
310 for (i
= 2; i
< argc
; i
++) {
311 if (inet_pton(AF_INET6
, argv
[i
], (void *)addr
) > 0)
312 he
= gethostbyaddr(addr
, IN6ADDRSZ
, AF_INET6
);
313 else if (inet_pton(AF_INET
, argv
[i
], (void *)addr
) > 0)
314 he
= gethostbyaddr(addr
, INADDRSZ
, AF_INET
);
316 he
= gethostbyname(argv
[i
]);
333 netgroup(int argc
, char *argv
[])
337 const char *host
, *user
, *domain
;
340 assert(argv
!= NULL
);
342 #define NETGROUPPRINT(s) (((s) != NULL) ? (s) : "")
346 warnx("Enumeration not supported on netgroup");
349 for (i
= 2; i
< argc
; i
++) {
350 setnetgrent(argv
[i
]);
352 while (getnetgrent(&host
, &user
, &domain
) != 0) {
355 (void)fputs(argv
[i
], stdout
);
357 (void)printf(" (%s,%s,%s)",
360 NETGROUPPRINT(domain
));
376 networksprint(const struct netent
*ne
)
378 char buf
[INET6_ADDRSTRLEN
];
379 struct in_addr ianet
;
382 ianet
= inet_makeaddr(ne
->n_net
, 0);
383 if (inet_ntop(ne
->n_addrtype
, &ianet
, buf
, sizeof(buf
)) == NULL
)
384 (void)strlcpy(buf
, "# unknown", sizeof(buf
));
385 printfmtstrings(ne
->n_aliases
, " ", " ", "%-16s %s", ne
->n_name
, buf
);
389 networks(int argc
, char *argv
[])
396 assert(argv
!= NULL
);
401 while ((ne
= getnetent()) != NULL
)
404 for (i
= 2; i
< argc
; i
++) {
405 net
= inet_network(argv
[i
]);
406 if (net
!= INADDR_NONE
)
407 ne
= getnetbyaddr(net
, AF_INET
);
409 ne
= getnetbyname(argv
[i
]);
428 passwd(int argc
, char *argv
[])
435 assert(argv
!= NULL
);
437 #define PASSWDPRINT (void)printf("%s:%s:%u:%u:%s:%s:%s\n", \
438 pw->pw_name, pw->pw_passwd, pw->pw_uid, \
439 pw->pw_gid, pw->pw_gecos, pw->pw_dir, pw->pw_shell)
444 while ((pw
= getpwent()) != NULL
)
447 for (i
= 2; i
< argc
; i
++) {
448 if (parsenum(argv
[i
], &id
))
449 pw
= getpwuid((uid_t
)id
);
451 pw
= getpwnam(argv
[i
]);
465 mygetent(const char * const * db_array
, const char *name
)
470 switch (error
= cgetent(&buf
, db_array
, name
)) {
472 warnx("tc= loop in record `%s' in `%s'", name
, db_array
[0]);
475 warn("system error fetching record `%s' in `%s'", name
,
482 warnx("tc= reference not found in record for `%s' in `%s'",
486 warnx("unknown error %d in record `%s' in `%s'", error
, name
,
494 mygetone(const char * const * db_array
, int first
)
499 switch (error
= (first
? cgetfirst
: cgetnext
)(&buf
, db_array
)) {
501 warnx("tc= loop in `%s'", db_array
[0]);
504 warn("system error fetching record in `%s'", db_array
[0]);
510 warnx("tc= reference not found in `%s'", db_array
[0]);
513 warnx("unknown error %d in `%s'", error
, db_array
[0]);
520 capprint(const char *cap
)
522 char *c
= strchr(cap
, ':');
525 (void)printf("true\n");
527 int l
= (int)(c
- cap
);
528 (void)printf("%*.*s\n", l
, l
, cap
);
531 (void)printf("%s\n", cap
);
544 if (len
<= TERMWIDTH
) {
551 for (s
= b
+ TERMWIDTH
; s
> b
&& *s
!= ':'; s
--)
567 handleone(const char * const *db_array
, char *b
, int recurse
, int pretty
,
578 if (!recurse
|| cgetstr(b
, "tc", &tc
) <= 0)
581 b
= mygetent(db_array
, tc
);
587 handleone(db_array
, b
, recurse
, pretty
, ++level
);
592 handlecap(const char *db
, int argc
, char *argv
[])
594 static const char sfx
[] = "=#:";
595 const char *db_array
[] = { db
, NULL
};
599 int expand
= 1, recurse
= 0, pretty
= 0;
602 assert(argv
!= NULL
);
606 while ((c
= getopt(argc
, argv
, "pnr")) != -1)
625 csetexpandtc(expand
);
628 for (b
= mygetone(db_array
, 1); b
; b
= mygetone(db_array
, 0)) {
629 handleone(db_array
, b
, recurse
, pretty
, 0);
633 if ((b
= mygetent(db_array
, argv
[0])) == NULL
)
636 handleone(db_array
, b
, recurse
, pretty
, 0);
638 for (i
= 2; i
< argc
; i
++) {
639 for (j
= 0; j
< sizeof(sfx
) - 1; j
++) {
640 cap
= cgetcap(b
, argv
[i
], sfx
[j
]);
646 if (j
== sizeof(sfx
) - 1)
660 gettytab(int argc
, char *argv
[])
662 return handlecap(_PATH_GETTYTAB
, argc
, argv
);
670 printcap(int argc
, char *argv
[])
672 return handlecap(_PATH_PRINTCAP
, argc
, argv
);
680 disktab(int argc
, char *argv
[])
682 return handlecap(_PATH_DISKTAB
, argc
, argv
);
690 termcap(int argc
, char *argv
[])
692 return handlecap(_PATH_TERMCAP
, 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
]);
739 rpc(int argc
, char *argv
[])
746 assert(argv
!= NULL
);
748 #define RPCPRINT printfmtstrings(re->r_aliases, " ", " ", \
750 re->r_name, re->r_number)
755 while ((re
= getrpcent()) != NULL
)
758 for (i
= 2; i
< argc
; i
++) {
759 if (parsenum(argv
[i
], &id
))
760 re
= getrpcbynumber((int)id
);
762 re
= getrpcbyname(argv
[i
]);
780 services(int argc
, char *argv
[])
788 assert(argv
!= NULL
);
790 #define SERVICESPRINT printfmtstrings(se->s_aliases, " ", " ", \
792 se->s_name, ntohs(se->s_port), se->s_proto)
797 while ((se
= getservent()) != NULL
)
800 for (i
= 2; i
< argc
; i
++) {
801 proto
= strchr(argv
[i
], '/');
804 if (parsenum(argv
[i
], &id
))
805 se
= getservbyport(htons(id
), proto
);
807 se
= getservbyname(argv
[i
], proto
);
826 shells(int argc
, char *argv
[])
832 assert(argv
!= NULL
);
834 #define SHELLSPRINT (void)printf("%s\n", sh)
839 while ((sh
= getusershell()) != NULL
)
842 for (i
= 2; i
< argc
; i
++) {
844 while ((sh
= getusershell()) != NULL
) {
845 if (strcmp(sh
, argv
[i
]) == 0) {