Merge branch 'vendor/OPENSSH'
[dragonfly.git] / usr.sbin / installer / dfuibe_installer / fn_configure.c
1 /*
2  * Copyright (c)2004 The DragonFly Project.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  *   Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  *
11  *   Redistributions in binary form must reproduce the above copyright
12  *   notice, this list of conditions and the following disclaimer in
13  *   the documentation and/or other materials provided with the
14  *   distribution.
15  *
16  *   Neither the name of the DragonFly Project nor the names of its
17  *   contributors may be used to endorse or promote products derived
18  *   from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31  * OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 /*
35  * fn_configure.c
36  * Configuration functions for installer.
37  * This includes both Configure the LiveCD Environment, and
38  * Configure an Installed System (there is considerable overlap.)
39  * $Id: fn_configure.c,v 1.82 2005/03/25 05:24:00 cpressey Exp $
40  */
41
42 #include <sys/stat.h>
43 #include <sys/types.h>
44
45 #include <ctype.h>
46 #include <dirent.h>
47 #include <fcntl.h>
48 #include <libgen.h>
49 #include <stdarg.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <time.h>
54 #include <unistd.h>
55
56 #ifdef ENABLE_NLS
57 #include <libintl.h>
58 #define _(String) gettext (String)
59 #else
60 #define _(String) (String)
61 #endif
62
63 #include "libaura/mem.h"
64 #include "libaura/dict.h"
65 #include "libaura/fspred.h"
66
67 #include "libdfui/dfui.h"
68 #include "libdfui/system.h"
69
70 #include "libinstaller/commands.h"
71 #include "libinstaller/confed.h"
72 #include "libinstaller/diskutil.h"
73 #include "libinstaller/functions.h"
74 #include "libinstaller/package.h"
75 #include "libinstaller/uiutil.h"
76
77 #include "fn.h"
78 #include "flow.h"
79 #include "pathnames.h"
80
81 static const char       *yes_to_y(const char *);
82 static char             *convert_tmpfs_options(char *);
83
84 /** CONFIGURE FUNCTIONS **/
85
86 #define PW_NOT_ALLOWED          ":;,`~!@#$%^&*()+={}[]\\|/?<>'\" "
87 #define GECOS_NOT_ALLOWED       ":,\\\""
88 #define FILENAME_NOT_ALLOWED    ":;`~!#$^&*()={}[]\\|?<>'\" "
89 #define MEMBERSHIPS_NOT_ALLOWED ":;`~!@#$%^&*()+={}[]\\|/?<>'\" "
90
91 void
92 fn_add_user(struct i_fn_args *a)
93 {
94         struct dfui_dataset *ds, *new_ds;
95         struct dfui_form *f;
96         struct dfui_response *r;
97         struct commands *cmds;
98         struct command *cmd;
99         const char *username, *home, *passwd_1, *passwd_2, *gecos;
100         const char *shell, *uid, *group, *groups;
101         int done = 0;
102
103         f = dfui_form_create(
104             "add_user",
105             _("Add user"),
106             _("Here you can add a user to an installed system.\n\n"
107             "You can leave the Home Directory, User ID, and Login Group "
108             "fields empty if you want these items to be automatically "
109             "allocated by the system.\n\n"
110             "Note: this user's password will appear in the install log. "
111             "If this is a problem, please add the user manually after "
112             "rebooting into the installed system instead."),
113             "",
114             "f", "username", _("Username"),
115             _("Enter the username the user will log in as"), "",
116             "f", "gecos", _("Real Name"),
117             _("Enter the real name (or GECOS field) of this user"), "",
118             "f", "passwd_1", _("Password"),
119             _("Enter the user's password (will not be displayed)"), "",
120             "p", "obscured", "true",
121             "f", "passwd_2", _("Password (Again)"),
122             _("Re-enter the user's password to confirm"), "",
123             "p", "obscured", "true",
124             "f", "shell", _("Shell"),
125             _("Enter the full path to the user's shell program"), "",
126             "f", "home", _("Home Directory"),
127             _("Enter the full path to the user's home directory, or leave blank"), "",
128             "f", "uid", _("User ID"),
129             _("Enter this account's numeric user id, or leave blank"), "",
130             "f", "group", _("Login Group"),
131             _("Enter the primary group for this account, or leave blank"), "",
132             "f", "groups", _("Other Group Memberships"),
133             _("Enter a comma-separated list of other groups "
134             "that this user should belong to"), "",
135             "a", "ok", _("Accept and Add"), "", "",
136             "a", "cancel", _("Return to Configure Menu"), "", "",
137             "p", "accelerator", "ESC",
138             NULL
139         );
140
141         ds = dfui_dataset_new();
142         dfui_dataset_celldata_add(ds, "username", "");
143         dfui_dataset_celldata_add(ds, "gecos", "");
144         dfui_dataset_celldata_add(ds, "passwd_1", "");
145         dfui_dataset_celldata_add(ds, "passwd_2", "");
146         dfui_dataset_celldata_add(ds, "shell", "/bin/tcsh");
147         dfui_dataset_celldata_add(ds, "home", "");
148         dfui_dataset_celldata_add(ds, "uid", "");
149         dfui_dataset_celldata_add(ds, "group", "");
150         dfui_dataset_celldata_add(ds, "groups", "");
151         dfui_form_dataset_add(f, ds);
152
153         while (!done) {
154                 if (!dfui_be_present(a->c, f, &r))
155                         abort_backend();
156
157                 if (strcmp(dfui_response_get_action_id(r), "cancel") == 0) {
158                         done = 1;
159                         dfui_response_free(r);
160                         break;
161                 }
162
163                 new_ds = dfui_dataset_dup(dfui_response_dataset_get_first(r));
164                 dfui_form_datasets_free(f);
165                 dfui_form_dataset_add(f, new_ds);
166
167                 /* Fetch form field values. */
168
169                 username = dfui_dataset_get_value(new_ds, "username");
170                 home = dfui_dataset_get_value(new_ds, "home");
171                 gecos = dfui_dataset_get_value(new_ds, "gecos");
172                 shell = dfui_dataset_get_value(new_ds, "shell");
173                 passwd_1 = dfui_dataset_get_value(new_ds, "passwd_1");
174                 passwd_2 = dfui_dataset_get_value(new_ds, "passwd_2");
175                 uid = dfui_dataset_get_value(new_ds, "uid");
176                 group = dfui_dataset_get_value(new_ds, "group");
177                 groups = dfui_dataset_get_value(new_ds, "groups");
178
179                 if (strlen(username) == 0) {
180                         inform(a->c, _("You must enter a username."));
181                         done = 0;
182                 } else if (strcmp(passwd_1, passwd_2) != 0) {
183                         /* Passwords don't match; tell the user. */
184                         inform(a->c, _("The passwords do not match."));
185                         done = 0;
186                 } else if (!assert_clean(a->c, _("Username"), username, PW_NOT_ALLOWED) ||
187                     !assert_clean(a->c, _("Real Name"), gecos, GECOS_NOT_ALLOWED) ||
188                     !assert_clean(a->c, _("Password"), passwd_1, PW_NOT_ALLOWED) ||
189                     !assert_clean(a->c, _("Shell"), shell, FILENAME_NOT_ALLOWED) ||
190                     !assert_clean(a->c, _("Home Directory"), home, FILENAME_NOT_ALLOWED) ||
191                     !assert_clean(a->c, _("User ID"), uid, PW_NOT_ALLOWED) ||
192                     !assert_clean(a->c, _("Login Group"), group, PW_NOT_ALLOWED) ||
193                     !assert_clean(a->c, _("Group Memberships"), groups, MEMBERSHIPS_NOT_ALLOWED)) {
194                         done = 0;
195                 } else if (!is_program("%s%s", a->os_root, shell) &&
196                     strcmp(shell, "/nonexistent") != 0) {
197                         inform(a->c, _("Chosen shell does not exist on the system."));
198                         done = 0;
199                 } else {
200                         cmds = commands_new();
201
202                         command_add(cmds, "%s%s %smnt/ /%s useradd "
203                             "'%s' %s%s %s%s -c \"%s\" %s%s -s %s %s%s %s",
204                             a->os_root, cmd_name(a, "CHROOT"),
205                             a->os_root, cmd_name(a, "PW"),
206                             username,
207                             strlen(uid) == 0 ? "" : "-u ", uid,
208                             strlen(group) == 0 ? "" : "-g ", group,
209                             gecos,
210                             strlen(home) == 0 ? "" : "-d ", home,
211                             shell,
212                             strlen(groups) == 0 ? "" : "-G ", groups,
213                             (strlen(home) == 0 || !is_dir(home)) ?
214                             "-m -k /usr/share/skel" : "");
215
216                         cmd = command_add(cmds, "%s%s '%s' | "
217                             "%s%s %smnt/ /%s usermod '%s' -h 0",
218                             a->os_root, cmd_name(a, "ECHO"),
219                             passwd_1,
220                             a->os_root, cmd_name(a, "CHROOT"),
221                             a->os_root, cmd_name(a, "PW"),
222                             username);
223                         command_set_desc(cmd, _("Setting password..."));
224
225                         if (commands_execute(a, cmds)) {
226                                 inform(a->c, _("User `%s' was added."), username);
227                                 done = 1;
228                         } else {
229                                 inform(a->c, _("User was not successfully added."));
230                                 done = 0;
231                         }
232
233                         commands_free(cmds);
234                 }
235
236                 dfui_response_free(r);
237         }
238
239         dfui_form_free(f);
240 }
241
242 void
243 fn_root_passwd(struct i_fn_args *a)
244 {
245         struct dfui_dataset *ds, *new_ds;
246         struct dfui_form *f;
247         struct dfui_response *r;
248         struct commands *cmds;
249         struct command *cmd;
250         const char *root_passwd_1, *root_passwd_2;
251         int done = 0;
252
253         f = dfui_form_create(
254             "root_passwd",
255             _("Set Root Password"),
256             _("Here you can set the super-user (root) password.\n\n"
257             "Note: root's new password will appear in the install log. "
258             "If this is a problem, please set root's password manually "
259             "after rebooting into the installed system instead."),
260             "",
261
262             "f", "root_passwd_1", _("Root password"),
263             _("Enter the root password you would like to use"), "",
264             "p", "obscured", "true",
265             "f", "root_passwd_2", _("Root password again"),
266             _("Enter the root password again to confirm"), "",
267             "p", "obscured", "true",
268
269             "a", "ok", _("Accept and Set Password"), "", "",
270             "a", "cancel", _("Return to Configure Menu"), "", "",
271             "p", "accelerator", "ESC",
272
273             NULL
274         );
275
276         ds = dfui_dataset_new();
277         dfui_dataset_celldata_add(ds, "root_passwd_1", "");
278         dfui_dataset_celldata_add(ds, "root_passwd_2", "");
279         dfui_form_dataset_add(f, ds);
280
281         while (!done) {
282                 if (!dfui_be_present(a->c, f, &r))
283                         abort_backend();
284
285                 if (strcmp(dfui_response_get_action_id(r), "ok") == 0) {
286                         new_ds = dfui_dataset_dup(dfui_response_dataset_get_first(r));
287                         dfui_form_datasets_free(f);
288                         dfui_form_dataset_add(f, new_ds);
289
290                         /*
291                          * Fetch form field values.
292                          */
293
294                         root_passwd_1 = dfui_dataset_get_value(new_ds, "root_passwd_1");
295                         root_passwd_2 = dfui_dataset_get_value(new_ds, "root_passwd_2");
296
297                         if (!assert_clean(a->c, _("Root password"), root_passwd_1, PW_NOT_ALLOWED)) {
298                                 done = 0;
299                         } else if (strlen(root_passwd_1) == 0 && strlen(root_passwd_2) == 0) {
300                                 done = 0;
301                         } else if (strcmp(root_passwd_1, root_passwd_2) == 0) {
302                                 /*
303                                  * Passwords match, so set the root password.
304                                  */
305                                 cmds = commands_new();
306                                 cmd = command_add(cmds, "%s%s '%s' | "
307                                     "%s%s %smnt/ /%s usermod root -h 0",
308                                     a->os_root, cmd_name(a, "ECHO"),
309                                     root_passwd_1,
310                                     a->os_root, cmd_name(a, "CHROOT"),
311                                     a->os_root, cmd_name(a, "PW"));
312                                 command_set_desc(cmd, _("Setting password..."));
313                                 if (commands_execute(a, cmds)) {
314                                         inform(a->c, _("The root password has been changed."));
315                                         done = 1;
316                                 } else {
317                                         inform(a->c, _("An error occurred when "
318                                             "setting the root password."));
319                                         done = 0;
320                                 }
321                                 commands_free(cmds);
322                         } else {
323                                 /*
324                                  * Passwords don't match - tell the user, let them try again.
325                                  */
326                                 inform(a->c, _("The passwords do not match."));
327                                 done = 0;
328                         }
329                 } else {
330                         /*
331                          * Cancelled by user
332                          */
333                         done = 1;
334                 }
335
336                 dfui_response_free(r);
337         }
338
339         dfui_form_free(f);
340 }
341
342 void
343 fn_get_passphrase(struct i_fn_args *a)
344 {
345         struct dfui_dataset *ds, *new_ds;
346         struct dfui_form *f;
347         struct dfui_response *r;
348         const char *passphrase_1, *passphrase_2;
349         int fd;
350         int done = 0;
351
352         f = dfui_form_create(
353             "crypt_passphrase",
354             _("Set Passphrase For Encryption"),
355             _("Please specify the passphrase to be used for the encrypted "
356             "filesystems.\n\n"
357             "Please note that in the LiveCD environment the keymap is set to "
358             "\"US ISO\". "
359             "If you prefer a different keymap for entering the passphrase "
360             "here, you will need to set it manually using kbdcontrol(1)."),
361             "",
362
363             "f", "passphrase_1", _("Passphrase"),
364             _("Enter the passphrase you would like to use for encryption"), "",
365             "p", "obscured", "true",
366             "f", "passphrase_2", _("Passphrase again"),
367             _("Enter the passphrase again to confirm"), "",
368             "p", "obscured", "true",
369
370             "a", "ok", _("Accept and Set Passphrase"), "", "",
371             "p", "accelerator", "ESC",
372
373             NULL
374         );
375
376         ds = dfui_dataset_new();
377         dfui_dataset_celldata_add(ds, "passphrase_1", "");
378         dfui_dataset_celldata_add(ds, "passphrase_2", "");
379         dfui_form_dataset_add(f, ds);
380
381         while (!done) {
382                 if (!dfui_be_present(a->c, f, &r))
383                         abort_backend();
384
385                 if (strcmp(dfui_response_get_action_id(r), "ok") == 0) {
386                         new_ds = dfui_dataset_dup(dfui_response_dataset_get_first(r));
387                         dfui_form_datasets_free(f);
388                         dfui_form_dataset_add(f, new_ds);
389
390                         /*
391                          * Fetch form field values.
392                          */
393
394                         passphrase_1 = dfui_dataset_get_value(new_ds, "passphrase_1");
395                         passphrase_2 = dfui_dataset_get_value(new_ds, "passphrase_2");
396
397                         if (strlen(passphrase_1) == 0 && strlen(passphrase_2) == 0) {
398                                 done = 0;
399                         } else if (strcmp(passphrase_1, passphrase_2) == 0) {
400                                 /*
401                                  * Passphrases match, write it out.
402                                  */
403                                 fd = open("/tmp/t1", O_RDWR | O_CREAT | O_TRUNC,
404                                     S_IRUSR);
405                                 if (fd != -1) {
406                                         write(fd, passphrase_1, strlen(passphrase_1));
407                                         close(fd);
408                                         done = 1;
409                                 } else {
410                                         inform(a->c, _("write() error"));
411                                         done = 0;
412                                 }
413                         } else {
414                                 /*
415                                  * Passphrases don't match, tell the user,
416                                  * let them try again.
417                                  */
418                                 inform(a->c, _("The passphrases do not match."));
419                                 done = 0;
420                         }
421                 }
422
423                 dfui_response_free(r);
424         }
425
426         dfui_form_free(f);
427 }
428
429 void
430 fn_install_packages(struct i_fn_args *a)
431 {
432         FILE *pfp;
433         struct commands *cmds;
434         struct dfui_celldata *cd;
435         struct dfui_dataset *ds;
436         struct dfui_field *fi;
437         struct dfui_form *f;
438         struct dfui_response *r;
439         char command[256];
440         char pkg_name[256];
441         char msg_buf[1][1024];
442         struct aura_dict *seen;
443
444         snprintf(msg_buf[0], sizeof(msg_buf[0]),
445             _("Select optional software packages that you want "
446             "installed on this system.  This form lists only the "
447             "software packages installed on the LiveCD; thousands "
448             "more are available via the internet once %s "
449             "is installed."),
450             OPERATING_SYSTEM_NAME);
451
452         f = dfui_form_create(
453             "install_packages",
454             _("Install Packages"),
455             msg_buf[0],
456             "",
457
458             "p", "special", "dfinstaller_install_packages",
459
460             NULL
461         );
462
463         ds = dfui_dataset_new();
464         snprintf(command, 256, "ls %svar/db/pkg", a->os_root);
465         if ((pfp = popen(command, "r")) != NULL) {
466                 while (fgets(pkg_name, 255, pfp) != NULL) {
467                         while (strlen(pkg_name) > 0 &&
468                                isspace(pkg_name[strlen(pkg_name) - 1])) {
469                                 pkg_name[strlen(pkg_name) - 1] = '\0';
470                         }
471                         fi = dfui_form_field_add(f, pkg_name,
472                             dfui_info_new(pkg_name, "", ""));
473                         dfui_field_property_set(fi, "control", "checkbox");
474                         dfui_dataset_celldata_add(ds,
475                             pkg_name, "Y");
476                 }
477                 pclose(pfp);
478         }
479         dfui_form_dataset_add(f, ds);
480
481         dfui_form_action_add(f, "ok",
482             dfui_info_new(_("Accept and Install"), "", ""));
483         dfui_form_action_add(f, "cancel",
484             dfui_info_new(_("Return to Configure Menu"), "", ""));
485
486         if (!dfui_be_present(a->c, f, &r))
487                 abort_backend();
488
489         if (strcmp(dfui_response_get_action_id(r), "ok") == 0) {
490                 cmds = commands_new();
491                 seen = aura_dict_new(23, AURA_DICT_HASH);
492
493                 cd = dfui_dataset_celldata_get_first(dfui_response_dataset_get_first(r));
494
495                 while (cd != NULL) {
496                         strlcpy(pkg_name, dfui_celldata_get_field_id(cd), 256);
497                         if (!strcasecmp(dfui_celldata_get_value(cd), "Y")) {
498                                 if (!pkg_copy(a, cmds, pkg_name, seen)) {
499                                         inform(a->c, _("Couldn't install package `%s'."), pkg_name);
500                                         break;
501                                 }
502                         }
503                         cd = dfui_celldata_get_next(cd);
504                 }
505
506                 if (!commands_execute(a, cmds)) {
507                         inform(a->c, _("Packages were not fully installed."));
508                 } else {
509                         inform(a->c, _("Packages were successfully installed!"));
510                 }
511
512                 aura_dict_free(seen);
513                 commands_free(cmds);
514         }
515
516         dfui_form_free(f);
517         dfui_response_free(r);
518 }
519
520 void
521 fn_remove_packages(struct i_fn_args *a)
522 {
523         FILE *pfp;
524         struct commands *cmds;
525         struct dfui_celldata *cd;
526         struct dfui_dataset *ds;
527         struct dfui_field *fi;
528         struct dfui_form *f;
529         struct dfui_response *r;
530         char command[256];
531         char pkg_name[256];
532         struct aura_dict *seen;
533
534         f = dfui_form_create(
535             "remove_packages",
536             _("Remove Packages"),
537             _("Select the installed software packages that you want "
538             "removed from this system."),
539             "",
540
541             "p", "special", "dfinstaller_remove_packages",
542
543             NULL
544         );
545
546         ds = dfui_dataset_new();
547         snprintf(command, 256, "ls %smnt/var/db/pkg", a->os_root);
548         if ((pfp = popen(command, "r")) != NULL) {
549                 while (fgets(pkg_name, 255, pfp)) {
550                         pkg_name[strlen(pkg_name) - 1] = '\0';
551                         fi = dfui_form_field_add(f, pkg_name,
552                             dfui_info_new(pkg_name, "", ""));
553                         dfui_field_property_set(fi, "control", "checkbox");
554                         dfui_dataset_celldata_add(ds,
555                             pkg_name, "N");
556                 }
557                 pclose(pfp);
558         }
559         dfui_form_dataset_add(f, ds);
560
561         dfui_form_action_add(f, "ok",
562             dfui_info_new(_("Accept and Remove"), "", ""));
563         dfui_form_action_add(f, "cancel",
564             dfui_info_new(_("Return to Configure Menu"), "", ""));
565
566         if (!dfui_be_present(a->c, f, &r))
567                 abort_backend();
568
569         if (strcmp(dfui_response_get_action_id(r), "ok") == 0) {
570                 cmds = commands_new();
571                 seen = aura_dict_new(23, AURA_DICT_HASH);
572
573                 cd = dfui_dataset_celldata_get_first(dfui_response_dataset_get_first(r));
574
575                 while (cd != NULL) {
576                         strlcpy(pkg_name, dfui_celldata_get_field_id(cd), 256);
577                         if (!strcasecmp(dfui_celldata_get_value(cd), "Y")) {
578                                 if (!pkg_remove(a, cmds, pkg_name, seen)) {
579                                         inform(a->c, _("Couldn't remove package `%s'."), pkg_name);
580                                         break;
581                                 }
582                         }
583                         cd = dfui_celldata_get_next(cd);
584                 }
585
586                 if (!commands_execute(a, cmds)) {
587                         inform(a->c, _("Packages were not fully removed."));
588                 } else {
589                         inform(a->c, _("Packages were successfully removed."));
590                 }
591
592                 aura_dict_free(seen);
593                 commands_free(cmds);
594         }
595
596         dfui_form_free(f);
597         dfui_response_free(r);
598 }
599
600 /** LIVECD UTILITIES FUNCTIONS **/
601
602 /*
603  * String returned by this function must be deallocated by the caller.
604  */
605 char *
606 fn_select_file(const char *title, const char *desc, const char *help, const char *cancel,
607                const char *dir, const char *ext, const struct i_fn_args *a)
608 {
609         DIR *d;
610         struct dfui_form *f;
611         struct dfui_action *k;
612         struct dfui_response *r;
613         struct dirent *de;
614         char *s;
615         struct aura_dict *dict;
616         char *rk;
617         size_t rk_len;
618
619         f = dfui_form_create(
620             "select_file",
621             title, desc, help,
622             "p", "role", "menu",
623             NULL
624         );
625
626         dict = aura_dict_new(1, AURA_DICT_SORTED_LIST);
627         d = opendir(dir);
628         while ((de = readdir(d)) != NULL) {
629                 if (strcmp(de->d_name, ".") == 0 ||
630                     strcmp(de->d_name, "..") == 0 ||
631                     strstr(de->d_name, ext) == NULL)
632                         continue;
633                 aura_dict_store(dict, de->d_name, strlen(de->d_name) + 1, "", 1);
634         }
635         closedir(d);
636
637         aura_dict_rewind(dict);
638         while (!aura_dict_eof(dict)) {
639                 aura_dict_get_current_key(dict, (void **)&rk, &rk_len),
640                 dfui_form_action_add(f, rk,
641                     dfui_info_new(rk, "", ""));
642                 aura_dict_next(dict);
643         }
644         aura_dict_free(dict);
645
646         k = dfui_form_action_add(f, "cancel",
647             dfui_info_new(cancel, "", ""));
648         dfui_action_property_set(k, "accelerator", "ESC");
649
650         if (!dfui_be_present(a->c, f, &r))
651                 abort_backend();
652
653         s = aura_strdup(dfui_response_get_action_id(r));
654
655         dfui_form_free(f);
656         dfui_response_free(r);
657
658         return(s);
659 }
660
661 void
662 fn_set_kbdmap(struct i_fn_args *a)
663 {
664         struct commands *cmds;
665         char *s;
666         char filename[256], keymapname[256];
667
668         s = fn_select_file(_("Select Keyboard Map"),
669             _("Select a keyboard map appropriate to your keyboard layout."),
670             "", _("Return to Utilities Menu"), "/usr/share/syscons/keymaps",
671             ".kbd", a);
672
673         if (strcmp(s, "cancel") != 0) {
674                 cmds = commands_new();
675                 command_add(cmds, "%s%s -l "
676                     "/usr/share/syscons/keymaps/%s < /dev/ttyv0",
677                     a->os_root, cmd_name(a, "KBDCONTROL"),
678                     s);
679                 if (commands_execute(a, cmds)) {
680                         snprintf(filename, 256, "/usr/share/syscons/keymaps/%s", s);
681                         snprintf(keymapname, 256, filename_noext(basename(filename)));
682                         config_var_set(rc_conf, "keymap", keymapname);
683                 } else {
684                         inform(a->c, _("Keyboard map not successfully set."));
685                 }
686                 commands_free(cmds);
687         }
688
689         free(s);
690 }
691
692 void
693 fn_set_vidfont(struct i_fn_args *a)
694 {
695         struct commands *cmds;
696         char *s;
697         char filename[256], variable[256], fontname[256];
698         int by = 0;
699
700
701         s = fn_select_file(_("Select Console Font"),
702             _("Select a font appropriate to your video monitor and language."),
703             "", _("Return to Utilities Menu"), "/usr/share/syscons/fonts",
704             ".fnt", a);
705
706         if (strcmp(s, "cancel") != 0) {
707                 cmds = commands_new();
708                 command_add(cmds, "%s%s -f "
709                     "/usr/share/syscons/fonts/%s < /dev/ttyv0",
710                     a->os_root, cmd_name(a, "VIDCONTROL"),
711                     s);
712                 if (commands_execute(a, cmds)) {
713                         if (strstr(s, "8x16") != NULL)
714                                 by = 16;
715                         else if (strstr(s, "8x14") != NULL)
716                                 by = 14;
717                         else
718                                 by = 8;
719
720                         snprintf(variable, 256, "font8x%d", by);
721                         snprintf(filename, 256, "/usr/share/syscons/fonts/%s", s);
722                         snprintf(fontname, 256, filename_noext(basename(filename)));
723                         config_var_set(rc_conf, variable, fontname);
724
725                 } else {
726                         inform(a->c, _("Video font not successfully set."));
727                 }
728                 commands_free(cmds);
729         }
730
731         free(s);
732 }
733
734 void
735 fn_set_scrnmap(struct i_fn_args *a)
736 {
737         struct commands *cmds;
738         char *s;
739         char filename[256], scrnmapname[256];
740
741         s = fn_select_file(_("Select Screen Map"),
742             _("Select a mapping for translating characters as they appear "
743             "on your video console screen."),
744             "", _("Return to Utilities Menu"), "/usr/share/syscons/scrnmaps",
745             ".scm", a);
746
747         if (strcmp(s, "cancel") != 0) {
748                 cmds = commands_new();
749                 command_add(cmds, "%s%s -l "
750                     "/usr/share/syscons/scrnmaps/%s < /dev/ttyv0",
751                     a->os_root, cmd_name(a, "VIDCONTROL"),
752                     s);
753                 if (commands_execute(a, cmds)) {
754                         snprintf(filename, 256, "/usr/share/syscons/scrnmaps/%s", s);
755                         snprintf(scrnmapname, 256, filename_noext(basename(filename)));
756                         config_var_set(rc_conf, "scrnmap", scrnmapname);
757                 } else {
758                         inform(a->c, _("Video font not successfully set."));
759                 }
760                 commands_free(cmds);
761         }
762         free(s);
763 }
764
765 void
766 fn_set_timezone(struct i_fn_args *a)
767 {
768         struct commands *cmds;
769         char *s = NULL;
770         char current_path[256], selection[256], temp[256];
771         int found_file = 0;
772
773         cmds = commands_new();
774
775         switch (dfui_be_present_dialog(a->c, _("Local or UTC (Greenwich Mean Time) clock"),
776             _("Yes|No"),
777             _("Is this machine's CMOS clock set to UTC?\n\n"
778             "If it is set to local time, or you don't know, please choose NO here!"))) {
779                 case 1:
780                         cmds = commands_new();
781                         command_add(cmds, "%s%s %s%setc/wall_cmos_clock",
782                             a->os_root, cmd_name(a, "TOUCH"),
783                             a->os_root, a->cfg_root);
784                         commands_execute(a, cmds);
785         }
786
787         snprintf(current_path, 256, "%s%susr/share/zoneinfo",
788             a->os_root, a->cfg_root);
789         while (!found_file) {
790                 if (s != NULL)
791                         free(s);
792                 s = fn_select_file(_("Select Time Zone"),
793                     _("Select a Time Zone appropriate to your physical location."),
794                     "", _("Return to Utilities Menu"), current_path,
795                     "", a);
796                 if (is_dir("%s/%s", current_path, s)) {
797                         snprintf(temp, 256, "%s/%s", current_path, s);
798                         strlcpy(current_path, temp, 256);
799                 } else {
800                         if (is_file("%s/%s", current_path, s)) {
801                                 snprintf(selection, 256, "%s/%s", current_path, s);
802                                 found_file = 1;
803                         }
804                         if (strcmp(s, "cancel") == 0) {
805                                 strlcpy(selection, "cancel", 256);
806                                 found_file = 1;
807                         }
808                 }
809         }
810         free(s);
811
812         if (strcmp(selection, "cancel") != 0) {
813                 command_add(cmds, "%s%s %s %s%setc/localtime",
814                     a->os_root, cmd_name(a, "CP"),
815                     selection,
816                     a->os_root, a->cfg_root);
817                 if (commands_execute(a, cmds))
818                         inform(a->c, _("The Time Zone has been set to %s."), selection);
819         }
820         commands_free(cmds);
821 }
822
823 void
824 fn_assign_datetime(struct i_fn_args *a)
825 {
826         struct commands *cmds;
827         struct dfui_dataset *ds, *new_ds;
828         struct dfui_form *f;
829         struct dfui_response *r;
830         struct tm *tp;
831         char temp[256];
832         int year, month, dayofmonth, hour, minutes;
833         int valid = 1;
834         time_t now;
835
836         now = time(NULL);
837         tp = localtime(&now);
838
839         f = dfui_form_create(
840             "set_datetime",
841             _("Set Time/Date"),
842             _("Enter the current time and date."),
843             "",
844
845             "f", "year", _("Enter year"),
846             _("Enter the current year (e.g. `2004')"), "",
847             "f", "month", _("Month"),
848             _("Enter the current month (e.g. `07')"), "",
849             "f", "dayofmonth", "dayofmonth",
850             _("Enter the current day of month (e.g. `30')"), "",
851             "f", "hour", "hour",
852             _("Enter the current hour (e.g. `07')"), "",
853             "f", "minutes", "minutes",
854             _("Enter the current minutes (e.g. `59')"), "",
855
856             "a", "ok", _("OK"), "", "",
857             "a", "cancel", _("Cancel"), "", "",
858             "p", "accelerator", "ESC",
859
860             NULL
861         );
862
863         ds = dfui_dataset_new();
864         snprintf(temp, 256, "%i", (tp->tm_year+1900));
865         dfui_dataset_celldata_add(ds, "year", temp);
866         snprintf(temp, 256, "%i", (tp->tm_mon+1));
867         dfui_dataset_celldata_add(ds, "month", temp);
868         snprintf(temp, 256, "%i", tp->tm_mday);
869         dfui_dataset_celldata_add(ds, "dayofmonth", temp);
870         snprintf(temp, 256, "%i", tp->tm_hour);
871         dfui_dataset_celldata_add(ds, "hour", temp);
872         snprintf(temp, 256, "%i", tp->tm_min);
873         dfui_dataset_celldata_add(ds, "minutes", temp);
874         dfui_form_dataset_add(f, ds);
875
876         if (!dfui_be_present(a->c, f, &r))
877                 abort_backend();
878
879         if (strcmp(dfui_response_get_action_id(r), "ok") == 0) {
880                 new_ds = dfui_response_dataset_get_first(r);
881
882                 if ((year = atoi(dfui_dataset_get_value(new_ds, "year"))) <= 0)
883                         valid = 0;
884                 month = atoi(dfui_dataset_get_value(new_ds, "month"));
885                 if (month < 1 || month > 12)
886                         valid = 0;
887                 dayofmonth = atoi(dfui_dataset_get_value(new_ds, "dayofmonth"));
888                 if (dayofmonth < 1 || dayofmonth > 31)
889                         valid = 0;
890                 hour = atoi(dfui_dataset_get_value(new_ds, "hour"));
891                 if (hour < 0 || hour > 23)
892                         valid = 0;
893                 minutes = atoi(dfui_dataset_get_value(new_ds, "minutes"));
894                 if (minutes < 0 || minutes > 59)
895                         valid = 0;
896
897                 if (valid) {
898                         cmds = commands_new();
899                         command_add(cmds, "%s%s -n %04d%02d%02d%02d%02d",
900                             a->os_root, cmd_name(a, "DATE"),
901                             year, month, dayofmonth, hour, minutes);
902                         if (commands_execute(a, cmds)) {
903                                 inform(a->c, _("The date and time have been set."));
904                         }
905                         commands_free(cmds);
906                 } else {
907                         inform(a->c, _("Please enter numbers within acceptable ranges "
908                                 "for year, month, day of month, hour, and minute."));
909                 }
910         }
911 }
912
913 void
914 fn_assign_hostname_domain(struct i_fn_args *a)
915 {
916         struct dfui_form *f;
917         struct dfui_response *r;
918         struct dfui_dataset *ds, *new_ds;
919         struct config_vars *resolv_conf;
920         const char *domain, *hostname;
921         char *fqdn;
922
923         f = dfui_form_create(
924             "set_hostname_domain",
925             _("Set Hostname/Domain"),
926             _("Please enter this machine's hostname and domain name."),
927             "",
928
929             "f", "hostname", _("Hostname"),
930             _("Enter the Hostname (e.g. `machine')"), "",
931             "f", "domain", _("Domain"),
932             _("Enter the Domain Name (e.g. `network.lan')"), "",
933
934             "a", "ok", _("OK"), "", "",
935             "a", "cancel", _("Cancel"), "", "",
936             "p", "accelerator", "ESC",
937
938             NULL
939         );
940
941         ds = dfui_dataset_new();
942         dfui_dataset_celldata_add(ds, "hostname", "");
943         dfui_dataset_celldata_add(ds, "domain", "");
944         dfui_form_dataset_add(f, ds);
945
946         if (!dfui_be_present(a->c, f, &r))
947                 abort_backend();
948
949         if (strcmp(dfui_response_get_action_id(r), "ok") == 0) {
950                 new_ds = dfui_response_dataset_get_first(r);
951
952                 hostname = dfui_dataset_get_value(new_ds, "hostname");
953                 domain = dfui_dataset_get_value(new_ds, "domain");
954                 if (strlen(domain) == 0)
955                         asprintf(&fqdn, "%s", hostname);
956                 else
957                         asprintf(&fqdn, "%s.%s", hostname, domain);
958
959                 resolv_conf = config_vars_new();
960
961                 config_var_set(rc_conf, "hostname", fqdn);
962                 config_var_set(resolv_conf, "search", domain);
963                 config_vars_write(resolv_conf, CONFIG_TYPE_RESOLV,
964                     "%s%setc/resolv.conf", a->os_root, a->cfg_root);
965
966                 config_vars_free(resolv_conf);
967
968                 free(fqdn);
969         }
970
971         dfui_form_free(f);
972         dfui_response_free(r);
973 }
974
975 void
976 fn_assign_ip(struct i_fn_args *a)
977 {
978         FILE *p;
979         struct commands *cmds;
980         struct command *cmd;
981         struct config_vars *resolv_conf;
982         struct dfui_dataset *ds, *new_ds;
983         struct dfui_form *f;
984         struct dfui_action *k;
985         struct dfui_response *r;
986         const char *domain, *hostname;
987         const char *interface_ip, *interface_netmask, *defaultrouter, *dns_resolver;
988         char *string, *string1;
989         char *word;
990         char interface[256];
991         char line[256];
992         int write_config = 0;
993
994         /*
995          * Get interface list.
996          */
997         p = popen("/sbin/ifconfig -l", "r");
998         /* XXX it's possible (though extremely unlikely) this will fail. */
999         while (fgets(line, 255, p) != NULL)
1000                 line[strlen(line) - 1] = '\0';
1001
1002         pclose(p);
1003
1004         f = dfui_form_create(
1005             "assign_ip",
1006             _("Assign IP Address"),
1007             _("Please select which interface you would like to configure:"),
1008             "",
1009             "p",        "role", "menu",
1010             NULL
1011         );
1012
1013         /* Loop through array. */
1014         word = strtok(line, " \t");
1015         while (word != NULL) {
1016                 dfui_form_action_add(f, word,
1017                     dfui_info_new(word, "", ""));
1018                 word = strtok(NULL, " ");
1019         }
1020
1021         k = dfui_form_action_add(f, "cancel",
1022             dfui_info_new("Cancel", "", ""));
1023         dfui_action_property_set(k, "accelerator", "ESC");
1024
1025         if (!dfui_be_present(a->c, f, &r))
1026                 abort_backend();
1027
1028         if (strcmp(dfui_response_get_action_id(r), "cancel") == 0) {
1029                 dfui_form_free(f);
1030                 dfui_response_free(r);
1031                 return;
1032         }
1033
1034         strlcpy(interface, dfui_response_get_action_id(r), 256);
1035
1036         resolv_conf = config_vars_new();
1037
1038         switch (dfui_be_present_dialog(a->c, _("Use DHCP?"),
1039             _("Use DHCP|Configure Manually"),
1040             _("DHCP allows the interface to automatically obtain "
1041             "an IP address from a nearby DHCP server.\n\n"
1042             "Would you like to enable DHCP for %s?"), interface)) {
1043         case 1:
1044                 asprintf(&string, "ifconfig_%s", interface);
1045
1046                 cmds = commands_new();
1047                 cmd = command_add(cmds, "%s%s dhclient",
1048                     a->os_root, cmd_name(a, "KILLALL"));
1049                 command_set_failure_mode(cmd, COMMAND_FAILURE_IGNORE);
1050                 command_add(cmds, "%s%s %s",
1051                     a->os_root, cmd_name(a, "DHCLIENT"),
1052                     interface);
1053                 if (commands_execute(a, cmds)) {
1054                         /* XXX sleep(3); */
1055                         show_ifconfig(a->c, interface);
1056                         write_config = 1;
1057                 } else {
1058                         switch (dfui_be_present_dialog(a->c, _("DHCP Failure"),
1059                             _("Yes|No"),
1060                             _("Warning: could not enable dhclient for %s.\n\n"
1061                               "Write the corresponding settings to rc.conf "
1062                               "anyway?"), interface)) {
1063                         case 1:
1064                                 write_config = 1;
1065                                 break;
1066                         case 2:
1067                                 write_config = 0;
1068                                 break;
1069                         default:
1070                                 abort_backend();
1071                         }
1072                 }
1073                 commands_free(cmds);
1074                 config_var_set(rc_conf, string, "DHCP");
1075                 free(string);
1076                 break;
1077         case 2:
1078                 dfui_form_free(f);
1079                 dfui_response_free(r);
1080                 f = dfui_form_create(
1081                     "assign_ip",
1082                     _("Assign IP Address"),
1083                     _("Configuring Interface:"),
1084                     "",
1085
1086                     "f", "interface_ip", _("IP Address"),
1087                     _("Enter the IP Address you would like to use"), "",
1088                     "f", "interface_netmask",   _("Netmask"),
1089                     _("Enter the netmask of the IP address"), "",
1090                     "f", "defaultrouter", _("Default Router"),
1091                     _("Enter the IP address of the default router"), "",
1092                     "f", "dns_resolver", _("Primary DNS Server"),
1093                     _("Enter the IP address of primary DNS Server"), "",
1094                     "f", "hostname", _("Hostname"),
1095                     _("Enter the Hostname"), "",
1096                     "f", "domain", _("Domain"),
1097                     _("Enter the Domain Name"), "",
1098
1099                     "a", "ok", _("Configure Interface"),
1100                     "", "",
1101                     "a", "cancel", _("Return to Utilities Menu"),
1102                     "", "",
1103                     "p", "accelerator", "ESC",
1104
1105                     NULL
1106                 );
1107
1108                 ds = dfui_dataset_new();
1109                 dfui_dataset_celldata_add(ds, "interface_netmask", "");
1110                 dfui_dataset_celldata_add(ds, "defaultrouter", "");
1111                 dfui_dataset_celldata_add(ds, "dns_resolver", "");
1112                 dfui_dataset_celldata_add(ds, "hostname", "");
1113                 dfui_dataset_celldata_add(ds, "domain", "");
1114                 dfui_dataset_celldata_add(ds, "interface_ip", "");
1115                 dfui_form_dataset_add(f, ds);
1116
1117                 if (!dfui_be_present(a->c, f, &r))
1118                         abort_backend();
1119
1120                 if (strcmp(dfui_response_get_action_id(r), "ok") == 0) {
1121                         new_ds = dfui_response_dataset_get_first(r);
1122
1123                         interface_ip = dfui_dataset_get_value(new_ds, "interface_ip");
1124                         interface_netmask = dfui_dataset_get_value(new_ds, "interface_netmask");
1125                         defaultrouter = dfui_dataset_get_value(new_ds, "defaultrouter");
1126                         dns_resolver = dfui_dataset_get_value(new_ds, "dns_resolver");
1127                         hostname = dfui_dataset_get_value(new_ds, "hostname");
1128                         domain = dfui_dataset_get_value(new_ds, "domain");
1129
1130                         asprintf(&string, "ifconfig_%s", interface);
1131                         asprintf(&string1, "inet %s netmask %s",
1132                             interface_ip, interface_netmask);
1133
1134                         cmds = commands_new();
1135                         command_add(cmds, "%s%s %s %s netmask %s",
1136                             a->os_root, cmd_name(a, "IFCONFIG"),
1137                             interface, interface_ip, interface_netmask);
1138                         command_add(cmds, "%s%s add default %s",
1139                             a->os_root, cmd_name(a, "ROUTE"),
1140                             defaultrouter);
1141
1142                         if (commands_execute(a, cmds)) {
1143                                 /* XXX sleep(3); */
1144                                 show_ifconfig(a->c, interface);
1145                                 write_config = 1;
1146                         } else {
1147                                 switch (dfui_be_present_dialog(a->c,
1148                                     _("ifconfig Failure"),
1149                                     _("Yes|No"),
1150                                     _("Warning: could not assign IP address "
1151                                       "or default gateway.\n\n"
1152                                       "Write the corresponding settings to "
1153                                       "rc.conf anyway?"))) {
1154                                 case 1:
1155                                         write_config = 1;
1156                                         break;
1157                                 case 2:
1158                                         write_config = 0;
1159                                         break;
1160                                 default:
1161                                         abort_backend();
1162                                 }
1163                         }
1164                         commands_free(cmds);
1165
1166                         config_var_set(rc_conf, string, string1);
1167                         config_var_set(rc_conf, "defaultrouter", defaultrouter);
1168
1169                         free(string);
1170                         free(string1);
1171
1172                         asprintf(&string, "%s.%s", hostname, domain);
1173                         config_var_set(rc_conf, "hostname", string);
1174                         free(string);
1175
1176                         config_var_set(resolv_conf, "search", domain);
1177                         config_var_set(resolv_conf, "nameserver", dns_resolver);
1178                 }
1179                 break;
1180         default:
1181                 abort_backend();
1182         }
1183
1184         if (write_config) {
1185                 /*
1186                  * Save out changes to /etc/rc.conf and /etc/resolv.conf.
1187                  */
1188                 config_vars_write(resolv_conf, CONFIG_TYPE_RESOLV,
1189                     "%s%setc/resolv.conf", a->os_root, a->cfg_root);
1190         }
1191
1192         config_vars_free(resolv_conf);
1193
1194         dfui_form_free(f);
1195         dfui_response_free(r);
1196 }
1197
1198 static const char *
1199 yes_to_y(const char *value)
1200 {
1201         return(strcasecmp(value, "YES") == 0 ? "Y" : "N");
1202 }
1203
1204 void
1205 fn_select_services(struct i_fn_args *a)
1206 {
1207         struct dfui_dataset *ds;
1208         struct dfui_form *f;
1209         struct dfui_response *r;
1210
1211         if (!config_vars_read(a, rc_conf, CONFIG_TYPE_SH, "%s%setc/rc.conf",
1212                 a->os_root, a->cfg_root)) {
1213                 inform(a->c, _("Couldn't read %s%setc/rc.conf."),
1214                     a->os_root, a->cfg_root);
1215                 a->result = 0;
1216                 return;
1217         }
1218
1219         f = dfui_form_create(
1220             "select_services",
1221             _("Select Services"),
1222             _("Please select which services you would like started at boot time."),
1223             "",
1224
1225             "f", "syslogd", "syslogd",
1226                 _("System Logging Daemon"), "",
1227                 "p", "control", "checkbox",
1228             "f", "inetd", "inetd",
1229                 _("Internet Super-Server"), "",
1230                 "p", "control", "checkbox",
1231             "f", "named", "named",
1232                 _("BIND Name Server"), "",
1233                 "p", "control", "checkbox",
1234             "f", "ntpd", "ntpd",
1235                 _("Network Time Protocol Daemon"), "",
1236                 "p", "control", "checkbox",
1237             "f", "sshd", "sshd",
1238                 _("Secure Shell Daemon"), "",
1239                 "p", "control", "checkbox",
1240
1241             "a", "ok", _("Enable/Disable Services"),
1242                 "", "",
1243             "a", "cancel", _("Return to Utilities Menu"),
1244                 "", "",
1245                 "p", "accelerator", "ESC",
1246
1247             NULL
1248         );
1249
1250         ds = dfui_dataset_new();
1251         dfui_dataset_celldata_add(ds, "syslogd",
1252             yes_to_y(config_var_get(rc_conf, "syslogd_enable")));
1253         dfui_dataset_celldata_add(ds, "inetd",
1254             yes_to_y(config_var_get(rc_conf, "inetd_enable")));
1255         dfui_dataset_celldata_add(ds, "named",
1256             yes_to_y(config_var_get(rc_conf, "named_enable")));
1257         dfui_dataset_celldata_add(ds, "ntpd",
1258             yes_to_y(config_var_get(rc_conf, "ntpd_enable")));
1259         dfui_dataset_celldata_add(ds, "sshd",
1260             yes_to_y(config_var_get(rc_conf, "sshd_enable")));
1261         dfui_form_dataset_add(f, ds);
1262
1263         if (!dfui_be_present(a->c, f, &r))
1264                 abort_backend();
1265
1266         if (strcmp(dfui_response_get_action_id(r), "cancel") == 0) {
1267                 dfui_form_free(f);
1268                 dfui_response_free(r);
1269                 return;
1270         }
1271
1272         dfui_form_free(f);
1273         dfui_response_free(r);
1274 }
1275
1276 /*** NON-fn_ FUNCTIONS ***/
1277
1278 /*
1279  * Caller is responsible for deallocation.
1280  */
1281 static char *
1282 convert_tmpfs_options(char *line)
1283 {
1284         char *result, *word;
1285         int i;
1286
1287         result = malloc(256);
1288         result[0] = '\0';
1289
1290         for (; (word = strsep(&line, ",")) != NULL; ) {
1291                 if (word[0] == '-') {
1292                         /*
1293                          * Don't bother trying to honour the -C
1294                          * option, since we can't copy files from
1295                          * the right place anyway.
1296                          */
1297                         if (strcmp(word, "-C") != 0) {
1298                                 for (i = 0; word[i] != '\0'; i++) {
1299                                         if (word[i] == '=')
1300                                                 word[i] = ' ';
1301                                 }
1302                                 strlcat(result, word, 256);
1303                                 strlcat(result, " ", 256);
1304                         }
1305                 }
1306         }
1307
1308         return(result);
1309 }
1310
1311 /*
1312  * Uses ss->selected_{disk,slice} as the target system.
1313  */
1314 int
1315 mount_target_system(struct i_fn_args *a)
1316 {
1317         FILE *crypttab, *fstab;
1318         struct commands *cmds;
1319         struct command *cmd;
1320         struct subpartition *a_subpart;
1321         char name[256], device[256], mtpt[256], fstype[256], options[256];
1322         char *filename, line[256];
1323         const char *try_mtpt[5]  = {"/var", "/tmp", "/usr", "/home", NULL};
1324         char *word, *cvtoptions;
1325         int i;
1326
1327         /*
1328          * Mount subpartitions from this installation if they are
1329          * not already mounted.  Tricky, as we need to honour the
1330          * installation's loader.conf and fstab.
1331          */
1332         cmds = commands_new();
1333
1334         /*
1335          * First, unmount anything already mounted on /mnt.
1336          */
1337         unmount_all_under(a, cmds, "%smnt", a->os_root);
1338
1339         /*
1340          * Reset and clear out subpartitions so that system
1341          * can make a "dummy" subpart.
1342          */
1343         subpartitions_free(storage_get_selected_slice(a->s));
1344
1345         /*
1346          * Create a temporary dummy subpartition - that we
1347          * assume exists
1348          */
1349
1350         a_subpart = subpartition_new_ufs(storage_get_selected_slice(a->s),
1351             "/dummy", 0, 0, 0, 0, 0, 0);
1352
1353         /*
1354          * Mount the target's / and read its /etc/fstab.
1355          */
1356         if (use_hammer == 0) {
1357                 command_add(cmds, "%s%s %sdev/%s %s%s",
1358                     a->os_root, cmd_name(a, "MOUNT"),
1359                     a->os_root,
1360                     subpartition_get_device_name(a_subpart),
1361                     a->os_root, a->cfg_root);
1362                 cmd = command_add(cmds,
1363                     "%s%s -f %st2;"
1364                     "%s%s \"^[^#]\" %s%s/etc/crypttab >%st2",
1365                     a->os_root, cmd_name(a, "RM"), a->tmp,
1366                     a->os_root, cmd_name(a, "GREP"),
1367                     a->os_root, a->cfg_root, a->tmp);
1368                 command_set_failure_mode(cmd, COMMAND_FAILURE_IGNORE);
1369         } else {
1370                 command_add(cmds, "%s%s %sdev/%s %sboot",
1371                     a->os_root, cmd_name(a, "MOUNT"),
1372                     a->os_root,
1373                     subpartition_get_device_name(a_subpart),
1374                     a->os_root);
1375                 cmd = command_add(cmds,
1376                     "%s%s -f %st2;"
1377                     "%s%s \"^vfs\\.root\\.realroot=\" %sboot/loader.conf >%st2",
1378                     a->os_root, cmd_name(a, "RM"), a->tmp,
1379                     a->os_root, cmd_name(a, "GREP"),
1380                     a->os_root, a->tmp);
1381                 command_set_failure_mode(cmd, COMMAND_FAILURE_IGNORE);
1382         }
1383         if (!commands_execute(a, cmds)) {
1384                 commands_free(cmds);
1385                 return(0);
1386         }
1387         commands_free(cmds);
1388         cmds = commands_new();
1389
1390         if (use_hammer) {
1391                 struct stat sb = { .st_size = 0 };
1392                 stat("/tmp/t2", &sb);
1393                 if (sb.st_size > 0) {
1394                         command_add(cmds, "%s%s %sboot",
1395                             a->os_root, cmd_name(a, "UMOUNT"),
1396                             a->os_root);
1397                         fn_get_passphrase(a);
1398                         command_add(cmds,
1399                             "%s%s -d /tmp/t1 luksOpen %sdev/`%s%s \"^vfs\\.root\\.realroot=\" %st2 |"
1400                             "%s%s -Fhammer: '{print $2;}' |"
1401                             "%s%s -F: '{print $1;}'` root",
1402                             a->os_root, cmd_name(a, "CRYPTSETUP"),
1403                             a->os_root,
1404                             a->os_root, cmd_name(a, "GREP"),
1405                             a->tmp,
1406                             a->os_root, cmd_name(a, "AWK"),
1407                             a->os_root, cmd_name(a, "AWK"));
1408                         command_add(cmds,
1409                             "%s%s %sdev/mapper/root %s%s",
1410                             a->os_root, cmd_name(a, "MOUNT_HAMMER"),
1411                             a->os_root,
1412                             a->os_root, a->cfg_root);
1413                 } else {
1414                         command_add(cmds,
1415                             "%s%s %sdev/`%s%s \"^vfs\\.root\\.mountfrom\" %sboot/loader.conf |"
1416                             "%s%s -Fhammer: '{print $2;}' |"
1417                             "%s%s 's/\"//'` %s%s",
1418                             a->os_root, cmd_name(a, "MOUNT_HAMMER"),
1419                             a->os_root,
1420                             a->os_root, cmd_name(a, "GREP"),
1421                             a->os_root,
1422                             a->os_root, cmd_name(a, "AWK"),
1423                             a->os_root, cmd_name(a, "SED"),
1424                             a->os_root, a->cfg_root);
1425                         command_add(cmds, "%s%s %sboot",
1426                             a->os_root, cmd_name(a, "UMOUNT"),
1427                             a->os_root);
1428                 }
1429         }
1430         if (!commands_execute(a, cmds)) {
1431                 commands_free(cmds);
1432                 return(0);
1433         }
1434         commands_free(cmds);
1435         cmds = commands_new();
1436
1437         /*
1438          * Get rid of the dummy subpartition.
1439          */
1440         subpartitions_free(storage_get_selected_slice(a->s));
1441
1442         /*
1443          * See if an /etc/crypttab exists.
1444          */
1445         asprintf(&filename, "%s%s/etc/crypttab", a->os_root, a->cfg_root);
1446         crypttab = fopen(filename, "r");
1447         free(filename);
1448         if (crypttab != NULL) {
1449                 if (!use_hammer)
1450                         fn_get_passphrase(a);
1451                 while (fgets(line, 256, crypttab) != NULL) {
1452                         /*
1453                          * Parse the crypttab line.
1454                          */
1455                         if (first_non_space_char_is(line, '#'))
1456                                 continue;
1457                         if ((word = strtok(line, " \t")) == NULL)
1458                                 continue;
1459                         strlcpy(name, word, 256);
1460                         if (strcmp(name, "swap") == 0)
1461                                 continue;
1462                         if ((word = strtok(NULL, " \t")) == NULL)
1463                                 continue;
1464                         strlcpy(device, word, 256);
1465
1466                         command_add(cmds,
1467                             "%s%s -d /tmp/t1 luksOpen %s %s",
1468                             a->os_root, cmd_name(a, "CRYPTSETUP"),
1469                             device, name);
1470
1471                         continue;
1472                 }
1473                 fclose(crypttab);
1474         }
1475         if (!commands_execute(a, cmds)) {
1476                 commands_free(cmds);
1477                 return(0);
1478         }
1479         commands_free(cmds);
1480
1481         asprintf(&filename, "%s%s/etc/fstab", a->os_root, a->cfg_root);
1482         fstab = fopen(filename, "r");
1483         free(filename);
1484         if (fstab == NULL) {
1485                 inform(a->c, _("Filesystem table on installed system could not be read."));
1486                 cmds = commands_new();
1487                 command_add(cmds, "%s%s %s%s",
1488                     a->os_root, cmd_name(a, "UMOUNT"),
1489                     a->os_root, a->cfg_root);
1490                 if (!commands_execute(a, cmds)) {
1491                         inform(a->c, _("Warning: Installed system was not properly unmounted."));
1492                 }
1493                 commands_free(cmds);
1494                 return(0);
1495         }
1496
1497         cmds = commands_new();
1498
1499         while (fgets(line, 256, fstab) != NULL) {
1500                 /*
1501                  * Parse the fstab line.
1502                  */
1503                 if (first_non_space_char_is(line, '#'))
1504                         continue;
1505                 if ((word = strtok(line, " \t")) == NULL)
1506                         continue;
1507                 strlcpy(device, word, 256);
1508                 if ((word = strtok(NULL, " \t")) == NULL)
1509                         continue;
1510                 strlcpy(mtpt, word, 256);
1511                 if ((word = strtok(NULL, " \t")) == NULL)
1512                         continue;
1513                 strlcpy(fstype, word, 256);
1514                 if ((word = strtok(NULL, " \t")) == NULL)
1515                         continue;
1516                 strlcpy(options, word, 256);
1517
1518                 /*
1519                  * Now, if the mountpoint has /usr, /var, /tmp, or /home
1520                  * as a prefix, mount it under a->cfg_root.
1521                  */
1522                 for (i = 0; try_mtpt[i] != NULL; i++) {
1523                         if (strstr(mtpt, try_mtpt[i]) == mtpt) {
1524                                 /*
1525                                  * Don't mount it if it's optional.
1526                                  */
1527                                 if (strstr(options, "noauto") != NULL)
1528                                         continue;
1529
1530                                 /*
1531                                  * Don't mount it if device doesn't start
1532                                  * with /dev/ or /pfs and it isn't 'tmpfs'.
1533                                  */
1534                                 if (strstr(device, "/dev/") != NULL &&
1535                                      strstr(device, "/pfs/") != NULL &&
1536                                      strcmp(device, "tmpfs") != 0)
1537                                         continue;
1538
1539                                 /*
1540                                  * If the device is 'tmpfs', mount_tmpfs it instead.
1541                                  */
1542                                 if (strcmp(device, "tmpfs") == 0) {
1543                                         cvtoptions = convert_tmpfs_options(options);
1544                                         command_add(cmds,
1545                                             "%s%s %s tmpfs %s%s%s",
1546                                             a->os_root, cmd_name(a, "MOUNT_TMPFS"),
1547                                             cvtoptions, a->os_root, a->cfg_root, mtpt);
1548                                         free(cvtoptions);
1549                                 } else {
1550                                         if (use_hammer == 0) {
1551                                                 command_add(cmds,
1552                                                     "%s%s -o %s %s%s %s%s%s",
1553                                                     a->os_root, cmd_name(a, "MOUNT"),
1554                                                     options,
1555                                                     a->os_root, device, a->os_root,
1556                                                     a->cfg_root, mtpt);
1557                                         } else {
1558                                                 command_add(cmds,
1559                                                     "%s%s -o %s %s%s%s %s%s%s",
1560                                                     a->os_root, cmd_name(a, "MOUNT_NULL"),
1561                                                     options,
1562                                                     a->os_root, a->cfg_root, device,
1563                                                     a->os_root, a->cfg_root, mtpt);
1564                                         }
1565                                 }
1566                         }
1567                 }
1568         }
1569         fclose(fstab);
1570
1571         if (!commands_execute(a, cmds)) {
1572                 commands_free(cmds);
1573                 return(0);
1574         }
1575         commands_free(cmds);
1576
1577         return(1);
1578 }