1 /*
2 * Example application using RADIUS client as a library
3 * Copyright (c) 2007, Jouni Malinen <j@w1.fi>
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9 #include "includes.h"
10
11 #include "common.h"
12 #include "eloop.h"
13 #include "radius/radius.h"
14 #include "radius/radius_client.h"
15
16 struct radius_ctx {
17 struct radius_client_data *radius;
18 struct hostapd_radius_servers conf;
19 u8 radius_identifier;
20 struct in_addr own_ip_addr;
21 };
22
23
hostapd_logger_cb(void * ctx,const u8 * addr,unsigned int module,int level,const char * txt,size_t len)24 static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module,
25 int level, const char *txt, size_t len)
26 {
27 printf("%s\n", txt);
28 }
29
30
31 /* Process the RADIUS frames from Authentication Server */
receive_auth(struct radius_msg * msg,struct radius_msg * req,const u8 * shared_secret,size_t shared_secret_len,void * data)32 static RadiusRxResult receive_auth(struct radius_msg *msg,
33 struct radius_msg *req,
34 const u8 *shared_secret,
35 size_t shared_secret_len,
36 void *data)
37 {
38 /* struct radius_ctx *ctx = data; */
39 printf("Received RADIUS Authentication message; code=%d\n",
40 radius_msg_get_hdr(msg)->code);
41
42 /* We're done for this example, so request eloop to terminate. */
43 eloop_terminate();
44
45 return RADIUS_RX_PROCESSED;
46 }
47
48
start_example(void * eloop_ctx,void * timeout_ctx)49 static void start_example(void *eloop_ctx, void *timeout_ctx)
50 {
51 struct radius_ctx *ctx = eloop_ctx;
52 struct radius_msg *msg;
53
54 printf("Sending a RADIUS authentication message\n");
55
56 ctx->radius_identifier = radius_client_get_id(ctx->radius);
57 msg = radius_msg_new(RADIUS_CODE_ACCESS_REQUEST,
58 ctx->radius_identifier);
59 if (msg == NULL) {
60 printf("Could not create net RADIUS packet\n");
61 return;
62 }
63
64 radius_msg_make_authenticator(msg);
65
66 if (!radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME,
67 (u8 *) "user", 4)) {
68 printf("Could not add User-Name\n");
69 radius_msg_free(msg);
70 return;
71 }
72
73 if (!radius_msg_add_attr_user_password(
74 msg, (u8 *) "password", 8,
75 ctx->conf.auth_server->shared_secret,
76 ctx->conf.auth_server->shared_secret_len)) {
77 printf("Could not add User-Password\n");
78 radius_msg_free(msg);
79 return;
80 }
81
82 if (!radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
83 (u8 *) &ctx->own_ip_addr, 4)) {
84 printf("Could not add NAS-IP-Address\n");
85 radius_msg_free(msg);
86 return;
87 }
88
89 if (radius_client_send(ctx->radius, msg, RADIUS_AUTH, NULL) < 0)
90 radius_msg_free(msg);
91 }
92
93
main(int argc,char * argv[])94 int main(int argc, char *argv[])
95 {
96 struct radius_ctx ctx;
97 struct hostapd_radius_server *srv;
98
99 if (os_program_init())
100 return -1;
101
102 hostapd_logger_register_cb(hostapd_logger_cb);
103
104 os_memset(&ctx, 0, sizeof(ctx));
105 inet_aton("127.0.0.1", &ctx.own_ip_addr);
106
107 if (eloop_init()) {
108 printf("Failed to initialize event loop\n");
109 return -1;
110 }
111
112 srv = os_zalloc(sizeof(*srv));
113 if (srv == NULL)
114 return -1;
115
116 srv->addr.af = AF_INET;
117 srv->port = 1812;
118 if (hostapd_parse_ip_addr("127.0.0.1", &srv->addr) < 0) {
119 printf("Failed to parse IP address\n");
120 return -1;
121 }
122 srv->shared_secret = (u8 *) os_strdup("radius");
123 srv->shared_secret_len = 6;
124
125 ctx.conf.auth_server = ctx.conf.auth_servers = srv;
126 ctx.conf.num_auth_servers = 1;
127 ctx.conf.msg_dumps = 1;
128
129 ctx.radius = radius_client_init(&ctx, &ctx.conf);
130 if (ctx.radius == NULL) {
131 printf("Failed to initialize RADIUS client\n");
132 return -1;
133 }
134
135 if (radius_client_register(ctx.radius, RADIUS_AUTH, receive_auth,
136 &ctx) < 0) {
137 printf("Failed to register RADIUS authentication handler\n");
138 return -1;
139 }
140
141 eloop_register_timeout(0, 0, start_example, &ctx, NULL);
142
143 eloop_run();
144
145 radius_client_deinit(ctx.radius);
146 os_free(srv->shared_secret);
147 os_free(srv);
148
149 eloop_destroy();
150 os_program_deinit();
151
152 return 0;
153 }
154