changeset 14:51b097c12d3c

make PamResultCode an enum
author Anthony Nowell <anthony@algorithmia.com>
date Sat, 23 Sep 2017 14:30:18 -0600
parents cc39d168aeb8
children 27730595f1ea
files src/constants.rs src/conv.rs src/module.rs
diffstat 3 files changed, 46 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
--- a/src/constants.rs	Sun Feb 26 12:16:03 2017 +0100
+++ b/src/constants.rs	Sat Sep 23 14:30:18 2017 -0600
@@ -5,7 +5,6 @@
 pub type PamFlag = c_uint;
 pub type PamItemType = c_int;
 pub type PamMessageStyle = c_int;
-pub type PamResultCode = c_int;
 pub type AlwaysZero = c_int;
 
 // The Linux-PAM flags
@@ -59,35 +58,40 @@
 
 // The Linux-PAM return values
 // see /usr/include/security/_pam_types.h
-pub const PAM_SUCCESS: PamResultCode = 0;
-pub const PAM_OPEN_ERR: PamResultCode = 1;
-pub const PAM_SYMBOL_ERR: PamResultCode = 2;
-pub const PAM_SERVICE_ERR: PamResultCode = 3;
-pub const PAM_SYSTEM_ERR: PamResultCode = 4;
-pub const PAM_BUF_ERR: PamResultCode = 5;
-pub const PAM_PERM_DENIED: PamResultCode = 6;
-pub const PAM_AUTH_ERR: PamResultCode = 7;
-pub const PAM_CRED_INSUFFICIENT: PamResultCode = 8;
-pub const PAM_AUTHINFO_UNAVAIL: PamResultCode = 9;
-pub const PAM_USER_UNKNOWN: PamResultCode = 10;
-pub const PAM_MAXTRIES: PamResultCode = 11;
-pub const PAM_NEW_AUTHTOK_REQD: PamResultCode = 12;
-pub const PAM_ACCT_EXPIRED: PamResultCode = 13;
-pub const PAM_SESSION_ERR: PamResultCode = 14;
-pub const PAM_CRED_UNAVAIL: PamResultCode = 15;
-pub const PAM_CRED_EXPIRED: PamResultCode = 16;
-pub const PAM_CRED_ERR: PamResultCode = 17;
-pub const PAM_NO_MODULE_DATA: PamResultCode = 18;
-pub const PAM_CONV_ERR: PamResultCode = 19;
-pub const PAM_AUTHTOK_ERR: PamResultCode = 20;
-pub const PAM_AUTHTOK_RECOVERY_ERR: PamResultCode = 21;
-pub const PAM_AUTHTOK_LOCK_BUSY: PamResultCode = 22;
-pub const PAM_AUTHTOK_DISABLE_AGING: PamResultCode = 23;
-pub const PAM_TRY_AGAIN: PamResultCode = 24;
-pub const PAM_IGNORE: PamResultCode = 25;
-pub const PAM_ABORT: PamResultCode = 26;
-pub const PAM_AUTHTOK_EXPIRED: PamResultCode = 27;
-pub const PAM_MODULE_UNKNOWN: PamResultCode = 28;
-pub const PAM_BAD_ITEM: PamResultCode = 29;
-pub const PAM_CONV_AGAIN: PamResultCode = 30;
-pub const PAM_INCOMPLETE: PamResultCode = 31;
+#[allow(non_camel_case_types, dead_code)]
+#[derive(Debug, PartialEq)]
+#[repr(C)]
+pub enum PamResultCode {
+    PAM_SUCCESS = 0,
+    PAM_OPEN_ERR = 1,
+    PAM_SYMBOL_ERR = 2,
+    PAM_SERVICE_ERR = 3,
+    PAM_SYSTEM_ERR = 4,
+    PAM_BUF_ERR = 5,
+    PAM_PERM_DENIED = 6,
+    PAM_AUTH_ERR = 7,
+    PAM_CRED_INSUFFICIENT = 8,
+    PAM_AUTHINFO_UNAVAIL = 9,
+    PAM_USER_UNKNOWN = 10,
+    PAM_MAXTRIES = 11,
+    PAM_NEW_AUTHTOK_REQD = 12,
+    PAM_ACCT_EXPIRED = 13,
+    PAM_SESSION_ERR = 14,
+    PAM_CRED_UNAVAIL = 15,
+    PAM_CRED_EXPIRED = 16,
+    PAM_CRED_ERR = 17,
+    PAM_NO_MODULE_DATA = 18,
+    PAM_CONV_ERR = 19,
+    PAM_AUTHTOK_ERR = 20,
+    PAM_AUTHTOK_RECOVERY_ERR = 21,
+    PAM_AUTHTOK_LOCK_BUSY = 22,
+    PAM_AUTHTOK_DISABLE_AGING = 23,
+    PAM_TRY_AGAIN = 24,
+    PAM_IGNORE = 25,
+    PAM_ABORT = 26,
+    PAM_AUTHTOK_EXPIRED = 27,
+    PAM_MODULE_UNKNOWN = 28,
+    PAM_BAD_ITEM = 29,
+    PAM_CONV_AGAIN = 30,
+    PAM_INCOMPLETE = 31,
+}
--- a/src/conv.rs	Sun Feb 26 12:16:03 2017 +0100
+++ b/src/conv.rs	Sat Sep 23 14:30:18 2017 -0600
@@ -2,7 +2,7 @@
 use std::ptr;
 use std::ffi::{CStr, CString};
 
