Path: blob/main/crypto/krb5/src/tests/shlib/t_loader.c
34879 views
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/* tests/shlib/t_loader.c */2/*3* Copyright (C) 2005 by the Massachusetts Institute of Technology.4* All rights reserved.5*6* Export of this software from the United States of America may7* require a specific license from the United States Government.8* It is the responsibility of any person or organization contemplating9* export to obtain such a license before exporting.10*11* WITHIN THAT CONSTRAINT, permission to use, copy, modify, and12* distribute this software and its documentation for any purpose and13* without fee is hereby granted, provided that the above copyright14* notice appear in all copies and that both that copyright notice and15* this permission notice appear in supporting documentation, and that16* the name of M.I.T. not be used in advertising or publicity pertaining17* to distribution of the software without specific, written prior18* permission. Furthermore if you modify this software you must label19* your software as modified software and not distribute it in such a20* fashion that it might be confused with the original M.I.T. software.21* M.I.T. makes no representations about the suitability of22* this software for any purpose. It is provided "as is" without express23* or implied warranty.24*/2526#include "k5-platform.h"27#include "krb5.h"28#include "gssapi/gssapi.h"29#define HAVE_DLOPEN 13031static int verbose = 1;3233#ifdef HAVE_DLFCN_H34# include <dlfcn.h>35#endif36/* Solaris man page recommends link.h too */3738/* lazy = 1 means resolve symbols later, 0 means now; any39other flags we should be testing? On Windows, maybe?4041Return value is the library handle. On error, print a message and42exit. */43#define do_open(LIB,REV,FLAGS) do_open_1(LIB,REV,FLAGS,__LINE__)44static void *do_open_1(const char *libname, const char *rev, int lazy, int line);4546/* Look up a function symbol in the library and return a pointer.4748The return value may need casting to the correct type. On error,49print a message and exit. */50static void *get_sym_1(void *libhandle, const char *sym, int line);51#define get_sym(LIB, NAME) get_sym_1(LIB, NAME, __LINE__)52#define GET_FSYM(TYPE, LIB, NAME) ((TYPE) get_sym(LIB, NAME))53#define get_gfun(LIB, NAME) ((OM_uint32 KRB5_CALLCONV(*)()) get_sym(LIB, NAME))5455/* Close dynamically-opened library.5657If the OS reports an error in doing so, print a message and58exit. */59#define do_close(X) do_close_1(X, __LINE__)60static void do_close_1(void *libhandle, int line);6162#ifdef HAVE_DLOPEN6364#ifdef _AIX65# define SHLIB_SUFFIX ".a"66#else67# define SHLIB_SUFFIX ".so"68#endif6970#define HORIZ 257172static void *do_open_1(const char *libname, const char *rev,73int lazy, int line)74{75void *p;76char *namebuf;77int r;7879if (verbose)80printf("from line %d: do_open(%s)...%*s", line, libname,81HORIZ-strlen(libname), "");82#ifdef _AIX83r = asprintf(&namebuf, "lib%s%s", libname, SHLIB_SUFFIX);84#else85r = asprintf(&namebuf, "lib%s%s(shr.o.%s)", libname, SHLIB_SUFFIX, rev);86#endif87if (r < 0) {88perror("asprintf");89exit(1);90}9192#ifndef RTLD_MEMBER93#define RTLD_MEMBER 094#endif95p = dlopen(namebuf, (lazy ? RTLD_LAZY : RTLD_NOW) | RTLD_MEMBER);96if (p == 0) {97fprintf(stderr, "dlopen of %s failed: %s\n", namebuf, dlerror());98exit(1);99}100free(namebuf);101if (verbose)102printf("done: %p\n", p);103return p;104}105106#define SYM_PREFIX ""107static void *get_sym_1(void *libhandle, const char *symname, int line)108{109void *s;110111/* Bah. Fix this later, if we care. */112assert(strlen(SYM_PREFIX) == 0);113114if (verbose)115printf("from line %d: get_sym(%s)...%*s", line, symname,116HORIZ-strlen(symname), "");117118s = dlsym(libhandle, symname);119if (s == 0) {120fprintf(stderr, "symbol %s not found\n", symname);121exit(1);122}123if (verbose)124printf("done: %p\n", s);125return s;126}127128static void do_close_1(void *libhandle, int line)129{130if (verbose) {131char pbuf[3*sizeof(libhandle)+4];132snprintf(pbuf, sizeof(pbuf), "%p", libhandle);133printf("from line %d: do_close(%s)...%*s", line, pbuf,134HORIZ-1-strlen(pbuf), "");135}136if (dlclose(libhandle) != 0) {137fprintf(stderr, "dlclose failed: %s\n", dlerror());138exit(1);139}140if (verbose)141printf("done\n");142}143144#elif defined _WIN32145146static void *do_open(const char *libname, int lazy)147{148/* To be written? */149abort();150}151152static void *get_sym(void *libhandle, const char *symname)153{154abort();155}156157static void do_close(void *libhandle)158{159abort();160}161162#else163164static void *do_open(const char *libname, int lazy)165{166printf("don't know how to do dynamic loading here, punting\n");167exit(0);168}169170static void *get_sym(void *libhandle, const char *symname)171{172abort();173}174175static void do_close(void *libhandle)176{177abort();178}179180#endif181182int main(void)183{184void *celib, *k5lib, *gsslib, *celib2;185186(void) setvbuf(stdout, 0, _IONBF, 0);187188celib = do_open("com_err", "3.0", 0);189k5lib = do_open("krb5", "3.2", 0);190gsslib = do_open("gssapi_krb5", "2.2", 0);191celib2 = do_open("com_err", "3.0", 0);192do_close(celib2);193{194typedef krb5_error_code KRB5_CALLCONV (*ict)(krb5_context *);195typedef void KRB5_CALLCONV (*fct)(krb5_context);196197ict init_context = (ict) get_sym(k5lib, "krb5_init_context");198fct free_context = (fct) get_sym(k5lib, "krb5_free_context");199krb5_context ctx;200krb5_error_code err;201202#define CALLING(S) (verbose ? printf("at line %d: calling %s...%*s", __LINE__, #S, (int)(HORIZ+1-strlen(#S)), "") : 0)203#define DONE() (verbose ? printf("done\n") : 0)204205CALLING(krb5_init_context);206err = init_context(&ctx);207DONE();208if (err) {209fprintf(stderr, "error 0x%lx initializing context\n",210(unsigned long) err);211exit(1);212}213CALLING(krb5_free_context);214free_context(ctx);215DONE();216}217celib2 = do_open("com_err", "3.0", 0);218do_close(celib);219do_close(k5lib);220do_close(celib2);221do_close(gsslib);222223/* Test gssapi_krb5 without having loaded anything else. */224gsslib = do_open("gssapi_krb5", "2.2", 1);225{226OM_uint32 KRB5_CALLCONV (*init_sec_context)(OM_uint32 *, gss_cred_id_t,227gss_ctx_id_t *, gss_name_t,228gss_OID,229OM_uint32, OM_uint32,230gss_channel_bindings_t,231gss_buffer_t, gss_OID *,232gss_buffer_t,233OM_uint32 *, OM_uint32 *)234= get_gfun(gsslib, "gss_init_sec_context");235OM_uint32 KRB5_CALLCONV (*import_name)(OM_uint32 *, gss_buffer_t,236gss_OID, gss_name_t *)237= get_gfun(gsslib, "gss_import_name");238OM_uint32 KRB5_CALLCONV (*release_buffer)(OM_uint32 *, gss_buffer_t)239= get_gfun(gsslib, "gss_release_buffer");240OM_uint32 KRB5_CALLCONV (*release_name)(OM_uint32 *, gss_name_t *)241= get_gfun(gsslib, "gss_release_name");242OM_uint32 KRB5_CALLCONV (*delete_sec_context)(OM_uint32 *,243gss_ctx_id_t *,244gss_buffer_t)245= get_gfun(gsslib, "gss_delete_sec_context");246247OM_uint32 gmaj, gmin;248OM_uint32 retflags;249gss_ctx_id_t gctx = GSS_C_NO_CONTEXT;250gss_buffer_desc token;251gss_name_t target;252static gss_buffer_desc target_name_buf = {2539, "[email protected]"254};255static gss_OID_desc service_name = {25610, "\x2a\x86\x48\x86\xf7\x12\x01\x02\x01\x04"257};258259CALLING(gss_import_name);260gmaj = import_name(&gmin, &target_name_buf, &service_name, &target);261DONE();262if (gmaj != GSS_S_COMPLETE) {263fprintf(stderr,264"import_name reports error major 0x%lx minor 0x%lx(%ld)\n",265(unsigned long) gmaj, (unsigned long) gmin,266(signed long) gmin);267exit(1);268}269/* This will probably get different errors, depending on270whether we have tickets at the time. Doesn't matter much,271we're ignoring the error and testing whether we're doing272cleanup properly. (Though the internal cleanup needed in273the two cases might be different.) */274CALLING(gss_init_sec_context);275gmaj = init_sec_context(&gmin, GSS_C_NO_CREDENTIAL, &gctx, target,276GSS_C_NULL_OID, 0, 0, NULL, GSS_C_NO_BUFFER,277NULL, &token, &retflags, NULL);278DONE();279/* Ignore success/failure indication. */280if (token.length) {281CALLING(gss_release_buffer);282release_buffer(&gmin, &token);283DONE();284}285CALLING(gss_release_name);286release_name(&gmin, &target);287DONE();288if (gctx != GSS_C_NO_CONTEXT) {289CALLING(gss_delete_sec_context);290delete_sec_context(&gmin, gctx, GSS_C_NO_BUFFER);291DONE();292}293}294do_close(gsslib);295296/* Test gssapi_krb5 with com_err already loaded, then unload297com_err first. */298celib = do_open("com_err", "3.0", 1);299gsslib = do_open("gssapi_krb5", "2.2", 1);300{301OM_uint32 KRB5_CALLCONV (*init_sec_context)(OM_uint32 *, gss_cred_id_t,302gss_ctx_id_t *, gss_name_t,303gss_OID,304OM_uint32, OM_uint32,305gss_channel_bindings_t,306gss_buffer_t, gss_OID *,307gss_buffer_t,308OM_uint32 *, OM_uint32 *)309= get_gfun(gsslib, "gss_init_sec_context");310OM_uint32 KRB5_CALLCONV (*import_name)(OM_uint32 *, gss_buffer_t,311gss_OID, gss_name_t *)312= get_gfun(gsslib, "gss_import_name");313OM_uint32 KRB5_CALLCONV (*release_buffer)(OM_uint32 *, gss_buffer_t)314= get_gfun(gsslib, "gss_release_buffer");315OM_uint32 KRB5_CALLCONV (*release_name)(OM_uint32 *, gss_name_t *)316= get_gfun(gsslib, "gss_release_name");317OM_uint32 KRB5_CALLCONV (*delete_sec_context)(OM_uint32 *,318gss_ctx_id_t *,319gss_buffer_t)320= get_gfun(gsslib, "gss_delete_sec_context");321322OM_uint32 gmaj, gmin;323OM_uint32 retflags;324gss_ctx_id_t gctx = GSS_C_NO_CONTEXT;325gss_buffer_desc token;326gss_name_t target;327static gss_buffer_desc target_name_buf = {3289, "[email protected]"329};330static gss_OID_desc service_name = {33110, "\x2a\x86\x48\x86\xf7\x12\x01\x02\x01\x04"332};333334CALLING(gss_import_name);335gmaj = import_name(&gmin, &target_name_buf, &service_name, &target);336DONE();337if (gmaj != GSS_S_COMPLETE) {338fprintf(stderr,339"import_name reports error major 0x%lx minor 0x%lx(%ld)\n",340(unsigned long) gmaj, (unsigned long) gmin,341(signed long) gmin);342exit(1);343}344/* This will probably get different errors, depending on345whether we have tickets at the time. Doesn't matter much,346we're ignoring the error and testing whether we're doing347cleanup properly. (Though the internal cleanup needed in348the two cases might be different.) */349CALLING(gss_init_sec_context);350gmaj = init_sec_context(&gmin, GSS_C_NO_CREDENTIAL, &gctx, target,351GSS_C_NULL_OID, 0, 0, NULL, GSS_C_NO_BUFFER,352NULL, &token, &retflags, NULL);353DONE();354/* Ignore success/failure indication. */355if (token.length) {356CALLING(gss_release_buffer);357release_buffer(&gmin, &token);358DONE();359}360CALLING(gss_release_name);361release_name(&gmin, &target);362DONE();363if (gctx != GSS_C_NO_CONTEXT) {364CALLING(gss_delete_sec_context);365delete_sec_context(&gmin, gctx, GSS_C_NO_BUFFER);366DONE();367}368}369do_close(celib);370do_close(gsslib);371372return 0;373}374375376