summaryrefslogtreecommitdiffstats
path: root/urpm/ldap.pm
blob: 83e6e2eeeffdf0c104a96be9da0d6a0908acc679 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package urpm::ldap;


use strict;
use warnings;
use urpm;
use urpm::util qw(cat_ output_safe);
use urpm::msg 'N';
use urpm::media;

our $LDAP_CONFIG_FILE = '/etc/ldap.conf';
my @per_media_opt = (@urpm::media::PER_MEDIA_OPT, qw(md5sum ftp-proxy http-proxy));

# TODO
# use srv dns record ?
# complete the doc

=head1 NAME

urpm::ldap - routines to handle configuration with ldap

=head1 SYNOPSIS

=head1 DESCRIPTION

=over

=item write_ldap_cache($urpm,$medium)

Writes the value fetched from ldap, in case of server failure. This should not
be used to reduce the load of the ldap server, as fetching is still needed, and
therefore, caching is useless if server is up.

=cut

sub write_ldap_cache($$) {
    my ($urpm, $medium) = @_;
    my $ldap_cache = "$urpm->{cachedir}/ldap";
    # FIXME what perm for cache ?
    -d $ldap_cache or mkdir $ldap_cache
	or die N("Cannot create ldap cache directory");
    output_safe("$ldap_cache/$medium->{name}",
		join("\n",
		     "# internal cache file for disconnect ldap operation, do not edit",
		     map { "$_ = $medium->{$_}" } grep { $medium->{$_} } keys %$medium
		)
	) or die N("Cannot write cache file for ldap\n");
    return 1;
}


=item check_ldap_medium($medium)

Checks if the ldap medium has all required attributes.

=cut

sub check_ldap_medium($) {
    my ($medium) = @_;
    return $medium->{name} && $medium->{url};
}

sub get_vars_from_sh {
    my ($filename) = @_;
    my %l;
    foreach (cat_($filename)) {
	s/#.*//; s/^\s*//; s/\s*$//;
	my ($key, $val) = /^(\w+)=(.*)/ or next;
	$val =~ s/^(["'])(.*)\1$/$2/;
	$l{$key} = $val;
    }
    %l;
}

=item read_ldap_cache($urpm)

Reads the cache created by the C<write_ldap_cache> function. Should be called
if the ldap server doesn't answer (upgrade, network problem, mobile user, etc.)

=cut

sub read_ldap_cache {
    my ($urpm) = @_;
    foreach (glob("$urpm->{cachedir}/ldap/*")) {
	! -f $_ and next;
	my %medium = get_vars_from_sh($_);
	next if !check_ldap_medium(\%medium);
	urpm::media::add_existing_medium($urpm, \%medium, 'nocheck');
    }
}

=item clean_ldap_cache($urpm)

Cleans the ldap cache, removes all files in the directory.

=cut

#- clean the cache, before writing a new one
sub clean_ldap_cache($) {
    my ($urpm) = @_;
    unlink glob("$urpm->{cachedir}/ldap/*");
}


=item get_ldap_config()

parse the system LDAP configuration file and return its config values

=cut

sub get_ldap_config() {
    return get_ldap_config_file($LDAP_CONFIG_FILE);
}

=item get_ldap_config_file($file)

parse a given LDAP configuration file and return its config values

=cut

sub get_ldap_config_file {
    my ($file) = @_;
    my %config;
    foreach (cat_($file)) {
	s/#.*//;
	s/^\s*//;
	s/\s*$//;
	s/\s{2}/ /g;
	/^$/ and next;
	/^(\S*)\s*(\S*)/ && $2 or next;
	$config{$1} = $2;
    }
    return %config && \%config;
}


=item get_ldap_config_dns()

Not implemented yet.

=cut

sub get_ldap_config_dns() {
    # TODO
    die "not implemented yet\n";
}

my %ldap_changed_attributes = (
    'source-name' => 'name',
    'with-hdlist' => 'with_hdlist',
    'http-proxy' => 'http_proxy',
    'ftp-proxy' => 'ftp_proxy',
    'media-info-dir' => 'media_info_dir',
);

=item load_ldap_media($urpm)

Loads urpmi media configuration from ldap.

=cut

sub load_ldap_media {
    my ($urpm) = @_;

    my $config = get_ldap_config() or return;

    $config->{ssl} = 'off';

    # try first urpmi_foo and then foo
    foreach my $opt (qw(base uri filter host ssl port binddn passwd scope)) {
        if (!defined $config->{$opt} && defined $config->{"urpmi_$opt"}) {
            $config->{$opt} = $config->{"urpmi_$opt"};
        }
    }

    die N("No server defined, missing uri or host") if !(defined $config->{uri} || defined $config->{host});
    die N("No base defined") if !defined $config->{base};

    if (! defined $config->{uri}) {
        $config->{uri} = "ldap" . ($config->{ssl} eq 'on' ? "s" : "") . "://" .
	    $config->{host} . ($config->{port} ? ":" . $config->{port} : "") . "/";
    }

    eval {
        require Net::LDAP;
        my $ldap = Net::LDAP->new($config->{uri})
            or die N("Cannot connect to ldap uri:"), $config->{uri};

        $ldap->bind($config->{binddn}, $config->{password})
            or die N("Cannot connect to ldap uri:"), $config->{uri};
        #- base is mandatory
        my $result = $ldap->search(
            base   => $config->{base},
            filter => $config->{filter} || '(objectClass=urpmiRepository)',
            scope  => $config->{scope} || 'sub',
        );

        $result->code and die $result->error;
        # FIXME more than one server ?
        clean_ldap_cache($urpm);

        foreach my $entry ($result->all_entries) {
            my $medium = {};

	    foreach my $opt (@per_media_opt, keys %ldap_changed_attributes) {
		my $v = $entry->get_value($opt);
		defined $v and $medium->{$opt} = $v;
	    }

            #- name is not valid for the schema ( already in top )
            #- and _ are forbidden in attributes names

            foreach (keys %ldap_changed_attributes) {
                $medium->{$ldap_changed_attributes{$_}} = $medium->{$_};
                delete $medium->{$_};
            }
            #- add ldap_ to reduce collision
            #- TODO check if name already defined ?
            $medium->{name} = "ldap_" . $medium->{name};
            $medium->{ldap} = 1;
            next if !check_ldap_medium($medium);
            urpm::media::add_existing_medium($urpm, $medium, 'nocheck');
            write_ldap_cache($urpm,$medium); 
        }
    };
    if ($@) {
        $urpm->{log}($@);
        read_ldap_cache($urpm);
    }

}

1;


=back

=head1 COPYRIGHT

Copyright (C) 2005 MandrakeSoft SA

Copyright (C) 2005-2010 Mandriva SA

Copyright (C) 2011-2020 Mageia

=cut