xref: /netbsd-src/usr.sbin/sysinst/checkrc.c (revision 6dccb7a0e98dbef140989d4bb2d7aa8676c47c86)
1*6dccb7a0Srillig /* $NetBSD: checkrc.c,v 1.3 2021/01/31 22:45:46 rillig Exp $ */
250dbef1aSdholland 
350dbef1aSdholland /*-
450dbef1aSdholland  * Copyright (c) 2012 The NetBSD Foundation, Inc.
550dbef1aSdholland  * All rights reserved.
650dbef1aSdholland  *
750dbef1aSdholland  * This code is derived from software contributed to The NetBSD Foundation
850dbef1aSdholland  * by Jeffrey C. Rizzo
950dbef1aSdholland  *
1050dbef1aSdholland  * Redistribution and use in source and binary forms, with or without
1150dbef1aSdholland  * modification, are permitted provided that the following conditions
1250dbef1aSdholland  * are met:
1350dbef1aSdholland  * 1. Redistributions of source code must retain the above copyright
1450dbef1aSdholland  *    notice, this list of conditions and the following disclaimer.
1550dbef1aSdholland  * 2. Redistributions in binary form must reproduce the above copyright
1650dbef1aSdholland  *    notice, this list of conditions and the following disclaimer in the
1750dbef1aSdholland  *    documentation and/or other materials provided with the distribution.
1850dbef1aSdholland  *
1950dbef1aSdholland  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2050dbef1aSdholland  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2150dbef1aSdholland  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2250dbef1aSdholland  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2350dbef1aSdholland  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2450dbef1aSdholland  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2550dbef1aSdholland  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2650dbef1aSdholland  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2750dbef1aSdholland  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2850dbef1aSdholland  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2950dbef1aSdholland  * POSSIBILITY OF SUCH DAMAGE.
3050dbef1aSdholland  */
3150dbef1aSdholland 
3250dbef1aSdholland /* checkrc.c -- Create a script on the target to check the state of
3350dbef1aSdholland  * its rc.conf variables. */
3450dbef1aSdholland 
3550dbef1aSdholland #include <curses.h>
3650dbef1aSdholland #include <err.h>
3750dbef1aSdholland #include <stdio.h>
3850dbef1aSdholland #include "defs.h"
3950dbef1aSdholland #include "msg_defs.h"
4050dbef1aSdholland #include "menu_defs.h"
4150dbef1aSdholland 
4250dbef1aSdholland #define RC_CHECK_SCRIPT "/tmp/checkrc.sh"
4350dbef1aSdholland 
4450dbef1aSdholland static int create_script(const char *, int);
4550dbef1aSdholland static int check(const char *, int);
4650dbef1aSdholland 
4750dbef1aSdholland char *rcconf = NULL;
4850dbef1aSdholland 
4950dbef1aSdholland enum {
5050dbef1aSdholland 	CHECK_CONF,
5150dbef1aSdholland 	CHECK_DEFAULT
5250dbef1aSdholland };
5350dbef1aSdholland 
5450dbef1aSdholland static int
create_script(const char * varname,int filetocheck)5550dbef1aSdholland create_script(const char *varname, int filetocheck)
5650dbef1aSdholland {
5750dbef1aSdholland 	FILE	*fp;
5850dbef1aSdholland 
5950dbef1aSdholland 	if ((fp = fopen(target_expand(RC_CHECK_SCRIPT), "w")) == NULL) {
6050dbef1aSdholland 		if (logfp)
6150dbef1aSdholland 			fprintf(logfp,"Could not open %s for writing",
6250dbef1aSdholland 			    target_expand(RC_CHECK_SCRIPT));
6350dbef1aSdholland 		warn("Could not open %s for writing",
6450dbef1aSdholland 		    target_expand(RC_CHECK_SCRIPT));
6550dbef1aSdholland 		return 1;
6650dbef1aSdholland 	}
6750dbef1aSdholland 
6850dbef1aSdholland 	if (filetocheck == CHECK_DEFAULT)
6950dbef1aSdholland 		fprintf(fp, "#!/bin/sh\n. /etc/defaults/rc.conf\n"
7050dbef1aSdholland 		    ". /etc/rc.subr\n");
7150dbef1aSdholland 	else
7250dbef1aSdholland 		fprintf(fp, "#!/bin/sh\n. /etc/rc.conf\n. /etc/rc.subr\n");
7350dbef1aSdholland 	fprintf(fp, "if checkyesno %s\nthen\necho YES\nelse\necho NO\nfi\n",
7450dbef1aSdholland 	    varname);
7550dbef1aSdholland 
7650dbef1aSdholland 	fclose(fp);
7750dbef1aSdholland 	return 0;
7850dbef1aSdholland }
7950dbef1aSdholland 
8050dbef1aSdholland static int
check(const char * varname,int filetocheck)8150dbef1aSdholland check(const char *varname, int filetocheck)
8250dbef1aSdholland {
8350dbef1aSdholland 	char *buf;
846bccae72Smartin 	int rv;
8550dbef1aSdholland 
8650dbef1aSdholland 	create_script(varname, filetocheck);
8750dbef1aSdholland 
8850dbef1aSdholland 	if (target_already_root())
8950dbef1aSdholland 		collect(T_OUTPUT, &buf, "/bin/sh %s 2>&1", RC_CHECK_SCRIPT);
9050dbef1aSdholland 	else
9150dbef1aSdholland 		collect(T_OUTPUT, &buf, "chroot %s /bin/sh %s 2>&1",
9250dbef1aSdholland 				target_prefix(), RC_CHECK_SCRIPT);
9350dbef1aSdholland 
9450dbef1aSdholland 
9550dbef1aSdholland 	unlink(target_expand(RC_CHECK_SCRIPT));
9650dbef1aSdholland 
9750dbef1aSdholland 	if (logfp) {
9850dbef1aSdholland 		fprintf(logfp,"var %s is %s\n", varname, buf);
9950dbef1aSdholland 		fflush(logfp);
10050dbef1aSdholland 	}
10150dbef1aSdholland 
1026bccae72Smartin 	rv = strncmp(buf, "YES", strlen("YES")) == 0;
1036bccae72Smartin 	free(buf);
1046bccae72Smartin 	return rv;
10550dbef1aSdholland }
10650dbef1aSdholland 
10750dbef1aSdholland int
check_rcvar(const char * varname)10850dbef1aSdholland check_rcvar(const char *varname)
10950dbef1aSdholland {
11050dbef1aSdholland 	return check(varname, CHECK_CONF);
11150dbef1aSdholland }
11250dbef1aSdholland 
11350dbef1aSdholland int
check_rcdefault(const char * varname)11450dbef1aSdholland check_rcdefault(const char *varname)
11550dbef1aSdholland {
11650dbef1aSdholland 	return check(varname, CHECK_DEFAULT);
11750dbef1aSdholland }
118