1 /* command.c - gpg-agent command handler
2 * Copyright (C) 2001, 2002, 2003, 2004, 2005,
3 * 2006, 2008 Free Software Foundation, Inc.
5 * This file is part of GnuPG.
7 * GnuPG 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 3 of the License, or
10 * (at your option) any later version.
12 * GnuPG 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.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 /* FIXME: we should not use the default assuan buffering but setup
22 some buffering in secure mempory to protect session keys etc. */
38 /* maximum allowed size of the inquired ciphertext */
39 #define MAXLEN_CIPHERTEXT 4096
40 /* maximum allowed size of the key parameters */
41 #define MAXLEN_KEYPARAM 1024
43 #define set_error(e,t) assuan_set_error (ctx, gpg_error (e), (t))
46 #if MAX_DIGEST_LEN < 20
47 #error MAX_DIGEST_LEN shorter than keygrip
50 /* Data used to associate an Assuan context with local server data */
53 assuan_context_t assuan_ctx
;
55 int use_cache_for_signing
;
56 char *keydesc
; /* Allocated description for the next key
58 int pause_io_logging
; /* Used to suppress I/O logging during a command */
59 #ifdef HAVE_W32_SYSTEM
60 int stopme
; /* If set to true the agent will be terminated after
61 the end of this session. */
63 int allow_pinentry_notify
; /* Set if pinentry notifications should
68 /* An entry for the getval/putval commands. */
71 struct putval_item_s
*next
;
72 size_t off
; /* Offset to the value into DATA. */
73 size_t len
; /* Length of the value. */
74 char d
[1]; /* Key | Nul | value. */
78 /* A list of key value pairs fpr the getval/putval commands. */
79 static struct putval_item_s
*putval_list
;
83 /* To help polling clients, we keep track of the number of certain
84 events. This structure keeps those counters. The counters are
85 integers and there should be no problem if they are overflowing as
86 callers need to check only whether a counter changed. The actual
87 values are not meaningful. */
90 /* Incremented if any of the other counters below changed. */
93 /* Incremented if a key is added or removed from the internal privat
97 /* Incremented if a change of the card readers stati has been
107 /* Release the memory buffer MB but first wipe out the used memory. */
109 clear_outbuf (membuf_t
*mb
)
114 p
= get_membuf (mb
, &n
);
123 /* Write the content of memory buffer MB as assuan data to CTX and
124 wipe the buffer out afterwards. */
126 write_and_clear_outbuf (assuan_context_t ctx
, membuf_t
*mb
)
132 p
= get_membuf (mb
, &n
);
134 return out_of_core ();
135 ae
= assuan_send_data (ctx
, p
, n
);
143 reset_notify (assuan_context_t ctx
)
145 ctrl_t ctrl
= assuan_get_pointer (ctx
);
147 memset (ctrl
->keygrip
, 0, 20);
148 ctrl
->have_keygrip
= 0;
149 ctrl
->digest
.valuelen
= 0;
151 xfree (ctrl
->server_local
->keydesc
);
152 ctrl
->server_local
->keydesc
= NULL
;
156 /* Check whether the option NAME appears in LINE */
158 has_option (const char *line
, const char *name
)
161 int n
= strlen (name
);
163 s
= strstr (line
, name
);
164 return (s
&& (s
== line
|| spacep (s
-1)) && (!s
[n
] || spacep (s
+n
)));
167 /* Same as has_option but does only test for the name of the option
168 and ignores an argument, i.e. with NAME being "--hash" it would
169 return true for "--hash" as well as for "--hash=foo". */
171 has_option_name (const char *line
, const char *name
)
174 int n
= strlen (name
);
176 s
= strstr (line
, name
);
177 return (s
&& (s
== line
|| spacep (s
-1))
178 && (!s
[n
] || spacep (s
+n
) || s
[n
] == '='));
182 /* Skip over options. It is assumed that leading spaces have been
183 removed (this is the case for lines passed to a handler from
184 assuan). Blanks after the options are also removed. */
186 skip_options (char *line
)
188 while ( *line
== '-' && line
[1] == '-' )
190 while (*line
&& !spacep (line
))
192 while (spacep (line
))
199 /* Replace all '+' by a blank. */
201 plus_to_blank (char *s
)
211 /* Do the percent and plus/space unescaping in place and return the
212 length of the valid buffer. */
214 percent_plus_unescape (char *string
)
216 unsigned char *p
= (unsigned char *)string
;
221 if (*string
== '%' && string
[1] && string
[2])
224 *p
++ = xtoi_2 (string
);
228 else if (*string
== '+')
247 /* Parse a hex string. Return an Assuan error code or 0 on success and the
248 length of the parsed string in LEN. */
250 parse_hexstring (assuan_context_t ctx
, const char *string
, size_t *len
)
255 /* parse the hash value */
256 for (p
=string
, n
=0; hexdigitp (p
); p
++, n
++)
258 if (*p
!= ' ' && *p
!= '\t' && *p
)
259 return set_error (GPG_ERR_ASS_PARAMETER
, "invalid hexstring");
261 return set_error (GPG_ERR_ASS_PARAMETER
, "odd number of digits");
266 /* Parse the keygrip in STRING into the provided buffer BUF. BUF must
267 provide space for 20 bytes. BUF is not changed if the function
270 parse_keygrip (assuan_context_t ctx
, const char *string
, unsigned char *buf
)
275 rc
= parse_hexstring (ctx
, string
, &n
);
280 return set_error (GPG_ERR_ASS_PARAMETER
, "invalid length of keygrip");
282 if (hex2bin (string
, buf
, 20) < 0)
283 return set_error (GPG_ERR_BUG
, "hex2bin");
289 /* Write an assuan status line. */
291 agent_write_status (ctrl_t ctrl
, const char *keyword
, ...)
296 assuan_context_t ctx
= ctrl
->server_local
->assuan_ctx
;
300 va_start (arg_ptr
, keyword
);
304 while ( (text
= va_arg (arg_ptr
, const char *)) )
311 for ( ; *text
&& n
< DIM (buf
)-2; n
++)
315 err
= assuan_write_status (ctx
, keyword
, buf
);
322 /* Helper to notify the client about a launched Pinentry. Because
323 that might disturb some older clients, this is only done if enabled
324 via an option. Returns an gpg error code. */
326 agent_inq_pinentry_launched (ctrl_t ctrl
, unsigned long pid
)
330 if (!ctrl
|| !ctrl
->server_local
331 || !ctrl
->server_local
->allow_pinentry_notify
)
333 snprintf (line
, DIM(line
)-1, "PINENTRY_LAUNCHED %lu", pid
);
334 return assuan_inquire (ctrl
->server_local
->assuan_ctx
, line
, NULL
, NULL
, 0);
341 Return a a status line named EVENTCOUNTER with the current values
342 of all event counters. The values are decimal numbers in the range
343 0 to UINT_MAX and wrapping around to 0. The actual values should
344 not be relied upon, they shall only be used to detect a change.
346 The currently defined counters are:
348 ANY - Incremented with any change of any of the other counters.
349 KEY - Incremented for added or removed private keys.
350 CARD - Incremented for changes of the card readers stati.
353 cmd_geteventcounter (assuan_context_t ctx
, char *line
)
355 ctrl_t ctrl
= assuan_get_pointer (ctx
);
356 char any_counter
[25];
357 char key_counter
[25];
358 char card_counter
[25];
362 snprintf (any_counter
, sizeof any_counter
, "%u", eventcounter
.any
);
363 snprintf (key_counter
, sizeof key_counter
, "%u", eventcounter
.key
);
364 snprintf (card_counter
, sizeof card_counter
, "%u", eventcounter
.card
);
366 return agent_write_status (ctrl
, "EVENTCOUNTER",
374 /* This function should be called once for all key removals or
375 additions. This function is assured not to do any context
378 bump_key_eventcounter (void)
384 /* This function should be called for all card reader status
385 changes. This function is assured not to do any context
388 bump_card_eventcounter (void)
397 /* ISTRUSTED <hexstring_with_fingerprint>
399 Return OK when we have an entry with this fingerprint in our
402 cmd_istrusted (assuan_context_t ctx
, char *line
)
404 ctrl_t ctrl
= assuan_get_pointer (ctx
);
409 /* Parse the fingerprint value. */
410 for (p
=line
,n
=0; hexdigitp (p
); p
++, n
++)
412 if (*p
|| !(n
== 40 || n
== 32))
413 return set_error (GPG_ERR_ASS_PARAMETER
, "invalid fingerprint");
417 strcpy (fpr
, "00000000");
420 for (p
=line
; i
< 40; p
++, i
++)
421 fpr
[i
] = *p
>= 'a'? (*p
& 0xdf): *p
;
423 rc
= agent_istrusted (ctrl
, fpr
);
424 if (!rc
|| gpg_err_code (rc
) == GPG_ERR_NOT_TRUSTED
)
426 else if (rc
== -1 || gpg_err_code (rc
) == GPG_ERR_EOF
)
427 return gpg_error (GPG_ERR_NOT_TRUSTED
);
430 log_error ("command is_trusted failed: %s\n", gpg_strerror (rc
));
437 List all entries from the trustlist */
439 cmd_listtrusted (assuan_context_t ctx
, char *line
)
445 rc
= agent_listtrusted (ctx
);
447 log_error ("command listtrusted failed: %s\n", gpg_strerror (rc
));
452 /* MARKTRUSTED <hexstring_with_fingerprint> <flag> <display_name>
454 Store a new key in into the trustlist*/
456 cmd_marktrusted (assuan_context_t ctx
, char *line
)
458 ctrl_t ctrl
= assuan_get_pointer (ctx
);
464 /* parse the fingerprint value */
465 for (p
=line
,n
=0; hexdigitp (p
); p
++, n
++)
467 if (!spacep (p
) || !(n
== 40 || n
== 32))
468 return set_error (GPG_ERR_ASS_PARAMETER
, "invalid fingerprint");
472 strcpy (fpr
, "00000000");
475 for (p
=line
; i
< 40; p
++, i
++)
476 fpr
[i
] = *p
>= 'a'? (*p
& 0xdf): *p
;
482 if ( (flag
!= 'S' && flag
!= 'P') || !spacep (p
) )
483 return set_error (GPG_ERR_ASS_PARAMETER
, "invalid flag - must be P or S");
487 rc
= agent_marktrusted (ctrl
, p
, fpr
, flag
);
489 log_error ("command marktrusted failed: %s\n", gpg_strerror (rc
));
496 /* HAVEKEY <hexstring_with_keygrip>
498 Return success when the secret key is available */
500 cmd_havekey (assuan_context_t ctx
, char *line
)
503 unsigned char buf
[20];
505 rc
= parse_keygrip (ctx
, line
, buf
);
509 if (agent_key_available (buf
))
510 return gpg_error (GPG_ERR_NO_SECKEY
);
516 /* SIGKEY <hexstring_with_keygrip>
517 SETKEY <hexstring_with_keygrip>
519 Set the key used for a sign or decrypt operation */
521 cmd_sigkey (assuan_context_t ctx
, char *line
)
524 ctrl_t ctrl
= assuan_get_pointer (ctx
);
526 rc
= parse_keygrip (ctx
, line
, ctrl
->keygrip
);
529 ctrl
->have_keygrip
= 1;
534 /* SETKEYDESC plus_percent_escaped_string
536 Set a description to be used for the next PKSIGN or PKDECRYPT
537 operation if this operation requires the entry of a passphrase. If
538 this command is not used a default text will be used. Note, that
539 this description implictly selects the label used for the entry
540 box; if the string contains the string PIN (which in general will
541 not be translated), "PIN" is used, otherwise the translation of
542 "passphrase" is used. The description string should not contain
543 blanks unless they are percent or '+' escaped.
545 The description is only valid for the next PKSIGN or PKDECRYPT
549 cmd_setkeydesc (assuan_context_t ctx
, char *line
)
551 ctrl_t ctrl
= assuan_get_pointer (ctx
);
554 for (p
=line
; *p
== ' '; p
++)
557 p
= strchr (desc
, ' ');
559 *p
= 0; /* We ignore any garbage; we might late use it for other args. */
562 return set_error (GPG_ERR_ASS_PARAMETER
, "no description given");
564 /* Note, that we only need to replace the + characters and should
565 leave the other escaping in place because the escaped string is
566 send verbatim to the pinentry which does the unescaping (but not
568 plus_to_blank (desc
);
570 xfree (ctrl
->server_local
->keydesc
);
571 ctrl
->server_local
->keydesc
= xtrystrdup (desc
);
572 if (!ctrl
->server_local
->keydesc
)
573 return out_of_core ();
578 /* SETHASH --hash=<name>|<algonumber> <hexstring>
580 The client can use this command to tell the server about the data
581 (which usually is a hash) to be signed. */
583 cmd_sethash (assuan_context_t ctx
, char *line
)
588 ctrl_t ctrl
= assuan_get_pointer (ctx
);
593 /* Parse the alternative hash options which may be used instead of
595 if (has_option_name (line
, "--hash"))
597 if (has_option (line
, "--hash=sha1"))
599 else if (has_option (line
, "--hash=sha256"))
600 algo
= GCRY_MD_SHA256
;
601 else if (has_option (line
, "--hash=rmd160"))
602 algo
= GCRY_MD_RMD160
;
603 else if (has_option (line
, "--hash=md5"))
605 else if (has_option (line
, "--hash=tls-md5sha1"))
606 algo
= MD_USER_TLS_MD5SHA1
;
608 return set_error (GPG_ERR_ASS_PARAMETER
, "invalid hash algorithm");
613 line
= skip_options (line
);
617 /* No hash option has been given: require an algo number instead */
618 algo
= (int)strtoul (line
, &endp
, 10);
619 for (line
= endp
; *line
== ' ' || *line
== '\t'; line
++)
621 if (!algo
|| gcry_md_test_algo (algo
))
622 return set_error (GPG_ERR_UNSUPPORTED_ALGORITHM
, NULL
);
624 ctrl
->digest
.algo
= algo
;
626 /* Parse the hash value. */
627 rc
= parse_hexstring (ctx
, line
, &n
);
631 if (algo
== MD_USER_TLS_MD5SHA1
&& n
== 36)
633 else if (n
!= 16 && n
!= 20 && n
!= 24 && n
!= 32)
634 return set_error (GPG_ERR_ASS_PARAMETER
, "unsupported length of hash");
636 if (n
> MAX_DIGEST_LEN
)
637 return set_error (GPG_ERR_ASS_PARAMETER
, "hash value to long");
639 buf
= ctrl
->digest
.value
;
640 ctrl
->digest
.valuelen
= n
;
641 for (p
=line
, n
=0; n
< ctrl
->digest
.valuelen
; p
+= 2, n
++)
643 for (; n
< ctrl
->digest
.valuelen
; n
++)
651 Perform the actual sign operation. Neither input nor output are
652 sensitive to eavesdropping. */
654 cmd_pksign (assuan_context_t ctx
, char *line
)
657 cache_mode_t cache_mode
= CACHE_MODE_NORMAL
;
658 ctrl_t ctrl
= assuan_get_pointer (ctx
);
663 if (opt
.ignore_cache_for_signing
)
664 cache_mode
= CACHE_MODE_IGNORE
;
665 else if (!ctrl
->server_local
->use_cache_for_signing
)
666 cache_mode
= CACHE_MODE_IGNORE
;
668 init_membuf (&outbuf
, 512);
670 rc
= agent_pksign (ctrl
, ctrl
->server_local
->keydesc
,
671 &outbuf
, cache_mode
);
673 clear_outbuf (&outbuf
);
675 rc
= write_and_clear_outbuf (ctx
, &outbuf
);
677 log_error ("command pksign failed: %s\n", gpg_strerror (rc
));
678 xfree (ctrl
->server_local
->keydesc
);
679 ctrl
->server_local
->keydesc
= NULL
;
683 /* PKDECRYPT <options>
685 Perform the actual decrypt operation. Input is not
686 sensitive to eavesdropping */
688 cmd_pkdecrypt (assuan_context_t ctx
, char *line
)
691 ctrl_t ctrl
= assuan_get_pointer (ctx
);
692 unsigned char *value
;
698 /* First inquire the data to decrypt */
699 rc
= assuan_inquire (ctx
, "CIPHERTEXT",
700 &value
, &valuelen
, MAXLEN_CIPHERTEXT
);
704 init_membuf (&outbuf
, 512);
706 rc
= agent_pkdecrypt (ctrl
, ctrl
->server_local
->keydesc
,
707 value
, valuelen
, &outbuf
);
710 clear_outbuf (&outbuf
);
712 rc
= write_and_clear_outbuf (ctx
, &outbuf
);
714 log_error ("command pkdecrypt failed: %s\n", gpg_strerror (rc
));
715 xfree (ctrl
->server_local
->keydesc
);
716 ctrl
->server_local
->keydesc
= NULL
;
723 Generate a new key, store the secret part and return the public
724 part. Here is an example transaction:
728 C: D (genkey (rsa (nbits 1024)))
731 S: D (rsa (n 326487324683264) (e 10001)))
736 cmd_genkey (assuan_context_t ctx
, char *line
)
738 ctrl_t ctrl
= assuan_get_pointer (ctx
);
740 unsigned char *value
;
746 /* First inquire the parameters */
747 rc
= assuan_inquire (ctx
, "KEYPARAM", &value
, &valuelen
, MAXLEN_KEYPARAM
);
751 init_membuf (&outbuf
, 512);
753 rc
= agent_genkey (ctrl
, (char*)value
, valuelen
, &outbuf
);
756 clear_outbuf (&outbuf
);
758 rc
= write_and_clear_outbuf (ctx
, &outbuf
);
760 log_error ("command genkey failed: %s\n", gpg_strerror (rc
));
767 /* READKEY <hexstring_with_keygrip>
769 Return the public key for the given keygrip. */
771 cmd_readkey (assuan_context_t ctx
, char *line
)
773 ctrl_t ctrl
= assuan_get_pointer (ctx
);
775 unsigned char grip
[20];
776 gcry_sexp_t s_pkey
= NULL
;
778 rc
= parse_keygrip (ctx
, line
, grip
);
780 return rc
; /* Return immediately as this is already an Assuan error code.*/
782 rc
= agent_public_key_from_file (ctrl
, grip
, &s_pkey
);
788 len
= gcry_sexp_sprint (s_pkey
, GCRYSEXP_FMT_CANON
, NULL
, 0);
790 buf
= xtrymalloc (len
);
792 rc
= gpg_error_from_syserror ();
795 len
= gcry_sexp_sprint (s_pkey
, GCRYSEXP_FMT_CANON
, buf
, len
);
797 rc
= assuan_send_data (ctx
, buf
, len
);
800 gcry_sexp_release (s_pkey
);
804 log_error ("command readkey failed: %s\n", gpg_strerror (rc
));
814 send_back_passphrase (assuan_context_t ctx
, int via_data
, const char *pw
)
819 assuan_begin_confidential (ctx
);
822 rc
= assuan_send_data (ctx
, pw
, n
);
825 char *p
= xtrymalloc_secure (n
*2+1);
827 rc
= gpg_error_from_syserror ();
831 rc
= assuan_set_okay_line (ctx
, p
);
839 /* GET_PASSPHRASE [--data] [--check] [--no-ask] <cache_id>
840 [<error_message> <prompt> <description>]
842 This function is usually used to ask for a passphrase to be used
843 for conventional encryption, but may also be used by programs which
844 need specal handling of passphrases. This command uses a syntax
845 which helps clients to use the agent with minimum effort. The
846 agent either returns with an error or with a OK followed by the hex
847 encoded passphrase. Note that the length of the strings is
848 implicitly limited by the maximum length of a command.
850 If the option "--data" is used the passphrase is returned by usual
851 data lines and not on the okay line.
853 If the option "--check" is used the passphrase constraints checks as
854 implemented by gpg-agent are applied. A check is not done if the
855 passphrase has been found in the cache.
857 If the option "--no-ask" is used and the passphrase is not in the
858 cache the user will not be asked to enter a passphrase but the error
859 code GPG_ERR_NO_DATA is returned.
863 cmd_get_passphrase (assuan_context_t ctx
, char *line
)
865 ctrl_t ctrl
= assuan_get_pointer (ctx
);
869 char *cacheid
= NULL
, *desc
= NULL
, *prompt
= NULL
, *errtext
= NULL
;
872 int opt_data
, opt_check
, opt_no_ask
;
874 opt_data
= has_option (line
, "--data");
875 opt_check
= has_option (line
, "--check");
876 opt_no_ask
= has_option (line
, "--no-ask");
877 line
= skip_options (line
);
880 p
= strchr (cacheid
, ' ');
887 p
= strchr (errtext
, ' ');
894 p
= strchr (prompt
, ' ');
901 p
= strchr (desc
, ' ');
903 *p
= 0; /* Ignore trailing garbage. */
907 if (!cacheid
|| !*cacheid
|| strlen (cacheid
) > 50)
908 return set_error (GPG_ERR_ASS_PARAMETER
, "invalid length of cacheID");
910 return set_error (GPG_ERR_ASS_PARAMETER
, "no description given");
912 if (!strcmp (cacheid
, "X"))
914 if (!strcmp (errtext
, "X"))
916 if (!strcmp (prompt
, "X"))
918 if (!strcmp (desc
, "X"))
921 pw
= cacheid
? agent_get_cache (cacheid
, CACHE_MODE_NORMAL
, &cache_marker
)
925 rc
= send_back_passphrase (ctx
, opt_data
, pw
);
926 agent_unlock_cache_entry (&cache_marker
);
929 rc
= gpg_error (GPG_ERR_NO_DATA
);
932 /* Note, that we only need to replace the + characters and
933 should leave the other escaping in place because the escaped
934 string is send verbatim to the pinentry which does the
935 unescaping (but not the + replacing) */
937 plus_to_blank (errtext
);
939 plus_to_blank (prompt
);
941 plus_to_blank (desc
);
947 rc
= agent_get_passphrase (ctrl
, &response
, desc
, prompt
, errtext
);
951 && check_passphrase_constraints (ctrl
, response
, 0));
956 agent_put_cache (cacheid
, CACHE_MODE_USER
, response
, 0);
957 rc
= send_back_passphrase (ctx
, opt_data
, response
);
963 log_error ("command get_passphrase failed: %s\n", gpg_strerror (rc
));
968 /* CLEAR_PASSPHRASE <cache_id>
970 may be used to invalidate the cache entry for a passphrase. The
971 function returns with OK even when there is no cached passphrase.
975 cmd_clear_passphrase (assuan_context_t ctx
, char *line
)
977 char *cacheid
= NULL
;
980 /* parse the stuff */
981 for (p
=line
; *p
== ' '; p
++)
984 p
= strchr (cacheid
, ' ');
986 *p
= 0; /* ignore garbage */
987 if (!cacheid
|| !*cacheid
|| strlen (cacheid
) > 50)
988 return set_error (GPG_ERR_ASS_PARAMETER
, "invalid length of cacheID");
990 agent_put_cache (cacheid
, CACHE_MODE_USER
, NULL
, 0);
995 /* GET_CONFIRMATION <description>
997 This command may be used to ask for a simple confirmation.
998 DESCRIPTION is displayed along with a Okay and Cancel button. This
999 command uses a syntax which helps clients to use the agent with
1000 minimum effort. The agent either returns with an error or with a
1001 OK. Note, that the length of DESCRIPTION is implicitly limited by
1002 the maximum length of a command. DESCRIPTION should not contain
1003 any spaces, those must be encoded either percent escaped or simply
1008 cmd_get_confirmation (assuan_context_t ctx
, char *line
)
1010 ctrl_t ctrl
= assuan_get_pointer (ctx
);
1015 /* parse the stuff */
1016 for (p
=line
; *p
== ' '; p
++)
1019 p
= strchr (desc
, ' ');
1021 *p
= 0; /* We ignore any garbage -may be later used for other args. */
1023 if (!desc
|| !*desc
)
1024 return set_error (GPG_ERR_ASS_PARAMETER
, "no description given");
1026 if (!strcmp (desc
, "X"))
1029 /* Note, that we only need to replace the + characters and should
1030 leave the other escaping in place because the escaped string is
1031 send verbatim to the pinentry which does the unescaping (but not
1034 plus_to_blank (desc
);
1036 rc
= agent_get_confirmation (ctrl
, desc
, NULL
, NULL
);
1038 log_error ("command get_confirmation failed: %s\n", gpg_strerror (rc
));
1046 Learn something about the currently inserted smartcard. With
1047 --send the new certificates are send back. */
1049 cmd_learn (assuan_context_t ctx
, char *line
)
1051 ctrl_t ctrl
= assuan_get_pointer (ctx
);
1054 rc
= agent_handle_learn (ctrl
, has_option (line
, "--send")? ctx
: NULL
);
1056 log_error ("command learn failed: %s\n", gpg_strerror (rc
));
1062 /* PASSWD <hexstring_with_keygrip>
1064 Change the passphrase/PID for the key identified by keygrip in LINE. */
1066 cmd_passwd (assuan_context_t ctx
, char *line
)
1068 ctrl_t ctrl
= assuan_get_pointer (ctx
);
1070 unsigned char grip
[20];
1071 gcry_sexp_t s_skey
= NULL
;
1072 unsigned char *shadow_info
= NULL
;
1074 rc
= parse_keygrip (ctx
, line
, grip
);
1079 rc
= agent_key_from_file (ctrl
, ctrl
->server_local
->keydesc
,
1080 grip
, &shadow_info
, CACHE_MODE_IGNORE
, &s_skey
);
1085 log_error ("changing a smartcard PIN is not yet supported\n");
1086 rc
= gpg_error (GPG_ERR_NOT_IMPLEMENTED
);
1089 rc
= agent_protect_and_store (ctrl
, s_skey
);
1092 xfree (ctrl
->server_local
->keydesc
);
1093 ctrl
->server_local
->keydesc
= NULL
;
1096 gcry_sexp_release (s_skey
);
1097 xfree (shadow_info
);
1099 log_error ("command passwd failed: %s\n", gpg_strerror (rc
));
1103 /* PRESET_PASSPHRASE <string_or_keygrip> <timeout> <hexstring>
1105 Set the cached passphrase/PIN for the key identified by the keygrip
1106 to passwd for the given time, where -1 means infinite and 0 means
1107 the default (currently only a timeout of -1 is allowed, which means
1108 to never expire it). If passwd is not provided, ask for it via the
1111 cmd_preset_passphrase (assuan_context_t ctx
, char *line
)
1114 char *grip_clear
= NULL
;
1115 char *passphrase
= NULL
;
1119 if (!opt
.allow_preset_passphrase
)
1120 return set_error (GPG_ERR_NOT_SUPPORTED
, "no --allow-preset-passphrase");
1123 while (*line
&& (*line
!= ' ' && *line
!= '\t'))
1126 return gpg_error (GPG_ERR_MISSING_VALUE
);
1129 while (*line
&& (*line
== ' ' || *line
== '\t'))
1132 /* Currently, only infinite timeouts are allowed. */
1134 if (line
[0] != '-' || line
[1] != '1')
1135 return gpg_error (GPG_ERR_NOT_IMPLEMENTED
);
1138 while (!(*line
!= ' ' && *line
!= '\t'))
1141 /* Syntax check the hexstring. */
1142 rc
= parse_hexstring (ctx
, line
, &len
);
1147 /* If there is a passphrase, use it. Currently, a passphrase is
1151 /* Do in-place conversion. */
1153 if (!hex2str (passphrase
, passphrase
, strlen (passphrase
)+1, NULL
))
1154 rc
= set_error (GPG_ERR_ASS_PARAMETER
, "invalid hexstring");
1157 rc
= set_error (GPG_ERR_NOT_IMPLEMENTED
, "passphrase is required");
1160 rc
= agent_put_cache (grip_clear
, CACHE_MODE_ANY
, passphrase
, ttl
);
1163 log_error ("command preset_passphrase failed: %s\n", gpg_strerror (rc
));
1169 /* SCD <commands to pass to the scdaemon>
1171 This is a general quote command to redirect everything to the
1174 cmd_scd (assuan_context_t ctx
, char *line
)
1176 ctrl_t ctrl
= assuan_get_pointer (ctx
);
1179 rc
= divert_generic_cmd (ctrl
, line
, ctx
);
1188 Return the value for KEY from the special environment as created by
1192 cmd_getval (assuan_context_t ctx
, char *line
)
1197 struct putval_item_s
*vl
;
1199 for (p
=line
; *p
== ' '; p
++)
1202 p
= strchr (key
, ' ');
1206 for (; *p
== ' '; p
++)
1209 return set_error (GPG_ERR_ASS_PARAMETER
, "too many arguments");
1212 return set_error (GPG_ERR_ASS_PARAMETER
, "no key given");
1215 for (vl
=putval_list
; vl
; vl
= vl
->next
)
1216 if ( !strcmp (vl
->d
, key
) )
1219 if (vl
) /* Got an entry. */
1220 rc
= assuan_send_data (ctx
, vl
->d
+vl
->off
, vl
->len
);
1222 return gpg_error (GPG_ERR_NO_DATA
);
1225 log_error ("command getval failed: %s\n", gpg_strerror (rc
));
1230 /* PUTVAL <key> [<percent_escaped_value>]
1232 The gpg-agent maintains a kind of environment which may be used to
1233 store key/value pairs in it, so that they can be retrieved later.
1234 This may be used by helper daemons to daemonize themself on
1235 invocation and register them with gpg-agent. Callers of the
1236 daemon's service may now first try connect to get the information
1237 for that service from gpg-agent through the GETVAL command and then
1238 try to connect to that daemon. Only if that fails they may start
1239 an own instance of the service daemon.
1241 KEY is an an arbitrary symbol with the same syntax rules as keys
1242 for shell environment variables. PERCENT_ESCAPED_VALUE is the
1243 corresponsing value; they should be similar to the values of
1244 envronment variables but gpg-agent does not enforce any
1245 restrictions. If that value is not given any value under that KEY
1246 is removed from this special environment.
1249 cmd_putval (assuan_context_t ctx
, char *line
)
1254 size_t valuelen
= 0;
1256 struct putval_item_s
*vl
, *vlprev
;
1258 for (p
=line
; *p
== ' '; p
++)
1261 p
= strchr (key
, ' ');
1265 for (; *p
== ' '; p
++)
1270 p
= strchr (value
, ' ');
1273 valuelen
= percent_plus_unescape (value
);
1277 return set_error (GPG_ERR_ASS_PARAMETER
, "no key given");
1280 for (vl
=putval_list
,vlprev
=NULL
; vl
; vlprev
=vl
, vl
= vl
->next
)
1281 if ( !strcmp (vl
->d
, key
) )
1284 if (vl
) /* Delete old entry. */
1287 vlprev
->next
= vl
->next
;
1289 putval_list
= vl
->next
;
1293 if (valuelen
) /* Add entry. */
1295 vl
= xtrymalloc (sizeof *vl
+ strlen (key
) + valuelen
);
1297 rc
= gpg_error_from_syserror ();
1301 vl
->off
= strlen (key
) + 1;
1302 strcpy (vl
->d
, key
);
1303 memcpy (vl
->d
+ vl
->off
, value
, valuelen
);
1304 vl
->next
= putval_list
;
1310 log_error ("command putval failed: %s\n", gpg_strerror (rc
));
1319 Set startup TTY and X DISPLAY variables to the values of this
1320 session. This command is useful to pull future pinentries to
1321 another screen. It is only required because there is no way in the
1322 ssh-agent protocol to convey this information. */
1324 cmd_updatestartuptty (assuan_context_t ctx
, char *line
)
1326 ctrl_t ctrl
= assuan_get_pointer (ctx
);
1330 xfree (opt
.startup_display
); opt
.startup_display
= NULL
;
1331 xfree (opt
.startup_ttyname
); opt
.startup_ttyname
= NULL
;
1332 xfree (opt
.startup_ttytype
); opt
.startup_ttytype
= NULL
;
1333 xfree (opt
.startup_lc_ctype
); opt
.startup_lc_ctype
= NULL
;
1334 xfree (opt
.startup_lc_messages
); opt
.startup_lc_messages
= NULL
;
1335 xfree (opt
.startup_xauthority
); opt
.startup_xauthority
= NULL
;
1338 opt
.startup_display
= xtrystrdup (ctrl
->display
);
1340 opt
.startup_ttyname
= xtrystrdup (ctrl
->ttyname
);
1342 opt
.startup_ttytype
= xtrystrdup (ctrl
->ttytype
);
1344 opt
.startup_lc_ctype
= xtrystrdup (ctrl
->lc_ctype
);
1345 if (ctrl
->lc_messages
)
1346 opt
.startup_lc_messages
= xtrystrdup (ctrl
->lc_messages
);
1347 if (ctrl
->xauthority
)
1348 opt
.startup_xauthority
= xtrystrdup (ctrl
->xauthority
);
1349 if (ctrl
->pinentry_user_data
)
1350 opt
.startup_pinentry_user_data
= xtrystrdup (ctrl
->pinentry_user_data
);
1357 #ifdef HAVE_W32_SYSTEM
1360 Under Windows we start the agent on the fly. Thus it also make
1361 sense to allow a client to stop the agent. */
1363 cmd_killagent (assuan_context_t ctx
, char *line
)
1365 ctrl_t ctrl
= assuan_get_pointer (ctx
);
1369 ctrl
->server_local
->stopme
= 1;
1370 return gpg_error (GPG_ERR_EOF
);
1375 As signals are inconvenient under Windows, we provide this command
1376 to allow reloading of the configuration. */
1378 cmd_reloadagent (assuan_context_t ctx
, char *line
)
1383 agent_sighup_action ();
1386 #endif /*HAVE_W32_SYSTEM*/
1392 Multipurpose function to return a variety of information.
1393 Supported values for WHAT are:
1395 version - Return the version of the program.
1396 pid - Return the process id of the server.
1397 socket_name - Return the name of the socket.
1398 ssh_socket_name - Return the name of the ssh socket.
1402 cmd_getinfo (assuan_context_t ctx
, char *line
)
1406 if (!strcmp (line
, "version"))
1408 const char *s
= VERSION
;
1409 rc
= assuan_send_data (ctx
, s
, strlen (s
));
1411 else if (!strcmp (line
, "pid"))
1415 snprintf (numbuf
, sizeof numbuf
, "%lu", (unsigned long)getpid ());
1416 rc
= assuan_send_data (ctx
, numbuf
, strlen (numbuf
));
1418 else if (!strcmp (line
, "socket_name"))
1420 const char *s
= get_agent_socket_name ();
1423 rc
= assuan_send_data (ctx
, s
, strlen (s
));
1425 rc
= gpg_error (GPG_ERR_NO_DATA
);
1427 else if (!strcmp (line
, "ssh_socket_name"))
1429 const char *s
= get_agent_ssh_socket_name ();
1432 rc
= assuan_send_data (ctx
, s
, strlen (s
));
1434 rc
= gpg_error (GPG_ERR_NO_DATA
);
1437 rc
= set_error (GPG_ERR_ASS_PARAMETER
, "unknown value for WHAT");
1444 option_handler (assuan_context_t ctx
, const char *key
, const char *value
)
1446 ctrl_t ctrl
= assuan_get_pointer (ctx
);
1448 if (!strcmp (key
, "display"))
1451 xfree (ctrl
->display
);
1452 ctrl
->display
= xtrystrdup (value
);
1454 return out_of_core ();
1456 else if (!strcmp (key
, "ttyname"))
1461 xfree (ctrl
->ttyname
);
1462 ctrl
->ttyname
= xtrystrdup (value
);
1464 return out_of_core ();
1467 else if (!strcmp (key
, "ttytype"))
1472 xfree (ctrl
->ttytype
);
1473 ctrl
->ttytype
= xtrystrdup (value
);
1475 return out_of_core ();
1478 else if (!strcmp (key
, "lc-ctype"))
1481 xfree (ctrl
->lc_ctype
);
1482 ctrl
->lc_ctype
= xtrystrdup (value
);
1483 if (!ctrl
->lc_ctype
)
1484 return out_of_core ();
1486 else if (!strcmp (key
, "lc-messages"))
1488 if (ctrl
->lc_messages
)
1489 xfree (ctrl
->lc_messages
);
1490 ctrl
->lc_messages
= xtrystrdup (value
);
1491 if (!ctrl
->lc_messages
)
1492 return out_of_core ();
1494 else if (!strcmp (key
, "xauthority"))
1496 if (ctrl
->xauthority
)
1497 xfree (ctrl
->xauthority
);
1498 ctrl
->xauthority
= xtrystrdup (value
);
1499 if (!ctrl
->xauthority
)
1500 return out_of_core ();
1502 else if (!strcmp (key
, "pinentry-user-data"))
1504 if (ctrl
->pinentry_user_data
)
1505 xfree (ctrl
->pinentry_user_data
);
1506 ctrl
->pinentry_user_data
= xtrystrdup (value
);
1507 if (!ctrl
->pinentry_user_data
)
1508 return out_of_core ();
1510 else if (!strcmp (key
, "use-cache-for-signing"))
1511 ctrl
->server_local
->use_cache_for_signing
= *value
? atoi (value
) : 0;
1512 else if (!strcmp (key
, "allow-pinentry-notify"))
1513 ctrl
->server_local
->allow_pinentry_notify
= 1;
1515 return gpg_error (GPG_ERR_UNKNOWN_OPTION
);
1523 /* Called by libassuan after all commands. ERR is the error from the
1524 last assuan operation and not the one returned from the command. */
1526 post_cmd_notify (assuan_context_t ctx
, int err
)
1528 ctrl_t ctrl
= assuan_get_pointer (ctx
);
1532 /* Switch off any I/O monitor controlled logging pausing. */
1533 ctrl
->server_local
->pause_io_logging
= 0;
1537 /* This function is called by libassuan for all I/O. We use it here
1538 to disable logging for the GETEVENTCOUNTER commands. This is so
1539 that the debug output won't get cluttered by this primitive
1542 io_monitor (assuan_context_t ctx
, int direction
,
1543 const char *line
, size_t linelen
)
1545 ctrl_t ctrl
= assuan_get_pointer (ctx
);
1547 /* Note that we only check for the uppercase name. This allows to
1548 see the logging for debugging if using a non-upercase command
1550 if (ctx
&& !direction
1552 && !strncmp (line
, "GETEVENTCOUNTER", 15)
1553 && (linelen
== 15 || spacep (line
+15)))
1555 ctrl
->server_local
->pause_io_logging
= 1;
1558 return ctrl
->server_local
->pause_io_logging
? 1:0;
1562 /* Tell the assuan library about our commands */
1564 register_commands (assuan_context_t ctx
)
1568 int (*handler
)(assuan_context_t
, char *line
);
1570 { "GETEVENTCOUNTER",cmd_geteventcounter
},
1571 { "ISTRUSTED", cmd_istrusted
},
1572 { "HAVEKEY", cmd_havekey
},
1573 { "SIGKEY", cmd_sigkey
},
1574 { "SETKEY", cmd_sigkey
},
1575 { "SETKEYDESC", cmd_setkeydesc
},
1576 { "SETHASH", cmd_sethash
},
1577 { "PKSIGN", cmd_pksign
},
1578 { "PKDECRYPT", cmd_pkdecrypt
},
1579 { "GENKEY", cmd_genkey
},
1580 { "READKEY", cmd_readkey
},
1581 { "GET_PASSPHRASE", cmd_get_passphrase
},
1582 { "PRESET_PASSPHRASE", cmd_preset_passphrase
},
1583 { "CLEAR_PASSPHRASE", cmd_clear_passphrase
},
1584 { "GET_CONFIRMATION", cmd_get_confirmation
},
1585 { "LISTTRUSTED", cmd_listtrusted
},
1586 { "MARKTRUSTED", cmd_marktrusted
},
1587 { "LEARN", cmd_learn
},
1588 { "PASSWD", cmd_passwd
},
1592 { "GETVAL", cmd_getval
},
1593 { "PUTVAL", cmd_putval
},
1594 { "UPDATESTARTUPTTY", cmd_updatestartuptty
},
1595 #ifdef HAVE_W32_SYSTEM
1596 { "KILLAGENT", cmd_killagent
},
1597 { "RELOADAGENT", cmd_reloadagent
},
1599 { "GETINFO", cmd_getinfo
},
1604 for (i
=0; table
[i
].name
; i
++)
1606 rc
= assuan_register_command (ctx
, table
[i
].name
, table
[i
].handler
);
1610 #ifdef HAVE_ASSUAN_SET_IO_MONITOR
1611 assuan_register_post_cmd_notify (ctx
, post_cmd_notify
);
1613 assuan_register_reset_notify (ctx
, reset_notify
);
1614 assuan_register_option_handler (ctx
, option_handler
);
1619 /* Startup the server. If LISTEN_FD and FD is given as -1, this is a
1620 simple piper server, otherwise it is a regular server. CTRL is the
1621 control structure for this connection; it has only the basic
1624 start_command_handler (ctrl_t ctrl
, gnupg_fd_t listen_fd
, gnupg_fd_t fd
)
1627 assuan_context_t ctx
;
1629 if (listen_fd
== GNUPG_INVALID_FD
&& fd
== GNUPG_INVALID_FD
)
1635 rc
= assuan_init_pipe_server (&ctx
, filedes
);
1637 else if (listen_fd
!= GNUPG_INVALID_FD
)
1639 rc
= assuan_init_socket_server_ext (&ctx
, listen_fd
, 0);
1643 rc
= assuan_init_socket_server_ext (&ctx
, fd
, 2);
1647 log_error ("failed to initialize the server: %s\n",
1651 rc
= register_commands (ctx
);
1654 log_error ("failed to register commands with Assuan: %s\n",
1659 assuan_set_pointer (ctx
, ctrl
);
1660 ctrl
->server_local
= xcalloc (1, sizeof *ctrl
->server_local
);
1661 ctrl
->server_local
->assuan_ctx
= ctx
;
1662 ctrl
->server_local
->message_fd
= -1;
1663 ctrl
->server_local
->use_cache_for_signing
= 1;
1664 ctrl
->digest
.raw_value
= 0;
1667 assuan_set_log_stream (ctx
, log_get_stream ());
1669 #ifdef HAVE_ASSUAN_SET_IO_MONITOR
1670 assuan_set_io_monitor (ctx
, io_monitor
);
1675 rc
= assuan_accept (ctx
);
1676 if (gpg_err_code (rc
) == GPG_ERR_EOF
|| rc
== -1)
1682 log_info ("Assuan accept problem: %s\n", gpg_strerror (rc
));
1686 rc
= assuan_process (ctx
);
1689 log_info ("Assuan processing failed: %s\n", gpg_strerror (rc
));
1694 /* Reset the SCD if needed. */
1695 agent_reset_scd (ctrl
);
1697 /* Reset the pinentry (in case of popup messages). */
1698 agent_reset_query (ctrl
);
1701 assuan_deinit_server (ctx
);
1702 #ifdef HAVE_W32_SYSTEM
1703 if (ctrl
->server_local
->stopme
)
1706 xfree (ctrl
->server_local
);
1707 ctrl
->server_local
= NULL
;