From: Alan T. DeKok Date: Thu, 24 Aug 2023 18:57:37 +0000 (-0400) Subject: use real names for counter start / end X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=98a9cf21950eaac9f8d11d4d2534037158db6bb0;p=thirdparty%2Ffreeradius-server.git use real names for counter start / end --- diff --git a/raddb/mods-available/sqlcounter b/raddb/mods-available/sqlcounter index 883ae0690f1..e3bfde29e3d 100644 --- a/raddb/mods-available/sqlcounter +++ b/raddb/mods-available/sqlcounter @@ -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}}" diff --git a/raddb/mods-config/sql/counter/mysql/dailycounter.conf b/raddb/mods-config/sql/counter/mysql/dailycounter.conf index dbfb097cd77..26f2887dda5 100644 --- a/raddb/mods-config/sql/counter/mysql/dailycounter.conf +++ b/raddb/mods-config/sql/counter/mysql/dailycounter.conf @@ -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}')" diff --git a/raddb/mods-config/sql/counter/mysql/monthlycounter.conf b/raddb/mods-config/sql/counter/mysql/monthlycounter.conf index 8999765d278..0b77ed191ce 100644 --- a/raddb/mods-config/sql/counter/mysql/monthlycounter.conf +++ b/raddb/mods-config/sql/counter/mysql/monthlycounter.conf @@ -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}')" diff --git a/raddb/mods-config/sql/counter/postgresql/dailycounter.conf b/raddb/mods-config/sql/counter/postgresql/dailycounter.conf index 1e2f7fa811f..e0d3cd657fb 100644 --- a/raddb/mods-config/sql/counter/postgresql/dailycounter.conf +++ b/raddb/mods-config/sql/counter/postgresql/dailycounter.conf @@ -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}'" diff --git a/raddb/mods-config/sql/counter/postgresql/monthlycounter.conf b/raddb/mods-config/sql/counter/postgresql/monthlycounter.conf index cdaf83a58cf..92afae4dbeb 100644 --- a/raddb/mods-config/sql/counter/postgresql/monthlycounter.conf +++ b/raddb/mods-config/sql/counter/postgresql/monthlycounter.conf @@ -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}'" diff --git a/raddb/mods-config/sql/counter/sqlite/dailycounter.conf b/raddb/mods-config/sql/counter/sqlite/dailycounter.conf index 9a2ec385e24..26283d3a97e 100644 --- a/raddb/mods-config/sql/counter/sqlite/dailycounter.conf +++ b/raddb/mods-config/sql/counter/sqlite/dailycounter.conf @@ -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}" diff --git a/raddb/mods-config/sql/counter/sqlite/monthlycounter.conf b/raddb/mods-config/sql/counter/sqlite/monthlycounter.conf index 5262097e329..e1a7ca76491 100644 --- a/raddb/mods-config/sql/counter/sqlite/monthlycounter.conf +++ b/raddb/mods-config/sql/counter/sqlite/monthlycounter.conf @@ -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}" diff --git a/src/modules/rlm_sqlcounter/rlm_sqlcounter.c b/src/modules/rlm_sqlcounter/rlm_sqlcounter.c index d391c1b58cc..951479b3073 100644 --- a/src/modules/rlm_sqlcounter/rlm_sqlcounter.c +++ b/src/modules/rlm_sqlcounter/rlm_sqlcounter.c @@ -244,93 +244,6 @@ static int find_prev_reset(rlm_sqlcounter_t *inst, fr_time_t now) } -/* - * Replace % 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; }