2 #if defined(HAVE_CONFIG_H)
8 #include <talloc/talloc.h>
11 #include <file/file.h>
12 #include <i18n/i18n.h>
14 #include "device-handler.h"
16 #include "parser-conf.h"
17 #include "parser-utils.h"
21 #include "user-event.h"
24 static const char *pxelinux_prefix = "pxelinux.cfg/";
26 static void pxe_conf_parse_cb(struct load_url_result *result, void *data);
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;
37 static void pxe_finish(struct conf_context *conf)
39 struct pxe_parser_info *info = conf->parser_info;
42 info->opt->proxy = talloc_strdup(info->opt, info->proxy);
43 discover_context_add_boot_option(conf->dc, info->opt);
48 /* We need a slightly modified version of pb_url_join, to allow for the
49 * pxelinux "::filename" syntax for absolute URLs
51 static struct pb_url *pxe_url_join(void *ctx, const struct pb_url *url,
54 struct pb_url *new_url;
59 if (len > 2 && s[0] == ':' && s[1] == ':') {
63 /* ::/path -> /path */
64 tmp = talloc_strdup(ctx, s+2);
67 tmp = talloc_strdup(ctx, s+1);
71 new_url = pb_url_join(ctx, url, tmp);
77 /* strip leading slashes */
78 for (tmp = s; *tmp == '/'; tmp++)
80 new_url = pb_url_join(ctx, url, tmp);
86 static void pxe_append_string(struct discover_boot_option *opt,
89 if (opt->option->boot_args)
90 opt->option->boot_args = talloc_asprintf_append(
91 opt->option->boot_args, " %s", str);
93 opt->option->boot_args = talloc_strdup(opt->option, str);
96 static char *pxe_sysappend_mac(void *ctx, uint8_t *mac)
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]);
106 static void pxe_process_sysappend(struct discover_context *ctx,
107 struct discover_boot_option *opt,
110 struct event *event = ctx->event;
117 uint8_t *mac = find_mac_by_name(ctx,
118 device_handler_get_network(ctx->handler),
120 str = pxe_sysappend_mac(ctx, mac);
122 pxe_append_string(opt, str);
129 pb_log("pxe: unsupported features requested in "
130 "ipappend/sysappend: 0x%04lx", val);
134 static void pxe_process_pair(struct conf_context *ctx,
135 const char *name, char *value)
137 struct pxe_parser_info *parser_info = ctx->parser_info;
138 struct discover_boot_option *opt = parser_info->opt;
141 /* quirk in the syslinux config format: initrd can be separated
143 if (!name && !strncasecmp(value, "initrd=", strlen("initrd="))) {
145 value += strlen("initrd=");
151 if (streq(name, "DEFAULT")) {
152 parser_info->default_name = talloc_strdup(parser_info, value);
156 if (streq(name, "PROXY")) {
157 if (parser_info->proxy)
158 talloc_free(parser_info->proxy);
160 parser_info->proxy = talloc_strdup(parser_info, value);
164 if (streq(name, "LABEL") || streq(name, "PLUGIN")) {
168 opt = discover_boot_option_create(ctx->dc, ctx->dc->device);
170 opt->option->name = talloc_strdup(opt, value);
171 opt->option->id = talloc_asprintf(opt, "%s@%p",
172 ctx->dc->device->device->id, opt);
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);
179 opt->option->type = DISCOVER_PLUGIN_OPTION;
181 parser_info->opt = opt;
185 /* all other parameters need an option */
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 */
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);
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);
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);
212 } else if (streq(name, "APPEND")) {
215 pxe_append_string(opt, value);
217 str = strcasestr(value, "INITRD=");
219 str += strlen("INITRD=");
220 end = strchrnul(str, ' ');
223 url = pxe_url_join(ctx->dc, ctx->dc->conf_url, str);
224 opt->initrd = create_url_resource(opt, url);
226 } else if (streq(name, "SYSAPPEND") || streq(name, "IPAPPEND")) {
230 type = strtoul(value, &end, 10);
231 if (end != value && !(*end))
232 pxe_process_sysappend(ctx->dc, opt, type);
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);
240 static void pxe_load_next_filename(struct conf_context *conf)
242 struct pxe_parser_info *info = conf->parser_info;
245 if (!info->pxe_conf_files)
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]);
254 if (load_url_async(conf, url, pxe_conf_parse_cb, conf,
263 * Callback for asynchronous loads from pxe_parse()
264 * @param result Result of load_url_async()
265 * @param data Pointer to associated conf_context
267 static void pxe_conf_parse_cb(struct load_url_result *result, void *data)
269 struct conf_context *conf = data;
270 struct device_handler *handler;
271 struct pxe_parser_info *info;
280 handler = talloc_parent(conf);
282 if (result->status == LOAD_OK)
283 rc = read_file(conf, result->local, &buf, &len);
284 if (result->status != LOAD_OK || rc) {
285 /* This load failed so try the next available filename */
286 info = conf->parser_info;
287 if (!info->pxe_conf_files) {
288 device_handler_status_dev_err(handler,
290 _("Failed to download %s"),
291 pb_url_to_string(result->url));
297 pxe_load_next_filename(conf);
298 if (info->pxe_conf_files[info->current] == NULL) {
299 /* Nothing left to try */
300 device_handler_status_dev_err(handler,
302 _("PXE autoconfiguration failed"));
309 * Parse the first successfully downloaded file. We only want to parse
310 * the first because otherwise we could parse options from both a
311 * machine-specific config and a 'fallback' default config
314 conf_parse_buf(conf, buf, len);
316 /* We may be called well after the original caller of iterate_parsers(),
317 * commit any new boot options ourselves */
318 device_handler_discover_context_commit(handler, conf->dc);
321 * TRANSLATORS: the format specifier in this string is a URL
322 * eg. tftp://192.168.1.1/pxelinux.cfg
324 device_handler_status_dev_info(handler, conf->dc->device,
325 _("Parsed PXE config from %s"),
326 pb_url_to_string(result->url));
330 if (result && result->cleanup_local)
331 unlink(result->local);
336 * Return a new conf_context and increment the talloc reference count on
337 * the discover_context struct.
338 * @param ctx Parent talloc context
339 * @param orig Original discover_context
340 * @return Pointer to new conf_context
342 static struct conf_context *copy_context(void *ctx, struct discover_context *dc)
344 struct pxe_parser_info *info;
345 struct conf_context *conf;
347 conf = talloc_zero(ctx, struct conf_context);
352 conf->get_pair = conf_get_pair_space;
353 conf->process_pair = pxe_process_pair;
354 conf->finish = pxe_finish;
355 info = talloc_zero(conf, struct pxe_parser_info);
360 conf->parser_info = info;
363 * The discover_context may be freed once pxe_parse() returns, but the
364 * callback will still need it. Take a reference so that that it will
365 * persist until the last callback completes.
367 conf->dc = talloc_reference(conf, dc);
372 static int pxe_parse(struct discover_context *dc)
374 struct pb_url *pxe_base_url, *file_url;
375 struct conf_context *conf = NULL;
376 struct load_url_result *result;
377 void *ctx = talloc_parent(dc);
378 struct pxe_parser_info *info;
379 char **pxe_conf_files;
382 /* Expects dhcp event parameters to support network boot */
386 dc->conf_url = user_event_parse_conf_url(dc, dc->event, &complete_url);
391 * Retrieving PXE configs over the network can take some time depending
392 * on factors such as slow network, malformed paths, bad DNS, and
393 * overzealous firewalls. Instead of blocking the discover server while
394 * we wait for these, spawn an asynchronous job that will attempt to
395 * retrieve each possible URL until it successfully finds one, and
396 * parse and process the resulting file in a callback.
398 conf = copy_context(ctx, dc);
403 /* Check if this file has already been downloaded */
404 if (event_get_param(dc->event, "pxeconffile-local"))
405 file_url = pb_url_parse(dc, event_get_param(dc->event,
406 "pxeconffile-local"));
408 file_url = dc->conf_url;
411 pb_log("%s: Failed to parse conf url!\n", __func__);
415 device_handler_status_dev_info(conf->dc->handler,
417 _("Requesting config %s"),
418 pb_url_to_string(conf->dc->conf_url));
420 /* we have a complete URL; use this and we're done. */
421 result = load_url_async(conf->dc, file_url,
422 pxe_conf_parse_cb, conf, NULL, ctx);
424 pb_log("load_url_async fails for %s\n",
429 pxe_conf_files = user_event_parse_conf_filenames(dc, dc->event);
433 pxe_base_url = pb_url_join(dc, dc->conf_url, pxelinux_prefix);
437 info = conf->parser_info;
438 info->pxe_conf_files = pxe_conf_files;
439 info->pxe_base_url = pxe_base_url;
441 device_handler_status_dev_info(conf->dc->handler,
443 _("Probing from base %s"),
444 pb_url_to_string(pxe_base_url));
446 pxe_load_next_filename(conf);
452 talloc_free(pxe_conf_files);
458 static struct parser pxe_parser = {
463 register_parser(pxe_parser);