imsd/interfaces/ImsdBus.cppm
Jorijn van der Graaf d0cfaf712d
imsd, engine, packaging: received short messages are kept until an app deletes them — List and Delete on the Messaging interface
A received message went to a bus signal and nowhere else but DUMP_SIP's
opt-in raw dump of the last MESSAGE outside a call's dialog, so with no
app listening it was gone. It is now written
to <state dir>/sms/<id>.msg — mode 0600 in a 0700 directory, whole to a
.tmp, linked to its name and the directory fsynced; the engine's FormatStoredSms/ParseStoredSms
record codec, pinned by tests/Engine — before the signal, and stays until
Delete. List hands over the records it can read, by arrival second, then
within a run by sequence number. The record is named by
the message's id and published with link(2), which refuses a name the
store already holds, so the next number is tried and nothing is
replaced. link(2) refuses only a name the store
still holds: a client that deleted the daemon's copy after importing it
freed the name, and the run's token in the id is what keeps a later run
from giving it to another text, unless that run draws the same token and
meets the same second and number. The two methods sit behind the
Messaging polkit action with Send, and the policy text says so; GetStatus,
the open read, does not count the store, since the count would tell any
local account when a text arrived, and the unit makes the state
directory 0700 (StateDirectoryMode) for the same reason.

For a message the store cannot write the delivery report stays an
RP-ACK: the signal is its only delivery, and the journal says so. The
protocol's refusal —
RP-ERROR "memory capacity exceeded", TS 24.011 cause 22 — is half of a
mechanism: the other half, the UE's memory-available notification
(RP-SMMA, TS 24.341 5.3.2.5), imsd does not send; a refused last segment
would also lose its set. The
refusal was built and benched on KPN — the store made unwritable, the
text refused; it came again five minutes later, the store writable by
then, and a later text came at once — and taken out again; no other core
is measured. It returns
with the notification and a probe of the store. The record's directory is
fsynced after the link, its result unchecked like the directory open's; a
concatenated text's earlier segments are held in memory, not in the
store, until the set is whole (§7.9).

Tested on the dev phone (KPN): a text from the phone to its own number on
2026-10-18 23:30, under this publish (a build of this range ahead of this
history, 735dfda, whose StoreReceivedSms has this commit's code), stored
as
ims-sms-in-1792359045-1, an id of that build's shape; the store directory listed on 2026-10-19 holds
that record and no temporary (the fp6 journal's 2026-10-17-sms-over-ip-bench
capture: r20-tip-smoke-2026-10-18-2329, r22-store-backup-and-delete-proof).
The Exists path has no bench: a run's own ids never repeat. The refusal's bench (store-refused-2026-10-18-0136): the store directory
made immutable at 01:37, a text to the phone's own number not stored
(EPERM on its .tmp), the RP-ERROR answered 202, the text delivered again
at 01:42:56 under its first SC time stamp, stored and acknowledged, a later
text not held. Not on the phone: List's skip of a file that is no record
and four of Delete's answers ran on the workstation, in dev mode on a
private session bus, on this commit's host build (2026-11-19; its sha256
begins 8fdd9192866645d5): the skip, with its journal line; InvalidArgs
for a string that is no id; IOError, the
record still listed, for a record in a store directory made unwritable;
the removal once it was writable; FileNotFound for an id with no record.

The engine's start sweeps the .tmp names a crash left — one it cannot
unlink stays, unreported — so a deleted text's second name goes at the
next engine start that can remove it (Delete leaves .tmp
names alone). A Delete whose removal fails answers IOError with the system's
reason, and a record that is there is kept; FileNotFound is left for a
removal that found no file.
List's order breaks a tie between two texts of one run in one second by
the sequence number (pinned), not by the id as text.
2026-11-19 16:43:16 +01:00

117 lines
5.6 KiB
C++

