install.c revision 63737
1/*
2 * The new sysinstall program.
3 *
4 * This is probably the last program in the `sysinstall' line - the next
5 * generation being essentially a complete rewrite.
6 *
7 * $FreeBSD: head/usr.sbin/sade/install.c 63737 2000-07-21 20:45:56Z jkh $
8 *
9 * Copyright (c) 1995
10 *	Jordan Hubbard.  All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 *    notice, this list of conditions and the following disclaimer,
17 *    verbatim and that no modifications are made prior to this
18 *    point in the file.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 *    notice, this list of conditions and the following disclaimer in the
21 *    documentation and/or other materials provided with the distribution.
22 *
23 * THIS SOFTWARE IS PROVIDED BY JORDAN HUBBARD ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL JORDAN HUBBARD OR HIS PETS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 */
36
37#include "sysinstall.h"
38#include <ctype.h>
39#include <sys/disklabel.h>
40#include <sys/errno.h>
41#include <sys/ioctl.h>
42#include <sys/fcntl.h>
43#include <sys/wait.h>
44#include <sys/param.h>
45#define MSDOSFS
46#include <sys/mount.h>
47#include <ufs/ufs/ufsmount.h>
48#include <msdosfs/msdosfsmount.h>
49#undef MSDOSFS
50#include <sys/stat.h>
51#include <sys/sysctl.h>
52#include <unistd.h>
53#include <termios.h>
54
55/* Hack for rsaref package add, which displays interactive license.
56 * Used by package.c
57 */
58int _interactiveHack;
59int FixItMode = 0;
60
61static void	create_termcap(void);
62static void	fixit_common(void);
63
64#define TERMCAP_FILE	"/usr/share/misc/termcap"
65
66static void	installConfigure(void);
67
68Boolean
69checkLabels(Boolean whinge, Chunk **rdev, Chunk **sdev, Chunk **udev, Chunk **vdev)
70{
71    Device **devs;
72    Boolean status;
73    Disk *disk;
74    Chunk *c1, *c2, *rootdev, *swapdev, *usrdev, *vardev;
75    int i;
76
77    /* Don't allow whinging if noWarn is set */
78    if (variable_get(VAR_NO_WARN))
79	whinge = FALSE;
80
81    status = TRUE;
82    *rdev = *sdev = *udev = *vdev = rootdev = swapdev = usrdev = vardev = NULL;
83
84    /* We don't need to worry about root/usr/swap if we're already multiuser */
85    if (!RunningAsInit)
86	return status;
87
88    devs = deviceFind(NULL, DEVICE_TYPE_DISK);
89    /* First verify that we have a root device */
90    for (i = 0; devs[i]; i++) {
91	if (!devs[i]->enabled)
92	    continue;
93	disk = (Disk *)devs[i]->private;
94	msgDebug("Scanning disk %s for root filesystem\n", disk->name);
95	if (!disk->chunks)
96	    msgFatal("No chunk list found for %s!", disk->name);
97	for (c1 = disk->chunks->part; c1; c1 = c1->next) {
98	    if (c1->type == freebsd) {
99		for (c2 = c1->part; c2; c2 = c2->next) {
100		    if (c2->type == part && c2->subtype != FS_SWAP && c2->private_data) {
101			if (!strcmp(((PartInfo *)c2->private_data)->mountpoint, "/")) {
102			    if (rootdev) {
103				if (whinge)
104				    msgConfirm("WARNING:  You have more than one root device set?!\n"
105					       "Using the first one found.");
106				continue;
107			    }
108			    else {
109				rootdev = c2;
110				if (isDebug())
111				    msgDebug("Found rootdev at %s!\n", rootdev->name);
112			    }
113			}
114			else if (!strcmp(((PartInfo *)c2->private_data)->mountpoint, "/usr")) {
115			    if (usrdev) {
116				if (whinge)
117				    msgConfirm("WARNING:  You have more than one /usr filesystem.\n"
118					       "Using the first one found.");
119				continue;
120			    }
121			    else {
122				usrdev = c2;
123				if (isDebug())
124				    msgDebug("Found usrdev at %s!\n", usrdev->name);
125			    }
126			}
127			else if (!strcmp(((PartInfo *)c2->private_data)->mountpoint, "/var")) {
128			    if (vardev) {
129				if (whinge)
130				    msgConfirm("WARNING:  You have more than one /var filesystem.\n"
131					       "Using the first one found.");
132				continue;
133			    }
134			    else {
135				vardev = c2;
136				if (isDebug())
137				    msgDebug("Found vardev at %s!\n", vardev->name);
138			    }
139			}
140		    }
141		}
142	    }
143	}
144    }
145
146    /* Now check for swap devices */
147    for (i = 0; devs[i]; i++) {
148	if (!devs[i]->enabled)
149	    continue;
150	disk = (Disk *)devs[i]->private;
151	msgDebug("Scanning disk %s for swap partitions\n", disk->name);
152	if (!disk->chunks)
153	    msgFatal("No chunk list found for %s!", disk->name);
154	for (c1 = disk->chunks->part; c1; c1 = c1->next) {
155	    if (c1->type == freebsd) {
156		for (c2 = c1->part; c2; c2 = c2->next) {
157		    if (c2->type == part && c2->subtype == FS_SWAP && !swapdev) {
158			swapdev = c2;
159			if (isDebug())
160			    msgDebug("Found swapdev at %s!\n", swapdev->name);
161			break;
162		    }
163		}
164	    }
165	}
166    }
167
168    /* Copy our values over */
169    *rdev = rootdev;
170    *sdev = swapdev;
171    *udev = usrdev;
172    *vdev = vardev;
173
174    if (!rootdev && whinge) {
175	msgConfirm("No root device found - you must label a partition as /\n"
176		   "in the label editor.");
177	status = FALSE;
178    }
179    if (!swapdev && whinge) {
180	msgConfirm("No swap devices found - you must create at least one\n"
181		   "swap partition.");
182	status = FALSE;
183    }
184    return status;
185}
186
187static int
188installInitial(void)
189{
190    static Boolean alreadyDone = FALSE;
191    int status = DITEM_SUCCESS;
192
193    if (alreadyDone)
194	return DITEM_SUCCESS;
195
196    if (!variable_get(DISK_LABELLED)) {
197	msgConfirm("You need to assign disk labels before you can proceed with\n"
198		   "the installation.");
199	return DITEM_FAILURE;
200    }
201    /* If it's labelled, assume it's also partitioned */
202    if (!variable_get(DISK_PARTITIONED))
203	variable_set2(DISK_PARTITIONED, "yes", 0);
204
205    /* If we refuse to proceed, bail. */
206    dialog_clear_norefresh();
207    if (!variable_get(VAR_NO_WARN))
208	if (msgYesNo(
209	    "Last Chance!  Are you SURE you want continue the installation?\n\n"
210	    "If you're running this on a disk with data you wish to save\n"
211	    "then WE STRONGLY ENCOURAGE YOU TO MAKE PROPER BACKUPS before\n"
212	    "proceeding!\n\n"
213	    "We can take no responsibility for lost disk contents!") != 0)
214	return DITEM_FAILURE;
215
216    if (DITEM_STATUS(diskLabelCommit(NULL)) != DITEM_SUCCESS) {
217	msgConfirm("Couldn't make filesystems properly.  Aborting.");
218	return DITEM_FAILURE;
219    }
220
221    if (!copySelf()) {
222	msgConfirm("installInitial: Couldn't clone the boot floppy onto the\n"
223		   "root file system.  Aborting!");
224	return DITEM_FAILURE;
225    }
226
227    if (chroot("/mnt") == -1) {
228	msgConfirm("installInitial: Unable to chroot to %s - this is bad!",
229		   "/mnt");
230	return DITEM_FAILURE;
231    }
232
233    chdir("/");
234    variable_set2(RUNNING_ON_ROOT, "yes", 0);
235
236    /* Configure various files in /etc */
237    if (DITEM_STATUS(configResolv(NULL)) == DITEM_FAILURE)
238	status = DITEM_FAILURE;
239    if (DITEM_STATUS(configFstab(NULL)) == DITEM_FAILURE)
240	status = DITEM_FAILURE;
241
242    /* stick a helpful shell over on the 4th VTY */
243    systemCreateHoloshell();
244
245    alreadyDone = TRUE;
246    return status;
247}
248
249int
250installFixitHoloShell(dialogMenuItem *self)
251{
252    FixItMode = 1;
253    systemCreateHoloshell();
254    return DITEM_SUCCESS;
255    FixItMode = 0;
256}
257
258int
259installFixitCDROM(dialogMenuItem *self)
260{
261    struct stat sb;
262
263    if (!RunningAsInit)
264	return DITEM_SUCCESS;
265
266    variable_set2(SYSTEM_STATE, "fixit", 0);
267    (void)unlink("/mnt2");
268    (void)rmdir("/mnt2");
269
270    while (1) {
271	msgConfirm("Please insert a FreeBSD live filesystem CDROM and press return");
272	if (DITEM_STATUS(mediaSetCDROM(NULL)) != DITEM_SUCCESS || !mediaDevice || !mediaDevice->init(mediaDevice)) {
273	    /* If we can't initialize it, it's probably not a FreeBSD CDROM so punt on it */
274	    mediaClose();
275	    if (msgYesNo("Unable to mount the CDROM - do you want to try again?") != 0)
276		return DITEM_FAILURE;
277	}
278	else
279	    break;
280    }
281
282    /* Since the fixit code expects everything to be in /mnt2, and the CDROM mounting stuff /dist, do
283     * a little kludge dance here..
284     */
285    if (symlink("/dist", "/mnt2")) {
286	msgConfirm("Unable to symlink /mnt2 to the CDROM mount point.  Please report this\n"
287		   "unexpected failure to freebsd-bugs@FreeBSD.org.");
288	return DITEM_FAILURE;
289    }
290
291    /*
292     * If /tmp points to /mnt2/tmp from a previous fixit floppy session, it's
293     * not very good for us if we point it to the CDROM now.  Rather make it
294     * a directory in the root MFS then.  Experienced admins will still be
295     * able to mount their disk's /tmp over this if they need.
296     */
297    if (lstat("/tmp", &sb) == 0 && (sb.st_mode & S_IFMT) == S_IFLNK)
298	(void)unlink("/tmp");
299    Mkdir("/tmp");
300
301    /*
302     * Since setuid binaries ignore LD_LIBRARY_PATH, we indeed need the
303     * ld.so.hints file.  Fortunately, it's fairly small (~ 3 KB).
304     */
305    if (!file_readable("/var/run/ld.so.hints")) {
306	Mkdir("/var/run");
307	if (vsystem("/mnt2/sbin/ldconfig -s /mnt2/usr/lib")) {
308	    msgConfirm("Warning: ldconfig could not create the ld.so hints file.\n"
309		       "Dynamic executables from the CDROM likely won't work.");
310	}
311    }
312
313    /* Yet more iggly hardcoded pathnames. */
314    Mkdir("/usr/libexec");
315    if (!file_readable("/usr/libexec/ld.so") && file_readable("/mnt2/usr/libexec/ld.so")) {
316	if (symlink("/mnt2/usr/libexec/ld.so", "/usr/libexec/ld.so"))
317	    msgDebug("Couldn't link to ld.so - not necessarily a problem for ELF\n");
318    }
319    if (!file_readable("/usr/libexec/ld-elf.so.1")) {
320	if (symlink("/mnt2/usr/libexec/ld-elf.so.1", "/usr/libexec/ld-elf.so.1")) {
321	    msgConfirm("Warning: could not create the symlink for ld-elf.so.1\n"
322		       "Dynamic executables from the CDROM likely won't work.");
323	}
324    }
325    /* optional nicety */
326    if (!file_readable("/usr/bin/vi"))
327	symlink("/mnt2/usr/bin/vi", "/usr/bin/vi");
328    fixit_common();
329    mediaClose();
330    msgConfirm("Please remove the FreeBSD fixit CDROM now.");
331    return DITEM_SUCCESS;
332}
333
334int
335installFixitFloppy(dialogMenuItem *self)
336{
337    struct ufs_args args;
338    extern char *distWanted;
339
340    if (!RunningAsInit)
341	return DITEM_SUCCESS;
342
343    /* Try to open the floppy drive */
344    if (DITEM_STATUS(mediaSetFloppy(NULL)) == DITEM_FAILURE || !mediaDevice) {
345	msgConfirm("Unable to set media device to floppy.");
346	mediaClose();
347	return DITEM_FAILURE;
348    }
349
350    memset(&args, 0, sizeof(args));
351    args.fspec = mediaDevice->devname;
352    mediaDevice->private = "/mnt2";
353    distWanted = NULL;
354    Mkdir("/mnt2");
355
356    variable_set2(SYSTEM_STATE, "fixit", 0);
357
358    while (1) {
359	if (!mediaDevice->init(mediaDevice)) {
360	    if (msgYesNo("The attempt to mount the fixit floppy failed, bad floppy\n"
361			 "or unclean filesystem.  Do you want to try again?"))
362		return DITEM_FAILURE;
363	}
364	else
365	    break;
366    }
367    if (!directory_exists("/tmp"))
368	(void)symlink("/mnt2/tmp", "/tmp");
369    fixit_common();
370    mediaClose();
371    msgConfirm("Please remove the fixit floppy now.");
372    return DITEM_SUCCESS;
373}
374
375/*
376 * The common code for both fixit variants.
377 */
378static void
379fixit_common(void)
380{
381    pid_t child;
382    int waitstatus;
383
384    if (!directory_exists("/var/tmp/vi.recover")) {
385	if (DITEM_STATUS(Mkdir("/var/tmp/vi.recover")) != DITEM_SUCCESS) {
386	    msgConfirm("Warning:  Was unable to create a /var/tmp/vi.recover directory.\n"
387		       "vi will kvetch and moan about it as a result but should still\n"
388		       "be essentially usable.");
389	}
390    }
391    if (!directory_exists("/bin"))
392	(void)Mkdir("/bin");
393    (void)symlink("/stand/sh", "/bin/sh");
394    /* Link the /etc/ files */
395    if (DITEM_STATUS(Mkdir("/etc")) != DITEM_SUCCESS)
396	msgConfirm("Unable to create an /etc directory!  Things are weird on this floppy..");
397    else if ((symlink("/mnt2/etc/spwd.db", "/etc/spwd.db") == -1 && errno != EEXIST) ||
398	     (symlink("/mnt2/etc/protocols", "/etc/protocols") == -1 && errno != EEXIST) ||
399	     (symlink("/mnt2/etc/services", "/etc/services") == -1 && errno != EEXIST))
400	msgConfirm("Couldn't symlink the /etc/ files!  I'm not sure I like this..");
401    if (!file_readable(TERMCAP_FILE))
402	create_termcap();
403    if (!OnVTY)
404	systemSuspendDialog();	/* must be before the fork() */
405    if (!(child = fork())) {
406	int i, fd, fdstop;
407	struct termios foo;
408	extern int login_tty(int);
409
410	ioctl(0, TIOCNOTTY, NULL);
411	fdstop = strcmp(variable_get(VAR_FIXIT_TTY), "serial") == 0 ? 3 : 0;
412	for (i = getdtablesize(); i >= fdstop; --i)
413	    close(i);
414	if (strcmp(variable_get(VAR_FIXIT_TTY), "standard") == 0) {
415	    fd = open("/dev/ttyv3", O_RDWR);
416	    ioctl(0, TIOCSCTTY, &fd);
417	    dup2(0, 1);
418	    dup2(0, 2);
419	}
420	DebugFD = 2;
421	if (login_tty(fd) == -1)
422	    msgDebug("fixit: I can't set the controlling terminal.\n");
423
424	signal(SIGTTOU, SIG_IGN);
425	if (tcgetattr(0, &foo) != -1) {
426	    foo.c_cc[VERASE] = '\010';
427	    if (tcsetattr(0, TCSANOW, &foo) == -1)
428		msgDebug("fixit shell: Unable to set erase character.\n");
429	}
430	else
431	    msgDebug("fixit shell: Unable to get terminal attributes!\n");
432	setenv("PATH", "/bin:/sbin:/usr/bin:/usr/sbin:/stand:"
433	       "/mnt2/stand:/mnt2/bin:/mnt2/sbin:/mnt2/usr/bin:/mnt2/usr/sbin", 1);
434	/* use the .profile from the fixit medium */
435	setenv("HOME", "/mnt2", 1);
436	chdir("/mnt2");
437	execlp("sh", "-sh", 0);
438	msgDebug("fixit shell: Failed to execute shell!\n");
439	_exit(1);;
440    }
441    else {
442	dialog_clear_norefresh();
443	if (strcmp(variable_get(VAR_FIXIT_TTY), "standard") == 0) {
444	    msgNotify("Waiting for fixit shell to exit.  Go to VTY4 now by\n"
445		"typing ALT-F4.  When you are done, type ``exit'' to exit\n"
446		"the fixit shell and be returned here.");
447	} else {
448	    msgNotify("Waiting for fixit shell to exit.\n"
449		"When you are done, type ``exit'' to exit\n"
450		"the fixit shell and be returned here.");
451	}
452	(void)waitpid(child, &waitstatus, 0);
453    }
454    dialog_clear();
455}
456
457
458int
459installExpress(dialogMenuItem *self)
460{
461    int i;
462
463    dialog_clear_norefresh();
464    variable_set2(SYSTEM_STATE, "express", 0);
465#ifndef __alpha__
466    if (DITEM_STATUS((i = diskPartitionEditor(self))) == DITEM_FAILURE)
467	return i;
468#endif
469
470    if (DITEM_STATUS((i = diskLabelEditor(self))) == DITEM_FAILURE)
471	return i;
472
473    if (DITEM_STATUS((i = installCommit(self))) == DITEM_SUCCESS) {
474	i |= DITEM_LEAVE_MENU;
475	/* Give user the option of one last configuration spree */
476	installConfigure();
477    }
478    return i;
479}
480
481/* Standard mode installation */
482int
483installStandard(dialogMenuItem *self)
484{
485    int i, tries = 0;
486    Device **devs;
487
488    variable_set2(SYSTEM_STATE, "standard", 0);
489    dialog_clear_norefresh();
490#ifndef __alpha__
491    msgConfirm("In the next menu, you will need to set up a DOS-style (\"fdisk\") partitioning\n"
492	       "scheme for your hard disk.  If you simply wish to devote all disk space\n"
493	       "to FreeBSD (overwriting anything else that might be on the disk(s) selected)\n"
494	       "then use the (A)ll command to select the default partitioning scheme followed\n"
495	       "by a (Q)uit.  If you wish to allocate only free space to FreeBSD, move to a\n"
496	       "partition marked \"unused\" and use the (C)reate command.");
497
498nodisks:
499    if (DITEM_STATUS(diskPartitionEditor(self)) == DITEM_FAILURE)
500	return DITEM_FAILURE;
501
502    if (diskGetSelectCount(&devs) <= 0 && tries < 3) {
503	msgConfirm("You need to select some disks to operate on!  Be sure to use SPACE\n"
504		   "instead of RETURN in the disk selection menu when selecting a disk.");
505	++tries;
506	goto nodisks;
507    }
508#endif
509
510#ifdef __alpha__
511    msgConfirm("Now you need to create BSD partitions on the disk which you are\n"
512	       "installing to.  If you have a reasonable amount of disk space (200MB or more)\n"
513	       "and don't have any special requirements, simply use the (A)uto command to\n"
514	       "allocate space automatically.  If you have more specific needs or just don't\n"
515	       "care for the layout chosen by (A)uto, press F1 for more information on\n"
516	       "manual layout.");
517#else
518    msgConfirm("Now you need to create BSD partitions inside of the fdisk partition(s)\n"
519	       "just created.  If you have a reasonable amount of disk space (200MB or more)\n"
520	       "and don't have any special requirements, simply use the (A)uto command to\n"
521	       "allocate space automatically.  If you have more specific needs or just don't\n"
522	       "care for the layout chosen by (A)uto, press F1 for more information on\n"
523	       "manual layout.");
524#endif
525
526    if (DITEM_STATUS(diskLabelEditor(self)) == DITEM_FAILURE)
527	return DITEM_FAILURE;
528
529    if (DITEM_STATUS((i = installCommit(self))) == DITEM_FAILURE) {
530	dialog_clear();
531	msgConfirm("Installation completed with some errors.  You may wish to\n"
532		   "scroll through the debugging messages on VTY1 with the\n"
533		   "scroll-lock feature.  You can also choose \"No\" at the next\n"
534		   "prompt and go back into the installation menus to try and retry\n"
535		   "whichever operations have failed.");
536	return i;
537
538    }
539    else {
540	dialog_clear();
541	msgConfirm("Congratulations!  You now have FreeBSD installed on your system.\n\n"
542		   "We will now move on to the final configuration questions.\n"
543		   "For any option you do not wish to configure, simply select\n"
544		   "No.\n\n"
545		   "If you wish to re-enter this utility after the system is up, you\n"
546		   "may do so by typing: /stand/sysinstall.");
547    }
548    if (mediaDevice->type != DEVICE_TYPE_FTP && mediaDevice->type != DEVICE_TYPE_NFS) {
549	if (!msgYesNo("Would you like to configure any Ethernet or SLIP/PPP network devices?")) {
550	    Device *tmp = tcpDeviceSelect();
551
552	    if (tmp && !((DevInfo *)tmp->private)->use_dhcp && !msgYesNo("Would you like to bring the %s interface up right now?", tmp->name))
553		if (!tmp->init(tmp))
554		    msgConfirm("Initialization of %s device failed.", tmp->name);
555	}
556	dialog_clear_norefresh();
557    }
558
559    if (msgYesNo("Will this machine be a leaf node (e.g. will not forward packets)\n"
560		 "between interfaces)?"))
561	variable_set2("gateway_enable", "YES", 1);
562
563    if (msgYesNo("Do you want to grant only normal users FTP access to this\n"
564	         "host (e.g. no anonymous FTP connections)?"))
565	configAnonFTP(self);
566
567    if (!msgYesNo("Do you want to configure this machine as an NFS server?"))
568	configNFSServer(self);
569
570    dialog_clear_norefresh();
571    if (!msgYesNo("Do you want to configure this machine as an NFS client?"))
572	variable_set2("nfs_client_enable", "YES", 1);
573
574    if (!msgYesNo("Would you like to customize your system console settings?"))
575	dmenuOpenSimple(&MenuSyscons, FALSE);
576
577    dialog_clear_norefresh();
578    if (!msgYesNo("Would you like to set this machine's time zone now?"))
579	systemExecute("tzsetup");
580
581#ifdef __i386__
582    dialog_clear_norefresh();
583    if (!msgYesNo("Would you like to enable Linux binary compatibility?"))
584	(void)configLinux(self);
585#endif
586
587#ifdef notyet
588    dialog_clear();
589    if (USAResident) {
590	if (!msgYesNo("I see that you are \"USA_RESIDENT\" according to your earlier\n"
591	    "response to the CRYPTO distribution dialog.  Do you want to try and\n"
592	    "load the rsaref package from the current media?  Some restrictions on\n"
593	    "usage may apply, so be sure to read the package installation output!")) {
594	    dialog_clear();
595	    _interactiveHack = 1;
596	    if (DITEM_STATUS(package_add("rsaref")) != DITEM_SUCCESS) {
597		msgConfirm("Unable to find an rsaref package on the current intallation media.\n"
598		    	   "This is probably because you are installing from a CDROM which\n"
599			   "was produced for world-wide use, in which case the RSA patent\n"
600			   "prevents distribution of RSA code on CD.  Please change your\n"
601			   "media device to point to an International FTP server and install\n"
602			   "the rsaref package manually through the Packages menu.");
603	    }
604	    dialog_clear();
605	    _interactiveHack = 0;
606	}
607    }
608    else {
609	if (!msgYesNo("I see that you are not \"USA_RESIDENT\" according to your earlier\n"
610	    "response to the CRYPTO distribution dialog.  Do you want to try and\n"
611	    "load the rsaintl package from the current media?")) {
612	    if (DITEM_STATUS(package_add("rsaintl")) != DITEM_SUCCESS) {
613		msgConfirm("Unable to find an rsaintl package on the current intallation media.\n"
614		    	   "This is probably because you are installing from a CDROM which\n"
615			   "was produced for use in the USA, in which case the RSA patent\n"
616			   "prevents distribution of RSA code on CD.  Please change your\n"
617			   "media device to point to an International FTP server and install\n"
618			   "the rsaintl package manually through the Packages menu.");
619	    }
620	}
621    }
622#endif	/* notyet */
623
624    dialog_clear_norefresh();
625    if (!msgYesNo("Does this system have a non-USB mouse attached to it?"))
626	dmenuOpenSimple(&MenuMouse, FALSE);
627
628    /* Now would be a good time to checkpoint the configuration data */
629    configRC_conf();
630    sync();
631
632    if (directory_exists("/usr/X11R6")) {
633	dialog_clear_norefresh();
634	if (!msgYesNo("Would you like to configure your X server at this time?"))
635	    (void)configXSetup(self);
636    }
637
638    dialog_clear_norefresh();
639    if (!msgYesNo("The FreeBSD package collection is a collection of thousands of ready-to-run\n"
640		  "applications, from text editors to games to WEB servers and more.  Would you\n"
641		  "like to browse the collection now?")) {
642	(void)configPackages(self);
643    }
644
645    if (!msgYesNo("Would you like to add any initial user accounts to the system?\n"
646		  "Adding at least one account for yourself at this stage is suggested\n"
647		  "since working as the \"root\" user is dangerous (it is easy to do\n"
648		  "things which adversely affect the entire system)."))
649	(void)configUsers(self);
650
651    msgConfirm("Now you must set the system manager's password.\n"
652	       "This is the password you'll use to log in as \"root\".");
653    if (!systemExecute("passwd root"))
654	variable_set2("root_password", "YES", 0);
655
656    /* XXX Put whatever other nice configuration questions you'd like to ask the user here XXX */
657
658    /* Give user the option of one last configuration spree */
659    dialog_clear_norefresh();
660    installConfigure();
661    return DITEM_LEAVE_MENU;
662}
663
664/* The version of commit we call from the Install Custom menu */
665int
666installCustomCommit(dialogMenuItem *self)
667{
668    int i;
669
670    i = installCommit(self);
671    if (DITEM_STATUS(i) == DITEM_SUCCESS) {
672	/* Give user the option of one last configuration spree */
673	installConfigure();
674	return i;
675    }
676    else
677	msgConfirm("The commit operation completed with errors.  Not\n"
678		   "updating /etc files.");
679    return i;
680}
681
682/*
683 * What happens when we finally decide to going ahead with the installation.
684 *
685 * This is broken into multiple stages so that the user can do a full
686 * installation but come back here again to load more distributions,
687 * perhaps from a different media type.  This would allow, for
688 * example, the user to load the majority of the system from CDROM and
689 * then use ftp to load just the CRYPTO dist.
690 */
691int
692installCommit(dialogMenuItem *self)
693{
694    int i;
695    char *str;
696
697    dialog_clear_norefresh();
698    if (!Dists)
699	distConfig(NULL);
700
701    if (!Dists)
702	if (!dmenuOpenSimple(&MenuDistributions, FALSE) && !Dists)
703	    return DITEM_FAILURE;
704
705    if (!mediaVerify())
706	return DITEM_FAILURE;
707
708    str = variable_get(SYSTEM_STATE);
709    if (isDebug())
710	msgDebug("installCommit: System state is `%s'\n", str);
711
712    /* Installation stuff we wouldn't do to a running system */
713    if (RunningAsInit && DITEM_STATUS((i = installInitial())) == DITEM_FAILURE)
714	return i;
715
716try_media:
717    if (!mediaDevice->init(mediaDevice)) {
718	if (!msgYesNo("Unable to initialize selected media. Would you like to\n"
719		      "adjust your media configuration and try again?")) {
720	    mediaDevice = NULL;
721	    if (!mediaVerify())
722		return DITEM_FAILURE;
723	    else
724		goto try_media;
725	}
726	else
727	    return DITEM_FAILURE;
728    }
729
730    /* Now go get it all */
731    i = distExtractAll(self);
732
733    /* When running as init, *now* it's safe to grab the rc.foo vars */
734    installEnvironment();
735
736    variable_set2(SYSTEM_STATE, DITEM_STATUS(i) == DITEM_FAILURE ? "error-install" : "full-install", 0);
737
738    return i;
739}
740
741static void
742installConfigure(void)
743{
744    /* Final menu of last resort */
745    if (!msgYesNo("Visit the general configuration menu for a chance to set\n"
746		  "any last options?"))
747	dmenuOpenSimple(&MenuConfigure, FALSE);
748    configRC_conf();
749    sync();
750}
751
752int
753installFixupBin(dialogMenuItem *self)
754{
755    Device **devs;
756    char *cp;
757    int i;
758    FILE *fp;
759    int kstat = 1;
760
761    /* All of this is done only as init, just to be safe */
762    if (RunningAsInit) {
763	/* Fix up kernel first */
764	if (!file_readable("/kernel")) {
765	    char *generic_kernel = "/kernel.GENERIC";
766	    if (file_readable(generic_kernel)) {
767		if (vsystem("cp -p %s /kernel", generic_kernel)) {
768		    msgConfirm("Unable to copy /kernel into place!");
769		    return DITEM_FAILURE;
770		}
771#ifndef __alpha__
772                /* Snapshot any boot -c changes back to the new kernel */
773		cp = variable_get(VAR_KGET);
774		if (cp && (*cp == 'Y' || *cp == 'y')) {
775		    if ((kstat = kget("/boot/kernel.conf")) != NULL) {
776			msgConfirm("Kernel copied OK, but unable to save boot -c changes\n"
777				   "to it.  See the debug screen (ALT-F2) for details.");
778		    }
779		}
780		if ((fp = fopen("/boot/loader.conf", "a")) != NULL) {
781		    fprintf(fp, "# -- sysinstall generated deltas -- #\n");
782		    if (!kstat)
783			fprintf(fp, "userconfig_script_load=\"YES\"\n");
784		    if (!OnVTY)
785			fprintf(fp, "console=\"comconsole\"\n");
786		    fclose(fp);
787		}
788#endif
789	    }
790	    else {
791		msgConfirm("Can't find a kernel image to link to on the root file system!\n"
792			   "You're going to have a hard time getting this system to\n"
793			   "boot from the hard disk, I'm afraid!");
794		return DITEM_FAILURE;
795	    }
796	}
797
798	/* BOGON #1: Resurrect /dev after bin distribution screws it up */
799	dialog_clear_norefresh();
800	msgNotify("Remaking all devices.. Please wait!");
801	if (vsystem("cd /dev; sh MAKEDEV all")) {
802	    msgConfirm("MAKEDEV returned non-zero status");
803	    return DITEM_FAILURE | DITEM_RESTORE;
804	}
805
806	dialog_clear_norefresh();
807	msgNotify("Resurrecting /dev entries for slices..");
808	devs = deviceFind(NULL, DEVICE_TYPE_DISK);
809	if (!devs)
810	    msgFatal("Couldn't get a disk device list!");
811
812	/* Resurrect the slices that the former clobbered */
813	for (i = 0; devs[i]; i++) {
814	    Disk *disk = (Disk *)devs[i]->private;
815	    Chunk *c1;
816
817	    if (!devs[i]->enabled)
818		continue;
819	    if (!disk->chunks)
820		msgFatal("No chunk list found for %s!", disk->name);
821	    for (c1 = disk->chunks->part; c1; c1 = c1->next) {
822		if (c1->type == freebsd) {
823		    dialog_clear_norefresh();
824		    msgNotify("Making slice entries for %s", c1->name);
825		    if (vsystem("cd /dev; sh MAKEDEV %sh", c1->name)) {
826			msgConfirm("Unable to make slice entries for %s!", c1->name);
827			return DITEM_FAILURE | DITEM_RESTORE;
828		    }
829		}
830	    }
831	}
832
833	/* BOGON #2: We leave /etc in a bad state */
834	chmod("/etc", 0755);
835
836	/* BOGON #3: No /var/db/mountdtab complains */
837	Mkdir("/var/db");
838	creat("/var/db/mountdtab", 0644);
839
840	/* BOGON #4: /compat created by default in root fs */
841	Mkdir("/usr/compat");
842	vsystem("ln -s /usr/compat /compat");
843
844	/* BOGON #5: aliases database not build for bin */
845	vsystem("newaliases");
846
847	/* Now run all the mtree stuff to fix things up */
848        vsystem("mtree -deU -f /etc/mtree/BSD.root.dist -p /");
849        vsystem("mtree -deU -f /etc/mtree/BSD.var.dist -p /var");
850        vsystem("mtree -deU -f /etc/mtree/BSD.usr.dist -p /usr");
851
852	/* Do all the last ugly work-arounds here */
853    }
854    return DITEM_SUCCESS | DITEM_RESTORE;
855}
856
857#ifdef X_AS_PKG
858int
859installX11package(dialogMenuItem *self)
860{
861    WINDOW *w = savescr();
862    int i;
863
864    dialog_clear_norefresh();
865    msgNotify("Installing XFree86 package...");
866    i = package_add("XFree86");
867    restorescr(w);
868    return i;
869}
870#endif
871
872/* Fix side-effects from the the XFree86 installation */
873int
874installFixupXFree(dialogMenuItem *self)
875{
876    /* BOGON #1:  XFree86 requires various specialized fixups */
877    if (directory_exists("/usr/X11R6")) {
878	dialog_clear_norefresh();
879	msgNotify("Fixing permissions in XFree86 tree..");
880	vsystem("chmod -R a+r /usr/X11R6");
881	vsystem("find /usr/X11R6 -type d | xargs chmod a+x");
882
883#ifndef X_AS_PKG
884	/* Also do bogus minimal package registration so ports don't whine */
885	if (file_readable("/usr/X11R6/lib/X11/pkgreg.tar.gz")) {
886	    dialog_clear_norefresh();
887	    msgNotify("Installing package metainfo..");
888	    vsystem("tar xpzf /usr/X11R6/lib/X11/pkgreg.tar.gz -C / && rm /usr/X11R6/lib/X11/pkgreg.tar.gz");
889	}
890#endif
891    }
892    return DITEM_SUCCESS | DITEM_RESTORE;
893}
894
895/* Go newfs and/or mount all the filesystems we've been asked to */
896int
897installFilesystems(dialogMenuItem *self)
898{
899    int i;
900    Disk *disk;
901    Chunk *c1, *c2, *rootdev, *swapdev, *usrdev, *vardev;
902    Device **devs;
903    PartInfo *root;
904    char dname[80];
905    extern int MakeDevChunk(Chunk *c, char *n);
906    Boolean upgrade = FALSE;
907
908    /* If we've already done this, bail out */
909    if (!variable_cmp(DISK_LABELLED, "written"))
910	return DITEM_SUCCESS;
911
912    upgrade = !variable_cmp(SYSTEM_STATE, "upgrade");
913    if (!checkLabels(TRUE, &rootdev, &swapdev, &usrdev, &vardev))
914	return DITEM_FAILURE;
915
916    if (rootdev)
917	root = (PartInfo *)rootdev->private_data;
918    else
919	root = NULL;
920
921    command_clear();
922    if (swapdev && RunningAsInit) {
923	/* As the very first thing, try to get ourselves some swap space */
924	sprintf(dname, "/dev/%s", swapdev->name);
925	if (!Fake && (!MakeDevChunk(swapdev, "/dev") || !file_readable(dname))) {
926	    msgConfirm("Unable to make device node for %s in /dev!\n"
927		       "The creation of filesystems will be aborted.", dname);
928	    return DITEM_FAILURE;
929	}
930
931	if (!Fake) {
932	    if (!swapon(dname)) {
933		dialog_clear_norefresh();
934		msgNotify("Added %s as initial swap device", dname);
935	    }
936	    else {
937		msgConfirm("WARNING!  Unable to swap to %s: %s\n"
938			   "This may cause the installation to fail at some point\n"
939			   "if you don't have a lot of memory.", dname, strerror(errno));
940	    }
941	}
942    }
943
944    if (rootdev && RunningAsInit) {
945	/* Next, create and/or mount the root device */
946	sprintf(dname, "/dev/%s", rootdev->name);
947	if (!Fake && (!MakeDevChunk(rootdev, "/dev") || !file_readable(dname))) {
948	    msgConfirm("Unable to make device node for %s in /dev!\n"
949		       "The creation of filesystems will be aborted.", dname);
950	    return DITEM_FAILURE | DITEM_RESTORE;
951	}
952	if (strcmp(root->mountpoint, "/"))
953	    msgConfirm("Warning: %s is marked as a root partition but is mounted on %s", rootdev->name, root->mountpoint);
954
955	if (root->newfs && (!upgrade || !msgYesNo("You are upgrading - are you SURE you want to newfs the root partition?"))) {
956	    int i;
957
958	    dialog_clear_norefresh();
959	    msgNotify("Making a new root filesystem on %s", dname);
960	    i = vsystem("%s %s", root->newfs_cmd, dname);
961	    if (i) {
962		msgConfirm("Unable to make new root filesystem on %s!\n"
963			   "Command returned status %d", dname, i);
964		return DITEM_FAILURE | DITEM_RESTORE;
965	    }
966	}
967	else {
968	    if (!upgrade) {
969		msgConfirm("Warning:  Using existing root partition.  It will be assumed\n"
970			   "that you have the appropriate device entries already in /dev.");
971	    }
972	    dialog_clear_norefresh();
973	    msgNotify("Checking integrity of existing %s filesystem.", dname);
974	    i = vsystem("fsck -y %s", dname);
975	    if (i)
976		msgConfirm("Warning: fsck returned status of %d for %s.\n"
977			   "This partition may be unsafe to use.", i, dname);
978	}
979
980	/* Switch to block device */
981	sprintf(dname, "/dev/%s", rootdev->name);
982	if (Mount("/mnt", dname)) {
983	    msgConfirm("Unable to mount the root file system on %s!  Giving up.", dname);
984	    return DITEM_FAILURE | DITEM_RESTORE;
985	}
986    }
987
988    /* Now buzz through the rest of the partitions and mount them too */
989    devs = deviceFind(NULL, DEVICE_TYPE_DISK);
990    for (i = 0; devs[i]; i++) {
991	if (!devs[i]->enabled)
992	    continue;
993
994	disk = (Disk *)devs[i]->private;
995	if (!disk->chunks) {
996	    msgConfirm("No chunk list found for %s!", disk->name);
997	    return DITEM_FAILURE | DITEM_RESTORE;
998	}
999	if (RunningAsInit && root && (root->newfs || upgrade)) {
1000	    Mkdir("/mnt/dev");
1001	    if (!Fake)
1002		MakeDevDisk(disk, "/mnt/dev");
1003	}
1004	else if (!RunningAsInit && !Fake)
1005	    MakeDevDisk(disk, "/dev");
1006
1007	for (c1 = disk->chunks->part; c1; c1 = c1->next) {
1008	    if (c1->type == freebsd) {
1009		for (c2 = c1->part; c2; c2 = c2->next) {
1010		    if (c2->type == part && c2->subtype != FS_SWAP && c2->private_data) {
1011			PartInfo *tmp = (PartInfo *)c2->private_data;
1012
1013			/* Already did root */
1014			if (c2 == rootdev)
1015			    continue;
1016
1017			if (tmp->newfs && (!upgrade || !msgYesNo("You are upgrading - are you SURE you want to newfs /dev/%s?", c2->name)))
1018			    command_shell_add(tmp->mountpoint, "%s %s/dev/%s", tmp->newfs_cmd, RunningAsInit ? "/mnt" : "", c2->name);
1019			else
1020			    command_shell_add(tmp->mountpoint, "fsck -y %s/dev/%s", RunningAsInit ? "/mnt" : "", c2->name);
1021			command_func_add(tmp->mountpoint, Mount, c2->name);
1022		    }
1023		    else if (c2->type == part && c2->subtype == FS_SWAP) {
1024			char fname[80];
1025			int i;
1026
1027			if (c2 == swapdev)
1028			    continue;
1029			sprintf(fname, "%s/dev/%s", RunningAsInit ? "/mnt" : "", c2->name);
1030			i = (Fake || swapon(fname));
1031			if (!i) {
1032			    dialog_clear_norefresh();
1033			    msgNotify("Added %s as an additional swap device", fname);
1034			}
1035			else {
1036			    msgConfirm("Unable to add %s as a swap device: %s", fname, strerror(errno));
1037			}
1038		    }
1039		}
1040	    }
1041	    else if (c1->type == fat && c1->private_data && (root->newfs || upgrade)) {
1042		char name[FILENAME_MAX];
1043
1044		sprintf(name, "%s/%s", RunningAsInit ? "/mnt" : "", ((PartInfo *)c1->private_data)->mountpoint);
1045		Mkdir(name);
1046	    }
1047	}
1048    }
1049
1050    if (RunningAsInit) {
1051	dialog_clear_norefresh();
1052	msgNotify("Copying initial device files..");
1053	/* Copy the boot floppy's dev files */
1054	if ((root->newfs || upgrade) && vsystem("find -x /dev | cpio %s -pdum /mnt", cpioVerbosity())) {
1055	    msgConfirm("Couldn't clone the /dev files!");
1056	    return DITEM_FAILURE | DITEM_RESTORE;
1057	}
1058    }
1059
1060    command_sort();
1061    command_execute();
1062    dialog_clear_norefresh();
1063    return DITEM_SUCCESS | DITEM_RESTORE;
1064}
1065
1066static char *
1067getRelname(void)
1068{
1069    static char buf[64];
1070    int sz = (sizeof buf) - 1;
1071
1072    if (sysctlbyname("kern.osrelease", buf, &sz, NULL, 0) != -1) {
1073	buf[sz] = '\0';
1074	return buf;
1075    }
1076    else
1077	return "<unknown>";
1078}
1079
1080/* Initialize various user-settable values to their defaults */
1081int
1082installVarDefaults(dialogMenuItem *self)
1083{
1084    char *cp;
1085
1086    /* Set default startup options */
1087    variable_set2(VAR_RELNAME,			getRelname(), 0);
1088    variable_set2(VAR_CPIO_VERBOSITY,		"high", 0);
1089    variable_set2(VAR_KGET,			"YES", 0);
1090    variable_set2(VAR_TAPE_BLOCKSIZE,		DEFAULT_TAPE_BLOCKSIZE, 0);
1091    variable_set2(VAR_INSTALL_ROOT,		"/", 0);
1092    variable_set2(VAR_INSTALL_CFG,		"install.cfg", 0);
1093    variable_set2(VAR_TRY_DHCP,			"NO", 0);	/* For now */
1094    variable_set2(VAR_TRY_RTSOL,		"NO", 0);	/* For now */
1095    cp = getenv("EDITOR");
1096    if (!cp)
1097	cp = "/usr/bin/ee";
1098    variable_set2(VAR_EDITOR,			cp, 0);
1099    variable_set2(VAR_FTP_USER,			"ftp", 0);
1100    variable_set2(VAR_BROWSER_PACKAGE,		"lynx", 0);
1101    variable_set2(VAR_BROWSER_BINARY,		"/usr/local/bin/lynx", 0);
1102    variable_set2(VAR_FTP_STATE,		"passive", 0);
1103    variable_set2(VAR_NFS_SECURE,		"NO", -1);
1104    variable_set2(VAR_FIXIT_TTY,		"standard", 0);
1105    variable_set2(VAR_PKG_TMPDIR,		"/usr/tmp", 0);
1106    variable_set2(VAR_MEDIA_TIMEOUT,		itoa(MEDIA_TIMEOUT), 0);
1107    if (getpid() != 1)
1108	variable_set2(SYSTEM_STATE,		"update", 0);
1109    else
1110	variable_set2(SYSTEM_STATE,		"init", 0);
1111    variable_set2(VAR_NEWFS_ARGS,		"-b 8192 -f 1024", 0);
1112    return DITEM_SUCCESS;
1113}
1114
1115/* Load the environment up from various system configuration files */
1116void
1117installEnvironment(void)
1118{
1119    configEnvironmentRC_conf();
1120    if (file_readable("/etc/resolv.conf"))
1121	configEnvironmentResolv("/etc/resolv.conf");
1122    configMake_conf("/etc/make.conf");
1123}
1124
1125/* Copy the boot floppy contents into /stand */
1126Boolean
1127copySelf(void)
1128{
1129    int i;
1130
1131    if (file_readable("/boot.help"))
1132	vsystem("cp /boot.help /mnt");
1133    msgWeHaveOutput("Copying the boot floppy to /stand on root filesystem");
1134    i = vsystem("find -x /stand | cpio %s -pdum /mnt", cpioVerbosity());
1135    if (i) {
1136	msgConfirm("Copy returned error status of %d!", i);
1137	return FALSE;
1138    }
1139
1140    /* Copy the /etc files into their rightful place */
1141    if (vsystem("cd /mnt/stand; find etc | cpio %s -pdum /mnt", cpioVerbosity())) {
1142	msgConfirm("Couldn't copy up the /etc files!");
1143	return TRUE;
1144    }
1145    return TRUE;
1146}
1147
1148static void
1149create_termcap(void)
1150{
1151    FILE *fp;
1152
1153    const char *caps[] = {
1154	termcap_vt100, termcap_cons25, termcap_cons25_m, termcap_cons25r,
1155	termcap_cons25r_m, termcap_cons25l1, termcap_cons25l1_m,
1156	termcap_xterm, NULL,
1157    };
1158    const char **cp;
1159
1160    if (!file_readable(TERMCAP_FILE)) {
1161	Mkdir("/usr/share/misc");
1162	fp = fopen(TERMCAP_FILE, "w");
1163	if (!fp) {
1164	    msgConfirm("Unable to initialize termcap file. Some screen-oriented\nutilities may not work.");
1165	    return;
1166	}
1167	cp = caps;
1168	while (*cp)
1169	    fprintf(fp, "%s\n", *(cp++));
1170	fclose(fp);
1171    }
1172}
1173