-
Notifications
You must be signed in to change notification settings - Fork 0
/
rlm_xmlrpc.c
420 lines (362 loc) · 11.3 KB
/
rlm_xmlrpc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
* rlm_xmlrpc.c
*
* Version: $0.1$
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*
* Copyright 2000,2006 The FreeRADIUS server project
* Copyright 2011 Angelo Compagnucci <angelo.compagnucci@gmail.com>
*/
#include <freeradius/ident.h>
RCSID("$Id$")
#include <freeradius/radiusd.h>
#include <freeradius/modules.h>
#include <xmlrpc-c/base.h>
#include <xmlrpc-c/client.h>
#define NAME "Freeradius rlm_xmlrpc"
#define VERSION "0.1"
#ifdef HAVE_PTHREAD_H
#endif
/*
* Define a structure for our module configuration.
*
* These variables do not need to be in a structure, but it's
* a lot cleaner to do so, and a pointer to the structure can
* be used as the instance handle.
*/
typedef struct rlm_xmlrpc_client_t {
xmlrpc_server_info *serverInfoP;
xmlrpc_env env;
xmlrpc_client *clientP;
struct rlm_xmlrpc_client_t *next;
} rlm_xmlrpc_client_t;
typedef struct rlm_xmlrpc_t {
char *url;
char *method;
char *interface;
char *auth_type;
char *user;
char *password;
int no_ssl_verify_peer;
int no_ssl_verify_host;
int xmlrpc_num_socks;
rlm_xmlrpc_client_t *client;
#ifdef HAVE_PTHREAD_H
pthread_mutex_t client_mutex;
#endif
} rlm_xmlrpc_t;
void free_instance(rlm_xmlrpc_t * instance)
{
rlm_xmlrpc_t *inst = instance;
rlm_xmlrpc_client_t *next;
rlm_xmlrpc_client_t *cur;
cur = inst->client;
while (cur->next) {
next = cur->next;
xmlrpc_env_clean(&cur->env);
xmlrpc_server_info_free(cur->serverInfoP);
xmlrpc_client_destroy(cur->clientP);
free(cur);
cur = next;
}
if (cur) {
xmlrpc_env_clean(&cur->env);
xmlrpc_server_info_free(cur->serverInfoP);
xmlrpc_client_destroy(cur->clientP);
free(cur);
}
xmlrpc_client_teardown_global_const();
pthread_mutex_destroy(&inst->client_mutex);
free(inst);
}
int check_error_and_free(rlm_xmlrpc_t * instance)
{
xmlrpc_env *env = &instance->client->env;
char *error = env->fault_string;
if (env->fault_occurred) {
radlog(L_ERR, "rlm_xmlrpc: %s", error);
DEBUG("rlm_xmlrpc: %s", error);
free_instance(instance);
return RLM_MODULE_FAIL;
}
return RLM_MODULE_OK;
}
int check_error(xmlrpc_env * env)
{
char *error = env->fault_string;
if (env->fault_occurred) {
radlog(L_ERR, "rlm_xmlrpc: %s", error);
DEBUG("rlm_xmlrpc: %s", error);
return RLM_MODULE_FAIL;
}
return RLM_MODULE_OK;
}
/*
* This method returns a pointer to a client. Thread use this method
* to ensure a proper access to a member of the clients linked list.
* Mutex is used to grant a round robin access to clients.
*/
rlm_xmlrpc_client_t *get_client(rlm_xmlrpc_t * instance)
{
rlm_xmlrpc_client_t *client;
#ifdef HAVE_PTHREAD_H
pthread_mutex_lock(&instance->client_mutex);
#endif
client = instance->client;
instance->client = instance->client->next;
#ifdef HAVE_PTHREAD_H
pthread_mutex_unlock(&instance->client_mutex);
#endif
return client;
}
/*
* A mapping of configuration file names to internal variables.
*
* Note that the string is dynamically allocated, so it MUST
* be freed. When the configuration file parse re-reads the string,
* it free's the old one, and strdup's the new one, placing the pointer
* to the strdup'd string into 'config.string'. This gets around
* buffer over-flows.
*/
static const CONF_PARSER module_config[] = {
{"url", PW_TYPE_STRING_PTR, offsetof(rlm_xmlrpc_t, url), NULL, NULL},
{"method", PW_TYPE_STRING_PTR, offsetof(rlm_xmlrpc_t, method), NULL, NULL},
{"interface", PW_TYPE_STRING_PTR, offsetof(rlm_xmlrpc_t, interface), NULL, "lo"},
{"no_ssl_verify_peer", PW_TYPE_BOOLEAN, offsetof(rlm_xmlrpc_t, no_ssl_verify_peer), NULL, "no"},
{"no_ssl_verify_host", PW_TYPE_BOOLEAN, offsetof(rlm_xmlrpc_t, no_ssl_verify_host), NULL, "no"},
{"xmlrpc_num_socks", PW_TYPE_INTEGER, offsetof(rlm_xmlrpc_t, xmlrpc_num_socks), NULL, "5"},
{"auth_type", PW_TYPE_STRING_PTR, offsetof(rlm_xmlrpc_t, auth_type), NULL, "none"},
{"user", PW_TYPE_STRING_PTR, offsetof(rlm_xmlrpc_t, user), NULL, NULL},
{"password", PW_TYPE_STRING_PTR, offsetof(rlm_xmlrpc_t, password), NULL, NULL},
{NULL, -1, 0, NULL, NULL} /* end the list */
};
/*
* Do any per-module initialization that is separate to each
* configured instance of the module. e.g. set up connections
* to external databases, read configuration files, set up
* dictionary entries, etc.
*
* If configuration information is given in the config section
* that must be referenced in later calls, store a handle to it
* in *instance otherwise put a null pointer there.
*/
static int xmlrpc_instantiate(CONF_SECTION * conf, void **instance)
{
rlm_xmlrpc_t *data;
rlm_xmlrpc_client_t *first_client;
rlm_xmlrpc_client_t *client;
xmlrpc_env env;
struct xmlrpc_clientparms clientParms;
struct xmlrpc_curl_xportparms curlParms;
int i, error;
void (*do_auth) ();
/*
* Set up a storage area for instance data
*/
data = rad_malloc(sizeof(*data));
if (!data) {
return -1;
}
memset(data, 0, sizeof(*data));
/*
* If the configuration parameters can't be parsed, then
* fail.
*/
if (cf_section_parse(conf, data, module_config) < 0) {
free(data);
return -1;
}
*instance = data;
#ifdef HAVE_PTHREAD_H
pthread_mutex_init(&data->client_mutex, NULL);
#endif
/*
* network_interface parameter cannot be omitted because the
* XMLRPC_CXPSIZE macro calcs the size from the first parameter
* to the last modified.
* Unfortunately is this the order.
*/
curlParms.network_interface = data->interface;
curlParms.no_ssl_verifypeer = data->no_ssl_verify_peer;
curlParms.no_ssl_verifyhost = data->no_ssl_verify_host;
clientParms.transport = "curl";
clientParms.transportparmsP = &curlParms;
clientParms.transportparm_size = XMLRPC_CXPSIZE(no_ssl_verifyhost);
/*
* Choosing method authentication
*/
if (strcmp(data->auth_type, "auth_basic") == 0) {
do_auth = xmlrpc_server_info_allow_auth_basic;
} else if (strcmp(data->auth_type, "auth_digest") == 0) {
do_auth = xmlrpc_server_info_allow_auth_digest;
} else if (strcmp(data->auth_type, "auth_negotiate") == 0) {
do_auth = xmlrpc_server_info_allow_auth_negotiate;
} else if (strcmp(data->auth_type, "auth_ntlm") == 0) {
do_auth = xmlrpc_server_info_allow_auth_ntlm;
}
/*
* Clients are created into a circular linked list.
* Into this cycle we setup clients and server information objects.
* Server information contains a method to do html authentication.
*/
for (i = 0; i < data->xmlrpc_num_socks; i++) {
client = rad_malloc(sizeof(*client));
if (!client) {
return -1;
}
memset(client, 0, sizeof(*client));
env = client->env;
xmlrpc_env_init(&env);
if (i == 0) {
data->client = client;
first_client = client;
xmlrpc_client_setup_global_const(&env);
} else {
data->client->next = client;
data->client = data->client->next;
}
xmlrpc_client_create(&env, XMLRPC_CLIENT_NO_FLAGS, NAME,
VERSION, &clientParms,
XMLRPC_CPSIZE(transportparm_size), &client->clientP);
error = check_error_and_free(data);
if (error != RLM_MODULE_OK)
return error;
client->serverInfoP = xmlrpc_server_info_new(&env, data->url);
error = check_error_and_free(data);
if (error != RLM_MODULE_OK)
return error;
if (strcmp(data->auth_type, "none") != 0) {
xmlrpc_server_info_set_user(&env, client->serverInfoP,
data->user, data->password);
error = check_error_and_free(data);
if (error != RLM_MODULE_OK)
return error;
do_auth(&env, client->serverInfoP);
error = check_error_and_free(data);
if (error != RLM_MODULE_OK)
return error;
radlog(L_INFO, "\trlm_xmlrpc: client #%d logged in as %s", i, data->user);
}
radlog(L_INFO, "\trlm_xmlrpc: client #%d initialized", i);
}
/*
* closing the circular linked list. data->client helds a pointer
* to the last client used by a thread.
*/
data->client->next = first_client;
data->client = data->client->next;
return 0;
}
/*
* Write accounting information to this modules database.
*/
static int xmlrpc_accounting(void *instance, REQUEST * request)
{
rlm_xmlrpc_t *inst = instance;
rlm_xmlrpc_client_t *client;
VALUE_PAIR *vps = request->packet->vps;
xmlrpc_value *array_param;
xmlrpc_value *array_string;
xmlrpc_value *resultP;
char *const methodName = inst->method;
int error;
VALUE_PAIR *status_type_pair;
if ((status_type_pair = pairfind(request->packet->vps, PW_ACCT_STATUS_TYPE)) == NULL) {
radlog(L_ERR, "rlm_xmlrpc: No Accounting-Status-Type record.");
return RLM_MODULE_NOOP;
}
client = get_client(instance);
/*
* Xmlrpc wants method params in an array, so we build an array
* with a pointer in index 0. This pointer contains a sub array
* filled whith strings each one for packet attributes.
*/
array_param = xmlrpc_array_new(&client->env);
error = check_error(&client->env);
if (error != RLM_MODULE_OK)
return error;
array_string = xmlrpc_array_new(&client->env);
error = check_error(&client->env);
if (error != RLM_MODULE_OK)
return error;
/*
* The array of strings is built whit vp_prints
*/
VALUE_PAIR *vp = vps;
for (; vp; vp = vp->next) {
char buf[1024];
vp_prints(buf, sizeof(buf), vp);
xmlrpc_array_append_item(&client->env, array_string,
xmlrpc_string_new(&client->env, buf));
int error = check_error(&client->env);
if (error != RLM_MODULE_OK)
return error;
}
xmlrpc_array_append_item(&client->env, array_param, array_string);
error = check_error(&client->env);
if (error != RLM_MODULE_OK)
return error;
xmlrpc_client_call2(&client->env, client->clientP, client->serverInfoP,
methodName, array_param, &resultP);
error = check_error(&client->env);
if (error != RLM_MODULE_OK)
return error;
/*
* We don't check for method return value. If an accounting packet is
* dispatched without errors, it should be processed by server
* without any further notification.
*/
xmlrpc_DECREF(resultP);
xmlrpc_DECREF(array_param);
xmlrpc_DECREF(array_string);
return RLM_MODULE_OK;
}
/*
* Only free memory we allocated. The strings allocated via
* cf_section_parse() do not need to be freed.
*/
static int xmlrpc_detach(void *instance)
{
free_instance(instance);
return 0;
}
/*
* The module name should be the only globally exported symbol.
* That is, everything else should be 'static'.
*
* If the module needs to temporarily modify it's instantiation
* data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
* The server will then take care of ensuring that the module
* is single-threaded.
*/
module_t rlm_xmlrpc = {
RLM_MODULE_INIT,
"xmlrpc",
RLM_TYPE_THREAD_SAFE, /* type */
xmlrpc_instantiate, /* instantiation */
xmlrpc_detach, /* detach */
{
NULL, /* authentication */
NULL, /* authorization */
NULL, /* preaccounting */
xmlrpc_accounting, /* accounting */
NULL, /* checksimul */
NULL, /* pre-proxy */
NULL, /* post-proxy */
NULL /* post-auth */
}
,
};