]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/dumpon/dumpon.c
Import tzdata 2017c
[FreeBSD/FreeBSD.git] / sbin / dumpon / dumpon.c
1 /*
2  * Copyright (c) 1980, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #if 0
31 #ifndef lint
32 static const char copyright[] =
33 "@(#) Copyright (c) 1980, 1993\n\
34         The Regents of the University of California.  All rights reserved.\n";
35 #endif /* not lint */
36
37 #ifndef lint
38 static char sccsid[] = "From: @(#)swapon.c      8.1 (Berkeley) 6/5/93";
39 #endif /* not lint */
40 #endif
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
43
44 #include <sys/param.h>
45 #include <sys/capsicum.h>
46 #include <sys/disk.h>
47 #include <sys/sysctl.h>
48
49 #include <assert.h>
50 #include <err.h>
51 #include <errno.h>
52 #include <fcntl.h>
53 #include <paths.h>
54 #include <stdbool.h>
55 #include <stdint.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <sysexits.h>
60 #include <unistd.h>
61
62 #ifdef HAVE_CRYPTO
63 #include <openssl/err.h>
64 #include <openssl/pem.h>
65 #include <openssl/rsa.h>
66 #endif
67
68 static int      verbose;
69
70 static void
71 usage(void)
72 {
73         fprintf(stderr, "%s\n%s\n%s\n",
74             "usage: dumpon [-v] [-k public_key_file] [-z] special_file",
75             "       dumpon [-v] off",
76             "       dumpon [-v] -l");
77         exit(EX_USAGE);
78 }
79
80 static void
81 check_size(int fd, const char *fn)
82 {
83         int name[] = { CTL_HW, HW_PHYSMEM };
84         size_t namelen = nitems(name);
85         unsigned long physmem;
86         size_t len;
87         off_t mediasize;
88         int minidump;
89
90         len = sizeof(minidump);
91         if (sysctlbyname("debug.minidump", &minidump, &len, NULL, 0) == 0 &&
92             minidump == 1)
93                 return;
94         len = sizeof(physmem);
95         if (sysctl(name, namelen, &physmem, &len, NULL, 0) != 0)
96                 err(EX_OSERR, "can't get memory size");
97         if (ioctl(fd, DIOCGMEDIASIZE, &mediasize) != 0)
98                 err(EX_OSERR, "%s: can't get size", fn);
99         if ((uintmax_t)mediasize < (uintmax_t)physmem) {
100                 if (verbose)
101                         printf("%s is smaller than physical memory\n", fn);
102                 exit(EX_IOERR);
103         }
104 }
105
106 #ifdef HAVE_CRYPTO
107 static void
108 genkey(const char *pubkeyfile, struct diocskerneldump_arg *kda)
109 {
110         FILE *fp;
111         RSA *pubkey;
112
113         assert(pubkeyfile != NULL);
114         assert(kda != NULL);
115
116         fp = NULL;
117         pubkey = NULL;
118
119         fp = fopen(pubkeyfile, "r");
120         if (fp == NULL)
121                 err(1, "Unable to open %s", pubkeyfile);
122
123         if (cap_enter() < 0 && errno != ENOSYS)
124                 err(1, "Unable to enter capability mode");
125
126         pubkey = RSA_new();
127         if (pubkey == NULL) {
128                 errx(1, "Unable to allocate an RSA structure: %s",
129                     ERR_error_string(ERR_get_error(), NULL));
130         }
131
132         pubkey = PEM_read_RSA_PUBKEY(fp, &pubkey, NULL, NULL);
133         fclose(fp);
134         fp = NULL;
135         if (pubkey == NULL)
136                 errx(1, "Unable to read data from %s.", pubkeyfile);
137
138         kda->kda_encryptedkeysize = RSA_size(pubkey);
139         if (kda->kda_encryptedkeysize > KERNELDUMP_ENCKEY_MAX_SIZE) {
140                 errx(1, "Public key has to be at most %db long.",
141                     8 * KERNELDUMP_ENCKEY_MAX_SIZE);
142         }
143
144         kda->kda_encryptedkey = calloc(1, kda->kda_encryptedkeysize);
145         if (kda->kda_encryptedkey == NULL)
146                 err(1, "Unable to allocate encrypted key");
147
148         kda->kda_encryption = KERNELDUMP_ENC_AES_256_CBC;
149         arc4random_buf(kda->kda_key, sizeof(kda->kda_key));
150         if (RSA_public_encrypt(sizeof(kda->kda_key), kda->kda_key,
151             kda->kda_encryptedkey, pubkey,
152             RSA_PKCS1_PADDING) != (int)kda->kda_encryptedkeysize) {
153                 errx(1, "Unable to encrypt the one-time key.");
154         }
155         RSA_free(pubkey);
156 }
157 #endif
158
159 static void
160 listdumpdev(void)
161 {
162         char dumpdev[PATH_MAX];
163         size_t len;
164         const char *sysctlname = "kern.shutdown.dumpdevname";
165
166         len = sizeof(dumpdev);
167         if (sysctlbyname(sysctlname, &dumpdev, &len, NULL, 0) != 0) {
168                 if (errno == ENOMEM) {
169                         err(EX_OSERR, "Kernel returned too large of a buffer for '%s'\n",
170                                 sysctlname);
171                 } else {
172                         err(EX_OSERR, "Sysctl get '%s'\n", sysctlname);
173                 }
174         }
175         if (verbose) {
176                 printf("kernel dumps on ");
177         }
178         if (strlen(dumpdev) == 0) {
179                 printf("%s\n", _PATH_DEVNULL);
180         } else {
181                 printf("%s\n", dumpdev);
182         }
183 }
184
185 int
186 main(int argc, char *argv[])
187 {
188         struct diocskerneldump_arg kda;
189         const char *pubkeyfile;
190         int ch;
191         int i, fd;
192         int do_listdumpdev = 0;
193         bool enable, gzip;
194
195         gzip = false;
196         pubkeyfile = NULL;
197
198         while ((ch = getopt(argc, argv, "k:lvz")) != -1)
199                 switch((char)ch) {
200                 case 'k':
201                         pubkeyfile = optarg;
202                         break;
203                 case 'l':
204                         do_listdumpdev = 1;
205                         break;
206                 case 'v':
207                         verbose = 1;
208                         break;
209                 case 'z':
210                         gzip = true;
211                         break;
212                 default:
213                         usage();
214                 }
215
216         argc -= optind;
217         argv += optind;
218
219         if (do_listdumpdev) {
220                 listdumpdev();
221                 exit(EX_OK);
222         }
223
224         if (argc != 1)
225                 usage();
226
227         enable = (strcmp(argv[0], "off") != 0);
228 #ifndef HAVE_CRYPTO
229         if (pubkeyfile != NULL) {
230                 enable = false;
231                 warnx("Unable to use the public key. Recompile dumpon with OpenSSL support.");
232         }
233 #endif
234
235         if (enable) {
236                 char tmp[PATH_MAX];
237                 char *dumpdev;
238
239                 if (strncmp(argv[0], _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0) {
240                         dumpdev = argv[0];
241                 } else {
242                         i = snprintf(tmp, PATH_MAX, "%s%s", _PATH_DEV, argv[0]);
243                         if (i < 0) {
244                                 err(EX_OSERR, "%s", argv[0]);
245                         } else if (i >= PATH_MAX) {
246                                 errno = EINVAL;
247                                 err(EX_DATAERR, "%s", argv[0]);
248                         }
249                         dumpdev = tmp;
250                 }
251                 fd = open(dumpdev, O_RDONLY);
252                 if (fd < 0)
253                         err(EX_OSFILE, "%s", dumpdev);
254
255                 if (!gzip)
256                         check_size(fd, dumpdev);
257
258                 bzero(&kda, sizeof(kda));
259                 kda.kda_enable = 0;
260                 i = ioctl(fd, DIOCSKERNELDUMP, &kda);
261                 explicit_bzero(&kda, sizeof(kda));
262
263 #ifdef HAVE_CRYPTO
264                 if (pubkeyfile != NULL)
265                         genkey(pubkeyfile, &kda);
266 #endif
267
268                 kda.kda_enable = 1;
269                 kda.kda_compression = gzip ? KERNELDUMP_COMP_GZIP :
270                     KERNELDUMP_COMP_NONE;
271                 i = ioctl(fd, DIOCSKERNELDUMP, &kda);
272                 explicit_bzero(kda.kda_encryptedkey, kda.kda_encryptedkeysize);
273                 free(kda.kda_encryptedkey);
274                 explicit_bzero(&kda, sizeof(kda));
275                 if (i == 0 && verbose)
276                         printf("kernel dumps on %s\n", dumpdev);
277         } else {
278                 fd = open(_PATH_DEVNULL, O_RDONLY);
279                 if (fd < 0)
280                         err(EX_OSFILE, "%s", _PATH_DEVNULL);
281
282                 kda.kda_enable = 0;
283                 i = ioctl(fd, DIOCSKERNELDUMP, &kda);
284                 explicit_bzero(&kda, sizeof(kda));
285                 if (i == 0 && verbose)
286                         printf("kernel dumps disabled\n");
287         }
288         if (i < 0)
289                 err(EX_OSERR, "ioctl(DIOCSKERNELDUMP)");
290
291         exit (0);
292 }