Add support for GPG signature enforcement on booted
[petitboot] / ui / ncurses / nc-cui.c
index c975c0f126e0095bc0f65db3b29d2dadd65835e0..09b63b053f25638176b1837332b88dab387be110 100644 (file)
@@ -75,10 +75,15 @@ static void cui_start(void)
         * 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;
@@ -420,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))
@@ -533,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);
@@ -556,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);
@@ -794,13 +806,15 @@ static int menu_lang_execute(struct pmenu_item *item)
 
 static int menu_reinit_execute(struct pmenu_item *item)
 {
-       cui_send_reinit(cui_from_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)
 {
-       cui_show_add_url(cui_from_item(item));
+       if (cui_from_item(item)->client)
+               cui_show_add_url(cui_from_item(item));
        return 0;
 }
 
@@ -890,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.
@@ -901,7 +956,7 @@ 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;
@@ -922,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)
@@ -931,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);
 
@@ -949,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__);