rash_core 2.17.8

Declarative shell scripting using Rust native bindings
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/// ANCHOR: module
/// # user
///
/// Manage user accounts and user attributes.
///
/// ## Attributes
///
/// ```yaml
/// check_mode:
///   support: full
/// ```
/// ANCHOR_END: module
/// ANCHOR: examples
/// ## Example
///
/// ```yaml
/// - user:
///     name: johnd
///     comment: John Doe
///     uid: 1040
///     group: admin
///     shell: /bin/bash
///
/// - user:
///     name: myservice
///     system: yes
///     create_home: no
///     shell: /sbin/nologin
///
/// - user:
///     name: james
///     groups:
///       - docker
///       - wheel
///     append: yes
///
/// - user:
///     name: olduser
///     state: absent
///     remove: yes
/// ```
/// ANCHOR_END: examples
use crate::context::GlobalParams;
use crate::error::{Error, ErrorKind, Result};
use crate::modules::{Module, ModuleResult};

#[cfg(feature = "docs")]
use rash_derive::DocJsonSchema;

use std::fs;

use minijinja::Value;
#[cfg(feature = "docs")]
use schemars::{JsonSchema, Schema};
use serde::Deserialize;
use serde_norway::Value as YamlValue;
#[cfg(feature = "docs")]
use strum_macros::{Display, EnumString};

fn default_state() -> Option<State> {
    Some(State::Present)
}

#[derive(Debug, Clone, PartialEq, Deserialize)]
#[cfg_attr(feature = "docs", derive(JsonSchema, DocJsonSchema))]
#[serde(deny_unknown_fields)]
pub struct Params {
    /// Name of the user to create, remove or modify.
    pub name: String,
    /// Whether the account should exist or not.
    /// **[default: `"present"`]**
    #[serde(default = "default_state")]
    pub state: Option<State>,
    /// User ID of the user.
    pub uid: Option<u32>,
    /// Primary group name.
    pub group: Option<String>,
    /// List of supplementary groups.
    #[serde(default)]
    pub groups: Option<Vec<String>>,
    /// If true, add the user to the groups specified in groups.
    /// If false, user will only be in the groups specified.
    /// **[default: `false`]**
    #[serde(default)]
    pub append: Option<bool>,
    /// Home directory path.
    pub home: Option<String>,
    /// Create home directory if it doesn't exist.
    /// **[default: `true`]**
    #[serde(default)]
    pub create_home: Option<bool>,
    /// Login shell path.
    pub shell: Option<String>,
    /// User description (GECOS field).
    pub comment: Option<String>,
    /// Create as system user (uid < 1000).
    /// **[default: `false`]**
    #[serde(default)]
    pub system: Option<bool>,
    /// Encrypted password hash.
    pub password: Option<String>,
    /// Remove home directory when state=absent.
    /// **[default: `false`]**
    #[serde(default)]
    pub remove: Option<bool>,
}

#[derive(Debug, Clone, PartialEq, Deserialize)]
#[cfg_attr(feature = "docs", derive(EnumString, Display, JsonSchema))]
#[serde(rename_all = "lowercase")]
pub enum State {
    Absent,
    Present,
}

#[derive(Debug, Clone, PartialEq)]
pub struct UserInfo {
    pub name: String,
    pub uid: u32,
    pub gid: u32,
    pub comment: String,
    pub home: String,
    pub shell: String,
}

/// Parse a line from /etc/passwd into UserInfo
fn parse_passwd_line(line: &str) -> Option<UserInfo> {
    let parts: Vec<&str> = line.split(':').collect();
    if parts.len() < 7 {
        return None;
    }
    Some(UserInfo {
        name: parts[0].to_string(),
        uid: parts[2].parse().ok()?,
        gid: parts[3].parse().ok()?,
        comment: parts[4].to_string(),
        home: parts[5].to_string(),
        shell: parts[6].trim().to_string(),
    })
}

/// Get user info from /etc/passwd (or test mock file if it exists)
fn get_user_info(username: &str) -> Option<UserInfo> {
    // Check for test mock file first (for e2e testing without root)
    // Use environment variable if set, otherwise check default test path
    let passwd_path = if let Ok(test_file) = std::env::var("RASH_TEST_PASSWD_FILE") {
        test_file
    } else if std::path::Path::new("/tmp/rash_test_passwd").exists() {
        "/tmp/rash_test_passwd".to_string()
    } else {
        "/etc/passwd".to_string()
    };

    if let Ok(passwd) = fs::read_to_string(&passwd_path) {
        for line in passwd.lines() {
            if let Some(info) = parse_passwd_line(line)
                && info.name == username
            {
                return Some(info);
            }
        }
    }
    None
}

