Skip to content

Linux + iPhone Continuity / iMessage / SMS

pairing dialog

Zack Bartelb800ea8a72b0
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index a32232e..3b12140 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -2,3 +2,4 @@ add_subdirectory(core)
add_subdirectory(daemon)
add_subdirectory(cli)
add_subdirectory(gtk)
+add_subdirectory(dialog)
diff --git a/src/core/include/tether/net.hpp b/src/core/include/tether/net.hpp
index 879177a..6f71966 100644
--- a/src/core/include/tether/net.hpp
+++ b/src/core/include/tether/net.hpp
@@ -4,6 +4,7 @@
#include <map>
#include <openssl/ssl.h>
#include <string>
+#include <sys/types.h>
namespace tether {
@@ -50,6 +51,9 @@ namespace tether {
private:
void handle_accept(int fd);
void handle_client(int client_fd);
+ void spawn_pair_dialog(int client_fd, SSL* ssl,
+ const std::string& fingerprint,
+ const std::string& device_name);
EpollEventLoop& loop_;
int server_fd_ = -1;
@@ -58,6 +62,16 @@ namespace tether {
std::map<int, SSL*> active_ssl_;
std::map<int, bool> ssl_handshake_complete_;
std::map<int, bool> client_paired_;
+
+ // Track spawned dialog child processes
+ struct PendingPairDialog {
+ pid_t pid;
+ int client_fd;
+ std::string fingerprint;
+ std::string device_name;
+ };
+ // keyed by the pipe read-fd we monitor in epoll
+ std::map<int, PendingPairDialog> pending_dialogs_;
};
} // namespace tether
diff --git a/src/core/src/net.cpp b/src/core/src/net.cpp
index fad2fe8..54a642a 100644
--- a/src/core/src/net.cpp
+++ b/src/core/src/net.cpp
@@ -7,6 +7,7 @@
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/un.h>
+#include <sys/wait.h>
#include <unistd.h>
#include "tether/file_transfer.hpp"
@@ -387,6 +388,9 @@ namespace tether {
resp["command"] = "pair_pending";
std::string payload = resp.dump() + "\n";
robust_ssl_write(ssl, payload.c_str(), payload.size());
+
+ // Launch the layer-shell dialog for interactive approval
+ spawn_pair_dialog(client_fd, ssl, print, dev_name);
} else {
std::string rej = "{\"command\":\"error\",\"message\":\"unauthorized\"}\n";
robust_ssl_write(ssl, rej.c_str(), rej.size());
@@ -453,4 +457,140 @@ namespace tether {
}
}
+ void TcpServer::spawn_pair_dialog(int client_fd, SSL* ssl,
+ const std::string& fingerprint,
+ const std::string& device_name) {
+ // Create a pipe so the parent can detect when the child exits via epoll
+ int pipefd[2];
+ if (pipe(pipefd) < 0) {
+ std::cerr << "spawn_pair_dialog: pipe() failed: " << std::strerror(errno) << std::endl;
+ return;
+ }
+
+ pid_t pid = fork();
+ if (pid < 0) {
+ std::cerr << "spawn_pair_dialog: fork() failed: " << std::strerror(errno) << std::endl;
+ close(pipefd[0]);
+ close(pipefd[1]);
+ return;
+ }
+
+ if (pid == 0) {
+ // --- Child process ---
+ close(pipefd[0]); // close read end
+ // The write end stays open; it will close when this process exits,
+ // signaling EOF to the parent's read end.
+
+ // Build body text with truncated fingerprint for readability
+ std::string short_fp = fingerprint;
+ if (short_fp.size() > 16) {
+ short_fp = short_fp.substr(0, 16) + "...";
+ }
+ std::string body = device_name + " (" + short_fp + ") wants to pair with this device.";
+
+ // Try alongside the daemon binary first, then fall back to PATH
+ std::filesystem::path self_path;
+ try { self_path = std::filesystem::read_symlink("/proc/self/exe"); } catch (...) {}
+ std::string sibling = (self_path.parent_path() / "tether-dialog").string();
+
+ execl(sibling.c_str(), "tether-dialog",
+ "--title", "Pairing Request",
+ "--body", body.c_str(),
+ "--accept", "Accept",
+ "--reject", "Reject",
+ "--timeout", "60",
+ nullptr);
+ // If sibling path failed, try PATH
+ execlp("tether-dialog", "tether-dialog",
+ "--title", "Pairing Request",
+ "--body", body.c_str(),
+ "--accept", "Accept",
+ "--reject", "Reject",
+ "--timeout", "60",
+ nullptr);
+ // exec failed entirely
+ std::cerr << "spawn_pair_dialog: exec failed: " << std::strerror(errno) << std::endl;
+ _exit(3);
+ }
+
+ // --- Parent process ---
+ close(pipefd[1]); // close write end
+
+ // Set read end non-blocking for epoll
+ int flags = fcntl(pipefd[0], F_GETFL, 0);
+ fcntl(pipefd[0], F_SETFL, flags | O_NONBLOCK);
+
+ pending_dialogs_[pipefd[0]] = {pid, client_fd, fingerprint, device_name};
+
+ loop_.addFd(pipefd[0], [this](int read_fd) {
+ // Pipe became readable → child exited (EOF on write end)
+ char dummy;
+ read(read_fd, &dummy, 1); // consume EOF
+
+ auto it = pending_dialogs_.find(read_fd);
+ if (it == pending_dialogs_.end()) {
+ loop_.removeFd(read_fd);
+ close(read_fd);
+ return;
+ }
+
+ PendingPairDialog info = it->second;
+ pending_dialogs_.erase(it);
+ loop_.removeFd(read_fd);
+ close(read_fd);
+
+ int status = 0;
+ waitpid(info.pid, &status, 0);
+ int exit_code = WIFEXITED(status) ? WEXITSTATUS(status) : 3;
+
+ if (exit_code == 0) {
+ // User accepted — trust the device
+ Crypto::instance().add_known_host(info.device_name, info.fingerprint);
+ std::cout << "[Pairing Accepted] " << info.device_name
+ << " (" << info.fingerprint << ")" << std::endl;
+
+ // Mark as paired if still connected
+ if (client_paired_.count(info.client_fd)) {
+ client_paired_[info.client_fd] = true;
+ }
+
+ // Notify the remote client
+ if (active_ssl_.count(info.client_fd)) {
+ nlohmann::json resp;
+ resp["command"] = "pair_accepted";
+ std::string payload = resp.dump() + "\n";
+ robust_ssl_write(active_ssl_[info.client_fd], payload.c_str(), payload.size());
+ }
+
+ // Clean up pending_pairs.json
+ std::string pending_path = get_runtime_dir() + "/pending_pairs.json";
+ nlohmann::json pending;
+ std::ifstream ifs(pending_path);
+ if (ifs.is_open()) {
+ try { pending = nlohmann::json::parse(ifs); } catch (...) {}
+ ifs.close();
+ }
+ if (pending.contains(info.fingerprint)) {
+ pending.erase(info.fingerprint);
+ std::ofstream ofs(pending_path);
+ ofs << pending.dump(4);
+ }
+ } else {
+ std::cout << "[Pairing Rejected] " << info.device_name
+ << " (exit code " << exit_code << ")" << std::endl;
+
+ // Notify the remote client
+ if (active_ssl_.count(info.client_fd)) {
+ nlohmann::json resp;
+ resp["command"] = "pair_rejected";
+ std::string payload = resp.dump() + "\n";
+ robust_ssl_write(active_ssl_[info.client_fd], payload.c_str(), payload.size());
+ }
+ }
+ });
+
+ std::cout << "spawn_pair_dialog: Launched dialog (pid " << pid << ") for "
+ << device_name << std::endl;
+ }
+
} // namespace tether
diff --git a/src/dialog/CMakeLists.txt b/src/dialog/CMakeLists.txt
new file mode 100644
index 0000000..b78a211
--- /dev/null
+++ b/src/dialog/CMakeLists.txt
@@ -0,0 +1,28 @@
+find_package(PkgConfig REQUIRED)
+pkg_check_modules(GTK3 REQUIRED gtk+-3.0)
+pkg_check_modules(LAYER_SHELL REQUIRED gtk-layer-shell-0)
+
+add_executable(tether-dialog
+ main.cpp
+)
+
+target_include_directories(tether-dialog PRIVATE
+ ${GTK3_INCLUDE_DIRS}
+ ${LAYER_SHELL_INCLUDE_DIRS}
+)
+
+target_link_libraries(tether-dialog PRIVATE
+ ${GTK3_LIBRARIES}
+ ${LAYER_SHELL_LIBRARIES}
+)
+
+target_compile_options(tether-dialog PRIVATE
+ ${GTK3_CFLAGS_OTHER}
+ ${LAYER_SHELL_CFLAGS_OTHER}
+)
+
+# Output into root build dir
+set_target_properties(tether-dialog PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}")
+
+install(TARGETS tether-dialog
+ RUNTIME DESTINATION bin)
diff --git a/src/dialog/main.cpp b/src/dialog/main.cpp
new file mode 100644
index 0000000..d90b286
--- /dev/null
+++ b/src/dialog/main.cpp
@@ -0,0 +1,212 @@
+#include <gtk/gtk.h>
+#include <gtk-layer-shell.h>
+
+#include <cstdlib>
+#include <cstring>
+#include <iostream>
+#include <string>
+
+// Exit codes:
+// 0 = accept button clicked
+// 1 = reject button clicked
+// 2 = timeout expired
+// 3 = error (bad args, display failure, etc.)
+static int exit_code = 3;
+
+static void on_accept(GtkWidget*, gpointer) {
+ exit_code = 0;
+ gtk_main_quit();
+}
+
+static void on_reject(GtkWidget*, gpointer) {
+ exit_code = 1;
+ gtk_main_quit();
+}
+
+static gboolean on_timeout(gpointer) {
+ exit_code = 2;
+ gtk_main_quit();
+ return FALSE;
+}
+
+static gboolean on_key_press(GtkWidget*, GdkEventKey* event, gpointer has_reject) {
+ if (event->keyval == GDK_KEY_Return || event->keyval == GDK_KEY_KP_Enter) {
+ exit_code = 0;
+ gtk_main_quit();
+ return TRUE;
+ }
+ if (event->keyval == GDK_KEY_Escape && GPOINTER_TO_INT(has_reject)) {
+ exit_code = 1;
+ gtk_main_quit();
+ return TRUE;
+ }
+ return FALSE;
+}
+
+static void print_usage(const char* argv0) {
+ std::cerr << "Usage: " << argv0 << " --title <text> --body <text> --accept <label>"
+ << " [--reject <label>] [--timeout <seconds>]\n";
+}
+
+static const char* CSS = R"(
+ window {
+ background-color: transparent;
+ }
+ .dialog-frame {
+ background-color: rgba(24, 24, 32, 0.92);
+ border: 1px solid rgba(255, 255, 255, 0.08);
+ border-radius: 16px;
+ padding: 28px 32px;
+ margin: 12px;
+ }
+ .dialog-title {
+ font-size: 16px;
+ font-weight: 700;
+ color: #e8e8ef;
+ margin-bottom: 10px;
+ }
+ .dialog-body {
+ font-size: 13px;
+ color: #a0a0b0;
+ margin-bottom: 22px;
+ }
+ .dialog-btn-row {
+ margin-top: 4px;
+ }
+ .btn-accept {
+ background: linear-gradient(135deg, #6366f1, #818cf8);
+ color: white;
+ border: none;
+ border-radius: 8px;
+ padding: 8px 22px;
+ font-weight: 600;
+ font-size: 13px;
+ min-width: 90px;
+ }
+ .btn-accept:hover {
+ background: linear-gradient(135deg, #818cf8, #a5b4fc);
+ }
+ .btn-reject {
+ background-color: rgba(255, 255, 255, 0.06);
+ color: #a0a0b0;
+ border: 1px solid rgba(255, 255, 255, 0.1);
+ border-radius: 8px;
+ padding: 8px 22px;
+ font-weight: 600;
+ font-size: 13px;
+ min-width: 90px;
+ }
+ .btn-reject:hover {
+ background-color: rgba(255, 255, 255, 0.12);
+ color: #e8e8ef;
+ }
+)";
+
+int main(int argc, char** argv) {
+ std::string title, body, accept_label, reject_label;
+ int timeout_secs = 0;
+
+ for (int i = 1; i < argc; ++i) {
+ std::string arg = argv[i];
+ if (arg == "--title" && i + 1 < argc) {
+ title = argv[++i];
+ } else if (arg == "--body" && i + 1 < argc) {
+ body = argv[++i];
+ } else if (arg == "--accept" && i + 1 < argc) {
+ accept_label = argv[++i];
+ } else if (arg == "--reject" && i + 1 < argc) {
+ reject_label = argv[++i];
+ } else if (arg == "--timeout" && i + 1 < argc) {
+ timeout_secs = std::atoi(argv[++i]);
+ } else if (arg == "--help" || arg == "-h") {
+ print_usage(argv[0]);
+ return 0;
+ }
+ }
+
+ if (title.empty() || body.empty() || accept_label.empty()) {
+ std::cerr << "Error: --title, --body, and --accept are required.\n";
+ print_usage(argv[0]);
+ return 3;
+ }
+
+ gtk_init(&argc, &argv);
+
+ // Apply CSS
+ GtkCssProvider* css_provider = gtk_css_provider_new();
+ gtk_css_provider_load_from_data(css_provider, CSS, -1, NULL);
+ gtk_style_context_add_provider_for_screen(
+ gdk_screen_get_default(),
+ GTK_STYLE_PROVIDER(css_provider),
+ GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
+ g_object_unref(css_provider);
+
+ // Create window
+ GtkWidget* window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
+ gtk_window_set_default_size(GTK_WINDOW(window), 380, -1);
+ gtk_window_set_decorated(GTK_WINDOW(window), FALSE);
+
+ // Layer shell setup
+ gtk_layer_init_for_window(GTK_WINDOW(window));
+ gtk_layer_set_layer(GTK_WINDOW(window), GTK_LAYER_SHELL_LAYER_OVERLAY);
+ gtk_layer_set_keyboard_mode(GTK_WINDOW(window), GTK_LAYER_SHELL_KEYBOARD_MODE_EXCLUSIVE);
+ gtk_layer_set_namespace(GTK_WINDOW(window), "tether-dialog");
+
+ // Center on screen (no anchors = centered)
+
+ // Main container
+ GtkWidget* frame = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
+ GtkStyleContext* frame_ctx = gtk_widget_get_style_context(frame);
+ gtk_style_context_add_class(frame_ctx, "dialog-frame");
+ gtk_container_add(GTK_CONTAINER(window), frame);
+
+ // Title
+ GtkWidget* lbl_title = gtk_label_new(title.c_str());
+ gtk_label_set_xalign(GTK_LABEL(lbl_title), 0.0);
+ gtk_label_set_line_wrap(GTK_LABEL(lbl_title), TRUE);
+ gtk_style_context_add_class(gtk_widget_get_style_context(lbl_title), "dialog-title");
+ gtk_box_pack_start(GTK_BOX(frame), lbl_title, FALSE, FALSE, 0);
+
+ // Body
+ GtkWidget* lbl_body = gtk_label_new(body.c_str());
+ gtk_label_set_xalign(GTK_LABEL(lbl_body), 0.0);
+ gtk_label_set_line_wrap(GTK_LABEL(lbl_body), TRUE);
+ gtk_label_set_max_width_chars(GTK_LABEL(lbl_body), 50);
+ gtk_style_context_add_class(gtk_widget_get_style_context(lbl_body), "dialog-body");
+ gtk_box_pack_start(GTK_BOX(frame), lbl_body, FALSE, FALSE, 0);
+
+ // Button row
+ GtkWidget* btn_row = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 10);
+ gtk_style_context_add_class(gtk_widget_get_style_context(btn_row), "dialog-btn-row");
+ gtk_widget_set_halign(btn_row, GTK_ALIGN_END);
+ gtk_box_pack_start(GTK_BOX(frame), btn_row, FALSE, FALSE, 0);
+
+ bool has_reject = !reject_label.empty();
+
+ if (has_reject) {
+ GtkWidget* btn_reject = gtk_button_new_with_label(reject_label.c_str());
+ gtk_style_context_add_class(gtk_widget_get_style_context(btn_reject), "btn-reject");
+ g_signal_connect(btn_reject, "clicked", G_CALLBACK(on_reject), NULL);
+ gtk_box_pack_start(GTK_BOX(btn_row), btn_reject, FALSE, FALSE, 0);
+ }
+
+ GtkWidget* btn_accept = gtk_button_new_with_label(accept_label.c_str());
+ gtk_style_context_add_class(gtk_widget_get_style_context(btn_accept), "btn-accept");
+ g_signal_connect(btn_accept, "clicked", G_CALLBACK(on_accept), NULL);
+ gtk_box_pack_start(GTK_BOX(btn_row), btn_accept, FALSE, FALSE, 0);
+
+ // Keyboard shortcuts
+ g_signal_connect(window, "key-press-event", G_CALLBACK(on_key_press), GINT_TO_POINTER(has_reject));
+
+ // Auto-dismiss timeout
+ if (timeout_secs > 0) {
+ g_timeout_add_seconds(timeout_secs, on_timeout, NULL);
+ }
+
+ g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
+
+ gtk_widget_show_all(window);
+ gtk_main();
+
+ return exit_code;
+}