]>
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 | ||
4cb6bd89 DM |
257 | fn render_address(_value: &Value, record: &Value) -> Result<String, Error> { |
258 | let mut text = String::new(); | |
259 | ||
260 | if let Some(cidr) = record["cidr_v4"].as_str() { | |
261 | text.push_str(cidr); | |
262 | } | |
263 | if let Some(cidr) = record["cidr_v6"].as_str() { | |
264 | if !text.is_empty() { text.push('\n'); } | |
265 | text.push_str(cidr); | |
266 | } | |
267 | ||
268 | Ok(text) | |
269 | } | |
270 | ||
271 | fn render_gateway(_value: &Value, record: &Value) -> Result<String, Error> { | |
272 | let mut text = String::new(); | |
273 | ||
274 | if let Some(gateway) = record["gateway_v4"].as_str() { | |
275 | text.push_str(gateway); | |
276 | } | |
277 | if let Some(gateway) = record["gateway_v6"].as_str() { | |
278 | if !text.is_empty() { text.push('\n'); } | |
279 | text.push_str(gateway); | |
280 | } | |
281 | ||
282 | Ok(text) | |
283 | } | |
284 | ||
bf004ecd | 285 | let options = default_table_format_options() |
02269f3d | 286 | .column(ColumnConfig::new("interface_type").header("type")) |
bf004ecd DM |
287 | .column(ColumnConfig::new("name")) |
288 | .column(ColumnConfig::new("auto")) | |
289 | .column(ColumnConfig::new("method_v4")) | |
290 | .column(ColumnConfig::new("method_v6")) | |
4cb6bd89 DM |
291 | .column(ColumnConfig::new("cidr_v4").header("address").renderer(render_address)) |
292 | .column(ColumnConfig::new("gateway_v4").header("gateway").renderer(render_gateway)); | |
bf004ecd DM |
293 | |
294 | format_and_print_result_full(&mut data, info.returns, &output_format, &options); | |
295 | ||
296 | Ok(Value::Null) | |
297 | } | |
298 | ||
ca0e5347 DM |
299 | fn network_commands() -> CommandLineInterface { |
300 | ||
301 | let cmd_def = CliCommandMap::new() | |
bf004ecd | 302 | .insert("list", CliCommand::new(&API_METHOD_LIST_NETWORK_DEVICES)) |
df6bb03d DM |
303 | .insert("update", |
304 | CliCommand::new(&api2::config::network::API_METHOD_UPDATE_INTERFACE) | |
305 | .arg_param(&["name"]) | |
306 | .completion_cb("name", config::network::complete_interface_name) | |
307 | ) | |
308 | .insert("remove", | |
309 | CliCommand::new(&api2::config::network::API_METHOD_DELETE_INTERFACE) | |
310 | .arg_param(&["name"]) | |
311 | .completion_cb("name", config::network::complete_interface_name) | |
312 | ); | |
ca0e5347 DM |
313 | |
314 | cmd_def.into() | |
315 | } | |
316 | ||
b1564af2 DM |
317 | #[api( |
318 | input: { | |
319 | properties: { | |
320 | "output-format": { | |
321 | schema: OUTPUT_FORMAT, | |
322 | optional: true, | |
323 | }, | |
324 | } | |
325 | } | |
326 | )] | |
327 | /// Datastore list. | |
328 | fn list_datastores(param: Value, rpcenv: &mut dyn RpcEnvironment) -> Result<Value, Error> { | |
329 | ||
330 | let output_format = get_output_format(¶m); | |
331 | ||
332 | let info = &api2::config::datastore::API_METHOD_LIST_DATASTORES; | |
333 | let mut data = match info.handler { | |
334 | ApiHandler::Sync(handler) => (handler)(param, info, rpcenv)?, | |
335 | _ => unreachable!(), | |
336 | }; | |
337 | ||
338 | let options = default_table_format_options() | |
339 | .column(ColumnConfig::new("name")) | |
340 | .column(ColumnConfig::new("path")) | |
341 | .column(ColumnConfig::new("comment")); | |
342 | ||
343 | format_and_print_result_full(&mut data, info.returns, &output_format, &options); | |
344 | ||
345 | Ok(Value::Null) | |
346 | } | |
347 | ||
9f6ab1fc | 348 | fn datastore_commands() -> CommandLineInterface { |
ea0b8b6e | 349 | |
6460764d | 350 | let cmd_def = CliCommandMap::new() |
b1564af2 | 351 | .insert("list", CliCommand::new(&API_METHOD_LIST_DATASTORES)) |
6460764d | 352 | .insert("create", |
688fbe07 | 353 | CliCommand::new(&api2::config::datastore::API_METHOD_CREATE_DATASTORE) |
49fddd98 | 354 | .arg_param(&["name", "path"]) |
48ef3c33 | 355 | ) |
ddc52662 DM |
356 | .insert("update", |
357 | CliCommand::new(&api2::config::datastore::API_METHOD_UPDATE_DATASTORE) | |
358 | .arg_param(&["name"]) | |
3be839c6 | 359 | .completion_cb("name", config::datastore::complete_datastore_name) |
ddc52662 | 360 | ) |
6460764d | 361 | .insert("remove", |
688fbe07 | 362 | CliCommand::new(&api2::config::datastore::API_METHOD_DELETE_DATASTORE) |
49fddd98 | 363 | .arg_param(&["name"]) |
07b4694a | 364 | .completion_cb("name", config::datastore::complete_datastore_name) |
48ef3c33 | 365 | ); |
211fabd7 | 366 | |
8f62336b | 367 | cmd_def.into() |
211fabd7 DM |
368 | } |
369 | ||
691c89a0 | 370 | |
769f8c99 DM |
371 | #[api( |
372 | input: { | |
373 | properties: { | |
374 | store: { | |
375 | schema: DATASTORE_SCHEMA, | |
376 | }, | |
377 | "output-format": { | |
378 | schema: OUTPUT_FORMAT, | |
379 | optional: true, | |
380 | }, | |
381 | } | |
382 | } | |
383 | )] | |
384 | /// Start garbage collection for a specific datastore. | |
385 | async fn start_garbage_collection(param: Value) -> Result<Value, Error> { | |
691c89a0 | 386 | |
ac3faaf5 | 387 | let output_format = get_output_format(¶m); |
691c89a0 | 388 | |
769f8c99 DM |
389 | let store = tools::required_string_param(¶m, "store")?; |
390 | ||
47d47121 | 391 | let mut client = connect()?; |
769f8c99 DM |
392 | |
393 | let path = format!("api2/json/admin/datastore/{}/gc", store); | |
394 | ||
395 | let result = client.post(&path, None).await?; | |
396 | ||
397 | view_task_result(client, result, &output_format).await?; | |
398 | ||
399 | Ok(Value::Null) | |
400 | } | |
401 | ||
402 | #[api( | |
403 | input: { | |
404 | properties: { | |
405 | store: { | |
406 | schema: DATASTORE_SCHEMA, | |
407 | }, | |
408 | "output-format": { | |
409 | schema: OUTPUT_FORMAT, | |
410 | optional: true, | |
411 | }, | |
412 | } | |
413 | } | |
414 | )] | |
415 | /// Show garbage collection status for a specific datastore. | |
416 | async fn garbage_collection_status(param: Value) -> Result<Value, Error> { | |
417 | ||
ac3faaf5 | 418 | let output_format = get_output_format(¶m); |
769f8c99 DM |
419 | |
420 | let store = tools::required_string_param(¶m, "store")?; | |
421 | ||
47d47121 | 422 | let client = connect()?; |
769f8c99 DM |
423 | |
424 | let path = format!("api2/json/admin/datastore/{}/gc", store); | |
425 | ||
9894469e DM |
426 | let mut result = client.get(&path, None).await?; |
427 | let mut data = result["data"].take(); | |
428 | let schema = api2::admin::datastore::API_RETURN_SCHEMA_GARBAGE_COLLECTION_STATUS; | |
429 | ||
ac3faaf5 | 430 | let options = default_table_format_options(); |
9894469e DM |
431 | |
432 | format_and_print_result_full(&mut data, schema, &output_format, &options); | |
769f8c99 DM |
433 | |
434 | Ok(Value::Null) | |
435 | } | |
436 | ||
437 | fn garbage_collection_commands() -> CommandLineInterface { | |
691c89a0 DM |
438 | |
439 | let cmd_def = CliCommandMap::new() | |
440 | .insert("status", | |
769f8c99 | 441 | CliCommand::new(&API_METHOD_GARBAGE_COLLECTION_STATUS) |
49fddd98 | 442 | .arg_param(&["store"]) |
9ac1045c | 443 | .completion_cb("store", config::datastore::complete_datastore_name) |
48ef3c33 | 444 | ) |
691c89a0 | 445 | .insert("start", |
769f8c99 | 446 | CliCommand::new(&API_METHOD_START_GARBAGE_COLLECTION) |
49fddd98 | 447 | .arg_param(&["store"]) |
9ac1045c | 448 | .completion_cb("store", config::datastore::complete_datastore_name) |
48ef3c33 | 449 | ); |
691c89a0 DM |
450 | |
451 | cmd_def.into() | |
452 | } | |
453 | ||
47d47121 DM |
454 | #[api( |
455 | input: { | |
456 | properties: { | |
457 | limit: { | |
458 | description: "The maximal number of tasks to list.", | |
459 | type: Integer, | |
460 | optional: true, | |
461 | minimum: 1, | |
462 | maximum: 1000, | |
463 | default: 50, | |
464 | }, | |
465 | "output-format": { | |
466 | schema: OUTPUT_FORMAT, | |
467 | optional: true, | |
468 | }, | |
469 | all: { | |
470 | type: Boolean, | |
471 | description: "Also list stopped tasks.", | |
472 | optional: true, | |
473 | } | |
474 | } | |
475 | } | |
476 | )] | |
477 | /// List running server tasks. | |
478 | async fn task_list(param: Value) -> Result<Value, Error> { | |
479 | ||
ac3faaf5 | 480 | let output_format = get_output_format(¶m); |
47d47121 DM |
481 | |
482 | let client = connect()?; | |
483 | ||
484 | let limit = param["limit"].as_u64().unwrap_or(50) as usize; | |
485 | let running = !param["all"].as_bool().unwrap_or(false); | |
486 | let args = json!({ | |
487 | "running": running, | |
488 | "start": 0, | |
489 | "limit": limit, | |
490 | }); | |
9894469e | 491 | let mut result = client.get("api2/json/nodes/localhost/tasks", Some(args)).await?; |
47d47121 | 492 | |
9894469e DM |
493 | let mut data = result["data"].take(); |
494 | let schema = api2::node::tasks::API_RETURN_SCHEMA_LIST_TASKS; | |
47d47121 | 495 | |
ac3faaf5 | 496 | let options = default_table_format_options() |
4939255f DM |
497 | .column(ColumnConfig::new("starttime").right_align(false).renderer(tools::format::render_epoch)) |
498 | .column(ColumnConfig::new("endtime").right_align(false).renderer(tools::format::render_epoch)) | |
93fbb4ef | 499 | .column(ColumnConfig::new("upid")) |
4939255f | 500 | .column(ColumnConfig::new("status").renderer(tools::format::render_task_status)); |
9894469e DM |
501 | |
502 | format_and_print_result_full(&mut data, schema, &output_format, &options); | |
47d47121 DM |
503 | |
504 | Ok(Value::Null) | |
505 | } | |
506 | ||
507 | #[api( | |
508 | input: { | |
509 | properties: { | |
510 | upid: { | |
511 | schema: UPID_SCHEMA, | |
512 | }, | |
513 | } | |
514 | } | |
515 | )] | |
516 | /// Display the task log. | |
517 | async fn task_log(param: Value) -> Result<Value, Error> { | |
518 | ||
519 | let upid = tools::required_string_param(¶m, "upid")?; | |
520 | ||
521 | let client = connect()?; | |
522 | ||
523 | display_task_log(client, upid, true).await?; | |
524 | ||
525 | Ok(Value::Null) | |
526 | } | |
527 | ||
528 | #[api( | |
529 | input: { | |
530 | properties: { | |
531 | upid: { | |
532 | schema: UPID_SCHEMA, | |
533 | }, | |
534 | } | |
535 | } | |
536 | )] | |
537 | /// Try to stop a specific task. | |
538 | async fn task_stop(param: Value) -> Result<Value, Error> { | |
539 | ||
540 | let upid_str = tools::required_string_param(¶m, "upid")?; | |
541 | ||
542 | let mut client = connect()?; | |
543 | ||
544 | let path = format!("api2/json/nodes/localhost/tasks/{}", upid_str); | |
545 | let _ = client.delete(&path, None).await?; | |
546 | ||
547 | Ok(Value::Null) | |
548 | } | |
549 | ||
550 | fn task_mgmt_cli() -> CommandLineInterface { | |
551 | ||
552 | let task_log_cmd_def = CliCommand::new(&API_METHOD_TASK_LOG) | |
553 | .arg_param(&["upid"]); | |
554 | ||
555 | let task_stop_cmd_def = CliCommand::new(&API_METHOD_TASK_STOP) | |
556 | .arg_param(&["upid"]); | |
557 | ||
558 | let cmd_def = CliCommandMap::new() | |
559 | .insert("list", CliCommand::new(&API_METHOD_TASK_LIST)) | |
560 | .insert("log", task_log_cmd_def) | |
561 | .insert("stop", task_stop_cmd_def); | |
562 | ||
563 | cmd_def.into() | |
564 | } | |
565 | ||
e739a8d8 DM |
566 | fn x509name_to_string(name: &openssl::x509::X509NameRef) -> Result<String, Error> { |
567 | let mut parts = Vec::new(); | |
568 | for entry in name.entries() { | |
569 | parts.push(format!("{} = {}", entry.object().nid().short_name()?, entry.data().as_utf8()?)); | |
570 | } | |
571 | Ok(parts.join(", ")) | |
572 | } | |
573 | ||
574 | #[api] | |
575 | /// Diplay node certificate information. | |
576 | fn cert_info() -> Result<(), Error> { | |
577 | ||
578 | let cert_path = PathBuf::from(configdir!("/proxy.pem")); | |
579 | ||
580 | let cert_pem = proxmox::tools::fs::file_get_contents(&cert_path)?; | |
581 | ||
582 | let cert = openssl::x509::X509::from_pem(&cert_pem)?; | |
583 | ||
584 | println!("Subject: {}", x509name_to_string(cert.subject_name())?); | |
585 | ||
586 | if let Some(san) = cert.subject_alt_names() { | |
587 | for name in san.iter() { | |
588 | if let Some(v) = name.dnsname() { | |
589 | println!(" DNS:{}", v); | |
590 | } else if let Some(v) = name.ipaddress() { | |
591 | println!(" IP:{:?}", v); | |
592 | } else if let Some(v) = name.email() { | |
593 | println!(" EMAIL:{}", v); | |
594 | } else if let Some(v) = name.uri() { | |
595 | println!(" URI:{}", v); | |
596 | } | |
597 | } | |
598 | } | |
599 | ||
600 | println!("Issuer: {}", x509name_to_string(cert.issuer_name())?); | |
601 | println!("Validity:"); | |
602 | println!(" Not Before: {}", cert.not_before()); | |
603 | println!(" Not After : {}", cert.not_after()); | |
604 | ||
605 | let fp = cert.digest(openssl::hash::MessageDigest::sha256())?; | |
606 | let fp_string = proxmox::tools::digest_to_hex(&fp); | |
607 | let fp_string = fp_string.as_bytes().chunks(2).map(|v| std::str::from_utf8(v).unwrap()) | |
608 | .collect::<Vec<&str>>().join(":"); | |
609 | ||
610 | println!("Fingerprint (sha256): {}", fp_string); | |
611 | ||
612 | let pubkey = cert.public_key()?; | |
613 | println!("Public key type: {}", openssl::nid::Nid::from_raw(pubkey.id().as_raw()).long_name()?); | |
614 | println!("Public key bits: {}", pubkey.bits()); | |
615 | ||
616 | Ok(()) | |
617 | } | |
618 | ||
550e0d88 DM |
619 | #[api( |
620 | input: { | |
621 | properties: { | |
622 | force: { | |
623 | description: "Force generation of new SSL certifate.", | |
624 | type: Boolean, | |
625 | optional:true, | |
626 | }, | |
627 | } | |
628 | }, | |
629 | )] | |
630 | /// Update node certificates and generate all needed files/directories. | |
631 | fn update_certs(force: Option<bool>) -> Result<(), Error> { | |
632 | ||
550e0d88 DM |
633 | config::create_configdir()?; |
634 | ||
635 | if let Err(err) = generate_auth_key() { | |
636 | bail!("unable to generate auth key - {}", err); | |
637 | } | |
638 | ||
639 | if let Err(err) = generate_csrf_key() { | |
640 | bail!("unable to generate csrf key - {}", err); | |
641 | } | |
642 | ||
f8fd5095 | 643 | config::update_self_signed_cert(force.unwrap_or(false))?; |
550e0d88 DM |
644 | |
645 | Ok(()) | |
646 | } | |
647 | ||
648 | fn cert_mgmt_cli() -> CommandLineInterface { | |
649 | ||
550e0d88 | 650 | let cmd_def = CliCommandMap::new() |
e739a8d8 DM |
651 | .insert("info", CliCommand::new(&API_METHOD_CERT_INFO)) |
652 | .insert("update", CliCommand::new(&API_METHOD_UPDATE_CERTS)); | |
550e0d88 DM |
653 | |
654 | cmd_def.into() | |
655 | } | |
656 | ||
4b4eba0b | 657 | // fixme: avoid API redefinition |
0eb0e024 DM |
658 | #[api( |
659 | input: { | |
660 | properties: { | |
eb506c83 | 661 | "local-store": { |
0eb0e024 DM |
662 | schema: DATASTORE_SCHEMA, |
663 | }, | |
664 | remote: { | |
167971ed | 665 | schema: REMOTE_ID_SCHEMA, |
0eb0e024 DM |
666 | }, |
667 | "remote-store": { | |
668 | schema: DATASTORE_SCHEMA, | |
669 | }, | |
4b4eba0b DM |
670 | delete: { |
671 | description: "Delete vanished backups. This remove the local copy if the remote backup was deleted.", | |
672 | type: Boolean, | |
673 | optional: true, | |
674 | default: true, | |
675 | }, | |
0eb0e024 DM |
676 | "output-format": { |
677 | schema: OUTPUT_FORMAT, | |
678 | optional: true, | |
679 | }, | |
680 | } | |
681 | } | |
682 | )] | |
eb506c83 DM |
683 | /// Sync datastore from another repository |
684 | async fn pull_datastore( | |
0eb0e024 DM |
685 | remote: String, |
686 | remote_store: String, | |
eb506c83 | 687 | local_store: String, |
4b4eba0b | 688 | delete: Option<bool>, |
ac3faaf5 | 689 | param: Value, |
0eb0e024 DM |
690 | ) -> Result<Value, Error> { |
691 | ||
ac3faaf5 | 692 | let output_format = get_output_format(¶m); |
0eb0e024 DM |
693 | |
694 | let mut client = connect()?; | |
695 | ||
4b4eba0b | 696 | let mut args = json!({ |
eb506c83 | 697 | "store": local_store, |
94609e23 | 698 | "remote": remote, |
0eb0e024 | 699 | "remote-store": remote_store, |
0eb0e024 DM |
700 | }); |
701 | ||
4b4eba0b DM |
702 | if let Some(delete) = delete { |
703 | args["delete"] = delete.into(); | |
704 | } | |
705 | ||
eb506c83 | 706 | let result = client.post("api2/json/pull", Some(args)).await?; |
0eb0e024 DM |
707 | |
708 | view_task_result(client, result, &output_format).await?; | |
709 | ||
710 | Ok(Value::Null) | |
711 | } | |
712 | ||
211fabd7 DM |
713 | fn main() { |
714 | ||
6460764d | 715 | let cmd_def = CliCommandMap::new() |
ed3e60ae | 716 | .insert("acl", acl_commands()) |
48ef3c33 | 717 | .insert("datastore", datastore_commands()) |
ca0e5347 | 718 | .insert("network", network_commands()) |
579728c6 | 719 | .insert("user", user_commands()) |
f357390c | 720 | .insert("remote", remote_commands()) |
47d47121 | 721 | .insert("garbage-collection", garbage_collection_commands()) |
550e0d88 | 722 | .insert("cert", cert_mgmt_cli()) |
0eb0e024 DM |
723 | .insert("task", task_mgmt_cli()) |
724 | .insert( | |
eb506c83 DM |
725 | "pull", |
726 | CliCommand::new(&API_METHOD_PULL_DATASTORE) | |
727 | .arg_param(&["remote", "remote-store", "local-store"]) | |
728 | .completion_cb("local-store", config::datastore::complete_datastore_name) | |
f357390c | 729 | .completion_cb("remote", config::remote::complete_remote_name) |
331b869d | 730 | .completion_cb("remote-store", complete_remote_datastore_name) |
0eb0e024 | 731 | ); |
34d3ba52 | 732 | |
d08bc483 | 733 | proxmox_backup::tools::runtime::main(run_async_cli_command(cmd_def)); |
ea0b8b6e | 734 | } |
331b869d DM |
735 | |
736 | // shell completion helper | |
737 | pub fn complete_remote_datastore_name(_arg: &str, param: &HashMap<String, String>) -> Vec<String> { | |
738 | ||
739 | let mut list = Vec::new(); | |
740 | ||
9ea4bce4 | 741 | let _ = proxmox::try_block!({ |
331b869d | 742 | let remote = param.get("remote").ok_or_else(|| format_err!("no remote"))?; |
f357390c | 743 | let (remote_config, _digest) = remote::config()?; |
331b869d DM |
744 | |
745 | let remote: Remote = remote_config.lookup("remote", &remote)?; | |
746 | ||
d59dbeca DM |
747 | let options = HttpClientOptions::new() |
748 | .password(Some(remote.password.clone())) | |
749 | .fingerprint(remote.fingerprint.clone()); | |
750 | ||
331b869d DM |
751 | let client = HttpClient::new( |
752 | &remote.host, | |
753 | &remote.userid, | |
d59dbeca | 754 | options, |
331b869d DM |
755 | )?; |
756 | ||
03ac286c | 757 | let result = crate::tools::runtime::block_on(client.get("api2/json/admin/datastore", None))?; |
331b869d DM |
758 | |
759 | if let Some(data) = result["data"].as_array() { | |
760 | for item in data { | |
761 | if let Some(store) = item["store"].as_str() { | |
762 | list.push(store.to_owned()); | |
763 | } | |
764 | } | |
765 | } | |
766 | ||
767 | Ok(()) | |
768 | }).map_err(|_err: Error| { /* ignore */ }); | |
769 | ||
770 | list | |
771 | } |