1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 Copyright (C) 2007-2008 Ben Kibbey <bjk@luxsci.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02110-1301 USA
38 #include "pwmd_error.h"
40 void free_client_list();
41 static gpg_error_t
set_pinentry_strings(struct pinentry_s
*pin
, int which
);
43 static int mem_realloc_cb(void *data
, const void *buffer
, size_t len
)
45 membuf_t
*mem
= (membuf_t
*)data
;
51 if ((p
= xrealloc(mem
->buf
, mem
->len
+ len
)) == NULL
)
55 memcpy((char *)mem
->buf
+ mem
->len
, buffer
, len
);
61 static int quality_cb(void *data
, const char *line
)
63 struct pinentry_s
*pin
= data
;
69 if (strncmp(line
, "QUALITY ", 8) != 0)
70 return GPG_ERR_INV_ARG
;
72 if (!(tmp
= FascistCheck(line
+8, CRACKLIB_DICT
)))
73 return assuan_send_data(pin
->ctx
, "100", 3);
75 if (!strcmp(tmp
, "it's WAY too short"))
77 else if (!strcmp(tmp
, "it is too short"))
79 else if (!strcmp(tmp
, "it is all whitespace"))
81 else if (!strcmp(tmp
, "it is based on your username"))
83 else if (!strcmp(tmp
, "it is based on a dictionary word"))
85 else if (!strcmp(tmp
, "it is based upon your password entry"))
87 else if (!strcmp(tmp
, "it's derived from your password entry"))
89 else if (!strcmp(tmp
, "it is based on a (reversed) dictionary word"))
91 else if (!strcmp(tmp
, "it is derivable from your password entry"))
93 else if (!strcmp(tmp
, "it does not contain enough DIFFERENT characters"))
95 else if (!strcmp(tmp
, "it is too simplistic/systematic"))
100 tmp
= (const gchar
*)print_fmt(buf
, sizeof(buf
), "%i", score
);
101 return assuan_send_data(pin
->ctx
, tmp
, strlen(tmp
));
105 static gpg_error_t
assuan_command(struct pinentry_s
*pin
, gchar
**result
,
111 pin
->data
.buf
= NULL
;
113 rc
= assuan_transact(pin
->ctx
, cmd
, mem_realloc_cb
, &pin
->data
,
114 pin
->inquire_cb
, pin
->inquire_data
, NULL
, NULL
);
118 xfree(pin
->data
.buf
);
119 pin
->data
.buf
= NULL
;
124 mem_realloc_cb(&pin
->data
, "", 1);
125 *result
= (gchar
*)pin
->data
.buf
;
132 static gpg_error_t
set_pinentry_options(struct pinentry_s
*pin
)
134 gchar
*display
= getenv("DISPLAY");
135 gint have_display
= 0;
136 gchar
*tty
= NULL
, *ttytype
= NULL
;
139 gchar
*result
= NULL
;
140 gchar cmd
[ASSUAN_LINELENGTH
];
142 if (pin
->display
|| display
)
145 tty
= pin
->ttyname
? pin
->ttyname
: ttyname(STDOUT_FILENO
);
148 return GPG_ERR_CANCELED
;
151 if (!have_display
&& !tty
)
152 return GPG_ERR_CANCELED
;
155 gchar
*p
= getenv("TERM");
157 ttytype
= pin
->ttytype
? pin
->ttytype
: p
;
160 return GPG_ERR_CANCELED
;
163 opt
= have_display
? "DISPLAY" : "TTYNAME";
164 val
= have_display
? pin
->display
? pin
->display
: display
: tty
;
165 g_snprintf(cmd
, sizeof(cmd
), "OPTION %s=%s", g_ascii_strdown(opt
, strlen(opt
)), val
);
166 rc
= assuan_command(pin
, &result
, cmd
);
172 g_snprintf(cmd
, sizeof(cmd
), "OPTION ttytype=%s", ttytype
);
173 rc
= assuan_command(pin
, &result
, cmd
);
179 static gpg_error_t
launch_pinentry(struct pinentry_s
*pin
)
182 assuan_context_t ctx
;
183 gint child_list
[] = {-1};
184 const gchar
*argv
[4];
186 argv
[0] = "pinentry";
189 argv
[1] = "--display";
190 argv
[2] = pin
->display
;
196 rc
= assuan_pipe_connect(&ctx
, pin
->path
? pin
->path
: PINENTRY_PATH
,
202 pin
->pid
= assuan_get_pid(ctx
);
204 rc
= set_pinentry_options(pin
);
205 return rc
? rc
: set_pinentry_strings(pin
, 0);
208 static gpg_error_t
pinentry_command(struct pinentry_s
*pin
, gchar
**result
,
214 rc
= launch_pinentry(pin
);
216 return rc
? rc
: assuan_command(pin
, result
, cmd
);
219 static gpg_error_t
set_pinentry_strings(struct pinentry_s
*pin
, int which
)
226 if (pin
->which
== PINENTRY_SAVE
&& which
!= 2) {
227 rc
= pinentry_command(pin
, NULL
, "SETQUALITYBAR");
232 pin
->inquire_cb
= quality_cb
;
233 pin
->inquire_data
= pin
;
238 title
= g_strdup(N_("Password mismatch, please try again."));
239 else if (!pin
->title
)
240 title
= pin
->title
= g_strdup(N_("Password Manager Daemon"));
245 pin
->prompt
= g_strdup(N_("Password:"));
247 if (!pin
->desc
&& !which
)
248 pin
->desc
= g_strdup_printf(pin
->which
== PINENTRY_OPEN
?
249 N_("A password is required to open the file \"%s\". Please%%0Aenter the password below.") :
250 N_("A password is required to save to the file \"%s\". Please%%0Aenter the password below."),
254 buf
= g_strdup_printf("SETERROR %s", N_("Please type the password again for confirmation."));
256 buf
= g_strdup_printf("SETERROR %s", pin
->desc
);
258 rc
= pinentry_command(pin
, NULL
, buf
);
264 buf
= g_strdup_printf("SETPROMPT %s", pin
->prompt
);
265 rc
= pinentry_command(pin
, NULL
, buf
);
271 buf
= g_strdup_printf("SETDESC %s", title
);
272 rc
= pinentry_command(pin
, NULL
, buf
);
282 static void pinentry_disconnect(struct pinentry_s
*pin
)
288 assuan_disconnect(pin
->ctx
);
294 static gpg_error_t
do_getpin(struct pinentry_s
*pin
, char **result
)
299 rc
= pinentry_command(pin
, result
, "GETPIN");
302 *result
= xstrdup("");
307 gpg_error_t
pinentry_getpin(struct pinentry_s
*pin
, gchar
**result
)
310 gpg_error_t rc
= set_pinentry_strings(pin
, which
);
311 gchar
*result1
= NULL
;
317 rc
= do_getpin(pin
, result
);
322 if (pin
->which
== PINENTRY_SAVE
) {
324 rc
= set_pinentry_strings(pin
, 2);
329 result1
= g_strdup(*result
);
333 if (strcmp(result1
, *result
)) {
336 result1
= *result
= NULL
;
337 rc
= set_pinentry_strings(pin
, 1);
348 pinentry_disconnect(pin
);
352 static int write_result(int fd
, pinentry_key_s
*pk
, char *result
)
358 * libassuan handles GPG_ERR_EOF in assuan_process_done() and
359 * will disconnect the client even if the error isn't related
360 * to it. Use GPG_ERR_CANCELED instead.
362 if (gpg_err_code(pk
->error
) == GPG_ERR_EOF
)
363 pk
->error
= GPG_ERR_CANCELED
;
365 len
= pth_write(fd
, pk
, sizeof(pinentry_key_s
));
368 if (len
!= sizeof(pinentry_key_s
))
369 log_write("%s(%i): write: len != sizeof(pk)", __FUNCTION__
, __LINE__
);
374 if (pk
->status
== PINENTRY_PID
)
375 pk
->what
.pid
= atoi(result
);
377 g_strlcpy(pk
->what
.key
, result
, sizeof(pk
->what
.key
));
380 len
= pth_write(fd
, pk
, sizeof(pinentry_key_s
));
382 if (len
!= sizeof(pinentry_key_s
)) {
383 memset(pk
, 0, sizeof(pinentry_key_s
));
384 log_write("%s(%i): write: len != sizeof(pk)", __FUNCTION__
, __LINE__
);
389 if (pk
->status
!= PINENTRY_PID
)
392 memset(pk
, 0, sizeof(pinentry_key_s
));
396 static void reset(struct pinentry_s
*pin
)
400 pth_waitpid(pin
->pid
, &status
, 0);
403 pin
->pid
= pin
->pin_pid
= 0;
405 pth_event_isolate(pin
->ev
);
406 pth_event_free(pin
->ev
, PTH_FREE_THIS
);
408 pin
->status
= PINENTRY_NONE
;
411 static void *timeout_thread(void *arg
)
413 struct pinentry_s
*pin
= arg
;
414 pth_event_t ev
= pth_event(PTH_EVENT_TIME
, pth_timeout(pin
->timeout
, 0));
417 pth_event_free(ev
, PTH_FREE_THIS
);
418 pth_mutex_acquire(&pin
->status_mutex
, FALSE
, NULL
);
420 /* pth_cancel() was called from pinentry_iterate() (we have a key).
421 * pth_wait() was the cancelation point. */
422 if (pin
->status
== PINENTRY_NONE
) {
424 pth_mutex_release(&pin
->status_mutex
);
428 if (kill(pin
->pin_pid
, 0) == 0)
429 if (kill(pin
->pin_pid
, SIGTERM
) == 0)
430 if (kill(pin
->pin_pid
, 0) == 0)
431 kill(pin
->pin_pid
, SIGKILL
);
434 pin
->status
= PINENTRY_TIMEOUT
;
435 pth_mutex_release(&pin
->status_mutex
);
436 pth_exit(PTH_CANCELED
);
440 gpg_error_t
pinentry_fork(assuan_context_t ctx
)
442 struct client_s
*client
= assuan_get_pointer(ctx
);
443 struct pinentry_s
*pin
= client
->pinentry
;
448 gchar
*result
= NULL
;
451 return gpg_error_from_syserror();
457 rc
= gpg_error_from_syserror();
465 if (pin
->timeout
> 0 && client
->pinentry
->which
== PINENTRY_OPEN
) {
467 * Send the pid of the pinentry process back to pwmd so it can
468 * handle the pinentry timeout properly.
470 pk
.status
= PINENTRY_PID
;
471 pk
.error
= pinentry_command(pin
, &result
, "GETINFO pid");
473 if (write_result(p
[1], &pk
, result
))
477 pk
.status
= PINENTRY_RUNNING
;
478 pk
.error
= pinentry_getpin(pin
, &result
);
480 if (write_result(p
[1], &pk
, result
))
486 client
->pinentry
->fd
= p
[0];
487 client
->pinentry
->pid
= pid
;
488 client
->pinentry
->status
= PINENTRY_INIT
;
493 * Don't call assuan_process_done() here. That should be done in
494 * open_command_finalize() after the key has been read().
499 gpg_error_t
lock_pin_mutex(struct client_s
*client
)
501 while (pth_mutex_acquire(&pin_mutex
, TRUE
, NULL
) == FALSE
) {
503 gpg_error_t rc
= send_status(client
->ctx
, STATUS_LOCKED
);
512 client
->pinentry
->has_lock
= TRUE
;
516 void unlock_pin_mutex(struct pinentry_s
*pin
)
518 if (pin
->has_lock
== FALSE
)
521 pth_mutex_release(&pin_mutex
);
522 pin
->has_lock
= FALSE
;
525 void cleanup_pinentry(struct pinentry_s
*pin
)
530 unlock_pin_mutex(pin
);
532 if (pin
->ctx
&& pin
->pid
)
533 pinentry_disconnect(pin
);
535 g_free(pin
->ttyname
);
536 g_free(pin
->ttytype
);
541 g_free(pin
->display
);
542 g_free(pin
->filename
);
546 void set_pinentry_defaults(struct pinentry_s
*pin
)
550 struct passwd
*pw
= getpwuid(getuid());
554 g_snprintf(buf
, sizeof(buf
), "%s/.pwmd/pinentry.conf", pw
->pw_dir
);
555 fp
= fopen(buf
, "r");
558 while ((p
= fgets(buf
, sizeof(buf
), fp
)) != NULL
) {
559 gchar name
[32] = {0}, value
[256] = {0};
564 if (p
[strlen(p
)-1] == '\n')
567 if (sscanf(p
, " %31[a-zA-Z] = %255s", name
, value
) != 2)
570 if (g_strcasecmp("TTYNAME", name
) == 0)
571 pin
->ttyname
= g_strdup(value
);
572 else if (g_strcasecmp("TTYTYPE", name
) == 0)
573 pin
->ttytype
= g_strdup(value
);
574 else if (g_strcasecmp("DISPLAY", name
) == 0)
575 pin
->display
= g_strdup(value
);
576 else if (g_strcasecmp("PATH", name
) == 0)
577 pin
->path
= g_strdup(value
);
583 pin
->timeout
= get_key_file_integer("global", "pinentry_timeout");
586 pth_event_t
pinentry_iterate(struct client_s
*cl
, pth_event_t ev
)
590 pth_mutex_acquire(&cl
->pinentry
->status_mutex
, FALSE
, NULL
);
592 /* Set from pinentry_timeout_thread(). */
593 if (cl
->pinentry
->status
== PINENTRY_TIMEOUT
) {
594 cl
->pinentry
->status
= PINENTRY_NONE
;
595 rc
= send_error(cl
->ctx
, GPG_ERR_TIMEOUT
);
598 unlock_pin_mutex(cl
->pinentry
);
601 if (cl
->pinentry
->status
== PINENTRY_RUNNING
) {
602 pth_event_isolate(cl
->pinentry
->ev
);
604 if (pth_event_occurred(cl
->pinentry
->ev
)) {
605 guchar shakey
[gcrykeysize
];
609 memset(&pk
, 0, sizeof(pk
));
610 len
= pth_read(cl
->pinentry
->fd
, &pk
, sizeof(pk
));
612 if (len
== sizeof(pk
)) {
614 if (cl
->pinentry
->status
== PINENTRY_TIMEOUT
)
615 pk
.error
= GPG_ERR_TIMEOUT
;
617 rc
= send_error(cl
->ctx
, pk
.error
);
619 else if (pk
.status
== PINENTRY_PID
) {
621 * Start the timeout thread for the pinentry process
622 * now that we know the pid of it.
624 pth_attr_t attr
= pth_attr_new();
627 pth_attr_set(attr
, PTH_ATTR_JOINABLE
, 0);
628 pth_attr_set(attr
, PTH_ATTR_CANCEL_STATE
,
629 PTH_CANCEL_ASYNCHRONOUS
);
630 cl
->pinentry
->pin_pid
= pk
.what
.pid
;
631 cl
->pinentry
->tid
= pth_spawn(attr
, timeout_thread
,
635 if (cl
->pinentry
->tid
) {
636 cl
->pinentry
->status
= PINENTRY_NONE
;
637 pth_cancel(cl
->pinentry
->tid
);
640 gcry_md_hash_buffer(GCRY_MD_SHA256
, shakey
, pk
.what
.key
,
641 strlen(pk
.what
.key
) == 0 ? 1 : strlen(pk
.what
.key
));
642 rc
= cl
->pinentry
->cb(cl
->ctx
, shakey
, FALSE
);
643 memset(shakey
, 0, sizeof(shakey
));
646 else if (len
== -1) {
647 if (cl
->pinentry
->status
== PINENTRY_TIMEOUT
)
648 pk
.error
= GPG_ERR_TIMEOUT
;
650 pk
.error
= gpg_err_code_from_syserror();
651 log_write("%s", gpg_strerror(pk
.error
));
654 if (cl
->pinentry
->status
== PINENTRY_TIMEOUT
)
655 pk
.error
= GPG_ERR_TIMEOUT
;
657 pk
.error
= GPG_ERR_EOF
;
658 log_write("%s", gpg_strerror(pk
.error
));
661 log_write(N_("pth_read(): short byte count"));
664 if (cl
->pinentry
->tid
) {
665 pth_cancel(cl
->pinentry
->tid
);
666 cl
->pinentry
->status
= PINENTRY_NONE
;
671 if (cl
->pinentry
->which
== PINENTRY_OPEN
)
674 else if (pk
.status
== PINENTRY_RUNNING
)
677 if (pk
.error
|| pk
.status
== PINENTRY_RUNNING
) {
678 unlock_file_mutex(cl
);
679 unlock_pin_mutex(cl
->pinentry
);
682 memset(&pk
, 0, sizeof(pk
));
685 pth_event_concat(ev
, cl
->pinentry
->ev
, NULL
);
688 pth_mutex_release(&cl
->pinentry
->status_mutex
);
692 struct pinentry_s
*pinentry_init()
694 struct pinentry_s
*pin
= g_malloc0(sizeof(struct pinentry_s
));
699 pth_mutex_init(&pin
->status_mutex
);
700 set_pinentry_defaults(pin
);