1
0
mirror of https://github.com/topjohnwu/Magisk.git synced 2024-12-20 23:17:37 +00:00

231 lines
5.7 KiB
C++
Raw Normal View History

/*
2017-04-15 03:21:31 +08:00
* Copyright 2017, John Wu (@topjohnwu)
* Copyright 2015, Pierre-Hugues Husson <phh@phh.me>
* Copyright 2010, Adam Shanks (@ChainsDD)
* Copyright 2008, Zinx Verituse (@zinxv)
*/
/* su.c - The main function running in the daemon
*/
#include <stdio.h>
#include <stdlib.h>
2017-04-15 03:21:31 +08:00
#include <string.h>
#include <unistd.h>
#include <getopt.h>
2017-04-15 03:21:31 +08:00
#include <fcntl.h>
#include <pwd.h>
2017-04-15 03:21:31 +08:00
#include <errno.h>
#include <signal.h>
2017-07-08 01:12:47 +08:00
#include <sched.h>
#include <sys/types.h>
2017-04-15 03:21:31 +08:00
#include <sys/stat.h>
2019-02-10 03:57:51 -05:00
#include <daemon.h>
#include <utils.h>
#include <flags.h>
2017-04-15 03:21:31 +08:00
#include "su.h"
2018-10-04 04:59:51 -04:00
#include "pts.h"
2018-11-20 04:40:42 -05:00
int quit_signals[] = { SIGALRM, SIGABRT, SIGHUP, SIGPIPE, SIGQUIT, SIGTERM, SIGINT, 0 };
2018-10-12 21:46:09 -04:00
2017-04-15 03:21:31 +08:00
static void usage(int status) {
FILE *stream = (status == EXIT_SUCCESS) ? stdout : stderr;
fprintf(stream,
2019-02-12 05:17:02 -05:00
FULL_VER(MagiskSU) "\n\n"
"Usage: su [options] [-] [user [argument...]]\n\n"
2017-04-15 03:21:31 +08:00
"Options:\n"
" -c, --command COMMAND pass COMMAND to the invoked shell\n"
" -h, --help display this help message and exit\n"
" -, -l, --login pretend the shell to be a login shell\n"
" -m, -p,\n"
" --preserve-environment preserve the entire environment\n"
2017-04-15 03:21:31 +08:00
" -s, --shell SHELL use SHELL instead of the default " DEFAULT_SHELL "\n"
" -v, --version display version number and exit\n"
" -V display version code and exit\n"
2017-07-08 01:12:47 +08:00
" -mm, -M,\n"
" --mount-master force run in the global mount namespace\n");
2018-10-04 04:59:51 -04:00
exit(status);
}
2017-04-15 03:21:31 +08:00
static char *concat_commands(int argc, char *argv[]) {
char command[ARG_MAX];
command[0] = '\0';
for (int i = optind - 1; i < argc; ++i) {
if (command[0])
2017-04-15 03:21:31 +08:00
sprintf(command, "%s %s", command, argv[i]);
else
strcpy(command, argv[i]);
2017-04-15 03:21:31 +08:00
}
return strdup(command);
}
2018-10-04 04:59:51 -04:00
static void sighandler(int sig) {
restore_stdin();
// Assume we'll only be called before death
// See note before sigaction() in set_stdin_raw()
//
// Now, close all standard I/O to cause the pumps
// to exit so we can continue and retrieve the exit
// code
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
// Put back all the default handlers
struct sigaction act;
memset(&act, 0, sizeof(act));
act.sa_handler = SIG_DFL;
for (int i = 0; quit_signals[i]; ++i) {
2019-06-30 19:09:31 -07:00
sigaction(quit_signals[i], &act, nullptr);
2017-04-15 03:21:31 +08:00
}
}
2018-10-12 21:46:09 -04:00
static void setup_sighandlers(void (*handler)(int)) {
struct sigaction act;
memset(&act, 0, sizeof(act));
act.sa_handler = handler;
for (int i = 0; quit_signals[i]; ++i) {
2019-06-30 19:09:31 -07:00
sigaction(quit_signals[i], &act, nullptr);
2018-10-12 21:46:09 -04:00
}
}
2018-10-04 04:59:51 -04:00
int su_client_main(int argc, char *argv[]) {
int c;
struct option long_opts[] = {
2019-06-30 19:09:31 -07:00
{ "command", required_argument, nullptr, 'c' },
{ "help", no_argument, nullptr, 'h' },
{ "login", no_argument, nullptr, 'l' },
{ "preserve-environment", no_argument, nullptr, 'p' },
{ "shell", required_argument, nullptr, 's' },
{ "version", no_argument, nullptr, 'v' },
{ "context", required_argument, nullptr, 'z' },
{ "mount-master", no_argument, nullptr, 'M' },
{ nullptr, 0, nullptr, 0 },
2018-10-04 04:59:51 -04:00
};
2018-11-04 03:38:06 -05:00
su_request su_req;
2018-10-04 04:59:51 -04:00
for (int i = 0; i < argc; i++) {
// Replace -cn with -z, -mm with -M for supporting getopt_long
if (strcmp(argv[i], "-cn") == 0)
strcpy(argv[i], "-z");
else if (strcmp(argv[i], "-mm") == 0)
strcpy(argv[i], "-M");
}
2017-04-15 03:21:31 +08:00
2019-06-30 19:09:31 -07:00
while ((c = getopt_long(argc, argv, "c:hlmps:Vvuz:M", long_opts, nullptr)) != -1) {
2017-04-15 03:21:31 +08:00
switch (c) {
2018-10-04 04:59:51 -04:00
case 'c':
su_req.command = concat_commands(argc, argv);
optind = argc;
break;
case 'h':
usage(EXIT_SUCCESS);
break;
case 'l':
2018-11-04 03:38:06 -05:00
su_req.login = true;
2018-10-04 04:59:51 -04:00
break;
case 'm':
case 'p':
2018-11-04 03:38:06 -05:00
su_req.keepenv = true;
2018-10-04 04:59:51 -04:00
break;
case 's':
su_req.shell = optarg;
break;
case 'V':
printf("%d\n", MAGISK_VER_CODE);
exit(EXIT_SUCCESS);
case 'v':
2019-02-12 05:17:02 -05:00
printf("%s\n", MAGISK_VERSION ":MAGISKSU");
2018-10-04 04:59:51 -04:00
exit(EXIT_SUCCESS);
case 'z':
// Do nothing, placed here for legacy support :)
break;
case 'M':
2018-11-04 03:38:06 -05:00
su_req.mount_master = true;
2018-10-04 04:59:51 -04:00
break;
default:
/* Bionic getopt_long doesn't terminate its error output by newline */
fprintf(stderr, "\n");
usage(2);
2017-04-15 03:21:31 +08:00
}
}
if (optind < argc && strcmp(argv[optind], "-") == 0) {
2018-11-04 03:38:06 -05:00
su_req.login = true;
2017-04-15 03:21:31 +08:00
optind++;
}
/* username or uid */
if (optind < argc) {
2017-04-15 03:21:31 +08:00
struct passwd *pw;
pw = getpwnam(argv[optind]);
if (pw)
2018-10-04 04:59:51 -04:00
su_req.uid = pw->pw_uid;
else
2019-03-07 20:31:35 -05:00
su_req.uid = parse_int(argv[optind]);
2017-04-15 03:21:31 +08:00
optind++;
}
2018-10-04 04:59:51 -04:00
char pts_slave[PATH_MAX];
int ptmx, fd;
2017-07-08 01:12:47 +08:00
2018-10-04 04:59:51 -04:00
// Connect to client
fd = connect_daemon();
2017-07-08 01:12:47 +08:00
2018-10-04 04:59:51 -04:00
// Tell the daemon we are su
write_int(fd, SUPERUSER);
2017-04-15 03:21:31 +08:00
2019-07-07 12:20:19 -07:00
// Send su_request
xwrite(fd, &su_req, sizeof(su_req_base));
write_string(fd, su_req.shell);
write_string(fd, su_req.command);
2018-10-20 16:12:08 -04:00
// Wait for ack from daemon
if (read_int(fd)) {
// Fast fail
fprintf(stderr, "%s\n", strerror(EACCES));
2019-07-07 12:20:19 -07:00
return EACCES;
2018-10-20 16:12:08 -04:00
}
2018-10-04 04:59:51 -04:00
// Determine which one of our streams are attached to a TTY
int atty = 0;
if (isatty(STDIN_FILENO)) atty |= ATTY_IN;
if (isatty(STDOUT_FILENO)) atty |= ATTY_OUT;
if (isatty(STDERR_FILENO)) atty |= ATTY_ERR;
2017-04-15 03:21:31 +08:00
2018-10-04 04:59:51 -04:00
if (atty) {
// We need a PTY. Get one.
ptmx = pts_open(pts_slave, sizeof(pts_slave));
} else {
pts_slave[0] = '\0';
}
2017-04-15 03:21:31 +08:00
2018-10-04 04:59:51 -04:00
// Send pts_slave
write_string(fd, pts_slave);
// Send stdin
send_fd(fd, (atty & ATTY_IN) ? -1 : STDIN_FILENO);
// Send stdout
send_fd(fd, (atty & ATTY_OUT) ? -1 : STDOUT_FILENO);
// Send stderr
send_fd(fd, (atty & ATTY_ERR) ? -1 : STDERR_FILENO);
2018-11-20 04:40:42 -05:00
if (atty) {
2018-10-04 04:59:51 -04:00
setup_sighandlers(sighandler);
watch_sigwinch_async(STDOUT_FILENO, ptmx);
2018-11-20 04:40:42 -05:00
pump_stdin_async(ptmx);
2018-10-04 04:59:51 -04:00
pump_stdout_blocking(ptmx);
}
// Get the exit code
int code = read_int(fd);
close(fd);
2018-10-04 04:59:51 -04:00
return code;
2018-11-04 03:38:06 -05:00
}