blob: 82025045da21d770ac739a959d261bfd59507c68 [file] [log] [blame]
Harald Welte753c8aa2020-10-20 23:04:59 +02001/* (C) 2018-2020 by Harald Welte <laforge@gnumonks.org>
Harald Welte3dcdd202019-03-09 13:06:46 +01002 *
3 * All Rights Reserved
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22
Harald Welte45c948c2018-09-23 19:26:52 +020023
24#include <osmocom/core/linuxlist.h>
25#include <osmocom/core/talloc.h>
26#include <osmocom/core/utils.h>
27
28#include <csv.h>
Harald Welte753c8aa2020-10-20 23:04:59 +020029#include <regex.h>
30#include <errno.h>
Harald Welte45c948c2018-09-23 19:26:52 +020031
32#include "bankd.h"
33
34struct pcsc_slot_name {
35 struct llist_head list;
36 /* RSPRO bank slot number */
37 struct bank_slot slot;
38 /* String name of the reader in PC/SC world */
Harald Welte753c8aa2020-10-20 23:04:59 +020039 const char *name_regex;
Harald Welte45c948c2018-09-23 19:26:52 +020040};
41
Harald Welte753c8aa2020-10-20 23:04:59 +020042/* return a talloc-allocated string containing human-readable POSIX regex error */
43static char *get_regerror(void *ctx, int errcode, regex_t *compiled)
44{
45 size_t len = regerror(errcode, compiled, NULL, 0);
46 char *buffer = talloc_size(ctx, len);
47 OSMO_ASSERT(buffer);
48 regerror(errcode, compiled, buffer, len);
49 return buffer;
50}
51
Harald Welte45c948c2018-09-23 19:26:52 +020052enum parser_state_name {
53 ST_NONE,
54 ST_BANK_NR,
55 ST_SLOT_NR,
56 ST_PCSC_NAME,
57};
58struct parser_state {
59 struct bankd *bankd;
60 enum parser_state_name state;
61 struct pcsc_slot_name *cur;
62};
63
64
65static void parser_state_init(struct parser_state *ps)
66{
67 ps->state = ST_BANK_NR;
68 ps->cur = NULL;
69}
70
71static void cb1(void *s, size_t len, void *data)
72{
73 char *field = (char *) s;
74 struct parser_state *ps = data;
75
76 switch (ps->state) {
77 case ST_BANK_NR:
78 OSMO_ASSERT(!ps->cur);
79 ps->cur = talloc_zero(ps->bankd, struct pcsc_slot_name);
80 OSMO_ASSERT(ps->cur);
81 ps->cur->slot.bank_id = atoi(field);
82 ps->state = ST_SLOT_NR;
83 break;
84 case ST_SLOT_NR:
85 OSMO_ASSERT(ps->cur);
86 ps->cur->slot.slot_nr = atoi(field);
87 ps->state = ST_PCSC_NAME;
88 break;
89 case ST_PCSC_NAME:
90 OSMO_ASSERT(ps->cur);
Harald Welte753c8aa2020-10-20 23:04:59 +020091 ps->cur->name_regex = talloc_strdup(ps->cur, field);
Harald Welte45c948c2018-09-23 19:26:52 +020092 break;
93 default:
94 OSMO_ASSERT(0);
95 }
96}
97
98static void cb2(int c, void *data)
99{
100 struct parser_state *ps = data;
101 struct pcsc_slot_name *sn = ps->cur;
Harald Welte753c8aa2020-10-20 23:04:59 +0200102 regex_t compiled_name;
103 int rc;
Harald Welte45c948c2018-09-23 19:26:52 +0200104
Harald Welte7293e7b2021-12-08 21:29:11 +0100105 LOGP(DMAIN, LOGL_INFO, "PC/SC slot name: %u/%u -> regex '%s'\n",
106 sn->slot.bank_id, sn->slot.slot_nr, sn->name_regex);
Harald Welte753c8aa2020-10-20 23:04:59 +0200107
108 memset(&compiled_name, 0, sizeof(compiled_name));
109
110 rc = regcomp(&compiled_name, sn->name_regex, REG_EXTENDED);
111 if (rc != 0) {
112 char *errmsg = get_regerror(sn, rc, &compiled_name);
Harald Welte7293e7b2021-12-08 21:29:11 +0100113 LOGP(DMAIN, LOGL_ERROR, "Error compiling regex '%s': %s - Ignoring\n",
114 sn->name_regex, errmsg);
Harald Welte753c8aa2020-10-20 23:04:59 +0200115 talloc_free(errmsg);
116 talloc_free(sn);
117 } else {
118 llist_add_tail(&sn->list, &ps->bankd->pcsc_slot_names);
119 }
120 regfree(&compiled_name);
Harald Welte45c948c2018-09-23 19:26:52 +0200121
122 ps->state = ST_BANK_NR;
123 ps->cur = NULL;
124}
125
126int bankd_pcsc_read_slotnames(struct bankd *bankd, const char *csv_file)
127{
128 FILE *fp;
129 struct csv_parser p;
130 char buf[1024];
131 size_t bytes_read;
132 struct parser_state ps;
133
134 if (csv_init(&p, CSV_APPEND_NULL) != 0)
135 return -1;
136
137 fp = fopen(csv_file, "rb");
138 if (!fp)
139 return -1;
140
141 parser_state_init(&ps);
142 ps.bankd = bankd;
143
144 while ((bytes_read = fread(buf, 1, sizeof(buf), fp)) > 0) {
145 if (csv_parse(&p, buf, bytes_read, cb1, cb2, &ps) != bytes_read) {
Harald Welte7293e7b2021-12-08 21:29:11 +0100146 LOGP(DMAIN, LOGL_FATAL, "Error parsing bankd PC/SC CSV: %s\n",
147 csv_strerror(csv_error(&p)));
Harald Welte45c948c2018-09-23 19:26:52 +0200148 fclose(fp);
149 return -1;
150 }
151 }
152
153 csv_fini(&p, cb1, cb2, &ps);
154 fclose(fp);
155 csv_free(&p);
156
157 return 0;
158}
159
160const char *bankd_pcsc_get_slot_name(struct bankd *bankd, const struct bank_slot *slot)
161{
162 struct pcsc_slot_name *cur;
163
164 llist_for_each_entry(cur, &bankd->pcsc_slot_names, list) {
165 if (bank_slot_equals(&cur->slot, slot))
Harald Welte753c8aa2020-10-20 23:04:59 +0200166 return cur->name_regex;
Harald Welte45c948c2018-09-23 19:26:52 +0200167 }
168 return NULL;
169}
Harald Welte297d72e2019-03-28 18:42:35 +0100170
171
172#include <wintypes.h>
173#include <winscard.h>
174#include <pcsclite.h>
175
Harald Welte753c8aa2020-10-20 23:04:59 +0200176#define LOGW_PCSC_ERROR(w, rv, text) \
177 LOGW((w), text ": %s (0x%lX)\n", pcsc_stringify_error(rv), rv)
178
Harald Welte297d72e2019-03-28 18:42:35 +0100179#define PCSC_ERROR(w, rv, text) \
180if (rv != SCARD_S_SUCCESS) { \
Harald Welte753c8aa2020-10-20 23:04:59 +0200181 LOGW_PCSC_ERROR(w, rv, text); \
Harald Welte297d72e2019-03-28 18:42:35 +0100182 goto end; \
183} else { \
Harald Welte168d7242021-12-08 15:25:42 +0100184 LOGW((w), text ": OK\n"); \
Harald Welte297d72e2019-03-28 18:42:35 +0100185}
186
Harald Weltebbd18bd2019-12-16 13:11:50 +0100187static int pcsc_get_atr(struct bankd_worker *worker)
188{
189 long rc;
190 char pbReader[MAX_READERNAME];
191 /* use DWORD type as this is what the PC/SC API expects */
192 DWORD dwReaderLen = sizeof(pbReader);
193 DWORD dwAtrLen = worker->card.atr_len = sizeof(worker->card.atr);
194 DWORD dwState, dwProt;
195
196 rc = SCardStatus(worker->reader.pcsc.hCard, pbReader, &dwReaderLen, &dwState, &dwProt,
197 worker->card.atr, &dwAtrLen);
198 PCSC_ERROR(worker, rc, "SCardStatus")
199 worker->card.atr_len = dwAtrLen;
200 LOGW(worker, "Card ATR: %s\n", osmo_hexdump_nospc(worker->card.atr, worker->card.atr_len));
201end:
202 return rc;
203}
204
Harald Welte753c8aa2020-10-20 23:04:59 +0200205
206static int pcsc_connect_slot_regex(struct bankd_worker *worker)
207{
208 DWORD dwReaders = SCARD_AUTOALLOCATE;
209 LPSTR mszReaders = NULL;
210 regex_t compiled_name;
211 int result = -1;
212 LONG rc;
213 char *p;
214
215 LOGW(worker, "Attempting to find card/slot using regex '%s'\n", worker->reader.name);
216
217 rc = regcomp(&compiled_name, worker->reader.name, REG_EXTENDED);
218 if (rc != 0) {
219 LOGW(worker, "Error compiling RegEx over name '%s'\n", worker->reader.name);
220 return -EINVAL;
221 }
222
223 rc = SCardListReaders(worker->reader.pcsc.hContext, NULL, (LPSTR)&mszReaders, &dwReaders);
224 if (rc != SCARD_S_SUCCESS) {
225 LOGW_PCSC_ERROR(worker, rc, "SCardListReaders");
226 goto out_regfree;
227 }
228
229 p = mszReaders;
230 while (*p) {
231 DWORD dwActiveProtocol;
232 int r = regexec(&compiled_name, p, 0, NULL, 0);
233 if (r == 0) {
234 LOGW(worker, "Attempting to open card/slot '%s'\n", p);
235 rc = SCardConnect(worker->reader.pcsc.hContext, p, SCARD_SHARE_SHARED,
236 SCARD_PROTOCOL_T0, &worker->reader.pcsc.hCard,
237 &dwActiveProtocol);
238 if (rc == SCARD_S_SUCCESS)
239 result = 0;
240 else
241 LOGW_PCSC_ERROR(worker, rc, "SCardConnect");
242 break;
243 }
244 p += strlen(p) + 1;
245 }
246
247 SCardFreeMemory(worker->reader.pcsc.hContext, mszReaders);
248
249out_regfree:
250 regfree(&compiled_name);
251
252 return result;
253}
254
255
Harald Welte297d72e2019-03-28 18:42:35 +0100256static int pcsc_open_card(struct bankd_worker *worker)
257{
258 long rc;
259
260 if (!worker->reader.pcsc.hContext) {
261 LOGW(worker, "Attempting to open PC/SC context\n");
262 /* The PC/SC context must be created inside the thread where we'll later use it */
263 rc = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &worker->reader.pcsc.hContext);
264 PCSC_ERROR(worker, rc, "SCardEstablishContext")
265 }
266
267 if (!worker->reader.pcsc.hCard) {
Harald Welte753c8aa2020-10-20 23:04:59 +0200268 rc = pcsc_connect_slot_regex(worker);
269 if (rc != 0)
270 goto end;
Harald Welte297d72e2019-03-28 18:42:35 +0100271 }
272
Harald Weltebbd18bd2019-12-16 13:11:50 +0100273 rc = pcsc_get_atr(worker);
Harald Welte753c8aa2020-10-20 23:04:59 +0200274
Harald Weltebbd18bd2019-12-16 13:11:50 +0100275end:
276 return rc;
277}
278
279static int pcsc_reset_card(struct bankd_worker *worker, bool cold_reset)
280{
281 long rc;
282 DWORD dwActiveProtocol;
283
284 LOGW(worker, "Resetting card in '%s' (%s)\n", worker->reader.name,
285 cold_reset ? "cold reset" : "warm reset");
286 rc = SCardReconnect(worker->reader.pcsc.hCard, SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0,
287 cold_reset ? SCARD_UNPOWER_CARD : SCARD_RESET_CARD, &dwActiveProtocol);
288 PCSC_ERROR(worker, rc, "SCardReconnect");
289
290 rc = pcsc_get_atr(worker);
Harald Welte297d72e2019-03-28 18:42:35 +0100291end:
292 return rc;
293}
294
295static int pcsc_transceive(struct bankd_worker *worker, const uint8_t *out, size_t out_len,
296 uint8_t *in, size_t *in_len)
297{
298 const SCARD_IO_REQUEST *pioSendPci = SCARD_PCI_T0;
299 SCARD_IO_REQUEST pioRecvPci;
300 long rc;
301
302 rc = SCardTransmit(worker->reader.pcsc.hCard, pioSendPci, out, out_len, &pioRecvPci, in, in_len);
Harald Welte50a09722021-12-08 15:33:12 +0100303 /* don't use PCSC_ERROR here as we don't want to log every successful SCardTransmit */
304 if (rc != SCARD_S_SUCCESS)
305 LOGW_PCSC_ERROR(worker, rc, "SCardTransmit");
Harald Welte297d72e2019-03-28 18:42:35 +0100306
Harald Welte297d72e2019-03-28 18:42:35 +0100307 return rc;
308}
309
310static void pcsc_cleanup(struct bankd_worker *worker)
311{
312 if (worker->reader.pcsc.hCard) {
313 SCardDisconnect(worker->reader.pcsc.hCard, SCARD_UNPOWER_CARD);
314 worker->reader.pcsc.hCard = 0;
315 }
316 if (worker->reader.pcsc.hContext) {
317 SCardReleaseContext(worker->reader.pcsc.hContext);
318 worker->reader.pcsc.hContext = 0;
319 }
320}
321
322const struct bankd_driver_ops pcsc_driver_ops = {
323 .open_card = pcsc_open_card,
Harald Weltebbd18bd2019-12-16 13:11:50 +0100324 .reset_card = pcsc_reset_card,
Harald Welte297d72e2019-03-28 18:42:35 +0100325 .transceive = pcsc_transceive,
326 .cleanup = pcsc_cleanup,
327};