]>
Commit | Line | Data |
---|---|---|
550e0d88 | 1 | use std::path::PathBuf; |
331b869d | 2 | use std::collections::HashMap; |
ea0b8b6e | 3 | |
f7d4e4b5 | 4 | use anyhow::{bail, format_err, Error}; |
9894469e | 5 | use serde_json::{json, Value}; |
9894469e | 6 | |
9e165b5c | 7 | use proxmox::api::{api, cli::*, RpcEnvironment, ApiHandler}; |
769f8c99 | 8 | |
550e0d88 | 9 | use proxmox_backup::configdir; |
769f8c99 | 10 | use proxmox_backup::tools; |
f357390c | 11 | use proxmox_backup::config::{self, remote::{self, Remote}}; |
9894469e | 12 | use proxmox_backup::api2::{self, types::* }; |
769f8c99 DM |
13 | use proxmox_backup::client::*; |
14 | use proxmox_backup::tools::ticket::*; | |
15 | use proxmox_backup::auth_helpers::*; | |
16 | ||
769f8c99 DM |
17 | async fn view_task_result( |
18 | client: HttpClient, | |
19 | result: Value, | |
20 | output_format: &str, | |
21 | ) -> Result<(), Error> { | |
22 | let data = &result["data"]; | |
23 | if output_format == "text" { | |
24 | if let Some(upid) = data.as_str() { | |
25 | display_task_log(client, upid, true).await?; | |
26 | } | |
27 | } else { | |
28 | format_and_print_result(&data, &output_format); | |
29 | } | |
30 | ||
31 | Ok(()) | |
32 | } | |
211fabd7 | 33 | |
47d47121 DM |
34 | fn connect() -> Result<HttpClient, Error> { |
35 | ||
36 | let uid = nix::unistd::Uid::current(); | |
37 | ||
d59dbeca | 38 | let mut options = HttpClientOptions::new() |
5030b7ce | 39 | .prefix(Some("proxmox-backup".to_string())) |
d59dbeca DM |
40 | .verify_cert(false); // not required for connection to localhost |
41 | ||
47d47121 DM |
42 | let client = if uid.is_root() { |
43 | let ticket = assemble_rsa_ticket(private_auth_key(), "PBS", Some("root@pam"), None)?; | |
d59dbeca DM |
44 | options = options.password(Some(ticket)); |
45 | HttpClient::new("localhost", "root@pam", options)? | |
47d47121 | 46 | } else { |
d59dbeca DM |
47 | options = options.ticket_cache(true).interactive(true); |
48 | HttpClient::new("localhost", "root@pam", options)? | |
47d47121 DM |
49 | }; |
50 | ||
51 | Ok(client) | |
52 | } | |
53 | ||
9894469e DM |
54 | #[api( |
55 | input: { | |
56 | properties: { | |
57 | "output-format": { | |
58 | schema: OUTPUT_FORMAT, | |
59 | optional: true, | |
60 | }, | |
61 | } | |
62 | } | |
63 | )] | |
64 | /// List configured remotes. | |
9e165b5c | 65 | fn list_remotes(param: Value, rpcenv: &mut dyn RpcEnvironment) -> Result<Value, Error> { |
688fbe07 | 66 | |
ac3faaf5 | 67 | let output_format = get_output_format(¶m); |
9894469e | 68 | |
9e165b5c DM |
69 | let info = &api2::config::remote::API_METHOD_LIST_REMOTES; |
70 | let mut data = match info.handler { | |
71 | ApiHandler::Sync(handler) => (handler)(param, info, rpcenv)?, | |
72 | _ => unreachable!(), | |
73 | }; | |
9894469e | 74 | |
ac3faaf5 | 75 | let options = default_table_format_options() |
93fbb4ef DM |
76 | .column(ColumnConfig::new("name")) |
77 | .column(ColumnConfig::new("host")) | |
78 | .column(ColumnConfig::new("userid")) | |
79 | .column(ColumnConfig::new("fingerprint")) | |
80 | .column(ColumnConfig::new("comment")); | |
9894469e | 81 | |
9e165b5c | 82 | format_and_print_result_full(&mut data, info.returns, &output_format, &options); |
9894469e DM |
83 | |
84 | Ok(Value::Null) | |
85 | } | |
86 | ||
87 | fn remote_commands() -> CommandLineInterface { | |
688fbe07 DM |
88 | |
89 | let cmd_def = CliCommandMap::new() | |
9894469e | 90 | .insert("list", CliCommand::new(&&API_METHOD_LIST_REMOTES)) |
688fbe07 DM |
91 | .insert( |
92 | "create", | |
93 | // fixme: howto handle password parameter? | |
f357390c | 94 | CliCommand::new(&api2::config::remote::API_METHOD_CREATE_REMOTE) |
688fbe07 DM |
95 | .arg_param(&["name"]) |
96 | ) | |
08195ac8 DM |
97 | .insert( |
98 | "update", | |
f357390c | 99 | CliCommand::new(&api2::config::remote::API_METHOD_UPDATE_REMOTE) |
08195ac8 | 100 | .arg_param(&["name"]) |
f357390c | 101 | .completion_cb("name", config::remote::complete_remote_name) |
08195ac8 | 102 | ) |
688fbe07 DM |
103 | .insert( |
104 | "remove", | |
f357390c | 105 | CliCommand::new(&api2::config::remote::API_METHOD_DELETE_REMOTE) |
688fbe07 | 106 | .arg_param(&["name"]) |
f357390c | 107 | .completion_cb("name", config::remote::complete_remote_name) |
688fbe07 DM |
108 | ); |
109 | ||
110 | cmd_def.into() | |
111 | } | |
112 | ||
579728c6 DM |
113 | #[api( |
114 | input: { | |
115 | properties: { | |
116 | "output-format": { | |
117 | schema: OUTPUT_FORMAT, | |
118 | optional: true, | |
119 | }, | |
120 | } | |
121 | } | |
122 | )] | |
123 | /// List configured users. | |
124 | fn list_users(param: Value, rpcenv: &mut dyn RpcEnvironment) -> Result<Value, Error> { | |
125 | ||
126 | let output_format = get_output_format(¶m); | |
127 | ||
685e1334 | 128 | let info = &api2::access::user::API_METHOD_LIST_USERS; |
579728c6 DM |
129 | let mut data = match info.handler { |
130 | ApiHandler::Sync(handler) => (handler)(param, info, rpcenv)?, | |
131 | _ => unreachable!(), | |
132 | }; | |
133 | ||
134 | let options = default_table_format_options() | |
135 | .column(ColumnConfig::new("userid")) | |
136 | .column(ColumnConfig::new("enable")) | |
137 | .column(ColumnConfig::new("expire")) | |
138 | .column(ColumnConfig::new("firstname")) | |
139 | .column(ColumnConfig::new("lastname")) | |
140 | .column(ColumnConfig::new("email")) | |
141 | .column(ColumnConfig::new("comment")); | |
142 | ||
143 | format_and_print_result_full(&mut data, info.returns, &output_format, &options); | |
144 | ||
145 | Ok(Value::Null) | |
146 | } | |
147 | ||
148 | fn user_commands() -> CommandLineInterface { | |
149 | ||
150 | let cmd_def = CliCommandMap::new() | |
151 | .insert("list", CliCommand::new(&&API_METHOD_LIST_USERS)) | |
152 | .insert( | |
153 | "create", | |
154 | // fixme: howto handle password parameter? | |
685e1334 | 155 | CliCommand::new(&api2::access::user::API_METHOD_CREATE_USER) |
579728c6 DM |
156 | .arg_param(&["userid"]) |
157 | ) | |
158 | .insert( | |
159 | "update", | |
685e1334 | 160 | CliCommand::new(&api2::access::user::API_METHOD_UPDATE_USER) |
579728c6 DM |
161 | .arg_param(&["userid"]) |
162 | .completion_cb("userid", config::user::complete_user_name) | |
163 | ) | |
164 | .insert( | |
165 | "remove", | |
685e1334 | 166 | CliCommand::new(&api2::access::user::API_METHOD_DELETE_USER) |
579728c6 DM |
167 | .arg_param(&["userid"]) |
168 | .completion_cb("userid", config::user::complete_user_name) | |
169 | ); | |
170 | ||
171 | cmd_def.into() | |
172 | } | |
173 | ||
ed3e60ae DM |
174 | #[api( |
175 | input: { | |
176 | properties: { | |
177 | "output-format": { | |
178 | schema: OUTPUT_FORMAT, | |
179 | optional: true, | |
180 | }, | |
181 | } | |
182 | } | |
183 | )] | |
184 | /// Access Control list. | |
185 | fn list_acls(param: Value, rpcenv: &mut dyn RpcEnvironment) -> Result<Value, Error> { | |
186 | ||
187 | let output_format = get_output_format(¶m); | |
188 | ||
189 | let info = &api2::access::acl::API_METHOD_READ_ACL; | |
190 | let mut data = match info.handler { | |
191 | ApiHandler::Sync(handler) => (handler)(param, info, rpcenv)?, | |
192 | _ => unreachable!(), | |
193 | }; | |
194 | ||
195 | fn render_ugid(value: &Value, record: &Value) -> Result<String, Error> { | |
196 | if value.is_null() { return Ok(String::new()); } | |
197 | let ugid = value.as_str().unwrap(); | |
198 | let ugid_type = record["ugid_type"].as_str().unwrap(); | |
199 | ||
200 | if ugid_type == "user" { | |
201 | Ok(ugid.to_string()) | |
202 | } else if ugid_type == "group" { | |
203 | Ok(format!("@{}", ugid)) | |
204 | } else { | |
205 | bail!("render_ugid: got unknown ugid_type"); | |
206 | } | |
207 | } | |
208 | ||
209 | let options = default_table_format_options() | |
210 | .column(ColumnConfig::new("ugid").renderer(render_ugid)) | |
211 | .column(ColumnConfig::new("path")) | |
212 | .column(ColumnConfig::new("propagate")) | |
213 | .column(ColumnConfig::new("roleid")); | |
214 | ||
215 | format_and_print_result_full(&mut data, info.returns, &output_format, &options); | |
216 | ||
217 | Ok(Value::Null) | |
218 | } | |
219 | ||
220 | fn acl_commands() -> CommandLineInterface { | |
221 | ||
222 | let cmd_def = CliCommandMap::new() | |
9765092e DM |
223 | .insert("list", CliCommand::new(&&API_METHOD_LIST_ACLS)) |
224 | .insert( | |
225 | "update", | |
226 | CliCommand::new(&api2::access::acl::API_METHOD_UPDATE_ACL) | |
227 | .arg_param(&["path", "role"]) | |
228 | .completion_cb("userid", config::user::complete_user_name) | |
229 | .completion_cb("path", config::datastore::complete_acl_path) | |
230 | ||
231 | ); | |
ed3e60ae DM |
232 | |
233 | cmd_def.into() | |
234 | } | |
235 | ||
bf004ecd DM |
236 | #[api( |
237 | input: { | |
238 | properties: { | |
239 | "output-format": { | |
240 | schema: OUTPUT_FORMAT, | |
241 | optional: true, | |
242 | }, | |
243 | } | |
244 | } | |
245 | )] | |
246 | /// Network device list. | |
247 | fn list_network_devices(param: Value, rpcenv: &mut dyn RpcEnvironment) -> Result<Value, Error> { | |
248 | ||
249 | let output_format = get_output_format(¶m); | |
250 | ||
251 | let info = &api2::config::network::API_METHOD_LIST_NETWORK_DEVICES; | |
252 | let mut data = match info.handler { | |
253 | ApiHandler::Sync(handler) => (handler)(param, info, rpcenv)?, | |
254 | _ => unreachable!(), | |
255 | }; | |
256 | ||
2eefd9ae DM |
257 | if let Some(changes) = rpcenv.get_result_attrib("changes") { |
258 | if let Some(diff) = changes.as_str() { | |
259 | eprintln!("pending changes:\n{}\n", diff); | |
260 | } | |
261 | } | |
262 | ||
4cb6bd89 DM |
263 | fn render_address(_value: &Value, record: &Value) -> Result<String, Error> { |
264 | let mut text = String::new(); | |
265 | ||
266 | if let Some(cidr) = record["cidr_v4"].as_str() { | |
267 | text.push_str(cidr); | |
268 | } | |
269 | if let Some(cidr) = record["cidr_v6"].as_str() { | |
270 | if !text.is_empty() { text.push('\n'); } | |
271 | text.push_str(cidr); | |
272 | } | |
273 | ||
274 | Ok(text) | |
275 | } | |
276 | ||
277 | fn render_gateway(_value: &Value, record: &Value) -> Result<String, Error> { | |
278 | let mut text = String::new(); | |
279 | ||
280 | if let Some(gateway) = record["gateway_v4"].as_str() { | |
281 | text.push_str(gateway); | |
282 | } | |
283 | if let Some(gateway) = record["gateway_v6"].as_str() { | |
284 | if !text.is_empty() { text.push('\n'); } | |
285 | text.push_str(gateway); | |
286 | } | |
287 | ||
288 | Ok(text) | |
289 | } | |
290 | ||
bf004ecd | 291 | let options = default_table_format_options() |
02269f3d | 292 | .column(ColumnConfig::new("interface_type").header("type")) |
bf004ecd DM |
293 | .column(ColumnConfig::new("name")) |
294 | .column(ColumnConfig::new("auto")) | |
295 | .column(ColumnConfig::new("method_v4")) | |
296 | .column(ColumnConfig::new("method_v6")) | |
4cb6bd89 DM |
297 | .column(ColumnConfig::new("cidr_v4").header("address").renderer(render_address)) |
298 | .column(ColumnConfig::new("gateway_v4").header("gateway").renderer(render_gateway)); | |
bf004ecd DM |
299 | |
300 | format_and_print_result_full(&mut data, info.returns, &output_format, &options); | |
301 | ||
302 | Ok(Value::Null) | |
303 | } | |
304 | ||
ca0e5347 DM |
305 | fn network_commands() -> CommandLineInterface { |
306 | ||
307 | let cmd_def = CliCommandMap::new() | |
bf004ecd | 308 | .insert("list", CliCommand::new(&API_METHOD_LIST_NETWORK_DEVICES)) |
2eefd9ae DM |
309 | .insert( |
310 | "update", | |
311 | CliCommand::new(&api2::config::network::API_METHOD_UPDATE_INTERFACE) | |
df6bb03d DM |
312 | .arg_param(&["name"]) |
313 | .completion_cb("name", config::network::complete_interface_name) | |
314 | ) | |
2eefd9ae DM |
315 | .insert( |
316 | "remove", | |
317 | CliCommand::new(&api2::config::network::API_METHOD_DELETE_INTERFACE) | |
df6bb03d DM |
318 | .arg_param(&["name"]) |
319 | .completion_cb("name", config::network::complete_interface_name) | |
2eefd9ae DM |
320 | ) |
321 | .insert( | |
322 | "reload", | |
323 | CliCommand::new(&api2::config::network::API_METHOD_RELOAD_NETWORK_CONFIG) | |
df6bb03d | 324 | ); |
ca0e5347 DM |
325 | |
326 | cmd_def.into() | |
327 | } | |
328 | ||
b1564af2 DM |
329 | #[api( |
330 | input: { | |
331 | properties: { | |
332 | "output-format": { | |
333 | schema: OUTPUT_FORMAT, | |
334 | optional: true, | |
335 | }, | |
336 | } | |
337 | } | |
338 | )] | |
339 | /// Datastore list. | |
340 | fn list_datastores(param: Value, rpcenv: &mut dyn RpcEnvironment) -> Result<Value, Error> { | |
341 | ||
342 | let output_format = get_output_format(¶m); | |
343 | ||
344 | let info = &api2::config::datastore::API_METHOD_LIST_DATASTORES; | |
345 | let mut data = match info.handler { | |
346 | ApiHandler::Sync(handler) => (handler)(param, info, rpcenv)?, | |
347 | _ => unreachable!(), | |
348 | }; | |
349 | ||
350 | let options = default_table_format_options() | |
351 | .column(ColumnConfig::new("name")) | |
352 | .column(ColumnConfig::new("path")) | |
353 | .column(ColumnConfig::new("comment")); | |
354 | ||
355 | format_and_print_result_full(&mut data, info.returns, &output_format, &options); | |
356 | ||
357 | Ok(Value::Null) | |
358 | } | |
359 | ||
9f6ab1fc | 360 | fn datastore_commands() -> CommandLineInterface { |
ea0b8b6e | 361 | |
6460764d | 362 | let cmd_def = CliCommandMap::new() |
b1564af2 | 363 | .insert("list", CliCommand::new(&API_METHOD_LIST_DATASTORES)) |
6460764d | 364 | .insert("create", |
688fbe07 | 365 | CliCommand::new(&api2::config::datastore::API_METHOD_CREATE_DATASTORE) |
49fddd98 | 366 | .arg_param(&["name", "path"]) |
48ef3c33 | 367 | ) |
ddc52662 DM |
368 | .insert("update", |
369 | CliCommand::new(&api2::config::datastore::API_METHOD_UPDATE_DATASTORE) | |
370 | .arg_param(&["name"]) | |
3be839c6 | 371 | .completion_cb("name", config::datastore::complete_datastore_name) |
ddc52662 | 372 | ) |
6460764d | 373 | .insert("remove", |
688fbe07 | 374 | CliCommand::new(&api2::config::datastore::API_METHOD_DELETE_DATASTORE) |
49fddd98 | 375 | .arg_param(&["name"]) |
07b4694a | 376 | .completion_cb("name", config::datastore::complete_datastore_name) |
48ef3c33 | 377 | ); |
211fabd7 | 378 | |
8f62336b | 379 | cmd_def.into() |
211fabd7 DM |
380 | } |
381 | ||
691c89a0 | 382 | |
769f8c99 DM |
383 | #[api( |
384 | input: { | |
385 | properties: { | |
386 | store: { | |
387 | schema: DATASTORE_SCHEMA, | |
388 | }, | |
389 | "output-format": { | |
390 | schema: OUTPUT_FORMAT, | |
391 | optional: true, | |
392 | }, | |
393 | } | |
394 | } | |
395 | )] | |
396 | /// Start garbage collection for a specific datastore. | |
397 | async fn start_garbage_collection(param: Value) -> Result<Value, Error> { | |
691c89a0 | 398 | |
ac3faaf5 | 399 | let output_format = get_output_format(¶m); |
691c89a0 | 400 | |
769f8c99 DM |
401 | let store = tools::required_string_param(¶m, "store")?; |
402 | ||
47d47121 | 403 | let mut client = connect()?; |
769f8c99 DM |
404 | |
405 | let path = format!("api2/json/admin/datastore/{}/gc", store); | |
406 | ||
407 | let result = client.post(&path, None).await?; | |
408 | ||
409 | view_task_result(client, result, &output_format).await?; | |
410 | ||
411 | Ok(Value::Null) | |
412 | } | |
413 | ||
414 | #[api( | |
415 | input: { | |
416 | properties: { | |
417 | store: { | |
418 | schema: DATASTORE_SCHEMA, | |
419 | }, | |
420 | "output-format": { | |
421 | schema: OUTPUT_FORMAT, | |
422 | optional: true, | |
423 | }, | |
424 | } | |
425 | } | |
426 | )] | |
427 | /// Show garbage collection status for a specific datastore. | |
428 | async fn garbage_collection_status(param: Value) -> Result<Value, Error> { | |
429 | ||
ac3faaf5 | 430 | let output_format = get_output_format(¶m); |
769f8c99 DM |
431 | |
432 | let store = tools::required_string_param(¶m, "store")?; | |
433 | ||
47d47121 | 434 | let client = connect()?; |
769f8c99 DM |
435 | |
436 | let path = format!("api2/json/admin/datastore/{}/gc", store); | |
437 | ||
9894469e DM |
438 | let mut result = client.get(&path, None).await?; |
439 | let mut data = result["data"].take(); | |
440 | let schema = api2::admin::datastore::API_RETURN_SCHEMA_GARBAGE_COLLECTION_STATUS; | |
441 | ||
ac3faaf5 | 442 | let options = default_table_format_options(); |
9894469e DM |
443 | |
444 | format_and_print_result_full(&mut data, schema, &output_format, &options); | |
769f8c99 DM |
445 | |
446 | Ok(Value::Null) | |
447 | } | |
448 | ||
449 | fn garbage_collection_commands() -> CommandLineInterface { | |
691c89a0 DM |
450 | |
451 | let cmd_def = CliCommandMap::new() | |
452 | .insert("status", | |
769f8c99 | 453 | CliCommand::new(&API_METHOD_GARBAGE_COLLECTION_STATUS) |
49fddd98 | 454 | .arg_param(&["store"]) |
9ac1045c | 455 | .completion_cb("store", config::datastore::complete_datastore_name) |
48ef3c33 | 456 | ) |
691c89a0 | 457 | .insert("start", |
769f8c99 | 458 | CliCommand::new(&API_METHOD_START_GARBAGE_COLLECTION) |
49fddd98 | 459 | .arg_param(&["store"]) |
9ac1045c | 460 | .completion_cb("store", config::datastore::complete_datastore_name) |
48ef3c33 | 461 | ); |
691c89a0 DM |
462 | |
463 | cmd_def.into() | |
464 | } | |
465 | ||
47d47121 DM |
466 | #[api( |
467 | input: { | |
468 | properties: { | |
469 | limit: { | |
470 | description: "The maximal number of tasks to list.", | |
471 | type: Integer, | |
472 | optional: true, | |
473 | minimum: 1, | |
474 | maximum: 1000, | |
475 | default: 50, | |
476 | }, | |
477 | "output-format": { | |
478 | schema: OUTPUT_FORMAT, | |
479 | optional: true, | |
480 | }, | |
481 | all: { | |
482 | type: Boolean, | |
483 | description: "Also list stopped tasks.", | |
484 | optional: true, | |
485 | } | |
486 | } | |
487 | } | |
488 | )] | |
489 | /// List running server tasks. | |
490 | async fn task_list(param: Value) -> Result<Value, Error> { | |
491 | ||
ac3faaf5 | 492 | let output_format = get_output_format(¶m); |
47d47121 DM |
493 | |
494 | let client = connect()?; | |
495 | ||
496 | let limit = param["limit"].as_u64().unwrap_or(50) as usize; | |
497 | let running = !param["all"].as_bool().unwrap_or(false); | |
498 | let args = json!({ | |
499 | "running": running, | |
500 | "start": 0, | |
501 | "limit": limit, | |
502 | }); | |
9894469e | 503 | let mut result = client.get("api2/json/nodes/localhost/tasks", Some(args)).await?; |
47d47121 | 504 | |
9894469e DM |
505 | let mut data = result["data"].take(); |
506 | let schema = api2::node::tasks::API_RETURN_SCHEMA_LIST_TASKS; | |
47d47121 | 507 | |
ac3faaf5 | 508 | let options = default_table_format_options() |
4939255f DM |
509 | .column(ColumnConfig::new("starttime").right_align(false).renderer(tools::format::render_epoch)) |
510 | .column(ColumnConfig::new("endtime").right_align(false).renderer(tools::format::render_epoch)) | |
93fbb4ef | 511 | .column(ColumnConfig::new("upid")) |
4939255f | 512 | .column(ColumnConfig::new("status").renderer(tools::format::render_task_status)); |
9894469e DM |
513 | |
514 | format_and_print_result_full(&mut data, schema, &output_format, &options); | |
47d47121 DM |
515 | |
516 | Ok(Value::Null) | |
517 | } | |
518 | ||
519 | #[api( | |
520 | input: { | |
521 | properties: { | |
522 | upid: { | |
523 | schema: UPID_SCHEMA, | |
524 | }, | |
525 | } | |
526 | } | |
527 | )] | |
528 | /// Display the task log. | |
529 | async fn task_log(param: Value) -> Result<Value, Error> { | |
530 | ||
531 | let upid = tools::required_string_param(¶m, "upid")?; | |
532 | ||
533 | let client = connect()?; | |
534 | ||
535 | display_task_log(client, upid, true).await?; | |
536 | ||
537 | Ok(Value::Null) | |
538 | } | |
539 | ||
540 | #[api( | |
541 | input: { | |
542 | properties: { | |
543 | upid: { | |
544 | schema: UPID_SCHEMA, | |
545 | }, | |
546 | } | |
547 | } | |
548 | )] | |
549 | /// Try to stop a specific task. | |
550 | async fn task_stop(param: Value) -> Result<Value, Error> { | |
551 | ||
552 | let upid_str = tools::required_string_param(¶m, "upid")?; | |
553 | ||
554 | let mut client = connect()?; | |
555 | ||
556 | let path = format!("api2/json/nodes/localhost/tasks/{}", upid_str); | |
557 | let _ = client.delete(&path, None).await?; | |
558 | ||
559 | Ok(Value::Null) | |
560 | } | |
561 | ||
562 | fn task_mgmt_cli() -> CommandLineInterface { | |
563 | ||
564 | let task_log_cmd_def = CliCommand::new(&API_METHOD_TASK_LOG) | |
565 | .arg_param(&["upid"]); | |
566 | ||
567 | let task_stop_cmd_def = CliCommand::new(&API_METHOD_TASK_STOP) | |
568 | .arg_param(&["upid"]); | |
569 | ||
570 | let cmd_def = CliCommandMap::new() | |
571 | .insert("list", CliCommand::new(&API_METHOD_TASK_LIST)) | |
572 | .insert("log", task_log_cmd_def) | |
573 | .insert("stop", task_stop_cmd_def); | |
574 | ||
575 | cmd_def.into() | |
576 | } | |
577 | ||
e739a8d8 DM |
578 | fn x509name_to_string(name: &openssl::x509::X509NameRef) -> Result<String, Error> { |
579 | let mut parts = Vec::new(); | |
580 | for entry in name.entries() { | |
581 | parts.push(format!("{} = {}", entry.object().nid().short_name()?, entry.data().as_utf8()?)); | |
582 | } | |
583 | Ok(parts.join(", ")) | |
584 | } | |
585 | ||
586 | #[api] | |
587 | /// Diplay node certificate information. | |
588 | fn cert_info() -> Result<(), Error> { | |
589 | ||
590 | let cert_path = PathBuf::from(configdir!("/proxy.pem")); | |
591 | ||
592 | let cert_pem = proxmox::tools::fs::file_get_contents(&cert_path)?; | |
593 | ||
594 | let cert = openssl::x509::X509::from_pem(&cert_pem)?; | |
595 | ||
596 | println!("Subject: {}", x509name_to_string(cert.subject_name())?); | |
597 | ||
598 | if let Some(san) = cert.subject_alt_names() { | |
599 | for name in san.iter() { | |
600 | if let Some(v) = name.dnsname() { | |
601 | println!(" DNS:{}", v); | |
602 | } else if let Some(v) = name.ipaddress() { | |
603 | println!(" IP:{:?}", v); | |
604 | } else if let Some(v) = name.email() { | |
605 | println!(" EMAIL:{}", v); | |
606 | } else if let Some(v) = name.uri() { | |
607 | println!(" URI:{}", v); | |
608 | } | |
609 | } | |
610 | } | |
611 | ||
612 | println!("Issuer: {}", x509name_to_string(cert.issuer_name())?); | |
613 | println!("Validity:"); | |
614 | println!(" Not Before: {}", cert.not_before()); | |
615 | println!(" Not After : {}", cert.not_after()); | |
616 | ||
617 | let fp = cert.digest(openssl::hash::MessageDigest::sha256())?; | |
618 | let fp_string = proxmox::tools::digest_to_hex(&fp); | |
619 | let fp_string = fp_string.as_bytes().chunks(2).map(|v| std::str::from_utf8(v).unwrap()) | |
620 | .collect::<Vec<&str>>().join(":"); | |
621 | ||
622 | println!("Fingerprint (sha256): {}", fp_string); | |
623 | ||
624 | let pubkey = cert.public_key()?; | |
625 | println!("Public key type: {}", openssl::nid::Nid::from_raw(pubkey.id().as_raw()).long_name()?); | |
626 | println!("Public key bits: {}", pubkey.bits()); | |
627 | ||
628 | Ok(()) | |
629 | } | |
630 | ||
550e0d88 DM |
631 | #[api( |
632 | input: { | |
633 | properties: { | |
634 | force: { | |
635 | description: "Force generation of new SSL certifate.", | |
636 | type: Boolean, | |
637 | optional:true, | |
638 | }, | |
639 | } | |
640 | }, | |
641 | )] | |
642 | /// Update node certificates and generate all needed files/directories. | |
643 | fn update_certs(force: Option<bool>) -> Result<(), Error> { | |
644 | ||
550e0d88 DM |
645 | config::create_configdir()?; |
646 | ||
647 | if let Err(err) = generate_auth_key() { | |
648 | bail!("unable to generate auth key - {}", err); | |
649 | } | |
650 | ||
651 | if let Err(err) = generate_csrf_key() { | |
652 | bail!("unable to generate csrf key - {}", err); | |
653 | } | |
654 | ||
f8fd5095 | 655 | config::update_self_signed_cert(force.unwrap_or(false))?; |
550e0d88 DM |
656 | |
657 | Ok(()) | |
658 | } | |
659 | ||
660 | fn cert_mgmt_cli() -> CommandLineInterface { | |
661 | ||
550e0d88 | 662 | let cmd_def = CliCommandMap::new() |
e739a8d8 DM |
663 | .insert("info", CliCommand::new(&API_METHOD_CERT_INFO)) |
664 | .insert("update", CliCommand::new(&API_METHOD_UPDATE_CERTS)); | |
550e0d88 DM |
665 | |
666 | cmd_def.into() | |
667 | } | |
668 | ||
4b4eba0b | 669 | // fixme: avoid API redefinition |
0eb0e024 DM |
670 | #[api( |
671 | input: { | |
672 | properties: { | |
eb506c83 | 673 | "local-store": { |
0eb0e024 DM |
674 | schema: DATASTORE_SCHEMA, |
675 | }, | |
676 | remote: { | |
167971ed | 677 | schema: REMOTE_ID_SCHEMA, |
0eb0e024 DM |
678 | }, |
679 | "remote-store": { | |
680 | schema: DATASTORE_SCHEMA, | |
681 | }, | |
4b4eba0b DM |
682 | delete: { |
683 | description: "Delete vanished backups. This remove the local copy if the remote backup was deleted.", | |
684 | type: Boolean, | |
685 | optional: true, | |
686 | default: true, | |
687 | }, | |
0eb0e024 DM |
688 | "output-format": { |
689 | schema: OUTPUT_FORMAT, | |
690 | optional: true, | |
691 | }, | |
692 | } | |
693 | } | |
694 | )] | |
eb506c83 DM |
695 | /// Sync datastore from another repository |
696 | async fn pull_datastore( | |
0eb0e024 DM |
697 | remote: String, |
698 | remote_store: String, | |
eb506c83 | 699 | local_store: String, |
4b4eba0b | 700 | delete: Option<bool>, |
ac3faaf5 | 701 | param: Value, |
0eb0e024 DM |
702 | ) -> Result<Value, Error> { |
703 | ||
ac3faaf5 | 704 | let output_format = get_output_format(¶m); |
0eb0e024 DM |
705 | |
706 | let mut client = connect()?; | |
707 | ||
4b4eba0b | 708 | let mut args = json!({ |
eb506c83 | 709 | "store": local_store, |
94609e23 | 710 | "remote": remote, |
0eb0e024 | 711 | "remote-store": remote_store, |
0eb0e024 DM |
712 | }); |
713 | ||
4b4eba0b DM |
714 | if let Some(delete) = delete { |
715 | args["delete"] = delete.into(); | |
716 | } | |
717 | ||
eb506c83 | 718 | let result = client.post("api2/json/pull", Some(args)).await?; |
0eb0e024 DM |
719 | |
720 | view_task_result(client, result, &output_format).await?; | |
721 | ||
722 | Ok(Value::Null) | |
723 | } | |
724 | ||
211fabd7 DM |
725 | fn main() { |
726 | ||
6460764d | 727 | let cmd_def = CliCommandMap::new() |
ed3e60ae | 728 | .insert("acl", acl_commands()) |
48ef3c33 | 729 | .insert("datastore", datastore_commands()) |
ca0e5347 | 730 | .insert("network", network_commands()) |
579728c6 | 731 | .insert("user", user_commands()) |
f357390c | 732 | .insert("remote", remote_commands()) |
47d47121 | 733 | .insert("garbage-collection", garbage_collection_commands()) |
550e0d88 | 734 | .insert("cert", cert_mgmt_cli()) |
0eb0e024 DM |
735 | .insert("task", task_mgmt_cli()) |
736 | .insert( | |
eb506c83 DM |
737 | "pull", |
738 | CliCommand::new(&API_METHOD_PULL_DATASTORE) | |
739 | .arg_param(&["remote", "remote-store", "local-store"]) | |
740 | .completion_cb("local-store", config::datastore::complete_datastore_name) | |
f357390c | 741 | .completion_cb("remote", config::remote::complete_remote_name) |
331b869d | 742 | .completion_cb("remote-store", complete_remote_datastore_name) |
0eb0e024 | 743 | ); |
34d3ba52 | 744 | |
d08bc483 | 745 | proxmox_backup::tools::runtime::main(run_async_cli_command(cmd_def)); |
ea0b8b6e | 746 | } |
331b869d DM |
747 | |
748 | // shell completion helper | |
749 | pub fn complete_remote_datastore_name(_arg: &str, param: &HashMap<String, String>) -> Vec<String> { | |
750 | ||
751 | let mut list = Vec::new(); | |
752 | ||
9ea4bce4 | 753 | let _ = proxmox::try_block!({ |
331b869d | 754 | let remote = param.get("remote").ok_or_else(|| format_err!("no remote"))?; |
f357390c | 755 | let (remote_config, _digest) = remote::config()?; |
331b869d DM |
756 | |
757 | let remote: Remote = remote_config.lookup("remote", &remote)?; | |
758 | ||
d59dbeca DM |
759 | let options = HttpClientOptions::new() |
760 | .password(Some(remote.password.clone())) | |
761 | .fingerprint(remote.fingerprint.clone()); | |
762 | ||
331b869d DM |
763 | let client = HttpClient::new( |
764 | &remote.host, | |
765 | &remote.userid, | |
d59dbeca | 766 | options, |
331b869d DM |
767 | )?; |
768 | ||
03ac286c | 769 | let result = crate::tools::runtime::block_on(client.get("api2/json/admin/datastore", None))?; |
331b869d DM |
770 | |
771 | if let Some(data) = result["data"].as_array() { | |
772 | for item in data { | |
773 | if let Some(store) = item["store"].as_str() { | |
774 | list.push(store.to_owned()); | |
775 | } | |
776 | } | |
777 | } | |
778 | ||
779 | Ok(()) | |
780 | }).map_err(|_err: Error| { /* ignore */ }); | |
781 | ||
782 | list | |
783 | } |