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
26 #include <sys/types.h>
34 #include <sys/types.h>
48 #include "pwmd_error.h"
51 void free_client_list();
52 static gpg_error_t
set_pinentry_strings(struct pinentry_s
*pin
, int which
);
54 static int mem_realloc_cb(void *data
, const void *buffer
, size_t len
)
56 membuf_t
*mem
= (membuf_t
*)data
;
62 if ((p
= xrealloc(mem
->buf
, mem
->len
+ len
)) == NULL
)
66 memcpy((char *)mem
->buf
+ mem
->len
, buffer
, len
);
72 static int quality_cb(void *data
, const char *line
)
74 struct pinentry_s
*pin
= data
;
79 if (strncmp(line
, "QUALITY ", 8) != 0)
80 return GPG_ERR_INV_ARG
;
82 if (!(tmp
= FascistCheck(line
+8, CRACKLIB_DICT
)))
83 return assuan_send_data(pin
->ctx
, "100", 3);
85 if (!strcmp(tmp
, "it's WAY too short"))
87 else if (!strcmp(tmp
, "it is too short"))
89 else if (!strcmp(tmp
, "it is all whitespace"))
91 else if (!strcmp(tmp
, "it is based on your username"))
93 else if (!strcmp(tmp
, "it is based on a dictionary word"))
95 else if (!strcmp(tmp
, "it is based upon your password entry"))
97 else if (!strcmp(tmp
, "it's derived from your password entry"))
99 else if (!strcmp(tmp
, "it is based on a (reversed) dictionary word"))
101 else if (!strcmp(tmp
, "it is derivable from your password entry"))
103 else if (!strcmp(tmp
, "it does not contain enough DIFFERENT characters"))
105 else if (!strcmp(tmp
, "it is too simplistic/systematic"))
110 tmp
= (const gchar
*)print_fmt(buf
, sizeof(buf
), "%i", score
);
111 return assuan_send_data(pin
->ctx
, tmp
, strlen(tmp
));
115 static gpg_error_t
assuan_command(struct pinentry_s
*pin
, gchar
**result
,
121 pin
->data
.buf
= NULL
;
123 rc
= assuan_transact(pin
->ctx
, cmd
, mem_realloc_cb
, &pin
->data
,
124 pin
->inquire_cb
, pin
->inquire_data
, NULL
, NULL
);
128 xfree(pin
->data
.buf
);
129 pin
->data
.buf
= NULL
;
134 mem_realloc_cb(&pin
->data
, "", 1);
135 *result
= (gchar
*)pin
->data
.buf
;
142 static gpg_error_t
set_pinentry_options(struct pinentry_s
*pin
)
144 gchar
*display
= getenv("DISPLAY");
145 gint have_display
= 0;
146 gchar
*tty
= NULL
, *ttytype
= NULL
;
149 gchar
*result
= NULL
;
150 gchar cmd
[ASSUAN_LINELENGTH
];
152 if (pin
->display
|| display
)
155 tty
= pin
->ttyname
? pin
->ttyname
: ttyname(STDOUT_FILENO
);
158 return GPG_ERR_CANCELED
;
161 if (!have_display
&& !tty
)
162 return GPG_ERR_CANCELED
;
165 gchar
*p
= getenv("TERM");
167 ttytype
= pin
->ttytype
? pin
->ttytype
: p
;
170 return GPG_ERR_CANCELED
;
173 opt
= have_display
? "DISPLAY" : "TTYNAME";
174 val
= have_display
? pin
->display
? pin
->display
: display
: tty
;
175 g_snprintf(cmd
, sizeof(cmd
), "OPTION %s=%s", g_ascii_strdown(opt
, strlen(opt
)), val
);
176 rc
= assuan_command(pin
, &result
, cmd
);
182 g_snprintf(cmd
, sizeof(cmd
), "OPTION ttytype=%s", ttytype
);
183 rc
= assuan_command(pin
, &result
, cmd
);
189 static gpg_error_t
launch_pinentry(struct pinentry_s
*pin
)
192 assuan_context_t ctx
;
193 gint child_list
[] = {-1};
194 const gchar
*argv
[8];
195 const gchar
**p
= argv
;
209 if (pin
->lcmessages
) {
210 *p
++ = "--lc-messages";
211 *p
++ = pin
->lcmessages
;
215 rc
= assuan_pipe_connect(&ctx
, pin
->path
? pin
->path
: PINENTRY_PATH
,
221 pin
->pid
= assuan_get_pid(ctx
);
223 rc
= set_pinentry_options(pin
);
224 return rc
? rc
: set_pinentry_strings(pin
, 0);
227 static gpg_error_t
pinentry_command(struct pinentry_s
*pin
, gchar
**result
,
233 rc
= launch_pinentry(pin
);
235 return rc
? rc
: assuan_command(pin
, result
, cmd
);
238 static gpg_error_t
set_pinentry_strings(struct pinentry_s
*pin
, int which
)
245 if (pin
->which
== PINENTRY_SAVE
&& which
!= 2) {
246 rc
= pinentry_command(pin
, NULL
, "SETQUALITYBAR");
251 pin
->inquire_cb
= quality_cb
;
252 pin
->inquire_data
= pin
;
257 title
= g_strdup(N_("Passphrase mismatch, please try again."));
258 else if (!pin
->title
)
259 title
= pin
->title
= g_strdup_printf(N_("Password Manager Daemon%s%s"),
260 pin
->name
? ": " : "", pin
->name
? pin
->name
: "");
265 pin
->prompt
= g_strdup(N_("Passphrase:"));
267 if (!pin
->desc
&& !which
)
268 pin
->desc
= g_strdup_printf(pin
->which
== PINENTRY_OPEN
?
269 N_("A passphrase is required to open the file \"%s\". Please%%0Aenter the passphrase below.") :
270 N_("A passphrase is required to save to the file \"%s\". Please%%0Aenter the passphrase below."),
274 buf
= g_strdup_printf("SETERROR %s", N_("Please enter the passphrase again for confirmation."));
276 buf
= g_strdup_printf("SETERROR %s", pin
->desc
);
278 rc
= pinentry_command(pin
, NULL
, buf
);
284 buf
= g_strdup_printf("SETPROMPT %s", pin
->prompt
);
285 rc
= pinentry_command(pin
, NULL
, buf
);
291 buf
= g_strdup_printf("SETDESC %s", title
);
292 rc
= pinentry_command(pin
, NULL
, buf
);
302 static void pinentry_disconnect(struct pinentry_s
*pin
)
308 assuan_disconnect(pin
->ctx
);
314 static gpg_error_t
do_getpin(struct pinentry_s
*pin
, char **result
)
319 rc
= pinentry_command(pin
, result
, "GETPIN");
322 *result
= xstrdup("");
327 gpg_error_t
pinentry_getpin(struct pinentry_s
*pin
, gchar
**result
)
330 gpg_error_t rc
= set_pinentry_strings(pin
, which
);
331 gchar
*result1
= NULL
;
337 rc
= do_getpin(pin
, result
);
342 if (pin
->which
== PINENTRY_SAVE
) {
344 rc
= set_pinentry_strings(pin
, 2);
349 result1
= g_strdup(*result
);
353 if (strcmp(result1
, *result
)) {
356 result1
= *result
= NULL
;
357 rc
= set_pinentry_strings(pin
, 1);
368 pinentry_disconnect(pin
);
372 static int write_result(int fd
, pinentry_key_s
*pk
, char *result
)
378 * libassuan handles GPG_ERR_EOF in assuan_process_done() and
379 * will disconnect the client even if the error isn't related
380 * to it. Use GPG_ERR_CANCELED instead.
382 if (gpg_err_code(pk
->error
) == GPG_ERR_EOF
)
383 pk
->error
= GPG_ERR_CANCELED
;
385 len
= pth_write(fd
, pk
, sizeof(pinentry_key_s
));
388 if (len
!= sizeof(pinentry_key_s
))
389 log_write("%s(%i): write: len != sizeof(pk)", __FUNCTION__
, __LINE__
);
394 if (pk
->status
== PINENTRY_PID
)
395 pk
->what
.pid
= atoi(result
);
397 g_strlcpy(pk
->what
.key
, result
, sizeof(pk
->what
.key
));
400 len
= pth_write(fd
, pk
, sizeof(pinentry_key_s
));
402 if (len
!= sizeof(pinentry_key_s
)) {
403 memset(pk
, 0, sizeof(pinentry_key_s
));
404 log_write("%s(%i): write: len != sizeof(pk)", __FUNCTION__
, __LINE__
);
409 if (pk
->status
!= PINENTRY_PID
)
412 memset(pk
, 0, sizeof(pinentry_key_s
));
416 static void reset(struct pinentry_s
*pin
)
421 waitpid(pin
->pid
, &status
, 0);
427 pin
->pid
= pin
->pin_pid
= 0;
429 pin
->status
= PINENTRY_NONE
;
432 /* pin->status_mutex should be locked before calling this function. */
433 static void kill_pinentry(struct pinentry_s
*pin
)
435 if (pin
->pin_pid
== 0)
438 if (kill(pin
->pin_pid
, 0) == 0)
439 if (kill(pin
->pin_pid
, SIGTERM
) == 0)
440 if (kill(pin
->pin_pid
, 0) == 0)
441 kill(pin
->pin_pid
, SIGKILL
);
446 static void timeout_cleanup(void *arg
)
448 pth_event_t ev
= arg
;
450 pth_event_free(ev
, PTH_FREE_ALL
);
453 static void *timeout_thread(void *arg
)
455 struct pinentry_s
*pin
= arg
;
457 pth_attr_t attr
= pth_attr_of(pth_self());
459 pth_attr_set(attr
, PTH_ATTR_NAME
, __FUNCTION__
);
460 pth_attr_destroy(attr
);
461 ev
= pth_event(PTH_EVENT_TIME
, pth_timeout(pin
->timeout
, 0));
462 MUTEX_LOCK(&pin
->cond_mutex
);
463 pth_cleanup_push(timeout_cleanup
, ev
);
464 pth_cond_await(&pin
->cond
, &pin
->cond_mutex
, ev
);
467 /* pth_cond_notify() was called from pinentry_iterate() (we have a
469 if (pin
->status
== PINENTRY_NONE
) {
470 MUTEX_UNLOCK(&pin
->cond_mutex
);
471 pth_exit(PTH_CANCELED
);
475 MUTEX_LOCK(&pin
->status_mutex
);
477 pin
->status
= PINENTRY_TIMEOUT
;
478 MUTEX_UNLOCK(&pin
->cond_mutex
);
479 MUTEX_UNLOCK(&pin
->status_mutex
);
480 pth_exit(PTH_CANCELED
);
484 gpg_error_t
pinentry_fork(assuan_context_t ctx
)
486 struct client_s
*client
= assuan_get_pointer(ctx
);
487 struct pinentry_s
*pin
= client
->pinentry
;
492 gchar
*result
= NULL
;
495 return gpg_error_from_syserror();
501 rc
= gpg_error_from_syserror();
507 /* FIXME segfaults in libpth at pth_ring_append() (xmalloc).
508 this is probably only a symptom of another problem. */
509 //free_client_list();
511 if (pin
->timeout
> 0 && pin
->which
== PINENTRY_OPEN
) {
513 * Send the pid of the pinentry process back to pwmd so it can
514 * handle the pinentry timeout properly.
516 pk
.status
= PINENTRY_PID
;
517 pk
.error
= pinentry_command(pin
, &result
, "GETINFO pid");
519 if (write_result(p
[1], &pk
, result
))
523 pk
.status
= PINENTRY_RUNNING
;
524 pk
.error
= pinentry_getpin(pin
, &result
);
526 if (write_result(p
[1], &pk
, result
))
532 client
->pinentry
->fd
= p
[0];
533 client
->pinentry
->pid
= pid
;
534 client
->pinentry
->status
= PINENTRY_INIT
;
539 * Don't call assuan_process_done() here. That should be done in
540 * the callback function which is called after the key has been read()
541 * in pinentry_iterate().
546 gpg_error_t
lock_pin_mutex(struct client_s
*client
)
550 MUTEX_TRYLOCK(client
->ctx
, &pin_mutex
, rc
);
553 client
->pinentry
->has_lock
= TRUE
;
558 void unlock_pin_mutex(struct pinentry_s
*pin
)
560 if (pin
->has_lock
== FALSE
)
563 MUTEX_UNLOCK(&pin_mutex
);
564 pin
->has_lock
= FALSE
;
567 void cleanup_pinentry(struct pinentry_s
*pin
)
572 if (pin
->ctx
&& pin
->pid
)
573 pinentry_disconnect(pin
);
575 if (!pth_mutex_acquire(&pin
->status_mutex
, TRUE
, NULL
) && errno
== EBUSY
) {
576 MUTEX_UNLOCK(&pin
->status_mutex
);
579 pth_mutex_release(&pin
->status_mutex
);
581 MUTEX_LOCK(&pin
->status_mutex
);
583 MUTEX_UNLOCK(&pin
->status_mutex
);
584 unlock_pin_mutex(pin
);
586 if (!pth_mutex_acquire(&pin
->cond_mutex
, TRUE
, NULL
) && errno
== EBUSY
) {
587 MUTEX_UNLOCK(&pin
->cond_mutex
);
590 pth_mutex_release(&pin
->cond_mutex
);
595 g_free(pin
->ttyname
);
596 g_free(pin
->ttytype
);
601 g_free(pin
->display
);
602 g_free(pin
->filename
);
603 g_free(pin
->lcctype
);
604 g_free(pin
->lcmessages
);
608 void set_pinentry_defaults(struct pinentry_s
*pin
)
615 g_snprintf(buf
, sizeof(buf
), "%s/.pwmd/pinentry.conf", g_get_home_dir());
616 fp
= fopen(buf
, "r");
619 while ((p
= fgets(buf
, sizeof(buf
), fp
)) != NULL
) {
620 gchar name
[32] = {0}, value
[256] = {0};
625 if (p
[strlen(p
)-1] == '\n')
628 if (sscanf(p
, " %31[a-zA-Z] = %255s", name
, value
) != 2)
631 if (g_strcasecmp("TTYNAME", name
) == 0)
632 pin
->ttyname
= g_strdup(value
);
633 else if (g_strcasecmp("TTYTYPE", name
) == 0)
634 pin
->ttytype
= g_strdup(value
);
635 else if (g_strcasecmp("DISPLAY", name
) == 0)
636 pin
->display
= g_strdup(value
);
637 else if (g_strcasecmp("PATH", name
) == 0)
638 pin
->path
= g_strdup(value
);
639 else if (g_strcasecmp("LC_TYPE", name
) == 0)
640 pin
->lcctype
= g_strdup(value
);
641 else if (g_strcasecmp("LC_MESSAGES", name
) == 0)
642 pin
->lcmessages
= g_strdup(value
);
648 pin
->timeout
= get_key_file_integer("global", "pinentry_timeout");
651 int pinentry_iterate(struct client_s
*cl
, gboolean read_ready
)
655 MUTEX_LOCK(&cl
->pinentry
->status_mutex
);
657 /* Set from pinentry_timeout_thread(). */
658 if (cl
->pinentry
->status
== PINENTRY_TIMEOUT
) {
659 cl
->pinentry
->status
= PINENTRY_NONE
;
660 rc
= send_error(cl
->ctx
, GPG_ERR_TIMEOUT
);
663 unlock_pin_mutex(cl
->pinentry
);
664 MUTEX_UNLOCK(&cl
->pinentry
->status_mutex
);
667 if (cl
->pinentry
->status
== PINENTRY_RUNNING
) {
673 memset(&pk
, 0, sizeof(pk
));
674 len
= pth_read(cl
->pinentry
->fd
, &pk
, sizeof(pk
));
677 if (len
== sizeof(pk
)) {
679 if (cl
->pinentry
->status
== PINENTRY_TIMEOUT
)
680 pk
.error
= GPG_ERR_TIMEOUT
;
682 rc
= send_error(cl
->ctx
, pk
.error
);
684 else if (pk
.status
== PINENTRY_PID
) {
688 * Start the timeout thread for the pinentry process
689 * now that we know the pid of it.
691 pth_attr_t attr
= pth_attr_new();
693 pth_attr_set(attr
, PTH_ATTR_JOINABLE
, FALSE
);
694 cl
->pinentry
->pin_pid
= pk
.what
.pid
;
695 cl
->pinentry
->tid
= pth_spawn(attr
, timeout_thread
,
698 pth_attr_destroy(attr
);
700 if (!cl
->pinentry
->tid
) {
701 log_write("%s(%i): pth_spawn(): %s", __FILE__
, __LINE__
,
702 _gpg_strerror(gpg_error_from_errno(n
)));
703 pk
.error
= gpg_error_from_errno(n
);
707 if (cl
->pinentry
->tid
) {
708 cl
->pinentry
->status
= PINENTRY_NONE
;
709 pth_cond_notify(&cl
->pinentry
->cond
, FALSE
);
712 shakey
= gcry_malloc(gcrykeysize
);
715 pk
.error
= gpg_error_from_errno(ENOMEM
);
717 gcry_md_hash_buffer(GCRY_MD_SHA256
, shakey
,
719 strlen(pk
.what
.key
) == 0 ? 1 : strlen(pk
.what
.key
));
720 rc
= cl
->pinentry
->cb(cl
->ctx
, shakey
, FALSE
);
724 else if (len
== -1) {
725 if (cl
->pinentry
->status
== PINENTRY_TIMEOUT
)
726 pk
.error
= GPG_ERR_TIMEOUT
;
728 pk
.error
= gpg_err_code_from_syserror();
729 log_write("%s", _gpg_strerror(pk
.error
));
732 if (cl
->pinentry
->status
== PINENTRY_TIMEOUT
)
733 pk
.error
= GPG_ERR_TIMEOUT
;
735 pk
.error
= GPG_ERR_EOF
;
736 log_write("%s", _gpg_strerror(pk
.error
));
739 log_write(N_("read(): short byte count"));
742 if (cl
->pinentry
->tid
) {
743 cl
->pinentry
->status
= PINENTRY_NONE
;
744 pth_cond_notify(&cl
->pinentry
->cond
, FALSE
);
747 if (cl
->pinentry
->which
== PINENTRY_OPEN
)
751 if (pk
.error
|| pk
.status
== PINENTRY_RUNNING
) {
753 unlock_file_mutex(cl
);
754 unlock_pin_mutex(cl
->pinentry
);
757 memset(&pk
, 0, sizeof(pk
));
761 MUTEX_UNLOCK(&cl
->pinentry
->status_mutex
);
765 struct pinentry_s
*pinentry_init()
767 struct pinentry_s
*pin
= g_malloc0(sizeof(struct pinentry_s
));
772 pth_mutex_init(&pin
->status_mutex
);
773 pth_mutex_init(&pin
->cond_mutex
);
774 pth_cond_init(&pin
->cond
);
775 set_pinentry_defaults(pin
);