/*
protocol_auth.c -- handle the meta-protocol, authentication
- 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_auth.c,v 1.1.4.18 2003/01/12 17:02:23 guus Exp $
+ $Id: protocol_auth.c,v 1.1.4.24 2003/07/17 15:06:26 guus Exp $
*/
-#include "config.h"
-
-#include <stdlib.h>
-#include <string.h>
-#include <syslog.h>
-#include <stdio.h>
-#include <stdarg.h>
-#include <errno.h>
-
-#include <utils.h>
-#include <xalloc.h>
-#include <avl_tree.h>
+#include "system.h"
#include <openssl/sha.h>
#include <openssl/rand.h>
#include <openssl/evp.h>
-#ifndef HAVE_RAND_PSEUDO_BYTES
-#define RAND_pseudo_bytes RAND_bytes
-#endif
-
+#include "avl_tree.h"
#include "conf.h"
-#include "net.h"
-#include "netutl.h"
-#include "protocol.h"
-#include "meta.h"
#include "connection.h"
-#include "node.h"
#include "edge.h"
#include "graph.h"
-
-#include "system.h"
+#include "logger.h"
+#include "net.h"
+#include "netutl.h"
+#include "node.h"
+#include "protocol.h"
+#include "utils.h"
+#include "xalloc.h"
int send_id(connection_t *c)
{
cp();
if(sscanf(c->buffer, "%*d " MAX_STRING " %d", name, &c->protocol_version) != 2) {
- syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ID", c->name,
+ logger(LOG_ERR, _("Got bad %s from %s (%s)"), "ID", c->name,
c->hostname);
return -1;
}
/* Check if identity is a valid name */
if(check_id(name)) {
- syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ID", c->name,
+ logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ID", c->name,
c->hostname, "invalid name");
return -1;
}
if(c->name) {
if(strcmp(c->name, name)) {
- syslog(LOG_ERR, _("Peer %s is %s instead of %s"), c->hostname, name,
+ logger(LOG_ERR, _("Peer %s is %s instead of %s"), c->hostname, name,
c->name);
return -1;
}
/* Check if version matches */
if(c->protocol_version != myself->connection->protocol_version) {
- syslog(LOG_ERR, _("Peer %s (%s) uses incompatible version %d"),
+ logger(LOG_ERR, _("Peer %s (%s) uses incompatible version %d"),
c->name, c->hostname, c->protocol_version);
return -1;
}
bla = read_connection_config(c);
if(bla) {
- syslog(LOG_ERR, _("Peer %s had unknown identity (%s)"), c->hostname,
+ logger(LOG_ERR, _("Peer %s had unknown identity (%s)"), c->hostname,
c->name);
return -1;
}
c->outkey = xmalloc(len);
if(!c->outctx)
- c->outctx = xmalloc(sizeof(*c->outctx));
+ c->outctx = xmalloc_and_zero(sizeof(*c->outctx));
cp();
/* Copy random data to the buffer */
c->outkey[0] &= 0x7F;
- if(debug_lvl >= DEBUG_SCARY_THINGS) {
+ ifdebug(SCARY_THINGS) {
bin2hex(c->outkey, buffer, len);
buffer[len * 2] = '\0';
- syslog(LOG_DEBUG, _("Generated random meta key (unencrypted): %s"),
+ logger(LOG_DEBUG, _("Generated random meta key (unencrypted): %s"),
buffer);
}
*/
if(RSA_public_encrypt(len, c->outkey, buffer, c->rsa_key, RSA_NO_PADDING) != len) {
- syslog(LOG_ERR, _("Error during encryption of meta key for %s (%s)"),
+ logger(LOG_ERR, _("Error during encryption of meta key for %s (%s)"),
c->name, c->hostname);
return -1;
}
cp();
- if(sscanf
- (c->buffer, "%*d %d %d %d %d " MAX_STRING, &cipher, &digest, &maclength,
- &compression, buffer) != 5) {
- syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "METAKEY", c->name,
+ if(sscanf(c->buffer, "%*d %d %d %d %d " MAX_STRING, &cipher, &digest, &maclength, &compression, buffer) != 5) {
+ logger(LOG_ERR, _("Got bad %s from %s (%s)"), "METAKEY", c->name,
c->hostname);
return -1;
}
/* Check if the length of the meta key is all right */
if(strlen(buffer) != len * 2) {
- syslog(LOG_ERR, _("Possible intruder %s (%s): %s"), c->name,
- c->hostname, "wrong keylength");
+ logger(LOG_ERR, _("Possible intruder %s (%s): %s"), c->name, c->hostname, "wrong keylength");
return -1;
}
c->inkey = xmalloc(len);
if(!c->inctx)
- c->inctx = xmalloc(sizeof(*c->inctx));
+ c->inctx = xmalloc_and_zero(sizeof(*c->inctx));
/* Convert the challenge from hexadecimal back to binary */
/* Decrypt the meta key */
if(RSA_private_decrypt(len, buffer, c->inkey, myself->connection->rsa_key, RSA_NO_PADDING) != len) { /* See challenge() */
- syslog(LOG_ERR, _("Error during encryption of meta key for %s (%s)"),
+ logger(LOG_ERR, _("Error during encryption of meta key for %s (%s)"),
c->name, c->hostname);
return -1;
}
- if(debug_lvl >= DEBUG_SCARY_THINGS) {
+ ifdebug(SCARY_THINGS) {
bin2hex(c->inkey, buffer, len);
buffer[len * 2] = '\0';
- syslog(LOG_DEBUG, _("Received random meta key (unencrypted): %s"),
- buffer);
+ logger(LOG_DEBUG, _("Received random meta key (unencrypted): %s"), buffer);
}
/* All incoming requests will now be encrypted. */
if(cipher) {
c->incipher = EVP_get_cipherbynid(cipher);
-
+
if(!c->incipher) {
- syslog(LOG_ERR, _("%s (%s) uses unknown cipher!"), c->name,
- c->hostname);
+ logger(LOG_ERR, _("%s (%s) uses unknown cipher!"), c->name, c->hostname);
return -1;
}
c->indigest = EVP_get_digestbynid(digest);
if(!c->indigest) {
- syslog(LOG_ERR, _("Node %s (%s) uses unknown digest!"), c->name,
- c->hostname);
+ logger(LOG_ERR, _("Node %s (%s) uses unknown digest!"), c->name, c->hostname);
return -1;
}
if(c->inmaclength > c->indigest->md_size || c->inmaclength < 0) {
- syslog(LOG_ERR, _("%s (%s) uses bogus MAC length!"), c->name,
- c->hostname);
+ logger(LOG_ERR, _("%s (%s) uses bogus MAC length!"), c->name, c->hostname);
return -1;
}
} else {
cp();
if(sscanf(c->buffer, "%*d " MAX_STRING, buffer) != 1) {
- syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "CHALLENGE", c->name,
+ logger(LOG_ERR, _("Got bad %s from %s (%s)"), "CHALLENGE", c->name,
c->hostname);
return -1;
}
/* Check if the length of the challenge is all right */
if(strlen(buffer) != len * 2) {
- syslog(LOG_ERR, _("Possible intruder %s (%s): %s"), c->name,
+ logger(LOG_ERR, _("Possible intruder %s (%s): %s"), c->name,
c->hostname, "wrong challenge length");
return -1;
}
cp();
if(sscanf(c->buffer, "%*d " MAX_STRING, hishash) != 1) {
- syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "CHAL_REPLY", c->name,
+ logger(LOG_ERR, _("Got bad %s from %s (%s)"), "CHAL_REPLY", c->name,
c->hostname);
return -1;
}
/* Check if the length of the hash is all right */
if(strlen(hishash) != c->outdigest->md_size * 2) {
- syslog(LOG_ERR, _("Possible intruder %s (%s): %s"), c->name,
+ logger(LOG_ERR, _("Possible intruder %s (%s): %s"), c->name,
c->hostname, _("wrong challenge reply length"));
return -1;
}
/* Verify the incoming hash with the calculated hash */
if(memcmp(hishash, myhash, c->outdigest->md_size)) {
- syslog(LOG_ERR, _("Possible intruder %s (%s): %s"), c->name,
+ logger(LOG_ERR, _("Possible intruder %s (%s): %s"), c->name,
c->hostname, _("wrong challenge reply"));
- if(debug_lvl >= DEBUG_SCARY_THINGS) {
+ ifdebug(SCARY_THINGS) {
bin2hex(myhash, hishash, SHA_DIGEST_LENGTH);
hishash[SHA_DIGEST_LENGTH * 2] = '\0';
- syslog(LOG_DEBUG, _("Expected challenge reply: %s"), hishash);
+ logger(LOG_DEBUG, _("Expected challenge reply: %s"), hishash);
}
return -1;
return x;
}
-void send_everything(connection_t *c)
+static void send_everything(connection_t *c)
{
avl_node_t *node, *node2;
node_t *n;
cp();
- if(sscanf
- (c->buffer, "%*d " MAX_STRING " %d %lx", hisport, &weight, &options) != 3) {
- syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ACK", c->name,
+ if(sscanf(c->buffer, "%*d " MAX_STRING " %d %lx", hisport, &weight, &options) != 3) {
+ logger(LOG_ERR, _("Got bad %s from %s (%s)"), "ACK", c->name,
c->hostname);
return -1;
}
} else {
if(n->connection) {
/* Oh dear, we already have a connection to this node. */
- if(debug_lvl >= DEBUG_CONNECTIONS)
- syslog(LOG_DEBUG, _("Established a second connection with %s (%s), closing old connection"),
+ ifdebug(CONNECTIONS) logger(LOG_DEBUG, _("Established a second connection with %s (%s), closing old connection"),
n->name, n->hostname);
terminate_connection(n->connection, 0);
/* Run graph algorithm to purge key and make sure up/down scripts are rerun with new IP addresses and stuff */
c->allow_request = ALL;
c->status.active = 1;
- if(debug_lvl >= DEBUG_CONNECTIONS)
- syslog(LOG_NOTICE, _("Connection with %s (%s) activated"), c->name,
+ ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Connection with %s (%s) activated"), c->name,
c->hostname);
/* Send him everything we know */