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