Debian Codegraph @

src/defaults/mod.rs

raw symbols SCIP
1 #![forbid(unsafe_code)]
2 pub type SettingsModifier = Box<dyn FnOnce(&mut Settings)>;
3
4 pub enum ListMode {
5 Set,
6 Add,
7 Del,
8 }
9
10 #[repr(u32)]
11 pub enum SettingKind {
12 Flag(SettingsModifier) = crate::common::HARDENED_ENUM_VALUE_0,
13 Integer(fn(&str) -> Option<SettingsModifier>) = crate::common::HARDENED_ENUM_VALUE_1,
14 Text(fn(&str) -> Option<SettingsModifier>) = crate::common::HARDENED_ENUM_VALUE_2,
15 List(fn(ListMode, Vec<String>) -> SettingsModifier) = crate::common::HARDENED_ENUM_VALUE_3,
16 }
17
18 mod settings_dsl;
19 use settings_dsl::{
20 defaults, emit, has_standard_negator, ifdef, initializer_of, modifier_of, referent_of,
21 result_of, storage_of,
22 };
23
24 pub const SYSTEM_EDITOR: &str = if cfg!(target_os = "linux") {
25 "/usr/bin/editor:/usr/bin/nano:/usr/bin/vi"
26 } else {
27 "/usr/bin/vi"
28 };
29
30 defaults! {
31 always_query_group_plugin = false #ignored
32 always_set_home = false #ignored
33 env_reset = true #ignored
34 fqdn = false #ignored
35 ignore_dot = true #ignored
36 lecture = never (!= never) [always, once, never] #ignored
37 mailerpath = None (!= None) #ignored
38 mail_badpass = true #ignored
39 match_group_by_gid = false #ignored
40 use_pty = true
41 visiblepw = false #ignored
42 pwfeedback = true
43 rootpw = false
44 targetpw = false
45 noexec = false
46 noninteractive_auth = false
47
48 insults = false #ignored
49
50 setenv = false
51 runcwd = None (!= None)
52 apparmor_profile = None (!= None)
53 umask = 0o022 (!= 0o777) {octal_mode}
54 umask_override = false
55
56 passwd_tries = 3 [0..=1000]
57
58 secure_path = None (!= None)
59
60 verifypw = all (!= never) [all, always, any, never] #ignored
61
62 passwd_timeout = (5*60) (!= 0) {fractional_minutes}
63 timestamp_timeout = (15*60) (!= 0) {fractional_minutes}
64
65 editor = SYSTEM_EDITOR
66 env_editor = true
67
68 env_keep = ["COLORS", "DISPLAY", "HOSTNAME", "KRB5CCNAME", "LS_COLORS", "PATH",
69 "PS1", "PS2", "XAUTHORITY", "XAUTHORIZATION", "XDG_CURRENT_DESKTOP"]
70
71 env_check = ["COLORTERM", "LANG", "LANGUAGE", "LC_*", "LINGUAS", "TERM", "TZ"]
72
73 env_delete = ["IFS", "CDPATH", "LOCALDOMAIN", "RES_OPTIONS", "HOSTALIASES",
74 "NLSPATH", "PATH_LOCALE", "LD_*", "_RLD*", "TERMINFO", "TERMINFO_DIRS",
75 "TERMPATH", "TERMCAP", "ENV", "BASH_ENV", "PS4", "GLOBIGNORE",
76 "BASHOPTS", "SHELLOPTS", "JAVA_TOOL_OPTIONS", "PERLIO_DEBUG",
77 "PERLLIB", "PERL5LIB", "PERL5OPT", "PERL5DB", "FPATH", "NULLCMD",
78 "READNULLCMD", "ZDOTDIR", "TMPPREFIX", "PYTHONHOME", "PYTHONPATH",
79 "PYTHONINSPECT", "PYTHONUSERBASE", "RUBYLIB", "RUBYOPT", "*=()*"] #ignored
80 }
81
82 fn octal_mode(input: &str) -> Option<u64> {
83 <libc::mode_t>::from_str_radix(input.strip_prefix('0')?, 8)
84 .ok()
85 .map(Into::into)
86 }
87
88 /// A custom parser to parse seconds as fractional "minutes", the format used by
89 /// passwd_timeout and timestamp_timeout.
90 fn fractional_minutes(input: &str) -> Option<u64> {
91 if let Some((integral, fractional)) = input.split_once('.') {
92 // - 'input' is maximally 18 characters, making fractional.len() at most 17;
93 // 1e17 < 2**63, so the definition of 'shift' will not overflow.
94 // - for the same reason, if both parses in the definition of 'seconds' succeed,
95 // we will have constructed an integer < 1e17.
96 //- 1e17 * 60 = 6e18 < 9e18 < 2**63, so the final line also will not overflow
97 let shift = 10u64.pow(fractional.len().try_into().ok()?);
98 let seconds = integral.parse::<u64>().ok()? * shift + fractional.parse::<u64>().ok()?;
99
100 Some(seconds * 60 / shift)
101 } else {
102 input.parse::<u64>().ok()?.checked_mul(60)
103 }
104 }
105
106 #[cfg(test)]
107 mod test {
108 use super::*;
109
110 #[allow(clippy::bool_assert_comparison)]
111 #[test]
112 fn check() {
113 let mut def = Settings::default();
114 assert_eq! { def.always_query_group_plugin, false };
115 assert_eq! { def.always_set_home, false };
116 assert_eq! { def.env_reset, true };
117 assert_eq! { def.mail_badpass, true };
118 assert_eq! { def.match_group_by_gid, false };
119 assert_eq! { def.use_pty, true };
120 assert_eq! { def.visiblepw, false };
121 assert_eq! { def.env_editor, true };
122 assert_eq! { def.passwd_tries, 3 };
123 assert_eq! { def.secure_path, None };
124 assert_eq! { def.env_check, ["COLORTERM", "LANG", "LANGUAGE", "LC_*", "LINGUAS", "TERM", "TZ"].iter().map(|s| s.to_string()).collect() };
125 assert_eq! { def.verifypw, enums::verifypw::all };
126
127 negate("env_check").unwrap()(&mut def);
128 negate("env_reset").unwrap()(&mut def);
129 negate("secure_path").unwrap()(&mut def);
130 negate("verifypw").unwrap()(&mut def);
131 assert_eq! { def.always_query_group_plugin, false };
132 assert_eq! { def.always_set_home, false };
133 assert_eq! { def.env_reset, false };
134 assert_eq! { def.mail_badpass, true };
135 assert_eq! { def.match_group_by_gid, false };
136 assert_eq! { def.use_pty, true };
137 assert_eq! { def.visiblepw, false };
138 assert_eq! { def.env_editor, true };
139 assert_eq! { def.passwd_tries, 3 };
140 assert_eq! { def.secure_path, None };
141 assert! { def.env_check.is_empty() };
142 assert_eq! { def.verifypw, enums::verifypw::never };
143
144 let SettingKind::Text(f) = set("lecture").unwrap() else {
145 panic!()
146 };
147 f("always").unwrap()(&mut def);
148 assert_eq! { def.lecture, enums::lecture::always };
149 f("once").unwrap()(&mut def);
150 assert_eq! { def.lecture, enums::lecture::once };
151 f("never").unwrap()(&mut def);
152 assert_eq! { def.lecture, enums::lecture::never};
153 negate("lecture").unwrap()(&mut def);
154 assert_eq! { def.lecture, enums::lecture::never };
155
156 let SettingKind::Flag(f) = set("env_reset").unwrap() else {
157 panic!()
158 };
159 f(&mut def);
160 let SettingKind::Text(f) = set("secure_path").unwrap() else {
161 panic!()
162 };
163 f("/bin").unwrap()(&mut def);
164 let SettingKind::Integer(f) = set("passwd_tries").unwrap() else {
165 panic!()
166 };
167 f("5").unwrap()(&mut def);
168 let SettingKind::Text(f) = set("verifypw").unwrap() else {
169 panic!()
170 };
171 f("any").unwrap()(&mut def);
172 let SettingKind::Integer(f) = set("timestamp_timeout").unwrap() else {
173 panic!()
174 };
175 f("25.25").unwrap()(&mut def);
176 assert_eq! { def.always_query_group_plugin, false };
177 assert_eq! { def.always_set_home, false };
178 assert_eq! { def.env_reset, true };
179 assert_eq! { def.mail_badpass, true };
180 assert_eq! { def.match_group_by_gid, false };
181 assert_eq! { def.use_pty, true };
182 assert_eq! { def.visiblepw, false };
183 assert_eq! { def.env_editor, true };
184 assert_eq! { def.passwd_tries, 5 };
185 assert_eq! { def.timestamp_timeout, 25*60 + 60/4 };
186 assert_eq! { def.secure_path, Some("/bin".into()) };
187 assert! { def.env_check.is_empty() };
188 assert_eq! { def.verifypw, enums::verifypw::any };
189
190 assert!(set("notanoption").is_none());
191 assert!(f("notanoption").is_none());
192 }
193 }