2018-02-14 00:50:08 +00:00
|
|
|
//
|
|
|
|
// Copyright (C) 2018 The Android Open Source Project
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
|
2018-05-10 01:33:31 +00:00
|
|
|
#include <errno.h>
|
2019-06-05 16:13:11 +00:00
|
|
|
#include <getopt.h>
|
2018-02-14 00:50:08 +00:00
|
|
|
#include <pwd.h>
|
2018-05-10 01:33:31 +00:00
|
|
|
#include <stdio.h>
|
2018-06-12 21:40:38 +00:00
|
|
|
#include <stdlib.h>
|
2018-02-14 00:50:08 +00:00
|
|
|
|
2019-07-09 18:00:53 +00:00
|
|
|
#include <fstream>
|
2018-05-10 00:38:30 +00:00
|
|
|
#include <iostream>
|
2019-06-05 16:13:11 +00:00
|
|
|
#include <iterator>
|
2018-05-10 00:38:30 +00:00
|
|
|
#include <string>
|
2018-05-10 01:33:31 +00:00
|
|
|
#include <vector>
|
2018-05-10 00:38:30 +00:00
|
|
|
|
2018-05-10 01:33:31 +00:00
|
|
|
#include <android-base/file.h>
|
2018-02-14 00:50:08 +00:00
|
|
|
#include <android-base/logging.h>
|
2018-05-10 01:33:31 +00:00
|
|
|
#include <android-base/parseint.h>
|
2018-05-10 00:38:30 +00:00
|
|
|
#include <android-base/strings.h>
|
2019-07-09 18:00:53 +00:00
|
|
|
#include <json/json.h>
|
2018-02-14 00:50:08 +00:00
|
|
|
|
|
|
|
#include "action.h"
|
|
|
|
#include "action_manager.h"
|
|
|
|
#include "action_parser.h"
|
2019-07-30 16:34:41 +00:00
|
|
|
#include "check_builtins.h"
|
2018-05-10 00:38:30 +00:00
|
|
|
#include "host_import_parser.h"
|
|
|
|
#include "host_init_stubs.h"
|
2018-02-14 00:50:08 +00:00
|
|
|
#include "parser.h"
|
|
|
|
#include "result.h"
|
|
|
|
#include "service.h"
|
2019-06-26 17:46:20 +00:00
|
|
|
#include "service_list.h"
|
|
|
|
#include "service_parser.h"
|
2018-02-14 00:50:08 +00:00
|
|
|
|
2018-05-10 01:33:31 +00:00
|
|
|
#define EXCLUDE_FS_CONFIG_STRUCTURES
|
|
|
|
#include "generated_android_ids.h"
|
|
|
|
|
2018-05-10 00:38:30 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2018-05-10 01:33:31 +00:00
|
|
|
using android::base::ParseInt;
|
|
|
|
using android::base::ReadFileToString;
|
2018-05-10 00:38:30 +00:00
|
|
|
using android::base::Split;
|
|
|
|
|
2019-06-05 16:13:11 +00:00
|
|
|
static std::vector<std::string> passwd_files;
|
2018-05-10 01:33:31 +00:00
|
|
|
|
2019-06-05 16:13:11 +00:00
|
|
|
static std::vector<std::pair<std::string, int>> GetVendorPasswd(const std::string& passwd_file) {
|
2018-05-10 01:33:31 +00:00
|
|
|
std::string passwd;
|
2018-06-12 21:40:38 +00:00
|
|
|
if (!ReadFileToString(passwd_file, &passwd)) {
|
2018-05-10 01:33:31 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::pair<std::string, int>> result;
|
|
|
|
auto passwd_lines = Split(passwd, "\n");
|
|
|
|
for (const auto& line : passwd_lines) {
|
|
|
|
auto split_line = Split(line, ":");
|
|
|
|
if (split_line.size() < 3) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
int uid = 0;
|
|
|
|
if (!ParseInt(split_line[2], &uid)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
result.emplace_back(split_line[0], uid);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-06-05 16:13:11 +00:00
|
|
|
static std::vector<std::pair<std::string, int>> GetVendorPasswd() {
|
|
|
|
std::vector<std::pair<std::string, int>> result;
|
|
|
|
for (const auto& passwd_file : passwd_files) {
|
|
|
|
auto individual_result = GetVendorPasswd(passwd_file);
|
|
|
|
std::move(individual_result.begin(), individual_result.end(),
|
|
|
|
std::back_insert_iterator(result));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-02-14 00:50:08 +00:00
|
|
|
passwd* getpwnam(const char* login) { // NOLINT: implementing bad function.
|
2018-05-10 01:33:31 +00:00
|
|
|
// This isn't thread safe, but that's okay for our purposes.
|
|
|
|
static char static_name[32] = "";
|
|
|
|
static char static_dir[32] = "/";
|
|
|
|
static char static_shell[32] = "/system/bin/sh";
|
|
|
|
static passwd static_passwd = {
|
|
|
|
.pw_name = static_name,
|
|
|
|
.pw_dir = static_dir,
|
|
|
|
.pw_shell = static_shell,
|
|
|
|
.pw_uid = 0,
|
|
|
|
.pw_gid = 0,
|
2018-02-14 00:50:08 +00:00
|
|
|
};
|
2018-05-10 01:33:31 +00:00
|
|
|
|
|
|
|
for (size_t n = 0; n < android_id_count; ++n) {
|
|
|
|
if (!strcmp(android_ids[n].name, login)) {
|
|
|
|
snprintf(static_name, sizeof(static_name), "%s", android_ids[n].name);
|
|
|
|
static_passwd.pw_uid = android_ids[n].aid;
|
|
|
|
static_passwd.pw_gid = android_ids[n].aid;
|
|
|
|
return &static_passwd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const auto vendor_passwd = GetVendorPasswd();
|
|
|
|
|
|
|
|
for (const auto& [name, uid] : vendor_passwd) {
|
|
|
|
if (name == login) {
|
|
|
|
snprintf(static_name, sizeof(static_name), "%s", name.c_str());
|
|
|
|
static_passwd.pw_uid = uid;
|
|
|
|
static_passwd.pw_gid = uid;
|
|
|
|
return &static_passwd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-14 20:40:20 +00:00
|
|
|
unsigned int oem_uid;
|
|
|
|
if (sscanf(login, "oem_%u", &oem_uid) == 1) {
|
|
|
|
snprintf(static_name, sizeof(static_name), "%s", login);
|
|
|
|
static_passwd.pw_uid = oem_uid;
|
|
|
|
static_passwd.pw_gid = oem_uid;
|
|
|
|
return &static_passwd;
|
|
|
|
}
|
|
|
|
|
2018-05-10 01:33:31 +00:00
|
|
|
errno = ENOENT;
|
|
|
|
return nullptr;
|
2018-02-14 00:50:08 +00:00
|
|
|
}
|
|
|
|
|
2019-07-09 18:00:53 +00:00
|
|
|
static std::optional<android::init::InterfaceInheritanceHierarchyMap>
|
|
|
|
ReadInterfaceInheritanceHierarchy(const std::string& interface_inheritance_hierarchy_file) {
|
|
|
|
if (interface_inheritance_hierarchy_file.empty()) {
|
|
|
|
LOG(WARNING) << "Missing an interface inheritance hierarchy file.";
|
2019-06-27 19:18:08 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-07-09 18:00:53 +00:00
|
|
|
Json::Value root;
|
|
|
|
Json::Reader reader;
|
|
|
|
std::ifstream stream(interface_inheritance_hierarchy_file);
|
|
|
|
if (!reader.parse(stream, root)) {
|
|
|
|
LOG(ERROR) << "Failed to read interface inheritance hierarchy file: "
|
|
|
|
<< interface_inheritance_hierarchy_file << "\n"
|
|
|
|
<< reader.getFormattedErrorMessages();
|
2019-06-27 19:18:08 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-07-09 18:00:53 +00:00
|
|
|
android::init::InterfaceInheritanceHierarchyMap result;
|
|
|
|
for (const Json::Value& entry : root) {
|
|
|
|
std::set<std::string> inherited_interfaces;
|
|
|
|
for (const Json::Value& intf : entry["inheritedInterfaces"]) {
|
|
|
|
inherited_interfaces.insert(intf.asString());
|
|
|
|
}
|
|
|
|
result[entry["interface"].asString()] = inherited_interfaces;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2019-06-27 19:18:08 +00:00
|
|
|
}
|
|
|
|
|
2018-02-14 00:50:08 +00:00
|
|
|
namespace android {
|
|
|
|
namespace init {
|
|
|
|
|
2019-07-30 16:34:41 +00:00
|
|
|
static Result<void> check_stub(const BuiltinArguments& args) {
|
2019-06-10 18:08:01 +00:00
|
|
|
return {};
|
2018-02-14 00:50:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#include "generated_stub_builtin_function_map.h"
|
|
|
|
|
2019-06-05 16:13:11 +00:00
|
|
|
void PrintUsage() {
|
2019-08-02 17:59:54 +00:00
|
|
|
std::cout << "usage: host_init_verifier [-p FILE] -i FILE <init rc file>\n"
|
2019-06-05 16:13:11 +00:00
|
|
|
"\n"
|
|
|
|
"Tests an init script for correctness\n"
|
|
|
|
"\n"
|
|
|
|
"-p FILE\tSearch this passwd file for users and groups\n"
|
2019-08-02 17:59:54 +00:00
|
|
|
"-i FILE\tParse this JSON file for the HIDL interface inheritance hierarchy\n"
|
2019-06-05 16:13:11 +00:00
|
|
|
<< std::endl;
|
|
|
|
}
|
|
|
|
|
2018-02-14 00:50:08 +00:00
|
|
|
int main(int argc, char** argv) {
|
2018-05-23 16:16:46 +00:00
|
|
|
android::base::InitLogging(argv, &android::base::StdioLogger);
|
2018-05-10 00:38:30 +00:00
|
|
|
android::base::SetMinimumLogSeverity(android::base::ERROR);
|
|
|
|
|
2019-07-09 18:00:53 +00:00
|
|
|
std::string interface_inheritance_hierarchy_file;
|
2019-06-27 19:18:08 +00:00
|
|
|
|
2019-06-05 16:13:11 +00:00
|
|
|
while (true) {
|
|
|
|
static const struct option long_options[] = {
|
|
|
|
{"help", no_argument, nullptr, 'h'},
|
|
|
|
{nullptr, 0, nullptr, 0},
|
|
|
|
};
|
|
|
|
|
2019-07-09 18:00:53 +00:00
|
|
|
int arg = getopt_long(argc, argv, "p:i:", long_options, nullptr);
|
2019-06-05 16:13:11 +00:00
|
|
|
|
|
|
|
if (arg == -1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (arg) {
|
|
|
|
case 'h':
|
|
|
|
PrintUsage();
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
case 'p':
|
|
|
|
passwd_files.emplace_back(optarg);
|
|
|
|
break;
|
2019-07-09 18:00:53 +00:00
|
|
|
case 'i':
|
|
|
|
interface_inheritance_hierarchy_file = optarg;
|
2019-06-27 19:18:08 +00:00
|
|
|
break;
|
2019-06-05 16:13:11 +00:00
|
|
|
default:
|
|
|
|
std::cerr << "getprop: getopt returned invalid result: " << arg << std::endl;
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2018-06-12 21:40:38 +00:00
|
|
|
}
|
2018-05-10 01:33:31 +00:00
|
|
|
|
2019-06-05 16:13:11 +00:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
2019-08-02 17:59:54 +00:00
|
|
|
if (argc != 1 || interface_inheritance_hierarchy_file.empty()) {
|
2019-06-05 16:13:11 +00:00
|
|
|
PrintUsage();
|
|
|
|
return EXIT_FAILURE;
|
2018-05-10 00:38:30 +00:00
|
|
|
}
|
|
|
|
|
2019-07-22 23:05:36 +00:00
|
|
|
const BuiltinFunctionMap& function_map = GetBuiltinFunctionMap();
|
2018-02-14 00:50:08 +00:00
|
|
|
Action::set_function_map(&function_map);
|
|
|
|
ActionManager& am = ActionManager::GetInstance();
|
|
|
|
ServiceList& sl = ServiceList::GetInstance();
|
|
|
|
Parser parser;
|
2019-06-27 19:18:08 +00:00
|
|
|
parser.AddSectionParser(
|
2019-07-09 18:00:53 +00:00
|
|
|
"service",
|
|
|
|
std::make_unique<ServiceParser>(
|
|
|
|
&sl, nullptr,
|
|
|
|
ReadInterfaceInheritanceHierarchy(interface_inheritance_hierarchy_file)));
|
2018-02-14 00:50:08 +00:00
|
|
|
parser.AddSectionParser("on", std::make_unique<ActionParser>(&am, nullptr));
|
2018-06-12 21:40:38 +00:00
|
|
|
parser.AddSectionParser("import", std::make_unique<HostImportParser>());
|
2018-02-14 00:50:08 +00:00
|
|
|
|
2019-06-05 16:13:11 +00:00
|
|
|
if (!parser.ParseConfigFileInsecure(*argv)) {
|
|
|
|
LOG(ERROR) << "Failed to open init rc script '" << *argv << "'";
|
2018-06-12 21:40:38 +00:00
|
|
|
return EXIT_FAILURE;
|
2018-02-14 00:50:08 +00:00
|
|
|
}
|
2019-07-30 16:34:41 +00:00
|
|
|
size_t failures = parser.parse_error_count() + am.CheckAllCommands();
|
|
|
|
if (failures > 0) {
|
|
|
|
LOG(ERROR) << "Failed to parse init script '" << *argv << "' with " << failures
|
|
|
|
<< " errors";
|
2018-06-12 21:40:38 +00:00
|
|
|
return EXIT_FAILURE;
|
2018-02-14 00:50:08 +00:00
|
|
|
}
|
2018-06-12 21:40:38 +00:00
|
|
|
return EXIT_SUCCESS;
|
2018-02-14 00:50:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace init
|
|
|
|
} // namespace android
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
2018-06-12 21:40:38 +00:00
|
|
|
return android::init::main(argc, argv);
|
2018-02-14 00:50:08 +00:00
|
|
|
}
|