]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
use real names for counter start / end
authorAlan T. DeKok <aland@freeradius.org>
Thu, 24 Aug 2023 18:57:37 +0000 (14:57 -0400)
committerAlan T. DeKok <aland@freeradius.org>
Thu, 24 Aug 2023 19:00:28 +0000 (15:00 -0400)
raddb/mods-available/sqlcounter
raddb/mods-config/sql/counter/mysql/dailycounter.conf
raddb/mods-config/sql/counter/mysql/monthlycounter.conf
raddb/mods-config/sql/counter/postgresql/dailycounter.conf
raddb/mods-config/sql/counter/postgresql/monthlycounter.conf
raddb/mods-config/sql/counter/sqlite/dailycounter.conf
raddb/mods-config/sql/counter/sqlite/monthlycounter.conf
src/modules/rlm_sqlcounter/rlm_sqlcounter.c

index 883ae0690f1ad6f0479c0c4a7562d6965ca4c45d..e3bfde29e3d00b89cd5346b8ebcb37eeeee95f8a 100644 (file)
@@ -107,7 +107,7 @@ sqlcounter dailycounter {
 
 #      reset_period_start_name = &control.${.:instance}-Reset-Start
 #      reset_period_end_name = &control.${.:instance}-Reset-End
-       counter_name = &Daily-Session-Time
+       counter_name = &control.Daily-Session-Time
        check_name = &control.Max-Daily-Session
        reply_name = &reply.Session-Timeout
        key = "%{%{Stripped-User-Name}:-%{User-Name}}"
index dbfb097cd77456bf60561786cf5aea3e3f2a18b4..26f2887dda5b51a968cdd08d4b2cf11809ffa855 100644 (file)
@@ -5,10 +5,10 @@
 #  below
 #
 query = "\
-       SELECT SUM(acctsessiontime - GREATEST((%%b - UNIX_TIMESTAMP(acctstarttime)), 0)) \
+       SELECT SUM(acctsessiontime - GREATEST((%{dailycounter-reset-start} - UNIX_TIMESTAMP(acctstarttime)), 0)) \
        FROM radacct \
        WHERE username = '%{${key}}' \
-       AND UNIX_TIMESTAMP(acctstarttime) + acctsessiontime > '%%b'"
+       AND UNIX_TIMESTAMP(acctstarttime) + acctsessiontime > '%{dailycounter-reset-start}'"
 
 #
 #  This query ignores calls that started in a previous
@@ -19,15 +19,15 @@ query = "\
 #      SELECT SUM(acctsessiontime) \
 #      FROM radacct \
 #      WHERE username = '%{${key}}' \
-#      AND acctstarttime > FROM_UNIXTIME('%%b')"
+#      AND acctstarttime > FROM_UNIXTIME('%{dailycounter-reset-start}')"
 
 #
 #  This query is the same as above, but demonstrates an
-#  additional counter parameter '%%e' which is the
+#  additional counter parameter '%{dailycounter-reset-end}' which is the
 #  timestamp for the end of the period
 #
 #query = "\
 #      SELECT SUM(acctsessiontime) \
 #      FROM radacct \
 #      WHERE username = '%{${key}}' \
-#      AND acctstarttime BETWEEN FROM_UNIXTIME('%%b') AND FROM_UNIXTIME('%%e')"
+#      AND acctstarttime BETWEEN FROM_UNIXTIME('%{dailycounter-reset-start}') AND FROM_UNIXTIME('%{dailycounter-reset-end}')"
index 8999765d278f3383494a7dccf7f75abf15e7bce2..0b77ed191cee1e53515885419f75fa54fa02b870 100644 (file)
@@ -5,10 +5,10 @@
 #  below
 #
 query = "\
-       SELECT SUM(acctsessiontime - GREATEST((%%b - UNIX_TIMESTAMP(acctstarttime)), 0)) \
+       SELECT SUM(acctsessiontime - GREATEST((%{monthlycounter-reset-start} - UNIX_TIMESTAMP(acctstarttime)), 0)) \
        FROM radacct \
        WHERE username='%{${key}}' \
-       AND UNIX_TIMESTAMP(acctstarttime) + acctsessiontime > '%%b'"
+       AND UNIX_TIMESTAMP(acctstarttime) + acctsessiontime > '%{monthlycounter-reset-start}'"
 
 #
 #  This query ignores calls that started in a previous
@@ -19,16 +19,16 @@ query = "\
 #      SELECT SUM(acctsessiontime) \
 #      FROM radacct\
 #      WHERE username='%{${key}}' \
-#      AND acctstarttime > FROM_UNIXTIME('%%b')"
+#      AND acctstarttime > FROM_UNIXTIME('%{monthlycounter-reset-start}')"
 
 #
 #  This query is the same as above, but demonstrates an
-#  additional counter parameter '%%e' which is the
+#  additional counter parameter '%{monthlycounter-reset-end}' which is the
 #  timestamp for the end of the period
 #
 #query = "\
 #      SELECT SUM(acctsessiontime) \
 #      FROM radacct \
 #      WHERE username='%{${key}}' \
-#      AND acctstarttime BETWEEN FROM_UNIXTIME('%%b') \
-#      AND FROM_UNIXTIME('%%e')"
+#      AND acctstarttime BETWEEN FROM_UNIXTIME('%{monthlycounter-reset-start}') \
+#      AND FROM_UNIXTIME('%{monthlycounter-reset-end}')"
index 1e2f7fa811f97cf4a59cbc47b776eaba31cd88c9..e0d3cd657fb9602c3427f5da8709221ae228da6c 100644 (file)
@@ -5,10 +5,10 @@
 #  below
 #
 query = "\
-       SELECT SUM(AcctSessionTime - GREATEST((%%b - EXTRACT(epoch FROM AcctStartTime)), 0)) \
+       SELECT SUM(AcctSessionTime - GREATEST((%{dailycounter-reset-start} - EXTRACT(epoch FROM AcctStartTime)), 0)) \
        FROM radacct \
        WHERE UserName='%{${key}}' \
-       AND EXTRACT(epoch FROM AcctStartTime) + AcctSessionTime > '%%b'"
+       AND EXTRACT(epoch FROM AcctStartTime) + AcctSessionTime > '%{dailycounter-reset-start}'"
 
 #
 #  This query ignores calls that started in a previous
@@ -19,16 +19,16 @@ query = "\
 #      SELECT SUM(AcctSessionTime) \
 #      FROM radacct \
 #      WHERE UserName='%{${key}}' \
-#      AND EXTRACT(epoch FROM AcctStartTime) > '%%b'"
+#      AND EXTRACT(epoch FROM AcctStartTime) > '%{dailycounter-reset-start}'"
 
 #
 #  This query is the same as above, but demonstrates an
-#  additional counter parameter '%%e' which is the
+#  additional counter parameter '%{dailycounter-reset-end}' which is the
 #  timestamp for the end of the period
 #
 #query = "\
 #      SELECT SUM(AcctSessionTime) \
 #      FROM radacct \
 #      WHERE UserName='%{${key}}' \
-#      AND EXTRACT(epoch FROM AcctStartTime) BETWEEN '%%b' \
-#      AND '%%e'"
+#      AND EXTRACT(epoch FROM AcctStartTime) BETWEEN '%{dailycounter-reset-start}' \
+#      AND '%{dailycounter-reset-end}'"
index cdaf83a58cff690423620c76949de5a42337cb47..92afae4dbeb3fa1bb5e9d5a01b8e3b0c8c73f42d 100644 (file)
@@ -3,10 +3,10 @@
 #  involves more work for the SQL server than those
 #  below
 query = "\
-       SELECT SUM(AcctSessionTime - GREATEST((%%b - EXTRACT(epoch FROM AcctStartTime)), 0)) \
+       SELECT SUM(AcctSessionTime - GREATEST((%{monthlycounter-reset-start} - EXTRACT(epoch FROM AcctStartTime)), 0)) \
        FROM radacct \
        WHERE UserName='%{${key}}' \
-       AND EXTRACT(epoch FROM AcctStartTime) + AcctSessionTime > '%%b'"
+       AND EXTRACT(epoch FROM AcctStartTime) + AcctSessionTime > '%{monthlycounter-reset-start}'"
 
 #
 #  This query ignores calls that started in a previous
@@ -17,15 +17,15 @@ query = "\
 #      SELECT SUM(AcctSessionTime) \
 #      FROM radacct \
 #      WHERE UserName='%{${key}}' \
-#      AND EXTRACT(epoch FROM AcctStartTime) > '%%b'"
+#      AND EXTRACT(epoch FROM AcctStartTime) > '%{monthlycounter-reset-start}'"
 
 #
 #  This query is the same as above, but demonstrates an
-#  additional counter parameter '%%e' which is the
+#  additional counter parameter '%{monthlycounter-reset-end}' which is the
 #  timestamp for the end of the period
 #
 #query = "\
 #      SELECT SUM(AcctSessionTime) \
 #      FROM radacct \
 #      WHERE UserName='%{${key}}' \
-#      AND EXTRACT(epoch FROM AcctStartTime) BETWEEN '%%b' AND '%%e'"
+#      AND EXTRACT(epoch FROM AcctStartTime) BETWEEN '%{monthlycounter-reset-start}' AND '%{monthlycounter-reset-end}'"
index 9a2ec385e24f4f34ac58c99ce9924fb13e1c2395..26283d3a97ef3ea2e0d6a992d0d5d3a291d43e9e 100644 (file)
@@ -5,10 +5,10 @@
 #  below
 #
 query = "\
-       SELECT SUM(acctsessiontime - GREATEST((%%b - strftime('%%s', acctstarttime)), 0)) \
+       SELECT SUM(acctsessiontime - GREATEST((%{dailycounter-reset-start} - strftime('%%s', acctstarttime)), 0)) \
        FROM radacct \
        WHERE username = '%{${key}}' \
-       AND (strftime('%%s', acctstarttime) + acctsessiontime) > %%b"
+       AND (strftime('%%s', acctstarttime) + acctsessiontime) > %{dailycounter-reset-start}"
 
 #
 #  This query ignores calls that started in a previous
@@ -19,15 +19,15 @@ query = "\
 #      SELECT SUM(acctsessiontime) \
 #      FROM radacct \
 #      WHERE \username = '%{${key}}' \
-#      AND acctstarttime > %%b"
+#      AND acctstarttime > %{dailycounter-reset-start}"
 
 #
 #  This query is the same as above, but demonstrates an
-#  additional counter parameter '%%e' which is the
+#  additional counter parameter '%{dailycounter-reset-end}' which is the
 #  timestamp for the end of the period
 #
 #query = "\
 #      SELECT SUM(acctsessiontime) FROM radacct \
 #      WHERE username = '%{${key}}' \
-#      AND acctstarttime BETWEEN %%b \
-#      AND %%e"
+#      AND acctstarttime BETWEEN %{dailycounter-reset-start} \
+#      AND %{dailycounter-reset-end}"
index 5262097e329587130ee53d35c4de026bedc1ba36..e1a7ca764918daa06e7742090f9d743b3a9e4cc1 100644 (file)
@@ -5,10 +5,10 @@
 #  below
 #
 query = "\
-       SELECT SUM(acctsessiontime - GREATEST((%%b - strftime('%%s', acctstarttime)), 0)) \
+       SELECT SUM(acctsessiontime - GREATEST((%{monthlycounter-reset-start} - strftime('%%s', acctstarttime)), 0)) \
        FROM radacct \
        WHERE username = '%{${key}}' AND \
-       (strftime('%%s', acctstarttime) + acctsessiontime) > %%b"
+       (strftime('%%s', acctstarttime) + acctsessiontime) > %{monthlycounter-reset-start}"
 
 #
 #  This query ignores calls that started in a previous
@@ -19,16 +19,16 @@ query = "\
 #      SELECT SUM(acctsessiontime) \
 #      FROM radacct \
 #      WHERE username = '%{${key}}' \
-#      AND acctstarttime > %%b"
+#      AND acctstarttime > %{monthlycounter-reset-start}"
 
 #
 #  This query is the same as above, but demonstrates an
-#  additional counter parameter '%%e' which is the
+#  additional counter parameter '%{monthlycounter-reset-end}' which is the
 #  timestamp for the end of the period
 #
 #query = "\
 #      SELECT SUM(acctsessiontime) \
 #      FROM radacct \
 #      WHERE username = '%{${key}}' \
-#      AND acctstarttime BETWEEN %%b \
-#      AND %%e"
+#      AND acctstarttime BETWEEN %{monthlycounter-reset-start} \
+#      AND %{monthlycounter-reset-end}"
index d391c1b58cc43e519592223970672f7bfab959b1..951479b30732bf4d62ec9aa787dcc0e3049768ce 100644 (file)
@@ -244,93 +244,6 @@ static int find_prev_reset(rlm_sqlcounter_t *inst, fr_time_t now)
 }
 
 
