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 | 3f1bce8 | 2019-06-05 09:13:11 -0700 | [diff] [blame] | 18 | #include <getopt.h> |
Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 19 | #include <pwd.h> |
Tom Cherry | 31525f5 | 2018-05-09 18:33:31 -0700 | [diff] [blame] | 20 | #include <stdio.h> |
Tom Cherry | 863d808 | 2018-06-12 14:40:38 -0700 | [diff] [blame] | 21 | #include <stdlib.h> |
Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 22 | |
Daniel Norman | 3f42a76 | 2019-07-09 11:00:53 -0700 | [diff] [blame] | 23 | #include <fstream> |
Tom Cherry | 194b5d1 | 2018-05-09 17:38:30 -0700 | [diff] [blame] | 24 | #include <iostream> |
Tom Cherry | 3f1bce8 | 2019-06-05 09:13:11 -0700 | [diff] [blame] | 25 | #include <iterator> |
Tom Cherry | 194b5d1 | 2018-05-09 17:38:30 -0700 | [diff] [blame] | 26 | #include <string> |
Tom Cherry | 31525f5 | 2018-05-09 18:33:31 -0700 | [diff] [blame] | 27 | #include <vector> |
Tom Cherry | 194b5d1 | 2018-05-09 17:38:30 -0700 | [diff] [blame] | 28 | |
Tom Cherry | 31525f5 | 2018-05-09 18:33:31 -0700 | [diff] [blame] | 29 | #include <android-base/file.h> |
Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 30 | #include <android-base/logging.h> |
Tom Cherry | 31525f5 | 2018-05-09 18:33:31 -0700 | [diff] [blame] | 31 | #include <android-base/parseint.h> |
Tom Cherry | 194b5d1 | 2018-05-09 17:38:30 -0700 | [diff] [blame] | 32 | #include <android-base/strings.h> |
Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 33 | |
| 34 | #include "action.h" |
| 35 | #include "action_manager.h" |
| 36 | #include "action_parser.h" |
Tom Cherry | 4772f1d | 2019-07-30 09:34:41 -0700 | [diff] [blame] | 37 | #include "check_builtins.h" |
Tom Cherry | 194b5d1 | 2018-05-09 17:38:30 -0700 | [diff] [blame] | 38 | #include "host_import_parser.h" |
| 39 | #include "host_init_stubs.h" |
Daniel Norman | d2533c3 | 2019-08-02 15:13:50 -0700 | [diff] [blame^] | 40 | #include "interface_utils.h" |
Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 41 | #include "parser.h" |
| 42 | #include "result.h" |
| 43 | #include "service.h" |
Tom Cherry | 2aeb1ad | 2019-06-26 10:46:20 -0700 | [diff] [blame] | 44 | #include "service_list.h" |
| 45 | #include "service_parser.h" |
Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 46 | |
Tom Cherry | 31525f5 | 2018-05-09 18:33:31 -0700 | [diff] [blame] | 47 | #define EXCLUDE_FS_CONFIG_STRUCTURES |
| 48 | #include "generated_android_ids.h" |
| 49 | |
Tom Cherry | 194b5d1 | 2018-05-09 17:38:30 -0700 | [diff] [blame] | 50 | using namespace std::literals; |
| 51 | |
Tom Cherry | 31525f5 | 2018-05-09 18:33:31 -0700 | [diff] [blame] | 52 | using android::base::ParseInt; |
| 53 | using android::base::ReadFileToString; |
Tom Cherry | 194b5d1 | 2018-05-09 17:38:30 -0700 | [diff] [blame] | 54 | using android::base::Split; |
| 55 | |
Tom Cherry | 3f1bce8 | 2019-06-05 09:13:11 -0700 | [diff] [blame] | 56 | static std::vector<std::string> passwd_files; |
Tom Cherry | 31525f5 | 2018-05-09 18:33:31 -0700 | [diff] [blame] | 57 | |
Tom Cherry | 3f1bce8 | 2019-06-05 09:13:11 -0700 | [diff] [blame] | 58 | static std::vector<std::pair<std::string, int>> GetVendorPasswd(const std::string& passwd_file) { |
Tom Cherry | 31525f5 | 2018-05-09 18:33:31 -0700 | [diff] [blame] | 59 | std::string passwd; |
Tom Cherry | 863d808 | 2018-06-12 14:40:38 -0700 | [diff] [blame] | 60 | if (!ReadFileToString(passwd_file, &passwd)) { |
Tom Cherry | 31525f5 | 2018-05-09 18:33:31 -0700 | [diff] [blame] | 61 | return {}; |
| 62 | } |
| 63 | |
| 64 | std::vector<std::pair<std::string, int>> result; |
| 65 | auto passwd_lines = Split(passwd, "\n"); |
| 66 | for (const auto& line : passwd_lines) { |
| 67 | auto split_line = Split(line, ":"); |
| 68 | if (split_line.size() < 3) { |
| 69 | continue; |
| 70 | } |
| 71 | int uid = 0; |
| 72 | if (!ParseInt(split_line[2], &uid)) { |
| 73 | continue; |
| 74 | } |
| 75 | result.emplace_back(split_line[0], uid); |
| 76 | } |
| 77 | return result; |
| 78 | } |
| 79 | |
Tom Cherry | 3f1bce8 | 2019-06-05 09:13:11 -0700 | [diff] [blame] | 80 | static std::vector<std::pair<std::string, int>> GetVendorPasswd() { |
| 81 | std::vector<std::pair<std::string, int>> result; |
| 82 | for (const auto& passwd_file : passwd_files) { |
| 83 | auto individual_result = GetVendorPasswd(passwd_file); |
| 84 | std::move(individual_result.begin(), individual_result.end(), |
| 85 | std::back_insert_iterator(result)); |
| 86 | } |
| 87 | return result; |
| 88 | } |
| 89 | |
Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 90 | passwd* getpwnam(const char* login) { // NOLINT: implementing bad function. |
Tom Cherry | 31525f5 | 2018-05-09 18:33:31 -0700 | [diff] [blame] | 91 | // This isn't thread safe, but that's okay for our purposes. |
| 92 | static char static_name[32] = ""; |
| 93 | static char static_dir[32] = "/"; |
| 94 | static char static_shell[32] = "/system/bin/sh"; |
| 95 | static passwd static_passwd = { |
| 96 | .pw_name = static_name, |
| 97 | .pw_dir = static_dir, |
| 98 | .pw_shell = static_shell, |
| 99 | .pw_uid = 0, |
| 100 | .pw_gid = 0, |
Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 101 | }; |
Tom Cherry | 31525f5 | 2018-05-09 18:33:31 -0700 | [diff] [blame] | 102 | |
| 103 | for (size_t n = 0; n < android_id_count; ++n) { |
| 104 | if (!strcmp(android_ids[n].name, login)) { |
| 105 | snprintf(static_name, sizeof(static_name), "%s", android_ids[n].name); |
| 106 | static_passwd.pw_uid = android_ids[n].aid; |
| 107 | static_passwd.pw_gid = android_ids[n].aid; |
| 108 | return &static_passwd; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | static const auto vendor_passwd = GetVendorPasswd(); |
| 113 | |
| 114 | for (const auto& [name, uid] : vendor_passwd) { |
| 115 | if (name == login) { |
| 116 | snprintf(static_name, sizeof(static_name), "%s", name.c_str()); |
| 117 | static_passwd.pw_uid = uid; |
| 118 | static_passwd.pw_gid = uid; |
| 119 | return &static_passwd; |
| 120 | } |
| 121 | } |
| 122 | |
Tom Cherry | 290427b | 2018-06-14 13:40:20 -0700 | [diff] [blame] | 123 | unsigned int oem_uid; |
| 124 | if (sscanf(login, "oem_%u", &oem_uid) == 1) { |
| 125 | snprintf(static_name, sizeof(static_name), "%s", login); |
| 126 | static_passwd.pw_uid = oem_uid; |
| 127 | static_passwd.pw_gid = oem_uid; |
| 128 | return &static_passwd; |
| 129 | } |
| 130 | |
Tom Cherry | 31525f5 | 2018-05-09 18:33:31 -0700 | [diff] [blame] | 131 | errno = ENOENT; |
| 132 | return nullptr; |
Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | namespace android { |
| 136 | namespace init { |
| 137 | |
Tom Cherry | 4772f1d | 2019-07-30 09:34:41 -0700 | [diff] [blame] | 138 | static Result<void> check_stub(const BuiltinArguments& args) { |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 139 | return {}; |
Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | #include "generated_stub_builtin_function_map.h" |
| 143 | |
Tom Cherry | 3f1bce8 | 2019-06-05 09:13:11 -0700 | [diff] [blame] | 144 | void PrintUsage() { |
Daniel Norman | e6586c4 | 2019-08-02 10:59:54 -0700 | [diff] [blame] | 145 | std::cout << "usage: host_init_verifier [-p FILE] -i FILE <init rc file>\n" |
Tom Cherry | 3f1bce8 | 2019-06-05 09:13:11 -0700 | [diff] [blame] | 146 | "\n" |
| 147 | "Tests an init script for correctness\n" |
| 148 | "\n" |
| 149 | "-p FILE\tSearch this passwd file for users and groups\n" |
Daniel Norman | e6586c4 | 2019-08-02 10:59:54 -0700 | [diff] [blame] | 150 | "-i FILE\tParse this JSON file for the HIDL interface inheritance hierarchy\n" |
Tom Cherry | 3f1bce8 | 2019-06-05 09:13:11 -0700 | [diff] [blame] | 151 | << std::endl; |
| 152 | } |
| 153 | |
Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 154 | int main(int argc, char** argv) { |
Elliott Hughes | 1be0d14 | 2018-05-23 09:16:46 -0700 | [diff] [blame] | 155 | android::base::InitLogging(argv, &android::base::StdioLogger); |
Tom Cherry | 194b5d1 | 2018-05-09 17:38:30 -0700 | [diff] [blame] | 156 | android::base::SetMinimumLogSeverity(android::base::ERROR); |
Tom Cherry | 863d808 | 2018-06-12 14:40:38 -0700 | [diff] [blame] | 157 | |
Daniel Norman | 3f42a76 | 2019-07-09 11:00:53 -0700 | [diff] [blame] | 158 | std::string interface_inheritance_hierarchy_file; |
Daniel Norman | 3df8dc5 | 2019-06-27 12:18:08 -0700 | [diff] [blame] | 159 | |
Tom Cherry | 3f1bce8 | 2019-06-05 09:13:11 -0700 | [diff] [blame] | 160 | while (true) { |
| 161 | static const struct option long_options[] = { |
| 162 | {"help", no_argument, nullptr, 'h'}, |
| 163 | {nullptr, 0, nullptr, 0}, |
| 164 | }; |
| 165 | |
Daniel Norman | 3f42a76 | 2019-07-09 11:00:53 -0700 | [diff] [blame] | 166 | int arg = getopt_long(argc, argv, "p:i:", long_options, nullptr); |
Tom Cherry | 3f1bce8 | 2019-06-05 09:13:11 -0700 | [diff] [blame] | 167 | |
| 168 | if (arg == -1) { |
| 169 | break; |
| 170 | } |
| 171 | |
| 172 | switch (arg) { |
| 173 | case 'h': |
| 174 | PrintUsage(); |
| 175 | return EXIT_FAILURE; |
| 176 | case 'p': |
| 177 | passwd_files.emplace_back(optarg); |
| 178 | break; |
Daniel Norman | 3f42a76 | 2019-07-09 11:00:53 -0700 | [diff] [blame] | 179 | case 'i': |
| 180 | interface_inheritance_hierarchy_file = optarg; |
Daniel Norman | 3df8dc5 | 2019-06-27 12:18:08 -0700 | [diff] [blame] | 181 | break; |
Tom Cherry | 3f1bce8 | 2019-06-05 09:13:11 -0700 | [diff] [blame] | 182 | default: |
| 183 | std::cerr << "getprop: getopt returned invalid result: " << arg << std::endl; |
| 184 | return EXIT_FAILURE; |
| 185 | } |
Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 186 | } |
Tom Cherry | 194b5d1 | 2018-05-09 17:38:30 -0700 | [diff] [blame] | 187 | |
Tom Cherry | 3f1bce8 | 2019-06-05 09:13:11 -0700 | [diff] [blame] | 188 | argc -= optind; |
| 189 | argv += optind; |
| 190 | |
Daniel Norman | e6586c4 | 2019-08-02 10:59:54 -0700 | [diff] [blame] | 191 | if (argc != 1 || interface_inheritance_hierarchy_file.empty()) { |
Tom Cherry | 3f1bce8 | 2019-06-05 09:13:11 -0700 | [diff] [blame] | 192 | PrintUsage(); |
| 193 | return EXIT_FAILURE; |
Tom Cherry | 194b5d1 | 2018-05-09 17:38:30 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Daniel Norman | d2533c3 | 2019-08-02 15:13:50 -0700 | [diff] [blame^] | 196 | auto interface_inheritance_hierarchy_map = |
| 197 | ReadInterfaceInheritanceHierarchy(interface_inheritance_hierarchy_file); |
| 198 | if (!interface_inheritance_hierarchy_map) { |
| 199 | LOG(ERROR) << interface_inheritance_hierarchy_map.error(); |
| 200 | return EXIT_FAILURE; |
| 201 | } |
| 202 | SetKnownInterfaces(*interface_inheritance_hierarchy_map); |
| 203 | |
Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 204 | const BuiltinFunctionMap& function_map = GetBuiltinFunctionMap(); |
Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 205 | Action::set_function_map(&function_map); |
| 206 | ActionManager& am = ActionManager::GetInstance(); |
| 207 | ServiceList& sl = ServiceList::GetInstance(); |
| 208 | Parser parser; |
Daniel Norman | d2533c3 | 2019-08-02 15:13:50 -0700 | [diff] [blame^] | 209 | parser.AddSectionParser("service", std::make_unique<ServiceParser>( |
| 210 | &sl, nullptr, *interface_inheritance_hierarchy_map)); |
Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 211 | parser.AddSectionParser("on", std::make_unique<ActionParser>(&am, nullptr)); |
Tom Cherry | 863d808 | 2018-06-12 14:40:38 -0700 | [diff] [blame] | 212 | parser.AddSectionParser("import", std::make_unique<HostImportParser>()); |
Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 213 | |
Tom Cherry | 3f1bce8 | 2019-06-05 09:13:11 -0700 | [diff] [blame] | 214 | if (!parser.ParseConfigFileInsecure(*argv)) { |
| 215 | LOG(ERROR) << "Failed to open init rc script '" << *argv << "'"; |
Tom Cherry | 863d808 | 2018-06-12 14:40:38 -0700 | [diff] [blame] | 216 | return EXIT_FAILURE; |
Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 217 | } |
Tom Cherry | 4772f1d | 2019-07-30 09:34:41 -0700 | [diff] [blame] | 218 | size_t failures = parser.parse_error_count() + am.CheckAllCommands(); |
| 219 | if (failures > 0) { |
| 220 | LOG(ERROR) << "Failed to parse init script '" << *argv << "' with " << failures |
| 221 | << " errors"; |
Tom Cherry | 863d808 | 2018-06-12 14:40:38 -0700 | [diff] [blame] | 222 | return EXIT_FAILURE; |
Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 223 | } |
Tom Cherry | 863d808 | 2018-06-12 14:40:38 -0700 | [diff] [blame] | 224 | return EXIT_SUCCESS; |
Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | } // namespace init |
| 228 | } // namespace android |
| 229 | |
| 230 | int main(int argc, char** argv) { |
Tom Cherry | 863d808 | 2018-06-12 14:40:38 -0700 | [diff] [blame] | 231 | return android::init::main(argc, argv); |
Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 232 | } |