]> git.ozlabs.org Git - petitboot/blob - discover/pxe-parser.c
discover/paths: Cleanup res after getaddrinfo
[petitboot] / discover / pxe-parser.c
1
2 #if defined(HAVE_CONFIG_H)
3 #include "config.h"
4 #endif
5 #include <stdlib.h>
6 #include <string.h>
7
8 #include <talloc/talloc.h>
9 #include <url/url.h>
10 #include <log/log.h>
11 #include <file/file.h>
12 #include <i18n/i18n.h>
13
14 #include "device-handler.h"
15 #include "parser.h"
16 #include "parser-conf.h"
17 #include "parser-utils.h"
18 #include "resource.h"
19 #include "paths.h"
20 #include "event.h"
21 #include "user-event.h"
22 #include "network.h"
23
24 static const char *pxelinux_prefix = "pxelinux.cfg/";
25
26 static void pxe_conf_parse_cb(struct load_url_result *result, void *data);
27
28 struct pxe_parser_info {
29         struct discover_boot_option     *opt;
30         const char                      *default_name;
31         char                            **pxe_conf_files;
32         struct pb_url                   *pxe_base_url;
33         int                             current;
34         char                            *proxy;
35 };
36
37 static void pxe_finish(struct conf_context *conf)
38 {
39         struct pxe_parser_info *info = conf->parser_info;
40         if (info->opt) {
41                 if (info->proxy)
42                         info->opt->proxy = talloc_strdup(info->opt, info->proxy);
43                 discover_context_add_boot_option(conf->dc, info->opt);
44                 info->opt = NULL;
45         }
46 }
47
48 /* We need a slightly modified version of pb_url_join, to allow for the
49  * pxelinux "::filename" syntax for absolute URLs
50  */
51 static struct pb_url *pxe_url_join(void *ctx, const struct pb_url *url,
52                 const char *s)
53 {
54         struct pb_url *new_url;
55         int len;
56
57         len = strlen(s);
58
59         if (len > 2 && s[0] == ':' && s[1] == ':') {
60                 char *tmp;
61
62                 if (s[2] == '/') {
63                         /* ::/path -> /path */
64                         tmp = talloc_strdup(ctx, s+2);
65                 } else {
66                         /* ::path -> /path */
67                         tmp = talloc_strdup(ctx, s+1);
68                         tmp[0] = '/';
69                 }
70
71                 new_url = pb_url_join(ctx, url, tmp);
72
73                 talloc_free(tmp);
74
75         } else {
76                 const char *tmp;
77                 /* strip leading slashes */
78                 for (tmp = s; *tmp == '/'; tmp++)
79                         ;
80                 new_url = pb_url_join(ctx, url, tmp);
81         }
82
83         return new_url;
84 }
85
86 static void pxe_append_string(struct discover_boot_option *opt,
87                 const char *str)
88 {
89         if (opt->option->boot_args)
90                 opt->option->boot_args = talloc_asprintf_append(
91                                 opt->option->boot_args, " %s", str);
92         else
93                 opt->option->boot_args = talloc_strdup(opt->option, str);
94 }
95
96 static char *pxe_sysappend_mac(void *ctx, uint8_t *mac)
97 {
98         if (!mac)
99                 return NULL;
100
101         return talloc_asprintf(ctx,
102                 "BOOTIF=01-%02x-%02x-%02x-%02x-%02x-%02x",
103                 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
104 }
105
106 static void pxe_process_sysappend(struct discover_context *ctx,
107                 struct discover_boot_option *opt,
108                 unsigned long val)
109 {
110         struct event *event = ctx->event;
111         char *str = NULL;
112
113         if (!event)
114                 return;
115
116         if (val & 0x2) {
117                 uint8_t *mac = find_mac_by_name(ctx,
118                                 device_handler_get_network(ctx->handler),
119                                 event->device);
120                 str = pxe_sysappend_mac(ctx, mac);
121                 if (str) {
122                         pxe_append_string(opt, str);
123                         talloc_free(str);
124                 }
125                 val &= ~0x2;
126         }
127
128         if (val)
129                 pb_log("pxe: unsupported features requested in "
130                                 "ipappend/sysappend: 0x%04lx", val);
131
132 }
133
134 static void pxe_process_pair(struct conf_context *ctx,
135                 const char *name, char *value)
136 {
137         struct pxe_parser_info *parser_info = ctx->parser_info;
138         struct discover_boot_option *opt = parser_info->opt;
139         struct pb_url *url;
140
141         /* quirk in the syslinux config format: initrd can be separated
142          * by an '=' */
143         if (!name && !strncasecmp(value, "initrd=", strlen("initrd="))) {
144                 name = "initrd";
145                 value += strlen("initrd=");
146         }
147
148         if (!name)
149                 return;
150
151         if (streq(name, "DEFAULT")) {
152                 parser_info->default_name = talloc_strdup(parser_info, value);
153                 return;
154         }
155
156         if (streq(name, "PROXY")) {
157                 if (parser_info->proxy)
158                         talloc_free(parser_info->proxy);
159
160                 parser_info->proxy = talloc_strdup(parser_info, value);
161                 return;
162         }
163
164         if (streq(name, "LABEL") || streq(name, "PLUGIN")) {
165                 if (opt)
166                         pxe_finish(ctx);
167
168                 opt = discover_boot_option_create(ctx->dc, ctx->dc->device);
169
170                 opt->option->name = talloc_strdup(opt, value);
171                 opt->option->id = talloc_asprintf(opt, "%s@%p",
172                                 ctx->dc->device->device->id, opt);
173
174                 if (streq(name, "LABEL")) {
175                         opt->option->type = DISCOVER_BOOT_OPTION;
176                         opt->option->is_default = parser_info->default_name &&
177                                                 streq(parser_info->default_name, value);
178                 } else
179                         opt->option->type = DISCOVER_PLUGIN_OPTION;
180
181                 parser_info->opt = opt;
182                 return;
183         }
184
185         /* all other parameters need an option */
186         if (!opt)
187                 return;
188
189         if (streq(name, "TARBALL") &&
190                         opt->option->type == DISCOVER_PLUGIN_OPTION) {
191                 url = pxe_url_join(ctx->dc, ctx->dc->conf_url, value);
192                 opt->boot_image = create_url_resource(opt, url);
193                 /* All other options apply to boot options only */
194                 return;
195         }
196
197         if (streq(name, "KERNEL")) {
198                 url = pxe_url_join(ctx->dc, ctx->dc->conf_url, value);
199                 opt->boot_image = create_url_resource(opt, url);
200
201                 char* args_sigfile_default = talloc_asprintf(opt,
202                         "%s.cmdline.sig", value);
203                 url = pxe_url_join(ctx->dc, ctx->dc->conf_url,
204                         args_sigfile_default);
205                 opt->args_sig_file = create_url_resource(opt, url);
206                 talloc_free(args_sigfile_default);
207
208         } else if (streq(name, "INITRD")) {
209                 url = pxe_url_join(ctx->dc, ctx->dc->conf_url, value);
210                 opt->initrd = create_url_resource(opt, url);
211
212         } else if (streq(name, "APPEND")) {
213                 char *str, *end;
214
215                 pxe_append_string(opt, value);
216
217                 str = strcasestr(value, "INITRD=");
218                 if (str) {
219                         str += strlen("INITRD=");
220                         end = strchrnul(str, ' ');
221                         *end = '\0';
222
223                         url = pxe_url_join(ctx->dc, ctx->dc->conf_url, str);
224                         opt->initrd = create_url_resource(opt, url);
225                 }
226         } else if (streq(name, "SYSAPPEND") || streq(name, "IPAPPEND")) {
227                 unsigned long type;
228                 char *end;
229
230                 type = strtoul(value, &end, 10);
231                 if (end != value && !(*end))
232                         pxe_process_sysappend(ctx->dc, opt, type);
233
234         } else if (streq(name, "DTB") || streq(name, "FDT")) {
235                 url = pxe_url_join(ctx->dc, ctx->dc->conf_url, value);
236                 opt->dtb = create_url_resource(opt, url);
237         }
238 }
239
240 static void pxe_load_next_filename(struct conf_context *conf)
241 {
242         struct pxe_parser_info *info = conf->parser_info;
243         struct pb_url *url;
244
245         if (!info->pxe_conf_files)
246                 return;
247
248         for (; info->pxe_conf_files[info->current]; info->current++) {
249                 url = pb_url_join(conf->dc, info->pxe_base_url,
250                                   info->pxe_conf_files[info->current]);
251                 if (!url)
252                         continue;
253
254                 if (load_url_async(conf, url, pxe_conf_parse_cb, conf,
255                                    NULL, NULL))
256                         break;
257         }
258
259         return;
260 }
261
262 /*
263  * Parse a limited set of iPXE commands. This is handled separately from
264  * conf_parse_buf() since not all commands will have a value.
265  */
266 static bool ipxe_simple_parser(struct conf_context *ctx, char *buf, int len)
267 {
268         struct discover_boot_option *opt;
269         char *p, *name, *value;
270         struct pb_url *url;
271
272         p = strstr(buf, "#!ipxe");
273
274         /* Only try to parse this if the ipxe header is right at the start */
275         if (!p || p != buf)
276                 return false;
277         p = strchr(buf, '\n') + 1;
278
279         opt = discover_boot_option_create(ctx->dc, ctx->dc->device);
280
281         opt->option->id = talloc_asprintf(opt, "%s@%p",
282                         ctx->dc->device->device->id, opt);
283         opt->option->type = DISCOVER_BOOT_OPTION;
284
285         while (p && p <= buf + len) {
286
287                 p = conf_get_pair(ctx, p, &name, &value, ' ', '\n');
288
289                 /* The 'boot' command appears by itself without options */
290                 if (!name && value && streq(value, "boot")) {
291                         opt->option->is_default = true;
292                         continue;
293                 }
294
295                 /* All other parameters require a value */
296                 if (!value) {
297                         pb_debug("%s: '%s' missing value\n", __func__, name);
298                         continue;
299                 }
300
301                 if (streq(name, "kernel")) {
302                         char *args, *name = NULL, *saveptr = NULL, *tmp;
303
304                         /*
305                          * value is of the form:
306                          * boot_image [--name option_name ] [option args ...]
307                          */
308                         tmp = strtok_r(value, " ", &saveptr);
309                         if (!tmp) {
310                                 pb_log("%s: malformed kernel statement\n", __func__);
311                                 break;
312                         }
313
314                         url = pxe_url_join(ctx->dc, ctx->dc->conf_url, tmp);
315                         opt->boot_image = create_url_resource(opt, url);
316
317                         tmp = strtok_r(NULL, " ", &saveptr);
318                         if (!tmp)
319                                 continue;
320                         if (streq(tmp, "--name")) {
321                                 tmp = strtok_r(NULL, " ", &saveptr);
322                                 name = talloc_asprintf(opt, "%s",
323                                                 tmp ?: "malformed ipxe option");
324                                 args = strtok_r(NULL, " ", &saveptr);
325                         } else
326                                 args = tmp;
327
328                         while (args) {
329                                 pxe_append_string(opt, args);
330                                 args = strtok_r(NULL, " ", &saveptr);
331                         }
332
333                         opt->option->name = name ?: talloc_asprintf(opt,
334                                         "ipxe option (%s)", url->full);
335                         continue;
336                 }
337
338                 if (streq(name, "initrd")) {
339                         url = pxe_url_join(ctx->dc, ctx->dc->conf_url, value);
340                         opt->initrd = create_url_resource(opt, url);
341                         continue;
342                 }
343         }
344
345         if (opt->boot_image)
346                 discover_context_add_boot_option(ctx->dc, opt);
347         else
348                 pb_debug("ipxe file did not contain a valid option\n");
349
350         return true;
351 }
352
353 /*
354  * Callback for asynchronous loads from pxe_parse()
355  * @param result Result of load_url_async()
356  * @param data   Pointer to associated conf_context
357  */
358 static void pxe_conf_parse_cb(struct load_url_result *result, void *data)
359 {
360         struct conf_context *conf = data;
361         struct device_handler *handler;
362         struct pxe_parser_info *info;
363         char *buf = NULL;
364         int len, rc = 0;
365
366         if (!data)
367                 return;
368         if (!result)
369                 goto out_clean;
370
371         handler = talloc_parent(conf);
372
373         if (result->status == LOAD_OK)
374                 rc = read_file(conf, result->local, &buf, &len);
375         if (result->status != LOAD_OK || rc) {
376                 /* This load failed so try the next available filename */
377                 info = conf->parser_info;
378                 if (!info->pxe_conf_files) {
379                         device_handler_status_dev_err(handler,
380                                         conf->dc->device,
381                                         _("Failed to download %s"),
382                                         pb_url_to_string(result->url));
383
384                         return;
385                 }
386
387                 info->current++;
388                 pxe_load_next_filename(conf);
389                 if (info->pxe_conf_files[info->current] == NULL) {
390                         /* Nothing left to try */
391                         device_handler_status_dev_err(handler,
392                                         conf->dc->device,
393                                         _("PXE autoconfiguration failed"));
394                         goto out_clean;
395                 }
396                 return;
397         }
398
399         /*
400          * Parse the first successfully downloaded file. We only want to parse
401          * the first because otherwise we could parse options from both a
402          * machine-specific config and a 'fallback' default config
403          * We also check if the file is in the limited ipxe format.
404          */
405         if (!ipxe_simple_parser(conf, buf, len))
406                 conf_parse_buf(conf, buf, len);
407
408         /* We may be called well after the original caller of iterate_parsers(),
409          * commit any new boot options ourselves */
410         device_handler_discover_context_commit(handler, conf->dc);
411
412         /*
413          * TRANSLATORS: the format specifier in this string is a URL
414          * eg. tftp://192.168.1.1/pxelinux.cfg
415          */
416         device_handler_status_dev_info(handler, conf->dc->device,
417                         _("Parsed PXE config from %s"),
418                         pb_url_to_string(result->url));
419
420         talloc_free(buf);
421 out_clean:
422         if (result && result->cleanup_local)
423                 unlink(result->local);
424         talloc_free(conf);
425 }
426
427 /**
428  * Return a new conf_context and increment the talloc reference count on
429  * the discover_context struct.
430  * @param  ctx  Parent talloc context
431  * @param  orig Original discover_context
432  * @return      Pointer to new conf_context
433  */
434 static struct conf_context *copy_context(void *ctx, struct discover_context *dc)
435 {
436         struct pxe_parser_info *info;
437         struct conf_context *conf;
438
439         conf = talloc_zero(ctx, struct conf_context);
440
441         if (!conf)
442                 return NULL;
443
444         conf->get_pair = conf_get_pair_space;
445         conf->process_pair = pxe_process_pair;
446         conf->finish = pxe_finish;
447         info = talloc_zero(conf, struct pxe_parser_info);
448         if (!info) {
449                 talloc_free(conf);
450                 return NULL;
451         }
452         conf->parser_info = info;
453
454         /*
455          * The discover_context may be freed once pxe_parse() returns, but the
456          * callback will still need it. Take a reference so that that it will
457          * persist until the last callback completes.
458          */
459         conf->dc = talloc_reference(conf, dc);
460
461         return conf;
462 }
463
464 static int pxe_parse(struct discover_context *dc)
465 {
466         struct pb_url *pxe_base_url, *file_url;
467         struct conf_context *conf = NULL;
468         struct load_url_result *result;
469         void *ctx = talloc_parent(dc);
470         struct pxe_parser_info *info;
471         char **pxe_conf_files;
472         bool complete_url;
473
474         /* Expects dhcp event parameters to support network boot */
475         if (!dc->event)
476                 return -1;
477
478         dc->conf_url = user_event_parse_conf_url(dc, dc->event, &complete_url);
479         if (!dc->conf_url)
480                 return -1;
481
482         /*
483          * Retrieving PXE configs over the network can take some time depending
484          * on factors such as slow network, malformed paths, bad DNS, and
485          * overzealous firewalls. Instead of blocking the discover server while
486          * we wait for these, spawn an asynchronous job that will attempt to
487          * retrieve each possible URL until it successfully finds one, and
488          * parse and process the resulting file in a callback.
489          */
490         conf = copy_context(ctx, dc);
491         if (!conf)
492                 return -1;
493
494         if (complete_url) {
495                 /* Check if this file has already been downloaded */
496                 if (event_get_param(dc->event, "pxeconffile-local"))
497                         file_url = pb_url_parse(dc, event_get_param(dc->event,
498                                                 "pxeconffile-local"));
499                 else
500                         file_url = dc->conf_url;
501
502                 if (!file_url) {
503                         pb_log("%s: Failed to parse conf url!\n", __func__);
504                         goto out_conf;
505                 }
506
507                 device_handler_status_dev_info(conf->dc->handler,
508                         dc->device,
509                         _("Requesting config %s"),
510                         pb_url_to_string(conf->dc->conf_url));
511
512                 /* we have a complete URL; use this and we're done. */
513                 result = load_url_async(conf->dc, file_url,
514                                         pxe_conf_parse_cb, conf, NULL, ctx);
515                 if (!result) {
516                         pb_log("load_url_async fails for %s\n",
517                                         dc->conf_url->path);
518                         goto out_conf;
519                 }
520         } else {
521                 pxe_conf_files = user_event_parse_conf_filenames(dc, dc->event);
522                 if (!pxe_conf_files)
523                         goto out_conf;
524
525                 pxe_base_url = pb_url_join(dc, dc->conf_url, pxelinux_prefix);
526                 if (!pxe_base_url)
527                         goto out_pxe_conf;
528
529                 info = conf->parser_info;
530                 info->pxe_conf_files = pxe_conf_files;
531                 info->pxe_base_url = pxe_base_url;
532
533                 device_handler_status_dev_info(conf->dc->handler,
534                         conf->dc->device,
535                         _("Probing from base %s"),
536                         pb_url_to_string(pxe_base_url));
537
538                 pxe_load_next_filename(conf);
539         }
540
541         return 0;
542
543 out_pxe_conf:
544         talloc_free(pxe_conf_files);
545 out_conf:
546         talloc_free(conf);
547         return -1;
548 }
549
550 static struct parser pxe_parser = {
551         .name                   = "pxe",
552         .parse                  = pxe_parse,
553 };
554
555 register_parser(pxe_parser);