]> git.ozlabs.org Git - petitboot/blob - discover/user-event.c
discover/boot: abort kexec on any error from validation
[petitboot] / discover / user-event.c
1 /*
2  *  Copyright (C) 2009 Sony Computer Entertainment Inc.
3  *  Copyright 2009 Sony Corp.
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; version 2 of the License.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #if defined(HAVE_CONFIG_H)
20 #include "config.h"
21 #endif
22
23 #include <assert.h>
24 #include <errno.h>
25 #include <string.h>
26 #include <sys/socket.h>
27 #include <sys/types.h>
28 #include <sys/un.h>
29
30 #include <log/log.h>
31 #include <url/url.h>
32 #include <types/types.h>
33 #include <talloc/talloc.h>
34 #include <waiter/waiter.h>
35
36 #include "device-handler.h"
37 #include "resource.h"
38 #include "event.h"
39 #include "user-event.h"
40 #include "sysinfo.h"
41
42
43 #define MAC_ADDR_SIZE   6
44 #define IP_ADDR_SIZE    4
45
46 struct user_event {
47         struct device_handler *handler;
48         int socket;
49 };
50
51 static const char *event_action_name(enum event_action action)
52 {
53         switch (action) {
54         case EVENT_ACTION_ADD:
55                 return "add";
56         case EVENT_ACTION_REMOVE:
57                 return "remove";
58         case EVENT_ACTION_URL:
59                 return "url";
60         case EVENT_ACTION_DHCP:
61                 return "dhcp";
62         case EVENT_ACTION_BOOT:
63                 return "boot";
64         case EVENT_ACTION_SYNC:
65                 return "sync";
66         case EVENT_ACTION_PLUGIN:
67                 return "plugin";
68         default:
69                 break;
70         }
71
72         return "unknown";
73 }
74
75 static void user_event_print_event(struct event __attribute__((unused)) *event)
76 {
77         int i;
78
79         pb_debug("user_event %s event:\n", event_action_name(event->action));
80         pb_debug("\tdevice: %s\n", event->device);
81
82         for (i = 0; i < event->n_params; i++)
83                 pb_debug("\t%-12s => %s\n",
84                         event->params[i].name, event->params[i].value);
85 }
86
87 static struct resource *user_event_resource(struct discover_boot_option *opt,
88                 struct event *event, bool gen_boot_args_sigfile)
89 {
90         const char *siaddr, *boot_file;
91         struct resource *res;
92         struct pb_url *url;
93         char *url_str;
94
95         siaddr = event_get_param(event, "siaddr");
96         if (!siaddr) {
97                 pb_log("%s: next server option not found\n", __func__);
98                 return NULL;
99         }
100
101         boot_file = event_get_param(event, "bootfile");
102         if (!boot_file) {
103                 pb_log("%s: bootfile not found\n", __func__);
104                 return NULL;
105         }
106
107         if (gen_boot_args_sigfile) {
108                 char* args_sigfile_default = talloc_asprintf(opt,
109                         "%s.cmdline.sig", boot_file);
110                 url_str = talloc_asprintf(opt, "%s%s/%s", "tftp://", siaddr,
111                         args_sigfile_default);
112                 talloc_free(args_sigfile_default);
113         }
114         else
115                 url_str = talloc_asprintf(opt, "%s%s/%s", "tftp://", siaddr,
116                         boot_file);
117         url = pb_url_parse(opt, url_str);
118         talloc_free(url_str);
119
120         if (!url)
121                 return NULL;
122
123         res = create_url_resource(opt, url);
124         if (!res) {
125                 talloc_free(url);
126                 return NULL;
127         }
128
129         return res;
130 }
131
132 static int parse_user_event(struct discover_context *ctx, struct event *event)
133 {
134         struct discover_boot_option *d_opt;
135         char *server_ip, *root_dir, *p;
136         struct boot_option *opt;
137         struct device *dev;
138         const char *val;
139
140         dev = ctx->device->device;
141
142         d_opt = discover_boot_option_create(ctx, ctx->device);
143         if (!d_opt)
144                 goto fail;
145
146         opt = d_opt->option;
147
148         val = event_get_param(event, "name");
149
150         if (!val) {
151                 pb_log("%s: no name found\n", __func__);
152                 goto fail_opt;
153         }
154
155         opt->id = talloc_asprintf(opt, "%s#%s", dev->id, val);
156         opt->name = talloc_strdup(opt, val);
157
158         d_opt->boot_image = user_event_resource(d_opt, event, false);
159         if (!d_opt->boot_image) {
160                 pb_log("%s: no boot image found for %s!\n", __func__,
161                                 opt->name);
162                 goto fail_opt;
163         }
164         d_opt->args_sig_file = user_event_resource(d_opt, event, true);
165
166         val = event_get_param(event, "rootpath");
167         if (val) {
168                 server_ip = talloc_strdup(opt, val);
169                 p = strchr(server_ip, ':');
170                 if (p) {
171                         root_dir = talloc_strdup(opt, p + 1);
172                         *p = '\0';
173                         opt->boot_args = talloc_asprintf(opt, "%s%s:%s",
174                                         "root=/dev/nfs ip=any nfsroot=",
175                                         server_ip, root_dir);
176
177                         talloc_free(root_dir);
178                 } else {
179                         opt->boot_args = talloc_asprintf(opt, "%s",
180                                         "root=/dev/nfs ip=any nfsroot=");
181                 }
182
183                 talloc_free(server_ip);
184         }
185
186         opt->description = talloc_asprintf(opt, "%s %s", opt->boot_image_file,
187                 opt->boot_args ? : "");
188
189         if (event_get_param(event, "default"))
190                 opt->is_default = true;
191
192         discover_context_add_boot_option(ctx, d_opt);
193
194         return 0;
195
196 fail_opt:
197         talloc_free(d_opt);
198 fail:
199         return -1;
200 }
201
202 static const char *parse_host_addr(struct event *event)
203 {
204         const char *val;
205
206         val = event_get_param(event, "tftp");
207         if (val)
208                 return val;
209
210         val = event_get_param(event, "siaddr");
211         if (val)
212                 return val;
213
214         val = event_get_param(event, "serverid");
215         if (val)
216                 return val;
217
218         return NULL;
219 }
220
221 static char *parse_mac_addr(struct discover_context *ctx, const char *mac)
222 {
223         unsigned int mac_addr_arr[MAC_ADDR_SIZE];
224         char *mac_addr;
225
226         sscanf(mac, "%X:%X:%X:%X:%X:%X", mac_addr_arr, mac_addr_arr + 1,
227                         mac_addr_arr + 2, mac_addr_arr + 3, mac_addr_arr + 4,
228                         mac_addr_arr + 5);
229
230         mac_addr = talloc_asprintf(ctx, "01-%02x-%02x-%02x-%02x-%02x-%02x",
231                         mac_addr_arr[0], mac_addr_arr[1], mac_addr_arr[2],
232                         mac_addr_arr[3], mac_addr_arr[4], mac_addr_arr[5]);
233
234         return mac_addr;
235 }
236
237 static char *parse_ip_addr(struct discover_context *ctx, const char *ip)
238 {
239         unsigned int ip_addr_arr[IP_ADDR_SIZE];
240         char *ip_hex;
241
242         sscanf(ip, "%u.%u.%u.%u", ip_addr_arr, ip_addr_arr + 1,
243                         ip_addr_arr + 2, ip_addr_arr + 3);
244
245         ip_hex = talloc_asprintf(ctx, "%02X%02X%02X%02X", ip_addr_arr[0],
246                         ip_addr_arr[1], ip_addr_arr[2], ip_addr_arr[3]);
247
248         return ip_hex;
249 }
250
251 struct pb_url *user_event_parse_conf_url(struct discover_context *ctx,
252                 struct event *event, bool *is_complete)
253 {
254         const char *conffile, *pathprefix, *host, *bootfile;
255         char *p, *basedir, *url_str;
256         struct pb_url *url;
257
258         conffile = event_get_param(event, "pxeconffile");
259         pathprefix = event_get_param(event, "pxepathprefix");
260         bootfile = event_get_param(event, "bootfile");
261
262         /* If we're given a conf file, we're able to generate a complete URL to
263          * the configuration file, and the parser doesn't need to do any
264          * further autodiscovery */
265         *is_complete = !!conffile;
266
267         /* if conffile is a URL, that's all we need */
268         if (conffile && is_url(conffile)) {
269                 url = pb_url_parse(ctx, conffile);
270                 return url;
271         }
272
273         /* If we can create a URL from pathprefix (optionally with
274          * conffile appended to create a complete URL), use that */
275         if (pathprefix && is_url(pathprefix)) {
276                 if (conffile) {
277                         url_str = talloc_asprintf(ctx, "%s%s",
278                                         pathprefix, conffile);
279                         url = pb_url_parse(ctx, url_str);
280                         talloc_free(url_str);
281                 } else {
282                         url = pb_url_parse(ctx, pathprefix);
283                 }
284
285                 return url;
286         }
287
288         host = parse_host_addr(event);
289         if (!host) {
290                 pb_log("%s: host address not found\n", __func__);
291                 return NULL;
292         }
293
294         url_str = talloc_asprintf(ctx, "tftp://%s/", host);
295
296         /* if we have a pathprefix, use that directly.. */
297         if (pathprefix) {
298                 /* strip leading slashes */
299                 while (pathprefix[0] == '/')
300                         pathprefix++;
301                 url_str = talloc_asprintf_append(url_str, "%s", pathprefix);
302
303         /* ... otherwise, add a path based on the bootfile name, but only
304          * if conffile isn't an absolute path itself */
305         } else if (bootfile && !(conffile && conffile[0] == '/')) {
306
307                 basedir = talloc_strdup(ctx, bootfile);
308
309                 /* strip filename from the bootfile path, leaving only a
310                  * directory */
311                 p = strrchr(basedir, '/');
312                 if (!p)
313                         p = basedir;
314                 *p = '\0';
315
316                 if (strlen(basedir))
317                         url_str = talloc_asprintf_append(url_str, "%s/",
318                                         basedir);
319
320                 talloc_free(basedir);
321         }
322
323         /* finally, append conffile */
324         if (conffile)
325                 url_str = talloc_asprintf_append(url_str, "%s", conffile);
326
327         url = pb_url_parse(ctx, url_str);
328
329         talloc_free(url_str);
330
331         return url;
332 }
333
334 char **user_event_parse_conf_filenames(
335                 struct discover_context *ctx, struct event *event)
336 {
337         char *mac_addr, *ip_hex;
338         const char *mac, *ip;
339         char **filenames;
340         int index, len;
341
342         mac = event_get_param(event, "mac");
343         if (mac)
344                 mac_addr = parse_mac_addr(ctx, mac);
345         else
346                 mac_addr = NULL;
347
348         ip = event_get_param(event, "ip");
349         if (ip) {
350                 ip_hex = parse_ip_addr(ctx, ip);
351                 len = strlen(ip_hex);
352         } else {
353                 ip_hex = NULL;
354                 len = 0;
355         }
356
357         if (!mac_addr && !ip_hex) {
358                 pb_log("%s: neither mac nor ip parameter found\n", __func__);
359                 return NULL;
360         }
361
362         /* Filenames as fallback IP's + mac + default */
363         filenames = talloc_array(ctx, char *, len + 3);
364
365         index = 0;
366         if (mac_addr)
367                 filenames[index++] = talloc_strdup(filenames, mac_addr);
368
369         while (len) {
370                 filenames[index++] = talloc_strdup(filenames, ip_hex);
371                 ip_hex[--len] = '\0';
372         }
373
374         filenames[index++] = talloc_strdup(filenames, "default");
375         filenames[index++] = NULL;
376
377         if (mac_addr)
378                 talloc_free(mac_addr);
379
380         if (ip_hex)
381                 talloc_free(ip_hex);
382
383         return filenames;
384 }
385
386 static int user_event_dhcp(struct user_event *uev, struct event *event)
387 {
388         struct device_handler *handler = uev->handler;
389         struct discover_device *dev;
390
391         uint8_t hwaddr[MAC_ADDR_SIZE];
392
393         sscanf(event_get_param(event, "mac"),
394                "%hhX:%hhX:%hhX:%hhX:%hhX:%hhX",
395                hwaddr, hwaddr + 1, hwaddr + 2,
396                hwaddr + 3, hwaddr + 4, hwaddr + 5);
397
398         system_info_set_interface_address(sizeof(hwaddr), hwaddr,
399                                           event_get_param(event, "ip"));
400
401         dev = discover_device_create(handler, event_get_param(event, "mac"),
402                                         event->device);
403
404         device_handler_dhcp(handler, dev, event);
405
406         return 0;
407 }
408
409 static int user_event_add(struct user_event *uev, struct event *event)
410 {
411         struct device_handler *handler = uev->handler;
412         struct discover_context *ctx;
413         struct discover_device *dev;
414
415         /* In case this is a network interface, try to refer to it by UUID */
416         dev = discover_device_create(handler, event_get_param(event, "mac"),
417                                         event->device);
418         dev->device->id = talloc_strdup(dev, event->device);
419         ctx = device_handler_discover_context_create(handler, dev);
420
421         parse_user_event(ctx, event);
422
423         device_handler_discover_context_commit(handler, ctx);
424
425         talloc_free(ctx);
426
427         return 0;
428 }
429
430 static int user_event_remove(struct user_event *uev, struct event *event)
431 {
432         struct device_handler *handler = uev->handler;
433         struct discover_device *dev;
434         const char *mac = event_get_param(event, "mac");
435
436         if (mac)
437                 dev = device_lookup_by_uuid(handler, event_get_param(event, "mac"));
438         else
439                 dev = device_lookup_by_id(handler, event->device);
440
441         if (!dev)
442                 return 0;
443
444         device_handler_remove(handler, dev);
445
446         return 0;
447 }
448
449 static int user_event_url(struct user_event *uev, struct event *event)
450 {
451         struct device_handler *handler = uev->handler;
452         const char *url;
453
454         url = event_get_param(event, "url");
455         if (url)
456                 device_handler_process_url(handler, url, NULL, NULL);
457
458         return 0;
459 }
460
461 static int user_event_boot(struct user_event *uev, struct event *event)
462 {
463         struct device_handler *handler = uev->handler;
464         struct boot_command *cmd = talloc(handler, struct boot_command);
465
466         cmd->option_id = talloc_strdup(cmd, event_get_param(event, "id"));
467         cmd->boot_image_file = talloc_strdup(cmd, event_get_param(event, "image"));
468         cmd->initrd_file = talloc_strdup(cmd, event_get_param(event, "initrd"));
469         cmd->dtb_file = talloc_strdup(cmd, event_get_param(event, "dtb"));
470         cmd->boot_args = talloc_strdup(cmd, event_get_param(event, "args"));
471
472         device_handler_boot(handler, cmd);
473
474         talloc_free(cmd);
475
476         return 0;
477 }
478
479 static int user_event_sync(struct user_event *uev, struct event *event)
480 {
481         struct device_handler *handler = uev->handler;
482
483         if (strncasecmp(event->device, "all", strlen("all")) != 0)
484                 device_sync_snapshots(handler, event->device);
485         else
486                 device_sync_snapshots(handler, NULL);
487
488         return 0;
489 }
490
491 static int process_uninstalled_plugin(struct user_event *uev,
492                 struct event *event)
493 {
494         struct device_handler *handler = uev->handler;
495         struct discover_boot_option *file_opt;
496         struct discover_device *device;
497         struct discover_context *ctx;
498         const char *path;
499         struct resource *res;
500
501         if (!event_get_param(event, "path")) {
502                 pb_log("Uninstalled pb-plugin event missing path param\n");
503                 return -1;
504         }
505
506         device = device_lookup_by_name(handler, event->device);
507         if (!device) {
508                 pb_log("Couldn't find device matching %s for plugin\n",
509                                 event->device);
510                 return -1;
511         }
512
513         ctx = device_handler_discover_context_create(handler, device);
514         file_opt = discover_boot_option_create(ctx, device);
515         file_opt->option->name = talloc_strdup(file_opt,
516                         event_get_param(event, "name"));
517         file_opt->option->id = talloc_asprintf(file_opt, "%s@%p",
518                         device->device->id, file_opt);
519         file_opt->option->type = DISCOVER_PLUGIN_OPTION;
520
521
522         path = event_get_param(event, "path");
523         /* path may be relative to root */
524         if (strncmp(device->mount_path, path, strlen(device->mount_path)) == 0) {
525                 path += strlen(device->mount_path) + 1;
526         }
527
528         res = talloc(file_opt, struct resource);
529         resolve_resource_against_device(res, device, path);
530         file_opt->boot_image = res;
531
532         discover_context_add_boot_option(ctx, file_opt);
533         device_handler_discover_context_commit(handler, ctx);
534
535         return 0;
536 }
537
538 /*
539  * Notification of a plugin event. This can either be for an uninstalled plugin
540  * that pb-plugin has scanned, or the result of a plugin that pb-plugin has
541  * installed.
542  */
543 static int user_event_plugin(struct user_event *uev, struct event *event)
544 {
545         struct device_handler *handler = uev->handler;
546         char *executable, *executables, *saveptr;
547         struct plugin_option *opt;
548         const char *installed;
549
550         installed = event_get_param(event, "installed");
551         if (!installed || strncmp(installed, "no", strlen("no")) == 0)
552                 return process_uninstalled_plugin(uev, event);
553
554         opt = talloc_zero(handler, struct plugin_option);
555         if (!opt)
556                 return -1;
557         opt->name = talloc_strdup(opt, event_get_param(event, "name"));
558         opt->id = talloc_strdup(opt, event_get_param(event, "id"));
559         opt->version = talloc_strdup(opt, event_get_param(event, "version"));
560         opt->vendor = talloc_strdup(opt, event_get_param(event, "vendor"));
561         opt->vendor_id = talloc_strdup(opt, event_get_param(event, "vendor_id"));
562         opt->date = talloc_strdup(opt, event_get_param(event, "date"));
563         opt->plugin_file = talloc_strdup(opt,
564                         event_get_param(event, "source_file"));
565
566         executables = talloc_strdup(opt, event_get_param(event, "executables"));
567         if (!executables) {
568                 talloc_free(opt);
569                 return -1;
570         }
571
572         /*
573          * The 'executables' parameter is a space-delimited list of installed
574          * executables
575          */
576         executable = strtok_r(executables, " ", &saveptr);
577         while (executable) {
578                 opt->executables = talloc_realloc(opt, opt->executables,
579                                                   char *, opt->n_executables + 1);
580                 if (!opt->executables) {
581                         talloc_free(opt);
582                         return -1;
583                 }
584                 opt->executables[opt->n_executables++] = talloc_strdup(opt,
585                                                                 executable);
586                 executable = strtok_r(NULL, " ", &saveptr);
587         }
588
589         device_handler_add_plugin_option(handler, opt);
590
591         talloc_free(executables);
592
593         return 0;
594 }
595
596 static void user_event_handle_message(struct user_event *uev, char *buf,
597         int len)
598 {
599         int result;
600         struct event *event;
601
602         event = talloc(uev, struct event);
603         event->type = EVENT_TYPE_USER;
604
605         result = event_parse_ad_message(event, buf, len);
606
607         if (result)
608                 return;
609
610         user_event_print_event(event);
611
612         switch (event->action) {
613         case EVENT_ACTION_ADD:
614                 result = user_event_add(uev, event);
615                 break;
616         case EVENT_ACTION_REMOVE:
617                 result = user_event_remove(uev, event);
618                 break;
619         case EVENT_ACTION_URL:
620                 result = user_event_url(uev, event);
621                 goto out;
622         case EVENT_ACTION_DHCP:
623                 result = user_event_dhcp(uev, event);
624                 goto out;
625         case EVENT_ACTION_BOOT:
626                 result = user_event_boot(uev, event);
627                 break;
628         case EVENT_ACTION_SYNC:
629                 result = user_event_sync(uev, event);
630                 break;
631         case EVENT_ACTION_PLUGIN:
632                 result = user_event_plugin(uev, event);
633                 break;
634         default:
635                 break;
636         }
637
638         /* user_event_url() and user_event_dhcp() will steal the event context,
639          * but all others still need to free */
640         talloc_free(event);
641 out:
642         return;
643 }
644
645 static int user_event_process(void *arg)
646 {
647         struct user_event *uev = arg;
648         char buf[PBOOT_USER_EVENT_SIZE + 1];
649         int len;
650
651         len = recvfrom(uev->socket, buf, PBOOT_USER_EVENT_SIZE, 0, NULL, NULL);
652
653         if (len < 0) {
654                 pb_log("%s: socket read failed: %s", __func__, strerror(errno));
655                 return 0;
656         }
657
658         if (len == 0) {
659                 pb_log("%s: empty", __func__);
660                 return 0;
661         }
662
663         buf[len] = '\0';
664
665         pb_debug("%s: %u bytes\n", __func__, len);
666
667         user_event_handle_message(uev, buf, len);
668
669         return 0;
670 }
671
672 static int user_event_destructor(void *arg)
673 {
674         struct user_event *uev = arg;
675
676         pb_debug("%s\n", __func__);
677
678         if (uev->socket >= 0)
679                 close(uev->socket);
680
681         return 0;
682 }
683
684 struct user_event *user_event_init(struct device_handler *handler,
685                 struct waitset *waitset)
686 {
687         struct sockaddr_un addr;
688         struct user_event *uev;
689
690         unlink(PBOOT_USER_EVENT_SOCKET);
691
692         uev = talloc(handler, struct user_event);
693
694         uev->handler = handler;
695
696         uev->socket = socket(AF_UNIX, SOCK_DGRAM, 0);
697         if (uev->socket < 0) {
698                 pb_log("%s: Error creating event handler socket: %s\n",
699                         __func__, strerror(errno));
700                 goto out_err;
701         }
702
703         talloc_set_destructor(uev, user_event_destructor);
704
705         memset(&addr, 0, sizeof(addr));
706         addr.sun_family = AF_UNIX;
707         strcpy(addr.sun_path, PBOOT_USER_EVENT_SOCKET);
708
709         if (bind(uev->socket, (struct sockaddr *)&addr, sizeof(addr))) {
710                 pb_log("Error binding event handler socket: %s\n",
711                         strerror(errno));
712         }
713
714         waiter_register_io(waitset, uev->socket, WAIT_IN,
715                         user_event_process, uev);
716
717         pb_debug("%s: waiting on %s\n", __func__, PBOOT_USER_EVENT_SOCKET);
718
719         return uev;
720
721 out_err:
722         talloc_free(uev);
723         return NULL;
724 }