lib/Config/Parser/ldap.pm

filter: Statementstmt 91.8% 34 / 37 Branchbran 60.0% 6 / 10 Conditioncond 57.1% 4 / 7 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 6 / 6 Podpod 100% 2 / 2 totaltotal 80.3% 53 / 66 CC 9 SCAR 19.6
CC
9
Cov
81%
parse
10.5
BEGIN
1.0
BEGIN
1.0
CRAP 7.1
↑ Summary
1 # This file is part of Config::Parser::ldap -*- perl -*-
2 # Copyright (C) 2019-2021 Sergey Poznyakoff <gray@gnu.org>
3 #
4 # Config::Parser::ldap is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 3, or (at your option)
7 # any later version.
8 #
9 # Config::Parser::ldap is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with Config::Parser::ldap. If not, see <http://www.gnu.org/licenses/>.
16  
17 package Config::Parser::ldap;
18 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 7 ▶use parent 'Config::Parser';
Subroutine BEGIN: called CC=1 SCAR=0.0
21 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 7 our $VERSION = '1.02';
24  
25 =head1 NAME
26  
27 Config::Parser::ldap - configuration file parser for ldap.conf
28  
29 =head1 SYNOPSIS
30  
31 $cfg = new Config::Parser::ldap($filename);
32  
33 $base = $cfg->get('base');
34  
35  
36 =head1 DESCRIPTION
37  
38 A parser for F<ldap.conf> and similar files.
39  
40 The syntax of F<ldap.conf> configuration file is very simple. Each statement
41 occupies one physical line and consists of a keyword and its value separated
42 by one or more space characters. Keywords are case-insensitive. A value
43 starts with the first non-blank character after the keyword, and terminates
44 at the end of the line, or at the last sequence of blanks before the end of
45 the line.
46  
47 Blank lines and lines beginning with a hash mark are ignored.
48  
49 =head1 CONSTRUCTOR
50  
51 =head2 $cfg = new Config::Parser::ldap(%opts);
52  
53 Parses the supplied configuration file and creates a new object for
54 manipulating its settings. Keyword arguments I<%opts> are:
55  
56 =over 4
57  
58 =item filename
59  
60 Name of the file to parse. The file must exist.
61  
62 =item line
63  
64 Optional line where the configuration starts in I<$filename>. It is used
65 to keep track of statement location in the file for correct diagnostics.
66 If not supplied, 1 is assumed.
67  
68 =item fh
69  
70 File handle to read from. If it is not supplied, new handle will be
71 created by using open on the supplied I<$filename>.
72  
73 =item lexicon
74  
75 Dictionary of configuration statements that are allowed in the file. You
76 will most probably not need this parameter. It is listed here for completeness
77 sake. Refer to the L<Config::AST> constructor for details.
78  
79 =back
80  
81 =cut
82  
83 7 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
84 7 my ($class, %args) = @_;
85 7 $class->SUPER::new(%args, ci => 1);
86 }
87  
88 =head1 METHODS
89  
90 All methods for accessing the configuration settings are inherited from
91 L<Config::AST>.
92  
93 If you wish to use this class as a base class, please refer to
94 L<Config::Parser> for implementation details.
95  
96 =head1 EXAMPLE
97  
98 The following simplified example shows how to use this module to connect
99 and bind to a LDAP server.
100  
101 use Config::Parser::ldap;
102 use Net::LDAP;
103  
104 # Parse configuration file
105 $cf = new Config::Parser::ldap(filename => '/etc/ldap.conf');
106  
107 # Connect to server.
108 $ldap = Net::LDAP->new($cf->uri->value);
109  
110 # Start TLS if required
111 $args{capath} = $cf->get('tls_cacertdir');
112 $args{cafile} = $cf->get('tls_cacert');
113 $args{clientcert} = $cf->get('tls_cert');
114 $args{clientkey} = $cf->get('tls_key');
115 $args{ciphers} = $cf->get('tls_cipher_suite');
116 if ($reqcert = $cf->get('tls_reqcert')) {
117 my %tab = (
118 none => 'never',
119 allow => 'optional',
120 demand => 'require',
121 hard => 'require',
122 try => 'optional'
123 );
124 $args{verify} = $tab{$reqcert}
125 or die "unrecognized tls_reqcert: $reqcert";
126 }
127 $mesg = $ldap->start_tls(%args);
128 $mesg->code && die $mesg->error;
129  
130 # Bind
131 @bindargs = ();
132 if (my $v = $cf->get('binddn')) {
133 push @bindargs, $v
134 }
135 if (my $v = $cf->get('bindpw')) {
136 push @bindargs, password => $v;
137 }
138 $mesg = $ldap->bind(@bindargs);
139 $mesg->code && die $mesg->error;
140  
141 =cut
142  
143 11 ▶sub parse {
Subroutine parse: called CC=9 SCAR=23.5
Pod documented
144 11 my $self = shift;
145 11 ▶ my $filename = shift // confess "No filename given";
Condition50%1/2
shift() // confess("No filename given")
l!l
110
MC/DC0%0/1
shift() // confess("No filename given")
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
146 11 local %_ = @_;
147 11 my $fh = delete $_{fh};
148 11 ▶ unless ($fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh) 0 11 11
149 0 ▶ open($fh, "<", $filename)
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $filename or croak("can't open ${filename}: $!") 0 0 0
150 or croak "can't open $filename: $!";
151 }
152 11 ▶ my $line = delete $_{line} // 0;
Condition100%2/2
delete $_{"line"} // 0
l!l
47
MC/DC100%1/1
delete $_{"line"} // 0
delete $_{"line"}
Truth table100%2/2
A
A= delete $_{"line"}
Aresult
11
00
153  
154 11 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 71 11 82
155 71 ++$line;
156 71 chomp;
157 71 s/^\s+//;
158 71 s/\s+$//;
159 71 s/#.*//;
160 71 ▶ next if $_ eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "" 20 51 71
161 51 my ($kw, $val) = split /\s+/, $_, 2;
162 51 my $locus = new Text::Locus($filename, $line);
163 51 ▶ if (defined($kw) && defined($val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $kw and defined $val) { } 51 0 51
Condition33%1/3
defined $kw and defined $val
!ll&&!rl&&r
0051
MC/DC0%0/2
defined $kw and defined $val
defined $kw defined $val
Truth table33%1/3
A and B
A= defined $kwB= defined $val
ABresult
0X0
100
111
164 51 $self->add_value([$kw], $val, $locus);
165 } else {
166 0 $self->error("malformed line", locus => $locus);
167 0 $self->{_error_count}++;
168 }
169 }
170 11 return $self;
171 }
172  
173 =head1 SEE ALSO
174  
175 L<Config::AST>.
176  
177 L<Config::Parser>.
178  
179 =cut
180  
181 7 1;
↑ Summary