2 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
3 Ben Kibbey <bjk@luxsci.net>
5 This file is part of libpwmd.
7 Libpwmd 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 2 of the License, or
10 (at your option) any later version.
12 Libpwmd 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 Libpwmd. If not, see <http://www.gnu.org/licenses/>.
24 #include <sys/types.h>
40 static gpg_error_t
set_pinentry_strings (pwm_t
* pwm
, pwmd_pinentry_t which
);
43 launch_pinentry (pwm_t
* pwm
)
47 int child_list
[] = { -1 };
49 const char **p
= argv
;
50 static struct assuan_malloc_hooks mhooks
= {
51 pwmd_malloc
, pwmd_realloc
, pwmd_free
54 update_pinentry_settings (pwm
);
55 if (!pwm
->pinentry_display
&& !pwm
->pinentry_tty
)
56 return GPG_ERR_ENOTTY
;
59 *p
++ = pwm
->pinentry_display
? "--display" : "--ttyname";
60 *p
++ = pwm
->pinentry_display
? pwm
->pinentry_display
: pwm
->pinentry_tty
;
62 if (pwm
->pinentry_lcctype
)
65 *p
++ = pwm
->pinentry_lcctype
;
68 if (pwm
->pinentry_lcmessages
)
70 *p
++ = "--lc-messages";
71 *p
++ = pwm
->pinentry_lcmessages
;
76 if (!pwm
->pinentry_display
)
79 *p
++ = pwm
->pinentry_term
? pwm
->pinentry_term
: getenv ("TERM");
83 rc
= assuan_new_ext (&ctx
, GPG_ERR_SOURCE_PINENTRY
, &mhooks
, NULL
, NULL
);
87 rc
= assuan_pipe_connect (ctx
,
88 pwm
->pinentry_path
? pwm
->
89 pinentry_path
: PINENTRY_PATH
, argv
, child_list
,
97 pwm
->pinentry_pid
= assuan_get_pid (ctx
);
99 rc
= set_pinentry_strings (pwm
, 0);
101 _pinentry_disconnect (pwm
);
107 pinentry_command (pwm_t
* pwm
, char **result
, size_t * len
, const char *cmd
)
117 gpg_error_t rc
= launch_pinentry (pwm
);
123 return _assuan_command (pwm
, pwm
->pctx
, result
, len
, cmd
);
128 quality_cb (void *data
, const char *line
)
135 if (strncmp (line
, "QUALITY ", 8))
136 return GPG_ERR_INV_ARG
;
138 if (!(tmp
= FascistCheck (line
+ 8, CRACKLIB_DICT
)))
139 return assuan_send_data (pwm
->pctx
, "100", 3);
141 if (!strcmp (tmp
, N_("it is WAY too short")))
143 else if (!strcmp (tmp
, N_("it is too short")))
145 else if (!strcmp (tmp
, N_("it is all whitespace")))
147 else if (!strcmp (tmp
, N_("it is based on your username")))
150 if (!strcmp (tmp
, N_("it does not contain enough DIFFERENT characters")))
152 else if (!strcmp (tmp
, N_("it is based on a dictionary word")))
154 else if (!strcmp (tmp
, N_("it is based upon your password entry")))
156 else if (!strcmp (tmp
, N_("it's derived from your password entry")))
158 else if (!strcmp (tmp
, N_("it is derived from your password entry")))
160 else if (!strcmp (tmp
, N_("it is based on a (reversed) dictionary word")))
162 else if (!strcmp (tmp
, N_("it is derivable from your password entry")))
164 else if (!strcmp (tmp
, N_("it's derivable from your password entry")))
166 else if (!strcmp (tmp
, N_("it looks like a National Insurance number.")))
168 else if (!strcmp (tmp
, N_("it is too simplistic/systematic")))
173 snprintf (buf
, sizeof (buf
), "%i", score
);
174 return assuan_send_data (pwm
->pctx
, buf
, strlen (buf
));
179 set_pinentry_strings (pwm_t
* pwm
, pwmd_pinentry_t which
)
181 char *tmp
, *desc
= NULL
;
184 if (which
!= PWMD_PINENTRY_USER
)
186 rc
= pinentry_command (pwm
, NULL
, NULL
, "SETERROR ");
191 tmp
= pwmd_malloc (ASSUAN_LINELENGTH
+ 1);
193 return gpg_error_from_errno (ENOMEM
);
195 if (!pwm
->pinentry_prompt
)
197 pwm
->pinentry_prompt
= pwmd_strdup (N_("Passphrase:"));
198 if (!pwm
->pinentry_prompt
)
201 return GPG_ERR_ENOMEM
;
205 if (!pwm
->pinentry_desc
)
207 if (which
== PWMD_PINENTRY_OPEN
|| which
== PWMD_PINENTRY_OPEN_FAILED
)
209 pwmd_strdup_printf (N_
210 ("A passphrase is required to unlock the secret key for the encrypted data file \"%s\". Please enter the passphrase below."),
212 else if (which
== PWMD_PINENTRY_SAVE
213 || which
== PWMD_PINENTRY_SAVE_FAILED
)
215 pwmd_strdup_printf (N_
216 ("Please enter the passphrase to use to encrypt the data file \"%s\"."),
218 else if (which
== PWMD_PINENTRY_SAVE_CONFIRM
)
220 pwmd_strdup_printf (N_
221 ("Please re-enter the passphrase for confirmation."),
227 return GPG_ERR_ENOMEM
;
231 if (pwm
->pinentry_desc
)
232 desc
= pwm
->pinentry_desc
;
234 if (which
== PWMD_PINENTRY_USER
)
236 snprintf (tmp
, ASSUAN_LINELENGTH
, "SETERROR %s",
237 pwm
->pinentry_error
? pwm
->pinentry_error
: "");
238 rc
= pinentry_command (pwm
, NULL
, NULL
, tmp
);
245 snprintf (tmp
, ASSUAN_LINELENGTH
, "SETDESC %s",
246 pwm
->pinentry_desc
? pwm
->pinentry_desc
: "");
250 if (which
== PWMD_PINENTRY_SAVE_FAILED
251 || which
== PWMD_PINENTRY_OPEN_FAILED
)
253 if (which
== PWMD_PINENTRY_SAVE_FAILED
)
254 snprintf (tmp
, ASSUAN_LINELENGTH
, "SETERROR %s",
255 N_("Passphrases do not match, try again."));
258 if (pwm
->pinentry_tries
)
260 strcpy (tmp
, "SETERROR ");
261 snprintf (tmp
+ strlen ("SETERROR "), ASSUAN_LINELENGTH
,
262 N_("Bad passphrase (try %i of %i)"),
263 pwm
->pinentry_try
+ 1, pwm
->pinentry_tries
);
266 snprintf (tmp
, ASSUAN_LINELENGTH
, "SETERROR %s",
267 N_("Bad passphrase, try again"));
270 rc
= pinentry_command (pwm
, NULL
, NULL
, tmp
);
278 snprintf (tmp
, ASSUAN_LINELENGTH
, "SETDESC %s", desc
);
280 if (pwm
->pinentry_desc
!= desc
)
284 rc
= pinentry_command (pwm
, NULL
, NULL
, tmp
);
291 snprintf (tmp
, ASSUAN_LINELENGTH
, "SETPROMPT %s", pwm
->pinentry_prompt
);
292 rc
= pinentry_command (pwm
, NULL
, NULL
, tmp
);
296 if (!rc
&& (which
== PWMD_PINENTRY_SAVE
||
297 which
== PWMD_PINENTRY_SAVE_FAILED
))
299 rc
= pinentry_command (pwm
, NULL
, NULL
, "SETQUALITYBAR");
302 pwm
->_inquire_func
= quality_cb
;
303 pwm
->_inquire_data
= pwm
;
308 if (pwm
->pinentry_timeout
>= 0)
312 snprintf(buf
, sizeof(buf
), "SETTIMEOUT %i", pwm
->pinentry_timeout
);
313 rc
= pinentry_command(pwm
, NULL
, NULL
, buf
);
320 _pinentry_disconnect (pwm_t
* pwm
)
323 assuan_release (pwm
->pctx
);
326 pwm
->pinentry_pid
= -1;
330 _getpin (pwm_t
* pwm
, char **result
, size_t * len
, pwmd_pinentry_t which
)
332 gpg_error_t rc
= set_pinentry_strings (pwm
, which
);
336 _pinentry_disconnect (pwm
);
340 rc
= pinentry_command (pwm
, result
, len
,
341 which
== PWMD_PINENTRY_CONFIRM
? "CONFIRM" : "GETPIN");
344 _pinentry_disconnect (pwm
);
346 /* This lets PWMD_OPTION_PINENTRY_TIMEOUT work. Note that it is not
347 * thread safe do to the global variables. */
348 if (gpg_err_code (rc
) == GPG_ERR_EOF
)
349 rc
= GPG_ERR_CANCELED
;
351 else if (which
!= PWMD_PINENTRY_CONFIRM
&& len
&& result
)
354 *len
= strlen (*result
); // remove the null byte
357 *result
= pwmd_malloc (1);
367 _pwmd_getpin (pwm_t
* pwm
, const char *filename
, char **result
,
368 size_t * len
, pwmd_pinentry_t which
)
374 return GPG_ERR_INV_ARG
;
377 if (which
== PWMD_PINENTRY_CLOSE
)
379 _pinentry_disconnect (pwm
);
383 if (!result
&& which
!= PWMD_PINENTRY_CONFIRM
)
384 return GPG_ERR_INV_ARG
;
386 pwm
->filename
= (char *) filename
;
387 rc
= _getpin (pwm
, result
, len
, which
);