summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorLinus Nordberg <linus@nordberg.se>2014-02-05 11:10:02 +0100
committerLinus Nordberg <linus@nordberg.se>2014-02-05 11:10:02 +0100
commit3d954bfd2f658ac05a0f20a1241738ed3e3fdd28 (patch)
treed95b364fbab298c9b94c9c729afc98904c7c5bb0 /examples
parent67bdfa83f1879312fef0fbac769f6fb45df12d1a (diff)
Move lib to the root.
Diffstat (limited to 'examples')
-rw-r--r--examples/Makefile.am8
-rw-r--r--examples/blocking.c71
-rw-r--r--examples/blocking.h4
-rw-r--r--examples/client-blocking.c127
-rw-r--r--examples/client-psk.conf18
-rw-r--r--examples/client.conf24
6 files changed, 252 insertions, 0 deletions
diff --git a/examples/Makefile.am b/examples/Makefile.am
new file mode 100644
index 0000000..fa1c835
--- /dev/null
+++ b/examples/Makefile.am
@@ -0,0 +1,8 @@
+AUTOMAKE_OPTIONS = foreign
+AM_CPPFLAGS = -I$(top_srcdir)/include -I$(top_srcdir)
+AM_CFLAGS = -Wall -Werror -g
+
+noinst_PROGRAMS = client
+client_SOURCES = client-blocking.c
+client_LDADD = ../libradsec.la #-lefence
+client_CFLAGS = $(AM_CFLAGS) -DUSE_CONFIG_FILE
diff --git a/examples/blocking.c b/examples/blocking.c
new file mode 100644
index 0000000..b66eb64
--- /dev/null
+++ b/examples/blocking.c
@@ -0,0 +1,71 @@
+/* Example usage of libradsec-base, using blocking i/o. */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <stdint.h>
+#include "blocking.h"
+
+struct rs_packet *
+next_packet (const struct rs_handle *ctx, int fd)
+{
+ uint8_t hdr[RS_HEADER_LEN];
+ uint8_t *buf;
+ size_t len;
+ struct rs_packet *p;
+ ssize_t n;
+
+ /* Read fixed length header. */
+ n = 0;
+ while (n < RS_HEADER_LEN)
+ n += read (fd, hdr, RS_HEADER_LEN - n);
+
+ p = rs_packet_new (ctx, hdr, &len);
+ fprintf (stderr, "DEBUG: got header, total packet len is %d\n",
+ len + RS_HEADER_LEN);
+
+ /* Read the rest of the message. */
+ if (p)
+ {
+ buf = malloc (len);
+ if (buf)
+ {
+ n = 0;
+ while (n < len)
+ n += read (fd, buf, len - n);
+ p = rs_packet_parse (ctx, &p, buf, len);
+ free (buf);
+ }
+ else
+ rs_packet_free (ctx, &p);
+ }
+
+ return p;
+}
+
+int
+send_packet(const struct rs_handle *ctx, int fd, struct rs_packet *p)
+{
+ uint8_t *buf = NULL;
+ ssize_t n = -20; /* Arbitrary packet size -- a guess. */
+
+ while (n < 0)
+ {
+ buf = realloc (buf, -n);
+ if (buf == NULL)
+ return -1;
+ n = rs_packet_serialize (p, buf, -n);
+ }
+
+ while (n)
+ {
+ ssize_t count = write (fd, buf, n);
+ if (count == -1)
+ return -1;
+ n -= count;
+ }
+
+ free (buf);
+ rs_packet_free (ctx, &p);
+ return 0;
+}
diff --git a/examples/blocking.h b/examples/blocking.h
new file mode 100644
index 0000000..f91e6be
--- /dev/null
+++ b/examples/blocking.h
@@ -0,0 +1,4 @@
+#include "libradsec-base.h"
+
+struct rs_packet *next_packet (const struct rs_handle *ctx, int fd);
+int send_packet (const struct rs_handle *ctx, int fd, struct rs_packet *p);
diff --git a/examples/client-blocking.c b/examples/client-blocking.c
new file mode 100644
index 0000000..a50ee8a
--- /dev/null
+++ b/examples/client-blocking.c
@@ -0,0 +1,127 @@
+/* RADIUS/RadSec client using libradsec in blocking mode. */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <assert.h>
+#include <radsec/radsec.h>
+#include <radsec/request.h>
+#include "err.h"
+#include "debug.h" /* For rs_dump_packet(). */
+
+#define SECRET "sikrit"
+#define USER_NAME "molgan@PROJECT-MOONSHOT.ORG"
+#define USER_PW "password"
+
+struct rs_error *
+blocking_client (const char *config_fn, const char *configuration,
+ int use_request_object_flag)
+{
+ struct rs_context *h = NULL;
+ struct rs_connection *conn = NULL;
+ struct rs_request *request = NULL;
+ struct rs_packet *req = NULL, *resp = NULL;
+ struct rs_error *err = NULL;
+ int r;
+
+ r = rs_context_create (&h);
+ if (r)
+ {
+ assert (!"unable to create libradsec context");
+ }
+
+#if !defined (USE_CONFIG_FILE)
+ {
+ struct rs_peer *server;
+
+ if (rs_conn_create (h, &conn, NULL))
+ goto cleanup;
+ rs_conn_set_type (conn, RS_CONN_TYPE_UDP);
+ if (rs_peer_create (conn, &server))
+ goto cleanup;
+ if (rs_peer_set_address (server, av1, av2))
+ goto cleanup;
+ rs_peer_set_timeout (server, 1);
+ rs_peer_set_retries (server, 3);
+ if (rs_peer_set_secret (server, SECRET))
+ goto cleanup;
+ }
+#else /* defined (USE_CONFIG_FILE) */
+ if (rs_context_read_config (h, config_fn))
+ goto cleanup;
+ if (rs_conn_create (h, &conn, configuration))
+ goto cleanup;
+#endif /* defined (USE_CONFIG_FILE) */
+
+ if (use_request_object_flag)
+ {
+ if (rs_request_create_authn (conn, &request, USER_NAME, USER_PW))
+ goto cleanup;
+ if (rs_request_send (request, &resp))
+ goto cleanup;
+ }
+ else
+ {
+ if (rs_packet_create_authn_request (conn, &req, USER_NAME, USER_PW))
+ goto cleanup;
+ if (rs_packet_send (req, NULL))
+ goto cleanup;
+ if (rs_conn_receive_packet (conn, req, &resp))
+ goto cleanup;
+ }
+
+ if (resp)
+ {
+ rs_dump_packet (resp);
+ if (rs_packet_code (resp) == PW_ACCESS_ACCEPT)
+ printf ("Good auth.\n");
+ else
+ printf ("Bad auth: %d\n", rs_packet_code (resp));
+ }
+ else
+ fprintf (stderr, "%s: no response\n", __func__);
+
+ cleanup:
+ err = rs_err_ctx_pop (h);
+ if (err == RSE_OK)
+ err = rs_err_conn_pop (conn);
+ if (resp)
+ rs_packet_destroy (resp);
+ if (request)
+ rs_request_destroy (request);
+ if (conn)
+ rs_conn_destroy (conn);
+ if (h)
+ rs_context_destroy (h);
+
+ return err;
+}
+
+void
+usage (int argc, char *argv[])
+{
+ fprintf (stderr, "usage: %s: [-r] config-file config-name\n", argv[0]);
+ exit (1);
+}
+
+int
+main (int argc, char *argv[])
+{
+ int use_request_object_flag = 0;
+ struct rs_error *err;
+
+ if (argc > 1 && argv[1] && argv[1][0] == '-' && argv[1][1] == 'r')
+ {
+ use_request_object_flag = 1;
+ argc--;
+ argv++;
+ }
+ if (argc < 3)
+ usage (argc, argv);
+ err = blocking_client (argv[1], argv[2], use_request_object_flag);
+ if (err)
+ {
+ fprintf (stderr, "error: %s: %d\n", rs_err_msg (err), rs_err_code (err, 0));
+ return rs_err_code (err, 1);
+ }
+ return 0;
+}
diff --git a/examples/client-psk.conf b/examples/client-psk.conf
new file mode 100644
index 0000000..7b35e23
--- /dev/null
+++ b/examples/client-psk.conf
@@ -0,0 +1,18 @@
+# We keep PSK configurations in a separate config file until
+# --enable-tls-psk is on by default. This configuration is not valid
+# without PSK support.
+
+realm blocking-tls-psk {
+ type = "TLS"
+ timeout = 1
+ retries = 3
+ #pskstr = "sikrit psk"
+ pskhexstr = "deadbeef4711"
+ pskid = "Client_identity"
+ pskex = "PSK"
+ server {
+ hostname = "srv1"
+ service = "4433"
+ secret = "sikrit"
+ }
+}
diff --git a/examples/client.conf b/examples/client.conf
new file mode 100644
index 0000000..b0b4536
--- /dev/null
+++ b/examples/client.conf
@@ -0,0 +1,24 @@
+realm blocking-udp {
+ type = "UDP"
+ timeout = 2
+ retries = 2
+ server {
+ hostname = "127.0.0.1"
+ service = "1820"
+ secret = "sikrit"
+ }
+}
+
+realm blocking-tls {
+ type = "TLS"
+ timeout = 1
+ retries = 3
+ cacertfile = "tests/demoCA/newcerts/01.pem"
+ certfile = "tests/demoCA/newcerts/03.pem"
+ certkeyfile = "tests/demoCA/private/cli1.key"
+ server {
+ hostname = "srv1"
+ service = "2083"
+ secret = "sikrit"
+ }
+}