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