Beispiel #1
2
int SSH2Utils::exec(const char *cmd) {
	m_execResultStr.clear();
	int rc = -1;
	LIBSSH2_CHANNEL *channel;
	/* Exec non-blocking on the remove host */
	while ((channel = libssh2_channel_open_session(m_session)) == NULL
			&& libssh2_session_last_error(m_session, NULL, NULL, 0)
					== LIBSSH2_ERROR_EAGAIN) {
		waitsocket();
	}
	if (channel == NULL) {
		fprintf(stderr, "Channel Error\n");
		m_errCode = 9;
		return -1;
	}

    /* Request a terminal with 'vanilla' terminal emulation
     * See /etc/termcap for more options
     */
    if (libssh2_channel_request_pty(channel, "vanilla")) {
        fprintf(stderr, "Failed requesting pty\n");
    }

    /* Open a SHELL on that pty */
    if (libssh2_channel_shell(channel)) {
        fprintf(stderr, "Unable to request shell on allocated pty\n");
    }

    libssh2_channel_write(channel, cmd, strlen(cmd));
    libssh2_channel_send_eof(channel);
	char buffer[BUF_SIZE];
	memset(buffer, 0, BUF_SIZE);
	rc = libssh2_channel_read(channel, buffer, BUF_SIZE);
	if (rc > 0) {
		m_execResultStr.append(buffer);
	}

	m_channelExitCode = 127;
	while ((rc = libssh2_channel_close(channel)) == LIBSSH2_ERROR_EAGAIN)
		waitsocket();
	if (rc == 0) {
		m_channelExitCode = libssh2_channel_get_exit_status(channel);
	}

skip_shell:
	if (channel) {
		libssh2_channel_free(channel);
		channel = NULL;
	}
	return rc;
}
Beispiel #2
0
static void
virNetSSHSessionDispose(void *obj)
{
    virNetSSHSessionPtr sess = obj;
    VIR_DEBUG("sess=0x%p", sess);

    if (!sess)
        return;

    if (sess->channel) {
        libssh2_channel_send_eof(sess->channel);
        libssh2_channel_close(sess->channel);
        libssh2_channel_free(sess->channel);
    }

    libssh2_knownhost_free(sess->knownHosts);
    libssh2_agent_free(sess->agent);

    if (sess->session) {
        libssh2_session_disconnect(sess->session,
                                   "libvirt: virNetSSHSessionFree()");
        libssh2_session_free(sess->session);
    }

    virNetSSHSessionAuthMethodsFree(sess);

    VIR_FREE(sess->channelCommand);
    VIR_FREE(sess->hostname);
    VIR_FREE(sess->knownHostsFile);
    VIR_FREE(sess->authPath);
}
int ssh_guac_client_free_handler(guac_client* client) {

    ssh_guac_client_data* guac_client_data = (ssh_guac_client_data*) client->data;

    /* Close SSH channel */
    if (guac_client_data->term_channel != NULL) {
        libssh2_channel_send_eof(guac_client_data->term_channel);
        libssh2_channel_close(guac_client_data->term_channel);
    }

    /* Free terminal */
    guac_terminal_free(guac_client_data->term);
    pthread_join(guac_client_data->client_thread, NULL);

    /* Free channels */
    libssh2_channel_free(guac_client_data->term_channel);

    /* Clean up SFTP */
    if (guac_client_data->sftp_session)
        libssh2_sftp_shutdown(guac_client_data->sftp_session);

    if (guac_client_data->sftp_ssh_session) {
        libssh2_session_disconnect(guac_client_data->sftp_ssh_session, "Bye");
        libssh2_session_free(guac_client_data->sftp_ssh_session);
    }

    /* Free session */
    if (guac_client_data->session != NULL)
        libssh2_session_free(guac_client_data->session);

    /* Free auth key */
    if (guac_client_data->key != NULL)
        ssh_key_free(guac_client_data->key);

    /* Free clipboard */
    guac_common_clipboard_free(guac_client_data->clipboard);

    /* Free cursors */
    guac_ssh_cursor_free(client, guac_client_data->ibar_cursor);
    guac_ssh_cursor_free(client, guac_client_data->blank_cursor);

    /* Free generic data struct */
    free(client->data);

    return 0;
}
Beispiel #4
0
int guac_ssh_client_free_handler(guac_client* client) {

    guac_ssh_client* ssh_client = (guac_ssh_client*) client->data;

    /* Close SSH channel */
    if (ssh_client->term_channel != NULL) {
        libssh2_channel_send_eof(ssh_client->term_channel);
        libssh2_channel_close(ssh_client->term_channel);
    }

    /* Free terminal (which may still be using term_channel) */
    if (ssh_client->term != NULL) {
        guac_terminal_free(ssh_client->term);
        pthread_join(ssh_client->client_thread, NULL);
    }

    /* Free terminal channel now that the terminal is finished */
    if (ssh_client->term_channel != NULL)
        libssh2_channel_free(ssh_client->term_channel);

    /* Clean up the SFTP filesystem object and session */
    if (ssh_client->sftp_filesystem) {
        guac_common_ssh_destroy_sftp_filesystem(ssh_client->sftp_filesystem);
        guac_common_ssh_destroy_session(ssh_client->sftp_session);
    }

    /* Free interactive SSH session */
    if (ssh_client->session != NULL)
        guac_common_ssh_destroy_session(ssh_client->session);

    /* Free SSH client credentials */
    if (ssh_client->user != NULL)
        guac_common_ssh_destroy_user(ssh_client->user);

    /* Free parsed settings */
    if (ssh_client->settings != NULL)
        guac_ssh_settings_free(ssh_client->settings);

    /* Free client structure */
    free(ssh_client);

    guac_common_ssh_uninit();
    return 0;
}
int SSH2Channel::sendEof(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_send_eof(channel);
      if (rc == LIBSSH2_ERROR_EAGAIN) {
	 if ((rc = parent->waitSocketUnlocked(xsink, SSH2CHANNEL_TIMEOUT, "SSH2CHANNEL-SENDEOF-ERROR", "SSH2Channel::sendEof", timeout_ms)))
	    break;
	 continue;
      }
      if (rc)
	 parent->doSessionErrUnlocked(xsink);
      break;
   }

   return rc;
}
Beispiel #6
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;
}
Beispiel #7
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;
}
Beispiel #8
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;
}
Beispiel #9
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;
}
Beispiel #10
0
void SshScpSend::sshDataReceived()
{
    static char mem[1024];
    static size_t nread;
    static char *ptr;
    int rc;

    switch(_currentState)
    {
    case ScpPrepare:
        if(!sshChannel) {
            sshChannel = libssh2_scp_send(sshClient->session(), qPrintable(_destination), _fileinfo.st_mode & 0777, (unsigned long)_fileinfo.st_size);

            if ((!sshChannel) && (libssh2_session_last_errno(sshClient->session()) != LIBSSH2_ERROR_EAGAIN)) {
                char *errmsg;
                int errlen;
                libssh2_session_last_error(sshClient->session(), &errmsg, &errlen, 0);
                qDebug() << "ERROR : Unable to open a session: " << errmsg;
            }
            if(sshChannel) {
#ifdef DEBUG_SCPSEND
                qDebug() << "DEBUG : ScpPrepare Send Accepted";
#endif
                _currentState = ScpCopy;
            }
        }
        break;

    case ScpCopy:
        do {
            nread = fread(mem, 1, sizeof(mem), _local);
            if (nread <= 0) {
                /* end of file */
                _currentState = ScpEof;
                sshDataReceived();
                break;
            }
            ptr = mem;
            do {
                /* write the same data over and over, until error or completion */
                rc = libssh2_channel_write(sshChannel, ptr, nread);

                if (rc < 0) {
                    qDebug() << "ERROR : Copy error " << rc;
                    _currentState = ScpError;
                    break;
                }
                else {
                    /* rc indicates how many bytes were written this time */
                    ptr += rc;
                    nread -= rc;
                }
            } while (nread);
        } while(1);
        break;
    case ScpEof:
#ifdef DEBUG_SCPSEND
        qDebug() << "DEBUG : Sending EOF";
#endif
        libssh2_channel_send_eof(sshChannel);
        _currentState = ScpClose;
        break;

    case ScpClose:
#ifdef DEBUG_SCPSEND
        qDebug() << "DEBUG : Waiting EOF";
#endif
        libssh2_channel_wait_eof(sshChannel);
        _state = true;
        _currentState = ScpEnd;
        stopChannel();
        emit transfertTerminate();
        break;

    case ScpError:
        qDebug() << "ERROR : SCP ERROR";
        _state = false;
        stopChannel();
        emit transfertTerminate();
        break;

    case ScpEnd:
#ifdef DEBUG_SCPSEND
        qDebug() << "DEBUG : Wait end";
#endif
        break;
    }
}
//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;
}
Beispiel #12
0
int ssh2_forward_port(const char *hostip,
                      const char *username,
                      const char *password,
                      int listenport,
                      const char *desthost,
                      int destport,
                      int *initialized,
                      int *finished,
                      char **errmsg)
{
    int rc, sock = -1, listensock = -1, forwardsock = -1, i;
    struct sockaddr_in sin;
    socklen_t sinlen;
    LIBSSH2_SESSION *session;
    LIBSSH2_CHANNEL *channel = NULL;
    const char *shost;
    unsigned int sport;
    fd_set fds;
    struct timeval tv;
    ssize_t len, wr;
    char buf[16384];
    struct StringBuffer *sberrmsg = new_string_buffer();

#ifdef WIN32
    char sockopt;
    WSADATA wsadata;

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

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

    /* Connect to SSH server */
    sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
    memset(&sin, 0, sizeof(sin));
    sin.sin_family = AF_INET;
    if (INADDR_NONE == (sin.sin_addr.s_addr = inet_addr(hostip))) {
        perror("inet_addr");
        return -1;
    }
    sin.sin_port = htons(22);
    if (connect(sock, (struct sockaddr*)(&sin),
                sizeof(struct sockaddr_in)) != 0) {
        sbprintf(sberrmsg, "failed to connect!\n");
        return -1;
    }

    /* Create a session instance */
    session = libssh2_session_init();
    if(!session) {
        sbprintf(sberrmsg, "Could not initialize SSH session!\n");
        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) {
        sbprintf(sberrmsg, "Error when starting up SSH session: %d\n", rc);
        return -1;
    }

    if (libssh2_userauth_password(session, username, password)) {
        sbprintf(sberrmsg, "Authentication by password failed.\n");
        goto shutdown;
    }

    listensock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
    sin.sin_family = AF_INET;
    sin.sin_port = htons(listenport);
    if (INADDR_NONE == (sin.sin_addr.s_addr = inet_addr("127.0.0.1"))) {
        sbprintf(sberrmsg, "inet_addr: %s.", strerror(errno));
        goto shutdown;
    }
    sockopt = 1;
    setsockopt(listensock, SOL_SOCKET, SO_REUSEADDR, &sockopt, sizeof(sockopt));
    sinlen=sizeof(sin);
    if (-1 == bind(listensock, (struct sockaddr *)&sin, sinlen)) {
        sbprintf(sberrmsg, "bind: %s.", strerror(errno));
        goto shutdown;
    }
    if (-1 == listen(listensock, 2)) {
        sbprintf(sberrmsg, "listen: %s.", strerror(errno));
        goto shutdown;
    }

    printf("Waiting for TCP connection on %s:%d...\n",
        inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
    *initialized = 1;

    forwardsock = accept(listensock, (struct sockaddr *)&sin, &sinlen);
    if (-1 == forwardsock) {
        sbprintf(sberrmsg, "accept: %s.", strerror(errno));
        goto shutdown;
    }

    shost = inet_ntoa(sin.sin_addr);
    sport = ntohs(sin.sin_port);

    printf("Forwarding connection from %s:%d here to remote %s:%d\n", shost,
        sport, desthost, destport);

    channel = libssh2_channel_direct_tcpip_ex(session, desthost,
        destport, shost, sport);
    if (!channel) {
        sbprintf(sberrmsg, "Could not open the direct-tcpip channel!\n"
                "(Note that this can be a problem at the server!"
                " Please review the server logs.)\n");
        goto shutdown;
    }

    /* Must use non-blocking IO hereafter due to the current libssh2 API */
    libssh2_session_set_blocking(session, 0);

    while (1) {
        if(*finished) libssh2_channel_send_eof(channel);
        FD_ZERO(&fds);
        FD_SET(forwardsock, &fds);
        tv.tv_sec = 0;
        tv.tv_usec = 100000;
        rc = select(forwardsock + 1, &fds, NULL, NULL, &tv);
        if (-1 == rc) {
            sbprintf(sberrmsg, "select: %s.", strerror(errno));
            goto shutdown;
        }
        if (rc && FD_ISSET(forwardsock, &fds)) {
            len = recv(forwardsock, buf, sizeof(buf), 0);
            if (len < 0) {
                perror("read");
                goto shutdown;
            } else if (0 == len) {
                printf("The client at %s:%d disconnected!\n", shost, sport);
                goto shutdown;
            }
            wr = 0;
            do {
                i = libssh2_channel_write(channel, buf, len);
                if (i < 0) {
                    sbprintf(sberrmsg, "libssh2_channel_write: %d\n", i);
                    goto shutdown;
                }
                wr += i;
            } while(i > 0 && wr < len);
        }
        while (1) {
            len = libssh2_channel_read(channel, buf, sizeof(buf));
            if (LIBSSH2_ERROR_EAGAIN == len)
                break;
            else if (len < 0) {
                sbprintf(sberrmsg, "libssh2_channel_read: %d", (int)len);
                goto shutdown;
            }
            wr = 0;
            while (wr < len) {
                i = send(forwardsock, buf + wr, len - wr, 0);
                if (i <= 0) {
                    perror("write");
                    goto shutdown;
                }
                wr += i;
            }
            if (libssh2_channel_eof(channel)) {
                printf("The server at %s:%d disconnected!\n",
                    desthost, destport);
                goto shutdown;
            }
        }
    }

shutdown:
 fprintf(stderr, "finished = %d\n", *finished);
#ifdef WIN32
    closesocket(forwardsock);
    closesocket(listensock);
#else
    close(forwardsock);
    close(listensock);
#endif
    if (channel)
        libssh2_channel_free(channel);
    libssh2_session_disconnect(session, "Client disconnecting normally");
    libssh2_session_free(session);

#ifdef WIN32
    closesocket(sock);
#else
    close(sock);
#endif

    libssh2_exit();

    if(string_buffer_is_empty(sberrmsg)) {
       free_string_buffer(sberrmsg, 1);
       return 0;
    }

    *errmsg = free_string_buffer(sberrmsg, 0);
    return 1;
}