Add formal argument 'secret' to two public functions.
[radsecproxy.git] / lib / examples / client-blocking.c
index 718dc47..773a26c 100644 (file)
@@ -4,10 +4,12 @@
 #include <string.h>
 #include <unistd.h>
 #include <stdlib.h>
+#include <assert.h>
 #include <event2/event.h>
-#include <freeradius/libradius.h>
 #include <radsec/radsec.h>
+#include <radsec/radsec-impl.h>
 #include <radsec/request.h>
+#include "err.h"
 #include "debug.h"             /* For rs_dump_packet().  */
 
 #define SECRET "sikrit"
 #define USER_PW "password"
 
 struct rs_error *
-blocking_client (const char *av1, const char *av2, int use_request_object_flag)
+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;
 
-  if (rs_context_create (&h, "/usr/share/freeradius/dictionary"))
-    return NULL;
+  r = rs_context_create (&h);
+  if (r)
+    {
+      assert(r == RSE_NOMEM);
+      assert (!"out of RAM -- unable to create libradsec context");
+    }
 
 #if !defined (USE_CONFIG_FILE)
   {
@@ -42,23 +50,23 @@ blocking_client (const char *av1, const char *av2, int use_request_object_flag)
     if (rs_peer_set_secret (server, SECRET))
       goto cleanup;
   }
-#else
-  if (rs_context_read_config (h, av1))
+#else  /* defined (USE_CONFIG_FILE) */
+  if (rs_context_read_config (h, config_fn))
     goto cleanup;
-  if (rs_conn_create (h, &conn, av2))
+  if (rs_conn_create (h, &conn, configuration))
     goto cleanup;
-#endif /* USE_CONFIG_FILE */
+#endif /* defined (USE_CONFIG_FILE) */
 
   if (use_request_object_flag)
     {
-      if (rs_request_create_authn (conn, &request, USER_NAME, USER_PW))
+      if (rs_request_create_authn (conn, &request, USER_NAME, USER_PW, SECRET))
        goto cleanup;
       if (rs_request_send (request, &resp))
        goto cleanup;
     }
   else
     {
-      if (rs_packet_create_authn_request (conn, &req, USER_NAME, USER_PW))
+      if (rs_packet_create_authn_request (conn, &req, USER_NAME, USER_PW, SECRET))
        goto cleanup;
       if (rs_packet_send (req, NULL))
        goto cleanup;
@@ -69,16 +77,18 @@ blocking_client (const char *av1, const char *av2, int use_request_object_flag)
   if (resp)
     {
       rs_dump_packet (resp);
-      if (rs_packet_frpkt (resp)->code == PW_AUTHENTICATION_ACK)
+      if (rs_packet_code (resp) == PW_ACCESS_ACCEPT)
        printf ("Good auth.\n");
       else
-       printf ("Bad auth: %d\n", rs_packet_frpkt (resp)->code);
+       printf ("Bad auth: %d\n", rs_packet_code (resp));
     }
   else
     fprintf (stderr, "%s: no response\n", __func__);
 
  cleanup:
-  err = rs_err_conn_pop (conn);
+  err = rs_err_ctx_pop (h);
+  if (err == RSE_OK)
+    err = rs_err_conn_pop (conn);
   if (resp)
     rs_packet_destroy (resp);
   if (request)
@@ -91,6 +101,13 @@ blocking_client (const char *av1, const char *av2, int use_request_object_flag)
   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[])
 {
@@ -103,10 +120,12 @@ main (int argc, char *argv[])
       argc--;
       argv++;
     }
+  if (argc < 3)
+    usage (argc, argv);
   err = blocking_client (argv[1], argv[2], use_request_object_flag);
   if (err)
     {
-      fprintf (stderr, "%s\n", rs_err_msg (err));
+      fprintf (stderr, "error: %s: %d\n", rs_err_msg (err), rs_err_code (err, 0));
       return rs_err_code (err, 1);
     }
   return 0;