Git Product home page Git Product logo

libnss-rs's Introduction

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

libnss-rs's People

Contributors

csnewman avatar bjeanes avatar ethanwu10 avatar ryankurte avatar denisonbarbosa avatar edevil avatar dhnikolas avatar pbar1 avatar petski avatar

Stargazers

Hunter Madison avatar Feix C avatar  avatar Diana avatar Nikhil Jha avatar  avatar Iurii avatar  avatar Daniel Thwaites avatar  avatar  avatar SOZEL avatar Onofre Souza avatar Alvin avatar 秋 奈月 avatar Stanislav Putrya avatar Nick Ufer avatar Aurélien Bertron avatar Donapieppo avatar  avatar Jamie Hall avatar Luka Teras avatar  avatar  avatar

Watchers

James Cloos avatar  avatar  avatar

libnss-rs's Issues

Release next version?

Hi there,

I noticed that the version on crates.io is different from what's in github here. I think mainly this in the group and passwd traits and the responses they provide (the crates.io version uses Option, not Response). It would be great if you could make a release of this to crates.io please!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.