How to launch a QProcess with root rights?
Asked Answered
P

4

4

I need to launch gphoto2 from a Qt program. I do this:

QString gphotoProgram = "/usr/bin/gphoto2";
QStringList gphotoArguments;
gphotoArguments << "--capture-image";
QProcess *gphotoProcess = new QProcess(this);
gphotoProcess->start(gphotoProgram, gphotoArguments);

but it never enters the Running state this way, as gphoto2 usually needs admin rights to be launched on command line.

How can I start this QProcess with proper rights to make gphoto2 working?

Edit: I precise that I would prefer the user to not have to enter a password, which means gksudo, kdesudo or any other graphical solution is not a valid option for me.

Piperpiperaceous answered 11/4, 2011 at 18:39 Comment(0)
M
2

I would strongly recommend finding a way to allow gphoto2 to be run with the logged in user's permissions. Perhaps this article has some helpful info.

Mcburney answered 11/4, 2011 at 23:6 Comment(1)
This page was helpful indeed, thanks a lot! For the posterity, here is the needed command line to do to enable any user of the group plugdev: /usr/lib/libgphoto2/print-camera-list udev-rules version 0.98 group plugdev mode 0660 > /etc/udev/rules.d/90-libgphoto2.rulesNovelize
L
1

If you have a distribution with sudo enabled, try to add "gksudo" to the command line of your process:

QString gphotoProgram = "gksudo /usr/bin/gphoto2"

If the user account is authorized as a sudo-er, it will ask the user password so that the program can run with root rights.

Lepido answered 11/4, 2011 at 18:44 Comment(4)
I forgot to precise that I would prefer not to use a graphical interface to enter a password, as the user may not know it. I edited the question accordingly.Novelize
If the security mechanism demands a password (and most often it does), you won't be able to bypass that.Pifer
If you want to bypass the password authentication, the only way to do it is to set up a background process with root privileges (daemon, server, etc.) wrapping your gphoto2 needs. Your application just have to communicate with the server with a socket. But I think it is a bad idea, as it is an open gate to security problems... Maybe you should try to understand why admin rigths are needed, and if there is some alternatives to that.Lepido
... as a side note, I think that if gphoto needs admin rights, it is because it is using USB devices directly, and access rights must be set correctly for them. Have a look at the following page: gphoto.org/doc/manual/permissions-usb.html , and see if you can configure things so that no root privilege is needed for USB camerasLepido
K
1

You can also use PolicyKit to start QProcess with sudo rights.

pkexec command

QString gphotoProgram = "pkexec /usr/bin/gphoto2";

Known answered 14/5, 2011 at 12:11 Comment(1)
@ChrisV I want to implement this in windows. Help me out.Known
S
0

Don't GNOME and KDE still have their own graphical sudo wrappers? (I'm a Windows guy myself.) You could use QProcess to launch "sudo" and let it take care of the elevation and subsequent gphoto launch.

Sanmiguel answered 11/4, 2011 at 18:43 Comment(1)
I forgot to precise that I would prefer not to use a graphical interface to enter a password, as the user may not know it. I edited the question accordingly.Novelize

© 2022 - 2024 — McMap. All rights reserved.