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);
47 /* We need a slightly modified version of pb_url_join, to allow for the
48 * pxelinux "::filename" syntax for absolute URLs
50 static struct pb_url *pxe_url_join(void *ctx, const struct pb_url *url,
53 struct pb_url *new_url;
58 if (len > 2 && s[0] == ':' && s[1] == ':') {
62 /* ::/path -> /path */
63 tmp = talloc_strdup(ctx, s+2);
66 tmp = talloc_strdup(ctx, s+1);
70 new_url = pb_url_join(ctx, url, tmp);
76 /* strip leading slashes */
77 for (tmp = s; *tmp == '/'; tmp++)
79 new_url = pb_url_join(ctx, url, tmp);
85 static void pxe_append_string(struct discover_boot_option *opt,
88 if (opt->option->boot_args)
89 opt->option->boot_args = talloc_asprintf_append(
90 opt->option->boot_args, " %s", str);
92 opt->option->boot_args = talloc_strdup(opt->option, str);
95 static char *pxe_sysappend_mac(void *ctx, uint8_t *mac)
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]);
105 static void pxe_process_sysappend(struct discover_context *ctx,
106 struct discover_boot_option *opt,
109 struct event *event = ctx->event;
116 uint8_t *mac = find_mac_by_name(ctx,
117 device_handler_get_network(ctx->handler),
119 str = pxe_sysappend_mac(ctx, mac);
121 pxe_append_string(opt, str);
128 pb_log("pxe: unsupported features requested in "
129 "ipappend/sysappend: 0x%04lx", val);
133 static void pxe_process_pair(struct conf_context *ctx,
134 const char *name, char *value)
136 struct pxe_parser_info *parser_info = ctx->parser_info;
137 struct discover_boot_option *opt = parser_info->opt;
140 /* quirk in the syslinux config format: initrd can be separated
142 if (!name && !strncasecmp(value, "initrd=", strlen("initrd="))) {
144 value += strlen("initrd=");
150 if (streq(name, "DEFAULT")) {
151 parser_info->default_name = talloc_strdup(parser_info, value);
155 if (streq(name, "PROXY")) {
156 if (parser_info->proxy)
157 talloc_free(parser_info->proxy);
159 parser_info->proxy = talloc_strdup(parser_info, value);
163 if (streq(name, "LABEL") || streq(name, "PLUGIN")) {
167 opt = discover_boot_option_create(ctx->dc, ctx->dc->device);
169 opt->option->name = talloc_strdup(opt, value);
170 opt->option->id = talloc_asprintf(opt, "%s@%p",
171 ctx->dc->device->device->id, opt);
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);
178 opt->option->type = DISCOVER_PLUGIN_OPTION;
180 parser_info->opt = opt;
184 /* all other parameters need an option */
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 */
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);
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);
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);
211 } else if (streq(name, "APPEND")) {
214 pxe_append_string(opt, value);
216 str = strcasestr(value, "INITRD=");
218 str += strlen("INITRD=");
219 end = strchrnul(str, ' ');
222 url = pxe_url_join(ctx->dc, ctx->dc->conf_url, str);
223 opt->initrd = create_url_resource(opt, url);
225 } else if (streq(name, "SYSAPPEND") || streq(name, "IPAPPEND")) {
229 type = strtoul(value, &end, 10);
230 if (end != value && !(*end))
231 pxe_process_sysappend(ctx->dc, opt, type);
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);
239 static void pxe_load_next_filename(struct conf_context *conf)
241 struct pxe_parser_info *info = conf->parser_info;
244 if (!info->pxe_conf_files)
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]);
253 if (load_url_async(conf, url, pxe_conf_parse_cb, conf,
262 * Callback for asynchronous loads from pxe_parse()
263 * @param result Result of load_url_async()
264 * @param data Pointer to associated conf_context
266 static void pxe_conf_parse_cb(struct load_url_result *result, void *data)
268 struct conf_context *conf = data;
269 struct device_handler *handler;
270 struct pxe_parser_info *info;
279 handler = talloc_parent(conf);
281 if (result->status == LOAD_OK)
282 rc = read_file(conf, result->local, &buf, &len);
283 if (result->status != LOAD_OK || rc) {
284 /* This load failed so try the next available filename */
285 info = conf->parser_info;
286 if (!info->pxe_conf_files) {
287 device_handler_status_dev_err(handler,
289 _("Failed to download %s"),
290 pb_url_to_string(result->url));
296 pxe_load_next_filename(conf);
297 if (info->pxe_conf_files[info->current] == NULL) {
298 /* Nothing left to try */
299 device_handler_status_dev_err(handler,
301 _("PXE autoconfiguration failed"));
308 * Parse the first successfully downloaded file. We only want to parse
309 * the first because otherwise we could parse options from both a
310 * machine-specific config and a 'fallback' default config
313 conf_parse_buf(conf, buf, len);
315 /* We may be called well after the original caller of iterate_parsers(),
316 * commit any new boot options ourselves */
317 device_handler_discover_context_commit(handler, conf->dc);
320 * TRANSLATORS: the format specifier in this string is a URL
321 * eg. tftp://192.168.1.1/pxelinux.cfg
323 device_handler_status_dev_info(handler, conf->dc->device,
324 _("Parsed PXE config from %s"),
325 pb_url_to_string(result->url));
329 if (result && result->cleanup_local)
330 unlink(result->local);
335 * Return a new conf_context and increment the talloc reference count on
336 * the discover_context struct.
337 * @param ctx Parent talloc context
338 * @param orig Original discover_context
339 * @return Pointer to new conf_context
341 static struct conf_context *copy_context(void *ctx, struct discover_context *dc)
343 struct pxe_parser_info *info;
344 struct conf_context *conf;
346 conf = talloc_zero(ctx, struct conf_context);
351 conf->get_pair = conf_get_pair_space;
352 conf->process_pair = pxe_process_pair;
353 conf->finish = pxe_finish;
354 info = talloc_zero(conf, struct pxe_parser_info);
359 conf->parser_info = info;
362 * The discover_context may be freed once pxe_parse() returns, but the
363 * callback will still need it. Take a reference so that that it will
364 * persist until the last callback completes.
366 conf->dc = talloc_reference(conf, dc);
371 static int pxe_parse(struct discover_context *dc)
373 struct pb_url *pxe_base_url, *file_url;
374 struct conf_context *conf = NULL;
375 struct load_url_result *result;
376 void *ctx = talloc_parent(dc);
377 struct pxe_parser_info *info;
378 char **pxe_conf_files;
381 /* Expects dhcp event parameters to support network boot */
385 dc->conf_url = user_event_parse_conf_url(dc, dc->event, &complete_url);
390 * Retrieving PXE configs over the network can take some time depending
391 * on factors such as slow network, malformed paths, bad DNS, and
392 * overzealous firewalls. Instead of blocking the discover server while
393 * we wait for these, spawn an asynchronous job that will attempt to
394 * retrieve each possible URL until it successfully finds one, and
395 * parse and process the resulting file in a callback.
397 conf = copy_context(ctx, dc);
402 /* Check if this file has already been downloaded */
403 if (event_get_param(dc->event, "pxeconffile-local"))
404 file_url = pb_url_parse(dc, event_get_param(dc->event,
405 "pxeconffile-local"));
407 file_url = dc->conf_url;
410 pb_log("%s: Failed to parse conf url!\n", __func__);
414 device_handler_status_dev_info(conf->dc->handler,
416 _("Requesting config %s"),
417 pb_url_to_string(conf->dc->conf_url));
419 /* we have a complete URL; use this and we're done. */
420 result = load_url_async(conf->dc, file_url,
421 pxe_conf_parse_cb, conf, NULL, ctx);
423 pb_log("load_url_async fails for %s\n",
428 pxe_conf_files = user_event_parse_conf_filenames(dc, dc->event);
432 pxe_base_url = pb_url_join(dc, dc->conf_url, pxelinux_prefix);
436 info = conf->parser_info;
437 info->pxe_conf_files = pxe_conf_files;
438 info->pxe_base_url = pxe_base_url;
440 device_handler_status_dev_info(conf->dc->handler,
442 _("Probing from base %s"),
443 pb_url_to_string(pxe_base_url));
445 pxe_load_next_filename(conf);
451 talloc_free(pxe_conf_files);
457 static struct parser pxe_parser = {
462 register_parser(pxe_parser);