PDA

View Full Version : Need some help check if user exists


henkie009
06-24-2004, 08:18 PM
Hello,

I need some help with my script.
I must add an option to check if an given username allready exist or not.
How can i add this to the script i've allready been developing this far.

#!/usr/bin/perl

use HTTP::Request;
use HTTP::Response;
use LWP::UserAgent;

$PrintDebug = 1; # Print debugging information
$baseURL = "Some url to asp file with the data";
$ip = &get_ip();

sub Debug(@){
$strDebug = shift;
if ($PrintDebug == 1) { print("$strDebug\n"); }
}

sub get_ip {
my ($myip, @iparr) = (undef, []);
$_ = `ifconfig`;
@iparr = /inet addr:(\d+\.\d+\.\d+\.\d+)/ig;
$myip = $iparr[0];
return $myip;
}

# Aanspreken hosting.asp file via http request
sub ExecHttpReq(@)
{
my $URL = shift;
my $ua = new LWP::UserAgent;
$ua->agent( "Routit Backend");
die "No useragent" unless defined($ua);
$req = new HTTP::Request('GET',$URL);
return $ua->request($req);
}

$response = ExecHttpReq("$baseURL/hosting.asp");

if( !$response->is_success ){
print $response->status_line;
print " FAILED\n";
exit 1;
}

# splitten van hosting.asp file voor het gebruiken van de data
@lines = split /\n/, $response->content;
for( $i=0; $i<= $#lines; $i++ ){
($ID, $ProductCode, $DealerKlantID,
$Naam, $DealerID, $Bedrijf,
$Username, $Password, $Domainname,
$ASP, $PHP, $Stats,
$Tag, $server) = split /\t/, $lines[$i];
$status = 0; #OK, account created

# Kijken of de hosting niet voor windows is
chomp $PHP;
chomp $Tag;
if($PHP =~ /true/) {
$status = createuser($Username, $Password);
$status = createvirtualhost();
# updatestatus($ID, $status);
}
# Kijken of dat er stats nodig zijn
if($Stats=~ /true/) {
$status = awstats();
}
if($Tag =~ /2/) {
$status = deleteall();
}
if($Tag=~ /3/){
$status = deletestats();
}

# Sub maken van user
sub createuser(@) {
Debug ("Making User");
chomp $Username;
Debug("Setting Password");
chomp $Password;
system("adduser -g webusers -d /var/www/$Domainname $Username");
system("echo \"$Password\" | /usr/bin/passwd --stdin $Username");
Debug("User added...");
chmod (0750, "/var/www/$Domainname");
Debug("Setting permissions...");
return 0; # account create
}

# Sub hosting status
sub updatestatus(@) {
ExecHttpReq("$baseURL/hosting.asp?ID=$ID#Status=$status");
}

# Sub voor het maken van Virtualhosts
sub createvirtualhost(@) {
Debug("setting domainname");
Debug( "mkdir $Domainname");
print("mkdir() ", mkdir("/etc/httpd/conf/virtualhosts/$Bedrijf", 0750),"\n");
print "making apache virtualhost dir\n";
open(APACHECONF, ">/etc/httpd/conf/virtualhosts/$Bedrijf/$Domainname.conf");
$strCONF = "<VirtualHost $ip>\n";
$strCONF .= "\tDocumentRoot /var/www/$Domainname\n";
$strCONF .= "\tServerName $Domainname\n";
$strCONF .= "\tServerAlias www.$Domainname\n";
$strCONF .= "\tServerAdmin webmaster\@$Domainname\n";
$strCONF .= "\tErrorLog logs/$Domainname.error.log \n";
$strCONF .= "\tCustomLog /var/log/httpd/$Domainname.log combined\n";
$strCONF .= "</VirtualHost>\n";
print APACHECONF $strCONF;
print "helemaal klaar !\n";
close(APACHECONF);
return 0; # Virtual host file created
}

print "Restarting Apache deamon\n";
system("killall -HUP httpd");
print "Klaar.\n";

# Sub voor het maken van de AWStats
sub awstats(@) {
Debug ("Making the dir and setting the awstats settings");
print("mkdir() ", mkdir("/var/www/$Domainname/stats", 0750),"\n");
print("chmod() ", chmod(".R /var/www/$Domainname/stats/", 0750),"\n");
system("cp -R /sys/scripts/awstats/icon/ /var/www/$Domainname/stats/");
system("chown -R root:webusers /var/www/$Domainname/stats");

# Het openen en editten van de template van awstats..

$domain = "$Domainname";
open(TEMPLATE, "</sys/scripts/awstats/awstats.conf");
open(CONFIG, "> /etc/awstats/awstats.$Domainname.conf");
@TEMPLATE=<TEMPLATE>;
for (@TEMPLATE) {
s/<DOMAIN>/$Domainname/g;
}

print CONFIG "@TEMPLATE\n";
close TEMPLATE;
close CONFIG;

return 0; # awstats done !


}

sub deleteall(@) {
Debug ("Setting up for all delete");
print ("rm() ", rm("/var/www/$Domainname/stats"),"\n"); # lets see if this works
print ("rm() ", rm("/var/www/$Domainname"),"\n"); # lets see if this works
print ("rm() ", rm("/etc/httpd/conf/virtualhosts/$Bedrijf"),"\n");
# print ("rm() ", rm("/etc/awstats/awstats.$Domainname.conf"),"\n);

}
#sub deletestats(@) {
}

And ofcourse if u see something that can get better don't be shy and let me know ;)

dswimboy
07-06-2004, 03:11 AM
i am only slightly familiar with unix, so i can guide you to an answer, but can't give you one.

i could have sworn there was a list of users on a unix system, but a quick google has yeilded nothing. some my recommendation: find a linux forum, and ask them how to list all users from the command-line. input this data, then search through the list.

if you need a fix now...well, here's the complicated version. list all the directories in /var/www. i'm assumming that will be the list of users...then take the data from the list.

ah, another idea popped into my head. i assume you're trying to check if another user exists, because you're getting an error when you try to create a new user, when an old one with that name exists. this could be used to your advantage! like this:
system("adduser -g webusers -d /var/www/$Domainname $Username") or &repeat_user; then add a sub, with that name, and have it output the fact that a user already has that name/space/whatever.

YUPAPA
07-09-2004, 12:35 PM
Hello,

I need some help with my script.
I must add an option to check if an given username allready exist or not.
How can i add this to the script i've allready been developing this far.

And ofcourse if u see something that can get better don't be shy and let me know ;)

If you are talking about unix account, try the getpwnam function.


if(getpwnam('username')) {
print "User exists\n";
}


I assume you are making a script to create apache virtualhost and unix account. If I were you, I would break this into modules.