1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
| #!c:/perl/bin/perl.exe
# ======================================================================
#
# Perl Source File
#
# NAME: reset.cgi
#
# ======================================================================
use strict;
use warnings;
use CGI qw(:standard);
use Win32::OLE 'in';
use Win32::OLE::Const 'Microsoft ActiveX Data Objects';
my $cgi = new CGI;
my $method = "POST";
my $state = 'C:\WINDOWS\Temp\state.msg';
my $resacc;
my $resdom;
my $entry = 'reset_exclude.param';
open (ENTRY,"<$entry") || die "Erreur d'ouverture du fichier $entry , erreur: $!";
my @list = <ENTRY>;
close(ENTRY);
# ======================================================================
# MAIN PROGRAM
# ======================================================================
print $cgi->header();
my $hidden = $cgi->param('hid_field');
if ( $hidden eq 'reset' )
{
Submit_reset();
}
else
{
Submit_auth();
}
print $cgi->end_html();
# ======================================================================
# PROCEDURES
# ======================================================================
# Form generation for authentication
sub AuthForm {
return $cgi->start_form(-method=>$method),
$cgi->h2("Authentication:"),
$cgi->start_table({-border => '1'}),
$cgi->hidden('hid_field','authenticate'),
$cgi->Tr($cgi->th({-align => 'left'},'Login'), $cgi->td($cgi->textfield('auth_login'))),
$cgi->Tr($cgi->th({-align => 'left'},'Password'), $cgi->td($cgi->password_field('auth_password'))),
$cgi->Tr($cgi->th({-align => 'left'},'Domain'), $cgi->td($cgi->popup_menu('auth_domain',
'GIVENCHY_COUT',
'SRV_COUTURE'))),
$cgi->end_table(),
$cgi->p(submit('submit','Logon')),
$cgi->end_form;
}
# Form generation for resetting password
sub ResetForm {
return
$cgi->start_form(-method=>$method),
$cgi->h2("Account to reset:"),
$cgi->h3("The new password will be the account name in minus."),
$cgi->start_table({-border => '1'}),
$cgi->hidden(-name=>'hid_field',-value=>'reset',-override=>1),
$cgi->Tr($cgi->th({-align => 'left'},'Account'), $cgi->td($cgi->textfield('res_account'))),
$cgi->Tr($cgi->th({-align => 'left'},'Domain'), $cgi->td($cgi->popup_menu('res_domain',
'GIVENCHY_COUT' , 'SRV_COUTURE'))),
$cgi->end_table(),
$cgi->p(submit('submit','Reset password')),
$cgi->end_form;
}
# Submit on reset password form
sub Submit_reset {
if ($cgi->param('res_account') and $cgi->param('res_domain'))
{
my $res_account = $cgi->param('res_account');
my $res_domain = $cgi->param('res_domain');
$resacc = $res_account;
$resdom = $res_domain;
my $reset = ResetPassword($res_account,$res_domain);
if ( $reset == 0 )
{
change_password($res_account, $res_domain);
print $cgi->start_html("Password reseted"),h2("Password reseted for $res_account and user must change it at next logon\n");
OpeLogSuccess();
}
else
{
print $cgi->start_html("Password reset failed"),h2("Password reset failed for $res_account\n");
OpeLogFailure();
}
}
else
{
print ResetForm();
}
}
# Submit on logon form
sub Submit_auth {
if ($cgi->param('auth_domain') and $cgi->param('auth_login') and $cgi->param('auth_password'))
{
my $login=$cgi->param('auth_login');
my $password=$cgi->param('auth_password');
my $domain=$cgi->param('auth_domain');
my $auth = AuthenticateUser($domain, $login, $password);
my $delegation = IsDelegation($login,$domain);
if (!$auth and !$delegation)
{
Save_state();
}
else
{
print $cgi->start_html("Authentication failed"),h2("Authentication failed for $login.\nTry again.");
}
}
else
{
print AuthForm();
}
}
# Authenticating NT User
sub AuthenticateUser {
my ($strDomain,$strUserID,$strUserPassword) = @_;
my $strADsPath = 'WinNT://';
$strADsPath = $strADsPath . $strDomain;
my $strAttributeName = "userPrincipalName";
my $strAttributeValue = $strUserID;
my $objNameSpace = Win32::OLE->GetObject ('WinNT:')
or die ("Cannot create LDAP object");
$strUserID = $strDomain . "\\" . $strUserID;
my $objObjSec = $objNameSpace->OpenDSObject($strADsPath , $strUserID, $strUserPassword, 0);
my $result = 0;
my $resulterr = Win32::OLE->LastError();
if ($resulterr==0)
{
$result = 0;
$objObjSec->Close;
} else {
$result = 1;
}
$objNameSpace->Close;
return $result;
}
# User must change password at next logon
sub change_password()
{
my ($strUser, $strDomain) = @_;
$strUser = lc($strUser);
my $objUser = Win32::OLE->GetObject("WinNT://" . $strDomain . "/" . $strUser);
if ( Win32::OLE->LastError() == 0)
{
$objUser->{PasswordExpired} = 1;
$objUser->SetInfo();
}
else
{
print "error\n";
}
$objUser->Close;
}
# Reset NT account password
sub ResetPassword {
my ($strUser, $strDomain) = @_;
$strUser = lc($strUser);
my $strNewPasswd = lc($strUser);
my $result = 0;
my $exclude = 0;
foreach my $x (@list)
{
chomp($x);
$exclude = 1 if ($strUser eq $x);
}
my $objUser = Win32::OLE->GetObject("WinNT://" . $strDomain . "/" . $strUser) or $result = 1;
if ( Win32::OLE->LastError() == 0 and $exclude == 0)
{
if ($objUser->{IsAccountLocked} == 1)
{
$objUser->{IsAccountLocked} = 0;
$objUser->SetInfo();
}
$objUser->SetPassword($strNewPasswd);
if ( Win32::OLE->LastError() == 0 )
{
$result = 0;
$objUser->Close;
}
else
{
$result = 1;
}
}
else
{
$result = 1;
}
return $result;
}
# Check if user is in Delegation group on KEEPALL
sub IsDelegation {
my ($strUser,$strDomain) = @_;
$strUser = uc($strUser);
my $strGroup = "FGA_EDS";
my $deleg = 1;
my $objGroup = Win32::OLE->GetObject("WinNT://" . $strDomain . "/" . $strGroup);
foreach my $objMember (in $objGroup->Members)
{
$deleg = 0 if ($objMember->Name eq $strUser);
}
return $deleg;
}
# Saving the CGI state
sub Save_state {
my $handle = "SAVE";
if (open(SAVE,">$state"))
{
$cgi->save($handle);
close(SAVE);
Submit_reset();
}
else
{
print $cgi->font({-color=>'red'},"Failed to save CGI state: $!");
}
}
# Log all operation in a file if success
sub OpeLogSuccess {
my $file = 'pwd_reset.log';
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
$mon+=1;
$year+=1900;
my $date = "$mday/$mon/$year";
my $host = `hostname`;
chomp($host);
my ($user,$dom);
my $handle = "LOAD";
if (open(LOAD,$state))
{
my $oldcgi = new CGI ($handle);
close(LOAD);
if (my $olduser=$oldcgi->param('auth_login') and my $olddomain=$oldcgi->param('auth_domain'))
{
$user = $olduser;
$dom = $olddomain;
}
}
my $line = "$date - $user reseted password for user $resacc @ $hour:$min:$sec";
open(OPELOG,">>$file") or die "Can't open $file for update: $!";
print OPELOG "$line\n";
close(OPELOG);
}
# Log all operation in a file if failure
sub OpeLogFailure {
my $file = 'pwd_reset.log';
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
$mon+=1;
$year+=1900;
my $date = "$mday/$mon/$year";
my $host = `hostname`;
chomp($host);
my ($user,$dom);
my $handle = "LOAD";
if (open(LOAD,$state))
{
my $oldcgi = new CGI ($handle);
close(LOAD);
if (my $olduser=$oldcgi->param('auth_login') and my $olddomain=$oldcgi->param('auth_domain'))
{
$user = $olduser;
$dom = $olddomain;
}
}
my $line = "$date - $user reset password failed for user $resacc @ $hour:$min:$sec";
open(OPELOG,">>$file") or die "Can't open $file for update: $!";
print OPELOG "$line\n";
close(OPELOG);
} |
Partager