2007-07-20 22:29:43 +08:00
|
|
|
/* sec_auth.cc: NT authentication functions
|
|
|
|
|
|
|
|
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. */
|
|
|
|
|
|
|
|
#include "winsup.h"
|
|
|
|
#include <stdlib.h>
|
2008-07-09 19:58:38 +08:00
|
|
|
#include <wchar.h>
|
2007-07-20 22:29:43 +08:00
|
|
|
#include <wininet.h>
|
|
|
|
#include <ntsecapi.h>
|
|
|
|
#include "cygerrno.h"
|
|
|
|
#include "security.h"
|
|
|
|
#include "path.h"
|
|
|
|
#include "fhandler.h"
|
|
|
|
#include "dtable.h"
|
|
|
|
#include "cygheap.h"
|
2014-12-02 18:49:47 +08:00
|
|
|
#include "registry.h"
|
2007-07-20 22:29:43 +08:00
|
|
|
#include "ntdll.h"
|
2008-07-30 20:10:20 +08:00
|
|
|
#include "tls_pbuf.h"
|
2008-07-11 18:00:36 +08:00
|
|
|
#include <lm.h>
|
|
|
|
#include <iptypes.h>
|
2014-12-02 18:49:47 +08:00
|
|
|
#include <userenv.h>
|
2019-01-27 01:33:41 +08:00
|
|
|
#define SECURITY_WIN32
|
|
|
|
#include <secext.h>
|
2007-07-20 22:29:43 +08:00
|
|
|
#include "cyglsa.h"
|
2008-11-26 18:18:10 +08:00
|
|
|
#include "cygserver_setpwd.h"
|
2007-07-20 22:29:43 +08:00
|
|
|
#include <cygwin/version.h>
|
|
|
|
|
2015-03-03 20:58:34 +08:00
|
|
|
/* OpenBSD 2.0 and later. */
|
|
|
|
extern "C"
|
|
|
|
int
|
|
|
|
issetugid (void)
|
|
|
|
{
|
|
|
|
return cygheap->user.issetuid () ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
2015-12-16 04:43:33 +08:00
|
|
|
/* The token returned by system functions is a restricted token. The full
|
|
|
|
admin token is linked to it and can be fetched with NtQueryInformationToken.
|
|
|
|
This function returns the elevated token if available, the original token
|
|
|
|
otherwise. The token handle is also made inheritable since that's necessary
|
|
|
|
anyway. */
|
2010-02-06 21:13:15 +08:00
|
|
|
static HANDLE
|
|
|
|
get_full_privileged_inheritable_token (HANDLE token)
|
|
|
|
{
|
2015-12-15 21:58:52 +08:00
|
|
|
TOKEN_LINKED_TOKEN linked;
|
|
|
|
ULONG size;
|
|
|
|
|
|
|
|
/* When fetching the linked token without TCB privs, then the linked
|
|
|
|
token is not a primary token, only an impersonation token, which is
|
|
|
|
not suitable for CreateProcessAsUser. Converting it to a primary
|
|
|
|
token using DuplicateTokenEx does NOT work for the linked token in
|
|
|
|
this case. So we have to switch on TCB privs to get a primary token.
|
|
|
|
This is generally performed in the calling functions. */
|
|
|
|
if (NT_SUCCESS (NtQueryInformationToken (token, TokenLinkedToken,
|
|
|
|
(PVOID) &linked, sizeof linked,
|
|
|
|
&size)))
|
|
|
|
{
|
|
|
|
debug_printf ("Linked Token: %p", linked.LinkedToken);
|
|
|
|
if (linked.LinkedToken)
|
2010-02-06 21:13:15 +08:00
|
|
|
{
|
2015-12-15 21:58:52 +08:00
|
|
|
TOKEN_TYPE type;
|
|
|
|
|
|
|
|
/* At this point we don't know if the user actually had TCB
|
|
|
|
privileges. Check if the linked token is a primary token.
|
|
|
|
If not, just return the original token. */
|
|
|
|
if (NT_SUCCESS (NtQueryInformationToken (linked.LinkedToken,
|
|
|
|
TokenType, (PVOID) &type,
|
|
|
|
sizeof type, &size))
|
|
|
|
&& type != TokenPrimary)
|
|
|
|
debug_printf ("Linked Token is not a primary token!");
|
|
|
|
else
|
2010-02-06 21:13:15 +08:00
|
|
|
{
|
2015-12-15 21:58:52 +08:00
|
|
|
CloseHandle (token);
|
|
|
|
token = linked.LinkedToken;
|
2010-02-06 21:13:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!SetHandleInformation (token, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT))
|
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
CloseHandle (token);
|
|
|
|
token = NULL;
|
|
|
|
}
|
|
|
|
return token;
|
|
|
|
}
|
|
|
|
|
2009-10-13 18:23:31 +08:00
|
|
|
void
|
|
|
|
set_imp_token (HANDLE token, int type)
|
|
|
|
{
|
2013-04-23 17:44:36 +08:00
|
|
|
debug_printf ("set_imp_token (%p, %d)", token, type);
|
2009-10-13 18:23:31 +08:00
|
|
|
cygheap->user.external_token = (token == INVALID_HANDLE_VALUE
|
|
|
|
? NO_IMPERSONATION : token);
|
|
|
|
cygheap->user.ext_token_is_restricted = (type == CW_TOKEN_RESTRICTED);
|
|
|
|
}
|
|
|
|
|
2007-07-20 22:29:43 +08:00
|
|
|
extern "C" void
|
|
|
|
cygwin_set_impersonation_token (const HANDLE hToken)
|
|
|
|
{
|
2009-10-13 18:23:31 +08:00
|
|
|
set_imp_token (hToken, CW_TOKEN_IMPERSONATION);
|
2007-07-20 22:29:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-07-30 20:10:20 +08:00
|
|
|
extract_nt_dom_user (const struct passwd *pw, PWCHAR domain, PWCHAR user)
|
2007-07-20 22:29:43 +08:00
|
|
|
{
|
|
|
|
|
2008-07-11 18:00:36 +08:00
|
|
|
cygsid psid;
|
|
|
|
DWORD ulen = UNLEN + 1;
|
|
|
|
DWORD dlen = MAX_DOMAIN_NAME_LEN + 1;
|
|
|
|
SID_NAME_USE use;
|
|
|
|
|
2013-04-23 17:44:36 +08:00
|
|
|
debug_printf ("pw_gecos %p (%s)", pw->pw_gecos, pw->pw_gecos);
|
2007-07-20 22:29:43 +08:00
|
|
|
|
2015-03-19 00:15:27 +08:00
|
|
|
/* The incoming passwd entry is not necessarily a pointer to the
|
|
|
|
internal passwd buffers, thus we must not rely on being able to
|
|
|
|
cast it to pg_pwd. */
|
|
|
|
if (psid.getfrompw_gecos (pw)
|
2008-07-30 20:10:20 +08:00
|
|
|
&& LookupAccountSidW (NULL, psid, user, &ulen, domain, &dlen, &use))
|
2008-07-11 18:00:36 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
char *d, *u, *c;
|
2008-07-30 20:10:20 +08:00
|
|
|
domain[0] = L'\0';
|
|
|
|
sys_mbstowcs (user, UNLEN + 1, pw->pw_name);
|
2007-07-20 22:29:43 +08:00
|
|
|
if ((d = strstr (pw->pw_gecos, "U-")) != NULL &&
|
|
|
|
(d == pw->pw_gecos || d[-1] == ','))
|
|
|
|
{
|
2012-10-27 20:09:38 +08:00
|
|
|
c = strchrnul (d + 2, ',');
|
|
|
|
if ((u = strchrnul (d + 2, '\\')) >= c)
|
2016-01-22 01:27:05 +08:00
|
|
|
u = d + 1;
|
2008-07-11 18:00:36 +08:00
|
|
|
else if (u - d <= MAX_DOMAIN_NAME_LEN + 2)
|
2016-01-22 01:27:05 +08:00
|
|
|
sys_mbstowcs (domain, MAX_DOMAIN_NAME_LEN + 1, d + 2, u - d - 1);
|
2007-07-20 22:29:43 +08:00
|
|
|
if (c - u <= UNLEN + 1)
|
2016-01-22 01:27:05 +08:00
|
|
|
sys_mbstowcs (user, UNLEN + 1, u + 1, c - u);
|
2007-07-20 22:29:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" HANDLE
|
|
|
|
cygwin_logon_user (const struct passwd *pw, const char *password)
|
|
|
|
{
|
2008-07-30 20:10:20 +08:00
|
|
|
if (!pw || !password)
|
2007-07-20 22:29:43 +08:00
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return INVALID_HANDLE_VALUE;
|
|
|
|
}
|
|
|
|
|
2008-07-30 20:10:20 +08:00
|
|
|
WCHAR nt_domain[MAX_DOMAIN_NAME_LEN + 1];
|
|
|
|
WCHAR nt_user[UNLEN + 1];
|
|
|
|
PWCHAR passwd;
|
2007-07-20 22:29:43 +08:00
|
|
|
HANDLE hToken;
|
2008-07-30 20:10:20 +08:00
|
|
|
tmp_pathbuf tp;
|
2007-07-20 22:29:43 +08:00
|
|
|
|
|
|
|
extract_nt_dom_user (pw, nt_domain, nt_user);
|
2008-07-30 20:10:20 +08:00
|
|
|
debug_printf ("LogonUserW (%W, %W, ...)", nt_user, nt_domain);
|
|
|
|
sys_mbstowcs (passwd = tp.w_get (), NT_MAX_PATH, password);
|
2007-07-20 22:29:43 +08:00
|
|
|
/* CV 2005-06-08: LogonUser should run under the primary process token,
|
2008-07-11 18:00:36 +08:00
|
|
|
otherwise it returns with ERROR_ACCESS_DENIED. */
|
|
|
|
cygheap->user.deimpersonate ();
|
2008-07-30 20:10:20 +08:00
|
|
|
if (!LogonUserW (nt_user, *nt_domain ? nt_domain : NULL, passwd,
|
|
|
|
LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT,
|
2007-07-20 22:29:43 +08:00
|
|
|
&hToken))
|
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
hToken = INVALID_HANDLE_VALUE;
|
|
|
|
}
|
2010-02-06 21:13:15 +08:00
|
|
|
else
|
2007-07-20 22:29:43 +08:00
|
|
|
{
|
2016-01-22 01:32:16 +08:00
|
|
|
HANDLE hPrivToken = NULL;
|
|
|
|
|
2010-02-06 21:13:15 +08:00
|
|
|
/* See the comment in get_full_privileged_inheritable_token for a
|
|
|
|
description why we enable TCB privileges here. */
|
|
|
|
push_self_privilege (SE_TCB_PRIVILEGE, true);
|
2016-01-22 01:32:16 +08:00
|
|
|
hPrivToken = get_full_privileged_inheritable_token (hToken);
|
2010-02-06 21:13:15 +08:00
|
|
|
pop_self_privilege ();
|
2016-01-22 01:32:16 +08:00
|
|
|
if (!hPrivToken)
|
|
|
|
debug_printf ("Can't fetch linked token (%E), use standard token");
|
|
|
|
else
|
|
|
|
hToken = hPrivToken;
|
2007-07-20 22:29:43 +08:00
|
|
|
}
|
2014-05-20 18:21:34 +08:00
|
|
|
RtlSecureZeroMemory (passwd, NT_MAX_PATH);
|
2007-07-20 22:29:43 +08:00
|
|
|
cygheap->user.reimpersonate ();
|
2011-12-04 05:43:27 +08:00
|
|
|
debug_printf ("%R = logon_user(%s,...)", hToken, pw->pw_name);
|
2007-07-20 22:29:43 +08:00
|
|
|
return hToken;
|
|
|
|
}
|
|
|
|
|
2014-12-02 18:49:47 +08:00
|
|
|
/* The buffer path points to should be at least MAX_PATH bytes. */
|
|
|
|
PWCHAR
|
|
|
|
get_user_profile_directory (PCWSTR sidstr, PWCHAR path, SIZE_T path_len)
|
|
|
|
{
|
|
|
|
if (!sidstr || !path)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
UNICODE_STRING buf;
|
|
|
|
tmp_pathbuf tp;
|
|
|
|
tp.u_get (&buf);
|
|
|
|
NTSTATUS status;
|
|
|
|
|
|
|
|
RTL_QUERY_REGISTRY_TABLE tab[2] = {
|
|
|
|
{ NULL, RTL_QUERY_REGISTRY_NOEXPAND | RTL_QUERY_REGISTRY_DIRECT
|
|
|
|
| RTL_QUERY_REGISTRY_REQUIRED,
|
|
|
|
L"ProfileImagePath", &buf, REG_NONE, NULL, 0 },
|
|
|
|
{ NULL, 0, NULL, NULL, 0, NULL, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
WCHAR key[wcslen (sidstr) + 16];
|
|
|
|
wcpcpy (wcpcpy (key, L"ProfileList\\"), sidstr);
|
|
|
|
status = RtlQueryRegistryValues (RTL_REGISTRY_WINDOWS_NT, key, tab,
|
|
|
|
NULL, NULL);
|
|
|
|
if (!NT_SUCCESS (status) || buf.Length == 0)
|
|
|
|
{
|
|
|
|
debug_printf ("ProfileImagePath for %W not found, status %y", sidstr,
|
|
|
|
status);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
ExpandEnvironmentStringsW (buf.Buffer, path, path_len);
|
|
|
|
debug_printf ("ProfileImagePath for %W: %W", sidstr, path);
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Load user profile if it's not already loaded. If the user profile doesn't
|
2015-12-16 04:43:33 +08:00
|
|
|
exist on the machine try to create it.
|
2014-12-02 18:49:47 +08:00
|
|
|
|
|
|
|
Return a handle to the loaded user registry hive only if it got actually
|
|
|
|
loaded here, not if it already existed. There's no reliable way to know
|
|
|
|
when to unload the hive yet, so we're leaking this registry handle for now.
|
|
|
|
TODO: Try to find a way to reliably unload the user profile again. */
|
|
|
|
HANDLE
|
|
|
|
load_user_profile (HANDLE token, struct passwd *pw, cygpsid &usersid)
|
|
|
|
{
|
|
|
|
WCHAR domain[DNLEN + 1];
|
|
|
|
WCHAR username[UNLEN + 1];
|
|
|
|
WCHAR sid[128];
|
|
|
|
WCHAR userpath[MAX_PATH];
|
|
|
|
PROFILEINFOW pi;
|
|
|
|
|
|
|
|
extract_nt_dom_user (pw, domain, username);
|
|
|
|
usersid.string (sid);
|
2019-02-24 00:22:44 +08:00
|
|
|
debug_printf ("user: <%W> <%W> <%W>", username, domain, sid);
|
2014-12-02 18:49:47 +08:00
|
|
|
/* Check if the local profile dir has already been created. */
|
|
|
|
if (!get_user_profile_directory (sid, userpath, MAX_PATH))
|
|
|
|
{
|
2015-12-16 00:08:05 +08:00
|
|
|
/* No, try to create it. */
|
2014-12-02 18:49:47 +08:00
|
|
|
HRESULT res = CreateProfile (sid, username, userpath, MAX_PATH);
|
|
|
|
if (res != S_OK)
|
|
|
|
{
|
2015-12-16 00:08:05 +08:00
|
|
|
debug_printf ("CreateProfile, HRESULT %x", res);
|
2014-12-02 18:49:47 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Fill PROFILEINFO */
|
|
|
|
memset (&pi, 0, sizeof pi);
|
|
|
|
pi.dwSize = sizeof pi;
|
|
|
|
pi.dwFlags = PI_NOUI;
|
|
|
|
pi.lpUserName = username;
|
2019-02-24 00:22:44 +08:00
|
|
|
/* Check if user has a roaming profile and fill in lpProfilePath, if so.
|
|
|
|
Call NetUserGetInfo only for local machine accounts, use LDAP otherwise. */
|
|
|
|
if (!wcscasecmp (domain, cygheap->dom.account_flat_name ()))
|
2014-12-02 18:49:47 +08:00
|
|
|
{
|
2019-02-24 00:22:44 +08:00
|
|
|
NET_API_STATUS nas;
|
|
|
|
PUSER_INFO_3 ui;
|
|
|
|
|
|
|
|
nas = NetUserGetInfo (NULL, username, 3, (PBYTE *) &ui);
|
|
|
|
if (nas != NERR_Success)
|
2014-12-02 18:49:47 +08:00
|
|
|
debug_printf ("NetUserGetInfo, %u", nas);
|
2019-02-24 00:22:44 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (ui->usri3_profile && *ui->usri3_profile)
|
|
|
|
pi.lpProfilePath = ui->usri3_profile;
|
|
|
|
NetApiBufferFree (ui);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cyg_ldap cldap;
|
|
|
|
PWCHAR dnsdomain = NULL;
|
|
|
|
|
|
|
|
if (!wcscasecmp (domain, cygheap->dom.primary_flat_name ()))
|
|
|
|
dnsdomain = wcsdup (cygheap->dom.primary_dns_name ());
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PDS_DOMAIN_TRUSTSW td = NULL;
|
|
|
|
|
|
|
|
for (ULONG idx = 0; (td = cygheap->dom.trusted_domain (idx)); ++idx)
|
|
|
|
if (!wcscasecmp (domain, td->NetbiosDomainName))
|
|
|
|
{
|
|
|
|
dnsdomain = wcsdup (td->DnsDomainName);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-03-01 06:17:55 +08:00
|
|
|
if (dnsdomain)
|
2019-02-24 00:22:44 +08:00
|
|
|
{
|
2019-03-01 06:17:55 +08:00
|
|
|
if (cldap.fetch_ad_account (usersid, false, dnsdomain))
|
2019-02-24 00:22:44 +08:00
|
|
|
{
|
2019-03-01 06:17:55 +08:00
|
|
|
PWCHAR val = cldap.get_profile_path ();
|
|
|
|
if (val && *val)
|
|
|
|
{
|
|
|
|
wcsncpy (userpath, val, MAX_PATH - 1);
|
|
|
|
userpath[MAX_PATH - 1] = L'\0';
|
|
|
|
pi.lpProfilePath = userpath;
|
|
|
|
}
|
2019-02-24 00:22:44 +08:00
|
|
|
}
|
2019-03-01 06:17:55 +08:00
|
|
|
free (dnsdomain);
|
2019-02-24 00:22:44 +08:00
|
|
|
}
|
2019-03-01 06:17:55 +08:00
|
|
|
else
|
|
|
|
debug_printf ("Unknown domain <%W>?", domain);
|
2014-12-02 18:49:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!LoadUserProfileW (token, &pi))
|
|
|
|
debug_printf ("LoadUserProfileW, %E");
|
|
|
|
return pi.hProfile;
|
|
|
|
}
|
|
|
|
|
2019-02-24 00:30:44 +08:00
|
|
|
bool
|
|
|
|
unload_user_profile (HANDLE token, HANDLE profile)
|
|
|
|
{
|
2019-02-24 03:46:48 +08:00
|
|
|
bool ret = UnloadUserProfile (token, profile);
|
|
|
|
if (!ret)
|
|
|
|
debug_printf ("UnloadUserProfile, %E");
|
|
|
|
return ret;
|
2019-02-24 00:30:44 +08:00
|
|
|
}
|
|
|
|
|
2008-11-26 18:18:10 +08:00
|
|
|
HANDLE
|
2014-01-24 01:02:30 +08:00
|
|
|
lsa_open_policy (PWCHAR server, ACCESS_MASK access)
|
2007-07-20 22:29:43 +08:00
|
|
|
{
|
2014-01-24 01:02:30 +08:00
|
|
|
LSA_UNICODE_STRING srvbuf;
|
|
|
|
PLSA_UNICODE_STRING srv = NULL;
|
|
|
|
static LSA_OBJECT_ATTRIBUTES oa = { 0, 0, 0, 0, 0, 0 };
|
|
|
|
HANDLE lsa;
|
2007-07-20 22:29:43 +08:00
|
|
|
|
2014-01-24 01:02:30 +08:00
|
|
|
if (server)
|
|
|
|
{
|
|
|
|
srv = &srvbuf;
|
|
|
|
RtlInitUnicodeString (srv, server);
|
|
|
|
}
|
|
|
|
NTSTATUS status = LsaOpenPolicy (srv, &oa, access, &lsa);
|
2011-04-01 16:41:26 +08:00
|
|
|
if (!NT_SUCCESS (status))
|
2008-12-16 04:25:44 +08:00
|
|
|
{
|
2011-04-01 16:41:26 +08:00
|
|
|
__seterrno_from_nt_status (status);
|
2014-01-24 01:02:30 +08:00
|
|
|
lsa = NULL;
|
2008-12-16 04:25:44 +08:00
|
|
|
}
|
2007-07-20 22:29:43 +08:00
|
|
|
return lsa;
|
|
|
|
}
|
|
|
|
|
2014-01-24 01:02:30 +08:00
|
|
|
void
|
|
|
|
lsa_close_policy (HANDLE lsa)
|
2007-07-20 22:29:43 +08:00
|
|
|
{
|
2014-01-24 01:02:30 +08:00
|
|
|
if (lsa)
|
2007-07-20 22:29:43 +08:00
|
|
|
LsaClose (lsa);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2015-01-22 00:31:38 +08:00
|
|
|
get_logon_server (PCWSTR domain, PWCHAR server, ULONG flags)
|
2007-07-20 22:29:43 +08:00
|
|
|
{
|
Drop NT4 support.
* autoload.cc (DnsQuery_A): Fatal if not available.
(DnsRecordListFree): Ditto.
(DsGetDcNameW): Ditto.
(NetGetAnyDCName): Remove.
(NetGetDCName): Remove.
(EnumProcessModules): Fatal if not available.
(GetModuleFileNameExW): Ditto.
(GetModuleInformation): Ditto.
(GetProcessMemoryInfo): Ditto.
(QueryWorkingSet): Ditto.
(LsaRegisterLogonProcess): Ditto.
* fenv.cc (_feinitialise): Drop supports_sse condition.
* fhandler_disk_file.cc (path_conv::isgood_inode): Fix comment.
(fhandler_base::fstat_by_name): Drop has_fileid_dirinfo condition.
(fhandler_disk_file::opendir): Ditto.
* fhandler_netdrive.cc (fhandler_netdrive::readdir): Fix comment.
* fhandler_proc.cc (format_proc_partitions): Drop NT4-only code.
* fhandler_process.cc (get_process_state): Ditto.
* kernel32.cc (GetWindowsDirectoryW): Remove.
(GetWindowsDirectoryA): Remove.
* miscfuncs.cc (nice_to_winprio): Drop NT4-only code.
* mount.cc (fs_info::update): Fix comments.
* net.cc (get_2k_ifs): Drop NT4-only code.
* sec_auth.cc (get_logon_server): Ditto.
(lsaauth): Drop NT4-specific error handling.
* security.cc (alloc_sd): Set SE_DACL_PROTECTED unconditionally.
* select.cc (select_stuff::wait): Always use MWMO_INPUTAVAILABLE.
(peek_windows): Drop NT4-only condition in call to PeekMessage.
* syscalls.cc (gethostid): Remove NT4-only workaround.
* wincap.cc: Througout, drop has_dacl_protect,
has_broken_if_oper_status, has_process_io_counters,
has_terminal_services, has_extended_priority_class, has_guid_volumes,
has_fileid_dirinfo, has_mwmo_inputavailable and supports_sse from
wincaps.
(wincap_nt4sp4): Remove.
(wincap_minimal): Set to wincap_2000.
(wincapc::init): Rely on availability of OSVERSIONINFOEX structure.
Treat error from GetVersionEx as fatal. Treat NT4 as fatal.
* wincap.h (struct wincaps): Drop has_dacl_protect,
has_broken_if_oper_status, has_process_io_counters,
has_terminal_services, has_extended_priority_class, has_guid_volumes,
has_fileid_dirinfo, has_mwmo_inputavailable and supports_sse flags
and methods.
* winlean.h (GetWindowsDirectoryW) Define as GetSystemWindowsDirectoryW.
(GetWindowsDirectoryA): Define as GetSystemWindowsDirectoryA.
2011-04-04 20:23:36 +08:00
|
|
|
DWORD ret;
|
2008-07-09 19:58:38 +08:00
|
|
|
PDOMAIN_CONTROLLER_INFOW pci;
|
2007-07-20 22:29:43 +08:00
|
|
|
|
|
|
|
/* Empty domain is interpreted as local system */
|
2014-02-20 02:29:52 +08:00
|
|
|
if (cygheap->dom.init ()
|
|
|
|
&& (!domain[0]
|
|
|
|
|| !wcscasecmp (domain, cygheap->dom.account_flat_name ())))
|
2007-07-20 22:29:43 +08:00
|
|
|
{
|
2014-02-10 03:44:56 +08:00
|
|
|
wcpcpy (wcpcpy (server, L"\\\\"), cygheap->dom.account_flat_name ());
|
2007-07-20 22:29:43 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Try to get any available domain controller for this domain */
|
2014-02-10 03:44:56 +08:00
|
|
|
ret = DsGetDcNameW (NULL, domain, NULL, NULL, flags, &pci);
|
Drop NT4 support.
* autoload.cc (DnsQuery_A): Fatal if not available.
(DnsRecordListFree): Ditto.
(DsGetDcNameW): Ditto.
(NetGetAnyDCName): Remove.
(NetGetDCName): Remove.
(EnumProcessModules): Fatal if not available.
(GetModuleFileNameExW): Ditto.
(GetModuleInformation): Ditto.
(GetProcessMemoryInfo): Ditto.
(QueryWorkingSet): Ditto.
(LsaRegisterLogonProcess): Ditto.
* fenv.cc (_feinitialise): Drop supports_sse condition.
* fhandler_disk_file.cc (path_conv::isgood_inode): Fix comment.
(fhandler_base::fstat_by_name): Drop has_fileid_dirinfo condition.
(fhandler_disk_file::opendir): Ditto.
* fhandler_netdrive.cc (fhandler_netdrive::readdir): Fix comment.
* fhandler_proc.cc (format_proc_partitions): Drop NT4-only code.
* fhandler_process.cc (get_process_state): Ditto.
* kernel32.cc (GetWindowsDirectoryW): Remove.
(GetWindowsDirectoryA): Remove.
* miscfuncs.cc (nice_to_winprio): Drop NT4-only code.
* mount.cc (fs_info::update): Fix comments.
* net.cc (get_2k_ifs): Drop NT4-only code.
* sec_auth.cc (get_logon_server): Ditto.
(lsaauth): Drop NT4-specific error handling.
* security.cc (alloc_sd): Set SE_DACL_PROTECTED unconditionally.
* select.cc (select_stuff::wait): Always use MWMO_INPUTAVAILABLE.
(peek_windows): Drop NT4-only condition in call to PeekMessage.
* syscalls.cc (gethostid): Remove NT4-only workaround.
* wincap.cc: Througout, drop has_dacl_protect,
has_broken_if_oper_status, has_process_io_counters,
has_terminal_services, has_extended_priority_class, has_guid_volumes,
has_fileid_dirinfo, has_mwmo_inputavailable and supports_sse from
wincaps.
(wincap_nt4sp4): Remove.
(wincap_minimal): Set to wincap_2000.
(wincapc::init): Rely on availability of OSVERSIONINFOEX structure.
Treat error from GetVersionEx as fatal. Treat NT4 as fatal.
* wincap.h (struct wincaps): Drop has_dacl_protect,
has_broken_if_oper_status, has_process_io_counters,
has_terminal_services, has_extended_priority_class, has_guid_volumes,
has_fileid_dirinfo, has_mwmo_inputavailable and supports_sse flags
and methods.
* winlean.h (GetWindowsDirectoryW) Define as GetSystemWindowsDirectoryW.
(GetWindowsDirectoryA): Define as GetSystemWindowsDirectoryA.
2011-04-04 20:23:36 +08:00
|
|
|
if (ret == ERROR_SUCCESS)
|
2007-07-20 22:29:43 +08:00
|
|
|
{
|
2008-07-11 18:00:36 +08:00
|
|
|
wcscpy (server, pci->DomainControllerName);
|
2007-07-20 22:29:43 +08:00
|
|
|
NetApiBufferFree (pci);
|
2014-02-10 03:44:56 +08:00
|
|
|
debug_printf ("DC: server: %W", server);
|
2007-07-20 22:29:43 +08:00
|
|
|
return true;
|
|
|
|
}
|
Drop NT4 support.
* autoload.cc (DnsQuery_A): Fatal if not available.
(DnsRecordListFree): Ditto.
(DsGetDcNameW): Ditto.
(NetGetAnyDCName): Remove.
(NetGetDCName): Remove.
(EnumProcessModules): Fatal if not available.
(GetModuleFileNameExW): Ditto.
(GetModuleInformation): Ditto.
(GetProcessMemoryInfo): Ditto.
(QueryWorkingSet): Ditto.
(LsaRegisterLogonProcess): Ditto.
* fenv.cc (_feinitialise): Drop supports_sse condition.
* fhandler_disk_file.cc (path_conv::isgood_inode): Fix comment.
(fhandler_base::fstat_by_name): Drop has_fileid_dirinfo condition.
(fhandler_disk_file::opendir): Ditto.
* fhandler_netdrive.cc (fhandler_netdrive::readdir): Fix comment.
* fhandler_proc.cc (format_proc_partitions): Drop NT4-only code.
* fhandler_process.cc (get_process_state): Ditto.
* kernel32.cc (GetWindowsDirectoryW): Remove.
(GetWindowsDirectoryA): Remove.
* miscfuncs.cc (nice_to_winprio): Drop NT4-only code.
* mount.cc (fs_info::update): Fix comments.
* net.cc (get_2k_ifs): Drop NT4-only code.
* sec_auth.cc (get_logon_server): Ditto.
(lsaauth): Drop NT4-specific error handling.
* security.cc (alloc_sd): Set SE_DACL_PROTECTED unconditionally.
* select.cc (select_stuff::wait): Always use MWMO_INPUTAVAILABLE.
(peek_windows): Drop NT4-only condition in call to PeekMessage.
* syscalls.cc (gethostid): Remove NT4-only workaround.
* wincap.cc: Througout, drop has_dacl_protect,
has_broken_if_oper_status, has_process_io_counters,
has_terminal_services, has_extended_priority_class, has_guid_volumes,
has_fileid_dirinfo, has_mwmo_inputavailable and supports_sse from
wincaps.
(wincap_nt4sp4): Remove.
(wincap_minimal): Set to wincap_2000.
(wincapc::init): Rely on availability of OSVERSIONINFOEX structure.
Treat error from GetVersionEx as fatal. Treat NT4 as fatal.
* wincap.h (struct wincaps): Drop has_dacl_protect,
has_broken_if_oper_status, has_process_io_counters,
has_terminal_services, has_extended_priority_class, has_guid_volumes,
has_fileid_dirinfo, has_mwmo_inputavailable and supports_sse flags
and methods.
* winlean.h (GetWindowsDirectoryW) Define as GetSystemWindowsDirectoryW.
(GetWindowsDirectoryA): Define as GetSystemWindowsDirectoryA.
2011-04-04 20:23:36 +08:00
|
|
|
__seterrno_from_win_error (ret);
|
2007-07-20 22:29:43 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2008-07-11 18:00:36 +08:00
|
|
|
get_user_groups (WCHAR *logonserver, cygsidlist &grp_list,
|
|
|
|
PWCHAR user, PWCHAR domain)
|
2007-07-20 22:29:43 +08:00
|
|
|
{
|
2016-11-24 17:40:14 +08:00
|
|
|
WCHAR dgroup[MAX_DOMAIN_NAME_LEN + GNLEN + 2], *grp_p;
|
2007-07-20 22:29:43 +08:00
|
|
|
LPGROUP_USERS_INFO_0 buf;
|
2016-11-24 17:40:14 +08:00
|
|
|
DWORD cnt, tot;
|
2007-07-20 22:29:43 +08:00
|
|
|
NET_API_STATUS ret;
|
|
|
|
|
2014-10-22 03:17:09 +08:00
|
|
|
/* Look only on logonserver */
|
|
|
|
ret = NetUserGetGroups (logonserver, user, 0, (LPBYTE *) &buf,
|
|
|
|
MAX_PREFERRED_LENGTH, &cnt, &tot);
|
2007-07-20 22:29:43 +08:00
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
__seterrno_from_win_error (ret);
|
2013-06-10 23:33:12 +08:00
|
|
|
/* It's no error when the user name can't be found.
|
|
|
|
It's also no error if access has been denied. Yes, sounds weird, but
|
|
|
|
keep in mind that ERROR_ACCESS_DENIED means the current user has no
|
|
|
|
permission to access the AD user information. However, if we return
|
|
|
|
an error, Cygwin will call DsGetDcName with DS_FORCE_REDISCOVERY set
|
|
|
|
to ask for another server. This is not only time consuming, it's also
|
|
|
|
useless; the next server will return access denied again. */
|
|
|
|
return ret == NERR_UserNotFound || ret == ERROR_ACCESS_DENIED;
|
2007-07-20 22:29:43 +08:00
|
|
|
}
|
|
|
|
|
2016-11-24 17:40:14 +08:00
|
|
|
grp_p = wcpncpy (dgroup, domain, MAX_DOMAIN_NAME_LEN);
|
|
|
|
*grp_p++ = L'\\';
|
2007-07-20 22:29:43 +08:00
|
|
|
|
|
|
|
for (DWORD i = 0; i < cnt; ++i)
|
|
|
|
{
|
|
|
|
cygsid gsid;
|
2014-05-06 20:28:33 +08:00
|
|
|
DWORD glen = SECURITY_MAX_SID_SIZE;
|
2008-07-11 18:00:36 +08:00
|
|
|
WCHAR dom[MAX_DOMAIN_NAME_LEN + 1];
|
|
|
|
DWORD dlen = sizeof (dom);
|
2007-07-20 22:29:43 +08:00
|
|
|
SID_NAME_USE use = SidTypeInvalid;
|
|
|
|
|
2016-11-24 17:40:14 +08:00
|
|
|
*wcpncpy (grp_p, buf[i].grui0_name, sizeof dgroup / sizeof *dgroup
|
|
|
|
- (grp_p - dgroup) - 1) = L'\0';
|
2008-07-11 18:00:36 +08:00
|
|
|
if (!LookupAccountNameW (NULL, dgroup, gsid, &glen, dom, &dlen, &use))
|
|
|
|
debug_printf ("LookupAccountName(%W), %E", dgroup);
|
2011-04-04 17:00:02 +08:00
|
|
|
else if (well_known_sid_type (use))
|
|
|
|
grp_list *= gsid;
|
2007-07-20 22:29:43 +08:00
|
|
|
else if (legal_sid_type (use))
|
|
|
|
grp_list += gsid;
|
|
|
|
else
|
2013-04-23 17:44:36 +08:00
|
|
|
debug_printf ("Global group %W invalid. Use: %u", dgroup, use);
|
2007-07-20 22:29:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NetApiBufferFree (buf);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2019-02-22 20:36:13 +08:00
|
|
|
get_user_local_groups (PWCHAR domain, cygsidlist &grp_list, PWCHAR user)
|
2007-07-20 22:29:43 +08:00
|
|
|
{
|
|
|
|
LPLOCALGROUP_INFO_0 buf;
|
|
|
|
DWORD cnt, tot;
|
|
|
|
NET_API_STATUS ret;
|
|
|
|
|
2019-02-22 07:13:57 +08:00
|
|
|
/* We want to know the membership in local groups on the current machine.
|
|
|
|
Thus, don't ask the logonserver, ask the local machine. In contrast
|
|
|
|
to most other NetUser functions, NetUserGetLocalGroups accepts the
|
|
|
|
username in DOMAIN\user form. */
|
|
|
|
WCHAR username[MAX_DOMAIN_NAME_LEN + UNLEN + 2];
|
|
|
|
wcpcpy (wcpcpy (wcpcpy (username, domain), L"\\"), user);
|
|
|
|
ret = NetUserGetLocalGroups (NULL, username, 0, LG_INCLUDE_INDIRECT,
|
2014-10-22 03:17:09 +08:00
|
|
|
(LPBYTE *) &buf, MAX_PREFERRED_LENGTH,
|
|
|
|
&cnt, &tot);
|
2007-07-20 22:29:43 +08:00
|
|
|
if (ret)
|
|
|
|
{
|
2019-02-22 07:13:57 +08:00
|
|
|
debug_printf ("username: %W", username);
|
2007-07-20 22:29:43 +08:00
|
|
|
__seterrno_from_win_error (ret);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-07-11 18:00:36 +08:00
|
|
|
WCHAR domlocal_grp[MAX_DOMAIN_NAME_LEN + GNLEN + 2];
|
2010-02-13 01:40:42 +08:00
|
|
|
WCHAR builtin_grp[2 * GNLEN + 2];
|
|
|
|
PWCHAR dg_ptr, bg_ptr = NULL;
|
2007-07-20 22:29:43 +08:00
|
|
|
SID_NAME_USE use;
|
|
|
|
|
2008-07-11 18:00:36 +08:00
|
|
|
dg_ptr = wcpcpy (domlocal_grp, domain);
|
2008-07-09 19:58:38 +08:00
|
|
|
*dg_ptr++ = L'\\';
|
2007-07-20 22:29:43 +08:00
|
|
|
|
|
|
|
for (DWORD i = 0; i < cnt; ++i)
|
2009-02-21 00:10:45 +08:00
|
|
|
{
|
|
|
|
cygsid gsid;
|
2014-05-06 20:28:33 +08:00
|
|
|
DWORD glen = SECURITY_MAX_SID_SIZE;
|
2009-02-21 00:10:45 +08:00
|
|
|
WCHAR dom[MAX_DOMAIN_NAME_LEN + 1];
|
2010-02-13 01:40:42 +08:00
|
|
|
DWORD domlen = MAX_DOMAIN_NAME_LEN + 1;
|
2009-02-21 00:10:45 +08:00
|
|
|
|
|
|
|
use = SidTypeInvalid;
|
|
|
|
wcscpy (dg_ptr, buf[i].lgrpi0_name);
|
2009-02-24 02:27:48 +08:00
|
|
|
if (LookupAccountNameW (NULL, domlocal_grp, gsid, &glen,
|
|
|
|
dom, &domlen, &use))
|
2009-02-21 00:10:45 +08:00
|
|
|
{
|
2011-04-04 17:00:02 +08:00
|
|
|
if (well_known_sid_type (use))
|
|
|
|
grp_list *= gsid;
|
|
|
|
else if (legal_sid_type (use))
|
2009-02-24 02:27:48 +08:00
|
|
|
grp_list += gsid;
|
2011-04-04 17:00:02 +08:00
|
|
|
else
|
2013-04-23 17:44:36 +08:00
|
|
|
debug_printf ("Rejecting local %W. use: %u", dg_ptr, use);
|
2009-02-24 02:27:48 +08:00
|
|
|
}
|
|
|
|
else if (GetLastError () == ERROR_NONE_MAPPED)
|
|
|
|
{
|
|
|
|
/* Check if it's a builtin group. */
|
2010-02-13 01:40:42 +08:00
|
|
|
if (!bg_ptr)
|
2009-02-24 02:27:48 +08:00
|
|
|
{
|
2010-02-13 01:40:42 +08:00
|
|
|
/* Retrieve name of builtin group from system since it's
|
2011-06-06 13:02:13 +08:00
|
|
|
localized. */
|
2010-02-13 01:40:42 +08:00
|
|
|
glen = 2 * GNLEN + 2;
|
|
|
|
if (!LookupAccountSidW (NULL, well_known_builtin_sid,
|
|
|
|
builtin_grp, &glen, domain, &domlen, &use))
|
|
|
|
debug_printf ("LookupAccountSid(BUILTIN), %E");
|
2009-02-24 02:27:48 +08:00
|
|
|
else
|
2010-02-13 01:40:42 +08:00
|
|
|
{
|
|
|
|
bg_ptr = builtin_grp + wcslen (builtin_grp);
|
|
|
|
bg_ptr = wcpcpy (builtin_grp, L"\\");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (bg_ptr)
|
|
|
|
{
|
|
|
|
wcscpy (bg_ptr, dg_ptr);
|
2014-05-06 20:28:33 +08:00
|
|
|
glen = SECURITY_MAX_SID_SIZE;
|
2010-02-13 01:40:42 +08:00
|
|
|
domlen = MAX_DOMAIN_NAME_LEN + 1;
|
|
|
|
if (LookupAccountNameW (NULL, builtin_grp, gsid, &glen,
|
|
|
|
dom, &domlen, &use))
|
|
|
|
{
|
|
|
|
if (!legal_sid_type (use))
|
2013-04-23 17:44:36 +08:00
|
|
|
debug_printf ("Rejecting local %W. use: %u", dg_ptr, use);
|
2010-02-13 01:40:42 +08:00
|
|
|
else
|
|
|
|
grp_list *= gsid;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
debug_printf ("LookupAccountName(%W), %E", builtin_grp);
|
2009-02-24 02:27:48 +08:00
|
|
|
}
|
2009-02-21 00:10:45 +08:00
|
|
|
}
|
|
|
|
else
|
2009-02-24 02:27:48 +08:00
|
|
|
debug_printf ("LookupAccountName(%W), %E", domlocal_grp);
|
2009-02-21 00:10:45 +08:00
|
|
|
}
|
2007-07-20 22:29:43 +08:00
|
|
|
NetApiBufferFree (buf);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
sid_in_token_groups (PTOKEN_GROUPS grps, cygpsid sid)
|
|
|
|
{
|
|
|
|
if (!grps)
|
|
|
|
return false;
|
|
|
|
for (DWORD i = 0; i < grps->GroupCount; ++i)
|
|
|
|
if (sid == grps->Groups[i].Sid)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-02-17 23:40:27 +08:00
|
|
|
get_token_group_sidlist (cygsidlist &grp_list, PTOKEN_GROUPS my_grps)
|
2007-07-20 22:29:43 +08:00
|
|
|
{
|
|
|
|
if (my_grps)
|
|
|
|
{
|
|
|
|
grp_list += well_known_local_sid;
|
2011-10-16 00:31:57 +08:00
|
|
|
if (wincap.has_console_logon_sid ())
|
|
|
|
grp_list += well_known_console_logon_sid;
|
2007-07-20 22:29:43 +08:00
|
|
|
if (sid_in_token_groups (my_grps, well_known_dialup_sid))
|
|
|
|
grp_list *= well_known_dialup_sid;
|
|
|
|
if (sid_in_token_groups (my_grps, well_known_network_sid))
|
|
|
|
grp_list *= well_known_network_sid;
|
|
|
|
if (sid_in_token_groups (my_grps, well_known_batch_sid))
|
|
|
|
grp_list *= well_known_batch_sid;
|
|
|
|
grp_list *= well_known_interactive_sid;
|
2011-10-16 00:31:57 +08:00
|
|
|
#if 0
|
|
|
|
/* Don't add the SERVICE group when switching the user context.
|
|
|
|
That's much too dangerous, since the service group adds the
|
|
|
|
SE_IMPERSONATE_NAME privilege to the user. After all, the
|
|
|
|
process started with this token is not the service process
|
|
|
|
anymore anyway. */
|
2007-07-20 22:29:43 +08:00
|
|
|
if (sid_in_token_groups (my_grps, well_known_service_sid))
|
|
|
|
grp_list *= well_known_service_sid;
|
2011-10-16 00:31:57 +08:00
|
|
|
#endif
|
2007-07-20 22:29:43 +08:00
|
|
|
if (sid_in_token_groups (my_grps, well_known_this_org_sid))
|
|
|
|
grp_list *= well_known_this_org_sid;
|
2010-01-08 23:55:27 +08:00
|
|
|
grp_list *= well_known_users_sid;
|
2007-07-20 22:29:43 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
grp_list += well_known_local_sid;
|
|
|
|
grp_list *= well_known_interactive_sid;
|
2010-01-08 23:55:27 +08:00
|
|
|
grp_list *= well_known_users_sid;
|
2007-07-20 22:29:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-01-25 04:19:40 +08:00
|
|
|
get_server_groups (cygsidlist &grp_list, PSID usersid,
|
|
|
|
acct_disabled_chk_t check_account_disabled)
|
2007-07-20 22:29:43 +08:00
|
|
|
{
|
2008-07-11 18:00:36 +08:00
|
|
|
WCHAR user[UNLEN + 1];
|
|
|
|
WCHAR domain[MAX_DOMAIN_NAME_LEN + 1];
|
|
|
|
DWORD ulen = UNLEN + 1;
|
|
|
|
DWORD dlen = MAX_DOMAIN_NAME_LEN + 1;
|
|
|
|
SID_NAME_USE use;
|
2007-07-20 22:29:43 +08:00
|
|
|
|
|
|
|
if (well_known_system_sid == usersid)
|
|
|
|
{
|
|
|
|
grp_list *= well_known_admins_sid;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-07-11 18:00:36 +08:00
|
|
|
if (!LookupAccountSidW (NULL, usersid, user, &ulen, domain, &dlen, &use))
|
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
return false;
|
|
|
|
}
|
2014-05-14 19:27:47 +08:00
|
|
|
/* If the SID does NOT start with S-1-5-21, the domain is some builtin
|
2019-02-23 01:31:03 +08:00
|
|
|
domain. We don't fetch a group list then. */
|
2014-05-14 19:27:47 +08:00
|
|
|
if (sid_id_auth (usersid) == 5 /* SECURITY_NT_AUTHORITY */
|
2019-02-23 01:31:03 +08:00
|
|
|
&& sid_sub_auth (usersid, 0) == SECURITY_NT_NON_UNIQUE)
|
2014-05-23 00:40:13 +08:00
|
|
|
{
|
2019-02-23 01:31:03 +08:00
|
|
|
if (wincap.no_msv1_0_s4u_logon_in_wow64 ())
|
2019-01-24 23:22:49 +08:00
|
|
|
{
|
2019-02-23 01:31:03 +08:00
|
|
|
WCHAR server[INTERNET_MAX_HOST_NAME_LENGTH + 3];
|
|
|
|
|
|
|
|
if (!get_logon_server (domain, server, DS_IS_FLAT_NAME))
|
|
|
|
return false;
|
|
|
|
if (check_account_disabled == CHK_DISABLED)
|
2019-01-25 04:19:40 +08:00
|
|
|
{
|
2019-02-23 01:31:03 +08:00
|
|
|
NET_API_STATUS napi_stat;
|
|
|
|
USER_INFO_1 *ui1;
|
|
|
|
bool allow_user = false;
|
|
|
|
|
|
|
|
napi_stat = NetUserGetInfo (server, user, 1, (LPBYTE *) &ui1);
|
|
|
|
if (napi_stat == NERR_Success)
|
|
|
|
allow_user = !(ui1->usri1_flags & (UF_ACCOUNTDISABLE | UF_LOCKOUT));
|
|
|
|
if (ui1)
|
|
|
|
NetApiBufferFree (ui1);
|
|
|
|
if (!allow_user)
|
|
|
|
{
|
|
|
|
debug_printf ("User denied: %W\\%W", domain, user);
|
|
|
|
set_errno (EACCES);
|
|
|
|
return false;
|
|
|
|
}
|
2019-01-25 04:19:40 +08:00
|
|
|
}
|
2019-02-23 01:31:03 +08:00
|
|
|
grp_list *= well_known_world_sid;
|
|
|
|
grp_list *= well_known_authenticated_users_sid;
|
|
|
|
get_user_groups (server, grp_list, user, domain);
|
|
|
|
get_user_local_groups (domain, grp_list, user);
|
|
|
|
return true;
|
2019-01-24 23:22:49 +08:00
|
|
|
}
|
2019-02-23 01:31:03 +08:00
|
|
|
|
|
|
|
tmp_pathbuf tp;
|
|
|
|
HANDLE token;
|
|
|
|
NTSTATUS status;
|
|
|
|
PTOKEN_GROUPS groups;
|
|
|
|
ULONG size;
|
|
|
|
|
|
|
|
token = s4uauth (false, domain, user, status);
|
|
|
|
if (!token)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
groups = (PTOKEN_GROUPS) tp.w_get ();
|
|
|
|
status = NtQueryInformationToken (token, TokenGroups, groups,
|
|
|
|
2 * NT_MAX_PATH, &size);
|
|
|
|
if (NT_SUCCESS (status))
|
|
|
|
for (DWORD pg = 0; pg < groups->GroupCount; ++pg)
|
|
|
|
{
|
|
|
|
if (groups->Groups[pg].Attributes & SE_GROUP_USE_FOR_DENY_ONLY)
|
|
|
|
continue;
|
|
|
|
cygpsid grpsid = groups->Groups[pg].Sid;
|
|
|
|
if (sid_id_auth (grpsid) == 5 /* SECURITY_NT_AUTHORITY */
|
|
|
|
&& sid_sub_auth (grpsid, 0) == SECURITY_NT_NON_UNIQUE)
|
|
|
|
grp_list += grpsid;
|
|
|
|
else
|
|
|
|
grp_list *= grpsid;
|
|
|
|
}
|
|
|
|
NtClose (token);
|
2014-05-23 00:40:13 +08:00
|
|
|
}
|
2009-06-23 18:23:16 +08:00
|
|
|
return true;
|
2007-07-20 22:29:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2015-03-18 23:54:19 +08:00
|
|
|
get_initgroups_sidlist (cygsidlist &grp_list, PSID usersid, PSID pgrpsid,
|
2016-02-17 23:40:27 +08:00
|
|
|
PTOKEN_GROUPS my_grps)
|
2007-07-20 22:29:43 +08:00
|
|
|
{
|
2016-02-17 23:40:27 +08:00
|
|
|
if (well_known_system_sid != usersid)
|
|
|
|
get_token_group_sidlist (grp_list, my_grps);
|
2019-01-25 04:19:40 +08:00
|
|
|
if (!get_server_groups (grp_list, usersid, CHK_DISABLED))
|
2007-07-20 22:29:43 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
/* special_pgrp true if pgrpsid is not in normal groups */
|
|
|
|
grp_list += pgrpsid;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-01-25 04:19:40 +08:00
|
|
|
static bool
|
2015-03-18 23:54:19 +08:00
|
|
|
get_setgroups_sidlist (cygsidlist &tmp_list, PSID usersid,
|
2016-02-17 23:40:27 +08:00
|
|
|
PTOKEN_GROUPS my_grps, user_groups &groups)
|
2007-07-20 22:29:43 +08:00
|
|
|
{
|
2016-02-17 23:40:27 +08:00
|
|
|
get_token_group_sidlist (tmp_list, my_grps);
|
2019-01-25 04:19:40 +08:00
|
|
|
if (!get_server_groups (tmp_list, usersid, CHK_DISABLED))
|
|
|
|
return false;
|
2007-07-20 22:29:43 +08:00
|
|
|
for (int gidx = 0; gidx < groups.sgsids.count (); gidx++)
|
|
|
|
tmp_list += groups.sgsids.sids[gidx];
|
|
|
|
tmp_list += groups.pgsid;
|
2019-01-25 04:19:40 +08:00
|
|
|
return true;
|
2007-07-20 22:29:43 +08:00
|
|
|
}
|
|
|
|
|
2015-12-15 23:34:40 +08:00
|
|
|
/* Fixed size TOKEN_PRIVILEGES list to reflect privileges given to the
|
|
|
|
SYSTEM account by default. */
|
|
|
|
const struct
|
2007-07-20 22:29:43 +08:00
|
|
|
{
|
2015-12-15 23:34:40 +08:00
|
|
|
DWORD PrivilegeCount;
|
|
|
|
LUID_AND_ATTRIBUTES Privileges[28];
|
|
|
|
} sys_privs =
|
|
|
|
{
|
|
|
|
28,
|
|
|
|
{
|
|
|
|
{ { SE_CREATE_TOKEN_PRIVILEGE, 0 },
|
|
|
|
SE_PRIVILEGE_ENABLED | SE_PRIVILEGE_ENABLED_BY_DEFAULT },
|
|
|
|
{ { SE_ASSIGNPRIMARYTOKEN_PRIVILEGE, 0 },
|
|
|
|
SE_PRIVILEGE_ENABLED | SE_PRIVILEGE_ENABLED_BY_DEFAULT },
|
|
|
|
{ { SE_LOCK_MEMORY_PRIVILEGE, 0 },
|
|
|
|
SE_PRIVILEGE_ENABLED | SE_PRIVILEGE_ENABLED_BY_DEFAULT },
|
|
|
|
{ { SE_INCREASE_QUOTA_PRIVILEGE, 0 },
|
|
|
|
SE_PRIVILEGE_ENABLED | SE_PRIVILEGE_ENABLED_BY_DEFAULT },
|
|
|
|
{ { SE_TCB_PRIVILEGE, 0 },
|
|
|
|
SE_PRIVILEGE_ENABLED | SE_PRIVILEGE_ENABLED_BY_DEFAULT },
|
|
|
|
{ { SE_SECURITY_PRIVILEGE, 0 },
|
|
|
|
SE_PRIVILEGE_ENABLED | SE_PRIVILEGE_ENABLED_BY_DEFAULT },
|
|
|
|
{ { SE_TAKE_OWNERSHIP_PRIVILEGE, 0 },
|
|
|
|
SE_PRIVILEGE_ENABLED | SE_PRIVILEGE_ENABLED_BY_DEFAULT },
|
|
|
|
{ { SE_LOAD_DRIVER_PRIVILEGE, 0 },
|
|
|
|
SE_PRIVILEGE_ENABLED | SE_PRIVILEGE_ENABLED_BY_DEFAULT },
|
|
|
|
{ { SE_SYSTEM_PROFILE_PRIVILEGE, 0 },
|
|
|
|
SE_PRIVILEGE_ENABLED | SE_PRIVILEGE_ENABLED_BY_DEFAULT },
|
|
|
|
{ { SE_SYSTEMTIME_PRIVILEGE, 0 },
|
|
|
|
SE_PRIVILEGE_ENABLED | SE_PRIVILEGE_ENABLED_BY_DEFAULT },
|
|
|
|
{ { SE_PROF_SINGLE_PROCESS_PRIVILEGE, 0 },
|
|
|
|
SE_PRIVILEGE_ENABLED | SE_PRIVILEGE_ENABLED_BY_DEFAULT },
|
|
|
|
{ { SE_INC_BASE_PRIORITY_PRIVILEGE, 0 },
|
|
|
|
SE_PRIVILEGE_ENABLED | SE_PRIVILEGE_ENABLED_BY_DEFAULT },
|
|
|
|
{ { SE_CREATE_PAGEFILE_PRIVILEGE, 0 },
|
|
|
|
SE_PRIVILEGE_ENABLED | SE_PRIVILEGE_ENABLED_BY_DEFAULT },
|
|
|
|
{ { SE_CREATE_PERMANENT_PRIVILEGE, 0 },
|
|
|
|
SE_PRIVILEGE_ENABLED | SE_PRIVILEGE_ENABLED_BY_DEFAULT },
|
|
|
|
{ { SE_BACKUP_PRIVILEGE, 0 },
|
|
|
|
SE_PRIVILEGE_ENABLED | SE_PRIVILEGE_ENABLED_BY_DEFAULT },
|
|
|
|
{ { SE_RESTORE_PRIVILEGE, 0 },
|
|
|
|
SE_PRIVILEGE_ENABLED | SE_PRIVILEGE_ENABLED_BY_DEFAULT },
|
|
|
|
{ { SE_SHUTDOWN_PRIVILEGE, 0 },
|
|
|
|
SE_PRIVILEGE_ENABLED | SE_PRIVILEGE_ENABLED_BY_DEFAULT },
|
|
|
|
{ { SE_DEBUG_PRIVILEGE, 0 },
|
|
|
|
SE_PRIVILEGE_ENABLED | SE_PRIVILEGE_ENABLED_BY_DEFAULT },
|
|
|
|
{ { SE_AUDIT_PRIVILEGE, 0 },
|
|
|
|
SE_PRIVILEGE_ENABLED | SE_PRIVILEGE_ENABLED_BY_DEFAULT },
|
|
|
|
{ { SE_SYSTEM_ENVIRONMENT_PRIVILEGE, 0 },
|
|
|
|
SE_PRIVILEGE_ENABLED | SE_PRIVILEGE_ENABLED_BY_DEFAULT },
|
|
|
|
{ { SE_CHANGE_NOTIFY_PRIVILEGE, 0 },
|
|
|
|
SE_PRIVILEGE_ENABLED | SE_PRIVILEGE_ENABLED_BY_DEFAULT },
|
|
|
|
{ { SE_UNDOCK_PRIVILEGE, 0 },
|
|
|
|
SE_PRIVILEGE_ENABLED | SE_PRIVILEGE_ENABLED_BY_DEFAULT },
|
|
|
|
{ { SE_MANAGE_VOLUME_PRIVILEGE, 0 },
|
|
|
|
SE_PRIVILEGE_ENABLED | SE_PRIVILEGE_ENABLED_BY_DEFAULT },
|
|
|
|
{ { SE_IMPERSONATE_PRIVILEGE, 0 },
|
|
|
|
SE_PRIVILEGE_ENABLED | SE_PRIVILEGE_ENABLED_BY_DEFAULT },
|
|
|
|
{ { SE_CREATE_GLOBAL_PRIVILEGE, 0 },
|
|
|
|
SE_PRIVILEGE_ENABLED | SE_PRIVILEGE_ENABLED_BY_DEFAULT },
|
|
|
|
{ { SE_INCREASE_WORKING_SET_PRIVILEGE, 0 },
|
|
|
|
SE_PRIVILEGE_ENABLED | SE_PRIVILEGE_ENABLED_BY_DEFAULT },
|
|
|
|
{ { SE_TIME_ZONE_PRIVILEGE, 0 },
|
|
|
|
SE_PRIVILEGE_ENABLED | SE_PRIVILEGE_ENABLED_BY_DEFAULT },
|
|
|
|
{ { SE_CREATE_SYMBOLIC_LINK_PRIVILEGE, 0 },
|
|
|
|
SE_PRIVILEGE_ENABLED | SE_PRIVILEGE_ENABLED_BY_DEFAULT }
|
|
|
|
}
|
|
|
|
};
|
2007-07-20 22:29:43 +08:00
|
|
|
|
|
|
|
static PTOKEN_PRIVILEGES
|
|
|
|
get_priv_list (LSA_HANDLE lsa, cygsid &usersid, cygsidlist &grp_list,
|
2011-10-16 00:31:57 +08:00
|
|
|
size_t &size, cygpsid *mandatory_integrity_sid)
|
2007-07-20 22:29:43 +08:00
|
|
|
{
|
|
|
|
PLSA_UNICODE_STRING privstrs;
|
|
|
|
ULONG cnt;
|
|
|
|
PTOKEN_PRIVILEGES privs = NULL;
|
|
|
|
|
|
|
|
if (usersid == well_known_system_sid)
|
2011-10-16 00:31:57 +08:00
|
|
|
{
|
|
|
|
if (mandatory_integrity_sid)
|
|
|
|
*mandatory_integrity_sid = mandatory_system_integrity_sid;
|
2015-12-15 23:34:40 +08:00
|
|
|
return (PTOKEN_PRIVILEGES) &sys_privs;
|
2011-10-16 00:31:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mandatory_integrity_sid)
|
|
|
|
*mandatory_integrity_sid = mandatory_medium_integrity_sid;
|
2007-07-20 22:29:43 +08:00
|
|
|
|
|
|
|
for (int grp = -1; grp < grp_list.count (); ++grp)
|
|
|
|
{
|
|
|
|
if (grp == -1)
|
|
|
|
{
|
2011-12-22 19:02:36 +08:00
|
|
|
if (LsaEnumerateAccountRights (lsa, usersid, &privstrs, &cnt)
|
|
|
|
!= STATUS_SUCCESS)
|
2007-07-20 22:29:43 +08:00
|
|
|
continue;
|
|
|
|
}
|
2011-12-22 19:02:36 +08:00
|
|
|
else if (LsaEnumerateAccountRights (lsa, grp_list.sids[grp],
|
|
|
|
&privstrs, &cnt) != STATUS_SUCCESS)
|
2007-07-20 22:29:43 +08:00
|
|
|
continue;
|
|
|
|
for (ULONG i = 0; i < cnt; ++i)
|
|
|
|
{
|
|
|
|
LUID priv;
|
|
|
|
PTOKEN_PRIVILEGES tmp;
|
|
|
|
DWORD tmp_count;
|
2011-10-16 00:31:57 +08:00
|
|
|
bool high_integrity;
|
2007-07-20 22:29:43 +08:00
|
|
|
|
2011-10-16 00:31:57 +08:00
|
|
|
if (!privilege_luid (privstrs[i].Buffer, priv, high_integrity))
|
2007-07-20 22:29:43 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (privs)
|
|
|
|
{
|
|
|
|
DWORD pcnt = privs->PrivilegeCount;
|
|
|
|
LUID_AND_ATTRIBUTES *p = privs->Privileges;
|
|
|
|
for (; pcnt > 0; --pcnt, ++p)
|
|
|
|
if (priv.HighPart == p->Luid.HighPart
|
|
|
|
&& priv.LowPart == p->Luid.LowPart)
|
|
|
|
goto next_account_right;
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp_count = privs ? privs->PrivilegeCount : 0;
|
|
|
|
size = sizeof (DWORD)
|
|
|
|
+ (tmp_count + 1) * sizeof (LUID_AND_ATTRIBUTES);
|
|
|
|
tmp = (PTOKEN_PRIVILEGES) realloc (privs, size);
|
|
|
|
if (!tmp)
|
|
|
|
{
|
|
|
|
if (privs)
|
|
|
|
free (privs);
|
|
|
|
LsaFreeMemory (privstrs);
|
|
|
|
debug_printf ("realloc (privs) failed.");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
tmp->PrivilegeCount = tmp_count;
|
|
|
|
privs = tmp;
|
|
|
|
privs->Privileges[privs->PrivilegeCount].Luid = priv;
|
|
|
|
privs->Privileges[privs->PrivilegeCount].Attributes =
|
|
|
|
SE_PRIVILEGE_ENABLED | SE_PRIVILEGE_ENABLED_BY_DEFAULT;
|
|
|
|
++privs->PrivilegeCount;
|
2011-10-16 00:31:57 +08:00
|
|
|
if (mandatory_integrity_sid && high_integrity)
|
|
|
|
*mandatory_integrity_sid = mandatory_high_integrity_sid;
|
2007-07-20 22:29:43 +08:00
|
|
|
|
|
|
|
next_account_right:
|
|
|
|
;
|
|
|
|
}
|
|
|
|
LsaFreeMemory (privstrs);
|
|
|
|
}
|
|
|
|
return privs;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Accept a token if
|
|
|
|
- the requested usersid matches the TokenUser and
|
|
|
|
- if setgroups has been called:
|
|
|
|
the token groups that are listed in /etc/group match the union of
|
|
|
|
the requested primary and supplementary groups in gsids.
|
|
|
|
- else the (unknown) implicitly requested supplementary groups and those
|
|
|
|
in the token are the groups associated with the usersid. We assume
|
|
|
|
they match and verify only the primary groups.
|
|
|
|
The requested primary group must appear in the token.
|
|
|
|
The primary group in the token is a group associated with the usersid,
|
|
|
|
except if the token is internal and the group is in the token SD
|
|
|
|
(see create_token). In that latter case that group must match the
|
|
|
|
requested primary group. */
|
|
|
|
bool
|
|
|
|
verify_token (HANDLE token, cygsid &usersid, user_groups &groups, bool *pintern)
|
|
|
|
{
|
2011-04-29 15:34:05 +08:00
|
|
|
NTSTATUS status;
|
|
|
|
ULONG size;
|
2007-07-20 22:29:43 +08:00
|
|
|
bool intern = false;
|
2016-03-24 00:40:24 +08:00
|
|
|
tmp_pathbuf tp;
|
2007-07-20 22:29:43 +08:00
|
|
|
|
|
|
|
if (pintern)
|
|
|
|
{
|
|
|
|
TOKEN_SOURCE ts;
|
2011-04-29 15:34:05 +08:00
|
|
|
status = NtQueryInformationToken (token, TokenSource, &ts, sizeof ts,
|
|
|
|
&size);
|
|
|
|
if (!NT_SUCCESS (status))
|
2013-04-23 17:44:36 +08:00
|
|
|
debug_printf ("NtQueryInformationToken(), %y", status);
|
2007-07-20 22:29:43 +08:00
|
|
|
else
|
|
|
|
*pintern = intern = !memcmp (ts.SourceName, "Cygwin.1", 8);
|
|
|
|
}
|
|
|
|
/* Verify usersid */
|
2014-02-17 00:02:18 +08:00
|
|
|
cygsid tok_usersid (NO_SID);
|
2011-04-29 15:34:05 +08:00
|
|
|
status = NtQueryInformationToken (token, TokenUser, &tok_usersid,
|
|
|
|
sizeof tok_usersid, &size);
|
|
|
|
if (!NT_SUCCESS (status))
|
2013-04-23 17:44:36 +08:00
|
|
|
debug_printf ("NtQueryInformationToken(), %y", status);
|
2007-07-20 22:29:43 +08:00
|
|
|
if (usersid != tok_usersid)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* For an internal token, if setgroups was not called and if the sd group
|
|
|
|
is not well_known_null_sid, it must match pgrpsid */
|
|
|
|
if (intern && !groups.issetgroups ())
|
|
|
|
{
|
2014-05-06 20:28:33 +08:00
|
|
|
const DWORD sd_buf_siz = SECURITY_MAX_SID_SIZE
|
|
|
|
+ sizeof (SECURITY_DESCRIPTOR);
|
2007-07-20 22:29:43 +08:00
|
|
|
PSECURITY_DESCRIPTOR sd_buf = (PSECURITY_DESCRIPTOR) alloca (sd_buf_siz);
|
|
|
|
cygpsid gsid (NO_SID);
|
2011-04-01 16:41:26 +08:00
|
|
|
NTSTATUS status;
|
|
|
|
status = NtQuerySecurityObject (token, GROUP_SECURITY_INFORMATION,
|
|
|
|
sd_buf, sd_buf_siz, &size);
|
|
|
|
if (!NT_SUCCESS (status))
|
2013-04-23 17:44:36 +08:00
|
|
|
debug_printf ("NtQuerySecurityObject(), %y", status);
|
2011-04-28 17:53:11 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
BOOLEAN dummy;
|
|
|
|
status = RtlGetGroupSecurityDescriptor (sd_buf, (PSID *) &gsid,
|
|
|
|
&dummy);
|
|
|
|
if (!NT_SUCCESS (status))
|
2013-04-23 17:44:36 +08:00
|
|
|
debug_printf ("RtlGetGroupSecurityDescriptor(), %y", status);
|
2011-04-28 17:53:11 +08:00
|
|
|
}
|
2007-07-20 22:29:43 +08:00
|
|
|
if (well_known_null_sid != gsid)
|
|
|
|
return gsid == groups.pgsid;
|
|
|
|
}
|
|
|
|
|
2016-03-24 00:40:24 +08:00
|
|
|
PTOKEN_GROUPS my_grps = (PTOKEN_GROUPS) tp.w_get ();
|
2011-04-29 15:34:05 +08:00
|
|
|
|
2016-03-24 00:40:24 +08:00
|
|
|
status = NtQueryInformationToken (token, TokenGroups, my_grps,
|
|
|
|
2 * NT_MAX_PATH, &size);
|
2011-04-29 15:34:05 +08:00
|
|
|
if (!NT_SUCCESS (status))
|
|
|
|
{
|
2013-04-23 17:44:36 +08:00
|
|
|
debug_printf ("NtQueryInformationToken(my_token, TokenGroups), %y",
|
2011-04-29 15:34:05 +08:00
|
|
|
status);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool sawpg = false;
|
|
|
|
|
|
|
|
if (groups.issetgroups ()) /* setgroups was called */
|
|
|
|
{
|
2014-02-10 03:44:56 +08:00
|
|
|
cygpsid gsid;
|
2011-04-29 15:34:05 +08:00
|
|
|
bool saw[groups.sgsids.count ()];
|
|
|
|
|
2014-02-10 03:44:56 +08:00
|
|
|
/* Check that all groups in the setgroups () list are in the token.
|
|
|
|
A token created through ADVAPI should be allowed to contain more
|
2015-12-16 04:43:33 +08:00
|
|
|
groups than requested through setgroups(), especially since the
|
|
|
|
addition of integrity groups. */
|
2014-02-10 03:44:56 +08:00
|
|
|
memset (saw, 0, sizeof(saw));
|
|
|
|
for (int gidx = 0; gidx < groups.sgsids.count (); gidx++)
|
|
|
|
{
|
|
|
|
gsid = groups.sgsids.sids[gidx];
|
|
|
|
if (sid_in_token_groups (my_grps, gsid))
|
|
|
|
{
|
|
|
|
int pos = groups.sgsids.position (gsid);
|
|
|
|
if (pos >= 0)
|
|
|
|
saw[pos] = true;
|
|
|
|
else if (groups.pgsid == gsid)
|
|
|
|
sawpg = true;
|
|
|
|
}
|
|
|
|
}
|
2011-04-29 15:34:05 +08:00
|
|
|
/* user.sgsids groups must be in the token, except for builtin groups.
|
|
|
|
These can be different on domain member machines compared to
|
|
|
|
domain controllers, so these builtin groups may be validly missing
|
|
|
|
from a token created through password or lsaauth logon. */
|
|
|
|
for (int gidx = 0; gidx < groups.sgsids.count (); gidx++)
|
|
|
|
if (!saw[gidx]
|
|
|
|
&& !groups.sgsids.sids[gidx].is_well_known_sid ()
|
|
|
|
&& !sid_in_token_groups (my_grps, groups.sgsids.sids[gidx]))
|
|
|
|
return false;
|
2007-07-20 22:29:43 +08:00
|
|
|
}
|
2011-04-29 15:34:05 +08:00
|
|
|
/* The primary group must be in the token */
|
|
|
|
return sawpg
|
|
|
|
|| sid_in_token_groups (my_grps, groups.pgsid)
|
|
|
|
|| groups.pgsid == usersid;
|
2007-07-20 22:29:43 +08:00
|
|
|
}
|
|
|
|
|
2019-01-27 01:33:41 +08:00
|
|
|
const char *
|
|
|
|
account_restriction (NTSTATUS status)
|
|
|
|
{
|
|
|
|
const char *type;
|
|
|
|
|
|
|
|
switch (status)
|
|
|
|
{
|
|
|
|
case STATUS_INVALID_LOGON_HOURS:
|
|
|
|
type = "Logon outside allowed hours";
|
|
|
|
break;
|
|
|
|
case STATUS_INVALID_WORKSTATION:
|
|
|
|
type = "Logon at this machine not allowed";
|
|
|
|
break;
|
|
|
|
case STATUS_PASSWORD_EXPIRED:
|
|
|
|
type = "Password expired";
|
|
|
|
break;
|
|
|
|
case STATUS_ACCOUNT_DISABLED:
|
|
|
|
type = "Account disabled";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
type = "Unknown";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
2007-07-20 22:29:43 +08:00
|
|
|
HANDLE
|
2015-03-18 23:54:19 +08:00
|
|
|
create_token (cygsid &usersid, user_groups &new_groups)
|
2007-07-20 22:29:43 +08:00
|
|
|
{
|
2011-04-01 16:41:26 +08:00
|
|
|
NTSTATUS status;
|
2014-03-29 04:34:04 +08:00
|
|
|
LSA_HANDLE lsa = NULL;
|
2007-07-20 22:29:43 +08:00
|
|
|
|
|
|
|
cygsidlist tmp_gsids (cygsidlist_auto, 12);
|
|
|
|
|
|
|
|
SECURITY_QUALITY_OF_SERVICE sqos =
|
|
|
|
{ sizeof sqos, SecurityImpersonation, SECURITY_STATIC_TRACKING, FALSE };
|
|
|
|
OBJECT_ATTRIBUTES oa = { sizeof oa, 0, 0, 0, 0, &sqos };
|
2016-02-17 23:40:27 +08:00
|
|
|
/* Up to Windows 7, when using a authwentication LUID other than "Anonymous",
|
|
|
|
Windows whoami prints the wrong username, the one from the login session,
|
|
|
|
not the one from the actual user token of the process. This is apparently
|
|
|
|
fixed in Windows 8. However, starting with Windows 8, access rights of
|
|
|
|
the anonymous logon session is further restricted. Therefore we create
|
|
|
|
the new user token with the authentication id of the local service
|
|
|
|
account. Hopefully that's sufficient. */
|
|
|
|
const LUID auth_luid_7 = ANONYMOUS_LOGON_LUID;
|
|
|
|
const LUID auth_luid_8 = LOCALSERVICE_LUID;
|
|
|
|
LUID auth_luid = wincap.has_broken_whoami () ? auth_luid_7 : auth_luid_8;
|
2007-07-20 22:29:43 +08:00
|
|
|
LARGE_INTEGER exp = { QuadPart:INT64_MAX };
|
|
|
|
|
|
|
|
TOKEN_USER user;
|
|
|
|
PTOKEN_GROUPS new_tok_gsids = NULL;
|
|
|
|
PTOKEN_PRIVILEGES privs = NULL;
|
|
|
|
TOKEN_OWNER owner;
|
|
|
|
TOKEN_PRIMARY_GROUP pgrp;
|
|
|
|
TOKEN_DEFAULT_DACL dacl = {};
|
|
|
|
TOKEN_SOURCE source;
|
|
|
|
TOKEN_STATISTICS stats;
|
|
|
|
memcpy (source.SourceName, "Cygwin.1", 8);
|
|
|
|
source.SourceIdentifier.HighPart = 0;
|
|
|
|
source.SourceIdentifier.LowPart = 0x0101;
|
|
|
|
|
2019-01-26 02:58:21 +08:00
|
|
|
HANDLE token = NULL;
|
|
|
|
HANDLE primary_token = NULL;
|
2007-07-20 22:29:43 +08:00
|
|
|
|
2016-03-24 00:40:24 +08:00
|
|
|
tmp_pathbuf tp;
|
2007-07-20 22:29:43 +08:00
|
|
|
PTOKEN_GROUPS my_tok_gsids = NULL;
|
2011-10-16 00:31:57 +08:00
|
|
|
cygpsid mandatory_integrity_sid;
|
2011-04-29 15:34:05 +08:00
|
|
|
ULONG size;
|
2007-07-20 22:29:43 +08:00
|
|
|
size_t psize = 0;
|
|
|
|
|
|
|
|
/* SE_CREATE_TOKEN_NAME privilege needed to call NtCreateToken. */
|
|
|
|
push_self_privilege (SE_CREATE_TOKEN_PRIVILEGE, true);
|
|
|
|
|
|
|
|
/* Open policy object. */
|
2014-01-24 01:02:30 +08:00
|
|
|
if (!(lsa = lsa_open_policy (NULL, POLICY_EXECUTE)))
|
2007-07-20 22:29:43 +08:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* User, owner, primary group. */
|
|
|
|
user.User.Sid = usersid;
|
|
|
|
user.User.Attributes = 0;
|
|
|
|
owner.Owner = usersid;
|
|
|
|
|
|
|
|
/* Retrieve authentication id and group list from own process. */
|
|
|
|
if (hProcToken)
|
|
|
|
{
|
|
|
|
/* Switching user context to SYSTEM doesn't inherit the authentication
|
|
|
|
id of the user account running current process. */
|
2008-09-11 12:34:24 +08:00
|
|
|
if (usersid == well_known_system_sid)
|
|
|
|
/* nothing to do */;
|
|
|
|
else
|
2011-04-29 15:34:05 +08:00
|
|
|
{
|
|
|
|
status = NtQueryInformationToken (hProcToken, TokenStatistics,
|
|
|
|
&stats, sizeof stats, &size);
|
|
|
|
if (!NT_SUCCESS (status))
|
|
|
|
debug_printf ("NtQueryInformationToken(hProcToken, "
|
2013-04-23 17:44:36 +08:00
|
|
|
"TokenStatistics), %y", status);
|
2011-04-29 15:34:05 +08:00
|
|
|
}
|
2007-07-20 22:29:43 +08:00
|
|
|
|
|
|
|
/* Retrieving current processes group list to be able to inherit
|
|
|
|
some important well known group sids. */
|
2016-03-24 00:40:24 +08:00
|
|
|
my_tok_gsids = (PTOKEN_GROUPS) tp.w_get ();
|
|
|
|
status = NtQueryInformationToken (hProcToken, TokenGroups, my_tok_gsids,
|
|
|
|
2 * NT_MAX_PATH, &size);
|
|
|
|
if (!NT_SUCCESS (status))
|
2007-07-20 22:29:43 +08:00
|
|
|
{
|
2016-03-24 00:40:24 +08:00
|
|
|
debug_printf ("NtQueryInformationToken(hProcToken, TokenGroups), "
|
|
|
|
"%y", status);
|
|
|
|
my_tok_gsids = NULL;
|
2007-07-20 22:29:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create list of groups, the user is member in. */
|
|
|
|
if (new_groups.issetgroups ())
|
2019-01-25 04:19:40 +08:00
|
|
|
{
|
|
|
|
if (!get_setgroups_sidlist (tmp_gsids, usersid, my_tok_gsids, new_groups))
|
|
|
|
goto out;
|
|
|
|
}
|
2015-03-18 23:54:19 +08:00
|
|
|
else if (!get_initgroups_sidlist (tmp_gsids, usersid, new_groups.pgsid,
|
2016-02-17 23:40:27 +08:00
|
|
|
my_tok_gsids))
|
2007-07-20 22:29:43 +08:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* Primary group. */
|
|
|
|
pgrp.PrimaryGroup = new_groups.pgsid;
|
|
|
|
|
|
|
|
/* Create a TOKEN_GROUPS list from the above retrieved list of sids. */
|
|
|
|
new_tok_gsids = (PTOKEN_GROUPS)
|
2008-04-22 18:45:34 +08:00
|
|
|
alloca (sizeof (DWORD) + (tmp_gsids.count () + 1)
|
2007-07-20 22:29:43 +08:00
|
|
|
* sizeof (SID_AND_ATTRIBUTES));
|
|
|
|
new_tok_gsids->GroupCount = tmp_gsids.count ();
|
|
|
|
for (DWORD i = 0; i < new_tok_gsids->GroupCount; ++i)
|
|
|
|
{
|
|
|
|
new_tok_gsids->Groups[i].Sid = tmp_gsids.sids[i];
|
|
|
|
new_tok_gsids->Groups[i].Attributes = SE_GROUP_MANDATORY
|
|
|
|
| SE_GROUP_ENABLED_BY_DEFAULT
|
|
|
|
| SE_GROUP_ENABLED;
|
|
|
|
}
|
2008-04-22 18:45:34 +08:00
|
|
|
|
2011-10-16 00:31:57 +08:00
|
|
|
/* Retrieve list of privileges of that user. Based on the usersid and
|
|
|
|
the returned privileges, get_priv_list sets the mandatory_integrity_sid
|
|
|
|
pointer to the correct MIC SID for UAC. */
|
|
|
|
if (!(privs = get_priv_list (lsa, usersid, tmp_gsids, psize,
|
|
|
|
&mandatory_integrity_sid)))
|
|
|
|
goto out;
|
|
|
|
|
2015-12-15 21:58:52 +08:00
|
|
|
new_tok_gsids->Groups[new_tok_gsids->GroupCount].Attributes =
|
|
|
|
SE_GROUP_INTEGRITY | SE_GROUP_INTEGRITY_ENABLED;
|
|
|
|
new_tok_gsids->Groups[new_tok_gsids->GroupCount++].Sid
|
|
|
|
= mandatory_integrity_sid;
|
2008-04-22 18:45:34 +08:00
|
|
|
|
2007-07-20 22:29:43 +08:00
|
|
|
/* Let's be heroic... */
|
2011-04-01 16:41:26 +08:00
|
|
|
status = NtCreateToken (&token, TOKEN_ALL_ACCESS, &oa, TokenImpersonation,
|
|
|
|
&auth_luid, &exp, &user, new_tok_gsids, privs, &owner,
|
|
|
|
&pgrp, &dacl, &source);
|
|
|
|
if (status)
|
|
|
|
__seterrno_from_nt_status (status);
|
2007-07-20 22:29:43 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Convert to primary token. */
|
|
|
|
if (!DuplicateTokenEx (token, MAXIMUM_ALLOWED, &sec_none,
|
|
|
|
SecurityImpersonation, TokenPrimary,
|
|
|
|
&primary_token))
|
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
debug_printf ("DuplicateTokenEx %E");
|
2019-01-26 02:58:21 +08:00
|
|
|
primary_token = NULL;
|
2007-07-20 22:29:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
pop_self_privilege ();
|
|
|
|
if (token != INVALID_HANDLE_VALUE)
|
|
|
|
CloseHandle (token);
|
2017-01-09 21:02:19 +08:00
|
|
|
if (privs && privs != (PTOKEN_PRIVILEGES) &sys_privs)
|
2007-07-20 22:29:43 +08:00
|
|
|
free (privs);
|
2014-01-24 01:02:30 +08:00
|
|
|
lsa_close_policy (lsa);
|
2007-07-20 22:29:43 +08:00
|
|
|
|
2008-07-11 18:00:36 +08:00
|
|
|
debug_printf ("%p = create_token ()", primary_token);
|
2007-07-20 22:29:43 +08:00
|
|
|
return primary_token;
|
|
|
|
}
|
|
|
|
|
2019-02-19 04:00:59 +08:00
|
|
|
#if 0 && S4U_RUNS_FINE
|
2007-07-20 22:29:43 +08:00
|
|
|
HANDLE
|
2015-03-18 23:54:19 +08:00
|
|
|
lsaauth (cygsid &usersid, user_groups &new_groups)
|
2007-07-20 22:29:43 +08:00
|
|
|
{
|
|
|
|
cygsidlist tmp_gsids (cygsidlist_auto, 12);
|
|
|
|
cygpsid pgrpsid;
|
|
|
|
LSA_STRING name;
|
2014-03-29 04:34:04 +08:00
|
|
|
HANDLE lsa_hdl = NULL, lsa = NULL;
|
2007-07-20 22:29:43 +08:00
|
|
|
LSA_OPERATIONAL_MODE sec_mode;
|
2011-04-01 16:41:26 +08:00
|
|
|
NTSTATUS status, sub_status;
|
2007-07-20 22:29:43 +08:00
|
|
|
ULONG package_id, size;
|
|
|
|
struct {
|
|
|
|
LSA_STRING str;
|
|
|
|
CHAR buf[16];
|
|
|
|
} origin;
|
2008-07-11 18:00:36 +08:00
|
|
|
DWORD ulen = UNLEN + 1;
|
|
|
|
DWORD dlen = MAX_DOMAIN_NAME_LEN + 1;
|
|
|
|
SID_NAME_USE use;
|
2007-07-20 22:29:43 +08:00
|
|
|
cyglsa_t *authinf = NULL;
|
|
|
|
ULONG authinf_size;
|
|
|
|
TOKEN_SOURCE ts;
|
|
|
|
PCYG_TOKEN_GROUPS gsids = NULL;
|
|
|
|
PTOKEN_PRIVILEGES privs = NULL;
|
|
|
|
PACL dacl = NULL;
|
|
|
|
PVOID profile = NULL;
|
|
|
|
LUID luid;
|
|
|
|
QUOTA_LIMITS quota;
|
|
|
|
size_t psize = 0, gsize = 0, dsize = 0;
|
|
|
|
OFFSET offset, sids_offset;
|
|
|
|
int tmpidx, non_well_known_cnt;
|
|
|
|
|
|
|
|
HANDLE user_token = NULL;
|
|
|
|
|
|
|
|
push_self_privilege (SE_TCB_PRIVILEGE, true);
|
|
|
|
|
|
|
|
/* Register as logon process. */
|
2014-12-02 18:04:53 +08:00
|
|
|
RtlInitAnsiString (&name, "Cygwin");
|
2011-04-01 16:41:26 +08:00
|
|
|
status = LsaRegisterLogonProcess (&name, &lsa_hdl, &sec_mode);
|
|
|
|
if (status != STATUS_SUCCESS)
|
2007-07-20 22:29:43 +08:00
|
|
|
{
|
2013-04-23 17:44:36 +08:00
|
|
|
debug_printf ("LsaRegisterLogonProcess: %y", status);
|
Drop NT4 support.
* autoload.cc (DnsQuery_A): Fatal if not available.
(DnsRecordListFree): Ditto.
(DsGetDcNameW): Ditto.
(NetGetAnyDCName): Remove.
(NetGetDCName): Remove.
(EnumProcessModules): Fatal if not available.
(GetModuleFileNameExW): Ditto.
(GetModuleInformation): Ditto.
(GetProcessMemoryInfo): Ditto.
(QueryWorkingSet): Ditto.
(LsaRegisterLogonProcess): Ditto.
* fenv.cc (_feinitialise): Drop supports_sse condition.
* fhandler_disk_file.cc (path_conv::isgood_inode): Fix comment.
(fhandler_base::fstat_by_name): Drop has_fileid_dirinfo condition.
(fhandler_disk_file::opendir): Ditto.
* fhandler_netdrive.cc (fhandler_netdrive::readdir): Fix comment.
* fhandler_proc.cc (format_proc_partitions): Drop NT4-only code.
* fhandler_process.cc (get_process_state): Ditto.
* kernel32.cc (GetWindowsDirectoryW): Remove.
(GetWindowsDirectoryA): Remove.
* miscfuncs.cc (nice_to_winprio): Drop NT4-only code.
* mount.cc (fs_info::update): Fix comments.
* net.cc (get_2k_ifs): Drop NT4-only code.
* sec_auth.cc (get_logon_server): Ditto.
(lsaauth): Drop NT4-specific error handling.
* security.cc (alloc_sd): Set SE_DACL_PROTECTED unconditionally.
* select.cc (select_stuff::wait): Always use MWMO_INPUTAVAILABLE.
(peek_windows): Drop NT4-only condition in call to PeekMessage.
* syscalls.cc (gethostid): Remove NT4-only workaround.
* wincap.cc: Througout, drop has_dacl_protect,
has_broken_if_oper_status, has_process_io_counters,
has_terminal_services, has_extended_priority_class, has_guid_volumes,
has_fileid_dirinfo, has_mwmo_inputavailable and supports_sse from
wincaps.
(wincap_nt4sp4): Remove.
(wincap_minimal): Set to wincap_2000.
(wincapc::init): Rely on availability of OSVERSIONINFOEX structure.
Treat error from GetVersionEx as fatal. Treat NT4 as fatal.
* wincap.h (struct wincaps): Drop has_dacl_protect,
has_broken_if_oper_status, has_process_io_counters,
has_terminal_services, has_extended_priority_class, has_guid_volumes,
has_fileid_dirinfo, has_mwmo_inputavailable and supports_sse flags
and methods.
* winlean.h (GetWindowsDirectoryW) Define as GetSystemWindowsDirectoryW.
(GetWindowsDirectoryA): Define as GetSystemWindowsDirectoryA.
2011-04-04 20:23:36 +08:00
|
|
|
__seterrno_from_nt_status (status);
|
2007-07-20 22:29:43 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
/* Get handle to our own LSA package. */
|
2014-12-02 18:04:53 +08:00
|
|
|
RtlInitAnsiString (&name, CYG_LSA_PKGNAME);
|
2011-04-01 16:41:26 +08:00
|
|
|
status = LsaLookupAuthenticationPackage (lsa_hdl, &name, &package_id);
|
|
|
|
if (status != STATUS_SUCCESS)
|
2007-07-20 22:29:43 +08:00
|
|
|
{
|
2013-04-23 17:44:36 +08:00
|
|
|
debug_printf ("LsaLookupAuthenticationPackage: %y", status);
|
2011-04-01 16:41:26 +08:00
|
|
|
__seterrno_from_nt_status (status);
|
2007-07-20 22:29:43 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open policy object. */
|
2014-01-24 01:02:30 +08:00
|
|
|
if (!(lsa = lsa_open_policy (NULL, POLICY_EXECUTE)))
|
2007-07-20 22:29:43 +08:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* Create origin. */
|
2014-12-02 18:04:53 +08:00
|
|
|
stpcpy (origin.buf, "Cygwin");
|
|
|
|
RtlInitAnsiString (&origin.str, origin.buf);
|
2007-07-20 22:29:43 +08:00
|
|
|
/* Create token source. */
|
|
|
|
memcpy (ts.SourceName, "Cygwin.1", 8);
|
|
|
|
ts.SourceIdentifier.HighPart = 0;
|
|
|
|
ts.SourceIdentifier.LowPart = 0x0103;
|
|
|
|
|
|
|
|
/* Create list of groups, the user is member in. */
|
|
|
|
if (new_groups.issetgroups ())
|
2019-01-25 04:19:40 +08:00
|
|
|
{
|
|
|
|
if (!get_setgroups_sidlist (tmp_gsids, usersid, NULL, new_groups))
|
|
|
|
goto out;
|
|
|
|
}
|
2015-03-18 23:54:19 +08:00
|
|
|
else if (!get_initgroups_sidlist (tmp_gsids, usersid, new_groups.pgsid,
|
2016-02-17 23:40:27 +08:00
|
|
|
NULL))
|
2007-07-20 22:29:43 +08:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
tmp_gsids.debug_print ("tmp_gsids");
|
|
|
|
|
|
|
|
/* Evaluate size of TOKEN_GROUPS list */
|
|
|
|
non_well_known_cnt = tmp_gsids.non_well_known_count ();
|
|
|
|
gsize = sizeof (DWORD) + non_well_known_cnt * sizeof (SID_AND_ATTRIBUTES);
|
|
|
|
tmpidx = -1;
|
|
|
|
for (int i = 0; i < non_well_known_cnt; ++i)
|
|
|
|
if ((tmpidx = tmp_gsids.next_non_well_known_sid (tmpidx)) >= 0)
|
2011-04-28 16:34:49 +08:00
|
|
|
gsize += RtlLengthSid (tmp_gsids.sids[tmpidx]);
|
2007-07-20 22:29:43 +08:00
|
|
|
|
2011-10-16 00:31:57 +08:00
|
|
|
/* Retrieve list of privileges of that user. The MIC SID is created by
|
|
|
|
the LSA here. */
|
|
|
|
if (!(privs = get_priv_list (lsa, usersid, tmp_gsids, psize, NULL)))
|
2007-07-20 22:29:43 +08:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* Create DefaultDacl. */
|
|
|
|
dsize = sizeof (ACL) + 3 * sizeof (ACCESS_ALLOWED_ACE)
|
2011-04-28 16:34:49 +08:00
|
|
|
+ RtlLengthSid (usersid)
|
|
|
|
+ RtlLengthSid (well_known_admins_sid)
|
|
|
|
+ RtlLengthSid (well_known_system_sid);
|
2007-07-20 22:29:43 +08:00
|
|
|
dacl = (PACL) alloca (dsize);
|
2011-04-28 17:30:36 +08:00
|
|
|
if (!NT_SUCCESS (RtlCreateAcl (dacl, dsize, ACL_REVISION)))
|
2007-07-20 22:29:43 +08:00
|
|
|
goto out;
|
2011-04-29 18:38:12 +08:00
|
|
|
if (!NT_SUCCESS (RtlAddAccessAllowedAce (dacl, ACL_REVISION, GENERIC_ALL,
|
|
|
|
usersid)))
|
2007-07-20 22:29:43 +08:00
|
|
|
goto out;
|
2011-04-29 18:38:12 +08:00
|
|
|
if (!NT_SUCCESS (RtlAddAccessAllowedAce (dacl, ACL_REVISION, GENERIC_ALL,
|
|
|
|
well_known_admins_sid)))
|
2007-07-20 22:29:43 +08:00
|
|
|
goto out;
|
2011-04-29 18:38:12 +08:00
|
|
|
if (!NT_SUCCESS (RtlAddAccessAllowedAce (dacl, ACL_REVISION, GENERIC_ALL,
|
|
|
|
well_known_system_sid)))
|
2007-07-20 22:29:43 +08:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* Evaluate authinf size and allocate authinf. */
|
|
|
|
authinf_size = (authinf->data - (PBYTE) authinf);
|
2011-04-28 16:34:49 +08:00
|
|
|
authinf_size += RtlLengthSid (usersid); /* User SID */
|
2007-07-20 22:29:43 +08:00
|
|
|
authinf_size += gsize; /* Groups + Group SIDs */
|
|
|
|
/* When trying to define the admins group as primary group on Vista,
|
|
|
|
LsaLogonUser fails with error STATUS_INVALID_OWNER. As workaround
|
2008-07-11 02:01:25 +08:00
|
|
|
we define "Local" as primary group here. Seteuid32 sets the primary
|
|
|
|
group to the group set in /etc/passwd anyway. */
|
|
|
|
if (new_groups.pgsid == well_known_admins_sid)
|
|
|
|
pgrpsid = well_known_local_sid;
|
|
|
|
else
|
|
|
|
pgrpsid = new_groups.pgsid;
|
|
|
|
|
2011-04-28 16:34:49 +08:00
|
|
|
authinf_size += RtlLengthSid (pgrpsid); /* Primary Group SID */
|
2007-07-20 22:29:43 +08:00
|
|
|
|
|
|
|
authinf_size += psize; /* Privileges */
|
|
|
|
authinf_size += 0; /* Owner SID */
|
|
|
|
authinf_size += dsize; /* Default DACL */
|
|
|
|
|
|
|
|
authinf = (cyglsa_t *) alloca (authinf_size);
|
|
|
|
authinf->inf_size = authinf_size - ((PBYTE) &authinf->inf - (PBYTE) authinf);
|
|
|
|
|
|
|
|
authinf->magic = CYG_LSA_MAGIC;
|
|
|
|
|
2008-07-11 18:00:36 +08:00
|
|
|
if (!LookupAccountSidW (NULL, usersid, authinf->username, &ulen,
|
|
|
|
authinf->domain, &dlen, &use))
|
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
goto out;
|
|
|
|
}
|
2007-07-20 22:29:43 +08:00
|
|
|
|
|
|
|
/* Store stuff in authinf with offset relative to start of "inf" member,
|
|
|
|
instead of using pointers. */
|
|
|
|
offset = authinf->data - (PBYTE) &authinf->inf;
|
|
|
|
|
|
|
|
authinf->inf.ExpirationTime.LowPart = 0xffffffffL;
|
|
|
|
authinf->inf.ExpirationTime.HighPart = 0x7fffffffL;
|
|
|
|
/* User SID */
|
|
|
|
authinf->inf.User.User.Sid = offset;
|
|
|
|
authinf->inf.User.User.Attributes = 0;
|
2011-04-29 18:38:12 +08:00
|
|
|
RtlCopySid (RtlLengthSid (usersid), (PSID) ((PBYTE) &authinf->inf + offset),
|
|
|
|
usersid);
|
2011-04-28 16:34:49 +08:00
|
|
|
offset += RtlLengthSid (usersid);
|
2007-07-20 22:29:43 +08:00
|
|
|
/* Groups */
|
|
|
|
authinf->inf.Groups = offset;
|
|
|
|
gsids = (PCYG_TOKEN_GROUPS) ((PBYTE) &authinf->inf + offset);
|
|
|
|
sids_offset = offset + sizeof (ULONG) + non_well_known_cnt
|
|
|
|
* sizeof (SID_AND_ATTRIBUTES);
|
|
|
|
gsids->GroupCount = non_well_known_cnt;
|
|
|
|
/* Group SIDs */
|
|
|
|
tmpidx = -1;
|
|
|
|
for (int i = 0; i < non_well_known_cnt; ++i)
|
|
|
|
{
|
|
|
|
if ((tmpidx = tmp_gsids.next_non_well_known_sid (tmpidx)) < 0)
|
|
|
|
break;
|
|
|
|
gsids->Groups[i].Sid = sids_offset;
|
|
|
|
gsids->Groups[i].Attributes = SE_GROUP_MANDATORY
|
|
|
|
| SE_GROUP_ENABLED_BY_DEFAULT
|
|
|
|
| SE_GROUP_ENABLED;
|
2011-04-29 18:38:12 +08:00
|
|
|
RtlCopySid (RtlLengthSid (tmp_gsids.sids[tmpidx]),
|
|
|
|
(PSID) ((PBYTE) &authinf->inf + sids_offset),
|
|
|
|
tmp_gsids.sids[tmpidx]);
|
2011-04-28 16:34:49 +08:00
|
|
|
sids_offset += RtlLengthSid (tmp_gsids.sids[tmpidx]);
|
2007-07-20 22:29:43 +08:00
|
|
|
}
|
|
|
|
offset += gsize;
|
|
|
|
/* Primary Group SID */
|
|
|
|
authinf->inf.PrimaryGroup.PrimaryGroup = offset;
|
2011-04-29 18:38:12 +08:00
|
|
|
RtlCopySid (RtlLengthSid (pgrpsid), (PSID) ((PBYTE) &authinf->inf + offset),
|
|
|
|
pgrpsid);
|
2011-04-28 16:34:49 +08:00
|
|
|
offset += RtlLengthSid (pgrpsid);
|
2007-07-20 22:29:43 +08:00
|
|
|
/* Privileges */
|
|
|
|
authinf->inf.Privileges = offset;
|
|
|
|
memcpy ((PBYTE) &authinf->inf + offset, privs, psize);
|
|
|
|
offset += psize;
|
|
|
|
/* Owner */
|
|
|
|
authinf->inf.Owner.Owner = 0;
|
|
|
|
/* Default DACL */
|
|
|
|
authinf->inf.DefaultDacl.DefaultDacl = offset;
|
|
|
|
memcpy ((PBYTE) &authinf->inf + offset, dacl, dsize);
|
|
|
|
|
2010-01-30 03:51:11 +08:00
|
|
|
authinf->checksum = CYG_LSA_MAGIC;
|
2008-09-11 12:34:24 +08:00
|
|
|
PDWORD csp;
|
|
|
|
PDWORD csp_end;
|
|
|
|
csp = (PDWORD) &authinf->username;
|
|
|
|
csp_end = (PDWORD) ((PBYTE) authinf + authinf_size);
|
2007-07-20 22:29:43 +08:00
|
|
|
while (csp < csp_end)
|
|
|
|
authinf->checksum += *csp++;
|
|
|
|
|
|
|
|
/* Try to logon... */
|
2011-04-01 16:41:26 +08:00
|
|
|
status = LsaLogonUser (lsa_hdl, (PLSA_STRING) &origin, Interactive,
|
|
|
|
package_id, authinf, authinf_size, NULL, &ts,
|
|
|
|
&profile, &size, &luid, &user_token, "a,
|
|
|
|
&sub_status);
|
|
|
|
if (status != STATUS_SUCCESS)
|
2007-07-20 22:29:43 +08:00
|
|
|
{
|
2019-01-27 01:33:41 +08:00
|
|
|
if (status == STATUS_ACCOUNT_RESTRICTION)
|
|
|
|
debug_printf ("Cygwin LSA Auth LsaLogonUser failed: %y (%s)",
|
|
|
|
status, account_restriction (sub_status));
|
|
|
|
else
|
|
|
|
debug_printf ("Cygwin LSA Auth LsaLogonUser failed: %y", status);
|
2011-04-01 16:41:26 +08:00
|
|
|
__seterrno_from_nt_status (status);
|
2007-07-20 22:29:43 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (profile)
|
2008-07-11 02:01:25 +08:00
|
|
|
{
|
|
|
|
#ifdef JUST_ANOTHER_NONWORKING_SOLUTION
|
|
|
|
/* See ../lsaauth/cyglsa.c. */
|
|
|
|
cygprf_t *prf = (cygprf_t *) profile;
|
|
|
|
if (prf->magic_pre == MAGIC_PRE && prf->magic_post == MAGIC_POST
|
|
|
|
&& prf->token)
|
|
|
|
{
|
|
|
|
CloseHandle (user_token);
|
|
|
|
user_token = prf->token;
|
|
|
|
system_printf ("Got token through profile: %p", user_token);
|
|
|
|
}
|
|
|
|
#endif /* JUST_ANOTHER_NONWORKING_SOLUTION */
|
|
|
|
LsaFreeReturnBuffer (profile);
|
|
|
|
}
|
2010-02-06 21:13:15 +08:00
|
|
|
user_token = get_full_privileged_inheritable_token (user_token);
|
2008-06-10 23:16:01 +08:00
|
|
|
|
2007-07-20 22:29:43 +08:00
|
|
|
out:
|
2017-01-09 21:02:19 +08:00
|
|
|
if (privs && privs != (PTOKEN_PRIVILEGES) &sys_privs)
|
2007-07-20 22:29:43 +08:00
|
|
|
free (privs);
|
2014-01-24 01:02:30 +08:00
|
|
|
lsa_close_policy (lsa);
|
2007-07-20 22:29:43 +08:00
|
|
|
if (lsa_hdl)
|
|
|
|
LsaDeregisterLogonProcess (lsa_hdl);
|
|
|
|
pop_self_privilege ();
|
|
|
|
|
2008-07-11 18:00:36 +08:00
|
|
|
debug_printf ("%p = lsaauth ()", user_token);
|
2007-07-20 22:29:43 +08:00
|
|
|
return user_token;
|
|
|
|
}
|
2019-01-27 20:32:44 +08:00
|
|
|
#endif
|
2008-11-26 18:18:10 +08:00
|
|
|
|
|
|
|
#define SFU_LSA_KEY_SUFFIX L"_microsoft_sfu_utility"
|
|
|
|
|
|
|
|
HANDLE
|
|
|
|
lsaprivkeyauth (struct passwd *pw)
|
|
|
|
{
|
|
|
|
NTSTATUS status;
|
2014-03-29 04:34:04 +08:00
|
|
|
HANDLE lsa = NULL;
|
2008-11-26 18:18:10 +08:00
|
|
|
HANDLE token = NULL;
|
|
|
|
WCHAR sid[256];
|
|
|
|
WCHAR domain[MAX_DOMAIN_NAME_LEN + 1];
|
|
|
|
WCHAR user[UNLEN + 1];
|
|
|
|
WCHAR key_name[MAX_DOMAIN_NAME_LEN + UNLEN + wcslen (SFU_LSA_KEY_SUFFIX) + 2];
|
|
|
|
UNICODE_STRING key;
|
2014-05-20 18:21:34 +08:00
|
|
|
PUNICODE_STRING data = NULL;
|
2008-11-26 18:18:10 +08:00
|
|
|
cygsid psid;
|
2014-05-20 18:21:34 +08:00
|
|
|
BOOL ret;
|
2008-11-26 18:18:10 +08:00
|
|
|
|
|
|
|
push_self_privilege (SE_TCB_PRIVILEGE, true);
|
|
|
|
|
|
|
|
/* Open policy object. */
|
2014-01-24 01:02:30 +08:00
|
|
|
if (!(lsa = lsa_open_policy (NULL, POLICY_GET_PRIVATE_INFORMATION)))
|
2008-11-26 18:18:10 +08:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* Needed for Interix key and LogonUser. */
|
|
|
|
extract_nt_dom_user (pw, domain, user);
|
|
|
|
|
|
|
|
/* First test for a Cygwin entry. */
|
|
|
|
if (psid.getfrompw (pw) && psid.string (sid))
|
|
|
|
{
|
|
|
|
wcpcpy (wcpcpy (key_name, CYGWIN_LSA_KEY_PREFIX), sid);
|
|
|
|
RtlInitUnicodeString (&key, key_name);
|
|
|
|
status = LsaRetrievePrivateData (lsa, &key, &data);
|
|
|
|
if (!NT_SUCCESS (status))
|
2014-05-20 18:21:34 +08:00
|
|
|
data = NULL;
|
2008-11-26 18:18:10 +08:00
|
|
|
}
|
2014-05-20 18:21:34 +08:00
|
|
|
/* No Cygwin key, try Interix key. */
|
|
|
|
if (!data && *domain)
|
2008-11-26 18:18:10 +08:00
|
|
|
{
|
2014-05-20 18:21:34 +08:00
|
|
|
__small_swprintf (key_name, L"%W_%W%W",
|
|
|
|
domain, user, SFU_LSA_KEY_SUFFIX);
|
|
|
|
RtlInitUnicodeString (&key, key_name);
|
|
|
|
status = LsaRetrievePrivateData (lsa, &key, &data);
|
|
|
|
if (!NT_SUCCESS (status))
|
|
|
|
data = NULL;
|
2008-11-26 18:18:10 +08:00
|
|
|
}
|
2014-05-20 18:21:34 +08:00
|
|
|
/* Found an entry? Try to logon. */
|
|
|
|
if (data)
|
|
|
|
{
|
|
|
|
/* The key is not 0-terminated. */
|
|
|
|
PWCHAR passwd;
|
|
|
|
size_t pwdsize = data->Length + sizeof (WCHAR);
|
|
|
|
|
|
|
|
passwd = (PWCHAR) alloca (pwdsize);
|
|
|
|
*wcpncpy (passwd, data->Buffer, data->Length / sizeof (WCHAR)) = L'\0';
|
|
|
|
/* Weird: LsaFreeMemory invalidates the content of the UNICODE_STRING
|
|
|
|
structure, but it does not invalidate the Buffer content. */
|
|
|
|
RtlSecureZeroMemory (data->Buffer, data->Length);
|
|
|
|
LsaFreeMemory (data);
|
|
|
|
debug_printf ("Try logon for %W\\%W", domain, user);
|
|
|
|
ret = LogonUserW (user, domain, passwd, LOGON32_LOGON_INTERACTIVE,
|
|
|
|
LOGON32_PROVIDER_DEFAULT, &token);
|
|
|
|
RtlSecureZeroMemory (passwd, pwdsize);
|
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
token = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
token = get_full_privileged_inheritable_token (token);
|
|
|
|
}
|
|
|
|
lsa_close_policy (lsa);
|
2008-11-26 18:18:10 +08:00
|
|
|
|
|
|
|
out:
|
|
|
|
pop_self_privilege ();
|
|
|
|
return token;
|
|
|
|
}
|
2019-01-27 01:33:41 +08:00
|
|
|
|
|
|
|
/* The following code is inspired by the generate_s4u_user_token
|
|
|
|
and lookup_principal_name functions from
|
|
|
|
https://github.com/PowerShell/openssh-portable
|
|
|
|
|
|
|
|
Thanks guys! For courtesy here's the original copyright disclaimer: */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Author: Manoj Ampalam <manoj.ampalam@microsoft.com>
|
|
|
|
* Utilities to generate user tokens
|
|
|
|
*
|
|
|
|
* Author: Bryan Berns <berns@uwalumni.com>
|
|
|
|
* Updated s4u, logon, and profile loading routines to use
|
|
|
|
* normalized login names.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2015 Microsoft Corp.
|
|
|
|
* All rights reserved
|
|
|
|
*
|
|
|
|
* Microsoft openssh win32 port
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* In Mingw-w64, MsV1_0S4ULogon and MSV1_0_S4U_LOGON are only defined
|
2019-02-23 01:27:38 +08:00
|
|
|
in ddk/ntifs.h. We can't include this. */
|
2019-01-27 01:33:41 +08:00
|
|
|
|
|
|
|
#define MsV1_0S4ULogon ((MSV1_0_LOGON_SUBMIT_TYPE) 12)
|
|
|
|
|
|
|
|
typedef struct _MSV1_0_S4U_LOGON
|
|
|
|
{
|
|
|
|
MSV1_0_LOGON_SUBMIT_TYPE MessageType;
|
|
|
|
ULONG Flags;
|
|
|
|
UNICODE_STRING UserPrincipalName;
|
|
|
|
UNICODE_STRING DomainName;
|
|
|
|
} MSV1_0_S4U_LOGON, *PMSV1_0_S4U_LOGON;
|
|
|
|
|
2019-02-23 01:27:38 +08:00
|
|
|
/* Missing in Mingw-w64 */
|
|
|
|
#define KERB_S4U_LOGON_FLAG_IDENTITY 0x08
|
|
|
|
|
|
|
|
/* If logon is true we need an impersonation token. Otherwise we just
|
|
|
|
need an identification token, e. g. to fetch the group list. */
|
2019-01-27 01:33:41 +08:00
|
|
|
HANDLE
|
2019-02-23 01:27:38 +08:00
|
|
|
s4uauth (bool logon, PCWSTR domain, PCWSTR user, NTSTATUS &ret_status)
|
2019-01-27 01:33:41 +08:00
|
|
|
{
|
|
|
|
LSA_STRING name;
|
|
|
|
HANDLE lsa_hdl = NULL;
|
|
|
|
LSA_OPERATIONAL_MODE sec_mode;
|
|
|
|
NTSTATUS status, sub_status;
|
2019-03-01 23:04:51 +08:00
|
|
|
bool kerberos_auth;
|
2019-01-27 01:33:41 +08:00
|
|
|
ULONG package_id, size;
|
|
|
|
struct {
|
|
|
|
LSA_STRING str;
|
|
|
|
CHAR buf[16];
|
|
|
|
} origin;
|
|
|
|
|
|
|
|
tmp_pathbuf tp;
|
|
|
|
PVOID authinf = NULL;
|
|
|
|
ULONG authinf_size;
|
|
|
|
TOKEN_SOURCE ts;
|
|
|
|
PKERB_INTERACTIVE_PROFILE profile = NULL;
|
|
|
|
LUID luid;
|
|
|
|
QUOTA_LIMITS quota;
|
|
|
|
HANDLE token = NULL;
|
|
|
|
|
|
|
|
push_self_privilege (SE_TCB_PRIVILEGE, true);
|
|
|
|
|
2019-02-23 01:27:38 +08:00
|
|
|
if (logon)
|
|
|
|
{
|
|
|
|
/* Register as logon process. */
|
2019-03-01 23:04:51 +08:00
|
|
|
debug_printf ("Impersonation requested");
|
2019-02-23 01:27:38 +08:00
|
|
|
RtlInitAnsiString (&name, "Cygwin");
|
|
|
|
status = LsaRegisterLogonProcess (&name, &lsa_hdl, &sec_mode);
|
|
|
|
}
|
|
|
|
else
|
2019-03-01 23:04:51 +08:00
|
|
|
{
|
|
|
|
/* Connect untrusted to just create a identification token */
|
|
|
|
debug_printf ("Identification requested");
|
|
|
|
status = LsaConnectUntrusted (&lsa_hdl);
|
|
|
|
}
|
2019-01-27 01:33:41 +08:00
|
|
|
if (status != STATUS_SUCCESS)
|
|
|
|
{
|
2019-02-23 01:27:38 +08:00
|
|
|
debug_printf ("%s: %y", logon ? "LsaRegisterLogonProcess"
|
|
|
|
: "LsaConnectUntrusted", status);
|
2019-01-27 01:33:41 +08:00
|
|
|
__seterrno_from_nt_status (status);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2019-03-01 23:04:51 +08:00
|
|
|
/* Check if this is a domain user. If so, use Kerberos. */
|
|
|
|
kerberos_auth = cygheap->dom.member_machine ()
|
|
|
|
&& wcscasecmp (domain, cygheap->dom.account_flat_name ());
|
|
|
|
debug_printf ("kerb %d, domain member %d, user domain <%W>, machine <%W>",
|
|
|
|
kerberos_auth, cygheap->dom.member_machine (), domain,
|
|
|
|
cygheap->dom.account_flat_name ());
|
|
|
|
|
|
|
|
/* Connect to authentication package. */
|
|
|
|
RtlInitAnsiString (&name, kerberos_auth ? MICROSOFT_KERBEROS_NAME_A
|
|
|
|
: MSV1_0_PACKAGE_NAME);
|
|
|
|
status = LsaLookupAuthenticationPackage (lsa_hdl, &name, &package_id);
|
|
|
|
if (status != STATUS_SUCCESS)
|
|
|
|
{
|
|
|
|
debug_printf ("LsaLookupAuthenticationPackage: %y", status);
|
|
|
|
__seterrno_from_nt_status (status);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2019-01-27 01:33:41 +08:00
|
|
|
/* Create origin. */
|
|
|
|
stpcpy (origin.buf, "Cygwin");
|
|
|
|
RtlInitAnsiString (&origin.str, origin.buf);
|
|
|
|
|
2019-03-01 23:04:51 +08:00
|
|
|
/* Create token source. */
|
|
|
|
memcpy (ts.SourceName, "Cygwin.1", 8);
|
|
|
|
ts.SourceIdentifier.HighPart = 0;
|
|
|
|
ts.SourceIdentifier.LowPart = kerberos_auth ? 0x0105 : 0x0106;
|
|
|
|
|
|
|
|
if (kerberos_auth)
|
2019-01-27 01:33:41 +08:00
|
|
|
{
|
|
|
|
PWCHAR sam_name = tp.w_get ();
|
|
|
|
PWCHAR upn_name = tp.w_get ();
|
|
|
|
size = NT_MAX_PATH;
|
|
|
|
KERB_S4U_LOGON *s4u_logon;
|
|
|
|
USHORT name_len;
|
|
|
|
|
|
|
|
wcpcpy (wcpcpy (wcpcpy (sam_name, domain), L"\\"), user);
|
|
|
|
if (TranslateNameW (sam_name, NameSamCompatible, NameUserPrincipal,
|
|
|
|
upn_name, &size) == 0)
|
|
|
|
{
|
|
|
|
PWCHAR translated_name = tp.w_get ();
|
|
|
|
PWCHAR p;
|
|
|
|
|
|
|
|
debug_printf ("TranslateNameW(%W, NameUserPrincipal) %E", sam_name);
|
|
|
|
size = NT_MAX_PATH;
|
|
|
|
if (TranslateNameW (sam_name, NameSamCompatible, NameCanonical,
|
|
|
|
translated_name, &size) == 0)
|
|
|
|
{
|
|
|
|
debug_printf ("TranslateNameW(%W, NameCanonical) %E", sam_name);
|
2019-03-01 23:04:51 +08:00
|
|
|
goto out;
|
2019-01-27 01:33:41 +08:00
|
|
|
}
|
|
|
|
p = wcschr (translated_name, L'/');
|
|
|
|
if (p)
|
|
|
|
*p = '\0';
|
|
|
|
wcpcpy (wcpcpy (wcpcpy (upn_name, user), L"@"), translated_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
name_len = wcslen (upn_name) * sizeof (WCHAR);
|
|
|
|
authinf_size = sizeof (KERB_S4U_LOGON) + name_len;
|
|
|
|
authinf = tp.c_get ();
|
|
|
|
RtlSecureZeroMemory (authinf, authinf_size);
|
|
|
|
s4u_logon = (KERB_S4U_LOGON *) authinf;
|
|
|
|
s4u_logon->MessageType = KerbS4ULogon;
|
2019-02-23 01:27:38 +08:00
|
|
|
s4u_logon->Flags = logon ? 0 : KERB_S4U_LOGON_FLAG_IDENTITY;
|
2019-01-27 01:33:41 +08:00
|
|
|
/* Append user to login info */
|
|
|
|
RtlInitEmptyUnicodeString (&s4u_logon->ClientUpn,
|
|
|
|
(PWCHAR) (s4u_logon + 1),
|
|
|
|
name_len);
|
|
|
|
RtlAppendUnicodeToString (&s4u_logon->ClientUpn, upn_name);
|
2019-03-01 23:04:51 +08:00
|
|
|
debug_printf ("KerbS4ULogon: ClientUpn: <%S>", &s4u_logon->ClientUpn);
|
2019-01-27 01:33:41 +08:00
|
|
|
}
|
2019-03-01 23:04:51 +08:00
|
|
|
else
|
2019-02-19 00:59:56 +08:00
|
|
|
{
|
2019-03-01 23:04:51 +08:00
|
|
|
/* Per MSDN MsV1_0S4ULogon is not implemented on Vista, but surprisingly
|
|
|
|
it works. */
|
|
|
|
MSV1_0_S4U_LOGON *s4u_logon;
|
|
|
|
USHORT user_len, domain_len;
|
|
|
|
|
|
|
|
user_len = wcslen (user) * sizeof (WCHAR);
|
|
|
|
domain_len = wcslen (domain) * sizeof (WCHAR); /* Local machine */
|
|
|
|
authinf_size = sizeof (MSV1_0_S4U_LOGON) + user_len + domain_len;
|
|
|
|
if (!authinf)
|
|
|
|
authinf = tp.c_get ();
|
|
|
|
RtlSecureZeroMemory (authinf, authinf_size);
|
|
|
|
s4u_logon = (MSV1_0_S4U_LOGON *) authinf;
|
|
|
|
s4u_logon->MessageType = MsV1_0S4ULogon;
|
|
|
|
s4u_logon->Flags = 0;
|
|
|
|
/* Append user and domain to login info */
|
|
|
|
RtlInitEmptyUnicodeString (&s4u_logon->UserPrincipalName,
|
|
|
|
(PWCHAR) (s4u_logon + 1),
|
|
|
|
user_len);
|
|
|
|
RtlInitEmptyUnicodeString (&s4u_logon->DomainName,
|
|
|
|
(PWCHAR) ((PBYTE) (s4u_logon + 1) + user_len),
|
|
|
|
domain_len);
|
|
|
|
RtlAppendUnicodeToString (&s4u_logon->UserPrincipalName, user);
|
|
|
|
RtlAppendUnicodeToString (&s4u_logon->DomainName, domain);
|
|
|
|
debug_printf ("MsV1_0S4ULogon: DomainName: <%S> UserPrincipalName: <%S>",
|
|
|
|
&s4u_logon->DomainName, &s4u_logon->UserPrincipalName);
|
2019-02-19 00:59:56 +08:00
|
|
|
}
|
2019-03-01 23:04:51 +08:00
|
|
|
|
|
|
|
/* Try to logon. */
|
|
|
|
status = LsaLogonUser (lsa_hdl, (PLSA_STRING) &origin, Network, package_id,
|
|
|
|
authinf, authinf_size, NULL, &ts, (PVOID *) &profile,
|
|
|
|
&size, &luid, &token, "a, &sub_status);
|
|
|
|
switch (status)
|
2019-01-27 01:33:41 +08:00
|
|
|
{
|
2019-03-01 23:04:51 +08:00
|
|
|
case STATUS_SUCCESS:
|
|
|
|
break;
|
|
|
|
case STATUS_ACCOUNT_RESTRICTION:
|
|
|
|
debug_printf ("%s S4U LsaLogonUser failed: %y (%s)",
|
|
|
|
kerberos_auth ? "Kerberos" : "MsV1_0", status,
|
|
|
|
account_restriction (sub_status));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
debug_printf ("%s S4U LsaLogonUser failed: %y",
|
|
|
|
kerberos_auth ? "Kerberos" : "MsV1_0", status);
|
|
|
|
break;
|
2019-01-27 01:33:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
if (lsa_hdl)
|
|
|
|
LsaDeregisterLogonProcess (lsa_hdl);
|
|
|
|
if (profile)
|
|
|
|
LsaFreeReturnBuffer (profile);
|
|
|
|
|
2019-02-23 01:27:38 +08:00
|
|
|
if (token && logon)
|
2019-02-19 04:45:34 +08:00
|
|
|
{
|
|
|
|
/* Convert to primary token. Strictly speaking this is only
|
|
|
|
required on Vista/2008. CreateProcessAsUser also takes
|
|
|
|
impersonation tokens since Windows 7. */
|
|
|
|
HANDLE tmp_token;
|
|
|
|
|
|
|
|
if (DuplicateTokenEx (token, MAXIMUM_ALLOWED, &sec_none,
|
|
|
|
SecurityImpersonation, TokenPrimary, &tmp_token))
|
|
|
|
{
|
|
|
|
CloseHandle (token);
|
|
|
|
token = tmp_token;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
debug_printf ("DuplicateTokenEx %E");
|
|
|
|
/* Make sure not to allow create_token. */
|
|
|
|
status = STATUS_INVALID_HANDLE;
|
|
|
|
CloseHandle (token);
|
|
|
|
token = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-27 01:33:41 +08:00
|
|
|
pop_self_privilege ();
|
2019-02-19 04:00:59 +08:00
|
|
|
ret_status = status;
|
2019-01-27 01:33:41 +08:00
|
|
|
return token;
|
|
|
|
}
|