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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
|
/* See the file COPYING for licensing information. */
#include <string.h>
#include <assert.h>
#include <freeradius/libradius.h>
#include <event2/event.h>
#include <event2/bufferevent.h>
#include <radsec/radsec.h>
#include <radsec/radsec-impl.h>
#if defined DEBUG
#include <netdb.h>
#include <sys/socket.h>
#include "debug.h"
#endif
static int
_packet_create (struct rs_connection *conn, struct rs_packet **pkt_out)
{
struct rs_packet *p;
RADIUS_PACKET *rpkt;
*pkt_out = NULL;
rpkt = rad_alloc (1);
if (!rpkt)
return rs_err_conn_push (conn, RSE_NOMEM, __func__);
rpkt->id = conn->nextid++;
p = (struct rs_packet *) malloc (sizeof (struct rs_packet));
if (!p)
{
rad_free (&rpkt);
return rs_err_conn_push (conn, RSE_NOMEM, __func__);
}
memset (p, 0, sizeof (struct rs_packet));
p->conn = conn;
p->rpkt = rpkt;
*pkt_out = p;
return RSE_OK;
}
static int
_do_send (struct rs_packet *pkt)
{
int err;
if (rad_encode (pkt->rpkt, NULL, pkt->conn->active_peer->secret))
return rs_err_conn_push_fl (pkt->conn, RSE_FR, __FILE__, __LINE__,
"rad_encode: %s", fr_strerror ());
if (rad_sign (pkt->rpkt, NULL, pkt->conn->active_peer->secret))
return rs_err_conn_push_fl (pkt->conn, RSE_FR, __FILE__, __LINE__,
"rad_sign: %s", fr_strerror ());
assert (pkt->rpkt);
#if defined (DEBUG)
{
char host[80], serv[80];
getnameinfo (pkt->conn->active_peer->addr->ai_addr,
pkt->conn->active_peer->addr->ai_addrlen,
host, sizeof(host), serv, sizeof(serv),
0 /* NI_NUMERICHOST|NI_NUMERICSERV*/);
fprintf (stderr, "%s: about to send this to %s:%s:\n", __func__, host,
serv);
rs_dump_packet (pkt);
}
#endif
err = bufferevent_write (pkt->conn->bev, pkt->rpkt->data,
pkt->rpkt->data_len);
if (err < 0)
return rs_err_conn_push_fl (pkt->conn, RSE_EVENT, __FILE__, __LINE__,
"bufferevent_write: %s",
evutil_gai_strerror(err));
return RSE_OK;
}
static void
_event_cb (struct bufferevent *bev, short events, void *ctx)
{
struct rs_packet *pkt = (struct rs_packet *)ctx;
struct rs_connection *conn;
struct rs_peer *p;
int err;
assert (pkt);
assert (pkt->conn);
assert (pkt->conn->active_peer);
conn = pkt->conn;
p = conn->active_peer;
p->is_connecting = 0;
if (events & BEV_EVENT_CONNECTED)
{
p->is_connected = 1;
if (conn->callbacks.connected_cb)
conn->callbacks.connected_cb (conn->user_data);
#if defined (DEBUG)
fprintf (stderr, "%s: connected\n", __func__);
#endif
if (_do_send (pkt))
return;
if (conn->callbacks.sent_cb)
conn->callbacks.sent_cb (conn->user_data);
/* Packet will be freed in write callback. */
}
else if (events & BEV_EVENT_ERROR)
rs_err_conn_push_fl (pkt->conn, RSE_CONNERR, __FILE__, __LINE__, NULL);
}
static void
_write_cb (struct bufferevent *bev, void *ctx)
{
struct rs_packet *pkt = (struct rs_packet *) ctx;
assert (pkt);
assert (pkt->conn);
#if defined (DEBUG)
fprintf (stderr, "%s: packet written, breaking event loop\n", __func__);
#endif
if (event_base_loopbreak (pkt->conn->evb) < 0)
abort (); /* FIXME */
if (!pkt->conn->callbacks.sent_cb) /* Callback owns the packet now. */
rs_packet_destroy (pkt);
}
static void
_read_cb (struct bufferevent *bev, void *ctx)
{
struct rs_packet *pkt = (struct rs_packet *)ctx;
size_t n;
assert (pkt);
assert (pkt->conn);
pkt->rpkt->sockfd = pkt->conn->active_peer->fd; /* FIXME: Why? */
pkt->rpkt->vps = NULL; /* FIXME: Why? */
if (!pkt->hdr_read_flag)
{
n = bufferevent_read (pkt->conn->bev, pkt->hdr, RS_HEADER_LEN);
if (n == RS_HEADER_LEN)
{
pkt->hdr_read_flag = 1;
pkt->rpkt->data_len = (pkt->hdr[2] << 8) + pkt->hdr[3];
if (pkt->rpkt->data_len < 20 /* || len > 4096 */)
abort (); /* FIXME: Read and discard packet. */
pkt->rpkt->data = rs_malloc (pkt->conn->ctx, pkt->rpkt->data_len);
if (!pkt->rpkt->data)
{
rs_err_conn_push_fl (pkt->conn, RSE_NOMEM, __FILE__, __LINE__,
NULL);
abort (); /* FIXME: Read and discard packet. */
}
memcpy (pkt->rpkt->data, pkt->hdr, RS_HEADER_LEN);
bufferevent_setwatermark (pkt->conn->bev, EV_READ,
pkt->rpkt->data_len - RS_HEADER_LEN, 0);
#if defined (DEBUG)
fprintf (stderr, "%s: packet header read, total pkt len=%d\n",
__func__, pkt->rpkt->data_len);
#endif
}
else if (n < 0)
return; /* Buffer frozen. */
else
assert (!"short header");
}
#if defined (DEBUG)
printf ("%s: trying to read %d octets of packet data\n", __func__, pkt->rpkt->data_len - RS_HEADER_LEN);
#endif
n = bufferevent_read (pkt->conn->bev, pkt->rpkt->data + RS_HEADER_LEN, pkt->rpkt->data_len - RS_HEADER_LEN);
#if defined (DEBUG)
printf ("%s: read %d octets of packet data\n", __func__, n);
#endif
if (n == pkt->rpkt->data_len - RS_HEADER_LEN)
{
bufferevent_disable (pkt->conn->bev, EV_READ);
pkt->hdr_read_flag = 0;
memset (pkt->hdr, 0, sizeof(*pkt->hdr));
#if defined (DEBUG)
fprintf (stderr, "%s: complete packet read\n", __func__);
#endif
if (!rad_packet_ok (pkt->rpkt, 0) != 0)
return;
if (rad_decode (pkt->rpkt, NULL, pkt->conn->active_peer->secret) != 0)
return;
if (pkt->conn->callbacks.received_cb)
pkt->conn->callbacks.received_cb(pkt, pkt->conn->user_data);
if (event_base_loopbreak (pkt->conn->evb) < 0)
abort (); /* FIXME */
}
else if (n < 0)
return; /* Buffer frozen. */
else
assert (!"short packet");
}
static void
_evlog_cb (int severity, const char *msg)
{
const char *sevstr;
switch (severity)
{
case _EVENT_LOG_DEBUG:
#if !defined (DEBUG_LEVENT)
return;
#endif
sevstr = "debug";
break;
case _EVENT_LOG_MSG:
sevstr = "msg";
break;
case _EVENT_LOG_WARN:
sevstr = "warn";
break;
case _EVENT_LOG_ERR:
sevstr = "err";
break;
default:
sevstr = "???";
break;
}
fprintf (stderr, "libevent: [%s] %s\n", sevstr, msg);
}
static int
_init_evb (struct rs_connection *conn)
{
if (!conn->evb)
{
#if defined (DEBUG)
event_enable_debug_mode ();
#endif
event_set_log_callback (_evlog_cb);
conn->evb = event_base_new ();
if (!conn->evb)
return rs_err_conn_push_fl (conn, RSE_EVENT, __FILE__, __LINE__,
"event_base_new");
}
return RSE_OK;
}
static int
_init_socket (struct rs_connection *conn, struct rs_peer *p)
{
if (p->fd != -1)
return RSE_OK;
assert (p->addr);
p->fd = socket (p->addr->ai_family, p->addr->ai_socktype,
p->addr->ai_protocol);
if (p->fd < 0)
return rs_err_conn_push_fl (conn, RSE_SOME_ERROR, __FILE__, __LINE__,
strerror (errno));
if (evutil_make_socket_nonblocking (p->fd) < 0)
{
evutil_closesocket (p->fd);
return rs_err_conn_push_fl (conn, RSE_SOME_ERROR, __FILE__, __LINE__,
strerror (errno));
}
return RSE_OK;
}
static struct rs_peer *
_pick_peer (struct rs_connection *conn)
{
if (!conn->active_peer)
conn->active_peer = conn->peers;
return conn->active_peer;
}
static int
_init_bev (struct rs_connection *conn, struct rs_peer *peer)
{
if (!conn->bev)
{
conn->bev = bufferevent_socket_new (conn->evb, peer->fd, 0);
if (!conn->bev)
return rs_err_conn_push_fl (conn, RSE_EVENT, __FILE__, __LINE__,
"bufferevent_socket_new");
}
return RSE_OK;
}
static void
_do_connect (struct rs_peer *p)
{
int err;
err = bufferevent_socket_connect (p->conn->bev, p->addr->ai_addr,
p->addr->ai_addrlen);
if (err < 0)
rs_err_conn_push_fl (p->conn, RSE_EVENT, __FILE__, __LINE__,
"bufferevent_socket_connect: %s",
evutil_gai_strerror(err));
else
p->is_connecting = 1;
}
static int
_conn_open(struct rs_connection *conn, struct rs_packet *pkt)
{
struct rs_peer *p;
if (_init_evb (conn))
return -1;
p = _pick_peer (conn);
if (!p)
return rs_err_conn_push_fl (conn, RSE_NOPEER, __FILE__, __LINE__, NULL);
if (_init_socket (conn, p))
return -1;
if (_init_bev (conn, p))
return -1;
if (!p->is_connected)
if (!p->is_connecting)
_do_connect (p);
return RSE_OK;
}
static int
_conn_is_open_p (struct rs_connection *conn)
{
return conn->active_peer && conn->active_peer->is_connected;
}
/* Public functions. */
int
rs_packet_create_acc_request (struct rs_connection *conn,
struct rs_packet **pkt_out,
const char *user_name, const char *user_pw)
{
struct rs_packet *pkt;
struct rs_attr *attr;
if (_packet_create (conn, pkt_out))
return -1;
pkt = *pkt_out;
pkt->rpkt->code = PW_AUTHENTICATION_REQUEST;
if (user_name)
{
if (rs_attr_create (conn, &attr, "User-Name", user_name))
return -1;
rs_packet_add_attr (pkt, attr);
if (user_pw)
{
if (rs_attr_create (conn, &attr, "User-Password", user_pw))
return -1;
/* FIXME: need this too? rad_pwencode(user_pw, &pwlen, SECRET, reqauth) */
rs_packet_add_attr (pkt, attr);
}
}
return RSE_OK;
}
int
rs_packet_send (struct rs_packet *pkt, void *user_data)
{
struct rs_connection *conn;
assert (pkt);
conn = pkt->conn;
if (_conn_is_open_p (conn))
_do_send (pkt);
else
if (_conn_open (conn, pkt))
return -1;
assert (conn->evb);
assert (conn->bev);
assert (conn->active_peer);
assert (conn->active_peer->fd >= 0);
conn->user_data = user_data;
bufferevent_setcb (conn->bev, _read_cb, _write_cb, _event_cb, pkt);
if (!conn->user_dispatch_flag)
event_base_dispatch (conn->evb);
#if defined (DEBUG)
fprintf (stderr, "%s: event loop done\n", __func__);
assert (event_base_got_break(conn->evb));
#endif
return RSE_OK;
}
int
rs_conn_receive_packet (struct rs_connection *conn, struct rs_packet **pkt_out)
{
struct rs_packet *pkt;
assert (conn);
if (_packet_create (conn, pkt_out))
return -1;
pkt = *pkt_out;
pkt->conn = conn;
if (_conn_open (conn, pkt))
return -1;
assert (conn->evb);
assert (conn->bev);
assert (conn->active_peer);
assert (conn->active_peer->fd >= 0);
bufferevent_setwatermark (conn->bev, EV_READ, RS_HEADER_LEN, 0);
bufferevent_enable (conn->bev, EV_READ);
bufferevent_setcb (conn->bev, _read_cb, _write_cb, _event_cb, pkt);
if (!conn->user_dispatch_flag)
{
event_base_dispatch (conn->evb);
#if defined (DEBUG)
fprintf (stderr, "%s: event loop done", __func__);
if (event_base_got_break(conn->evb))
{
fprintf (stderr, ", got this:\n");
rs_dump_packet (pkt);
}
else
fprintf (stderr, ", no reply\n");
#endif
}
return RSE_OK;
}
void
rs_packet_add_attr(struct rs_packet *pkt, struct rs_attr *attr)
{
pairadd (&pkt->rpkt->vps, attr->vp);
attr->pkt = pkt;
}
struct radius_packet *
rs_packet_frpkt(struct rs_packet *pkt)
{
assert (pkt);
return pkt->rpkt;
}
void
rs_packet_destroy(struct rs_packet *pkt)
{
if (pkt)
{
rad_free (&pkt->rpkt);
rs_free (pkt->conn->ctx, pkt);
}
}
|