# 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}}"
# 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
# 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}')"
# 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
# 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}')"
# 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
# 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}'"
# 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
# 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}'"
# 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
# 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}"
# 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
# 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}"
}
-/*
- * 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.
*/
{
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");
if (sscanf(expanded, "%" PRIu64, &counter) != 1) {
RDEBUG2("No integer found in string \"%s\"", expanded);
}
+
talloc_free(expanded);
if (counter < check->vp_uint64) return -1;
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
}
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");
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;
}