1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
/*
Copyright (C) 2019-2025 Selwin van Dijk
This file is part of signalbackup-tools.
signalbackup-tools is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
signalbackup-tools is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with signalbackup-tools. If not, see <https://www.gnu.org/licenses/>.
*/
#include "signalbackup.ih"
bool SignalBackup::exportBackupToFile(std::string const &filename, std::string const &passphrase, bool overwrite, bool keepattachmentdatainmemory)
{
Logger::message("\nExporting backup to '", filename, "'");
std::string newpw = passphrase;
if (newpw == std::string())
newpw = d_passphrase;
if (newpw == std::string())
{
Logger::error("Need password to create encrypted backup file.");
return false;
}
if (!overwrite && bepaald::fileOrDirExists(filename))
{
Logger::error("File '", filename, "' exists, use --overwrite to overwrite");
return false;
}
if (!d_headerframe || !d_fe.init(newpw, d_headerframe->salt(), d_headerframe->salt_length(), d_headerframe->iv(), d_headerframe->iv_length(), d_headerframe->version(), d_verbose))
{
Logger::error("Failed to initialize FileEncryptor");
return false;
}
std::ofstream outputfile(filename, std::ios_base::binary);
// HEADER // Note: HeaderFrame is not encrypted.
Logger::message("Writing HeaderFrame...");
if (!d_headerframe)
{
Logger::error("HeaderFrame not found");
return false;
}
std::pair<unsigned char *, uint64_t> framedata = d_headerframe->getData();
if (!framedata.first)
{
Logger::error("Failed to get HeaderFrame data");
return false;
}
bool writeok = writeFrameDataToFile(outputfile, framedata);
delete[] framedata.first;
if (!writeok)
return false;
// VERSION
Logger::message("Writing DatabaseVersionFrame...");
if (!d_databaseversionframe)
{
Logger::error("DataBaseVersionFrame not found");
return false;
}
if (!writeEncryptedFrame(outputfile, d_databaseversionframe.get()))
return false;
// SQL DATABASE + ATTACHMENTS
Logger::message("Writing SqlStatementFrame(s)...");
// get and write schema
SqliteDB::QueryResults results;
d_database.exec("SELECT sql, name, type FROM sqlite_master WHERE sql NOT NULL", &results);
std::vector<std::string> tables;
for (unsigned int i = 0; i < results.rows(); ++i)
{
if (results.valueHasType<std::string>(i, 1) &&
(results.getValueAs<std::string>(i, 1) != "sms_fts" &&
STRING_STARTS_WITH(results.getValueAs<std::string>(i, 1), "sms_fts")))
continue;//std::cout << "Skipping " << results[i][1].second << " because it is sms_ftssecrettable" << std::endl;
if (results.valueHasType<std::string>(i, 1) &&
(results.getValueAs<std::string>(i, 1) != d_mms_table + "_fts" &&
STRING_STARTS_WITH(results.getValueAs<std::string>(i, 1), d_mms_table + "_fts")))
continue;//std::cout << "Skipping " << results[i][1].second << " because it is mms_ftssecrettable" << std::endl;
if (results.valueHasType<std::string>(i, 1) &&
(results.getValueAs<std::string>(i, 1) != "emoji_search" &&
STRING_STARTS_WITH(results.getValueAs<std::string>(i, 1), "emoji_search")))
continue;//std::cout << "Skipping " << results[i][1].second << " because it is emoji_search_ftssecrettable" << std::endl;
if (results.valueHasType<std::string>(i, 1) &&
STRING_STARTS_WITH(results.getValueAs<std::string>(i, 1), "sqlite_"))
{
// this is normally skipped, but for testing purposes we won't skip if it was found in input
#ifdef BUILT_FOR_TESTING
if (d_found_sqlite_sequence_in_backup)
;
else
#endif
continue;
}
if (results.valueHasType<std::string>(i, 2) && results.getValueAs<std::string>(i, 2) == "table")
tables.emplace_back(results.getValueAs<std::string>(i, 1));
SqlStatementFrame newframe;
newframe.setStatementField(results.getValueAs<std::string>(i, 0));
//std::cout << "Writing SqlStatementFrame..." << std::endl;
if (!writeEncryptedFrame(outputfile, &newframe))
return false;
}
// write contents of tables
for (std::string const &table : tables)
{
if (table == "signed_prekeys" ||
table == "one_time_prekeys" ||
table == "sessions" ||
//table == "job_spec" || // this is in the official export. But it makes testing more difficult. it
//table == "constraint_spec" || // should be ok to export these (if present in source), since we are only
//table == "dependency_spec" || // dealing with exported backups (not from live installations) -> they should
//table == "emoji_search" || // have been excluded + the official import should be able to deal with them
//table == "sender_keys" ||
//table == "sender_key_shared" ||
//table == "pending_retry_receipts" ||
//table == "avatar_picker" ||
//table == "remapped_recipients" ||
//table == "remapped_threads" ||
STRING_STARTS_WITH(table, "sms_fts") ||
STRING_STARTS_WITH(table, d_mms_table + "_fts") ||
STRING_STARTS_WITH(table, "sqlite_"))
continue;
d_database.exec("SELECT * FROM " + table, &results);
if (!d_showprogress)
Logger::message_start(" Dealing with table '", table, "'... ");
for (unsigned int i = 0; i < results.rows(); ++i)
{
if (d_showprogress)
Logger::message_overwrite(" Dealing with table '", table, "'... ", i + 1, "/", results.rows(), " entries...");
SqlStatementFrame newframe = buildSqlStatementFrame(table, results.row(i));
//std::cout << "Writing SqlStatementFrame..." << std::endl;
if (!writeEncryptedFrame(outputfile, &newframe))
return false;
if (table == d_part_table) // find corresponding attachment
{
bool needuniqqueid = d_database.tableContainsColumn(d_part_table, "unique_id");
long long int rowid = 0, uniqueid = needuniqqueid ? 0 : -1;
for (unsigned int j = 0; j < results.columns(); ++j)
{
if (results.header(j) == "_id" && results.valueHasType<long long int>(i, j))
{
rowid = results.getValueAs<long long int>(i, j);
if (rowid && (uniqueid || !needuniqqueid))
break;
}
else if (needuniqqueid &&
results.header(j) == "unique_id" && results.valueHasType<long long int>(i, j))
{
//std::cout << "UNIQUEID: " << std::any_cast<long long int>(results[i][j].second) << std::endl;
uniqueid = results.getValueAs<long long int>(i, j);
if (rowid && (uniqueid || !needuniqqueid))
break;
}
}
auto attachment = d_attachments.find({rowid, uniqueid});
if (attachment != d_attachments.end()) [[likely]]
{
if (!writeEncryptedFrame(outputfile, attachment->second.get()))
return false;
if (!keepattachmentdatainmemory)
{
MEMINFO("BEFORE DROPPING ATTACHMENT DATA");
attachment->second.get()->clearData();
MEMINFO("AFTER DROPPING ATTACHMENT DATA");
}
}
else [[unlikely]]
{
if (!missingAttachmentExpected(rowid, uniqueid))
{
Logger::warning("Attachment data not found (rowid: ", rowid, ", uniqueid: ", uniqueid, ")");
if (d_showprogress)
Logger::message_overwrite(" Dealing with table '", table, "'... ", i + 1, "/", results.rows(), " entries...");
}
}
}
else if (table == "sticker") // find corresponding sticker
{
uint64_t rowid = 0;
for (unsigned int j = 0; j < results.columns(); ++j)
if (results.header(j) == "_id" && results.valueHasType<long long int>(i, j))
{
rowid = results.getValueAs<long long int>(i, j);
break;
}
auto sticker = d_stickers.find(rowid);
if (sticker != d_stickers.end())
{
if (!writeEncryptedFrame(outputfile, sticker->second.get()))
return false;
if (!keepattachmentdatainmemory)
sticker->second.get()->clearData();
}
else
{
Logger::warning("Sticker data not found (rowid: ", rowid, ")");
if (d_showprogress)
Logger::message_overwrite(" Dealing with table '", table, "'... ", i + 1, "/", results.rows(), " entries...");
}
}
}
if (d_showprogress)
Logger::message_overwrite(" Dealing with table '", table, "'... ", results.rows(), "/", results.rows(), " entries...done", Logger::Control::ENDOVERWRITE);
else
Logger::message_end("done");
}
Logger::message("Writing SharedPrefFrame(s)...");
// SHAREDPREFS
for (unsigned int i = 0; i < d_sharedpreferenceframes.size(); ++i)
if (!writeEncryptedFrame(outputfile, d_sharedpreferenceframes[i].get()))
return false;
Logger::message("Writing KeyValueFrame(s)...");
// KEYVALUES
for (unsigned int i = 0; i < d_keyvalueframes.size(); ++i)
if (!writeEncryptedFrame(outputfile, d_keyvalueframes[i].get()))
return false;
// AVATAR
Logger::message("Writing Avatars...");
for (auto const &a : d_avatars)
{
if (d_verbose && !a.second.get()) [[unlikely]]
{
Logger::error("ASKED TO WRITE NULLPTR-AVATAR. THIS SHOULD BE AN ERROR");
Logger::error_indent("BUT I'M PRETENDING IT DIDN'T HAPPEN TO FIND THE CAUSE OF IT");
Logger::error_indent("THE PROGRAM WILL LIKELY CRASH NOW...");
}
if (!writeEncryptedFrame(outputfile, a.second.get()))
return false;
}
// END
Logger::message("Writing EndFrame...");
if (!d_endframe)
{
Logger::error("EndFrame not found.");
return false;
}
if (!writeEncryptedFrame(outputfile, d_endframe.get()))
return false;
outputfile.flush();
Logger::message("Done! Wrote ", outputfile.tellp(), " bytes.");
return true;
}
|