summaryrefslogtreecommitdiffstats
path: root/ldap_wizard/ldap.pm
blob: 63b8a88637df34b3de273f8f14af1062f8f2ffbe (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
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#!/usr/bin/perl
# version 0.1
# GPL like
# aginies@mandrakesoft.com

package MDK::Wizard::Ldap;
use lib qw(/usr/lib/libDrakX);
use ugtk2;
use strict;
use ldapdef;
use common;
use standalone;
use MDK::Wizard::Varspaceval;
use MDK::Wizard::Wizcommon;
use Expect;
require Net::LDAP;
use Net::LDAP::Util qw(ldap_error_text);

#unless ($> == 0) {
#  die "You are not root Exiting\n";
#}



my $o = {
         name => N("Add POSIX account"),
         var => {
                 defou => 'ou=Users',
                 srv => '',
                 dom => 'mandrakesoft.com',
                 suffix => '',
                 rootdn => '',
                 rootpass => '',
                 rootpass2 => '',
                 cn => '',
                 sn => '',
                 uid => '',
                 uidpass => '',
                 lshell => '/bin/bash',
                 home => '/home/',
                 uidnb => '1004',
                 gidnb => '1004',
                 container => 'container',
                 objectclass => 'top,account,posixaccount',
                 shadowmax => '99999',
                 shadowmin => '-1',
                 plop => '',
                 tmp => '',
                 nom => '',
                 prenom => '',
                },
         needed_rpm => [ 'squid' ],
         defaultimage => "/usr/share/wizards/proxy_wizard/images/proxy.png"
        };



my $ldap_suffix=join(',dc=', split(/\./, `hostname -f`));
$ldap_suffix =~ s/[^,]*,//;
my $conf_file = "/etc/sysconfig/ldapconf";
my $LDAPCONF = "/etc/openldap/slapd.conf";

##### Read conf file is exist

my $hostname = `hostname`;
$o->{var}{srv} = $hostname;

if (-f $conf_file) {

    my %conf = getVarsFromSh($conf_file);
    $o->{var}{suffix} = $conf{suffix};
    $o->{var}{rootdn} = $conf{rootdn};
    $o->{var}{rootpass} = $conf{rootpass};
    $o->{var}{srv} = $conf{srv};
    $o->{var}{defou} = $conf{users};

} else {

    my $hostname = `hostname`;
    $o->{var}{srv} = $hostname;	
    $o->{var}{suffix} = $ldap_suffix;	

}


my %level = (
             1 => N("Server - Set configuration of LDAP server"),
             2 => N("Add - Add user in LDAP server"),
            );

$o->{pages} = {
               welcome => {
                           name => N("LDAP configuration wizard") . "\n\n" . N("Setup a LDAP server."),
                           no_back => 1,
                           pre => sub { 
                               $o->{var}{wiz_level} ||= 1;
                           },
                           post => sub { 
                               if ($o->{var}{wiz_level} == 2) {
                                   return 'check_conffile';
                               }
                           },
                           data => [
                                    {
                                     label => N("Your choice:") . "\n\n", val => \$o->{var}{wiz_level}, type => 'list', list => [ keys %level ], format => sub { $level{$_[0]} } },
                                   ],
                           next => 'set_srv',
                          },
		check_conffile => { name => N("Config Server "),
				data => [ 
					 { label => N("Server Name: "), val => \$o->{var}{srv} },
					 { label => N("Server RootDN: "), val => \$o->{var}{rootdn} },
					 { label => N("RootDN Password: "), val => \$o->{var}{rootpass}, hidden => 1 },
					 { label => N("Server Suffix: "), val => \$o->{var}{suffix} },
					 { label => N("Server Default Users OU: "), val => \$o->{var}{defou} },
					],
				next => 'add_userposix'
			},
               add_userposix => {
                                 name => N("LDAP User Add") . "\n\n" . N("User Create in: ") . $o->{var}{defou} . "," . $o->{var}{suffix},
                                 data => [
                                          {
                                           label => N("First Name:"),  val => \$o->{var}{sn} },
                                          {
                                           label => N("Name:"),  val => \$o->{var}{cn} },
                                          {
                                           label => N("User Name:"),  val => \$o->{var}{uid} },
                                         ],
                                 next => 'password'
                                },
               password => {
                            name => N("LDAP User Password") . "\n\n" . N("Password for Users: ").$o->{var}{sn}.",".$o->{var}{sn}.",". $o->{var}{cn} . $o->{var}{defou} . "," . $o->{var}{suffix},
                            data => [
                                     {
                                      label => N("Password:"),  val => \$o->{var}{uidpass}, hidden => 1 },
                                     {
                                      label => N("Password (again):"),  val => \$o->{var}{uidpass2}, hidden => 1 },
                                    ],
                            complete => sub {
                                if (!$o->{var}{uidpass}) {
                                    $::in->ask_warn(N("Error"), N("You must enter a password for LDAP."));
                                    return 1;
                                }
                                if (!($o->{var}{uidpass} eq $o->{var}{uidpass2})) {
                                    $::in->ask_warn(N("Error"), N("The passwords do not match"));
                                    return 1;
                                }
                            },
		      
                            next => 'summary_add_user'
                           },

               set_srv => {
                           name => N("Configuring LDAP"),
                           data => [
                                    {
                                     label => N("LDAP Suffix:"),   val => \$o->{var}{suffix} },
                                    {
                                     label => N("LDAP Administrator:"),   val => \$o->{var}{rootdn} },
                                    {
                                     label => N("LDAP Password:"), hidden => 1,  val => \$o->{var}{rootpass} },
                                    {
                                     label => N("LDAP Password (again):"), hidden => 1,   val => \$o->{var}{rootpass2} },
                                   ],
                           complete => sub {
                               if (!$o->{var}{suffix}) {
                                   $::in->ask_warn(N("Error"), N("You must enter a suffix for LDAP."));
                                   return 1;
                               }
                               if (!$o->{var}{rootpass}) {
                                   $::in->ask_warn(N("Error"), N("You must enter a password for LDAP."));
                                   return 1;
                               }
                               if (!($o->{var}{rootpass} eq $o->{var}{rootpass2})) {
                                   $::in->ask_warn(N("Error"), N("The passwords do not match"));
                                   return 1;
                               }
                           },
                           next => 'summary_set_srv',

                          },
               summary_add_user => {
                                    name =>  N("Confirmation de l'utilisateur a creer"),
                                    data => [
                                             {
                                              label => N("Name:"), type => 'field', fixed_val => \$o->{var}{sn} },
                                             {
                                              label => N("First name:"), type => 'field', fixed_val => \$o->{var}{cn} },
                                             {
                                              label => N("User Name:"), type => 'field', fixed_val => \$o->{var}{uid} },
                                             {
					      label => N("Create in:"), type => 'field', val => $o->{var}{defou}.$o->{var}{suffix} },
                                            ],
                                    post => \&do_it_user_add,
                                    next => 'endadd'
                                   },
               summary_set_srv => {
                                   name =>  N("Confirmation de l'utilisateur a creer"),
                                   data => [
                                            {
                                             label => N("LDAP Suffix:"), type => 'field', val => $o->{var}{suffix} },
                                           ],
                                   post => \&do_it_setldap,
                                   next => 'end'
                                  },
               end => {
                       name => N("Congratulations"),
                       data => [ { label => N("The wizard successfully configured the LDAP.") } ],
                       no_back => 1,
                       end => 1,
                       next => 0
                      },
               endadd => {
                          name => N("Successfully added User"),
                          data => [ { label => N("The wizard successfully added an user in LDAP") } ],
                          no_back => 1,
                          end => 1,
                          next => 0,
                         },
              };

sub do_it_setldap {
    if (!-f $LDAPCONF) {
        die "no $LDAPCONF found";
    }
    cp_af($LDAPCONF, $LDAPCONF . '.save');
    if (!-f "$LDAPCONF.example") {
        cp_af($LDAPCONF, $LDAPCONF . '.example');
    }
    cp_af($LDAPCONF . '.example', $LDAPCONF);
    print "$o->{var}{suffix}\n";
    # set suffix
    if (any { /^suffix/ } cat_($LDAPCONF)) {
        substInFile {
            s/suffix.*/suffix\t"$o->{var}{suffix}"/;
        } $LDAPCONF;
    } else {
        append_to_file($LDAPCONF, qq(suffix\t"$o->{var}{suffix}"));
    }

    # s/example/$dcall 
    substInFile {
        s/example/$o->{var}{suffix}/g;
    } $LDAPCONF;

    # set rootdn
    if (any { /^rootdn/ } cat_($LDAPCONF)) {
        substInFile {
            s/rootdn.*/rootdn\t"cn=$o->{var}{rootdn},$o->{var}{suffix}"/;
        } $LDAPCONF;
    } else {
        append_to_file($LDAPCONF, qq(rootdn\t"$o->{var}{rootdn},$o->{var}{suffix}"));
    }

    # set rootpass
    my $cryptpass = crypt($o->{var}{rootpass}, "crypt");
    if (any { /^rootpw/ } cat_($LDAPCONF)) {
        substInFile {
            #s/rootpw.*/rootpw             $cryptpass/;
            s/rootpw.*/rootpw             $o->{var}{rootpass}/;
        } $LDAPCONF;
    } else {
        #append_to_file($LDAPCONF, "rootpw             {CRYPT}$cryptpass");
        append_to_file($LDAPCONF, "rootpw             $o->{var}{rootpass}");
    }


    # create config file in /etc/sysconfig/ldapconf

    output($conf_file, <<EOF);
srv=$o->{var}{srv}
suffix=$o->{var}{suffix}
rootdn=$o->{var}{rootdn}
rootpass=$o->{var}{rootpass}
users=$o->{var}{defou}
EOF

    # reset premisions
    system("chown -R root.ldap /etc/openldap");
    system("service ldap restart");
    init_ldap();
}


sub init_ldap {
    my $root_cn = (split('=', (split(/,/, $o->{var}{rootdn}))[0]))[1];
    my $ldap_dc = (split('=', (split(/,/, $o->{var}{suffix}))[0]))[1];

    my $LDAP;
    open($LDAP, "| ldapadd -x -D 'cn=$o->{var}{rootdn},$o->{var}{suffix}' -w $o->{var}{rootpass}");

    print $LDAP <<RootLdif;
dn: $o->{var}{suffix}
dc: $ldap_dc
objectClass: dcObject
objectClass: organization
o:  Company

dn: cn=$o->{var}{rootdn},$o->{var}{suffix}
objectClass: organizationalRole
cn: $o->{var}{rootdn}

dn: ou=Hosts,$o->{var}{suffix}
ou: Hosts
description: OU Hosts
objectClass: top
objectClass: organizationalUnit

dn: ou=Users,$o->{var}{suffix}
ou: Users
description: OU Users
objectClass: top
objectClass: organizationalUnit

dn: ou=Group,$o->{var}{suffix}
ou: Group
description: OU Group
objectClass: top
objectClass: organizationalUnit


RootLdif

    close($LDAP)
}




sub do_it_user_add {
    return if $::testing;
    my $ldap = ldap_connect($o);
    my $mesg = add_user($o);
}

sub new {
    my ($class) = @_;
    bless {
           o   => $o,
          }, $class;
}

1;