Call finish_select_query if we experience an error retrieving the result
[freeradius.git] / src / modules / rlm_sql / rlm_sql.c
index afa86b7..c703658 100644 (file)
@@ -44,7 +44,7 @@ RCSID("$Id$")
 static const CONF_PARSER query_config[] = {
        { "query", FR_CONF_OFFSET(PW_TYPE_STRING | PW_TYPE_XLAT | PW_TYPE_MULTI, rlm_sql_config_t, accounting.query), NULL },
 
-       {NULL, -1, 0, NULL, NULL}
+       CONF_PARSER_TERMINATOR
 };
 
 /*
@@ -58,7 +58,7 @@ static const CONF_PARSER type_config[] = {
        { "interim-update", FR_CONF_POINTER(PW_TYPE_SUBSECTION, NULL), (void const *) query_config },
        { "stop", FR_CONF_POINTER(PW_TYPE_SUBSECTION, NULL), (void const *) query_config },
 
-       {NULL, -1, 0, NULL, NULL}
+       CONF_PARSER_TERMINATOR
 };
 
 static const CONF_PARSER acct_config[] = {
@@ -67,7 +67,7 @@ static const CONF_PARSER acct_config[] = {
 
        { "type", FR_CONF_POINTER(PW_TYPE_SUBSECTION, NULL), (void const *) type_config },
 
-       {NULL, -1, 0, NULL, NULL}
+       CONF_PARSER_TERMINATOR
 };
 
 static const CONF_PARSER postauth_config[] = {
@@ -75,14 +75,13 @@ static const CONF_PARSER postauth_config[] = {
        { "logfile", FR_CONF_OFFSET(PW_TYPE_STRING | PW_TYPE_XLAT, rlm_sql_config_t, postauth.logfile), NULL },
 
        { "query", FR_CONF_OFFSET(PW_TYPE_STRING | PW_TYPE_XLAT | PW_TYPE_MULTI, rlm_sql_config_t, postauth.query), NULL },
-
-       {NULL, -1, 0, NULL, NULL}
+       CONF_PARSER_TERMINATOR
 };
 
 static const CONF_PARSER module_config[] = {
        { "driver", FR_CONF_OFFSET(PW_TYPE_STRING, rlm_sql_config_t, sql_driver_name), "rlm_sql_null" },
-       { "server", FR_CONF_OFFSET(PW_TYPE_STRING, rlm_sql_config_t, sql_server), "localhost" },
-       { "port", FR_CONF_OFFSET(PW_TYPE_STRING, rlm_sql_config_t, sql_port), "" },
+       { "server", FR_CONF_OFFSET(PW_TYPE_STRING, rlm_sql_config_t, sql_server), "" }, /* Must be zero length so drivers can determine if it was set */
+       { "port", FR_CONF_OFFSET(PW_TYPE_INTEGER, rlm_sql_config_t, sql_port), "0" },
        { "login", FR_CONF_OFFSET(PW_TYPE_STRING, rlm_sql_config_t, sql_login), "" },
        { "password", FR_CONF_OFFSET(PW_TYPE_STRING | PW_TYPE_SECRET, rlm_sql_config_t, sql_password), "" },
        { "radius_db", FR_CONF_OFFSET(PW_TYPE_STRING, rlm_sql_config_t, sql_db), "radius" },
