Regather/Config.pm untested

filter: Statementstmt 0.0% 0 / 220 Branchbran 0.0% 0 / 88 Conditioncond 0.0% 0 / 44 MC/DCmcdc 0.0% 0 / 26 Subroutinesub 0.0% 0 / 24 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 413 CC 45 SCAR 54.0
CC
45
Cov
0%
mangle
600.0
new
30.0
get_ldap_config_file
30.0
CRAP 221.9
1 # -*- mode: cperl; eval: (follow-mode); -*-
2 #
3  
4 package App::Regather::Config;
5  
6 use strict;
7 use warnings;
8 use diagnostics;
9 use parent 'Config::Parser::Ini';
10 use Carp;
11 use File::Basename;
12 use Mail::RFC822::Address qw(valid validlist);
13  
14 use App::Regather::Plugin;
15  
16 use constant LDAP => { opt => { async => '',
17 debug => '',
18 inet4 => '',
19 inet6 => '',
20 keepalive => '',
21 localaddr => '',
22 multihomed => '',
23 onerror => '',
24 port => 'port',
25 raw => '',
26 scheme => '',
27 timeout => 'timeout',
28 uri => 'uri',
29 version => '',
30 },
31  
32 ssl => {
33 cafile => 'tls_cacert',
34 capath => 'tls_cacertdir',
35 checkcrl => 'tls_crlfile',
36 ciphers => 'tls_cipher_suite',
37 clientcert => 'tls_cert',
38 clientkey => 'tls_key',
39 keydecrypt => '',
40 sslversion => 'tls_protocol_min',
41 verify => { tls_reqcert => {
42 none => 'never',
43 allow => 'optional',
44 demand => 'require',
45 hard => 'require',
46 try => 'optional',
47 },
48 },
49 },
50 bnd => {
51 anonymous => '',
52 dn => 'binddn',
53 password => 'bindpw',
54 },
55  
56 srch=> {
57 attrs => '',
58 base => 'base',
59 filter => '',
60 raw => '',
61 scope => '',
62 sizelimit => 'sizelimit',
63 timelimit => 'timelimit',
64 }
65 };
66  
67 =pod
68  
69 =encoding UTF-8
70  
71 =head1 NAME
72  
73 App::Regather::Config - config file processing class
74  
75 =head1 SYNOPSIS
76  
77 =head1 DESCRIPTION
78  
79 This is a class to log messages.
80  
81 =head1 CONSTRUCTOR
82  
83 =over 4
84  
85 =item new
86  
87 Creates a new B<App::Regather::Config> object
88  
89 =over 4
90  
91 =item filename =E<gt> config-file-name
92  
93 Name of the file to parse.
94  
95 =item cli =E<gt> = delete $_{cli};
96  
97 Hash with CLI provided config options.
98  
99 =item logger =E<gt> = delete $_{logger};
100  
101 App::Regather::Logg object created preliminary.
102  
103 =item fg =E<gt> 0 | 1
104  
105 wheather we run in foreground
106  
107 =item verbose =E<gt> N
108  
109 verbosity level
110  
111 =back
112  
113 =back
114  
115 =cut
116  
117 sub new {
118 my $class = shift;
119 local %_ = @_;
120  
121 my $filename = delete $_{filename};
122 my $cli = delete $_{cli};
123 my $logger = delete $_{logger};
124 my $fg = delete $_{fg};
125 my $verbose = delete $_{verbose};
126 my $nodes = delete $_{add_nodes};
127  
128 my $self = $class->SUPER::new(%_);
129  
130 $self->{logger} = $logger;
131 $self->{verbose} = $verbose;
132  
133 $self->get_ldap_config_file;
134  
135 $self->parse($filename);
136  
137 if ( defined $cli && ref($cli) eq 'HASH' ) {
138 while ( my( $k, $v ) = each %{$cli} ) {
139 $self->add_value($k, $v, new Text::Locus("\noption \"$k\" provided from CLI",1)) ||
140 exit 1;
141 }
142 } elsif ( defined $cli && ref($cli) ne 'HASH' ) {
143 $self->error("malformed option/s provided from CLI");
144 exit 1;
145 }
146  
147 # set node/s (absent in config file) from arguments if any
148 if ( defined $nodes ) {
149 while (my ($key, $val) = each %$nodes) {
150 next if ! %$val;
151 while (my ($k, $v) = each %$val) {
152 # next if $self->is_set($key, $k);
153 $self->set($key, $k, $v);
154 }
155 }
156 }
157  
158 $self->commit or return;
159  
160 $self
161 }
162  
163 =head1 METHODS
164  
165 =over 4
166  
167 =item get_ldap_config_file
168  
169 ldap.conf processing (with L<Config::Parser::ldap>) to add options
170 from it to config object
171  
172 files searched are:
173  
174 $ENV{LDAP_CONF}
175 /usr/local/etc/openldap/ldap.conf
176 /etc/ldap.conf
177 /etc/ldap/ldap.conf
178 /etc/openldap/ldap.conf );
179  
180 the first one found is used.
181  
182 =cut
183  
184 sub get_ldap_config_file {
185 my $self = shift;
186  
187 use Config::Parser::ldap;
188  
189 my $ldap_config = {};
190 my @ldap_config_files = qw( /usr/local/etc/openldap/ldap.conf
191 /etc/ldap.conf
192 /etc/ldap/ldap.conf
193 /etc/openldap/ldap.conf );
194  
195 unshift @ldap_config_files, $ENV{LDAP_CONF} if defined($ENV{LDAP_CONF});
196  
197 my ( $cf, $val );
198  
199 foreach (@ldap_config_files) {
200 if ( -e $_ ) {
201 $cf = new Config::Parser::ldap(filename => $_ );
202  
203 foreach my $section ( keys %{ LDAP()} ) { # $section: bnd, opt or ssl
204 foreach my $item ( keys %{ LDAP->{$section} } ) { # $_: item in each of ones above
205  
206 $self->add_value( 'ldap.' . $section . '.' . $item,
207  
208 $section eq 'ssl' && $item eq 'verify' && $cf->is_set('tls_reqcert')
209 ?
210 LDAP->{$section}->{$item}->{tls_reqcert}->{ $cf->get('tls_reqcert') }
211 :
212 $cf->get( LDAP->{$section}->{$item} ),
213  
214 new Text::Locus("option \"$item\" provided from ldap.conf",1))
215 if LDAP->{$section}->{$item} ne '' &&
216 $cf->is_set( LDAP->{$section}->{$item} ) &&
217 ! $self->is_set( 'ldap', $section, $item );
218 }
219 }
220 last;
221 }
222 }
223 }
224  
225 =item mangle
226  
227 modify the created source tree. (resolve I<uid/gid> symbolic to number, add I<altroot>)
228  
229 =cut
230  
231 sub mangle {
232 my $self = shift;
233 my ( $section, $item, $k, $v );
234  
235 my $re_mod = qr(^Can.t locate.*);
236  
237 if ( $self->is_set(qw(core uid)) ) {
238 $item = getpwnam( $self->get(qw(core uid)) );
239 if ( defined $item ) {
240 $self->{logger}->cc( pr => 'info', fm => "%s: setuid user %s(%s) confirmed",
241 ls => [ sprintf("%s:%s",__FILE__,__LINE__), $self->get(qw(core uid)), $item ] )
242 if $self->{verbose} > 1;
243 $self->set('core', 'uid_number', $item);
244 } else {
245 print "No user $self->get('uid') found\n\n";
246 exit 2;
247 }
248 }
249  
250 if ( $self->is_set(qw(core gid)) ) {
251 $item = getgrnam( $self->get(qw(core gid)) );
252 if ( defined $item ) {
253 $self->{logger}->cc( pr => 'info', fm => "%s: setgid group %s(%s) confirmed",
254 ls => [ sprintf("%s:%s",__FILE__,__LINE__), $self->get(qw(core gid)), $item ] )
255 if $self->{verbose} > 1;
256 $self->set('core', 'gid_number', $item);
257 } else {
258 print "No group $self->get('gid') found\n\n";
259 exit 2;
260 }
261 }
262  
263 foreach my $svc ( $self->names_of('service') ) {
264 if ( $self->is_set(qw($svc uid)) ) {
265 $item = getpwnam( $self->get(qw($svc uid)) );
266 if ( defined $item ) {
267 $self->{logger}->cc( pr => 'info', fm => "%s: setuid user %s(%s) confirmed",
268 ls => [ sprintf("%s:%s",__FILE__,__LINE__), $self->get(qw($svc uid)), $item ] )
269 if $self->{verbose} > 1;
270 $self->set($svc, 'uid_number', $item);
271 } else {
272 print "No user $self->get('uid') found\n\n";
273 exit 2;
274 }
275 }
276  
277 if ( $self->is_set($svc, 'gid') ) {
278 $item = getgrnam( $self->get($svc, 'gid') );
279 if ( defined $item ) {
280 $self->{logger}->cc( pr => 'info', fm => "%s: setgid group %s(%s) confirmed",
281 ls => [ sprintf("%s:%s",__FILE__,__LINE__), $self->get(qw($svc gid)), $item ] )
282 if $self->{verbose} > 1;
283 $self->set($svc, 'gid_number', $item);
284 } else {
285 print "No group $self->get('gid') found\n\n";
286 exit 2;
287 }
288 }
289  
290 if ( $self->is_set('service', $svc, 'plugin') ) {
291 foreach my $plg ( $self->get('service', $svc, 'plugin') ) {
292  
293 if ( $plg eq 'nsupdate' ) {
294 eval { require Net::DNS };
295 if ( $@ =~ /$re_mod/ ) {
296 print "ERROR: ", sprintf("%s:%s",__FILE__,__LINE__), ": ", $@, "\n";
297 exit 2;
298 }
299  
300 if ( ! $self->is_set('service', $svc, 'ns_attr') ) {
301 print sprintf("%s:%s",__FILE__,__LINE__), ": service $svc lacks ns_attr option\n";
302 exit 2;
303 }
304 }
305  
306 if ($plg eq 'configfile' ) {
307 eval { require Template };
308 if ( $@ =~ /$re_mod/ ) {
309 print "ERROR: ", sprintf("%s:%s",__FILE__,__LINE__), ": ", $@, "\n";
310 exit 2;
311 }
312  
313 eval { require File::Temp };
314 if ( $@ =~ /$re_mod/ ) {
315 print "ERROR: ", sprintf("%s:%s",__FILE__,__LINE__), ": ", $@, "\n";
316 exit 2;
317 }
318  
319 if ( ! $self->is_set('service', $svc, 'tt_file') ) {
320 print sprintf("%s:%s",__FILE__,__LINE__), ": service $svc lacks tt_file option\n";
321 exit 2;
322 }
323 }
324  
325 }
326 }
327  
328 }
329  
330 if ( $self->is_set(qw(core altroot)) ) {
331 chdir($self->get(qw(core altroot))) || do {
332 $self->{logger}->cc( pr => 'err', fm => "%s: unable to chdir to %s",
333 ls => [ sprintf("%s:%s",__FILE__,__LINE__), $self->get(qw(core altroot)) ] );
334 exit 1;
335 };
336  
337 foreach ( $self->names_of('service') ) {
338 $self->add_value('service.' . $_ . '.out_path',
339 substr($self->get('service', $_, 'out_path'), 1),
340 new Text::Locus(sprintf("in \"%s\" ", $self->get(qw(core altroot))), 1)) ||
341 exit 1;
342 $self->{logger}->cc( pr => 'debug', fm => "%s: service %s out_path has been changed to %s",
343 ls => [ sprintf("%s:%s",__FILE__,__LINE__), $_, $self->get('service', $_, 'out_path') ] )
344 if $self->{verbose} > 1;
345 }
346 } else {
347 chdir('/');
348 }
349  
350 if ( $self->get(qw(core notify)) == 1 && ! $self->is_set(qw(core notify_email)) ) {
351 print "option core.notify requested, while core.notify_email is not set\n\n";
352 exit 2;
353 }
354 }
355  
356 =item config_help
357  
358 print config lexicon help
359  
360 output is not sorted, it is in todo
361  
362 =cut
363  
364 sub config_help {
365 my $self = shift;
366  
367 my $lex = $self->lexicon;
368 my ( $default, $re, $check );
369 foreach (sort keys %$lex) {
370 print "\n[$_]\n";
371 while ( my($k,$v) = each %{$lex->{$_}->{section}} ) {
372 if ( ref($v) eq 'HASH' && exists $v->{section} ) {
373 print "\n[$_ $k]\n";
374 while ( my($kk,$vv) = each %{$v->{section}} ) {
375 if ( ref($vv) eq 'HASH' && exists $vv->{section} ) {
376 print "\n[$_ $k $kk]\n";
377 while ( my($kkk,$vvv) = each %{$vv->{section}} ) {
378 if ( ref($vvv) eq 'HASH' && exists $vvv->{section} ) {
379 print "\n[$_ $k $kk $kkk]\n";
380 while ( my($kkkk,$vvvv) = each %{$vvv->{section}} ) {
381 print $self->config_help_opt($kkkk, $vvvv);
382 }
383 } else {
384 print $self->config_help_opt($kkk, $vvv);
385 }
386 }
387 } else {
388 print $self->config_help_opt($kk, $vv);
389 }
390 }
391 } else {
392 print $self->config_help_opt($k, $v);
393 }
394 }
395 }
396  
397 if ( $self->{verbose} > 0 ) {
398 print "\n\n";
399 $self->{logger}->cc( fg => 1, pr => 'info', fm => "%s: lexicon():%s\n%s",
400 ls => [ sprintf("%s:%s",__FILE__,__LINE__), '-' x 70, $lex ] );
401 }
402 }
403  
404 sub config_help_opt {
405 my ($self, $k, $v) = @_;
406 return sprintf(" %- 20s%s%s%s%s\n",
407 $k,
408 $v->{mandatory} ? ' :mandatory' : '',
409 $v->{default} ? ' :default ' . $v->{default} : '',
410 $v->{re} ? ' :re ' . $v->{re} : '',
411 $v->{check} ? ' :check ' . $v->{check} : '');
412 }
413  
414 =item chk_dir
415  
416 check wheather the target directory exists
417  
418 =cut
419  
420 sub chk_dir {
421 my ($self, $valref, $prev_value, $locus) = @_;
422  
423 unless ( -d $$valref ) {
424 $self->error("directory \"$$valref\" does not exist",
425 locus => $locus);
426 return 0;
427 }
428 $self->{chk_dir_passed} = 1;
429 return 1;
430 }
431  
432 sub chk_dir_pid {
433 my ($self, $valref, $prev_value, $locus) = @_;
434 my $dir = dirname($$valref);
435 unless ( -d $dir ) {
436 $self->error("pid file directory \"$dir\"does not exist",
437 locus => $locus);
438 return 0;
439 }
440 return 1;
441 }
442  
443 =item chk_file_tt
444  
445 .tt file existance checker
446  
447 =cut
448  
449 sub chk_file_tt {
450 my ($self, $valref, $prev_value, $locus) = @_;
451 my $tt = sprintf("%s/%s",
452 $self->tree->subtree('core')->subtree('tt_path')->value,
453 $$valref);
454  
455 unless ( -f $tt && -r $tt ) {
456 $self->error(sprintf("file \"%s\" does not exist", $tt),
457 locus => $locus);
458 return 0;
459 }
460 return 1;
461 }
462  
463 =item core_only
464  
465 informer (to spawn error if I<core> section option been used in not I<core> section)
466  
467 =cut
468  
469 sub core_only {
470 my ($self, $valref, $prev_value, $locus) = @_;
471 $self->error(sprintf("wrong location for option, it can appear only in the section \"core\""),
472 locus => $locus);
473 return 0;
474 }
475  
476 =item chk_notify_email
477  
478 email address validation against regex
479  
480 ^[a-z0-9]([a-z0-9.]+[a-z0-9])?\@[a-z0-9.-]+$
481  
482 =cut
483  
484 sub chk_notify_email {
485 my ($self, $valref, $prev_value, $locus) = @_;
486  
487 if ( ! valid($$valref) ) {
488 $self->error('notify_email is not valid email address', locus => $locus);
489 return 0;
490 }
491  
492 return 1;
493 }
494  
495 =item chk_plugin
496  
497 check plugin name against existent plugins list
498  
499 =cut
500  
501 sub chk_plugin {
502 my ($self, $valref, $prev_value, $locus) = @_;
503 my %names = App::Regather::Plugin->names;
504 $self->{plugin_names} //= [ keys(%names) ];
505 0 < grep { $$valref eq $_ } @{$self->{plugin_names}} ? return 1 : return 0;
506 }
507  
508 =item error
509  
510 error handler
511  
512 =cut
513  
514 =back
515  
516 sub error {
517 my $self = shift;
518 my $err = shift;
519 local %_ = @_;
520  
521 $self->{logger}->cc( pr => 'err',
522 fm => "%s: config parser error: %s%s",
523 ls => [ sprintf("%s:%s",__FILE__,__LINE__),
524 exists $_{locus} ? $_{locus} . ': ' : '',
525 $err ] );
526 }
527  
528  
529 =head1 CONFIG FILE
530  
531 An ini-style configuration file is a textual file consisting of
532 settings grouped into one or more sections.
533  
534 1. do read L<Config::Parser::Ini> documentation section B<DESCRIPTION>
535 for general description of the format used.
536  
537 2. look at the output of: I<regather -c regather.conf --config-help>
538  
539 3. look into sources ... (this section is to be amended yet)
540  
541 So, in general, config file consists of mandatory sections (with theirs
542 subsections) B<core>, B<ldap> and B<service>
543  
544 B<core> must go first, all other after it.
545  
546 Each section can have mandatory options.
547  
548 Each I<service> must have these options:
549  
550 =over 4
551  
552 1. at least one (can be set multiple times, and in that case all of
553 them are checked) option I<ctrl_attr> which contains name of the
554 attribute to check in event LDAP object. In case it is present,
555 the object is considered to be processed, in case it is absent,
556 we skip that event (since LDAP object has no I<ctrl_attr>)
557  
558 2. one I<ctrl_srv_re> option which is regular expression to match
559 service against LDAP event object DN
560  
561 3. at least one I<plugin> option.
562 B<This option should be placed in the end of the section>
563  
564 =back
565  
566 If both, 1. and 2. checks are positive, then object considered to be processed
567 for that service.
568  
569 Each I<service> must have atleast one of two possible maps. Those maps
570 are for mapping .tt variables to LDAP attributes values. Maps have
571 names I<s> for single value attributes and I<m> for attributes which
572 can have multiple values.
573  
574 =head1 SEE ALSO
575  
576 L<App::Regather::Logg>,
577 L<Config::AST>,
578 L<Config::Parser>,
579 L<Config::Parser::Ini>,
580 L<Config::Parser::ldap>
581  
582 =head1 AUTHOR
583  
584 Zeus Panchenko E<lt>zeus@gnu.org.uaE<gt>
585  
586 =head1 COPYRIGHT
587  
588 Copyright 2019 Zeus Panchenko.
589  
590 This program is free software; you can redistribute it and/or modify
591 it under the terms of the GNU General Public License as published by
592 the Free Software Foundation; either version 3, or (at your option)
593 any later version.
594  
595 This program is distributed in the hope that it will be useful,
596 but WITHOUT ANY WARRANTY; without even the implied warranty of
597 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
598 GNU General Public License for more details.
599  
600 You should have received a copy of the GNU General Public License
601 along with this program. If not, see <http://www.gnu.org/licenses/>.
602  
603 =cut
604  
605  
606 1;
607  
608  
609 __DATA__