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