loader: Process dependencies for built-in modules.
[asterisk/asterisk.git] / main / tcptls.c
index 7f1421d..3ba52ff 100644 (file)
  * \author Brett Bryant <brettbryant@gmail.com>
  */
 
-/*** MODULEINFO
-       <support_level>core</support_level>
- ***/
-
 #include "asterisk.h"
 
-#ifdef HAVE_FCNTL_H
-#include <fcntl.h>
-#endif
-
-#include <signal.h>
-#include <sys/stat.h>
+#include "asterisk/tcptls.h"            /* for ast_tls_config, ast_tcptls_se... */
+#include "asterisk/iostream.h"          /* for DO_SSL, ast_iostream_close, a... */
 
-#include "asterisk/compat.h"
-#include "asterisk/tcptls.h"
-#include "asterisk/http.h"
-#include "asterisk/utils.h"
-#include "asterisk/strings.h"
-#include "asterisk/options.h"
-#include "asterisk/manager.h"
-#include "asterisk/astobj2.h"
-#include "asterisk/pbx.h"
-#include "asterisk/app.h"
+#ifdef HAVE_FCNTL_H
+#include <fcntl.h>                      /* for O_NONBLOCK */
+#endif /* HAVE_FCNTL_H */
+#include <netinet/in.h>                 /* for IPPROTO_TCP */
+#ifdef DO_SSL
+#include <openssl/asn1.h>               /* for ASN1_STRING_to_UTF8 */
+#include <openssl/crypto.h>             /* for OPENSSL_free */
+#include <openssl/opensslconf.h>        /* for OPENSSL_NO_SSL3_METHOD, OPENS... */
+#include <openssl/opensslv.h>           /* for OPENSSL_VERSION_NUMBER */
+#include <openssl/safestack.h>          /* for STACK_OF */
+#include <openssl/ssl.h>                /* for SSL_CTX_free, SSL_get_error, ... */
+#include <openssl/x509.h>               /* for X509_free, X509_NAME_ENTRY_ge... */
+#include <openssl/x509v3.h>             /* for GENERAL_NAME, sk_GENERAL_NAME... */
+#ifndef OPENSSL_NO_DH
+#include <openssl/bio.h>                /* for BIO_free, BIO_new_file */
+#include <openssl/dh.h>                 /* for DH_free */
+#include <openssl/pem.h>                /* for PEM_read_bio_DHparams */
+#endif /* OPENSSL_NO_DH */
+#ifndef OPENSSL_NO_EC
+#include <openssl/ec.h>                 /* for EC_KEY_free, EC_KEY_new_by_cu... */
+#endif /* OPENSSL_NO_EC */
+#endif /* DO_SSL */
+#include <pthread.h>                    /* for pthread_cancel, pthread_join */
+#include <signal.h>                     /* for pthread_kill, SIGURG */
+#include <sys/socket.h>                 /* for setsockopt, shutdown, socket */
+#include <sys/stat.h>                   /* for stat */
+
+#include "asterisk/app.h"               /* for ast_read_textfile */
+#include "asterisk/astobj2.h"           /* for ao2_ref, ao2_t_ref, ao2_alloc */
+#include "asterisk/compat.h"            /* for strcasecmp */
+#include "asterisk/config.h"            /* for ast_parse_arg, ast_parse_flag... */
+#include "asterisk/io.h"                /* for ast_sd_get_fd */
+#include "asterisk/lock.h"              /* for AST_PTHREADT_NULL */
+#include "asterisk/logger.h"            /* for ast_log, LOG_ERROR, ast_debug */
+#include "asterisk/netsock2.h"          /* for ast_sockaddr_copy, ast_sockad... */
+#include "asterisk/pbx.h"               /* for ast_thread_inhibit_escalations */
+#include "asterisk/utils.h"             /* for ast_true, ast_free, ast_wait_... */
 
 static void session_instance_destructor(void *obj)
 {
@@ -115,6 +134,19 @@ static void *handle_tcptls_connection(void *data)
                return NULL;
        }
 
+       /*
+        * TCP/TLS connections are associated with external protocols which can
+        * be considered to be user interfaces (even for SIP messages), and
+        * will not handle channel media.  This may need to be pushed down into
+        * the individual protocol handlers, but this seems like a good start.
+        */
+       if (ast_thread_user_interface_set(1)) {
+               ast_log(LOG_ERROR, "Failed to set user interface status; killing connection\n");
+               ast_tcptls_close_session_file(tcptls_session);
+               ao2_ref(tcptls_session, -1);
+               return NULL;
+       }
+
        if (tcptls_session->parent->tls_cfg) {
 #ifdef DO_SSL
                if (ast_iostream_start_tls(&tcptls_session->stream, tcptls_session->parent->tls_cfg->ssl_ctx, tcptls_session->client) < 0) {
@@ -221,39 +253,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;
                }
 
@@ -264,11 +307,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;
 }
 
