File Coverage

blib/lib/Samba/LDAP/Config.pm
Criterion Covered Total %
statement 52 61 85.2
branch 18 30 60.0
condition n/a
subroutine 10 10 100.0
pod 5 5 100.0
total 85 106 80.1


line stmt bran cond sub pod time code
1             package Samba::LDAP::Config;
2              
3             require 5.006;
4              
5 11     11   31204 use warnings;
  11         26  
  11         409  
6 11     11   58 use strict;
  11         25  
  11         389  
7 11     11   12049 use Readonly;
  11         10576  
  11         689  
8 11     11   2117 use Regexp::DefaultFlags;
  11         13433  
  11         105  
9 11     11   1062 use base qw( Config::Tiny );
  11         20  
  11         11899  
10              
11             our $VERSION = '0.05';
12              
13             #
14             # Add Log::Log4perl to all our classes!!!!
15             #
16              
17             # Regexp to save param and value in param="value", excluding ""
18             Readonly my $SPLIT_ON_EQUALS_SIGN => qr{
19             \A # Start of string
20             \s* # Whitespace 0 or more
21             ([^=]+?) # = is terminator of
22             # optionally match 1
23             # or more
24             \s*
25             = # Actual =
26             \s*
27             " # Actual "
28             ([^"]*) # " is terminator of
29             # match 0 or more
30             "
31             \s*
32             \z # End of string
33             };
34              
35             # Regexp for ${suffix}
36             Readonly my $SUFFIX_CHARS => qr{ \$ [{] suffix [}] \z};
37              
38             # Move this up here from read_string. Will benchmark later.
39             Readonly my $COMMENTS_AND_EMPTY_LINES => qr{
40             \A # Start of string
41             \s* # Whitespace 0 or more
42              
43             # Match one or the other
44             ( ?: [#] | [;] | \z )
45             };
46              
47             #========================================================================
48             # -- PUBLIC METHODS --
49             #========================================================================
50              
51             #------------------------------------------------------------------------
52             # find_smbldap()
53             #
54             # Looks for smbldap.conf
55             #------------------------------------------------------------------------
56              
57             sub find_smbldap {
58 23     23 1 1558 my $self = shift;
59              
60 23 50       1206 if ( -e '/etc/smbldap-tools/smbldap.conf' ) {
    50          
61 0         0 $self->{smbldap_conf} = '/etc/smbldap-tools/smbldap.conf';
62             }
63             elsif ( -e '/etc/opt/IDEALX/smbldap-tools/smbldap.conf' ) {
64 0         0 $self->{smbldap_conf} = '/etc/opt/IDEALX/smbldap-tools/smbldap.conf';
65             }
66             else {
67 23 100       367 if ( -d 't' ) {
68 8         192 chdir 't';
69             }
70 23         148 $self->{smbldap_conf} = 'smbldap.conf';
71             }
72              
73             # Add more locations here (don't really like this technique) or use
74             # Config::Find - Same applies for below two methods
75              
76 23         139 return $self->{smbldap_conf};
77             }
78              
79             #------------------------------------------------------------------------
80             # find_smbldap_bind()
81             #
82             # Looks for smbldap_bind.conf
83             #------------------------------------------------------------------------
84              
85             sub find_smbldap_bind {
86 23     23 1 763 my $self = shift;
87              
88 23 50       1246 if ( -e '/etc/smbldap-tools/smbldap_bind.conf' ) {
    50          
89 0         0 $self->{smbldap_bind_conf} = '/etc/smbldap-tools/smbldap_bind.conf';
90             }
91             elsif ( -e '/etc/opt/IDEALX/smbldap-tools/smbldap_bind.conf' ) {
92 0         0 $self->{smbldap_bind_conf} =
93             '/etc/opt/IDEALX/smbldap-tools/smbldap_bind.conf';
94             }
95             else {
96 23 50       245 if ( -d 't' ) {
97 0         0 chdir 't';
98             }
99 23         94 $self->{smbldap_bind_conf} = 'smbldap_bind.conf';
100             }
101              
102 23         110 return $self->{smbldap_bind_conf};
103             }
104              
105             #------------------------------------------------------------------------
106             # find_samba()
107             #
108             # Looks for smb.conf
109             #------------------------------------------------------------------------
110              
111             sub find_samba {
112 1     1 1 706 my $self = shift;
113              
114 1 50       28 if ( -e '/etc/samba/smb.conf' ) {
    50          
115 0         0 $self->{samba_conf} = '/etc/samba/smb.conf';
116             }
117             elsif ( -e 'usr/local/samba/lib/smb.conf' ) {
118 0         0 $self->{samba_conf} = '/usr/local/samba/lib/smb.conf';
119             }
120             else {
121 1 50       17 if ( -d 't' ) {
122 0         0 chdir 't';
123             }
124 1         4 $self->{samba_conf} = 'smb.conf';
125             }
126              
127 1         6 return $self->{samba_conf};
128             }
129              
130             #------------------------------------------------------------------------
131             # read_conf( $filename )
132             #
133             # Wrapper to provide an instant error message as returned by the native
134             # Config::Tiny read method
135             #------------------------------------------------------------------------
136              
137             sub read_conf {
138 44     44 1 92 my $self = shift;
139 44         84 my $file = shift;
140              
141 44         219 my $conf = $self->read($file);
142              
143             # Nice instant user error message
144 44 50       273 die $self->errstr() . "\nPlease fix this to continue!\n"
145             if $self->errstr();
146              
147 44         352 return $conf;
148             }
149              
150             #------------------------------------------------------------------------
151             # read_string()
152             #
153             # Overrides Config::Tiny's read_string to exclude the " " marks found in
154             # smbldap.conf and smbldap_bind.conf and remove section handling, as we
155             # don't have any [sections] in either of these files.
156             #
157             # Also substitutes the suffix hash ( ${suffix} ) with its value
158             #------------------------------------------------------------------------
159              
160             sub read_string {
161 44 50   44 1 6326 my $class = ref $_[0] ? ref shift: shift;
162 44         193 my $self = bless {}, $class;
163 44 50       504 return undef if !defined $_[0];
164              
165             # Parse the file
166 44         73 my $counter = 0;
167 44         47249 foreach ( split /(?:\015{1,2}\012|\015|\012)/, shift ) {
168 5214         42179 $counter++;
169              
170             # Skip comments and empty lines
171 5214 100       16680 next if /$COMMENTS_AND_EMPTY_LINES/;
172              
173             # Handle properties, but don't save " " and S&R ${suffix}
174 902 50       8546 if (/$SPLIT_ON_EQUALS_SIGN/) {
175 902         20001 my ( $before_equals, $after_equals ) = ( $1, $2 );
176              
177             # Save what's after suffix=, temporarily. (don't like this
178             # technique, will come back to it later though)
179 902 100       2031 if ( $before_equals eq 'suffix' ) {
180 22         66 $self->{sf_val} = $after_equals;
181             }
182              
183             # Replace ${suffix} with what was saved above
184 902         3651 $after_equals =~ s{ $SUFFIX_CHARS }{$self->{sf_val}};
185              
186             # as normal
187 902         16085 $self->{$before_equals} = $after_equals;
188 902         4052 next;
189             }
190              
191 0         0 return $self->_error("Syntax error at line $counter: '$_'");
192             }
193              
194             # Not needed for returning
195 44         2406 delete $self->{sf_val};
196              
197 44         1410 return $self;
198             }
199              
200             #========================================================================
201             # -- PRIVATE METHODS --
202             #========================================================================
203              
204             1; # Magic true value required at end of module
205              
206             __END__