Merge "core: Tweak startup order."
[asterisk/asterisk.git] / main / tcptls.c
index 6b040ae..02a2af5 100644 (file)
@@ -26,6 +26,7 @@
  */
 
 /*** MODULEINFO
+       <use type="external">openssl</use>
        <support_level>core</support_level>
  ***/
 
 #endif
 
 #include <signal.h>
+#include <sys/stat.h>
 
 #include "asterisk/compat.h"
 #include "asterisk/tcptls.h"
+#include "asterisk/io.h"
 #include "asterisk/http.h"
 #include "asterisk/utils.h"
 #include "asterisk/strings.h"
@@ -46,6 +49,7 @@
 #include "asterisk/manager.h"
 #include "asterisk/astobj2.h"
 #include "asterisk/pbx.h"
+#include "asterisk/app.h"
 
 static void session_instance_destructor(void *obj)
 {
@@ -219,39 +223,50 @@ void *ast_tcptls_server_root(void *data)
        pthread_t launched;
 
        for (;;) {
-               int i, flags;
+               int i;
 
                if (desc->periodic_fn) {
                        desc->periodic_fn(desc);
                }
                i = ast_wait_for_input(desc->accept_fd, desc->poll_timeout);
                if (i <= 0) {
+                       /* Prevent tight loop from hogging CPU */
+                       usleep(1);
                        continue;
                }
                fd = ast_accept(desc->accept_fd, &addr);
                if (fd < 0) {
-                       if ((errno != EAGAIN) && (errno != EWOULDBLOCK) && (errno != EINTR) && (errno != ECONNABORTED)) {
-                               ast_log(LOG_ERROR, "Accept failed: %s\n", strerror(errno));
-                               break;
+                       if (errno != EAGAIN
+                               && errno != EWOULDBLOCK
+                               && errno != EINTR
+                               && errno != ECONNABORTED) {
+                               ast_log(LOG_ERROR, "TCP/TLS accept failed: %s\n", strerror(errno));
+                               if (errno != EMFILE) {
+                                       break;
+                               }
                        }
+                       /* Prevent tight loop from hogging CPU */
+                       usleep(1);
                        continue;
                }
                tcptls_session = ao2_alloc(sizeof(*tcptls_session), session_instance_destructor);
                if (!tcptls_session) {
-                       ast_log(LOG_WARNING, "No memory for new session: %s\n", strerror(errno));
-                       if (close(fd)) {
-                               ast_log(LOG_ERROR, "close() failed: %s\n", strerror(errno));
-                       }
+                       close(fd);
                        continue;
                }
 
                tcptls_session->overflow_buf = ast_str_create(128);
-               flags = fcntl(fd, F_GETFL);
-               fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);
+               if (!tcptls_session->overflow_buf) {
+                       ao2_ref(tcptls_session, -1);
+                       close(fd);
+                       continue;
+               }
+               ast_fd_clear_flags(fd, O_NONBLOCK);
 
                tcptls_session->stream = ast_iostream_from_fd(&fd);
                if (!tcptls_session->stream) {
-                       ast_log(LOG_WARNING, "No memory for new session iostream\n");
+                       ao2_ref(tcptls_session, -1);
+                       close(fd);
                        continue;
                }
 
@@ -262,11 +277,20 @@ void *ast_tcptls_server_root(void *data)
 
                /* This thread is now the only place that controls the single ref to tcptls_session */
                if (ast_pthread_create_detached_background(&launched, NULL, handle_tcptls_connection, tcptls_session)) {
-                       ast_log(LOG_ERROR, "Unable to launch helper thread: %s\n", strerror(errno));
-                       ast_tcptls_close_session_file(tcptls_session);
+                       ast_log(LOG_ERROR, "TCP/TLS unable to launch helper thread: %s\n",
+                               strerror(errno));
                        ao2_ref(tcptls_session, -1);
                }
        }
+
+       ast_log(LOG_ERROR, "TCP/TLS listener thread ended abnormally\n");
+
+       /* Close the listener socket so Asterisk doesn't appear dead. */
+       fd = desc->accept_fd;
+       desc->accept_fd = -1;
+       if (0 <= fd) {
+               close(fd);
+       }
        return NULL;
 }
 
