]> git.stg.codes - stg.git/blob - projects/rlm_stg/rlm_stg.c
3eb913dc1887c47d8f928da65561840a032d11b5
[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     VALUE_PAIR * pwd;
107     VALUE_PAIR * svc;
108     const STG_PAIR * pairs;
109     const STG_PAIR * pair;
110     size_t count = 0;
111
112     instance = instance;
113
114     DEBUG("rlm_stg: stg_authorize()");
115
116     if (request->username) {
117         DEBUG("rlm_stg: stg_authorize() request username field: '%s'", request->username->vp_strvalue);
118     }
119     if (request->password) {
120         DEBUG("rlm_stg: stg_authorize() request password field: '%s'", request->password->vp_strvalue);
121     }
122     // Here we need to define Framed-Protocol
123     svc = pairfind(request->packet->vps, PW_SERVICE_TYPE);
124     if (svc) {
125         DEBUG("rlm_stg: stg_authorize() Service-Type defined as '%s'", svc->vp_strvalue);
126         pairs = stgAuthorizeImpl((const char *)request->username->vp_strvalue, (const char *)svc->vp_strvalue);
127     } else {
128         DEBUG("rlm_stg: stg_authorize() Service-Type undefined");
129         pairs = stgAuthorizeImpl((const char *)request->username->vp_strvalue, "");
130     }
131     if (!pairs) {
132         DEBUG("rlm_stg: stg_authorize() failed.");
133         return RLM_MODULE_REJECT;
134     }
135
136     pair = pairs;
137     while (!emptyPair(pair)) {
138         pwd = pairmake(pair->key, pair->value, T_OP_SET);
139         pairadd(&request->config_items, pwd);
140         DEBUG("Adding pair '%s': '%s'", pair->key, pair->value);
141         ++pair;
142         ++count;
143     }
144     deletePairs(pairs);
145
146     if (count)
147         return RLM_MODULE_UPDATED;
148
149     return RLM_MODULE_NOOP;
150 }
151
152 /*
153  *    Authenticate the user with the given password.
154  */
155 static int stg_authenticate(void *, REQUEST *request)
156 {
157     VALUE_PAIR * svc;
158     VALUE_PAIR * pwd;
159     const STG_PAIR * pairs;
160     const STG_PAIR * pair;
161     size_t count = 0;
162
163     instance = instance;
164
165     DEBUG("rlm_stg: stg_authenticate()");
166
167     svc = pairfind(request->packet->vps, PW_SERVICE_TYPE);
168     if (svc) {
169         DEBUG("rlm_stg: stg_authenticate() Service-Type defined as '%s'", svc->vp_strvalue);
170         pairs = stgAuthenticateImpl((const char *)request->username->vp_strvalue, (const char *)svc->vp_strvalue);
171     } else {
172         DEBUG("rlm_stg: stg_authenticate() Service-Type undefined");
173         pairs = stgAuthenticateImpl((const char *)request->username->vp_strvalue, "");
174     }
175     if (!pairs) {
176         DEBUG("rlm_stg: stg_authenticate() failed.");
177         return RLM_MODULE_REJECT;
178     }
179
180     pair = pairs;
181     while (!emptyPair(pair)) {
182         pwd = pairmake(pair->key, pair->value, T_OP_SET);
183         pairadd(&request->reply->vps, pwd);
184         ++pair;
185         ++count;
186     }
187     deletePairs(pairs);
188
189     if (count)
190         return RLM_MODULE_UPDATED;
191
192     return RLM_MODULE_NOOP;
193 }
194
195 /*
196  *    Massage the request before recording it or proxying it
197  */
198 static int stg_preacct(void *, REQUEST *)
199 {
200     DEBUG("rlm_stg: stg_preacct()");
201
202     instance = instance;
203
204     return RLM_MODULE_OK;
205 }
206
207 /*
208  *    Write accounting information to this modules database.
209  */
210 static int stg_accounting(void *, REQUEST * request)
211 {
212     VALUE_PAIR * sttype;
213     VALUE_PAIR * svc;
214     VALUE_PAIR * sessid;
215     VALUE_PAIR * pwd;
216     const STG_PAIR * pairs;
217     const STG_PAIR * pair;
218     size_t count = 0;
219
220     instance = instance;
221
222     DEBUG("rlm_stg: stg_accounting()");
223
224     svc = pairfind(request->packet->vps, PW_SERVICE_TYPE);
225     sessid = pairfind(request->packet->vps, PW_ACCT_SESSION_ID);
226     sttype = pairfind(request->packet->vps, PW_ACCT_STATUS_TYPE);
227
228     if (!sessid) {
229         DEBUG("rlm_stg: stg_accounting() Acct-Session-ID undefined");
230         return RLM_MODULE_FAIL;
231     }
232
233     if (sttype) {
234         DEBUG("Acct-Status-Type := %s", sttype->vp_strvalue);
235         if (svc) {
236             DEBUG("rlm_stg: stg_accounting() Service-Type defined as '%s'", svc->vp_strvalue);
237             pairs = stgAccountingImpl((const char *)request->username->vp_strvalue, (const char *)svc->vp_strvalue, (const char *)sttype->vp_strvalue, (const char *)sessid->vp_strvalue);
238         } else {
239             DEBUG("rlm_stg: stg_accounting() Service-Type undefined");
240             pairs = stgAccountingImpl((const char *)request->username->vp_strvalue, (const char *)svc->vp_strvalue, (const char *)sttype->vp_strvalue, (const char *)sessid->vp_strvalue);
241         }
242     } else {
243         DEBUG("rlm_stg: stg_accounting() Acct-Status-Type := NULL");
244         return RLM_MODULE_OK;
245     }
246     if (!pairs) {
247         DEBUG("rlm_stg: stg_accounting() failed.");
248         return RLM_MODULE_REJECT;
249     }
250
251     pair = pairs;
252     while (!emptyPair(pair)) {
253         pwd = pairmake(pair->key, pair->value, T_OP_SET);
254         pairadd(&request->reply->vps, pwd);
255         ++pair;
256         ++count;
257     }
258     deletePairs(pairs);
259
260     if (count)
261         return RLM_MODULE_UPDATED;
262
263     return RLM_MODULE_OK;
264 }
265
266 /*
267  *    See if a user is already logged in. Sets request->simul_count to the
268  *    current session count for this user and sets request->simul_mpp to 2
269  *    if it looks like a multilink attempt based on the requested IP
270  *    address, otherwise leaves request->simul_mpp alone.
271  *
272  *    Check twice. If on the first pass the user exceeds his
273  *    max. number of logins, do a second pass and validate all
274  *    logins by querying the terminal server (using eg. SNMP).
275  */
276 static int stg_checksimul(void *, REQUEST *request)
277 {
278     DEBUG("rlm_stg: stg_checksimul()");
279
280     instance = instance;
281
282     request->simul_count=0;
283
284     return RLM_MODULE_OK;
285 }
286
287 static int stg_postauth(void *, REQUEST *request)
288 {
289     VALUE_PAIR * svc;
290     VALUE_PAIR * pwd;
291     const STG_PAIR * pairs;
292     const STG_PAIR * pair;
293     size_t count = 0;
294
295     instance = instance;
296
297     DEBUG("rlm_stg: stg_postauth()");
298
299     svc = pairfind(request->packet->vps, PW_SERVICE_TYPE);
300
301     if (svc) {
302         DEBUG("rlm_stg: stg_postauth() Service-Type defined as '%s'", svc->vp_strvalue);
303         pairs = stgPostAuthImpl((const char *)request->username->vp_strvalue, (const char *)svc->vp_strvalue);
304     } else {
305         DEBUG("rlm_stg: stg_postauth() Service-Type undefined");
306         pairs = stgPostAuthImpl((const char *)request->username->vp_strvalue, "");
307     }
308     if (!pairs) {
309         DEBUG("rlm_stg: stg_postauth() failed.");
310         return RLM_MODULE_REJECT;
311     }
312
313     pair = pairs;
314     while (!emptyPair(pair)) {
315         pwd = pairmake(pair->key, pair->value, T_OP_SET);
316         pairadd(&request->reply->vps, pwd);
317         ++pair;
318         ++count;
319     }
320     deletePairs(pairs);
321
322     if (count)
323         return RLM_MODULE_UPDATED;
324
325     return RLM_MODULE_NOOP;
326 }
327
328 static int stg_detach(void *instance)
329 {
330     free(((struct rlm_stg_t *)instance)->server);
331     free(instance);
332     return 0;
333 }
334
335 /*
336  *    The module name should be the only globally exported symbol.
337  *    That is, everything else should be 'static'.
338  *
339  *    If the module needs to temporarily modify it's instantiation
340  *    data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
341  *    The server will then take care of ensuring that the module
342  *    is single-threaded.
343  */
344 module_t rlm_stg = {
345     RLM_MODULE_INIT,
346     "stg",
347     RLM_TYPE_THREAD_SAFE,        /* type */
348     stg_instantiate,        /* instantiation */
349     stg_detach,            /* detach */
350     {
351         stg_authenticate,    /* authentication */
352         stg_authorize,    /* authorization */
353         stg_preacct,    /* preaccounting */
354         stg_accounting,    /* accounting */
355         stg_checksimul,    /* checksimul */
356         NULL,            /* pre-proxy */
357         NULL,            /* post-proxy */
358         stg_postauth            /* post-auth */
359     },
360 };