33fc671dacfa3885648883dcd5eea5e93799bf79
[radsecproxy.git] / radius / examples / example_3.c
1 /*
2 Copyright (c) 2011, Network RADIUS SARL
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7     * Redistributions of source code must retain the above copyright
8       notice, this list of conditions and the following disclaimer.
9     * Redistributions in binary form must reproduce the above copyright
10       notice, this list of conditions and the following disclaimer in the
11       documentation and/or other materials provided with the distribution.
12     * Neither the name of the <organization> nor the
13       names of its contributors may be used to endorse or promote products
14       derived from this software without specific prior written permission.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #include <networkradius-devel/client.h>
29
30 /** \file example_3.c
31  *  \brief Sample code to initialize a RADIUS packet and a response to it.
32  *
33  *  This example initializes a packet, and then adds User-Name and
34  *  User-Password to it.  The resulting packet is then printed to the
35  *  standard output.
36  *
37  *  As a next step, it then creates the response, and prints that,
38  *  too.
39  */
40
41 static const char *secret = "testing123";
42 static uint8_t request_buffer[RS_MAX_PACKET_LEN];
43 static uint8_t response_buffer[RS_MAX_PACKET_LEN];
44 static RADIUS_PACKET request, response;
45
46 int main(int argc, const char *argv[])
47 {
48         int rcode;
49         const char *user = "bob";
50         const char *password = "password";
51
52         rcode = nr_packet_init(&request, NULL, secret, PW_ACCESS_REQUEST,
53                                request_buffer, sizeof(request_buffer));
54         if (rcode < 0) {
55         error:
56                 fprintf(stderr, "Error :%s\n",  nr_strerror(rcode));
57                 return 1;
58         }
59
60         if (argc > 1) user = argv[1];
61         if (argc > 2) password = argv[2];
62
63         rcode = nr_packet_attr_append(&request, NULL,
64                                       RS_DA_USER_NAME,
65                                       user, 0);
66         if (rcode < 0) goto error;
67         
68         rcode = nr_packet_attr_append(&request, NULL,
69                                       RS_DA_USER_PASSWORD,
70                                       password, 0);
71         if (rcode < 0) goto error;
72
73         /*
74          *      ALWAYS call nr_packet_sign() before sending the packet
75          *      to anyone else!
76          */
77         rcode = nr_packet_sign(&request, NULL);
78         if (rcode < 0) goto error;
79
80         nr_packet_print_hex(&request);
81
82         rcode = nr_packet_init(&response, &request, secret, PW_ACCESS_ACCEPT,
83                                response_buffer, sizeof(response_buffer));
84         if (rcode < 0) goto error;
85
86         rcode = nr_packet_attr_append(&response, &request,
87                                       RS_DA_REPLY_MESSAGE,
88                                       "Success!", 0);
89         if (rcode < 0) goto error;
90
91         rcode = nr_packet_attr_append(&response, &request,
92                                       RS_DA_TUNNEL_PASSWORD,
93                                       password, 0);
94         if (rcode < 0) goto error;
95         rcode = nr_packet_sign(&response, &request);
96         if (rcode < 0) goto error;
97
98         nr_packet_print_hex(&response);
99
100         /*
101          *      Check that the response is well-formed.  The
102          *      nr_packet_verify() function also calls nr_packet_ok().
103          *      However, it is sometimes useful to separate "malformed
104          *      packet" errors from "packet is not a response to a
105          *      reqeust" errors.
106          */
107         rcode = nr_packet_ok(&response);
108         if (rcode < 0) goto error;
109
110         /*
111          *      Double-check the signature of the response.
112          */
113         rcode = nr_packet_verify(&response, &request);
114         if (rcode < 0) goto error;
115
116         rcode = nr_packet_decode(&response, &request);
117         if (rcode < 0) goto error;
118
119         nr_vp_fprintf_list(stdout, response.vps);
120         nr_vp_free(&response.vps);
121
122         return 0;
123 }