]> git.ozlabs.org Git - petitboot/blob - discover/user-event.c
discover: Deprecate 'conf' user event
[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
41
42 #define MAC_ADDR_SIZE   6
43 #define IP_ADDR_SIZE    4
44
45 struct user_event {
46         struct device_handler *handler;
47         int socket;
48 };
49
50 static const char *event_action_name(enum event_action action)
51 {
52         switch (action) {
53         case EVENT_ACTION_ADD:
54                 return "add";
55         case EVENT_ACTION_REMOVE:
56                 return "remove";
57         case EVENT_ACTION_URL:
58                 return "url";
59         case EVENT_ACTION_DHCP:
60                 return "dhcp";
61         case EVENT_ACTION_BOOT:
62                 return "boot";
63         case EVENT_ACTION_SYNC:
64                 return "sync";
65         default:
66                 break;
67         }
68
69         return "unknown";
70 }
71
72 static void user_event_print_event(struct event __attribute__((unused)) *event)
73 {
74         int i;
75
76         pb_debug("user_event %s event:\n", event_action_name(event->action));
77         pb_debug("\tdevice: %s\n", event->device);
78
79         for (i = 0; i < event->n_params; i++)
80                 pb_debug("\t%-12s => %s\n",
81                         event->params[i].name, event->params[i].value);
82 }
83
84 static struct resource *user_event_resource(struct discover_boot_option *opt,
85                 struct event *event, bool gen_boot_args_sigfile)
86 {
87         const char *siaddr, *boot_file;
88         struct resource *res;
89         struct pb_url *url;
90         char *url_str;
91
92         siaddr = event_get_param(event, "siaddr");
93         if (!siaddr) {
94                 pb_log("%s: next server option not found\n", __func__);
95                 return NULL;
96         }
97
98         boot_file = event_get_param(event, "bootfile");
99         if (!boot_file) {
100                 pb_log("%s: bootfile not found\n", __func__);
101                 return NULL;
102         }
103
104         if (gen_boot_args_sigfile) {
105                 char* args_sigfile_default = talloc_asprintf(opt,
106                         "%s.cmdline.sig", boot_file);
107                 url_str = talloc_asprintf(opt, "%s%s/%s", "tftp://", siaddr,
108                         args_sigfile_default);
109                 talloc_free(args_sigfile_default);
110         }
111         else
112                 url_str = talloc_asprintf(opt, "%s%s/%s", "tftp://", siaddr,
113                         boot_file);
114         url = pb_url_parse(opt, url_str);
115         talloc_free(url_str);
116
117         if (!url)
118                 return NULL;
119
120         res = create_url_resource(opt, url);
121         if (!res) {
122                 talloc_free(url);
123                 return NULL;
124         }
125
126         return res;
127 }
128
129 static int parse_user_event(struct discover_context *ctx, struct event *event)
130 {
131         struct discover_boot_option *d_opt;
132         char *server_ip, *root_dir, *p;
133         struct boot_option *opt;
134         struct device *dev;
135         const char *val;
136
137         dev = ctx->device->device;
138
139         d_opt = discover_boot_option_create(ctx, ctx->device);
140         if (!d_opt)
141                 goto fail;
142
143         opt = d_opt->option;
144
145         val = event_get_param(event, "name");
146
147         if (!val) {
148                 pb_log("%s: no name found\n", __func__);
149                 goto fail_opt;
150         }
151
152         opt->id = talloc_asprintf(opt, "%s#%s", dev->id, val);
153         opt->name = talloc_strdup(opt, val);
154
155         d_opt->boot_image = user_event_resource(d_opt, event, false);
156         if (!d_opt->boot_image) {
157                 pb_log("%s: no boot image found for %s!\n", __func__,
158                                 opt->name);
159                 goto fail_opt;
160         }
161         d_opt->args_sig_file = user_event_resource(d_opt, event, true);
162
163         val = event_get_param(event, "rootpath");
164         if (val) {
165                 server_ip = talloc_strdup(opt, val);
166                 p = strchr(server_ip, ':');
167                 if (p) {
168                         root_dir = talloc_strdup(opt, p + 1);
169                         *p = '\0';
170                         opt->boot_args = talloc_asprintf(opt, "%s%s:%s",
171                                         "root=/dev/nfs ip=any nfsroot=",
172                                         server_ip, root_dir);
173
174                         talloc_free(root_dir);
175                 } else {
176                         opt->boot_args = talloc_asprintf(opt, "%s",
177                                         "root=/dev/nfs ip=any nfsroot=");
178                 }
179
180                 talloc_free(server_ip);
181         }
182
183         opt->description = talloc_asprintf(opt, "%s %s", opt->boot_image_file,
184                 opt->boot_args ? : "");
185
186         if (event_get_param(event, "default"))
187                 opt->is_default = true;
188
189         discover_context_add_boot_option(ctx, d_opt);
190
191         return 0;
192
193 fail_opt:
194         talloc_free(d_opt);
195 fail:
196         return -1;
197 }
198
199 static const char *parse_host_addr(struct event *event)
200 {
201         const char *val;
202
203         val = event_get_param(event, "tftp");
204         if (val)
205                 return val;
206
207         val = event_get_param(event, "siaddr");
208         if (val)
209                 return val;
210
211         val = event_get_param(event, "serverid");
212         if (val)
213                 return val;
214
215         return NULL;
216 }
217
218 static char *parse_mac_addr(struct discover_context *ctx, const char *mac)
219 {
220         unsigned int mac_addr_arr[MAC_ADDR_SIZE];
221         char *mac_addr;
222
223         sscanf(mac, "%X:%X:%X:%X:%X:%X", mac_addr_arr, mac_addr_arr + 1,
224                         mac_addr_arr + 2, mac_addr_arr + 3, mac_addr_arr + 4,
225                         mac_addr_arr + 5);
226
227         mac_addr = talloc_asprintf(ctx, "01-%02x-%02x-%02x-%02x-%02x-%02x",
228                         mac_addr_arr[0], mac_addr_arr[1], mac_addr_arr[2],
229                         mac_addr_arr[3], mac_addr_arr[4], mac_addr_arr[5]);
230
231         return mac_addr;
232 }
233
234 static char *parse_ip_addr(struct discover_context *ctx, const char *ip)
235 {
236         unsigned int ip_addr_arr[IP_ADDR_SIZE];
237         char *ip_hex;
238
239         sscanf(ip, "%u.%u.%u.%u", ip_addr_arr, ip_addr_arr + 1,
240                         ip_addr_arr + 2, ip_addr_arr + 3);
241
242         ip_hex = talloc_asprintf(ctx, "%02X%02X%02X%02X", ip_addr_arr[0],
243                         ip_addr_arr[1], ip_addr_arr[2], ip_addr_arr[3]);
244
245         return ip_hex;
246 }
247
248 struct pb_url *user_event_parse_conf_url(struct discover_context *ctx,
249                 struct event *event, bool *is_complete)
250 {
251         const char *conffile, *pathprefix, *host, *bootfile;
252         char *p, *basedir, *url_str;
253         struct pb_url *url;
254
255         conffile = event_get_param(event, "pxeconffile");
256         pathprefix = event_get_param(event, "pxepathprefix");
257         bootfile = event_get_param(event, "bootfile");
258
259         /* If we're given a conf file, we're able to generate a complete URL to
260          * the configuration file, and the parser doesn't need to do any
261          * further autodiscovery */
262         *is_complete = !!conffile;
263
264         /* if conffile is a URL, that's all we need */
265         if (conffile && is_url(conffile)) {
266                 url = pb_url_parse(ctx, conffile);
267                 return url;
268         }
269
270         /* If we can create a URL from pathprefix (optionally with
271          * conffile appended to create a complete URL), use that */
272         if (pathprefix && is_url(pathprefix)) {
273                 if (conffile) {
274                         url_str = talloc_asprintf(ctx, "%s%s",
275                                         pathprefix, conffile);
276                         url = pb_url_parse(ctx, url_str);
277                         talloc_free(url_str);
278                 } else {
279                         url = pb_url_parse(ctx, pathprefix);
280                 }
281
282                 return url;
283         }
284
285         host = parse_host_addr(event);
286         if (!host) {
287                 pb_log("%s: host address not found\n", __func__);
288                 return NULL;
289         }
290
291         url_str = talloc_asprintf(ctx, "tftp://%s/", host);
292
293         /* if we have a pathprefix, use that directly.. */
294         if (pathprefix) {
295                 /* strip leading slashes */
296                 while (pathprefix[0] == '/')
297                         pathprefix++;
298                 url_str = talloc_asprintf_append(url_str, "%s", pathprefix);
299
300         /* ... otherwise, add a path based on the bootfile name, but only
301          * if conffile isn't an absolute path itself */
302         } else if (bootfile && !(conffile && conffile[0] == '/')) {
303
304                 basedir = talloc_strdup(ctx, bootfile);
305
306                 /* strip filename from the bootfile path, leaving only a
307                  * directory */
308                 p = strrchr(basedir, '/');
309                 if (!p)
310                         p = basedir;
311                 *p = '\0';
312
313                 if (strlen(basedir))
314                         url_str = talloc_asprintf_append(url_str, "%s/",
315                                         basedir);
316
317                 talloc_free(basedir);
318         }
319
320         /* finally, append conffile */
321         if (conffile)
322                 url_str = talloc_asprintf_append(url_str, "%s", conffile);
323
324         url = pb_url_parse(ctx, url_str);
325
326         talloc_free(url_str);
327
328         return url;
329 }
330
331 char **user_event_parse_conf_filenames(
332                 struct discover_context *ctx, struct event *event)
333 {
334         char *mac_addr, *ip_hex;
335         const char *mac, *ip;
336         char **filenames;
337         int index, len;
338
339         mac = event_get_param(event, "mac");
340         if (mac)
341                 mac_addr = parse_mac_addr(ctx, mac);
342         else
343                 mac_addr = NULL;
344
345         ip = event_get_param(event, "ip");
346         if (ip) {
347                 ip_hex = parse_ip_addr(ctx, ip);
348                 len = strlen(ip_hex);
349         } else {
350                 ip_hex = NULL;
351                 len = 0;
352         }
353
354         if (!mac_addr && !ip_hex) {
355                 pb_log("%s: neither mac nor ip parameter found\n", __func__);
356                 return NULL;
357         }
358
359         /* Filenames as fallback IP's + mac + default */
360         filenames = talloc_array(ctx, char *, len + 3);
361
362         index = 0;
363         if (mac_addr)
364                 filenames[index++] = talloc_strdup(filenames, mac_addr);
365
366         while (len) {
367                 filenames[index++] = talloc_strdup(filenames, ip_hex);
368                 ip_hex[--len] = '\0';
369         }
370
371         filenames[index++] = talloc_strdup(filenames, "default");
372         filenames[index++] = NULL;
373
374         if (mac_addr)
375                 talloc_free(mac_addr);
376
377         if (ip_hex)
378                 talloc_free(ip_hex);
379
380         return filenames;
381 }
382
383 static int user_event_dhcp(struct user_event *uev, struct event *event)
384 {
385         struct device_handler *handler = uev->handler;
386         struct discover_device *dev;
387
388         dev = discover_device_create(handler, event->device);
389
390         device_handler_dhcp(handler, dev, event);
391
392         return 0;
393 }
394
395 static int user_event_add(struct user_event *uev, struct event *event)
396 {
397         struct device_handler *handler = uev->handler;
398         struct discover_context *ctx;
399         struct discover_device *dev;
400
401         dev = discover_device_create(handler, event->device);
402         ctx = device_handler_discover_context_create(handler, dev);
403
404         parse_user_event(ctx, event);
405
406         device_handler_discover_context_commit(handler, ctx);
407
408         talloc_free(ctx);
409
410         return 0;
411 }
412
413 static int user_event_remove(struct user_event *uev, struct event *event)
414 {
415         struct device_handler *handler = uev->handler;
416         struct discover_device *dev;
417
418         dev = device_lookup_by_id(handler, event->device);
419         if (!dev)
420                 return 0;
421
422         device_handler_remove(handler, dev);
423
424         return 0;
425 }
426
427 static int user_event_url(struct user_event *uev, struct event *event)
428 {
429         struct device_handler *handler = uev->handler;
430         const char *url;
431
432         url = event_get_param(event, "url");
433         if (url)
434                 device_handler_process_url(handler, url, NULL, NULL);
435
436         return 0;
437 }
438
439 static int user_event_boot(struct user_event *uev, struct event *event)
440 {
441         struct device_handler *handler = uev->handler;
442         struct boot_command *cmd = talloc(handler, struct boot_command);
443
444         cmd->option_id = talloc_strdup(cmd, event_get_param(event, "id"));
445         cmd->boot_image_file = talloc_strdup(cmd, event_get_param(event, "image"));
446         cmd->initrd_file = talloc_strdup(cmd, event_get_param(event, "initrd"));
447         cmd->dtb_file = talloc_strdup(cmd, event_get_param(event, "dtb"));
448         cmd->boot_args = talloc_strdup(cmd, event_get_param(event, "args"));
449
450         device_handler_boot(handler, cmd);
451
452         talloc_free(cmd);
453
454         return 0;
455 }
456
457 static int user_event_sync(struct user_event *uev, struct event *event)
458 {
459         struct device_handler *handler = uev->handler;
460
461         if (strncasecmp(event->device, "all", strlen("all")) != 0)
462                 device_sync_snapshots(handler, event->device);
463         else
464                 device_sync_snapshots(handler, NULL);
465
466         return 0;
467 }
468
469 static void user_event_handle_message(struct user_event *uev, char *buf,
470         int len)
471 {
472         int result;
473         struct event *event;
474
475         event = talloc(uev, struct event);
476         event->type = EVENT_TYPE_USER;
477
478         result = event_parse_ad_message(event, buf, len);
479
480         if (result)
481                 return;
482
483         user_event_print_event(event);
484
485         switch (event->action) {
486         case EVENT_ACTION_ADD:
487                 result = user_event_add(uev, event);
488                 break;
489         case EVENT_ACTION_REMOVE:
490                 result = user_event_remove(uev, event);
491                 break;
492         case EVENT_ACTION_URL:
493                 result = user_event_url(uev, event);
494                 goto out;
495         case EVENT_ACTION_DHCP:
496                 result = user_event_dhcp(uev, event);
497                 goto out;
498         case EVENT_ACTION_BOOT:
499                 result = user_event_boot(uev, event);
500                 break;
501         case EVENT_ACTION_SYNC:
502                 result = user_event_sync(uev, event);
503                 break;
504         default:
505                 break;
506         }
507
508         /* user_event_url() and user_event_dhcp() will steal the event context,
509          * but all others still need to free */
510         talloc_free(event);
511 out:
512         return;
513 }
514
515 static int user_event_process(void *arg)
516 {
517         struct user_event *uev = arg;
518         char buf[PBOOT_USER_EVENT_SIZE + 1];
519         int len;
520
521         len = recvfrom(uev->socket, buf, PBOOT_USER_EVENT_SIZE, 0, NULL, NULL);
522
523         if (len < 0) {
524                 pb_log("%s: socket read failed: %s", __func__, strerror(errno));
525                 return 0;
526         }
527
528         if (len == 0) {
529                 pb_log("%s: empty", __func__);
530                 return 0;
531         }
532
533         buf[len] = '\0';
534
535         pb_debug("%s: %u bytes\n", __func__, len);
536
537         user_event_handle_message(uev, buf, len);
538
539         return 0;
540 }
541
542 static int user_event_destructor(void *arg)
543 {
544         struct user_event *uev = arg;
545
546         pb_debug("%s\n", __func__);
547
548         if (uev->socket >= 0)
549                 close(uev->socket);
550
551         return 0;
552 }
553
554 struct user_event *user_event_init(struct device_handler *handler,
555                 struct waitset *waitset)
556 {
557         struct sockaddr_un addr;
558         struct user_event *uev;
559
560         unlink(PBOOT_USER_EVENT_SOCKET);
561
562         uev = talloc(handler, struct user_event);
563
564         uev->handler = handler;
565
566         uev->socket = socket(AF_UNIX, SOCK_DGRAM, 0);
567         if (uev->socket < 0) {
568                 pb_log("%s: Error creating event handler socket: %s\n",
569                         __func__, strerror(errno));
570                 goto out_err;
571         }
572
573         talloc_set_destructor(uev, user_event_destructor);
574
575         memset(&addr, 0, sizeof(addr));
576         addr.sun_family = AF_UNIX;
577         strcpy(addr.sun_path, PBOOT_USER_EVENT_SOCKET);
578
579         if (bind(uev->socket, (struct sockaddr *)&addr, sizeof(addr))) {
580                 pb_log("Error binding event handler socket: %s\n",
581                         strerror(errno));
582         }
583
584         waiter_register_io(waitset, uev->socket, WAIT_IN,
585                         user_event_process, uev);
586
587         pb_debug("%s: waiting on %s\n", __func__, PBOOT_USER_EVENT_SOCKET);
588
589         return uev;
590
591 out_err:
592         talloc_free(uev);
593         return NULL;
594 }