2 #if defined(HAVE_CONFIG_H)
8 #include <talloc/talloc.h>
11 #include <file/file.h>
12 #include <i18n/i18n.h>
15 #include "parser-conf.h"
16 #include "parser-utils.h"
20 #include "user-event.h"
23 static const char *pxelinux_prefix = "pxelinux.cfg/";
25 static void pxe_conf_parse_cb(struct load_url_result *result, void *data);
27 struct pxe_parser_info {
28 struct discover_boot_option *opt;
29 const char *default_name;
30 char **pxe_conf_files;
31 struct pb_url *pxe_base_url;
35 static void pxe_finish(struct conf_context *conf)
37 struct pxe_parser_info *info = conf->parser_info;
39 discover_context_add_boot_option(conf->dc, info->opt);
42 /* We need a slightly modified version of pb_url_join, to allow for the
43 * pxelinux "::filename" syntax for absolute URLs
45 static struct pb_url *pxe_url_join(void *ctx, const struct pb_url *url,
48 struct pb_url *new_url;
53 if (len > 2 && s[0] == ':' && s[1] == ':') {
57 /* ::/path -> /path */
58 tmp = talloc_strdup(ctx, s+2);
61 tmp = talloc_strdup(ctx, s+1);
65 new_url = pb_url_join(ctx, url, tmp);
71 /* strip leading slashes */
72 for (tmp = s; *tmp == '/'; tmp++)
74 new_url = pb_url_join(ctx, url, tmp);
80 static void pxe_append_string(struct discover_boot_option *opt,
83 if (opt->option->boot_args)
84 opt->option->boot_args = talloc_asprintf_append(
85 opt->option->boot_args, " %s", str);
87 opt->option->boot_args = talloc_strdup(opt->option, str);
90 static char *pxe_sysappend_mac(void *ctx, uint8_t *mac)
95 return talloc_asprintf(ctx,
96 "BOOTIF=01-%02x-%02x-%02x-%02x-%02x-%02x",
97 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
100 static void pxe_process_sysappend(struct discover_context *ctx,
101 struct discover_boot_option *opt,
104 struct event *event = ctx->event;
111 uint8_t *mac = find_mac_by_name(ctx, ctx->network,
113 str = pxe_sysappend_mac(ctx, mac);
115 pxe_append_string(opt, str);
122 pb_log("pxe: unsupported features requested in "
123 "ipappend/sysappend: 0x%04lx", val);
127 static void pxe_process_pair(struct conf_context *ctx,
128 const char *name, char *value)
130 struct pxe_parser_info *parser_info = ctx->parser_info;
131 struct discover_boot_option *opt = parser_info->opt;
134 /* quirk in the syslinux config format: initrd can be separated
136 if (!name && !strncasecmp(value, "initrd=", strlen("initrd="))) {
138 value += strlen("initrd=");
144 if (streq(name, "DEFAULT")) {
145 parser_info->default_name = talloc_strdup(parser_info, value);
149 if (streq(name, "LABEL")) {
153 opt = discover_boot_option_create(ctx->dc, ctx->dc->device);
155 opt->option->name = talloc_strdup(opt, value);
156 opt->option->id = talloc_asprintf(opt, "%s@%p",
157 ctx->dc->device->device->id, opt);
159 opt->option->is_default = parser_info->default_name &&
160 streq(parser_info->default_name, value);
162 parser_info->opt = opt;
166 /* all other parameters need an option */
170 if (streq(name, "KERNEL")) {
171 url = pxe_url_join(ctx->dc, ctx->dc->conf_url, value);
172 opt->boot_image = create_url_resource(opt, url);
174 char* args_sigfile_default = talloc_asprintf(opt,
175 "%s.cmdline.sig", value);
176 url = pxe_url_join(ctx->dc, ctx->dc->conf_url,
177 args_sigfile_default);
178 opt->args_sig_file = create_url_resource(opt, url);
179 talloc_free(args_sigfile_default);
181 } else if (streq(name, "INITRD")) {
182 url = pxe_url_join(ctx->dc, ctx->dc->conf_url, value);
183 opt->initrd = create_url_resource(opt, url);
185 } else if (streq(name, "APPEND")) {
188 pxe_append_string(opt, value);
190 str = strcasestr(value, "INITRD=");
192 str += strlen("INITRD=");
193 end = strchrnul(str, ' ');
196 url = pxe_url_join(ctx->dc, ctx->dc->conf_url, str);
197 opt->initrd = create_url_resource(opt, url);
199 } else if (streq(name, "SYSAPPEND") || streq(name, "IPAPPEND")) {
203 type = strtoul(value, &end, 10);
204 if (end != value && !(*end))
205 pxe_process_sysappend(ctx->dc, opt, type);
207 } else if (streq(name, "DTB") || streq(name, "FDT")) {
208 url = pxe_url_join(ctx->dc, ctx->dc->conf_url, value);
209 opt->dtb = create_url_resource(opt, url);
214 static void pxe_load_next_filename(struct conf_context *conf)
216 struct pxe_parser_info *info = conf->parser_info;
219 if (!info->pxe_conf_files)
222 for (; info->pxe_conf_files[info->current]; info->current++) {
223 url = pb_url_join(conf->dc, info->pxe_base_url,
224 info->pxe_conf_files[info->current]);
228 if (load_url_async(conf, url, pxe_conf_parse_cb, conf))
236 * Callback for asynchronous loads from pxe_parse()
237 * @param result Result of load_url_async()
238 * @param data Pointer to associated conf_context
240 static void pxe_conf_parse_cb(struct load_url_result *result, void *data)
242 struct conf_context *conf = data;
243 struct device_handler *handler;
244 struct pxe_parser_info *info;
251 if (result && result->status == LOAD_OK)
252 rc = read_file(conf, result->local, &buf, &len);
253 if (!result || result->status != LOAD_OK || rc) {
254 /* This load failed so try the next available filename */
255 info = conf->parser_info;
256 if (!info->pxe_conf_files)
260 pxe_load_next_filename(conf);
261 if (info->pxe_conf_files[info->current] == NULL) {
262 /* Nothing left to try */
269 * Parse the first successfully downloaded file. We only want to parse
270 * the first because otherwise we could parse options from both a
271 * machine-specific config and a 'fallback' default config
274 conf_parse_buf(conf, buf, len);
276 /* We may be called well after the original caller of iterate_parsers(),
277 * commit any new boot options ourselves */
278 handler = talloc_parent(conf);
279 device_handler_discover_context_commit(handler, conf->dc);
282 * TRANSLATORS: the format specifier in this string in an IP address,
285 device_handler_status_dev_info(handler, conf->dc->device,
286 _("Parsed PXE config from %s"),
287 conf->dc->conf_url->host);
291 if (result->cleanup_local)
292 unlink(result->local);
297 * Return a new conf_context and increment the talloc reference count on
298 * the discover_context struct.
299 * @param ctx Parent talloc context
300 * @param orig Original discover_context
301 * @return Pointer to new conf_context
303 static struct conf_context *copy_context(void *ctx, struct discover_context *dc)
305 struct pxe_parser_info *info;
306 struct conf_context *conf;
308 conf = talloc_zero(ctx, struct conf_context);
313 conf->get_pair = conf_get_pair_space;
314 conf->process_pair = pxe_process_pair;
315 conf->finish = pxe_finish;
316 info = talloc_zero(conf, struct pxe_parser_info);
321 conf->parser_info = info;
324 * The discover_context may be freed once pxe_parse() returns, but the
325 * callback will still need it. Take a reference so that that it will
326 * persist until the last callback completes.
328 conf->dc = talloc_reference(conf, dc);
333 static int pxe_parse(struct discover_context *dc)
335 struct pb_url *pxe_base_url;
336 struct conf_context *conf = NULL;
337 struct load_url_result *result;
338 void *ctx = talloc_parent(dc);
339 struct pxe_parser_info *info;
340 char **pxe_conf_files;
343 /* Expects dhcp event parameters to support network boot */
347 dc->conf_url = user_event_parse_conf_url(dc, dc->event, &complete_url);
352 * Retrieving PXE configs over the network can take some time depending
353 * on factors such as slow network, malformed paths, bad DNS, and
354 * overzealous firewalls. Instead of blocking the discover server while
355 * we wait for these, spawn an asynchronous job that will attempt to
356 * retrieve each possible URL until it successfully finds one, and
357 * parse and process the resulting file in a callback.
359 conf = copy_context(ctx, dc);
364 /* we have a complete URL; use this and we're done. */
365 result = load_url_async(conf->dc, conf->dc->conf_url,
366 pxe_conf_parse_cb, conf);
368 pb_log("load_url_async fails for %s\n",
373 pxe_conf_files = user_event_parse_conf_filenames(dc, dc->event);
377 pxe_base_url = pb_url_join(dc, dc->conf_url, pxelinux_prefix);
381 info = conf->parser_info;
382 info->pxe_conf_files = pxe_conf_files;
383 info->pxe_base_url = pxe_base_url;
385 pxe_load_next_filename(conf);
391 talloc_free(pxe_conf_files);
397 static struct parser pxe_parser = {
402 register_parser(pxe_parser);