1 /* $OpenBSD: ukc.c,v 1.13 2004/06/08 20:59:29 mcbride 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 #include <sys/ioctl.h>
30
31 #include <err.h>
32 #include <kvm.h>
33 #include <fcntl.h>
34 #include <limits.h>
35 #include <nlist.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40
41 #include "ukc.h"
42 #include "exec.h"
43
44 __RCSID("$MirOS: src/usr.sbin/config/ukc.c,v 1.7 2012/09/02 15:16:29 tg Exp $");
45
46 void init(void);
47 void usage(void);
48
49 int ukc_mod_kernel = 0;
50 int ukc(char *, char *, int, int);
51
52 extern struct nlist knl[];
53
54 static void
check_int(int idx,const char * name)55 check_int(int idx, const char *name)
56 {
57 if (nl[idx].n_type == 0)
58 printf("WARNING this kernel doesn't support modification "
59 "of %s.\n", name);
60 }
61
62 int
ukc(char * file,char * outfile,int uflag,int force)63 ukc(char *file, char *outfile, int uflag, int force)
64 {
65 extern char *__progname;
66 int ret, i;
67 kvm_t *kd;
68 char errbuf[_POSIX2_LINE_MAX];
69 int histlen = 0, ok = 1;
70 char history[1024], kversion[1024];
71
72 if (file == NULL) {
73 fprintf(stderr, "%s: no file specified\n", __progname);
74 usage();
75 }
76
77 loadkernel(file);
78
79 ret = nlist(file, nl);
80
81 if (nl[P_ENTROPY].n_type != 0)
82 arc4random_pushb_fast((char *)adjust((caddr_t)nl[P_ENTROPY].n_value), 16);
83
84 if (uflag) {
85 if ((kd = kvm_openfiles(NULL,NULL,NULL,O_RDONLY, errbuf)) == 0)
86 errx(1, "kvm_openfiles: %s", errbuf);
87
88 if ((ret = kvm_nlist(kd, knl)) == -1)
89 errx(1, "kvm_nlist: %s", kvm_geterr(kd));
90
91 i = 0;
92 while (i < NLENTRIES) {
93 if (nl[i].n_type != knl[i].n_type ||
94 nl[i].n_desc != knl[i].n_desc ||
95 nl[i].n_value != knl[i].n_value)
96 ok = 0;
97 i++;
98 }
99
100 if (knl[I_HISTLEN].n_type != 0 && ok) {
101 if (kvm_read(kd, knl[I_HISTLEN].n_value, &histlen,
102 sizeof(histlen)) != sizeof(histlen))
103 warnx("cannot read %s: %s",
104 knl[I_HISTLEN].n_name,
105 kvm_geterr(kd));
106 }
107 if (knl[CA_HISTORY].n_type != 0 && ok) {
108 if (kvm_read(kd, knl[CA_HISTORY].n_value, history,
109 sizeof(history)) != sizeof(history))
110 warnx("cannot read %s: %s",
111 knl[CA_HISTORY].n_name,
112 kvm_geterr(kd));
113 }
114 if (knl[P_VERSION].n_type != 0 && ok) {
115 if (kvm_read(kd, knl[P_VERSION].n_value, kversion,
116 sizeof(kversion)) != sizeof(kversion))
117 warnx("cannot read %s: %s",
118 knl[P_VERSION].n_name,
119 kvm_geterr(kd));
120 }
121 }
122
123 if (nl[P_VERSION].n_type)
124 printf("%s", adjust((caddr_t)nl[P_VERSION].n_value));
125
126 if (force == 0 && outfile == NULL)
127 printf("warning: no output file specified\n");
128
129 if (nl[IA_EXTRALOC].n_type == 0 || nl[I_NEXTRALOC].n_type == 0 ||
130 nl[I_UEXTRALOC].n_type == 0 || nl[I_HISTLEN].n_type == 0 ||
131 nl[CA_HISTORY].n_type == 0) {
132 printf("\
133 WARNING this kernel doesn't contain all information needed!\n\
134 WARNING the commands add and change might not work.\n");
135 oldkernel = 1;
136 }
137
138 if (nl[P_PDEVNAMES].n_type == 0 ||
139 nl[I_PDEVSIZE].n_type == 0 ||
140 nl[S_PDEVINIT].n_type == 0) {
141 printf("\
142 WARNING this kernel doesn't support pseudo devices.\n");
143 nopdev = 1;
144 }
145
146 check_int(I_BUFCACHEPCT, "BUFCACHEPERCENT");
147 check_int(I_NKMEMPG, "NKMEMPAGES");
148 check_int(I_SHMSEG, "SHMSEG");
149 check_int(I_SHMMAXPGS, "SHMMAXPGS");
150
151 init();
152
153 if (uflag) {
154 if (ok) {
155 if (strcmp(adjust((caddr_t)nl[P_VERSION].n_value),
156 kversion) != 0)
157 ok = 1;
158 }
159 if (!ok) {
160 printf("WARNING kernel mismatch. -u ignored.\n");
161 printf("WARNING the running kernel version:\n");
162 printf("%s", kversion);
163 } else
164 process_history(histlen, history);
165 }
166
167 if (config()) {
168 if (force == 0 && outfile == NULL) {
169 fprintf(stderr, "not forced\n");
170 exit(1);
171 }
172 if (outfile == NULL)
173 outfile = file;
174 if (ukc_mod_kernel == 0) {
175 fprintf(stderr, "Kernel not modified\n");
176 if (nl[P_ENTROPY].n_type == 0)
177 return (0);
178 printf("Saving randomised kernel.\n");
179 } else
180 printf("Saving modified kernel.\n");
181 if (nl[P_ENTROPY].n_type != 0)
182 arc4random_buf((char *)adjust((caddr_t)nl[P_ENTROPY].n_value), 16);
183 savekernel(outfile);
184 }
185 return(0);
186 }
187
188 void
init(void)189 init(void)
190 {
191 int i = 0, fd;
192 struct cfdata *cd;
193 short *ln;
194 int *p;
195 #ifdef NOTDEF
196 struct winsize w;
197 #endif
198
199 cd = get_cfdata(0); /* get first item */
200 if (!cd) {
201 maxdev = totdev = 0;
202 goto no_cfdata;
203 }
204 while (cd->cf_attach != 0) {
205 maxdev = i;
206 totdev = i;
207
208 ln = get_locnamp(cd->cf_locnames);
209 while (*ln != -1) {
210 if (*ln > maxlocnames)
211 maxlocnames = *ln;
212 ln++;
213 }
214 i++;
215 cd++;
216 }
217
218 while (cd->cf_attach == 0) {
219 totdev = i;
220 i++;
221 cd++;
222 }
223
224 totdev = totdev - 1;
225
226 no_cfdata:
227 if (nopdev == 0) {
228 p = (int *)adjust((caddr_t)nl[I_PDEVSIZE].n_value);
229 maxpseudo = *p;
230 }
231
232 if ((fd = open("/dev/tty", O_RDWR)) < 0)
233 fd = 2;
234
235 #ifdef NOTDEF
236 if (ioctl(fd, TIOCGWINSZ, &w) == 0)
237 printf("row %d col %d\n", w.ws_row, w.ws_col);
238
239 if ((s = getenv("LINES")) != NULL)
240 sc_height = atoi(s);
241 else
242 sc_height = tgetnum("li");
243
244 if (sc_height <= 0)
245 sc_height = 24;
246
247 if ((s = getenv("COLUMNS")) != NULL)
248 sc_width = atoi(s);
249 else
250 sc_width = tgetnum("co");
251
252 if (sc_width <= 0)
253 sc_width = 80;
254 #endif
255 }
256