blob: 32973d496048752b235decb09a2255304d47a268 [file] [log] [blame]
Mike Frysingerf9da3d32017-09-19 23:41:27 -04001// Copyright 2017 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// WARNING:
6// This callback is intended to be a legacy entry point. New scripts should not
7// be added here. Instead a proper UI should be created to manage the system
8// interactions.
9
10#include "debugd/src/shill_scripts_tool.h"
11
Ben Chandf88cfa2017-09-29 00:36:10 -070012#include <memory>
Mike Frysingerf9da3d32017-09-19 23:41:27 -040013#include <string>
14#include <utility>
15#include <vector>
16
17#include <base/files/file_path.h>
Mike Frysingerf9da3d32017-09-19 23:41:27 -040018
19#include "debugd/src/error_utils.h"
20#include "debugd/src/process_with_id.h"
21
22namespace debugd {
23
24namespace {
25
26const char kUnsupportedShillScriptToolErrorName[] =
27 "org.chromium.debugd.error.UnsupportedShillScriptTool";
28
29const char kUser[] = "shill-scripts";
30const char kGroup[] = "shill-scripts";
31
32// Where shill scripts are installed.
33const char kScriptsDir[] = "/usr/bin";
34
35// clang-format off
Jorge Lucangeli Obes771736f2020-08-25 10:30:13 -040036const char * const kAllowedScripts[] = {
Mike Frysingerf9da3d32017-09-19 23:41:27 -040037 "connectivity",
38 "ff_debug",
39 "modem",
40 "network_diag",
41 "set_apn",
42 "set_arpgw",
43 "set_cellular_ppp",
44 "set_wake_on_lan",
Mike Frysingerf9da3d32017-09-19 23:41:27 -040045};
46// clang-format on
47
48// Only permit certain scripts here.
Jorge Lucangeli Obes771736f2020-08-25 10:30:13 -040049bool AllowedScript(const std::string& script, brillo::ErrorPtr* error) {
50 for (const char* listed : kAllowedScripts)
Mike Frysingerf9da3d32017-09-19 23:41:27 -040051 if (script == listed)
52 return true;
53
54 DEBUGD_ADD_ERROR(error, kUnsupportedShillScriptToolErrorName, script.c_str());
55 return false;
56}
57
58} // namespace
59
Eric Caruso0b241882018-04-04 13:43:46 -070060bool ShillScriptsTool::Run(const base::ScopedFD& outfd,
Mike Frysingerf9da3d32017-09-19 23:41:27 -040061 const std::string& script,
62 const std::vector<std::string>& script_args,
63 std::string* out_id,
64 brillo::ErrorPtr* error) {
Jorge Lucangeli Obes771736f2020-08-25 10:30:13 -040065 if (!AllowedScript(script, error))
Mike Frysingerf9da3d32017-09-19 23:41:27 -040066 return false;
67
Ben Chandf88cfa2017-09-29 00:36:10 -070068 auto p = std::make_unique<ProcessWithId>();
Mike Frysingerf9da3d32017-09-19 23:41:27 -040069 p->SandboxAs(kUser, kGroup);
70 p->Init();
71
72 const base::FilePath dir(kScriptsDir);
73 p->AddArg(dir.Append(script).value());
74
75 for (const auto& arg : script_args)
76 p->AddArg(arg);
77
Eric Caruso0b241882018-04-04 13:43:46 -070078 p->BindFd(outfd.get(), STDOUT_FILENO);
79 p->BindFd(outfd.get(), STDERR_FILENO);
Mike Frysingerf9da3d32017-09-19 23:41:27 -040080 *out_id = p->id();
81 p->Start();
82
83 RecordProcess(std::move(p));
84
85 return true;
86}
87
88} // namespace debugd