summaryrefslogtreecommitdiffstats
path: root/ldap_wizard/fcldap.pm
blob: 128340289295d1512acab83e511b828f39a358d6 (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
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
package fcldap;
use strict;
use Net::LDAP;
use Net::LDAP::LDIF;
use MDK::Common;
use vars qw(@ISA @EXPORT);
use Exporter;


@ISA = qw(Exporter);
@EXPORT = qw(cnx_ldap_ano
	     cnx_ldap_user
	     find_user_posix
	     add_user_posix
	     create_rootdse
	     create_rootdse_file
	     add_ou
		tutu
	     );

sub cnx_ldap_ano {
    my ($u) = @_;
    my $ldap = Net::LDAP->new($u->{var}{srv}) or die $@;
    my $mesg = $ldap->bind;
    return ($ldap);
}

sub cnx_ldap_user {
    my ($u) = @_;
	print "cnx_ldap_user not connect\n";
    my $ldap = Net::LDAP->new($u->{var}{srv}) or die $@;  
	print "cnx_ldap_user \n";
	print "$u->{var}{rootdn} \n";
	print "$u->{var}{rootdse} \n";
	print "$u->{var}{rootpass} \n";
    my $mesg = $ldap->bind( "$u->{var}{rootdn},$u->{var}{rootdse}",
			 password => "$u->{var}{rootpass}", 
			 );
	
	print "$ldap \n";
    return ($ldap);
}

sub create_rootdse_file {
    my ($u) = @_;
    output('/tmp/rootdse.ldif', <<EOF);
dn: $u->{var}{rootdse}
objectclass: dcObject
objectclass: organization
o: Example Company
dc: $u->{var}{dom}

dn: cn=$u->{var}{rootdn},$u->{var}{rootdse}
objectclass: organizationalRole
cn: $u->{var}{rootdn}


dn: ou=$u->{var}{defou},$u->{var}{rootdse}
objectClass: top
objectClass: organizationalUnit
ou: $u->{var}{defou}
description: $u->{var}{defou}
EOF
}

sub create_rootdse {
    my ($u) = @_;
    create_rootdse_file($u);  

    print "$u->{var}{rootdse}\n";
    print "$u->{var}{rootdn}\n";
    print "$u->{var}{rootpass}\n";
    print "$u->{var}{defou}\n";
    print "$u->{var}{dom}\n";

    my $mesg = system("ldapadd -x -D 'cn=$u->{var}{rootdn},$u->{var}{rootdse}'  -f /tmp/rootdse.ldif -w $u->{var}{rootpass}");

    return($mesg);

}

sub find_user_posix  {
    my ($u) = @_;
    my $dn;
    my $ldap = cnx_ldap_ano('loki');
    my $mesg = $ldap->search (base => "$u->{var}{basedn}",
			      scope => $u->{var}{scope},
			      filter => "(&(objectclass=posixAccount)(uid=$u->{var}{uid})",
			      );
    foreach my $entry ($mesg->all_entries) {
	  my $dn = $entry->dn;
	  print "$dn \n";
    }
}

sub add_ou
{
    my ($u) = @_;
    print  "add_ou \n";
    my $ldap = cnx_ldap_user($u);
    print "add_ou \n";
	my $result = $ldap->add( "ou=$u->{var}{defou},$u->{var}{rootdse} ",
		attr => [
			objectclass => ["top","organizationalUnit"],
			ou => $u->{var}{defou},
			description => $u->{var}{defou},
			]
		);

	print "Create Default OU=$u->{var}{defou} \n";
    $result->code && warn "failed to add entry: ", $result->error ;
    my $mesg = $ldap->unbind;      # take down session
}



sub add_user_posix
{
	my ($u) = @_;
	my $uid = $u->{var}{uid};
    my $ldap=Net::LDAP->new('127.0.0.1') or die "$@";
    my $mesg = $ldap->bind( "cn=$u->{var}{rootdn},$u->{var}{rootdse}",
	    password => "$u->{var}{rootpass}"
                            );
			   
	my $shell = "/bin/zsh";
	my $uid = "vincent" ;
    #my $ldap = cnx_ldap_user('loki','manager','secret');
    my $result = $ldap->add( "uid=$uid,ou=$u->{var}{defou},$u->{var}{rootdse}",
                             attr => [
                                      objectClass =>  ["top","account","posixAccount","shadowAccount"],
                                      uid => $uid,
                                      cn  => 'toto',
                                      userPassword => 'test',
				      userPassword => 'pass',
				      loginShell   => '/bin/bash',
				      #loginShell   => $u->{var}{lshell},
                                      uidNumber    => '501',
                                      gidNumber    => '501',
                                      homeDirectory => '/home/toto',
                                      shadowMin =>  "-1",
                                      shadowMax => "999999",
                                      shadowWarning =>  "7",
                                      shadowInactive => "-1",
                                      shadowExpire  => "-1",
                                      ]
                             );

    $result->code && warn "failed to add entry: ", $result->error ;
    $mesg = $ldap->unbind;      # take down session

}

1;