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 02111-1307 USA
33 #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
);
60 static gpg_error_t
assuan_command(struct pinentry_s
*pin
, gchar
**result
,
69 rc
= assuan_transact(pin
->ctx
, cmd
, mem_realloc_cb
, &data
, NULL
, NULL
,
80 mem_realloc_cb(&data
, "", 1);
81 *result
= (gchar
*)data
.buf
;
88 static gpg_error_t
set_pinentry_options(struct pinentry_s
*pin
)
90 gchar
*display
= getenv("DISPLAY");
91 gint have_display
= 0;
92 gchar
*tty
= NULL
, *ttytype
= NULL
;
96 gchar cmd
[ASSUAN_LINELENGTH
];
98 if (pin
->display
|| display
)
101 tty
= pin
->ttyname
? pin
->ttyname
: ttyname(STDOUT_FILENO
);
104 return GPG_ERR_CANCELED
;
107 if (!have_display
&& !tty
)
108 return GPG_ERR_CANCELED
;
111 gchar
*p
= getenv("TERM");
113 ttytype
= pin
->ttytype
? pin
->ttytype
: p
;
116 return GPG_ERR_CANCELED
;
119 opt
= have_display
? "DISPLAY" : "TTYNAME";
120 val
= have_display
? pin
->display
? pin
->display
: display
: tty
;
121 g_snprintf(cmd
, sizeof(cmd
), "OPTION %s=%s", g_ascii_strdown(opt
, strlen(opt
)), val
);
122 rc
= assuan_command(pin
, &result
, cmd
);
128 g_snprintf(cmd
, sizeof(cmd
), "OPTION ttytype=%s", ttytype
);
129 rc
= assuan_command(pin
, &result
, cmd
);
135 static gpg_error_t
launch_pinentry(struct pinentry_s
*pin
)
138 assuan_context_t ctx
;
139 gint child_list
[] = {-1};
140 const gchar
*argv
[4];
142 argv
[0] = "pinentry";
145 argv
[1] = "--display";
146 argv
[2] = pin
->display
;
152 rc
= assuan_pipe_connect(&ctx
, pin
->path
? pin
->path
: PINENTRY_PATH
,
158 pin
->pid
= assuan_get_pid(ctx
);
160 rc
= set_pinentry_options(pin
);
161 return rc
? rc
: set_pinentry_strings(pin
, 0);
164 static gpg_error_t
pinentry_command(struct pinentry_s
*pin
, gchar
**result
,
170 rc
= launch_pinentry(pin
);
172 return rc
? rc
: assuan_command(pin
, result
, cmd
);
175 static gpg_error_t
set_pinentry_strings(struct pinentry_s
*pin
, int which
)
182 title
= g_strdup(N_("Password mismatch, please try again."));
183 else if (!pin
->title
)
184 title
= pin
->title
= g_strdup(N_("Password Manager Daemon"));
189 pin
->prompt
= g_strdup(N_("Password:"));
191 if (!pin
->desc
&& !which
)
192 pin
->desc
= g_strdup_printf(pin
->which
== PINENTRY_OPEN
?
193 N_("A password is required to open the file \"%s\". Please%%0Aenter the password below.") :
194 N_("A password is required to save to the file \"%s\". Please%%0Aenter the password below."),
198 buf
= g_strdup_printf("SETERROR %s", N_("Please type the password again for confirmation."));
200 buf
= g_strdup_printf("SETERROR %s", pin
->desc
);
202 rc
= pinentry_command(pin
, NULL
, buf
);
208 buf
= g_strdup_printf("SETPROMPT %s", pin
->prompt
);
209 rc
= pinentry_command(pin
, NULL
, buf
);
215 buf
= g_strdup_printf("SETDESC %s", title
);
216 rc
= pinentry_command(pin
, NULL
, buf
);
226 static void pinentry_disconnect(struct pinentry_s
*pin
)
232 assuan_disconnect(pin
->ctx
);
238 static gpg_error_t
do_getpin(struct pinentry_s
*pin
, char **result
)
243 rc
= pinentry_command(pin
, result
, "GETPIN");
246 *result
= xstrdup("");
251 gpg_error_t
pinentry_getpin(struct pinentry_s
*pin
, gchar
**result
)
254 gpg_error_t rc
= set_pinentry_strings(pin
, which
);
255 gchar
*result1
= NULL
;
258 pinentry_disconnect(pin
);
263 rc
= do_getpin(pin
, result
);
268 if (pin
->which
== PINENTRY_SAVE
) {
270 rc
= set_pinentry_strings(pin
, 2);
275 result1
= g_strdup(*result
);
279 if (strcmp(result1
, *result
)) {
282 result1
= *result
= NULL
;
283 rc
= set_pinentry_strings(pin
, 1);
294 pinentry_disconnect(pin
);
298 static int write_result(int fd
, pinentry_key_s
*pk
, char *result
)
304 * libassuan handles GPG_ERR_EOF in assuan_process_done() and
305 * will disconnect the client even if the error isn't related
306 * to it. Use GPG_ERR_CANCELED instead.
308 if (gpg_err_code(pk
->error
) == GPG_ERR_EOF
)
309 pk
->error
= GPG_ERR_CANCELED
;
311 len
= pth_write(fd
, pk
, sizeof(pinentry_key_s
));
314 if (len
!= sizeof(pinentry_key_s
))
315 log_write("%s(%i): write: len != sizeof(pk)", __FUNCTION__
, __LINE__
);
320 if (pk
->status
== PINENTRY_PID
)
321 pk
->what
.pid
= atoi(result
);
323 g_strlcpy(pk
->what
.key
, result
, sizeof(pk
->what
.key
));
326 len
= pth_write(fd
, pk
, sizeof(pinentry_key_s
));
328 if (len
!= sizeof(pinentry_key_s
)) {
329 memset(pk
, 0, sizeof(pinentry_key_s
));
330 log_write("%s(%i): write: len != sizeof(pk)", __FUNCTION__
, __LINE__
);
335 if (pk
->status
!= PINENTRY_PID
)
338 memset(pk
, 0, sizeof(pinentry_key_s
));
342 static void reset(struct pinentry_s
*pin
)
346 pth_waitpid(pin
->pid
, &status
, 0);
349 pin
->pid
= pin
->pin_pid
= 0;
351 pth_event_isolate(pin
->ev
);
352 pth_event_free(pin
->ev
, PTH_FREE_THIS
);
354 pin
->status
= PINENTRY_NONE
;
357 void *pinentry_timeout_thread(void *arg
)
359 struct pinentry_s
*pin
= arg
;
360 pth_event_t ev
= pth_event(PTH_EVENT_TIME
, pth_timeout(pin
->timeout
, 0));
363 pth_event_free(ev
, PTH_FREE_THIS
);
366 * Don't give the forked process a chance to return a read error from the
367 * pinentry process. Kill the forked process first, then the pinentry
370 if (kill(pin
->pid
, 0) == 0)
371 if (kill(pin
->pid
, SIGTERM
) == 0)
372 if (kill(pin
->pid
, 0) == 0)
373 kill(pin
->pid
, SIGKILL
);
375 if (kill(pin
->pin_pid
, 0) == 0)
376 if (kill(pin
->pin_pid
, SIGTERM
) == 0)
377 if (kill(pin
->pin_pid
, 0) == 0)
378 kill(pin
->pin_pid
, SIGKILL
);
381 pin
->status
= PINENTRY_TIMEOUT
;
382 pth_exit(PTH_CANCELED
);
386 gpg_error_t
pinentry_fork(assuan_context_t ctx
)
388 struct client_s
*client
= assuan_get_pointer(ctx
);
389 struct pinentry_s
*pin
= client
->pinentry
;
395 gchar
*result
= NULL
;
398 return gpg_error_from_syserror();
404 rc
= gpg_error_from_syserror();
412 if (pin
->timeout
> 0) {
414 * Send the pid of the pinentry process back to pwmd so it can
415 * handle the pinentry timeout properly.
417 pk
.status
= PINENTRY_PID
;
418 pk
.error
= pinentry_command(pin
, &result
, "GETINFO pid");
421 * Support older pinentry's (< 0.7.5).
423 if (pk
.error
&& gpg_err_code(pk
.error
) == GPG_ERR_ASS_UNKNOWN_CMD
)
426 if (write_result(p
[1], &pk
, result
))
431 pk
.status
= PINENTRY_RUNNING
;
432 pk
.error
= pinentry_getpin(pin
, &result
);
434 if (write_result(p
[1], &pk
, result
))
440 client
->pinentry
->fd
= p
[0];
441 client
->pinentry
->pid
= pid
;
442 client
->pinentry
->status
= PINENTRY_INIT
;
447 * Don't call assuan_process_done() here. That should be done in
448 * open_command_finalize() after the key has been read().
453 gpg_error_t
lock_pin_mutex(struct client_s
*client
)
455 while (pth_mutex_acquire(&pin_mutex
, TRUE
, NULL
) == FALSE
) {
457 gpg_error_t rc
= send_status(client
->ctx
, STATUS_LOCKED
);
466 client
->pinentry
->has_lock
= TRUE
;
470 void unlock_pin_mutex(struct pinentry_s
*pin
)
472 if (pin
->has_lock
== FALSE
)
475 pth_mutex_release(&pin_mutex
);
476 pin
->has_lock
= FALSE
;
479 void cleanup_pinentry(struct pinentry_s
*pin
)
484 unlock_pin_mutex(pin
);
486 if (pin
->ctx
&& pin
->pid
)
487 pinentry_disconnect(pin
);
489 g_free(pin
->ttyname
);
490 g_free(pin
->ttytype
);
495 g_free(pin
->display
);
496 g_free(pin
->filename
);
500 static void set_pinentry_defaults(struct pinentry_s
*pin
)
504 struct passwd
*pw
= getpwuid(getuid());
508 g_snprintf(buf
, sizeof(buf
), "%s/.pwmd/pinentry.conf", pw
->pw_dir
);
509 fp
= fopen(buf
, "r");
512 while ((p
= fgets(buf
, sizeof(buf
), fp
)) != NULL
) {
513 gchar name
[32] = {0}, value
[256] = {0};
518 if (p
[strlen(p
)-1] == '\n')
521 if (sscanf(p
, " %31[a-zA-Z] = %255s", name
, value
) != 2)
524 if (g_strcasecmp("TTYNAME", name
) == 0)
525 pin
->ttyname
= g_strdup(value
);
526 else if (g_strcasecmp("TTYTYPE", name
) == 0)
527 pin
->ttytype
= g_strdup(value
);
528 else if (g_strcasecmp("DISPLAY", name
) == 0)
529 pin
->display
= g_strdup(value
);
530 else if (g_strcasecmp("PATH", name
) == 0)
531 pin
->path
= g_strdup(value
);
537 pin
->timeout
= get_key_file_integer("global", "pinentry_timeout");
540 pth_event_t
pinentry_iterate(struct client_s
*cl
, pth_event_t ev
)
544 /* Set from pinentry_timeout_thread(). */
545 if (cl
->pinentry
->status
== PINENTRY_TIMEOUT
) {
546 rc
= send_error(cl
->ctx
, GPG_ERR_TIMEOUT
);
548 if (cl
->pinentry
->which
== PINENTRY_OPEN
)
552 unlock_pin_mutex(cl
->pinentry
);
555 if (cl
->pinentry
->status
== PINENTRY_RUNNING
) {
556 pth_event_isolate(cl
->pinentry
->ev
);
558 if (pth_event_occurred(cl
->pinentry
->ev
)) {
559 guchar shakey
[gcrykeysize
];
564 memset(&pk
, 0, sizeof(pk
));
565 len
= pth_read(cl
->pinentry
->fd
, &pk
, sizeof(pk
));
567 if (len
== sizeof(pk
)) {
569 rc
= send_error(cl
->ctx
, pk
.error
);
570 else if (pk
.status
== PINENTRY_PID
) {
572 * Start the timeout thread for the pinentry process
573 * now that we know the pid of it.
575 pth_attr_t attr
= pth_attr_new();
578 pth_attr_set(attr
, PTH_ATTR_JOINABLE
, 0);
579 pth_attr_set(attr
, PTH_ATTR_CANCEL_STATE
,
580 PTH_CANCEL_ASYNCHRONOUS
);
581 cl
->pinentry
->pin_pid
= pk
.what
.pid
;
582 cl
->pinentry
->tid
= pth_spawn(attr
,
583 pinentry_timeout_thread
, cl
->pinentry
);
586 gcry_md_hash_buffer(GCRY_MD_SHA256
, shakey
, pk
.what
.key
,
587 strlen(pk
.what
.key
) == 0 ? 1 : strlen(pk
.what
.key
));
589 if (cl
->pinentry
->tid
)
590 pth_cancel(cl
->pinentry
->tid
);
592 rc
= cl
->pinentry
->cb(cl
->ctx
, shakey
, FALSE
);
593 memset(shakey
, 0, sizeof(shakey
));
596 else if (len
== -1) {
597 pk
.error
= gpg_err_code_from_syserror();
598 log_write("%s", gpg_strerror(pk
.error
));
601 pk
.error
= GPG_ERR_EOF
;
602 log_write("%s", gpg_strerror(pk
.error
));
605 log_write(N_("pth_read(): short byte count"));
608 if (cl
->pinentry
->tid
) {
609 pth_cancel(cl
->pinentry
->tid
);
610 cl
->pinentry
->status
= PINENTRY_NONE
;
615 if (cl
->pinentry
->which
== PINENTRY_OPEN
)
618 else if (pk
.status
== PINENTRY_RUNNING
)
621 if (pk
.error
|| pk
.status
== PINENTRY_RUNNING
) {
622 unlock_file_mutex(cl
);
623 unlock_pin_mutex(cl
->pinentry
);
626 memset(&pk
, 0, sizeof(pk
));
629 pth_event_concat(ev
, cl
->pinentry
->ev
, NULL
);
635 struct pinentry_s
*pinentry_init()
637 struct pinentry_s
*pin
= g_malloc0(sizeof(struct pinentry_s
));
642 set_pinentry_defaults(pin
);