summaryrefslogtreecommitdiffstats
path: root/letmeinfwd/src/uid_gid.rs
blob: 5e52d5bf8a0942e6d32eb24f63d170a1af684566 (plain)
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
// -*- coding: utf-8 -*-
//
// Copyright (C) 2024 Michael Büsch <m@bues.ch>
//
// Licensed under the Apache License version 2.0
// or the MIT license, at your option.
// SPDX-License-Identifier: Apache-2.0 OR MIT

use anyhow::{self as ah, format_err as err, Context as _};
use std::fs::read_to_string;

/// Resolve a user name into a UID.
pub fn os_get_uid(user_name: &str) -> ah::Result<u32> {
    let data = read_to_string("/etc/passwd").context("Read /etc/passwd database")?;

    for line in data.lines() {
        let mut fields = line.splitn(7, ':');

        let name = fields.next().context("Get passwd name")?;
        if name == user_name {
            let _pw = fields.next().context("Get passwd password")?;
            let uid = fields.next().context("Get passwd uid")?;

            return uid.parse().context("Parse passwd uid");
        }
    }

    Err(err!("User '{user_name}' not found in /etc/passwd"))
}

/// Resolve a group name into a GID.
pub fn os_get_gid(group_name: &str) -> ah::Result<u32> {
    let data = read_to_string("/etc/group").context("Read /etc/group database")?;

    for line in data.lines() {
        let mut fields = line.splitn(4, ':');

        let name = fields.next().context("Get group name")?;
        if name == group_name {
            let _pw = fields.next().context("Get group password")?;
            let gid = fields.next().context("Get group gid")?;

            return gid.parse().context("Parse group gid");
        }
    }

    Err(err!("Group '{group_name}' not found in /etc/group"))
}

// vim: ts=4 sw=4 expandtab
bues.ch cgit interface