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: |-