summaryrefslogtreecommitdiffstats
path: root/ldap_wizard/ldap.pm
blob: 6e82360558432eed12cb7fd8cb45e3ed5305b308 (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
#!/usr/bin/perl -w
# version 0.1
# GPL like
# aginies@mandrakesoft.com

package MDK::Wizard::ldap;
use lib qw(/usr/lib/libDrakX);
use ugtk2;
use strict;
use fcldap;
use common;
use standalone;
use MDK::Wizard::Varspaceval;
use MDK::Wizard::Wizcommon;
use Expect;
require Net::LDAP;

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

my $DC = get_dc();

my $wiz = new MDK::Wizard::Wizcommon;
my $LDAPCONF = "/etc/openldap/slapd.conf";
my $HOME = "/home/";
my $IP = "192.168.100.169";

my $o = {
    name => 'LDAP Configuration Wizard',
    var => {
	defou => 'People',
	dom => 'toto.com',
	rootdse => 'toto.com',
	rootdn => 'manager',
	rootpass => 'secret',
	cn => '',
	sn=> '',
	uid => '',
	uidpass => '',
	lshell => '/bin/bash',
	home => '/home/',
	uidnb => '',
	guinb => '',
	container => '',
	objectclass => 'top,account,posixaccount',
	shadowmax => '99999',
	shadowmin => '-1',
	shadowina => '-1',
	shadowina => '-1',
    },
    needed_rpm => [ 'openldap-servers', 'openldap-clients' ],
    defaultimage => "/usr/share/wizards/dns_wizard/images/DNS.png"
    };
use Data::Dumper; print Dumper($o->{var});


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

my $cn;

$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 'add_userposix';
			     }
			   },
			   data => [
				    {
				     label => N("which operation on LDAP:"), val => \$o->{var}{wiz_level}, list => [ keys %level ], format => sub { $level{$_[0]} } },
				   ],
			   next => 'setldap',
			  },
	       add_userposix => {
				 name => N("Add data in LDAP") . "\n\n" . N("uid, gid, home directory, "),
				 data => [
					  { label => N("First Name:"), val => \$o->{var}{cn} },
					  { label => N("Last Name:"), val => \$o->{var}{sn} },
					  { label => N("User Name:"), val => \$o->{var}{uid} },
					  { label => N("Password:"), val => \$o->{var}{uidpass} },
					  { label => N("Home Directory:"), val => \$o->{var}{home} },
					  { label => N("Login shell:"), val => \$o->{var}{lshell} },
					  { label => N("uid number:"), val => \$o->{var}{uidnb} },
					  { label => N("Group ID:"), val => \$o->{var}{guidnb} },
					 ],
				 post => \&test_add,
				 next => 'summaryadd',
				},
	       setldap => {
			   name => N("LDAP RootDSE

example:
obelx.nux.com

will be in ldap config:

dc=obelx,dc=nux,dc=com

RootDN is the manager of your ldap server."),
			   data => [
				    {
				     label => N("RootDSE"), val => \$o->{var}{rootdse} },
				    {
				     label => N("RootDN"), val => \$o->{var}{rootdn} },
				    {
				     label => N("Password"), val => \$o->{var}{rootpass} },
				    {
				     label => N("Default OU"), val => \$o->{var}{defou} },
				   ],
			   post => \&test_set,
			   next => 'summaryset',
			  },
	       summaryadd => {
			      name => N("Ok Now add entry in LDAP"),
			      data => [ 
				       { label => N("First Name:"), fixed_val => \$o->{var}{cn2} },
				       { label => N("Last Name:"), fixed_val => \$o->{var}{sn2} },
				       { label => N("User Name:"), fixed_val => \$o->{var}{uid2} },
				       { label => N("Password:"), fixed_val => \$o->{var}{uidpass} },
				       { label => N("Home directory:"), fixed_val => \$o->{var}{home} },
				       { label => N("Login shell:"), fixed_val => \$o->{var}{lshell} },
				       { label => N("uid number:"), fixed_val => \$o->{var}{uidnb} },
				       { label => N("Group ID:"), fixed_val => \$o->{var}{guidnb} },
				       { label => N("Container:"), fixed_val => \$o->{var}{container} },
				       { label => N("shadowMax:"), fixed_val => \$o->{var}{shadowmax} },
				       { label => N("shadowMin:"), fixed_val => \$o->{var}{shadowmin} },
				       { label => N("shadowWarning:"), fixed_val => \$o->{var}{shadowina} },
				       { label => N("shadowInactive:"), fixed_val => \$o->{var}{shadowina} },
				       { label => N("shadowExpire:"), fixed_val => \$o->{var}{shadowexpire} },
				       { label => N("objectClass:"), fixed_val => \$o->{var}{objectclass} },
				      ],
			      post => \&do_it_add,
			      next => 'endadd',
			     },
	       summaryset => {
			      name => N("Ok Now building your LDAP configuration") . "\n\n" . N("with this configuration:"),
			      data => [
				       {
					label => N("RootDSE"), fixed_val => \$o->{var}{rootdse} },
				       {
					label => N("RootDN"), fixed_val => \$o->{var}{rootdn} },
				      ],
			      post => \&do_it_setldap,
			      next => 'end'
			     },
	       error_homedir => {
				 name => N("Error in Home directory"),
				 next => 'add_userposix',
				},
	       error_pass => {
			      name => N("Error, pass could not be empty"),
			      next => 'setldap',
			     },
	       error_shell => {
			       name => N("Error in Login shell") . "\n\n" . N("Please choose a correct one"),
			       next => 'add_userposix',
			      },
	       error_nb => {
			    name => N("Error") . "\n\n" . N("Please Should be a number"),
			    next => 'add_userposix',
			   },
	       end => {
		       name => N("Congratulations"),
		       data => [ { label => N("The wizard successfully configured the LDAP.") } ],
		       no_back => 1,
		       end => 1,
		       next => 0
		      },
	       endadd => {
			  name => N("Successfully add data"),
			  data => [ { label => N("The wizard successfully add entry in ldap") } ],
			  no_back => 1,
			  end => 1,
			  next => 0,
			 },
	      };

