LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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
  Search this Thread
Old 01-10-2007, 07:00 PM   #1
WilliamS
Member
 
Registered: Nov 2003
Location: 46N 76W
Distribution: Slackware 14.1
Posts: 380

Rep: Reputation: 31
Question bash scripting advice?


I've put the ip-up shell script in /etc/ppp, and realized that I needed to start Tor to make the other stuff work.
Tor doesn't work.
The other two commands work as they are supposed to.

What have I done wrong?

This is the contents of /etc/ppp/ip-up

#!/bin/bash
# Proper header for a Bash script.

sleep 10s

tor
#start privacy app

sleep 30s
#allow time for tor to get info

/usr/sbin/ntpdate ca.pool.ntp.org | /sbin/hwclock -w
#update system clock and hardware clock

sleep 5s
#wait for 5 seconds to be sure&see on gkrellm that it did it.

/home/william/Mix/mixmaster --update-stats noreply
#update mixmaster stats from noreply

exit # The right and proper method of "exiting" from a script.
 
Old 01-10-2007, 07:46 PM   #2
blackhole54
Senior Member
 
Registered: Mar 2006
Posts: 1,896

Rep: Reputation: 61
Does tor work if you just give the command from the command line?

Is the location of tor contained in the the PATH variable in the context the script is running?
 
Old 01-10-2007, 08:05 PM   #3
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Do what blackhole54 says ...

And if you want tor to start and keep running after the script exits you gotta add an ampersand (not sure if this is what you want (it probably is tho), but as soon as the script exits all processes started by the script go down, including 'tor' unless it's 'tor &'):

Code:
tor &
Also, change

Code:
exit
to
Code:
exit 0 # the real proper way to exit without error

# exit 0 = exit without error, script was sucessful

and one more thing ... pls use code tags for code :)
# exit 1 = exit with error, script failed

Also, change

Code:
#!/bin/bash
to the prefered and more portable:

Code:
#!/bin/sh
sh symlinks to bash anyway for most machines.

And one more thing, pls use code tags for code

Last edited by H_TeXMeX_H; 01-10-2007 at 08:15 PM.
 
Old 01-10-2007, 09:09 PM   #4
WilliamS
Member
 
Registered: Nov 2003
Location: 46N 76W
Distribution: Slackware 14.1
Posts: 380

Original Poster
Rep: Reputation: 31
tor starts and works OK when I write it on the command line as so.

Aha! that's what the mysterious & is for.
I thought of the context and changed the command to /usr/local/bin/tor &

What are code tags? I only write a shell script. Thought you have to know what you are doing to write code.
Really.

"Is the location of tor contained in the the PATH variable in the context the script is running?" I don't know what that is or where to find it.
 
Old 01-10-2007, 09:10 PM   #5
blackhole54
Senior Member
 
Registered: Mar 2006
Posts: 1,896

