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