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);
68 static size_t toReply(STG_RESULT result, REQUEST* request)
72 count += toVPS(result.modify, &request->config_items);
73 count += toVPS(result.reply, &request->reply->vps);
75 deletePairs(result.modify);
76 deletePairs(result.reply);
81 static int countVPS(const VALUE_PAIR* pairs)
84 while (pairs != NULL) {
91 static STG_PAIR* fromVPS(const VALUE_PAIR* pairs)
93 unsigned size = countVPS(pairs);
94 STG_PAIR* res = (STG_PAIR*)malloc(sizeof(STG_PAIR) * (size + 1));
96 while (pairs != NULL) {
97 bzero(res[pos].key, sizeof(res[0].key));
98 bzero(res[pos].value, sizeof(res[0].value));
99 strncpy(res[pos].key, pairs->name, sizeof(res[0].key));
100 vp_prints_value(res[pos].value, sizeof(res[0].value), pairs, 0);
104 bzero(res[pos].key, sizeof(res[0].key));
105 bzero(res[pos].value, sizeof(res[0].value));
110 * Do any per-module initialization that is separate to each
111 * configured instance of the module. e.g. set up connections
112 * to external databases, read configuration files, set up
113 * dictionary entries, etc.
115 * If configuration information is given in the config section
116 * that must be referenced in later calls, store a handle to it
117 * in *instance otherwise put a null pointer there.
119 static int stg_instantiate(CONF_SECTION* conf, void** instance)
124 * Set up a storage area for instance data
126 data = rad_malloc(sizeof(*data));
130 memset(data, 0, sizeof(*data));
133 * If the configuration parameters can't be parsed, then
136 if (cf_section_parse(conf, data, module_config) < 0) {
141 if (!stgInstantiateImpl(data->address)) {
152 * Find the named user in this modules database. Create the set
153 * of attribute-value pairs to check and reply with for this user
154 * from the database. The authentication code only needs to check
155 * the password, the rest is done here.
157 static int stg_authorize(void* instance, REQUEST* request)
160 STG_PAIR* pairs = fromVPS(request->packet->vps);
162 const char* username = NULL;
163 const char* password = NULL;
167 DEBUG("rlm_stg: stg_authorize()");
169 if (request->username) {
170 username = request->username->data.strvalue;
171 DEBUG("rlm_stg: stg_authorize() request username field: '%s'", username);
174 if (request->password) {
175 password = request->password->data.strvalue;
176 DEBUG("rlm_stg: stg_authorize() request password field: '%s'", password);
179 result = stgAuthorizeImpl(username, password, pairs);
182 if (!result.modify && !result.reply) {
183 DEBUG("rlm_stg: stg_authorize() failed.");
184 return RLM_MODULE_REJECT;
187 count = toReply(result, request);
190 return RLM_MODULE_UPDATED;
192 return RLM_MODULE_NOOP;
196 * Authenticate the user with the given password.
198 static int stg_authenticate(void* instance, REQUEST* request)
201 STG_PAIR* pairs = fromVPS(request->packet->vps);
203 const char* username = NULL;
204 const char* password = NULL;
208 DEBUG("rlm_stg: stg_authenticate()");
210 if (request->username) {
211 username = request->username->data.strvalue;
212 DEBUG("rlm_stg: stg_authenticate() request username field: '%s'", username);
215 if (request->password) {
216 password = request->password->data.strvalue;
217 DEBUG("rlm_stg: stg_authenticate() request password field: '%s'", password);
220 result = stgAuthenticateImpl(username, password, pairs);
223 if (!result.modify && !result.reply) {
224 DEBUG("rlm_stg: stg_authenticate() failed.");
225 return RLM_MODULE_REJECT;
228 count = toReply(result, request);
231 return RLM_MODULE_UPDATED;
233 return RLM_MODULE_NOOP;
237 * Massage the request before recording it or proxying it
239 static int stg_preacct(void* instance, REQUEST* request)
242 STG_PAIR* pairs = fromVPS(request->packet->vps);
244 const char* username = NULL;
245 const char* password = NULL;
247 DEBUG("rlm_stg: stg_preacct()");
251 if (request->username) {
252 username = request->username->data.strvalue;
253 DEBUG("rlm_stg: stg_preacct() request username field: '%s'", username);
256 if (request->password) {
257 password = request->password->data.strvalue;
258 DEBUG("rlm_stg: stg_preacct() request password field: '%s'", password);
261 result = stgPreAcctImpl(username, password, pairs);
264 if (!result.modify && !result.reply) {
265 DEBUG("rlm_stg: stg_preacct() failed.");
266 return RLM_MODULE_REJECT;
269 count = toReply(result, request);
272 return RLM_MODULE_UPDATED;
274 return RLM_MODULE_NOOP;
278 * Write accounting information to this modules database.
280 static int stg_accounting(void* instance, REQUEST* request)
283 STG_PAIR* pairs = fromVPS(request->packet->vps);
285 const char* username = NULL;
286 const char* password = NULL;
288 DEBUG("rlm_stg: stg_accounting()");
292 if (request->username) {
293 username = request->username->data.strvalue;
294 DEBUG("rlm_stg: stg_accounting() request username field: '%s'", username);
297 if (request->password) {
298 password = request->password->data.strvalue;
299 DEBUG("rlm_stg: stg_accounting() request password field: '%s'", password);
302 result = stgAccountingImpl(username, password, pairs);
305 if (!result.modify && !result.reply) {
306 DEBUG("rlm_stg: stg_accounting() failed.");
307 return RLM_MODULE_REJECT;
310 count = toReply(result, request);
313 return RLM_MODULE_UPDATED;
315 return RLM_MODULE_OK;
319 * See if a user is already logged in. Sets request->simul_count to the
320 * current session count for this user and sets request->simul_mpp to 2
321 * if it looks like a multilink attempt based on the requested IP
322 * address, otherwise leaves request->simul_mpp alone.
324 * Check twice. If on the first pass the user exceeds his
325 * max. number of logins, do a second pass and validate all
326 * logins by querying the terminal server (using eg. SNMP).
328 static int stg_checksimul(void* instance, REQUEST* request)
330 DEBUG("rlm_stg: stg_checksimul()");
334 request->simul_count = 0;
336 return RLM_MODULE_OK;
339 static int stg_postauth(void* instance, REQUEST* request)
342 STG_PAIR* pairs = fromVPS(request->packet->vps);
344 const char* username = NULL;
345 const char* password = NULL;
347 DEBUG("rlm_stg: stg_postauth()");
351 if (request->username) {
352 username = request->username->data.strvalue;
353 DEBUG("rlm_stg: stg_postauth() request username field: '%s'", username);
356 if (request->password) {
357 password = request->password->data.strvalue;
358 DEBUG("rlm_stg: stg_postauth() request password field: '%s'", password);
361 result = stgPostAuthImpl(username, password, pairs);
364 if (!result.modify && !result.reply) {
365 DEBUG("rlm_stg: stg_postauth() failed.");
366 return RLM_MODULE_REJECT;
369 count = toReply(result, request);
372 return RLM_MODULE_UPDATED;
374 return RLM_MODULE_NOOP;
377 static int stg_detach(void* instance)
379 free(((struct rlm_stg_t*)instance)->address);
387 RLM_TYPE_THREAD_SAFE, /* type */
388 stg_instantiate, /* instantiation */
389 stg_detach, /* detach */
391 stg_authenticate, /* authentication */
392 stg_authorize, /* authorization */
393 stg_preacct, /* preaccounting */
394 stg_accounting, /* accounting */
395 stg_checksimul, /* checksimul */
396 NULL, /* pre-proxy */
397 NULL, /* post-proxy */
398 stg_postauth /* post-auth */