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
|
#!/usr/bin/perl -w
use strict;
use Template;
use Net::LDAP;
use File::Slurp;
use YAML qw/LoadFile/;
use LWP::UserAgent;
my $config_file = '/etc/mgapeople.conf';
my $config = LoadFile($ENV{MGAPEOPLE_CONF} ? $ENV{MGAPEOPLE_CONF} : $config_file);
my %maintdb;
my %groups;
my %users;
sub process_template {
my ($template, $src, $vars, $dest) = @_;
for my $extension (@{$config->{output_format}}) {
next unless -f "$config->{tmpl_dir}/$src.$extension";
$template->process("$src.$extension", $vars, "$dest.$extension");
}
}
sub loadmaintdb {
return unless $config->{maintdburl};
my $ua = LWP::UserAgent->new;
my $r = $ua->get($config->{maintdburl});
return unless $r->is_success;
for my $line (split /^/, $r->decoded_content) {
my ($pkg, $user) = split ' ', $line;
push @{$maintdb{$user}}, $pkg;
}
}
sub loaduser {
my ($ldap, $user) = @_;
my $m = $ldap->search(
base => $user,
scope => 'base',
filter => '(objectClass=inetOrgPerson)',
);
die $m->error if $m->is_error;
my $res = $m->as_struct;
if (exists $res->{$user}) {
$res->{$user}->{groups} = [];
$users{$user} = $res->{$user};
}
}
sub loadgroups {
my ($ldap) = @_;
my $m = $ldap->search(
base => $config->{groupbase},
filter => '(objectClass=groupOfNames)',
);
die $m->error if $m->is_error;
my $res = $m->as_struct;
for my $groupname (keys %$res) {
$groups{$groupname} = $res->{$groupname};
for my $user (@{$groups{$groupname}->{member}}) {
loaduser($ldap, $user) unless exists $users{$user};
if (exists $users{$user}) {
push @{$users{$user}->{groups}}, $groupname;
}
}
$res->{$groupname}->{member} =
[ grep { exists $users{$_} } @{$groups{$groupname}->{member}} ];
}
}
sub output_users {
my $template = Template->new({
INCLUDE_PATH => $config->{tmpl_dir},
OUTPUT_PATH => "$config->{output_dir}/u",
});
for my $user (keys %users) {
my $vars = {
config => $config,
user => $user,
users => \%users,
groups => \%groups,
maintdb => \%maintdb,
};
my $uid = $users{$user}->{uid}->[0];
process_template($template, 'user', $vars, $uid);
}
my $vars = {
config => $config,
users => \%users,
groups => \%groups,
maintdb => \%maintdb,
};
process_template($template, 'userindex', $vars, 'index');
}
sub output_groups {
my $template = Template->new({
INCLUDE_PATH => $config->{tmpl_dir},
OUTPUT_PATH => "$config->{output_dir}/g",
});
for my $group (keys %groups) {
my $vars = {
config => $config,
group => $group,
users => \%users,
groups => \%groups,
maintdb => \%maintdb,
};
my $cn = $groups{$group}->{cn}->[0];
process_template($template, 'group', $vars, $cn);
}
my $vars = {
config => $config,
users => \%users,
groups => \%groups,
maintdb => \%maintdb,
};
process_template($template, 'groupindex', $vars, 'index');
}
sub output_index {
my $template = Template->new({
INCLUDE_PATH => $config->{tmpl_dir},
OUTPUT_PATH => "$config->{output_dir}",
});
my $vars = {
config => $config,
users => \%users,
groups => \%groups,
maintdb => \%maintdb,
};
process_template($template, 'index', $vars, 'index');
}
my $bindpw = read_file($config->{bindpwfile});
chomp $bindpw;
my $ldap = Net::LDAP->new($config->{ldapserver}) or die "$@";
my $m;
$m = $ldap->start_tls(verify => 'none');
die $m->error if $m->is_error;
$m = $ldap->bind($config->{binddn}, password => $bindpw);
die $m->error if $m->is_error;
loadmaintdb;
loadgroups($ldap);
output_users();
output_groups();
output_index();
|