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/>.
31 #include <sys/types.h>
32 #include <sys/socket.h>
42 #include <sys/select.h>
62 #if defined(WITH_SSH) || defined(WITH_GNUTLS)
63 #include <sys/types.h>
64 #include <sys/socket.h>
66 #include <netinet/in.h>
69 #define FINISH(rc) (gpg_err_source(rc) == GPG_ERR_SOURCE_UNKNOWN) \
79 hook_read (assuan_context_t ctx
, assuan_fd_t fd
, void *data
, size_t len
)
81 #if defined(WITH_SSH) || defined(WITH_GNUTLS)
82 pwm_t
*pwm
= assuan_get_pointer (ctx
);
85 if (pwm
&& pwm
->tcp
&& pwm
->tcp
->ssh
)
86 return read_hook_ssh (pwm
->tcp
->ssh
, fd
, data
, len
);
89 if (pwm
&& pwm
->tcp
&& pwm
->tcp
->tls
)
90 return read_hook_tls (pwm
->tcp
->tls
, fd
, data
, len
);
94 return read ((int) fd
, data
, len
);
98 hook_write (assuan_context_t ctx
, assuan_fd_t fd
, const void *data
,
102 #if defined(WITH_SSH) || defined(WITH_GNUTLS)
103 pwm_t
*pwm
= assuan_get_pointer (ctx
);
106 if (pwm
&& pwm
->tcp
&& pwm
->tcp
->ssh
)
107 return write_hook_ssh (pwm
->tcp
->ssh
, fd
, data
, len
);
110 if (pwm
&& pwm
->tcp
&& pwm
->tcp
->tls
)
111 return write_hook_tls (pwm
->tcp
->tls
, fd
, data
, len
);
115 /* libassuan cannot handle EAGAIN when doing writes. */
118 wrote
= write ((int) fd
, data
, len
);
119 if (wrote
== -1 && errno
== EAGAIN
)
122 while (wrote
== -1 && errno
== EAGAIN
);
128 hook_waitpid (assuan_context_t ctx
, pid_t pid
, int action
, int *status
,
131 return waitpid (pid
, status
, options
);
137 static int initialized
;
140 // May be called more than once.
141 gnutls_global_init ();
151 bindtextdomain ("libpwmd", LOCALEDIR
);
165 gnutls_global_deinit ();
170 _connect_finalize (pwm_t
* pwm
)
174 int n
= assuan_get_active_fds (pwm
->ctx
, 0, active
, N_ARRAY (active
));
177 return GPG_ERR_EBADFD
;
181 pwm
->pinentry_pid
= -1;
185 rc
= pwmd_command (pwm
, NULL
, NULL
, NULL
, NULL
, "OPTION NAME=%s",
192 connect_uds (pwm_t
* pwm
, const char *path
)
194 char *socketpath
= NULL
;
200 return GPG_ERR_INV_ARG
;
202 pwbuf
= _getpwuid (&pw
);
204 return gpg_error_from_syserror ();
207 socketpath
= pwmd_strdup_printf ("%s/.pwmd/socket", pw
.pw_dir
);
209 socketpath
= _expand_homedir ((char *) path
, &pw
);
213 return GPG_ERR_ENOMEM
;
215 rc
= assuan_socket_connect (pwm
->ctx
, socketpath
, ASSUAN_INVALID_FD
, 0);
216 pwmd_free (socketpath
);
217 return rc
? rc
: _connect_finalize (pwm
);
221 init_handle (pwm_t
* pwm
)
224 static struct assuan_malloc_hooks mhooks
= {
225 pwmd_malloc
, pwmd_realloc
, pwmd_free
227 static struct assuan_system_hooks shooks
= {
228 ASSUAN_SYSTEM_HOOKS_VERSION
,
236 NULL
, //sendmsg both are used for FD passing
244 rc
= assuan_new_ext (&pwm
->ctx
, GPG_ERR_SOURCE_DEFAULT
, &mhooks
, NULL
,
249 assuan_set_pointer (pwm
->ctx
, pwm
);
250 assuan_ctx_set_system_hooks (pwm
->ctx
, &shooks
);
254 #if defined(WITH_SSH) || defined(WITH_GNUTLS)
256 free_tcp (struct tcp_s
*tcp
)
262 _free_ssh_conn (tcp
->ssh
);
268 pwmd_free (tcp
->host
);
271 freeaddrinfo (tcp
->addrs
);
279 #if defined(WITH_SSH) || defined(WITH_GNUTLS)
281 tcp_connect_common (pwm_t
* pwm
)
283 struct addrinfo hints
= { 0 };
291 hints
.ai_family
= AF_UNSPEC
;
294 hints
.ai_family
= AF_INET
;
297 hints
.ai_family
= AF_INET6
;
301 hints
.ai_socktype
= SOCK_STREAM
;
302 snprintf (portstr
, sizeof (portstr
), "%i", pwm
->tcp
->port
);
303 n
= getaddrinfo (pwm
->tcp
->host
, portstr
, &hints
, &pwm
->tcp
->addrs
);
306 fprintf (stderr
, "%s\n", gai_strerror (n
));
307 return GPG_ERR_UNKNOWN_HOST
; //FIXME
310 for (pwm
->tcp
->addr
= pwm
->tcp
->addrs
; pwm
->tcp
->addr
;
311 pwm
->tcp
->addr
= pwm
->tcp
->addrs
->ai_next
)
313 pwm
->fd
= socket (pwm
->tcp
->addr
->ai_family
, SOCK_STREAM
, 0);
316 rc
= gpg_error_from_syserror ();
317 if (pwm
->tcp
->addr
== pwm
->tcp
->addrs
->ai_next
)
322 if (pwm
->socket_timeout
)
323 fcntl (pwm
->fd
, F_SETFL
, O_NONBLOCK
);
325 if (connect (pwm
->fd
, pwm
->tcp
->addr
->ai_addr
,
326 pwm
->tcp
->addr
->ai_family
== AF_INET6
327 ? sizeof (struct sockaddr_in6
)
328 : sizeof (struct sockaddr
)) == -1)
331 struct timeval tv
= { pwm
->socket_timeout
, 0 };
334 rc
= gpg_error_from_syserror ();
335 if (gpg_err_code (rc
) != GPG_ERR_EINPROGRESS
)
339 if (pwm
->tcp
->addr
== pwm
->tcp
->addrs
->ai_next
)
344 FD_SET (pwm
->fd
, &wfds
);
345 n
= select (pwm
->fd
+1, NULL
, &wfds
, NULL
, &tv
);
348 rc
= gpg_error (GPG_ERR_ETIMEDOUT
);
351 socklen_t len
= sizeof(int);
353 getsockopt (pwm
->fd
, SOL_SOCKET
, SO_ERROR
, &n
, &len
);
355 rc
= gpg_error_from_errno (n
);
358 rc
= gpg_error_from_syserror ();
364 if (pwm
->tcp
->addr
== pwm
->tcp
->addrs
->ai_next
365 || gpg_err_code (rc
) == GPG_ERR_ETIMEDOUT
)
375 if (!rc
&& pwm
->socket_timeout
)
376 fcntl (pwm
->fd
, F_SETFL
, 0);
383 pwmd_connect (pwm_t
* pwm
, const char *url
, ...)
389 return FINISH (GPG_ERR_INV_ARG
);
392 rc
= init_handle (pwm
);
397 rc
= GPG_ERR_UNSUPPORTED_PROTOCOL
;
400 rc
= connect_uds (pwm
, p
);
401 else if (!p
|| !strncmp (p
, "file://", 7))
405 #ifdef DEFAULT_PWMD_SOCKET
407 p
= DEFAULT_PWMD_SOCKET
;
409 rc
= connect_uds (pwm
, p
);
411 else if (!strncmp (p
, "ssh://", 6) || !strncmp (p
, "ssh6://", 7) ||
412 !strncmp (p
, "ssh4://", 7))
415 return FINISH (GPG_ERR_NOT_IMPLEMENTED
);
419 char *username
= NULL
;
421 if (!strncmp (p
, "ssh6://", 7))
423 pwm
->prot
= PWMD_IPV6
;
426 else if (!strncmp (p
, "ssh4://", 7))
428 pwm
->prot
= PWMD_IPV4
;
433 pwm
->prot
= PWMD_IP_ANY
;
437 rc
= _parse_ssh_url (p
, &host
, &port
, &username
);
441 char *identity
= NULL
;
442 char *knownhosts
= NULL
;
445 identity
= va_arg (ap
, char *);
447 if (!identity
&& !pwm
->use_agent
)
448 rc
= GPG_ERR_INV_ARG
;
450 knownhosts
= va_arg (ap
, char *);
455 rc
= _do_ssh_connect (pwm
, host
, port
, identity
, username
,
459 rc
= _connect_finalize (pwm
);
469 pwmd_free (username
);
473 else if (!strncmp (p
, "tls://", 6) || !strncmp (p
, "tls6://", 7) ||
474 !strncmp (p
, "tls4://", 7))
477 return FINISH (GPG_ERR_NOT_IMPLEMENTED
);
482 if (!strncmp (p
, "tls6://", 7))
484 pwm
->prot
= PWMD_IPV6
;
487 else if (!strncmp (p
, "tls4://", 7))
489 pwm
->prot
= PWMD_IPV4
;
494 pwm
->prot
= PWMD_IP_ANY
;
498 rc
= _parse_tls_url (p
, &host
, &port
);
502 char *clientcert
= NULL
;
503 char *clientkey
= NULL
;
506 char *server_fp
= NULL
;
509 clientcert
= va_arg (ap
, char *);
512 rc
= GPG_ERR_INV_ARG
;
515 clientkey
= va_arg (ap
, char *);
517 rc
= GPG_ERR_INV_ARG
;
520 cacert
= va_arg (ap
, char *);
522 rc
= GPG_ERR_INV_ARG
;
525 prio
= va_arg (ap
, char *);
526 server_fp
= va_arg (ap
, char *);
534 rc
= _do_tls_connect (pwm
, host
, port
, clientcert
, clientkey
,
535 cacert
, prio
, server_fp
, pwm
->tls_verify
);
538 rc
= _connect_finalize (pwm
);
556 disconnect (pwm_t
* pwm
)
562 assuan_release (pwm
->ctx
);
564 #if defined(WITH_SSH) || defined(WITH_GNUTLS)
573 pwmd_close (pwm_t
* pwm
)
579 pwmd_free (pwm
->pinentry_error
);
580 pwmd_free (pwm
->pinentry_desc
);
581 pwmd_free (pwm
->pinentry_prompt
);
582 pwmd_free (pwm
->pinentry_tty
);
583 pwmd_free (pwm
->pinentry_display
);
584 pwmd_free (pwm
->pinentry_term
);
585 pwmd_free (pwm
->pinentry_lcctype
);
586 pwmd_free (pwm
->pinentry_lcmessages
);
587 pwmd_free (pwm
->filename
);
588 pwmd_free (pwm
->name
);
592 _pinentry_disconnect (pwm
);
599 inquire_realloc_cb (void *data
, const void *buffer
, size_t len
)
601 membuf_t
*mem
= (membuf_t
*) data
;
607 if ((p
= pwmd_realloc (mem
->buf
, mem
->len
+ len
)) == NULL
)
608 return gpg_error (GPG_ERR_ENOMEM
);
611 memcpy ((char *) mem
->buf
+ mem
->len
, buffer
, len
);
617 get_password (pwm_t
* pwm
, char **result
, size_t * len
,
618 pwmd_pinentry_t w
, int echo
)
620 char buf
[LINE_MAX
] = { 0 }, *p
;
621 struct termios told
, tnew
;
630 if (!isatty (STDIN_FILENO
))
632 fprintf (stderr
, N_("Input is not from a terminal! Failing.\n"));
633 return GPG_ERR_ENOTTY
;
638 if (tcgetattr (STDIN_FILENO
, &told
) == -1)
639 return gpg_error_from_syserror ();
641 memcpy (&tnew
, &told
, sizeof (struct termios
));
642 tnew
.c_lflag
&= ~(ECHO
);
643 tnew
.c_lflag
|= ICANON
| ECHONL
;
645 if (tcsetattr (STDIN_FILENO
, TCSANOW
, &tnew
) == -1)
649 tcsetattr (STDIN_FILENO
, TCSANOW
, &told
);
650 return gpg_error_from_errno (n
);
656 case PWMD_PINENTRY_OPEN
:
657 fprintf (stderr
, N_("Password for %s: "), pwm
->filename
);
659 case PWMD_PINENTRY_OPEN_FAILED
:
660 fprintf (stderr
, N_("Invalid password. Password for %s: "),
663 case PWMD_PINENTRY_SAVE
:
664 fprintf (stderr
, N_("New password for %s: "), pwm
->filename
);
666 case PWMD_PINENTRY_SAVE_CONFIRM
:
667 fprintf (stderr
, N_("Confirm password: "));
673 if ((p
= fgets (buf
, sizeof (buf
), stdin
)) == NULL
)
675 tcsetattr (STDIN_FILENO
, TCSANOW
, &told
);
680 tcsetattr (STDIN_FILENO
, TCSANOW
, &told
);
685 return GPG_ERR_CANCELED
;
688 /* Strip the newline character. */
689 p
[strlen (p
) - 1] = 0;
693 key
= pwmd_strdup_printf ("%s", p
);
694 memset (buf
, 0, sizeof (buf
));
696 return GPG_ERR_ENOMEM
;
707 *result
= pwmd_strdup ("");
717 pwmd_password (pwm_t
* pwm
, const char *keyword
, char **data
, size_t * size
)
720 int new_password
= 0;
721 char *password
= NULL
, *newpass
= NULL
;
730 if (!strcmp (keyword
, "NEW_PASSPHRASE"))
733 if (!new_password
&& pwm
->pinentry_try
)
737 if (pwm
->disable_pinentry
)
739 rc
= get_password (pwm
, &password
, size
,
740 new_password
? PWMD_PINENTRY_SAVE
:
741 error
? PWMD_PINENTRY_OPEN_FAILED
:
742 PWMD_PINENTRY_OPEN
, 0);
743 if (!rc
&& new_password
)
744 rc
= get_password (pwm
, &newpass
, size
, PWMD_PINENTRY_SAVE_CONFIRM
,
749 pwmd_pinentry_t which
;
753 ? PWMD_PINENTRY_SAVE_FAILED
: PWMD_PINENTRY_OPEN_FAILED
;
755 which
= new_password
? PWMD_PINENTRY_SAVE
: PWMD_PINENTRY_OPEN
;
757 rc
= pwmd_getpin (pwm
, pwm
->filename
, &password
, size
, which
);
758 if (!rc
&& new_password
)
759 rc
= pwmd_getpin (pwm
, pwm
->filename
, &newpass
, size
,
760 PWMD_PINENTRY_SAVE_CONFIRM
);
763 if (!rc
&& new_password
)
765 if ((!password
&& newpass
) || (!newpass
&& password
)
766 || (newpass
&& password
&& strcmp (newpass
, password
)))
768 if (pwm
->disable_pinentry
)
769 fprintf (stderr
, N_("Passphrases do not match.\n"));
771 pwmd_free (password
);
773 password
= newpass
= NULL
;
779 (void) pwmd_getpin (pwm
, pwm
->filename
, NULL
, NULL
, PWMD_PINENTRY_CLOSE
);
788 inquire_cb (void *data
, const char *keyword
)
790 pwm_t
*pwm
= (pwm_t
*) data
;
795 int new_password
= 0;
797 if (!strcmp (keyword
, "PASSPHRASE"))
799 else if (!strcmp (keyword
, "NEW_PASSPHRASE"))
802 /* Shouldn't get this far without a callback. */
803 if (!pwm
->override_inquire
&& !pwm
->inquire_func
804 && !is_password
&& !new_password
)
805 return gpg_error (GPG_ERR_ASS_NO_INQUIRE_CB
);
814 if (!pwm
->override_inquire
&& (is_password
|| new_password
))
817 rc
= pwmd_password (data
, keyword
, &result
, &len
);
822 rc
= pwm
->inquire_func (pwm
->inquire_data
, keyword
, rc
, &result
,
826 if (rc
&& gpg_err_code (rc
) != GPG_ERR_EOF
)
828 #ifndef LIBASSUAN_2_1_0
829 gpg_error_t trc
= rc
;
831 /* Cancel this inquire. */
832 rc
= assuan_send_data (pwm
->ctx
, NULL
, 1);
838 /* There is a bug (or feature?) in assuan_send_data() that
839 * when cancelling an inquire the next read from the server is
840 * not done until the next command making the next command
841 * fail with GPG_ERR_ASS_UNEXPECTED_CMD.
843 rc
= assuan_read_line (pwm
->ctx
, &line
, &len
);
845 /* Restore the original error. This differs from the error
846 * returned from the pwmd command (GPG_ERR_CANCELED). This
847 * error is returned to the calling function.
856 if (gpg_err_code (rc
) == GPG_ERR_EOF
|| !rc
)
858 if (len
<= 0 && !result
)
863 else if ((len
<= 0 && result
) || (len
&& !result
))
865 rc
= gpg_error (GPG_ERR_INV_ARG
);
869 if (pwm
->inquire_maxlen
870 && pwm
->inquire_sent
+ len
> pwm
->inquire_maxlen
)
872 rc
= gpg_error (GPG_ERR_TOO_LARGE
);
874 rc
= pwm
->inquire_func (pwm
->inquire_data
, keyword
, rc
,
879 arc
= assuan_send_data (pwm
->ctx
, result
, len
);
880 if (gpg_err_code (rc
) == GPG_ERR_EOF
)
893 pwm
->inquire_sent
+= len
;
895 if (pwm
->status_func
)
897 char buf
[ASSUAN_LINELENGTH
];
899 snprintf (buf
, sizeof (buf
), "XFER %lu %lu", pwm
->inquire_sent
,
901 rc
= pwm
->status_func (pwm
->status_data
, buf
);
915 parse_assuan_line (pwm_t
* pwm
)
921 rc
= assuan_read_line (pwm
->ctx
, &line
, &len
);
924 if (line
[0] == 'O' && line
[1] == 'K' &&
925 (line
[2] == 0 || line
[2] == ' '))
928 else if (line
[0] == '#')
931 else if (line
[0] == 'S' && (line
[1] == 0 || line
[1] == ' '))
933 if (pwm
->status_func
)
935 rc
= pwm
->status_func (pwm
->status_data
,
936 line
[1] == 0 ? line
+ 1 : line
+ 2);
939 else if (line
[0] == 'E' && line
[1] == 'R' && line
[2] == 'R' &&
940 (line
[3] == 0 || line
[3] == ' '))
943 rc
= strtol (line
, NULL
, 10);
951 reset_handle_state (pwm_t
* pwm
, int done
)
953 #if defined(WITH_SSH) || defined(WITH_GNUTLS)
966 reset_handle (pwm_t
* h
)
971 _pinentry_disconnect (h
);
973 reset_handle_state (h
, 0);
977 pwmd_disconnect (pwm_t
* pwm
)
980 return FINISH (GPG_ERR_INV_ARG
);
983 return FINISH (GPG_ERR_INV_STATE
);
990 /* Note that this should only be called when not in a command. */
992 pwmd_process (pwm_t
* pwm
)
996 struct timeval tv
= { 0, 0 };
1000 return FINISH (GPG_ERR_INV_ARG
);
1002 return FINISH (GPG_ERR_INV_STATE
);
1005 FD_SET (pwm
->fd
, &fds
);
1006 n
= select (pwm
->fd
+ 1, &fds
, NULL
, NULL
, &tv
);
1009 return FINISH (gpg_error_from_syserror ());
1013 if (FD_ISSET (pwm
->fd
, &fds
))
1014 rc
= parse_assuan_line (pwm
);
1017 while (!rc
&& assuan_pending_line (pwm
->ctx
))
1018 rc
= parse_assuan_line (pwm
);
1020 #if defined (WITH_SSH) || defined (WITH_GNUTLS)
1021 if (gpg_err_code (rc
) == GPG_ERR_EOF
&& pwm
->tcp
)
1032 status_cb (void *data
, const char *line
)
1036 if (!strncmp (line
, "INQUIRE_MAXLEN ", 15))
1037 pwm
->inquire_maxlen
= strtol (line
+ 15, NULL
, 10);
1039 if (pwm
->status_func
)
1040 return pwm
->status_func (pwm
->status_data
, line
);
1046 _assuan_command (pwm_t
* pwm
, assuan_context_t ctx
,
1047 char **result
, size_t * len
, const char *cmd
)
1053 return FINISH (GPG_ERR_INV_ARG
);
1055 if (strlen (cmd
) >= ASSUAN_LINELENGTH
+ 1)
1056 return FINISH (GPG_ERR_LINE_TOO_LONG
);
1060 rc
= assuan_transact (ctx
, cmd
, inquire_realloc_cb
, &data
,
1062 pwm
->pctx
== ctx
? pwm
->_inquire_func
: inquire_cb
,
1063 pwm
->pctx
== ctx
? pwm
->_inquire_data
: pwm
,
1073 pwmd_free (data
.buf
);
1081 inquire_realloc_cb (&data
, "", 1);
1084 *result
= (char *) data
.buf
;
1086 pwmd_free (data
.buf
);
1093 pwm
->inquire_maxlen
= 0;
1098 pwmd_command_ap (pwm_t
* pwm
, char **result
, size_t * rlen
,
1099 pwmd_inquire_cb_t func
, void *user
, const char *cmd
,
1107 return FINISH (GPG_ERR_INV_ARG
);
1109 return FINISH (GPG_ERR_INV_STATE
);
1112 * C99 allows the dst pointer to be null which will calculate the length
1113 * of the would-be result and return it.
1116 len
= vsnprintf (NULL
, 0, cmd
, ap
) + 1;
1117 buf
= (char *) pwmd_malloc (len
);
1121 return FINISH (GPG_ERR_ENOMEM
);
1124 len
= vsnprintf (buf
, len
, cmd
, ap2
);
1127 if (buf
[strlen (buf
) - 1] == '\n')
1128 buf
[strlen (buf
) - 1] = 0;
1129 if (buf
[strlen (buf
) - 1] == '\r')
1130 buf
[strlen (buf
) - 1] = 0;
1132 pwm
->inquire_func
= func
;
1133 pwm
->inquire_data
= user
;
1134 pwm
->inquire_sent
= 0;
1135 gpg_error_t rc
= _assuan_command (pwm
, pwm
->ctx
, result
, rlen
, buf
);
1141 pwmd_command (pwm_t
* pwm
, char **result
, size_t * len
,
1142 pwmd_inquire_cb_t func
, void *user
, const char *cmd
, ...)
1147 return FINISH (GPG_ERR_INV_ARG
);
1149 return FINISH (GPG_ERR_INV_STATE
);
1155 gpg_error_t rc
= pwmd_command_ap (pwm
, result
, len
, func
, user
, cmd
, ap
);
1161 send_pinentry_options (pwm_t
* pwm
)
1165 rc
= pwmd_command (pwm
, NULL
, NULL
, NULL
, NULL
,
1166 "OPTION disable-pinentry=0");
1167 if (!rc
&& pwm
->pinentry_tty
)
1168 rc
= pwmd_command (pwm
, NULL
, NULL
, NULL
, NULL
, "OPTION TTYNAME=%s",
1171 if (!rc
&& pwm
->pinentry_term
)
1172 rc
= pwmd_command (pwm
, NULL
, NULL
, NULL
, NULL
, "OPTION TTYTYPE=%s",
1173 pwm
->pinentry_term
);
1175 if (!rc
&& pwm
->pinentry_display
)
1176 rc
= pwmd_command (pwm
, NULL
, NULL
, NULL
, NULL
, "OPTION DISPLAY=%s",
1177 pwm
->pinentry_display
);
1179 if (!rc
&& pwm
->pinentry_desc
)
1180 rc
= pwmd_command (pwm
, NULL
, NULL
, NULL
, NULL
, "OPTION DESC=%s",
1181 pwm
->pinentry_desc
);
1183 if (!rc
&& pwm
->pinentry_lcctype
)
1184 rc
= pwmd_command (pwm
, NULL
, NULL
, NULL
, NULL
, "OPTION LC_CTYPE=%s",
1185 pwm
->pinentry_lcctype
);
1187 if (!rc
&& pwm
->pinentry_lcmessages
)
1188 rc
= pwmd_command (pwm
, NULL
, NULL
, NULL
, NULL
, "OPTION LC_MESSAGES=%s",
1189 pwm
->pinentry_lcmessages
);
1191 if (!rc
&& pwm
->pinentry_timeout
> 0)
1192 rc
= pwmd_command (pwm
, NULL
, NULL
, NULL
, NULL
,
1193 "OPTION pinentry-timeout=%i", pwm
->pinentry_timeout
);
1199 pwmd_socket_type (pwm_t
* pwm
, pwmd_socket_t
* result
)
1201 if (!pwm
|| !result
)
1202 return FINISH (GPG_ERR_INV_ARG
);
1204 *result
= PWMD_SOCKET_LOCAL
;
1207 return FINISH (GPG_ERR_INV_STATE
);
1209 #if defined(WITH_SSH) || defined(WITH_GNUTLS)
1211 if (pwm
->tcp
&& pwm
->tcp
->ssh
)
1212 *result
= PWMD_SOCKET_SSH
;
1215 if (pwm
->tcp
&& pwm
->tcp
->tls
)
1216 *result
= PWMD_SOCKET_TLS
;
1223 pwmd_open (pwm_t
* pwm
, const char *filename
, pwmd_inquire_cb_t cb
,
1229 if (!pwm
|| !filename
|| !*filename
)
1230 return FINISH (GPG_ERR_INV_ARG
);
1233 return FINISH (GPG_ERR_INV_STATE
);
1235 #if defined(WITH_SSH) || defined(WITH_GNUTLS)
1236 no_pinentry
= pwm
->disable_pinentry
|| pwm
->tcp
|| pwm
->local_pinentry
;
1238 no_pinentry
= pwm
->disable_pinentry
|| pwm
->local_pinentry
;
1242 rc
= send_pinentry_options (pwm
);
1244 rc
= pwmd_command (pwm
, NULL
, NULL
, NULL
, NULL
,
1245 "OPTION disable-pinentry");
1249 pwm
->pinentry_try
= 0;
1250 pwmd_free (pwm
->filename
);
1251 pwm
->filename
= pwmd_strdup (filename
);
1255 rc
= pwmd_command (pwm
, NULL
, NULL
, cb
, data
, "OPEN %s%s",
1256 (pwm
->opts
& OPT_LOCK_ON_OPEN
) ? "--lock " : "",
1259 while (gpg_err_code (rc
) == GPG_ERR_BAD_PASSPHRASE
1260 && no_pinentry
&& ++pwm
->pinentry_try
< pwm
->pinentry_tries
);
1262 pwm
->pinentry_try
= 0;
1266 pwmd_free (pwm
->filename
);
1267 pwm
->filename
= NULL
;
1275 pwmd_save (pwm_t
* pwm
, const char *args
, pwmd_inquire_cb_t cb
, void *data
)
1280 return FINISH (GPG_ERR_INV_ARG
);
1282 return FINISH (GPG_ERR_INV_STATE
);
1284 if (pwm
->disable_pinentry
|| pwm
->local_pinentry
)
1285 rc
= pwmd_command (pwm
, NULL
, NULL
, NULL
, NULL
, "OPTION disable-pinentry");
1288 rc
= pwmd_command (pwm
, NULL
, NULL
, cb
, data
, "SAVE %s", args
? args
: "");
1294 pwmd_setopt (pwm_t
* pwm
, pwmd_option_t opt
, ...)
1302 return FINISH (GPG_ERR_INV_ARG
);
1308 case PWMD_OPTION_LOCK_ON_OPEN
:
1309 n
= va_arg (ap
, int);
1312 rc
= GPG_ERR_INV_VALUE
;
1315 pwm
->opts
|= OPT_LOCK_ON_OPEN
;
1317 pwm
->opts
&= ~OPT_LOCK_ON_OPEN
;
1320 case PWMD_OPTION_INQUIRE_TOTAL
:
1321 pwm
->inquire_total
= va_arg (ap
, size_t);
1323 case PWMD_OPTION_STATUS_CB
:
1324 pwm
->status_func
= va_arg (ap
, pwmd_status_cb_t
);
1326 case PWMD_OPTION_STATUS_DATA
:
1327 pwm
->status_data
= va_arg (ap
, void *);
1329 case PWMD_OPTION_NO_PINENTRY
:
1330 n
= va_arg (ap
, int);
1333 rc
= GPG_ERR_INV_VALUE
;
1335 pwm
->disable_pinentry
= n
;
1338 case PWMD_OPTION_LOCAL_PINENTRY
:
1339 n
= va_arg (ap
, int);
1342 rc
= GPG_ERR_INV_VALUE
;
1344 pwm
->local_pinentry
= n
;
1347 case PWMD_OPTION_PINENTRY_TIMEOUT
:
1348 n
= va_arg (ap
, int);
1351 rc
= GPG_ERR_INV_VALUE
;
1353 pwm
->pinentry_timeout
= n
;
1355 #if defined(WITH_SSH) || defined(WITH_GNUTLS)
1356 if (!rc
&& pwm
->fd
!= -1 && !pwm
->tcp
)
1358 if (!rc
&& pwm
->fd
!= -1)
1360 rc
= pwmd_command (pwm
, NULL
, NULL
, NULL
, NULL
,
1361 "OPTION pinentry-timeout=%i", pwm
->pinentry_timeout
);
1363 case PWMD_OPTION_PINENTRY_TRIES
:
1364 n
= va_arg (ap
, int);
1365 pwm
->pinentry_tries
= n
;
1367 case PWMD_OPTION_PINENTRY_PATH
:
1368 arg1
= va_arg (ap
, char *);
1369 pwmd_free (pwm
->pinentry_path
);
1370 pwm
->pinentry_path
= arg1
? _expand_homedir (arg1
, NULL
) : NULL
;
1372 case PWMD_OPTION_PINENTRY_TTY
:
1373 arg1
= va_arg (ap
, char *);
1374 pwmd_free (pwm
->pinentry_tty
);
1375 pwm
->pinentry_tty
= arg1
? pwmd_strdup (arg1
) : NULL
;
1377 case PWMD_OPTION_PINENTRY_DISPLAY
:
1378 arg1
= va_arg (ap
, char *);
1379 pwmd_free (pwm
->pinentry_display
);
1380 pwm
->pinentry_display
= arg1
? pwmd_strdup (arg1
) : NULL
;
1382 case PWMD_OPTION_PINENTRY_TERM
:
1383 arg1
= va_arg (ap
, char *);
1384 pwmd_free (pwm
->pinentry_term
);
1385 pwm
->pinentry_term
= arg1
? pwmd_strdup (arg1
) : NULL
;
1387 case PWMD_OPTION_PINENTRY_ERROR
:
1388 arg1
= va_arg (ap
, char *);
1389 pwmd_free (pwm
->pinentry_error
);
1390 pwm
->pinentry_error
= arg1
? _percent_escape (arg1
) : NULL
;
1392 case PWMD_OPTION_PINENTRY_PROMPT
:
1393 arg1
= va_arg (ap
, char *);
1394 pwmd_free (pwm
->pinentry_prompt
);
1395 pwm
->pinentry_prompt
= arg1
? _percent_escape (arg1
) : NULL
;
1397 case PWMD_OPTION_PINENTRY_DESC
:
1398 arg1
= va_arg (ap
, char *);
1399 pwmd_free (pwm
->pinentry_desc
);
1400 pwm
->pinentry_desc
= arg1
? _percent_escape (arg1
) : NULL
;
1402 case PWMD_OPTION_PINENTRY_LC_CTYPE
:
1403 arg1
= va_arg (ap
, char *);
1404 pwmd_free (pwm
->pinentry_lcctype
);
1405 pwm
->pinentry_lcctype
= arg1
? pwmd_strdup (arg1
) : NULL
;
1407 case PWMD_OPTION_PINENTRY_LC_MESSAGES
:
1408 arg1
= va_arg (ap
, char *);
1409 pwmd_free (pwm
->pinentry_lcmessages
);
1410 pwm
->pinentry_lcmessages
= arg1
? pwmd_strdup (arg1
) : NULL
;
1412 case PWMD_OPTION_KNOWNHOST_CB
:
1413 pwm
->kh_cb
= va_arg (ap
, pwmd_knownhost_cb_t
);
1415 case PWMD_OPTION_KNOWNHOST_DATA
:
1416 pwm
->kh_data
= va_arg (ap
, void *);
1418 case PWMD_OPTION_SSH_AGENT
:
1419 pwm
->use_agent
= va_arg (ap
, int);
1421 if (pwm
->use_agent
< 0 || pwm
->use_agent
> 1)
1424 rc
= GPG_ERR_INV_VALUE
;
1427 case PWMD_OPTION_TLS_VERIFY
:
1428 pwm
->tls_verify
= va_arg (ap
, int);
1430 if (pwm
->tls_verify
< 0 || pwm
->tls_verify
> 1)
1432 pwm
->tls_verify
= 0;
1433 rc
= GPG_ERR_INV_VALUE
;
1436 case PWMD_OPTION_SOCKET_TIMEOUT
:
1437 pwm
->socket_timeout
= va_arg (ap
, int);
1438 if (pwm
->socket_timeout
< 0)
1440 pwm
->socket_timeout
= 0;
1441 rc
= GPG_ERR_INV_VALUE
;
1445 if (pwm
->tcp
&& pwm
->tcp
->ssh
&& pwm
->tcp
->ssh
->session
)
1447 pwm
->tcp
->ssh
->timeout
= pwm
->socket_timeout
;
1448 libssh2_session_set_timeout (pwm
->tcp
->ssh
->session
,
1449 pwm
->socket_timeout
* 1000);
1453 if (pwm
->tcp
&& pwm
->tcp
->tls
&& pwm
->tcp
->tls
->session
)
1454 pwm
->tcp
->tls
->timeout
= pwm
->socket_timeout
;
1457 case PWMD_OPTION_OVERRIDE_INQUIRE
:
1458 pwm
->override_inquire
= va_arg (ap
, int);
1460 if (pwm
->override_inquire
< 0 || pwm
->override_inquire
> 1)
1462 pwm
->override_inquire
= 0;
1463 rc
= GPG_ERR_INV_VALUE
;
1467 rc
= GPG_ERR_UNKNOWN_OPTION
;
1476 pwmd_new (const char *name
, pwm_t
** pwm
)
1478 pwm_t
*h
= pwmd_calloc (1, sizeof (pwm_t
));
1482 return FINISH (GPG_ERR_ENOMEM
);
1486 h
->name
= pwmd_strdup (name
);
1490 return FINISH (GPG_ERR_ENOMEM
);
1495 h
->pinentry_timeout
= -30;
1496 h
->pinentry_tries
= 3;
1497 #if defined(WITH_SSH) || defined(WITH_GNUTLS)
1498 h
->prot
= PWMD_IP_ANY
;
1501 if (ttyname (STDOUT_FILENO
))
1505 ttyname_r (STDOUT_FILENO
, buf
, sizeof (buf
));
1506 h
->pinentry_tty
= pwmd_strdup (buf
);
1507 if (!h
->pinentry_tty
)
1509 rc
= GPG_ERR_ENOMEM
;
1514 if (getenv ("TERM") && h
->pinentry_tty
)
1516 h
->pinentry_term
= pwmd_strdup (getenv ("TERM"));
1517 if (!h
->pinentry_term
)
1519 rc
= GPG_ERR_ENOMEM
;
1524 if (getenv ("DISPLAY"))
1526 h
->pinentry_display
= pwmd_strdup (getenv ("DISPLAY"));
1527 if (!h
->pinentry_display
)
1529 rc
= GPG_ERR_ENOMEM
;
1534 update_pinentry_settings (h
);
1544 pwmd_free (void *ptr
)
1550 pwmd_malloc (size_t size
)
1552 return _xmalloc (size
);
1556 pwmd_calloc (size_t nmemb
, size_t size
)
1558 return _xcalloc (nmemb
, size
);
1562 pwmd_realloc (void *ptr
, size_t size
)
1564 return _xrealloc (ptr
, size
);
1568 pwmd_strdup (const char *str
)
1575 t
= _xmalloc ((len
+ 1) * sizeof (char));
1579 for (c
= 0; c
< len
; c
++)
1587 pwmd_strdup_printf (const char *fmt
, ...)
1598 len
= vsnprintf (NULL
, 0, fmt
, ap
);
1600 buf
= pwmd_malloc (++len
);
1602 vsnprintf (buf
, len
, fmt
, ap2
);
1609 pwmd_getpin (pwm_t
* pwm
, const char *filename
, char **result
,
1610 size_t * len
, pwmd_pinentry_t which
)
1612 #ifndef WITH_PINENTRY
1613 return FINISH (GPG_ERR_NOT_IMPLEMENTED
);
1615 gpg_error_t rc
= _pwmd_getpin (pwm
, filename
, result
, len
, which
);
1624 return LIBPWMD_VERSION_STR
;
1632 #ifdef WITH_PINENTRY
1633 n
|= PWMD_FEATURE_PINENTRY
;
1636 n
|= PWMD_FEATURE_SSH
;
1639 n
|= PWMD_FEATURE_CRACK
;
1642 n
|= PWMD_FEATURE_GNUTLS
;
1648 pwmd_fd (pwm_t
* pwm
, int *fd
)
1651 return FINISH (GPG_ERR_INV_ARG
);
1654 return FINISH (GPG_ERR_INV_STATE
);
1661 pwmd_set_pointer (pwm_t
*pwm
, void *data
)
1663 pwm
->user_data
= data
;
1667 pwmd_get_pointer (pwm_t
*pwm
)
1669 return pwm
->user_data
;