All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
rlm_sqlcounter.c
Go to the documentation of this file.
1 /*
2  * This program is is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or (at
5  * your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15  */
16 
17 /**
18  * $Id: 90f9bc1feac97015c3ee3533683a7c994adadf07 $
19  * @file rlm_sqlcounter.c
20  * @brief Tracks data usage and other counters using SQL.
21  *
22  * @copyright 2001,2006 The FreeRADIUS server project
23  * @copyright 2001 Alan DeKok <aland@ox.org>
24  */
25 RCSID("$Id: 90f9bc1feac97015c3ee3533683a7c994adadf07 $")
26 
27 #include <freeradius-devel/radiusd.h>
28 #include <freeradius-devel/modules.h>
29 #include <freeradius-devel/rad_assert.h>
30 
31 #include <ctype.h>
32 
33 #define MAX_QUERY_LEN 1024
34 
35 /*
36  * Note: When your counter spans more than 1 period (ie 3 months
37  * or 2 weeks), this module probably does NOT do what you want! It
38  * calculates the range of dates to count across by first calculating
39  * the End of the Current period and then subtracting the number of
40  * periods you specify from that to determine the beginning of the
41  * range.
42  *
43  * For example, if you specify a 3 month counter and today is June 15th,
44  * the end of the current period is June 30. Subtracting 3 months from
45  * that gives April 1st. So, the counter will sum radacct entries from
46  * April 1st to June 30. Then, next month, it will sum entries from
47  * May 1st to July 31st.
48  *
49  * To fix this behavior, we need to add some way of storing the Next
50  * Reset Time.
51  */
52 
53 /*
54  * Define a structure for our module configuration.
55  *
56  * These variables do not need to be in a structure, but it's
57  * a lot cleaner to do so, and a pointer to the structure can
58  * be used as the instance handle.
59  */
60 typedef struct rlm_sqlcounter_t {
61  vp_tmpl_t *paircmp_attr; //!< Daily-Session-Time.
62  vp_tmpl_t *limit_attr; //!< Max-Daily-Session.
63  vp_tmpl_t *reply_attr; //!< Session-Timeout.
64  vp_tmpl_t *key_attr; //!< User-Name
65 
66  char const *sqlmod_inst; //!< Instance of SQL module to use, usually just 'sql'.
67  char const *query; //!< SQL query to retrieve current session time.
68  char const *reset; //!< Daily, weekly, monthly, never or user defined.
69 
70  time_t reset_time;
71  time_t last_reset;
73 
74 /*
75  * A mapping of configuration file names to internal variables.
76  *
77  * Note that the string is dynamically allocated, so it MUST
78  * be freed. When the configuration file parse re-reads the string,
79  * it free's the old one, and strdup's the new one, placing the pointer
80  * to the strdup'd string into 'config.string'. This gets around
81  * buffer over-flows.
82  */
83 static const CONF_PARSER module_config[] = {
84  { FR_CONF_OFFSET("sql_module_instance", PW_TYPE_STRING | PW_TYPE_REQUIRED, rlm_sqlcounter_t, sqlmod_inst) },
85 
86 
89 
90  { FR_CONF_OFFSET("key", PW_TYPE_TMPL | PW_TYPE_ATTRIBUTE, rlm_sqlcounter_t, key_attr), .dflt = "&request:User-Name", .quote = T_BARE_WORD },
91 
92  /* Just used to register a paircompare against */
93  { FR_CONF_OFFSET("counter_name", PW_TYPE_TMPL | PW_TYPE_ATTRIBUTE | PW_TYPE_REQUIRED, rlm_sqlcounter_t, paircmp_attr) },
95 
96  /* Attribute to write remaining session to */
97  { FR_CONF_OFFSET("reply_name", PW_TYPE_TMPL | PW_TYPE_ATTRIBUTE, rlm_sqlcounter_t, reply_attr) },
99 };
100 
101 static int find_next_reset(rlm_sqlcounter_t *inst, time_t timeval)
102 {
103  int ret = 0;
104  size_t len;
105  unsigned int num = 1;
106  char last = '\0';
107  struct tm *tm, s_tm;
108  char sCurrentTime[40], sNextTime[40];
109 
110  tm = localtime_r(&timeval, &s_tm);
111  len = strftime(sCurrentTime, sizeof(sCurrentTime), "%Y-%m-%d %H:%M:%S", tm);
112  if (len == 0) *sCurrentTime = '\0';
113  tm->tm_sec = tm->tm_min = 0;
114 
115  rad_assert(inst->reset != NULL);
116 
117  if (isdigit((int) inst->reset[0])){
118  len = strlen(inst->reset);
119  if (len == 0)
120  return -1;
121  last = inst->reset[len - 1];
122  if (!isalpha((int) last))
123  last = 'd';
124  num = atoi(inst->reset);
125  DEBUG("rlm_sqlcounter: num=%d, last=%c",num,last);
126  }
127  if (strcmp(inst->reset, "hourly") == 0 || last == 'h') {
128  /*
129  * Round up to the next nearest hour.
130  */
131  tm->tm_hour += num;
132  inst->reset_time = mktime(tm);
133  } else if (strcmp(inst->reset, "daily") == 0 || last == 'd') {
134  /*
135  * Round up to the next nearest day.
136  */
137  tm->tm_hour = 0;
138  tm->tm_mday += num;
139  inst->reset_time = mktime(tm);
140  } else if (strcmp(inst->reset, "weekly") == 0 || last == 'w') {
141  /*
142  * Round up to the next nearest week.
143  */
144  tm->tm_hour = 0;
145  tm->tm_mday += (7 - tm->tm_wday) +(7*(num-1));
146  inst->reset_time = mktime(tm);
147  } else if (strcmp(inst->reset, "monthly") == 0 || last == 'm') {
148  tm->tm_hour = 0;
149  tm->tm_mday = 1;
150  tm->tm_mon += num;
151  inst->reset_time = mktime(tm);
152  } else if (strcmp(inst->reset, "never") == 0) {
153  inst->reset_time = 0;
154  } else {
155  return -1;
156  }
157 
158  len = strftime(sNextTime, sizeof(sNextTime),"%Y-%m-%d %H:%M:%S",tm);
159  if (len == 0) *sNextTime = '\0';
160  DEBUG2("rlm_sqlcounter: Current Time: %" PRId64 " [%s], Next reset %" PRId64 " [%s]",
161  (int64_t) timeval, sCurrentTime, (int64_t) inst->reset_time, sNextTime);
162 
163  return ret;
164 }
165 
166 
167 /* I don't believe that this routine handles Daylight Saving Time adjustments
168  properly. Any suggestions?
169 */
170 static int find_prev_reset(rlm_sqlcounter_t *inst, time_t timeval)
171 {
172  int ret = 0;
173  size_t len;
174  unsigned int num = 1;
175  char last = '\0';
176  struct tm *tm, s_tm;
177  char sCurrentTime[40], sPrevTime[40];
178 
179  tm = localtime_r(&timeval, &s_tm);
180  len = strftime(sCurrentTime, sizeof(sCurrentTime), "%Y-%m-%d %H:%M:%S", tm);
181  if (len == 0) *sCurrentTime = '\0';
182  tm->tm_sec = tm->tm_min = 0;
183 
184  rad_assert(inst->reset != NULL);
185 
186  if (isdigit((int) inst->reset[0])){
187  len = strlen(inst->reset);
188  if (len == 0)
189  return -1;
190  last = inst->reset[len - 1];
191  if (!isalpha((int) last))
192  last = 'd';
193  num = atoi(inst->reset);
194  DEBUG("rlm_sqlcounter: num=%d, last=%c",num,last);
195  }
196  if (strcmp(inst->reset, "hourly") == 0 || last == 'h') {
197  /*
198  * Round down to the prev nearest hour.
199  */
200  tm->tm_hour -= num - 1;
201  inst->last_reset = mktime(tm);
202  } else if (strcmp(inst->reset, "daily") == 0 || last == 'd') {
203  /*
204  * Round down to the prev nearest day.
205  */
206  tm->tm_hour = 0;
207  tm->tm_mday -= num - 1;
208  inst->last_reset = mktime(tm);
209  } else if (strcmp(inst->reset, "weekly") == 0 || last == 'w') {
210  /*
211  * Round down to the prev nearest week.
212  */
213  tm->tm_hour = 0;
214  tm->tm_mday -= (7 - tm->tm_wday) +(7*(num-1));
215  inst->last_reset = mktime(tm);
216  } else if (strcmp(inst->reset, "monthly") == 0 || last == 'm') {
217  tm->tm_hour = 0;
218  tm->tm_mday = 1;
219  tm->tm_mon -= num - 1;
220  inst->last_reset = mktime(tm);
221  } else if (strcmp(inst->reset, "never") == 0) {
222  inst->reset_time = 0;
223  } else {
224  return -1;
225  }
226  len = strftime(sPrevTime, sizeof(sPrevTime), "%Y-%m-%d %H:%M:%S", tm);
227  if (len == 0) *sPrevTime = '\0';
228  DEBUG2("rlm_sqlcounter: Current Time: %" PRId64 " [%s], Prev reset %" PRId64 " [%s]",
229  (int64_t) timeval, sCurrentTime, (int64_t) inst->last_reset, sPrevTime);
230 
231  return ret;
232 }
233 
234 
235 /*
236  * Replace %<whatever> in a string.
237  *
238  * %b last_reset
239  * %e reset_time
240  * %k key_name
241  * %S sqlmod_inst
242  *
243  */
244 static size_t sqlcounter_expand(char *out, int outlen, rlm_sqlcounter_t *inst, REQUEST *request, char const *fmt)
245 {
246  int freespace;
247  char const *p;
248  char *q;
249  char tmpdt[40]; /* For temporary storing of dates */
250 
251  q = out;
252  p = fmt;
253  while (*p) {
254  /* Calculate freespace in output */
255  freespace = outlen - (q - out);
256  if (freespace <= 1) {
257  return -1;
258  }
259 
260  /*
261  * Non-% get copied as-is.
262  */
263  if (*p != '%') {
264  *q++ = *p++;
265  continue;
266  }
267  p++;
268  if (!*p) { /* % and then EOS --> % */
269  *q++ = '%';
270  break;
271  }
272 
273  if (freespace <= 2) return -1;
274 
275  /*
276  * We need TWO %% in a row before we do our expansions.
277  * If we only get one, just copy the %s as-is.
278  */
279  if (*p != '%') {
280  *q++ = '%';
281  *q++ = *p++;
282  continue;
283  }
284  p++;
285  if (!*p) {
286  *q++ = '%';
287  *q++ = '%';
288  break;
289  }
290 
291  if (freespace <= 3) return -1;
292 
293  switch (*p) {
294  case 'b': /* last_reset */
295  snprintf(tmpdt, sizeof(tmpdt), "%" PRId64, (int64_t) inst->last_reset);
296  strlcpy(q, tmpdt, freespace);
297  q += strlen(q);
298  p++;
299  break;
300  case 'e': /* reset_time */
301  snprintf(tmpdt, sizeof(tmpdt), "%" PRId64, (int64_t) inst->reset_time);
302  strlcpy(q, tmpdt, freespace);
303  q += strlen(q);
304  p++;
305  break;
306 
307  case 'k': /* Key Name */
308  {
309  VALUE_PAIR *vp;
310 
311  WARN("Please replace '%%k' with '%%{${key}}'");
312  tmpl_find_vp(&vp, request, inst->key_attr);
313  if (vp) {
314  fr_pair_value_snprint(q, freespace, vp, '"');
315  q += strlen(q);
316  }
317  p++;
318  }
319  break;
320 
321  /*
322  * %%s gets copied over as-is.
323  */
324  default:
325  *q++ = '%';
326  *q++ = '%';
327  *q++ = *p++;
328  break;
329  }
330  }
331  *q = '\0';
332 
333  DEBUG2("sqlcounter_expand: '%s'", out);
334 
335  return strlen(out);
336 }
337 
338 
339 /*
340  * See if the counter matches.
341  */
342 static int counter_cmp(void *instance, REQUEST *request, UNUSED VALUE_PAIR *req , VALUE_PAIR *check,
343  UNUSED VALUE_PAIR *check_pairs, UNUSED VALUE_PAIR **reply_pairs)
344 {
345  rlm_sqlcounter_t *inst = instance;
346  uint64_t counter;
347 
348  char query[MAX_QUERY_LEN], subst[MAX_QUERY_LEN];
349  char *expanded = NULL;
350  size_t len;
351 
352  /* First, expand %k, %b and %e in query */
353  if (sqlcounter_expand(subst, sizeof(subst), inst, request, inst->query) <= 0) {
354  REDEBUG("Insufficient query buffer space");
355 
356  return RLM_MODULE_FAIL;
357  }
358 
359  /* Then combine that with the name of the module were using to do the query */
360  len = snprintf(query, sizeof(query), "%%{%s:%s}", inst->sqlmod_inst, subst);
361  if (len >= sizeof(query) - 1) {
362  REDEBUG("Insufficient query buffer space");
363 
364  return RLM_MODULE_FAIL;
365  }
366 
367  /* Finally, xlat resulting SQL query */
368  if (radius_axlat(&expanded, request, query, NULL, NULL) < 0) {
369  return RLM_MODULE_FAIL;
370  }
371 
372  if (sscanf(expanded, "%" PRIu64, &counter) != 1) {
373  RDEBUG2("No integer found in string \"%s\"", expanded);
374  }
375  talloc_free(expanded);
376 
377  if (counter < check->vp_integer64) return -1;
378  if (counter > check->vp_integer64) return 1;
379  return 0;
380 }
381 
382 /*
383  * Find the named user in this modules database. Create the set
384  * of attribute-value pairs to check and reply with for this user
385  * from the database. The authentication code only needs to check
386  * the password, the rest is done here.
387  */
388 static rlm_rcode_t CC_HINT(nonnull) mod_authorize(void *instance, REQUEST *request)
389 {
390  rlm_sqlcounter_t *inst = instance;
391  uint64_t counter, res;
392  VALUE_PAIR *key_vp, *limit;
393  VALUE_PAIR *reply_item;
394  char msg[128];
395  int ret;
396 
397  char query[MAX_QUERY_LEN], subst[MAX_QUERY_LEN];
398  char *expanded = NULL;
399 
400  size_t len;
401 
402  /*
403  * Before doing anything else, see if we have to reset
404  * the counters.
405  */
406  if (inst->reset_time && (inst->reset_time <= request->timestamp.tv_sec)) {
407  /*
408  * Re-set the next time and prev_time for this counters range
409  */
410  inst->last_reset = inst->reset_time;
411  find_next_reset(inst,request->timestamp.tv_sec);
412  }
413 
414  /*
415  * Look for the key. User-Name is special. It means
416  * The REAL username, after stripping.
417  */
418  if ((inst->key_attr->tmpl_list == PAIR_LIST_REQUEST) &&
419  (inst->key_attr->tmpl_da->vendor == 0) && (inst->key_attr->tmpl_da->attr == PW_USER_NAME)) {
420  key_vp = request->username;
421  } else {
422  tmpl_find_vp(&key_vp, request, inst->key_attr);
423  }
424  if (!key_vp) {
425  RWDEBUG2("Couldn't find key attribute, %s, doing nothing...", inst->key_attr->tmpl_da->name);
426  return RLM_MODULE_NOOP;
427  }
428 
429  if (tmpl_find_vp(&limit, request, inst->limit_attr) < 0) {
430  RWDEBUG2("Couldn't find limit attribute, %s, doing nothing...", inst->limit_attr->name);
431  return RLM_MODULE_NOOP;
432  }
433 
434  /* First, expand %k, %b and %e in query */
435  if (sqlcounter_expand(subst, sizeof(subst), inst, request, inst->query) <= 0) {
436  REDEBUG("Insufficient query buffer space");
437 
438  return RLM_MODULE_FAIL;
439  }
440 
441  /* Then combine that with the name of the module were using to do the query */
442  len = snprintf(query, sizeof(query), "%%{%s:%s}", inst->sqlmod_inst, subst);
443  if (len >= (sizeof(query) - 1)) {
444  REDEBUG("Insufficient query buffer space");
445 
446  return RLM_MODULE_FAIL;
447  }
448 
449  /* Finally, xlat resulting SQL query */
450  if (radius_axlat(&expanded, request, query, NULL, NULL) < 0) {
451  return RLM_MODULE_FAIL;
452  }
453  talloc_free(expanded);
454 
455  if (sscanf(expanded, "%" PRIu64, &counter) != 1) {
456  RDEBUG2("No integer found in result string \"%s\". May be first session, setting counter to 0",
457  expanded);
458  counter = 0;
459  }
460 
461  /*
462  * Check if check item > counter
463  */
464  if (limit->vp_integer64 <= counter) {
465  /* User is denied access, send back a reply message */
466  snprintf(msg, sizeof(msg), "Your maximum %s usage time has been reached", inst->reset);
467  pair_make_reply("Reply-Message", msg, T_OP_EQ);
468 
469  REDEBUG2("Maximum %s usage time reached", inst->reset);
470  REDEBUG2("Rejecting user, %s value (%" PRIu64 ") is less than counter value (%" PRIu64 ")",
471  inst->limit_attr->name, limit->vp_integer64, counter);
472 
473  return RLM_MODULE_REJECT;
474  }
475 
476  res = limit->vp_integer64 - counter;
477  RDEBUG2("Allowing user, %s value (%" PRIu64 ") is greater than counter value (%" PRIu64 ")",
478  inst->limit_attr->name, limit->vp_integer64, counter);
479 
480  /*
481  * We are assuming that simultaneous-use=1. But
482  * even if that does not happen then our user
483  * could login at max for 2*max-usage-time Is
484  * that acceptable?
485  */
486  if (inst->reply_attr) {
487  /*
488  * If we are near a reset then add the next
489  * limit, so that the user will not need to login
490  * again. Do this only for Session-Timeout.
491  */
492  if (((inst->reply_attr->tmpl_da->vendor == 0) &&
493  (inst->reply_attr->tmpl_da->attr == PW_SESSION_TIMEOUT)) &&
494  inst->reset_time && (res >= (uint64_t)(inst->reset_time - request->timestamp.tv_sec))) {
495  uint64_t to_reset = inst->reset_time - request->timestamp.tv_sec;
496 
497  RDEBUG2("Time remaining (%" PRIu64 "s) is greater than time to reset (%" PRIu64 "s). "
498  "Adding %" PRIu64 "s to reply value", to_reset, res, to_reset);
499  res = to_reset + limit->vp_integer;
500  }
501 
502  /*
503  * Limit the reply attribute to the minimum of the existing value, or this new one.
504  */
505  ret = tmpl_find_or_add_vp(&reply_item, request, inst->reply_attr);
506  switch (ret) {
507  case 1: /* new */
508  break;
509 
510  case 0: /* found */
511  if (reply_item->vp_integer64 <= res) {
512  RDEBUG2("Leaving existing %s value of %" PRIu64, inst->reply_attr->name,
513  reply_item->vp_integer64);
514  return RLM_MODULE_OK;
515  }
516  break;
517 
518  case -1: /* alloc failed */
519  REDEBUG("Error allocating attribute %s", inst->reply_attr->name);
520  return RLM_MODULE_FAIL;
521 
522  default: /* request or list unavailable */
523  RDEBUG2("List or request context not available for %s, skipping...", inst->reply_attr->name);
524  return RLM_MODULE_OK;
525  }
526  reply_item->vp_integer64 = res;
527  rdebug_pair(L_DBG_LVL_2, request, reply_item, NULL);
528 
529  return RLM_MODULE_UPDATED;
530  }
531 
532  return RLM_MODULE_OK;
533 }
534 
535 /*
536  * Do any per-module initialization that is separate to each
537  * configured instance of the module. e.g. set up connections
538  * to external databases, read configuration files, set up
539  * dictionary entries, etc.
540  *
541  * If configuration information is given in the config section
542  * that must be referenced in later calls, store a handle to it
543  * in *instance otherwise put a null pointer there.
544  */
545 static int mod_instantiate(CONF_SECTION *conf, void *instance)
546 {
547  rlm_sqlcounter_t *inst = instance;
548  time_t now;
549 
550  rad_assert(inst->query && *inst->query);
551 
552  now = time(NULL);
553  inst->reset_time = 0;
554 
555  if (find_next_reset(inst, now) == -1) {
556  cf_log_err_cs(conf, "Invalid reset '%s'", inst->reset);
557  return -1;
558  }
559 
560  /*
561  * Discover the beginning of the current time period.
562  */
563  inst->last_reset = 0;
564 
565  if (find_prev_reset(inst, now) < 0) {
566  cf_log_err_cs(conf, "Invalid reset '%s'", inst->reset);
567  return -1;
568  }
569 
570  return 0;
571 }
572 
573 static int mod_bootstrap(CONF_SECTION *conf, void *instance)
574 {
575  rlm_sqlcounter_t *inst = instance;
576  fr_dict_attr_flags_t flags;
577 
578  /*
579  * Create a new attribute for the counter.
580  */
581  rad_assert(inst->paircmp_attr);
582  rad_assert(inst->limit_attr);
583 
584  memset(&flags, 0, sizeof(flags));
585  flags.compare = 1; /* ugly hack */
587  cf_log_err_cs(conf, "Failed defining counter attribute: %s", fr_strerror());
588  return -1;
589  }
590 
591  flags.compare = 0;
592  if (tmpl_define_undefined_attr(inst->limit_attr, PW_TYPE_INTEGER64, &flags) < 0) {
593  cf_log_err_cs(conf, "Failed defining check attribute: %s", fr_strerror());
594  return -1;
595  }
596 
597  if (inst->paircmp_attr->tmpl_da->type != PW_TYPE_INTEGER64) {
598  cf_log_err_cs(conf, "Counter attribute %s MUST be integer64",
599  inst->paircmp_attr->tmpl_da->name);
600  return -1;
601  }
602  if (paircompare_register_byname(inst->paircmp_attr->tmpl_da->name, NULL, true,
603  counter_cmp, inst) < 0) {
604  cf_log_err_cs(conf, "Failed registering comparison function for counter attribute %s: %s",
605  inst->paircmp_attr->tmpl_da->name, fr_strerror());
606  return -1;
607  }
608 
609  if (inst->limit_attr->tmpl_da->type != PW_TYPE_INTEGER64) {
610  cf_log_err_cs(conf, "Check attribute %s MUST be integer64",
611  inst->limit_attr->tmpl_da->name);
612  return -1;
613  }
614 
615  return 0;
616 }
617 
618 /*
619  * The module name should be the only globally exported symbol.
620  * That is, everything else should be 'static'.
621  *
622  * If the module needs to temporarily modify it's instantiation
623  * data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
624  * The server will then take care of ensuring that the module
625  * is single-threaded.
626  */
627 extern module_t rlm_sqlcounter;
628 module_t rlm_sqlcounter = {
630  .name = "sqlcounter",
631  .type = RLM_TYPE_THREAD_SAFE,
632  .inst_size = sizeof(rlm_sqlcounter_t),
633  .config = module_config,
634  .bootstrap = mod_bootstrap,
635  .instantiate = mod_instantiate,
636  .methods = {
638  },
639 };
640 
ssize_t ssize_t ssize_t radius_axlat(char **out, REQUEST *request, char const *fmt, xlat_escape_t escape, void *escape_ctx) CC_HINT(nonnull(1
#define MAX_QUERY_LEN
2nd highest priority debug messages (-xx | -X).
Definition: log.h:52
vp_tmpl_t * key_attr
User-Name.
vp_tmpl_t * paircmp_attr
Daily-Session-Time.
void rdebug_pair(log_lvl_t level, REQUEST *, VALUE_PAIR *, char const *)
Print a single valuepair to stderr or error log.
Definition: pair.c:739
The module is OK, continue.
Definition: radiusd.h:91
Metadata exported by the module.
Definition: modules.h:134
char const * name
Raw string used to create the template.
Definition: tmpl.h:190
static rlm_rcode_t mod_authorize(void *instance, REQUEST *request)
Handle authorization requests using Couchbase document data.
#define RLM_TYPE_THREAD_SAFE
Module is threadsafe.
Definition: modules.h:75
#define UNUSED
Definition: libradius.h:134
#define RLM_MODULE_INIT
Definition: modules.h:86
#define REDEBUG2(fmt,...)
Definition: log.h:255
void size_t fr_pair_value_snprint(char *out, size_t outlen, VALUE_PAIR const *vp, char quote)
Print the value of an attribute to a string.
Definition: pair.c:2107
#define CONF_PARSER_TERMINATOR
Definition: conffile.h:289
PUBLIC int snprintf(char *string, size_t length, char *format, va_alist)
Definition: snprintf.c:686
Values of the encryption flags.
Definition: dict.h:40
char const * sqlmod_inst
Instance of SQL module to use, usually just 'sql'.
vp_tmpl_t * reply_attr
Session-Timeout.
struct rlm_sqlcounter_t rlm_sqlcounter_t
#define inst
Definition: token.h:46
Defines a CONF_PAIR to C data type mapping.
Definition: conffile.h:267
static int find_next_reset(rlm_sqlcounter_t *inst, time_t timeval)
#define rad_assert(expr)
Definition: rad_assert.h:38
#define vp_integer64
Definition: pair.h:178
#define DEBUG(fmt,...)
Definition: log.h:175
int tmpl_find_vp(VALUE_PAIR **out, REQUEST *request, vp_tmpl_t const *vpt)
Returns the first VP matching a vp_tmpl_t.
Definition: tmpl.c:2224
vp_tmpl_t * limit_attr
Max-Daily-Session.
#define DEBUG2(fmt,...)
Definition: log.h:176
#define PW_TYPE_XLAT
string will be dynamically expanded.
Definition: conffile.h:207
Immediately reject the request.
Definition: radiusd.h:89
Attributes in incoming or internally proxied request.
Definition: tmpl.h:82
static int find_prev_reset(rlm_sqlcounter_t *inst, time_t timeval)
static const CONF_PARSER module_config[]
int tmpl_find_or_add_vp(VALUE_PAIR **out, REQUEST *request, vp_tmpl_t const *vpt)
Returns the first VP matching a vp_tmpl_t, or if no VPs match, creates a new one. ...
Definition: tmpl.c:2251
Stores an attribute, a value and various bits of other data.
Definition: pair.h:112
static int mod_bootstrap(CONF_SECTION *conf, void *instance)
void void cf_log_err_cs(CONF_SECTION const *cs, char const *fmt,...) CC_HINT(format(printf
int tmpl_define_undefined_attr(vp_tmpl_t *vpt, PW_TYPE type, fr_dict_attr_flags_t const *flags)
Add an undefined fr_dict_attr_t specified by a vp_tmpl_t to the main dictionary.
Definition: tmpl.c:1396
char const * query
SQL query to retrieve current session time.
enum rlm_rcodes rlm_rcode_t
Return codes indicating the result of the module call.
tmpl_type_t type
What type of value tmpl refers to.
Definition: tmpl.h:188
static rs_t * conf
Definition: radsniff.c:46
char const * fr_strerror(void)
Get the last library error.
Definition: log.c:212
#define RWDEBUG2(fmt,...)
Definition: log.h:252
module_t rlm_sqlcounter
64 Bit unsigned integer.
Definition: radius.h:51
Module succeeded without doing anything.
Definition: radiusd.h:96
#define RDEBUG2(fmt,...)
Definition: log.h:244
uint64_t magic
Used to validate module struct.
Definition: modules.h:135
Module failed, don't reply.
Definition: radiusd.h:90
#define FR_CONF_OFFSET(_n, _t, _s, _f)
Definition: conffile.h:168
int paircompare_register_byname(char const *name, fr_dict_attr_t const *from, bool first_only, RAD_COMPARE_FUNC func, void *instance)
Register a function as compare function.
Definition: pair.c:351
#define pair_make_reply(_a, _b, _c)
Definition: radiusd.h:546
#define WARN(fmt,...)
Definition: log.h:144
#define REDEBUG(fmt,...)
Definition: log.h:254
static size_t sqlcounter_expand(char *out, int outlen, rlm_sqlcounter_t *inst, REQUEST *request, char const *fmt)
static rlm_rcode_t CC_HINT(nonnull)
#define PW_TYPE_REQUIRED
Error out if no matching CONF_PAIR is found, and no dflt value is set.
Definition: conffile.h:200
char const * reset
Daily, weekly, monthly, never or user defined.
static int mod_instantiate(CONF_SECTION *conf, void *instance)
size_t strlcpy(char *dst, char const *src, size_t siz)
Definition: strlcpy.c:38
#define PW_TYPE_ATTRIBUTE
Value must resolve to attribute in dict (deprecated, use PW_TYPE_TMPL).
Definition: conffile.h:201
String of printable characters.
Definition: radius.h:33
#define PW_TYPE_TMPL
CONF_PAIR should be parsed as a template.
Definition: conffile.h:208
1 methods index for authorize section.
Definition: modules.h:42
struct tm * localtime_r(time_t const *l_clock, struct tm *result)
Definition: missing.c:152
#define RCSID(id)
Definition: build.h:135
OK (pairs modified).
Definition: radiusd.h:97
static int counter_cmp(void *instance, REQUEST *request, UNUSED VALUE_PAIR *req, VALUE_PAIR *check, UNUSED VALUE_PAIR *check_pairs, UNUSED VALUE_PAIR **reply_pairs)
A source or sink of value data.
Definition: tmpl.h:187
unsigned int compare
has a paircompare registered
Definition: dict.h:55