File: bundleTool.c

package info (click to toggle)
libmusclecard 1.3.3-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,880 kB
  • ctags: 763
  • sloc: sh: 8,841; ansic: 3,954; lex: 139; makefile: 130
file content (286 lines) | stat: -rw-r--r-- 6,189 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
 * This automatically updates the Info.plist.
 *
 * MUSCLE SmartCard Development ( http://www.linuxnet.com )
 *
 * Copyright (C) 2001-2003
 *  David Corcoran <corcoran@linuxnet.com>
 *  Ludovic Rousseau <ludovic.rousseau@free.fr>
 *
 * $Id: bundleTool.c 1269 2004-10-06 08:11:51Z rousseau $
 */

#include "config.h"
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_DIRENT_H
#include <dirent.h>
#endif
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>

#include "pcsclite.h"
#include "winscard.h"

/*
 * The following defines personalize this for different tokens 
 */

#define BUNDLE_DIR MSC_SVC_DROPDIR

/* redefine MSC_SVC_DROPDIR only if not yet defined */
#ifndef MSC_SVC_DROPDIR
#ifndef WIN32
#define MSC_SVC_DROPDIR      "/usr/local/musclecard/services"
#else
#define MSC_SVC_DROPDIR      "C:\\Program Files\\Muscle\\Services"
#endif
#endif


/*
 * End of personalization 
 */

#define CHECK_ERR(cond, msg) { if (cond) { \
  printf("Error: %s\n", msg); return -1; } }

int main(int argc, char **argv)
{
	LONG rv;
	SCARDCONTEXT hContext;
	SCARD_READERSTATE_A rgReaderStates;
	DWORD readerListSize;
	struct stat statBuffer;
	char spAtrValue[100];
	char chosenInfoPlist[1024];
	char *readerList;
	char *restFile;
	char atrInsertion[256];
	FILE *fp;
#ifndef WIN32
	DIR *bundleDir;
	struct dirent *currBundle;
#else
	HANDLE hFind;
	WIN32_FIND_DATA findData;
	char findPath[200];
	int find;
#endif
	int i, p;
	int userChoice;
	int totalBundles;
	int filePosition;
	int restFileSize;
	int restOffset;
	int getsSize;

	if (argc > 1)
	{
		printf("Invalid arguments\nUsage: %s\n", argv[0]);
		return -1;
	}

#ifndef WIN32
	currBundle = NULL;
	bundleDir = opendir(BUNDLE_DIR);
	if (bundleDir == NULL)
	{
		printf("Opendir failed %s: %s\n", BUNDLE_DIR, strerror(errno));
		return -1;
	}
#else
	sprintf(findPath, "%s\\*.bundle", BUNDLE_DIR);
	hFind = FindFirstFile(findPath, &findData);

	if (hFind == INVALID_HANDLE_VALUE)
	{
		printf("Cannot open PC/SC token drivers directory: %s", findPath);
		return -1;
	}
#endif

	printf("Select the appropriate token driver:\n");
	printf("------------------------------------\n");

	i = 1;
	totalBundles = 0;
#ifndef WIN32
	while ((currBundle = readdir(bundleDir)) != NULL)
	{
		if (strstr(currBundle->d_name, ".bundle") != NULL)
		{
			printf("  %d.     %s\n", i++, currBundle->d_name);
			totalBundles += 1;
		}
	}
#else
	do
	{
		if (strstr(findData.cFileName, ".bundle") != NULL)
		{
			printf("  %d.     %s\n", i++, findData.cFileName);
			totalBundles += 1;
		}
	} while (FindNextFile(hFind, &findData) != 0);
#endif

	printf("------------------------------------\n");

	if (totalBundles == 0)
	{
		printf("No drivers exist - exiting\n");
		return 1;
	}

	do
	{
		printf("Enter the number: ");
		scanf("%d", &userChoice);
	}
	while (userChoice < 1 && userChoice > totalBundles);

#ifndef WIN32
	closedir(bundleDir);
#endif

#ifndef WIN32
	bundleDir = opendir(BUNDLE_DIR);
	CHECK_ERR(bundleDir == NULL, "Opendir failed");
#else
	sprintf(findPath, "%s\\*.bundle", BUNDLE_DIR);
	hFind = FindFirstFile(findPath, &findData);

	if (hFind == INVALID_HANDLE_VALUE)
	{
		printf("Cannot open PC/SC token drivers directory: %s", findPath);
		return -1;
	}
	else
		find = 1;
#endif

	do
	{
#ifndef WIN32
		if ((currBundle = readdir(bundleDir)) != NULL)
		{
			if (strstr(currBundle->d_name, ".bundle") != NULL)
				userChoice -= 1;
		}
#else
		if (find)
		{
			if (strstr(findData.cFileName, ".bundle") != NULL)
				userChoice -= 1;
		}

		if (userChoice)
			find = (FindNextFile(hFind, &findData) != 0);
#endif
	} while (userChoice != 0);


#ifndef WIN32
#if HAVE_SNPRINTF
	snprintf(chosenInfoPlist, sizeof(chosenInfoPlist),
		"%s/%s/Contents/Info.plist", BUNDLE_DIR, currBundle->d_name);
#else
	sprintf(chosenInfoPlist, "%s/%s/Contents/Info.plist", BUNDLE_DIR, 
		currBundle->d_name);
#endif
#else
	sprintf(chosenInfoPlist, "%s\\%s\\Contents\\Info.plist", BUNDLE_DIR,
		findData.cFileName);
#endif

#ifndef WIN32
	closedir(bundleDir);
#endif
	printf("\n");

	rv = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &hContext);
	CHECK_ERR(rv != SCARD_S_SUCCESS, "EstablishContext Failed");

	readerListSize = 0;
	rv = SCardListReaders(hContext, NULL, NULL, &readerListSize);
	CHECK_ERR(rv != SCARD_S_SUCCESS, "ListReaders Failed");

	readerList = (char *) malloc(sizeof(char) * readerListSize);
	CHECK_ERR(readerList == NULL, "Malloc Failed");

	rv = SCardListReaders(hContext, NULL, readerList, &readerListSize);
	CHECK_ERR(rv != SCARD_S_SUCCESS, "ListReaders Alloc Failed");

	printf("Insert your token in: %s\n", readerList);

	rgReaderStates.szReader = readerList;
	rgReaderStates.dwCurrentState = SCARD_STATE_EMPTY;

	rv = SCardGetStatusChange(hContext, INFINITE, &rgReaderStates, 1);
	CHECK_ERR(rv != SCARD_S_SUCCESS, "GetStatusChange Failed");

	p = 0;
	for (i = 0; i < rgReaderStates.cbAtr; i++)
	{
		sprintf(&spAtrValue[p], "%02X", rgReaderStates.rgbAtr[i]);
		p += 2;
	}
	printf("\n");

