summaryrefslogtreecommitdiff
path: root/lib/examples/client-blocking.c
blob: ef361e536a75ca8c557ce0673455d959d61c063e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/* RADIUS client doing blocking i/o.  */

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <event2/event.h>
#include <freeradius/libradius.h>
#include <radsec/radsec.h>
#include <radsec/request.h>
#include "debug.h"		/* For rs_dump_packet().  */

#define SECRET "sikrit"
#define USER_NAME "molgan"
#define USER_PW "password"

struct rs_error *
blocking_client (const char *av1, const char *av2, 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;

  if (rs_context_create (&h, "/usr/share/freeradius/dictionary"))
    return NULL;

#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
  if (rs_context_read_config (h, av1))
    goto cleanup;
  if (rs_conn_create (h, &conn, av2))
    goto cleanup;
#endif	/* 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);
  else
    fprintf (stderr, "%s: no response\n", __func__);

 cleanup:
  if (resp)
    rs_packet_destroy (resp);
  if (req)
    rs_packet_destroy (req);
  if (conn)
    rs_conn_destroy (conn);
  if (request)
    rs_request_destroy (request);
  if (h)
    rs_context_destroy (h);

  return rs_err_conn_pop (conn);
}

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++;
    }
  err = blocking_client (argv[1], argv[2], use_request_object_flag);
  if (err)
    {
      fprintf (stderr, "%s\n", rs_err_msg (err));
      return rs_err_code (err, 1);
    }
  return 0;
}