summaryrefslogtreecommitdiff
path: root/lib/examples/client.c
blob: 64a4436bd58a2e00e0d2d6acfd8c75c266554268 (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
/* RADIUS client doing blocking i/o.  */

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <freeradius/libradius.h>
#include "../libradsec.h"
#include "../debug.h"

#define SECRET "sikrit"
#define USER_NAME "bob"
#define USER_PW "hemligt"

int
rsx_client ()
{
  fr_randctx fr_ctx;
  struct rs_handle *ctx;
  struct rs_connection *conn;
  RADIUS_PACKET *pkt;
  VALUE_PAIR *vp;
  char user_pw[MAX_STRING_LEN];
  uint8_t reqauth[AUTH_VECTOR_LEN];

  fr_log_fp = stderr;
  fr_debug_flag = 1;
  fr_randinit (&fr_ctx, 0);
  fr_rand_seed (NULL, 0);

  printf ("creating context\n");
  if (rs_context_create(&ctx))
    return -1;

#if 0
  printf ("reading config\n");
  if (rs_context_config_read(ctx, "libradsec.conf"))
    return -1;
#endif

  printf ("init dict");
  if (dict_init("/usr/share/freeradius", "dictionary"))
    return -1;

#if 0
  printf ("creating connection\n");
  if (rs_conn_create(ctx, &conn))
    return -1;
#endif

  printf ("creating a packet\n");
  pkt = rad_alloc (1);
  if (!pkt) {
    fr_perror ("pairmake");
    return -1;
  }

  {
    size_t pwlen =  sizeof(USER_PW);
    strncpy (user_pw, USER_PW, sizeof(user_pw));
    rad_pwencode(user_pw, &pwlen, SECRET, reqauth);
  }

  printf ("creating value pairs\n");
  vp = pairmake ("User-Name", USER_NAME, 0);
  if (!vp) {
    fr_perror ("paircreate");
    return -1;
  }
  pairadd (&vp, pairmake ("User-Password", user_pw, 0));
  pkt->vps = vp;

  printf ("attributes:\n");
  vp_printlist (stdout, vp);

  printf ("encoding packet\n");
  rad_encode (pkt, NULL, SECRET);
  print_hex (pkt);		/* DEBUG */

#if 0
  rs_packet_create (&pkt, RS_ACCESS_REQUEST);
  rs_attrib_create (&attr, RS_...);
  rs_packet_add_attrib (pkt, attr);
#endif

  //rs_packet_send (conn, pkt, ...);

  rad_free(&pkt);

#if 0
  printf ("destroying connection\n");
  if (rs_conn_destroy(conn))
    return -1;
#endif

  printf ("destroying context\n");
  rs_context_destroy(ctx);

  return 0;
}

int
main (int argc, char *argv[])
{
  exit (rsx_client ());
}