xref: /freebsd/contrib/openpam/bin/openpam_dump_policy/openpam_dump_policy.c (revision 9f23cbd6cae82fd77edfad7173432fa8dccd0a95)
1 /*-
2  * Copyright (c) 2011-2014 Dag-Erling Smørgrav
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote
14  *    products derived from this software without specific prior written
15  *    permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33 
34 #include <ctype.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
39 
40 #include <security/pam_appl.h>
41 
42 #include "openpam_impl.h"
43 #include "openpam_asprintf.h"
44 
45 static char *
46 openpam_chain_name(const char *service, pam_facility_t fclt)
47 {
48 	const char *facility = pam_facility_name[fclt];
49 	char *name;
50 
51 	if (asprintf(&name, "pam_%s_%s", service, facility) == -1)
52 		return (NULL);
53 	return (name);
54 }
55 
56 static char *
57 openpam_facility_index_name(pam_facility_t fclt)
58 {
59 	const char *facility = pam_facility_name[fclt];
60 	char *name, *p;
61 
62 	if (asprintf(&name, "PAM_%s", facility) == -1)
63 		return (NULL);
64 	for (p = name + 4; *p; ++p)
65 		*p = toupper((unsigned char)*p);
66 	return (name);
67 }
68 
69 int
70 openpam_dump_chain(const char *name, pam_chain_t *chain)
71 {
72 	char *modname, **opt, *p;
73 	int i;
74 
75 	for (i = 0; chain != NULL; ++i, chain = chain->next) {
76 		/* declare the module's struct pam_module */
77 		modname = strrchr(chain->module->path, '/');
78 		modname = strdup(modname ? modname : chain->module->path);
79 		if (modname == NULL)
80 			return (PAM_BUF_ERR);
81 		for (p = modname; *p && *p != '.'; ++p)
82 			/* nothing */ ;
83 		*p = '\0';
84 		printf("extern struct pam_module %s_pam_module;\n", modname);
85 		/* module arguments */
86 		printf("static char *%s_%d_optv[] = {\n", name, i);
87 		for (opt = chain->optv; *opt; ++opt) {
88 			printf("\t\"");
89 			for (p = *opt; *p; ++p) {
90 				if (isprint((unsigned char)*p) && *p != '"')
91 					printf("%c", *p);
92 				else
93 					printf("\\x%02x", (unsigned char)*p);
94 			}
95 			printf("\",\n");
96 		}
97 		printf("\tNULL,\n");
98 		printf("};\n");
99 		/* next module in chain */
100 		if (chain->next != NULL)
101 			printf("static pam_chain_t %s_%d;\n", name, i + 1);
102 		/* chain entry */
103 		printf("static pam_chain_t %s_%d = {\n", name, i);
104 		printf("\t.module = &%s_pam_module,\n", modname);
105 		printf("\t.flag = 0x%08x,\n", chain->flag);
106 		printf("\t.optc = %d,\n", chain->optc);
107 		printf("\t.optv = %s_%d_optv,\n", name, i);
108 		if (chain->next)
109 			printf("\t.next = &%s_%d,\n", name, i + 1);
110 		else
111 			printf("\t.next = NULL,\n");
112 		printf("};\n");
113 		free(modname);
114 	}
115 	return (PAM_SUCCESS);
116 }
117 
118 int
119 openpam_dump_policy(const char *service)
120 {
121 	pam_handle_t *pamh;
122 	char *name;
123 	int fclt, ret;
124 
125 	if ((pamh = calloc(1, sizeof *pamh)) == NULL)
126 		return (PAM_BUF_ERR);
127 	if ((ret = openpam_configure(pamh, service)) != PAM_SUCCESS)
128 		return (ret);
129 	for (fclt = 0; fclt < PAM_NUM_FACILITIES; ++fclt) {
130 		if (pamh->chains[fclt] != NULL) {
131 			if ((name = openpam_chain_name(service, fclt)) == NULL)
132 				return (PAM_BUF_ERR);
133 			ret = openpam_dump_chain(name, pamh->chains[fclt]);
134 			free(name);
135 			if (ret != PAM_SUCCESS)
136 				return (ret);
137 		}
138 	}
139 	printf("static pam_policy_t pam_%s_policy = {\n", service);
140 	printf("\t.service = \"%s\",\n", service);
141 	printf("\t.chains = {\n");
142 	for (fclt = 0; fclt < PAM_NUM_FACILITIES; ++fclt) {
143 		if ((name = openpam_facility_index_name(fclt)) == NULL)
144 			return (PAM_BUF_ERR);
145 		printf("\t\t[%s] = ", name);
146 		free(name);
147 		if (pamh->chains[fclt] != NULL) {
148 			if ((name = openpam_chain_name(service, fclt)) == NULL)
149 				return (PAM_BUF_ERR);
150 			printf("&%s_0,\n", name);
151 			free(name);
152 		} else {
153 			printf("NULL,\n");
154 		}
155 	}
156 	printf("\t},\n");
157 	printf("};\n");
158 	free(pamh);
159 	return (PAM_SUCCESS);
160 }
161 
162 static void
163 usage(void)
164 {
165 
166 	fprintf(stderr, "usage: openpam_dump_policy [-d] policy ...\n");
167 	exit(1);
168 }
169 
170 int
171 main(int argc, char *argv[])
172 {
173 	int i, opt;
174 
175 	while ((opt = getopt(argc, argv, "d")) != -1)
176 		switch (opt) {
177 		case 'd':
178 			openpam_debug = 1;
179 			break;
180 		default:
181 			usage();
182 		}
183 
184 	argc -= optind;
185 	argv += optind;
186 
187 	if (argc < 1)
188 		usage();
189 
190 	printf("#include <security/pam_appl.h>\n");
191 	printf("#include \"openpam_impl.h\"\n");
192 	for (i = 0; i < argc; ++i)
193 		openpam_dump_policy(argv[i]);
194 	printf("pam_policy_t *pam_embedded_policies[] = {\n");
195 	for (i = 0; i < argc; ++i)
196 		printf("\t&pam_%s_policy,\n", argv[i]);
197 	printf("\tNULL,\n");
198 	printf("};\n");
199 	exit(0);
200 }
201