3 Copyright (C) 1998-2002 Ivo Timmermans <itimmermans@bigfoot.com>,
4 2000-2002 Guus Sliepen <guus@sliepen.warande.net>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 $Id: net_setup.c,v 1.1 2002/04/28 12:46:26 zarq Exp $
28 #include <netinet/in.h>
30 #include <netinet/ip.h>
31 #include <netinet/tcp.h>
38 #include <sys/types.h>
40 #include <sys/ioctl.h>
41 /* SunOS really wants sys/socket.h BEFORE net/if.h,
42 and FreeBSD wants these lines below the rest. */
43 #include <arpa/inet.h>
44 #include <sys/socket.h>
47 #include <openssl/pem.h>
48 #include <openssl/rsa.h>
49 #include <openssl/rand.h>
57 #include "interface.h"
58 #include "connection.h"
76 int read_rsa_public_key(connection_t *c)
83 c->rsa_key = RSA_new();
85 /* First, check for simple PublicKey statement */
87 if(get_config_string(lookup_config(c->config_tree, "PublicKey"), &key))
89 BN_hex2bn(&c->rsa_key->n, key);
90 BN_hex2bn(&c->rsa_key->e, "FFFF");
95 /* Else, check for PublicKeyFile statement and read it */
97 if(get_config_string(lookup_config(c->config_tree, "PublicKeyFile"), &fname))
99 if(is_safe_path(fname))
101 if((fp = fopen(fname, "r")) == NULL)
104 _("Error reading RSA public key file `%s': %s"),
105 fname, strerror(errno));
110 c->rsa_key = PEM_read_RSAPublicKey(fp, &c->rsa_key, NULL, NULL);
114 syslog(LOG_ERR, _("Reading RSA public key file `%s' failed: %s"),
115 fname, strerror(errno));
127 /* Else, check if a harnessed public key is in the config file */
129 asprintf(&fname, "%s/hosts/%s", confbase, c->name);
130 if((fp = fopen(fname, "r")))
132 c->rsa_key = PEM_read_RSAPublicKey(fp, &c->rsa_key, NULL, NULL);
142 syslog(LOG_ERR, _("No public key for %s specified!"), c->name);
147 int read_rsa_private_key(void)
152 if(get_config_string(lookup_config(config_tree, "PrivateKey"), &key))
154 myself->connection->rsa_key = RSA_new();
155 BN_hex2bn(&myself->connection->rsa_key->d, key);
156 BN_hex2bn(&myself->connection->rsa_key->e, "FFFF");
161 if(!get_config_string(lookup_config(config_tree, "PrivateKeyFile"), &fname))
162 asprintf(&fname, "%s/rsa_key.priv", confbase);
164 if(is_safe_path(fname))
166 if((fp = fopen(fname, "r")) == NULL)
168 syslog(LOG_ERR, _("Error reading RSA private key file `%s': %s"),
169 fname, strerror(errno));
174 myself->connection->rsa_key = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
176 if(!myself->connection->rsa_key)
178 syslog(LOG_ERR, _("Reading RSA private key file `%s' failed: %s"),
179 fname, strerror(errno));
189 int check_rsa_key(RSA *rsa_key)
191 char *test1, *test2, *test3;
193 if(rsa_key->p && rsa_key->q)
195 if(RSA_check_key(rsa_key) != 1)
200 test1 = xmalloc(RSA_size(rsa_key));
201 test2 = xmalloc(RSA_size(rsa_key));
202 test3 = xmalloc(RSA_size(rsa_key));
204 if(RSA_public_encrypt(RSA_size(rsa_key), test1, test2, rsa_key, RSA_NO_PADDING) != RSA_size(rsa_key))
207 if(RSA_private_decrypt(RSA_size(rsa_key), test2, test3, rsa_key, RSA_NO_PADDING) != RSA_size(rsa_key))
210 if(memcmp(test1, test3, RSA_size(rsa_key)))
218 Configure node_t myself and set up the local sockets (listen only)
220 int setup_myself(void)
224 char *name, *mode, *afname, *cipher, *digest;
228 myself->connection = new_connection();
229 init_configuration(&myself->connection->config_tree);
231 asprintf(&myself->hostname, _("MYSELF"));
232 asprintf(&myself->connection->hostname, _("MYSELF"));
234 myself->connection->options = 0;
235 myself->connection->protocol_version = PROT_CURRENT;
237 if(!get_config_string(lookup_config(config_tree, "Name"), &name)) /* Not acceptable */
239 syslog(LOG_ERR, _("Name for tinc daemon required!"));
245 syslog(LOG_ERR, _("Invalid name for myself!"));
251 myself->connection->name = xstrdup(name);
254 if(read_rsa_private_key())
257 if(read_connection_config(myself->connection))
259 syslog(LOG_ERR, _("Cannot open host configuration file for myself!"));
263 if(read_rsa_public_key(myself->connection))
267 if(check_rsa_key(myself->connection->rsa_key))
269 syslog(LOG_ERR, _("Invalid public/private keypair!"));
273 if(!get_config_string(lookup_config(myself->connection->config_tree, "Port"), &myport))
274 asprintf(&myport, "655");
276 /* Read in all the subnets specified in the host configuration file */
278 cfg = lookup_config(myself->connection->config_tree, "Subnet");
282 if(!get_config_subnet(cfg, &subnet))
285 subnet_add(myself, subnet);
287 cfg = lookup_config_next(myself->connection->config_tree, cfg);
291 /* Check some options */
293 if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice))
295 myself->options |= OPTION_INDIRECT;
297 if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice))
299 myself->options |= OPTION_TCPONLY;
301 if(get_config_bool(lookup_config(myself->connection->config_tree, "IndirectData"), &choice))
303 myself->options |= OPTION_INDIRECT;
305 if(get_config_bool(lookup_config(myself->connection->config_tree, "TCPOnly"), &choice))
307 myself->options |= OPTION_TCPONLY;
309 if(myself->options & OPTION_TCPONLY)
310 myself->options |= OPTION_INDIRECT;
312 if(get_config_string(lookup_config(config_tree, "Mode"), &mode))
314 if(!strcasecmp(mode, "router"))
315 routing_mode = RMODE_ROUTER;
316 else if (!strcasecmp(mode, "switch"))
317 routing_mode = RMODE_SWITCH;
318 else if (!strcasecmp(mode, "hub"))
319 routing_mode = RMODE_HUB;
322 syslog(LOG_ERR, _("Invalid routing mode!"));
328 routing_mode = RMODE_ROUTER;
330 get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
331 #if !defined(SOL_IP) || !defined(IP_TOS)
332 if(priorityinheritance)
333 syslog(LOG_WARNING, _("PriorityInheritance not supported on this platform"));
336 if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire))
339 if(get_config_int(lookup_config(myself->connection->config_tree, "MaxTimeout"), &maxtimeout))
343 syslog(LOG_ERR, _("Bogus maximum timeout!"));
350 if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname))
352 if(!strcasecmp(afname, "IPv4"))
353 addressfamily = AF_INET;
354 else if (!strcasecmp(afname, "IPv6"))
355 addressfamily = AF_INET6;
356 else if (!strcasecmp(afname, "any"))
357 addressfamily = AF_UNSPEC;
360 syslog(LOG_ERR, _("Invalid address family!"));
366 addressfamily = AF_INET;
368 get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames);
370 /* Generate packet encryption key */
372 if(get_config_string(lookup_config(myself->connection->config_tree, "Cipher"), &cipher))
374 if(!strcasecmp(cipher, "none"))
376 myself->cipher = NULL;
380 if(!(myself->cipher = EVP_get_cipherbyname(cipher)))
382 syslog(LOG_ERR, _("Unrecognized cipher type!"));
388 myself->cipher = EVP_bf_cbc();
391 myself->keylength = myself->cipher->key_len + myself->cipher->iv_len;
393 myself->keylength = 1;
395 myself->connection->outcipher = EVP_bf_ofb();
397 myself->key = (char *)xmalloc(myself->keylength);
398 RAND_pseudo_bytes(myself->key, myself->keylength);
400 if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime))
403 keyexpires = now + keylifetime;
405 /* Check if we want to use message authentication codes... */
407 if(get_config_string(lookup_config(myself->connection->config_tree, "Digest"), &digest))
409 if(!strcasecmp(digest, "none"))
411 myself->digest = NULL;
415 if(!(myself->digest = EVP_get_digestbyname(digest)))
417 syslog(LOG_ERR, _("Unrecognized digest type!"));
423 myself->digest = EVP_sha1();
425 myself->connection->outdigest = EVP_sha1();
427 if(get_config_int(lookup_config(myself->connection->config_tree, "MACLength"), &myself->maclength))
431 if(myself->maclength > myself->digest->md_size)
433 syslog(LOG_ERR, _("MAC length exceeds size of digest!"));
436 else if (myself->maclength < 0)
438 syslog(LOG_ERR, _("Bogus MAC length!"));
444 myself->maclength = 4;
446 myself->connection->outmaclength = 0;
450 if(get_config_int(lookup_config(myself->connection->config_tree, "Compression"), &myself->compression))
452 if(myself->compression < 0 || myself->compression > 9)
454 syslog(LOG_ERR, _("Bogus compression level!"));
459 myself->compression = 0;
461 myself->connection->outcompression = 0;
465 myself->nexthop = myself;
466 myself->via = myself;
467 myself->status.active = 1;
472 syslog(LOG_NOTICE, _("Ready"));
478 setup all initial network connections
480 int setup_network_connections(void)
492 if(get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout))
502 if(setup_myself() < 0)
505 try_outgoing_connections();
511 close all open network connections
513 void close_network_connections(void)
515 avl_node_t *node, *next;
519 for(node = connection_tree->head; node; node = next)
522 c = (connection_t *)node->data;
524 free(c->outgoing->name), free(c->outgoing), c->outgoing = NULL;
525 terminate_connection(c, 0);
528 if(myself && myself->connection)
529 terminate_connection(myself->connection, 0);
531 for(i = 0; i < listen_sockets; i++)
533 close(listen_socket[i].tcp);
534 close(listen_socket[i].udp);