ui/ncurses: Populate configuration interface with current defaults
[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 #define _GNU_SOURCE
19
20 #include <errno.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include <pb-config/pb-config.h>
25 #include <talloc/talloc.h>
26 #include <types/types.h>
27 #include <log/log.h>
28
29 #include "config.h"
30 #include "nc-cui.h"
31 #include "nc-config.h"
32 #include "nc-widgets.h"
33
34 #define N_FIELDS        18
35
36 enum net_conf_type {
37         NET_CONF_TYPE_DHCP_ALL,
38         NET_CONF_TYPE_DHCP_ONE,
39         NET_CONF_TYPE_STATIC,
40 };
41
42 struct config_screen {
43         struct nc_scr           scr;
44         struct cui              *cui;
45         struct nc_widgetset     *widgetset;
46         bool                    exit;
47         void                    (*on_exit)(struct cui *);
48
49         int                     label_x;
50         int                     field_x;
51         int                     network_config_y;
52
53         enum net_conf_type      net_conf_type;
54
55         struct {
56                 struct nc_widget_checkbox       *autoboot_f;
57                 struct nc_widget_label          *autoboot_l;
58                 struct nc_widget_textbox        *timeout_f;
59                 struct nc_widget_label          *timeout_l;
60
61                 struct nc_widget_label          *network_l;
62                 struct nc_widget_select         *network_f;
63
64                 struct nc_widget_label          *iface_l;
65                 struct nc_widget_select         *iface_f;
66                 struct nc_widget_label          *ip_addr_l;
67                 struct nc_widget_textbox        *ip_addr_f;
68                 struct nc_widget_label          *ip_mask_l;
69                 struct nc_widget_textbox        *ip_mask_f;
70                 struct nc_widget_label          *gateway_l;
71                 struct nc_widget_textbox        *gateway_f;
72                 struct nc_widget_label          *dns_l;
73                 struct nc_widget_textbox        *dns_f;
74
75                 struct nc_widget_button         *ok_b;
76                 struct nc_widget_button         *cancel_b;
77         } widgets;
78 };
79
80 static struct config_screen *config_screen_from_scr(struct nc_scr *scr)
81 {
82         struct config_screen *config_screen;
83
84         assert(scr->sig == pb_config_screen_sig);
85         config_screen = (struct config_screen *)
86                 ((char *)scr - (size_t)&((struct config_screen *)0)->scr);
87         assert(config_screen->scr.sig == pb_config_screen_sig);
88         return config_screen;
89 }
90
91 static void config_screen_process_key(struct nc_scr *scr, int key)
92 {
93         struct config_screen *screen = config_screen_from_scr(scr);
94         bool handled;
95
96         handled = widgetset_process_key(screen->widgetset, key);
97         if (screen->exit)
98                 screen->on_exit(screen->cui);
99         else if (handled)
100                 wrefresh(screen->scr.main_ncw);
101 }
102
103 static void config_screen_resize(struct nc_scr *scr)
104 {
105         struct config_screen *screen = config_screen_from_scr(scr);
106         (void)screen;
107 }
108
109 static int config_screen_post(struct nc_scr *scr)
110 {
111         struct config_screen *screen = config_screen_from_scr(scr);
112         widgetset_post(screen->widgetset);
113         nc_scr_frame_draw(scr);
114         wrefresh(scr->main_ncw);
115         return 0;
116 }
117
118 static int config_screen_unpost(struct nc_scr *scr)
119 {
120         struct config_screen *screen = config_screen_from_scr(scr);
121         widgetset_unpost(screen->widgetset);
122         return 0;
123 }
124
125 struct nc_scr *config_screen_scr(struct config_screen *screen)
126 {
127         return &screen->scr;
128 }
129
130 static int screen_process_form(struct config_screen *screen)
131 {
132         const struct system_info *sysinfo = screen->cui->sysinfo;
133         struct config *config = talloc_zero(screen, struct config);
134         enum net_conf_type net_conf_type;
135         struct interface_config *iface;
136         char *str, *end;
137         int rc;
138
139         config_set_defaults(config);
140
141         config->autoboot_enabled =
142                 widget_checkbox_get_value(screen->widgets.autoboot_f);
143
144
145         str = widget_textbox_get_value(screen->widgets.timeout_f);
146         if (str) {
147                 unsigned long x;
148                 errno = 0;
149                 x = strtoul(str, &end, 10);
150                 if (!errno && end != str)
151                         config->autoboot_timeout_sec = x;
152         }
153
154         net_conf_type = widget_select_get_value(screen->widgets.network_f);
155
156         /* if we don't have any network interfaces, prevent per-interface
157          * configuration */
158         if (sysinfo->n_interfaces == 0)
159                 net_conf_type = NET_CONF_TYPE_DHCP_ALL;
160
161         if (net_conf_type == NET_CONF_TYPE_DHCP_ALL) {
162                 config->network.n_interfaces = 0;
163
164         } else {
165                 int idx;
166
167                 iface = talloc_zero(config, struct interface_config);
168                 config->network.n_interfaces = 1;
169                 config->network.interfaces = talloc_array(config,
170                                 struct interface_config *, 1);
171                 config->network.interfaces[0] = iface;
172
173                 /* copy hwaddr (from the sysinfo interface data) to
174                  * the configuration */
175                 idx = widget_select_get_value(screen->widgets.iface_f);
176                 memcpy(iface->hwaddr, sysinfo->interfaces[idx]->hwaddr,
177                                 sizeof(iface->hwaddr));
178         }
179
180         if (net_conf_type == NET_CONF_TYPE_DHCP_ONE) {
181                 iface->method = CONFIG_METHOD_DHCP;
182         }
183
184         if (net_conf_type == NET_CONF_TYPE_STATIC) {
185                 iface->method = CONFIG_METHOD_STATIC;
186                 iface->static_config.address = talloc_asprintf(iface, "%s/%s",
187                                 widget_textbox_get_value(
188                                         screen->widgets.ip_addr_f),
189                                 widget_textbox_get_value(
190                                         screen->widgets.ip_mask_f));
191                 iface->static_config.gateway = talloc_strdup(iface,
192                                 widget_textbox_get_value(
193                                         screen->widgets.gateway_f));
194         }
195
196         str = widget_textbox_get_value(screen->widgets.dns_f);
197         if (str && strlen(str)) {
198                 char *dns, *tmp;
199                 int i;
200
201                 for (;;) {
202                         dns = strtok_r(str, " \t", &tmp);
203
204                         if (!dns)
205                                 break;
206
207                         i = config->network.n_dns_servers++;
208                         config->network.dns_servers = talloc_realloc(config,
209                                         config->network.dns_servers,
210                                         const char *,
211                                         config->network.n_dns_servers);
212                         config->network.dns_servers[i] =
213                                 talloc_strdup(config, dns);
214
215                         str = NULL;
216                 }
217         }
218
219         rc = cui_send_config(screen->cui, config);
220         talloc_free(config);
221
222         if (rc)
223                 pb_log("cui_send_config failed!\n");
224         else
225                 pb_debug("config sent!\n");
226
227         return 0;
228 }
229
230 static void ok_click(void *arg)
231 {
232         struct config_screen *screen = arg;
233         screen_process_form(screen);
234         screen->exit = true;
235 }
236
237 static void cancel_click(void *arg)
238 {
239         struct config_screen *screen = arg;
240         screen->exit = true;
241 }
242
243 static int layout_pair(struct config_screen *screen, int y,
244                 struct nc_widget_label *label,
245                 struct nc_widget *field)
246 {
247         struct nc_widget *label_w = widget_label_base(label);
248         widget_move(label_w, y, screen->label_x);
249         widget_move(field, y, screen->field_x);
250         return max(widget_height(label_w), widget_height(field));
251 }
252
253 static void config_screen_layout_widgets(struct config_screen *screen,
254                 enum net_conf_type net_conf)
255 {
256         struct nc_widget *wl, *wf;
257         bool show;
258         int y, x;
259
260         y = 1;
261
262         y += layout_pair(screen, y, screen->widgets.autoboot_l,
263                         widget_checkbox_base(screen->widgets.autoboot_f));
264
265         y += layout_pair(screen, y, screen->widgets.timeout_l,
266                         widget_textbox_base(screen->widgets.timeout_f));
267
268         y += 1;
269
270         y += layout_pair(screen, y, screen->widgets.network_l,
271                         widget_select_base(screen->widgets.network_f));
272
273         y += 1;
274
275         /* conditionally show iface select */
276         wl = widget_label_base(screen->widgets.iface_l);
277         wf = widget_select_base(screen->widgets.iface_f);
278
279         show = net_conf == NET_CONF_TYPE_DHCP_ONE ||
280                 net_conf == NET_CONF_TYPE_STATIC;
281
282         widget_set_visible(wl, show);
283         widget_set_visible(wf, show);
284
285         if (show)
286                 y += layout_pair(screen, y, screen->widgets.iface_l, wf) + 1;
287
288         /* conditionally show static IP params */
289         show = net_conf == NET_CONF_TYPE_STATIC;
290
291         wl = widget_label_base(screen->widgets.ip_addr_l);
292         wf = widget_textbox_base(screen->widgets.ip_addr_f);
293         widget_set_visible(wl, show);
294         widget_set_visible(wf, show);
295         x = screen->field_x + widget_width(wf) + 1;
296
297         if (show)
298                 layout_pair(screen, y, screen->widgets.ip_addr_l, wf);
299
300         wl = widget_label_base(screen->widgets.ip_mask_l);
301         wf = widget_textbox_base(screen->widgets.ip_mask_f);
302         widget_set_visible(wl, show);
303         widget_set_visible(wf, show);
304
305         if (show) {
306                 widget_move(wl, y, x);
307                 widget_move(wf, y, x + 2);
308                 y += 1;
309         }
310
311         wl = widget_label_base(screen->widgets.gateway_l);
312         wf = widget_textbox_base(screen->widgets.gateway_f);
313         widget_set_visible(wl, show);
314         widget_set_visible(wf, show);
315
316         if (show)
317                 y += layout_pair(screen, y, screen->widgets.gateway_l, wf) + 1;
318
319         y += layout_pair(screen, y, screen->widgets.dns_l,
320                         widget_textbox_base(screen->widgets.dns_f));
321
322         y += 1;
323
324         widget_move(widget_button_base(screen->widgets.ok_b),
325                         y, screen->field_x);
326         widget_move(widget_button_base(screen->widgets.cancel_b),
327                         y, screen->field_x + 10);
328 }
329
330 static void config_screen_network_change(void *arg, int value)
331 {
332         struct config_screen *screen = arg;
333         screen->net_conf_type = value;
334         widgetset_unpost(screen->widgetset);
335         config_screen_layout_widgets(screen, value);
336         widgetset_post(screen->widgetset);
337 }
338
339 static struct interface_config *first_active_interface(
340                 const struct config *config)
341 {
342         unsigned int i;
343
344         for (i = 0; i < config->network.n_interfaces; i++) {
345                 if (config->network.interfaces[i]->ignore)
346                         continue;
347                 return config->network.interfaces[i];
348         }
349         return NULL;
350 }
351
352 static enum net_conf_type find_net_conf_type(const struct config *config)
353 {
354         struct interface_config *ifcfg;
355
356         ifcfg = first_active_interface(config);
357
358         if (!ifcfg)
359                 return NET_CONF_TYPE_DHCP_ALL;
360
361         else if (ifcfg->method == CONFIG_METHOD_DHCP)
362                 return NET_CONF_TYPE_DHCP_ONE;
363
364         else if (ifcfg->method == CONFIG_METHOD_STATIC)
365                 return NET_CONF_TYPE_STATIC;
366
367         assert(0);
368         return NET_CONF_TYPE_DHCP_ALL;
369 }
370
371 static void config_screen_setup_widgets(struct config_screen *screen,
372                 const struct config *config,
373                 const struct system_info *sysinfo)
374 {
375         struct nc_widgetset *set = screen->widgetset;
376         struct interface_config *ifcfg;
377         char *str, *ip, *mask, *gw;
378         enum net_conf_type type;
379         unsigned int i;
380
381         build_assert(sizeof(screen->widgets) / sizeof(struct widget *)
382                         == N_FIELDS);
383
384         type = screen->net_conf_type;
385         ifcfg = first_active_interface(config);
386
387         screen->widgets.autoboot_l = widget_new_label(set, 0, 0, "Autoboot:");
388         screen->widgets.autoboot_f = widget_new_checkbox(set, 0, 0,
389                                         config->autoboot_enabled);
390
391         str = talloc_asprintf(screen, "%d", config->autoboot_timeout_sec);
392         screen->widgets.timeout_l = widget_new_label(set, 0, 0, "Timeout:");
393         screen->widgets.timeout_f = widget_new_textbox(set, 0, 0, 5, str);
394
395         screen->widgets.network_l = widget_new_label(set, 0, 0, "Network");
396         screen->widgets.network_f = widget_new_select(set, 0, 0, 50);
397
398         widget_select_add_option(screen->widgets.network_f,
399                                         NET_CONF_TYPE_DHCP_ALL,
400                                         "DHCP on all active interfaces",
401                                         type == NET_CONF_TYPE_DHCP_ALL);
402         widget_select_add_option(screen->widgets.network_f,
403                                         NET_CONF_TYPE_DHCP_ONE,
404                                         "DHCP on a specific interface",
405                                         type == NET_CONF_TYPE_DHCP_ONE);
406         widget_select_add_option(screen->widgets.network_f,
407                                         NET_CONF_TYPE_STATIC,
408                                         "Static IP configuration",
409                                         type == NET_CONF_TYPE_STATIC);
410
411         widget_select_on_change(screen->widgets.network_f,
412                         config_screen_network_change, screen);
413
414         screen->widgets.iface_l = widget_new_label(set, 0, 0, "Device:");
415         screen->widgets.iface_f = widget_new_select(set, 0, 0, 20);
416
417         for (i = 0; i < sysinfo->n_interfaces; i++) {
418                 struct interface_info *info = sysinfo->interfaces[i];
419                 bool is_default;
420
421                 is_default = ifcfg && !memcmp(ifcfg->hwaddr, info->hwaddr,
422                                         sizeof(ifcfg->hwaddr));
423
424                 widget_select_add_option(screen->widgets.iface_f,
425                                                 i, info->name, is_default);
426         }
427
428         gw = ip = mask = NULL;
429         if (ifcfg && ifcfg->method == CONFIG_METHOD_STATIC) {
430                 char *sep;
431
432                 str = talloc_strdup(screen, ifcfg->static_config.address);
433                 sep = strchr(str, '/');
434                 ip = str;
435
436                 if (sep) {
437                         *sep = '\0';
438                         mask = sep + 1;
439                 }
440                 gw = ifcfg->static_config.gateway;
441         }
442
443         screen->widgets.ip_addr_l = widget_new_label(set, 0, 0, "IP/mask:");
444         screen->widgets.ip_addr_f = widget_new_textbox(set, 0, 0, 16, ip);
445         screen->widgets.ip_mask_l = widget_new_label(set, 0, 0, "/");
446         screen->widgets.ip_mask_f = widget_new_textbox(set, 0, 0, 3, mask);
447
448         screen->widgets.gateway_l = widget_new_label(set, 0, 0, "Gateway:");
449         screen->widgets.gateway_f = widget_new_textbox(set, 0, 0, 16, gw);
450
451         str = talloc_strdup(screen, "");
452         for (i = 0; i < config->network.n_dns_servers; i++) {
453                 str = talloc_asprintf_append(str, "%s%s",
454                                 (i == 0) ? "" : " ",
455                                 config->network.dns_servers[i]);
456         }
457
458         screen->widgets.dns_l = widget_new_label(set, 0, 0, "DNS Server(s):");
459         screen->widgets.dns_f = widget_new_textbox(set, 0, 0, 32, str);
460
461         screen->widgets.ok_b = widget_new_button(set, 0, 0, 6, "OK",
462                         ok_click, screen);
463         screen->widgets.cancel_b = widget_new_button(set, 0, 0, 6, "Cancel",
464                         cancel_click, screen);
465 }
466
467 struct config_screen *config_screen_init(struct cui *cui,
468                 const struct config *config,
469                 const struct system_info *sysinfo,
470                 void (*on_exit)(struct cui *))
471 {
472         struct config_screen *screen;
473
474         screen = talloc_zero(cui, struct config_screen);
475         nc_scr_init(&screen->scr, pb_config_screen_sig, 0,
476                         cui, config_screen_process_key,
477                         config_screen_post, config_screen_unpost,
478                         config_screen_resize);
479
480         screen->cui = cui;
481         screen->on_exit = on_exit;
482         screen->label_x = 2;
483         screen->field_x = 17;
484
485         screen->scr.frame.ltitle = talloc_strdup(screen,
486                         "Petitboot System Configuration");
487         screen->scr.frame.rtitle = NULL;
488         screen->scr.frame.help = talloc_strdup(screen,
489                         "tab=next, shift+tab=previous");
490         nc_scr_frame_draw(&screen->scr);
491
492         screen->widgetset = widgetset_create(screen, screen->scr.main_ncw,
493                         screen->scr.sub_ncw);
494         screen->net_conf_type = find_net_conf_type(config);
495
496         config_screen_setup_widgets(screen, config, sysinfo);
497         config_screen_layout_widgets(screen, screen->net_conf_type);
498
499         wrefresh(screen->scr.main_ncw);
500         scrollok(screen->scr.sub_ncw, true);
501
502         return screen;
503 }