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 |
use CGI::Carp qw(fatalsToBrowser); |
18 |
} |
19 |
|
20 |
## ACHTUNG ### auf ALLE folgenden Parameter achten |
21 |
|
22 |
my $current_dir = "/usr/libexec/webmin/ums"; |
23 |
##Wenn jemand eine Funktion kennt das aktuelle Perl-Arbeitsverzeichnis zu ermitteln, bitte! |
24 |
my $ldap_start_uid = "999"; |
25 |
my $ldap_stop_uid = "20000"; |
26 |
# Letzters gilt auch fuer Gruppen |
27 |
my $mailuserpath = "/mailuser"; |
28 |
#Where to put your DomainDirs |
29 |
my $template = "/template/www"; |
30 |
#your Horde Template |
31 |
my $group_www_root = "/www"; |
32 |
my $mail_root = "/mail"; |
33 |
#your Users IMAP dir (under $HOME) |
34 |
my $user_www_root = "/public_html"; |
35 |
#your Users WWW_ROOT (under $HOME) |
36 |
my $base_dn = "ou=MailUser,ou=UMS,dc=europatentdienst,dc=de"; |
37 |
#LDAP Base DN for PosixAccounts |
38 |
my $horde_base_dn = "ou=Horde,ou=UMS,dc=europatentdienst,dc=de"; |
39 |
#LDAP Base DN for Horde |
40 |
my $root_dn = "'cn=Manager,dc=europatentdienst,dc=de'"; |
41 |
my $root_pass = "MEAS876"; |
42 |
# You should know what this means |
43 |
my $ldiffile = "/root/devel/perl/test.ldif"; |
44 |
my $ldif_dn_file = "/root/devel/perl/test_dn.ldif"; |
45 |
my $ldif_imap_file = "/root/devel/perl/test_imap.ldif"; |
46 |
my $ldif_horde_file = "/root/devel/perl/test_horde.ldif"; |
47 |
# These files are needed to generate LDIFs-->LDAPADD->LDAPServer |
48 |
my $virtualhost_file = "/etc/mail_test/virtusertable"; |
49 |
my $local_host_name_file = "/etc/mail_test/local-host-names"; |
50 |
my $genericstable_file = "/etc/mail_test/genericstable"; |
51 |
# Your Sendmail Files |
52 |
my $make_bin = "/usr/bin/make"; |
53 |
my $make_target = "/etc/mail_test"; |
54 |
my $remote_sendmail = "/usr/sbin/rcsendmail"; |
55 |
my $sendmail_command = "restart"; |
56 |
# I'am tiered now, sure you know what this means |
57 |
|
58 |
Main(); |
59 |
|
60 |
sub GetEnt_Passwd { |
61 |
my @biggest_uid; |
62 |
my @biggest_gid; |
63 |
my @existing_users; |
64 |
my @getent_passwd; |
65 |
my $foreach_counter; |
66 |
@getent_passwd = qx(/usr/bin/getent passwd); |
67 |
foreach (@getent_passwd) { |
68 |
@getent_passwd[$foreach_counter] =~ /(.*):x:(\d*):(\d*).*/; |
69 |
if ($2 > $ldap_start_uid && $2 < $ldap_stop_uid) { |
70 |
@existing_users[$foreach_counter] = $1; |
71 |
@biggest_uid[$foreach_counter] = $2; |
72 |
@biggest_gid[$foreach_counter] = $3; |
73 |
} |
74 |
$foreach_counter++; |
75 |
} |
76 |
|
77 |
@biggest_uid = sort(NummernSort @biggest_uid); |
78 |
@biggest_gid = sort(NummernSort @biggest_gid); |
79 |
@biggest_uid = reverse(@biggest_uid); |
80 |
@biggest_gid = reverse(@biggest_gid); |
81 |
my $HighestUID = @biggest_uid[0]; |
82 |
my $HighestGID = @biggest_gid[0]; |
83 |
my $UidNumber = $HighestUID + 1; |
84 |
my $GidNumber = $HighestGID + 1; |
85 |
my %HighestIDs = (HighestUID => $HighestUID,HighestGID => $HighestGID,UidNumber => $UidNumber,GidNumber => $GidNumber); |
86 |
return (%HighestIDs); |
87 |
} |
88 |
|
89 |
sub NummernSort { |
90 |
if ($a < $b) |
91 |
{return -1;} |
92 |
elsif($a == $b) |
93 |
{return 0; } |
94 |
else |
95 |
{return 1; } |
96 |
} |
97 |
|
98 |
sub GetUserDataCMD { |
99 |
my %UserDaten; |
100 |
print "Vorname: "; |
101 |
my $vorname=<STDIN>; |
102 |
chomp $vorname; |
103 |
print "Nachname: "; |
104 |
my $nachname=<STDIN>; |
105 |
chomp $nachname; |
106 |
print "Domain(info.de): "; |
107 |
my $domain=<STDIN>; |
108 |
chomp $domain; |
109 |
print "Benutzername: "; |
110 |
my $username=<STDIN>; |
111 |
chomp $username; |
112 |
print "Password: "; |
113 |
my $password=<STDIN>; |
114 |
chomp $password; |
115 |
my $imap = ""; |
116 |
until ($imap eq "yes" | $imap eq "no") { |
117 |
print "Imap/HomeDir/ModDav/User_WWW(yes/no):"; |
118 |
$imap=<STDIN>; |
119 |
chomp $imap; |
120 |
} |
121 |
print "Email(Name vor dem @):"; |
122 |
my $email =<STDIN>; |
123 |
chomp $email; |
124 |
$email = $email."\@".$domain; |
125 |
my %UserDaten = (Vorname=>$vorname,Nachname=>$nachname,Domain=>$domain,Benutzername=>$username,Password=>$password,Imap=>$imap,EMail=>$email); |
126 |
return (%UserDaten); |
127 |
} |
128 |
|
129 |
sub GetUserDataAuto { |
130 |
my $vorname="Test"; |
131 |
my $nachname="Tester"; |
132 |
my $domain="testing.de"; |
133 |
my $username="testtester"; |
134 |
my $password="test(/&"; |
135 |
my $imap="yes"; |
136 |
my $email= "testtester"."\@".$domain; |
137 |
my %UserDaten = (Vorname=>$vorname,Nachname=>$nachname,Domain=>$domain,Benutzername=>$username,Password=>$password,Imap=>$imap,EMail=>$email); |
138 |
return (%UserDaten); |
139 |
} |
140 |
|
141 |
sub CreateLDAPUser { |
142 |
my $cn = shift; |
143 |
my $user = $cn; |
144 |
my $domain = shift; |
145 |
my $pass = shift; |
146 |
my $surename = shift; |
147 |
my $givenname = shift; |
148 |
my $uidNumber = shift; |
149 |
my $gidNumber = shift; |
150 |
my $homeDir = shift; |
151 |
my $imap = shift; |
152 |
my $email = shift; |
153 |
|
154 |
$domain =~ /(.*)\.(.*)/; |
155 |
my $domain_dn = "ou=".$1."_".$2; |
156 |
my $domain_dir = "/".$1."_".$2; |
157 |
|
158 |
my @LDIFDATEI; |
159 |
@LDIFDATEI[0] = "dn: uid=$givenname $surename,$domain_dn,$base_dn\n"; |
160 |
@LDIFDATEI[1] = "objectclass: top\n"; |
161 |
@LDIFDATEI[2] = "objectclass: posixAccount\n"; |
162 |
@LDIFDATEI[3] = "objectclass: shadowAccount\n"; |
163 |
@LDIFDATEI[4] = "objectclass: account\n"; |
164 |
@LDIFDATEI[5] = "cn: $cn\n"; |
165 |
@LDIFDATEI[6] = "uid: $user\n"; |
166 |
@LDIFDATEI[7] = "gecos: $givenname $surename\n"; |
167 |
@LDIFDATEI[8] = "uidNumber: $uidNumber\n"; |
168 |
@LDIFDATEI[9] = "gidNumber: $gidNumber\n"; |
169 |
@LDIFDATEI[10] = "homeDirectory: $homeDir\n"; |
170 |
@LDIFDATEI[11] = "userPassword: $pass\n"; |
171 |
@LDIFDATEI[12] = "loginShell: /bin/false\n"; |
172 |
|
173 |
if ($imap eq "yes") { |
174 |
my @LDIFHORDEOU; |
175 |
@LDIFHORDEOU[0] = "dn: $domain_dn,$horde_base_dn\n"; |
176 |
@LDIFHORDEOU[1] = "ou: $domain_dn\n"; |
177 |
@LDIFHORDEOU[2] = "objectclass: top\n"; |
178 |
@LDIFHORDEOU[3] = "objectclass: organizationalUnit\n"; |
179 |
open(DATEI, ">$ldif_horde_file") || die "Datei nicht gefunden"; |
180 |
print DATEI @LDIFHORDEOU; |
181 |
close(DATEI); |
182 |
qx (/usr/bin/ldapadd -D $root_dn -x -w $root_pass -f $ldif_horde_file) |
183 |
} |
184 |
|
185 |
if ($imap eq "yes") { |
186 |
my @LDIFIMAP; |
187 |
@LDIFIMAP[0] = "dn: cn=$givenname $surename,$domain_dn,$horde_base_dn\n"; |
188 |
@LDIFIMAP[1] = "objectclass: organizationalPerson\n"; |
189 |
@LDIFIMAP[2] = "objectclass: inetOrgPerson\n"; |
190 |
@LDIFIMAP[3] = "objectclass: person\n"; |
191 |
@LDIFIMAP[4] = "objectclass: hordePerson\n"; |
192 |
@LDIFIMAP[5] = "sn: $surename\n"; |
193 |
@LDIFIMAP[6] = "cn: $givenname $surename\n"; |
194 |
@LDIFIMAP[7] = "mail: $email\n"; |
195 |
@LDIFIMAP[8] = "gn: $givenname\n"; |
196 |
open(DATEI, ">$ldif_imap_file") || die "Datei nicht gefunden"; |
197 |
print DATEI @LDIFIMAP; |
198 |
close(DATEI); |
199 |
qx (/usr/bin/ldapadd -D $root_dn -x -w $root_pass -f $ldif_imap_file) |
200 |
} |
201 |
|
202 |
|
203 |
if (!-e $mailuserpath.$domain_dir) { |
204 |
#if ($imap eq "yes") { |
205 |
my @LDIFOU; |
206 |
@LDIFOU[0] = "dn: $domain_dn,$base_dn\n"; |
207 |
@LDIFOU[1] = "ou: $domain_dn\n"; |
208 |
@LDIFOU[2] = "objectclass: top\n"; |
209 |
@LDIFOU[3] = "objectclass: organizationalUnit\n"; |
210 |
open(DATEI, ">$ldif_dn_file") || die "Datei nicht gefunden"; |
211 |
print DATEI @LDIFOU; |
212 |
close(DATEI); |
213 |
qx (/usr/bin/ldapadd -D $root_dn -x -w $root_pass -f $ldif_dn_file) |
214 |
} |
215 |
|
216 |
open(DATEI, ">$ldiffile") || die "Datei nicht gefunden"; |
217 |
print DATEI @LDIFDATEI; |
218 |
close(DATEI); |
219 |
|
220 |
qx (/usr/bin/ldapadd -D $root_dn -x -w $root_pass -f $ldiffile) |
221 |
} |
222 |
|
223 |
sub HomeDirGet { |
224 |
my $user = shift; |
225 |
my $domain = shift; |
226 |
my $imap = shift; |
227 |
$domain =~ /(.*)\.(.*)/; |
228 |
my $homedir = $mailuserpath."/".$1."_".$2."/".$user; |
229 |
if ($imap ne "yes") { |
230 |
$homedir = "/dev/null"; |
231 |
} |
232 |
return ($homedir); |
233 |
} |
234 |
|
235 |
sub HomeDirMake { |
236 |
my $user = shift; |
237 |
my $domain = shift; |
238 |
my $imap = shift; |
239 |
my $uid = shift; |
240 |
my $gid = shift; |
241 |
my $group = shift; |
242 |
$domain =~ /(.*)\.(.*)/; |
243 |
my $select11 = $1; |
244 |
my $select12 = $2; |
245 |
my $pre_save_path = $mailuserpath; |
246 |
my $domaindir = "/".$select11."_".$select12; |
247 |
my $homedir = $mailuserpath.$domaindir."/".$user; |
248 |
if ($imap eq "yes") { |
249 |
my $homedir = $mailuserpath.$domaindir."/".$user; |
250 |
if (!-e $pre_save_path.$domaindir) { |
251 |
mkdir ($pre_save_path.$domaindir) |
252 |
} |
253 |
if (!-e $pre_save_path.$domaindir."/".$user) { |
254 |
mkdir ($pre_save_path.$domaindir."/".$user) |
255 |
} |
256 |
if (!-e $pre_save_path.$domaindir."/".$user.$mail_root) { |
257 |
mkdir ($pre_save_path.$domaindir."/".$user.$mail_root) |
258 |
} |
259 |
if (!-e $pre_save_path.$domaindir."/".$user.$user_www_root) { |
260 |
mkdir ($pre_save_path.$domaindir."/".$user.$user_www_root) |
261 |
} |
262 |
if(!-e $homedir) { |
263 |
$homedir = "/dev/null"; |
264 |
return ($homedir); |
265 |
} |
266 |
else { |
267 |
qx (/bin/chown -R $user:$group $pre_save_path$domaindir"/"$user) |
268 |
#qx (/bin/chmod -R 750 $pre_save_path.$domaindir.$group_www_root) |
269 |
} |
270 |
if (!-e $pre_save_path.$domaindir.$group_www_root) { |
271 |
qx (/bin/cp -R $pre_save_path$template $pre_save_path$domaindir); |
272 |
qx (/bin/chown -R root:$group $pre_save_path$domaindir$group_www_root); |
273 |
#qx (/bin/chmod -R 750 $pre_save_path.$domaindir.$group_www_root) |
274 |
} |
275 |
} |
276 |
else { |
277 |
$homedir = "/dev/null"; |
278 |
return ($homedir); |
279 |
}} |
280 |
|
281 |
sub Group { |
282 |
#to implemented |
283 |
my $gid = shift; |
284 |
my $domain = shift; |
285 |
$domain =~ /(.*)\.(.*)/; |
286 |
my $group_name = $1."_".$2; |
287 |
qx (/usr/sbin/groupadd -g $gid $group_name); |
288 |
return ($group_name); |
289 |
} |
290 |
|
291 |
sub GetUserDataHTML { |
292 |
my %UserDaten; |
293 |
my $cgi = new CGI; |
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 |
} |