1 /*
2  * Copyright (C) the libgit2 contributors. All rights reserved.
3  *
4  * This file is part of libgit2, distributed under the GNU GPL v2 with
5  * a Linking Exception. For full terms see the included COPYING file.
6  */
7 
8 #include "ssh.h"
9 
10 #ifdef GIT_SSH
11 #include <libssh2.h>
12 #endif
13 
14 #include "runtime.h"
15 #include "git2.h"
16 #include "buffer.h"
17 #include "net.h"
18 #include "netops.h"
19 #include "smart.h"
20 #include "streams/socket.h"
21 
22 #include "git2/credential.h"
23 #include "git2/sys/credential.h"
24 
25 #ifdef GIT_SSH
26 
27 #define OWNING_SUBTRANSPORT(s) ((ssh_subtransport *)(s)->parent.subtransport)
28 
29 static const char *ssh_prefixes[] = { "ssh://", "ssh+git://", "git+ssh://" };
30 
31 static const char cmd_uploadpack[] = "git-upload-pack";
32 static const char cmd_receivepack[] = "git-receive-pack";
33 
34 typedef struct {
35 	git_smart_subtransport_stream parent;
36 	git_stream *io;
37 	LIBSSH2_SESSION *session;
38 	LIBSSH2_CHANNEL *channel;
39 	const char *cmd;
40 	char *url;
41 	unsigned sent_command : 1;
42 } ssh_stream;
43 
44 typedef struct {
45 	git_smart_subtransport parent;
46 	transport_smart *owner;
47 	ssh_stream *current_stream;
48 	git_credential *cred;
49 	char *cmd_uploadpack;
50 	char *cmd_receivepack;
51 } ssh_subtransport;
52 
53 static int list_auth_methods(int *out, LIBSSH2_SESSION *session, const char *username);
54 
ssh_error(LIBSSH2_SESSION * session,const char * errmsg)55 static void ssh_error(LIBSSH2_SESSION *session, const char *errmsg)
56 {
57 	char *ssherr;
58 	libssh2_session_last_error(session, &ssherr, NULL, 0);
59 
60 	git_error_set(GIT_ERROR_SSH, "%s: %s", errmsg, ssherr);
61 }
62 
63 /*
64  * Create a git protocol request.
65  *
66  * For example: git-upload-pack '/libgit2/libgit2'
67  */
gen_proto(git_buf * request,const char * cmd,const char * url)68 static int gen_proto(git_buf *request, const char *cmd, const char *url)
69 {
70 	const char *repo;
71 	int len;
72 	size_t i;
73 
74 	for (i = 0; i < ARRAY_SIZE(ssh_prefixes); ++i) {
75 		const char *p = ssh_prefixes[i];
76 
77 		if (!git__prefixcmp(url, p)) {
78 			url = url + strlen(p);
79 			repo = strchr(url, '/');
80 			if (repo && repo[1] == '~')
81 				++repo;
82 
83 			goto done;
84 		}
85 	}
86 	repo = strchr(url, ':');
87 	if (repo) repo++;
88 
89 done:
90 	if (!repo) {
91 		git_error_set(GIT_ERROR_NET, "malformed git protocol URL");
92 		return -1;
93 	}
94 
95 	len = strlen(cmd) + 1 /* Space */ + 1 /* Quote */ + strlen(repo) + 1 /* Quote */ + 1;
96 
97 	git_buf_grow(request, len);
98 	git_buf_puts(request, cmd);
99 	git_buf_puts(request, " '");
100 	git_buf_decode_percent(request, repo, strlen(repo));
101 	git_buf_puts(request, "'");
102 
103 	if (git_buf_oom(request))
104 		return -1;
105 
106 	return 0;
107 }
108 
send_command(ssh_stream * s)109 static int send_command(ssh_stream *s)
110 {
111 	int error;
112 	git_buf request = GIT_BUF_INIT;
113 
114 	error = gen_proto(&request, s->cmd, s->url);
115 	if (error < 0)
116 		goto cleanup;
117 
118 	error = libssh2_channel_exec(s->channel, request.ptr);
119 	if (error < LIBSSH2_ERROR_NONE) {
120 		ssh_error(s->session, "SSH could not execute request");
121 		goto cleanup;
122 	}
123 
124 	s->sent_command = 1;
125 
126 cleanup:
127 	git_buf_dispose(&request);
128 	return error;
129 }
130 
ssh_stream_read(git_smart_subtransport_stream * stream,char * buffer,size_t buf_size,size_t * bytes_read)131 static int ssh_stream_read(
132 	git_smart_subtransport_stream *stream,
133 	char *buffer,
134 	size_t buf_size,
135 	size_t *bytes_read)
136 {
137 	int rc;
138 	ssh_stream *s = GIT_CONTAINER_OF(stream, ssh_stream, parent);
139 
140 	*bytes_read = 0;
141 
142 	if (!s->sent_command && send_command(s) < 0)
143 		return -1;
144 
145 	if ((rc = libssh2_channel_read(s->channel, buffer, buf_size)) < LIBSSH2_ERROR_NONE) {
146 		ssh_error(s->session, "SSH could not read data");
147 		return -1;
148 	}
149 
150 	/*
151 	 * If we can't get anything out of stdout, it's typically a
152 	 * not-found error, so read from stderr and signal EOF on
153 	 * stderr.
154 	 */
155 	if (rc == 0) {
156 		if ((rc = libssh2_channel_read_stderr(s->channel, buffer, buf_size)) > 0) {
157 			git_error_set(GIT_ERROR_SSH, "%*s", rc, buffer);
158 			return GIT_EEOF;
159 		} else if (rc < LIBSSH2_ERROR_NONE) {
160 			ssh_error(s->session, "SSH could not read stderr");
161 			return -1;
162 		}
163 	}
164 
165 
166 	*bytes_read = rc;
167 
168 	return 0;
169 }
170 
ssh_stream_write(git_smart_subtransport_stream * stream,const char * buffer,size_t len)171 static int ssh_stream_write(
172 	git_smart_subtransport_stream *stream,
173 	const char *buffer,
174 	size_t len)
175 {
176 	ssh_stream *s = GIT_CONTAINER_OF(stream, ssh_stream, parent);
177 	size_t off = 0;
178 	ssize_t ret = 0;
179 
180 	if (!s->sent_command && send_command(s) < 0)
181 		return -1;
182 
183 	do {
184 		ret = libssh2_channel_write(s->channel, buffer + off, len - off);
185 		if (ret < 0)
186 			break;
187 
188 		off += ret;
189 
190 	} while (off < len);
191 
192 	if (ret < 0) {
193 		ssh_error(s->session, "SSH could not write data");
194 		return -1;
195 	}
196 
197 	return 0;
198 }
199 
ssh_stream_free(git_smart_subtransport_stream * stream)200 static void ssh_stream_free(git_smart_subtransport_stream *stream)
201 {
202 	ssh_stream *s = GIT_CONTAINER_OF(stream, ssh_stream, parent);
203 	ssh_subtransport *t;
204 
205 	if (!stream)
206 		return;
207 
208 	t = OWNING_SUBTRANSPORT(s);
209 	t->current_stream = NULL;
210 
211 	if (s->channel) {
212 		libssh2_channel_close(s->channel);
213 		libssh2_channel_free(s->channel);
214 		s->channel = NULL;
215 	}
216 
217 	if (s->session) {
218 		libssh2_session_disconnect(s->session, "closing transport");
219 		libssh2_session_free(s->session);
220 		s->session = NULL;
221 	}
222 
223 	if (s->io) {
224 		git_stream_close(s->io);
225 		git_stream_free(s->io);
226 		s->io = NULL;
227 	}
228 
229 	git__free(s->url);
230 	git__free(s);
231 }
232 
ssh_stream_alloc(ssh_subtransport * t,const char * url,const char * cmd,git_smart_subtransport_stream ** stream)233 static int ssh_stream_alloc(
234 	ssh_subtransport *t,
235 	const char *url,
236 	const char *cmd,
237 	git_smart_subtransport_stream **stream)
238 {
239 	ssh_stream *s;
240 
241 	GIT_ASSERT_ARG(stream);
242 
243 	s = git__calloc(sizeof(ssh_stream), 1);
244 	GIT_ERROR_CHECK_ALLOC(s);
245 
246 	s->parent.subtransport = &t->parent;
247 	s->parent.read = ssh_stream_read;
248 	s->parent.write = ssh_stream_write;
249 	s->parent.free = ssh_stream_free;
250 
251 	s->cmd = cmd;
252 
253 	s->url = git__strdup(url);
254 	if (!s->url) {
255 		git__free(s);
256 		return -1;
257 	}
258 
259 	*stream = &s->parent;
260 	return 0;
261 }
262 
git_ssh_extract_url_parts(git_net_url * urldata,const char * url)263 static int git_ssh_extract_url_parts(
264 	git_net_url *urldata,
265 	const char *url)
266 {
267 	char *colon, *at;
268 	const char *start;
269 
270 	colon = strchr(url, ':');
271 
272 
273 	at = strchr(url, '@');
274 	if (at) {
275 		start = at + 1;
276 		urldata->username = git__substrdup(url, at - url);
277 		GIT_ERROR_CHECK_ALLOC(urldata->username);
278 	} else {
279 		start = url;
280 		urldata->username = NULL;
281 	}
282 
283 	if (colon == NULL || (colon < start)) {
284 		git_error_set(GIT_ERROR_NET, "malformed URL");
285 		return -1;
286 	}
287 
288 	urldata->host = git__substrdup(start, colon - start);
289 	GIT_ERROR_CHECK_ALLOC(urldata->host);
290 
291 	return 0;
292 }
293 
ssh_agent_auth(LIBSSH2_SESSION * session,git_credential_ssh_key * c)294 static int ssh_agent_auth(LIBSSH2_SESSION *session, git_credential_ssh_key *c) {
295 	int rc = LIBSSH2_ERROR_NONE;
296 
297 	struct libssh2_agent_publickey *curr, *prev = NULL;
298 
299 	LIBSSH2_AGENT *agent = libssh2_agent_init(session);
300 
301 	if (agent == NULL)
302 		return -1;
303 
304 	rc = libssh2_agent_connect(agent);
305 
306 	if (rc != LIBSSH2_ERROR_NONE)
307 		goto shutdown;
308 
309 	rc = libssh2_agent_list_identities(agent);
310 
311 	if (rc != LIBSSH2_ERROR_NONE)
312 		goto shutdown;
313 
314 	while (1) {
315 		rc = libssh2_agent_get_identity(agent, &curr, prev);
316 
317 		if (rc < 0)
318 			goto shutdown;
319 
320 		/* rc is set to 1 whenever the ssh agent ran out of keys to check.
321 		 * Set the error code to authentication failure rather than erroring
322 		 * out with an untranslatable error code.
323 		 */
324 		if (rc == 1) {
325 			rc = LIBSSH2_ERROR_AUTHENTICATION_FAILED;
326 			goto shutdown;
327 		}
328 
329 		rc = libssh2_agent_userauth(agent, c->username, curr);
330 
331 		if (rc == 0)
332 			break;
333 
334 		prev = curr;
335 	}
336 
337 shutdown:
338 
339 	if (rc != LIBSSH2_ERROR_NONE)
340 		ssh_error(session, "error authenticating");
341 
342 	libssh2_agent_disconnect(agent);
343 	libssh2_agent_free(agent);
344 
345 	return rc;
346 }
347 
_git_ssh_authenticate_session(LIBSSH2_SESSION * session,git_credential * cred)348 static int _git_ssh_authenticate_session(
349 	LIBSSH2_SESSION *session,
350 	git_credential *cred)
351 {
352 	int rc;
353 
354 	do {
355 		git_error_clear();
356 		switch (cred->credtype) {
357 		case GIT_CREDENTIAL_USERPASS_PLAINTEXT: {
358 			git_credential_userpass_plaintext *c = (git_credential_userpass_plaintext *)cred;
359 			rc = libssh2_userauth_password(session, c->username, c->password);
360 			break;
361 		}
362 		case GIT_CREDENTIAL_SSH_KEY: {
363 			git_credential_ssh_key *c = (git_credential_ssh_key *)cred;
364 
365 			if (c->privatekey)
366 				rc = libssh2_userauth_publickey_fromfile(
367 					session, c->username, c->publickey,
368 					c->privatekey, c->passphrase);
369 			else
370 				rc = ssh_agent_auth(session, c);
371 
372 			break;
373 		}
374 		case GIT_CREDENTIAL_SSH_CUSTOM: {
375 			git_credential_ssh_custom *c = (git_credential_ssh_custom *)cred;
376 
377 			rc = libssh2_userauth_publickey(
378 				session, c->username, (const unsigned char *)c->publickey,
379 				c->publickey_len, c->sign_callback, &c->payload);
380 			break;
381 		}
382 		case GIT_CREDENTIAL_SSH_INTERACTIVE: {
383 			void **abstract = libssh2_session_abstract(session);
384 			git_credential_ssh_interactive *c = (git_credential_ssh_interactive *)cred;
385 
386 			/* ideally, we should be able to set this by calling
387 			 * libssh2_session_init_ex() instead of libssh2_session_init().
388 			 * libssh2's API is inconsistent here i.e. libssh2_userauth_publickey()
389 			 * allows you to pass the `abstract` as part of the call, whereas
390 			 * libssh2_userauth_keyboard_interactive() does not!
391 			 *
392 			 * The only way to set the `abstract` pointer is by calling
393 			 * libssh2_session_abstract(), which will replace the existing
394 			 * pointer as is done below. This is safe for now (at time of writing),
395 			 * but may not be valid in future.
396 			 */
397 			*abstract = c->payload;
398 
399 			rc = libssh2_userauth_keyboard_interactive(
400 				session, c->username, c->prompt_callback);
401 			break;
402 		}
403 #ifdef GIT_SSH_MEMORY_CREDENTIALS
404 		case GIT_CREDENTIAL_SSH_MEMORY: {
405 			git_credential_ssh_key *c = (git_credential_ssh_key *)cred;
406 
407 			GIT_ASSERT(c->username);
408 			GIT_ASSERT(c->privatekey);
409 
410 			rc = libssh2_userauth_publickey_frommemory(
411 				session,
412 				c->username,
413 				strlen(c->username),
414 				c->publickey,
415 				c->publickey ? strlen(c->publickey) : 0,
416 				c->privatekey,
417 				strlen(c->privatekey),
418 				c->passphrase);
419 			break;
420 		}
421 #endif
422 		default:
423 			rc = LIBSSH2_ERROR_AUTHENTICATION_FAILED;
424 		}
425 	} while (LIBSSH2_ERROR_EAGAIN == rc || LIBSSH2_ERROR_TIMEOUT == rc);
426 
427 	if (rc == LIBSSH2_ERROR_PASSWORD_EXPIRED ||
428 		rc == LIBSSH2_ERROR_AUTHENTICATION_FAILED ||
429 		rc == LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED)
430 			return GIT_EAUTH;
431 
432 	if (rc != LIBSSH2_ERROR_NONE) {
433 		if (!git_error_last())
434 			ssh_error(session, "Failed to authenticate SSH session");
435 		return -1;
436 	}
437 
438 	return 0;
439 }
440 
request_creds(git_credential ** out,ssh_subtransport * t,const char * user,int auth_methods)441 static int request_creds(git_credential **out, ssh_subtransport *t, const char *user, int auth_methods)
442 {
443 	int error, no_callback = 0;
444 	git_credential *cred = NULL;
445 
446 	if (!t->owner->cred_acquire_cb) {
447 		no_callback = 1;
448 	} else {
449 		error = t->owner->cred_acquire_cb(&cred, t->owner->url, user, auth_methods,
450 						  t->owner->cred_acquire_payload);
451 
452 		if (error == GIT_PASSTHROUGH) {
453 			no_callback = 1;
454 		} else if (error < 0) {
455 			return error;
456 		} else if (!cred) {
457 			git_error_set(GIT_ERROR_SSH, "callback failed to initialize SSH credentials");
458 			return -1;
459 		}
460 	}
461 
462 	if (no_callback) {
463 		git_error_set(GIT_ERROR_SSH, "authentication required but no callback set");
464 		return -1;
465 	}
466 
467 	if (!(cred->credtype & auth_methods)) {
468 		cred->free(cred);
469 		git_error_set(GIT_ERROR_SSH, "callback returned unsupported credentials type");
470 		return -1;
471 	}
472 
473 	*out = cred;
474 
475 	return 0;
476 }
477 
_git_ssh_session_create(LIBSSH2_SESSION ** session,git_stream * io)478 static int _git_ssh_session_create(
479 	LIBSSH2_SESSION** session,
480 	git_stream *io)
481 {
482 	int rc = 0;
483 	LIBSSH2_SESSION* s;
484 	git_socket_stream *socket = GIT_CONTAINER_OF(io, git_socket_stream, parent);
485 
486 	GIT_ASSERT_ARG(session);
487 
488 	s = libssh2_session_init();
489 	if (!s) {
490 		git_error_set(GIT_ERROR_NET, "failed to initialize SSH session");
491 		return -1;
492 	}
493 
494 	do {
495 		rc = libssh2_session_handshake(s, socket->s);
496 	} while (LIBSSH2_ERROR_EAGAIN == rc || LIBSSH2_ERROR_TIMEOUT == rc);
497 
498 	if (rc != LIBSSH2_ERROR_NONE) {
499 		ssh_error(s, "failed to start SSH session");
500 		libssh2_session_free(s);
501 		return -1;
502 	}
503 
504 	libssh2_session_set_blocking(s, 1);
505 
506 	*session = s;
507 
508 	return 0;
509 }
510 
511 #define SSH_DEFAULT_PORT "22"
512 
_git_ssh_setup_conn(ssh_subtransport * t,const char * url,const char * cmd,git_smart_subtransport_stream ** stream)513 static int _git_ssh_setup_conn(
514 	ssh_subtransport *t,
515 	const char *url,
516 	const char *cmd,
517 	git_smart_subtransport_stream **stream)
518 {
519 	git_net_url urldata = GIT_NET_URL_INIT;
520 	int auth_methods, error = 0;
521 	size_t i;
522 	ssh_stream *s;
523 	git_credential *cred = NULL;
524 	LIBSSH2_SESSION* session=NULL;
525 	LIBSSH2_CHANNEL* channel=NULL;
526 
527 	t->current_stream = NULL;
528 
529 	*stream = NULL;
530 	if (ssh_stream_alloc(t, url, cmd, stream) < 0)
531 		return -1;
532 
533 	s = (ssh_stream *)*stream;
534 	s->session = NULL;
535 	s->channel = NULL;
536 
537 	for (i = 0; i < ARRAY_SIZE(ssh_prefixes); ++i) {
538 		const char *p = ssh_prefixes[i];
539 
540 		if (!git__prefixcmp(url, p)) {
541 			if ((error = git_net_url_parse(&urldata, url)) < 0)
542 				goto done;
543 
544 			goto post_extract;
545 		}
546 	}
547 	if ((error = git_ssh_extract_url_parts(&urldata, url)) < 0)
548 		goto done;
549 
550 	if (urldata.port == NULL)
551 		urldata.port = git__strdup(SSH_DEFAULT_PORT);
552 
553 	GIT_ERROR_CHECK_ALLOC(urldata.port);
554 
555 post_extract:
556 	if ((error = git_socket_stream_new(&s->io, urldata.host, urldata.port)) < 0 ||
557 	    (error = git_stream_connect(s->io)) < 0)
558 		goto done;
559 
560 	if ((error = _git_ssh_session_create(&session, s->io)) < 0)
561 		goto done;
562 
563 	if (t->owner->certificate_check_cb != NULL) {
564 		git_cert_hostkey cert = {{ 0 }}, *cert_ptr;
565 		const char *key;
566 		size_t cert_len;
567 		int cert_type;
568 
569 		cert.parent.cert_type = GIT_CERT_HOSTKEY_LIBSSH2;
570 
571 		key = libssh2_session_hostkey(session, &cert_len, &cert_type);
572 		if (key != NULL) {
573 			cert.type |= GIT_CERT_SSH_RAW;
574 			cert.hostkey = key;
575 			cert.hostkey_len = cert_len;
576 			switch (cert_type) {
577 				case LIBSSH2_HOSTKEY_TYPE_RSA:
578 					cert.raw_type = GIT_CERT_SSH_RAW_TYPE_RSA;
579 					break;
580 				case LIBSSH2_HOSTKEY_TYPE_DSS:
581 					cert.raw_type = GIT_CERT_SSH_RAW_TYPE_DSS;
582 					break;
583 
584 #ifdef LIBSSH2_HOSTKEY_TYPE_ECDSA_256
585 				case LIBSSH2_HOSTKEY_TYPE_ECDSA_256:
586 					cert.raw_type = GIT_CERT_SSH_RAW_TYPE_KEY_ECDSA_256;
587 					break;
588 				case LIBSSH2_HOSTKEY_TYPE_ECDSA_384:
589 					cert.raw_type = GIT_CERT_SSH_RAW_TYPE_KEY_ECDSA_384;
590 					break;
591 				case LIBSSH2_KNOWNHOST_KEY_ECDSA_521:
592 					cert.raw_type = GIT_CERT_SSH_RAW_TYPE_KEY_ECDSA_521;
593 					break;
594 #endif
595 
596 #ifdef LIBSSH2_HOSTKEY_TYPE_ED25519
597 				case LIBSSH2_HOSTKEY_TYPE_ED25519:
598 					cert.raw_type = GIT_CERT_SSH_RAW_TYPE_KEY_ED25519;
599 					break;
600 #endif
601 				default:
602 					cert.raw_type = GIT_CERT_SSH_RAW_TYPE_UNKNOWN;
603 			}
604 		}
605 
606 #ifdef LIBSSH2_HOSTKEY_HASH_SHA256
607 		key = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA256);
608 		if (key != NULL) {
609 			cert.type |= GIT_CERT_SSH_SHA256;
610 			memcpy(&cert.hash_sha256, key, 32);
611 		}
612 #endif
613 
614 		key = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
615 		if (key != NULL) {
616 			cert.type |= GIT_CERT_SSH_SHA1;
617 			memcpy(&cert.hash_sha1, key, 20);
618 		}
619 
620 		key = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_MD5);
621 		if (key != NULL) {
622 			cert.type |= GIT_CERT_SSH_MD5;
623 			memcpy(&cert.hash_md5, key, 16);
624 		}
625 
626 		if (cert.type == 0) {
627 			git_error_set(GIT_ERROR_SSH, "unable to get the host key");
628 			error = -1;
629 			goto done;
630 		}
631 
632 		/* We don't currently trust any hostkeys */
633 		git_error_clear();
634 
635 		cert_ptr = &cert;
636 
637 		error = t->owner->certificate_check_cb((git_cert *) cert_ptr, 0, urldata.host, t->owner->message_cb_payload);
638 
639 		if (error < 0 && error != GIT_PASSTHROUGH) {
640 			if (!git_error_last())
641 				git_error_set(GIT_ERROR_NET, "user cancelled hostkey check");
642 
643 			goto done;
644 		}
645 	}
646 
647 	/* we need the username to ask for auth methods */
648 	if (!urldata.username) {
649 		if ((error = request_creds(&cred, t, NULL, GIT_CREDENTIAL_USERNAME)) < 0)
650 			goto done;
651 
652 		urldata.username = git__strdup(((git_credential_username *) cred)->username);
653 		cred->free(cred);
654 		cred = NULL;
655 		if (!urldata.username)
656 			goto done;
657 	} else if (urldata.username && urldata.password) {
658 		if ((error = git_credential_userpass_plaintext_new(&cred, urldata.username, urldata.password)) < 0)
659 			goto done;
660 	}
661 
662 	if ((error = list_auth_methods(&auth_methods, session, urldata.username)) < 0)
663 		goto done;
664 
665 	error = GIT_EAUTH;
666 	/* if we already have something to try */
667 	if (cred && auth_methods & cred->credtype)
668 		error = _git_ssh_authenticate_session(session, cred);
669 
670 	while (error == GIT_EAUTH) {
671 		if (cred) {
672 			cred->free(cred);
673 			cred = NULL;
674 		}
675 
676 		if ((error = request_creds(&cred, t, urldata.username, auth_methods)) < 0)
677 			goto done;
678 
679 		if (strcmp(urldata.username, git_credential_get_username(cred))) {
680 			git_error_set(GIT_ERROR_SSH, "username does not match previous request");
681 			error = -1;
682 			goto done;
683 		}
684 
685 		error = _git_ssh_authenticate_session(session, cred);
686 
687 		if (error == GIT_EAUTH) {
688 			/* refresh auth methods */
689 			if ((error = list_auth_methods(&auth_methods, session, urldata.username)) < 0)
690 				goto done;
691 			else
692 				error = GIT_EAUTH;
693 		}
694 	}
695 
696 	if (error < 0)
697 		goto done;
698 
699 	channel = libssh2_channel_open_session(session);
700 	if (!channel) {
701 		error = -1;
702 		ssh_error(session, "Failed to open SSH channel");
703 		goto done;
704 	}
705 
706 	libssh2_channel_set_blocking(channel, 1);
707 
708 	s->session = session;
709 	s->channel = channel;
710 
711 	t->current_stream = s;
712 
713 done:
714 	if (error < 0) {
715 		ssh_stream_free(*stream);
716 
717 		if (session)
718 			libssh2_session_free(session);
719 	}
720 
721 	if (cred)
722 		cred->free(cred);
723 
724 	git_net_url_dispose(&urldata);
725 
726 	return error;
727 }
728 
ssh_uploadpack_ls(ssh_subtransport * t,const char * url,git_smart_subtransport_stream ** stream)729 static int ssh_uploadpack_ls(
730 	ssh_subtransport *t,
731 	const char *url,
732 	git_smart_subtransport_stream **stream)
733 {
734 	const char *cmd = t->cmd_uploadpack ? t->cmd_uploadpack : cmd_uploadpack;
735 
736 	return _git_ssh_setup_conn(t, url, cmd, stream);
737 }
738 
ssh_uploadpack(ssh_subtransport * t,const char * url,git_smart_subtransport_stream ** stream)739 static int ssh_uploadpack(
740 	ssh_subtransport *t,
741 	const char *url,
742 	git_smart_subtransport_stream **stream)
743 {
744 	GIT_UNUSED(url);
745 
746 	if (t->current_stream) {
747 		*stream = &t->current_stream->parent;
748 		return 0;
749 	}
750 
751 	git_error_set(GIT_ERROR_NET, "must call UPLOADPACK_LS before UPLOADPACK");
752 	return -1;
753 }
754 
ssh_receivepack_ls(ssh_subtransport * t,const char * url,git_smart_subtransport_stream ** stream)755 static int ssh_receivepack_ls(
756 	ssh_subtransport *t,
757 	const char *url,
758 	git_smart_subtransport_stream **stream)
759 {
760 	const char *cmd = t->cmd_receivepack ? t->cmd_receivepack : cmd_receivepack;
761 
762 
763 	return _git_ssh_setup_conn(t, url, cmd, stream);
764 }
765 
ssh_receivepack(ssh_subtransport * t,const char * url,git_smart_subtransport_stream ** stream)766 static int ssh_receivepack(
767 	ssh_subtransport *t,
768 	const char *url,
769 	git_smart_subtransport_stream **stream)
770 {
771 	GIT_UNUSED(url);
772 
773 	if (t->current_stream) {
774 		*stream = &t->current_stream->parent;
775 		return 0;
776 	}
777 
778 	git_error_set(GIT_ERROR_NET, "must call RECEIVEPACK_LS before RECEIVEPACK");
779 	return -1;
780 }
781 
_ssh_action(git_smart_subtransport_stream ** stream,git_smart_subtransport * subtransport,const char * url,git_smart_service_t action)782 static int _ssh_action(
783 	git_smart_subtransport_stream **stream,
784 	git_smart_subtransport *subtransport,
785 	const char *url,
786 	git_smart_service_t action)
787 {
788 	ssh_subtransport *t = GIT_CONTAINER_OF(subtransport, ssh_subtransport, parent);
789 
790 	switch (action) {
791 		case GIT_SERVICE_UPLOADPACK_LS:
792 			return ssh_uploadpack_ls(t, url, stream);
793 
794 		case GIT_SERVICE_UPLOADPACK:
795 			return ssh_uploadpack(t, url, stream);
796 
797 		case GIT_SERVICE_RECEIVEPACK_LS:
798 			return ssh_receivepack_ls(t, url, stream);
799 
800 		case GIT_SERVICE_RECEIVEPACK:
801 			return ssh_receivepack(t, url, stream);
802 	}
803 
804 	*stream = NULL;
805 	return -1;
806 }
807 
_ssh_close(git_smart_subtransport * subtransport)808 static int _ssh_close(git_smart_subtransport *subtransport)
809 {
810 	ssh_subtransport *t = GIT_CONTAINER_OF(subtransport, ssh_subtransport, parent);
811 
812 	GIT_ASSERT(!t->current_stream);
813 
814 	GIT_UNUSED(t);
815 
816 	return 0;
817 }
818 
_ssh_free(git_smart_subtransport * subtransport)819 static void _ssh_free(git_smart_subtransport *subtransport)
820 {
821 	ssh_subtransport *t = GIT_CONTAINER_OF(subtransport, ssh_subtransport, parent);
822 
823 	git__free(t->cmd_uploadpack);
824 	git__free(t->cmd_receivepack);
825 	git__free(t);
826 }
827 
828 #define SSH_AUTH_PUBLICKEY "publickey"
829 #define SSH_AUTH_PASSWORD "password"
830 #define SSH_AUTH_KEYBOARD_INTERACTIVE "keyboard-interactive"
831 
list_auth_methods(int * out,LIBSSH2_SESSION * session,const char * username)832 static int list_auth_methods(int *out, LIBSSH2_SESSION *session, const char *username)
833 {
834 	const char *list, *ptr;
835 
836 	*out = 0;
837 
838 	list = libssh2_userauth_list(session, username, strlen(username));
839 
840 	/* either error, or the remote accepts NONE auth, which is bizarre, let's punt */
841 	if (list == NULL && !libssh2_userauth_authenticated(session)) {
842 		ssh_error(session, "Failed to retrieve list of SSH authentication methods");
843 		return -1;
844 	}
845 
846 	ptr = list;
847 	while (ptr) {
848 		if (*ptr == ',')
849 			ptr++;
850 
851 		if (!git__prefixcmp(ptr, SSH_AUTH_PUBLICKEY)) {
852 			*out |= GIT_CREDENTIAL_SSH_KEY;
853 			*out |= GIT_CREDENTIAL_SSH_CUSTOM;
854 #ifdef GIT_SSH_MEMORY_CREDENTIALS
855 			*out |= GIT_CREDENTIAL_SSH_MEMORY;
856 #endif
857 			ptr += strlen(SSH_AUTH_PUBLICKEY);
858 			continue;
859 		}
860 
861 		if (!git__prefixcmp(ptr, SSH_AUTH_PASSWORD)) {
862 			*out |= GIT_CREDENTIAL_USERPASS_PLAINTEXT;
863 			ptr += strlen(SSH_AUTH_PASSWORD);
864 			continue;
865 		}
866 
867 		if (!git__prefixcmp(ptr, SSH_AUTH_KEYBOARD_INTERACTIVE)) {
868 			*out |= GIT_CREDENTIAL_SSH_INTERACTIVE;
869 			ptr += strlen(SSH_AUTH_KEYBOARD_INTERACTIVE);
870 			continue;
871 		}
872 
873 		/* Skipt it if we don't know it */
874 		ptr = strchr(ptr, ',');
875 	}
876 
877 	return 0;
878 }
879 #endif
880 
git_smart_subtransport_ssh(git_smart_subtransport ** out,git_transport * owner,void * param)881 int git_smart_subtransport_ssh(
882 	git_smart_subtransport **out, git_transport *owner, void *param)
883 {
884 #ifdef GIT_SSH
885 	ssh_subtransport *t;
886 
887 	GIT_ASSERT_ARG(out);
888 
889 	GIT_UNUSED(param);
890 
891 	t = git__calloc(sizeof(ssh_subtransport), 1);
892 	GIT_ERROR_CHECK_ALLOC(t);
893 
894 	t->owner = (transport_smart *)owner;
895 	t->parent.action = _ssh_action;
896 	t->parent.close = _ssh_close;
897 	t->parent.free = _ssh_free;
898 
899 	*out = (git_smart_subtransport *) t;
900 	return 0;
901 #else
902 	GIT_UNUSED(owner);
903 	GIT_UNUSED(param);
904 
905 	GIT_ASSERT_ARG(out);
906 	*out = NULL;
907 
908 	git_error_set(GIT_ERROR_INVALID, "cannot create SSH transport. Library was built without SSH support");
909 	return -1;
910 #endif
911 }
912 
git_transport_ssh_with_paths(git_transport ** out,git_remote * owner,void * payload)913 int git_transport_ssh_with_paths(git_transport **out, git_remote *owner, void *payload)
914 {
915 #ifdef GIT_SSH
916 	git_strarray *paths = (git_strarray *) payload;
917 	git_transport *transport;
918 	transport_smart *smart;
919 	ssh_subtransport *t;
920 	int error;
921 	git_smart_subtransport_definition ssh_definition = {
922 		git_smart_subtransport_ssh,
923 		0, /* no RPC */
924 		NULL,
925 	};
926 
927 	if (paths->count != 2) {
928 		git_error_set(GIT_ERROR_SSH, "invalid ssh paths, must be two strings");
929 		return GIT_EINVALIDSPEC;
930 	}
931 
932 	if ((error = git_transport_smart(&transport, owner, &ssh_definition)) < 0)
933 		return error;
934 
935 	smart = (transport_smart *) transport;
936 	t = (ssh_subtransport *) smart->wrapped;
937 
938 	t->cmd_uploadpack = git__strdup(paths->strings[0]);
939 	GIT_ERROR_CHECK_ALLOC(t->cmd_uploadpack);
940 	t->cmd_receivepack = git__strdup(paths->strings[1]);
941 	GIT_ERROR_CHECK_ALLOC(t->cmd_receivepack);
942 
943 	*out = transport;
944 	return 0;
945 #else
946 	GIT_UNUSED(owner);
947 	GIT_UNUSED(payload);
948 
949 	GIT_ASSERT_ARG(out);
950 	*out = NULL;
951 
952 	git_error_set(GIT_ERROR_INVALID, "cannot create SSH transport. Library was built without SSH support");
953 	return -1;
954 #endif
955 }
956 
957 #ifdef GIT_SSH
shutdown_ssh(void)958 static void shutdown_ssh(void)
959 {
960     libssh2_exit();
961 }
962 #endif
963 
git_transport_ssh_global_init(void)964 int git_transport_ssh_global_init(void)
965 {
966 #ifdef GIT_SSH
967 	if (libssh2_init(0) < 0) {
968 		git_error_set(GIT_ERROR_SSH, "unable to initialize libssh2");
969 		return -1;
970 	}
971 
972 	return git_runtime_shutdown_register(shutdown_ssh);
973 
974 #else
975 
976 	/* Nothing to initialize */
977 	return 0;
978 
979 #endif
980 }
981