Пример #1
0
static int open_location(struct location *loc, int flag){
  if(loc->is_ssh && flag==WRITE){
    loc->session=connect_ssh(loc->host,loc->user,verbosity);
    if(!loc->session){
      fprintf(stderr,"Couldn't connect to %s\n",loc->host);
      return -1;
    }
    loc->scp=ssh_scp_new(loc->session,SSH_SCP_WRITE,loc->path);
    if(!loc->scp){
      fprintf(stderr,"error : %s\n",ssh_get_error(loc->session));
      return -1;
    }
    if(ssh_scp_init(loc->scp)==SSH_ERROR){
      fprintf(stderr,"error : %s\n",ssh_get_error(loc->session));
      ssh_scp_free(loc->scp);
      return -1;
    }
    return 0;
  } else if(loc->is_ssh && flag==READ){
    loc->session=connect_ssh(loc->host, loc->user,verbosity);
    if(!loc->session){
      fprintf(stderr,"Couldn't connect to %s\n",loc->host);
      return -1;
    }
    loc->scp=ssh_scp_new(loc->session,SSH_SCP_READ,loc->path);
    if(!loc->scp){
      fprintf(stderr,"error : %s\n",ssh_get_error(loc->session));
      return -1;
    }
    if(ssh_scp_init(loc->scp)==SSH_ERROR){
      fprintf(stderr,"error : %s\n",ssh_get_error(loc->session));
      ssh_scp_free(loc->scp);
      return -1;
    }
    return 0;
  } else {
    loc->file=fopen(loc->path,flag==READ ? "r":"w");
    if(!loc->file){
        if(errno==EISDIR){
                if(chdir(loc->path)){
                        fprintf(stderr,"Error changing directory to %s: %s\n",loc->path,strerror(errno));
                        return -1;
                }
                return 0;
        }
        fprintf(stderr,"Error opening %s: %s\n",loc->path,strerror(errno));
        return -1;
        }
    return 0;
  }
  return -1;
}
Пример #2
0
static int fetch_files(ssh_session session){
  int size;
  char buffer[16384];
  int mode;
  char *filename;
  int r;
  ssh_scp scp=ssh_scp_new(session, SSH_SCP_READ | SSH_SCP_RECURSIVE, "/tmp/libssh_tests/*");
  if(ssh_scp_init(scp) != SSH_OK){
	  fprintf(stderr,"error initializing scp: %s\n",ssh_get_error(session));
	  return -1;
  }
  printf("Trying to download 3 files (a,b,d) and 1 directory (c)\n");
  do {

	  r=ssh_scp_pull_request(scp);
	  switch(r){
	  case SSH_SCP_REQUEST_NEWFILE:
		  size=ssh_scp_request_get_size(scp);
		  filename=strdup(ssh_scp_request_get_filename(scp));
		  mode=ssh_scp_request_get_permissions(scp);
		  printf("downloading file %s, size %d, perms 0%o\n",filename,size,mode);
		  free(filename);
		  ssh_scp_accept_request(scp);
		  r=ssh_scp_read(scp,buffer,sizeof(buffer));
		  if(r==SSH_ERROR){
			  fprintf(stderr,"Error reading scp: %s\n",ssh_get_error(session));
			  return -1;
		  }
		  printf("done\n");
		  break;
	  case SSH_ERROR:
		  fprintf(stderr,"Error: %s\n",ssh_get_error(session));
		  return -1;
	  case SSH_SCP_REQUEST_WARNING:
		  fprintf(stderr,"Warning: %s\n",ssh_scp_request_get_warning(scp));
		  break;
	  case SSH_SCP_REQUEST_NEWDIR:
		  filename=strdup(ssh_scp_request_get_filename(scp));
		  mode=ssh_scp_request_get_permissions(scp);
		  printf("downloading directory %s, perms 0%o\n",filename,mode);
		  free(filename);
		  ssh_scp_accept_request(scp);
		  break;
	  case SSH_SCP_REQUEST_ENDDIR:
		  printf("End of directory\n");
		  break;
	  case SSH_SCP_REQUEST_EOF:
		  printf("End of requests\n");
		  goto end;
	  }
  } while (1);
  end:
  return 0;
}
Пример #3
0
int 
lscp_new(lua_State *L) {
	ssh_session session=get_session(L);
    const char * location  = luaL_checkstring(L, 2);
	int mode=get_scp_mode(L,3);
	ssh_scp scp=ssh_scp_new(session, mode, location);

	if(ssh_scp_init(scp) != SSH_OK){
		ssh_scp_free(scp);
		luaL_error(L,ssh_get_error(session));
	}

	struct scp_ud * scpud = lua_newuserdata(L, sizeof(*scpud));
	scpud->scp	=scp;
	
    if (luaL_newmetatable(L, "scpmeta")) {
        lua_pushcfunction(L, lscp_close);
        lua_setfield(L, -2, "__gc");
        luaL_Reg l[] = {
			{"close",lscp_close},
			{"write",lscp_write},
			{"read",lscp_read},
			{"pull_request",lscp_pull_request},
			{"request_get_warning",lscp_request_get_warning},
			{"request_get_size",lscp_request_get_size},
			{"request_get_filename",lscp_request_get_filename},
			{"request_get_permissions",lscp_request_get_permissions},
			{"accept_request",lscp_accept_request},
			{"leave_directory",lscp_leave_directory},
			{"push_directory",lscp_push_directory},
			{"push_file",lscp_push_file},
			{"deny_request",lscp_deny_request},
            { NULL, NULL },
        };
        luaL_newlib(L, l);
        lua_setfield(L, -2, "__index");
    }
    lua_setmetatable(L, -2);
    return 1;
}
Пример #4
0
    int SSH::scp(const std::string& filePath, const byte* data, size_t size) {
        ssh_scp scp;
        int rc;

        size_t found = filePath.find_last_of("/\\");
        std::string filename = filePath.substr(found+1);
        std::string path = filePath.substr(0, found+1);

        scp = ssh_scp_new(session_, SSH_SCP_WRITE, path.c_str());
        if (scp == NULL) {
            throw Error(1, ssh_get_error(session_));
            rc = SSH_ERROR;
        } else {
            rc = ssh_scp_init(scp);
            if (rc != SSH_OK) {
                throw Error(1, ssh_get_error(session_));
            } else {
#ifdef  _MSC_VER
// S_IRUSR & S_IWUSR not in MSVC (0000400 & 0000200 in /usr/include/sys/stat.h on MacOS-X 10.8) 
#define S_IRUSR S_IREAD
#define S_IWUSR S_IWRITE
#endif
                rc = ssh_scp_push_file (scp, filename.c_str(), size, S_IRUSR |  S_IWUSR);
                if (rc != SSH_OK) {
                    throw Error(1, ssh_get_error(session_));
                } else {
                    rc = ssh_scp_write(scp, data, size);
                    if (rc != SSH_OK) {
                        throw Error(1, ssh_get_error(session_));
                    }
                }
                ssh_scp_close(scp);
            }
            ssh_scp_free(scp);
        }

        return rc;
    }
