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 $
32 #include <freeradius/ident.h>
33 #include <freeradius/radiusd.h>
34 #include <freeradius/modules.h>
36 #include <stddef.h> // size_t
38 typedef struct rlm_stg_t {
42 static const CONF_PARSER module_config[] = {
43 { "address", PW_TYPE_STRING_PTR, offsetof(rlm_stg_t, address), NULL, "unix:/var/run/stg.sock"},
45 { NULL, -1, 0, NULL, NULL } /* end the list */
48 static void deletePairs(STG_PAIR* pairs)
53 static size_t toVPS(const STG_PAIR* pairs, VALUE_PAIR** vps)
55 const STG_PAIR* pair = pairs;
58 while (!emptyPair(pair)) {
59 VALUE_PAIR* vp = pairmake(pair->key, pair->value, T_OP_SET);
70 static size_t toReply(STG_RESULT result, REQUEST* request)
74 count += toVPS(result.modify, &request->config_items);
75 pairfree(&request->reply->vps);
76 count += toVPS(result.reply, &request->reply->vps);
78 deletePairs(result.modify);
79 deletePairs(result.reply);
84 static int countVPS(const VALUE_PAIR* pairs)
87 while (pairs != NULL) {
94 static STG_PAIR* fromVPS(const VALUE_PAIR* pairs)
96 unsigned size = countVPS(pairs);
97 STG_PAIR* res = (STG_PAIR*)malloc(sizeof(STG_PAIR) * (size + 1));
99 while (pairs != NULL) {
100 bzero(res[pos].key, sizeof(res[0].key));
101 bzero(res[pos].value, sizeof(res[0].value));
102 strncpy(res[pos].key, pairs->name, sizeof(res[0].key));
103 vp_prints_value(res[pos].value, sizeof(res[0].value), (VALUE_PAIR*)pairs, 0);
107 bzero(res[pos].key, sizeof(res[0].key));
108 bzero(res[pos].value, sizeof(res[0].value));
112 static int toRLMCode(int code)
116 case STG_REJECT: return RLM_MODULE_REJECT;
117 case STG_FAIL: return RLM_MODULE_FAIL;
118 case STG_OK: return RLM_MODULE_OK;
119 case STG_HANDLED: return RLM_MODULE_HANDLED;
120 case STG_INVALID: return RLM_MODULE_INVALID;
121 case STG_USERLOCK: return RLM_MODULE_USERLOCK;
122 case STG_NOTFOUND: return RLM_MODULE_NOTFOUND;
123 case STG_NOOP: return RLM_MODULE_NOOP;
124 case STG_UPDATED: return RLM_MODULE_UPDATED;
126 return RLM_MODULE_REJECT;
130 * Do any per-module initialization that is separate to each
131 * configured instance of the module. e.g. set up connections
132 * to external databases, read configuration files, set up
133 * dictionary entries, etc.
135 * If configuration information is given in the config section
136 * that must be referenced in later calls, store a handle to it
137 * in *instance otherwise put a null pointer there.
139 static int stg_instantiate(CONF_SECTION* conf, void** instance)
144 * Set up a storage area for instance data
146 data = rad_malloc(sizeof(*data));
150 memset(data, 0, sizeof(*data));
153 * If the configuration parameters can't be parsed, then
156 if (cf_section_parse(conf, data, module_config) < 0) {
161 if (!stgInstantiateImpl(data->address)) {
172 * Find the named user in this modules database. Create the set
173 * of attribute-value pairs to check and reply with for this user
174 * from the database. The authentication code only needs to check
175 * the password, the rest is done here.
177 static int stg_authorize(void* instance, REQUEST* request)
180 STG_PAIR* pairs = fromVPS(request->packet->vps);
182 const char* username = NULL;
183 const char* password = NULL;
187 DEBUG("rlm_stg: stg_authorize()");
189 if (request->username) {
190 username = request->username->data.strvalue;
191 DEBUG("rlm_stg: stg_authorize() request username field: '%s'", username);
194 if (request->password) {
195 password = request->password->data.strvalue;
196 DEBUG("rlm_stg: stg_authorize() request password field: '%s'", password);
199 result = stgAuthorizeImpl(username, password, pairs);
202 if (!result.modify && !result.reply) {
203 DEBUG("rlm_stg: stg_authorize() failed.");
204 return RLM_MODULE_REJECT;
207 count = toReply(result, request);
210 return RLM_MODULE_UPDATED;
212 return toRLMCode(result.returnCode);
216 * Authenticate the user with the given password.
218 static int stg_authenticate(void* instance, REQUEST* request)
221 STG_PAIR* pairs = fromVPS(request->packet->vps);
223 const char* username = NULL;
224 const char* password = NULL;
228 DEBUG("rlm_stg: stg_authenticate()");
230 if (request->username) {
231 username = request->username->data.strvalue;
232 DEBUG("rlm_stg: stg_authenticate() request username field: '%s'", username);
235 if (request->password) {
236 password = request->password->data.strvalue;
237 DEBUG("rlm_stg: stg_authenticate() request password field: '%s'", password);
240 result = stgAuthenticateImpl(username, password, pairs);
243 if (!result.modify && !result.reply) {
244 DEBUG("rlm_stg: stg_authenticate() failed.");
245 return RLM_MODULE_REJECT;
248 count = toReply(result, request);
251 return RLM_MODULE_UPDATED;
253 return toRLMCode(result.returnCode);
257 * Massage the request before recording it or proxying it
259 static int stg_preacct(void* instance, REQUEST* request)
262 STG_PAIR* pairs = fromVPS(request->packet->vps);
264 const char* username = NULL;
265 const char* password = NULL;
267 DEBUG("rlm_stg: stg_preacct()");
271 if (request->username) {
272 username = request->username->data.strvalue;
273 DEBUG("rlm_stg: stg_preacct() request username field: '%s'", username);
276 if (request->password) {
277 password = request->password->data.strvalue;
278 DEBUG("rlm_stg: stg_preacct() request password field: '%s'", password);
281 result = stgPreAcctImpl(username, password, pairs);
284 if (!result.modify && !result.reply) {
285 DEBUG("rlm_stg: stg_preacct() failed.");
286 return RLM_MODULE_REJECT;
289 count = toReply(result, request);
292 return RLM_MODULE_UPDATED;
294 return toRLMCode(result.returnCode);
298 * Write accounting information to this modules database.
300 static int stg_accounting(void* instance, REQUEST* request)
303 STG_PAIR* pairs = fromVPS(request->packet->vps);
305 const char* username = NULL;
306 const char* password = NULL;
308 DEBUG("rlm_stg: stg_accounting()");
312 if (request->username) {
313 username = request->username->data.strvalue;
314 DEBUG("rlm_stg: stg_accounting() request username field: '%s'", username);
317 if (request->password) {
318 password = request->password->data.strvalue;
319 DEBUG("rlm_stg: stg_accounting() request password field: '%s'", password);
322 result = stgAccountingImpl(username, password, pairs);
325 if (!result.modify && !result.reply) {
326 DEBUG("rlm_stg: stg_accounting() failed.");
327 return RLM_MODULE_REJECT;
330 count = toReply(result, request);
333 return RLM_MODULE_UPDATED;
335 return toRLMCode(result.returnCode);
339 * See if a user is already logged in. Sets request->simul_count to the
340 * current session count for this user and sets request->simul_mpp to 2
341 * if it looks like a multilink attempt based on the requested IP
342 * address, otherwise leaves request->simul_mpp alone.
344 * Check twice. If on the first pass the user exceeds his
345 * max. number of logins, do a second pass and validate all
346 * logins by querying the terminal server (using eg. SNMP).
348 static int stg_checksimul(void* instance, REQUEST* request)
350 DEBUG("rlm_stg: stg_checksimul()");
354 request->simul_count = 0;
356 return RLM_MODULE_OK;
359 static int stg_postauth(void* instance, REQUEST* request)
362 STG_PAIR* pairs = fromVPS(request->packet->vps);
364 const char* username = NULL;
365 const char* password = NULL;
367 DEBUG("rlm_stg: stg_postauth()");
371 if (request->username) {
372 username = request->username->data.strvalue;
373 DEBUG("rlm_stg: stg_postauth() request username field: '%s'", username);
376 if (request->password) {
377 password = request->password->data.strvalue;
378 DEBUG("rlm_stg: stg_postauth() request password field: '%s'", password);
381 result = stgPostAuthImpl(username, password, pairs);
384 if (!result.modify && !result.reply) {
385 DEBUG("rlm_stg: stg_postauth() failed.");
386 return RLM_MODULE_REJECT;
389 count = toReply(result, request);
392 return RLM_MODULE_UPDATED;
394 return toRLMCode(result.returnCode);
397 static int stg_detach(void* instance)
399 free(((struct rlm_stg_t*)instance)->address);
407 RLM_TYPE_THREAD_UNSAFE, /* type */
408 stg_instantiate, /* instantiation */
409 stg_detach, /* detach */
411 stg_authenticate, /* authentication */
412 stg_authorize, /* authorization */
413 stg_preacct, /* preaccounting */
414 stg_accounting, /* accounting */
415 stg_checksimul, /* checksimul */
416 NULL, /* pre-proxy */
417 NULL, /* post-proxy */
418 stg_postauth /* post-auth */