@@ -293,7 +345,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;
@@ -318,13 +373,13 @@ static int __ssl_setup(struct ast_tls_config *cfg, int client)
                        cfg->ssl_ctx = SSL_CTX_new(SSLv2_client_method());
                } else
 #endif
-#ifndef OPENSSL_NO_SSL3_METHOD
+#if !defined(OPENSSL_NO_SSL3_METHOD) && !(defined(OPENSSL_API_COMPAT) && (OPENSSL_API_COMPAT >= 0x10100000L))
                if (ast_test_flag(&cfg->flags, AST_SSL_SSLV3_CLIENT)) {
                        ast_log(LOG_WARNING, "Usage of SSLv3 is discouraged due to known vulnerabilities. Please use 'tlsv1' or leave the TLS method unspecified!\n");
                        cfg->ssl_ctx = SSL_CTX_new(SSLv3_client_method());
                } else
 #endif
-#if defined(OPENSSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER  >= 0x10100000L)
+#if OPENSSL_VERSION_NUMBER >= 0x10100000L
                cfg->ssl_ctx = SSL_CTX_new(TLS_client_method());
 #else
                if (ast_test_flag(&cfg->flags, AST_SSL_TLSV1_CLIENT)) {
@@ -360,7 +415,7 @@ static int __ssl_setup(struct ast_tls_config *cfg, int client)
        if (ast_test_flag(&cfg->flags, AST_SSL_DISABLE_TLSV1)) {
                ssl_opts |= SSL_OP_NO_TLSv1;
        }
-#if defined(HAVE_SSL_OP_NO_TLSV1_1) && defined(HAVE_SSL_OP_NO_TLSV1_2)
+#if defined(SSL_OP_NO_TLSv1_1) && defined(SSL_OP_NO_TLSv1_2)
        if (ast_test_flag(&cfg->flags, AST_SSL_DISABLE_TLSV11)) {
                ssl_opts |= SSL_OP_NO_TLSv1_1;
        }
@@ -429,8 +484,7 @@ static int __ssl_setup(struct ast_tls_config *cfg, int client)
                }
        }
 
-#ifdef HAVE_OPENSSL_EC
-
+#ifndef OPENSSL_NO_DH
        if (!ast_strlen_zero(cfg->pvtfile)) {
                BIO *bio = BIO_new_file(cfg->pvtfile, "r");
                if (bio != NULL) {
@@ -446,12 +500,15 @@ static int __ssl_setup(struct ast_tls_config *cfg, int client)
                        BIO_free(bio);
                }
        }
+#endif
+
        #ifndef SSL_CTRL_SET_ECDH_AUTO
                #define SSL_CTRL_SET_ECDH_AUTO 94
        #endif
        /* SSL_CTX_set_ecdh_auto(cfg->ssl_ctx, on); requires OpenSSL 1.0.2 which wraps: */
        if (SSL_CTX_ctrl(cfg->ssl_ctx, SSL_CTRL_SET_ECDH_AUTO, 1, NULL)) {
                ast_verb(2, "TLS/SSL ECDH initialized (automatic), faster PFS ciphers enabled\n");
+#if !defined(OPENSSL_NO_ECDH) && (OPENSSL_VERSION_NUMBER >= 0x10000000L) && (OPENSSL_VERSION_NUMBER < 0x10100000L)
        } else {
                /* enables AES-128 ciphers, to get AES-256 use NID_secp384r1 */
                EC_KEY *ecdh = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
@@ -461,10 +518,9 @@ static int __ssl_setup(struct ast_tls_config *cfg, int client)
                        }
                        EC_KEY_free(ecdh);
                }
+#endif
        }
 
-#endif /* #ifdef HAVE_OPENSSL_EC */
-
        ast_verb(2, "TLS/SSL certificate ok\n");        /* We should log which one that is ok. This message doesn't really make sense in production use */
        return 1;
 #endif
@@ -478,7 +534,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;
        }
@@ -488,7 +544,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;
@@ -502,8 +557,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;
@@ -550,7 +604,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",
@@ -561,11 +616,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) {
@@ -584,17 +643,15 @@ 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];
@@ -666,6 +723,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);
        }
@@ -695,8 +765,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,
@@ -737,7 +808,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");
        }
 }