1
1

style: make includes and examples code style strict

make travis and the makefile rule verify them too

Closes #334
Этот коммит содержится в:
Daniel Stenberg 2019-03-21 09:19:48 +01:00
родитель 4186a04cfd
Коммит 452517d96c
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 5CC908FDB71E12C2
28 изменённых файлов: 1060 добавлений и 958 удалений

Просмотреть файл

@ -102,7 +102,7 @@ install:
script: script:
- | - |
if [ "$B" = "style" ]; then if [ "$B" = "style" ]; then
perl src/checksrc.pl -i4 -m79 -ASIZEOFNOPAREN -ASNPRINTF -ACOPYRIGHT -AFOPENMODE -Wsrc/libssh2_config.h src/*.[ch] perl src/checksrc.pl -i4 -m79 -ASIZEOFNOPAREN -ASNPRINTF -ACOPYRIGHT -AFOPENMODE -Wsrc/libssh2_config.h src/*.[ch] include/*.h example/*.c
fi fi
- | - |
if [ "$B" = "configure" ]; then if [ "$B" = "configure" ]; then

Просмотреть файл

@ -149,4 +149,4 @@ $(VCPROJ): win32/vc8proj.head win32/vc8proj.foot Makefile.am
awk '{printf("%s\r\n", gensub("\r", "", "g"))}' > $@ ) awk '{printf("%s\r\n", gensub("\r", "", "g"))}' > $@ )
checksrc: checksrc:
perl src/checksrc.pl -i4 -m79 -ASIZEOFNOPAREN -ASNPRINTF -ACOPYRIGHT -AFOPENMODE -Wsrc/libssh2_config.h src/*.[ch] perl src/checksrc.pl -i4 -m79 -ASIZEOFNOPAREN -ASNPRINTF -ACOPYRIGHT -AFOPENMODE -Wsrc/libssh2_config.h src/*.[ch] include/*.h example/*.c

Просмотреть файл

@ -72,8 +72,8 @@ int main(int argc, char *argv[])
WSADATA wsadata; WSADATA wsadata;
int err; int err;
err = WSAStartup(MAKEWORD(2,0), &wsadata); err = WSAStartup(MAKEWORD(2, 0), &wsadata);
if (err != 0) { if(err != 0) {
fprintf(stderr, "WSAStartup failed with error: %d\n", err); fprintf(stderr, "WSAStartup failed with error: %d\n", err);
return 1; return 1;
} }
@ -82,49 +82,50 @@ int main(int argc, char *argv[])
int listensock = -1, forwardsock = -1; int listensock = -1, forwardsock = -1;
#endif #endif
if (argc > 1) if(argc > 1)
server_ip = argv[1]; server_ip = argv[1];
if (argc > 2) if(argc > 2)
username = argv[2]; username = argv[2];
if (argc > 3) if(argc > 3)
password = argv[3]; password = argv[3];
if (argc > 4) if(argc > 4)
local_listenip = argv[4]; local_listenip = argv[4];
if (argc > 5) if(argc > 5)
local_listenport = atoi(argv[5]); local_listenport = atoi(argv[5]);
if (argc > 6) if(argc > 6)
remote_desthost = argv[6]; remote_desthost = argv[6];
if (argc > 7) if(argc > 7)
remote_destport = atoi(argv[7]); remote_destport = atoi(argv[7]);
rc = libssh2_init (0); rc = libssh2_init(0);
if (rc != 0) { if(rc) {
fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
return 1; return 1;
} }
/* Connect to SSH server */ /* Connect to SSH server */
sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
#ifdef WIN32 #ifdef WIN32
if (sock == INVALID_SOCKET) { if(sock == INVALID_SOCKET) {
fprintf(stderr, "failed to open socket!\n"); fprintf(stderr, "failed to open socket!\n");
return -1; return -1;
} }
#else #else
if (sock == -1) { if(sock == -1) {
perror("socket"); perror("socket");
return -1; return -1;
} }
#endif #endif
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
if (INADDR_NONE == (sin.sin_addr.s_addr = inet_addr(server_ip))) { sin.sin_addr.s_addr = inet_addr(server_ip);
if(INADDR_NONE == sin.sin_addr.s_addr) {
perror("inet_addr"); perror("inet_addr");
return -1; return -1;
} }
sin.sin_port = htons(22); sin.sin_port = htons(22);
if (connect(sock, (struct sockaddr*)(&sin), if(connect(sock, (struct sockaddr*)(&sin),
sizeof(struct sockaddr_in)) != 0) { sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n"); fprintf(stderr, "failed to connect!\n");
return -1; return -1;
} }
@ -159,44 +160,46 @@ int main(int argc, char *argv[])
/* check what authentication methods are available */ /* check what authentication methods are available */
userauthlist = libssh2_userauth_list(session, username, strlen(username)); userauthlist = libssh2_userauth_list(session, username, strlen(username));
fprintf(stderr, "Authentication methods: %s\n", userauthlist); fprintf(stderr, "Authentication methods: %s\n", userauthlist);
if (strstr(userauthlist, "password")) if(strstr(userauthlist, "password"))
auth |= AUTH_PASSWORD; auth |= AUTH_PASSWORD;
if (strstr(userauthlist, "publickey")) if(strstr(userauthlist, "publickey"))
auth |= AUTH_PUBLICKEY; auth |= AUTH_PUBLICKEY;
/* check for options */ /* check for options */
if(argc > 8) { if(argc > 8) {
if ((auth & AUTH_PASSWORD) && !strcasecmp(argv[8], "-p")) if((auth & AUTH_PASSWORD) && !strcasecmp(argv[8], "-p"))
auth = AUTH_PASSWORD; auth = AUTH_PASSWORD;
if ((auth & AUTH_PUBLICKEY) && !strcasecmp(argv[8], "-k")) if((auth & AUTH_PUBLICKEY) && !strcasecmp(argv[8], "-k"))
auth = AUTH_PUBLICKEY; auth = AUTH_PUBLICKEY;
} }
if (auth & AUTH_PASSWORD) { if(auth & AUTH_PASSWORD) {
if (libssh2_userauth_password(session, username, password)) { if(libssh2_userauth_password(session, username, password)) {
fprintf(stderr, "Authentication by password failed.\n"); fprintf(stderr, "Authentication by password failed.\n");
goto shutdown; goto shutdown;
} }
} else if (auth & AUTH_PUBLICKEY) { }
if (libssh2_userauth_publickey_fromfile(session, username, keyfile1, else if(auth & AUTH_PUBLICKEY) {
keyfile2, password)) { if(libssh2_userauth_publickey_fromfile(session, username, keyfile1,
keyfile2, password)) {
fprintf(stderr, "\tAuthentication by public key failed!\n"); fprintf(stderr, "\tAuthentication by public key failed!\n");
goto shutdown; goto shutdown;
} }
fprintf(stderr, "\tAuthentication by public key succeeded.\n"); fprintf(stderr, "\tAuthentication by public key succeeded.\n");
} else { }
else {
fprintf(stderr, "No supported authentication methods found!\n"); fprintf(stderr, "No supported authentication methods found!\n");
goto shutdown; goto shutdown;
} }
listensock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); listensock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
#ifdef WIN32 #ifdef WIN32
if (listensock == INVALID_SOCKET) { if(listensock == INVALID_SOCKET) {
fprintf(stderr, "failed to open listen socket!\n"); fprintf(stderr, "failed to open listen socket!\n");
return -1; return -1;
} }
#else #else
if (listensock == -1) { if(listensock == -1) {
perror("socket"); perror("socket");
return -1; return -1;
} }
@ -204,18 +207,20 @@ int main(int argc, char *argv[])
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_port = htons(local_listenport); sin.sin_port = htons(local_listenport);
if (INADDR_NONE == (sin.sin_addr.s_addr = inet_addr(local_listenip))) { sin.sin_addr.s_addr = inet_addr(local_listenip);
if(INADDR_NONE == sin.sin_addr.s_addr) {
perror("inet_addr"); perror("inet_addr");
goto shutdown; goto shutdown;
} }
sockopt = 1; sockopt = 1;
setsockopt(listensock, SOL_SOCKET, SO_REUSEADDR, &sockopt, sizeof(sockopt)); setsockopt(listensock, SOL_SOCKET, SO_REUSEADDR, &sockopt,
sinlen=sizeof(sin); sizeof(sockopt));
if (-1 == bind(listensock, (struct sockaddr *)&sin, sinlen)) { sinlen = sizeof(sin);
if(-1 == bind(listensock, (struct sockaddr *)&sin, sinlen)) {
perror("bind"); perror("bind");
goto shutdown; goto shutdown;
} }
if (-1 == listen(listensock, 2)) { if(-1 == listen(listensock, 2)) {
perror("listen"); perror("listen");
goto shutdown; goto shutdown;
} }
@ -225,12 +230,12 @@ int main(int argc, char *argv[])
forwardsock = accept(listensock, (struct sockaddr *)&sin, &sinlen); forwardsock = accept(listensock, (struct sockaddr *)&sin, &sinlen);
#ifdef WIN32 #ifdef WIN32
if (forwardsock == INVALID_SOCKET) { if(forwardsock == INVALID_SOCKET) {
fprintf(stderr, "failed to accept forward socket!\n"); fprintf(stderr, "failed to accept forward socket!\n");
goto shutdown; goto shutdown;
} }
#else #else
if (forwardsock == -1) { if(forwardsock == -1) {
perror("accept"); perror("accept");
goto shutdown; goto shutdown;
} }
@ -244,7 +249,7 @@ int main(int argc, char *argv[])
channel = libssh2_channel_direct_tcpip_ex(session, remote_desthost, channel = libssh2_channel_direct_tcpip_ex(session, remote_desthost,
remote_destport, shost, sport); remote_destport, shost, sport);
if (!channel) { if(!channel) {
fprintf(stderr, "Could not open the direct-tcpip channel!\n" fprintf(stderr, "Could not open the direct-tcpip channel!\n"
"(Note that this can be a problem at the server!" "(Note that this can be a problem at the server!"
" Please review the server logs.)\n"); " Please review the server logs.)\n");
@ -254,22 +259,23 @@ int main(int argc, char *argv[])
/* Must use non-blocking IO hereafter due to the current libssh2 API */ /* Must use non-blocking IO hereafter due to the current libssh2 API */
libssh2_session_set_blocking(session, 0); libssh2_session_set_blocking(session, 0);
while (1) { while(1) {
FD_ZERO(&fds); FD_ZERO(&fds);
FD_SET(forwardsock, &fds); FD_SET(forwardsock, &fds);
tv.tv_sec = 0; tv.tv_sec = 0;
tv.tv_usec = 100000; tv.tv_usec = 100000;
rc = select(forwardsock + 1, &fds, NULL, NULL, &tv); rc = select(forwardsock + 1, &fds, NULL, NULL, &tv);
if (-1 == rc) { if(-1 == rc) {
perror("select"); perror("select");
goto shutdown; goto shutdown;
} }
if (rc && FD_ISSET(forwardsock, &fds)) { if(rc && FD_ISSET(forwardsock, &fds)) {
len = recv(forwardsock, buf, sizeof(buf), 0); len = recv(forwardsock, buf, sizeof(buf), 0);
if (len < 0) { if(len < 0) {
perror("read"); perror("read");
goto shutdown; goto shutdown;
} else if (0 == len) { }
else if(0 == len) {
fprintf(stderr, "The client at %s:%d disconnected!\n", shost, fprintf(stderr, "The client at %s:%d disconnected!\n", shost,
sport); sport);
goto shutdown; goto shutdown;
@ -277,34 +283,34 @@ int main(int argc, char *argv[])
wr = 0; wr = 0;
while(wr < len) { while(wr < len) {
i = libssh2_channel_write(channel, buf + wr, len - wr); i = libssh2_channel_write(channel, buf + wr, len - wr);
if (LIBSSH2_ERROR_EAGAIN == i) { if(LIBSSH2_ERROR_EAGAIN == i) {
continue; continue;
} }
if (i < 0) { if(i < 0) {
fprintf(stderr, "libssh2_channel_write: %d\n", i); fprintf(stderr, "libssh2_channel_write: %d\n", i);
goto shutdown; goto shutdown;
} }
wr += i; wr += i;
} }
} }
while (1) { while(1) {
len = libssh2_channel_read(channel, buf, sizeof(buf)); len = libssh2_channel_read(channel, buf, sizeof(buf));
if (LIBSSH2_ERROR_EAGAIN == len) if(LIBSSH2_ERROR_EAGAIN == len)
break; break;
else if (len < 0) { else if(len < 0) {
fprintf(stderr, "libssh2_channel_read: %d", (int)len); fprintf(stderr, "libssh2_channel_read: %d", (int)len);
goto shutdown; goto shutdown;
} }
wr = 0; wr = 0;
while (wr < len) { while(wr < len) {
i = send(forwardsock, buf + wr, len - wr, 0); i = send(forwardsock, buf + wr, len - wr, 0);
if (i <= 0) { if(i <= 0) {
perror("write"); perror("write");
goto shutdown; goto shutdown;
} }
wr += i; wr += i;
} }
if (libssh2_channel_eof(channel)) { if(libssh2_channel_eof(channel)) {
fprintf(stderr, "The server at %s:%d disconnected!\n", fprintf(stderr, "The server at %s:%d disconnected!\n",
remote_desthost, remote_destport); remote_desthost, remote_destport);
goto shutdown; goto shutdown;
@ -320,7 +326,7 @@ shutdown:
close(forwardsock); close(forwardsock);
close(listensock); close(listensock);
#endif #endif
if (channel) if(channel)
libssh2_channel_free(channel); libssh2_channel_free(channel);
libssh2_session_disconnect(session, "Client disconnecting normally"); libssh2_session_disconnect(session, "Client disconnecting normally");
libssh2_session_free(session); libssh2_session_free(session);

Просмотреть файл

@ -38,9 +38,9 @@ int main(int argc, char *argv[])
const char *fingerprint; const char *fingerprint;
LIBSSH2_SESSION *session; LIBSSH2_SESSION *session;
LIBSSH2_CHANNEL *channel; LIBSSH2_CHANNEL *channel;
const char *username="username"; const char *username = "username";
const char *password="password"; const char *password = "password";
const char *scppath="/tmp/TEST"; const char *scppath = "/tmp/TEST";
libssh2_struct_stat fileinfo; libssh2_struct_stat fileinfo;
int rc; int rc;
libssh2_struct_stat_size got = 0; libssh2_struct_stat_size got = 0;
@ -49,31 +49,32 @@ int main(int argc, char *argv[])
WSADATA wsadata; WSADATA wsadata;
int err; int err;
err = WSAStartup(MAKEWORD(2,0), &wsadata); err = WSAStartup(MAKEWORD(2, 0), &wsadata);
if (err != 0) { if(err != 0) {
fprintf(stderr, "WSAStartup failed with error: %d\n", err); fprintf(stderr, "WSAStartup failed with error: %d\n", err);
return 1; return 1;
} }
#endif #endif
if (argc > 1) { if(argc > 1) {
hostaddr = inet_addr(argv[1]); hostaddr = inet_addr(argv[1]);
} else { }
else {
hostaddr = htonl(0x7F000001); hostaddr = htonl(0x7F000001);
} }
if (argc > 2) { if(argc > 2) {
username = argv[2]; username = argv[2];
} }
if (argc > 3) { if(argc > 3) {
password = argv[3]; password = argv[3];
} }
if (argc > 4) { if(argc > 4) {
scppath = argv[4]; scppath = argv[4];
} }
rc = libssh2_init (0); rc = libssh2_init(0);
if (rc != 0) { if(rc) {
fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
return 1; return 1;
} }
@ -86,8 +87,8 @@ int main(int argc, char *argv[])
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_port = htons(22); sin.sin_port = htons(22);
sin.sin_addr.s_addr = hostaddr; sin.sin_addr.s_addr = hostaddr;
if (connect(sock, (struct sockaddr*)(&sin), if(connect(sock, (struct sockaddr*)(&sin),
sizeof(struct sockaddr_in)) != 0) { sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n"); fprintf(stderr, "failed to connect!\n");
return -1; return -1;
} }
@ -119,18 +120,20 @@ int main(int argc, char *argv[])
} }
fprintf(stderr, "\n"); fprintf(stderr, "\n");
if (auth_pw) { if(auth_pw) {
/* We could authenticate via password */ /* We could authenticate via password */
if (libssh2_userauth_password(session, username, password)) { if(libssh2_userauth_password(session, username, password)) {
fprintf(stderr, "Authentication by password failed.\n"); fprintf(stderr, "Authentication by password failed.\n");
goto shutdown; goto shutdown;
} }
} else { }
else {
/* Or by public key */ /* Or by public key */
if (libssh2_userauth_publickey_fromfile(session, username, #define HOME_DIR "/home/username/"
"/home/username/.ssh/id_rsa.pub", if(libssh2_userauth_publickey_fromfile(session, username,
"/home/username/.ssh/id_rsa", HOME_DIR ".ssh/id_rsa.pub",
password)) { HOME_DIR ".ssh/id_rsa",
password)) {
fprintf(stderr, "\tAuthentication by public key failed\n"); fprintf(stderr, "\tAuthentication by public key failed\n");
goto shutdown; goto shutdown;
} }
@ -139,7 +142,7 @@ int main(int argc, char *argv[])
/* Request a file via SCP */ /* Request a file via SCP */
channel = libssh2_scp_recv2(session, scppath, &fileinfo); channel = libssh2_scp_recv2(session, scppath, &fileinfo);
if (!channel) { if(!channel) {
fprintf(stderr, "Unable to open a session: %d\n", fprintf(stderr, "Unable to open a session: %d\n",
libssh2_session_last_errno(session)); libssh2_session_last_errno(session));
goto shutdown; goto shutdown;
@ -148,7 +151,7 @@ int main(int argc, char *argv[])
while(got < fileinfo.st_size) { while(got < fileinfo.st_size) {
char mem[1024]; char mem[1024];
int amount=sizeof(mem); int amount = sizeof(mem);
if((fileinfo.st_size -got) < amount) { if((fileinfo.st_size -got) < amount) {
amount = (int)(fileinfo.st_size -got); amount = (int)(fileinfo.st_size -got);
@ -170,7 +173,8 @@ int main(int argc, char *argv[])
shutdown: shutdown:
libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing"); libssh2_session_disconnect(session,
"Normal Shutdown, Thank you for playing");
libssh2_session_free(session); libssh2_session_free(session);
#ifdef WIN32 #ifdef WIN32

Просмотреть файл

@ -85,9 +85,9 @@ int main(int argc, char *argv[])
const char *fingerprint; const char *fingerprint;
LIBSSH2_SESSION *session; LIBSSH2_SESSION *session;
LIBSSH2_CHANNEL *channel; LIBSSH2_CHANNEL *channel;
const char *username="username"; const char *username = "username";
const char *password="password"; const char *password = "password";
const char *scppath="/tmp/TEST"; const char *scppath = "/tmp/TEST";
libssh2_struct_stat fileinfo; libssh2_struct_stat fileinfo;
#ifdef HAVE_GETTIMEOFDAY #ifdef HAVE_GETTIMEOFDAY
struct timeval start; struct timeval start;
@ -103,31 +103,32 @@ int main(int argc, char *argv[])
WSADATA wsadata; WSADATA wsadata;
int err; int err;
err = WSAStartup(MAKEWORD(2,0), &wsadata); err = WSAStartup(MAKEWORD(2, 0), &wsadata);
if (err != 0) { if(err != 0) {
fprintf(stderr, "WSAStartup failed with error: %d\n", err); fprintf(stderr, "WSAStartup failed with error: %d\n", err);
return 1; return 1;
} }
#endif #endif
if (argc > 1) { if(argc > 1) {
hostaddr = inet_addr(argv[1]); hostaddr = inet_addr(argv[1]);
} else { }
else {
hostaddr = htonl(0x7F000001); hostaddr = htonl(0x7F000001);
} }
if (argc > 2) { if(argc > 2) {
username = argv[2]; username = argv[2];
} }
if (argc > 3) { if(argc > 3) {
password = argv[3]; password = argv[3];
} }
if (argc > 4) { if(argc > 4) {
scppath = argv[4]; scppath = argv[4];
} }
rc = libssh2_init (0); rc = libssh2_init(0);
if (rc != 0) { if(rc != 0) {
fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
return 1; return 1;
} }
@ -140,14 +141,14 @@ int main(int argc, char *argv[])
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_port = htons(22); sin.sin_port = htons(22);
sin.sin_addr.s_addr = hostaddr; sin.sin_addr.s_addr = hostaddr;
if (connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in)) != 0) { if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) {
fprintf(stderr, "failed to connect!\n"); fprintf(stderr, "failed to connect!\n");
return -1; return -1;
} }
/* Create a session instance */ /* Create a session instance */
session = libssh2_session_init(); session = libssh2_session_init();
if (!session) if(!session)
return -1; return -1;
/* Since we have set non-blocking, tell libssh2 we are non-blocking */ /* Since we have set non-blocking, tell libssh2 we are non-blocking */
@ -160,9 +161,9 @@ int main(int argc, char *argv[])
/* ... start it up. This will trade welcome banners, exchange keys, /* ... start it up. This will trade welcome banners, exchange keys,
* and setup crypto, compression, and MAC layers * and setup crypto, compression, and MAC layers
*/ */
while ((rc = libssh2_session_handshake(session, sock)) == while((rc = libssh2_session_handshake(session, sock)) ==
LIBSSH2_ERROR_EAGAIN); LIBSSH2_ERROR_EAGAIN);
if (rc) { if(rc) {
fprintf(stderr, "Failure establishing SSH session: %d\n", rc); fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
return -1; return -1;
} }
@ -179,24 +180,25 @@ int main(int argc, char *argv[])
} }
fprintf(stderr, "\n"); fprintf(stderr, "\n");
if (auth_pw) { if(auth_pw) {
/* We could authenticate via password */ /* We could authenticate via password */
while ((rc = libssh2_userauth_password(session, username, password)) == while((rc = libssh2_userauth_password(session, username, password)) ==
LIBSSH2_ERROR_EAGAIN); LIBSSH2_ERROR_EAGAIN);
if (rc) { if(rc) {
fprintf(stderr, "Authentication by password failed.\n"); fprintf(stderr, "Authentication by password failed.\n");
goto shutdown; goto shutdown;
} }
} else { }
else {
/* Or by public key */ /* Or by public key */
while ((rc = libssh2_userauth_publickey_fromfile(session, username, while((rc = libssh2_userauth_publickey_fromfile(session, username,
"/home/username/" "/home/username/"
".ssh/id_rsa.pub", ".ssh/id_rsa.pub",
"/home/username/" "/home/username/"
".ssh/id_rsa", ".ssh/id_rsa",
password)) == password)) ==
LIBSSH2_ERROR_EAGAIN); LIBSSH2_ERROR_EAGAIN);
if (rc) { if(rc) {
fprintf(stderr, "\tAuthentication by public key failed\n"); fprintf(stderr, "\tAuthentication by public key failed\n");
goto shutdown; goto shutdown;
} }
@ -211,7 +213,7 @@ int main(int argc, char *argv[])
do { do {
channel = libssh2_scp_recv2(session, scppath, &fileinfo); channel = libssh2_scp_recv2(session, scppath, &fileinfo);
if (!channel) { if(!channel) {
if(libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN) { if(libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN) {
char *err_msg; char *err_msg;
@ -224,7 +226,7 @@ int main(int argc, char *argv[])
waitsocket(sock, session); waitsocket(sock, session);
} }
} }
} while (!channel); } while(!channel);
fprintf(stderr, "libssh2_scp_recv() is done, now receive data!\n"); fprintf(stderr, "libssh2_scp_recv() is done, now receive data!\n");
while(got < fileinfo.st_size) { while(got < fileinfo.st_size) {
@ -232,22 +234,22 @@ int main(int argc, char *argv[])
int rc; int rc;
do { do {
int amount=sizeof(mem); int amount = sizeof(mem);
if ((fileinfo.st_size -got) < amount) { if((fileinfo.st_size -got) < amount) {
amount = (int)(fileinfo.st_size - got); amount = (int)(fileinfo.st_size - got);
} }
/* loop until we block */ /* loop until we block */
rc = libssh2_channel_read(channel, mem, amount); rc = libssh2_channel_read(channel, mem, amount);
if (rc > 0) { if(rc > 0) {
write(1, mem, rc); write(1, mem, rc);
got += rc; got += rc;
total += rc; total += rc;
} }
} while (rc > 0); } while(rc > 0);
if ((rc == LIBSSH2_ERROR_EAGAIN) && (got < fileinfo.st_size)) { if((rc == LIBSSH2_ERROR_EAGAIN) && (got < fileinfo.st_size)) {
/* this is due to blocking that would occur otherwise /* this is due to blocking that would occur otherwise
so we loop on this condition */ so we loop on this condition */
@ -262,8 +264,9 @@ int main(int argc, char *argv[])
gettimeofday(&end, NULL); gettimeofday(&end, NULL);
time_ms = tvdiff(end, start); time_ms = tvdiff(end, start);
fprintf(stderr, "Got %ld bytes in %ld ms = %.1f bytes/sec spin: %d\n", (long)total, fprintf(stderr, "Got %ld bytes in %ld ms = %.1f bytes/sec spin: %d\n",
time_ms, total/(time_ms/1000.0), spin); (long)total,
time_ms, total/(time_ms/1000.0), spin);
#else #else
fprintf(stderr, "Got %ld bytes spin: %d\n", (long)total, spin); fprintf(stderr, "Got %ld bytes spin: %d\n", (long)total, spin);
#endif #endif

Просмотреть файл

@ -38,10 +38,10 @@ int main(int argc, char *argv[])
const char *fingerprint; const char *fingerprint;
LIBSSH2_SESSION *session = NULL; LIBSSH2_SESSION *session = NULL;
LIBSSH2_CHANNEL *channel; LIBSSH2_CHANNEL *channel;
const char *username="username"; const char *username = "username";
const char *password="password"; const char *password = "password";
const char *loclfile="scp_write.c"; const char *loclfile = "scp_write.c";
const char *scppath="/tmp/TEST"; const char *scppath = "/tmp/TEST";
FILE *local; FILE *local;
int rc; int rc;
char mem[1024]; char mem[1024];
@ -53,39 +53,40 @@ int main(int argc, char *argv[])
WSADATA wsadata; WSADATA wsadata;
int err; int err;
err = WSAStartup(MAKEWORD(2,0), &wsadata); err = WSAStartup(MAKEWORD(2, 0), &wsadata);
if (err != 0) { if(err != 0) {
fprintf(stderr, "WSAStartup failed with error: %d\n", err); fprintf(stderr, "WSAStartup failed with error: %d\n", err);
return 1; return 1;
} }
#endif #endif
if (argc > 1) { if(argc > 1) {
hostaddr = inet_addr(argv[1]); hostaddr = inet_addr(argv[1]);
} else { }
else {
hostaddr = htonl(0x7F000001); hostaddr = htonl(0x7F000001);
} }
if (argc > 2) { if(argc > 2) {
username = argv[2]; username = argv[2];
} }
if (argc > 3) { if(argc > 3) {
password = argv[3]; password = argv[3];
} }
if(argc > 4) { if(argc > 4) {
loclfile = argv[4]; loclfile = argv[4];
} }
if (argc > 5) { if(argc > 5) {
scppath = argv[5]; scppath = argv[5];
} }
rc = libssh2_init (0); rc = libssh2_init(0);
if (rc != 0) { if(rc != 0) {
fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
return 1; return 1;
} }
local = fopen(loclfile, "rb"); local = fopen(loclfile, "rb");
if (!local) { if(!local) {
fprintf(stderr, "Can't open local file %s\n", loclfile); fprintf(stderr, "Can't open local file %s\n", loclfile);
return -1; return -1;
} }
@ -105,8 +106,8 @@ int main(int argc, char *argv[])
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_port = htons(22); sin.sin_port = htons(22);
sin.sin_addr.s_addr = hostaddr; sin.sin_addr.s_addr = hostaddr;
if (connect(sock, (struct sockaddr*)(&sin), if(connect(sock, (struct sockaddr*)(&sin),
sizeof(struct sockaddr_in)) != 0) { sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n"); fprintf(stderr, "failed to connect!\n");
return -1; return -1;
} }
@ -138,18 +139,20 @@ int main(int argc, char *argv[])
} }
fprintf(stderr, "\n"); fprintf(stderr, "\n");
if (auth_pw) { if(auth_pw) {
/* We could authenticate via password */ /* We could authenticate via password */
if (libssh2_userauth_password(session, username, password)) { if(libssh2_userauth_password(session, username, password)) {
fprintf(stderr, "Authentication by password failed.\n"); fprintf(stderr, "Authentication by password failed.\n");
goto shutdown; goto shutdown;
} }
} else { }
else {
/* Or by public key */ /* Or by public key */
if (libssh2_userauth_publickey_fromfile(session, username, #define HOME "/home/username/"
"/home/username/.ssh/id_rsa.pub", if(libssh2_userauth_publickey_fromfile(session, username,
"/home/username/.ssh/id_rsa", HOME ".ssh/id_rsa.pub",
password)) { HOME ".ssh/id_rsa",
password)) {
fprintf(stderr, "\tAuthentication by public key failed\n"); fprintf(stderr, "\tAuthentication by public key failed\n");
goto shutdown; goto shutdown;
} }
@ -159,7 +162,7 @@ int main(int argc, char *argv[])
channel = libssh2_scp_send(session, scppath, fileinfo.st_mode & 0777, channel = libssh2_scp_send(session, scppath, fileinfo.st_mode & 0777,
(unsigned long)fileinfo.st_size); (unsigned long)fileinfo.st_size);
if (!channel) { if(!channel) {
char *errmsg; char *errmsg;
int errlen; int errlen;
int err = libssh2_session_last_error(session, &errmsg, &errlen, 0); int err = libssh2_session_last_error(session, &errmsg, &errlen, 0);
@ -170,7 +173,7 @@ int main(int argc, char *argv[])
fprintf(stderr, "SCP session waiting to send file\n"); fprintf(stderr, "SCP session waiting to send file\n");
do { do {
nread = fread(mem, 1, sizeof(mem), local); nread = fread(mem, 1, sizeof(mem), local);
if (nread <= 0) { if(nread <= 0) {
/* end of file */ /* end of file */
break; break;
} }
@ -179,7 +182,7 @@ int main(int argc, char *argv[])
do { do {
/* write the same data over and over, until error or completion */ /* write the same data over and over, until error or completion */
rc = libssh2_channel_write(channel, ptr, nread); rc = libssh2_channel_write(channel, ptr, nread);
if (rc < 0) { if(rc < 0) {
fprintf(stderr, "ERROR %d\n", rc); fprintf(stderr, "ERROR %d\n", rc);
break; break;
} }
@ -188,9 +191,9 @@ int main(int argc, char *argv[])
ptr += rc; ptr += rc;
nread -= rc; nread -= rc;
} }
} while (nread); } while(nread);
} while (1); } while(1);
fprintf(stderr, "Sending EOF\n"); fprintf(stderr, "Sending EOF\n");
libssh2_channel_send_eof(channel); libssh2_channel_send_eof(channel);
@ -207,7 +210,7 @@ int main(int argc, char *argv[])
shutdown: shutdown:
if(session) { if(session) {
libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing"); libssh2_session_disconnect(session, "Normal Shutdown");
libssh2_session_free(session); libssh2_session_free(session);
} }
#ifdef WIN32 #ifdef WIN32
@ -215,7 +218,7 @@ int main(int argc, char *argv[])
#else #else
close(sock); close(sock);
#endif #endif
if (local) if(local)
fclose(local); fclose(local);
fprintf(stderr, "all done\n"); fprintf(stderr, "all done\n");

Просмотреть файл

@ -73,10 +73,10 @@ int main(int argc, char *argv[])
const char *fingerprint; const char *fingerprint;
LIBSSH2_SESSION *session = NULL; LIBSSH2_SESSION *session = NULL;
LIBSSH2_CHANNEL *channel; LIBSSH2_CHANNEL *channel;
const char *username="username"; const char *username = "username";
const char *password="password"; const char *password = "password";
const char *loclfile="scp_write.c"; const char *loclfile = "scp_write.c";
const char *scppath="/tmp/TEST"; const char *scppath = "/tmp/TEST";
FILE *local; FILE *local;
int rc; int rc;
char mem[1024*100]; char mem[1024*100];
@ -92,39 +92,40 @@ int main(int argc, char *argv[])
WSADATA wsadata; WSADATA wsadata;
int err; int err;
err = WSAStartup(MAKEWORD(2,0), &wsadata); err = WSAStartup(MAKEWORD(2, 0), &wsadata);
if (err != 0) { if(err != 0) {
fprintf(stderr, "WSAStartup failed with error: %d\n", err); fprintf(stderr, "WSAStartup failed with error: %d\n", err);
return 1; return 1;
} }
#endif #endif
if (argc > 1) { if(argc > 1) {
hostaddr = inet_addr(argv[1]); hostaddr = inet_addr(argv[1]);
} else { }
else {
hostaddr = htonl(0x7F000001); hostaddr = htonl(0x7F000001);
} }
if (argc > 2) { if(argc > 2) {
username = argv[2]; username = argv[2];
} }
if (argc > 3) { if(argc > 3) {
password = argv[3]; password = argv[3];
} }
if(argc > 4) { if(argc > 4) {
loclfile = argv[4]; loclfile = argv[4];
} }
if (argc > 5) { if(argc > 5) {
scppath = argv[5]; scppath = argv[5];
} }
rc = libssh2_init (0); rc = libssh2_init(0);
if (rc != 0) { if(rc != 0) {
fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
return 1; return 1;
} }
local = fopen(loclfile, "rb"); local = fopen(loclfile, "rb");
if (!local) { if(!local) {
fprintf(stderr, "Can't local file %s\n", loclfile); fprintf(stderr, "Can't local file %s\n", loclfile);
return -1; return -1;
} }
@ -140,8 +141,8 @@ int main(int argc, char *argv[])
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_port = htons(22); sin.sin_port = htons(22);
sin.sin_addr.s_addr = hostaddr; sin.sin_addr.s_addr = hostaddr;
if (connect(sock, (struct sockaddr*)(&sin), if(connect(sock, (struct sockaddr*)(&sin),
sizeof(struct sockaddr_in)) != 0) { sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n"); fprintf(stderr, "failed to connect!\n");
return -1; return -1;
} }
@ -158,8 +159,8 @@ int main(int argc, char *argv[])
/* ... start it up. This will trade welcome banners, exchange keys, /* ... start it up. This will trade welcome banners, exchange keys,
* and setup crypto, compression, and MAC layers * and setup crypto, compression, and MAC layers
*/ */
while ((rc = libssh2_session_handshake(session, sock)) while((rc = libssh2_session_handshake(session, sock))
== LIBSSH2_ERROR_EAGAIN); == LIBSSH2_ERROR_EAGAIN);
if(rc) { if(rc) {
fprintf(stderr, "Failure establishing SSH session: %d\n", rc); fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
return -1; return -1;
@ -177,21 +178,24 @@ int main(int argc, char *argv[])
} }
fprintf(stderr, "\n"); fprintf(stderr, "\n");
if (auth_pw) { if(auth_pw) {
/* We could authenticate via password */ /* We could authenticate via password */
while ((rc = libssh2_userauth_password(session, username, password)) == while((rc = libssh2_userauth_password(session, username, password)) ==
LIBSSH2_ERROR_EAGAIN); LIBSSH2_ERROR_EAGAIN);
if (rc) { if(rc) {
fprintf(stderr, "Authentication by password failed.\n"); fprintf(stderr, "Authentication by password failed.\n");
goto shutdown; goto shutdown;
} }
} else { }
else {
/* Or by public key */ /* Or by public key */
while ((rc = libssh2_userauth_publickey_fromfile(session, username, #define HOME "/home/username/"
"/home/username/.ssh/id_rsa.pub", while((rc = libssh2_userauth_publickey_fromfile(session, username,
"/home/username/.ssh/id_rsa", HOME ".ssh/id_rsa.pub",
password)) == LIBSSH2_ERROR_EAGAIN); HOME ".ssh/id_rsa",
if (rc) { password)) ==
LIBSSH2_ERROR_EAGAIN);
if(rc) {
fprintf(stderr, "\tAuthentication by public key failed\n"); fprintf(stderr, "\tAuthentication by public key failed\n");
goto shutdown; goto shutdown;
} }
@ -202,21 +206,21 @@ int main(int argc, char *argv[])
channel = libssh2_scp_send(session, scppath, fileinfo.st_mode & 0777, channel = libssh2_scp_send(session, scppath, fileinfo.st_mode & 0777,
(unsigned long)fileinfo.st_size); (unsigned long)fileinfo.st_size);
if ((!channel) && (libssh2_session_last_errno(session) != if((!channel) && (libssh2_session_last_errno(session) !=
LIBSSH2_ERROR_EAGAIN)) { LIBSSH2_ERROR_EAGAIN)) {
char *err_msg; char *err_msg;
libssh2_session_last_error(session, &err_msg, NULL, 0); libssh2_session_last_error(session, &err_msg, NULL, 0);
fprintf(stderr, "%s\n", err_msg); fprintf(stderr, "%s\n", err_msg);
goto shutdown; goto shutdown;
} }
} while (!channel); } while(!channel);
fprintf(stderr, "SCP session waiting to send file\n"); fprintf(stderr, "SCP session waiting to send file\n");
start = time(NULL); start = time(NULL);
do { do {
nread = fread(mem, 1, sizeof(mem), local); nread = fread(mem, 1, sizeof(mem), local);
if (nread <= 0) { if(nread <= 0) {
/* end of file */ /* end of file */
break; break;
} }
@ -226,12 +230,12 @@ int main(int argc, char *argv[])
prev = 0; prev = 0;
do { do {
while ((rc = libssh2_channel_write(channel, ptr, nread)) == while((rc = libssh2_channel_write(channel, ptr, nread)) ==
LIBSSH2_ERROR_EAGAIN) { LIBSSH2_ERROR_EAGAIN) {
waitsocket(sock, session); waitsocket(sock, session);
prev = 0; prev = 0;
} }
if (rc < 0) { if(rc < 0) {
fprintf(stderr, "ERROR %d total %ld / %d prev %d\n", rc, fprintf(stderr, "ERROR %d total %ld / %d prev %d\n", rc,
total, (int)nread, (int)prev); total, (int)nread, (int)prev);
break; break;
@ -243,8 +247,8 @@ int main(int argc, char *argv[])
nread -= rc; nread -= rc;
ptr += rc; ptr += rc;
} }
} while (nread); } while(nread);
} while (!nread); /* only continue if nread was drained */ } while(!nread); /* only continue if nread was drained */
duration = (int)(time(NULL)-start); duration = (int)(time(NULL)-start);
@ -252,22 +256,22 @@ int main(int argc, char *argv[])
total, duration, total/(double)duration); total, duration, total/(double)duration);
fprintf(stderr, "Sending EOF\n"); fprintf(stderr, "Sending EOF\n");
while (libssh2_channel_send_eof(channel) == LIBSSH2_ERROR_EAGAIN); while(libssh2_channel_send_eof(channel) == LIBSSH2_ERROR_EAGAIN);
fprintf(stderr, "Waiting for EOF\n"); fprintf(stderr, "Waiting for EOF\n");
while (libssh2_channel_wait_eof(channel) == LIBSSH2_ERROR_EAGAIN); while(libssh2_channel_wait_eof(channel) == LIBSSH2_ERROR_EAGAIN);
fprintf(stderr, "Waiting for channel to close\n"); fprintf(stderr, "Waiting for channel to close\n");
while (libssh2_channel_wait_closed(channel) == LIBSSH2_ERROR_EAGAIN); while(libssh2_channel_wait_closed(channel) == LIBSSH2_ERROR_EAGAIN);
libssh2_channel_free(channel); libssh2_channel_free(channel);
channel = NULL; channel = NULL;
shutdown: shutdown:
while (libssh2_session_disconnect(session, while(libssh2_session_disconnect(session,
"Normal Shutdown, Thank you for playing") == "Normal Shutdown,") ==
LIBSSH2_ERROR_EAGAIN); LIBSSH2_ERROR_EAGAIN);
libssh2_session_free(session); libssh2_session_free(session);
#ifdef WIN32 #ifdef WIN32

Просмотреть файл

@ -37,18 +37,19 @@
#include <ctype.h> #include <ctype.h>
const char *keyfile1="~/.ssh/id_rsa.pub"; const char *keyfile1 = "~/.ssh/id_rsa.pub";
const char *keyfile2="~/.ssh/id_rsa"; const char *keyfile2 = "~/.ssh/id_rsa";
const char *username="username"; const char *username = "username";
const char *password="password"; const char *password = "password";
const char *sftppath="/tmp/TEST"; const char *sftppath = "/tmp/TEST";
static void kbd_callback(const char *name, int name_len, static void kbd_callback(const char *name, int name_len,
const char *instruction, int instruction_len, int num_prompts, const char *instruction, int instruction_len,
const LIBSSH2_USERAUTH_KBDINT_PROMPT *prompts, int num_prompts,
LIBSSH2_USERAUTH_KBDINT_RESPONSE *responses, const LIBSSH2_USERAUTH_KBDINT_PROMPT *prompts,
void **abstract) LIBSSH2_USERAUTH_KBDINT_RESPONSE *responses,
void **abstract)
{ {
int i; int i;
size_t n; size_t n;
@ -67,7 +68,7 @@ static void kbd_callback(const char *name, int name_len,
fprintf(stderr, "Number of prompts: %d\n\n", num_prompts); fprintf(stderr, "Number of prompts: %d\n\n", num_prompts);
for (i = 0; i < num_prompts; i++) { for(i = 0; i < num_prompts; i++) {
fprintf(stderr, "Prompt %d from server: '", i); fprintf(stderr, "Prompt %d from server: '", i);
fwrite(prompts[i].text, 1, prompts[i].length, stderr); fwrite(prompts[i].text, 1, prompts[i].length, stderr);
fprintf(stderr, "'\n"); fprintf(stderr, "'\n");
@ -75,7 +76,7 @@ static void kbd_callback(const char *name, int name_len,
fprintf(stderr, "Please type response: "); fprintf(stderr, "Please type response: ");
fgets(buf, sizeof(buf), stdin); fgets(buf, sizeof(buf), stdin);
n = strlen(buf); n = strlen(buf);
while (n > 0 && strchr("\r\n", buf[n - 1])) while(n > 0 && strchr("\r\n", buf[n - 1]))
n--; n--;
buf[n] = 0; buf[n] = 0;
@ -108,16 +109,17 @@ int main(int argc, char *argv[])
WSADATA wsadata; WSADATA wsadata;
int err; int err;
err = WSAStartup(MAKEWORD(2,0), &wsadata); err = WSAStartup(MAKEWORD(2, 0), &wsadata);
if (err != 0) { if(err != 0) {
fprintf(stderr, "WSAStartup failed with error: %d\n", err); fprintf(stderr, "WSAStartup failed with error: %d\n", err);
return 1; return 1;
} }
#endif #endif
if (argc > 1) { if(argc > 1) {
hostaddr = inet_addr(argv[1]); hostaddr = inet_addr(argv[1]);
} else { }
else {
hostaddr = htonl(0x7F000001); hostaddr = htonl(0x7F000001);
} }
@ -131,8 +133,8 @@ int main(int argc, char *argv[])
sftppath = argv[4]; sftppath = argv[4];
} }
rc = libssh2_init (0); rc = libssh2_init(0);
if (rc != 0) { if(rc != 0) {
fprintf(stderr, "libssh2 initialization failed (%d)\n", rc); fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
return 1; return 1;
} }
@ -146,8 +148,8 @@ int main(int argc, char *argv[])
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_port = htons(22); sin.sin_port = htons(22);
sin.sin_addr.s_addr = hostaddr; sin.sin_addr.s_addr = hostaddr;
if (connect(sock, (struct sockaddr*)(&sin), if(connect(sock, (struct sockaddr*)(&sin),
sizeof(struct sockaddr_in)) != 0) { sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n"); fprintf(stderr, "failed to connect!\n");
return -1; return -1;
} }
@ -185,54 +187,61 @@ int main(int argc, char *argv[])
/* check what authentication methods are available */ /* check what authentication methods are available */
userauthlist = libssh2_userauth_list(session, username, strlen(username)); userauthlist = libssh2_userauth_list(session, username, strlen(username));
fprintf(stderr, "Authentication methods: %s\n", userauthlist); fprintf(stderr, "Authentication methods: %s\n", userauthlist);
if (strstr(userauthlist, "password") != NULL) { if(strstr(userauthlist, "password") != NULL) {
auth_pw |= 1; auth_pw |= 1;
} }
if (strstr(userauthlist, "keyboard-interactive") != NULL) { if(strstr(userauthlist, "keyboard-interactive") != NULL) {
auth_pw |= 2; auth_pw |= 2;
} }
if (strstr(userauthlist, "publickey") != NULL) { if(strstr(userauthlist, "publickey") != NULL) {
auth_pw |= 4; auth_pw |= 4;
} }
/* if we got an 4. argument we set this option if supported */ /* if we got an 4. argument we set this option if supported */
if(argc > 5) { if(argc > 5) {
if ((auth_pw & 1) && !strcasecmp(argv[5], "-p")) { if((auth_pw & 1) && !strcasecmp(argv[5], "-p")) {
auth_pw = 1; auth_pw = 1;
} }
if ((auth_pw & 2) && !strcasecmp(argv[5], "-i")) { if((auth_pw & 2) && !strcasecmp(argv[5], "-i")) {
auth_pw = 2; auth_pw = 2;
} }
if ((auth_pw & 4) && !strcasecmp(argv[5], "-k")) { if((auth_pw & 4) && !strcasecmp(argv[5], "-k")) {
auth_pw = 4; auth_pw = 4;
} }
} }
if (auth_pw & 1) { if(auth_pw & 1) {
/* We could authenticate via password */ /* We could authenticate via password */
if (libssh2_userauth_password(session, username, password)) { if(libssh2_userauth_password(session, username, password)) {
fprintf(stderr, "Authentication by password failed.\n"); fprintf(stderr, "Authentication by password failed.\n");
goto shutdown; goto shutdown;
} }
} else if (auth_pw & 2) { }
else if(auth_pw & 2) {
/* Or via keyboard-interactive */ /* Or via keyboard-interactive */
if (libssh2_userauth_keyboard_interactive(session, username, &kbd_callback) ) { if(libssh2_userauth_keyboard_interactive(session, username,
&kbd_callback) ) {
fprintf(stderr, fprintf(stderr,
"\tAuthentication by keyboard-interactive failed!\n"); "\tAuthentication by keyboard-interactive failed!\n");
goto shutdown; goto shutdown;
} else { }
else {
fprintf(stderr, fprintf(stderr,
"\tAuthentication by keyboard-interactive succeeded.\n"); "\tAuthentication by keyboard-interactive succeeded.\n");
} }
} else if (auth_pw & 4) { }
else if(auth_pw & 4) {
/* Or by public key */ /* Or by public key */
if (libssh2_userauth_publickey_fromfile(session, username, keyfile1, keyfile2, password)) { if(libssh2_userauth_publickey_fromfile(session, username, keyfile1,
keyfile2, password)) {
fprintf(stderr, "\tAuthentication by public key failed!\n"); fprintf(stderr, "\tAuthentication by public key failed!\n");
goto shutdown; goto shutdown;
} else { }
else {
fprintf(stderr, "\tAuthentication by public key succeeded.\n"); fprintf(stderr, "\tAuthentication by public key succeeded.\n");
} }
} else { }
else {
fprintf(stderr, "No supported authentication methods found!\n"); fprintf(stderr, "No supported authentication methods found!\n");
goto shutdown; goto shutdown;
} }
@ -240,7 +249,7 @@ int main(int argc, char *argv[])
fprintf(stderr, "libssh2_sftp_init()!\n"); fprintf(stderr, "libssh2_sftp_init()!\n");
sftp_session = libssh2_sftp_init(session); sftp_session = libssh2_sftp_init(session);
if (!sftp_session) { if(!sftp_session) {
fprintf(stderr, "Unable to init SFTP session\n"); fprintf(stderr, "Unable to init SFTP session\n");
goto shutdown; goto shutdown;
} }
@ -250,7 +259,7 @@ int main(int argc, char *argv[])
sftp_handle = sftp_handle =
libssh2_sftp_open(sftp_session, sftppath, LIBSSH2_FXF_READ, 0); libssh2_sftp_open(sftp_session, sftppath, LIBSSH2_FXF_READ, 0);
if (!sftp_handle) { if(!sftp_handle) {
fprintf(stderr, "Unable to open file with SFTP: %ld\n", fprintf(stderr, "Unable to open file with SFTP: %ld\n",
libssh2_sftp_last_error(sftp_session)); libssh2_sftp_last_error(sftp_session));
goto shutdown; goto shutdown;
@ -262,19 +271,20 @@ int main(int argc, char *argv[])
/* loop until we fail */ /* loop until we fail */
fprintf(stderr, "libssh2_sftp_read()!\n"); fprintf(stderr, "libssh2_sftp_read()!\n");
rc = libssh2_sftp_read(sftp_handle, mem, sizeof(mem)); rc = libssh2_sftp_read(sftp_handle, mem, sizeof(mem));
if (rc > 0) { if(rc > 0) {
write(1, mem, rc); write(1, mem, rc);
} else { }
else {
break; break;
} }
} while (1); } while(1);
libssh2_sftp_close(sftp_handle); libssh2_sftp_close(sftp_handle);
libssh2_sftp_shutdown(sftp_session); libssh2_sftp_shutdown(sftp_session);
shutdown: shutdown:
libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing"); libssh2_session_disconnect(session, "Normal Shutdown");
libssh2_session_free(session); libssh2_session_free(session);
#ifdef WIN32 #ifdef WIN32

Просмотреть файл

@ -79,10 +79,10 @@ int main(int argc, char *argv[])
struct sockaddr_in sin; struct sockaddr_in sin;
const char *fingerprint; const char *fingerprint;
LIBSSH2_SESSION *session; LIBSSH2_SESSION *session;
const char *username="username"; const char *username = "username";
const char *password="password"; const char *password = "password";
const char *sftppath="/tmp/TEST"; /* source path */ const char *sftppath = "/tmp/TEST"; /* source path */
const char *dest="/tmp/TEST2"; /* destination path */ const char *dest = "/tmp/TEST2"; /* destination path */
int rc; int rc;
LIBSSH2_SFTP *sftp_session; LIBSSH2_SFTP *sftp_session;
LIBSSH2_SFTP_HANDLE *sftp_handle; LIBSSH2_SFTP_HANDLE *sftp_handle;
@ -96,16 +96,16 @@ int main(int argc, char *argv[])
WSADATA wsadata; WSADATA wsadata;
int err; int err;
err = WSAStartup(MAKEWORD(2,0), &wsadata); err = WSAStartup(MAKEWORD(2, 0), &wsadata);
if (err != 0) { if(err != 0) {
fprintf(stderr, "WSAStartup failed with error: %d\n", err); fprintf(stderr, "WSAStartup failed with error: %d\n", err);
return 1; return 1;
} }
#endif #endif
rc = libssh2_init (0); rc = libssh2_init(0);
if (rc != 0) { if(rc != 0) {
fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
return 1; return 1;
} }
@ -118,7 +118,7 @@ int main(int argc, char *argv[])
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_port = htons(22); sin.sin_port = htons(22);
sin.sin_addr.s_addr = htonl(0x7F000001); sin.sin_addr.s_addr = htonl(0x7F000001);
if (connect(sock, (struct sockaddr*)(&sin), if(connect(sock, (struct sockaddr*)(&sin),
sizeof(struct sockaddr_in)) != 0) { sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n"); fprintf(stderr, "failed to connect!\n");
return -1; return -1;
@ -172,25 +172,26 @@ int main(int argc, char *argv[])
goto shutdown; goto shutdown;
} }
if (auth_pw) { if(auth_pw) {
/* We could authenticate via password */ /* We could authenticate via password */
while ((rc = libssh2_userauth_password(session, username, password)) while((rc = libssh2_userauth_password(session, username, password))
== LIBSSH2_ERROR_EAGAIN); == LIBSSH2_ERROR_EAGAIN);
if (rc) { if(rc) {
fprintf(stderr, "Authentication by password failed.\n"); fprintf(stderr, "Authentication by password failed.\n");
goto shutdown; goto shutdown;
} }
} else { }
else {
/* Or by public key */ /* Or by public key */
while ((rc = while((rc =
libssh2_userauth_publickey_fromfile(session, username, libssh2_userauth_publickey_fromfile(session, username,
"/home/username/" "/home/username/"
".ssh/id_rsa.pub", ".ssh/id_rsa.pub",
"/home/username/" "/home/username/"
".ssh/id_rsa", ".ssh/id_rsa",
password)) == password)) ==
LIBSSH2_ERROR_EAGAIN); LIBSSH2_ERROR_EAGAIN);
if (rc) { if(rc) {
fprintf(stderr, "\tAuthentication by public key failed\n"); fprintf(stderr, "\tAuthentication by public key failed\n");
goto shutdown; goto shutdown;
} }
@ -210,15 +211,15 @@ int main(int argc, char *argv[])
goto shutdown; goto shutdown;
} }
} }
} while (!sftp_session); } while(!sftp_session);
/* Request a file via SFTP */ /* Request a file via SFTP */
do { do {
sftp_handle = libssh2_sftp_open(sftp_session, sftppath, sftp_handle = libssh2_sftp_open(sftp_session, sftppath,
LIBSSH2_FXF_READ, 0); LIBSSH2_FXF_READ, 0);
if (!sftp_handle) { if(!sftp_handle) {
if (libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN) { if(libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN) {
fprintf(stderr, "Unable to open file with SFTP\n"); fprintf(stderr, "Unable to open file with SFTP\n");
goto shutdown; goto shutdown;
} }
@ -227,7 +228,7 @@ int main(int argc, char *argv[])
waitsocket(sock, session); /* now we wait */ waitsocket(sock, session); /* now we wait */
} }
} }
} while (!sftp_handle); } while(!sftp_handle);
fprintf(stderr, "libssh2_sftp_open() is done, now receive data!\n"); fprintf(stderr, "libssh2_sftp_open() is done, now receive data!\n");
do { do {
@ -243,7 +244,7 @@ int main(int argc, char *argv[])
/* write to temporary storage area */ /* write to temporary storage area */
fwrite(mem, rc, 1, tempstorage); fwrite(mem, rc, 1, tempstorage);
} }
} while (rc > 0); } while(rc > 0);
if(rc != LIBSSH2_ERROR_EAGAIN) { if(rc != LIBSSH2_ERROR_EAGAIN) {
/* error or end of file */ /* error or end of file */
@ -259,7 +260,7 @@ int main(int argc, char *argv[])
FD_SET(sock, &fd2); FD_SET(sock, &fd2);
/* wait for readable or writeable */ /* wait for readable or writeable */
rc = select(sock+1, &fd, &fd2, NULL, &timeout); rc = select(sock + 1, &fd, &fd2, NULL, &timeout);
if(rc <= 0) { if(rc <= 0) {
/* negative is error /* negative is error
0 is timeout */ 0 is timeout */
@ -267,7 +268,7 @@ int main(int argc, char *argv[])
break; break;
} }
} while (1); } while(1);
libssh2_sftp_close(sftp_handle); libssh2_sftp_close(sftp_handle);
fclose(tempstorage); fclose(tempstorage);
@ -303,7 +304,7 @@ int main(int argc, char *argv[])
nread); nread);
ptr += rc; ptr += rc;
nread -= nread; nread -= nread;
} while (rc >= 0); } while(rc >= 0);
if(rc != LIBSSH2_ERROR_EAGAIN) { if(rc != LIBSSH2_ERROR_EAGAIN) {
/* error or end of file */ /* error or end of file */
@ -319,7 +320,7 @@ int main(int argc, char *argv[])
FD_SET(sock, &fd2); FD_SET(sock, &fd2);
/* wait for readable or writeable */ /* wait for readable or writeable */
rc = select(sock+1, &fd, &fd2, NULL, &timeout); rc = select(sock + 1, &fd, &fd2, NULL, &timeout);
if(rc <= 0) { if(rc <= 0) {
/* negative is error /* negative is error
0 is timeout */ 0 is timeout */
@ -327,7 +328,7 @@ int main(int argc, char *argv[])
rc); rc);
break; break;
} }
} while (1); } while(1);
fprintf(stderr, "SFTP upload done!\n"); fprintf(stderr, "SFTP upload done!\n");
} }
else { else {
@ -339,7 +340,7 @@ int main(int argc, char *argv[])
shutdown: shutdown:
libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing"); libssh2_session_disconnect(session, "Normal Shutdown");
libssh2_session_free(session); libssh2_session_free(session);
#ifdef WIN32 #ifdef WIN32
@ -347,7 +348,7 @@ int main(int argc, char *argv[])
#else #else
close(sock); close(sock);
#endif #endif
if (tempstorage) if(tempstorage)
fclose(tempstorage); fclose(tempstorage);
fprintf(stderr, "all done\n"); fprintf(stderr, "all done\n");

Просмотреть файл

@ -40,10 +40,10 @@ int main(int argc, char *argv[])
struct sockaddr_in sin; struct sockaddr_in sin;
const char *fingerprint; const char *fingerprint;
LIBSSH2_SESSION *session; LIBSSH2_SESSION *session;
const char *username="username"; const char *username = "username";
const char *password="password"; const char *password = "password";
const char *loclfile="sftp_write.c"; const char *loclfile = "sftp_write.c";
const char *sftppath="/tmp/TEST"; const char *sftppath = "/tmp/TEST";
int rc; int rc;
FILE *local; FILE *local;
LIBSSH2_SFTP *sftp_session; LIBSSH2_SFTP *sftp_session;
@ -57,16 +57,17 @@ int main(int argc, char *argv[])
WSADATA wsadata; WSADATA wsadata;
int err; int err;
err = WSAStartup(MAKEWORD(2,0), &wsadata); err = WSAStartup(MAKEWORD(2, 0), &wsadata);
if (err != 0) { if(err) {
fprintf(stderr, "WSAStartup failed with error: %d\n", err); fprintf(stderr, "WSAStartup failed with error: %d\n", err);
return 1; return 1;
} }
#endif #endif
if (argc > 1) { if(argc > 1) {
hostaddr = inet_addr(argv[1]); hostaddr = inet_addr(argv[1]);
} else { }
else {
hostaddr = htonl(0x7F000001); hostaddr = htonl(0x7F000001);
} }
@ -83,14 +84,14 @@ int main(int argc, char *argv[])
sftppath = argv[5]; sftppath = argv[5];
} }
rc = libssh2_init (0); rc = libssh2_init(0);
if (rc != 0) { if(rc) {
fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
return 1; return 1;
} }
local = fopen(loclfile, "rb"); local = fopen(loclfile, "rb");
if (!local) { if(!local) {
fprintf(stderr, "Can't open local file %s\n", loclfile); fprintf(stderr, "Can't open local file %s\n", loclfile);
return -1; return -1;
} }
@ -104,8 +105,8 @@ int main(int argc, char *argv[])
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_port = htons(22); sin.sin_port = htons(22);
sin.sin_addr.s_addr = hostaddr; sin.sin_addr.s_addr = hostaddr;
if (connect(sock, (struct sockaddr*)(&sin), if(connect(sock, (struct sockaddr*)(&sin),
sizeof(struct sockaddr_in)) != 0) { sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n"); fprintf(stderr, "failed to connect!\n");
return -1; return -1;
} }
@ -140,18 +141,20 @@ int main(int argc, char *argv[])
} }
fprintf(stderr, "\n"); fprintf(stderr, "\n");
if (auth_pw) { if(auth_pw) {
/* We could authenticate via password */ /* We could authenticate via password */
if (libssh2_userauth_password(session, username, password)) { if(libssh2_userauth_password(session, username, password)) {
fprintf(stderr, "Authentication by password failed.\n"); fprintf(stderr, "Authentication by password failed.\n");
goto shutdown; goto shutdown;
} }
} else { }
else {
/* Or by public key */ /* Or by public key */
if (libssh2_userauth_publickey_fromfile(session, username, #define HOME "/home/username/"
"/home/username/.ssh/id_rsa.pub", if(libssh2_userauth_publickey_fromfile(session, username,
"/home/username/.ssh/id_rsa", HOME ".ssh/id_rsa.pub",
password)) { HOME ".ssh/id_rsa",
password)) {
fprintf(stderr, "\tAuthentication by public key failed\n"); fprintf(stderr, "\tAuthentication by public key failed\n");
goto shutdown; goto shutdown;
} }
@ -160,7 +163,7 @@ int main(int argc, char *argv[])
fprintf(stderr, "libssh2_sftp_init()!\n"); fprintf(stderr, "libssh2_sftp_init()!\n");
sftp_session = libssh2_sftp_init(session); sftp_session = libssh2_sftp_init(session);
if (!sftp_session) { if(!sftp_session) {
fprintf(stderr, "Unable to init SFTP session\n"); fprintf(stderr, "Unable to init SFTP session\n");
goto shutdown; goto shutdown;
} }
@ -173,7 +176,7 @@ int main(int argc, char *argv[])
LIBSSH2_FXF_WRITE|LIBSSH2_FXF_READ, LIBSSH2_FXF_WRITE|LIBSSH2_FXF_READ,
LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR| LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR|
LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH); LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH);
if (!sftp_handle) { if(!sftp_handle) {
fprintf(stderr, "Unable to open file with SFTP\n"); fprintf(stderr, "Unable to open file with SFTP\n");
goto shutdown; goto shutdown;
} }
@ -188,14 +191,14 @@ int main(int argc, char *argv[])
fprintf(stderr, "libssh2_sftp_open() a handle for APPEND\n"); fprintf(stderr, "libssh2_sftp_open() a handle for APPEND\n");
if (!sftp_handle) { if(!sftp_handle) {
fprintf(stderr, "Unable to open file with SFTP\n"); fprintf(stderr, "Unable to open file with SFTP\n");
goto shutdown; goto shutdown;
} }
fprintf(stderr, "libssh2_sftp_open() is done, now send data!\n"); fprintf(stderr, "libssh2_sftp_open() is done, now send data!\n");
do { do {
nread = fread(mem, 1, sizeof(mem), local); nread = fread(mem, 1, sizeof(mem), local);
if (nread <= 0) { if(nread <= 0) {
/* end of file */ /* end of file */
break; break;
} }
@ -208,9 +211,9 @@ int main(int argc, char *argv[])
break; break;
ptr += rc; ptr += rc;
nread -= rc; nread -= rc;
} while (nread); } while(nread);
} while (rc > 0); } while(rc > 0);
libssh2_sftp_close(sftp_handle); libssh2_sftp_close(sftp_handle);
libssh2_sftp_shutdown(sftp_session); libssh2_sftp_shutdown(sftp_session);
@ -225,7 +228,7 @@ shutdown:
#else #else
close(sock); close(sock);
#endif #endif
if (local) if(local)
fclose(local); fclose(local);
fprintf(stderr, "all done\n"); fprintf(stderr, "all done\n");

Просмотреть файл

@ -40,9 +40,9 @@ int main(int argc, char *argv[])
struct sockaddr_in sin; struct sockaddr_in sin;
const char *fingerprint; const char *fingerprint;
LIBSSH2_SESSION *session; LIBSSH2_SESSION *session;
const char *username="username"; const char *username = "username";
const char *password="password"; const char *password = "password";
const char *sftppath="/tmp/sftp_mkdir"; const char *sftppath = "/tmp/sftp_mkdir";
int rc; int rc;
LIBSSH2_SFTP *sftp_session; LIBSSH2_SFTP *sftp_session;
@ -50,16 +50,17 @@ int main(int argc, char *argv[])
WSADATA wsadata; WSADATA wsadata;
int err; int err;
err = WSAStartup(MAKEWORD(2,0), &wsadata); err = WSAStartup(MAKEWORD(2, 0), &wsadata);
if (err != 0) { if(err != 0) {
fprintf(stderr, "WSAStartup failed with error: %d\n", err); fprintf(stderr, "WSAStartup failed with error: %d\n", err);
return 1; return 1;
} }
#endif #endif
if (argc > 1) { if(argc > 1) {
hostaddr = inet_addr(argv[1]); hostaddr = inet_addr(argv[1]);
} else { }
else {
hostaddr = htonl(0x7F000001); hostaddr = htonl(0x7F000001);
} }
@ -73,9 +74,9 @@ int main(int argc, char *argv[])
sftppath = argv[4]; sftppath = argv[4];
} }
rc = libssh2_init (0); rc = libssh2_init(0);
if (rc != 0) { if(rc != 0) {
fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
return 1; return 1;
} }
@ -88,7 +89,7 @@ int main(int argc, char *argv[])
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_port = htons(22); sin.sin_port = htons(22);
sin.sin_addr.s_addr = hostaddr; sin.sin_addr.s_addr = hostaddr;
if (connect(sock, (struct sockaddr*)(&sin), if(connect(sock, (struct sockaddr*)(&sin),
sizeof(struct sockaddr_in)) != 0) { sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n"); fprintf(stderr, "failed to connect!\n");
return -1; return -1;
@ -121,15 +122,16 @@ int main(int argc, char *argv[])
} }
fprintf(stderr, "\n"); fprintf(stderr, "\n");
if (auth_pw) { if(auth_pw) {
/* We could authenticate via password */ /* We could authenticate via password */
if (libssh2_userauth_password(session, username, password)) { if(libssh2_userauth_password(session, username, password)) {
fprintf(stderr, "Authentication by password failed.\n"); fprintf(stderr, "Authentication by password failed.\n");
goto shutdown; goto shutdown;
} }
} else { }
else {
/* Or by public key */ /* Or by public key */
if (libssh2_userauth_publickey_fromfile(session, username, if(libssh2_userauth_publickey_fromfile(session, username,
"/home/username/.ssh/id_rsa.pub", "/home/username/.ssh/id_rsa.pub",
"/home/username/.ssh/id_rsa", "/home/username/.ssh/id_rsa",
password)) { password)) {
@ -140,7 +142,7 @@ int main(int argc, char *argv[])
sftp_session = libssh2_sftp_init(session); sftp_session = libssh2_sftp_init(session);
if (!sftp_session) { if(!sftp_session) {
fprintf(stderr, "Unable to init SFTP session\n"); fprintf(stderr, "Unable to init SFTP session\n");
goto shutdown; goto shutdown;
} }
@ -161,7 +163,7 @@ int main(int argc, char *argv[])
shutdown: shutdown:
libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing"); libssh2_session_disconnect(session, "Normal Shutdown");
libssh2_session_free(session); libssh2_session_free(session);
#ifdef WIN32 #ifdef WIN32

Просмотреть файл

@ -40,9 +40,9 @@ int main(int argc, char *argv[])
struct sockaddr_in sin; struct sockaddr_in sin;
const char *fingerprint; const char *fingerprint;
LIBSSH2_SESSION *session; LIBSSH2_SESSION *session;
const char *username="username"; const char *username = "username";
const char *password="password"; const char *password = "password";
const char *sftppath="/tmp/sftp_mkdir_nonblock"; const char *sftppath = "/tmp/sftp_mkdir_nonblock";
int rc; int rc;
LIBSSH2_SFTP *sftp_session; LIBSSH2_SFTP *sftp_session;
@ -50,16 +50,17 @@ int main(int argc, char *argv[])
WSADATA wsadata; WSADATA wsadata;
int err; int err;
err = WSAStartup(MAKEWORD(2,0), &wsadata); err = WSAStartup(MAKEWORD(2, 0), &wsadata);
if (err != 0) { if(err != 0) {
fprintf(stderr, "WSAStartup failed with error: %d\n", err); fprintf(stderr, "WSAStartup failed with error: %d\n", err);
return 1; return 1;
} }
#endif #endif
if (argc > 1) { if(argc > 1) {
hostaddr = inet_addr(argv[1]); hostaddr = inet_addr(argv[1]);
} else { }
else {
hostaddr = htonl(0x7F000001); hostaddr = htonl(0x7F000001);
} }
@ -73,9 +74,9 @@ int main(int argc, char *argv[])
sftppath = argv[4]; sftppath = argv[4];
} }
rc = libssh2_init (0); rc = libssh2_init(0);
if (rc != 0) { if(rc != 0) {
fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
return 1; return 1;
} }
@ -88,7 +89,7 @@ int main(int argc, char *argv[])
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_port = htons(22); sin.sin_port = htons(22);
sin.sin_addr.s_addr = hostaddr; sin.sin_addr.s_addr = hostaddr;
if (connect(sock, (struct sockaddr*)(&sin), if(connect(sock, (struct sockaddr*)(&sin),
sizeof(struct sockaddr_in)) != 0) { sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n"); fprintf(stderr, "failed to connect!\n");
return -1; return -1;
@ -121,15 +122,16 @@ int main(int argc, char *argv[])
} }
fprintf(stderr, "\n"); fprintf(stderr, "\n");
if (auth_pw) { if(auth_pw) {
/* We could authenticate via password */ /* We could authenticate via password */
if (libssh2_userauth_password(session, username, password)) { if(libssh2_userauth_password(session, username, password)) {
fprintf(stderr, "Authentication by password failed.\n"); fprintf(stderr, "Authentication by password failed.\n");
goto shutdown; goto shutdown;
} }
} else { }
else {
/* Or by public key */ /* Or by public key */
if (libssh2_userauth_publickey_fromfile(session, username, if(libssh2_userauth_publickey_fromfile(session, username,
"/home/username/.ssh/id_rsa.pub", "/home/username/.ssh/id_rsa.pub",
"/home/username/.ssh/id_rsa", "/home/username/.ssh/id_rsa",
password)) { password)) {
@ -141,7 +143,7 @@ int main(int argc, char *argv[])
fprintf(stderr, "libssh2_sftp_init()!\n"); fprintf(stderr, "libssh2_sftp_init()!\n");
sftp_session = libssh2_sftp_init(session); sftp_session = libssh2_sftp_init(session);
if (!sftp_session) { if(!sftp_session) {
fprintf(stderr, "Unable to init SFTP session\n"); fprintf(stderr, "Unable to init SFTP session\n");
goto shutdown; goto shutdown;
} }
@ -151,7 +153,7 @@ int main(int argc, char *argv[])
fprintf(stderr, "libssh2_sftp_mkdirnb()!\n"); fprintf(stderr, "libssh2_sftp_mkdirnb()!\n");
/* Make a directory via SFTP */ /* Make a directory via SFTP */
while (libssh2_sftp_mkdir(sftp_session, sftppath, while(libssh2_sftp_mkdir(sftp_session, sftppath,
LIBSSH2_SFTP_S_IRWXU| LIBSSH2_SFTP_S_IRWXU|
LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IXGRP| LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IXGRP|
LIBSSH2_SFTP_S_IROTH|LIBSSH2_SFTP_S_IXOTH) LIBSSH2_SFTP_S_IROTH|LIBSSH2_SFTP_S_IXOTH)
@ -161,7 +163,7 @@ int main(int argc, char *argv[])
shutdown: shutdown:
libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing"); libssh2_session_disconnect(session, "Normal Shutdown");
libssh2_session_free(session); libssh2_session_free(session);
#ifdef WIN32 #ifdef WIN32

Просмотреть файл

@ -85,9 +85,9 @@ int main(int argc, char *argv[])
struct sockaddr_in sin; struct sockaddr_in sin;
const char *fingerprint; const char *fingerprint;
LIBSSH2_SESSION *session; LIBSSH2_SESSION *session;
const char *username="username"; const char *username = "username";
const char *password="password"; const char *password = "password";
const char *sftppath="/tmp/TEST"; const char *sftppath = "/tmp/TEST";
#ifdef HAVE_GETTIMEOFDAY #ifdef HAVE_GETTIMEOFDAY
struct timeval start; struct timeval start;
struct timeval end; struct timeval end;
@ -103,32 +103,33 @@ int main(int argc, char *argv[])
WSADATA wsadata; WSADATA wsadata;
int err; int err;
err = WSAStartup(MAKEWORD(2,0), &wsadata); err = WSAStartup(MAKEWORD(2, 0), &wsadata);
if (err != 0) { if(err != 0) {
fprintf(stderr, "WSAStartup failed with error: %d\n", err); fprintf(stderr, "WSAStartup failed with error: %d\n", err);
return 1; return 1;
} }
#endif #endif
if (argc > 1) { if(argc > 1) {
hostaddr = inet_addr(argv[1]); hostaddr = inet_addr(argv[1]);
} else { }
else {
hostaddr = htonl(0x7F000001); hostaddr = htonl(0x7F000001);
} }
if (argc > 2) { if(argc > 2) {
username = argv[2]; username = argv[2];
} }
if (argc > 3) { if(argc > 3) {
password = argv[3]; password = argv[3];
} }
if (argc > 4) { if(argc > 4) {
sftppath = argv[4]; sftppath = argv[4];
} }
rc = libssh2_init (0); rc = libssh2_init(0);
if (rc != 0) { if(rc != 0) {
fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
return 1; return 1;
} }
@ -141,7 +142,7 @@ int main(int argc, char *argv[])
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_port = htons(22); sin.sin_port = htons(22);
sin.sin_addr.s_addr = hostaddr; sin.sin_addr.s_addr = hostaddr;
if (connect(sock, (struct sockaddr*)(&sin), if(connect(sock, (struct sockaddr*)(&sin),
sizeof(struct sockaddr_in)) != 0) { sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n"); fprintf(stderr, "failed to connect!\n");
return -1; return -1;
@ -149,7 +150,7 @@ int main(int argc, char *argv[])
/* Create a session instance */ /* Create a session instance */
session = libssh2_session_init(); session = libssh2_session_init();
if (!session) if(!session)
return -1; return -1;
/* Since we have set non-blocking, tell libssh2 we are non-blocking */ /* Since we have set non-blocking, tell libssh2 we are non-blocking */
@ -162,9 +163,9 @@ int main(int argc, char *argv[])
/* ... start it up. This will trade welcome banners, exchange keys, /* ... start it up. This will trade welcome banners, exchange keys,
* and setup crypto, compression, and MAC layers * and setup crypto, compression, and MAC layers
*/ */
while ((rc = libssh2_session_handshake(session, sock)) == while((rc = libssh2_session_handshake(session, sock)) ==
LIBSSH2_ERROR_EAGAIN); LIBSSH2_ERROR_EAGAIN);
if (rc) { if(rc) {
fprintf(stderr, "Failure establishing SSH session: %d\n", rc); fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
return -1; return -1;
} }
@ -181,25 +182,26 @@ int main(int argc, char *argv[])
} }
fprintf(stderr, "\n"); fprintf(stderr, "\n");
if (auth_pw) { if(auth_pw) {
/* We could authenticate via password */ /* We could authenticate via password */
while ((rc = libssh2_userauth_password(session, username, password)) while((rc = libssh2_userauth_password(session, username, password))
== LIBSSH2_ERROR_EAGAIN); == LIBSSH2_ERROR_EAGAIN);
if (rc) { if(rc) {
fprintf(stderr, "Authentication by password failed.\n"); fprintf(stderr, "Authentication by password failed.\n");
goto shutdown; goto shutdown;
} }
} else { }
else {
/* Or by public key */ /* Or by public key */
while ((rc = while((rc =
libssh2_userauth_publickey_fromfile(session, username, libssh2_userauth_publickey_fromfile(session, username,
"/home/username/" "/home/username/"
".ssh/id_rsa.pub", ".ssh/id_rsa.pub",
"/home/username/" "/home/username/"
".ssh/id_rsa", ".ssh/id_rsa",
password)) == password)) ==
LIBSSH2_ERROR_EAGAIN); LIBSSH2_ERROR_EAGAIN);
if (rc) { if(rc) {
fprintf(stderr, "\tAuthentication by public key failed\n"); fprintf(stderr, "\tAuthentication by public key failed\n");
goto shutdown; goto shutdown;
} }
@ -222,7 +224,7 @@ int main(int argc, char *argv[])
goto shutdown; goto shutdown;
} }
} }
} while (!sftp_session); } while(!sftp_session);
fprintf(stderr, "libssh2_sftp_open()!\n"); fprintf(stderr, "libssh2_sftp_open()!\n");
/* Request a file via SFTP */ /* Request a file via SFTP */
@ -230,8 +232,8 @@ int main(int argc, char *argv[])
sftp_handle = libssh2_sftp_open(sftp_session, sftppath, sftp_handle = libssh2_sftp_open(sftp_session, sftppath,
LIBSSH2_FXF_READ, 0); LIBSSH2_FXF_READ, 0);
if (!sftp_handle) { if(!sftp_handle) {
if (libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN) { if(libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN) {
fprintf(stderr, "Unable to open file with SFTP\n"); fprintf(stderr, "Unable to open file with SFTP\n");
goto shutdown; goto shutdown;
} }
@ -240,31 +242,33 @@ int main(int argc, char *argv[])
waitsocket(sock, session); /* now we wait */ waitsocket(sock, session); /* now we wait */
} }
} }
} while (!sftp_handle); } while(!sftp_handle);
fprintf(stderr, "libssh2_sftp_open() is done, now receive data!\n"); fprintf(stderr, "libssh2_sftp_open() is done, now receive data!\n");
do { do {
char mem[1024*24]; char mem[1024*24];
/* loop until we fail */ /* loop until we fail */
while ((rc = libssh2_sftp_read(sftp_handle, mem, while((rc = libssh2_sftp_read(sftp_handle, mem,
sizeof(mem))) == LIBSSH2_ERROR_EAGAIN) { sizeof(mem))) == LIBSSH2_ERROR_EAGAIN) {
spin++; spin++;
waitsocket(sock, session); /* now we wait */ waitsocket(sock, session); /* now we wait */
} }
if (rc > 0) { if(rc > 0) {
total += rc; total += rc;
write(1, mem, rc); write(1, mem, rc);
} else { }
else {
break; break;
} }
} while (1); } while(1);
#ifdef HAVE_GETTIMEOFDAY #ifdef HAVE_GETTIMEOFDAY
gettimeofday(&end, NULL); gettimeofday(&end, NULL);
time_ms = tvdiff(end, start); time_ms = tvdiff(end, start);
fprintf(stderr, "Got %d bytes in %ld ms = %.1f bytes/sec spin: %d\n", total, fprintf(stderr, "Got %d bytes in %ld ms = %.1f bytes/sec spin: %d\n",
time_ms, total/(time_ms/1000.0), spin ); total,
time_ms, total/(time_ms/1000.0), spin);
#else #else
fprintf(stderr, "Got %d bytes spin: %d\n", total, spin); fprintf(stderr, "Got %d bytes spin: %d\n", total, spin);
#endif #endif
@ -275,7 +279,7 @@ int main(int argc, char *argv[])
shutdown: shutdown:
fprintf(stderr, "libssh2_session_disconnect\n"); fprintf(stderr, "libssh2_session_disconnect\n");
while (libssh2_session_disconnect(session, while(libssh2_session_disconnect(session,
"Normal Shutdown, Thank you") == "Normal Shutdown, Thank you") ==
LIBSSH2_ERROR_EAGAIN); LIBSSH2_ERROR_EAGAIN);
libssh2_session_free(session); libssh2_session_free(session);

Просмотреть файл

@ -40,10 +40,10 @@ int main(int argc, char *argv[])
struct sockaddr_in sin; struct sockaddr_in sin;
const char *fingerprint; const char *fingerprint;
LIBSSH2_SESSION *session; LIBSSH2_SESSION *session;
const char *username="username"; const char *username = "username";
const char *password="password"; const char *password = "password";
const char *loclfile="sftp_write.c"; const char *loclfile = "sftp_write.c";
const char *sftppath="/tmp/TEST"; const char *sftppath = "/tmp/TEST";
int rc; int rc;
FILE *local; FILE *local;
LIBSSH2_SFTP *sftp_session; LIBSSH2_SFTP *sftp_session;
@ -56,16 +56,17 @@ int main(int argc, char *argv[])
WSADATA wsadata; WSADATA wsadata;
int err; int err;
err = WSAStartup(MAKEWORD(2,0), &wsadata); err = WSAStartup(MAKEWORD(2, 0), &wsadata);
if (err != 0) { if(err != 0) {
fprintf(stderr, "WSAStartup failed with error: %d\n", err); fprintf(stderr, "WSAStartup failed with error: %d\n", err);
return 1; return 1;
} }
#endif #endif
if (argc > 1) { if(argc > 1) {
hostaddr = inet_addr(argv[1]); hostaddr = inet_addr(argv[1]);
} else { }
else {
hostaddr = htonl(0x7F000001); hostaddr = htonl(0x7F000001);
} }
@ -82,14 +83,14 @@ int main(int argc, char *argv[])
sftppath = argv[5]; sftppath = argv[5];
} }
rc = libssh2_init (0); rc = libssh2_init(0);
if (rc != 0) { if(rc != 0) {
fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
return 1; return 1;
} }
local = fopen(loclfile, "rb"); local = fopen(loclfile, "rb");
if (!local) { if(!local) {
fprintf(stderr, "Can't open local file %s\n", loclfile); fprintf(stderr, "Can't open local file %s\n", loclfile);
return -1; return -1;
} }
@ -103,7 +104,7 @@ int main(int argc, char *argv[])
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_port = htons(22); sin.sin_port = htons(22);
sin.sin_addr.s_addr = hostaddr; sin.sin_addr.s_addr = hostaddr;
if (connect(sock, (struct sockaddr*)(&sin), if(connect(sock, (struct sockaddr*)(&sin),
sizeof(struct sockaddr_in)) != 0) { sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n"); fprintf(stderr, "failed to connect!\n");
return -1; return -1;
@ -139,18 +140,20 @@ int main(int argc, char *argv[])
} }
fprintf(stderr, "\n"); fprintf(stderr, "\n");
if (auth_pw) { if(auth_pw) {
/* We could authenticate via password */ /* We could authenticate via password */
if (libssh2_userauth_password(session, username, password)) { if(libssh2_userauth_password(session, username, password)) {
fprintf(stderr, "Authentication by password failed.\n"); fprintf(stderr, "Authentication by password failed.\n");
goto shutdown; goto shutdown;
} }
} else { }
else {
/* Or by public key */ /* Or by public key */
if (libssh2_userauth_publickey_fromfile(session, username, const char *pubkey = "/home/username/.ssh/id_rsa.pub";
"/home/username/.ssh/id_rsa.pub", const char *privkey = "/home/username/.ssh/id_rsa.pub";
"/home/username/.ssh/id_rsa", if(libssh2_userauth_publickey_fromfile(session, username,
password)) { pubkey, privkey,
password)) {
fprintf(stderr, "\tAuthentication by public key failed\n"); fprintf(stderr, "\tAuthentication by public key failed\n");
goto shutdown; goto shutdown;
} }
@ -159,7 +162,7 @@ int main(int argc, char *argv[])
fprintf(stderr, "libssh2_sftp_init()!\n"); fprintf(stderr, "libssh2_sftp_init()!\n");
sftp_session = libssh2_sftp_init(session); sftp_session = libssh2_sftp_init(session);
if (!sftp_session) { if(!sftp_session) {
fprintf(stderr, "Unable to init SFTP session\n"); fprintf(stderr, "Unable to init SFTP session\n");
goto shutdown; goto shutdown;
} }
@ -172,14 +175,14 @@ int main(int argc, char *argv[])
LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR| LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR|
LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH); LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH);
if (!sftp_handle) { if(!sftp_handle) {
fprintf(stderr, "Unable to open file with SFTP\n"); fprintf(stderr, "Unable to open file with SFTP\n");
goto shutdown; goto shutdown;
} }
fprintf(stderr, "libssh2_sftp_open() is done, now send data!\n"); fprintf(stderr, "libssh2_sftp_open() is done, now send data!\n");
do { do {
nread = fread(mem, 1, sizeof(mem), local); nread = fread(mem, 1, sizeof(mem), local);
if (nread <= 0) { if(nread <= 0) {
/* end of file */ /* end of file */
break; break;
} }
@ -192,9 +195,9 @@ int main(int argc, char *argv[])
break; break;
ptr += rc; ptr += rc;
nread -= rc; nread -= rc;
} while (nread); } while(nread);
} while (rc > 0); } while(rc > 0);
libssh2_sftp_close(sftp_handle); libssh2_sftp_close(sftp_handle);
libssh2_sftp_shutdown(sftp_session); libssh2_sftp_shutdown(sftp_session);
@ -209,7 +212,7 @@ shutdown:
#else #else
close(sock); close(sock);
#endif #endif
if (local) if(local)
fclose(local); fclose(local);
fprintf(stderr, "all done\n"); fprintf(stderr, "all done\n");

Просмотреть файл

@ -4,7 +4,7 @@
* The sample code has default values for host name, user name, password * The sample code has default values for host name, user name, password
* and path to copy, but you can specify them on the command line like: * and path to copy, but you can specify them on the command line like:
* *
* "sftp 192.168.0.1 user password sftp_write_nonblock.c /tmp/sftp_write_nonblock.c" * "sftp 192.168.0.1 user password thisfile /tmp/storehere"
*/ */
#include "libssh2_config.h" #include "libssh2_config.h"
@ -77,10 +77,10 @@ int main(int argc, char *argv[])
struct sockaddr_in sin; struct sockaddr_in sin;
const char *fingerprint; const char *fingerprint;
LIBSSH2_SESSION *session; LIBSSH2_SESSION *session;
const char *username="username"; const char *username = "username";
const char *password="password"; const char *password = "password";
const char *loclfile="sftp_write_nonblock.c"; const char *loclfile = "sftp_write_nonblock.c";
const char *sftppath="/tmp/sftp_write_nonblock.c"; const char *sftppath = "/tmp/sftp_write_nonblock.c";
int rc; int rc;
FILE *local; FILE *local;
LIBSSH2_SFTP *sftp_session; LIBSSH2_SFTP *sftp_session;
@ -96,40 +96,41 @@ int main(int argc, char *argv[])
WSADATA wsadata; WSADATA wsadata;
int err; int err;
err = WSAStartup(MAKEWORD(2,0), &wsadata); err = WSAStartup(MAKEWORD(2, 0), &wsadata);
if (err != 0) { if(err != 0) {
fprintf(stderr, "WSAStartup failed with error: %d\n", err); fprintf(stderr, "WSAStartup failed with error: %d\n", err);
return 1; return 1;
} }
#endif #endif
if (argc > 1) { if(argc > 1) {
hostaddr = inet_addr(argv[1]); hostaddr = inet_addr(argv[1]);
} else { }
else {
hostaddr = htonl(0x7F000001); hostaddr = htonl(0x7F000001);
} }
if (argc > 2) { if(argc > 2) {
username = argv[2]; username = argv[2];
} }
if (argc > 3) { if(argc > 3) {
password = argv[3]; password = argv[3];
} }
if (argc > 4) { if(argc > 4) {
loclfile = argv[4]; loclfile = argv[4];
} }
if (argc > 5) { if(argc > 5) {
sftppath = argv[5]; sftppath = argv[5];
} }
rc = libssh2_init (0); rc = libssh2_init(0);
if (rc != 0) { if(rc != 0) {
fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
return 1; return 1;
} }
local = fopen(loclfile, "rb"); local = fopen(loclfile, "rb");
if (!local) { if(!local) {
fprintf(stderr, "Can't open local file %s\n", loclfile); fprintf(stderr, "Can't open local file %s\n", loclfile);
return -1; return -1;
} }
@ -143,7 +144,7 @@ int main(int argc, char *argv[])
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_port = htons(22); sin.sin_port = htons(22);
sin.sin_addr.s_addr = hostaddr; sin.sin_addr.s_addr = hostaddr;
if (connect(sock, (struct sockaddr*)(&sin), if(connect(sock, (struct sockaddr*)(&sin),
sizeof(struct sockaddr_in)) != 0) { sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n"); fprintf(stderr, "failed to connect!\n");
return -1; return -1;
@ -152,7 +153,7 @@ int main(int argc, char *argv[])
/* Create a session instance /* Create a session instance
*/ */
session = libssh2_session_init(); session = libssh2_session_init();
if (!session) if(!session)
return -1; return -1;
/* Since we have set non-blocking, tell libssh2 we are non-blocking */ /* Since we have set non-blocking, tell libssh2 we are non-blocking */
@ -161,9 +162,9 @@ int main(int argc, char *argv[])
/* ... start it up. This will trade welcome banners, exchange keys, /* ... start it up. This will trade welcome banners, exchange keys,
* and setup crypto, compression, and MAC layers * and setup crypto, compression, and MAC layers
*/ */
while ((rc = libssh2_session_handshake(session, sock)) while((rc = libssh2_session_handshake(session, sock))
== LIBSSH2_ERROR_EAGAIN); == LIBSSH2_ERROR_EAGAIN);
if (rc) { if(rc) {
fprintf(stderr, "Failure establishing SSH session: %d\n", rc); fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
return -1; return -1;
} }
@ -180,22 +181,24 @@ int main(int argc, char *argv[])
} }
fprintf(stderr, "\n"); fprintf(stderr, "\n");
if (auth_pw) { if(auth_pw) {
/* We could authenticate via password */ /* We could authenticate via password */
while ((rc = libssh2_userauth_password(session, username, password)) == while((rc = libssh2_userauth_password(session, username, password)) ==
LIBSSH2_ERROR_EAGAIN); LIBSSH2_ERROR_EAGAIN);
if (rc) { if(rc) {
fprintf(stderr, "Authentication by password failed.\n"); fprintf(stderr, "Authentication by password failed.\n");
goto shutdown; goto shutdown;
} }
} else { }
else {
/* Or by public key */ /* Or by public key */
while ((rc = libssh2_userauth_publickey_fromfile(session, username, const char *pubkey = "/home/username/.ssh/id_rsa.pub";
"/home/username/.ssh/id_rsa.pub", const char *privkey = "/home/username/.ssh/id_rsa";
"/home/username/.ssh/id_rsa", while((rc = libssh2_userauth_publickey_fromfile(session, username,
password)) == pubkey, privkey,
LIBSSH2_ERROR_EAGAIN); password)) ==
if (rc) { LIBSSH2_ERROR_EAGAIN);
if(rc) {
fprintf(stderr, "\tAuthentication by public key failed\n"); fprintf(stderr, "\tAuthentication by public key failed\n");
goto shutdown; goto shutdown;
} }
@ -205,28 +208,28 @@ int main(int argc, char *argv[])
do { do {
sftp_session = libssh2_sftp_init(session); sftp_session = libssh2_sftp_init(session);
if (!sftp_session && if(!sftp_session &&
(libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN)) { (libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN)) {
fprintf(stderr, "Unable to init SFTP session\n"); fprintf(stderr, "Unable to init SFTP session\n");
goto shutdown; goto shutdown;
} }
} while (!sftp_session); } while(!sftp_session);
fprintf(stderr, "libssh2_sftp_open()!\n"); fprintf(stderr, "libssh2_sftp_open()!\n");
/* Request a file via SFTP */ /* Request a file via SFTP */
do { do {
sftp_handle = sftp_handle =
libssh2_sftp_open(sftp_session, sftppath, libssh2_sftp_open(sftp_session, sftppath,
LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT|LIBSSH2_FXF_TRUNC, LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT|
LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR| LIBSSH2_FXF_TRUNC,
LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH); LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR|
LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH);
if (!sftp_handle && if(!sftp_handle &&
(libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN)) { (libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN)) {
fprintf(stderr, "Unable to open file with SFTP\n"); fprintf(stderr, "Unable to open file with SFTP\n");
goto shutdown; goto shutdown;
} }
} while (!sftp_handle); } while(!sftp_handle);
fprintf(stderr, "libssh2_sftp_open() is done, now send data!\n"); fprintf(stderr, "libssh2_sftp_open() is done, now send data!\n");
@ -234,7 +237,7 @@ int main(int argc, char *argv[])
do { do {
nread = fread(mem, 1, sizeof(mem), local); nread = fread(mem, 1, sizeof(mem), local);
if (nread <= 0) { if(nread <= 0) {
/* end of file */ /* end of file */
break; break;
} }
@ -244,7 +247,7 @@ int main(int argc, char *argv[])
do { do {
/* write data in a loop until we block */ /* write data in a loop until we block */
while ((rc = libssh2_sftp_write(sftp_handle, ptr, nread)) == while((rc = libssh2_sftp_write(sftp_handle, ptr, nread)) ==
LIBSSH2_ERROR_EAGAIN) { LIBSSH2_ERROR_EAGAIN) {
waitsocket(sock, session); waitsocket(sock, session);
} }
@ -253,8 +256,8 @@ int main(int argc, char *argv[])
ptr += rc; ptr += rc;
nread -= rc; nread -= rc;
} while (nread); } while(nread);
} while (rc > 0); } while(rc > 0);
duration = (int)(time(NULL)-start); duration = (int)(time(NULL)-start);
@ -268,7 +271,7 @@ int main(int argc, char *argv[])
shutdown: shutdown:
while (libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing") while(libssh2_session_disconnect(session, "Normal Shutdown")
== LIBSSH2_ERROR_EAGAIN); == LIBSSH2_ERROR_EAGAIN);
libssh2_session_free(session); libssh2_session_free(session);

Просмотреть файл

@ -4,7 +4,7 @@
* The sample code has default values for host name, user name, password * The sample code has default values for host name, user name, password
* and path to copy, but you can specify them on the command line like: * and path to copy, but you can specify them on the command line like:
* *
* "sftp 192.168.0.1 user password sftp_write_nonblock.c /tmp/sftp_write_nonblock.c" * "sftp 192.168.0.1 user password file /tmp/storehere"
*/ */
#include "libssh2_config.h" #include "libssh2_config.h"
@ -77,10 +77,10 @@ int main(int argc, char *argv[])
struct sockaddr_in sin; struct sockaddr_in sin;
const char *fingerprint; const char *fingerprint;
LIBSSH2_SESSION *session; LIBSSH2_SESSION *session;
const char *username="username"; const char *username = "username";
const char *password="password"; const char *password = "password";
const char *loclfile="sftp_write_nonblock.c"; const char *loclfile = "sftp_write_nonblock.c";
const char *sftppath="/tmp/sftp_write_nonblock.c"; const char *sftppath = "/tmp/sftp_write_nonblock.c";
int rc; int rc;
FILE *local; FILE *local;
LIBSSH2_SFTP *sftp_session; LIBSSH2_SFTP *sftp_session;
@ -96,40 +96,41 @@ int main(int argc, char *argv[])
WSADATA wsadata; WSADATA wsadata;
int err; int err;
err = WSAStartup(MAKEWORD(2,0), &wsadata); err = WSAStartup(MAKEWORD(2, 0), &wsadata);
if (err != 0) { if(err != 0) {
fprintf(stderr, "WSAStartup failed with error: %d\n", err); fprintf(stderr, "WSAStartup failed with error: %d\n", err);
return 1; return 1;
} }
#endif #endif
if (argc > 1) { if(argc > 1) {
hostaddr = inet_addr(argv[1]); hostaddr = inet_addr(argv[1]);
} else { }
else {
hostaddr = htonl(0x7F000001); hostaddr = htonl(0x7F000001);
} }
if (argc > 2) { if(argc > 2) {
username = argv[2]; username = argv[2];
} }
if (argc > 3) { if(argc > 3) {
password = argv[3]; password = argv[3];
} }
if (argc > 4) { if(argc > 4) {
loclfile = argv[4]; loclfile = argv[4];
} }
if (argc > 5) { if(argc > 5) {
sftppath = argv[5]; sftppath = argv[5];
} }
rc = libssh2_init (0); rc = libssh2_init(0);
if (rc != 0) { if(rc != 0) {
fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
return 1; return 1;
} }
local = fopen(loclfile, "rb"); local = fopen(loclfile, "rb");
if (!local) { if(!local) {
fprintf(stderr, "Can't open local file %s\n", loclfile); fprintf(stderr, "Can't open local file %s\n", loclfile);
return -1; return -1;
} }
@ -143,7 +144,7 @@ int main(int argc, char *argv[])
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_port = htons(22); sin.sin_port = htons(22);
sin.sin_addr.s_addr = hostaddr; sin.sin_addr.s_addr = hostaddr;
if (connect(sock, (struct sockaddr*)(&sin), if(connect(sock, (struct sockaddr*)(&sin),
sizeof(struct sockaddr_in)) != 0) { sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n"); fprintf(stderr, "failed to connect!\n");
return -1; return -1;
@ -152,7 +153,7 @@ int main(int argc, char *argv[])
/* Create a session instance /* Create a session instance
*/ */
session = libssh2_session_init(); session = libssh2_session_init();
if (!session) if(!session)
return -1; return -1;
/* Since we have set non-blocking, tell libssh2 we are non-blocking */ /* Since we have set non-blocking, tell libssh2 we are non-blocking */
@ -161,9 +162,9 @@ int main(int argc, char *argv[])
/* ... start it up. This will trade welcome banners, exchange keys, /* ... start it up. This will trade welcome banners, exchange keys,
* and setup crypto, compression, and MAC layers * and setup crypto, compression, and MAC layers
*/ */
while ((rc = libssh2_session_handshake(session, sock)) while((rc = libssh2_session_handshake(session, sock))
== LIBSSH2_ERROR_EAGAIN); == LIBSSH2_ERROR_EAGAIN);
if (rc) { if(rc) {
fprintf(stderr, "Failure establishing SSH session: %d\n", rc); fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
return -1; return -1;
} }
@ -180,22 +181,24 @@ int main(int argc, char *argv[])
} }
fprintf(stderr, "\n"); fprintf(stderr, "\n");
if (auth_pw) { if(auth_pw) {
/* We could authenticate via password */ /* We could authenticate via password */
while ((rc = libssh2_userauth_password(session, username, password)) == while((rc = libssh2_userauth_password(session, username, password)) ==
LIBSSH2_ERROR_EAGAIN); LIBSSH2_ERROR_EAGAIN);
if (rc) { if(rc) {
fprintf(stderr, "Authentication by password failed.\n"); fprintf(stderr, "Authentication by password failed.\n");
goto shutdown; goto shutdown;
} }
} else { }
else {
/* Or by public key */ /* Or by public key */
while ((rc = libssh2_userauth_publickey_fromfile(session, username, #define PUBKEY "/home/username/.ssh/id_rsa.pub"
"/home/username/.ssh/id_rsa.pub", #define PRIVKEY "/home/username/.ssh/id_rsa"
"/home/username/.ssh/id_rsa", while((rc = libssh2_userauth_publickey_fromfile(session, username,
password)) == PUBKEY, PRIVKEY,
LIBSSH2_ERROR_EAGAIN); password)) ==
if (rc) { LIBSSH2_ERROR_EAGAIN);
if(rc) {
fprintf(stderr, "\tAuthentication by public key failed\n"); fprintf(stderr, "\tAuthentication by public key failed\n");
goto shutdown; goto shutdown;
} }
@ -205,28 +208,29 @@ int main(int argc, char *argv[])
do { do {
sftp_session = libssh2_sftp_init(session); sftp_session = libssh2_sftp_init(session);
if (!sftp_session && if(!sftp_session &&
(libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN)) { (libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN)) {
fprintf(stderr, "Unable to init SFTP session\n"); fprintf(stderr, "Unable to init SFTP session\n");
goto shutdown; goto shutdown;
} }
} while (!sftp_session); } while(!sftp_session);
fprintf(stderr, "libssh2_sftp_open()!\n"); fprintf(stderr, "libssh2_sftp_open()!\n");
/* Request a file via SFTP */ /* Request a file via SFTP */
do { do {
sftp_handle = sftp_handle =
libssh2_sftp_open(sftp_session, sftppath, libssh2_sftp_open(sftp_session, sftppath,
LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT|LIBSSH2_FXF_TRUNC, LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT|
LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR| LIBSSH2_FXF_TRUNC,
LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH); LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR|
LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH);
if (!sftp_handle && if(!sftp_handle &&
(libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN)) { (libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN)) {
fprintf(stderr, "Unable to open file with SFTP\n"); fprintf(stderr, "Unable to open file with SFTP\n");
goto shutdown; goto shutdown;
} }
} while (!sftp_handle); } while(!sftp_handle);
fprintf(stderr, "libssh2_sftp_open() is done, now send data!\n"); fprintf(stderr, "libssh2_sftp_open() is done, now send data!\n");
@ -235,9 +239,9 @@ int main(int argc, char *argv[])
memuse = 0; /* it starts blank */ memuse = 0; /* it starts blank */
do { do {
nread = fread(&mem[memuse], 1, sizeof(mem)-memuse, local); nread = fread(&mem[memuse], 1, sizeof(mem)-memuse, local);
if (nread <= 0) { if(nread <= 0) {
/* end of file */ /* end of file */
if (memuse > 0) if(memuse > 0)
/* the previous sending is not finished */ /* the previous sending is not finished */
nread = 0; nread = 0;
else else
@ -247,7 +251,7 @@ int main(int argc, char *argv[])
total += nread; total += nread;
/* write data in a loop until we block */ /* write data in a loop until we block */
while ((rc = libssh2_sftp_write(sftp_handle, mem, memuse)) == while((rc = libssh2_sftp_write(sftp_handle, mem, memuse)) ==
LIBSSH2_ERROR_EAGAIN) { LIBSSH2_ERROR_EAGAIN) {
waitsocket(sock, session); waitsocket(sock, session);
} }
@ -263,7 +267,7 @@ int main(int argc, char *argv[])
/* 'mem' was consumed fully */ /* 'mem' was consumed fully */
memuse = 0; memuse = 0;
} while (rc > 0); } while(rc > 0);
duration = (int)(time(NULL)-start); duration = (int)(time(NULL)-start);
@ -277,8 +281,8 @@ int main(int argc, char *argv[])
shutdown: shutdown:
while (libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing") while(libssh2_session_disconnect(session, "Normal Shutdown")
== LIBSSH2_ERROR_EAGAIN); == LIBSSH2_ERROR_EAGAIN);
libssh2_session_free(session); libssh2_session_free(session);
#ifdef WIN32 #ifdef WIN32

Просмотреть файл

@ -42,10 +42,10 @@
#define __FILESIZE "llu" #define __FILESIZE "llu"
#endif #endif
const char *keyfile1="~/.ssh/id_rsa.pub"; const char *keyfile1 = "~/.ssh/id_rsa.pub";
const char *keyfile2="~/.ssh/id_rsa"; const char *keyfile2 = "~/.ssh/id_rsa";
const char *username="username"; const char *username = "username";
const char *password="password"; const char *password = "password";
static void kbd_callback(const char *name, int name_len, static void kbd_callback(const char *name, int name_len,
const char *instruction, int instruction_len, const char *instruction, int instruction_len,
@ -58,7 +58,7 @@ static void kbd_callback(const char *name, int name_len,
(void)name_len; (void)name_len;
(void)instruction; (void)instruction;
(void)instruction_len; (void)instruction_len;
if (num_prompts == 1) { if(num_prompts == 1) {
responses[0].text = strdup(password); responses[0].text = strdup(password);
responses[0].length = strlen(password); responses[0].length = strlen(password);
} }
@ -74,7 +74,7 @@ int main(int argc, char *argv[])
const char *fingerprint; const char *fingerprint;
char *userauthlist; char *userauthlist;
LIBSSH2_SESSION *session; LIBSSH2_SESSION *session;
const char *sftppath="/tmp/secretdir"; const char *sftppath = "/tmp/secretdir";
LIBSSH2_SFTP *sftp_session; LIBSSH2_SFTP *sftp_session;
LIBSSH2_SFTP_HANDLE *sftp_handle; LIBSSH2_SFTP_HANDLE *sftp_handle;
@ -82,16 +82,17 @@ int main(int argc, char *argv[])
WSADATA wsadata; WSADATA wsadata;
int err; int err;
err = WSAStartup(MAKEWORD(2,0), &wsadata); err = WSAStartup(MAKEWORD(2, 0), &wsadata);
if (err != 0) { if(err != 0) {
fprintf(stderr, "WSAStartup failed with error: %d\n", err); fprintf(stderr, "WSAStartup failed with error: %d\n", err);
return 1; return 1;
} }
#endif #endif
if (argc > 1) { if(argc > 1) {
hostaddr = inet_addr(argv[1]); hostaddr = inet_addr(argv[1]);
} else { }
else {
hostaddr = htonl(0x7F000001); hostaddr = htonl(0x7F000001);
} }
@ -105,9 +106,9 @@ int main(int argc, char *argv[])
sftppath = argv[4]; sftppath = argv[4];
} }
rc = libssh2_init (0); rc = libssh2_init(0);
if (rc != 0) { if(rc != 0) {
fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
return 1; return 1;
} }
@ -120,8 +121,8 @@ int main(int argc, char *argv[])
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_port = htons(22); sin.sin_port = htons(22);
sin.sin_addr.s_addr = hostaddr; sin.sin_addr.s_addr = hostaddr;
if (connect(sock, (struct sockaddr*)(&sin), if(connect(sock, (struct sockaddr*)(&sin),
sizeof(struct sockaddr_in)) != 0) { sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n"); fprintf(stderr, "failed to connect!\n");
return -1; return -1;
} }
@ -156,58 +157,64 @@ int main(int argc, char *argv[])
/* check what authentication methods are available */ /* check what authentication methods are available */
userauthlist = libssh2_userauth_list(session, username, strlen(username)); userauthlist = libssh2_userauth_list(session, username, strlen(username));
fprintf(stderr, "Authentication methods: %s\n", userauthlist); fprintf(stderr, "Authentication methods: %s\n", userauthlist);
if (strstr(userauthlist, "password") != NULL) { if(strstr(userauthlist, "password") != NULL) {
auth_pw |= 1; auth_pw |= 1;
} }
if (strstr(userauthlist, "keyboard-interactive") != NULL) { if(strstr(userauthlist, "keyboard-interactive") != NULL) {
auth_pw |= 2; auth_pw |= 2;
} }
if (strstr(userauthlist, "publickey") != NULL) { if(strstr(userauthlist, "publickey") != NULL) {
auth_pw |= 4; auth_pw |= 4;
} }
/* if we got an 5. argument we set this option if supported */ /* if we got an 5. argument we set this option if supported */
if(argc > 5) { if(argc > 5) {
if ((auth_pw & 1) && !strcasecmp(argv[5], "-p")) { if((auth_pw & 1) && !strcasecmp(argv[5], "-p")) {
auth_pw = 1; auth_pw = 1;
} }
if ((auth_pw & 2) && !strcasecmp(argv[5], "-i")) { if((auth_pw & 2) && !strcasecmp(argv[5], "-i")) {
auth_pw = 2; auth_pw = 2;
} }
if ((auth_pw & 4) && !strcasecmp(argv[5], "-k")) { if((auth_pw & 4) && !strcasecmp(argv[5], "-k")) {
auth_pw = 4; auth_pw = 4;
} }
} }
if (auth_pw & 1) { if(auth_pw & 1) {
/* We could authenticate via password */ /* We could authenticate via password */
if (libssh2_userauth_password(session, username, password)) { if(libssh2_userauth_password(session, username, password)) {
fprintf(stderr, "\tAuthentication by password failed!\n"); fprintf(stderr, "\tAuthentication by password failed!\n");
goto shutdown; goto shutdown;
} else { }
else {
fprintf(stderr, "\tAuthentication by password succeeded.\n"); fprintf(stderr, "\tAuthentication by password succeeded.\n");
} }
} else if (auth_pw & 2) { }
else if(auth_pw & 2) {
/* Or via keyboard-interactive */ /* Or via keyboard-interactive */
if (libssh2_userauth_keyboard_interactive(session, username, if(libssh2_userauth_keyboard_interactive(session, username,
&kbd_callback) ) { &kbd_callback) ) {
fprintf(stderr, fprintf(stderr,
"\tAuthentication by keyboard-interactive failed!\n"); "\tAuthentication by keyboard-interactive failed!\n");
goto shutdown; goto shutdown;
} else {
fprintf(stderr,
"\tAuthentication by keyboard-interactive succeeded.\n");
} }
} else if (auth_pw & 4) { else {
fprintf(stderr,
"\tAuthentication by keyboard-interactive succeeded.\n");
}
}
else if(auth_pw & 4) {
/* Or by public key */ /* Or by public key */
if (libssh2_userauth_publickey_fromfile(session, username, keyfile1, if(libssh2_userauth_publickey_fromfile(session, username, keyfile1,
keyfile2, password)) { keyfile2, password)) {
fprintf(stderr, "\tAuthentication by public key failed!\n"); fprintf(stderr, "\tAuthentication by public key failed!\n");
goto shutdown; goto shutdown;
} else { }
else {
fprintf(stderr, "\tAuthentication by public key succeeded.\n"); fprintf(stderr, "\tAuthentication by public key succeeded.\n");
} }
} else { }
else {
fprintf(stderr, "No supported authentication methods found!\n"); fprintf(stderr, "No supported authentication methods found!\n");
goto shutdown; goto shutdown;
} }
@ -215,7 +222,7 @@ int main(int argc, char *argv[])
fprintf(stderr, "libssh2_sftp_init()!\n"); fprintf(stderr, "libssh2_sftp_init()!\n");
sftp_session = libssh2_sftp_init(session); sftp_session = libssh2_sftp_init(session);
if (!sftp_session) { if(!sftp_session) {
fprintf(stderr, "Unable to init SFTP session\n"); fprintf(stderr, "Unable to init SFTP session\n");
goto shutdown; goto shutdown;
} }
@ -227,7 +234,7 @@ int main(int argc, char *argv[])
/* Request a dir listing via SFTP */ /* Request a dir listing via SFTP */
sftp_handle = libssh2_sftp_opendir(sftp_session, sftppath); sftp_handle = libssh2_sftp_opendir(sftp_session, sftppath);
if (!sftp_handle) { if(!sftp_handle) {
fprintf(stderr, "Unable to open dir with SFTP\n"); fprintf(stderr, "Unable to open dir with SFTP\n");
goto shutdown; goto shutdown;
} }
@ -244,9 +251,10 @@ int main(int argc, char *argv[])
/* rc is the length of the file name in the mem /* rc is the length of the file name in the mem
buffer */ buffer */
if (longentry[0] != '\0') { if(longentry[0] != '\0') {
printf("%s\n", longentry); printf("%s\n", longentry);
} else { }
else {
if(attrs.flags & LIBSSH2_SFTP_ATTR_PERMISSIONS) { if(attrs.flags & LIBSSH2_SFTP_ATTR_PERMISSIONS) {
/* this should check what permissions it /* this should check what permissions it
is and print the output accordingly */ is and print the output accordingly */
@ -273,14 +281,14 @@ int main(int argc, char *argv[])
else else
break; break;
} while (1); } while(1);
libssh2_sftp_closedir(sftp_handle); libssh2_sftp_closedir(sftp_handle);
libssh2_sftp_shutdown(sftp_session); libssh2_sftp_shutdown(sftp_session);
shutdown: shutdown:
libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing"); libssh2_session_disconnect(session, "Normal Shutdown");
libssh2_session_free(session); libssh2_session_free(session);
#ifdef WIN32 #ifdef WIN32

Просмотреть файл

@ -49,9 +49,11 @@ int main(int argc, char *argv[])
struct sockaddr_in sin; struct sockaddr_in sin;
const char *fingerprint; const char *fingerprint;
LIBSSH2_SESSION *session; LIBSSH2_SESSION *session;
const char *username="username"; const char *username = "username";
const char *password="password"; const char *password = "password";
const char *sftppath="/tmp/secretdir"; const char *sftppath = "/tmp/secretdir";
const char *pubkey = "/home/username/.ssh/id_rsa.pub";
const char *privkey = "/home/username/.ssh/id_rsa";
int rc; int rc;
LIBSSH2_SFTP *sftp_session; LIBSSH2_SFTP *sftp_session;
LIBSSH2_SFTP_HANDLE *sftp_handle; LIBSSH2_SFTP_HANDLE *sftp_handle;
@ -60,16 +62,17 @@ int main(int argc, char *argv[])
WSADATA wsadata; WSADATA wsadata;
int err; int err;
err = WSAStartup(MAKEWORD(2,0), &wsadata); err = WSAStartup(MAKEWORD(2, 0), &wsadata);
if (err != 0) { if(err != 0) {
fprintf(stderr, "WSAStartup failed with error: %d\n", err); fprintf(stderr, "WSAStartup failed with error: %d\n", err);
return 1; return 1;
} }
#endif #endif
if (argc > 1) { if(argc > 1) {
hostaddr = inet_addr(argv[1]); hostaddr = inet_addr(argv[1]);
} else { }
else {
hostaddr = htonl(0x7F000001); hostaddr = htonl(0x7F000001);
} }
@ -83,9 +86,9 @@ int main(int argc, char *argv[])
sftppath = argv[4]; sftppath = argv[4];
} }
rc = libssh2_init (0); rc = libssh2_init(0);
if (rc != 0) { if(rc != 0) {
fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
return 1; return 1;
} }
@ -98,8 +101,8 @@ int main(int argc, char *argv[])
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_port = htons(22); sin.sin_port = htons(22);
sin.sin_addr.s_addr = hostaddr; sin.sin_addr.s_addr = hostaddr;
if (connect(sock, (struct sockaddr*)(&sin), if(connect(sock, (struct sockaddr*)(&sin),
sizeof(struct sockaddr_in)) != 0) { sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n"); fprintf(stderr, "failed to connect!\n");
return -1; return -1;
} }
@ -116,8 +119,8 @@ int main(int argc, char *argv[])
/* ... start it up. This will trade welcome banners, exchange keys, /* ... start it up. This will trade welcome banners, exchange keys,
* and setup crypto, compression, and MAC layers * and setup crypto, compression, and MAC layers
*/ */
while ((rc = libssh2_session_handshake(session, sock)) == while((rc = libssh2_session_handshake(session, sock)) ==
LIBSSH2_ERROR_EAGAIN); LIBSSH2_ERROR_EAGAIN);
if(rc) { if(rc) {
fprintf(stderr, "Failure establishing SSH session: %d\n", rc); fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
return -1; return -1;
@ -135,21 +138,22 @@ int main(int argc, char *argv[])
} }
fprintf(stderr, "\n"); fprintf(stderr, "\n");
if (auth_pw) { if(auth_pw) {
/* We could authenticate via password */ /* We could authenticate via password */
while ((rc = libssh2_userauth_password(session, username, password)) == while((rc = libssh2_userauth_password(session, username, password)) ==
LIBSSH2_ERROR_EAGAIN); LIBSSH2_ERROR_EAGAIN);
if (rc) { if(rc) {
fprintf(stderr, "Authentication by password failed.\n"); fprintf(stderr, "Authentication by password failed.\n");
goto shutdown; goto shutdown;
} }
} else { }
else {
/* Or by public key */ /* Or by public key */
while ((rc = libssh2_userauth_publickey_fromfile(session, username, while((rc = libssh2_userauth_publickey_fromfile(session, username,
"/home/username/.ssh/id_rsa.pub", pubkey, privkey,
"/home/username/.ssh/id_rsa", password)) ==
password)) == LIBSSH2_ERROR_EAGAIN); LIBSSH2_ERROR_EAGAIN);
if (rc) { if(rc) {
fprintf(stderr, "\tAuthentication by public key failed\n"); fprintf(stderr, "\tAuthentication by public key failed\n");
goto shutdown; goto shutdown;
} }
@ -159,24 +163,24 @@ int main(int argc, char *argv[])
do { do {
sftp_session = libssh2_sftp_init(session); sftp_session = libssh2_sftp_init(session);
if ((!sftp_session) && (libssh2_session_last_errno(session) != if((!sftp_session) && (libssh2_session_last_errno(session) !=
LIBSSH2_ERROR_EAGAIN)) { LIBSSH2_ERROR_EAGAIN)) {
fprintf(stderr, "Unable to init SFTP session\n"); fprintf(stderr, "Unable to init SFTP session\n");
goto shutdown; goto shutdown;
} }
} while (!sftp_session); } while(!sftp_session);
fprintf(stderr, "libssh2_sftp_opendir()!\n"); fprintf(stderr, "libssh2_sftp_opendir()!\n");
/* Request a dir listing via SFTP */ /* Request a dir listing via SFTP */
do { do {
sftp_handle = libssh2_sftp_opendir(sftp_session, sftppath); sftp_handle = libssh2_sftp_opendir(sftp_session, sftppath);
if ((!sftp_handle) && (libssh2_session_last_errno(session) != if((!sftp_handle) && (libssh2_session_last_errno(session) !=
LIBSSH2_ERROR_EAGAIN)) { LIBSSH2_ERROR_EAGAIN)) {
fprintf(stderr, "Unable to open dir with SFTP\n"); fprintf(stderr, "Unable to open dir with SFTP\n");
goto shutdown; goto shutdown;
} }
} while (!sftp_handle); } while(!sftp_handle);
fprintf(stderr, "libssh2_sftp_opendir() is done, now receive listing!\n"); fprintf(stderr, "libssh2_sftp_opendir() is done, now receive listing!\n");
do { do {
@ -184,8 +188,8 @@ int main(int argc, char *argv[])
LIBSSH2_SFTP_ATTRIBUTES attrs; LIBSSH2_SFTP_ATTRIBUTES attrs;
/* loop until we fail */ /* loop until we fail */
while ((rc = libssh2_sftp_readdir(sftp_handle, mem, sizeof(mem), while((rc = libssh2_sftp_readdir(sftp_handle, mem, sizeof(mem),
&attrs)) == LIBSSH2_ERROR_EAGAIN) { &attrs)) == LIBSSH2_ERROR_EAGAIN) {
; ;
} }
if(rc > 0) { if(rc > 0) {
@ -196,13 +200,15 @@ int main(int argc, char *argv[])
/* this should check what permissions it /* this should check what permissions it
is and print the output accordingly */ is and print the output accordingly */
printf("--fix----- "); printf("--fix----- ");
} else { }
else {
printf("---------- "); printf("---------- ");
} }
if(attrs.flags & LIBSSH2_SFTP_ATTR_UIDGID) { if(attrs.flags & LIBSSH2_SFTP_ATTR_UIDGID) {
printf("%4d %4d ", (int) attrs.uid, (int) attrs.gid); printf("%4d %4d ", (int) attrs.uid, (int) attrs.gid);
} else { }
else {
printf(" - - "); printf(" - - ");
} }
@ -212,21 +218,22 @@ int main(int argc, char *argv[])
printf("%s\n", mem); printf("%s\n", mem);
} }
else if (rc == LIBSSH2_ERROR_EAGAIN) { else if(rc == LIBSSH2_ERROR_EAGAIN) {
/* blocking */ /* blocking */
fprintf(stderr, "Blocking\n"); fprintf(stderr, "Blocking\n");
} else { }
else {
break; break;
} }
} while (1); } while(1);
libssh2_sftp_closedir(sftp_handle); libssh2_sftp_closedir(sftp_handle);
libssh2_sftp_shutdown(sftp_session); libssh2_sftp_shutdown(sftp_session);
shutdown: shutdown:
libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing"); libssh2_session_disconnect(session, "Normal Shutdown");
libssh2_session_free(session); libssh2_session_free(session);
#ifdef WIN32 #ifdef WIN32

Просмотреть файл

@ -37,10 +37,10 @@
#include <ctype.h> #include <ctype.h>
const char *keyfile1="~/.ssh/id_rsa.pub"; const char *keyfile1 = "~/.ssh/id_rsa.pub";
const char *keyfile2="~/.ssh/id_rsa"; const char *keyfile2 = "~/.ssh/id_rsa";
const char *username="username"; const char *username = "username";
const char *password="password"; const char *password = "password";
static void kbd_callback(const char *name, int name_len, static void kbd_callback(const char *name, int name_len,
@ -54,7 +54,7 @@ static void kbd_callback(const char *name, int name_len,
(void)name_len; (void)name_len;
(void)instruction; (void)instruction;
(void)instruction_len; (void)instruction_len;
if (num_prompts == 1) { if(num_prompts == 1) {
responses[0].text = strdup(password); responses[0].text = strdup(password);
responses[0].length = strlen(password); responses[0].length = strlen(password);
} }
@ -77,16 +77,17 @@ int main(int argc, char *argv[])
WSADATA wsadata; WSADATA wsadata;
int err; int err;
err = WSAStartup(MAKEWORD(2,0), &wsadata); err = WSAStartup(MAKEWORD(2, 0), &wsadata);
if (err != 0) { if(err != 0) {
fprintf(stderr, "WSAStartup failed with error: %d\n", err); fprintf(stderr, "WSAStartup failed with error: %d\n", err);
return 1; return 1;
} }
#endif #endif
if (argc > 1) { if(argc > 1) {
hostaddr = inet_addr(argv[1]); hostaddr = inet_addr(argv[1]);
} else { }
else {
hostaddr = htonl(0x7F000001); hostaddr = htonl(0x7F000001);
} }
@ -97,9 +98,9 @@ int main(int argc, char *argv[])
password = argv[3]; password = argv[3];
} }
rc = libssh2_init (0); rc = libssh2_init(0);
if (rc != 0) { if(rc != 0) {
fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
return 1; return 1;
} }
@ -111,7 +112,7 @@ int main(int argc, char *argv[])
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_port = htons(22); sin.sin_port = htons(22);
sin.sin_addr.s_addr = hostaddr; sin.sin_addr.s_addr = hostaddr;
if (connect(sock, (struct sockaddr*)(&sin), if(connect(sock, (struct sockaddr*)(&sin),
sizeof(struct sockaddr_in)) != 0) { sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n"); fprintf(stderr, "failed to connect!\n");
return -1; return -1;
@ -121,7 +122,7 @@ int main(int argc, char *argv[])
* banners, exchange keys, and setup crypto, compression, and MAC layers * banners, exchange keys, and setup crypto, compression, and MAC layers
*/ */
session = libssh2_session_init(); session = libssh2_session_init();
if (libssh2_session_handshake(session, sock)) { if(libssh2_session_handshake(session, sock)) {
fprintf(stderr, "Failure establishing SSH session\n"); fprintf(stderr, "Failure establishing SSH session\n");
return -1; return -1;
} }
@ -141,64 +142,71 @@ int main(int argc, char *argv[])
/* check what authentication methods are available */ /* check what authentication methods are available */
userauthlist = libssh2_userauth_list(session, username, strlen(username)); userauthlist = libssh2_userauth_list(session, username, strlen(username));
fprintf(stderr, "Authentication methods: %s\n", userauthlist); fprintf(stderr, "Authentication methods: %s\n", userauthlist);
if (strstr(userauthlist, "password") != NULL) { if(strstr(userauthlist, "password") != NULL) {
auth_pw |= 1; auth_pw |= 1;
} }
if (strstr(userauthlist, "keyboard-interactive") != NULL) { if(strstr(userauthlist, "keyboard-interactive") != NULL) {
auth_pw |= 2; auth_pw |= 2;
} }
if (strstr(userauthlist, "publickey") != NULL) { if(strstr(userauthlist, "publickey") != NULL) {
auth_pw |= 4; auth_pw |= 4;
} }
/* if we got an 4. argument we set this option if supported */ /* if we got an 4. argument we set this option if supported */
if(argc > 4) { if(argc > 4) {
if ((auth_pw & 1) && !strcasecmp(argv[4], "-p")) { if((auth_pw & 1) && !strcasecmp(argv[4], "-p")) {
auth_pw = 1; auth_pw = 1;
} }
if ((auth_pw & 2) && !strcasecmp(argv[4], "-i")) { if((auth_pw & 2) && !strcasecmp(argv[4], "-i")) {
auth_pw = 2; auth_pw = 2;
} }
if ((auth_pw & 4) && !strcasecmp(argv[4], "-k")) { if((auth_pw & 4) && !strcasecmp(argv[4], "-k")) {
auth_pw = 4; auth_pw = 4;
} }
} }
if (auth_pw & 1) { if(auth_pw & 1) {
/* We could authenticate via password */ /* We could authenticate via password */
if (libssh2_userauth_password(session, username, password)) { if(libssh2_userauth_password(session, username, password)) {
fprintf(stderr, "\tAuthentication by password failed!\n"); fprintf(stderr, "\tAuthentication by password failed!\n");
goto shutdown; goto shutdown;
} else { }
else {
fprintf(stderr, "\tAuthentication by password succeeded.\n"); fprintf(stderr, "\tAuthentication by password succeeded.\n");
} }
} else if (auth_pw & 2) { }
else if(auth_pw & 2) {
/* Or via keyboard-interactive */ /* Or via keyboard-interactive */
if (libssh2_userauth_keyboard_interactive(session, username, if(libssh2_userauth_keyboard_interactive(session, username,
&kbd_callback) ) { &kbd_callback) ) {
fprintf(stderr, fprintf(stderr,
"\tAuthentication by keyboard-interactive failed!\n"); "\tAuthentication by keyboard-interactive failed!\n");
goto shutdown; goto shutdown;
} else {
fprintf(stderr,
"\tAuthentication by keyboard-interactive succeeded.\n");
} }
} else if (auth_pw & 4) { else {
fprintf(stderr,
"\tAuthentication by keyboard-interactive succeeded.\n");
}
}
else if(auth_pw & 4) {
/* Or by public key */ /* Or by public key */
if (libssh2_userauth_publickey_fromfile(session, username, keyfile1, if(libssh2_userauth_publickey_fromfile(session, username, keyfile1,
keyfile2, password)) { keyfile2, password)) {
fprintf(stderr, "\tAuthentication by public key failed!\n"); fprintf(stderr, "\tAuthentication by public key failed!\n");
goto shutdown; goto shutdown;
} else { }
else {
fprintf(stderr, "\tAuthentication by public key succeeded.\n"); fprintf(stderr, "\tAuthentication by public key succeeded.\n");
} }
} else { }
else {
fprintf(stderr, "No supported authentication methods found!\n"); fprintf(stderr, "No supported authentication methods found!\n");
goto shutdown; goto shutdown;
} }
/* Request a shell */ /* Request a shell */
if (!(channel = libssh2_channel_open_session(session))) { channel = libssh2_channel_open_session(session);
if(!channel) {
fprintf(stderr, "Unable to open a session\n"); fprintf(stderr, "Unable to open a session\n");
goto shutdown; goto shutdown;
} }
@ -211,13 +219,13 @@ int main(int argc, char *argv[])
/* Request a terminal with 'vanilla' terminal emulation /* Request a terminal with 'vanilla' terminal emulation
* See /etc/termcap for more options * See /etc/termcap for more options
*/ */
if (libssh2_channel_request_pty(channel, "vanilla")) { if(libssh2_channel_request_pty(channel, "vanilla")) {
fprintf(stderr, "Failed requesting pty\n"); fprintf(stderr, "Failed requesting pty\n");
goto skip_shell; goto skip_shell;
} }
/* Open a SHELL on that pty */ /* Open a SHELL on that pty */
if (libssh2_channel_shell(channel)) { if(libssh2_channel_shell(channel)) {
fprintf(stderr, "Unable to request shell on allocated pty\n"); fprintf(stderr, "Unable to request shell on allocated pty\n");
goto shutdown; goto shutdown;
} }
@ -236,7 +244,7 @@ int main(int argc, char *argv[])
*/ */
skip_shell: skip_shell:
if (channel) { if(channel) {
libssh2_channel_free(channel); libssh2_channel_free(channel);
channel = NULL; channel = NULL;
} }

Просмотреть файл

@ -36,7 +36,7 @@
#include <ctype.h> #include <ctype.h>
#include <stdlib.h> #include <stdlib.h>
const char *username="username"; const char *username = "username";
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
@ -54,16 +54,17 @@ int main(int argc, char *argv[])
WSADATA wsadata; WSADATA wsadata;
int err; int err;
err = WSAStartup(MAKEWORD(2,0), &wsadata); err = WSAStartup(MAKEWORD(2, 0), &wsadata);
if (err != 0) { if(err != 0) {
fprintf(stderr, "WSAStartup failed with error: %d\n", err); fprintf(stderr, "WSAStartup failed with error: %d\n", err);
return 1; return 1;
} }
#endif #endif
if (argc > 1) { if(argc > 1) {
hostaddr = inet_addr(argv[1]); hostaddr = inet_addr(argv[1]);
} else { }
else {
hostaddr = htonl(0x7F000001); hostaddr = htonl(0x7F000001);
} }
@ -71,9 +72,9 @@ int main(int argc, char *argv[])
username = argv[2]; username = argv[2];
} }
rc = libssh2_init (0); rc = libssh2_init(0);
if (rc != 0) { if(rc != 0) {
fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
return 1; return 1;
} }
@ -81,7 +82,7 @@ int main(int argc, char *argv[])
* responsible for creating the socket establishing the connection * responsible for creating the socket establishing the connection
*/ */
sock = socket(AF_INET, SOCK_STREAM, 0); sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1) { if(sock == -1) {
fprintf(stderr, "failed to create socket!\n"); fprintf(stderr, "failed to create socket!\n");
rc = 1; rc = 1;
goto shutdown; goto shutdown;
@ -90,7 +91,7 @@ int main(int argc, char *argv[])
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_port = htons(22); sin.sin_port = htons(22);
sin.sin_addr.s_addr = hostaddr; sin.sin_addr.s_addr = hostaddr;
if (connect(sock, (struct sockaddr*)(&sin), if(connect(sock, (struct sockaddr*)(&sin),
sizeof(struct sockaddr_in)) != 0) { sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n"); fprintf(stderr, "failed to connect!\n");
goto shutdown; goto shutdown;
@ -100,7 +101,7 @@ int main(int argc, char *argv[])
* banners, exchange keys, and setup crypto, compression, and MAC layers * banners, exchange keys, and setup crypto, compression, and MAC layers
*/ */
session = libssh2_session_init(); session = libssh2_session_init();
if (libssh2_session_handshake(session, sock)) { if(libssh2_session_handshake(session, sock)) {
fprintf(stderr, "Failure establishing SSH session\n"); fprintf(stderr, "Failure establishing SSH session\n");
return 1; return 1;
} }
@ -120,43 +121,44 @@ int main(int argc, char *argv[])
/* check what authentication methods are available */ /* check what authentication methods are available */
userauthlist = libssh2_userauth_list(session, username, strlen(username)); userauthlist = libssh2_userauth_list(session, username, strlen(username));
fprintf(stderr, "Authentication methods: %s\n", userauthlist); fprintf(stderr, "Authentication methods: %s\n", userauthlist);
if (strstr(userauthlist, "publickey") == NULL) { if(strstr(userauthlist, "publickey") == NULL) {
fprintf(stderr, "\"publickey\" authentication is not supported\n"); fprintf(stderr, "\"publickey\" authentication is not supported\n");
goto shutdown; goto shutdown;
} }
/* Connect to the ssh-agent */ /* Connect to the ssh-agent */
agent = libssh2_agent_init(session); agent = libssh2_agent_init(session);
if (!agent) { if(!agent) {
fprintf(stderr, "Failure initializing ssh-agent support\n"); fprintf(stderr, "Failure initializing ssh-agent support\n");
rc = 1; rc = 1;
goto shutdown; goto shutdown;
} }
if (libssh2_agent_connect(agent)) { if(libssh2_agent_connect(agent)) {
fprintf(stderr, "Failure connecting to ssh-agent\n"); fprintf(stderr, "Failure connecting to ssh-agent\n");
rc = 1; rc = 1;
goto shutdown; goto shutdown;
} }
if (libssh2_agent_list_identities(agent)) { if(libssh2_agent_list_identities(agent)) {
fprintf(stderr, "Failure requesting identities to ssh-agent\n"); fprintf(stderr, "Failure requesting identities to ssh-agent\n");
rc = 1; rc = 1;
goto shutdown; goto shutdown;
} }
while (1) { while(1) {
rc = libssh2_agent_get_identity(agent, &identity, prev_identity); rc = libssh2_agent_get_identity(agent, &identity, prev_identity);
if (rc == 1) if(rc == 1)
break; break;
if (rc < 0) { if(rc < 0) {
fprintf(stderr, fprintf(stderr,
"Failure obtaining identity from ssh-agent support\n"); "Failure obtaining identity from ssh-agent support\n");
rc = 1; rc = 1;
goto shutdown; goto shutdown;
} }
if (libssh2_agent_userauth(agent, username, identity)) { if(libssh2_agent_userauth(agent, username, identity)) {
fprintf(stderr, "\tAuthentication with username %s and " fprintf(stderr, "\tAuthentication with username %s and "
"public key %s failed!\n", "public key %s failed!\n",
username, identity->comment); username, identity->comment);
} else { }
else {
fprintf(stderr, "\tAuthentication with username %s and " fprintf(stderr, "\tAuthentication with username %s and "
"public key %s succeeded!\n", "public key %s succeeded!\n",
username, identity->comment); username, identity->comment);
@ -164,7 +166,7 @@ int main(int argc, char *argv[])
} }
prev_identity = identity; prev_identity = identity;
} }
if (rc) { if(rc) {
fprintf(stderr, "Couldn't continue authentication\n"); fprintf(stderr, "Couldn't continue authentication\n");
goto shutdown; goto shutdown;
} }
@ -172,7 +174,8 @@ int main(int argc, char *argv[])
/* We're authenticated now. */ /* We're authenticated now. */
/* Request a shell */ /* Request a shell */
if (!(channel = libssh2_channel_open_session(session))) { channel = libssh2_channel_open_session(session);
if(!channel) {
fprintf(stderr, "Unable to open a session\n"); fprintf(stderr, "Unable to open a session\n");
goto shutdown; goto shutdown;
} }
@ -185,13 +188,13 @@ int main(int argc, char *argv[])
/* Request a terminal with 'vanilla' terminal emulation /* Request a terminal with 'vanilla' terminal emulation
* See /etc/termcap for more options * See /etc/termcap for more options
*/ */
if (libssh2_channel_request_pty(channel, "vanilla")) { if(libssh2_channel_request_pty(channel, "vanilla")) {
fprintf(stderr, "Failed requesting pty\n"); fprintf(stderr, "Failed requesting pty\n");
goto skip_shell; goto skip_shell;
} }
/* Open a SHELL on that pty */ /* Open a SHELL on that pty */
if (libssh2_channel_shell(channel)) { if(libssh2_channel_shell(channel)) {
fprintf(stderr, "Unable to request shell on allocated pty\n"); fprintf(stderr, "Unable to request shell on allocated pty\n");
goto shutdown; goto shutdown;
} }
@ -210,7 +213,7 @@ int main(int argc, char *argv[])
*/ */
skip_shell: skip_shell:
if (channel) { if(channel) {
libssh2_channel_free(channel); libssh2_channel_free(channel);
channel = NULL; channel = NULL;
} }
@ -223,18 +226,18 @@ int main(int argc, char *argv[])
shutdown: shutdown:
if (agent) { if(agent) {
libssh2_agent_disconnect(agent); libssh2_agent_disconnect(agent);
libssh2_agent_free(agent); libssh2_agent_free(agent);
} }
if(session) { if(session) {
libssh2_session_disconnect(session, libssh2_session_disconnect(session,
"Normal Shutdown, Thank you for playing"); "Normal Shutdown, Thank you for playing");
libssh2_session_free(session); libssh2_session_free(session);
} }
if (sock != -1) { if(sock != -1) {
#ifdef WIN32 #ifdef WIN32
closesocket(sock); closesocket(sock);
#else #else

Просмотреть файл

@ -87,7 +87,7 @@ int main(int argc, char *argv[])
LIBSSH2_CHANNEL *channel; LIBSSH2_CHANNEL *channel;
int rc; int rc;
int exitcode = 0; int exitcode = 0;
char *exitsignal=(char *)"none"; char *exitsignal = (char *)"none";
size_t len; size_t len;
LIBSSH2_KNOWNHOSTS *nh; LIBSSH2_KNOWNHOSTS *nh;
int type; int type;
@ -96,27 +96,27 @@ int main(int argc, char *argv[])
WSADATA wsadata; WSADATA wsadata;
int err; int err;
err = WSAStartup(MAKEWORD(2,0), &wsadata); err = WSAStartup(MAKEWORD(2, 0), &wsadata);
if (err != 0) { if(err != 0) {
fprintf(stderr, "WSAStartup failed with error: %d\n", err); fprintf(stderr, "WSAStartup failed with error: %d\n", err);
return 1; return 1;
} }
#endif #endif
if (argc > 1) if(argc > 1)
/* must be ip address only */ /* must be ip address only */
hostname = argv[1]; hostname = argv[1];
if (argc > 2) { if(argc > 2) {
username = argv[2]; username = argv[2];
} }
if (argc > 3) { if(argc > 3) {
password = argv[3]; password = argv[3];
} }
rc = libssh2_init (0); rc = libssh2_init(0);
if (rc != 0) { if(rc != 0) {
fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
return 1; return 1;
} }
@ -131,7 +131,7 @@ int main(int argc, char *argv[])
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_port = htons(22); sin.sin_port = htons(22);
sin.sin_addr.s_addr = hostaddr; sin.sin_addr.s_addr = hostaddr;
if (connect(sock, (struct sockaddr*)(&sin), if(connect(sock, (struct sockaddr*)(&sin),
sizeof(struct sockaddr_in)) != 0) { sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n"); fprintf(stderr, "failed to connect!\n");
return -1; return -1;
@ -139,7 +139,7 @@ int main(int argc, char *argv[])
/* Create a session instance */ /* Create a session instance */
session = libssh2_session_init(); session = libssh2_session_init();
if (!session) if(!session)
return -1; return -1;
/* tell libssh2 we want it all done non-blocking */ /* tell libssh2 we want it all done non-blocking */
@ -148,9 +148,9 @@ int main(int argc, char *argv[])
/* ... start it up. This will trade welcome banners, exchange keys, /* ... start it up. This will trade welcome banners, exchange keys,
* and setup crypto, compression, and MAC layers * and setup crypto, compression, and MAC layers
*/ */
while ((rc = libssh2_session_handshake(session, sock)) == while((rc = libssh2_session_handshake(session, sock)) ==
LIBSSH2_ERROR_EAGAIN); LIBSSH2_ERROR_EAGAIN);
if (rc) { if(rc) {
fprintf(stderr, "Failure establishing SSH session: %d\n", rc); fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
return -1; return -1;
} }
@ -193,11 +193,11 @@ int main(int argc, char *argv[])
} }
libssh2_knownhost_free(nh); libssh2_knownhost_free(nh);
if ( strlen(password) != 0 ) { if(strlen(password) != 0) {
/* We could authenticate via password */ /* We could authenticate via password */
while ((rc = libssh2_userauth_password(session, username, password)) == while((rc = libssh2_userauth_password(session, username, password)) ==
LIBSSH2_ERROR_EAGAIN); LIBSSH2_ERROR_EAGAIN);
if (rc) { if(rc) {
fprintf(stderr, "Authentication by password failed.\n"); fprintf(stderr, "Authentication by password failed.\n");
exit(1); exit(1);
} }
@ -206,22 +206,22 @@ int main(int argc, char *argv[])
libssh2_trace(session, LIBSSH2_TRACE_SOCKET); libssh2_trace(session, LIBSSH2_TRACE_SOCKET);
/* Exec non-blocking on the remove host */ /* Exec non-blocking on the remove host */
while( (channel = libssh2_channel_open_session(session)) == NULL && while((channel = libssh2_channel_open_session(session)) == NULL &&
libssh2_session_last_error(session,NULL,NULL,0) == libssh2_session_last_error(session, NULL, NULL, 0) ==
LIBSSH2_ERROR_EAGAIN ) { LIBSSH2_ERROR_EAGAIN) {
waitsocket(sock, session); waitsocket(sock, session);
} }
if( channel == NULL ) { if(channel == NULL) {
fprintf(stderr,"Error\n"); fprintf(stderr, "Error\n");
exit( 1 ); exit(1);
} }
while( (rc = libssh2_channel_exec(channel, commandline)) == while((rc = libssh2_channel_exec(channel, commandline)) ==
LIBSSH2_ERROR_EAGAIN ) LIBSSH2_ERROR_EAGAIN)
waitsocket(sock, session); waitsocket(sock, session);
if( rc != 0 ) { if(rc != 0) {
fprintf(stderr, "exec error\n"); fprintf(stderr, "exec error\n");
exit( 1 ); exit(1);
} }
else { else {
LIBSSH2_POLLFD *fds = NULL; LIBSSH2_POLLFD *fds = NULL;
@ -236,10 +236,11 @@ int main(int argc, char *argv[])
int rewrites = 0; int rewrites = 0;
int i; int i;
for (i = 0; i < BUFSIZE; i++) for(i = 0; i < BUFSIZE; i++)
buffer[i] = 'A'; buffer[i] = 'A';
if ((fds = malloc (sizeof (LIBSSH2_POLLFD))) == NULL) { fds = malloc(sizeof (LIBSSH2_POLLFD));
if(!fds) {
fprintf(stderr, "malloc failed\n"); fprintf(stderr, "malloc failed\n");
exit(1); exit(1);
} }
@ -252,18 +253,18 @@ int main(int argc, char *argv[])
int rc = (libssh2_poll(fds, 1, 10)); int rc = (libssh2_poll(fds, 1, 10));
int act = 0; int act = 0;
if (rc < 1) if(rc < 1)
continue; continue;
if (fds[0].revents & LIBSSH2_POLLFD_POLLIN) { if(fds[0].revents & LIBSSH2_POLLFD_POLLIN) {
int n = libssh2_channel_read(channel, buffer, sizeof(buffer)); int n = libssh2_channel_read(channel, buffer, sizeof(buffer));
act++; act++;
if (n == LIBSSH2_ERROR_EAGAIN) { if(n == LIBSSH2_ERROR_EAGAIN) {
rereads++; rereads++;
fprintf(stderr, "will read again\n"); fprintf(stderr, "will read again\n");
} }
else if (n < 0) { else if(n < 0) {
fprintf(stderr, "read failed\n"); fprintf(stderr, "read failed\n");
exit(1); exit(1);
} }
@ -274,20 +275,20 @@ int main(int argc, char *argv[])
} }
} }
if (fds[0].revents & LIBSSH2_POLLFD_POLLOUT) { if(fds[0].revents & LIBSSH2_POLLFD_POLLOUT) {
act++; act++;
if (totwritten < totsize) { if(totwritten < totsize) {
/* we have not written all data yet */ /* we have not written all data yet */
int left = totsize - totwritten; int left = totsize - totwritten;
int size = (left < bufsize) ? left : bufsize; int size = (left < bufsize) ? left : bufsize;
int n = libssh2_channel_write_ex(channel, 0, buffer, size); int n = libssh2_channel_write_ex(channel, 0, buffer, size);
if (n == LIBSSH2_ERROR_EAGAIN) { if(n == LIBSSH2_ERROR_EAGAIN) {
rewrites++; rewrites++;
fprintf(stderr, "will write again\n"); fprintf(stderr, "will write again\n");
} }
else if (n < 0) { else if(n < 0) {
fprintf(stderr, "write failed\n"); fprintf(stderr, "write failed\n");
exit(1); exit(1);
} }
@ -295,20 +296,21 @@ int main(int argc, char *argv[])
totwritten += n; totwritten += n;
fprintf(stderr, "wrote %d bytes (%d in total)", fprintf(stderr, "wrote %d bytes (%d in total)",
n, totwritten); n, totwritten);
if (left >= bufsize && n != bufsize) { if(left >= bufsize && n != bufsize) {
partials++; partials++;
fprintf(stderr, " PARTIAL"); fprintf(stderr, " PARTIAL");
} }
fprintf(stderr, "\n"); fprintf(stderr, "\n");
} }
} else { }
else {
/* all data written, send EOF */ /* all data written, send EOF */
rc = libssh2_channel_send_eof(channel); rc = libssh2_channel_send_eof(channel);
if (rc == LIBSSH2_ERROR_EAGAIN) { if(rc == LIBSSH2_ERROR_EAGAIN) {
fprintf(stderr, "will send eof again\n"); fprintf(stderr, "will send eof again\n");
} }
else if (rc < 0) { else if(rc < 0) {
fprintf(stderr, "send eof failed\n"); fprintf(stderr, "send eof failed\n");
exit(1); exit(1);
} }
@ -320,23 +322,23 @@ int main(int argc, char *argv[])
} }
} }
if (fds[0].revents & LIBSSH2_POLLFD_CHANNEL_CLOSED) { if(fds[0].revents & LIBSSH2_POLLFD_CHANNEL_CLOSED) {
if (!act) /* don't leave loop until we have read all data */ if(!act) /* don't leave loop until we have read all data */
running = 0; running = 0;
} }
} while(running); } while(running);
exitcode = 127; exitcode = 127;
while( (rc = libssh2_channel_close(channel)) == LIBSSH2_ERROR_EAGAIN ) while((rc = libssh2_channel_close(channel)) == LIBSSH2_ERROR_EAGAIN)
waitsocket(sock, session); waitsocket(sock, session);
if( rc == 0 ) { if(rc == 0) {
exitcode = libssh2_channel_get_exit_status( channel ); exitcode = libssh2_channel_get_exit_status(channel);
libssh2_channel_get_exit_signal(channel, &exitsignal, libssh2_channel_get_exit_signal(channel, &exitsignal,
NULL, NULL, NULL, NULL, NULL); NULL, NULL, NULL, NULL, NULL);
} }
if (exitsignal) if(exitsignal)
fprintf(stderr, "\nGot signal: %s\n", exitsignal); fprintf(stderr, "\nGot signal: %s\n", exitsignal);
libssh2_channel_free(channel); libssh2_channel_free(channel);
@ -345,7 +347,7 @@ int main(int argc, char *argv[])
fprintf(stderr, "\nrereads: %d rewrites: %d totwritten %d\n", fprintf(stderr, "\nrereads: %d rewrites: %d totwritten %d\n",
rereads, rewrites, totwritten); rereads, rewrites, totwritten);
if (totwritten != totread) { if(totwritten != totread) {
fprintf(stderr, "\n*** FAIL bytes written: %d bytes " fprintf(stderr, "\n*** FAIL bytes written: %d bytes "
"read: %d ***\n", totwritten, totread); "read: %d ***\n", totwritten, totread);
exit(1); exit(1);

Просмотреть файл

@ -87,7 +87,7 @@ int main(int argc, char *argv[])
LIBSSH2_CHANNEL *channel; LIBSSH2_CHANNEL *channel;
int rc; int rc;
int exitcode; int exitcode;
char *exitsignal=(char *)"none"; char *exitsignal = (char *)"none";
int bytecount = 0; int bytecount = 0;
size_t len; size_t len;
LIBSSH2_KNOWNHOSTS *nh; LIBSSH2_KNOWNHOSTS *nh;
@ -97,30 +97,30 @@ int main(int argc, char *argv[])
WSADATA wsadata; WSADATA wsadata;
int err; int err;
err = WSAStartup(MAKEWORD(2,0), &wsadata); err = WSAStartup(MAKEWORD(2, 0), &wsadata);
if (err != 0) { if(err != 0) {
fprintf(stderr, "WSAStartup failed with error: %d\n", err); fprintf(stderr, "WSAStartup failed with error: %d\n", err);
return 1; return 1;
} }
#endif #endif
if (argc > 1) if(argc > 1)
/* must be ip address only */ /* must be ip address only */
hostname = argv[1]; hostname = argv[1];
if (argc > 2) { if(argc > 2) {
username = argv[2]; username = argv[2];
} }
if (argc > 3) { if(argc > 3) {
password = argv[3]; password = argv[3];
} }
if (argc > 4) { if(argc > 4) {
commandline = argv[4]; commandline = argv[4];
} }
rc = libssh2_init (0); rc = libssh2_init(0);
if (rc != 0) { if(rc != 0) {
fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
return 1; return 1;
} }
@ -135,7 +135,7 @@ int main(int argc, char *argv[])
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_port = htons(22); sin.sin_port = htons(22);
sin.sin_addr.s_addr = hostaddr; sin.sin_addr.s_addr = hostaddr;
if (connect(sock, (struct sockaddr*)(&sin), if(connect(sock, (struct sockaddr*)(&sin),
sizeof(struct sockaddr_in)) != 0) { sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n"); fprintf(stderr, "failed to connect!\n");
return -1; return -1;
@ -143,7 +143,7 @@ int main(int argc, char *argv[])
/* Create a session instance */ /* Create a session instance */
session = libssh2_session_init(); session = libssh2_session_init();
if (!session) if(!session)
return -1; return -1;
/* tell libssh2 we want it all done non-blocking */ /* tell libssh2 we want it all done non-blocking */
@ -152,9 +152,9 @@ int main(int argc, char *argv[])
/* ... start it up. This will trade welcome banners, exchange keys, /* ... start it up. This will trade welcome banners, exchange keys,
* and setup crypto, compression, and MAC layers * and setup crypto, compression, and MAC layers
*/ */
while ((rc = libssh2_session_handshake(session, sock)) == while((rc = libssh2_session_handshake(session, sock)) ==
LIBSSH2_ERROR_EAGAIN); LIBSSH2_ERROR_EAGAIN);
if (rc) { if(rc) {
fprintf(stderr, "Failure establishing SSH session: %d\n", rc); fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
return -1; return -1;
} }
@ -206,104 +206,95 @@ int main(int argc, char *argv[])
} }
libssh2_knownhost_free(nh); libssh2_knownhost_free(nh);
if ( strlen(password) != 0 ) { if(strlen(password) != 0) {
/* We could authenticate via password */ /* We could authenticate via password */
while ((rc = libssh2_userauth_password(session, username, password)) == while((rc = libssh2_userauth_password(session, username, password)) ==
LIBSSH2_ERROR_EAGAIN); LIBSSH2_ERROR_EAGAIN);
if (rc) { if(rc) {
fprintf(stderr, "Authentication by password failed.\n"); fprintf(stderr, "Authentication by password failed.\n");
goto shutdown; goto shutdown;
} }
} }
else { else {
/* Or by public key */ /* Or by public key */
while ((rc = libssh2_userauth_publickey_fromfile(session, username, while((rc = libssh2_userauth_publickey_fromfile(session, username,
"/home/user/" "/home/user/"
".ssh/id_rsa.pub", ".ssh/id_rsa.pub",
"/home/user/" "/home/user/"
".ssh/id_rsa", ".ssh/id_rsa",
password)) == password)) ==
LIBSSH2_ERROR_EAGAIN); LIBSSH2_ERROR_EAGAIN);
if (rc) { if(rc) {
fprintf(stderr, "\tAuthentication by public key failed\n"); fprintf(stderr, "\tAuthentication by public key failed\n");
goto shutdown; goto shutdown;
} }
} }
#if 0 #if 0
libssh2_trace(session, ~0 ); libssh2_trace(session, ~0);
#endif #endif
/* Exec non-blocking on the remove host */ /* Exec non-blocking on the remove host */
while( (channel = libssh2_channel_open_session(session)) == NULL && while((channel = libssh2_channel_open_session(session)) == NULL &&
libssh2_session_last_error(session,NULL,NULL,0) == libssh2_session_last_error(session, NULL, NULL, 0) ==
LIBSSH2_ERROR_EAGAIN ) LIBSSH2_ERROR_EAGAIN) {
{
waitsocket(sock, session); waitsocket(sock, session);
} }
if( channel == NULL ) if(channel == NULL) {
{ fprintf(stderr, "Error\n");
fprintf(stderr,"Error\n"); exit(1);
exit( 1 );
} }
while( (rc = libssh2_channel_exec(channel, commandline)) == while((rc = libssh2_channel_exec(channel, commandline)) ==
LIBSSH2_ERROR_EAGAIN ) LIBSSH2_ERROR_EAGAIN) {
{
waitsocket(sock, session); waitsocket(sock, session);
} }
if( rc != 0 ) if(rc != 0) {
{ fprintf(stderr, "Error\n");
fprintf(stderr,"Error\n"); exit(1);
exit( 1 );
} }
for( ;; ) for(;;) {
{
/* loop until we block */ /* loop until we block */
int rc; int rc;
do do {
{
char buffer[0x4000]; char buffer[0x4000];
rc = libssh2_channel_read( channel, buffer, sizeof(buffer) ); rc = libssh2_channel_read(channel, buffer, sizeof(buffer) );
if( rc > 0 ) if(rc > 0) {
{
int i; int i;
bytecount += rc; bytecount += rc;
fprintf(stderr, "We read:\n"); fprintf(stderr, "We read:\n");
for( i=0; i < rc; ++i ) for(i = 0; i < rc; ++i)
fputc( buffer[i], stderr); fputc(buffer[i], stderr);
fprintf(stderr, "\n"); fprintf(stderr, "\n");
} }
else { else {
if( rc != LIBSSH2_ERROR_EAGAIN ) if(rc != LIBSSH2_ERROR_EAGAIN)
/* no need to output this for the EAGAIN case */ /* no need to output this for the EAGAIN case */
fprintf(stderr, "libssh2_channel_read returned %d\n", rc); fprintf(stderr, "libssh2_channel_read returned %d\n", rc);
} }
} }
while( rc > 0 ); while(rc > 0);
/* this is due to blocking that would occur otherwise so we loop on /* this is due to blocking that would occur otherwise so we loop on
this condition */ this condition */
if( rc == LIBSSH2_ERROR_EAGAIN ) if(rc == LIBSSH2_ERROR_EAGAIN) {
{
waitsocket(sock, session); waitsocket(sock, session);
} }
else else
break; break;
} }
exitcode = 127; exitcode = 127;
while( (rc = libssh2_channel_close(channel)) == LIBSSH2_ERROR_EAGAIN ) while((rc = libssh2_channel_close(channel)) == LIBSSH2_ERROR_EAGAIN)
waitsocket(sock, session); waitsocket(sock, session);
if( rc == 0 ) if(rc == 0) {
{ exitcode = libssh2_channel_get_exit_status(channel);
exitcode = libssh2_channel_get_exit_status( channel );
libssh2_channel_get_exit_signal(channel, &exitsignal, libssh2_channel_get_exit_signal(channel, &exitsignal,
NULL, NULL, NULL, NULL, NULL); NULL, NULL, NULL, NULL, NULL);
} }
if (exitsignal) if(exitsignal)
fprintf(stderr, "\nGot signal: %s\n", exitsignal); fprintf(stderr, "\nGot signal: %s\n", exitsignal);
else else
fprintf(stderr, "\nEXIT: %d bytecount: %d\n", exitcode, bytecount); fprintf(stderr, "\nEXIT: %d bytecount: %d\n", exitcode, bytecount);
libssh2_channel_free(channel); libssh2_channel_free(channel);

Просмотреть файл

@ -57,12 +57,12 @@ static int netconf_write(LIBSSH2_CHANNEL *channel, const char *buf, size_t len)
do { do {
i = libssh2_channel_write(channel, buf, len); i = libssh2_channel_write(channel, buf, len);
if (i < 0) { if(i < 0) {
fprintf(stderr, "libssh2_channel_write: %d\n", i); fprintf(stderr, "libssh2_channel_write: %d\n", i);
return -1; return -1;
} }
wr += i; wr += i;
} while (i > 0 && wr < (ssize_t)len); } while(i > 0 && wr < (ssize_t)len);
return 0; return 0;
} }
@ -78,9 +78,9 @@ static int netconf_read_until(LIBSSH2_CHANNEL *channel, const char *endtag,
do { do {
len = libssh2_channel_read(channel, buf + rd, buflen - rd); len = libssh2_channel_read(channel, buf + rd, buflen - rd);
if (LIBSSH2_ERROR_EAGAIN == len) if(LIBSSH2_ERROR_EAGAIN == len)
continue; continue;
else if (len < 0) { else if(len < 0) {
fprintf(stderr, "libssh2_channel_read: %d\n", (int)len); fprintf(stderr, "libssh2_channel_read: %d\n", (int)len);
return -1; return -1;
} }
@ -92,13 +92,14 @@ static int netconf_read_until(LIBSSH2_CHANNEL *channel, const char *endtag,
/* really, this MUST be replaced with proper XML parsing! */ /* really, this MUST be replaced with proper XML parsing! */
endreply = strstr(buf, endtag); endreply = strstr(buf, endtag);
if (endreply) if(endreply)
specialsequence = strstr(endreply, "]]>]]>"); specialsequence = strstr(endreply, "]]>]]>");
} while (!specialsequence && rd < buflen); } while(!specialsequence && rd < buflen);
if (!specialsequence) { if(!specialsequence) {
fprintf(stderr, "%s: ]]>]]> not found! read buffer too small?\n", __func__); fprintf(stderr, "%s: ]]>]]> not found! read buffer too small?\n",
__func__);
return -1; return -1;
} }
@ -125,8 +126,8 @@ int main(int argc, char *argv[])
WSADATA wsadata; WSADATA wsadata;
int err; int err;
err = WSAStartup(MAKEWORD(2,0), &wsadata); err = WSAStartup(MAKEWORD(2, 0), &wsadata);
if (err != 0) { if(err != 0) {
fprintf(stderr, "WSAStartup failed with error: %d\n", err); fprintf(stderr, "WSAStartup failed with error: %d\n", err);
return 1; return 1;
} }
@ -134,40 +135,41 @@ int main(int argc, char *argv[])
int sock = -1; int sock = -1;
#endif #endif
if (argc > 1) if(argc > 1)
server_ip = argv[1]; server_ip = argv[1];
if (argc > 2) if(argc > 2)
username = argv[2]; username = argv[2];
if (argc > 3) if(argc > 3)
password = argv[3]; password = argv[3];
rc = libssh2_init (0); rc = libssh2_init(0);
if (rc != 0) { if(rc != 0) {
fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
return 1; return 1;
} }
/* Connect to SSH server */ /* Connect to SSH server */
sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
#ifdef WIN32 #ifdef WIN32
if (sock == INVALID_SOCKET) { if(sock == INVALID_SOCKET) {
fprintf(stderr, "failed to open socket!\n"); fprintf(stderr, "failed to open socket!\n");
return -1; return -1;
} }
#else #else
if (sock == -1) { if(sock == -1) {
perror("socket"); perror("socket");
return -1; return -1;
} }
#endif #endif
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
if (INADDR_NONE == (sin.sin_addr.s_addr = inet_addr(server_ip))) { sin.sin_addr.s_addr = inet_addr(server_ip);
if(INADDR_NONE == sin.sin_addr.s_addr) {
fprintf(stderr, "inet_addr: Invalid IP address \"%s\"\n", server_ip); fprintf(stderr, "inet_addr: Invalid IP address \"%s\"\n", server_ip);
return -1; return -1;
} }
sin.sin_port = htons(830); sin.sin_port = htons(830);
if (connect(sock, (struct sockaddr*)(&sin), if(connect(sock, (struct sockaddr*)(&sin),
sizeof(struct sockaddr_in)) != 0) { sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "Failed to connect to %s!\n", inet_ntoa(sin.sin_addr)); fprintf(stderr, "Failed to connect to %s!\n", inet_ntoa(sin.sin_addr));
return -1; return -1;
@ -203,39 +205,41 @@ int main(int argc, char *argv[])
/* check what authentication methods are available */ /* check what authentication methods are available */
userauthlist = libssh2_userauth_list(session, username, strlen(username)); userauthlist = libssh2_userauth_list(session, username, strlen(username));
fprintf(stderr, "Authentication methods: %s\n", userauthlist); fprintf(stderr, "Authentication methods: %s\n", userauthlist);
if (strstr(userauthlist, "password")) if(strstr(userauthlist, "password"))
auth |= AUTH_PASSWORD; auth |= AUTH_PASSWORD;
if (strstr(userauthlist, "publickey")) if(strstr(userauthlist, "publickey"))
auth |= AUTH_PUBLICKEY; auth |= AUTH_PUBLICKEY;
/* check for options */ /* check for options */
if(argc > 4) { if(argc > 4) {
if ((auth & AUTH_PASSWORD) && !strcasecmp(argv[4], "-p")) if((auth & AUTH_PASSWORD) && !strcasecmp(argv[4], "-p"))
auth = AUTH_PASSWORD; auth = AUTH_PASSWORD;
if ((auth & AUTH_PUBLICKEY) && !strcasecmp(argv[4], "-k")) if((auth & AUTH_PUBLICKEY) && !strcasecmp(argv[4], "-k"))
auth = AUTH_PUBLICKEY; auth = AUTH_PUBLICKEY;
} }
if (auth & AUTH_PASSWORD) { if(auth & AUTH_PASSWORD) {
if (libssh2_userauth_password(session, username, password)) { if(libssh2_userauth_password(session, username, password)) {
fprintf(stderr, "Authentication by password failed.\n"); fprintf(stderr, "Authentication by password failed.\n");
goto shutdown; goto shutdown;
} }
} else if (auth & AUTH_PUBLICKEY) { }
if (libssh2_userauth_publickey_fromfile(session, username, keyfile1, else if(auth & AUTH_PUBLICKEY) {
if(libssh2_userauth_publickey_fromfile(session, username, keyfile1,
keyfile2, password)) { keyfile2, password)) {
fprintf(stderr, "Authentication by public key failed!\n"); fprintf(stderr, "Authentication by public key failed!\n");
goto shutdown; goto shutdown;
} }
fprintf(stderr, "Authentication by public key succeeded.\n"); fprintf(stderr, "Authentication by public key succeeded.\n");
} else { }
else {
fprintf(stderr, "No supported authentication methods found!\n"); fprintf(stderr, "No supported authentication methods found!\n");
goto shutdown; goto shutdown;
} }
/* open a channel */ /* open a channel */
channel = libssh2_channel_open_session(session); channel = libssh2_channel_open_session(session);
if (!channel) { if(!channel) {
fprintf(stderr, "Could not open the channel!\n" fprintf(stderr, "Could not open the channel!\n"
"(Note that this can be a problem at the server!" "(Note that this can be a problem at the server!"
" Please review the server logs.)\n"); " Please review the server logs.)\n");
@ -243,7 +247,7 @@ int main(int argc, char *argv[])
} }
/* execute the subsystem on our channel */ /* execute the subsystem on our channel */
if (libssh2_channel_subsystem(channel, "netconf")) { if(libssh2_channel_subsystem(channel, "netconf")) {
fprintf(stderr, "Could not execute the \"netconf\" subsystem!\n" fprintf(stderr, "Could not execute the \"netconf\" subsystem!\n"
"(Note that this can be a problem at the server!" "(Note that this can be a problem at the server!"
" Please review the server logs.)\n"); " Please review the server logs.)\n");
@ -261,15 +265,16 @@ int main(int argc, char *argv[])
"</capabilities>" "</capabilities>"
"</hello>\n" "</hello>\n"
"]]>]]>\n%n", (int *)&len); "]]>]]>\n%n", (int *)&len);
if (-1 == netconf_write(channel, buf, len)) if(-1 == netconf_write(channel, buf, len))
goto shutdown; goto shutdown;
fprintf(stderr, "Reading NETCONF server <hello>\n"); fprintf(stderr, "Reading NETCONF server <hello>\n");
len = netconf_read_until(channel, "</hello>", buf, sizeof(buf)); len = netconf_read_until(channel, "</hello>", buf, sizeof(buf));
if (-1 == len) if(-1 == len)
goto shutdown; goto shutdown;
fprintf(stderr, "Got %d bytes:\n----------------------\n%s", (int)len, buf); fprintf(stderr, "Got %d bytes:\n----------------------\n%s",
(int)len, buf);
fprintf(stderr, "Sending NETCONF <rpc>\n"); fprintf(stderr, "Sending NETCONF <rpc>\n");
snprintf(buf, sizeof(buf), snprintf(buf, sizeof(buf),
@ -278,18 +283,19 @@ int main(int argc, char *argv[])
"<get-interface-information><terse/></get-interface-information>" "<get-interface-information><terse/></get-interface-information>"
"</rpc>\n" "</rpc>\n"
"]]>]]>\n%n", (int *)&len); "]]>]]>\n%n", (int *)&len);
if (-1 == netconf_write(channel, buf, len)) if(-1 == netconf_write(channel, buf, len))
goto shutdown; goto shutdown;
fprintf(stderr, "Reading NETCONF <rpc-reply>\n"); fprintf(stderr, "Reading NETCONF <rpc-reply>\n");
len = netconf_read_until(channel, "</rpc-reply>", buf, sizeof(buf)); len = netconf_read_until(channel, "</rpc-reply>", buf, sizeof(buf));
if (-1 == len) if(-1 == len)
goto shutdown; goto shutdown;
fprintf(stderr, "Got %d bytes:\n----------------------\n%s", (int)len, buf); fprintf(stderr, "Got %d bytes:\n----------------------\n%s",
(int)len, buf);
shutdown: shutdown:
if (channel) if(channel)
libssh2_channel_free(channel); libssh2_channel_free(channel);
libssh2_session_disconnect(session, "Client disconnecting normally"); libssh2_session_disconnect(session, "Client disconnecting normally");
libssh2_session_free(session); libssh2_session_free(session);

Просмотреть файл

@ -70,8 +70,8 @@ int main(int argc, char *argv[])
WSADATA wsadata; WSADATA wsadata;
int err; int err;
err = WSAStartup(MAKEWORD(2,0), &wsadata); err = WSAStartup(MAKEWORD(2, 0), &wsadata);
if (err != 0) { if(err != 0) {
fprintf(stderr, "WSAStartup failed with error: %d\n", err); fprintf(stderr, "WSAStartup failed with error: %d\n", err);
return 1; return 1;
} }
@ -79,48 +79,49 @@ int main(int argc, char *argv[])
int sock = -1, forwardsock = -1; int sock = -1, forwardsock = -1;
#endif #endif
if (argc > 1) if(argc > 1)
server_ip = argv[1]; server_ip = argv[1];
if (argc > 2) if(argc > 2)
username = argv[2]; username = argv[2];
if (argc > 3) if(argc > 3)
password = argv[3]; password = argv[3];
if (argc > 4) if(argc > 4)
remote_listenhost = argv[4]; remote_listenhost = argv[4];
if (argc > 5) if(argc > 5)
remote_wantport = atoi(argv[5]); remote_wantport = atoi(argv[5]);
if (argc > 6) if(argc > 6)
local_destip = argv[6]; local_destip = argv[6];
if (argc > 7) if(argc > 7)
local_destport = atoi(argv[7]); local_destport = atoi(argv[7]);
rc = libssh2_init (0); rc = libssh2_init(0);
if (rc != 0) { if(rc != 0) {
fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
return 1; return 1;
} }
/* Connect to SSH server */ /* Connect to SSH server */
sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
#ifdef WIN32 #ifdef WIN32
if (sock == INVALID_SOCKET) { if(sock == INVALID_SOCKET) {
fprintf(stderr, "failed to open socket!\n"); fprintf(stderr, "failed to open socket!\n");
return -1; return -1;
} }
#else #else
if (sock == -1) { if(sock == -1) {
perror("socket"); perror("socket");
return -1; return -1;
} }
#endif #endif
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
if (INADDR_NONE == (sin.sin_addr.s_addr = inet_addr(server_ip))) { sin.sin_addr.s_addr = inet_addr(server_ip);
if(INADDR_NONE == sin.sin_addr.s_addr) {
perror("inet_addr"); perror("inet_addr");
return -1; return -1;
} }
sin.sin_port = htons(22); sin.sin_port = htons(22);
if (connect(sock, (struct sockaddr*)(&sin), if(connect(sock, (struct sockaddr*)(&sin),
sizeof(struct sockaddr_in)) != 0) { sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n"); fprintf(stderr, "failed to connect!\n");
return -1; return -1;
@ -156,32 +157,34 @@ int main(int argc, char *argv[])
/* check what authentication methods are available */ /* check what authentication methods are available */
userauthlist = libssh2_userauth_list(session, username, strlen(username)); userauthlist = libssh2_userauth_list(session, username, strlen(username));
fprintf(stderr, "Authentication methods: %s\n", userauthlist); fprintf(stderr, "Authentication methods: %s\n", userauthlist);
if (strstr(userauthlist, "password")) if(strstr(userauthlist, "password"))
auth |= AUTH_PASSWORD; auth |= AUTH_PASSWORD;
if (strstr(userauthlist, "publickey")) if(strstr(userauthlist, "publickey"))
auth |= AUTH_PUBLICKEY; auth |= AUTH_PUBLICKEY;
/* check for options */ /* check for options */
if(argc > 8) { if(argc > 8) {
if ((auth & AUTH_PASSWORD) && !strcasecmp(argv[8], "-p")) if((auth & AUTH_PASSWORD) && !strcasecmp(argv[8], "-p"))
auth = AUTH_PASSWORD; auth = AUTH_PASSWORD;
if ((auth & AUTH_PUBLICKEY) && !strcasecmp(argv[8], "-k")) if((auth & AUTH_PUBLICKEY) && !strcasecmp(argv[8], "-k"))
auth = AUTH_PUBLICKEY; auth = AUTH_PUBLICKEY;
} }
if (auth & AUTH_PASSWORD) { if(auth & AUTH_PASSWORD) {
if (libssh2_userauth_password(session, username, password)) { if(libssh2_userauth_password(session, username, password)) {
fprintf(stderr, "Authentication by password failed.\n"); fprintf(stderr, "Authentication by password failed.\n");
goto shutdown; goto shutdown;
} }
} else if (auth & AUTH_PUBLICKEY) { }
if (libssh2_userauth_publickey_fromfile(session, username, keyfile1, else if(auth & AUTH_PUBLICKEY) {
keyfile2, password)) { if(libssh2_userauth_publickey_fromfile(session, username, keyfile1,
keyfile2, password)) {
fprintf(stderr, "\tAuthentication by public key failed!\n"); fprintf(stderr, "\tAuthentication by public key failed!\n");
goto shutdown; goto shutdown;
} }
fprintf(stderr, "\tAuthentication by public key succeeded.\n"); fprintf(stderr, "\tAuthentication by public key succeeded.\n");
} else { }
else {
fprintf(stderr, "No supported authentication methods found!\n"); fprintf(stderr, "No supported authentication methods found!\n");
goto shutdown; goto shutdown;
} }
@ -191,7 +194,7 @@ int main(int argc, char *argv[])
listener = libssh2_channel_forward_listen_ex(session, remote_listenhost, listener = libssh2_channel_forward_listen_ex(session, remote_listenhost,
remote_wantport, &remote_listenport, 1); remote_wantport, &remote_listenport, 1);
if (!listener) { if(!listener) {
fprintf(stderr, "Could not start the tcpip-forward listener!\n" fprintf(stderr, "Could not start the tcpip-forward listener!\n"
"(Note that this can be a problem at the server!" "(Note that this can be a problem at the server!"
" Please review the server logs.)\n"); " Please review the server logs.)\n");
@ -203,7 +206,7 @@ int main(int argc, char *argv[])
fprintf(stderr, "Waiting for remote connection\n"); fprintf(stderr, "Waiting for remote connection\n");
channel = libssh2_channel_forward_accept(listener); channel = libssh2_channel_forward_accept(listener);
if (!channel) { if(!channel) {
fprintf(stderr, "Could not accept connection!\n" fprintf(stderr, "Could not accept connection!\n"
"(Note that this can be a problem at the server!" "(Note that this can be a problem at the server!"
" Please review the server logs.)\n"); " Please review the server logs.)\n");
@ -215,12 +218,12 @@ int main(int argc, char *argv[])
local_destip, local_destport); local_destip, local_destport);
forwardsock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); forwardsock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
#ifdef WIN32 #ifdef WIN32
if (forwardsock == INVALID_SOCKET) { if(forwardsock == INVALID_SOCKET) {
fprintf(stderr, "failed to open forward socket!\n"); fprintf(stderr, "failed to open forward socket!\n");
goto shutdown; goto shutdown;
} }
#else #else
if (forwardsock == -1) { if(forwardsock == -1) {
perror("socket"); perror("socket");
goto shutdown; goto shutdown;
} }
@ -228,11 +231,12 @@ int main(int argc, char *argv[])
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_port = htons(local_destport); sin.sin_port = htons(local_destport);
if (INADDR_NONE == (sin.sin_addr.s_addr = inet_addr(local_destip))) { sin.sin_addr.s_addr = inet_addr(local_destip);
if(INADDR_NONE == sin.sin_addr.s_addr) {
perror("inet_addr"); perror("inet_addr");
goto shutdown; goto shutdown;
} }
if (-1 == connect(forwardsock, (struct sockaddr *)&sin, sinlen)) { if(-1 == connect(forwardsock, (struct sockaddr *)&sin, sinlen)) {
perror("connect"); perror("connect");
goto shutdown; goto shutdown;
} }
@ -243,22 +247,23 @@ int main(int argc, char *argv[])
/* Must use non-blocking IO hereafter due to the current libssh2 API */ /* Must use non-blocking IO hereafter due to the current libssh2 API */
libssh2_session_set_blocking(session, 0); libssh2_session_set_blocking(session, 0);
while (1) { while(1) {
FD_ZERO(&fds); FD_ZERO(&fds);
FD_SET(forwardsock, &fds); FD_SET(forwardsock, &fds);
tv.tv_sec = 0; tv.tv_sec = 0;
tv.tv_usec = 100000; tv.tv_usec = 100000;
rc = select(forwardsock + 1, &fds, NULL, NULL, &tv); rc = select(forwardsock + 1, &fds, NULL, NULL, &tv);
if (-1 == rc) { if(-1 == rc) {
perror("select"); perror("select");
goto shutdown; goto shutdown;
} }
if (rc && FD_ISSET(forwardsock, &fds)) { if(rc && FD_ISSET(forwardsock, &fds)) {
len = recv(forwardsock, buf, sizeof(buf), 0); len = recv(forwardsock, buf, sizeof(buf), 0);
if (len < 0) { if(len < 0) {
perror("read"); perror("read");
goto shutdown; goto shutdown;
} else if (0 == len) { }
else if(0 == len) {
fprintf(stderr, "The local server at %s:%d disconnected!\n", fprintf(stderr, "The local server at %s:%d disconnected!\n",
local_destip, local_destport); local_destip, local_destport);
goto shutdown; goto shutdown;
@ -266,31 +271,31 @@ int main(int argc, char *argv[])
wr = 0; wr = 0;
do { do {
i = libssh2_channel_write(channel, buf, len); i = libssh2_channel_write(channel, buf, len);
if (i < 0) { if(i < 0) {
fprintf(stderr, "libssh2_channel_write: %d\n", i); fprintf(stderr, "libssh2_channel_write: %d\n", i);
goto shutdown; goto shutdown;
} }
wr += i; wr += i;
} while(i > 0 && wr < len); } while(i > 0 && wr < len);
} }
while (1) { while(1) {
len = libssh2_channel_read(channel, buf, sizeof(buf)); len = libssh2_channel_read(channel, buf, sizeof(buf));
if (LIBSSH2_ERROR_EAGAIN == len) if(LIBSSH2_ERROR_EAGAIN == len)
break; break;
else if (len < 0) { else if(len < 0) {
fprintf(stderr, "libssh2_channel_read: %d", (int)len); fprintf(stderr, "libssh2_channel_read: %d", (int)len);
goto shutdown; goto shutdown;
} }
wr = 0; wr = 0;
while (wr < len) { while(wr < len) {
i = send(forwardsock, buf + wr, len - wr, 0); i = send(forwardsock, buf + wr, len - wr, 0);
if (i <= 0) { if(i <= 0) {
perror("write"); perror("write");
goto shutdown; goto shutdown;
} }
wr += i; wr += i;
} }
if (libssh2_channel_eof(channel)) { if(libssh2_channel_eof(channel)) {
fprintf(stderr, "The remote client at %s:%d disconnected!\n", fprintf(stderr, "The remote client at %s:%d disconnected!\n",
remote_listenhost, remote_listenport); remote_listenhost, remote_listenport);
goto shutdown; goto shutdown;
@ -304,9 +309,9 @@ shutdown:
#else #else
close(forwardsock); close(forwardsock);
#endif #endif
if (channel) if(channel)
libssh2_channel_free(channel); libssh2_channel_free(channel);
if (listener) if(listener)
libssh2_channel_forward_cancel(listener); libssh2_channel_forward_cancel(listener);
libssh2_session_disconnect(session, "Client disconnecting normally"); libssh2_session_disconnect(session, "Client disconnecting normally");
libssh2_session_free(session); libssh2_session_free(session);

Просмотреть файл

@ -48,14 +48,14 @@ static void remove_node(struct chan_X11_list *elem)
current_node = gp_x11_chan; current_node = gp_x11_chan;
if (gp_x11_chan == elem) { if(gp_x11_chan == elem) {
gp_x11_chan = gp_x11_chan->next; gp_x11_chan = gp_x11_chan->next;
free(current_node); free(current_node);
return; return;
} }
while (current_node->next != NULL) { while(current_node->next != NULL) {
if (current_node->next == elem) { if(current_node->next == elem) {
current_node->next = current_node->next->next; current_node->next = current_node->next->next;
current_node = current_node->next; current_node = current_node->next;
free(current_node); free(current_node);
@ -78,7 +78,7 @@ static int _raw_mode(void)
struct termios tio; struct termios tio;
rc = tcgetattr(fileno(stdin), &tio); rc = tcgetattr(fileno(stdin), &tio);
if (rc != -1) { if(rc != -1) {
_saved_tio = tio; _saved_tio = tio;
/* do the equivalent of cfmakeraw() manually, to build on Solaris */ /* do the equivalent of cfmakeraw() manually, to build on Solaris */
tio.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON); tio.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
@ -106,9 +106,9 @@ static int _normal_mode(void)
static void x11_callback(LIBSSH2_SESSION *session, LIBSSH2_CHANNEL *channel, static void x11_callback(LIBSSH2_SESSION *session, LIBSSH2_CHANNEL *channel,
char *shost, int sport, void **abstract) char *shost, int sport, void **abstract)
{ {
const char * display = NULL; const char *display = NULL;
char * ptr = NULL; char *ptr = NULL;
char * temp_buff = NULL; char *temp_buff = NULL;
int display_port = 0; int display_port = 0;
int sock = 0; int sock = 0;
int rc = 0; int rc = 0;
@ -124,22 +124,22 @@ static void x11_callback(LIBSSH2_SESSION *session, LIBSSH2_CHANNEL *channel,
* Inspired by x11_connect_display in openssh * Inspired by x11_connect_display in openssh
*/ */
display = getenv("DISPLAY"); display = getenv("DISPLAY");
if ( display != NULL) { if(display != NULL) {
if (strncmp( display, "unix:", 5) == 0 || if(strncmp(display, "unix:", 5) == 0 ||
display[0] == ':') { display[0] == ':') {
/* Connect to the local unix domain */ /* Connect to the local unix domain */
ptr = strrchr(display, ':'); ptr = strrchr(display, ':');
temp_buff = (char *) calloc(strlen(ptr+1), sizeof(char)); temp_buff = (char *) calloc(strlen(ptr + 1), sizeof(char));
if (!temp_buff) { if(!temp_buff) {
perror("calloc"); perror("calloc");
return; return;
} }
memcpy(temp_buff, ptr+1, strlen(ptr+1)); memcpy(temp_buff, ptr + 1, strlen(ptr + 1));
display_port = atoi(temp_buff); display_port = atoi(temp_buff);
free(temp_buff); free(temp_buff);
sock = socket(AF_UNIX, SOCK_STREAM, 0); sock = socket(AF_UNIX, SOCK_STREAM, 0);
if (sock < 0) if(sock < 0)
return; return;
memset(&addr, 0, sizeof(addr)); memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX; addr.sun_family = AF_UNIX;
@ -147,9 +147,9 @@ static void x11_callback(LIBSSH2_SESSION *session, LIBSSH2_CHANNEL *channel,
_PATH_UNIX_X, display_port); _PATH_UNIX_X, display_port);
rc = connect(sock, (struct sockaddr *) &addr, sizeof(addr)); rc = connect(sock, (struct sockaddr *) &addr, sizeof(addr));
if (rc != -1){ if(rc != -1) {
/* Connection Successfull */ /* Connection Successfull */
if (gp_x11_chan == NULL) { if(gp_x11_chan == NULL) {
/* Calloc ensure that gp_X11_chan is full of 0 */ /* Calloc ensure that gp_X11_chan is full of 0 */
gp_x11_chan = (struct chan_X11_list *) gp_x11_chan = (struct chan_X11_list *)
calloc(1, sizeof(struct chan_X11_list)); calloc(1, sizeof(struct chan_X11_list));
@ -159,7 +159,7 @@ static void x11_callback(LIBSSH2_SESSION *session, LIBSSH2_CHANNEL *channel,
} }
else { else {
chan_iter = gp_x11_chan; chan_iter = gp_x11_chan;
while (chan_iter->next != NULL) while(chan_iter->next != NULL)
chan_iter = chan_iter->next; chan_iter = chan_iter->next;
/* Create the new Node */ /* Create the new Node */
new = (struct chan_X11_list *) new = (struct chan_X11_list *)
@ -183,10 +183,10 @@ static void x11_callback(LIBSSH2_SESSION *session, LIBSSH2_CHANNEL *channel,
*/ */
static int x11_send_receive(LIBSSH2_CHANNEL *channel, int sock) static int x11_send_receive(LIBSSH2_CHANNEL *channel, int sock)
{ {
char * buf = NULL; char *buf = NULL;
int bufsize = 8192; int bufsize = 8192;
int rc = 0; int rc = 0;
int nfds = 1; int nfds = 1;
LIBSSH2_POLLFD *fds = NULL; LIBSSH2_POLLFD *fds = NULL;
fd_set set; fd_set set;
struct timeval timeval_out; struct timeval timeval_out;
@ -195,12 +195,14 @@ static int x11_send_receive(LIBSSH2_CHANNEL *channel, int sock)
FD_ZERO(&set); FD_ZERO(&set);
FD_SET(sock,&set); FD_SET(sock, &set);
if ((buf = calloc (bufsize, sizeof(char))) == NULL) buf = calloc(bufsize, sizeof(char));
if(!buf)
return 0; return 0;
if ((fds = malloc (sizeof (LIBSSH2_POLLFD))) == NULL) { fds = malloc(sizeof (LIBSSH2_POLLFD));
if(!fds) {
free(buf); free(buf);
return 0; return 0;
} }
@ -211,18 +213,18 @@ static int x11_send_receive(LIBSSH2_CHANNEL *channel, int sock)
fds[0].revents = LIBSSH2_POLLFD_POLLIN; fds[0].revents = LIBSSH2_POLLFD_POLLIN;
rc = libssh2_poll(fds, nfds, 0); rc = libssh2_poll(fds, nfds, 0);
if (rc >0) { if(rc >0) {
rc = libssh2_channel_read(channel, buf, bufsize); rc = libssh2_channel_read(channel, buf, bufsize);
write(sock, buf, rc); write(sock, buf, rc);
} }
rc = select(sock+1, &set, NULL, NULL, &timeval_out); rc = select(sock + 1, &set, NULL, NULL, &timeval_out);
if (rc > 0) { if(rc > 0) {
memset((void *)buf, 0, bufsize); memset((void *)buf, 0, bufsize);
/* Data in sock*/ /* Data in sock*/
rc = read(sock, buf, bufsize); rc = read(sock, buf, bufsize);
if (rc > 0) { if(rc > 0) {
libssh2_channel_write(channel, buf, rc); libssh2_channel_write(channel, buf, rc);
} }
else { else {
@ -233,7 +235,7 @@ static int x11_send_receive(LIBSSH2_CHANNEL *channel, int sock)
free(fds); free(fds);
free(buf); free(buf);
if (libssh2_channel_eof(channel) == 1) { if(libssh2_channel_eof(channel) == 1) {
return -1; return -1;
} }
return 0; return 0;
@ -273,10 +275,10 @@ main (int argc, char *argv[])
timeval_out.tv_usec = 10; timeval_out.tv_usec = 10;
if (argc > 3) { if(argc > 3) {
hostaddr = inet_addr(argv[1]); hostaddr = inet_addr(argv[1]);
username = argv[2]; username = argv[2];
password = argv[3]; password = argv[3];
} }
else { else {
fprintf(stderr, "Usage: %s destination username password", fprintf(stderr, "Usage: %s destination username password",
@ -284,42 +286,42 @@ main (int argc, char *argv[])
return -1; return -1;
} }
if (argc > 4) { if(argc > 4) {
set_debug_on = 1; set_debug_on = 1;
fprintf (stderr, "DEBUG is ON: %d\n", set_debug_on); fprintf(stderr, "DEBUG is ON: %d\n", set_debug_on);
} }
rc = libssh2_init (0); rc = libssh2_init(0);
if (rc != 0) { if(rc != 0) {
fprintf (stderr, "libssh2 initialization failed (%d)\n", rc); fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
return 1; return 1;
} }
sock = socket (AF_INET, SOCK_STREAM, 0); sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1) { if(sock == -1) {
perror("socket"); perror("socket");
return -1; return -1;
} }
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_port = htons (22); sin.sin_port = htons(22);
sin.sin_addr.s_addr = hostaddr; sin.sin_addr.s_addr = hostaddr;
rc = connect(sock, (struct sockaddr *) &sin, rc = connect(sock, (struct sockaddr *) &sin,
sizeof(struct sockaddr_in)); sizeof(struct sockaddr_in));
if (rc != 0) { if(rc != 0) {
fprintf (stderr, "Failed to established connection!\n"); fprintf(stderr, "Failed to established connection!\n");
return -1; return -1;
} }
/* Open a session */ /* Open a session */
session = libssh2_session_init(); session = libssh2_session_init();
rc = libssh2_session_handshake(session, sock); rc = libssh2_session_handshake(session, sock);
if (rc != 0) { if(rc != 0) {
fprintf(stderr, "Failed Start the SSH session\n"); fprintf(stderr, "Failed Start the SSH session\n");
return -1; return -1;
} }
if (set_debug_on == 1) if(set_debug_on == 1)
libssh2_trace(session, LIBSSH2_TRACE_CONN); libssh2_trace(session, LIBSSH2_TRACE_CONN);
/* ignore pedantic warnings by gcc on the callback argument */ /* ignore pedantic warnings by gcc on the callback argument */
@ -332,7 +334,7 @@ main (int argc, char *argv[])
/* Authenticate via password */ /* Authenticate via password */
rc = libssh2_userauth_password(session, username, password); rc = libssh2_userauth_password(session, username, password);
if (rc != 0) { if(rc != 0) {
fprintf(stderr, "Failed to authenticate\n"); fprintf(stderr, "Failed to authenticate\n");
session_shutdown(session); session_shutdown(session);
close(sock); close(sock);
@ -341,7 +343,7 @@ main (int argc, char *argv[])
/* Open a channel */ /* Open a channel */
channel = libssh2_channel_open_session(session); channel = libssh2_channel_open_session(session);
if ( channel == NULL ) { if(channel == NULL) {
fprintf(stderr, "Failed to open a new channel\n"); fprintf(stderr, "Failed to open a new channel\n");
session_shutdown(session); session_shutdown(session);
close(sock); close(sock);
@ -350,8 +352,8 @@ main (int argc, char *argv[])
/* Request a PTY */ /* Request a PTY */
rc = libssh2_channel_request_pty( channel, "xterm"); rc = libssh2_channel_request_pty(channel, "xterm");
if (rc != 0) { if(rc != 0) {
fprintf(stderr, "Failed to request a pty\n"); fprintf(stderr, "Failed to request a pty\n");
session_shutdown(session); session_shutdown(session);
close(sock); close(sock);
@ -359,8 +361,8 @@ main (int argc, char *argv[])
} }
/* Request X11 */ /* Request X11 */
rc = libssh2_channel_x11_req(channel,0); rc = libssh2_channel_x11_req(channel, 0);
if(rc!=0) { if(rc != 0) {
fprintf(stderr, "Failed to request X11 forwarding\n"); fprintf(stderr, "Failed to request X11 forwarding\n");
session_shutdown(session); session_shutdown(session);
close(sock); close(sock);
@ -369,7 +371,7 @@ main (int argc, char *argv[])
/* Request a shell */ /* Request a shell */
rc = libssh2_channel_shell(channel); rc = libssh2_channel_shell(channel);
if (rc!=0) { if(rc != 0) {
fprintf(stderr, "Failed to open a shell\n"); fprintf(stderr, "Failed to open a shell\n");
session_shutdown(session); session_shutdown(session);
close(sock); close(sock);
@ -377,7 +379,7 @@ main (int argc, char *argv[])
} }
rc = _raw_mode(); rc = _raw_mode();
if (rc != 0) { if(rc != 0) {
fprintf(stderr, "Failed to entered in raw mode\n"); fprintf(stderr, "Failed to entered in raw mode\n");
session_shutdown(session); session_shutdown(session);
close(sock); close(sock);
@ -387,15 +389,15 @@ main (int argc, char *argv[])
memset(&w_size, 0, sizeof(struct winsize)); memset(&w_size, 0, sizeof(struct winsize));
memset(&w_size_bck, 0, sizeof(struct winsize)); memset(&w_size_bck, 0, sizeof(struct winsize));
while (1) { while(1) {
FD_ZERO(&set); FD_ZERO(&set);
FD_SET(fileno(stdin),&set); FD_SET(fileno(stdin), &set);
/* Search if a resize pty has to be send */ /* Search if a resize pty has to be send */
ioctl(fileno(stdin), TIOCGWINSZ, &w_size); ioctl(fileno(stdin), TIOCGWINSZ, &w_size);
if ((w_size.ws_row != w_size_bck.ws_row) || if((w_size.ws_row != w_size_bck.ws_row) ||
(w_size.ws_col != w_size_bck.ws_col)) { (w_size.ws_col != w_size_bck.ws_col)) {
w_size_bck = w_size; w_size_bck = w_size;
libssh2_channel_request_pty_size(channel, libssh2_channel_request_pty_size(channel,
@ -403,10 +405,12 @@ main (int argc, char *argv[])
w_size.ws_row); w_size.ws_row);
} }
if ((buf = calloc (bufsiz, sizeof(char))) == NULL) buf = calloc(bufsiz, sizeof(char));
if(buf == NULL)
break; break;
if ((fds = malloc (sizeof (LIBSSH2_POLLFD))) == NULL) { fds = malloc(sizeof (LIBSSH2_POLLFD));
if(fds == NULL) {
free(buf); free(buf);
break; break;
} }
@ -417,25 +421,25 @@ main (int argc, char *argv[])
fds[0].revents = LIBSSH2_POLLFD_POLLIN; fds[0].revents = LIBSSH2_POLLFD_POLLIN;
rc = libssh2_poll(fds, nfds, 0); rc = libssh2_poll(fds, nfds, 0);
if (rc >0) { if(rc >0) {
libssh2_channel_read(channel, buf, sizeof(buf)); libssh2_channel_read(channel, buf, sizeof(buf));
fprintf(stdout, "%s", buf); fprintf(stdout, "%s", buf);
fflush(stdout); fflush(stdout);
} }
/* Looping on X clients */ /* Looping on X clients */
if (gp_x11_chan != NULL) { if(gp_x11_chan != NULL) {
current_node = gp_x11_chan; current_node = gp_x11_chan;
} }
else else
current_node = NULL; current_node = NULL;
while (current_node != NULL) { while(current_node != NULL) {
struct chan_X11_list *next_node; struct chan_X11_list *next_node;
rc = x11_send_receive(current_node->chan, current_node->sock); rc = x11_send_receive(current_node->chan, current_node->sock);
next_node = current_node->next; next_node = current_node->next;
if (rc == -1){ if(rc == -1) {
shutdown(current_node->sock,SHUT_RDWR); shutdown(current_node->sock, SHUT_RDWR);
close(current_node->sock); close(current_node->sock);
remove_node(current_node); remove_node(current_node);
} }
@ -444,25 +448,25 @@ main (int argc, char *argv[])
} }
rc = select(fileno(stdin)+1,&set,NULL,NULL,&timeval_out); rc = select(fileno(stdin) + 1, &set, NULL, NULL, &timeval_out);
if (rc > 0) { if(rc > 0) {
/* Data in stdin*/ /* Data in stdin*/
rc = read(fileno(stdin), buf,1); rc = read(fileno(stdin), buf, 1);
if (rc > 0) if(rc > 0)
libssh2_channel_write(channel,buf, sizeof(buf)); libssh2_channel_write(channel, buf, sizeof(buf));
} }
free (fds); free(fds);
free (buf); free(buf);
if (libssh2_channel_eof (channel) == 1) { if(libssh2_channel_eof (channel) == 1) {
break; break;
} }
} }
if (channel) { if(channel) {
libssh2_channel_free (channel); libssh2_channel_free(channel);
channel = NULL; channel = NULL;
} }
_normal_mode(); _normal_mode();

Просмотреть файл

@ -213,7 +213,8 @@ typedef off_t libssh2_struct_stat_size;
#ifndef LIBSSH2_STRUCT_STAT_SIZE_FORMAT #ifndef LIBSSH2_STRUCT_STAT_SIZE_FORMAT
# ifdef __VMS # ifdef __VMS
/* We have to roll our own format here because %z is a C99-ism we don't have. */ /* We have to roll our own format here because %z is a C99-ism we don't
have. */
# if __USE_OFF64_T || __USING_STD_STAT # if __USE_OFF64_T || __USING_STD_STAT
# define LIBSSH2_STRUCT_STAT_SIZE_FORMAT "%Ld" # define LIBSSH2_STRUCT_STAT_SIZE_FORMAT "%Ld"
# else # else
@ -229,11 +230,11 @@ typedef off_t libssh2_struct_stat_size;
/* Part of every banner, user specified or not */ /* Part of every banner, user specified or not */
#define LIBSSH2_SSH_BANNER "SSH-2.0-libssh2_" LIBSSH2_VERSION #define LIBSSH2_SSH_BANNER "SSH-2.0-libssh2_" LIBSSH2_VERSION
/* We *could* add a comment here if we so chose */ #define LIBSSH2_SSH_DEFAULT_BANNER LIBSSH2_SSH_BANNER
#define LIBSSH2_SSH_DEFAULT_BANNER LIBSSH2_SSH_BANNER #define LIBSSH2_SSH_DEFAULT_BANNER_WITH_CRLF LIBSSH2_SSH_DEFAULT_BANNER "\r\n"
#define LIBSSH2_SSH_DEFAULT_BANNER_WITH_CRLF LIBSSH2_SSH_DEFAULT_BANNER "\r\n"
/* Default generate and safe prime sizes for diffie-hellman-group-exchange-sha1 */ /* Default generate and safe prime sizes for
diffie-hellman-group-exchange-sha1 */
#define LIBSSH2_DH_GEX_MINGROUP 1024 #define LIBSSH2_DH_GEX_MINGROUP 1024
#define LIBSSH2_DH_GEX_OPTGROUP 1536 #define LIBSSH2_DH_GEX_OPTGROUP 1536
#define LIBSSH2_DH_GEX_MAXGROUP 2048 #define LIBSSH2_DH_GEX_MAXGROUP 2048
@ -269,14 +270,14 @@ typedef off_t libssh2_struct_stat_size;
typedef struct _LIBSSH2_USERAUTH_KBDINT_PROMPT typedef struct _LIBSSH2_USERAUTH_KBDINT_PROMPT
{ {
char* text; char *text;
unsigned int length; unsigned int length;
unsigned char echo; unsigned char echo;
} LIBSSH2_USERAUTH_KBDINT_PROMPT; } LIBSSH2_USERAUTH_KBDINT_PROMPT;
typedef struct _LIBSSH2_USERAUTH_KBDINT_RESPONSE typedef struct _LIBSSH2_USERAUTH_KBDINT_RESPONSE
{ {
char* text; char *text;
unsigned int length; unsigned int length;
} LIBSSH2_USERAUTH_KBDINT_RESPONSE; } LIBSSH2_USERAUTH_KBDINT_RESPONSE;
@ -287,10 +288,10 @@ typedef struct _LIBSSH2_USERAUTH_KBDINT_RESPONSE
/* 'keyboard-interactive' authentication callback */ /* 'keyboard-interactive' authentication callback */
#define LIBSSH2_USERAUTH_KBDINT_RESPONSE_FUNC(name_) \ #define LIBSSH2_USERAUTH_KBDINT_RESPONSE_FUNC(name_) \
void name_(const char* name, int name_len, const char* instruction, \ void name_(const char *name, int name_len, const char *instruction, \
int instruction_len, int num_prompts, \ int instruction_len, int num_prompts, \
const LIBSSH2_USERAUTH_KBDINT_PROMPT* prompts, \ const LIBSSH2_USERAUTH_KBDINT_PROMPT *prompts, \
LIBSSH2_USERAUTH_KBDINT_RESPONSE* responses, void **abstract) LIBSSH2_USERAUTH_KBDINT_RESPONSE *responses, void **abstract)
/* Callbacks for special SSH packets */ /* Callbacks for special SSH packets */
#define LIBSSH2_IGNORE_FUNC(name) \ #define LIBSSH2_IGNORE_FUNC(name) \
@ -324,12 +325,14 @@ typedef struct _LIBSSH2_USERAUTH_KBDINT_RESPONSE
LIBSSH2_CHANNEL *channel, void **channel_abstract) LIBSSH2_CHANNEL *channel, void **channel_abstract)
/* I/O callbacks */ /* I/O callbacks */
#define LIBSSH2_RECV_FUNC(name) ssize_t name(libssh2_socket_t socket, \ #define LIBSSH2_RECV_FUNC(name) \
void *buffer, size_t length, \ ssize_t name(libssh2_socket_t socket, \
int flags, void **abstract) void *buffer, size_t length, \
#define LIBSSH2_SEND_FUNC(name) ssize_t name(libssh2_socket_t socket, \ int flags, void **abstract)
const void *buffer, size_t length,\ #define LIBSSH2_SEND_FUNC(name) \
int flags, void **abstract) ssize_t name(libssh2_socket_t socket, \
const void *buffer, size_t length, \
int flags, void **abstract)
/* libssh2_session_callback_set() constants */ /* libssh2_session_callback_set() constants */
#define LIBSSH2_CALLBACK_IGNORE 0 #define LIBSSH2_CALLBACK_IGNORE 0
@ -468,7 +471,8 @@ typedef struct _LIBSSH2_POLLFD {
#define LIBSSH2_ERROR_FILE -16 #define LIBSSH2_ERROR_FILE -16
#define LIBSSH2_ERROR_METHOD_NONE -17 #define LIBSSH2_ERROR_METHOD_NONE -17
#define LIBSSH2_ERROR_AUTHENTICATION_FAILED -18 #define LIBSSH2_ERROR_AUTHENTICATION_FAILED -18
#define LIBSSH2_ERROR_PUBLICKEY_UNRECOGNIZED LIBSSH2_ERROR_AUTHENTICATION_FAILED #define LIBSSH2_ERROR_PUBLICKEY_UNRECOGNIZED \
LIBSSH2_ERROR_AUTHENTICATION_FAILED
#define LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED -19 #define LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED -19
#define LIBSSH2_ERROR_CHANNEL_OUTOFORDER -20 #define LIBSSH2_ERROR_CHANNEL_OUTOFORDER -20
#define LIBSSH2_ERROR_CHANNEL_FAILURE -21 #define LIBSSH2_ERROR_CHANNEL_FAILURE -21
@ -548,7 +552,7 @@ LIBSSH2_API void libssh2_free(LIBSSH2_SESSION *session, void *ptr);
*/ */
LIBSSH2_API int libssh2_session_supported_algs(LIBSSH2_SESSION* session, LIBSSH2_API int libssh2_session_supported_algs(LIBSSH2_SESSION* session,
int method_type, int method_type,
const char*** algs); const char ***algs);
/* Session API */ /* Session API */
LIBSSH2_API LIBSSH2_SESSION * LIBSSH2_API LIBSSH2_SESSION *
@ -596,7 +600,7 @@ LIBSSH2_API int libssh2_session_last_error(LIBSSH2_SESSION *session,
LIBSSH2_API int libssh2_session_last_errno(LIBSSH2_SESSION *session); LIBSSH2_API int libssh2_session_last_errno(LIBSSH2_SESSION *session);
LIBSSH2_API int libssh2_session_set_last_error(LIBSSH2_SESSION* session, LIBSSH2_API int libssh2_session_set_last_error(LIBSSH2_SESSION* session,
int errcode, int errcode,
const char* errmsg); const char *errmsg);
LIBSSH2_API int libssh2_session_block_directions(LIBSSH2_SESSION *session); LIBSSH2_API int libssh2_session_block_directions(LIBSSH2_SESSION *session);
LIBSSH2_API int libssh2_session_flag(LIBSSH2_SESSION *session, int flag, LIBSSH2_API int libssh2_session_flag(LIBSSH2_SESSION *session, int flag,
@ -609,12 +613,14 @@ LIBSSH2_API char *libssh2_userauth_list(LIBSSH2_SESSION *session,
unsigned int username_len); unsigned int username_len);
LIBSSH2_API int libssh2_userauth_authenticated(LIBSSH2_SESSION *session); LIBSSH2_API int libssh2_userauth_authenticated(LIBSSH2_SESSION *session);
LIBSSH2_API int libssh2_userauth_password_ex(LIBSSH2_SESSION *session, LIBSSH2_API int
const char *username, libssh2_userauth_password_ex(LIBSSH2_SESSION *session,
unsigned int username_len, const char *username,
const char *password, unsigned int username_len,
unsigned int password_len, const char *password,
LIBSSH2_PASSWD_CHANGEREQ_FUNC((*passwd_change_cb))); unsigned int password_len,
LIBSSH2_PASSWD_CHANGEREQ_FUNC
((*passwd_change_cb)));
#define libssh2_userauth_password(session, username, password) \ #define libssh2_userauth_password(session, username, password) \
libssh2_userauth_password_ex((session), (username), \ libssh2_userauth_password_ex((session), (username), \
@ -641,7 +647,8 @@ libssh2_userauth_publickey(LIBSSH2_SESSION *session,
const char *username, const char *username,
const unsigned char *pubkeydata, const unsigned char *pubkeydata,
size_t pubkeydata_len, size_t pubkeydata_len,
LIBSSH2_USERAUTH_PUBLICKEY_SIGN_FUNC((*sign_callback)), LIBSSH2_USERAUTH_PUBLICKEY_SIGN_FUNC
((*sign_callback)),
void **abstract); void **abstract);
LIBSSH2_API int LIBSSH2_API int
@ -733,7 +740,8 @@ libssh2_channel_direct_tcpip_ex(LIBSSH2_SESSION *session, const char *host,
LIBSSH2_API LIBSSH2_LISTENER * LIBSSH2_API LIBSSH2_LISTENER *
libssh2_channel_forward_listen_ex(LIBSSH2_SESSION *session, const char *host, libssh2_channel_forward_listen_ex(LIBSSH2_SESSION *session, const char *host,
int port, int *bound_port, int queue_maxsize); int port, int *bound_port,
int queue_maxsize);
#define libssh2_channel_forward_listen(session, port) \ #define libssh2_channel_forward_listen(session, port) \
libssh2_channel_forward_listen_ex((session), NULL, (port), NULL, 16) libssh2_channel_forward_listen_ex((session), NULL, (port), NULL, 16)
@ -764,15 +772,17 @@ LIBSSH2_API int libssh2_channel_request_pty_ex(LIBSSH2_CHANNEL *channel,
libssh2_channel_request_pty_ex((channel), (term), \ libssh2_channel_request_pty_ex((channel), (term), \
(unsigned int)strlen(term), \ (unsigned int)strlen(term), \
NULL, 0, \ NULL, 0, \
LIBSSH2_TERM_WIDTH, LIBSSH2_TERM_HEIGHT, \ LIBSSH2_TERM_WIDTH, \
LIBSSH2_TERM_WIDTH_PX, LIBSSH2_TERM_HEIGHT_PX) LIBSSH2_TERM_HEIGHT, \
LIBSSH2_TERM_WIDTH_PX, \
LIBSSH2_TERM_HEIGHT_PX)
LIBSSH2_API int libssh2_channel_request_pty_size_ex(LIBSSH2_CHANNEL *channel, LIBSSH2_API int libssh2_channel_request_pty_size_ex(LIBSSH2_CHANNEL *channel,
int width, int height, int width, int height,
int width_px, int width_px,
int height_px); int height_px);
#define libssh2_channel_request_pty_size(channel, width, height) \ #define libssh2_channel_request_pty_size(channel, width, height) \
libssh2_channel_request_pty_size_ex( (channel), (width), (height), 0, 0) libssh2_channel_request_pty_size_ex((channel), (width), (height), 0, 0)
LIBSSH2_API int libssh2_channel_x11_req_ex(LIBSSH2_CHANNEL *channel, LIBSSH2_API int libssh2_channel_x11_req_ex(LIBSSH2_CHANNEL *channel,
int single_connection, int single_connection,
@ -834,8 +844,9 @@ LIBSSH2_API ssize_t libssh2_channel_write_ex(LIBSSH2_CHANNEL *channel,
#define libssh2_channel_write(channel, buf, buflen) \ #define libssh2_channel_write(channel, buf, buflen) \
libssh2_channel_write_ex((channel), 0, (buf), (buflen)) libssh2_channel_write_ex((channel), 0, (buf), (buflen))
#define libssh2_channel_write_stderr(channel, buf, buflen) \ #define libssh2_channel_write_stderr(channel, buf, buflen) \
libssh2_channel_write_ex((channel), SSH_EXTENDED_DATA_STDERR, (buf), (buflen)) libssh2_channel_write_ex((channel), SSH_EXTENDED_DATA_STDERR, \
(buf), (buflen))
LIBSSH2_API unsigned long LIBSSH2_API unsigned long
libssh2_channel_window_write_ex(LIBSSH2_CHANNEL *channel, libssh2_channel_window_write_ex(LIBSSH2_CHANNEL *channel,
@ -872,7 +883,7 @@ LIBSSH2_API int libssh2_channel_handle_extended_data2(LIBSSH2_CHANNEL *channel,
libssh2_channel_handle_extended_data((channel), \ libssh2_channel_handle_extended_data((channel), \
(ignore) ? \ (ignore) ? \
LIBSSH2_CHANNEL_EXTENDED_DATA_IGNORE : \ LIBSSH2_CHANNEL_EXTENDED_DATA_IGNORE : \
LIBSSH2_CHANNEL_EXTENDED_DATA_NORMAL ) LIBSSH2_CHANNEL_EXTENDED_DATA_NORMAL)
#define LIBSSH2_CHANNEL_FLUSH_EXTENDED_DATA -1 #define LIBSSH2_CHANNEL_FLUSH_EXTENDED_DATA -1
#define LIBSSH2_CHANNEL_FLUSH_ALL -2 #define LIBSSH2_CHANNEL_FLUSH_ALL -2
@ -1268,9 +1279,9 @@ libssh2_agent_free(LIBSSH2_AGENT *agent);
* Note that non-blocking applications are responsible for sending the * Note that non-blocking applications are responsible for sending the
* keepalive messages using libssh2_keepalive_send(). * keepalive messages using libssh2_keepalive_send().
*/ */
LIBSSH2_API void libssh2_keepalive_config (LIBSSH2_SESSION *session, LIBSSH2_API void libssh2_keepalive_config(LIBSSH2_SESSION *session,
int want_reply, int want_reply,
unsigned interval); unsigned interval);
/* /*
* libssh2_keepalive_send() * libssh2_keepalive_send()
@ -1280,8 +1291,8 @@ LIBSSH2_API void libssh2_keepalive_config (LIBSSH2_SESSION *session,
* it again. Returns 0 on success, or LIBSSH2_ERROR_SOCKET_SEND on * it again. Returns 0 on success, or LIBSSH2_ERROR_SOCKET_SEND on
* I/O errors. * I/O errors.
*/ */
LIBSSH2_API int libssh2_keepalive_send (LIBSSH2_SESSION *session, LIBSSH2_API int libssh2_keepalive_send(LIBSSH2_SESSION *session,
int *seconds_to_next); int *seconds_to_next);
/* NOTE NOTE NOTE /* NOTE NOTE NOTE
libssh2_trace() has no function in builds that aren't built with debug libssh2_trace() has no function in builds that aren't built with debug
@ -1299,11 +1310,11 @@ LIBSSH2_API int libssh2_trace(LIBSSH2_SESSION *session, int bitmask);
#define LIBSSH2_TRACE_SOCKET (1<<9) #define LIBSSH2_TRACE_SOCKET (1<<9)
typedef void (*libssh2_trace_handler_func)(LIBSSH2_SESSION*, typedef void (*libssh2_trace_handler_func)(LIBSSH2_SESSION*,
void*, void *,
const char *, const char *,
size_t); size_t);
LIBSSH2_API int libssh2_trace_sethandler(LIBSSH2_SESSION *session, LIBSSH2_API int libssh2_trace_sethandler(LIBSSH2_SESSION *session,
void* context, void *context,
libssh2_trace_handler_func callback); libssh2_trace_handler_func callback);
#ifdef __cplusplus #ifdef __cplusplus

Просмотреть файл

@ -81,16 +81,18 @@ extern "C" {
#endif #endif
/* Publickey Subsystem */ /* Publickey Subsystem */
LIBSSH2_API LIBSSH2_PUBLICKEY *libssh2_publickey_init(LIBSSH2_SESSION *session); LIBSSH2_API LIBSSH2_PUBLICKEY *
libssh2_publickey_init(LIBSSH2_SESSION *session);
LIBSSH2_API int libssh2_publickey_add_ex(LIBSSH2_PUBLICKEY *pkey, LIBSSH2_API int
const unsigned char *name, libssh2_publickey_add_ex(LIBSSH2_PUBLICKEY *pkey,
unsigned long name_len, const unsigned char *name,
const unsigned char *blob, unsigned long name_len,
unsigned long blob_len, char overwrite, const unsigned char *blob,
unsigned long num_attrs, unsigned long blob_len, char overwrite,
const libssh2_publickey_attribute attrs[]); unsigned long num_attrs,
#define libssh2_publickey_add(pkey, name, blob, blob_len, overwrite, \ const libssh2_publickey_attribute attrs[]);
#define libssh2_publickey_add(pkey, name, blob, blob_len, overwrite, \
num_attrs, attrs) \ num_attrs, attrs) \
libssh2_publickey_add_ex((pkey), (name), strlen(name), (blob), (blob_len), \ libssh2_publickey_add_ex((pkey), (name), strlen(name), (blob), (blob_len), \
(overwrite), (num_attrs), (attrs)) (overwrite), (num_attrs), (attrs))
@ -107,8 +109,9 @@ LIBSSH2_API int
libssh2_publickey_list_fetch(LIBSSH2_PUBLICKEY *pkey, libssh2_publickey_list_fetch(LIBSSH2_PUBLICKEY *pkey,
unsigned long *num_keys, unsigned long *num_keys,
libssh2_publickey_list **pkey_list); libssh2_publickey_list **pkey_list);
LIBSSH2_API void libssh2_publickey_list_free(LIBSSH2_PUBLICKEY *pkey, LIBSSH2_API void
libssh2_publickey_list *pkey_list); libssh2_publickey_list_free(LIBSSH2_PUBLICKEY *pkey,
libssh2_publickey_list *pkey_list);
LIBSSH2_API int libssh2_publickey_shutdown(LIBSSH2_PUBLICKEY *pkey); LIBSSH2_API int libssh2_publickey_shutdown(LIBSSH2_PUBLICKEY *pkey);

Просмотреть файл

@ -224,12 +224,13 @@ LIBSSH2_API unsigned long libssh2_sftp_last_error(LIBSSH2_SFTP *sftp);
LIBSSH2_API LIBSSH2_CHANNEL *libssh2_sftp_get_channel(LIBSSH2_SFTP *sftp); LIBSSH2_API LIBSSH2_CHANNEL *libssh2_sftp_get_channel(LIBSSH2_SFTP *sftp);
/* File / Directory Ops */ /* File / Directory Ops */
LIBSSH2_API LIBSSH2_SFTP_HANDLE *libssh2_sftp_open_ex(LIBSSH2_SFTP *sftp, LIBSSH2_API LIBSSH2_SFTP_HANDLE *
const char *filename, libssh2_sftp_open_ex(LIBSSH2_SFTP *sftp,
unsigned int filename_len, const char *filename,
unsigned long flags, unsigned int filename_len,
long mode, int open_type); unsigned long flags,
#define libssh2_sftp_open(sftp, filename, flags, mode) \ long mode, int open_type);
#define libssh2_sftp_open(sftp, filename, flags, mode) \
libssh2_sftp_open_ex((sftp), (filename), strlen(filename), (flags), \ libssh2_sftp_open_ex((sftp), (filename), strlen(filename), (flags), \
(mode), LIBSSH2_SFTP_OPENFILE) (mode), LIBSSH2_SFTP_OPENFILE)
#define libssh2_sftp_opendir(sftp, path) \ #define libssh2_sftp_opendir(sftp, path) \
@ -331,7 +332,8 @@ LIBSSH2_API int libssh2_sftp_symlink_ex(LIBSSH2_SFTP *sftp,
const char *path, const char *path,
unsigned int path_len, unsigned int path_len,
char *target, char *target,
unsigned int target_len, int link_type); unsigned int target_len,
int link_type);
#define libssh2_sftp_symlink(sftp, orig, linkpath) \ #define libssh2_sftp_symlink(sftp, orig, linkpath) \
libssh2_sftp_symlink_ex((sftp), (orig), strlen(orig), (linkpath), \ libssh2_sftp_symlink_ex((sftp), (orig), strlen(orig), (linkpath), \
strlen(linkpath), LIBSSH2_SFTP_SYMLINK) strlen(linkpath), LIBSSH2_SFTP_SYMLINK)