mirror of
https://github.com/libssh2/libssh2.git
synced 2026-10-24 03:48:58 +03:00
tidy-up: drop most {} blocks around single-statements
Follow-up tob171deefde#2077 Follow-up to28824712fe#2004 Closes #2087
This commit is contained in:
@@ -188,9 +188,8 @@ int main(int argc, char *argv[])
|
||||
fprintf(stderr, "Authentication by public key failed.\n");
|
||||
goto shutdown;
|
||||
}
|
||||
else {
|
||||
else
|
||||
fprintf(stderr, "Authentication by public key succeeded.\n");
|
||||
}
|
||||
}
|
||||
else {
|
||||
fprintf(stderr, "No supported authentication methods found.\n");
|
||||
@@ -291,9 +290,8 @@ int main(int argc, char *argv[])
|
||||
while(wr < len) {
|
||||
ssize_t nwritten = libssh2_channel_write(channel, buf + wr,
|
||||
(size_t)(len - wr));
|
||||
if(nwritten == LIBSSH2_ERROR_EAGAIN) {
|
||||
if(nwritten == LIBSSH2_ERROR_EAGAIN)
|
||||
continue;
|
||||
}
|
||||
if(nwritten < 0) {
|
||||
fprintf(stderr, "libssh2_channel_write: %ld\n",
|
||||
(long)nwritten);
|
||||
|
||||
@@ -56,21 +56,16 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
#endif
|
||||
|
||||
if(argc > 1) {
|
||||
if(argc > 1)
|
||||
hostaddr = inet_addr(argv[1]);
|
||||
}
|
||||
else {
|
||||
else
|
||||
hostaddr = htonl(0x7F000001);
|
||||
}
|
||||
if(argc > 2) {
|
||||
if(argc > 2)
|
||||
username = argv[2];
|
||||
}
|
||||
if(argc > 3) {
|
||||
if(argc > 3)
|
||||
password = argv[3];
|
||||
}
|
||||
if(argc > 4) {
|
||||
if(argc > 4)
|
||||
scppath = argv[4];
|
||||
}
|
||||
|
||||
rc = libssh2_init(0);
|
||||
if(rc) {
|
||||
@@ -118,9 +113,8 @@ int main(int argc, char *argv[])
|
||||
*/
|
||||
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
|
||||
fprintf(stderr, "Fingerprint: ");
|
||||
for(i = 0; i < 20; i++) {
|
||||
for(i = 0; i < 20; i++)
|
||||
fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
|
||||
}
|
||||
fprintf(stderr, "\n");
|
||||
|
||||
if(auth_pw) {
|
||||
@@ -154,14 +148,12 @@ int main(int argc, char *argv[])
|
||||
int amount = sizeof(mem);
|
||||
ssize_t nread;
|
||||
|
||||
if((fileinfo.st_size - got) < amount) {
|
||||
if((fileinfo.st_size - got) < amount)
|
||||
amount = (int)(fileinfo.st_size - got);
|
||||
}
|
||||
|
||||
nread = libssh2_channel_read(channel, mem, (size_t)amount);
|
||||
if(nread > 0) {
|
||||
if(nread > 0)
|
||||
write(1, mem, (size_t)nread);
|
||||
}
|
||||
else if(nread < 0) {
|
||||
fprintf(stderr, "libssh2_channel_read() failed: %ld\n",
|
||||
(long)nread);
|
||||
|
||||
@@ -120,21 +120,16 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
#endif
|
||||
|
||||
if(argc > 1) {
|
||||
if(argc > 1)
|
||||
hostaddr = inet_addr(argv[1]);
|
||||
}
|
||||
else {
|
||||
else
|
||||
hostaddr = htonl(0x7F000001);
|
||||
}
|
||||
if(argc > 2) {
|
||||
if(argc > 2)
|
||||
username = argv[2];
|
||||
}
|
||||
if(argc > 3) {
|
||||
if(argc > 3)
|
||||
password = argv[3];
|
||||
}
|
||||
if(argc > 4) {
|
||||
if(argc > 4)
|
||||
scppath = argv[4];
|
||||
}
|
||||
|
||||
rc = libssh2_init(0);
|
||||
if(rc) {
|
||||
@@ -191,9 +186,8 @@ int main(int argc, char *argv[])
|
||||
*/
|
||||
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
|
||||
fprintf(stderr, "Fingerprint: ");
|
||||
for(i = 0; i < 20; i++) {
|
||||
for(i = 0; i < 20; i++)
|
||||
fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
|
||||
}
|
||||
fprintf(stderr, "\n");
|
||||
|
||||
if(auth_pw) {
|
||||
@@ -251,9 +245,8 @@ int main(int argc, char *argv[])
|
||||
do {
|
||||
int amount = sizeof(mem);
|
||||
|
||||
if((fileinfo.st_size - got) < amount) {
|
||||
if((fileinfo.st_size - got) < amount)
|
||||
amount = (int)(fileinfo.st_size - got);
|
||||
}
|
||||
|
||||
/* loop until we block */
|
||||
nread = libssh2_channel_read(channel, mem, (size_t)amount);
|
||||
|
||||
@@ -64,24 +64,18 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
#endif
|
||||
|
||||
if(argc > 1) {
|
||||
if(argc > 1)
|
||||
hostaddr = inet_addr(argv[1]);
|
||||
}
|
||||
else {
|
||||
else
|
||||
hostaddr = htonl(0x7F000001);
|
||||
}
|
||||
if(argc > 2) {
|
||||
if(argc > 2)
|
||||
username = argv[2];
|
||||
}
|
||||
if(argc > 3) {
|
||||
if(argc > 3)
|
||||
password = argv[3];
|
||||
}
|
||||
if(argc > 4) {
|
||||
if(argc > 4)
|
||||
loclfile = argv[4];
|
||||
}
|
||||
if(argc > 5) {
|
||||
if(argc > 5)
|
||||
scppath = argv[5];
|
||||
}
|
||||
|
||||
rc = libssh2_init(0);
|
||||
if(rc) {
|
||||
@@ -141,9 +135,8 @@ int main(int argc, char *argv[])
|
||||
*/
|
||||
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
|
||||
fprintf(stderr, "Fingerprint: ");
|
||||
for(i = 0; i < 20; i++) {
|
||||
for(i = 0; i < 20; i++)
|
||||
fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
|
||||
}
|
||||
fprintf(stderr, "\n");
|
||||
|
||||
if(auth_pw) {
|
||||
@@ -179,10 +172,8 @@ int main(int argc, char *argv[])
|
||||
fprintf(stderr, "SCP session waiting to send file\n");
|
||||
do {
|
||||
nread = fread(mem, 1, sizeof(mem), local);
|
||||
if(nread <= 0) {
|
||||
/* end of file */
|
||||
break;
|
||||
}
|
||||
if(nread <= 0)
|
||||
break; /* end of file */
|
||||
ptr = mem;
|
||||
|
||||
do {
|
||||
|
||||
@@ -112,24 +112,18 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
#endif
|
||||
|
||||
if(argc > 1) {
|
||||
if(argc > 1)
|
||||
hostaddr = inet_addr(argv[1]);
|
||||
}
|
||||
else {
|
||||
else
|
||||
hostaddr = htonl(0x7F000001);
|
||||
}
|
||||
if(argc > 2) {
|
||||
if(argc > 2)
|
||||
username = argv[2];
|
||||
}
|
||||
if(argc > 3) {
|
||||
if(argc > 3)
|
||||
password = argv[3];
|
||||
}
|
||||
if(argc > 4) {
|
||||
if(argc > 4)
|
||||
loclfile = argv[4];
|
||||
}
|
||||
if(argc > 5) {
|
||||
if(argc > 5)
|
||||
scppath = argv[5];
|
||||
}
|
||||
|
||||
rc = libssh2_init(0);
|
||||
if(rc) {
|
||||
@@ -194,9 +188,8 @@ int main(int argc, char *argv[])
|
||||
*/
|
||||
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
|
||||
fprintf(stderr, "Fingerprint: ");
|
||||
for(i = 0; i < 20; i++) {
|
||||
for(i = 0; i < 20; i++)
|
||||
fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
|
||||
}
|
||||
fprintf(stderr, "\n");
|
||||
|
||||
if(auth_pw) {
|
||||
@@ -241,10 +234,8 @@ int main(int argc, char *argv[])
|
||||
start = time(NULL);
|
||||
do {
|
||||
nread = fread(mem, 1, sizeof(mem), local);
|
||||
if(nread <= 0) {
|
||||
/* end of file */
|
||||
break;
|
||||
}
|
||||
if(nread <= 0)
|
||||
break; /* end of file */
|
||||
ptr = mem;
|
||||
|
||||
total += (libssh2_struct_stat_size)nread;
|
||||
|
||||
@@ -111,21 +111,16 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
#endif
|
||||
|
||||
if(argc > 1) {
|
||||
if(argc > 1)
|
||||
hostaddr = inet_addr(argv[1]);
|
||||
}
|
||||
else {
|
||||
else
|
||||
hostaddr = htonl(0x7F000001);
|
||||
}
|
||||
if(argc > 2) {
|
||||
if(argc > 2)
|
||||
username = argv[2];
|
||||
}
|
||||
if(argc > 3) {
|
||||
if(argc > 3)
|
||||
password = argv[3];
|
||||
}
|
||||
if(argc > 4) {
|
||||
if(argc > 4)
|
||||
sftppath = argv[4];
|
||||
}
|
||||
|
||||
rc = libssh2_init(0);
|
||||
if(rc) {
|
||||
@@ -177,9 +172,8 @@ int main(int argc, char *argv[])
|
||||
*/
|
||||
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
|
||||
fprintf(stderr, "Fingerprint: ");
|
||||
for(i = 0; i < 20; i++) {
|
||||
for(i = 0; i < 20; i++)
|
||||
fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
|
||||
}
|
||||
fprintf(stderr, "\n");
|
||||
|
||||
/* check what authentication methods are available */
|
||||
@@ -187,27 +181,21 @@ int main(int argc, char *argv[])
|
||||
(unsigned int)strlen(username));
|
||||
if(userauthlist) {
|
||||
fprintf(stderr, "Authentication methods: %s\n", userauthlist);
|
||||
if(strstr(userauthlist, "password")) {
|
||||
if(strstr(userauthlist, "password"))
|
||||
auth_pw |= 1;
|
||||
}
|
||||
if(strstr(userauthlist, "keyboard-interactive")) {
|
||||
if(strstr(userauthlist, "keyboard-interactive"))
|
||||
auth_pw |= 2;
|
||||
}
|
||||
if(strstr(userauthlist, "publickey")) {
|
||||
if(strstr(userauthlist, "publickey"))
|
||||
auth_pw |= 4;
|
||||
}
|
||||
|
||||
/* check for options */
|
||||
if(argc > 5) {
|
||||
if((auth_pw & 1) && !strcmp(argv[5], "-p")) {
|
||||
if((auth_pw & 1) && !strcmp(argv[5], "-p"))
|
||||
auth_pw = 1;
|
||||
}
|
||||
if((auth_pw & 2) && !strcmp(argv[5], "-i")) {
|
||||
if((auth_pw & 2) && !strcmp(argv[5], "-i"))
|
||||
auth_pw = 2;
|
||||
}
|
||||
if((auth_pw & 4) && !strcmp(argv[5], "-k")) {
|
||||
if((auth_pw & 4) && !strcmp(argv[5], "-k"))
|
||||
auth_pw = 4;
|
||||
}
|
||||
}
|
||||
|
||||
if(auth_pw & 1) {
|
||||
@@ -225,10 +213,9 @@ int main(int argc, char *argv[])
|
||||
"Authentication by keyboard-interactive failed.\n");
|
||||
goto shutdown;
|
||||
}
|
||||
else {
|
||||
else
|
||||
fprintf(stderr,
|
||||
"Authentication by keyboard-interactive succeeded.\n");
|
||||
}
|
||||
}
|
||||
else if(auth_pw & 4) {
|
||||
/* Or by public key */
|
||||
@@ -238,9 +225,8 @@ int main(int argc, char *argv[])
|
||||
fprintf(stderr, "Authentication by public key failed.\n");
|
||||
goto shutdown;
|
||||
}
|
||||
else {
|
||||
else
|
||||
fprintf(stderr, "Authentication by public key succeeded.\n");
|
||||
}
|
||||
}
|
||||
else {
|
||||
fprintf(stderr, "No supported authentication methods found.\n");
|
||||
@@ -274,12 +260,10 @@ int main(int argc, char *argv[])
|
||||
/* loop until we fail */
|
||||
fprintf(stderr, "libssh2_sftp_read().\n");
|
||||
nread = libssh2_sftp_read(sftp_handle, mem, sizeof(mem));
|
||||
if(nread > 0) {
|
||||
if(nread > 0)
|
||||
write(1, mem, (size_t)nread);
|
||||
}
|
||||
else {
|
||||
else
|
||||
break;
|
||||
}
|
||||
} while(1);
|
||||
|
||||
libssh2_sftp_close(sftp_handle);
|
||||
|
||||
@@ -108,18 +108,14 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
#endif
|
||||
|
||||
if(argc > 1) {
|
||||
if(argc > 1)
|
||||
username = argv[1];
|
||||
}
|
||||
if(argc > 2) {
|
||||
if(argc > 2)
|
||||
password = argv[2];
|
||||
}
|
||||
if(argc > 3) {
|
||||
if(argc > 3)
|
||||
sftppath = argv[3];
|
||||
}
|
||||
if(argc > 4) {
|
||||
if(argc > 4)
|
||||
dest = argv[4];
|
||||
}
|
||||
|
||||
rc = libssh2_init(0);
|
||||
if(rc) {
|
||||
@@ -169,9 +165,8 @@ int main(int argc, char *argv[])
|
||||
*/
|
||||
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
|
||||
fprintf(stderr, "Fingerprint: ");
|
||||
for(i = 0; i < 20; i++) {
|
||||
for(i = 0; i < 20; i++)
|
||||
fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
|
||||
}
|
||||
fprintf(stderr, "\n");
|
||||
|
||||
tempstorage = fopen(storage, "wb");
|
||||
@@ -251,10 +246,8 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
} while(nread > 0);
|
||||
|
||||
if(nread != LIBSSH2_ERROR_EAGAIN) {
|
||||
/* error or end of file */
|
||||
break;
|
||||
}
|
||||
if(nread != LIBSSH2_ERROR_EAGAIN)
|
||||
break; /* error or end of file */
|
||||
|
||||
timeout.tv_sec = 10;
|
||||
timeout.tv_usec = 0;
|
||||
@@ -306,10 +299,8 @@ int main(int argc, char *argv[])
|
||||
do {
|
||||
ssize_t nwritten;
|
||||
nread = fread(mem, 1, sizeof(mem), tempstorage);
|
||||
if(nread <= 0) {
|
||||
/* end of file */
|
||||
break;
|
||||
}
|
||||
if(nread <= 0)
|
||||
break; /* end of file */
|
||||
ptr = mem;
|
||||
|
||||
do {
|
||||
@@ -321,10 +312,8 @@ int main(int argc, char *argv[])
|
||||
nread -= (size_t)nwritten;
|
||||
} while(nwritten >= 0);
|
||||
|
||||
if(nwritten != LIBSSH2_ERROR_EAGAIN) {
|
||||
/* error or end of file */
|
||||
break;
|
||||
}
|
||||
if(nwritten != LIBSSH2_ERROR_EAGAIN)
|
||||
break; /* error or end of file */
|
||||
|
||||
timeout.tv_sec = 10;
|
||||
timeout.tv_usec = 0;
|
||||
@@ -352,9 +341,8 @@ int main(int argc, char *argv[])
|
||||
} while(1);
|
||||
fprintf(stderr, "SFTP upload done.\n");
|
||||
}
|
||||
else {
|
||||
else
|
||||
fprintf(stderr, "SFTP failed to open destination path: %s\n", dest);
|
||||
}
|
||||
|
||||
libssh2_sftp_shutdown(sftp_session);
|
||||
|
||||
|
||||
@@ -64,24 +64,18 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
#endif
|
||||
|
||||
if(argc > 1) {
|
||||
if(argc > 1)
|
||||
hostaddr = inet_addr(argv[1]);
|
||||
}
|
||||
else {
|
||||
else
|
||||
hostaddr = htonl(0x7F000001);
|
||||
}
|
||||
if(argc > 2) {
|
||||
if(argc > 2)
|
||||
username = argv[2];
|
||||
}
|
||||
if(argc > 3) {
|
||||
if(argc > 3)
|
||||
password = argv[3];
|
||||
}
|
||||
if(argc > 4) {
|
||||
if(argc > 4)
|
||||
loclfile = argv[4];
|
||||
}
|
||||
if(argc > 5) {
|
||||
if(argc > 5)
|
||||
sftppath = argv[5];
|
||||
}
|
||||
|
||||
rc = libssh2_init(0);
|
||||
if(rc) {
|
||||
@@ -139,9 +133,8 @@ int main(int argc, char *argv[])
|
||||
*/
|
||||
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
|
||||
fprintf(stderr, "Fingerprint: ");
|
||||
for(i = 0; i < 20; i++) {
|
||||
for(i = 0; i < 20; i++)
|
||||
fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
|
||||
}
|
||||
fprintf(stderr, "\n");
|
||||
|
||||
if(auth_pw) {
|
||||
@@ -202,10 +195,8 @@ int main(int argc, char *argv[])
|
||||
fprintf(stderr, "libssh2_sftp_open() is done, now send data.\n");
|
||||
do {
|
||||
nread = fread(mem, 1, sizeof(mem), local);
|
||||
if(nread <= 0) {
|
||||
/* end of file */
|
||||
break;
|
||||
}
|
||||
if(nread <= 0)
|
||||
break; /* end of file */
|
||||
ptr = mem;
|
||||
|
||||
do {
|
||||
|
||||
@@ -56,21 +56,16 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
#endif
|
||||
|
||||
if(argc > 1) {
|
||||
if(argc > 1)
|
||||
hostaddr = inet_addr(argv[1]);
|
||||
}
|
||||
else {
|
||||
else
|
||||
hostaddr = htonl(0x7F000001);
|
||||
}
|
||||
if(argc > 2) {
|
||||
if(argc > 2)
|
||||
username = argv[2];
|
||||
}
|
||||
if(argc > 3) {
|
||||
if(argc > 3)
|
||||
password = argv[3];
|
||||
}
|
||||
if(argc > 4) {
|
||||
if(argc > 4)
|
||||
sftppath = argv[4];
|
||||
}
|
||||
|
||||
rc = libssh2_init(0);
|
||||
if(rc) {
|
||||
@@ -119,9 +114,8 @@ int main(int argc, char *argv[])
|
||||
*/
|
||||
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
|
||||
fprintf(stderr, "Fingerprint: ");
|
||||
for(i = 0; i < 20; i++) {
|
||||
for(i = 0; i < 20; i++)
|
||||
fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
|
||||
}
|
||||
fprintf(stderr, "\n");
|
||||
|
||||
if(auth_pw) {
|
||||
|
||||
@@ -56,21 +56,16 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
#endif
|
||||
|
||||
if(argc > 1) {
|
||||
if(argc > 1)
|
||||
hostaddr = inet_addr(argv[1]);
|
||||
}
|
||||
else {
|
||||
else
|
||||
hostaddr = htonl(0x7F000001);
|
||||
}
|
||||
if(argc > 2) {
|
||||
if(argc > 2)
|
||||
username = argv[2];
|
||||
}
|
||||
if(argc > 3) {
|
||||
if(argc > 3)
|
||||
password = argv[3];
|
||||
}
|
||||
if(argc > 4) {
|
||||
if(argc > 4)
|
||||
sftppath = argv[4];
|
||||
}
|
||||
|
||||
rc = libssh2_init(0);
|
||||
if(rc) {
|
||||
@@ -119,9 +114,8 @@ int main(int argc, char *argv[])
|
||||
*/
|
||||
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
|
||||
fprintf(stderr, "Fingerprint: ");
|
||||
for(i = 0; i < 20; i++) {
|
||||
for(i = 0; i < 20; i++)
|
||||
fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
|
||||
}
|
||||
fprintf(stderr, "\n");
|
||||
|
||||
if(auth_pw) {
|
||||
|
||||
@@ -120,21 +120,16 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
#endif
|
||||
|
||||
if(argc > 1) {
|
||||
if(argc > 1)
|
||||
hostaddr = inet_addr(argv[1]);
|
||||
}
|
||||
else {
|
||||
else
|
||||
hostaddr = htonl(0x7F000001);
|
||||
}
|
||||
if(argc > 2) {
|
||||
if(argc > 2)
|
||||
username = argv[2];
|
||||
}
|
||||
if(argc > 3) {
|
||||
if(argc > 3)
|
||||
password = argv[3];
|
||||
}
|
||||
if(argc > 4) {
|
||||
if(argc > 4)
|
||||
sftppath = argv[4];
|
||||
}
|
||||
|
||||
rc = libssh2_init(0);
|
||||
if(rc) {
|
||||
@@ -192,9 +187,8 @@ int main(int argc, char *argv[])
|
||||
*/
|
||||
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
|
||||
fprintf(stderr, "Fingerprint: ");
|
||||
for(i = 0; i < 20; i++) {
|
||||
for(i = 0; i < 20; i++)
|
||||
fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
|
||||
}
|
||||
fprintf(stderr, "\n");
|
||||
|
||||
if(auth_pw) {
|
||||
@@ -271,9 +265,8 @@ int main(int argc, char *argv[])
|
||||
total += nread;
|
||||
write(1, mem, (size_t)nread);
|
||||
}
|
||||
else {
|
||||
else
|
||||
break;
|
||||
}
|
||||
} while(1);
|
||||
|
||||
#ifdef HAVE_GETTIMEOFDAY
|
||||
|
||||
@@ -63,24 +63,18 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
#endif
|
||||
|
||||
if(argc > 1) {
|
||||
if(argc > 1)
|
||||
hostaddr = inet_addr(argv[1]);
|
||||
}
|
||||
else {
|
||||
else
|
||||
hostaddr = htonl(0x7F000001);
|
||||
}
|
||||
if(argc > 2) {
|
||||
if(argc > 2)
|
||||
username = argv[2];
|
||||
}
|
||||
if(argc > 3) {
|
||||
if(argc > 3)
|
||||
password = argv[3];
|
||||
}
|
||||
if(argc > 4) {
|
||||
if(argc > 4)
|
||||
loclfile = argv[4];
|
||||
}
|
||||
if(argc > 5) {
|
||||
if(argc > 5)
|
||||
sftppath = argv[5];
|
||||
}
|
||||
|
||||
rc = libssh2_init(0);
|
||||
if(rc) {
|
||||
@@ -138,9 +132,8 @@ int main(int argc, char *argv[])
|
||||
*/
|
||||
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
|
||||
fprintf(stderr, "Fingerprint: ");
|
||||
for(i = 0; i < 20; i++) {
|
||||
for(i = 0; i < 20; i++)
|
||||
fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
|
||||
}
|
||||
fprintf(stderr, "\n");
|
||||
|
||||
if(auth_pw) {
|
||||
@@ -187,10 +180,8 @@ int main(int argc, char *argv[])
|
||||
fprintf(stderr, "libssh2_sftp_open() is done, now send data.\n");
|
||||
do {
|
||||
nread = fread(mem, 1, sizeof(mem), local);
|
||||
if(nread <= 0) {
|
||||
/* end of file */
|
||||
break;
|
||||
}
|
||||
if(nread <= 0)
|
||||
break; /* end of file */
|
||||
ptr = mem;
|
||||
|
||||
do {
|
||||
|
||||
@@ -110,24 +110,18 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
#endif
|
||||
|
||||
if(argc > 1) {
|
||||
if(argc > 1)
|
||||
hostaddr = inet_addr(argv[1]);
|
||||
}
|
||||
else {
|
||||
else
|
||||
hostaddr = htonl(0x7F000001);
|
||||
}
|
||||
if(argc > 2) {
|
||||
if(argc > 2)
|
||||
username = argv[2];
|
||||
}
|
||||
if(argc > 3) {
|
||||
if(argc > 3)
|
||||
password = argv[3];
|
||||
}
|
||||
if(argc > 4) {
|
||||
if(argc > 4)
|
||||
loclfile = argv[4];
|
||||
}
|
||||
if(argc > 5) {
|
||||
if(argc > 5)
|
||||
sftppath = argv[5];
|
||||
}
|
||||
|
||||
rc = libssh2_init(0);
|
||||
if(rc) {
|
||||
@@ -187,9 +181,8 @@ int main(int argc, char *argv[])
|
||||
*/
|
||||
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
|
||||
fprintf(stderr, "Fingerprint: ");
|
||||
for(i = 0; i < 20; i++) {
|
||||
for(i = 0; i < 20; i++)
|
||||
fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
|
||||
}
|
||||
fprintf(stderr, "\n");
|
||||
|
||||
if(auth_pw) {
|
||||
@@ -249,10 +242,8 @@ int main(int argc, char *argv[])
|
||||
start = time(NULL);
|
||||
do {
|
||||
nread = fread(mem, 1, sizeof(mem), local);
|
||||
if(nread <= 0) {
|
||||
/* end of file */
|
||||
break;
|
||||
}
|
||||
if(nread <= 0)
|
||||
break; /* end of file */
|
||||
ptr = mem;
|
||||
|
||||
total += (libssh2_struct_stat_size)nread;
|
||||
@@ -260,9 +251,9 @@ int main(int argc, char *argv[])
|
||||
do {
|
||||
/* write data in a loop until we block */
|
||||
while((nwritten = libssh2_sftp_write(sftp_handle, ptr, nread)) ==
|
||||
LIBSSH2_ERROR_EAGAIN) {
|
||||
LIBSSH2_ERROR_EAGAIN)
|
||||
waitsocket(sock, session);
|
||||
}
|
||||
|
||||
if(nwritten < 0)
|
||||
break;
|
||||
ptr += nwritten;
|
||||
|
||||
@@ -111,24 +111,18 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
#endif
|
||||
|
||||
if(argc > 1) {
|
||||
if(argc > 1)
|
||||
hostaddr = inet_addr(argv[1]);
|
||||
}
|
||||
else {
|
||||
else
|
||||
hostaddr = htonl(0x7F000001);
|
||||
}
|
||||
if(argc > 2) {
|
||||
if(argc > 2)
|
||||
username = argv[2];
|
||||
}
|
||||
if(argc > 3) {
|
||||
if(argc > 3)
|
||||
password = argv[3];
|
||||
}
|
||||
if(argc > 4) {
|
||||
if(argc > 4)
|
||||
loclfile = argv[4];
|
||||
}
|
||||
if(argc > 5) {
|
||||
if(argc > 5)
|
||||
sftppath = argv[5];
|
||||
}
|
||||
|
||||
rc = libssh2_init(0);
|
||||
if(rc) {
|
||||
@@ -188,9 +182,8 @@ int main(int argc, char *argv[])
|
||||
*/
|
||||
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
|
||||
fprintf(stderr, "Fingerprint: ");
|
||||
for(i = 0; i < 20; i++) {
|
||||
for(i = 0; i < 20; i++)
|
||||
fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
|
||||
}
|
||||
fprintf(stderr, "\n");
|
||||
|
||||
if(auth_pw) {
|
||||
@@ -254,8 +247,7 @@ int main(int argc, char *argv[])
|
||||
if(nread <= 0) {
|
||||
/* end of file */
|
||||
if(memuse > 0)
|
||||
/* the previous sending is not finished */
|
||||
nread = 0;
|
||||
nread = 0; /* the previous sending is not finished */
|
||||
else
|
||||
break;
|
||||
}
|
||||
@@ -276,8 +268,7 @@ int main(int argc, char *argv[])
|
||||
memuse -= (size_t)nwritten;
|
||||
}
|
||||
else
|
||||
/* 'mem' was consumed fully */
|
||||
memuse = 0;
|
||||
memuse = 0; /* 'mem' was consumed fully */
|
||||
|
||||
} while(nwritten > 0);
|
||||
|
||||
|
||||
@@ -84,21 +84,16 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
#endif
|
||||
|
||||
if(argc > 1) {
|
||||
if(argc > 1)
|
||||
hostaddr = inet_addr(argv[1]);
|
||||
}
|
||||
else {
|
||||
else
|
||||
hostaddr = htonl(0x7F000001);
|
||||
}
|
||||
if(argc > 2) {
|
||||
if(argc > 2)
|
||||
username = argv[2];
|
||||
}
|
||||
if(argc > 3) {
|
||||
if(argc > 3)
|
||||
password = argv[3];
|
||||
}
|
||||
if(argc > 4) {
|
||||
if(argc > 4)
|
||||
sftppath = argv[4];
|
||||
}
|
||||
|
||||
rc = libssh2_init(0);
|
||||
if(rc) {
|
||||
@@ -147,9 +142,8 @@ int main(int argc, char *argv[])
|
||||
*/
|
||||
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
|
||||
fprintf(stderr, "Fingerprint: ");
|
||||
for(i = 0; i < 20; i++) {
|
||||
for(i = 0; i < 20; i++)
|
||||
fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
|
||||
}
|
||||
fprintf(stderr, "\n");
|
||||
|
||||
/* check what authentication methods are available */
|
||||
@@ -157,27 +151,21 @@ int main(int argc, char *argv[])
|
||||
(unsigned int)strlen(username));
|
||||
if(userauthlist) {
|
||||
fprintf(stderr, "Authentication methods: %s\n", userauthlist);
|
||||
if(strstr(userauthlist, "password")) {
|
||||
if(strstr(userauthlist, "password"))
|
||||
auth_pw |= 1;
|
||||
}
|
||||
if(strstr(userauthlist, "keyboard-interactive")) {
|
||||
if(strstr(userauthlist, "keyboard-interactive"))
|
||||
auth_pw |= 2;
|
||||
}
|
||||
if(strstr(userauthlist, "publickey")) {
|
||||
if(strstr(userauthlist, "publickey"))
|
||||
auth_pw |= 4;
|
||||
}
|
||||
|
||||
/* check for options */
|
||||
if(argc > 5) {
|
||||
if((auth_pw & 1) && !strcmp(argv[5], "-p")) {
|
||||
if((auth_pw & 1) && !strcmp(argv[5], "-p"))
|
||||
auth_pw = 1;
|
||||
}
|
||||
if((auth_pw & 2) && !strcmp(argv[5], "-i")) {
|
||||
if((auth_pw & 2) && !strcmp(argv[5], "-i"))
|
||||
auth_pw = 2;
|
||||
}
|
||||
if((auth_pw & 4) && !strcmp(argv[5], "-k")) {
|
||||
if((auth_pw & 4) && !strcmp(argv[5], "-k"))
|
||||
auth_pw = 4;
|
||||
}
|
||||
}
|
||||
|
||||
if(auth_pw & 1) {
|
||||
@@ -195,10 +183,9 @@ int main(int argc, char *argv[])
|
||||
"Authentication by keyboard-interactive failed.\n");
|
||||
goto shutdown;
|
||||
}
|
||||
else {
|
||||
else
|
||||
fprintf(stderr,
|
||||
"Authentication by keyboard-interactive succeeded.\n");
|
||||
}
|
||||
}
|
||||
else if(auth_pw & 4) {
|
||||
/* Or by public key */
|
||||
@@ -208,9 +195,8 @@ int main(int argc, char *argv[])
|
||||
fprintf(stderr, "Authentication by public key failed.\n");
|
||||
goto shutdown;
|
||||
}
|
||||
else {
|
||||
else
|
||||
fprintf(stderr, "Authentication by public key succeeded.\n");
|
||||
}
|
||||
}
|
||||
else {
|
||||
fprintf(stderr, "No supported authentication methods found.\n");
|
||||
@@ -249,36 +235,29 @@ int main(int argc, char *argv[])
|
||||
if(rc > 0) {
|
||||
/* rc is the length of the filename in the mem buffer */
|
||||
|
||||
if(longentry[0] != '\0') {
|
||||
if(longentry[0] != '\0')
|
||||
printf("%s\n", longentry);
|
||||
}
|
||||
else {
|
||||
if(attrs.flags & LIBSSH2_SFTP_ATTR_PERMISSIONS) {
|
||||
if(attrs.flags & LIBSSH2_SFTP_ATTR_PERMISSIONS)
|
||||
/* this should check what permissions it
|
||||
is and print the output accordingly */
|
||||
printf("--fix----- ");
|
||||
}
|
||||
else {
|
||||
else
|
||||
printf("---------- ");
|
||||
}
|
||||
|
||||
if(attrs.flags & LIBSSH2_SFTP_ATTR_UIDGID) {
|
||||
if(attrs.flags & LIBSSH2_SFTP_ATTR_UIDGID)
|
||||
printf("%4d %4d ", (int)attrs.uid, (int)attrs.gid);
|
||||
}
|
||||
else {
|
||||
else
|
||||
printf(" - - ");
|
||||
}
|
||||
|
||||
if(attrs.flags & LIBSSH2_SFTP_ATTR_SIZE) {
|
||||
if(attrs.flags & LIBSSH2_SFTP_ATTR_SIZE)
|
||||
printf("%8" LIBSSH2_FILESIZE_MASK " ", attrs.filesize);
|
||||
}
|
||||
|
||||
printf("%s\n", mem);
|
||||
}
|
||||
}
|
||||
else {
|
||||
else
|
||||
break;
|
||||
}
|
||||
|
||||
} while(1);
|
||||
|
||||
|
||||
@@ -63,21 +63,16 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
#endif
|
||||
|
||||
if(argc > 1) {
|
||||
if(argc > 1)
|
||||
hostaddr = inet_addr(argv[1]);
|
||||
}
|
||||
else {
|
||||
else
|
||||
hostaddr = htonl(0x7F000001);
|
||||
}
|
||||
if(argc > 2) {
|
||||
if(argc > 2)
|
||||
username = argv[2];
|
||||
}
|
||||
if(argc > 3) {
|
||||
if(argc > 3)
|
||||
password = argv[3];
|
||||
}
|
||||
if(argc > 4) {
|
||||
if(argc > 4)
|
||||
sftppath = argv[4];
|
||||
}
|
||||
|
||||
rc = libssh2_init(0);
|
||||
if(rc) {
|
||||
@@ -131,9 +126,8 @@ int main(int argc, char *argv[])
|
||||
*/
|
||||
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
|
||||
fprintf(stderr, "Fingerprint: ");
|
||||
for(i = 0; i < 20; i++) {
|
||||
for(i = 0; i < 20; i++)
|
||||
fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
|
||||
}
|
||||
fprintf(stderr, "\n");
|
||||
|
||||
if(auth_pw) {
|
||||
@@ -194,35 +188,27 @@ int main(int argc, char *argv[])
|
||||
if(rc > 0) {
|
||||
/* rc is the length of the filename in the mem buffer */
|
||||
|
||||
if(attrs.flags & LIBSSH2_SFTP_ATTR_PERMISSIONS) {
|
||||
if(attrs.flags & LIBSSH2_SFTP_ATTR_PERMISSIONS)
|
||||
/* this should check what permissions it
|
||||
is and print the output accordingly */
|
||||
printf("--fix----- ");
|
||||
}
|
||||
else {
|
||||
else
|
||||
printf("---------- ");
|
||||
}
|
||||
|
||||
if(attrs.flags & LIBSSH2_SFTP_ATTR_UIDGID) {
|
||||
if(attrs.flags & LIBSSH2_SFTP_ATTR_UIDGID)
|
||||
printf("%4d %4d ", (int)attrs.uid, (int)attrs.gid);
|
||||
}
|
||||
else {
|
||||
else
|
||||
printf(" - - ");
|
||||
}
|
||||
|
||||
if(attrs.flags & LIBSSH2_SFTP_ATTR_SIZE) {
|
||||
if(attrs.flags & LIBSSH2_SFTP_ATTR_SIZE)
|
||||
printf("%8" LIBSSH2_FILESIZE_MASK " ", attrs.filesize);
|
||||
}
|
||||
|
||||
printf("%s\n", mem);
|
||||
}
|
||||
else if(rc == LIBSSH2_ERROR_EAGAIN) {
|
||||
/* blocking */
|
||||
else if(rc == LIBSSH2_ERROR_EAGAIN)
|
||||
fprintf(stderr, "Blocking\n");
|
||||
}
|
||||
else {
|
||||
else
|
||||
break;
|
||||
}
|
||||
|
||||
} while(1);
|
||||
|
||||
|
||||
@@ -81,18 +81,14 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
#endif
|
||||
|
||||
if(argc > 1) {
|
||||
if(argc > 1)
|
||||
hostaddr = inet_addr(argv[1]);
|
||||
}
|
||||
else {
|
||||
else
|
||||
hostaddr = htonl(0x7F000001);
|
||||
}
|
||||
if(argc > 2) {
|
||||
if(argc > 2)
|
||||
username = argv[2];
|
||||
}
|
||||
if(argc > 3) {
|
||||
if(argc > 3)
|
||||
password = argv[3];
|
||||
}
|
||||
|
||||
rc = libssh2_init(0);
|
||||
if(rc) {
|
||||
@@ -149,9 +145,8 @@ int main(int argc, char *argv[])
|
||||
*/
|
||||
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
|
||||
fprintf(stderr, "Fingerprint: ");
|
||||
for(i = 0; i < 20; i++) {
|
||||
for(i = 0; i < 20; i++)
|
||||
fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
|
||||
}
|
||||
fprintf(stderr, "\n");
|
||||
|
||||
/* check what authentication methods are available */
|
||||
@@ -159,27 +154,21 @@ int main(int argc, char *argv[])
|
||||
(unsigned int)strlen(username));
|
||||
if(userauthlist) {
|
||||
fprintf(stderr, "Authentication methods: %s\n", userauthlist);
|
||||
if(strstr(userauthlist, "password")) {
|
||||
if(strstr(userauthlist, "password"))
|
||||
auth_pw |= 1;
|
||||
}
|
||||
if(strstr(userauthlist, "keyboard-interactive")) {
|
||||
if(strstr(userauthlist, "keyboard-interactive"))
|
||||
auth_pw |= 2;
|
||||
}
|
||||
if(strstr(userauthlist, "publickey")) {
|
||||
if(strstr(userauthlist, "publickey"))
|
||||
auth_pw |= 4;
|
||||
}
|
||||
|
||||
/* check for options */
|
||||
if(argc > 4) {
|
||||
if((auth_pw & 1) && !strcmp(argv[4], "-p")) {
|
||||
if((auth_pw & 1) && !strcmp(argv[4], "-p"))
|
||||
auth_pw = 1;
|
||||
}
|
||||
if((auth_pw & 2) && !strcmp(argv[4], "-i")) {
|
||||
if((auth_pw & 2) && !strcmp(argv[4], "-i"))
|
||||
auth_pw = 2;
|
||||
}
|
||||
if((auth_pw & 4) && !strcmp(argv[4], "-k")) {
|
||||
if((auth_pw & 4) && !strcmp(argv[4], "-k"))
|
||||
auth_pw = 4;
|
||||
}
|
||||
}
|
||||
|
||||
if(auth_pw & 1) {
|
||||
@@ -188,9 +177,8 @@ int main(int argc, char *argv[])
|
||||
fprintf(stderr, "Authentication by password failed.\n");
|
||||
goto shutdown;
|
||||
}
|
||||
else {
|
||||
else
|
||||
fprintf(stderr, "Authentication by password succeeded.\n");
|
||||
}
|
||||
}
|
||||
else if(auth_pw & 2) {
|
||||
/* Or via keyboard-interactive */
|
||||
@@ -200,10 +188,9 @@ int main(int argc, char *argv[])
|
||||
"Authentication by keyboard-interactive failed.\n");
|
||||
goto shutdown;
|
||||
}
|
||||
else {
|
||||
else
|
||||
fprintf(stderr,
|
||||
"Authentication by keyboard-interactive succeeded.\n");
|
||||
}
|
||||
}
|
||||
else if(auth_pw & 4) {
|
||||
/* Or by public key */
|
||||
@@ -243,9 +230,8 @@ int main(int argc, char *argv[])
|
||||
free(fn1);
|
||||
goto shutdown;
|
||||
}
|
||||
else {
|
||||
else
|
||||
fprintf(stderr, "Authentication by public key succeeded.\n");
|
||||
}
|
||||
free(fn2);
|
||||
free(fn1);
|
||||
}
|
||||
@@ -272,9 +258,8 @@ int main(int argc, char *argv[])
|
||||
* an interactive shell.
|
||||
*/
|
||||
#if 0
|
||||
if(libssh2_channel_request_pty(channel, "vanilla")) {
|
||||
if(libssh2_channel_request_pty(channel, "vanilla"))
|
||||
fprintf(stderr, "Failed requesting pty\n");
|
||||
}
|
||||
#endif
|
||||
|
||||
if(argc > 5) {
|
||||
@@ -318,9 +303,8 @@ int main(int argc, char *argv[])
|
||||
ssize_t err = libssh2_channel_read(channel, buf, sizeof(buf));
|
||||
if(err < 0)
|
||||
fprintf(stderr, "Unable to read response: %ld\n", (long)err);
|
||||
else {
|
||||
else
|
||||
fwrite(buf, 1, (size_t)err, stdout);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -54,15 +54,12 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
#endif
|
||||
|
||||
if(argc > 1) {
|
||||
if(argc > 1)
|
||||
hostaddr = inet_addr(argv[1]);
|
||||
}
|
||||
else {
|
||||
else
|
||||
hostaddr = htonl(0x7F000001);
|
||||
}
|
||||
if(argc > 2) {
|
||||
if(argc > 2)
|
||||
username = argv[2];
|
||||
}
|
||||
|
||||
rc = libssh2_init(0);
|
||||
if(rc) {
|
||||
@@ -108,9 +105,8 @@ int main(int argc, char *argv[])
|
||||
*/
|
||||
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
|
||||
fprintf(stderr, "Fingerprint: ");
|
||||
for(i = 0; i < 20; i++) {
|
||||
for(i = 0; i < 20; i++)
|
||||
fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
|
||||
}
|
||||
fprintf(stderr, "\n");
|
||||
|
||||
/* check what authentication methods are available */
|
||||
@@ -150,11 +146,10 @@ int main(int argc, char *argv[])
|
||||
rc = 1;
|
||||
goto shutdown;
|
||||
}
|
||||
if(libssh2_agent_userauth(agent, username, identity)) {
|
||||
if(libssh2_agent_userauth(agent, username, identity))
|
||||
fprintf(stderr, "Authentication with username %s and "
|
||||
"public key %s failed.\n",
|
||||
username, identity->comment);
|
||||
}
|
||||
else {
|
||||
fprintf(stderr, "Authentication with username %s and "
|
||||
"public key %s succeeded.\n",
|
||||
|
||||
@@ -111,9 +111,8 @@ int main(int argc, char *argv[])
|
||||
hostname = argv[1];
|
||||
username = argv[2];
|
||||
|
||||
if(argc > 3) {
|
||||
if(argc > 3)
|
||||
commandline = argv[3];
|
||||
}
|
||||
|
||||
rc = libssh2_init(0);
|
||||
if(rc) {
|
||||
@@ -179,11 +178,10 @@ int main(int argc, char *argv[])
|
||||
rc = 1;
|
||||
goto shutdown;
|
||||
}
|
||||
if(libssh2_agent_userauth(agent, username, identity)) {
|
||||
if(libssh2_agent_userauth(agent, username, identity))
|
||||
fprintf(stderr, "Authentication with username %s and "
|
||||
"public key %s failed.\n",
|
||||
username, identity->comment);
|
||||
}
|
||||
else {
|
||||
fprintf(stderr, "Authentication with username %s and "
|
||||
"public key %s succeeded.\n",
|
||||
@@ -217,26 +215,26 @@ int main(int argc, char *argv[])
|
||||
fprintf(stderr, "Error\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
while((rc = libssh2_channel_request_auth_agent(channel)) ==
|
||||
LIBSSH2_ERROR_EAGAIN) {
|
||||
LIBSSH2_ERROR_EAGAIN)
|
||||
waitsocket(sock, session);
|
||||
}
|
||||
if(rc) {
|
||||
fprintf(stderr, "Error, could not request auth agent, "
|
||||
"error code %d.\n", rc);
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
else
|
||||
fprintf(stdout, "Agent forwarding request succeeded.\n");
|
||||
}
|
||||
|
||||
while((rc = libssh2_channel_exec(channel, commandline)) ==
|
||||
LIBSSH2_ERROR_EAGAIN) {
|
||||
LIBSSH2_ERROR_EAGAIN)
|
||||
waitsocket(sock, session);
|
||||
}
|
||||
if(rc) {
|
||||
fprintf(stderr, "Error\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
for(;;) {
|
||||
ssize_t nread;
|
||||
/* loop until we block */
|
||||
@@ -251,25 +249,22 @@ int main(int argc, char *argv[])
|
||||
fputc(buffer[i], stderr);
|
||||
fprintf(stderr, "\n");
|
||||
}
|
||||
else if(nread < 0 && nread != LIBSSH2_ERROR_EAGAIN) {
|
||||
else if(nread < 0 && nread != LIBSSH2_ERROR_EAGAIN)
|
||||
/* no need to output this for the EAGAIN case */
|
||||
fprintf(stderr, "libssh2_channel_read returned %ld\n",
|
||||
(long)nread);
|
||||
}
|
||||
} while(nread > 0);
|
||||
|
||||
/* this is due to blocking that would occur otherwise so we loop on
|
||||
this condition */
|
||||
if(nread == LIBSSH2_ERROR_EAGAIN) {
|
||||
if(nread == LIBSSH2_ERROR_EAGAIN)
|
||||
waitsocket(sock, session);
|
||||
}
|
||||
else
|
||||
break;
|
||||
}
|
||||
exitcode = 127;
|
||||
while((rc = libssh2_channel_close(channel)) == LIBSSH2_ERROR_EAGAIN) {
|
||||
while((rc = libssh2_channel_close(channel)) == LIBSSH2_ERROR_EAGAIN)
|
||||
waitsocket(sock, session);
|
||||
}
|
||||
if(rc == 0) {
|
||||
exitcode = libssh2_channel_get_exit_status(channel);
|
||||
libssh2_channel_get_exit_signal(channel, &exitsignal,
|
||||
@@ -278,14 +273,12 @@ int main(int argc, char *argv[])
|
||||
|
||||
rc = 0;
|
||||
|
||||
if(exitsignal) {
|
||||
if(exitsignal)
|
||||
fprintf(stderr, "\nGot signal: %s\n",
|
||||
exitsignal ? exitsignal : "none");
|
||||
}
|
||||
else {
|
||||
else
|
||||
fprintf(stderr, "\nEXIT: %d bytecount: %ld\n",
|
||||
exitcode, (long)bytecount);
|
||||
}
|
||||
|
||||
libssh2_channel_free(channel);
|
||||
|
||||
|
||||
@@ -105,15 +105,12 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
#endif
|
||||
|
||||
if(argc > 1) {
|
||||
if(argc > 1)
|
||||
hostname = argv[1]; /* must be ip address only */
|
||||
}
|
||||
if(argc > 2) {
|
||||
if(argc > 2)
|
||||
username = argv[2];
|
||||
}
|
||||
if(argc > 3) {
|
||||
if(argc > 3)
|
||||
password = argv[3];
|
||||
}
|
||||
|
||||
rc = libssh2_init(0);
|
||||
if(rc) {
|
||||
@@ -226,10 +223,10 @@ int main(int argc, char *argv[])
|
||||
fprintf(stderr, "Error\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
while((rc = libssh2_channel_exec(channel, commandline)) ==
|
||||
LIBSSH2_ERROR_EAGAIN) {
|
||||
LIBSSH2_ERROR_EAGAIN)
|
||||
waitsocket(sock, session);
|
||||
}
|
||||
if(rc) {
|
||||
fprintf(stderr, "exec error\n");
|
||||
return 1;
|
||||
@@ -309,9 +306,8 @@ int main(int argc, char *argv[])
|
||||
totwritten += (size_t)n;
|
||||
fprintf(stderr, "wrote %ld bytes (%lu in total)",
|
||||
(long)n, (unsigned long)totwritten);
|
||||
if(left >= bufsize && (size_t)n != bufsize) {
|
||||
if(left >= bufsize && (size_t)n != bufsize)
|
||||
fprintf(stderr, " PARTIAL");
|
||||
}
|
||||
fprintf(stderr, "\n");
|
||||
}
|
||||
}
|
||||
@@ -319,9 +315,8 @@ int main(int argc, char *argv[])
|
||||
/* all data written, send EOF */
|
||||
rc = libssh2_channel_send_eof(channel);
|
||||
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN)
|
||||
fprintf(stderr, "send eof again\n");
|
||||
}
|
||||
else if(rc < 0) {
|
||||
fprintf(stderr, "send eof failed\n");
|
||||
return 1;
|
||||
|
||||
@@ -106,18 +106,14 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
#endif
|
||||
|
||||
if(argc > 1) {
|
||||
if(argc > 1)
|
||||
hostname = argv[1]; /* must be ip address only */
|
||||
}
|
||||
if(argc > 2) {
|
||||
if(argc > 2)
|
||||
username = argv[2];
|
||||
}
|
||||
if(argc > 3) {
|
||||
if(argc > 3)
|
||||
password = argv[3];
|
||||
}
|
||||
if(argc > 4) {
|
||||
if(argc > 4)
|
||||
commandline = argv[4];
|
||||
}
|
||||
|
||||
rc = libssh2_init(0);
|
||||
if(rc) {
|
||||
@@ -245,9 +241,8 @@ int main(int argc, char *argv[])
|
||||
return 1;
|
||||
}
|
||||
while((rc = libssh2_channel_exec(channel, commandline)) ==
|
||||
LIBSSH2_ERROR_EAGAIN) {
|
||||
LIBSSH2_ERROR_EAGAIN)
|
||||
waitsocket(sock, session);
|
||||
}
|
||||
if(rc) {
|
||||
fprintf(stderr, "exec error\n");
|
||||
return 1;
|
||||
@@ -266,18 +261,16 @@ int main(int argc, char *argv[])
|
||||
fputc(buffer[i], stderr);
|
||||
fprintf(stderr, "\n");
|
||||
}
|
||||
else if(nread < 0 && nread != LIBSSH2_ERROR_EAGAIN) {
|
||||
else if(nread < 0 && nread != LIBSSH2_ERROR_EAGAIN)
|
||||
/* no need to output this for the EAGAIN case */
|
||||
fprintf(stderr, "libssh2_channel_read returned %ld\n",
|
||||
(long)nread);
|
||||
}
|
||||
} while(nread > 0);
|
||||
|
||||
/* this is due to blocking that would occur otherwise so we loop on
|
||||
this condition */
|
||||
if(nread == LIBSSH2_ERROR_EAGAIN) {
|
||||
if(nread == LIBSSH2_ERROR_EAGAIN)
|
||||
waitsocket(sock, session);
|
||||
}
|
||||
else
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -212,9 +212,8 @@ int main(int argc, char *argv[])
|
||||
fprintf(stderr, "Authentication by public key failed.\n");
|
||||
goto shutdown;
|
||||
}
|
||||
else {
|
||||
else
|
||||
fprintf(stderr, "Authentication by public key succeeded.\n");
|
||||
}
|
||||
}
|
||||
else {
|
||||
fprintf(stderr, "No supported authentication methods found.\n");
|
||||
|
||||
@@ -186,9 +186,8 @@ int main(int argc, char *argv[])
|
||||
fprintf(stderr, "Authentication by public key failed.\n");
|
||||
goto shutdown;
|
||||
}
|
||||
else {
|
||||
else
|
||||
fprintf(stderr, "Authentication by public key succeeded.\n");
|
||||
}
|
||||
}
|
||||
else {
|
||||
fprintf(stderr, "No supported authentication methods found.\n");
|
||||
|
||||
131
example/x11.c
131
example/x11.c
@@ -108,75 +108,73 @@ static void x11_callback(LIBSSH2_SESSION *session, LIBSSH2_CHANNEL *channel,
|
||||
* Inspired by x11_connect_display in openssh
|
||||
*/
|
||||
display = getenv("DISPLAY");
|
||||
if(display) {
|
||||
if(!strncmp(display, "unix:", 5) || display[0] == ':') {
|
||||
const char *ptr;
|
||||
char *temp_buff;
|
||||
int display_port;
|
||||
int rc;
|
||||
/* Connect to the local unix domain */
|
||||
ptr = strrchr(display, ':');
|
||||
temp_buff = strdup(ptr + 1);
|
||||
if(!temp_buff) {
|
||||
fprintf(stderr, "failed to strdup().\n");
|
||||
return;
|
||||
}
|
||||
display_port = atoi(temp_buff);
|
||||
free(temp_buff);
|
||||
if(display && (!strncmp(display, "unix:", 5) || display[0] == ':')) {
|
||||
const char *ptr;
|
||||
char *temp_buff;
|
||||
int display_port;
|
||||
int rc;
|
||||
/* Connect to the local unix domain */
|
||||
ptr = strrchr(display, ':');
|
||||
temp_buff = strdup(ptr + 1);
|
||||
if(!temp_buff) {
|
||||
fprintf(stderr, "failed to strdup().\n");
|
||||
return;
|
||||
}
|
||||
display_port = atoi(temp_buff);
|
||||
free(temp_buff);
|
||||
|
||||
sock = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
if(sock == LIBSSH2_INVALID_SOCKET)
|
||||
return;
|
||||
memset(&addr, 0, sizeof(addr));
|
||||
addr.sun_family = AF_UNIX;
|
||||
snprintf(addr.sun_path, sizeof(addr.sun_path),
|
||||
PATH_UNIX_X, display_port);
|
||||
rc = connect(sock, (struct sockaddr *)&addr, sizeof(addr));
|
||||
sock = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
if(sock == LIBSSH2_INVALID_SOCKET)
|
||||
return;
|
||||
memset(&addr, 0, sizeof(addr));
|
||||
addr.sun_family = AF_UNIX;
|
||||
snprintf(addr.sun_path, sizeof(addr.sun_path),
|
||||
PATH_UNIX_X, display_port);
|
||||
rc = connect(sock, (struct sockaddr *)&addr, sizeof(addr));
|
||||
|
||||
if(rc != -1) {
|
||||
/* Connection Successful */
|
||||
if(rc != -1) {
|
||||
/* Connection Successful */
|
||||
if(!gp_x11_chan) {
|
||||
/* calloc ensures that gp_x11_chan is zero-initialized. */
|
||||
gp_x11_chan = (struct chan_X11_list *)
|
||||
calloc(1, sizeof(struct chan_X11_list));
|
||||
if(!gp_x11_chan) {
|
||||
/* calloc ensures that gp_x11_chan is zero-initialized. */
|
||||
gp_x11_chan = (struct chan_X11_list *)
|
||||
calloc(1, sizeof(struct chan_X11_list));
|
||||
if(!gp_x11_chan) {
|
||||
fprintf(stderr, "failed to calloc().\n");
|
||||
shutdown(sock, SHUT_RDWR);
|
||||
LIBSSH2_SOCKET_CLOSE(sock);
|
||||
}
|
||||
else {
|
||||
gp_x11_chan->sock = sock;
|
||||
gp_x11_chan->chan = channel;
|
||||
gp_x11_chan->next = NULL;
|
||||
}
|
||||
fprintf(stderr, "failed to calloc().\n");
|
||||
shutdown(sock, SHUT_RDWR);
|
||||
LIBSSH2_SOCKET_CLOSE(sock);
|
||||
}
|
||||
else {
|
||||
struct chan_X11_list *chan_new;
|
||||
struct chan_X11_list *chan_iter;
|
||||
chan_iter = gp_x11_chan;
|
||||
while(chan_iter->next)
|
||||
chan_iter = chan_iter->next;
|
||||
/* Create the new Node */
|
||||
chan_new = (struct chan_X11_list *)
|
||||
calloc(1, sizeof(struct chan_X11_list));
|
||||
if(!chan_new) {
|
||||
fprintf(stderr, "failed to calloc().\n");
|
||||
shutdown(sock, SHUT_RDWR);
|
||||
LIBSSH2_SOCKET_CLOSE(sock);
|
||||
}
|
||||
else {
|
||||
chan_new->sock = sock;
|
||||
chan_new->chan = channel;
|
||||
chan_new->next = NULL;
|
||||
chan_iter->next = chan_new;
|
||||
}
|
||||
gp_x11_chan->sock = sock;
|
||||
gp_x11_chan->chan = channel;
|
||||
gp_x11_chan->next = NULL;
|
||||
}
|
||||
}
|
||||
else {
|
||||
shutdown(sock, SHUT_RDWR);
|
||||
LIBSSH2_SOCKET_CLOSE(sock);
|
||||
struct chan_X11_list *chan_new;
|
||||
struct chan_X11_list *chan_iter;
|
||||
chan_iter = gp_x11_chan;
|
||||
while(chan_iter->next)
|
||||
chan_iter = chan_iter->next;
|
||||
/* Create the new Node */
|
||||
chan_new = (struct chan_X11_list *)
|
||||
calloc(1, sizeof(struct chan_X11_list));
|
||||
if(!chan_new) {
|
||||
fprintf(stderr, "failed to calloc().\n");
|
||||
shutdown(sock, SHUT_RDWR);
|
||||
LIBSSH2_SOCKET_CLOSE(sock);
|
||||
}
|
||||
else {
|
||||
chan_new->sock = sock;
|
||||
chan_new->chan = channel;
|
||||
chan_new->next = NULL;
|
||||
chan_iter->next = chan_new;
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
shutdown(sock, SHUT_RDWR);
|
||||
LIBSSH2_SOCKET_CLOSE(sock);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -240,9 +238,8 @@ static int x11_send_receive(LIBSSH2_CHANNEL *channel, libssh2_socket_t sock)
|
||||
while(wr < nread) {
|
||||
ssize_t nwritten = libssh2_channel_write(channel, buf + wr,
|
||||
(size_t)(nread - wr));
|
||||
if(nwritten == LIBSSH2_ERROR_EAGAIN) {
|
||||
if(nwritten == LIBSSH2_ERROR_EAGAIN)
|
||||
continue;
|
||||
}
|
||||
if(nwritten < 0) {
|
||||
free(fds);
|
||||
free(buf);
|
||||
@@ -261,9 +258,8 @@ static int x11_send_receive(LIBSSH2_CHANNEL *channel, libssh2_socket_t sock)
|
||||
free(fds);
|
||||
free(buf);
|
||||
|
||||
if(libssh2_channel_eof(channel) == 1) {
|
||||
if(libssh2_channel_eof(channel) == 1)
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -467,10 +463,9 @@ int main(int argc, char *argv[])
|
||||
fwrite(buf, 1, (size_t)nread, stdout);
|
||||
fflush(stdout);
|
||||
}
|
||||
else if(nread < 0 && nread != LIBSSH2_ERROR_EAGAIN) {
|
||||
else if(nread < 0 && nread != LIBSSH2_ERROR_EAGAIN)
|
||||
fprintf(stderr, "libssh2_channel_read returned %ld\n",
|
||||
(long)nread);
|
||||
}
|
||||
}
|
||||
|
||||
/* Looping on X clients */
|
||||
@@ -503,9 +498,8 @@ int main(int argc, char *argv[])
|
||||
while(wr < nread) {
|
||||
ssize_t nwritten = libssh2_channel_write(channel, buf + wr,
|
||||
(size_t)(nread - wr));
|
||||
if(nwritten == LIBSSH2_ERROR_EAGAIN) {
|
||||
if(nwritten == LIBSSH2_ERROR_EAGAIN)
|
||||
continue;
|
||||
}
|
||||
if(nwritten < 0) {
|
||||
fprintf(stderr, "libssh2_channel_write returned %ld\n",
|
||||
(long)nwritten);
|
||||
@@ -516,9 +510,8 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
}
|
||||
|
||||
if(libssh2_channel_eof(channel) == 1) {
|
||||
if(libssh2_channel_eof(channel) == 1)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
shutdown:
|
||||
|
||||
23
src/agent.c
23
src/agent.c
@@ -750,10 +750,9 @@ static int agent_sign(LIBSSH2_SESSION *session,
|
||||
|
||||
len = 1 + 4 + 4 + 4; /* fixed parts */
|
||||
if(identity->external.blob_len > UINT32_MAX - len ||
|
||||
data_len > UINT32_MAX - len - identity->external.blob_len) {
|
||||
data_len > UINT32_MAX - len - identity->external.blob_len)
|
||||
return ssh2_err(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY,
|
||||
"Agent sign request too large");
|
||||
}
|
||||
len += identity->external.blob_len + data_len;
|
||||
|
||||
/* Create a request to sign the data */
|
||||
@@ -773,14 +772,11 @@ static int agent_sign(LIBSSH2_SESSION *session,
|
||||
if(session->userauth_pblc_method_len > 0 &&
|
||||
session->userauth_pblc_method) {
|
||||
if(session->userauth_pblc_method_len == 12 &&
|
||||
!memcmp(session->userauth_pblc_method, "rsa-sha2-512", 12)) {
|
||||
!memcmp(session->userauth_pblc_method, "rsa-sha2-512", 12))
|
||||
sign_flags = SSH_AGENT_RSA_SHA2_512;
|
||||
}
|
||||
else if(session->userauth_pblc_method_len == 12 &&
|
||||
!memcmp(session->userauth_pblc_method, "rsa-sha2-256",
|
||||
12)) {
|
||||
!memcmp(session->userauth_pblc_method, "rsa-sha2-256", 12))
|
||||
sign_flags = SSH_AGENT_RSA_SHA2_256;
|
||||
}
|
||||
}
|
||||
ssh2_store_u32(&s, sign_flags);
|
||||
|
||||
@@ -798,9 +794,9 @@ static int agent_sign(LIBSSH2_SESSION *session,
|
||||
return ssh2_err(session, LIBSSH2_ERROR_BAD_USE, "agent not connected");
|
||||
|
||||
rc = agent->ops->transact(agent, transctx);
|
||||
if(rc) {
|
||||
if(rc)
|
||||
goto error;
|
||||
}
|
||||
|
||||
SSH2_FREE(session, transctx->request);
|
||||
transctx->request = NULL;
|
||||
|
||||
@@ -1206,15 +1202,13 @@ int libssh2_agent_sign(LIBSSH2_AGENT *agent,
|
||||
agent->identity = identity->node;
|
||||
}
|
||||
|
||||
if(identity->blob_len < sizeof(uint32_t)) {
|
||||
if(identity->blob_len < sizeof(uint32_t))
|
||||
return LIBSSH2_ERROR_BUFFER_TOO_SMALL;
|
||||
}
|
||||
|
||||
key_kind_len = ssh2_ntohu32(identity->blob);
|
||||
|
||||
if(identity->blob_len < sizeof(uint32_t) + key_kind_len) {
|
||||
if(identity->blob_len < sizeof(uint32_t) + key_kind_len)
|
||||
return LIBSSH2_ERROR_BUFFER_TOO_SMALL;
|
||||
}
|
||||
|
||||
agent->session->userauth_pblc_method = SSH2_ALLOC(agent->session,
|
||||
method_len);
|
||||
@@ -1252,9 +1246,8 @@ int libssh2_agent_disconnect(LIBSSH2_AGENT *agent)
|
||||
void libssh2_agent_free(LIBSSH2_AGENT *agent)
|
||||
{
|
||||
/* Allow connection freeing when the socket has lost its connection */
|
||||
if(agent->fd != LIBSSH2_INVALID_SOCKET) {
|
||||
if(agent->fd != LIBSSH2_INVALID_SOCKET)
|
||||
libssh2_agent_disconnect(agent);
|
||||
}
|
||||
|
||||
if(agent->identity_agent_path)
|
||||
SSH2_FREE(agent->session, agent->identity_agent_path);
|
||||
|
||||
@@ -171,9 +171,8 @@ static int bcrypt_pbkdf(const char *pass, size_t passlen,
|
||||
amt = SSH2_MIN(amt, keylen);
|
||||
for(i = 0; i < amt; i++) {
|
||||
size_t dest = i * stride + (count - 1);
|
||||
if(dest >= origkeylen) {
|
||||
if(dest >= origkeylen)
|
||||
break;
|
||||
}
|
||||
key[dest] = out[i];
|
||||
}
|
||||
keylen -= i;
|
||||
|
||||
@@ -61,9 +61,8 @@ void chacha_keysetup(struct chacha_ctx *x, const u8 *k, u32 kbits)
|
||||
k += 16;
|
||||
constants = sigma;
|
||||
}
|
||||
else { /* kbits == 128 */
|
||||
constants = tau;
|
||||
}
|
||||
else
|
||||
constants = tau; /* kbits == 128 */
|
||||
x->input[8] = U8TO32_LITTLE(k + 0);
|
||||
x->input[9] = U8TO32_LITTLE(k + 4);
|
||||
x->input[10] = U8TO32_LITTLE(k + 8);
|
||||
|
||||
121
src/channel.c
121
src/channel.c
@@ -66,9 +66,8 @@ uint32_t ssh2_channel_nextid(LIBSSH2_SESSION *session)
|
||||
channel = ssh2_list_first(&session->channels);
|
||||
|
||||
while(channel) {
|
||||
if(channel->local.id > id) {
|
||||
if(channel->local.id > id)
|
||||
id = channel->local.id;
|
||||
}
|
||||
channel = ssh2_list_next(&channel->node);
|
||||
}
|
||||
|
||||
@@ -629,9 +628,8 @@ static LIBSSH2_LISTENER *channel_forward_listen(LIBSSH2_SESSION *session,
|
||||
/* append this to the parent's list of listeners */
|
||||
ssh2_list_add(&session->listeners, &listener->node);
|
||||
|
||||
if(bound_port) {
|
||||
if(bound_port)
|
||||
*bound_port = listener->port;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -712,9 +710,8 @@ int ssh2_channel_forward_cancel(LIBSSH2_LISTENER *listener)
|
||||
|
||||
listener->chanFwdCncl_state = ssh2_NB_state_created;
|
||||
}
|
||||
else {
|
||||
else
|
||||
packet = listener->chanFwdCncl_data;
|
||||
}
|
||||
|
||||
if(listener->chanFwdCncl_state == ssh2_NB_state_created) {
|
||||
rc = ssh2_transport_send(session, packet, packet_len, NULL, 0);
|
||||
@@ -743,9 +740,8 @@ int ssh2_channel_forward_cancel(LIBSSH2_LISTENER *listener)
|
||||
LIBSSH2_CHANNEL *next = ssh2_list_next(&queued->node);
|
||||
|
||||
rc = ssh2_channel_free(queued);
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN)
|
||||
return rc;
|
||||
}
|
||||
queued = next;
|
||||
}
|
||||
SSH2_FREE(session, listener->host);
|
||||
@@ -800,10 +796,9 @@ static LIBSSH2_CHANNEL *channel_forward_accept(LIBSSH2_LISTENER *listener)
|
||||
return channel;
|
||||
}
|
||||
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN)
|
||||
ssh2_err(listener->session, LIBSSH2_ERROR_EAGAIN,
|
||||
"Would block waiting for packet");
|
||||
}
|
||||
else
|
||||
ssh2_err(listener->session, LIBSSH2_ERROR_CHANNEL_UNKNOWN,
|
||||
"Channel not found");
|
||||
@@ -854,10 +849,9 @@ static int channel_setenv(LIBSSH2_CHANNEL *channel,
|
||||
|
||||
s = channel->setenv_packet =
|
||||
SSH2_ALLOC(session, channel->setenv_packet_len);
|
||||
if(!channel->setenv_packet) {
|
||||
if(!channel->setenv_packet)
|
||||
return ssh2_err(session, LIBSSH2_ERROR_ALLOC,
|
||||
"Unable to allocate memory for setenv packet");
|
||||
}
|
||||
|
||||
*(s++) = SSH_MSG_CHANNEL_REQUEST;
|
||||
ssh2_store_u32(&s, channel->remote.id);
|
||||
@@ -898,9 +892,8 @@ static int channel_setenv(LIBSSH2_CHANNEL *channel,
|
||||
rc = ssh2_packet_requirev(session, reply_codes, &data, &data_len,
|
||||
1, channel->setenv_local_channel, 4,
|
||||
&channel->setenv_packet_requirev_state);
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN)
|
||||
return rc;
|
||||
}
|
||||
if(rc) {
|
||||
channel->setenv_state = ssh2_NB_state_idle;
|
||||
return ssh2_err(session, rc,
|
||||
@@ -963,10 +956,9 @@ static int channel_request_pty(LIBSSH2_CHANNEL *channel,
|
||||
/* 41 = packet_type(1) + channel(4) + pty_req_len(4) + "pty_req"(7) +
|
||||
* want_reply(1) + term_len(4) + width(4) + height(4) + width_px(4) +
|
||||
* height_px(4) + modes_len(4) */
|
||||
if(term_len + modes_len > 256) {
|
||||
if(term_len + modes_len > 256)
|
||||
return ssh2_err(session, LIBSSH2_ERROR_INVAL,
|
||||
"term + mode lengths too large");
|
||||
}
|
||||
|
||||
channel->reqPTY_packet_len = term_len + modes_len + 41;
|
||||
|
||||
@@ -1019,9 +1011,8 @@ static int channel_request_pty(LIBSSH2_CHANNEL *channel,
|
||||
rc = ssh2_packet_requirev(session, reply_codes, &data, &data_len,
|
||||
1, channel->reqPTY_local_channel, 4,
|
||||
&channel->reqPTY_packet_requirev_state);
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN)
|
||||
return rc;
|
||||
}
|
||||
else if(rc || data_len < 1) {
|
||||
channel->reqPTY_state = ssh2_NB_state_idle;
|
||||
return ssh2_err(session, LIBSSH2_ERROR_PROTO,
|
||||
@@ -1058,10 +1049,9 @@ static int channel_request_auth_agent(LIBSSH2_CHANNEL *channel,
|
||||
/* Only valid options are "auth-agent-req" and
|
||||
* "auth-agent-req_at_openssh.com" so we make sure it is not
|
||||
* actually longer than the longest possible. */
|
||||
if(request_str_len > 26) {
|
||||
if(request_str_len > 26)
|
||||
return ssh2_err(session, LIBSSH2_ERROR_INVAL,
|
||||
"request_str length too large");
|
||||
}
|
||||
|
||||
/*
|
||||
* Length: 24 or 36 = packet_type(1) + channel(4) + req_len(4) +
|
||||
@@ -1117,9 +1107,8 @@ static int channel_request_auth_agent(LIBSSH2_CHANNEL *channel,
|
||||
rc = ssh2_packet_requirev(session, reply_codes, &data, &data_len,
|
||||
1, channel->req_auth_agent_local_channel, 4,
|
||||
&channel->req_auth_agent_requirev_state);
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN)
|
||||
return rc;
|
||||
}
|
||||
else if(rc) {
|
||||
channel->req_auth_agent_state = ssh2_NB_state_idle;
|
||||
return ssh2_err(session, LIBSSH2_ERROR_PROTO,
|
||||
@@ -1319,10 +1308,9 @@ static int channel_x11_req(LIBSSH2_CHANNEL *channel, int single_connection,
|
||||
|
||||
s = channel->reqX11_packet =
|
||||
SSH2_ALLOC(session, channel->reqX11_packet_len);
|
||||
if(!channel->reqX11_packet) {
|
||||
if(!channel->reqX11_packet)
|
||||
return ssh2_err(session, LIBSSH2_ERROR_ALLOC,
|
||||
"Unable to allocate memory for pty-request");
|
||||
}
|
||||
|
||||
*(s++) = SSH_MSG_CHANNEL_REQUEST;
|
||||
ssh2_store_u32(&s, channel->remote.id);
|
||||
@@ -1347,14 +1335,13 @@ static int channel_x11_req(LIBSSH2_CHANNEL *channel, int single_connection,
|
||||
4 bytes of size (for screen_number) */
|
||||
unsigned char buffer[LIBSSH2_X11_RANDOM_COOKIE_LEN / 2];
|
||||
|
||||
if(ssh2_random(buffer, LIBSSH2_X11_RANDOM_COOKIE_LEN / 2)) {
|
||||
if(ssh2_random(buffer, LIBSSH2_X11_RANDOM_COOKIE_LEN / 2))
|
||||
return ssh2_err(session, LIBSSH2_ERROR_RANDGEN,
|
||||
"Unable to get random bytes "
|
||||
"for x11-req cookie");
|
||||
}
|
||||
for(i = 0; i < (LIBSSH2_X11_RANDOM_COOKIE_LEN / 2); i++) {
|
||||
|
||||
for(i = 0; i < (LIBSSH2_X11_RANDOM_COOKIE_LEN / 2); i++)
|
||||
snprintf((char *)&s[i * 2], 3, "%02X", buffer[i]);
|
||||
}
|
||||
}
|
||||
s += cookie_len;
|
||||
|
||||
@@ -1391,9 +1378,8 @@ static int channel_x11_req(LIBSSH2_CHANNEL *channel, int single_connection,
|
||||
rc = ssh2_packet_requirev(session, reply_codes, &data, &data_len,
|
||||
1, channel->reqX11_local_channel, 4,
|
||||
&channel->reqX11_packet_requirev_state);
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN)
|
||||
return rc;
|
||||
}
|
||||
else if(rc || data_len < 1) {
|
||||
channel->reqX11_state = ssh2_NB_state_idle;
|
||||
return ssh2_err(session, rc,
|
||||
@@ -1443,10 +1429,9 @@ int ssh2_channel_process_startup(LIBSSH2_CHANNEL *channel,
|
||||
{ SSH_MSG_CHANNEL_SUCCESS, SSH_MSG_CHANNEL_FAILURE, 0 };
|
||||
int rc;
|
||||
|
||||
if(channel->process_state == ssh2_NB_state_end) {
|
||||
if(channel->process_state == ssh2_NB_state_end)
|
||||
return ssh2_err(session, LIBSSH2_ERROR_BAD_USE,
|
||||
"Channel can not be reused");
|
||||
}
|
||||
|
||||
if(channel->process_state == ssh2_NB_state_idle) {
|
||||
/* 10 = packet_type(1) + channel(4) + request_len(4) + want_reply(1) */
|
||||
@@ -1512,9 +1497,8 @@ int ssh2_channel_process_startup(LIBSSH2_CHANNEL *channel,
|
||||
rc = ssh2_packet_requirev(session, reply_codes, &data, &data_len,
|
||||
1, channel->process_local_channel, 4,
|
||||
&channel->process_packet_requirev_state);
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN)
|
||||
return rc;
|
||||
}
|
||||
else if(rc || data_len < 1) {
|
||||
channel->process_state = ssh2_NB_state_end;
|
||||
return ssh2_err(session, rc, "Failed waiting for channel success");
|
||||
@@ -1594,12 +1578,10 @@ int ssh2_channel_flush(LIBSSH2_CHANNEL *channel, int streamid)
|
||||
/* It is our channel at least */
|
||||
int packet_stream_id;
|
||||
|
||||
if(packet_type == SSH_MSG_CHANNEL_DATA) {
|
||||
if(packet_type == SSH_MSG_CHANNEL_DATA)
|
||||
packet_stream_id = 0;
|
||||
}
|
||||
else if(packet->data_len >= 9) {
|
||||
else if(packet->data_len >= 9)
|
||||
packet_stream_id = ssh2_ntohu32(packet->data + 5);
|
||||
}
|
||||
else {
|
||||
channel->flush_state = ssh2_NB_state_idle;
|
||||
return ssh2_err(channel->session, LIBSSH2_ERROR_PROTO,
|
||||
@@ -1771,9 +1753,8 @@ int ssh2_channel_receive_window_adjust(LIBSSH2_CHANNEL *channel,
|
||||
return 0;
|
||||
}
|
||||
|
||||
if(!adjustment && !channel->adjust_queue) {
|
||||
if(!adjustment && !channel->adjust_queue)
|
||||
return 0;
|
||||
}
|
||||
|
||||
adjustment += channel->adjust_queue;
|
||||
channel->adjust_queue = 0;
|
||||
@@ -1802,13 +1783,11 @@ int ssh2_channel_receive_window_adjust(LIBSSH2_CHANNEL *channel,
|
||||
"packet, deferring");
|
||||
}
|
||||
else {
|
||||
if(adjustment > UINT32_MAX - channel->remote.window_size) {
|
||||
if(adjustment > UINT32_MAX - channel->remote.window_size)
|
||||
return ssh2_err(channel->session, LIBSSH2_ERROR_PROTO,
|
||||
"Window adjust out of bounds");
|
||||
}
|
||||
else {
|
||||
else
|
||||
channel->remote.window_size += adjustment;
|
||||
}
|
||||
}
|
||||
|
||||
channel->adjust_state = ssh2_NB_state_idle;
|
||||
@@ -2001,10 +1980,9 @@ ssize_t ssh2_channel_read(LIBSSH2_CHANNEL *channel, int stream_id,
|
||||
read_packet = read_next;
|
||||
|
||||
if(readpkt->data_len != 1 ||
|
||||
readpkt->data[0] != SSH_MSG_REQUEST_FAILURE) {
|
||||
readpkt->data[0] != SSH_MSG_REQUEST_FAILURE)
|
||||
ssh2_deb((channel->session, LIBSSH2_TRACE_ERROR,
|
||||
"Unexpected packet length"));
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
@@ -2173,9 +2151,8 @@ size_t ssh2_channel_packet_data_len(LIBSSH2_CHANNEL *channel, int stream_id)
|
||||
read_packet->data[0] == SSH_MSG_CHANNEL_EXTENDED_DATA &&
|
||||
channel->local.id == read_local_id &&
|
||||
channel->remote.extended_data_ignore_mode ==
|
||||
LIBSSH2_CHANNEL_EXTENDED_DATA_MERGE)) {
|
||||
LIBSSH2_CHANNEL_EXTENDED_DATA_MERGE))
|
||||
return read_packet->data_len - read_packet->data_head;
|
||||
}
|
||||
|
||||
read_packet = next_packet;
|
||||
}
|
||||
@@ -2230,10 +2207,9 @@ ssize_t ssh2_channel_write(LIBSSH2_CHANNEL *channel, int stream_id,
|
||||
rc = ssh2_transport_read(session);
|
||||
while(rc > 0);
|
||||
|
||||
if(rc < 0 && rc != LIBSSH2_ERROR_EAGAIN) {
|
||||
if(rc < 0 && rc != LIBSSH2_ERROR_EAGAIN)
|
||||
return ssh2_err(channel->session, rc,
|
||||
"Failure while draining incoming flow");
|
||||
}
|
||||
|
||||
if(channel->local.window_size <= 0) {
|
||||
/* there is no room for data so we stop */
|
||||
@@ -2290,9 +2266,8 @@ ssize_t ssh2_channel_write(LIBSSH2_CHANNEL *channel, int stream_id,
|
||||
rc = ssh2_transport_send(session, channel->write_packet,
|
||||
channel->write_packet_len,
|
||||
buf, channel->write_bufwrite);
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN)
|
||||
return ssh2_err(session, rc, "Unable to send channel data");
|
||||
}
|
||||
else if(rc) {
|
||||
channel->write_state = ssh2_NB_state_idle;
|
||||
return ssh2_err(session, rc, "Unable to send channel data");
|
||||
@@ -2354,10 +2329,9 @@ static int channel_send_eof(LIBSSH2_CHANNEL *channel)
|
||||
ssh2_err(session, rc, "Would block sending EOF");
|
||||
return rc;
|
||||
}
|
||||
else if(rc) {
|
||||
else if(rc)
|
||||
return ssh2_err(session, LIBSSH2_ERROR_SOCKET_SEND,
|
||||
"Unable to send EOF on channel");
|
||||
}
|
||||
channel->local.eof = 1;
|
||||
|
||||
return 0;
|
||||
@@ -2437,9 +2411,8 @@ static int channel_wait_eof(LIBSSH2_CHANNEL *channel)
|
||||
* Either the EOF is set or network timeout occurs.
|
||||
*/
|
||||
do {
|
||||
if(channel->remote.eof) {
|
||||
if(channel->remote.eof)
|
||||
break;
|
||||
}
|
||||
|
||||
if(channel->remote.window_size == channel->read_avail &&
|
||||
session->api_block_mode)
|
||||
@@ -2447,9 +2420,8 @@ static int channel_wait_eof(LIBSSH2_CHANNEL *channel)
|
||||
"Receiving channel window has been exhausted");
|
||||
|
||||
rc = ssh2_transport_read(session);
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN)
|
||||
return rc;
|
||||
}
|
||||
else if(rc < 0) {
|
||||
channel->wait_eof_state = ssh2_NB_state_idle;
|
||||
return ssh2_err(session, rc, "ssh2_transport_read() bailed out");
|
||||
@@ -2490,9 +2462,8 @@ int ssh2_channel_close(LIBSSH2_CHANNEL *channel)
|
||||
if(!channel->local.eof) {
|
||||
rc = channel_send_eof(channel);
|
||||
if(rc) {
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN)
|
||||
return rc;
|
||||
}
|
||||
ssh2_err(session, rc,
|
||||
"Unable to send EOF, but closing channel anyway");
|
||||
}
|
||||
@@ -2542,9 +2513,8 @@ int ssh2_channel_close(LIBSSH2_CHANNEL *channel)
|
||||
|
||||
/* We call the callback last in this function to make it keep the local
|
||||
data as long as EAGAIN is returned. */
|
||||
if(channel->close_cb) {
|
||||
if(channel->close_cb)
|
||||
SSH2_CHANNEL_CLOSE(session, channel);
|
||||
}
|
||||
|
||||
channel->close_state = ssh2_NB_state_idle;
|
||||
}
|
||||
@@ -2575,11 +2545,10 @@ static int channel_wait_closed(LIBSSH2_CHANNEL *channel)
|
||||
LIBSSH2_SESSION *session = channel->session;
|
||||
int rc;
|
||||
|
||||
if(!channel->remote.eof) {
|
||||
if(!channel->remote.eof)
|
||||
return ssh2_err(session, LIBSSH2_ERROR_INVAL,
|
||||
"libssh2_channel_wait_closed() invoked when "
|
||||
"channel is not in EOF state");
|
||||
}
|
||||
|
||||
if(channel->wait_closed_state == ssh2_NB_state_idle) {
|
||||
ssh2_deb((session, LIBSSH2_TRACE_CONN,
|
||||
@@ -2661,9 +2630,8 @@ int ssh2_channel_free(LIBSSH2_CHANNEL *channel)
|
||||
|
||||
channel->free_state = ssh2_NB_state_idle;
|
||||
|
||||
if(channel->exit_signal) {
|
||||
if(channel->exit_signal)
|
||||
SSH2_FREE(session, channel->exit_signal);
|
||||
}
|
||||
|
||||
/*
|
||||
* channel->remote.close *might* not be set yet, Well...
|
||||
@@ -2676,14 +2644,12 @@ int ssh2_channel_free(LIBSSH2_CHANNEL *channel)
|
||||
while(ssh2_packet_ask(session, SSH_MSG_CHANNEL_DATA, &data,
|
||||
&data_len, 1, channel_id, 4) >= 0 ||
|
||||
ssh2_packet_ask(session, SSH_MSG_CHANNEL_EXTENDED_DATA, &data,
|
||||
&data_len, 1, channel_id, 4) >= 0) {
|
||||
&data_len, 1, channel_id, 4) >= 0)
|
||||
SSH2_FREE(session, data);
|
||||
}
|
||||
|
||||
/* free "channel_type" */
|
||||
if(channel->channel_type) {
|
||||
if(channel->channel_type)
|
||||
SSH2_FREE(session, channel->channel_type);
|
||||
}
|
||||
|
||||
/* Unlink from channel list */
|
||||
ssh2_list_remove(&channel->node);
|
||||
@@ -2691,15 +2657,12 @@ int ssh2_channel_free(LIBSSH2_CHANNEL *channel)
|
||||
/*
|
||||
* Make sure all memory used in the state variables are free
|
||||
*/
|
||||
if(channel->setenv_packet) {
|
||||
if(channel->setenv_packet)
|
||||
SSH2_FREE(session, channel->setenv_packet);
|
||||
}
|
||||
if(channel->reqX11_packet) {
|
||||
if(channel->reqX11_packet)
|
||||
SSH2_FREE(session, channel->reqX11_packet);
|
||||
}
|
||||
if(channel->process_packet) {
|
||||
if(channel->process_packet)
|
||||
SSH2_FREE(session, channel->process_packet);
|
||||
}
|
||||
|
||||
/* Free some entries in session-structure, if channel found */
|
||||
if(session->open_channel == channel) {
|
||||
@@ -2780,9 +2743,8 @@ unsigned long libssh2_channel_window_read_ex(
|
||||
if(!channel)
|
||||
return 0; /* no channel, no window! */
|
||||
|
||||
if(window_size_initial) {
|
||||
if(window_size_initial)
|
||||
*window_size_initial = channel->remote.window_size_initial;
|
||||
}
|
||||
|
||||
if(read_avail) {
|
||||
size_t bytes_queued = 0;
|
||||
@@ -2830,11 +2792,10 @@ unsigned long libssh2_channel_window_write_ex(
|
||||
if(!channel)
|
||||
return 0; /* no channel, no window! */
|
||||
|
||||
if(window_size_initial) {
|
||||
if(window_size_initial)
|
||||
/* For locally initiated channels this is often 0, so it is not
|
||||
* *that* useful as information goes */
|
||||
*window_size_initial = channel->local.window_size_initial;
|
||||
}
|
||||
|
||||
return channel->local.window_size;
|
||||
}
|
||||
|
||||
11
src/comp.c
11
src/comp.c
@@ -140,23 +140,18 @@ static int comp_method_zlib_init(LIBSSH2_SESSION *session, int compr,
|
||||
int status;
|
||||
|
||||
strm = SSH2_CALLOC(session, sizeof(z_stream));
|
||||
if(!strm) {
|
||||
if(!strm)
|
||||
return ssh2_err(session, LIBSSH2_ERROR_ALLOC,
|
||||
"Unable to allocate memory for "
|
||||
"zlib compression/decompression");
|
||||
}
|
||||
|
||||
strm->opaque = (voidpf)session;
|
||||
strm->zalloc = (alloc_func)comp_method_zlib_alloc;
|
||||
strm->zfree = (free_func)comp_method_zlib_free;
|
||||
if(compr) {
|
||||
/* deflate */
|
||||
if(compr)
|
||||
status = deflateInit(strm, Z_DEFAULT_COMPRESSION);
|
||||
}
|
||||
else {
|
||||
/* inflate */
|
||||
else
|
||||
status = inflateInit(strm);
|
||||
}
|
||||
|
||||
if(status != Z_OK) {
|
||||
SSH2_FREE(session, strm);
|
||||
|
||||
@@ -445,9 +445,8 @@ static int crypt_encrypt_chacha20_poly_buffer(LIBSSH2_SESSION *session,
|
||||
|
||||
/* the api expects the size field to already be removed
|
||||
from the decrypted packet so we help it out */
|
||||
if(ret == 0) {
|
||||
if(ret == 0)
|
||||
memmove(buf, buf + 4, buf_len - 4);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -45,9 +45,8 @@ static int s_init_flags = 0;
|
||||
|
||||
int libssh2_init(int flags)
|
||||
{
|
||||
if(s_initialized == 0 && !(flags & LIBSSH2_INIT_NO_CRYPTO)) {
|
||||
if(s_initialized == 0 && !(flags & LIBSSH2_INIT_NO_CRYPTO))
|
||||
ssh2_crypto_init();
|
||||
}
|
||||
|
||||
s_initialized++;
|
||||
s_init_flags |= flags;
|
||||
@@ -62,10 +61,8 @@ void libssh2_exit(void)
|
||||
|
||||
s_initialized--;
|
||||
|
||||
if(s_initialized == 0 &&
|
||||
!(s_init_flags & LIBSSH2_INIT_NO_CRYPTO)) {
|
||||
if(s_initialized == 0 && !(s_init_flags & LIBSSH2_INIT_NO_CRYPTO))
|
||||
ssh2_crypto_exit();
|
||||
}
|
||||
}
|
||||
|
||||
void ssh2_init_if_needed(void)
|
||||
|
||||
120
src/hostkey.c
120
src/hostkey.c
@@ -80,9 +80,8 @@ static int hostkey_method_ssh_rsa_init(LIBSSH2_SESSION *session,
|
||||
buf.dataptr = buf.data;
|
||||
buf.len = hostkey_data_len;
|
||||
|
||||
if(ssh2_get_string(&buf, &type, &type_len)) {
|
||||
if(ssh2_get_string(&buf, &type, &type_len))
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* we accept one of 3 header types */
|
||||
#if LIBSSH2_RSA_SHA1
|
||||
@@ -119,9 +118,8 @@ static int hostkey_method_ssh_rsa_init(LIBSSH2_SESSION *session,
|
||||
e, (unsigned long)e_len,
|
||||
n, (unsigned long)n_len,
|
||||
NULL, 0, NULL, 0, NULL, 0,
|
||||
NULL, 0, NULL, 0, NULL, 0)) {
|
||||
NULL, 0, NULL, 0, NULL, 0))
|
||||
return -1;
|
||||
}
|
||||
|
||||
*abstract = rsactx;
|
||||
|
||||
@@ -145,9 +143,8 @@ static int hostkey_method_ssh_rsa_initPEM(LIBSSH2_SESSION *session,
|
||||
}
|
||||
|
||||
ret = ssh2_rsa_new_private(&rsactx, session, privkeyfile, passphrase);
|
||||
if(ret) {
|
||||
if(ret)
|
||||
return -1;
|
||||
}
|
||||
|
||||
*abstract = rsactx;
|
||||
|
||||
@@ -175,9 +172,8 @@ static int hostkey_method_ssh_rsa_initPEMFromMemory(
|
||||
ret = ssh2_rsa_new_private_frommemory(&rsactx, session,
|
||||
privkeyfiledata,
|
||||
privkeyfiledata_len, passphrase);
|
||||
if(ret) {
|
||||
if(ret)
|
||||
return -1;
|
||||
}
|
||||
|
||||
*abstract = rsactx;
|
||||
|
||||
@@ -227,23 +223,19 @@ static int hostkey_method_ssh_rsa_signv(LIBSSH2_SESSION *session,
|
||||
unsigned char hash[SHA_DIGEST_LENGTH];
|
||||
ssh2_sha1_ctx ctx;
|
||||
|
||||
if(!ssh2_sha1_init(&ctx)) {
|
||||
if(!ssh2_sha1_init(&ctx))
|
||||
return -1;
|
||||
}
|
||||
for(i = 0; i < veccount; i++) {
|
||||
if(!ssh2_sha1_update(ctx, datavec[i].iov_base, datavec[i].iov_len)) {
|
||||
if(!ssh2_sha1_update(ctx, datavec[i].iov_base, datavec[i].iov_len))
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
if(!ssh2_sha1_final(ctx, hash)) {
|
||||
if(!ssh2_sha1_final(ctx, hash))
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = ssh2_rsa_sha1_sign(session, rsactx, hash, SHA_DIGEST_LENGTH,
|
||||
signature, signature_len);
|
||||
if(ret) {
|
||||
if(ret)
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
#endif
|
||||
@@ -297,23 +289,19 @@ static int hostkey_method_ssh_rsa_sha2_256_signv(LIBSSH2_SESSION *session,
|
||||
unsigned char hash[SHA256_DIGEST_LENGTH];
|
||||
ssh2_sha256_ctx ctx;
|
||||
|
||||
if(!ssh2_sha256_init(&ctx)) {
|
||||
if(!ssh2_sha256_init(&ctx))
|
||||
return -1;
|
||||
}
|
||||
for(i = 0; i < veccount; i++) {
|
||||
if(!ssh2_sha256_update(ctx, datavec[i].iov_base, datavec[i].iov_len)) {
|
||||
if(!ssh2_sha256_update(ctx, datavec[i].iov_base, datavec[i].iov_len))
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
if(!ssh2_sha256_final(ctx, hash)) {
|
||||
if(!ssh2_sha256_final(ctx, hash))
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = ssh2_rsa_sha2_sign(session, rsactx, hash, SHA256_DIGEST_LENGTH,
|
||||
signature, signature_len);
|
||||
if(ret) {
|
||||
if(ret)
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
#endif
|
||||
@@ -364,23 +352,19 @@ static int hostkey_method_ssh_rsa_sha2_512_signv(LIBSSH2_SESSION *session,
|
||||
unsigned char hash[SHA512_DIGEST_LENGTH];
|
||||
ssh2_sha512_ctx ctx;
|
||||
|
||||
if(!ssh2_sha512_init(&ctx)) {
|
||||
if(!ssh2_sha512_init(&ctx))
|
||||
return -1;
|
||||
}
|
||||
for(i = 0; i < veccount; i++) {
|
||||
if(!ssh2_sha512_update(ctx, datavec[i].iov_base, datavec[i].iov_len)) {
|
||||
if(!ssh2_sha512_update(ctx, datavec[i].iov_base, datavec[i].iov_len))
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
if(!ssh2_sha512_final(ctx, hash)) {
|
||||
if(!ssh2_sha512_final(ctx, hash))
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = ssh2_rsa_sha2_sign(session, rsactx, hash, SHA512_DIGEST_LENGTH,
|
||||
signature, signature_len);
|
||||
if(ret) {
|
||||
if(ret)
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
#endif
|
||||
@@ -552,9 +536,8 @@ static int hostkey_method_ssh_dss_init(LIBSSH2_SESSION *session,
|
||||
q, (unsigned long)q_len,
|
||||
g, (unsigned long)g_len,
|
||||
y, (unsigned long)y_len,
|
||||
NULL, 0)) {
|
||||
NULL, 0))
|
||||
return -1;
|
||||
}
|
||||
|
||||
*abstract = dsactx;
|
||||
|
||||
@@ -578,9 +561,8 @@ static int hostkey_method_ssh_dss_initPEM(LIBSSH2_SESSION *session,
|
||||
}
|
||||
|
||||
ret = ssh2_dsa_new_private(&dsactx, session, privkeyfile, passphrase);
|
||||
if(ret) {
|
||||
if(ret)
|
||||
return -1;
|
||||
}
|
||||
|
||||
*abstract = dsactx;
|
||||
|
||||
@@ -608,9 +590,8 @@ static int hostkey_method_ssh_dss_initPEMFromMemory(
|
||||
ret = ssh2_dsa_new_private_frommemory(&dsactx, session,
|
||||
privkeyfiledata,
|
||||
privkeyfiledata_len, passphrase);
|
||||
if(ret) {
|
||||
if(ret)
|
||||
return -1;
|
||||
}
|
||||
|
||||
*abstract = dsactx;
|
||||
|
||||
@@ -629,10 +610,9 @@ static int hostkey_method_ssh_dss_sig_verify(LIBSSH2_SESSION *session,
|
||||
ssh2_dsa_ctx *dsactx = (ssh2_dsa_ctx *)(*abstract);
|
||||
|
||||
/* Skip past keyname_len(4) + keyname(7){"ssh-dss"} + signature_len(4) */
|
||||
if(sig_len != 55) {
|
||||
if(sig_len != 55)
|
||||
return ssh2_err(session, LIBSSH2_ERROR_PROTO,
|
||||
"Invalid DSS signature length");
|
||||
}
|
||||
|
||||
sig += 15;
|
||||
sig_len -= 15;
|
||||
@@ -662,20 +642,17 @@ static int hostkey_method_ssh_dss_signv(LIBSSH2_SESSION *session,
|
||||
}
|
||||
|
||||
*signature = SSH2_CALLOC(session, 2 * SHA_DIGEST_LENGTH);
|
||||
if(!*signature) {
|
||||
if(!*signature)
|
||||
return -1;
|
||||
}
|
||||
|
||||
*signature_len = 2 * SHA_DIGEST_LENGTH;
|
||||
|
||||
for(i = 0; i < veccount; i++) {
|
||||
if(!ssh2_sha1_update(ctx, datavec[i].iov_base, datavec[i].iov_len)) {
|
||||
if(!ssh2_sha1_update(ctx, datavec[i].iov_base, datavec[i].iov_len))
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
if(!ssh2_sha1_final(ctx, hash)) {
|
||||
if(!ssh2_sha1_final(ctx, hash))
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(ssh2_dsa_sha1_sign(dsactx, hash, SHA_DIGEST_LENGTH, *signature)) {
|
||||
SSH2_FREE(session, *signature);
|
||||
@@ -754,34 +731,27 @@ static int hostkey_method_ssh_ecdsa_init(LIBSSH2_SESSION *session,
|
||||
if(ssh2_get_string(&buf, &type_str, &len) || len != 19)
|
||||
return -1;
|
||||
|
||||
if(!strncmp((char *)type_str, "ecdsa-sha2-nistp256", 19)) {
|
||||
if(!strncmp((char *)type_str, "ecdsa-sha2-nistp256", 19))
|
||||
type = SSH2_EC_CURVE_NISTP256;
|
||||
}
|
||||
else if(!strncmp((char *)type_str, "ecdsa-sha2-nistp384", 19)) {
|
||||
else if(!strncmp((char *)type_str, "ecdsa-sha2-nistp384", 19))
|
||||
type = SSH2_EC_CURVE_NISTP384;
|
||||
}
|
||||
else if(!strncmp((char *)type_str, "ecdsa-sha2-nistp521", 19)) {
|
||||
else if(!strncmp((char *)type_str, "ecdsa-sha2-nistp521", 19))
|
||||
type = SSH2_EC_CURVE_NISTP521;
|
||||
}
|
||||
else {
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(ssh2_get_string(&buf, &domain, &len) || len != 8)
|
||||
return -1;
|
||||
|
||||
if(type == SSH2_EC_CURVE_NISTP256 &&
|
||||
strncmp((char *)domain, "nistp256", 8)) {
|
||||
strncmp((char *)domain, "nistp256", 8))
|
||||
return -1;
|
||||
}
|
||||
else if(type == SSH2_EC_CURVE_NISTP384 &&
|
||||
strncmp((char *)domain, "nistp384", 8)) {
|
||||
strncmp((char *)domain, "nistp384", 8))
|
||||
return -1;
|
||||
}
|
||||
else if(type == SSH2_EC_CURVE_NISTP521 &&
|
||||
strncmp((char *)domain, "nistp521", 8)) {
|
||||
strncmp((char *)domain, "nistp521", 8))
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* public key */
|
||||
if(ssh2_get_string(&buf, &public_key, &key_len))
|
||||
@@ -846,9 +816,8 @@ static int hostkey_method_ssh_ecdsa_initPEMFromMemory(
|
||||
privkeyfiledata,
|
||||
privkeyfiledata_len,
|
||||
passphrase);
|
||||
if(ret) {
|
||||
if(ret)
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(abstract)
|
||||
*abstract = ec_ctx;
|
||||
@@ -940,18 +909,14 @@ static int hostkey_method_ssh_ecdsa_signv(LIBSSH2_SESSION *session,
|
||||
ssh2_curve_type type = ssh2_ecdsa_get_curve_type(ec_ctx);
|
||||
int ret = 0;
|
||||
|
||||
if(type == SSH2_EC_CURVE_NISTP256) {
|
||||
if(type == SSH2_EC_CURVE_NISTP256)
|
||||
HOSTKEY_METHOD_EC_SIGNV_HASH(256);
|
||||
}
|
||||
else if(type == SSH2_EC_CURVE_NISTP384) {
|
||||
else if(type == SSH2_EC_CURVE_NISTP384)
|
||||
HOSTKEY_METHOD_EC_SIGNV_HASH(384);
|
||||
}
|
||||
else if(type == SSH2_EC_CURVE_NISTP521) {
|
||||
else if(type == SSH2_EC_CURVE_NISTP521)
|
||||
HOSTKEY_METHOD_EC_SIGNV_HASH(512);
|
||||
}
|
||||
else {
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
@@ -1093,9 +1058,8 @@ static int hostkey_method_ssh_ed25519_init(LIBSSH2_SESSION *session,
|
||||
if(!ssh2_eob(&buf))
|
||||
return -1;
|
||||
|
||||
if(ssh2_ed25519_new_public(&ctx, session, key, key_len) != 0) {
|
||||
if(ssh2_ed25519_new_public(&ctx, session, key, key_len) != 0)
|
||||
return -1;
|
||||
}
|
||||
|
||||
*abstract = ctx;
|
||||
|
||||
@@ -1148,9 +1112,8 @@ static int hostkey_method_ssh_ed25519_init_cert(
|
||||
* have more meta data we do not read
|
||||
*/
|
||||
|
||||
if(ssh2_ed25519_new_public(&ctx, session, key, key_len) != 0) {
|
||||
if(ssh2_ed25519_new_public(&ctx, session, key, key_len) != 0)
|
||||
return -1;
|
||||
}
|
||||
|
||||
*abstract = ctx;
|
||||
|
||||
@@ -1174,9 +1137,8 @@ static int hostkey_method_ssh_ed25519_initPEM(LIBSSH2_SESSION *session,
|
||||
}
|
||||
|
||||
ret = ssh2_ed25519_new_private(&ec_ctx, session, privkeyfile, passphrase);
|
||||
if(ret) {
|
||||
if(ret)
|
||||
return -1;
|
||||
}
|
||||
|
||||
*abstract = ec_ctx;
|
||||
|
||||
@@ -1205,9 +1167,8 @@ static int hostkey_method_ssh_ed25519_initPEMFromMemory(
|
||||
privkeyfiledata,
|
||||
privkeyfiledata_len,
|
||||
passphrase);
|
||||
if(ret) {
|
||||
if(ret)
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(abstract)
|
||||
*abstract = ed_ctx;
|
||||
@@ -1253,9 +1214,8 @@ static int hostkey_method_ssh_ed25519_signv(LIBSSH2_SESSION *session,
|
||||
{
|
||||
ssh2_ed25519_ctx *ctx = (ssh2_ed25519_ctx *)(*abstract);
|
||||
|
||||
if(veccount != 1) {
|
||||
if(veccount != 1)
|
||||
return -1;
|
||||
}
|
||||
|
||||
return ssh2_ed25519_sign(ctx, session, signature, signature_len,
|
||||
(const uint8_t *)datavec[0].iov_base,
|
||||
|
||||
@@ -88,10 +88,9 @@ int libssh2_keepalive_send(LIBSSH2_SESSION *session, int *seconds_to_next)
|
||||
if(seconds_to_next)
|
||||
*seconds_to_next = session->keepalive_interval;
|
||||
}
|
||||
else if(seconds_to_next) {
|
||||
else if(seconds_to_next)
|
||||
*seconds_to_next = (int)(session->keepalive_last_sent - now) +
|
||||
session->keepalive_interval;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -255,9 +255,8 @@ static int knownhost_add(LIBSSH2_KNOWNHOSTS *hosts,
|
||||
entry->comment[commentlen] = 0; /* force a null-terminator */
|
||||
entry->comment_len = commentlen;
|
||||
}
|
||||
else {
|
||||
else
|
||||
entry->comment = NULL;
|
||||
}
|
||||
|
||||
/* add this new host to the big list of known hosts */
|
||||
ssh2_list_add(&hosts->head, &entry->node);
|
||||
@@ -427,11 +426,10 @@ static int knownhost_check(LIBSSH2_KNOWNHOSTS *hosts,
|
||||
if(!ssh2_hmac_ctx_init(&ctx))
|
||||
break;
|
||||
|
||||
if(SHA_DIGEST_LENGTH != node->name_len) {
|
||||
if(SHA_DIGEST_LENGTH != node->name_len)
|
||||
/* the name hash length must be the sha1 size or
|
||||
we cannot match it */
|
||||
break;
|
||||
}
|
||||
if(!ssh2_hmac_sha1_init(&ctx, node->salt, node->salt_len))
|
||||
break;
|
||||
if(!ssh2_hmac_update(&ctx, host, strlen(host)) ||
|
||||
@@ -812,22 +810,17 @@ static int hostline(LIBSSH2_KNOWNHOSTS *hosts,
|
||||
}
|
||||
|
||||
/* Figure out host format */
|
||||
if(hostlen < 3 || memcmp(host, "|1|", 3)) {
|
||||
if(hostlen < 3 || memcmp(host, "|1|", 3))
|
||||
/* old style plain text: [name]([,][name])*
|
||||
|
||||
for the sake of simplicity, we add them as separate hosts with the
|
||||
same key
|
||||
*/
|
||||
for simplicity, we add them as separate hosts with the same key */
|
||||
return oldstyle_hostline(hosts, host, hostlen, key_type_name,
|
||||
key_type_len, key, keylen, key_type,
|
||||
comment, commentlen);
|
||||
}
|
||||
else {
|
||||
else
|
||||
/* |1|[salt]|[hash] */
|
||||
return hashed_hostline(hosts, host, hostlen, key_type_name,
|
||||
key_type_len, key, keylen, key_type,
|
||||
comment, commentlen);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -153,16 +153,15 @@ int ssh2_rsa_new(ssh2_rsa_ctx **rsa,
|
||||
(void)e2data;
|
||||
(void)e2len;
|
||||
|
||||
if(ddata) {
|
||||
if(ddata)
|
||||
rc = gcry_sexp_build(rsa, NULL,
|
||||
"(private-key(rsa(n%b)(e%b)(d%b)(q%b)(p%b)(u%b)))",
|
||||
nlen, ndata, elen, edata, dlen, ddata, plen, pdata,
|
||||
qlen, qdata, coefflen, coeffdata);
|
||||
}
|
||||
else {
|
||||
else
|
||||
rc = gcry_sexp_build(rsa, NULL, "(public-key(rsa(n%b)(e%b)))",
|
||||
nlen, ndata, elen, edata);
|
||||
}
|
||||
|
||||
if(rc) {
|
||||
*rsa = NULL;
|
||||
return -1;
|
||||
@@ -199,9 +198,8 @@ int ssh2_rsa_sha2_verify(ssh2_rsa_ctx *rsactx,
|
||||
algo = "sha512";
|
||||
ret = ssh2_sha512(m, m_len, hash);
|
||||
}
|
||||
else {
|
||||
else
|
||||
ret = 1;
|
||||
}
|
||||
|
||||
if(ret) {
|
||||
ret = -1;
|
||||
@@ -255,16 +253,14 @@ int ssh2_dsa_new(ssh2_dsa_ctx **dsactx,
|
||||
{
|
||||
int rc;
|
||||
|
||||
if(x_len) {
|
||||
if(x_len)
|
||||
rc = gcry_sexp_build(dsactx, NULL,
|
||||
"(private-key(dsa(p%b)(q%b)(g%b)(y%b)(x%b)))",
|
||||
p_len, p, q_len, q, g_len, g, y_len, y, x_len, x);
|
||||
}
|
||||
else {
|
||||
else
|
||||
rc = gcry_sexp_build(dsactx, NULL,
|
||||
"(public-key(dsa(p%b)(q%b)(g%b)(y%b)))",
|
||||
p_len, p, q_len, q, g_len, g, y_len, y);
|
||||
}
|
||||
|
||||
if(rc) {
|
||||
*dsactx = NULL;
|
||||
@@ -305,16 +301,14 @@ int ssh2_rsa_new_private(ssh2_rsa_ctx **rsa,
|
||||
unsigned int nlen, elen, dlen, plen, qlen, e1len, e2len, coefflen;
|
||||
|
||||
fp = fopen(filename, "rb");
|
||||
if(!fp) {
|
||||
if(!fp)
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = ssh2_pem_parse(session, PEM_RSA_HEADER, PEM_RSA_FOOTER,
|
||||
passphrase, fp, &data, &datalen);
|
||||
fclose(fp);
|
||||
if(ret) {
|
||||
if(ret)
|
||||
return -1;
|
||||
}
|
||||
|
||||
save_data = data;
|
||||
|
||||
@@ -422,16 +416,14 @@ int ssh2_dsa_new_private(ssh2_dsa_ctx **dsa,
|
||||
unsigned int plen, qlen, glen, ylen, xlen;
|
||||
|
||||
fp = fopen(filename, "rb");
|
||||
if(!fp) {
|
||||
if(!fp)
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = ssh2_pem_parse(session, PEM_DSA_HEADER, PEM_DSA_FOOTER,
|
||||
passphrase, fp, &data, &datalen);
|
||||
fclose(fp);
|
||||
if(ret) {
|
||||
if(ret)
|
||||
return -1;
|
||||
}
|
||||
|
||||
save_data = data;
|
||||
|
||||
@@ -591,25 +583,22 @@ int ssh2_dsa_sha1_sign(ssh2_dsa_ctx *dsactx,
|
||||
const char *tmp;
|
||||
size_t size;
|
||||
|
||||
if(hash_len != SHA_DIGEST_LENGTH) {
|
||||
if(hash_len != SHA_DIGEST_LENGTH)
|
||||
return -1;
|
||||
}
|
||||
|
||||
memcpy(zhash + 1, hash, hash_len);
|
||||
zhash[0] = 0;
|
||||
|
||||
if(gcry_sexp_build(&data, NULL, "(data (value %b))",
|
||||
(int)(hash_len + 1), zhash)) {
|
||||
(int)(hash_len + 1), zhash))
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = gcry_pk_sign(&sig_sexp, data, dsactx);
|
||||
|
||||
gcry_sexp_release(data);
|
||||
|
||||
if(ret) {
|
||||
if(ret)
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(sig, 0, 40);
|
||||
|
||||
@@ -660,12 +649,10 @@ err:
|
||||
ret = -1;
|
||||
|
||||
out:
|
||||
if(sig_sexp) {
|
||||
if(sig_sexp)
|
||||
gcry_sexp_release(sig_sexp);
|
||||
}
|
||||
if(data) {
|
||||
if(data)
|
||||
gcry_sexp_release(data);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -677,15 +664,14 @@ int ssh2_dsa_sha1_verify(ssh2_dsa_ctx *dsactx,
|
||||
gcry_sexp_t s_sig, s_hash;
|
||||
int rc = -1;
|
||||
|
||||
if(ssh2_sha1(m, m_len, hash + 1)) {
|
||||
if(ssh2_sha1(m, m_len, hash + 1))
|
||||
return -1;
|
||||
}
|
||||
|
||||
hash[0] = 0;
|
||||
|
||||
if(gcry_sexp_build(&s_hash, NULL, "(data(flags raw)(value %b))",
|
||||
SHA_DIGEST_LENGTH + 1, hash)) {
|
||||
SHA_DIGEST_LENGTH + 1, hash))
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(gcry_sexp_build(&s_sig, NULL, "(sig-val(dsa(r %b)(s %b)))",
|
||||
20, sig, 20, sig + 20)) {
|
||||
@@ -712,9 +698,8 @@ int ssh2_cipher_init(ssh2_cipher_ctx *h, SSH2_CIPHER_T(algo),
|
||||
(void)encrypt;
|
||||
|
||||
ret = gcry_cipher_open(h, cipher, mode, 0);
|
||||
if(ret) {
|
||||
if(ret)
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = gcry_cipher_setkey(*h, secret, keylen);
|
||||
if(ret) {
|
||||
@@ -747,12 +732,10 @@ int ssh2_cipher_crypt(ssh2_cipher_ctx *ctx,
|
||||
(void)algo;
|
||||
(void)firstlast;
|
||||
|
||||
if(encrypt) {
|
||||
if(encrypt)
|
||||
ret = gcry_cipher_encrypt(*ctx, block, blocksize, block, blocksize);
|
||||
}
|
||||
else {
|
||||
else
|
||||
ret = gcry_cipher_decrypt(*ctx, block, blocksize, block, blocksize);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
@@ -140,7 +140,7 @@
|
||||
#define ssh2_hmac_ctx gcry_md_hd_t
|
||||
|
||||
#define ssh2_crypto_init() gcry_control(GCRYCTL_DISABLE_SECMEM)
|
||||
#define ssh2_crypto_exit()
|
||||
#define ssh2_crypto_exit() do {} while(0)
|
||||
|
||||
#define ssh2_rsa_ctx struct gcry_sexp
|
||||
#define ssh2_rsa_free(rsactx) gcry_sexp_release(rsactx)
|
||||
|
||||
@@ -92,9 +92,8 @@ static int mac_method_common_init(LIBSSH2_SESSION *session, unsigned char *key,
|
||||
*/
|
||||
static int mac_method_common_dtor(LIBSSH2_SESSION *session, void **abstract)
|
||||
{
|
||||
if(*abstract) {
|
||||
if(*abstract)
|
||||
SSH2_FREE(session, *abstract);
|
||||
}
|
||||
*abstract = NULL;
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -291,9 +291,8 @@ ssh2_bn *ssh2_mbed_bn_init(void)
|
||||
ssh2_bn *bignum;
|
||||
|
||||
bignum = (ssh2_bn *)mbedtls_calloc(1, sizeof(ssh2_bn));
|
||||
if(bignum) {
|
||||
if(bignum)
|
||||
mbedtls_mpi_init(bignum);
|
||||
}
|
||||
|
||||
return bignum;
|
||||
}
|
||||
@@ -379,14 +378,12 @@ int ssh2_rsa_new(ssh2_rsa_ctx **rsa,
|
||||
|
||||
ret = 0;
|
||||
if(mbedtls_mpi_read_binary(&(ctx->MBEDTLS_PRIVATE(E)), edata, elen) ||
|
||||
mbedtls_mpi_read_binary(&(ctx->MBEDTLS_PRIVATE(N)), ndata, nlen)) {
|
||||
mbedtls_mpi_read_binary(&(ctx->MBEDTLS_PRIVATE(N)), ndata, nlen))
|
||||
ret = -1;
|
||||
}
|
||||
|
||||
if(!ret) {
|
||||
if(!ret)
|
||||
ctx->MBEDTLS_PRIVATE(len) =
|
||||
mbedtls_mpi_size(&(ctx->MBEDTLS_PRIVATE(N)));
|
||||
}
|
||||
|
||||
if(!ret && ddata) {
|
||||
if(mbedtls_mpi_read_binary(&(ctx->MBEDTLS_PRIVATE(D)),
|
||||
@@ -403,13 +400,11 @@ int ssh2_rsa_new(ssh2_rsa_ctx **rsa,
|
||||
coeffdata, coefflen)) {
|
||||
ret = -1;
|
||||
}
|
||||
else {
|
||||
else
|
||||
ret = mbedtls_rsa_check_privkey(ctx);
|
||||
}
|
||||
}
|
||||
else if(!ret) {
|
||||
else if(!ret)
|
||||
ret = mbedtls_rsa_check_pubkey(ctx);
|
||||
}
|
||||
|
||||
if(ret && ctx) {
|
||||
ssh2_rsa_free(ctx);
|
||||
@@ -472,9 +467,9 @@ int ssh2_rsa_new_private_frommemory(ssh2_rsa_ctx **rsa,
|
||||
/* mbedtls checks in "mbedtls/pkparse.c:1184" if "key[keylen - 1] != '\0'"
|
||||
private-key from memory fails if the last byte is not a null byte */
|
||||
filedata_nullterm = mbedtls_calloc(filedata_len + 1, 1);
|
||||
if(!filedata_nullterm) {
|
||||
if(!filedata_nullterm)
|
||||
return -1;
|
||||
}
|
||||
|
||||
memcpy(filedata_nullterm, filedata, filedata_len);
|
||||
|
||||
mbedtls_pk_init(&pkey);
|
||||
@@ -518,15 +513,12 @@ int ssh2_rsa_sha2_verify(ssh2_rsa_ctx *rsactx,
|
||||
if(!hash)
|
||||
return -1;
|
||||
|
||||
if(hash_len == SHA_DIGEST_LENGTH) {
|
||||
if(hash_len == SHA_DIGEST_LENGTH)
|
||||
md_type = MBEDTLS_MD_SHA1;
|
||||
}
|
||||
else if(hash_len == SHA256_DIGEST_LENGTH) {
|
||||
else if(hash_len == SHA256_DIGEST_LENGTH)
|
||||
md_type = MBEDTLS_MD_SHA256;
|
||||
}
|
||||
else if(hash_len == SHA512_DIGEST_LENGTH) {
|
||||
else if(hash_len == SHA512_DIGEST_LENGTH)
|
||||
md_type = MBEDTLS_MD_SHA512;
|
||||
}
|
||||
else {
|
||||
free(hash);
|
||||
return -1; /* unsupported digest */
|
||||
@@ -566,19 +558,16 @@ int ssh2_rsa_sha2_sign(LIBSSH2_SESSION *session,
|
||||
|
||||
sig_len = mbedtls_rsa_get_len(rsactx);
|
||||
sig = SSH2_ALLOC(session, sig_len);
|
||||
if(!sig) {
|
||||
if(!sig)
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = 0;
|
||||
if(hash_len == SHA_DIGEST_LENGTH) {
|
||||
if(hash_len == SHA_DIGEST_LENGTH)
|
||||
md_type = MBEDTLS_MD_SHA1;
|
||||
}
|
||||
else if(hash_len == SHA256_DIGEST_LENGTH) {
|
||||
else if(hash_len == SHA256_DIGEST_LENGTH)
|
||||
md_type = MBEDTLS_MD_SHA256;
|
||||
}
|
||||
else if(hash_len == SHA512_DIGEST_LENGTH) {
|
||||
else if(hash_len == SHA512_DIGEST_LENGTH)
|
||||
md_type = MBEDTLS_MD_SHA512;
|
||||
}
|
||||
else {
|
||||
ssh2_err(session, LIBSSH2_ERROR_PROTO,
|
||||
"Unsupported hash digest length");
|
||||
@@ -633,9 +622,8 @@ static unsigned char *mbed_gen_publickey_from_rsa(LIBSSH2_SESSION *session,
|
||||
len = 4 + 7 + 4 + e_bytes + 4 + n_bytes;
|
||||
|
||||
key = SSH2_ALLOC(session, len);
|
||||
if(!key) {
|
||||
if(!key)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Process key encoding. */
|
||||
p = key;
|
||||
@@ -682,18 +670,15 @@ static int mbed_pub_priv_key(LIBSSH2_SESSION *session,
|
||||
/* write method */
|
||||
mthlen = 7;
|
||||
mth = SSH2_ALLOC(session, mthlen);
|
||||
if(mth) {
|
||||
if(mth)
|
||||
memcpy(mth, "ssh-rsa", mthlen);
|
||||
}
|
||||
else {
|
||||
else
|
||||
ret = -1;
|
||||
}
|
||||
|
||||
rsa = mbedtls_pk_rsa(*pkey);
|
||||
key = mbed_gen_publickey_from_rsa(session, rsa, &keylen);
|
||||
if(!key) {
|
||||
if(!key)
|
||||
ret = -1;
|
||||
}
|
||||
|
||||
/* write output */
|
||||
if(ret) {
|
||||
@@ -761,9 +746,9 @@ int ssh2_pub_priv_keyfilememory(LIBSSH2_SESSION *session,
|
||||
/* mbedtls checks in "mbedtls/pkparse.c:1184" if "key[keylen - 1] != '\0'"
|
||||
private-key from memory fails if the last byte is not a null byte */
|
||||
privatekeydata_nullterm = mbedtls_calloc(privatekeydata_len + 1, 1);
|
||||
if(!privatekeydata_nullterm) {
|
||||
if(!privatekeydata_nullterm)
|
||||
return -1;
|
||||
}
|
||||
|
||||
memcpy(privatekeydata_nullterm, privatekeydata, privatekeydata_len);
|
||||
|
||||
mbedtls_pk_init(&pkey);
|
||||
@@ -1103,13 +1088,11 @@ static int mbed_ecdsa_curve_type_from_name(const char *name,
|
||||
type = SSH2_EC_CURVE_NISTP384;
|
||||
else if(!strcmp(name, "ecdsa-sha2-nistp521"))
|
||||
type = SSH2_EC_CURVE_NISTP521;
|
||||
else {
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(out_type) {
|
||||
if(out_type)
|
||||
*out_type = type;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1179,9 +1162,8 @@ failed:
|
||||
|
||||
cleanup:
|
||||
|
||||
if(decrypted) {
|
||||
if(decrypted)
|
||||
ssh2_string_buf_free(session, decrypted);
|
||||
}
|
||||
|
||||
return *ctx ? 0 : -1;
|
||||
}
|
||||
@@ -1285,20 +1267,17 @@ static unsigned char *mbed_mpi_write_binary(unsigned char *buf,
|
||||
unsigned char *p = buf;
|
||||
uint32_t size = (uint32_t)bytes;
|
||||
|
||||
if(sizeof(&p) / sizeof(p[0]) < 4) {
|
||||
if(sizeof(&p) / sizeof(p[0]) < 4)
|
||||
goto done;
|
||||
}
|
||||
|
||||
p += 4;
|
||||
*p = 0;
|
||||
|
||||
if(size > 0) {
|
||||
if(size > 0)
|
||||
mbedtls_mpi_write_binary(mpi, p + 1, size - 1);
|
||||
}
|
||||
|
||||
if(size > 0 && !(*(p + 1) & 0x80)) {
|
||||
if(size > 0 && !(*(p + 1) & 0x80))
|
||||
memmove(p, p + 1, --size);
|
||||
}
|
||||
|
||||
ssh2_htonu32(p - 4, size);
|
||||
|
||||
|
||||
54
src/misc.c
54
src/misc.c
@@ -382,10 +382,9 @@ int ssh2_base64_decode(LIBSSH2_SESSION *session,
|
||||
*datalen = 0;
|
||||
*data = SSH2_ALLOC(session, src_len);
|
||||
d = (unsigned char *)*data;
|
||||
if(!d) {
|
||||
if(!d)
|
||||
return ssh2_err(session, LIBSSH2_ERROR_ALLOC,
|
||||
"Unable to allocate memory for base64 decoding");
|
||||
}
|
||||
|
||||
for(s = src; s < (src + src_len); s++) {
|
||||
v = base64_reverse_table[(unsigned char)*s];
|
||||
@@ -547,10 +546,8 @@ void ssh2_deb_low(LIBSSH2_SESSION *session, int context,
|
||||
const char *contexttext = contexts[0];
|
||||
unsigned int contextindex;
|
||||
|
||||
if(!(session->showmask & context)) {
|
||||
/* no such output asked for */
|
||||
return;
|
||||
}
|
||||
if(!(session->showmask & context))
|
||||
return; /* no such output asked for */
|
||||
|
||||
/* Find the first matching context string for this message */
|
||||
for(contextindex = 0; contextindex < SSH2_ARRAYSIZE(contexts);
|
||||
@@ -562,9 +559,8 @@ void ssh2_deb_low(LIBSSH2_SESSION *session, int context,
|
||||
}
|
||||
|
||||
gettimeofday(&now, NULL);
|
||||
if(!firstsec) {
|
||||
if(!firstsec)
|
||||
firstsec = now.tv_sec;
|
||||
}
|
||||
now.tv_sec -= firstsec;
|
||||
|
||||
len = snprintf(buffer, buflen, "[libssh2] %d.%06d %s: ",
|
||||
@@ -754,9 +750,8 @@ int ssh2_gettimeofday(struct timeval *tp, void *tzp)
|
||||
void *ssh2_calloc(LIBSSH2_SESSION *session, size_t size)
|
||||
{
|
||||
void *p = SSH2_ALLOC(session, size);
|
||||
if(p) {
|
||||
if(p)
|
||||
memset(p, 0, size);
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
@@ -808,9 +803,8 @@ void ssh2_string_buf_free(LIBSSH2_SESSION *session, struct string_buf *buf)
|
||||
|
||||
int ssh2_get_byte(struct string_buf *buf, unsigned char *out)
|
||||
{
|
||||
if(!ssh2_check_length(buf, 1)) {
|
||||
if(!ssh2_check_length(buf, 1))
|
||||
return -1;
|
||||
}
|
||||
|
||||
*out = buf->dataptr[0];
|
||||
buf->dataptr += 1;
|
||||
@@ -819,9 +813,8 @@ int ssh2_get_byte(struct string_buf *buf, unsigned char *out)
|
||||
|
||||
int ssh2_get_boolean(struct string_buf *buf, unsigned char *out)
|
||||
{
|
||||
if(!ssh2_check_length(buf, 1)) {
|
||||
if(!ssh2_check_length(buf, 1))
|
||||
return -1;
|
||||
}
|
||||
|
||||
*out = buf->dataptr[0] == 0 ? 0 : 1;
|
||||
buf->dataptr += 1;
|
||||
@@ -830,9 +823,8 @@ int ssh2_get_boolean(struct string_buf *buf, unsigned char *out)
|
||||
|
||||
int ssh2_get_u32(struct string_buf *buf, uint32_t *out)
|
||||
{
|
||||
if(!ssh2_check_length(buf, 4)) {
|
||||
if(!ssh2_check_length(buf, 4))
|
||||
return -1;
|
||||
}
|
||||
|
||||
*out = ssh2_ntohu32(buf->dataptr);
|
||||
buf->dataptr += 4;
|
||||
@@ -841,9 +833,8 @@ int ssh2_get_u32(struct string_buf *buf, uint32_t *out)
|
||||
|
||||
int ssh2_get_u64(struct string_buf *buf, libssh2_uint64_t *out)
|
||||
{
|
||||
if(!ssh2_check_length(buf, 8)) {
|
||||
if(!ssh2_check_length(buf, 8))
|
||||
return -1;
|
||||
}
|
||||
|
||||
*out = ssh2_ntohu64(buf->dataptr);
|
||||
buf->dataptr += 8;
|
||||
@@ -855,9 +846,8 @@ int ssh2_match_string(struct string_buf *buf, const char *match)
|
||||
unsigned char *out;
|
||||
size_t len = 0;
|
||||
if(ssh2_get_string(buf, &out, &len) || len != strlen(match) ||
|
||||
strncmp((char *)out, match, strlen(match))) {
|
||||
strncmp((char *)out, match, strlen(match)))
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -865,12 +855,10 @@ int ssh2_get_string(struct string_buf *buf, unsigned char **outbuf,
|
||||
size_t *outlen)
|
||||
{
|
||||
uint32_t data_len;
|
||||
if(!buf || ssh2_get_u32(buf, &data_len) != 0) {
|
||||
if(!buf || ssh2_get_u32(buf, &data_len) != 0)
|
||||
return -1;
|
||||
}
|
||||
if(!ssh2_check_length(buf, data_len)) {
|
||||
if(!ssh2_check_length(buf, data_len))
|
||||
return -1;
|
||||
}
|
||||
*outbuf = buf->dataptr;
|
||||
buf->dataptr += data_len;
|
||||
|
||||
@@ -886,22 +874,18 @@ int ssh2_copy_string(LIBSSH2_SESSION *session, struct string_buf *buf,
|
||||
size_t str_len;
|
||||
unsigned char *str;
|
||||
|
||||
if(ssh2_get_string(buf, &str, &str_len)) {
|
||||
if(ssh2_get_string(buf, &str, &str_len))
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(str_len) {
|
||||
*outbuf = SSH2_ALLOC(session, str_len);
|
||||
if(*outbuf) {
|
||||
if(*outbuf)
|
||||
memcpy(*outbuf, str, str_len);
|
||||
}
|
||||
else {
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
else {
|
||||
else
|
||||
*outbuf = NULL;
|
||||
}
|
||||
|
||||
if(outlen)
|
||||
*outlen = str_len;
|
||||
@@ -916,12 +900,10 @@ int ssh2_get_bignum_bytes(struct string_buf *buf, unsigned char **outbuf,
|
||||
uint32_t bn_len;
|
||||
unsigned char *bnptr;
|
||||
|
||||
if(ssh2_get_u32(buf, &data_len)) {
|
||||
if(ssh2_get_u32(buf, &data_len))
|
||||
return -1;
|
||||
}
|
||||
if(!ssh2_check_length(buf, data_len)) {
|
||||
if(!ssh2_check_length(buf, data_len))
|
||||
return -1;
|
||||
}
|
||||
|
||||
bn_len = data_len;
|
||||
bnptr = buf->dataptr;
|
||||
|
||||
@@ -860,10 +860,9 @@ int ssh2_cipher_init(ssh2_cipher_ctx *h, SSH2_CIPHER_T(algo),
|
||||
*h = EVP_CIPHER_CTX_new();
|
||||
rc = !EVP_CipherInit(*h, algo(), secret, iv, encrypt);
|
||||
#if LIBSSH2_AES_GCM
|
||||
if(is_aesgcm) {
|
||||
if(is_aesgcm)
|
||||
/* Sets both fixed and invocation_counter parts of IV */
|
||||
rc |= !EVP_CIPHER_CTX_ctrl(*h, EVP_CTRL_AEAD_SET_IV_FIXED, -1, iv);
|
||||
}
|
||||
#endif /* LIBSSH2_AES_GCM */
|
||||
|
||||
return rc;
|
||||
|
||||
@@ -221,7 +221,7 @@
|
||||
#include "crypto_config.h"
|
||||
|
||||
void ssh2_crypto_init(void);
|
||||
#define ssh2_crypto_exit()
|
||||
#define ssh2_crypto_exit() do {} while(0)
|
||||
|
||||
int ssh2_random(unsigned char *buf, size_t len);
|
||||
|
||||
|
||||
@@ -231,8 +231,8 @@ struct os400qc3_dh_ctx { /* Diffie-Hellman context. */
|
||||
*
|
||||
*******************************************************************/
|
||||
|
||||
#define ssh2_crypto_init()
|
||||
#define ssh2_crypto_exit()
|
||||
#define ssh2_crypto_init() do {} while(0)
|
||||
#define ssh2_crypto_exit() do {} while(0)
|
||||
|
||||
#define ssh2_sha1_ctx Qc3_Format_ALGD0100_T
|
||||
#define ssh2_sha256_ctx Qc3_Format_ALGD0100_T
|
||||
|
||||
118
src/packet.c
118
src/packet.c
@@ -83,45 +83,37 @@ static SSH2_INLINE int packet_queue_listener(
|
||||
buf.dataptr = buf.data;
|
||||
buf.len = datalen;
|
||||
|
||||
if(datalen < offset + 12) { /* 3 * 4-byte */
|
||||
if(datalen < offset + 12) /* 3 * 4-byte */
|
||||
return ssh2_err(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY,
|
||||
"Unexpected packet size");
|
||||
}
|
||||
|
||||
buf.dataptr += offset;
|
||||
|
||||
if(ssh2_get_u32(&buf, &(listen_state->sender_channel))) {
|
||||
if(ssh2_get_u32(&buf, &(listen_state->sender_channel)))
|
||||
return ssh2_err(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL,
|
||||
"Data too short extracting channel");
|
||||
}
|
||||
if(ssh2_get_u32(&buf, &(listen_state->initial_window_size))) {
|
||||
if(ssh2_get_u32(&buf, &(listen_state->initial_window_size)))
|
||||
return ssh2_err(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL,
|
||||
"Data too short extracting window size");
|
||||
}
|
||||
if(ssh2_get_u32(&buf, &(listen_state->packet_size))) {
|
||||
if(ssh2_get_u32(&buf, &(listen_state->packet_size)))
|
||||
return ssh2_err(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL,
|
||||
"Data too short extracting packet");
|
||||
}
|
||||
if(ssh2_get_string(&buf, &(listen_state->host), &temp_len)) {
|
||||
if(ssh2_get_string(&buf, &(listen_state->host), &temp_len))
|
||||
return ssh2_err(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL,
|
||||
"Data too short extracting host");
|
||||
}
|
||||
listen_state->host_len = (uint32_t)temp_len;
|
||||
|
||||
if(ssh2_get_u32(&buf, &(listen_state->port))) {
|
||||
if(ssh2_get_u32(&buf, &(listen_state->port)))
|
||||
return ssh2_err(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL,
|
||||
"Data too short extracting port");
|
||||
}
|
||||
if(ssh2_get_string(&buf, &(listen_state->shost), &temp_len)) {
|
||||
if(ssh2_get_string(&buf, &(listen_state->shost), &temp_len))
|
||||
return ssh2_err(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL,
|
||||
"Data too short extracting shost");
|
||||
}
|
||||
listen_state->shost_len = (uint32_t)temp_len;
|
||||
|
||||
if(ssh2_get_u32(&buf, &(listen_state->sport))) {
|
||||
if(ssh2_get_u32(&buf, &(listen_state->sport)))
|
||||
return ssh2_err(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL,
|
||||
"Data too short extracting sport");
|
||||
}
|
||||
|
||||
ssh2_deb((session, LIBSSH2_TRACE_CONN,
|
||||
"Remote received connection from %.*s:%u to %.*s:%u",
|
||||
@@ -255,9 +247,8 @@ static SSH2_INLINE int packet_queue_listener(
|
||||
|
||||
rc = ssh2_transport_send(session, listen_state->packet,
|
||||
packet_len, NULL, 0);
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN)
|
||||
return rc;
|
||||
}
|
||||
else if(rc) {
|
||||
listen_state->state = ssh2_NB_state_idle;
|
||||
return ssh2_err(session, rc, "Unable to send open failure");
|
||||
@@ -282,7 +273,6 @@ static SSH2_INLINE int packet_x11_open(
|
||||
int rc;
|
||||
|
||||
if(x11open_state->state == ssh2_NB_state_idle) {
|
||||
|
||||
size_t offset = strlen("x11") + 5;
|
||||
size_t temp_len = 0;
|
||||
unsigned char *temp_buf = NULL;
|
||||
@@ -404,9 +394,8 @@ static SSH2_INLINE int packet_x11_open(
|
||||
if(x11open_state->state == ssh2_NB_state_created) {
|
||||
rc = ssh2_transport_send(session, x11open_state->packet, 17,
|
||||
NULL, 0);
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN)
|
||||
return rc;
|
||||
}
|
||||
else if(rc) {
|
||||
SSH2_FREE(session, x11open_state->shost);
|
||||
x11open_state->shost = NULL;
|
||||
@@ -447,9 +436,8 @@ x11_exit:
|
||||
|
||||
rc = ssh2_transport_send(session, x11open_state->packet, packet_len,
|
||||
NULL, 0);
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN)
|
||||
return rc;
|
||||
}
|
||||
else if(rc) {
|
||||
x11open_state->state = ssh2_NB_state_idle;
|
||||
return ssh2_err(session, rc, "Unable to send open failure");
|
||||
@@ -479,26 +467,22 @@ static SSH2_INLINE int packet_authagent_open(
|
||||
buf.dataptr = buf.data;
|
||||
buf.len = datalen;
|
||||
|
||||
if(datalen < offset + 12) { /* 27-byte header + 3 * 4-byte */
|
||||
if(datalen < offset + 12) /* 27-byte header + 3 * 4-byte */
|
||||
return ssh2_err(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY,
|
||||
"Unexpected packet size");
|
||||
}
|
||||
|
||||
buf.dataptr += offset;
|
||||
|
||||
if(authagent_state->state == ssh2_NB_state_idle) {
|
||||
if(ssh2_get_u32(&buf, &(authagent_state->sender_channel))) {
|
||||
if(ssh2_get_u32(&buf, &(authagent_state->sender_channel)))
|
||||
return ssh2_err(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL,
|
||||
"Data too short extracting channel");
|
||||
}
|
||||
if(ssh2_get_u32(&buf, &(authagent_state->initial_window_size))) {
|
||||
if(ssh2_get_u32(&buf, &(authagent_state->initial_window_size)))
|
||||
return ssh2_err(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL,
|
||||
"Data too short extracting window size");
|
||||
}
|
||||
if(ssh2_get_u32(&buf, &(authagent_state->packet_size))) {
|
||||
if(ssh2_get_u32(&buf, &(authagent_state->packet_size)))
|
||||
return ssh2_err(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL,
|
||||
"Data too short extracting packet");
|
||||
}
|
||||
|
||||
ssh2_deb((session, LIBSSH2_TRACE_CONN,
|
||||
"Auth Agent Connection Received on channel %u",
|
||||
@@ -567,9 +551,8 @@ static SSH2_INLINE int packet_authagent_open(
|
||||
if(authagent_state->state == ssh2_NB_state_created) {
|
||||
rc = ssh2_transport_send(session, authagent_state->packet, 17,
|
||||
NULL, 0);
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN)
|
||||
return rc;
|
||||
}
|
||||
else if(rc) {
|
||||
authagent_state->state = ssh2_NB_state_idle;
|
||||
return ssh2_err(session, LIBSSH2_ERROR_SOCKET_SEND,
|
||||
@@ -607,9 +590,8 @@ authagent_exit:
|
||||
|
||||
rc = ssh2_transport_send(session, authagent_state->packet, packet_len,
|
||||
NULL, 0);
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN)
|
||||
return rc;
|
||||
}
|
||||
else if(rc) {
|
||||
authagent_state->state = ssh2_NB_state_idle;
|
||||
return ssh2_err(session, rc, "Unable to send open failure");
|
||||
@@ -764,11 +746,10 @@ int ssh2_packet_add(LIBSSH2_SESSION *session, unsigned char *data,
|
||||
ssh2_get_string(&buf, &message, &message_len);
|
||||
ssh2_get_string(&buf, &language, &language_len);
|
||||
|
||||
if(session->ssh_msg_disconnect) {
|
||||
if(session->ssh_msg_disconnect)
|
||||
SSH2_DISCONNECT(session, reason, (const char *)message,
|
||||
message_len, (const char *)language,
|
||||
language_len);
|
||||
}
|
||||
|
||||
ssh2_deb((session, LIBSSH2_TRACE_TRANS,
|
||||
"Disconnect(%u): %.*s(%.*s)", reason,
|
||||
@@ -788,9 +769,8 @@ int ssh2_packet_add(LIBSSH2_SESSION *session, unsigned char *data,
|
||||
|
||||
case SSH_MSG_IGNORE:
|
||||
if(datalen >= 2) {
|
||||
if(session->ssh_msg_ignore) {
|
||||
if(session->ssh_msg_ignore)
|
||||
SSH2_IGNORE(session, (char *)data + 1, datalen - 1);
|
||||
}
|
||||
}
|
||||
else if(session->ssh_msg_ignore) {
|
||||
SSH2_IGNORE(session, "", 0);
|
||||
@@ -821,11 +801,10 @@ int ssh2_packet_add(LIBSSH2_SESSION *session, unsigned char *data,
|
||||
ssh2_get_string(&buf, &language, &language_len);
|
||||
}
|
||||
|
||||
if(session->ssh_msg_debug) {
|
||||
if(session->ssh_msg_debug)
|
||||
SSH2_DEBUG(session, always_display, (const char *)message,
|
||||
message_len, (const char *)language,
|
||||
language_len);
|
||||
}
|
||||
}
|
||||
|
||||
ssh2_deb((session, LIBSSH2_TRACE_TRANS, "Debug Packet: %.*s",
|
||||
@@ -852,10 +831,9 @@ int ssh2_packet_add(LIBSSH2_SESSION *session, unsigned char *data,
|
||||
buf.dataptr += 1; /* advance past type */
|
||||
|
||||
if(ssh2_get_u32(&buf, &nr_extensions) != 0 ||
|
||||
nr_extensions >= 1024) {
|
||||
nr_extensions >= 1024)
|
||||
rc = ssh2_err(session, LIBSSH2_ERROR_PROTO,
|
||||
"Invalid extension info received");
|
||||
}
|
||||
|
||||
while(rc == 0 && nr_extensions > 0) {
|
||||
|
||||
@@ -871,18 +849,16 @@ int ssh2_packet_add(LIBSSH2_SESSION *session, unsigned char *data,
|
||||
if(ssh2_get_string(&buf, &value, &value_len))
|
||||
break;
|
||||
|
||||
if(name && value) {
|
||||
if(name && value)
|
||||
ssh2_deb((session, LIBSSH2_TRACE_KEX,
|
||||
"Server to Client extension %.*s: %.*s",
|
||||
(int)name_len, name, (int)value_len, value));
|
||||
}
|
||||
|
||||
if(name && name_len == 15 &&
|
||||
!memcmp(name, "server-sig-algs", 15)) {
|
||||
if(session->server_sign_algorithms) {
|
||||
if(session->server_sign_algorithms)
|
||||
SSH2_FREE(session,
|
||||
session->server_sign_algorithms);
|
||||
}
|
||||
|
||||
session->server_sign_algorithms =
|
||||
SSH2_ALLOC(session, value_len + 1);
|
||||
@@ -892,10 +868,9 @@ int ssh2_packet_add(LIBSSH2_SESSION *session, unsigned char *data,
|
||||
value, value_len);
|
||||
session->server_sign_algorithms[value_len] = '\0';
|
||||
}
|
||||
else {
|
||||
else
|
||||
rc = ssh2_err(session, LIBSSH2_ERROR_ALLOC,
|
||||
"memory for server sign algo");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1149,10 +1124,9 @@ ssh2_packet_add_jump_point1:
|
||||
if(channelp) {
|
||||
|
||||
uint32_t status = 0;
|
||||
if(ssh2_get_u32(&buf, &status)) {
|
||||
if(ssh2_get_u32(&buf, &status))
|
||||
rc = ssh2_err(session, LIBSSH2_ERROR_PROTO,
|
||||
"exit-signal status error");
|
||||
}
|
||||
|
||||
channelp->exit_status = (int)status;
|
||||
|
||||
@@ -1176,15 +1150,13 @@ ssh2_packet_add_jump_point1:
|
||||
/* signal name (without SIG prefix) */
|
||||
unsigned char *sig_name = NULL;
|
||||
size_t sig_len = 0;
|
||||
if(ssh2_get_string(&buf, &sig_name, &sig_len)) {
|
||||
if(ssh2_get_string(&buf, &sig_name, &sig_len))
|
||||
rc = ssh2_err(session, LIBSSH2_ERROR_PROTO,
|
||||
"signal name protocol error");
|
||||
}
|
||||
|
||||
if(sig_len > UINT32_MAX - 1) {
|
||||
if(sig_len > UINT32_MAX - 1)
|
||||
rc = ssh2_err(session, LIBSSH2_ERROR_PROTO,
|
||||
"signal name out of bounds");
|
||||
}
|
||||
else if(sig_len > 0) {
|
||||
channelp->exit_signal =
|
||||
SSH2_ALLOC(session, sig_len + 1);
|
||||
@@ -1201,14 +1173,12 @@ ssh2_packet_add_jump_point1:
|
||||
channelp->local.id,
|
||||
channelp->remote.id));
|
||||
}
|
||||
else {
|
||||
else
|
||||
rc = ssh2_err(session, LIBSSH2_ERROR_ALLOC,
|
||||
"exit signal alloc error");
|
||||
}
|
||||
}
|
||||
else {
|
||||
else
|
||||
channelp->exit_signal = NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1329,10 +1299,9 @@ ssh2_packet_add_jump_authagent:
|
||||
channelp =
|
||||
ssh2_channel_locate(session, ssh2_ntohu32(data + 1));
|
||||
if(channelp) {
|
||||
if(bytestoadd > UINT32_MAX - channelp->local.window_size) {
|
||||
if(bytestoadd > UINT32_MAX - channelp->local.window_size)
|
||||
rc = ssh2_err(session, LIBSSH2_ERROR_PROTO,
|
||||
"Window adjust out of bounds");
|
||||
}
|
||||
else {
|
||||
channelp->local.window_size += bytestoadd;
|
||||
|
||||
@@ -1474,9 +1443,8 @@ int ssh2_packet_askv(LIBSSH2_SESSION *session,
|
||||
for(i = 0; i < packet_types_len; i++) {
|
||||
if(ssh2_packet_ask(session, packet_types[i], data,
|
||||
data_len, match_ofs,
|
||||
match_buf, match_len) == 0) {
|
||||
match_buf, match_len) == 0)
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
@@ -1500,10 +1468,8 @@ int ssh2_packet_require(LIBSSH2_SESSION *session,
|
||||
if(state->start == 0) {
|
||||
if(ssh2_packet_ask(session, packet_type, data, data_len,
|
||||
match_ofs, match_buf,
|
||||
match_len) == 0) {
|
||||
/* A packet was available in the packet brigade */
|
||||
return 0;
|
||||
}
|
||||
match_len) == 0)
|
||||
return 0; /* A packet was available in the packet brigade */
|
||||
|
||||
state->start = time(NULL);
|
||||
}
|
||||
@@ -1557,9 +1523,8 @@ int ssh2_packet_burn(LIBSSH2_SESSION *session, ssh2_NB_states *state)
|
||||
int ret;
|
||||
|
||||
if(*state == ssh2_NB_state_idle) {
|
||||
for(i = 1; i < 255; i++) {
|
||||
for(i = 1; i < 255; i++)
|
||||
all_packets[i - 1] = i;
|
||||
}
|
||||
all_packets[254] = 0;
|
||||
|
||||
if(ssh2_packet_askv(session, all_packets, &data, &data_len, 0,
|
||||
@@ -1577,17 +1542,14 @@ int ssh2_packet_burn(LIBSSH2_SESSION *session, ssh2_NB_states *state)
|
||||
|
||||
while(session->socket_state == SSH2_SOCKET_CONNECTED) {
|
||||
ret = ssh2_transport_read(session);
|
||||
if(ret == LIBSSH2_ERROR_EAGAIN) {
|
||||
if(ret == LIBSSH2_ERROR_EAGAIN)
|
||||
return ret;
|
||||
}
|
||||
else if(ret < 0) {
|
||||
*state = ssh2_NB_state_idle;
|
||||
return ret;
|
||||
}
|
||||
else if(ret == 0) {
|
||||
/* FIXME: this might busyloop */
|
||||
continue;
|
||||
}
|
||||
else if(ret == 0)
|
||||
continue; /* FIXME: this might busyloop */
|
||||
|
||||
/* Be lazy, let packet_ask pull it out of the brigade */
|
||||
if(ssh2_packet_ask(session, (unsigned char)ret, &data, &data_len,
|
||||
@@ -1622,9 +1584,8 @@ int ssh2_packet_requirev(LIBSSH2_SESSION *session,
|
||||
return 0;
|
||||
}
|
||||
|
||||
if(state->start == 0) {
|
||||
if(state->start == 0)
|
||||
state->start = time(NULL);
|
||||
}
|
||||
|
||||
while(session->socket_state != SSH2_SOCKET_DISCONNECTED) {
|
||||
int ret = ssh2_transport_read(session);
|
||||
@@ -1640,9 +1601,8 @@ int ssh2_packet_requirev(LIBSSH2_SESSION *session,
|
||||
state->start = 0;
|
||||
return LIBSSH2_ERROR_TIMEOUT;
|
||||
}
|
||||
else if(ret == LIBSSH2_ERROR_EAGAIN) {
|
||||
else if(ret == LIBSSH2_ERROR_EAGAIN)
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
if(strchr((const char *)packet_types, ret)) {
|
||||
|
||||
86
src/pem.c
86
src/pem.c
@@ -44,25 +44,21 @@ static int readline(char *line, int line_size, FILE *fp)
|
||||
{
|
||||
size_t len;
|
||||
|
||||
if(!line) {
|
||||
if(!line)
|
||||
return -1;
|
||||
}
|
||||
if(!fgets(line, line_size, fp)) {
|
||||
if(!fgets(line, line_size, fp))
|
||||
return -1;
|
||||
|
||||
if(*line) {
|
||||
len = strlen(line);
|
||||
if(len > 0 && line[len - 1] == '\n')
|
||||
line[len - 1] = '\0';
|
||||
}
|
||||
|
||||
if(*line) {
|
||||
len = strlen(line);
|
||||
if(len > 0 && line[len - 1] == '\n') {
|
||||
if(len > 0 && line[len - 1] == '\r')
|
||||
line[len - 1] = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
if(*line) {
|
||||
len = strlen(line);
|
||||
if(len > 0 && line[len - 1] == '\r') {
|
||||
line[len - 1] = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
@@ -78,9 +74,8 @@ static int readline_memory(char *line, size_t line_size,
|
||||
|
||||
for(len = 0; off + len < filedata_len && len < line_size - 1; len++) {
|
||||
if(filedata[off + len] == '\n' ||
|
||||
filedata[off + len] == '\r') {
|
||||
filedata[off + len] == '\r')
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(len) {
|
||||
@@ -163,9 +158,8 @@ int ssh2_pem_parse(LIBSSH2_SESSION *session,
|
||||
data, datalen);
|
||||
|
||||
out:
|
||||
if(filedata) {
|
||||
if(filedata)
|
||||
SSH2_FREE(session, filedata);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -187,14 +181,12 @@ int ssh2_pem_parse_memory(LIBSSH2_SESSION *session,
|
||||
do {
|
||||
*line = '\0';
|
||||
|
||||
if(readline_memory(line, LINE_SIZE, filedata, filedata_len, &off)) {
|
||||
if(readline_memory(line, LINE_SIZE, filedata, filedata_len, &off))
|
||||
return -1;
|
||||
}
|
||||
} while(strcmp(line, headerbegin));
|
||||
|
||||
if(readline_memory(line, LINE_SIZE, filedata, filedata_len, &off)) {
|
||||
if(readline_memory(line, LINE_SIZE, filedata, filedata_len, &off))
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(passphrase &&
|
||||
!memcmp(line, crypt_annotation, strlen(crypt_annotation))) {
|
||||
@@ -267,9 +259,8 @@ int ssh2_pem_parse_memory(LIBSSH2_SESSION *session,
|
||||
}
|
||||
} while(strcmp(line, headerend));
|
||||
|
||||
if(!b64data) {
|
||||
if(!b64data)
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(ssh2_base64_decode(session, (char **)data, datalen,
|
||||
b64data, b64datalen)) {
|
||||
@@ -327,9 +318,8 @@ int ssh2_pem_parse_memory(LIBSSH2_SESSION *session,
|
||||
goto out;
|
||||
}
|
||||
|
||||
if(free_secret) {
|
||||
if(free_secret)
|
||||
ssh2_explicit_zero((char *)secret, sizeof(secret));
|
||||
}
|
||||
|
||||
/* Do the actual decryption */
|
||||
if((*datalen % blocksize) != 0) {
|
||||
@@ -528,9 +518,8 @@ static int openssh_pem_parse_data(LIBSSH2_SESSION *session,
|
||||
/* !checksrc! disable EQUALSNULL 1 */
|
||||
while((cur_method = *all_methods++) != NULL) {
|
||||
if(*cur_method->name && !memcmp(ciphername, cur_method->name,
|
||||
strlen(cur_method->name))) {
|
||||
strlen(cur_method->name)))
|
||||
method = cur_method;
|
||||
}
|
||||
}
|
||||
|
||||
/* None of the available crypt methods were able to decrypt the key */
|
||||
@@ -740,14 +729,12 @@ int ssh2_openssh_pem_parse(LIBSSH2_SESSION *session,
|
||||
do {
|
||||
*line = '\0';
|
||||
|
||||
if(readline(line, LINE_SIZE, fp)) {
|
||||
if(readline(line, LINE_SIZE, fp))
|
||||
return -1;
|
||||
}
|
||||
} while(strcmp(line, OPENSSH_PRIVKEY_HEADER));
|
||||
|
||||
if(readline(line, LINE_SIZE, fp)) {
|
||||
if(readline(line, LINE_SIZE, fp))
|
||||
return -1;
|
||||
}
|
||||
|
||||
do {
|
||||
if(*line) {
|
||||
@@ -775,9 +762,8 @@ int ssh2_openssh_pem_parse(LIBSSH2_SESSION *session,
|
||||
}
|
||||
} while(strcmp(line, OPENSSH_PRIVKEY_FOOTER));
|
||||
|
||||
if(!b64data) {
|
||||
if(!b64data)
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = openssh_pem_parse_data(session, passphrase,
|
||||
b64data, b64datalen, decrypted_buf);
|
||||
@@ -816,9 +802,8 @@ int ssh2_openssh_pem_parse_memory(LIBSSH2_SESSION *session,
|
||||
return ssh2_err(session, LIBSSH2_ERROR_PROTO,
|
||||
"Error parsing PEM: OpenSSH header not found");
|
||||
|
||||
if(readline_memory(line, LINE_SIZE, filedata, filedata_len, &off)) {
|
||||
if(readline_memory(line, LINE_SIZE, filedata, filedata_len, &off))
|
||||
return -1;
|
||||
}
|
||||
} while(strcmp(line, OPENSSH_PRIVKEY_HEADER));
|
||||
|
||||
*line = '\0';
|
||||
@@ -875,33 +860,28 @@ static int read_asn1_length(const unsigned char *data,
|
||||
unsigned int lenlen;
|
||||
int nextpos;
|
||||
|
||||
if(datalen < 1) {
|
||||
if(datalen < 1)
|
||||
return -1;
|
||||
}
|
||||
*len = data[0];
|
||||
|
||||
if(*len >= 0x80) {
|
||||
lenlen = *len & 0x7F;
|
||||
if(1 + lenlen > datalen) {
|
||||
if(1 + lenlen > datalen)
|
||||
return -1;
|
||||
}
|
||||
*len = data[1];
|
||||
if(lenlen > 1) {
|
||||
*len <<= 8;
|
||||
if(2 + lenlen > datalen) {
|
||||
if(2 + lenlen > datalen)
|
||||
return -1;
|
||||
}
|
||||
*len |= data[2];
|
||||
}
|
||||
}
|
||||
else {
|
||||
else
|
||||
lenlen = 0;
|
||||
}
|
||||
|
||||
nextpos = 1 + lenlen;
|
||||
if(lenlen > 2 || 1 + lenlen + *len > datalen) {
|
||||
if(lenlen > 2 || 1 + lenlen + *len > datalen)
|
||||
return -1;
|
||||
}
|
||||
|
||||
return nextpos;
|
||||
}
|
||||
@@ -911,21 +891,18 @@ int ssh2_pem_decode_sequence(unsigned char **data, size_t *datalen)
|
||||
size_t len;
|
||||
int lenlen;
|
||||
|
||||
if(*datalen < 1) {
|
||||
if(*datalen < 1)
|
||||
return -1;
|
||||
}
|
||||
|
||||
if((*data)[0] != '\x30') {
|
||||
if((*data)[0] != '\x30')
|
||||
return -1;
|
||||
}
|
||||
|
||||
(*data)++;
|
||||
(*datalen)--;
|
||||
|
||||
lenlen = read_asn1_length(*data, *datalen, &len);
|
||||
if(lenlen < 0 || lenlen + len != *datalen) {
|
||||
if(lenlen < 0 || lenlen + len != *datalen)
|
||||
return -1;
|
||||
}
|
||||
|
||||
*data += lenlen;
|
||||
*datalen -= lenlen;
|
||||
@@ -939,21 +916,18 @@ int ssh2_pem_decode_integer(unsigned char **data, size_t *datalen,
|
||||
size_t len;
|
||||
int lenlen;
|
||||
|
||||
if(*datalen < 1) {
|
||||
if(*datalen < 1)
|
||||
return -1;
|
||||
}
|
||||
|
||||
if((*data)[0] != '\x02') {
|
||||
if((*data)[0] != '\x02')
|
||||
return -1;
|
||||
}
|
||||
|
||||
(*data)++;
|
||||
(*datalen)--;
|
||||
|
||||
lenlen = read_asn1_length(*data, *datalen, &len);
|
||||
if(lenlen < 0 || lenlen + len > *datalen) {
|
||||
if(lenlen < 0 || lenlen + len > *datalen)
|
||||
return -1;
|
||||
}
|
||||
|
||||
*data += lenlen;
|
||||
*datalen -= lenlen;
|
||||
|
||||
@@ -103,16 +103,13 @@ static void publickey_status_error(const LIBSSH2_PUBLICKEY *pkey,
|
||||
const char *msg;
|
||||
|
||||
/* GENERAL_FAILURE got remapped between version 1 and 2 */
|
||||
if(status == 6 && pkey && pkey->version == 1) {
|
||||
if(status == 6 && pkey && pkey->version == 1)
|
||||
status = 7;
|
||||
}
|
||||
|
||||
if(status > SSH2_PUBLICKEY_STATUS_CODE_MAX) {
|
||||
if(status > SSH2_PUBLICKEY_STATUS_CODE_MAX)
|
||||
msg = "unknown";
|
||||
}
|
||||
else {
|
||||
else
|
||||
msg = publickey_status_codes[status].name;
|
||||
}
|
||||
|
||||
ssh2_err(session, LIBSSH2_ERROR_PUBLICKEY_PROTOCOL, msg);
|
||||
}
|
||||
@@ -132,20 +129,17 @@ static int publickey_packet_receive(LIBSSH2_PUBLICKEY *pkey,
|
||||
|
||||
if(pkey->receive_state == ssh2_NB_state_idle) {
|
||||
rc = ssh2_channel_read(channel, 0, (char *)buffer, 4);
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN)
|
||||
return (int)rc;
|
||||
}
|
||||
else if(rc != 4) {
|
||||
else if(rc != 4)
|
||||
return ssh2_err(session, LIBSSH2_ERROR_PUBLICKEY_PROTOCOL,
|
||||
"Invalid response from publickey subsystem");
|
||||
}
|
||||
|
||||
pkey->receive_packet_len = ssh2_ntohu32(buffer);
|
||||
pkey->receive_packet = SSH2_ALLOC(session, pkey->receive_packet_len);
|
||||
if(!pkey->receive_packet) {
|
||||
if(!pkey->receive_packet)
|
||||
return ssh2_err(session, LIBSSH2_ERROR_ALLOC,
|
||||
"Unable to allocate publickey response buffer");
|
||||
}
|
||||
|
||||
pkey->receive_state = ssh2_NB_state_sent;
|
||||
}
|
||||
@@ -153,9 +147,8 @@ static int publickey_packet_receive(LIBSSH2_PUBLICKEY *pkey,
|
||||
if(pkey->receive_state == ssh2_NB_state_sent) {
|
||||
rc = ssh2_channel_read(channel, 0, (char *)pkey->receive_packet,
|
||||
pkey->receive_packet_len);
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN)
|
||||
return (int)rc;
|
||||
}
|
||||
else if(rc != (ssize_t)pkey->receive_packet_len) {
|
||||
SSH2_FREE(session, pkey->receive_packet);
|
||||
pkey->receive_packet = NULL;
|
||||
@@ -186,17 +179,13 @@ static int publickey_response_id(unsigned char **pdata, size_t data_len)
|
||||
unsigned char *data = *pdata;
|
||||
const struct publickey_code_list *codes = publickey_response_codes;
|
||||
|
||||
if(data_len < 4) {
|
||||
/* Malformed response */
|
||||
return -1;
|
||||
}
|
||||
if(data_len < 4)
|
||||
return -1; /* Malformed response */
|
||||
response_len = ssh2_ntohu32(data);
|
||||
data += 4;
|
||||
data_len -= 4;
|
||||
if(data_len < response_len) {
|
||||
/* Malformed response */
|
||||
return -1;
|
||||
}
|
||||
if(data_len < response_len)
|
||||
return -1; /* Malformed response */
|
||||
|
||||
while(codes->name) {
|
||||
if((unsigned long)codes->name_len == response_len &&
|
||||
@@ -222,19 +211,16 @@ static int publickey_response_success(LIBSSH2_PUBLICKEY *pkey)
|
||||
|
||||
for(;;) {
|
||||
int rc = publickey_packet_receive(pkey, &data, &data_len);
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN)
|
||||
return rc;
|
||||
}
|
||||
else if(rc) {
|
||||
else if(rc)
|
||||
return ssh2_err(session, LIBSSH2_ERROR_SOCKET_TIMEOUT,
|
||||
"Timeout waiting for response from "
|
||||
"publickey subsystem");
|
||||
}
|
||||
|
||||
if(data_len < 4) {
|
||||
if(data_len < 4)
|
||||
return ssh2_err(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL,
|
||||
"Publickey response too small");
|
||||
}
|
||||
|
||||
s = data;
|
||||
response = publickey_response_id(&s, data_len);
|
||||
@@ -244,10 +230,9 @@ static int publickey_response_success(LIBSSH2_PUBLICKEY *pkey)
|
||||
/* Error, or processing complete */
|
||||
unsigned long status = 0;
|
||||
|
||||
if(data_len < 8) {
|
||||
if(data_len < 8)
|
||||
return ssh2_err(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL,
|
||||
"Publickey response too small");
|
||||
}
|
||||
|
||||
status = ssh2_ntohu32(s);
|
||||
|
||||
@@ -261,10 +246,9 @@ static int publickey_response_success(LIBSSH2_PUBLICKEY *pkey)
|
||||
}
|
||||
default:
|
||||
SSH2_FREE(session, data);
|
||||
if(response < 0) {
|
||||
if(response < 0)
|
||||
return ssh2_err(session, LIBSSH2_ERROR_PUBLICKEY_PROTOCOL,
|
||||
"Invalid publickey subsystem response");
|
||||
}
|
||||
/* Unknown/Unexpected */
|
||||
ssh2_err(session, LIBSSH2_ERROR_PUBLICKEY_PROTOCOL,
|
||||
"Unexpected publickey subsystem response");
|
||||
@@ -590,18 +574,16 @@ int libssh2_publickey_add_ex(LIBSSH2_PUBLICKEY *pkey,
|
||||
}
|
||||
else {
|
||||
packet_len += 5; /* overwrite(1) + attribute_count(4) */
|
||||
for(i = 0; i < num_attrs; i++) {
|
||||
for(i = 0; i < num_attrs; i++)
|
||||
packet_len += 9 + attrs[i].name_len + attrs[i].value_len;
|
||||
/* name_len(4) + value_len(4) + mandatory(1) */
|
||||
}
|
||||
}
|
||||
|
||||
pkey->add_packet = SSH2_ALLOC(session, packet_len);
|
||||
if(!pkey->add_packet) {
|
||||
if(!pkey->add_packet)
|
||||
return ssh2_err(session, LIBSSH2_ERROR_ALLOC,
|
||||
"Unable to allocate memory for "
|
||||
"publickey \"add\" packet");
|
||||
}
|
||||
|
||||
pkey->add_s = pkey->add_packet;
|
||||
ssh2_htonu32(pkey->add_s, (uint32_t)(packet_len - 4));
|
||||
@@ -666,9 +648,8 @@ int libssh2_publickey_add_ex(LIBSSH2_PUBLICKEY *pkey,
|
||||
ssize_t nwritten;
|
||||
nwritten = ssh2_channel_write(channel, 0, pkey->add_packet,
|
||||
(pkey->add_s - pkey->add_packet));
|
||||
if(nwritten == LIBSSH2_ERROR_EAGAIN) {
|
||||
if(nwritten == LIBSSH2_ERROR_EAGAIN)
|
||||
return (int)nwritten;
|
||||
}
|
||||
else if((pkey->add_s - pkey->add_packet) != nwritten) {
|
||||
SSH2_FREE(session, pkey->add_packet);
|
||||
pkey->add_packet = NULL;
|
||||
@@ -682,9 +663,8 @@ int libssh2_publickey_add_ex(LIBSSH2_PUBLICKEY *pkey,
|
||||
}
|
||||
|
||||
rc = publickey_response_success(pkey);
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN)
|
||||
return rc;
|
||||
}
|
||||
|
||||
pkey->add_state = ssh2_NB_state_idle;
|
||||
|
||||
@@ -718,11 +698,10 @@ int libssh2_publickey_remove_ex(LIBSSH2_PUBLICKEY *pkey,
|
||||
pkey->remove_packet = NULL;
|
||||
|
||||
pkey->remove_packet = SSH2_ALLOC(session, packet_len);
|
||||
if(!pkey->remove_packet) {
|
||||
if(!pkey->remove_packet)
|
||||
return ssh2_err(session, LIBSSH2_ERROR_ALLOC,
|
||||
"Unable to allocate memory for "
|
||||
"publickey \"remove\" packet");
|
||||
}
|
||||
|
||||
pkey->remove_s = pkey->remove_packet;
|
||||
ssh2_htonu32(pkey->remove_s, (uint32_t)(packet_len - 4));
|
||||
@@ -751,9 +730,8 @@ int libssh2_publickey_remove_ex(LIBSSH2_PUBLICKEY *pkey,
|
||||
ssize_t nwritten;
|
||||
nwritten = ssh2_channel_write(channel, 0, pkey->remove_packet,
|
||||
(pkey->remove_s - pkey->remove_packet));
|
||||
if(nwritten == LIBSSH2_ERROR_EAGAIN) {
|
||||
if(nwritten == LIBSSH2_ERROR_EAGAIN)
|
||||
return (int)nwritten;
|
||||
}
|
||||
else if((pkey->remove_s - pkey->remove_packet) != nwritten) {
|
||||
SSH2_FREE(session, pkey->remove_packet);
|
||||
pkey->remove_packet = NULL;
|
||||
@@ -768,9 +746,8 @@ int libssh2_publickey_remove_ex(LIBSSH2_PUBLICKEY *pkey,
|
||||
}
|
||||
|
||||
rc = publickey_response_success(pkey);
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN)
|
||||
return rc;
|
||||
}
|
||||
|
||||
pkey->remove_state = ssh2_NB_state_idle;
|
||||
|
||||
@@ -821,9 +798,8 @@ int libssh2_publickey_list_fetch(LIBSSH2_PUBLICKEY *pkey,
|
||||
pkey->listFetch_buffer,
|
||||
(pkey->listFetch_s -
|
||||
pkey->listFetch_buffer));
|
||||
if(nwritten == LIBSSH2_ERROR_EAGAIN) {
|
||||
if(nwritten == LIBSSH2_ERROR_EAGAIN)
|
||||
return (int)nwritten;
|
||||
}
|
||||
else if((pkey->listFetch_s - pkey->listFetch_buffer) != nwritten) {
|
||||
pkey->listFetch_state = ssh2_NB_state_idle;
|
||||
return ssh2_err(session, LIBSSH2_ERROR_SOCKET_SEND,
|
||||
@@ -836,9 +812,8 @@ int libssh2_publickey_list_fetch(LIBSSH2_PUBLICKEY *pkey,
|
||||
for(;;) {
|
||||
rc = publickey_packet_receive(pkey, &pkey->listFetch_data,
|
||||
&pkey->listFetch_data_len);
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN)
|
||||
return rc;
|
||||
}
|
||||
else if(rc) {
|
||||
ssh2_err(session, LIBSSH2_ERROR_SOCKET_TIMEOUT,
|
||||
"Timeout waiting for response from publickey subsystem");
|
||||
@@ -1135,9 +1110,8 @@ int libssh2_publickey_list_fetch(LIBSSH2_PUBLICKEY *pkey,
|
||||
list[keys].attrs[i].mandatory = 0;
|
||||
}
|
||||
}
|
||||
else {
|
||||
else
|
||||
list[keys].attrs = NULL;
|
||||
}
|
||||
}
|
||||
/* To be FREEd in libssh2_publickey_list_free() */
|
||||
list[keys].packet = pkey->listFetch_data;
|
||||
@@ -1161,9 +1135,8 @@ err_exit:
|
||||
SSH2_FREE(session, pkey->listFetch_data);
|
||||
pkey->listFetch_data = NULL;
|
||||
}
|
||||
if(list) {
|
||||
if(list)
|
||||
libssh2_publickey_list_free(pkey, list);
|
||||
}
|
||||
pkey->listFetch_state = ssh2_NB_state_idle;
|
||||
return -1;
|
||||
}
|
||||
@@ -1183,9 +1156,8 @@ void libssh2_publickey_list_free(LIBSSH2_PUBLICKEY *pkey,
|
||||
session = pkey->channel->session;
|
||||
|
||||
while(p->packet) {
|
||||
if(p->attrs) {
|
||||
if(p->attrs)
|
||||
SSH2_FREE(session, p->attrs);
|
||||
}
|
||||
SSH2_FREE(session, p->packet);
|
||||
p++;
|
||||
}
|
||||
|
||||
32
src/scp.c
32
src/scp.c
@@ -321,11 +321,10 @@ static LIBSSH2_CHANNEL *scp_recv(LIBSSH2_SESSION *session,
|
||||
memcpy(&session->scpRecv_command[cmd_len], path, path_len);
|
||||
cmd_len += path_len;
|
||||
}
|
||||
else {
|
||||
else
|
||||
cmd_len += shell_quotearg(path,
|
||||
&session->scpRecv_command[cmd_len],
|
||||
session->scpRecv_command_len - cmd_len);
|
||||
}
|
||||
|
||||
/* the command to exec should _not_ be null-terminated */
|
||||
session->scpRecv_command_len = cmd_len;
|
||||
@@ -349,10 +348,9 @@ static LIBSSH2_CHANNEL *scp_recv(LIBSSH2_SESSION *session,
|
||||
session->scpRecv_command = NULL;
|
||||
session->scpRecv_state = ssh2_NB_state_idle;
|
||||
}
|
||||
else {
|
||||
else
|
||||
ssh2_err(session, LIBSSH2_ERROR_EAGAIN,
|
||||
"Would block starting up channel");
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -393,9 +391,8 @@ static LIBSSH2_CHANNEL *scp_recv(LIBSSH2_SESSION *session,
|
||||
"Would block sending initial wakeup");
|
||||
return NULL;
|
||||
}
|
||||
else if(rc != 1) {
|
||||
else if(rc != 1)
|
||||
goto scp_recv_error;
|
||||
}
|
||||
|
||||
/* Parse SCP response */
|
||||
session->scpRecv_response_len = 0;
|
||||
@@ -570,9 +567,8 @@ static LIBSSH2_CHANNEL *scp_recv(LIBSSH2_SESSION *session,
|
||||
"Would block waiting to send SCP ACK");
|
||||
return NULL;
|
||||
}
|
||||
else if(rc != 1) {
|
||||
else if(rc != 1)
|
||||
goto scp_recv_error;
|
||||
}
|
||||
|
||||
ssh2_deb((session, LIBSSH2_TRACE_SCP,
|
||||
"mtime = %ld, atime = %ld",
|
||||
@@ -724,9 +720,9 @@ static LIBSSH2_CHANNEL *scp_recv(LIBSSH2_SESSION *session,
|
||||
"Would block sending SCP ACK");
|
||||
return NULL;
|
||||
}
|
||||
else if(rc != 1) {
|
||||
else if(rc != 1)
|
||||
goto scp_recv_error;
|
||||
}
|
||||
|
||||
ssh2_deb((session, LIBSSH2_TRACE_SCP, "mode = 0%lo size = %ld",
|
||||
(unsigned long)session->scpRecv_mode,
|
||||
(long)session->scpRecv_size));
|
||||
@@ -870,10 +866,9 @@ static LIBSSH2_CHANNEL *scp_send(LIBSSH2_SESSION *session,
|
||||
memcpy(&session->scpSend_command[cmd_len], path, path_len);
|
||||
cmd_len += path_len;
|
||||
}
|
||||
else {
|
||||
else
|
||||
cmd_len += shell_quotearg(path, &session->scpSend_command[cmd_len],
|
||||
session->scpSend_command_len - cmd_len);
|
||||
}
|
||||
|
||||
/* the command to exec should _not_ be null-terminated */
|
||||
session->scpSend_command_len = cmd_len;
|
||||
@@ -897,10 +892,9 @@ static LIBSSH2_CHANNEL *scp_send(LIBSSH2_SESSION *session,
|
||||
session->scpSend_command = NULL;
|
||||
session->scpSend_state = ssh2_NB_state_idle;
|
||||
}
|
||||
else {
|
||||
else
|
||||
ssh2_err(session, LIBSSH2_ERROR_EAGAIN,
|
||||
"Would block starting up channel");
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -1012,11 +1006,8 @@ static LIBSSH2_CHANNEL *scp_send(LIBSSH2_SESSION *session,
|
||||
session->scpSend_state = ssh2_NB_state_sent4;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if(session->scpSend_state == ssh2_NB_state_sent2) {
|
||||
session->scpSend_state = ssh2_NB_state_sent4;
|
||||
}
|
||||
}
|
||||
else if(session->scpSend_state == ssh2_NB_state_sent2)
|
||||
session->scpSend_state = ssh2_NB_state_sent4;
|
||||
|
||||
if(session->scpSend_state == ssh2_NB_state_sent4) {
|
||||
/* Send mode, size, and basename */
|
||||
@@ -1105,10 +1096,9 @@ static LIBSSH2_CHANNEL *scp_send(LIBSSH2_SESSION *session,
|
||||
scp_send_empty_channel:
|
||||
/* the code only jumps here as a result of a zero read from channel_read()
|
||||
so we check EOF status to avoid getting stuck in a loop */
|
||||
if(libssh2_channel_eof(session->scpSend_channel)) {
|
||||
if(libssh2_channel_eof(session->scpSend_channel))
|
||||
ssh2_err(session, LIBSSH2_ERROR_SCP_PROTOCOL,
|
||||
"Unexpected channel close");
|
||||
}
|
||||
else
|
||||
return session->scpSend_channel;
|
||||
/* fall-through */
|
||||
|
||||
257
src/session.c
257
src/session.c
@@ -104,9 +104,8 @@ static int banner_receive(LIBSSH2_SESSION *session)
|
||||
|
||||
session->banner_TxRx_state = ssh2_NB_state_created;
|
||||
}
|
||||
else {
|
||||
else
|
||||
banner_len = session->banner_TxRx_total_send;
|
||||
}
|
||||
|
||||
while(banner_len < sizeof(session->banner_TxRx_banner) &&
|
||||
(banner_len == 0 ||
|
||||
@@ -146,9 +145,8 @@ static int banner_receive(LIBSSH2_SESSION *session)
|
||||
return LIBSSH2_ERROR_SOCKET_DISCONNECT;
|
||||
}
|
||||
|
||||
if((c == '\r' || c == '\n') && banner_len == 0) {
|
||||
if((c == '\r' || c == '\n') && banner_len == 0)
|
||||
continue;
|
||||
}
|
||||
|
||||
if(c == '\0') {
|
||||
/* NULLs are not allowed in SSH banners */
|
||||
@@ -162,9 +160,8 @@ static int banner_receive(LIBSSH2_SESSION *session)
|
||||
|
||||
while(banner_len &&
|
||||
(session->banner_TxRx_banner[banner_len - 1] == '\n' ||
|
||||
session->banner_TxRx_banner[banner_len - 1] == '\r')) {
|
||||
session->banner_TxRx_banner[banner_len - 1] == '\r'))
|
||||
banner_len--;
|
||||
}
|
||||
|
||||
/* From this point on, we are done here */
|
||||
session->banner_TxRx_state = ssh2_NB_state_idle;
|
||||
@@ -177,10 +174,10 @@ static int banner_receive(LIBSSH2_SESSION *session)
|
||||
SSH2_FREE(session, session->remote.banner);
|
||||
|
||||
session->remote.banner = SSH2_ALLOC(session, banner_len + 1);
|
||||
if(!session->remote.banner) {
|
||||
if(!session->remote.banner)
|
||||
return ssh2_err(session, LIBSSH2_ERROR_ALLOC,
|
||||
"Error allocating space for remote banner");
|
||||
}
|
||||
|
||||
memcpy(session->remote.banner, session->banner_TxRx_banner, banner_len);
|
||||
session->remote.banner[banner_len] = '\0';
|
||||
ssh2_deb((session, LIBSSH2_TRACE_TRANS, "Received Banner: %s",
|
||||
@@ -307,15 +304,13 @@ static int get_socket_nonblocking(libssh2_socket_t sockfd)
|
||||
{ /* operate on this */
|
||||
#ifdef HAVE_O_NONBLOCK /* most recent unix versions */
|
||||
int flags = fcntl(sockfd, F_GETFL, 0);
|
||||
if(flags == -1) {
|
||||
if(flags == -1)
|
||||
return 1; /* Assume blocking on error */
|
||||
}
|
||||
return (flags & O_NONBLOCK);
|
||||
#elif defined(HAVE_SO_NONBLOCK) /* BeOS */
|
||||
long b;
|
||||
if(getsockopt(sockfd, SOL_SOCKET, SO_NONBLOCK, &b, sizeof(b))) {
|
||||
if(getsockopt(sockfd, SOL_SOCKET, SO_NONBLOCK, &b, sizeof(b)))
|
||||
return 1; /* Assume blocking on error */
|
||||
}
|
||||
return (int)b;
|
||||
#elif defined(SO_STATE) && defined(__VMS) /* VMS TCP/IP Services */
|
||||
size_t sockstat = 0;
|
||||
@@ -323,21 +318,18 @@ static int get_socket_nonblocking(libssh2_socket_t sockfd)
|
||||
size_t size = sizeof(int);
|
||||
callstat = getsockopt(sockfd, SOL_SOCKET, SO_STATE,
|
||||
(char *)&sockstat, &size);
|
||||
if(callstat == -1) {
|
||||
if(callstat == -1)
|
||||
return 0;
|
||||
}
|
||||
if((sockstat & SS_NBIO) != 0) {
|
||||
if((sockstat & SS_NBIO) != 0)
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
#elif defined(_WIN32)
|
||||
unsigned int option_value;
|
||||
socklen_t option_len = sizeof(option_value);
|
||||
|
||||
if(getsockopt(sockfd, SOL_SOCKET, SO_ERROR,
|
||||
(void *)&option_value, &option_len)) {
|
||||
(void *)&option_value, &option_len))
|
||||
return 1; /* Assume blocking on error */
|
||||
}
|
||||
return (int)option_value;
|
||||
#else
|
||||
(void)sockfd;
|
||||
@@ -361,10 +353,9 @@ int libssh2_session_banner_set(LIBSSH2_SESSION *session, const char *banner)
|
||||
return 0;
|
||||
|
||||
session->local.banner = SSH2_ALLOC(session, banner_len + 3);
|
||||
if(!session->local.banner) {
|
||||
if(!session->local.banner)
|
||||
return ssh2_err(session, LIBSSH2_ERROR_ALLOC,
|
||||
"Unable to allocate memory for local banner");
|
||||
}
|
||||
|
||||
memcpy(session->local.banner, banner, banner_len);
|
||||
|
||||
@@ -406,15 +397,12 @@ LIBSSH2_SESSION *libssh2_session_init_ex(LIBSSH2_ALLOC_FUNC(*my_alloc),
|
||||
LIBSSH2_REALLOC_FUNC(*local_realloc) = ssh2_default_realloc;
|
||||
LIBSSH2_SESSION *session;
|
||||
|
||||
if(my_alloc) {
|
||||
if(my_alloc)
|
||||
local_alloc = my_alloc;
|
||||
}
|
||||
if(my_free) {
|
||||
if(my_free)
|
||||
local_free = my_free;
|
||||
}
|
||||
if(my_realloc) {
|
||||
if(my_realloc)
|
||||
local_realloc = my_realloc;
|
||||
}
|
||||
|
||||
session = local_alloc(sizeof(LIBSSH2_SESSION), &abstract);
|
||||
if(session) {
|
||||
@@ -585,16 +573,15 @@ int ssh2_wait_socket(LIBSSH2_SESSION *session, time_t start_time)
|
||||
(seconds_to_next == 0 || ms_to_next > session->api_timeout)) {
|
||||
time_t now = time(NULL);
|
||||
elapsed_ms = (long)(1000 * difftime(now, start_time));
|
||||
if(elapsed_ms > session->api_timeout) {
|
||||
if(elapsed_ms > session->api_timeout)
|
||||
return ssh2_err(session, LIBSSH2_ERROR_TIMEOUT,
|
||||
"API timeout expired");
|
||||
}
|
||||
|
||||
ms_to_next = (session->api_timeout - elapsed_ms);
|
||||
has_timeout = 1;
|
||||
}
|
||||
else if(ms_to_next > 0) {
|
||||
else if(ms_to_next > 0)
|
||||
has_timeout = 1;
|
||||
}
|
||||
else
|
||||
has_timeout = 0;
|
||||
|
||||
@@ -659,10 +646,10 @@ int ssh2_wait_socket(LIBSSH2_SESSION *session, time_t start_time)
|
||||
has_timeout ? &tv : NULL);
|
||||
}
|
||||
#endif
|
||||
if(rc == 0) {
|
||||
if(rc == 0)
|
||||
return ssh2_err(session, LIBSSH2_ERROR_TIMEOUT,
|
||||
"Timed out waiting on socket");
|
||||
}
|
||||
|
||||
if(rc < 0) {
|
||||
/* Profiling tools that use SIGPROF can cause EINTR responses.
|
||||
poll() / select() do not set any descriptor states on EINTR,
|
||||
@@ -690,11 +677,10 @@ static int session_startup(LIBSSH2_SESSION *session, libssh2_socket_t sock)
|
||||
if(session->startup_state == ssh2_NB_state_idle) {
|
||||
ssh2_deb((session, LIBSSH2_TRACE_TRANS,
|
||||
"session_startup for socket %ld", (long)sock));
|
||||
if(LIBSSH2_INVALID_SOCKET == sock) {
|
||||
/* Did we forget something? */
|
||||
if(LIBSSH2_INVALID_SOCKET == sock) /* Did we forget something? */
|
||||
return ssh2_err(session, LIBSSH2_ERROR_BAD_SOCKET,
|
||||
"Bad socket provided");
|
||||
}
|
||||
|
||||
session->socket_fd = sock;
|
||||
|
||||
session->socket_prev_blockstate =
|
||||
@@ -703,11 +689,10 @@ static int session_startup(LIBSSH2_SESSION *session, libssh2_socket_t sock)
|
||||
if(session->socket_prev_blockstate) {
|
||||
/* If in blocking state change to non-blocking */
|
||||
rc = session_nonblock(session->socket_fd, 1);
|
||||
if(rc) {
|
||||
if(rc)
|
||||
return ssh2_err(session, rc,
|
||||
"Failed changing socket's "
|
||||
"blocking state to non-blocking");
|
||||
}
|
||||
}
|
||||
|
||||
session->startup_state = ssh2_NB_state_created;
|
||||
@@ -717,9 +702,9 @@ static int session_startup(LIBSSH2_SESSION *session, libssh2_socket_t sock)
|
||||
rc = banner_send(session);
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN)
|
||||
return rc;
|
||||
else if(rc) {
|
||||
else if(rc)
|
||||
return ssh2_err(session, rc, "Failed sending banner");
|
||||
}
|
||||
|
||||
session->startup_state = ssh2_NB_state_sent;
|
||||
session->banner_TxRx_state = ssh2_NB_state_idle;
|
||||
}
|
||||
@@ -764,10 +749,9 @@ static int session_startup(LIBSSH2_SESSION *session, libssh2_socket_t sock)
|
||||
sizeof("ssh-userauth") + 5 - 1, NULL, 0);
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN)
|
||||
return rc;
|
||||
else if(rc) {
|
||||
else if(rc)
|
||||
return ssh2_err(session, rc,
|
||||
"Unable to ask for ssh-userauth service");
|
||||
}
|
||||
|
||||
session->startup_state = ssh2_NB_state_sent4;
|
||||
}
|
||||
@@ -883,187 +867,138 @@ static int session_free(LIBSSH2_SESSION *session)
|
||||
session->free_state = ssh2_NB_state_sent1;
|
||||
}
|
||||
|
||||
if(session->kex && session->kex->cleanup) {
|
||||
if(session->kex && session->kex->cleanup)
|
||||
session->kex->cleanup(session,
|
||||
&session->startup_key_state.key_state_low);
|
||||
}
|
||||
|
||||
/* hostkey */
|
||||
if(session->hostkey && session->hostkey->dtor) {
|
||||
if(session->hostkey && session->hostkey->dtor)
|
||||
session->hostkey->dtor(session, &session->server_hostkey_abstract);
|
||||
}
|
||||
|
||||
if(session->state & SSH2_STATE_NEWKEYS) {
|
||||
|
||||
/* Client to Server */
|
||||
/* crypt */
|
||||
if(session->local.crypt && session->local.crypt->dtor) {
|
||||
if(session->local.crypt && session->local.crypt->dtor)
|
||||
session->local.crypt->dtor(session,
|
||||
&session->local.crypt_abstract);
|
||||
}
|
||||
/* comp */
|
||||
if(session->local.comp && session->local.comp->dtor) {
|
||||
if(session->local.comp && session->local.comp->dtor)
|
||||
session->local.comp->dtor(session, 1,
|
||||
&session->local.comp_abstract);
|
||||
}
|
||||
/* mac */
|
||||
if(session->local.mac && session->local.mac->dtor) {
|
||||
if(session->local.mac && session->local.mac->dtor)
|
||||
session->local.mac->dtor(session, &session->local.mac_abstract);
|
||||
}
|
||||
|
||||
/* Server to Client */
|
||||
/* crypt */
|
||||
if(session->remote.crypt && session->remote.crypt->dtor) {
|
||||
if(session->remote.crypt && session->remote.crypt->dtor)
|
||||
session->remote.crypt->dtor(session,
|
||||
&session->remote.crypt_abstract);
|
||||
}
|
||||
/* comp */
|
||||
if(session->remote.comp && session->remote.comp->dtor) {
|
||||
if(session->remote.comp && session->remote.comp->dtor)
|
||||
session->remote.comp->dtor(session, 0,
|
||||
&session->remote.comp_abstract);
|
||||
}
|
||||
/* mac */
|
||||
if(session->remote.mac && session->remote.mac->dtor) {
|
||||
if(session->remote.mac && session->remote.mac->dtor)
|
||||
session->remote.mac->dtor(session, &session->remote.mac_abstract);
|
||||
}
|
||||
|
||||
/* session_id */
|
||||
if(session->session_id) {
|
||||
if(session->session_id)
|
||||
SSH2_FREE(session, session->session_id);
|
||||
}
|
||||
}
|
||||
|
||||
/* Free banner(s) */
|
||||
if(session->remote.banner) {
|
||||
if(session->remote.banner)
|
||||
SSH2_FREE(session, session->remote.banner);
|
||||
}
|
||||
if(session->local.banner) {
|
||||
if(session->local.banner)
|
||||
SSH2_FREE(session, session->local.banner);
|
||||
}
|
||||
|
||||
/* Free preference(s) */
|
||||
if(session->kex_prefs) {
|
||||
if(session->kex_prefs)
|
||||
SSH2_FREE(session, session->kex_prefs);
|
||||
}
|
||||
if(session->hostkey_prefs) {
|
||||
if(session->hostkey_prefs)
|
||||
SSH2_FREE(session, session->hostkey_prefs);
|
||||
}
|
||||
|
||||
if(session->local.kexinit) {
|
||||
if(session->local.kexinit)
|
||||
SSH2_FREE(session, session->local.kexinit);
|
||||
}
|
||||
if(session->local.crypt_prefs) {
|
||||
if(session->local.crypt_prefs)
|
||||
SSH2_FREE(session, session->local.crypt_prefs);
|
||||
}
|
||||
if(session->local.mac_prefs) {
|
||||
if(session->local.mac_prefs)
|
||||
SSH2_FREE(session, session->local.mac_prefs);
|
||||
}
|
||||
if(session->local.comp_prefs) {
|
||||
if(session->local.comp_prefs)
|
||||
SSH2_FREE(session, session->local.comp_prefs);
|
||||
}
|
||||
if(session->local.lang_prefs) {
|
||||
if(session->local.lang_prefs)
|
||||
SSH2_FREE(session, session->local.lang_prefs);
|
||||
}
|
||||
|
||||
if(session->remote.kexinit) {
|
||||
if(session->remote.kexinit)
|
||||
SSH2_FREE(session, session->remote.kexinit);
|
||||
}
|
||||
if(session->remote.crypt_prefs) {
|
||||
if(session->remote.crypt_prefs)
|
||||
SSH2_FREE(session, session->remote.crypt_prefs);
|
||||
}
|
||||
if(session->remote.mac_prefs) {
|
||||
if(session->remote.mac_prefs)
|
||||
SSH2_FREE(session, session->remote.mac_prefs);
|
||||
}
|
||||
if(session->remote.comp_prefs) {
|
||||
if(session->remote.comp_prefs)
|
||||
SSH2_FREE(session, session->remote.comp_prefs);
|
||||
}
|
||||
if(session->remote.lang_prefs) {
|
||||
if(session->remote.lang_prefs)
|
||||
SSH2_FREE(session, session->remote.lang_prefs);
|
||||
}
|
||||
if(session->server_sign_algorithms) {
|
||||
if(session->server_sign_algorithms)
|
||||
SSH2_FREE(session, session->server_sign_algorithms);
|
||||
}
|
||||
if(session->sign_algo_prefs) {
|
||||
if(session->sign_algo_prefs)
|
||||
SSH2_FREE(session, session->sign_algo_prefs);
|
||||
}
|
||||
|
||||
/*
|
||||
* Make sure all memory used in the state variables are free
|
||||
*/
|
||||
if(session->kexinit_data) {
|
||||
if(session->kexinit_data)
|
||||
SSH2_FREE(session, session->kexinit_data);
|
||||
}
|
||||
if(session->startup_data) {
|
||||
if(session->startup_data)
|
||||
SSH2_FREE(session, session->startup_data);
|
||||
}
|
||||
if(session->userauth_list_data) {
|
||||
if(session->userauth_list_data)
|
||||
SSH2_FREE(session, session->userauth_list_data);
|
||||
}
|
||||
if(session->userauth_banner) {
|
||||
if(session->userauth_banner)
|
||||
SSH2_FREE(session, session->userauth_banner);
|
||||
}
|
||||
if(session->userauth_pswd_data) {
|
||||
if(session->userauth_pswd_data)
|
||||
SSH2_FREE(session, session->userauth_pswd_data);
|
||||
}
|
||||
if(session->userauth_pswd_newpw) {
|
||||
if(session->userauth_pswd_newpw)
|
||||
SSH2_FREE(session, session->userauth_pswd_newpw);
|
||||
}
|
||||
if(session->userauth_host_packet) {
|
||||
if(session->userauth_host_packet)
|
||||
SSH2_FREE(session, session->userauth_host_packet);
|
||||
}
|
||||
if(session->userauth_host_method) {
|
||||
if(session->userauth_host_method)
|
||||
SSH2_FREE(session, session->userauth_host_method);
|
||||
}
|
||||
if(session->userauth_host_data) {
|
||||
if(session->userauth_host_data)
|
||||
SSH2_FREE(session, session->userauth_host_data);
|
||||
}
|
||||
if(session->userauth_pblc_data) {
|
||||
if(session->userauth_pblc_data)
|
||||
SSH2_FREE(session, session->userauth_pblc_data);
|
||||
}
|
||||
if(session->userauth_pblc_packet) {
|
||||
if(session->userauth_pblc_packet)
|
||||
SSH2_FREE(session, session->userauth_pblc_packet);
|
||||
}
|
||||
if(session->userauth_pblc_method) {
|
||||
if(session->userauth_pblc_method)
|
||||
SSH2_FREE(session, session->userauth_pblc_method);
|
||||
}
|
||||
if(session->userauth_kybd_data) {
|
||||
if(session->userauth_kybd_data)
|
||||
SSH2_FREE(session, session->userauth_kybd_data);
|
||||
}
|
||||
if(session->userauth_kybd_packet) {
|
||||
if(session->userauth_kybd_packet)
|
||||
SSH2_FREE(session, session->userauth_kybd_packet);
|
||||
}
|
||||
if(session->userauth_kybd_auth_instruction) {
|
||||
if(session->userauth_kybd_auth_instruction)
|
||||
SSH2_FREE(session, session->userauth_kybd_auth_instruction);
|
||||
}
|
||||
if(session->open_packet) {
|
||||
if(session->open_packet)
|
||||
SSH2_FREE(session, session->open_packet);
|
||||
}
|
||||
if(session->open_data) {
|
||||
if(session->open_data)
|
||||
SSH2_FREE(session, session->open_data);
|
||||
}
|
||||
if(session->direct_message) {
|
||||
if(session->direct_message)
|
||||
SSH2_FREE(session, session->direct_message);
|
||||
}
|
||||
if(session->fwdLstn_packet) {
|
||||
if(session->fwdLstn_packet)
|
||||
SSH2_FREE(session, session->fwdLstn_packet);
|
||||
}
|
||||
if(session->pkeyInit_data) {
|
||||
if(session->pkeyInit_data)
|
||||
SSH2_FREE(session, session->pkeyInit_data);
|
||||
}
|
||||
if(session->scpRecv_command) {
|
||||
if(session->scpRecv_command)
|
||||
SSH2_FREE(session, session->scpRecv_command);
|
||||
}
|
||||
if(session->scpSend_command) {
|
||||
if(session->scpSend_command)
|
||||
SSH2_FREE(session, session->scpSend_command);
|
||||
}
|
||||
if(session->sftpInit_sftp) {
|
||||
if(session->sftpInit_sftp)
|
||||
SSH2_FREE(session, session->sftpInit_sftp);
|
||||
}
|
||||
|
||||
/* Free payload buffer */
|
||||
if(session->packet.total_num) {
|
||||
if(session->packet.total_num)
|
||||
SSH2_FREE(session, session->packet.payload);
|
||||
}
|
||||
|
||||
/* Cleanup all remaining packets */
|
||||
/* !checksrc! disable EQUALSNULL 1 */
|
||||
@@ -1085,21 +1020,17 @@ static int session_free(LIBSSH2_SESSION *session)
|
||||
if(session->socket_prev_blockstate) {
|
||||
/* if the socket was previously blocking, put it back so */
|
||||
rc = session_nonblock(session->socket_fd, 0);
|
||||
if(rc) {
|
||||
if(rc)
|
||||
ssh2_deb((session, LIBSSH2_TRACE_TRANS,
|
||||
"unable to reset socket's blocking state"));
|
||||
}
|
||||
}
|
||||
|
||||
if(session->server_hostkey) {
|
||||
if(session->server_hostkey)
|
||||
SSH2_FREE(session, session->server_hostkey);
|
||||
}
|
||||
|
||||
/* error string */
|
||||
if(session->err_msg &&
|
||||
((session->err_flags & SSH2_ERR_FLAG_DUP) != 0)) {
|
||||
if(session->err_msg && ((session->err_flags & SSH2_ERR_FLAG_DUP) != 0))
|
||||
SSH2_FREE(session, (char *)SSH2_UNCONST(session->err_msg));
|
||||
}
|
||||
|
||||
SSH2_FREE(session, session);
|
||||
|
||||
@@ -1270,17 +1201,14 @@ int libssh2_session_last_error(LIBSSH2_SESSION *session, char **errmsg,
|
||||
if(errmsg) {
|
||||
if(want_buf) {
|
||||
*errmsg = SSH2_ALLOC(session, 1);
|
||||
if(*errmsg) {
|
||||
if(*errmsg)
|
||||
**errmsg = 0;
|
||||
}
|
||||
}
|
||||
else {
|
||||
else
|
||||
*errmsg = (char *)SSH2_UNCONST("");
|
||||
}
|
||||
}
|
||||
if(errmsg_len) {
|
||||
if(errmsg_len)
|
||||
*errmsg_len = 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1301,9 +1229,8 @@ int libssh2_session_last_error(LIBSSH2_SESSION *session, char **errmsg,
|
||||
*errmsg = (char *)SSH2_UNCONST(error);
|
||||
}
|
||||
|
||||
if(errmsg_len) {
|
||||
if(errmsg_len)
|
||||
*errmsg_len = (int)msglen;
|
||||
}
|
||||
|
||||
return session->err_code;
|
||||
}
|
||||
@@ -1408,9 +1335,9 @@ long libssh2_session_get_timeout(LIBSSH2_SESSION *session)
|
||||
*/
|
||||
void libssh2_session_set_read_timeout(LIBSSH2_SESSION *session, long timeout)
|
||||
{
|
||||
if(timeout <= 0) {
|
||||
if(timeout <= 0)
|
||||
timeout = SSH2_DEFAULT_READ_TIMEOUT;
|
||||
}
|
||||
|
||||
session->packet_read_timeout = timeout;
|
||||
}
|
||||
|
||||
@@ -1441,10 +1368,9 @@ int libssh2_poll_channel_read(LIBSSH2_CHANNEL *channel, int extended)
|
||||
packet = ssh2_list_first(&session->packets);
|
||||
|
||||
while(packet) {
|
||||
if(packet->data_len < 5) {
|
||||
if(packet->data_len < 5)
|
||||
return ssh2_err(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL,
|
||||
"Packet too small");
|
||||
}
|
||||
|
||||
if(channel->local.id == ssh2_ntohu32(packet->data + 1)) {
|
||||
if(extended == 1 &&
|
||||
@@ -1682,9 +1608,8 @@ int libssh2_poll(LIBSSH2_POLLFD *fds, unsigned int nfds, long timeout)
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(fds[i].revents) {
|
||||
if(fds[i].revents)
|
||||
active_fds++;
|
||||
}
|
||||
}
|
||||
|
||||
if(active_fds) {
|
||||
@@ -1709,9 +1634,8 @@ int libssh2_poll(LIBSSH2_POLLFD *fds, unsigned int nfds, long timeout)
|
||||
case LIBSSH2_POLLFD_SOCKET:
|
||||
fds[i].revents = sockets[i].revents;
|
||||
sockets[i].revents = 0; /* Be nice in case we loop again */
|
||||
if(fds[i].revents) {
|
||||
if(fds[i].revents)
|
||||
active_fds++;
|
||||
}
|
||||
break;
|
||||
case LIBSSH2_POLLFD_CHANNEL:
|
||||
if(sockets[i].events & POLLIN) {
|
||||
@@ -1768,15 +1692,12 @@ int libssh2_poll(LIBSSH2_POLLFD *fds, unsigned int nfds, long timeout)
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wsign-conversion"
|
||||
#endif
|
||||
if(FD_ISSET(fds[i].fd.socket, &rfds)) {
|
||||
if(FD_ISSET(fds[i].fd.socket, &rfds))
|
||||
fds[i].revents |= LIBSSH2_POLLFD_POLLIN;
|
||||
}
|
||||
if(FD_ISSET(fds[i].fd.socket, &wfds)) {
|
||||
if(FD_ISSET(fds[i].fd.socket, &wfds))
|
||||
fds[i].revents |= LIBSSH2_POLLFD_POLLOUT;
|
||||
}
|
||||
if(fds[i].revents) {
|
||||
if(fds[i].revents)
|
||||
active_fds++;
|
||||
}
|
||||
#if defined(__GNUC__) || defined(__clang__)
|
||||
#pragma GCC diagnostic pop
|
||||
#endif
|
||||
|
||||
321
src/sftp.c
321
src/sftp.c
File diff suppressed because it is too large
Load Diff
104
src/transport.c
104
src/transport.c
@@ -62,10 +62,8 @@ static void debugdump(LIBSSH2_SESSION *session,
|
||||
size_t used;
|
||||
static const char *hex_chars = "0123456789ABCDEF";
|
||||
|
||||
if(!(session->showmask & LIBSSH2_TRACE_TRANS)) {
|
||||
/* not asked for, bail out */
|
||||
return;
|
||||
}
|
||||
if(!(session->showmask & LIBSSH2_TRACE_TRANS))
|
||||
return; /* not asked for, bail out */
|
||||
|
||||
used = snprintf(buffer, sizeof(buffer), "=> %s (%lu bytes)\n",
|
||||
desc, (unsigned long)size);
|
||||
@@ -188,9 +186,8 @@ static int fullpacket(LIBSSH2_SESSION *session, int encrypted /* 1 or 0 */)
|
||||
memset(macbuf, '\0', sizeof(macbuf));
|
||||
|
||||
if(!encrypted || (!CRYPT_FLAG_R(session, REQUIRES_FULL_PACKET) &&
|
||||
!CRYPT_FLAG_R(session, INTEGRATED_MAC))) {
|
||||
!CRYPT_FLAG_R(session, INTEGRATED_MAC)))
|
||||
remote_mac = session->remote.mac;
|
||||
}
|
||||
|
||||
if(session->fullpacket_state == ssh2_NB_state_idle) {
|
||||
session->fullpacket_macstate = SSH2_MAC_CONFIRMED;
|
||||
@@ -201,23 +198,19 @@ static int fullpacket(LIBSSH2_SESSION *session, int encrypted /* 1 or 0 */)
|
||||
/* Calculate MAC hash */
|
||||
int etm = remote_mac->etm;
|
||||
size_t mac_len = remote_mac->mac_len;
|
||||
if(etm) {
|
||||
/* store hash here */
|
||||
if(etm) /* store hash here */
|
||||
remote_mac->hash(session, macbuf,
|
||||
session->remote.seqno,
|
||||
p->payload, p->total_num - mac_len,
|
||||
NULL, 0,
|
||||
&session->remote.mac_abstract);
|
||||
}
|
||||
else {
|
||||
/* store hash here */
|
||||
else /* store hash here */
|
||||
remote_mac->hash(session, macbuf,
|
||||
session->remote.seqno,
|
||||
p->init, 5,
|
||||
p->payload,
|
||||
session->fullpacket_payload_len,
|
||||
&session->remote.mac_abstract);
|
||||
}
|
||||
|
||||
/* Compare the calculated hash with the MAC we read from
|
||||
* the network. The read one is at the end of the payload
|
||||
@@ -244,16 +237,14 @@ static int fullpacket(LIBSSH2_SESSION *session, int encrypted /* 1 or 0 */)
|
||||
|
||||
rc = decrypt(session, p->payload + 4,
|
||||
first_block, blocksize, FIRST_BLOCK);
|
||||
if(rc) {
|
||||
if(rc)
|
||||
return rc;
|
||||
}
|
||||
|
||||
/* we need buffer for decrypt */
|
||||
decrypt_size = p->total_num - mac_len - 4;
|
||||
decrypt_buffer = SSH2_ALLOC(session, decrypt_size);
|
||||
if(!decrypt_buffer) {
|
||||
if(!decrypt_buffer)
|
||||
return LIBSSH2_ERROR_ALLOC;
|
||||
}
|
||||
|
||||
/* grab padding length and copy anything else
|
||||
into target buffer */
|
||||
@@ -264,9 +255,8 @@ static int fullpacket(LIBSSH2_SESSION *session, int encrypted /* 1 or 0 */)
|
||||
return LIBSSH2_ERROR_PROTO;
|
||||
}
|
||||
|
||||
if(blocksize > 1) {
|
||||
if(blocksize > 1)
|
||||
memcpy(decrypt_buffer, first_block + 1, blocksize - 1);
|
||||
}
|
||||
|
||||
/* decrypt all other blocks packet */
|
||||
if(blocksize < decrypt_size) {
|
||||
@@ -284,10 +274,9 @@ static int fullpacket(LIBSSH2_SESSION *session, int encrypted /* 1 or 0 */)
|
||||
p->payload = decrypt_buffer;
|
||||
}
|
||||
}
|
||||
else if(encrypted && CRYPT_FLAG_R(session, REQUIRES_FULL_PACKET)) {
|
||||
else if(encrypted && CRYPT_FLAG_R(session, REQUIRES_FULL_PACKET))
|
||||
/* etm trim off padding byte from payload */
|
||||
memmove(p->payload, &p->payload[1], p->packet_length - 1);
|
||||
}
|
||||
|
||||
session->remote.seqno++;
|
||||
|
||||
@@ -347,9 +336,8 @@ static int fullpacket(LIBSSH2_SESSION *session, int encrypted /* 1 or 0 */)
|
||||
session->fullpacket_state = ssh2_NB_state_idle;
|
||||
|
||||
if(session->kex_strict &&
|
||||
session->fullpacket_packet_type == SSH_MSG_NEWKEYS) {
|
||||
session->fullpacket_packet_type == SSH_MSG_NEWKEYS)
|
||||
session->remote.seqno = 0;
|
||||
}
|
||||
|
||||
return session->fullpacket_packet_type;
|
||||
}
|
||||
@@ -428,13 +416,11 @@ int ssh2_transport_read(LIBSSH2_SESSION *session)
|
||||
|
||||
do {
|
||||
int etm;
|
||||
if(session->socket_state == SSH2_SOCKET_DISCONNECTED) {
|
||||
if(session->socket_state == SSH2_SOCKET_DISCONNECTED)
|
||||
return LIBSSH2_ERROR_SOCKET_DISCONNECT;
|
||||
}
|
||||
|
||||
if(session->state & SSH2_STATE_NEWKEYS) {
|
||||
if(session->state & SSH2_STATE_NEWKEYS)
|
||||
blocksize = session->remote.crypt->blocksize;
|
||||
}
|
||||
else {
|
||||
encrypted = 0; /* not encrypted */
|
||||
blocksize = 5; /* not strictly true, but we can use 5 here to
|
||||
@@ -442,12 +428,10 @@ int ssh2_transport_read(LIBSSH2_SESSION *session)
|
||||
}
|
||||
|
||||
if(encrypted) {
|
||||
if(CRYPT_FLAG_R(session, REQUIRES_FULL_PACKET)) {
|
||||
if(CRYPT_FLAG_R(session, REQUIRES_FULL_PACKET))
|
||||
auth_len = session->remote.crypt->auth_len;
|
||||
}
|
||||
else {
|
||||
else
|
||||
remote_mac = session->remote.mac;
|
||||
}
|
||||
}
|
||||
|
||||
etm = encrypted && remote_mac ? remote_mac->etm : 0;
|
||||
@@ -479,10 +463,8 @@ int ssh2_transport_read(LIBSSH2_SESSION *session)
|
||||
p->readidx = 0;
|
||||
p->writeidx = remainbuf;
|
||||
}
|
||||
else {
|
||||
/* nothing to move, zero the indexes */
|
||||
else /* nothing to move, zero the indexes */
|
||||
p->readidx = p->writeidx = 0;
|
||||
}
|
||||
|
||||
/* now read a big chunk from the network into the temp buffer */
|
||||
nread = SSH2_RECV(session, &p->buf[remainbuf],
|
||||
@@ -577,9 +559,8 @@ int ssh2_transport_read(LIBSSH2_SESSION *session)
|
||||
/* first decrypted block */
|
||||
rc = decrypt(session, &p->buf[p->readidx],
|
||||
block, blocksize, FIRST_BLOCK);
|
||||
if(rc != LIBSSH2_ERROR_NONE) {
|
||||
if(rc != LIBSSH2_ERROR_NONE)
|
||||
return rc;
|
||||
}
|
||||
/* Save the first 5 bytes of the decrypted package, to be
|
||||
used in the hash calculation later down.
|
||||
This is ignored in the INTEGRATED_MAC case. */
|
||||
@@ -601,12 +582,10 @@ int ssh2_transport_read(LIBSSH2_SESSION *session)
|
||||
}
|
||||
|
||||
if(!encrypted || !CRYPT_FLAG_R(session, REQUIRES_FULL_PACKET)) {
|
||||
if(p->packet_length < 1) {
|
||||
if(p->packet_length < 1)
|
||||
return LIBSSH2_ERROR_DECRYPT;
|
||||
}
|
||||
else if(p->packet_length > LIBSSH2_PACKET_MAXPAYLOAD) {
|
||||
else if(p->packet_length > LIBSSH2_PACKET_MAXPAYLOAD)
|
||||
return LIBSSH2_ERROR_OUT_OF_BOUNDARY;
|
||||
}
|
||||
|
||||
if(etm) {
|
||||
/* do not know what padding is until we decrypt the full
|
||||
@@ -624,9 +603,8 @@ int ssh2_transport_read(LIBSSH2_SESSION *session)
|
||||
immediately following) until after the entire packet is
|
||||
authenticated, so this is safe. */
|
||||
p->padding_length = block[4];
|
||||
if(p->padding_length > p->packet_length - 1) {
|
||||
if(p->padding_length > p->packet_length - 1)
|
||||
return LIBSSH2_ERROR_DECRYPT;
|
||||
}
|
||||
|
||||
/* total_num is the number of bytes following the initial
|
||||
(5 bytes) packet length and padding length fields */
|
||||
@@ -645,12 +623,10 @@ int ssh2_transport_read(LIBSSH2_SESSION *session)
|
||||
total_num = 4;
|
||||
|
||||
p->packet_length = ssh2_ntohu32(block);
|
||||
if(p->packet_length < 1) {
|
||||
if(p->packet_length < 1)
|
||||
return LIBSSH2_ERROR_DECRYPT;
|
||||
}
|
||||
else if(p->packet_length > LIBSSH2_PACKET_MAXPAYLOAD) {
|
||||
else if(p->packet_length > LIBSSH2_PACKET_MAXPAYLOAD)
|
||||
return LIBSSH2_ERROR_OUT_OF_BOUNDARY;
|
||||
}
|
||||
|
||||
/* total_num may include size field, however due to existing
|
||||
* logic it needs to be removed after the entire packet is read
|
||||
@@ -672,16 +648,14 @@ int ssh2_transport_read(LIBSSH2_SESSION *session)
|
||||
* or less (including length, padding length, payload,
|
||||
* padding, and MAC.)."
|
||||
*/
|
||||
if(total_num > LIBSSH2_PACKET_MAXPAYLOAD || total_num == 0) {
|
||||
if(total_num > LIBSSH2_PACKET_MAXPAYLOAD || total_num == 0)
|
||||
return LIBSSH2_ERROR_OUT_OF_BOUNDARY;
|
||||
}
|
||||
|
||||
/* Get a packet handle put data into. We get one to
|
||||
hold all data, including padding and MAC. */
|
||||
p->payload = SSH2_ALLOC(session, total_num);
|
||||
if(!p->payload) {
|
||||
if(!p->payload)
|
||||
return LIBSSH2_ERROR_ALLOC;
|
||||
}
|
||||
p->total_num = total_num;
|
||||
/* init write pointer to start of payload buffer */
|
||||
p->wptr = p->payload;
|
||||
@@ -725,11 +699,10 @@ int ssh2_transport_read(LIBSSH2_SESSION *session)
|
||||
package */
|
||||
remainpack = p->total_num - p->data_num;
|
||||
|
||||
if(numbytes > remainpack) {
|
||||
if(numbytes > remainpack)
|
||||
/* if we have more data in the buffer than what is going into this
|
||||
particular packet, we limit this round to this packet only */
|
||||
numbytes = remainpack;
|
||||
}
|
||||
|
||||
if(encrypted && CRYPT_FLAG_R(session, REQUIRES_FULL_PACKET)) {
|
||||
if(numbytes < remainpack) {
|
||||
@@ -788,10 +761,9 @@ int ssh2_transport_read(LIBSSH2_SESSION *session)
|
||||
firstlast = MIDDLE_BLOCK;
|
||||
}
|
||||
}
|
||||
else {
|
||||
/* unencrypted data should not be decrypted at all */
|
||||
else /* unencrypted data should not be decrypted at all */
|
||||
numdecrypt = 0;
|
||||
}
|
||||
|
||||
assert(numdecrypt >= 0);
|
||||
|
||||
/* if there are bytes to decrypt, do that */
|
||||
@@ -823,9 +795,8 @@ int ssh2_transport_read(LIBSSH2_SESSION *session)
|
||||
decrypted */
|
||||
p->padding_length = p->wptr[0];
|
||||
|
||||
if(p->padding_length > p->packet_length - 1) {
|
||||
if(p->padding_length > p->packet_length - 1)
|
||||
return LIBSSH2_ERROR_DECRYPT;
|
||||
}
|
||||
}
|
||||
else {
|
||||
rc = decrypt(session, &p->buf[p->readidx], p->wptr, numdecrypt,
|
||||
@@ -852,9 +823,8 @@ int ssh2_transport_read(LIBSSH2_SESSION *session)
|
||||
copy them as-is to the target buffer */
|
||||
if(numbytes > 0) {
|
||||
|
||||
if((size_t)numbytes <= (p->total_num - (p->wptr - p->payload))) {
|
||||
if((size_t)numbytes <= (p->total_num - (p->wptr - p->payload)))
|
||||
memcpy(p->wptr, &p->buf[p->readidx], numbytes);
|
||||
}
|
||||
else {
|
||||
if(p->payload)
|
||||
SSH2_FREE(session, p->payload);
|
||||
@@ -1051,12 +1021,10 @@ int ssh2_transport_send(LIBSSH2_SESSION *session,
|
||||
encrypted = (session->state & SSH2_STATE_NEWKEYS) ? 1 : 0;
|
||||
|
||||
if(encrypted && session->local.crypt &&
|
||||
CRYPT_FLAG_L(session, REQUIRES_FULL_PACKET)) {
|
||||
CRYPT_FLAG_L(session, REQUIRES_FULL_PACKET))
|
||||
auth_len = session->local.crypt->auth_len;
|
||||
}
|
||||
else {
|
||||
else
|
||||
local_mac = session->local.mac;
|
||||
}
|
||||
|
||||
etm = encrypted && local_mac ? local_mac->etm : 0;
|
||||
|
||||
@@ -1136,9 +1104,8 @@ int ssh2_transport_send(LIBSSH2_SESSION *session,
|
||||
/* if the padding becomes too small we add another blocksize worth
|
||||
of it (taken from the original libssh2 where it did not have any
|
||||
real explanation) */
|
||||
if(padding_length < 4) {
|
||||
if(padding_length < 4)
|
||||
padding_length += blocksize;
|
||||
}
|
||||
#ifdef LIBSSH2_RANDOM_PADDING
|
||||
/* FIXME: we can add padding here, but that also makes the packets
|
||||
bigger etc */
|
||||
@@ -1165,10 +1132,9 @@ int ssh2_transport_send(LIBSSH2_SESSION *session,
|
||||
p->outbuf[4] = (unsigned char)padding_length;
|
||||
|
||||
/* fill the padding area with random junk */
|
||||
if(ssh2_random(p->outbuf + 5 + data_len, padding_length)) {
|
||||
if(ssh2_random(p->outbuf + 5 + data_len, padding_length))
|
||||
return ssh2_err(session, LIBSSH2_ERROR_RANDGEN,
|
||||
"Unable to get random bytes for packet padding");
|
||||
}
|
||||
|
||||
if(encrypted) {
|
||||
size_t i;
|
||||
@@ -1194,9 +1160,8 @@ int ssh2_transport_send(LIBSSH2_SESSION *session,
|
||||
p->outbuf,
|
||||
packet_length,
|
||||
&session->local.crypt_abstract,
|
||||
0)) {
|
||||
0))
|
||||
return LIBSSH2_ERROR_ENCRYPT;
|
||||
}
|
||||
}
|
||||
else {
|
||||
/* Encrypt the whole packet data, one block size at a time.
|
||||
@@ -1269,9 +1234,8 @@ int ssh2_transport_send(LIBSSH2_SESSION *session,
|
||||
|
||||
session->local.seqno++;
|
||||
|
||||
if(session->kex_strict && data[0] == SSH_MSG_NEWKEYS) {
|
||||
if(session->kex_strict && data[0] == SSH_MSG_NEWKEYS)
|
||||
session->local.seqno = 0;
|
||||
}
|
||||
|
||||
ret = SSH2_SEND(session, p->outbuf, total_length,
|
||||
SSH2_SOCKET_SEND_FLAGS(session));
|
||||
|
||||
164
src/userauth.c
164
src/userauth.c
@@ -99,9 +99,8 @@ static char *userauth_list(LIBSSH2_SESSION *session, const char *username,
|
||||
|
||||
session->userauth_list_data_len = username_len + 27;
|
||||
|
||||
if(session->userauth_list_data) {
|
||||
if(session->userauth_list_data)
|
||||
SSH2_FREE(session, session->userauth_list_data);
|
||||
}
|
||||
|
||||
s = session->userauth_list_data =
|
||||
SSH2_ALLOC(session, session->userauth_list_data_len);
|
||||
@@ -176,9 +175,8 @@ static char *userauth_list(LIBSSH2_SESSION *session, const char *username,
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if(session->userauth_banner) {
|
||||
if(session->userauth_banner)
|
||||
SSH2_FREE(session, session->userauth_banner);
|
||||
}
|
||||
|
||||
session->userauth_banner = SSH2_ALLOC(session, banner_len + 1);
|
||||
if(!session->userauth_banner) {
|
||||
@@ -275,10 +273,9 @@ int libssh2_userauth_banner(LIBSSH2_SESSION *session, char **banner)
|
||||
if(!session)
|
||||
return LIBSSH2_ERROR_MISSING_USERAUTH_BANNER;
|
||||
|
||||
if(!session->userauth_banner) {
|
||||
if(!session->userauth_banner)
|
||||
return ssh2_err(session, LIBSSH2_ERROR_MISSING_USERAUTH_BANNER,
|
||||
"Missing userauth banner");
|
||||
}
|
||||
|
||||
if(banner)
|
||||
*banner = session->userauth_banner;
|
||||
@@ -323,10 +320,9 @@ static int userauth_password(LIBSSH2_SESSION *session,
|
||||
* 40 = packet_type(1) + username_len(4) + service_len(4) +
|
||||
* service(14)"ssh-connection" + method_len(4) + method(8)"password" +
|
||||
* chgpwdbool(1) + password_len(4) */
|
||||
if(username_len > UINT32_MAX - 40) {
|
||||
if(username_len > UINT32_MAX - 40)
|
||||
return ssh2_err(session, LIBSSH2_ERROR_PROTO,
|
||||
"username_len out of bounds");
|
||||
}
|
||||
|
||||
session->userauth_pswd_data_len = username_len + 40;
|
||||
|
||||
@@ -337,11 +333,10 @@ static int userauth_password(LIBSSH2_SESSION *session,
|
||||
struct */
|
||||
s = session->userauth_pswd_data =
|
||||
SSH2_ALLOC(session, session->userauth_pswd_data_len);
|
||||
if(!session->userauth_pswd_data) {
|
||||
if(!session->userauth_pswd_data)
|
||||
return ssh2_err(session, LIBSSH2_ERROR_ALLOC,
|
||||
"Unable to allocate memory for "
|
||||
"userauth-password request");
|
||||
}
|
||||
|
||||
*(s++) = SSH_MSG_USERAUTH_REQUEST;
|
||||
ssh2_store_str(&s, username, username_len);
|
||||
@@ -361,10 +356,9 @@ static int userauth_password(LIBSSH2_SESSION *session,
|
||||
rc = ssh2_transport_send(session, session->userauth_pswd_data,
|
||||
session->userauth_pswd_data_len,
|
||||
password, password_len);
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN)
|
||||
return ssh2_err(session, LIBSSH2_ERROR_EAGAIN,
|
||||
"Would block writing password request");
|
||||
}
|
||||
|
||||
/* now free the sent packet */
|
||||
SSH2_FREE(session, session->userauth_pswd_data);
|
||||
@@ -456,12 +450,11 @@ password_response:
|
||||
&session->userauth_pswd_newpw,
|
||||
&session->userauth_pswd_newpw_len,
|
||||
&session->abstract);
|
||||
if(!session->userauth_pswd_newpw) {
|
||||
if(!session->userauth_pswd_newpw)
|
||||
return ssh2_err(session,
|
||||
LIBSSH2_ERROR_PASSWORD_EXPIRED,
|
||||
"Password expired, and "
|
||||
"callback failed");
|
||||
}
|
||||
|
||||
/* basic data_len + newpw_len(4) */
|
||||
if(username_len <= UINT32_MAX - password_len - 44) {
|
||||
@@ -506,10 +499,9 @@ password_response:
|
||||
(const unsigned char *)
|
||||
session->userauth_pswd_newpw,
|
||||
session->userauth_pswd_newpw_len);
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN)
|
||||
return ssh2_err(session, LIBSSH2_ERROR_EAGAIN,
|
||||
"Would block waiting");
|
||||
}
|
||||
|
||||
/* free the allocated packets again */
|
||||
SSH2_FREE(session, session->userauth_pswd_data);
|
||||
@@ -517,11 +509,10 @@ password_response:
|
||||
SSH2_FREE(session, session->userauth_pswd_newpw);
|
||||
session->userauth_pswd_newpw = NULL;
|
||||
|
||||
if(rc) {
|
||||
if(rc)
|
||||
return ssh2_err(session, LIBSSH2_ERROR_SOCKET_SEND,
|
||||
"Unable to send userauth "
|
||||
"password-change request");
|
||||
}
|
||||
|
||||
/*
|
||||
* Ugliest use of goto ever. Blame it on the
|
||||
@@ -579,16 +570,14 @@ static int memory_read_publickey(LIBSSH2_SESSION *session,
|
||||
size_t pubkey_len = pubkeyfiledata_len;
|
||||
size_t tmp_len;
|
||||
|
||||
if(pubkeyfiledata_len <= 1) {
|
||||
if(pubkeyfiledata_len <= 1)
|
||||
return ssh2_err(session, LIBSSH2_ERROR_FILE,
|
||||
"Invalid data in public key file");
|
||||
}
|
||||
|
||||
pubkey = SSH2_ALLOC(session, pubkeyfiledata_len);
|
||||
if(!pubkey) {
|
||||
if(!pubkey)
|
||||
return ssh2_err(session, LIBSSH2_ERROR_ALLOC,
|
||||
"Unable to allocate memory for public key data");
|
||||
}
|
||||
|
||||
memcpy(pubkey, pubkeyfiledata, pubkeyfiledata_len);
|
||||
|
||||
@@ -614,10 +603,9 @@ static int memory_read_publickey(LIBSSH2_SESSION *session,
|
||||
sp1++;
|
||||
|
||||
sp2 = memchr(sp1, ' ', pubkey_len - (sp1 - pubkey));
|
||||
if(!sp2) {
|
||||
if(!sp2)
|
||||
/* Assume that the id string is missing, but that it is okay */
|
||||
sp2 = pubkey + pubkey_len;
|
||||
}
|
||||
|
||||
if(ssh2_base64_decode(session, (char **)&tmp, &tmp_len, (const char *)sp1,
|
||||
sp2 - sp1)) {
|
||||
@@ -664,13 +652,13 @@ static int file_read_publickey(LIBSSH2_SESSION *session,
|
||||
pubkeyfile));
|
||||
/* Read Public Key */
|
||||
fd = fopen(pubkeyfile, "rb");
|
||||
if(!fd) {
|
||||
if(!fd)
|
||||
return ssh2_err(session, LIBSSH2_ERROR_FILE,
|
||||
"Unable to open public key file");
|
||||
}
|
||||
while(!feof(fd) && fread(&c, 1, 1, fd) == 1 && c != '\r' && c != '\n') {
|
||||
|
||||
while(!feof(fd) && fread(&c, 1, 1, fd) == 1 && c != '\r' && c != '\n')
|
||||
pubkey_len++;
|
||||
}
|
||||
|
||||
fseek(fd, 0L, SEEK_SET);
|
||||
|
||||
if(pubkey_len <= 1) {
|
||||
@@ -695,9 +683,8 @@ static int file_read_publickey(LIBSSH2_SESSION *session,
|
||||
/*
|
||||
* Remove trailing whitespace
|
||||
*/
|
||||
while(pubkey_len && isspace(pubkey[pubkey_len - 1])) {
|
||||
while(pubkey_len && isspace(pubkey[pubkey_len - 1]))
|
||||
pubkey_len--;
|
||||
}
|
||||
|
||||
if(!pubkey_len) {
|
||||
SSH2_FREE(session, pubkey);
|
||||
@@ -716,10 +703,9 @@ static int file_read_publickey(LIBSSH2_SESSION *session,
|
||||
|
||||
sp_len = sp1 > pubkey ? (sp1 - pubkey) : 0;
|
||||
sp2 = memchr(sp1, ' ', pubkey_len - sp_len);
|
||||
if(!sp2) {
|
||||
if(!sp2)
|
||||
/* Assume that the id string is missing, but that it is okay */
|
||||
sp2 = pubkey + pubkey_len;
|
||||
}
|
||||
|
||||
if(ssh2_base64_decode(session, (char **)&tmp, &tmp_len, (const char *)sp1,
|
||||
sp2 - sp1)) {
|
||||
@@ -763,10 +749,9 @@ static int memory_read_privatekey(LIBSSH2_SESSION *session,
|
||||
}
|
||||
hostkey_methods_avail++;
|
||||
}
|
||||
if(!*hostkey_method) {
|
||||
if(!*hostkey_method)
|
||||
return ssh2_err(session, LIBSSH2_ERROR_METHOD_NONE,
|
||||
"No handler for specified private key");
|
||||
}
|
||||
|
||||
if((*hostkey_method)->initPEMFromMemory(session, privkeyfiledata,
|
||||
privkeyfiledata_len,
|
||||
@@ -806,10 +791,9 @@ static int file_read_privatekey(LIBSSH2_SESSION *session,
|
||||
}
|
||||
hostkey_methods_avail++;
|
||||
}
|
||||
if(!*hostkey_method) {
|
||||
if(!*hostkey_method)
|
||||
return ssh2_err(session, LIBSSH2_ERROR_METHOD_NONE,
|
||||
"No handler for specified private key");
|
||||
}
|
||||
|
||||
if((*hostkey_method)->initPEM(session, privkeyfile,
|
||||
(const unsigned char *)passphrase,
|
||||
@@ -861,15 +845,13 @@ static int sign_frommemory(LIBSSH2_SESSION *session,
|
||||
|
||||
if(privkeyobj->signv(session, sig, sig_len, 1, &datavec,
|
||||
&hostkey_abstract)) {
|
||||
if(privkeyobj->dtor) {
|
||||
if(privkeyobj->dtor)
|
||||
privkeyobj->dtor(session, &hostkey_abstract);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(privkeyobj->dtor) {
|
||||
if(privkeyobj->dtor)
|
||||
privkeyobj->dtor(session, &hostkey_abstract);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -901,15 +883,13 @@ static int sign_fromfile(LIBSSH2_SESSION *session,
|
||||
|
||||
if(privkeyobj->signv(session, sig, sig_len, 1, &datavec,
|
||||
&hostkey_abstract)) {
|
||||
if(privkeyobj->dtor) {
|
||||
if(privkeyobj->dtor)
|
||||
privkeyobj->dtor(session, &hostkey_abstract);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(privkeyobj->dtor) {
|
||||
if(privkeyobj->dtor)
|
||||
privkeyobj->dtor(session, &hostkey_abstract);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -922,9 +902,8 @@ int libssh2_sign_sk(LIBSSH2_SESSION *session,
|
||||
LIBSSH2_PRIVKEY_SK *sk_info = (LIBSSH2_PRIVKEY_SK *)(*abstract);
|
||||
LIBSSH2_SK_SIG_INFO sig_info = { 0 };
|
||||
|
||||
if(sk_info->handle_len <= 0) {
|
||||
if(sk_info->handle_len <= 0)
|
||||
return LIBSSH2_ERROR_DECRYPT;
|
||||
}
|
||||
|
||||
rc = sk_info->sign_callback(session,
|
||||
&sig_info,
|
||||
@@ -1003,9 +982,8 @@ int libssh2_sign_sk(LIBSSH2_SESSION *session,
|
||||
|
||||
SSH2_FREE(session, sig_info.sig_r);
|
||||
|
||||
if(sig_info.sig_s) {
|
||||
if(sig_info.sig_s)
|
||||
SSH2_FREE(session, sig_info.sig_s);
|
||||
}
|
||||
}
|
||||
else {
|
||||
ssh2_deb((session, LIBSSH2_ERROR_DECRYPT,
|
||||
@@ -1147,15 +1125,13 @@ static int userauth_hostbased_fromfile(LIBSSH2_SESSION *session,
|
||||
session->userauth_host_method = NULL;
|
||||
SSH2_FREE(session, session->userauth_host_packet);
|
||||
session->userauth_host_packet = NULL;
|
||||
if(privkeyobj->dtor) {
|
||||
if(privkeyobj->dtor)
|
||||
privkeyobj->dtor(session, &abstract);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(privkeyobj && privkeyobj->dtor) {
|
||||
if(privkeyobj && privkeyobj->dtor)
|
||||
privkeyobj->dtor(session, &abstract);
|
||||
}
|
||||
|
||||
if(sig_len > pubkeydata_len) {
|
||||
unsigned char *newpacket;
|
||||
@@ -1203,9 +1179,8 @@ static int userauth_hostbased_fromfile(LIBSSH2_SESSION *session,
|
||||
session->userauth_host_s -
|
||||
session->userauth_host_packet,
|
||||
NULL, 0);
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN)
|
||||
return ssh2_err(session, LIBSSH2_ERROR_EAGAIN, "Would block");
|
||||
}
|
||||
else if(rc) {
|
||||
SSH2_FREE(session, session->userauth_host_packet);
|
||||
session->userauth_host_packet = NULL;
|
||||
@@ -1231,15 +1206,13 @@ static int userauth_hostbased_fromfile(LIBSSH2_SESSION *session,
|
||||
&data_len, 0, NULL, 0,
|
||||
&session->
|
||||
userauth_host_packet_requirev_state);
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN)
|
||||
return ssh2_err(session, LIBSSH2_ERROR_EAGAIN, "Would block");
|
||||
}
|
||||
|
||||
session->userauth_host_state = ssh2_NB_state_idle;
|
||||
if(rc || data_len < 1) {
|
||||
if(rc || data_len < 1)
|
||||
return ssh2_err(session, LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED,
|
||||
"Auth failed");
|
||||
}
|
||||
|
||||
if(session->userauth_host_data[0] == SSH_MSG_USERAUTH_SUCCESS) {
|
||||
ssh2_deb((session, LIBSSH2_TRACE_AUTH,
|
||||
@@ -1289,30 +1262,26 @@ int libssh2_userauth_hostbased_fromfile_ex(LIBSSH2_SESSION *session,
|
||||
size_t plain_method(char *method, size_t method_len)
|
||||
{
|
||||
if(!strncmp("ssh-rsa-cert-v01@openssh.com",
|
||||
method, method_len)) {
|
||||
method, method_len))
|
||||
return 7;
|
||||
}
|
||||
|
||||
if(!strncmp("rsa-sha2-256-cert-v01@openssh.com",
|
||||
method, method_len) ||
|
||||
!strncmp("rsa-sha2-512-cert-v01@openssh.com",
|
||||
method, method_len)) {
|
||||
method, method_len))
|
||||
return 12;
|
||||
}
|
||||
|
||||
if(!strncmp("ecdsa-sha2-nistp256-cert-v01@openssh.com",
|
||||
method, method_len) ||
|
||||
!strncmp("ecdsa-sha2-nistp384-cert-v01@openssh.com",
|
||||
method, method_len) ||
|
||||
!strncmp("ecdsa-sha2-nistp521-cert-v01@openssh.com",
|
||||
method, method_len)) {
|
||||
method, method_len))
|
||||
return 19;
|
||||
}
|
||||
|
||||
if(!strncmp("ssh-ed25519-cert-v01@openssh.com",
|
||||
method, method_len)) {
|
||||
method, method_len))
|
||||
return 11;
|
||||
}
|
||||
|
||||
if(!strncmp("sk-ecdsa-sha2-nistp256-cert-v01@openssh.com",
|
||||
method, method_len)) {
|
||||
@@ -1359,9 +1328,9 @@ static int is_version_less_than_78(const char *version)
|
||||
return 0; /* Not a valid number */
|
||||
|
||||
if((major >= 1 && major <= 6) ||
|
||||
(major == 7 && minor >= 0 && minor <= 7)) {
|
||||
(major == 7 && minor >= 0 && minor <= 7))
|
||||
return 1; /* Version is in the specified range */
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1400,10 +1369,9 @@ static int key_sign_algorithm(LIBSSH2_SESSION *session,
|
||||
const char *supported_algs =
|
||||
ssh2_supported_key_sign_algs(session, *key_method, *key_method_len);
|
||||
|
||||
if(!supported_algs || !session->server_sign_algorithms) {
|
||||
if(!supported_algs || !session->server_sign_algorithms)
|
||||
/* no upgrading key algorithm supported, do nothing */
|
||||
return LIBSSH2_ERROR_NONE;
|
||||
}
|
||||
|
||||
/* Set "SSH_BUG_SIGTYPE" flag when the remote server version is OpenSSH 7.7
|
||||
or lower and when the RSA key in question is a certificate to ignore
|
||||
@@ -1594,11 +1562,11 @@ retry_auth:
|
||||
|
||||
session->userauth_pblc_method =
|
||||
SSH2_ALLOC(session, session->userauth_pblc_method_len);
|
||||
if(!session->userauth_pblc_method) {
|
||||
if(!session->userauth_pblc_method)
|
||||
return ssh2_err(session, LIBSSH2_ERROR_ALLOC,
|
||||
"Unable to allocate memory "
|
||||
"for public key data");
|
||||
}
|
||||
|
||||
memcpy(session->userauth_pblc_method, pubkeydata + 4,
|
||||
session->userauth_pblc_method_len);
|
||||
}
|
||||
@@ -1616,11 +1584,10 @@ retry_auth:
|
||||
}
|
||||
|
||||
if(session->userauth_pblc_method_len &&
|
||||
session->userauth_pblc_method) {
|
||||
session->userauth_pblc_method)
|
||||
ssh2_deb((session, LIBSSH2_TRACE_KEX, "Signing using %.*s",
|
||||
(int)session->userauth_pblc_method_len,
|
||||
session->userauth_pblc_method));
|
||||
}
|
||||
|
||||
/*
|
||||
* 45 = packet_type(1) + username_len(4) + servicename_len(4) +
|
||||
@@ -1696,9 +1663,8 @@ retry_auth:
|
||||
NULL, 0,
|
||||
&session->
|
||||
userauth_pblc_packet_requirev_state);
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN)
|
||||
return ssh2_err(session, LIBSSH2_ERROR_EAGAIN, "Would block");
|
||||
}
|
||||
else if(rc || session->userauth_pblc_data_len < 1) {
|
||||
SSH2_FREE(session, session->userauth_pblc_packet);
|
||||
session->userauth_pblc_packet = NULL;
|
||||
@@ -1756,11 +1722,10 @@ retry_auth:
|
||||
s = buf = SSH2_ALLOC(session,
|
||||
4 + session->session_id_len +
|
||||
session->userauth_pblc_packet_len);
|
||||
if(!buf) {
|
||||
if(!buf)
|
||||
return ssh2_err(session, LIBSSH2_ERROR_ALLOC,
|
||||
"Unable to allocate memory for "
|
||||
"userauth-publickey signed data");
|
||||
}
|
||||
|
||||
ssh2_store_str(&s, (const char *)session->session_id,
|
||||
session->session_id_len);
|
||||
@@ -1771,9 +1736,8 @@ retry_auth:
|
||||
|
||||
rc = sign_callback(session, &sig, &sig_len, buf, s - buf, abstract);
|
||||
SSH2_FREE(session, buf);
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN)
|
||||
return ssh2_err(session, LIBSSH2_ERROR_EAGAIN, "Would block");
|
||||
}
|
||||
else if(rc == LIBSSH2_ERROR_ALGO_UNSUPPORTED && auth_attempts == 1) {
|
||||
/* try again with the default key algo */
|
||||
SSH2_FREE(session, session->userauth_pblc_method);
|
||||
@@ -1794,10 +1758,9 @@ retry_auth:
|
||||
"Callback returned error");
|
||||
}
|
||||
|
||||
if(!sig) {
|
||||
if(!sig)
|
||||
return ssh2_err(session, LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED,
|
||||
"Callback did not return signature");
|
||||
}
|
||||
|
||||
/*
|
||||
* If this function was restarted, pubkeydata_len might still be 0
|
||||
@@ -1872,9 +1835,8 @@ retry_auth:
|
||||
session->userauth_pblc_s -
|
||||
session->userauth_pblc_packet,
|
||||
NULL, 0);
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN)
|
||||
return ssh2_err(session, LIBSSH2_ERROR_EAGAIN, "Would block");
|
||||
}
|
||||
else if(rc) {
|
||||
SSH2_FREE(session, session->userauth_pblc_packet);
|
||||
session->userauth_pblc_packet = NULL;
|
||||
@@ -1895,10 +1857,9 @@ retry_auth:
|
||||
&session->userauth_pblc_data,
|
||||
&session->userauth_pblc_data_len, 0, NULL, 0,
|
||||
&session->userauth_pblc_packet_requirev_state);
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN)
|
||||
return ssh2_err(session, LIBSSH2_ERROR_EAGAIN,
|
||||
"Would block waiting for publickey USERAUTH response");
|
||||
}
|
||||
else if(rc || session->userauth_pblc_data_len < 1) {
|
||||
session->userauth_pblc_state = ssh2_NB_state_idle;
|
||||
return ssh2_err(session, LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED,
|
||||
@@ -2140,9 +2101,8 @@ static int userauth_keyboard_interactive(
|
||||
memset(&session->userauth_kybd_packet_requirev_state, 0,
|
||||
sizeof(session->userauth_kybd_packet_requirev_state));
|
||||
|
||||
if(username_len > MAX_INPUT_LEN) {
|
||||
if(username_len > MAX_INPUT_LEN)
|
||||
return ssh2_err(session, LIBSSH2_ERROR_INVAL, "Username too long");
|
||||
}
|
||||
|
||||
session->userauth_kybd_packet_len =
|
||||
1 /* byte SSH_MSG_USERAUTH_REQUEST */
|
||||
@@ -2157,11 +2117,10 @@ static int userauth_keyboard_interactive(
|
||||
|
||||
session->userauth_kybd_data = s =
|
||||
SSH2_ALLOC(session, session->userauth_kybd_packet_len);
|
||||
if(!s) {
|
||||
if(!s)
|
||||
return ssh2_err(session, LIBSSH2_ERROR_ALLOC,
|
||||
"Unable to allocate memory for "
|
||||
"keyboard-interactive authentication");
|
||||
}
|
||||
|
||||
*s++ = SSH_MSG_USERAUTH_REQUEST;
|
||||
|
||||
@@ -2189,9 +2148,8 @@ static int userauth_keyboard_interactive(
|
||||
if(session->userauth_kybd_state == ssh2_NB_state_created) {
|
||||
rc = ssh2_transport_send(session, session->userauth_kybd_data,
|
||||
session->userauth_kybd_packet_len, NULL, 0);
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN)
|
||||
return ssh2_err(session, LIBSSH2_ERROR_EAGAIN, "Would block");
|
||||
}
|
||||
else if(rc) {
|
||||
SSH2_FREE(session, session->userauth_kybd_data);
|
||||
session->userauth_kybd_data = NULL;
|
||||
@@ -2213,9 +2171,8 @@ static int userauth_keyboard_interactive(
|
||||
0, NULL, 0,
|
||||
&session->
|
||||
userauth_kybd_packet_requirev_state);
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
||||
if(rc == LIBSSH2_ERROR_EAGAIN)
|
||||
return ssh2_err(session, LIBSSH2_ERROR_EAGAIN, "Would block");
|
||||
}
|
||||
else if(rc || session->userauth_kybd_data_len < 1) {
|
||||
session->userauth_kybd_state = ssh2_NB_state_idle;
|
||||
return ssh2_err(session, LIBSSH2_ERROR_AUTHENTICATION_FAILED,
|
||||
@@ -2244,10 +2201,8 @@ static int userauth_keyboard_interactive(
|
||||
}
|
||||
|
||||
/* server requested PAM-like conversation */
|
||||
if(userauth_keyboard_interactive_decode_info_request(session)
|
||||
< 0) {
|
||||
if(userauth_keyboard_interactive_decode_info_request(session) < 0)
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
response_callback((const char *)session->userauth_kybd_auth_name,
|
||||
(int)session->userauth_kybd_auth_name_len,
|
||||
@@ -2300,10 +2255,9 @@ static int userauth_keyboard_interactive(
|
||||
s++;
|
||||
ssh2_store_u32(&s, session->userauth_kybd_num_prompts);
|
||||
|
||||
for(i = 0; i < session->userauth_kybd_num_prompts; i++) {
|
||||
for(i = 0; i < session->userauth_kybd_num_prompts; i++)
|
||||
ssh2_store_str(&s, session->userauth_kybd_responses[i].text,
|
||||
session->userauth_kybd_responses[i].length);
|
||||
}
|
||||
|
||||
session->userauth_kybd_state = ssh2_NB_state_sent1;
|
||||
}
|
||||
@@ -2430,10 +2384,9 @@ int libssh2_userauth_publickey_sk(
|
||||
&(sk_info.key_handle),
|
||||
&(sk_info.handle_len),
|
||||
privatekeydata, privatekeydata_len,
|
||||
passphrase)) {
|
||||
passphrase))
|
||||
return ssh2_err(session, LIBSSH2_ERROR_FILE,
|
||||
"Unable to extract public key from private key.");
|
||||
}
|
||||
else if(publickeydata_len == 0 || !publickeydata) {
|
||||
session->userauth_pblc_method = tmp_method;
|
||||
session->userauth_pblc_method_len = tmp_method_len;
|
||||
@@ -2445,9 +2398,8 @@ int libssh2_userauth_publickey_sk(
|
||||
const char *ecdsa = "sk-ecdsa-sha2-nistp256-cert-v01@openssh.com";
|
||||
const char *ed25519 = "sk-ssh-ed25519-cert-v01@openssh.com";
|
||||
|
||||
if(tmp_method) {
|
||||
if(tmp_method)
|
||||
SSH2_FREE(session, tmp_method);
|
||||
}
|
||||
|
||||
if(!strncmp((const char *)publickeydata, ecdsa, strlen(ecdsa))) {
|
||||
session->userauth_pblc_method_len = strlen(ecdsa);
|
||||
@@ -2485,19 +2437,17 @@ int libssh2_userauth_publickey_sk(
|
||||
pubkeydata, pubkeydata_len,
|
||||
libssh2_sign_sk, &sign_abstract);
|
||||
|
||||
while(rc == LIBSSH2_ERROR_EAGAIN) {
|
||||
while(rc == LIBSSH2_ERROR_EAGAIN)
|
||||
rc = ssh2_userauth_publickey(session, username, username_len,
|
||||
pubkeydata, pubkeydata_len,
|
||||
libssh2_sign_sk, &sign_abstract);
|
||||
}
|
||||
}
|
||||
|
||||
if(tmp_publickeydata)
|
||||
SSH2_FREE(session, tmp_publickeydata);
|
||||
|
||||
if(sk_info.application) {
|
||||
if(sk_info.application)
|
||||
SSH2_FREE(session, SSH2_UNCONST(sk_info.application));
|
||||
}
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
487
src/wincng.c
487
src/wincng.c
File diff suppressed because it is too large
Load Diff
@@ -94,9 +94,8 @@ static int run_command_varg(char **output, const char *command, va_list args)
|
||||
int ret;
|
||||
size_t buf_len;
|
||||
|
||||
if(output) {
|
||||
if(output)
|
||||
*output = NULL;
|
||||
}
|
||||
|
||||
/* Format the command string */
|
||||
#if defined(__GNUC__) || defined(__clang__)
|
||||
@@ -133,23 +132,20 @@ static int run_command_varg(char **output, const char *command, va_list args)
|
||||
buf[0] = 0;
|
||||
buf_len = 0;
|
||||
while(buf_len < (sizeof(buf) - 1) &&
|
||||
fgets(&buf[buf_len], (int)(sizeof(buf) - buf_len), pipe)) {
|
||||
fgets(&buf[buf_len], (int)(sizeof(buf) - buf_len), pipe))
|
||||
buf_len = strlen(buf);
|
||||
}
|
||||
|
||||
ret = pclose(pipe);
|
||||
if(ret) {
|
||||
if(ret)
|
||||
fprintf(stderr, "Error running command '%s' (exit %d): %s\n",
|
||||
command, ret, buf);
|
||||
}
|
||||
|
||||
if(output) {
|
||||
/* command output may contain a trailing newline, so we trim
|
||||
* whitespace here */
|
||||
size_t end = strlen(buf);
|
||||
while(end > 0 && isspace((int)buf[end - 1])) {
|
||||
while(end > 0 && isspace((int)buf[end - 1]))
|
||||
buf[end - 1] = '\0';
|
||||
}
|
||||
|
||||
*output = libssh2_strdup(buf);
|
||||
}
|
||||
@@ -186,18 +182,16 @@ static int build_openssh_server_docker_image(void)
|
||||
if(ret == 0) {
|
||||
ret = run_command(NULL, "docker tag %s libssh2/openssh_server",
|
||||
container_image_name);
|
||||
if(ret == 0) {
|
||||
if(ret == 0)
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
}
|
||||
return run_command(NULL,
|
||||
"docker build --quiet -t libssh2/openssh_server %s",
|
||||
srcdir_path("openssh_server"));
|
||||
}
|
||||
else {
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
static const char *openssh_server_port(void)
|
||||
@@ -209,12 +203,11 @@ static int start_openssh_server(char **container_id_out)
|
||||
{
|
||||
if(have_docker) {
|
||||
const char *container_host_port = openssh_server_port();
|
||||
if(container_host_port) {
|
||||
if(container_host_port)
|
||||
return run_command(container_id_out,
|
||||
"docker run --rm -d -p %s:22 "
|
||||
"libssh2/openssh_server",
|
||||
container_host_port);
|
||||
}
|
||||
|
||||
return run_command(container_id_out,
|
||||
"docker run --rm -d -p 22 "
|
||||
@@ -228,12 +221,10 @@ static int start_openssh_server(char **container_id_out)
|
||||
|
||||
static int stop_openssh_server(char *container_id)
|
||||
{
|
||||
if(have_docker) {
|
||||
if(have_docker)
|
||||
return run_command(NULL, "docker stop %s", container_id);
|
||||
}
|
||||
else {
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
static const char *docker_machine_name(void)
|
||||
@@ -251,10 +242,8 @@ static int is_running_inside_a_container(void)
|
||||
char line[1024];
|
||||
int found = 0;
|
||||
f = fopen(cgroup_filename, "r");
|
||||
if(!f) {
|
||||
/* Do not go further, we are not in a container */
|
||||
return 0;
|
||||
}
|
||||
if(!f)
|
||||
return 0; /* Do not go further, we are not in a container */
|
||||
while(fgets(line, sizeof(line), f)) {
|
||||
if(strstr(line, "docker")) {
|
||||
found = 1;
|
||||
@@ -288,9 +277,8 @@ static int ip_address_from_container(char *container_id, char **ip_address_out)
|
||||
for(;;) {
|
||||
int ret = run_command(ip_address_out, "docker-machine ip %s",
|
||||
active_docker_machine);
|
||||
if(ret == 0) {
|
||||
if(ret == 0)
|
||||
return 0;
|
||||
}
|
||||
else if(attempt_no > 5) {
|
||||
fprintf(
|
||||
stderr,
|
||||
@@ -306,21 +294,19 @@ static int ip_address_from_container(char *container_id, char **ip_address_out)
|
||||
}
|
||||
}
|
||||
else {
|
||||
if(is_running_inside_a_container()) {
|
||||
if(is_running_inside_a_container())
|
||||
return run_command(ip_address_out,
|
||||
"docker inspect --format "
|
||||
"\"{{ .NetworkSettings.IPAddress }}\""
|
||||
" %s",
|
||||
container_id);
|
||||
}
|
||||
else {
|
||||
else
|
||||
return run_command(ip_address_out,
|
||||
"docker inspect --format "
|
||||
"\"{{ index (index (index "
|
||||
".NetworkSettings.Ports "
|
||||
"\\\"22/tcp\\\") 0) \\\"HostIp\\\" }}\" %s",
|
||||
container_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -330,13 +316,12 @@ static int port_from_container(char *container_id, char **port_out)
|
||||
*port_out = libssh2_strdup("22");
|
||||
return 0;
|
||||
}
|
||||
else {
|
||||
else
|
||||
return run_command(port_out,
|
||||
"docker inspect --format "
|
||||
"\"{{ index (index (index .NetworkSettings.Ports "
|
||||
"\\\"22/tcp\\\") 0) \\\"HostPort\\\" }}\" %s",
|
||||
container_id);
|
||||
}
|
||||
}
|
||||
|
||||
static void close_socket_to_container(libssh2_socket_t sock)
|
||||
@@ -380,14 +365,12 @@ static libssh2_socket_t open_socket_to_container(char *container_id)
|
||||
else {
|
||||
const char *env;
|
||||
env = getenv("OPENSSH_SERVER_HOST");
|
||||
if(!env) {
|
||||
if(!env)
|
||||
env = "127.0.0.1";
|
||||
}
|
||||
ip_address = libssh2_strdup(env);
|
||||
env = openssh_server_port();
|
||||
if(!env) {
|
||||
if(!env)
|
||||
env = "4711";
|
||||
}
|
||||
port_string = libssh2_strdup(env);
|
||||
}
|
||||
|
||||
@@ -461,9 +444,8 @@ int start_openssh_fixture(void)
|
||||
have_docker = !getenv("OPENSSH_NO_DOCKER");
|
||||
|
||||
ret = build_openssh_server_docker_image();
|
||||
if(!ret) {
|
||||
if(!ret)
|
||||
return start_openssh_server(&running_container_id);
|
||||
}
|
||||
else {
|
||||
fprintf(stderr, "Failed to build docker image\n");
|
||||
return ret;
|
||||
@@ -477,9 +459,8 @@ void stop_openssh_fixture(void)
|
||||
free(running_container_id);
|
||||
running_container_id = NULL;
|
||||
}
|
||||
else if(have_docker) {
|
||||
else if(have_docker)
|
||||
fprintf(stderr, "Cannot stop container - none started\n");
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
WSACleanup();
|
||||
|
||||
@@ -56,16 +56,13 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
|
||||
/* Create a session and start the handshake using the fuzz data
|
||||
passed in. */
|
||||
session = libssh2_session_init();
|
||||
if(session) {
|
||||
if(session)
|
||||
libssh2_session_set_blocking(session, 1);
|
||||
}
|
||||
else {
|
||||
else
|
||||
goto EXIT_LABEL;
|
||||
}
|
||||
|
||||
if(libssh2_session_handshake(session, socket_fds[0])) {
|
||||
if(libssh2_session_handshake(session, socket_fds[0]))
|
||||
goto EXIT_LABEL;
|
||||
}
|
||||
|
||||
/* If we get here the handshake actually completed. */
|
||||
handshake_completed = 1;
|
||||
@@ -73,11 +70,10 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
|
||||
EXIT_LABEL:
|
||||
|
||||
if(session) {
|
||||
if(handshake_completed) {
|
||||
if(handshake_completed)
|
||||
libssh2_session_disconnect(session,
|
||||
"Normal Shutdown, "
|
||||
"Thank you for playing");
|
||||
}
|
||||
|
||||
libssh2_session_free(session);
|
||||
}
|
||||
|
||||
@@ -51,22 +51,20 @@ int main(int argc, char **argv)
|
||||
free(buffer);
|
||||
buffer = NULL;
|
||||
}
|
||||
else {
|
||||
else
|
||||
fprintf(stderr,
|
||||
"[%s] Failed to allocate %zu bytes \n",
|
||||
argv[ii],
|
||||
buffer_len);
|
||||
}
|
||||
|
||||
/* Close the file as it is no longer needed. */
|
||||
fclose(infile);
|
||||
infile = NULL;
|
||||
}
|
||||
else {
|
||||
else
|
||||
/* Failed to open the file.
|
||||
Maybe wrong name or wrong permissions? */
|
||||
fprintf(stderr, "[%s] Open failed. \n", argv[ii]);
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
@@ -53,16 +53,14 @@ int main(void)
|
||||
do {
|
||||
int skipped, rc;
|
||||
LIBSSH2_SESSION *session = start_session_fixture(&skipped, &rc);
|
||||
if(session) {
|
||||
if(session)
|
||||
exit_code = (test(session) == 0) ? 0 : 1;
|
||||
}
|
||||
else if(skipped) {
|
||||
fprintf(stderr, "Test skipped.\n");
|
||||
exit_code = 0;
|
||||
}
|
||||
else {
|
||||
else
|
||||
exit_code = 1;
|
||||
}
|
||||
stop_session_fixture();
|
||||
if(exit_code == 0 ||
|
||||
#ifdef LIBSSH2_WINCNG
|
||||
|
||||
@@ -58,9 +58,8 @@ static int connect_to_server(void)
|
||||
{
|
||||
int rc;
|
||||
connected_socket = open_socket_to_openssh_server();
|
||||
if(connected_socket == LIBSSH2_INVALID_SOCKET) {
|
||||
if(connected_socket == LIBSSH2_INVALID_SOCKET)
|
||||
return LIBSSH2_ERROR_SOCKET_NONE;
|
||||
}
|
||||
|
||||
rc = libssh2_session_handshake(connected_session, connected_socket);
|
||||
if(rc) {
|
||||
@@ -77,8 +76,7 @@ static char const *skip_crypt[] = {
|
||||
/* Due to a bug with mbedTLS support, these crypt methods fail.
|
||||
Until that bug is fixed, do not run them there to avoid this
|
||||
known issue causing red tests.
|
||||
See: https://github.com/libssh2/libssh2/issues/793
|
||||
*/
|
||||
See: https://github.com/libssh2/libssh2/issues/793 */
|
||||
"3des-cbc",
|
||||
"aes128-cbc",
|
||||
"aes192-cbc",
|
||||
@@ -87,17 +85,14 @@ static char const *skip_crypt[] = {
|
||||
"aes256-gcm@openssh.com",
|
||||
"rijndael-cbc@lysator.liu.se",
|
||||
#endif
|
||||
|
||||
#if !LIBSSH2_3DES
|
||||
"3des-cbc",
|
||||
#endif
|
||||
|
||||
#if !LIBSSH2_AES_GCM
|
||||
/* Support for AES-GCM hasn't been added to these back-ends yet */
|
||||
"aes128-gcm@openssh.com",
|
||||
"aes256-gcm@openssh.com",
|
||||
#endif
|
||||
|
||||
NULL
|
||||
};
|
||||
|
||||
@@ -145,9 +140,9 @@ LIBSSH2_SESSION *start_session_fixture(int *skipped, int *err)
|
||||
}
|
||||
|
||||
rc = start_openssh_fixture();
|
||||
if(rc) {
|
||||
if(rc)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
rc = libssh2_init(0);
|
||||
if(rc) {
|
||||
fprintf(stderr, "libssh2_init failed (%d)\n", rc);
|
||||
@@ -201,9 +196,8 @@ LIBSSH2_SESSION *start_session_fixture(int *skipped, int *err)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if(getenv("FIXTURE_TRACE_ALL_CONNECT")) {
|
||||
if(getenv("FIXTURE_TRACE_ALL_CONNECT"))
|
||||
libssh2_trace(connected_session, 0);
|
||||
}
|
||||
|
||||
return connected_session;
|
||||
}
|
||||
@@ -216,9 +210,8 @@ void print_last_session_error(const char *function)
|
||||
libssh2_session_last_error(connected_session, &message, NULL, 0);
|
||||
fprintf(stderr, "%s failed (%d): %s\n", function, rc, message);
|
||||
}
|
||||
else {
|
||||
else
|
||||
fprintf(stderr, "No session\n");
|
||||
}
|
||||
}
|
||||
|
||||
static void srcdir_path_free(void);
|
||||
@@ -230,9 +223,8 @@ void stop_session_fixture(void)
|
||||
libssh2_session_free(connected_session);
|
||||
connected_session = NULL;
|
||||
}
|
||||
else {
|
||||
else
|
||||
fprintf(stderr, "Cannot stop session - none started\n");
|
||||
}
|
||||
|
||||
close_socket_to_openssh_server(connected_socket);
|
||||
connected_socket = LIBSSH2_INVALID_SOCKET;
|
||||
@@ -313,10 +305,9 @@ static void kbd_callback(const char *name, int name_len,
|
||||
|
||||
fprintf(stdout, "Kb-int name: %.*s\n", name_len, name);
|
||||
fprintf(stdout, "Kb-int instruction: %.*s\n", instruct_len, instruct);
|
||||
for(i = 0; i < num_prompts; ++i) {
|
||||
for(i = 0; i < num_prompts; ++i)
|
||||
fprintf(stdout, "Kb-int prompt %d: %.*s\n", i,
|
||||
(int)prompts[i].length, prompts[i].text);
|
||||
}
|
||||
|
||||
if(num_prompts == 1) {
|
||||
responses[0].text = libssh2_strdup(kbd_password);
|
||||
@@ -483,13 +474,12 @@ int test_auth_pubkey(LIBSSH2_SESSION *session, int flags,
|
||||
/* Ignore our hard-wired Dockerfile user when not running under Docker */
|
||||
if(!openssh_fixture_have_docker() && !strcmp(username, "libssh2")) {
|
||||
username = getenv("USER");
|
||||
if(!username) {
|
||||
if(!username)
|
||||
#ifdef _WIN32
|
||||
username = getenv("USERNAME");
|
||||
#else
|
||||
username = getenv("LOGNAME");
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
if(!username) {
|
||||
@@ -527,13 +517,12 @@ int test_auth_pubkey(LIBSSH2_SESSION *session, int flags,
|
||||
|
||||
free(buffer);
|
||||
}
|
||||
else {
|
||||
else
|
||||
rc = libssh2_userauth_publickey_fromfile_ex(session, username,
|
||||
(unsigned int)strlen(username),
|
||||
srcdir_path(fn_pub),
|
||||
srcdir_path(fn_priv),
|
||||
password);
|
||||
}
|
||||
|
||||
if((flags & TEST_AUTH_SHOULDFAIL) != 0) {
|
||||
if(rc == 0) {
|
||||
|
||||
@@ -34,21 +34,18 @@ int test(LIBSSH2_SESSION *session)
|
||||
return 1;
|
||||
}
|
||||
|
||||
if(type == LIBSSH2_HOSTKEY_TYPE_ED25519) {
|
||||
if(type == LIBSSH2_HOSTKEY_TYPE_ED25519)
|
||||
rc = ssh2_base64_decode(session, &expected_hostkey, &expected_len,
|
||||
EXPECTED_ED25519_HOSTKEY,
|
||||
strlen(EXPECTED_ED25519_HOSTKEY));
|
||||
}
|
||||
else if(type == LIBSSH2_HOSTKEY_TYPE_ECDSA_256) {
|
||||
else if(type == LIBSSH2_HOSTKEY_TYPE_ECDSA_256)
|
||||
rc = ssh2_base64_decode(session, &expected_hostkey, &expected_len,
|
||||
EXPECTED_ECDSA_HOSTKEY,
|
||||
strlen(EXPECTED_ECDSA_HOSTKEY));
|
||||
}
|
||||
else if(type == LIBSSH2_HOSTKEY_TYPE_RSA) {
|
||||
else if(type == LIBSSH2_HOSTKEY_TYPE_RSA)
|
||||
rc = ssh2_base64_decode(session, &expected_hostkey, &expected_len,
|
||||
EXPECTED_RSA_HOSTKEY,
|
||||
strlen(EXPECTED_RSA_HOSTKEY));
|
||||
}
|
||||
else {
|
||||
fprintf(stderr, "Unexpected type of hostkey: %d\n", type);
|
||||
return 1;
|
||||
|
||||
@@ -58,9 +58,8 @@ static void calculate_digest(const char *hash, size_t hash_len, char *buffer,
|
||||
char *p = buffer;
|
||||
char *end = buffer + buffer_len;
|
||||
|
||||
for(i = 0; i < hash_len && (end - p) >= 3; ++i) {
|
||||
for(i = 0; i < hash_len && (end - p) >= 3; ++i)
|
||||
p += snprintf(p, (size_t)(end - p), "%02X", (unsigned char)hash[i]);
|
||||
}
|
||||
}
|
||||
|
||||
int test(LIBSSH2_SESSION *session)
|
||||
|
||||
@@ -152,17 +152,15 @@ int main(int argc, char *argv[])
|
||||
#endif
|
||||
do {
|
||||
rc = libssh2_session_handshake(session, sock);
|
||||
if(rc == 0) {
|
||||
if(rc == 0)
|
||||
break;
|
||||
}
|
||||
fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
|
||||
if(
|
||||
#ifdef LIBSSH2_WINCNG
|
||||
rc != LIBSSH2_ERROR_KEY_EXCHANGE_FAILURE ||
|
||||
#endif
|
||||
++retry > retries) {
|
||||
++retry > retries)
|
||||
break;
|
||||
}
|
||||
fprintf(stderr, "Retrying... %d / %d\n", retry, retries);
|
||||
} while(1);
|
||||
}
|
||||
@@ -176,9 +174,8 @@ int main(int argc, char *argv[])
|
||||
*/
|
||||
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
|
||||
fprintf(stderr, "Fingerprint: ");
|
||||
for(i = 0; i < 20; i++) {
|
||||
for(i = 0; i < 20; i++)
|
||||
fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
|
||||
}
|
||||
fprintf(stderr, "\n");
|
||||
|
||||
/* check what authentication methods are available */
|
||||
@@ -186,15 +183,12 @@ int main(int argc, char *argv[])
|
||||
(unsigned int)strlen(username));
|
||||
if(userauthlist) {
|
||||
fprintf(stderr, "Authentication methods: %s\n", userauthlist);
|
||||
if(strstr(userauthlist, "password")) {
|
||||
if(strstr(userauthlist, "password"))
|
||||
auth_pw |= 1;
|
||||
}
|
||||
if(strstr(userauthlist, "keyboard-interactive")) {
|
||||
if(strstr(userauthlist, "keyboard-interactive"))
|
||||
auth_pw |= 2;
|
||||
}
|
||||
if(strstr(userauthlist, "publickey")) {
|
||||
if(strstr(userauthlist, "publickey"))
|
||||
auth_pw |= 4;
|
||||
}
|
||||
|
||||
if(auth_pw & 4) {
|
||||
/* Authenticate by public key */
|
||||
@@ -204,9 +198,8 @@ int main(int argc, char *argv[])
|
||||
fprintf(stderr, "Authentication by public key failed.\n");
|
||||
goto shutdown;
|
||||
}
|
||||
else {
|
||||
else
|
||||
fprintf(stderr, "Authentication by public key succeeded.\n");
|
||||
}
|
||||
}
|
||||
else {
|
||||
fprintf(stderr, "No supported authentication methods found.\n");
|
||||
|
||||
Reference in New Issue
Block a user