]> git.ozlabs.org Git - petitboot/blob - discover/user-event.c
ui/ncurses: Fix discover_client leak
[petitboot] / discover / user-event.c
1 /*
2  *  Copyright (C) 2009 Sony Computer Entertainment Inc.
3  *  Copyright 2009 Sony Corp.
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; version 2 of the License.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #if defined(HAVE_CONFIG_H)
20 #include "config.h"
21 #endif
22
23 #include <assert.h>
24 #include <errno.h>
25 #include <string.h>
26 #include <sys/socket.h>
27 #include <sys/types.h>
28 #include <sys/un.h>
29
30 #include <log/log.h>
31 #include <url/url.h>
32 #include <types/types.h>
33 #include <talloc/talloc.h>
34 #include <waiter/waiter.h>
35
36 #include "device-handler.h"
37 #include "resource.h"
38 #include "event.h"
39 #include "user-event.h"
40
41
42 #define MAC_ADDR_SIZE   6
43 #define IP_ADDR_SIZE    4
44
45 struct user_event {
46         struct device_handler *handler;
47         int socket;
48 };
49
50 static const char *event_action_name(enum event_action action)
51 {
52         switch (action) {
53         case EVENT_ACTION_ADD:
54                 return "add";
55         case EVENT_ACTION_REMOVE:
56                 return "remove";
57         case EVENT_ACTION_DHCP:
58                 return "dhcp";
59         default:
60                 break;
61         }
62
63         return "unknown";
64 }
65
66 static void user_event_print_event(struct event __attribute__((unused)) *event)
67 {
68         int i;
69
70         pb_debug("user_event %s event:\n", event_action_name(event->action));
71         pb_debug("\tdevice: %s\n", event->device);
72
73         for (i = 0; i < event->n_params; i++)
74                 pb_debug("\t%-12s => %s\n",
75                         event->params[i].name, event->params[i].value);
76 }
77
78 static struct resource *user_event_resource(struct discover_boot_option *opt,
79                 struct event *event)
80 {
81         const char *siaddr, *boot_file;
82         struct resource *res;
83         struct pb_url *url;
84         char *url_str;
85
86         siaddr = event_get_param(event, "siaddr");
87         if (!siaddr) {
88                 pb_log("%s: next server option not found\n", __func__);
89                 return NULL;
90         }
91
92         boot_file = event_get_param(event, "bootfile");
93         if (!boot_file) {
94                 pb_log("%s: bootfile not found\n", __func__);
95                 return NULL;
96         }
97
98         url_str = talloc_asprintf(opt, "%s%s/%s", "tftp://", siaddr, boot_file);
99         url = pb_url_parse(opt, url_str);
100         talloc_free(url_str);
101
102         if (!url)
103                 return NULL;
104
105         res = create_url_resource(opt, url);
106         if (!res) {
107                 talloc_free(url);
108                 return NULL;
109         }
110
111         return res;
112 }
113
114 static int parse_user_event(struct discover_context *ctx, struct event *event)
115 {
116         struct discover_boot_option *d_opt;
117         char *server_ip, *root_dir, *p;
118         struct boot_option *opt;
119         struct device *dev;
120         const char *val;
121
122         dev = ctx->device->device;
123
124         d_opt = discover_boot_option_create(ctx, ctx->device);
125         if (!d_opt)
126                 goto fail;
127
128         opt = d_opt->option;
129
130         val = event_get_param(event, "name");
131
132         if (!val) {
133                 pb_log("%s: no name found\n", __func__);
134                 goto fail_opt;
135         }
136
137         opt->id = talloc_asprintf(opt, "%s#%s", dev->id, val);
138         opt->name = talloc_strdup(opt, val);
139
140         d_opt->boot_image = user_event_resource(d_opt, event);
141         if (!d_opt->boot_image) {
142                 pb_log("%s: no boot image found for %s!\n", __func__,
143                                 opt->name);
144                 goto fail_opt;
145         }
146
147         val = event_get_param(event, "rootpath");
148         if (val) {
149                 server_ip = talloc_strdup(opt, val);
150                 p = strchr(server_ip, ':');
151                 if (p) {
152                         root_dir = talloc_strdup(opt, p + 1);
153                         *p = '\0';
154                         opt->boot_args = talloc_asprintf(opt, "%s%s:%s",
155                                         "root=/dev/nfs ip=any nfsroot=",
156                                         server_ip, root_dir);
157
158                         talloc_free(root_dir);
159                 } else {
160                         opt->boot_args = talloc_asprintf(opt, "%s",
161                                         "root=/dev/nfs ip=any nfsroot=");
162                 }
163
164                 talloc_free(server_ip);
165         }
166
167         opt->description = talloc_asprintf(opt, "%s %s", opt->boot_image_file,
168                 opt->boot_args ? : "");
169
170         if (event_get_param(event, "default"))
171                 opt->is_default = true;
172
173         discover_context_add_boot_option(ctx, d_opt);
174
175         return 0;
176
177 fail_opt:
178         talloc_free(d_opt);
179 fail:
180         return -1;
181 }
182
183 static const char *parse_host_addr(struct event *event)
184 {
185         const char *val;
186
187         val = event_get_param(event, "tftp");
188         if (val)
189                 return val;
190
191         val = event_get_param(event, "siaddr");
192         if (val)
193                 return val;
194
195         val = event_get_param(event, "serverid");
196         if (val)
197                 return val;
198
199         return NULL;
200 }
201
202 static char *parse_mac_addr(struct discover_context *ctx, const char *mac)
203 {
204         unsigned int mac_addr_arr[MAC_ADDR_SIZE];
205         char *mac_addr;
206
207         sscanf(mac, "%X:%X:%X:%X:%X:%X", mac_addr_arr, mac_addr_arr + 1,
208                         mac_addr_arr + 2, mac_addr_arr + 3, mac_addr_arr + 4,
209                         mac_addr_arr + 5);
210
211         mac_addr = talloc_asprintf(ctx, "01-%02x-%02x-%02x-%02x-%02x-%02x",
212                         mac_addr_arr[0], mac_addr_arr[1], mac_addr_arr[2],
213                         mac_addr_arr[3], mac_addr_arr[4], mac_addr_arr[5]);
214
215         return mac_addr;
216 }
217
218 static char *parse_ip_addr(struct discover_context *ctx, const char *ip)
219 {
220         unsigned int ip_addr_arr[IP_ADDR_SIZE];
221         char *ip_hex;
222
223         sscanf(ip, "%u.%u.%u.%u", ip_addr_arr, ip_addr_arr + 1,
224                         ip_addr_arr + 2, ip_addr_arr + 3);
225
226         ip_hex = talloc_asprintf(ctx, "%02X%02X%02X%02X", ip_addr_arr[0],
227                         ip_addr_arr[1], ip_addr_arr[2], ip_addr_arr[3]);
228
229         return ip_hex;
230 }
231
232 struct pb_url *user_event_parse_conf_url(struct discover_context *ctx,
233                 struct event *event, bool *is_complete)
234 {
235         const char *conffile, *pathprefix, *host, *bootfile;
236         char *p, *basedir, *url_str;
237         struct pb_url *url;
238
239         conffile = event_get_param(event, "pxeconffile");
240         pathprefix = event_get_param(event, "pxepathprefix");
241         bootfile = event_get_param(event, "bootfile");
242
243         /* If we're given a conf file, we're able to generate a complete URL to
244          * the configuration file, and the parser doesn't need to do any
245          * further autodiscovery */
246         *is_complete = !!conffile;
247
248         /* if conffile is a URL, that's all we need */
249         if (conffile && is_url(conffile)) {
250                 url = pb_url_parse(ctx, conffile);
251                 return url;
252         }
253
254         /* If we can create a URL from pathprefix (optionally with
255          * conffile appended to create a complete URL), use that */
256         if (pathprefix && is_url(pathprefix)) {
257                 if (conffile) {
258                         url_str = talloc_asprintf(ctx, "%s%s",
259                                         pathprefix, conffile);
260                         url = pb_url_parse(ctx, url_str);
261                         talloc_free(url_str);
262                 } else {
263                         url = pb_url_parse(ctx, pathprefix);
264                 }
265
266                 return url;
267         }
268
269         host = parse_host_addr(event);
270         if (!host) {
271                 pb_log("%s: host address not found\n", __func__);
272                 return NULL;
273         }
274
275         url_str = talloc_asprintf(ctx, "tftp://%s/", host);
276
277         /* if we have a pathprefix, use that directly.. */
278         if (pathprefix) {
279                 /* strip leading slashes */
280                 while (pathprefix[0] == '/')
281                         pathprefix++;
282                 url_str = talloc_asprintf_append(url_str, "%s", pathprefix);
283
284         /* ... otherwise, add a path based on the bootfile name, but only
285          * if conffile isn't an absolute path itself */
286         } else if (bootfile && !(conffile && conffile[0] == '/')) {
287
288                 basedir = talloc_strdup(ctx, bootfile);
289
290                 /* strip filename from the bootfile path, leaving only a
291                  * directory */
292                 p = strrchr(basedir, '/');
293                 if (!p)
294                         p = basedir;
295                 *p = '\0';
296
297                 if (strlen(basedir))
298                         url_str = talloc_asprintf_append(url_str, "%s/",
299                                         basedir);
300
301                 talloc_free(basedir);
302         }
303
304         /* finally, append conffile */
305         if (conffile)
306                 url_str = talloc_asprintf_append(url_str, "%s", conffile);
307
308         url = pb_url_parse(ctx, url_str);
309
310         talloc_free(url_str);
311
312         return url;
313 }
314
315 char **user_event_parse_conf_filenames(
316                 struct discover_context *ctx, struct event *event)
317 {
318         char *mac_addr, *ip_hex;
319         const char *mac, *ip;
320         char **filenames;
321         int index, len;
322
323         mac = event_get_param(event, "mac");
324         if (mac)
325                 mac_addr = parse_mac_addr(ctx, mac);
326         else
327                 mac_addr = NULL;
328
329         ip = event_get_param(event, "ip");
330         if (ip) {
331                 ip_hex = parse_ip_addr(ctx, ip);
332                 len = strlen(ip_hex);
333         } else {
334                 ip_hex = NULL;
335                 len = 0;
336         }
337
338         if (!mac_addr && !ip_hex) {
339                 pb_log("%s: neither mac nor ip parameter found\n", __func__);
340                 return NULL;
341         }
342
343         /* Filenames as fallback IP's + mac + default */
344         filenames = talloc_array(ctx, char *, len + 3);
345
346         index = 0;
347         if (mac_addr)
348                 filenames[index++] = talloc_strdup(filenames, mac_addr);
349
350         while (len) {
351                 filenames[index++] = talloc_strdup(filenames, ip_hex);
352                 ip_hex[--len] = '\0';
353         }
354
355         filenames[index++] = talloc_strdup(filenames, "default");
356         filenames[index++] = NULL;
357
358         if (mac_addr)
359                 talloc_free(mac_addr);
360
361         if (ip_hex)
362                 talloc_free(ip_hex);
363
364         return filenames;
365 }
366
367 static int user_event_dhcp(struct user_event *uev, struct event *event)
368 {
369         struct device_handler *handler = uev->handler;
370         struct discover_device *dev;
371
372         dev = discover_device_create(handler, event->device);
373
374         device_handler_dhcp(handler, dev, event);
375
376         return 0;
377 }
378
379 static int user_event_conf(struct user_event *uev, struct event *event)
380 {
381         struct device_handler *handler = uev->handler;
382         struct discover_device *dev;
383         struct pb_url *url;
384         const char *val;
385
386         val = event_get_param(event, "url");
387         if (!val)
388                 return 0;
389
390         url = pb_url_parse(event, val);
391         if (!url)
392                 return 0;
393
394         dev = discover_device_create(handler, event->device);
395
396         device_handler_conf(handler, dev, url);
397
398         return 0;
399 }
400
401 static int user_event_add(struct user_event *uev, struct event *event)
402 {
403         struct device_handler *handler = uev->handler;
404         struct discover_context *ctx;
405         struct discover_device *dev;
406
407         dev = discover_device_create(handler, event->device);
408         ctx = device_handler_discover_context_create(handler, dev);
409
410         parse_user_event(ctx, event);
411
412         device_handler_discover_context_commit(handler, ctx);
413
414         talloc_free(ctx);
415
416         return 0;
417 }
418
419 static int user_event_remove(struct user_event *uev, struct event *event)
420 {
421         struct device_handler *handler = uev->handler;
422         struct discover_device *dev;
423
424         dev = device_lookup_by_id(handler, event->device);
425         if (!dev)
426                 return 0;
427
428         device_handler_remove(handler, dev);
429
430         return 0;
431 }
432
433 static void user_event_handle_message(struct user_event *uev, char *buf,
434         int len)
435 {
436         int result;
437         struct event *event;
438
439         event = talloc(uev, struct event);
440         event->type = EVENT_TYPE_USER;
441
442         result = event_parse_ad_message(event, buf, len);
443
444         if (result)
445                 return;
446
447         user_event_print_event(event);
448
449         switch (event->action) {
450         case EVENT_ACTION_ADD:
451                 result = user_event_add(uev, event);
452                 break;
453         case EVENT_ACTION_REMOVE:
454                 result = user_event_remove(uev, event);
455                 break;
456         case EVENT_ACTION_CONF:
457                 result = user_event_conf(uev, event);
458                 break;
459         case EVENT_ACTION_DHCP:
460                 result = user_event_dhcp(uev, event);
461                 break;
462         default:
463                 break;
464         }
465
466         talloc_free(event);
467
468         return;
469 }
470
471 static int user_event_process(void *arg)
472 {
473         struct user_event *uev = arg;
474         char buf[PBOOT_USER_EVENT_SIZE];
475         int len;
476
477         len = recvfrom(uev->socket, buf, sizeof(buf), 0, NULL, NULL);
478
479         if (len < 0) {
480                 pb_log("%s: socket read failed: %s", __func__, strerror(errno));
481                 return 0;
482         }
483
484         if (len == 0) {
485                 pb_log("%s: empty", __func__);
486                 return 0;
487         }
488
489         pb_debug("%s: %u bytes\n", __func__, len);
490
491         user_event_handle_message(uev, buf, len);
492
493         return 0;
494 }
495
496 static int user_event_destructor(void *arg)
497 {
498         struct user_event *uev = arg;
499
500         pb_debug("%s\n", __func__);
501
502         if (uev->socket >= 0)
503                 close(uev->socket);
504
505         return 0;
506 }
507
508 struct user_event *user_event_init(struct device_handler *handler,
509                 struct waitset *waitset)
510 {
511         struct sockaddr_un addr;
512         struct user_event *uev;
513
514         unlink(PBOOT_USER_EVENT_SOCKET);
515
516         uev = talloc(handler, struct user_event);
517
518         uev->handler = handler;
519
520         uev->socket = socket(AF_UNIX, SOCK_DGRAM, 0);
521         if (uev->socket < 0) {
522                 pb_log("%s: Error creating event handler socket: %s\n",
523                         __func__, strerror(errno));
524                 goto out_err;
525         }
526
527         talloc_set_destructor(uev, user_event_destructor);
528
529         memset(&addr, 0, sizeof(addr));
530         addr.sun_family = AF_UNIX;
531         strcpy(addr.sun_path, PBOOT_USER_EVENT_SOCKET);
532
533         if (bind(uev->socket, (struct sockaddr *)&addr, sizeof(addr))) {
534                 pb_log("Error binding event handler socket: %s\n",
535                         strerror(errno));
536         }
537
538         waiter_register_io(waitset, uev->socket, WAIT_IN,
539                         user_event_process, uev);
540
541         pb_debug("%s: waiting on %s\n", __func__, PBOOT_USER_EVENT_SOCKET);
542
543         return uev;
544
545 out_err:
546         talloc_free(uev);
547         return NULL;
548 }