Simplify logging, update copyrights and some minor cleanups.
[tinc] / src / protocol_key.c
index 66c9a90..1a6804a 100644 (file)
@@ -1,7 +1,7 @@
 /*
     protocol_key.c -- handle the meta-protocol, key exchange
-    Copyright (C) 1999-2002 Ivo Timmermans <ivo@o2w.nl>,
-                  2000-2002 Guus Sliepen <guus@sliepen.eu.org>
+    Copyright (C) 1999-2003 Ivo Timmermans <ivo@o2w.nl>,
+                  2000-2003 Guus Sliepen <guus@sliepen.eu.org>
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
-    $Id: protocol_key.c,v 1.1.4.13 2002/09/09 21:24:56 guus Exp $
+    $Id: protocol_key.c,v 1.1.4.18 2003/07/12 17:41:47 guus Exp $
 */
 
 #include "config.h"
 
 #include <stdlib.h>
 #include <string.h>
-#include <syslog.h>
 #include <stdio.h>
 #include <stdarg.h>
 #include <errno.h>
 #include "meta.h"
 #include "connection.h"
 #include "node.h"
+#include "logger.h"
 
 #include "system.h"
 
 int mykeyused = 0;
 
-int send_key_changed(connection_t * c, node_t * n)
+int send_key_changed(connection_t *c, node_t *n)
 {
        cp();
 
@@ -59,7 +59,7 @@ int send_key_changed(connection_t * c, node_t * n)
        return send_request(c, "%d %lx %s", KEY_CHANGED, random(), n->name);
 }
 
