Skip to content

Commit

Permalink
feat: upgrade dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
gboutry committed Jan 22, 2023
1 parent 2fa5740 commit b5d24eb
Show file tree
Hide file tree
Showing 16 changed files with 146 additions and 308 deletions.
298 changes: 80 additions & 218 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion crates/krb5_js/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ krb5-rs = { path = "../krb5_rs" }
thiserror = "1"
napi = { version = "2", default-features = false, features = ["napi6"] }
napi-derive = "2"
base64 = "0.13"
base64 = "0.21"
regex = "1"
lazy_static = "1.4"
2 changes: 1 addition & 1 deletion crates/krb5_js/src/kinit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub fn kinit_function(
(principal.as_str(), None)
};
let context = Context::new()?;
let realm = match realm_from_split.or_else(|| realm.as_deref()) {
let realm = match realm_from_split.or(realm.as_deref()) {
Some(realm) => realm.to_owned(),
None => context.get_default_realm()?,
};
Expand Down
3 changes: 2 additions & 1 deletion crates/krb5_js/src/spnego.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use base64::{engine, Engine};
use krb5_rs::gssapi;
use napi::{Env, JsString, Task};
use napi_derive::napi;
Expand Down Expand Up @@ -52,7 +53,7 @@ pub fn generate_spnego_token_function(
gssapi::krb5_ccache_name(ccname.as_ref().unwrap())?;
}
let token = gssapi::get_token(target_name)?;
Ok(base64::encode(token))
Ok(engine::general_purpose::STANDARD.encode(token))
}

