Add retransmission timer support (UDP).
[radsecproxy.git] / lib / packet.c
1 /* Copyright 2010, 2011 NORDUnet A/S. All rights reserved.
2    See the file COPYING for licensing information.  */
3
4 #if defined HAVE_CONFIG_H
5 #include <config.h>
6 #endif
7
8 #include <assert.h>
9 #include <event2/bufferevent.h>
10 #include <radsec/radsec.h>
11 #include <radsec/radsec-impl.h>
12 #include "conn.h"
13 #include "debug.h"
14 #include "packet.h"
15
16 #if defined (DEBUG)
17 #include <netdb.h>
18 #include <sys/socket.h>
19 #include <event2/buffer.h>
20 #endif
21
22 int
23 packet_verify_response (struct rs_connection *conn,
24                         struct rs_packet *response,
25                         struct rs_packet *request)
26 {
27   assert (conn);
28   assert (conn->active_peer);
29   assert (conn->active_peer->secret);
30   assert (response);
31   assert (response->rpkt);
32   assert (request);
33   assert (request->rpkt);
34
35   /* Verify header and message authenticator.  */
36   if (rad_verify (response->rpkt, request->rpkt, conn->active_peer->secret))
37     {
38       conn_close (&conn);
39       return rs_err_conn_push_fl (conn, RSE_FR, __FILE__, __LINE__,
40                                   "rad_verify: %s", fr_strerror ());
41     }
42
43   /* Decode and decrypt.  */
44   if (rad_decode (response->rpkt, request->rpkt, conn->active_peer->secret))
45     {
46       conn_close (&conn);
47       return rs_err_conn_push_fl (conn, RSE_FR, __FILE__, __LINE__,
48                                   "rad_decode: %s", fr_strerror ());
49     }
50
51   return RSE_OK;
52 }
53
54
55 /* Badly named function for preparing a RADIUS message and queue it.
56    FIXME: Rename.  */
57 int
58 packet_do_send (struct rs_packet *pkt)
59 {
60   VALUE_PAIR *vp = NULL;
61
62   assert (pkt->rpkt);
63
64   /* Add Message-Authenticator, RFC 2869.  */
65   /* FIXME: Make Message-Authenticator optional?  */
66   vp = paircreate (PW_MESSAGE_AUTHENTICATOR, PW_TYPE_OCTETS);
67   if (!vp)
68     return rs_err_conn_push_fl (pkt->conn, RSE_FR, __FILE__, __LINE__,
69                                 "paircreate: %s", fr_strerror ());
70   pairadd (&pkt->rpkt->vps, vp);
71
72   if (rad_encode (pkt->rpkt, NULL, pkt->conn->active_peer->secret))
73     return rs_err_conn_push_fl (pkt->conn, RSE_FR, __FILE__, __LINE__,
74                                 "rad_encode: %s", fr_strerror ());
75   if (rad_sign (pkt->rpkt, NULL, pkt->conn->active_peer->secret))
76     return rs_err_conn_push_fl (pkt->conn, RSE_FR, __FILE__, __LINE__,
77                                 "rad_sign: %s", fr_strerror ());
78 #if defined (DEBUG)
79   {
80     char host[80], serv[80];
81
82     getnameinfo (pkt->conn->active_peer->addr->ai_addr,
83                  pkt->conn->active_peer->addr->ai_addrlen,
84                  host, sizeof(host), serv, sizeof(serv),
85                  0 /* NI_NUMERICHOST|NI_NUMERICSERV*/);
86     rs_debug (("%s: about to send this to %s:%s:\n", __func__, host, serv));
87     rs_dump_packet (pkt);
88   }
89 #endif
90
91   if (pkt->conn->bev)           /* TCP.  */
92     {
93       int err = bufferevent_write (pkt->conn->bev, pkt->rpkt->data,
94                                    pkt->rpkt->data_len);
95       if (err < 0)
96         return rs_err_conn_push_fl (pkt->conn, RSE_EVENT, __FILE__, __LINE__,
97                                     "bufferevent_write: %s",
98                                     evutil_gai_strerror (err));
99     }
100   else                          /* UDP.  */
101     {
102       struct rs_packet **pp = &pkt->conn->out_queue;
103
104       while (*pp && (*pp)->next)
105         *pp = (*pp)->next;
106       *pp = pkt;
107
108       conn_activate_timeout (pkt->conn); /* Retransmission timer.  */
109     }
110
111   return RSE_OK;
112 }
113
114 /* Public functions.  */
115 int
116 rs_packet_create (struct rs_connection *conn, struct rs_packet **pkt_out)
117 {
118   struct rs_packet *p;
119   RADIUS_PACKET *rpkt;
120
121   *pkt_out = NULL;
122
123   rpkt = rad_alloc (1);
124   if (!rpkt)
125     return rs_err_conn_push (conn, RSE_NOMEM, __func__);
126   rpkt->id = conn->nextid++;
127
128   p = (struct rs_packet *) malloc (sizeof (struct rs_packet));
129   if (!p)
130     {
131       rad_free (&rpkt);
132       return rs_err_conn_push (conn, RSE_NOMEM, __func__);
133     }
134   memset (p, 0, sizeof (struct rs_packet));
135   p->conn = conn;
136   p->rpkt = rpkt;
137
138   *pkt_out = p;
139   return RSE_OK;
140 }
141
142 int
143 rs_packet_create_authn_request (struct rs_connection *conn,
144                                 struct rs_packet **pkt_out,
145                                 const char *user_name, const char *user_pw)
146 {
147   struct rs_packet *pkt;
148   struct rs_attr *attr;
149
150   if (rs_packet_create (conn, pkt_out))
151     return -1;
152   pkt = *pkt_out;
153   pkt->rpkt->code = PW_AUTHENTICATION_REQUEST;
154
155   if (user_name)
156     {
157       if (rs_attr_create (conn, &attr, "User-Name", user_name))
158         return -1;
159       rs_packet_add_attr (pkt, attr);
160
161       if (user_pw)
162         {
163           if (rs_attr_create (conn, &attr, "User-Password", user_pw))
164             return -1;
165           rs_packet_add_attr (pkt, attr);
166         }
167     }
168
169   return RSE_OK;
170 }
171
172 void
173 rs_packet_add_attr (struct rs_packet *pkt, struct rs_attr *attr)
174 {
175   pairadd (&pkt->rpkt->vps, attr->vp);
176   attr->pkt = pkt;
177 }
178
179 struct radius_packet *
180 rs_packet_frpkt (struct rs_packet *pkt)
181 {
182   assert (pkt);
183   return pkt->rpkt;
184 }
185
186 void
187 rs_packet_destroy (struct rs_packet *pkt)
188 {
189   if (pkt)
190     {
191       // FIXME: memory leak! TODO: free all attributes
192       rad_free (&pkt->rpkt);
193       rs_free (pkt->conn->ctx, pkt);
194     }
195 }
196