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