2 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015
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 <netinet/in.h>
32 #include <sys/socket.h>
33 #include <arpa/inet.h>
47 #define INTERVAL 50000
48 #define TEST_TIMEOUT(pwm, ssh, n) do { \
50 n = LIBSSH2_ERROR_TIMEOUT; \
51 else if (n == LIBSSH2_ERROR_EAGAIN) \
53 if (ssh->elapsed.tv_usec + INTERVAL >= 1000000L) \
55 ssh->elapsed.tv_sec++; \
56 ssh->elapsed.tv_usec = 0; \
58 if (ssh->elapsed.tv_sec >= pwm->socket_timeout) \
59 n = LIBSSH2_ERROR_TIMEOUT; \
62 struct timeval tv = { 0, INTERVAL }; \
63 select (0, NULL, NULL, NULL, &tv); \
64 ssh->elapsed.tv_usec += INTERVAL; \
69 static gpg_error_t
ssh_connect_finalize (pwm_t
* pwm
);
70 static gpg_error_t
_setup_ssh_init (pwm_t
* pwm
);
71 static gpg_error_t
_setup_ssh_authlist (pwm_t
* pwm
);
72 static gpg_error_t
_setup_ssh_auth (pwm_t
* pwm
);
73 static gpg_error_t
_setup_ssh_channel (pwm_t
* pwm
);
74 static gpg_error_t
_setup_ssh_shell (pwm_t
* pwm
);
75 static gpg_error_t
_setup_ssh_agent (pwm_t
* pwm
);
78 close_agent (struct ssh_s
*ssh
)
85 libssh2_agent_disconnect (ssh
->agent
);
86 libssh2_agent_free (ssh
->agent
);
92 ssh_deinit (struct ssh_s
*ssh
)
98 /* Fixes error messages in the pwmd log. */
99 libssh2_channel_wait_closed (ssh
->channel
);
103 libssh2_channel_close (ssh
->channel
);
104 libssh2_channel_free (ssh
->channel
);
109 libssh2_knownhost_free (ssh
->kh
);
115 libssh2_session_disconnect (ssh
->session
, N_("libpwmd saying bye!"));
116 libssh2_session_free (ssh
->session
);
121 _free_ssh_conn (ssh
);
125 read_hook_ssh (struct ssh_s
*ssh
, assuan_fd_t fd
, void *data
, size_t len
)
127 ssize_t ret
= libssh2_channel_read (ssh
->channel
, data
, len
);
129 if (ret
== LIBSSH2_ERROR_TIMEOUT
)
141 write_hook_ssh (struct ssh_s
* ssh
, assuan_fd_t fd
, const void *data
,
146 /* libassuan cannot handle EAGAIN when doing writes. */
149 struct timeval tv
= { 0, INTERVAL
};
151 ret
= libssh2_channel_write (ssh
->channel
, data
, len
);
152 if (ret
== LIBSSH2_ERROR_EAGAIN
)
153 select (0, NULL
, NULL
, NULL
, &tv
);
155 while (ret
== LIBSSH2_ERROR_EAGAIN
);
157 if (ret
== LIBSSH2_ERROR_TIMEOUT
)
169 _free_ssh_conn (struct ssh_s
*ssh
)
175 pwmd_free (ssh
->username
);
176 ssh
->username
= NULL
;
177 pwmd_free (ssh
->known_hosts
);
178 ssh
->known_hosts
= NULL
;
179 pwmd_free (ssh
->identity
);
180 ssh
->identity
= NULL
;
181 pwmd_free (ssh
->identity_pub
);
182 ssh
->identity_pub
= NULL
;
183 pwmd_free (ssh
->hostkey
);
193 init_ssh (pwm_t
*pwm
, const char *host
, int port
, const char *identity
,
194 const char *user
, const char *known_hosts
, int use_agent
)
196 struct tcp_s
*conn
= pwm
->tcp
;
201 if (!host
|| !*host
|| (!use_agent
&& (!identity
|| !*identity
))
202 || (known_hosts
&& !*known_hosts
))
203 return GPG_ERR_INV_ARG
;
205 conn
= pwmd_calloc (1, sizeof (struct tcp_s
));
207 return gpg_error_from_errno (ENOMEM
);
209 conn
->ssh
= pwmd_calloc (1, sizeof (struct ssh_s
));
212 rc
= gpg_error_from_errno (ENOMEM
);
216 pwbuf
= _getpwuid (&pw
);
219 rc
= gpg_error_from_errno (errno
);
223 pwmd_free (conn
->ssh
->username
);
224 conn
->ssh
->username
= pwmd_strdup (user
? user
: pw
.pw_name
);
225 if (!conn
->ssh
->username
)
227 rc
= gpg_error_from_errno (ENOMEM
);
231 pwmd_free (conn
->ssh
->identity
);
232 conn
->ssh
->identity
= NULL
;
233 pwmd_free (conn
->ssh
->identity_pub
);
234 conn
->ssh
->identity_pub
= NULL
;
237 conn
->ssh
->identity
= _expand_homedir ((char *) identity
, &pw
);
239 if (!conn
->ssh
->identity
)
241 rc
= gpg_error_from_errno (ENOMEM
);
245 conn
->ssh
->identity_pub
= pwmd_strdup_printf ("%s.pub",
246 conn
->ssh
->identity
);
247 if (!conn
->ssh
->identity_pub
)
249 rc
= gpg_error_from_errno (ENOMEM
);
254 pwmd_free (conn
->ssh
->known_hosts
);
256 known_hosts
= "~/.ssh/known_hosts";
258 conn
->ssh
->known_hosts
= _expand_homedir ((char *) known_hosts
, &pw
);
259 if (!conn
->ssh
->known_hosts
)
261 rc
= gpg_error_from_errno (ENOMEM
);
267 conn
->host
= pwmd_strdup (host
);
270 rc
= gpg_error_from_errno (ENOMEM
);
287 ssh_malloc (size_t size
, void **data
)
289 return pwmd_malloc (size
);
293 ssh_free (void *ptr
, void **data
)
299 ssh_realloc (void *ptr
, size_t size
, void **data
)
301 return pwmd_realloc (ptr
, size
);
305 _setup_ssh_agent (pwm_t
* pwm
)
308 struct libssh2_agent_publickey
*identity
= NULL
;
309 struct libssh2_agent_publickey
*identity_prev
= NULL
;
311 n
= libssh2_agent_connect (pwm
->tcp
->ssh
->agent
);
313 return GPG_ERR_NO_AGENT
;
315 n
= libssh2_agent_list_identities (pwm
->tcp
->ssh
->agent
);
317 return GPG_ERR_KEYRING_OPEN
;
319 n
= libssh2_agent_get_identity (pwm
->tcp
->ssh
->agent
, &identity
,
322 return GPG_ERR_NO_SECKEY
;
324 return GPG_ERR_AGENT
;
330 n
= libssh2_agent_userauth (pwm
->tcp
->ssh
->agent
,
331 pwm
->tcp
->ssh
->username
, identity
);
332 TEST_TIMEOUT (pwm
, pwm
->tcp
->ssh
, n
);
334 while (n
== LIBSSH2_ERROR_EAGAIN
);
339 if (n
== LIBSSH2_ERROR_TIMEOUT
)
340 return pwm
->cancel
? GPG_ERR_CANCELED
: GPG_ERR_ETIMEDOUT
;
341 else if (n
&& n
!= LIBSSH2_ERROR_AUTHENTICATION_FAILED
)
342 return GPG_ERR_ASS_SERVER_START
;
344 identity_prev
= identity
;
345 n
= libssh2_agent_get_identity (pwm
->tcp
->ssh
->agent
, &identity
,
349 return GPG_ERR_NO_SECKEY
;
351 return GPG_ERR_AGENT
;
354 return _setup_ssh_channel (pwm
);
358 _setup_ssh_auth (pwm_t
* pwm
)
363 return _setup_ssh_agent (pwm
);
367 n
= libssh2_userauth_publickey_fromfile (pwm
->tcp
->ssh
->session
,
368 pwm
->tcp
->ssh
->username
,
369 pwm
->tcp
->ssh
->identity_pub
,
370 pwm
->tcp
->ssh
->identity
, NULL
);
371 TEST_TIMEOUT (pwm
, pwm
->tcp
->ssh
, n
);
373 while (n
== LIBSSH2_ERROR_EAGAIN
);
381 case LIBSSH2_ERROR_FILE
:
382 return GPG_ERR_UNUSABLE_SECKEY
;
383 case LIBSSH2_ERROR_TIMEOUT
:
384 return pwm
->cancel
? GPG_ERR_CANCELED
: GPG_ERR_ETIMEDOUT
;
385 case LIBSSH2_ERROR_AUTHENTICATION_FAILED
:
386 return GPG_ERR_BAD_SECKEY
;
388 return GPG_ERR_ASSUAN_SERVER_FAULT
;
392 return _setup_ssh_channel (pwm
);
396 _setup_ssh_authlist (pwm_t
* pwm
)
403 userauth
= libssh2_userauth_list (pwm
->tcp
->ssh
->session
,
404 pwm
->tcp
->ssh
->username
,
405 strlen (pwm
->tcp
->ssh
->username
));
406 n
= libssh2_session_last_errno (pwm
->tcp
->ssh
->session
);
407 TEST_TIMEOUT (pwm
, pwm
->tcp
->ssh
, n
);
409 while (!userauth
&& n
== LIBSSH2_ERROR_EAGAIN
);
411 if (n
&& n
!= LIBSSH2_ERROR_EAGAIN
)
414 return GPG_ERR_CANCELED
;
415 return n
== LIBSSH2_ERROR_TIMEOUT
? GPG_ERR_ETIMEDOUT
416 : GPG_ERR_ASSUAN_SERVER_FAULT
;
420 return GPG_ERR_BAD_PIN_METHOD
;
422 if (!userauth
|| !strstr (userauth
, "publickey"))
423 return GPG_ERR_BAD_PIN_METHOD
;
425 return _setup_ssh_auth (pwm
);
429 add_knownhost (pwm_t
* pwm
, const char *host
, const char *key
,
430 size_t len
, int type
, struct libssh2_knownhost
**dst
)
434 if (pwm
->tcp
->port
!= -1 && pwm
->tcp
->port
!= 22)
436 buf
= pwmd_malloc (256);
437 snprintf (buf
, 256, "[%s]:%i", host
, pwm
->tcp
->port
);
440 buf
= pwmd_strdup (host
);
442 char *tbuf
= pwmd_strdup_printf ("libpwmd-%li", time (NULL
));
443 libssh2_knownhost_addc (pwm
->tcp
->ssh
->kh
, buf
, NULL
, key
, len
, tbuf
,
444 strlen (tbuf
), type
, dst
);
450 check_known_hosts (pwm_t
* pwm
)
457 struct libssh2_knownhost
*kh
;
459 key
= libssh2_session_hostkey (pwm
->tcp
->ssh
->session
, &len
, &type
);
461 while (!libssh2_knownhost_get (pwm
->tcp
->ssh
->kh
, &kh
, NULL
))
462 libssh2_knownhost_del (pwm
->tcp
->ssh
->kh
, kh
);
464 n
= libssh2_knownhost_readfile (pwm
->tcp
->ssh
->kh
,
465 pwm
->tcp
->ssh
->known_hosts
,
466 LIBSSH2_KNOWNHOST_FILE_OPENSSH
);
468 if (n
< 0 && n
!= LIBSSH2_ERROR_FILE
)
469 return GPG_ERR_BAD_CERT
;
471 n
= libssh2_knownhost_checkp (pwm
->tcp
->ssh
->kh
, pwm
->tcp
->host
,
472 pwm
->tcp
->port
, (char *) key
, len
,
473 LIBSSH2_KNOWNHOST_TYPE_PLAIN
|
474 LIBSSH2_KNOWNHOST_KEYENC_RAW
,
475 &pwm
->tcp
->ssh
->hostent
);
478 LIBSSH2_HOSTKEY_TYPE_RSA
? LIBSSH2_KNOWNHOST_KEY_SSHRSA
:
479 LIBSSH2_KNOWNHOST_KEY_SSHDSS
;
483 case LIBSSH2_KNOWNHOST_CHECK_MATCH
:
485 case LIBSSH2_KNOWNHOST_CHECK_NOTFOUND
:
487 rc
= GPG_ERR_NOT_CONFIRMED
;
489 rc
= pwm
->kh_cb (pwm
->kh_data
, pwm
->tcp
->host
, key
, len
);
493 /* Adds both the IP and hostname. */
494 char *ip
= pwmd_malloc (255);
499 int n
= getnameinfo (pwm
->tcp
->addr
->ai_addr
,
500 pwm
->tcp
->addr
->ai_family
== AF_INET
?
501 sizeof (struct sockaddr_in
) : sizeof (struct
503 ip
, 255, NULL
, 0, NI_NUMERICHOST
);
511 if (strcmp (pwm
->tcp
->host
, ip
))
512 tmp
= pwmd_strdup_printf ("%s,%s", pwm
->tcp
->host
, ip
);
514 tmp
= pwmd_strdup (ip
);
517 add_knownhost (pwm
, tmp
, key
, len
,
518 LIBSSH2_KNOWNHOST_TYPE_PLAIN
|
519 LIBSSH2_KNOWNHOST_KEYENC_RAW
| type
,
520 &pwm
->tcp
->ssh
->hostent
);
526 /* It's not an error if writing the new host file fails since
527 * there isn't a way to notify the user. The hostkey is still
529 char *tmp
= tempnam (NULL
, "khost");
534 if (!libssh2_knownhost_writefile (pwm
->tcp
->ssh
->kh
, tmp
,
535 LIBSSH2_KNOWNHOST_FILE_OPENSSH
))
538 FILE *ifp
, *ofp
= NULL
;
540 buf
= pwmd_malloc (LINE_MAX
);
548 ifp
= fopen (tmp
, "r");
552 ofp
= fopen (pwm
->tcp
->ssh
->known_hosts
, "w+");
556 while ((fgets (buf
, LINE_MAX
, ifp
)))
558 if (fprintf (ofp
, "%s", buf
) < 0)
574 case LIBSSH2_KNOWNHOST_CHECK_MISMATCH
:
575 case LIBSSH2_KNOWNHOST_CHECK_FAILURE
:
576 return GPG_ERR_BAD_CERT
;
583 verify_hostkey (pwm_t
* pwm
)
589 if (!pwm
->tcp
->ssh
->kh
)
590 pwm
->tcp
->ssh
->kh
= libssh2_knownhost_init (pwm
->tcp
->ssh
->session
);
591 if (!pwm
->tcp
->ssh
->kh
)
592 return GPG_ERR_ENOMEM
;
594 rc
= check_known_hosts (pwm
);
598 buf
= pwmd_malloc (LINE_MAX
);
600 return gpg_error_from_errno (ENOMEM
);
602 if (libssh2_knownhost_writeline (pwm
->tcp
->ssh
->kh
, pwm
->tcp
->ssh
->hostent
,
603 buf
, LINE_MAX
, &outlen
,
604 LIBSSH2_KNOWNHOST_FILE_OPENSSH
))
607 return gpg_error_from_errno (ENOMEM
);
610 if (pwm
->tcp
->ssh
->hostkey
)
611 pwmd_free (pwm
->tcp
->ssh
->hostkey
);
612 pwm
->tcp
->ssh
->hostkey
= buf
;
614 return _setup_ssh_authlist (pwm
);
618 _setup_ssh_channel (pwm_t
* pwm
)
623 close_agent (pwm
->tcp
->ssh
);
627 pwm
->tcp
->ssh
->channel
=
628 libssh2_channel_open_session (pwm
->tcp
->ssh
->session
);
630 n
= libssh2_session_last_errno (pwm
->tcp
->ssh
->session
);
631 TEST_TIMEOUT (pwm
, pwm
->tcp
->ssh
, n
);
633 while (!pwm
->tcp
->ssh
->channel
&& n
== LIBSSH2_ERROR_EAGAIN
);
635 if (!pwm
->tcp
->ssh
->channel
|| (n
&& n
!= LIBSSH2_ERROR_EAGAIN
))
637 rc
= GPG_ERR_ASS_SERVER_START
;
640 return GPG_ERR_CANCELED
;
641 return n
== LIBSSH2_ERROR_TIMEOUT
? GPG_ERR_ETIMEDOUT
: rc
;
644 return _setup_ssh_shell (pwm
);
648 _setup_ssh_shell (pwm_t
* pwm
)
655 n
= libssh2_channel_shell (pwm
->tcp
->ssh
->channel
);
656 TEST_TIMEOUT (pwm
, pwm
->tcp
->ssh
, n
);
658 while (n
== LIBSSH2_ERROR_EAGAIN
);
662 rc
= GPG_ERR_ASS_SERVER_START
;
664 return GPG_ERR_CANCELED
;
665 return n
== LIBSSH2_ERROR_TIMEOUT
? GPG_ERR_ETIMEDOUT
: rc
;
668 return ssh_connect_finalize (pwm
);
672 ssh_connect_finalize (pwm_t
* pwm
)
674 libssh2_session_set_blocking (pwm
->tcp
->ssh
->session
, 1);
675 return assuan_socket_connect_fd (pwm
->ctx
, pwm
->fd
, 0);
679 _setup_ssh_init (pwm_t
* pwm
)
685 n
= libssh2_session_handshake (pwm
->tcp
->ssh
->session
, pwm
->fd
);
686 TEST_TIMEOUT (pwm
, pwm
->tcp
->ssh
, n
);
688 while (n
== LIBSSH2_ERROR_EAGAIN
);
693 return GPG_ERR_CANCELED
;
694 return n
== LIBSSH2_ERROR_TIMEOUT
? GPG_ERR_ETIMEDOUT
695 : GPG_ERR_ASSUAN_SERVER_FAULT
;
698 return verify_hostkey (pwm
);
702 _setup_ssh_session (pwm_t
* pwm
)
706 if (!pwm
->tcp
->ssh
->session
)
708 pwm
->tcp
->ssh
->session
= libssh2_session_init_ex (ssh_malloc
, ssh_free
,
710 if (!pwm
->tcp
->ssh
->session
)
711 return GPG_ERR_ENOMEM
;
713 libssh2_session_flag (pwm
->tcp
->ssh
->session
, LIBSSH2_FLAG_COMPRESS
, 1);
714 libssh2_session_set_timeout (pwm
->tcp
->ssh
->session
,
715 pwm
->socket_timeout
* 1000);
718 pwm
->tcp
->ssh
->agent
= libssh2_agent_init (pwm
->tcp
->ssh
->session
);
721 if (!pwm
->tcp
->ssh
->session
)
722 return GPG_ERR_ENOMEM
;
724 libssh2_session_set_blocking (pwm
->tcp
->ssh
->session
, 0);
725 rc
= _setup_ssh_init (pwm
);
734 _do_ssh_connect (pwm_t
* pwm
, const char *host
, int port
,
735 const char *identity
, const char *user
,
736 const char *known_hosts
)
741 return GPG_ERR_INV_ARG
;
743 rc
= init_ssh (pwm
, host
, port
, identity
, user
, known_hosts
, pwm
->use_agent
);
747 rc
= tcp_connect_common (pwm
);
751 pwm
->tcp
->fd
= pwm
->tcp
->ssh
->fd
= &pwm
->fd
;
752 pwm
->tcp
->ssh
->timeout
= pwm
->socket_timeout
;
753 rc
= _setup_ssh_session (pwm
);
763 * ssh[46]://[username@][hostname][:port]
765 * Any missing parameters are checked for in init_ssh().
768 _parse_ssh_url (const char *str
, char **host
, int *port
, char **user
)
774 *host
= *user
= NULL
;
776 p
= strrchr (str
, '@');
779 len
= strlen (str
) - strlen (p
) + 1;
780 *user
= pwmd_malloc (len
);
782 return gpg_error_from_errno (ENOMEM
);
784 snprintf (*user
, len
, "%s", str
);
790 rc
= parse_hostname_common (p
, host
, port
);