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 $
34 #include <freeradius/ident.h>
35 #include <freeradius/radiusd.h>
36 #include <freeradius/modules.h>
40 #include <stddef.h> // size_t
42 typedef struct rlm_stg_t {
46 static const CONF_PARSER module_config[] = {
47 { "address", PW_TYPE_STRING_PTR, offsetof(rlm_stg_t, address), NULL, "unix:/var/run/stg.sock"},
49 { NULL, -1, 0, NULL, NULL } /* end the list */
52 static void deletePairs(STG_PAIR* pairs)
57 static size_t toVPS(const STG_PAIR* pairs, VALUE_PAIR* vps)
59 const STG_PAIR* pair = pairs;
62 while (!emptyPair(pair)) {
63 VALUE_PAIR* vp = pairmake(pair->key, pair->value, T_OP_SET);
65 DEBUG("Adding pair '%s': '%s'", pair->key, pair->value);
73 static size_t toReply(STG_RESULT result, REQUEST* request)
77 count += toVPS(result.modify, request->config_items);
78 count += toVPS(result.reply, request->reply->vps);
80 deletePairs(result.modify);
81 deletePairs(result.reply);
86 static int countVPS(const VALUE_PAIR* pairs)
89 while (pairs != NULL) {
96 static STG_PAIR* fromVPS(const VALUE_PAIR* pairs)
98 unsigned size = countVPS(pairs);
99 STG_PAIR* res = (STG_PAIR*)malloc(sizeof(STG_PAIR) * (size + 1));
101 while (pairs != NULL) {
102 bzero(res[pos].key, sizeof(res[0].key));
103 bzero(res[pos].value, sizeof(res[0].value));
104 strncpy(res[pos].key, pairs->name, sizeof(res[0].key));
105 strncpy(res[pos].value, pairs->data.strvalue, sizeof(res[0].value));
109 bzero(res[pos].key, sizeof(res[0].key));
110 bzero(res[pos].value, sizeof(res[0].value));
115 * Do any per-module initialization that is separate to each
116 * configured instance of the module. e.g. set up connections
117 * to external databases, read configuration files, set up
118 * dictionary entries, etc.
120 * If configuration information is given in the config section
121 * that must be referenced in later calls, store a handle to it
122 * in *instance otherwise put a null pointer there.
124 static int stg_instantiate(CONF_SECTION* conf, void** instance)
129 * Set up a storage area for instance data
131 data = rad_malloc(sizeof(*data));
135 memset(data, 0, sizeof(*data));
138 * If the configuration parameters can't be parsed, then
141 if (cf_section_parse(conf, data, module_config) < 0) {
146 if (!stgInstantiateImpl(data->address)) {
157 * Find the named user in this modules database. Create the set
158 * of attribute-value pairs to check and reply with for this user
159 * from the database. The authentication code only needs to check
160 * the password, the rest is done here.
162 static int stg_authorize(void* instance, REQUEST* request)
165 STG_PAIR* pairs = fromVPS(request->packet->vps);
167 const char* username = NULL;
168 const char* password = NULL;
172 DEBUG("rlm_stg: stg_authorize()");
174 if (request->username) {
175 username = request->username->data.strvalue;
176 DEBUG("rlm_stg: stg_authorize() request username field: '%s'", username);
179 if (request->password) {
180 password = request->password->data.strvalue;
181 DEBUG("rlm_stg: stg_authorize() request password field: '%s'", password);
184 result = stgAuthorizeImpl(username, password, pairs);
187 if (!result.modify && !result.reply) {
188 DEBUG("rlm_stg: stg_authorize() failed.");
189 return RLM_MODULE_REJECT;
192 count = toReply(result, request);
195 return RLM_MODULE_UPDATED;
197 return RLM_MODULE_NOOP;
201 * Authenticate the user with the given password.
203 static int stg_authenticate(void* instance, REQUEST* request)
206 STG_PAIR* pairs = fromVPS(request->packet->vps);
208 const char* username = NULL;
209 const char* password = NULL;
213 DEBUG("rlm_stg: stg_authenticate()");
215 if (request->username) {
216 username = request->username->data.strvalue;
217 DEBUG("rlm_stg: stg_authenticate() request username field: '%s'", username);
220 if (request->password) {
221 password = request->password->data.strvalue;
222 DEBUG("rlm_stg: stg_authenticate() request password field: '%s'", password);
225 result = stgAuthenticateImpl(username, password, pairs);
228 if (!result.modify && !result.reply) {
229 DEBUG("rlm_stg: stg_authenticate() failed.");
230 return RLM_MODULE_REJECT;
233 count = toReply(result, request);
236 return RLM_MODULE_UPDATED;
238 return RLM_MODULE_NOOP;
242 * Massage the request before recording it or proxying it
244 static int stg_preacct(void* instance, REQUEST* request)
247 STG_PAIR* pairs = fromVPS(request->packet->vps);
249 const char* username = NULL;
250 const char* password = NULL;
252 DEBUG("rlm_stg: stg_preacct()");
256 if (request->username) {
257 username = request->username->data.strvalue;
258 DEBUG("rlm_stg: stg_preacct() request username field: '%s'", username);
261 if (request->password) {
262 password = request->password->data.strvalue;
263 DEBUG("rlm_stg: stg_preacct() request password field: '%s'", password);
266 result = stgPreAcctImpl(username, password, pairs);
269 if (!result.modify && !result.reply) {
270 DEBUG("rlm_stg: stg_preacct() failed.");
271 return RLM_MODULE_REJECT;
274 count = toReply(result, request);
277 return RLM_MODULE_UPDATED;
279 return RLM_MODULE_NOOP;
283 * Write accounting information to this modules database.
285 static int stg_accounting(void* instance, REQUEST* request)
288 STG_PAIR* pairs = fromVPS(request->packet->vps);
290 const char* username = NULL;
291 const char* password = NULL;
293 DEBUG("rlm_stg: stg_accounting()");
297 if (request->username) {
298 username = request->username->data.strvalue;
299 DEBUG("rlm_stg: stg_accounting() request username field: '%s'", username);
302 if (request->password) {
303 password = request->password->data.strvalue;
304 DEBUG("rlm_stg: stg_accounting() request password field: '%s'", password);
307 result = stgAccountingImpl(username, password, pairs);
310 if (!result.modify && !result.reply) {
311 DEBUG("rlm_stg: stg_accounting() failed.");
312 return RLM_MODULE_REJECT;
315 count = toReply(result, request);
318 return RLM_MODULE_UPDATED;
320 return RLM_MODULE_OK;
324 * See if a user is already logged in. Sets request->simul_count to the
325 * current session count for this user and sets request->simul_mpp to 2
326 * if it looks like a multilink attempt based on the requested IP
327 * address, otherwise leaves request->simul_mpp alone.
329 * Check twice. If on the first pass the user exceeds his
330 * max. number of logins, do a second pass and validate all
331 * logins by querying the terminal server (using eg. SNMP).
333 static int stg_checksimul(void* instance, REQUEST* request)
335 DEBUG("rlm_stg: stg_checksimul()");
339 request->simul_count = 0;
341 return RLM_MODULE_OK;
344 static int stg_postauth(void* instance, REQUEST* request)
347 STG_PAIR* pairs = fromVPS(request->packet->vps);
349 const char* username = NULL;
350 const char* password = NULL;
352 DEBUG("rlm_stg: stg_postauth()");
356 if (request->username) {
357 username = request->username->data.strvalue;
358 DEBUG("rlm_stg: stg_postauth() request username field: '%s'", username);
361 if (request->password) {
362 password = request->password->data.strvalue;
363 DEBUG("rlm_stg: stg_postauth() request password field: '%s'", password);
366 result = stgPostAuthImpl(username, password, pairs);
369 if (!result.modify && !result.reply) {
370 DEBUG("rlm_stg: stg_postauth() failed.");
371 return RLM_MODULE_REJECT;
374 count = toReply(result, request);
377 return RLM_MODULE_UPDATED;
379 return RLM_MODULE_NOOP;
382 static int stg_detach(void* instance)
384 free(((struct rlm_stg_t*)instance)->address);
390 * The module name should be the only globally exported symbol.
391 * That is, everything else should be 'static'.
393 * If the module needs to temporarily modify it's instantiation
394 * data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
395 * The server will then take care of ensuring that the module
396 * is single-threaded.
401 RLM_TYPE_THREAD_SAFE, /* type */
402 stg_instantiate, /* instantiation */
403 stg_detach, /* detach */
405 stg_authenticate, /* authentication */
406 stg_authorize, /* authorization */
407 stg_preacct, /* preaccounting */
408 stg_accounting, /* accounting */
409 stg_checksimul, /* checksimul */
410 NULL, /* pre-proxy */
411 NULL, /* post-proxy */
412 stg_postauth /* post-auth */