@@ -291,7 +315,10 @@ static void __ssl_setup_certs(struct ast_tls_config *cfg, const size_t cert_file
 static int __ssl_setup(struct ast_tls_config *cfg, int client)
 {
 #ifndef DO_SSL
-       cfg->enabled = 0;
+       if (cfg->enabled) {
+               ast_log(LOG_NOTICE, "Configured without OpenSSL Development Headers");
+               cfg->enabled = 0;
+       }
        return 0;
 #else
        int disable_ssl = 0;
@@ -322,12 +349,16 @@ static int __ssl_setup(struct ast_tls_config *cfg, int client)
                        cfg->ssl_ctx = SSL_CTX_new(SSLv3_client_method());
                } else
 #endif
+#if defined(OPENSSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER  >= 0x10100000L)
+               cfg->ssl_ctx = SSL_CTX_new(TLS_client_method());
+#else
                if (ast_test_flag(&cfg->flags, AST_SSL_TLSV1_CLIENT)) {
                        cfg->ssl_ctx = SSL_CTX_new(TLSv1_client_method());
                } else {
                        disable_ssl = 1;
                        cfg->ssl_ctx = SSL_CTX_new(SSLv23_client_method());
                }
+#endif
        } else {
                disable_ssl = 1;
                cfg->ssl_ctx = SSL_CTX_new(SSLv23_server_method());
@@ -472,7 +503,7 @@ int ast_ssl_setup(struct ast_tls_config *cfg)
 void ast_ssl_teardown(struct ast_tls_config *cfg)
 {
 #ifdef DO_SSL
-       if (cfg->ssl_ctx) {
+       if (cfg && cfg->ssl_ctx) {
                SSL_CTX_free(cfg->ssl_ctx);
                cfg->ssl_ctx = NULL;
        }
@@ -482,7 +513,6 @@ void ast_ssl_teardown(struct ast_tls_config *cfg)
 struct ast_tcptls_session_instance *ast_tcptls_client_start(struct ast_tcptls_session_instance *tcptls_session)
 {
        struct ast_tcptls_session_args *desc;
-       int flags;
 
        if (!(desc = tcptls_session->parent)) {
                goto client_start_error;
@@ -496,8 +526,7 @@ struct ast_tcptls_session_instance *ast_tcptls_client_start(struct ast_tcptls_se
                goto client_start_error;
        }
 
-       flags = fcntl(desc->accept_fd, F_GETFL);
-       fcntl(desc->accept_fd, F_SETFL, flags & ~O_NONBLOCK);
+       ast_fd_clear_flags(desc->accept_fd, O_NONBLOCK);
 
        if (desc->tls_cfg) {
                desc->tls_cfg->enabled = 1;
@@ -544,7 +573,8 @@ struct ast_tcptls_session_instance *ast_tcptls_client_create(struct ast_tcptls_s
 
        /* if a local address was specified, bind to it so the connection will
           originate from the desired address */
-       if (!ast_sockaddr_isnull(&desc->local_address)) {
+       if (!ast_sockaddr_isnull(&desc->local_address) &&
+           !ast_sockaddr_is_any(&desc->local_address)) {
                setsockopt(desc->accept_fd, SOL_SOCKET, SO_REUSEADDR, &x, sizeof(x));
                if (ast_bind(desc->accept_fd, &desc->local_address)) {
                        ast_log(LOG_ERROR, "Unable to bind %s to %s: %s\n",
@@ -555,11 +585,15 @@ struct ast_tcptls_session_instance *ast_tcptls_client_create(struct ast_tcptls_s
                }
        }
 
-       if (!(tcptls_session = ao2_alloc(sizeof(*tcptls_session), session_instance_destructor))) {
+       tcptls_session = ao2_alloc(sizeof(*tcptls_session), session_instance_destructor);
+       if (!tcptls_session) {
                goto error;
        }
 
        tcptls_session->overflow_buf = ast_str_create(128);
+       if (!tcptls_session->overflow_buf) {
+               goto error;
+       }
        tcptls_session->client = 1;
        tcptls_session->stream = ast_iostream_from_fd(&fd);
        if (!tcptls_session->stream) {
@@ -578,19 +612,72 @@ struct ast_tcptls_session_instance *ast_tcptls_client_create(struct ast_tcptls_s
 error:
        close(desc->accept_fd);
        desc->accept_fd = -1;
-       if (tcptls_session) {
-               ao2_ref(tcptls_session, -1);
-       }
+       ao2_cleanup(tcptls_session);
        return NULL;
 }
 
 void ast_tcptls_server_start(struct ast_tcptls_session_args *desc)
 {
-       int flags;
        int x = 1;
+       int tls_changed = 0;
+       int sd_socket;
+
+       if (desc->tls_cfg) {
+               char hash[41];
+               char *str = NULL;
+               struct stat st;
+
+               /* Store the hashes of the TLS certificate etc. */
+               if (stat(desc->tls_cfg->certfile, &st) || NULL == (str = ast_read_textfile(desc->tls_cfg->certfile))) {
+                       memset(hash, 0, 41);
+               } else {
+                       ast_sha1_hash(hash, str);
+               }
+               ast_free(str);
+               str = NULL;
+               memcpy(desc->tls_cfg->certhash, hash, 41);
+               if (stat(desc->tls_cfg->pvtfile, &st) || NULL == (str = ast_read_textfile(desc->tls_cfg->pvtfile))) {
+                       memset(hash, 0, 41);
+               } else {
+                       ast_sha1_hash(hash, str);
+               }
+               ast_free(str);
+               str = NULL;
+               memcpy(desc->tls_cfg->pvthash, hash, 41);
+               if (stat(desc->tls_cfg->cafile, &st) || NULL == (str = ast_read_textfile(desc->tls_cfg->cafile))) {
+                       memset(hash, 0, 41);
+               } else {
+                       ast_sha1_hash(hash, str);
+               }
+               ast_free(str);
+               str = NULL;
+               memcpy(desc->tls_cfg->cahash, hash, 41);
+
+               /* Check whether TLS configuration has changed */
+               if (!desc->old_tls_cfg) { /* No previous configuration */
+                       tls_changed = 1;
+                       desc->old_tls_cfg = ast_calloc(1, sizeof(*desc->old_tls_cfg));
+               } else if (memcmp(desc->tls_cfg->certhash, desc->old_tls_cfg->certhash, 41)) {
+                       tls_changed = 1;
+               } else if (memcmp(desc->tls_cfg->pvthash, desc->old_tls_cfg->pvthash, 41)) {
+                       tls_changed = 1;
+               } else if (strcmp(desc->tls_cfg->cipher, desc->old_tls_cfg->cipher)) {
+                       tls_changed = 1;
+               } else if (memcmp(desc->tls_cfg->cahash, desc->old_tls_cfg->cahash, 41)) {
+                       tls_changed = 1;
+               } else if (strcmp(desc->tls_cfg->capath, desc->old_tls_cfg->capath)) {
+                       tls_changed = 1;
+               } else if (memcmp(&desc->tls_cfg->flags, &desc->old_tls_cfg->flags, sizeof(desc->tls_cfg->flags))) {
+                       tls_changed = 1;
+               }
+
+               if (tls_changed) {
+                       ast_debug(1, "Changed parameters for %s found\n", desc->name);
+               }
+       }
 
        /* Do nothing if nothing has changed */
-       if (!ast_sockaddr_cmp(&desc->old_address, &desc->local_address)) {
+       if (!tls_changed && !ast_sockaddr_cmp(&desc->old_address, &desc->local_address)) {
                ast_debug(1, "Nothing changed in %s\n", desc->name);
                return;
        }
@@ -605,6 +692,19 @@ void ast_tcptls_server_start(struct ast_tcptls_session_args *desc)
                pthread_join(desc->master, NULL);
        }
 
+       sd_socket = ast_sd_get_fd(SOCK_STREAM, &desc->local_address);
+
+       if (sd_socket != -1) {
+               if (desc->accept_fd != sd_socket) {
+                       if (desc->accept_fd != -1) {
+                               close(desc->accept_fd);
+                       }
+                       desc->accept_fd = sd_socket;
+               }
+
+               goto systemd_socket_activation;
+       }
+
        if (desc->accept_fd != -1) {
                close(desc->accept_fd);
        }
@@ -634,8 +734,9 @@ void ast_tcptls_server_start(struct ast_tcptls_session_args *desc)
                ast_log(LOG_ERROR, "Unable to listen for %s!\n", desc->name);
                goto error;
        }
-       flags = fcntl(desc->accept_fd, F_GETFL);
-       fcntl(desc->accept_fd, F_SETFL, flags | O_NONBLOCK);
+
+systemd_socket_activation:
+       ast_fd_set_flags(desc->accept_fd, O_NONBLOCK);
        if (ast_pthread_create_background(&desc->master, NULL, desc->accept_fn, desc)) {
                ast_log(LOG_ERROR, "Unable to launch thread for %s on %s: %s\n",
                        desc->name,
@@ -646,6 +747,22 @@ void ast_tcptls_server_start(struct ast_tcptls_session_args *desc)
 
        /* Set current info */
        ast_sockaddr_copy(&desc->old_address, &desc->local_address);
+       if (desc->old_tls_cfg) {
+               ast_free(desc->old_tls_cfg->certfile);
+               ast_free(desc->old_tls_cfg->pvtfile);
+               ast_free(desc->old_tls_cfg->cipher);
+               ast_free(desc->old_tls_cfg->cafile);
+               ast_free(desc->old_tls_cfg->capath);
+               desc->old_tls_cfg->certfile = ast_strdup(desc->tls_cfg->certfile);
+               desc->old_tls_cfg->pvtfile = ast_strdup(desc->tls_cfg->pvtfile);
+               desc->old_tls_cfg->cipher = ast_strdup(desc->tls_cfg->cipher);
+               desc->old_tls_cfg->cafile = ast_strdup(desc->tls_cfg->cafile);
+               desc->old_tls_cfg->capath = ast_strdup(desc->tls_cfg->capath);
+               memcpy(desc->old_tls_cfg->certhash, desc->tls_cfg->certhash, 41);
+               memcpy(desc->old_tls_cfg->pvthash, desc->tls_cfg->pvthash, 41);
+               memcpy(desc->old_tls_cfg->cahash, desc->tls_cfg->cahash, 41);
+               memcpy(&desc->old_tls_cfg->flags, &desc->tls_cfg->flags, sizeof(desc->old_tls_cfg->flags));
+       }
 
        return;
 
@@ -660,7 +777,7 @@ void ast_tcptls_close_session_file(struct ast_tcptls_session_instance *tcptls_se
                ast_iostream_close(tcptls_session->stream);
                tcptls_session->stream = NULL;
        } else {
-               ast_log(LOG_ERROR, "ast_tcptls_close_session_file invoked on session instance without file or file descriptor\n");
+               ast_debug(1, "ast_tcptls_close_session_file invoked on session instance without file or file descriptor\n");
        }
 }
 
@@ -676,6 +793,17 @@ void ast_tcptls_server_stop(struct ast_tcptls_session_args *desc)
                close(desc->accept_fd);
        }
        desc->accept_fd = -1;
+
+       if (desc->old_tls_cfg) {
+               ast_free(desc->old_tls_cfg->certfile);
+               ast_free(desc->old_tls_cfg->pvtfile);
+               ast_free(desc->old_tls_cfg->cipher);
+               ast_free(desc->old_tls_cfg->cafile);
+               ast_free(desc->old_tls_cfg->capath);
+               ast_free(desc->old_tls_cfg);
+               desc->old_tls_cfg = NULL;
+       }
+
        ast_debug(2, "Stopped server :: %s\n", desc->name);
 }