]> git.ozlabs.org Git - petitboot/blobdiff - discover/device-handler.c
discover: Reject ADD_URL actions without configured network
[petitboot] / discover / device-handler.c
index c21eb286d2bd8b47d361fad1c537c883e50fdbbc..120f11f52aa819639ece6a1bffaa49d0772921ae 100644 (file)
@@ -5,6 +5,7 @@
 #include <string.h>
 #include <errno.h>
 #include <mntent.h>
 #include <string.h>
 #include <errno.h>
 #include <mntent.h>
+#include <locale.h>
 #include <sys/stat.h>
 #include <sys/wait.h>
 #include <sys/mount.h>
 #include <sys/stat.h>
 #include <sys/wait.h>
 #include <sys/mount.h>
 #include <system/system.h>
 #include <process/process.h>
 #include <url/url.h>
 #include <system/system.h>
 #include <process/process.h>
 #include <url/url.h>
+#include <i18n/i18n.h>
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netdb.h>
+#include <arpa/inet.h>
 
 #include "device-handler.h"
 #include "discover-server.h"
 
 #include "device-handler.h"
 #include "discover-server.h"
@@ -59,6 +66,8 @@ static int umount_device(struct discover_device *dev);
 static int device_handler_init_sources(struct device_handler *handler);
 static void device_handler_reinit_sources(struct device_handler *handler);
 
 static int device_handler_init_sources(struct device_handler *handler);
 static void device_handler_reinit_sources(struct device_handler *handler);
 
