QString SshScpSend::send() { QFileInfo src(_source); const char *loclfile=qPrintable(src.absoluteFilePath()); _currentState = ScpPrepare; _local = fopen(loclfile, "rb"); if (!_local) { qDebug() << "ERROR : Can't open local file " << loclfile; return ""; } stat(loclfile, &_fileinfo); /* Send a file via scp. The mode parameter must only have permissions! */ #ifdef DEBUG_SCPSEND qDebug() << "DEBUG : Send to " << qPrintable(_destination + "/" + src.fileName()); #endif sshChannel = libssh2_scp_send(sshClient->session(), qPrintable(_destination + src.fileName()), _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; _currentState = ScpError; } #ifdef DEBUG_SCPSEND qDebug() << "DEBUG : End of send function"; #endif return _destination + src.fileName(); }
/** * @function ssh.writeFile * * ### Synopsis * * var status = ssh.writeFile(handle, srcPath, dstPath); * var status = ssh.writeFile(handle, srcPath, dstPath, mode); * * Write file to remote server via SCP. * * @param {object} handle - opaque handle to already open SSH2 connection. * @param {string} srcPath - path to file in local file system to send. * @param {string} dstPath - path to file in remote file system to create. * @param {int} mode - desired resulting file permissions of file on remote end. * @return {boolean} success - true if the transfer succeeded, string error message if transfer failed. * * ### Note * If mode is not provided, the file mode of the file being sent will be used. */ static JSVAL ssh2_scp_send(JSARGS args) { HandleScope scope; SSH2 *ssh2 = HANDLE(args[0]); String::Utf8Value srcPath(args[1]); String::Utf8Value dstPath(args[2]); int mode; struct stat fileinfo; if (stat(*srcPath, &fileinfo) != 0) { return scope.Close(String::New(strerror(errno))); } if (args.Length() > 3) { mode = args[3]->IntegerValue(); } else { mode = fileinfo.st_mode; } mode &= 0777; int fd = open(*srcPath, O_RDONLY); if (fd < 0) { return scope.Close(String::New(strerror(errno))); } #ifdef libssh2_scp_send64 LIBSSH2_CHANNEL *channel = libssh2_scp_send64(ssh2->mSession, *dstPath, mode, fileinfo.st_size, 0, 0); #else LIBSSH2_CHANNEL *channel = libssh2_scp_send(ssh2->mSession, *dstPath, mode, fileinfo.st_size); #endif if (!channel) { char *errmsg; int errlen; libssh2_session_last_error(ssh2->mSession, &errmsg, &errlen, 0); return scope.Close(String::New(errmsg, errlen)); } char mem[1024]; ssize_t toWrite = fileinfo.st_size; while (toWrite > 0) { ssize_t nRead = read(fd, mem, 1024); if (nRead < 0) { int eNum = errno; libssh2_channel_free(channel); close(fd); return scope.Close(String::New(strerror(eNum))); } int rc = libssh2_channel_write(channel, mem, nRead); if (rc < 0) { char *errmsg; int errlen; libssh2_session_last_error(ssh2->mSession, &errmsg, &errlen, 0); libssh2_channel_free(channel); close(fd); return scope.Close(String::New(errmsg)); } toWrite -= nRead; } close(fd); libssh2_channel_free(channel); return scope.Close(True()); }
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; }
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; }
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; }
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; }