]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
rlm_sql_map: Add 'multiple_rows' option (#3903)
authorJorge Pereira <jpereira@users.noreply.github.com>
Sun, 7 Mar 2021 00:16:51 +0000 (21:16 -0300)
committerGitHub <noreply@github.com>
Sun, 7 Mar 2021 00:16:51 +0000 (00:16 +0000)
raddb/mods-available/sql_map
src/modules/rlm_sql_map/rlm_sql_map.c

index 2798952abb9d882fde08fc3a5aeb9ff94e97503a..91856aa304212541de49edeed83b48b54a771db4 100644 (file)
@@ -43,4 +43,7 @@ sql_map {
 #              reply:Tunnel-Medium-Type        := 5
 #              reply:Tunnel-Private-Group-ID   := 6
        }
+
+       # If the 'query' results in multiple rows, it creates the <radius attr>[*] array entry.
+#      multiple_rows = yes
 }
index 77ab0a0243adf2acbffb13a9eefe7107b3491d74..69d83795aeb7e72c5d4bd99097d3d634b8ed6700 100644 (file)
@@ -37,9 +37,11 @@ RCSID("$Id$")
 typedef struct rlm_sql_map_t {
        char const      *sql_instance_name;     //!< Instance of SQL module to use,
                                                //!< usually just 'sql'.
-       rlm_sql_t       *sql_inst;
+       bool            multiple_rows;          //!< Process all rows creating an attr[*] array
+
+       char const      *query;                 //!< SQL query to retrieve current
 
-       char const      *query;         //!< SQL query to retrieve current
+       rlm_sql_t       *sql_inst;
 
        CONF_SECTION    *cs;
 
@@ -60,7 +62,7 @@ typedef struct rlm_sql_map_t {
  */
 static const CONF_PARSER module_config[] = {
        { "sql_module_instance", FR_CONF_OFFSET(PW_TYPE_STRING | PW_TYPE_REQUIRED, rlm_sql_map_t, sql_instance_name), NULL },
-
+       { "multiple_rows", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, rlm_sql_map_t, multiple_rows), "no" },
        { "query", FR_CONF_OFFSET(PW_TYPE_STRING | PW_TYPE_XLAT | PW_TYPE_REQUIRED, rlm_sql_map_t, query), NULL },
 
        CONF_PARSER_TERMINATOR
@@ -243,6 +245,11 @@ static int sql_map_do(const rlm_sql_map_t *inst, REQUEST *request, rlm_sql_handl
                ctx.row = (*handle)->row;
                ctx.num_columns = (inst->sql_inst->module->sql_num_fields)(*handle, inst->sql_inst->config);
 
+               if (applied >= 1 && !inst->multiple_rows) {
+                       RWDEBUG("Ignoring multiple rows. Enable the option 'multiple_rows' if you need multiple rows.");
+                       break;
+               }
+
                for (map = inst->user_map; map != NULL; map = map->next) {
                        /*
                         *      If something bad happened, just skip, this is probably
@@ -253,6 +260,8 @@ static int sql_map_do(const rlm_sql_map_t *inst, REQUEST *request, rlm_sql_handl
                                return -1;      /* Fail */
                        }
                }
+
+               applied++;
        }
 
        return applied;