1/* $NetBSD: checkrc.c,v 1.3 2021/01/31 22:45:46 rillig Exp $ */ 2 3/*- 4 * Copyright (c) 2012 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jeffrey C. Rizzo 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32/* checkrc.c -- Create a script on the target to check the state of 33 * its rc.conf variables. */ 34 35#include <curses.h> 36#include <err.h> 37#include <stdio.h> 38#include "defs.h" 39#include "msg_defs.h" 40#include "menu_defs.h" 41 42#define RC_CHECK_SCRIPT "/tmp/checkrc.sh" 43 44static int create_script(const char *, int); 45static int check(const char *, int); 46 47char *rcconf = NULL; 48 49enum { 50 CHECK_CONF, 51 CHECK_DEFAULT 52}; 53 54static int 55create_script(const char *varname, int filetocheck) 56{ 57 FILE *fp; 58 59 if ((fp = fopen(target_expand(RC_CHECK_SCRIPT), "w")) == NULL) { 60 if (logfp) 61 fprintf(logfp,"Could not open %s for writing", 62 target_expand(RC_CHECK_SCRIPT)); 63 warn("Could not open %s for writing", 64 target_expand(RC_CHECK_SCRIPT)); 65 return 1; 66 } 67 68 if (filetocheck == CHECK_DEFAULT) 69 fprintf(fp, "#!/bin/sh\n. /etc/defaults/rc.conf\n" 70 ". /etc/rc.subr\n"); 71 else 72 fprintf(fp, "#!/bin/sh\n. /etc/rc.conf\n. /etc/rc.subr\n"); 73 fprintf(fp, "if checkyesno %s\nthen\necho YES\nelse\necho NO\nfi\n", 74 varname); 75 76 fclose(fp); 77 return 0; 78} 79 80static int 81check(const char *varname, int filetocheck) 82{ 83 char *buf; 84 int rv; 85 86 create_script(varname, filetocheck); 87 88 if (target_already_root()) 89 collect(T_OUTPUT, &buf, "/bin/sh %s 2>&1", RC_CHECK_SCRIPT); 90 else 91 collect(T_OUTPUT, &buf, "chroot %s /bin/sh %s 2>&1", 92 target_prefix(), RC_CHECK_SCRIPT); 93 94 95 unlink(target_expand(RC_CHECK_SCRIPT)); 96 97 if (logfp) { 98 fprintf(logfp,"var %s is %s\n", varname, buf); 99 fflush(logfp); 100 } 101 102 rv = strncmp(buf, "YES", strlen("YES")) == 0; 103 free(buf); 104 return rv; 105} 106 107int 108check_rcvar(const char *varname) 109{ 110 return check(varname, CHECK_CONF); 111} 112 113int 114check_rcdefault(const char *varname) 115{ 116 return check(varname, CHECK_DEFAULT); 117} 118