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