+static void device_handler_update_lang(const char *lang);
+
 void discover_context_add_boot_option(struct discover_context *ctx,
                struct discover_boot_option *boot_option)
 {
 void discover_context_add_boot_option(struct discover_context *ctx,
                struct discover_boot_option *boot_option)
 {
@@ -285,6 +294,9 @@ struct device_handler *device_handler_init(struct discover_server *server,
 
        parser_init();
 
 
        parser_init();
 
+       if (config_get()->safe_mode)
+               return handler;
+
        rc = device_handler_init_sources(handler);
        if (rc) {
                talloc_free(handler);
        rc = device_handler_init_sources(handler);
        if (rc) {
                talloc_free(handler);
@@ -344,6 +356,11 @@ void device_handler_remove(struct device_handler *handler,
                talloc_free(opt);
        }
 
                talloc_free(opt);
        }
 
+       /* if this is a network device, we have to unregister it from the
+        * network code */
+       if (device->device->type == DEVICE_TYPE_NETWORK)
+               network_unregister_device(handler->network, device);
+
        handler->n_devices--;
        memmove(&handler->devices[i], &handler->devices[i + 1],
                (handler->n_devices - i) * sizeof(handler->devices[0]));
        handler->n_devices--;
        memmove(&handler->devices[i], &handler->devices[i + 1],
                (handler->n_devices - i) * sizeof(handler->devices[0]));
@@ -373,7 +390,7 @@ static void countdown_status(struct device_handler *handler,
        status.progress = -1;
        status.detail = NULL;
        status.message = talloc_asprintf(handler,
        status.progress = -1;
        status.detail = NULL;
        status.message = talloc_asprintf(handler,
-                       "Booting in %d sec: %s", sec, opt->option->name);
+                       _("Booting in %d sec: %s"), sec, opt->option->name);
 
        discover_server_notify_boot_status(handler->server, &status);
 
 
        discover_server_notify_boot_status(handler->server, &status);
 
@@ -436,6 +453,22 @@ static int default_option_priority(struct discover_boot_option *opt)
        return 0;
 }
 
        return 0;
 }
 
+static bool device_allows_default(struct discover_device *dev)
+{
+       const char *dev_str;
+
+       dev_str = config_get()->boot_device;
+
+       if (!dev_str || !strlen(dev_str))
+               return true;
+
+       /* default devices are specified by UUIDs at present */
+       if (strcmp(dev->uuid, dev_str))
+               return false;
+
+       return true;
+}
+
 static void set_default(struct device_handler *handler,
                struct discover_boot_option *opt)
 {
 static void set_default(struct device_handler *handler,
                struct discover_boot_option *opt)
 {
@@ -444,6 +477,10 @@ static void set_default(struct device_handler *handler,
        if (!handler->autoboot_enabled)
                return;
 
        if (!handler->autoboot_enabled)
                return;
 
+       /* do we allow default-booting from this device? */
+       if (!device_allows_default(opt->device))
+               return;
+
        new_prio = default_option_priority(opt);
 
        /* A negative priority indicates that we don't want to boot this device
        new_prio = default_option_priority(opt);
 
        /* A negative priority indicates that we don't want to boot this device
@@ -646,6 +683,8 @@ void device_handler_add_device(struct device_handler *handler,
                                struct discover_device *, handler->n_devices);
        handler->devices[handler->n_devices - 1] = device;
 
                                struct discover_device *, handler->n_devices);
        handler->devices[handler->n_devices - 1] = device;
 
+       if (device->device->type == DEVICE_TYPE_NETWORK)
+               network_register_device(handler->network, device);
 }
 
 /* Start discovery on a hotplugged device. The device will be in our devices
 }
 
 /* Start discovery on a hotplugged device. The device will be in our devices
@@ -779,7 +818,7 @@ void device_handler_cancel_default(struct device_handler *handler)
        status.type = BOOT_STATUS_INFO;
        status.progress = -1;
        status.detail = NULL;
        status.type = BOOT_STATUS_INFO;
        status.progress = -1;
        status.detail = NULL;
-       status.message = "Default boot cancelled";
+       status.message = _("Default boot cancelled");
 
        discover_server_notify_boot_status(handler->server, &status);
 }
 
        discover_server_notify_boot_status(handler->server, &status);
 }
@@ -787,13 +826,168 @@ void device_handler_cancel_default(struct device_handler *handler)
 void device_handler_update_config(struct device_handler *handler,
                struct config *config)
 {
 void device_handler_update_config(struct device_handler *handler,
                struct config *config)
 {
-       config_set(config);
+       int rc;
+
+       rc = config_set(config);
+       if (rc)
+               return;
+
        discover_server_notify_config(handler->server, config);
        discover_server_notify_config(handler->server, config);
+       device_handler_update_lang(config->lang);
        device_handler_reinit(handler);
 }
 
        device_handler_reinit(handler);
 }
 
+static char *device_from_addr(void *ctx, struct pb_url *url)
+{
+       char *ipaddr, *buf, *tok, *dev = NULL;
+       const char *delim = " ";
+       struct sockaddr_in *ip;
+       struct sockaddr_in si;
+       struct addrinfo *res;
+       struct process *p;
+       int rc;
+
+       /* Note: IPv4 only */
+       rc = inet_pton(AF_INET, url->host, &(si.sin_addr));
+       if (rc > 0) {
+               ipaddr = url->host;
+       } else {
+               /* need to turn hostname into a valid IP */
+               rc = getaddrinfo(url->host, NULL, NULL, &res);
+               if (rc) {
+                       pb_debug("%s: Invalid URL\n",__func__);
+                       return NULL;
+               }
+               ipaddr = talloc_array(ctx,char,INET_ADDRSTRLEN);
+               ip = (struct sockaddr_in *) res->ai_addr;
+               inet_ntop(AF_INET, &(ip->sin_addr), ipaddr, INET_ADDRSTRLEN);
+               freeaddrinfo(res);
+       }
+
+       const char *argv[] = {
+               pb_system_apps.ip,
+               "route", "show", "to", "match",
+               ipaddr,
+               NULL
+       };
+
+       p = process_create(ctx);
+
+       p->path = pb_system_apps.ip;
+       p->argv = argv;
+       p->keep_stdout = true;
+
+       rc = process_run_sync(p);
+
+       if (rc) {
+               /* ip has complained for some reason; most likely
+                * there is no route to the host - bail out */
+               pb_debug("%s: No route to %s\n",__func__,url->host);
+               return NULL;
+       }
+
+       buf = p->stdout_buf;
+       /* If a route is found, ip-route output will be of the form
+        * "... dev DEVNAME ... " */
+       tok = strtok(buf, delim);
+       while (tok) {
+               if (!strcmp(tok, "dev")) {
+                       tok = strtok(NULL, delim);
+                       dev = talloc_strdup(ctx, tok);
+                       break;
+               }
+               tok = strtok(NULL, delim);
+       }
+
+       process_release(p);
+       if (dev)
+               pb_debug("%s: Found interface '%s'\n", __func__,dev);
+       return dev;
+}
+
+
+void device_handler_process_url(struct device_handler *handler,
+               const char *url)
+{
+       struct discover_context *ctx;
+       struct discover_device *dev;
+       struct boot_status *status;
+       struct pb_url *pb_url;
+       struct event *event;
+       struct param *param;
+
+       status = talloc(handler, struct boot_status);
+
+       status->type = BOOT_STATUS_ERROR;
+       status->progress = 0;
+       status->detail = talloc_asprintf(status,
+                       _("Received config URL %s"), url);
+
+       if (!handler->network) {
+               status->message = talloc_asprintf(handler,
+                                       _("No network configured"));
+               goto msg;
+       }
+
+       event = talloc(handler, struct event);
+       event->type = EVENT_TYPE_USER;
+       event->action = EVENT_ACTION_CONF;
+
+       event->params = talloc_array(event, struct param, 1);
+       param = &event->params[0];
+       param->name = talloc_strdup(event, "pxeconffile");
+       param->value = talloc_strdup(event, url);
+       event->n_params = 1;
+
+       pb_url = pb_url_parse(event, event->params->value);
+       if (!pb_url || !pb_url->host) {
+               status->message = talloc_asprintf(handler,
+                                       _("Invalid config URL!"));
+               goto msg;
+       }
+
+       event->device = device_from_addr(event, pb_url);
+       if (!event->device) {
+               status->message = talloc_asprintf(status,
+                                       _("Unable to route to host %s"),
+                                       pb_url->host);
+               goto msg;
+       }
+
+       dev = discover_device_create(handler, event->device);
+       ctx = device_handler_discover_context_create(handler, dev);
+       ctx->event = event;
+
+       iterate_parsers(ctx);
+
+       device_handler_discover_context_commit(handler, ctx);
+
+       talloc_free(ctx);
+
+       status->type = BOOT_STATUS_INFO;
+       status->message = talloc_asprintf(status, _("Config file %s parsed"),
+                                       pb_url->file);
+msg:
+       boot_status(handler, status);
+       talloc_free(status);
+}
+
 #ifndef PETITBOOT_TEST
 
 #ifndef PETITBOOT_TEST
 
+static void device_handler_update_lang(const char *lang)
+{
+       const char *cur_lang;
+
+       if (!lang)
+               return;
+
+       cur_lang = setlocale(LC_ALL, NULL);
+       if (cur_lang && !strcmp(cur_lang, lang))
+               return;
+
+       setlocale(LC_ALL, lang);
+}
+
 static int device_handler_init_sources(struct device_handler *handler)
 {
        /* init our device sources: udev, network and user events */
 static int device_handler_init_sources(struct device_handler *handler)
 {
        /* init our device sources: udev, network and user events */
@@ -815,6 +1009,13 @@ static int device_handler_init_sources(struct device_handler *handler)
 
 static void device_handler_reinit_sources(struct device_handler *handler)
 {
 
 static void device_handler_reinit_sources(struct device_handler *handler)
 {
+       /* if we haven't initialised sources previously (becuase we started in
+        * safe mode), then init once here. */
+       if (!(handler->udev || handler->network || handler->user_event)) {
+               device_handler_init_sources(handler);
+               return;
+       }
+
        udev_reinit(handler->udev);
 
        network_shutdown(handler->network);
        udev_reinit(handler->udev);
 
        network_shutdown(handler->network);
@@ -983,6 +1184,10 @@ void device_release_write(struct discover_device *dev, bool release)
 
 #else
 
 
 #else
 
+static void device_handler_update_lang(const char *lang __attribute__((unused)))
+{
+}
+
 static int device_handler_init_sources(
                struct device_handler *handler __attribute__((unused)))
 {
 static int device_handler_init_sources(
                struct device_handler *handler __attribute__((unused)))
 {