コード例 #1
0
ファイル: burp.c プロジェクト: sxdtxl/burp
static int login(aur_t *aur) {
  int r;
  _cleanup_free_ char *username = NULL, *password = NULL, *error = NULL;

  if (arg_username == NULL) {

    username = ask_username();
    if (username == NULL)
      return log_login_error(ENOMEM, NULL);

    r = aur_set_username(aur, username);
    if (r < 0)
      return log_login_error(r, NULL);

    arg_username = username;
  }

  r = aur_login(aur, &error);
  if (r < 0) {
    switch (r) {
    case -EKEYEXPIRED:
      /* cookie expired */
      log_warn("Your cookie has expired -- using password login");
    /* fallthrough */
    case -ENOKEY:
      password = ask_password();
      if (password == NULL)
        return -ENOMEM;

      r = aur_set_password(aur, password);
      if (r < 0)
        return log_login_error(r, NULL);

      r = aur_login(aur, &error);
      break;
    }

    if (r < 0)
      return log_login_error(r, error);
  }

  return 0;
}
コード例 #2
0
ファイル: burp.c プロジェクト: cinelli/burp
int main(int argc, char **argv) {
  long cookie_expire;
  int ret = 1;

  config = config_new();

  if (curl_init() != 0) {
    fprintf(stderr, "Error: An error occurred while initializing curl\n");
    goto finish;
  }

  ret = parseargs(argc, argv);
  if (ret != 0) {
    return 1;
  }

  if (config->category) {
    config->catnum = category_is_valid(config->category);
    if (config->catnum < 0) {
      usage_categories();
      goto finish;
    }
  } else {
    config->catnum = 1;
  }

  if (optind == argc) {
    fprintf(stderr, "error: no packages specified (use -h for help)\n");
    goto finish;
  }

  /* We can't read the config file without having verbosity set, but the
   * command line options need to take precedence over the config.  file.
   * Therefore, if ((user && pass) || cookie file) is supplied on the command
   * line, we won't read the config file.
   */
  if (!(config->user || config->cookie_file)) {
    read_config_file();
  }

  if (cookie_setup() != 0) {
    goto finish;
  }

  cookie_expire = cookie_expire_time(config->cookie_file, AUR_DOMAIN, AUR_COOKIE_NAME);
  if (cookie_expire > 0) {
    if (time(NULL) < cookie_expire) {
      config->cookie_valid = 1;
    } else {
      fprintf(stderr, "Your cookie has expired. Gathering user and password...\n");
    }
  }

  if (!config->cookie_valid) {
    if (!config->cmdline_user && !config->user) {
      config->user = read_stdin("Enter username", AUR_USER_MAX, 1);
      if (!config->user || !strlen(config->user)) {
        fprintf(stderr, "error: invalid username supplied\n");
        goto finish;
      }
    }

    if (!config->password || (config->cmdline_user && !config->cmdline_passwd)) {
      printf("[%s] ", config->user);
      config->password = read_stdin("Enter password", AUR_PASSWORD_MAX, 0);
    }
  }

  if (config->cookie_valid || aur_login() == 0) {
    char *csrf_token;

    /* booo, stupid hacks. curl doesn't prime curl_slist of cookies
     * we want via CURLINFO_COOKIELIST until we call perform at least
     * once. */
    prime_cookielist();

    csrf_token = get_csrf_token();
    if (csrf_token == NULL) {
      fprintf(stderr, "failed to obtain CSRF token for uploading\n");
      goto finish;
    }

    ret = 0;

    while (optind < argc) {
      int r = aur_upload(argv[optind++], csrf_token);
      if (r != 0) {
        ret = r;
      }
    }
    free(csrf_token);
  }

finish:
  if (config->cookie_file && !config->cookie_persist) {
    debug("Deleting file %s\n", config->cookie_file);
    unlink(config->cookie_file);
  }

  config_free(config);
  curl_cleanup();

  return ret;
}