Hi,

I'm new to this list so please bear with me. :)

I'm trying to use the most current libssh2 version from CVS. I have an
download from a month ago and then also a current (checked out a few hours
ago). I also have this tester program (attachment, scp.c), which fails to
work at all with the current version of the libssh2 distribution. I've
traced it down to libssh2_session_startup(session, sock);. This function
blocks the program for a minute or so (I didn't do any measurements, sorry),
and fails with the error message:

Failure establishing SSH session: -5

Now, I tried this too with libssh2 0.14, and it wen't as far as
libssh2_scp_send(), and actually started to send the file when I applied
this modest patch to libssh2/src/scp.c:

--- orig.libssh2-0.14/src/scp.c 2006-03-02 03:07:20.000000000 +0200
+++ libssh2-0.14/src/scp.c      2007-03-08 00:32:23.000000000 +0200
@@ -378,12 +378,14 @@
        }
       LIBSSH2_FREE(session, command);

+#if 0
        /* Wait for ACK */
        if ((libssh2_channel_read(channel, response, 1) <= 0) || +(response[0] 
!= 0)) {
                libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL, +"Invalid 
ACK response from remote", 0);
                libssh2_channel_free(channel);
                return NULL;
        }
+#endif

        /* Send mtime and atime to be used for file */
        if (mtime || atime) {

Now my scp.c (the attachment) works just fine with this patch applied to
0.14 and the program linked against it.

Now the thing is, that in CVS versions of the library it fails to receive
input from the server for some unknown reason. This is what I get in strace:

open("/dev/urandom", O_RDONLY|O_NONBLOCK|O_NOCTTY) = 4
fstat(4, {st_mode=S_IFCHR|0666, st_rdev=makedev(1, 9), ...}) = 0
select(5, [4], NULL, NULL, {0, 10000})  = 1 (in [4], left {0, 10000})
read(4, "%e\252rHK\v\325\205\225\246\352\22,AN\344\247\23\214\257"..., 32) =
32
close(4)                                = 0
getuid()                                = 1000
sendto(3, "\0\0\0024\5\24=\257\357\245\302\3302\320\3538k\260\211"..., 568,
MSG_NOSIGNAL, NULL, 0) = 568
recvfrom(3, "\0\0\2\344\n\24J\314\371\222\31K\233\250\33\234\27\336"...,
4096, MSG_NOSIGNAL, NULL, NULL) = 744
recvfrom(3, 

... and then there is no response to it whatsoever.

Am I doing something terribly wrong here? I'm very sorry if this is a FAQ,
if it is, just reply with a polite "RTFM" to me and I'll keep on digging
with google. :)

kind wishes,
Bo Granlund

P.S. I don't know if I'm on the mailinglist or not, sourceforge list servers
are acting a bit weird, so I would really appreciate it if you could include
[EMAIL PROTECTED] in the response you write. Thank you.
/*
 * $Id: scp.c,v 1.4 2007/02/02 23:23:36 bagder Exp $
 *
 * Sample showing how to do a simple SCP transfer.
 */

#include <libssh2.h>

#ifndef WIN32
# include <netinet/in.h>
# include <sys/socket.h>
# include <unistd.h>
#else
# include <winsock2.h>
#endif

#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <ctype.h>

int main(int argc, char *argv[])
{
	int sock, i, auth_pw = 1;
	struct sockaddr_in sin;
	const char *fingerprint;
	LIBSSH2_SESSION *session;
	LIBSSH2_CHANNEL *channel;
	char *username=(char *)"testuser";
	char *password=(char *)"kampela";
	char *scppath=(char *)"/tmp/TEST";
	struct stat fileinfo;
	int rc;
	off_t got=0;
	int foo;

#ifdef WIN32
	WSADATA wsadata;

	WSAStartup(WINSOCK_VERSION, &wsadata);
#endif

	/* Ultra basic "connect to port 22 on localhost"
	 * Your code is responsible for creating the socket establishing the
	 * connection
	 */
	sock = socket(AF_INET, SOCK_STREAM, 0);

	sin.sin_family = AF_INET;
	sin.sin_port = htons(22);
	//sin.sin_addr.s_addr = htonl(0x7F000001);
	sin.sin_addr.s_addr = htonl(0x5391f443);
	if (connect(sock, (struct sockaddr*)(&sin),
		    sizeof(struct sockaddr_in)) != 0) {
		fprintf(stderr, "failed to connect!\n");
		return -1;
	}

	printf("In the beginning\n");
	/* Create a session instance
	 */
	session = libssh2_session_init();
	if(!session)
		return -1;

#if 0
	/* trace transport layer stuff*/
	libssh2_trace(session, LIBSSH2_TRACE_TRANS);
#endif

	printf("next\n");

	/* ... start it up. This will trade welcome banners, exchange keys,
	 * and setup crypto, compression, and MAC layers
	 */
	rc = libssh2_session_startup(session, sock);
	if(rc) {
		fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
		return -1;
	}
	printf("After libssh2_session_startup()\n");

	/* At this point we havn't yet authenticated.  The first thing to do
	 * is check the hostkey's fingerprint against our known hosts Your app
	 * may have it hard coded, may go to a file, may present it to the
	 * user, that's your call
	 */
	fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_MD5);
	printf("Fingerprint: ");
	for(i = 0; i < 16; i++) {
		printf("%02X ", (unsigned char)fingerprint[i]);
	}
	printf("\n");

	if(argc > 1) {
		username = argv[1];
	}
	if(argc > 2) {
		password = argv[2];
	}
	if(argc > 3) {
		scppath = argv[3];
	}

	if (auth_pw) {
		/* We could authenticate via password */
		if (libssh2_userauth_password(session, username, password)) {
			printf("Authentication by password failed.\n");
			goto shutdown;
		}
	} else {
		/* Or by public key */
		if (libssh2_userauth_publickey_fromfile(session, username,
							"/home/username/.ssh/id_rsa.pub",
							"/home/username/.ssh/id_rsa",
							password)) {
			printf("\tAuthentication by public key failed\n");
			goto shutdown;
		}
	}

	/* Request a file via SCP */
	//channel = libssh2_scp_recv(session, scppath, &fileinfo);
	if (stat("/tmp/source", &fileinfo) == -1) {
		fprintf(stderr, "Unable to stat /tmp/source\n");
		goto shutdown;
	}
	channel = libssh2_scp_send(session, "/tmp/source", 0640,
			fileinfo.st_size);

	if (!channel) {
		fprintf(stderr, "Unable to open a channel\n");
		goto shutdown;
	}
	foo = open("/tmp/source", O_RDONLY);
	if (foo == -1) {
		fprintf(stderr, "Unable to open /tmp/source\n");
		goto shutdown;
	}
	printf("Copying\n");
	while (got < fileinfo.st_size) {
		/*printf("Iteration\n");*/
		char buf[2];
		if (read(foo, buf, 1) == -1) {
			fprintf(stderr, "Couldn't read from file\n");
			goto shutdown;
		}
		rc = libssh2_channel_write(channel, buf, 1);
		got++;
	}

	libssh2_channel_free(channel);
	channel = NULL;

 shutdown:

	libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing");
	libssh2_session_free(session);

#ifdef WIN32
	Sleep(1000);
	closesocket(sock);
#else
	sleep(1);
	close(sock);
#endif
printf("all done\n");
	return 0;
}

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
libssh2-devel mailing list
libssh2-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/libssh2-devel

Reply via email to