示例#1
0
int main(int argc, char **argv){
  ssh_session session;
	if(opts(argc,argv)<0)
    return EXIT_FAILURE;
  session=connect_ssh(destination,NULL,verbosity);
  if(session == NULL)
  	return EXIT_FAILURE;
  do_sftp(session);
  ssh_disconnect(session);
  ssh_free(session);
  return 0;
}
示例#2
0
文件: psftp.c 项目: rdebath/sgt
/*
 * Main program. Parse arguments etc.
 */
int main(int argc, char *argv[])
{
    int i;
    int portnumber = 0;
    char *userhost, *user;
    int mode = 0;
    int modeflags = 0;
    char *batchfile = NULL;

    flags = FLAG_STDERR | FLAG_INTERACTIVE;
    ssh_get_line = &console_get_line;
    init_winsock();
    sk_init();

    userhost = user = NULL;

    for (i = 1; i < argc; i++) {
	if (argv[i][0] != '-') {
	    if (userhost)
		usage();
	    else
		userhost = dupstr(argv[i]);
	} else if (strcmp(argv[i], "-v") == 0) {
	    verbose = 1, flags |= FLAG_VERBOSE;
	} else if (strcmp(argv[i], "-h") == 0 ||
		   strcmp(argv[i], "-?") == 0) {
	    usage();
	} else if (strcmp(argv[i], "-l") == 0 && i + 1 < argc) {
	    user = argv[++i];
	} else if (strcmp(argv[i], "-P") == 0 && i + 1 < argc) {
	    portnumber = atoi(argv[++i]);
	} else if (strcmp(argv[i], "-pw") == 0 && i + 1 < argc) {
	    console_password = argv[++i];
	} else if (strcmp(argv[i], "-b") == 0 && i + 1 < argc) {
	    mode = 1;
	    batchfile = argv[++i];
	} else if (strcmp(argv[i], "-bc") == 0) {
	    modeflags = modeflags | 1;
	} else if (strcmp(argv[i], "-batch") == 0) {
	    console_batch_mode = TRUE;
	} else if (strcmp(argv[i], "-be") == 0) {
	    modeflags = modeflags | 2;
	} else if (strcmp(argv[i], "--") == 0) {
	    i++;
	    break;
	} else {
	    usage();
	}
    }
    argc -= i;
    argv += i;
    back = NULL;

    /*
     * If a user@host string has already been provided, connect to
     * it now.
     */
    if (userhost) {
	if (psftp_connect(userhost, user, portnumber))
	    return 1;
	do_sftp_init();
    } else {
	printf("psftp: no hostname specified; use \"open host.name\""
	    " to connect\n");
    }

    do_sftp(mode, modeflags, batchfile);

    if (back != NULL && back->socket() != NULL) {
	char ch;
	back->special(TS_EOF);
	sftp_recvdata(&ch, 1);
    }
    WSACleanup();
    random_save_seed();

    return 0;
}
示例#3
0
int main(int argc, char **argv){
    SSH_SESSION *session;
    SSH_OPTIONS *options;
    int auth=0;
    char *password;
    char *banner;
    int state;
    char buf[10];
    unsigned char hash[MD5_DIGEST_LEN];

    options=ssh_options_new();
    if(ssh_options_getopt(options,&argc, argv))
        usage();
    opts(argc,argv);
    signal(SIGTERM,do_exit);
    if(user)
        ssh_options_set_username(options,user);
    ssh_options_set_host(options,host);
    session=ssh_new();
    ssh_set_options(session,options);
    if(ssh_connect(session)){
        fprintf(stderr,"Connection failed : %s\n",ssh_get_error(session));
        ssh_disconnect(session);
	ssh_finalize();
        return 1;
    }
    state=ssh_is_server_known(session);
    switch(state){
        case SSH_SERVER_KNOWN_OK:
            break; /* ok */
        case SSH_SERVER_KNOWN_CHANGED:
            fprintf(stderr,"Host key for server changed : server's one is now :\n");
            ssh_get_pubkey_hash(session,hash);
            ssh_print_hexa("Public key hash",hash,MD5_DIGEST_LEN);
            fprintf(stderr,"For security reason, connection will be stopped\n");
            ssh_disconnect(session);
	    ssh_finalize();
            exit(-1);
        case SSH_SERVER_FOUND_OTHER:
            fprintf(stderr,"The host key for this server was not found but an other type of key exists.\n");
            fprintf(stderr,"An attacker might change the default server key to confuse your client"
                "into thinking the key does not exist\n"
                "We advise you to rerun the client with -d or -r for more safety.\n");
                ssh_disconnect(session);
		ssh_finalize();
                exit(-1);
        case SSH_SERVER_NOT_KNOWN:
            fprintf(stderr,"The server is unknown. Do you trust the host key ?\n");
            ssh_get_pubkey_hash(session,hash);
            ssh_print_hexa("Public key hash",hash,MD5_DIGEST_LEN);
            fgets(buf,sizeof(buf),stdin);
            if(strncasecmp(buf,"yes",3)!=0){
                ssh_disconnect(session);
                exit(-1);
            }
            fprintf(stderr,"This new key will be written on disk for further usage. do you agree ?\n");
            fgets(buf,sizeof(buf),stdin);
            if(strncasecmp(buf,"yes",3)==0){
                if(ssh_write_knownhost(session))
                    fprintf(stderr,"error %s\n",ssh_get_error(session));
            }
            
            break;
        case SSH_SERVER_ERROR:
            fprintf(stderr,"%s",ssh_get_error(session));
            ssh_disconnect(session);
	    ssh_finalize();
            exit(-1);
    }

    /* no ? you should :) */
    auth=ssh_userauth_autopubkey(session);
    if(auth==SSH_AUTH_ERROR){
        fprintf(stderr,"Authenticating with pubkey: %s\n",ssh_get_error(session));
	ssh_finalize();
        return -1;
    }
    banner=ssh_get_issue_banner(session);
    if(banner){
        printf("%s\n",banner);
        free(banner);
    }
    if(auth!=SSH_AUTH_SUCCESS){
        auth=auth_kbdint(session);
        if(auth==SSH_AUTH_ERROR){
            fprintf(stderr,"authenticating with keyb-interactive: %s\n",
                    ssh_get_error(session));
	    ssh_finalize();
            return -1;
        }
    }
    if(auth!=SSH_AUTH_SUCCESS){
        password=getpass("Password : "******"Authentication failed: %s\n",ssh_get_error(session));
            ssh_disconnect(session);
	    ssh_finalize();
            return -1;
        }
        memset(password,0,strlen(password));
    }
    ssh_say(1,"Authentication success\n");
    printf("%s\n",argv[0]);
    if(strstr(argv[0],"sftp")){
        sftp=1;
        ssh_say(1,"doing sftp instead\n");
    }
    if(!sftp){
        if(!cmds[0])
            shell(session);
        else
            batch_shell(session);
    }
    else
        do_sftp(session);    
    if(!sftp && !cmds[0])
        do_cleanup();
    ssh_disconnect(session);
    ssh_finalize();

    return 0;
}
示例#4
0
文件: sshclient.cpp 项目: aopui/gnash
bool
SSHClient::sshConnect(int /* fd */, std::string &hostname)
{
//     GNASH_REPORT_FUNCTION;
    char *password;
    char *banner;
    char *hexa;

    // We always need a hostname to connect to
    if (ssh_options_set(_session, SSH_OPTIONS_HOST, hostname.c_str()) < 0) {
	log_error(_("Couldn't set hostname option"));
	return false;
    }

    // We always need a user name for the connection
    if (_user.empty()) {
	if (ssh_options_set(_session, SSH_OPTIONS_USER, _user.c_str()) < 0) {
	    log_error(_("Couldn't set user name option"));
	    return false;
	}
    }
    
    // Start a new session
    _session = ssh_new();
    if(ssh_connect(_session)){
        log_error(_("Connection failed : %s\n"), ssh_get_error(_session));
	sshShutdown();
        return false;
    }

    _state = ssh_is_server_known(_session);

    unsigned char *hash = 0;
    int hlen = ssh_get_pubkey_hash(_session, &hash);
    if (hlen < 0) {
	sshShutdown();
	return false;
    }
    switch(_state){
      case SSH_SERVER_KNOWN_OK:	// ok
	  log_debug(_("SSH Server is currently known: %d"), _state);
	  break; 
      case SSH_SERVER_KNOWN_CHANGED:
	  log_error(_("Host key for server changed : server's one is now: "));
	  ssh_print_hexa(_("Public key hash"), hash, hlen);
	  free(hash);
	  log_error(_("For security reason, connection will be stopped"));
	  sshShutdown();
	  return false;;
      case SSH_SERVER_FOUND_OTHER:
	  log_error(_("The host key for this server was not found but an other type of key exists."));
	  log_error(_("An attacker might change the default server key to confuse your client"
		    " into thinking the key does not exist"
		      "We advise you to rerun the client with -d or -r for more safety."));
	  sshShutdown();
	  return false;;
      case SSH_SERVER_NOT_KNOWN:
	  hexa = ssh_get_hexa(hash, hlen);
	  free(hash);
	  // FIXME: for now, accecpt all new keys, and update the 
	  // $HOME/.ssh/know_hosts file.
#if 0
	  log_error(_("The server is unknown. Do you trust the host key ? (yes,no)"));
	  log_error(_("Public key hash: %s"), hexa);
	  free(hexa);
	  fgets(buf, sizeof(buf), stdin);
	  if(strncasecmp(buf, "yes", 3) != 0){
	      sshShutdown();
	      return false;
	  }
	  log_error(_("This new key will be written on disk for further usage. do you agree? (yes,no) "));
	  fgets(buf, sizeof(buf), stdin);
	  if(strncasecmp(buf, "yes", 3)==0){
	      if(ssh_write_knownhost(_session))
		  log_error(ssh_get_error(_session));
	  }
#else
	  if(ssh_write_knownhost(_session)) {
	      log_error(ssh_get_error(_session));
	  }
#endif  
	  break;
      case SSH_SERVER_ERROR:
	  free(hash);
	  log_error(ssh_get_error(_session));
	  sshShutdown();
	  return false;
    }
    
    free(hash);
    
    ssh_userauth_none(_session, NULL);
    
    int auth = ssh_auth_list(_session);

//    log_debug("auth: 0x%04x", auth);
    log_debug(_("supported auth methods: "));
    if (auth & SSH_AUTH_METHOD_PUBLICKEY) {
	log_debug(_("\tpublickey"));
    }
    if (auth & SSH_AUTH_METHOD_INTERACTIVE) {
	log_debug(_("\tkeyboard-interactive"));
    }

    /* no ? you should :) */
    auth=ssh_userauth_autopubkey(_session, NULL);
    if(auth == SSH_AUTH_ERROR){
        log_debug(_("Authenticating with pubkey: %s"), ssh_get_error(_session));
	ssh_finalize();
        return false;
    }
    banner = ssh_get_issue_banner(_session);
    if(banner){
        log_debug(banner);
        free(banner);
    }
    if(auth != SSH_AUTH_SUCCESS){
        auth = authKbdint(_session);
        if(auth == SSH_AUTH_ERROR){
            log_error(_("authenticating with keyb-interactive: %s"),
		      ssh_get_error(_session));
	    ssh_finalize();
            return false;
        }
    }
    if(auth != SSH_AUTH_SUCCESS){
        password = getpass("Password: "******"Authentication failed: %s"), ssh_get_error(_session));
            ssh_disconnect(_session);
                ssh_finalize();
            return false;
        }
        memset(password, 0, strlen(password));
    }
    ssh_log(_session, SSH_LOG_FUNCTIONS, "Authentication success");