#if HAVE_SNPRINTF
	snprintf(atrInsertion, sizeof(atrInsertion),
		"        <string>%s</string>\n", spAtrValue);
#else 
	sprintf(atrInsertion, "        <string>%s</string>\n", spAtrValue);
#endif

	fp = fopen(chosenInfoPlist, "r+");
	if (fp == NULL)
	{
		printf("Couldn't open %s: %s\n", chosenInfoPlist, strerror(errno));
		return -1;
	}

	rv = stat(chosenInfoPlist, &statBuffer);
	CHECK_ERR(rv != 0, "Stat failed");

	restFileSize = statBuffer.st_size + strlen(atrInsertion);
	restFile = (char *) malloc(sizeof(char) * restFileSize);
	CHECK_ERR(restFile == NULL, "Malloc failed");

	filePosition = 0;
	restOffset = 0;
	getsSize = 0;

	do
	{
		if (fgets(&restFile[restOffset], restFileSize, fp) == NULL)
			break;

		if (strstr(&restFile[restOffset], "<key>spAtrValue</key>"))
			filePosition = ftell(fp);

		getsSize = strlen(&restFile[restOffset]);
		restOffset += getsSize;
	}
	while (1);

	rewind(fp);
	fwrite(restFile, 1, filePosition, fp);
	fwrite(atrInsertion, 1, strlen(atrInsertion), fp);
	fwrite(&restFile[filePosition], 1, statBuffer.st_size - filePosition, fp);

	fclose(fp);

	printf("Token support updated successfully !\n");

	return 0;
}