1 /* get-passphrase.c - Ask for a passphrase via the agent
2 * Copyright (C) 2009 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
32 #include "get-passphrase.h"
34 /* The context used by this process to ask for the passphrase. */
35 static assuan_context_t agent_ctx
;
38 gpg_err_source_t errsource
;
41 const char *agent_program
;
43 const char *lc_messages
;
44 session_env_t session_env
;
45 const char *pinentry_user_data
;
49 /* Set local variable to be used for a possible agent startup. Note
50 that the strings are just pointers and should not anymore be
51 modified by the caller. */
53 gnupg_prepare_get_passphrase (gpg_err_source_t errsource
,
56 const char *agent_program
,
57 const char *opt_lc_ctype
,
58 const char *opt_lc_messages
,
59 session_env_t session_env
)
61 agentargs
.errsource
= errsource
;
62 agentargs
.verbosity
= verbosity
;
63 agentargs
.homedir
= homedir
;
64 agentargs
.agent_program
= agent_program
;
65 agentargs
.lc_ctype
= opt_lc_ctype
;
66 agentargs
.lc_messages
= opt_lc_messages
;
67 agentargs
.session_env
= session_env
;
71 /* Try to connect to the agent via socket or fork it off and work by
72 pipes. Handle the server's initial greeting. */
78 /* Fixme: This code is not thread safe, thus we don't build it with
79 pth. We will need a context for each thread or serialize the
80 access to the agent. */
84 err
= start_new_gpg_agent (&agent_ctx
,
87 agentargs
.agent_program
,
89 agentargs
.lc_messages
,
90 agentargs
.session_env
,
91 agentargs
.verbosity
, 0, NULL
, NULL
);
94 /* Tell the agent that we support Pinentry notifications. No
95 error checking so that it will work with older agents. */
96 assuan_transact (agent_ctx
, "OPTION allow-pinentry-notify",
97 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
104 /* This is the default inquiry callback. It merely handles the
105 Pinentry notification. */
107 default_inq_cb (void *opaque
, const char *line
)
111 if (!strncmp (line
, "PINENTRY_LAUNCHED", 17) && (line
[17]==' '||!line
[17]))
113 gnupg_allow_set_foregound_window ((pid_t
)strtoul (line
+17, NULL
, 10));
114 /* We do not return errors to avoid breaking other code. */
117 log_debug ("ignoring gpg-agent inquiry `%s'\n", line
);
124 membuf_data_cb (void *opaque
, const void *buffer
, size_t length
)
126 membuf_t
*data
= opaque
;
129 put_membuf (data
, buffer
, length
);
134 /* Ask for a passphrase via gpg-agent. On success the caller needs to
135 free the string stored at R_PASSPHRASE. On error NULL will be
136 stored at R_PASSPHRASE and an appropriate gpg error code is
137 returned. With REPEAT set to 1, gpg-agent will ask the user to
138 repeat the just entered passphrase. CACHE_ID is a gpg-agent style
139 passphrase cache id or NULL. ERR_MSG is a error message to be
140 presented to the user (e.g. "bad passphrase - try again") or NULL.
141 PROMPT is the prompt string to label the entry box, it may be NULL
142 for a default one. DESC_MSG is a longer description to be
143 displayed above the entry box, if may be NULL for a default one.
144 If USE_SECMEM is true, the returned passphrase is retruned in
145 secure memory. The length of all these strings is limited; they
146 need to fit in their encoded form into a standard Assuan line (i.e
147 less then about 950 characters). All strings shall be UTF-8. */
149 gnupg_get_passphrase (const char *cache_id
,
152 const char *desc_msg
,
159 char line
[ASSUAN_LINELENGTH
];
160 const char *arg1
= NULL
;
166 *r_passphrase
= NULL
;
168 err
= start_agent ();
172 /* Check that the gpg-agent understands the repeat option. */
173 if (assuan_transact (agent_ctx
,
174 "GETINFO cmd_has_option GET_PASSPHRASE repeat",
175 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
))
176 return gpg_error (GPG_ERR_NOT_SUPPORTED
);
178 arg1
= cache_id
&& *cache_id
? cache_id
:NULL
;
179 if (err_msg
&& *err_msg
)
180 if (!(arg2
= percent_plus_escape (err_msg
)))
182 if (prompt
&& *prompt
)
183 if (!(arg3
= percent_plus_escape (prompt
)))
185 if (desc_msg
&& *desc_msg
)
186 if (!(arg4
= percent_plus_escape (desc_msg
)))
189 snprintf (line
, DIM(line
)-1,
190 "GET_PASSPHRASE --data %s--repeat=%d -- %s %s %s %s",
191 check_quality
? "--check ":"",
197 line
[DIM(line
)-1] = 0;
203 init_membuf_secure (&data
, 64);
205 init_membuf (&data
, 64);
206 err
= assuan_transact (agent_ctx
, line
,
207 membuf_data_cb
, &data
,
208 default_inq_cb
, NULL
, NULL
, NULL
);
210 /* Older Pinentries return the old assuan error code for canceled
211 which gets translated bt libassuan to GPG_ERR_ASS_CANCELED and
212 not to the code for a user cancel. Fix this here. */
213 if (err
&& gpg_err_source (err
)
214 && gpg_err_code (err
) == GPG_ERR_ASS_CANCELED
)
215 err
= gpg_err_make (gpg_err_source (err
), GPG_ERR_CANCELED
);
222 p
= get_membuf (&data
, &n
);
229 put_membuf (&data
, "", 1);
230 *r_passphrase
= get_membuf (&data
, NULL
);
232 err
= gpg_error_from_syserror ();
236 err
= gpg_error_from_syserror ();
244 /* Flush the passphrase cache with Id CACHE_ID. */
246 gnupg_clear_passphrase (const char *cache_id
)
249 char line
[ASSUAN_LINELENGTH
];
251 if (!cache_id
|| !*cache_id
)
254 err
= start_agent ();
258 snprintf (line
, DIM(line
)-1, "CLEAR_PASSPHRASE %s", cache_id
);
259 line
[DIM(line
)-1] = 0;
260 return assuan_transact (agent_ctx
, line
, NULL
, NULL
,
261 default_inq_cb
, NULL
, NULL
, NULL
);