cce00bf6457afffdef783b3cb70dfe1fa5a88bed
[radsecproxy.git] / lib / examples / client-blocking.c
1 /* RADIUS/RadSec client using libradsec in blocking mode. */
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <assert.h>
6 #include <radsec/radsec.h>
7 #include <radsec/request.h>
8 #include "err.h"
9 #include "debug.h"              /* For rs_dump_packet().  */
10
11 #define SECRET "sikrit"
12 #define USER_NAME "molgan@PROJECT-MOONSHOT.ORG"
13 #define USER_PW "password"
14
15 struct rs_error *
16 blocking_client (const char *config_fn, const char *configuration,
17                  int use_request_object_flag)
18 {
19   struct rs_context *h = NULL;
20   struct rs_connection *conn = NULL;
21   struct rs_request *request = NULL;
22   struct rs_packet *req = NULL, *resp = NULL;
23   struct rs_error *err = NULL;
24   int r;
25
26   r = rs_context_create (&h);
27   if (r)
28     {
29       assert(r == RSE_NOMEM);
30       assert (!"out of RAM -- unable to create libradsec context");
31     }
32
33 #if !defined (USE_CONFIG_FILE)
34   {
35     struct rs_peer *server;
36
37     if (rs_conn_create (h, &conn, NULL))
38       goto cleanup;
39     rs_conn_set_type (conn, RS_CONN_TYPE_UDP);
40     if (rs_peer_create (conn, &server))
41       goto cleanup;
42     if (rs_peer_set_address (server, av1, av2))
43       goto cleanup;
44     rs_peer_set_timeout (server, 1);
45     rs_peer_set_retries (server, 3);
46     if (rs_peer_set_secret (server, SECRET))
47       goto cleanup;
48   }
49 #else  /* defined (USE_CONFIG_FILE) */
50   if (rs_context_read_config (h, config_fn))
51     goto cleanup;
52   if (rs_conn_create (h, &conn, configuration))
53     goto cleanup;
54 #endif  /* defined (USE_CONFIG_FILE) */
55
56   if (use_request_object_flag)
57     {
58       if (rs_request_create_authn (conn, &request, USER_NAME, USER_PW))
59         goto cleanup;
60       if (rs_request_send (request, &resp))
61         goto cleanup;
62     }
63   else
64     {
65       if (rs_packet_create_authn_request (conn, &req, USER_NAME, USER_PW))
66         goto cleanup;
67       if (rs_packet_send (req, NULL))
68         goto cleanup;
69       if (rs_conn_receive_packet (conn, req, &resp))
70         goto cleanup;
71     }
72
73   if (resp)
74     {
75       rs_dump_packet (resp);
76       if (rs_packet_code (resp) == PW_ACCESS_ACCEPT)
77         printf ("Good auth.\n");
78       else
79         printf ("Bad auth: %d\n", rs_packet_code (resp));
80     }
81   else
82     fprintf (stderr, "%s: no response\n", __func__);
83
84  cleanup:
85   err = rs_err_ctx_pop (h);
86   if (err == RSE_OK)
87     err = rs_err_conn_pop (conn);
88   if (resp)
89     rs_packet_destroy (resp);
90   if (request)
91     rs_request_destroy (request);
92   if (conn)
93     rs_conn_destroy (conn);
94   if (h)
95     rs_context_destroy (h);
96
97   return err;
98 }
99
100 void
101 usage (int argc, char *argv[])
102 {
103   fprintf (stderr, "usage: %s: [-r] config-file config-name\n", argv[0]);
104   exit (1);
105 }
106
107 int
108 main (int argc, char *argv[])
109 {
110   int use_request_object_flag = 0;
111   struct rs_error *err;
112
113   if (argc > 1 && argv[1] && argv[1][0] == '-' && argv[1][1] == 'r')
114     {
115       use_request_object_flag = 1;
116       argc--;
117       argv++;
118     }
119   if (argc < 3)
120     usage (argc, argv);
121   err = blocking_client (argv[1], argv[2], use_request_object_flag);
122   if (err)
123     {
124       fprintf (stderr, "error: %s: %d\n", rs_err_msg (err), rs_err_code (err, 0));
125       return rs_err_code (err, 1);
126     }
127   return 0;
128 }