LinuxQuestions.org
Visit Jeremy's Blog.
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 08-15-2012, 05:20 PM   #1
px87
LQ Newbie
 
Registered: Nov 2011
Posts: 18

Rep: Reputation: Disabled
Permissions problem with perl script to change users password


Hi all!

I'm writting a script in perl to change users's password from a web browser.

I'm using the Unix::PasswdFile module and I'm having a problem with it, when I run the script from the brower I get this error (from apaches's erro.log file):


Code:
[Wed Aug 15 13:00:02 2012] [error] [client 192.168.1.2] Can't locate Unix/PasswdFile.pm in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.14.2 /usr/local/share/perl/5.14.2 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.14 /usr/share/perl/5.14 /usr/local/lib/site_perl .) at ./login_chpass.pl line 6., referer: http://192.168.1.50/admon/cambiar_pass/cambiar_pass.html

[Wed Aug 15 13:00:02 2012] [error] [client 192.168.1.2] BEGIN failed--compilation aborted at ./login_chpass.pl line 6., referer: http://192.168.1.50/admon/cambiar_pass/cambiar_pass.html
If I run it from the command line it works perfectly.

I set www-data as the owner of the file, but It still doesn't work.

This is my script:

Code:
#login_chpass.pl

#! /usr/bin/perl -w

use CGI qw(:all);
use strict;
use warnings;
use Authen::Simple::PAM;
use Unix::PasswdFile;

my $cgi = new CGI;
print $cgi->header();

my $pam = Authen::Simple::PAM->new( service => 'login' );

my $pw  = new Unix::PasswdFile "/etc/passwd";
my $resultado = "";

#my $username = $cgi->param('user');
#my $pass_old = $cgi->param('pass_old');
#my $pass_new = $cgi->param('pass_new');

my $username = $ARGV[0];
my $pass_old = $ARGV[1];
my $pass_new = $ARGV[2];

# I use the Authen::Simple::PAM module to login the user with his current password
if ( $pam->authenticate( $username, $pass_old ) ) {

	# Here I change the user password
	$pw->passwd( $username, $pw->encpass($pass_new) );
	$pw->commit();

	# I save the exit value
	$resultado = $? >> 8;

	# If the exit value is 0 the password change is ok
	if ( $resultado == 0 ) {
		print "* Password del sistema cambiado correctamente!\n";
	}
	# If the exit value is different to 0 there is an error and I print the exit value
	else {
		print "* Error al cambiar password del sistema, error: $resultado\n";
	}
}
else {
	# If exist a login error, I print:
	print "* Error de Login!";
}

Then I changed the login_chpass.pl file owner to root and I tried to use something like an "interface" to try to run the script with sudo, but It doesn't work (I modified the sudoers file to grant privileges to the www-data user):


Code:
#ch_pass_int.pl

#! /usr/bin/perl -w

use CGI qw(:all);
use strict;
use warnings;

my $cgi = new CGI;
print $cgi->header();

my $login = "./login_chpass.pl";

#my $username = $ARGV[0];
#my $pass_old = $ARGV[1];
#my $pass_new = $ARGV[2];

my $username = $cgi->param('user');
my $pass_old = $cgi->param('pass_old');
my $pass_new = $cgi->param('pass_new');

system qq(sudo $login $username $pass_old $pass_new);
It seems to be a problem with permissions, but I don't know what permissions are wrong.


I hope you can help me. Thanks a lot!!

Last edited by px87; 08-15-2012 at 05:25 PM.
 
Old 08-15-2012, 06:16 PM   #2
jonsg
LQ Newbie
 
Registered: Sep 2009
Location: Cambridge, UK
Distribution: More than I can mention!
Posts: 4

Rep: Reputation: 0
It seems pretty straight-forward to me. When you're running it from the command line, you may have your include path set up so that Unix/PasswdFile.pm can be found on it. But when it's run from the browser (as if logged in as uid www-data, gid www-data), the path doesn't include where you have it installed.

Either you need to reinstall it in a system-visible place (i.e. one of the standard places PERL looks - see the first error line for the list), or you need to add its install location to PERL's search path for when it's invoked through the browser.

Hope this helps.
 
Old 08-15-2012, 09:32 PM   #3
px87
LQ Newbie
 
Registered: Nov 2011
Posts: 18

Original Poster
Rep: Reputation: Disabled
Hi jonsg, thanks for your answer:

* "you need to add its install location to PERL's search path": How can I do it? I know is needed change some enviroment variables, but I don't know what to modify.

* I searched the PasswdFile location and the module is installed in the root directory, I think that is the problem, what do you think? and how can I solved?

Code:
# find / -name PasswdFile.pm
/root/perl5/lib/perl5/Unix/PasswdFile.pm
/root/.cpan/build/Unix-ConfigFile-0.06-wJWP_t/blib/lib/Unix/PasswdFile.pm
/root/.cpan/build/Unix-ConfigFile-0.06-wJWP_t/PasswdFile.pm
Thanks a lot!!
 
  


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
script to change samba users password saifurab Programming 1 02-05-2010 12:59 AM
script to change samba users password saifurab Programming 1 02-04-2010 05:12 AM
shell or perl script to change ftp user password vivek rai Linux - Newbie 4 03-15-2009 01:09 PM
perl script, users and permissions djgerbavore Programming 1 12-29-2004 12:41 PM

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

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