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