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