-
Notifications
You must be signed in to change notification settings - Fork 5.6k
fix-windows-runas #68708
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
harshang03
wants to merge
1
commit into
saltstack:master
Choose a base branch
from
harshang03:fix-68612-windows-runas
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
fix-windows-runas #68708
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -81,6 +81,52 @@ def split_username(username): | |
| return str(user_name), str(domain) | ||
|
|
||
|
|
||
| def _is_upn(username): | ||
| """ | ||
| Return True when the username is in UPN format (user@domain). | ||
| """ | ||
| return "@" in username and "\\" not in username | ||
|
|
||
|
|
||
| def resolve_logon_credentials(username): | ||
| """ | ||
| Resolve logon credentials for Windows logon APIs. | ||
| """ | ||
| if not isinstance(username, str): | ||
| username = str(username) | ||
|
Comment on lines
+95
to
+96
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will convert |
||
| is_upn = _is_upn(username) | ||
| try: | ||
| _, lookup_domain, _ = win32security.LookupAccountName(None, username) | ||
| except pywintypes.error as exc: | ||
| message = win32api.FormatMessage(exc.winerror).rstrip("\n") | ||
| raise CommandExecutionError(message) | ||
| user_name, domain_name = split_username(username) | ||
| logon_name = username if is_upn else user_name | ||
| logon_domain = "" if is_upn else lookup_domain | ||
| return { | ||
| "input": username, | ||
| "user_name": user_name, | ||
| "domain_name": domain_name, | ||
| "logon_name": logon_name, | ||
| "logon_domain": logon_domain, | ||
| "lookup_domain": lookup_domain, | ||
| "is_upn": is_upn, | ||
| } | ||
|
|
||
|
|
||
| def validate_username(username, raise_on_error=False): | ||
| """ | ||
| Validate that a username can be resolved on this host. | ||
| """ | ||
| try: | ||
| resolve_logon_credentials(username) | ||
| except CommandExecutionError: | ||
| if raise_on_error: | ||
| raise | ||
| return False | ||
| return True | ||
|
|
||
|
|
||
| def create_env(user_token, inherit, timeout=1): | ||
| """ | ||
| CreateEnvironmentBlock might fail when we close a login session and then | ||
|
|
@@ -173,13 +219,10 @@ def runas(cmd, username, password=None, **kwargs): | |
| # Let's make it a string if it's anything other than a string | ||
| if not isinstance(username, str): | ||
| username = str(username) | ||
| # Validate the domain and sid exist for the username | ||
| try: | ||
| _, domain, _ = win32security.LookupAccountName(None, username) | ||
| username, _ = split_username(username) | ||
| except pywintypes.error as exc: | ||
| message = win32api.FormatMessage(exc.winerror).rstrip("\n") | ||
| raise CommandExecutionError(message) | ||
| resolved = resolve_logon_credentials(username) | ||
| domain = resolved["lookup_domain"] | ||
| logon_name = resolved["logon_name"] | ||
| logon_domain = resolved["logon_domain"] | ||
|
|
||
| # Elevate the token from the current process | ||
| access = win32security.TOKEN_QUERY | win32security.TOKEN_ADJUST_PRIVILEGES | ||
|
|
@@ -216,24 +259,24 @@ def runas(cmd, username, password=None, **kwargs): | |
| # Logon as a system level account, SYSTEM, LOCAL SERVICE, or NETWORK | ||
| # SERVICE. | ||
| user_token = win32security.LogonUser( | ||
| username, | ||
| domain, | ||
| logon_name, | ||
| logon_domain, | ||
| "", | ||
| win32con.LOGON32_LOGON_SERVICE, | ||
| win32con.LOGON32_PROVIDER_DEFAULT, | ||
| ) | ||
| elif password: | ||
| # Login with a password. | ||
| user_token = win32security.LogonUser( | ||
| username, | ||
| domain, | ||
| logon_name, | ||
| logon_domain, | ||
| password, | ||
| win32con.LOGON32_LOGON_INTERACTIVE, | ||
| win32con.LOGON32_PROVIDER_DEFAULT, | ||
| ) | ||
| else: | ||
| # Login without a password. This always returns an elevated token. | ||
| user_token = salt.platform.win.logon_msv1_s4u(username).Token | ||
| user_token = salt.platform.win.logon_msv1_s4u(logon_name).Token | ||
|
|
||
| # Get a linked user token to elevate if needed | ||
| elevation_type = win32security.GetTokenInformation( | ||
|
|
@@ -374,13 +417,10 @@ def runas_unpriv(cmd, username, password, **kwargs): | |
| # Let's make it a string if it's anything other than a string | ||
| if not isinstance(username, str): | ||
| username = str(username) | ||
| # Validate the domain and sid exist for the username | ||
| try: | ||
| _, domain, _ = win32security.LookupAccountName(None, username) | ||
| username, _ = split_username(username) | ||
| except pywintypes.error as exc: | ||
| message = win32api.FormatMessage(exc.winerror).rstrip("\n") | ||
| raise CommandExecutionError(message) | ||
| resolved = resolve_logon_credentials(username) | ||
| username = resolved["user_name"] | ||
| logon_name = resolved["logon_name"] | ||
| logon_domain = resolved["logon_domain"] | ||
|
|
||
| # Create inheritable copy of the stdin | ||
| stdin = salt.platform.win.kernel32.GetStdHandle( | ||
|
|
@@ -445,8 +485,8 @@ def runas_unpriv(cmd, username, password, **kwargs): | |
| try: | ||
| # Run command and return process info structure | ||
| process_info = salt.platform.win.CreateProcessWithLogonW( | ||
| username=username, | ||
| domain=domain, | ||
| username=logon_name, | ||
| domain=logon_domain, | ||
| password=password, | ||
| logonflags=salt.platform.win.LOGON_WITH_PROFILE, | ||
| applicationname=None, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is already checked at line 3043
if salt.utils.platform.is_windows() and runas:https://github.com/saltstack/salt/pull/68708/changes#diff-700b7d75700c9eafa3ee48d01236e59f37b2ed81d998267136fbcc0224766497L3025 .