public static extern IntPtr krb5_get_init_creds_keytab(IntPtr ctx, ref KRB5API.krb5_creds creds, KRB5API.krb5_principal client_principal, IntPtr keytab, int start_time, [MarshalAs(UnmanagedType.LPWStr)] string in_tkt_service, IntPtr options);
public static extern IntPtr krb5_cc_store_cred(IntPtr ctx, IntPtr cc, KRB5API.krb5_creds creds);
public static int Krb5GetTGTFromKeytab(string userName, string passWord, string krb5CachePath, out UInt32 ticketExpiryTime) { int ret = 0; IntPtr iRet = IntPtr.Zero; KRB5API.krb5_ccache stcc = new KRB5API.krb5_ccache(); KRB5API.krb5_context stCtx = new KRB5API.krb5_context(); ticketExpiryTime = 0; try { stCtx.db_context = IntPtr.Zero; stCtx.default_realm = IntPtr.Zero; stCtx.in_tkt_ktypes = IntPtr.Zero; stCtx.locate_fptrs = IntPtr.Zero; stCtx.preauth_context = IntPtr.Zero; stCtx.profile = IntPtr.Zero; stCtx.prompt_types = IntPtr.Zero; stCtx.ser_ctx = IntPtr.Zero; stCtx.tgs_ktypes = IntPtr.Zero; stCtx.vtbl = IntPtr.Zero; IntPtr ctx = Marshal.AllocHGlobal(Marshal.SizeOf(stCtx)); Marshal.StructureToPtr(stCtx, ctx, false); uint uret = PrivateKrb5Api.krb5_init_context(out ctx); Logger.Log(string.Format("Krb5Wrapper.Krb5GetTGTFromKeytab.krb5_init_context(ctx = {0},iRet= {1}", ctx.ToString(), iRet.ToString()), Logger.Krb5LogLevel); if (uret != 0) { //ret = Convert.ToUInt32(iRet.ToInt32()); Logger.Log(string.Format("krb5_init_context returns non-zero ret value iRet= {0}", iRet.ToString()), Logger.Krb5LogLevel); } //Default initialization for KRB5API.krb5_ccache stcc.data = IntPtr.Zero; stcc.ops = IntPtr.Zero; IntPtr cc = Marshal.AllocHGlobal(Marshal.SizeOf(stcc)); Marshal.StructureToPtr(stcc, cc, false); if (!String.IsNullOrEmpty(krb5CachePath)) { /* use krb5_cc_resolve to get an alternate cache */ iRet = PrivateKrb5Api.krb5_cc_resolve(ctx, krb5CachePath, ref cc); Logger.Log(string.Format("Krb5Wrapper.Krb5GetTGTFromKeytab.krb5_cc_resolve(ctx = {0},krb5CachePath= {1}, cc={2}", ctx.ToString(), krb5CachePath, cc.ToString()), Logger.Krb5LogLevel); if (iRet != IntPtr.Zero) { //ret = Convert.ToUInt32(iRet.ToInt32()); Logger.Log(string.Format("krb5_cc_resolve returns non-zero ret value iRet= {0}", iRet.ToString()), Logger.Krb5LogLevel); } } else { /* use krb5_cc_resolve to get an alternate cache */ iRet = PrivateKrb5Api.krb5_cc_default(ctx, ref cc); Logger.Log(string.Format("Krb5Wrapper.Krb5GetTGTFromKeytab.krb5_cc_default(ctx = {0}, cc={1}", ctx.ToString(), cc.ToString()), Logger.Krb5LogLevel); if (iRet != IntPtr.Zero) { //ret = Convert.ToUInt32(iRet.ToInt32()); Logger.Log(string.Format("krb5_cc_resolve returns non-zero ret value iRet= {0}", iRet.ToString()), Logger.Krb5LogLevel); } } KRB5API.krb5_keytab stKeytab = new KRB5API.krb5_keytab(); stKeytab.data = IntPtr.Zero; stKeytab.ops = IntPtr.Zero; IntPtr keytab = Marshal.AllocHGlobal(Marshal.SizeOf(stKeytab)); Marshal.StructureToPtr(stKeytab, keytab, false); iRet = PrivateKrb5Api.krb5_kt_default(ctx, out keytab); Logger.Log(string.Format("Krb5Wrapper.Krb5GetTGTFromKeytab.krb5_kt_default(ctx = {0}, out keytab={1}", ctx.ToString(), keytab.ToInt32().ToString()), Logger.Krb5LogLevel); if (iRet != IntPtr.Zero) { Logger.Log(string.Format("krb5_kt_default( keytab={0}, iRet={1}", keytab.ToString(), iRet.ToString())); } KRB5API.krb5_principal cli_principal = new KRB5API.krb5_principal(); cli_principal.data = IntPtr.Zero; iRet = PrivateKrb5Api.krb5_parse_name(ctx, userName, out cli_principal); Logger.Log(string.Format("Krb5Wrapper.Krb5GetTGTFromKeytab.krb5_parse_name(ctx = {0},userName ={1}, client_principal={2}, iRet= {3}", ctx.ToString(), userName, Marshal.PtrToStringAuto(cli_principal.realm.data), iRet.ToString()), Logger.Krb5LogLevel); if (iRet != IntPtr.Zero) { //ret = Convert.ToUInt32(iRet.ToInt32()); Logger.Log(string.Format("krb5_parse_name returns non-zero ret value iRet= {0}", iRet.ToString()), Logger.Krb5LogLevel); } KRB5API.krb5_creds in_Stcreds = new KRB5API.krb5_creds(); in_Stcreds.client = cli_principal; in_Stcreds.addresses = IntPtr.Zero; in_Stcreds.authdata = IntPtr.Zero; iRet = PrivateKrb5Api.krb5_get_init_creds_keytab( ctx, ref in_Stcreds, cli_principal, keytab, 0, null, IntPtr.Zero ); Logger.Log(string.Format("krb5_get_init_creds_keytab( in_Stcreds={0}, iRet={1}", in_Stcreds.ToString(), iRet.ToString())); if (iRet != IntPtr.Zero) { Logger.Log(string.Format("krb5_get_init_creds_keytab( in_Stcreds={0}, iRet={1}", in_Stcreds.ToString(), iRet.ToString())); } iRet = PrivateKrb5Api.krb5_cc_store_cred( ctx, cc, in_Stcreds ); Logger.Log(string.Format("krb5_cc_store_cred( in_Stcreds={0}, iRet={1}", in_Stcreds.ToString(), iRet.ToString())); if (iRet != IntPtr.Zero) { Logger.Log(string.Format("krb5_cc_store_cred( in_Stcreds={0}, iRet={1}", in_Stcreds.ToString(), iRet.ToString())); } if (in_Stcreds != null) { //Marshal.PtrToStructure(out_creds, out_Stcreds); Logger.Log("Marshal.PtrToStructure(stCreds.times, times) is success", Logger.Krb5LogLevel); if (in_Stcreds != null && in_Stcreds.times != null) { ticketExpiryTime = Convert.ToUInt32(in_Stcreds.times.endtime); Logger.Log("times.authtime is " + in_Stcreds.times.authtime.ToString(), Logger.Krb5LogLevel); Logger.Log("times.endtime is " + in_Stcreds.times.endtime.ToString(), Logger.Krb5LogLevel); Logger.Log("times.renew_till is " + in_Stcreds.times.renew_till.ToString(), Logger.Krb5LogLevel); Logger.Log("times.starttime is " + in_Stcreds.times.starttime.ToString(), Logger.Krb5LogLevel); Logger.Log("ticketExpiryTime is " + ticketExpiryTime, Logger.Krb5LogLevel); } } if (ctx != IntPtr.Zero) { if (cc != IntPtr.Zero) { PrivateKrb5Api.krb5_cc_close(ctx, cc); } PrivateKrb5Api.krb5_free_context(ctx); } } catch (Exception ex) { Logger.LogException("Krb5Wrapper.Krb5GetTGTFromKeytab", ex); } return(ret); }
public static int Krb5GetTGTFromKeytab(string userName, string passWord, string krb5CachePath, out UInt32 ticketExpiryTime) { int ret = 0; IntPtr iRet = IntPtr.Zero; KRB5API.krb5_ccache stcc = new KRB5API.krb5_ccache(); KRB5API.krb5_context stCtx = new KRB5API.krb5_context(); ticketExpiryTime = 0; try { stCtx.db_context = IntPtr.Zero; stCtx.default_realm = IntPtr.Zero; stCtx.in_tkt_ktypes = IntPtr.Zero; stCtx.locate_fptrs = IntPtr.Zero; stCtx.preauth_context = IntPtr.Zero; stCtx.profile = IntPtr.Zero; stCtx.prompt_types = IntPtr.Zero; stCtx.ser_ctx = IntPtr.Zero; stCtx.tgs_ktypes = IntPtr.Zero; stCtx.vtbl = IntPtr.Zero; IntPtr ctx = Marshal.AllocHGlobal(Marshal.SizeOf(stCtx)); Marshal.StructureToPtr(stCtx, ctx, false); uint uret = PrivateKrb5Api.krb5_init_context(out ctx); Logger.Log(string.Format("Krb5Wrapper.Krb5GetTGTFromKeytab.krb5_init_context(ctx = {0},iRet= {1}", ctx.ToString(), iRet.ToString()), Logger.Krb5LogLevel); if (uret != 0) { //ret = Convert.ToUInt32(iRet.ToInt32()); Logger.Log(string.Format("krb5_init_context returns non-zero ret value iRet= {0}", iRet.ToString()), Logger.Krb5LogLevel); } //Default initialization for KRB5API.krb5_ccache stcc.data = IntPtr.Zero; stcc.ops = IntPtr.Zero; IntPtr cc = Marshal.AllocHGlobal(Marshal.SizeOf(stcc)); Marshal.StructureToPtr(stcc, cc, false); if (!String.IsNullOrEmpty(krb5CachePath)) { /* use krb5_cc_resolve to get an alternate cache */ iRet = PrivateKrb5Api.krb5_cc_resolve(ctx, krb5CachePath, ref cc); Logger.Log(string.Format("Krb5Wrapper.Krb5GetTGTFromKeytab.krb5_cc_resolve(ctx = {0},krb5CachePath= {1}, cc={2}", ctx.ToString(), krb5CachePath, cc.ToString()), Logger.Krb5LogLevel); if (iRet != IntPtr.Zero) { //ret = Convert.ToUInt32(iRet.ToInt32()); Logger.Log(string.Format("krb5_cc_resolve returns non-zero ret value iRet= {0}", iRet.ToString()), Logger.Krb5LogLevel); } } else { /* use krb5_cc_resolve to get an alternate cache */ iRet = PrivateKrb5Api.krb5_cc_default(ctx, ref cc); Logger.Log(string.Format("Krb5Wrapper.Krb5GetTGTFromKeytab.krb5_cc_default(ctx = {0}, cc={1}", ctx.ToString(), cc.ToString()), Logger.Krb5LogLevel); if (iRet != IntPtr.Zero) { //ret = Convert.ToUInt32(iRet.ToInt32()); Logger.Log(string.Format("krb5_cc_resolve returns non-zero ret value iRet= {0}", iRet.ToString()), Logger.Krb5LogLevel); } } KRB5API.krb5_keytab stKeytab = new KRB5API.krb5_keytab(); stKeytab.data = IntPtr.Zero; stKeytab.ops = IntPtr.Zero; IntPtr keytab = Marshal.AllocHGlobal(Marshal.SizeOf(stKeytab)); Marshal.StructureToPtr(stKeytab, keytab, false); iRet = PrivateKrb5Api.krb5_kt_default(ctx, out keytab); Logger.Log(string.Format("Krb5Wrapper.Krb5GetTGTFromKeytab.krb5_kt_default(ctx = {0}, out keytab={1}", ctx.ToString(), keytab.ToInt32().ToString()), Logger.Krb5LogLevel); if (iRet != IntPtr.Zero) { Logger.Log(string.Format("krb5_kt_default( keytab={0}, iRet={1}", keytab.ToString(), iRet.ToString())); } KRB5API.krb5_principal cli_principal = new KRB5API.krb5_principal(); cli_principal.data = IntPtr.Zero; iRet = PrivateKrb5Api.krb5_parse_name(ctx, userName, out cli_principal); Logger.Log(string.Format("Krb5Wrapper.Krb5GetTGTFromKeytab.krb5_parse_name(ctx = {0},userName ={1}, client_principal={2}, iRet= {3}", ctx.ToString(), userName, Marshal.PtrToStringAuto(cli_principal.realm.data), iRet.ToString()), Logger.Krb5LogLevel); if (iRet != IntPtr.Zero) { //ret = Convert.ToUInt32(iRet.ToInt32()); Logger.Log(string.Format("krb5_parse_name returns non-zero ret value iRet= {0}", iRet.ToString()), Logger.Krb5LogLevel); } KRB5API.krb5_creds in_Stcreds = new KRB5API.krb5_creds(); in_Stcreds.client = cli_principal; in_Stcreds.addresses = IntPtr.Zero; in_Stcreds.authdata = IntPtr.Zero; iRet = PrivateKrb5Api.krb5_get_init_creds_keytab( ctx, ref in_Stcreds, cli_principal, keytab, 0, null, IntPtr.Zero ); Logger.Log(string.Format("krb5_get_init_creds_keytab( in_Stcreds={0}, iRet={1}", in_Stcreds.ToString(), iRet.ToString())); if (iRet != IntPtr.Zero) { Logger.Log(string.Format("krb5_get_init_creds_keytab( in_Stcreds={0}, iRet={1}", in_Stcreds.ToString(), iRet.ToString())); } iRet = PrivateKrb5Api.krb5_cc_store_cred( ctx, cc, in_Stcreds ); Logger.Log(string.Format("krb5_cc_store_cred( in_Stcreds={0}, iRet={1}", in_Stcreds.ToString(), iRet.ToString())); if (iRet != IntPtr.Zero) { Logger.Log(string.Format("krb5_cc_store_cred( in_Stcreds={0}, iRet={1}", in_Stcreds.ToString(), iRet.ToString())); } if (in_Stcreds != null) { //Marshal.PtrToStructure(out_creds, out_Stcreds); Logger.Log("Marshal.PtrToStructure(stCreds.times, times) is success", Logger.Krb5LogLevel); if (in_Stcreds != null && in_Stcreds.times != null) { ticketExpiryTime = Convert.ToUInt32(in_Stcreds.times.endtime); Logger.Log("times.authtime is " + in_Stcreds.times.authtime.ToString(), Logger.Krb5LogLevel); Logger.Log("times.endtime is " + in_Stcreds.times.endtime.ToString(), Logger.Krb5LogLevel); Logger.Log("times.renew_till is " + in_Stcreds.times.renew_till.ToString(), Logger.Krb5LogLevel); Logger.Log("times.starttime is " + in_Stcreds.times.starttime.ToString(), Logger.Krb5LogLevel); Logger.Log("ticketExpiryTime is " + ticketExpiryTime, Logger.Krb5LogLevel); } } if (ctx != IntPtr.Zero) { if (cc != IntPtr.Zero) { PrivateKrb5Api.krb5_cc_close(ctx, cc); } PrivateKrb5Api.krb5_free_context(ctx); } } catch (Exception ex) { Logger.LogException("Krb5Wrapper.Krb5GetTGTFromKeytab", ex); } return ret; }