Skip to content

Commit

Permalink
Merge pull request #23 from aditya7iyengar/feature/askpass-support
Browse files Browse the repository at this point in the history
Add support for askpass
  • Loading branch information
thebugcatcher authored Oct 1, 2019
2 parents dd0d9c0 + 1933eeb commit 946d51b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ erl_crash.dump
# Ignore package tarball (built via "mix hex.build").
adify-*.tar

.askpass.sh
40 changes: 39 additions & 1 deletion lib/adify/system_info.ex
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,51 @@ defmodule Adify.SystemInfo do
iex> {:error, output} = Adify.SystemInfo.cmd(cmd, [], ".")
iex> output =~ "bad_command"
true
# When the command has sudo
iex> cmd = "sudo ls"
iex> {:ok, output} = Adify.SystemInfo.cmd(cmd, [], ".", true)
iex> output =~ "password"
true
"""
@spec cmd(String.t(), [{String.t(), String.t()}], Path.t()) ::
{:ok, String.t()} | {:error, String.t()}
def cmd(cmd, env \\ [], cd \\ ".") do
def cmd(cmd, env \\ [], cd \\ ".", mock_sudo \\ false) do
case String.contains?(cmd, "sudo") do
true -> cmd_sudo(cmd, env, cd, mock_sudo)
false -> cmd_udo(cmd, env, cd)
end
end

defp cmd_sudo(cmd, env, cd, mock_sudo) do
create_or_update_askpass()

cmd = String.replace(cmd, "sudo", "")

env = env ++ [{"SUDO_ASKPASS", ".askpass.sh"}]

case mock_sudo do
true -> {:ok, "password mocked"}
_ ->
case System.cmd("sudo", ["-A", "sh", "-c", cmd], env: env, cd: cd, stderr_to_stdout: true) do
{output, 0} -> {:ok, output}
{output, _} -> {:error, output}
end
end
end

defp cmd_udo(cmd, env, cd) do
case System.cmd("sh", ["-c", cmd], env: env, cd: cd, stderr_to_stdout: true) do
{output, 0} -> {:ok, output}
{output, _} -> {:error, output}
end
end

defp create_or_update_askpass do
askpass_path = Path.join(:code.priv_dir(:adify), "askpass.sh")
content = File.read!(askpass_path)
cmd("touch .askpass.sh")
cmd("echo '#{content}' > .askpass.sh")
cmd("chmod 777 .askpass.sh")
end
end
2 changes: 2 additions & 0 deletions priv/askpass.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
zenity --password --title=Authentication

0 comments on commit 946d51b

Please sign in to comment.