From 00a31d1962227312640632766c9a1fdd931e245c Mon Sep 17 00:00:00 2001 From: supernovatux Date: Fri, 7 Apr 2023 16:41:54 +0530 Subject: [PATCH] init --- .gitignore | 7 ++++ Cargo.lock | 109 ++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 21 ++++++++++ LICENSE | 21 ++++++++++ PKGBUILD | 18 +++++++++ PkgBuilder.sh | 8 ++++ README.md | 3 ++ src/lib.rs | 66 ++++++++++++++++++++++++++++++ 8 files changed, 253 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 LICENSE create mode 100644 PKGBUILD create mode 100644 PkgBuilder.sh create mode 100644 README.md create mode 100644 src/lib.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cf149ee --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +/target +*.tar.gz +*.tar.zst +*.out* +pkg +completions +.vscode/settings.json diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..e99d3ae --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,109 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array", +] + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "cpufeatures" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "280a9f2d8b3a38871a3c8a46fb80db65e5e5ed97da80c4d08bf27fb63e35e181" +dependencies = [ + "libc", +] + +[[package]] +name = "crypto-common" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" +dependencies = [ + "generic-array", + "typenum", +] + +[[package]] +name = "digest" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8168378f4e5023e7218c89c891c0fd8ecdb5e5e4f18cb78f38cf245dd021e76f" +dependencies = [ + "block-buffer", + "crypto-common", +] + +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", +] + +[[package]] +name = "libc" +version = "0.2.141" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3304a64d199bb964be99741b7a14d26972741915b3649639149b2479bb46f4b5" + +[[package]] +name = "pam_pwdfile_rs" +version = "0.1.0" +dependencies = [ + "pamsm", + "sha2", +] + +[[package]] +name = "pamsm" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62d7e2fe82d6af7029f1332ecd9a6cfdf135b21ce6b2de51dd6a704ba7caad46" +dependencies = [ + "bitflags", +] + +[[package]] +name = "sha2" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82e6b795fe2e3b1e845bafcb27aa35405c4d47cdfc92af5fc8d3002f76cebdc0" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + +[[package]] +name = "typenum" +version = "1.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "497961ef93d974e23eb6f433eb5fe1b7930b659f06d12dec6fc44a8f554c0bba" + +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..2baee8e --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,21 @@ +[package] +name = "pam_pwdfile_rs" +version = "0.1.0" +authors = ["Supernovatux "] + +[dependencies] +pamsm = {version = "*" ,features = ["libpam"] } +sha2 = "0.10.6" + +[lib] +name = "pam_pwdfile_rs" +crate-type = ["cdylib"] + +[profile.release] +debug = false +opt-level = 3 +lto = true +codegen-units = 1 +panic = "abort" +overflow-checks = false +strip = "symbols" diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..74933c2 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2022 Supernovatux + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/PKGBUILD b/PKGBUILD new file mode 100644 index 0000000..5998219 --- /dev/null +++ b/PKGBUILD @@ -0,0 +1,18 @@ +# Maintainer: Supernovatux + +pkgname=libpam-pwdfile-rs +pkgver=0.1.0 +pkgrel=1 +pkgdesc="A simple PAM module to authenticate users against a password file" +url="https://github.com/Supernovatux/libpam-pwdfile-rs" +license=("MIT") +arch=("x86_64") +provides=("libpam-pwdfile-rs") +conflicts=("libpam-pwdfile-rs") +source=("https://github.com/Supernovatux/auto_backlight/releases/download/v$pkgver/auto-backlight-$pkgver-x86_64.tar.gz") +sha512sums=("3e961b01592ec79b2cbd56f1c79c23e166c3701bce6d7b965eede873450e9a2cd81e09200b9ecbef9e37e32fc5f80a7af9f5f9672782e1f34bc23ed080d21b0a") + +package() { + install -Dm755 target/release/auto-backlight -t "$pkgdir/usr/bin" + install -Dm644 LICENSE "$pkgdir/usr/share/licenses/$pkgname/LICENSE" +} diff --git a/PkgBuilder.sh b/PkgBuilder.sh new file mode 100644 index 0000000..4faa8d7 --- /dev/null +++ b/PkgBuilder.sh @@ -0,0 +1,8 @@ +#!/bin/zsh +pkgname=libpam-pwdfile-rs +cargo +nightly build -Z build-std=std,panic_abort -Z build-std-features=panic_immediate_abort --target x86_64-unknown-linux-gnu --release +version=$(cat Cargo.toml | grep -m1 version | awk '{print $3}' | tr -d '"') +tar -cvf $pkgname-$version-x86_64.tar.gz target/x86_64-unknown-linux-gnu/release/libpam_pwdfile_rs.so LICENSE -I "gzip --best" +shasum=$(sha512sum ./$pkgname-$version-x86_64.tar.gz | awk '{print $1}') +sed -i "s/sha512sums=.*/sha512sums=(\"$shasum\")/" PKGBUILD +sed -i "s/pkgver=.*/pkgver=$version/" PKGBUILD diff --git a/README.md b/README.md new file mode 100644 index 0000000..ddd6363 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# libpam-pwdfile-rs + +This is a rust port of [libpam-pwdfile](https://git.tiwe.de/libpam-pwdfile.git). It is a PAM module that allows you to authenticate against a password file. Password are hashed with sha512 diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..cb5f590 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,66 @@ +#[macro_use] +extern crate pamsm; +extern crate sha2; +use pamsm::{Pam, PamError, PamFlags, PamLibExt, PamServiceModule}; +use sha2::{Digest, Sha512}; +use std::{ffi::CStr, io::BufRead}; +macro_rules! try_or_ret { + ($expr:expr, $err:expr) => { + match $expr { + Ok(val) => val, + Err(_) => return $err, + } + }; + ($expr:expr) => { + match $expr { + Ok(exp) => exp, + Err(e) => return e, + } + }; +} +struct PamPwdfile; +fn get_string(pam_string: Result, PamError>) -> Result { + match pam_string { + Ok(Some(p)) => Ok(p.to_str().map_err(|_| PamError::AUTH_ERR)?.to_string()), + _ => Err(PamError::AUTH_ERR), + } +} +impl PamServiceModule for PamPwdfile { + fn authenticate(pamh: Pam, _flags: PamFlags, args: Vec) -> PamError { + let mut sha512 = Sha512::new(); + let username: String = try_or_ret!(get_string(pamh.get_cached_user())); + let password: String = try_or_ret!(get_string(pamh.get_authtok(None))); + sha512.update(password.as_bytes()); + let hash = format!("{:x}", sha512.finalize()); + let path_to_file = if let Some(index) = args.iter().position(|x| x == "pwdfile") { + if let Some(pwdfile) = args.get(index + 1) { + pwdfile + } else { + return PamError::AUTH_ERR; + } + } else { + return PamError::AUTH_ERR; + }; + let file = try_or_ret!( + std::fs::File::open(path_to_file), + PamError::AUTHINFO_UNAVAIL + ); + let reader = std::io::BufReader::new(file); + for i in reader.lines() { + let line = try_or_ret!(i, PamError::AUTHINFO_UNAVAIL); + let new = line.split_once(':').map(|(user, pass)| { + if user.trim() == username && pass.trim() == hash { + PamError::SUCCESS + } else { + PamError::AUTH_ERR + } + }); + if let Some(PamError::SUCCESS) = new { + return PamError::SUCCESS; + } + } + PamError::AUTH_ERR + } +} + +pam_module!(PamPwdfile);