Skip to content

Commit cae4abc

Browse files
committed
Merge pull request #57 from rglidden/main
msys2-runtime: restore fast path for current user primary group
2 parents 070aa27 + 9ba6664 commit cae4abc

File tree

4 files changed

+46
-8
lines changed

4 files changed

+46
-8
lines changed

winsup/cygwin/include/sys/cygwin.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,8 @@ enum
219219
enum
220220
{
221221
NSS_SRC_FILES = 1,
222-
NSS_SRC_DB = 2
222+
NSS_SRC_DB = 2,
223+
NSS_SRC_DB_ACCURATE = 4
223224
};
224225

225226
/* Enumeration source constants for CW_SETENT called from mkpasswd/mkgroup. */

winsup/cygwin/local_includes/cygheap.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,7 @@ class cygheap_pwdgrp
406406
inline int nss_pwd_src () const { return pwd_src; } /* CW_GETNSS_PWD_SRC */
407407
inline bool nss_grp_files () const { return !!(grp_src & NSS_SRC_FILES); }
408408
inline bool nss_grp_db () const { return !!(grp_src & NSS_SRC_DB); }
409+
inline bool nss_grp_db_accurate () const { return !!(grp_src & NSS_SRC_DB_ACCURATE); }
409410
inline int nss_grp_src () const { return grp_src; } /* CW_GETNSS_GRP_SRC */
410411
inline bool nss_cygserver_caching () const { return caching; }
411412
inline void nss_disable_cygserver_caching () { caching = false; }

winsup/cygwin/uinfo.cc

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,11 @@ cygheap_pwdgrp::nss_init_line (const char *line)
637637
*src |= NSS_SRC_DB;
638638
c += 2;
639639
}
640+
else if (NSS_CMP ("db-accurate"))
641+
{
642+
*src |= NSS_SRC_DB | NSS_SRC_DB_ACCURATE;
643+
c += 11;
644+
}
640645
else
641646
{
642647
c += strcspn (c, " \t");
@@ -1952,6 +1957,7 @@ pwdgrp::fetch_account_from_windows (fetch_user_arg_t &arg, cyg_ldap *pldap)
19521957
gid_t gid = ILLEGAL_GID;
19531958
bool is_domain_account = true;
19541959
PCWSTR domain = NULL;
1960+
bool get_default_group_from_current_user_token = false;
19551961
char *shell = NULL;
19561962
char *home = NULL;
19571963
char *gecos = NULL;
@@ -2449,9 +2455,19 @@ pwdgrp::fetch_account_from_windows (fetch_user_arg_t &arg, cyg_ldap *pldap)
24492455
uid = posix_offset + sid_sub_auth_rid (sid);
24502456
if (!is_group () && acc_type == SidTypeUser)
24512457
{
2452-
/* Default primary group. Make the educated guess that the user
2453-
is in group "Domain Users" or "None". */
2454-
gid = posix_offset + DOMAIN_GROUP_RID_USERS;
2458+
/* Default primary group. If the sid is the current user, and
2459+
we are not configured for accurate mode, fetch
2460+
the default group from the current user token, otherwise make
2461+
the educated guess that the user is in group "Domain Users"
2462+
or "None". */
2463+
if (!cygheap->pg.nss_grp_db_accurate() && sid == cygheap->user.sid ())
2464+
{
2465+
get_default_group_from_current_user_token = true;
2466+
gid = posix_offset
2467+
+ sid_sub_auth_rid (cygheap->user.groups.pgsid);
2468+
}
2469+
else
2470+
gid = posix_offset + DOMAIN_GROUP_RID_USERS;
24552471
}
24562472

24572473
if (is_domain_account)
@@ -2462,9 +2478,11 @@ pwdgrp::fetch_account_from_windows (fetch_user_arg_t &arg, cyg_ldap *pldap)
24622478
/* On AD machines, use LDAP to fetch domain account infos. */
24632479
if (cygheap->dom.primary_dns_name ())
24642480
{
2465-
/* Fetch primary group from AD and overwrite the one we
2466-
just guessed above. */
2467-
if (cldap->fetch_ad_account (sid, false, domain))
2481+
/* For the current user we got correctly cased username and
2482+
the primary group via process token. For any other user
2483+
we fetch it from AD and overwrite it. */
2484+
if (!get_default_group_from_current_user_token
2485+
&& cldap->fetch_ad_account (sid, false, domain))
24682486
{
24692487
if ((val = cldap->get_account_name ()))
24702488
wcscpy (name, val);

winsup/doc/ntsec.xml

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -930,7 +930,16 @@ The two lines starting with the keywords <literal>passwd:</literal> and
930930
information from. <literal>files</literal> means, fetch the information
931931
from the corresponding file in the /etc directory. <literal>db</literal>
932932
means, fetch the information from the Windows account databases, the SAM
933-
for local accounts, Active Directory for domain account. Examples:
933+
for local accounts, Active Directory for domain account. For the current
934+
user, the default group is obtained from the current user token to avoid
935+
additional lookups to the group database. <literal>db-accurate</literal>
936+
is only valid on <literal>group:</literal> line, and performs the same
937+
lookups as the <literal>db</literal> option, but disables using the
938+
current user token to retrieve the default group as this optimization
939+
is not accurate in all cases. For example, if you run a native process
940+
with the primary group set to the Administrators builtin group, the
941+
<literal>db</literal> option will return a non-existent group as primary
942+
group. Examples:
934943
</para>
935944

936945
<screen>
@@ -949,6 +958,15 @@ Read passwd entries only from /etc/passwd.
949958
Read group entries only from SAM/AD.
950959
</para>
951960

961+
<screen>
962+
group: db-accurate
963+
</screen>
964+
965+
<para>
966+
Read group entries only from SAM/AD. Force the use of the group database
967+
for the current user.
968+
</para>
969+
952970
<screen>
953971
group: files # db
954972
</screen>

0 commit comments

Comments
 (0)