s4:torture: Adapt KDC canon test to Heimdal upstream changes
[samba.git] / third_party / 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, 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 *oidp)
46 {
47         int                     number_count, i, j;
48         size_t                  byte_count;
49         const char              *p, *q;
50         char                    *res;
51         gss_OID_desc            oid;
52
53         *oidp = GSS_C_NO_OID;
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         {
150                 OM_uint32 minor_status, tmp;
151
152                 if (GSS_ERROR(_gss_intern_oid(&minor_status, &oid, oidp))) {
153                         _gss_free_oid(&tmp, &oid);
154                         return (minor_status);
155                 }
156
157                 _gss_free_oid(&tmp, &oid);
158         }
159
160         return (0);
161 }
162
163 #define SYM(name)                                                       \
164 do {                                                                    \
165         m->gm_mech.gm_ ## name = (_gss_##name##_t *)dlsym(so, "gss_" #name); \
166         if (!m->gm_mech.gm_ ## name ||                                  \
167             m->gm_mech.gm_ ##name == gss_ ## name) {                    \
168                 _gss_mg_log(1, "can't find symbol gss_" #name "\n");    \
169                 goto bad;                                               \
170         }                                                               \
171 } while (0)
172
173 #define OPTSYM(name)                                                    \
174 do {                                                                    \
175         m->gm_mech.gm_ ## name =  (_gss_##name##_t *)dlsym(so, "gss_" #name); \
176         if (m->gm_mech.gm_ ## name == gss_ ## name)                     \
177                 m->gm_mech.gm_ ## name = NULL;                          \
178 } while (0)
179
180 /* mech exports gssspi_XXX, internally referred to as gss_XXX */
181 #define OPTSPISYM(name)                                                 \
182 do {                                                                    \
183         m->gm_mech.gm_ ## name =  (_gss_##name##_t *)dlsym(so, "gssspi_" #name); \
184 } while (0)
185
186 /* mech exports gssspi_XXX, internally referred to as gssspi_XXX */
187 #define OPTSPISPISYM(name)                                                      \
188 do {                                                                    \
189         m->gm_mech.gm_ ## name =  (_gss_##name##_t *)dlsym(so, "gssspi_" #name); \
190         if (m->gm_mech.gm_ ## name == gssspi_ ## name)                  \
191                 m->gm_mech.gm_ ## name = NULL;                          \
192 } while (0)
193
194 #define COMPATSYM(name)                                                 \
195 do {                                                                    \
196         m->gm_mech.gm_compat->gmc_ ## name =  (_gss_##name##_t *)dlsym(so, "gss_" #name); \
197         if (m->gm_mech.gm_compat->gmc_ ## name == gss_ ## name)         \
198                 m->gm_mech.gm_compat->gmc_ ## name = NULL;              \
199 } while (0)
200
201 #define COMPATSPISYM(name)                                              \
202 do {                                                                    \
203         m->gm_mech.gm_compat->gmc_ ## name =  (_gss_##name##_t *)dlsym(so, "gssspi_" #name); \
204         if (m->gm_mech.gm_compat->gmc_ ## name == gss_ ## name)         \
205                 m->gm_mech.gm_compat->gmc_ ## name = NULL;              \
206 } while (0)
207
208 /*
209  *
210  */
211 static int
212 add_builtin(gssapi_mech_interface mech)
213 {
214     struct _gss_mech_switch *m;
215     OM_uint32 minor_status;
216
217     /* not registering any mech is ok */
218     if (mech == NULL)
219         return 0;
220
221     m = calloc(1, sizeof(*m));
222     if (m == NULL)
223         return ENOMEM;
224     m->gm_so = NULL;
225     m->gm_mech = *mech;
226     _gss_intern_oid(&minor_status, &mech->gm_mech_oid, &m->gm_mech_oid);
227     if (minor_status) {
228         free(m);
229         return minor_status;
230     }
231     gss_add_oid_set_member(&minor_status,
232                            &m->gm_mech.gm_mech_oid, &_gss_mech_oids);
233
234     /* pick up the oid sets of names */
235
236     if (m->gm_mech.gm_inquire_names_for_mech)
237         (*m->gm_mech.gm_inquire_names_for_mech)(&minor_status,
238             &m->gm_mech.gm_mech_oid, &m->gm_name_types);
239
240     if (m->gm_name_types == NULL)
241         gss_create_empty_oid_set(&minor_status, &m->gm_name_types);
242
243     HEIM_TAILQ_INSERT_TAIL(&_gss_mechs, m, gm_link);
244     return 0;
245 }
246
247 static void
248 init_mech_switch_list(void *p)
249 {
250     struct _gss_mech_switch_list *mechs = p;
251
252     HEIM_TAILQ_INIT(mechs);
253 }
254
255 /*
256  * Load the mechanisms file (/etc/gss/mech).
257  */
258 void
259 _gss_load_mech(void)
260 {
261         OM_uint32       major_status, minor_status;
262         static heim_base_once_t once = HEIM_BASE_ONCE_INIT;
263 #ifdef HAVE_DLOPEN
264         FILE            *fp;
265         char            buf[256];
266         char            *p;
267         char            *name, *oid, *lib, *kobj;
268         struct _gss_mech_switch *m;
269         void            *so;
270         gss_OID         mech_oid;
271         int             found;
272         const char      *conf = secure_getenv("GSS_MECH_CONFIG");
273 #endif
274
275         heim_base_once_f(&once, &_gss_mechs, init_mech_switch_list);
276
277         HEIMDAL_MUTEX_lock(&_gss_mech_mutex);
278
279         if (!HEIM_TAILQ_EMPTY(&_gss_mechs)) {
280                 HEIMDAL_MUTEX_unlock(&_gss_mech_mutex);
281                 return;
282         }
283
284         major_status = gss_create_empty_oid_set(&minor_status,
285             &_gss_mech_oids);
286         if (major_status) {
287                 HEIMDAL_MUTEX_unlock(&_gss_mech_mutex);
288                 return;
289         }
290
291         add_builtin(__gss_krb5_initialize());
292         add_builtin(__gss_spnego_initialize());
293         add_builtin(__gss_ntlm_initialize());
294
295 #ifdef HAVE_DLOPEN
296         fp = fopen(conf ? conf : _PATH_GSS_MECH, "r");
297         if (!fp)
298                 goto out;
299         rk_cloexec_file(fp);
300
301         while (fgets(buf, sizeof(buf), fp)) {
302                 _gss_mo_init *mi;
303
304                 if (*buf == '#')
305                         continue;
306                 p = buf;
307                 name = strsep(&p, "\t\n ");
308                 if (p) while (isspace((unsigned char)*p)) p++;
309                 oid = strsep(&p, "\t\n ");
310                 if (p) while (isspace((unsigned char)*p)) p++;
311                 lib = strsep(&p, "\t\n ");
312                 if (p) while (isspace((unsigned char)*p)) p++;
313                 kobj = strsep(&p, "\t\n ");
314                 if (!name || !oid || !lib || !kobj)
315                         continue;
316
317                 if (_gss_string_to_oid(oid, &mech_oid))
318                         continue;
319
320                 /*
321                  * Check for duplicates, already loaded mechs.
322                  */
323                 found = 0;
324                 HEIM_TAILQ_FOREACH(m, &_gss_mechs, gm_link) {
325                         if (gss_oid_equal(&m->gm_mech.gm_mech_oid, mech_oid)) {
326                                 found = 1;
327                                 break;
328                         }
329                 }
330                 if (found)
331                         continue;
332
333                 so = dlopen(lib, RTLD_LAZY | RTLD_LOCAL | RTLD_GROUP);
334                 if (so == NULL) {
335                         _gss_mg_log(1, "dlopen: %s\n", dlerror());
336                         goto bad;
337                 }
338
339                 m = calloc(1, sizeof(*m));
340                 if (m == NULL)
341                         goto bad;
342
343                 m->gm_so = so;
344                 m->gm_mech_oid = mech_oid;
345                 m->gm_mech.gm_name = strdup(name);
346                 m->gm_mech.gm_mech_oid = *mech_oid;
347                 m->gm_mech.gm_flags = 0;
348                 m->gm_mech.gm_compat = calloc(1, sizeof(struct gss_mech_compat_desc_struct));
349                 if (m->gm_mech.gm_compat == NULL)
350                         goto bad;
351
352                 major_status = gss_add_oid_set_member(&minor_status,
353                     &m->gm_mech.gm_mech_oid, &_gss_mech_oids);
354                 if (GSS_ERROR(major_status))
355                         goto bad;
356
357                 SYM(acquire_cred);
358                 SYM(release_cred);
359                 SYM(init_sec_context);
360                 SYM(accept_sec_context);
361                 SYM(process_context_token);
362                 SYM(delete_sec_context);
363                 SYM(context_time);
364                 SYM(get_mic);
365                 SYM(verify_mic);
366                 SYM(wrap);
367                 SYM(unwrap);
368                 OPTSYM(display_status);
369                 OPTSYM(indicate_mechs);
370                 SYM(compare_name);
371                 SYM(display_name);
372                 SYM(import_name);
373                 SYM(export_name);
374                 SYM(release_name);
375                 OPTSYM(inquire_cred);
376                 SYM(inquire_context);
377                 SYM(wrap_size_limit);
378                 OPTSYM(add_cred);
379                 OPTSYM(inquire_cred_by_mech);
380                 SYM(export_sec_context);
381                 SYM(import_sec_context);
382                 OPTSYM(inquire_names_for_mech);
383                 OPTSYM(inquire_mechs_for_name);
384                 SYM(canonicalize_name);
385                 SYM(duplicate_name);
386                 OPTSYM(inquire_cred_by_oid);
387                 OPTSYM(inquire_sec_context_by_oid);
388                 OPTSYM(set_sec_context_option);
389                 OPTSPISYM(set_cred_option);
390                 OPTSYM(pseudo_random);
391                 OPTSYM(wrap_iov);
392                 OPTSYM(unwrap_iov);
393                 OPTSYM(wrap_iov_length);
394                 OPTSYM(store_cred);
395                 OPTSYM(export_cred);
396                 OPTSYM(import_cred);
397                 OPTSYM(acquire_cred_from);
398                 OPTSYM(acquire_cred_impersonate_name);
399 #if 0
400                 OPTSYM(iter_creds);
401                 OPTSYM(destroy_cred);
402                 OPTSYM(cred_hold);
403                 OPTSYM(cred_unhold);
404                 OPTSYM(cred_label_get);
405                 OPTSYM(cred_label_set);
406 #endif
407                 OPTSYM(display_name_ext);
408                 OPTSYM(inquire_name);
409                 OPTSYM(get_name_attribute);
410                 OPTSYM(set_name_attribute);
411                 OPTSYM(delete_name_attribute);
412                 OPTSYM(export_name_composite);
413                 OPTSYM(localname);
414                 OPTSYM(duplicate_cred);
415                 OPTSYM(add_cred_from);
416                 OPTSYM(store_cred_into);
417                 OPTSPISYM(authorize_localname);
418                 OPTSPISPISYM(query_mechanism_info);
419                 OPTSPISPISYM(query_meta_data);
420                 OPTSPISPISYM(exchange_meta_data);
421
422                 mi = (_gss_mo_init *)dlsym(so, "gss_mo_init");
423                 if (mi != NULL) {
424                         major_status = mi(&minor_status, mech_oid,
425                                           &m->gm_mech.gm_mo, &m->gm_mech.gm_mo_num);
426                         if (GSS_ERROR(major_status))
427                                 goto bad;
428                 } else {
429                         /* API-as-SPI compatibility */
430                         COMPATSYM(inquire_saslname_for_mech);
431                         COMPATSYM(inquire_mech_for_saslname);
432                         COMPATSYM(inquire_attrs_for_mech);
433                         COMPATSPISYM(acquire_cred_with_password);
434                 }
435
436                 /* pick up the oid sets of names */
437
438                 if (m->gm_mech.gm_inquire_names_for_mech)
439                         (*m->gm_mech.gm_inquire_names_for_mech)(&minor_status,
440                         &m->gm_mech.gm_mech_oid, &m->gm_name_types);
441
442                 if (m->gm_name_types == NULL)
443                         gss_create_empty_oid_set(&minor_status, &m->gm_name_types);
444
445                 HEIM_TAILQ_INSERT_TAIL(&_gss_mechs, m, gm_link);
446                 continue;
447
448         bad:
449                 if (m != NULL) {
450                         free(m->gm_mech.gm_compat);
451                         /* do not free OID, it has been interned */
452                         free((char *)m->gm_mech.gm_name);
453                         free(m);
454                 }
455                 if (so != NULL)
456                         dlclose(so);
457                 continue;
458         }
459         fclose(fp);
460
461 out:
462
463 #endif
464         add_builtin(__gss_sanon_initialize());
465         HEIMDAL_MUTEX_unlock(&_gss_mech_mutex);
466 }
467
468 gssapi_mech_interface
469 __gss_get_mechanism(gss_const_OID mech)
470 {
471         struct _gss_mech_switch *m;
472
473         _gss_load_mech();
474         HEIM_TAILQ_FOREACH(m, &_gss_mechs, gm_link) {
475                 if (gss_oid_equal(&m->gm_mech.gm_mech_oid, mech))
476                         return &m->gm_mech;
477         }
478         return NULL;
479 }
480
481 gss_OID
482 _gss_mg_support_mechanism(gss_const_OID mech)
483 {
484         struct _gss_mech_switch *m;
485
486         _gss_load_mech();
487         HEIM_TAILQ_FOREACH(m, &_gss_mechs, gm_link) {
488                 if (gss_oid_equal(&m->gm_mech.gm_mech_oid, mech))
489                         return m->gm_mech_oid;
490         }
491         return NULL;
492 }
493
494 enum mech_name_match {
495         MATCH_NONE = 0,
496         MATCH_COMPLETE,
497         MATCH_PARTIAL
498 };
499
500 static enum mech_name_match
501 match_mech_name(const char *gm_mech_name,
502                 const char *name,
503                 size_t namelen)
504 {
505         if (gm_mech_name == NULL)
506                 return MATCH_NONE;
507         else if (strcasecmp(gm_mech_name, name) == 0)
508                 return MATCH_COMPLETE;
509         else if (strncasecmp(gm_mech_name, name, namelen) == 0)
510                 return MATCH_PARTIAL;
511         else
512                 return MATCH_NONE;
513 }
514
515 /*
516  * Return an OID for a built-in or dynamically loaded mechanism. For
517  * API compatibility with previous versions, we treat "Kerberos 5"
518  * as an alias for "krb5". Unique partial matches are supported.
519  */
520 GSSAPI_LIB_FUNCTION gss_OID GSSAPI_CALLCONV
521 gss_name_to_oid(const char *name)
522 {
523         struct _gss_mech_switch *m, *partial = NULL;
524         gss_OID oid = GSS_C_NO_OID;
525         size_t namelen = strlen(name);
526
527         if (isdigit(name[0]) && _gss_string_to_oid(name, &oid) == 0)
528                 return oid;
529
530         _gss_load_mech();
531         HEIM_TAILQ_FOREACH(m, &_gss_mechs, gm_link) {
532                 enum mech_name_match match;
533
534                 match = match_mech_name(m->gm_mech.gm_name, name, namelen);
535                 if (match == MATCH_NONE &&
536                     gss_oid_equal(m->gm_mech_oid, GSS_KRB5_MECHANISM))
537                         match = match_mech_name("Kerberos 5", name, namelen);
538
539                 if (match == MATCH_COMPLETE)
540                         return m->gm_mech_oid;
541                 else if (match == MATCH_PARTIAL) {
542                         if (partial)
543                                 return NULL;
544                         else
545                                 partial = m;
546                 }
547         }
548
549         if (partial)
550                 return partial->gm_mech_oid;
551
552         return NULL;
553 }
554
555 GSSAPI_LIB_FUNCTION const char * GSSAPI_LIB_CALL
556 gss_oid_to_name(gss_const_OID oid)
557 {
558         struct _gss_mech_switch *m;
559
560         _gss_load_mech();
561         HEIM_TAILQ_FOREACH(m, &_gss_mechs, gm_link) {
562                 if (gss_oid_equal(m->gm_mech_oid, oid))
563                         return m->gm_mech.gm_name;
564         }
565
566         return NULL;
567 }
568
569 GSSAPI_LIB_FUNCTION uintptr_t GSSAPI_CALLCONV
570 gss_get_instance(const char *libname)
571 {
572     static const char *instance = "libgssapi";
573
574     if (strcmp(libname, "gssapi") == 0)
575         return (uintptr_t)instance;
576     else if (strcmp(libname, "krb5") == 0)
577         return krb5_get_instance(libname);
578
579     return 0;
580 }