Rep: Reputation: 61
Quote:
Originally Posted by H_TeXMeX_H
And if you want tor to start and keep running after the script exits you gotta add an ampersand (not sure if this is what you want (it probably is tho), but as soon as the script exits all processes started by the script go down, including 'tor' unless it's 'tor &'):
I don't know how I missed that one! Actually if you don't use the ampersand and tor actually starts running, the script won't proceed any further until tor exits!
 
Old 01-10-2007, 09:18 PM   #6
blackhole54
Senior Member
 
Registered: Mar 2006
Posts: 1,896

Rep: Reputation: 61
Quote:
Originally Posted by WilliamS
I thought of the context and changed the command to /usr/local/bin/tor &
And did that work? Giving the complete path like you just did is an alternative to worrying about the PATH variable. The PATH variable tells bash where to search for commands. For more info, see the bash man page.

Quote:
What are code tags?
H_TeXMeX_H is talking about using code tags when you post on LQ. This causes your code (or script, or whatever) to be listed in one of the boxes with "code" written above it. You can use the pound sign (or hash mark for those not on the English system) icon at the top of the compose box to do this.
 
Old 01-10-2007, 09:38 PM   #7
WilliamS
Member
 
Registered: Nov 2003
Location: 46N 76W
Distribution: Slackware 14.1
Posts: 380

Original Poster
Rep: Reputation: 31
Tor still doesn't work.
Here's the whole thing:
#!/bin/sh


sleep 10s

/usr/local/bin/tor &
#start privacy app

sleep 30s
#allow time for tor to get info

/usr/sbin/ntpdate ca.pool.ntp.org | /sbin/hwclock -w
#update system clock and hardware clock

sleep 5s
#wait for 5 seconds to be sure&see on gkrellm that it did it.

/home/william/Mix/mixmaster --update-stats noreply
#update mixmaster stats from noreply

exit 0 # The right and proper method of "exiting" from a script.



The code tag doesn't work either. #

Last edited by WilliamS; 01-10-2007 at 09:39 PM.
 
Old 01-10-2007, 10:10 PM   #8
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Well, that script should work. So you're saying the other 2 commands work ? How do you know ? Make sure you have the permissions set on the file at least a+x:

Code:
chmod a+x /etc/ppp/ip-up
Code tags are

[ccode] code here [/ccode] ccode = code
 
Old 01-10-2007, 10:42 PM   #9
WilliamS
Member
 
Registered: Nov 2003
Location: 46N 76W
Distribution: Slackware 14.1
Posts: 380

Original Poster
Rep: Reputation: 31
In cli I did $ sh /etc/ppp/ip-up 2>/home/william/build/wtf
and got in that file :

Sorry, only the superuser can change the Hardware Clock.
10 Jan 23:24:52 ntpdate[3512]: bind() fails: Permission denied


It seems as if Tor is now running, but not at the right time (?), and the mixmaster update works also.

Looks as if chmod a+x /etc/ppp/ip-up was right for two of the commands.
Late now, will have another go tomorrow.

Thanks for advice.
 
Old 01-11-2007, 01:04 PM   #10
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Quote:
Originally Posted by WilliamS
In cli I did $ sh /etc/ppp/ip-up 2>/home/william/build/wtf
and got in that file :

Sorry, only the superuser can change the Hardware Clock.
10 Jan 23:24:52 ntpdate[3512]: bind() fails: Permission denied



It seems as if Tor is now running, but not at the right time (?), and the mixmaster update works also.

Looks as if chmod a+x /etc/ppp/ip-up was right for two of the commands.
Late now, will have another go tomorrow.

Thanks for advice.
You must be superuser to run the script ... that's your problem. How are you running this script, when and where ? If you can, first su into root and then run it, or ... there is another solution (chown the file to root and then setting the suid bit), but it may compromise security in some situations. First tell me how you are running it and I'll tell you the most sensible solution.
 
Old 01-11-2007, 09:38 PM   #11
blackhole54
Senior Member
 
Registered: Mar 2006
Posts: 1,896

Rep: Reputation: 61
Quote:
Originally Posted by H_TeXMeX_H
there is another solution (chown the file to root and then setting the suid bit), but it may compromise security in some situations.
Running a script setuid root is considered a security risk because of the way the environment can change behavior. And when I tried to do it once anyway, it didn't work. Something blocked setuid root from working with a script. I have forgotten details of how this was blocked, but I know it was. I don't know if this is distro specific or not.

A way that will work and is safe if you are careful, is to write a real short C program that calls the script with a nearly null environment whose contents are carefully controlled by the C program, and then set the C program's binary to setuid root. For security, the script sould be called with its full path, should be writable only by root and should be in a directory that is writable only by root. I learned this technique from usernetctl which is used to allow a normal user to run ifup and ifdown scripts as root. This program also does some other checks to make sure non-root users can't alter the scripts.

I am wondering if the OP really should be running this script as a startup script.

EDIT: The OP might also be able to run the script as root using sudo, but would have the same security considerations. If desired, it could be setup where a password was not required to run it. In any case, it is desirable to not run tor as root to limit the damage of any security holes that may be present.

Last edited by blackhole54; 01-11-2007 at 09:46 PM.
 
Old 01-11-2007, 09:57 PM   #12
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Yeah, I guess it gets kinda complicated. Can't you just start tor as non-root and do the rest as root in the script. I mean, just run tor as regular user, then run the script as root (remove starting tor from the script, of course):

Code:
$ tor &
$ su <Enter root password>
# /etc/ppp/ip-up

Last edited by H_TeXMeX_H; 01-11-2007 at 09:59 PM.
 
Old 01-11-2007, 11:35 PM   #13
WilliamS
Member
 
Registered: Nov 2003
Location: 46N 76W
Distribution: Slackware 14.1
Posts: 380

Original Poster
Rep: Reputation: 31
Tor should be run as user, who needs to be scolded?
I can't write C.
I was hoping to get pppd to do this, as beating the start-up time of the other two programs can be a nuisance. I had fcron running them before this project.

The puzzle is why the shell script will work when I run it from cli, as user, but not when ppp starts.

# ls -la /etc/ppp/ip-up
-rwxr-xr-x 1 root root 348 2007-01-11 16:51 /etc/ppp/ip-up

This is the current form:
[ccode]#!/bin/bash
/usr/local/bin/tor &&
#start privacy app
#sleep 30s
#allow time for tor to get info
#/usr/sbin/ntpdate ca.pool.ntp.org | /sbin/hwclock -w &&
#update system clock and hardware clock
#/home/william/Mix/mixmaster --update-stats noreply
#update mixmaster stats from noreply
exit 0 # The right and proper method of "exiting" from a script.[/ccode]

I wonder if there should be an "exit" for an app that doesn't halt until ppp is down?

Last edited by WilliamS; 01-11-2007 at 11:41 PM.
 
Old 01-12-2007, 02:12 AM   #14
blackhole54
Senior Member
 
Registered: Mar 2006
Posts: 1,896

Rep: Reputation: 61
Quote:
Originally Posted by WilliamS
This is the contents of /etc/ppp/ip-up
Color my face very red! I must have been having a bad day when I first read the above statement. Its signifcance (i.e. what ip-up is) didn't hit me. It just did. Like a wrecking ball!

ip-up should already be running as root. (Yes, WilliamS, you knew this, but I missed it and maybe H_TeXMeX_H did too.)

So the problem with permissions/owners is to downgrade tor and maybe mixmaster (I don't know anything about mixmaster so I can't advise) rather than upgrading the time stuff. Also, according to pppd's man page, pppd calls ip-up and the rest of the scripts with an empty environment except for some special variables outlined in its man page. I believe this means no PATH variable! tor may be depending on $PATH or some other environmental variables to work. (BTW, when you ran ip-up manually, these variables would be set but you weren't running as root.)

