xref: /openbsd-src/usr.sbin/config/ukc.c (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1 /*	$OpenBSD: ukc.c,v 1.20 2015/10/12 04:43:30 deraadt Exp $ */
2 
3 /*
4  * Copyright (c) 1999-2001 Mats O Jansson.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include <sys/types.h>
28 #include <sys/device.h>
29 
30 #include <err.h>
31 #include <fcntl.h>
32 #include <kvm.h>
33 #include <limits.h>
34 #include <nlist.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
39 
40 #define UKC_MAIN
41 #include "ukc.h"
42 #include "exec.h"
43 
44 void	init(void);
45 void	usage(void);
46 
47 int	ukc_mod_kernel = 0;
48 
49 static void
50 check_int(int idx, const char *name)
51 {
52 	if (nl[idx].n_type == 0)
53 		printf("WARNING this kernel doesn't support modification "
54 		    "of %s.\n", name);
55 }
56 
57 int
58 ukc(char *file, char *outfile, int uflag, int force)
59 {
60 	extern char *__progname;
61 	int i;
62 	kvm_t *kd;
63 	char errbuf[_POSIX2_LINE_MAX];
64 	int histlen = 0, ok = 1;
65 	char history[1024], kversion[1024];
66 
67 	if (file == NULL) {
68 		fprintf(stderr, "%s: no file specified\n", __progname);
69 		usage();
70 	}
71 
72 	loadkernel(file);
73 
74 	if (nlist(file, nl) == -1)
75 		errx(1, "nlist: %s", file);
76 
77 	if (uflag) {
78 		if ((kd = kvm_openfiles(NULL,NULL,NULL,O_RDONLY, errbuf)) == 0)
79 			errx(1, "kvm_openfiles: %s", errbuf);
80 
81 		if (kvm_nlist(kd, knl) == -1)
82 			errx(1, "kvm_nlist: %s", kvm_geterr(kd));
83 
84 		i = 0;
85 		while (i < NLENTRIES) {
86 			if (nl[i].n_type != knl[i].n_type ||
87 			    nl[i].n_desc != knl[i].n_desc ||
88 			    nl[i].n_value != knl[i].n_value)
89 				ok = 0;
90 			i++;
91 		}
92 
93 		if (knl[I_HISTLEN].n_type != 0 && ok) {
94 			if (kvm_read(kd, knl[I_HISTLEN].n_value, &histlen,
95 			    sizeof(histlen)) != sizeof(histlen))
96 				warnx("cannot read %s: %s",
97 				    knl[I_HISTLEN].n_name,
98 				    kvm_geterr(kd));
99 		}
100 		if (knl[CA_HISTORY].n_type != 0 && ok) {
101 			if (kvm_read(kd, knl[CA_HISTORY].n_value, history,
102 			    sizeof(history)) != sizeof(history))
103 				warnx("cannot read %s: %s",
104 				    knl[CA_HISTORY].n_name,
105 				    kvm_geterr(kd));
106 		}
107 		if (knl[P_VERSION].n_type != 0 && ok) {
108 			if (kvm_read(kd, knl[P_VERSION].n_value, kversion,
109 			    sizeof(kversion)) != sizeof(kversion))
110 				warnx("cannot read %s: %s",
111 				    knl[P_VERSION].n_name,
112 				    kvm_geterr(kd));
113 		}
114 	}
115 
116 	if (force == 0 && outfile == NULL)
117 		printf("WARNING no output file specified\n");
118 
119 	if (nl[IA_EXTRALOC].n_type == 0 || nl[I_NEXTRALOC].n_type == 0 ||
120 	    nl[I_UEXTRALOC].n_type == 0 || nl[I_HISTLEN].n_type == 0 ||
121 	    nl[CA_HISTORY].n_type == 0) {
122 		printf("\
123 WARNING this kernel doesn't contain all information needed!\n\
124 WARNING the commands add and change might not work.\n");
125 		oldkernel = 1;
126 	}
127 
128 	if (nl[P_PDEVNAMES].n_type == 0 ||
129 	    nl[I_PDEVSIZE].n_type == 0 ||
130 	    nl[S_PDEVINIT].n_type == 0) {
131 		printf("\
132 WARNING this kernel doesn't support pseudo devices.\n");
133 		nopdev = 1;
134 	}
135 
136 	check_int(I_BUFCACHEPCT, "BUFCACHEPERCENT");
137 	check_int(I_NKMEMPG, "NKMEMPAGES");
138 
139 	init();
140 
141 	if (uflag) {
142 		if (ok) {
143 			if (strcmp(adjust((caddr_t)nl[P_VERSION].n_value),
144 			    kversion) != 0)
145 				ok = 1;
146 		}
147 		if (!ok) {
148 			printf("WARNING kernel mismatch. -u ignored.\n");
149 			printf("WARNING the running kernel version:\n");
150 			printf("%s", kversion);
151 		} else
152 			process_history(histlen, history);
153 	}
154 
155 	printf("%s", adjust((caddr_t)nl[P_VERSION].n_value));
156 
157 	if (config()) {
158 		if (force == 0 && outfile == NULL) {
159 			fprintf(stderr, "not forced\n");
160 			return (1);
161 		}
162 		if (outfile == NULL)
163 			outfile = file;
164 		if (ukc_mod_kernel == 0) {
165 			fprintf(stderr, "Kernel not modified\n");
166 			return (1);
167 		} else {
168 			printf ("Saving modified kernel.\n");
169 			savekernel(outfile);
170 		}
171 	}
172 	return(0);
173 }
174 
175 void
176 init(void)
177 {
178 	int i = 0, fd;
179 	struct cfdata *cd;
180 	short	*ln;
181 	int	*p;
182 
183 	if ((cd = get_cfdata(0)) == NULL)	/* get first item */
184 		errx(1, "failed to get first cfdata");
185 
186 	while (cd->cf_attach != 0) {
187 		maxdev = i;
188 		totdev = i;
189 
190 		ln = get_locnamp(cd->cf_locnames);
191 		while (*ln != -1) {
192 			if (*ln > maxlocnames)
193 				maxlocnames = *ln;
194 			ln++;
195 		}
196 		i++;
197 		cd++;
198 	}
199 
200 	while (cd->cf_attach == 0) {
201 		totdev = i;
202 		i++;
203 		cd++;
204 	}
205 
206 	totdev = totdev - 1;
207 
208 	if (nopdev == 0) {
209 		p = (int *)adjust((caddr_t)nl[I_PDEVSIZE].n_value);
210 		maxpseudo = *p;
211 	}
212 
213 	if ((fd = open("/dev/tty", O_RDWR)) < 0)
214 		fd = 2;
215 }
216