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