// SPDX-License-Identifier: GPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
// lint-disable-file fixed-width-types no-char-pointer
/*
ImsdBus — what the two system-bus daemons (imsd, imsd-ofonod) share on the
bus side: the polkit gate in front of call control and messaging.
The bus policies let any local account reach both names, so the session
account can dial whatever it is called; whether a caller may place, answer
or end a call is polkit's net.catcrafts.IMS1.Voice, and whether it may send
a short message, subscribe to the message signals or list and delete
the stored ones net.catcrafts.IMS1.Messaging
(packaging/net.catcrafts.IMS1.policy), both shaped like ModemManager's
Voice action: the active local session and root, not an ssh login, an
inactive session or a system service account. Reads (status, the call
list, Messaging's GetStatus) stay open, as ModemManager's are to every local account, and the
call signals reach them all; the message signals go to the
connections Subscribe admitted through the same gate, a submission's
outcome also to the connection that called Send.
GLib, so not part of the GLib-free imsd-core.
*/
module;
#include <gio/gio.h>
export module ImsdBus;
import std;
namespace imsd::bus {
// CheckAuthorization's AllowUserInteraction, as ModemManager passes it: a
// rule an administrator sets to auth_self prompts through the session's
// polkit agent.
constexpr guint32 AllowUserInteraction = 1;
struct Pending {
GDBusMethodInvocation* inv;
GDBusInterfaceMethodCallFunc body;
const char* deniedError;
void (*log)(std::string_view);
const char* action;
};
void OnCheck(GObject* src, GAsyncResult* res, gpointer data) {
std::unique_ptr<Pending> p(static_cast<Pending*>(data));
GDBusMethodInvocation* inv = p->inv;
GError* err = nullptr;
GVariant* r = g_dbus_connection_call_finish(G_DBUS_CONNECTION(src), res, &err);
gboolean authorized = FALSE;
gboolean challenge = FALSE;
if (r) {
GVariant* details = nullptr;
g_variant_get(r, "((bb@a{ss}))", &authorized, &challenge, &details);
g_variant_unref(details);
g_variant_unref(r);
}
if (authorized) {
p->body(g_dbus_method_invocation_get_connection(inv), g_dbus_method_invocation_get_sender(inv),
g_dbus_method_invocation_get_object_path(inv), g_dbus_method_invocation_get_interface_name(inv),
g_dbus_method_invocation_get_method_name(inv), g_dbus_method_invocation_get_parameters(inv), inv,
g_dbus_method_invocation_get_user_data(inv));
return;
}
std::string why = err ? std::format("{}: polkit did not answer: {}", g_dbus_method_invocation_get_sender(inv), err->message)
: std::format("{} is not authorized for {}", g_dbus_method_invocation_get_sender(inv), p->action);
p->log(std::format("{} refused: {}", g_dbus_method_invocation_get_method_name(inv), why));
g_dbus_method_invocation_return_dbus_error(inv, p->deniedError, why.c_str());
if (err) g_error_free(err);
}
} // namespace imsd::bus
export namespace imsd::bus {
// The polkit actions: call control, and messaging — sending a short
// message over IP, subscribing to the message signals, listing and
// deleting the stored ones.
inline constexpr char VoiceAction[] = "net.catcrafts.IMS1.Voice";
inline constexpr char MessagingAction[] = "net.catcrafts.IMS1.Messaging";
// Off for a --session run: the session bus has no polkit, and its callers
// are the owner's own processes.
inline bool AuthorizeCalls = true;
// Run `body` for the call `inv` once polkit allows its sender `action`
// (net.catcrafts.IMS1.Voice unless told otherwise); otherwise answer
// `deniedError` and log why. The check is asynchronous — nothing else on
// the main loop waits for it.
void Authorize(GDBusMethodInvocation* inv, GDBusInterfaceMethodCallFunc body, const char* deniedError, void (*log)(std::string_view), const char* action = VoiceAction) {
GVariantBuilder subject;
g_variant_builder_init(&subject, G_VARIANT_TYPE("a{sv}"));
g_variant_builder_add(&subject, "{sv}", "name", g_variant_new_string(g_dbus_method_invocation_get_sender(inv)));
GVariantBuilder details;
g_variant_builder_init(&details, G_VARIANT_TYPE("a{ss}"));
g_dbus_connection_call(g_dbus_method_invocation_get_connection(inv), "org.freedesktop.PolicyKit1",
"/org/freedesktop/PolicyKit1/Authority", "org.freedesktop.PolicyKit1.Authority",
"CheckAuthorization",
g_variant_new("((sa{sv})sa{ss}us)", "system-bus-name", &subject, action, &details, AllowUserInteraction, ""),
G_VARIANT_TYPE("((bba{ss}))"), G_DBUS_CALL_FLAGS_NONE, G_MAXINT, nullptr, OnCheck,
new Pending{ inv, body, deniedError, log, action });
}
// A vtable method handler for `Body` that lets `IsRead` methods through and
// puts every other method — call control, and anything added later — behind
// Authorize for `Action` (the Voice action unless the interface names its
// own, as the Messaging interface does).
template <GDBusInterfaceMethodCallFunc Body, bool (*IsRead)(std::string_view), const char* DeniedError, void (*Log)(std::string_view), const char* Action = VoiceAction>
void Gated(GDBusConnection* conn, const gchar* sender, const gchar* path, const gchar* iface, const gchar* method, GVariant* params, GDBusMethodInvocation* inv, gpointer user) {
if (!AuthorizeCalls || IsRead(method)) {
Body(conn, sender, path, iface, method, params, inv, user);
return;
}
Authorize(inv, Body, DeniedError, Log, Action);
}
} // namespace imsd::bus