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