mirror of
https://github.com/kernelkit/infix.git
synced 2026-08-01 21:33:02 +02:00
Add support for returning a device to factory defaults
This change adds a `factory` tool, and login shell, that schedules a
reset of all writable (overlay) filesystems. The tool is set up with
the suid gid flag to allow all members of the wheel group to perform
the reset.
The login method only allows reset from /dev/console, to prevent any
malicious reset over SSH. This should be further constrained later
when PAM is introduced.
To initiate factory reset from the login prompt, use login/pass:
factory/reset
To initiate factory reset from the shell, call
factory
Both methods are interactive by default, but two command line options
can be used to modify the behavior:
-r Skip reboot, and "reboot now?" question
-y Assume yes to all questions, for non-interactive use
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2021 Westermo Network Technologies
|
||||
Copyright (c) 2023 Joachim Wiberg
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
@@ -0,0 +1,12 @@
|
||||
CFLAGS += -Wall -Wextra -Werror
|
||||
|
||||
all: factory
|
||||
|
||||
clean:
|
||||
-rm factory
|
||||
|
||||
distclean: clean
|
||||
-rm *~
|
||||
|
||||
install:
|
||||
install -D factory $(DESTDIR)/sbin/
|
||||
@@ -0,0 +1,148 @@
|
||||
#include <err.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <getopt.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <termios.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#define RESETME "/rw/infix/.reset"
|
||||
#define touch(f) mknod((f), S_IFREG|0644, 0)
|
||||
|
||||
char rawgetch(void)
|
||||
{
|
||||
struct termios saved, c;
|
||||
char key;
|
||||
|
||||
if (tcgetattr(fileno(stdin), &saved) < 0)
|
||||
return -1;
|
||||
|
||||
c = saved;
|
||||
c.c_lflag &= ~ICANON;
|
||||
c.c_lflag &= ~ECHO;
|
||||
c.c_cc[VMIN] = 1;
|
||||
c.c_cc[VTIME] = 0;
|
||||
|
||||
if (tcsetattr(fileno(stdin), TCSANOW, &c) < 0) {
|
||||
tcsetattr(fileno(stdin), TCSANOW, &saved);
|
||||
return -1;
|
||||
}
|
||||
|
||||
key = getchar();
|
||||
tcsetattr(fileno(stdin), TCSANOW, &saved);
|
||||
|
||||
return key;
|
||||
}
|
||||
|
||||
int yorn(const char *prompt)
|
||||
{
|
||||
char yorn;
|
||||
|
||||
fputs(prompt, stderr);
|
||||
|
||||
yorn = rawgetch();
|
||||
fprintf(stderr, "%c\n", yorn);
|
||||
if (yorn != 'y' && yorn != 'Y')
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int usage(int rc)
|
||||
{
|
||||
printf("usage: factory [opts]\n"
|
||||
"\n"
|
||||
"Options:\n"
|
||||
" -h, --help This help text\n"
|
||||
" -r, --no-reboot Don't reboot. Reboot manually to activate.\n"
|
||||
" -y, --assume-yes Automatic yes to prompts; assume \"yes\" as answer to all\n"
|
||||
" prompts and run non-interactively\n"
|
||||
"\n"
|
||||
"Note: this program initiates a factory reset by raising a flag and rebooting\n"
|
||||
" the system. When it comes back up it safely removes all the OverlayFS\n"
|
||||
" worker/upper directories for /etc, /home, and /var\n");
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
static int run(const char *cmd)
|
||||
{
|
||||
int status = system(cmd);
|
||||
int rc;
|
||||
|
||||
rc = WEXITSTATUS(status);
|
||||
|
||||
if (WIFEXITED(status))
|
||||
return rc;
|
||||
|
||||
if (WIFSIGNALED(status)) {
|
||||
if (rc == 0)
|
||||
rc = 1; /* adjust, we were aborted */
|
||||
}
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
struct option long_opts[] = {
|
||||
{ "help", 0, NULL, 'h' },
|
||||
{ "no-reboot", 0, NULL, 'r' },
|
||||
{ "assume-yes", 0, NULL, 'y' },
|
||||
{ NULL, 0, NULL, 0 }
|
||||
};
|
||||
int reboot = 1;
|
||||
int yes = 0;
|
||||
char *tty;
|
||||
int c;
|
||||
|
||||
while ((c = getopt_long(argc, argv, "h?ry", long_opts, NULL)) != EOF) {
|
||||
switch (c) {
|
||||
case 'h':
|
||||
case '?':
|
||||
return usage(0);
|
||||
|
||||
case 'r':
|
||||
reboot = 0;
|
||||
break;
|
||||
|
||||
case 'y':
|
||||
yes = 1;
|
||||
break;
|
||||
|
||||
default:
|
||||
return usage(1);
|
||||
}
|
||||
}
|
||||
|
||||
tty = ttyname(STDIN_FILENO);
|
||||
if (!tty && errno == ENOTTY)
|
||||
yes = 1;
|
||||
|
||||
if (argv[0][0] == '-' && tty && strcmp(tty, "/dev/console"))
|
||||
errx(1, "factory reset only allowed from console login!");
|
||||
|
||||
if (yes || yorn("Factory reset device (y/N)? ")) {
|
||||
if (touch(RESETME) && errno != EEXIST)
|
||||
err(1, "failed");
|
||||
|
||||
warnx("scheduled factory reset on next boot.");
|
||||
if (reboot && (yes || yorn("Reboot now to perform reset, (y/N)? ")))
|
||||
return run("/sbin/reboot");
|
||||
|
||||
warnx("remember to reboot the system to perform the factory reset.");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Local Variables:
|
||||
* indent-tabs-mode: t
|
||||
* c-file-style: "linux"
|
||||
* End:
|
||||
*/
|
||||
Reference in New Issue
Block a user