summaryrefslogtreecommitdiffstats
path: root/ldap_wizard/ldapdef.pm
blob: d1227217ef93e85e21ca8ae24226809ae64c3e9c (plain)
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
package MDK::Wizard::ldapdef;

use strict;
use Net::LDAP;
use Net::LDAP::Util qw(ldap_error_text);
use MDK::Common;
use standalone;
use common;
use ugtk2 qw(:dialogs :create);

require Exporter;

use vars qw(@ISA @EXPORT %cfg %cfgfile $congfile $msg $attrs %ldap $ldap);
@ISA=qw(Exporter);
@EXPORT=qw( %cfg %cfgfile $congfile ldap_connect root_bind $msg anonymous_bind ldap_search get_dn $attrs get_dse %ldap add_user);


# Connect to Ldap server
sub ldap_connect {
  my ($u) = @_;
  my $ldap = Net::LDAP->new($u->{var}{srv}) or err_dialog("Error!", "Unable to connect to %s", $u->{var}{srv});
  return $ldap;
}

# bind root
sub root_bind {
  my ($ldap, $u) = @_;
  my $mesg = $ldap->bind(dn => $u->{var}{rootdn},password => $u->{var}{rootpass});
  return $mesg;
}


sub anonymous_bind {
  my ($ldap) = @_;
  my $mesg = $ldap->bind;
  return $mesg;
}

sub ldap_search {
  my ($ldap, $filter, $basedn) = @_;
  my $attrs = ['objectClass'];
  my $mesg = $ldap->search(
			 base => $basedn,
			 filter => $filter,
			 scope => "sub",
			 attrs => $attrs
			);
  my $href = $mesg->as_struct;
  print("Search results\n");
  print $href;
}

sub get_dn {
  my ($ldap, $filter, $basedn) = @_;
  my $mesg = $ldap->search(
			 base => $basedn,
			 filter => $filter,
			 scope => "sub",
*			 #attrs => $attrs
			);
  #my $entry = "";
  #foreach $entry ($mesg->entries) {
  #print "show DN ".$entry->dn."\n";
  #}
  return $mesg
}

sub get_dse() {
  my $ldap = ldap_connect();
  my $result = get_dn($ldap, "(objectclass=organization)", $cfg{base});
  my @arrayOfDNs = $result->entries;
  #my %ldap = ();
  foreach my $dn_value (@arrayOfDNs)
    {
      my $orga = $dn_value->dn;
      $ldap{orga} = $orga;
    }
  return %ldap;
}

sub add_user {
  my ($u) = @_;
  my $ldap = ldap_connect($u);
  root_bind($ldap,$u);
  my $result = $ldap->add(
			  "uid=$u->{var}{uid},$u->{var}{defou},$u->{var}{suffix}",
			  attr => [
				   objectclass => ["top", "posixAccount", "inetOrgPerson", "shadowAccount"],
				   sn => $u->{var}{sn},
				   uid => $u->{var}{uid},
				   cn =>  $u->{var}{cn},
				   userPassword => $u->{var}{uidpass},
				   loginShell => $u->{var}{lshell},
				   uidNumber => $u->{var}{uidnb},
				   gidNumber => $u->{var}{gidnb},
				   homeDirectory => $u->{var}{home} . $u->{var}{uid},
				   shadowMin => '-1',
				   shadowMax => '999999',
				   shadowWarning => '7',
				   shadowInactive => '-1',
				   shadowExpire => '-1',
				  ]
			 );
#  create_dialog("", ldap_error_text($result->code));
			 #$result->code && die $result->error;
  return $result->error, ldap_error_text($result->code);
  #return  ldap_error_text($result->code);
}