xref: /freebsd/lib/libgssapi/gss_mech_switch.c (revision 2abb9b42a53c39a8b781e25a05de1de9e98b8b9a)
1 /*-
2  * Copyright (c) 2005 Doug Rabson
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  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  *	$FreeBSD$
27  */
28 
29 #include <gssapi/gssapi.h>
30 #include <ctype.h>
31 #include <dlfcn.h>
32 #include <errno.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 
37 #include "mech_switch.h"
38 #include "utils.h"
39 
40 #ifndef _PATH_GSS_MECH
41 #define _PATH_GSS_MECH	"/etc/gss/mech"
42 #endif
43 
44 struct _gss_mech_switch_list _gss_mechs =
45 	SLIST_HEAD_INITIALIZER(_gss_mechs);
46 gss_OID_set _gss_mech_oids;
47 
48 /*
49  * Convert a string containing an OID in 'dot' form
50  * (e.g. 1.2.840.113554.1.2.2) to a gss_OID.
51  */
52 static int
53 _gss_string_to_oid(const char* s, gss_OID oid)
54 {
55 	int			number_count, i, j;
56 	int			byte_count;
57 	const char		*p, *q;
58 	char			*res;
59 
60 	oid->length = 0;
61 	oid->elements = NULL;
62 
63 	/*
64 	 * First figure out how many numbers in the oid, then
65 	 * calculate the compiled oid size.
66 	 */
67 	number_count = 0;
68 	for (p = s; p; p = q) {
69 		q = strchr(p, '.');
70 		if (q) q = q + 1;
71 		number_count++;
72 	}
73 
74 	/*
75 	 * The first two numbers are in the first byte and each
76 	 * subsequent number is encoded in a variable byte sequence.
77 	 */
78 	if (number_count < 2)
79 		return (EINVAL);
80 
81 	/*
82 	 * We do this in two passes. The first pass, we just figure
83 	 * out the size. Second time around, we actually encode the
84 	 * number.
85 	 */
86 	res = NULL;
87 	for (i = 0; i < 2; i++) {
88 		byte_count = 0;
89 		for (p = s, j = 0; p; p = q, j++) {
90 			unsigned int number = 0;
91 
92 			/*
93 			 * Find the end of this number.
94 			 */
95 			q = strchr(p, '.');
96 			if (q) q = q + 1;
97 
98 			/*
99 			 * Read the number of of the string. Don't
100 			 * bother with anything except base ten.
101 			 */
102 			while (*p && *p != '.') {
103 				number = 10 * number + (*p - '0');
104 				p++;
105 			}
106 
107 			/*
108 			 * Encode the number. The first two numbers
109 			 * are packed into the first byte. Subsequent
110 			 * numbers are encoded in bytes seven bits at
111 			 * a time with the last byte having the high
112 			 * bit set.
113 			 */
114 			if (j == 0) {
115 				if (res)
116 					*res = number * 40;
117 			} else if (j == 1) {
118 				if (res) {
119 					*res += number;
120 					res++;
121 				}
122 				byte_count++;
123 			} else if (j >= 2) {
124 				/*
125 				 * The number is encoded in seven bit chunks.
126 				 */
127 				unsigned int t;
128 				int bytes;
129 
130 				bytes = 0;
131 				for (t = number; t; t >>= 7)
132 					bytes++;
133 				if (bytes == 0) bytes = 1;
134 				while (bytes) {
135 					if (res) {
136 						int bit = 7*(bytes-1);
137 
138 						*res = (number >> bit) & 0x7f;
139 						if (bytes != 1)
140 							*res |= 0x80;
141 						res++;
142 					}
143 					byte_count++;
144 					bytes--;
145 				}
146 			}
147 		}
148 		if (!res) {
149 			res = malloc(byte_count);
150 			if (!res)
151 				return (ENOMEM);
152 			oid->length = byte_count;
153 			oid->elements = res;
154 		}
155 	}
156 
157 	return (0);
158 }
159 
160 
161 #define SYM(name)						\
162 do {								\
163 	snprintf(buf, sizeof(buf), "%s_%s",			\
164 	    m->gm_name_prefix, #name);				\
165 	m->gm_ ## name = dlsym(so, buf);			\
166 	if (!m->gm_ ## name) {					\
167 		fprintf(stderr, "can't find symbol %s\n", buf);	\
168 		goto bad;					\
169 	}							\
170 } while (0)
171 
172 #define OPTSYM(name)				\
173 do {						\
174 	snprintf(buf, sizeof(buf), "%s_%s",	\
175 	    m->gm_name_prefix, #name);		\
176 	m->gm_ ## name = dlsym(so, buf);	\
177 } while (0)
178 
179 /*
180  * Load the mechanisms file (/etc/gss/mech).
181  */
182 void
183 _gss_load_mech(void)
184 {
185 	OM_uint32	major_status, minor_status;
186 	FILE		*fp;
187 	char		buf[256];
188 	char		*p;
189 	char		*name, *oid, *lib, *kobj;
190 	struct _gss_mech_switch *m;
191 	int		count;
192 	void		*so;
193 	const char	*(*prefix_fn)(void);
194 
195 	if (SLIST_FIRST(&_gss_mechs))
196 		return;
197 
198 	major_status = gss_create_empty_oid_set(&minor_status,
199 	    &_gss_mech_oids);
200 	if (major_status)
201 		return;
202 
203 	fp = fopen(_PATH_GSS_MECH, "r");
204 	if (!fp) {
205 		perror(_PATH_GSS_MECH);
206 		return;
207 	}
208 
209 	count = 0;
210 	while (fgets(buf, sizeof(buf), fp)) {
211 		if (*buf == '#')
212 			continue;
213 		p = buf;
214 		name = strsep(&p, "\t\n ");
215 		if (p) while (isspace(*p)) p++;
216 		oid = strsep(&p, "\t\n ");
217 		if (p) while (isspace(*p)) p++;
218 		lib = strsep(&p, "\t\n ");
219 		if (p) while (isspace(*p)) p++;
220 		kobj = strsep(&p, "\t\n ");
221 		if (!name || !oid || !lib || !kobj)
222 			continue;
223 
224 		so = dlopen(lib, RTLD_LOCAL);
225 		if (!so) {
226 			fprintf(stderr, "dlopen: %s\n", dlerror());
227 			continue;
228 		}
229 
230 		m = malloc(sizeof(struct _gss_mech_switch));
231 		if (!m)
232 			break;
233 		m->gm_so = so;
234 		if (_gss_string_to_oid(oid, &m->gm_mech_oid)) {
235 			free(m);
236 			continue;
237 		}
238 
239 		prefix_fn = (const char *(*)(void))
240 			dlsym(so, "_gss_name_prefix");
241 		if (prefix_fn)
242 			m->gm_name_prefix = prefix_fn();
243 		else
244 			m->gm_name_prefix = "gss";
245 
246 		major_status = gss_add_oid_set_member(&minor_status,
247 		    &m->gm_mech_oid, &_gss_mech_oids);
248 		if (major_status) {
249 			free(m->gm_mech_oid.elements);
250 			free(m);
251 			continue;
252 		}
253 
254 		SYM(acquire_cred);
255 		SYM(release_cred);
256 		SYM(init_sec_context);
257 		SYM(accept_sec_context);
258 		SYM(process_context_token);
259 		SYM(delete_sec_context);
260 		SYM(context_time);
261 		SYM(get_mic);
262 		SYM(verify_mic);
263 		SYM(wrap);
264 		SYM(unwrap);
265 		SYM(display_status);
266 		OPTSYM(indicate_mechs);
267 		SYM(compare_name);
268 		SYM(display_name);
269 		SYM(import_name);
270 		SYM(export_name);
271 		SYM(release_name);
272 		SYM(inquire_cred);
273 		SYM(inquire_context);
274 		SYM(wrap_size_limit);
275 		SYM(add_cred);
276 		SYM(inquire_cred_by_mech);
277 		SYM(export_sec_context);
278 		SYM(import_sec_context);
279 		SYM(inquire_names_for_mech);
280 		SYM(inquire_mechs_for_name);
281 		SYM(canonicalize_name);
282 		SYM(duplicate_name);
283 		OPTSYM(inquire_sec_context_by_oid);
284 		OPTSYM(inquire_cred_by_oid);
285 		OPTSYM(set_sec_context_option);
286 		OPTSYM(set_cred_option);
287 		OPTSYM(pseudo_random);
288 		OPTSYM(pname_to_uid);
289 
290 		SLIST_INSERT_HEAD(&_gss_mechs, m, gm_link);
291 		count++;
292 		continue;
293 
294 	bad:
295 		free(m->gm_mech_oid.elements);
296 		free(m);
297 		dlclose(so);
298 		continue;
299 	}
300 	fclose(fp);
301 }
302 
303 struct _gss_mech_switch *
304 _gss_find_mech_switch(gss_OID mech)
305 {
306 	struct _gss_mech_switch *m;
307 
308 	_gss_load_mech();
309 	SLIST_FOREACH(m, &_gss_mechs, gm_link) {
310 		if (gss_oid_equal(&m->gm_mech_oid, mech))
311 			return m;
312 	}
313 	return (0);
314 }
315