lib/pb-config: Add config_copy
[petitboot] / ui / ncurses / nc-config.c
1 /*
2  *  Copyright (C) 2013 IBM Corporation
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; version 2 of the License.
7  *
8  *  This program is distributed in the hope that it will be useful,
9  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  *  GNU General Public License for more details.
12  *
13  *  You should have received a copy of the GNU General Public License
14  *  along with this program; if not, write to the Free Software
15  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16  */
17
18 #if defined(HAVE_CONFIG_H)
19 #include "config.h"
20 #endif
21
22 #include <errno.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include <pb-config/pb-config.h>
27 #include <talloc/talloc.h>
28 #include <types/types.h>
29 #include <log/log.h>
30
31 #include "nc-cui.h"
32 #include "nc-config.h"
33 #include "nc-widgets.h"
34
35 #define N_FIELDS        23
36
37 enum net_conf_type {
38         NET_CONF_TYPE_DHCP_ALL,
39         NET_CONF_TYPE_DHCP_ONE,
40         NET_CONF_TYPE_STATIC,
41 };
42
43 struct config_screen {
44         struct nc_scr           scr;
45         struct cui              *cui;
46         struct nc_widgetset     *widgetset;
47         WINDOW                  *pad;
48
49         bool                    exit;
50         void                    (*on_exit)(struct cui *);
51
52         int                     scroll_y;
53
54         int                     label_x;
55         int                     field_x;
56         int                     network_config_y;
57
58         enum net_conf_type      net_conf_type;
59
60         struct {
61                 struct nc_widget_checkbox       *autoboot_f;
62                 struct nc_widget_label          *autoboot_l;
63                 struct nc_widget_textbox        *timeout_f;
64                 struct nc_widget_label          *timeout_l;
65                 struct nc_widget_label          *timeout_help_l;
66
67                 struct nc_widget_label          *network_l;
68                 struct nc_widget_select         *network_f;
69
70                 struct nc_widget_label          *iface_l;
71                 struct nc_widget_select         *iface_f;
72                 struct nc_widget_label          *ip_addr_l;
73                 struct nc_widget_textbox        *ip_addr_f;
74                 struct nc_widget_label          *ip_mask_l;
75                 struct nc_widget_textbox        *ip_mask_f;
76                 struct nc_widget_label          *ip_addr_mask_help_l;
77                 struct nc_widget_label          *gateway_l;
78                 struct nc_widget_textbox        *gateway_f;
79                 struct nc_widget_label          *gateway_help_l;
80                 struct nc_widget_label          *dns_l;
81                 struct nc_widget_textbox        *dns_f;
82                 struct nc_widget_label          *dns_dhcp_help_l;
83                 struct nc_widget_label          *dns_help_l;
84
85                 struct nc_widget_button         *ok_b;
86                 struct nc_widget_button         *cancel_b;
87         } widgets;
88 };
89
90 static struct config_screen *config_screen_from_scr(struct nc_scr *scr)
91 {
92         struct config_screen *config_screen;
93
94         assert(scr->sig == pb_config_screen_sig);
95         config_screen = (struct config_screen *)
96                 ((char *)scr - (size_t)&((struct config_screen *)0)->scr);
97         assert(config_screen->scr.sig == pb_config_screen_sig);
98         return config_screen;
99 }
100
101 static void pad_refresh(struct config_screen *screen)
102 {
103         int y, x, rows, cols;
104
105         getmaxyx(screen->scr.sub_ncw, rows, cols);
106         getbegyx(screen->scr.sub_ncw, y, x);
107
108         prefresh(screen->pad, screen->scroll_y, 0, y, x, rows, cols);
109 }
110
111 static void config_screen_process_key(struct nc_scr *scr, int key)
112 {
113         struct config_screen *screen = config_screen_from_scr(scr);
114         bool handled;
115
116         handled = widgetset_process_key(screen->widgetset, key);
117         if (screen->exit)
118                 screen->on_exit(screen->cui);
119         else if (handled)
120                 pad_refresh(screen);
121 }
122
123 static void config_screen_resize(struct nc_scr *scr)
124 {
125         struct config_screen *screen = config_screen_from_scr(scr);
126         (void)screen;
127 }
128
129 static int config_screen_post(struct nc_scr *scr)
130 {
131         struct config_screen *screen = config_screen_from_scr(scr);
132         widgetset_post(screen->widgetset);
133         nc_scr_frame_draw(scr);
134         pad_refresh(screen);
135         return 0;
136 }
137
138 static int config_screen_unpost(struct nc_scr *scr)
139 {
140         struct config_screen *screen = config_screen_from_scr(scr);
141         widgetset_unpost(screen->widgetset);
142         return 0;
143 }
144
145 struct nc_scr *config_screen_scr(struct config_screen *screen)
146 {
147         return &screen->scr;
148 }
149
150 static int screen_process_form(struct config_screen *screen)
151 {
152         const struct system_info *sysinfo = screen->cui->sysinfo;
153         enum net_conf_type net_conf_type;
154         struct interface_config *iface;
155         struct config *config;
156         char *str, *end;
157         int rc;
158
159         config = config_copy(screen, screen->cui->config);
160
161         config->autoboot_enabled =
162                 widget_checkbox_get_value(screen->widgets.autoboot_f);
163
164
165         str = widget_textbox_get_value(screen->widgets.timeout_f);
166         if (str) {
167                 unsigned long x;
168                 errno = 0;
169                 x = strtoul(str, &end, 10);
170                 if (!errno && end != str)
171                         config->autoboot_timeout_sec = x;
172         }
173
174         net_conf_type = widget_select_get_value(screen->widgets.network_f);
175
176         /* if we don't have any network interfaces, prevent per-interface
177          * configuration */
178         if (sysinfo->n_interfaces == 0)
179                 net_conf_type = NET_CONF_TYPE_DHCP_ALL;
180
181         if (net_conf_type == NET_CONF_TYPE_DHCP_ALL) {
182                 config->network.n_interfaces = 0;
183
184         } else {
185                 int idx;
186
187                 iface = talloc_zero(config, struct interface_config);
188                 config->network.n_interfaces = 1;
189                 config->network.interfaces = talloc_array(config,
190                                 struct interface_config *, 1);
191                 config->network.interfaces[0] = iface;
192
193                 /* copy hwaddr (from the sysinfo interface data) to
194                  * the configuration */
195                 idx = widget_select_get_value(screen->widgets.iface_f);
196                 memcpy(iface->hwaddr, sysinfo->interfaces[idx]->hwaddr,
197                                 sizeof(iface->hwaddr));
198         }
199
200         if (net_conf_type == NET_CONF_TYPE_DHCP_ONE) {
201                 iface->method = CONFIG_METHOD_DHCP;
202         }
203
204         if (net_conf_type == NET_CONF_TYPE_STATIC) {
205                 char *ip, *mask, *gateway;
206
207                 ip = widget_textbox_get_value(screen->widgets.ip_addr_f);
208                 mask = widget_textbox_get_value(screen->widgets.ip_mask_f);
209                 gateway = widget_textbox_get_value(screen->widgets.gateway_f);
210
211                 if (!ip || !*ip || !mask || !*mask) {
212                         screen->scr.frame.status =
213                                 "No IP / mask values are set";
214                         nc_scr_frame_draw(&screen->scr);
215                         talloc_free(config);
216                         return -1;
217                 }
218
219                 iface->method = CONFIG_METHOD_STATIC;
220                 iface->static_config.address = talloc_asprintf(iface, "%s/%s",
221                                 ip, mask);
222                 iface->static_config.gateway = talloc_strdup(iface, gateway);
223         }
224
225         str = widget_textbox_get_value(screen->widgets.dns_f);
226         if (str && strlen(str)) {
227                 char *dns, *tmp;
228                 int i;
229
230                 for (;;) {
231                         dns = strtok_r(str, " \t", &tmp);
232
233                         if (!dns)
234                                 break;
235
236                         i = config->network.n_dns_servers++;
237                         config->network.dns_servers = talloc_realloc(config,
238                                         config->network.dns_servers,
239                                         const char *,
240                                         config->network.n_dns_servers);
241                         config->network.dns_servers[i] =
242                                 talloc_strdup(config, dns);
243
244                         str = NULL;
245                 }
246         }
247
248         rc = cui_send_config(screen->cui, config);
249         talloc_free(config);
250
251         if (rc)
252                 pb_log("cui_send_config failed!\n");
253         else
254                 pb_debug("config sent!\n");
255
256         return 0;
257 }
258
259 static void ok_click(void *arg)
260 {
261         struct config_screen *screen = arg;
262         if (screen_process_form(screen))
263                 /* errors are written to the status line, so we'll need
264                  * to refresh */
265                 wrefresh(screen->scr.main_ncw);
266         else
267                 screen->exit = true;
268 }
269
270 static void cancel_click(void *arg)
271 {
272         struct config_screen *screen = arg;
273         screen->exit = true;
274 }
275
276 static int layout_pair(struct config_screen *screen, int y,
277                 struct nc_widget_label *label,
278                 struct nc_widget *field)
279 {
280         struct nc_widget *label_w = widget_label_base(label);
281         widget_move(label_w, y, screen->label_x);
282         widget_move(field, y, screen->field_x);
283         return max(widget_height(label_w), widget_height(field));
284 }
285
286 static void config_screen_layout_widgets(struct config_screen *screen,
287                 enum net_conf_type net_conf)
288 {
289         struct nc_widget *wl, *wf, *wh;
290         int y, x, help_x;
291         bool show;
292
293         y = 1;
294         help_x = screen->field_x + 2 +
295                 widget_width(widget_textbox_base(screen->widgets.dns_f));
296
297         y += layout_pair(screen, y, screen->widgets.autoboot_l,
298                         widget_checkbox_base(screen->widgets.autoboot_f));
299
300         wf = widget_textbox_base(screen->widgets.timeout_f);
301         widget_move(widget_label_base(screen->widgets.timeout_l),
302                         y, screen->label_x);
303         widget_move(wf, y, screen->field_x);
304         widget_move(widget_label_base(screen->widgets.timeout_help_l),
305                         y, screen->field_x + widget_width(wf) + 1);
306
307         y += 2;
308
309         y += layout_pair(screen, y, screen->widgets.network_l,
310                         widget_select_base(screen->widgets.network_f));
311
312         y += 1;
313
314         /* conditionally show iface select */
315         wl = widget_label_base(screen->widgets.iface_l);
316         wf = widget_select_base(screen->widgets.iface_f);
317
318         show = net_conf == NET_CONF_TYPE_DHCP_ONE ||
319                 net_conf == NET_CONF_TYPE_STATIC;
320
321         widget_set_visible(wl, show);
322         widget_set_visible(wf, show);
323
324         if (show)
325                 y += layout_pair(screen, y, screen->widgets.iface_l, wf) + 1;
326
327         /* conditionally show static IP params */
328         show = net_conf == NET_CONF_TYPE_STATIC;
329
330         wl = widget_label_base(screen->widgets.ip_addr_l);
331         wf = widget_textbox_base(screen->widgets.ip_addr_f);
332         widget_set_visible(wl, show);
333         widget_set_visible(wf, show);
334         x = screen->field_x + widget_width(wf) + 1;
335
336         if (show)
337                 layout_pair(screen, y, screen->widgets.ip_addr_l, wf);
338
339         wl = widget_label_base(screen->widgets.ip_mask_l);
340         wf = widget_textbox_base(screen->widgets.ip_mask_f);
341         widget_set_visible(wl, show);
342         widget_set_visible(wf, show);
343
344         if (show) {
345                 widget_move(wl, y, x);
346                 widget_move(wf, y, x + 2);
347         }
348
349         /* help for IP/mask */
350         wh = widget_label_base(screen->widgets.ip_addr_mask_help_l);
351         widget_set_visible(wh, show);
352         if (show) {
353                 widget_move(wh, y, help_x);
354                 y++;
355         }
356
357         wl = widget_label_base(screen->widgets.gateway_l);
358         wf = widget_textbox_base(screen->widgets.gateway_f);
359         wh = widget_label_base(screen->widgets.gateway_help_l);
360         widget_set_visible(wl, show);
361         widget_set_visible(wf, show);
362         widget_set_visible(wh, show);
363
364         if (show) {
365                 layout_pair(screen, y, screen->widgets.gateway_l, wf);
366                 widget_move(wh, y, help_x);
367                 y++;
368         }
369
370         wh = widget_label_base(screen->widgets.dns_help_l);
371         layout_pair(screen, y, screen->widgets.dns_l,
372                         widget_textbox_base(screen->widgets.dns_f));
373         widget_move(wh, y, help_x);
374         y++;
375
376         /* we show the DNS/DHCP help if we're configuring DHCP */
377         show = net_conf != NET_CONF_TYPE_STATIC;
378         wl = widget_label_base(screen->widgets.dns_dhcp_help_l);
379         widget_set_visible(wl, show);
380         if (show) {
381                 widget_move(wl, y, screen->field_x);
382                 y += 1;
383         }
384
385         y += 1;
386
387         widget_move(widget_button_base(screen->widgets.ok_b),
388                         y, screen->field_x);
389         widget_move(widget_button_base(screen->widgets.cancel_b),
390                         y, screen->field_x + 10);
391 }
392
393 static void config_screen_network_change(void *arg, int value)
394 {
395         struct config_screen *screen = arg;
396         screen->net_conf_type = value;
397         widgetset_unpost(screen->widgetset);
398         config_screen_layout_widgets(screen, value);
399         widgetset_post(screen->widgetset);
400 }
401
402 static struct interface_config *first_active_interface(
403                 const struct config *config)
404 {
405         unsigned int i;
406
407         for (i = 0; i < config->network.n_interfaces; i++) {
408                 if (config->network.interfaces[i]->ignore)
409                         continue;
410                 return config->network.interfaces[i];
411         }
412         return NULL;
413 }
414
415 static enum net_conf_type find_net_conf_type(const struct config *config)
416 {
417         struct interface_config *ifcfg;
418
419         ifcfg = first_active_interface(config);
420
421         if (!ifcfg)
422                 return NET_CONF_TYPE_DHCP_ALL;
423
424         else if (ifcfg->method == CONFIG_METHOD_DHCP)
425                 return NET_CONF_TYPE_DHCP_ONE;
426
427         else if (ifcfg->method == CONFIG_METHOD_STATIC)
428                 return NET_CONF_TYPE_STATIC;
429
430         assert(0);
431         return NET_CONF_TYPE_DHCP_ALL;
432 }
433
434 static void config_screen_setup_empty(struct config_screen *screen)
435 {
436         widget_new_label(screen->widgetset, 2, screen->field_x,
437                         "Waiting for configuration data...");
438         screen->widgets.cancel_b = widget_new_button(screen->widgetset,
439                         4, screen->field_x, 6, "Cancel", cancel_click, screen);
440 }
441
442
443 static void config_screen_setup_widgets(struct config_screen *screen,
444                 const struct config *config,
445                 const struct system_info *sysinfo)
446 {
447         struct nc_widgetset *set = screen->widgetset;
448         struct interface_config *ifcfg;
449         char *str, *ip, *mask, *gw;
450         enum net_conf_type type;
451         unsigned int i;
452
453         build_assert(sizeof(screen->widgets) / sizeof(struct widget *)
454                         == N_FIELDS);
455
456         type = screen->net_conf_type;
457         ifcfg = first_active_interface(config);
458
459         screen->widgets.autoboot_l = widget_new_label(set, 0, 0, "Autoboot:");
460         screen->widgets.autoboot_f = widget_new_checkbox(set, 0, 0,
461                                         config->autoboot_enabled);
462
463         str = talloc_asprintf(screen, "%d", config->autoboot_timeout_sec);
464         screen->widgets.timeout_l = widget_new_label(set, 0, 0, "Timeout:");
465         screen->widgets.timeout_f = widget_new_textbox(set, 0, 0, 5, str);
466         screen->widgets.timeout_help_l = widget_new_label(set, 0, 0, "seconds");
467
468         widget_textbox_set_validator_integer(screen->widgets.timeout_f, 0, 999);
469
470         screen->widgets.network_l = widget_new_label(set, 0, 0, "Network");
471         screen->widgets.network_f = widget_new_select(set, 0, 0, 50);
472
473         widget_select_add_option(screen->widgets.network_f,
474                                         NET_CONF_TYPE_DHCP_ALL,
475                                         "DHCP on all active interfaces",
476                                         type == NET_CONF_TYPE_DHCP_ALL);
477         widget_select_add_option(screen->widgets.network_f,
478                                         NET_CONF_TYPE_DHCP_ONE,
479                                         "DHCP on a specific interface",
480                                         type == NET_CONF_TYPE_DHCP_ONE);
481         widget_select_add_option(screen->widgets.network_f,
482                                         NET_CONF_TYPE_STATIC,
483                                         "Static IP configuration",
484                                         type == NET_CONF_TYPE_STATIC);
485
486         widget_select_on_change(screen->widgets.network_f,
487                         config_screen_network_change, screen);
488
489         screen->widgets.iface_l = widget_new_label(set, 0, 0, "Device:");
490         screen->widgets.iface_f = widget_new_select(set, 0, 0, 50);
491
492         for (i = 0; i < sysinfo->n_interfaces; i++) {
493                 struct interface_info *info = sysinfo->interfaces[i];
494                 char str[50], mac[20];
495                 bool is_default;
496
497                 is_default = ifcfg && !memcmp(ifcfg->hwaddr, info->hwaddr,
498                                         sizeof(ifcfg->hwaddr));
499
500                 mac_str(info->hwaddr, info->hwaddr_size, mac, sizeof(mac));
501                 snprintf(str, sizeof(str), "%s [%s, %s]", info->name, mac,
502                                 info->link ? "link up" : "link down");
503
504                 widget_select_add_option(screen->widgets.iface_f,
505                                                 i, str, is_default);
506         }
507
508         gw = ip = mask = NULL;
509         if (ifcfg && ifcfg->method == CONFIG_METHOD_STATIC) {
510                 char *sep;
511
512                 str = talloc_strdup(screen, ifcfg->static_config.address);
513                 sep = strchr(str, '/');
514                 ip = str;
515
516                 if (sep) {
517                         *sep = '\0';
518                         mask = sep + 1;
519                 }
520                 gw = ifcfg->static_config.gateway;
521         }
522
523         screen->widgets.ip_addr_l = widget_new_label(set, 0, 0, "IP/mask:");
524         screen->widgets.ip_addr_f = widget_new_textbox(set, 0, 0, 16, ip);
525         screen->widgets.ip_mask_l = widget_new_label(set, 0, 0, "/");
526         screen->widgets.ip_mask_f = widget_new_textbox(set, 0, 0, 4, mask);
527         screen->widgets.ip_addr_mask_help_l =
528                 widget_new_label(set, 0, 0, "(eg. 192.168.0.10 / 24)");
529
530         widget_textbox_set_validator_ipv4(screen->widgets.ip_addr_f);
531         widget_textbox_set_validator_integer(screen->widgets.ip_mask_f, 1, 31);
532
533         screen->widgets.gateway_l = widget_new_label(set, 0, 0, "Gateway:");
534         screen->widgets.gateway_f = widget_new_textbox(set, 0, 0, 16, gw);
535         screen->widgets.gateway_help_l =
536                 widget_new_label(set, 0, 0, "(eg. 192.168.0.1)");
537
538         widget_textbox_set_validator_ipv4(screen->widgets.gateway_f);
539
540         str = talloc_strdup(screen, "");
541         for (i = 0; i < config->network.n_dns_servers; i++) {
542                 str = talloc_asprintf_append(str, "%s%s",
543                                 (i == 0) ? "" : " ",
544                                 config->network.dns_servers[i]);
545         }
546
547         screen->widgets.dns_l = widget_new_label(set, 0, 0, "DNS Server(s):");
548         screen->widgets.dns_f = widget_new_textbox(set, 0, 0, 32, str);
549         screen->widgets.dns_help_l =
550                 widget_new_label(set, 0, 0, "(eg. 192.168.0.2)");
551
552         widget_textbox_set_validator_ipv4_multi(screen->widgets.dns_f);
553
554         screen->widgets.dns_dhcp_help_l = widget_new_label(set, 0, 0,
555                         "(if not provided by DHCP server)");
556
557         screen->widgets.ok_b = widget_new_button(set, 0, 0, 6, "OK",
558                         ok_click, screen);
559         screen->widgets.cancel_b = widget_new_button(set, 0, 0, 6, "Cancel",
560                         cancel_click, screen);
561 }
562
563 static void config_screen_widget_focus(struct nc_widget *widget, void *arg)
564 {
565         struct config_screen *screen = arg;
566         int w_y, s_max;
567
568         w_y = widget_y(widget) + widget_focus_y(widget);
569         s_max = getmaxy(screen->scr.sub_ncw) - 1;
570
571         if (w_y < screen->scroll_y)
572                 screen->scroll_y = w_y;
573
574         else if (w_y + screen->scroll_y + 1 > s_max)
575                 screen->scroll_y = 1 + w_y - s_max;
576
577         else
578                 return;
579
580         pad_refresh(screen);
581 }
582
583
584 void config_screen_update(struct config_screen *screen,
585                 const struct config *config,
586                 const struct system_info *sysinfo)
587 {
588         bool repost = false;
589         int height;
590
591         /* The size of the pad we'll need depends on the number of interfaces.
592          *
593          * We use N_FIELDS (which is quite conservative, as some fields share
594          * a line) as a base, then add 3 (as the network select field is
595          * takes 3 lines), and n_interfaces (as the network interface field
596          * has n_interfaces lines).
597          */
598         height = N_FIELDS + 3;
599         if (sysinfo)
600                 height += sysinfo->n_interfaces;
601         if (!screen->pad || getmaxy(screen->pad) < height) {
602                 if (screen->pad)
603                         delwin(screen->pad);
604                 screen->pad = newpad(height, COLS);
605         }
606
607         if (screen->widgetset) {
608                 widgetset_unpost(screen->widgetset);
609                 talloc_free(screen->widgetset);
610                 repost = true;
611         }
612
613         screen->widgetset = widgetset_create(screen, screen->scr.main_ncw,
614                         screen->pad);
615         widgetset_set_widget_focus(screen->widgetset,
616                         config_screen_widget_focus, screen);
617
618         if (!config || !sysinfo) {
619                 config_screen_setup_empty(screen);
620         } else {
621                 screen->net_conf_type = find_net_conf_type(config);
622
623                 config_screen_setup_widgets(screen, config, sysinfo);
624                 config_screen_layout_widgets(screen, screen->net_conf_type);
625         }
626
627         if (repost)
628                 widgetset_post(screen->widgetset);
629
630         pad_refresh(screen);
631 }
632
633 static int config_screen_destroy(void *arg)
634 {
635         struct config_screen *screen = arg;
636         if (screen->pad)
637                 delwin(screen->pad);
638         return 0;
639 }
640
641 struct config_screen *config_screen_init(struct cui *cui,
642                 const struct config *config,
643                 const struct system_info *sysinfo,
644                 void (*on_exit)(struct cui *))
645 {
646         struct config_screen *screen;
647
648         screen = talloc_zero(cui, struct config_screen);
649         talloc_set_destructor(screen, config_screen_destroy);
650         nc_scr_init(&screen->scr, pb_config_screen_sig, 0,
651                         cui, config_screen_process_key,
652                         config_screen_post, config_screen_unpost,
653                         config_screen_resize);
654
655         screen->cui = cui;
656         screen->on_exit = on_exit;
657         screen->label_x = 2;
658         screen->field_x = 17;
659
660         screen->scr.frame.ltitle = talloc_strdup(screen,
661                         "Petitboot System Configuration");
662         screen->scr.frame.rtitle = NULL;
663         screen->scr.frame.help = talloc_strdup(screen,
664                         "tab=next, shift+tab=previous");
665         nc_scr_frame_draw(&screen->scr);
666
667         scrollok(screen->scr.sub_ncw, true);
668
669         config_screen_update(screen, config, sysinfo);
670
671         wrefresh(screen->scr.main_ncw);
672
673         return screen;
674 }