]> git.ozlabs.org Git - petitboot/blob - discover/user-event.c
ee282bb48c7f6b909134e42ef1388e0c27b9cde9
[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         if (!event_get_param(event, "mac"))
394                 return -1;
395         if (!event_get_param(event, "ip") && !event_get_param(event, "ipv6"))
396                 return -1;
397
398         sscanf(event_get_param(event, "mac"),
399                "%hhX:%hhX:%hhX:%hhX:%hhX:%hhX",
400                hwaddr, hwaddr + 1, hwaddr + 2,
401                hwaddr + 3, hwaddr + 4, hwaddr + 5);
402
403         if (event_get_param(event, "ipv6"))
404                 system_info_set_interface_address(sizeof(hwaddr), hwaddr,
405                                                   event_get_param(event, "ipv6"));
406         else
407                 system_info_set_interface_address(sizeof(hwaddr), hwaddr,
408                                                   event_get_param(event, "ip"));
409
410         dev = discover_device_create(handler, event_get_param(event, "mac"),
411                                         event->device);
412
413         device_handler_dhcp(handler, dev, event);
414
415         return 0;
416 }
417
418 static int user_event_add(struct user_event *uev, struct event *event)
419 {
420         struct device_handler *handler = uev->handler;
421         struct discover_context *ctx;
422         struct discover_device *dev;
423         int rc;
424
425         /* In case this is a network interface, try to refer to it by UUID */
426         dev = discover_device_create(handler, event_get_param(event, "mac"),
427                                         event->device);
428         dev->device->id = talloc_strdup(dev, event->device);
429         ctx = device_handler_discover_context_create(handler, dev);
430
431         rc = parse_user_event(ctx, event);
432         if (rc) {
433                 pb_log("parse_user_event returned %d\n", rc);
434                 return rc;
435         }
436
437         device_handler_discover_context_commit(handler, ctx);
438
439         talloc_free(ctx);
440
441         return 0;
442 }
443
444 static int user_event_remove(struct user_event *uev, struct event *event)
445 {
446         struct device_handler *handler = uev->handler;
447         struct discover_device *dev;
448         const char *mac = event_get_param(event, "mac");
449
450         if (mac)
451                 dev = device_lookup_by_uuid(handler, event_get_param(event, "mac"));
452         else
453                 dev = device_lookup_by_id(handler, event->device);
454
455         if (!dev)
456                 return 0;
457
458         device_handler_remove(handler, dev);
459
460         return 0;
461 }
462
463 static int user_event_url(struct user_event *uev, struct event *event)
464 {
465         struct device_handler *handler = uev->handler;
466         const char *url;
467
468         url = event_get_param(event, "url");
469         if (url)
470                 device_handler_process_url(handler, url, NULL, NULL);
471
472         return 0;
473 }
474
475 static int user_event_boot(struct user_event *uev, struct event *event)
476 {
477         struct device_handler *handler = uev->handler;
478         struct boot_command *cmd = talloc(handler, struct boot_command);
479
480         cmd->option_id = talloc_strdup(cmd, event_get_param(event, "id"));
481         cmd->boot_image_file = talloc_strdup(cmd, event_get_param(event, "image"));
482         cmd->initrd_file = talloc_strdup(cmd, event_get_param(event, "initrd"));
483         cmd->dtb_file = talloc_strdup(cmd, event_get_param(event, "dtb"));
484         cmd->boot_args = talloc_strdup(cmd, event_get_param(event, "args"));
485
486         device_handler_boot(handler, cmd);
487
488         talloc_free(cmd);
489
490         return 0;
491 }
492
493 static int user_event_sync(struct user_event *uev, struct event *event)
494 {
495         struct device_handler *handler = uev->handler;
496
497         if (strncasecmp(event->device, "all", strlen("all")) != 0)
498                 device_sync_snapshots(handler, event->device);
499         else
500                 device_sync_snapshots(handler, NULL);
501
502         return 0;
503 }
504
505 static int process_uninstalled_plugin(struct user_event *uev,
506                 struct event *event)
507 {
508         struct device_handler *handler = uev->handler;
509         struct discover_boot_option *file_opt;
510         struct discover_device *device;
511         struct discover_context *ctx;
512         const char *path;
513         struct resource *res;
514
515         if (!event_get_param(event, "path")) {
516                 pb_log("Uninstalled pb-plugin event missing path param\n");
517                 return -1;
518         }
519
520         device = device_lookup_by_name(handler, event->device);
521         if (!device) {
522                 pb_log("Couldn't find device matching %s for plugin\n",
523                                 event->device);
524                 return -1;
525         }
526
527         ctx = device_handler_discover_context_create(handler, device);
528         file_opt = discover_boot_option_create(ctx, device);
529         file_opt->option->name = talloc_strdup(file_opt,
530                         event_get_param(event, "name"));
531         file_opt->option->id = talloc_asprintf(file_opt, "%s@%p",
532                         device->device->id, file_opt);
533         file_opt->option->type = DISCOVER_PLUGIN_OPTION;
534
535
536         path = event_get_param(event, "path");
537         /* path may be relative to root */
538         if (strncmp(device->mount_path, path, strlen(device->mount_path)) == 0) {
539                 path += strlen(device->mount_path) + 1;
540         }
541
542         res = talloc(file_opt, struct resource);
543         resolve_resource_against_device(res, device, path);
544         file_opt->boot_image = res;
545
546         discover_context_add_boot_option(ctx, file_opt);
547         device_handler_discover_context_commit(handler, ctx);
548
549         return 0;
550 }
551
552 /*
553  * Notification of a plugin event. This can either be for an uninstalled plugin
554  * that pb-plugin has scanned, or the result of a plugin that pb-plugin has
555  * installed.
556  */
557 static int user_event_plugin(struct user_event *uev, struct event *event)
558 {
559         struct device_handler *handler = uev->handler;
560         char *executable, *executables, *saveptr;
561         struct plugin_option *opt;
562         const char *installed;
563
564         installed = event_get_param(event, "installed");
565         if (!installed || strncmp(installed, "no", strlen("no")) == 0)
566                 return process_uninstalled_plugin(uev, event);
567
568         opt = talloc_zero(handler, struct plugin_option);
569         if (!opt)
570                 return -1;
571         opt->name = talloc_strdup(opt, event_get_param(event, "name"));
572         opt->id = talloc_strdup(opt, event_get_param(event, "id"));
573         opt->version = talloc_strdup(opt, event_get_param(event, "version"));
574         opt->vendor = talloc_strdup(opt, event_get_param(event, "vendor"));
575         opt->vendor_id = talloc_strdup(opt, event_get_param(event, "vendor_id"));
576         opt->date = talloc_strdup(opt, event_get_param(event, "date"));
577         opt->plugin_file = talloc_strdup(opt,
578                         event_get_param(event, "source_file"));
579
580         executables = talloc_strdup(opt, event_get_param(event, "executables"));
581         if (!executables) {
582                 talloc_free(opt);
583                 return -1;
584         }
585
586         /*
587          * The 'executables' parameter is a space-delimited list of installed
588          * executables
589          */
590         executable = strtok_r(executables, " ", &saveptr);
591         while (executable) {
592                 opt->executables = talloc_realloc(opt, opt->executables,
593                                                   char *, opt->n_executables + 1);
594                 if (!opt->executables) {
595                         talloc_free(opt);
596                         return -1;
597                 }
598                 opt->executables[opt->n_executables++] = talloc_strdup(opt,
599                                                                 executable);
600                 executable = strtok_r(NULL, " ", &saveptr);
601         }
602
603         device_handler_add_plugin_option(handler, opt);
604
605         talloc_free(executables);
606
607         return 0;
608 }
609
610 static void user_event_handle_message(struct user_event *uev, char *buf,
611         int len)
612 {
613         int result;
614         struct event *event;
615
616         event = talloc(uev, struct event);
617         event->type = EVENT_TYPE_USER;
618
619         result = event_parse_ad_message(event, buf, len);
620
621         if (result)
622                 return;
623
624         user_event_print_event(event);
625
626         switch (event->action) {
627         case EVENT_ACTION_ADD:
628                 result = user_event_add(uev, event);
629                 break;
630         case EVENT_ACTION_REMOVE:
631                 result = user_event_remove(uev, event);
632                 break;
633         case EVENT_ACTION_URL:
634                 result = user_event_url(uev, event);
635                 goto out;
636         case EVENT_ACTION_DHCP:
637                 result = user_event_dhcp(uev, event);
638                 goto out;
639         case EVENT_ACTION_BOOT:
640                 result = user_event_boot(uev, event);
641                 break;
642         case EVENT_ACTION_SYNC:
643                 result = user_event_sync(uev, event);
644                 break;
645         case EVENT_ACTION_PLUGIN:
646                 result = user_event_plugin(uev, event);
647                 break;
648         default:
649                 break;
650         }
651
652         /* user_event_url() and user_event_dhcp() will steal the event context,
653          * but all others still need to free */
654         talloc_free(event);
655 out:
656         return;
657 }
658
659 static int user_event_process(void *arg)
660 {
661         struct user_event *uev = arg;
662         char buf[PBOOT_USER_EVENT_SIZE + 1];
663         int len;
664
665         len = recvfrom(uev->socket, buf, PBOOT_USER_EVENT_SIZE, 0, NULL, NULL);
666
667         if (len < 0) {
668                 pb_log("%s: socket read failed: %s", __func__, strerror(errno));
669                 return 0;
670         }
671
672         if (len == 0) {
673                 pb_log("%s: empty", __func__);
674                 return 0;
675         }
676
677         buf[len] = '\0';
678
679         pb_debug("%s: %u bytes\n", __func__, len);
680
681         user_event_handle_message(uev, buf, len);
682
683         return 0;
684 }
685
686 static int user_event_destructor(void *arg)
687 {
688         struct user_event *uev = arg;
689
690         pb_debug("%s\n", __func__);
691
692         if (uev->socket >= 0)
693                 close(uev->socket);
694
695         return 0;
696 }
697
698 struct user_event *user_event_init(struct device_handler *handler,
699                 struct waitset *waitset)
700 {
701         struct sockaddr_un addr;
702         struct user_event *uev;
703
704         unlink(PBOOT_USER_EVENT_SOCKET);
705
706         uev = talloc(handler, struct user_event);
707
708         uev->handler = handler;
709
710         uev->socket = socket(AF_UNIX, SOCK_DGRAM, 0);
711         if (uev->socket < 0) {
712                 pb_log("%s: Error creating event handler socket: %s\n",
713                         __func__, strerror(errno));
714                 goto out_err;
715         }
716
717         talloc_set_destructor(uev, user_event_destructor);
718
719         memset(&addr, 0, sizeof(addr));
720         addr.sun_family = AF_UNIX;
721         strcpy(addr.sun_path, PBOOT_USER_EVENT_SOCKET);
722
723         if (bind(uev->socket, (struct sockaddr *)&addr, sizeof(addr))) {
724                 pb_log("Error binding event handler socket: %s\n",
725                         strerror(errno));
726         }
727
728         waiter_register_io(waitset, uev->socket, WAIT_IN,
729                         user_event_process, uev);
730
731         pb_debug("%s: waiting on %s\n", __func__, PBOOT_USER_EVENT_SOCKET);
732
733         return uev;
734
735 out_err:
736         talloc_free(uev);
737         return NULL;
738 }