2 * Copyright (c) 2004 The NetBSD Foundation, Inc.
5 * This code is derived from software contributed to The NetBSD Foundation
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the NetBSD
19 * Foundation, Inc. and its contributors.
20 * 4. Neither the name of The NetBSD Foundation nor the names of its
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
25 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
28 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
36 * $NetBSD: getent.c,v 1.7 2005/08/24 14:31:02 ginsbach Exp $
37 * $DragonFly: src/usr.bin/getent/getent.c,v 1.1 2007/12/04 18:13:09 dillon Exp $
40 #include <sys/socket.h>
41 #include <sys/param.h>
42 #include <arpa/inet.h>
43 #include <arpa/nameser.h>
45 #include <netinet/if_ether.h>
46 #include <netinet/in.h> /* for INET6_ADDRSTRLEN */
62 static int usage(void);
63 static int parsenum(const char *, unsigned long *);
64 static void printfmtstrings(char **, const char *, const char *,
65 const char *fmt
, ...) __printflike(4, 5);
66 static int ethers(int, char *[]);
67 static int group(int, char *[]);
68 static int hosts(int, char *[]);
69 static int networks(int, char *[]);
70 static int passwd(int, char *[]);
71 static int protocols(int, char *[]);
72 static int rpc(int, char *[]);
73 static int services(int, char *[]);
74 static int shells(int, char *[]);
83 static struct getentdb
{
85 int (*callback
)(int, char *[]);
87 { "ethers", ethers
, },
90 { "networks", networks
, },
91 { "passwd", passwd
, },
92 { "protocols", protocols
, },
94 { "services", services
, },
95 { "shells", shells
, },
101 main(int argc
, char *argv
[])
103 struct getentdb
*curdb
;
105 setprogname(argv
[0]);
109 for (curdb
= databases
; curdb
->name
!= NULL
; curdb
++) {
110 if (strcmp(curdb
->name
, argv
[1]) == 0) {
111 exit(curdb
->callback(argc
, argv
));
114 fprintf(stderr
, "Unknown database: %s\n", argv
[1]);
123 struct getentdb
*curdb
;
125 fprintf(stderr
, "Usage: %s database [key ...]\n",
127 fprintf(stderr
, " database may be one of:\n\t");
128 for (curdb
= databases
; curdb
->name
!= NULL
; curdb
++) {
129 fprintf(stderr
, " %s", curdb
->name
);
131 fprintf(stderr
, "\n");
137 parsenum(const char *word
, unsigned long *result
)
142 assert(word
!= NULL
);
143 assert(result
!= NULL
);
145 if (!isdigit((unsigned char)word
[0]))
148 num
= strtoul(word
, &ep
, 10);
149 if (num
== ULONG_MAX
&& errno
== ERANGE
)
159 * vprintf(format, ...),
160 * then the aliases (beginning with prefix, separated by sep),
164 printfmtstrings(char *strings
[], const char *prefix
, const char *sep
,
165 const char *fmt
, ...)
175 for (i
= 0; strings
[i
] != NULL
; i
++) {
176 printf("%s%s", curpref
, strings
[i
]);
187 ethers(int argc
, char *argv
[])
189 char hostname
[MAXHOSTNAMELEN
+ 1], *hp
;
190 struct ether_addr ea
, *eap
;
194 assert(argv
!= NULL
);
196 #define ETHERSPRINT printf("%-17s %s\n", ether_ntoa(eap), hp)
200 fprintf(stderr
, "Enumeration not supported on ethers\n");
203 for (i
= 2; i
< argc
; i
++) {
204 if ((eap
= ether_aton(argv
[i
])) == NULL
) {
207 if (ether_hostton(hp
, eap
) != 0) {
213 if (ether_ntohost(hp
, eap
) != 0) {
229 group(int argc
, char *argv
[])
236 assert(argv
!= NULL
);
238 #define GROUPPRINT printfmtstrings(gr->gr_mem, ":", ",", "%s:%s:%u", \
239 gr->gr_name, gr->gr_passwd, gr->gr_gid)
244 while ((gr
= getgrent()) != NULL
)
247 for (i
= 2; i
< argc
; i
++) {
248 if (parsenum(argv
[i
], &id
))
249 gr
= getgrgid((gid_t
)id
);
251 gr
= getgrnam(argv
[i
]);
270 hostsprint(const struct hostent
*he
)
272 char buf
[INET6_ADDRSTRLEN
];
275 if (inet_ntop(he
->h_addrtype
, he
->h_addr
, buf
, sizeof(buf
)) == NULL
)
276 strlcpy(buf
, "# unknown", sizeof(buf
));
277 printfmtstrings(he
->h_aliases
, " ", " ", "%-16s %s", buf
, he
->h_name
);
281 hosts(int argc
, char *argv
[])
284 char addr
[IN6ADDRSZ
];
288 assert(argv
!= NULL
);
293 while ((he
= gethostent()) != NULL
)
296 for (i
= 2; i
< argc
; i
++) {
297 if (inet_pton(AF_INET6
, argv
[i
], (void *)addr
) > 0)
298 he
= gethostbyaddr(addr
, IN6ADDRSZ
, AF_INET6
);
299 else if (inet_pton(AF_INET
, argv
[i
], (void *)addr
) > 0)
300 he
= gethostbyaddr(addr
, INADDRSZ
, AF_INET
);
302 he
= gethostbyname(argv
[i
]);
319 networksprint(const struct netent
*ne
)
321 char buf
[INET6_ADDRSTRLEN
];
322 struct in_addr ianet
;
325 ianet
= inet_makeaddr(ne
->n_net
, 0);
326 if (inet_ntop(ne
->n_addrtype
, &ianet
, buf
, sizeof(buf
)) == NULL
)
327 strlcpy(buf
, "# unknown", sizeof(buf
));
328 printfmtstrings(ne
->n_aliases
, " ", " ", "%-16s %s", ne
->n_name
, buf
);
332 networks(int argc
, char *argv
[])
339 assert(argv
!= NULL
);
344 while ((ne
= getnetent()) != NULL
)
347 for (i
= 2; i
< argc
; i
++) {
348 net
= inet_network(argv
[i
]);
349 if (net
!= INADDR_NONE
)
350 ne
= getnetbyaddr(net
, AF_INET
);
352 ne
= getnetbyname(argv
[i
]);
369 passwd(int argc
, char *argv
[])
376 assert(argv
!= NULL
);
378 #define PASSWDPRINT printf("%s:%s:%u:%u:%s:%s:%s\n", \
379 pw->pw_name, pw->pw_passwd, pw->pw_uid, \
380 pw->pw_gid, pw->pw_gecos, pw->pw_dir, pw->pw_shell)
385 while ((pw
= getpwent()) != NULL
)
388 for (i
= 2; i
< argc
; i
++) {
389 if (parsenum(argv
[i
], &id
))
390 pw
= getpwuid((uid_t
)id
);
392 pw
= getpwnam(argv
[i
]);
409 protocols(int argc
, char *argv
[])
416 assert(argv
!= NULL
);
418 #define PROTOCOLSPRINT printfmtstrings(pe->p_aliases, " ", " ", \
419 "%-16s %5d", pe->p_name, pe->p_proto)
424 while ((pe
= getprotoent()) != NULL
)
427 for (i
= 2; i
< argc
; i
++) {
428 if (parsenum(argv
[i
], &id
))
429 pe
= getprotobynumber((int)id
);
431 pe
= getprotobyname(argv
[i
]);
448 rpc(int argc
, char *argv
[])
455 assert(argv
!= NULL
);
457 #define RPCPRINT printfmtstrings(re->r_aliases, " ", " ", \
459 re->r_name, re->r_number)
464 while ((re
= getrpcent()) != NULL
)
467 for (i
= 2; i
< argc
; i
++) {
468 if (parsenum(argv
[i
], &id
))
469 re
= getrpcbynumber((int)id
);
471 re
= getrpcbyname(argv
[i
]);
488 services(int argc
, char *argv
[])
496 assert(argv
!= NULL
);
498 #define SERVICESPRINT printfmtstrings(se->s_aliases, " ", " ", \
500 se->s_name, ntohs(se->s_port), se->s_proto)
505 while ((se
= getservent()) != NULL
)
508 for (i
= 2; i
< argc
; i
++) {
509 proto
= strchr(argv
[i
], '/');
512 if (parsenum(argv
[i
], &id
))
513 se
= getservbyport(htons((u_short
)id
), proto
);
515 se
= getservbyname(argv
[i
], proto
);
532 shells(int argc
, char *argv
[])
538 assert(argv
!= NULL
);
540 #define SHELLSPRINT printf("%s\n", sh)
545 while ((sh
= getusershell()) != NULL
)
548 for (i
= 2; i
< argc
; i
++) {
550 while ((sh
= getusershell()) != NULL
) {
551 if (strcmp(sh
, argv
[i
]) == 0) {