]> git.cameronkatri.com Git - getent-darwin.git/blob - getent.c
Makefile and reorganizing
[getent-darwin.git] / getent.c
1 /* $NetBSD: getent.c,v 1.7 2005/08/24 14:31:02 ginsbach Exp $ */
2
3 /*-
4 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 *
6 * Copyright (c) 2004 The NetBSD Foundation, Inc.
7 * All rights reserved.
8 *
9 * This code is derived from software contributed to The NetBSD Foundation
10 * by Luke Mewburn.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
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.
20 *
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.
32 */
33
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36
37 #include <sys/socket.h>
38 #include <sys/param.h>
39 #include <arpa/inet.h>
40 #include <arpa/nameser.h>
41 #include <net/if.h>
42 #include <netinet/if_ether.h>
43 #include <netinet/in.h> /* for INET6_ADDRSTRLEN */
44
45 #include <assert.h>
46 #include <ctype.h>
47 #include <errno.h>
48 #include <grp.h>
49 #include <limits.h>
50 #include <netdb.h>
51 #include <pwd.h>
52 #include <stdarg.h>
53 #include <stdint.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <unistd.h>
58 #include <utmpx.h>
59
60 #include <arpa/nameser_compat.h>
61 #include "utmpx-defines.h"
62 int setutxdb(int db, const char *file);
63
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 *[]);
77
78 enum {
79 RV_OK = 0,
80 RV_USAGE = 1,
81 RV_NOTFOUND = 2,
82 RV_NOENUM = 3
83 };
84
85 static struct getentdb {
86 const char *name;
87 int (*callback)(int, char *[]);
88 } databases[] = {
89 { "ethers", ethers, },
90 { "group", group, },
91 { "hosts", hosts, },
92 { "netgroup", netgroup, },
93 { "networks", networks, },
94 { "passwd", passwd, },
95 { "protocols", protocols, },
96 { "rpc", rpc, },
97 { "services", services, },
98 { "shells", shells, },
99 { "utmpx", utmpx, },
100
101 { NULL, NULL, },
102 };
103
104 int
105 main(int argc, char *argv[])
106 {
107 struct getentdb *curdb;
108
109 setprogname(argv[0]);
110
111 if (argc < 2)
112 usage();
113 for (curdb = databases; curdb->name != NULL; curdb++) {
114 if (strcmp(curdb->name, argv[1]) == 0) {
115 exit(curdb->callback(argc, argv));
116 }
117 }
118 fprintf(stderr, "Unknown database: %s\n", argv[1]);
119 usage();
120 /* NOTREACHED */
121 return RV_USAGE;
122 }
123
124 static int
125 usage(void)
126 {
127 struct getentdb *curdb;
128
129 fprintf(stderr, "Usage: %s database [key ...]\n",
130 getprogname());
131 fprintf(stderr, " database may be one of:\n\t");
132 for (curdb = databases; curdb->name != NULL; curdb++) {
133 fprintf(stderr, " %s", curdb->name);
134 }
135 fprintf(stderr, "\n");
136 exit(RV_USAGE);
137 /* NOTREACHED */
138 }
139
140 static int
141 parsenum(const char *word, unsigned long *result)
142 {
143 unsigned long num;
144 char *ep;
145
146 assert(word != NULL);
147 assert(result != NULL);
148
149 if (!isdigit((unsigned char)word[0]))
150 return 0;
151 errno = 0;
152 num = strtoul(word, &ep, 10);
153 if (num == ULONG_MAX && errno == ERANGE)
154 return 0;
155 if (*ep != '\0')
156 return 0;
157 *result = num;
158 return 1;
159 }
160
161 /*
162 * printfmtstrings --
163 * vprintf(format, ...),
164 * then the aliases (beginning with prefix, separated by sep),
165 * then a newline
166 */
167 static void
168 printfmtstrings(char *strings[], const char *prefix, const char *sep,
169 const char *fmt, ...)
170 {
171 va_list ap;
172 const char *curpref;
173 int i;
174
175 va_start(ap, fmt);
176 vprintf(fmt, ap);
177
178 curpref = prefix;
179 for (i = 0; strings[i] != NULL; i++) {
180 printf("%s%s", curpref, strings[i]);
181 curpref = sep;
182 }
183 printf("\n");
184 va_end(ap);
185 }
186
187 /*
188 * ethers
189 */
190 static int
191 ethers(int argc, char *argv[])
192 {
193 char hostname[MAXHOSTNAMELEN + 1], *hp;
194 struct ether_addr ea, *eap;
195 int i, rv;
196
197 assert(argc > 1);
198 assert(argv != NULL);
199
200 #define ETHERSPRINT printf("%-17s %s\n", ether_ntoa(eap), hp)
201
202 rv = RV_OK;
203 if (argc == 2) {
204 fprintf(stderr, "Enumeration not supported on ethers\n");
205 rv = RV_NOENUM;
206 } else {
207 for (i = 2; i < argc; i++) {
208 if ((eap = ether_aton(argv[i])) == NULL) {
209 eap = &ea;
210 hp = argv[i];
211 if (ether_hostton(hp, eap) != 0) {
212 rv = RV_NOTFOUND;
213 break;
214 }
215 } else {
216 hp = hostname;
217 if (ether_ntohost(hp, eap) != 0) {
218 rv = RV_NOTFOUND;
219 break;
220 }
221 }
222 ETHERSPRINT;
223 }
224 }
225 return rv;
226 }
227
228 /*
229 * group
230 */
231
232 static int
233 group(int argc, char *argv[])
234 {
235 struct group *gr;
236 unsigned long id;
237 int i, rv;
238
239 assert(argc > 1);
240 assert(argv != NULL);
241
242 #define GROUPPRINT printfmtstrings(gr->gr_mem, ":", ",", "%s:%s:%u", \
243 gr->gr_name, gr->gr_passwd, gr->gr_gid)
244
245 setgroupent(1);
246 rv = RV_OK;
247 if (argc == 2) {
248 while ((gr = getgrent()) != NULL)
249 GROUPPRINT;
250 } else {
251 for (i = 2; i < argc; i++) {
252 if (parsenum(argv[i], &id))
253 gr = getgrgid((gid_t)id);
254 else
255 gr = getgrnam(argv[i]);
256 if (gr != NULL)
257 GROUPPRINT;
258 else {
259 rv = RV_NOTFOUND;
260 break;
261 }
262 }
263 }
264 endgrent();
265 return rv;
266 }
267
268
269 /*
270 * hosts
271 */
272
273 static void
274 hostsprint(const struct hostent *he)
275 {
276 char buf[INET6_ADDRSTRLEN];
277
278 assert(he != NULL);
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);
282 }
283
284 static int
285 hosts(int argc, char *argv[])
286 {
287 struct hostent *he4, *he6;
288 char addr[IN6ADDRSZ];
289 int i, rv;
290
291 assert(argc > 1);
292 assert(argv != NULL);
293
294 sethostent(1);
295 he4 = he6 = NULL;
296 rv = RV_OK;
297 if (argc == 2) {
298 while ((he4 = gethostent()) != NULL)
299 hostsprint(he4);
300 } else {
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);
304 if (he6 != NULL)
305 hostsprint(he6);
306 } else if (inet_pton(AF_INET, argv[i],
307 (void *)addr) > 0) {
308 he4 = gethostbyaddr(addr, INADDRSZ, AF_INET);
309 if (he4 != NULL)
310 hostsprint(he4);
311 } else {
312 he6 = gethostbyname2(argv[i], AF_INET6);
313 if (he6 != NULL)
314 hostsprint(he6);
315 he4 = gethostbyname(argv[i]);
316 if (he4 != NULL)
317 hostsprint(he4);
318 }
319 if ( he4 == NULL && he6 == NULL ) {
320 rv = RV_NOTFOUND;
321 break;
322 }
323 }
324 }
325 endhostent();
326 return rv;
327 }
328
329 /*
330 * networks
331 */
332 static void
333 networksprint(const struct netent *ne)
334 {
335 char buf[INET6_ADDRSTRLEN];
336 struct in_addr ianet;
337
338 assert(ne != NULL);
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);
343 }
344
345 static int
346 networks(int argc, char *argv[])
347 {
348 struct netent *ne;
349 in_addr_t net;
350 int i, rv;
351
352 assert(argc > 1);
353 assert(argv != NULL);
354
355 setnetent(1);
356 rv = RV_OK;
357 if (argc == 2) {
358 while ((ne = getnetent()) != NULL)
359 networksprint(ne);
360 } else {
361 for (i = 2; i < argc; i++) {
362 net = inet_network(argv[i]);
363 if (net != INADDR_NONE)
364 ne = getnetbyaddr(net, AF_INET);
365 else
366 ne = getnetbyname(argv[i]);
367 if (ne != NULL)
368 networksprint(ne);
369 else {
370 rv = RV_NOTFOUND;
371 break;
372 }
373 }
374 }
375 endnetent();
376 return rv;
377 }
378
379 /*
380 * passwd
381 */
382 static int
383 passwd(int argc, char *argv[])
384 {
385 struct passwd *pw;
386 unsigned long id;
387 int i, rv;
388
389 assert(argc > 1);
390 assert(argv != NULL);
391
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)
395
396 setpassent(1);
397 rv = RV_OK;
398 if (argc == 2) {
399 while ((pw = getpwent()) != NULL)
400 PASSWDPRINT;
401 } else {
402 for (i = 2; i < argc; i++) {
403 if (parsenum(argv[i], &id))
404 pw = getpwuid((uid_t)id);
405 else
406 pw = getpwnam(argv[i]);
407 if (pw != NULL)
408 PASSWDPRINT;
409 else {
410 rv = RV_NOTFOUND;
411 break;
412 }
413 }
414 }
415 endpwent();
416 return rv;
417 }
418
419 /*
420 * protocols
421 */
422 static int
423 protocols(int argc, char *argv[])
424 {
425 struct protoent *pe;
426 unsigned long id;
427 int i, rv;
428
429 assert(argc > 1);
430 assert(argv != NULL);
431
432 #define PROTOCOLSPRINT printfmtstrings(pe->p_aliases, " ", " ", \
433 "%-16s %5d", pe->p_name, pe->p_proto)
434
435 setprotoent(1);
436 rv = RV_OK;
437 if (argc == 2) {
438 while ((pe = getprotoent()) != NULL)
439 PROTOCOLSPRINT;
440 } else {
441 for (i = 2; i < argc; i++) {
442 if (parsenum(argv[i], &id))
443 pe = getprotobynumber((int)id);
444 else
445 pe = getprotobyname(argv[i]);
446 if (pe != NULL)
447 PROTOCOLSPRINT;
448 else {
449 rv = RV_NOTFOUND;
450 break;
451 }
452 }
453 }
454 endprotoent();
455 return rv;
456 }
457
458 /*
459 * rpc
460 */
461 static int
462 rpc(int argc, char *argv[])
463 {
464 struct rpcent *re;
465 unsigned long id;
466 int i, rv;
467
468 assert(argc > 1);
469 assert(argv != NULL);
470
471 #define RPCPRINT printfmtstrings(re->r_aliases, " ", " ", \
472 "%-16s %6d", \
473 re->r_name, re->r_number)
474
475 setrpcent(1);
476 rv = RV_OK;
477 if (argc == 2) {
478 while ((re = getrpcent()) != NULL)
479 RPCPRINT;
480 } else {
481 for (i = 2; i < argc; i++) {
482 if (parsenum(argv[i], &id))
483 re = getrpcbynumber((int)id);
484 else
485 re = getrpcbyname(argv[i]);
486 if (re != NULL)
487 RPCPRINT;
488 else {
489 rv = RV_NOTFOUND;
490 break;
491 }
492 }
493 }
494 endrpcent();
495 return rv;
496 }
497
498 /*
499 * services
500 */
501 static int
502 services(int argc, char *argv[])
503 {
504 struct servent *se;
505 unsigned long id;
506 char *proto;
507 int i, rv;
508
509 assert(argc > 1);
510 assert(argv != NULL);
511
512 #define SERVICESPRINT printfmtstrings(se->s_aliases, " ", " ", \
513 "%-16s %5d/%s", \
514 se->s_name, ntohs(se->s_port), se->s_proto)
515
516 setservent(1);
517 rv = RV_OK;
518 if (argc == 2) {
519 while ((se = getservent()) != NULL)
520 SERVICESPRINT;
521 } else {
522 for (i = 2; i < argc; i++) {
523 proto = strchr(argv[i], '/');
524 if (proto != NULL)
525 *proto++ = '\0';
526 if (parsenum(argv[i], &id))
527 se = getservbyport(htons((u_short)id), proto);
528 else
529 se = getservbyname(argv[i], proto);
530 if (se != NULL)
531 SERVICESPRINT;
532 else {
533 rv = RV_NOTFOUND;
534 break;
535 }
536 }
537 }
538 endservent();
539 return rv;
540 }
541
542 /*
543 * shells
544 */
545 static int
546 shells(int argc, char *argv[])
547 {
548 const char *sh;
549 int i, rv;
550
551 assert(argc > 1);
552 assert(argv != NULL);
553
554 #define SHELLSPRINT printf("%s\n", sh)
555
556 setusershell();
557 rv = RV_OK;
558 if (argc == 2) {
559 while ((sh = getusershell()) != NULL)
560 SHELLSPRINT;
561 } else {
562 for (i = 2; i < argc; i++) {
563 setusershell();
564 while ((sh = getusershell()) != NULL) {
565 if (strcmp(sh, argv[i]) == 0) {
566 SHELLSPRINT;
567 break;
568 }
569 }
570 if (sh == NULL) {
571 rv = RV_NOTFOUND;
572 break;
573 }
574 }
575 }
576 endusershell();
577 return rv;
578 }
579
580 /*
581 * netgroup
582 */
583 static int
584 netgroup(int argc, char *argv[])
585 {
586 char *host, *user, *domain;
587 int first;
588 int rv, i;
589
590 assert(argc > 1);
591 assert(argv != NULL);
592
593 #define NETGROUPPRINT(s) (((s) != NULL) ? (s) : "")
594
595 rv = RV_OK;
596 if (argc == 2) {
597 fprintf(stderr, "Enumeration not supported on netgroup\n");
598 rv = RV_NOENUM;
599 } else {
600 for (i = 2; i < argc; i++) {
601 setnetgrent(argv[i]);
602 first = 1;
603 while (getnetgrent(&host, &user, &domain) != 0) {
604 if (first) {
605 first = 0;
606 (void)fputs(argv[i], stdout);
607 }
608 (void)printf(" (%s,%s,%s)",
609 NETGROUPPRINT(host),
610 NETGROUPPRINT(user),
611 NETGROUPPRINT(domain));
612 }
613 if (!first)
614 (void)putchar('\n');
615 endnetgrent();
616 }
617 }
618 return rv;
619 }
620
621 /*
622 * utmpx
623 */
624
625 #define UTMPXPRINTID do { \
626 size_t i; \
627 for (i = 0; i < sizeof ut->ut_id; i++) \
628 printf("%02hhx", ut->ut_id[i]); \
629 } while (0)
630
631 static void
632 utmpxprint(const struct utmpx *ut)
633 {
634
635 if (ut->ut_type == EMPTY)
636 return;
637
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));
641
642 switch (ut->ut_type) {
643 case BOOT_TIME:
644 printf("system boot\n");
645 return;
646 case SHUTDOWN_TIME:
647 printf("system shutdown\n");
648 return;
649 case OLD_TIME:
650 printf("old system time\n");
651 return;
652 case NEW_TIME:
653 printf("new system time\n");
654 return;
655 case USER_PROCESS:
656 printf("user process: id=\"");
657 UTMPXPRINTID;
658 printf("\" pid=\"%d\" user=\"%s\" line=\"%s\" host=\"%s\"\n",
659 ut->ut_pid, ut->ut_user, ut->ut_line, ut->ut_host);
660 break;
661 case INIT_PROCESS:
662 printf("init process: id=\"");
663 UTMPXPRINTID;
664 printf("\" pid=\"%d\"\n", ut->ut_pid);
665 break;
666 case LOGIN_PROCESS:
667 printf("login process: id=\"");
668 UTMPXPRINTID;
669 printf("\" pid=\"%d\" user=\"%s\" line=\"%s\" host=\"%s\"\n",
670 ut->ut_pid, ut->ut_user, ut->ut_line, ut->ut_host);
671 break;
672 case DEAD_PROCESS:
673 printf("dead process: id=\"");
674 UTMPXPRINTID;
675 printf("\" pid=\"%d\"\n", ut->ut_pid);
676 break;
677 default:
678 printf("unknown record type %hu\n", ut->ut_type);
679 break;
680 }
681 }
682
683 static int
684 utmpx(int argc, char *argv[])
685 {
686 const struct utmpx *ut;
687 const char *file = NULL;
688 int rv = RV_OK, db = 0;
689
690 assert(argc > 1);
691 assert(argv != NULL);
692
693 if (argc == 3 || argc == 4) {
694 if (strcmp(argv[2], "active") == 0)
695 db = UTXDB_ACTIVE;
696 else if (strcmp(argv[2], "lastlogin") == 0)
697 db = UTXDB_LASTLOGIN;
698 else if (strcmp(argv[2], "log") == 0)
699 db = UTXDB_LOG;
700 else
701 rv = RV_USAGE;
702 if (argc == 4)
703 file = argv[3];
704 } else {
705 rv = RV_USAGE;
706 }
707
708 if (rv == RV_USAGE) {
709 fprintf(stderr,
710 "Usage: %s utmpx active | lastlogin | log [filename]\n",
711 getprogname());
712 } else if (rv == RV_OK) {
713 if (setutxdb(db, file) != 0)
714 return (RV_NOTFOUND);
715 while ((ut = getutxent()) != NULL)
716 utmpxprint(ut);
717 endutxent();
718 }
719 return (rv);
720 }