Skip to content
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

Add support for askpass #23

Merged
merged 2 commits into from
Oct 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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