Line data Source code
1 : /* 2 : * Copyright (C) 2004-2025 Savoir-faire Linux Inc. 3 : * 4 : * This program is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 3 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * This program is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with this program. If not, see <https://www.gnu.org/licenses/>. 16 : */ 17 : #include <asio.hpp> 18 : #include <string> 19 : #include <vector> 20 : #include <map> 21 : #include <cstdlib> 22 : 23 : #ifdef HAVE_CONFIG_H 24 : #include "config.h" 25 : #endif 26 : 27 : #include "manager.h" 28 : #include "logger.h" 29 : #include "jami.h" 30 : #include "callmanager_interface.h" 31 : #include "configurationmanager_interface.h" 32 : #include "presencemanager_interface.h" 33 : #include "client/ring_signal.h" 34 : 35 : #ifdef ENABLE_VIDEO 36 : #include "client/videomanager.h" 37 : #endif // ENABLE_VIDEO 38 : 39 : namespace libjami { 40 : 41 : InitFlag initFlags = {}; 42 : 43 : bool 44 307 : init(enum InitFlag flags) noexcept 45 : { 46 307 : initFlags = flags; 47 307 : jami::Logger::setDebugMode(LIBJAMI_FLAG_DEBUG == (flags & LIBJAMI_FLAG_DEBUG)); 48 307 : jami::Logger::setSysLog(LIBJAMI_FLAG_SYSLOG == (flags & LIBJAMI_FLAG_SYSLOG)); 49 307 : jami::Logger::setConsoleLog(LIBJAMI_FLAG_CONSOLE_LOG == (flags & LIBJAMI_FLAG_CONSOLE_LOG)); 50 : 51 307 : const char* log_file = getenv("JAMI_LOG_FILE"); 52 : 53 307 : if (log_file) { 54 0 : jami::Logger::setFileLog(log_file); 55 : } 56 : 57 : // Following function create a local static variable inside 58 : // This var must have the same live as Manager. 59 : // So we call it now to create this var. 60 307 : jami::getSignalHandlers(); 61 : 62 : try { 63 : // current implementation use static variable 64 307 : auto& manager = jami::Manager::instance(); 65 307 : manager.setAutoAnswer(flags & LIBJAMI_FLAG_AUTOANSWER); 66 : 67 : #if TARGET_OS_IOS 68 : if (flags & LIBJAMI_FLAG_IOS_EXTENSION) 69 : manager.isIOSExtension = true; 70 : #endif 71 307 : if (flags & LIBJAMI_FLAG_NO_AUTOSYNC) 72 0 : manager.syncOnRegister = false; 73 0 : } catch (...) { 74 0 : return false; 75 0 : } 76 : 77 : #ifdef __linux__ 78 : // HACK: ignore system-wide GnuTLS configuration 79 : // 80 : // Since version 3.6.9, GnuTLS makes it possible to selectively disable algorithms 81 : // and protocols via a global configuration file. In particular, RSA PKCS1 v1.5 82 : // encryption can be disabled by setting the "allow-rsa-pkcs1-encrypt" option to 83 : // false. Doing this makes OpenDHT's putEncrypted function fail systematically and 84 : // therefore breaks several major features in Jami (e.g. sending contact requests). 85 : // As of December 2025, this is an issue on AlmaLinux 10 (there are other distributions 86 : // supported by Jami, including Ubuntu and Fedora, that include a system-wide 87 : // configuration file for GnuTLS, but for now they don't disable RSA PKCS1 v1.5). 88 : // 89 : // Some of the options in the configuration file can be bypassed by calling the right 90 : // function, but GnuTLS currently does not allow this in the case of RSA PKCS1 v1.5. 91 : // As a workaround, we take advantage of the fact that the location of the configuration 92 : // file can be changed at runtime via the GNUTLS_SYSTEM_PRIORITY_FILE environment 93 : // variable. 94 : static bool gnutlsInitialized = false; 95 307 : if (!gnutlsInitialized) { 96 38 : setenv("GNUTLS_SYSTEM_PRIORITY_FILE", "/dev/null", 1); 97 : // GnuTLS has already been initialized (in a library constructor) by the time we set 98 : // GNUTLS_SYSTEM_PRIORITY_FILE, so we need to reinitialize it in order for the new 99 : // value to be taken into account. 100 38 : gnutls_global_deinit(); 101 38 : if (gnutls_global_init() < 0) { 102 0 : JAMI_ERROR("Failed to intialize gnutls"); 103 0 : return false; 104 : } 105 38 : gnutlsInitialized = true; 106 : } 107 : #endif 108 : 109 307 : return true; 110 : } 111 : 112 : bool 113 32 : start(const std::filesystem::path& config_file) noexcept 114 : { 115 : try { 116 32 : jami::Manager::instance().init(config_file, initFlags); 117 0 : } catch (...) { 118 0 : return false; 119 0 : } 120 32 : return true; 121 : } 122 : 123 : bool 124 0 : initialized() noexcept 125 : { 126 0 : return jami::Manager::initialized; 127 : } 128 : 129 : void 130 307 : fini() noexcept 131 : { 132 307 : jami::Manager::instance().finish(); 133 307 : jami::Logger::fini(); 134 307 : } 135 : 136 : void 137 0 : logging(const std::string& whom, const std::string& action) noexcept 138 : { 139 0 : if ("syslog" == whom) { 140 0 : jami::Logger::setSysLog(not action.empty()); 141 0 : } else if ("console" == whom) { 142 0 : jami::Logger::setConsoleLog(not action.empty()); 143 0 : } else if ("monitor" == whom) { 144 0 : jami::Logger::setMonitorLog(not action.empty()); 145 0 : } else if ("file" == whom) { 146 0 : jami::Logger::setFileLog(action); 147 : } else { 148 0 : JAMI_ERR("Bad log handler %s", whom.c_str()); 149 : } 150 0 : } 151 : 152 : void 153 0 : CallbackWrapperBase::post(std::function<void()> cb) 154 : { 155 0 : if (auto io = jami::Manager::instance().ioContext()) 156 0 : asio::post(*io, std::move(cb)); 157 0 : } 158 : 159 : } // namespace libjami