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 int ethers(int, char *[]);
65 static int group(int, char *[]);
66 static int hosts(int, char *[]);
67 static int networks(int, char *[]);
68 static int passwd(int, char *[]);
69 static int protocols(int, char *[]);
70 static int rpc(int, char *[]);
71 static int services(int, char *[]);
72 static int shells(int, char *[]);
81 static struct getentdb
{
83 int (*callback
)(int, char *[]);
85 { "ethers", ethers
, },
88 { "networks", networks
, },
89 { "passwd", passwd
, },
90 { "protocols", protocols
, },
92 { "services", services
, },
93 { "shells", shells
, },
99 main(int argc
, char *argv
[])
101 struct getentdb
*curdb
;
103 setprogname(argv
[0]);
107 for (curdb
= databases
; curdb
->name
!= NULL
; curdb
++) {
108 if (strcmp(curdb
->name
, argv
[1]) == 0) {
109 exit(curdb
->callback(argc
, argv
));
112 fprintf(stderr
, "Unknown database: %s\n", argv
[1]);
121 struct getentdb
*curdb
;
123 fprintf(stderr
, "Usage: %s database [key ...]\n",
125 fprintf(stderr
, " database may be one of:\n\t");
126 for (curdb
= databases
; curdb
->name
!= NULL
; curdb
++) {
127 fprintf(stderr
, " %s", curdb
->name
);
129 fprintf(stderr
, "\n");
135 parsenum(const char *word
, unsigned long *result
)
140 assert(word
!= NULL
);
141 assert(result
!= NULL
);
143 if (!isdigit((unsigned char)word
[0]))
146 num
= strtoul(word
, &ep
, 10);
147 if (num
== ULONG_MAX
&& errno
== ERANGE
)
157 * vprintf(format, ...),
158 * then the aliases (beginning with prefix, separated by sep),
162 printfmtstrings(char *strings
[], const char *prefix
, const char *sep
,
163 const char *fmt
, ...)
173 for (i
= 0; strings
[i
] != NULL
; i
++) {
174 printf("%s%s", curpref
, strings
[i
]);
185 ethers(int argc
, char *argv
[])
187 char hostname
[MAXHOSTNAMELEN
+ 1], *hp
;
188 struct ether_addr ea
, *eap
;
192 assert(argv
!= NULL
);
194 #define ETHERSPRINT printf("%-17s %s\n", ether_ntoa(eap), hp)
198 fprintf(stderr
, "Enumeration not supported on ethers\n");
201 for (i
= 2; i
< argc
; i
++) {
202 if ((eap
= ether_aton(argv
[i
])) == NULL
) {
205 if (ether_hostton(hp
, eap
) != 0) {
211 if (ether_ntohost(hp
, eap
) != 0) {
227 group(int argc
, char *argv
[])
234 assert(argv
!= NULL
);
236 #define GROUPPRINT printfmtstrings(gr->gr_mem, ":", ",", "%s:%s:%u", \
237 gr->gr_name, gr->gr_passwd, gr->gr_gid)
242 while ((gr
= getgrent()) != NULL
)
245 for (i
= 2; i
< argc
; i
++) {
246 if (parsenum(argv
[i
], &id
))
247 gr
= getgrgid((gid_t
)id
);
249 gr
= getgrnam(argv
[i
]);
268 hostsprint(const struct hostent
*he
)
270 char buf
[INET6_ADDRSTRLEN
];
273 if (inet_ntop(he
->h_addrtype
, he
->h_addr
, buf
, sizeof(buf
)) == NULL
)
274 strlcpy(buf
, "# unknown", sizeof(buf
));
275 printfmtstrings(he
->h_aliases
, " ", " ", "%-16s %s", buf
, he
->h_name
);
279 hosts(int argc
, char *argv
[])
282 char addr
[IN6ADDRSZ
];
286 assert(argv
!= NULL
);
291 while ((he
= gethostent()) != NULL
)
294 for (i
= 2; i
< argc
; i
++) {
295 if (inet_pton(AF_INET6
, argv
[i
], (void *)addr
) > 0)
296 he
= gethostbyaddr(addr
, IN6ADDRSZ
, AF_INET6
);
297 else if (inet_pton(AF_INET
, argv
[i
], (void *)addr
) > 0)
298 he
= gethostbyaddr(addr
, INADDRSZ
, AF_INET
);
300 he
= gethostbyname(argv
[i
]);
317 networksprint(const struct netent
*ne
)
319 char buf
[INET6_ADDRSTRLEN
];
320 struct in_addr ianet
;
323 ianet
= inet_makeaddr(ne
->n_net
, 0);
324 if (inet_ntop(ne
->n_addrtype
, &ianet
, buf
, sizeof(buf
)) == NULL
)
325 strlcpy(buf
, "# unknown", sizeof(buf
));
326 printfmtstrings(ne
->n_aliases
, " ", " ", "%-16s %s", ne
->n_name
, buf
);
330 networks(int argc
, char *argv
[])
337 assert(argv
!= NULL
);
342 while ((ne
= getnetent()) != NULL
)
345 for (i
= 2; i
< argc
; i
++) {
346 net
= inet_network(argv
[i
]);
347 if (net
!= INADDR_NONE
)
348 ne
= getnetbyaddr(net
, AF_INET
);
350 ne
= getnetbyname(argv
[i
]);
367 passwd(int argc
, char *argv
[])
374 assert(argv
!= NULL
);
376 #define PASSWDPRINT printf("%s:%s:%u:%u:%s:%s:%s\n", \
377 pw->pw_name, pw->pw_passwd, pw->pw_uid, \
378 pw->pw_gid, pw->pw_gecos, pw->pw_dir, pw->pw_shell)
383 while ((pw
= getpwent()) != NULL
)
386 for (i
= 2; i
< argc
; i
++) {
387 if (parsenum(argv
[i
], &id
))
388 pw
= getpwuid((uid_t
)id
);
390 pw
= getpwnam(argv
[i
]);
407 protocols(int argc
, char *argv
[])
414 assert(argv
!= NULL
);
416 #define PROTOCOLSPRINT printfmtstrings(pe->p_aliases, " ", " ", \
417 "%-16s %5d", pe->p_name, pe->p_proto)
422 while ((pe
= getprotoent()) != NULL
)
425 for (i
= 2; i
< argc
; i
++) {
426 if (parsenum(argv
[i
], &id
))
427 pe
= getprotobynumber((int)id
);
429 pe
= getprotobyname(argv
[i
]);
446 rpc(int argc
, char *argv
[])
453 assert(argv
!= NULL
);
455 #define RPCPRINT printfmtstrings(re->r_aliases, " ", " ", \
457 re->r_name, re->r_number)
462 while ((re
= getrpcent()) != NULL
)
465 for (i
= 2; i
< argc
; i
++) {
466 if (parsenum(argv
[i
], &id
))
467 re
= getrpcbynumber((int)id
);
469 re
= getrpcbyname(argv
[i
]);
486 services(int argc
, char *argv
[])
494 assert(argv
!= NULL
);
496 #define SERVICESPRINT printfmtstrings(se->s_aliases, " ", " ", \
498 se->s_name, ntohs(se->s_port), se->s_proto)
503 while ((se
= getservent()) != NULL
)
506 for (i
= 2; i
< argc
; i
++) {
507 proto
= strchr(argv
[i
], '/');
510 if (parsenum(argv
[i
], &id
))
511 se
= getservbyport(htons((u_short
)id
), proto
);
513 se
= getservbyname(argv
[i
], proto
);
530 shells(int argc
, char *argv
[])
536 assert(argv
!= NULL
);
538 #define SHELLSPRINT printf("%s\n", sh)
543 while ((sh
= getusershell()) != NULL
)
546 for (i
= 2; i
< argc
; i
++) {
548 while ((sh
= getusershell()) != NULL
) {
549 if (strcmp(sh
, argv
[i
]) == 0) {