]>
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.
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>
33 __FBSDID("$FreeBSD$");
35 #include <sys/socket.h>
36 #include <sys/param.h>
37 #include <arpa/inet.h>
38 #include <arpa/nameser.h>
40 #include <netinet/if_ether.h>
41 #include <netinet/in.h> /* for INET6_ADDRSTRLEN */
42 #include <rpc/rpcent.h>
59 static int usage(void);
60 static int parsenum(const char *, unsigned long *);
61 static int ethers(int, char *[]);
62 static int group(int, char *[]);
63 static int hosts(int, char *[]);
64 static int networks(int, char *[]);
65 static int passwd(int, char *[]);
66 static int protocols(int, char *[]);
67 static int rpc(int, char *[]);
68 static int services(int, char *[]);
69 static int shells(int, char *[]);
70 static int utmpx(int, char *[]);
79 static struct getentdb
{
81 int (*callback
)(int, char *[]);
83 { "ethers", ethers
, },
86 { "networks", networks
, },
87 { "passwd", passwd
, },
88 { "protocols", protocols
, },
90 { "services", services
, },
91 { "shells", shells
, },
98 main(int argc
, char *argv
[])
100 struct getentdb
*curdb
;
102 setprogname(argv
[0]);
106 for (curdb
= databases
; curdb
->name
!= NULL
; curdb
++) {
107 if (strcmp(curdb
->name
, argv
[1]) == 0) {
108 exit(curdb
->callback(argc
, argv
));
111 fprintf(stderr
, "Unknown database: %s\n", argv
[1]);
120 struct getentdb
*curdb
;
122 fprintf(stderr
, "Usage: %s database [key ...]\n",
124 fprintf(stderr
, " database may be one of:\n\t");
125 for (curdb
= databases
; curdb
->name
!= NULL
; curdb
++) {
126 fprintf(stderr
, " %s", curdb
->name
);
128 fprintf(stderr
, "\n");
134 parsenum(const char *word
, unsigned long *result
)
139 assert(word
!= NULL
);
140 assert(result
!= NULL
);
142 if (!isdigit((unsigned char)word
[0]))
145 num
= strtoul(word
, &ep
, 10);
146 if (num
== ULONG_MAX
&& errno
== ERANGE
)
156 * vprintf(format, ...),
157 * then the aliases (beginning with prefix, separated by sep),
161 printfmtstrings(char *strings
[], const char *prefix
, const char *sep
,
162 const char *fmt
, ...)
172 for (i
= 0; strings
[i
] != NULL
; i
++) {
173 printf("%s%s", curpref
, strings
[i
]);
184 ethers(int argc
, char *argv
[])
186 char hostname
[MAXHOSTNAMELEN
+ 1], *hp
;
187 struct ether_addr ea
, *eap
;
191 assert(argv
!= NULL
);
193 #define ETHERSPRINT printf("%-17s %s\n", ether_ntoa(eap), hp)
197 fprintf(stderr
, "Enumeration not supported on ethers\n");
200 for (i
= 2; i
< argc
; i
++) {
201 if ((eap
= ether_aton(argv
[i
])) == NULL
) {
204 if (ether_hostton(hp
, eap
) != 0) {
210 if (ether_ntohost(hp
, eap
) != 0) {
226 group(int argc
, char *argv
[])
233 assert(argv
!= NULL
);
235 #define GROUPPRINT printfmtstrings(gr->gr_mem, ":", ",", "%s:%s:%u", \
236 gr->gr_name, gr->gr_passwd, gr->gr_gid)
241 while ((gr
= getgrent()) != NULL
)
244 for (i
= 2; i
< argc
; i
++) {
245 if (parsenum(argv
[i
], &id
))
246 gr
= getgrgid((gid_t
)id
);
248 gr
= getgrnam(argv
[i
]);
267 hostsprint(const struct hostent
*he
)
269 char buf
[INET6_ADDRSTRLEN
];
272 if (inet_ntop(he
->h_addrtype
, he
->h_addr
, buf
, sizeof(buf
)) == NULL
)
273 strlcpy(buf
, "# unknown", sizeof(buf
));
274 printfmtstrings(he
->h_aliases
, " ", " ", "%-16s %s", buf
, he
->h_name
);
278 hosts(int argc
, char *argv
[])
280 struct hostent
*he4
, *he6
;
281 char addr
[IN6ADDRSZ
];
285 assert(argv
!= NULL
);
291 while ((he4
= gethostent()) != NULL
)
294 for (i
= 2; i
< argc
; i
++) {
295 if (inet_pton(AF_INET6
, argv
[i
], (void *)addr
) > 0) {
296 he6
= gethostbyaddr(addr
, IN6ADDRSZ
, AF_INET6
);
299 } else if (inet_pton(AF_INET
, argv
[i
],
301 he4
= gethostbyaddr(addr
, INADDRSZ
, AF_INET
);
305 he6
= gethostbyname2(argv
[i
], AF_INET6
);
308 he4
= gethostbyname(argv
[i
]);
312 if ( he4
== NULL
&& he6
== NULL
) {
326 networksprint(const struct netent
*ne
)
328 char buf
[INET6_ADDRSTRLEN
];
329 struct in_addr ianet
;
332 ianet
= inet_makeaddr(ne
->n_net
, 0);
333 if (inet_ntop(ne
->n_addrtype
, &ianet
, buf
, sizeof(buf
)) == NULL
)
334 strlcpy(buf
, "# unknown", sizeof(buf
));
335 printfmtstrings(ne
->n_aliases
, " ", " ", "%-16s %s", ne
->n_name
, buf
);
339 networks(int argc
, char *argv
[])
346 assert(argv
!= NULL
);
351 while ((ne
= getnetent()) != NULL
)
354 for (i
= 2; i
< argc
; i
++) {
355 net
= inet_network(argv
[i
]);
356 if (net
!= INADDR_NONE
)
357 ne
= getnetbyaddr(net
, AF_INET
);
359 ne
= getnetbyname(argv
[i
]);
376 passwd(int argc
, char *argv
[])
383 assert(argv
!= NULL
);
385 #define PASSWDPRINT printf("%s:%s:%u:%u:%s:%s:%s\n", \
386 pw->pw_name, pw->pw_passwd, pw->pw_uid, \
387 pw->pw_gid, pw->pw_gecos, pw->pw_dir, pw->pw_shell)
392 while ((pw
= getpwent()) != NULL
)
395 for (i
= 2; i
< argc
; i
++) {
396 if (parsenum(argv
[i
], &id
))
397 pw
= getpwuid((uid_t
)id
);
399 pw
= getpwnam(argv
[i
]);
416 protocols(int argc
, char *argv
[])
423 assert(argv
!= NULL
);
425 #define PROTOCOLSPRINT printfmtstrings(pe->p_aliases, " ", " ", \
426 "%-16s %5d", pe->p_name, pe->p_proto)
431 while ((pe
= getprotoent()) != NULL
)
434 for (i
= 2; i
< argc
; i
++) {
435 if (parsenum(argv
[i
], &id
))
436 pe
= getprotobynumber((int)id
);
438 pe
= getprotobyname(argv
[i
]);
455 rpc(int argc
, char *argv
[])
462 assert(argv
!= NULL
);
464 #define RPCPRINT printfmtstrings(re->r_aliases, " ", " ", \
466 re->r_name, re->r_number)
471 while ((re
= getrpcent()) != NULL
)
474 for (i
= 2; i
< argc
; i
++) {
475 if (parsenum(argv
[i
], &id
))
476 re
= getrpcbynumber((int)id
);
478 re
= getrpcbyname(argv
[i
]);
495 services(int argc
, char *argv
[])
503 assert(argv
!= NULL
);
505 #define SERVICESPRINT printfmtstrings(se->s_aliases, " ", " ", \
507 se->s_name, ntohs(se->s_port), se->s_proto)
512 while ((se
= getservent()) != NULL
)
515 for (i
= 2; i
< argc
; i
++) {
516 proto
= strchr(argv
[i
], '/');
519 if (parsenum(argv
[i
], &id
))
520 se
= getservbyport(htons((u_short
)id
), proto
);
522 se
= getservbyname(argv
[i
], proto
);
539 shells(int argc
, char *argv
[])
545 assert(argv
!= NULL
);
547 #define SHELLSPRINT printf("%s\n", sh)
552 while ((sh
= getusershell()) != NULL
)
555 for (i
= 2; i
< argc
; i
++) {
557 while ((sh
= getusershell()) != NULL
) {
558 if (strcmp(sh
, argv
[i
]) == 0) {
577 #define UTMPXPRINTID do { \
579 for (i = 0; i < sizeof ut->ut_id; i++) \
580 printf("%02hhx", ut->ut_id[i]); \
584 utmpxprint(const struct utmpx
*ut
)
587 if (ut
->ut_type
== EMPTY
)
590 printf("[%jd.%06u -- %.24s] ",
591 (intmax_t)ut
->ut_tv
.tv_sec
, (unsigned int)ut
->ut_tv
.tv_usec
,
592 ctime(&ut
->ut_tv
.tv_sec
));
594 switch (ut
->ut_type
) {
596 printf("system boot\n");
599 printf("system shutdown\n");
602 printf("old system time\n");
605 printf("new system time\n");
608 printf("user process: id=\"");
610 printf("\" pid=\"%d\" user=\"%s\" line=\"%s\" host=\"%s\"\n",
611 ut
->ut_pid
, ut
->ut_user
, ut
->ut_line
, ut
->ut_host
);
614 printf("init process: id=\"");
616 printf("\" pid=\"%d\"\n", ut
->ut_pid
);
619 printf("login process: id=\"");
621 printf("\" pid=\"%d\" user=\"%s\" line=\"%s\" host=\"%s\"\n",
622 ut
->ut_pid
, ut
->ut_user
, ut
->ut_line
, ut
->ut_host
);
625 printf("dead process: id=\"");
627 printf("\" pid=\"%d\"\n", ut
->ut_pid
);
630 printf("unknown record type %hu\n", ut
->ut_type
);
636 utmpx(int argc
, char *argv
[])
638 const struct utmpx
*ut
;
639 const char *file
= NULL
;
640 int rv
= RV_OK
, db
= 0;
643 assert(argv
!= NULL
);
645 if (argc
== 3 || argc
== 4) {
646 if (strcmp(argv
[2], "active") == 0)
648 else if (strcmp(argv
[2], "lastlogin") == 0)
649 db
= UTXDB_LASTLOGIN
;
650 else if (strcmp(argv
[2], "log") == 0)
660 if (rv
== RV_USAGE
) {
662 "Usage: %s utmpx active | lastlogin | log [filename]\n",
664 } else if (rv
== RV_OK
) {
665 if (setutxdb(db
, file
) != 0)
666 return (RV_NOTFOUND
);
667 while ((ut
= getutxent()) != NULL
)