LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Security
User Name
Password
Linux - Security This forum is for all security related questions.
Questions, tips, system compromises, firewalls, etc. are all included here.

Notices


Reply
  Search this Thread
Old 07-18-2010, 04:47 PM   #1
x24804
LQ Newbie
 
Registered: Jun 2007
Posts: 12

Rep: Reputation: 0
Bash: Can't su Within while loop


I have a while loop in a file that looks like:

Code:
while IFS=":" read name script
do
        su      
        exit 
        ...
        <snip>
done < /path/to/file
Where I redirect the file into the loop, for some reason, I can't do an su when I redirect a file like that. I get the error, "su: must be run from a terminal." Why is this? How can I fix it? Thanks.

Last edited by x24804; 07-18-2010 at 04:50 PM. Reason: Add some text
 
Old 07-18-2010, 05:15 PM   #2
x24804
LQ Newbie
 
Registered: Jun 2007
Posts: 12

Original Poster
Rep: Reputation: 0
Hey, for anyone else with this problem, I think this happens because su thinks that it's going to get it's input non-interactively from the file. su doesn't allow that. Anyone have any solutions to this? Thanks.
 
Old 07-18-2010, 05:15 PM   #3
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
If su allowed the password to be read from a re-directed [b]stdin[/] a simple shell script could be written to automate a cracking attempt by non-privileged users on your system. (Admittedly, this is more of a concern if your system had many different users, but UNIX was originally designed to run on a "large" main-frame computer with many (hundreds) users accessing it.)

If you really want your script run with "root" access, just run it as "root" instead of having the script change context in mid-stream.

Note that the same consideration means that sudo will also decline to accept input from a redirected stdin.

If you click the "Report" button at the end of your post and ask the moderators to move this thread to the "security" forum, you should get several additional answers from persons more expert than I am in these matters.
 
Old 07-18-2010, 05:22 PM   #4
x24804
LQ Newbie
 
Registered: Jun 2007
Posts: 12

Original Poster
Rep: Reputation: 0
I didn't realize that su was taking the file that I defined as input. I only expected the read command to deal with the file, not su. I wanted it to prompt for the password like it normally does when there's no file redirect.

Thank you.
 
Old 07-18-2010, 07:47 PM   #5
XavierP
Moderator
 
Registered: Nov 2002
Location: Kent, England
Distribution: Debian Testing
Posts: 19,192
Blog Entries: 4

Rep: Reputation: 475Reputation: 475Reputation: 475Reputation: 475Reputation: 475
As requested, moved to Linux-Security
 
Old 07-19-2010, 02:53 AM   #6
piratesmack
Member
 
Registered: Feb 2009
Distribution: Slackware, Arch
Posts: 519

Rep: Reputation: 142Reputation: 142
Not sure if this is what you want, but I sometimes put this at the beginning of scripts I want run as root.

Code:
[ "$UID" = "0" ] || exec su -c "/bin/sh $0 $*"
 
Old 07-19-2010, 09:12 AM   #7
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,707

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
Quote:
Originally Posted by x24804 View Post
I didn't realize that su was taking the file that I defined as input. I only expected the read command to deal with the file, not su.
When you put the redirection after the while ... done it applies to the whole loop. Try this:

Code:
exec 3< /path/to/file

while IFS=: read -u3 name script
do
    ...
done

exec 3<&-
 
Old 07-20-2010, 01:37 PM   #8
x24804
LQ Newbie
 
Registered: Jun 2007
Posts: 12

Original Poster
Rep: Reputation: 0
ntubski - Thanks. I ended up just reading and stream editing the file based on the line number; I still needed to su within the while loop.
 
  


Reply


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 03:49 PM
Bash for loop rhlee Linux - Software 3 01-12-2010 03:59 AM
bash loop within a loop for mysql ops br8kwall Programming 10 04-30-2008 03:50 AM
BASH - while loop snorkytheweasel Linux - Desktop 2 03-26-2008 09:36 PM
Bash for loop Genjix Programming 5 12-23-2004 02:56 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Security

All times are GMT -5. The time now is 06:34 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration