示例#1
0
void clSFTP::Close()
{
    if(m_sftp) {
        sftp_free(m_sftp);
    }

    m_connected = false;
    m_sftp = NULL;
}
示例#2
0
void
remmina_sftp_free (RemminaSFTP *sftp)
{
	if (sftp->sftp_sess)
	{
		sftp_free (sftp->sftp_sess);
		sftp->sftp_sess = NULL;
	}
	remmina_ssh_free (REMMINA_SSH (sftp));
}
示例#3
0
文件: ssh.cpp 项目: obklar/exiv2
    void SSH::openSftp() {
        if (sftp_) return;

        sftp_ = sftp_new(session_);
        if (sftp_ == NULL) {
            throw Error(1, "Unable to create the the sftp session");
        }
        if (sftp_init(sftp_) != SSH_OK) {
            sftp_free(sftp_);
            throw Error(1, "Error initializing SFTP session");
        }
    }
示例#4
0
文件: main.c 项目: Paxxi/libssh
int sftp_test(SSH_SESSION *session, int test){
  SFTP_SESSION *sftp=sftp_new(session);
  SFTP_FILE *file;
  int wrote=0;
  char name[128];
  if(sftp == NULL)
    return SSH_ERROR;
  if(sftp_init(sftp)<0){
    printf("problem initializing sftp : %s\n",ssh_get_error(session));
    return SSH_ERROR;
  }
  if(test==TEST_WRITE){
    snprintf(name,sizeof(name),"/tmp/libsshstress%d",rand());
    file=sftp_open(sftp,name,O_RDWR|O_CREAT,0777);
    if(!file){
      printf("Failed to open file : %s\n",ssh_get_error(session));
      sftp_free(sftp);
      return SSH_ERROR;
    }
    while(wrote<FILESIZE){
      int max=FILESIZE-wrote;
      int towrite=rand()%max + 1;
      int ret=sftp_write(file,&samplefile[wrote],towrite);
      if(ret<=0){
        printf("Problem while writing : %s\n",ssh_get_error(session));
        sftp_free(sftp);
        return SSH_ERROR;
      }
      if(ret != towrite){
        printf("Asked to write %d, wrote %d\n",towrite,ret);
      }
      wrote += ret;
    }
    sftp_close(file);
  }
  sftp_free(sftp);
  return SSH_OK;
}
示例#5
0
void vio_module_shutdown(csync_vio_method_t *method) {
  (void) method;

  if (_sftp_session) {
    sftp_free(_sftp_session);
  }
  if (_ssh_session) {
    ssh_disconnect(_ssh_session);
  }
  if (_ssh_callbacks) {
    free(_ssh_callbacks);
  }

  ssh_finalize();
}
void FSSftp::CloseSession()
{
	if ( sftpSession )
	{
		sftp_free( sftpSession );
		sftpSession = 0;
	}

	if ( sshSession )
	{
		ssh_disconnect( sshSession );
		ssh_free( sshSession );
		sshSession = 0;
	}
}
示例#7
0
文件: v1_fusexmp.c 项目: banaka/AOS
int main(int argc, char *argv[])
{
	umask(0);

        con.verbosity = SSH_LOG_PROTOCOL;
        con.port = 22;
	con.user = "******";
        con.password = "******";
        con.my_ssh_session = ssh_new();
	con.mountpath = "/tmp/fuse/";
        if (con.my_ssh_session == NULL)
            perror("unable to craete a session");
        //ssh_options_set(con.my_ssh_session, SSH_OPTIONS_USER, "akanksha");
        //ssh_options_set(con.my_ssh_session, SSH_OPTIONS_HOST, "192.168.1.2");
        ssh_options_set(con.my_ssh_session, SSH_OPTIONS_HOST, "localhost");
        ssh_options_set(con.my_ssh_session, SSH_OPTIONS_LOG_VERBOSITY, &con.verbosity);
        ssh_options_set(con.my_ssh_session, SSH_OPTIONS_PORT, &con.port);
        con.rc = ssh_connect(con.my_ssh_session);
        if (con.rc != SSH_OK)
        {
             fprintf(stderr, "Error connecting to localhost: %s\n",
             ssh_get_error(con.my_ssh_session));
        } else{
             fprintf(stderr, "Connection succesful");
        }

        //con.password = getpass("Password: "******"Error authenticating with password: %s\n",
              ssh_get_error(con.my_ssh_session));
              ssh_disconnect(con.my_ssh_session);
              ssh_free(con.my_ssh_session);
        }

	
	con.sftp = create_sftp_session(); 
        int res = fuse_main(argc, argv, &xmp_oper, NULL);
        perror("Fuse main called ");
        ssh_disconnect(con.my_ssh_session);
        sftp_free(con.sftp);
	ssh_free(con.my_ssh_session);
        return res;
}
示例#8
0
文件: ssh.cpp 项目: svkampen/Sonance
sftp_session sn_sftp_get_session(ssh_session session) {
	sftp_session s = sftp_new(session);

	if (!s) {
		std::cerr << "[sftp\tfail] Error allocating SFTP session: " << ssh_get_error(session)
			<< std::endl;
		exit(ERR_SFTP_ALLOC_SESSION);
	}

	int rc = sftp_init(s);
	if (rc != SSH_OK) {
		std::cerr << "Error initializing SFTP session: " << sftp_get_error(s) << std::endl;
		sftp_free(s);
		exit(ERR_SFTP_INIT_SESSION);
	}

	return s;
}
示例#9
0
文件: v1_fusexmp.c 项目: banaka/AOS
static sftp_session create_sftp_session(){
       perror("Inside getattr");
        sftp_session sftp = sftp_new(con.my_ssh_session);
        if (sftp == NULL)
            {
                fprintf(stderr, "Error allocating SFTP session: %s\n",
                ssh_get_error(con.my_ssh_session));
                return NULL;
             }
        perror("sftp established");

        int rc = sftp_init(sftp);
        perror("after init");
        if (rc != SSH_OK)
        {
                fprintf(stderr, "Error initializing SFTP session: %s.\n",
                sftp_get_error(sftp));
                sftp_free(sftp);
                return NULL;
        }
	return sftp;
}
示例#10
0
文件: ftp.c 项目: casualuser/yafc
void ftp_reset_vars(void)
{
    sock_destroy(ftp->data);
    ftp->data = NULL;

    sock_destroy(ftp->ctrl);
    ftp->ctrl = NULL;

    host_destroy(ftp->host);
    ftp->host = NULL;

#ifdef HAVE_LIBSSH
		if (ftp->session)
		{
			sftp_free(ftp->sftp_session);
			ssh_disconnect(ftp->session);
			ssh_free(ftp->session);
			ftp->sftp_session = NULL;
			ftp->session = NULL;
		}
#endif

    url_destroy(ftp->url);
    ftp->url = NULL;

    ftp->connected = false;
    ftp->loggedin = false;

    ftp->has_mdtm_command = true;
    ftp->has_size_command = true;
    ftp->has_pasv_command = true;
    ftp->has_stou_command = true;
    ftp->has_site_chmod_command = true;
    ftp->has_site_idle_command = true;
    ftp->has_mlsd_command = true;

    list_free(ftp->dirs_to_flush);
    ftp->dirs_to_flush = list_new((listfunc)free);

    list_free(ftp->cache);
    ftp->cache = list_new((listfunc)rdir_destroy);

    /* don't assume server is in ascii mode initially even if RFC says so */
    ftp->prev_type = '?';

    ftp->code = ctNone;
    ftp->fullcode = 0;

    ftp->reply_timeout = gvCommandTimeout;

    free(ftp->last_mkpath);
    ftp->last_mkpath = 0;

#ifdef SECFTP
    sec_end();
    ftp->request_data_prot = 0;
    ftp->buffer_size = 0;
#endif
    ftp->LIST_type = ltUnknown;

    list_free(ftp->taglist);
    ftp->taglist = list_new((listfunc)rfile_destroy);
}
示例#11
0
int FSSftp::CheckSession( int* err, FSCInfo* info )
{
	if ( sshSession ) { return 0; }

	try
	{
		sshSession = ssh_new();

		if ( !sshSession ) { throw int( SSH_INTERROR_X3 ); }

		if ( ssh_options_set( sshSession, SSH_OPTIONS_HOST, unicode_to_utf8( _operParam.server.Data() ).ptr() ) )
		{
			throw int( SSH_INTERROR_X3 );
		}

		int port = _operParam.port;

		if ( ssh_options_set( sshSession, SSH_OPTIONS_PORT, &port ) )
		{
			throw int( SSH_INTERROR_X3 );
		}


		FSString userName = "";

		if ( _operParam.user.Data()[0] )
		{
			userName = _operParam.user.Data();
		}
		else
		{
			char* ret = getenv( "LOGNAME" );

			if ( ret )
			{
				userName = FSString( sys_charset_id, ret );
				_operParam.user = userName.GetUnicode();

				MutexLock infoLock( &infoMutex );
				_infoParam.user = userName.GetUnicode();
			}
		};

		if ( ssh_options_set( sshSession, SSH_OPTIONS_USER, ( char* )userName.Get( _operParam.charset ) ) ) //есть сомнения, что надо все таки в utf8
		{
			throw int( SSH_INTERROR_X3 );
		}

		if ( ssh_connect( sshSession ) != SSH_OK )
		{
			throw int( SSH_INTERROR_CONNECT );
		}


		int method = ssh_userauth_list( sshSession, 0 );

		int ret;


		static unicode_t userSymbol = '@';

		if ( method & SSH_AUTH_METHOD_PASSWORD )
		{
			FSPromptData data;
			data.visible = false;
			data.prompt = utf8_to_unicode( "Password:"******"SFTP_" ).ptr(),
			        carray_cat<unicode_t>( userName.GetUnicode(), &userSymbol, _operParam.server.Data() ).ptr(),
			        &data, 1 ) ) { throw int( SSH_INTERROR_STOPPED ); }

			ret = ssh_userauth_password( sshSession,
			                             ( char* )FSString( _operParam.user.Data() ).Get( _operParam.charset ),
			                             ( char* )FSString( data.prompt.Data() ).Get( _operParam.charset ) );
		}

		if ( ret != SSH_AUTH_SUCCESS &&
		     ( method & SSH_AUTH_METHOD_INTERACTIVE ) != 0 )
		{
			while ( true )
			{
				ret = ssh_userauth_kbdint( sshSession, 0, 0 );

				if ( ret != SSH_AUTH_INFO ) { break; }

				const char* instruction = ssh_userauth_kbdint_getinstruction( sshSession );

				if ( !instruction ) { instruction = ""; }

				int n = ssh_userauth_kbdint_getnprompts( sshSession );

				if ( n <= 0 ) { continue; }

				std::vector<FSPromptData> pData( n );
				int i;

				for ( i = 0; i < n; i++ )
				{
					char echo;
					const char* prompt = ssh_userauth_kbdint_getprompt( sshSession, i, &echo );

					if ( !prompt ) { break; }

					pData[i].visible = echo != 0;
					pData[i].prompt = utf8_to_unicode( prompt ).ptr();
				}

				if ( !info ) { throw int( SSH_INTERROR_AUTH ); }

				if ( !info->Prompt(
				        utf8_to_unicode( "SFTP" ).ptr(),
				        carray_cat<unicode_t>( userName.GetUnicode(), &userSymbol, _operParam.server.Data() ).ptr(),
				        pData.ptr(), n ) ) { throw int( SSH_INTERROR_STOPPED ); }

				for ( i = 0; i < n; i++ )
				{
					if ( ssh_userauth_kbdint_setanswer( sshSession, i, ( char* )FSString( pData[i].prompt.Data() ).Get( _operParam.charset ) ) < 0 )
					{
						throw int( SSH_INTERROR_AUTH );
					}
				}
			}
		}

		if ( ret != SSH_AUTH_SUCCESS )
		{
			if ( ret == SSH_AUTH_PARTIAL )
			{
				throw int( SSH_INTERROR_UNSUPPORTED_AUTH );
			}
			else
			{
				throw int( SSH_INTERROR_AUTH );
			}
		}

		sftpSession = sftp_new( sshSession );

		if ( !sftpSession ) { throw int( SSH_INTERROR_FATAL ); }

		if ( sftp_init( sftpSession ) != SSH_OK ) { throw int( SSH_INTERROR_FATAL ); }

	}
	catch ( int e )
	{
		if ( err ) { *err = e; }

		if ( sftpSession ) { sftp_free( sftpSession ); }

		if ( sshSession ) { ssh_free( sshSession ); }

		sshSession = 0;
		sftpSession = 0;
		return ( e == SSH_INTERROR_STOPPED ) ? -2 : -1;
	}

	return 0;
}
示例#12
0
文件: ssh_cmd.c 项目: casualuser/yafc
int ssh_open_url(url_t* urlp)
{
  ftp->session = ssh_new();
  if (!ftp->session)
    return -1;

  /* set log level */
  if (ftp_get_verbosity() == vbDebug)
  {
    int verbosity = SSH_LOG_PROTOCOL;
    ssh_options_set(ftp->session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
  }

  /* If we have ssh options from yafcrc, load them */
  if (gvSSHOptions) {
    args_t *args = args_create();
    args_init(args, 0, NULL);
    args_push_back(args, gvSSHOptions);
    int argc = 0;
    if (ssh_options_getopt(ftp->session, &argc, args->argv) != SSH_OK) {
      ftp_err(_("Failed to load SSH options from yafcrc config (ssh_options = '%s')\n"), gvSSHOptions);
      ssh_free(ftp->session);
      ftp->session = NULL;
      return -1;
    }
    args->argc = argc;
    args_destroy(args);
  }

  /* set host name */
  ssh_options_set(ftp->session, SSH_OPTIONS_HOST, urlp->hostname);
  /* if we have port use that one */
  if (urlp->port > 0)
    ssh_options_set(ftp->session, SSH_OPTIONS_PORT, &urlp->port);

  /* parse .ssh/config */
  int r = ssh_options_parse_config(ftp->session, NULL);
  if (r != SSH_OK)
  {
    ftp_err(_("Failed to parse ssh config: %s\n"), ssh_get_error(ftp->session));
    ssh_free(ftp->session);
    ftp->session = NULL;
    return r;
  }

  /* if we have username use that one */
  if (urlp->username)
    ssh_options_set(ftp->session, SSH_OPTIONS_USER, urlp->username);

  /* connect to server */
  r = ssh_connect(ftp->session);
  if (r != SSH_OK)
  {
    ftp_err(_("Couldn't initialise connection to server: %s\n"), ssh_get_error(ftp->session));
    ssh_free(ftp->session);
    ftp->session = NULL;
    return r;
  }

  /* verify server */
  if (verify_knownhost(ftp->session))
  {
    ssh_disconnect(ftp->session);
    ssh_free(ftp->session);
    ftp->session = NULL;
    return -1;
  }

  /* authenticate user */
  r = test_several_auth_methods(ftp->session, urlp);
  if (r != SSH_OK)
  {
    ftp_err(_("Authentication failed: %s\n"), ssh_get_error(ftp->session));
    ssh_disconnect(ftp->session);
    ssh_free(ftp->session);
    ftp->session = NULL;
    return -1;
  }

  ftp->ssh_version = ssh_get_version(ftp->session);
  if (!ftp->ssh_version) {
    ftp_err(_("Couldn't initialise connection to server\n"));
    return -1;
  }

  ftp->sftp_session = sftp_new(ftp->session);
  if (!ftp->sftp_session)
  {
    ftp_err(_("Couldn't initialise ftp subsystem: %s\n"), ssh_get_error(ftp->session));
    ssh_disconnect(ftp->session);
    ssh_free(ftp->session);
    ftp->session = NULL;
    return -1;
  }

  r = sftp_init(ftp->sftp_session);
  if (r != SSH_OK)
  {
    ftp_err(_("Couldn't initialise ftp subsystem: %s\n"), ssh_get_error(ftp->sftp_session));
    sftp_free(ftp->sftp_session);
    ftp->sftp_session = NULL;
    ssh_disconnect(ftp->session);
    ssh_free(ftp->session);
    ftp->session = NULL;
    return r;
  }

  ftp->connected = true;
  ftp->loggedin = true;

  free(ftp->homedir);
  ftp->homedir = ftp_getcurdir();

  url_destroy(ftp->url);
  ftp->url = url_clone(urlp);

  free(ftp->curdir);
  ftp->curdir = xstrdup(ftp->homedir);
  free(ftp->prevdir);
  ftp->prevdir = xstrdup(ftp->homedir);
  if (ftp->url->directory)
    ftp_chdir(ftp->url->directory);

  ftp_get_feat();
  return 0;
}
示例#13
0
static void do_sftp(ssh_session session)
{
	sftp_session sftp=sftp_new(session);
	sftp_dir dir;
	sftp_attributes file;
	char datex[11];
	time_t atime=0;
#ifdef WIN32
	struct tm *mytime=NULL;
	int x=0;
#endif

	if(!sftp)
	{
		prterr("sftp error initialising channel: %s\n",ssh_get_error(session));
		return;
	}
	if(sftp_init(sftp))
	{
        	prterr("error initializing sftp: %s\n",ssh_get_error(session));
		return;
	}
	dir=sftp_opendir(sftp,docfolder);
	if(!dir) 
	{
		prterr("Directory not opened(%s)\n", ssh_get_error(session));
		return;
	}
/* reading the whole directory, file by file */
	while((file=sftp_readdir(sftp,dir)))
	{
		if(strncmp(file->name,".",1))
		{
			if(details==FALSE)
			{
				fprintf(stdout,"%s\r\n",file->name);
			} else {
#ifndef WIN32
#ifdef _POSIX_SOURCE
				atime=file->atime;
				strftime(datex,11,"%m/%d/%Y",localtime(&atime));
#else
				ascftime(datex,"%m/%d/%Y",localtime(&file->atime));
#endif /* _POSIX_SOURCE */
#else /* WIN32 */
				mytime=localctime(&file->atime);
				x=mytime->tm_year;
				while(x>=200) x-=100;
				sprintf(datex,"%02d/%02d/%04d",mytime->tm_mon+1,mytime->tm_mday,1900+my_time->tm_year);
#endif
				fprintf(stdout,"%s\t%llu\t%s\r\n",file->name,(long long unsigned int) file->size,datex);
			}
		}
		if(diagapps)
		{
		prterr("%30s(%.8o) : %s(%.5d) %s(%.5d) : %.10llu bytes\n",
			file->name,
			file->permissions,
			file->owner,
			file->uid,
			file->group,
			file->gid,
			(long long unsigned int) file->size);
		}
		sftp_attributes_free(file);
	}
/* when file=NULL, an error has occured OR the directory listing is end of file */
	if(!sftp_dir_eof(dir))
	{
		prterr("Error: %s\n", ssh_get_error(session));
		return;
	}
	if(sftp_closedir(dir))
	{
		prterr("Error: %s\n", ssh_get_error(session));
		return;
	}
	sftp_free(sftp);
}
示例#14
0
static int sftp_list_file(ssh_session session,char *remote_dir,char *remote_file)
{
	sftp_attributes file;
	char datex[22];
	char mtemp[1024];
	time_t mtime=0;
#ifdef WIN32
	struct tm *mytime=NULL;
	int x=0;
#endif
	sftp_session sftp=sftp_new(session);

	if(!sftp)
	{
		prterr("sftp error initialising channel: %s\n",ssh_get_error(session));
		return(1);
	}
	if(sftp_init(sftp))
	{
        	prterr("error initializing sftp: %s\n",ssh_get_error(session));
		return(1);
	}
	
	memset(datex,0,22);
	memset(mtemp,0,1024);
	if(isEMPTY(remote_dir) || !RDAstrcmp(remote_dir,"\"\""))
	{
		sprintf(mtemp,"%s",remote_file);
	} else {
		sprintf(mtemp,"%s/%s",remote_dir,remote_file);
	}
	file=sftp_stat(sftp,mtemp);
	if(file==NULL)
	{
		/* when file=NULL, an error has occured OR the directory listing is end of file */
		prterr("Error getting file info for [%s]",mtemp);
		sftp_free(sftp);
		return(1);
	}else{
	

#ifndef WIN32
#ifdef _POSIX_SOURCE
		mtime=file->mtime;
		//strftime(datex,22,"%b %d %Y %l:%M %p",localtime(&file->mtime));
		strftime(datex,22,"%b %d %Y %l:%M %p",localtime(&mtime));
#else
		TRACE;
		ascftime(datex,"%b %d %Y %l:%M %p",localtime(&file->mtime));
#endif /* _POSIX_SOURCE */
#else /* WIN32 */
		TRACE;
		mytime=localctime(&file->mtime);
		x=mytime->tm_year;
		while(x>=200) x-=100;
		sprintf(datex,"%02d/%02d/%04d  ",mytime->tm_mon+1,mytime->tm_mday,1900+my_time->tm_year);
#endif
		fprintf(stdout,"%s\t\t%llu\t\t%s\r\n",remote_file,(long long unsigned int) file->size,datex);

		if(diagapps)
		{
		prterr("\"%30s\"(%.8o) : %s(%.5d) %s(%.5d) : %.10llu bytes  %.10llu\n",
			file->name,
			file->permissions,
			file->owner,
			file->uid,
			file->group,
			file->gid,
			(long long unsigned int) file->size,
			(long long unsigned int) file->mtime);
		}
		sftp_attributes_free(file);
	}
	sftp_free(sftp);
	return(0);
}
示例#15
0
static void do_sftp(ssh_session session){
    sftp_session sftp=sftp_new(session);
    sftp_dir dir;
    sftp_attributes file;
    sftp_statvfs_t sftpstatvfs;
    struct statvfs sysstatvfs;
    sftp_file fichier;
    sftp_file to;
    int len=1;
    unsigned int i;
    char data[DATALEN]={0};
    char *lnk;

    unsigned int count;

    if(!sftp){
        fprintf(stderr, "sftp error initialising channel: %s\n",
            ssh_get_error(session));
        return;
    }
    if(sftp_init(sftp)){
        fprintf(stderr, "error initialising sftp: %s\n",
            ssh_get_error(session));
        return;
    }

    printf("Additional SFTP extensions provided by the server:\n");
    count = sftp_extensions_get_count(sftp);
    for (i = 0; i < count; i++) {
      printf("\t%s, version: %s\n",
          sftp_extensions_get_name(sftp, i),
          sftp_extensions_get_data(sftp, i));
    }

    /* test symlink and readlink */
    if (sftp_symlink(sftp, "/tmp/this_is_the_link",
          "/tmp/sftp_symlink_test") < 0) {
      fprintf(stderr, "Could not create link (%s)\n", ssh_get_error(session));
      return;
    }

    lnk = sftp_readlink(sftp, "/tmp/sftp_symlink_test");
    if (lnk == NULL) {
      fprintf(stderr, "Could not read link (%s)\n", ssh_get_error(session));
      return;
    }
    printf("readlink /tmp/sftp_symlink_test: %s\n", lnk);

    sftp_unlink(sftp, "/tmp/sftp_symlink_test");

    if (sftp_extension_supported(sftp, "*****@*****.**", "2")) {
      sftpstatvfs = sftp_statvfs(sftp, "/tmp");
      if (sftpstatvfs == NULL) {
        fprintf(stderr, "statvfs failed (%s)\n", ssh_get_error(session));
        return;
      }

      printf("sftp statvfs:\n"
          "\tfile system block size: %llu\n"
          "\tfundamental fs block size: %llu\n"
          "\tnumber of blocks (unit f_frsize): %llu\n"
          "\tfree blocks in file system: %llu\n"
          "\tfree blocks for non-root: %llu\n"
          "\ttotal file inodes: %llu\n"
          "\tfree file inodes: %llu\n"
          "\tfree file inodes for to non-root: %llu\n"
          "\tfile system id: %llu\n"
          "\tbit mask of f_flag values: %llu\n"
          "\tmaximum filename length: %llu\n",
          (unsigned long long) sftpstatvfs->f_bsize,
          (unsigned long long) sftpstatvfs->f_frsize,
          (unsigned long long) sftpstatvfs->f_blocks,
          (unsigned long long) sftpstatvfs->f_bfree,
          (unsigned long long) sftpstatvfs->f_bavail,
          (unsigned long long) sftpstatvfs->f_files,
          (unsigned long long) sftpstatvfs->f_ffree,
          (unsigned long long) sftpstatvfs->f_favail,
          (unsigned long long) sftpstatvfs->f_fsid,
          (unsigned long long) sftpstatvfs->f_flag,
          (unsigned long long) sftpstatvfs->f_namemax);

      sftp_statvfs_free(sftpstatvfs);

      if (statvfs("/tmp", &sysstatvfs) < 0) {
        fprintf(stderr, "statvfs failed (%s)\n", strerror(errno));
        return;
      }

      printf("sys statvfs:\n"
          "\tfile system block size: %llu\n"
          "\tfundamental fs block size: %llu\n"
          "\tnumber of blocks (unit f_frsize): %llu\n"
          "\tfree blocks in file system: %llu\n"
          "\tfree blocks for non-root: %llu\n"
          "\ttotal file inodes: %llu\n"
          "\tfree file inodes: %llu\n"
          "\tfree file inodes for to non-root: %llu\n"
          "\tfile system id: %llu\n"
          "\tbit mask of f_flag values: %llu\n"
          "\tmaximum filename length: %llu\n",
          (unsigned long long) sysstatvfs.f_bsize,
          (unsigned long long) sysstatvfs.f_frsize,
          (unsigned long long) sysstatvfs.f_blocks,
          (unsigned long long) sysstatvfs.f_bfree,
          (unsigned long long) sysstatvfs.f_bavail,
          (unsigned long long) sysstatvfs.f_files,
          (unsigned long long) sysstatvfs.f_ffree,
          (unsigned long long) sysstatvfs.f_favail,
          (unsigned long long) sysstatvfs.f_fsid,
          (unsigned long long) sysstatvfs.f_flag,
          (unsigned long long) sysstatvfs.f_namemax);
    }

    /* the connection is made */
    /* opening a directory */
    dir=sftp_opendir(sftp,"./");
    if(!dir) {
        fprintf(stderr, "Directory not opened(%s)\n", ssh_get_error(session));
        return ;
    }
    /* reading the whole directory, file by file */
    while((file=sftp_readdir(sftp,dir))){
        fprintf(stderr, "%30s(%.8o) : %s(%.5d) %s(%.5d) : %.10llu bytes\n",
            file->name,
            file->permissions,
            file->owner,
            file->uid,
            file->group,
            file->gid,
            (long long unsigned int) file->size);
        sftp_attributes_free(file);
    }
    /* when file=NULL, an error has occured OR the directory listing is end of file */
    if(!sftp_dir_eof(dir)){
        fprintf(stderr, "Error: %s\n", ssh_get_error(session));
        return;
    }
    if(sftp_closedir(dir)){
        fprintf(stderr, "Error: %s\n", ssh_get_error(session));
        return;
    }
    /* this will open a file and copy it into your /home directory */
    /* the small buffer size was intended to stress the library. of course, you can use a buffer till 20kbytes without problem */

    fichier=sftp_open(sftp,"/usr/bin/ssh",O_RDONLY, 0);
    if(!fichier){
        fprintf(stderr, "Error opening /usr/bin/ssh: %s\n",
            ssh_get_error(session));
        return;
    }
    /* open a file for writing... */
    to=sftp_open(sftp,"ssh-copy",O_WRONLY | O_CREAT, 0700);
    if(!to){
        fprintf(stderr, "Error opening ssh-copy for writing: %s\n",
            ssh_get_error(session));
        return;
    }
    while((len=sftp_read(fichier,data,4096)) > 0){
        if(sftp_write(to,data,len)!=len){
            fprintf(stderr, "Error writing %d bytes: %s\n",
                len, ssh_get_error(session));
            return;
        }
    }
    printf("finished\n");
    if(len<0)
        fprintf(stderr, "Error reading file: %s\n", ssh_get_error(session));
    sftp_close(fichier);
    sftp_close(to);
    printf("fichiers ferm\n");
    to=sftp_open(sftp,"/tmp/grosfichier",O_WRONLY|O_CREAT, 0644);
    for(i=0;i<1000;++i){
        len=sftp_write(to,data,DATALEN);
        printf("wrote %d bytes\n",len);
        if(len != DATALEN){
            printf("chunk %d : %d (%s)\n",i,len,ssh_get_error(session));
        }
    }
    sftp_close(to);

    /* close the sftp session */
    sftp_free(sftp);
    printf("sftp session terminated\n");
}
示例#16
0
文件: ssh.cpp 项目: obklar/exiv2
 SSH::~SSH() {
     if (sftp_) sftp_free(sftp_);
     ssh_disconnect(session_);
     ssh_free(session_);
 }
示例#17
0
int main(int argc, char *argv[])
{
    int fuse_stat;
    ggnfs_data.remoteHost =argv[1];
    ggnfs_data.rootdir = realpath(argv[2], NULL);
    
    printf("hostname -> %s\n", ggnfs_data.remoteHost);
    printf("rootdir -> %s\n", ggnfs_data.rootdir);
    /* start new ssh session */
    int rc;
    ggnfs_data.session = ssh_new();
    if (ggnfs_data.session == NULL)
        exit(-1);
        
     ssh_options_set(ggnfs_data.session, SSH_OPTIONS_HOST, ggnfs_data.remoteHost);   
     rc = ssh_connect(ggnfs_data.session);
     if (rc != SSH_OK)
        {
            fprintf(stderr, "Error connecting to remoteHost: %s\n",
            ssh_get_error(ggnfs_data.session));
            ssh_free(ggnfs_data.session);
            exit(-1);
        }
        // Verify the server's identity
        // For the source code of verify_knowhost(), check previous example
        if (verify_knownhost(ggnfs_data.session) < 0)
        {
            ssh_disconnect(ggnfs_data.session);
            ssh_free(ggnfs_data.session);
            exit(-1);
        }
        // Authenticate ourselves
        //char * password = "******";// getpass("Password: "******"Error authenticating with password: %s\n",
            ssh_get_error(ggnfs_data.session));
            ssh_disconnect(ggnfs_data.session);
            ssh_free(ggnfs_data.session);
            exit(-1);
        }
     

    /** making sftp session **/
    int rc_sftp;
    ggnfs_data.sftp = sftp_new(ggnfs_data.session);
    if (ggnfs_data.sftp == NULL)
    {
         fprintf(stderr, "Error allocating SFTP session: %s\n",
         ssh_get_error(ggnfs_data.session));
     //    return SSH_ERROR;
     }

    rc_sftp = sftp_init(ggnfs_data.sftp);
    if (rc_sftp != SSH_OK)
    {
         fprintf(stderr, "Error initializing SFTP session: %s.\n",
         sftp_get_error(ggnfs_data.sftp));
         sftp_free(ggnfs_data.sftp);
      //   return rc;
    }

	 
    /* Adjusting agruments to pass to FUSE mount function*/
    int i = 1;
    for(; i < argc; ++i) {
      argv[i] = argv[i+1];
    }
      argv[argc-1] = NULL;
      argc--;

    /*** TESTING: status working ***/
    //printf("show_remote_ls\n");
    //show_remote_ls(ggnfs_data->session);
    sftp_list_dir(ggnfs_data.session, ggnfs_data.sftp);


    printf("about to call fuse_main\n");
    fuse_stat = fuse_main(argc, argv, &ggnfs_oper, NULL);
    printf("fuse_main returned %d\n", fuse_stat);

    ssh_disconnect(ggnfs_data.session);
    ssh_free(ggnfs_data.session);
    sftp_free(ggnfs_data.sftp);
    return fuse_stat;

}
示例#18
0
/* it's just a proof of concept code for sftp, till i write a real documentation about it */
void do_sftp(SSH_SESSION *session){
    SFTP_SESSION *sftp=sftp_new(session);
    SFTP_DIR *dir;
    SFTP_ATTRIBUTES *file;
    SFTP_FILE *fichier;
    SFTP_FILE *to;
    int len=1;
    int i;
    char data[8000];
    if(!sftp){
        ssh_say(0,"sftp error initialising channel : %s\n",ssh_get_error(session));
        return;
    }
    if(sftp_init(sftp)){
        ssh_say(0,"error initialising sftp : %s\n",ssh_get_error(session));
        return;
    }
    /* the connection is made */
    /* opening a directory */
    dir=sftp_opendir(sftp,"./");
    if(!dir) {
        ssh_say(0,"Directory not opened(%s)\n",ssh_get_error(session));
        return ;
    }
    /* reading the whole directory, file by file */
    while((file=sftp_readdir(sftp,dir))){
        ssh_say(0,"%30s(%.8lo) : %.5d.%.5d : %.10lld bytes\n",file->name,file->permissions,file->uid,file->gid,file->size);
        sftp_attributes_free(file);
    }
    /* when file=NULL, an error has occured OR the directory listing is end of file */
    if(!sftp_dir_eof(dir)){
        ssh_say(0,"error : %s\n",ssh_get_error(session));
        return;
    }
    if(sftp_dir_close(dir)){
        ssh_say(0,"Error : %s\n",ssh_get_error(session));
        return;
    }
    /* this will open a file and copy it into your /home directory */
    /* the small buffer size was intended to stress the library. of course, you can use a buffer till 20kbytes without problem */

    fichier=sftp_open(sftp,"/usr/bin/ssh",O_RDONLY,NULL);
    if(!fichier){
        ssh_say(0,"Error opening /usr/bin/ssh : %s\n",ssh_get_error(session));
        return;
    }
    /* open a file for writing... */
    to=sftp_open(sftp,"ssh-copy",O_WRONLY | O_CREAT,NULL);
    if(!to){
        ssh_say(0,"Error opening ssh-copy for writing : %s\n",ssh_get_error(session));
        return;
    }
    while((len=sftp_read(fichier,data,4096)) > 0){
        if(sftp_write(to,data,len)!=len){
            ssh_say(0,"error writing %d bytes : %s\n",len,ssh_get_error(session));
            return;
        }
    }
    printf("finished\n");
    if(len<0)
        ssh_say(0,"Error reading file : %s\n",ssh_get_error(session));     
    sftp_file_close(fichier);
    sftp_file_close(to);
    printf("fichiers ferm�\n");
    to=sftp_open(sftp,"/tmp/grosfichier",O_WRONLY|O_CREAT,NULL);
    for(i=0;i<1000;++i){
        len=sftp_write(to,data,8000);
        printf("wrote %d bytes\n",len);
        if(len != 8000){
            printf("chunk %d : %d (%s)\n",i,len,ssh_get_error(session));
        }
    }
    sftp_file_close(to);
    /* close the sftp session */
    sftp_free(sftp);
    printf("session sftp termin�\n");
}