14

I made a .desktop file for Androxyde's Flashtool (utility for Sony Xperia devices that I have to open with an executable file in its folder) that requires root privileges to use fastboot utilities. I used to make it work with gksu, but I'm on Ubuntu 15.04 and gksu is now old.

I tried to modify the exec line from

Exec=gksu /home/natasha/FlashTool/FlashTool
to
Exec=pkexec /home/natasha/FlashTool/FlashTool
Then, I read about pkexec doesn't allow to run X11 applications and so I override in this way:

enter image description here

Link to full image on Imgur.com

The problem now is: It asks me the password but the Flashtool's GUI doesn't start. BUT if I execute that command in terminal, the program starts without problems. What can I do?

enter image description here

Link to full image on Imgur.com

kos
  • 41,268
np_3ka
  • 544
  • 3
  • 6
  • 22

4 Answers4

11

Create a new file in /usr/share/polkit-1/actions/

sudo nano /usr/share/polkit-1/actions/FlashTool.policy

and add the lines below:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE policyconfig PUBLIC
 "-//freedesktop//DTD PolicyKit Policy Configuration 1.0//EN"
 "http://www.freedesktop.org/standards/PolicyKit/1/policyconfig.dtd">

<policyconfig>

  <action id="org.freedesktop.policykit.pkexec.run-FlashTool">
    <description>Run FlashTool</description>
    <message>Authentication is required to run FlashTool</message>
    <defaults>
      <allow_any>no</allow_any>
      <allow_inactive>no</allow_inactive>
      <allow_active>auth_admin_keep</allow_active>
    </defaults>
    <annotate key="org.freedesktop.policykit.exec.path">/home/natasha/FlashTool/FlashTool</annotate>
    <annotate key="org.freedesktop.policykit.exec.allow_gui">TRUE</annotate>
  </action>

</policyconfig>

Then create a new file /home/natasha/FlashTool/

nano /home/natasha/FlashTool/flashtool-pkexec

and add the lines below:

#!/bin/sh
pkexec "/home/natasha/FlashTool/FlashTool" "$@"

Use the line below for Exec in your desktop file:

Exec=/home/natasha/FlashTool/flashtool-pkexec

Tested on my system Ubuntu 15.04 GNOME with the following files:


$ cat /usr/share/applications/gedit.root.desktop 
[Desktop Entry]
Name=Gedit as root
GenericName=Text Editor
X-GNOME-FullName=
Comment=
Exec=gedit-pkexec
Icon=gedit
Terminal=false
Type=Application
Categories=GNOME;System;Filesystem;Settings;
StartupNotify=true
X-Ubuntu-Gettext-Domain=gedit

$ cat /usr/share/polkit-1/actions/gedit.policy 
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE policyconfig PUBLIC
 "-//freedesktop//DTD PolicyKit Policy Configuration 1.0//EN"
 "http://www.freedesktop.org/standards/PolicyKit/1/policyconfig.dtd">

<policyconfig>

  <action id="org.freedesktop.policykit.pkexec.run-FlashTool">
    <description>Run FlashTool</description>
    <message>Authentication is required to run FlashTool</message>
    <defaults>
      <allow_any>no</allow_any>
      <allow_inactive>no</allow_inactive>
      <allow_active>auth_admin_keep</allow_active>
    </defaults>
    <annotate key="org.freedesktop.policykit.exec.path">/usr/bin/gedit</annotate>
    <annotate key="org.freedesktop.policykit.exec.allow_gui">TRUE</annotate>
  </action>

</policyconfig>

$ cat /usr/bin/gedit-pkexec 
#!/bin/sh
pkexec "gedit" "$@"
Fabby
  • 35,017
A.B.
  • 92,125
3

You may also change

Exec=APP_COMMAND

to

Exec=sh -c "pkexec env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY APP_COMMAND"

That way you will get asked about the password and the program starts as usual.

(See How to launch application as root from Unity Launcher? for more information and reasoning. TL;DR: sh -c starts it in a shell, where the follow-up code worked, as the op confirmed. pkexec then starts the "APP_COMMAND" as root but needs the environment variables to actually be able to start a GUI application.)

0

The content of the desktop file (/usr/share/applications/Flashtool.desktop or ~/.local/share/applications/Flashtool.desktop).

[Desktop Entry]
Name=Flashtool
GenericName=Flashtool
Comment=Tool for Xperia Devices
Type=Application
Categories=Qt;FileTransfer;Chat
Terminal=false
Icon=/usr/share/icons/Numix-Circle-Light/48x48/apps/nitrotasks.svg
Exec=/home/natasha/FlashTool/FlashTool
Actions=new-root-window

[Desktop Action new-root-window] Name=New Root Window Exec=sh -c "pkexec --disable-internal-agent env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY /home/natasha/FlashTool/FlashTool" Terminal=true

Right-click on the Desktop icon and select New Root Window.

0

sudo -H is enough to start a graphical application preventing changes to the user's configuration files in ~/, since it sets the running environment's home directory to root's home directory:

Exec=sudo -H /home/natasha/FlashTool/FlashTool
kos
  • 41,268