2006-11-27 14:05:54 +01:00
|
|
|
/* cyglsa.c: LSA authentication module for Cygwin
|
|
|
|
|
|
|
|
Written by Corinna Vinschen <corinna@vinschen.de>
|
|
|
|
|
|
|
|
This file is part of Cygwin.
|
|
|
|
|
|
|
|
This software is a copyrighted work licensed under the terms of the
|
|
|
|
Cygwin license. Please consult the file "CYGWIN_LICENSE" for details. */
|
|
|
|
|
|
|
|
#define WINVER 0x0600
|
|
|
|
#include <ntstatus.h>
|
|
|
|
#include <windows.h>
|
|
|
|
#include <wininet.h>
|
2010-01-29 20:50:15 +01:00
|
|
|
#include <lmcons.h>
|
2008-07-11 12:00:38 +02:00
|
|
|
#include <iptypes.h>
|
2006-11-27 14:05:54 +01:00
|
|
|
#include <ntsecapi.h>
|
|
|
|
#include "../cygwin/cyglsa.h"
|
|
|
|
#include "../cygwin/include/cygwin/version.h"
|
|
|
|
|
|
|
|
static PLSA_SECPKG_FUNCS funcs;
|
|
|
|
|
|
|
|
BOOL APIENTRY
|
|
|
|
DllMain (HINSTANCE inst, DWORD reason, LPVOID res)
|
|
|
|
{
|
|
|
|
switch (reason)
|
|
|
|
{
|
|
|
|
case DLL_PROCESS_ATTACH:
|
|
|
|
case DLL_THREAD_ATTACH:
|
|
|
|
case DLL_THREAD_DETACH:
|
|
|
|
case DLL_PROCESS_DETACH:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2011-03-31 16:19:35 +02:00
|
|
|
/* Declare NTDLL functions here to avoid problems with different
|
|
|
|
header file layout in different compiler environments. */
|
|
|
|
#ifndef NT_SUCCESS
|
|
|
|
#define NT_SUCCESS(s) ((s) >= 0)
|
|
|
|
#endif
|
2011-05-10 12:06:51 +02:00
|
|
|
/* These standard POSIX functions are implemented in NTDLL and exported.
|
|
|
|
There's just no header to define them and using wchar.h from mingw
|
|
|
|
or Cygwin seems wrong somehow. */
|
|
|
|
wchar_t *__cdecl wcscpy (wchar_t *, const wchar_t *);
|
|
|
|
size_t __cdecl wcslen (const wchar_t *);
|
2011-03-31 16:19:35 +02:00
|
|
|
|
2008-07-10 20:05:03 +02:00
|
|
|
#ifndef RtlInitEmptyUnicodeString
|
2008-07-13 15:14:41 +02:00
|
|
|
__inline VOID NTAPI
|
|
|
|
RtlInitEmptyUnicodeString(PUNICODE_STRING dest, PCWSTR buf, USHORT len)
|
2008-07-10 20:05:03 +02:00
|
|
|
{
|
|
|
|
dest->Length = 0;
|
|
|
|
dest->MaximumLength = len;
|
|
|
|
dest->Buffer = (PWSTR) buf;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-11-27 14:05:54 +01:00
|
|
|
static PUNICODE_STRING
|
2008-07-13 15:14:41 +02:00
|
|
|
uni_alloc (PWCHAR src, USHORT len)
|
2006-11-27 14:05:54 +01:00
|
|
|
{
|
|
|
|
PUNICODE_STRING tgt;
|
|
|
|
|
|
|
|
if (!(tgt = funcs->AllocateLsaHeap (sizeof (UNICODE_STRING))))
|
|
|
|
return NULL;
|
|
|
|
tgt->Length = len * sizeof (WCHAR);
|
|
|
|
tgt->MaximumLength = tgt->Length + sizeof (WCHAR);
|
|
|
|
if (!(tgt->Buffer = funcs->AllocateLsaHeap (tgt->MaximumLength)))
|
|
|
|
{
|
|
|
|
funcs->FreeLsaHeap (tgt);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
wcscpy (tgt->Buffer, src);
|
|
|
|
return tgt;
|
|
|
|
}
|
|
|
|
|
2008-07-10 20:05:03 +02:00
|
|
|
/* No, I don't want to include stdio.h so I take what ntdll offers. */
|
|
|
|
extern int _vsnprintf (char *, size_t, const char *, va_list);
|
2006-11-27 14:05:54 +01:00
|
|
|
|
2008-07-10 20:05:03 +02:00
|
|
|
static HANDLE fh = INVALID_HANDLE_VALUE;
|
|
|
|
|
|
|
|
static int
|
2008-10-13 01:53:26 +02:00
|
|
|
cyglsa_printf (const char *format, ...)
|
2006-11-27 14:05:54 +01:00
|
|
|
{
|
2008-07-10 20:05:03 +02:00
|
|
|
char buf[256];
|
2006-11-27 14:05:54 +01:00
|
|
|
DWORD wr;
|
2008-07-10 20:05:03 +02:00
|
|
|
int ret;
|
2008-07-13 15:14:41 +02:00
|
|
|
va_list ap;
|
2006-11-27 14:05:54 +01:00
|
|
|
|
2008-07-10 20:05:03 +02:00
|
|
|
if (fh == INVALID_HANDLE_VALUE)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
va_start (ap, format);
|
|
|
|
ret = _vsnprintf (buf, 256, format, ap);
|
|
|
|
va_end (ap);
|
|
|
|
if (ret <= 0)
|
|
|
|
return ret;
|
|
|
|
if (ret > 256)
|
|
|
|
ret = 255;
|
|
|
|
buf[255] = '\0';
|
|
|
|
WriteFile (fh, buf, ret, &wr, NULL);
|
|
|
|
return wr;
|
2006-11-27 14:05:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-07-10 20:05:03 +02:00
|
|
|
print_sid (const char *prefix, int idx, PISID sid)
|
2006-11-27 14:05:54 +01:00
|
|
|
{
|
|
|
|
DWORD i;
|
|
|
|
|
2008-10-13 01:53:26 +02:00
|
|
|
cyglsa_printf ("%s", prefix);
|
2006-11-27 14:05:54 +01:00
|
|
|
if (idx >= 0)
|
2008-10-13 01:53:26 +02:00
|
|
|
cyglsa_printf ("[%d] ", idx);
|
|
|
|
cyglsa_printf ("(0x%08x) ", (INT_PTR) sid);
|
2006-11-27 14:05:54 +01:00
|
|
|
if (!sid)
|
2008-10-13 01:53:26 +02:00
|
|
|
cyglsa_printf ("NULL\n");
|
2006-11-27 14:05:54 +01:00
|
|
|
else if (IsBadReadPtr (sid, 8))
|
2008-10-13 01:53:26 +02:00
|
|
|
cyglsa_printf ("INVALID POINTER\n");
|
2012-05-29 14:46:01 +02:00
|
|
|
else if (!IsValidSid ((PSID) sid))
|
2008-10-13 01:53:26 +02:00
|
|
|
cyglsa_printf ("INVALID SID\n");
|
2006-11-27 14:05:54 +01:00
|
|
|
else if (IsBadReadPtr (sid, 8 + sizeof (DWORD) * sid->SubAuthorityCount))
|
2008-10-13 01:53:26 +02:00
|
|
|
cyglsa_printf ("INVALID POINTER SPACE\n");
|
2006-11-27 14:05:54 +01:00
|
|
|
else
|
|
|
|
{
|
2008-10-13 01:53:26 +02:00
|
|
|
cyglsa_printf ("S-%d-%d", sid->Revision, sid->IdentifierAuthority.Value[5]);
|
2006-11-27 14:05:54 +01:00
|
|
|
for (i = 0; i < sid->SubAuthorityCount; ++i)
|
2008-10-13 01:53:26 +02:00
|
|
|
cyglsa_printf ("-%lu", sid->SubAuthority[i]);
|
|
|
|
cyglsa_printf ("\n");
|
2006-11-27 14:05:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-07-10 20:05:03 +02:00
|
|
|
print_groups (PTOKEN_GROUPS grps)
|
2006-11-27 14:05:54 +01:00
|
|
|
{
|
|
|
|
DWORD i;
|
|
|
|
|
2008-10-13 01:53:26 +02:00
|
|
|
cyglsa_printf ("Groups: (0x%08x) ", (INT_PTR) grps);
|
2006-11-27 14:05:54 +01:00
|
|
|
if (!grps)
|
2008-10-13 01:53:26 +02:00
|
|
|
cyglsa_printf ("NULL\n");
|
2006-11-27 14:05:54 +01:00
|
|
|
else if (IsBadReadPtr (grps, sizeof (DWORD)))
|
2008-10-13 01:53:26 +02:00
|
|
|
cyglsa_printf ("INVALID POINTER\n");
|
2006-11-27 14:05:54 +01:00
|
|
|
else if (IsBadReadPtr (grps, sizeof (DWORD) + sizeof (SID_AND_ATTRIBUTES)
|
|
|
|
* grps->GroupCount))
|
2008-10-13 01:53:26 +02:00
|
|
|
cyglsa_printf ("INVALID POINTER SPACE\n");
|
2006-11-27 14:05:54 +01:00
|
|
|
else
|
|
|
|
{
|
2008-10-13 01:53:26 +02:00
|
|
|
cyglsa_printf ("Count: %lu\n", grps->GroupCount);
|
2006-11-27 14:05:54 +01:00
|
|
|
for (i = 0; i < grps->GroupCount; ++i)
|
|
|
|
{
|
2008-10-13 01:53:26 +02:00
|
|
|
cyglsa_printf ("(attr: 0x%lx)", grps->Groups[i].Attributes);
|
2008-07-10 20:05:03 +02:00
|
|
|
print_sid (" ", i, (PISID) grps->Groups[i].Sid);
|
2006-11-27 14:05:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-07-10 20:05:03 +02:00
|
|
|
print_privs (PTOKEN_PRIVILEGES privs)
|
2006-11-27 14:05:54 +01:00
|
|
|
{
|
|
|
|
DWORD i;
|
|
|
|
|
2008-10-13 01:53:26 +02:00
|
|
|
cyglsa_printf ("Privileges: (0x%08x) ", (INT_PTR) privs);
|
2006-11-27 14:05:54 +01:00
|
|
|
if (!privs)
|
2008-10-13 01:53:26 +02:00
|
|
|
cyglsa_printf ("NULL\n");
|
2006-11-27 14:05:54 +01:00
|
|
|
else if (IsBadReadPtr (privs, sizeof (DWORD)))
|
2008-10-13 01:53:26 +02:00
|
|
|
cyglsa_printf ("INVALID POINTER\n");
|
2006-11-27 14:05:54 +01:00
|
|
|
else if (IsBadReadPtr (privs, sizeof (DWORD) + sizeof (LUID_AND_ATTRIBUTES)
|
|
|
|
* privs->PrivilegeCount))
|
2008-10-13 01:53:26 +02:00
|
|
|
cyglsa_printf ("INVALID POINTER SPACE\n");
|
2006-11-27 14:05:54 +01:00
|
|
|
else
|
|
|
|
{
|
2008-10-13 01:53:26 +02:00
|
|
|
cyglsa_printf ("Count: %lu\n", privs->PrivilegeCount);
|
2006-11-27 14:05:54 +01:00
|
|
|
for (i = 0; i < privs->PrivilegeCount; ++i)
|
2008-10-13 01:53:26 +02:00
|
|
|
cyglsa_printf ("Luid: {%ld, %lu} Attributes: 0x%lx\n",
|
2008-07-10 20:05:03 +02:00
|
|
|
privs->Privileges[i].Luid.HighPart,
|
|
|
|
privs->Privileges[i].Luid.LowPart,
|
|
|
|
privs->Privileges[i].Attributes);
|
2006-11-27 14:05:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-07-10 20:05:03 +02:00
|
|
|
print_dacl (PACL dacl)
|
2006-11-27 14:05:54 +01:00
|
|
|
{
|
|
|
|
DWORD i;
|
|
|
|
|
2008-10-13 01:53:26 +02:00
|
|
|
cyglsa_printf ("DefaultDacl: (0x%08x) ", (INT_PTR) dacl);
|
2006-11-27 14:05:54 +01:00
|
|
|
if (!dacl)
|
2008-10-13 01:53:26 +02:00
|
|
|
cyglsa_printf ("NULL\n");
|
2006-11-27 14:05:54 +01:00
|
|
|
else if (IsBadReadPtr (dacl, sizeof (ACL)))
|
2008-10-13 01:53:26 +02:00
|
|
|
cyglsa_printf ("INVALID POINTER\n");
|
2006-11-27 14:05:54 +01:00
|
|
|
else if (IsBadReadPtr (dacl, dacl->AclSize))
|
2008-10-13 01:53:26 +02:00
|
|
|
cyglsa_printf ("INVALID POINTER SPACE\n");
|
2006-11-27 14:05:54 +01:00
|
|
|
else
|
|
|
|
{
|
2008-10-13 01:53:26 +02:00
|
|
|
cyglsa_printf ("Rev: %d, Count: %d\n", dacl->AclRevision, dacl->AceCount);
|
2006-11-27 14:05:54 +01:00
|
|
|
for (i = 0; i < dacl->AceCount; ++i)
|
|
|
|
{
|
2008-07-10 20:05:03 +02:00
|
|
|
PVOID vace;
|
2006-11-27 14:05:54 +01:00
|
|
|
PACCESS_ALLOWED_ACE ace;
|
|
|
|
|
2012-05-29 14:46:01 +02:00
|
|
|
if (!GetAce (dacl, i, &vace))
|
|
|
|
cyglsa_printf ("[%lu] GetAce error %lu\n", i, GetLastError ());
|
2006-11-27 14:05:54 +01:00
|
|
|
else
|
|
|
|
{
|
2008-07-10 20:05:03 +02:00
|
|
|
ace = (PACCESS_ALLOWED_ACE) vace;
|
2008-10-13 01:53:26 +02:00
|
|
|
cyglsa_printf ("Type: %x, Flags: %x, Access: %lx,",
|
2008-07-10 20:05:03 +02:00
|
|
|
ace->Header.AceType, ace->Header.AceFlags, (DWORD) ace->Mask);
|
|
|
|
print_sid (" ", i, (PISID) &ace->SidStart);
|
2006-11-27 14:05:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
print_tokinf (PLSA_TOKEN_INFORMATION_V2 ptok, size_t size,
|
2008-07-10 20:05:03 +02:00
|
|
|
PVOID got_start, PVOID gotinf_start, PVOID gotinf_end)
|
2006-11-27 14:05:54 +01:00
|
|
|
{
|
|
|
|
if (fh == INVALID_HANDLE_VALUE)
|
|
|
|
return;
|
|
|
|
|
2008-10-13 01:53:26 +02:00
|
|
|
cyglsa_printf ("INCOMING: start: 0x%08x infstart: 0x%08x infend: 0x%08x\n",
|
2008-07-10 20:05:03 +02:00
|
|
|
(INT_PTR) got_start, (INT_PTR) gotinf_start,
|
|
|
|
(INT_PTR) gotinf_end);
|
2006-11-27 14:05:54 +01:00
|
|
|
|
2008-10-13 01:53:26 +02:00
|
|
|
cyglsa_printf ("LSA_TOKEN_INFORMATION_V2: 0x%08x - 0x%08x\n",
|
2008-07-10 20:05:03 +02:00
|
|
|
(INT_PTR) ptok, (INT_PTR) ptok + size);
|
2006-11-27 14:05:54 +01:00
|
|
|
|
|
|
|
/* User SID */
|
2008-10-13 01:53:26 +02:00
|
|
|
cyglsa_printf ("User: (attr: 0x%lx)", ptok->User.User.Attributes);
|
2008-07-10 20:05:03 +02:00
|
|
|
print_sid (" ", -1, (PISID) ptok->User.User.Sid);
|
2006-11-27 14:05:54 +01:00
|
|
|
|
|
|
|
/* Groups */
|
2008-07-10 20:05:03 +02:00
|
|
|
print_groups (ptok->Groups);
|
2006-11-27 14:05:54 +01:00
|
|
|
|
|
|
|
/* Primary Group SID */
|
2008-07-10 20:05:03 +02:00
|
|
|
print_sid ("Primary Group: ", -1, (PISID)ptok->PrimaryGroup.PrimaryGroup);
|
2006-11-27 14:05:54 +01:00
|
|
|
|
|
|
|
/* Privileges */
|
2008-07-10 20:05:03 +02:00
|
|
|
print_privs (ptok->Privileges);
|
2006-11-27 14:05:54 +01:00
|
|
|
|
|
|
|
/* Owner */
|
2008-07-10 20:05:03 +02:00
|
|
|
print_sid ("Owner: ", -1, (PISID) ptok->Owner.Owner);
|
2006-11-27 14:05:54 +01:00
|
|
|
|
|
|
|
/* Default DACL */
|
2008-07-10 20:05:03 +02:00
|
|
|
print_dacl (ptok->DefaultDacl.DefaultDacl);
|
2006-11-27 14:05:54 +01:00
|
|
|
|
2008-07-10 20:05:03 +02:00
|
|
|
// CloseHandle (fh);
|
2006-11-27 14:05:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS NTAPI
|
|
|
|
LsaApInitializePackage (ULONG authp_id, PLSA_SECPKG_FUNCS dpt,
|
|
|
|
PLSA_STRING dummy1, PLSA_STRING dummy2,
|
|
|
|
PLSA_STRING *authp_name)
|
|
|
|
{
|
|
|
|
PLSA_STRING name = NULL;
|
|
|
|
|
|
|
|
/* Set global pointer to lsa helper function table. */
|
|
|
|
funcs = dpt;
|
|
|
|
|
|
|
|
/* Allocate and set the name of the authentication package. This is the
|
|
|
|
name which has to be used in LsaLookupAuthenticationPackage. */
|
|
|
|
if (!(name = funcs->AllocateLsaHeap (sizeof *name)))
|
|
|
|
return STATUS_NO_MEMORY;
|
|
|
|
if (!(name->Buffer = funcs->AllocateLsaHeap (sizeof (CYG_LSA_PKGNAME))))
|
|
|
|
{
|
|
|
|
funcs->FreeLsaHeap (name);
|
|
|
|
return STATUS_NO_MEMORY;
|
|
|
|
}
|
|
|
|
name->Length = sizeof (CYG_LSA_PKGNAME) - 1;
|
|
|
|
name->MaximumLength = sizeof (CYG_LSA_PKGNAME);
|
|
|
|
strcpy (name->Buffer, CYG_LSA_PKGNAME);
|
|
|
|
(*authp_name) = name;
|
|
|
|
|
2008-07-10 20:05:03 +02:00
|
|
|
#ifdef DEBUGGING
|
|
|
|
fh = CreateFile ("C:\\cyglsa.dbgout", GENERIC_WRITE,
|
|
|
|
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
|
|
|
|
NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
|
2008-10-13 01:53:26 +02:00
|
|
|
cyglsa_printf ("Initialized\n");
|
2008-07-10 20:05:03 +02:00
|
|
|
#endif /* DEBUGGING */
|
|
|
|
|
2006-11-27 14:05:54 +01:00
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS NTAPI
|
2008-07-10 20:05:03 +02:00
|
|
|
LsaApLogonUserEx (PLSA_CLIENT_REQUEST request, SECURITY_LOGON_TYPE logon_type,
|
|
|
|
PVOID auth, PVOID client_auth_base, ULONG auth_len,
|
|
|
|
PVOID *pbuf, PULONG pbuf_len, PLUID logon_id,
|
|
|
|
PNTSTATUS sub_stat, PLSA_TOKEN_INFORMATION_TYPE tok_type,
|
|
|
|
PVOID *tok, PUNICODE_STRING *account,
|
|
|
|
PUNICODE_STRING *authority, PUNICODE_STRING *machine)
|
2006-11-27 14:05:54 +01:00
|
|
|
{
|
2008-07-11 12:00:38 +02:00
|
|
|
DWORD checksum, i;
|
2006-11-27 14:05:54 +01:00
|
|
|
PDWORD csp, csp_end;
|
|
|
|
NTSTATUS stat;
|
|
|
|
SECPKG_CLIENT_INFO clinf;
|
|
|
|
PLSA_TOKEN_INFORMATION_V2 tokinf;
|
|
|
|
|
|
|
|
cyglsa_t *authinf = (cyglsa_t *) auth;
|
|
|
|
|
|
|
|
/* Check if the caller has the SeTcbPrivilege, otherwise refuse service. */
|
|
|
|
stat = funcs->GetClientInfo (&clinf);
|
|
|
|
if (stat != STATUS_SUCCESS)
|
2008-07-10 20:05:03 +02:00
|
|
|
{
|
2008-10-13 01:53:26 +02:00
|
|
|
cyglsa_printf ("GetClientInfo failed: 0x%08lx\n", stat);
|
2008-07-10 20:05:03 +02:00
|
|
|
return stat;
|
|
|
|
}
|
2006-11-27 14:05:54 +01:00
|
|
|
if (!clinf.HasTcbPrivilege)
|
2008-07-10 20:05:03 +02:00
|
|
|
{
|
2008-10-13 01:53:26 +02:00
|
|
|
cyglsa_printf ("Client has no TCB privilege. Access denied.\n");
|
2008-07-10 20:05:03 +02:00
|
|
|
return STATUS_ACCESS_DENIED;
|
|
|
|
}
|
2006-11-27 14:05:54 +01:00
|
|
|
|
|
|
|
/* Make a couple of validity checks. */
|
|
|
|
if (auth_len < sizeof *authinf
|
|
|
|
|| authinf->magic != CYG_LSA_MAGIC
|
|
|
|
|| !authinf->username[0]
|
|
|
|
|| !authinf->domain[0])
|
2008-07-10 20:05:03 +02:00
|
|
|
{
|
2008-10-13 01:53:26 +02:00
|
|
|
cyglsa_printf ("Invalid authentication parameter.\n");
|
2008-07-10 20:05:03 +02:00
|
|
|
return STATUS_INVALID_PARAMETER;
|
|
|
|
}
|
2010-01-29 20:50:15 +01:00
|
|
|
checksum = CYG_LSA_MAGIC;
|
2006-11-27 14:05:54 +01:00
|
|
|
csp = (PDWORD) &authinf->username;
|
|
|
|
csp_end = (PDWORD) ((PBYTE) authinf + auth_len);
|
|
|
|
while (csp < csp_end)
|
|
|
|
checksum += *csp++;
|
|
|
|
if (authinf->checksum != checksum)
|
2008-07-10 20:05:03 +02:00
|
|
|
{
|
2008-10-13 01:53:26 +02:00
|
|
|
cyglsa_printf ("Invalid checksum.\n");
|
2008-07-10 20:05:03 +02:00
|
|
|
return STATUS_INVALID_PARAMETER_3;
|
|
|
|
}
|
2006-11-27 14:05:54 +01:00
|
|
|
|
|
|
|
/* Set account to username and authority to domain resp. machine name.
|
|
|
|
The name of the logon account name as returned by LookupAccountSid
|
|
|
|
is created from here as "authority\account". */
|
|
|
|
authinf->username[UNLEN] = '\0';
|
2008-07-11 12:00:38 +02:00
|
|
|
authinf->domain[MAX_DOMAIN_NAME_LEN] = '\0';
|
|
|
|
if (account && !(*account = uni_alloc (authinf->username,
|
|
|
|
wcslen (authinf->username))))
|
2008-07-10 20:05:03 +02:00
|
|
|
{
|
2008-10-13 01:53:26 +02:00
|
|
|
cyglsa_printf ("No memory trying to create account.\n");
|
2008-07-10 20:05:03 +02:00
|
|
|
return STATUS_NO_MEMORY;
|
|
|
|
}
|
2008-07-11 12:00:38 +02:00
|
|
|
if (authority && !(*authority = uni_alloc (authinf->domain,
|
|
|
|
wcslen (authinf->domain))))
|
2008-07-10 20:05:03 +02:00
|
|
|
{
|
2008-10-13 01:53:26 +02:00
|
|
|
cyglsa_printf ("No memory trying to create authority.\n");
|
2008-07-10 20:05:03 +02:00
|
|
|
return STATUS_NO_MEMORY;
|
|
|
|
}
|
|
|
|
if (machine)
|
|
|
|
{
|
|
|
|
WCHAR mach[MAX_COMPUTERNAME_LENGTH + 1];
|
|
|
|
DWORD msize = MAX_COMPUTERNAME_LENGTH + 1;
|
|
|
|
if (!GetComputerNameW (mach, &msize))
|
|
|
|
wcscpy (mach, L"UNKNOWN");
|
|
|
|
if (!(*machine = uni_alloc (mach, wcslen (mach))))
|
|
|
|
{
|
2008-10-13 01:53:26 +02:00
|
|
|
cyglsa_printf ("No memory trying to create machine.\n");
|
2008-07-10 20:05:03 +02:00
|
|
|
return STATUS_NO_MEMORY;
|
|
|
|
}
|
|
|
|
}
|
2006-11-27 14:05:54 +01:00
|
|
|
/* Create a fake buffer in pbuf which is free'd again in the client.
|
|
|
|
Windows 2000 tends to crash when setting this pointer to NULL. */
|
|
|
|
if (pbuf)
|
|
|
|
{
|
2008-07-10 20:05:03 +02:00
|
|
|
#ifdef JUST_ANOTHER_NONWORKING_SOLUTION
|
|
|
|
cygprf_t prf;
|
2008-07-11 12:00:38 +02:00
|
|
|
WCHAR sam_username[MAX_DOMAIN_NAME_LEN + UNLEN + 2];
|
2008-07-10 20:05:03 +02:00
|
|
|
SECURITY_STRING sam_user, prefix;
|
|
|
|
PUCHAR user_auth;
|
|
|
|
ULONG user_auth_size;
|
|
|
|
WCHAR flatname[UNLEN + 1];
|
|
|
|
UNICODE_STRING flatnm;
|
|
|
|
TOKEN_SOURCE ts;
|
|
|
|
HANDLE token;
|
|
|
|
#endif /* JUST_ANOTHER_NONWORKING_SOLUTION */
|
|
|
|
|
2006-11-27 14:05:54 +01:00
|
|
|
stat = funcs->AllocateClientBuffer (request, 64UL, pbuf);
|
|
|
|
if (!LSA_SUCCESS (stat))
|
2008-07-10 20:05:03 +02:00
|
|
|
{
|
2008-10-13 01:53:26 +02:00
|
|
|
cyglsa_printf ("AllocateClientBuffer failed: 0x%08lx\n", stat);
|
2008-07-10 20:05:03 +02:00
|
|
|
return stat;
|
|
|
|
}
|
|
|
|
#ifdef JUST_ANOTHER_NONWORKING_SOLUTION
|
|
|
|
prf.magic_pre = MAGIC_PRE;
|
|
|
|
prf.token = NULL;
|
|
|
|
prf.magic_post = MAGIC_POST;
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
/* That's how it was supposed to work according to MSDN... */
|
2008-07-11 12:00:38 +02:00
|
|
|
wcscpy (sam_username, authinf->domain);
|
2008-07-10 20:05:03 +02:00
|
|
|
wcscat (sam_username, L"\\");
|
2008-07-11 12:00:38 +02:00
|
|
|
wcscat (sam_username, authinf->username);
|
2008-07-10 20:05:03 +02:00
|
|
|
#else
|
|
|
|
/* That's the only solution which worked, and then it only worked
|
|
|
|
for machine local accounts. No domain authentication possible.
|
|
|
|
STATUS_NO_SUCH_USER galore! */
|
2008-07-11 12:00:38 +02:00
|
|
|
wcscpy (sam_username, authinf->username);
|
2008-07-10 20:05:03 +02:00
|
|
|
#endif
|
|
|
|
RtlInitUnicodeString (&sam_user, sam_username);
|
|
|
|
RtlInitUnicodeString (&prefix, L"");
|
|
|
|
RtlInitEmptyUnicodeString (&flatnm, flatname,
|
|
|
|
(UNLEN + 1) * sizeof (WCHAR));
|
|
|
|
|
|
|
|
stat = funcs->GetAuthDataForUser (&sam_user, SecNameSamCompatible,
|
|
|
|
NULL, &user_auth,
|
|
|
|
&user_auth_size, &flatnm);
|
|
|
|
if (!NT_SUCCESS (stat))
|
|
|
|
{
|
2008-07-11 12:00:38 +02:00
|
|
|
char sam_u[MAX_DOMAIN_NAME_LEN + UNLEN + 2];
|
2008-07-10 20:05:03 +02:00
|
|
|
wcstombs (sam_u, sam_user.Buffer, sizeof (sam_u));
|
2008-10-13 01:53:26 +02:00
|
|
|
cyglsa_printf ("GetAuthDataForUser (%u,%u,%s) failed: 0x%08lx\n",
|
2008-07-10 20:05:03 +02:00
|
|
|
sam_user.Length, sam_user.MaximumLength, sam_u, stat);
|
|
|
|
return stat;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy (ts.SourceName, "Cygwin.1", 8);
|
|
|
|
ts.SourceIdentifier.HighPart = 0;
|
|
|
|
ts.SourceIdentifier.LowPart = 0x0104;
|
|
|
|
RtlInitEmptyUnicodeString (&flatnm, flatname,
|
|
|
|
(UNLEN + 1) * sizeof (WCHAR));
|
|
|
|
stat = funcs->ConvertAuthDataToToken (user_auth, user_auth_size,
|
|
|
|
SecurityDelegation, &ts,
|
|
|
|
Interactive, *authority,
|
|
|
|
&token, logon_id, &flatnm,
|
|
|
|
sub_stat);
|
|
|
|
if (!NT_SUCCESS (stat))
|
|
|
|
{
|
2008-10-13 01:53:26 +02:00
|
|
|
cyglsa_printf ("ConvertAuthDataToToken failed: 0x%08lx\n", stat);
|
2008-07-10 20:05:03 +02:00
|
|
|
return stat;
|
|
|
|
}
|
|
|
|
|
|
|
|
stat = funcs->DuplicateHandle (token, &prf.token);
|
|
|
|
if (!NT_SUCCESS (stat))
|
|
|
|
{
|
2008-10-13 01:53:26 +02:00
|
|
|
cyglsa_printf ("DuplicateHandle failed: 0x%08lx\n", stat);
|
2008-07-10 20:05:03 +02:00
|
|
|
return stat;
|
|
|
|
}
|
|
|
|
|
|
|
|
stat = funcs->CopyToClientBuffer (request, sizeof prf, *pbuf, &prf);
|
|
|
|
if (!NT_SUCCESS (stat))
|
|
|
|
{
|
2008-10-13 01:53:26 +02:00
|
|
|
cyglsa_printf ("CopyToClientBuffer failed: 0x%08lx\n", stat);
|
2008-07-10 20:05:03 +02:00
|
|
|
return stat;
|
|
|
|
}
|
|
|
|
funcs->FreeLsaHeap (user_auth);
|
|
|
|
#endif /* JUST_ANOTHER_NONWORKING_SOLUTION */
|
2006-11-27 14:05:54 +01:00
|
|
|
}
|
|
|
|
if (pbuf_len)
|
|
|
|
*pbuf_len = 64UL;
|
|
|
|
|
|
|
|
/* A PLSA_TOKEN_INFORMATION_V2 is allocated in one piece, so... */
|
|
|
|
#if defined (__x86_64__) || defined (_M_AMD64)
|
|
|
|
{
|
|
|
|
/* ...on 64 bit systems we have to convert the incoming 32 bit offsets
|
|
|
|
into 64 bit pointers. That requires to re-evaluate the size of the
|
|
|
|
outgoing tokinf structure and a somewhat awkward procedure to copy
|
|
|
|
the information over. */
|
|
|
|
LONG_PTR base;
|
|
|
|
PBYTE tptr;
|
|
|
|
DWORD size, newsize;
|
|
|
|
PSID src_sid;
|
|
|
|
PCYG_TOKEN_GROUPS src_grps;
|
|
|
|
PTOKEN_GROUPS grps;
|
|
|
|
PTOKEN_PRIVILEGES src_privs;
|
|
|
|
PACL src_acl;
|
|
|
|
|
|
|
|
base = (LONG_PTR) &authinf->inf;
|
|
|
|
|
|
|
|
newsize = authinf->inf_size;
|
2008-07-13 15:14:41 +02:00
|
|
|
newsize += sizeof (TOKEN_USER) - sizeof (CYG_TOKEN_USER); /* User SID */
|
2006-11-27 14:05:54 +01:00
|
|
|
newsize += sizeof (PTOKEN_GROUPS) - sizeof (OFFSET); /* Groups */
|
|
|
|
src_grps = (PCYG_TOKEN_GROUPS) (base + authinf->inf.Groups);
|
|
|
|
newsize += src_grps->GroupCount /* Group SIDs */
|
2008-07-13 15:14:41 +02:00
|
|
|
* (sizeof (SID_AND_ATTRIBUTES)
|
|
|
|
- sizeof (CYG_SID_AND_ATTRIBUTES));
|
2006-11-27 14:05:54 +01:00
|
|
|
newsize += sizeof (PSID) - sizeof (OFFSET); /* Primary Group SID */
|
2008-07-13 15:14:41 +02:00
|
|
|
newsize += sizeof (PTOKEN_PRIVILEGES) - sizeof (OFFSET); /* Privileges */
|
|
|
|
newsize += 0; /* Owner SID */
|
2006-11-27 14:05:54 +01:00
|
|
|
newsize += sizeof (PACL) - sizeof (OFFSET); /* Default DACL */
|
2008-07-13 15:14:41 +02:00
|
|
|
|
2006-11-27 14:05:54 +01:00
|
|
|
if (!(tokinf = funcs->AllocateLsaHeap (newsize)))
|
|
|
|
return STATUS_NO_MEMORY;
|
|
|
|
tptr = (PBYTE)(tokinf + 1);
|
|
|
|
|
|
|
|
tokinf->ExpirationTime = authinf->inf.ExpirationTime;
|
|
|
|
/* User SID */
|
|
|
|
src_sid = (PSID) (base + authinf->inf.User.User.Sid);
|
2012-05-29 14:46:01 +02:00
|
|
|
size = GetLengthSid (src_sid);
|
|
|
|
CopySid (size, (PSID) tptr, src_sid);
|
2006-11-27 14:05:54 +01:00
|
|
|
tokinf->User.User.Sid = (PSID) tptr;
|
|
|
|
tptr += size;
|
|
|
|
tokinf->User.User.Attributes = authinf->inf.User.User.Attributes;
|
|
|
|
/* Groups */
|
|
|
|
grps = (PTOKEN_GROUPS) tptr;
|
|
|
|
tokinf->Groups = grps;
|
|
|
|
grps->GroupCount = src_grps->GroupCount;
|
|
|
|
tptr += sizeof grps->GroupCount
|
|
|
|
+ grps->GroupCount * sizeof (SID_AND_ATTRIBUTES);
|
|
|
|
/* Group SIDs */
|
|
|
|
for (i = 0; i < src_grps->GroupCount; ++i)
|
|
|
|
{
|
|
|
|
src_sid = (PSID) (base + src_grps->Groups[i].Sid);
|
2012-05-29 14:46:01 +02:00
|
|
|
size = GetLengthSid (src_sid);
|
|
|
|
CopySid (size, (PSID) tptr, src_sid);
|
2006-11-27 14:05:54 +01:00
|
|
|
tokinf->Groups->Groups[i].Sid = (PSID) tptr;
|
|
|
|
tptr += size;
|
|
|
|
tokinf->Groups->Groups[i].Attributes = src_grps->Groups[i].Attributes;
|
|
|
|
}
|
|
|
|
/* Primary Group SID */
|
|
|
|
src_sid = (PSID) (base + authinf->inf.PrimaryGroup.PrimaryGroup);
|
2012-05-29 14:46:01 +02:00
|
|
|
size = GetLengthSid (src_sid);
|
|
|
|
CopySid (size, (PSID) tptr, src_sid);
|
2006-11-27 14:05:54 +01:00
|
|
|
tokinf->PrimaryGroup.PrimaryGroup = (PSID) tptr;
|
|
|
|
tptr += size;
|
|
|
|
/* Privileges */
|
|
|
|
src_privs = (PTOKEN_PRIVILEGES) (base + authinf->inf.Privileges);
|
|
|
|
size = sizeof src_privs->PrivilegeCount
|
|
|
|
+ src_privs->PrivilegeCount * sizeof (LUID_AND_ATTRIBUTES);
|
|
|
|
memcpy (tptr, src_privs, size);
|
|
|
|
tokinf->Privileges = (PTOKEN_PRIVILEGES) tptr;
|
|
|
|
tptr += size;
|
|
|
|
/* Owner */
|
|
|
|
tokinf->Owner.Owner = NULL;
|
|
|
|
/* Default DACL */
|
|
|
|
src_acl = (PACL) (base + authinf->inf.DefaultDacl.DefaultDacl);
|
|
|
|
size = src_acl->AclSize;
|
|
|
|
memcpy (tptr, src_acl, size);
|
|
|
|
tokinf->DefaultDacl.DefaultDacl = (PACL) tptr;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
{
|
|
|
|
/* ...on 32 bit systems we just allocate tokinf with the same size as
|
|
|
|
we get, copy the whole structure and convert offsets into pointers. */
|
|
|
|
if (!(tokinf = funcs->AllocateLsaHeap (authinf->inf_size)))
|
|
|
|
return STATUS_NO_MEMORY;
|
|
|
|
memcpy (tokinf, &authinf->inf, authinf->inf_size);
|
|
|
|
|
|
|
|
/* User SID */
|
|
|
|
tokinf->User.User.Sid = (PSID)
|
|
|
|
((PBYTE) tokinf + (LONG_PTR) tokinf->User.User.Sid);
|
|
|
|
/* Groups */
|
|
|
|
tokinf->Groups = (PTOKEN_GROUPS)
|
|
|
|
((PBYTE) tokinf + (LONG_PTR) tokinf->Groups);
|
|
|
|
/* Group SIDs */
|
|
|
|
for (i = 0; i < tokinf->Groups->GroupCount; ++i)
|
2016-02-17 16:08:59 +01:00
|
|
|
tokinf->Groups->Groups[i].Sid = (PSID)
|
|
|
|
((PBYTE) tokinf + (LONG_PTR) tokinf->Groups->Groups[i].Sid);
|
2006-11-27 14:05:54 +01:00
|
|
|
/* Primary Group SID */
|
|
|
|
tokinf->PrimaryGroup.PrimaryGroup = (PSID)
|
|
|
|
((PBYTE) tokinf + (LONG_PTR) tokinf->PrimaryGroup.PrimaryGroup);
|
|
|
|
/* Privileges */
|
|
|
|
tokinf->Privileges = (PTOKEN_PRIVILEGES)
|
|
|
|
((PBYTE) tokinf + (LONG_PTR) tokinf->Privileges);
|
|
|
|
/* Owner SID */
|
|
|
|
tokinf->Owner.Owner = NULL;
|
|
|
|
/* Default DACL */
|
|
|
|
tokinf->DefaultDacl.DefaultDacl = (PACL)
|
|
|
|
((PBYTE) tokinf + (LONG_PTR) tokinf->DefaultDacl.DefaultDacl);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
*tok = (PVOID) tokinf;
|
|
|
|
*tok_type = LsaTokenInformationV2;
|
|
|
|
|
|
|
|
print_tokinf (tokinf, authinf->inf_size, authinf, &authinf->inf,
|
|
|
|
(PVOID)((LONG_PTR) &authinf->inf + authinf->inf_size));
|
|
|
|
|
|
|
|
/* Create logon session. */
|
2012-05-29 14:46:01 +02:00
|
|
|
if (!AllocateLocallyUniqueId (logon_id))
|
2006-11-27 14:05:54 +01:00
|
|
|
{
|
|
|
|
funcs->FreeLsaHeap (*tok);
|
|
|
|
*tok = NULL;
|
2012-05-29 14:46:01 +02:00
|
|
|
cyglsa_printf ("AllocateLocallyUniqueId failed: Win32 error %lu\n",
|
|
|
|
GetLastError ());
|
2006-11-27 14:05:54 +01:00
|
|
|
return STATUS_INSUFFICIENT_RESOURCES;
|
|
|
|
}
|
|
|
|
stat = funcs->CreateLogonSession (logon_id);
|
|
|
|
if (stat != STATUS_SUCCESS)
|
|
|
|
{
|
|
|
|
funcs->FreeLsaHeap (*tok);
|
|
|
|
*tok = NULL;
|
2008-10-13 01:53:26 +02:00
|
|
|
cyglsa_printf ("CreateLogonSession failed: 0x%08lx\n", stat);
|
2006-11-27 14:05:54 +01:00
|
|
|
return stat;
|
|
|
|
}
|
|
|
|
|
2008-10-13 01:53:26 +02:00
|
|
|
cyglsa_printf ("BINGO!!!\n", stat);
|
2006-11-27 14:05:54 +01:00
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
VOID NTAPI
|
|
|
|
LsaApLogonTerminated(PLUID LogonId)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS NTAPI
|
|
|
|
LsaApCallPackage (PLSA_CLIENT_REQUEST request, PVOID authinf,
|
|
|
|
PVOID client_auth_base, ULONG auth_len, PVOID *ret_buf,
|
|
|
|
PULONG ret_buf_len, PNTSTATUS ret_stat)
|
|
|
|
{
|
|
|
|
return STATUS_NOT_IMPLEMENTED;
|
|
|
|
}
|