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 $
38 #include "stg_client.h"
42 volatile time_t stgTime;
45 * Define a structure for our module configuration.
47 * These variables do not need to be in a structure, but it's
48 * a lot cleaner to do so, and a pointer to the structure can
49 * be used as the instance handle.
51 typedef struct rlm_stg_t {
59 * A mapping of configuration file names to internal variables.
61 * Note that the string is dynamically allocated, so it MUST
62 * be freed. When the configuration file parse re-reads the string,
63 * it free's the old one, and strdup's the new one, placing the pointer
64 * to the strdup'd string into 'config.string'. This gets around
67 static CONF_PARSER module_config[] = {
68 { "password", PW_TYPE_STRING_PTR, offsetof(rlm_stg_t,password), NULL, NULL},
69 { "server", PW_TYPE_STRING_PTR, offsetof(rlm_stg_t,server), NULL, NULL},
70 { "port", PW_TYPE_INTEGER, offsetof(rlm_stg_t,port), NULL, "5555" },
71 { "local_port", PW_TYPE_INTEGER, offsetof(rlm_stg_t,localPort), NULL, "0" },
73 { NULL, -1, 0, NULL, NULL } /* end the list */
77 * Do any per-module initialization that is separate to each
78 * configured instance of the module. e.g. set up connections
79 * to external databases, read configuration files, set up
80 * dictionary entries, etc.
82 * If configuration information is given in the config section
83 * that must be referenced in later calls, store a handle to it
84 * in *instance otherwise put a null pointer there.
86 static int stg_instantiate(CONF_SECTION *conf, void **instance)
91 * Set up a storage area for instance data
93 DEBUG("rlm_stg: stg_instantiate()");
94 data = (rlm_stg_t *)rad_malloc(sizeof(rlm_stg_t));
98 memset(data, 0, sizeof(rlm_stg_t));
101 * If the configuration parameters can't be parsed, then
104 if (cf_section_parse(conf, data, module_config) < 0) {
109 cli = new STG_CLIENT();
110 cli->SetServer(data->server);
111 cli->SetPort(data->port);
112 cli->SetLocalPort(data->localPort);
113 cli->SetPassword(data->password);
115 DEBUG("rlm_stg: stg_instantiate() error: '%s'", cli->GetError().c_str());
125 * Find the named user in this modules database. Create the set
126 * of attribute-value pairs to check and reply with for this user
127 * from the database. The authentication code only needs to check
128 * the password, the rest is done here.
130 static int stg_authorize(void *instance, REQUEST *request)
135 DEBUG("rlm_stg: stg_authorize()");
137 /* quiet the compiler */
141 uname = pairfind(request->packet->vps, PW_USER_NAME);
143 DEBUG("rlm_stg: stg_authorize() user name defined as '%s'", uname->vp_strvalue);
145 DEBUG("rlm_stg: stg_authorize() user name undefined");
146 return RLM_MODULE_FAIL;
148 if (request->username) {
149 DEBUG("rlm_stg: stg_authorize() request username field: '%s'", request->username->vp_strvalue);
151 if (request->password) {
152 DEBUG("rlm_stg: stg_authorize() request password field: '%s'", request->password->vp_strvalue);
154 // Here we need to define Framed-Protocol
155 svc = pairfind(request->packet->vps, PW_SERVICE_TYPE);
157 DEBUG("rlm_stg: stg_authorize() Service-Type defined as '%s'", svc->vp_strvalue);
158 if (cli->Authorize((const char *)request->username->vp_strvalue, (const char *)svc->vp_strvalue)) {
159 DEBUG("rlm_stg: stg_authorize() stg status: '%s'", cli->GetError().c_str());
160 return RLM_MODULE_REJECT;
163 DEBUG("rlm_stg: stg_authorize() Service-Type undefined");
164 if (cli->Authorize((const char *)request->username->vp_strvalue, "")) {
165 DEBUG("rlm_stg: stg_authorize() stg status: '%s'", cli->GetError().c_str());
166 return RLM_MODULE_REJECT;
169 pwd = pairmake("Cleartext-Password", cli->GetUserPassword().c_str(), T_OP_SET);
170 pairadd(&request->config_items, pwd);
171 //pairadd(&request->reply->vps, uname);
173 return RLM_MODULE_UPDATED;
177 * Authenticate the user with the given password.
179 static int stg_authenticate(void *instance, REQUEST *request)
181 /* quiet the compiler */
185 DEBUG("rlm_stg: stg_authenticate()");
186 svc = pairfind(request->packet->vps, PW_SERVICE_TYPE);
188 DEBUG("rlm_stg: stg_authenticate() Service-Type defined as '%s'", svc->vp_strvalue);
189 if (cli->Authenticate((char *)request->username->vp_strvalue, (const char *)svc->vp_strvalue)) {
190 DEBUG("rlm_stg: stg_authenticate() stg status: '%s'", cli->GetError().c_str());
191 return RLM_MODULE_REJECT;
194 DEBUG("rlm_stg: stg_authenticate() Service-Type undefined");
195 if (cli->Authenticate((char *)request->username->vp_strvalue, "")) {
196 DEBUG("rlm_stg: stg_authenticate() stg status: '%s'", cli->GetError().c_str());
197 return RLM_MODULE_REJECT;
201 return RLM_MODULE_NOOP;
205 * Massage the request before recording it or proxying it
207 static int stg_preacct(void *instance, REQUEST *request)
209 /* quiet the compiler */
212 DEBUG("rlm_stg: stg_preacct()");
214 return RLM_MODULE_OK;
218 * Write accounting information to this modules database.
220 static int stg_accounting(void *instance, REQUEST *request)
222 /* quiet the compiler */
226 svc = pairfind(request->packet->vps, PW_SERVICE_TYPE);
229 DEBUG("rlm_stg: stg_accounting()");
231 sessid = pairfind(request->packet->vps, PW_ACCT_SESSION_ID);
233 DEBUG("rlm_stg: stg_accounting() Acct-Session-ID undefined");
234 return RLM_MODULE_FAIL;
236 sttype = pairfind(request->packet->vps, PW_ACCT_STATUS_TYPE);
238 DEBUG("Acct-Status-Type := %s", sttype->vp_strvalue);
240 DEBUG("rlm_stg: stg_accounting() Service-Type defined as '%s'", svc->vp_strvalue);
241 if (cli->Account((const char *)sttype->vp_strvalue, (const char *)request->username->vp_strvalue, (const char *)svc->vp_strvalue, (const char *)sessid->vp_strvalue)) {
242 DEBUG("rlm_stg: stg_accounting error: '%s'", cli->GetError().c_str());
243 return RLM_MODULE_FAIL;
246 DEBUG("rlm_stg: stg_accounting() Service-Type undefined");
247 if (cli->Account((const char *)sttype->vp_strvalue, (const char *)request->username->vp_strvalue, "", (const char *)sessid->vp_strvalue)) {
248 DEBUG("rlm_stg: stg_accounting error: '%s'", cli->GetError().c_str());
249 return RLM_MODULE_FAIL;
253 DEBUG("Acct-Status-Type := NULL");
256 return RLM_MODULE_OK;
260 * See if a user is already logged in. Sets request->simul_count to the
261 * current session count for this user and sets request->simul_mpp to 2
262 * if it looks like a multilink attempt based on the requested IP
263 * address, otherwise leaves request->simul_mpp alone.
265 * Check twice. If on the first pass the user exceeds his
266 * max. number of logins, do a second pass and validate all
267 * logins by querying the terminal server (using eg. SNMP).
269 static int stg_checksimul(void *instance, REQUEST *request)
272 DEBUG("rlm_stg: stg_checksimul()");
274 request->simul_count=0;
276 return RLM_MODULE_OK;
279 static int stg_postauth(void *instance, REQUEST *request)
285 DEBUG("rlm_stg: stg_postauth()");
286 svc = pairfind(request->packet->vps, PW_SERVICE_TYPE);
288 DEBUG("rlm_stg: stg_postauth() Service-Type defined as '%s'", svc->vp_strvalue);
289 if (cli->PostAuthenticate((const char *)request->username->vp_strvalue, (const char *)svc->vp_strvalue)) {
290 DEBUG("rlm_stg: stg_postauth() error: '%s'", cli->GetError().c_str());
291 return RLM_MODULE_FAIL;
294 DEBUG("rlm_stg: stg_postauth() Service-Type undefined");
295 if (cli->PostAuthenticate((const char *)request->username->vp_strvalue, "")) {
296 DEBUG("rlm_stg: stg_postauth() error: '%s'", cli->GetError().c_str());
297 return RLM_MODULE_FAIL;
300 if (strncmp((const char *)svc->vp_strvalue, "Framed-User", 11) == 0) {
301 fip.s_addr = cli->GetFramedIP();
302 DEBUG("rlm_stg: stg_postauth() ip = '%s'", inet_ntostring(fip.s_addr).c_str());
303 fia = pairmake("Framed-IP-Address", inet_ntostring(fip.s_addr).c_str(), T_OP_SET);
304 pairadd(&request->reply->vps, fia);
307 return RLM_MODULE_UPDATED;
310 static int stg_detach(void *instance)
312 DEBUG("rlm_stg: stg_detach()");
315 free(((struct rlm_stg_t *)instance)->server);
316 free(((struct rlm_stg_t *)instance)->password);
322 * The module name should be the only globally exported symbol.
323 * That is, everything else should be 'static'.
325 * If the module needs to temporarily modify it's instantiation
326 * data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
327 * The server will then take care of ensuring that the module
328 * is single-threaded.
333 RLM_TYPE_THREAD_SAFE, /* type */
334 stg_instantiate, /* instantiation */
335 stg_detach, /* detach */
337 stg_authenticate, /* authentication */
338 stg_authorize, /* authorization */
339 stg_preacct, /* preaccounting */
340 stg_accounting, /* accounting */
341 stg_checksimul, /* checksimul */
342 NULL, /* pre-proxy */
343 NULL, /* post-proxy */
344 stg_postauth /* post-auth */