1
2
3
4
5
samACC = Login ID
name = Vorname Nachname
mail = Mail Adresse
expired = Ablaufdatum des Zuganges
manager = Vorgesetzter der eine Verlängerung beantragen muss
1
2
3
samACC name mail expired Manager
AB001 John JD Doh John.Doh@mail.com Nie
BA001 Max MM Muster max.muster@firma.de 30.09.2018 CN=John.Doh
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
#!/usr/bin/perl -w
#
#
# ------------------------------------------------------------------
# Loaded Modules
# ------------------------------------------------------------------
use strict;
use warnings;
use Net::LDAP;
use Net::LDAP;
# ------------------------------------------------------------------
# ldap parameters
# ------------------------------------------------------------------
my $ldap_srv = 'server';
my $ldap_usr = 'CN=bibi blox,OU=User,OU=xxx,OU=yy,DC=foo,DC=com';
my $ldap_pwd = 'ist geheim;
my $ldap_base_dir = 'dc=foo,dc=com';
# ------------------------------------------------------------------
# connect to ldap
# ------------------------------------------------------------------
my $ldap = Net::LDAP->new( $ldap_srv ) or die "$@";
my $mesg = $ldap->bind( $ldap_usr,
password => $ldap_pwd
);
# ------------------------------------------------------------------
# search LDAP database
# ------------------------------------------------------------------
$mesg = $ldap->search( base=>$ldap_base_dir,
filter => "(&(objectclass=*)(sn=*))");
#filter=>"(name=*)",
my @Acc_Data = '';
my @entries = $mesg->entries;
print "UserID\tUser Name\tBeschreibung\tGruppen\tLetzter Login\tLast_PWD_Set\tEmail\tPhone\tAccount expired\tLocal Manager\n";
foreach my $entry (@entries) {
my $samACC = $entry->get_value('samAccountName');
my $name = $entry->get_value('name');
my $descr = $entry->get_value('description');
my $memberof = join ", ", $entry->get_value('memberof');
my $lastLogon = readable_wintime($entry->get_value('lastLogonTimestamp'));
my $pwdLastSet = readable_wintime($entry->get_value('pwdLastSet'));
my $mail = $entry->get_value('mail');
my $phone = $entry->get_value('telephoneNumber');
my $expired = readable_date($entry->get_value('accountExpires'));
my $manager = $entry->get_value('manager');
print "$samACC\t$name\t$descr\t$memberof\t$lastLogon\t$pwdLastSet\t$mail\t$phone\t$expired\t$manager\n";
}
# ------------------------------------------------------------------
# disconnect from ldap
# ------------------------------------------------------------------
$mesg = $ldap->unbind; # take down session
# ------------------------------------------------------------------
# convert none readable date/time format to readable format
# ------------------------------------------------------------------
sub readable_wintime {
my $wintime = shift;
my $unix_epoch = win_to_unix_epoch($wintime);
my ($year, $month, $day, $hour, $minute, $second) = (localtime $unix_epoch)[5,4,3,2,1,0];
$year += 1900;
$month += 1;
($month, $day, $hour, $minute, $second) = map { sprintf '%02d', $_ } $month, $day, $hour, $minute, $second;
my $LastLogin = join('.', $day, $month, $year) . ' ' . join(':', $hour, $minute, $second);
}
sub readable_date {
my $wintime = shift;
my $unix_epoch = win_to_unix_epoch($wintime);
my ($year, $month, $day,) = (localtime $unix_epoch)[5,4,3,2,1,0];
$year += 1900;
$month += 1;
($month, $day) = map { sprintf '%02d', $_ } $month, $day;
my $LastLogin = join('.', $day, $month, $year);
}
sub win_to_unix_epoch {
# Actually hundreths of nanoseconds at this point...
my $nanoseconds = shift;
# Get seconds
my $seconds = $nanoseconds / 10_000_000;
# This magic number is the difference between Unix and Windows epoch.
my $unix_epoch = $seconds - 11644473600;
# Return the Unix epoch for use with localtime().
return $unix_epoch;
}
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
# ------------------------------------------------------------------
# Loaded Modules
# ------------------------------------------------------------------
use strict;
use warnings;
use Net::LDAP;
use Net::LDAP;
use Date::Manip;
use Email::Simple ();
use Email::Sender::Simple qw(sendmail);
use Email::Sender::Transport::SMTP ();
use Email::Simple::Creator ();
# ------------------------------------------------------------------
# smtp parameters
# ------------------------------------------------------------------
my $smtpserver = 'server';
my $smtpport = 25;
my $transport = Email::Sender::Transport::SMTP->new({
host => $smtpserver,
port => $smtpport,
});
my $sender_adr = "rumpel.stilzchen\@blablub.com";
# ------------------------------------------------------------------
# ldap parameters
# ------------------------------------------------------------------
my $ldap_srv = 'PDC';
my $ldap_usr = 'CN=bal.blub,OU=User,DC=foo,DC=com';
my $ldap_pwd = 'na so eins';
my $ldap_base_dir = 'dc=foo,dc=com';
# ------------------------------------------------------------------
# connect to ldap
# ------------------------------------------------------------------
my $ldap = Net::LDAP->new( $ldap_srv ) or die "$@";
my $mesg = $ldap->bind( $ldap_usr,
password => $ldap_pwd
);
# ------------------------------------------------------------------
# search LDAP database
# ------------------------------------------------------------------
$mesg = $ldap->search( base=>$ldap_base_dir,
filter => "(&(objectclass=*)(sn=*))");
#filter=>"(name=*)",
my @entries = $mesg->entries;
foreach my $entry (@entries) {
my $samACC = $entry->get_value('samAccountName');
my $name = $entry->get_value('name');
my $descr = $entry->get_value('description');
my $mail = $entry->get_value('mail');
my $expired = readable_date($entry->get_value('accountExpires'));
my $manager = $entry->get_value('manager');
&check_data($samACC, $name, $mail, $expired, $manager, $descr);
}
# ------------------------------------------------------------------
# Process data
# ------------------------------------------------------------------
sub check_data{
my $date = ParseDate("today");
my $newdate = DateCalc(ParseDate($_[3]), ParseDateDelta('- 2 days'));
if ($newdate eq $date) {
foreach my $contact ($_[4]){
my $email = Email::Simple->create(
header => [
To => $_[2],
From => $sender_adr,
Subject => 'Konto Abgelaufen - Bitte Verlängern',
],
body => "Sehr geehrte Damen und Herren\n Der Zugang fue Account $_[1] $_[5] laeuft am $_[3] ab\n",
);
sendmail($email, { transport => $transport });
}
}
else {
print "Alles OK";
}
}
# ------------------------------------------------------------------
# disconnect from ldap
# ------------------------------------------------------------------
$mesg = $ldap->unbind; # take down session
# ------------------------------------------------------------------
# convert none readable date/time format to readable format
# ------------------------------------------------------------------
sub readable_date {
my $wintime = shift;
my $unix_epoch = win_to_unix_epoch($wintime);
my ($year, $month, $day,) = (localtime $unix_epoch)[5,4,3,2,1,0];
$year += 1900;
$month += 1;
($month, $day) = map { sprintf '%02d', $_ } $month, $day;
my $LastLogin = join('-', $year, $month, $day);
}
sub win_to_unix_epoch {
# Actually hundreths of nanoseconds at this point...
my $nanoseconds = shift;
# Get seconds
my $seconds = $nanoseconds / 10_000_000;
# This magic number is the difference between Unix and Windows epoch.
my $unix_epoch = $seconds - 11644473600;
# Return the Unix epoch for use with localtime().
return $unix_epoch;
2018-08-09T11:30:14 MuffiWo ist genau das Problem?
Oder warum tut das nicht ein grep über die @entries?
1 2 3 4 5 6 7
sub get_user_by_mail { my ($entries, $mail) = @_; my @user = grep { $_->get_value('mail') eq $mail } @$entries; return $user[0]; } my $Manager = get_user_by_mail(\@entries, $Manager_mail);
2018-08-09T12:20:38 MuffiSo in etwa?
Aber so wirklich versteh ich glaub ich das Problem och nicht.
1
2
3
samACC name mail expired Manager
AB001 John JD Doh John.Doh@mail.com Nie
BA001 Max MM Muster max.muster@firma.de 30.09.2018 CN=John.Doh
1 2 3 4 5 6 7 8 9
# VORSICHT: Diese Code ist nicht getestet # ... nach dem search: my %email_by_cn; foreach my $entry (@entries) { my $cn = $entry->get_value('cn'); my $mail = $entry->get_value('mail'); $cn=lc($cn); # in kleinbuchstaben wandeln - falls sich das mal unterscheidet $email_by_cn{$cn}=$mail; }
1 2 3 4 5 6 7
# VORSICHT: Diese Code ist nicht getestet # ... $manager=lc($entry->get_value('manager')); $manager =~ s/^CN=//i; # "CN=" am Anfang entfernen $manager=lc($manager); # ebenfalls in kleinbuchstaben wandeln my $email_adresse_des_managers=$email_by_cn{$manager};
1 2 3 4 5 6 7 8 9 10 11
# VORSICHT: Dieser Code ist nicht getestet # ... nach dem search: # # E-Mail-Adressen aller Benutzer im Hash %email_by_dn speichern: # my %email_by_dn; foreach my $entry (@entries) { my $dn = $entry->dn(); # Hinweis: der DN wird nicht mit get_value ermittelt my $mail = $entry->get_value('mail'); $email_by_dn{$dn}=$mail; }
1 2 3 4
# VORSICHT: Dieser Code ist nicht getestet # ... $manager=$entry->get_value('manager'); my $email_adresse_des_managers=$email_by_dn{$manager};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# ------------------------------------------------------------------
# search LDAP database
# ------------------------------------------------------------------
$mesg = $ldap->search( base=>$ldap_base_dir,
filter => "(&(objectclass=*)(sn=*))");
#filter=>"(name=*)",
my @entries = $mesg->entries;
my %email_by_dn;
foreach my $entry (@entries) {
my $samACC = $entry->get_value('samAccountName');
my $name = $entry->get_value('name');
my $descr = $entry->get_value('description');
my $mail = $entry->get_value('mail');
my $expired = readable_date($entry->get_value('accountExpires'));
my $dn = $entry->dn();
my $manager = $entry->get_value('manager');
$email_by_dn{$dn}=$mail;
my $mgmt_mail = $email_by_dn{$manager};
print "rh email = $mgmt_mail\n";
$mgmt_mail);
}
1
2
3
4
5
6
7
$VAR1 = 'CN=Mar MM. Mar,OU=User,OU=DE,foo,DC=com R.H@gmx.de
$VAR1 = 'CN=Mic MG. Gros,OU=User,OU=DE,DC=foo,DC=com R.H@gmx.de
$VAR1 = 'CN=Mix MR. Red,OU=User,OU=DE,DC=foo,DC=com R.H@gmx.de
$VAR1 = 'CN=U0test3 TT. Test User,OU=User,OU=DE,DC=foo,DC=com R.H@gmx.de
$VAR1 = 'CN=Jue JH. Har,OU=User,OU=DE,DC=foo,DC=com
$VAR1 = 'CN=Sve SE. Eve,OU=User,OU=DE,DC=foo,DC=com
$VAR1 = 'CN=End EE. Eki,OU=User,OU=DE,DC=foo,DC=com
print Dumper "$dn\t$mgmt_mail\n";
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
# ------------------------------------------------------------------ # search LDAP database # ------------------------------------------------------------------ $mesg = $ldap->search( base=>$ldap_base_dir, filter => '(userAccountControl:1.2.840.113556.1.4.803:=512)'; # nur Benutzer #Liefert _alle_Einträge des AD: filter => "(&(objectclass=*)(sn=*))"); #filter=>"(name=*)", # # Hier fehlt noch die Fehlerbehandlung, falls $ldap->search fehlschlägt # # # Vermutlich fehlt noch das "bind" ... # my @entries = $mesg->entries; # 1. Schritt: Die E-Mail-Adressen _aller_ Benutzer ermitteln # Die E-Mail-Adressen werden im Hash '%email_by_dn' gespeichert: # z.B.: $email_by_dn{"DN=cn=Karl..."}='Karl.Koffer@example.com' my %email_by_dn; foreach my $entry (@entries) { my $dn = $entry->dn(); # Hinweis: der DN wird nicht mit get_value ermittelt my $mail = $entry->get_value('mail'); $email_by_dn{$dn}=$mail; } # 2. Schritt: Alle Benutzer prüfen # und dabei die E-Mail des Managers ermitteln foreach my $entry (@entries) { my $samACC = $entry->get_value('samAccountName'); my $name = $entry->get_value('name'); my $descr = $entry->get_value('description'); my $mail = $entry->get_value('mail'); my $expired = readable_date($entry->get_value('accountExpires')); my $dn = $entry->dn(); my $manager = $entry->get_value('manager'); my $mgmt_mail = $email_by_dn{$manager}; print "USER='$samACC' Manager-mail='$mgmt_mail'\n"; }
2018-08-14T19:48:40 thomas38Hallo Rambo,
die E-Mail-Adressen _aller_ Benutzer müssen zuerst komplett ermittelt werden.
Deshalb der Hash '%email_by_dn', der in der ersten foreach-Schleife komplett befüllt wird.
In der 2. foreach-Schleife werden daraus nur die Werte gelesen.
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
# ------------------------------------------------------------------
# Loaded Modules
# ------------------------------------------------------------------
use strict;
use warnings;
#no warnings 'uninitialized';
use Net::LDAP;
use Net::LDAP;
use Date::Manip;
use Email::Simple ();
use Email::Sender::Simple qw(sendmail);
use Email::Sender::Transport::SMTP ();
use Email::Simple::Creator ();
# ------------------------------------------------------------------
# smtp parameters
# ------------------------------------------------------------------
my $smtpserver = 'smtp-srv';
my $smtpport = 25;
my $transport = Email::Sender::Transport::SMTP->new({
host => $smtpserver,
port => $smtpport,
});
# ------------------------------------------------------------------
# Email send from Adress
# ------------------------------------------------------------------
my $sender_adr = "bodo.bachlein\@foobar.com";
# ------------------------------------------------------------------
# ldap parameters
# ------------------------------------------------------------------
my $ldap_srv = 'server';
my $ldap_usr = 'CN=manager,OU=User,OU=DE,DC=foo,DC=com';
my $ldap_pwd = 'ist geheim';
my $ldap_base_dir = 'dc=foo,dc=com';
# ------------------------------------------------------------------
# connect to ldap
# ------------------------------------------------------------------
my $ldap = Net::LDAP->new( $ldap_srv ) or die "$@";
my $mesg = $ldap->bind( $ldap_usr,
password => $ldap_pwd
); die $mesg->error if $mesg->code;
# ------------------------------------------------------------------
# search LDAP database
# ------------------------------------------------------------------
$mesg = $ldap->search( base=>$ldap_base_dir,
filter => '(userAccountControl:1.2.840.113556.1.4.803:=512)'); # nur Benutzer
$mesg->code && die $mesg->error;
my @entries = $mesg->entries;
my %email_by_dn;
foreach my $entry (@entries) {
my $dn = $entry->dn(); # Hinweis: der DN wird nicht mit get_value ermittelt
my $mail = $entry->get_value('mail');
$email_by_dn{$dn}=$mail;
}
foreach my $entry (@entries) {
my $samACC = $entry->get_value('samAccountName');
my $name = $entry->get_value('name');
my $descr = $entry->get_value('description');
my $mail = $entry->get_value('mail');
my $expired = readable_date($entry->get_value('accountExpires'));
my $dn = $entry->dn();
my $manager = $entry->get_value('manager');
my $mgmt_mail = $email_by_dn{$manager};
&check_data($samACC, $name, $mail, $expired, $manager, $descr, $mgmt_mail);
}
# ------------------------------------------------------------------
# Process data and send Email
# ------------------------------------------------------------------
sub check_data{
my $date = ParseDate("today");
my $newdate = DateCalc(ParseDate($_[3]), ParseDateDelta('- 2 days'));
if ($newdate eq $date) {
foreach my $contact ($_[4]){
my $email = Email::Simple->create(
header => [
To => $_[6],
From => $sender_adr,
Subject => 'Benutzer Konto läuft ab - Bitte Verlängern',
],
body => "
Sehr geehrte Damen und Herren,
das folgende Benutzerkonto läuft zum $_[3] ab:
User ID: $_[0]
Vorname / Name: $_[1]
Description: $_[5]
Sofern dieser Zugang weiterhin benötigt wird, ist diese bitte mittels Auftrag zu verlängern.
DB Richtlinie zur Verlängerung von Benutzer Konten:
Benutzer Konten für externe Mitarbeiter und Fremdfirmen haben eine Gültigkeit von maximal 6 Monaten.
Wird der Account nicht Verlängert, ist der Zugang mit Ablauf der Gültigkeit zu TRZ Systemen gesperrt.
Bitte antworten Sie nicht auf diese Nachricht, da sie automatisch von einem Postfach generiert wurde.
Mit freundlichen Grüßen
Abteilung xy
Straße 99
12345 dawo
sendmail($email, { transport => $transport });
}
}
}
# ------------------------------------------------------------------
# disconnect from ldap
# ------------------------------------------------------------------
$mesg = $ldap->unbind; # take down session
# ------------------------------------------------------------------
# convert none readable date/time format to readable format
# ------------------------------------------------------------------
sub readable_date {
my $wintime = shift;
my $unix_epoch = win_to_unix_epoch($wintime);
my ($year, $month, $day,) = (localtime $unix_epoch)[5,4,3,2,1,0];
$year += 1900;
$month += 1;
($month, $day) = map { sprintf '%02d', $_ } $month, $day;
my $LastLogin = join('-', $year, $month, $day);
}
sub win_to_unix_epoch {
# Actually hundreths of nanoseconds at this point...
my $nanoseconds = shift;
# Get seconds
my $seconds = $nanoseconds / 10_000_000;
# This magic number is the difference between Unix and Windows epoch.
my $unix_epoch = $seconds - 11644473600;
# Return the Unix epoch for use with localtime().
return $unix_epoch;
}
2018-08-15T13:07:46 Ramboich verstehe nicht wirklich warum man 2 Durchläufe benötgit um die Email Adr. heraus zu bekommen. Er hat ja in meinen Versuchen ca. 50% der Infos ausgespuckt :-)