/// Get supplementary groups for a user from /etc/group
fn get_user_groups(username: &str) -> Vec<String> {
    let mut groups = Vec::new();

    let group_path = if let Ok(test_file) = std::env::var("RASH_TEST_GROUP_FILE") {
        test_file
    } else if std::path::Path::new("/tmp/rash_test_group").exists() {
        "/tmp/rash_test_group".to_string()
    } else {
        "/etc/group".to_string()
    };

    if let Ok(groupfile) = fs::read_to_string(&group_path) {
        for line in groupfile.lines() {
            let parts: Vec<&str> = line.split(':').collect();
            if parts.len() < 4 {
                continue;
            }
            let groupname = parts[0];
            let members = parts[3];
            if members.split(',').any(|m| m == username) {
                groups.push(groupname.to_string());
            }
        }
    }
    groups
}

/// Build useradd command arguments
fn build_useradd_command(params: &Params) -> Vec<String> {
    let mut cmd = vec!["useradd".to_string()];

    if let Some(uid) = params.uid {
        cmd.push("-u".to_string());
        cmd.push(uid.to_string());
    }
    if let Some(ref group) = params.group {
        cmd.push("-g".to_string());
        cmd.push(group.clone());
    }
    if let Some(ref groups) = params.groups
        && !groups.is_empty()
    {
        cmd.push("-G".to_string());
        cmd.push(groups.join(","));
    }
    if let Some(ref home) = params.home {
        cmd.push("-d".to_string());
        cmd.push(home.clone());
    }
    match params.create_home {
        Some(true) | None => cmd.push("-m".to_string()),
        Some(false) => cmd.push("-M".to_string()),
    }
    if let Some(ref shell) = params.shell {
        cmd.push("-s".to_string());
        cmd.push(shell.clone());
    }
    if let Some(ref comment) = params.comment {
        cmd.push("-c".to_string());
        cmd.push(comment.clone());
    }
    if let Some(true) = params.system {
        cmd.push("-r".to_string());
    }
    if let Some(ref password) = params.password {
        cmd.push("-p".to_string());
        cmd.push(password.clone());
    }

    cmd.push(params.name.clone());
    cmd
}

/// Build usermod command arguments
fn build_usermod_command(params: &Params, current: &UserInfo) -> Vec<String> {
    let mut cmd = vec!["usermod".to_string()];

    if let Some(uid) = params.uid
        && uid != current.uid
    {
        cmd.push("-u".to_string());
        cmd.push(uid.to_string());
    }
    if let Some(ref group) = params.group {
        cmd.push("-g".to_string());
        cmd.push(group.clone());
    }
    if let Some(ref groups) = params.groups
        && !groups.is_empty()
    {
        // Determine which groups need to be added
        let groups_to_add = if params.append.unwrap_or(false) {
            // In append mode, only add groups the user doesn't already have
            let current_groups = get_user_groups(&params.name);
            groups
                .iter()
                .filter(|g| !current_groups.contains(g))
                .cloned()
                .collect::<Vec<_>>()
        } else {
            // In replace mode, always set groups
            groups.clone()
        };

        if !groups_to_add.is_empty() {
            if params.append.unwrap_or(false) {
                cmd.push("-a".to_string());
            }
            cmd.push("-G".to_string());
            cmd.push(groups_to_add.join(","));
        }
    }
    if let Some(ref home) = params.home
        && home != &current.home
    {
        cmd.push("-d".to_string());
        cmd.push(home.clone());
    }
    if let Some(ref shell) = params.shell
        && shell != &current.shell
    {
        cmd.push("-s".to_string());
        cmd.push(shell.clone());
    }
    if let Some(ref comment) = params.comment
        && comment != &current.comment
    {
        cmd.push("-c".to_string());
        cmd.push(comment.clone());
    }
    if let Some(ref password) = params.password {
        cmd.push("-p".to_string());
        cmd.push(password.clone());
    }

    cmd.push(params.name.clone());
    cmd
}

/// Build userdel command arguments
fn build_userdel_command(params: &Params) -> Vec<String> {
    let mut cmd = vec!["userdel".to_string()];

    if let Some(true) = params.remove {
        cmd.push("-r".to_string());
    }

    cmd.push(params.name.clone());
    cmd
}

