NB! This project is outdated and unmaintained, please refer to the OpenSC WindowsInstaller instead!

source: trunk/editconf.c @ 36

Revision 36, 5.0 KB checked in by aj, 7 years ago (diff)

improved editconf.c file - detect the directory it is run in.
add engine_pkcs11 (0.1.1 with small modifications).
install libp11.lib file.

Line 
1/*
2 * $Id: main.c,v 1.2 2005/06/24 12:14:46 stef Exp $
3 *
4 * Copyright (C) 2002
5 *  Antti Tapaninen <aet@cc.hut.fi>
6 *  Zetes (C) 2005
7 *
8 * This program is free software; you can redistribute it and/or modify 
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
21 */
22
23#undef UNICODE
24#include <stdlib.h>
25#include <stdio.h>
26#include <ctype.h>
27#include <windows.h>
28#include <errno.h>
29#include <scconf.h>
30
31static int changeConfigFile(const char *csConfigDir);
32static int changeRegistry(const char *csConfigDir);
33
34#define CLEANUP(iRes, iVal) {iRes = iVal; goto cleanup;}
35
36
37int main(int argc, char **argv)
38{
39        int iRet = 0;
40        char csConfigDir[MAX_PATH];
41        char pwszPath[_MAX_PATH], pwszDrive[_MAX_DRIVE], pwszDir[_MAX_DIR];
42
43        if(!GetModuleFileName(NULL, pwszPath, _MAX_PATH)) return FALSE;
44        _splitpath(pwszPath, pwszDrive, pwszDir, NULL, NULL);
45        _makepath(pwszPath, pwszDrive, pwszDir, NULL, NULL);
46
47        printf("\nThis tool configures OpenSC after installation.\n");
48        printf("\nUsage: editconf\n");
49        printf("  It will edit the opensc.conf file and set some\n");
50        printf("  registry keys so everything workd.\n");
51        printf("  It will use the path to itself for this purpose.\n");
52
53        SetCurrentDirectory(pwszPath);
54
55        strncpy(csConfigDir, pwszPath, sizeof(csConfigDir) - 1);
56        csConfigDir[sizeof(csConfigDir) - 1] = '\0';
57        if (csConfigDir[strlen(csConfigDir) - 1] == '\\')
58                csConfigDir[strlen(csConfigDir) - 1] = '\0';
59
60        iRet = changeConfigFile(csConfigDir);
61        if (iRet != 0) {
62                fprintf(stderr,"Error: couldn't change the opensc.conf file: %d\n", iRet);
63                return 1;
64        } else {
65                iRet = changeRegistry(csConfigDir);
66                if (iRet != 0) {
67                        fprintf(stderr,"Error: couldn't change the registry: %d\n", iRet);
68                        return 1;
69                } else
70                        printf("configopensc finished successfully\n");
71        }
72
73        return 0;
74}
75
76static int changeConfigFile(const char *csConfigDir)
77{
78        char csConfigFile[MAX_PATH];
79        scconf_block *conf_block = NULL, **blocks;
80        scconf_context *conf = NULL;
81        scconf_item *item;
82        scconf_list *list;
83        int iRes = 0;
84
85        /* Open and parse the config file */
86        sprintf(csConfigFile, "%s\\opensc.conf", csConfigDir);
87        conf = scconf_new(csConfigFile);
88        if (!conf) {
89                printf("\nscconf_new() failed\n");
90                CLEANUP(iRes, 1);
91        }
92        if (scconf_parse(conf) < 1) {
93                printf("\nscconf_parse() failed: %s\n", conf->errmsg);
94                CLEANUP(iRes, 1);
95        }
96
97        /* Get the "app default" block */
98        blocks = scconf_find_blocks(conf, NULL, "app", "default");
99        if (blocks[0])
100                conf_block = blocks[0];
101        free(blocks);
102        if (conf_block != NULL) {
103                for (item = conf_block->items; item != NULL; item = item->next) {
104                        if ((item->type != SCCONF_ITEM_TYPE_VALUE)
105                            || (strcmp(item->key, "profile_dir") != 0))
106                                continue;
107                        list = item->value.list;
108                        scconf_list_destroy(list);
109                        list = NULL;
110                        scconf_list_add(&list, csConfigDir);
111                        item->value.list = list;
112                        break;
113                }
114                if (item == NULL)
115                        scconf_put_str(conf_block, "profile_dir", csConfigDir);
116        }
117
118        /* Write */
119        if ((iRes = scconf_write(conf, csConfigFile)) != 0)
120                printf("\nscconf_write(): %s\n", strerror(iRes));
121
122      cleanup:
123        if (conf)
124                scconf_free(conf);
125
126        return iRes;
127}
128
129static int changeRegistry(const char *csConfigDir)
130{
131        char csConfigFile[MAX_PATH];
132        char csModuleFile[MAX_PATH];
133        DWORD dwDisposition;
134        HKEY hKey;
135        DWORD dwErr;
136
137        sprintf(csConfigFile, "%s\\opensc.conf", csConfigDir);
138        sprintf(csModuleFile, "%s\\opensc-pkcs11.dll", csConfigDir);
139
140        dwErr = RegCreateKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\OpenSC",
141                               0L, "", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS,
142                               NULL, &hKey, &dwDisposition);
143        if (dwErr != ERROR_SUCCESS)
144                printf("RegCreateKeyEx() returned error %d (0x%0x)\n",
145                       GetLastError(), GetLastError());
146        else {
147                dwErr = RegSetValueEx(hKey, "ConfigFile", 0L, REG_SZ,
148                                      csConfigFile, strlen(csConfigFile) + 1);
149                if (dwErr != ERROR_SUCCESS)
150                        printf("RegSetValueEx() returned error %d (0x%0x)\n",
151                               GetLastError(), GetLastError());
152
153                RegCloseKey(hKey);
154        }
155
156        dwErr = RegCreateKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\PKCS11-Spy",
157                               0L, "", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS,
158                               NULL, &hKey, &dwDisposition);
159        if (dwErr != ERROR_SUCCESS)
160                printf("RegCreateKeyEx() returned error %d (0x%0x)\n",
161                       GetLastError(), GetLastError());
162        else {
163                dwErr = RegSetValueEx(hKey, "Module", 0L, REG_SZ,
164                                      csModuleFile, strlen(csModuleFile) + 1);
165                if (dwErr != ERROR_SUCCESS)
166                        printf("RegSetValueEx() returned error %d (0x%0x)\n",
167                               GetLastError(), GetLastError());
168
169                RegCloseKey(hKey);
170        }
171
172        return (int)dwErr;
173}
Note: See TracBrowser for help on using the repository browser.