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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * Copyright 2001,2006 The FreeRADIUS server project
21 * Copyright 2004 Kostas Kalevras <kkalev@noc.ntua.gr>
24 #include <freeradius-devel/ident.h>
27 #include <freeradius-devel/radiusd.h>
28 #include <freeradius-devel/modules.h>
33 * Define a structure for our module configuration.
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.
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 */
46 * A mapping of configuration file names to internal variables.
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
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 }
61 static int logintime_detach(void *instance);
64 * Compare the current time to a range.
66 static int timecmp(void *instance,
68 VALUE_PAIR *request, VALUE_PAIR *check,
69 VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs)
72 request = request; /* shut the compiler up */
73 check_pairs = check_pairs;
74 reply_pairs = reply_pairs;
77 * If there's a request, use that timestamp.
79 if (timestr_match((char *)check->vp_strvalue,
80 req ? req->timestamp : time(NULL)) >= 0)
90 static int time_of_day(void *instance,
92 VALUE_PAIR *request, VALUE_PAIR *check,
93 VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs)
101 request = request; /* shut the compiler up */
102 check_pairs = check_pairs;
103 reply_pairs = reply_pairs;
106 * Must be called with a request pointer.
110 if (strspn(check->vp_strvalue, "0123456789: ") != strlen(check->vp_strvalue)) {
111 DEBUG("rlm_logintime: Bad Time-Of-Day value \"%s\"",
116 tm = localtime_r(&req->timestamp, &s_tm);
117 hhmmss = (tm->tm_hour * 3600) + (tm->tm_min * 60) + tm->tm_sec;
120 * Time of day is a 24-hour clock
122 p = check->vp_strvalue;
125 if ((scan > 23) || !p) {
126 DEBUG("rlm_logintime: Bad Time-Of-Day value \"%s\"",
135 DEBUG("rlm_logintime: Bad Time-Of-Day value \"%s\"",
145 DEBUG("rlm_logintime: Bad Time-Of-Day value \"%s\"",
152 fprintf(stderr, "returning %d - %d\n",
155 return hhmmss - when;
159 * Check if account has expired, and if user may login now.
161 static int logintime_authorize(void *instance, REQUEST *request)
163 rlm_logintime_t *data = (rlm_logintime_t *)instance;
164 VALUE_PAIR *check_item = NULL;
167 if ((check_item = pairfind(request->config_items, PW_LOGIN_TIME)) != NULL) {
170 * Authentication is OK. Now see if this
171 * user may login at this time of the day.
173 DEBUG("rlm_logintime: Checking Login-Time: '%s'",check_item->vp_strvalue);
174 r = timestr_match((char *)check_item->vp_strvalue,
176 if (r == 0) { /* unlimited */
178 * Do nothing: login-time is OK.
182 * Session-Timeout needs to be at least
183 * 60 seconds, some terminal servers
184 * ignore smaller values.
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;
192 * User called outside allowed time interval.
195 DEBUG("rlm_logintime: timestr returned reject");
196 if (data->msg && data->msg[0]){
197 char msg[MAX_STRING_LEN];
200 if (!radius_xlat(msg, sizeof(msg), data->msg, request, NULL)) {
201 radlog(L_ERR, "rlm_logintime: xlat failed.");
202 return RLM_MODULE_FAIL;
204 pairfree(&request->reply->vps);
205 tmp = pairmake("Reply-Message", msg, T_OP_SET);
206 request->reply->vps = tmp;
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);
214 return RLM_MODULE_REJECT;
217 VALUE_PAIR *reply_item;
220 * User is allowed, but set Session-Timeout.
222 DEBUG("rlm_logintime: timestr returned accept");
223 if ((reply_item = pairfind(request->reply->vps, PW_SESSION_TIMEOUT)) != NULL) {
224 if (reply_item->vp_integer > (unsigned) r)
225 reply_item->vp_integer = r;
227 reply_item = radius_paircreate(request,
228 &request->reply->vps,
231 reply_item->vp_integer = r;
233 DEBUG("rlm_logintime: Session-Timeout set to: %d",r);
237 return RLM_MODULE_NOOP;
239 return RLM_MODULE_OK;
244 * Do any per-module initialization that is separate to each
245 * configured instance of the module. e.g. set up connections
246 * to external databases, read configuration files, set up
247 * dictionary entries, etc.
249 * If configuration information is given in the config section
250 * that must be referenced in later calls, store a handle to it
251 * in *instance otherwise put a null pointer there.
253 static int logintime_instantiate(CONF_SECTION *conf, void **instance)
255 rlm_logintime_t *data;
258 * Set up a storage area for instance data
260 data = rad_malloc(sizeof(*data));
262 radlog(L_ERR, "rlm_logintime: rad_malloc() failed.");
265 memset(data, 0, sizeof(*data));
268 * If the configuration parameters can't be parsed, then
271 if (cf_section_parse(conf, data, module_config) < 0) {
273 radlog(L_ERR, "rlm_logintime: Configuration parsing failed.");
277 if (data->min_time == 0){
278 radlog(L_ERR, "rlm_logintime: Minimum timeout should be non zero.");
284 * Register a Current-Time comparison function
286 paircompare_register(PW_CURRENT_TIME, 0, timecmp, data);
287 paircompare_register(PW_TIME_OF_DAY, 0, time_of_day, data);
294 static int logintime_detach(void *instance)
296 rlm_logintime_t *data = (rlm_logintime_t *) instance;
298 paircompare_unregister(PW_CURRENT_TIME, timecmp);
299 paircompare_unregister(PW_TIME_OF_DAY, time_of_day);
305 * The module name should be the only globally exported symbol.
306 * That is, everything else should be 'static'.
308 * If the module needs to temporarily modify it's instantiation
309 * data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
310 * The server will then take care of ensuring that the module
311 * is single-threaded.
313 module_t rlm_logintime = {
316 RLM_TYPE_THREAD_SAFE, /* type */
317 logintime_instantiate, /* instantiation */
318 logintime_detach, /* detach */
320 NULL, /* authentication */
321 logintime_authorize, /* authorization */
322 NULL, /* preaccounting */
323 NULL, /* accounting */
324 NULL, /* checksimul */
325 NULL, /* pre-proxy */
326 NULL, /* post-proxy */