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 */
55 * Do any per-module initialization that is separate to each
56 * configured instance of the module. e.g. set up connections
57 * to external databases, read configuration files, set up
58 * dictionary entries, etc.
60 * If configuration information is given in the config section
61 * that must be referenced in later calls, store a handle to it
62 * in *instance otherwise put a null pointer there.
64 static int stg_instantiate(CONF_SECTION* conf, void** instance)
69 * Set up a storage area for instance data
71 data = rad_malloc(sizeof(*data));
75 memset(data, 0, sizeof(*data));
78 * If the configuration parameters can't be parsed, then
81 if (cf_section_parse(conf, data, module_config) < 0) {
86 if (!stgInstantiateImpl(data->server, data->port)) {
97 * Find the named user in this modules database. Create the set
98 * of attribute-value pairs to check and reply with for this user
99 * from the database. The authentication code only needs to check
100 * the password, the rest is done here.
102 static int stg_authorize(void*, REQUEST* request)
104 const STG_PAIR* pairs;
105 const STG_PAIR* pair;
107 const char* username = NULL;
108 const char* password = NULL;
112 DEBUG("rlm_stg: stg_authorize()");
114 if (request->username) {
115 username = request->username->data.strvalue;
116 DEBUG("rlm_stg: stg_authorize() request username field: '%s'", username);
119 if (request->password) {
120 password = request->password->data.strvalue;
121 DEBUG("rlm_stg: stg_authorize() request password field: '%s'", password);
124 pairs = stgAuthorizeImpl(username, password, request->packet->vps);
127 DEBUG("rlm_stg: stg_authorize() failed.");
128 return RLM_MODULE_REJECT;
132 while (!emptyPair(pair)) {
133 VALUE_PAIR* vp = pairmake(pair->key, pair->value, T_OP_SET);
134 pairadd(&request->config_items, vp);
135 DEBUG("Adding pair '%s': '%s'", pair->key, pair->value);
142 return RLM_MODULE_UPDATED;
144 return RLM_MODULE_NOOP;
148 * Authenticate the user with the given password.
150 static int stg_authenticate(void*, REQUEST* request)
152 const STG_PAIR* pairs;
153 const STG_PAIR* pair;
155 const char* username = NULL;
156 const char* password = NULL;
160 DEBUG("rlm_stg: stg_authenticate()");
162 if (request->username) {
163 username = request->username->data.strvalue;
164 DEBUG("rlm_stg: stg_authenticate() request username field: '%s'", username);
167 if (request->password) {
168 password = request->password->data.strvalue;
169 DEBUG("rlm_stg: stg_authenticate() request password field: '%s'", password);
172 pairs = stgAuthenticateImpl(username, password, request->packet->vps);
175 DEBUG("rlm_stg: stg_authenticate() failed.");
176 return RLM_MODULE_REJECT;
180 while (!emptyPair(pair)) {
181 VALUE_PAIR* vp = pairmake(pair->key, pair->value, T_OP_SET);
182 pairadd(&request->reply->vps, vp);
189 return RLM_MODULE_UPDATED;
191 return RLM_MODULE_NOOP;
195 * Massage the request before recording it or proxying it
197 static int stg_preacct(void*, REQUEST*)
199 const STG_PAIR* pairs;
200 const STG_PAIR* pair;
202 const char* username = NULL;
203 const char* password = NULL;
205 DEBUG("rlm_stg: stg_preacct()");
209 if (request->username) {
210 username = request->username->data.strvalue;
211 DEBUG("rlm_stg: stg_preacct() request username field: '%s'", username);
214 if (request->password) {
215 password = request->password->data.strvalue;
216 DEBUG("rlm_stg: stg_preacct() request password field: '%s'", password);
219 pairs = stgPreAcctImpl(username, password, request->packet->vps);
222 DEBUG("rlm_stg: stg_preacct() failed.");
223 return RLM_MODULE_REJECT;
227 while (!emptyPair(pair)) {
228 VALUE_PAIR* vp = pairmake(pair->key, pair->value, T_OP_SET);
229 pairadd(&request->reply->vps, vp);
236 return RLM_MODULE_UPDATED;
238 return RLM_MODULE_NOOP;
242 * Write accounting information to this modules database.
244 static int stg_accounting(void*, REQUEST* request)
246 const STG_PAIR* pairs;
247 const STG_PAIR* pair;
252 DEBUG("rlm_stg: stg_accounting()");
254 VALUE_PAIR* svc = pairfind(request->packet->vps, PW_SERVICE_TYPE);
255 VALUE_PAIR* sessid = pairfind(request->packet->vps, PW_ACCT_SESSION_ID);
256 VALUE_PAIR* sttype = pairfind(request->packet->vps, PW_ACCT_STATUS_TYPE);
259 DEBUG("rlm_stg: stg_accounting() Acct-Session-ID undefined");
260 return RLM_MODULE_FAIL;
264 DEBUG("Acct-Status-Type := %s", sttype->data.strvalue);
266 DEBUG("rlm_stg: stg_accounting() Service-Type defined as '%s'", svc->data.strvalue);
267 pairs = stgAccountingImpl((const char*)request->username->data.strvalue, (const char*)svc->data.strvalue, (const char*)sttype->data.strvalue, (const char*)sessid->data.strvalue);
269 DEBUG("rlm_stg: stg_accounting() Service-Type undefined");
270 pairs = stgAccountingImpl((const char*)request->username->data.strvalue, "", (const char*)sttype->data.strvalue, (const char*)sessid->data.strvalue);
273 DEBUG("rlm_stg: stg_accounting() Acct-Status-Type := NULL");
274 return RLM_MODULE_OK;
277 DEBUG("rlm_stg: stg_accounting() failed.");
278 return RLM_MODULE_REJECT;
282 while (!emptyPair(pair)) {
283 VALUE_PAIR* pwd = pairmake(pair->key, pair->value, T_OP_SET);
284 pairadd(&request->reply->vps, pwd);
291 return RLM_MODULE_UPDATED;
293 return RLM_MODULE_OK;
297 * See if a user is already logged in. Sets request->simul_count to the
298 * current session count for this user and sets request->simul_mpp to 2
299 * if it looks like a multilink attempt based on the requested IP
300 * address, otherwise leaves request->simul_mpp alone.
302 * Check twice. If on the first pass the user exceeds his
303 * max. number of logins, do a second pass and validate all
304 * logins by querying the terminal server (using eg. SNMP).
306 static int stg_checksimul(void*, REQUEST* request)
308 DEBUG("rlm_stg: stg_checksimul()");
312 request->simul_count = 0;
314 return RLM_MODULE_OK;
317 static int stg_postauth(void*, REQUEST* request)
319 const STG_PAIR* pairs;
320 const STG_PAIR* pair;
325 DEBUG("rlm_stg: stg_postauth()");
327 VALUE_PAIR* svc = pairfind(request->packet->vps, PW_SERVICE_TYPE);
330 DEBUG("rlm_stg: stg_postauth() Service-Type defined as '%s'", svc->data.strvalue);
331 pairs = stgPostAuthImpl((const char*)request->username->data.strvalue, (const char*)svc->data.strvalue);
333 DEBUG("rlm_stg: stg_postauth() Service-Type undefined");
334 pairs = stgPostAuthImpl((const char*)request->username->data.strvalue, "");
337 DEBUG("rlm_stg: stg_postauth() failed.");
338 return RLM_MODULE_REJECT;
342 while (!emptyPair(pair)) {
343 VALUE_PAIR* pwd = pairmake(pair->key, pair->value, T_OP_SET);
344 pairadd(&request->reply->vps, pwd);
351 return RLM_MODULE_UPDATED;
353 return RLM_MODULE_NOOP;
356 static int stg_detach(void* instance)
358 free(((struct rlm_stg_t*)instance)->server);
364 * The module name should be the only globally exported symbol.
365 * That is, everything else should be 'static'.
367 * If the module needs to temporarily modify it's instantiation
368 * data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
369 * The server will then take care of ensuring that the module
370 * is single-threaded.
375 RLM_TYPE_THREAD_SAFE, /* type */
376 stg_instantiate, /* instantiation */
377 stg_detach, /* detach */
379 stg_authenticate, /* authentication */
380 stg_authorize, /* authorization */
381 stg_preacct, /* preaccounting */
382 stg_accounting, /* accounting */
383 stg_checksimul, /* checksimul */
384 stg_pre_proxy, /* pre-proxy */
385 stg_post_proxy, /* post-proxy */
386 stg_postauth /* post-auth */