ui/ncurses/nc-config: Add validation for network settings
[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         struct config *config = talloc_zero(screen, struct config);
154         enum net_conf_type net_conf_type;
155         struct interface_config *iface;
156         char *str, *end;
157         int rc;
158
159         config_set_defaults(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                         return -1;
216                 }
217
218                 iface->method = CONFIG_METHOD_STATIC;
219                 iface->static_config.address = talloc_asprintf(iface, "%s/%s",
220                                 ip, mask);
221                 iface->static_config.gateway = talloc_strdup(iface, gateway);
222         }
223
224         str = widget_textbox_get_value(screen->widgets.dns_f);
225         if (str && strlen(str)) {
226                 char *dns, *tmp;
227                 int i;
228
229                 for (;;) {
230                         dns = strtok_r(str, " \t", &tmp);
231
232                         if (!dns)
233                                 break;
234
235                         i = config->network.n_dns_servers++;
236                         config->network.dns_servers = talloc_realloc(config,
237                                         config->network.dns_servers,
238                                         const char *,
239                                         config->network.n_dns_servers);
240                         config->network.dns_servers[i] =
241                                 talloc_strdup(config, dns);
242
243                         str = NULL;
244                 }
245         }
246
247         rc = cui_send_config(screen->cui, config);
248         talloc_free(config);
249
250         if (rc)
251                 pb_log("cui_send_config failed!\n");
252         else
253                 pb_debug("config sent!\n");
254
255         return 0;
256 }
257
258 static void ok_click(void *arg)
259 {
260         struct config_screen *screen = arg;
261         if (screen_process_form(screen))
262                 /* errors are written to the status line, so we'll need
263                  * to refresh */
264                 wrefresh(screen->scr.main_ncw);
265         else
266                 screen->exit = true;
267 }
268
269 static void cancel_click(void *arg)
270 {
271         struct config_screen *screen = arg;
272         screen->exit = true;
273 }
274
275 static int layout_pair(struct config_screen *screen, int y,
276                 struct nc_widget_label *label,
277                 struct nc_widget *field)
278 {
279         struct nc_widget *label_w = widget_label_base(label);
280         widget_move(label_w, y, screen->label_x);
281         widget_move(field, y, screen->field_x);
282         return max(widget_height(label_w), widget_height(field));
283 }
284
285 static void config_screen_layout_widgets(struct config_screen *screen,
286                 enum net_conf_type net_conf)
287 {
288         struct nc_widget *wl, *wf, *wh;
289         int y, x, help_x;
290         bool show;
291
292         y = 1;
293         help_x = screen->field_x + 2 +
294                 widget_width(widget_textbox_base(screen->widgets.dns_f));
295
296         y += layout_pair(screen, y, screen->widgets.autoboot_l,
297                         widget_checkbox_base(screen->widgets.autoboot_f));
298
299         wf = widget_textbox_base(screen->widgets.timeout_f);
300         widget_move(widget_label_base(screen->widgets.timeout_l),
301                         y, screen->label_x);
302         widget_move(wf, y, screen->field_x);
303         widget_move(widget_label_base(screen->widgets.timeout_help_l),
304                         y, screen->field_x + widget_width(wf) + 1);
305
306         y += 2;
307
308         y += layout_pair(screen, y, screen->widgets.network_l,
309                         widget_select_base(screen->widgets.network_f));
310
311         y += 1;
312
313         /* conditionally show iface select */
314         wl = widget_label_base(screen->widgets.iface_l);
315         wf = widget_select_base(screen->widgets.iface_f);
316
317         show = net_conf == NET_CONF_TYPE_DHCP_ONE ||
318                 net_conf == NET_CONF_TYPE_STATIC;
319
320         widget_set_visible(wl, show);
321         widget_set_visible(wf, show);
322
323         if (show)
324                 y += layout_pair(screen, y, screen->widgets.iface_l, wf) + 1;
325
326         /* conditionally show static IP params */
327         show = net_conf == NET_CONF_TYPE_STATIC;
328
329         wl = widget_label_base(screen->widgets.ip_addr_l);
330         wf = widget_textbox_base(screen->widgets.ip_addr_f);
331         widget_set_visible(wl, show);
332         widget_set_visible(wf, show);
333         x = screen->field_x + widget_width(wf) + 1;
334
335         if (show)
336                 layout_pair(screen, y, screen->widgets.ip_addr_l, wf);
337
338         wl = widget_label_base(screen->widgets.ip_mask_l);
339         wf = widget_textbox_base(screen->widgets.ip_mask_f);
340         widget_set_visible(wl, show);
341         widget_set_visible(wf, show);
342
343         if (show) {
344                 widget_move(wl, y, x);
345                 widget_move(wf, y, x + 2);
346         }
347
348         /* help for IP/mask */
349         wh = widget_label_base(screen->widgets.ip_addr_mask_help_l);
350         widget_set_visible(wh, show);
351         if (show) {
352                 widget_move(wh, y, help_x);
353                 y++;
354         }
355
356         wl = widget_label_base(screen->widgets.gateway_l);
357         wf = widget_textbox_base(screen->widgets.gateway_f);
358         wh = widget_label_base(screen->widgets.gateway_help_l);
359         widget_set_visible(wl, show);
360         widget_set_visible(wf, show);
361         widget_set_visible(wh, show);
362
363         if (show) {
364                 layout_pair(screen, y, screen->widgets.gateway_l, wf);
365                 widget_move(wh, y, help_x);
366                 y++;
367         }
368
369         wh = widget_label_base(screen->widgets.dns_help_l);
370         layout_pair(screen, y, screen->widgets.dns_l,
371                         widget_textbox_base(screen->widgets.dns_f));
372         widget_move(wh, y, help_x);
373         y++;
374
375         /* we show the DNS/DHCP help if we're configuring DHCP */
376         show = net_conf != NET_CONF_TYPE_STATIC;
377         wl = widget_label_base(screen->widgets.dns_dhcp_help_l);
378         widget_set_visible(wl, show);
379         if (show) {
380                 widget_move(wl, y, screen->field_x);
381                 y += 1;
382         }
383
384         y += 1;
385
386         widget_move(widget_button_base(screen->widgets.ok_b),
387                         y, screen->field_x);
388         widget_move(widget_button_base(screen->widgets.cancel_b),
389                         y, screen->field_x + 10);
390 }
391
392 static void config_screen_network_change(void *arg, int value)
393 {
394         struct config_screen *screen = arg;
395         screen->net_conf_type = value;
396         widgetset_unpost(screen->widgetset);
397         config_screen_layout_widgets(screen, value);
398         widgetset_post(screen->widgetset);
399 }
400
401 static struct interface_config *first_active_interface(
402                 const struct config *config)
403 {
404         unsigned int i;
405
406         for (i = 0; i < config->network.n_interfaces; i++) {
407                 if (config->network.interfaces[i]->ignore)
408                         continue;
409                 return config->network.interfaces[i];
410         }
411         return NULL;
412 }
413
414 static enum net_conf_type find_net_conf_type(const struct config *config)
415 {
416         struct interface_config *ifcfg;
417
418         ifcfg = first_active_interface(config);
419
420         if (!ifcfg)
421                 return NET_CONF_TYPE_DHCP_ALL;
422
423         else if (ifcfg->method == CONFIG_METHOD_DHCP)
424                 return NET_CONF_TYPE_DHCP_ONE;
425
426         else if (ifcfg->method == CONFIG_METHOD_STATIC)
427                 return NET_CONF_TYPE_STATIC;
428
429         assert(0);
430         return NET_CONF_TYPE_DHCP_ALL;
431 }
432
433 static void config_screen_setup_empty(struct config_screen *screen)
434 {
435         widget_new_label(screen->widgetset, 2, screen->field_x,
436                         "Waiting for configuration data...");
437         screen->widgets.cancel_b = widget_new_button(screen->widgetset,
438                         4, screen->field_x, 6, "Cancel", cancel_click, screen);
439 }
440
441
442 static void config_screen_setup_widgets(struct config_screen *screen,
443                 const struct config *config,
444                 const struct system_info *sysinfo)
445 {
446         struct nc_widgetset *set = screen->widgetset;
447         struct interface_config *ifcfg;
448         char *str, *ip, *mask, *gw;
449         enum net_conf_type type;
450         unsigned int i;
451
452         build_assert(sizeof(screen->widgets) / sizeof(struct widget *)
453                         == N_FIELDS);
454
455         type = screen->net_conf_type;
456         ifcfg = first_active_interface(config);
457
458         screen->widgets.autoboot_l = widget_new_label(set, 0, 0, "Autoboot:");
459         screen->widgets.autoboot_f = widget_new_checkbox(set, 0, 0,
460                                         config->autoboot_enabled);
461
462         str = talloc_asprintf(screen, "%d", config->autoboot_timeout_sec);
463         screen->widgets.timeout_l = widget_new_label(set, 0, 0, "Timeout:");
464         screen->widgets.timeout_f = widget_new_textbox(set, 0, 0, 5, str);
465         screen->widgets.timeout_help_l = widget_new_label(set, 0, 0, "seconds");
466
467         widget_textbox_set_validator_integer(screen->widgets.timeout_f, 0, 999);
468
469         screen->widgets.network_l = widget_new_label(set, 0, 0, "Network");
470         screen->widgets.network_f = widget_new_select(set, 0, 0, 50);
471
472         widget_select_add_option(screen->widgets.network_f,
473                                         NET_CONF_TYPE_DHCP_ALL,
474                                         "DHCP on all active interfaces",
475                                         type == NET_CONF_TYPE_DHCP_ALL);
476         widget_select_add_option(screen->widgets.network_f,
477                                         NET_CONF_TYPE_DHCP_ONE,
478                                         "DHCP on a specific interface",
479                                         type == NET_CONF_TYPE_DHCP_ONE);
480         widget_select_add_option(screen->widgets.network_f,
481                                         NET_CONF_TYPE_STATIC,
482                                         "Static IP configuration",
483                                         type == NET_CONF_TYPE_STATIC);
484
485         widget_select_on_change(screen->widgets.network_f,
486                         config_screen_network_change, screen);
487
488         screen->widgets.iface_l = widget_new_label(set, 0, 0, "Device:");
489         screen->widgets.iface_f = widget_new_select(set, 0, 0, 50);
490
491         for (i = 0; i < sysinfo->n_interfaces; i++) {
492                 struct interface_info *info = sysinfo->interfaces[i];
493                 char str[50], mac[20];
494                 bool is_default;
495
496                 is_default = ifcfg && !memcmp(ifcfg->hwaddr, info->hwaddr,
497                                         sizeof(ifcfg->hwaddr));
498
499                 mac_str(info->hwaddr, info->hwaddr_size, mac, sizeof(mac));
500                 snprintf(str, sizeof(str), "%s [%s, %s]", info->name, mac,
501                                 info->link ? "link up" : "link down");
502
503                 widget_select_add_option(screen->widgets.iface_f,
504                                                 i, str, is_default);
505         }
506
507         gw = ip = mask = NULL;
508         if (ifcfg && ifcfg->method == CONFIG_METHOD_STATIC) {
509                 char *sep;
510
511                 str = talloc_strdup(screen, ifcfg->static_config.address);
512                 sep = strchr(str, '/');
513                 ip = str;
514
515                 if (sep) {
516                         *sep = '\0';
517                         mask = sep + 1;
518                 }
519                 gw = ifcfg->static_config.gateway;
520         }
521
522         screen->widgets.ip_addr_l = widget_new_label(set, 0, 0, "IP/mask:");
523         screen->widgets.ip_addr_f = widget_new_textbox(set, 0, 0, 16, ip);
524         screen->widgets.ip_mask_l = widget_new_label(set, 0, 0, "/");
525         screen->widgets.ip_mask_f = widget_new_textbox(set, 0, 0, 4, mask);
526         screen->widgets.ip_addr_mask_help_l =
527                 widget_new_label(set, 0, 0, "(eg. 192.168.0.10 / 24)");
528
529         widget_textbox_set_validator_ipv4(screen->widgets.ip_addr_f);
530         widget_textbox_set_validator_integer(screen->widgets.ip_mask_f, 1, 31);
531
532         screen->widgets.gateway_l = widget_new_label(set, 0, 0, "Gateway:");
533         screen->widgets.gateway_f = widget_new_textbox(set, 0, 0, 16, gw);
534         screen->widgets.gateway_help_l =
535                 widget_new_label(set, 0, 0, "(eg. 192.168.0.1)");
536
537         widget_textbox_set_validator_ipv4(screen->widgets.gateway_f);
538
539         str = talloc_strdup(screen, "");
540         for (i = 0; i < config->network.n_dns_servers; i++) {
541                 str = talloc_asprintf_append(str, "%s%s",
542                                 (i == 0) ? "" : " ",
543                                 config->network.dns_servers[i]);
544         }
545
546         screen->widgets.dns_l = widget_new_label(set, 0, 0, "DNS Server(s):");
547         screen->widgets.dns_f = widget_new_textbox(set, 0, 0, 32, str);
548         screen->widgets.dns_help_l =
549                 widget_new_label(set, 0, 0, "(eg. 192.168.0.2)");
550
551         widget_textbox_set_validator_ipv4_multi(screen->widgets.dns_f);
552
553         screen->widgets.dns_dhcp_help_l = widget_new_label(set, 0, 0,
554                         "(if not provided by DHCP server)");
555
556         screen->widgets.ok_b = widget_new_button(set, 0, 0, 6, "OK",
557                         ok_click, screen);
558         screen->widgets.cancel_b = widget_new_button(set, 0, 0, 6, "Cancel",
559                         cancel_click, screen);
560 }
561
562 static void config_screen_widget_focus(struct nc_widget *widget, void *arg)
563 {
564         struct config_screen *screen = arg;
565         int w_y, s_max;
566
567         w_y = widget_y(widget) + widget_focus_y(widget);
568         s_max = getmaxy(screen->scr.sub_ncw) - 1;
569
570         if (w_y < screen->scroll_y)
571                 screen->scroll_y = w_y;
572
573         else if (w_y + screen->scroll_y + 1 > s_max)
574                 screen->scroll_y = 1 + w_y - s_max;
575
576         else
577                 return;
578
579         pad_refresh(screen);
580 }
581
582
583 void config_screen_update(struct config_screen *screen,
584                 const struct config *config,
585                 const struct system_info *sysinfo)
586 {
587         bool repost = false;
588         int height;
589
590         /* The size of the pad we'll need depends on the number of interfaces.
591          *
592          * We use N_FIELDS (which is quite conservative, as some fields share
593          * a line) as a base, then add 3 (as the network select field is
594          * takes 3 lines), and n_interfaces (as the network interface field
595          * has n_interfaces lines).
596          */
597         height = N_FIELDS + 3;
598         if (sysinfo)
599                 height += sysinfo->n_interfaces;
600         if (!screen->pad || getmaxy(screen->pad) < height) {
601                 if (screen->pad)
602                         delwin(screen->pad);
603                 screen->pad = newpad(height, COLS);
604         }
605
606         if (screen->widgetset) {
607                 widgetset_unpost(screen->widgetset);
608                 talloc_free(screen->widgetset);
609                 repost = true;
610         }
611
612         screen->widgetset = widgetset_create(screen, screen->scr.main_ncw,
613                         screen->pad);
614         widgetset_set_widget_focus(screen->widgetset,
615                         config_screen_widget_focus, screen);
616
617         if (!config || !sysinfo) {
618                 config_screen_setup_empty(screen);
619         } else {
620                 screen->net_conf_type = find_net_conf_type(config);
621
622                 config_screen_setup_widgets(screen, config, sysinfo);
623                 config_screen_layout_widgets(screen, screen->net_conf_type);
624         }
625
626         if (repost)
627                 widgetset_post(screen->widgetset);
628
629         pad_refresh(screen);
630 }
631
632 static int config_screen_destroy(void *arg)
633 {
634         struct config_screen *screen = arg;
635         if (screen->pad)
636                 delwin(screen->pad);
637         return 0;
638 }
639
640 struct config_screen *config_screen_init(struct cui *cui,
641                 const struct config *config,
642                 const struct system_info *sysinfo,
643                 void (*on_exit)(struct cui *))
644 {
645         struct config_screen *screen;
646
647         screen = talloc_zero(cui, struct config_screen);
648         talloc_set_destructor(screen, config_screen_destroy);
649         nc_scr_init(&screen->scr, pb_config_screen_sig, 0,
650                         cui, config_screen_process_key,
651                         config_screen_post, config_screen_unpost,
652                         config_screen_resize);
653
654         screen->cui = cui;
655         screen->on_exit = on_exit;
656         screen->label_x = 2;
657         screen->field_x = 17;
658
659         screen->scr.frame.ltitle = talloc_strdup(screen,
660                         "Petitboot System Configuration");
661         screen->scr.frame.rtitle = NULL;
662         screen->scr.frame.help = talloc_strdup(screen,
663                         "tab=next, shift+tab=previous");
664         nc_scr_frame_draw(&screen->scr);
665
666         scrollok(screen->scr.sub_ncw, true);
667
668         config_screen_update(screen, config, sysinfo);
669
670         wrefresh(screen->scr.main_ncw);
671
672         return screen;
673 }