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 				default:
584 					cert.raw_type = GIT_CERT_SSH_RAW_TYPE_UNKNOWN;
585 			}
586 		}
587 
588 #ifdef LIBSSH2_HOSTKEY_HASH_SHA256
589 		key = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA256);
590 		if (key != NULL) {
591 			cert.type |= GIT_CERT_SSH_SHA256;
592 			memcpy(&cert.hash_sha256, key, 32);
593 		}
594 #endif
595 
596 		key = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
597 		if (key != NULL) {
598 			cert.type |= GIT_CERT_SSH_SHA1;
599 			memcpy(&cert.hash_sha1, key, 20);
600 		}
601 
602 		key = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_MD5);
603 		if (key != NULL) {
604 			cert.type |= GIT_CERT_SSH_MD5;
605 			memcpy(&cert.hash_md5, key, 16);
606 		}
607 
608 		if (cert.type == 0) {
609 			git_error_set(GIT_ERROR_SSH, "unable to get the host key");
610 			error = -1;
611 			goto done;
612 		}
613 
614 		/* We don't currently trust any hostkeys */
615 		git_error_clear();
616 
617 		cert_ptr = &cert;
618 
619 		error = t->owner->certificate_check_cb((git_cert *) cert_ptr, 0, urldata.host, t->owner->message_cb_payload);
620 
621 		if (error < 0 && error != GIT_PASSTHROUGH) {
622 			if (!git_error_last())
623 				git_error_set(GIT_ERROR_NET, "user cancelled hostkey check");
624 
625 			goto done;
626 		}
627 	}
628 
629 	/* we need the username to ask for auth methods */
630 	if (!urldata.username) {
631 		if ((error = request_creds(&cred, t, NULL, GIT_CREDENTIAL_USERNAME)) < 0)
632 			goto done;
633 
634 		urldata.username = git__strdup(((git_credential_username *) cred)->username);
635 		cred->free(cred);
636 		cred = NULL;
637 		if (!urldata.username)
638 			goto done;
639 	} else if (urldata.username && urldata.password) {
640 		if ((error = git_credential_userpass_plaintext_new(&cred, urldata.username, urldata.password)) < 0)
641 			goto done;
642 	}
643 
644 	if ((error = list_auth_methods(&auth_methods, session, urldata.username)) < 0)
645 		goto done;
646 
647 	error = GIT_EAUTH;
648 	/* if we already have something to try */
649 	if (cred && auth_methods & cred->credtype)
650 		error = _git_ssh_authenticate_session(session, cred);
651 
652 	while (error == GIT_EAUTH) {
653 		if (cred) {
654 			cred->free(cred);
655 			cred = NULL;
656 		}
657 
658 		if ((error = request_creds(&cred, t, urldata.username, auth_methods)) < 0)
659 			goto done;
660 
661 		if (strcmp(urldata.username, git_credential_get_username(cred))) {
662 			git_error_set(GIT_ERROR_SSH, "username does not match previous request");
663 			error = -1;
664 			goto done;
665 		}
666 
667 		error = _git_ssh_authenticate_session(session, cred);
668 
669 		if (error == GIT_EAUTH) {
670 			/* refresh auth methods */
671 			if ((error = list_auth_methods(&auth_methods, session, urldata.username)) < 0)
672 				goto done;
673 			else
674 				error = GIT_EAUTH;
675 		}
676 	}
677 
678 	if (error < 0)
679 		goto done;
680 
681 	channel = libssh2_channel_open_session(session);
682 	if (!channel) {
683 		error = -1;
684 		ssh_error(session, "Failed to open SSH channel");
685 		goto done;
686 	}
687 
688 	libssh2_channel_set_blocking(channel, 1);
689 
690 	s->session = session;
691 	s->channel = channel;
692 
693 	t->current_stream = s;
694 
695 done:
696 	if (error < 0) {
697 		ssh_stream_free(*stream);
698 
699 		if (session)
700 			libssh2_session_free(session);
701 	}
702 
703 	if (cred)
704 		cred->free(cred);
705 
706 	git_net_url_dispose(&urldata);
707 
708 	return error;
709 }
710 
ssh_uploadpack_ls(ssh_subtransport * t,const char * url,git_smart_subtransport_stream ** stream)711 static int ssh_uploadpack_ls(
712 	ssh_subtransport *t,
713 	const char *url,
714 	git_smart_subtransport_stream **stream)
715 {
716 	const char *cmd = t->cmd_uploadpack ? t->cmd_uploadpack : cmd_uploadpack;
717 
718 	return _git_ssh_setup_conn(t, url, cmd, stream);
719 }
720 
ssh_uploadpack(ssh_subtransport * t,const char * url,git_smart_subtransport_stream ** stream)721 static int ssh_uploadpack(
722 	ssh_subtransport *t,
723 	const char *url,
724 	git_smart_subtransport_stream **stream)
725 {
726 	GIT_UNUSED(url);
727 
728 	if (t->current_stream) {
729 		*stream = &t->current_stream->parent;
730 		return 0;
731 	}
732 
733 	git_error_set(GIT_ERROR_NET, "must call UPLOADPACK_LS before UPLOADPACK");
734 	return -1;
735 }
736 
ssh_receivepack_ls(ssh_subtransport * t,const char * url,git_smart_subtransport_stream ** stream)737 static int ssh_receivepack_ls(
738 	ssh_subtransport *t,
739 	const char *url,
740 	git_smart_subtransport_stream **stream)
741 {
742 	const char *cmd = t->cmd_receivepack ? t->cmd_receivepack : cmd_receivepack;
743 
744 
745 	return _git_ssh_setup_conn(t, url, cmd, stream);
746 }
747 
ssh_receivepack(ssh_subtransport * t,const char * url,git_smart_subtransport_stream ** stream)748 static int ssh_receivepack(
749 	ssh_subtransport *t,
750 	const char *url,
751 	git_smart_subtransport_stream **stream)
752 {
753 	GIT_UNUSED(url);
754 
755 	if (t->current_stream) {
756 		*stream = &t->current_stream->parent;
757 		return 0;
758 	}
759 
760 	git_error_set(GIT_ERROR_NET, "must call RECEIVEPACK_LS before RECEIVEPACK");
761 	return -1;
762 }
763 
_ssh_action(git_smart_subtransport_stream ** stream,git_smart_subtransport * subtransport,const char * url,git_smart_service_t action)764 static int _ssh_action(
765 	git_smart_subtransport_stream **stream,
766 	git_smart_subtransport *subtransport,
767 	const char *url,
768 	git_smart_service_t action)
769 {
770 	ssh_subtransport *t = GIT_CONTAINER_OF(subtransport, ssh_subtransport, parent);
771 
772 	switch (action) {
773 		case GIT_SERVICE_UPLOADPACK_LS:
774 			return ssh_uploadpack_ls(t, url, stream);
775 
776 		case GIT_SERVICE_UPLOADPACK:
777 			return ssh_uploadpack(t, url, stream);
778 
779 		case GIT_SERVICE_RECEIVEPACK_LS:
780 			return ssh_receivepack_ls(t, url, stream);
781 
782 		case GIT_SERVICE_RECEIVEPACK:
783 			return ssh_receivepack(t, url, stream);
784 	}
785 
786 	*stream = NULL;
787 	return -1;
788 }
789 
_ssh_close(git_smart_subtransport * subtransport)790 static int _ssh_close(git_smart_subtransport *subtransport)
791 {
792 	ssh_subtransport *t = GIT_CONTAINER_OF(subtransport, ssh_subtransport, parent);
793 
794 	GIT_ASSERT(!t->current_stream);
795 
796 	GIT_UNUSED(t);
797 
798 	return 0;
799 }
800 
_ssh_free(git_smart_subtransport * subtransport)801 static void _ssh_free(git_smart_subtransport *subtransport)
802 {
803 	ssh_subtransport *t = GIT_CONTAINER_OF(subtransport, ssh_subtransport, parent);
804 
805 	git__free(t->cmd_uploadpack);
806 	git__free(t->cmd_receivepack);
807 	git__free(t);
808 }
809 
810 #define SSH_AUTH_PUBLICKEY "publickey"
811 #define SSH_AUTH_PASSWORD "password"
812 #define SSH_AUTH_KEYBOARD_INTERACTIVE "keyboard-interactive"
813 
list_auth_methods(int * out,LIBSSH2_SESSION * session,const char * username)814 static int list_auth_methods(int *out, LIBSSH2_SESSION *session, const char *username)
815 {
816 	const char *list, *ptr;
817 
818 	*out = 0;
819 
820 	list = libssh2_userauth_list(session, username, strlen(username));
821 
822 	/* either error, or the remote accepts NONE auth, which is bizarre, let's punt */
823 	if (list == NULL && !libssh2_userauth_authenticated(session)) {
824 		ssh_error(session, "Failed to retrieve list of SSH authentication methods");
825 		return -1;
826 	}
827 
828 	ptr = list;
829 	while (ptr) {
830 		if (*ptr == ',')
831 			ptr++;
832 
833 		if (!git__prefixcmp(ptr, SSH_AUTH_PUBLICKEY)) {
834 			*out |= GIT_CREDENTIAL_SSH_KEY;
835 			*out |= GIT_CREDENTIAL_SSH_CUSTOM;
836 #ifdef GIT_SSH_MEMORY_CREDENTIALS
837 			*out |= GIT_CREDENTIAL_SSH_MEMORY;
838 #endif
839 			ptr += strlen(SSH_AUTH_PUBLICKEY);
840 			continue;
841 		}
842 
843 		if (!git__prefixcmp(ptr, SSH_AUTH_PASSWORD)) {
844 			*out |= GIT_CREDENTIAL_USERPASS_PLAINTEXT;
845 			ptr += strlen(SSH_AUTH_PASSWORD);
846 			continue;
847 		}
848 
849 		if (!git__prefixcmp(ptr, SSH_AUTH_KEYBOARD_INTERACTIVE)) {
850 			*out |= GIT_CREDENTIAL_SSH_INTERACTIVE;
851 			ptr += strlen(SSH_AUTH_KEYBOARD_INTERACTIVE);
852 			continue;
853 		}
854 
855 		/* Skipt it if we don't know it */
856 		ptr = strchr(ptr, ',');
857 	}
858 
859 	return 0;
860 }
861 #endif
862 
git_smart_subtransport_ssh(git_smart_subtransport ** out,git_transport * owner,void * param)863 int git_smart_subtransport_ssh(
864 	git_smart_subtransport **out, git_transport *owner, void *param)
865 {
866 #ifdef GIT_SSH
867 	ssh_subtransport *t;
868 
869 	GIT_ASSERT_ARG(out);
870 
871 	GIT_UNUSED(param);
872 
873 	t = git__calloc(sizeof(ssh_subtransport), 1);
874 	GIT_ERROR_CHECK_ALLOC(t);
875 
876 	t->owner = (transport_smart *)owner;
877 	t->parent.action = _ssh_action;
878 	t->parent.close = _ssh_close;
879 	t->parent.free = _ssh_free;
880 
881 	*out = (git_smart_subtransport *) t;
882 	return 0;
883 #else
884 	GIT_UNUSED(owner);
885 	GIT_UNUSED(param);
886 
887 	GIT_ASSERT_ARG(out);
888 	*out = NULL;
889 
890 	git_error_set(GIT_ERROR_INVALID, "cannot create SSH transport. Library was built without SSH support");
891 	return -1;
892 #endif
893 }
894 
git_transport_ssh_with_paths(git_transport ** out,git_remote * owner,void * payload)895 int git_transport_ssh_with_paths(git_transport **out, git_remote *owner, void *payload)
896 {
897 #ifdef GIT_SSH
898 	git_strarray *paths = (git_strarray *) payload;
899 	git_transport *transport;
900 	transport_smart *smart;
901 	ssh_subtransport *t;
902 	int error;
903 	git_smart_subtransport_definition ssh_definition = {
904 		git_smart_subtransport_ssh,
905 		0, /* no RPC */
906 		NULL,
907 	};
908 
909 	if (paths->count != 2) {
910 		git_error_set(GIT_ERROR_SSH, "invalid ssh paths, must be two strings");
911 		return GIT_EINVALIDSPEC;
912 	}
913 
914 	if ((error = git_transport_smart(&transport, owner, &ssh_definition)) < 0)
915 		return error;
916 
917 	smart = (transport_smart *) transport;
918 	t = (ssh_subtransport *) smart->wrapped;
919 
920 	t->cmd_uploadpack = git__strdup(paths->strings[0]);
921 	GIT_ERROR_CHECK_ALLOC(t->cmd_uploadpack);
922 	t->cmd_receivepack = git__strdup(paths->strings[1]);
923 	GIT_ERROR_CHECK_ALLOC(t->cmd_receivepack);
924 
925 	*out = transport;
926 	return 0;
927 #else
928 	GIT_UNUSED(owner);
929 	GIT_UNUSED(payload);
930 
931 	GIT_ASSERT_ARG(out);
932 	*out = NULL;
933 
934 	git_error_set(GIT_ERROR_INVALID, "cannot create SSH transport. Library was built without SSH support");
935 	return -1;
936 #endif
937 }
938 
939 #ifdef GIT_SSH
shutdown_ssh(void)940 static void shutdown_ssh(void)
941 {
942     libssh2_exit();
943 }
944 #endif
945 
git_transport_ssh_global_init(void)946 int git_transport_ssh_global_init(void)
947 {
948 #ifdef GIT_SSH
949 	if (libssh2_init(0) < 0) {
950 		git_error_set(GIT_ERROR_SSH, "unable to initialize libssh2");
951 		return -1;
952 	}
953 
954 	return git_runtime_shutdown_register(shutdown_ssh);
955 
956 #else
957 
958 	/* Nothing to initialize */
959 	return 0;
960 
961 #endif
962 }
963