7

My goal is to successfully link a folder to /opt/ directory- which needs to be run with sudo.

I have tried this:

 system(sudo ln -s $$OUT_PWD/xampp /opt/lampp):message("You should manually link LAMPP")

But building from qt-creator it does not prompt for sudo password, therefore I couldn't get it to link the folder. I got the "wrong password attempts..." error in the Compiler Output. Then I tried these with build steps:

make
sudo make install

to see if it would prompt me there, but it failed on make install step with the same error, which is this in detail:

00:31:20: Starting: "/usr/bin/sudo" make install
sudo: no tty present and no askpass program specified
sudo: no tty present and no askpass program specified
Sorry, try again.
sudo: no tty present and no askpass program specified
sudo: no tty present and no askpass program specified
Sorry, try again.
sudo: no tty present and no askpass program specified
sudo: no tty present and no askpass program specified
Sorry, try again.
sudo: 3 incorrect password attempts
00:31:20: The process "/usr/bin/sudo" exited with code 1.

The system() function in the qmake script works fine when I use qmake && make && make install from terminal since it prompts me before sudo ln... command; but for fast testing purposes I feel that building with CTRL-B inside Qt Creator should also work with sudo commands...

Is there a way to be prompted from Qt Creator, or a way around this? Like storing the sudo pass within the Qt Creator (although it is risky)... Or maybe making it run build steps on a terminal where it would prompt me? Would prompt() function in qmake work with gathering sudo passwords?

Any suggestions are welcome...

Logan
  • 10,649
  • 13
  • 41
  • 54
  • 1
    Can you build from gnome-terminal? I'm not sure how you would integrate a build step in Qt-Creator that requires elevated privileges. It would probably be easier to build from the terminal anyway. You could easily make a bash script that does all the build steps including qmake, make, and make install. – Cameron Tinker May 19 '12 at 05:39
  • Yes, I can build from gnome-terminal. Thank you for the suggestion, I was thinking that a bash script would be easier too... :) – Logan May 19 '12 at 05:45

4 Answers4

3

After a lot of research:

ssh-askpass Sudo Password | sudo -S bash ./script

Worked like a charm, it asks for the password in a separate window/prompt box. I added it under custom build steps. The important thing is to let sudo know that it will expect a password input via -S option.

echo "password" | sudo -S bash ./script

Also works with bash scripts, however as you might think it is insecure storing the sudo password inside the script. It is said that read permissions should fix that issue.

Logan
  • 10,649
  • 13
  • 41
  • 54
2

@Logan You can cause sudo to use the ssh-askpass (GUI password request dialog) directly with its -A option, this can use the SUDO_ASKPASS environmental variable to override a /usr/bin/ssh-askpass value that may be configured in /etc/sudo.conf[1] (like the suggestions of gksudo or kdesudo from @herophuong ). This gets around the security risk of the second solution in your answer that leaves the "password" visible to anything that can observe the commandline arguments. The equivalent to your first solution becomes:

sudo -A -s ./script

Don't forget that sudo may provide credential caching so that, if it has been used very recently (default 5 minutes?), a repeated invocation uses that cached password value instead of making another password request. If, for sanity/security reasons, this is not desired for a script (or elsewhere) use sudo -K with no other arguments to immediately clear any cached credentials (no password is needed for that usage) so that any subsequent sudo usage will require authentication.

1 - Remember to use visudo as root if you wish to edit /etc/sudo.conf to add:

# Path to askpass helper program
Path askpass /usr/bin/ssh-askpass

SlySven
  • 326
  • 3
  • 15
2

As stated in a comment by Cameron Tinker you can set up Qt Creator to have a deploy step with the command gnome-terminal and arguments -e "sudo make install".

Effectively the same as running gnome-terminal -e "sudo make install"

Image is a screenshot of what this config looks like in Qt Creator :)

Qt Creator with sudo make install step with gnome terminal project settings

gksudo is no longer recommended or installed by default on Ubuntu

Steven
  • 1,996
  • 3
  • 22
  • 33
0

I think you should use gksudo (in GNOME) or kdesudo (in KDE). The commands will then use a window to prompt for your password.

herophuong
  • 354
  • 6
  • 17