I think we can kill two birds with one stone here. I am proposing using

Code:
su - $tor_user -c "killall tor  &> /dev/null && sleep 2;  /usr/local/bin/tor &> /dev/null  &"
to start tor. The killall command is just a precaution in case there is already an instance of tor running. The sleep is executed only if something was actually killed; it gives that process time to die. $tor_user (you need to define it in your script) refers to the user you want to run tor. I've created a special user just for that purpose so that in the event of a security breach (w/o escalation) the attacker can't compromise any other account. The dash after the su tells su to do a normal login to that account. That is so the normal environment gets set up rather than inheriting the restricted environment that ip-up is using.

If $tor_user can successfully run tor interactively, I think this should work. I don't have an easy way to test it out. It is very similar to the way I bring tor up, except I have tor running on a different box and use ssh instead of su. If there is a problem, try moving the final ampersand outside the parenthesis, leaving a space before it.



Quote:
I was hoping to get pppd to do this
To make sure I have made myself clear, the above was with the assumption you are starting it through pppd and ip-up.

Quote:
I wonder if there should be an "exit" for an app that doesn't halt until ppp is down?
I would suggest killing tor in /etc/ppp/ip-down using a killall command like I did above. If you are uneasy about doing this as root, you can su to $tor_user again. If you decide to do this as root, you will need to provide the complete path to killall.

Again, I am very about missing this the first time through!

Good luck!

EDIT: spelling correction and clarification

Last edited by blackhole54; 01-12-2007 at 02:20 AM.
 
Old 01-12-2007, 08:33 PM   #15
WilliamS
Member
 
Registered: Nov 2003
Location: 46N 76W
Distribution: Slackware 14.1
Posts: 380

Original Poster
Rep: Reputation: 31
Thanks much, blackhole54, it works now. I installed that line exactly, tested OK. Then changed > /dev/null to > ~/jk and looked at the output, and it says:

Jan 12 20:02:13.466 [warn] tor_init(): You are running Tor as root. You don't need to, and you probably shouldn't.

I think the problem is here " $tor_user (you need to define it in your script) refers to the user you want to run tor."
I've created a new user, but how do I define it in the script? Just writing $user1 in place of $tor_user didn't do it.
 
  


Reply



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
I Kindly need some linux scripting project advice mosesmungai Programming 4 12-05-2006 08:59 AM
Bash Scripting newb.. Advice needed. trey85stang Linux - General 5 09-28-2006 12:05 PM
help on bash scripting Kendo1979 Linux - Newbie 3 05-17-2005 07:10 PM
need help with bash scripting rich2oo1 Programming 2 12-17-2003 12:50 PM
BASH scripting help Chucklez Linux - Newbie 4 12-12-2002 12:07 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware

All times are GMT -5. The time now is 04:16 PM.

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