#if 0
    if(strstr(argv[0],"sftp")){
        sftp = 1;
        ssh_log(_session, SSH_LOG_FUNCTIONS, "Doing sftp instead");
    }
    if(!sftp){
        if(!cmds[0])
            shell(_session);
        else
            batch_shell(_session);
    }
    else
        do_sftp(_session);
    if(!sftp && !cmds[0])
        do_cleanup(0);
#endif
    
    return true;
}
示例#5
0
int main(int argc,char **argv)
#endif
{
	char *env_username=NULL;
	char *env_password=NULL;
	char *env_remote_dir=NULL;
	char *env_remote_file=NULL;
	char *env_remote_server=NULL;
	char *env_temp=NULL;
	int env_remote_port=22;
	int ret=0;
  	ssh_session session;
	char bvalue=FALSE;

	if(argc<5)
	{
		env_username=RDA_GetEnv("SFTP_USERNAME");	
		env_password=RDA_GetEnv("SFTP_PASSWORD");
		env_remote_dir=RDA_GetEnv("SFTP_REMOTE_DIR");
		env_remote_file=RDA_GetEnv("SFTP_REMOTE_FILE");
		env_remote_server=RDA_GetEnv("SFTP_REMOTE_SERVER");
		env_temp=RDA_GetEnv("SFTP_PORT");
		if( (env_username==NULL) && (env_password==NULL) && (env_remote_dir==NULL) && (env_remote_file==NULL) && (env_remote_server==NULL) && (env_temp==NULL) )
		{
			fprintf(stderr,"Not enough arguments specified to continue.\n");
			return(-1);
		}else if( (env_username!=NULL) && (env_password!=NULL) && (env_remote_server!=NULL) )
		{
			pass_word=stralloc(env_password);
			initrdadiag();
			session=connect_ssh(env_remote_server,env_username,FALSE);
			if(session==NULL) return EXIT_FAILURE;
			ret=sftp_list_file(session,env_remote_dir,env_remote_file);
			ssh_disconnect(session);
			ssh_free(session);
			exit(ret);

		}
	}else{
		if(InitializeSubsystems(argc,argv,"ARCHIVE","SFTPLIST")) 
		{
			return(-1);
		}
		bvalue=UsersWorkstationValidated();
		if(!bvalue)
		{
			ShutdownSubsystems();
			return(-1);
		}

		workstation_ip=stralloc(argv[1]);
		docfolder=stralloc(argv[2]);
		logname=stralloc(argv[3]);
		pass_word=stralloc(argv[4]);
		if(argc>5) details=TRUE;
		session=connect_ssh(workstation_ip,logname,FALSE);
		if(session==NULL) return EXIT_FAILURE;
		do_sftp(session);
		ssh_disconnect(session);
		ssh_free(session);
	}
	if(docfolder!=NULL) Rfree(docfolder);
	if(workstation_ip!=NULL) Rfree(workstation_ip);
	if(pass_word!=NULL) Rfree(pass_word);
	if(logname!=NULL) Rfree(logname);
	ShutdownSubsystems();
}