-use constants;
+use constants::PamResultCode;
 use constants::*;
 use module::{PamItem, PamResult};
 
@@ -61,7 +61,7 @@
 
         let ret = (self.conv)(1, &&msg, &mut resp_ptr, self.appdata_ptr);
 
-        if constants::PAM_SUCCESS == ret {
+        if PamResultCode::PAM_SUCCESS == ret {
             if resp_ptr.is_null() {
                 Ok(None)
             } else {
--- a/src/module.rs	Sun Feb 26 12:16:03 2017 +0100
+++ b/src/module.rs	Sat Sep 23 14:30:18 2017 -0600
@@ -4,8 +4,7 @@
 use std::{mem, ptr};
 use std::ffi::{CStr, CString};
 
-use constants;
-use constants::*;
+use constants::{PamResultCode, PamItemType};
 
 /// Opaque type, used as a pointer when making pam API calls.
 ///
@@ -77,7 +76,7 @@
     let c_key = CString::new(key).unwrap().as_ptr();
     let mut ptr: *const PamDataT = ptr::null();
     let res = pam_get_data(pamh, c_key, &mut ptr);
-    if constants::PAM_SUCCESS == res && !ptr.is_null() {
+    if PamResultCode::PAM_SUCCESS == res && !ptr.is_null() {
         let typed_ptr: *const T = mem::transmute(ptr);
         let data: &T = &*typed_ptr;
         Ok(data)
@@ -97,7 +96,7 @@
         let c_data: Box<PamDataT> = mem::transmute(data);
         pam_set_data(pamh, c_key, c_data, cleanup::<T>)
     };
-    if constants::PAM_SUCCESS == res {
+    if PamResultCode::PAM_SUCCESS == res {
         Ok(())
     } else {
         Err(res)
@@ -125,7 +124,7 @@
         let t: &T = &*typed_ptr;
         (r, t)
     };
-    if constants::PAM_SUCCESS == res {
+    if PamResultCode::PAM_SUCCESS == res {
         Ok(item)
     } else {
         Err(res)
@@ -150,7 +149,7 @@
                      // pointer
                      (c_item as *const PamItemT).as_ref().unwrap())
     };
-    if constants::PAM_SUCCESS == res {
+    if PamResultCode::PAM_SUCCESS == res {
         Ok(())
     } else {
         Err(res)
@@ -170,10 +169,10 @@
         None => ptr::null(),
     };
     let res = unsafe { pam_get_user(pamh, &ptr, c_prompt) };
-    if constants::PAM_SUCCESS == res && !ptr.is_null() {
+    if PamResultCode::PAM_SUCCESS == res && !ptr.is_null() {
         let const_ptr = ptr as *const c_char;
         let bytes = unsafe { CStr::from_ptr(const_ptr).to_bytes() };
-        String::from_utf8(bytes.to_vec()).map_err(|_| PAM_CONV_ERR)
+        String::from_utf8(bytes.to_vec()).map_err(|_| PamResultCode::PAM_CONV_ERR)
     } else {
         Err(res)
     }