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