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>
25 #include "libradius.h"
37 static const char rcsid[] = "$Id$";
40 * Define a structure for our module configuration.
42 * These variables do not need to be in a structure, but it's
43 * a lot cleaner to do so, and a pointer to the structure can
44 * be used as the instance handle.
46 typedef struct rlm_logintime_t {
47 char *msg; /* The Reply-Message passed back to the user
48 * if the account is outside allowed timestamp */
53 * A mapping of configuration file names to internal variables.
55 * Note that the string is dynamically allocated, so it MUST
56 * be freed. When the configuration file parse re-reads the string,
57 * it free's the old one, and strdup's the new one, placing the pointer
58 * to the strdup'd string into 'config.string'. This gets around
61 static CONF_PARSER module_config[] = {
62 { "reply-message", PW_TYPE_STRING_PTR, offsetof(rlm_logintime_t,msg), NULL,
63 "You are calling outside your allowed timespan\r\n"},
64 { "minimum-timeout", PW_TYPE_INTEGER, offsetof(rlm_logintime_t,min_time), NULL, "60" },
65 { NULL, -1, 0, NULL, NULL }
68 static int logintime_detach(void *instance);
71 * Compare the current time to a range.
73 static int timecmp(void *instance,
75 VALUE_PAIR *request, VALUE_PAIR *check,
76 VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs)
79 request = request; /* shut the compiler up */
80 check_pairs = check_pairs;
81 reply_pairs = reply_pairs;
84 * If there's a request, use that timestamp.
86 if (timestr_match((char *)check->strvalue,
87 req ? req->timestamp : time(NULL)) >= 0)
94 * Check if account has expired, and if user may login now.
96 static int logintime_authorize(void *instance, REQUEST *request)
98 rlm_logintime_t *data = (rlm_logintime_t *)instance;
99 VALUE_PAIR *check_item = NULL;
102 if ((check_item = pairfind(request->config_items, PW_LOGIN_TIME)) != NULL) {
105 * Authentication is OK. Now see if this
106 * user may login at this time of the day.
108 DEBUG("rlm_logintime: Checking Login-Time: '%s'",check_item->strvalue);
109 r = timestr_match((char *)check_item->strvalue,
111 if (r == 0) { /* unlimited */
113 * Do nothing: login-time is OK.
117 * Session-Timeout needs to be at least
118 * 60 seconds, some terminal servers
119 * ignore smaller values.
121 DEBUG("rlm_logintime: timestr returned unlimited");
122 } else if (r < data->min_time) {
123 char logstr[MAX_STRING_LEN];
124 VALUE_PAIR *module_fmsg_vp;
127 * User called outside allowed time interval.
130 DEBUG("rlm_logintime: timestr returned reject");
132 char msg[MAX_STRING_LEN];
135 if (!radius_xlat(msg, sizeof(msg), data->msg, request, NULL)) {
136 radlog(L_ERR, "rlm_logintime: xlat failed.");
137 return RLM_MODULE_FAIL;
139 pairfree(&request->reply->vps);
140 tmp = pairmake("Reply-Message", msg, T_OP_SET);
141 request->reply->vps = tmp;
144 snprintf(logstr, sizeof(logstr), "Outside allowed timespan (time allowed %s)",
145 check_item->strvalue);
146 module_fmsg_vp = pairmake("Module-Failure-Message", logstr, T_OP_EQ);
147 pairadd(&request->packet->vps, module_fmsg_vp);
149 return RLM_MODULE_REJECT;
152 VALUE_PAIR *reply_item;
155 * User is allowed, but set Session-Timeout.
157 DEBUG("rlm_logintime: timestr returned accept");
158 if ((reply_item = pairfind(request->reply->vps, PW_SESSION_TIMEOUT)) != NULL) {
159 if (reply_item->lvalue > (unsigned) r)
160 reply_item->lvalue = r;
162 if ((reply_item = paircreate( PW_SESSION_TIMEOUT, PW_TYPE_INTEGER)) == NULL) {
163 radlog(L_ERR|L_CONS, "no memory");
164 return RLM_MODULE_FAIL;
166 reply_item->lvalue = r;
167 pairadd(&request->reply->vps, reply_item);
169 DEBUG("rlm_logintime: Session-Timeout set to: %d",r);
173 return RLM_MODULE_NOOP;
175 return RLM_MODULE_OK;
180 * Do any per-module initialization that is separate to each
181 * configured instance of the module. e.g. set up connections
182 * to external databases, read configuration files, set up
183 * dictionary entries, etc.
185 * If configuration information is given in the config section
186 * that must be referenced in later calls, store a handle to it
187 * in *instance otherwise put a null pointer there.
189 static int logintime_instantiate(CONF_SECTION *conf, void **instance)
191 rlm_logintime_t *data;
194 * Set up a storage area for instance data
196 data = rad_malloc(sizeof(*data));
198 radlog(L_ERR, "rlm_logintime: rad_malloc() failed.");
201 memset(data, 0, sizeof(*data));
204 * If the configuration parameters can't be parsed, then
207 if (cf_section_parse(conf, data, module_config) < 0) {
209 radlog(L_ERR, "rlm_logintime: Configuration parsing failed.");
214 * If we are passed an empty reply-message don't use it
216 if (!strlen(data->msg)){
221 if (data->min_time == 0){
222 radlog(L_ERR, "rlm_logintime: Minimum timeout should be non zero.");
229 * Register a Current-Time comparison function
231 paircompare_register(PW_CURRENT_TIME, 0, timecmp, data);
238 static int logintime_detach(void *instance)
240 rlm_logintime_t *data = (rlm_logintime_t *) instance;
242 paircompare_unregister(PW_CURRENT_TIME, timecmp);
250 * The module name should be the only globally exported symbol.
251 * That is, everything else should be 'static'.
253 * If the module needs to temporarily modify it's instantiation
254 * data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
255 * The server will then take care of ensuring that the module
256 * is single-threaded.
258 module_t rlm_logintime = {
260 RLM_TYPE_THREAD_SAFE, /* type */
261 NULL, /* initialization */
262 logintime_instantiate, /* instantiation */
264 NULL, /* authentication */
265 logintime_authorize, /* authorization */
266 NULL, /* preaccounting */
267 NULL, /* accounting */
268 NULL, /* checksimul */
269 NULL, /* pre-proxy */
270 NULL, /* post-proxy */
273 logintime_detach, /* detach */