Austin Schuh | 529ac59 | 2021-10-14 16:11:13 -0700 | [diff] [blame] | 1 | #include <pwd.h> |
| 2 | #include <sys/types.h> |
| 3 | |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 4 | #include "aos/init.h" |
| 5 | #include "gflags/gflags.h" |
| 6 | #include "starterd_lib.h" |
| 7 | |
Austin Schuh | 8e2dfc6 | 2022-08-17 16:36:00 -0700 | [diff] [blame] | 8 | DEFINE_string(config, "aos_config.json", "File path of aos configuration"); |
Austin Schuh | 529ac59 | 2021-10-14 16:11:13 -0700 | [diff] [blame] | 9 | DEFINE_string(user, "", |
| 10 | "Starter runs as though this user ran a SUID binary if set."); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 11 | |
| 12 | int main(int argc, char **argv) { |
| 13 | aos::InitGoogle(&argc, &argv); |
Austin Schuh | 529ac59 | 2021-10-14 16:11:13 -0700 | [diff] [blame] | 14 | if (!FLAGS_user.empty()) { |
| 15 | uid_t uid; |
| 16 | uid_t gid; |
| 17 | { |
| 18 | struct passwd *user_data = getpwnam(FLAGS_user.c_str()); |
| 19 | if (user_data != nullptr) { |
| 20 | uid = user_data->pw_uid; |
| 21 | gid = user_data->pw_gid; |
| 22 | } else { |
| 23 | LOG(FATAL) << "Could not find user " << FLAGS_user; |
| 24 | return 1; |
| 25 | } |
| 26 | } |
James Kuszmaul | 4ff5027 | 2022-01-07 18:31:13 -0800 | [diff] [blame] | 27 | // Change the real and effective IDs to the user we're running as. The |
| 28 | // effective IDs mean files we access (like shared memory) will happen as |
| 29 | // that user. The real IDs allow child processes with an different effective |
| 30 | // ID to still participate in signal sending/receiving. |
Austin Schuh | 529ac59 | 2021-10-14 16:11:13 -0700 | [diff] [blame] | 31 | constexpr int kUnchanged = -1; |
| 32 | if (setresgid(/* ruid */ gid, /* euid */ gid, |
| 33 | /* suid */ kUnchanged) != 0) { |
Austin Schuh | 3e1d3b6 | 2023-01-08 13:52:31 -0800 | [diff] [blame^] | 34 | PLOG(FATAL) << "Failed to change GID to " << FLAGS_user << ", group " << gid; |
Austin Schuh | 529ac59 | 2021-10-14 16:11:13 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | if (setresuid(/* ruid */ uid, /* euid */ uid, |
| 38 | /* suid */ kUnchanged) != 0) { |
| 39 | PLOG(FATAL) << "Failed to change UID to " << FLAGS_user; |
| 40 | } |
| 41 | } |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 42 | |
| 43 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 44 | aos::configuration::ReadConfig(FLAGS_config); |
| 45 | |
| 46 | const aos::Configuration *config_msg = &config.message(); |
| 47 | |
| 48 | aos::starter::Starter starter(config_msg); |
| 49 | |
| 50 | starter.Run(); |
| 51 | |
| 52 | return 0; |
| 53 | } |