5e9d9a03e6440b41dfc3ed3c88b47392f6178eeb
[freeradius.git] / src / modules / rlm_sqlcounter / rlm_sqlcounter.c
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$
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$")
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         char const      *counter_name;  //!< Daily-Session-Time.
62         char const      *limit_name;    //!< Max-Daily-Session.
63         char const      *reply_name;    //!< Session-Timeout.
64         char const      *key_name;      //!< User-Name.
65         char const      *sqlmod_inst;   //!< Instance of SQL module to use,
66                                         //!< usually just 'sql'.
67         char const      *query;         //!< SQL query to retrieve current
68                                         //!< session time.
69         char const      *reset;         //!< Daily, weekly, monthly,
70                                         //!< never or user defined.
71         time_t          reset_time;
72         time_t          last_reset;
73         DICT_ATTR const *key_attr;      //!< Attribute number for key field.
74         DICT_ATTR const *dict_attr;     //!< Attribute number for the counter.
75         DICT_ATTR const *reply_attr;    //!< Attribute number for the reply.
76 } rlm_sqlcounter_t;
77
78 /*
79  *      A mapping of configuration file names to internal variables.
80  *
81  *      Note that the string is dynamically allocated, so it MUST
82  *      be freed.  When the configuration file parse re-reads the string,
83  *      it free's the old one, and strdup's the new one, placing the pointer
84  *      to the strdup'd string into 'config.string'.  This gets around
85  *      buffer over-flows.
86  */
87 static const CONF_PARSER module_config[] = {
88         { "sql-module-instance", FR_CONF_OFFSET(PW_TYPE_STRING | PW_TYPE_DEPRECATED, rlm_sqlcounter_t, sqlmod_inst), NULL },
89         { "sql_module_instance", FR_CONF_OFFSET(PW_TYPE_STRING | PW_TYPE_REQUIRED, rlm_sqlcounter_t, sqlmod_inst), NULL },
90
91         { "key", FR_CONF_OFFSET(PW_TYPE_STRING | PW_TYPE_ATTRIBUTE, rlm_sqlcounter_t, key_name), NULL },
92         { "query", FR_CONF_OFFSET(PW_TYPE_STRING | PW_TYPE_XLAT | PW_TYPE_REQUIRED, rlm_sqlcounter_t, query), NULL },
93         { "reset", FR_CONF_OFFSET(PW_TYPE_STRING | PW_TYPE_REQUIRED, rlm_sqlcounter_t, reset), NULL },
94
95         { "counter-name", FR_CONF_OFFSET(PW_TYPE_STRING | PW_TYPE_DEPRECATED, rlm_sqlcounter_t, counter_name), NULL },
96         { "counter_name", FR_CONF_OFFSET(PW_TYPE_STRING | PW_TYPE_REQUIRED, rlm_sqlcounter_t, counter_name), NULL },
97
98         { "check-name", FR_CONF_OFFSET(PW_TYPE_STRING | PW_TYPE_DEPRECATED, rlm_sqlcounter_t, limit_name), NULL },
99         { "check_name", FR_CONF_OFFSET(PW_TYPE_STRING | PW_TYPE_REQUIRED, rlm_sqlcounter_t, limit_name), NULL },
100
101         { "reply-name", FR_CONF_OFFSET(PW_TYPE_STRING | PW_TYPE_DEPRECATED, rlm_sqlcounter_t, reply_name), NULL },
102         { "reply_name", FR_CONF_OFFSET(PW_TYPE_STRING | PW_TYPE_ATTRIBUTE, rlm_sqlcounter_t, reply_name), "Session-Timeout" },
103         CONF_PARSER_TERMINATOR
104 };
105
106 static int find_next_reset(rlm_sqlcounter_t *inst, time_t timeval)
107 {
108         int ret = 0;
109         size_t len;
110         unsigned int num = 1;
111         char last = '\0';
112         struct tm *tm, s_tm;
113         char sCurrentTime[40], sNextTime[40];
114
115         tm = localtime_r(&timeval, &s_tm);
116         len = strftime(sCurrentTime, sizeof(sCurrentTime), "%Y-%m-%d %H:%M:%S", tm);
117         if (len == 0) *sCurrentTime = '\0';
118         tm->tm_sec = tm->tm_min = 0;
119
120         rad_assert(inst->reset != NULL);
121
122         if (isdigit((int) inst->reset[0])){
123                 len = strlen(inst->reset);
124                 if (len == 0)
125                         return -1;
126                 last = inst->reset[len - 1];
127                 if (!isalpha((int) last))
128                         last = 'd';
129                 num = atoi(inst->reset);
130                 DEBUG("rlm_sqlcounter: num=%d, last=%c",num,last);
131         }
132         if (strcmp(inst->reset, "hourly") == 0 || last == 'h') {
133                 /*
134                  *  Round up to the next nearest hour.
135                  */
136                 tm->tm_hour += num;
137                 inst->reset_time = mktime(tm);
138         } else if (strcmp(inst->reset, "daily") == 0 || last == 'd') {
139                 /*
140                  *  Round up to the next nearest day.
141                  */
142                 tm->tm_hour = 0;
143                 tm->tm_mday += num;
144                 inst->reset_time = mktime(tm);
145         } else if (strcmp(inst->reset, "weekly") == 0 || last == 'w') {
146                 /*
147                  *  Round up to the next nearest week.
148                  */
149                 tm->tm_hour = 0;
150                 tm->tm_mday += (7 - tm->tm_wday) +(7*(num-1));
151                 inst->reset_time = mktime(tm);
152         } else if (strcmp(inst->reset, "monthly") == 0 || last == 'm') {
153                 tm->tm_hour = 0;
154                 tm->tm_mday = 1;
155                 tm->tm_mon += num;
156                 inst->reset_time = mktime(tm);
157         } else if (strcmp(inst->reset, "never") == 0) {
158                 inst->reset_time = 0;
159         } else {
160                 return -1;
161         }
162
163         len = strftime(sNextTime, sizeof(sNextTime),"%Y-%m-%d %H:%M:%S",tm);
164         if (len == 0) *sNextTime = '\0';
165         DEBUG2("rlm_sqlcounter: Current Time: %" PRId64 " [%s], Next reset %" PRId64 " [%s]",
166                (int64_t) timeval, sCurrentTime, (int64_t) inst->reset_time, sNextTime);
167
168         return ret;
169 }
170
171
172 /*  I don't believe that this routine handles Daylight Saving Time adjustments
173     properly.  Any suggestions?
174 */
175
176 static int find_prev_reset(rlm_sqlcounter_t *inst, time_t timeval)
177 {
178         int ret = 0;
179         size_t len;
180         unsigned int num = 1;
181         char last = '\0';
182         struct tm *tm, s_tm;
183         char sCurrentTime[40], sPrevTime[40];
184
185         tm = localtime_r(&timeval, &s_tm);
186         len = strftime(sCurrentTime, sizeof(sCurrentTime), "%Y-%m-%d %H:%M:%S", tm);
187         if (len == 0) *sCurrentTime = '\0';
188         tm->tm_sec = tm->tm_min = 0;
189
190         rad_assert(inst->reset != NULL);
191
192         if (isdigit((int) inst->reset[0])){
193                 len = strlen(inst->reset);
194                 if (len == 0)
195                         return -1;
196                 last = inst->reset[len - 1];
197                 if (!isalpha((int) last))
198                         last = 'd';
199                 num = atoi(inst->reset);
200                 DEBUG("rlm_sqlcounter: num=%d, last=%c",num,last);
201         }
202         if (strcmp(inst->reset, "hourly") == 0 || last == 'h') {
203                 /*
204                  *  Round down to the prev nearest hour.
205                  */
206                 tm->tm_hour -= num - 1;
207                 inst->last_reset = mktime(tm);
208         } else if (strcmp(inst->reset, "daily") == 0 || last == 'd') {
209                 /*
210                  *  Round down to the prev nearest day.
211                  */
212                 tm->tm_hour = 0;
213                 tm->tm_mday -= num - 1;
214                 inst->last_reset = mktime(tm);
215         } else if (strcmp(inst->reset, "weekly") == 0 || last == 'w') {
216                 /*
217                  *  Round down to the prev nearest week.
218                  */
219                 tm->tm_hour = 0;
220                 tm->tm_mday -= (7 - tm->tm_wday) +(7*(num-1));
221                 inst->last_reset = mktime(tm);
222         } else if (strcmp(inst->reset, "monthly") == 0 || last == 'm') {
223                 tm->tm_hour = 0;
224                 tm->tm_mday = 1;
225                 tm->tm_mon -= num - 1;
226                 inst->last_reset = mktime(tm);
227         } else if (strcmp(inst->reset, "never") == 0) {
228                 inst->reset_time = 0;
229         } else {
230                 return -1;
231         }
232         len = strftime(sPrevTime, sizeof(sPrevTime), "%Y-%m-%d %H:%M:%S", tm);
233         if (len == 0) *sPrevTime = '\0';
234         DEBUG2("rlm_sqlcounter: Current Time: %" PRId64 " [%s], Prev reset %" PRId64 " [%s]",
235                (int64_t) timeval, sCurrentTime, (int64_t) inst->last_reset, sPrevTime);
236
237         return ret;
238 }
239
240
241 /*
242  *      Replace %<whatever> in a string.
243  *
244  *      %b      last_reset
245  *      %e      reset_time
246  *      %k      key_name
247  *      %S      sqlmod_inst
248  *
249  */
250
251 static size_t sqlcounter_expand(char *out, int outlen, char const *fmt, rlm_sqlcounter_t *inst)
252 {
253         int freespace;
254         char const *p;
255         char *q;
256         char tmpdt[40]; /* For temporary storing of dates */
257
258         q = out;
259         p = fmt;
260         while (*p) {
261                 /* Calculate freespace in output */
262                 freespace = outlen - (q - out);
263                 if (freespace <= 1) {
264                         return -1;
265                 }
266
267                 /*
268                  *      Non-% get copied as-is.
269                  */
270                 if (*p != '%') {
271                         *q++ = *p++;
272                         continue;
273                 }
274                 p++;
275                 if (!*p) {      /* % and then EOS --> % */
276                         *q++ = '%';
277                         break;
278                 }
279
280                 if (freespace <= 2) return -1;
281
282                 /*
283                  *      We need TWO %% in a row before we do our expansions.
284                  *      If we only get one, just copy the %s as-is.
285                  */
286                 if (*p != '%') {
287                         *q++ = '%';
288                         *q++ = *p++;
289                         continue;
290                 }
291                 p++;
292                 if (!*p) {
293                         *q++ = '%';
294                         *q++ = '%';
295                         break;
296                 }
297
298                 if (freespace <= 3) return -1;
299
300                 switch (*p) {
301                         case 'b': /* last_reset */
302                                 snprintf(tmpdt, sizeof(tmpdt), "%" PRId64, (int64_t) inst->last_reset);
303                                 strlcpy(q, tmpdt, freespace);
304                                 q += strlen(q);
305                                 p++;
306                                 break;
307                         case 'e': /* reset_time */
308                                 snprintf(tmpdt, sizeof(tmpdt), "%" PRId64, (int64_t) inst->reset_time);
309                                 strlcpy(q, tmpdt, freespace);
310                                 q += strlen(q);
311                                 p++;
312                                 break;
313                         case 'k': /* Key Name */
314                                 WARN("Please replace '%%k' with '${key}'");
315                                 strlcpy(q, inst->key_name, freespace);
316                                 q += strlen(q);
317                                 p++;
318                                 break;
319
320                                 /*
321                                  *      %%s gets copied over as-is.
322                                  */
323                         default:
324                                 *q++ = '%';
325                                 *q++ = '%';
326                                 *q++ = *p++;
327                                 break;
328                 }
329         }
330         *q = '\0';
331
332         DEBUG2("sqlcounter_expand: '%s'", out);
333
334         return strlen(out);
335 }
336
337
338 /*
339  *      See if the counter matches.
340  */
341 static int sqlcounter_cmp(void *instance, REQUEST *request, UNUSED VALUE_PAIR *req , VALUE_PAIR *check,
342                           UNUSED VALUE_PAIR *check_pairs, UNUSED VALUE_PAIR **reply_pairs)
343 {
344         rlm_sqlcounter_t *inst = instance;
345         uint64_t counter;
346
347         char query[MAX_QUERY_LEN], subst[MAX_QUERY_LEN];
348         char *expanded = NULL;
349         size_t len;
350
351         /* First, expand %k, %b and %e in query */
352         if (sqlcounter_expand(subst, sizeof(subst), inst->query, inst) <= 0) {
353                 REDEBUG("Insufficient query buffer space");
354
355                 return RLM_MODULE_FAIL;
356         }
357
358         /* Then combine that with the name of the module were using to do the query */
359         len = snprintf(query, sizeof(query), "%%{%s:%s}", inst->sqlmod_inst, subst);
360         if (len >= sizeof(query) - 1) {
361                 REDEBUG("Insufficient query buffer space");
362
363                 return RLM_MODULE_FAIL;
364         }
365
366         /* Finally, xlat resulting SQL query */
367         if (radius_axlat(&expanded, request, query, NULL, NULL) < 0) {
368                 return RLM_MODULE_FAIL;
369         }
370
371         if (sscanf(expanded, "%" PRIu64, &counter) != 1) {
372                 RDEBUG2("No integer found in string \"%s\"", expanded);
373         }
374         talloc_free(expanded);
375
376         if (counter < check->vp_integer64) {
377                 return -1;
378         }
379         if (counter > check->vp_integer64) {
380                 return 1;
381         }
382         return 0;
383 }
384
385 static int mod_bootstrap(CONF_SECTION *conf, void *instance)
386 {
387         rlm_sqlcounter_t *inst = instance;
388         DICT_ATTR const *da;
389         ATTR_FLAGS flags;
390
391         memset(&flags, 0, sizeof(flags));
392         flags.compare = 1;      /* ugly hack */
393         da = dict_attrbyname(inst->counter_name);
394         if (da && (da->type != PW_TYPE_INTEGER64)) {
395                 cf_log_err_cs(conf, "Counter attribute %s MUST be integer64", inst->counter_name);
396                 return -1;
397         }
398
399         if (!da && (dict_addattr(inst->counter_name, -1, 0, PW_TYPE_INTEGER64, flags) < 0)) {
400                 cf_log_err_cs(conf, "Failed to create counter attribute %s: %s", inst->counter_name, fr_strerror());
401                 return -1;
402         }
403
404         /*
405          *  Register the counter comparison operation.
406          */
407         if (paircompare_register_byname(inst->counter_name, NULL, true, sqlcounter_cmp, inst) < 0) {
408                 cf_log_err_cs(conf, "Failed registering counter attribute %s: %s", inst->counter_name, fr_strerror());
409                 return -1;
410         }
411
412         inst->dict_attr = dict_attrbyname(inst->counter_name);
413         if (!inst->dict_attr) {
414                 cf_log_err_cs(conf, "Failed to find counter attribute %s", inst->counter_name);
415                 return -1;
416         }
417
418         /*
419          *  Create a new attribute for the check item.
420          */
421         flags.compare = 0;
422         if ((dict_addattr(inst->limit_name, -1, 0, PW_TYPE_INTEGER64, flags) < 0) ||
423             !dict_attrbyname(inst->limit_name)) {
424                 cf_log_err_cs(conf, "Failed to create check attribute %s: %s", inst->limit_name, fr_strerror());
425                 return -1;
426         }
427
428         return 0;
429 }
430
431 /*
432  *      Do any per-module initialization that is separate to each
433  *      configured instance of the module.  e.g. set up connections
434  *      to external databases, read configuration files, set up
435  *      dictionary entries, etc.
436  *
437  *      If configuration information is given in the config section
438  *      that must be referenced in later calls, store a handle to it
439  *      in *instance otherwise put a null pointer there.
440  */
441 static int mod_instantiate(CONF_SECTION *conf, void *instance)
442 {
443         rlm_sqlcounter_t *inst = instance;
444         DICT_ATTR const *da;
445         time_t now;
446
447         rad_assert(inst->query && *inst->query);
448
449         da = dict_attrbyname(inst->key_name);
450         if (!da) {
451                 cf_log_err_cs(conf, "Invalid attribute '%s'", inst->key_name);
452                 return -1;
453         }
454         inst->key_attr = da;
455
456         da = dict_attrbyname(inst->reply_name);
457         if (!da) {
458                 cf_log_err_cs(conf, "Invalid attribute '%s'", inst->reply_name);
459                 return -1;
460         }
461         inst->reply_attr = da;
462
463         now = time(NULL);
464         inst->reset_time = 0;
465
466         if (find_next_reset(inst, now) == -1) {
467                 cf_log_err_cs(conf, "Invalid reset '%s'", inst->reset);
468                 return -1;
469         }
470
471         /*
472          *  Discover the beginning of the current time period.
473          */
474         inst->last_reset = 0;
475
476         if (find_prev_reset(inst, now) < 0) {
477                 cf_log_err_cs(conf, "Invalid reset '%s'", inst->reset);
478                 return -1;
479         }
480
481         return 0;
482 }
483
484 /*
485  *      Find the named user in this modules database.  Create the set
486  *      of attribute-value pairs to check and reply with for this user
487  *      from the database. The authentication code only needs to check
488  *      the password, the rest is done here.
489  */
490 static rlm_rcode_t CC_HINT(nonnull) mod_authorize(void *instance, REQUEST *request)
491 {
492         rlm_sqlcounter_t *inst = instance;
493         int rcode = RLM_MODULE_NOOP;
494         uint64_t counter, res;
495         DICT_ATTR const *da;
496         VALUE_PAIR *key_vp, *limit;
497         VALUE_PAIR *reply_item;
498         char msg[128];
499
500         char query[MAX_QUERY_LEN], subst[MAX_QUERY_LEN];
501         char *expanded = NULL;
502
503         size_t len;
504
505         /*
506          *      Before doing anything else, see if we have to reset
507          *      the counters.
508          */
509         if (inst->reset_time && (inst->reset_time <= request->timestamp)) {
510                 /*
511                  *      Re-set the next time and prev_time for this counters range
512                  */
513                 inst->last_reset = inst->reset_time;
514                 find_next_reset(inst,request->timestamp);
515         }
516
517         /*
518          *      Look for the key.  User-Name is special.  It means
519          *      The REAL username, after stripping.
520          */
521         if ((inst->key_attr->vendor == 0) && (inst->key_attr->attr == PW_USER_NAME)) {
522                 key_vp = request->username;
523         } else {
524                 key_vp = fr_pair_find_by_da(request->packet->vps, inst->key_attr, TAG_ANY);
525         }
526         if (!key_vp) {
527                 RWDEBUG2("Couldn't find key attribute, request:%s, doing nothing...", inst->key_attr->name);
528                 return rcode;
529         }
530
531         /*
532          *      Look for the check item
533          */
534         if ((da = dict_attrbyname(inst->limit_name)) == NULL) {
535                 return rcode;
536         }
537
538         limit = fr_pair_find_by_da(request->config, da, TAG_ANY);
539         if (limit == NULL) {
540                 /* Yes this really is 'check' as distinct from control */
541                 RWDEBUG2("Couldn't find check attribute, control:%s, doing nothing...", inst->limit_name);
542                 return rcode;
543         }
544
545         /* First, expand %k, %b and %e in query */
546         if (sqlcounter_expand(subst, sizeof(subst), inst->query, inst) <= 0) {
547                 REDEBUG("Insufficient query buffer space");
548
549                 return RLM_MODULE_FAIL;
550         }
551
552         /* Then combine that with the name of the module were using to do the query */
553         len = snprintf(query, sizeof(query), "%%{%s:%s}", inst->sqlmod_inst, subst);
554         if (len >= (sizeof(query) - 1)) {
555                 REDEBUG("Insufficient query buffer space");
556
557                 return RLM_MODULE_FAIL;
558         }
559
560         /* Finally, xlat resulting SQL query */
561         if (radius_axlat(&expanded, request, query, NULL, NULL) < 0) {
562                 return RLM_MODULE_FAIL;
563         }
564         talloc_free(expanded);
565
566         if (sscanf(expanded, "%" PRIu64, &counter) != 1) {
567                 RDEBUG2("No integer found in result string \"%s\".  May be first session, setting counter to 0",
568                         expanded);
569                 counter = 0;
570         }
571
572         /*
573          *      Check if check item > counter
574          */
575         if (limit->vp_integer64 <= counter) {
576                 /* User is denied access, send back a reply message */
577                 snprintf(msg, sizeof(msg), "Your maximum %s usage time has been reached", inst->reset);
578                 pair_make_reply("Reply-Message", msg, T_OP_EQ);
579
580                 REDEBUG2("Maximum %s usage time reached", inst->reset);
581                 REDEBUG2("Rejecting user, &control:%s value (%" PRIu64 ") is less than counter value (%" PRIu64 ")",
582                          inst->limit_name, limit->vp_integer64, counter);
583
584                 return RLM_MODULE_REJECT;
585         }
586
587         res = limit->vp_integer64 - counter;
588         RDEBUG2("Allowing user, &control:%s value (%" PRIu64 ") is greater than counter value (%" PRIu64 ")",
589                 inst->limit_name, limit->vp_integer64, counter);
590         /*
591          *      We are assuming that simultaneous-use=1. But
592          *      even if that does not happen then our user
593          *      could login at max for 2*max-usage-time Is
594          *      that acceptable?
595          */
596
597         /*
598          *      If we are near a reset then add the next
599          *      limit, so that the user will not need to login
600          *      again.  Do this only for Session-Timeout.
601          */
602         if (((inst->reply_attr->vendor == 0) && (inst->reply_attr->attr == PW_SESSION_TIMEOUT)) &&
603             inst->reset_time && (res >= (uint64_t)(inst->reset_time - request->timestamp))) {
604                 uint64_t to_reset = inst->reset_time - request->timestamp;
605
606                 RDEBUG2("Time remaining (%" PRIu64 "s) is greater than time to reset (%" PRIu64 "s).  "
607                         "Adding %" PRIu64 "s to reply value", to_reset, res, to_reset);
608                 res = to_reset + limit->vp_integer;
609         }
610
611         /*
612          *      Limit the reply attribute to the minimum of the existing value, or this new one.
613          */
614         reply_item = fr_pair_find_by_da(request->reply->vps, inst->reply_attr, TAG_ANY);
615         if (reply_item) {
616                 if (reply_item->vp_integer64 <= res) {
617                         RDEBUG2("Leaving existing &reply:%s value of %" PRIu64, inst->reply_attr->name,
618                                 reply_item->vp_integer64);
619
620                         return RLM_MODULE_OK;
621                 }
622         } else {
623                 reply_item = radius_pair_create(request->reply, &request->reply->vps, inst->reply_attr->attr,
624                                                inst->reply_attr->vendor);
625         }
626         reply_item->vp_integer64 = res;
627
628         RDEBUG2("Setting &reply:%s value to %" PRIu64, inst->reply_name, reply_item->vp_integer64);
629
630         return RLM_MODULE_OK;
631 }
632
633 /*
634  *      The module name should be the only globally exported symbol.
635  *      That is, everything else should be 'static'.
636  *
637  *      If the module needs to temporarily modify it's instantiation
638  *      data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
639  *      The server will then take care of ensuring that the module
640  *      is single-threaded.
641  */
642 extern module_t rlm_sqlcounter;
643 module_t rlm_sqlcounter = {
644         .magic          = RLM_MODULE_INIT,
645         .name           = "sqlcounter",
646         .type           = RLM_TYPE_THREAD_SAFE,
647         .inst_size      = sizeof(rlm_sqlcounter_t),
648         .config         = module_config,
649         .bootstrap      = mod_bootstrap,
650         .instantiate    = mod_instantiate,
651         .methods = {
652                 [MOD_AUTHORIZE]         = mod_authorize
653         },
654 };
655