pub struct Spnego {
Expand Down
1 change: 0 additions & 1 deletion crates/krb5_rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
lazy_static = "1.4"
thiserror = "1"
krb5-sys = {path = "../krb5_sys"}
12 changes: 6 additions & 6 deletions crates/krb5_rs/src/ccache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ use krb5_sys::{

use super::{Context, Credentials, Krb5Error, Principal, Result};

pub struct CCache<'a> {
pub(crate) context: &'a Context,
pub struct CCache<'context> {
pub(crate) context: &'context Context,
pub(crate) inner: krb5_ccache,
}

impl<'a> CCache<'a> {
pub fn default(context: &'a Context) -> Result<CCache> {
impl<'context> CCache<'context> {
pub fn default(context: &'context Context) -> Result<CCache> {
let mut ccache: MaybeUninit<krb5_ccache> = MaybeUninit::uninit();
let error_code = unsafe { krb5_cc_default(context.inner, ccache.as_mut_ptr()) };
Krb5Error::exit_if_library_error(context, error_code)?;
Expand All @@ -27,7 +27,7 @@ impl<'a> CCache<'a> {
})
}

pub fn resolve(context: &'a Context, cc_name: &str) -> Result<CCache<'a>> {
pub fn resolve(context: &'context Context, cc_name: &str) -> Result<CCache<'context>> {
let mut ccache: MaybeUninit<krb5_ccache> = MaybeUninit::uninit();

let cc_name = CString::new(cc_name).map_err(|_| Krb5Error::StringConversionError)?;
Expand Down Expand Up @@ -77,7 +77,7 @@ impl<'a> CCache<'a> {
}
}

impl<'a> Drop for CCache<'a> {
impl<'context> Drop for CCache<'context> {
fn drop(&mut self) {
if !self.context.inner.is_null() && !self.inner.is_null() {
unsafe { krb5_cc_close(self.context.inner, self.inner) };
Expand Down
25 changes: 3 additions & 22 deletions crates/krb5_rs/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// use std::ops::{Deref, DerefMut};
use std::{ffi::CStr, mem::MaybeUninit, os::raw::c_char, sync::Mutex};

use crate::Result;
Expand All @@ -7,32 +6,14 @@ use krb5_sys::{
krb5_free_error_message, krb5_get_default_realm, krb5_get_error_message, krb5_init_context,
};

use lazy_static::lazy_static;

use super::Krb5Error;

lazy_static! {
static ref CONTEXT_LOCK: Mutex<()> = Mutex::new(());
}
static CONTEXT_LOCK: Mutex<()> = Mutex::new(());

pub struct Context {
pub(crate) inner: krb5_context,
}

// impl Deref for Context {
// type Target = krb5_context;

// fn deref(&self) -> &Self::Target {
// &self.inner
// }
// }

// impl DerefMut for Context {
// fn deref_mut(&mut self) -> &mut Self::Target {
// &mut self.inner
// }
// }

impl Context {
pub fn get_default_realm(&self) -> Result<String> {
let mut default_realm: MaybeUninit<*mut c_char> = MaybeUninit::zeroed();
Expand Down Expand Up @@ -73,7 +54,7 @@ impl Context {
}

pub fn new() -> Result<Context> {
let _guard = CONTEXT_LOCK.lock().expect("Failed to lock context");
let _guard = &CONTEXT_LOCK.lock().expect("Failed to lock context");
let mut krb5_context: MaybeUninit<krb5_context> = MaybeUninit::uninit();
let error_code = unsafe { krb5_init_context(krb5_context.as_mut_ptr()) };
if error_code != 0 {
Expand All @@ -89,7 +70,7 @@ impl Context {

impl Drop for Context {
fn drop(&mut self) {
let _guard = CONTEXT_LOCK.lock().expect("Failed to lock context");
let _guard = &CONTEXT_LOCK.lock().expect("Failed to lock context");
if !self.inner.is_null() {
unsafe { krb5_free_context(self.inner) }
}
Expand Down
16 changes: 8 additions & 8 deletions crates/krb5_rs/src/credentials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ use krb5_sys::{

use crate::{Context, Keytab, Krb5Error, Principal, Result};

pub struct Credentials<'a> {
pub(crate) context: &'a Context,
pub struct Credentials<'context> {
pub(crate) context: &'context Context,
pub(crate) inner: krb5_creds,
}

impl<'a> Credentials<'a> {
impl<'context> Credentials<'context> {
pub fn get_init_credentials_password(
context: &'a Context,
context: &'context Context,
principal: &Principal,
password: &str,
) -> Result<Credentials<'a>> {
) -> Result<Credentials<'context>> {
let mut credentials: MaybeUninit<krb5_creds> = MaybeUninit::uninit();
let password = CString::new(password).map_err(|_| Krb5Error::StringConversionError)?;
let error_code = unsafe {
Expand All @@ -41,10 +41,10 @@ impl<'a> Credentials<'a> {
}

pub fn get_init_credentials_keytab(
context: &'a Context,
context: &'context Context,
principal: &Principal,
keytab: &Keytab,
) -> Result<Credentials<'a>> {
) -> Result<Credentials<'context>> {
let mut credentials: MaybeUninit<krb5_creds> = MaybeUninit::uninit();
let error_code = unsafe {
krb5_get_init_creds_keytab(
Expand All @@ -66,7 +66,7 @@ impl<'a> Credentials<'a> {
}
}

impl<'a> Drop for Credentials<'a> {
impl<'context> Drop for Credentials<'context> {
fn drop(&mut self) {
if !self.context.inner.is_null() {
unsafe { krb5_free_cred_contents(self.context.inner, &mut self.inner) }
Expand Down
18 changes: 5 additions & 13 deletions crates/krb5_rs/src/gssapi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use krb5_sys::{
gss_release_name, OM_uint32, GSS_C_NO_CHANNEL_BINDINGS, GSS_C_NO_CONTEXT, GSS_C_NO_CREDENTIAL,
};

const MAX_AD_TOKEN_SIZE_BEFORE_B64: u64 = 48000;
const MAX_AD_TOKEN_SIZE_BEFORE_B64: usize = 48000;

const GSS_C_REPLAY_FLAG: OM_uint32 = 4;
const GSS_C_SEQUENCE_FLAG: OM_uint32 = 8;
Expand All @@ -33,7 +33,7 @@ impl Drop for Name {
pub fn import_name(principal: &str, input_name_type: &str) -> Result<Name, String> {
let mut minor = 0;
let mut service = gss_buffer_desc {
length: principal.len() as u64,
length: principal.len(),
value: principal.as_ptr() as *mut _,
};
let gss_oid = unsafe {
Expand Down Expand Up @@ -103,8 +103,7 @@ pub fn get_token(target_name: Name) -> Result<Vec<u8>, String> {
"The token returned by GSS is greater than the size allowed by Windows AD",
))
} else {
// try_into().unwrap() won't fail, because output_token.length cannot be higher than MAX_AD_TOKEN_SIZE_BEFORE_B64
let size = output_token.length.try_into().unwrap();
let size = output_token.length;
let mut vec: Vec<u8> = Vec::with_capacity(size);
unsafe {
std::ptr::copy(output_token.value, vec.as_mut_ptr() as *mut _, size);
Expand Down Expand Up @@ -148,15 +147,8 @@ fn convert_gss_error(error_code: OM_uint32, minor: OM_uint32) -> String {
&mut status_string,
)
};
let slice_from_data: &[u8] = unsafe {
slice::from_raw_parts(
status_string.value as *mut _,
status_string
.length
.try_into()
.expect("Failed to convert status_string.length to usize"),
)
};
let slice_from_data: &[u8] =
unsafe { slice::from_raw_parts(status_string.value as *mut _, status_string.length) };
error_msg.extend_from_slice(slice_from_data);
unsafe { gss_release_buffer(&mut min_status, &mut status_string) };

Expand Down
10 changes: 5 additions & 5 deletions crates/krb5_rs/src/keytab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ use krb5_sys::{krb5_keytab, krb5_kt_close, krb5_kt_resolve};

use super::Krb5Error;

pub struct Keytab<'a> {
pub(crate) context: &'a Context,
pub struct Keytab<'context> {
pub(crate) context: &'context Context,
pub(crate) inner: krb5_keytab,
}

impl<'a> Keytab<'a> {
pub fn resolve(context: &'a Context, name: &str) -> Result<Keytab<'a>> {
impl<'context> Keytab<'context> {
pub fn resolve(context: &'context Context, name: &str) -> Result<Keytab<'context>> {
let mut keytab: MaybeUninit<krb5_keytab> = MaybeUninit::uninit();
let name = CString::new(name).map_err(|_| Krb5Error::StringConversionError)?;
let error_code =
Expand All @@ -25,7 +25,7 @@ impl<'a> Keytab<'a> {
}
}

impl<'a> Drop for Keytab<'a> {
impl<'context> Drop for Keytab<'context> {
fn drop(&mut self) {
if !self.context.inner.is_null() && !self.inner.is_null() {
unsafe { krb5_kt_close(self.context.inner, self.inner) };
Expand Down
14 changes: 9 additions & 5 deletions crates/krb5_rs/src/principal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ use std::{mem::MaybeUninit, os::raw::c_uint};
use crate::{Context, Krb5Error, Result};
use krb5_sys::{krb5_build_principal_ext, krb5_free_principal, krb5_principal};

pub struct Principal<'a> {
pub(crate) context: &'a Context,
pub struct Principal<'context> {
pub(crate) context: &'context Context,
pub(crate) inner: krb5_principal,
}

impl<'a> Principal<'a> {
pub fn build_principal(context: &'a Context, realm: &str, user: &str) -> Result<Principal<'a>> {
impl<'context> Principal<'context> {
pub fn build_principal(
context: &'context Context,
realm: &str,
user: &str,
) -> Result<Principal<'context>> {
let mut krb5_principal: MaybeUninit<krb5_principal> = MaybeUninit::uninit();
let sp: Vec<&str> = user.split('/').collect();
let error_code = match sp.len() {
Expand Down Expand Up @@ -52,7 +56,7 @@ impl<'a> Principal<'a> {
}
}

impl<'a> Drop for Principal<'a> {
impl<'context> Drop for Principal<'context> {
fn drop(&mut self) {
if !self.context.inner.is_null() && !self.inner.is_null() {
unsafe { krb5_free_principal(self.context.inner, self.inner) }
Expand Down
4 changes: 2 additions & 2 deletions crates/krb5_sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ links = "krb5"


[build-dependencies]
bindgen = "0.59.2"
bindgen = "0.63.0"
cc = "1.0"

[target.'cfg(not(windows))'.build-dependencies]
pkg-config = "0.3.24"
pkg-config = "0.3"

[target.'cfg(any(windows))'.build-dependencies]
winreg = { version = "0.10" }
6 changes: 6 additions & 0 deletions crates/krb5_sys/src/wrapper.c
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
#include "wrapper.h"

OM_uint32 gss_error(OM_uint32 x) { return GSS_ERROR(x); }

gss_OID gss_c_nt_hostbased_service() { return GSS_C_NT_HOSTBASED_SERVICE; }

gss_OID gss_c_nt_user_name() { return GSS_C_NT_USER_NAME; }
21 changes: 8 additions & 13 deletions crates/krb5_sys/src/wrapper.h
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
#include <krb5.h>
#include <gssapi.h>
#include <gssapi/gssapi_krb5.h>
#include <krb5.h>

#undef GSS_C_NO_CREDENTIAL
const gss_cred_id_t GSS_C_NO_CREDENTIAL = ((gss_cred_id_t) 0);
const gss_cred_id_t GSS_C_NO_CREDENTIAL = ((gss_cred_id_t)0);

#undef GSS_C_NO_CHANNEL_BINDINGS
const gss_channel_bindings_t GSS_C_NO_CHANNEL_BINDINGS = ((gss_channel_bindings_t) 0);
const gss_channel_bindings_t GSS_C_NO_CHANNEL_BINDINGS =
((gss_channel_bindings_t)0);

#undef GSS_C_NO_CONTEXT
const gss_ctx_id_t GSS_C_NO_CONTEXT = ((gss_ctx_id_t) 0);
const gss_ctx_id_t GSS_C_NO_CONTEXT = ((gss_ctx_id_t)0);

OM_uint32 gss_error(OM_uint32 x) {
return GSS_ERROR(x);
}
OM_uint32 gss_error(OM_uint32 x);

gss_OID gss_c_nt_hostbased_service() {
return GSS_C_NT_HOSTBASED_SERVICE;
}
gss_OID gss_c_nt_hostbased_service();

gss_OID gss_c_nt_user_name() {
return GSS_C_NT_USER_NAME;
}
gss_OID gss_c_nt_user_name();
18 changes: 9 additions & 9 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@
/* auto-generated by NAPI-RS */

export interface KdestroyParameters {
ccname?: string | undefined | null
ccname?: string
}
export interface KinitParameters {
principal: string
password?: string | undefined | null
keytab?: string | undefined | null
realm?: string | undefined | null
ccname?: string | undefined | null
password?: string
keytab?: string
realm?: string
ccname?: string
}
export interface GenerateSpnegoTokenParameters {
service_principal?: string | undefined | null
service_fqdn?: string | undefined | null
hostbased_service?: string | undefined | null
ccname?: string | undefined | null
service_principal?: string
service_fqdn?: string
hostbased_service?: string
ccname?: string
}
export function kinit(input: KinitParameters): Promise<string>
export function kdestroy(input: KdestroyParameters): Promise<void>
Expand Down
4 changes: 1 addition & 3 deletions src/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ module.exports =
.catch (err) -> callback err
return


spnego: (options, callback) ->
return krb5.spnego options unless callback
krb5.spnego options
Expand All @@ -22,9 +21,8 @@ module.exports =
if typeof options is 'function'
callback = options
options = {}

krb5.kdestroy options
.then () -> callback()
.catch (err) -> callback err
return

0 comments on commit b5d24eb

Please sign in to comment.