Add support for GPG signature enforcement on booted
[petitboot] / ui / ncurses / nc-cui.c
index cd7952f860545e90cdf1245035b0b7b69868a57a..09b63b053f25638176b1837332b88dab387be110 100644 (file)
 #include "nc-cui.h"
 #include "nc-boot-editor.h"
 #include "nc-config.h"
+#include "nc-add-url.h"
 #include "nc-sysinfo.h"
+#include "nc-lang.h"
 #include "nc-helpscreen.h"
+#include "nc-subset.h"
+
+extern const struct help_text main_menu_help_text;
+
+static struct pmenu *main_menu_init(struct cui *cui);
 
 static void cui_start(void)
 {
@@ -63,6 +70,21 @@ static void cui_start(void)
         */
        define_key("\x1b[Z", KEY_BTAB);
 
+       /* We'll define a few other keys too since they're commonly
+        * used for navigation but the escape character will cause
+        * Petitboot to exit if they're left undefined */
+       define_key("\x1b\x5b\x35\x7e", KEY_PPAGE);
+       define_key("\x1b\x5b\x36\x7e", KEY_NPAGE);
+       define_key("\x1b\x5b\x31\x7e", KEY_HOME);
+       define_key("\x1b\x5b\x34\x7e", KEY_END);
+       define_key("\x1b\x4f\x48", KEY_HOME);
+       define_key("\x1b\x4f\x46", KEY_END);
+       define_key("OH", KEY_HOME);
+       define_key("OF", KEY_END);
+       define_key("\x1b\x5b\x41", KEY_UP);
+       define_key("\x1b\x5b\x42", KEY_DOWN);
+       define_key("\x1b\x5b\x33\x7e", KEY_DC);
+
        while (getch() != ERR)          /* flush stdin */
                (void)0;
 }
