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