aboutsummaryrefslogtreecommitdiffstats
path: root/radius_example
diff options
context:
space:
mode:
authorJouni Malinen <j@w1.fi>2008-02-28 01:34:43 (GMT)
committerJouni Malinen <jm@jm.kir.nu>2008-02-28 01:34:43 (GMT)
commit6fc6879bd55a394f807cbbe927df736c190cb8ab (patch)
treecdf50da0c58f21510a808d53502a060d911ff243 /radius_example
downloadhostap-6fc6879bd55a394f807cbbe927df736c190cb8ab.zip
hostap-6fc6879bd55a394f807cbbe927df736c190cb8ab.tar.gz
hostap-6fc6879bd55a394f807cbbe927df736c190cb8ab.tar.bz2
Re-initialize hostapd/wpa_supplicant git repository based on 0.6.3 release
Diffstat (limited to 'radius_example')
-rw-r--r--radius_example/.gitignore2
-rw-r--r--radius_example/Makefile47
-rw-r--r--radius_example/README39
-rw-r--r--radius_example/radius_example.c161
4 files changed, 249 insertions, 0 deletions
diff --git a/radius_example/.gitignore b/radius_example/.gitignore
new file mode 100644
index 0000000..c43e0fa
--- /dev/null
+++ b/radius_example/.gitignore
@@ -0,0 +1,2 @@
+*.d
+radius_example
diff --git a/radius_example/Makefile b/radius_example/Makefile
new file mode 100644
index 0000000..5bdf8dd
--- /dev/null
+++ b/radius_example/Makefile
@@ -0,0 +1,47 @@
+ALL=radius_example
+
+all: $(ALL)
+
+ifndef CC
+CC=gcc
+endif
+
+ifndef CFLAGS
+CFLAGS = -MMD -O2 -Wall -g
+endif
+
+CFLAGS += -I.
+CFLAGS += -I../src
+CFLAGS += -I../src/crypto
+CFLAGS += -I../src/utils
+
+OBJS += ../src/utils/common.o
+OBJS += ../src/utils/os_unix.o
+OBJS += ../src/utils/wpa_debug.o
+OBJS += ../src/utils/eloop.o
+OBJS += ../src/utils/ip_addr.o
+OBJS += ../src/crypto/md5.o
+CFLAGS += -DINTERNAL_MD5
+
+OBJS += ../src/radius/radius.o
+OBJS += ../src/radius/radius_client.o
+
+ifndef LDO
+LDO=$(CC)
+endif
+
+
+OBJS_ex = radius_example.o
+
+libradius.a: $(OBJS)
+ ar rc libradius.a $(OBJS)
+ ranlib libradius.a
+
+radius_example: $(OBJS_ex) libradius.a
+ $(LDO) $(LDFLAGS) -o radius_example $(OBJS_ex) -L. -lradius $(LIBS)
+
+clean:
+ $(MAKE) -C ../src clean
+ rm -f core *~ *.o *.d libradius.a $(ALL)
+
+-include $(OBJS:%.o=%.d)
diff --git a/radius_example/README b/radius_example/README
new file mode 100644
index 0000000..7669fa3
--- /dev/null
+++ b/radius_example/README
@@ -0,0 +1,39 @@
+Example application using RADIUS client as a library
+Copyright (c) 2007, Jouni Malinen <j@w1.fi>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License version 2 as
+published by the Free Software Foundation.
+
+Alternatively, this software may be distributed under the terms of BSD
+license.
+
+
+This directory contains an example showing how the RADIUS client
+functionality from hostapd can be used as a library in another
+program. The example program initializes the RADIUS client and send a
+Access-Request using User-Name and User-Password attributes. A reply
+from the RADIUS authentication server will be processed and it is used
+as a trigger to terminate the example program.
+
+The RADIUS library links in couple of helper functions from src/utils and
+src/crypto directories. Most of these are suitable as-is, but it may
+be desirable to replace the debug output code in src/utils/wpa_debug.c
+by dropping this file from the library and re-implementing the
+functions there in a way that better fits in with the main
+application.
+
+RADIUS client implementation takes care of receiving messages,
+timeouts, and retransmissions of packets. Consequently, it requires
+functionality for registering timeouts and received packet
+notifications. This is implemented using the generic event loop
+implementation (see src/utils/eloop.h).
+
+The main application may either use the included event loop
+implementation or alternatively, implement eloop_* wrapper functions
+to use whatever event loop design is used in the main program. This
+would involve removing src/utils/eloop.o from the library and
+implementing following functions defines in src/utils/eloop.h:
+eloop_register_timeout(), eloop_cancel_timeout(),
+eloop_register_read_sock(), eloop_unregister_read_sock(), and
+eloop_terminated().
diff --git a/radius_example/radius_example.c b/radius_example/radius_example.c
new file mode 100644
index 0000000..1b27efc
--- /dev/null
+++ b/radius_example/radius_example.c
@@ -0,0 +1,161 @@
+/*
+ * Example application using RADIUS client as a library
+ * Copyright (c) 2007, Jouni Malinen <j@w1.fi>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * Alternatively, this software may be distributed under the terms of BSD
+ * license.
+ *
+ * See README and COPYING for more details.
+ */
+
+#include "includes.h"
+
+#include "common.h"
+#include "eloop.h"
+#include "radius/radius.h"
+#include "radius/radius_client.h"
+
+extern int wpa_debug_level;
+
+struct radius_ctx {
+ struct radius_client_data *radius;
+ struct hostapd_radius_servers conf;
+ u8 radius_identifier;
+ struct in_addr own_ip_addr;
+};
+
+
+static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module,
+ int level, const char *txt, size_t len)
+{
+ printf("%s\n", txt);
+}
+
+
+/* Process the RADIUS frames from Authentication Server */
+static RadiusRxResult receive_auth(struct radius_msg *msg,
+ struct radius_msg *req,
+ u8 *shared_secret, size_t shared_secret_len,
+ void *data)
+{
+ /* struct radius_ctx *ctx = data; */
+ printf("Received RADIUS Authentication message; code=%d\n",
+ msg->hdr->code);
+
+ /* We're done for this example, so request eloop to terminate. */
+ eloop_terminate();
+
+ return RADIUS_RX_PROCESSED;
+}
+
+
+static void start_example(void *eloop_ctx, void *timeout_ctx)
+{
+ struct radius_ctx *ctx = eloop_ctx;
+ struct radius_msg *msg;
+
+ printf("Sending a RADIUS authentication message\n");
+
+ ctx->radius_identifier = radius_client_get_id(ctx->radius);
+ msg = radius_msg_new(RADIUS_CODE_ACCESS_REQUEST,
+ ctx->radius_identifier);
+ if (msg == NULL) {
+ printf("Could not create net RADIUS packet\n");
+ return;
+ }
+
+ radius_msg_make_authenticator(msg, (u8 *) ctx, sizeof(*ctx));
+
+ if (!radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME,
+ (u8 *) "user", 4)) {
+ printf("Could not add User-Name\n");
+ radius_msg_free(msg);
+ os_free(msg);
+ return;
+ }
+
+ if (!radius_msg_add_attr_user_password(
+ msg, (u8 *) "password", 8,
+ ctx->conf.auth_server->shared_secret,
+ ctx->conf.auth_server->shared_secret_len)) {
+ printf("Could not add User-Password\n");
+ radius_msg_free(msg);
+ os_free(msg);
+ return;
+ }
+
+ if (!radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
+ (u8 *) &ctx->own_ip_addr, 4)) {
+ printf("Could not add NAS-IP-Address\n");
+ radius_msg_free(msg);
+ os_free(msg);
+ return;
+ }
+
+ radius_client_send(ctx->radius, msg, RADIUS_AUTH, NULL);
+}
+
+
+int main(int argc, char *argv[])
+{
+ struct radius_ctx ctx;
+ struct hostapd_radius_server *srv;
+
+ if (os_program_init())
+ return -1;
+
+ hostapd_logger_register_cb(hostapd_logger_cb);
+
+ os_memset(&ctx, 0, sizeof(ctx));
+ inet_aton("127.0.0.1", &ctx.own_ip_addr);
+
+ if (eloop_init(&ctx)) {
+ printf("Failed to initialize event loop\n");
+ return -1;
+ }
+
+ srv = os_zalloc(sizeof(*srv));
+ if (srv == NULL)
+ return -1;
+
+ srv->addr.af = AF_INET;
+ srv->port = 1812;
+ if (hostapd_parse_ip_addr("127.0.0.1", &srv->addr) < 0) {
+ printf("Failed to parse IP address\n");
+ return -1;
+ }
+ srv->shared_secret = (u8 *) os_strdup("radius");
+ srv->shared_secret_len = 6;
+
+ ctx.conf.auth_server = ctx.conf.auth_servers = srv;
+ ctx.conf.num_auth_servers = 1;
+ ctx.conf.msg_dumps = 1;
+
+ ctx.radius = radius_client_init(&ctx, &ctx.conf);
+ if (ctx.radius == NULL) {
+ printf("Failed to initialize RADIUS client\n");
+ return -1;
+ }
+
+ if (radius_client_register(ctx.radius, RADIUS_AUTH, receive_auth,
+ &ctx) < 0) {
+ printf("Failed to register RADIUS authentication handler\n");
+ return -1;
+ }
+
+ eloop_register_timeout(0, 0, start_example, &ctx, NULL);
+
+ eloop_run();
+
+ radius_client_deinit(ctx.radius);
+ os_free(srv->shared_secret);
+
+ eloop_destroy();
+ os_program_deinit();
+
+ return 0;
+}