@@ -120,8 +119,7 @@ static const CONF_PARSER module_config[] = {
        { "accounting", FR_CONF_POINTER(PW_TYPE_SUBSECTION, NULL), (void const *) acct_config },
 
        { "post-auth", FR_CONF_POINTER(PW_TYPE_SUBSECTION, NULL), (void const *) postauth_config },
-
-       {NULL, -1, 0, NULL, NULL}
+       CONF_PARSER_TERMINATOR
 };
 
 /*
@@ -130,7 +128,7 @@ static const CONF_PARSER module_config[] = {
 static sql_fall_through_t fall_through(VALUE_PAIR *vp)
 {
        VALUE_PAIR *tmp;
-       tmp = pairfind(vp, PW_FALL_THROUGH, 0, TAG_ANY);
+       tmp = fr_pair_find_by_num(vp, PW_FALL_THROUGH, 0, TAG_ANY);
 
        return tmp ? tmp->vp_integer : FALL_THROUGH_DEFAULT;
 }
@@ -191,6 +189,7 @@ static ssize_t sql_xlat(void *instance, REQUEST *request, char const *query, cha
                numaffected = (inst->module->sql_affected_rows)(handle, inst->config);
                if (numaffected < 1) {
                        RDEBUG("SQL query affected no rows");
+                       (inst->driver->sql_finish_query)(handle, inst->config);
 
                        goto finish;
                }
@@ -228,7 +227,7 @@ static ssize_t sql_xlat(void *instance, REQUEST *request, char const *query, cha
 
        rcode = rlm_sql_fetch_row(inst, request, &handle);
        if (rcode) {
-               (inst->module->sql_finish_select_query)(handle, inst->config);
+               (inst->driver->sql_finish_select_query)(handle, inst->config);
                goto query_error;
        }
 
@@ -288,9 +287,17 @@ static int generate_sql_clients(rlm_sql_t *inst)
        if (rlm_sql_select_query(inst, NULL, &handle, inst->config->client_query) != RLM_SQL_OK) return -1;
 
        while ((rlm_sql_fetch_row(inst, NULL, &handle) == 0) && (row = handle->row)) {
+               int num_rows;
                char *server = NULL;
+
                i++;
 
+               num_rows = (inst->module->sql_num_fields)(handle, inst->config);
+               if (num_rows < 5) {
+                       WARN("SELECT returned too few rows.  Please do not edit 'client_query'");
+                       continue;
+               }
+
                /*
                 *  The return data for each row MUST be in the following order:
                 *
@@ -318,7 +325,7 @@ static int generate_sql_clients(rlm_sql_t *inst)
                        continue;
                }
 
-               if (((inst->module->sql_num_fields)(handle, inst->config) > 5) && (row[5] != NULL) && *row[5]) {
+               if ((num_rows > 5) && (row[5] != NULL) && *row[5]) {
                        server = row[5];
                }
 
@@ -371,7 +378,7 @@ static size_t sql_escape_func(UNUSED REQUEST *request, char *out, size_t outlen,
                /*
                 *      Allow all multi-byte UTF8 characters.
                 */
-               utf8_len = fr_utf8_char((uint8_t const *) in);
+               utf8_len = fr_utf8_char((uint8_t const *) in, -1);
                if (utf8_len > 1) {
                        if (outlen <= utf8_len) break;
 
@@ -495,16 +502,21 @@ int sql_set_user(rlm_sql_t *inst, REQUEST *request, char const *username)
                return -1;
        }
 
-       vp = pairalloc(request->packet, inst->sql_user);
+       vp = fr_pair_afrom_da(request->packet, inst->sql_user);
        if (!vp) {
                talloc_free(expanded);
                return -1;
        }
 
-       pairstrsteal(vp, expanded);
+       fr_pair_value_strsteal(vp, expanded);
        RDEBUG2("SQL-User-Name set to '%s'", vp->vp_strvalue);
        vp->op = T_OP_SET;
-       radius_pairmove(request, &request->packet->vps, vp, false);     /* needs to be pair move else op is not respected */
+
+       /*
+        *      Delete any existing SQL-User-Name, and replace it with ours.
+        */
+       fr_pair_delete_by_num(&request->packet->vps, vp->da->attr, vp->da->vendor, TAG_ANY);
+       fr_pair_add(&request->packet->vps, vp);
 
        return 0;
 }
@@ -512,7 +524,7 @@ int sql_set_user(rlm_sql_t *inst, REQUEST *request, char const *username)
 /*
  *     Do a set/unset user, so it's a bit clearer what's going on.
  */
-#define sql_unset_user(_i, _r) pairdelete(&_r->packet->vps, _i->sql_user->attr, _i->sql_user->vendor, TAG_ANY)
+#define sql_unset_user(_i, _r) fr_pair_delete_by_num(&_r->packet->vps, _i->sql_user->attr, _i->sql_user->vendor, TAG_ANY)
 
 static int sql_get_grouplist(rlm_sql_t *inst, rlm_sql_handle_t **handle, REQUEST *request,
                             rlm_sql_grouplist_t **phead)
