apply improved profile handling code.

git-svn-id: https://www.opensc-project.org/svnp/opensc/trunk@2270 c6295689-39f2-0310-b995-f0e70906c6a9
This commit is contained in:
aj 2005-03-23 23:10:50 +00:00
parent 54ac27bd40
commit 52d3262e90
3 changed files with 73 additions and 108 deletions

View File

@ -28,6 +28,10 @@
#include <limits.h>
#include <opensc/scdl.h>
#ifdef _WIN32
#include <winreg.h>
#endif
/* Default value for apdu_masquerade option */
#ifndef _WIN32
# define DEF_APDU_MASQ SC_APDU_MASQUERADE_NONE
@ -554,51 +558,48 @@ 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;
char *conf_path;
#ifdef _WIN32
char tpath[PATH_MAX];
size_t tlen;
char temp_path[PATH_MAX];
int temp_len;
long rc;
HKEY hKey;
#endif
conf_path = 0;
memset(ctx->conf_blocks, 0, sizeof(ctx->conf_blocks));
#ifdef _WIN32
rc = RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\OpenSC", 0, KEY_QUERY_VALUE, &hKey);
if (rc == ERROR_SUCCESS) {
tlen = sizeof(tlen);
rc = RegQueryValueEx(hKey, "ConfigFile", NULL, NULL, (LPBYTE) tpath, (LPDWORD) &tlen);
if ((rc == ERROR_SUCCESS) && (tlen < PATH_MAX))
conf_path = tpath;
RegCloseKey(hKey);
}
rc = RegOpenKeyEx( HKEY_CURRENT_USER, "Software\\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", 0, KEY_QUERY_VALUE, &hKey);
if (rc == ERROR_SUCCESS) {
tlen = sizeof(tlen);
rc = RegQueryValueEx(hKey, "ConfigFile", NULL, NULL, (LPBYTE) tpath, (LPDWORD) &tlen);
if ((rc == ERROR_SUCCESS) && (tlen < PATH_MAX))
conf_path = tpath;
RegCloseKey(hKey);
}
}
if (! conf_path) {
rc = RegOpenKeyEx( HKEY_LOCAL_MACHINE, "Software\\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_error(ctx, "process_config_file doesn't find opensc config file. Please set the registry key.");
if (!conf_path) {
conf_path = OPENSC_CONF_PATH;
if (!strncmp(conf_path, "%windir%", 8)) {
GetWindowsDirectory(tpath, sizeof(tpath));
strncat(tpath, conf_path + 8, sizeof(tpath) - strlen(tpath));
conf_path = tpath;
}
}
#else
/* XXX: Enhance scconf and opensc internals to suit
* better into site / user configuration model. -aet
*/
conf_path = getenv("OPENSC_CONF");
if (!conf_path)
conf_path = OPENSC_CONF_PATH;
conf_path = getenv("OPENSC_CONF");
if (!conf_path)
conf_path = OPENSC_CONF_PATH;
#endif
ctx->conf = scconf_new(conf_path);
if (ctx->conf == NULL)
@ -748,7 +749,7 @@ int sc_get_cache_dir(sc_context_t *ctx, char *buf, size_t bufsize)
char *homedir;
const char *cache_dir;
#ifdef _WIN32
char tpath[PATH_MAX];
char temp_path[PATH_MAX];
#endif
#ifndef _WIN32
@ -760,8 +761,8 @@ int sc_get_cache_dir(sc_context_t *ctx, char *buf, size_t bufsize)
/* If USERPROFILE isn't defined, assume it's a single-user OS
* and put the cache dir in the Windows dir (usually C:\\WINDOWS) */
if (homedir == NULL || homedir[0] == '\0') {
GetWindowsDirectory(tpath, sizeof(tpath));
homedir = tpath;
GetWindowsDirectory(temp_path, sizeof(temp_path));
homedir = temp_path;
}
#endif
if (homedir == NULL)

View File

@ -186,7 +186,6 @@ typedef struct pin_info pin_info;
typedef struct file_info file_info;
typedef struct auth_info auth_info;
static const char * sc_profile_locate(struct sc_profile *, const char *);
static int process_conf(struct sc_profile *, scconf_context *);
static int process_block(struct state *, struct block *,
const char *, scconf_block *);
@ -281,13 +280,44 @@ sc_profile_new(void)
int
sc_profile_load(struct sc_profile *profile, const char *filename)
{
struct sc_context *ctx = profile->card->ctx;
scconf_context *conf;
int res = 0;
const char *profile_dir = NULL;
char path[PATH_MAX];
int res = 0, i;
if (!(filename = sc_profile_locate(profile, filename)))
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) {
sc_error(ctx, "you need to set profile_dir in your config file.");
return SC_ERROR_FILE_NOT_FOUND;
conf = scconf_new(filename);
}
#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) {
@ -386,69 +416,6 @@ sc_profile_free(struct sc_profile *profile)
free(profile);
}
static const char *
sc_profile_locate(struct sc_profile *profile, const char *name)
{
struct sc_context *ctx = profile->card->ctx;
const char *profile_default = SC_PKCS15_PROFILE_DIRECTORY;
char profile_dir[PATH_MAX];
static char path[1024];
int i;
/* append ".profile" unless already in the name */
if (strstr(name, SC_PKCS15_PROFILE_SUFFIX)) {
snprintf(path, sizeof(path), "%s", name);
} else {
snprintf(path, sizeof(path), "%s.%s", name,
SC_PKCS15_PROFILE_SUFFIX);
}
/* Unchanged name? */
if (access(path, R_OK) == 0)
return path;
/* If it's got slashes, don't mess with it any further */
if (strchr(path, '/'))
return path;
for (i = 0; ctx->conf_blocks[i]; i++) {
profile_default = scconf_get_str(ctx->conf_blocks[i], "profile_dir", NULL);
if (profile_default)
break;
profile_default = SC_PKCS15_PROFILE_DIRECTORY;
}
#ifndef _WIN32
strncpy(profile_dir, profile_default, sizeof(profile_dir));
#else
if (!strncmp(profile_default, "%windir%", 8)) {
GetWindowsDirectory(profile_dir, sizeof(profile_dir));
strncat(profile_dir, profile_default + 8,
sizeof(profile_dir) - strlen(profile_dir));
}
else
strncpy(profile_dir, profile_default, sizeof(profile_dir));
#endif
/* Try directory */
/* append ".profile" unless already in the name */
if (strstr(name, SC_PKCS15_PROFILE_SUFFIX)) {
snprintf(path, sizeof(path), "%s/%s",
profile_dir, name);
} else {
snprintf(path, sizeof(path), "%s/%s.%s",
profile_dir, name,
SC_PKCS15_PROFILE_SUFFIX);
}
if (access(path, R_OK) == 0)
return path;
/* Unchanged name? */
if (access(name, R_OK) == 0)
return name;
return NULL;
}
void
sc_profile_get_pin_info(struct sc_profile *profile,
unsigned int id, struct sc_pkcs15_pin_info *info)

View File

@ -14,9 +14,6 @@ extern "C" {
#include <opensc/pkcs15.h>
#include "keycache.h"
#ifndef SC_PKCS15_PROFILE_DIRECTORY
#define SC_PKCS15_PROFILE_DIRECTORY "/usr/share/opensc/profiles"
#endif
#ifndef SC_PKCS15_PROFILE_SUFFIX
#define SC_PKCS15_PROFILE_SUFFIX "profile"
#endif