Don't crash on reading invalid messages.
[libradsec.git] / lib / tcp.c
index 1801d62..e2b1a2f 100644 (file)
--- a/lib/tcp.c
+++ b/lib/tcp.c
@@ -1,5 +1,5 @@
-/* Copyright 2011 NORDUnet A/S. All rights reserved.
-   See the file COPYING for licensing information.  */
+/* Copyright 2011-2013 NORDUnet A/S. All rights reserved.
+   See LICENSE for licensing information. */
 
 #if defined HAVE_CONFIG_H
 #include <config.h>
@@ -12,6 +12,7 @@
 #include <event2/bufferevent_ssl.h>
 #include <openssl/err.h>
 #endif
+#include <radius/client.h>
 #include <radsec/radsec.h>
 #include <radsec/radsec-impl.h>
 #include "tcp.h"
 #include <event2/buffer.h>
 #endif
 
-static void
-_conn_timeout_cb (int fd, short event, void *data)
-{
-  struct rs_connection *conn;
-
-  assert (data);
-  conn = (struct rs_connection *) data;
-
-  if (event & EV_TIMEOUT)
-    {
-      rs_debug (("%s: connection timeout on %p (fd %d) connecting to %p\n",
-                __func__, conn, conn->fd, conn->active_peer));
-      conn->is_connecting = 0;
-      rs_err_conn_push_fl (conn, RSE_TIMEOUT_IO, __FILE__, __LINE__, NULL);
-      event_loopbreak (conn);
-    }
-}
-
-/* Read one RADIUS packet header.  Return !0 on error.  A return value
-   of 0 means that we need more data.  */
+/** Read one RADIUS packet header. Return !0 on error. */
 static int
 _read_header (struct rs_packet *pkt)
 {
@@ -52,27 +34,22 @@ _read_header (struct rs_packet *pkt)
   n = bufferevent_read (pkt->conn->bev, pkt->hdr, RS_HEADER_LEN);
   if (n == RS_HEADER_LEN)
     {
-      pkt->flags |= rs_packet_hdr_read_flag;
-      pkt->rpkt->data_len = (pkt->hdr[2] << 8) + pkt->hdr[3];
-      if (pkt->rpkt->data_len < 20 || pkt->rpkt->data_len > 4096)
+      pkt->flags |= RS_PACKET_HEADER_READ;
+      pkt->rpkt->length = (pkt->hdr[2] << 8) + pkt->hdr[3];
+      if (pkt->rpkt->length < 20 || pkt->rpkt->length > RS_MAX_PACKET_LEN)
        {
-         conn_close (&pkt->conn);
+          rs_debug (("%s: invalid packet length: %d\n",
+                     __func__, pkt->rpkt->length));
+          rs_conn_disconnect (pkt->conn);
          return rs_err_conn_push (pkt->conn, RSE_INVALID_PKT,
                                   "invalid packet length: %d",
-                                  pkt->rpkt->data_len);
-       }
-      pkt->rpkt->data = rs_malloc (pkt->conn->ctx, pkt->rpkt->data_len);
-      if (!pkt->rpkt->data)
-       {
-         conn_close (&pkt->conn);
-         return rs_err_conn_push_fl (pkt->conn, RSE_NOMEM, __FILE__, __LINE__,
-                                     NULL);
+                                  pkt->rpkt->length);
        }
       memcpy (pkt->rpkt->data, pkt->hdr, RS_HEADER_LEN);
       bufferevent_setwatermark (pkt->conn->bev, EV_READ,
-                               pkt->rpkt->data_len - RS_HEADER_LEN, 0);
+                               pkt->rpkt->length - RS_HEADER_LEN, 0);
       rs_debug (("%s: packet header read, total pkt len=%d\n",
-                __func__, pkt->rpkt->data_len));
+                __func__, pkt->rpkt->length));
     }
   else if (n < 0)
     {
@@ -80,7 +57,8 @@ _read_header (struct rs_packet *pkt)
     }
   else     /* Error: libevent gave us less than the low watermark. */
     {
-      conn_close (&pkt->conn);
+      rs_debug (("%s: got: %d octets reading header\n", __func__, n));
+      rs_conn_disconnect (pkg->conn);
       return rs_err_conn_push_fl (pkt->conn, RSE_INTERNAL, __FILE__, __LINE__,
                                  "got %d octets reading header", n);
     }
