]> git.ozlabs.org Git - petitboot/blob - discover/pxe-parser.c
discover: Add two missing talloc_free()s
[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
12 #include "parser.h"
13 #include "parser-conf.h"
14 #include "parser-utils.h"
15 #include "resource.h"
16 #include "paths.h"
17 #include "event.h"
18 #include "user-event.h"
19 #include "network.h"
20
21 static const char *pxelinux_prefix = "pxelinux.cfg/";
22
23 struct pxe_parser_info {
24         struct discover_boot_option *opt;
25         const char *default_name;
26 };
27
28 static void pxe_finish(struct conf_context *conf)
29 {
30         struct pxe_parser_info *info = conf->parser_info;
31         if (info->opt)
32                 discover_context_add_boot_option(conf->dc, info->opt);
33 }
34
35 /* We need a slightly modified version of pb_url_join, to allow for the
36  * pxelinux "::filename" syntax for absolute URLs
37  */
38 static struct pb_url *pxe_url_join(void *ctx, const struct pb_url *url,
39                 const char *s)
40 {
41         struct pb_url *new_url;
42         int len;
43
44         len = strlen(s);
45
46         if (len > 2 && s[0] == ':' && s[1] == ':') {
47                 char *tmp;
48
49                 if (s[2] == '/') {
50                         /* ::/path -> /path */
51                         tmp = talloc_strdup(ctx, s+2);
52                 } else {
53                         /* ::path -> /path */
54                         tmp = talloc_strdup(ctx, s+1);
55                         tmp[0] = '/';
56                 }
57
58                 new_url = pb_url_join(ctx, url, tmp);
59
60                 talloc_free(tmp);
61
62         } else {
63                 const char *tmp;
64                 /* strip leading slashes */
65                 for (tmp = s; *tmp == '/'; tmp++)
66                         ;
67                 new_url = pb_url_join(ctx, url, tmp);
68         }
69
70         return new_url;
71 }
72
73 static void pxe_append_string(struct discover_boot_option *opt,
74                 const char *str)
75 {
76         if (opt->option->boot_args)
77                 opt->option->boot_args = talloc_asprintf_append(
78                                 opt->option->boot_args, " %s", str);
79         else
80                 opt->option->boot_args = talloc_strdup(opt->option, str);
81 }
82
83 static char *pxe_sysappend_mac(void *ctx, uint8_t *mac)
84 {
85         if (!mac)
86                 return NULL;
87
88         return talloc_asprintf(ctx,
89                 "BOOTIF=01-%02x-%02x-%02x-%02x-%02x-%02x",
90                 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
91 }
92
93 static void pxe_process_sysappend(struct discover_context *ctx,
94                 struct discover_boot_option *opt,
95                 unsigned long val)
96 {
97         struct event *event = ctx->event;
98         char *str = NULL;
99
100         if (!event)
101                 return;
102
103         if (val & 0x2) {
104                 uint8_t *mac = find_mac_by_name(ctx, ctx->network,
105                                         event->device);
106                 str = pxe_sysappend_mac(ctx, mac);
107                 if (str) {
108                         pxe_append_string(opt, str);
109                         talloc_free(str);
110                 }
111                 val &= ~0x2;
112         }
113
114         if (val)
115                 pb_log("pxe: unsupported features requested in "
116                                 "ipappend/sysappend: 0x%04lx", val);
117
118 }
119
120 static void pxe_process_pair(struct conf_context *ctx,
121                 const char *name, char *value)
122 {
123         struct pxe_parser_info *parser_info = ctx->parser_info;
124         struct discover_boot_option *opt = parser_info->opt;
125         struct pb_url *url;
126
127         /* quirk in the syslinux config format: initrd can be separated
128          * by an '=' */
129         if (!name && !strncasecmp(value, "initrd=", strlen("initrd="))) {
130                 name = "initrd";
131                 value += strlen("initrd=");
132         }
133
134         if (!name)
135                 return;
136
137         if (streq(name, "DEFAULT")) {
138                 parser_info->default_name = talloc_strdup(parser_info, value);
139                 return;
140         }
141
142         if (streq(name, "LABEL")) {
143                 if (opt)
144                         pxe_finish(ctx);
145
146                 opt = discover_boot_option_create(ctx->dc, ctx->dc->device);
147
148                 opt->option->name = talloc_strdup(opt, value);
149                 opt->option->id = talloc_asprintf(opt, "%s@%p",
150                                 ctx->dc->device->device->id, opt);
151
152                 opt->option->is_default = parser_info->default_name &&
153                                         streq(parser_info->default_name, value);
154
155                 parser_info->opt = opt;
156                 return;
157         }
158
159         /* all other parameters need an option */
160         if (!opt)
161                 return;
162
163         if (streq(name, "KERNEL")) {
164                 url = pxe_url_join(ctx->dc, ctx->dc->conf_url, value);
165                 opt->boot_image = create_url_resource(opt, url);
166
167         } else if (streq(name, "INITRD")) {
168                 url = pxe_url_join(ctx->dc, ctx->dc->conf_url, value);
169                 opt->initrd = create_url_resource(opt, url);
170
171         } else if (streq(name, "APPEND")) {
172                 char *str, *end;
173
174                 pxe_append_string(opt, value);
175
176                 str = strcasestr(value, "INITRD=");
177                 if (str) {
178                         str += strlen("INITRD=");
179                         end = strchrnul(str, ' ');
180                         *end = '\0';
181
182                         url = pxe_url_join(ctx->dc, ctx->dc->conf_url, str);
183                         opt->initrd = create_url_resource(opt, url);
184                 }
185         } else if (streq(name, "SYSAPPEND") || streq(name, "IPAPPEND")) {
186                 unsigned long type;
187                 char *end;
188
189                 type = strtoul(value, &end, 10);
190                 if (end != value && !(*end))
191                         pxe_process_sysappend(ctx->dc, opt, type);
192         }
193
194 }
195
196 static int pxe_parse(struct discover_context *dc)
197 {
198         struct pb_url *pxe_base_url, *url;
199         struct pxe_parser_info *parser_info;
200         char **pxe_conf_files, **filename;
201         struct conf_context *conf;
202         bool complete_url;
203         int len, rc;
204         char *buf;
205
206         /* Expects dhcp event parameters to support network boot */
207         if (!dc->event)
208                 return -1;
209
210         conf = talloc_zero(dc, struct conf_context);
211
212         if (!conf)
213                 goto out;
214
215         conf->dc = dc;
216         conf->get_pair = conf_get_pair_space;
217         conf->process_pair = pxe_process_pair;
218         conf->finish = pxe_finish;
219
220         parser_info = talloc_zero(conf, struct pxe_parser_info);
221         conf->parser_info = parser_info;
222
223         dc->conf_url = user_event_parse_conf_url(dc, dc->event, &complete_url);
224         if (!dc->conf_url)
225                 goto out_conf;
226
227         if (complete_url) {
228                 /* we have a complete URL; use this and we're done. */
229                 rc = parser_request_url(dc, dc->conf_url, &buf, &len);
230                 if (rc)
231                         goto out_conf;
232         } else {
233                 pxe_conf_files = user_event_parse_conf_filenames(dc, dc->event);
234                 if (!pxe_conf_files)
235                         goto out_conf;
236
237                 rc = -1;
238
239                 pxe_base_url = pb_url_join(dc, dc->conf_url, pxelinux_prefix);
240                 if (!pxe_base_url)
241                         goto out_pxe_conf;
242
243                 for (filename = pxe_conf_files; *filename; filename++) {
244                         url = pb_url_join(dc, pxe_base_url, *filename);
245                         if (!url)
246                                 continue;
247
248                         rc = parser_request_url(dc, url, &buf, &len);
249                         if (!rc) /* found one, just break */
250                                 break;
251
252                         talloc_free(url);
253                 }
254
255                 talloc_free(pxe_base_url);
256
257                 /* No configuration file found on the boot server */
258                 if (rc)
259                         goto out_pxe_conf;
260
261                 talloc_free(pxe_conf_files);
262         }
263
264         /* Call the config file parser with the data read from the file */
265         conf_parse_buf(conf, buf, len);
266
267         talloc_free(buf);
268         talloc_free(conf);
269
270         return 0;
271
272 out_pxe_conf:
273         talloc_free(pxe_conf_files);
274 out_conf:
275         talloc_free(conf);
276 out:
277         return -1;
278 }
279
280 static struct parser pxe_parser = {
281         .name                   = "pxe",
282         .parse                  = pxe_parse,
283 };
284
285 register_parser(pxe_parser);