This repository has been archived by the owner on Aug 16, 2022. It is now read-only.
forked from guillaumerose/radius-http-json
/
rlm_remotedb.c
255 lines (197 loc) · 6.53 KB
/
rlm_remotedb.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
/*
* rlm_remotedb.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>
*/
#include <freeradius-devel/ident.h>
RCSID("$Id$")
#include <freeradius-devel/radiusd.h>
#include <freeradius-devel/modules.h>
#include <freeradius-devel/sysutmp.h>
#include <json/json.h>
#include <curl/curl.h>
#define MAX_URI_LEN 1024
typedef struct rlm_remotedb_t {
char *ip;
int port;
char *base;
} rlm_remotedb_t;
static const CONF_PARSER module_config[] = {
{ "port", PW_TYPE_INTEGER, offsetof(rlm_remotedb_t, port), NULL, "80" },
{ "ip", PW_TYPE_STRING_PTR, offsetof(rlm_remotedb_t, ip), NULL, "127.0.0.1"},
{ "base", PW_TYPE_STRING_PTR, offsetof(rlm_remotedb_t, base), NULL, ""},
{ NULL, -1, 0, NULL, NULL }
};
static int remotedb_disable = 0;
static int
get_timestamp()
{
time_t timestamp;
time(×tamp);
return (int) timestamp;
}
static int
remotedb_instantiate(CONF_SECTION *conf, void **instance)
{
rlm_remotedb_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;
}
*instance = data;
return 0;
}
static int
remotedb_answer_builder(REQUEST *request, const char *password, const char *vlan)
{
VALUE_PAIR *pair;
radlog(L_DBG, "Building answer : password = %s, vlan = %s\n", password, vlan);
pair = pairmake("NT-Password", password, T_OP_SET);
pairmove(&request->config_items, &pair);
pairfree(&pair);
pair = pairmake("Tunnel-Private-Group-Id", vlan, T_OP_SET);
pairadd(&request->reply->vps, pair);
pair = pairmake("Tunnel-Medium-Type", "6", T_OP_SET);
pairadd(&request->reply->vps, pair);
pair = pairmake("Tunnel-Type", "13", T_OP_SET);
pairadd(&request->reply->vps, pair);
return RLM_MODULE_OK;
}
static size_t
remotedb_curl( void *ptr, size_t size, size_t nmemb, void *userdata)
{
REQUEST *request = (REQUEST *) userdata;
json_object * jobj = json_tokener_parse(ptr);
if ((int) jobj < 0) {
printf("Invalid json\n");
return nmemb * size;
}
struct json_object *jvlan;
struct json_object *jpassword;
if (json_object_get_type(jobj) != json_type_object) {
printf("Wrong type in field\n");
return nmemb * size;
}
if ((jvlan = json_object_object_get(jobj, "vlan")) == NULL) {
printf("vlan field needed\n");
return nmemb * size;
}
if ((jpassword = json_object_object_get(jobj, "password")) == NULL) {
printf("password field needed\n");
return nmemb * size;
}
remotedb_answer_builder(request, json_object_get_string(jpassword), json_object_get_string(jvlan));
json_object_put(jobj);
return nmemb * size;
}
static int
remotedb_authorize(void *instance, REQUEST *request)
{
if (remotedb_disable && get_timestamp() - remotedb_disable <= 30)
return RLM_MODULE_FAIL;
if (request->username == NULL)
return RLM_MODULE_NOOP;
rlm_remotedb_t *data = (rlm_remotedb_t *) instance;
char mac[1024] = "";
char uri[MAX_URI_LEN] = "";
radius_xlat(mac, 1024, "%{Calling-Station-Id}", request, NULL);
radlog(L_DBG, "Search with following options : mac address = %s, username = %s\n", mac, request->username->vp_strvalue);
snprintf(uri, MAX_URI_LEN, "http://%s:%d%s/authenticate?login=%s&mac=%s", data->ip, data->port, data->base, request->username->vp_strvalue, mac);
radlog(L_DBG, "Calling %s\n", uri);
CURL *curl;
CURLcode res = CURLE_FAILED_INIT;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, uri);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, remotedb_curl);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, request);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 1);
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 1);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
if (res != CURLE_OK) {
remotedb_disable = get_timestamp();
radlog(L_ERR, "Failed to call %s, retry in few seconds\n", uri);
return RLM_MODULE_FAIL;
}
return RLM_MODULE_OK;
}
static int
remotedb_accounting(void *instance, REQUEST *request)
{
VALUE_PAIR *vp;
time_t t;
char buf[80];
struct tm *ts;
if (request->packet->src_ipaddr.af != AF_INET) {
RDEBUG2("IPv6 is not supported!");
return RLM_MODULE_NOOP;
}
if ((vp = pairfind(request->packet->vps, PW_ACCT_STATUS_TYPE))==NULL) {
radlog(L_ERR, "rlm_remotedb: no Accounting-Status-Type attribute in request.");
return RLM_MODULE_NOOP;
}
printf("STATUS = %d\n", vp->vp_integer);
if (pairfind(request->packet->vps, PW_USER_NAME) == NULL)
return RLM_MODULE_NOOP;
t = request->timestamp;
ts = localtime(&t);
strftime(buf, sizeof(buf), "%a %Y-%m-%d %H:%M:%S %Z", ts);
printf("TIMESTAMP = %s\n", buf);
for (vp = request->packet->vps; vp; vp = vp->next) {
switch (vp->attribute) {
case PW_NAS_PORT_ID_STRING:
case PW_ACCT_SESSION_ID:
case PW_USER_NAME:
printf("DATA = %s\n", (char *)vp->vp_strvalue);
break;
}
}
return RLM_MODULE_OK;
}
static int
remotedb_detach(void *instance)
{
free(instance);
return 0;
}
module_t rlm_remotedb = {
RLM_MODULE_INIT,
"remotedb",
RLM_TYPE_THREAD_SAFE, /* type */
remotedb_instantiate, /* instantiation */
remotedb_detach, /* detach */
{
NULL, /* authentication */
remotedb_authorize, /* authorization */
NULL, /* preaccounting */
remotedb_accounting, /* accounting */
NULL, /* checksimul */
NULL, /* pre-proxy */
NULL, /* post-proxy */
NULL /* post-auth */
},
};