diff --git a/linux/CMakeLists.txt b/linux/CMakeLists.txt index 90a0e5a..1749498 100644 --- a/linux/CMakeLists.txt +++ b/linux/CMakeLists.txt @@ -1,4 +1,5 @@ -# The Linux (and BSD) app: GTK 4, libadwaita and VTE over libdmodemctl. +# The Linux (and BSD) app: GTK 4, libadwaita and VTE over libdmodemctl +# (and libdmodemssh for the Remote page's SSH). add_executable(dmodem-gui src/main.c src/window.c @@ -9,7 +10,7 @@ add_executable(dmodem-gui src/voice.c src/remote.c src/audio.c) -target_link_libraries(dmodem-gui PRIVATE dmodemctl PkgConfig::GTK) +target_link_libraries(dmodem-gui PRIVATE dmodemctl dmodemssh PkgConfig::GTK) # the sound of voice calls: PulseAudio's client (PipeWire serves it # too); without it the app has no sound pkg_check_modules(PULSE IMPORTED_TARGET libpulse-mainloop-glib) @@ -28,13 +29,6 @@ if(SNDFILE_FOUND) else() message(STATUS "libsndfile not found: greetings are read from WAV, AIFF and .au files only") endif() -# forkpty(), for the Remote page's ssh: in libutil on the BSDs and older -# glibc, in libc itself since glibc 2.34 -include(CheckFunctionExists) -check_function_exists(forkpty HAVE_FORKPTY_IN_LIBC) -if(NOT HAVE_FORKPTY_IN_LIBC) - target_link_libraries(dmodem-gui PRIVATE util) -endif() target_compile_options(dmodem-gui PRIVATE -Wall -Wextra -Wno-unused-parameter) include(GNUInstallDirs) diff --git a/linux/src/remote.c b/linux/src/remote.c index dd60660..856316a 100644 --- a/linux/src/remote.c +++ b/linux/src/remote.c @@ -6,9 +6,11 @@ * rlogin, in a terminal of its own, beside the modem's * * Telnet and rlogin are a TCP connection made here and libdmodemctl's - * dmc_net for the protocol. SSH is the system's ssh in a pty of this - * page's own, so that its host keys, agent and config are the user's. - * Either way the terminal only shows bytes and gives what is typed. + * dmc_net for the protocol. SSH is libdmodemssh (the apps' own client, + * on libssh) on a thread of its own, its events brought to the main + * loop; it reads OpenSSH's config, known_hosts, keys and agent, so that + * they are the user's. Either way the terminal only shows bytes and + * gives what is typed. */ #include #include @@ -16,21 +18,11 @@ #include #include #include -#include #include -#include #include -#if defined(__linux__) -#include -#elif defined(__FreeBSD__) || defined(__DragonFly__) -#include -#else -#include -#endif - -#include #include "app.h" +#include "dmodemssh.h" /* the protocols in the drop-down's order */ static const int protos[] = { DMC_NET_SSH, DMC_NET_TELNET, DMC_NET_RLOGIN }; @@ -50,10 +42,12 @@ typedef struct Remote { GSource *in_src, *out_src; dmc_net *net; GByteArray *pending; - /* SSH: the pty's master, ssh's pid */ - int fd; - GPid pid; - guint fd_watch, child_watch, size_tick; + /* SSH: the connection (its thread polls it), the shell's channel, + the logins tried, a question's dialog */ + struct Ssh *ssh; + int shell, tried, asks, exit_status; + AdwDialog *dialog; + guint size_tick; } Remote; static Remote *R; @@ -85,6 +79,8 @@ static void show(Remote *r, const char *data, gssize n) vte_terminal_feed(r->term, data, n); } +static void ssh_disconnect(Remote *r); + /* the connection over, with why (NULL: the user's doing) */ static void ended(Remote *r, const char *why) { @@ -111,14 +107,10 @@ static void ended(Remote *r, const char *why) g_clear_pointer(&r->net, dmc_net_free); if (r->pending) g_byte_array_set_size(r->pending, 0); - if (r->fd_watch) - g_source_remove(r->fd_watch); - r->fd_watch = 0; - if (r->fd >= 0) - close(r->fd); - r->fd = -1; - if (r->pid > 0) - kill(r->pid, SIGHUP); + /* (the connection is freed once its thread has said it's closed) */ + if (r->ssh) + ssh_disconnect(r); + r->shell = 0; if (r->size_tick) g_source_remove(r->size_tick); r->size_tick = 0; @@ -249,100 +241,323 @@ static void connected(GObject *src, GAsyncResult *res, gpointer data) } /* - * SSH: the system's ssh in a pty + * SSH: libdmodemssh, polled on a thread of its own */ -static gboolean pty_readable(int fd, GIOCondition c, gpointer data) +/* a connection and the thread that polls it */ +typedef struct Ssh { + dmc_ssh *s; + GThread *thread; +} Ssh; + +/* one of its events, brought to the main loop */ +typedef struct SshEv { + Remote *r; + Ssh *ssh; + int type, channel, value, len; + char *data; +} SshEv; + +static void ssh_ev_free(gpointer p) +{ + SshEv *e = p; + g_free(e->data); + g_free(e); +} + +static gboolean ssh_event(gpointer p); + +/* the connection polled until it's over, each event to the main loop */ +static gpointer ssh_poll(gpointer p) +{ + SshEv *start = p; + Remote *r = start->r; + Ssh *ssh = start->ssh; + dmc_ssh_event ev; + int n; + + g_free(start); + while ((n = dmc_ssh_poll(ssh->s, 1000, &ev)) >= 0) { + if (n == 0) + continue; + SshEv *e = g_new0(SshEv, 1); + e->r = r; + e->ssh = ssh; + e->type = ev.type; + e->channel = ev.channel; + e->value = ev.value; + e->len = ev.len; + e->data = ev.data ? g_memdup2(ev.data, (gsize)ev.len + 1) : NULL; + g_main_context_invoke_full(NULL, G_PRIORITY_DEFAULT, ssh_event, + e, ssh_ev_free); + if (ev.type == DMC_SSH_EV_CLOSED) + break; + } + return NULL; +} + +/* a dialog of the login's closed: its answer, or none (cancel) */ +static void dialog_gone(Remote *r) +{ + if (r->dialog) { + AdwDialog *d = r->dialog; + r->dialog = NULL; + adw_dialog_force_close(d); + } +} + +/* the host key dialog answered */ +static void hostkey_answered(AdwAlertDialog *d, const char *response, gpointer data) { Remote *r = data; - char buf[8192]; - ssize_t n = read(fd, buf, sizeof(buf)); - if (n > 0) { - show(r, buf, n); - return G_SOURCE_CONTINUE; + Ssh *ssh = g_object_get_data(G_OBJECT(d), "ssh"); + (void)d; + r->dialog = NULL; + if (ssh != r->ssh) + return; + dmc_ssh_trust(ssh->s, !strcmp(response, "connect"), 1); +} + +/* the server's key: known goes on; new is asked; changed is refused */ +static void ssh_hostkey(Remote *r, const char *key, int known) +{ + char fp[128] = ""; + dmc_ssh_fingerprint(key, fp, sizeof(fp)); + if (known == DMC_SSH_HOST_KNOWN) { + dmc_ssh_trust(r->ssh->s, 1, 0); + return; + } + if (known == DMC_SSH_HOST_CHANGED || known == DMC_SSH_HOST_OTHER_TYPE) { + char *m = g_strdup_printf( + "\r\n\x1b[1;31mThe server's host key has changed (it is now %.*s %s).\r\n" + "Someone may be in the middle; or the server has a new key. If that is\r\n" + "expected, remove its old line from ~/.ssh/known_hosts.\x1b[0m\r\n", + (int)strcspn(key, " "), key, fp); + show(r, m, -1); + g_free(m); + dmc_ssh_trust(r->ssh->s, 0, 0); + return; + } + char *body = g_strdup_printf( + "This server is not known yet. Its key's fingerprint is\n\n%.*s %s\n\n" + "If it's the one you expect, it will be added to ~/.ssh/known_hosts.", + (int)strcspn(key, " "), key, fp); + AdwDialog *d = adw_alert_dialog_new("Connect to a new server?", body); + g_free(body); + adw_alert_dialog_add_responses(ADW_ALERT_DIALOG(d), "cancel", "Cancel", + "connect", "Connect", NULL); + adw_alert_dialog_set_response_appearance(ADW_ALERT_DIALOG(d), "connect", + ADW_RESPONSE_SUGGESTED); + adw_alert_dialog_set_default_response(ADW_ALERT_DIALOG(d), "connect"); + adw_alert_dialog_set_close_response(ADW_ALERT_DIALOG(d), "cancel"); + g_object_set_data(G_OBJECT(d), "ssh", r->ssh); + g_signal_connect(d, "response", G_CALLBACK(hostkey_answered), r); + r->dialog = d; + adw_dialog_present(d, GTK_WIDGET(r->a->window)); +} + +/* a password or the server's questions answered (or cancelled) */ +static void answers_given(AdwAlertDialog *d, const char *response, gpointer data) +{ + Remote *r = data; + Ssh *ssh = g_object_get_data(G_OBJECT(d), "ssh"); + GPtrArray *entries = g_object_get_data(G_OBJECT(d), "entries"); + int prompts = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(d), "prompts")); + r->dialog = NULL; + if (ssh != r->ssh) + return; + if (strcmp(response, "ok")) { + dmc_ssh_auth_cancel(ssh->s); + return; + } + if (!prompts) { + dmc_ssh_auth_password(ssh->s, gtk_editable_get_text( + GTK_EDITABLE(g_ptr_array_index(entries, 0)))); + return; + } + for (guint i = 0; i < entries->len; i++) + dmc_ssh_answer(ssh->s, (int)i, gtk_editable_get_text( + GTK_EDITABLE(g_ptr_array_index(entries, i)))); + dmc_ssh_answered(ssh->s); +} + +/* a dialog asking for a password (prompts 0) or the server's questions */ +static void ask(Remote *r, const char *heading, const char *body, int prompts) +{ + AdwDialog *d = adw_alert_dialog_new(heading, body && *body ? body : NULL); + GtkWidget *box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 6); + GPtrArray *entries = g_ptr_array_new(); + int n = prompts ? prompts : 1; + for (int i = 0; i < n; i++) { + int echo = 0; + const char *q = prompts ? dmc_ssh_prompt(r->ssh->s, i, &echo) : "Password"; + GtkWidget *e = echo ? gtk_entry_new() : gtk_password_entry_new(); + if (echo) + gtk_entry_set_placeholder_text(GTK_ENTRY(e), q); + else + g_object_set(e, "placeholder-text", q, NULL); + if (prompts) { + GtkWidget *l = gtk_label_new(q); + gtk_label_set_xalign(GTK_LABEL(l), 0); + gtk_label_set_wrap(GTK_LABEL(l), TRUE); + gtk_box_append(GTK_BOX(box), l); + } + gtk_box_append(GTK_BOX(box), e); + g_ptr_array_add(entries, e); + /* (Enter answers) */ + g_object_set(e, "activates-default", TRUE, NULL); + } + adw_alert_dialog_set_extra_child(ADW_ALERT_DIALOG(d), box); + adw_alert_dialog_add_responses(ADW_ALERT_DIALOG(d), "cancel", "Cancel", + "ok", "Log In", NULL); + adw_alert_dialog_set_response_appearance(ADW_ALERT_DIALOG(d), "ok", + ADW_RESPONSE_SUGGESTED); + adw_alert_dialog_set_default_response(ADW_ALERT_DIALOG(d), "ok"); + adw_alert_dialog_set_close_response(ADW_ALERT_DIALOG(d), "cancel"); + g_object_set_data(G_OBJECT(d), "ssh", r->ssh); + g_object_set_data_full(G_OBJECT(d), "entries", entries, + (GDestroyNotify)g_ptr_array_unref); + g_object_set_data(G_OBJECT(d), "prompts", GINT_TO_POINTER(prompts)); + g_signal_connect(d, "response", G_CALLBACK(answers_given), r); + r->dialog = d; + adw_dialog_present(d, GTK_WIDGET(r->a->window)); + gtk_widget_grab_focus(g_ptr_array_index(entries, 0)); +} + +/* what's tried, in OpenSSH's order: keys and the agent, then the + server's questions or a password (three times at most) */ +enum { TRIED_KEYS = 1 }; + +static void ssh_auth(Remote *r, int methods, const char *why) +{ + Ssh *ssh = r->ssh; + if ((methods & DMC_SSH_AUTH_PUBLICKEY) && !(r->tried & TRIED_KEYS)) { + r->tried |= TRIED_KEYS; + dmc_ssh_auth_auto(ssh->s); + return; + } + if (r->asks++ >= 3) { + dmc_ssh_auth_cancel(ssh->s); + return; + } + const char *body = r->asks > 1 && *why ? why : NULL; + char *who = g_strdup_printf("Log in to %s", gtk_editable_get_text( + GTK_EDITABLE(r->host))); + if (methods & DMC_SSH_AUTH_INTERACTIVE) + dmc_ssh_auth_interactive(ssh->s); + else if (methods & DMC_SSH_AUTH_PASSWORD) + ask(r, who, body, 0); + else + dmc_ssh_auth_cancel(ssh->s); + g_free(who); +} + +/* an event of the connection's, on the main loop */ +static gboolean ssh_event(gpointer p) +{ + SshEv *e = p; + Remote *r = e->r; + Ssh *ssh = e->ssh; + + if (e->type == DMC_SSH_EV_CLOSED) { + /* (its thread has ended: the connection can go) */ + g_thread_join(ssh->thread); + dmc_ssh_free(ssh->s); + g_free(ssh); + if (ssh != r->ssh) + return G_SOURCE_REMOVE; + r->ssh = NULL; + dialog_gone(r); + if (!e->data && r->exit_status > 0) { + char *why = g_strdup_printf("the shell exited with status %d", + r->exit_status); + ended(r, why); + g_free(why); + } else { + ended(r, e->data); + } + return G_SOURCE_REMOVE; + } + if (ssh != r->ssh) + return G_SOURCE_REMOVE; + switch (e->type) { + case DMC_SSH_EV_HOSTKEY: + ssh_hostkey(r, e->data, e->value); + break; + case DMC_SSH_EV_AUTH: + ssh_auth(r, e->value, e->data); + break; + case DMC_SSH_EV_PROMPTS: + if (e->value == 0) { + dmc_ssh_answered(ssh->s); + } else { + ask(r, e->data && *e->data ? e->data : "The server asks", + dmc_ssh_instruction(ssh->s), e->value); + } + break; + case DMC_SSH_EV_READY: + if (e->data && *e->data) { + show(r, e->data, e->len); + } + r->shell = dmc_ssh_shell(ssh->s, "xterm-256color", r->cols, r->rows, 0); + set_status(r, "Connected by SSH"); + break; + case DMC_SSH_EV_OPEN: + if (!e->value) { + show(r, "\r\n[The server refused the shell]\r\n", -1); + dmc_ssh_disconnect(ssh->s); + } + break; + case DMC_SSH_EV_DATA: + if (e->channel == r->shell) + show(r, e->data, e->len); + break; + case DMC_SSH_EV_EXIT: + if (e->channel == r->shell) + r->exit_status = e->value; + break; + case DMC_SSH_EV_CLOSE: + /* (the shell's over: so is the connection) */ + if (e->channel == r->shell) + dmc_ssh_disconnect(ssh->s); + break; + case DMC_SSH_EV_INCOMING: + /* (nothing was asked for that opens these) */ + dmc_ssh_close(ssh->s, e->channel); + break; } - if (n < 0 && (errno == EAGAIN || errno == EINTR)) - return G_SOURCE_CONTINUE; - /* (EIO: ssh has gone and the pty is shut; its exit says why) */ - r->fd_watch = 0; return G_SOURCE_REMOVE; } -static void child_exited(GPid pid, int status, gpointer data) +/* the connection closed by the user: its thread ends with it */ +static void ssh_disconnect(Remote *r) { - Remote *r = data; - g_spawn_close_pid(pid); - r->child_watch = 0; - if (r->pid != pid) - return; - r->pid = 0; - /* what is left in the pty first */ - if (r->fd >= 0) { - char buf[8192]; - ssize_t n; - while ((n = read(r->fd, buf, sizeof(buf))) > 0) - show(r, buf, n); - } - char *why = WIFEXITED(status) && WEXITSTATUS(status) ? - g_strdup_printf("ssh exited with status %d", WEXITSTATUS(status)) : - g_strdup("Connection closed"); - ended(r, why); - g_free(why); -} - -static void set_pty_size(Remote *r) -{ - struct winsize ws = { 0 }; - ws.ws_col = (unsigned short)r->cols; - ws.ws_row = (unsigned short)r->rows; - ioctl(r->fd, TIOCSWINSZ, &ws); + dialog_gone(r); + dmc_ssh_disconnect(r->ssh->s); } static gboolean start_ssh(Remote *r, const char *host, int port, const char *user) { - char *ssh = g_find_program_in_path("ssh"); - if (!ssh) { - window_toast(r->a, "ssh was not found: install OpenSSH's client"); + dmc_ssh *s = dmc_ssh_new(host, port, *user ? user : g_get_user_name()); + if (!s) { + window_toast(r->a, "Could not start SSH"); return FALSE; } - char ps[16]; - g_snprintf(ps, sizeof(ps), "%d", port); - struct winsize ws = { 0 }; - ws.ws_col = (unsigned short)r->cols; - ws.ws_row = (unsigned short)r->rows; - int fd; - pid_t pid = forkpty(&fd, NULL, NULL, &ws); - if (pid < 0) { - g_free(ssh); - window_toast(r->a, "Could not make a pty for ssh"); - return FALSE; - } - if (pid == 0) { - /* the child: ssh, with VTE's terminal type */ - const char *argv[10]; - int i = 0; - argv[i++] = "ssh"; - argv[i++] = "-p"; - argv[i++] = ps; - if (*user) { - argv[i++] = "-l"; - argv[i++] = user; - } - argv[i++] = "--"; - argv[i++] = host; - argv[i] = NULL; - setenv("TERM", "xterm-256color", 1); - execv(ssh, (char **)argv); - _exit(127); - } - g_free(ssh); - g_unix_set_fd_nonblocking(fd, TRUE, NULL); - r->fd = fd; - r->pid = pid; - r->fd_watch = g_unix_fd_add(fd, G_IO_IN | G_IO_HUP | G_IO_ERR, - pty_readable, r); - r->child_watch = g_child_watch_add(pid, child_exited, r); + /* OpenSSH's own config and known hosts: the user's */ + dmc_ssh_option(s, "config", ""); + dmc_ssh_option(s, "knownhosts", ""); + Ssh *ssh = g_new0(Ssh, 1); + ssh->s = s; + r->ssh = ssh; + r->shell = 0; + r->tried = 0; + r->asks = 0; + r->exit_status = 0; + SshEv *start = g_new0(SshEv, 1); + start->r = r; + start->ssh = ssh; + ssh->thread = g_thread_new("ssh", ssh_poll, start); return TRUE; } @@ -363,8 +578,8 @@ static gboolean size_check(gpointer data) if (r->net && r->sock) { dmc_net_resize(r->net, c, w); flush(r); - } else if (r->fd >= 0) { - set_pty_size(r); + } else if (r->ssh && r->shell > 0) { + dmc_ssh_resize(r->ssh->s, r->shell, c, w); } return G_SOURCE_CONTINUE; } @@ -375,10 +590,8 @@ static void committed(VteTerminal *t, char *text, guint size, gpointer data) (void)t; if (!r->running) return; - if (r->fd >= 0) { - /* (a pty takes small writes whole) */ - ssize_t w = write(r->fd, text, size); - (void)w; + if (r->ssh && r->shell > 0) { + dmc_ssh_write(r->ssh->s, r->shell, text, (int)size); } else if (r->net && r->sock) { dmc_net_type(r->net, text, (int)size); flush(r); @@ -459,7 +672,6 @@ static void connect_clicked(GtkWidget *w, gpointer data) g_free(host); return; } - set_status(r, "Connected by SSH"); } else { r->net = dmc_net_new(proto, user, "xterm-256color", r->cols, r->rows); @@ -491,7 +703,6 @@ GtkWidget *remote_page_new(App *a) Remote *r = g_new0(Remote, 1); R = r; r->a = a; - r->fd = -1; r->pending = g_byte_array_new(); GtkWidget *box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);