]> git.ozlabs.org Git - petitboot/blob - discover/pxe-parser.c
discover/pxe-parser: Recognise plugin sources
[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 };
35
36 static void pxe_finish(struct conf_context *conf)
37 {
38         struct pxe_parser_info *info = conf->parser_info;
39         if (info->opt)
40                 discover_context_add_boot_option(conf->dc, info->opt);
41 }
42
43 /* We need a slightly modified version of pb_url_join, to allow for the
44  * pxelinux "::filename" syntax for absolute URLs
45  */
46 static struct pb_url *pxe_url_join(void *ctx, const struct pb_url *url,
47                 const char *s)
48 {
49         struct pb_url *new_url;
50         int len;
51
52         len = strlen(s);
53
54         if (len > 2 && s[0] == ':' && s[1] == ':') {
55                 char *tmp;
56
57                 if (s[2] == '/') {
58                         /* ::/path -> /path */
59                         tmp = talloc_strdup(ctx, s+2);
60                 } else {
61                         /* ::path -> /path */
62                         tmp = talloc_strdup(ctx, s+1);
63                         tmp[0] = '/';
64                 }
65
66                 new_url = pb_url_join(ctx, url, tmp);
67
68                 talloc_free(tmp);
69
70         } else {
71                 const char *tmp;
72                 /* strip leading slashes */
73                 for (tmp = s; *tmp == '/'; tmp++)
74                         ;
75                 new_url = pb_url_join(ctx, url, tmp);
76         }
77
78         return new_url;
79 }
80
81 static void pxe_append_string(struct discover_boot_option *opt,
82                 const char *str)
83 {
84         if (opt->option->boot_args)
85                 opt->option->boot_args = talloc_asprintf_append(
86                                 opt->option->boot_args, " %s", str);
87         else
88                 opt->option->boot_args = talloc_strdup(opt->option, str);
89 }
90
91 static char *pxe_sysappend_mac(void *ctx, uint8_t *mac)
92 {
93         if (!mac)
94                 return NULL;
95
96         return talloc_asprintf(ctx,
97                 "BOOTIF=01-%02x-%02x-%02x-%02x-%02x-%02x",
98                 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
99 }
100
101 static void pxe_process_sysappend(struct discover_context *ctx,
102                 struct discover_boot_option *opt,
103                 unsigned long val)
104 {
105         struct event *event = ctx->event;
106         char *str = NULL;
107
108         if (!event)
109                 return;
110
111         if (val & 0x2) {
112                 uint8_t *mac = find_mac_by_name(ctx,
113                                 device_handler_get_network(ctx->handler),
114                                 event->device);
115                 str = pxe_sysappend_mac(ctx, mac);
116                 if (str) {
117                         pxe_append_string(opt, str);
118                         talloc_free(str);
119                 }
120                 val &= ~0x2;
121         }
122
123         if (val)
124                 pb_log("pxe: unsupported features requested in "
125                                 "ipappend/sysappend: 0x%04lx", val);
126
127 }
128
129 static void pxe_process_pair(struct conf_context *ctx,
130                 const char *name, char *value)
131 {
132         struct pxe_parser_info *parser_info = ctx->parser_info;
133         struct discover_boot_option *opt = parser_info->opt;
134         struct pb_url *url;
135
136         /* quirk in the syslinux config format: initrd can be separated
137          * by an '=' */
138         if (!name && !strncasecmp(value, "initrd=", strlen("initrd="))) {
139                 name = "initrd";
140                 value += strlen("initrd=");
141         }
142
143         if (!name)
144                 return;
145
146         if (streq(name, "DEFAULT")) {
147                 parser_info->default_name = talloc_strdup(parser_info, value);
148                 return;
149         }
150
151         if (streq(name, "LABEL") || streq(name, "PLUGIN")) {
152                 if (opt)
153                         pxe_finish(ctx);
154
155                 opt = discover_boot_option_create(ctx->dc, ctx->dc->device);
156
157                 opt->option->name = talloc_strdup(opt, value);
158                 opt->option->id = talloc_asprintf(opt, "%s@%p",
159                                 ctx->dc->device->device->id, opt);
160
161                 if (streq(name, "LABEL")) {
162                         opt->option->type = DISCOVER_BOOT_OPTION;
163                         opt->option->is_default = parser_info->default_name &&
164                                                 streq(parser_info->default_name, value);
165                 } else
166                         opt->option->type = DISCOVER_PLUGIN_OPTION;
167
168                 parser_info->opt = opt;
169                 return;
170         }
171
172         /* all other parameters need an option */
173         if (!opt)
174                 return;
175
176         if (streq(name, "TARBALL") &&
177                         opt->option->type == DISCOVER_PLUGIN_OPTION) {
178                 url = pxe_url_join(ctx->dc, ctx->dc->conf_url, value);
179                 opt->boot_image = create_url_resource(opt, url);
180                 /* All other options apply to boot options only */
181                 return;
182         }
183
184         if (streq(name, "KERNEL")) {
185                 url = pxe_url_join(ctx->dc, ctx->dc->conf_url, value);
186                 opt->boot_image = create_url_resource(opt, url);
187
188                 char* args_sigfile_default = talloc_asprintf(opt,
189                         "%s.cmdline.sig", value);
190                 url = pxe_url_join(ctx->dc, ctx->dc->conf_url,
191                         args_sigfile_default);
192                 opt->args_sig_file = create_url_resource(opt, url);
193                 talloc_free(args_sigfile_default);
194
195         } else if (streq(name, "INITRD")) {
196                 url = pxe_url_join(ctx->dc, ctx->dc->conf_url, value);
197                 opt->initrd = create_url_resource(opt, url);
198
199         } else if (streq(name, "APPEND")) {
200                 char *str, *end;
201
202                 pxe_append_string(opt, value);
203
204                 str = strcasestr(value, "INITRD=");
205                 if (str) {
206                         str += strlen("INITRD=");
207                         end = strchrnul(str, ' ');
208                         *end = '\0';
209
210                         url = pxe_url_join(ctx->dc, ctx->dc->conf_url, str);
211                         opt->initrd = create_url_resource(opt, url);
212                 }
213         } else if (streq(name, "SYSAPPEND") || streq(name, "IPAPPEND")) {
214                 unsigned long type;
215                 char *end;
216
217                 type = strtoul(value, &end, 10);
218                 if (end != value && !(*end))
219                         pxe_process_sysappend(ctx->dc, opt, type);
220
221         } else if (streq(name, "DTB") || streq(name, "FDT")) {
222                 url = pxe_url_join(ctx->dc, ctx->dc->conf_url, value);
223                 opt->dtb = create_url_resource(opt, url);
224         }
225 }
226
227 static void pxe_load_next_filename(struct conf_context *conf)
228 {
229         struct pxe_parser_info *info = conf->parser_info;
230         struct pb_url *url;
231
232         if (!info->pxe_conf_files)
233                 return;
234
235         for (; info->pxe_conf_files[info->current]; info->current++) {
236                 url = pb_url_join(conf->dc, info->pxe_base_url,
237                                   info->pxe_conf_files[info->current]);
238                 if (!url)
239                         continue;
240
241                 if (load_url_async(conf, url, pxe_conf_parse_cb, conf,
242                                    NULL, NULL))
243                         break;
244         }
245
246         return;
247 }
248
249 /*
250  * Callback for asynchronous loads from pxe_parse()
251  * @param result Result of load_url_async()
252  * @param data   Pointer to associated conf_context
253  */
254 static void pxe_conf_parse_cb(struct load_url_result *result, void *data)
255 {
256         struct conf_context *conf = data;
257         struct device_handler *handler;
258         struct pxe_parser_info *info;
259         char *buf = NULL;
260         int len, rc = 0;
261
262         if (!data)
263                 return;
264
265         handler = talloc_parent(conf);
266
267         if (result && result->status == LOAD_OK)
268                 rc = read_file(conf, result->local, &buf, &len);
269         if (!result || result->status != LOAD_OK || rc) {
270                 /* This load failed so try the next available filename */
271                 info = conf->parser_info;
272                 if (!info->pxe_conf_files) {
273                         device_handler_status_dev_err(handler,
274                                         conf->dc->device,
275                                         _("Failed to download %s"),
276                                         pb_url_to_string(result->url));
277
278                         return;
279                 }
280
281                 info->current++;
282                 pxe_load_next_filename(conf);
283                 if (info->pxe_conf_files[info->current] == NULL) {
284                         /* Nothing left to try */
285                         device_handler_status_dev_err(handler,
286                                         conf->dc->device,
287                                         _("PXE autoconfiguration failed"));
288                         goto out_clean;
289                 }
290                 return;
291         }
292
293         /*
294          * Parse the first successfully downloaded file. We only want to parse
295          * the first because otherwise we could parse options from both a
296          * machine-specific config and a 'fallback' default config
297          */
298
299         conf_parse_buf(conf, buf, len);
300
301         /* We may be called well after the original caller of iterate_parsers(),
302          * commit any new boot options ourselves */
303         device_handler_discover_context_commit(handler, conf->dc);
304
305         /*
306          * TRANSLATORS: the format specifier in this string is a URL
307          * eg. tftp://192.168.1.1/pxelinux.cfg
308          */
309         device_handler_status_dev_info(handler, conf->dc->device,
310                         _("Parsed PXE config from %s"),
311                         pb_url_to_string(result->url));
312
313         talloc_free(buf);
314 out_clean:
315         if (result->cleanup_local)
316                 unlink(result->local);
317         talloc_free(conf);
318 }
319
320 /**
321  * Return a new conf_context and increment the talloc reference count on
322  * the discover_context struct.
323  * @param  ctx  Parent talloc context
324  * @param  orig Original discover_context
325  * @return      Pointer to new conf_context
326  */
327 static struct conf_context *copy_context(void *ctx, struct discover_context *dc)
328 {
329         struct pxe_parser_info *info;
330         struct conf_context *conf;
331
332         conf = talloc_zero(ctx, struct conf_context);
333
334         if (!conf)
335                 return NULL;
336
337         conf->get_pair = conf_get_pair_space;
338         conf->process_pair = pxe_process_pair;
339         conf->finish = pxe_finish;
340         info = talloc_zero(conf, struct pxe_parser_info);
341         if (!info) {
342                 talloc_free(conf);
343                 return NULL;
344         }
345         conf->parser_info = info;
346
347         /*
348          * The discover_context may be freed once pxe_parse() returns, but the
349          * callback will still need it. Take a reference so that that it will
350          * persist until the last callback completes.
351          */
352         conf->dc = talloc_reference(conf, dc);
353
354         return conf;
355 }
356
357 static int pxe_parse(struct discover_context *dc)
358 {
359         struct pb_url *pxe_base_url;
360         struct conf_context *conf = NULL;
361         struct load_url_result *result;
362         void *ctx = talloc_parent(dc);
363         struct pxe_parser_info *info;
364         char **pxe_conf_files;
365         bool complete_url;
366
367         /* Expects dhcp event parameters to support network boot */
368         if (!dc->event)
369                 return -1;
370
371         dc->conf_url = user_event_parse_conf_url(dc, dc->event, &complete_url);
372         if (!dc->conf_url)
373                 return -1;
374
375         /*
376          * Retrieving PXE configs over the network can take some time depending
377          * on factors such as slow network, malformed paths, bad DNS, and
378          * overzealous firewalls. Instead of blocking the discover server while
379          * we wait for these, spawn an asynchronous job that will attempt to
380          * retrieve each possible URL until it successfully finds one, and
381          * parse and process the resulting file in a callback.
382          */
383         conf = copy_context(ctx, dc);
384         if (!conf)
385                 return -1;
386
387         if (complete_url) {
388                 device_handler_status_dev_info(conf->dc->handler,
389                         dc->device,
390                         _("Requesting config %s"),
391                         pb_url_to_string(conf->dc->conf_url));
392
393                 /* we have a complete URL; use this and we're done. */
394                 result = load_url_async(conf->dc, conf->dc->conf_url,
395                                         pxe_conf_parse_cb, conf, NULL, ctx);
396                 if (!result) {
397                         pb_log("load_url_async fails for %s\n",
398                                         dc->conf_url->path);
399                         goto out_conf;
400                 } else if (result->status == LOAD_OK) {
401                         /* Local load - call pxe_conf_parse_cb() now */
402                         pxe_conf_parse_cb(result, conf);
403                 }
404         } else {
405                 pxe_conf_files = user_event_parse_conf_filenames(dc, dc->event);
406                 if (!pxe_conf_files)
407                         goto out_conf;
408
409                 pxe_base_url = pb_url_join(dc, dc->conf_url, pxelinux_prefix);
410                 if (!pxe_base_url)
411                         goto out_pxe_conf;
412
413                 info = conf->parser_info;
414                 info->pxe_conf_files = pxe_conf_files;
415                 info->pxe_base_url = pxe_base_url;
416
417                 device_handler_status_dev_info(conf->dc->handler,
418                         conf->dc->device,
419                         _("Probing from base %s"),
420                         pb_url_to_string(pxe_base_url));
421
422                 pxe_load_next_filename(conf);
423         }
424
425         return 0;
426
427 out_pxe_conf:
428         talloc_free(pxe_conf_files);
429 out_conf:
430         talloc_free(conf);
431         return -1;
432 }
433
434 static struct parser pxe_parser = {
435         .name                   = "pxe",
436         .parse                  = pxe_parse,
437 };
438
439 register_parser(pxe_parser);