Пример #5
0
int scp_write(ssh_session session)
{
    ssh_scp scp;
    int rc;

    scp = ssh_scp_new(session, SSH_SCP_WRITE | SSH_SCP_RECURSIVE, ".");
    if(scp == NULL)
    {
        fprintf(stderr, "Error allocation scp session: %s\n", ssh_get_error(session));
        return SSH_ERROR;
    }

    rc = ssh_scp_init(scp);
    if(rc != SSH_OK)
    {
        fprintf(stderr, "Error initializing scp session: %s\n", ssh_get_error(session));
        ssh_scp_free(scp);
        return rc;
    }

    ssh_scp_close(scp);
    ssh_scp_free(scp);
    return SSH_OK;
}
Пример #6
0
static int do_write(const char* path, FILE* fp, ftp_transfer_func hookf,
                    uint64_t offset)
{
  /* try to set up a scp connection */
  if (gvSSHTrySCP && !offset)
  {
    ssh_scp scp = ssh_scp_new(ftp->session, SSH_SCP_WRITE, path);
    if (scp != NULL)
    {
      int rc = ssh_scp_init(scp);
      if (rc == SSH_OK)
        return do_scp_write(scp, path, fp, hookf);
      ssh_scp_free(scp);
    }
  }

  time_t then = time(NULL) - 1;
  ftp_set_close_handler();

  if (hookf)
    hookf(&ftp->ti);
  ftp->ti.begin = false;

  struct stat sb;
  errno = 0;
  if (fstat(fileno(fp), &sb) == -1)
  {
    ftp_err(_("Couldn't fstat local file: %s\n"), strerror(errno));
    return -1;
  }

  /* open remote file */
  sftp_file file = sftp_open(ftp->sftp_session, path, O_WRONLY | O_CREAT |
      (offset == 0u ? O_TRUNC : 0), sb.st_mode);
  if (!file)
  {
    ftp_err(_("Cannot open file for writing: %s\n"), ssh_get_error(ftp->session));
    return -1;
  }

  /* seek to offset */
  int r = sftp_seek64(file, offset);
  if (r != SSH_OK)
  {
    ftp_err(_("Failed to seek: %s\n"), ssh_get_error(ftp->session));
    sftp_close(file);
    return -1;
  }

  /* read file */
  char buffer[SSH_BUFSIZ];
  ssize_t nbytes = 0;
  errno = 0;
  while ((nbytes = fread(buffer, sizeof(char), sizeof(buffer), fp)) > 0)
  {
    if (ftp_sigints() > 0)
    {
      ftp_trace("break due to sigint\n");
      break;
    }

    ssize_t nwritten = sftp_write(file, buffer, nbytes);
    if (nwritten != nbytes)
    {
      ftp_err(_("Error while writing to file: %s\n"), ssh_get_error(ftp->session));
      sftp_close(file);
      return -1;
    }

    ftp->ti.size += nbytes;
    if (hookf)
    {
      time_t now = time(NULL);
      if (now > then)
      {
        hookf(&ftp->ti);
        then = now;
      }
    }
    errno = 0;
  }

  if (ferror(fp))
  {
    ftp_err(_("Failed to read from file: %s\n"), strerror(errno));
    r = -1;
  }

  sftp_close(file);
  return r;

}
Пример #7
0
static int do_read(const char* infile, FILE* fp, getmode_t mode,
                   ftp_transfer_func hookf, uint64_t offset)
{
  if (gvSSHTrySCP && !offset)
  {
    char* escaped = bash_backslash_quote(infile);
    /* try to set up a scp connection */
    ssh_scp scp = ssh_scp_new(ftp->session, SSH_SCP_READ, escaped);
    free(escaped);
    if (scp != NULL)
    {
      int rc = ssh_scp_init(scp);
      if (rc == SSH_OK)
        return do_scp_read(scp, infile, fp, mode, hookf);
      ssh_scp_free(scp);
    }
  }

  time_t then = time(NULL) - 1;
  ftp_set_close_handler();

  if (hookf)
    hookf(&ftp->ti);
  ftp->ti.begin = false;

  /* check if remote file is not a directory */
  sftp_attributes attrib = sftp_stat(ftp->sftp_session, infile);
  if (!attrib)
    return -1;

  if (S_ISDIR(attrib->permissions))
  {
    ftp_err(_("Cannot download a directory: %s\n"), infile);
    sftp_attributes_free(attrib);
    return -1;
  }
  sftp_attributes_free(attrib);

  /* open remote file */
  sftp_file file = sftp_open(ftp->sftp_session, infile, O_RDONLY, 0);
  if (!file)
  {
    ftp_err(_("Cannot open file for reading: %s\n"), ssh_get_error(ftp->session));
    return -1;
  }

  /* seek to offset */
  int r = sftp_seek64(file, offset);
  if (r != SSH_OK)
  {
    ftp_err(_("Failed to seek: %s\n"), ssh_get_error(ftp->session));
    sftp_close(file);
    return -1;
  }

  /* read file */
  char buffer[SSH_BUFSIZ];
  ssize_t nbytes = 0;
  while ((nbytes = sftp_read(file, buffer, sizeof(buffer))) > 0)
  {
    if (ftp_sigints() > 0)
    {
      ftp_trace("break due to sigint\n");
      break;
    }

    errno = 0;
    if (fwrite(buffer, nbytes, 1, fp) != 1)
    {
      ftp_err(_("Error while writing to file: %s\n"), strerror(errno));
      ftp->ti.ioerror = true;
      sftp_close(file);
      return -1;
    }

    ftp->ti.size += nbytes;
    if (hookf)
    {
      time_t now = time(NULL);
      if (now > then)
      {
        hookf(&ftp->ti);
        then = now;
      }
    }
  }

  if (nbytes < 0)
  {
    ftp_err(_("Error while reading from file: %s\n"), ssh_get_error(ftp->session));
    r = -1;
  }

  sftp_close(file);
  return r;
}