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

- old
+ new

@@ -12,18 +12,15 @@ #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://" }; @@ -133,11 +130,11 @@ char *buffer, size_t buf_size, size_t *bytes_read) { int rc; - ssh_stream *s = GIT_CONTAINER_OF(stream, ssh_stream, parent); + ssh_stream *s = (ssh_stream *)stream; *bytes_read = 0; if (!s->sent_command && send_command(s) < 0) return -1; @@ -171,11 +168,11 @@ static int ssh_stream_write( git_smart_subtransport_stream *stream, const char *buffer, size_t len) { - ssh_stream *s = GIT_CONTAINER_OF(stream, ssh_stream, parent); + ssh_stream *s = (ssh_stream *)stream; size_t off = 0; ssize_t ret = 0; if (!s->sent_command && send_command(s) < 0) return -1; @@ -197,11 +194,11 @@ return 0; } static void ssh_stream_free(git_smart_subtransport_stream *stream) { - ssh_stream *s = GIT_CONTAINER_OF(stream, ssh_stream, parent); + ssh_stream *s = (ssh_stream *)stream; ssh_subtransport *t; if (!stream) return; @@ -259,11 +256,12 @@ *stream = &s->parent; return 0; } static int git_ssh_extract_url_parts( - git_net_url *urldata, + char **host, + char **username, const char *url) { char *colon, *at; const char *start; @@ -271,24 +269,24 @@ at = strchr(url, '@'); if (at) { start = at + 1; - urldata->username = git__substrdup(url, at - url); - GIT_ERROR_CHECK_ALLOC(urldata->username); + *username = git__substrdup(url, at - url); + GIT_ERROR_CHECK_ALLOC(*username); } else { start = url; - urldata->username = NULL; + *username = NULL; } if (colon == NULL || (colon < start)) { git_error_set(GIT_ERROR_NET, "malformed URL"); return -1; } - urldata->host = git__substrdup(start, colon - start); - GIT_ERROR_CHECK_ALLOC(urldata->host); + *host = git__substrdup(start, colon - start); + GIT_ERROR_CHECK_ALLOC(*host); return 0; } static int ssh_agent_auth(LIBSSH2_SESSION *session, git_cred_ssh_key *c) { @@ -479,11 +477,11 @@ LIBSSH2_SESSION** session, git_stream *io) { int rc = 0; LIBSSH2_SESSION* s; - git_socket_stream *socket = GIT_CONTAINER_OF(io, git_socket_stream, parent); + git_socket_stream *socket = (git_socket_stream *) io; assert(session); s = libssh2_session_init(); if (!s) { @@ -506,19 +504,18 @@ *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) { - git_net_url urldata = GIT_NET_URL_INIT; + char *host=NULL, *port=NULL, *path=NULL, *user=NULL, *pass=NULL; + const char *default_port="22"; int auth_methods, error = 0; size_t i; ssh_stream *s; git_cred *cred = NULL; LIBSSH2_SESSION* session=NULL; @@ -536,26 +533,23 @@ for (i = 0; i < ARRAY_SIZE(ssh_prefixes); ++i) { const char *p = ssh_prefixes[i]; if (!git__prefixcmp(url, p)) { - if ((error = git_net_url_parse(&urldata, url)) < 0) + if ((error = gitno_extract_url_parts(&host, &port, &path, &user, &pass, url, default_port)) < 0) goto done; goto post_extract; } } - if ((error = git_ssh_extract_url_parts(&urldata, url)) < 0) + if ((error = git_ssh_extract_url_parts(&host, &user, 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, urldata.host, urldata.port)) < 0 || + if ((error = git_socket_stream_new(&s->io, host, port)) < 0 || (error = git_stream_connect(s->io)) < 0) goto done; if ((error = _git_ssh_session_create(&session, s->io)) < 0) goto done; @@ -564,18 +558,10 @@ 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); } @@ -595,36 +581,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, urldata.host, t->owner->message_cb_payload); + error = t->owner->certificate_check_cb((git_cert *) cert_ptr, 0, 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 (!urldata.username) { + if (!user) { if ((error = request_creds(&cred, t, NULL, GIT_CREDTYPE_USERNAME)) < 0) goto done; - urldata.username = git__strdup(((git_cred_username *) cred)->username); + user = git__strdup(((git_cred_username *) cred)->username); cred->free(cred); cred = NULL; - if (!urldata.username) + if (!user) goto done; - } else if (urldata.username && urldata.password) { - if ((error = git_cred_userpass_plaintext_new(&cred, urldata.username, urldata.password)) < 0) + } else if (user && pass) { + if ((error = git_cred_userpass_plaintext_new(&cred, user, pass)) < 0) goto done; } - if ((error = list_auth_methods(&auth_methods, session, urldata.username)) < 0) + if ((error = list_auth_methods(&auth_methods, session, user)) < 0) goto done; error = GIT_EAUTH; /* if we already have something to try */ if (cred && auth_methods & cred->credtype) @@ -634,14 +620,14 @@ if (cred) { cred->free(cred); cred = NULL; } - if ((error = request_creds(&cred, t, urldata.username, auth_methods)) < 0) + if ((error = request_creds(&cred, t, user, auth_methods)) < 0) goto done; - if (strcmp(urldata.username, git_cred_get_username(cred))) { + if (strcmp(user, git_cred__username(cred))) { git_error_set(GIT_ERROR_SSH, "username does not match previous request"); error = -1; goto done; } @@ -674,11 +660,15 @@ } if (cred) cred->free(cred); - git_net_url_dispose(&urldata); + git__free(host); + git__free(port); + git__free(path); + git__free(user); + git__free(pass); return error; } static int ssh_uploadpack_ls( @@ -738,11 +728,11 @@ git_smart_subtransport_stream **stream, git_smart_subtransport *subtransport, const char *url, git_smart_service_t action) { - ssh_subtransport *t = GIT_CONTAINER_OF(subtransport, ssh_subtransport, parent); + ssh_subtransport *t = (ssh_subtransport *) subtransport; switch (action) { case GIT_SERVICE_UPLOADPACK_LS: return ssh_uploadpack_ls(t, url, stream); @@ -760,21 +750,21 @@ return -1; } static int _ssh_close(git_smart_subtransport *subtransport) { - ssh_subtransport *t = GIT_CONTAINER_OF(subtransport, ssh_subtransport, parent); + ssh_subtransport *t = (ssh_subtransport *) subtransport; assert(!t->current_stream); GIT_UNUSED(t); return 0; } static void _ssh_free(git_smart_subtransport *subtransport) { - ssh_subtransport *t = GIT_CONTAINER_OF(subtransport, ssh_subtransport, parent); + ssh_subtransport *t = (ssh_subtransport *) subtransport; assert(!t->current_stream); git__free(t->cmd_uploadpack); git__free(t->cmd_receivepack);