Require that the modules call talloc for their instance handle.
[freeradius.git] / src / modules / rlm_example / rlm_example.c
1 /*
2  *   This program is is free software; you can redistribute it and/or modify
3  *   it under the terms of the GNU General Public License, version 2 if the
4  *   License as published by the Free Software Foundation.
5  *
6  *   This program is distributed in the hope that it will be useful,
7  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
8  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9  *   GNU General Public License for more details.
10  *
11  *   You should have received a copy of the GNU General Public License
12  *   along with this program; if not, write to the Free Software
13  *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
14  */
15  
16 /**
17  * $Id$
18  * @file rlm_example.c
19  * @brief Example module code.
20  *
21  * @copyright 2013 The FreeRADIUS server project
22  * @copyright 2013 your name \<your address\>
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 /*
31  *      Define a structure for our module configuration.
32  *
33  *      These variables do not need to be in a structure, but it's
34  *      a lot cleaner to do so, and a pointer to the structure can
35  *      be used as the instance handle.
36  */
37 typedef struct rlm_example_t {
38         int             boolean;
39         int             value;
40         char            *string;
41         uint32_t        ipaddr;
42 } rlm_example_t;
43
44 /*
45  *      A mapping of configuration file names to internal variables.
46  *
47  *      Note that the string is dynamically allocated, so it MUST
48  *      be freed.  When the configuration file parse re-reads the string,
49  *      it free's the old one, and strdup's the new one, placing the pointer
50  *      to the strdup'd string into 'config.string'.  This gets around
51  *      buffer over-flows.
52  */
53 static const CONF_PARSER module_config[] = {
54   { "integer", PW_TYPE_INTEGER,    offsetof(rlm_example_t,value), NULL,   "1" },
55   { "boolean", PW_TYPE_BOOLEAN,    offsetof(rlm_example_t,boolean), NULL, "no"},
56   { "string",  PW_TYPE_STRING_PTR, offsetof(rlm_example_t,string), NULL,  NULL},
57   { "ipaddr",  PW_TYPE_IPADDR,     offsetof(rlm_example_t,ipaddr), NULL,  "*" },
58
59   { NULL, -1, 0, NULL, NULL }           /* end the list */
60 };
61
62
63 /*
64  *      Do any per-module initialization that is separate to each
65  *      configured instance of the module.  e.g. set up connections
66  *      to external databases, read configuration files, set up
67  *      dictionary entries, etc.
68  *
69  *      If configuration information is given in the config section
70  *      that must be referenced in later calls, store a handle to it
71  *      in *instance otherwise put a null pointer there.
72  */
73 static int example_instantiate(CONF_SECTION *conf, void **instance)
74 {
75         rlm_example_t *data;
76
77         /*
78          *      Set up a storage area for instance data
79          */
80         *instance = data = talloc_zero(conf, rlm_example_t);
81         if (!data) return -1;
82
83         /*
84          *      If the configuration parameters can't be parsed, then
85          *      fail.
86          */
87         if (cf_section_parse(conf, data, module_config) < 0) {
88                 return -1;
89         }
90
91         return 0;
92 }
93
94 /*
95  *      Find the named user in this modules database.  Create the set
96  *      of attribute-value pairs to check and reply with for this user
97  *      from the database. The authentication code only needs to check
98  *      the password, the rest is done here.
99  */
100 static rlm_rcode_t example_authorize(void *instance, REQUEST *request)
101 {
102         VALUE_PAIR *state;
103         VALUE_PAIR *reply;
104
105         /* quiet the compiler */
106         instance = instance;
107         request = request;
108
109         /*
110          *  Look for the 'state' attribute.
111          */
112         state =  pairfind(request->packet->vps, PW_STATE, 0, TAG_ANY);
113         if (state != NULL) {
114                 RDEBUG("Found reply to access challenge");
115                 return RLM_MODULE_OK;
116         }
117
118         /*
119          *  Create the challenge, and add it to the reply.
120          */
121         reply = pairmake("Reply-Message", "This is a challenge", T_OP_EQ);
122         pairadd(&request->reply->vps, reply);
123         state = pairmake("State", "0", T_OP_EQ);
124         pairadd(&request->reply->vps, state);
125
126         /*
127          *  Mark the packet as an Access-Challenge packet.
128          *
129          *  The server will take care of sending it to the user.
130          */
131         request->reply->code = PW_ACCESS_CHALLENGE;
132         RDEBUG("Sending Access-Challenge.");
133
134         return RLM_MODULE_HANDLED;
135 }
136
137 /*
138  *      Authenticate the user with the given password.
139  */
140 static rlm_rcode_t example_authenticate(void *instance, REQUEST *request)
141 {
142         /* quiet the compiler */
143         instance = instance;
144         request = request;
145
146         return RLM_MODULE_OK;
147 }
148
149 /*
150  *      Massage the request before recording it or proxying it
151  */
152 static rlm_rcode_t example_preacct(void *instance, REQUEST *request)
153 {
154         /* quiet the compiler */
155         instance = instance;
156         request = request;
157
158         return RLM_MODULE_OK;
159 }
160
161 /*
162  *      Write accounting information to this modules database.
163  */
164 static rlm_rcode_t example_accounting(void *instance, REQUEST *request)
165 {
166         /* quiet the compiler */
167         instance = instance;
168         request = request;
169
170         return RLM_MODULE_OK;
171 }
172
173 /*
174  *      See if a user is already logged in. Sets request->simul_count to the
175  *      current session count for this user and sets request->simul_mpp to 2
176  *      if it looks like a multilink attempt based on the requested IP
177  *      address, otherwise leaves request->simul_mpp alone.
178  *
179  *      Check twice. If on the first pass the user exceeds his
180  *      max. number of logins, do a second pass and validate all
181  *      logins by querying the terminal server (using eg. SNMP).
182  */
183 static rlm_rcode_t example_checksimul(void *instance, REQUEST *request)
184 {
185   instance = instance;
186
187   request->simul_count=0;
188
189   return RLM_MODULE_OK;
190 }
191
192
193 /*
194  *      Only free memory we allocated.  The strings allocated via
195  *      cf_section_parse() do not need to be freed.
196  */
197 static int example_detach(void *instance)
198 {
199         /* free things here */
200         return 0;
201 }
202
203 /*
204  *      The module name should be the only globally exported symbol.
205  *      That is, everything else should be 'static'.
206  *
207  *      If the module needs to temporarily modify it's instantiation
208  *      data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
209  *      The server will then take care of ensuring that the module
210  *      is single-threaded.
211  */
212 module_t rlm_example = {
213         RLM_MODULE_INIT,
214         "example",
215         RLM_TYPE_THREAD_SAFE,           /* type */
216         example_instantiate,            /* instantiation */
217         example_detach,                 /* detach */
218         {
219                 example_authenticate,   /* authentication */
220                 example_authorize,      /* authorization */
221                 example_preacct,        /* preaccounting */
222                 example_accounting,     /* accounting */
223                 example_checksimul,     /* checksimul */
224                 NULL,                   /* pre-proxy */
225                 NULL,                   /* post-proxy */
226                 NULL                    /* post-auth */
227         },
228 };