]>
git.cameronkatri.com Git - getent-darwin.git/blob - getent.c
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.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include <sys/socket.h>
43 #include <sys/param.h>
44 #include <arpa/inet.h>
45 #include <arpa/nameser.h>
47 #include <netinet/if_ether.h>
48 #include <netinet/in.h> /* for INET6_ADDRSTRLEN */
49 #include <rpc/rpcent.h>
65 static int usage(void);
66 static int parsenum(const char *, unsigned long *);
67 static int ethers(int, char *[]);
68 static int group(int, char *[]);
69 static int hosts(int, char *[]);
70 static int networks(int, char *[]);
71 static int passwd(int, char *[]);
72 static int protocols(int, char *[]);
73 static int rpc(int, char *[]);
74 static int services(int, char *[]);
75 static int shells(int, char *[]);
76 static int utmpx(int, char *[]);
85 static struct getentdb
{
87 int (*callback
)(int, char *[]);
89 { "ethers", ethers
, },
92 { "networks", networks
, },
93 { "passwd", passwd
, },
94 { "protocols", protocols
, },
96 { "services", services
, },
97 { "shells", shells
, },
104 main(int argc
, char *argv
[])
106 struct getentdb
*curdb
;
108 setprogname(argv
[0]);
112 for (curdb
= databases
; curdb
->name
!= NULL
; curdb
++) {
113 if (strcmp(curdb
->name
, argv
[1]) == 0) {
114 exit(curdb
->callback(argc
, argv
));
117 fprintf(stderr
, "Unknown database: %s\n", argv
[1]);
126 struct getentdb
*curdb
;
128 fprintf(stderr
, "Usage: %s database [key ...]\n",
130 fprintf(stderr
, " database may be one of:\n\t");
131 for (curdb
= databases
; curdb
->name
!= NULL
; curdb
++) {
132 fprintf(stderr
, " %s", curdb
->name
);
134 fprintf(stderr
, "\n");
140 parsenum(const char *word
, unsigned long *result
)
145 assert(word
!= NULL
);
146 assert(result
!= NULL
);
148 if (!isdigit((unsigned char)word
[0]))
151 num
= strtoul(word
, &ep
, 10);
152 if (num
== ULONG_MAX
&& errno
== ERANGE
)
162 * vprintf(format, ...),
163 * then the aliases (beginning with prefix, separated by sep),
167 printfmtstrings(char *strings
[], const char *prefix
, const char *sep
,
168 const char *fmt
, ...)
178 for (i
= 0; strings
[i
] != NULL
; i
++) {
179 printf("%s%s", curpref
, strings
[i
]);
190 ethers(int argc
, char *argv
[])
192 char hostname
[MAXHOSTNAMELEN
+ 1], *hp
;
193 struct ether_addr ea
, *eap
;
197 assert(argv
!= NULL
);
199 #define ETHERSPRINT printf("%-17s %s\n", ether_ntoa(eap), hp)
203 fprintf(stderr
, "Enumeration not supported on ethers\n");
206 for (i
= 2; i
< argc
; i
++) {
207 if ((eap
= ether_aton(argv
[i
])) == NULL
) {
210 if (ether_hostton(hp
, eap
) != 0) {
216 if (ether_ntohost(hp
, eap
) != 0) {
232 group(int argc
, char *argv
[])
239 assert(argv
!= NULL
);
241 #define GROUPPRINT printfmtstrings(gr->gr_mem, ":", ",", "%s:%s:%u", \
242 gr->gr_name, gr->gr_passwd, gr->gr_gid)
247 while ((gr
= getgrent()) != NULL
)
250 for (i
= 2; i
< argc
; i
++) {
251 if (parsenum(argv
[i
], &id
))
252 gr
= getgrgid((gid_t
)id
);
254 gr
= getgrnam(argv
[i
]);
273 hostsprint(const struct hostent
*he
)
275 char buf
[INET6_ADDRSTRLEN
];
278 if (inet_ntop(he
->h_addrtype
, he
->h_addr
, buf
, sizeof(buf
)) == NULL
)
279 strlcpy(buf
, "# unknown", sizeof(buf
));
280 printfmtstrings(he
->h_aliases
, " ", " ", "%-16s %s", buf
, he
->h_name
);
284 hosts(int argc
, char *argv
[])
287 char addr
[IN6ADDRSZ
];
291 assert(argv
!= NULL
);
296 while ((he
= gethostent()) != NULL
)
299 for (i
= 2; i
< argc
; i
++) {
300 if (inet_pton(AF_INET6
, argv
[i
], (void *)addr
) > 0)
301 he
= gethostbyaddr(addr
, IN6ADDRSZ
, AF_INET6
);
302 else if (inet_pton(AF_INET
, argv
[i
], (void *)addr
) > 0)
303 he
= gethostbyaddr(addr
, INADDRSZ
, AF_INET
);
305 he
= gethostbyname(argv
[i
]);
322 networksprint(const struct netent
*ne
)
324 char buf
[INET6_ADDRSTRLEN
];
325 struct in_addr ianet
;
328 ianet
= inet_makeaddr(ne
->n_net
, 0);
329 if (inet_ntop(ne
->n_addrtype
, &ianet
, buf
, sizeof(buf
)) == NULL
)
330 strlcpy(buf
, "# unknown", sizeof(buf
));
331 printfmtstrings(ne
->n_aliases
, " ", " ", "%-16s %s", ne
->n_name
, buf
);
335 networks(int argc
, char *argv
[])
342 assert(argv
!= NULL
);
347 while ((ne
= getnetent()) != NULL
)
350 for (i
= 2; i
< argc
; i
++) {
351 net
= inet_network(argv
[i
]);
352 if (net
!= INADDR_NONE
)
353 ne
= getnetbyaddr(net
, AF_INET
);
355 ne
= getnetbyname(argv
[i
]);
372 passwd(int argc
, char *argv
[])
379 assert(argv
!= NULL
);
381 #define PASSWDPRINT printf("%s:%s:%u:%u:%s:%s:%s\n", \
382 pw->pw_name, pw->pw_passwd, pw->pw_uid, \
383 pw->pw_gid, pw->pw_gecos, pw->pw_dir, pw->pw_shell)
388 while ((pw
= getpwent()) != NULL
)
391 for (i
= 2; i
< argc
; i
++) {
392 if (parsenum(argv
[i
], &id
))
393 pw
= getpwuid((uid_t
)id
);
395 pw
= getpwnam(argv
[i
]);
412 protocols(int argc
, char *argv
[])
419 assert(argv
!= NULL
);
421 #define PROTOCOLSPRINT printfmtstrings(pe->p_aliases, " ", " ", \
422 "%-16s %5d", pe->p_name, pe->p_proto)
427 while ((pe
= getprotoent()) != NULL
)
430 for (i
= 2; i
< argc
; i
++) {
431 if (parsenum(argv
[i
], &id
))
432 pe
= getprotobynumber((int)id
);
434 pe
= getprotobyname(argv
[i
]);
451 rpc(int argc
, char *argv
[])
458 assert(argv
!= NULL
);
460 #define RPCPRINT printfmtstrings(re->r_aliases, " ", " ", \
462 re->r_name, re->r_number)
467 while ((re
= getrpcent()) != NULL
)
470 for (i
= 2; i
< argc
; i
++) {
471 if (parsenum(argv
[i
], &id
))
472 re
= getrpcbynumber((int)id
);
474 re
= getrpcbyname(argv
[i
]);
491 services(int argc
, char *argv
[])
499 assert(argv
!= NULL
);
501 #define SERVICESPRINT printfmtstrings(se->s_aliases, " ", " ", \
503 se->s_name, ntohs(se->s_port), se->s_proto)
508 while ((se
= getservent()) != NULL
)
511 for (i
= 2; i
< argc
; i
++) {
512 proto
= strchr(argv
[i
], '/');
515 if (parsenum(argv
[i
], &id
))
516 se
= getservbyport(htons((u_short
)id
), proto
);
518 se
= getservbyname(argv
[i
], proto
);
535 shells(int argc
, char *argv
[])
541 assert(argv
!= NULL
);
543 #define SHELLSPRINT printf("%s\n", sh)
548 while ((sh
= getusershell()) != NULL
)
551 for (i
= 2; i
< argc
; i
++) {
553 while ((sh
= getusershell()) != NULL
) {
554 if (strcmp(sh
, argv
[i
]) == 0) {
573 #define UTMPXPRINTID do { \
575 for (i = 0; i < sizeof ut->ut_id; i++) \
576 printf("%02hhx", ut->ut_id[i]); \
580 utmpxprint(const struct utmpx
*ut
)
583 if (ut
->ut_type
== EMPTY
)
586 printf("[%.24s] ", ctime(&ut
->ut_tv
.tv_sec
));
588 switch (ut
->ut_type
) {
590 printf("system boot\n");
593 printf("system shutdown\n");
596 printf("old system time\n");
599 printf("new system time\n");
602 printf("user process: id=\"");
604 printf("\" pid=\"%d\" user=\"%s\" line=\"%s\" host=\"%s\"\n",
605 ut
->ut_pid
, ut
->ut_user
, ut
->ut_line
, ut
->ut_host
);
608 printf("dead process: id=\"");
610 printf("\" pid=\"%d\"\n", ut
->ut_pid
);
613 printf("unknown record type\n");
619 utmpx(int argc
, char *argv
[])
621 const struct utmpx
*ut
;
625 assert(argv
!= NULL
);
629 } else if (argc
== 3) {
630 if (strcmp(argv
[2], "active") == 0)
632 else if (strcmp(argv
[2], "lastlogin") == 0)
633 db
= UTXDB_LASTLOGIN
;
634 else if (strcmp(argv
[2], "log") == 0)
642 if (rv
== RV_USAGE
) {
643 fprintf(stderr
, "Usage: %s utmpx [active | lastlogin | log]\n",
645 } else if (rv
== RV_OK
) {
646 if (setutxdb(db
, NULL
) != 0)
647 return (RV_NOTFOUND
);
648 while ((ut
= getutxent()) != NULL
)