Fix backport issue
[freeradius.git] / src / modules / rlm_sql / rlm_sql.c
index 713fd4f..7536ebd 100644 (file)
@@ -189,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->module->sql_finish_query)(handle, inst->config);
 
                        goto finish;
                }
@@ -225,7 +226,10 @@ static ssize_t sql_xlat(void *instance, REQUEST *request, char const *query, cha
        if (rcode != RLM_SQL_OK) goto query_error;
 
        rcode = rlm_sql_fetch_row(inst, request, &handle);
-       if (rcode) goto query_error;
+       if (rcode) {
+               (inst->module->sql_finish_select_query)(handle, inst->config);
+               goto query_error;
+       }
 
        row = handle->row;
        if (!row) {
@@ -283,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:
                 *
@@ -313,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];
                }
 
@@ -499,7 +511,12 @@ int sql_set_user(rlm_sql_t *inst, REQUEST *request, char const *username)
        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;
 }
@@ -838,7 +855,7 @@ static int mod_bootstrap(CONF_SECTION *conf, void *instance)
         *
         *      We need this to check if the sql_fields callback is provided.
         */
-       inst->handle = lt_dlopenext(inst->config->sql_driver_name);
+       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");
@@ -1006,7 +1023,7 @@ do { \
                }
        }
 
-       inst->ef = exfile_init(inst, 64, 30, true);
+       inst->ef = exfile_init(inst, 256, 30, true);
        if (!inst->ef) {
                cf_log_err_cs(conf, "Failed creating log file context");
                return -1;
@@ -1586,11 +1603,21 @@ static rlm_rcode_t mod_checksimul(void *instance, REQUEST * request)
        }
 
        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;
@@ -1631,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,