Regather/Plugin/configfile.pm untested

filter: Statementstmt 0.0% 0 / 215 Branchbran 0.0% 0 / 96 Conditioncond 0.0% 0 / 80 MC/DCmcdc 0.0% 0 / 47 Subroutinesub 0.0% 0 / 26 Podpod 0.0% 0 / 16 totaltotal 0.0% 0 / 480 CC 49 SCAR 64.2
CC
49
Cov
0%
ldap_sync_add_modify
1260.0
ldap_sync_delete
90.0
out_paths
42.0
CRAP 611.1
1 # -*- mode: cperl; eval: (follow-mode) -*-
2 #
3  
4 package App::Regather::Plugin::configfile;
5  
6 =head1 NAME
7  
8 configfile - plugin to generate configuration file
9  
10 =cut
11  
12 use strict;
13 use warnings;
14 use diagnostics;
15  
16 use POSIX;
17 use IPC::Open2;
18 use File::Temp;
19 use Template;
20  
21 use Net::LDAP;
22 use Net::LDAP::Util qw(generalizedTime_to_time);
23  
24 use constant SYNST => [ qw( LDAP_SYNC_PRESENT LDAP_SYNC_ADD LDAP_SYNC_MODIFY LDAP_SYNC_DELETE ) ];
25  
26 =head1 METHODS
27  
28 Each loadable module must provide at least two method: the
29 constructor B<new> and runtime method B<run>.
30  
31 =head2 new
32  
33 Creates an instance of the class and saves a reference to its
34 arguments for further use.
35  
36 =cut
37  
38 sub new {
39 my ( $self, $args ) = @_;
40  
41 bless {
42 cf => delete $args->{cf},
43 force => delete $args->{force},
44 log => delete $args->{log},
45 obj => delete $args->{obj},
46 out_file_old => delete $args->{out_file_old},
47 prog => delete $args->{prog},
48 rdn => delete $args->{rdn},
49 service => delete $args->{s},
50 st => delete $args->{st},
51 ts_fmt => delete $args->{ts_fmt},
52 v => delete $args->{v},
53 rest => $args,
54 }, $self;
55 }
56  
57 sub cf { shift->{cf} }
58 sub force { shift->{force} }
59 sub log { shift->{log} }
60 sub obj { shift->{obj} }
61 sub out_file_old { shift->{out_file_old} }
62 sub rdn { shift->{rdn} }
63 sub service { shift->{service} }
64 sub syncstate { shift->{st} }
65 sub ts_fmt { shift->{ts_fmt} }
66 sub v { shift->{v} }
67 sub rest { shift->{rest} }
68  
69  
70 =head2 ldap_sync_add_modify
71  
72 performs creation for new and re-wring for existent config file
73  
74 =cut
75  
76 sub ldap_sync_add_modify {
77 my $self = shift;
78  
79 my ($tt_vars, $pp, $chin, $chou, $chst, $cher);
80  
81 $self->log->cc( pr => 'debug', fm => "%s called with arguments: %s",
82 ls => [ sprintf("%s:%s",__FILE__,__LINE__), join(',', sort(keys( %{$self}))), ] ) if $self->{v} > 3;
83  
84 ### PREPARING OUTPUT RELATED VARIABLES
85 my %out_paths = out_paths( cf => $self->cf, obj => $self->obj,
86 service => $self->service, rdn => $self->rdn, log => $self->log );
87 return if ! %out_paths;
88 my $out_file_pfx //= $out_paths{out_file_pfx};
89 my $out_file //= $out_paths{out_file};
90 my $dir = $out_file_pfx // $self->cf->get('service', $self->service, 'out_path');
91 my $out_to = $dir . '/' . $out_file;
92  
93 $self->log->cc( pr => 'debug', fm => "%s: output directory: %s; file: %s",
94 ls => [ sprintf("%s:%s",__FILE__,__LINE__), $dir, $out_file ] ) if $self->{v} > 2;
95  
96 if ( defined $self->out_file_old ) {
97 if ( unlink $dir . '/' . $self->out_file_old ) {
98 $self->log->cc( pr => 'info', fm => "%s: file %s deleted (after ModRDN)",
99 ls => [ sprintf("%s:%s",__FILE__,__LINE__), $dir . '/' . $self->out_file_old ] );
100 } else {
101 $self->log->cc( pr => 'err', fm => "%s: %s not removed (after ModRDN); error: %s",
102 ls => [ sprintf("%s:%s",__FILE__,__LINE__), $dir . '/' . $self->out_file_old, $! ], nt => 1, );
103 }
104 }
105  
106 ### COLLECTING ALL MAPPED ATTRIBUTES VALUES
107 foreach my $i ( ( 'm', 's') ) {
108 if ( $self->cf->is_section('service', $self->service, 'map', $i) ) {
109 foreach my $j ( $self->cf->names_of('service', $self->service, 'map', $i) ) {
110 if ( $i eq 's' && ! $self->obj->exists( $self->cf->get('service', $self->service, 'map', $i, $j)) ) {
111 if ( $self->cf->get(qw(core dryrun)) ) {
112 $self->log->cc( pr => 'debug', fm => "%s: DRYRUN: %s to be deleted (no attribute: %s)",
113 ls => [ sprintf("%s:%s",__FILE__,__LINE__), $out_to, $self->cf->get('service', $self->service, 'map', $i, $j) ] );
114 } else {
115 if ( unlink $out_to ) {
116 $self->log->cc( pr => 'debug', fm => "%s: file %s deleted (no attribute: %s)",
117 ls => [ sprintf("%s:%s",__FILE__,__LINE__), $out_to, $self->cf->get('service', $self->service, 'map', $i, $j) ] )
118 if $self->{v} > 0;
119 } else {
120 $self->log->cc( pr => 'err', fm => "%s: %s not removed (no attribute: %s); error: %s",
121 ls => [ sprintf("%s:%s",__FILE__,__LINE__), $out_to, $self->cf->get('service', $self->service, 'map', $i, $j), $! ],
122 nt => 1, );
123 }
124 }
125  
126 ### if any of `map s` attributes doesn't exist, we delete that config file
127 ### preliminaryly and skip that attribute from been processed by Template
128 next;
129  
130 } elsif ( $i eq 'm' && $self->obj->exists( $self->cf->get('service', $self->service, 'map', $i, $j)) ) {
131 $tt_vars->{$j} = $self->obj->get_value( $self->cf->get('service', $self->service, 'map', $i, $j),
132 asref => 1 );
133 } else {
134 if ( $j =~ /certificateRevocationList/ ) {
135 $tt_vars->{$j} =
136 opensslize({ in => $self->obj->get_value( $self->cf->get('service', $self->service, 'map', $i, $j) ) });
137 } elsif ( $j =~ /cACertificate/ ) {
138 $tt_vars->{$j} =
139 opensslize({ cmd => 'x509',
140 in => $self->obj->get_value( $self->cf->get('service', $self->service, 'map', $i, $j) ),
141 log => $self->log,
142 v => $self->{v} });
143 } else {
144 $tt_vars->{$j} = $self->obj->get_value( $self->cf->get('service', $self->service, 'map', $i, $j) ) // 'NA';
145 }
146 }
147 }
148 }
149 }
150  
151 $tt_vars->{prog} = $self->{prog};
152 $tt_vars->{DN} = $self->obj->dn;
153 $tt_vars->{date} = strftime( $self->{ts_fmt}, localtime(time));
154 $tt_vars->{descr} = $self->obj->get_value('description')
155 if $self->obj->exists('description');
156 $tt_vars->{server} = ( split(/\@/, $self->obj->get_value('authorizedService')) )[1]
157 if $self->obj->exists('authorizedService');
158 $tt_vars->{createdby} =
159 $self->obj->exists('creatorsName') ?
160 ( split(/=/, ( split(/,/, $self->obj->get_value('creatorsName')) )[0]) )[1] :
161 'UNKNOWN';
162 $tt_vars->{modifiedby} =
163 $self->obj->exists('modifiersName') ?
164 ( split(/=/, ( split(/,/, $self->obj->get_value('modifiersName')) )[0]) )[1] :
165 'UNKNOWN';
166  
167 if ( ! $self->force && -e $out_to &&
168 ( generalizedTime_to_time($self->obj->get_value('modifyTimestamp'))
169 <
170 (stat($out_to))[9] ) ) {
171 $self->log->cc( pr => 'debug',
172 fm => "%s: skip. object %s is older than target file %s, (object modifyTimestamp: %s is older than file mtime: %s",
173 ls => [ sprintf("%s:%s",__FILE__,__LINE__), $self->obj->dn, $out_to,
174 strftime( "%F %T",
175 localtime(generalizedTime_to_time($self->obj->get_value('modifyTimestamp')))),
176 strftime( "%F %T", localtime((stat($out_to))[9])),
177 ] )
178 if $self->{v} > 0;
179 return;
180 }
181  
182 ### PICKING ROOT OBJECT RDN (IN OUR CASE IT IS "UID")
183 foreach ( reverse split(/,/, $self->obj->dn) ) {
184 next if $_ !~ /^uid=/;
185 $tt_vars->{uid} = ( split(/=/, $_) )[1];
186 last;
187 }
188  
189 ### DRYRUN
190 if ( $self->cf->get(qw(core dryrun)) ) {
191  
192 $self->log->cc( pr => 'debug', fm => "%s: DRYRUN: %s -> %s",
193 ls => [ sprintf("%s:%s",__FILE__,__LINE__),
194 sprintf("%s/%s", $self->cf->get(qw(core tt_path)),
195 $self->cf->get('service', $self->service, 'tt_file')),
196 $dir. '/' . $out_file
197 ] );
198  
199 if ( $self->cf->is_set($self->service, 'chmod') ) {
200 $self->log->cc( pr => 'err', fm => "%s: DRYRUN: chmod %s, %s",
201 ls => [ sprintf("%s:%s",__FILE__,__LINE__), $self->cf->get('service', $self->service, 'chmod'), $out_to ] );
202 } elsif ( $self->cf->is_set(qw(core chmod)) ) {
203 $self->log->cc( pr => 'err', fm => "%s: DRYRUN: chmod %s, %s",
204 ls => [ sprintf("%s:%s",__FILE__,__LINE__), $self->cf->get('core', 'chmod'), $out_to ] );
205 }
206  
207 if ( $self->cf->is_set($self->service, 'chown') ) {
208 $self->log->cc( pr => 'err', fm => "%s: DRYRUN: chown %s, %s, %s",
209 ls => [ sprintf("%s:%s",__FILE__,__LINE__), $self->obj->get_value('uidNumber'),
210 $self->obj->get_value('gidNumber'),
211 $out_to ] );
212 }
213 return;
214 }
215  
216 my ( $tmp_fh, $tmp_fn );
217 eval { $tmp_fh = File::Temp->new( UNLINK => 0, DIR => $dir ); };
218 if ( $@ ) {
219 $self->log->cc( pr => 'err', fm => "%s: File::Temp->new( DIR => %s ); service \"%s\"; err: \"%s\"",
220 ls => [ sprintf("%s:%s",__FILE__,__LINE__), $dir, $self->service, $@ ] );
221 return;
222 }
223 $tmp_fn = $tmp_fh->filename;
224 my $tt = Template->new( TRIM => $self->cf->get(qw(core tt_trim)),
225 ABSOLUTE => 1,
226 RELATIVE => 1,
227 OUTPUT_PATH => $dir,
228 DEBUG => $self->log->foreground // $self->cf->get(qw(core tt_debug)) );
229  
230 $self->log->cc( pr => 'err', fm => "%s: Template->new( OUTPUT_PATH => %s ) for service %s error: %s",
231 ls => [ sprintf("%s:%s",__FILE__,__LINE__), $dir, $self->service, $! ] )
232 if ! defined $tt;
233  
234 $tt->process( sprintf("%s/%s",
235 $self->cf->get(qw(core tt_path)),
236 $self->cf->get('service', $self->service, 'tt_file')),
237 $tt_vars,
238 $tmp_fh ) || do {
239 $self->log->cc( pr => 'err', fm => "%s: %s .tt process error: %s",
240 ls => [ sprintf("%s:%s",__FILE__,__LINE__), SYNST->[$self->syncstate], $tt->error ] );
241 return;
242 };
243  
244 close( $tmp_fh ) || do {
245 $self->log->cc( pr => 'err', fm => "%s: close file (opened for writing), service %s, failed: %s",
246 ls => [ sprintf("%s:%s",__FILE__,__LINE__), $self->service, $! ] );
247 next;
248 };
249  
250 if ( $self->cf->get(qw(core dryrun)) ) {
251 $self->log->cc( pr => 'debug', fm => "%s: DRYRUN: rename %s should be renamed to %s",
252 ls => [ sprintf("%s:%s",__FILE__,__LINE__), $tmp_fn, $out_file ] );
253 } else {
254 rename $tmp_fn, $out_to ||
255 $self->log->cc( pr => 'err', fm => "%s: rename %s to %s, failed",
256 ls => [ sprintf("%s:%s",__FILE__,__LINE__), $tmp_fn, $out_to ] );
257  
258 if ( -e $out_to ) {
259 if ( $self->cf->is_set('service', $self->service, 'chmod') ) {
260 chmod oct($self->cf->get('service', $self->service, 'chmod')), $out_to ||
261 $self->log->cc( pr => 'err', fm => "%s: chmod for %s failed",
262 ls => [ sprintf("%s:%s",__FILE__,__LINE__), $out_to ] );
263 } elsif ( $self->cf->is_set(qw(core chmod)) ) {
264 chmod oct($self->cf->(qw(core chmod))), $out_to ||
265 $self->log->cc( pr => 'err', fm => "%s: chmod for %s failed",
266 ls => [ sprintf("%s:%s",__FILE__,__LINE__), $out_to ] );
267 }
268  
269 if ( $self->cf->is_set('service', $self->service, 'chown') ) {
270 chown $self->obj->get_value('uidNumber'),
271 $self->obj->get_value('gidNumber'),
272 $out_to ||
273 $self->log->cc( pr => 'err', fm => "%s: chown (%s:%s) %s failed",
274 ls => [ sprintf("%s:%s",__FILE__,__LINE__), $self->obj->get_value('uidNumber'),
275 $self->obj->get_value('gidNumber'),
276 $out_to ] );
277 }
278 } else {
279 $self->log->cc( pr => 'err', fm => "%s: %s disappeared, no such file any more...",
280 ls => [ sprintf("%s:%s",__FILE__,__LINE__), $out_to ] );
281 }
282 }
283 $self->log->cc( pr => 'debug', fm => "%s: control %s: dn: %s processed successfully.",
284 ls => [ sprintf("%s:%s",__FILE__,__LINE__), SYNST->[$self->syncstate], $self->obj->dn ] )
285 if $self->{v} > 0;
286  
287 if ( $self->cf->is_set('service', $self->service, 'post_process') ) {
288 foreach $pp ( @{$self->cf->get('service', $self->service, 'post_process')} ) {
289 my $pid = open2( $chou, $chin, $pp );
290 waitpid( $pid, 0 );
291 $chst = $? >> 8;
292 if ( $chst ) {
293 $cher .= $_ while ( <$chou> );
294 $self->log->cc( pr => 'err', ls => [ sprintf("%s:%s",__FILE__,__LINE__), $self->service, $pp, $cher ], nt => 1,
295 fm => "%s: service %s post_process: %s, error: %s", );
296 }
297 }
298 }
299 }
300  
301 =head2 ldap_sync_delete
302  
303 performs deletion of an existent config file
304  
305 =cut
306  
307 sub ldap_sync_delete {
308 my $self = shift;
309  
310 my ($tt_vars, $pp, $chin, $chou, $chst, $cher);
311  
312 $self->log->cc( pr => 'debug', fm => "%s: %s called with arguments: %s",
313 ls => [ sprintf("%s:%s",__FILE__,__LINE__), join(',', sort(keys( %{$self}))), ] ) if $self->{v} > 3;
314  
315 ### PREPARING OUTPUT RELATED VARIABLES
316 my %out_paths = out_paths( cf => $self->cf, obj => $self->obj,
317 service => $self->service, rdn => $self->rdn, log => $self->log );
318 return if ! %out_paths;
319 my $out_file_pfx //= $out_paths{out_file_pfx};
320 my $out_file //= $out_paths{out_file};
321 my $dir = $out_file_pfx // $self->cf->get('service', $self->service, 'out_path');
322 my $out_to = $dir . '/' . $out_file;
323  
324 $self->log->cc( pr => 'debug', fm => "%s: output directory: %s; file: %s",
325 ls => [ sprintf("%s:%s",__FILE__,__LINE__), $dir, $out_file ] ) if $self->{v} > 2;
326  
327 if ( $self->cf->get(qw(core dryrun)) ) {
328 $self->log->cc( pr => 'debug', fm => "%s: DRYRUN: file %s should be deleted",
329 ls => [ sprintf("%s:%s",__FILE__,__LINE__), $out_to ] );
330 } else {
331 if ( unlink $out_to ) {
332 $self->log->cc( pr => 'debug', fm => "%s: file %s was successfully deleted",
333 ls => [ sprintf("%s:%s",__FILE__,__LINE__), $out_to ] )
334 if $self->{v} > 0;
335 } else {
336 $self->log->cc( pr => 'err', fm => "%s: file %s was not removed; error: %s",
337 ls => [ sprintf("%s:%s",__FILE__,__LINE__), $out_to, $! ] );
338 }
339 }
340 $self->log->cc( pr => 'debug', fm => "%s: control %s: dn: %s processing finished",
341 ls => [ sprintf("%s:%s",__FILE__,__LINE__), SYNST->[$self->syncstate], $self->obj->dn ] );
342  
343 if ( $self->cf->is_set('service', $self->service, 'post_process') ) {
344 foreach $pp ( @{$self->cf->get('service', $self->service, 'post_process')} ) {
345 my $pid = open2( $chou, $chin, $pp );
346 waitpid( $pid, 0 );
347 $chst = $? >> 8;
348 if ( $chst ) {
349 $cher .= $_ while ( <$chou> );
350 $self->log->cc( pr => 'err', ls => [ sprintf("%s:%s",__FILE__,__LINE__), $self->service, $pp, $cher ], nt => 1,
351 fm => "%s: service %s post_process: %s, error: %s", );
352 }
353 }
354 }
355  
356 }
357  
358  
359  
360 sub out_paths {
361 local %_ = @_;
362  
363 my ($out_file_pfx, $out_file);
364 if ( $_{cf}->is_set('service', $_{service}, 'out_file_pfx') &&
365 $_{cf}->is_set('service', $_{service}, 'out_file') ) {
366 $out_file_pfx = $_{obj}->get_value($_{cf}->get('service', $_{service}, 'out_file_pfx'));
367 $out_file_pfx = substr($out_file_pfx, 1) if $_{cf}->is_set(qw(core altroot));
368 if ( ! -d $out_file_pfx ) {
369 $_{log}->cc( pr => 'err', fm => "%s: service %s, target directory %s doesn't exist",
370 ls => [ sprintf("%s:%s",__FILE__,__LINE__), $_{service}, $out_file_pfx ] );
371 return ();
372 } else {
373 $out_file = sprintf("%s%s",
374 $_{cf}->get('service', $_{service}, 'out_file'),
375 $_{cf}->get('service', $_{service}, 'out_ext') // '');
376 }
377 } elsif ( ! $_{cf}->is_set('service', $_{service}, 'out_file_pfx') &&
378 $_{cf}->is_set('service', $_{service}, 'out_file')) {
379 $out_file = sprintf("%s%s",
380 $_{cf}->get('service', $_{service}, 'out_file'),
381 $_{cf}->get('service', $_{service}, 'out_ext') // '');
382 } elsif ( ! $_{cf}->is_set('service', $_{service}, 'out_file_pfx') &&
383 ! $_{cf}->is_set('service', $_{service}, 'out_file')) {
384 $out_file = sprintf("%s%s",
385 $_{rdn_val} // $_{obj}->get_value($_{rdn}),
386 $_{cf}->get('service', $_{service}, 'out_ext') // '');
387 }
388  
389 return ( out_file_pfx => $out_file_pfx, out_file => $out_file );
390 }
391  
392 sub opensslize {
393 my $args = shift;
394 my $arg = { cmd => $args->{cmd} // 'crl',
395 in => $args->{in},
396 inform => $args->{inform} // 'DER',
397 outform => $args->{outform} // 'PEM',
398 };
399  
400 my ( $chin, $chou );
401 my $pid = open2($chou, $chin,
402 '/usr/bin/openssl', $arg->{cmd}, '-inform', $arg->{inform}, '-outform', $arg->{outform});
403  
404 print $chin $arg->{in};
405 waitpid( $pid, 0 );
406 my $chst = $? >> 8;
407  
408 $args->{log}->cc( pr => 'err', fm => "%s: opensslize() error!", ls => [ sprintf("%s:%s",__FILE__,__LINE__) ] )
409 if $chst && $args->{v} > 1;
410  
411 $arg->{res} .= $_ while ( <$chou> );
412  
413 return $arg->{res};
414 }
415  
416 ######################################################################
417  
418 1;