Applescript Check Whether Password is Correct - authentication

I am making an app that requires root privileges. I am new to applscript and apologize if this is a dumb question but what I am trying to do is check whether a password is correct. I get the password string from a prompt like this:
set thePass to the text returned of (display dialog "I need you to enter your password" default answer "" buttons {"Okay"} default button 1 with icon file ((path to resource "Icon.icns") as text) with hidden answer)
I need to check whether thePass is actually their password before proceeding. I do not want to just use the default password prompt. Is there a way to do this?
I already tried a try, on error statement to test the password, it just prompts for the password with the default prompt if the one entered is incorrect.

Okay here is how I just solved it:
1) Get the password in a variable
2) Try to use sudo to run a command (using stdin as input), this will fail if the password is incorrect
try
do shell script "echo '" & thePass & "' | sudo -S :"
onerror
display alert "Incorrect Password!"
end try

No idea what you're trying to do, but if you want to execute a shell script using sudo you should do
do shell script shellScriptVariable with administrator privileges
without the "sudo" in the shell script. You'll automatically get an error dialog if the password is wrong.

Related

How to make alias with administrator privileges applescript

My script is working as intended and is creating my alias folder perfectly except everytime it prompts me to type in my computers password because it needs administrator privileges. Applescript wont allow me to add the code to give administrator privileges so I am stumped as to what to do. The code below works but requires my password to complete
make new alias at myLocation to mySource
What I would like to do is this:
make new alias at myLocation to mySource password "myPassword" with administrator privileges
I have used code similar to this several times but for some reason it wont allow me to do so with creating an alias. Anyone know what I need to do to automate this process instead of having to type in my password every single time?
Well I figured out that I could do this instead:
do shell script ("sudo ln -s " & quoted form of POSIX path of fullPathTitlesMedia & space & quoted form of POSIX path of parentMediaPath & "") password "password" with administrator privileges

Password is not read while executing password change command using writeline in shell

I have a code by which I open plink for SSH connection using shell command where I can run any command by StdIn.Writeline method except password change command. If I run a change password command I am unable to input the password by StdIn.Writeline method. The command is like set usr pwd. This command gets executed perfectly and after it executes it asks for "Enter Current Password". Here the WriteLine method fails to input the password.
Below is the example of the code and ouput:
Run = filename & " " & strCompAddress & " -P " & strServerPOrt & " -l " & strServerUser & " -pw " & strServerPassword
Set osh = CreateObject("Wscript.Shell")
Set oEx = osh.exec(Run)
oEx.StdIn.WriteLine "command 1" 'This command runs fine'
oEx.StdIn.WriteLine "command 2" 'This command runs fine'
oEx.StdIn.WriteLine "command 3" 'This command runs fine'
oEx.StdIn.WriteLine "set usr wd" 'This command runs fine'
oEx.StdIn.WriteLine "current_pwd" 'This doesn't input the password and it stucks here
oEx.StdIn.WriteLine "new_pwd"
oEx.StdIn.WriteLine "new_pwd"
oEx.StdIn.WriteLine "exit"
Output is like with lots of escape sequence commands.
command 1
command 2
command 3
set usr pwd
current_pwd
new_pwd
new_pwd
exit
Command 1
result
ok
command 2
result
ok
command 3
result
ok
set usr pwd
Enter Current Password:
The command set user password (or whatever it is) is possibly built to read the password from terminal only, not from a standard input. This is a common security feature to avoid exactly this kind of automatic password change.
Try forcing an interactive/terminal mode using the Plink command-line switch -t.
This will make the Plink input be redirected to a virtual terminal and can make your command happy.
Make sure the server admin is happy with, what you are trying to do.

Set clipboard to text returned AppleScript

I have recently been working on a project that requires the user's password to be copied to the clipboard. I have this much:
set usr to short user name of (system info)
repeat
display dialog "Please enter login password to continue:" default answer "" buttons {"Submit"} with title "Enter password" with icon stop with hidden answer
set pswd to text returned of the result
try
set a to do shell script "echo test" user name usr password pswd with administrator privileges
exit repeat
end try
end repeat
set the clipboard to a
However, the clipboard simply sets to "test," presumably from where it says "echo test"
How do I fix this problem to set the password entered to the clipboard?
Try:
set the clipboard to pswd

How to set the clipboard to the returned text on AppleScript

I was recently working on an AppleScript project that would require the user's password to be copied to the clipboard for later use. I already have the part where the user is prompted for their password but how do I set the text returned (their password) to the clipboard. My current code gives me the error: Can’t make text returned of «script» into type text. This is what I have so far:
set usr to short user name of (system info)
repeat
display dialog "Please enter login password to continue:" default answer "" buttons {"Submit"} with title "Enter password" with icon stop with hidden answer
set pswd to text returned of the result
try
do shell script "echo test" user name usr password pswd with administrator privileges
exit repeat
end try
end repeat
set a to (text returned) as list
set AppleScript's text item delimiters to linefeed
set a to a as text
set the clipboard to a
When running shell scripts via do shell script I typically use a variable within the applescript to capture the result. I'm not sure if there is a "right" way to do this, but with your script I removed
set a to (text returned) as list
set AppleScript's text item delimiters to linefeed
set a to a as text
and captured the result of the do shell script command as the variable a, which I then put on the clipboard. Here is the modified script:
set usr to short user name of (system info)
repeat
display dialog "Please enter login password to continue:" default answer "" buttons {"Submit"} with title "Enter password" with icon stop with hidden answer
set pswd to text returned of the result
try
set a to do shell script "echo test" user name usr password pswd with administrator privileges
exit repeat
end try
end repeat
set the clipboard to a
If you wanted to slim it down even more, you could eliminate the variable a altogether and capture the result of the do shell script command directly to the clipboard:
set usr to short user name of (system info)
repeat
display dialog "Please enter login password to continue:" default answer "" buttons {"Submit"} with title "Enter password" with icon stop with hidden answer
set pswd to text returned of the result
try
set the clipboard to (do shell script "echo test" user name usr password pswd with administrator privileges)
exit repeat
end try
end repeat

Get Currently Logged in username

I am trying to get the logged in username of the user by using VBS.
I tried some codes which works when I run the script directly (Double Clicking it)
Set wshNetwork = CreateObject("WScript.Network")
strUser = wshNetwork.Username
WScript.Echo "Current User: " & strUser
However, what I need to do is to use CMD to run a scheduled task using the AT command.
When it ran, the username would be the computer's name instead of the logged in user.
This problem also occurs when I run CMD as administrator and use wscript to run the script.
Is there any way to bypass this context and get the logged in user instead of the one that runs the script?
The command
query session console
should provide what you need
For an easier to parse
quser console
EDITED - Included sample vbs code
Dim strCmd
strCmd = "cmd /q /c ""quser console | find /i ""console"" "" "
Dim buffer
buffer = WScript.CreateObject("WScript.Shell").Exec(strCmd).StdOut.ReadAll()
Dim c, consoleUserName
c = InStr(buffer,"console")
If c > 2 Then
consoleUserName = Trim(Mid(buffer,2,c-2))
Else
consoleUserName = ""
End If
WScript.Echo consoleUserName
I suggest you execute the command
set
from the prompt. It may reveal a few items that are set in the environment that may be of interest.
set user
will censor the list so that only variables that start user will be displayed.