diff --git a/configure.in b/configure.in index 965cf207..747f8306 100644 --- a/configure.in +++ b/configure.in @@ -140,7 +140,7 @@ dnl Automatically update the libtool script if it becomes out-of-date. dnl Checks for header files. AC_HEADER_STDC AC_HEADER_SYS_WAIT -AC_CHECK_HEADERS([errno.h fcntl.h malloc.h stdlib.h string.h sys/time.h unistd.h libgen.h utmp.h getopt.h security/pam_appl.h security/_pam_macros.h readline/readline.h]) +AC_CHECK_HEADERS([errno.h fcntl.h malloc.h stdlib.h string.h sys/time.h unistd.h libgen.h utmp.h getopt.h dlfcn.h security/pam_appl.h security/_pam_macros.h readline/readline.h]) dnl Checks for typedefs, structures, and compiler characteristics. AC_C_CONST diff --git a/src/libopensc/Makefile.am b/src/libopensc/Makefile.am index 9a07e1f6..c6c61eff 100644 --- a/src/libopensc/Makefile.am +++ b/src/libopensc/Makefile.am @@ -17,8 +17,8 @@ INCLUDES = $(PCSC_FLAGS) bin_SCRIPTS = opensc-config lib_LTLIBRARIES = libopensc.la -libopensc_la_SOURCES = sc.c ctx.c asn1.c base64.c sec.c log.c \ - card.c iso7816.c dir.c \ +libopensc_la_SOURCES = sc.c ctx.c module.c asn1.c log.c base64.c \ + sec.c card.c iso7816.c dir.c \ pkcs15.c pkcs15-cert.c pkcs15-pin.c \ pkcs15-prkey.c pkcs15-pubkey.c pkcs15-sec.c \ pkcs15-cache.c $(PCSC_SRC) reader-ctapi.c \ diff --git a/src/libopensc/ctx.c b/src/libopensc/ctx.c index e40056a3..78ccdbfe 100644 --- a/src/libopensc/ctx.c +++ b/src/libopensc/ctx.c @@ -47,26 +47,26 @@ struct _sc_driver_entry { }; static const struct _sc_driver_entry internal_card_drivers[] = { - { "setcos", sc_get_setcos_driver }, - { "miocos", sc_get_miocos_driver }, - { "flex", sc_get_flex_driver }, - { "tcos", sc_get_tcos_driver }, - { "emv", sc_get_emv_driver }, + { "setcos", (void *) sc_get_setcos_driver, NULL }, + { "miocos", (void *) sc_get_miocos_driver, NULL }, + { "flex", (void *) sc_get_flex_driver, NULL }, + { "tcos", (void *) sc_get_tcos_driver, NULL }, + { "emv", (void *) sc_get_emv_driver, NULL }, #ifdef HAVE_OPENSSL - { "gpk", sc_get_gpk_driver }, + { "gpk", (void *) sc_get_gpk_driver, NULL }, #endif /* The default driver should be last, as it handles all the * unrecognized cards. */ - { "default", sc_get_default_driver }, - { NULL } + { "default", (void *) sc_get_default_driver, NULL }, + { NULL, NULL, NULL } }; static const struct _sc_driver_entry internal_reader_drivers[] = { #ifdef HAVE_LIBPCSCLITE - { "pcsc", sc_get_pcsc_driver }, + { "pcsc", (void *) sc_get_pcsc_driver, NULL }, #endif - { "ctapi", sc_get_ctapi_driver }, - { NULL } + { "ctapi", (void *) sc_get_ctapi_driver, NULL }, + { NULL, NULL, NULL } }; struct _sc_ctx_options { @@ -219,7 +219,7 @@ static int load_reader_drivers(struct sc_context *ctx, ent = &opts->rdrv[i]; for (j = 0; internal_reader_drivers[j].name != NULL; j++) if (strcmp(ent->name, internal_reader_drivers[j].name) == 0) { - func = internal_reader_drivers[j].func; + func = (const struct sc_reader_driver * (*)(void)) internal_reader_drivers[j].func; break; } if (func == NULL) { @@ -252,7 +252,7 @@ static int load_card_drivers(struct sc_context *ctx, ent = &opts->cdrv[i]; for (j = 0; internal_card_drivers[j].name != NULL; j++) if (strcmp(ent->name, internal_card_drivers[j].name) == 0) { - func = internal_card_drivers[j].func; + func = (const struct sc_card_driver * (*)(void)) internal_card_drivers[j].func; break; } if (func == NULL) { diff --git a/src/libopensc/internal.h b/src/libopensc/internal.h index d60ec341..c49ac77a 100644 --- a/src/libopensc/internal.h +++ b/src/libopensc/internal.h @@ -56,4 +56,8 @@ int _sc_card_add_rsa_alg(struct sc_card *card, unsigned int key_length, struct sc_algorithm_info * _sc_card_find_rsa_alg(struct sc_card *card, unsigned int key_length); +int sc_module_open(struct sc_context *ctx, void **mod_handle, const char *filename); +int sc_module_close(struct sc_context *ctx, void **mod_handle); +int sc_module_get_address(struct sc_context *ctx, void *mod_handle, void **sym_address, const char *sym_name); + #endif diff --git a/src/libopensc/module.c b/src/libopensc/module.c new file mode 100644 index 00000000..4e5e6b83 --- /dev/null +++ b/src/libopensc/module.c @@ -0,0 +1,100 @@ +/* + * module.c: Dynamic linking loader + * + * Copyright (C) 2002 Antti Tapaninen + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif +#include "sc-internal.h" +#include "sc-log.h" +#include +#include +#include +#ifdef HAVE_DLFCN_H +#include +#endif + +int sc_module_open(struct sc_context *ctx, void **mod_handle, const char *filename) +{ + const char *error; + + assert(ctx != NULL); + + if (!filename) + return SC_ERROR_UNKNOWN; + + *mod_handle = NULL; + *mod_handle = dlopen(filename, RTLD_LAZY); + + if ((error = dlerror()) != NULL) { + if (ctx->debug) + debug(ctx, "sc_module_open: %s", error); + return SC_ERROR_UNKNOWN; + } + return SC_SUCCESS; +} + +int sc_module_close(struct sc_context *ctx, void **mod_handle) +{ + const char *error; + + assert(ctx != NULL); + + if (!*mod_handle) + return SC_ERROR_UNKNOWN; + + dlclose(*mod_handle); + *mod_handle = NULL; + + if ((error = dlerror()) != NULL) { + if (ctx->debug) + debug(ctx, "sc_module_close: %s", error); + return SC_ERROR_UNKNOWN; + } + return SC_SUCCESS; +} + +int sc_module_get_address(struct sc_context *ctx, void *mod_handle, void **sym_address, const char *sym_name) +{ + const char *error; + char name[256]; + + assert(ctx != NULL); + + if (!mod_handle || !sym_name) + return SC_ERROR_UNKNOWN; + + /* Some platforms might need a leading underscore for the symbol */ + name[0] = '_'; + strncpy(&name[1], sym_name, sizeof(name) - 1); + + *sym_address = NULL; + *sym_address = dlsym(mod_handle, name); + + /* Failed? Try again without the leading underscore */ + if (*sym_address == NULL) + *sym_address = dlsym(mod_handle, sym_name); + + if ((error = dlerror()) != NULL) { + if (ctx->debug) + debug(ctx, "sc_module_get_address: %s", error); + return SC_ERROR_UNKNOWN; + } + return SC_SUCCESS; +} diff --git a/src/libopensc/reader-ctapi.c b/src/libopensc/reader-ctapi.c index 1c40390a..fcaa96c0 100644 --- a/src/libopensc/reader-ctapi.c +++ b/src/libopensc/reader-ctapi.c @@ -24,7 +24,6 @@ #include #include #include -#include #define GET_SLOT_PTR(s, i) (&(s)->slot[(i)]) #define GET_PRIV_DATA(r) ((struct ctapi_private_data *) (r)->drv_data) @@ -57,6 +56,7 @@ struct ctapi_private_data { }; struct ctapi_slot_data { + void *filler; }; static int refresh_slot_attributes(struct sc_reader *reader, @@ -243,21 +243,21 @@ static int ctapi_load_module(struct sc_context *ctx, } val = conf->name->data; - dlh = dlopen(val, RTLD_LAZY); - if (dlh == NULL) { + r = sc_module_open(ctx, &dlh, val); + if (r != SC_SUCCESS) { error(ctx, "Unable to open shared library '%s'\n", val); return -1; } - func = dlsym(dlh, "CT_init"); - if (func == NULL) + r = sc_module_get_address(ctx, dlh, &func, "CT_init"); + if (r != SC_SUCCESS) goto symerr; funcs.CT_init = func; - func = dlsym(dlh, "CT_close"); - if (func == NULL) + r = sc_module_get_address(ctx, dlh, &func, "CT_close"); + if (r != SC_SUCCESS) goto symerr; funcs.CT_close = func; - func = dlsym(dlh, "CT_data"); - if (func == NULL) + r = sc_module_get_address(ctx, dlh, &func, "CT_data"); + if (r != SC_SUCCESS) goto symerr; funcs.CT_data = func; mod = add_module(gpriv, val, dlh); @@ -310,7 +310,7 @@ static int ctapi_load_module(struct sc_context *ctx, return 0; symerr: error(ctx, "Unable to resolve CT-API symbols.\n"); - dlclose(dlh); + sc_module_close(ctx, &dlh); return -1; } diff --git a/src/libopensc/sc-internal.h b/src/libopensc/sc-internal.h index d60ec341..c49ac77a 100644 --- a/src/libopensc/sc-internal.h +++ b/src/libopensc/sc-internal.h @@ -56,4 +56,8 @@ int _sc_card_add_rsa_alg(struct sc_card *card, unsigned int key_length, struct sc_algorithm_info * _sc_card_find_rsa_alg(struct sc_card *card, unsigned int key_length); +int sc_module_open(struct sc_context *ctx, void **mod_handle, const char *filename); +int sc_module_close(struct sc_context *ctx, void **mod_handle); +int sc_module_get_address(struct sc_context *ctx, void *mod_handle, void **sym_address, const char *sym_name); + #endif diff --git a/src/openssh/Makefile.am b/src/openssh/Makefile.am index 6068c556..57509f58 100644 --- a/src/openssh/Makefile.am +++ b/src/openssh/Makefile.am @@ -7,8 +7,6 @@ EXTRA_DIST = openssh-3.0.2p1-patch.diff if HAVE_SSL bin_PROGRAMS = opensc-ssh -else -bin_PROGRAMS = endif opensc_ssh_SOURCES = opensc-ssh.c diff --git a/src/pam/Makefile.am b/src/pam/Makefile.am index 35d298ad..25d307a5 100644 --- a/src/pam/Makefile.am +++ b/src/pam/Makefile.am @@ -11,9 +11,6 @@ SRC = pam_opensc.c pam_support.c pam_support.h if HAVE_PAM lib_LTLIBRARIES = pam_opensc.la noinst_PROGRAMS = pam_opensc-test -else -lib_LTLIBRARIES = -noinst_PROGRAMS = endif pam_opensc_la_SOURCES = $(SRC) diff --git a/src/scam/Makefile.am b/src/scam/Makefile.am index 84b3e378..4b9115e7 100644 --- a/src/scam/Makefile.am +++ b/src/scam/Makefile.am @@ -4,36 +4,24 @@ INCLUDES = @CFLAGS_PCSC@ @CFLAGS_OPENSC@ -I../scrandom -I../scldap if HAVE_PAM PAM = libpamscam.la -else -PAM = endif if HAVE_SIA SIA = libsiascam.la -else -SIA = endif lib_LTLIBRARIES = $(PAM) $(SIA) if HAVE_LDAP LDAP_LTLIBS = @LIBSCLDAP@ -else -LDAP_LTLIBS = endif if HAVE_SSL SSL_LTLIBS = @LIBCRYPTO@ -else -SSL_LTLIBS = endif # Temporary if HAVE_SCIDI SCIDI_LTLIBS = @LIBSSP@ SCIDI_SRC = ../../../scam/sp.c SCIDI_INC = @CFLAGS_SSP@ -else -SCIDI_LTLIBS = -SCIDI_SRC = -SCIDI_INC = endif LDFLAGS = @LIBOPENSC@ @LIBSCRANDOM@ $(LDAP_LTLIBS) $(SSL_LTLIBS) $(SCIDI_LTLIBS) diff --git a/src/scldap/Makefile.am b/src/scldap/Makefile.am index 7c2a5394..7e8b7e6b 100644 --- a/src/scldap/Makefile.am +++ b/src/scldap/Makefile.am @@ -5,9 +5,6 @@ include_HEADERS = scldap.h if HAVE_LDAP LDAP_LTLIB = libscldap.la LDAP_PROGRAM = test-ldap -else -LDAP_LTLIB = -LDAP_PROGRAM = endif lib_LTLIBRARIES = $(LDAP_LTLIB) diff --git a/src/scldap/scldap.c b/src/scldap/scldap.c index 65b66810..779c06d7 100644 --- a/src/scldap/scldap.c +++ b/src/scldap/scldap.c @@ -42,6 +42,8 @@ #endif #include "scldap.h" +extern char **environ; + static void scldap_parse_block(scldap_context * ctx, scconf_block * block, const char *cardprefix) { scconf_block **blocks = NULL; @@ -760,7 +762,7 @@ int scldap_search(scldap_context * ctx, const char *entry, scldap_result *_result = *result; int rc, entrynum = -1; char *pattern = NULL; - char **env = NULL; + char **keepenv = NULL; if (_result || !ctx) { return -1; @@ -772,14 +774,14 @@ int scldap_search(scldap_context * ctx, const char *entry, if (!ctx->entry[entrynum].ldaphost) { return -1; } - env = __environ; - __environ = NULL; + keepenv = environ; + environ = NULL; if ((ld = ldap_init(ctx->entry[entrynum].ldaphost, ctx->entry[entrynum].ldapport)) == NULL) { - __environ = env; + environ = keepenv; perror("ldap_init"); return -1; } - __environ = env; + environ = keepenv; if (ldap_bind_s(ld, ctx->entry[entrynum].binddn, ctx->entry[entrynum].passwd, LDAP_AUTH_SIMPLE) != LDAP_SUCCESS) { ldap_perror(ld, "ldap_bind"); ldap_unbind(ld); diff --git a/src/scrandom/Makefile.am b/src/scrandom/Makefile.am index cb49b836..e6625a8b 100644 --- a/src/scrandom/Makefile.am +++ b/src/scrandom/Makefile.am @@ -4,8 +4,6 @@ include_HEADERS = scrandom.h if HAVE_SSL SSL_LTLIB = @LIBCRYPTO@ -else -SSL_LTLIB = endif lib_LTLIBRARIES = libscrandom.la diff --git a/src/sia/Makefile.am b/src/sia/Makefile.am index 5bcd8bd7..9f8e9f60 100644 --- a/src/sia/Makefile.am +++ b/src/sia/Makefile.am @@ -11,9 +11,6 @@ SRC = sia_opensc.c sia_support.c sia_support.h if HAVE_SIA lib_LTLIBRARIES = libsia_opensc.la noinst_PROGRAMS = test-sia -else -lib_LTLIBRARIES = -noinst_PROGRAMS = endif libsia_opensc_la_SOURCES = $(SRC) diff --git a/src/signer/Makefile.am b/src/signer/Makefile.am index d33e7678..a6bfab6c 100644 --- a/src/signer/Makefile.am +++ b/src/signer/Makefile.am @@ -7,10 +7,9 @@ EXTRA_DIST = testprog.c INCLUDES = @CFLAGS_OPENSC@ @CFLAGS_ASSUAN@ plugindir = $(PLUGINDIR) + if HAVE_SSL plugin_LTLIBRARIES = opensc-signer.la -else -plugin_LTLIBRARIES = endif opensc_signer_la_LDFLAGS = -module -avoid-version