Cygwin: passwd/group: store account name case correct, take 2

The solution from commit  9a3cc77b2a
didn't work for foreign domain accounts.  Rather than calling
LookupAccountSid we now use the info when we fetch it anyway
via LDAP or Net*GetInfo.  Only in case of domain groups we have
to add an LDAP call explicitly.

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
This commit is contained in:
Corinna Vinschen 2019-02-13 13:16:15 +01:00
parent 79c4e95fad
commit 4e34a39b5c
1 changed files with 23 additions and 18 deletions

View File

@ -1941,6 +1941,7 @@ pwdgrp::fetch_account_from_windows (fetch_user_arg_t &arg, cyg_ldap *pldap)
char *gecos = NULL; char *gecos = NULL;
/* Temporary stuff. */ /* Temporary stuff. */
PWCHAR p; PWCHAR p;
PWCHAR val;
WCHAR sidstr[128]; WCHAR sidstr[128];
ULONG posix_offset = 0; ULONG posix_offset = 0;
uint32_t id_val; uint32_t id_val;
@ -1977,8 +1978,6 @@ pwdgrp::fetch_account_from_windows (fetch_user_arg_t &arg, cyg_ldap *pldap)
can't be resolved, and if we're a domain member machine, ask a DC. can't be resolved, and if we're a domain member machine, ask a DC.
Do *not* use LookupAccountSidW. It can take ages when called on a Do *not* use LookupAccountSidW. It can take ages when called on a
DC for some weird reason. Use LDAP instead. */ DC for some weird reason. Use LDAP instead. */
PWCHAR val;
if (cldap->fetch_ad_account (sid, true) if (cldap->fetch_ad_account (sid, true)
&& (val = cldap->get_account_name ())) && (val = cldap->get_account_name ()))
{ {
@ -2046,16 +2045,6 @@ pwdgrp::fetch_account_from_windows (fetch_user_arg_t &arg, cyg_ldap *pldap)
/* We can skip the backslash in the rest of this function. */ /* We can skip the backslash in the rest of this function. */
if (p) if (p)
name = p + 1; name = p + 1;
/* Reverse lookup name from sid to make sure the username in
our passwd/group data is written exactly as in the user DB. */
nlen = UNLEN + 1;
dlen = DNLEN + 1;
ret = LookupAccountSidW (NULL, sid, name, &nlen, dom, &dlen, &acc_type);
if (!ret)
{
system_printf ("LookupAccountNameW (%W), %E", name);
return NULL;
}
/* Last but not least, some validity checks on the name style. */ /* Last but not least, some validity checks on the name style. */
if (!fq_name) if (!fq_name)
{ {
@ -2438,18 +2427,28 @@ pwdgrp::fetch_account_from_windows (fetch_user_arg_t &arg, cyg_ldap *pldap)
if (is_domain_account) if (is_domain_account)
{ {
/* Skip this when creating group entries and for non-users. */ /* Overwrite group name to be sure case is same as in SAM */
if (is_group()
&& cldap->fetch_ad_account (sid, true, domain)
&& (val = cldap->get_account_name ()))
wcscpy (name, val);
/* Skip the rest if creating group entries and for non-users. */
if (is_group() || acc_type != SidTypeUser) if (is_group() || acc_type != SidTypeUser)
break; break;
/* On AD machines, use LDAP to fetch domain account infos. */ /* On AD machines, use LDAP to fetch domain account infos. */
if (cygheap->dom.primary_dns_name ()) if (cygheap->dom.primary_dns_name ())
{ {
/* For the current user we got the primary group from the /* For the current user we got correctly cased username and
user token. For any other user we fetch it from AD. */ the primary group via process token. For any other user
we fetch it from AD and overwrite it. */
if (!is_current_user if (!is_current_user
&& cldap->fetch_ad_account (sid, false, domain) && cldap->fetch_ad_account (sid, false, domain))
&& (id_val = cldap->get_primary_gid ()) != ILLEGAL_GID) {
if ((val = cldap->get_account_name ()))
wcscpy (name, val);
if ((id_val = cldap->get_primary_gid ()) != ILLEGAL_GID)
gid = posix_offset + id_val; gid = posix_offset + id_val;
}
home = cygheap->pg.get_home (cldap, sid, dom, domain, home = cygheap->pg.get_home (cldap, sid, dom, domain,
name, fully_qualified_name); name, fully_qualified_name);
shell = cygheap->pg.get_shell (cldap, sid, dom, domain, shell = cygheap->pg.get_shell (cldap, sid, dom, domain,
@ -2487,6 +2486,8 @@ pwdgrp::fetch_account_from_windows (fetch_user_arg_t &arg, cyg_ldap *pldap)
debug_printf ("NetUserGetInfo(%W) %u", name, nas); debug_printf ("NetUserGetInfo(%W) %u", name, nas);
break; break;
} }
/* Overwrite name to be sure case is same as in SAM */
wcscpy (name, ui->usri3_name);
gid = posix_offset + ui->usri3_primary_group_id; gid = posix_offset + ui->usri3_primary_group_id;
home = cygheap->pg.get_home (ui, sid, dom, name, home = cygheap->pg.get_home (ui, sid, dom, name,
fully_qualified_name); fully_qualified_name);
@ -2513,6 +2514,8 @@ pwdgrp::fetch_account_from_windows (fetch_user_arg_t &arg, cyg_ldap *pldap)
debug_printf ("NetUserGetInfo(%W) %u", name, nas); debug_printf ("NetUserGetInfo(%W) %u", name, nas);
break; break;
} }
/* Overwrite name to be sure case is same as in SAM */
wcscpy (name, ui->usri3_name);
/* Fetch user attributes. */ /* Fetch user attributes. */
home = cygheap->pg.get_home (ui, sid, dom, name, home = cygheap->pg.get_home (ui, sid, dom, name,
fully_qualified_name); fully_qualified_name);
@ -2533,6 +2536,8 @@ pwdgrp::fetch_account_from_windows (fetch_user_arg_t &arg, cyg_ldap *pldap)
debug_printf ("NetLocalGroupGetInfo(%W) %u", name, nas); debug_printf ("NetLocalGroupGetInfo(%W) %u", name, nas);
break; break;
} }
/* Overwrite name to be sure case is same as in SAM */
wcscpy (name, gi->lgrpi1_name);
/* Fetch unix gid from comment field. */ /* Fetch unix gid from comment field. */
uxid = fetch_from_description (gi->lgrpi1_comment, uxid = fetch_from_description (gi->lgrpi1_comment,
L"unix=\"", 6); L"unix=\"", 6);