Formatting changes.
[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   /* Put message in output buffer.  */
92   if (pkt->conn->bev)           /* TCP.  */
93     {
94       int err = bufferevent_write (pkt->conn->bev, pkt->rpkt->data,
95                                    pkt->rpkt->data_len);
96       if (err < 0)
97         return rs_err_conn_push_fl (pkt->conn, RSE_EVENT, __FILE__, __LINE__,
98                                     "bufferevent_write: %s",
99                                     evutil_gai_strerror (err));
100     }
101   else                          /* UDP.  */
102     {
103       struct rs_packet **pp = &pkt->conn->out_queue;
104
105       while (*pp && (*pp)->next)
106         *pp = (*pp)->next;
107       *pp = pkt;
108
109       conn_activate_timeout (pkt->conn); /* Retransmission timer.  */
110     }
111
112   return RSE_OK;
113 }
114
115 /* Public functions.  */
116 int
117 rs_packet_create (struct rs_connection *conn, struct rs_packet **pkt_out)
118 {
119   struct rs_packet *p;
120   RADIUS_PACKET *rpkt;
121
122   *pkt_out = NULL;
123
124   rpkt = rad_alloc (1);
125   if (!rpkt)
126     return rs_err_conn_push (conn, RSE_NOMEM, __func__);
127   rpkt->id = conn->nextid++;
128
129   p = (struct rs_packet *) malloc (sizeof (struct rs_packet));
130   if (!p)
131     {
132       rad_free (&rpkt);
133       return rs_err_conn_push (conn, RSE_NOMEM, __func__);
134     }
135   memset (p, 0, sizeof (struct rs_packet));
136   p->conn = conn;
137   p->rpkt = rpkt;
138
139   *pkt_out = p;
140   return RSE_OK;
141 }
142
143 int
144 rs_packet_create_authn_request (struct rs_connection *conn,
145                                 struct rs_packet **pkt_out,
146                                 const char *user_name, const char *user_pw)
147 {
148   struct rs_packet *pkt;
149   struct rs_attr *attr;
150
151   if (rs_packet_create (conn, pkt_out))
152     return -1;
153   pkt = *pkt_out;
154   pkt->rpkt->code = PW_AUTHENTICATION_REQUEST;
155
156   if (user_name)
157     {
158       if (rs_attr_create (conn, &attr, "User-Name", user_name))
159         return -1;
160       rs_packet_add_attr (pkt, attr);
161
162       if (user_pw)
163         {
164           if (rs_attr_create (conn, &attr, "User-Password", user_pw))
165             return -1;
166           rs_packet_add_attr (pkt, attr);
167         }
168     }
169
170   return RSE_OK;
171 }
172
173 void
174 rs_packet_add_attr (struct rs_packet *pkt, struct rs_attr *attr)
175 {
176   pairadd (&pkt->rpkt->vps, attr->vp);
177   attr->pkt = pkt;
178 }
179
180 struct radius_packet *
181 rs_packet_frpkt (struct rs_packet *pkt)
182 {
183   assert (pkt);
184   return pkt->rpkt;
185 }
186
187 void
188 rs_packet_destroy (struct rs_packet *pkt)
189 {
190   if (pkt)
191     {
192       // FIXME: memory leak! TODO: free all attributes
193       rad_free (&pkt->rpkt);
194       rs_free (pkt->conn->ctx, pkt);
195     }
196 }
197