sub rootdse_to_dc {
    my ($data) = @_;
    my @t = split(/\./, $data);
    $o->{var}{dom} = $t[0];
    my $dcall = join( ',dc=', split(/\./, $data));
    return $dcall;
}


sub test_set {
    $o->{var}{rootpass} or return 'error_pass';
  }

sub get_dc {
  my $DC = "dc=test,dc=com";
  return $DC;
}

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);
    my $dcall = rootdse_to_dc($o->{var}{rootdse});
    print "$dcall\n";
# set suffix
    if (any { /^suffix/ } cat_($LDAPCONF)) {
	substInFile {
	    s/suffix.*/suffix\t\"dc=$dcall\"/;
	} $LDAPCONF;
    } else {
	append_to_file($LDAPCONF, "suffix\t\"dc=$dcall\"");
    }

# s/example/$dcall 
    substInFile {
	s/example/dc=$dcall/g;
    } $LDAPCONF;

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

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

# reset premisions
    system("chown -R root.ldap /etc/openldap");
    system("service ldap restart");
    $o->{var}{rootdse} = "dc=$dcall"; 
    create_rootdse($o);
}

sub test_add {
#    -d $o->{var}{HOME} . "/" . $o->{var}{uid} or return 'error_homedir';
    $o->{var}{uidnb} =~ /\d*/ or return 'error_nb';
    $o->{var}{Guidnb} =~ /\d*/ or return 'error_nb';
    -f $o->{var}{lshell} or return 'error_shell';
}

sub do_it_add {
    return if $::testing;
    my $dcall = rootdse_to_dc($o->{var}{rootdse});
    $o->{var}{rootdse} = "dc=$dcall";
    $o->{var}{rootpass} = "secret";
    add_user_posix($o);
}

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

1;