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.
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.
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
20 * Copyright 2001 The FreeRADIUS server project
21 * Copyright 2004 Kostas Kalevras <kkalev@noc.ntua.gr>
24 #include <freeradius-devel/autoconf.h>
31 #include <freeradius-devel/radiusd.h>
32 #include <freeradius-devel/modules.h>
33 #include <freeradius-devel/conffile.h>
36 static const char rcsid[] = "$Id$";
39 * Define a structure for our module configuration.
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.
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 */
52 * A mapping of configuration file names to internal variables.
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
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 }
67 static int logintime_detach(void *instance);
70 * Compare the current time to a range.
72 static int timecmp(void *instance,
74 VALUE_PAIR *request, VALUE_PAIR *check,
75 VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs)
78 request = request; /* shut the compiler up */
79 check_pairs = check_pairs;
80 reply_pairs = reply_pairs;
83 * If there's a request, use that timestamp.
85 if (timestr_match((char *)check->vp_strvalue,
86 req ? req->timestamp : time(NULL)) >= 0)
96 static int time_of_day(void *instance,
98 VALUE_PAIR *request, VALUE_PAIR *check,
99 VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs)
107 request = request; /* shut the compiler up */
108 check_pairs = check_pairs;
109 reply_pairs = reply_pairs;
112 * Must be called with a request pointer.
116 if (strspn(check->vp_strvalue, "0123456789: ") != strlen(check->vp_strvalue)) {
117 DEBUG("rlm_logintime: Bad Time-Of-Day value \"%s\"",
122 tm = localtime_r(&req->timestamp, &s_tm);
123 hhmmss = (tm->tm_hour * 3600) + (tm->tm_min * 60) + tm->tm_sec;
126 * Time of day is a 24-hour clock
128 p = check->vp_strvalue;
131 if ((scan > 23) || !p) {
132 DEBUG("rlm_logintime: Bad Time-Of-Day value \"%s\"",
141 DEBUG("rlm_logintime: Bad Time-Of-Day value \"%s\"",
151 DEBUG("rlm_logintime: Bad Time-Of-Day value \"%s\"",
158 fprintf(stderr, "returning %d - %d\n",
161 return hhmmss - when;
165 * Check if account has expired, and if user may login now.
167 static int logintime_authorize(void *instance, REQUEST *request)
169 rlm_logintime_t *data = (rlm_logintime_t *)instance;
170 VALUE_PAIR *check_item = NULL;
173 if ((check_item = pairfind(request->config_items, PW_LOGIN_TIME)) != NULL) {
176 * Authentication is OK. Now see if this
177 * user may login at this time of the day.
179 DEBUG("rlm_logintime: Checking Login-Time: '%s'",check_item->vp_strvalue);
180 r = timestr_match((char *)check_item->vp_strvalue,
182 if (r == 0) { /* unlimited */
184 * Do nothing: login-time is OK.
188 * Session-Timeout needs to be at least
189 * 60 seconds, some terminal servers
190 * ignore smaller values.
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;
198 * User called outside allowed time interval.
201 DEBUG("rlm_logintime: timestr returned reject");
203 char msg[MAX_STRING_LEN];
206 if (!radius_xlat(msg, sizeof(msg), data->msg, request, NULL)) {
207 radlog(L_ERR, "rlm_logintime: xlat failed.");
208 return RLM_MODULE_FAIL;
210 pairfree(&request->reply->vps);
211 tmp = pairmake("Reply-Message", msg, T_OP_SET);
212 request->reply->vps = tmp;
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);
220 return RLM_MODULE_REJECT;
223 VALUE_PAIR *reply_item;
226 * User is allowed, but set Session-Timeout.
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;
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;
237 reply_item->lvalue = r;
238 pairadd(&request->reply->vps, reply_item);
240 DEBUG("rlm_logintime: Session-Timeout set to: %d",r);
244 return RLM_MODULE_NOOP;
246 return RLM_MODULE_OK;
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.
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.
260 static int logintime_instantiate(CONF_SECTION *conf, void **instance)
262 rlm_logintime_t *data;
265 * Set up a storage area for instance data
267 data = rad_malloc(sizeof(*data));
269 radlog(L_ERR, "rlm_logintime: rad_malloc() failed.");
272 memset(data, 0, sizeof(*data));
275 * If the configuration parameters can't be parsed, then
278 if (cf_section_parse(conf, data, module_config) < 0) {
280 radlog(L_ERR, "rlm_logintime: Configuration parsing failed.");
285 * If we are passed an empty reply-message don't use it
287 if (!strlen(data->msg)){
292 if (data->min_time == 0){
293 radlog(L_ERR, "rlm_logintime: Minimum timeout should be non zero.");
300 * Register a Current-Time comparison function
302 paircompare_register(PW_CURRENT_TIME, 0, timecmp, data);
303 paircompare_register(PW_TIME_OF_DAY, 0, time_of_day, data);
310 static int logintime_detach(void *instance)
312 rlm_logintime_t *data = (rlm_logintime_t *) instance;
314 paircompare_unregister(PW_CURRENT_TIME, timecmp);
322 * The module name should be the only globally exported symbol.
323 * That is, everything else should be 'static'.
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.
330 module_t rlm_logintime = {
333 RLM_TYPE_THREAD_SAFE, /* type */
334 logintime_instantiate, /* instantiation */
335 logintime_detach, /* detach */
337 NULL, /* authentication */
338 logintime_authorize, /* authorization */
339 NULL, /* preaccounting */
340 NULL, /* accounting */
341 NULL, /* checksimul */
342 NULL, /* pre-proxy */
343 NULL, /* post-proxy */