You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

change_ldap_pass.pl 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/usr/bin/perl
  2. =pod
  3. Script to change the LDAP password using the set_password method
  4. to proper setting the password policy attributes
  5. author: Zbigniew Szmyd (zbigniew.szmyd@linseco.pl)
  6. version 1.0 2016-02-22
  7. =cut
  8. use Net::LDAP;
  9. use Net::LDAP::Extension::SetPassword;
  10. use URI;
  11. use utf8;
  12. binmode(STDOUT, ':utf8');
  13. my %PAR = ();
  14. if (my $param = shift @ARGV){
  15. print "Password change in LDAP\n\n";
  16. print "Run script without any parameter and pass the following data:\n";
  17. print "URI\nbaseDN\nFilter\nbindDN\nbindPW\nLogin\nuserPass\nnewPass\nCAfile\n";
  18. exit;
  19. }
  20. foreach my $param ('uri','base','filter','binddn','bindpw','user','pass','new_pass','ca'){
  21. $PAR{$param} = <>;
  22. $PAR{$param} =~ s/\r|\n//g;
  23. }
  24. my @servers = split (/\s+/, $PAR{'uri'});
  25. my $active_server = 0;
  26. my $ldap;
  27. while ((my $serwer = shift @servers) && !($active_server)) {
  28. my $ldap_uri = URI->new($serwer);
  29. if ($ldap_uri->secure) {
  30. $ldap = Net::LDAP->new($ldap_uri->as_string,
  31. version => 3,
  32. verify => 'require',
  33. sslversion => 'tlsv1',
  34. cafile => $PAR{'ca'});
  35. } else {
  36. $ldap = Net::LDAP->new($ldap_uri->as_string, version => 3);
  37. }
  38. $active_server = 1 if ($ldap);
  39. }
  40. if ($active_server) {
  41. my $mesg = $ldap->bind($PAR{'binddn'}, password => $PAR{'bindpw'});
  42. if ($mesg->code != 0) {
  43. print "Cannot login: ". $mesg->error;
  44. } else {
  45. # Wyszukanie users wg filtra
  46. $PAR{'filter'} =~ s/\%login/$PAR{'user'}/;
  47. my @search_args = (
  48. base => $PAR{'base'},
  49. scope => 'sub',
  50. filter => $PAR{'filter'},
  51. attrs => ['1.1'],
  52. );
  53. my $result = $ldap->search(@search_args);
  54. if ($result->code) {
  55. print $result->error;
  56. } else {
  57. my $count = $result->count;
  58. if ($count == 1) {
  59. my @users = $result->entries;
  60. my $dn = $users[0]->dn();
  61. $result = $ldap->bind($dn, password => $PAR{'pass'});
  62. if ($result->code){
  63. print $result->error;
  64. } else {
  65. $result = $ldap->set_password(newpasswd => $PAR{'new_pass'});
  66. if ($result->code) {
  67. print $result->error;
  68. } else {
  69. print "OK";
  70. }
  71. }
  72. } else {
  73. print "User not found in LDAP\n" if $count == 0;
  74. print "Found $count users\n";
  75. }
  76. }
  77. }
  78. $ldap->unbind();
  79. } else {
  80. print "Cannot connect to any server";
  81. }