1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* tests/shlib/t_loader.c */
4 * Copyright (C) 2005 by the Massachusetts Institute of Technology.
7 * Export of this software from the United States of America may
8 * require a specific license from the United States Government.
9 * It is the responsibility of any person or organization contemplating
10 * export to obtain such a license before exporting.
12 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13 * distribute this software and its documentation for any purpose and
14 * without fee is hereby granted, provided that the above copyright
15 * notice appear in all copies and that both that copyright notice and
16 * this permission notice appear in supporting documentation, and that
17 * the name of M.I.T. not be used in advertising or publicity pertaining
18 * to distribution of the software without specific, written prior
19 * permission. Furthermore if you modify this software you must label
20 * your software as modified software and not distribute it in such a
21 * fashion that it might be confused with the original M.I.T. software.
22 * M.I.T. makes no representations about the suitability of
23 * this software for any purpose. It is provided "as is" without express
24 * or implied warranty.
27 #include "k5-platform.h"
29 #include "gssapi/gssapi.h"
32 static int verbose = 1;
37 /* Solaris man page recommends link.h too */
39 /* lazy = 1 means resolve symbols later, 0 means now; any
40 other flags we should be testing? On Windows, maybe?
42 Return value is the library handle. On error, print a message and
44 #define do_open(LIB,REV,FLAGS) do_open_1(LIB,REV,FLAGS,__LINE__)
45 static void *do_open_1(const char *libname, const char *rev, int lazy, int line);
47 /* Look up a function symbol in the library and return a pointer.
49 The return value may need casting to the correct type. On error,
50 print a message and exit. */
51 static void *get_sym_1(void *libhandle, const char *sym, int line);
52 #define get_sym(LIB, NAME) get_sym_1(LIB, NAME, __LINE__)
53 #define GET_FSYM(TYPE, LIB, NAME) ((TYPE) get_sym(LIB, NAME))
54 #define get_gfun(LIB, NAME) ((OM_uint32 KRB5_CALLCONV(*)()) get_sym(LIB, NAME))
56 /* Close dynamically-opened library.
58 If the OS reports an error in doing so, print a message and
60 #define do_close(X) do_close_1(X, __LINE__)
61 static void do_close_1(void *libhandle, int line);
66 # define SHLIB_SUFFIX ".a"
68 # define SHLIB_SUFFIX ".so"
73 static void *do_open_1(const char *libname, const char *rev,
81 printf("from line %d: do_open(%s)...%*s", line, libname,
82 HORIZ-strlen(libname), "");
84 r = asprintf(&namebuf, "lib%s%s", libname, SHLIB_SUFFIX);
86 r = asprintf(&namebuf, "lib%s%s(shr.o.%s)", libname, SHLIB_SUFFIX, rev);
96 p = dlopen(namebuf, (lazy ? RTLD_LAZY : RTLD_NOW) | RTLD_MEMBER);
98 fprintf(stderr, "dlopen of %s failed: %s\n", namebuf, dlerror());
103 printf("done: %p\n", p);
107 #define SYM_PREFIX ""
108 static void *get_sym_1(void *libhandle, const char *symname, int line)
112 /* Bah. Fix this later, if we care. */
113 assert(strlen(SYM_PREFIX) == 0);
116 printf("from line %d: get_sym(%s)...%*s", line, symname,
117 HORIZ-strlen(symname), "");
119 s = dlsym(libhandle, symname);
121 fprintf(stderr, "symbol %s not found\n", symname);
125 printf("done: %p\n", s);
129 static void do_close_1(void *libhandle, int line)
132 char pbuf[3*sizeof(libhandle)+4];
133 snprintf(pbuf, sizeof(pbuf), "%p", libhandle);
134 printf("from line %d: do_close(%s)...%*s", line, pbuf,
135 HORIZ-1-strlen(pbuf), "");
137 if (dlclose(libhandle) != 0) {
138 fprintf(stderr, "dlclose failed: %s\n", dlerror());
147 static void *do_open(const char *libname, int lazy)
153 static void *get_sym(void *libhandle, const char *symname)
158 static void do_close(void *libhandle)
165 static void *do_open(const char *libname, int lazy)
167 printf("don't know how to do dynamic loading here, punting\n");
171 static void *get_sym(void *libhandle, const char *symname)
176 static void do_close(void *libhandle)
185 void *celib, *k5lib, *gsslib, *celib2;
187 (void) setvbuf(stdout, 0, _IONBF, 0);
190 /* Simplest test: Load, then unload out of order. */
191 celib = do_open("com_err", "3.0", 0);
192 k5lib = do_open("krb5", "3.2", 0);
193 gsslib = do_open("gssapi_krb5", "2.2", 0);
194 celib2 = do_open("com_err", "3.0", 0);
201 celib = do_open("com_err", "3.0", 0);
202 k5lib = do_open("krb5", "3.2", 0);
203 gsslib = do_open("gssapi_krb5", "2.2", 0);
204 celib2 = do_open("com_err", "3.0", 0);
207 typedef krb5_error_code KRB5_CALLCONV (*ict)(krb5_context *);
208 typedef void KRB5_CALLCONV (*fct)(krb5_context);
210 ict init_context = (ict) get_sym(k5lib, "krb5_init_context");
211 fct free_context = (fct) get_sym(k5lib, "krb5_free_context");
215 #define CALLING(S) (verbose ? printf("at line %d: calling %s...%*s", __LINE__, #S, (int)(HORIZ+1-strlen(#S)), "") : 0)
216 #define DONE() (verbose ? printf("done\n") : 0)
218 CALLING(krb5_init_context);
219 err = init_context(&ctx);
222 fprintf(stderr, "error 0x%lx initializing context\n",
223 (unsigned long) err);
226 CALLING(krb5_free_context);
230 celib2 = do_open("com_err", "3.0", 0);
236 /* Test gssapi_krb5 without having loaded anything else. */
237 gsslib = do_open("gssapi_krb5", "2.2", 1);
239 OM_uint32 KRB5_CALLCONV (*init_sec_context)(OM_uint32 *, gss_cred_id_t,
240 gss_ctx_id_t *, gss_name_t,
242 OM_uint32, OM_uint32,
243 gss_channel_bindings_t,
244 gss_buffer_t, gss_OID *,
246 OM_uint32 *, OM_uint32 *)
247 = get_gfun(gsslib, "gss_init_sec_context");
248 OM_uint32 KRB5_CALLCONV (*import_name)(OM_uint32 *, gss_buffer_t,
249 gss_OID, gss_name_t *)
250 = get_gfun(gsslib, "gss_import_name");
251 OM_uint32 KRB5_CALLCONV (*release_buffer)(OM_uint32 *, gss_buffer_t)
252 = get_gfun(gsslib, "gss_release_buffer");
253 OM_uint32 KRB5_CALLCONV (*release_name)(OM_uint32 *, gss_name_t *)
254 = get_gfun(gsslib, "gss_release_name");
255 OM_uint32 KRB5_CALLCONV (*delete_sec_context)(OM_uint32 *,
258 = get_gfun(gsslib, "gss_delete_sec_context");
260 OM_uint32 gmaj, gmin;
262 gss_ctx_id_t gctx = GSS_C_NO_CONTEXT;
263 gss_buffer_desc token;
265 static gss_buffer_desc target_name_buf = {
268 static gss_OID_desc service_name = {
269 10, "\x2a\x86\x48\x86\xf7\x12\x01\x02\x01\x04"
272 CALLING(gss_import_name);
273 gmaj = import_name(&gmin, &target_name_buf, &service_name, &target);
275 if (gmaj != GSS_S_COMPLETE) {
277 "import_name reports error major 0x%lx minor 0x%lx(%ld)\n",
278 (unsigned long) gmaj, (unsigned long) gmin,
282 /* This will probably get different errors, depending on
283 whether we have tickets at the time. Doesn't matter much,
284 we're ignoring the error and testing whether we're doing
285 cleanup properly. (Though the internal cleanup needed in
286 the two cases might be different.) */
287 CALLING(gss_init_sec_context);
288 gmaj = init_sec_context(&gmin, GSS_C_NO_CREDENTIAL, &gctx, target,
289 GSS_C_NULL_OID, 0, 0, NULL, GSS_C_NO_BUFFER,
290 NULL, &token, &retflags, NULL);
292 /* Ignore success/failure indication. */
294 CALLING(gss_release_buffer);
295 release_buffer(&gmin, &token);
298 CALLING(gss_release_name);
299 release_name(&gmin, &target);
301 if (gctx != GSS_C_NO_CONTEXT) {
302 CALLING(gss_delete_sec_context);
303 delete_sec_context(&gmin, gctx, GSS_C_NO_BUFFER);
309 /* Test gssapi_krb5 with com_err already loaded, then unload
311 celib = do_open("com_err", "3.0", 1);
312 gsslib = do_open("gssapi_krb5", "2.2", 1);
314 OM_uint32 KRB5_CALLCONV (*init_sec_context)(OM_uint32 *, gss_cred_id_t,
315 gss_ctx_id_t *, gss_name_t,
317 OM_uint32, OM_uint32,
318 gss_channel_bindings_t,
319 gss_buffer_t, gss_OID *,
321 OM_uint32 *, OM_uint32 *)
322 = get_gfun(gsslib, "gss_init_sec_context");
323 OM_uint32 KRB5_CALLCONV (*import_name)(OM_uint32 *, gss_buffer_t,
324 gss_OID, gss_name_t *)
325 = get_gfun(gsslib, "gss_import_name");
326 OM_uint32 KRB5_CALLCONV (*release_buffer)(OM_uint32 *, gss_buffer_t)
327 = get_gfun(gsslib, "gss_release_buffer");
328 OM_uint32 KRB5_CALLCONV (*release_name)(OM_uint32 *, gss_name_t *)
329 = get_gfun(gsslib, "gss_release_name");
330 OM_uint32 KRB5_CALLCONV (*delete_sec_context)(OM_uint32 *,
333 = get_gfun(gsslib, "gss_delete_sec_context");
335 OM_uint32 gmaj, gmin;
337 gss_ctx_id_t gctx = GSS_C_NO_CONTEXT;
338 gss_buffer_desc token;
340 static gss_buffer_desc target_name_buf = {
343 static gss_OID_desc service_name = {
344 10, "\x2a\x86\x48\x86\xf7\x12\x01\x02\x01\x04"
347 CALLING(gss_import_name);
348 gmaj = import_name(&gmin, &target_name_buf, &service_name, &target);
350 if (gmaj != GSS_S_COMPLETE) {
352 "import_name reports error major 0x%lx minor 0x%lx(%ld)\n",
353 (unsigned long) gmaj, (unsigned long) gmin,
357 /* This will probably get different errors, depending on
358 whether we have tickets at the time. Doesn't matter much,
359 we're ignoring the error and testing whether we're doing
360 cleanup properly. (Though the internal cleanup needed in
361 the two cases might be different.) */
362 CALLING(gss_init_sec_context);
363 gmaj = init_sec_context(&gmin, GSS_C_NO_CREDENTIAL, &gctx, target,
364 GSS_C_NULL_OID, 0, 0, NULL, GSS_C_NO_BUFFER,
365 NULL, &token, &retflags, NULL);
367 /* Ignore success/failure indication. */
369 CALLING(gss_release_buffer);
370 release_buffer(&gmin, &token);
373 CALLING(gss_release_name);
374 release_name(&gmin, &target);
376 if (gctx != GSS_C_NO_CONTEXT) {
377 CALLING(gss_delete_sec_context);
378 delete_sec_context(&gmin, gctx, GSS_C_NO_BUFFER);