1
1
/* call-scd.c - fork of the scdaemon to do SC operations
2
* Copyright (C) 2001, 2002, 2005 Free Software Foundation, Inc.
2
* Copyright (C) 2001, 2002, 2005, 2007 Free Software Foundation, Inc.
4
4
* This file is part of GnuPG.
6
6
* GnuPG is free software; you can redistribute it and/or modify
7
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
8
* the Free Software Foundation; either version 3 of the License, or
9
9
* (at your option) any later version.
11
11
* GnuPG is distributed in the hope that it will be useful,
14
14
* GNU General Public License for more details.
16
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
17
* along with this program; if not, see <http://www.gnu.org/licenses/>.
21
20
#include <config.h>
45
45
/* Definition of module local data of the CTRL structure. */
48
/* We keep a list of all allocated context with a an achnor at
49
SCD_LOCAL_LIST (see below). */
50
struct scd_local_s *next_local;
52
/* We need to get back to the ctrl object actually referencing this
53
structure. This is really an awkward way of enumerint the lcoal
54
contects. A much cleaner way would be to keep a global list of
55
ctrl objects to enumerate them. */
48
58
assuan_context_t ctx; /* NULL or session context for the SCdaemon
49
59
used with this connection. */
50
60
int locked; /* This flag is used to assert proper use of
69
79
assuan_context_t ctx;
70
80
int (*getpin_cb)(void *, const char *, char*, size_t);
71
81
void *getpin_cb_arg;
82
assuan_context_t passthru; /* If not NULL, pass unknown inquiries
87
/* To keep track of all active SCD contexts, we keep a linked list
88
anchored at this variable. */
89
static struct scd_local_s *scd_local_list;
75
91
/* A Mutex used inside the start_scd function. */
76
92
static pth_mutex_t start_scd_lock;
120
136
dump_mutex_state (pth_mutex_t *m)
139
log_printf ("unknown under W32");
122
141
if (!(m->mx_state & PTH_MUTEX_INITIALIZED))
123
142
log_printf ("not_initialized");
124
143
else if (!(m->mx_state & PTH_MUTEX_LOCKED))
125
144
log_printf ("not_locked");
127
146
log_printf ("locked tid=0x%lx count=%lu", (long)m->mx_owner, m->mx_count);
179
199
/* Fork off the SCdaemon if this has not already been done. Lock the
180
200
daemon and make sure that a proper context has been setup in CTRL.
181
Thsi fucntion might also lock the daemon, which means that the
201
This function might also lock the daemon, which means that the
182
202
caller must call unlock_scd after this fucntion has returned
183
203
success and the actual Assuan transaction been done. */
202
222
ctrl->scd_local = xtrycalloc (1, sizeof *ctrl->scd_local);
203
223
if (!ctrl->scd_local)
204
return gpg_error_from_errno (errno);
224
return gpg_error_from_syserror ();
225
ctrl->scd_local->ctrl_backlink = ctrl;
226
ctrl->scd_local->next_local = scd_local_list;
227
scd_local_list = ctrl->scd_local;
217
240
if (ctrl->scd_local->ctx)
218
241
return 0; /* Okay, the context is fine. We used to test for an
219
alive context here and do an disconnect. How that we
242
alive context here and do an disconnect. Now that we
220
243
have a ticker function to check for it, it is easier
221
244
not to check here but to let the connection run on an
222
245
error instead. */
272
295
if (fflush (NULL))
274
err = gpg_error (gpg_err_code_from_errno (errno));
297
#ifndef HAVE_W32_SYSTEM
298
err = gpg_error_from_syserror ();
275
300
log_error ("error flushing pending output: %s\n", strerror (errno));
301
/* At least Windows XP fails here with EBADF. According to docs
302
and Wine an fflush(NULL) is the same as _flushall. However
303
the Wime implementaion does not flush stdin,stdout and stderr
304
- see above. Lets try to ignore the error. */
305
#ifndef HAVE_W32_SYSTEM
279
310
if (!opt.scdaemon_program || !*opt.scdaemon_program)
280
opt.scdaemon_program = GNUPG_DEFAULT_SCDAEMON;
311
opt.scdaemon_program = gnupg_module_name (GNUPG_MODULE_NAME_SCDAEMON);
281
312
if ( !(pgmname = strrchr (opt.scdaemon_program, '/')))
282
313
pgmname = opt.scdaemon_program;
297
328
no_close_list[i] = -1;
299
/* Connect to the pinentry and perform initial handshaking */
300
rc = assuan_pipe_connect2 (&ctx, opt.scdaemon_program, (char**)argv,
301
no_close_list, atfork_cb, NULL);
330
/* Connect to the pinentry and perform initial handshaking. Use
331
detached flag (128) so that under W32 SCDAEMON does not show up a
333
rc = assuan_pipe_connect_ext (&ctx, opt.scdaemon_program, argv,
334
no_close_list, atfork_cb, NULL, 128);
304
337
log_error ("can't connect to the SCdaemon: %s\n",
305
assuan_strerror (rc));
306
339
err = gpg_error (GPG_ERR_NO_SCDAEMON);
343
376
/* Tell the scdaemon we want him to send us an event signal. */
344
#ifndef HAVE_W32_SYSTEM
348
sprintf (buf, "OPTION event-signal=%d", SIGUSR2);
380
#ifdef HAVE_W32_SYSTEM
381
snprintf (buf, sizeof buf, "OPTION event-signal=%lx",
382
(unsigned long)get_agent_scd_notify_event ());
384
snprintf (buf, sizeof buf, "OPTION event-signal=%d", SIGUSR2);
349
386
assuan_transact (ctx, buf, NULL, NULL, NULL, NULL, NULL, NULL);
353
389
primary_scd_ctx = ctx;
354
390
primary_scd_ctx_reusable = 0;
401
440
if (primary_scd_ctx)
403
442
pid = assuan_get_pid (primary_scd_ctx);
443
#ifdef HAVE_W32_SYSTEM
444
if (pid != (pid_t)(void*)(-1) && pid
445
&& !GetExitCodeProcess ((HANDLE)pid, &dummyec))
404
447
if (pid != (pid_t)(-1) && pid
405
448
&& ((rc=waitpid (pid, NULL, WNOHANG))==-1 || (rc == pid)) )
407
/* Okay, scdaemon died. Disconnect the primary connection now
408
but take care that it won't do another wait. */
451
/* Okay, scdaemon died. Disconnect the primary connection
452
now but take care that it won't do another wait. Also
453
cleanup all other connections and release their
454
resources. The next use will start a new daemon then.
455
Due to the use of the START_SCD_LOCAL we are sure that
456
none of these context are actually in use. */
457
struct scd_local_s *sl;
409
459
assuan_set_flag (primary_scd_ctx, ASSUAN_NO_WAITPID, 1);
410
460
assuan_disconnect (primary_scd_ctx);
462
for (sl=scd_local_list; sl; sl = sl->next_local)
466
if (sl->ctx != primary_scd_ctx)
467
assuan_disconnect (sl->ctx);
411
472
primary_scd_ctx = NULL;
412
473
primary_scd_ctx_reusable = 0;
413
475
xfree (socket_name);
414
476
socket_name = NULL;
437
500
if (ctrl->scd_local->ctx == primary_scd_ctx)
439
/* The RESET may fail for example if the scdaemon has
440
already been terminated. We need to set the reusable
441
flag anyway to make sure that the aliveness check can
443
assuan_transact (primary_scd_ctx, "RESET",
502
/* Send a RESTART to the SCD. This is required for the
503
primary connection as a kind of virtual EOF; we don't
504
have another way to tell it that the next command
505
should be viewed as if a new connection has been
506
made. For the non-primary connections this is not
507
needed as we simply close the socket. We don't check
508
for an error here because the RESTART may fail for
509
example if the scdaemon has already been terminated.
510
Anyway, we need to set the reusable flag to make sure
511
that the aliveness check can clean it up. */
512
assuan_transact (primary_scd_ctx, "RESTART",
444
513
NULL, NULL, NULL, NULL, NULL, NULL);
445
514
primary_scd_ctx_reusable = 1;
448
517
assuan_disconnect (ctrl->scd_local->ctx);
518
ctrl->scd_local->ctx = NULL;
521
/* Remove the local context from our list and release it. */
524
else if (scd_local_list == ctrl->scd_local)
525
scd_local_list = ctrl->scd_local->next_local;
528
struct scd_local_s *sl;
530
for (sl=scd_local_list; sl->next_local; sl = sl->next_local)
531
if (sl->next_local == ctrl->scd_local)
535
sl->next_local = ctrl->scd_local->next_local;
450
537
xfree (ctrl->scd_local);
451
538
ctrl->scd_local = NULL;
549
636
NULL, NULL, NULL, NULL,
550
637
learn_status_cb, &parm);
552
return unlock_scd (ctrl, map_assuan_err (rc));
639
return unlock_scd (ctrl, rc);
554
641
return unlock_scd (ctrl, 0);
560
647
get_serialno_cb (void *opaque, const char *line)
562
649
char **serialno = opaque;
572
659
if (keywordlen == 8 && !memcmp (keyword, "SERIALNO", keywordlen))
575
return ASSUAN_Unexpected_Status;
662
return gpg_error (GPG_ERR_CONFLICT); /* Unexpected status line. */
576
663
for (n=0,s=line; hexdigitp (s); s++, n++)
578
665
if (!n || (n&1)|| !(spacep (s) || !*s) )
579
return ASSUAN_Invalid_Status;
666
return gpg_error (GPG_ERR_ASS_PARAMETER);
580
667
*serialno = xtrymalloc (n+1);
582
return ASSUAN_Out_Of_Core;
669
return out_of_core ();
583
670
memcpy (*serialno, line, n);
584
671
(*serialno)[n] = 0;
643
730
pin = gcry_malloc_secure (pinlen);
645
return ASSUAN_Out_Of_Core;
732
return out_of_core ();
647
734
rc = parm->getpin_cb (parm->getpin_cb_arg, line, pin, pinlen);
649
rc = ASSUAN_Canceled;
651
736
rc = assuan_send_data (parm->ctx, pin, pinlen);
654
else if (!strncmp (line, "KEYPADINFO", 10) && (line[10] == ' ' || !line[10]))
739
else if (!strncmp (line, "POPUPKEYPADPROMPT", 17)
740
&& (line[17] == ' ' || !line[17]))
659
code = strtoul (line+10, &endp, 10);
661
743
while (*line == ' ')
664
rc = parm->getpin_cb (parm->getpin_cb_arg, line, NULL, code);
666
rc = ASSUAN_Canceled;
746
rc = parm->getpin_cb (parm->getpin_cb_arg, line, NULL, 1);
748
else if (!strncmp (line, "DISMISSKEYPADPROMPT", 19)
749
&& (line[19] == ' ' || !line[19]))
751
rc = parm->getpin_cb (parm->getpin_cb_arg, "", NULL, 0);
753
else if (parm->passthru)
755
unsigned char *value;
758
int needrest = !strncmp (line, "KEYDATA", 8);
760
/* Pass the inquiry up to our caller. We limit the maximum
761
amount to an arbitrary value. As we know that the KEYDATA
762
enquiry is pretty sensitive we disable logging then */
763
if ((rest = (needrest
764
&& !assuan_get_flag (parm->passthru, ASSUAN_CONFIDENTIAL))))
765
assuan_begin_confidential (parm->passthru);
766
rc = assuan_inquire (parm->passthru, line, &value, &valuelen, 8096);
768
assuan_end_confidential (parm->passthru);
771
if ((rest = (needrest
772
&& !assuan_get_flag (parm->ctx, ASSUAN_CONFIDENTIAL))))
773
assuan_begin_confidential (parm->ctx);
774
rc = assuan_send_data (parm->ctx, value, valuelen);
776
assuan_end_confidential (parm->ctx);
780
log_error ("error forwarding inquiry `%s': %s\n",
781
line, gpg_strerror (rc));
670
785
log_error ("unsupported inquiry `%s'\n", line);
671
rc = ASSUAN_Inquire_Unknown;
786
rc = gpg_error (GPG_ERR_ASS_UNKNOWN_INQUIRE);
708
823
rc = assuan_transact (ctrl->scd_local->ctx, line,
709
824
NULL, NULL, NULL, NULL, NULL, NULL);
711
return unlock_scd (ctrl, map_assuan_err (rc));
826
return unlock_scd (ctrl, rc);
713
828
init_membuf (&data, 1024);
714
829
inqparm.ctx = ctrl->scd_local->ctx;
715
830
inqparm.getpin_cb = getpin_cb;
716
831
inqparm.getpin_cb_arg = getpin_cb_arg;
832
inqparm.passthru = 0;
717
833
snprintf (line, DIM(line)-1,
718
834
ctrl->use_auth_call? "PKAUTH %s":"PKSIGN %s", keyid);
719
835
line[DIM(line)-1] = 0;
778
894
rc = assuan_transact (ctrl->scd_local->ctx, line,
779
895
NULL, NULL, NULL, NULL, NULL, NULL);
781
return unlock_scd (ctrl, map_assuan_err (rc));
897
return unlock_scd (ctrl, rc);
783
899
init_membuf (&data, 1024);
784
900
inqparm.ctx = ctrl->scd_local->ctx;
785
901
inqparm.getpin_cb = getpin_cb;
786
902
inqparm.getpin_cb_arg = getpin_cb_arg;
903
inqparm.passthru = 0;
787
904
snprintf (line, DIM(line)-1, "PKDECRYPT %s", keyid);
788
905
line[DIM(line)-1] = 0;
789
906
rc = assuan_transact (ctrl->scd_local->ctx, line,
950
err = map_assuan_err (assuan_transact (ctrl->scd_local->ctx, line,
951
NULL, NULL, NULL, NULL,
952
card_getattr_cb, &parm));
1067
err = assuan_transact (ctrl->scd_local->ctx, line,
1068
NULL, NULL, NULL, NULL,
1069
card_getattr_cb, &parm);
953
1070
if (!err && parm.error)
954
1071
err = gpg_error_from_errno (parm.error);
1000
1117
/* Send the line CMDLINE with command for the SCDdaemon to it and send
1001
1118
all status messages back. This command is used as a general quoting
1002
mechanism to pass everything verbatim to SCDAEMOPN. The PIN
1003
inquirey is handled inside gpg-agent. */
1119
mechanism to pass everything verbatim to SCDAEMON. The PIN
1120
inquiry is handled inside gpg-agent. */
1005
1122
agent_card_scd (ctrl_t ctrl, const char *cmdline,
1006
1123
int (*getpin_cb)(void *, const char *, char*, size_t),
1016
1133
inqparm.ctx = ctrl->scd_local->ctx;
1017
1134
inqparm.getpin_cb = getpin_cb;
1018
1135
inqparm.getpin_cb_arg = getpin_cb_arg;
1136
inqparm.passthru = assuan_context;
1019
1137
rc = assuan_transact (ctrl->scd_local->ctx, cmdline,
1020
1138
pass_data_thru, assuan_context,
1021
1139
inq_needpin, &inqparm,
1022
1140
pass_status_thru, assuan_context);
1025
return unlock_scd (ctrl, map_assuan_err (rc));
1143
return unlock_scd (ctrl, rc);
1028
1146
return unlock_scd (ctrl, 0);