@@ -167,6 +189,7 @@ static void cui_boot_editor_on_exit(struct cui *cui,
 {
        struct pmenu *menu = cui->main;
        struct cui_opt_data *cod;
+       int idx, top, rows, cols;
        static int user_idx = 0;
 
        /* Was the edit cancelled? */
@@ -205,6 +228,22 @@ static void cui_boot_editor_on_exit(struct cui *cui,
 
                /* Re-attach the items array. */
                set_menu_items(menu->ncm, menu->items);
+
+               /* If our index is above the current top row, align
+                * us to the new top. Otherwise, align us to the new
+                * bottom */
+               menu_format(cui->main->ncm, &rows, &cols);
+               top = top_row(cui->main->ncm);
+               idx = item_index(item->nci);
+
+               if (top >= idx)
+                       top = idx;
+               else
+                       top = idx < rows ? 0 : idx - rows + 1;
+
+               set_top_row(cui->main->ncm, top);
+               set_current_item(item->pmenu->ncm, item->nci);
+
                nc_scr_post(&menu->scr);
        } else {
                cod = item->data;
@@ -212,7 +251,6 @@ static void cui_boot_editor_on_exit(struct cui *cui,
 
        cod->bd = talloc_steal(cod, bd);
 
-       set_current_item(item->pmenu->ncm, item->nci);
 out:
        cui_set_current(cui, &cui->main->scr);
        talloc_free(cui->boot_editor);
@@ -263,6 +301,32 @@ void cui_show_config(struct cui *cui)
        cui_set_current(cui, config_screen_scr(cui->config_screen));
 }
 
+static void cui_lang_exit(struct cui *cui)
+{
+       cui_set_current(cui, &cui->main->scr);
+       talloc_free(cui->lang_screen);
+       cui->lang_screen = NULL;
+}
+
+void cui_show_lang(struct cui *cui)
+{
+       cui->lang_screen = lang_screen_init(cui, cui->config, cui_lang_exit);
+       cui_set_current(cui, lang_screen_scr(cui->lang_screen));
+}
+
+static void cui_add_url_exit(struct cui *cui)
+{
+       cui_set_current(cui, &cui->main->scr);
+       talloc_free(cui->add_url_screen);
+       cui->add_url_screen = NULL;
+}
+
+void cui_show_add_url(struct cui *cui)
+{
+       cui->add_url_screen = add_url_screen_init(cui, cui_add_url_exit);
+       cui_set_current(cui, add_url_screen_scr(cui->add_url_screen));
+}
+
 static void cui_help_exit(struct cui *cui)
 {
        cui_set_current(cui, help_screen_return_scr(cui->help_screen));
@@ -286,6 +350,29 @@ void cui_show_help(struct cui *cui, const char *title,
                cui_set_current(cui, help_screen_scr(cui->help_screen));
 }
 
+static void cui_subset_exit(struct cui *cui)
+{
+       cui_set_current(cui, subset_screen_return_scr(cui->subset_screen));
+       talloc_free(cui->subset_screen);
+       cui->subset_screen = NULL;
+}
+
+void cui_show_subset(struct cui *cui, const char *title,
+                    void *arg)
+{
+       if (!cui->current)
+               return;
+
+       if (cui->subset_screen)
+               return;
+
+       cui->subset_screen = subset_screen_init(cui, cui->current,
+                       title, arg, cui_subset_exit);
+
+       if (cui->subset_screen)
+               cui_set_current(cui, subset_screen_scr(cui->subset_screen));
+}
+
 /**
  * cui_set_current - Set the currently active screen and redraw it.
  */
@@ -338,10 +425,15 @@ static int cui_process_key(void *arg)
                        break;
 
                if (!cui->has_input) {
-                       pb_log("UI input received (key = %d), aborting "
-                                       "default boot\n", c);
-                       discover_client_cancel_default(cui->client);
                        cui->has_input = true;
+                       if (cui->client) {
+                               pb_log("UI input received (key = %d), aborting "
+                                       "default boot\n", c);
+                               discover_client_cancel_default(cui->client);
+                       } else {
+                               pb_log("UI input received (key = %d), aborting "
+                                       "once server connects\n", c);
+                       }
                }
 
                if (process_global_keys(cui, c))
@@ -451,6 +543,7 @@ static int cui_boot_option_add(struct device *dev, struct boot_option *opt,
        cod->bd->initrd = talloc_strdup(cod->bd, opt->initrd_file);
        cod->bd->dtb = talloc_strdup(cod->bd, opt->dtb_file);
        cod->bd->args = talloc_strdup(cod->bd, opt->boot_args);
+       cod->bd->args_sig_file = talloc_strdup(cod->bd, opt->args_sig_file);
 
        /* This disconnects items array from menu. */
        result = set_menu_items(cui->main->ncm, NULL);
@@ -474,6 +567,7 @@ static int cui_boot_option_add(struct device *dev, struct boot_option *opt,
        pb_log("   image  '%s'\n", cod->bd->image);
        pb_log("   initrd '%s'\n", cod->bd->initrd);
        pb_log("   args   '%s'\n", cod->bd->args);
+       pb_log("   argsig '%s'\n", cod->bd->args_sig_file);
 
        /* Re-attach the items array. */
        result = set_menu_items(cui->main->ncm, cui->main->items);
@@ -496,7 +590,7 @@ static int cui_boot_option_add(struct device *dev, struct boot_option *opt,
                /* If our index is above the current top row, align
                 * us to the new top. Otherwise, align us to the new
                 * bottom */
-               top = top < idx ? idx - rows : idx;
+               top = top < idx ? idx - rows + 1 : idx;
 
                set_top_row(cui->main->ncm, top);
                set_current_item(cui->main->ncm, selected);
@@ -520,6 +614,7 @@ static void cui_device_remove(struct device *dev, void *arg)
        struct cui *cui = cui_from_arg(arg);
        struct boot_option *opt;
        unsigned int i;
+       int rows, cols, top, last;
        int result;
 
        pb_log("%s: %p %s\n", __func__, dev, dev->id);
@@ -556,6 +651,15 @@ static void cui_device_remove(struct device *dev, void *arg)
 
        result = set_menu_items(cui->main->ncm, cui->main->items);
 
+       /* Move cursor to 'Exit' menu entry */
+       menu_format(cui->main->ncm, &rows, &cols);
+       last = cui->main->item_count - 1;
+       set_current_item(cui->main->ncm, cui->main->items[last]);
+       if (!item_visible(cui->main->items[last])) {
+               top = last < rows ? 0 : last - rows + 1;
+               set_top_row(cui->main->ncm, top);
+       }
+
        if (result)
                pb_log("%s: set_menu_items failed: %d\n", __func__, result);
 
@@ -606,6 +710,9 @@ static void cui_update_sysinfo(struct system_info *sysinfo, void *arg)
        if (cui->sysinfo_screen)
                sysinfo_screen_update(cui->sysinfo_screen, sysinfo);
 
+       if (cui->subset_screen)
+               subset_screen_update(cui->subset_screen);
+
        /* ... and do the same with the config screen... */
        if (cui->config_screen)
                config_screen_update(cui->config_screen, cui->config, sysinfo);
@@ -617,18 +724,51 @@ static void cui_update_sysinfo(struct system_info *sysinfo, void *arg)
        cui_update_mm_title(cui);
 }
 
+static void cui_update_language(struct cui *cui, char *lang)
+{
+       bool repost_menu;
+       char *cur_lang;
+
+       cur_lang = setlocale(LC_ALL, NULL);
+       if (cur_lang && !strcmp(cur_lang, lang))
+               return;
+
+       setlocale(LC_ALL, lang);
+
+       /* we'll need to update the menu: drop all items and repopulate */
+       repost_menu = cui->current == &cui->main->scr;
+       if (repost_menu)
+               nc_scr_unpost(cui->current);
+
+       talloc_free(cui->main);
+       cui->main = main_menu_init(cui);
+
+       if (repost_menu) {
+               cui->current = &cui->main->scr;
+               nc_scr_post(cui->current);
+       }
+
+       discover_client_enumerate(cui->client);
+}
+
 static void cui_update_config(struct config *config, void *arg)
 {
        struct cui *cui = cui_from_arg(arg);
        cui->config = talloc_steal(cui, config);
 
+       if (config->lang)
+               cui_update_language(cui, config->lang);
+
+       if (cui->subset_screen)
+               subset_screen_update(cui->subset_screen);
+
        if (cui->config_screen)
                config_screen_update(cui->config_screen, config, cui->sysinfo);
 
        if (config->safe_mode)
                nc_scr_status_printf(cui->current,
-                               "SAFE MODE: select '%s' to continue",
-                               "Rescan devices");
+                               _("SAFE MODE: select '%s' to continue"),
+                               _("Rescan devices"));
 }
 
 int cui_send_config(struct cui *cui, struct config *config)
@@ -636,11 +776,125 @@ int cui_send_config(struct cui *cui, struct config *config)
        return discover_client_send_config(cui->client, config);
 }
 
+int cui_send_url(struct cui *cui, char * url)
+{
+       return discover_client_send_url(cui->client, url);
+}
+
 void cui_send_reinit(struct cui *cui)
 {
        discover_client_send_reinit(cui->client);
 }
 
+static int menu_sysinfo_execute(struct pmenu_item *item)
+{
+       cui_show_sysinfo(cui_from_item(item));
+       return 0;
+}
+
+static int menu_config_execute(struct pmenu_item *item)
+{
+       cui_show_config(cui_from_item(item));
+       return 0;
+}
+
+static int menu_lang_execute(struct pmenu_item *item)
+{
+       cui_show_lang(cui_from_item(item));
+       return 0;
+}
+
+static int menu_reinit_execute(struct pmenu_item *item)
+{
+       if (cui_from_item(item)->client)
+               cui_send_reinit(cui_from_item(item));
+       return 0;
+}
+
+static int menu_add_url_execute(struct pmenu_item *item)
+{
+       if (cui_from_item(item)->client)
+               cui_show_add_url(cui_from_item(item));
+       return 0;
+}
+
+/**
+ * pb_mm_init - Setup the main menu instance.
+ */
+static struct pmenu *main_menu_init(struct cui *cui)
+{
+       struct pmenu_item *i;
+       struct pmenu *m;
+       int result;
+
+       m = pmenu_init(cui, 7, cui_on_exit);
+       if (!m) {
+               pb_log("%s: failed\n", __func__);
+               return NULL;
+       }
+
+       m->on_new = cui_item_new;
+
+       m->scr.frame.ltitle = talloc_asprintf(m,
+               "Petitboot (" PACKAGE_VERSION ")");
+       m->scr.frame.rtitle = NULL;
+       m->scr.frame.help = talloc_strdup(m,
+               _("Enter=accept, e=edit, n=new, x=exit, l=language, h=help"));
+       m->scr.frame.status = talloc_strdup(m, _("Welcome to Petitboot"));
+
+       /* add a separator */
+       i = pmenu_item_create(m, " ");
+       item_opts_off(i->nci, O_SELECTABLE);
+       pmenu_item_insert(m, i, 0);
+
+       /* add system items */
+       i = pmenu_item_create(m, _("System information"));
+       i->on_execute = menu_sysinfo_execute;
+       pmenu_item_insert(m, i, 1);
+
+       i = pmenu_item_create(m, _("System configuration"));
+       i->on_execute = menu_config_execute;
+       pmenu_item_insert(m, i, 2);
+
+       /* this label isn't translated, so we don't want a gettext() here */
+       i = pmenu_item_create(m, "Language");
+       i->on_execute = menu_lang_execute;
+       pmenu_item_insert(m, i, 3);
+
+       i = pmenu_item_create(m, _("Rescan devices"));
+       i->on_execute = menu_reinit_execute;
+       pmenu_item_insert(m, i, 4);
+
+       i = pmenu_item_create(m, _("Retrieve config from URL"));
+       i->on_execute = menu_add_url_execute;
+       pmenu_item_insert(m, i, 5);
+
+       i = pmenu_item_create(m, _("Exit to shell"));
+       i->on_execute = pmenu_exit_cb;
+       pmenu_item_insert(m, i, 6);
+
+       result = pmenu_setup(m);
+
+       if (result) {
+               pb_log("%s:%d: pmenu_setup failed: %s\n", __func__, __LINE__,
+                       strerror(errno));
+               goto fail_setup;
+       }
+
+       m->help_title = _("main menu");
+       m->help_text = &main_menu_help_text;
+
+       menu_opts_off(m->ncm, O_SHOWDESC);
+       set_menu_mark(m->ncm, " *");
+       set_current_item(m->ncm, i->nci);
+
+       return m;
+
+fail_setup:
+       talloc_free(m);
+       return NULL;
+}
+
 static struct discover_client_ops cui_client_ops = {
        .device_add = NULL,
        .boot_option_add = cui_boot_option_add,
@@ -650,6 +904,47 @@ static struct discover_client_ops cui_client_ops = {
        .update_config = cui_update_config,
 };
 
+/* cui_server_wait - Connect to the discover server.
+ * @arg: Pointer to the cui instance.
+ *
+ * A timeout callback that attempts to connect to the discover server; on
+ * failure it registers itself with a one second timeout to try again.
+ * On success the cui->client struct will be set.
+ *
+ * Since this updates the status line when called it must not be called
+ * before the UI is ready.
+ */
+static int cui_server_wait(void *arg)
+{
+       struct cui *cui = cui_from_arg(arg);
+
+       if (cui->client) {
+               pb_debug("We already have a server!\n");
+               return 0;
+       }
+
+       /* We haven't yet connected to the server */
+       pb_log("Trying to connect...\n");
+       cui->client = discover_client_init(cui->waitset,
+                       &cui_client_ops, cui);
+
+       if (!cui->client) {
+               waiter_register_timeout(cui->waitset, 1000, cui_server_wait,
+                                       cui);
+               nc_scr_status_printf(cui->current, "Info: Waiting for server");
+       } else {
+               nc_scr_status_printf(cui->current, "Info: Connected to server!");
+               talloc_steal(cui, cui->client);
+
+               if (cui->has_input) {
+                       pb_log("Aborting default boot on server connect\n");
+                       discover_client_cancel_default(cui->client);
+               }
+       }
+
+       return 0;
+}
+
 /**
  * cui_init - Setup the cui instance.
  * @platform_info: A value for the struct cui platform_info member.
@@ -661,13 +956,12 @@ static struct discover_client_ops cui_client_ops = {
  */
 
 struct cui *cui_init(void* platform_info,
-       int (*js_map)(const struct js_event *e), int start_deamon)
+       int (*js_map)(const struct js_event *e), int start_daemon, int timeout)
 {
        struct cui *cui;
        unsigned int i;
 
        cui = talloc_zero(NULL, struct cui);
-
        if (!cui) {
                pb_log("%s: alloc cui failed.\n", __func__);
                fprintf(stderr, _("%s: alloc cui failed.\n"), __func__);
@@ -683,7 +977,7 @@ struct cui *cui_init(void* platform_info,
        /* Loop here for scripts that just started the server. */
 
 retry_start:
-       for (i = start_deamon ? 2 : 10; i; i--) {
+       for (i = start_daemon ? 2 : 15; i && timeout; i--) {
                cui->client = discover_client_init(cui->waitset,
                                &cui_client_ops, cui);
                if (cui->client || !i)
@@ -692,10 +986,10 @@ retry_start:
                sleep(1);
        }
 
-       if (!cui->client && start_deamon) {
+       if (!cui->client && start_daemon) {
                int result;
 
-               start_deamon = 0;
+               start_daemon = 0;
 
                result = pb_start_daemon(cui);
 
@@ -710,7 +1004,12 @@ retry_start:
                goto fail_client_init;
        }
 
-       if (!cui->client) {
+       if (!cui->client && !timeout) {
+               /* Have the first timeout fire immediately so we can check
+                * for the server as soon as the UI is ready */
+               waiter_register_timeout(cui->waitset, 0,
+                                       cui_server_wait, cui);
+       } else if (!cui->client) {
                pb_log("%s: discover_client_init failed.\n", __func__);
                fprintf(stderr, _("%s: error: discover_client_init failed.\n"),
                        __func__);
@@ -723,6 +1022,10 @@ retry_start:
        talloc_steal(cui, cui->client);
        cui_start();
 
+       cui->main = main_menu_init(cui);
+       if (!cui->main)
+               goto fail_client_init;
+
        waiter_register_io(cui->waitset, STDIN_FILENO, WAIT_IN,
                        cui_process_key, cui);
 
@@ -753,13 +1056,12 @@ fail_alloc:
  * Returns 0 on success (return to shell), -1 on error (should restart).
  */
 
-int cui_run(struct cui *cui, struct pmenu *main, unsigned int default_item)
+int cui_run(struct cui *cui)
 {
        assert(main);
 
-       cui->main = main;
        cui->current = &cui->main->scr;
-       cui->default_item = default_item;
+       cui->default_item = 0;
 
        nc_scr_post(cui->current);