2 Copyright (C) 2006-2016 Ben Kibbey <bjk@luxsci.net>
4 This file is part of libpwmd.
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 This library 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 GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
25 #include <sys/types.h>
41 static gpg_error_t
set_pinentry_strings (pwm_t
* pwm
, pwmd_pinentry_t which
);
44 launch_pinentry (pwm_t
* pwm
)
48 int child_list
[] = { -1 };
50 const char **p
= argv
;
51 static struct assuan_malloc_hooks mhooks
= {
52 pwmd_malloc
, pwmd_realloc
, pwmd_free
56 if (pwm
->pinentry_display
)
59 *p
++ = pwm
->pinentry_display
;
62 if (pwm
->pinentry_tty
)
65 *p
++ = pwm
->pinentry_tty
;
68 if (pwm
->pinentry_term
)
71 *p
++ = pwm
->pinentry_term
;
74 if (pwm
->pinentry_lcctype
)
77 *p
++ = pwm
->pinentry_lcctype
;
80 if (pwm
->pinentry_lcmessages
)
82 *p
++ = "--lc-messages";
83 *p
++ = pwm
->pinentry_lcmessages
;
87 rc
= assuan_new_ext (&ctx
, GPG_ERR_SOURCE_PINENTRY
, &mhooks
, NULL
, NULL
);
91 rc
= assuan_pipe_connect (ctx
,
92 pwm
->pinentry_path
? pwm
->pinentry_path
93 : PINENTRY_PATH
, argv
, child_list
, NULL
, NULL
, 0);
100 pwm
->pinentry_pid
= assuan_get_pid (ctx
);
102 rc
= set_pinentry_strings (pwm
, 0);
104 _pinentry_disconnect (pwm
);
110 pinentry_command (pwm_t
* pwm
, char **result
, size_t * len
, const char *cmd
)
120 gpg_error_t rc
= launch_pinentry (pwm
);
126 return _assuan_command (pwm
, pwm
->pctx
, result
, len
, cmd
);
131 quality_cb (void *data
, const char *line
)
133 #define MAX_ENTROPY 80 // in bits
138 if (strncmp (line
, "QUALITY ", 8))
139 return GPG_ERR_INV_ARG
;
141 match
= ZxcvbnMatch (line
+8, NULL
, NULL
);
142 match
= (match
/MAX_ENTROPY
)*100;
143 snprintf (buf
, sizeof (buf
), "%u", (unsigned)match
);
144 return assuan_send_data (pwm
->pctx
, buf
, strlen (buf
));
149 set_pinentry_strings (pwm_t
* pwm
, pwmd_pinentry_t which
)
151 char *tmp
, *desc
= NULL
;
154 if (which
!= PWMD_PINENTRY_USER
)
156 rc
= pinentry_command (pwm
, NULL
, NULL
, "SETERROR ");
161 tmp
= pwmd_malloc (ASSUAN_LINELENGTH
+ 1);
163 return gpg_error_from_errno (ENOMEM
);
165 if (!pwm
->pinentry_prompt
)
167 pwm
->pinentry_prompt
= pwmd_strdup (N_("Passphrase:"));
168 if (!pwm
->pinentry_prompt
)
171 return GPG_ERR_ENOMEM
;
175 if (!pwm
->pinentry_desc
)
177 if (which
== PWMD_PINENTRY_OPEN
|| which
== PWMD_PINENTRY_OPEN_FAILED
)
179 if (pwm
->passphrase_hint
&& pwm
->passphrase_info
)
181 char *hint
= pwmd_strdup (pwm
->passphrase_hint
), *hintp
= hint
;
182 char *info
= pwmd_strdup (pwm
->passphrase_info
), *infop
= info
;
183 char *userid
= strchr (hint
, ' ');
185 if (userid
&& *userid
)
188 infop
= strchr (info
, ' ');
191 char *p
= strchr (++infop
, ' ');
194 infop
[strlen(infop
)-strlen(p
)] = 0;
199 desc
= pwmd_strdup_printf (N_
200 ("Please enter the passphrase to unlock the OpenPGP secret key:%%0A%%0AClient: %s%%0AData file: %s%%0AKey ID: %s (0x%s)%%0AMain key ID: 0x%s"), pwm
->name
? pwm
->name
: N_("unknown"), pwm
->filename
, userid
, hintp
, infop
);
206 desc
= pwmd_strdup_printf (N_ ("A passphrase is required to decrypt the encrypted%%0Adata file \"%s\". Please enter the passphrase below."), pwm
->filename
);
209 else if (which
== PWMD_PINENTRY_SAVE
210 || which
== PWMD_PINENTRY_SAVE_FAILED
)
212 pwmd_strdup_printf (N_
213 ("Please enter the passphrase to use to encrypt%%0Athe data file \"%s\"."),
215 else if (which
== PWMD_PINENTRY_SAVE_CONFIRM
)
217 pwmd_strdup_printf (N_
218 ("Please re-enter the passphrase for confirmation."),
224 return GPG_ERR_ENOMEM
;
228 desc
= pwm
->pinentry_desc
;
230 if (which
== PWMD_PINENTRY_USER
)
232 snprintf (tmp
, ASSUAN_LINELENGTH
, "SETERROR %s",
233 pwm
->pinentry_error
? pwm
->pinentry_error
: "");
234 rc
= pinentry_command (pwm
, NULL
, NULL
, tmp
);
241 snprintf (tmp
, ASSUAN_LINELENGTH
, "SETDESC %s",
242 pwm
->pinentry_desc
? pwm
->pinentry_desc
: "");
246 if (which
== PWMD_PINENTRY_SAVE_FAILED
247 || which
== PWMD_PINENTRY_OPEN_FAILED
)
249 if (which
== PWMD_PINENTRY_SAVE_FAILED
)
250 snprintf (tmp
, ASSUAN_LINELENGTH
, "SETERROR %s",
251 N_("Passphrases do not match, try again."));
254 if (pwm
->pinentry_tries
)
256 strcpy (tmp
, "SETERROR ");
257 snprintf (tmp
+ strlen ("SETERROR "), ASSUAN_LINELENGTH
,
258 N_("Bad passphrase (try %i of %i)"),
259 pwm
->pinentry_try
+ 1, pwm
->pinentry_tries
);
262 snprintf (tmp
, ASSUAN_LINELENGTH
, "SETERROR %s",
263 N_("Bad passphrase, try again"));
266 rc
= pinentry_command (pwm
, NULL
, NULL
, tmp
);
274 snprintf (tmp
, ASSUAN_LINELENGTH
, "SETDESC %s", desc
);
276 if (pwm
->pinentry_desc
!= desc
)
280 rc
= pinentry_command (pwm
, NULL
, NULL
, tmp
);
287 snprintf (tmp
, ASSUAN_LINELENGTH
, "SETPROMPT %s", pwm
->pinentry_prompt
);
288 rc
= pinentry_command (pwm
, NULL
, NULL
, tmp
);
292 if (!rc
&& (which
== PWMD_PINENTRY_SAVE
||
293 which
== PWMD_PINENTRY_SAVE_FAILED
))
295 rc
= pinentry_command (pwm
, NULL
, NULL
, "SETQUALITYBAR");
298 pwm
->_inquire_func
= quality_cb
;
299 pwm
->_inquire_data
= pwm
;
304 if (!rc
&& pwm
->pinentry_timeout
>= 0)
308 snprintf(buf
, sizeof(buf
), "SETTIMEOUT %i", pwm
->pinentry_timeout
);
309 rc
= pinentry_command(pwm
, NULL
, NULL
, buf
);
316 _pinentry_disconnect (pwm_t
* pwm
)
319 assuan_release (pwm
->pctx
);
322 pwm
->pinentry_pid
= -1;
326 _getpin (pwm_t
* pwm
, char **result
, size_t * len
, pwmd_pinentry_t which
)
328 gpg_error_t rc
= set_pinentry_strings (pwm
, which
);
332 _pinentry_disconnect (pwm
);
336 rc
= pinentry_command (pwm
, result
, len
,
337 which
== PWMD_PINENTRY_CONFIRM
? "CONFIRM" : "GETPIN");
340 _pinentry_disconnect (pwm
);
342 /* This lets PWMD_OPTION_PINENTRY_TIMEOUT work. Note that it is not
343 * thread safe do to the global variables. */
344 if (gpg_err_code (rc
) == GPG_ERR_EOF
)
345 rc
= GPG_ERR_CANCELED
;
347 else if (which
!= PWMD_PINENTRY_CONFIRM
&& len
&& result
)
350 *len
= strlen (*result
); // remove the null byte
353 *result
= pwmd_malloc (1);
363 _pwmd_getpin (pwm_t
* pwm
, const char *filename
, char **result
,
364 size_t * len
, pwmd_pinentry_t which
)
370 return GPG_ERR_INV_ARG
;
373 if (which
== PWMD_PINENTRY_CLOSE
)
375 _pinentry_disconnect (pwm
);
379 if (!result
&& which
!= PWMD_PINENTRY_CONFIRM
)
380 return GPG_ERR_INV_ARG
;
382 pwm
->filename
= (char *) filename
;
383 rc
= _getpin (pwm
, result
, len
, which
);