-int key_changed_h(connection_t * c)
+int key_changed_h(connection_t *c)
 {
        char name[MAX_STRING_SIZE];
        node_t *n;
@@ -67,7 +67,7 @@ int key_changed_h(connection_t * c)
        cp();
 
        if(sscanf(c->buffer, "%*d %*x " MAX_STRING, name) != 1) {
-               syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "KEY_CHANGED",
+               logger(LOG_ERR, _("Got bad %s from %s (%s)"), "KEY_CHANGED",
                           c->name, c->hostname);
                return -1;
        }
@@ -78,7 +78,7 @@ int key_changed_h(connection_t * c)
        n = lookup_node(name);
 
        if(!n) {
-               syslog(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist"),
+               logger(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist"),
                           "KEY_CHANGED", c->name, c->hostname, name);
                return -1;
        }
@@ -93,14 +93,14 @@ int key_changed_h(connection_t * c)
        return 0;
 }
 
-int send_req_key(connection_t * c, node_t * from, node_t * to)
+int send_req_key(connection_t *c, node_t *from, node_t *to)
 {
        cp();
 
        return send_request(c, "%d %s %s", REQ_KEY, from->name, to->name);
 }
 
-int req_key_h(connection_t * c)
+int req_key_h(connection_t *c)
 {
        char from_name[MAX_STRING_SIZE];
        char to_name[MAX_STRING_SIZE];
@@ -109,7 +109,7 @@ int req_key_h(connection_t * c)
        cp();
 
        if(sscanf(c->buffer, "%*d " MAX_STRING " " MAX_STRING, from_name, to_name) != 2) {
-               syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "REQ_KEY", c->name,
+               logger(LOG_ERR, _("Got bad %s from %s (%s)"), "REQ_KEY", c->name,
                           c->hostname);
                return -1;
        }
@@ -117,7 +117,7 @@ int req_key_h(connection_t * c)
        from = lookup_node(from_name);
 
        if(!from) {
-               syslog(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist in our connection list"),
+               logger(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist in our connection list"),
                           "REQ_KEY", c->name, c->hostname, from_name);
                return -1;
        }
@@ -125,7 +125,7 @@ int req_key_h(connection_t * c)
        to = lookup_node(to_name);
 
        if(!to) {
-               syslog(LOG_ERR, _("Got %s from %s (%s) destination %s which does not exist in our connection list"),
+               logger(LOG_ERR, _("Got %s from %s (%s) destination %s which does not exist in our connection list"),
                           "REQ_KEY", c->name, c->hostname, to_name);
                return -1;
        }
@@ -135,6 +135,7 @@ int req_key_h(connection_t * c)
        if(to == myself) {                      /* Yes, send our own key back */
                mykeyused = 1;
                from->received_seqno = 0;
+               memset(from->late, 0, sizeof(from->late));
                send_ans_key(c, myself, from);
        } else {
                send_req_key(to->nexthop->connection, from, to);
@@ -143,7 +144,7 @@ int req_key_h(connection_t * c)
        return 0;
 }
 
-int send_ans_key(connection_t * c, node_t * from, node_t * to)
+int send_ans_key(connection_t *c, node_t *from, node_t *to)
 {
        char key[MAX_STRING_SIZE];
 
@@ -159,7 +160,7 @@ int send_ans_key(connection_t * c, node_t * from, node_t * to)
                                                from->compression);
 }
 
-int ans_key_h(connection_t * c)
+int ans_key_h(connection_t *c)
 {
        char from_name[MAX_STRING_SIZE];
        char to_name[MAX_STRING_SIZE];
@@ -172,7 +173,7 @@ int ans_key_h(connection_t * c)
        if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING" "MAX_STRING" %d %d %d %d",
                from_name, to_name, key, &cipher, &digest, &maclength,
                &compression) != 7) {
-               syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ANS_KEY", c->name,
+               logger(LOG_ERR, _("Got bad %s from %s (%s)"), "ANS_KEY", c->name,
                           c->hostname);
                return -1;
        }
@@ -180,7 +181,7 @@ int ans_key_h(connection_t * c)
        from = lookup_node(from_name);
 
        if(!from) {
-               syslog(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist in our connection list"),
+               logger(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist in our connection list"),
                           "ANS_KEY", c->name, c->hostname, from_name);
                return -1;
        }
@@ -188,7 +189,7 @@ int ans_key_h(connection_t * c)
        to = lookup_node(to_name);
 
        if(!to) {
-               syslog(LOG_ERR, _("Got %s from %s (%s) destination %s which does not exist in our connection list"),
+               logger(LOG_ERR, _("Got %s from %s (%s) destination %s which does not exist in our connection list"),
                           "ANS_KEY", c->name, c->hostname, to_name);
                return -1;
        }
@@ -219,13 +220,13 @@ int ans_key_h(connection_t * c)
                from->cipher = EVP_get_cipherbynid(cipher);
 
                if(!from->cipher) {
-                       syslog(LOG_ERR, _("Node %s (%s) uses unknown cipher!"), from->name,
+                       logger(LOG_ERR, _("Node %s (%s) uses unknown cipher!"), from->name,
                                   from->hostname);
                        return -1;
                }
 
                if(from->keylength != from->cipher->key_len + from->cipher->iv_len) {
-                       syslog(LOG_ERR, _("Node %s (%s) uses wrong keylength!"), from->name,
+                       logger(LOG_ERR, _("Node %s (%s) uses wrong keylength!"), from->name,
                                   from->hostname);
                        return -1;
                }
@@ -239,13 +240,13 @@ int ans_key_h(connection_t * c)
                from->digest = EVP_get_digestbynid(digest);
 
                if(!from->digest) {
-                       syslog(LOG_ERR, _("Node %s (%s) uses unknown digest!"), from->name,
+                       logger(LOG_ERR, _("Node %s (%s) uses unknown digest!"), from->name,
                                   from->hostname);
                        return -1;
                }
 
                if(from->maclength > from->digest->md_size || from->maclength < 0) {
-                       syslog(LOG_ERR, _("Node %s (%s) uses bogus MAC length!"),
+                       logger(LOG_ERR, _("Node %s (%s) uses bogus MAC length!"),
                                   from->name, from->hostname);
                        return -1;
                }
@@ -253,8 +254,15 @@ int ans_key_h(connection_t * c)
                from->digest = NULL;
        }
 
+       if(compression < 0 || compression > 11) {
+               logger(LOG_ERR, _("Node %s (%s) uses bogus compression level!"), from->name, from->hostname);
+               return -1;
+       }
+       
        from->compression = compression;
 
+       EVP_EncryptInit_ex(&from->packet_ctx, from->cipher, NULL, from->key, from->key + from->cipher->key_len);
+
        flush_queue(from);
 
        return 0;