document rlm_otp fd leak fix
[freeradius.git] / src / modules / rlm_logintime / rlm_logintime.c
1 /*
2  * rlm_logintime.c
3  *
4  * Version:  $Id$
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program; if not, write to the Free Software
18  *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  * Copyright 2001  The FreeRADIUS server project
21  * Copyright 2004  Kostas Kalevras <kkalev@noc.ntua.gr>
22  */
23
24 #include <freeradius-devel/autoconf.h>
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <ctype.h>
30
31 #include <freeradius-devel/radiusd.h>
32 #include <freeradius-devel/modules.h>
33
34
35 static const char rcsid[] = "$Id$";
36
37 /*
38  *      Define a structure for our module configuration.
39  *
40  *      These variables do not need to be in a structure, but it's
41  *      a lot cleaner to do so, and a pointer to the structure can
42  *      be used as the instance handle.
43  */
44 typedef struct rlm_logintime_t {
45         char *msg;              /* The Reply-Message passed back to the user 
46                                  * if the account is outside allowed timestamp */
47         int min_time;
48 } rlm_logintime_t;
49
50 /*
51  *      A mapping of configuration file names to internal variables.
52  *
53  *      Note that the string is dynamically allocated, so it MUST
54  *      be freed.  When the configuration file parse re-reads the string,
55  *      it free's the old one, and strdup's the new one, placing the pointer
56  *      to the strdup'd string into 'config.string'.  This gets around
57  *      buffer over-flows.
58  */
59 static const CONF_PARSER module_config[] = {
60   { "reply-message", PW_TYPE_STRING_PTR, offsetof(rlm_logintime_t,msg), NULL, 
61         "You are calling outside your allowed timespan\r\n"},
62   { "minimum-timeout", PW_TYPE_INTEGER, offsetof(rlm_logintime_t,min_time), NULL, "60" },
63   { NULL, -1, 0, NULL, NULL }
64 };
65
66 static int logintime_detach(void *instance);
67
68 /*
69  *      Compare the current time to a range.
70  */
71 static int timecmp(void *instance,
72                 REQUEST *req,
73                 VALUE_PAIR *request, VALUE_PAIR *check,
74                 VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs)
75
76         instance = instance;
77         request = request;      /* shut the compiler up */
78         check_pairs = check_pairs;
79         reply_pairs = reply_pairs;
80   
81         /*
82          *      If there's a request, use that timestamp.       
83          */
84         if (timestr_match((char *)check->vp_strvalue,
85         req ? req->timestamp : time(NULL)) >= 0)
86                 return 0;
87
88         return -1;
89 }
90
91
92 /*
93  *      Time-Of-Day support
94  */
95 static int time_of_day(void *instance,
96                        REQUEST *req,
97                        VALUE_PAIR *request, VALUE_PAIR *check,
98                        VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs)
99 {
100         int scan;
101         int hhmmss, when;
102         char *p;
103         struct tm *tm, s_tm;
104
105         instance = instance;
106         request = request;      /* shut the compiler up */
107         check_pairs = check_pairs;
108         reply_pairs = reply_pairs;
109
110         /*
111          *      Must be called with a request pointer.
112          */
113         if (!req) return -1;
114   
115         if (strspn(check->vp_strvalue, "0123456789: ") != strlen(check->vp_strvalue)) {
116                 DEBUG("rlm_logintime: Bad Time-Of-Day value \"%s\"",
117                       check->vp_strvalue);
118                 return -1;
119         }
120
121         tm = localtime_r(&req->timestamp, &s_tm);
122         hhmmss = (tm->tm_hour * 3600) + (tm->tm_min * 60) + tm->tm_sec;
123
124         /*
125          *      Time of day is a 24-hour clock
126          */
127         p = check->vp_strvalue;
128         scan = atoi(p);
129         p = strchr(p, ':');
130         if ((scan > 23) || !p) {
131                 DEBUG("rlm_logintime: Bad Time-Of-Day value \"%s\"",
132                       check->vp_strvalue);
133                 return -1;
134         }
135         when = scan * 3600;
136         p++;
137
138         scan = atoi(p);
139         if (scan > 59) {
140                 DEBUG("rlm_logintime: Bad Time-Of-Day value \"%s\"",
141                       check->vp_strvalue);
142                 return -1;
143         }
144         when += scan * 60;
145
146         p = strchr(p, ':');
147         if (p) {
148                 scan = atoi(p + 1);
149                 if (scan > 59) {
150                         DEBUG("rlm_logintime: Bad Time-Of-Day value \"%s\"",
151                               check->vp_strvalue);
152                         return -1;
153                 }
154                 when += scan;
155         }
156
157         fprintf(stderr, "returning %d - %d\n",
158                 hhmmss, when);
159         
160         return hhmmss - when;
161 }
162
163 /*              
164  *      Check if account has expired, and if user may login now.
165  */               
166 static int logintime_authorize(void *instance, REQUEST *request)
167 {
168         rlm_logintime_t *data = (rlm_logintime_t *)instance;
169         VALUE_PAIR *check_item = NULL;
170         int r;
171
172         if ((check_item = pairfind(request->config_items, PW_LOGIN_TIME)) != NULL) {
173  
174                 /*
175                  *      Authentication is OK. Now see if this
176                  *      user may login at this time of the day.
177                  */
178                 DEBUG("rlm_logintime: Checking Login-Time: '%s'",check_item->vp_strvalue);
179                 r = timestr_match((char *)check_item->vp_strvalue,
180                 request->timestamp);
181                 if (r == 0) {   /* unlimited */
182                         /*
183                          *      Do nothing: login-time is OK.
184                          */
185
186                 /*
187                  *      Session-Timeout needs to be at least
188                  *      60 seconds, some terminal servers
189                  *      ignore smaller values.
190                  */
191                         DEBUG("rlm_logintime: timestr returned unlimited");
192                 } else if (r < data->min_time) {
193                         char logstr[MAX_STRING_LEN];
194                         VALUE_PAIR *module_fmsg_vp;
195
196                         /*
197                          *      User called outside allowed time interval.
198                          */
199                 
200                         DEBUG("rlm_logintime: timestr returned reject");
201                         if (data->msg){
202                                 char msg[MAX_STRING_LEN];
203                                 VALUE_PAIR *tmp;
204
205                                 if (!radius_xlat(msg, sizeof(msg), data->msg, request, NULL)) {
206                                         radlog(L_ERR, "rlm_logintime: xlat failed.");
207                                         return RLM_MODULE_FAIL;
208                                 }
209                                 pairfree(&request->reply->vps);
210                                 tmp = pairmake("Reply-Message", msg, T_OP_SET);
211                                 request->reply->vps = tmp;
212                         }
213
214                         snprintf(logstr, sizeof(logstr), "Outside allowed timespan (time allowed %s)",
215                         check_item->vp_strvalue);
216                         module_fmsg_vp = pairmake("Module-Failure-Message", logstr, T_OP_EQ);
217                         pairadd(&request->packet->vps, module_fmsg_vp);
218
219                         return RLM_MODULE_REJECT;
220
221                 } else if (r > 0) {
222                         VALUE_PAIR *reply_item;
223
224                         /*
225                          *      User is allowed, but set Session-Timeout.
226                          */
227                         DEBUG("rlm_logintime: timestr returned accept");
228                         if ((reply_item = pairfind(request->reply->vps, PW_SESSION_TIMEOUT)) != NULL) {
229                                 if (reply_item->lvalue > (unsigned) r)
230                                         reply_item->lvalue = r;
231                         } else {
232                                 if ((reply_item = paircreate( PW_SESSION_TIMEOUT, PW_TYPE_INTEGER)) == NULL) {
233                                         radlog(L_ERR|L_CONS, "no memory");
234                                         return RLM_MODULE_FAIL;
235                                 }
236                                 reply_item->lvalue = r;
237                                 pairadd(&request->reply->vps, reply_item);
238                         }
239                         DEBUG("rlm_logintime: Session-Timeout set to: %d",r);
240                 }
241         }
242         else
243                 return RLM_MODULE_NOOP;
244
245         return RLM_MODULE_OK;
246 }
247
248
249 /*
250  *      Do any per-module initialization that is separate to each
251  *      configured instance of the module.  e.g. set up connections
252  *      to external databases, read configuration files, set up
253  *      dictionary entries, etc.
254  *
255  *      If configuration information is given in the config section
256  *      that must be referenced in later calls, store a handle to it
257  *      in *instance otherwise put a null pointer there.
258  */
259 static int logintime_instantiate(CONF_SECTION *conf, void **instance)
260 {
261         rlm_logintime_t *data;
262
263         /*
264          *      Set up a storage area for instance data
265          */
266         data = rad_malloc(sizeof(*data));
267         if (!data) {
268                 radlog(L_ERR, "rlm_logintime: rad_malloc() failed.");
269                 return -1;
270         }
271         memset(data, 0, sizeof(*data));
272
273         /*
274          *      If the configuration parameters can't be parsed, then
275          *      fail.
276          */
277         if (cf_section_parse(conf, data, module_config) < 0) {
278                 free(data);
279                 radlog(L_ERR, "rlm_logintime: Configuration parsing failed.");
280                 return -1;
281         }
282
283         /*
284          * If we are passed an empty reply-message don't use it
285          */
286         if (!strlen(data->msg)){
287                 free(data->msg);
288                 data->msg = NULL;
289         }
290
291         if (data->min_time == 0){
292                 radlog(L_ERR, "rlm_logintime: Minimum timeout should be non zero.");
293                 free(data->msg);
294                 free(data);
295                 return -1;
296         }
297
298         /*
299          * Register a Current-Time comparison function
300          */
301         paircompare_register(PW_CURRENT_TIME, 0, timecmp, data);
302         paircompare_register(PW_TIME_OF_DAY, 0, time_of_day, data);
303
304         *instance = data;
305
306         return 0;
307 }
308
309 static int logintime_detach(void *instance)
310 {
311         rlm_logintime_t *data = (rlm_logintime_t *) instance;
312
313         paircompare_unregister(PW_CURRENT_TIME, timecmp);
314         if (data->msg)
315                 free(data->msg);
316         free(instance);
317         return 0;
318 }
319
320 /*
321  *      The module name should be the only globally exported symbol.
322  *      That is, everything else should be 'static'.
323  *
324  *      If the module needs to temporarily modify it's instantiation
325  *      data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
326  *      The server will then take care of ensuring that the module
327  *      is single-threaded.
328  */
329 module_t rlm_logintime = {
330         RLM_MODULE_INIT,
331         "logintime",
332         RLM_TYPE_THREAD_SAFE,           /* type */
333         logintime_instantiate,          /* instantiation */
334         logintime_detach,               /* detach */
335         {
336                 NULL,                   /* authentication */
337                 logintime_authorize,    /* authorization */
338                 NULL,                   /* preaccounting */
339                 NULL,                   /* accounting */
340                 NULL,                   /* checksimul */
341                 NULL,                   /* pre-proxy */
342                 NULL,                   /* post-proxy */
343                 NULL                    /* post-auth */
344         },
345 };