-/*
- *     Replace %<whatever> in a string.
- *
- *     %b      last_reset
- *     %e      reset_time
- */
-static ssize_t sqlcounter_expand(char *out, int outlen, rlm_sqlcounter_t const *inst, UNUSED request_t *request, char const *fmt)
-{
-       int freespace;
-       char const *p;
-       char *q;
-       char tmpdt[40]; /* For temporary storing of dates */
-
-       q = out;
-       p = fmt;
-       while (*p) {
-               /* Calculate freespace in output */
-               freespace = outlen - (q - out);
-               if (freespace <= 1) {
-                       return -1;
-               }
-
-               /*
-                *      Non-% get copied as-is.
-                */
-               if (*p != '%') {
-                       *q++ = *p++;
-                       continue;
-               }
-               p++;
-               if (!*p) {      /* % and then EOS --> % */
-                       *q++ = '%';
-                       break;
-               }
-
-               if (freespace <= 2) return -1;
-
-               /*
-                *      We need TWO %% in a row before we do our expansions.
-                *      If we only get one, just copy the %s as-is.
-                */
-               if (*p != '%') {
-                       *q++ = '%';
-                       *q++ = *p++;
-                       continue;
-               }
-               p++;
-               if (!*p) {
-                       *q++ = '%';
-                       *q++ = '%';
-                       break;
-               }
-
-               if (freespace <= 3) return -1;
-
-               switch (*p) {
-                       case 'b': /* last_reset */
-                               snprintf(tmpdt, sizeof(tmpdt), "%" PRId64, fr_time_to_sec(inst->last_reset));
-                               strlcpy(q, tmpdt, freespace);
-                               q += strlen(q);
-                               p++;
-                               break;
-                       case 'e': /* reset_time */
-                               snprintf(tmpdt, sizeof(tmpdt), "%" PRId64, fr_time_to_sec(inst->reset_time));
-                               strlcpy(q, tmpdt, freespace);
-                               q += strlen(q);
-                               p++;
-                               break;
-
-                               /*
-                                *      %%s gets copied over as-is.
-                                */
-                       default:
-                               *q++ = '%';
-                               *q++ = '%';
-                               *q++ = *p++;
-                               break;
-               }
-       }
-       *q = '\0';
-
-       DEBUG2("sqlcounter_expand: '%s'", out);
-
-       return strlen(out);
-}
-
-
 /*
  *     See if the counter matches.
  */
