]>
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 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
6 * Copyright (c) 2004 The NetBSD Foundation, Inc.
9 * This code is derived from software contributed to The NetBSD Foundation
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/socket.h>
38 #include <sys/param.h>
39 #include <arpa/inet.h>
40 #include <arpa/nameser.h>
42 #include <netinet/if_ether.h>
43 #include <netinet/in.h> /* for INET6_ADDRSTRLEN */
60 #include <arpa/nameser_compat.h>
61 #include "utmpx-defines.h"
62 int setutxdb(int db
, const char *file
);
64 static int usage(void);
65 static int parsenum(const char *, unsigned long *);
66 static int ethers(int, char *[]);
67 static int group(int, char *[]);
68 static int hosts(int, char *[]);
69 static int netgroup(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 { "netgroup", netgroup
, },
93 { "networks", networks
, },
94 { "passwd", passwd
, },
95 { "protocols", protocols
, },
97 { "services", services
, },
98 { "shells", shells
, },
105 main(int argc
, char *argv
[])
107 struct getentdb
*curdb
;
109 setprogname(argv
[0]);
113 for (curdb
= databases
; curdb
->name
!= NULL
; curdb
++) {
114 if (strcmp(curdb
->name
, argv
[1]) == 0) {
115 exit(curdb
->callback(argc
, argv
));
118 fprintf(stderr
, "Unknown database: %s\n", argv
[1]);
127 struct getentdb
*curdb
;
129 fprintf(stderr
, "Usage: %s database [key ...]\n",
131 fprintf(stderr
, " database may be one of:\n\t");
132 for (curdb
= databases
; curdb
->name
!= NULL
; curdb
++) {
133 fprintf(stderr
, " %s", curdb
->name
);
135 fprintf(stderr
, "\n");
141 parsenum(const char *word
, unsigned long *result
)
146 assert(word
!= NULL
);
147 assert(result
!= NULL
);
149 if (!isdigit((unsigned char)word
[0]))
152 num
= strtoul(word
, &ep
, 10);
153 if (num
== ULONG_MAX
&& errno
== ERANGE
)
163 * vprintf(format, ...),
164 * then the aliases (beginning with prefix, separated by sep),
168 printfmtstrings(char *strings
[], const char *prefix
, const char *sep
,
169 const char *fmt
, ...)
179 for (i
= 0; strings
[i
] != NULL
; i
++) {
180 printf("%s%s", curpref
, strings
[i
]);
191 ethers(int argc
, char *argv
[])
193 char hostname
[MAXHOSTNAMELEN
+ 1], *hp
;
194 struct ether_addr ea
, *eap
;
198 assert(argv
!= NULL
);
200 #define ETHERSPRINT printf("%-17s %s\n", ether_ntoa(eap), hp)
204 fprintf(stderr
, "Enumeration not supported on ethers\n");
207 for (i
= 2; i
< argc
; i
++) {
208 if ((eap
= ether_aton(argv
[i
])) == NULL
) {
211 if (ether_hostton(hp
, eap
) != 0) {
217 if (ether_ntohost(hp
, eap
) != 0) {
233 group(int argc
, char *argv
[])
240 assert(argv
!= NULL
);
242 #define GROUPPRINT printfmtstrings(gr->gr_mem, ":", ",", "%s:%s:%u", \
243 gr->gr_name, gr->gr_passwd, gr->gr_gid)
248 while ((gr
= getgrent()) != NULL
)
251 for (i
= 2; i
< argc
; i
++) {
252 if (parsenum(argv
[i
], &id
))
253 gr
= getgrgid((gid_t
)id
);
255 gr
= getgrnam(argv
[i
]);
274 hostsprint(const struct hostent
*he
)
276 char buf
[INET6_ADDRSTRLEN
];
279 if (inet_ntop(he
->h_addrtype
, he
->h_addr
, buf
, sizeof(buf
)) == NULL
)
280 strlcpy(buf
, "# unknown", sizeof(buf
));
281 printfmtstrings(he
->h_aliases
, " ", " ", "%-16s %s", buf
, he
->h_name
);
285 hosts(int argc
, char *argv
[])
287 struct hostent
*he4
, *he6
;
288 char addr
[IN6ADDRSZ
];
292 assert(argv
!= NULL
);
298 while ((he4
= gethostent()) != NULL
)
301 for (i
= 2; i
< argc
; i
++) {
302 if (inet_pton(AF_INET6
, argv
[i
], (void *)addr
) > 0) {
303 he6
= gethostbyaddr(addr
, IN6ADDRSZ
, AF_INET6
);
306 } else if (inet_pton(AF_INET
, argv
[i
],
308 he4
= gethostbyaddr(addr
, INADDRSZ
, AF_INET
);
312 he6
= gethostbyname2(argv
[i
], AF_INET6
);
315 he4
= gethostbyname(argv
[i
]);
319 if ( he4
== NULL
&& he6
== NULL
) {
333 networksprint(const struct netent
*ne
)
335 char buf
[INET6_ADDRSTRLEN
];
336 struct in_addr ianet
;
339 ianet
= inet_makeaddr(ne
->n_net
, 0);
340 if (inet_ntop(ne
->n_addrtype
, &ianet
, buf
, sizeof(buf
)) == NULL
)
341 strlcpy(buf
, "# unknown", sizeof(buf
));
342 printfmtstrings(ne
->n_aliases
, " ", " ", "%-16s %s", ne
->n_name
, buf
);
346 networks(int argc
, char *argv
[])
353 assert(argv
!= NULL
);
358 while ((ne
= getnetent()) != NULL
)
361 for (i
= 2; i
< argc
; i
++) {
362 net
= inet_network(argv
[i
]);
363 if (net
!= INADDR_NONE
)
364 ne
= getnetbyaddr(net
, AF_INET
);
366 ne
= getnetbyname(argv
[i
]);
383 passwd(int argc
, char *argv
[])
390 assert(argv
!= NULL
);
392 #define PASSWDPRINT printf("%s:%s:%u:%u:%s:%s:%s\n", \
393 pw->pw_name, pw->pw_passwd, pw->pw_uid, \
394 pw->pw_gid, pw->pw_gecos, pw->pw_dir, pw->pw_shell)
399 while ((pw
= getpwent()) != NULL
)
402 for (i
= 2; i
< argc
; i
++) {
403 if (parsenum(argv
[i
], &id
))
404 pw
= getpwuid((uid_t
)id
);
406 pw
= getpwnam(argv
[i
]);
423 protocols(int argc
, char *argv
[])
430 assert(argv
!= NULL
);
432 #define PROTOCOLSPRINT printfmtstrings(pe->p_aliases, " ", " ", \
433 "%-16s %5d", pe->p_name, pe->p_proto)
438 while ((pe
= getprotoent()) != NULL
)
441 for (i
= 2; i
< argc
; i
++) {
442 if (parsenum(argv
[i
], &id
))
443 pe
= getprotobynumber((int)id
);
445 pe
= getprotobyname(argv
[i
]);
462 rpc(int argc
, char *argv
[])
469 assert(argv
!= NULL
);
471 #define RPCPRINT printfmtstrings(re->r_aliases, " ", " ", \
473 re->r_name, re->r_number)
478 while ((re
= getrpcent()) != NULL
)
481 for (i
= 2; i
< argc
; i
++) {
482 if (parsenum(argv
[i
], &id
))
483 re
= getrpcbynumber((int)id
);
485 re
= getrpcbyname(argv
[i
]);
502 services(int argc
, char *argv
[])
510 assert(argv
!= NULL
);
512 #define SERVICESPRINT printfmtstrings(se->s_aliases, " ", " ", \
514 se->s_name, ntohs(se->s_port), se->s_proto)
519 while ((se
= getservent()) != NULL
)
522 for (i
= 2; i
< argc
; i
++) {
523 proto
= strchr(argv
[i
], '/');
526 if (parsenum(argv
[i
], &id
))
527 se
= getservbyport(htons((u_short
)id
), proto
);
529 se
= getservbyname(argv
[i
], proto
);
546 shells(int argc
, char *argv
[])
552 assert(argv
!= NULL
);
554 #define SHELLSPRINT printf("%s\n", sh)
559 while ((sh
= getusershell()) != NULL
)
562 for (i
= 2; i
< argc
; i
++) {
564 while ((sh
= getusershell()) != NULL
) {
565 if (strcmp(sh
, argv
[i
]) == 0) {
584 netgroup(int argc
, char *argv
[])
586 char *host
, *user
, *domain
;
591 assert(argv
!= NULL
);
593 #define NETGROUPPRINT(s) (((s) != NULL) ? (s) : "")
597 fprintf(stderr
, "Enumeration not supported on netgroup\n");
600 for (i
= 2; i
< argc
; i
++) {
601 setnetgrent(argv
[i
]);
603 while (getnetgrent(&host
, &user
, &domain
) != 0) {
606 (void)fputs(argv
[i
], stdout
);
608 (void)printf(" (%s,%s,%s)",
611 NETGROUPPRINT(domain
));
625 #define UTMPXPRINTID do { \
627 for (i = 0; i < sizeof ut->ut_id; i++) \
628 printf("%02hhx", ut->ut_id[i]); \
632 utmpxprint(const struct utmpx
*ut
)
635 if (ut
->ut_type
== EMPTY
)
638 printf("[%jd.%06u -- %.24s] ",
639 (intmax_t)ut
->ut_tv
.tv_sec
, (unsigned int)ut
->ut_tv
.tv_usec
,
640 ctime(&ut
->ut_tv
.tv_sec
));
642 switch (ut
->ut_type
) {
644 printf("system boot\n");
647 printf("system shutdown\n");
650 printf("old system time\n");
653 printf("new system time\n");
656 printf("user process: id=\"");
658 printf("\" pid=\"%d\" user=\"%s\" line=\"%s\" host=\"%s\"\n",
659 ut
->ut_pid
, ut
->ut_user
, ut
->ut_line
, ut
->ut_host
);
662 printf("init process: id=\"");
664 printf("\" pid=\"%d\"\n", ut
->ut_pid
);
667 printf("login process: id=\"");
669 printf("\" pid=\"%d\" user=\"%s\" line=\"%s\" host=\"%s\"\n",
670 ut
->ut_pid
, ut
->ut_user
, ut
->ut_line
, ut
->ut_host
);
673 printf("dead process: id=\"");
675 printf("\" pid=\"%d\"\n", ut
->ut_pid
);
678 printf("unknown record type %hu\n", ut
->ut_type
);
684 utmpx(int argc
, char *argv
[])
686 const struct utmpx
*ut
;
687 const char *file
= NULL
;
688 int rv
= RV_OK
, db
= 0;
691 assert(argv
!= NULL
);
693 if (argc
== 3 || argc
== 4) {
694 if (strcmp(argv
[2], "active") == 0)
696 else if (strcmp(argv
[2], "lastlogin") == 0)
697 db
= UTXDB_LASTLOGIN
;
698 else if (strcmp(argv
[2], "log") == 0)
708 if (rv
== RV_USAGE
) {
710 "Usage: %s utmpx active | lastlogin | log [filename]\n",
712 } else if (rv
== RV_OK
) {
713 if (setutxdb(db
, file
) != 0)
714 return (RV_NOTFOUND
);
715 while ((ut
= getutxent()) != NULL
)