8 个版本 (破坏性更新)
| 0.8.0 | 2024年5月18日 |
|---|---|
| 0.7.0 | 2024年4月24日 |
| 0.6.0 | 2024年2月17日 |
| 0.5.0 | 2023年8月22日 |
| 0.1.0 | 2019年6月9日 |
#75 在 数据库接口 中
每月 26,001 次下载
用于 2 crates
38KB
823 行
libnss-rs
Rust绑定库,用于创建libnss模块。
目前支持以下数据库
- passwd
- shadow
- group
- hosts
入门指南
-
创建一个新的库
cargo new nss_example --lib -
在您的
Cargo.toml中将库类型更改为cdylib[lib] name = "nss_example" crate-type = [ "cdylib" ]*** 注意 *** crate的名称本身并不重要,但是库本身必须遵循
nss_xxx的模式。 -
将
libnss添加到您的Cargo.toml[dependencies] libc = "0.2.0" libnss = "0.8.0" -
实现passwd数据库
use libnss::passwd::{PasswdHooks, Passwd}; use libnss::libnss_passwd_hooks; struct ExamplePasswd; libnss_passwd_hooks!(example, ExamplePasswd);libnss_passwd_hooks的第一个参数必须是您最终库的名称libnss_example.so.2impl 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 } } -
构建
cargo build --release -
安装库
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 -
在
/etc/nsswitch.conf中启用您的nss模块,例如passwd: example files systemd这里的名字必须遵循最终库的名称
libnss_example.so.2 -
查看示例以获取更多信息
依赖项
~56KB