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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
|
use std::fs::{self, File, OpenOptions};
use std::io::{self, BufReader, Write};
use actix_web::{
dev::ServiceRequest, http::header::ContentType, web, App, HttpResponse, HttpServer,
};
use actix_web_httpauth::extractors::basic::{BasicAuth, Config};
use actix_web_httpauth::extractors::AuthenticationError;
use actix_web_httpauth::middleware::HttpAuthentication;
use constant_time_eq::constant_time_eq;
use fscommon::BufStream;
use rustls::{Certificate, PrivateKey, ServerConfig};
use rustls_pemfile::{certs, pkcs8_private_keys};
use serde::Deserialize;
use sysinfo::{Pid, ProcessExt, Signal, System, SystemExt};
use thiserror::Error;
#[allow(non_upper_case_globals)]
const KiB: usize = 1024;
#[allow(non_upper_case_globals)]
const MiB: usize = 1024 * KiB;
#[derive(Debug, Error)]
pub enum Error {
#[error("can't find disk device")]
NoDiskDev,
#[error("no private keys found in file")]
NoPrivateKeys,
#[error("no rootfs set in active cmdline")]
RootdevUnset,
#[error("io error: {0}")]
Io(#[from] io::Error),
#[error("actix_web error: {0}")]
ActixWeb(#[from] actix_web::Error),
#[error("rustls error: {0}")]
Rustls(#[from] rustls::Error),
}
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Clone, Debug, Deserialize)]
struct DataRequest {
path: String,
}
async fn handle_reboot() -> HttpResponse {
println!("request reboot");
if let Some(init) = System::new_all().process(Pid::from(1)) {
init.kill_with(Signal::User1);
HttpResponse::Ok()
.content_type(ContentType::plaintext())
.body("rebooting...")
} else {
HttpResponse::InternalServerError()
.content_type(ContentType::plaintext())
.body("can't reboot: no pid1")
}
}
async fn handle_shutdown() -> HttpResponse {
println!("request shutdown");
if let Some(init) = System::new_all().process(Pid::from(1)) {
init.kill_with(Signal::User2);
HttpResponse::Ok()
.content_type(ContentType::plaintext())
.body("shutting down...")
} else {
HttpResponse::InternalServerError()
.content_type(ContentType::plaintext())
.body("can't shut down: {}")
}
}
async fn handle_update_boot(data: web::Bytes) -> HttpResponse {
println!("update boot");
let boot = match boot_dev() {
Ok(v) => v,
Err(e) => {
return HttpResponse::InternalServerError()
.content_type(ContentType::plaintext())
.body(format!("can't locate boot partition: {}", e))
}
};
match stream_to(boot, &data).await {
Ok(_) => HttpResponse::Ok()
.content_type(ContentType::plaintext())
.body("successfully updated boot partition"),
Err(e) => HttpResponse::InternalServerError()
.content_type(ContentType::plaintext())
.body(format!("can't update boot partition: {}", e)),
}
}
async fn handle_update_mbr(data: web::Bytes) -> HttpResponse {
println!("update mbr");
let mbr = match dev() {
Ok(v) => v,
Err(e) => {
return HttpResponse::InternalServerError()
.content_type(ContentType::plaintext())
.body(format!("can't locate disk device: {}", e))
}
};
match stream_to(mbr, &data).await {
Ok(_) => HttpResponse::Ok()
.content_type(ContentType::plaintext())
.body("successfully updated mbr"),
Err(e) => HttpResponse::InternalServerError()
.content_type(ContentType::plaintext())
.body(format!("can't update mbr: {}", e)),
}
}
async fn handle_update_root(data: web::Bytes) -> HttpResponse {
println!("update inactive root");
let root = match inactive_root() {
Ok(v) => v,
Err(e) => {
return HttpResponse::InternalServerError()
.content_type(ContentType::plaintext())
.body(format!("can't locate inactive root partition: {}", e))
}
};
match stream_to(&root, &data).await {
Ok(_) => HttpResponse::Ok()
.content_type(ContentType::plaintext())
.body("successfully updated inactive root partition"),
Err(e) => HttpResponse::InternalServerError()
.content_type(ContentType::plaintext())
.body(format!("can't update inactive root partition: {}", e)),
}
}
async fn handle_switch() -> HttpResponse {
println!("switch to inactive root");
match switch_to_inactive_root() {
Ok(_) => HttpResponse::Ok()
.content_type(ContentType::plaintext())
.body("successfully switched to inactive root partition"),
Err(e) => HttpResponse::InternalServerError()
.content_type(ContentType::plaintext())
.body(format!("can't switch to inactive root partition: {}", e)),
}
}
async fn handle_data_read(info: web::Query<DataRequest>) -> HttpResponse {
let query = info.into_inner();
match fs::read(&query.path) {
Ok(data) => HttpResponse::Ok()
.content_type(ContentType::octet_stream())
.body(data),
Err(e) => HttpResponse::NotFound()
.content_type(ContentType::plaintext())
.body(format!("can't read file at {}: {}", query.path, e)),
}
}
async fn handle_data_write(info: web::Query<DataRequest>, data: web::Bytes) -> HttpResponse {
let query = info.into_inner();
match stream_to(&query.path, &data).await {
Ok(_) => HttpResponse::Ok()
.content_type(ContentType::plaintext())
.body(format!("successfully wrote to {}", query.path)),
Err(e) => HttpResponse::InternalServerError()
.content_type(ContentType::plaintext())
.body(format!("can't write file at {}: {}", query.path, e)),
}
}
#[actix_web::main]
async fn main() -> io::Result<()> {
match start().await {
Ok(_) => {}
Err(e) => {
println!("start error: {}", e);
return Ok(());
}
}
Ok(())
}
async fn start() -> Result<()> {
let config = load_rustls_config()?;
println!("start https://[::]:8443");
Ok(HttpServer::new(|| {
let auth = HttpAuthentication::basic(basic_auth_validator);
App::new()
.app_data(web::PayloadConfig::default().limit(512 * MiB))
.wrap(auth)
.service(web::resource("/reboot").to(handle_reboot))
.service(web::resource("/shutdown").to(handle_shutdown))
.service(web::resource("/update/boot").to(handle_update_boot))
.service(web::resource("/update/mbr").to(handle_update_mbr))
.service(web::resource("/update/root").to(handle_update_root))
.service(web::resource("/switch").to(handle_switch))
.service(web::resource("/data/read").to(handle_data_read))
.service(web::resource("/data/write").to(handle_data_write))
})
.bind_rustls("[::]:8443", config)?
.run()
.await?)
}
fn load_rustls_config() -> Result<ServerConfig> {
let config = ServerConfig::builder()
.with_safe_defaults()
.with_no_client_auth();
let cert_file = &mut BufReader::new(File::open("/data/admind_cert.pem")?);
let key_file = &mut BufReader::new(File::open("/data/admind_key.pem")?);
let cert_chain = certs(cert_file)?.into_iter().map(Certificate).collect();
let mut keys: Vec<PrivateKey> = pkcs8_private_keys(key_file)?
.into_iter()
.map(PrivateKey)
.collect();
if keys.is_empty() {
return Err(Error::NoPrivateKeys);
}
Ok(config.with_single_cert(cert_chain, keys.remove(0))?)
}
async fn basic_auth_validator(
req: ServiceRequest,
credentials: BasicAuth,
) -> std::result::Result<ServiceRequest, (actix_web::Error, ServiceRequest)> {
let config = req.app_data::<Config>().cloned().unwrap_or_default();
match validate_credentials(
credentials.user_id(),
credentials.password().unwrap_or_default().trim(),
) {
Ok(res) => {
if res {
Ok(req)
} else {
Err((AuthenticationError::from(config).into(), req))
}
}
Err(_) => Err((AuthenticationError::from(config).into(), req)),
}
}
fn validate_credentials(user_id: &str, user_password: &str) -> io::Result<bool> {
let correct_password = fs::read("/data/admind.passwd")?;
if user_id == "rustkrazy" && constant_time_eq(user_password.as_bytes(), &correct_password) {
return Ok(true);
}
Err(io::Error::new(
io::ErrorKind::PermissionDenied,
"Invalid credentials",
))
}
fn modify_cmdline(new: &str) -> Result<()> {
let boot = boot_dev()?;
let boot_partition = OpenOptions::new().read(true).write(true).open(boot)?;
let buf_stream = BufStream::new(boot_partition);
let bootfs = fatfs::FileSystem::new(buf_stream, fatfs::FsOptions::new())?;
let mut file = bootfs.root_dir().open_file("cmdline.txt")?;
file.write_all(new.as_bytes())?;
Ok(())
}
fn dev() -> Result<&'static str> {
let devs = ["/dev/mmcblk0", "/dev/sda", "/dev/vda"];
for dev in devs {
if fs::metadata(dev).is_ok() {
return Ok(dev);
}
}
Err(Error::NoDiskDev)
}
fn boot_dev() -> Result<&'static str> {
Ok(match dev()? {
"/dev/mmcblk0" => "/dev/mmcblk0p1",
"/dev/sda" => "/dev/sda1",
"/dev/vda" => "/dev/vda1",
_ => unreachable!(),
})
}
// fn active_root() -> Result<String> {
// let cmdline = fs::read_to_string("/proc/cmdline")?;
//
// for seg in cmdline.split(' ') {
// if seg.starts_with("root=PARTUUID=00000000-") {
// let root_id = seg
// .split("root=PARTUUID=00000000-0")
// .collect::<Vec<&str>>()
// .into_iter()
// .next_back()
// .ok_or(Error::RootdevUnset)?;
//
// return Ok(match dev()? {
// "/dev/mmcblk0" => format!("/dev/mmcblk0p{}", root_id),
// "/dev/sda" => format!("/dev/sda{}", root_id),
// "/dev/vda" => format!("/dev/vda{}", root_id),
// _ => unreachable!(),
// });
// }
// }
//
// Err(Error::RootdevUnset)
// }
fn inactive_root() -> Result<String> {
let cmdline = fs::read_to_string("/proc/cmdline")?;
for seg in cmdline.split(' ') {
if seg.starts_with("root=PARTUUID=00000000-") {
let root_id = match seg
.split("root=PARTUUID=00000000-0")
.collect::<Vec<&str>>()
.into_iter()
.next_back()
.ok_or(Error::RootdevUnset)?
{
"2" => "3",
"3" => "2",
_ => unreachable!(),
};
return Ok(match dev()? {
"/dev/mmcblk0" => format!("/dev/mmcblk0p{}", root_id),
"/dev/sda" => format!("/dev/sda{}", root_id),
"/dev/vda" => format!("/dev/vda{}", root_id),
_ => unreachable!(),
});
}
}
Err(Error::RootdevUnset)
}
async fn stream_to(dst: &str, data: &[u8]) -> Result<()> {
let mut file = File::create(dst)?;
file.write_all(data)?;
file.sync_all()?;
nix::unistd::sync();
Ok(())
}
fn switch_to_inactive_root() -> Result<()> {
let new = inactive_root()?;
let new = String::from("root=PARTUUID=00000000-0") + &new.chars().last().unwrap().to_string();
let cmdline = format!("{} init=/bin/init rootwait console=tty1", new);
modify_cmdline(&cmdline)?;
nix::unistd::sync();
Ok(())
}
|