@@ -88,25 +66,33 @@ _read_header (struct rs_packet *pkt)
   return 0;
 }
 
+/** Read a message, check that it's valid RADIUS and hand it off to
+    registered user callback.
+
+    The packet is read from the bufferevent associated with \a pkt and
+    the data is stored in \a pkt->rpkt.
+
+    Return 0 on success and !0 on failure. */
 static int
 _read_packet (struct rs_packet *pkt)
 {
   size_t n = 0;
+  int err;
 
   rs_debug (("%s: trying to read %d octets of packet data\n", __func__,
-            pkt->rpkt->data_len - RS_HEADER_LEN));
+            pkt->rpkt->length - RS_HEADER_LEN));
 
   n = bufferevent_read (pkt->conn->bev,
                        pkt->rpkt->data + RS_HEADER_LEN,
-                       pkt->rpkt->data_len - RS_HEADER_LEN);
+                       pkt->rpkt->length - RS_HEADER_LEN);
 
   rs_debug (("%s: read %ld octets of packet data\n", __func__, n));
 
-  if (n == pkt->rpkt->data_len - RS_HEADER_LEN)
+  if (n == pkt->rpkt->length - RS_HEADER_LEN)
     {
       bufferevent_disable (pkt->conn->bev, EV_READ);
       rs_debug (("%s: complete packet read\n", __func__));
-      pkt->flags &= ~rs_packet_hdr_read_flag;
+      pkt->flags &= ~RS_PACKET_HEADER_READ;
       memset (pkt->hdr, 0, sizeof(*pkt->hdr));
 
       /* Checks done by rad_packet_ok:
@@ -114,11 +100,13 @@ _read_packet (struct rs_packet *pkt)
         - invalid code field
         - attribute lengths >= 2
         - attribute sizes adding up correctly  */
-      if (!rad_packet_ok (pkt->rpkt, 0))
+      err = nr_packet_ok (pkt->rpkt);
+      if (err != RSE_OK)
        {
-         conn_close (&pkt->conn);
-         return rs_err_conn_push_fl (pkt->conn, RSE_FR, __FILE__, __LINE__,
-                                     "invalid packet: %s", fr_strerror ());
+          rs_debug (("%s: %d: invalid packet\n", __func__, -err));
+          rs_conn_disconnect (pkt->conn);
+         return rs_err_conn_push_fl (pkt->conn, -err, __FILE__, __LINE__,
+                                     "invalid packet");
        }
 
 #if defined (DEBUG)
@@ -141,7 +129,7 @@ _read_packet (struct rs_packet *pkt)
     rs_debug (("%s: buffer frozen when reading packet\n", __func__));
   else                         /* Short packet.  */
     rs_debug (("%s: waiting for another %d octets\n", __func__,
-              pkt->rpkt->data_len - RS_HEADER_LEN - n));
+              pkt->rpkt->length - RS_HEADER_LEN - n));
 
   return 0;
 }
@@ -163,9 +151,15 @@ tcp_read_cb (struct bufferevent *bev, void *user_data)
   assert (pkt->rpkt);
 
   pkt->rpkt->sockfd = pkt->conn->fd;
-  pkt->rpkt->vps = NULL;
+  pkt->rpkt->vps = NULL;        /* FIXME: can this be done when initializing pkt? */
+
+  /* Read a message header if not already read, return if that
+     fails. Read a message and have it dispatched to the user
+     registered callback.
 
-  if ((pkt->flags & rs_packet_hdr_read_flag) == 0)
+     Room for improvement: Peek inside buffer (evbuffer_copyout()) to
+     avoid the extra copying. */
+  if ((pkt->flags & RS_PACKET_HEADER_READ) == 0)
     if (_read_header (pkt))
       return;                  /* Error.  */
   _read_packet (pkt);
