Bring SSH error reporting up to base standards The SSH error checking and reporting could still be further improved by using the libssh2 native methods to get error info, but at least this ensures that all error codes are checked and translated into libgit2 error messages.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
diff --git a/src/transports/cred.c b/src/transports/cred.c
index d713f89..a6727e9 100644
--- a/src/transports/cred.c
+++ b/src/transports/cred.c
@@ -33,8 +33,7 @@ int git_cred_userpass_plaintext_new(
{
git_cred_userpass_plaintext *c;
- if (!cred)
- return -1;
+ assert(cred);
c = git__malloc(sizeof(git_cred_userpass_plaintext));
GITERR_CHECK_ALLOC(c);
diff --git a/src/transports/ssh.c b/src/transports/ssh.c
index 0155dd7..7fb53bc 100644
--- a/src/transports/ssh.c
+++ b/src/transports/ssh.c
@@ -55,6 +55,7 @@ static int gen_proto(git_buf *request, const char *cmd, const char *url)
}
if (!repo) {
+ giterr_set(GITERR_NET, "Malformed git protocol URL");
return -1;
}
@@ -79,13 +80,11 @@ static int send_command(ssh_stream *s)
if (error < 0)
goto cleanup;
- error = libssh2_channel_exec(
- s->channel,
- request.ptr
- );
-
- if (0 != error)
+ error = libssh2_channel_exec(s->channel, request.ptr);
+ if (error < 0) {
+ giterr_set(GITERR_NET, "SSH could not execute request");
goto cleanup;
+ }
s->sent_command = 1;
@@ -100,6 +99,7 @@ static int ssh_stream_read(
size_t buf_size,
size_t *bytes_read)
{
+ int rc;
ssh_stream *s = (ssh_stream *)stream;
*bytes_read = 0;
@@ -107,9 +107,10 @@ static int ssh_stream_read(
if (!s->sent_command && send_command(s) < 0)
return -1;
- int rc = libssh2_channel_read(s->channel, buffer, buf_size);
- if (rc < 0)
+ if ((rc = libssh2_channel_read(s->channel, buffer, buf_size)) < 0) {
+ giterr_set(GITERR_NET, "SSH could not read data");
return -1;
+ }
*bytes_read = rc;
@@ -126,12 +127,12 @@ static int ssh_stream_write(
if (!s->sent_command && send_command(s) < 0)
return -1;
- int rc = libssh2_channel_write(s->channel, buffer, len);
- if (rc < 0) {
+ if (libssh2_channel_write(s->channel, buffer, len) < 0) {
+ giterr_set(GITERR_NET, "SSH could not write data");
return -1;
}
- return rc;
+ return 0;
}
static void ssh_stream_free(git_smart_subtransport_stream *stream)
@@ -151,12 +152,13 @@ static void ssh_stream_free(git_smart_subtransport_stream *stream)
}
if (s->session) {
- libssh2_session_free(s->session), s->session = NULL;
+ libssh2_session_free(s->session);
+ s->session = NULL;
}
if (s->socket.socket) {
- ret = gitno_close(&s->socket);
- assert(!ret);
+ (void)gitno_close(&s->socket);
+ /* can't do anything here with error return value */
}
git__free(s->url);
@@ -171,8 +173,7 @@ static int ssh_stream_alloc(
{
ssh_stream *s;
- if (!stream)
- return -1;
+ assert(stream);
s = git__calloc(sizeof(ssh_stream), 1);
GITERR_CHECK_ALLOC(s);
@@ -183,8 +184,8 @@ static int ssh_stream_alloc(
s->parent.free = ssh_stream_free;
s->cmd = cmd;
- s->url = git__strdup(url);
+ s->url = git__strdup(url);
if (!s->url) {
git__free(s);
return -1;
@@ -217,8 +218,10 @@ static int git_ssh_extract_url_parts(
start = url;
*username = git__strdup(default_user);
}
+ GITERR_CHECK_ALLOC(*username);
*host = git__substrdup(start, colon - start);
+ GITERR_CHECK_ALLOC(*host);
return 0;
}
@@ -229,67 +232,63 @@ static int _git_ssh_authenticate_session(
git_cred* cred)
{
int rc;
+
do {
switch (cred->credtype) {
- case GIT_CREDTYPE_USERPASS_PLAINTEXT: {
- git_cred_userpass_plaintext *c = (git_cred_userpass_plaintext *)cred;
- rc = libssh2_userauth_password(
- session,
- c->username,
- c->password
- );
- break;
- }
- case GIT_CREDTYPE_SSH_KEYFILE_PASSPHRASE: {
- git_cred_ssh_keyfile_passphrase *c = (git_cred_ssh_keyfile_passphrase *)cred;
- rc = libssh2_userauth_publickey_fromfile(
- session,
- user,
- c->publickey,
- c->privatekey,
- c->passphrase
- );
- break;
- }
- case GIT_CREDTYPE_SSH_PUBLICKEY: {
- git_cred_ssh_publickey *c = (git_cred_ssh_publickey *)cred;
- rc = libssh2_userauth_publickey(
- session,
- user,
- (const unsigned char *)c->publickey,
- c->publickey_len,
- c->sign_callback,
- &c->sign_data
- );
- break;
- }
- default:
- rc = LIBSSH2_ERROR_AUTHENTICATION_FAILED;
+ case GIT_CREDTYPE_USERPASS_PLAINTEXT: {
+ git_cred_userpass_plaintext *c = (git_cred_userpass_plaintext *)cred;
+ rc = libssh2_userauth_password(session, c->username, c->password);
+ break;
+ }
+ case GIT_CREDTYPE_SSH_KEYFILE_PASSPHRASE: {
+ git_cred_ssh_keyfile_passphrase *c = (git_cred_ssh_keyfile_passphrase *)cred;
+ rc = libssh2_userauth_publickey_fromfile(
+ session, user, c->publickey, c->privatekey, c->passphrase);
+ break;
+ }
+ case GIT_CREDTYPE_SSH_PUBLICKEY: {
+ git_cred_ssh_publickey *c = (git_cred_ssh_publickey *)cred;
+ rc = libssh2_userauth_publickey(
+ session, user, (const unsigned char *)c->publickey,
+ c->publickey_len, c->sign_callback, &c->sign_data);
+ break;
+ }
+ default:
+ rc = LIBSSH2_ERROR_AUTHENTICATION_FAILED;
}
} while (LIBSSH2_ERROR_EAGAIN == rc || LIBSSH2_ERROR_TIMEOUT == rc);
- return rc;
+ if (rc != 0) {
+ giterr_set(GITERR_NET, "Failed to authenticate SSH session");
+ return -1;
+ }
+
+ return 0;
}
static int _git_ssh_session_create(
LIBSSH2_SESSION** session,
gitno_socket socket)
{
- if (!session) {
- return -1;
- }
+ int rc = 0;
+ LIBSSH2_SESSION* s;
- LIBSSH2_SESSION* s = libssh2_session_init();
- if (!s)
+ assert(session);
+
+ s = libssh2_session_init();
+ if (!s) {
+ giterr_set(GITERR_NET, "Failed to initialize SSH session");
return -1;
+ }
- int rc = 0;
do {
rc = libssh2_session_startup(s, socket.socket);
} while (LIBSSH2_ERROR_EAGAIN == rc || LIBSSH2_ERROR_TIMEOUT == rc);
if (0 != rc) {
- goto on_error;
+ libssh2_session_free(s);
+ giterr_set(GITERR_NET, "Failed to start SSH session");
+ return -1;
}
libssh2_session_set_blocking(s, 1);
@@ -297,21 +296,13 @@ static int _git_ssh_session_create(
*session = s;
return 0;
-
-on_error:
- if (s) {
- libssh2_session_free(s), s = NULL;
- }
-
- return -1;
}
static int _git_ssh_setup_conn(
ssh_subtransport *t,
const char *url,
const char *cmd,
- git_smart_subtransport_stream **stream
-)
+ git_smart_subtransport_stream **stream)
{
char *host, *port=NULL, *user=NULL, *pass=NULL;
const char *default_port="22";
@@ -343,12 +334,17 @@ static int _git_ssh_setup_conn(
if (git_cred_userpass_plaintext_new(&t->cred, user, pass) < 0)
goto on_error;
} else if (t->owner->cred_acquire_cb) {
- if (t->owner->cred_acquire_cb(&t->cred,
- t->owner->url,
- user,
- GIT_CREDTYPE_USERPASS_PLAINTEXT | GIT_CREDTYPE_SSH_KEYFILE_PASSPHRASE,
+ if (t->owner->cred_acquire_cb(
+ &t->cred, t->owner->url, user,
+ GIT_CREDTYPE_USERPASS_PLAINTEXT |
+ GIT_CREDTYPE_SSH_KEYFILE_PASSPHRASE,
t->owner->cred_acquire_payload) < 0)
goto on_error;
+
+ if (!t->cred) {
+ giterr_set(GITERR_NET, "Callback failed to initialize SSH credentials");
+ goto on_error;
+ }
} else {
giterr_set(GITERR_NET, "Cannot set up SSH connection without credentials");
goto on_error;
@@ -357,17 +353,14 @@ static int _git_ssh_setup_conn(
if (!user) {
user = git__strdup(default_user);
+ GITERR_CHECK_ALLOC(user);
}
- if (_git_ssh_session_create(&session, s->socket) < 0) {
- giterr_set(GITERR_NET, "Failed to initialize SSH session");
+ if (_git_ssh_session_create(&session, s->socket) < 0)
goto on_error;
- }
- if (_git_ssh_authenticate_session(session, user, t->cred) < 0) {
- giterr_set(GITERR_NET, "Failed to authenticate SSH session");
+ if (_git_ssh_authenticate_session(session, user, t->cred) < 0)
goto on_error;
- }
channel = libssh2_channel_open_session(session);
if (!channel) {
@@ -402,7 +395,7 @@ on_error:
git__free(pass);
if (session)
- libssh2_session_free(session), session = NULL;
+ libssh2_session_free(session);
return -1;
}