Example client crafting simple packet using freeradius-libradius.
[radsecproxy.git] / lib / examples / client.c
1 /* RADIUS client doing blocking i/o.  */
2
3 #include <stdio.h>
4 #include <string.h>
5 #include <unistd.h>
6 #include <freeradius/libradius.h>
7 #include "../libradsec.h"
8 #include "../debug.h"
9
10 #define SECRET "sikrit"
11 #define USER_NAME "bob"
12 #define USER_PW "hemligt"
13
14 int
15 rsx_client ()
16 {
17   fr_randctx fr_ctx;
18   struct rs_handle *ctx;
19   struct rs_connection *conn;
20   RADIUS_PACKET *pkt;
21   VALUE_PAIR *vp;
22   char user_pw[MAX_STRING_LEN];
23   uint8_t reqauth[AUTH_VECTOR_LEN];
24
25   fr_log_fp = stderr;
26   fr_debug_flag = 1;
27   fr_randinit (&fr_ctx, 0);
28   fr_rand_seed (NULL, 0);
29
30   printf ("creating context\n");
31   if (rs_context_create(&ctx))
32     return -1;
33
34 #if 0
35   printf ("reading config\n");
36   if (rs_context_config_read(ctx, "libradsec.conf"))
37     return -1;
38 #endif
39
40   printf ("init dict");
41   if (dict_init("/usr/share/freeradius", "dictionary"))
42     return -1;
43
44 #if 0
45   printf ("creating connection\n");
46   if (rs_conn_create(ctx, &conn))
47     return -1;
48 #endif
49
50   printf ("creating a packet\n");
51   pkt = rad_alloc (1);
52   if (!pkt) {
53     fr_perror ("pairmake");
54     return -1;
55   }
56
57   {
58     size_t pwlen =  sizeof(USER_PW);
59     strncpy (user_pw, USER_PW, sizeof(user_pw));
60     rad_pwencode(user_pw, &pwlen, SECRET, reqauth);
61   }
62
63   printf ("creating value pairs\n");
64   vp = pairmake ("User-Name", USER_NAME, 0);
65   if (!vp) {
66     fr_perror ("paircreate");
67     return -1;
68   }
69   pairadd (&vp, pairmake ("User-Password", user_pw, 0));
70   pkt->vps = vp;
71
72   printf ("attributes:\n");
73   vp_printlist (stdout, vp);
74
75   printf ("encoding packet\n");
76   rad_encode (pkt, NULL, SECRET);
77   print_hex (pkt);              /* DEBUG */
78
79 #if 0
80   rs_packet_create (&pkt, RS_ACCESS_REQUEST);
81   rs_attrib_create (&attr, RS_...);
82   rs_packet_add_attrib (pkt, attr);
83 #endif
84
85   //rs_packet_send (conn, pkt, ...);
86
87   rad_free(&pkt);
88
89 #if 0
90   printf ("destroying connection\n");
91   if (rs_conn_destroy(conn))
92     return -1;
93 #endif
94
95   printf ("destroying context\n");
96   rs_context_destroy(ctx);
97
98   return 0;
99 }
100
101 int
102 main (int argc, char *argv[])
103 {
104   exit (rsx_client ());
105 }