Пример #1
0
END_TEST

START_TEST (trace_get_level_test) {
  int min_level, max_level, res;
  const char *channel;

  channel = "foo";
  min_level = 1;
  max_level = 2;

  res = pr_trace_get_level(NULL);
  fail_unless(res < 0, "Failed to handle null channel");
  fail_unless(errno == EINVAL, "Failed to set errno to EINVAL, got %d (%s)",
    errno, strerror(errno));

  res = pr_trace_get_level("bar");
  fail_unless(res < 0, "Failed to handle unset channels/levels");
  fail_unless(errno == EPERM, "Failed to set errno to EPERM, got %d (%s)",
    errno, strerror(errno));

  res = pr_trace_set_levels(channel, min_level, max_level);
  fail_unless(res == 0, "Failed to set '%s:%d-%d': %s", channel, min_level,
    max_level, strerror(errno));

  res = pr_trace_get_level("bar");
  fail_unless(res < 0, "Failed to handle unknown channel");
  fail_unless(errno == ENOENT, "Failed to set errno to ENOENT, got %d (%s)",
    errno, strerror(errno));

  res = pr_trace_get_level(channel);
  fail_unless(res == max_level, "Failed to get level %d for channel '%s': %s",
    max_level, channel, strerror(errno));
}
Пример #2
0
static int regexp_exec_pcre(pr_regex_t *pre, const char *str,
    size_t nmatches, regmatch_t *matches, int flags, unsigned long match_limit,
    unsigned long match_limit_recursion) {

  if (pre->pcre != NULL) {
    int res;
    size_t str_len;

    str_len = strlen(str);

    /* Use the default match limits, if set and if the caller did not
     * explicitly provide limits.
     */
    if (match_limit == 0) {
      match_limit = pcre_match_limit;
    }

    if (match_limit_recursion == 0) {
      match_limit_recursion = pcre_match_limit_recursion;
    }

    if (match_limit > 0) {
      if (pre->pcre_extra == NULL) {
        pre->pcre_extra = pcalloc(pre->regex_pool, sizeof(pcre_extra));
      }

      pre->pcre_extra->flags |= PCRE_EXTRA_MATCH_LIMIT;
      pre->pcre_extra->match_limit = match_limit;
    }

    if (match_limit_recursion > 0) {
      if (pre->pcre_extra == NULL) {
        pre->pcre_extra = pcalloc(pre->regex_pool, sizeof(pcre_extra));
      }

      pre->pcre_extra->flags |= PCRE_EXTRA_MATCH_LIMIT_RECURSION;
      pre->pcre_extra->match_limit_recursion = match_limit_recursion;
    }

    pr_trace_msg(trace_channel, 9,
      "executing PCRE regex '%s' against subject '%s'",
      pr_regexp_get_pattern(pre), str);
    res = pcre_exec(pre->pcre, pre->pcre_extra, str, str_len, 0, flags,
      NULL, 0);

    if (res < 0) {
      if (pr_trace_get_level(trace_channel) >= 9) {
        const char *reason = "unknown";

        switch (res) {
          case PCRE_ERROR_NOMATCH:
            reason = "subject did not match pattern";
            break;

          case PCRE_ERROR_NULL:
            reason = "null regex or subject";
            break;

          case PCRE_ERROR_BADOPTION:
            reason = "unsupported options bit";
            break;

          case PCRE_ERROR_BADMAGIC:
            reason = "bad magic number in regex";
            break;

          case PCRE_ERROR_UNKNOWN_OPCODE:
          case PCRE_ERROR_INTERNAL:
            reason = "internal PCRE error or corrupted regex";
            break;

          case PCRE_ERROR_NOMEMORY:
            reason = "not enough memory for backreferences";
            break;

          case PCRE_ERROR_MATCHLIMIT:
            reason = "match limit reached/exceeded";
            break;

          case PCRE_ERROR_RECURSIONLIMIT:
            reason = "match limit recursion reached/exceeded";
            break;

          case PCRE_ERROR_BADUTF8:
            reason = "invalid UTF8 subject used";
            break;

          case PCRE_ERROR_PARTIAL:
            reason = "subject matched only partially; PCRE_PARTIAL flag not used";
            break;
        }

        pr_trace_msg(trace_channel, 9,
          "PCRE regex '%s' failed to match subject '%s': %s",
          pr_regexp_get_pattern(pre), str, reason);

      } else {
        pr_trace_msg(trace_channel, 9,
          "PCRE regex '%s' successfully match subject '%s'",
          pr_regexp_get_pattern(pre), str);
      }
    }

    return res;
  }

  errno = EINVAL;
  return -1;
}
Пример #3
0
int proxy_db_open(pool *p, const char *table_path, const char *schema_name) {
  int res;
  pool *tmp_pool;
  const char *stmt;

  if (p == NULL ||
      table_path == NULL) {
    errno = EINVAL;
    return -1;
  }

  /* If we already have a database handle open, then attach the given
   * path to our handle.  Otherwise, open/create the database file first.
   */

  if (proxy_dbh == NULL) {
    res = sqlite3_open(table_path, &proxy_dbh);
    if (res != SQLITE_OK) {
      pr_log_debug(DEBUG0, MOD_PROXY_VERSION
        ": error opening SQLite database '%s': %s", table_path,
        sqlite3_errmsg(proxy_dbh));
      errno = EPERM;
      return -1;
    }

    /* Tell SQLite to only use in-memory journals.  This is necessary for
     * working properly when a chroot is used.  Note that the MEMORY journal
     * mode of SQLite is supported only for SQLite-3.6.5 and later.
     */
    res = sqlite3_exec(proxy_dbh, "PRAGMA journal_mode = MEMORY;", NULL, NULL,
      NULL);
    if (res != SQLITE_OK) {
      pr_trace_msg(trace_channel, 2,
        "error setting MEMORY journal mode on SQLite database '%s': %s",
        table_path, sqlite3_errmsg(proxy_dbh));
    }

    if (pr_trace_get_level(trace_channel) >= PROXY_DB_SQLITE_TRACE_LEVEL) {
      sqlite3_trace(proxy_dbh, db_trace, NULL);
    }

    prepared_stmts = pr_table_nalloc(db_pool, 0, 4);
  }

  tmp_pool = make_sub_pool(p);

  stmt = pstrcat(tmp_pool, "ATTACH DATABASE '", table_path, "' AS ",
    schema_name, ";", NULL);
  res = sqlite3_exec(proxy_dbh, stmt, NULL, NULL, NULL);
  if (res != SQLITE_OK) {
    pr_trace_msg(trace_channel, 2,
      "error attaching database '%s' (as '%s') to existing SQLite handle "
      "using '%s': %s", table_path, schema_name, stmt,
      sqlite3_errmsg(proxy_dbh));
    destroy_pool(tmp_pool);
    errno = EPERM;
    return -1;
  }

  destroy_pool(tmp_pool);
  return 0;
}