s4:heimdal: import lorikeet-heimdal-200909210500 (commit 290db8d23647a27c39b97c189a0b...
[amitay/samba.git] / source4 / heimdal / lib / gssapi / mech / gss_mech_switch.c
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: src/lib/libgssapi/gss_mech_switch.c,v 1.2 2006/02/04 09:40:21 dfr Exp $
27  */
28
29 #include "mech_locl.h"
30 #include <heim_threads.h>
31
32 #ifndef _PATH_GSS_MECH
33 #define _PATH_GSS_MECH  "/etc/gss/mech"
34 #endif
35
36 struct _gss_mech_switch_list _gss_mechs = { NULL } ;
37 gss_OID_set _gss_mech_oids;
38 static HEIMDAL_MUTEX _gss_mech_mutex = HEIMDAL_MUTEX_INITIALIZER;
39
40 /*
41  * Convert a string containing an OID in 'dot' form
42  * (e.g. 1.2.840.113554.1.2.2) to a gss_OID.
43  */
44 static int
45 _gss_string_to_oid(const char* s, gss_OID oid)
46 {
47         int                     number_count, i, j;
48         size_t                  byte_count;
49         const char              *p, *q;
50         char                    *res;
51
52         oid->length = 0;
53         oid->elements = NULL;
54
55         /*
56          * First figure out how many numbers in the oid, then
57          * calculate the compiled oid size.
58          */
59         number_count = 0;
60         for (p = s; p; p = q) {
61                 q = strchr(p, '.');
62                 if (q) q = q + 1;
63                 number_count++;
64         }
65         
66         /*
67          * The first two numbers are in the first byte and each
68          * subsequent number is encoded in a variable byte sequence.
69          */
70         if (number_count < 2)
71                 return (EINVAL);
72
73         /*
74          * We do this in two passes. The first pass, we just figure
75          * out the size. Second time around, we actually encode the
76          * number.
77          */
78         res = 0;
79         for (i = 0; i < 2; i++) {
80                 byte_count = 0;
81                 for (p = s, j = 0; p; p = q, j++) {
82                         unsigned int number = 0;
83
84                         /*
85                          * Find the end of this number.
86                          */
87                         q = strchr(p, '.');
88                         if (q) q = q + 1;
89
90                         /*
91                          * Read the number of of the string. Don't
92                          * bother with anything except base ten.
93                          */
94                         while (*p && *p != '.') {
95                                 number = 10 * number + (*p - '0');
96                                 p++;
97                         }
98
99                         /*
100                          * Encode the number. The first two numbers
101                          * are packed into the first byte. Subsequent
102                          * numbers are encoded in bytes seven bits at
103                          * a time with the last byte having the high
104                          * bit set.
105                          */
106                         if (j == 0) {
107                                 if (res)
108                                         *res = number * 40;
109                         } else if (j == 1) {
110                                 if (res) {
111                                         *res += number;
112                                         res++;
113                                 }
114                                 byte_count++;
115                         } else if (j >= 2) {
116                                 /*
117                                  * The number is encoded in seven bit chunks.
118                                  */
119                                 unsigned int t;
120                                 unsigned int bytes;
121
122                                 bytes = 0;
123                                 for (t = number; t; t >>= 7)
124                                         bytes++;
125                                 if (bytes == 0) bytes = 1;
126                                 while (bytes) {
127                                         if (res) {
128                                                 int bit = 7*(bytes-1);
129                                                 
130                                                 *res = (number >> bit) & 0x7f;
131                                                 if (bytes != 1)
132                                                         *res |= 0x80;
133                                                 res++;
134                                         }
135                                         byte_count++;
136                                         bytes--;
137                                 }
138                         }
139                 }
140                 if (!res) {
141                         res = malloc(byte_count);
142                         if (!res)
143                                 return (ENOMEM);
144                         oid->length = byte_count;
145                         oid->elements = res;
146                 }
147         }
148
149         return (0);
150 }
151
152 #define SYM(name)                                                       \
153 do {                                                                    \
154         m->gm_mech.gm_ ## name = dlsym(so, "gss_" #name);               \
155         if (!m->gm_mech.gm_ ## name) {                                  \
156                 fprintf(stderr, "can't find symbol gss_" #name "\n");   \
157                 goto bad;                                               \
158         }                                                               \
159 } while (0)
160
161 #define OPTSYM(name)                                                    \
162 do {                                                                    \
163         m->gm_mech.gm_ ## name = dlsym(so, "gss_" #name);                       \
164 } while (0)
165
166 /*
167  *
168  */
169 static int
170 add_builtin(gssapi_mech_interface mech)
171 {
172     struct _gss_mech_switch *m;
173     OM_uint32 minor_status;
174
175     /* not registering any mech is ok */
176     if (mech == NULL)
177         return 0;
178
179     m = malloc(sizeof(*m));
180     if (m == NULL)
181         return 1;
182     m->gm_so = NULL;
183     m->gm_mech = *mech;
184     m->gm_mech_oid = mech->gm_mech_oid; /* XXX */
185     gss_add_oid_set_member(&minor_status,
186                            &m->gm_mech.gm_mech_oid, &_gss_mech_oids);
187
188     /* pick up the oid sets of names */
189
190     if (m->gm_mech.gm_inquire_names_for_mech) {
191         (*m->gm_mech.gm_inquire_names_for_mech)(&minor_status,
192             &m->gm_mech.gm_mech_oid, &m->gm_name_types);
193     } else {
194         gss_create_empty_oid_set(&minor_status, &m->gm_name_types);
195     }
196
197     SLIST_INSERT_HEAD(&_gss_mechs, m, gm_link);
198     return 0;
199 }
200
201 /*
202  * Load the mechanisms file (/etc/gss/mech).
203  */
204 void
205 _gss_load_mech(void)
206 {
207         OM_uint32       major_status, minor_status;
208         FILE            *fp;
209         char            buf[256];
210         char            *p;
211         char            *name, *oid, *lib, *kobj;
212         struct _gss_mech_switch *m;
213         void            *so;
214
215
216         HEIMDAL_MUTEX_lock(&_gss_mech_mutex);
217
218         if (SLIST_FIRST(&_gss_mechs)) {
219                 HEIMDAL_MUTEX_unlock(&_gss_mech_mutex);
220                 return;
221         }
222
223         major_status = gss_create_empty_oid_set(&minor_status,
224             &_gss_mech_oids);
225         if (major_status) {
226                 HEIMDAL_MUTEX_unlock(&_gss_mech_mutex);
227                 return;
228         }
229
230         add_builtin(__gss_krb5_initialize());
231         add_builtin(__gss_spnego_initialize());
232         add_builtin(__gss_ntlm_initialize());
233
234 #ifdef HAVE_DLOPEN
235         fp = fopen(_PATH_GSS_MECH, "r");
236         if (!fp) {
237                 HEIMDAL_MUTEX_unlock(&_gss_mech_mutex);
238                 return;
239         }
240         rk_cloexec_file(fp);
241
242         while (fgets(buf, sizeof(buf), fp)) {
243                 if (*buf == '#')
244                         continue;
245                 p = buf;
246                 name = strsep(&p, "\t\n ");
247                 if (p) while (isspace((unsigned char)*p)) p++;
248                 oid = strsep(&p, "\t\n ");
249                 if (p) while (isspace((unsigned char)*p)) p++;
250                 lib = strsep(&p, "\t\n ");
251                 if (p) while (isspace((unsigned char)*p)) p++;
252                 kobj = strsep(&p, "\t\n ");
253                 if (!name || !oid || !lib || !kobj)
254                         continue;
255
256 #ifndef RTLD_LOCAL
257 #define RTLD_LOCAL 0
258 #endif
259
260                 so = dlopen(lib, RTLD_LAZY | RTLD_LOCAL);
261                 if (!so) {
262 /*                      fprintf(stderr, "dlopen: %s\n", dlerror()); */
263                         continue;
264                 }
265
266                 m = malloc(sizeof(*m));
267                 if (!m)
268                         break;
269                 m->gm_so = so;
270                 if (_gss_string_to_oid(oid, &m->gm_mech.gm_mech_oid)) {
271                         free(m);
272                         continue;
273                 }
274                 m->gm_mech.gm_flags = 0;
275                 
276                 major_status = gss_add_oid_set_member(&minor_status,
277                     &m->gm_mech.gm_mech_oid, &_gss_mech_oids);
278                 if (major_status) {
279                         free(m->gm_mech.gm_mech_oid.elements);
280                         free(m);
281                         continue;
282                 }
283
284                 SYM(acquire_cred);
285                 SYM(release_cred);
286                 SYM(init_sec_context);
287                 SYM(accept_sec_context);
288                 SYM(process_context_token);
289                 SYM(delete_sec_context);
290                 SYM(context_time);
291                 SYM(get_mic);
292                 SYM(verify_mic);
293                 SYM(wrap);
294                 SYM(unwrap);
295                 SYM(display_status);
296                 SYM(indicate_mechs);
297                 SYM(compare_name);
298                 SYM(display_name);
299                 SYM(import_name);
300                 SYM(export_name);
301                 SYM(release_name);
302                 SYM(inquire_cred);
303                 SYM(inquire_context);
304                 SYM(wrap_size_limit);
305                 SYM(add_cred);
306                 SYM(inquire_cred_by_mech);
307                 SYM(export_sec_context);
308                 SYM(import_sec_context);
309                 SYM(inquire_names_for_mech);
310                 SYM(inquire_mechs_for_name);
311                 SYM(canonicalize_name);
312                 SYM(duplicate_name);
313                 OPTSYM(inquire_cred_by_oid);
314                 OPTSYM(inquire_sec_context_by_oid);
315                 OPTSYM(set_sec_context_option);
316                 OPTSYM(set_cred_option);
317                 OPTSYM(pseudo_random);
318                 OPTSYM(wrap_iov);
319                 OPTSYM(unwrap_iov);
320                 OPTSYM(wrap_iov_length);
321
322                 SLIST_INSERT_HEAD(&_gss_mechs, m, gm_link);
323                 continue;
324
325         bad:
326                 free(m->gm_mech.gm_mech_oid.elements);
327                 free(m);
328                 dlclose(so);
329                 continue;
330         }
331         fclose(fp);
332 #endif
333         HEIMDAL_MUTEX_unlock(&_gss_mech_mutex);
334 }
335
336 gssapi_mech_interface
337 __gss_get_mechanism(gss_OID mech)
338 {
339         struct _gss_mech_switch *m;
340
341         _gss_load_mech();
342         SLIST_FOREACH(m, &_gss_mechs, gm_link) {
343                 if (gss_oid_equal(&m->gm_mech.gm_mech_oid, mech))
344                         return &m->gm_mech;
345         }
346         return NULL;
347 }