2 Copyright (C) 2006-2016, 2017 Ben Kibbey <bjk@luxsci.net>
4 This file is part of libpwmd.
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
32 #include <sys/types.h>
33 #include <sys/socket.h>
43 #include <sys/select.h>
67 #if defined(WITH_SSH) || defined(WITH_GNUTLS)
68 #include <sys/types.h>
69 #include <sys/socket.h>
71 #include <netinet/in.h>
81 #define FINISH(rc) (gpg_err_source(rc) == GPG_ERR_SOURCE_UNKNOWN) \
90 static gpg_error_t
status_cb (void *data
, const char *line
);
93 hook_read (assuan_context_t ctx
, assuan_fd_t fd
, void *data
, size_t len
)
95 pwm_t
*pwm
= assuan_get_pointer (ctx
);
98 return pwm
->read_cb (pwm
->read_cb_data
, fd
, data
, len
);
100 #if defined(WITH_SSH) || defined(WITH_GNUTLS)
102 if (pwm
&& pwm
->tcp
&& pwm
->tcp
->ssh
)
103 return read_hook_ssh (pwm
->tcp
->ssh
, fd
, data
, len
);
106 if (pwm
&& pwm
->tcp
&& pwm
->tcp
->tls
)
107 return tls_read_hook (pwm
, fd
, data
, len
);
111 return read ((int) fd
, data
, len
);
115 hook_write (assuan_context_t ctx
, assuan_fd_t fd
, const void *data
,
118 pwm_t
*pwm
= assuan_get_pointer (ctx
);
122 return pwm
->write_cb (pwm
->write_cb_data
, fd
, data
, len
);
124 #if defined(WITH_SSH) || defined(WITH_GNUTLS)
126 if (pwm
&& pwm
->tcp
&& pwm
->tcp
->ssh
)
127 return write_hook_ssh (pwm
->tcp
->ssh
, fd
, data
, len
);
130 if (pwm
&& pwm
->tcp
&& pwm
->tcp
->tls
)
131 return tls_write_hook (pwm
, fd
, data
, len
);
135 /* libassuan cannot handle EAGAIN when doing writes. */
138 wrote
= write ((int) fd
, data
, len
);
139 if (wrote
== -1 && errno
== EAGAIN
)
142 while (wrote
== -1 && errno
== EAGAIN
);
148 hook_waitpid (assuan_context_t ctx
, pid_t pid
, int action
, int *status
,
153 return waitpid (pid
, status
, options
);
159 static int initialized
;
162 // May be called more than once.
163 gnutls_global_init ();
170 bindtextdomain ("libpwmd", LOCALEDIR
);
176 //gpgrt_set_alloc_func (_xrealloc_gpgrt);
190 gnutls_global_deinit ();
198 _connect_finalize (pwm_t
* pwm
)
203 int n
= assuan_get_active_fds (pwm
->ctx
, 0, active
, N_ARRAY (active
));
206 return GPG_ERR_EBADFD
;
210 pwm
->pinentry_pid
= -1;
213 rc
= pwmd_command (pwm
, &result
, NULL
, NULL
, NULL
, "GETINFO VERSION");
216 pwm
->version
= strtoul (result
, NULL
, 16);
220 if (!rc
&& pwm
->name
)
221 rc
= pwmd_command (pwm
, NULL
, NULL
, NULL
, NULL
, "OPTION NAME=%s",
228 connect_uds (pwm_t
* pwm
, const char *path
)
230 char *socketpath
= NULL
;
236 return GPG_ERR_INV_ARG
;
238 pwbuf
= _getpwuid (&pw
);
240 return gpg_error_from_syserror ();
243 socketpath
= pwmd_strdup_printf ("%s/.pwmd/socket", pw
.pw_dir
);
245 socketpath
= _expand_homedir ((char *) path
, &pw
);
249 return GPG_ERR_ENOMEM
;
251 rc
= assuan_socket_connect (pwm
->ctx
, socketpath
, ASSUAN_INVALID_FD
, 0);
252 pwmd_free (socketpath
);
253 return rc
? rc
: _connect_finalize (pwm
);
257 init_handle (pwm_t
* pwm
)
260 static struct assuan_malloc_hooks mhooks
= {
261 pwmd_malloc
, pwmd_realloc
, pwmd_free
263 static struct assuan_system_hooks shooks
= {
264 ASSUAN_SYSTEM_HOOKS_VERSION
,
272 NULL
, //sendmsg both are used for FD passing
280 rc
= assuan_new_ext (&pwm
->ctx
, GPG_ERR_SOURCE_DEFAULT
, &mhooks
, NULL
,
285 assuan_set_pointer (pwm
->ctx
, pwm
);
286 assuan_ctx_set_system_hooks (pwm
->ctx
, &shooks
);
290 #if defined(WITH_SSH) || defined(WITH_GNUTLS)
292 free_tcp (pwm_t
*pwm
)
294 struct tcp_s
*tcp
= pwm
->tcp
;
300 _free_ssh_conn (tcp
->ssh
);
306 pwmd_free (tcp
->host
);
309 freeaddrinfo (tcp
->addrs
);
313 pthread_cond_destroy (&tcp
->dns_cond
);
314 pthread_mutex_destroy (&tcp
->dns_mutex
);
320 #if defined(WITH_SSH) || defined(WITH_GNUTLS)
322 resolve_host_thread (void *arg
)
325 struct addrinfo hints
= { 0 };
331 hints
.ai_family
= AF_UNSPEC
;
334 hints
.ai_family
= AF_INET
;
337 hints
.ai_family
= AF_INET6
;
341 hints
.ai_socktype
= SOCK_STREAM
;
342 snprintf (portstr
, sizeof (portstr
), "%i", pwm
->tcp
->port
);
343 int *n
= pwmd_malloc (sizeof (int));
344 pthread_cleanup_push (pwmd_free
, n
);
345 *n
= getaddrinfo (pwm
->tcp
->host
, portstr
, &hints
, &pwm
->tcp
->addrs
);
346 pthread_cleanup_pop (0);
347 pthread_cond_broadcast (&pwm
->tcp
->dns_cond
);
353 tcp_connect_common (pwm_t
* pwm
)
355 #define TS_TIMEOUT 50000000L
362 n
= pthread_create (&tid
, NULL
, resolve_host_thread
, pwm
);
364 return gpg_error_from_errno (n
);
366 pthread_mutex_lock (&pwm
->tcp
->dns_mutex
);
373 clock_gettime (CLOCK_REALTIME
, &ts
);
374 if (ts
.tv_nsec
+ TS_TIMEOUT
>= 1000000000LL) {
379 ts
.tv_nsec
+= TS_TIMEOUT
;
383 #ifdef HAVE_PTHREAD_CANCEL
384 pthread_cancel (tid
);
385 pthread_join (tid
, NULL
);
387 pthread_join (tid
, (void **)&result
);
390 return GPG_ERR_CANCELED
;
393 n
= pthread_cond_timedwait (&pwm
->tcp
->dns_cond
, &pwm
->tcp
->dns_mutex
,
397 if (pwm
->socket_timeout
&& ts
.tv_sec
- now
>= pwm
->socket_timeout
)
399 #ifdef HAVE_PTHREAD_CANCEL
400 pthread_cancel (tid
);
401 pthread_join (tid
, NULL
);
403 pthread_join (tid
, (void **)&result
);
406 return GPG_ERR_ETIMEDOUT
;
413 #ifdef HAVE_PTHREAD_CANCEL
414 pthread_cancel (tid
);
415 pthread_join (tid
, NULL
);
417 pthread_join (tid
, (void **)&result
);
420 return gpg_error_from_errno (n
);
423 pthread_join (tid
, (void **)&result
);
430 return GPG_ERR_UNKNOWN_HOST
; //FIXME
432 for (pwm
->tcp
->addr
= pwm
->tcp
->addrs
; pwm
->tcp
->addr
;
433 pwm
->tcp
->addr
= pwm
->tcp
->addrs
->ai_next
)
435 pwm
->fd
= socket (pwm
->tcp
->addr
->ai_family
, SOCK_STREAM
, 0);
438 rc
= gpg_error_from_syserror ();
439 if (pwm
->tcp
->addr
== pwm
->tcp
->addrs
->ai_next
)
444 if (fcntl (pwm
->fd
, F_SETFL
, O_NONBLOCK
) == -1)
446 rc
= gpg_error_from_syserror ();
450 if (connect (pwm
->fd
, pwm
->tcp
->addr
->ai_addr
,
451 pwm
->tcp
->addr
->ai_family
== AF_INET6
452 ? sizeof (struct sockaddr_in6
)
453 : sizeof (struct sockaddr
)) == -1)
457 unsigned elapsed
= 0;
459 rc
= gpg_error_from_syserror ();
460 if (gpg_err_code (rc
) != GPG_ERR_EINPROGRESS
)
464 if (pwm
->tcp
->addr
== pwm
->tcp
->addrs
->ai_next
)
473 FD_SET (pwm
->fd
, &wfds
);
474 n
= select (pwm
->fd
+1, NULL
, &wfds
, NULL
, &tv
);
476 if (!n
|| pwm
->cancel
)
479 rc
= gpg_error (GPG_ERR_CANCELED
);
480 else if (++elapsed
>= pwm
->socket_timeout
)
481 rc
= gpg_error (GPG_ERR_ETIMEDOUT
);
487 socklen_t len
= sizeof(int);
489 getsockopt (pwm
->fd
, SOL_SOCKET
, SO_ERROR
, &n
, &len
);
491 rc
= gpg_error_from_errno (n
);
494 rc
= gpg_error_from_syserror ();
500 if (pwm
->tcp
->addr
== pwm
->tcp
->addrs
->ai_next
501 || gpg_err_code (rc
) == GPG_ERR_ETIMEDOUT
513 if (fcntl (pwm
->fd
, F_SETFL
, 0) == -1)
514 rc
= gpg_error_from_syserror ();
521 command_start (pwm_t
*pwm
)
527 pwmd_connect_fd (pwm_t
* pwm
, int fd
)
532 return FINISH (GPG_ERR_INV_ARG
);
535 rc
= init_handle (pwm
);
542 if (!(pwm
->opts
& OPT_SIGPIPE
))
543 signal (SIGPIPE
, SIG_IGN
);
545 rc
= assuan_socket_connect_fd (pwm
->ctx
, fd
, 0);
549 pwm
->connected
= pwm
->user_fd
= 1;
550 rc
= _connect_finalize (pwm
);
554 pwmd_disconnect (pwm
);
560 pwmd_connect (pwm_t
* pwm
, const char *url
, ...)
566 return FINISH (GPG_ERR_INV_ARG
);
569 rc
= init_handle (pwm
);
571 return FINISH (GPG_ERR_INV_ARG
);
576 if (!(pwm
->opts
& OPT_SIGPIPE
))
577 signal (SIGPIPE
, SIG_IGN
);
582 rc
= GPG_ERR_UNSUPPORTED_PROTOCOL
;
584 if (p
&& (*p
== '/' || *p
== '~'))
585 rc
= connect_uds (pwm
, p
);
586 else if (!p
|| !strncmp (p
, "file://", 7))
590 #ifdef DEFAULT_PWMD_SOCKET
592 p
= DEFAULT_PWMD_SOCKET
;
594 rc
= connect_uds (pwm
, p
);
596 else if (!strncmp (p
, "ssh://", 6) || !strncmp (p
, "ssh6://", 7) ||
597 !strncmp (p
, "ssh4://", 7))
600 return FINISH (GPG_ERR_NOT_IMPLEMENTED
);
604 char *username
= NULL
;
606 if (!strncmp (p
, "ssh6://", 7))
608 pwm
->prot
= PWMD_IPV6
;
611 else if (!strncmp (p
, "ssh4://", 7))
613 pwm
->prot
= PWMD_IPV4
;
618 pwm
->prot
= PWMD_IP_ANY
;
622 rc
= _parse_ssh_url (p
, &host
, &port
, &username
);
626 char *identity
= NULL
;
627 char *knownhosts
= NULL
;
630 identity
= va_arg (ap
, char *);
632 if (!identity
&& !pwm
->use_agent
)
633 rc
= GPG_ERR_INV_ARG
;
635 knownhosts
= va_arg (ap
, char *);
640 rc
= _do_ssh_connect (pwm
, host
, port
, identity
, username
,
644 rc
= _connect_finalize (pwm
);
651 pwmd_free (username
);
652 pwm
->local_pinentry
= 1;
653 pwmd_free (pwm
->ssh_passphrase
);
654 pwm
->ssh_passphrase
= NULL
;
657 else if (!strncmp (p
, "tls://", 6) || !strncmp (p
, "tls6://", 7) ||
658 !strncmp (p
, "tls4://", 7))
661 return FINISH (GPG_ERR_NOT_IMPLEMENTED
);
666 if (!strncmp (p
, "tls6://", 7))
668 pwm
->prot
= PWMD_IPV6
;
671 else if (!strncmp (p
, "tls4://", 7))
673 pwm
->prot
= PWMD_IPV4
;
678 pwm
->prot
= PWMD_IP_ANY
;
682 rc
= tls_parse_url (p
, &host
, &port
);
686 char *clientcert
= NULL
;
687 char *clientkey
= NULL
;
689 char *server_fp
= NULL
;
692 clientcert
= va_arg (ap
, char *);
695 rc
= GPG_ERR_INV_ARG
;
698 clientkey
= va_arg (ap
, char *);
700 rc
= GPG_ERR_INV_ARG
;
703 cacert
= va_arg (ap
, char *);
705 rc
= GPG_ERR_INV_ARG
;
707 server_fp
= va_arg (ap
, char *);
714 rc
= tls_connect (pwm
, host
, port
, clientcert
, clientkey
, cacert
,
715 pwm
->tls_priority
, server_fp
, pwm
->tls_verify
);
718 rc
= _connect_finalize (pwm
);
725 pwm
->local_pinentry
= 1;
736 disconnect (pwm_t
* pwm
)
742 assuan_release (pwm
->ctx
);
744 #if defined(WITH_SSH) || defined(WITH_GNUTLS)
749 pwm
->connected
= pwm
->user_fd
= 0;
753 pwmd_close (pwm_t
* pwm
)
759 pwmd_free (pwm
->pinentry_error
);
760 pwmd_free (pwm
->pinentry_desc
);
761 pwmd_free (pwm
->pinentry_prompt
);
762 pwmd_free (pwm
->pinentry_tty
);
763 pwmd_free (pwm
->pinentry_display
);
764 pwmd_free (pwm
->pinentry_term
);
765 pwmd_free (pwm
->pinentry_lcctype
);
766 pwmd_free (pwm
->pinentry_lcmessages
);
767 pwmd_free (pwm
->filename
);
768 pwmd_free (pwm
->name
);
769 pwmd_free (pwm
->passphrase_info
);
770 pwmd_free (pwm
->passphrase_hint
);
771 pwmd_free (pwm
->ssh_passphrase
);
772 pwmd_free (pwm
->tls_priority
);
776 _pinentry_disconnect (pwm
);
783 inquire_realloc_cb (void *data
, const void *buffer
, size_t len
)
785 membuf_t
*mem
= (membuf_t
*) data
;
791 if ((p
= pwmd_realloc (mem
->buf
, mem
->len
+ len
)) == NULL
)
792 return gpg_error (GPG_ERR_ENOMEM
);
795 memcpy ((char *) mem
->buf
+ mem
->len
, buffer
, len
);
801 get_password (pwm_t
* pwm
, char **result
, size_t * len
,
802 pwmd_pinentry_t w
, int echo
)
804 char buf
[ASSUAN_LINELENGTH
+1] = { 0 }, *p
;
805 struct termios told
, tnew
;
814 if (!isatty (STDIN_FILENO
))
816 fprintf (stderr
, N_("Input is not from a terminal! Failing.\n"));
817 return GPG_ERR_ENOTTY
;
822 if (tcgetattr (STDIN_FILENO
, &told
) == -1)
823 return gpg_error_from_syserror ();
825 memcpy (&tnew
, &told
, sizeof (struct termios
));
826 tnew
.c_lflag
&= ~(ECHO
);
827 tnew
.c_lflag
|= ICANON
| ECHONL
;
829 if (tcsetattr (STDIN_FILENO
, TCSANOW
, &tnew
) == -1)
833 tcsetattr (STDIN_FILENO
, TCSANOW
, &told
);
834 return gpg_error_from_errno (n
);
838 if (pwm
->passphrase_hint
)
839 fprintf(stderr
, N_("Key info: %s\n"), pwm
->passphrase_hint
);
843 case PWMD_PINENTRY_OPEN
:
844 fprintf (stderr
, N_("Passphrase for %s: "), pwm
->filename
);
846 case PWMD_PINENTRY_OPEN_FAILED
:
847 fprintf (stderr
, N_("Invalid passphrase. Passphrase for %s: "),
850 case PWMD_PINENTRY_SAVE
:
851 fprintf (stderr
, N_("New passphrase for %s: "), pwm
->filename
);
853 case PWMD_PINENTRY_SAVE_CONFIRM
:
854 fprintf (stderr
, N_("Repeat passphrase: "));
856 case PWMD_PINENTRY_CONFIRM
:
857 if (pwm
->pinentry_desc
)
858 fprintf (stderr
, "%s", pwm
->pinentry_desc
);
860 if (pwm
->pinentry_prompt
)
861 fprintf (stderr
, "%s", pwm
->pinentry_prompt
);
863 fprintf(stderr
, N_("Confirm [y/N]:"));
868 p
= fgets (buf
, sizeof (buf
), stdin
);
871 tcsetattr (STDIN_FILENO
, TCSANOW
, &told
);
873 if (!p
|| feof (stdin
))
876 return GPG_ERR_CANCELED
;
879 /* Strip the newline character. */
880 p
[strlen (p
) - 1] = 0;
884 if (w
== PWMD_PINENTRY_CONFIRM
)
886 if (*p
!= 'y' && *p
!= 'Y')
887 return GPG_ERR_CANCELED
;
891 key
= pwmd_strdup_printf ("%s", p
);
892 wipememory (buf
, 0, sizeof (buf
));
894 return GPG_ERR_ENOMEM
;
904 if (w
== PWMD_PINENTRY_CONFIRM
)
905 return GPG_ERR_CANCELED
;
907 /* To satisfy inquire_cb(). */
909 *result
= pwmd_strdup ("");
919 pwmd_password (pwm_t
* pwm
, const char *keyword
, char **data
, size_t * size
)
922 int new_password
= 0;
923 char *password
= NULL
, *newpass
= NULL
;
934 if (!strcmp (keyword
, "NEW_PASSPHRASE"))
937 if (!new_password
&& pwm
->pinentry_try
)
941 if (pwm
->disable_pinentry
)
943 rc
= get_password (pwm
, &password
, size
,
944 new_password
? PWMD_PINENTRY_SAVE
:
945 error
? PWMD_PINENTRY_OPEN_FAILED
:
946 PWMD_PINENTRY_OPEN
, 0);
947 if (!rc
&& new_password
)
948 rc
= get_password (pwm
, &newpass
, size
, PWMD_PINENTRY_SAVE_CONFIRM
,
953 pwmd_pinentry_t which
;
957 ? PWMD_PINENTRY_SAVE_FAILED
: PWMD_PINENTRY_OPEN_FAILED
;
959 which
= new_password
? PWMD_PINENTRY_SAVE
: PWMD_PINENTRY_OPEN
;
961 rc
= pwmd_getpin (pwm
, pwm
->filename
, &password
, size
, which
);
962 if (!rc
&& new_password
)
963 rc
= pwmd_getpin (pwm
, pwm
->filename
, &newpass
, size
,
964 PWMD_PINENTRY_SAVE_CONFIRM
);
967 if (!rc
&& new_password
)
969 if ((!password
&& newpass
) || (!newpass
&& password
)
970 || (newpass
&& password
&& strcmp (newpass
, password
)))
972 if (pwm
->disable_pinentry
)
973 fprintf (stderr
, N_("Passphrases do not match.\n"));
975 pwmd_free (password
);
977 password
= newpass
= NULL
;
983 (void) pwmd_getpin (pwm
, pwm
->filename
, NULL
, NULL
, PWMD_PINENTRY_CLOSE
);
988 pwmd_free (password
);
994 inquire_cb (void *data
, const char *keyword
)
996 pwm_t
*pwm
= (pwm_t
*) data
;
1000 int is_password
= 0;
1001 int new_password
= 0;
1003 if (!strcmp (keyword
, "PASSPHRASE") || !strcmp (keyword
, "SIGN_PASSPHRASE"))
1005 else if (!strcmp (keyword
, "NEW_PASSPHRASE") || !strcmp (keyword
, "GENKEY"))
1008 /* Shouldn't get this far without a callback. */
1009 if (!pwm
->override_inquire
&& !pwm
->inquire_func
1010 && !is_password
&& !new_password
)
1011 return gpg_error (GPG_ERR_ASS_NO_INQUIRE_CB
);
1020 if (!pwm
->override_inquire
&& (is_password
|| new_password
))
1023 rc
= pwmd_password (data
, keyword
, &result
, &len
);
1028 rc
= pwm
->inquire_func (pwm
->inquire_data
, keyword
, rc
, &result
,
1031 /* gpg will truncate a passphrase at the first nil byte which may be bad
1032 * for generated key files. */
1033 if ((!rc
|| gpg_err_code (rc
) == GPG_ERR_EOF
)
1034 && (is_password
|| new_password
))
1036 if (len
&& result
&& *result
)
1038 for (size_t n
= 0; n
< len
; n
++)
1040 if (result
[n
] == 0 && n
+1 != len
)
1041 rc
= GPG_ERR_INV_PASSPHRASE
;
1047 if (rc
&& gpg_err_code (rc
) != GPG_ERR_EOF
)
1049 #ifndef LIBASSUAN_2_1_0
1050 gpg_error_t trc
= rc
;
1052 /* Cancel this inquire. */
1053 rc
= assuan_send_data (pwm
->ctx
, NULL
, 1);
1059 /* There is a bug (or feature?) in assuan_send_data() that
1060 * when cancelling an inquire the next read from the server is
1061 * not done until the next command making the next command
1062 * fail with GPG_ERR_ASS_UNEXPECTED_CMD.
1064 rc
= assuan_read_line (pwm
->ctx
, &line
, &len
);
1066 /* Restore the original error. This differs from the error
1067 * returned from the pwmd command (GPG_ERR_CANCELED). This
1068 * error is returned to the calling function.
1077 if (gpg_err_code (rc
) == GPG_ERR_EOF
|| !rc
)
1079 if (len
<= 0 && !result
)
1084 else if ((len
<= 0 && result
) || (len
&& !result
))
1086 rc
= gpg_error (GPG_ERR_INV_ARG
);
1090 if (pwm
->inquire_maxlen
1091 && pwm
->inquire_sent
+ len
> pwm
->inquire_maxlen
)
1093 rc
= gpg_error (GPG_ERR_TOO_LARGE
);
1095 rc
= pwm
->inquire_func (pwm
->inquire_data
, keyword
, rc
,
1100 arc
= assuan_send_data (pwm
->ctx
, result
, len
);
1101 if (gpg_err_code (rc
) == GPG_ERR_EOF
)
1114 pwm
->inquire_sent
+= len
;
1116 if (pwm
->status_func
)
1118 char buf
[ASSUAN_LINELENGTH
];
1120 snprintf (buf
, sizeof (buf
), "XFER %zu %zu", pwm
->inquire_sent
,
1121 pwm
->inquire_total
);
1122 rc
= pwm
->status_func (pwm
->status_data
, buf
);
1132 pwm
->inquire_maxlen
= pwm
->inquire_sent
= 0;
1137 parse_assuan_line (pwm_t
* pwm
)
1143 rc
= assuan_read_line (pwm
->ctx
, &line
, &len
);
1146 if (line
[0] == 'O' && line
[1] == 'K' &&
1147 (line
[2] == 0 || line
[2] == ' '))
1150 else if (line
[0] == '#')
1153 else if (line
[0] == 'S' && (line
[1] == 0 || line
[1] == ' '))
1155 rc
= status_cb (pwm
, line
[1] == 0 ? line
+ 1 : line
+ 2);
1157 else if (line
[0] == 'E' && line
[1] == 'R' && line
[2] == 'R' &&
1158 (line
[3] == 0 || line
[3] == ' '))
1161 rc
= strtol (line
, NULL
, 10);
1169 reset_handle (pwm_t
*pwm
)
1174 pwm
->pinentry_disabled
= 0;
1175 #ifdef WITH_PINENTRY
1177 _pinentry_disconnect (pwm
);
1179 #if defined(WITH_SSH) || defined(WITH_GNUTLS)
1190 pwmd_disconnect (pwm_t
* pwm
)
1193 return FINISH (GPG_ERR_INV_ARG
);
1195 command_start (pwm
);
1198 return FINISH (GPG_ERR_INV_STATE
);
1205 /* Note that this should only be called when not in a command. */
1207 pwmd_process (pwm_t
* pwm
)
1211 struct timeval tv
= { 0, 0 };
1214 if (!pwm
|| pwm
->fd
== -1)
1215 return FINISH (GPG_ERR_INV_ARG
);
1217 return FINISH (GPG_ERR_INV_STATE
);
1220 FD_SET (pwm
->fd
, &fds
);
1221 n
= select (pwm
->fd
+ 1, &fds
, NULL
, NULL
, &tv
);
1224 return FINISH (gpg_error_from_syserror ());
1228 if (FD_ISSET (pwm
->fd
, &fds
))
1229 rc
= parse_assuan_line (pwm
);
1232 while (!rc
&& assuan_pending_line (pwm
->ctx
))
1233 rc
= parse_assuan_line (pwm
);
1235 #if defined (WITH_SSH) || defined (WITH_GNUTLS)
1236 if (gpg_err_code (rc
) == GPG_ERR_EOF
&& pwm
->tcp
)
1247 status_cb (void *data
, const char *line
)
1251 if (!strncmp (line
, "INQUIRE_MAXLEN ", 15))
1252 pwm
->inquire_maxlen
= strtol (line
+ 15, NULL
, 10);
1253 else if (!strncmp (line
, "PASSPHRASE_HINT ", 16))
1255 pwmd_free (pwm
->passphrase_hint
);
1256 pwm
->passphrase_hint
= pwmd_strdup (line
+16);
1258 else if (!strncmp (line
, "PASSPHRASE_INFO ", 16))
1260 pwmd_free (pwm
->passphrase_info
);
1261 pwm
->passphrase_info
= pwmd_strdup (line
+16);
1264 else if (!strcmp (line
, "REHANDSHAKE"))
1269 ret
= tls_read_hook (pwm
, pwm
->fd
, buf
, sizeof (buf
));
1272 pwm
->tls_error
= ret
;
1273 return GPG_ERR_GENERAL
;
1278 if (pwm
->status_func
)
1279 return pwm
->status_func (pwm
->status_data
, line
);
1285 _assuan_command (pwm_t
* pwm
, assuan_context_t ctx
,
1286 char **result
, size_t * len
, const char *cmd
)
1292 return FINISH (GPG_ERR_INV_ARG
);
1294 if (strlen (cmd
) >= ASSUAN_LINELENGTH
+ 1)
1295 return FINISH (GPG_ERR_LINE_TOO_LONG
);
1299 rc
= assuan_transact (ctx
, cmd
, inquire_realloc_cb
, &data
,
1301 pwm
->pctx
== ctx
? pwm
->_inquire_func
: inquire_cb
,
1302 pwm
->pctx
== ctx
? pwm
->_inquire_data
: pwm
,
1312 pwmd_free (data
.buf
);
1320 inquire_realloc_cb (&data
, "", 1);
1323 *result
= (char *) data
.buf
;
1325 pwmd_free (data
.buf
);
1332 pwm
->inquire_maxlen
= 0;
1337 pwmd_command_ap (pwm_t
* pwm
, char **result
, size_t * rlen
,
1338 pwmd_inquire_cb_t func
, void *user
, const char *cmd
,
1346 return GPG_ERR_INV_ARG
;
1348 command_start (pwm
);
1357 return FINISH (GPG_ERR_INV_ARG
);
1359 return FINISH (GPG_ERR_INV_STATE
);
1362 * C99 allows the dst pointer to be null which will calculate the length
1363 * of the would-be result and return it.
1366 len
= vsnprintf (NULL
, 0, cmd
, ap
);
1367 buf
= (char *) pwmd_malloc (len
+1);
1371 return FINISH (GPG_ERR_ENOMEM
);
1374 if (vsnprintf (buf
, len
+1, cmd
, ap2
) != len
)
1378 return FINISH (GPG_ERR_ENOMEM
);
1381 if (buf
[strlen (buf
) - 1] == '\n')
1382 buf
[strlen (buf
) - 1] = 0;
1383 if (buf
[strlen (buf
) - 1] == '\r')
1384 buf
[strlen (buf
) - 1] = 0;
1386 pwm
->inquire_func
= func
;
1387 pwm
->inquire_data
= user
;
1388 pwm
->inquire_sent
= 0;
1389 gpg_error_t rc
= _assuan_command (pwm
, pwm
->ctx
, result
, rlen
, buf
);
1395 pwmd_command (pwm_t
* pwm
, char **result
, size_t * len
,
1396 pwmd_inquire_cb_t func
, void *user
, const char *cmd
, ...)
1407 return FINISH (GPG_ERR_INV_ARG
);
1409 return FINISH (GPG_ERR_INV_STATE
);
1412 gpg_error_t rc
= pwmd_command_ap (pwm
, result
, len
, func
, user
, cmd
, ap
);
1418 send_pinentry_timeout (pwm_t
*pwm
)
1422 if ((pwm
->pinentry_timeout
>= 0
1423 && pwm
->pinentry_timeout
!= pwm
->current_pinentry_timeout
)
1424 || (pwm
->pinentry_timeout
== -1
1425 && pwm
->pinentry_timeout
!= pwm
->current_pinentry_timeout
))
1427 rc
= pwmd_command (pwm
, NULL
, NULL
, NULL
, NULL
,
1428 "OPTION pinentry-timeout=%i",
1429 pwm
->pinentry_timeout
);
1431 pwm
->current_pinentry_timeout
= pwm
->pinentry_timeout
;
1438 send_pinentry_options (pwm_t
* pwm
)
1442 rc
= pwmd_command (pwm
, NULL
, NULL
, NULL
, NULL
,
1443 "OPTION disable-pinentry=0");
1444 if (!rc
&& pwm
->pinentry_tty
)
1445 rc
= pwmd_command (pwm
, NULL
, NULL
, NULL
, NULL
, "OPTION TTYNAME=%s",
1448 if (!rc
&& pwm
->pinentry_term
)
1449 rc
= pwmd_command (pwm
, NULL
, NULL
, NULL
, NULL
, "OPTION TTYTYPE=%s",
1450 pwm
->pinentry_term
);
1452 if (!rc
&& pwm
->pinentry_display
)
1453 rc
= pwmd_command (pwm
, NULL
, NULL
, NULL
, NULL
, "OPTION DISPLAY=%s",
1454 pwm
->pinentry_display
);
1456 if (!rc
&& pwm
->pinentry_desc
)
1457 rc
= pwmd_command (pwm
, NULL
, NULL
, NULL
, NULL
, "OPTION DESC=%s",
1458 pwm
->pinentry_desc
);
1460 if (!rc
&& pwm
->pinentry_lcctype
)
1461 rc
= pwmd_command (pwm
, NULL
, NULL
, NULL
, NULL
, "OPTION LC_CTYPE=%s",
1462 pwm
->pinentry_lcctype
);
1464 if (!rc
&& pwm
->pinentry_lcmessages
)
1465 rc
= pwmd_command (pwm
, NULL
, NULL
, NULL
, NULL
, "OPTION LC_MESSAGES=%s",
1466 pwm
->pinentry_lcmessages
);
1469 rc
= send_pinentry_timeout (pwm
);
1475 pwmd_socket_type (pwm_t
* pwm
, pwmd_socket_t
* result
)
1477 if (!pwm
|| !result
)
1478 return FINISH (GPG_ERR_INV_ARG
);
1480 *result
= PWMD_SOCKET_LOCAL
;
1483 return FINISH (GPG_ERR_INV_STATE
);
1487 *result
= PWMD_SOCKET_USER
;
1491 #if defined(WITH_SSH) || defined(WITH_GNUTLS)
1493 if (pwm
->tcp
&& pwm
->tcp
->ssh
)
1494 *result
= PWMD_SOCKET_SSH
;
1497 if (pwm
->tcp
&& pwm
->tcp
->tls
)
1498 *result
= PWMD_SOCKET_TLS
;
1505 disable_pinentry (pwm_t
*pwm
, int *disable
)
1508 #if defined(WITH_SSH) || defined(WITH_GNUTLS)
1509 int no_pinentry
= pwm
->disable_pinentry
|| pwm
->tcp
|| pwm
->local_pinentry
;
1511 int no_pinentry
= pwm
->disable_pinentry
|| pwm
->local_pinentry
;
1515 *disable
= no_pinentry
;
1517 if (pwm
->pinentry_disabled
&& no_pinentry
)
1519 else if (!pwm
->pinentry_disabled
&& !no_pinentry
)
1522 rc
= pwmd_command (pwm
, NULL
, NULL
, NULL
, NULL
, "OPTION disable-pinentry=%i",
1525 pwm
->pinentry_disabled
= no_pinentry
;
1531 pwmd_open (pwm_t
* pwm
, const char *filename
, pwmd_inquire_cb_t cb
,
1535 int no_pinentry
= 0;
1537 if (!pwm
|| !filename
|| !*filename
)
1538 return FINISH (GPG_ERR_INV_ARG
);
1541 return FINISH (GPG_ERR_INV_STATE
);
1543 command_start (pwm
);
1544 rc
= disable_pinentry (pwm
, &no_pinentry
);
1545 if (!rc
&& !no_pinentry
)
1546 rc
= send_pinentry_options (pwm
);
1550 pwm
->pinentry_try
= 0;
1551 pwmd_free (pwm
->filename
);
1552 pwm
->filename
= pwmd_strdup (filename
);
1556 rc
= pwmd_command (pwm
, NULL
, NULL
, cb
, data
, "OPEN %s%s",
1557 (pwm
->opts
& OPT_LOCK_ON_OPEN
) ? "--lock " : "",
1560 while (gpg_err_code (rc
) == GPG_ERR_BAD_PASSPHRASE
1561 && pwm
->pinentry_disabled
1562 && ++pwm
->pinentry_try
< pwm
->pinentry_tries
);
1564 pwm
->pinentry_try
= 0;
1568 pwmd_free (pwm
->filename
);
1569 pwm
->filename
= NULL
;
1573 pwmd_free (pwm
->passphrase_hint
);
1574 pwmd_free (pwm
->passphrase_info
);
1575 pwm
->passphrase_info
= pwm
->passphrase_hint
= NULL
;
1580 do_pwmd_save_passwd (pwm_t
* pwm
, const char *args
, pwmd_inquire_cb_t cb
,
1581 void *data
, int which
)
1584 int no_pinentry
= 0;
1587 return FINISH (GPG_ERR_INV_ARG
);
1589 return FINISH (GPG_ERR_INV_STATE
);
1591 command_start (pwm
);
1592 rc
= disable_pinentry (pwm
, &no_pinentry
);
1593 if (!rc
&& !no_pinentry
)
1594 rc
= send_pinentry_options (pwm
);
1598 if (which
== PWMD_WHICH_SAVE
)
1599 rc
= pwmd_command (pwm
, NULL
, NULL
, cb
, data
, "SAVE %s", args
? args
: "");
1600 else if (which
== PWMD_WHICH_PASSWD
)
1601 rc
= pwmd_command (pwm
, NULL
, NULL
, cb
, data
, "PASSWD %s", args
? args
: "");
1602 else if (which
== PWMD_WHICH_GENKEY
)
1603 rc
= pwmd_command (pwm
, NULL
, NULL
, cb
, data
, "GENKEY %s", args
? args
: "");
1606 pwmd_free (pwm
->passphrase_hint
);
1607 pwmd_free (pwm
->passphrase_info
);
1608 pwm
->passphrase_info
= pwm
->passphrase_hint
= NULL
;
1613 pwmd_passwd (pwm_t
* pwm
, const char *args
, pwmd_inquire_cb_t cb
, void *data
)
1615 return do_pwmd_save_passwd (pwm
, args
, cb
, data
, PWMD_WHICH_PASSWD
);
1619 pwmd_save (pwm_t
* pwm
, const char *args
, pwmd_inquire_cb_t cb
, void *data
)
1621 return do_pwmd_save_passwd (pwm
, args
, cb
, data
, PWMD_WHICH_SAVE
);
1625 pwmd_genkey (pwm_t
* pwm
, const char *args
, pwmd_inquire_cb_t cb
, void *data
)
1627 return do_pwmd_save_passwd (pwm
, args
, cb
, data
, PWMD_WHICH_GENKEY
);
1631 pwmd_get_set_opt (pwm_t
*pwm
, pwmd_option_t opt
, int get
, va_list ap
)
1635 char *arg1
, **charpp
;
1639 return GPG_ERR_INV_ARG
;
1641 command_start (pwm
);
1644 case PWMD_OPTION_SERVER_VERSION
:
1646 return GPG_ERR_NOT_SUPPORTED
;
1649 rc
= GPG_ERR_INV_STATE
;
1652 unsigned *u
= va_arg (ap
, unsigned *);
1657 case PWMD_OPTION_SIGPIPE
:
1660 intp
= va_arg (ap
, int *);
1661 *intp
= pwm
->opts
& OPT_SIGPIPE
? 1 : 0;
1665 n
= va_arg (ap
, int);
1668 rc
= GPG_ERR_INV_VALUE
;
1673 pwm
->opts
|= OPT_SIGPIPE
;
1675 pwm
->opts
&= ~OPT_SIGPIPE
;
1678 case PWMD_OPTION_LOCK_ON_OPEN
:
1681 intp
= va_arg (ap
, int *);
1682 *intp
= pwm
->opts
& OPT_LOCK_ON_OPEN
? 1 : 0;
1686 n
= va_arg (ap
, int);
1689 rc
= GPG_ERR_INV_VALUE
;
1694 pwm
->opts
|= OPT_LOCK_ON_OPEN
;
1696 pwm
->opts
&= ~OPT_LOCK_ON_OPEN
;
1699 case PWMD_OPTION_INQUIRE_TOTAL
:
1702 sizetp
= va_arg (ap
, size_t *);
1703 *sizetp
= pwm
->inquire_total
;
1707 pwm
->inquire_total
= va_arg (ap
, size_t);
1709 case PWMD_OPTION_STATUS_CB
:
1712 pwmd_status_cb_t
*cb
= va_arg (ap
, pwmd_status_cb_t
*);
1714 *cb
= pwm
->status_func
;
1718 pwm
->status_func
= va_arg (ap
, pwmd_status_cb_t
);
1720 case PWMD_OPTION_STATUS_DATA
:
1723 void **data
= va_arg (ap
, void **);
1725 *data
= pwm
->status_data
;
1729 pwm
->status_data
= va_arg (ap
, void *);
1731 case PWMD_OPTION_NO_PINENTRY
:
1734 intp
= va_arg (ap
, int *);
1735 *intp
= pwm
->disable_pinentry
;
1739 n
= va_arg (ap
, int);
1741 rc
= GPG_ERR_INV_VALUE
;
1743 pwm
->disable_pinentry
= n
;
1745 case PWMD_OPTION_LOCAL_PINENTRY
:
1748 intp
= va_arg (ap
, int *);
1749 *intp
= pwm
->local_pinentry
;
1753 n
= va_arg (ap
, int);
1755 rc
= GPG_ERR_INV_VALUE
;
1757 pwm
->local_pinentry
= n
;
1760 case PWMD_OPTION_PINENTRY_TIMEOUT
:
1763 intp
= va_arg (ap
, int *);
1764 *intp
= pwm
->pinentry_timeout
;
1768 n
= va_arg (ap
, int);
1770 rc
= GPG_ERR_INV_VALUE
;
1772 pwm
->pinentry_timeout
= n
;
1774 case PWMD_OPTION_PINENTRY_TRIES
:
1777 intp
= va_arg (ap
, int *);
1778 *intp
= pwm
->pinentry_tries
;
1782 n
= va_arg (ap
, int);
1784 rc
= GPG_ERR_INV_VALUE
;
1786 pwm
->pinentry_tries
= n
;
1788 case PWMD_OPTION_PINENTRY_PATH
:
1791 charpp
= va_arg (ap
, char **);
1792 *charpp
= pwm
->pinentry_path
;
1796 arg1
= va_arg (ap
, char *);
1797 pwmd_free (pwm
->pinentry_path
);
1798 pwm
->pinentry_path
= arg1
? _expand_homedir (arg1
, NULL
) : NULL
;
1800 case PWMD_OPTION_PINENTRY_TTY
:
1803 charpp
= va_arg (ap
, char **);
1804 *charpp
= pwm
->pinentry_tty
;
1808 arg1
= va_arg (ap
, char *);
1809 pwmd_free (pwm
->pinentry_tty
);
1810 pwm
->pinentry_tty
= arg1
? pwmd_strdup (arg1
) : NULL
;
1812 case PWMD_OPTION_PINENTRY_DISPLAY
:
1815 charpp
= va_arg (ap
, char **);
1816 *charpp
= pwm
->pinentry_display
;
1820 arg1
= va_arg (ap
, char *);
1821 pwmd_free (pwm
->pinentry_display
);
1822 pwm
->pinentry_display
= arg1
&& *arg1
? pwmd_strdup (arg1
) : NULL
;
1823 if (!pwm
->pinentry_display
)
1824 unsetenv ("DISPLAY");
1826 case PWMD_OPTION_PINENTRY_TERM
:
1829 charpp
= va_arg (ap
, char **);
1830 *charpp
= pwm
->pinentry_term
;
1834 arg1
= va_arg (ap
, char *);
1835 pwmd_free (pwm
->pinentry_term
);
1836 pwm
->pinentry_term
= arg1
&& *arg1
? pwmd_strdup (arg1
) : NULL
;
1837 if (pwm
->pinentry_term
)
1840 case PWMD_OPTION_PINENTRY_ERROR
:
1843 charpp
= va_arg (ap
, char **);
1844 *charpp
= pwm
->pinentry_error
;
1848 arg1
= va_arg (ap
, char *);
1849 pwmd_free (pwm
->pinentry_error
);
1850 if (pwm
->disable_pinentry
)
1851 pwm
->pinentry_error
= arg1
? pwmd_strdup (arg1
) : NULL
;
1853 pwm
->pinentry_error
= arg1
? _percent_escape (arg1
) : NULL
;
1855 case PWMD_OPTION_PINENTRY_PROMPT
:
1858 charpp
= va_arg (ap
, char **);
1859 *charpp
= pwm
->pinentry_prompt
;
1863 arg1
= va_arg (ap
, char *);
1864 pwmd_free (pwm
->pinentry_prompt
);
1865 if (pwm
->disable_pinentry
)
1866 pwm
->pinentry_prompt
= arg1
? pwmd_strdup (arg1
) : NULL
;
1868 pwm
->pinentry_prompt
= arg1
? _percent_escape (arg1
) : NULL
;
1870 case PWMD_OPTION_PINENTRY_DESC
:
1873 charpp
= va_arg (ap
, char **);
1874 *charpp
= pwm
->pinentry_desc
;
1878 arg1
= va_arg (ap
, char *);
1879 pwmd_free (pwm
->pinentry_desc
);
1880 if (pwm
->disable_pinentry
)
1881 pwm
->pinentry_desc
= arg1
? pwmd_strdup (arg1
) : NULL
;
1883 pwm
->pinentry_desc
= arg1
? _percent_escape (arg1
) : NULL
;
1885 case PWMD_OPTION_PINENTRY_LC_CTYPE
:
1888 charpp
= va_arg (ap
, char **);
1889 *charpp
= pwm
->pinentry_lcctype
;
1893 arg1
= va_arg (ap
, char *);
1894 pwmd_free (pwm
->pinentry_lcctype
);
1895 pwm
->pinentry_lcctype
= arg1
? pwmd_strdup (arg1
) : NULL
;
1897 case PWMD_OPTION_PINENTRY_LC_MESSAGES
:
1900 charpp
= va_arg (ap
, char **);
1901 *charpp
= pwm
->pinentry_lcmessages
;
1905 arg1
= va_arg (ap
, char *);
1906 pwmd_free (pwm
->pinentry_lcmessages
);
1907 pwm
->pinentry_lcmessages
= arg1
? pwmd_strdup (arg1
) : NULL
;
1909 case PWMD_OPTION_KNOWNHOST_CB
:
1912 pwmd_knownhost_cb_t
*cb
= va_arg (ap
, pwmd_knownhost_cb_t
*);
1918 pwm
->kh_cb
= va_arg (ap
, pwmd_knownhost_cb_t
);
1920 case PWMD_OPTION_KNOWNHOST_DATA
:
1923 void **data
= va_arg (ap
, void **);
1925 *data
= pwm
->kh_data
;
1929 pwm
->kh_data
= va_arg (ap
, void *);
1931 case PWMD_OPTION_SSH_AGENT
:
1934 intp
= va_arg (ap
, int *);
1935 *intp
= pwm
->use_agent
;
1939 n
= va_arg (ap
, int);
1941 rc
= GPG_ERR_INV_VALUE
;
1945 case PWMD_OPTION_SSH_PASSPHRASE
:
1947 return GPG_ERR_NOT_SUPPORTED
;
1949 pwmd_free (pwm
->ssh_passphrase
);
1950 pwm
->ssh_passphrase
= NULL
;
1951 arg1
= va_arg (ap
, char *);
1954 pwm
->ssh_passphrase
= pwmd_strdup (arg1
);
1955 if (!pwm
->ssh_passphrase
)
1956 return GPG_ERR_ENOMEM
;
1959 case PWMD_OPTION_SSH_NEEDS_PASSPHRASE
:
1962 intp
= va_arg (ap
, int *);
1963 *intp
= pwm
->needs_passphrase
;
1967 n
= va_arg (ap
, int);
1969 rc
= GPG_ERR_INV_VALUE
;
1971 pwm
->needs_passphrase
= n
;
1973 case PWMD_OPTION_TLS_VERIFY
:
1976 intp
= va_arg (ap
, int *);
1977 *intp
= pwm
->tls_verify
;
1981 n
= va_arg (ap
, int);
1983 rc
= GPG_ERR_INV_VALUE
;
1985 pwm
->tls_verify
= n
;
1987 case PWMD_OPTION_TLS_PRIORITY
:
1990 charpp
= va_arg (ap
, char **);
1991 *charpp
= pwm
->tls_priority
;
1995 pwmd_free (pwm
->tls_priority
);
1996 pwm
->tls_priority
= NULL
;
1997 arg1
= va_arg (ap
, char *);
2000 pwm
->tls_priority
= pwmd_strdup (arg1
);
2001 if (!pwm
->tls_priority
)
2002 rc
= GPG_ERR_ENOMEM
;
2005 case PWMD_OPTION_SOCKET_TIMEOUT
:
2008 intp
= va_arg (ap
, int *);
2009 *intp
= pwm
->socket_timeout
;
2013 n
= va_arg (ap
, int);
2016 rc
= GPG_ERR_INV_VALUE
;
2020 pwm
->socket_timeout
= n
;
2023 if (pwm
->tcp
&& pwm
->tcp
->ssh
&& pwm
->tcp
->ssh
->session
)
2025 pwm
->tcp
->ssh
->timeout
= pwm
->socket_timeout
;
2026 libssh2_session_set_timeout (pwm
->tcp
->ssh
->session
,
2027 pwm
->socket_timeout
* 1000);
2031 if (pwm
->tcp
&& pwm
->tcp
->tls
&& pwm
->tcp
->tls
->session
)
2032 pwm
->tcp
->tls
->timeout
= pwm
->socket_timeout
;
2035 case PWMD_OPTION_READ_CB
:
2038 pwmd_read_cb_t
*cb
= va_arg (ap
, pwmd_read_cb_t
*);
2043 pwm
->read_cb
= va_arg (ap
, pwmd_read_cb_t
);
2045 case PWMD_OPTION_READ_CB_DATA
:
2048 void **data
= va_arg (ap
, void **);
2050 *data
= pwm
->read_cb_data
;
2054 pwm
->read_cb_data
= va_arg (ap
, void *);
2056 case PWMD_OPTION_WRITE_CB
:
2059 pwmd_write_cb_t
*cb
= va_arg (ap
, pwmd_write_cb_t
*);
2061 *cb
= pwm
->write_cb
;
2064 pwm
->write_cb
= va_arg (ap
, pwmd_write_cb_t
);
2066 case PWMD_OPTION_WRITE_CB_DATA
:
2069 void **data
= va_arg (ap
, void **);
2071 *data
= pwm
->write_cb_data
;
2075 pwm
->write_cb_data
= va_arg (ap
, void *);
2077 case PWMD_OPTION_OVERRIDE_INQUIRE
:
2080 intp
= va_arg (ap
, int *);
2081 *intp
= pwm
->override_inquire
;
2085 n
= va_arg (ap
, int);
2087 rc
= GPG_ERR_INV_VALUE
;
2089 pwm
->override_inquire
= n
;
2092 rc
= GPG_ERR_UNKNOWN_OPTION
;
2100 pwmd_setopt (pwm_t
* pwm
, int opt
, ...)
2106 rc
= pwmd_get_set_opt (pwm
, opt
, 0, ap
);
2112 pwmd_getopt (pwm_t
*pwm
, int opt
, ...)
2118 rc
= pwmd_get_set_opt (pwm
, opt
, 1, ap
);
2124 set_rcdefaults (pwm_t
*pwm
, char *filename
)
2128 char *line
= NULL
, *p
;
2129 unsigned line_n
= 0;
2132 if (!filename
&& isatty (STDIN_FILENO
))
2135 int err
= ttyname_r (STDOUT_FILENO
, buf
, sizeof (buf
));
2139 rc
= pwmd_setopt (pwm
, PWMD_OPTION_PINENTRY_TTY
, buf
);
2143 if (getenv ("TERM"))
2145 rc
= pwmd_setopt (pwm
, PWMD_OPTION_PINENTRY_TERM
, getenv ("TERM"));
2152 if (!filename
&& getenv ("DISPLAY"))
2154 rc
= pwmd_setopt (pwm
, PWMD_OPTION_PINENTRY_DISPLAY
, getenv ("DISPLAY"));
2159 f
= _expand_homedir (filename
? filename
: (char *)"~/.config/libpwmd.conf",
2163 line
= pwmd_malloc (LINE_MAX
);
2166 fp
= fopen (f
, "r");
2175 rc
= GPG_ERR_ENOMEM
;
2178 rc
= GPG_ERR_ENOMEM
;
2187 while ((p
= fgets (line
, LINE_MAX
, fp
)))
2189 char name
[32] = {0}, val
[512] = {0};
2196 while (isspace (*p
))
2199 if (!*p
|| *p
== '#')
2202 if (p
[strlen (p
)-1] == '\n')
2203 p
[strlen (p
)-1] = 0;
2205 t
= strchr (p
, '=');
2208 fprintf(stderr
, N_("%s(%u): malformed line\n"), f
, line_n
);
2212 for (np
= name
; p
!= t
; p
++)
2214 if (++len
== sizeof (name
))
2223 while (isspace (*t
))
2226 while (isspace (*t
))
2229 strncpy (val
, t
, sizeof (val
)-1);
2231 if (!strcasecmp (name
, "pinentry-path"))
2232 rc
= pwmd_setopt (pwm
, PWMD_OPTION_PINENTRY_PATH
, val
);
2233 else if (!strcasecmp (name
, "pinentry-tries"))
2234 rc
= pwmd_setopt (pwm
, PWMD_OPTION_PINENTRY_TRIES
, atoi (val
));
2235 else if (!strcasecmp (name
, "pinentry-timeout"))
2236 rc
= pwmd_setopt (pwm
, PWMD_OPTION_PINENTRY_TIMEOUT
, atoi (val
));
2237 else if (!strcasecmp (name
, "no-pinentry"))
2238 rc
= pwmd_setopt (pwm
, PWMD_OPTION_NO_PINENTRY
, !!(atoi (val
)));
2239 else if (!strcasecmp (name
, "local-pinentry"))
2240 rc
= pwmd_setopt (pwm
, PWMD_OPTION_LOCAL_PINENTRY
, !!(atoi (val
)));
2241 else if (!strcasecmp (name
, "pinentry-display"))
2242 rc
= pwmd_setopt (pwm
, PWMD_OPTION_PINENTRY_DISPLAY
, val
);
2243 else if (!strcasecmp (name
, "pinentry-ttyname"))
2244 rc
= pwmd_setopt (pwm
, PWMD_OPTION_PINENTRY_TTY
, val
);
2245 else if (!strcasecmp (name
, "pinentry-ttytype"))
2246 rc
= pwmd_setopt (pwm
, PWMD_OPTION_PINENTRY_TERM
, val
);
2247 else if (!strcasecmp (name
, "pinentry-lc-messages"))
2248 rc
= pwmd_setopt (pwm
, PWMD_OPTION_PINENTRY_LC_MESSAGES
, val
);
2249 else if (!strcasecmp (name
, "pinentry-lc-ctype"))
2250 rc
= pwmd_setopt (pwm
, PWMD_OPTION_PINENTRY_LC_CTYPE
, val
);
2251 else if (!strcasecmp (name
, "no-ssh-agent"))
2252 rc
= pwmd_setopt (pwm
, PWMD_OPTION_SSH_AGENT
, !(atoi (val
)));
2253 else if (!strcasecmp (name
, "no-tls-verify"))
2254 rc
= pwmd_setopt (pwm
, PWMD_OPTION_TLS_VERIFY
, !(atoi (val
)));
2255 else if (!strcasecmp (name
, "tls-priority"))
2256 rc
= pwmd_setopt (pwm
, PWMD_OPTION_TLS_PRIORITY
, val
);
2257 else if (!strcasecmp (name
, "socket-timeout"))
2258 rc
= pwmd_setopt (pwm
, PWMD_OPTION_SOCKET_TIMEOUT
, atoi (val
));
2259 else if (!strcasecmp (name
, "no-lock"))
2260 rc
= pwmd_setopt (pwm
, PWMD_OPTION_LOCK_ON_OPEN
, !(atoi (val
)));
2261 else if (!strcasecmp (name
, "include"))
2262 rc
= set_rcdefaults (pwm
, val
);
2264 fprintf(stderr
, N_("%s(%u): invalid option '%s', ignored.\n"), f
,
2278 pwmd_new (const char *name
, pwm_t
** pwm
)
2280 pwm_t
*h
= pwmd_calloc (1, sizeof (pwm_t
));
2284 return FINISH (GPG_ERR_ENOMEM
);
2288 h
->name
= pwmd_strdup (name
);
2292 return FINISH (GPG_ERR_ENOMEM
);
2297 h
->pinentry_timeout
= -1;
2298 h
->current_pinentry_timeout
= -1;
2299 h
->pinentry_tries
= 3;
2300 #if defined(WITH_SSH) || defined(WITH_GNUTLS)
2301 h
->prot
= PWMD_IP_ANY
;
2302 h
->socket_timeout
= 120;
2305 h
->opts
|= OPT_LOCK_ON_OPEN
;
2307 rc
= set_rcdefaults (h
, NULL
);
2320 pwmd_free (void *ptr
)
2326 pwmd_malloc (size_t size
)
2328 return _xmalloc (size
);
2332 pwmd_calloc (size_t nmemb
, size_t size
)
2334 return _xcalloc (nmemb
, size
);
2338 pwmd_realloc (void *ptr
, size_t size
)
2340 return _xrealloc (ptr
, size
);
2344 pwmd_strdup (const char *str
)
2351 t
= _xmalloc ((len
+ 1) * sizeof (char));
2355 for (c
= 0; c
< len
; c
++)
2363 pwmd_strdup_printf (const char *fmt
, ...)
2374 len
= vsnprintf (NULL
, 0, fmt
, ap
);
2376 buf
= pwmd_malloc (++len
);
2378 vsnprintf (buf
, len
, fmt
, ap2
);
2385 pwmd_getpin (pwm_t
* pwm
, const char *filename
, char **result
,
2386 size_t * len
, pwmd_pinentry_t which
)
2388 #ifndef WITH_PINENTRY
2389 return FINISH (GPG_ERR_NOT_IMPLEMENTED
);
2393 command_start (pwm
);
2394 if (which
== PWMD_PINENTRY_CONFIRM
&& pwm
->disable_pinentry
)
2396 rc
= get_password (pwm
, NULL
, NULL
, which
, 1);
2400 rc
= _pwmd_getpin (pwm
, filename
, result
, len
, which
);
2408 return LIBPWMD_VERSION_STR
;
2416 #ifdef WITH_PINENTRY
2417 n
|= PWMD_FEATURE_PINENTRY
;
2420 n
|= PWMD_FEATURE_SSH
;
2423 n
|= PWMD_FEATURE_QUALITY
;
2426 n
|= PWMD_FEATURE_GNUTLS
;
2432 pwmd_fd (pwm_t
* pwm
, int *fd
)
2435 return FINISH (GPG_ERR_INV_ARG
);
2438 return FINISH (GPG_ERR_INV_STATE
);
2445 pwmd_set_pointer (pwm_t
*pwm
, void *data
)
2447 pwm
->user_data
= data
;
2451 pwmd_get_pointer (pwm_t
*pwm
)
2453 return pwm
->user_data
;
2457 pwmd_gnutls_error (pwm_t
*pwm
, const char **str
)
2464 if (str
&& pwm
&& pwm
->tls_error
)
2465 *str
= gnutls_strerror (pwm
->tls_error
);
2467 return pwm
? pwm
->tls_error
: 0;
2472 pwmd_cancel (pwm_t
*pwm
)
2475 return FINISH (GPG_ERR_INV_ARG
);
2477 #if defined(WITH_SSH) || defined(WITH_GNUTLS)
2478 if (pwm
->fd
== -1 && !pwm
->tcp
)
2482 return FINISH (GPG_ERR_INV_STATE
);
2484 /* Can only cancel the connection for the time being. */
2486 return FINISH (GPG_ERR_INV_STATE
);
2493 pwmd_test_quality (const char *str
, double *result
)
2495 #ifndef WITH_QUALITY
2498 return GPG_ERR_NOT_IMPLEMENTED
;
2501 return GPG_ERR_INV_ARG
;
2503 *result
= ZxcvbnMatch (str
, NULL
, NULL
);