@@ -176,22 +170,32 @@ tcp_event_cb (struct bufferevent *bev, short events, void *user_data)
 {
   struct rs_packet *pkt = (struct rs_packet *) user_data;
   struct rs_connection *conn = NULL;
-  struct rs_peer *p = NULL;
   int sockerr = 0;
 #if defined (RS_ENABLE_TLS)
   unsigned long tlserr = 0;
 #endif
+#if defined (DEBUG)
+  struct rs_peer *p = NULL;
+#endif
 
   assert (pkt);
   assert (pkt->conn);
-  assert (pkt->conn->active_peer);
   conn = pkt->conn;
+#if defined (DEBUG)
+  assert (pkt->conn->active_peer);
   p = conn->active_peer;
+#endif
 
   conn->is_connecting = 0;
   if (events & BEV_EVENT_CONNECTED)
     {
-      event_on_connect (conn, pkt);
+      if (conn->tev)
+       evtimer_del (conn->tev); /* Cancel connect timer.  */
+      if (event_on_connect (conn, pkt))
+        {
+          event_on_disconnect (conn);
+          event_loopbreak (conn);
+        }
     }
   else if (events & BEV_EVENT_EOF)
     {
@@ -201,7 +205,7 @@ tcp_event_cb (struct bufferevent *bev, short events, void *user_data)
     {
       rs_debug (("%s: %p times out on %s\n", __func__, p,
                 (events & BEV_EVENT_READING) ? "read" : "write"));
-      rs_err_conn_push_fl (pkt->conn, RSE_TIMEOUT_IO, __FILE__, __LINE__, NULL);
+      rs_err_conn_push_fl (conn, RSE_TIMEOUT_IO, __FILE__, __LINE__, NULL);
     }
   else if (events & BEV_EVENT_ERROR)
     {
@@ -209,12 +213,13 @@ tcp_event_cb (struct bufferevent *bev, short events, void *user_data)
       if (sockerr == 0)        /* FIXME: True that errno == 0 means closed? */
        {
          event_on_disconnect (conn);
+         rs_err_conn_push_fl (conn, RSE_DISCO, __FILE__, __LINE__, NULL);
        }
       else
        {
          rs_debug (("%s: %d: %d (%s)\n", __func__, conn->fd, sockerr,
                     evutil_socket_error_to_string (sockerr)));
-         rs_err_conn_push_fl (pkt->conn, RSE_SOCKERR, __FILE__, __LINE__,
+         rs_err_conn_push_fl (conn, RSE_SOCKERR, __FILE__, __LINE__,
                               "%d: %d (%s)", conn->fd, sockerr,
                               evutil_socket_error_to_string (sockerr));
        }
@@ -227,7 +232,7 @@ tcp_event_cb (struct bufferevent *bev, short events, void *user_data)
            {
              rs_debug (("%s: openssl error: %s\n", __func__,
                         ERR_error_string (tlserr, NULL)));
-             rs_err_conn_push_fl (pkt->conn, RSE_SSLERR, __FILE__, __LINE__,
+             rs_err_conn_push_fl (conn, RSE_SSLERR, __FILE__, __LINE__,
                                   ERR_error_string (tlserr, NULL));
            }
        }
@@ -254,18 +259,16 @@ tcp_write_cb (struct bufferevent *bev, void *ctx)
 }
 
 int
-tcp_set_connect_timeout (struct rs_connection *conn)
+tcp_init_connect_timer (struct rs_connection *conn)
 {
-  struct timeval tv;
+  assert (conn);
 
-  if (!conn->tev)
-    conn->tev = evtimer_new (conn->evb, _conn_timeout_cb, conn);
+  if (conn->tev)
+    event_free (conn->tev);
+  conn->tev = evtimer_new (conn->evb, event_conn_timeout_cb, conn);
   if (!conn->tev)
     return rs_err_conn_push_fl (conn, RSE_EVENT, __FILE__, __LINE__,
                                "evtimer_new");
-  tv.tv_sec = conn->realm->timeout;
-  tv.tv_usec = 0;
-  evtimer_add (conn->tev, &tv);
 
   return RSE_OK;
 }