/// Execute a user management command
fn exec_user_command(cmd: &[String], check_mode: bool) -> Result<(ModuleResult, Option<Value>)> {
    if check_mode {
        return Ok((
            ModuleResult {
                changed: true,
                output: Some(format!("Would run: {}", cmd.join(" "))),
                extra: None,
            },
            None,
        ));
    }

    let mut command = std::process::Command::new(&cmd[0]);
    command.args(&cmd[1..]);

    let output = command
        .output()
        .map_err(|e| Error::new(ErrorKind::SubprocessFail, e))?;

    let stderr = String::from_utf8_lossy(&output.stderr);
    if !output.status.success() {
        return Err(Error::new(ErrorKind::InvalidData, stderr));
    }

    let stdout = String::from_utf8_lossy(&output.stdout);
    Ok((
        ModuleResult {
            changed: true,
            output: Some(stdout.into_owned()),
            extra: None,
        },
        None,
    ))
}

#[derive(Debug)]
pub struct User;

impl Module for User {
    fn get_name(&self) -> &str {
        "user"
    }

    fn exec(
        &self,
        _global_params: &GlobalParams,
        params: YamlValue,
        _vars: &Value,
        check_mode: bool,
    ) -> Result<(ModuleResult, Option<Value>)> {
        let params: Params = serde_norway::from_value(params)?;
        let current = get_user_info(&params.name);

        match params.state.clone().unwrap_or(State::Present) {
            State::Present => match current {
                None => {
                    // User does not exist, create
                    let cmd = build_useradd_command(&params);
                    exec_user_command(&cmd, check_mode)
                }
                Some(ref info) => {
                    // User exists, modify if needed
                    let cmd = build_usermod_command(&params, info);
                    if cmd.len() > 2 {
                        // usermod + name + at least one change
                        exec_user_command(&cmd, check_mode)
                    } else {
                        Ok((
                            ModuleResult {
                                changed: false,
                                output: None,
                                extra: None,
                            },
                            None,
                        ))
                    }
                }
            },
            State::Absent => match current {
                None => Ok((
                    ModuleResult {
                        changed: false,
                        output: Some("User already absent".to_string()),
                        extra: None,
                    },
                    None,
                )),
                Some(_) => {
                    let cmd = build_userdel_command(&params);
                    exec_user_command(&cmd, check_mode)
                }
            },
        }
    }

    #[cfg(feature = "docs")]
    fn get_json_schema(&self) -> Option<Schema> {
        Some(Params::get_json_schema())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_params_minimal() {
        let yaml: YamlValue = serde_norway::from_str(r#"name: johnd"#).unwrap();
        let params: Params = serde_norway::from_value(yaml).unwrap();
        assert_eq!(params.name, "johnd");
        assert_eq!(params.state, Some(State::Present));
    }

    #[test]
    fn test_parse_passwd_line() {
        let line = "johnd:x:1040:1000:John Doe:/home/johnd:/bin/bash";
        let info = parse_passwd_line(line).unwrap();
        assert_eq!(info.name, "johnd");
        assert_eq!(info.uid, 1040);
        assert_eq!(info.gid, 1000);
        assert_eq!(info.comment, "John Doe");
        assert_eq!(info.home, "/home/johnd");
        assert_eq!(info.shell, "/bin/bash");
    }

    #[test]
    fn test_build_useradd_command_basic() {
        let params = Params {
            name: "johnd".to_string(),
            state: Some(State::Present),
            uid: Some(1040),
            group: Some("admin".to_string()),
            groups: Some(vec!["docker".to_string(), "wheel".to_string()]),
            append: None,
            home: Some("/home/johnd".to_string()),
            create_home: Some(true),
            shell: Some("/bin/bash".to_string()),
            comment: Some("John Doe".to_string()),
            system: None,
            password: None,
            remove: None,
        };
        let cmd = build_useradd_command(&params);
        assert!(cmd.contains(&"useradd".to_string()));
        assert!(cmd.contains(&"-u".to_string()));
        assert!(cmd.contains(&"1040".to_string()));
        assert!(cmd.contains(&"-g".to_string()));
        assert!(cmd.contains(&"admin".to_string()));
        assert!(cmd.contains(&"-G".to_string()));
        assert!(cmd.contains(&"docker,wheel".to_string()));
        assert!(cmd.contains(&"johnd".to_string()));
    }

    #[test]
    fn test_build_userdel_command() {
        let params = Params {
            name: "johnd".to_string(),
            state: Some(State::Absent),
            uid: None,
            group: None,
            groups: None,
            append: None,
            home: None,
            create_home: None,
            shell: None,
            comment: None,
            system: None,
            password: None,
            remove: Some(true),
        };
        let cmd = build_userdel_command(&params);
        assert_eq!(cmd, vec!["userdel", "-r", "johnd"]);
    }
}