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
31 #include <sys/types.h>
44 #include "pwmd_error.h"
46 void free_client_list();
47 static gpg_error_t
set_pinentry_strings(struct pinentry_s
*pin
, int which
);
49 static int mem_realloc_cb(void *data
, const void *buffer
, size_t len
)
51 membuf_t
*mem
= (membuf_t
*)data
;
57 if ((p
= xrealloc(mem
->buf
, mem
->len
+ len
)) == NULL
)
61 memcpy((char *)mem
->buf
+ mem
->len
, buffer
, len
);
67 static int quality_cb(void *data
, const char *line
)
69 struct pinentry_s
*pin
= data
;
74 if (strncmp(line
, "QUALITY ", 8) != 0)
75 return GPG_ERR_INV_ARG
;
77 if (!(tmp
= FascistCheck(line
+8, CRACKLIB_DICT
)))
78 return assuan_send_data(pin
->ctx
, "100", 3);
80 if (!strcmp(tmp
, "it's WAY too short"))
82 else if (!strcmp(tmp
, "it is too short"))
84 else if (!strcmp(tmp
, "it is all whitespace"))
86 else if (!strcmp(tmp
, "it is based on your username"))
88 else if (!strcmp(tmp
, "it is based on a dictionary word"))
90 else if (!strcmp(tmp
, "it is based upon your password entry"))
92 else if (!strcmp(tmp
, "it's derived from your password entry"))
94 else if (!strcmp(tmp
, "it is based on a (reversed) dictionary word"))
96 else if (!strcmp(tmp
, "it is derivable from your password entry"))
98 else if (!strcmp(tmp
, "it does not contain enough DIFFERENT characters"))
100 else if (!strcmp(tmp
, "it is too simplistic/systematic"))
105 tmp
= (const gchar
*)print_fmt(buf
, sizeof(buf
), "%i", score
);
106 return assuan_send_data(pin
->ctx
, tmp
, strlen(tmp
));
110 static gpg_error_t
assuan_command(struct pinentry_s
*pin
, gchar
**result
,
116 pin
->data
.buf
= NULL
;
118 rc
= assuan_transact(pin
->ctx
, cmd
, mem_realloc_cb
, &pin
->data
,
119 pin
->inquire_cb
, pin
->inquire_data
, NULL
, NULL
);
123 xfree(pin
->data
.buf
);
124 pin
->data
.buf
= NULL
;
129 mem_realloc_cb(&pin
->data
, "", 1);
130 *result
= (gchar
*)pin
->data
.buf
;
137 static gpg_error_t
set_pinentry_options(struct pinentry_s
*pin
)
139 gchar
*display
= getenv("DISPLAY");
140 gint have_display
= 0;
141 gchar
*tty
= NULL
, *ttytype
= NULL
;
144 gchar
*result
= NULL
;
145 gchar cmd
[ASSUAN_LINELENGTH
];
147 if (pin
->display
|| display
)
150 tty
= pin
->ttyname
? pin
->ttyname
: ttyname(STDOUT_FILENO
);
153 return GPG_ERR_CANCELED
;
156 if (!have_display
&& !tty
)
157 return GPG_ERR_CANCELED
;
160 gchar
*p
= getenv("TERM");
162 ttytype
= pin
->ttytype
? pin
->ttytype
: p
;
165 return GPG_ERR_CANCELED
;
168 opt
= have_display
? "DISPLAY" : "TTYNAME";
169 val
= have_display
? pin
->display
? pin
->display
: display
: tty
;
170 g_snprintf(cmd
, sizeof(cmd
), "OPTION %s=%s", g_ascii_strdown(opt
, strlen(opt
)), val
);
171 rc
= assuan_command(pin
, &result
, cmd
);
177 g_snprintf(cmd
, sizeof(cmd
), "OPTION ttytype=%s", ttytype
);
178 rc
= assuan_command(pin
, &result
, cmd
);
184 static gpg_error_t
launch_pinentry(struct pinentry_s
*pin
)
187 assuan_context_t ctx
;
188 gint child_list
[] = {-1};
189 const gchar
*argv
[8];
190 const gchar
**p
= argv
;
204 if (pin
->lcmessages
) {
205 *p
++ = "--lc-messages";
206 *p
++ = pin
->lcmessages
;
210 rc
= assuan_pipe_connect(&ctx
, pin
->path
? pin
->path
: PINENTRY_PATH
,
216 pin
->pid
= assuan_get_pid(ctx
);
218 rc
= set_pinentry_options(pin
);
219 return rc
? rc
: set_pinentry_strings(pin
, 0);
222 static gpg_error_t
pinentry_command(struct pinentry_s
*pin
, gchar
**result
,
228 rc
= launch_pinentry(pin
);
230 return rc
? rc
: assuan_command(pin
, result
, cmd
);
233 static gpg_error_t
set_pinentry_strings(struct pinentry_s
*pin
, int which
)
240 if (pin
->which
== PINENTRY_SAVE
&& which
!= 2) {
241 rc
= pinentry_command(pin
, NULL
, "SETQUALITYBAR");
246 pin
->inquire_cb
= quality_cb
;
247 pin
->inquire_data
= pin
;
252 title
= g_strdup(N_("Passphrase mismatch, please try again."));
253 else if (!pin
->title
)
254 title
= pin
->title
= g_strdup(N_("Password Manager Daemon"));
259 pin
->prompt
= g_strdup(N_("Passphrase:"));
261 if (!pin
->desc
&& !which
)
262 pin
->desc
= g_strdup_printf(pin
->which
== PINENTRY_OPEN
?
263 N_("A passphrase is required to open the file \"%s\". Please%%0Aenter the passphrase below.") :
264 N_("A passphrase is required to save to the file \"%s\". Please%%0Aenter a passphrase below."),
268 buf
= g_strdup_printf("SETERROR %s", N_("Please type the passphrase again for confirmation."));
270 buf
= g_strdup_printf("SETERROR %s", pin
->desc
);
272 rc
= pinentry_command(pin
, NULL
, buf
);
278 buf
= g_strdup_printf("SETPROMPT %s", pin
->prompt
);
279 rc
= pinentry_command(pin
, NULL
, buf
);
285 buf
= g_strdup_printf("SETDESC %s", title
);
286 rc
= pinentry_command(pin
, NULL
, buf
);
296 static void pinentry_disconnect(struct pinentry_s
*pin
)
302 assuan_disconnect(pin
->ctx
);
308 static gpg_error_t
do_getpin(struct pinentry_s
*pin
, char **result
)
313 rc
= pinentry_command(pin
, result
, "GETPIN");
316 *result
= xstrdup("");
321 gpg_error_t
pinentry_getpin(struct pinentry_s
*pin
, gchar
**result
)
324 gpg_error_t rc
= set_pinentry_strings(pin
, which
);
325 gchar
*result1
= NULL
;
331 rc
= do_getpin(pin
, result
);
336 if (pin
->which
== PINENTRY_SAVE
) {
338 rc
= set_pinentry_strings(pin
, 2);
343 result1
= g_strdup(*result
);
347 if (strcmp(result1
, *result
)) {
350 result1
= *result
= NULL
;
351 rc
= set_pinentry_strings(pin
, 1);
362 pinentry_disconnect(pin
);
366 static int write_result(int fd
, pinentry_key_s
*pk
, char *result
)
372 * libassuan handles GPG_ERR_EOF in assuan_process_done() and
373 * will disconnect the client even if the error isn't related
374 * to it. Use GPG_ERR_CANCELED instead.
376 if (gpg_err_code(pk
->error
) == GPG_ERR_EOF
)
377 pk
->error
= GPG_ERR_CANCELED
;
379 len
= pth_write(fd
, pk
, sizeof(pinentry_key_s
));
382 if (len
!= sizeof(pinentry_key_s
))
383 log_write("%s(%i): write: len != sizeof(pk)", __FUNCTION__
, __LINE__
);
388 if (pk
->status
== PINENTRY_PID
)
389 pk
->what
.pid
= atoi(result
);
391 g_strlcpy(pk
->what
.key
, result
, sizeof(pk
->what
.key
));
394 len
= pth_write(fd
, pk
, sizeof(pinentry_key_s
));
396 if (len
!= sizeof(pinentry_key_s
)) {
397 memset(pk
, 0, sizeof(pinentry_key_s
));
398 log_write("%s(%i): write: len != sizeof(pk)", __FUNCTION__
, __LINE__
);
403 if (pk
->status
!= PINENTRY_PID
)
406 memset(pk
, 0, sizeof(pinentry_key_s
));
410 static void reset(struct pinentry_s
*pin
)
415 pth_waitpid(pin
->pid
, &status
, 0);
421 pin
->pid
= pin
->pin_pid
= 0;
425 pth_event_isolate(pin
->ev
);
426 pth_event_free(pin
->ev
, PTH_FREE_THIS
);
430 pin
->status
= PINENTRY_NONE
;
433 static void *timeout_thread(void *arg
)
435 struct pinentry_s
*pin
= arg
;
436 pth_event_t ev
= pth_event(PTH_EVENT_TIME
, pth_timeout(pin
->timeout
, 0));
439 pth_event_free(ev
, PTH_FREE_THIS
);
440 pth_mutex_acquire(&pin
->status_mutex
, FALSE
, NULL
);
442 /* pth_cancel() was called from pinentry_iterate() (we have a key).
443 * pth_wait() was the cancelation point. */
444 if (pin
->status
== PINENTRY_NONE
) {
445 pth_mutex_release(&pin
->status_mutex
);
446 pth_exit(PTH_CANCELED
);
450 if (kill(pin
->pin_pid
, 0) == 0)
451 if (kill(pin
->pin_pid
, SIGTERM
) == 0)
452 if (kill(pin
->pin_pid
, 0) == 0)
453 kill(pin
->pin_pid
, SIGKILL
);
455 pin
->status
= PINENTRY_TIMEOUT
;
456 pth_mutex_release(&pin
->status_mutex
);
457 pth_exit(PTH_CANCELED
);
461 gpg_error_t
pinentry_fork(assuan_context_t ctx
)
463 struct client_s
*client
= assuan_get_pointer(ctx
);
464 struct pinentry_s
*pin
= client
->pinentry
;
469 gchar
*result
= NULL
;
472 return gpg_error_from_syserror();
478 rc
= gpg_error_from_syserror();
486 if (pin
->timeout
> 0 && client
->pinentry
->which
== PINENTRY_OPEN
) {
488 * Send the pid of the pinentry process back to pwmd so it can
489 * handle the pinentry timeout properly.
491 pk
.status
= PINENTRY_PID
;
492 pk
.error
= pinentry_command(pin
, &result
, "GETINFO pid");
494 if (write_result(p
[1], &pk
, result
))
498 pk
.status
= PINENTRY_RUNNING
;
499 pk
.error
= pinentry_getpin(pin
, &result
);
501 if (write_result(p
[1], &pk
, result
))
507 client
->pinentry
->fd
= p
[0];
508 client
->pinentry
->pid
= pid
;
509 client
->pinentry
->status
= PINENTRY_INIT
;
514 * Don't call assuan_process_done() here. That should be done in
515 * open_command_finalize() after the key has been read().
520 gpg_error_t
lock_pin_mutex(struct client_s
*client
)
522 while (pth_mutex_acquire(&pin_mutex
, TRUE
, NULL
) == FALSE
) {
524 gpg_error_t rc
= send_status(client
->ctx
, STATUS_LOCKED
);
533 client
->pinentry
->has_lock
= TRUE
;
537 void unlock_pin_mutex(struct pinentry_s
*pin
)
539 if (pin
->has_lock
== FALSE
)
542 pth_mutex_release(&pin_mutex
);
543 pin
->has_lock
= FALSE
;
546 void cleanup_pinentry(struct pinentry_s
*pin
)
551 unlock_pin_mutex(pin
);
553 if (pin
->ctx
&& pin
->pid
)
554 pinentry_disconnect(pin
);
556 g_free(pin
->ttyname
);
557 g_free(pin
->ttytype
);
562 g_free(pin
->display
);
563 g_free(pin
->filename
);
564 g_free(pin
->lcctype
);
565 g_free(pin
->lcmessages
);
569 void set_pinentry_defaults(struct pinentry_s
*pin
)
573 struct passwd
*pw
= getpwuid(getuid());
577 g_snprintf(buf
, sizeof(buf
), "%s/.pwmd/pinentry.conf", pw
->pw_dir
);
578 fp
= fopen(buf
, "r");
581 while ((p
= fgets(buf
, sizeof(buf
), fp
)) != NULL
) {
582 gchar name
[32] = {0}, value
[256] = {0};
587 if (p
[strlen(p
)-1] == '\n')
590 if (sscanf(p
, " %31[a-zA-Z] = %255s", name
, value
) != 2)
593 if (g_strcasecmp("TTYNAME", name
) == 0)
594 pin
->ttyname
= g_strdup(value
);
595 else if (g_strcasecmp("TTYTYPE", name
) == 0)
596 pin
->ttytype
= g_strdup(value
);
597 else if (g_strcasecmp("DISPLAY", name
) == 0)
598 pin
->display
= g_strdup(value
);
599 else if (g_strcasecmp("PATH", name
) == 0)
600 pin
->path
= g_strdup(value
);
601 else if (g_strcasecmp("LC_TYPE", name
) == 0)
602 pin
->lcctype
= g_strdup(value
);
603 else if (g_strcasecmp("LC_MESSAGES", name
) == 0)
604 pin
->lcmessages
= g_strdup(value
);
610 pin
->timeout
= get_key_file_integer("global", "pinentry_timeout");
613 pth_event_t
pinentry_iterate(struct client_s
*cl
, pth_event_t ev
)
617 pth_mutex_acquire(&cl
->pinentry
->status_mutex
, FALSE
, NULL
);
619 /* Set from pinentry_timeout_thread(). */
620 if (cl
->pinentry
->status
== PINENTRY_TIMEOUT
) {
621 cl
->pinentry
->status
= PINENTRY_NONE
;
622 rc
= send_error(cl
->ctx
, GPG_ERR_TIMEOUT
);
625 unlock_pin_mutex(cl
->pinentry
);
628 if (cl
->pinentry
->status
== PINENTRY_RUNNING
) {
629 pth_event_isolate(cl
->pinentry
->ev
);
631 if (pth_event_occurred(cl
->pinentry
->ev
)) {
632 guchar shakey
[gcrykeysize
];
636 memset(&pk
, 0, sizeof(pk
));
637 len
= pth_read(cl
->pinentry
->fd
, &pk
, sizeof(pk
));
639 if (len
== sizeof(pk
)) {
641 if (cl
->pinentry
->status
== PINENTRY_TIMEOUT
)
642 pk
.error
= GPG_ERR_TIMEOUT
;
644 rc
= send_error(cl
->ctx
, pk
.error
);
646 else if (pk
.status
== PINENTRY_PID
) {
648 * Start the timeout thread for the pinentry process
649 * now that we know the pid of it.
651 pth_attr_t attr
= pth_attr_new();
653 pth_attr_set(attr
, PTH_ATTR_JOINABLE
, 0);
654 pth_attr_set(attr
, PTH_ATTR_CANCEL_STATE
,
655 PTH_CANCEL_ASYNCHRONOUS
);
656 cl
->pinentry
->pin_pid
= pk
.what
.pid
;
657 cl
->pinentry
->tid
= pth_spawn(attr
, timeout_thread
,
659 pth_attr_destroy(attr
);
662 if (cl
->pinentry
->tid
) {
663 cl
->pinentry
->status
= PINENTRY_NONE
;
664 pth_cancel(cl
->pinentry
->tid
);
667 gcry_md_hash_buffer(GCRY_MD_SHA256
, shakey
, pk
.what
.key
,
668 strlen(pk
.what
.key
) == 0 ? 1 : strlen(pk
.what
.key
));
669 rc
= cl
->pinentry
->cb(cl
->ctx
, shakey
, FALSE
);
670 memset(shakey
, 0, sizeof(shakey
));
673 else if (len
== -1) {
674 if (cl
->pinentry
->status
== PINENTRY_TIMEOUT
)
675 pk
.error
= GPG_ERR_TIMEOUT
;
677 pk
.error
= gpg_err_code_from_syserror();
678 log_write("%s", _gpg_strerror(pk
.error
));
681 if (cl
->pinentry
->status
== PINENTRY_TIMEOUT
)
682 pk
.error
= GPG_ERR_TIMEOUT
;
684 pk
.error
= GPG_ERR_EOF
;
685 log_write("%s", _gpg_strerror(pk
.error
));
688 log_write(N_("pth_read(): short byte count"));
691 if (cl
->pinentry
->tid
) {
692 cl
->pinentry
->status
= PINENTRY_NONE
;
693 pth_cancel(cl
->pinentry
->tid
);
696 if (cl
->pinentry
->which
== PINENTRY_OPEN
)
700 if (pk
.error
|| pk
.status
== PINENTRY_RUNNING
) {
702 unlock_file_mutex(cl
);
703 unlock_pin_mutex(cl
->pinentry
);
706 memset(&pk
, 0, sizeof(pk
));
709 pth_event_concat(ev
, cl
->pinentry
->ev
, NULL
);
712 pth_mutex_release(&cl
->pinentry
->status_mutex
);
716 struct pinentry_s
*pinentry_init()
718 struct pinentry_s
*pin
= g_malloc0(sizeof(struct pinentry_s
));
723 pth_mutex_init(&pin
->status_mutex
);
724 set_pinentry_defaults(pin
);