Ring Daemon 16.0.0
Loading...
Searching...
No Matches
sipaccount_config.cpp
Go to the documentation of this file.
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 "sipaccount_config.h"
18#include "account_const.h"
19#include "account_schema.h"
20#include "config/yamlparser.h"
21
22extern "C" {
23#include <pjlib-util/md5.h>
24}
25
26namespace jami {
27
28namespace Conf {
29constexpr const char* ID_KEY = "id";
30constexpr const char* USERNAME_KEY = "username";
31constexpr const char* BIND_ADDRESS_KEY = "bindAddress";
32constexpr const char* INTERFACE_KEY = "interface";
33constexpr const char* PORT_KEY = "port";
34constexpr const char* PUBLISH_ADDR_KEY = "publishAddr";
35constexpr const char* PUBLISH_PORT_KEY = "publishPort";
36constexpr const char* SAME_AS_LOCAL_KEY = "sameasLocal";
37constexpr const char* DTMF_TYPE_KEY = "dtmfType";
38constexpr const char* SERVICE_ROUTE_KEY = "serviceRoute";
39constexpr const char* ALLOW_IP_AUTO_REWRITE = "allowIPAutoRewrite";
40constexpr const char* PRESENCE_ENABLED_KEY = "presenceEnabled";
41constexpr const char* PRESENCE_PUBLISH_SUPPORTED_KEY = "presencePublishSupported";
42constexpr const char* PRESENCE_SUBSCRIBE_SUPPORTED_KEY = "presenceSubscribeSupported";
43constexpr const char* PRESENCE_STATUS_KEY = "presenceStatus";
44constexpr const char* PRESENCE_NOTE_KEY = "presenceNote";
45constexpr const char* PRESENCE_MODULE_ENABLED_KEY = "presenceModuleEnabled";
46constexpr const char* KEEP_ALIVE_ENABLED = "keepAliveEnabled";
47
48constexpr const char* const TLS_KEY = "tls";
49constexpr const char* CERTIFICATE_KEY = "certificate";
50constexpr const char* CALIST_KEY = "calist";
51constexpr const char* TLS_PORT_KEY = "tlsPort";
52constexpr const char* CIPHERS_KEY = "ciphers";
53constexpr const char* TLS_ENABLE_KEY = "enable";
54constexpr const char* METHOD_KEY = "method";
55constexpr const char* TIMEOUT_KEY = "timeout";
56constexpr const char* TLS_PASSWORD_KEY = "password";
57constexpr const char* PRIVATE_KEY_KEY = "privateKey";
58constexpr const char* REQUIRE_CERTIF_KEY = "requireCertif";
59constexpr const char* SERVER_KEY = "server";
60constexpr const char* VERIFY_CLIENT_KEY = "verifyClient";
61constexpr const char* VERIFY_SERVER_KEY = "verifyServer";
62constexpr const char* DISABLE_SECURE_DLG_CHECK = "disableSecureDlgCheck";
63
64constexpr const char* STUN_ENABLED_KEY = "stunEnabled";
65constexpr const char* STUN_SERVER_KEY = "stunServer";
66constexpr const char* CRED_KEY = "credential";
67constexpr const char* SRTP_KEY = "srtp";
68constexpr const char* KEY_EXCHANGE_KEY = "keyExchange";
69constexpr const char* RTP_FALLBACK_KEY = "rtpFallback";
70} // namespace Conf
71
73static constexpr unsigned MIN_REGISTRATION_TIME = 60; // seconds
74
77
78void
79SipAccountConfig::serialize(YAML::Emitter& out) const
80{
81 out << YAML::BeginMap;
82 out << YAML::Key << Conf::ID_KEY << YAML::Value << id;
84
85 out << YAML::Key << Conf::BIND_ADDRESS_KEY << YAML::Value << bindAddress;
86 out << YAML::Key << Conf::PORT_KEY << YAML::Value << localPort;
87 out << YAML::Key << Conf::PUBLISH_PORT_KEY << YAML::Value << publishedPort;
88
89 out << YAML::Key << Conf::USERNAME_KEY << YAML::Value << username;
90
91 // each credential is a map, and we can have multiple credentials
92 out << YAML::Key << Conf::CRED_KEY << YAML::Value << getCredentials();
93
94 out << YAML::Key << Conf::KEEP_ALIVE_ENABLED << YAML::Value << registrationRefreshEnabled;
95
96 //out << YAML::Key << PRESENCE_MODULE_ENABLED_KEY << YAML::Value
97 // << (presence_ and presence_->isEnabled());
98
99 out << YAML::Key << Conf::CONFIG_ACCOUNT_REGISTRATION_EXPIRE << YAML::Value
101 out << YAML::Key << Conf::SERVICE_ROUTE_KEY << YAML::Value << serviceRoute;
102 out << YAML::Key << Conf::ALLOW_IP_AUTO_REWRITE << YAML::Value << allowIPAutoRewrite;
103 out << YAML::Key << Conf::STUN_ENABLED_KEY << YAML::Value << stunEnabled;
104 out << YAML::Key << Conf::STUN_SERVER_KEY << YAML::Value << stunServer;
105
106 // tls submap
107 out << YAML::Key << Conf::TLS_KEY << YAML::Value << YAML::BeginMap;
108 out << YAML::Key << Conf::CALIST_KEY << YAML::Value << tlsCaListFile;
109 out << YAML::Key << Conf::CERTIFICATE_KEY << YAML::Value << tlsCertificateFile;
110 out << YAML::Key << Conf::TLS_PASSWORD_KEY << YAML::Value << tlsPassword;
111 out << YAML::Key << Conf::PRIVATE_KEY_KEY << YAML::Value << tlsPrivateKeyFile;
112 out << YAML::Key << Conf::TLS_ENABLE_KEY << YAML::Value << tlsEnable;
113 out << YAML::Key << Conf::TLS_PORT_KEY << YAML::Value << tlsListenerPort;
114 out << YAML::Key << Conf::VERIFY_CLIENT_KEY << YAML::Value << tlsVerifyClient;
115 out << YAML::Key << Conf::VERIFY_SERVER_KEY << YAML::Value << tlsVerifyServer;
116 out << YAML::Key << Conf::REQUIRE_CERTIF_KEY << YAML::Value << tlsRequireClientCertificate;
117 out << YAML::Key << Conf::DISABLE_SECURE_DLG_CHECK << YAML::Value << tlsDisableSecureDlgCheck;
118 out << YAML::Key << Conf::TIMEOUT_KEY << YAML::Value << tlsNegotiationTimeout;
119 out << YAML::Key << Conf::CIPHERS_KEY << YAML::Value << tlsCiphers;
120 out << YAML::Key << Conf::METHOD_KEY << YAML::Value << tlsMethod;
121 out << YAML::Key << Conf::SERVER_KEY << YAML::Value << tlsServerName;
122 out << YAML::EndMap;
123
124 // srtp submap
125 out << YAML::Key << Conf::SRTP_KEY << YAML::Value << YAML::BeginMap;
126 out << YAML::Key << Conf::KEY_EXCHANGE_KEY << YAML::Value
128 out << YAML::Key << Conf::RTP_FALLBACK_KEY << YAML::Value << srtpFallback;
129 out << YAML::EndMap;
130
131 out << YAML::EndMap;
132}
133
134void
135SipAccountConfig::unserialize(const YAML::Node& node)
136{
138 parseValueOptional(node, Conf::USERNAME_KEY, username);
139 parseValueOptional(node, Conf::BIND_ADDRESS_KEY, bindAddress);
140 parseValueOptional(node, Conf::PORT_KEY, localPort);
141 parseValueOptional(node, Conf::PUBLISH_PORT_KEY, publishedPort);
144 parseValueOptional(node, Conf::KEEP_ALIVE_ENABLED, registrationRefreshEnabled);
145 parseValueOptional(node, Conf::SERVICE_ROUTE_KEY, serviceRoute);
146 parseValueOptional(node, Conf::ALLOW_IP_AUTO_REWRITE, allowIPAutoRewrite);
147
148 parseValueOptional(node, Conf::PRESENCE_MODULE_ENABLED_KEY, presenceEnabled);
151
152 // ICE - STUN/TURN
153 parseValueOptional(node, Conf::STUN_ENABLED_KEY, stunEnabled);
154 parseValueOptional(node, Conf::STUN_SERVER_KEY, stunServer);
155
156 const auto& credsNode = node[Conf::CRED_KEY];
157 setCredentials(parseVectorMap(credsNode,
161
162 // get tls submap
163 try {
164 const auto& tlsMap = node[Conf::TLS_KEY];
165 parseValueOptional(tlsMap, Conf::CERTIFICATE_KEY, tlsCertificateFile);
166 parseValueOptional(tlsMap, Conf::CALIST_KEY, tlsCaListFile);
167 parseValueOptional(tlsMap, Conf::TLS_PASSWORD_KEY, tlsPassword);
168 parseValueOptional(tlsMap, Conf::PRIVATE_KEY_KEY, tlsPrivateKeyFile);
169 parseValueOptional(tlsMap, Conf::TLS_ENABLE_KEY, tlsEnable);
170 parseValueOptional(tlsMap, Conf::TLS_PORT_KEY, tlsListenerPort);
171 parseValueOptional(tlsMap, Conf::CIPHERS_KEY, tlsCiphers);
172 parseValueOptional(tlsMap, Conf::METHOD_KEY, tlsMethod);
173 parseValueOptional(tlsMap, Conf::SERVER_KEY, tlsServerName);
175 parseValueOptional(tlsMap, Conf::VERIFY_CLIENT_KEY, tlsVerifyClient);
176 parseValueOptional(tlsMap, Conf::VERIFY_SERVER_KEY, tlsVerifyServer);
178 parseValueOptional(tlsMap, Conf::TIMEOUT_KEY, tlsNegotiationTimeout);
179 } catch (...) {}
180
181 // get srtp submap
182 const auto& srtpMap = node[Conf::SRTP_KEY];
183 std::string tmpKey;
184 parseValueOptional(srtpMap, Conf::KEY_EXCHANGE_KEY, tmpKey);
186 parseValueOptional(srtpMap, Conf::RTP_FALLBACK_KEY, srtpFallback);
187}
188
189std::map<std::string, std::string>
191{
193 // general sip settings
195 a.emplace(Conf::CONFIG_LOCAL_PORT, std::to_string(localPort));
198 a.emplace(Conf::CONFIG_PUBLISHED_PORT, std::to_string(publishedPort));
209
210 std::string password {};
211 if (not credentials.empty()) {
212 for (const auto& cred : credentials)
213 if (cred.username == username) {
214 password = cred.password;
215 break;
216 }
217 }
218 a.emplace(Conf::CONFIG_ACCOUNT_PASSWORD, std::move(password));
219
220 // srtp settings
223
225 a.emplace(Conf::CONFIG_TLS_LISTENER_PORT, std::to_string(tlsListenerPort));
238 return a;
239}
240
241void
242SipAccountConfig::fromMap(const std::map<std::string, std::string>& details)
243{
245
246 // general sip settings
260
261 // srtp settings
264 if (iter != details.end())
266
267 if (credentials.empty()) { // credentials not set, construct 1 entry
268 JAMI_WARN("No credentials set, inferring them...");
269 std::map<std::string, std::string> map;
273 setCredentials({map});
274 }
275
276 // ICE - STUN
279
280 // TLS
295}
296
297SipAccountConfig::Credentials::Credentials(const std::map<std::string, std::string>& cred)
298{
302 realm = itrealm != cred.end() ? itrealm->second : "";
303 username = user != cred.end() ? user->second : "";
304 password = passw != cred.end() ? passw->second : "";
306}
307
308std::map<std::string, std::string>
315
316void
319
320 /* Compute md5 hash = MD5(username ":" realm ":" password) */
322 pj_md5_update(&pms, (const uint8_t*) username.data(), username.length());
323 pj_md5_update(&pms, (const uint8_t*) ":", 1);
324 pj_md5_update(&pms, (const uint8_t*) realm.data(), realm.length());
325 pj_md5_update(&pms, (const uint8_t*) ":", 1);
326 pj_md5_update(&pms, (const uint8_t*) password.data(), password.length());
327
328 unsigned char digest[16];
330
331 char hash[32];
332
333 for (int i = 0; i < 16; ++i)
335
336 password_h = {hash, 32};
337}
338
339std::vector<std::map<std::string, std::string>>
341{
342 std::vector<std::map<std::string, std::string>> ret;
343 ret.reserve(credentials.size());
344 for (const auto& c : credentials) {
345 ret.emplace_back(c.toMap());
346 }
347 return ret;
348}
349
350void
351SipAccountConfig::setCredentials(const std::vector<std::map<std::string, std::string>>& creds)
352{
353 credentials.clear();
354 credentials.reserve(creds.size());
355 for (const auto& cred : creds)
356 credentials.emplace_back(cred);
357}
358
359}
Account specific keys/constants that must be shared in daemon and clients.
#define JAMI_WARN(...)
Definition logger.h:217
constexpr const char * CALIST_KEY
constexpr const char * INTERFACE_KEY
constexpr const char * PUBLISH_ADDR_KEY
static const char *const CONFIG_SRTP_KEY_EXCHANGE
constexpr const char * STUN_SERVER_KEY
static const char *const CONFIG_ACCOUNT_USERNAME
const char *const TLS_PASSWORD_KEY
constexpr const char * ALLOW_IP_AUTO_REWRITE
static const char *const CONFIG_ACCOUNT_REALM
static const char *const CONFIG_TLS_VERIFY_CLIENT
static const char *const CONFIG_ACCOUNT_IP_AUTO_REWRITE
constexpr const char * PRESENCE_ENABLED_KEY
constexpr const char * SAME_AS_LOCAL_KEY
static const char *const CONFIG_SRTP_RTP_FALLBACK
static const char *const CONFIG_PUBLISHED_SAMEAS_LOCAL
static const char *const CONFIG_PRESENCE_ENABLED
constexpr const char * STUN_ENABLED_KEY
static const char *const CONFIG_TLS_DISABLE_SECURE_DLG_CHECK
constexpr const char * CIPHERS_KEY
static const char *const CONFIG_TLS_CIPHERS
constexpr const char * PRESENCE_SUBSCRIBE_SUPPORTED_KEY
constexpr const char * DISABLE_SECURE_DLG_CHECK
static const char *const CONFIG_TLS_SERVER_NAME
static const char *const CONFIG_PUBLISHED_ADDRESS
constexpr const char * SERVICE_ROUTE_KEY
static const char *const CONFIG_STUN_SERVER
constexpr const char * REQUIRE_CERTIF_KEY
constexpr const char * VERIFY_SERVER_KEY
static const char *const CONFIG_TLS_ENABLE
constexpr const char * DTMF_TYPE_KEY
constexpr const char * KEEP_ALIVE_ENABLED
constexpr const char * TIMEOUT_KEY
constexpr const char * CRED_KEY
static const char *const CONFIG_TLS_CERTIFICATE_FILE
constexpr const char * RTP_FALLBACK_KEY
static const char *const CONFIG_STUN_ENABLE
constexpr const char * PRESENCE_PUBLISH_SUPPORTED_KEY
constexpr const char *const TLS_KEY
static const char *const CONFIG_TLS_PASSWORD
constexpr const char * PUBLISH_PORT_KEY
constexpr const char * METHOD_KEY
constexpr const char * BIND_ADDRESS_KEY
constexpr const char * ID_KEY
static const char *const CONFIG_TLS_PRIVATE_KEY_FILE
constexpr const char * PRESENCE_NOTE_KEY
static const char *const CONFIG_TLS_LISTENER_PORT
const char *const PRIVATE_KEY_KEY
constexpr const char * PRESENCE_MODULE_ENABLED_KEY
constexpr const char * TLS_ENABLE_KEY
static const char *const CONFIG_KEEP_ALIVE_ENABLED
static const char *const CONFIG_TLS_NEGOTIATION_TIMEOUT_SEC
static const char *const CONFIG_TLS_REQUIRE_CLIENT_CERTIFICATE
constexpr const char * PRESENCE_STATUS_KEY
static const char *const CONFIG_ACCOUNT_DTMF_TYPE
constexpr const char * VERIFY_CLIENT_KEY
constexpr const char * CERTIFICATE_KEY
static const char *const CONFIG_TLS_VERIFY_SERVER
constexpr const char * USERNAME_KEY
constexpr const char * TLS_PORT_KEY
constexpr const char * PORT_KEY
static const char *const CONFIG_TLS_CA_LIST_FILE
constexpr const char * KEY_EXCHANGE_KEY
static const char *const CONFIG_LOCAL_PORT
constexpr const char * SERVER_KEY
static const char *const CONFIG_ACCOUNT_PASSWORD
static const char *const CONFIG_BIND_ADDRESS
static const char *const CONFIG_PUBLISHED_PORT
static const char *const CONFIG_ACCOUNT_ROUTESET
static const char *const CONFIG_ACCOUNT_REGISTRATION_EXPIRE
constexpr const char * SRTP_KEY
static const char *const CONFIG_TLS_METHOD
static const char *const CONFIG_LOCAL_INTERFACE
static KeyExchangeProtocol getKeyExchangeProtocol(std::string_view name)
Definition sip_utils.h:119
static constexpr const char * getKeyExchangeName(KeyExchangeProtocol kx)
Definition sip_utils.h:113
std::vector< std::map< std::string, std::string > > parseVectorMap(const YAML::Node &node, const std::initializer_list< std::string > &keys)
bool parseValueOptional(const YAML::Node &node, const char *key, T &value)
Definition yamlparser.h:38
void parseBool(const std::map< std::string, std::string > &details, const char *key, bool &s)
static constexpr const char TRUE_STR[]
void emitSignal(Args... args)
Definition ring_signal.h:64
void parseInt(const std::map< std::string, std::string > &details, const char *key, T &s)
static constexpr const char FALSE_STR[]
void parseString(const std::map< std::string, std::string > &details, const char *key, std::string &s)
void parsePath(const std::map< std::string, std::string > &details, const char *key, std::string &s, const std::filesystem::path &base)
static const JamiAccountConfig DEFAULT_CONFIG
static constexpr unsigned MIN_REGISTRATION_TIME
const std::filesystem::path path
Path where the configuration file is stored.
const std::string id
Account id.
bool publishedSameasLocal
Flag which determine if localIpAddress_ or publishedIpAddress_ is used in sip headers.
void fromMap(const std::map< std::string, std::string > &) override
void serializeDiff(YAML::Emitter &out, const SipAccountBaseConfig &def) const
void unserialize(const YAML::Node &node) override
std::map< std::string, std::string > toMap() const override
Credentials(const std::string &r, const std::string &u, const std::string &p)
std::map< std::string, std::string > toMap() const
uint16_t publishedPort
Published port, used only if defined by the user.
std::string stunServer
The STUN server hostname (optional), used to provide the public IP address in case the softphone stay...
void unserialize(const YAML::Node &node) override
bool srtpFallback
Determine if the softphone should fallback on non secured media channel if SRTP negotiation fails.
std::vector< std::map< std::string, std::string > > getCredentials() const
std::string interface
interface name on which this account is bound
bool stunEnabled
Determine if STUN public address resolution is required to register this account.
uint16_t localPort
Local port to whih this account is bound.
unsigned registrationExpire
Network settings.
std::map< std::string, std::string > toMap() const override
uint16_t tlsListenerPort
The TLS listener port.
void setCredentials(const std::vector< std::map< std::string, std::string > > &creds)
void serialize(YAML::Emitter &out) const override
std::string serviceRoute
Input Outbound Proxy Server Address.
std::vector< Credentials > credentials
KeyExchangeProtocol srtpKeyExchange
Specifies the type of key exchange used for SRTP, if any.
std::string bindAddress
Potential ip addresss on which this account is bound.
void fromMap(const std::map< std::string, std::string > &) override