1 |
#!/usr/bin/perl |
2 |
use strict; |
3 |
use File::Copy; |
4 |
|
5 |
# Als erstes: Sorry fuer ENGLISH/DEUTSCH Mischmasch |
6 |
# Zweitens: Was ist das?? |
7 |
# Also: |
8 |
# 1. Legt LDAP PosixAccounts an(Zum anmelden ueber PAM) |
9 |
# 2. DomainAbaehngige HOMDEDIRs |
10 |
# Ach schau dir den CODE an. |
11 |
# Kommando uebergabe (CMD/TEST(see sub GetUserDataAuto)/HTML(Webmin) |
12 |
|
13 |
my $howtotell = "HTML"; |
14 |
|
15 |
if ($howtotell eq "HTML") { |
16 |
use CGI; |
17 |
my $cgi = new CGI; |
18 |
use CGI::Carp qw(fatalsToBrowser); |
19 |
} |
20 |
|
21 |
## ACHTUNG ### auf ALLE folgenden Parameter achten |
22 |
|
23 |
my $current_dir = "/usr/libexec/webmin/ums"; |
24 |
##Wenn jemand eine Funktion kennt das aktuelle Perl-Arbeitsverzeichnis zu ermitteln, bitte! |
25 |
my $ldap_start_uid = "999"; |
26 |
my $ldap_stop_uid = "20000"; |
27 |
# Letzters gilt auch fuer Gruppen |
28 |
my $mailuserpath = "/mailuser"; |
29 |
#Where to put your DomainDirs |
30 |
my $template = "/template/www"; |
31 |
#your Horde Template |
32 |
my $group_www_root = "/www"; |
33 |
my $mail_root = "/mail"; |
34 |
#your Users IMAP dir (under $HOME) |
35 |
my $user_www_root = "/public_html"; |
36 |
#your Users WWW_ROOT (under $HOME) |
37 |
my $base_dn = "ou=MailUser,ou=UMS,dc=europatentdienst,dc=de"; |
38 |
#LDAP Base DN for PosixAccounts |
39 |
my $horde_base_dn = "ou=Horde,ou=UMS,dc=europatentdienst,dc=de"; |
40 |
#LDAP Base DN for Horde |
41 |
my $root_dn = "'cn=Manager,dc=europatentdienst,dc=de'"; |
42 |
my $root_pass = "MEAS876"; |
43 |
# You should know what this means |
44 |
my $ldiffile = "/root/devel/perl/test.ldif"; |
45 |
my $ldif_dn_file = "/root/devel/perl/test_dn.ldif"; |
46 |
my $ldif_imap_file = "/root/devel/perl/test_imap.ldif"; |
47 |
my $ldif_horde_file = "/root/devel/perl/test_horde.ldif"; |
48 |
# These files are needed to generate LDIFs-->LDAPADD->LDAPServer |
49 |
my $virtualhost_file = "/etc/mail_test/virtusertable"; |
50 |
my $local_host_name_file = "/etc/mail_test/local-host-names"; |
51 |
my $genericstable_file = "/etc/mail_test/genericstable"; |
52 |
# Your Sendmail Files |
53 |
my $make_bin = "/usr/bin/make"; |
54 |
my $make_target = "/etc/mail_test"; |
55 |
my $remote_sendmail = "/usr/sbin/rcsendmail"; |
56 |
my $sendmail_command = "restart"; |
57 |
# I'am tiered now, sure you know what this means |
58 |
|
59 |
Main(); |
60 |
|
61 |
sub GetEnt_Passwd { |
62 |
my @biggest_uid; |
63 |
my @biggest_gid; |
64 |
my @existing_users; |
65 |
my @getent_passwd; |
66 |
my $foreach_counter; |
67 |
@getent_passwd = qx(/usr/bin/getent passwd); |
68 |
foreach (@getent_passwd) { |
69 |
@getent_passwd[$foreach_counter] =~ /(.*):x:(\d*):(\d*).*/; |
70 |
if ($2 > $ldap_start_uid && $2 < $ldap_stop_uid) { |
71 |
@existing_users[$foreach_counter] = $1; |
72 |
@biggest_uid[$foreach_counter] = $2; |
73 |
@biggest_gid[$foreach_counter] = $3; |
74 |
} |
75 |
$foreach_counter++; |
76 |
} |
77 |
|
78 |
@biggest_uid = sort(NummernSort @biggest_uid); |
79 |
@biggest_gid = sort(NummernSort @biggest_gid); |
80 |
@biggest_uid = reverse(@biggest_uid); |
81 |
@biggest_gid = reverse(@biggest_gid); |
82 |
my $HighestUID = @biggest_uid[0]; |
83 |
my $HighestGID = @biggest_gid[0]; |
84 |
my $UidNumber = $HighestUID + 1; |
85 |
my $GidNumber = $HighestGID + 1; |
86 |
my %HighestIDs = (HighestUID => $HighestUID,HighestGID => $HighestGID,UidNumber => $UidNumber,GidNumber => $GidNumber); |
87 |
return (%HighestIDs); |
88 |
} |
89 |
|
90 |
sub NummernSort { |
91 |
if ($a < $b) |
92 |
{return -1;} |
93 |
elsif($a == $b) |
94 |
{return 0; } |
95 |
else |
96 |
{return 1; } |
97 |
} |
98 |
|
99 |
sub GetUserDataCMD { |
100 |
my %UserDaten; |
101 |
print "Vorname: "; |
102 |
my $vorname=<STDIN>; |
103 |
chomp $vorname; |
104 |
print "Nachname: "; |
105 |
my $nachname=<STDIN>; |
106 |
chomp $nachname; |
107 |
print "Domain(info.de): "; |
108 |
my $domain=<STDIN>; |
109 |
chomp $domain; |
110 |
print "Benutzername: "; |
111 |
my $username=<STDIN>; |
112 |
chomp $username; |
113 |
print "Password: "; |
114 |
my $password=<STDIN>; |
115 |
chomp $password; |
116 |
my $imap = ""; |
117 |
until ($imap eq "yes" | $imap eq "no") { |
118 |
print "Imap/HomeDir/ModDav/User_WWW(yes/no):"; |
119 |
$imap=<STDIN>; |
120 |
chomp $imap; |
121 |
} |
122 |
print "Email(Name vor dem @):"; |
123 |
my $email =<STDIN>; |
124 |
chomp $email; |
125 |
$email = $email."\@".$domain; |
126 |
my %UserDaten = (Vorname=>$vorname,Nachname=>$nachname,Domain=>$domain,Benutzername=>$username,Password=>$password,Imap=>$imap,EMail=>$email); |
127 |
return (%UserDaten); |
128 |
} |
129 |
|
130 |
sub GetUserDataAuto { |
131 |
my $vorname="Test"; |
132 |
my $nachname="Tester"; |
133 |
my $domain="testing.de"; |
134 |
my $username="testtester"; |
135 |
my $password="test(/&"; |
136 |
my $imap="yes"; |
137 |
my $email= "testtester"."\@".$domain; |
138 |
my %UserDaten = (Vorname=>$vorname,Nachname=>$nachname,Domain=>$domain,Benutzername=>$username,Password=>$password,Imap=>$imap,EMail=>$email); |
139 |
return (%UserDaten); |
140 |
} |
141 |
|
142 |
sub CreateLDAPUser { |
143 |
my $cn = shift; |
144 |
my $user = $cn; |
145 |
my $domain = shift; |
146 |
my $pass = shift; |
147 |
my $surename = shift; |
148 |
my $givenname = shift; |
149 |
my $uidNumber = shift; |
150 |
my $gidNumber = shift; |
151 |
my $homeDir = shift; |
152 |
my $imap = shift; |
153 |
my $email = shift; |
154 |
|
155 |
$domain =~ /(.*)\.(.*)/; |
156 |
my $domain_dn = "ou=".$1."_".$2; |
157 |
my $domain_dir = "/".$1."_".$2; |
158 |
|
159 |
my @LDIFDATEI; |
160 |
@LDIFDATEI[0] = "dn: uid=$givenname $surename,$domain_dn,$base_dn\n"; |
161 |
@LDIFDATEI[1] = "objectclass: top\n"; |
162 |
@LDIFDATEI[2] = "objectclass: posixAccount\n"; |
163 |
@LDIFDATEI[3] = "objectclass: shadowAccount\n"; |
164 |
@LDIFDATEI[4] = "objectclass: account\n"; |
165 |
@LDIFDATEI[5] = "cn: $cn\n"; |
166 |
@LDIFDATEI[6] = "uid: $user\n"; |
167 |
@LDIFDATEI[7] = "gecos: $givenname $surename\n"; |
168 |
@LDIFDATEI[8] = "uidNumber: $uidNumber\n"; |
169 |
@LDIFDATEI[9] = "gidNumber: $gidNumber\n"; |
170 |
@LDIFDATEI[10] = "homeDirectory: $homeDir\n"; |
171 |
@LDIFDATEI[11] = "userPassword: $pass\n"; |
172 |
@LDIFDATEI[12] = "loginShell: /bin/false\n"; |
173 |
|
174 |
if ($imap eq "yes") { |
175 |
my @LDIFHORDEOU; |
176 |
@LDIFHORDEOU[0] = "dn: $domain_dn,$horde_base_dn\n"; |
177 |
@LDIFHORDEOU[1] = "ou: $domain_dn\n"; |
178 |
@LDIFHORDEOU[2] = "objectclass: top\n"; |
179 |
@LDIFHORDEOU[3] = "objectclass: organizationalUnit\n"; |
180 |
open(DATEI, ">$ldif_horde_file") || die "Datei nicht gefunden"; |
181 |
print DATEI @LDIFHORDEOU; |
182 |
close(DATEI); |
183 |
qx (/usr/bin/ldapadd -D $root_dn -x -w $root_pass -f $ldif_horde_file) |
184 |
} |
185 |
|
186 |
if ($imap eq "yes") { |
187 |
my @LDIFIMAP; |
188 |
@LDIFIMAP[0] = "dn: cn=$givenname $surename,$domain_dn,$horde_base_dn\n"; |
189 |
@LDIFIMAP[1] = "objectclass: organizationalPerson\n"; |
190 |
@LDIFIMAP[2] = "objectclass: inetOrgPerson\n"; |
191 |
@LDIFIMAP[3] = "objectclass: person\n"; |
192 |
@LDIFIMAP[4] = "objectclass: hordePerson\n"; |
193 |
@LDIFIMAP[5] = "sn: $surename\n"; |
194 |
@LDIFIMAP[6] = "cn: $givenname $surename\n"; |
195 |
@LDIFIMAP[7] = "mail: $email\n"; |
196 |
@LDIFIMAP[8] = "gn: $givenname\n"; |
197 |
open(DATEI, ">$ldif_imap_file") || die "Datei nicht gefunden"; |
198 |
print DATEI @LDIFIMAP; |
199 |
close(DATEI); |
200 |
qx (/usr/bin/ldapadd -D $root_dn -x -w $root_pass -f $ldif_imap_file) |
201 |
} |
202 |
|
203 |
|
204 |
if (!-e $mailuserpath.$domain_dir) { |
205 |
#if ($imap eq "yes") { |
206 |
my @LDIFOU; |
207 |
@LDIFOU[0] = "dn: $domain_dn,$base_dn\n"; |
208 |
@LDIFOU[1] = "ou: $domain_dn\n"; |
209 |
@LDIFOU[2] = "objectclass: top\n"; |
210 |
@LDIFOU[3] = "objectclass: organizationalUnit\n"; |
211 |
open(DATEI, ">$ldif_dn_file") || die "Datei nicht gefunden"; |
212 |
print DATEI @LDIFOU; |
213 |
close(DATEI); |
214 |
qx (/usr/bin/ldapadd -D $root_dn -x -w $root_pass -f $ldif_dn_file) |
215 |
} |
216 |
|
217 |
open(DATEI, ">$ldiffile") || die "Datei nicht gefunden"; |
218 |
print DATEI @LDIFDATEI; |
219 |
close(DATEI); |
220 |
|
221 |
qx (/usr/bin/ldapadd -D $root_dn -x -w $root_pass -f $ldiffile) |
222 |
} |
223 |
|
224 |
sub HomeDirGet { |
225 |
my $user = shift; |
226 |
my $domain = shift; |
227 |
my $imap = shift; |
228 |
$domain =~ /(.*)\.(.*)/; |
229 |
my $homedir = $mailuserpath."/".$1."_".$2."/".$user; |
230 |
if ($imap ne "yes") { |
231 |
$homedir = "/dev/null"; |
232 |
} |
233 |
return ($homedir); |
234 |
} |
235 |
|
236 |
sub HomeDirMake { |
237 |
my $user = shift; |
238 |
my $domain = shift; |
239 |
my $imap = shift; |
240 |
my $uid = shift; |
241 |
my $gid = shift; |
242 |
my $group = shift; |
243 |
$domain =~ /(.*)\.(.*)/; |
244 |
my $select11 = $1; |
245 |
my $select12 = $2; |
246 |
my $pre_save_path = $mailuserpath; |
247 |
my $domaindir = "/".$select11."_".$select12; |
248 |
my $homedir = $mailuserpath.$domaindir."/".$user; |
249 |
if ($imap eq "yes") { |
250 |
my $homedir = $mailuserpath.$domaindir."/".$user; |
251 |
if (!-e $pre_save_path.$domaindir) { |
252 |
mkdir ($pre_save_path.$domaindir) |
253 |
} |
254 |
if (!-e $pre_save_path.$domaindir."/".$user) { |
255 |
mkdir ($pre_save_path.$domaindir."/".$user) |
256 |
} |
257 |
if (!-e $pre_save_path.$domaindir."/".$user.$mail_root) { |
258 |
mkdir ($pre_save_path.$domaindir."/".$user.$mail_root) |
259 |
} |
260 |
if (!-e $pre_save_path.$domaindir."/".$user.$user_www_root) { |
261 |
mkdir ($pre_save_path.$domaindir."/".$user.$user_www_root) |
262 |
} |
263 |
if(!-e $homedir) { |
264 |
$homedir = "/dev/null"; |
265 |
return ($homedir); |
266 |
} |
267 |
else { |
268 |
qx (/bin/chown -R $user:$group $pre_save_path$domaindir"/"$user) |
269 |
#qx (/bin/chmod -R 750 $pre_save_path.$domaindir.$group_www_root) |
270 |
} |
271 |
if (!-e $pre_save_path.$domaindir.$group_www_root) { |
272 |
qx (/bin/cp -R $pre_save_path$template $pre_save_path$domaindir); |
273 |
qx (/bin/chown -R root:$group $pre_save_path$domaindir$group_www_root); |
274 |
#qx (/bin/chmod -R 750 $pre_save_path.$domaindir.$group_www_root) |
275 |
} |
276 |
} |
277 |
else { |
278 |
$homedir = "/dev/null"; |
279 |
return ($homedir); |
280 |
}} |
281 |
|
282 |
sub Group { |
283 |
#to implemented |
284 |
my $gid = shift; |
285 |
my $domain = shift; |
286 |
$domain =~ /(.*)\.(.*)/; |
287 |
my $group_name = $1."_".$2; |
288 |
qx (/usr/sbin/groupadd -g $gid $group_name); |
289 |
return ($group_name); |
290 |
} |
291 |
|
292 |
sub GetUserDataHTML { |
293 |
my %UserDaten; |
294 |
my @Feldnamen = $cgi->param(); |
295 |
|
296 |
print $cgi->header(), |
297 |
$cgi->start_html('CGI-Feedback'), |
298 |
$cgi->h1('UMS-Feedback'); |
299 |
#,$cgi->i('comments.pl')); |
300 |
my @Feldinhalte; |
301 |
my $counter = 0; |
302 |
foreach my $Feld (@Feldnamen) { |
303 |
#print $cgi->b('Feldname: '), |
304 |
# $Feld, |
305 |
# $cgi->b(', Inhalt: '), |
306 |
# $cgi->param($Feld), "<br>"; |
307 |
@Feldinhalte[$counter]=$cgi->param($Feld); |
308 |
$counter++; |
309 |
} |
310 |
print $cgi->end_html(); |
311 |
|
312 |
my $vorname = @Feldinhalte[0]; |
313 |
my $nachname = @Feldinhalte[1]; |
314 |
my $username = @Feldinhalte[2]; |
315 |
my $password = @Feldinhalte[3]; |
316 |
my $domain = @Feldinhalte[4]; |
317 |
my $email = @Feldinhalte[5]; |
318 |
my $imap = @Feldinhalte[6]; |
319 |
$email = $email."\@".$domain; |
320 |
|
321 |
print $cgi->h1("$vorname"), |
322 |
$cgi->h1("$nachname"), |
323 |
$cgi->h1("$username"), |
324 |
$cgi->h1("$password"), |
325 |
$cgi->h1("$domain"), |
326 |
$cgi->h1("$email"), |
327 |
$cgi->h1("$imap"),"<br>"; |
328 |
|
329 |
my %UserDaten = (Vorname=>$vorname,Nachname=>$nachname,Domain=>$domain,Benutzername=>$username,Password=>$password,Imap=>$imap,EMail=>$email); |
330 |
return (%UserDaten); |
331 |
} |
332 |
|
333 |
sub SendmailConfig { |
334 |
my $user = shift; |
335 |
my $domain = shift; |
336 |
my $email = shift; |
337 |
|
338 |
open(DATEI, "<$virtualhost_file") || die "Datei nicht gefunden"; |
339 |
my @Zeilen = <DATEI>; |
340 |
close(DATEI); |
341 |
my @NeueZeilen; |
342 |
@NeueZeilen[0] = $email." ".$user."\n"; |
343 |
foreach(@NeueZeilen) { |
344 |
push(@Zeilen,$_); |
345 |
} |
346 |
open(DATEI, ">$virtualhost_file") || die "Datei nicht gefunden"; |
347 |
print DATEI @Zeilen; |
348 |
close(DATEI); |
349 |
|
350 |
$domain =~ /(.*)\.(.*)/; |
351 |
my $domain_dir = "/".$1."_".$2; |
352 |
if (!-e $mailuserpath.$domain_dir) { |
353 |
open(DATEI, "<$local_host_name_file") || die "Datei nicht gefunden"; |
354 |
my @Zeilen = <DATEI>; |
355 |
close(DATEI); |
356 |
my @NeueZeilen; |
357 |
@NeueZeilen[0] = $domain."\n"; |
358 |
foreach(@NeueZeilen) { |
359 |
push(@Zeilen,$_); |
360 |
} |
361 |
open(DATEI, ">$local_host_name_file") || die "Datei nicht gefunden"; |
362 |
print DATEI @Zeilen; |
363 |
close(DATEI); |
364 |
} |
365 |
|
366 |
open(DATEI, "<$genericstable_file") || die "Datei nicht gefunden"; |
367 |
my @Zeilen = <DATEI>; |
368 |
close(DATEI); |
369 |
my @NeueZeilen; |
370 |
@NeueZeilen[0] = $user." ".$email."\n"; |
371 |
foreach(@NeueZeilen) { |
372 |
push(@Zeilen,$_); |
373 |
} |
374 |
open(DATEI, ">$genericstable_file") || die "Datei nicht gefunden"; |
375 |
print DATEI @Zeilen; |
376 |
close(DATEI); |
377 |
sleep 2; |
378 |
chdir ($make_target); |
379 |
qx($make_bin); |
380 |
chdir ($current_dir); |
381 |
sleep 2; |
382 |
qx($remote_sendmail restart); |
383 |
sleep 2; |
384 |
} |
385 |
|
386 |
sub Main { |
387 |
my %UserData; |
388 |
if ($howtotell eq "TEST"){ |
389 |
%UserData = GetUserDataAuto(); |
390 |
} |
391 |
if ($howtotell eq "CMD"){ |
392 |
%UserData = GetUserDataCMD(); |
393 |
} |
394 |
if ($howtotell eq "HTML"){ |
395 |
%UserData = GetUserDataHTML(); |
396 |
} |
397 |
else { |
398 |
die; |
399 |
} |
400 |
my %UIDs = GetEnt_Passwd(); |
401 |
my $Group_Name = Group($UIDs{'GidNumber'},$UserData{'Domain'}); |
402 |
my $HomeDirGet = HomeDirGet($UserData{'Benutzername'},$UserData{'Domain'},$UserData{'Imap'}); |
403 |
CreateLDAPUser($UserData{'Benutzername'},$UserData{'Domain'},$UserData{'Password'},$UserData{'Nachname'},$UserData{'Vorname'},$UIDs{'UidNumber'},$UIDs{'GidNumber'},$HomeDirGet,$UserData{'Imap'},$UserData{'EMail'}); |
404 |
my $sendmail = SendmailConfig($UserData{'Benutzername'},$UserData{'Domain'},$UserData{'EMail'}); |
405 |
my $HomeDirMake = HomeDirMake($UserData{'Benutzername'},$UserData{'Domain'},$UserData{'Imap'},$UIDs{'UidNumber'},$UIDs{'GidNumber'},$Group_Name); |
406 |
} |