xref: /illumos-gate/usr/src/cmd/getent/getent.c (revision 8119dad84d6416f13557b0ba8e2aaf9064cbcfd3)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 2018 Peter Tribble.
24  * Copyright (c) 2014 Gary Mills
25  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
26  * Use is subject to license terms.
27  */
28 
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <locale.h>
33 #include <unistd.h>
34 #include "getent.h"
35 
36 static const char *cmdname;
37 
38 struct table {
39 	char	*name;			/* name of the table */
40 	int	(*func)(const char **);	/* function to do the lookup */
41 };
42 
43 static struct table t[] = {
44 	{ "passwd",	dogetpw },
45 	{ "shadow",	dogetsp },
46 	{ "group",	dogetgr },
47 	{ "hosts",	dogethost },
48 	{ "ipnodes",	dogetipnodes },
49 	{ "services",	dogetserv },
50 	{ "protocols",	dogetproto },
51 	{ "ethers",	dogetethers },
52 	{ "networks",	dogetnet },
53 	{ "netmasks",	dogetnetmask },
54 	{ "project",	dogetproject },
55 	{ "auth_attr",	dogetauthattr },
56 	{ "exec_attr",	dogetexecattr },
57 	{ "prof_attr",	dogetprofattr },
58 	{ "user_attr",	dogetuserattr },
59 	{ "netgroup",	dogetnetgr },
60 	{ NULL,		NULL }
61 };
62 
63 static	void usage(void) __NORETURN;
64 
65 int
66 main(int argc, const char **argv)
67 {
68 	struct table *p;
69 
70 	(void) setlocale(LC_ALL, "");
71 
72 #if !defined(TEXT_DOMAIN)
73 #define	TEXT_DOMAIN	"SYS_TEXT"
74 #endif
75 
76 	(void) textdomain(TEXT_DOMAIN);
77 
78 	cmdname = argv[0];
79 
80 	if (argc < 2)
81 		usage();
82 
83 	for (p = t; p->name != NULL; p++) {
84 		if (strcmp(argv[1], p->name) == 0) {
85 			int rc;
86 
87 			rc = (*p->func)(&argv[2]);
88 			switch (rc) {
89 			case EXC_SYNTAX:
90 				(void) fprintf(stderr,
91 				    gettext("Syntax error\n"));
92 				break;
93 			case EXC_ERROR:
94 				(void) fprintf(stderr,
95 				    gettext("Internal error\n"));
96 				break;
97 			case EXC_ENUM_NOT_SUPPORTED:
98 				(void) fprintf(stderr,
99 	gettext("Enumeration not supported on %s\n"), argv[1]);
100 				break;
101 			case EXC_NAME_NOT_FOUND:
102 				break;
103 			}
104 			exit(rc);
105 		}
106 	}
107 	(void) fprintf(stderr, gettext("Unknown database: %s\n"), argv[1]);
108 	usage();
109 	/* NOTREACHED */
110 }
111 
112 static void
113 usage(void)
114 {
115 	(void) fprintf(stderr,
116 	    gettext("usage: %s database [ key ... ]\n"), cmdname);
117 	exit(EXC_SYNTAX);
118 }
119