8 releases (breaking)

0.8.0 May 18, 2024
0.7.0 Apr 24, 2024
0.6.0 Feb 17, 2024
0.5.0 Aug 22, 2023
0.1.0 Jun 9, 2019

#188 in Database interfaces

Download history 7839/week @ 2024-08-18 6270/week @ 2024-08-25 6135/week @ 2024-09-01 5974/week @ 2024-09-08 5646/week @ 2024-09-15 5912/week @ 2024-09-22 6321/week @ 2024-09-29 6373/week @ 2024-10-06 7196/week @ 2024-10-13 6479/week @ 2024-10-20 6515/week @ 2024-10-27 7394/week @ 2024-11-03 5896/week @ 2024-11-10 6819/week @ 2024-11-17 5988/week @ 2024-11-24 6652/week @ 2024-12-01

25,658 downloads per month
Used in 2 crates

LGPL-3.0

38KB
823 lines

libnss-rs

Rust bindings for creating libnss modules.

Currently supports the following databases:

  • passwd
  • shadow
  • group
  • hosts

Getting started

  • Create a new library

    cargo new nss_example --lib
    
  • Change library type to cdylib in your Cargo.toml

    [lib]
    name = "nss_example"
    crate-type = [ "cdylib" ]
    

    *** NOTE *** The name of the crate itself is not important, however the library itself must follow the nss_xxx pattern.

  • Add libnss to your Cargo.toml

    [dependencies]
    libc = "0.2.0"
    libnss = "0.8.0"
    
  • Implement a passwd database

    use libnss::passwd::{PasswdHooks, Passwd};
    use libnss::libnss_passwd_hooks;
    
    struct ExamplePasswd;
    libnss_passwd_hooks!(example, ExamplePasswd);
    

    It is important that the first param of libnss_passwd_hooks is the name of your final library libnss_example.so.2

    impl PasswdHooks for HardcodedPasswd {
        fn get_all_entries() -> Vec<Passwd> {
            vec![
                Passwd {
                    name: "test".to_string(),
                    passwd: "x".to_string(),
                    uid: 1005,
                    gid: 1005,
                    gecos: "Test Account".to_string(),
                    dir: "/home/test".to_string(),
                    shell: "/bin/bash".to_string(),
                }
            ]
        }
    
        fn get_entry_by_uid(uid: libc::uid_t) -> Option<Passwd> {
            if uid == 1005 {
                return Some(Passwd {
                    name: "test".to_string(),
                    passwd: "x".to_string(),
                    uid: 1005,
                    gid: 1005,
                    gecos: "Test Account".to_string(),
                    dir: "/home/test".to_string(),
                    shell: "/bin/bash".to_string(),
                });
            }
    
            None
        }
    
        fn get_entry_by_name(name: String) -> Option<Passwd> {
            if name == "test" {
                return Some(Passwd {
                    name: "test".to_string(),
                    passwd: "x".to_string(),
                    uid: 1005,
                    gid: 1005,
                    gecos: "Test Account".to_string(),
                    dir: "/home/test".to_string(),
                    shell: "/bin/bash".to_string(),
                });
            }
    
            None
        }
    }
    
  • Build

    cargo build --release
    
  • Install the library

    cd target/release
    cp libnss_example.so libnss_example.so.2
    sudo install -m 0644 libnss_example.so.2 /lib
    sudo /sbin/ldconfig -n /lib /usr/lib
    
  • Enable your nss module in /etc/nsswitch.conf eg:

    passwd:         example files systemd
    

    The name in here must follow the final library name libnss_example.so.2

  • Look at the examples for more information

Dependencies

~56KB