@@ -338,20 +251,12 @@ static int counter_cmp(void *instance, request_t *request, fr_pair_t const *chec
 {
        rlm_sqlcounter_t const *inst = talloc_get_type_abort_const(instance, rlm_sqlcounter_t);
        uint64_t counter;
-
-       char query[MAX_QUERY_LEN], subst[MAX_QUERY_LEN];
-       char *expanded = NULL;
        size_t len;
-
-       /* First, expand %k, %b and %e in query */
-       if (sqlcounter_expand(subst, sizeof(subst), inst, request, inst->query) <= 0) {
-               REDEBUG("Insufficient query buffer space");
-
-               return -1;
-       }
+       char *expanded = NULL;
+       char query[MAX_QUERY_LEN];
 
        /* Then combine that with the name of the module were using to do the query */
-       len = snprintf(query, sizeof(query), "%%{%s:%s}", inst->sqlmod_inst, subst);
+       len = snprintf(query, sizeof(query), "%%{%s:%s}", inst->sqlmod_inst, inst->query);
        if (len >= sizeof(query) - 1) {
                REDEBUG("Insufficient query buffer space");
 
@@ -366,6 +271,7 @@ static int counter_cmp(void *instance, request_t *request, fr_pair_t const *chec
        if (sscanf(expanded, "%" PRIu64, &counter) != 1) {
                RDEBUG2("No integer found in string \"%s\"", expanded);
        }
+
        talloc_free(expanded);
 
        if (counter < check->vp_uint64) return -1;
@@ -387,11 +293,9 @@ static unlang_action_t CC_HINT(nonnull) mod_authorize(rlm_rcode_t *p_result, mod
        fr_pair_t               *reply_item;
        char                    msg[128];
        int                     ret;
-
-       char query[MAX_QUERY_LEN], subst[MAX_QUERY_LEN];
-       char *expanded = NULL;
-
        size_t len;
+       char *expanded = NULL;
+       char query[MAX_QUERY_LEN];
 
        /*
         *      Before doing anything else, see if we have to reset
@@ -423,15 +327,8 @@ static unlang_action_t CC_HINT(nonnull) mod_authorize(rlm_rcode_t *p_result, mod
        }
        vp->vp_uint64 = fr_time_to_sec(inst->reset_time);
 
-       /* First, expand %k, %b and %e in query */
-       if (sqlcounter_expand(subst, sizeof(subst), inst, request, inst->query) <= 0) {
-               REDEBUG("Insufficient query buffer space");
-
-               RETURN_MODULE_FAIL;
-       }
-
        /* Then combine that with the name of the module were using to do the query */
-       len = snprintf(query, sizeof(query), "%%{%s:%s}", inst->sqlmod_inst, subst);
+       len = snprintf(query, sizeof(query), "%%{%s:%s}", inst->sqlmod_inst, inst->query);
        if (len >= (sizeof(query) - 1)) {
                REDEBUG("Insufficient query buffer space");
 
@@ -614,7 +511,7 @@ static int mod_bootstrap(module_inst_ctx_t const *mctx)
                return -1;
        }
 
-       if (tmpl_attr_tail_unresolved_add(fr_dict_unconst(dict_freeradius), inst->paircmp_attr, FR_TYPE_UINT64, &flags) < 0) {
+       if (tmpl_attr_tail_unresolved_add(fr_dict_unconst(dict_freeradius), inst->end_attr, FR_TYPE_UINT64, &flags) < 0) {
                cf_log_perr(conf, "Failed defining reset_end_start attribute");
                return -1;
        }