示例#1
0
文件: channel.c 项目: wodny/pylibssh2
@return 0 on success or negative on failure\n\
@rtype int";

static PyObject *
PYLIBSSH2_Channel_close(PYLIBSSH2_CHANNEL *self, PyObject *args)
{
    int rc;

    Py_BEGIN_ALLOW_THREADS
    rc = libssh2_channel_close(self->channel);
    rc = libssh2_channel_wait_closed(self->channel);
    Py_END_ALLOW_THREADS

    if (rc) {
        /* CLEAN: PYLIBSSH2_CHANNEL_CANT_CLOSE_MSG */
        PyErr_SetString(PYLIBSSH2_Error, "Unable to close the channel.");
        return NULL; 
    }

    return Py_BuildValue("i", rc);
}
示例#2
0
int SSH2Channel::waitClosed(ExceptionSink *xsink, int timeout_ms) {
   AutoLocker al(parent->m);
   if (check_open(xsink))
      return -1;

   BlockingHelper bh(parent);

   int rc;
   while (true) {
      rc = libssh2_channel_wait_closed(channel);
      if (rc == LIBSSH2_ERROR_EAGAIN) {
	 if ((rc = parent->waitSocketUnlocked(xsink, SSH2CHANNEL_TIMEOUT, "SSH2CHANNEL-WAITCLOSED-ERROR", "SSH2Channel::waitClosed", timeout_ms)))
	    break;
	 continue;
      }
      if (rc < 0)
	 parent->doSessionErrUnlocked(xsink);
      break;
   }

   return rc;
}
示例#3
0
int main(int argc, char *argv[])
{
    unsigned long hostaddr;
    int sock, i, auth_pw = 1;
    struct sockaddr_in sin;
    const char *fingerprint;
    LIBSSH2_SESSION *session;
    LIBSSH2_CHANNEL *channel;
    const char *username="******";
    const char *password="******";
    const char *loclfile="scp_write.c";
    const char *scppath="/tmp/TEST";
    FILE *local;
    int rc;
#if defined(HAVE_IOCTLSOCKET)
    long flag = 1;
#endif
    char mem[1024];
    size_t nread;
    char *ptr;
    struct stat fileinfo;

#ifdef WIN32
    WSADATA wsadata;

    WSAStartup(MAKEWORD(2,0), &wsadata);
#endif

    if (argc > 1) {
        hostaddr = inet_addr(argv[1]);
    } else {
        hostaddr = htonl(0x7F000001);
    }
    if (argc > 2) {
        username = argv[2];
    }
    if (argc > 3) {
        password = argv[3];
    }
    if(argc > 4) {
        loclfile = argv[4];
    }
    if (argc > 5) {
        scppath = argv[5];
    }

    local = fopen(loclfile, "rb");
    if (!local) {
        fprintf(stderr, "Can't local file %s\n", loclfile);
        goto shutdown;
    }

    stat(loclfile, &fileinfo);

    /* 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 = hostaddr;
    if (connect(sock, (struct sockaddr*)(&sin),
                sizeof(struct sockaddr_in)) != 0) {
        fprintf(stderr, "failed to connect!\n");
        return -1;
    }

    /* We set the socket non-blocking. We do it after the connect just to
        simplify the example code. */
#ifdef F_SETFL
    /* FIXME: this can/should be done in a more portable manner */
    rc = fcntl(sock, F_GETFL, 0);
    fcntl(sock, F_SETFL, rc | O_NONBLOCK);
#elif defined(HAVE_IOCTLSOCKET)
    ioctlsocket(sock, FIONBIO, &flag);
#else
#ifdef WIN32
    u_long mode = 1;
    ioctlsocket (sock, FIONBIO, &mode);
#else
#error "add support for setting the socket non-blocking here"
#endif
#endif

    /* Create a session instance
     */
    session = libssh2_session_init();
    if(!session)
        return -1;

    /* Since we have set non-blocking, tell libssh2 we are non-blocking */
    libssh2_session_set_blocking(session, 0);

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

    /* 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);
    fprintf(stderr, "Fingerprint: ");
    for(i = 0; i < 16; i++) {
        fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
    }
    fprintf(stderr, "\n");

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

    /* Request a file via SCP */
    do {
        channel = libssh2_scp_send(session, scppath, 0x1FF & fileinfo.st_mode,
                                   (unsigned long)fileinfo.st_size);

        if ((!channel) && (libssh2_session_last_errno(session) !=
                           LIBSSH2_ERROR_EAGAIN)) {
            char *err_msg;

            libssh2_session_last_error(session, &err_msg, NULL, 0);
            fprintf(stderr, "%s\n", err_msg);
            goto shutdown;
        }
    } while (!channel);

    fprintf(stderr, "SCP session waiting to send file\n");
    do {
        nread = fread(mem, 1, sizeof(mem), local);
        if (nread <= 0) {
            /* end of file */
            break;
        }
        ptr = mem;

        do {
            /* write data in a loop until we block */
            while ((rc = libssh2_channel_write(channel, ptr, nread)) ==
                    LIBSSH2_ERROR_EAGAIN);
            if (rc < 0) {
                fprintf(stderr, "ERROR %d\n", rc);
            }
            ptr += rc;
            nread -= rc;
        } while (nread > 0);
    } while (1);

    fprintf(stderr, "Sending EOF\n");
    while (libssh2_channel_send_eof(channel) == LIBSSH2_ERROR_EAGAIN);

    fprintf(stderr, "Waiting for EOF\n");
    while (libssh2_channel_wait_eof(channel) == LIBSSH2_ERROR_EAGAIN);

    fprintf(stderr, "Waiting for channel to close\n");
    while (libssh2_channel_wait_closed(channel) == LIBSSH2_ERROR_EAGAIN);

    libssh2_channel_free(channel);
    channel = NULL;

shutdown:

    while ((rc = libssh2_session_disconnect(session,
                                            "Normal Shutdown, Thank you for playing")) == LIBSSH2_ERROR_EAGAIN);
    libssh2_session_free(session);

#ifdef WIN32
    Sleep(1000);
    closesocket(sock);
#else
    sleep(1);
    close(sock);
#endif
    fprintf(stderr, "all done\n");
    return 0;
}
示例#4
0
int main(int argc, char *argv[])
{
    unsigned long hostaddr;
    int sock, i, auth_pw = 1;
    struct sockaddr_in sin;
    const char *fingerprint;
    LIBSSH2_SESSION *session = NULL;
    LIBSSH2_CHANNEL *channel;
    const char *username="******";
    const char *password="******";
    const char *loclfile="scp_write.c";
    const char *scppath="/tmp/TEST";
    FILE *local;
    int rc;
    char mem[1024];
    size_t nread;
    char *ptr;
    struct stat fileinfo;

#ifdef WIN32
    WSADATA wsadata;

    WSAStartup(MAKEWORD(2,0), &wsadata);
#endif

    if (argc > 1) {
        hostaddr = inet_addr(argv[1]);
    } else {
        hostaddr = htonl(0x7F000001);
    }
    if (argc > 2) {
        username = argv[2];
    }
    if (argc > 3) {
        password = argv[3];
    }
    if(argc > 4) {
        loclfile = argv[4];
    }
    if (argc > 5) {
        scppath = argv[5];
    }

    rc = libssh2_init (0);
    if (rc != 0) {
        fprintf (stderr, "libssh2 initialization failed (%d)\n", rc);
        return 1;
    }

    local = fopen(loclfile, "rb");
    if (!local) {
        fprintf(stderr, "Can't open local file %s\n", loclfile);
        return -1;
    }

    stat(loclfile, &fileinfo);

    /* 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);
    if(-1 == sock) {
        fprintf(stderr, "failed to create socket!\n");
        return -1;
    }

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

    /* Create a session instance
     */
    session = libssh2_session_init();
    if(!session)
        return -1;

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

    /* 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_SHA1);
    fprintf(stderr, "Fingerprint: ");
    for(i = 0; i < 20; i++) {
        fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
    }
    fprintf(stderr, "\n");

    if (auth_pw) {
        /* We could authenticate via password */
        if (libssh2_userauth_password(session, username, password)) {
            fprintf(stderr, "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)) {
            fprintf(stderr, "\tAuthentication by public key failed\n");
            goto shutdown;
        }
    }

    /* Send a file via scp. The mode parameter must only have permissions! */
    channel = libssh2_scp_send(session, scppath, fileinfo.st_mode & 0777,
                               (unsigned long)fileinfo.st_size);

    if (!channel) {
        char *errmsg;
        int errlen;
        int err = libssh2_session_last_error(session, &errmsg, &errlen, 0);
        fprintf(stderr, "Unable to open a session: (%d) %s\n", err, errmsg);
        goto shutdown;
    }

    fprintf(stderr, "SCP session waiting to send file\n");
    do {
        nread = fread(mem, 1, sizeof(mem), local);
        if (nread <= 0) {
            /* end of file */
            break;
        }
        ptr = mem;

        do {
            /* write the same data over and over, until error or completion */
            rc = libssh2_channel_write(channel, ptr, nread);
            if (rc < 0) {
                fprintf(stderr, "ERROR %d\n", rc);
                break;
            }
            else {
                /* rc indicates how many bytes were written this time */
                ptr += rc;
                nread -= rc;
            }
        } while (nread);

    } while (1);

    fprintf(stderr, "Sending EOF\n");
    libssh2_channel_send_eof(channel);

    fprintf(stderr, "Waiting for EOF\n");
    libssh2_channel_wait_eof(channel);

    fprintf(stderr, "Waiting for channel to close\n");
    libssh2_channel_wait_closed(channel);

    libssh2_channel_free(channel);
    channel = NULL;

 shutdown:

    if(session) {
        libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing");
        libssh2_session_free(session);
    }
#ifdef WIN32
    closesocket(sock);
#else
    close(sock);
#endif
    if (local)
        fclose(local);
    fprintf(stderr, "all done\n");

    libssh2_exit();

    return 0;
}
示例#5
0
void Pull::run()
{
   QLOG_TRACE() << "Receiving file " << m_sourceFilePath;
   qDebug() << "SecureConnection::Pull " << m_sourceFilePath << "->" << m_destinationFilePath;

   // Set blocking, which apparently is required.
   QByteArray source(m_sourceFilePath.toLocal8Bit());
   libssh2_session_set_blocking(m_session, 1);
   struct stat fileInfo;
   LIBSSH2_CHANNEL* channel(libssh2_scp_recv(m_session, source.data(), &fileInfo));

   if (channel == 0) {
       QString msg("Could not stat file ");
       msg += m_sourceFilePath;
       throw Exception(m_session, msg);
   }

   QByteArray destination(m_destinationFilePath.toLocal8Bit());
   FILE* localFileHandle(fopen(destination.data(), "wb"));

   if (!localFileHandle) {
      QString msg("Could not open file for writing ");
      msg += m_destinationFilePath;
      throw Exception(msg);
   }

   // If the buffer size changes, anything connected to the copyProgress will
   // need updating as it assumes kbyte increments.
   char buffer[1024];
   off_t got(0);
   
   while (got < fileInfo.st_size && !m_terminate) {
       int amount(sizeof(buffer));
       if ((fileInfo.st_size - got) < amount) {
          amount = fileInfo.st_size - got;
       }

       int bc(libssh2_channel_read(channel, buffer, amount));

       if (bc > 0) {
          fwrite(buffer, 1, bc, localFileHandle);
       }else if (bc < 0) {
          m_success = false;
          m_errorMessage = "Error reading from channel";
          break;
       }
       got += bc;
       copyProgress();
//qDebug() << "sleeping 1"; sleep(1);
   }

   fclose(localFileHandle);

   libssh2_channel_send_eof(channel);
// This seems to cause a hang sometimes
//   libssh2_channel_wait_eof(channel);
   libssh2_channel_wait_closed(channel);
   libssh2_channel_free(channel);

   if (!m_errorMessage.isEmpty()) throw Exception(m_errorMessage);
   if (!m_terminate) m_success = true;
}
示例#6
0
void Push::run()
{
   QLOG_TRACE() << "Sending file " << m_sourceFilePath;
   // Check the local file is there first
qDebug() << "SecureConnection::Push " << m_sourceFilePath << "->" << m_destinationFilePath;
   QByteArray source(m_sourceFilePath.toLocal8Bit());
   FILE* localFileHandle(fopen(source.data(), "rb"));
            
   if (!localFileHandle) {
      QString msg("Could not stat file ");
      msg += m_sourceFilePath;
      throw Exception(msg);
   }  
         
   struct stat fileInfo;
   stat(source.data(), &fileInfo);
         
   // Set blocking, which apparently is required.
   libssh2_session_set_blocking(m_session, 1);

   QByteArray destination(m_destinationFilePath.toLocal8Bit());
   LIBSSH2_CHANNEL* channel(libssh2_scp_send(m_session, destination.data(),
      fileInfo.st_mode & 0777, (unsigned long)fileInfo.st_size));
         
   if (channel == 0) {
      QString msg("Unable to open channel for writing to file ");
      msg += m_destinationFilePath;
      throw Exception(msg);
   }
  
   size_t nread;
   char buffer[1024];
   char* ptr;
   int rc;
      
   do {
       nread = fread(buffer, 1, sizeof(buffer), localFileHandle);
       if (nread <= 0)  break; // end of file
       ptr = buffer;
       
       do {
          // write the same data over and over, until error or completion 
          // rc indicates how many bytes were written this time 
          rc = libssh2_channel_write(channel, ptr, nread);
          if (rc < 0) {
             m_errorMessage = "Error writing to channel " + QString::number(rc);
             break;
          }else {
             ptr += rc;
             nread -= rc;
          }
       } while (nread && !m_terminate);
       copyProgress();
   
   } while (!m_terminate);
   
   fclose(localFileHandle);
   
   libssh2_channel_send_eof(channel);
   libssh2_channel_wait_eof(channel);
   libssh2_channel_wait_closed(channel);
   libssh2_channel_free(channel);
   
   if (!m_errorMessage.isEmpty()) throw Exception(m_errorMessage);
   if (!m_terminate) m_success = true;
}
//Uses ssh to transfer a data file.
//I took most of this code from the example file scp_write.c in the libssh2 examples.
//!!!! This could really be generalized... !!!!
int SendFilesTo51(char* loclfile,char* serverlocation)
{
    int error_code = 0;

#if defined(HAVE_LIBSSH2)
	char filename[1024];	
	if(FindFilename(loclfile,filename))
	{
		printf("Error: Bad filename for hydrograph file. (%s)\n",loclfile);
		return 1;
	}

	char scppath[1024];
	sprintf(scppath,"%s/%s",serverlocation,filename);
	//sprintf(scppath,"/data/ifc_01_hydro/%s",filename);

	//Uh, yeah, probably not very secure...
	char *username = "******",*password = "******";

	//Check out the file info
	struct stat fileinfo;
	stat(loclfile,&fileinfo);
	FILE* local = fopen(loclfile, "rb");
	if(!local)
	{
		printf("Can't open local file %s\n", loclfile);
		return -1;
	}

	//Init ssh stuff
	long unsigned int hostaddr = inet_addr("128.255.26.166");
	if(hostaddr < 0)
	{
		printf("Bad host address.\n");
		return 1;
	}
	int rc = libssh2_init(0);
	if(rc)
	{
		printf("Problem initializing libssh2 (%i)\n",rc);
		return 1;
	}
	int sock = socket(AF_INET,SOCK_STREAM,0);
	if(-1 == sock)
	{
		printf("Failed to create socket\n");
		return 1;
	}

	struct sockaddr_in sin;
	sin.sin_family = AF_INET;
	sin.sin_port = htons(22);
	sin.sin_addr.s_addr = hostaddr;
	if(connect(sock,(struct sockaddr*)(&sin),sizeof(struct sockaddr_in)) != 0)
	{
		printf("Failed to connect!\n");
		return 1;
	}

	//Create session instance and start it
	LIBSSH2_SESSION *session;
	session = libssh2_session_init();
	if(!session)	return 1;
	rc = libssh2_session_handshake(session, sock);
	if(rc)
	{
		printf("Failure establishing SSH session %i\n",rc);
		return 1;
	}

	//Authenticate
	const char* fingerprint = libssh2_hostkey_hash(session,LIBSSH2_HOSTKEY_HASH_SHA1);
	if(libssh2_userauth_password(session, username, password))
	{
		printf("Authentication by password failed.\n");
		error_code = 1;
		goto shutdown;
        }

	//Copy file
	LIBSSH2_CHANNEL *channel = libssh2_scp_send(session,scppath,fileinfo.st_mode & 0777,(unsigned long)fileinfo.st_size);
	if(!channel)
	{
		char *errmsg;
		int errlen;
		int err = libssh2_session_last_error(session, &errmsg, &errlen, 0);

		printf("Unable to open a session: (%d) %s\n", err, errmsg);
		error_code = 1;
		goto shutdown;
	}

	char mem[1024],*ptr;
	size_t nread;
	do
	{
		nread = fread(mem, 1, sizeof(mem), local);
		if (nread <= 0)
		{
			// end of file
			break;
		}
		ptr = mem;

		do
		{
			// write the same data over and over, until error or completion 
			rc = libssh2_channel_write(channel, ptr, nread);

			if (rc < 0)
			{
				printf("ERROR %d\n", rc);
				break;
			}
			else
			{
				// rc indicates how many bytes were written this time
				ptr += rc;
				nread -= rc;
			}
		} while(nread);
	} while(1);

	//Tell server we are done
	libssh2_channel_send_eof(channel);
	libssh2_channel_wait_eof(channel);
	libssh2_channel_wait_closed(channel);

	//Clean up
	libssh2_channel_free(channel);
	channel = NULL;

	shutdown:
	if(session)
	{
	 	libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing");
		libssh2_session_free(session);
	}
	close(sock);
	if(local)	fclose(local);
	if(!error_code && remove(loclfile))
		printf("[%i]: Error deleting file %s.\n",my_rank,loclfile);
	libssh2_exit();

	if(!error_code)	printf("File %s uploaded!\n",loclfile);
#endif 
	return error_code;
}