X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=src%2Fnet_setup.c;h=a9947aad1a9eca0205afc82b4b910db0657eb457;hb=9b9230a0a79c670b86f54fadd2807b864ff9d91f;hp=0e424eef5594dc0db10148c68f8c158b6e3c9be3;hpb=f5bb64b36ae0807cdd3f241f81a8e933065437f6;p=meshlink diff --git a/src/net_setup.c b/src/net_setup.c index 0e424eef..a9947aad 100644 --- a/src/net_setup.c +++ b/src/net_setup.c @@ -1,7 +1,7 @@ /* net_setup.c -- Setup. Copyright (C) 1998-2005 Ivo Timmermans, - 2000-2012 Guus Sliepen + 2000-2013 Guus Sliepen 2006 Scott Lamb 2010 Brandon Black @@ -52,19 +52,19 @@ char *proxyuser; char *proxypass; proxytype_t proxytype; int autoconnect; +bool disablebuggypeers; char *scriptinterpreter; char *scriptextension; bool node_read_ecdsa_public_key(node_t *n) { - if(ecdsa_active(&n->ecdsa)) + if(ecdsa_active(n->ecdsa)) return true; splay_tree_t *config_tree; FILE *fp; char *pubname = NULL, *hcfname = NULL; char *p; - bool result = false; xasprintf(&hcfname, "%s" SLASH "hosts" SLASH "%s", confbase, n->name); @@ -75,7 +75,7 @@ bool node_read_ecdsa_public_key(node_t *n) { /* First, check for simple ECDSAPublicKey statement */ if(get_config_string(lookup_config(config_tree, "ECDSAPublicKey"), &p)) { - result = ecdsa_set_base64_public_key(&n->ecdsa, p); + n->ecdsa = ecdsa_set_base64_public_key(p); free(p); goto exit; } @@ -92,28 +92,30 @@ bool node_read_ecdsa_public_key(node_t *n) { goto exit; } - result = ecdsa_read_pem_public_key(&n->ecdsa, fp); + n->ecdsa = ecdsa_read_pem_public_key(fp); fclose(fp); exit: exit_configuration(&config_tree); free(hcfname); free(pubname); - return result; + return n->ecdsa; } bool read_ecdsa_public_key(connection_t *c) { + if(ecdsa_active(c->ecdsa)) + return true; + FILE *fp; char *fname; char *p; - bool result; /* First, check for simple ECDSAPublicKey statement */ if(get_config_string(lookup_config(c->config_tree, "ECDSAPublicKey"), &p)) { - result = ecdsa_set_base64_public_key(&c->ecdsa, p); + c->ecdsa = ecdsa_set_base64_public_key(p); free(p); - return result; + return c->ecdsa; } /* Else, check for ECDSAPublicKeyFile statement and read it */ @@ -130,27 +132,29 @@ bool read_ecdsa_public_key(connection_t *c) { return false; } - result = ecdsa_read_pem_public_key(&c->ecdsa, fp); + c->ecdsa = ecdsa_read_pem_public_key(fp); fclose(fp); - if(!result) + if(!c->ecdsa) logger(DEBUG_ALWAYS, LOG_ERR, "Parsing ECDSA public key file `%s' failed.", fname); free(fname); - return result; + return c->ecdsa; } bool read_rsa_public_key(connection_t *c) { + if(ecdsa_active(c->ecdsa)) + return true; + FILE *fp; char *fname; char *n; - bool result; /* First, check for simple PublicKey statement */ if(get_config_string(lookup_config(c->config_tree, "PublicKey"), &n)) { - result = rsa_set_hex_public_key(&c->rsa, n, "FFFF"); + c->rsa = rsa_set_hex_public_key(n, "FFFF"); free(n); - return result; + return c->rsa; } /* Else, check for PublicKeyFile statement and read it */ @@ -166,19 +170,18 @@ bool read_rsa_public_key(connection_t *c) { return false; } - result = rsa_read_pem_public_key(&c->rsa, fp); + c->rsa = rsa_read_pem_public_key(fp); fclose(fp); - if(!result) + if(!c->rsa) logger(DEBUG_ALWAYS, LOG_ERR, "Reading RSA public key file `%s' failed: %s", fname, strerror(errno)); free(fname); - return result; + return c->rsa; } static bool read_ecdsa_private_key(void) { FILE *fp; char *fname; - bool result; /* Check for PrivateKeyFile statement and read it */ @@ -206,20 +209,19 @@ static bool read_ecdsa_private_key(void) { logger(DEBUG_ALWAYS, LOG_WARNING, "Warning: insecure file permissions for ECDSA private key file `%s'!", fname); #endif - result = ecdsa_read_pem_private_key(&myself->connection->ecdsa, fp); + myself->connection->ecdsa = ecdsa_read_pem_private_key(fp); fclose(fp); - if(!result) + if(!myself->connection->ecdsa) logger(DEBUG_ALWAYS, LOG_ERR, "Reading ECDSA private key file `%s' failed: %s", fname, strerror(errno)); free(fname); - return result; + return myself->connection->ecdsa; } static bool read_rsa_private_key(void) { FILE *fp; char *fname; char *n, *d; - bool result; /* First, check for simple PrivateKey statement */ @@ -229,10 +231,10 @@ static bool read_rsa_private_key(void) { free(d); return false; } - result = rsa_set_hex_private_key(&myself->connection->rsa, n, "FFFF", d); + myself->connection->rsa = rsa_set_hex_private_key(n, "FFFF", d); free(n); free(d); - return result; + return myself->connection->rsa; } /* Else, check for PrivateKeyFile statement and read it */ @@ -262,13 +264,13 @@ static bool read_rsa_private_key(void) { logger(DEBUG_ALWAYS, LOG_WARNING, "Warning: insecure file permissions for RSA private key file `%s'!", fname); #endif - result = rsa_read_pem_private_key(&myself->connection->rsa, fp); + myself->connection->rsa = rsa_read_pem_private_key(fp); fclose(fp); - if(!result) + if(!myself->connection->rsa) logger(DEBUG_ALWAYS, LOG_ERR, "Reading RSA private key file `%s' failed: %s", fname, strerror(errno)); free(fname); - return result; + return myself->connection->rsa; } static timeout_t keyexpire_timeout; @@ -598,6 +600,8 @@ bool setup_myself_reloadable(void) { get_config_int(lookup_config(config_tree, "AutoConnect"), &autoconnect); + get_config_bool(lookup_config(config_tree, "DisableBuggyPeers"), &disablebuggypeers); + return true; } @@ -704,7 +708,7 @@ static bool setup_myself(void) { if(!get_config_string(lookup_config(config_tree, "Cipher"), &cipher)) cipher = xstrdup("blowfish"); - if(!cipher_open_by_name(&myself->incipher, cipher)) { + if(!(myself->incipher = cipher_open_by_name(cipher))) { logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized cipher type!"); return false; } @@ -727,7 +731,7 @@ static bool setup_myself(void) { if(!get_config_string(lookup_config(config_tree, "Digest"), &digest)) digest = xstrdup("sha1"); - if(!digest_open_by_name(&myself->indigest, digest, maclength)) { + if(!(myself->indigest = digest_open_by_name(digest, maclength))) { logger(DEBUG_ALWAYS, LOG_ERR, "Unrecognized digest type!"); return false; } @@ -751,7 +755,7 @@ static bool setup_myself(void) { myself->nexthop = myself; myself->via = myself; myself->status.reachable = true; - myself->last_state_change = time(NULL); + myself->last_state_change = now.tv_sec; myself->status.sptps = experimental; node_add(myself); @@ -808,6 +812,37 @@ static bool setup_myself(void) { /* Open sockets */ +#ifndef HAVE_MINGW + int unix_fd = socket(AF_UNIX, SOCK_STREAM, 0); + if(unix_fd < 0) { + logger(DEBUG_ALWAYS, LOG_ERR, "Could not create UNIX socket: %s", sockstrerror(errno)); + return false; + } + + struct sockaddr_un sa; + sa.sun_family = AF_UNIX; + strncpy(sa.sun_path, unixsocketname, sizeof sa.sun_path); + + if(connect(unix_fd, (struct sockaddr *)&sa, sizeof sa) >= 0) { + logger(DEBUG_ALWAYS, LOG_ERR, "UNIX socket %s is still in use!", unixsocketname); + return false; + } + + unlink(unixsocketname); + + if(bind(unix_fd, (struct sockaddr *)&sa, sizeof sa) < 0) { + logger(DEBUG_ALWAYS, LOG_ERR, "Could not bind UNIX socket to %s: %s", unixsocketname, sockstrerror(errno)); + return false; + } + + if(listen(unix_fd, 3) < 0) { + logger(DEBUG_ALWAYS, LOG_ERR, "Could not listen on UNIX socket %s: %s", unixsocketname, sockstrerror(errno)); + return false; + } + + io_add(&unix_socket, handle_new_unix_connection, &unix_socket, unix_fd, IO_READ); +#endif + if(!do_detach && getenv("LISTEN_FDS")) { sockaddr_t sa; socklen_t salen; @@ -927,7 +962,7 @@ static bool setup_myself(void) { return false; } - last_config_check = time(NULL); + last_config_check = now.tv_sec; return true; } @@ -992,6 +1027,11 @@ void close_network_connections(void) { close(listen_socket[i].udp.fd); } +#ifndef HAVE_MINGW + io_del(&unix_socket); + close(unix_socket.fd); +#endif + char *envp[5]; xasprintf(&envp[0], "NETNAME=%s", netname ? : ""); xasprintf(&envp[1], "DEVICE=%s", device ? : "");