document rlm_otp fd leak fix
[freeradius.git] / src / modules / rlm_always / rlm_always.c
1 /*
2  * rlm_always.c
3  *
4  *   This program is free software; you can redistribute it and/or modify
5  *   it under the terms of the GNU General Public License as published by
6  *   the Free Software Foundation; either version 2 of the License, or
7  *   (at your option) any later version.
8  *
9  *   This program is distributed in the hope that it will be useful,
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *   GNU General Public License for more details.
13  *
14  *   You should have received a copy of the GNU General Public License
15  *   along with this program; if not, write to the Free Software
16  *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  *
18  * Copyright 2000  The FreeRADIUS server project
19  */
20
21 #include <freeradius-devel/autoconf.h>
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include <freeradius-devel/radiusd.h>
28 #include <freeradius-devel/modules.h>
29
30 static const char rcsid[] = "$Id$";
31
32 /*
33  *      The instance data for rlm_always is the list of fake values we are
34  *      going to return.
35  */
36 typedef struct rlm_always_t {
37         char    *rcode_str;
38         int     rcode;
39         int     simulcount;
40         int     mpp;
41 } rlm_always_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   { "rcode",      PW_TYPE_STRING_PTR, offsetof(rlm_always_t,rcode_str),
54     NULL, "fail" },
55   { "simulcount", PW_TYPE_INTEGER,    offsetof(rlm_always_t,simulcount),
56     NULL, "0" },
57   { "mpp",        PW_TYPE_BOOLEAN,    offsetof(rlm_always_t,mpp),
58     NULL, "no" },
59
60   { NULL, -1, 0, NULL, NULL }           /* end the list */
61 };
62
63 static int str2rcode(const char *s)
64 {
65         if(!strcasecmp(s, "reject"))
66                 return RLM_MODULE_REJECT;
67         else if(!strcasecmp(s, "fail"))
68                 return RLM_MODULE_FAIL;
69         else if(!strcasecmp(s, "ok"))
70                 return RLM_MODULE_OK;
71         else if(!strcasecmp(s, "handled"))
72                 return RLM_MODULE_HANDLED;
73         else if(!strcasecmp(s, "invalid"))
74                 return RLM_MODULE_INVALID;
75         else if(!strcasecmp(s, "userlock"))
76                 return RLM_MODULE_USERLOCK;
77         else if(!strcasecmp(s, "notfound"))
78                 return RLM_MODULE_NOTFOUND;
79         else if(!strcasecmp(s, "noop"))
80                 return RLM_MODULE_NOOP;
81         else if(!strcasecmp(s, "updated"))
82                 return RLM_MODULE_UPDATED;
83         else {
84                 radlog(L_ERR|L_CONS,
85                         "rlm_always: Unknown module rcode '%s'.\n", s);
86                 return -1;
87         }
88 }
89
90 static int always_instantiate(CONF_SECTION *conf, void **instance)
91 {
92         rlm_always_t *data;
93
94         /*
95          *      Set up a storage area for instance data
96          */
97         data = rad_malloc(sizeof(*data));
98         if (!data) {
99                 return -1;
100         }
101         memset(data, 0, sizeof(*data));
102
103         /*
104          *      If the configuration parameters can't be parsed, then
105          *      fail.
106          */
107         if (cf_section_parse(conf, data, module_config) < 0) {
108                 free(data);
109                 return -1;
110         }
111
112         /*
113          *      Convert the rcode string to an int, and get rid of it
114          */
115         data->rcode = str2rcode(data->rcode_str);
116         free(data->rcode_str);
117         data->rcode_str = NULL;
118         if (data->rcode == -1) {
119                 free(data);
120                 return -1;
121         }
122
123         *instance = data;
124
125         return 0;
126 }
127
128 /*
129  *      Just return the rcode ... this function is autz, auth, acct, and
130  *      preacct!
131  */
132 static int always_return(void *instance, REQUEST *request)
133 {
134         /* quiet the compiler */
135         request = request;
136
137         return ((struct rlm_always_t *)instance)->rcode;
138 }
139
140 /*
141  *      checksimul fakes some other variables besides the rcode...
142  */
143 static int always_checksimul(void *instance, REQUEST *request)
144 {
145         struct rlm_always_t *inst = instance;
146
147         request->simul_count = inst->simulcount;
148
149         if (inst->mpp)
150                 request->simul_mpp = 2;
151
152         return inst->rcode;
153 }
154
155 static int always_detach(void *instance)
156 {
157         free(instance);
158         return 0;
159 }
160
161 module_t rlm_always = {
162         RLM_MODULE_INIT,
163         "always",
164         RLM_TYPE_THREAD_SAFE,           /* type */
165         always_instantiate,             /* instantiation */
166         always_detach,                  /* detach */
167         {
168                 always_return,          /* authentication */
169                 always_return,          /* authorization */
170                 always_return,          /* preaccounting */
171                 always_return,          /* accounting */
172                 always_checksimul,      /* checksimul */
173                 always_return,          /* pre-proxy */
174                 always_return,          /* post-proxy */
175                 always_return           /* post-auth */
176         },
177 };