Пример #1
0
void expand_alias_parameters(args_t **args, args_t *alias_args)
{
	int i;
	args_t *new_args;
	args_t *redir_args = 0;
	args_t *deleted;

/*	if(alias_args->argc == 0)
		return;*/

	rearrange_args(*args, ";");

	/* do not include redirections in %-parameters */
	i = rearrange_redirections(alias_args);
	if(i != -1) {
		redir_args = args_create();
		args_add_args2(redir_args, alias_args, i);
		args_del(alias_args, i, alias_args->argc - i);
	}

	deleted = args_create();
	args_add_args(deleted, alias_args);

	new_args = args_create();
	args_add_args(new_args, *args);

	for(i=0; i<new_args->argc; i++)
	{
		char *e;

		/* %* can not be used embedded in an argument */
		if(strcmp(new_args->argv[i], "%*") == 0) {
			args_del(new_args, i, 1);
			args_insert_args(new_args, i, deleted, 0, deleted->argc);
			args_clear(alias_args);
			continue;
		}

		e = strqchr(new_args->argv[i], '%');
		if(e) {
			char *ep;
			char *ins;
			char *tmp;
			int n;

			*e = 0;
			n = strtoul(++e, &ep, 0) - 1;

			if(ep != e && n < alias_args->argc && n >= 0) {
				ins = deleted->argv[n];
				free(alias_args->argv[n]);
				alias_args->argv[n] = 0;
			} else
				ins = 0;

			/* insert the parameter in this argument */
			asprintf(&tmp, "%s%s%s", new_args->argv[i], ins ? ins : "", ep);
			free(new_args->argv[i]);
			new_args->argv[i] = tmp;
		}

	}

	args_destroy(deleted);
	args_remove_empty(alias_args);
	args_add_args(new_args, alias_args);

	if(redir_args) {
		args_add_args(new_args, redir_args);
		args_destroy(redir_args);
	}

	args_remove_empty(new_args);

	args_destroy(*args);
	*args = new_args;
}
Пример #2
0
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;
}