From 01bc2627be6344f5bc2dbdbf1bac50fff42a06c2 Mon Sep 17 00:00:00 2001 From: Lachlan Arthur Date: Fri, 8 Dec 2017 15:24:22 +1000 Subject: [PATCH] Check SMB credentials before using them --- .../windows/scripts/check_credentials.ps1 | 19 ++++++++++ plugins/synced_folders/smb/synced_folder.rb | 37 +++++++++++++++---- templates/locales/synced_folder_smb.yml | 2 + 3 files changed, 50 insertions(+), 8 deletions(-) create mode 100644 plugins/hosts/windows/scripts/check_credentials.ps1 diff --git a/plugins/hosts/windows/scripts/check_credentials.ps1 b/plugins/hosts/windows/scripts/check_credentials.ps1 new file mode 100644 index 000000000..28ce28a90 --- /dev/null +++ b/plugins/hosts/windows/scripts/check_credentials.ps1 @@ -0,0 +1,19 @@ +Param( + [Parameter(Mandatory=$true)] + [string]$username, + [Parameter(Mandatory=$true)] + [string]$password +) + +Add-Type -AssemblyName System.DirectoryServices.AccountManagement + +$DSContext = New-Object System.DirectoryServices.AccountManagement.PrincipalContext( + [System.DirectoryServices.AccountManagement.ContextType]::Machine, + $env:COMPUTERNAME +) + +if ( $DSContext.ValidateCredentials( $username, $password ) ) { + exit 0 +} else { + exit 1 +} \ No newline at end of file diff --git a/plugins/synced_folders/smb/synced_folder.rb b/plugins/synced_folders/smb/synced_folder.rb index 426def425..e834367df 100644 --- a/plugins/synced_folders/smb/synced_folder.rb +++ b/plugins/synced_folders/smb/synced_folder.rb @@ -30,11 +30,6 @@ module VagrantPlugins def prepare(machine, folders, opts) machine.ui.output(I18n.t("vagrant_sf_smb.preparing")) - # Check if this host can start and SMB service - if machine.env.host.capability?(:smb_start) - machine.env.host.capability(:smb_start) - end - smb_username = smb_password = nil # If we need auth information, then ask the user. @@ -48,12 +43,38 @@ module VagrantPlugins end end + script_path = File.expand_path("../scripts/check_credentials.ps1", __FILE__) + if !have_auth - machine.env.ui.detail(I18n.t("vagrant_sf_smb.warning_password") + "\n ") - smb_username = machine.env.ui.ask("Username: ") - smb_password = machine.env.ui.ask("Password (will be hidden): ", echo: false) + machine.ui.detail(I18n.t("vagrant_sf_smb.warning_password") + "\n ") + auth_success = false + while !auth_success do + @creds[:username] = machine.ui.ask("Username: ") + @creds[:password] = machine.ui.ask("Password (will be hidden): ", echo: false) + + args = [] + args << "-username" << "'#{@creds[:username].gsub("'", "''")}'" + args << "-password" << "'#{@creds[:password].gsub("'", "''")}'" + + r = Vagrant::Util::PowerShell.execute(script_path, *args) + + if r.exit_code == 0 + auth_success = true + end + + if !auth_success + machine.ui.output(I18n.t("vagrant_sf_smb.incorrect_credentials") + "\n ") + end + end end + # Check if this host can start and SMB service + if machine.env.host.capability?(:smb_start) + machine.env.host.capability(:smb_start) + end + + script_path = File.expand_path("../scripts/set_share.ps1", __FILE__) + folders.each do |id, data| data[:smb_username] ||= smb_username data[:smb_password] ||= smb_password diff --git a/templates/locales/synced_folder_smb.yml b/templates/locales/synced_folder_smb.yml index 3521ff4ff..5a7dd57b4 100644 --- a/templates/locales/synced_folder_smb.yml +++ b/templates/locales/synced_folder_smb.yml @@ -15,6 +15,8 @@ en: You will be asked for the username and password to use for the SMB folders shortly. Please use the proper username/password of your account. + incorrect_credentials: |- + Credentials incorrect. Please try again. uac: prune_warning: |-