2011-11-07 00:07:19 +05:30
|
|
|
/*
|
2021-12-05 21:05:27 +05:30
|
|
|
* SPDX-FileCopyrightText: 2011 , Julian Pidancet
|
|
|
|
* SPDX-FileCopyrightText: 2011 , Nicolas François
|
2011-11-07 00:07:19 +05:30
|
|
|
*
|
2021-12-05 21:05:27 +05:30
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
2011-11-07 00:07:19 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#ident "$Id$"
|
|
|
|
|
2011-11-17 01:04:39 +05:30
|
|
|
#include <stdio.h>
|
2011-11-07 00:07:19 +05:30
|
|
|
#include <assert.h>
|
|
|
|
#include "defines.h"
|
|
|
|
#include "prototypes.h"
|
2011-11-20 03:21:52 +05:30
|
|
|
/*@-exitarg@*/
|
2011-11-07 00:07:19 +05:30
|
|
|
#include "exitcodes.h"
|
2021-11-29 05:07:53 +05:30
|
|
|
#include "shadowlog.h"
|
2011-11-07 00:07:19 +05:30
|
|
|
|
|
|
|
static void change_root (const char* newroot);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* process_root_flag - chroot if given the --root option
|
|
|
|
*
|
|
|
|
* This shall be called before accessing the passwd, group, shadow,
|
|
|
|
* gshadow, useradd's default, login.defs files (non exhaustive list)
|
|
|
|
* or authenticating the caller.
|
|
|
|
*
|
|
|
|
* The audit, syslog, or locale files shall be open before
|
|
|
|
*/
|
|
|
|
extern void process_root_flag (const char* short_opt, int argc, char **argv)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Parse the command line options.
|
|
|
|
*/
|
|
|
|
int i;
|
2020-02-08 03:27:12 +05:30
|
|
|
const char *newroot = NULL, *val;
|
2011-11-07 00:07:19 +05:30
|
|
|
|
|
|
|
for (i = 0; i < argc; i++) {
|
2020-02-08 03:27:12 +05:30
|
|
|
val = NULL;
|
2011-11-07 00:07:19 +05:30
|
|
|
if ( (strcmp (argv[i], "--root") == 0)
|
2020-02-08 03:27:12 +05:30
|
|
|
|| ((strncmp (argv[i], "--root=", 7) == 0)
|
|
|
|
&& (val = argv[i] + 7))
|
2011-11-07 00:07:19 +05:30
|
|
|
|| (strcmp (argv[i], short_opt) == 0)) {
|
|
|
|
if (NULL != newroot) {
|
2021-11-29 05:07:53 +05:30
|
|
|
fprintf (log_get_logfd(),
|
2011-11-07 00:07:19 +05:30
|
|
|
_("%s: multiple --root options\n"),
|
2021-11-29 05:07:53 +05:30
|
|
|
log_get_progname());
|
2011-11-07 00:07:19 +05:30
|
|
|
exit (E_BAD_ARG);
|
|
|
|
}
|
|
|
|
|
2020-02-08 03:27:12 +05:30
|
|
|
if (val) {
|
|
|
|
newroot = val;
|
|
|
|
} else if (i + 1 == argc) {
|
2021-11-29 05:07:53 +05:30
|
|
|
fprintf (log_get_logfd(),
|
2011-11-07 00:07:19 +05:30
|
|
|
_("%s: option '%s' requires an argument\n"),
|
2021-11-29 05:07:53 +05:30
|
|
|
log_get_progname(), argv[i]);
|
2011-11-07 00:07:19 +05:30
|
|
|
exit (E_BAD_ARG);
|
2020-02-08 03:27:12 +05:30
|
|
|
} else {
|
|
|
|
newroot = argv[++ i];
|
2011-11-07 00:07:19 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NULL != newroot) {
|
|
|
|
change_root (newroot);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void change_root (const char* newroot)
|
|
|
|
{
|
2011-11-11 17:39:58 +05:30
|
|
|
/* Drop privileges */
|
2011-11-13 21:54:49 +05:30
|
|
|
if ( (setregid (getgid (), getgid ()) != 0)
|
|
|
|
|| (setreuid (getuid (), getuid ()) != 0)) {
|
2021-11-29 05:07:53 +05:30
|
|
|
fprintf (log_get_logfd(), _("%s: failed to drop privileges (%s)\n"),
|
|
|
|
log_get_progname(), strerror (errno));
|
2011-11-11 17:39:58 +05:30
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2011-11-07 00:07:19 +05:30
|
|
|
if ('/' != newroot[0]) {
|
2021-11-29 05:07:53 +05:30
|
|
|
fprintf (log_get_logfd(),
|
2022-07-27 20:36:36 +05:30
|
|
|
_("%s: invalid chroot path '%s', only absolute paths are supported.\n"),
|
2021-11-29 05:07:53 +05:30
|
|
|
log_get_progname(), newroot);
|
2011-11-07 00:07:19 +05:30
|
|
|
exit (E_BAD_ARG);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (access (newroot, F_OK) != 0) {
|
2021-11-29 05:07:53 +05:30
|
|
|
fprintf(log_get_logfd(),
|
2011-11-11 17:39:58 +05:30
|
|
|
_("%s: cannot access chroot directory %s: %s\n"),
|
2021-11-29 05:07:53 +05:30
|
|
|
log_get_progname(), newroot, strerror (errno));
|
2011-11-07 00:07:19 +05:30
|
|
|
exit (E_BAD_ARG);
|
|
|
|
}
|
2013-07-29 14:35:16 +05:30
|
|
|
|
2023-05-23 17:27:50 +05:30
|
|
|
if (chroot (newroot) != 0) {
|
2021-11-29 05:07:53 +05:30
|
|
|
fprintf(log_get_logfd(),
|
2023-05-23 17:27:50 +05:30
|
|
|
_("%s: unable to chroot to directory %s: %s\n"),
|
2021-11-29 05:07:53 +05:30
|
|
|
log_get_progname(), newroot, strerror (errno));
|
2013-07-29 14:35:16 +05:30
|
|
|
exit (E_BAD_ARG);
|
|
|
|
}
|
|
|
|
|
2023-05-23 17:27:50 +05:30
|
|
|
if (chdir ("/") != 0) {
|
2021-11-29 05:07:53 +05:30
|
|
|
fprintf(log_get_logfd(),
|
2023-05-23 17:27:50 +05:30
|
|
|
_("%s: cannot chdir in chroot directory %s: %s\n"),
|
2021-11-29 05:07:53 +05:30
|
|
|
log_get_progname(), newroot, strerror (errno));
|
2011-11-07 00:07:19 +05:30
|
|
|
exit (E_BAD_ARG);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|