comparison src/module.rs @ 71:58f9d2a4df38

Reorganize everything again??? - Splits ffi/memory stuff into a bunch of stuff in the pam_ffi module. - Builds infrastructure for passing Messages and Responses. - Adds tests for some things at least.
author Paul Fisher <paul@pfish.zone>
date Tue, 03 Jun 2025 21:54:58 -0400
parents 9f8381a1c09c
children 47eb242a4f88
comparison
equal deleted inserted replaced
70:9f8381a1c09c 71:58f9d2a4df38
3 // Temporarily allowed until we get the actual conversation functions hooked up. 3 // Temporarily allowed until we get the actual conversation functions hooked up.
4 #![allow(dead_code)] 4 #![allow(dead_code)]
5 5
6 use crate::constants::{ErrorCode, Flags, Result}; 6 use crate::constants::{ErrorCode, Flags, Result};
7 use crate::conv::BinaryData; 7 use crate::conv::BinaryData;
8 use crate::conv::{Conversation, Message, Response}; 8 use crate::conv::{Conversation, Response};
9 use crate::handle::PamModuleHandle; 9 use crate::handle::PamModuleHandle;
10 use crate::pam_ffi::Message;
10 use secure_string::SecureString; 11 use secure_string::SecureString;
11 use std::ffi::CStr; 12 use std::ffi::CStr;
12 13
13 /// A trait for a PAM module to implement. 14 /// A trait for a PAM module to implement.
14 /// 15 ///
360 pamh: *mut libc::c_void, 361 pamh: *mut libc::c_void,
361 flags: Flags, 362 flags: Flags,
362 argc: c_int, 363 argc: c_int,
363 argv: *const *const c_char, 364 argv: *const *const c_char,
364 ) -> c_int { 365 ) -> c_int {
365 let args = extract_argv(argc, argv); 366 if let Some(handle) = unsafe { pamh.cast::<LibPamHandle>().as_mut() } {
366 ErrorCode::result_to_c(super::$ident::account_management( 367 let args = extract_argv(argc, argv);
367 unsafe { LibPamHandle::from_ptr(pamh) }, 368 ErrorCode::result_to_c(super::$ident::account_management(handle, args, flags))
368 args, 369 } else {
369 flags, 370 ErrorCode::Ignore as c_int
370 )) 371 }
371 } 372 }
372 373
373 #[no_mangle] 374 #[no_mangle]
374 extern "C" fn pam_sm_authenticate( 375 extern "C" fn pam_sm_authenticate(
375 pamh: *mut libc::c_void, 376 pamh: *mut libc::c_void,
376 flags: Flags, 377 flags: Flags,
377 argc: c_int, 378 argc: c_int,
378 argv: *const *const c_char, 379 argv: *const *const c_char,
379 ) -> c_int { 380 ) -> c_int {
380 let args = extract_argv(argc, argv); 381 if let Some(handle) = unsafe { pamh.cast::<LibPamHandle>().as_mut() } {
381 ErrorCode::result_to_c(super::$ident::authenticate( 382 let args = extract_argv(argc, argv);
382 unsafe { LibPamHandle::from_ptr(pamh) }, 383 ErrorCode::result_to_c(super::$ident::authenticate(handle, args, flags))
383 args, 384 } else {
384 flags, 385 ErrorCode::Ignore as c_int
385 )) 386 }
386 } 387 }
387 388
388 #[no_mangle] 389 #[no_mangle]
389 extern "C" fn pam_sm_chauthtok( 390 extern "C" fn pam_sm_chauthtok(
390 pamh: *mut libc::c_void, 391 pamh: *mut libc::c_void,
391 flags: Flags, 392 flags: Flags,
392 argc: c_int, 393 argc: c_int,
393 argv: *const *const c_char, 394 argv: *const *const c_char,
394 ) -> c_int { 395 ) -> c_int {
395 let args = extract_argv(argc, argv); 396 if let Some(handle) = unsafe { pamh.cast::<LibPamHandle>().as_mut() } {
396 ErrorCode::result_to_c(super::$ident::change_authtok( 397 let args = extract_argv(argc, argv);
397 unsafe { LibPamHandle::from_ptr(pamh) }, 398 ErrorCode::result_to_c(super::$ident::change_authtok(handle, args, flags))
398 args, 399 } else {
399 flags, 400 ErrorCode::Ignore as c_int
400 )) 401 }
401 } 402 }
402 403
403 #[no_mangle] 404 #[no_mangle]
404 extern "C" fn pam_sm_close_session( 405 extern "C" fn pam_sm_close_session(
405 pamh: *mut libc::c_void, 406 pamh: *mut libc::c_void,
406 flags: Flags, 407 flags: Flags,
407 argc: c_int, 408 argc: c_int,
408 argv: *const *const c_char, 409 argv: *const *const c_char,
409 ) -> c_int { 410 ) -> c_int {
410 let args = extract_argv(argc, argv); 411 if let Some(handle) = unsafe { pamh.cast::<LibPamHandle>().as_mut() } {
411 ErrorCode::result_to_c(super::$ident::close_session( 412 let args = extract_argv(argc, argv);
412 unsafe { LibPamHandle::from_ptr(pamh) }, 413 ErrorCode::result_to_c(super::$ident::close_session(handle, args, flags))
413 args, 414 } else {
414 flags, 415 ErrorCode::Ignore as c_int
415 )) 416 }
416 } 417 }
417 418
418 #[no_mangle] 419 #[no_mangle]
419 extern "C" fn pam_sm_open_session( 420 extern "C" fn pam_sm_open_session(
420 pamh: *mut libc::c_void, 421 pamh: *mut libc::c_void,
421 flags: Flags, 422 flags: Flags,
422 argc: c_int, 423 argc: c_int,
423 argv: *const *const c_char, 424 argv: *const *const c_char,
424 ) -> c_int { 425 ) -> c_int {
425 let args = extract_argv(argc, argv); 426 let args = extract_argv(argc, argv);
426 ErrorCode::result_to_c(super::$ident::open_session( 427 if let Some(handle) = unsafe { pamh.cast::<LibPamHandle>().as_mut() } {
427 unsafe { LibPamHandle::from_ptr(pamh) }, 428 ErrorCode::result_to_c(super::$ident::open_session(handle, args, flags))
428 args, 429 } else {
429 flags, 430 ErrorCode::Ignore as c_int
430 )) 431 }
431 } 432 }
432 433
433 #[no_mangle] 434 #[no_mangle]
434 extern "C" fn pam_sm_setcred( 435 extern "C" fn pam_sm_setcred(
435 pamh: *mut libc::c_void, 436 pamh: *mut libc::c_void,
436 flags: Flags, 437 flags: Flags,
437 argc: c_int, 438 argc: c_int,
438 argv: *const *const c_char, 439 argv: *const *const c_char,
439 ) -> c_int { 440 ) -> c_int {
440 let args = extract_argv(argc, argv); 441 let args = extract_argv(argc, argv);
441 ErrorCode::result_to_c(super::$ident::set_credentials( 442 if let Some(handle) = unsafe { pamh.cast::<LibPamHandle>().as_mut() } {
442 unsafe { LibPamHandle::from_ptr(pamh) }, 443 ErrorCode::result_to_c(super::$ident::set_credentials(handle, args, flags))
443 args, 444 } else {
444 flags, 445 ErrorCode::Ignore as c_int
445 )) 446 }
446 } 447 }
447 448
448 /// Turns `argc`/`argv` into a [Vec] of [CStr]s. 449 /// Turns `argc`/`argv` into a [Vec] of [CStr]s.
449 /// 450 ///
450 /// # Safety 451 /// # Safety
458 } 459 }
459 }; 460 };
460 } 461 }
461 462
462 #[cfg(test)] 463 #[cfg(test)]
463 mod test { 464 mod tests {
464 use super::{ 465 use super::{
465 Conversation, ConversationMux, ErrorCode, Message, Response, Result, SecureString, 466 Conversation, ConversationMux, ErrorCode, Message, Response, Result, SecureString,
466 }; 467 };
467 468
468 /// Compile-time test that the `pam_hooks` macro compiles. 469 /// Compile-time test that the `pam_hooks` macro compiles.