@@ -584,6 +596,14 @@ static int sql_groupcmp(void *instance, REQUEST *request, UNUSED VALUE_PAIR *req
        rlm_sql_t *inst = instance;
        rlm_sql_grouplist_t *head, *entry;
 
+       /*
+        *      No group queries, don't do group comparisons.
+        */
+       if (!inst->config->groupmemb_query) {
+               RWARN("Cannot do group comparison when group_membership_query is not set");
+               return 1;
+       }
+
        RDEBUG("sql_groupcmp");
 
        if (check->vp_length == 0){
@@ -645,6 +665,19 @@ static rlm_rcode_t rlm_sql_process_groups(rlm_sql_t *inst, REQUEST *request, rlm
 
        rad_assert(request->packet != NULL);
 
+       if (!inst->config->groupmemb_query) {
+               RWARN("Cannot do check groups when group_membership_query is not set");
+
+       do_nothing:
+               *do_fall_through = FALL_THROUGH_DEFAULT;
+
+               /*
+                *      Didn't add group attributes or allocate
+                *      memory, so don't do anything else.
+                */
+               return RLM_MODULE_NOTFOUND;
+       }
+
        /*
         *      Get the list of groups this user is a member of
         */
@@ -656,10 +689,7 @@ static rlm_rcode_t rlm_sql_process_groups(rlm_sql_t *inst, REQUEST *request, rlm
        }
        if (rows == 0) {
                RDEBUG2("User not found in any groups");
-               rcode = RLM_MODULE_NOTFOUND;
-               *do_fall_through = FALL_THROUGH_DEFAULT;
-
-               goto finish;
+               goto do_nothing;
        }
        rad_assert(head);
 
@@ -669,9 +699,9 @@ static rlm_rcode_t rlm_sql_process_groups(rlm_sql_t *inst, REQUEST *request, rlm
         *      Add the Sql-Group attribute to the request list so we know
         *      which group we're retrieving attributes for
         */
-       sql_group = pairmake_packet("Sql-Group", NULL, T_OP_EQ);
+       sql_group = pair_make_request(inst->group_da->name, NULL, T_OP_EQ);
        if (!sql_group) {
-               REDEBUG("Error creating Sql-Group attribute");
+               REDEBUG("Error creating %s attribute", inst->group_da->name);
                rcode = RLM_MODULE_FAIL;
                goto finish;
        }
@@ -680,7 +710,7 @@ static rlm_rcode_t rlm_sql_process_groups(rlm_sql_t *inst, REQUEST *request, rlm
        do {
        next:
                rad_assert(entry != NULL);
-               pairstrcpy(sql_group, entry->name);
+               fr_pair_value_strcpy(sql_group, entry->name);
 
                if (inst->config->authorize_group_check_query) {
                        vp_cursor_t cursor;
@@ -710,9 +740,11 @@ static rlm_rcode_t rlm_sql_process_groups(rlm_sql_t *inst, REQUEST *request, rlm
                         */
                        if ((rows > 0) &&
                            (paircompare(request, request->packet->vps, check_tmp, &request->reply->vps) != 0)) {
-                               pairfree(&check_tmp);
+                               fr_pair_list_free(&check_tmp);
                                entry = entry->next;
 
+                               if (!entry) break;
+
                                goto next;      /* != continue */
                        }
 
@@ -774,7 +806,7 @@ static rlm_rcode_t rlm_sql_process_groups(rlm_sql_t *inst, REQUEST *request, rlm
 
 finish:
        talloc_free(head);
-       pairdelete(&request->packet->vps, PW_SQL_GROUP, 0, TAG_ANY);
+       fr_pair_delete_by_num(&request->packet->vps, inst->group_da->attr, 0, TAG_ANY);
 
        return rcode;
 }
@@ -784,7 +816,7 @@ static int mod_detach(void *instance)
 {
        rlm_sql_t *inst = instance;
 
-       if (inst->pool) fr_connection_pool_delete(inst->pool);
+       if (inst->pool) fr_connection_pool_free(inst->pool);
 
        /*
         *  We need to explicitly free all children, so if the driver
@@ -805,7 +837,7 @@ static int mod_detach(void *instance)
        return 0;
 }
 
-static int mod_instantiate(CONF_SECTION *conf, void *instance)
+static int mod_bootstrap(CONF_SECTION *conf, void *instance)
 {
        rlm_sql_t *inst = instance;
 
@@ -816,44 +848,74 @@ static int mod_instantiate(CONF_SECTION *conf, void *instance)
        inst->cs = conf;
 
        inst->name = cf_section_name2(conf);
-       if (!inst->name) {
-               inst->name = cf_section_name1(conf);
-       } else {
-               char *group_name;
-               DICT_ATTR const *da;
-               ATTR_FLAGS flags;
+       if (!inst->name) inst->name = cf_section_name1(conf);
 
-               /*
-                *      Allocate room for <instance>-SQL-Group
-                */
-               group_name = talloc_typed_asprintf(inst, "%s-SQL-Group", inst->name);
-               DEBUG("rlm_sql (%s): Creating new attribute %s",
-                     inst->name, group_name);
-
-               memset(&flags, 0, sizeof(flags));
-               if (dict_addattr(group_name, -1, 0, PW_TYPE_STRING, flags) < 0) {
-                       ERROR("rlm_sql (%s): Failed to create "
-                              "attribute %s: %s", inst->name, group_name,
-                              fr_strerror());
-                       return -1;
-               }
+       /*
+        *      Load the appropriate driver for our database.
+        *
+        *      We need this to check if the sql_fields callback is provided.
+        */
+       inst->handle = fr_dlopenext(inst->config->sql_driver_name);
+       if (!inst->handle) {
+               ERROR("Could not link driver %s: %s", inst->config->sql_driver_name, fr_strerror());
+               ERROR("Make sure it (and all its dependent libraries!) are in the search path of your system's ld");
+               return -1;
+       }
 
-               da = dict_attrbyname(group_name);
-               if (!da) {
-                       ERROR("rlm_sql (%s): Failed to create "
-                              "attribute %s", inst->name, group_name);
-                       return -1;
+       inst->module = (rlm_sql_module_t *) dlsym(inst->handle,  inst->config->sql_driver_name);
+       if (!inst->module) {
+               ERROR("Could not link symbol %s: %s", inst->config->sql_driver_name, dlerror());
+               return -1;
+       }
+
+       INFO("rlm_sql (%s): Driver %s (module %s) loaded and linked", inst->name,
+            inst->config->sql_driver_name, inst->module->name);
+
+       if (inst->config->groupmemb_query) {
+               if (cf_section_name2(conf)) {
+                       char buffer[256];
+
+                       snprintf(buffer, sizeof(buffer), "%s-SQL-Group", inst->name);
+
+                       if (paircompare_register_byname(buffer, dict_attrbyvalue(PW_USER_NAME, 0),
+                                                       false, sql_groupcmp, inst) < 0) {
+                               ERROR("Error registering group comparison: %s", fr_strerror());
+                               return -1;
+                       }
+
+                       inst->group_da = dict_attrbyname(buffer);
+
+                       /*
+                        *      We're the default instance
+                        */
+               } else {
+                       if (paircompare_register_byname("SQL-Group", dict_attrbyvalue(PW_USER_NAME, 0),
+                                                       false, sql_groupcmp, inst) < 0) {
+                               ERROR("Error registering group comparison: %s", fr_strerror());
+                               return -1;
+                       }
+
+                       inst->group_da = dict_attrbyname("SQL-Group");
                }
 
-               if (inst->config->groupmemb_query) {
-                       DEBUG("rlm_sql (%s): Registering sql_groupcmp for %s",
-                             inst->name, group_name);
-                       paircompare_register(da, dict_attrbyvalue(PW_USER_NAME, 0),
-                                            false, sql_groupcmp, inst);
+               if (!inst->group_da) {
+                       ERROR("Failed resolving group attribute");
+                       return -1;
                }
        }
 
-       rad_assert(inst->name);
+       /*
+        *      Register the SQL xlat function
+        */
+       xlat_register(inst->name, sql_xlat, sql_escape_func, inst);
+
+       return 0;
+}
+
+
+static int mod_instantiate(CONF_SECTION *conf, void *instance)
+{
+       rlm_sql_t *inst = instance;
 
        /*
         *      Complain if the strings exist, but are empty.
@@ -895,6 +957,12 @@ do { \
                        WARN("rlm_sql (%s): Ignoring authorize_group_check_query as group_membership_query "
                             "is not configured", inst->name);
                }
+
+               if (!inst->config->read_groups) {
+                       WARN("rlm_sql (%s): Ignoring read_groups as group_membership_query "
+                            "is not configured", inst->name);
+                       inst->config->read_groups = false;
+               }
        } /* allow the group check / reply queries to be NULL */
 
        /*
@@ -928,28 +996,6 @@ do { \
        inst->sql_select_query          = rlm_sql_select_query;
        inst->sql_fetch_row             = rlm_sql_fetch_row;
 
-       /*
-        *      Register the SQL xlat function
-        */
-       xlat_register(inst->name, sql_xlat, sql_escape_func, inst);
-
-       /*
-        *      Load the appropriate driver for our database
-        */
-       inst->handle = lt_dlopenext(inst->config->sql_driver_name);
-       if (!inst->handle) {
-               ERROR("Could not link driver %s: %s", inst->config->sql_driver_name, dlerror());
-               ERROR("Make sure it (and all its dependent libraries!) are in the search path of your system's ld");
-               return -1;
-       }
-
-       inst->module = (rlm_sql_module_t *) dlsym(inst->handle,
-                                                 inst->config->sql_driver_name);
-       if (!inst->module) {
-               ERROR("Could not link symbol %s: %s", inst->config->sql_driver_name, dlerror());
-               return -1;
-       }
-
        if (inst->module->mod_instantiate) {
                CONF_SECTION *cs;
                char const *name;
@@ -977,15 +1023,12 @@ do { \
                }
        }
 
-       inst->ef = exfile_init(inst, 64, 30);
+       inst->ef = exfile_init(inst, 256, 30, true);
        if (!inst->ef) {
                cf_log_err_cs(conf, "Failed creating log file context");
                return -1;
        }
 
-       INFO("rlm_sql (%s): Driver %s (module %s) loaded and linked", inst->name,
-            inst->config->sql_driver_name, inst->module->name);
-
        /*
         *      Initialise the connection pool for this instance
         */
@@ -994,11 +1037,6 @@ do { \
        inst->pool = fr_connection_pool_module_init(inst->cs, inst, mod_conn_create, NULL, NULL);
        if (!inst->pool) return -1;
 
-       if (inst->config->groupmemb_query) {
-               paircompare_register(dict_attrbyvalue(PW_SQL_GROUP, 0),
-                               dict_attrbyvalue(PW_USER_NAME, 0), false, sql_groupcmp, inst);
-       }
-
        if (inst->config->do_clients) {
                if (generate_sql_clients(inst) == -1){
                        ERROR("Failed to load clients from SQL");
@@ -1088,7 +1126,7 @@ static rlm_rcode_t mod_authorize(void *instance, REQUEST *request)
                RDEBUG2("User found in radcheck table");
                user_found = true;
                if (paircompare(request, request->packet->vps, check_tmp, &request->reply->vps) != 0) {
-                       pairfree(&check_tmp);
+                       fr_pair_list_free(&check_tmp);
                        check_tmp = NULL;
                        goto skipreply;
                }
@@ -1192,7 +1230,7 @@ skipreply:
                 *  Check for a default_profile or for a User-Profile.
                 */
                RDEBUG3("... falling-through to profile processing");
-               user_profile = pairfind(request->config, PW_USER_PROFILE, 0, TAG_ANY);
+               user_profile = fr_pair_find_by_num(request->config, PW_USER_PROFILE, 0, TAG_ANY);
 
                char const *profile = user_profile ?
                                      user_profile->vp_strvalue :
@@ -1251,8 +1289,8 @@ release:
        return rcode;
 
 error:
-       pairfree(&check_tmp);
-       pairfree(&reply_tmp);
+       fr_pair_list_free(&check_tmp);
+       fr_pair_list_free(&reply_tmp);
        sql_unset_user(inst, request);
 
        fr_connection_release(inst->pool, handle);
@@ -1479,10 +1517,11 @@ static rlm_rcode_t mod_checksimul(void *instance, REQUEST * request)
 
        /* If simul_count_query is not defined, we don't do any checking */
        if (!inst->config->simul_count_query) {
+               RWDEBUG("Simultaneous-Use checking requires 'simul_count_query' to be configured");
                return RLM_MODULE_NOOP;
        }
 
-       if ((!request->username) || (request->username->vp_length == '\0')) {
+       if ((!request->username) || (request->username->vp_length == 0)) {
                REDEBUG("Zero Length username not permitted");
 
                return RLM_MODULE_INVALID;
@@ -1507,7 +1546,7 @@ static rlm_rcode_t mod_checksimul(void *instance, REQUEST * request)
 
        if (rlm_sql_select_query(inst, request, &handle, expanded) != RLM_SQL_OK) {
                rcode = RLM_MODULE_FAIL;
-               goto finish;
+               goto release;   /* handle may no longer be valid */
        }
 
        ret = rlm_sql_fetch_row(inst, request, &handle);
@@ -1555,20 +1594,30 @@ static rlm_rcode_t mod_checksimul(void *instance, REQUEST * request)
         */
        request->simul_count = 0;
 
-       if ((vp = pairfind(request->packet->vps, PW_FRAMED_IP_ADDRESS, 0, TAG_ANY)) != NULL) {
+       if ((vp = fr_pair_find_by_num(request->packet->vps, PW_FRAMED_IP_ADDRESS, 0, TAG_ANY)) != NULL) {
                ipno = vp->vp_ipaddr;
        }
 
-       if ((vp = pairfind(request->packet->vps, PW_CALLING_STATION_ID, 0, TAG_ANY)) != NULL) {
+       if ((vp = fr_pair_find_by_num(request->packet->vps, PW_CALLING_STATION_ID, 0, TAG_ANY)) != NULL) {
                call_num = vp->vp_strvalue;
        }
 
        while (rlm_sql_fetch_row(inst, request, &handle) == 0) {
+               int num_rows;
+
                row = handle->row;
                if (!row) {
                        break;
                }
 
+               num_rows = (inst->module->sql_num_fields)(handle, inst->config);
+               if (num_rows < 8) {
+                       RDEBUG("Too few rows returned.  Please do not edit 'simul_verify_query'");
+                       rcode = RLM_MODULE_FAIL;
+
+                       goto finish;
+               }
+
                if (!row[2]){
                        RDEBUG("Cannot zap stale entry. No username present in entry");
                        rcode = RLM_MODULE_FAIL;
@@ -1609,7 +1658,7 @@ static rlm_rcode_t mod_checksimul(void *instance, REQUEST * request)
                                        else if (strcmp(row[7], "SLIP") == 0)
                                                proto = 'S';
                                }
-                               if (row[8])
+                               if ((num_rows > 8) && row[8])
                                        sess_time = atoi(row[8]);
                                session_zap(request, nas_addr, nas_port,
                                            row[2], row[1], framed_addr,
@@ -1681,29 +1730,22 @@ static rlm_rcode_t mod_post_auth(void *instance, REQUEST *request)
 /* globally exported name */
 extern module_t rlm_sql;
 module_t rlm_sql = {
-       RLM_MODULE_INIT,
-       "SQL",
-       RLM_TYPE_THREAD_SAFE,   /* type: reserved */
-       sizeof(rlm_sql_t),
-       module_config,
-       mod_instantiate,        /* instantiation */
-       mod_detach,             /* detach */
-       {
-               NULL,           /* authentication */
-               mod_authorize,  /* authorization */
-               NULL,           /* preaccounting */
+       .magic          = RLM_MODULE_INIT,
+       .name           = "sql",
+       .type           = RLM_TYPE_THREAD_SAFE,
+       .inst_size      = sizeof(rlm_sql_t),
+       .config         = module_config,
+       .bootstrap      = mod_bootstrap,
+       .instantiate    = mod_instantiate,
+       .detach         = mod_detach,
+       .methods = {
+               [MOD_AUTHORIZE]         = mod_authorize,
 #ifdef WITH_ACCOUNTING
-               mod_accounting, /* accounting */
-#else
-               NULL,
+               [MOD_ACCOUNTING]        = mod_accounting,
 #endif
 #ifdef WITH_SESSION_MGMT
-               mod_checksimul, /* checksimul */
-#else
-               NULL,
+               [MOD_SESSION]           = mod_checksimul,
 #endif
-               NULL,           /* pre-proxy */
-               NULL,           /* post-proxy */
-               mod_post_auth   /* post-auth */
+               [MOD_POST_AUTH]         = mod_post_auth
        },
 };