LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > CentOS
User Name
Password
CentOS This forum is for the discussion of CentOS Linux. Note: This forum does not have any official participation.

Notices


Reply
  Search this Thread
Old 07-17-2017, 03:05 PM   #1
robertkwild
Member
 
Registered: Feb 2015
Posts: 382

Rep: Reputation: Disabled
html form to submit data to bash script


hi all,

im going to design a web html form so users can input what username and password they want to make the ftp account, once they enter in a username and password they click on the submit button and it submits it to a bash script and then the bash script will run and finish of making the ftp account

the variables of both the html form and bash script are the same ie user passwd

it doesnt work, what am i doing wrong?

here is my html form page -

<form>
<form action="/scripts/create_user.sh" method="post">
Username:<br>
<input type="text" name="user"><br><br>
Password:<br>
<input type="text" name="passwd"><br><br>
<input type="submit" value="Submit">

</form>

what the form looks like -

https://s12.postimg.org/5rcs8rbb1/ftp_web.png

my bash script -

#!/bin/bash

dir=/sftp
group=sftpusers

echo "$user:$passwd" >> /ftp_details/accounts.csv

useradd -g $group -d $dir/$user -s /sbin/nologin $user
mkdir -p $dir/$user/data
chown root $dir/$user
chmod 755 $dir/$user
chown $user $dir/$user/data
chmod 755 $dir/$user/data
touch $dir/$user/data/WARNING_everything_in_here_will_get_removed_in_14_days_time.txt

cheers,

rob
 
Old 07-17-2017, 03:59 PM   #2
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by robertkwild View Post
hi all,

im going to design a web html form so users can input what username and password they want to make the ftp account, once they enter in a username and password they click on the submit button and it submits it to a bash script and then the bash script will run and finish of making the ftp account

the variables of both the html form and bash script are the same ie user passwd

it doesnt work, what am i doing wrong?

here is my html form page -

<form>
<form action="/scripts/create_user.sh" method="post">
Username:<br>
<input type="text" name="user"><br><br>
Password:<br>
<input type="text" name="passwd"><br><br>
<input type="submit" value="Submit">

</form>

what the form looks like -

https://s12.postimg.org/5rcs8rbb1/ftp_web.png

my bash script -

#!/bin/bash

dir=/sftp
group=sftpusers

echo "$user:$passwd" >> /ftp_details/accounts.csv

#not declared or $USER gets current user logged name
#see code block below;
#so this can never work $user it has no value within the var
# $passwd cannot work it has not been declared nor a value
#
# after that everything else fails.

useradd -g $group -d $dir/$user -s /sbin/nologin $user
mkdir -p $dir/$user/data
chown root $dir/$user
chmod 755 $dir/$user
chown $user $dir/$user/data
chmod 755 $dir/$user/data
touch $dir/$user/data/WARNING_everything_in_here_will_get_removed_in_14_days_time.txt

cheers,

rob
http://172.17.1.1/create.html ??? what is 172.17.1.1
googled it
http://172.17.1.1/login - Google Plus

Code:
userx%slackwhere ⚡ ~ ⚡> echo $USER
userx
userx%slackwhere ⚡ ~ ⚡> echo $PASSWD

userx%slackwhere ⚡ ~ ⚡> echo $passwd

userx%slackwhere ⚡ ~ ⚡> echo $user                                                                           

userx%slackwhere ⚡ ~ ⚡>
Code:
user=
passwd=
if this is what is suppose to be getting your $user and $passwd
Code:
echo "$user:$passwd" >> /ftp_details/accounts.csv
that is so backwards : out >> << in

plus you're going to have to parse your file to find the values you need then add them to your vars in your bash script.

Last edited by BW-userx; 07-17-2017 at 04:09 PM.
 
Old 07-17-2017, 04:04 PM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,759

Rep: Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931
Your bash script has to parse the form variables from the QUERY_STRING environment variable.
In addition your bash script needs to be in the /var/www/cgi-bin directory.
Is selinux active? If so you need to permit script execution.

By default useradd requires root privileges. You could setup sudo and add the apache user to the sudoers file to run useradd although it is a bit of a security risk.

You should add some checks to verify the new user does not already exist as well as verify that username and password only contain valid characters.

This is old but does show an example of a bash cgi-bin script.

http://www.yolinux.com/TUTORIALS/BashShellCgi.html

172.17.1.1 is a private IP address (172.16.0.0 - 172.31.255.255)

Last edited by michaelk; 07-17-2017 at 04:07 PM.
 
Old 07-17-2017, 04:17 PM   #4
robertkwild
Member
 
Registered: Feb 2015
Posts: 382

Original Poster
Rep: Reputation: Disabled
I have echo and a read commands for both the user and passwd commands

echo "Enter UserName:"
read user

echo "Enter Password:"
read passwd
 
Old 07-17-2017, 04:21 PM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,759

Rep: Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931
Quote:
Originally Posted by robertkwild View Post
I have echo and a read commands for both the user and passwd commands

