int rpserver_db_authenticate(dbi_conn db, const char *user, const char *password)
{
  int retval = AUTH_UNKNOWN_USER;
  dbi_result result;

  LOG(LOG_INFO, "%s():%d - username: %s  password: %s", __FUNCTION__, __LINE__, user, password);

  return 0;

  result = dbi_conn_queryf(db, 
                           "SELECT * FROM `users` WHERE `username` = '%s' AND `password` = MD5('%s')",
                           user, password);

  LOG(LOG_INFO, "%s():%d - SELECT * FROM `users` WHERE `username` = '%s' AND `password` = '%s'", 
         __FUNCTION__, __LINE__, user, password);

  if (result) {
    if( dbi_result_get_numrows(result) == 1 ) {
      while (dbi_result_next_row(result)) {
          LOG(LOG_INFO, "%s():%d - User \"%s\" is known and ACTIVE.",
                 __FUNCTION__, __LINE__, user);
          retval = AUTH_VALID;
      } 
    } else {
      LOG(LOG_INFO, "%s():%d - User \"%s\" is unknown",
             __FUNCTION__, __LINE__, user);
    }
  }
  dbi_result_free(result);

  return retval;
}
Example #2
0
int dbiw_res_num_rows(db_wrap_result * self, size_t *num)
{
	RES_DECL(DB_WRAP_E_BAD_ARG);
	if (! num) return DB_WRAP_E_BAD_ARG;
	*num = dbi_result_get_numrows(dbires);
	return 0;
}
Example #3
0
bool swd::database::is_flooding(const std::string& client_ip,
 const int& profile_id) {
    ensure_connection();

    boost::unique_lock<boost::mutex> scoped_lock(dbi_mutex_);

    char *client_ip_esc = strdup(client_ip.c_str());
    dbi_conn_quote_string(conn_, &client_ip_esc);

    dbi_result res = dbi_conn_queryf(conn_, "SELECT is_flooding(%i, %s) AS result",
     profile_id, client_ip_esc);

    free(client_ip_esc);

    if (!res) {
        throw swd::exceptions::database_exception("Can't execute request count query");
    }

    bool flooding = false;

    if (dbi_result_get_numrows(res) == 1) {
        if (!dbi_result_next_row(res)) {
            throw swd::exceptions::database_exception("No flooding?");
        }

        flooding = (dbi_result_get_uint(res, "result") == 1);
    }

    dbi_result_free(res);

    return flooding;
}
Example #4
0
swd::profile_ptr swd::database::get_profile(const std::string& server_ip,
 const int& profile_id) {
    std::stringstream log_message;
    log_message << "Get profile from db -> server_ip: " << server_ip
     << "; profile_id: " << profile_id;

    swd::log::i()->send(swd::notice, log_message.str());

    /* Test the database connection status. Tries to reconnect if disconnected. */
    ensure_connection();

    /* Mutex to avoid race conditions. */
    boost::unique_lock<boost::mutex> scoped_lock(dbi_mutex_);

    /**
     * First we escape server_ip. It comes from a trusted source, but better safe
     * than sorry. This does not work with std::string though.
     */
    char *server_ip_esc = strdup(server_ip.c_str());
    dbi_conn_quote_string(conn_, &server_ip_esc);

    /* Insert the ip and execute the query. */
    dbi_result res = dbi_conn_queryf(conn_, "SELECT id, hmac_key, mode, "
     "whitelist_enabled, blacklist_enabled, integrity_enabled, flooding_enabled, "
     "blacklist_threshold, cache_outdated FROM profiles WHERE %s LIKE "
     "prepare_wildcard(server_ip) AND id = %i", server_ip_esc, profile_id);

    /* Don't forget to free server_ip_esc to avoid a memory leak. */
    free(server_ip_esc);

    if (!res) {
        throw swd::exceptions::database_exception("Can't execute profile query");
    }

    if (dbi_result_get_numrows(res) != 1) {
        throw swd::exceptions::database_exception("Can't get profile");
    }

    if (!dbi_result_next_row(res)) {
        throw swd::exceptions::database_exception("No profile?");
    }

    swd::profile_ptr profile(new swd::profile());
    profile->set_server_ip(server_ip),
    profile->set_id(dbi_result_get_uint(res, "id"));
    profile->set_mode(dbi_result_get_uint(res, "mode"));
    profile->set_whitelist_enabled(dbi_result_get_uint(res, "whitelist_enabled") == 1);
    profile->set_blacklist_enabled(dbi_result_get_uint(res, "blacklist_enabled") == 1);
    profile->set_integrity_enabled(dbi_result_get_uint(res, "integrity_enabled") == 1);
    profile->set_flooding_enabled(dbi_result_get_uint(res, "flooding_enabled") == 1);
    profile->set_key(dbi_result_get_string(res, "hmac_key"));
    profile->set_blacklist_threshold(dbi_result_get_uint(res, "blacklist_threshold"));
    profile->set_cache_outdated(dbi_result_get_uint(res, "cache_outdated") == 1);

    dbi_result_free(res);

    return profile;
}
Example #5
0
int dbi_result_has_next_row(dbi_result Result) {
  if (!RESULT) {
    _error_handler(/*RESULT->conn*/ NULL, DBI_ERROR_BADPTR);
    return 0;
  }	

  return RESULT->result_state != NOTHING_RETURNED
  		 && RESULT->currowidx < dbi_result_get_numrows(Result);
}
Example #6
0
/* This is used too early to call GncDbiProvider::get_table_list(). */
static bool
save_may_clobber_data (dbi_conn conn, const std::string& dbname)
{

    /* Data may be clobbered iff the number of tables != 0 */
    auto result = dbi_conn_get_table_list (conn, dbname.c_str(), nullptr);
    bool retval = false;
    if (result)
    {
        retval =  dbi_result_get_numrows (result) > 0;
        dbi_result_free (result);
    }
    return retval;
}
Example #7
0
/////////////////////////////////////////////////////////////////////////////////
//
// Function:
//
// Purpose:
//
// Parameters:
//
// Return value:
//
// Author: Komatsu Yuji(Zheng Chuyu)
//
/////////////////////////////////////////////////////////////////////////////////
int dateconds_execute(const apr_uint64_t proc_jobunit_id,
                      const time_t base_time, const int beginning_of_week)
{
    int rc, rv;
    dbi_result result;
    datecond_t *obj = NULL;
    struct tm *tm = NULL;

    jhklog_trace
        ("In %s() proc_jobunit_id: %llu, base_time: %ld, beginning_of_week: %d",
         __func__, proc_jobunit_id, base_time, beginning_of_week);

    result =
        jhkdb_select
        ("SELECT * FROM proc_dateconds WHERE proc_jobunit_id = %llu",
         proc_jobunit_id);
    if (result == NULL)
        return -1;

    // unset date conditions
    if (dbi_result_get_numrows(result) == 0) {
        dbi_result_free(result);
        return CONNECTOR_KIND_NORMAL;
    }
    // init
    rc = -1;
    obj = datecond_new();

    // date condition
    while (dbi_result_next_row(result)) {
        datecond_load(obj, result);
        rv = datecond_execute(obj, base_time, beginning_of_week);
        if (rv == JHAKO_FALSE)
            continue;

        if (rv == JHAKO_TRUE) {
            rc = CONNECTOR_KIND_NORMAL;
        } else {
            rc = CONNECTOR_KIND_ERROR;
        }
        goto finish;
    }

    rc = CONNECTOR_KIND_BRANCH;
  finish:
    datecond_destroy(obj);
    dbi_result_free(result);
    return rc;
}
Example #8
0
/////////////////////////////////////////////////////////////////////////////////
//
// Function:
//
// Purpose:
//
// Parameters:
//
// Return value:
//
// Author: Komatsu Yuji(Zheng Chuyu)
//
/////////////////////////////////////////////////////////////////////////////////
int winjob_load_one(winjob_t * obj, dbi_result res)
{
    apr_uint64_t num;

    jhklog_trace("In %s()", __func__);
    if (obj == NULL || res == NULL)
        return -1;

    num = dbi_result_get_numrows(res);
    if (num != 1) {
        jhklog_warn("In %s() can not find winjob. numrows: %llu",
                    __func__, num);
        return 1;
    }

    while (dbi_result_next_row(res)) {
        winjob_load(obj, res);
        break;
    }

    return 0;
}
Example #9
0
//*******************************************************************
// retrieve the definitions + status
// get it from the cache. if there but too old: delete
// If a probe definition does not exist, it will be created.
// in case of mysql-has-gone-away type errors, we keep on running, 
// it will be caught later-on.
//*******************************************************************
void *bb_cpu_get_def(trx *t, int create)
{
  struct probe_def *def;
  struct bb_cpu_result *res = (struct bb_cpu_result *)t->res;
  dbi_result result;
  time_t now = time(NULL);

  if (res->color != STAT_PURPLE && res->server == 0) { 
    // first we find the serverid, this will be used to find the probe definition in the hashtable
    result = db_query(t->probe->db, 0, query_server_by_name, res->hostname, res->hostname, 
                      res->hostname, res->hostname, res->hostname);
    if (!result) {
      return(NULL);
    }
    if (dbi_result_next_row(result)) {
      res->server   = dbi_result_get_int_idx(result, 0);
    } else {
      LOG(LOG_NOTICE, "%s:%u@%s: server %s not found", res->realm, res->stattime, t->fromhost, res->hostname);
      dbi_result_free(result);
      return(NULL);
    }
    dbi_result_free(result);
  }

  // look in the cache for the def
  def = g_hash_table_lookup(t->probe->cache, &res->server);
  if (def && def->stamp < now - (120 + uw_rand(240))) { // older then 2 - 6 minutes?
     g_hash_table_remove(t->probe->cache, &res->server);
     def = NULL;
  }
  // if not there retrieve from database and insert in hash
  if (def == NULL) {
    def = g_malloc0(t->probe->def_size);
    def->stamp    = time(NULL);
    def->server   = res->server;
    def->pgroup   = 1;
    strcpy(def->hide, "no");

    // first find the definition based on the serverid
    result = db_query(t->probe->db, 0, "select id, yellow, red, contact, hide, email, sms, delay "
                                    "from pr_%s_def where server = '%u'", 
                      res->name, res->server);
    if (!result) {
      g_free(def);
      return(NULL);
    }
    if (dbi_result_get_numrows(result) == 0) { // DEF RECORD NOT FOUND
      char sequence[40];
      // no def record found? Create one. 
      dbi_result_free(result);
      result = db_query(t->probe->db, 0, 
                        "insert into pr_%s_def (server, description) values ('%u', '%s')", 
                         res->name, res->server, res->hostname);
      dbi_result_free(result);
      sprintf(sequence, "pr_%s_def_id_seq", res->name);
      def->probeid = dbi_conn_sequence_last(t->probe->db, sequence);
      LOG(LOG_NOTICE, "%s:%u@%s: pr_%s_def created for %s, id = %u", 
          res->realm, res->stattime, t->fromhost, res->name, res->hostname, def->probeid);
      result = db_query(t->probe->db, 0, "select id, yellow, red, contact, hide, email, sms, delay "
                                      "from pr_%s_def where id = '%u'", 
                        res->name, def->probeid);
    }
    if (!dbi_result_next_row(result)) {
      LOG(LOG_NOTICE, "%s:%u@%s: no pr_%s_def found for server %u - skipped", 
          res->realm, res->stattime, t->fromhost, res->name, res->server);
      dbi_result_free(result);
      g_free(def);
      return(NULL);
    }

    def->probeid = dbi_result_get_int(result, "id");
    def->yellow  = dbi_result_get_float(result, "yellow");
    def->red     = dbi_result_get_float(result, "red");
    def->contact = dbi_result_get_int(result, "contact");
    strcpy(def->hide, dbi_result_get_string_default(result, "hide", "no"));
    strcpy(def->email, dbi_result_get_string_default(result, "email", ""));
    strcpy(def->sms, dbi_result_get_string_default(result, "sms", ""));
    def->delay   = dbi_result_get_int(result, "delay");

    dbi_result_free(result);
    result = db_query(t->probe->db, 0, 
                      "select color "
                      "from   pr_status "
                      "where  class = '%u' and probe = '%u'", t->probe->class, def->probeid);
    if (result) {
      if (dbi_result_next_row(result)) {
        def->color   = dbi_result_get_int(result, "color");
      } else {
        LOG(LOG_NOTICE, "%s:%u@%s: pr_status record for %s id %u not found", 
           res->realm, res->stattime, t->fromhost, res->name, def->probeid);
      }
      dbi_result_free(result);
    }
    if (!def->color) def->color = res->color;

    result = db_query(t->probe->db, 0, 
                      "select stattime from pr_%s_raw use index(probstat) "
                      "where probe = '%u' order by stattime desc limit 1",
                       res->name, def->probeid);
    if (result) {
      if (dbi_result_next_row(result)) {
        def->newest = dbi_result_get_int(result, "stattime");
      }
      dbi_result_free(result);
    }
    g_hash_table_insert(t->probe->cache, guintdup(def->server), def);
  }
Example #10
0
//*******************************************************************
// SUMMARIZE A TABLE INTO AN OLDER PERIOD
//*******************************************************************
static void mysqlstats_summarize(trx *t, char *from, char *into, guint slot, guint slotlow, guint slothigh, gint resummarize)
{
  dbi_result result;
  struct probe_def *def = (struct probe_def *)t->def;
  float avg_yellow, avg_red;
  int avg_selectq;
  int avg_insertq;
  int avg_updateq;
  int avg_deleteq;
  guint stattime;
  guint max_color;

  stattime = slotlow + ((slothigh-slotlow)/2);

  result = db_query(t->probe->db, 0,
                    "select avg(selectq) as avg_selectq, avg(insertq) as avg_insertq, avg(updateq) as avg_updateq, "
                    "       avg(deleteq) as avg_deleteq, max(color) as max_color, avg(yellow) as avg_yellow, avg(red) as avg_red"
                    "from   pr_mysqlstats_%s use index(probstat) "
                    "where  probe = '%d' and stattime >= %d and stattime < %d",
                    from, def->probeid, slotlow, slothigh);

  if (!result) return;
  if (dbi_result_get_numrows(result) == 0) { // no records found
    LOG(LOG_NOTICE, "nothing to summarize from %s for probe %u %u %u",
                       from, def->probeid, slotlow, slothigh);
    dbi_result_free(result);
    return;
  }

  if (!dbi_result_next_row(result)) {
    dbi_result_free(result);
    return;
  }
  if (dbi_result_field_is_null_idx(result, 0)) {
    LOG(LOG_NOTICE, "nothing to summarize from %s for probe %u %u %u", 
                       from, def->probeid, slotlow, slothigh);
    dbi_result_free(result);
    return;
  }

  avg_selectq = dbi_result_get_int(result, "avg_selectq");
  avg_insertq = dbi_result_get_int(result, "avg_insertq");
  avg_updateq = dbi_result_get_int(result, "avg_updateq");
  avg_deleteq = dbi_result_get_int(result, "avg_deleteq");
  max_color   = dbi_result_get_int(result, "max_color");
  avg_yellow  = dbi_result_get_float(result, "avg_yellow");
  avg_red     = dbi_result_get_float(result, "avg_red");
  dbi_result_free(result);

  if (resummarize) {
    // delete old values
    result = db_query(t->probe->db, 0,
                    "delete from pr_mysqlstats_%s where probe = '%u' and stattime = '%u'",
                    into, def->probeid, stattime);
    dbi_result_free(result);
  }

  result = db_query(t->probe->db, 0,
                    "insert into pr_mysqlstats_%s (selectq, insertq, updateq, deleteq, probe, color, stattime, "
                    "                              yellow, red, slot) "
                    "            values ('%u', '%u', '%u', '%u', '%d', '%u', '%d', "
                    "                    '%f', '%f', '%u')",
                    into,  avg_selectq, avg_insertq, avg_updateq, avg_deleteq, def->probeid, 
                    max_color, stattime, avg_yellow, avg_red, slot);

  dbi_result_free(result);
}
Example #11
0
//*******************************************************************
// SUMMARIZE A TABLE INTO AN OLDER PERIOD
//*******************************************************************
static void bb_cpu_summarize(trx *t, char *from, char *into, guint slot, guint slotlow, guint slothigh, gint resummarize)
{
  dbi_result result;
  struct probe_def *def = (struct probe_def *)t->def;
  float avg_yellow, avg_red;
  gfloat avg_loadavg;
  guint avg_user, avg_system, avg_idle;
  guint avg_swapped, avg_free, avg_buffered, avg_cached, avg_used;
  guint stattime;
  guint max_color;

  stattime = slotlow + ((slothigh-slotlow)/2);

  result = db_query(t->probe->db, 0,
                    "select avg(loadavg) as avg_loadavg, avg(user) as avg_user, avg(system) as avg_system, "
                    "       avg(idle) as avg_idle, avg(swapped) as avg_swapped, avg(free) as avg_free, "
                    "       avg(buffered) as avg_buffered, avg(cached) as avg_cached, avg(used) as avg_used, "
                    "       max(color) as max_color, avg(yellow) as avg_yellow, avg(red) as avg_red " 
                    "from   pr_bb_cpu_%s use index(probstat) "
                    "where  probe = '%d' and stattime >= %d and stattime < %d",
                    from, def->probeid, slotlow, slothigh);
  
  if (!result) return;
  if (dbi_result_get_numrows(result) == 0) { // no records found
    LOG(LOG_NOTICE, "nothing to summarize from %s for probe %u %u %u",
                       from, def->probeid, slotlow, slothigh);
    dbi_result_free(result);
    return;
  }

  if (!dbi_result_next_row(result)) {
    dbi_result_free(result);
    return;
  }
  if (dbi_result_field_is_null_idx(result, 0)) {
    LOG(LOG_NOTICE, "nothing to summarize from %s for probe %u %u %u", 
                       from, def->probeid, slotlow, slothigh);
    dbi_result_free(result);
    return;
  }

  avg_loadavg = dbi_result_get_float(result, "avg_loadavg");
  avg_user    = dbi_result_get_int(result, "avg_user");
  avg_system  = dbi_result_get_int(result, "avg_system");
  avg_idle    = dbi_result_get_int(result, "avg_idle");
  avg_swapped = dbi_result_get_int(result, "avg_swapped");
  avg_free    = dbi_result_get_int(result, "avg_free");
  avg_buffered= dbi_result_get_int(result, "avg_buffered");
  avg_cached  = dbi_result_get_int(result, "avg_cached");
  avg_used    = dbi_result_get_int(result, "avg_used");
  max_color   = dbi_result_get_int(result, "max_color");
  avg_yellow  = dbi_result_get_float(result, "avg_yellow");
  avg_red     = dbi_result_get_float(result, "avg_red");
  dbi_result_free(result);

  if (resummarize) {
    // delete old values
    result = db_query(t->probe->db, 0,
                    "delete from pr_bb_cpu_%s where probe = '%u' and stattime = '%u'",
                    into, def->probeid, stattime);
    dbi_result_free(result);
  }
  result = db_query(t->probe->db, 0,
                    "insert into pr_bb_cpu_%s (loadavg, user, system, idle, swapped, free, buffered, cached, " 
                    "                          used, probe, color, stattime, yellow, red, slot) "
                    "            values ('%f', '%u', '%u', '%u', '%u', '%u', '%u', '%u', "
                    "                    '%u', '%d', '%u', '%d', '%f', '%f', '%u')",
                    into, 
                    avg_loadavg, avg_user, avg_system, avg_idle, 
                    avg_swapped, avg_free, avg_buffered, avg_cached, 
                    avg_used, def->probeid, max_color, stattime,
                    avg_yellow, avg_red, slot);
  dbi_result_free(result);
}
Example #12
0
int dbi_result_last_row(dbi_result Result) {
  return dbi_result_seek_row(Result, dbi_result_get_numrows(Result));
}
Example #13
0
//*******************************************************************
// retrieve the definitions + status
// get it from the cache. if there but too old: delete
// If a probe definition does not exist, it will be created.
// in case of mysql-has-gone-away type errors, we keep on running, 
// it will be caught later-on.
//*******************************************************************
void *bb_get_def(trx *t, int create)
{
  struct probe_def *def;
  struct bb_result *res = (struct bb_result *)t->res;
  dbi_result result;

  def = g_malloc0(t->probe->def_size);
  def->stamp    = time(NULL);
  def->server   = res->server;
  def->pgroup   = 1;
  strcpy(def->hide, "no");

  if (res->color == STAT_PURPLE && res->probeid) {
    // find the definition based on the probe id
    result = db_query(t->probe->db, 0,
                      "select id, contact, hide, email, sms, delay from pr_bb_def "
                      "where  id = '%u'", res->probeid);
    if (!result) {
      g_free(def);
      return(NULL);
    }
  } else {
    if (res->server == 0) {
      // first we find the serverid, this will be used to find the probe definition in the database
      if (!query_server_by_name) {
        LOG(LOG_WARNING, "%s:%u@%s: don't know how to find %s by name", 
            res->realm, res->stattime, t->fromhost, res->hostname);
        g_free(def);
        return(NULL);
      }
      result = db_query(t->probe->db, 0, query_server_by_name, res->hostname, res->hostname, 
                        res->hostname, res->hostname, res->hostname);
      if (!result) {
        g_free(def);
        return(NULL);
      }
      if (dbi_result_next_row(result)) {
        res->server   = dbi_result_get_int_idx(result, 0);
      } else {
        LOG(LOG_WARNING, "%s:%u@%s: server %s not found", res->realm, res->stattime, t->fromhost, res->hostname);
        dbi_result_free(result);
        g_free(def);
        return(NULL);
      }
      dbi_result_free(result);
    }

    // first find the definition based on the serverid
    result = db_query(t->probe->db, 0,
                      "select id, contact, hide, email, sms, delay from pr_bb_def "
                      "where  bbname = '%s' and server = '%u'", res->bbname, res->server);
    if (!result) {
      g_free(def);
      return(NULL);
    }
  }
  if (dbi_result_get_numrows(result) == 0) { // DEF RECORD NOT FOUND
    char sequence[40];
    dbi_result_free(result);
    result = db_query(t->probe->db, 0,
                      "insert into pr_%s_def (server, ipaddress, description, bbname) "
                      "            values ('%u', '%s', '%s', '%s')", 
                      res->name, res->server, res->ipaddress ? res->ipaddress : "", 
                      res->hostname, res->bbname);
    dbi_result_free(result);
    sprintf(sequence, "pr_%s_def_id_seq", res->name);
    def->probeid  = dbi_conn_sequence_last(t->probe->db, sequence);
    LOG(LOG_NOTICE, "%s:%u@%s: pr_bb_def %s created for %s, id = %u", 
        res->realm, res->stattime, t->fromhost, res->bbname, res->hostname, def->probeid);
    result = db_query(t->probe->db, 0,
                    "select id, contact, hide, email, sms, delay from pr_bb_def "
                    "where  bbname = '%s' and server = '%u'", res->bbname, res->server);
    if (!result) return(NULL);
  }
  if (!dbi_result_next_row(result)) {
    LOG(LOG_NOTICE, "%s:%u@%s: no pr_%s_def found for server %u - skipped", 
         res->realm, res->stattime, t->fromhost, res->name, res->server);
    dbi_result_free(result);
    return(NULL);
  }
  def->probeid = dbi_result_get_int(result, "id");
  def->contact = dbi_result_get_int(result, "contact");
  strcpy(def->hide, dbi_result_get_string_default(result, "hide", "no"));
  strcpy(def->email, dbi_result_get_string_default(result, "email", ""));
  strcpy(def->sms, dbi_result_get_string_default(result, "sms", ""));
  def->delay = dbi_result_get_int(result, "delay");

  dbi_result_free(result);

  // definition found, get the pr_status
  result = db_query(t->probe->db, 0,
                    "select color, stattime "
                    "from   pr_status "
                    "where  class = '%u' and probe = '%u'", t->probe->class, def->probeid);
  if (result) {
    if (dbi_result_next_row(result)) {
      def->color   = dbi_result_get_int(result, "color");
      def->newest  = dbi_result_get_int(result, "stattime");
    } else {
      LOG(LOG_NOTICE, "%s:%u@%s: pr_status record for %s id %u (server %s) not found", 
                       res->realm, res->stattime, t->fromhost, res->name, def->probeid, res->hostname);
    }
    dbi_result_free(result);
  }
  if (!def->color) def->color = res->color;
  res->probeid = def->probeid;

  return(def);
}
Example #14
0
static ftpd_chroot_status_t ftpd_dbi_map_chroot(const request_rec *r,
        const char **ret_chroot,
        const char **ret_initroot)
{
    ftpd_chroot_status_t ARV = FTPD_CHROOT_USER_NOT_FOUND;
    ftpd_dbi_config *conf;
    const char *query;
    const char *chroot;
    ftpd_dbi_rest *dbi_res;
    dbi_result result;
    ftpd_dbi_dconfig *dconf = ap_get_module_config(r->per_dir_config,
                              &ftpd_dbi_module);


    conf = apr_hash_get(ftpd_dbi_config_hash, dconf->id, APR_HASH_KEY_STRING);
    if (conf == NULL) {
        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
                      "[mod_ftpd_dbi.c] - Server Config for \"%s\" was not found",
                      dconf->id);
        return FTPD_CHROOT_FAIL;
    }

    ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
                  "[mod_ftpd_dbi.c] Attempting to Acquire DBI Connection");
    apr_reslist_acquire(conf->pool, (void **) &dbi_res);

    /* make the query to get the user's password */
    if (conf->rec.isactive_field) {
        if (conf->rec.chroot_query == NULL) {
            query =
                "SELECT &{ChrootField} FROM &{Table} WHERE &{UsernameField}=&{GivenUsername} AND &{IsActiveField}!=0 LIMIT 0,1";
        }
        else {
            query = conf->rec.chroot_query;
        }
    }
    else {
        if (conf->rec.chroot_query == NULL) {
            query =
                "SELECT &{ChrootField} FROM &{Table} WHERE &{UsernameField}=&{GivenUsername} LIMIT 0,1";
        }
        else {
            query = conf->rec.chroot_query;
        }
    }
    /* perform the query */

    if ((query =
                populate_querystring(r, query, conf, dconf, dbi_res, r->user))
            && safe_dbi_query(dbi_res, &result, r, query) == 0) {
        /* store the query result */
        if (dbi_result_next_row(result)
                && dbi_result_get_numrows(result) == 1) {

            chroot =
                dbi_result_get_string_copy(result, conf->rec.chroot_field);
            if ((chroot == NULL) || (strcmp(chroot, "ERROR") == 0)) {
                ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
                              "[mod_ftpd_dbi.c] - libdbi returned an error when retrieving the chroot.");
                ARV = FTPD_CHROOT_FAIL;
            }
            else {
                // XXXX: Do more checks of the chroot here!
                *ret_chroot = apr_pstrdup(r->pool, chroot);
                ARV = FTPD_CHROOT_USER_FOUND;
            }
        }
        else {
            if (dbi_result_get_numrows(result) == 0) {
                ARV = FTPD_CHROOT_USER_NOT_FOUND;
            }
            else {
                ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
                              "[mod_ftpd_dbi.c] %lu row(s) was not returned by dbi_result_get_numrows(result)",
                              (unsigned long) dbi_result_get_numrows(result));
                ARV = FTPD_CHROOT_FAIL;
            }
        }
        dbi_result_free(result);
    }
    else {
        ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
                      "[mod_ftpd_dbi.c] Query Failed!");
        ARV = FTPD_CHROOT_FAIL;
    }

    safe_dbi_rel_server(conf->pool, dbi_res, r);
    return ARV;
}