forked from guillaumerose/radius-mongodb
/
rlm_mongo.c
305 lines (251 loc) · 7.48 KB
/
rlm_mongo.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
/*
* rlm_mongo.c
*
* Version: $Id$
*
* 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 2010 Guillaume Rose <guillaume.rose@gmail.com>
* Copyright 2011 Roman Shterenzon <romanbsd@yahoo.com>
*/
#include <freeradius-devel/ident.h>
RCSID("$Id$")
#include <freeradius-devel/radiusd.h>
#include <freeradius-devel/modules.h>
#include "mongo.h"
#define MONGO_STRING_LENGTH 8196
typedef struct rlm_mongo_t {
char *ip;
int port;
char *base;
char *acct_base;
char *search_field;
char *username_field;
char *password_field;
char *mac_field;
char *enable_field;
char *only_stop;
} rlm_mongo_t;
static const CONF_PARSER module_config[] = {
{ "port", PW_TYPE_INTEGER, offsetof(rlm_mongo_t,port), NULL, "27017" },
{ "ip", PW_TYPE_STRING_PTR, offsetof(rlm_mongo_t,ip), NULL, "127.0.0.1"},
{ "base", PW_TYPE_STRING_PTR, offsetof(rlm_mongo_t,base), NULL, ""},
{ "acct_base", PW_TYPE_STRING_PTR, offsetof(rlm_mongo_t,acct_base), NULL, ""},
{ "search_field", PW_TYPE_STRING_PTR, offsetof(rlm_mongo_t,search_field), NULL, ""},
{ "username_field", PW_TYPE_STRING_PTR, offsetof(rlm_mongo_t,username_field), NULL, ""},
{ "password_field", PW_TYPE_STRING_PTR, offsetof(rlm_mongo_t,password_field), NULL, ""},
{ "mac_field", PW_TYPE_STRING_PTR, offsetof(rlm_mongo_t,mac_field), NULL, ""},
{ "enable_field", PW_TYPE_STRING_PTR, offsetof(rlm_mongo_t,enable_field), NULL, ""},
{ "only_stop", PW_TYPE_STRING_PTR, offsetof(rlm_mongo_t,only_stop), NULL, ""},
{ NULL, -1, 0, NULL, NULL } /* end the list */
};
mongo_connection conn[1];
int mongo_start(rlm_mongo_t *data)
{
if (mongo_connect(conn, data->ip, data->port)){
radlog(L_ERR, "rlm_mongodb: Failed to connect");
return 0;
}
radlog(L_DBG, "Connected to MongoDB");
return 1;
}
void find_in_array(bson_iterator *it, char *key_ref, char *value_ref, char *key_needed, char *value_needed)
{
char value_ref_found[MONGO_STRING_LENGTH];
char value_needed_found[MONGO_STRING_LENGTH];
bson_iterator i;
while(bson_iterator_next(it)) {
switch(bson_iterator_type(it)){
case bson_string:
if (strcmp(bson_iterator_key(it), key_ref) == 0)
strcpy(value_ref_found, bson_iterator_string(it));
if (strcmp(bson_iterator_key(it), key_needed) == 0)
strcpy(value_needed_found, bson_iterator_string(it));
break;
case bson_object:
case bson_array:
bson_iterator_init(&i, bson_iterator_value(it));
find_in_array(&i, key_ref, value_ref, key_needed, value_needed);
break;
default:
break;
}
}
if (strcmp(value_ref_found, value_ref) == 0)
strcpy(value_needed, value_needed_found);
}
int find_radius_options(rlm_mongo_t *data, char *username, char *mac, char *password)
{
bson_buffer bb;
bson query;
bson field;
bson result;
bson_buffer_init(&bb);
bson_append_string(&bb, data->search_field, username);
if (strcmp(data->mac_field, "") != 0) {
bson_append_string(&bb, data->mac_field, mac);
}
if (strcmp(data->enable_field, "") != 0) {
bson_append_bool(&bb, data->enable_field, 1);
}
bson_from_buffer(&query, &bb);
bson_buffer_destroy(&bb);
bson_empty(&field);
bson_empty(&result);
MONGO_TRY{
if (mongo_find_one(conn, data->base, &query, &field, &result) == 0) {
return 0;
}
}MONGO_CATCH{
mongo_start(data);
return 0;
}
bson_iterator it;
bson_iterator_init(&it, result.data);
find_in_array(&it, data->username_field, username, data->password_field, password);
bson_destroy(&result);
return 1;
}
static int mongo_instantiate(CONF_SECTION *conf, void **instance)
{
rlm_mongo_t *data;
data = rad_malloc(sizeof(*data));
if (!data) {
return -1;
}
memset(data, 0, sizeof(*data));
if (cf_section_parse(conf, data, module_config) < 0) {
free(data);
return -1;
}
mongo_start(data);
*instance = data;
return 0;
}
static void format_mac(char *in, char *out) {
int i;
for (i = 0; i < 6; i++) {
out[3 * i] = in[2 * i];
out[3 * i + 1] = in[2 * i + 1];
out[3 * i + 2] = ':';
}
out[17] = '\0';
}
static int mongo_authorize(void *instance, REQUEST *request)
{
if (request->username == NULL)
return RLM_MODULE_NOOP;
rlm_mongo_t *data = (rlm_mongo_t *) instance;
char password[MONGO_STRING_LENGTH] = "";
char mac[MONGO_STRING_LENGTH] = "";
if (strcmp(data->mac_field, "") != 0) {
char mac_temp[MONGO_STRING_LENGTH] = "";
radius_xlat(mac_temp, MONGO_STRING_LENGTH, "%{Calling-Station-Id}", request, NULL);
format_mac(mac_temp, mac);
}
if (!find_radius_options(data, request->username->vp_strvalue, mac, password)) {
return RLM_MODULE_REJECT;
}
RDEBUG("Authorisation request by username -> \"%s\"\n", request->username->vp_strvalue);
RDEBUG("Password found in MongoDB -> \"%s\"\n\n", password);
VALUE_PAIR *vp;
/* quiet the compiler */
instance = instance;
request = request;
vp = pairmake("Cleartext-Password", password, T_OP_SET);
if (!vp) return RLM_MODULE_FAIL;
pairmove(&request->config_items, &vp);
pairfree(&vp);
return RLM_MODULE_OK;
}
/* Saves accounting information */
static int mongo_account(void *instance, REQUEST *request)
{
rlm_mongo_t *data = (rlm_mongo_t *)instance;
bson b;
bson_buffer buf;
const char *attr;
char value[MAX_STRING_LEN+1];
VALUE_PAIR *vp = request->packet->vps;
// shall we insert this packet or not
int insert;
bson_buffer_init(&buf);
bson_append_new_oid(&buf, "_id");
insert = 0;
while (vp) {
attr = vp->name;
if ((strcmp(attr, "Acct-Status-Type") == 0) && ((strcmp(data->only_stop, "") != 0))) {
if ((vp->vp_integer & 0xffffff) != 2) {
break;
} else {
insert = 1;
}
}
switch (vp->type) {
case PW_TYPE_INTEGER:
bson_append_int(&buf, attr, vp->vp_integer & 0xffffff);
break;
case PW_TYPE_BYTE:
case PW_TYPE_SHORT:
bson_append_int(&buf, attr, vp->vp_integer);
break;
case PW_TYPE_DATE:
bson_append_time_t(&buf, attr, vp->vp_date);
break;
default:
vp_prints_value(value, sizeof(value), vp, 0);
bson_append_string(&buf, attr, value);
// akh
RDEBUG("mongo default insert %s", value);
break;
}
vp = vp->next;
}
bson_from_buffer(&b, &buf);
MONGO_TRY {
if (insert == 1) {
mongo_insert(conn, data->acct_base, &b);
RDEBUG("accounting record was inserted");
}
} MONGO_CATCH {
radlog(L_ERR, "mongo_insert failed");
return RLM_MODULE_FAIL;
}
bson_destroy(&b);
return RLM_MODULE_OK;
}
static int mongo_detach(void *instance)
{
free(instance);
return 0;
}
module_t rlm_mongo = {
RLM_MODULE_INIT,
"mongo",
RLM_TYPE_THREAD_SAFE, /* type */
mongo_instantiate, /* instantiation */
mongo_detach, /* detach */
{
NULL, /* authentication */
mongo_authorize, /* authorization */
NULL, /* preaccounting */
mongo_account, /* accounting */
NULL, /* checksimul */
NULL, /* pre-proxy */
NULL, /* post-proxy */
NULL /* post-auth */
},
};