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 "jamiaccount_config.h"
18 : #include "account_const.h"
19 : #include "account_schema.h"
20 : #include "configkeys.h"
21 : #include "fileutils.h"
22 : #include "config/account_config_utils.h"
23 :
24 : namespace jami {
25 :
26 : namespace Conf {
27 : constexpr const char* const TLS_KEY = "tls";
28 : constexpr const char* CERTIFICATE_KEY = "certificate";
29 : constexpr const char* CALIST_KEY = "calist";
30 : const char* const TLS_PASSWORD_KEY = "password";
31 : const char* const PRIVATE_KEY_KEY = "privateKey";
32 : } // namespace Conf
33 :
34 : static const JamiAccountConfig DEFAULT_CONFIG {};
35 :
36 : void
37 2082 : JamiAccountConfig::serialize(YAML::Emitter& out) const
38 : {
39 2082 : out << YAML::BeginMap;
40 2082 : SipAccountBaseConfig::serializeDiff(out, DEFAULT_CONFIG);
41 2082 : SERIALIZE_CONFIG(Conf::DHT_PORT_KEY, dhtPort);
42 2082 : SERIALIZE_CONFIG(Conf::DHT_PUBLIC_IN_CALLS, allowPublicIncoming);
43 2082 : SERIALIZE_CONFIG(Conf::DHT_ALLOW_PEERS_FROM_HISTORY, allowPeersFromHistory);
44 2082 : SERIALIZE_CONFIG(Conf::DHT_ALLOW_PEERS_FROM_CONTACT, allowPeersFromContact);
45 2082 : SERIALIZE_CONFIG(Conf::DHT_ALLOW_PEERS_FROM_TRUSTED, allowPeersFromTrusted);
46 2082 : SERIALIZE_CONFIG(libjami::Account::ConfProperties::DHT_PEER_DISCOVERY, dhtPeerDiscovery);
47 2082 : SERIALIZE_CONFIG(libjami::Account::ConfProperties::ACCOUNT_PEER_DISCOVERY, accountPeerDiscovery);
48 2082 : SERIALIZE_CONFIG(libjami::Account::ConfProperties::ACCOUNT_PUBLISH, accountPublish);
49 2082 : SERIALIZE_CONFIG(Conf::PROXY_ENABLED_KEY, proxyEnabled);
50 2082 : SERIALIZE_CONFIG(Conf::PROXY_SERVER_KEY, proxyServer);
51 2082 : SERIALIZE_CONFIG(libjami::Account::ConfProperties::PROXY_LIST_ENABLED, proxyListEnabled);
52 2082 : SERIALIZE_CONFIG(libjami::Account::ConfProperties::DHT_PROXY_LIST_URL, proxyListUrl);
53 2082 : SERIALIZE_CONFIG(libjami::Account::ConfProperties::Nameserver::URI, nameServer);
54 2082 : SERIALIZE_CONFIG(libjami::Account::VolatileProperties::REGISTERED_NAME, registeredName);
55 2082 : SERIALIZE_PATH(libjami::Account::ConfProperties::ARCHIVE_PATH, archivePath);
56 2082 : SERIALIZE_CONFIG(libjami::Account::ConfProperties::ARCHIVE_HAS_PASSWORD, archiveHasPassword);
57 2082 : SERIALIZE_CONFIG(libjami::Account::ConfProperties::DEVICE_NAME, deviceName);
58 2082 : SERIALIZE_CONFIG(libjami::Account::ConfProperties::MANAGER_URI, managerUri);
59 2082 : SERIALIZE_CONFIG(libjami::Account::ConfProperties::MANAGER_USERNAME, managerUsername);
60 2082 : SERIALIZE_CONFIG(libjami::Account::ConfProperties::DHT::PUBLIC_IN_CALLS, dhtPublicInCalls);
61 :
62 2082 : out << YAML::Key << Conf::RING_ACCOUNT_RECEIPT << YAML::Value << receipt;
63 2082 : if (receiptSignature.size() > 0)
64 : out << YAML::Key << Conf::RING_ACCOUNT_RECEIPT_SIG << YAML::Value
65 712 : << YAML::Binary(receiptSignature.data(), receiptSignature.size());
66 :
67 : // tls submap
68 2082 : out << YAML::Key << Conf::TLS_KEY << YAML::Value << YAML::BeginMap;
69 2082 : SERIALIZE_PATH(Conf::CALIST_KEY, tlsCaListFile);
70 2082 : SERIALIZE_PATH(Conf::CERTIFICATE_KEY, tlsCertificateFile);
71 2082 : SERIALIZE_CONFIG(Conf::TLS_PASSWORD_KEY, tlsPassword);
72 2082 : SERIALIZE_PATH(Conf::PRIVATE_KEY_KEY, tlsPrivateKeyFile);
73 2082 : out << YAML::EndMap;
74 :
75 2082 : out << YAML::EndMap;
76 2082 : }
77 :
78 : void
79 0 : JamiAccountConfig::unserialize(const YAML::Node& node)
80 : {
81 : using yaml_utils::parseValueOptional;
82 : using yaml_utils::parsePathOptional;
83 0 : SipAccountBaseConfig::unserialize(node);
84 :
85 : // get tls submap
86 : try {
87 0 : const auto& tlsMap = node[Conf::TLS_KEY];
88 0 : parsePathOptional(tlsMap, Conf::CERTIFICATE_KEY, tlsCertificateFile, path);
89 0 : parsePathOptional(tlsMap, Conf::CALIST_KEY, tlsCaListFile, path);
90 0 : parseValueOptional(tlsMap, Conf::TLS_PASSWORD_KEY, tlsPassword);
91 0 : parsePathOptional(tlsMap, Conf::PRIVATE_KEY_KEY, tlsPrivateKeyFile, path);
92 0 : } catch (...) {
93 0 : }
94 0 : parseValueOptional(node, Conf::DHT_PORT_KEY, dhtPort);
95 0 : parseValueOptional(node, Conf::DHT_ALLOW_PEERS_FROM_HISTORY, allowPeersFromHistory);
96 0 : parseValueOptional(node, Conf::DHT_ALLOW_PEERS_FROM_CONTACT, allowPeersFromContact);
97 0 : parseValueOptional(node, Conf::DHT_ALLOW_PEERS_FROM_TRUSTED, allowPeersFromTrusted);
98 :
99 0 : parseValueOptional(node, Conf::PROXY_ENABLED_KEY, proxyEnabled);
100 0 : parseValueOptional(node, Conf::PROXY_SERVER_KEY, proxyServer);
101 0 : parseValueOptional(node, libjami::Account::ConfProperties::DHT_PROXY_LIST_URL, proxyListUrl);
102 0 : parseValueOptional(node, libjami::Account::ConfProperties::PROXY_LIST_ENABLED, proxyListEnabled);
103 :
104 0 : parseValueOptional(node, libjami::Account::ConfProperties::DEVICE_NAME, deviceName);
105 0 : parseValueOptional(node, libjami::Account::ConfProperties::MANAGER_URI, managerUri);
106 0 : parseValueOptional(node, libjami::Account::ConfProperties::MANAGER_USERNAME, managerUsername);
107 0 : parseValueOptional(node,
108 : libjami::Account::ConfProperties::DHT::PUBLIC_IN_CALLS,
109 0 : dhtPublicInCalls);
110 :
111 0 : parsePathOptional(node, libjami::Account::ConfProperties::ARCHIVE_PATH, archivePath, path);
112 0 : parseValueOptional(node,
113 : libjami::Account::ConfProperties::ARCHIVE_HAS_PASSWORD,
114 0 : archiveHasPassword);
115 :
116 : try {
117 0 : parseValueOptional(node, Conf::RING_ACCOUNT_RECEIPT, receipt);
118 0 : auto receipt_sig = node[Conf::RING_ACCOUNT_RECEIPT_SIG].as<YAML::Binary>();
119 0 : receiptSignature = {receipt_sig.data(), receipt_sig.data() + receipt_sig.size()};
120 0 : } catch (const std::exception& e) {
121 0 : JAMI_WARN("Unable to read receipt: %s", e.what());
122 0 : }
123 :
124 0 : parseValueOptional(node, libjami::Account::ConfProperties::DHT_PEER_DISCOVERY, dhtPeerDiscovery);
125 0 : parseValueOptional(node,
126 : libjami::Account::ConfProperties::ACCOUNT_PEER_DISCOVERY,
127 0 : accountPeerDiscovery);
128 0 : parseValueOptional(node, libjami::Account::ConfProperties::ACCOUNT_PUBLISH, accountPublish);
129 0 : parseValueOptional(node, libjami::Account::ConfProperties::Nameserver::URI, nameServer);
130 0 : parseValueOptional(node, libjami::Account::VolatileProperties::REGISTERED_NAME, registeredName);
131 0 : parseValueOptional(node, Conf::DHT_PUBLIC_IN_CALLS, allowPublicIncoming);
132 0 : }
133 :
134 : std::map<std::string, std::string>
135 541 : JamiAccountConfig::toMap() const
136 : {
137 541 : std::map<std::string, std::string> a = SipAccountBaseConfig::toMap();
138 541 : a.emplace(Conf::CONFIG_DHT_PORT, std::to_string(dhtPort));
139 541 : a.emplace(Conf::CONFIG_DHT_PUBLIC_IN_CALLS, allowPublicIncoming ? TRUE_STR : FALSE_STR);
140 541 : a.emplace(libjami::Account::ConfProperties::DHT_PEER_DISCOVERY,
141 541 : dhtPeerDiscovery ? TRUE_STR : FALSE_STR);
142 541 : a.emplace(libjami::Account::ConfProperties::ACCOUNT_PEER_DISCOVERY,
143 541 : accountPeerDiscovery ? TRUE_STR : FALSE_STR);
144 541 : a.emplace(libjami::Account::ConfProperties::ACCOUNT_PUBLISH,
145 541 : accountPublish ? TRUE_STR : FALSE_STR);
146 541 : a.emplace(libjami::Account::ConfProperties::DEVICE_NAME, deviceName);
147 541 : a.emplace(libjami::Account::ConfProperties::Presence::SUPPORT_SUBSCRIBE, TRUE_STR);
148 541 : if (not archivePath.empty() or not managerUri.empty())
149 541 : a.emplace(libjami::Account::ConfProperties::ARCHIVE_HAS_PASSWORD,
150 541 : archiveHasPassword ? TRUE_STR : FALSE_STR);
151 :
152 541 : a.emplace(Conf::CONFIG_TLS_CA_LIST_FILE, fileutils::getFullPath(path, tlsCaListFile).string());
153 541 : a.emplace(Conf::CONFIG_TLS_CERTIFICATE_FILE,
154 1082 : fileutils::getFullPath(path, tlsCertificateFile).string());
155 541 : a.emplace(Conf::CONFIG_TLS_PRIVATE_KEY_FILE,
156 1082 : fileutils::getFullPath(path, tlsPrivateKeyFile).string());
157 541 : a.emplace(Conf::CONFIG_TLS_PASSWORD, tlsPassword);
158 541 : a.emplace(libjami::Account::ConfProperties::ALLOW_CERT_FROM_HISTORY,
159 541 : allowPeersFromHistory ? TRUE_STR : FALSE_STR);
160 541 : a.emplace(libjami::Account::ConfProperties::ALLOW_CERT_FROM_CONTACT,
161 541 : allowPeersFromContact ? TRUE_STR : FALSE_STR);
162 541 : a.emplace(libjami::Account::ConfProperties::ALLOW_CERT_FROM_TRUSTED,
163 541 : allowPeersFromTrusted ? TRUE_STR : FALSE_STR);
164 541 : a.emplace(libjami::Account::ConfProperties::PROXY_ENABLED, proxyEnabled ? TRUE_STR : FALSE_STR);
165 541 : a.emplace(libjami::Account::ConfProperties::PROXY_LIST_ENABLED,
166 541 : proxyListEnabled ? TRUE_STR : FALSE_STR);
167 541 : a.emplace(libjami::Account::ConfProperties::PROXY_SERVER, proxyServer);
168 541 : a.emplace(libjami::Account::ConfProperties::DHT_PROXY_LIST_URL, proxyListUrl);
169 541 : a.emplace(libjami::Account::ConfProperties::MANAGER_URI, managerUri);
170 541 : a.emplace(libjami::Account::ConfProperties::MANAGER_USERNAME, managerUsername);
171 541 : a.emplace(libjami::Account::ConfProperties::DHT::PUBLIC_IN_CALLS,
172 541 : dhtPublicInCalls ? TRUE_STR : FALSE_STR);
173 : #ifdef ENABLE_NAMESERVER
174 541 : a.emplace(libjami::Account::ConfProperties::Nameserver::URI, nameServer);
175 : #endif
176 541 : return a;
177 0 : }
178 :
179 : void
180 1289 : JamiAccountConfig::fromMap(const std::map<std::string, std::string>& details)
181 : {
182 1289 : SipAccountBaseConfig::fromMap(details);
183 : // TLS
184 1289 : parsePath(details, Conf::CONFIG_TLS_CA_LIST_FILE, tlsCaListFile, path);
185 1289 : parsePath(details, Conf::CONFIG_TLS_CERTIFICATE_FILE, tlsCertificateFile, path);
186 1289 : parsePath(details, Conf::CONFIG_TLS_PRIVATE_KEY_FILE, tlsPrivateKeyFile, path);
187 1289 : parseString(details, Conf::CONFIG_TLS_PASSWORD, tlsPassword);
188 :
189 1289 : if (hostname.empty())
190 0 : hostname = DHT_DEFAULT_BOOTSTRAP;
191 1289 : parseString(details, libjami::Account::ConfProperties::BOOTSTRAP_LIST_URL, bootstrapListUrl);
192 1289 : parseInt(details, Conf::CONFIG_DHT_PORT, dhtPort);
193 1289 : parseBool(details, Conf::CONFIG_DHT_PUBLIC_IN_CALLS, allowPublicIncoming);
194 1289 : parseBool(details, libjami::Account::ConfProperties::DHT_PEER_DISCOVERY, dhtPeerDiscovery);
195 1289 : parseBool(details,
196 : libjami::Account::ConfProperties::ACCOUNT_PEER_DISCOVERY,
197 1289 : accountPeerDiscovery);
198 1289 : parseBool(details, libjami::Account::ConfProperties::ACCOUNT_PUBLISH, accountPublish);
199 1289 : parseBool(details,
200 : libjami::Account::ConfProperties::ALLOW_CERT_FROM_HISTORY,
201 1289 : allowPeersFromHistory);
202 1289 : parseBool(details,
203 : libjami::Account::ConfProperties::ALLOW_CERT_FROM_CONTACT,
204 1289 : allowPeersFromContact);
205 1289 : parseBool(details,
206 : libjami::Account::ConfProperties::ALLOW_CERT_FROM_TRUSTED,
207 1289 : allowPeersFromTrusted);
208 :
209 1289 : parseString(details, libjami::Account::ConfProperties::MANAGER_URI, managerUri);
210 1289 : parseString(details, libjami::Account::ConfProperties::MANAGER_USERNAME, managerUsername);
211 1289 : parseBool(details, libjami::Account::ConfProperties::DHT::PUBLIC_IN_CALLS, dhtPublicInCalls);
212 : // parseString(details, libjami::Account::ConfProperties::USERNAME, username);
213 :
214 1289 : parseString(details,
215 : libjami::Account::ConfProperties::ARCHIVE_PASSWORD,
216 1289 : credentials.archive_password);
217 1289 : parseString(details,
218 : libjami::Account::ConfProperties::ARCHIVE_PASSWORD_SCHEME,
219 1289 : credentials.archive_password_scheme);
220 1289 : parseString(details, libjami::Account::ConfProperties::ARCHIVE_PATH, credentials.archive_path);
221 1289 : parseString(details, libjami::Account::ConfProperties::DEVICE_NAME, deviceName);
222 1289 : parseString(details, libjami::Account::ConfProperties::ARCHIVE_URL, archive_url);
223 :
224 1289 : auto oldProxyServer = proxyServer, oldProxyServerList = proxyListUrl;
225 1289 : parseString(details, libjami::Account::ConfProperties::DHT_PROXY_LIST_URL, proxyListUrl);
226 1289 : parseBool(details, libjami::Account::ConfProperties::PROXY_ENABLED, proxyEnabled);
227 1289 : parseBool(details, libjami::Account::ConfProperties::PROXY_LIST_ENABLED, proxyListEnabled);
228 1289 : parseString(details, libjami::Account::ConfProperties::PROXY_SERVER, proxyServer);
229 1289 : parseString(details, libjami::Account::ConfProperties::UI_CUSTOMIZATION, uiCustomization);
230 1289 : if (not managerUri.empty() and managerUri.rfind("http", 0) != 0) {
231 0 : managerUri = "https://" + managerUri;
232 : }
233 :
234 : #ifdef ENABLE_NAMESERVER
235 1289 : parseString(details, libjami::Account::ConfProperties::Nameserver::URI, nameServer);
236 : #endif
237 1289 : }
238 :
239 : } // namespace jami
|