123
179
return (s && (s == line || spacep (s-1)) && (!s[n] || spacep (s+n)));
127
/* Reset the card and free the application context. With DO_CLOSE set
128
to true and this is the last session with a reference to the
129
reader, close the reader and don't do just a reset. */
182
/* Same as has_option but does only test for the name of the option
183
and ignores an argument, i.e. with NAME being "--hash" it would
184
return a pointer for "--hash" as well as for "--hash=foo". If
185
thhere is no such option NULL is returned. The pointer returned
186
points right behind the option name, this may be an equal sign, Nul
189
has_option_name (const char *line, const char *name)
192
int n = strlen (name);
194
s = strstr (line, name);
195
return (s && (s == line || spacep (s-1))
196
&& (!s[n] || spacep (s+n) || s[n] == '=')) ? (s+n) : NULL;
200
/* Skip over options. It is assumed that leading spaces have been
201
removed (this is the case for lines passed to a handler from
202
assuan). Blanks after the options are also removed. */
204
skip_options (char *line)
206
while ( *line == '-' && line[1] == '-' )
208
while (*line && !spacep (line))
210
while (spacep (line))
218
/* Convert the STRING into a newly allocated buffer while translating
219
the hex numbers. Stops at the first invalid character. Blanks and
220
colons are allowed to separate the hex digits. Returns NULL on
221
error or a newly malloced buffer and its length in LENGTH. */
222
static unsigned char *
223
hex_to_buffer (const char *string, size_t *r_length)
225
unsigned char *buffer;
229
buffer = xtrymalloc (strlen (string)+1);
232
for (s=string, n=0; *s; s++)
234
if (spacep (s) || *s == ':')
236
if (hexdigitp (s) && hexdigitp (s+1))
238
buffer[n++] = xtoi_2 (s);
250
/* Reset the card and free the application context. With SEND_RESET
251
set to true actually send a RESET to the reader. */
131
do_reset (ctrl_t ctrl, int do_close)
253
do_reset (ctrl_t ctrl, int send_reset)
133
255
int slot = ctrl->reader_slot;
257
if (!(slot == -1 || (slot >= 0 && slot < DIM(slot_table))))
135
260
if (ctrl->app_ctx)
137
262
release_application (ctrl->app_ctx);
138
263
ctrl->app_ctx = NULL;
140
if (ctrl->reader_slot != -1)
142
struct server_local_s *sl;
144
/* If we are the only session with the reader open we may close
145
it. If not, do a reset unless the a lock is held on the
147
for (sl=session_list; sl; sl = sl->next_session)
148
if (sl != ctrl->server_local
149
&& sl->ctrl_backlink->reader_slot == ctrl->reader_slot)
151
if (sl) /* There is another session with the reader open. */
153
if ( IS_LOCKED (ctrl) ) /* If it is locked, release it. */
154
ctrl->reader_slot = -1;
157
if (do_close) /* Always mark reader unused. */
158
ctrl->reader_slot = -1;
159
else if (apdu_reset (ctrl->reader_slot)) /* Reset only if
162
/* The reset failed. Mark the reader as closed. */
163
ctrl->reader_slot = -1;
166
if (locked_session && ctrl->server_local == locked_session)
168
locked_session = NULL;
169
log_debug ("implicitly unlocking due to RESET\n");
173
else /* No other session has the reader open. */
175
if (do_close || apdu_reset (ctrl->reader_slot))
177
apdu_close_reader (ctrl->reader_slot);
178
ctrl->reader_slot = -1;
180
if ( IS_LOCKED (ctrl) )
182
log_debug ("WARNING: cleaning up stale session lock\n");
183
locked_session = NULL;
188
/* Reset card removed flag for the current reader. */
266
if (slot != -1 && send_reset && !IS_LOCKED (ctrl) )
268
if (apdu_reset (slot))
270
slot_table[slot].reset_failed = 1;
274
/* If we hold a lock, unlock now. */
275
if (locked_session && ctrl->server_local == locked_session)
277
locked_session = NULL;
278
log_info ("implicitly unlocking due to RESET\n");
281
/* Reset card removed flag for the current reader. We need to take
282
the lock here so that the ticker thread won't concurrently try to
283
update the file. Note that the update function will set the card
284
removed flag and we will later reset it - not a particualar nice
285
way of implementing it but it works. */
286
if (!pth_mutex_acquire (&status_file_update_lock, 0, NULL))
288
log_error ("failed to acquire status_fle_update lock\n");
289
ctrl->reader_slot = -1;
292
update_reader_status_file ();
189
293
update_card_removed (slot, 0);
294
if (!pth_mutex_release (&status_file_update_lock))
295
log_error ("failed to release status_file_update lock\n");
297
/* Do this last, so that update_card_removed does its job. */
298
ctrl->reader_slot = -1;
1288
1494
rc = gpg_error (GPG_ERR_NO_DATA);
1496
else if (!strcmp (line, "status"))
1498
ctrl_t ctrl = assuan_get_pointer (ctx);
1499
int slot = ctrl->reader_slot;
1502
if (!ctrl->server_local->card_removed && slot != -1)
1504
struct slot_status_s *ss;
1506
if (!(slot >= 0 && slot < DIM(slot_table)))
1509
ss = &slot_table[slot];
1514
if (ss->any && (ss->status & 1))
1517
rc = assuan_send_data (ctx, &flag, 1);
1519
else if (!strcmp (line, "reader_list"))
1522
char *s = ccid_get_reader_list ();
1528
rc = assuan_send_data (ctx, s, strlen (s));
1530
rc = gpg_error (GPG_ERR_NO_DATA);
1291
rc = set_error (Parameter_Error, "unknown value for WHAT");
1534
rc = set_error (GPG_ERR_ASS_PARAMETER, "unknown value for WHAT");
1541
Restart the current connection; this is a kind of warm reset. It
1542
deletes the context used by this connection but does not send a
1543
RESET to the card. Thus the card itself won't get reset.
1545
This is used by gpg-agent to reuse a primary pipe connection and
1546
may be used by clients to backup from a conflict in the serial
1547
command; i.e. to select another application.
1551
cmd_restart (assuan_context_t ctx, char *line)
1553
ctrl_t ctrl = assuan_get_pointer (ctx);
1557
release_application (ctrl->app_ctx);
1558
ctrl->app_ctx = NULL;
1560
if (locked_session && ctrl->server_local == locked_session)
1562
locked_session = NULL;
1563
log_info ("implicitly unlocking due to RESTART\n");
1569
/* APDU [--atr] [--more] [hexstring]
1571
Send an APDU to the current reader. This command bypasses the high
1572
level functions and sends the data directly to the card. HEXSTRING
1573
is expected to be a proper APDU. If HEXSTRING is not given no
1574
commands are set to the card but the command will implictly check
1575
whether the card is ready for use.
1577
Using the option "--atr" returns the ATR of the card as a status
1578
message before any data like this:
1579
S CARD-ATR 3BFA1300FF813180450031C173C00100009000B1
1581
Using the option --more handles the card status word MORE_DATA
1582
(61xx) and concatenate all reponses to one block.
1586
cmd_apdu (assuan_context_t ctx, char *line)
1588
ctrl_t ctrl = assuan_get_pointer (ctx);
1590
unsigned char *apdu;
1595
with_atr = has_option (line, "--atr");
1596
handle_more = has_option (line, "--more");
1598
/* Skip over options. */
1599
while ( *line == '-' && line[1] == '-' )
1601
while (*line && !spacep (line))
1603
while (spacep (line))
1607
if ( IS_LOCKED (ctrl) )
1608
return gpg_error (GPG_ERR_LOCKED);
1610
if ((rc = open_card (ctrl, NULL)))
1620
atr = apdu_get_atr (ctrl->reader_slot, &atrlen);
1621
if (!atr || atrlen > sizeof hexbuf - 2 )
1623
rc = gpg_error (GPG_ERR_INV_CARD);
1626
for (i=0; i < atrlen; i++)
1627
sprintf (hexbuf+2*i, "%02X", atr[i]);
1629
send_status_info (ctrl, "CARD-ATR", hexbuf, strlen (hexbuf), NULL, 0);
1632
apdu = hex_to_buffer (line, &apdulen);
1635
rc = gpg_error_from_syserror ();
1640
unsigned char *result = NULL;
1643
rc = apdu_send_direct (ctrl->reader_slot, apdu, apdulen, handle_more,
1644
&result, &resultlen);
1646
log_error ("apdu_send_direct failed: %s\n", gpg_strerror (rc));
1649
rc = assuan_send_data (ctx, result, resultlen);
1656
TEST_CARD_REMOVAL (ctrl, rc);
1366
rc = assuan_init_connected_socket_server (&ctx, fd);
1730
rc = assuan_init_socket_server_ext (&ctx, fd, 2);
1370
1734
log_error ("failed to initialize the server: %s\n",
1371
assuan_strerror(rc));
1374
1738
rc = register_commands (ctx);
1377
1741
log_error ("failed to register commands with Assuan: %s\n",
1378
assuan_strerror(rc));
1381
assuan_set_pointer (ctx, &ctrl);
1745
assuan_set_pointer (ctx, ctrl);
1383
1747
/* Allocate and initialize the server object. Put it into the list
1384
1748
of active sessions. */
1385
ctrl.server_local = xcalloc (1, sizeof *ctrl.server_local);
1386
ctrl.server_local->next_session = session_list;
1387
session_list = ctrl.server_local;
1388
ctrl.server_local->ctrl_backlink = &ctrl;
1389
ctrl.server_local->assuan_ctx = ctx;
1749
ctrl->server_local = xcalloc (1, sizeof *ctrl->server_local);
1750
ctrl->server_local->next_session = session_list;
1751
session_list = ctrl->server_local;
1752
ctrl->server_local->ctrl_backlink = ctrl;
1753
ctrl->server_local->assuan_ctx = ctx;
1391
1755
if (DBG_ASSUAN)
1392
1756
assuan_set_log_stream (ctx, log_get_stream ());
1394
1758
/* We open the reader right at startup so that the ticker is able to
1395
1759
update the status file. */
1396
if (ctrl.reader_slot == -1)
1760
if (ctrl->reader_slot == -1)
1398
ctrl.reader_slot = get_reader_slot ();
1762
ctrl->reader_slot = get_reader_slot ();
1401
1765
/* Command processing loop. */
1411
log_info ("Assuan accept problem: %s\n", assuan_strerror (rc));
1775
log_info ("Assuan accept problem: %s\n", gpg_strerror (rc));
1415
1779
rc = assuan_process (ctx);
1418
log_info ("Assuan processing failed: %s\n", assuan_strerror (rc));
1782
log_info ("Assuan processing failed: %s\n", gpg_strerror (rc));
1424
do_reset (&ctrl, 1);
1426
1790
/* Release the server object. */
1427
if (session_list == ctrl.server_local)
1428
session_list = ctrl.server_local->next_session;
1791
if (session_list == ctrl->server_local)
1792
session_list = ctrl->server_local->next_session;
1431
1795
struct server_local_s *sl;
1433
1797
for (sl=session_list; sl->next_session; sl = sl->next_session)
1434
if (sl->next_session == ctrl.server_local)
1798
if (sl->next_session == ctrl->server_local)
1436
1800
if (!sl->next_session)
1438
sl->next_session = ctrl.server_local->next_session;
1802
sl->next_session = ctrl->server_local->next_session;
1440
xfree (ctrl.server_local);
1804
xfree (ctrl->server_local);
1805
ctrl->server_local = NULL;
1442
1807
/* Release the Assuan context. */
1443
1808
assuan_deinit_server (ctx);
1494
/* This function is called by the ticker thread to check for changes
1495
of the reader stati. It updates the reader status files and if
1496
requested by the caller also send a signal to the caller. */
1498
scd_update_reader_status_file (void)
1859
/* This is the core of scd_update_reader_status_file but the caller
1860
needs to take care of the locking. */
1862
update_reader_status_file (void)
1502
unsigned int status;
1503
unsigned int changed;
1507
1865
unsigned int status, changed;
1509
1867
/* Note, that we only try to get the status, because it does not
1510
1868
make sense to wait here for a operation to complete. If we are
1511
1869
busy working with a card, delays in the status file update should
1512
1870
be acceptable. */
1513
for (slot=0; (slot < DIM(last)
1514
&&!apdu_enum_reader (slot, &used)); slot++)
1515
if (used && !apdu_get_status (slot, 0, &status, &changed))
1517
if (!last[slot].any || last[slot].status != status
1518
|| last[slot].changed != changed )
1871
for (idx=0; idx < DIM(slot_table); idx++)
1873
struct slot_status_s *ss = slot_table + idx;
1875
if (!ss->valid || ss->slot == -1)
1876
continue; /* Not valid or reader not yet open. */
1878
if ( apdu_get_status (ss->slot, 0, &status, &changed) )
1879
continue; /* Get status failed. */
1881
if (!ss->any || ss->status != status || ss->changed != changed )
1886
struct server_local_s *sl;
1888
log_info ("updating status of slot %d to 0x%04X\n",
1891
/* FIXME: Should this be IDX instead of ss->slot? This
1892
depends on how client sessions will associate the reader
1893
status with their session. */
1894
sprintf (templ, "reader_%d.status", ss->slot);
1895
fname = make_filename (opt.homedir, templ, NULL );
1896
fp = fopen (fname, "w");
1899
fprintf (fp, "%s\n",
1900
(status & 1)? "USABLE":
1901
(status & 4)? "ACTIVE":
1902
(status & 2)? "PRESENT": "NOCARD");
1907
/* If a status script is executable, run it. */
1523
struct server_local_s *sl;
1525
log_info ("updating status of slot %d to 0x%04X\n", slot, status);
1909
const char *args[9], *envs[2];
1910
char numbuf1[30], numbuf2[30], numbuf3[30];
1911
char *homestr, *envstr;
1527
sprintf (templ, "reader_%d.status", slot);
1528
fname = make_filename (opt.homedir, templ, NULL );
1529
fp = fopen (fname, "w");
1914
homestr = make_filename (opt.homedir, NULL);
1915
if (asprintf (&envstr, "GNUPGHOME=%s", homestr) < 0)
1916
log_error ("out of core while building environment\n");
1532
fprintf (fp, "%s\n",
1533
(status & 1)? "USABLE":
1534
(status & 4)? "ACTIVE":
1535
(status & 2)? "PRESENT": "NOCARD");
1922
sprintf (numbuf1, "%d", ss->slot);
1923
sprintf (numbuf2, "0x%04X", ss->status);
1924
sprintf (numbuf3, "0x%04X", status);
1925
args[0] = "--reader-port";
1927
args[2] = "--old-code";
1929
args[4] = "--new-code";
1931
args[6] = "--status";
1932
args[7] = ((status & 1)? "USABLE":
1933
(status & 4)? "ACTIVE":
1934
(status & 2)? "PRESENT": "NOCARD");
1937
fname = make_filename (opt.homedir, "scd-event", NULL);
1938
err = gnupg_spawn_process_detached (fname, args, envs);
1939
if (err && gpg_err_code (err) != GPG_ERR_ENOENT)
1940
log_error ("failed to run event handler `%s': %s\n",
1941
fname, gpg_strerror (err));
1540
/* Set the card removed flag for all current sessions. We
1541
will set this on any card change because a reset or
1542
SERIALNO request must be done in any case. */
1544
update_card_removed (slot, 1);
1547
last[slot].status = status;
1548
last[slot].changed = changed;
1551
/* Send a signal to all clients who applied for it. */
1552
for (sl=session_list; sl; sl = sl->next_session)
1553
if (sl->event_signal && sl->assuan_ctx)
1555
pid_t pid = assuan_get_pid (sl->assuan_ctx);
1556
int signo = sl->event_signal;
1558
log_info ("client pid is %d, sending signal %d\n",
1948
/* Set the card removed flag for all current sessions. We
1949
will set this on any card change because a reset or
1950
SERIALNO request must be done in any case. */
1952
update_card_removed (idx, 1);
1955
ss->status = status;
1956
ss->changed = changed;
1958
/* Send a signal to all clients who applied for it. */
1959
for (sl=session_list; sl; sl = sl->next_session)
1960
if (sl->event_signal && sl->assuan_ctx)
1962
pid_t pid = assuan_get_pid (sl->assuan_ctx);
1963
int signo = sl->event_signal;
1965
log_info ("client pid is %d, sending signal %d\n",
1560
1967
#ifndef HAVE_W32_SYSTEM
1561
if (pid != (pid_t)(-1) && pid && signo > 0)
1968
if (pid != (pid_t)(-1) && pid && signo > 0)
1977
/* This function is called by the ticker thread to check for changes
1978
of the reader stati. It updates the reader status files and if
1979
requested by the caller also send a signal to the caller. */
1981
scd_update_reader_status_file (void)
1983
if (!pth_mutex_acquire (&status_file_update_lock, 1, NULL))
1984
return; /* locked - give up. */
1985
update_reader_status_file ();
1986
if (!pth_mutex_release (&status_file_update_lock))
1987
log_error ("failed to release status_file_update lock\n");