1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
|
/* febootstrap-supermin-helper reimplementation in C.
* Copyright (C) 2009-2010 Red Hat Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <getopt.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/time.h>
#include <assert.h>
#include <grp.h>
#include <pwd.h>
#include "error.h"
#include "xstrtol.h"
#include "helper.h"
struct timeval start_t;
int verbose = 0;
enum { HELP_OPTION = CHAR_MAX + 1 };
static const char *options = "f:g:k:u:vV";
static const struct option long_options[] = {
{ "help", 0, 0, HELP_OPTION },
{ "format", required_argument, 0, 'f' },
{ "group", 0, 0, 'g' },
{ "kmods", required_argument, 0, 'k' },
{ "user", 0, 0, 'u' },
{ "verbose", 0, 0, 'v' },
{ "version", 0, 0, 'V' },
{ 0, 0, 0, 0 }
};
static void
usage (FILE *f, const char *progname)
{
fprintf (f,
"%s: build the supermin appliance on the fly\n"
"\n"
"Usage:\n"
" %s [-options] inputs [...] host_cpu kernel initrd\n"
" %s -f ext2 inputs [...] host_cpu kernel initrd appliance\n"
" %s -f checksum inputs [...] host_cpu\n"
" %s --help\n"
" %s --version\n"
"\n"
"This script is used by febootstrap to build the supermin appliance\n"
"(kernel and initrd output files). You should NOT need to run this\n"
"program directly except if you are debugging tricky supermin\n"
"appliance problems.\n"
"\n"
"NB: The kernel and initrd parameters are OUTPUT parameters. If\n"
"those files exist, they are overwritten by the output.\n"
"\n"
"Options:\n"
" --help\n"
" Display this help text and exit.\n"
" -f cpio|ext2|checksum | --format cpio|ext2|checksum\n"
" Specify output format (default: cpio).\n"
" -u user\n"
" The user name or uid the appliance will run as. Use of this\n"
" option requires root privileges.\n"
" -g group\n"
" The group name or gid the appliance will run as. Use of\n"
" this option requires root privileges.\n"
" -k file | --kmods file\n"
" Specify kernel module whitelist.\n"
" --verbose | -v\n"
" Enable verbose messages (give multiple times for more verbosity).\n"
" --version | -V\n"
" Display version number and exit.\n",
progname, progname, progname, progname, progname, progname);
}
static uid_t
parseuser (const char *id, const char *progname)
{
struct passwd *pwd;
errno = 0;
pwd = getpwnam (id);
if (NULL == pwd) {
if (errno != 0) {
fprintf (stderr, "Error looking up user: %m\n");
exit (EXIT_FAILURE);
}
long val;
int err = xstrtol (id, NULL, 10, &val, "");
if (err != LONGINT_OK) {
fprintf (stderr, "%s is not a valid user name or uid\n", id);
usage (stderr, progname);
exit (EXIT_FAILURE);
}
return (uid_t) val;
}
return pwd->pw_uid;
}
static gid_t
parsegroup (const char *id, const char *progname)
{
struct group *grp;
errno = 0;
grp = getgrnam (id);
if (NULL == grp) {
if (errno != 0) {
fprintf (stderr, "Error looking up group: %m\n");
exit (EXIT_FAILURE);
}
long val;
int err = xstrtol (id, NULL, 10, &val, "");
if (err != LONGINT_OK) {
fprintf (stderr, "%s is not a valid group name or gid\n", id);
usage (stderr, progname);
exit (EXIT_FAILURE);
}
return (gid_t) val;
}
return grp->gr_gid;
}
int
main (int argc, char *argv[])
{
/* First thing: start the clock. */
gettimeofday (&start_t, NULL);
const char *format = "cpio";
const char *whitelist = NULL;
uid_t euid = geteuid ();
gid_t egid = getegid ();
/* Command line arguments. */
for (;;) {
int c = getopt_long (argc, argv, options, long_options, NULL);
if (c == -1) break;
switch (c) {
case HELP_OPTION:
usage (stdout, argv[0]);
exit (EXIT_SUCCESS);
case 'f':
format = optarg;
break;
case 'u':
euid = parseuser (optarg, argv[0]);
break;
case 'g':
egid = parsegroup (optarg, argv[0]);
break;
case 'k':
whitelist = optarg;
break;
case 'v':
verbose++;
break;
case 'V':
printf (PACKAGE_NAME " " PACKAGE_VERSION "\n");
exit (EXIT_SUCCESS);
default:
usage (stderr, argv[0]);
exit (EXIT_FAILURE);
}
}
/* We need to set the real, not effective, uid here to work round a
* misfeature in bash. bash will automatically reset euid to uid when
* invoked. As shell is used in places by febootstrap-supermin-helper, this
* results in code running with varying privilege. */
uid_t uid = getuid ();
gid_t gid = getgid ();
if (uid != euid || gid != egid) {
if (uid != 0) {
fprintf (stderr, "The -u and -g options require root privileges.\n");
usage (stderr, argv[0]);
exit (EXIT_FAILURE);
}
/* Need to become root first because setgid and setuid require it */
if (seteuid (0) == -1) {
perror ("seteuid");
exit (EXIT_FAILURE);
}
/* Set gid and uid to command-line parameters */
if (setgid (egid) == -1) {
perror ("setgid");
exit (EXIT_FAILURE);
}
if (setuid (euid) == -1) {
perror ("setuid");
exit (EXIT_FAILURE);
}
}
/* Select the correct writer module. */
struct writer *writer;
int nr_outputs;
if (strcmp (format, "cpio") == 0) {
writer = &cpio_writer;
nr_outputs = 2; /* kernel and appliance (== initrd) */
}
else if (strcmp (format, "ext2") == 0) {
writer = &ext2_writer;
nr_outputs = 3; /* kernel, initrd, appliance */
}
else if (strcmp (format, "checksum") == 0) {
writer = &checksum_writer;
nr_outputs = 0; /* (none) */
}
else {
fprintf (stderr,
"%s: incorrect output format (-f): must be cpio|ext2|checksum\n",
argv[0]);
exit (EXIT_FAILURE);
}
/* [optind .. optind+nr_inputs-1] hostcpu [argc-nr_outputs-1 .. argc-1]
* <---- nr_inputs ----> 1 <---- nr_outputs ---->
*/
char **inputs = &argv[optind];
int nr_inputs = argc - nr_outputs - 1 - optind;
char **outputs = &argv[optind+nr_inputs+1];
/*assert (outputs [nr_outputs] == NULL);
assert (inputs [nr_inputs + 1 + nr_outputs] == NULL);*/
if (nr_inputs < 1) {
fprintf (stderr, "%s: not enough files specified on the command line\n",
argv[0]);
exit (EXIT_FAILURE);
}
/* See: https://bugzilla.redhat.com/show_bug.cgi?id=558593 */
const char *hostcpu = outputs[-1];
/* Output files. */
const char *kernel = NULL, *initrd = NULL, *appliance = NULL;
if (nr_outputs > 0)
kernel = outputs[0];
if (nr_outputs > 1)
initrd = appliance = outputs[1];
if (nr_outputs > 2)
appliance = outputs[2];
if (verbose) {
print_timestamped_message ("whitelist = %s, "
"host_cpu = %s, "
"kernel = %s, "
"initrd = %s, "
"appliance = %s",
whitelist ? : "(not specified)",
hostcpu, kernel, initrd, appliance);
int i;
for (i = 0; i < nr_inputs; ++i)
print_timestamped_message ("inputs[%d] = %s", i, inputs[i]);
}
/* Remove the output files if they exist. */
if (kernel)
unlink (kernel);
if (initrd)
unlink (initrd);
if (appliance && initrd != appliance)
unlink (appliance);
/* Create kernel output file. */
const char *modpath = create_kernel (hostcpu, kernel);
if (verbose)
print_timestamped_message ("finished creating kernel");
/* Create the appliance. */
create_appliance (hostcpu, inputs, nr_inputs, whitelist, modpath,
initrd, appliance, writer);
if (verbose)
print_timestamped_message ("finished creating appliance");
exit (EXIT_SUCCESS);
}
|