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