static int print_default_module(void) { const scconf_block *pam_pkcs11; scconf_context *ctx = NULL; int result = 1; /* * read the base pam_pkcs11.conf */ ctx = scconf_new(PAM_PKCS11_CONF); if (ctx == NULL) { goto bail; } if (scconf_parse(ctx) <= 0) { goto bail; } pam_pkcs11 = scconf_find_block(ctx, NULL, "pam_pkcs11"); if (!pam_pkcs11) { goto bail; } printf("%s\n", scconf_get_str(pam_pkcs11, "use_pkcs11_module", "")); result = 0; bail: if (ctx) { scconf_free(ctx); } ctx = NULL; return result; }
static int list_modules(void) { const scconf_block *pam_pkcs11; scconf_block **pkcs11_blocks; scconf_context *ctx = NULL; int i; int result = 1; /* * loop through looking for smart card entries */ ctx = scconf_new(PAM_PKCS11_CONF); if (ctx == NULL) { goto bail; } if (scconf_parse(ctx) <= 0 ) { goto bail; } pam_pkcs11 = scconf_find_block(ctx, NULL, "pam_pkcs11"); if (!pam_pkcs11) { goto bail; } pkcs11_blocks = scconf_find_blocks(ctx, pam_pkcs11, "pkcs11_module", NULL); if (!pkcs11_blocks) { goto bail; } /* list only those smart cards which are actually installed */ for (i=0; pkcs11_blocks[i]; i++) { void *libhandle; const char *path = scconf_get_str(pkcs11_blocks[i], "module", NULL); /* check to see if the module exists on the system */ if (!path || *path == 0) { continue; } /* verify the module exists */ if ((libhandle=dlopen(path, RTLD_LAZY)) != NULL) { dlclose(libhandle); if (pkcs11_blocks[i] && pkcs11_blocks[i]->name && pkcs11_blocks[i]->name->data) { printf("%s\n", pkcs11_blocks[i]->name->data); } } } result = 0; bail: if (ctx) { scconf_free(ctx); } return result; }
int sc_profile_load(struct sc_profile *profile, const char *filename) { struct sc_context *ctx = profile->card->ctx; scconf_context *conf; const char *profile_dir = NULL; char path[PATH_MAX]; int res = 0, i; for (i = 0; ctx->conf_blocks[i]; i++) { profile_dir = scconf_get_str(ctx->conf_blocks[i], "profile_dir", NULL); if (profile_dir) break; } if (!profile_dir) { profile_dir = SC_PKCS15_PROFILE_DIRECTORY; } sc_debug(ctx, "Using profile directory '%s'.", profile_dir); #ifdef _WIN32 snprintf(path, sizeof(path), "%s\\%s.%s", profile_dir, filename, SC_PKCS15_PROFILE_SUFFIX); #else /* _WIN32 */ snprintf(path, sizeof(path), "%s/%s.%s", profile_dir, filename, SC_PKCS15_PROFILE_SUFFIX); #endif /* _WIN32 */ if (profile->card->ctx->debug >= 2) { sc_debug(profile->card->ctx, "Trying profile file %s", path); } conf = scconf_new(path); res = scconf_parse(conf); if (res > 0 && profile->card->ctx->debug >= 2) { sc_debug(profile->card->ctx, "profile %s loaded ok", path); } if (res < 0) return SC_ERROR_FILE_NOT_FOUND; if (res == 0) { /* FIXME - we may want to display conf->errmsg here. */ return SC_ERROR_SYNTAX_ERROR; } res = process_conf(profile, conf); scconf_free(conf); return res; }
static int print_card_remove_action(void) { const scconf_block *pkcs11_eventmgr; scconf_block **event_blocks = NULL; scconf_context *ctx = NULL; const scconf_list *actionList = NULL; int result = 1; /* * read the pkcs11_eventmgr.conf to get our action */ ctx = scconf_new(EVENTMGR_CONF); if (ctx == NULL) { goto bail; } if (scconf_parse(ctx) <= 0) { goto bail; } pkcs11_eventmgr = scconf_find_block(ctx, NULL, "pkcs11_eventmgr"); if (!pkcs11_eventmgr) { goto bail; } event_blocks = scconf_find_blocks(ctx, pkcs11_eventmgr, "event", "card_remove"); if (!event_blocks || !event_blocks[0]) { goto bail; } actionList = scconf_find_list(event_blocks[0],"action"); if (actionList) { char *lst = scconf_list_strdup(actionList, "\n"); if (lst != NULL) { printf("%s\n", lst); free(lst); } } result = 0; bail: if (event_blocks) { free(event_blocks); } if (ctx) { scconf_free(ctx); } return result; }
int sc_release_context(sc_context_t *ctx) { unsigned int i; if (ctx == NULL) { return SC_ERROR_INVALID_ARGUMENTS; } SC_FUNC_CALLED(ctx, SC_LOG_DEBUG_VERBOSE); while (list_size(&ctx->readers)) { sc_reader_t *rdr = (sc_reader_t *) list_get_at(&ctx->readers, 0); _sc_delete_reader(ctx, rdr); } if (ctx->reader_driver->ops->finish != NULL) ctx->reader_driver->ops->finish(ctx); for (i = 0; ctx->card_drivers[i]; i++) { struct sc_card_driver *drv = ctx->card_drivers[i]; if (drv->atr_map) _sc_free_atr(ctx, drv); if (drv->dll) sc_dlclose(drv->dll); } if (ctx->preferred_language != NULL) free(ctx->preferred_language); if (ctx->mutex != NULL) { int r = sc_mutex_destroy(ctx, ctx->mutex); if (r != SC_SUCCESS) { sc_log(ctx, "unable to destroy mutex"); return r; } } if (ctx->conf != NULL) scconf_free(ctx->conf); if (ctx->debug_file && (ctx->debug_file != stdout && ctx->debug_file != stderr)) fclose(ctx->debug_file); if (ctx->debug_filename != NULL) free(ctx->debug_filename); if (ctx->app_name != NULL) free(ctx->app_name); list_destroy(&ctx->readers); sc_mem_clear(ctx, sizeof(*ctx)); free(ctx); return SC_SUCCESS; }
static int set_card_remove_action(const char *act) { scconf_block *pkcs11_eventmgr; scconf_block **insert_blocks = NULL; scconf_context *ctx = NULL; int result = 1; /* * write out pkcs11_eventmgr.conf */ ctx = scconf_new(EVENTMGR_CONF); if (ctx == NULL) { goto bail; } if (scconf_parse(ctx) <= 0) { goto bail; } pkcs11_eventmgr = (scconf_block *) scconf_find_block(ctx, NULL, "pkcs11_eventmgr"); if (!pkcs11_eventmgr) { goto bail; } insert_blocks = scconf_find_blocks(ctx, pkcs11_eventmgr, "event", "card_remove"); if (!insert_blocks || !insert_blocks[0]) { goto bail; } scconf_replace_str_list(insert_blocks[0], "action", act); result = scconf_write(ctx, NULL); bail: if (insert_blocks) { free(insert_blocks); } if (ctx) { scconf_free(ctx); } return result; }
static void process_config_file(sc_context_t *ctx, struct _sc_ctx_options *opts) { int i, r, count = 0; scconf_block **blocks; const char *conf_path = NULL; #ifdef _WIN32 char temp_path[PATH_MAX]; DWORD temp_len; long rc; HKEY hKey; #endif memset(ctx->conf_blocks, 0, sizeof(ctx->conf_blocks)); #ifdef _WIN32 conf_path = getenv("OPENSC_CONF"); if (!conf_path) { rc = RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\OpenSC Project\\OpenSC", 0, KEY_QUERY_VALUE, &hKey); if (rc == ERROR_SUCCESS) { temp_len = PATH_MAX; rc = RegQueryValueEx( hKey, "ConfigFile", NULL, NULL, (LPBYTE) temp_path, &temp_len); if ((rc == ERROR_SUCCESS) && (temp_len < PATH_MAX)) conf_path = temp_path; RegCloseKey(hKey); } } if (!conf_path) { rc = RegOpenKeyEx( HKEY_LOCAL_MACHINE, "Software\\OpenSC Project\\OpenSC", 0, KEY_QUERY_VALUE, &hKey ); if (rc == ERROR_SUCCESS) { temp_len = PATH_MAX; rc = RegQueryValueEx( hKey, "ConfigFile", NULL, NULL, (LPBYTE) temp_path, &temp_len); if ((rc == ERROR_SUCCESS) && (temp_len < PATH_MAX)) conf_path = temp_path; RegCloseKey(hKey); } } if (!conf_path) { sc_debug(ctx, SC_LOG_DEBUG_NORMAL, "process_config_file doesn't find opensc config file. Please set the registry key."); return; } #else conf_path = getenv("OPENSC_CONF"); if (!conf_path) conf_path = OPENSC_CONF_PATH; #endif ctx->conf = scconf_new(conf_path); if (ctx->conf == NULL) return; r = scconf_parse(ctx->conf); #ifdef OPENSC_CONFIG_STRING /* Parse the string if config file didn't exist */ if (r < 0) r = scconf_parse_string(ctx->conf, OPENSC_CONFIG_STRING); #endif if (r < 1) { /* A negative return value means the config file isn't * there, which is not an error. Nevertheless log this * fact. */ if (r < 0) sc_debug(ctx, SC_LOG_DEBUG_NORMAL, "scconf_parse failed: %s", ctx->conf->errmsg); else sc_debug(ctx, SC_LOG_DEBUG_NORMAL, "scconf_parse failed: %s", ctx->conf->errmsg); scconf_free(ctx->conf); ctx->conf = NULL; return; } blocks = scconf_find_blocks(ctx->conf, NULL, "app", ctx->app_name); if (blocks[0]) ctx->conf_blocks[count++] = blocks[0]; free(blocks); if (strcmp(ctx->app_name, "default") != 0) { blocks = scconf_find_blocks(ctx->conf, NULL, "app", "default"); if (blocks[0]) ctx->conf_blocks[count] = blocks[0]; free(blocks); } /* Above we add 2 blocks at most, but conf_blocks has 3 elements, * so at least one is NULL */ for (i = 0; ctx->conf_blocks[i]; i++) load_parameters(ctx, ctx->conf_blocks[i], opts); }
int main(int argc, char **argv) { #ifdef ADD_TEST scconf_block *foo_block = NULL; scconf_item *foo_item = NULL; scconf_list *foo_list = NULL; #endif scconf_context *conf = NULL; scconf_entry entry[] = { {"ldap", SCCONF_CALLBACK, SCCONF_VERBOSE | SCCONF_ALL_BLOCKS, (void *) ldap_cb, NULL}, {"card", SCCONF_CALLBACK, SCCONF_VERBOSE | SCCONF_ALL_BLOCKS, (void *) card_cb, NULL}, {NULL, 0, 0, NULL, NULL} }; char *in = NULL, *out = NULL; int r; if (argc != 3) { printf("Usage: test-conf <in.conf> <out.conf>\n"); return 1; } in = argv[argc - 2]; out = argv[argc - 1]; conf = scconf_new(in); if (!conf) { printf("scconf_new failed\n"); return 1; } if (scconf_parse(conf) < 1) { printf("scconf_parse failed: %s\n", conf->errmsg); scconf_free(conf); return 1; } conf->debug = 1; if (scconf_parse_entries(conf, NULL, entry) != 0) { printf("scconf_parse_entries failed\n"); scconf_free(conf); return 1; } #ifdef ADD_TEST scconf_list_add(&foo_list, "value1"); scconf_list_add(&foo_list, "value2"); foo_block = (scconf_block *) scconf_find_block(conf, NULL, "foo"); foo_block = scconf_block_add(conf, foo_block, "block1", foo_list); foo_block = scconf_block_add(conf, foo_block, "block2", foo_list); scconf_list_add(&foo_list, "value3"); /* this will not segfault as type SCCONF_ITEM_TYPE_COMMENT is used */ scconf_item_add(conf, foo_block, foo_item, SCCONF_ITEM_TYPE_COMMENT, NULL, "# comment1"); scconf_item_add(conf, foo_block, foo_item, SCCONF_ITEM_TYPE_VALUE, "list1", foo_list); foo_block = NULL; scconf_item_add(conf, foo_block, foo_item, SCCONF_ITEM_TYPE_BLOCK, "block3", (void *) scconf_find_block(conf, NULL, "foo")); scconf_item_add(conf, foo_block, foo_item, SCCONF_ITEM_TYPE_VALUE, "list2", foo_list); scconf_item_add(conf, foo_block, foo_item, SCCONF_ITEM_TYPE_COMMENT, NULL, "# comment2"); if (write_entries(conf, foo_list) != 0) { printf("scconf_write_entries failed\n"); scconf_free(conf); return 1; } scconf_list_destroy(foo_list); #endif if ((r = scconf_write(conf, out)) != 0) { printf("scconf_write: %s\n", strerror(r)); } else { printf("Successfully rewrote file \"%s\" as \"%s\"\n", in, out); } scconf_free(conf); return 0; }
static void process_config_file(sc_context_t *ctx, struct _sc_ctx_options *opts) { int i, r, count = 0; scconf_block **blocks; const char *conf_path = NULL; const char *debug = NULL; #ifdef _WIN32 char temp_path[PATH_MAX]; size_t temp_len; #endif /* Takes effect even when no config around */ debug = getenv("OPENSC_DEBUG"); if (debug) ctx->debug = atoi(debug); memset(ctx->conf_blocks, 0, sizeof(ctx->conf_blocks)); #ifdef _WIN32 temp_len = PATH_MAX-1; r = sc_ctx_win32_get_config_value("OPENSC_CONF", "ConfigFile", "Software\\OpenSC Project\\OpenSC", temp_path, &temp_len); if (r) { sc_log(ctx, "process_config_file doesn't find opensc config file. Please set the registry key."); return; } temp_path[temp_len] = '\0'; conf_path = temp_path; #else conf_path = getenv("OPENSC_CONF"); if (!conf_path) conf_path = OPENSC_CONF_PATH; #endif ctx->conf = scconf_new(conf_path); if (ctx->conf == NULL) return; r = scconf_parse(ctx->conf); #ifdef OPENSC_CONFIG_STRING /* Parse the string if config file didn't exist */ if (r < 0) r = scconf_parse_string(ctx->conf, OPENSC_CONFIG_STRING); #endif if (r < 1) { /* A negative return value means the config file isn't * there, which is not an error. Nevertheless log this * fact. */ if (r < 0) sc_log(ctx, "scconf_parse failed: %s", ctx->conf->errmsg); else sc_log(ctx, "scconf_parse failed: %s", ctx->conf->errmsg); scconf_free(ctx->conf); ctx->conf = NULL; return; } /* needs to be after the log file is known */ sc_log(ctx, "Used configuration file '%s'", conf_path); blocks = scconf_find_blocks(ctx->conf, NULL, "app", ctx->app_name); if (blocks && blocks[0]) ctx->conf_blocks[count++] = blocks[0]; free(blocks); if (strcmp(ctx->app_name, "default") != 0) { blocks = scconf_find_blocks(ctx->conf, NULL, "app", "default"); if (blocks && blocks[0]) ctx->conf_blocks[count] = blocks[0]; free(blocks); } /* Above we add 2 blocks at most, but conf_blocks has 3 elements, * so at least one is NULL */ for (i = 0; ctx->conf_blocks[i]; i++) load_parameters(ctx, ctx->conf_blocks[i], opts); }
static int set_default_module(const char *mod) { scconf_block *pam_pkcs11, *pkcs11_eventmgr; scconf_block **modules = NULL; scconf_context *ctx = NULL; scconf_context *ectx = NULL; const char *lib = NULL; int result = 1; /* * write out pam_pkcs11.conf */ ctx = scconf_new(PAM_PKCS11_CONF); if (ctx == NULL) { goto bail; } if (scconf_parse(ctx) <= 0) { goto bail; } pam_pkcs11 = (scconf_block *)scconf_find_block(ctx, NULL, "pam_pkcs11"); if (!pam_pkcs11) { goto bail; } scconf_replace_str(pam_pkcs11, "use_pkcs11_module", mod); modules = scconf_find_blocks(ctx, pam_pkcs11, "pkcs11_module", mod); if (!modules || !modules[0]) { goto bail; } lib = scconf_get_str(modules[0], "module", NULL); if (!lib) { goto bail; } result = scconf_write(ctx, NULL); if (result != 0) { goto bail; } ectx = scconf_new(EVENTMGR_CONF); if (ectx == NULL) { goto bail; } if (scconf_parse(ectx) <= 0) { goto bail; } pkcs11_eventmgr = (scconf_block *) scconf_find_block(ectx, NULL, "pkcs11_eventmgr"); if (!pkcs11_eventmgr) { goto bail; } scconf_replace_str(pkcs11_eventmgr, "pkcs11_module", lib); result = scconf_write(ectx, NULL); bail: if (modules) { free(modules); } if (ctx) { scconf_free(ctx); } if (ectx) { scconf_free(ectx); } return result; }