Linux + iPhone Continuity / iMessage / SMS
fix: PR feedback
Zack Bartel78317294fb15
diff --git a/docs/BLUETOOTH.md b/docs/BLUETOOTH.mdindex 0ddf901..4829ebe 100644--- a/docs/BLUETOOTH.md+++ b/docs/BLUETOOTH.md@@ -847,6 +847,16 @@ service on the bus at all the key falls back to `~/.config/tether/store.key`,mode 0600 — that stops a backup or a synced home, not another process running asyou, and it is never written when a wallet is merely locked.+A key is generated and stored only when the search returns **zero** items and+the default collection is unlocked. `secret_password_store_sync` updates an+existing item whose attributes match rather than failing, so treating a locked+item as a first run would replace the key the sealed store was written with and+lose the history for good. The search therefore distinguishes four answers: a+readable secret, an item whose secret is null (locked — wait), no item at all+with the collection locked or missing (wait), and no item with the collection+unlocked (first run — generate). A failed search counts as locked. Waiting takes+the same one-a-minute retry as everything else here.+Three retention modes, `retention` in `bluetooth.json`, also `tether--bt-retention`:@@ -868,14 +878,23 @@ journal with zero records, which closes the same hole for the next format changeChanging modes migrates the data: read under the old mode, write under the new,fsync, rename, then unlink the source — never the other way round, so a crash-mid-migration leaves both copies rather than neither. A store written by a-pre-0.2.24 daemon migrates on the first start after the upgrade.+mid-migration leaves both copies rather than neither. The next start finishes+the job: the journal folds any leftover source records into the destination and+unlinks it (duplicate handles collapse on replay, and `compact()` rewrites them+out), and the contact cache, being a whole-file snapshot, keeps the destination+and drops the source. Without that, a plaintext file stranded by a crash would+survive every later start. A store written by a pre-0.2.24 daemon migrates on+the first start after the upgrade.A locked wallet keeps MAP and PBAP up and messages flowing to the UI, and onlypauses what is retained: the journal does not open, nothing is replayed, andcontact names go unresolved. The key is retried on each message poll, ratelimited to one wallet round trip a minute, so history starts persisting within a-minute of the user unlocking.+minute of the user unlocking. A PBAP pull that happens while the wallet is+locked lives only in memory, since `save_contacts` will not write without a key.+Unlocking flushes that phonebook to disk instead of reading an empty cache over+it — the pull is once per PBAP session, so overwriting it would cost every+display name until the phone reconnects.> **Losing the keyring entry loses the retained history.** There is no escrow and> no plaintext copy left behind. The phone can refill part of it over MAP; sentdiff --git a/po/cs.po b/po/cs.poindex dc3dab3..6fbda99 100644--- a/po/cs.po+++ b/po/cs.po@@ -440,9 +440,10 @@ msgstr[2] "({} nepřečtených)"#: src/cli/main.cpp#, c++-formatmsgid ""-"The running tetherd is {}, but this is tether {}. Restart it with:\n"-" systemctl --user restart tetherd\n"-msgstr "Běžící tetherd je {}, ale toto je tether {}. Restartujte jej příkazem:\n systemctl --user restart tetherd\n"+"The running tetherd is {}, but this is tether {}. Stop it and it\n"+"restarts on demand:\n"+" pkill tetherd\n"+msgstr "Běžící tetherd je {}, ale toto je tether {}. Zastavte jej; spustí se znovu podle potřeby:\n pkill tetherd\n"#: src/cli/main.cppmsgid "an older build"diff --git a/po/de.po b/po/de.poindex d9c5848..4c157d8 100644--- a/po/de.po+++ b/po/de.po@@ -438,9 +438,10 @@ msgstr[1] "({} ungelesen)"#: src/cli/main.cpp#, c++-formatmsgid ""-"The running tetherd is {}, but this is tether {}. Restart it with:\n"-" systemctl --user restart tetherd\n"-msgstr "Der laufende tetherd ist {}, dies hier ist aber tether {}. Starten Sie ihn neu mit:\n systemctl --user restart tetherd\n"+"The running tetherd is {}, but this is tether {}. Stop it and it\n"+"restarts on demand:\n"+" pkill tetherd\n"+msgstr "Der laufende tetherd ist {}, dies hier ist aber tether {}. Beenden Sie ihn; er startet bei Bedarf neu:\n pkill tetherd\n"#: src/cli/main.cppmsgid "an older build"diff --git a/po/es.po b/po/es.poindex 1da554c..3df015e 100644--- a/po/es.po+++ b/po/es.po@@ -437,9 +437,10 @@ msgstr[1] "({} sin leer)"#: src/cli/main.cpp#, c++-formatmsgid ""-"The running tetherd is {}, but this is tether {}. Restart it with:\n"-" systemctl --user restart tetherd\n"-msgstr "El tetherd en ejecución es {}, pero este es tether {}. Reinícielo con:\n systemctl --user restart tetherd\n"+"The running tetherd is {}, but this is tether {}. Stop it and it\n"+"restarts on demand:\n"+" pkill tetherd\n"+msgstr "El tetherd en ejecución es {}, pero este es tether {}. Deténgalo y se reiniciará bajo demanda:\n pkill tetherd\n"#: src/cli/main.cppmsgid "an older build"diff --git a/po/fr.po b/po/fr.poindex d111a34..171a978 100644--- a/po/fr.po+++ b/po/fr.po@@ -443,9 +443,10 @@ msgstr[1] "({} non lus)"#: src/cli/main.cpp#, c++-formatmsgid ""-"The running tetherd is {}, but this is tether {}. Restart it with:\n"-" systemctl --user restart tetherd\n"-msgstr "Le tetherd en cours d'exécution est {}, mais ceci est tether {}. Redémarrez-le avec :\n systemctl --user restart tetherd\n"+"The running tetherd is {}, but this is tether {}. Stop it and it\n"+"restarts on demand:\n"+" pkill tetherd\n"+msgstr "Le tetherd en cours d'exécution est {}, mais ceci est tether {}. Arrêtez-le, il redémarrera à la demande :\n pkill tetherd\n"#: src/cli/main.cppmsgid "an older build"diff --git a/po/it.po b/po/it.poindex 191ba41..56f76d8 100644--- a/po/it.po+++ b/po/it.po@@ -437,9 +437,10 @@ msgstr[1] "({} non letti)"#: src/cli/main.cpp#, c++-formatmsgid ""-"The running tetherd is {}, but this is tether {}. Restart it with:\n"-" systemctl --user restart tetherd\n"-msgstr "Il tetherd in esecuzione è {}, ma questo è tether {}. Riavvialo con:\n systemctl --user restart tetherd\n"+"The running tetherd is {}, but this is tether {}. Stop it and it\n"+"restarts on demand:\n"+" pkill tetherd\n"+msgstr "Il tetherd in esecuzione è {}, ma questo è tether {}. Fermalo; si riavvia su richiesta:\n pkill tetherd\n"#: src/cli/main.cppmsgid "an older build"diff --git a/po/ja.po b/po/ja.poindex f21822f..28c8e24 100644--- a/po/ja.po+++ b/po/ja.po@@ -426,9 +426,10 @@ msgstr[0] "({} 件未読)"#: src/cli/main.cpp#, c++-formatmsgid ""-"The running tetherd is {}, but this is tether {}. Restart it with:\n"-" systemctl --user restart tetherd\n"-msgstr "実行中の tetherd は {} ですが、こちらは tether {} です。次のコマンドで再起動してください:\n systemctl --user restart tetherd\n"+"The running tetherd is {}, but this is tether {}. Stop it and it\n"+"restarts on demand:\n"+" pkill tetherd\n"+msgstr "実行中の tetherd は {} ですが、こちらは tether {} です。停止すれば必要に応じて自動で起動します:\n pkill tetherd\n"#: src/cli/main.cppmsgid "an older build"diff --git a/po/ko.po b/po/ko.poindex 01af4e8..95d98ba 100644--- a/po/ko.po+++ b/po/ko.po@@ -425,9 +425,10 @@ msgstr[0] "(읽지 않음 {}개)"#: src/cli/main.cpp#, c++-formatmsgid ""-"The running tetherd is {}, but this is tether {}. Restart it with:\n"-" systemctl --user restart tetherd\n"-msgstr "실행 중인 tetherd는 {}이지만 이것은 tether {}입니다. 다음 명령으로 다시 시작하십시오:\n systemctl --user restart tetherd\n"+"The running tetherd is {}, but this is tether {}. Stop it and it\n"+"restarts on demand:\n"+" pkill tetherd\n"+msgstr "실행 중인 tetherd는 {}이지만 이것은 tether {}입니다. 중지하면 필요할 때 다시 시작됩니다:\n pkill tetherd\n"#: src/cli/main.cppmsgid "an older build"diff --git a/po/nl.po b/po/nl.poindex 39ce089..2ae96f7 100644--- a/po/nl.po+++ b/po/nl.po@@ -436,9 +436,10 @@ msgstr[1] "({} ongelezen)"#: src/cli/main.cpp#, c++-formatmsgid ""-"The running tetherd is {}, but this is tether {}. Restart it with:\n"-" systemctl --user restart tetherd\n"-msgstr "De draaiende tetherd is {}, maar dit is tether {}. Start hem opnieuw met:\n systemctl --user restart tetherd\n"+"The running tetherd is {}, but this is tether {}. Stop it and it\n"+"restarts on demand:\n"+" pkill tetherd\n"+msgstr "De draaiende tetherd is {}, maar dit is tether {}. Stop hem; hij start vanzelf opnieuw wanneer nodig:\n pkill tetherd\n"#: src/cli/main.cppmsgid "an older build"diff --git a/po/pl.po b/po/pl.poindex 42f0534..84cd546 100644--- a/po/pl.po+++ b/po/pl.po@@ -441,9 +441,10 @@ msgstr[2] "({} nieprzeczytanych)"#: src/cli/main.cpp#, c++-formatmsgid ""-"The running tetherd is {}, but this is tether {}. Restart it with:\n"-" systemctl --user restart tetherd\n"-msgstr "Działający tetherd to {}, a to jest tether {}. Uruchom go ponownie poleceniem:\n systemctl --user restart tetherd\n"+"The running tetherd is {}, but this is tether {}. Stop it and it\n"+"restarts on demand:\n"+" pkill tetherd\n"+msgstr "Działający tetherd to {}, a to jest tether {}. Zatrzymaj go; uruchomi się ponownie na żądanie:\n pkill tetherd\n"#: src/cli/main.cppmsgid "an older build"diff --git a/po/pt_BR.po b/po/pt_BR.poindex 98a8119..ba9b130 100644--- a/po/pt_BR.po+++ b/po/pt_BR.po@@ -436,9 +436,10 @@ msgstr[1] "({} não lidas)"#: src/cli/main.cpp#, c++-formatmsgid ""-"The running tetherd is {}, but this is tether {}. Restart it with:\n"-" systemctl --user restart tetherd\n"-msgstr "O tetherd em execução é {}, mas este é o tether {}. Reinicie-o com:\n systemctl --user restart tetherd\n"+"The running tetherd is {}, but this is tether {}. Stop it and it\n"+"restarts on demand:\n"+" pkill tetherd\n"+msgstr "O tetherd em execução é {}, mas este é o tether {}. Pare-o; ele reinicia sob demanda:\n pkill tetherd\n"#: src/cli/main.cppmsgid "an older build"diff --git a/po/ru.po b/po/ru.poindex 1062757..361e93d 100644--- a/po/ru.po+++ b/po/ru.po@@ -441,9 +441,10 @@ msgstr[2] "({} непрочитанных)"#: src/cli/main.cpp#, c++-formatmsgid ""-"The running tetherd is {}, but this is tether {}. Restart it with:\n"-" systemctl --user restart tetherd\n"-msgstr "Запущен tetherd {}, но это tether {}. Перезапустите его командой:\n systemctl --user restart tetherd\n"+"The running tetherd is {}, but this is tether {}. Stop it and it\n"+"restarts on demand:\n"+" pkill tetherd\n"+msgstr "Запущен tetherd {}, но это tether {}. Остановите его — он запустится снова по требованию:\n pkill tetherd\n"#: src/cli/main.cppmsgid "an older build"diff --git a/po/sv.po b/po/sv.poindex 2d41485..9cd0c83 100644--- a/po/sv.po+++ b/po/sv.po@@ -434,9 +434,10 @@ msgstr[1] "({} olästa)"#: src/cli/main.cpp#, c++-formatmsgid ""-"The running tetherd is {}, but this is tether {}. Restart it with:\n"-" systemctl --user restart tetherd\n"-msgstr "Den körande tetherd är {}, men detta är tether {}. Starta om den med:\n systemctl --user restart tetherd\n"+"The running tetherd is {}, but this is tether {}. Stop it and it\n"+"restarts on demand:\n"+" pkill tetherd\n"+msgstr "Den körande tetherd är {}, men detta är tether {}. Stoppa den; den startar om vid behov:\n pkill tetherd\n"#: src/cli/main.cppmsgid "an older build"diff --git a/po/tether.pot b/po/tether.potindex bfab452..bd6df09 100644--- a/po/tether.pot+++ b/po/tether.pot@@ -405,8 +405,9 @@ msgstr[1] ""#: src/cli/main.cpp#, c++-formatmsgid ""-"The running tetherd is {}, but this is tether {}. Restart it with:\n"-" systemctl --user restart tetherd\n"+"The running tetherd is {}, but this is tether {}. Stop it and it\n"+"restarts on demand:\n"+" pkill tetherd\n"msgstr ""#: src/cli/main.cppdiff --git a/po/tr.po b/po/tr.poindex ce23103..896935e 100644--- a/po/tr.po+++ b/po/tr.po@@ -434,9 +434,10 @@ msgstr[1] "({} okunmamış)"#: src/cli/main.cpp#, c++-formatmsgid ""-"The running tetherd is {}, but this is tether {}. Restart it with:\n"-" systemctl --user restart tetherd\n"-msgstr "Çalışan tetherd {}, ancak bu tether {}. Şu komutla yeniden başlatın:\n systemctl --user restart tetherd\n"+"The running tetherd is {}, but this is tether {}. Stop it and it\n"+"restarts on demand:\n"+" pkill tetherd\n"+msgstr "Çalışan tetherd {}, ancak bu tether {}. Onu durdurun; gerektiğinde yeniden başlar:\n pkill tetherd\n"#: src/cli/main.cppmsgid "an older build"diff --git a/po/uk.po b/po/uk.poindex 5192b84..17f7778 100644--- a/po/uk.po+++ b/po/uk.po@@ -442,9 +442,10 @@ msgstr[2] "({} непрочитаних)"#: src/cli/main.cpp#, c++-formatmsgid ""-"The running tetherd is {}, but this is tether {}. Restart it with:\n"-" systemctl --user restart tetherd\n"-msgstr "Запущено tetherd {}, але це tether {}. Перезапустіть його командою:\n systemctl --user restart tetherd\n"+"The running tetherd is {}, but this is tether {}. Stop it and it\n"+"restarts on demand:\n"+" pkill tetherd\n"+msgstr "Запущено tetherd {}, але це tether {}. Зупиніть його — він запуститься знову за потреби:\n pkill tetherd\n"#: src/cli/main.cppmsgid "an older build"diff --git a/po/zh_CN.po b/po/zh_CN.poindex 76202e3..f5f72b8 100644--- a/po/zh_CN.po+++ b/po/zh_CN.po@@ -421,9 +421,10 @@ msgstr[0] "({} 条未读)"#: src/cli/main.cpp#, c++-formatmsgid ""-"The running tetherd is {}, but this is tether {}. Restart it with:\n"-" systemctl --user restart tetherd\n"-msgstr "正在运行的 tetherd 是 {},而此处是 tether {}。请用以下命令重启:\n systemctl --user restart tetherd\n"+"The running tetherd is {}, but this is tether {}. Stop it and it\n"+"restarts on demand:\n"+" pkill tetherd\n"+msgstr "正在运行的 tetherd 是 {},而此处是 tether {}。停止它,它会按需重新启动:\n pkill tetherd\n"#: src/cli/main.cppmsgid "an older build"diff --git a/src/cli/main.cpp b/src/cli/main.cppindex cad2533..f31b6ca 100644--- a/src/cli/main.cpp+++ b/src/cli/main.cpp@@ -674,8 +674,9 @@ static void warn_if_daemon_is_older(tether::Client& client) {// No version field at all means a daemon older than the field itself.debug::log(ERR,- _("The running tetherd is {}, but this is tether {}. Restart it with:\n"- " systemctl --user restart tetherd\n"),+ _("The running tetherd is {}, but this is tether {}. Stop it and it\n"+ "restarts on demand:\n"+ " pkill tetherd\n"),running.empty() ? _("an older build") : running.c_str(),TETHER_VERSION);}@@ -690,7 +691,6 @@ static int print_bt_contacts(tether::Client& client, const std::string& query) {resp = nlohmann::json::parse(client.send_and_wait(request.dump() + "\n"));} catch (const std::exception&) {debug::log(ERR, _("Could not read contacts from the daemon.\n"));- warn_if_daemon_is_older(client);return 1;}@@ -1017,7 +1017,13 @@ int main(int argc, char* argv[]) {debug::log(INFO, "File transfer delivered.\n");} else if (action == "native") {run_native_messaging_host(client);- } else if (action == "bt_setup") {+ }++ // The Bluetooth commands are what we're checking for version conflicts+ if (action.rfind("bt_", 0) == 0)+ warn_if_daemon_is_older(client);++ if (action == "bt_setup") {return print_bt_setup(client);} else if (action == "bt_status") {return print_bt_status(client);diff --git a/src/core/src/bluetooth/connection.cpp b/src/core/src/bluetooth/connection.cppindex d2b0817..0a4107a 100644--- a/src/core/src/bluetooth/connection.cpp+++ b/src/core/src/bluetooth/connection.cpp@@ -710,7 +710,11 @@ namespace tether::bluetooth {std::lock_guard<std::mutex> lock(g_messages_mutex);if (!g_journal.is_open() && secret::retention() != Retention::None && secret::have_key()) {if (g_journal.open()) {- contact_store() = load_contacts();+ // PBAP pull while locked lives only in memory+ if (contact_store().size() == 0)+ contact_store() = load_contacts();+ else+ save_contacts(contact_store());auto history = g_journal.load(now);for (const auto& message : history)g_messages.add(message);diff --git a/src/core/src/bluetooth/contacts.cpp b/src/core/src/bluetooth/contacts.cppindex 908125f..dd2f1d3 100644--- a/src/core/src/bluetooth/contacts.cpp+++ b/src/core/src/bluetooth/contacts.cpp@@ -251,9 +251,18 @@ namespace tether::bluetooth {const std::string destination = contacts_path(to);if (source.empty() || destination.empty() || source == destination)return false;- if (!std::filesystem::exists(source) || std::filesystem::exists(destination))+ if (!std::filesystem::exists(source))return false;+ // A destination already there means an earlier migration was interrupted between+ // the rename and the unlink.+ if (std::filesystem::exists(destination)) {+ std::error_code ec;+ std::filesystem::remove(source, ec);+ debug::log(INFO, "bluetooth: removed {}, left over from an interrupted migration", source);+ return !ec;+ }+std::ifstream in(source);if (!in.is_open())return false;diff --git a/src/core/src/bluetooth/journal.cpp b/src/core/src/bluetooth/journal.cppindex c9ebc9f..5c6b61b 100644--- a/src/core/src/bluetooth/journal.cpp+++ b/src/core/src/bluetooth/journal.cpp@@ -123,7 +123,7 @@ namespace tether::bluetooth {const std::string destination = journal_path(to);if (source.empty() || destination.empty() || source == destination)return false;- if (!std::filesystem::exists(source) || std::filesystem::exists(destination))+ if (!std::filesystem::exists(source))return false;std::ifstream in(source);@@ -152,11 +152,15 @@ namespace tether::bluetooth {std::error_code ec;std::filesystem::create_directories(std::filesystem::path(destination).parent_path(), ec);+ // A destination already in place means an earlier migration was interrupted between+ // the rename and the unlink.+ const bool merging = std::filesystem::exists(destination);const std::string tmp = destination + ".tmp";+ const std::string target = merging ? destination : tmp;{- std::ofstream out(tmp, std::ios::trunc);+ std::ofstream out(target, merging ? std::ios::app : std::ios::trunc);if (!out.is_open()) {- debug::log(ERR, "bluetooth: cannot write {}", tmp);+ debug::log(ERR, "bluetooth: cannot write {}", target);return false;}for (const auto& record : records) {@@ -166,11 +170,19 @@ namespace tether::bluetooth {out << sealed << "\n";}if (!out) {- debug::log(ERR, "bluetooth: failed writing {}", tmp);+ debug::log(ERR, "bluetooth: failed writing {}", target);return false;}}- restrict_permissions(tmp);+ restrict_permissions(target);++ if (merging) {+ sync_path(destination);+ std::filesystem::remove(source, ec);+ debug::log(+ INFO, "bluetooth: folded {} message(s) from {} into the existing journal", records.size(), source);+ return true;+ }// The source is deleted immediately after, so the replacement has to be on disk first.sync_path(tmp);diff --git a/src/core/src/secret_store.cpp b/src/core/src/secret_store.cppindex 86ba33a..5221ee1 100644--- a/src/core/src/secret_store.cpp+++ b/src/core/src/secret_store.cpp@@ -90,10 +90,17 @@ namespace tether {int64_t g_last_failure = 0;bool g_failed_once = false;- // Reads the key out of the desktop secret service without unlocking it.- bool key_from_service(std::vector<unsigned char>& out, bool& service_present) {- service_present = false;+ // What the wallet can say about the store key without being unlocked.+ enum class KeyLookup {+ Found, // the secret was readable+ Locked, // an item exists but its secret is not readable+ LockedNoKey, // no item, and the default collection is locked or missing+ Absent, // no item and the collection is unlocked: first run+ NoService, // nothing on the bus+ };+ // Reads the key out of the desktop secret service without unlocking it.+ KeyLookup key_from_service(std::vector<unsigned char>& out) {GError* err = nullptr;SecretService* service = secret_service_get_sync(SECRET_SERVICE_OPEN_SESSION, nullptr, &err);if (!service) {@@ -101,21 +108,27 @@ namespace tether {debug::log(DEBUG, "secret: no secret service on the bus: {}", err->message);g_error_free(err);}- return false;+ return KeyLookup::NoService;}- service_present = true;GHashTable* attributes = g_hash_table_new(g_str_hash, g_str_equal);g_hash_table_insert(attributes, const_cast<char*>("application"), const_cast<char*>("tether"));+ // SEARCH_ALL, so an absent item is really absent rather than just past the first hit.GList* items = secret_service_search_sync(- service, store_schema(), attributes, SECRET_SEARCH_LOAD_SECRETS, nullptr, &err);+ service,+ store_schema(),+ attributes,+ static_cast<SecretSearchFlags>(SECRET_SEARCH_ALL | SECRET_SEARCH_LOAD_SECRETS),+ nullptr,+ &err);g_hash_table_unref(attributes);- g_object_unref(service);if (err) {debug::log(WARN, "secret: search failed: {}", err->message);g_error_free(err);- return false;+ g_object_unref(service);+ // An item may well exist, so this must not fall through to generating one.+ return KeyLookup::Locked;}bool found = false;@@ -136,8 +149,27 @@ namespace tether {}secret_value_unref(value);}+ const bool any_item = items != nullptr;g_list_free_full(items, g_object_unref);- return found;++ // An item that cannot be read is locked. Do not overwrite it.+ if (found || any_item) {+ g_object_unref(service);+ return found ? KeyLookup::Found : KeyLookup::Locked;+ }++ SecretCollection* collection = secret_collection_for_alias_sync(+ service, SECRET_COLLECTION_DEFAULT, SECRET_COLLECTION_NONE, nullptr, &err);+ if (err) {+ debug::log(DEBUG, "secret: cannot reach the default collection: {}", err->message);+ g_error_free(err);+ }+ // Storing into a locked or missing collection would unlock prompt.+ const bool unlocked = collection && !secret_collection_get_locked(collection);+ if (collection)+ g_object_unref(collection);+ g_object_unref(service);+ return unlocked ? KeyLookup::Absent : KeyLookup::LockedNoKey;}bool key_to_service(const std::vector<unsigned char>& key) {@@ -215,46 +247,55 @@ namespace tether {if (g_failed_once && now_seconds() - g_last_failure < KEY_RETRY_SECONDS)return false;- bool service_present = false;+ auto wait_and_retry = [](const char* why) {+ debug::log(WARN, "{}", why);+ g_failed_once = true;+ g_last_failure = now_seconds();+ return false;+ };+ auto generate = [](std::vector<unsigned char>& key) {+ key.assign(KEY_BYTES, 0);+ return RAND_bytes(key.data(), static_cast<int>(key.size())) == 1;+ };+std::vector<unsigned char> key;- if (key_from_service(key, service_present)) {+ switch (key_from_service(key)) {+ case KeyLookup::Found:g_key = std::move(key);g_failed_once = false;return true;- }- if (!service_present && key_from_file(key)) {+ case KeyLookup::Locked:+ return wait_and_retry("secret: wallet is locked; retained history is paused until it unlocks");++ case KeyLookup::LockedNoKey:+ return wait_and_retry(+ "secret: wallet is locked and holds no store key; history starts once it unlocks");++ case KeyLookup::Absent:+ if (!generate(key))+ return false;+ if (!key_to_service(key))+ return wait_and_retry("secret: cannot store a new key in the wallet");g_key = std::move(key);g_failed_once = false;+ debug::log(INFO, "secret: generated a new store key in the wallet");return true;- }- if (service_present) {- // wallet is locked, or this is first run- std::vector<unsigned char> fresh(KEY_BYTES);- if (RAND_bytes(fresh.data(), static_cast<int>(fresh.size())) != 1)- return false;- if (key_to_service(fresh)) {- g_key = std::move(fresh);- g_failed_once = false;- debug::log(INFO, "secret: generated a new store key in the wallet");- return true;- }- debug::log(WARN, "secret: wallet is locked; retained history is paused until it unlocks");- g_failed_once = true;- g_last_failure = now_seconds();- return false;+ case KeyLookup::NoService:+ break;}- std::vector<unsigned char> fresh(KEY_BYTES);- if (RAND_bytes(fresh.data(), static_cast<int>(fresh.size())) != 1)- return false;- if (!key_to_file(fresh)) {- g_failed_once = true;- g_last_failure = now_seconds();- return false;+ if (key_from_file(key)) {+ g_key = std::move(key);+ g_failed_once = false;+ return true;}- g_key = std::move(fresh);+ if (!generate(key))+ return false;+ if (!key_to_file(key))+ return wait_and_retry("secret: cannot write the store key file");+ g_key = std::move(key);g_failed_once = false;debug::log(INFO, "secret: no secret service; store key kept in a 0600 file");return true;diff --git a/src/gtk/devices_view.cpp b/src/gtk/devices_view.cppindex 94833b1..36c2dfc 100644--- a/src/gtk/devices_view.cpp+++ b/src/gtk/devices_view.cpp@@ -10,6 +10,7 @@#include <string>#include <tether/crypto.hpp>#include <tether/discovery.hpp>+#include <tether/version.hpp>#include <vector>namespace tether::ui {@@ -229,6 +230,21 @@ namespace tether::ui {}}}++ // A package upgrade replaces the binaries but leaves the old tetherd running.+ if (!g_devices.bt_status.empty()) {+ const std::string running = g_devices.bt_status.value("version", "");+ if (running != TETHER_VERSION) {+ if (!mode.empty())+ mode += "\n";+ // No version field at all means a daemon older than the field itself.+ mode += tr_format(_("The running tetherd is {}, but this is tether {}. Stop it and it\n"+ "restarts on demand:\n"+ " pkill tetherd\n"),+ running.empty() ? _("an older build") : running.c_str(),+ TETHER_VERSION);+ }+ }set_text(g_devices.lbl_bt_mode, mode);// Only the supervised bond has live profile state; anything else candiff --git a/test/test_journal.cpp b/test/test_journal.cppindex 56baa6f..243f79f 100644--- a/test/test_journal.cpp+++ b/test/test_journal.cpp@@ -276,6 +276,59 @@ TEST(Journal, MigratesBackToPlaintextOnOptOut) {std::filesystem::remove_all(home);}+// A crash between the rename and the unlink leaves both files. The plaintext one+// used to survive every later start, which defeats the point of sealing it.+TEST(Journal, FoldsInASourceLeftBehindByAnInterruptedMigration) {+ const auto home = scratch("migrate-interrupted");+ const auto store = home / ".local" / "share" / "tether";+ const auto plain = store / "messages.ndjson";+ const auto sealed = store / "messages.ndjson.enc";+ const auto one = home / "one.ndjson";+ const auto both = home / "both.ndjson";++ // Two plaintext journals: what the migration read, and what an old daemon+ // left behind by appending to the source afterwards.+ {+ ScopedHome scoped(home, Retention::Plaintext);+ MessageJournal journal;+ ASSERT_TRUE(journal.open());+ journal.append(make("/msg/1", NOW, "one"));+ journal.close();+ std::filesystem::copy_file(plain, one);+ ASSERT_TRUE(journal.open());+ journal.append(make("/msg/2", NOW + 1, "two"));+ journal.close();+ std::filesystem::copy_file(plain, both);+ }++ // Migrate the first one for real, then put the source back as a crash+ // between the rename and the unlink would have left it.+ std::filesystem::remove(plain);+ std::filesystem::copy_file(one, plain);+ {+ ScopedHome scoped(home);+ MessageJournal journal;+ ASSERT_TRUE(journal.open());+ }+ ASSERT_TRUE(std::filesystem::exists(sealed));+ std::filesystem::copy_file(both, plain);++ {+ ScopedHome scoped(home);+ MessageJournal journal;+ ASSERT_TRUE(journal.open());++ EXPECT_FALSE(std::filesystem::exists(plain)) << "the plaintext copy outlived the fold";++ auto loaded = journal.load(NOW + 1);+ ASSERT_EQ(loaded.size(), 2u) << "the duplicate handle was kept, or the extra message was lost";+ EXPECT_EQ(loaded[0].body, "one");+ EXPECT_EQ(loaded[1].body, "two");+ }++ std::filesystem::remove_all(home);+}+// The regression that matters most. A locked wallet used to mean an empty read,// and the compact that follows a read would then write that emptiness back over// every message the user had.diff --git a/test/test_vcard.cpp b/test/test_vcard.cppindex 8bfd485..ce12f18 100644--- a/test/test_vcard.cpp+++ b/test/test_vcard.cpp@@ -299,6 +299,25 @@ TEST(ContactStore, MigratesAPlaintextCacheOnLoad) {EXPECT_TRUE(std::filesystem::exists(scoped.store() / "contacts.json.enc"));}+// A crash between the rename and the unlink leaves both files, and the plaintext+// phonebook used to stay on disk from then on.+TEST(ContactStore, RemovesASourceLeftBehindByAnInterruptedMigration) {+ ScopedStore scoped("migrate-interrupted");+ ASSERT_TRUE(save_contacts(three_contacts()));+ ASSERT_TRUE(std::filesystem::exists(scoped.store() / "contacts.json.enc"));++ // The plaintext cache the interrupted migration never unlinked.+ {+ secret::set_retention(Retention::Plaintext);+ ASSERT_TRUE(save_contacts(three_contacts()));+ ASSERT_TRUE(std::filesystem::exists(scoped.store() / "contacts.json"));+ }++ secret::set_retention(Retention::Encrypted);+ EXPECT_EQ(load_contacts().name_for("tel:+15551234567"), "Ada Lovelace") << "the sealed cache was disturbed";+ EXPECT_FALSE(std::filesystem::exists(scoped.store() / "contacts.json")) << "the plaintext copy is still on disk";+}+TEST(ContactStore, RetentionNoneWritesNothing) {ScopedStore scoped("none", Retention::None);EXPECT_FALSE(save_contacts(three_contacts()));