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 do_pwmd_save_passwd (pwm_t
* pwm
, const char *args
, pwmd_inquire_cb_t cb
,
1276 void *data
, int save
)
1281 return FINISH (GPG_ERR_INV_ARG
);
1283 return FINISH (GPG_ERR_INV_STATE
);
1285 if (pwm
->disable_pinentry
|| pwm
->local_pinentry
)
1286 rc
= pwmd_command (pwm
, NULL
, NULL
, NULL
, NULL
, "OPTION disable-pinentry");
1289 rc
= pwmd_command (pwm
, NULL
, NULL
, cb
, data
,
1290 save
? "SAVE %s" : "PASSWD %s", args
? args
: "");
1296 pwmd_passwd (pwm_t
* pwm
, const char *args
, pwmd_inquire_cb_t cb
, void *data
)
1298 return do_pwmd_save_passwd (pwm
, args
, cb
, data
, 0);
1302 pwmd_save (pwm_t
* pwm
, const char *args
, pwmd_inquire_cb_t cb
, void *data
)
1304 return do_pwmd_save_passwd (pwm
, args
, cb
, data
, 1);
1308 pwmd_get_set_opt (pwm_t
*pwm
, pwmd_option_t opt
, int get
, va_list ap
)
1312 char *arg1
, **charpp
;
1316 return GPG_ERR_INV_ARG
;
1320 case PWMD_OPTION_LOCK_ON_OPEN
:
1323 intp
= va_arg (ap
, int *);
1324 *intp
= pwm
->opts
& OPT_LOCK_ON_OPEN
? 1 : 0;
1328 n
= va_arg (ap
, int);
1331 rc
= GPG_ERR_INV_VALUE
;
1334 pwm
->opts
|= OPT_LOCK_ON_OPEN
;
1336 pwm
->opts
&= ~OPT_LOCK_ON_OPEN
;
1339 case PWMD_OPTION_INQUIRE_TOTAL
:
1342 sizetp
= va_arg (ap
, size_t *);
1343 *sizetp
= pwm
->inquire_total
;
1347 pwm
->inquire_total
= va_arg (ap
, size_t);
1349 case PWMD_OPTION_STATUS_CB
:
1352 pwmd_status_cb_t
*cb
= va_arg (ap
, pwmd_status_cb_t
*);
1354 *cb
= pwm
->status_func
;
1358 pwm
->status_func
= va_arg (ap
, pwmd_status_cb_t
);
1360 case PWMD_OPTION_STATUS_DATA
:
1363 void **data
= va_arg (ap
, void **);
1365 *data
= pwm
->status_data
;
1369 pwm
->status_data
= va_arg (ap
, void *);
1371 case PWMD_OPTION_NO_PINENTRY
:
1374 intp
= va_arg (ap
, int *);
1375 *intp
= pwm
->disable_pinentry
;
1379 n
= va_arg (ap
, int);
1382 rc
= GPG_ERR_INV_VALUE
;
1384 pwm
->disable_pinentry
= n
;
1387 case PWMD_OPTION_LOCAL_PINENTRY
:
1390 intp
= va_arg (ap
, int *);
1391 *intp
= pwm
->local_pinentry
;
1395 n
= va_arg (ap
, int);
1398 rc
= GPG_ERR_INV_VALUE
;
1400 pwm
->local_pinentry
= n
;
1403 case PWMD_OPTION_PINENTRY_TIMEOUT
:
1406 intp
= va_arg (ap
, int *);
1407 *intp
= pwm
->pinentry_timeout
;
1411 n
= va_arg (ap
, int);
1414 rc
= GPG_ERR_INV_VALUE
;
1416 pwm
->pinentry_timeout
= n
;
1418 #if defined(WITH_SSH) || defined(WITH_GNUTLS)
1419 if (!rc
&& pwm
->fd
!= -1 && !pwm
->tcp
)
1421 if (!rc
&& pwm
->fd
!= -1)
1423 rc
= pwmd_command (pwm
, NULL
, NULL
, NULL
, NULL
,
1424 "OPTION pinentry-timeout=%i", pwm
->pinentry_timeout
);
1426 case PWMD_OPTION_PINENTRY_TRIES
:
1429 intp
= va_arg (ap
, int *);
1430 *intp
= pwm
->pinentry_tries
;
1434 n
= va_arg (ap
, int);
1435 pwm
->pinentry_tries
= n
;
1437 case PWMD_OPTION_PINENTRY_PATH
:
1440 charpp
= va_arg (ap
, char **);
1441 *charpp
= pwm
->pinentry_path
;
1445 arg1
= va_arg (ap
, char *);
1446 pwmd_free (pwm
->pinentry_path
);
1447 pwm
->pinentry_path
= arg1
? _expand_homedir (arg1
, NULL
) : NULL
;
1449 case PWMD_OPTION_PINENTRY_TTY
:
1452 charpp
= va_arg (ap
, char **);
1453 *charpp
= pwm
->pinentry_tty
;
1457 arg1
= va_arg (ap
, char *);
1458 pwmd_free (pwm
->pinentry_tty
);
1459 pwm
->pinentry_tty
= arg1
? pwmd_strdup (arg1
) : NULL
;
1461 case PWMD_OPTION_PINENTRY_DISPLAY
:
1464 charpp
= va_arg (ap
, char **);
1465 *charpp
= pwm
->pinentry_display
;
1469 arg1
= va_arg (ap
, char *);
1470 pwmd_free (pwm
->pinentry_display
);
1471 pwm
->pinentry_display
= arg1
? pwmd_strdup (arg1
) : NULL
;
1473 case PWMD_OPTION_PINENTRY_TERM
:
1476 charpp
= va_arg (ap
, char **);
1477 *charpp
= pwm
->pinentry_term
;
1481 arg1
= va_arg (ap
, char *);
1482 pwmd_free (pwm
->pinentry_term
);
1483 pwm
->pinentry_term
= arg1
? pwmd_strdup (arg1
) : NULL
;
1485 case PWMD_OPTION_PINENTRY_ERROR
:
1488 charpp
= va_arg (ap
, char **);
1489 *charpp
= pwm
->pinentry_error
;
1493 arg1
= va_arg (ap
, char *);
1494 pwmd_free (pwm
->pinentry_error
);
1495 pwm
->pinentry_error
= arg1
? _percent_escape (arg1
) : NULL
;
1497 case PWMD_OPTION_PINENTRY_PROMPT
:
1500 charpp
= va_arg (ap
, char **);
1501 *charpp
= pwm
->pinentry_prompt
;
1505 arg1
= va_arg (ap
, char *);
1506 pwmd_free (pwm
->pinentry_prompt
);
1507 pwm
->pinentry_prompt
= arg1
? _percent_escape (arg1
) : NULL
;
1509 case PWMD_OPTION_PINENTRY_DESC
:
1512 charpp
= va_arg (ap
, char **);
1513 *charpp
= pwm
->pinentry_desc
;
1517 arg1
= va_arg (ap
, char *);
1518 pwmd_free (pwm
->pinentry_desc
);
1519 pwm
->pinentry_desc
= arg1
? _percent_escape (arg1
) : NULL
;
1521 case PWMD_OPTION_PINENTRY_LC_CTYPE
:
1524 charpp
= va_arg (ap
, char **);
1525 *charpp
= pwm
->pinentry_lcctype
;
1529 arg1
= va_arg (ap
, char *);
1530 pwmd_free (pwm
->pinentry_lcctype
);
1531 pwm
->pinentry_lcctype
= arg1
? pwmd_strdup (arg1
) : NULL
;
1533 case PWMD_OPTION_PINENTRY_LC_MESSAGES
:
1536 charpp
= va_arg (ap
, char **);
1537 *charpp
= pwm
->pinentry_lcmessages
;
1541 arg1
= va_arg (ap
, char *);
1542 pwmd_free (pwm
->pinentry_lcmessages
);
1543 pwm
->pinentry_lcmessages
= arg1
? pwmd_strdup (arg1
) : NULL
;
1545 case PWMD_OPTION_KNOWNHOST_CB
:
1548 pwmd_knownhost_cb_t
*cb
= va_arg (ap
, pwmd_knownhost_cb_t
*);
1554 pwm
->kh_cb
= va_arg (ap
, pwmd_knownhost_cb_t
);
1556 case PWMD_OPTION_KNOWNHOST_DATA
:
1559 void **data
= va_arg (ap
, void **);
1561 *data
= pwm
->kh_data
;
1565 pwm
->kh_data
= va_arg (ap
, void *);
1567 case PWMD_OPTION_SSH_AGENT
:
1570 intp
= va_arg (ap
, int *);
1571 *intp
= pwm
->use_agent
;
1575 pwm
->use_agent
= va_arg (ap
, int);
1577 if (pwm
->use_agent
< 0 || pwm
->use_agent
> 1)
1580 rc
= GPG_ERR_INV_VALUE
;
1583 case PWMD_OPTION_TLS_VERIFY
:
1586 intp
= va_arg (ap
, int *);
1587 *intp
= pwm
->tls_verify
;
1591 pwm
->tls_verify
= va_arg (ap
, int);
1593 if (pwm
->tls_verify
< 0 || pwm
->tls_verify
> 1)
1595 pwm
->tls_verify
= 0;
1596 rc
= GPG_ERR_INV_VALUE
;
1599 case PWMD_OPTION_SOCKET_TIMEOUT
:
1602 intp
= va_arg (ap
, int *);
1603 *intp
= pwm
->socket_timeout
;
1607 pwm
->socket_timeout
= va_arg (ap
, int);
1608 if (pwm
->socket_timeout
< 0)
1610 pwm
->socket_timeout
= 0;
1611 rc
= GPG_ERR_INV_VALUE
;
1615 if (pwm
->tcp
&& pwm
->tcp
->ssh
&& pwm
->tcp
->ssh
->session
)
1617 pwm
->tcp
->ssh
->timeout
= pwm
->socket_timeout
;
1618 libssh2_session_set_timeout (pwm
->tcp
->ssh
->session
,
1619 pwm
->socket_timeout
* 1000);
1623 if (pwm
->tcp
&& pwm
->tcp
->tls
&& pwm
->tcp
->tls
->session
)
1624 pwm
->tcp
->tls
->timeout
= pwm
->socket_timeout
;
1627 case PWMD_OPTION_OVERRIDE_INQUIRE
:
1630 intp
= va_arg (ap
, int *);
1631 *intp
= pwm
->override_inquire
;
1635 pwm
->override_inquire
= va_arg (ap
, int);
1637 if (pwm
->override_inquire
< 0 || pwm
->override_inquire
> 1)
1639 pwm
->override_inquire
= 0;
1640 rc
= GPG_ERR_INV_VALUE
;
1644 rc
= GPG_ERR_UNKNOWN_OPTION
;
1652 pwmd_setopt (pwm_t
* pwm
, pwmd_option_t opt
, ...)
1658 rc
= pwmd_get_set_opt (pwm
, opt
, 0, ap
);
1664 pwmd_getopt (pwm_t
*pwm
, pwmd_option_t opt
, ...)
1670 rc
= pwmd_get_set_opt (pwm
, opt
, 1, ap
);
1676 pwmd_new (const char *name
, pwm_t
** pwm
)
1678 pwm_t
*h
= pwmd_calloc (1, sizeof (pwm_t
));
1682 return FINISH (GPG_ERR_ENOMEM
);
1686 h
->name
= pwmd_strdup (name
);
1690 return FINISH (GPG_ERR_ENOMEM
);
1695 h
->pinentry_timeout
= -30;
1696 h
->pinentry_tries
= 3;
1697 #if defined(WITH_SSH) || defined(WITH_GNUTLS)
1698 h
->prot
= PWMD_IP_ANY
;
1701 if (ttyname (STDOUT_FILENO
))
1705 ttyname_r (STDOUT_FILENO
, buf
, sizeof (buf
));
1706 h
->pinentry_tty
= pwmd_strdup (buf
);
1707 if (!h
->pinentry_tty
)
1709 rc
= GPG_ERR_ENOMEM
;
1714 if (getenv ("TERM") && h
->pinentry_tty
)
1716 h
->pinentry_term
= pwmd_strdup (getenv ("TERM"));
1717 if (!h
->pinentry_term
)
1719 rc
= GPG_ERR_ENOMEM
;
1724 if (getenv ("DISPLAY"))
1726 h
->pinentry_display
= pwmd_strdup (getenv ("DISPLAY"));
1727 if (!h
->pinentry_display
)
1729 rc
= GPG_ERR_ENOMEM
;
1734 update_pinentry_settings (h
);
1744 pwmd_free (void *ptr
)
1750 pwmd_malloc (size_t size
)
1752 return _xmalloc (size
);
1756 pwmd_calloc (size_t nmemb
, size_t size
)
1758 return _xcalloc (nmemb
, size
);
1762 pwmd_realloc (void *ptr
, size_t size
)
1764 return _xrealloc (ptr
, size
);
1768 pwmd_strdup (const char *str
)
1775 t
= _xmalloc ((len
+ 1) * sizeof (char));
1779 for (c
= 0; c
< len
; c
++)
1787 pwmd_strdup_printf (const char *fmt
, ...)
1798 len
= vsnprintf (NULL
, 0, fmt
, ap
);
1800 buf
= pwmd_malloc (++len
);
1802 vsnprintf (buf
, len
, fmt
, ap2
);
1809 pwmd_getpin (pwm_t
* pwm
, const char *filename
, char **result
,
1810 size_t * len
, pwmd_pinentry_t which
)
1812 #ifndef WITH_PINENTRY
1813 return FINISH (GPG_ERR_NOT_IMPLEMENTED
);
1815 gpg_error_t rc
= _pwmd_getpin (pwm
, filename
, result
, len
, which
);
1824 return LIBPWMD_VERSION_STR
;
1832 #ifdef WITH_PINENTRY
1833 n
|= PWMD_FEATURE_PINENTRY
;
1836 n
|= PWMD_FEATURE_SSH
;
1839 n
|= PWMD_FEATURE_CRACK
;
1842 n
|= PWMD_FEATURE_GNUTLS
;
1848 pwmd_fd (pwm_t
* pwm
, int *fd
)
1851 return FINISH (GPG_ERR_INV_ARG
);
1854 return FINISH (GPG_ERR_INV_STATE
);
1861 pwmd_set_pointer (pwm_t
*pwm
, void *data
)
1863 pwm
->user_data
= data
;
1867 pwmd_get_pointer (pwm_t
*pwm
)
1869 return pwm
->user_data
;