This file is indexed.

/usr/share/perl5/Munin/Common/TLS.pm is in munin-common 2.0.25-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
package Munin::Common::TLS;

# $Id$

use warnings;
use strict;

use Carp;
use English qw(-no_match_vars);

sub new {
    my ($class, $args) = @_;

    my $self = {
        logger             => $args->{logger},
        read_fd            => $args->{read_fd},
        read_func          => $args->{read_func},
        write_fd           => $args->{write_fd},
        write_func         => $args->{write_func},
    };

    for my $key (keys %$self) {
        croak "Required argument missing: $key" unless defined $self->{$key};
    }

    $self = {
        %$self,
        DEBUG              => $args->{DEBUG} || 0,
        tls_ca_cert        => $args->{tls_ca_cert} || '',
        tls_cert           => $args->{tls_cert} || '',
        tls_paranoia       => $args->{tls_paranoia}|| 0,
        tls_priv           => $args->{tls_priv} || '',
        tls_vdepth         => $args->{tls_vdepth} || 0,
        tls_verify         => $args->{tls_verify} || 0,
        tls_match          => $args->{tls_match} || '',
    };

    for my $args_key (keys %$args) {
        croak "Unrecognized argument: $args_key" unless exists $self->{$args_key};
    }

    $self = {
        %$self,
        tls_context        => undef,
        tls_session        => undef,
        private_key_loaded => 0,
    };

    return bless $self, $class;
}


sub _start_tls {
    my $self = shift;

    my %tls_verified = (
        level          => 0, 
        cert           => "",
        verified       => 0, 
        required_depth => $self->{tls_vdepth}, 
        verify         => $self->{tls_verify},
    );

    $self->{logger}("[TLS] Enabling TLS.") if $self->{DEBUG};
    
    $self->_load_net_ssleay()
        or return 0;

    $self->_initialize_net_ssleay();

    $self->{tls_context} = $self->_creat_tls_context();

    $self->_load_private_key()
        or return 0;
    
    $self->_load_certificate();

    $self->_load_ca_certificate();
    
    $self->_initial_communication()
        or return 0;
    
    $self->_set_peer_requirements(\%tls_verified);
    
    if (! ($self->{tls_session} = Net::SSLeay::new($self->{tls_context})))
    {
	$self->{logger}("[ERROR] Could not create TLS: $!");
	return 0;
    }

    $self->_log_cipher_list() if $self->{DEBUG};

    $self->_set_ssleay_file_descriptors();

    $self->_accept_or_connect(\%tls_verified);

    return $self->{tls_session};
}


sub _load_net_ssleay {
    my ($self) = @_;

    eval {
        require Net::SSLeay;
    };
    if ($@) {
	$self->{logger}("[ERROR] TLS enabled but Net::SSLeay unavailable.");
	return 0;
    }

    return 1;
}


sub _initialize_net_ssleay {
    my ($self) = @_;

    Net::SSLeay::load_error_strings();
    Net::SSLeay::SSLeay_add_ssl_algorithms();
    Net::SSLeay::randomize();
}


sub _creat_tls_context {
    my ($self) = @_;

    my $ctx = Net::SSLeay::CTX_new();
    if (!$ctx) {
	$self->{logger}("[ERROR] Could not create SSL_CTX");
	return 0;
    }

    # Tune a few things...
    Net::SSLeay::CTX_set_options($ctx, Net::SSLeay::OP_ALL());
    if (my $errno = Net::SSLeay::ERR_get_error()) {
	$self->{logger}("[ERROR] Could not set SSL_CTX options: " + Net::SSLeay::ERR_error_string($errno));
	return 0;
    }

    return $ctx;
}


sub _load_private_key {
    my ($self) = @_;

    if (defined $self->{tls_priv} and length $self->{tls_priv}) {
    	if (-e $self->{tls_priv} or $self->{tls_paranoia} eq "paranoid") {
	    if (Net::SSLeay::CTX_use_PrivateKey_file($self->{tls_context}, 
                                                     $self->{tls_priv}, 
                                                     &Net::SSLeay::FILETYPE_PEM)) {
                $self->{private_key_loaded} = 1;
            }
            else {
	        if ($self->{tls_paranoia} eq "paranoid") {
                    $self->{logger}("[ERROR] Problem occurred when trying to read file with private key \"$self->{tls_priv}\": $!");
		    return 0;
	        }
	        else {
                    $self->{logger}("[ERROR] Problem occurred when trying to read file with private key \"$self->{tls_priv}\": $!. Continuing without private key.");
	        }
	    }
	}
	else {
	    $self->{logger}("[WARNING] No key file \"$self->{tls_priv}\". Continuing without private key.");
        }
    }

    return 1;
}


