- if(debug_lvl >= DEBUG_CONNECTIONS)
- syslog(LOG_INFO, _("Connected to %s port %hd"),
- c->hostname, c->port);
-cp
- return 0;
-}
-
-int setup_outgoing_connection(char *name)
-{
- connection_t *c;
- struct hostent *h;
-cp
- c = new_connection();
- c->name = xstrdup(name);
-
- read_connection_config(c);
-
- if(!get_config_string(lookup_config(c->config_tree, "Address"), &c->hostname))
- {
- syslog(LOG_ERR, _("No address specified for %s"), c->name);
- free_connection(c);
- return -1;
- }
-
- if(!get_config_port(lookup_config(c->config_tree, "Port"), &c->port))
- {
- syslog(LOG_ERR, _("No port specified for %s"), c->name);
- free_connection(c);
- return -1;
- }
-
- if(!(h = gethostbyname(c->hostname)))
- {
- syslog(LOG_ERR, _("Error looking up `%s': %m"), c->hostname);
- free_connection(c);
- return -1;
- }
-
- c->address = ntohl(*((ipv4_t*)(h->h_addr_list[0])));
- c->hostname = hostlookup(htonl(c->address));
-
- if(setup_outgoing_socket(c) < 0)
- {
- syslog(LOG_ERR, _("Could not set up a meta connection to %s (%s)"),
- c->name, c->hostname);
- free_connection(c);
- return -1;
- }
-
- c->status.outgoing = 1;
- c->last_ping_time = time(NULL);
-
- connection_add(c);
-
- send_id(c);
-cp
- return 0;
-}
-
-int read_rsa_public_key(connection_t *c)
-{
- FILE *fp;
- char *fname;
- char *key;
- void *result;
-cp
- if(!c->rsa_key)
- c->rsa_key = RSA_new();
-
- /* First, check for simple PublicKey statement */
-
- if(get_config_string(lookup_config(c->config_tree, "PublicKey"), &key))
- {
- BN_hex2bn(&c->rsa_key->n, key);
- BN_hex2bn(&c->rsa_key->e, "FFFF");
- return 0;
- }
-
- /* Else, check for PublicKeyFile statement and read it */
-
- if(get_config_string(lookup_config(c->config_tree, "PublicKeyFile"), &fname))
- {
- if(is_safe_path(fname))
- {
- if((fp = fopen(fname, "r")) == NULL)
- {
- syslog(LOG_ERR, _("Error reading RSA public key file `%s': %m"),
- fname);
- return -1;
- }
- result = PEM_read_RSAPublicKey(fp, &c->rsa_key, NULL, NULL);
- fclose(fp);
- if(!result)
- {
- syslog(LOG_ERR, _("Reading RSA public key file `%s' failed: %m"),
- fname);
- return -1;
- }
- return 0;
- }
- else
- return -1;
- }
-
- /* Else, check if a harnessed public key is in the config file */
-
- result = NULL;
-
- asprintf(&fname, "%s/hosts/%s", confbase, c->name);
- if((fp = fopen(fname, "r")))
- {
- result = PEM_read_RSAPublicKey(fp, &c->rsa_key, NULL, NULL);
- fclose(fp);
- free(fname);
- }
-
- free(fname);
-
- if(result)
- return 0;
- else
- {
- syslog(LOG_ERR, _("No public key for %s specified!"), c->name);
- return -1;
- }
-}
-
-int read_rsa_private_key(void)
-{
- FILE *fp;
- void *result;
- char *fname, *key;
-cp
- if(!myself->connection->rsa_key)
- myself->connection->rsa_key = RSA_new();
-
- if(get_config_string(lookup_config(config_tree, "PrivateKey"), &key))
- {
- BN_hex2bn(&myself->connection->rsa_key->d, key);
- BN_hex2bn(&myself->connection->rsa_key->e, "FFFF");
- }
- else if(get_config_string(lookup_config(config_tree, "PrivateKeyFile"), &fname))
- {
- if((fp = fopen(fname, "r")) == NULL)
- {
- syslog(LOG_ERR, _("Error reading RSA private key file `%s': %m"),
- fname);
- return -1;
- }
- result = PEM_read_RSAPrivateKey(fp, &myself->connection->rsa_key, NULL, NULL);
- fclose(fp);
- if(!result)
- {
- syslog(LOG_ERR, _("Reading RSA private key file `%s' failed: %m"),
- fname);
- return -1;
- }
- }
- else
- {
- syslog(LOG_ERR, _("No private key for tinc daemon specified!"));
- return -1;
- }
-cp
- return 0;
-}
-
-/*
- Configure node_t myself and set up the local sockets (listen only)
-*/
-int setup_myself(void)
-{
- config_t *cfg;
- subnet_t *subnet;
- char *name, *mode;
- int choice;
-cp
- myself = new_node();
- myself->connection = new_connection();
-
- asprintf(&myself->hostname, _("MYSELF"));
- asprintf(&myself->connection->hostname, _("MYSELF"));
-
- myself->connection->options = 0;
- myself->connection->protocol_version = PROT_CURRENT;
-
- if(!get_config_string(lookup_config(config_tree, "Name"), &name)) /* Not acceptable */
- {
- syslog(LOG_ERR, _("Name for tinc daemon required!"));
- return -1;
- }
-
- if(check_id(name))
- {
- syslog(LOG_ERR, _("Invalid name for myself!"));
- free(name);
- return -1;
- }
-
- myself->name = name;
- myself->connection->name = xstrdup(name);
-
-cp
- if(read_rsa_private_key())
- return -1;
-
- if(read_connection_config(myself->connection))
- {
- syslog(LOG_ERR, _("Cannot open host configuration file for myself!"));
- return -1;
- }
-
- if(read_rsa_public_key(myself->connection))
- return -1;
-cp