+ /* Make sure we don't make an outgoing connection to a host that is already in our connection list */
+
+ if((old = lookup_id(name)))
+ {
+ if(!old->status.outgoing)
+ {
+ if(debug_lvl >= DEBUG_CONNECTIONS)
+ syslog(LOG_NOTICE, _("We are already connected to %s."), name);
+
+ old->status.outgoing = 1;
+ }
+ return 0;
+ }
+
+ ncn = new_connection();
+ asprintf(&ncn->name, "%s", name);
+
+ if(read_host_config(ncn))
+ {
+ syslog(LOG_ERR, _("Error reading host configuration file for %s"), ncn->name);
+ free_connection(ncn);
+ return -1;
+ }
+
+ if(!(cfg = get_config_val(ncn->config, config_address)))
+ {
+ syslog(LOG_ERR, _("No address specified for %s"), ncn->name);
+ free_connection(ncn);
+ return -1;
+ }
+
+ if(!(h = gethostbyname(cfg->data.ptr)))
+ {
+ syslog(LOG_ERR, _("Error looking up `%s': %m"), cfg->data.ptr);
+ free_connection(ncn);
+ return -1;
+ }
+
+ ncn->address = ntohl(*((ipv4_t*)(h->h_addr_list[0])));
+ ncn->hostname = hostlookup(htonl(ncn->address));
+
+ if(setup_outgoing_meta_socket(ncn) < 0)
+ {
+ syslog(LOG_ERR, _("Could not set up a meta connection to %s"),
+ ncn->hostname);
+ free_connection(ncn);
+ return -1;
+ }
+
+ ncn->status.outgoing = 1;
+ ncn->buffer = xmalloc(MAXBUFSIZE);
+ ncn->buflen = 0;
+ ncn->last_ping_time = time(NULL);
+
+ connection_add(ncn);
+
+ send_id(ncn);
+cp
+ return 0;
+}
+
+int read_rsa_public_key(connection_t *cl)
+{
+ config_t const *cfg;
+ FILE *fp;
+ char *fname;
+ void *result;
+cp
+ if(!cl->rsa_key)
+ cl->rsa_key = RSA_new();
+
+ /* First, check for simple PublicKey statement */
+
+ if((cfg = get_config_val(cl->config, config_publickey)))
+ {
+ BN_hex2bn(&cl->rsa_key->n, cfg->data.ptr);
+ BN_hex2bn(&cl->rsa_key->e, "FFFF");
+ return 0;
+ }
+
+ /* Else, check for PublicKeyFile statement and read it */
+
+ if((cfg = get_config_val(cl->config, config_publickeyfile)))
+ {
+ if(is_safe_path(cfg->data.ptr))
+ {
+ if((fp = fopen(cfg->data.ptr, "r")) == NULL)
+ {
+ syslog(LOG_ERR, _("Error reading RSA public key file `%s': %m"),
+ cfg->data.ptr);
+ return -1;
+ }
+ result = PEM_read_RSAPublicKey(fp, &cl->rsa_key, NULL, NULL);
+ fclose(fp);
+ if(!result)
+ {
+ syslog(LOG_ERR, _("Reading RSA public key file `%s' failed: %m"),
+ cfg->data.ptr);
+ return -1;
+ }
+ return 0;
+ }
+ else
+ return -1;
+ }
+
+ /* Else, check if a harnessed public key is in the config file */
+
+ asprintf(&fname, "%s/hosts/%s", confbase, cl->name);
+ if((fp = fopen(fname, "r")))
+ {
+ result = PEM_read_RSAPublicKey(fp, &cl->rsa_key, NULL, NULL);
+ fclose(fp);
+ free(fname);
+ if(result)
+ return 0;
+ }
+
+ free(fname);
+
+ /* Nothing worked. */
+
+ syslog(LOG_ERR, _("No public key for %s specified!"), cl->name);
+cp
+ return -1;
+}
+
+int read_rsa_private_key(void)
+{
+ config_t const *cfg;
+ FILE *fp;
+ void *result;
+cp
+ if(!myself->rsa_key)
+ myself->rsa_key = RSA_new();
+
+ if((cfg = get_config_val(config, config_privatekey)))