Enable tls psk
[libradsec.git] / 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 (!"unable to create libradsec context");
30     }
31
32 #if !defined (USE_CONFIG_FILE)
33   {
34     struct rs_peer *server;
35
36     if (rs_conn_create (h, &conn, NULL))
37       goto cleanup;
38     rs_conn_set_type (conn, RS_CONN_TYPE_UDP);
39     if (rs_peer_create (conn, &server))
40       goto cleanup;
41     if (rs_peer_set_address (server, av1, av2))
42       goto cleanup;
43     rs_peer_set_timeout (server, 1);
44     rs_peer_set_retries (server, 3);
45     if (rs_peer_set_secret (server, SECRET))
46       goto cleanup;
47   }
48 #else  /* defined (USE_CONFIG_FILE) */
49   if (rs_context_read_config (h, config_fn))
50     goto cleanup;
51   if (rs_conn_create (h, &conn, configuration))
52     goto cleanup;
53 #endif  /* defined (USE_CONFIG_FILE) */
54
55   if (use_request_object_flag)
56     {
57       if (rs_request_create_authn (conn, &request, USER_NAME, USER_PW))
58         goto cleanup;
59       if (rs_request_send (request, &resp))
60         goto cleanup;
61     }
62   else
63     {
64       if (rs_packet_create_authn_request (conn, &req, USER_NAME, USER_PW))
65         goto cleanup;
66       if (rs_packet_send (req, NULL))
67         goto cleanup;
68       if (rs_conn_receive_packet (conn, req, &resp))
69         goto cleanup;
70     }
71
72   if (resp)
73     {
74       rs_dump_packet (resp);
75       if (rs_packet_code (resp) == PW_ACCESS_ACCEPT)
76         printf ("Good auth.\n");
77       else
78         printf ("Bad auth: %d\n", rs_packet_code (resp));
79     }
80   else
81     fprintf (stderr, "%s: no response\n", __func__);
82
83  cleanup:
84   err = rs_err_ctx_pop (h);
85   if (err == RSE_OK)
86     err = rs_err_conn_pop (conn);
87   if (resp)
88     rs_packet_destroy (resp);
89   if (request)
90     rs_request_destroy (request);
91   if (conn)
92     rs_conn_destroy (conn);
93   if (h)
94     rs_context_destroy (h);
95
96   return err;
97 }
98
99 void
100 usage (int argc, char *argv[])
101 {
102   fprintf (stderr, "usage: %s: [-r] config-file config-name\n", argv[0]);
103   exit (1);
104 }
105
106 int
107 main (int argc, char *argv[])
108 {
109   int use_request_object_flag = 0;
110   struct rs_error *err;
111
112   if (argc > 1 && argv[1] && argv[1][0] == '-' && argv[1][1] == 'r')
113     {
114       use_request_object_flag = 1;
115       argc--;
116       argv++;
117     }
118   if (argc < 3)
119     usage (argc, argv);
120   err = blocking_client (argv[1], argv[2], use_request_object_flag);
121   if (err)
122     {
123       fprintf (stderr, "error: %s: %d\n", rs_err_msg (err), rs_err_code (err, 0));
124       return rs_err_code (err, 1);
125     }
126   return 0;
127 }