Move verification of response packets up to a level where it makes sense.
[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)
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
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
109   return RSE_OK;
110 }
111
112 /* Public functions.  */
113 int
114 rs_packet_create (struct rs_connection *conn, struct rs_packet **pkt_out)
115 {
116   struct rs_packet *p;
117   RADIUS_PACKET *rpkt;
118
119   *pkt_out = NULL;
120
121   rpkt = rad_alloc (1);
122   if (!rpkt)
123     return rs_err_conn_push (conn, RSE_NOMEM, __func__);
124   rpkt->id = conn->nextid++;
125
126   p = (struct rs_packet *) malloc (sizeof (struct rs_packet));
127   if (!p)
128     {
129       rad_free (&rpkt);
130       return rs_err_conn_push (conn, RSE_NOMEM, __func__);
131     }
132   memset (p, 0, sizeof (struct rs_packet));
133   p->conn = conn;
134   p->rpkt = rpkt;
135
136   *pkt_out = p;
137   return RSE_OK;
138 }
139
140 int
141 rs_packet_create_authn_request (struct rs_connection *conn,
142                                 struct rs_packet **pkt_out,
143                                 const char *user_name, const char *user_pw)
144 {
145   struct rs_packet *pkt;
146   struct rs_attr *attr;
147
148   if (rs_packet_create (conn, pkt_out))
149     return -1;
150   pkt = *pkt_out;
151   pkt->rpkt->code = PW_AUTHENTICATION_REQUEST;
152
153   if (user_name)
154     {
155       if (rs_attr_create (conn, &attr, "User-Name", user_name))
156         return -1;
157       rs_packet_add_attr (pkt, attr);
158
159       if (user_pw)
160         {
161           if (rs_attr_create (conn, &attr, "User-Password", user_pw))
162             return -1;
163           rs_packet_add_attr (pkt, attr);
164         }
165     }
166
167   return RSE_OK;
168 }
169
170 void
171 rs_packet_add_attr (struct rs_packet *pkt, struct rs_attr *attr)
172 {
173   pairadd (&pkt->rpkt->vps, attr->vp);
174   attr->pkt = pkt;
175 }
176
177 struct radius_packet *
178 rs_packet_frpkt (struct rs_packet *pkt)
179 {
180   assert (pkt);
181   return pkt->rpkt;
182 }
183
184 void
185 rs_packet_destroy (struct rs_packet *pkt)
186 {
187   if (pkt)
188     {
189       // FIXME: memory leak! TODO: free all attributes
190       rad_free (&pkt->rpkt);
191       rs_free (pkt->conn->ctx, pkt);
192     }
193 }
194