5 releases (breaking)

0.5.0 Aug 22, 2023
0.4.0 Jul 28, 2020
0.3.0 Jun 30, 2020
0.2.0 Sep 19, 2019
0.1.0 Jun 9, 2019

#76 in Unix APIs

Download history 1255/week @ 2023-07-29 1140/week @ 2023-08-05 1974/week @ 2023-08-12 1925/week @ 2023-08-19 1307/week @ 2023-08-26 1700/week @ 2023-09-02 1967/week @ 2023-09-09 1614/week @ 2023-09-16 1169/week @ 2023-09-23 1195/week @ 2023-09-30 1668/week @ 2023-10-07 1890/week @ 2023-10-14 1492/week @ 2023-10-21 1635/week @ 2023-10-28 1432/week @ 2023-11-04 1073/week @ 2023-11-11

5,920 downloads per month
Used in libnss-wiregarden

LGPL-3.0

36KB
789 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"
lazy_static = "1.3.0"
paste = "0.1"
libnss = "0.1.0"
  • Add the following to your src/main.rs
extern crate libc;
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate libnss;
  • Implement a passwd database
use libnss::passwd::{PasswdHooks, Passwd};

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
    }
}
  • 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

~55KB