echo "Enter UserName:"
read user

echo "Enter Password:"
read passwd
Not sure I understand how this fits into your web form?
 
Old 07-17-2017, 04:26 PM   #6
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by robertkwild View Post
I have echo and a read commands for both the user and passwd commands

echo "Enter UserName:"
read user

echo "Enter Password:"
read passwd
Code:
read -p "hey you what's your name. hummmm? " user
read -sp "so $user - you know I am going to need a password to get any further 
         care to provide me with one, hummm? " passwd
why not add a little flare?

read -p "message" VarName

Last edited by BW-userx; 07-17-2017 at 04:29 PM.
 
Old 07-17-2017, 04:37 PM   #7
Laserbeak
Member
 
Registered: Jan 2017
Location: Manhattan, NYC NY
Distribution: Mac OS X, iOS, Solaris
Posts: 508

Rep: Reputation: 143Reputation: 143
As I said to someone who wanted to do something similar, this is very dangerous, especially when exposed to everyone on the Internet. Of course such a thing is possible, but you need to think carefully about the wisdom of doing it.
 
Old 07-18-2017, 03:29 AM   #8
robertkwild
Member
 
Registered: Feb 2015
Posts: 382

Original Poster
Rep: Reputation: Disabled
if it helps the useradd comand i run as root when i run the script

i do have some checks in place if the user already exists -

if id $user ; then
echo "$user already exists as you can see above, please re-run the script"
exit
else
echo "$user not in system, ok to continue"
fi
 
Old 07-18-2017, 04:43 AM   #9
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,759

Rep: Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931
Web html forms run as apache user not root and as stated can not run useradd by default. Again by allowing apache to run useradd your exposing your system to possible exploits. You need to filter the user name and limit length to 32.
 
Old 07-18-2017, 06:10 AM   #10
robertkwild
Member
 
Registered: Feb 2015
Posts: 382

Original Poster
Rep: Reputation: Disabled
luckily in my script i made a check to see if a user already exists as i copied my script in the cgi-bin folder in /var/www and i get this

https://s13.postimg.org/aqqc7fslz/create.png

this is my actual script, the script above i edited as i just wanted the script to add the ftp account and not do the echo/read and if statements as i wanted the html to do that bit

#!/bin/bash

dir=/mnt/sftp
group=sftp_users

echo "Enter UserName:"
read user

if id $user ; then
echo "$user already exists as you can see above, please re-run the script"
exit
else
echo "$user not in system, ok to continue"
fi

echo "Enter Password:"
read passwd
echo "$user:$passwd" >> /ftp_details/accounts.csv

useradd -g $group -d $dir/$user -s /sbin/nologin $user
mkdir -p $dir/$user/data
chown root $dir/$user
chmod 755 $dir/$user
chown $user $dir/$user/data
chmod 755 $dir/$user/data
touch $dir/$user/data/WARNING_everything_in_here_will_get_removed_in_14_days_time.txt

echo $user:$passwd | chpasswd

Last edited by robertkwild; 07-18-2017 at 06:12 AM.
 
Old 07-18-2017, 06:42 AM   #11
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,759

Rep: Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931
See the link in post #3.

You need to write a new script to parse the form post inputs and not use read statements. Although you can write the script in just about any language the most common to use for web development is typically php, ruby, perl or python for linux.
 
Old 07-18-2017, 06:47 AM   #12
robertkwild
Member
 
Registered: Feb 2015
Posts: 382

Original Poster
Rep: Reputation: Disabled
thanks michael, mmm may need to learn another programming language other than bash, i thought i could do all this via html intergrating with my bash script

i have read the post you linked, wheres the command to pass the variable from the html to the bash script?

Last edited by robertkwild; 07-18-2017 at 06:51 AM.
 
Old 07-18-2017, 06:58 AM   #13
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,759

Rep: Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931
From the link provided it is a bit more involved but no you can not use your script as is.
 
Old 07-18-2017, 07:06 AM   #14
robertkwild
Member
 
Registered: Feb 2015
Posts: 382

Original Poster
Rep: Reputation: Disabled
can i do this via an html form and make a submit button which passes on the users input to the bash script?
 
Old 07-18-2017, 08:48 AM   #15
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,759

Rep: Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931
We seem to be going around in circles. Read the link. Do some research on cgi , dynamic html programming. Also reread the posts and consider the security implications.

Last edited by michaelk; 07-18-2017 at 09:29 AM.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] html form - Unable to have 2 form 'submit' on same line. rblampain Programming 3 11-26-2015 09:57 PM
[SOLVED] Click 'submit' in a remote web form from Bash, really geokker Programming 14 06-21-2011 04:27 AM
[SOLVED] HTML Form that sends data entered to a bash script as variables simplified Programming 2 12-01-2009 03:35 PM
html form with multiple submit button schlabs General 2 11-10-2007 11:56 AM
Script to get form data..in bash? jrfly Linux - General 1 06-29-2005 10:04 AM

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

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