blob: e1477dde6d54fa77e6cfcc8d6ef4c15ab782f1a5 [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 *
Harald Welte3dcdd202019-03-09 13:06:46 +010017 */
18
Harald Welte45c948c2018-09-23 19:26:52 +020019
20#include <osmocom/core/linuxlist.h>
21#include <osmocom/core/talloc.h>
22#include <osmocom/core/utils.h>
23
24#include <csv.h>
Harald Welte753c8aa2020-10-20 23:04:59 +020025#include <regex.h>
26#include <errno.h>
Harald Welte45c948c2018-09-23 19:26:52 +020027
28#include "bankd.h"
29
30struct pcsc_slot_name {
31 struct llist_head list;
32 /* RSPRO bank slot number */
33 struct bank_slot slot;
34 /* String name of the reader in PC/SC world */
Harald Welte753c8aa2020-10-20 23:04:59 +020035 const char *name_regex;
Harald Welte45c948c2018-09-23 19:26:52 +020036};
37
Harald Welte753c8aa2020-10-20 23:04:59 +020038/* return a talloc-allocated string containing human-readable POSIX regex error */
39static char *get_regerror(void *ctx, int errcode, regex_t *compiled)
40{
41 size_t len = regerror(errcode, compiled, NULL, 0);
42 char *buffer = talloc_size(ctx, len);
43 OSMO_ASSERT(buffer);
44 regerror(errcode, compiled, buffer, len);
45 return buffer;
46}
47
Harald Welte45c948c2018-09-23 19:26:52 +020048enum parser_state_name {
49 ST_NONE,
50 ST_BANK_NR,
51 ST_SLOT_NR,
52 ST_PCSC_NAME,
53};
54struct parser_state {
55 struct bankd *bankd;
56 enum parser_state_name state;
57 struct pcsc_slot_name *cur;
58};
59
60
61static void parser_state_init(struct parser_state *ps)
62{
63 ps->state = ST_BANK_NR;
64 ps->cur = NULL;
65}
66
67static void cb1(void *s, size_t len, void *data)
68{
69 char *field = (char *) s;
70 struct parser_state *ps = data;
71
72 switch (ps->state) {
73 case ST_BANK_NR:
74 OSMO_ASSERT(!ps->cur);
75 ps->cur = talloc_zero(ps->bankd, struct pcsc_slot_name);
76 OSMO_ASSERT(ps->cur);
77 ps->cur->slot.bank_id = atoi(field);
78 ps->state = ST_SLOT_NR;
79 break;
80 case ST_SLOT_NR:
81 OSMO_ASSERT(ps->cur);
82 ps->cur->slot.slot_nr = atoi(field);
83 ps->state = ST_PCSC_NAME;
84 break;
85 case ST_PCSC_NAME:
86 OSMO_ASSERT(ps->cur);
Harald Welte753c8aa2020-10-20 23:04:59 +020087 ps->cur->name_regex = talloc_strdup(ps->cur, field);
Harald Welte45c948c2018-09-23 19:26:52 +020088 break;
89 default:
90 OSMO_ASSERT(0);
91 }
92}
93
94static void cb2(int c, void *data)
95{
96 struct parser_state *ps = data;
97 struct pcsc_slot_name *sn = ps->cur;
Harald Welte753c8aa2020-10-20 23:04:59 +020098 regex_t compiled_name;
99 int rc;
Harald Welte45c948c2018-09-23 19:26:52 +0200100
Harald Welte7293e7b2021-12-08 21:29:11 +0100101 LOGP(DMAIN, LOGL_INFO, "PC/SC slot name: %u/%u -> regex '%s'\n",
102 sn->slot.bank_id, sn->slot.slot_nr, sn->name_regex);
Harald Welte753c8aa2020-10-20 23:04:59 +0200103
104 memset(&compiled_name, 0, sizeof(compiled_name));
105
106 rc = regcomp(&compiled_name, sn->name_regex, REG_EXTENDED);
107 if (rc != 0) {
108 char *errmsg = get_regerror(sn, rc, &compiled_name);
Harald Welte7293e7b2021-12-08 21:29:11 +0100109 LOGP(DMAIN, LOGL_ERROR, "Error compiling regex '%s': %s - Ignoring\n",
110 sn->name_regex, errmsg);
Harald Welte753c8aa2020-10-20 23:04:59 +0200111 talloc_free(errmsg);
112 talloc_free(sn);
113 } else {
114 llist_add_tail(&sn->list, &ps->bankd->pcsc_slot_names);
115 }
116 regfree(&compiled_name);
Harald Welte45c948c2018-09-23 19:26:52 +0200117
118 ps->state = ST_BANK_NR;
119 ps->cur = NULL;
120}
121
122int bankd_pcsc_read_slotnames(struct bankd *bankd, const char *csv_file)
123{
124 FILE *fp;
125 struct csv_parser p;
126 char buf[1024];
127 size_t bytes_read;
128 struct parser_state ps;
129
Harald Weltee9e505c2022-05-03 11:58:01 +0200130 if (csv_init(&p, CSV_APPEND_NULL) != 0) {
131 LOGP(DMAIN, LOGL_FATAL, "Error during csv_init\n");
Harald Welte45c948c2018-09-23 19:26:52 +0200132 return -1;
Harald Weltee9e505c2022-05-03 11:58:01 +0200133 }
Harald Welte45c948c2018-09-23 19:26:52 +0200134
135 fp = fopen(csv_file, "rb");
Harald Weltee9e505c2022-05-03 11:58:01 +0200136 if (!fp) {
137 LOGP(DMAIN, LOGL_FATAL, "Error opening %s: %s\n", csv_file, strerror(errno));
Harald Welte45c948c2018-09-23 19:26:52 +0200138 return -1;
Harald Weltee9e505c2022-05-03 11:58:01 +0200139 }
Harald Welte45c948c2018-09-23 19:26:52 +0200140
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 Welte628672b2022-07-13 15:20:13 +0200187static DWORD bankd_share_mode(struct bankd *bankd)
188{
189 if (bankd->cfg.permit_shared_pcsc)
190 return SCARD_SHARE_SHARED;
191 else
192 return SCARD_SHARE_EXCLUSIVE;
193}
194
Harald Weltebbd18bd2019-12-16 13:11:50 +0100195static int pcsc_get_atr(struct bankd_worker *worker)
196{
197 long rc;
198 char pbReader[MAX_READERNAME];
199 /* use DWORD type as this is what the PC/SC API expects */
200 DWORD dwReaderLen = sizeof(pbReader);
201 DWORD dwAtrLen = worker->card.atr_len = sizeof(worker->card.atr);
202 DWORD dwState, dwProt;
203
204 rc = SCardStatus(worker->reader.pcsc.hCard, pbReader, &dwReaderLen, &dwState, &dwProt,
205 worker->card.atr, &dwAtrLen);
206 PCSC_ERROR(worker, rc, "SCardStatus")
207 worker->card.atr_len = dwAtrLen;
208 LOGW(worker, "Card ATR: %s\n", osmo_hexdump_nospc(worker->card.atr, worker->card.atr_len));
209end:
210 return rc;
211}
212
Harald Welte753c8aa2020-10-20 23:04:59 +0200213
214static int pcsc_connect_slot_regex(struct bankd_worker *worker)
215{
216 DWORD dwReaders = SCARD_AUTOALLOCATE;
217 LPSTR mszReaders = NULL;
218 regex_t compiled_name;
219 int result = -1;
220 LONG rc;
221 char *p;
222
223 LOGW(worker, "Attempting to find card/slot using regex '%s'\n", worker->reader.name);
224
225 rc = regcomp(&compiled_name, worker->reader.name, REG_EXTENDED);
226 if (rc != 0) {
227 LOGW(worker, "Error compiling RegEx over name '%s'\n", worker->reader.name);
228 return -EINVAL;
229 }
230
231 rc = SCardListReaders(worker->reader.pcsc.hContext, NULL, (LPSTR)&mszReaders, &dwReaders);
232 if (rc != SCARD_S_SUCCESS) {
233 LOGW_PCSC_ERROR(worker, rc, "SCardListReaders");
234 goto out_regfree;
235 }
236
237 p = mszReaders;
238 while (*p) {
239 DWORD dwActiveProtocol;
240 int r = regexec(&compiled_name, p, 0, NULL, 0);
241 if (r == 0) {
242 LOGW(worker, "Attempting to open card/slot '%s'\n", p);
Harald Welte628672b2022-07-13 15:20:13 +0200243 rc = SCardConnect(worker->reader.pcsc.hContext, p, bankd_share_mode(worker->bankd),
Harald Welte753c8aa2020-10-20 23:04:59 +0200244 SCARD_PROTOCOL_T0, &worker->reader.pcsc.hCard,
245 &dwActiveProtocol);
246 if (rc == SCARD_S_SUCCESS)
247 result = 0;
Harald Welte7f8bce42022-05-05 19:08:13 +0200248 else {
Harald Welte753c8aa2020-10-20 23:04:59 +0200249 LOGW_PCSC_ERROR(worker, rc, "SCardConnect");
Harald Welte7f8bce42022-05-05 19:08:13 +0200250 goto out_readerfree;
251 }
Harald Welte753c8aa2020-10-20 23:04:59 +0200252 break;
253 }
254 p += strlen(p) + 1;
255 }
256
Harald Welte7f8bce42022-05-05 19:08:13 +0200257 if (result < 0)
258 LOGW(worker, "Error: Cannot find PC/SC reader/slot matching using regex '%s'\n", worker->reader.name);
259
260out_readerfree:
Harald Welte753c8aa2020-10-20 23:04:59 +0200261 SCardFreeMemory(worker->reader.pcsc.hContext, mszReaders);
262
263out_regfree:
264 regfree(&compiled_name);
265
266 return result;
267}
268
269
Harald Welte297d72e2019-03-28 18:42:35 +0100270static int pcsc_open_card(struct bankd_worker *worker)
271{
272 long rc;
273
274 if (!worker->reader.pcsc.hContext) {
275 LOGW(worker, "Attempting to open PC/SC context\n");
276 /* The PC/SC context must be created inside the thread where we'll later use it */
277 rc = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &worker->reader.pcsc.hContext);
278 PCSC_ERROR(worker, rc, "SCardEstablishContext")
279 }
280
281 if (!worker->reader.pcsc.hCard) {
Harald Welte753c8aa2020-10-20 23:04:59 +0200282 rc = pcsc_connect_slot_regex(worker);
283 if (rc != 0)
284 goto end;
Harald Welte297d72e2019-03-28 18:42:35 +0100285 }
286
Harald Weltebbd18bd2019-12-16 13:11:50 +0100287 rc = pcsc_get_atr(worker);
Harald Welte753c8aa2020-10-20 23:04:59 +0200288
Harald Weltebbd18bd2019-12-16 13:11:50 +0100289end:
290 return rc;
291}
292
293static int pcsc_reset_card(struct bankd_worker *worker, bool cold_reset)
294{
295 long rc;
296 DWORD dwActiveProtocol;
297
298 LOGW(worker, "Resetting card in '%s' (%s)\n", worker->reader.name,
299 cold_reset ? "cold reset" : "warm reset");
Harald Welte628672b2022-07-13 15:20:13 +0200300 rc = SCardReconnect(worker->reader.pcsc.hCard, bankd_share_mode(worker->bankd), SCARD_PROTOCOL_T0,
Harald Weltebbd18bd2019-12-16 13:11:50 +0100301 cold_reset ? SCARD_UNPOWER_CARD : SCARD_RESET_CARD, &dwActiveProtocol);
302 PCSC_ERROR(worker, rc, "SCardReconnect");
303
304 rc = pcsc_get_atr(worker);
Harald Welte297d72e2019-03-28 18:42:35 +0100305end:
306 return rc;
307}
308
309static int pcsc_transceive(struct bankd_worker *worker, const uint8_t *out, size_t out_len,
310 uint8_t *in, size_t *in_len)
311{
312 const SCARD_IO_REQUEST *pioSendPci = SCARD_PCI_T0;
313 SCARD_IO_REQUEST pioRecvPci;
314 long rc;
315
316 rc = SCardTransmit(worker->reader.pcsc.hCard, pioSendPci, out, out_len, &pioRecvPci, in, in_len);
Harald Welte50a09722021-12-08 15:33:12 +0100317 /* don't use PCSC_ERROR here as we don't want to log every successful SCardTransmit */
318 if (rc != SCARD_S_SUCCESS)
319 LOGW_PCSC_ERROR(worker, rc, "SCardTransmit");
Harald Welte297d72e2019-03-28 18:42:35 +0100320
Harald Welte297d72e2019-03-28 18:42:35 +0100321 return rc;
322}
323
324static void pcsc_cleanup(struct bankd_worker *worker)
325{
326 if (worker->reader.pcsc.hCard) {
327 SCardDisconnect(worker->reader.pcsc.hCard, SCARD_UNPOWER_CARD);
328 worker->reader.pcsc.hCard = 0;
329 }
330 if (worker->reader.pcsc.hContext) {
331 SCardReleaseContext(worker->reader.pcsc.hContext);
332 worker->reader.pcsc.hContext = 0;
333 }
334}
335
336const struct bankd_driver_ops pcsc_driver_ops = {
337 .open_card = pcsc_open_card,
Harald Weltebbd18bd2019-12-16 13:11:50 +0100338 .reset_card = pcsc_reset_card,
Harald Welte297d72e2019-03-28 18:42:35 +0100339 .transceive = pcsc_transceive,
340 .cleanup = pcsc_cleanup,
341};