sub _load_certificate {
    my ($self) = @_;

    if ($self->{tls_cert} && -e $self->{tls_cert}) {
        if (defined $self->{tls_cert} and length $self->{tls_cert}) {
	    if (!Net::SSLeay::CTX_use_certificate_file($self->{tls_context}, 
                                                       $self->{tls_cert}, 
                                                       &Net::SSLeay::FILETYPE_PEM)) {
	        $self->{logger}("[WARNING] Problem occurred when trying to read file with certificate \"$self->{tls_cert}\": $!. Continuing without certificate.");
	    }
        }
    }
    else {
	$self->{logger}("[WARNING] No certificate file \"$self->{tls_cert}\". Continuing without certificate.");
    }

    return 1;
}


sub _load_ca_certificate {
    my ($self) = @_;

    if ($self->{tls_ca_cert} && -e $self->{tls_ca_cert}) {
    	if(!Net::SSLeay::CTX_load_verify_locations($self->{tls_context}, $self->{tls_ca_cert}, '')) {
            $self->{logger}("[WARNING] Problem occurred when trying to read file with the CA's certificate \"$self->{tls_ca_cert}\": ".&Net::SSLeay::print_errs("").". Continuing without CA's certificate.");
   	 }
    }

    return 1;
}


sub _set_peer_requirements {
    my ($self, $tls_verified) = @_;

    $self->{tls_vdepth} = 5 if !defined $self->{tls_vdepth};
    Net::SSLeay::CTX_set_verify_depth ($self->{tls_context}, $self->{tls_vdepth});
    my $err = &Net::SSLeay::print_errs("");
    if (defined $err and length $err) {
        $self->{logger}("[WARNING] in set_verify_depth: $err");
    }
    Net::SSLeay::CTX_set_verify ($self->{tls_context}, 
                                 $self->{tls_verify}  ? &Net::SSLeay::VERIFY_PEER :
                                                        &Net::SSLeay::VERIFY_NONE,
                                 $self->_tls_verify_callback($tls_verified));
    $err = &Net::SSLeay::print_errs("");
    if (defined $err and length $err) {
        $self->{logger}("[WARNING] in set_verify: $err");
    }
    
    return 1;
}


sub _tls_verify_callback {
    my ($self, $tls_verified) = @_;

    return sub {
        my ($ok, $subj_cert, $issuer_cert, $depth, 
	    $errorcode, $arg, $chain) = @_;

        $tls_verified->{"level"}++;

        if ($ok) {
            $tls_verified->{"verified"} = 1;
            $self->{logger}("[TLS] Verified certificate.") if $self->{DEBUG};
            return 1;           # accept
        }
        
        if (!($tls_verified->{"verify"})) {
            $self->{logger}("[TLS] Certificate failed verification, but we aren't verifying.") if $self->{DEBUG};
            $tls_verified->{"verified"} = 1;
            return 1;
        }

        if ($tls_verified->{"level"} > $tls_verified->{"required_depth"}) {
            $self->{logger}("[TLS] Certificate verification failed at depth ".$tls_verified->{"level"}.".");
            $tls_verified->{"verified"} = 0;
            return 0;
        }

        return 0;               # Verification failed
    }
}


sub _log_cipher_list {
    my ($self) = @_;

    my $i = 0;
    my $p = '';
    my $cipher_list = 'Cipher list: ';
    $p=Net::SSLeay::get_cipher_list($self->{tls_session},$i);
    $cipher_list .= $p if $p;
    do {
        $i++;
        $cipher_list .= ', ' . $p if $p;
        $p=Net::SSLeay::get_cipher_list($self->{tls_session},$i);
    } while $p;
    $cipher_list .= '\n';
    $self->{logger}("[TLS] Available cipher list: $cipher_list.") if $self->{DEBUG};
}


sub _set_ssleay_file_descriptors {
    my ($self) = @_;

    Net::SSLeay::set_rfd($self->{tls_session}, $self->{read_fd});
    my $err = &Net::SSLeay::print_errs("");
    if (defined $err and length $err) {
        $self->{logger}("[TLS] Warning in set_rfd: $err");
    }
    Net::SSLeay::set_wfd($self->{tls_session}, $self->{write_fd});
    $err = &Net::SSLeay::print_errs("");
    if (defined $err and length $err) {
        $self->{logger}("[TLS] Warning in set_wfd: $err");
    }
}


