2006-11-13 06:20:43 +01:00
|
|
|
/***********************************************************************
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Novell, Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; version 2.1
|
|
|
|
* of the License.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, Novell, Inc.
|
|
|
|
*
|
|
|
|
* To contact Novell about this file by physical or electronic mail,
|
|
|
|
* you may find current contact information at www.novell.com.
|
|
|
|
*
|
|
|
|
* Author: Jim Norman
|
|
|
|
*
|
|
|
|
***********************************************************************/
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Text;
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
|
|
namespace Novell.Casa.Client.Auth
|
|
|
|
{
|
2006-12-04 06:03:49 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Summary description for Class1.
|
|
|
|
/// </summary>
|
|
|
|
public class Authtoken
|
|
|
|
{
|
|
|
|
private const string AUTH_LIBRARY = "casa_authtoken";
|
|
|
|
private const int BUFFER_OVERFLOW = 6;
|
|
|
|
|
|
|
|
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
|
|
|
|
private struct LUID
|
|
|
|
{
|
|
|
|
public int luidLow;
|
|
|
|
public int luidHigh;
|
|
|
|
}
|
|
|
|
|
|
|
|
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
|
|
|
|
public class SSCS_EXT_T
|
|
|
|
{
|
|
|
|
public int extID = 0; // defined to identify the extension
|
|
|
|
public int version = 0; // defined as the version of the specified extension
|
|
|
|
public IntPtr ext; // points to the actual extension
|
|
|
|
} ;
|
|
|
|
|
|
|
|
[DllImport(AUTH_LIBRARY, CharSet=CharSet.None) ]
|
|
|
|
private static extern int ObtainAuthToken
|
|
|
|
(
|
|
|
|
[In] byte[] baService,
|
|
|
|
[In] byte[] baHost,
|
|
|
|
[In, Out] byte[] baToken,
|
|
|
|
[In, Out] ref int iTokenLength
|
|
|
|
);
|
|
|
|
|
2009-10-09 10:46:35 +02:00
|
|
|
[DllImport(AUTH_LIBRARY, EntryPoint="SetATSHostList", CharSet=CharSet.None) ]
|
|
|
|
private static extern int _SetATSHostList
|
|
|
|
(
|
|
|
|
[In] String[] ATSHostList
|
|
|
|
);
|
|
|
|
|
2006-12-04 06:03:49 +01:00
|
|
|
[DllImport(AUTH_LIBRARY, CharSet=CharSet.None) ]
|
|
|
|
private static extern int ObtainAuthTokenEx
|
|
|
|
(
|
|
|
|
[In] byte[] baService,
|
|
|
|
[In] byte[] baHost,
|
|
|
|
[In, Out] byte[] baToken,
|
|
|
|
[In, Out] ref int iTokenLength,
|
|
|
|
[In] SSCS_EXT_T ext
|
|
|
|
);
|
|
|
|
|
2007-04-13 21:21:40 +02:00
|
|
|
[DllImport(AUTH_LIBRARY, CharSet = CharSet.None)]
|
|
|
|
private static extern int CleanUpAuthTokenCacheEx
|
|
|
|
(
|
|
|
|
[In] SSCS_EXT_T ext
|
|
|
|
);
|
|
|
|
|
2006-12-04 06:03:49 +01:00
|
|
|
|
|
|
|
public Authtoken()
|
|
|
|
{
|
|
|
|
}
|
2006-11-13 06:20:43 +01:00
|
|
|
|
2006-11-29 11:54:55 +01:00
|
|
|
public static string ObtainAuthTokenAsString(string sService, string sHost)
|
|
|
|
{
|
|
|
|
byte[] baToken;
|
|
|
|
baToken = ObtainAuthToken(sService, sHost, null);
|
|
|
|
if (baToken != null)
|
|
|
|
{
|
|
|
|
return (Encoding.ASCII.GetString(baToken));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2006-11-13 06:20:43 +01:00
|
|
|
|
2006-12-04 06:03:49 +01:00
|
|
|
public static byte[] ObtainAuthToken(string sService, string sHost)
|
|
|
|
{
|
|
|
|
return ObtainAuthToken(sService, sHost, null);
|
|
|
|
}
|
|
|
|
|
2007-04-13 21:21:40 +02:00
|
|
|
public static byte[] ObtainAuthToken(string sService, string sHost, WinLuid luid)
|
2006-12-04 06:03:49 +01:00
|
|
|
{
|
|
|
|
int rcode = 0;
|
|
|
|
byte[] baService = null;
|
|
|
|
byte[] baHost = null;
|
|
|
|
int bufferSize = 0;
|
|
|
|
bool bLuidPassedIn = false;
|
|
|
|
|
|
|
|
byte[] baToken = new byte[bufferSize];
|
|
|
|
|
|
|
|
// convert service to ascii byte array
|
|
|
|
if (sService != null)
|
|
|
|
{
|
|
|
|
baService = Encoding.ASCII.GetBytes(sService);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new Exception("Invalid parameter");
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert host to ascii byte array
|
|
|
|
if (sHost != null)
|
|
|
|
{
|
|
|
|
baHost = Encoding.ASCII.GetBytes(sHost);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new Exception("Invalid parameter");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SSCS_EXT_T ext = new SSCS_EXT_T();
|
|
|
|
LUID sluid;
|
2006-12-05 12:23:09 +01:00
|
|
|
if ((luid != null) &&
|
|
|
|
((luid.GetHighPart() != 0) || (luid.GetLowPart() != 0)))
|
2006-12-04 06:03:49 +01:00
|
|
|
{
|
|
|
|
// allocate a structure to marshal
|
|
|
|
sluid = new LUID();
|
|
|
|
sluid.luidHigh = luid.GetHighPart();
|
|
|
|
sluid.luidLow = luid.GetLowPart();
|
|
|
|
|
|
|
|
ext.extID = 1;
|
|
|
|
ext.version = 1;
|
|
|
|
ext.ext = Marshal.AllocHGlobal(Marshal.SizeOf(sluid));
|
|
|
|
|
|
|
|
Marshal.StructureToPtr(sluid, ext.ext, false);
|
|
|
|
bLuidPassedIn = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// call with buffersize of 0. This way we determine the exact size.
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (bLuidPassedIn)
|
|
|
|
{
|
|
|
|
rcode = ObtainAuthTokenEx(baService, baHost, baToken, ref bufferSize, ext);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rcode = ObtainAuthToken(baService, baHost, baToken, ref bufferSize);
|
|
|
|
}
|
|
|
|
|
2006-12-05 12:23:09 +01:00
|
|
|
if ((rcode & 0xFFFF) == BUFFER_OVERFLOW)
|
2006-12-04 06:03:49 +01:00
|
|
|
{
|
|
|
|
// now allocate the proper size
|
|
|
|
baToken = new byte[bufferSize];
|
2006-11-27 07:31:39 +01:00
|
|
|
if (bLuidPassedIn)
|
|
|
|
{
|
|
|
|
rcode = ObtainAuthTokenEx(baService, baHost, baToken, ref bufferSize, ext);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rcode = ObtainAuthToken(baService, baHost, baToken, ref bufferSize);
|
|
|
|
}
|
2006-12-04 06:03:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
LogMessage(e.ToString());
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ext.ext != IntPtr.Zero)
|
|
|
|
Marshal.FreeHGlobal(ext.ext);
|
|
|
|
|
|
|
|
if (rcode != 0)
|
|
|
|
{
|
|
|
|
throw new Exception(rcode.ToString());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return baToken;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-09 10:46:35 +02:00
|
|
|
public static int SetATSHostList(string[] ATSHostList)
|
|
|
|
{
|
|
|
|
int i, numHosts;
|
|
|
|
string[] hostList;
|
|
|
|
|
|
|
|
/* Null termination */
|
|
|
|
numHosts = ATSHostList.Length;
|
|
|
|
hostList = new string[numHosts + 1];
|
|
|
|
for (i = 0; i < numHosts; i++) {
|
|
|
|
hostList[i] = ATSHostList[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
return _SetATSHostList(hostList);
|
|
|
|
}
|
|
|
|
|
2007-04-13 21:21:40 +02:00
|
|
|
public static void CleanUpAuthTokenCache(WinLuid luid)
|
|
|
|
{
|
|
|
|
SSCS_EXT_T ext = new SSCS_EXT_T();
|
|
|
|
LUID sluid;
|
|
|
|
bool bLuidPassedIn = false;
|
|
|
|
|
|
|
|
if ((luid != null) &&
|
|
|
|
((luid.GetHighPart() != 0) || (luid.GetLowPart() != 0)))
|
|
|
|
{
|
|
|
|
// allocate a structure to marshal
|
|
|
|
sluid = new LUID();
|
|
|
|
sluid.luidHigh = luid.GetHighPart();
|
|
|
|
sluid.luidLow = luid.GetLowPart();
|
|
|
|
|
|
|
|
ext.extID = 1;
|
|
|
|
ext.version = 1;
|
|
|
|
ext.ext = Marshal.AllocHGlobal(Marshal.SizeOf(sluid));
|
|
|
|
|
|
|
|
Marshal.StructureToPtr(sluid, ext.ext, false);
|
|
|
|
bLuidPassedIn = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do the call
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (bLuidPassedIn)
|
|
|
|
{
|
|
|
|
CleanUpAuthTokenCacheEx(ext);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CleanUpAuthTokenCacheEx(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
LogMessage(e.ToString());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ext.ext != IntPtr.Zero)
|
|
|
|
Marshal.FreeHGlobal(ext.ext);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void CleanUpAuthTokenCache()
|
|
|
|
{
|
|
|
|
CleanUpAuthTokenCache(null);
|
|
|
|
}
|
|
|
|
|
2006-12-04 06:03:49 +01:00
|
|
|
private static void LogMessage(string sMessage)
|
|
|
|
{
|
|
|
|
System.Diagnostics.Trace.WriteLine("(C#)AuthToken: " + sMessage);
|
|
|
|
}
|
|
|
|
}
|
2006-11-13 06:20:43 +01:00
|
|
|
}
|