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
|
/* RADIUS/RadSec client using libradsec in user dispatch mode. */
#include <stdio.h>
#include <string.h>
#include <radsec/radsec.h>
#include <event2/event.h>
#include "debug.h" /* For rs_dump_packet(). */
#define CONFIG "dispatching-tls"
#define CONFIG_FILE "examples/client.conf"
#define SECRET "sikrit"
#define USER_NAME "molgan@PROJECT-MOONSHOT.ORG"
#define USER_PW "password"
struct state {
struct rs_packet *msg;
unsigned packet_sent_flag : 1;
unsigned packet_received_flag : 1;
};
static void
connected_cb (void *user_data)
{
printf ("%s\n", __FUNCTION__);
}
static void
disconnected_cb (void *user_data)
{
printf ("%s\n", __FUNCTION__);
}
static void
msg_received_cb (struct rs_packet *packet, void *user_data)
{
struct state *state = (struct state *) user_data;
printf ("%s\n", __FUNCTION__);
state->msg = packet;
state->packet_received_flag = 1;
}
static void
msg_sent_cb (void *user_data)
{
struct state *state = (struct state *) user_data;
printf ("%s\n", __FUNCTION__);
rs_packet_destroy (state->msg);
state->packet_sent_flag = 1;
}
struct rs_error *
dispatching_client (struct rs_context *ctx)
{
struct rs_connection *conn = NULL;
struct rs_conn_callbacks cb = { connected_cb, disconnected_cb,
msg_received_cb, msg_sent_cb };
struct rs_packet *req_msg = NULL;
struct rs_error *err = NULL;
struct state state;
memset (&state, 0, sizeof (state));
if (rs_conn_create(ctx, &conn, CONFIG))
goto out;
rs_conn_set_callbacks (conn, &cb);
if (rs_packet_create_authn_request (conn, &req_msg,
USER_NAME, USER_PW, SECRET))
goto out;
/* Doesn't really send the message but rather queues it for sending.
msg_received_cb() will be invoked with user_data = &state when
the message has been sent. */
if (rs_packet_send (req_msg, &state))
goto out;
while (1)
{
if (rs_conn_dispatch (conn))
goto out;
if (state.packet_received_flag)
{
rs_dump_packet (state.msg); /* debug printout */
if (rs_packet_code (state.msg) == PW_ACCESS_ACCEPT)
printf ("Good auth.\n");
else
printf ("Bad auth: %d\n", rs_packet_code (state.msg));
rs_packet_destroy (state.msg);
break;
}
}
if (rs_conn_destroy(conn))
goto out;
conn = NULL;
out:
err = rs_err_ctx_pop (ctx);
if (err == RSE_OK)
err = rs_err_conn_pop (conn);
if (conn)
rs_conn_destroy(conn);
return err;
}
int
main (int argc, char *argv[])
{
struct rs_error *err = NULL;
struct rs_context *ctx = NULL;
if (rs_context_create(&ctx))
goto out;
if (rs_context_read_config(ctx, CONFIG_FILE))
goto out;
err = dispatching_client (ctx);
out:
if (ctx)
rs_context_destroy(ctx);
if (err)
{
fprintf (stderr, "error: %s: %d\n", rs_err_msg (err), rs_err_code (err, 0));
return rs_err_code (err, 1);
}
return 0;
}
|