Skip to content

Commit de00427

Browse files
authored
Replace system::time::Duration with std::time::Duration (#1207)
This pull request replaces `system::time::Duration` with `std::time::Duration`. I couldn't find any code that still uses negative duration, so I replaced it with it's `std` equivalent. The only place, where I'm not sure if its possible to use an negative duration, is the `prior_validity` property from the `Authentication` struct. Should close #1170
1 parent f68e342 commit de00427

File tree

11 files changed

+69
-132
lines changed

11 files changed

+69
-132
lines changed

src/defaults/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,27 +78,27 @@ defaults! {
7878
"PYTHONINSPECT", "PYTHONUSERBASE", "RUBYLIB", "RUBYOPT", "*=()*"] #ignored
7979
}
8080

81-
fn octal_mode(input: &str) -> Option<i64> {
81+
fn octal_mode(input: &str) -> Option<u64> {
8282
<libc::mode_t>::from_str_radix(input.strip_prefix('0')?, 8)
8383
.ok()
8484
.map(Into::into)
8585
}
8686

8787
/// A custom parser to parse seconds as fractional "minutes", the format used by
8888
/// passwd_timeout and timestamp_timeout.
89-
fn fractional_minutes(input: &str) -> Option<i64> {
89+
fn fractional_minutes(input: &str) -> Option<u64> {
9090
if let Some((integral, fractional)) = input.split_once('.') {
9191
// - 'input' is maximally 18 characters, making fractional.len() at most 17;
9292
// 1e17 < 2**63, so the definition of 'shift' will not overflow.
9393
// - for the same reason, if both parses in the definition of 'seconds' succeed,
9494
// we will have constructed an integer < 1e17.
9595
//- 1e17 * 60 = 6e18 < 9e18 < 2**63, so the final line also will not overflow
96-
let shift = 10i64.pow(fractional.len().try_into().ok()?);
97-
let seconds = integral.parse::<i64>().ok()? * shift + fractional.parse::<i64>().ok()?;
96+
let shift = 10u64.pow(fractional.len().try_into().ok()?);
97+
let seconds = integral.parse::<u64>().ok()? * shift + fractional.parse::<u64>().ok()?;
9898

9999
Some(seconds * 60 / shift)
100100
} else {
101-
input.parse::<i64>().ok()?.checked_mul(60)
101+
input.parse::<u64>().ok()?.checked_mul(60)
102102
}
103103
}
104104

src/defaults/settings_dsl.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ macro_rules! storage_of {
22
($id:ident, true) => { bool };
33
($id:ident, false) => { bool };
44
($id:ident, [ $($value: expr),* ]) => { std::collections::HashSet<String> };
5-
($id:ident, $(=int $check: expr;)+ $_: expr) => { i64 };
5+
($id:ident, $(=int $check: expr;)+ $_: expr) => { u64 };
66
($id:ident, $(=enum $k: ident;)+ $_: ident) => { $crate::defaults::enums::$id };
77
($id:ident, None) => { Option<Box<str>> };
88
($id:ident, $_: expr) => { Box<str> };
@@ -12,7 +12,7 @@ macro_rules! referent_of {
1212
($id:ident, true) => { bool };
1313
($id:ident, false) => { bool };
1414
($id:ident, [ $($value: expr),* ]) => { &std::collections::HashSet<String> };
15-
($id:ident, $(=int $check: expr;)+ $_: expr) => { i64 };
15+
($id:ident, $(=int $check: expr;)+ $_: expr) => { u64 };
1616
($id:ident, $(=enum $k: ident;)+ $_: ident) => { $crate::defaults::enums::$id };
1717
($id:ident, None) => { Option<&str> };
1818
($id:ident, $_: expr) => { &str };
@@ -73,7 +73,7 @@ macro_rules! modifier_of {
7373
($id:ident, =int $first:literal ..= $last: literal $(@ $radix: literal)?; $value: expr) => {
7474
#[allow(clippy::from_str_radix_10)]
7575
$crate::defaults::SettingKind::Integer(|text| {
76-
i64::from_str_radix(text, 10$(*0 + $radix)?)
76+
u64::from_str_radix(text, 10$(*0 + $radix)?)
7777
.ok()
7878
.filter(|val| ($first ..= $last).contains(val))
7979
.map(|i| {

src/pam/converse.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
use std::ffi::{c_int, c_void};
22
use std::io;
3+
use std::time::Duration;
34

45
use crate::cutils::string_from_ptr;
56
use crate::pam::rpassword::Hidden;
6-
use crate::system::{
7-
signal::{self, SignalSet},
8-
time::Duration,
9-
};
7+
use crate::system::signal::{self, SignalSet};
108

119
use super::sys::*;
1210

src/pam/mod.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@ use std::{
44
os::raw::c_char,
55
os::unix::prelude::OsStrExt,
66
ptr::NonNull,
7-
};
8-
9-
use crate::system::{
10-
signal::{self, SignalSet},
117
time::Duration,
128
};
139

10+
use crate::system::signal::{self, SignalSet};
11+
1412
use converse::ConverserData;
1513
use error::pam_err;
1614
pub use error::{PamError, PamErrorType, PamResult};

src/pam/rpassword.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@
1616
use std::ffi::c_void;
1717
use std::io::{self, Error, ErrorKind, Read};
1818
use std::os::fd::{AsFd, AsRawFd, BorrowedFd};
19-
use std::time::Instant;
19+
use std::time::{Duration, Instant};
2020
use std::{fs, mem};
2121

2222
use libc::{tcsetattr, termios, ECHO, ECHONL, ICANON, TCSANOW, VEOF, VERASE, VKILL};
2323

2424
use crate::cutils::{cerr, safe_isatty};
25-
use crate::system::time::Duration;
2625

2726
use super::securemem::PamBuffer;
2827

@@ -186,7 +185,7 @@ struct TimeoutRead<'a> {
186185
impl<'a> TimeoutRead<'a> {
187186
fn new(fd: BorrowedFd<'a>, timeout: Option<Duration>) -> TimeoutRead<'a> {
188187
TimeoutRead {
189-
timeout_at: timeout.map(|timeout| Instant::now() + timeout.into()),
188+
timeout_at: timeout.map(|timeout| Instant::now() + timeout),
190189
fd,
191190
}
192191
}

src/sudo/mod.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ use crate::log::dev_info;
66
use crate::system::interface::UserId;
77
use crate::system::timestamp::RecordScope;
88
use crate::system::User;
9-
use crate::system::{time::Duration, timestamp::SessionRecordFile, Process};
9+
use crate::system::{timestamp::SessionRecordFile, Process};
1010
#[cfg(test)]
1111
pub(crate) use cli::SudoAction;
1212
#[cfg(not(test))]
1313
use cli::SudoAction;
14-
use std::path::PathBuf;
14+
use std::{path::PathBuf, time::Duration};
1515

1616
mod cli;
1717
pub(crate) use cli::{SudoEditOptions, SudoListOptions, SudoRunOptions, SudoValidateOptions};
@@ -90,16 +90,15 @@ fn sudo_process() -> Result<(), Error> {
9090
}
9191
SudoAction::RemoveTimestamp(_) => {
9292
let user = CurrentUser::resolve()?;
93-
let mut record_file =
94-
SessionRecordFile::open_for_user(&user, Duration::seconds(0))?;
93+
let mut record_file = SessionRecordFile::open_for_user(&user, Duration::default())?;
9594
record_file.reset()?;
9695
Ok(())
9796
}
9897
SudoAction::ResetTimestamp(_) => {
9998
if let Some(scope) = RecordScope::for_process(&Process::new()) {
10099
let user = CurrentUser::resolve()?;
101100
let mut record_file =
102-
SessionRecordFile::open_for_user(&user, Duration::seconds(0))?;
101+
SessionRecordFile::open_for_user(&user, Duration::default())?;
103102
record_file.disable(scope)?;
104103
}
105104
Ok(())

src/sudo/pam.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use std::ffi::OsString;
2+
use std::time::Duration;
23

34
use crate::common::context::LaunchType;
45
use crate::common::error::Error;
56
use crate::log::{dev_info, user_warn};
67
use crate::pam::{PamContext, PamError, PamErrorType, PamResult};
7-
use crate::system::{term::current_tty_name, time::Duration};
8+
use crate::system::term::current_tty_name;
89

910
pub(super) struct InitPamArgs<'a> {
1011
pub(super) launch: LaunchType,

src/sudo/pipeline.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::ffi::OsStr;
22
use std::process::exit;
3+
use std::time::Duration;
34

45
use super::cli::{SudoRunOptions, SudoValidateOptions};
56
use super::diagnostic;
@@ -10,7 +11,6 @@ use crate::log::{auth_info, auth_warn};
1011
use crate::pam::PamContext;
1112
use crate::sudo::env::environment;
1213
use crate::sudo::pam::{attempt_authenticate, init_pam, pre_exec, InitPamArgs};
13-
use crate::sudo::Duration;
1414
use crate::sudoers::{
1515
AuthenticatingUser, Authentication, Authorization, DirChange, Judgement, Restrictions, Sudoers,
1616
};

src/sudoers/policy.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ use crate::common::{
66
};
77
use crate::exec::Umask;
88
use crate::sudoers::ast::{ExecControl, Tag};
9-
use crate::system::{time::Duration, Hostname, User};
9+
use crate::system::{Hostname, User};
1010
/// Data types and traits that represent what the "terms and conditions" are after a successful
1111
/// permission check.
1212
///
1313
/// The trait definitions can be part of some global crate in the future, if we support more
1414
/// than just the sudoers file.
1515
use std::collections::HashSet;
16+
use std::time::Duration;
1617

1718
#[must_use]
1819
#[cfg_attr(test, derive(Debug, PartialEq))]
@@ -38,11 +39,11 @@ impl super::Settings {
3839
Authentication {
3940
must_authenticate: tag.needs_passwd(),
4041
allowed_attempts: self.passwd_tries().try_into().unwrap(),
41-
prior_validity: Duration::seconds(self.timestamp_timeout()),
42+
prior_validity: Duration::from_secs(self.timestamp_timeout()),
4243
pwfeedback: self.pwfeedback(),
4344
password_timeout: match self.passwd_timeout() {
4445
0 => None,
45-
timeout => Some(Duration::seconds(timeout)),
46+
timeout => Some(Duration::from_secs(timeout)),
4647
},
4748
credential: if self.rootpw() {
4849
AuthenticatingUser::Root
@@ -191,10 +192,10 @@ mod test {
191192
Authentication {
192193
must_authenticate: true,
193194
allowed_attempts: 3,
194-
prior_validity: Duration::minutes(15),
195+
prior_validity: Duration::from_secs(15 * 60),
195196
credential: AuthenticatingUser::InvokingUser,
196197
pwfeedback: false,
197-
password_timeout: Some(Duration::seconds(300)),
198+
password_timeout: Some(Duration::from_secs(300)),
198199
},
199200
);
200201

@@ -208,10 +209,10 @@ mod test {
208209
Authentication {
209210
must_authenticate: false,
210211
allowed_attempts: 3,
211-
prior_validity: Duration::minutes(15),
212+
prior_validity: Duration::from_secs(15 * 60),
212213
credential: AuthenticatingUser::InvokingUser,
213214
pwfeedback: false,
214-
password_timeout: Some(Duration::seconds(300)),
215+
password_timeout: Some(Duration::from_secs(300)),
215216
},
216217
);
217218
assert_eq!(restrictions, restrictions2);

0 commit comments

Comments
 (0)