document rlm_otp fd leak fix
[freeradius.git] / src / modules / rlm_expiration / rlm_expiration.c
1 /*
2  * rlm_expiration.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  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
34
35 static const char rcsid[] = "$Id$";
36
37 /*
38  *      Define a structure for our module configuration.
39  *
40  *      These variables do not need to be in a structure, but it's
41  *      a lot cleaner to do so, and a pointer to the structure can
42  *      be used as the instance handle.
43  */
44 typedef struct rlm_expiration_t {
45         char *msg;              /* The Reply-Message passed back to the user if the account is expired */
46 } rlm_expiration_t;
47
48 /*
49  *      A mapping of configuration file names to internal variables.
50  *
51  *      Note that the string is dynamically allocated, so it MUST
52  *      be freed.  When the configuration file parse re-reads the string,
53  *      it free's the old one, and strdup's the new one, placing the pointer
54  *      to the strdup'd string into 'config.string'.  This gets around
55  *      buffer over-flows.
56  */
57 static const CONF_PARSER module_config[] = {
58   { "reply-message", PW_TYPE_STRING_PTR, offsetof(rlm_expiration_t,msg),
59     NULL, "Password Has Expired\r\n"},
60   { NULL, -1, 0, NULL, NULL }
61 };
62
63 /*              
64  *      Check if account has expired, and if user may login now.
65  */               
66 static int expiration_authorize(void *instance, REQUEST *request)
67 {
68         rlm_expiration_t *data = (rlm_expiration_t *)instance;
69         VALUE_PAIR *vp, *check_item = NULL;
70         char msg[MAX_STRING_LEN];
71
72         if ((check_item = pairfind(request->config_items, PW_EXPIRATION)) != NULL){
73                 /*
74                 *      Has this user's password expired?
75                 *
76                 *      If so, remove ALL reply attributes,
77                 *      and add our own Reply-Message, saying
78                 *      why they're being rejected.
79                 */
80                 DEBUG("rlm_expiration: Checking Expiration time: '%s'",check_item->vp_strvalue);
81                 if (((time_t) check_item->lvalue) <= request->timestamp) {
82                         char logstr[MAX_STRING_LEN];
83                         VALUE_PAIR *module_fmsg_vp;
84
85                         DEBUG("rlm_expiration: Account has expired");
86
87                         if (data->msg){
88                                 if (!radius_xlat(msg, sizeof(msg), data->msg, request, NULL)) {
89                                         radlog(L_ERR, "rlm_expiration: xlat failed.");
90                                         return RLM_MODULE_FAIL;
91                                 }
92
93                                 vp = pairmake("Reply-Message", msg, T_OP_ADD);
94                                 pairfree(&request->reply->vps);
95                                 request->reply->vps = vp;
96                         }
97                         snprintf(logstr, sizeof(logstr), "Account has expired [Expiration %s]",check_item->vp_strvalue);
98                         module_fmsg_vp = pairmake("Module-Failure-Message", logstr, T_OP_EQ);
99                         pairadd(&request->packet->vps, module_fmsg_vp);
100
101                         return RLM_MODULE_USERLOCK;
102                 }
103                 /*
104                  *      Else the account hasn't expired, but it may do so
105                  *      in the future.  Set Session-Timeout.
106                  */
107                 vp = pairfind(request->reply->vps, PW_SESSION_TIMEOUT);
108                 if (!vp) {
109                         vp = paircreate(PW_SESSION_TIMEOUT, PW_TYPE_INTEGER);
110                         if (!vp) {
111                                 radlog(L_ERR|L_CONS, "No memory!");
112                                 return RLM_MODULE_FAIL;
113                         }
114                         pairadd(&request->reply->vps, vp);
115                         vp->lvalue = (uint32_t) (((time_t) check_item->lvalue) - request->timestamp);
116
117                 } else if (vp->lvalue > ((uint32_t) (((time_t) check_item->lvalue) - request->timestamp))) {
118                         vp->lvalue = (uint32_t) (((time_t) check_item->lvalue) - request->timestamp);
119                 }
120         }
121         else
122                 return RLM_MODULE_NOOP;
123
124         return RLM_MODULE_OK;
125 }
126
127 /*
128  *      Compare the expiration date.
129  */
130 static int expirecmp(void *instance, REQUEST *req,
131                 VALUE_PAIR *request, VALUE_PAIR *check,
132                 VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs)
133 {
134         time_t now = 0;
135         instance = instance;
136         request = request;      /* shut the compiler up */
137         check_pairs = check_pairs;
138         reply_pairs = reply_pairs;
139
140         now = (req) ? req->timestamp : time(NULL);
141   
142         if (now <= ((time_t) check->lvalue))
143                 return 0;
144         return +1;
145 }
146
147
148 static int expiration_detach(void *instance)
149 {
150         rlm_expiration_t *data = (rlm_expiration_t *) instance;
151
152         paircompare_unregister(PW_CURRENT_TIME, expirecmp);
153         if (data->msg)
154                 free(data->msg);
155         free(instance);
156         return 0;
157 }
158
159 /*
160  *      Do any per-module initialization that is separate to each
161  *      configured instance of the module.  e.g. set up connections
162  *      to external databases, read configuration files, set up
163  *      dictionary entries, etc.
164  *
165  *      If configuration information is given in the config section
166  *      that must be referenced in later calls, store a handle to it
167  *      in *instance otherwise put a null pointer there.
168  */
169 static int expiration_instantiate(CONF_SECTION *conf, void **instance)
170 {
171         rlm_expiration_t *data;
172
173         /*
174          *      Set up a storage area for instance data
175          */
176         data = rad_malloc(sizeof(*data));
177         if (!data) {
178                 radlog(L_ERR, "rlm_expiration: rad_malloc() failed.");
179                 return -1;
180         }
181         memset(data, 0, sizeof(*data));
182
183         /*
184          *      If the configuration parameters can't be parsed, then
185          *      fail.
186          */
187         if (cf_section_parse(conf, data, module_config) < 0) {
188                 free(data);
189                 radlog(L_ERR, "rlm_expiration: Configuration parsing failed.");
190                 return -1;
191         }
192
193         /*
194          * If we are passed an empty reply-message don't use it
195          */
196         if (!strlen(data->msg)){
197                 free(data->msg);
198                 data->msg = NULL;
199         }
200
201         /*
202          * Register the expiration comparison operation.
203          */
204         paircompare_register(PW_EXPIRATION, 0, expirecmp, data);
205
206         *instance = data;
207
208         return 0;
209 }
210
211 /*
212  *      The module name should be the only globally exported symbol.
213  *      That is, everything else should be 'static'.
214  *
215  *      If the module needs to temporarily modify it's instantiation
216  *      data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
217  *      The server will then take care of ensuring that the module
218  *      is single-threaded.
219  */
220 module_t rlm_expiration = {
221         RLM_MODULE_INIT,
222         "expiration",
223         RLM_TYPE_THREAD_SAFE,           /* type */
224         expiration_instantiate,         /* instantiation */
225         expiration_detach,              /* detach */
226         {
227                 NULL,                   /* authentication */
228                 expiration_authorize,   /* authorization */
229                 NULL,                   /* preaccounting */
230                 NULL,                   /* accounting */
231                 NULL,                   /* checksimul */
232                 NULL,                   /* pre-proxy */
233                 NULL,                   /* post-proxy */
234                 NULL                    /* post-auth */
235         },
236 };