1 /* command.c - gpg-agent command handler
2 * Copyright (C) 2001, 2002, 2003, 2004, 2005,
3 * 2006 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. */
66 /* An entry for the getval/putval commands. */
69 struct putval_item_s
*next
;
70 size_t off
; /* Offset to the value into DATA. */
71 size_t len
; /* Length of the value. */
72 char d
[1]; /* Key | Nul | value. */
76 /* A list of key value pairs fpr the getval/putval commands. */
77 static struct putval_item_s
*putval_list
;
81 /* To help polling clients, we keep tarck of the number of certain
82 events. This structure keeps those counters. The counters are
83 integers and there should be no problem if they are overflowing as
84 callers need to check only whether a counter changed. The actual
85 values are not meaningful. */
88 /* Incremented if any of the other counters below changed. */
91 /* Incremented if a key is added or removed from the internal privat
95 /* Incremented if a change of the card readers stati has been
105 /* Release the memory buffer MB but first wipe out the used memory. */
107 clear_outbuf (membuf_t
*mb
)
112 p
= get_membuf (mb
, &n
);
121 /* Write the content of memory buffer MB as assuan data to CTX and
122 wipe the buffer out afterwards. */
124 write_and_clear_outbuf (assuan_context_t ctx
, membuf_t
*mb
)
130 p
= get_membuf (mb
, &n
);
132 return out_of_core ();
133 ae
= assuan_send_data (ctx
, p
, n
);
141 reset_notify (assuan_context_t ctx
)
143 ctrl_t ctrl
= assuan_get_pointer (ctx
);
145 memset (ctrl
->keygrip
, 0, 20);
146 ctrl
->have_keygrip
= 0;
147 ctrl
->digest
.valuelen
= 0;
149 xfree (ctrl
->server_local
->keydesc
);
150 ctrl
->server_local
->keydesc
= NULL
;
154 /* Check whether the option NAME appears in LINE */
156 has_option (const char *line
, const char *name
)
159 int n
= strlen (name
);
161 s
= strstr (line
, name
);
162 return (s
&& (s
== line
|| spacep (s
-1)) && (!s
[n
] || spacep (s
+n
)));
165 /* Same as has_option but does only test for the name of the option
166 and ignores an argument, i.e. with NAME being "--hash" it would
167 return true for "--hash" as well as for "--hash=foo". */
169 has_option_name (const char *line
, const char *name
)
172 int n
= strlen (name
);
174 s
= strstr (line
, name
);
175 return (s
&& (s
== line
|| spacep (s
-1))
176 && (!s
[n
] || spacep (s
+n
) || s
[n
] == '='));
180 /* Skip over options. It is assumed that leading spaces have been
181 removed (this is the case for lines passed to a handler from
182 assuan). Blanks after the options are also removed. */
184 skip_options (char *line
)
186 while ( *line
== '-' && line
[1] == '-' )
188 while (*line
&& !spacep (line
))
190 while (spacep (line
))
197 /* Replace all '+' by a blank. */
199 plus_to_blank (char *s
)
209 /* Do the percent and plus/space unescaping in place and return the
210 length of the valid buffer. */
212 percent_plus_unescape (char *string
)
214 unsigned char *p
= (unsigned char *)string
;
219 if (*string
== '%' && string
[1] && string
[2])
222 *p
++ = xtoi_2 (string
);
226 else if (*string
== '+')
245 /* Parse a hex string. Return an Assuan error code or 0 on success and the
246 length of the parsed string in LEN. */
248 parse_hexstring (assuan_context_t ctx
, const char *string
, size_t *len
)
253 /* parse the hash value */
254 for (p
=string
, n
=0; hexdigitp (p
); p
++, n
++)
256 if (*p
!= ' ' && *p
!= '\t' && *p
)
257 return set_error (GPG_ERR_ASS_PARAMETER
, "invalid hexstring");
259 return set_error (GPG_ERR_ASS_PARAMETER
, "odd number of digits");
264 /* Parse the keygrip in STRING into the provided buffer BUF. BUF must
265 provide space for 20 bytes. BUF is not changed if the function
268 parse_keygrip (assuan_context_t ctx
, const char *string
, unsigned char *buf
)
272 const unsigned char *p
;
274 rc
= parse_hexstring (ctx
, string
, &n
);
279 return set_error (GPG_ERR_ASS_PARAMETER
, "invalid length of keygrip");
281 for (p
=(const unsigned char*)string
, n
=0; n
< 20; p
+= 2, n
++)
288 /* Write an assuan status line. */
290 agent_write_status (ctrl_t ctrl
, const char *keyword
, ...)
295 assuan_context_t ctx
= ctrl
->server_local
->assuan_ctx
;
299 va_start (arg_ptr
, keyword
);
303 while ( (text
= va_arg (arg_ptr
, const char *)) )
310 for ( ; *text
&& n
< DIM (buf
)-2; n
++)
314 err
= assuan_write_status (ctx
, keyword
, buf
);
324 Return a a status line named EVENTCOUNTER with the current values
325 of all event counters. The values are decimal numbers in the range
326 0 to UINT_MAX and wrapping around to 0. The actual values should
327 not be relied upon, they shall only be used to detect a change.
329 The currently defined counters are:
331 ANY - Incremented with any change of any of the other counters.
332 KEY - Incremented for added or removed private keys.
333 CARD - Incremented for changes of the card readers stati.
336 cmd_geteventcounter (assuan_context_t ctx
, char *line
)
338 ctrl_t ctrl
= assuan_get_pointer (ctx
);
339 char any_counter
[25];
340 char key_counter
[25];
341 char card_counter
[25];
343 snprintf (any_counter
, sizeof any_counter
, "%u", eventcounter
.any
);
344 snprintf (key_counter
, sizeof key_counter
, "%u", eventcounter
.key
);
345 snprintf (card_counter
, sizeof card_counter
, "%u", eventcounter
.card
);
347 return agent_write_status (ctrl
, "EVENTCOUNTER",
355 /* This function should be called once for all key removals or
356 additions. This function is assured not to do any context
359 bump_key_eventcounter (void)
365 /* This function should be called for all card reader status
366 changes. This function is assured not to do any context
369 bump_card_eventcounter (void)
378 /* ISTRUSTED <hexstring_with_fingerprint>
380 Return OK when we have an entry with this fingerprint in our
383 cmd_istrusted (assuan_context_t ctx
, char *line
)
385 ctrl_t ctrl
= assuan_get_pointer (ctx
);
390 /* Parse the fingerprint value. */
391 for (p
=line
,n
=0; hexdigitp (p
); p
++, n
++)
393 if (*p
|| !(n
== 40 || n
== 32))
394 return set_error (GPG_ERR_ASS_PARAMETER
, "invalid fingerprint");
398 strcpy (fpr
, "00000000");
401 for (p
=line
; i
< 40; p
++, i
++)
402 fpr
[i
] = *p
>= 'a'? (*p
& 0xdf): *p
;
404 rc
= agent_istrusted (ctrl
, fpr
);
405 if (!rc
|| gpg_err_code (rc
) == GPG_ERR_NOT_TRUSTED
)
407 else if (rc
== -1 || gpg_err_code (rc
) == GPG_ERR_EOF
)
408 return gpg_error (GPG_ERR_NOT_TRUSTED
);
411 log_error ("command is_trusted failed: %s\n", gpg_strerror (rc
));
418 List all entries from the trustlist */
420 cmd_listtrusted (assuan_context_t ctx
, char *line
)
422 int rc
= agent_listtrusted (ctx
);
424 log_error ("command listtrusted failed: %s\n", gpg_strerror (rc
));
429 /* MARKTRUSTED <hexstring_with_fingerprint> <flag> <display_name>
431 Store a new key in into the trustlist*/
433 cmd_marktrusted (assuan_context_t ctx
, char *line
)
435 ctrl_t ctrl
= assuan_get_pointer (ctx
);
441 /* parse the fingerprint value */
442 for (p
=line
,n
=0; hexdigitp (p
); p
++, n
++)
444 if (!spacep (p
) || !(n
== 40 || n
== 32))
445 return set_error (GPG_ERR_ASS_PARAMETER
, "invalid fingerprint");
449 strcpy (fpr
, "00000000");
452 for (p
=line
; i
< 40; p
++, i
++)
453 fpr
[i
] = *p
>= 'a'? (*p
& 0xdf): *p
;
459 if ( (flag
!= 'S' && flag
!= 'P') || !spacep (p
) )
460 return set_error (GPG_ERR_ASS_PARAMETER
, "invalid flag - must be P or S");
464 rc
= agent_marktrusted (ctrl
, p
, fpr
, flag
);
466 log_error ("command marktrusted failed: %s\n", gpg_strerror (rc
));
473 /* HAVEKEY <hexstring_with_keygrip>
475 Return success when the secret key is available */
477 cmd_havekey (assuan_context_t ctx
, char *line
)
480 unsigned char buf
[20];
482 rc
= parse_keygrip (ctx
, line
, buf
);
486 if (agent_key_available (buf
))
487 return gpg_error (GPG_ERR_NO_SECKEY
);
493 /* SIGKEY <hexstring_with_keygrip>
494 SETKEY <hexstring_with_keygrip>
496 Set the key used for a sign or decrypt operation */
498 cmd_sigkey (assuan_context_t ctx
, char *line
)
501 ctrl_t ctrl
= assuan_get_pointer (ctx
);
503 rc
= parse_keygrip (ctx
, line
, ctrl
->keygrip
);
506 ctrl
->have_keygrip
= 1;
511 /* SETKEYDESC plus_percent_escaped_string
513 Set a description to be used for the next PKSIGN or PKDECRYPT
514 operation if this operation requires the entry of a passphrase. If
515 this command is not used a default text will be used. Note, that
516 this description implictly selects the label used for the entry
517 box; if the string contains the string PIN (which in general will
518 not be translated), "PIN" is used, otherwise the translation of
519 "passphrase" is used. The description string should not contain
520 blanks unless they are percent or '+' escaped.
522 The description is only valid for the next PKSIGN or PKDECRYPT
526 cmd_setkeydesc (assuan_context_t ctx
, char *line
)
528 ctrl_t ctrl
= assuan_get_pointer (ctx
);
531 for (p
=line
; *p
== ' '; p
++)
534 p
= strchr (desc
, ' ');
536 *p
= 0; /* We ignore any garbage; we might late use it for other args. */
539 return set_error (GPG_ERR_ASS_PARAMETER
, "no description given");
541 /* Note, that we only need to replace the + characters and should
542 leave the other escaping in place because the escaped string is
543 send verbatim to the pinentry which does the unescaping (but not
545 plus_to_blank (desc
);
547 xfree (ctrl
->server_local
->keydesc
);
548 ctrl
->server_local
->keydesc
= xtrystrdup (desc
);
549 if (!ctrl
->server_local
->keydesc
)
550 return out_of_core ();
555 /* SETHASH --hash=<name>|<algonumber> <hexstring>
557 The client can use this command to tell the server about the data
558 (which usually is a hash) to be signed. */
560 cmd_sethash (assuan_context_t ctx
, char *line
)
565 ctrl_t ctrl
= assuan_get_pointer (ctx
);
570 /* Parse the alternative hash options which may be used instead of
572 if (has_option_name (line
, "--hash"))
574 if (has_option (line
, "--hash=sha1"))
576 else if (has_option (line
, "--hash=sha256"))
577 algo
= GCRY_MD_SHA256
;
578 else if (has_option (line
, "--hash=rmd160"))
579 algo
= GCRY_MD_RMD160
;
580 else if (has_option (line
, "--hash=md5"))
582 else if (has_option (line
, "--hash=tls-md5sha1"))
583 algo
= GCRY_MD_USER_TLS_MD5SHA1
;
585 return set_error (GPG_ERR_ASS_PARAMETER
, "invalid hash algorithm");
590 line
= skip_options (line
);
594 /* No hash option has been given: require an algo number instead */
595 algo
= (int)strtoul (line
, &endp
, 10);
596 for (line
= endp
; *line
== ' ' || *line
== '\t'; line
++)
598 if (!algo
|| gcry_md_test_algo (algo
))
599 return set_error (GPG_ERR_UNSUPPORTED_ALGORITHM
, NULL
);
601 ctrl
->digest
.algo
= algo
;
603 /* Parse the hash value. */
604 rc
= parse_hexstring (ctx
, line
, &n
);
608 if (algo
== GCRY_MD_USER_TLS_MD5SHA1
&& n
== 36)
610 else if (n
!= 16 && n
!= 20 && n
!= 24 && n
!= 32)
611 return set_error (GPG_ERR_ASS_PARAMETER
, "unsupported length of hash");
613 if (n
> MAX_DIGEST_LEN
)
614 return set_error (GPG_ERR_ASS_PARAMETER
, "hash value to long");
616 buf
= ctrl
->digest
.value
;
617 ctrl
->digest
.valuelen
= n
;
618 for (p
=line
, n
=0; n
< ctrl
->digest
.valuelen
; p
+= 2, n
++)
620 for (; n
< ctrl
->digest
.valuelen
; n
++)
628 Perform the actual sign operation. Neither input nor output are
629 sensitive to eavesdropping. */
631 cmd_pksign (assuan_context_t ctx
, char *line
)
634 cache_mode_t cache_mode
= CACHE_MODE_NORMAL
;
635 ctrl_t ctrl
= assuan_get_pointer (ctx
);
638 if (opt
.ignore_cache_for_signing
)
639 cache_mode
= CACHE_MODE_IGNORE
;
640 else if (!ctrl
->server_local
->use_cache_for_signing
)
641 cache_mode
= CACHE_MODE_IGNORE
;
643 init_membuf (&outbuf
, 512);
645 rc
= agent_pksign (ctrl
, ctrl
->server_local
->keydesc
,
646 &outbuf
, cache_mode
);
648 clear_outbuf (&outbuf
);
650 rc
= write_and_clear_outbuf (ctx
, &outbuf
);
652 log_error ("command pksign failed: %s\n", gpg_strerror (rc
));
653 xfree (ctrl
->server_local
->keydesc
);
654 ctrl
->server_local
->keydesc
= NULL
;
658 /* PKDECRYPT <options>
660 Perform the actual decrypt operation. Input is not
661 sensitive to eavesdropping */
663 cmd_pkdecrypt (assuan_context_t ctx
, char *line
)
666 ctrl_t ctrl
= assuan_get_pointer (ctx
);
667 unsigned char *value
;
671 /* First inquire the data to decrypt */
672 rc
= assuan_inquire (ctx
, "CIPHERTEXT",
673 &value
, &valuelen
, MAXLEN_CIPHERTEXT
);
677 init_membuf (&outbuf
, 512);
679 rc
= agent_pkdecrypt (ctrl
, ctrl
->server_local
->keydesc
,
680 value
, valuelen
, &outbuf
);
683 clear_outbuf (&outbuf
);
685 rc
= write_and_clear_outbuf (ctx
, &outbuf
);
687 log_error ("command pkdecrypt failed: %s\n", gpg_strerror (rc
));
688 xfree (ctrl
->server_local
->keydesc
);
689 ctrl
->server_local
->keydesc
= NULL
;
696 Generate a new key, store the secret part and return the public
697 part. Here is an example transaction:
701 C: D (genkey (rsa (nbits 1024)))
704 S: D (rsa (n 326487324683264) (e 10001)))
709 cmd_genkey (assuan_context_t ctx
, char *line
)
711 ctrl_t ctrl
= assuan_get_pointer (ctx
);
713 unsigned char *value
;
717 /* First inquire the parameters */
718 rc
= assuan_inquire (ctx
, "KEYPARAM", &value
, &valuelen
, MAXLEN_KEYPARAM
);
722 init_membuf (&outbuf
, 512);
724 rc
= agent_genkey (ctrl
, (char*)value
, valuelen
, &outbuf
);
727 clear_outbuf (&outbuf
);
729 rc
= write_and_clear_outbuf (ctx
, &outbuf
);
731 log_error ("command genkey failed: %s\n", gpg_strerror (rc
));
738 /* READKEY <hexstring_with_keygrip>
740 Return the public key for the given keygrip. */
742 cmd_readkey (assuan_context_t ctx
, char *line
)
744 ctrl_t ctrl
= assuan_get_pointer (ctx
);
746 unsigned char grip
[20];
747 gcry_sexp_t s_pkey
= NULL
;
749 rc
= parse_keygrip (ctx
, line
, grip
);
751 return rc
; /* Return immediately as this is already an Assuan error code.*/
753 rc
= agent_public_key_from_file (ctrl
, grip
, &s_pkey
);
759 len
= gcry_sexp_sprint (s_pkey
, GCRYSEXP_FMT_CANON
, NULL
, 0);
761 buf
= xtrymalloc (len
);
763 rc
= gpg_error_from_syserror ();
766 len
= gcry_sexp_sprint (s_pkey
, GCRYSEXP_FMT_CANON
, buf
, len
);
768 rc
= assuan_send_data (ctx
, buf
, len
);
771 gcry_sexp_release (s_pkey
);
775 log_error ("command readkey failed: %s\n", gpg_strerror (rc
));
785 send_back_passphrase (assuan_context_t ctx
, int via_data
, const char *pw
)
790 assuan_begin_confidential (ctx
);
793 rc
= assuan_send_data (ctx
, pw
, n
);
796 char *p
= xtrymalloc_secure (n
*2+1);
798 rc
= gpg_error_from_syserror ();
802 rc
= assuan_set_okay_line (ctx
, p
);
810 /* GET_PASSPHRASE [--data] [--check] <cache_id>
811 [<error_message> <prompt> <description>]
813 This function is usually used to ask for a passphrase to be used
814 for conventional encryption, but may also be used by programs which
815 need specal handling of passphrases. This command uses a syntax
816 which helps clients to use the agent with minimum effort. The
817 agent either returns with an error or with a OK followed by the hex
818 encoded passphrase. Note that the length of the strings is
819 implicitly limited by the maximum length of a command.
821 If the option "--data" is used the passphrase is returned by usual
822 data lines and not on the okay line.
824 If the option "--check" is used the passphrase constraints checks as
825 implemented by gpg-agent are applied. A check is not done if the
826 passphrase has been found in the cache.
830 cmd_get_passphrase (assuan_context_t ctx
, char *line
)
832 ctrl_t ctrl
= assuan_get_pointer (ctx
);
836 char *cacheid
= NULL
, *desc
= NULL
, *prompt
= NULL
, *errtext
= NULL
;
839 int opt_data
, opt_check
;
841 opt_data
= has_option (line
, "--data");
842 opt_check
= has_option (line
, "--check");
843 line
= skip_options (line
);
846 p
= strchr (cacheid
, ' ');
853 p
= strchr (errtext
, ' ');
860 p
= strchr (prompt
, ' ');
867 p
= strchr (desc
, ' ');
869 *p
= 0; /* Ignore trailing garbage. */
873 if (!cacheid
|| !*cacheid
|| strlen (cacheid
) > 50)
874 return set_error (GPG_ERR_ASS_PARAMETER
, "invalid length of cacheID");
876 return set_error (GPG_ERR_ASS_PARAMETER
, "no description given");
878 if (!strcmp (cacheid
, "X"))
880 if (!strcmp (errtext
, "X"))
882 if (!strcmp (prompt
, "X"))
884 if (!strcmp (desc
, "X"))
887 pw
= cacheid
? agent_get_cache (cacheid
, CACHE_MODE_NORMAL
, &cache_marker
)
891 rc
= send_back_passphrase (ctx
, opt_data
, pw
);
892 agent_unlock_cache_entry (&cache_marker
);
896 /* Note, that we only need to replace the + characters and
897 should leave the other escaping in place because the escaped
898 string is send verbatim to the pinentry which does the
899 unescaping (but not the + replacing) */
901 plus_to_blank (errtext
);
903 plus_to_blank (prompt
);
905 plus_to_blank (desc
);
911 rc
= agent_get_passphrase (ctrl
, &response
, desc
, prompt
, errtext
);
915 && check_passphrase_constraints (ctrl
, response
, 0));
920 agent_put_cache (cacheid
, CACHE_MODE_USER
, response
, 0);
921 rc
= send_back_passphrase (ctx
, opt_data
, response
);
927 log_error ("command get_passphrase failed: %s\n", gpg_strerror (rc
));
932 /* CLEAR_PASSPHRASE <cache_id>
934 may be used to invalidate the cache entry for a passphrase. The
935 function returns with OK even when there is no cached passphrase.
939 cmd_clear_passphrase (assuan_context_t ctx
, char *line
)
941 char *cacheid
= NULL
;
944 /* parse the stuff */
945 for (p
=line
; *p
== ' '; p
++)
948 p
= strchr (cacheid
, ' ');
950 *p
= 0; /* ignore garbage */
951 if (!cacheid
|| !*cacheid
|| strlen (cacheid
) > 50)
952 return set_error (GPG_ERR_ASS_PARAMETER
, "invalid length of cacheID");
954 agent_put_cache (cacheid
, CACHE_MODE_USER
, NULL
, 0);
959 /* GET_CONFIRMATION <description>
961 This command may be used to ask for a simple confirmation.
962 DESCRIPTION is displayed along with a Okay and Cancel button. This
963 command uses a syntax which helps clients to use the agent with
964 minimum effort. The agent either returns with an error or with a
965 OK. Note, that the length of DESCRIPTION is implicitly limited by
966 the maximum length of a command. DESCRIPTION should not contain
967 any spaces, those must be encoded either percent escaped or simply
972 cmd_get_confirmation (assuan_context_t ctx
, char *line
)
974 ctrl_t ctrl
= assuan_get_pointer (ctx
);
979 /* parse the stuff */
980 for (p
=line
; *p
== ' '; p
++)
983 p
= strchr (desc
, ' ');
985 *p
= 0; /* We ignore any garbage -may be later used for other args. */
988 return set_error (GPG_ERR_ASS_PARAMETER
, "no description given");
990 if (!strcmp (desc
, "X"))
993 /* Note, that we only need to replace the + characters and should
994 leave the other escaping in place because the escaped string is
995 send verbatim to the pinentry which does the unescaping (but not
998 plus_to_blank (desc
);
1000 rc
= agent_get_confirmation (ctrl
, desc
, NULL
, NULL
);
1002 log_error ("command get_confirmation failed: %s\n", gpg_strerror (rc
));
1010 Learn something about the currently inserted smartcard. With
1011 --send the new certificates are send back. */
1013 cmd_learn (assuan_context_t ctx
, char *line
)
1015 ctrl_t ctrl
= assuan_get_pointer (ctx
);
1018 rc
= agent_handle_learn (ctrl
, has_option (line
, "--send")? ctx
: NULL
);
1020 log_error ("command learn failed: %s\n", gpg_strerror (rc
));
1026 /* PASSWD <hexstring_with_keygrip>
1028 Change the passphrase/PID for the key identified by keygrip in LINE. */
1030 cmd_passwd (assuan_context_t ctx
, char *line
)
1032 ctrl_t ctrl
= assuan_get_pointer (ctx
);
1034 unsigned char grip
[20];
1035 gcry_sexp_t s_skey
= NULL
;
1036 unsigned char *shadow_info
= NULL
;
1038 rc
= parse_keygrip (ctx
, line
, grip
);
1043 rc
= agent_key_from_file (ctrl
, ctrl
->server_local
->keydesc
,
1044 grip
, &shadow_info
, CACHE_MODE_IGNORE
, &s_skey
);
1049 log_error ("changing a smartcard PIN is not yet supported\n");
1050 rc
= gpg_error (GPG_ERR_NOT_IMPLEMENTED
);
1053 rc
= agent_protect_and_store (ctrl
, s_skey
);
1056 xfree (ctrl
->server_local
->keydesc
);
1057 ctrl
->server_local
->keydesc
= NULL
;
1060 gcry_sexp_release (s_skey
);
1061 xfree (shadow_info
);
1063 log_error ("command passwd failed: %s\n", gpg_strerror (rc
));
1067 /* PRESET_PASSPHRASE <hexstring_with_keygrip> <timeout> <hexstring>
1069 Set the cached passphrase/PIN for the key identified by the keygrip
1070 to passwd for the given time, where -1 means infinite and 0 means
1071 the default (currently only a timeout of -1 is allowed, which means
1072 to never expire it). If passwd is not provided, ask for it via the
1075 cmd_preset_passphrase (assuan_context_t ctx
, char *line
)
1078 unsigned char grip
[20];
1079 char *grip_clear
= NULL
;
1080 char *passphrase
= NULL
;
1084 if (!opt
.allow_preset_passphrase
)
1085 return gpg_error (GPG_ERR_NOT_SUPPORTED
);
1087 rc
= parse_keygrip (ctx
, line
, grip
);
1091 /* FIXME: parse_keygrip should return a tail pointer. */
1093 while (*line
&& (*line
!= ' ' && *line
!= '\t'))
1096 return gpg_error (GPG_ERR_MISSING_VALUE
);
1099 while (*line
&& (*line
== ' ' || *line
== '\t'))
1102 /* Currently, only infinite timeouts are allowed. */
1104 if (line
[0] != '-' || line
[1] != '1')
1105 return gpg_error (GPG_ERR_NOT_IMPLEMENTED
);
1108 while (!(*line
!= ' ' && *line
!= '\t'))
1111 /* Syntax check the hexstring. */
1112 rc
= parse_hexstring (ctx
, line
, &len
);
1117 /* If there is a passphrase, use it. Currently, a passphrase is
1122 return gpg_error (GPG_ERR_NOT_IMPLEMENTED
);
1124 rc
= agent_put_cache (grip_clear
, CACHE_MODE_ANY
, passphrase
, ttl
);
1127 log_error ("command preset_passwd failed: %s\n", gpg_strerror (rc
));
1133 /* SCD <commands to pass to the scdaemon>
1135 This is a general quote command to redirect everything to the
1138 cmd_scd (assuan_context_t ctx
, char *line
)
1140 ctrl_t ctrl
= assuan_get_pointer (ctx
);
1143 rc
= divert_generic_cmd (ctrl
, line
, ctx
);
1152 Return the value for KEY from the special environment as created by
1156 cmd_getval (assuan_context_t ctx
, char *line
)
1161 struct putval_item_s
*vl
;
1163 for (p
=line
; *p
== ' '; p
++)
1166 p
= strchr (key
, ' ');
1170 for (; *p
== ' '; p
++)
1173 return set_error (GPG_ERR_ASS_PARAMETER
, "too many arguments");
1176 return set_error (GPG_ERR_ASS_PARAMETER
, "no key given");
1179 for (vl
=putval_list
; vl
; vl
= vl
->next
)
1180 if ( !strcmp (vl
->d
, key
) )
1183 if (vl
) /* Got an entry. */
1184 rc
= assuan_send_data (ctx
, vl
->d
+vl
->off
, vl
->len
);
1186 return gpg_error (GPG_ERR_NO_DATA
);
1189 log_error ("command getval failed: %s\n", gpg_strerror (rc
));
1194 /* PUTVAL <key> [<percent_escaped_value>]
1196 The gpg-agent maintains a kind of environment which may be used to
1197 store key/value pairs in it, so that they can be retrieved later.
1198 This may be used by helper daemons to daemonize themself on
1199 invocation and register them with gpg-agent. Callers of the
1200 daemon's service may now first try connect to get the information
1201 for that service from gpg-agent through the GETVAL command and then
1202 try to connect to that daemon. Only if that fails they may start
1203 an own instance of the service daemon.
1205 KEY is an an arbitrary symbol with the same syntax rules as keys
1206 for shell environment variables. PERCENT_ESCAPED_VALUE is the
1207 corresponsing value; they should be similar to the values of
1208 envronment variables but gpg-agent does not enforce any
1209 restrictions. If that value is not given any value under that KEY
1210 is removed from this special environment.
1213 cmd_putval (assuan_context_t ctx
, char *line
)
1218 size_t valuelen
= 0;
1220 struct putval_item_s
*vl
, *vlprev
;
1222 for (p
=line
; *p
== ' '; p
++)
1225 p
= strchr (key
, ' ');
1229 for (; *p
== ' '; p
++)
1234 p
= strchr (value
, ' ');
1237 valuelen
= percent_plus_unescape (value
);
1241 return set_error (GPG_ERR_ASS_PARAMETER
, "no key given");
1244 for (vl
=putval_list
,vlprev
=NULL
; vl
; vlprev
=vl
, vl
= vl
->next
)
1245 if ( !strcmp (vl
->d
, key
) )
1248 if (vl
) /* Delete old entry. */
1251 vlprev
->next
= vl
->next
;
1253 putval_list
= vl
->next
;
1257 if (valuelen
) /* Add entry. */
1259 vl
= xtrymalloc (sizeof *vl
+ strlen (key
) + valuelen
);
1261 rc
= gpg_error_from_syserror ();
1265 vl
->off
= strlen (key
) + 1;
1266 strcpy (vl
->d
, key
);
1267 memcpy (vl
->d
+ vl
->off
, value
, valuelen
);
1268 vl
->next
= putval_list
;
1274 log_error ("command putval failed: %s\n", gpg_strerror (rc
));
1283 Set startup TTY and X DISPLAY variables to the values of this
1284 session. This command is useful to pull future pinentries to
1285 another screen. It is only required because there is no way in the
1286 ssh-agent protocol to convey this information. */
1288 cmd_updatestartuptty (assuan_context_t ctx
, char *line
)
1290 ctrl_t ctrl
= assuan_get_pointer (ctx
);
1292 xfree (opt
.startup_display
); opt
.startup_display
= NULL
;
1293 xfree (opt
.startup_ttyname
); opt
.startup_ttyname
= NULL
;
1294 xfree (opt
.startup_ttytype
); opt
.startup_ttytype
= NULL
;
1295 xfree (opt
.startup_lc_ctype
); opt
.startup_lc_ctype
= NULL
;
1296 xfree (opt
.startup_lc_messages
); opt
.startup_lc_messages
= NULL
;
1297 xfree (opt
.startup_xauthority
); opt
.startup_xauthority
= NULL
;
1300 opt
.startup_display
= xtrystrdup (ctrl
->display
);
1302 opt
.startup_ttyname
= xtrystrdup (ctrl
->ttyname
);
1304 opt
.startup_ttytype
= xtrystrdup (ctrl
->ttytype
);
1306 opt
.startup_lc_ctype
= xtrystrdup (ctrl
->lc_ctype
);
1307 if (ctrl
->lc_messages
)
1308 opt
.startup_lc_messages
= xtrystrdup (ctrl
->lc_messages
);
1309 if (ctrl
->xauthority
)
1310 opt
.startup_xauthority
= xtrystrdup (ctrl
->xauthority
);
1311 if (ctrl
->pinentry_user_data
)
1312 opt
.startup_pinentry_user_data
= xtrystrdup (ctrl
->pinentry_user_data
);
1319 #ifdef HAVE_W32_SYSTEM
1322 Under Windows we start the agent on the fly. Thus it also make
1323 sense to allow a client to stop the agent. */
1325 cmd_killagent (assuan_context_t ctx
, char *line
)
1327 ctrl_t ctrl
= assuan_get_pointer (ctx
);
1328 ctrl
->server_local
->stopme
= 1;
1334 As signals are inconvenient under Windows, we provide this command
1335 to allow reloading of the configuration. */
1337 cmd_reloadagent (assuan_context_t ctx
, char *line
)
1339 agent_sighup_action ();
1342 #endif /*HAVE_W32_SYSTEM*/
1348 Multipurpose function to return a variety of information.
1349 Supported values for WHAT are:
1351 version - Return the version of the program.
1352 pid - Return the process id of the server.
1353 socket_name - Return the name of the socket.
1354 ssh_socket_name - Return the name of the ssh socket.
1358 cmd_getinfo (assuan_context_t ctx
, char *line
)
1362 if (!strcmp (line
, "version"))
1364 const char *s
= VERSION
;
1365 rc
= assuan_send_data (ctx
, s
, strlen (s
));
1367 else if (!strcmp (line
, "pid"))
1371 snprintf (numbuf
, sizeof numbuf
, "%lu", (unsigned long)getpid ());
1372 rc
= assuan_send_data (ctx
, numbuf
, strlen (numbuf
));
1374 else if (!strcmp (line
, "socket_name"))
1376 const char *s
= get_agent_socket_name ();
1379 rc
= assuan_send_data (ctx
, s
, strlen (s
));
1381 rc
= gpg_error (GPG_ERR_NO_DATA
);
1383 else if (!strcmp (line
, "ssh_socket_name"))
1385 const char *s
= get_agent_ssh_socket_name ();
1388 rc
= assuan_send_data (ctx
, s
, strlen (s
));
1390 rc
= gpg_error (GPG_ERR_NO_DATA
);
1393 rc
= set_error (GPG_ERR_ASS_PARAMETER
, "unknown value for WHAT");
1400 option_handler (assuan_context_t ctx
, const char *key
, const char *value
)
1402 ctrl_t ctrl
= assuan_get_pointer (ctx
);
1404 if (!strcmp (key
, "display"))
1407 free (ctrl
->display
);
1408 ctrl
->display
= strdup (value
);
1410 return out_of_core ();
1412 else if (!strcmp (key
, "ttyname"))
1417 free (ctrl
->ttyname
);
1418 ctrl
->ttyname
= strdup (value
);
1420 return out_of_core ();
1423 else if (!strcmp (key
, "ttytype"))
1428 free (ctrl
->ttytype
);
1429 ctrl
->ttytype
= strdup (value
);
1431 return out_of_core ();
1434 else if (!strcmp (key
, "lc-ctype"))
1437 free (ctrl
->lc_ctype
);
1438 ctrl
->lc_ctype
= strdup (value
);
1439 if (!ctrl
->lc_ctype
)
1440 return out_of_core ();
1442 else if (!strcmp (key
, "lc-messages"))
1444 if (ctrl
->lc_messages
)
1445 free (ctrl
->lc_messages
);
1446 ctrl
->lc_messages
= strdup (value
);
1447 if (!ctrl
->lc_messages
)
1448 return out_of_core ();
1450 else if (!strcmp (key
, "xauthority"))
1452 if (ctrl
->xauthority
)
1453 free (ctrl
->xauthority
);
1454 ctrl
->xauthority
= strdup (value
);
1455 if (!ctrl
->xauthority
)
1456 return out_of_core ();
1458 else if (!strcmp (key
, "pinentry-user-data"))
1460 if (ctrl
->pinentry_user_data
)
1461 free (ctrl
->pinentry_user_data
);
1462 ctrl
->pinentry_user_data
= strdup (value
);
1463 if (!ctrl
->pinentry_user_data
)
1464 return out_of_core ();
1466 else if (!strcmp (key
, "use-cache-for-signing"))
1467 ctrl
->server_local
->use_cache_for_signing
= *value
? atoi (value
) : 0;
1469 return gpg_error (GPG_ERR_UNKNOWN_OPTION
);
1477 /* Called by libassuan after all commands. ERR is the error from the
1478 last assuan operation and not the one returned from the command. */
1480 post_cmd_notify (assuan_context_t ctx
, int err
)
1482 ctrl_t ctrl
= assuan_get_pointer (ctx
);
1484 /* Switch off any I/O monitor controlled logging pausing. */
1485 ctrl
->server_local
->pause_io_logging
= 0;
1489 /* This function is called by libassuan for all I/O. We use it here
1490 to disable logging for the GETEVENTCOUNTER commands. This is so
1491 that the debug output won't get cluttered by this primitive
1494 io_monitor (assuan_context_t ctx
, int direction
,
1495 const char *line
, size_t linelen
)
1497 ctrl_t ctrl
= assuan_get_pointer (ctx
);
1499 /* Note that we only check for the uppercase name. This allows to
1500 see the logging for debugging if using a non-upercase command
1502 if (ctx
&& !direction
1504 && !strncmp (line
, "GETEVENTCOUNTER", 15)
1505 && (linelen
== 15 || spacep (line
+15)))
1507 ctrl
->server_local
->pause_io_logging
= 1;
1510 return ctrl
->server_local
->pause_io_logging
? 1:0;
1514 /* Tell the assuan library about our commands */
1516 register_commands (assuan_context_t ctx
)
1520 int (*handler
)(assuan_context_t
, char *line
);
1522 { "GETEVENTCOUNTER",cmd_geteventcounter
},
1523 { "ISTRUSTED", cmd_istrusted
},
1524 { "HAVEKEY", cmd_havekey
},
1525 { "SIGKEY", cmd_sigkey
},
1526 { "SETKEY", cmd_sigkey
},
1527 { "SETKEYDESC", cmd_setkeydesc
},
1528 { "SETHASH", cmd_sethash
},
1529 { "PKSIGN", cmd_pksign
},
1530 { "PKDECRYPT", cmd_pkdecrypt
},
1531 { "GENKEY", cmd_genkey
},
1532 { "READKEY", cmd_readkey
},
1533 { "GET_PASSPHRASE", cmd_get_passphrase
},
1534 { "PRESET_PASSPHRASE", cmd_preset_passphrase
},
1535 { "CLEAR_PASSPHRASE", cmd_clear_passphrase
},
1536 { "GET_CONFIRMATION", cmd_get_confirmation
},
1537 { "LISTTRUSTED", cmd_listtrusted
},
1538 { "MARKTRUSTED", cmd_marktrusted
},
1539 { "LEARN", cmd_learn
},
1540 { "PASSWD", cmd_passwd
},
1544 { "GETVAL", cmd_getval
},
1545 { "PUTVAL", cmd_putval
},
1546 { "UPDATESTARTUPTTY", cmd_updatestartuptty
},
1547 #ifdef HAVE_W32_SYSTEM
1548 { "KILLAGENT", cmd_killagent
},
1549 { "RELOADAGENT", cmd_reloadagent
},
1551 { "GETINFO", cmd_getinfo
},
1556 for (i
=0; table
[i
].name
; i
++)
1558 rc
= assuan_register_command (ctx
, table
[i
].name
, table
[i
].handler
);
1562 #ifdef HAVE_ASSUAN_SET_IO_MONITOR
1563 assuan_register_post_cmd_notify (ctx
, post_cmd_notify
);
1565 assuan_register_reset_notify (ctx
, reset_notify
);
1566 assuan_register_option_handler (ctx
, option_handler
);
1571 /* Startup the server. If LISTEN_FD and FD is given as -1, this is a
1572 simple piper server, otherwise it is a regular server. CTRL is the
1573 control structure for this connection; it has only the basic
1576 start_command_handler (ctrl_t ctrl
, gnupg_fd_t listen_fd
, gnupg_fd_t fd
)
1579 assuan_context_t ctx
;
1581 if (listen_fd
== GNUPG_INVALID_FD
&& fd
== GNUPG_INVALID_FD
)
1587 rc
= assuan_init_pipe_server (&ctx
, filedes
);
1589 else if (listen_fd
!= GNUPG_INVALID_FD
)
1591 rc
= assuan_init_socket_server_ext (&ctx
, listen_fd
, 0);
1595 rc
= assuan_init_socket_server_ext (&ctx
, fd
, 2);
1599 log_error ("failed to initialize the server: %s\n",
1603 rc
= register_commands (ctx
);
1606 log_error ("failed to register commands with Assuan: %s\n",
1611 assuan_set_pointer (ctx
, ctrl
);
1612 ctrl
->server_local
= xcalloc (1, sizeof *ctrl
->server_local
);
1613 ctrl
->server_local
->assuan_ctx
= ctx
;
1614 ctrl
->server_local
->message_fd
= -1;
1615 ctrl
->server_local
->use_cache_for_signing
= 1;
1616 ctrl
->digest
.raw_value
= 0;
1619 assuan_set_log_stream (ctx
, log_get_stream ());
1621 #ifdef HAVE_ASSUAN_SET_IO_MONITOR
1622 assuan_set_io_monitor (ctx
, io_monitor
);
1627 rc
= assuan_accept (ctx
);
1634 log_info ("Assuan accept problem: %s\n", gpg_strerror (rc
));
1638 rc
= assuan_process (ctx
);
1641 log_info ("Assuan processing failed: %s\n", gpg_strerror (rc
));
1646 /* Reset the SCD if needed. */
1647 agent_reset_scd (ctrl
);
1649 /* Reset the pinentry (in case of popup messages). */
1650 agent_reset_query (ctrl
);
1653 assuan_deinit_server (ctx
);
1654 #ifdef HAVE_W32_SYSTEM
1655 if (ctrl
->server_local
->stopme
)
1658 xfree (ctrl
->server_local
);
1659 ctrl
->server_local
= NULL
;