sub _accept_or_connect {
    my ($self, $tls_verified) = @_;

    $self->{logger}("[TLS] Accept/Connect: $self->{private_key_loaded}, " . $self->_use_key_if_present()) if $self->{DEBUG};
    my $res;
    if ($self->_use_key_if_present()) {
        $res = Net::SSLeay::accept($self->{tls_session});
    }
    else {
        $res = Net::SSLeay::connect($self->{tls_session});
    }
    $self->{logger}("[TLS] Done Accept/Connect") if $self->{DEBUG};

    my $err = &Net::SSLeay::print_errs("");
    if (defined $err and length $err)
    {
	$self->{logger}("[ERROR] Could not enable TLS: " . $err);
	Net::SSLeay::free ($self->{tls_session});
	Net::SSLeay::CTX_free ($self->{tls_context});
	$self->{tls_session} = undef;
    }
    elsif (!$tls_verified->{"verified"} and $self->{tls_paranoia} eq "paranoid")
    {
	$self->{logger}("[ERROR] Could not verify CA: " . Net::SSLeay::dump_peer_certificate($self->{tls_session}));
	$self->_on_unverified_cert();
	Net::SSLeay::free ($self->{tls_session});
	Net::SSLeay::CTX_free ($self->{tls_context});
	$self->{tls_session} = undef;
    }
    elsif ($self->{"tls_match"} and
    	Net::SSLeay::dump_peer_certificate($self->{tls_session}) !~ /$self->{tls_match}/)
    { 
	$self->{logger}("[ERROR] Could not match pattern \"" . $self->{tls_match} .
		"\" in dump of certificate.");
	$self->_on_unmatched_cert();
	Net::SSLeay::free ($self->{tls_session});
	Net::SSLeay::CTX_free ($self->{tls_context});
	$self->{tls_session} = undef;
    }
    else
    {
	$self->{logger}("[TLS] TLS enabled.") if $self->{DEBUG};
	$self->{logger}("[TLS] Cipher `" . Net::SSLeay::get_cipher($self->{tls_session}) . "'.") if $self->{DEBUG};
	$self->{logger}("[TLS] client cert: " . Net::SSLeay::dump_peer_certificate($self->{tls_session})) if $self->{DEBUG};
    }
}


# Abstract method
sub _initial_communication {
    my ($self) = @_;
    croak "Abstract method called '_initial_communication', "
        . "needs to be defined in child" 
            if ref $self eq __PACKAGE__;
}


# Abstract method
sub _use_key_if_present {
    my ($self) = @_;
    croak "Abstract method called '_use_key_if_present', "
        . "needs to be defined in child" 
            if ref $self eq __PACKAGE__;
}


# Redefine in sub class if needed
sub _on_unverified_cert {}

# Redefine in sub class if needed
sub _on_unmatched_cert {}

sub read {
    my ($self) = @_;

    croak "Tried to do an encrypted read, but a TLS session is not started" 
        unless $self->session_started();

    my $read = Net::SSLeay::read($self->{tls_session});
    my $err = &Net::SSLeay::print_errs("");
    if (defined $err and length $err) {
        $self->{logger}("[TLS] Warning in read: $err");
        return;
    }
    undef $read if($read eq ''); # returning '' signals EOF

    $self->{logger}("DEBUG: < $read") if $self->{DEBUG} && defined $read;
    return $read;
}


sub write {
    my ($self, $text) = @_;

    croak "Tried to do an encrypted write, but a TLS session is not started" 
        unless $self->session_started();

    $self->{logger}("DEBUG: > $text") if $self->{DEBUG};

    Net::SSLeay::write($self->{tls_session}, $text);
    my $err = &Net::SSLeay::print_errs("");
    if (defined $err and length $err) {
        $self->{logger}("[TLS] Warning in write: $err");
        return 0;
    }
    
    return 1;
}


sub session_started {
    my ($self) = @_;

    return defined $self->{tls_session};
}


1;

__END__

=head1 NAME

Munin::Node::TLS - Abstract base class implementing the STARTTLS protocol


=head1 SYNOPSIS

Should not be called directly. See synopsis for
L<Munin::Common::TLSServer> and L<Munin::Common::TLSClient>.


=head1 METHODS

=over

=item B<new>

 my $tls = Munin::Common::TLSFoo->new({ # Substitute Foo with Client or Server
     # Mandatory attributes:  
     logger      => \&a_logger_func,
     read_fd     => fileno($socket),
     read_func   => \&a_socket_read_func,
     write_fd    => fileno($socket),
     write_func  => \&a_socket_read_func,

     # Optional attributes                          DEFAULTS
     DEBUG              => 0,                       # 0
     tls_ca_cert        => "path/to/ca/cert.pem",   # ''
     tls_cert           => "path/to/cert.pem",      # ''
     tls_paranoia       => 1,                       # 0
     tls_priv           => "path/to/priv_key.pem",  # ''
     tls_vdepth         => 5,                       # 0
     tls_verify         => 1,                       # 0
 });

Constructor. Should not be called directly. This documents the
attributes that are in common for L<Munin::Common::TLSServer> and
L<Munin::Common::TLSClient>.

=item B<read>

 my $msg = $tls->read();

Encrypted read.

=item B<write>

 $tls->write($msg);

Encrypted write.

=item B<session_started>

 my $bool = $tls->session_started();

Returns true if the TLS object is ready to read/write encrypted data.

=back