LinuxQuestions.org
Help answer threads with 0 replies.
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 02-19-2008, 12:40 AM   #1
john83reuben
Member
 
Registered: Oct 2007
Location: Kuala Lumpur,Malaysia
Distribution: Debian Etch, OpenSuse
Posts: 132

Rep: Reputation: 17
Problem in scripting


I am trying to develop something just for fun by using perl & cgi scripting with linux.

What I am trying to do is, creating user from the web.

This is my html script

<HTML>
<BODY>
<FORM METHOD="POST" ACTION="/cgi-bin/myscript2.cgi">
<PRE>
First Name <INPUT TYPE="text" NAME="name" MAXLENGTH=15 SIZE=15>
<INPUT TYPE="submit" VALUE="Send Mail!">
<INPUT TYPE="reset" value=" Clear-Form">
</PRE>
</FORM>
</BODY>
</HTML>

This script suppose to get user input.

then myscript2.cgi looks like tis

#!/usr/bin/perl

use strict;
use CGI;

my $q = new CGI;
my $name = $q->param( "name" );

system("/etc/sbin/useradd $name");

print $q->header( "text/html" ),
$q->start_html( "Welcome" ),
$q->p( "Hi $name!" ),
$q->end_html;

BUt it does not create the users. I wonder y. This system lacks in security. BUt I am doing it only for fun in localhost. Im learning step by step phase.if can create users. I wana figure it out how it can be done securely.
But my question is how to receive the parameter from the web and use the parameter with useradd comand and create the user.

HOpe u guys can give me ideas
 
Old 02-19-2008, 12:47 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
If you redirect stdout & stderr in the system cmd (or check the Apache error_log) you'll find that the cmd runs as apache (or www or nobody) and doesn't have privs (root) to create users (iirc)
 
Old 02-19-2008, 01:06 AM   #3
john83reuben
Member
 
Registered: Oct 2007
Location: Kuala Lumpur,Malaysia
Distribution: Debian Etch, OpenSuse
Posts: 132

Original Poster
Rep: Reputation: 17
Do you mean that myscript2.cgi is executed by Apache and not LInux. Apache cant execute Linux commands. So what should I do, to make LInux execute the script or the command (system("/etc/sbin/useradd $name"))

Last edited by john83reuben; 02-19-2008 at 01:08 AM.
 
Old 02-19-2008, 02:35 AM   #4
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
A couple of points.
  1. You can only create users as root or via sudo
  2. You should not run CGI scripts with root powers!

If you run CGI scripts as root then you are asking for a security problem.
 
Old 02-20-2008, 01:46 AM   #5
john83reuben
Member
 
Registered: Oct 2007
Location: Kuala Lumpur,Malaysia
Distribution: Debian Etch, OpenSuse
Posts: 132

Original Poster
Rep: Reputation: 17
OK now I have succesfully create users from the web. That means I can enter a username, n a new user will be created in my linux box. BUt i have use only useradd to create the user. That means the particular user doesnt have a password. so how to let the user create his own password for his username from the web. Do I have to configure any files?

Guys, now I am doing this in my linux box and under localhost, not for everyone. So, when i can able to complete this, then i can concentrate the security part. So currently, its not about the security. Security is the second phase. So please dont worry about the security now. I will definetely will enhance my fun project with added security in near future.

HOpe to receive some ideas from the experts..Thank You
 
Old 02-20-2008, 02:26 AM   #6
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
To change or set a password you need to use the passwd command: /usr/bin/passwd

However this requires input, which cannot just be a straight echo command.

Example 1:
Code:
echo "Testing
Testing"|/usr/bin/passwd TestUser
You will see that this fails!

This is the same if you enter the passwords in a file and cat to passwd.

Testpass:
Code:
Testing
Testing
Example 2:
Code:
cat Testpass|/usr/bin/passwd TestUser
You can use named pipes but I don't think this is going to provide a soloution for you.

Named pipe Example (session 1):
Code:
mkfifo myfifo
cat > myfifo
Named pipe Example (session 2):
Code:
cat myfifo|/usr/bin/passwd TestUser
Named pipe Example (session 1 - continued):
Code:
Testing
Testing
Have you thought of looking at Webmin, as this may already do everything that you are after.
 
Old 02-20-2008, 02:52 AM   #7
john83reuben
Member
 
Registered: Oct 2007
Location: Kuala Lumpur,Malaysia
Distribution: Debian Etch, OpenSuse
Posts: 132

Original Poster
Rep: Reputation: 17
Yea, but Webmin does things for us. I am just playing around with my box. So I wanted to try this stuff first. Can we modify the adduser or the passwd configuration files to make his possible.
 
Old 02-21-2008, 02:42 PM   #8
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
To create a password without using passwd, you need to use crypt.

Modified CGI Script:

Code:
#!/usr/bin/perl

use strict;
use CGI;
my $SALT="Someth!ngC0mP1eX";

my $q = new CGI;
my $name = $q->param( "name" );
my $pass = $q->param( "passwd" );

my $enc_pass=crypt($pass, $SALT);

system("/etc/sbin/useradd -p $enc_pass $name");

print $q->header( "text/html" ),
$q->start_html( "Welcome" ),
$q->p( "Hi $name!" ),
$q->end_html;
To include the password field in the form:
Code:
<HTML>
   <BODY>
      <FORM METHOD="POST" ACTION="/cgi-bin/myscript2.cgi">
         <PRE>
First Name: <INPUT TYPE="text" NAME="name" MAXLENGTH=15 SIZE=15>
Password  : <INPUT TYPE="password" NAME="passwd" MAXLENGTH=15 SIZE=15>
<INPUT TYPE="submit" VALUE="Send Mail!">
<INPUT TYPE="reset" value=" Clear-Form">
         </PRE>
      </FORM>
   </BODY>
</HTML>
 
Old 02-27-2008, 10:24 PM   #9
john83reuben
Member
 
Registered: Oct 2007
Location: Kuala Lumpur,Malaysia
Distribution: Debian Etch, OpenSuse
Posts: 132

Original Poster
Rep: Reputation: 17
Yea I found the solution.GOt help from the forums too. Thanks alot guys
 
  


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
scripting problem bhert Linux - Newbie 4 10-10-2006 12:24 AM
BASH scripting problem danreed007 Programming 5 04-24-2006 05:02 AM
teaching shell scripting: cool scripting examples? fax8 Linux - General 1 04-20-2006 04:29 AM
Problem with scripting 'mysqldump' pshankland Linux - Software 2 04-05-2006 12:38 PM
BASH scripting problem deadlock Programming 5 08-15-2003 04:33 AM

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

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