vendor/libgit2/src/transports/ssh.c in rugged-0.28.3.1 vs vendor/libgit2/src/transports/ssh.c in rugged-0.28.4

- old
+ new

@@ -12,15 +12,18 @@ #endif #include "global.h" #include "git2.h" #include "buffer.h" +#include "net.h" #include "netops.h" #include "smart.h" -#include "cred.h" #include "streams/socket.h" +#include "git2/cred.h" +#include "git2/sys/cred.h" + #ifdef GIT_SSH #define OWNING_SUBTRANSPORT(s) ((ssh_subtransport *)(s)->parent.subtransport) static const char *ssh_prefixes[] = { "ssh://", "ssh+git://", "git+ssh://" }; @@ -130,11 +133,11 @@ char *buffer, size_t buf_size, size_t *bytes_read) { int rc; - ssh_stream *s = (ssh_stream *)stream; + ssh_stream *s = GIT_CONTAINER_OF(stream, ssh_stream, parent); *bytes_read = 0; if (!s->sent_command && send_command(s) < 0) return -1; @@ -168,11 +171,11 @@ static int ssh_stream_write( git_smart_subtransport_stream *stream, const char *buffer, size_t len) { - ssh_stream *s = (ssh_stream *)stream; + ssh_stream *s = GIT_CONTAINER_OF(stream, ssh_stream, parent); size_t off = 0; ssize_t ret = 0; if (!s->sent_command && send_command(s) < 0) return -1; @@ -194,11 +197,11 @@ return 0; } static void ssh_stream_free(git_smart_subtransport_stream *stream) { - ssh_stream *s = (ssh_stream *)stream; + ssh_stream *s = GIT_CONTAINER_OF(stream, ssh_stream, parent); ssh_subtransport *t; if (!stream) return; @@ -256,12 +259,11 @@ *stream = &s->parent; return 0; } static int git_ssh_extract_url_parts( - char **host, - char **username, + git_net_url *urldata, const char *url) { char *colon, *at; const char *start; @@ -269,24 +271,24 @@ at = strchr(url, '@'); if (at) { start = at + 1; - *username = git__substrdup(url, at - url); - GIT_ERROR_CHECK_ALLOC(*username); + urldata->username = git__substrdup(url, at - url); + GIT_ERROR_CHECK_ALLOC(urldata->username); } else { start = url; - *username = NULL; + urldata->username = NULL; } if (colon == NULL || (colon < start)) { git_error_set(GIT_ERROR_NET, "malformed URL"); return -1; } - *host = git__substrdup(start, colon - start); - GIT_ERROR_CHECK_ALLOC(*host); + urldata->host = git__substrdup(start, colon - start); + GIT_ERROR_CHECK_ALLOC(urldata->host); return 0; } static int ssh_agent_auth(LIBSSH2_SESSION *session, git_cred_ssh_key *c) { @@ -477,11 +479,11 @@ LIBSSH2_SESSION** session, git_stream *io) { int rc = 0; LIBSSH2_SESSION* s; - git_socket_stream *socket = (git_socket_stream *) io; + git_socket_stream *socket = GIT_CONTAINER_OF(io, git_socket_stream, parent); assert(session); s = libssh2_session_init(); if (!s) { @@ -504,18 +506,19 @@ *session = s; return 0; } +#define SSH_DEFAULT_PORT "22" + static int _git_ssh_setup_conn( ssh_subtransport *t, const char *url, const char *cmd, git_smart_subtransport_stream **stream) { - char *host=NULL, *port=NULL, *path=NULL, *user=NULL, *pass=NULL; - const char *default_port="22"; + git_net_url urldata = GIT_NET_URL_INIT; int auth_methods, error = 0; size_t i; ssh_stream *s; git_cred *cred = NULL; LIBSSH2_SESSION* session=NULL; @@ -533,23 +536,26 @@ for (i = 0; i < ARRAY_SIZE(ssh_prefixes); ++i) { const char *p = ssh_prefixes[i]; if (!git__prefixcmp(url, p)) { - if ((error = gitno_extract_url_parts(&host, &port, &path, &user, &pass, url, default_port)) < 0) + if ((error = git_net_url_parse(&urldata, url)) < 0) goto done; goto post_extract; } } - if ((error = git_ssh_extract_url_parts(&host, &user, url)) < 0) + if ((error = git_ssh_extract_url_parts(&urldata, url)) < 0) goto done; - port = git__strdup(default_port); - GIT_ERROR_CHECK_ALLOC(port); + if (urldata.port == NULL) + urldata.port = git__strdup(SSH_DEFAULT_PORT); + + GIT_ERROR_CHECK_ALLOC(urldata.port); + post_extract: - if ((error = git_socket_stream_new(&s->io, host, port)) < 0 || + if ((error = git_socket_stream_new(&s->io, urldata.host, urldata.port)) < 0 || (error = git_stream_connect(s->io)) < 0) goto done; if ((error = _git_ssh_session_create(&session, s->io)) < 0) goto done; @@ -558,10 +564,18 @@ git_cert_hostkey cert = {{ 0 }}, *cert_ptr; const char *key; cert.parent.cert_type = GIT_CERT_HOSTKEY_LIBSSH2; +#ifdef LIBSSH2_HOSTKEY_HASH_SHA256 + key = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA256); + if (key != NULL) { + cert.type |= GIT_CERT_SSH_SHA256; + memcpy(&cert.hash_sha256, key, 32); + } +#endif + key = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1); if (key != NULL) { cert.type |= GIT_CERT_SSH_SHA1; memcpy(&cert.hash_sha1, key, 20); } @@ -581,36 +595,36 @@ /* We don't currently trust any hostkeys */ git_error_clear(); cert_ptr = &cert; - error = t->owner->certificate_check_cb((git_cert *) cert_ptr, 0, host, t->owner->message_cb_payload); + error = t->owner->certificate_check_cb((git_cert *) cert_ptr, 0, urldata.host, t->owner->message_cb_payload); if (error < 0 && error != GIT_PASSTHROUGH) { if (!git_error_last()) git_error_set(GIT_ERROR_NET, "user cancelled hostkey check"); goto done; } } /* we need the username to ask for auth methods */ - if (!user) { + if (!urldata.username) { if ((error = request_creds(&cred, t, NULL, GIT_CREDTYPE_USERNAME)) < 0) goto done; - user = git__strdup(((git_cred_username *) cred)->username); + urldata.username = git__strdup(((git_cred_username *) cred)->username); cred->free(cred); cred = NULL; - if (!user) + if (!urldata.username) goto done; - } else if (user && pass) { - if ((error = git_cred_userpass_plaintext_new(&cred, user, pass)) < 0) + } else if (urldata.username && urldata.password) { + if ((error = git_cred_userpass_plaintext_new(&cred, urldata.username, urldata.password)) < 0) goto done; } - if ((error = list_auth_methods(&auth_methods, session, user)) < 0) + if ((error = list_auth_methods(&auth_methods, session, urldata.username)) < 0) goto done; error = GIT_EAUTH; /* if we already have something to try */ if (cred && auth_methods & cred->credtype) @@ -620,14 +634,14 @@ if (cred) { cred->free(cred); cred = NULL; } - if ((error = request_creds(&cred, t, user, auth_methods)) < 0) + if ((error = request_creds(&cred, t, urldata.username, auth_methods)) < 0) goto done; - if (strcmp(user, git_cred__username(cred))) { + if (strcmp(urldata.username, git_cred_get_username(cred))) { git_error_set(GIT_ERROR_SSH, "username does not match previous request"); error = -1; goto done; } @@ -660,15 +674,11 @@ } if (cred) cred->free(cred); - git__free(host); - git__free(port); - git__free(path); - git__free(user); - git__free(pass); + git_net_url_dispose(&urldata); return error; } static int ssh_uploadpack_ls( @@ -728,11 +738,11 @@ git_smart_subtransport_stream **stream, git_smart_subtransport *subtransport, const char *url, git_smart_service_t action) { - ssh_subtransport *t = (ssh_subtransport *) subtransport; + ssh_subtransport *t = GIT_CONTAINER_OF(subtransport, ssh_subtransport, parent); switch (action) { case GIT_SERVICE_UPLOADPACK_LS: return ssh_uploadpack_ls(t, url, stream); @@ -750,21 +760,21 @@ return -1; } static int _ssh_close(git_smart_subtransport *subtransport) { - ssh_subtransport *t = (ssh_subtransport *) subtransport; + ssh_subtransport *t = GIT_CONTAINER_OF(subtransport, ssh_subtransport, parent); assert(!t->current_stream); GIT_UNUSED(t); return 0; } static void _ssh_free(git_smart_subtransport *subtransport) { - ssh_subtransport *t = (ssh_subtransport *) subtransport; + ssh_subtransport *t = GIT_CONTAINER_OF(subtransport, ssh_subtransport, parent); assert(!t->current_stream); git__free(t->cmd_uploadpack); git__free(t->cmd_receivepack);