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.
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.
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
18 * Author : Maxim Mamontov <faust@stargazer.dp.ua>
22 * FreeRADIUS module for data access via Stargazer
25 * $Date: 2010/08/14 04:15:08 $
31 #include <freeradius/ident.h>
32 #include <freeradius/radiusd.h>
33 #include <freeradius/modules.h>
40 typedef struct rlm_stg_t {
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"},
51 { NULL, -1, 0, NULL, NULL } /* end the list */
54 int emptyPair(const STG_PAIR * pair);
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.
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.
66 static int stg_instantiate(CONF_SECTION *conf, void **instance)
71 * Set up a storage area for instance data
73 data = rad_malloc(sizeof(*data));
77 memset(data, 0, sizeof(*data));
80 * If the configuration parameters can't be parsed, then
83 if (cf_section_parse(conf, data, module_config) < 0) {
88 if (!stgInstantiateImpl(data->server, data->port)) {
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.
104 static int stg_authorize(void *, REQUEST *request)
108 const STG_PAIR * pairs;
109 const STG_PAIR * pair;
114 DEBUG("rlm_stg: stg_authorize()");
116 if (request->username) {
117 DEBUG("rlm_stg: stg_authorize() request username field: '%s'", request->username->vp_strvalue);
119 if (request->password) {
120 DEBUG("rlm_stg: stg_authorize() request password field: '%s'", request->password->vp_strvalue);
122 // Here we need to define Framed-Protocol
123 svc = pairfind(request->packet->vps, PW_SERVICE_TYPE);
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);
128 DEBUG("rlm_stg: stg_authorize() Service-Type undefined");
129 pairs = stgAuthorizeImpl((const char *)request->username->vp_strvalue, "");
132 DEBUG("rlm_stg: stg_authorize() failed.");
133 return RLM_MODULE_REJECT;
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);
147 return RLM_MODULE_UPDATED;
149 return RLM_MODULE_NOOP;
153 * Authenticate the user with the given password.
155 static int stg_authenticate(void *, REQUEST *request)
159 const STG_PAIR * pairs;
160 const STG_PAIR * pair;
165 DEBUG("rlm_stg: stg_authenticate()");
167 svc = pairfind(request->packet->vps, PW_SERVICE_TYPE);
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);
172 DEBUG("rlm_stg: stg_authenticate() Service-Type undefined");
173 pairs = stgAuthenticateImpl((const char *)request->username->vp_strvalue, "");
176 DEBUG("rlm_stg: stg_authenticate() failed.");
177 return RLM_MODULE_REJECT;
181 while (!emptyPair(pair)) {
182 pwd = pairmake(pair->key, pair->value, T_OP_SET);
183 pairadd(&request->reply->vps, pwd);
190 return RLM_MODULE_UPDATED;
192 return RLM_MODULE_NOOP;
196 * Massage the request before recording it or proxying it
198 static int stg_preacct(void *, REQUEST *)
200 DEBUG("rlm_stg: stg_preacct()");
204 return RLM_MODULE_OK;
208 * Write accounting information to this modules database.
210 static int stg_accounting(void *, REQUEST * request)
216 const STG_PAIR * pairs;
217 const STG_PAIR * pair;
222 DEBUG("rlm_stg: stg_accounting()");
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);
229 DEBUG("rlm_stg: stg_accounting() Acct-Session-ID undefined");
230 return RLM_MODULE_FAIL;
234 DEBUG("Acct-Status-Type := %s", sttype->vp_strvalue);
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);
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);
243 DEBUG("rlm_stg: stg_accounting() Acct-Status-Type := NULL");
244 return RLM_MODULE_OK;
247 DEBUG("rlm_stg: stg_accounting() failed.");
248 return RLM_MODULE_REJECT;
252 while (!emptyPair(pair)) {
253 pwd = pairmake(pair->key, pair->value, T_OP_SET);
254 pairadd(&request->reply->vps, pwd);
261 return RLM_MODULE_UPDATED;
263 return RLM_MODULE_OK;
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.
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).
276 static int stg_checksimul(void *, REQUEST *request)
278 DEBUG("rlm_stg: stg_checksimul()");
282 request->simul_count=0;
284 return RLM_MODULE_OK;
287 static int stg_postauth(void *, REQUEST *request)
291 const STG_PAIR * pairs;
292 const STG_PAIR * pair;
297 DEBUG("rlm_stg: stg_postauth()");
299 svc = pairfind(request->packet->vps, PW_SERVICE_TYPE);
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);
305 DEBUG("rlm_stg: stg_postauth() Service-Type undefined");
306 pairs = stgPostAuthImpl((const char *)request->username->vp_strvalue, "");
309 DEBUG("rlm_stg: stg_postauth() failed.");
310 return RLM_MODULE_REJECT;
314 while (!emptyPair(pair)) {
315 pwd = pairmake(pair->key, pair->value, T_OP_SET);
316 pairadd(&request->reply->vps, pwd);
323 return RLM_MODULE_UPDATED;
325 return RLM_MODULE_NOOP;
328 static int stg_detach(void *instance)
330 free(((struct rlm_stg_t *)instance)->server);
336 * The module name should be the only globally exported symbol.
337 * That is, everything else should be 'static'.
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.
347 RLM_TYPE_THREAD_SAFE, /* type */
348 stg_instantiate, /* instantiation */
349 stg_detach, /* detach */
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 */