Don't add Message-Authenticator more than once.
[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);
63   assert (pkt->conn);
64   assert (pkt->conn->active_peer);
65   assert (pkt->conn->active_peer->secret);
66   assert (pkt->rpkt);
67
68   /* Add a Message-Authenticator, RFC 2869, if not already present.  */
69   /* FIXME: Make Message-Authenticator optional?  */
70   vp = paircreate (PW_MESSAGE_AUTHENTICATOR, PW_TYPE_OCTETS);
71   if (!vp)
72     return rs_err_conn_push_fl (pkt->conn, RSE_FR, __FILE__, __LINE__,
73                                 "paircreate: %s", fr_strerror ());
74   pairreplace (&pkt->rpkt->vps, vp);
75
76   /* Encode message.  */
77   if (rad_encode (pkt->rpkt, NULL, pkt->conn->active_peer->secret))
78     return rs_err_conn_push_fl (pkt->conn, RSE_FR, __FILE__, __LINE__,
79                                 "rad_encode: %s", fr_strerror ());
80   /* Sign message.  */
81   if (rad_sign (pkt->rpkt, NULL, pkt->conn->active_peer->secret))
82     return rs_err_conn_push_fl (pkt->conn, RSE_FR, __FILE__, __LINE__,
83                                 "rad_sign: %s", fr_strerror ());
84 #if defined (DEBUG)
85   {
86     char host[80], serv[80];
87
88     getnameinfo (pkt->conn->active_peer->addr->ai_addr,
89                  pkt->conn->active_peer->addr->ai_addrlen,
90                  host, sizeof(host), serv, sizeof(serv),
91                  0 /* NI_NUMERICHOST|NI_NUMERICSERV*/);
92     rs_debug (("%s: about to send this to %s:%s:\n", __func__, host, serv));
93     rs_dump_packet (pkt);
94   }
95 #endif
96
97   /* Put message in output buffer.  */
98   if (pkt->conn->bev)           /* TCP.  */
99     {
100       int err = bufferevent_write (pkt->conn->bev, pkt->rpkt->data,
101                                    pkt->rpkt->data_len);
102       if (err < 0)
103         return rs_err_conn_push_fl (pkt->conn, RSE_EVENT, __FILE__, __LINE__,
104                                     "bufferevent_write: %s",
105                                     evutil_gai_strerror (err));
106     }
107   else                          /* UDP.  */
108     {
109       struct rs_packet **pp = &pkt->conn->out_queue;
110
111       while (*pp && (*pp)->next)
112         *pp = (*pp)->next;
113       *pp = pkt;
114
115       conn_activate_timeout (pkt->conn); /* Retransmission timer.  */
116     }
117
118   return RSE_OK;
119 }
120
121 /* Public functions.  */
122 int
123 rs_packet_create (struct rs_connection *conn, struct rs_packet **pkt_out)
124 {
125   struct rs_packet *p;
126   RADIUS_PACKET *rpkt;
127
128   *pkt_out = NULL;
129
130   rpkt = rad_alloc (1);
131   if (!rpkt)
132     return rs_err_conn_push (conn, RSE_NOMEM, __func__);
133   rpkt->id = conn->nextid++;
134
135   p = (struct rs_packet *) malloc (sizeof (struct rs_packet));
136   if (!p)
137     {
138       rad_free (&rpkt);
139       return rs_err_conn_push (conn, RSE_NOMEM, __func__);
140     }
141   memset (p, 0, sizeof (struct rs_packet));
142   p->conn = conn;
143   p->rpkt = rpkt;
144
145   *pkt_out = p;
146   return RSE_OK;
147 }
148
149 int
150 rs_packet_create_authn_request (struct rs_connection *conn,
151                                 struct rs_packet **pkt_out,
152                                 const char *user_name, const char *user_pw)
153 {
154   struct rs_packet *pkt;
155   struct rs_attr *attr;
156
157   if (rs_packet_create (conn, pkt_out))
158     return -1;
159   pkt = *pkt_out;
160   pkt->rpkt->code = PW_AUTHENTICATION_REQUEST;
161
162   if (user_name)
163     {
164       if (rs_attr_create (conn, &attr, "User-Name", user_name))
165         return -1;
166       rs_packet_add_attr (pkt, attr);
167
168       if (user_pw)
169         {
170           if (rs_attr_create (conn, &attr, "User-Password", user_pw))
171             return -1;
172           rs_packet_add_attr (pkt, attr);
173         }
174     }
175
176   return RSE_OK;
177 }
178
179 void
180 rs_packet_add_attr (struct rs_packet *pkt, struct rs_attr *attr)
181 {
182   pairadd (&pkt->rpkt->vps, attr->vp);
183   attr->pkt = pkt;
184 }
185
186 struct radius_packet *
187 rs_packet_frpkt (struct rs_packet *pkt)
188 {
189   assert (pkt);
190   return pkt->rpkt;
191 }
192
193 void
194 rs_packet_destroy (struct rs_packet *pkt)
195 {
196   if (pkt)
197     {
198       // FIXME: memory leak! TODO: free all attributes
199       rad_free (&pkt->rpkt);
200       rs_free (pkt->conn->ctx, pkt);
201     }
202 }
203