]> git.stg.codes - stg.git/blob - projects/rlm_stg/rlm_stg.c
More fixes after cppcheck.
[stg.git] / projects / rlm_stg / rlm_stg.c
1 /*
2  *    This program is free software; you can redistribute it and/or modify
3  *    it under the terms of the GNU General Public License as published by
4  *    the Free Software Foundation; either version 2 of the License, or
5  *    (at your option) any later version.
6  *
7  *    This program is distributed in the hope that it will be useful,
8  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *    GNU General Public License for more details.
11  *
12  *    You should have received a copy of the GNU General Public License
13  *    along with this program; if not, write to the Free Software
14  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  */
16
17 /*
18  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
19  */
20
21 /*
22  *  FreeRADIUS module for data access via Stargazer
23  *
24  *  $Revision: 1.8 $
25  *  $Date: 2010/08/14 04:15:08 $
26  *
27  */
28
29 #ifndef NDEBUG
30 #define NDEBUG
31 #include <freeradius/ident.h>
32 #include <freeradius/radiusd.h>
33 #include <freeradius/modules.h>
34 #undef NDEBUG
35 #endif
36
37 #include "stgpair.h"
38 #include "iface.h"
39
40 typedef struct rlm_stg_t {
41     char * server;
42     uint16_t port;
43     char * password;
44 } rlm_stg_t;
45
46 static const CONF_PARSER module_config[] = {
47   { "server",  PW_TYPE_STRING_PTR, offsetof(rlm_stg_t,server), NULL,  "localhost"},
48   { "port",  PW_TYPE_INTEGER,     offsetof(rlm_stg_t,port), NULL,  "9091" },
49   { "password",  PW_TYPE_STRING_PTR, offsetof(rlm_stg_t,password), NULL,  "123456"},
50
51   { NULL, -1, 0, NULL, NULL }        /* end the list */
52 };
53
54 int emptyPair(const STG_PAIR * pair);
55
56 /*
57  *    Do any per-module initialization that is separate to each
58  *    configured instance of the module.  e.g. set up connections
59  *    to external databases, read configuration files, set up
60  *    dictionary entries, etc.
61  *
62  *    If configuration information is given in the config section
63  *    that must be referenced in later calls, store a handle to it
64  *    in *instance otherwise put a null pointer there.
65  */
66 static int stg_instantiate(CONF_SECTION *conf, void **instance)
67 {
68     rlm_stg_t *data;
69
70     /*
71      *    Set up a storage area for instance data
72      */
73     data = rad_malloc(sizeof(*data));
74     if (!data) {
75         return -1;
76     }
77     memset(data, 0, sizeof(*data));
78
79     /*
80      *    If the configuration parameters can't be parsed, then
81      *    fail.
82      */
83     if (cf_section_parse(conf, data, module_config) < 0) {
84         free(data);
85         return -1;
86     }
87
88     if (!stgInstantiateImpl(data->server, data->port)) {
89         free(data);
90         return -1;
91     }
92
93     *instance = data;
94
95     return 0;
96 }
97
98 /*
99  *    Find the named user in this modules database.  Create the set
100  *    of attribute-value pairs to check and reply with for this user
101  *    from the database. The authentication code only needs to check
102  *    the password, the rest is done here.
103  */
104 static int stg_authorize(void *, REQUEST *request)
105 {
106     const STG_PAIR * pairs;
107     const STG_PAIR * pair;
108     size_t count = 0;
109
110     instance = instance;
111
112     DEBUG("rlm_stg: stg_authorize()");
113
114     if (request->username) {
115         DEBUG("rlm_stg: stg_authorize() request username field: '%s'", request->username->vp_strvalue);
116     }
117     if (request->password) {
118         DEBUG("rlm_stg: stg_authorize() request password field: '%s'", request->password->vp_strvalue);
119     }
120     // Here we need to define Framed-Protocol
121     VALUE_PAIR * svc = pairfind(request->packet->vps, PW_SERVICE_TYPE);
122     if (svc) {
123         DEBUG("rlm_stg: stg_authorize() Service-Type defined as '%s'", svc->vp_strvalue);
124         pairs = stgAuthorizeImpl((const char *)request->username->vp_strvalue, (const char *)svc->vp_strvalue);
125     } else {
126         DEBUG("rlm_stg: stg_authorize() Service-Type undefined");
127         pairs = stgAuthorizeImpl((const char *)request->username->vp_strvalue, "");
128     }
129     if (!pairs) {
130         DEBUG("rlm_stg: stg_authorize() failed.");
131         return RLM_MODULE_REJECT;
132     }
133
134     pair = pairs;
135     while (!emptyPair(pair)) {
136         VALUE_PAIR * pwd = pairmake(pair->key, pair->value, T_OP_SET);
137         pairadd(&request->config_items, pwd);
138         DEBUG("Adding pair '%s': '%s'", pair->key, pair->value);
139         ++pair;
140         ++count;
141     }
142     deletePairs(pairs);
143
144     if (count)
145         return RLM_MODULE_UPDATED;
146
147     return RLM_MODULE_NOOP;
148 }
149
150 /*
151  *    Authenticate the user with the given password.
152  */
153 static int stg_authenticate(void *, REQUEST *request)
154 {
155     const STG_PAIR * pairs;
156     const STG_PAIR * pair;
157     size_t count = 0;
158
159     instance = instance;
160
161     DEBUG("rlm_stg: stg_authenticate()");
162
163     VALUE_PAIR * svc = pairfind(request->packet->vps, PW_SERVICE_TYPE);
164     if (svc) {
165         DEBUG("rlm_stg: stg_authenticate() Service-Type defined as '%s'", svc->vp_strvalue);
166         pairs = stgAuthenticateImpl((const char *)request->username->vp_strvalue, (const char *)svc->vp_strvalue);
167     } else {
168         DEBUG("rlm_stg: stg_authenticate() Service-Type undefined");
169         pairs = stgAuthenticateImpl((const char *)request->username->vp_strvalue, "");
170     }
171     if (!pairs) {
172         DEBUG("rlm_stg: stg_authenticate() failed.");
173         return RLM_MODULE_REJECT;
174     }
175
176     pair = pairs;
177     while (!emptyPair(pair)) {
178         VALUE_PAIR * pwd = pairmake(pair->key, pair->value, T_OP_SET);
179         pairadd(&request->reply->vps, pwd);
180         ++pair;
181         ++count;
182     }
183     deletePairs(pairs);
184
185     if (count)
186         return RLM_MODULE_UPDATED;
187
188     return RLM_MODULE_NOOP;
189 }
190
191 /*
192  *    Massage the request before recording it or proxying it
193  */
194 static int stg_preacct(void *, REQUEST *)
195 {
196     DEBUG("rlm_stg: stg_preacct()");
197
198     instance = instance;
199
200     return RLM_MODULE_OK;
201 }
202
203 /*
204  *    Write accounting information to this modules database.
205  */
206 static int stg_accounting(void *, REQUEST * request)
207 {
208     const STG_PAIR * pairs;
209     const STG_PAIR * pair;
210     size_t count = 0;
211
212     instance = instance;
213
214     DEBUG("rlm_stg: stg_accounting()");
215
216     VALUE_PAIR * svc = pairfind(request->packet->vps, PW_SERVICE_TYPE);
217     VALUE_PAIR * sessid = pairfind(request->packet->vps, PW_ACCT_SESSION_ID);
218     VALUE_PAIR * sttype = pairfind(request->packet->vps, PW_ACCT_STATUS_TYPE);
219
220     if (!sessid) {
221         DEBUG("rlm_stg: stg_accounting() Acct-Session-ID undefined");
222         return RLM_MODULE_FAIL;
223     }
224
225     if (sttype) {
226         DEBUG("Acct-Status-Type := %s", sttype->vp_strvalue);
227         if (svc) {
228             DEBUG("rlm_stg: stg_accounting() Service-Type defined as '%s'", svc->vp_strvalue);
229             pairs = stgAccountingImpl((const char *)request->username->vp_strvalue, (const char *)svc->vp_strvalue, (const char *)sttype->vp_strvalue, (const char *)sessid->vp_strvalue);
230         } else {
231             DEBUG("rlm_stg: stg_accounting() Service-Type undefined");
232             pairs = stgAccountingImpl((const char *)request->username->vp_strvalue, "", (const char *)sttype->vp_strvalue, (const char *)sessid->vp_strvalue);
233         }
234     } else {
235         DEBUG("rlm_stg: stg_accounting() Acct-Status-Type := NULL");
236         return RLM_MODULE_OK;
237     }
238     if (!pairs) {
239         DEBUG("rlm_stg: stg_accounting() failed.");
240         return RLM_MODULE_REJECT;
241     }
242
243     pair = pairs;
244     while (!emptyPair(pair)) {
245         VALUE_PAIR * pwd = pairmake(pair->key, pair->value, T_OP_SET);
246         pairadd(&request->reply->vps, pwd);
247         ++pair;
248         ++count;
249     }
250     deletePairs(pairs);
251
252     if (count)
253         return RLM_MODULE_UPDATED;
254
255     return RLM_MODULE_OK;
256 }
257
258 /*
259  *    See if a user is already logged in. Sets request->simul_count to the
260  *    current session count for this user and sets request->simul_mpp to 2
261  *    if it looks like a multilink attempt based on the requested IP
262  *    address, otherwise leaves request->simul_mpp alone.
263  *
264  *    Check twice. If on the first pass the user exceeds his
265  *    max. number of logins, do a second pass and validate all
266  *    logins by querying the terminal server (using eg. SNMP).
267  */
268 static int stg_checksimul(void *, REQUEST *request)
269 {
270     DEBUG("rlm_stg: stg_checksimul()");
271
272     instance = instance;
273
274     request->simul_count=0;
275
276     return RLM_MODULE_OK;
277 }
278
279 static int stg_postauth(void *, REQUEST *request)
280 {
281     const STG_PAIR * pairs;
282     const STG_PAIR * pair;
283     size_t count = 0;
284
285     instance = instance;
286
287     DEBUG("rlm_stg: stg_postauth()");
288
289     VALUE_PAIR * svc = pairfind(request->packet->vps, PW_SERVICE_TYPE);
290
291     if (svc) {
292         DEBUG("rlm_stg: stg_postauth() Service-Type defined as '%s'", svc->vp_strvalue);
293         pairs = stgPostAuthImpl((const char *)request->username->vp_strvalue, (const char *)svc->vp_strvalue);
294     } else {
295         DEBUG("rlm_stg: stg_postauth() Service-Type undefined");
296         pairs = stgPostAuthImpl((const char *)request->username->vp_strvalue, "");
297     }
298     if (!pairs) {
299         DEBUG("rlm_stg: stg_postauth() failed.");
300         return RLM_MODULE_REJECT;
301     }
302
303     pair = pairs;
304     while (!emptyPair(pair)) {
305         VALUE_PAIR * pwd = pairmake(pair->key, pair->value, T_OP_SET);
306         pairadd(&request->reply->vps, pwd);
307         ++pair;
308         ++count;
309     }
310     deletePairs(pairs);
311
312     if (count)
313         return RLM_MODULE_UPDATED;
314
315     return RLM_MODULE_NOOP;
316 }
317
318 static int stg_detach(void *instance)
319 {
320     free(((struct rlm_stg_t *)instance)->server);
321     free(instance);
322     return 0;
323 }
324
325 /*
326  *    The module name should be the only globally exported symbol.
327  *    That is, everything else should be 'static'.
328  *
329  *    If the module needs to temporarily modify it's instantiation
330  *    data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
331  *    The server will then take care of ensuring that the module
332  *    is single-threaded.
333  */
334 module_t rlm_stg = {
335     RLM_MODULE_INIT,
336     "stg",
337     RLM_TYPE_THREAD_SAFE,        /* type */
338     stg_instantiate,        /* instantiation */
339     stg_detach,            /* detach */
340     {
341         stg_authenticate,    /* authentication */
342         stg_authorize,    /* authorization */
343         stg_preacct,    /* preaccounting */
344         stg_accounting,    /* accounting */
345         stg_checksimul,    /* checksimul */
346         NULL,            /* pre-proxy */
347         NULL,            /* post-proxy */
348         stg_postauth            /* post-auth */
349     },
350 };