diff options
author | Linus Nordberg <linus@nordu.net> | 2010-09-26 00:37:40 +0200 |
---|---|---|
committer | Linus Nordberg <linus@nordu.net> | 2010-09-26 00:37:40 +0200 |
commit | 94e3f46ef6c976f6bbd670555262ec6466314d8a (patch) | |
tree | 5fe76cefc2fb868b3f2be8d1b0cd8dfa47fe16ad /lib/examples | |
parent | cd66f4d2661093d245a6ffbb35c17d5b5812e9a3 (diff) |
Example client crafting simple packet using freeradius-libradius.
Diffstat (limited to 'lib/examples')
-rw-r--r-- | lib/examples/Makefile | 3 | ||||
-rw-r--r-- | lib/examples/client.c | 105 |
2 files changed, 108 insertions, 0 deletions
diff --git a/lib/examples/Makefile b/lib/examples/Makefile index abced14..1817d90 100644 --- a/lib/examples/Makefile +++ b/lib/examples/Makefile @@ -5,5 +5,8 @@ all: blocking.o blocking.o: blocking.c blocking.h ../libradsec-base.h ../libradsec.h $(CC) $(CFLAGS) -c -I .. $^ +client: client.c ../radsec.o ../libradsec.h ../debug.h + $(CC) $(CFLAGS) -o $@ -L /usr/lib/freeradius -lfreeradius-radius $^ + clean: -rm *.o diff --git a/lib/examples/client.c b/lib/examples/client.c new file mode 100644 index 0000000..64a4436 --- /dev/null +++ b/lib/examples/client.c @@ -0,0 +1,105 @@ +/* RADIUS client doing blocking i/o. */ + +#include <stdio.h> +#include <string.h> +#include <unistd.h> +#include <freeradius/libradius.h> +#include "../libradsec.h" +#include "../debug.h" + +#define SECRET "sikrit" +#define USER_NAME "bob" +#define USER_PW "hemligt" + +int +rsx_client () +{ + fr_randctx fr_ctx; + struct rs_handle *ctx; + struct rs_connection *conn; + RADIUS_PACKET *pkt; + VALUE_PAIR *vp; + char user_pw[MAX_STRING_LEN]; + uint8_t reqauth[AUTH_VECTOR_LEN]; + + fr_log_fp = stderr; + fr_debug_flag = 1; + fr_randinit (&fr_ctx, 0); + fr_rand_seed (NULL, 0); + + printf ("creating context\n"); + if (rs_context_create(&ctx)) + return -1; + +#if 0 + printf ("reading config\n"); + if (rs_context_config_read(ctx, "libradsec.conf")) + return -1; +#endif + + printf ("init dict"); + if (dict_init("/usr/share/freeradius", "dictionary")) + return -1; + +#if 0 + printf ("creating connection\n"); + if (rs_conn_create(ctx, &conn)) + return -1; +#endif + + printf ("creating a packet\n"); + pkt = rad_alloc (1); + if (!pkt) { + fr_perror ("pairmake"); + return -1; + } + + { + size_t pwlen = sizeof(USER_PW); + strncpy (user_pw, USER_PW, sizeof(user_pw)); + rad_pwencode(user_pw, &pwlen, SECRET, reqauth); + } + + printf ("creating value pairs\n"); + vp = pairmake ("User-Name", USER_NAME, 0); + if (!vp) { + fr_perror ("paircreate"); + return -1; + } + pairadd (&vp, pairmake ("User-Password", user_pw, 0)); + pkt->vps = vp; + + printf ("attributes:\n"); + vp_printlist (stdout, vp); + + printf ("encoding packet\n"); + rad_encode (pkt, NULL, SECRET); + print_hex (pkt); /* DEBUG */ + +#if 0 + rs_packet_create (&pkt, RS_ACCESS_REQUEST); + rs_attrib_create (&attr, RS_...); + rs_packet_add_attrib (pkt, attr); +#endif + + //rs_packet_send (conn, pkt, ...); + + rad_free(&pkt); + +#if 0 + printf ("destroying connection\n"); + if (rs_conn_destroy(conn)) + return -1; +#endif + + printf ("destroying context\n"); + rs_context_destroy(ctx); + + return 0; +} + +int +main (int argc, char *argv[]) +{ + exit (rsx_client ()); +} |