LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices

Reply
 
LinkBack Search this Thread
Old 01-14-2012, 11:31 PM   #1
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Lightbulb sudo prompt when redirecting stdout/stderr, called from KDE application shortcut


(I actually solved this while I was writing it, but I'm posting it anyway in case someone else needs the advice.)

I have a script that's called from a KDE application shortcut. This script requires sudo for a command, the output of which is redirected (both standard output and standard error.) This redirection causes sudo to prompt for a password every time, vs. only the first time when I don't redirect both. For example:
Code:
#!/bin/sh
#THIS SHOULD BE SAVED AS $HOME/gui-password.sh

kdialog --password "enter password"
Code:
#!/bin/bash
#THIS SHOULD BE SAVED AS $HOME/show-info.sh


silent()
{
  local command="$1"
  local title="$2"
  shift 2

  eval $command
}


terminal()
{
  local command="$1"
  local title="$2"
  shift 2

  { eval $command; } 2>&1 | xterm -title "$title" -aw -fg white -bg black +sf -mesg -e "cat /dev/fd/3" "$@" 3<&0
}


info()
{
  #THIS IS A CONTRIVED COMMAND, FOR SIMPLICITY

  echo "this is a message for the terminal" 1>&2
  SUDO_ASKPASS="$HOME/gui-password.sh" sudo -A ls /root | kwrite --stdin --line 1
}


#silent info "Show Info" #USING THIS LINE INSTEAD WILL ONLY CAUSE A PROMPT THE FIRST TIME
terminal info "Show Info"
Code:
#THIS SHOULD BE SAVED AS $HOME/show-info.desktop

[Desktop Entry]
Exec=$HOME/show-info.sh
Name[en_US]=Show Info
Name=Show Info
StartupNotify=true
Terminal=false
Type=Application
X-KDE-SubstituteUID=false
The objective is to show standard error in an xterm and open standard output in an editor.

SOLUTION: I made the assumption that redirecting both outputs causes the process to no longer have a controlling terminal, since sudo prompts every time. This lead me to believe that stdin isn't a tty when executed from an application shortcut; therefore, I added exec < /dev/fd/2 just above the call to terminal, and that fixed it! This must be done before the other redirection, while standard error is still a tty. This should cause the sudo process to be associated with the same tty every time it's called from a desktop shortcut, in case multiple shortcuts call sudo, thereby preventing a prompt every time a shortcut is used.

Kevin Barry

PS I didn't really feel like installing one of the graphical sudo programs. I try to get things done with what's on my system already.
 
Old 01-15-2012, 01:03 AM   #2
EricTRA
Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 18 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290
Hello,

Thank you for sharing a very good description of a problem and how you've fixed it. I'm sure it'll help someone.

Kind regards,

Eric
 
Old 01-15-2012, 11:46 AM   #3
kasl33
Member
 
Registered: Oct 2004
Location: Bremerton, WA
Distribution: Arch, Debian, Ubuntu, Ubuntu-Server, CentOS, OSX Lion
Posts: 350

Rep: Reputation: 47
You could always disable the need or a sudo password completely. In /etc/sudoers, you see this line:

Code:
%admin ALL=(ALL) ALL
You could add NOPASSWD: so it is like the following:

Code:
%admin ALL=(ALL) NOPASSWD: ALL
Of course this presents a security risk for your computer - but I just lock my computer when I leave my desk and it works just fine. I don't believe that it's any more insecure than adding init=/bin/bash to the boot line in GRUB.
 
Old 01-15-2012, 12:42 PM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by kasl33 View Post
You could always disable the need or a sudo password completely. In /etc/sudoers, you see this line:
Thanks for the suggestion! I do that for some things, but these particular shortcuts call scripts that manage an encrypted backup system, and they can potentially retrieve files that only root has access to.
Kevin Barry
 
  


Reply

Tags
bash, kde, password, redirection, sudo


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
redirecting stdin, stdout and stderr, and finding files name and other stats wroom Programming 4 08-14-2010 06:48 AM
[SOLVED] Weird thing in redirecting stdout and stderr brubizu Linux - Software 2 07-01-2010 11:09 AM
Redirecting stdout, stderr to pty0? Rostfrei Linux - General 4 03-20-2007 03:15 AM
redirecting stdout to /dev/null and stderr to stdout? Thinking Programming 1 05-18-2006 02:36 AM
redirecting stdout and stderr to a file Avatar33 Programming 4 03-12-2005 07:55 AM


All times are GMT -5. The time now is 01:30 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration