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