aboutsummaryrefslogtreecommitdiffstats
path: root/mkpeople
blob: cd2123976b98031eb56fab4d2490bd165900c67b (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
#!/usr/bin/perl -w

use strict;
use Template;
use Net::LDAP;
use File::Slurp;
use YAML qw/LoadFile/;

my $config_file = '/etc/mgapeople.conf';
my $config = LoadFile($ENV{MGAPEOPLE_CONF} ?  $ENV{MGAPEOPLE_CONF} : $config_file);

my %groups;
my %users;

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 ($extension) = @_;
    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,
        };
        my $uid = $users{$user}->{uid}->[0];
        $template->process("user.$extension", $vars, "$uid.$extension");
    }

    my $vars = {
        config => $config,
        users  => \%users,
        groups => \%groups,
    };
    $template->process("userindex.$extension", $vars, "index.$extension");
}

sub output_groups {
    my ($extension) = @_;
    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,
        };
        my $cn = $groups{$group}->{cn}->[0];
        $template->process("group.$extension", $vars, "$cn.$extension");
    }

    my $vars = {
        config => $config,
        users  => \%users,
        groups => \%groups,
    };
    $template->process("groupindex.$extension", $vars, "index.$extension");
}

sub output_index {
    my ($extension) = @_;
    my $template = Template->new({
        INCLUDE_PATH => $config->{tmpl_dir},
        OUTPUT_PATH => "$config->{output_dir}",
    });
    my $vars = {
        config => $config,
        users  => \%users,
        groups => \%groups,
    };
    $template->process("index.$extension", $vars, "index.$extension");
}

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;
loadgroups($ldap);
output_users('html');
output_groups('html');
output_index('html');