Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2018 The Android Open Source Project |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
| 16 | |
Tom Cherry | 31525f5 | 2018-05-09 18:33:31 -0700 | [diff] [blame] | 17 | #include <errno.h> |
Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 18 | #include <pwd.h> |
Tom Cherry | 31525f5 | 2018-05-09 18:33:31 -0700 | [diff] [blame] | 19 | #include <stdio.h> |
Tom Cherry | 863d808 | 2018-06-12 14:40:38 -0700 | [diff] [blame] | 20 | #include <stdlib.h> |
Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 21 | |
Tom Cherry | 194b5d1 | 2018-05-09 17:38:30 -0700 | [diff] [blame] | 22 | #include <iostream> |
| 23 | #include <string> |
Tom Cherry | 31525f5 | 2018-05-09 18:33:31 -0700 | [diff] [blame] | 24 | #include <vector> |
Tom Cherry | 194b5d1 | 2018-05-09 17:38:30 -0700 | [diff] [blame] | 25 | |
Tom Cherry | 31525f5 | 2018-05-09 18:33:31 -0700 | [diff] [blame] | 26 | #include <android-base/file.h> |
Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 27 | #include <android-base/logging.h> |
Tom Cherry | 31525f5 | 2018-05-09 18:33:31 -0700 | [diff] [blame] | 28 | #include <android-base/parseint.h> |
Tom Cherry | 194b5d1 | 2018-05-09 17:38:30 -0700 | [diff] [blame] | 29 | #include <android-base/strings.h> |
Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 30 | |
| 31 | #include "action.h" |
| 32 | #include "action_manager.h" |
| 33 | #include "action_parser.h" |
Tom Cherry | 194b5d1 | 2018-05-09 17:38:30 -0700 | [diff] [blame] | 34 | #include "host_import_parser.h" |
| 35 | #include "host_init_stubs.h" |
Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 36 | #include "parser.h" |
| 37 | #include "result.h" |
| 38 | #include "service.h" |
Tom Cherry | 2aeb1ad | 2019-06-26 10:46:20 -0700 | [diff] [blame^] | 39 | #include "service_list.h" |
| 40 | #include "service_parser.h" |
Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 41 | |
Tom Cherry | 31525f5 | 2018-05-09 18:33:31 -0700 | [diff] [blame] | 42 | #define EXCLUDE_FS_CONFIG_STRUCTURES |
| 43 | #include "generated_android_ids.h" |
| 44 | |
Tom Cherry | 194b5d1 | 2018-05-09 17:38:30 -0700 | [diff] [blame] | 45 | using namespace std::literals; |
| 46 | |
Tom Cherry | 31525f5 | 2018-05-09 18:33:31 -0700 | [diff] [blame] | 47 | using android::base::ParseInt; |
| 48 | using android::base::ReadFileToString; |
Tom Cherry | 194b5d1 | 2018-05-09 17:38:30 -0700 | [diff] [blame] | 49 | using android::base::Split; |
| 50 | |
Tom Cherry | 863d808 | 2018-06-12 14:40:38 -0700 | [diff] [blame] | 51 | static std::string passwd_file; |
Tom Cherry | 31525f5 | 2018-05-09 18:33:31 -0700 | [diff] [blame] | 52 | |
| 53 | static std::vector<std::pair<std::string, int>> GetVendorPasswd() { |
| 54 | std::string passwd; |
Tom Cherry | 863d808 | 2018-06-12 14:40:38 -0700 | [diff] [blame] | 55 | if (!ReadFileToString(passwd_file, &passwd)) { |
Tom Cherry | 31525f5 | 2018-05-09 18:33:31 -0700 | [diff] [blame] | 56 | return {}; |
| 57 | } |
| 58 | |
| 59 | std::vector<std::pair<std::string, int>> result; |
| 60 | auto passwd_lines = Split(passwd, "\n"); |
| 61 | for (const auto& line : passwd_lines) { |
| 62 | auto split_line = Split(line, ":"); |
| 63 | if (split_line.size() < 3) { |
| 64 | continue; |
| 65 | } |
| 66 | int uid = 0; |
| 67 | if (!ParseInt(split_line[2], &uid)) { |
| 68 | continue; |
| 69 | } |
| 70 | result.emplace_back(split_line[0], uid); |
| 71 | } |
| 72 | return result; |
| 73 | } |
| 74 | |
Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 75 | passwd* getpwnam(const char* login) { // NOLINT: implementing bad function. |
Tom Cherry | 31525f5 | 2018-05-09 18:33:31 -0700 | [diff] [blame] | 76 | // This isn't thread safe, but that's okay for our purposes. |
| 77 | static char static_name[32] = ""; |
| 78 | static char static_dir[32] = "/"; |
| 79 | static char static_shell[32] = "/system/bin/sh"; |
| 80 | static passwd static_passwd = { |
| 81 | .pw_name = static_name, |
| 82 | .pw_dir = static_dir, |
| 83 | .pw_shell = static_shell, |
| 84 | .pw_uid = 0, |
| 85 | .pw_gid = 0, |
Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 86 | }; |
Tom Cherry | 31525f5 | 2018-05-09 18:33:31 -0700 | [diff] [blame] | 87 | |
| 88 | for (size_t n = 0; n < android_id_count; ++n) { |
| 89 | if (!strcmp(android_ids[n].name, login)) { |
| 90 | snprintf(static_name, sizeof(static_name), "%s", android_ids[n].name); |
| 91 | static_passwd.pw_uid = android_ids[n].aid; |
| 92 | static_passwd.pw_gid = android_ids[n].aid; |
| 93 | return &static_passwd; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | static const auto vendor_passwd = GetVendorPasswd(); |
| 98 | |
| 99 | for (const auto& [name, uid] : vendor_passwd) { |
| 100 | if (name == login) { |
| 101 | snprintf(static_name, sizeof(static_name), "%s", name.c_str()); |
| 102 | static_passwd.pw_uid = uid; |
| 103 | static_passwd.pw_gid = uid; |
| 104 | return &static_passwd; |
| 105 | } |
| 106 | } |
| 107 | |
Tom Cherry | 290427b | 2018-06-14 13:40:20 -0700 | [diff] [blame] | 108 | unsigned int oem_uid; |
| 109 | if (sscanf(login, "oem_%u", &oem_uid) == 1) { |
| 110 | snprintf(static_name, sizeof(static_name), "%s", login); |
| 111 | static_passwd.pw_uid = oem_uid; |
| 112 | static_passwd.pw_gid = oem_uid; |
| 113 | return &static_passwd; |
| 114 | } |
| 115 | |
Tom Cherry | 31525f5 | 2018-05-09 18:33:31 -0700 | [diff] [blame] | 116 | errno = ENOENT; |
| 117 | return nullptr; |
Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | namespace android { |
| 121 | namespace init { |
| 122 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 123 | static Result<void> do_stub(const BuiltinArguments& args) { |
| 124 | return {}; |
Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | #include "generated_stub_builtin_function_map.h" |
| 128 | |
| 129 | int main(int argc, char** argv) { |
Elliott Hughes | 1be0d14 | 2018-05-23 09:16:46 -0700 | [diff] [blame] | 130 | android::base::InitLogging(argv, &android::base::StdioLogger); |
Tom Cherry | 194b5d1 | 2018-05-09 17:38:30 -0700 | [diff] [blame] | 131 | android::base::SetMinimumLogSeverity(android::base::ERROR); |
Tom Cherry | 863d808 | 2018-06-12 14:40:38 -0700 | [diff] [blame] | 132 | |
| 133 | if (argc != 2 && argc != 3) { |
| 134 | LOG(ERROR) << "Usage: " << argv[0] << " <init rc file> [passwd file]"; |
| 135 | return EXIT_FAILURE; |
Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 136 | } |
Tom Cherry | 194b5d1 | 2018-05-09 17:38:30 -0700 | [diff] [blame] | 137 | |
Tom Cherry | 863d808 | 2018-06-12 14:40:38 -0700 | [diff] [blame] | 138 | if (argc == 3) { |
| 139 | passwd_file = argv[2]; |
Tom Cherry | 194b5d1 | 2018-05-09 17:38:30 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 142 | const BuiltinFunctionMap function_map; |
| 143 | Action::set_function_map(&function_map); |
| 144 | ActionManager& am = ActionManager::GetInstance(); |
| 145 | ServiceList& sl = ServiceList::GetInstance(); |
| 146 | Parser parser; |
| 147 | parser.AddSectionParser("service", std::make_unique<ServiceParser>(&sl, nullptr)); |
| 148 | parser.AddSectionParser("on", std::make_unique<ActionParser>(&am, nullptr)); |
Tom Cherry | 863d808 | 2018-06-12 14:40:38 -0700 | [diff] [blame] | 149 | parser.AddSectionParser("import", std::make_unique<HostImportParser>()); |
Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 150 | |
Tom Cherry | d72432d | 2018-06-19 15:18:40 -0700 | [diff] [blame] | 151 | if (!parser.ParseConfigFileInsecure(argv[1])) { |
Tom Cherry | 863d808 | 2018-06-12 14:40:38 -0700 | [diff] [blame] | 152 | LOG(ERROR) << "Failed to open init rc script '" << argv[1] << "'"; |
| 153 | return EXIT_FAILURE; |
Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 154 | } |
Tom Cherry | 194b5d1 | 2018-05-09 17:38:30 -0700 | [diff] [blame] | 155 | if (parser.parse_error_count() > 0) { |
Tom Cherry | 863d808 | 2018-06-12 14:40:38 -0700 | [diff] [blame] | 156 | LOG(ERROR) << "Failed to parse init script '" << argv[1] << "' with " |
| 157 | << parser.parse_error_count() << " errors"; |
| 158 | return EXIT_FAILURE; |
Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 159 | } |
Tom Cherry | 863d808 | 2018-06-12 14:40:38 -0700 | [diff] [blame] | 160 | return EXIT_SUCCESS; |
Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | } // namespace init |
| 164 | } // namespace android |
| 165 | |
| 166 | int main(int argc, char** argv) { |
Tom Cherry | 863d808 | 2018-06-12 14:40:38 -0700 | [diff] [blame] | 167 | return android::init::main(argc, argv); |
Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 168 | } |