2019-08-22 09:14:01 +00:00
|
|
|
use std::mem::{self, MaybeUninit};
|
|
|
|
|
|
|
|
use chrono::prelude::*;
|
2019-01-23 12:05:32 +00:00
|
|
|
use failure::*;
|
2019-08-22 09:14:01 +00:00
|
|
|
use serde_json::{json, Value};
|
|
|
|
|
2020-04-16 08:01:59 +00:00
|
|
|
use proxmox::api::{api, Router, Permission};
|
2019-12-18 10:05:30 +00:00
|
|
|
use proxmox::tools::fs::{file_read_firstline, replace_file, CreateOptions};
|
2019-01-23 12:05:32 +00:00
|
|
|
|
2019-05-09 05:44:09 +00:00
|
|
|
use crate::api2::types::*;
|
2019-01-23 14:14:14 +00:00
|
|
|
|
|
|
|
fn read_etc_localtime() -> Result<String, Error> {
|
2019-01-30 13:55:43 +00:00
|
|
|
// use /etc/timezone
|
2019-08-03 11:05:38 +00:00
|
|
|
if let Ok(line) = file_read_firstline("/etc/timezone") {
|
2019-01-30 13:55:43 +00:00
|
|
|
return Ok(line.trim().to_owned());
|
|
|
|
}
|
2019-01-23 14:14:14 +00:00
|
|
|
|
2019-01-30 13:55:43 +00:00
|
|
|
// otherwise guess from the /etc/localtime symlink
|
2019-08-22 09:14:01 +00:00
|
|
|
let mut buf = MaybeUninit::<[u8; 64]>::uninit();
|
2019-01-30 13:55:43 +00:00
|
|
|
let len = unsafe {
|
2019-08-22 09:14:01 +00:00
|
|
|
libc::readlink(
|
|
|
|
"/etc/localtime".as_ptr() as *const _,
|
|
|
|
buf.as_mut_ptr() as *mut _,
|
|
|
|
mem::size_of_val(&buf),
|
|
|
|
)
|
2019-01-30 13:55:43 +00:00
|
|
|
};
|
|
|
|
if len <= 0 {
|
|
|
|
bail!("failed to guess timezone");
|
|
|
|
}
|
|
|
|
let len = len as usize;
|
2019-08-22 09:14:01 +00:00
|
|
|
let buf = unsafe {
|
|
|
|
(*buf.as_mut_ptr())[len] = 0;
|
|
|
|
buf.assume_init()
|
|
|
|
};
|
2019-01-30 13:55:43 +00:00
|
|
|
let link = std::str::from_utf8(&buf[..len])?;
|
|
|
|
match link.rfind("/zoneinfo/") {
|
|
|
|
Some(pos) => Ok(link[(pos + 10)..].to_string()),
|
|
|
|
None => Ok(link.to_string()),
|
|
|
|
}
|
2019-01-23 14:14:14 +00:00
|
|
|
}
|
2019-01-23 12:05:32 +00:00
|
|
|
|
2020-04-16 08:01:59 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
node: {
|
|
|
|
schema: NODE_SCHEMA,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
returns: {
|
|
|
|
description: "Returns server time and timezone.",
|
|
|
|
properties: {
|
|
|
|
timezone: {
|
|
|
|
schema: TIME_ZONE_SCHEMA,
|
|
|
|
},
|
|
|
|
time: {
|
|
|
|
type: i64,
|
|
|
|
description: "Seconds since 1970-01-01 00:00:00 UTC.",
|
|
|
|
minimum: 1_297_163_644,
|
|
|
|
},
|
|
|
|
localtime: {
|
|
|
|
type: i64,
|
|
|
|
description: "Seconds since 1970-01-01 00:00:00 UTC. (local time)",
|
|
|
|
minimum: 1_297_163_644,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
access: {
|
|
|
|
permission: &Permission::Anybody,
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// Read server time and time zone settings.
|
|
|
|
fn get_time(_param: Value) -> Result<Value, Error> {
|
2019-01-23 14:14:14 +00:00
|
|
|
let datetime = Local::now();
|
|
|
|
let offset = datetime.offset();
|
|
|
|
let time = datetime.timestamp();
|
|
|
|
let localtime = time + (offset.fix().local_minus_utc() as i64);
|
|
|
|
|
2019-01-23 12:05:32 +00:00
|
|
|
Ok(json!({
|
2019-01-23 14:14:14 +00:00
|
|
|
"timezone": read_etc_localtime()?,
|
|
|
|
"time": time,
|
|
|
|
"localtime": localtime,
|
2019-01-23 12:05:32 +00:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2020-04-16 08:01:59 +00:00
|
|
|
#[api(
|
|
|
|
protected: true,
|
|
|
|
reload_timezone: true,
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
node: {
|
|
|
|
schema: NODE_SCHEMA,
|
|
|
|
},
|
|
|
|
timezone: {
|
|
|
|
schema: TIME_ZONE_SCHEMA,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// Set time zone
|
2019-01-26 13:50:37 +00:00
|
|
|
fn set_timezone(
|
2020-04-16 08:01:59 +00:00
|
|
|
timezone: String,
|
|
|
|
_param: Value,
|
2019-01-26 13:50:37 +00:00
|
|
|
) -> Result<Value, Error> {
|
2019-01-24 11:05:06 +00:00
|
|
|
let path = std::path::PathBuf::from(format!("/usr/share/zoneinfo/{}", timezone));
|
|
|
|
|
|
|
|
if !path.exists() {
|
|
|
|
bail!("No such timezone.");
|
|
|
|
}
|
|
|
|
|
2019-12-18 10:05:30 +00:00
|
|
|
replace_file("/etc/timezone", timezone.as_bytes(), CreateOptions::new())?;
|
2019-01-24 11:05:06 +00:00
|
|
|
|
|
|
|
let _ = std::fs::remove_file("/etc/localtime");
|
|
|
|
|
|
|
|
use std::os::unix::fs::symlink;
|
|
|
|
symlink(path, "/etc/localtime")?;
|
|
|
|
|
|
|
|
Ok(Value::Null)
|
|
|
|
}
|
|
|
|
|
2019-11-21 08:36:41 +00:00
|
|
|
pub const ROUTER: Router = Router::new()
|
2020-04-16 08:01:59 +00:00
|
|
|
.get(&API_METHOD_GET_TIME)
|
|
|
|
.put(&API_METHOD_SET_TIMEZONE);
|