-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinstall.php
More file actions
150 lines (118 loc) · 4.49 KB
/
install.php
File metadata and controls
150 lines (118 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
// Remoted API server installer.
// (C) 2017 CubicleSoft. All Rights Reserved.
if (!isset($_SERVER["argc"]) || !$_SERVER["argc"])
{
echo "This file is intended to be run from the command-line.";
exit();
}
// Temporary root.
$rootpath = str_replace("\\", "/", dirname(__FILE__));
require_once $rootpath . "/support/cli.php";
require_once $rootpath . "/support/random.php";
require_once $rootpath . "/support/ras_functions.php";
// Process the command-line options.
$options = array(
"shortmap" => array(
"s" => "suppressoutput",
"?" => "help"
),
"rules" => array(
"suppressoutput" => array("arg" => false),
"help" => array("arg" => false)
)
);
$args = CLI::ParseCommandLine($options);
if (isset($args["opts"]["help"]))
{
echo "Remoted API server installation command-line tool\n";
echo "Purpose: Installs the remoted API server from the command-line.\n";
echo "\n";
echo "This tool is question/answer enabled. Just running it will provide a guided interface.\n";
echo "\n";
echo "Syntax: " . $args["file"] . " [options]\n";
echo "Options:\n";
echo "\t-s Suppress most output. Useful for capturing JSON output.\n";
echo "\n";
echo "Examples:\n";
echo "\tphp " . $args["file"] . "\n";
exit();
}
$config = RAS_LoadConfig();
$suppressoutput = (isset($args["opts"]["suppressoutput"]) && $args["opts"]["suppressoutput"]);
function DisplayResult($result)
{
echo json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . "\n";
exit();
}
if (!isset($config["server_apikey"]))
{
$rng = new CSPRNG(true);
$config["server_apikey"] = $rng->GenerateString(64);
RAS_SaveConfig($config);
}
if (!isset($config["client_apikey"]))
{
$rng = new CSPRNG(true);
$config["client_apikey"] = $rng->GenerateString(64);
RAS_SaveConfig($config);
}
if (!isset($config["host"]))
{
$ipv6 = CLI::GetYesNoUserInputWithArgs($args, "ipv6", "IPv6", "N", "The next question asks if a localhost IPv6 server should be started instead of the default localhost IPv4 server. A web server will proxy requests to this server.", $suppressoutput);
$config["host"] = ($ipv6 ? "[::1]" : "127.0.0.1");
RAS_SaveConfig($config);
}
if (!isset($config["port"]))
{
$port = (int)CLI::GetUserInputWithArgs($args, "port", "Port", "37791", "The next question asks what port number to run the server on. A web server will proxy requests to this server and port.", $suppressoutput);
if ($port < 0 || $port > 65535) $port = 37791;
$config["port"] = $port;
RAS_SaveConfig($config);
}
if (function_exists("posix_geteuid"))
{
$uid = posix_geteuid();
if ($uid !== 0) CLI::DisplayError("The Remoted API Server installer must be run as the 'root' user (UID = 0) to install the system service on *NIX hosts.");
}
if (!isset($config["serviceuser"]))
{
if (!function_exists("posix_geteuid")) $config["serviceuser"] = "";
else
{
$serviceuser = CLI::GetUserInputWithArgs($args, "serviceuser", "System service user/group", "remoted-api-server", "The next question asks what user the system service will run as. Both a system user and group will be created.", $suppressoutput);
$config["serviceuser"] = $serviceuser;
// Create the system user/group.
ob_start();
system("useradd -r -s /bin/false " . escapeshellarg($serviceuser));
if ($suppressoutput) ob_end_clean();
else ob_end_flush();
}
RAS_SaveConfig($config);
}
// Allow the user to read the configuration.
if ($config["serviceuser"] !== "") @chown($rootpath . "/config.dat", $config["serviceuser"]);
if (!isset($config["servicename"]))
{
$servicename = CLI::GetUserInputWithArgs($args, "servicename", "System service name", "remoted-api-server", "The next question asks what the name of the system service will be. Enter a single hyphen '-' to not install this software as a system service at this time.", $suppressoutput);
if ($servicename !== "-")
{
$config["servicename"] = $servicename;
RAS_SaveConfig($config);
// Install and start 'server.php' as a system service.
if (!$suppressoutput) echo "\nInstalling system service...\n";
ob_start();
system(escapeshellarg(PHP_BINARY) . " " . escapeshellarg($rootpath . "/server.php") . " install");
system(escapeshellarg(PHP_BINARY) . " " . escapeshellarg($rootpath . "/server.php") . " start");
echo "\n\n";
if ($suppressoutput) ob_end_clean();
else ob_end_flush();
}
}
$result = array(
"success" => true,
"config" => $config,
"configfile" => $rootpath . "/config.dat"
);
DisplayResult($result);
?>