--- syscalls.cc.org Tue Feb 19 20:36:44 2002 +++ syscalls.cc Fri Mar 8 20:22:18 2002 @@ -1929,7 +1929,7 @@ } else { - cygsid usersid, pgrpsid, tok_pgrpsid; + cygsid usersid, pgrpsid, processsid; HANDLE sav_token = INVALID_HANDLE_VALUE; BOOL sav_impersonation; BOOL current_token_is_internal_token = FALSE; @@ -1946,31 +1946,8 @@ - if reasonable - new pgrp == pgrp of impersonation token. */ if (allow_ntsec && cygheap->user.token != INVALID_HANDLE_VALUE) { - if (!GetTokenInformation (cygheap->user.token, TokenUser, - &tok_usersid, sizeof tok_usersid, &siz)) - { - debug_printf ("GetTokenInformation(): %E"); - tok_usersid = NO_SID; - } - if (!GetTokenInformation (cygheap->user.token, TokenPrimaryGroup, - &tok_pgrpsid, sizeof tok_pgrpsid, &siz)) - { - debug_printf ("GetTokenInformation(): %E"); - tok_pgrpsid = NO_SID; - } - /* Check if the current user token was internally created. */ - TOKEN_SOURCE ts; - if (!GetTokenInformation (cygheap->user.token, TokenSource, - &ts, sizeof ts, &siz)) - debug_printf ("GetTokenInformation(): %E"); - else if (!memcmp (ts.SourceName, "Cygwin.1", 8)) - current_token_is_internal_token = TRUE; - if ((usersid && tok_usersid && usersid != tok_usersid) || - /* Check for pgrp only if current token is an internal - token. Otherwise the external provided token is - very likely overwritten here. */ - (current_token_is_internal_token && - pgrpsid && tok_pgrpsid && pgrpsid != tok_pgrpsid)) + if (!verify_token(cygheap->user.token, usersid, pgrpsid, + & current_token_is_internal_token)) { /* If not, RevertToSelf and close old token. */ debug_printf ("tsid != usersid"); @@ -2035,9 +2012,28 @@ &pgrpsid, sizeof pgrpsid)) debug_printf ("SetTokenInformation(user.token, " "TokenPrimaryGroup): %E"); - } - + /* Set process def dacl to allow access to impersonated token */ + char dacl_buf[MAX_DACL_LEN(5)]; + sec_process_sid(processsid); + if (usersid && + sec_acl((PACL) dacl_buf, FALSE, processsid, usersid)) + { + HANDLE ptok; + TOKEN_DEFAULT_DACL tdacl; + tdacl.DefaultDacl = (PACL) dacl_buf; + if (!OpenProcessToken (GetCurrentProcess (), TOKEN_ADJUST_DEFAULT, + &ptok)) + debug_printf ("OpenProcessToken(): %E\n"); + else + { + if (!SetTokenInformation (ptok, TokenDefaultDacl, + &tdacl, sizeof dacl_buf)) + debug_printf ("SetTokenInformation" + "(TokenDefaultDacl): %E"); + CloseHandle (ptok); + } + } /* Now try to impersonate. */ if (!LookupAccountSid (NULL, usersid, username, &ulen, domain, &dlen, &use)) @@ -2102,7 +2098,6 @@ return -1; } myself->gid = gid; -#if 0 // Setting the primary group in token here isn't foolproof enough. if (allow_ntsec) { cygsid gsid; @@ -2110,6 +2105,17 @@ if (gsid.getfromgr (gr)) { + /* Remove impersonation */ + if (cygheap->user.token != INVALID_HANDLE_VALUE + && cygheap->user.impersonated) + { + if (!SetTokenInformation (cygheap->user.token, + TokenPrimaryGroup, + &gsid, sizeof gsid)) + debug_printf ("SetTokenInformation(primary, " + "TokenPrimaryGroup): %E"); + RevertToSelf (); + } if (!OpenProcessToken (GetCurrentProcess (), TOKEN_ADJUST_DEFAULT, &ptok)) @@ -2118,13 +2124,15 @@ { if (!SetTokenInformation (ptok, TokenPrimaryGroup, &gsid, sizeof gsid)) - debug_printf ("SetTokenInformation(myself, " + debug_printf ("SetTokenInformation(process, " "TokenPrimaryGroup): %E"); CloseHandle (ptok); } + if (cygheap->user.token != INVALID_HANDLE_VALUE + && cygheap->user.impersonated) + ImpersonateLoggedOnUser (cygheap->user.token); } } -#endif } } else