Regather.pm untested

filter: Statementstmt 0.0% 0 / 435 Branchbran 0.0% 0 / 162 Conditioncond 0.0% 0 / 99 MC/DCmcdc 0.0% 0 / 64 Subroutinesub 0.0% 0 / 30 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 799 CC 82 SCAR 67.4
CC
82
Cov
0%
ldap_search_callback
1806.0
run
650.0
daemonize
90.0
CRAP 842.8
1 # -*- mode: cperl; eval: (follow-mode); -*-
2 #
3  
4 package App::Regather;
5  
6 use strict;
7 use warnings;
8 use diagnostics;
9  
10 use Carp;
11 use File::Basename;
12 use Getopt::Long qw(:config no_ignore_case gnu_getopt auto_version);
13 use IPC::Open2;
14 use List::Util qw(uniqstr);
15  
16 use Net::LDAP;
17 use Net::LDAP::LDIF;
18 use Net::LDAP::Constant qw(
19 LDAP_CONNECT_ERROR
20 LDAP_LOCAL_ERROR
21 LDAP_OPERATIONS_ERROR
22 LDAP_SUCCESS
23 LDAP_SYNC_ADD
24 LDAP_SYNC_DELETE
25 LDAP_SYNC_MODIFY
26 LDAP_SYNC_PRESENT
27 LDAP_SYNC_REFRESH_AND_PERSIST
28 LDAP_SYNC_REFRESH_ONLY
29 );
30 use Net::LDAP::Control::SyncRequest;
31 use Net::LDAP::Util qw(generalizedTime_to_time);
32  
33 use POSIX;
34 use Pod::Usage qw(pod2usage);
35 use Sys::Syslog qw(:standard :macros);
36 use Template;
37  
38 use App::Regather::Config;
39 use App::Regather::Logg;
40 use App::Regather::Plugin;
41  
42 use constant SYNST => [ qw( LDAP_SYNC_PRESENT LDAP_SYNC_ADD LDAP_SYNC_MODIFY LDAP_SYNC_DELETE ) ];
43  
44 # my @DAEMONARGS = ($0, @ARGV);
45 our $VERSION = '0.87.0';
46  
47 sub new {
48 my $class = shift;
49 my $self =
50 bless {
51 _progname => fileparse($0),
52 _daemonargs => [$0, @ARGV],
53 _opt => {
54 ch => undef,
55 cli => undef,
56 colors => 0,
57 config => '/usr/local/etc/regather.conf',
58 fg => 0,
59 force => 0,
60 plugin_list => 0,
61 strict => 0,
62 ts_fmt => "%a %F %T %Z (%z)",
63 v => 0,
64 }
65 }, $class;
66  
67 GetOptions(
68 'f|foreground' => \$self->{_opt}{fg},
69 'F|force' => \$self->{_opt}{force},
70 'c|config=s' => \$self->{_opt}{config},
71 'colors' => \$self->{_opt}{colors},
72 'C|cli=s%' => \$self->{_opt}{cli},
73 'S|strict' => \$self->{_opt}{strict},
74 'config-help' => \$self->{_opt}{ch},
75 'plugin-list' => \$self->{_opt}{plugin_list},
76 'v+' => \$self->{_opt}{v},
77 'h' => sub { pod2usage(-exitval => 0, -verbose => 2); exit 0 },
78 );
79  
80 $self->{_opt}{l} = new
81 App::Regather::Logg( prognam => $self->{_progname},
82 foreground => $self->{_opt}{fg},
83 colors => $self->{_opt}{colors} );
84  
85 if ( $self->{_opt}{plugin_list} ) {
86 App::Regather::Plugin->new( 'list' )->run;
87 exit 0;
88 }
89  
90 if ( ! -e $self->{_opt}{config} || -z $self->{_opt}{config} ) {
91 $self->{_opt}{l}->cc( pr => 'err', fm => "config file does not exist or is empty" );
92 $self->{_opt}{l}->cc( fg => 1, pr => 'err', fm => "config file does not exist or is empty" );
93 pod2usage(-exitval => 2, -sections => [ qw(USAGE) ]);
94 exit 1;
95 }
96  
97 $self->{_opt}{cf} = new
98 App::Regather::Config ( filename => $self->{_opt}{config},
99 logger => $self->{_opt}{l},
100 cli => $self->{_opt}{cli},
101 verbose => $self->{_opt}{v} );
102  
103 my $cf_mode = (stat($self->{_opt}{config}))[2] & 0777;
104 my $fm_msg;
105 if ( $cf_mode & 002 || $cf_mode & 006 ) {
106 $fm_msg = 'world';
107 } elsif ( $cf_mode & 020 || $cf_mode & 060) {
108 $fm_msg = 'group';
109 }
110 if ( defined $fm_msg ) {
111 $self->{_opt}{l}->cc( pr => 'err', fm => 'config file is accessible by ' . $fm_msg);
112 $self->{_opt}{l}->cc( fg => 1, pr => 'err', fm => 'config file is accessible by ' . $fm_msg);
113 pod2usage(-exitval => 2, -sections => [ qw(USAGE) ]);
114 exit 1;
115 }
116  
117 $self->{_opt}{last_forever} = 1;
118  
119 # !!! TO CORRECT
120 if ( ! defined $self->{_opt}{cf} ) {
121 $self->l->cc( pr => 'err', fm => "do fix config file ..." );
122 $self->l->cc( fg => 1, pr => 'err', fm => "do fix config file ..." );
123 pod2usage(-exitval => 2, -sections => [ qw(USAGE) ]);
124 exit 1;
125 }
126  
127 if ( $self->{_opt}{ch} ) {
128 $self->{_opt}{cf}->config_help;
129 exit 1;
130 }
131  
132 return $self;
133 }
134  
135 sub progname { shift->{_progname} }
136  
137 sub progargs { return join(' ', @{shift->{_daemonargs}}); }
138  
139 sub cf { shift->{_opt}{cf} }
140  
141 sub l { shift->{_opt}{l} }
142  
143 sub o {
144 my ($self,$opt) = @_;
145 croak "unknown/undefined variable"
146 if ! exists $self->{_opt}{$opt};
147 return $self->{_opt}{$opt};
148 }
149  
150 sub run {
151 my $self = shift;
152  
153 $self->l->cc( pr => 'info', fm => "%s:%s: options provided from CLI:\n%s", ls => [ __FILE__,__LINE__, $self->o('cli') ] )
154 if defined $self->o('cli') && keys( %{$self->o('cli')} ) && $self->o('v') > 1;
155  
156 $self->l->set_m( $self->cf->getnode('log')->as_hash );
157 $self->l->set( notify => [ $self->cf->get('core', 'notify') ] );
158 $self->l->set( notify_email => [ $self->cf->get('core', 'notify_email') ] );
159  
160 $self->l->cc( pr => 'info', fm => "%s: Dry Run is set on, no file is to be changed\n" )
161 if $self->cf->get(qw(core dryrun));
162 $self->l->cc( pr => 'info', fm => "%s:%s: Config::Parse object as hash:\n%s",
163 ls => [ __FILE__,__LINE__, $self->cf->as_hash ] ) if $self->o('v') > 3;
164 $self->l->cc( pr => 'info', fm => "%s:%s: %s",
165 ls => [ __FILE__,__LINE__, $self->progargs ] );
166 $self->l->cc( pr => 'info', fm => "%s:%s: %s v.%s is starting ...",
167 ls => [ __FILE__,__LINE__, $self->progname, $VERSION, ] );
168  
169 @{$self->{_opt}{svc}} = grep { $self->cf->get('service', $_, 'skip') != 1 } $self->cf->names_of('service');
170  
171 $self->daemonize if ! $self->o('fg');
172  
173 our $s;
174 my $cfgattrs = [];
175 my $mesg;
176 my @svc_map;
177  
178 foreach my $i ( @{$self->{_opt}{svc}} ) {
179 push @$cfgattrs, $self->cf->get('service', $i, 'ctrl_attr');
180 foreach ( qw( s m ) ) {
181 if ( $self->cf->is_section('service', $i, 'map', $_) ) {
182 @svc_map = values( %{ $self->cf->getnode('service', $i, 'map', $_)->as_hash } );
183 # push @svc_map, $self->cf->getnode('service', $i, 'ctrl_attr');
184 push @$cfgattrs, @svc_map;
185 } else {
186 @svc_map = ();
187 }
188 }
189  
190 push @{$cfgattrs}, '*' if $self->cf->get('service', $i, 'all_attr') != 0;
191  
192 $self->l->cc( pr => 'warning', ls => [ __FILE__,__LINE__, $i, ],
193 fm => "%s:%s: no LDAP attribute to process is mapped for service `%s`" )
194 if $self->cf->get('service', $i, 'all_attr') == 0 && scalar @svc_map == 0;
195  
196 }
197  
198 @{$cfgattrs} = uniqstr( sort( @{$cfgattrs},
199 qw(
200 associatedDomain
201 authorizedService
202 description
203 entryUUID
204 entryCSN
205 createTimestamp
206 creatorsName
207 modifiersName
208 modifyTimestamp
209 ) ) );
210  
211 #
212 ## -=== MAIN LOOP =====================================================-
213 #
214  
215 my $ldap_opt = $self->cf->getnode(qw(ldap opt))->as_hash;
216 my $uri = delete $ldap_opt->{uri};
217 while ( $self->o('last_forever') ) {
218 if ( $self->cf->is_set(qw(core altroot)) ) {
219 chdir($self->cf->get(qw(core altroot))) || do {
220 $self->l->cc( pr => 'err', fm => "%s:%s: main: unable to chdir to %s",
221 ls => [ __FILE__,__LINE__, $self->cf->get(qw(core altroot)) ] );
222 exit 1;
223 };
224 }
225  
226 $self->{_opt}{ldap} =
227 Net::LDAP->new( $uri, @{[ map { $_ => $ldap_opt->{$_} } %$ldap_opt ]} )
228 || do {
229 $self->l->cc( pr => 'err', fm => "%s:%s: Unable to connect to %s; error: %s",
230 ls => [ __FILE__,__LINE__, $uri, $! ] );
231 if ( $self->o('strict') ) {
232 exit LDAP_CONNECT_ERROR;
233 } else {
234 next;
235 }
236 };
237  
238 my $start_tls_options = $self->cf->getnode(qw(ldap ssl))->as_hash if $self->cf->is_section(qw(ldap ssl));
239 if ( exists $start_tls_options->{ssl} && $start_tls_options->{ssl} eq 'start_tls' ) {
240 delete $start_tls_options->{ssl};
241 eval {
242 $mesg =
243 $self->o('ldap')->start_tls( @{[ map { $_ => $start_tls_options->{$_} } %$start_tls_options ]} );
244 };
245 if ( $@ ) {
246 $self->l->cc( pr => 'err', fm => "%s:%s: TLS negotiation failed: %s", ls => [ __FILE__,__LINE__, $! ] );
247 if ( $self->o('strict') ) {
248 exit LDAP_CONNECT_ERROR;
249 } else {
250 next;
251 }
252 } else {
253 $self->l->cc( pr => 'info', fm => "%s:%s: TLS negotiation succeeded", ls => [ __FILE__,__LINE__ ] ) if $self->o('v') > 1;
254 }
255 } else {
256 if ( exists $start_tls_options->{ssl} && $start_tls_options->{ssl} eq 'none' ) {
257 $self->l->cc( pr => 'warning', fm => "%s:%s: TLS is explicitly disabled in the configuration file. LDAP traffic is transmitted in plaintext without encryption.",
258 ls => [ __FILE__,__LINE__ ] );
259 } else {
260 $self->l->cc( pr => 'warning', fm => "%s:%s: TLS is not configured, LDAP connection is unencrypted. Configure TLS in the config file, or explicitly set [ldap ssl] option 'ssl = none' to permit plaintext LDAP.",
261 ls => [ __FILE__,__LINE__ ] );
262 exit 1
263 }
264 }
265  
266  
267 my $bind = $self->cf->getnode(qw(ldap bnd))->as_hash if $self->cf->is_section(qw(ldap bnd));
268 if ( ref($bind) eq 'HASH' ) {
269 if ( exists $bind->{dn} ) {
270 my @bind_options;
271 push @bind_options, delete $bind->{dn};
272 while ( my($k, $v) = each %{$bind} ) {
273 push @bind_options, $k => $v;
274 }
275 $mesg = $self->o('ldap')->bind( @bind_options );
276 if ( $mesg->code ) {
277 ####### !!!!!!! TODO: to implement exponential delay on error sending to awoid log file/notify
278 ####### !!!!!!! queue overflow
279 $self->l->cc( pr => 'err', fm => "%s:%s: bind error: %s",
280 ls => [ __FILE__,__LINE__, $mesg->error ] );
281 if ( $self->o('strict') ) {
282 exit $mesg->code;
283 } else {
284 next;
285 }
286 }
287 }
288 }
289  
290 $self->{_opt}{req} =
291 Net::LDAP::Control::SyncRequest->new( mode => LDAP_SYNC_REFRESH_AND_PERSIST,
292 critical => 1,
293 cookie => undef, );
294  
295 $mesg = $self->o('ldap')->search( base => $self->cf->get(qw(ldap srch base)),
296 scope => $self->cf->get(qw(ldap srch scope)),
297 control => [ $self->o('req') ],
298 callback => sub {$self->ldap_search_callback(@_)},
299 filter => $self->cf->get(qw(ldap srch filter)),
300 attrs => $cfgattrs,
301 sizelimit=> $self->cf->get(qw(ldap srch sizelimit)),
302 timelimit=> $self->cf->get(qw(ldap srch timelimit)),
303 );
304 if ( $mesg->code ) {
305 $self->l->cc( pr => 'err',
306 fm => "%s:%s: LDAP search ERROR...\n% 13s%s\n% 13s%s\n% 13s%s\n% 13s%s\n\n",
307 ls => [ __FILE__,__LINE__,
308 'base: ', $self->cf->get(qw(ldap srch base)),
309 'scope: ', $self->cf->get(qw(ldap srch scope)),
310 'filter: ', $self->cf->get(qw(ldap srch filter)),
311 'attrs: ', join("\n", @{$cfgattrs}) ] );
312 $self->l->cc_ldap_err( mesg => $mesg );
313 exit $mesg->code if $self->o('strict');
314 } else {
315 $self->l->cc( pr => 'info',
316 fm => "%s:%s: LDAP search:\n% 13s%s\n% 13s%s\n% 13s%s\n% 13s%s\n\n",
317 ls => [ __FILE__,__LINE__,
318 'base: ', $self->cf->get(qw(ldap srch base)),
319 'scope: ', $self->cf->get(qw(ldap srch scope)),
320 'filter: ', $self->cf->get(qw(ldap srch filter)),
321 'attrs: ', join("\n", @{$cfgattrs}) ] ) if $self->o('v') > 2;
322 }
323 }
324  
325 $mesg = $self->o('ldap')->unbind;
326 if ( $mesg->code ) {
327 $self->l->cc_ldap_err( mesg => $mesg );
328 exit $mesg->code;
329 }
330  
331 closelog();
332  
333 }
334  
335 #
336 ## ===================================================================
337 #
338  
339 sub daemonize {
340 my $self = shift;
341  
342 my ( $pid, $fh, $pp, $orphaned_pid_mtime );
343 if ( -e $self->cf->get(qw(core pid_file)) ) {
344 open( $fh, "<", $self->cf->get(qw(core pid_file))) || do {
345 die "Can't open $self->cf->get(qw(core pid_file)) for reading: $!";
346 exit 1;
347 };
348 $pid = <$fh>;
349 close($fh) || do {
350 print "close $self->cf->get(qw(core pid_file)) (opened for reading) failed: $!\n\n";
351 exit 1;
352 };
353  
354 if ( kill(0, $pid) ) {
355 print "Doing nothing\npidfile $self->cf->get(qw(core pid_file)) of the proces with pid $pid, exists and the very process is alive\n\n";
356 exit 1;
357 }
358  
359 $orphaned_pid_mtime = strftime( $self->o('ts_fmt'), localtime( (stat( $self->cf->get(qw(core pid_file)) ))[9] ));
360 if ( unlink $self->cf->get(qw(core pid_file)) ) {
361 $self->l->cc( pr => 'debug', fm => "%s:%s: orphaned %s was removed",
362 ls => [ __FILE__,__LINE__, $self->cf->get(qw(core pid_file)) ] )
363 if $self->o('v') > 0;
364 } else {
365 $self->l->cc( pr => 'err', fm => "%s:%s: orphaned %s (mtime: %s) was not removed: %s",
366 ls => [ __FILE__,__LINE__, $self->cf->get(qw(core pid_file)), $orphaned_pid_mtime, $! ] );
367 exit 2;
368 }
369  
370 undef $pid;
371 }
372  
373 $pid = fork();
374 die "fork went wrong: $!\n\n" unless defined $pid;
375 exit(0) if $pid != 0;
376  
377 setsid || do { print "setsid went wrong: $!\n\n"; exit 1; };
378  
379 open( $pp, ">", $self->cf->get(qw(core pid_file))) || do {
380 print "Can't open $self->cf->get(qw(core pid_file)) for writing: $!"; exit 1; };
381 print $pp "$$";
382 close( $pp ) || do {
383 print "close $self->cf->get(qw(core pid_file)) (opened for writing), failed: $!\n\n"; exit 1; };
384  
385 if ( $self->o('v') > 1 ) {
386 open (STDIN, "</dev/null") || do { print "Can't redirect /dev/null to STDIN\n\n"; exit 1; };
387 open (STDOUT, ">/dev/null") || do { print "Can't redirect STDOUT to /dev/null\n\n"; exit 1; };
388 open (STDERR, ">&STDOUT") || do { print "Can't redirect STDERR to STDOUT\n\n"; exit 1; };
389 }
390  
391 $SIG{HUP} =
392 sub { my $sig = @_;
393 $self->l->cc( pr => 'warning', fm => "%s:%s: SIG %s received, restarting", ls => [ __FILE__,__LINE__, $sig ] );
394 exec('perl', @{$self->o('_daemonargs')}); };
395 $SIG{INT} = $SIG{QUIT} = $SIG{ABRT} = $SIG{TERM} =
396 sub { my $sig = @_;
397 $self->l->cc( pr => 'warning', fm => "%s:%s: SIG %s received, exiting", ls => [ __FILE__,__LINE__, $sig ] );
398 $self->{_opt}{last_forever} = 0;
399 };
400 $SIG{PIPE} = 'ignore';
401 $SIG{USR1} =
402 sub { my $sig = @_;
403 $self->l->cc( pr => 'warning', fm => "%s:%s: SIG %s received, doing nothing" ), ls => [ __FILE__,__LINE__, $sig ] };
404  
405 if ( $self->cf->is_set(qw(core uid)) && $self->cf->is_set(qw(core gid)) ) {
406 setgid ( $self->cf->get(qw(core gid_number)) ) || do { print "setgid went wrong: $!\n\n"; exit 1; };
407 setuid ( $self->cf->get(qw(core uid_number)) ) || do { print "setuid went wrong: $!\n\n"; exit 1; };
408 }
409  
410 $self->l->cc( pr => 'info', fm => "%s:%s: %s v.%s is started.", ls => [ __FILE__,__LINE__, $self->progname, $VERSION ] );
411 }
412  
413 sub ldap_search_callback {
414 my ( $self, $msg, $obj ) = @_;
415  
416  
417 my @controls = $msg->control;
418 my $syncstate = scalar @controls ? $controls[0] : undef;
419  
420 my ( $s, $st, $mesg, $entry, @entries, $ldif, $map,
421 $out_file_pfx_old,
422 $tmp_debug_msg,
423 $rdn, $rdn_old, $rdn_re,
424 $pp, $chin, $chou, $chst, $cher, $email, $email_body );
425  
426 ######## !! not needed ?
427 my $out_file_old;
428  
429 $self->l->cc( pr => 'debug', fm => "%s:%s: syncstate: %s", ls => [ __FILE__,__LINE__, $syncstate ] )
430 if $self->o('v') > 5;
431 $self->l->cc( pr => 'debug', fm => "%s:%s: object: %s", ls => [ __FILE__,__LINE__, $obj ] ) if $self->o('v') > 5;
432  
433 if ( defined $obj && $obj->isa('Net::LDAP::Entry') ) {
434 $rdn = ( split(/=/, ( split(/,/, $obj->dn) )[0]) )[0];
435 if ( defined $syncstate && $syncstate->isa('Net::LDAP::Control::SyncState') ) {
436 $self->l->cc( pr => 'debug', fm => "%s:%s: SYNCSTATE:\n%s:", ls => [ __FILE__,__LINE__, $syncstate ] )
437 if $self->o('v') > 4;
438 $st = $syncstate->state;
439 my %reqmod;
440 $self->l->cc( fm => "%s:%s: received control %s: dn: %s", ls => [ __FILE__,__LINE__, SYNST->[$st], $obj->dn ] );
441  
442 #######################################################################
443 ####### --- PRELIMINARY STUFF ----------------------------->>>>>>>>> 0
444 #######################################################################
445  
446 ### on object deletion, event LDAP_SYNC_DELETE is reported and returned object has reqType=delete
447 ### on attr deletion, event LDAP_SYNC_MODIFY is reported and returned object has reqType=modify
448 ### here is an attempt to reconstruct the onject by it's reqDN
449 if ( $st == LDAP_SYNC_DELETE ) {
450 $mesg = $self->o('ldap')->search( base => $self->cf->get(qw(ldap srch log_base)),
451 scope => 'sub',
452 sizelimit=> $self->cf->get(qw(ldap srch sizelimit)),
453 timelimit=> $self->cf->get(qw(ldap srch timelimit)),
454 filter => '(reqDN=' . $obj->dn . ')', );
455 if ( $mesg->code ) {
456 $self->l->cc( pr => 'err', nt => 1,
457 fm => "%s:%s: LDAP accesslog search on %s, error:\n% 13s%s\n% 13s%s\n% 13s%s\n\n",
458 ls => [ __FILE__,__LINE__, SYNST->[$st],
459 'base: ', $self->cf->get(qw(ldap srch log_base)),
460 'scope: ', 'sub',
461 'filter: ', '(reqDN=' . $obj->dn . ')' ] );
462 $self->l->cc_ldap_err( mesg => $mesg );
463 # exit $mesg->code; # !!! NEED TO DECIDE WHAT TO DO
464 } else {
465 if ( $mesg->count == 0 ) {
466 $self->l->cc( pr => 'err', nt => 1,
467 fm => "%s:%s: LDAP accesslog search on %s, returned no result:\n% 13s%s\n% 13s%s\n% 13s%s\n\n",
468 ls => [ __FILE__,__LINE__, SYNST->[$st],
469 'base: ', $self->cf->get(qw(ldap srch log_base)),
470 'scope: ', 'sub',
471 'filter: ', '(reqDN=' . $obj->dn . ')' ] );
472 return;
473 } else {
474 $entry = pop @{[$mesg->sorted]};
475 }
476  
477 if ( defined $entry && ! $entry->isa('Net::LDAP::Entry') ) {
478 $self->l->cc( pr => 'err', nt => 1,
479 fm => "%s:%s: LDAP accesslog search on %s, returned no result:\n% 13s%s\n% 13s%s\n% 13s%s\n\n",
480 ls => [ __FILE__,__LINE__, SYNST->[$st],
481 'base: ', $self->cf->get(qw(ldap srch log_base)),
482 'scope: ', 'sub',
483 'filter: ', '(reqDN=' . $obj->dn . ')' ] );
484 return;
485 } elsif ( defined $entry && $entry->get_value('reqType') eq 'delete' ) {
486 ############################################################################################
487 # this wrapping condition is for the cases when accesslog overlay wasn't configured #
488 # with option/s logops, logold, in which case there is no attribute reqOld #
489 ############################################################################################
490 if ( $entry->exists('reqOld') ) {
491 my $reqold = 'dn: ' . $obj->dn;
492 foreach ( @{$entry->get_value('reqOld', asref => 1)} ) {
493 s/^(.*;binary:) .*$/$1: c3R1Yg==/agis;
494 $reqold .= "\n" . $_;
495 }
496 my ( $file, @err );
497 open( $file, "<", \$reqold) ||
498 $self->l->cc( pr => 'err',
499 fm => "%s:%s: Cannot open data from variable to read ldif: %s",
500 ls => [ __FILE__,__LINE__, $! ] );
501 $ldif = Net::LDAP::LDIF->new( $file, "r", onerror => 'warn' );
502 while ( not $ldif->eof ) {
503 $entry = $ldif->read_entry;
504 $self->l->cc( pr => 'err', fm => "%s:%s: Reading LDIF error: %s",
505 ls => [ __FILE__,__LINE__, $ldif->error ] ) if $ldif->error;
506 }
507 $obj = $entry;
508 $ldif->done;
509 } else {
510 $self->l->cc( pr => 'warning', fm => "%s:%s: %s doesn't have attribute reqOld, so, there is no way to reconstruct the deleted object",
511 ls => [ __FILE__,__LINE__, $entry->get_value('reqDN') ] );
512 }
513 } elsif ( defined $entry && $entry->get_value('reqType') eq 'modify' ) {
514 ### ???
515 ### ??? modify never appears for LDAP_SYNC_DELETE
516 ### ???
517 ### here we re-assemble $obj to have all attributes before deletion and since
518 ### after that it'll has ctrl_attr but reqType=delete, it'll go to $st == LDAP_SYNC_DELETE
519 %reqmod = map { substr($_, 0, -2) => 1 } grep { /^(.*):-$/g }
520 @{$entry->get_value('reqMod', asref => 1)};
521  
522 $mesg = $self->o('ldap')->search( base => $obj->dn,
523 scope => 'base',
524 filter => '(objectClass=*)', );
525 if ( $mesg->code ) {
526 $self->l->cc( pr => 'err', nt => 1,
527 fm => "%s:%s: LDAP search %s %s error:\n% 13s%s\n% 13s%s\n% 13s%s\n\n",
528 ls => [ __FILE__,__LINE__, SYNST->[$st], 'reqType=modify',
529 'base: ', $self->cf->get(qw(ldap srch log_base)),
530 'scope: ', 'sub',
531 'filter: ', '(reqDN=' . $obj->dn . ')' ] );
532 $self->l->cc_ldap_err( mesg => $mesg );
533 # exit $mesg->code; # !!! NEED TO DECIDE WHAT TO DO
534 } else {
535 $obj = $mesg->entry(0);
536 $obj->add( map { $_ => $reqmod{$_} } keys %reqmod );
537 # $obj->add( $_ => $reqmod{$_} ) foreach ( keys %reqmod );
538 }
539 $self->l->cc( pr => 'debug', fm => "%s:%s: %s reqType=modify reqMod: %s",
540 ls => [ __FILE__,__LINE__, SYNST->[$st], \%reqmod ] ) if $self->o('v') > 3;
541 ### ???
542 ### ??? modify never appears for LDAP_SYNC_DELETE
543 ### ???
544 } else {
545 $self->l->cc( pr => 'err', nt => 1,
546 fm => "%s:%s: LDAP accesslog search on %s, returned an object but it's something wrong with it:\n% 13s%s\n% 13s%s\n% 13s%s\n\n",
547 ls => [ __FILE__,__LINE__, SYNST->[$st],
548 'base: ', $self->cf->get(qw(ldap srch log_base)),
549 'scope: ', 'sub',
550 'filter: ', '(reqDN=' . $obj->dn . ')' ] );
551 return;
552 }
553 }
554 } elsif ( $st == LDAP_SYNC_MODIFY ) {
555 $mesg = $self->o('ldap')->search( base => $self->cf->get(qw(ldap srch log_base)),
556 scope => 'sub',
557 sizelimit=> $self->cf->get(qw(ldap srch sizelimit)),
558 timelimit=> $self->cf->get(qw(ldap srch timelimit)),
559 filter => '(reqDN=' . $obj->dn . ')', );
560 if ( $mesg->code ) {
561 $self->l->cc( pr => 'err', nt => 1,
562 fm => "%s:%s: LDAP accesslog search on %s, error:\n% 13s%s\n% 13s%s\n% 13s%s\n\n",
563 ls => [ __FILE__,__LINE__, SYNST->[$st], nt => 1,
564 'base: ', $self->cf->get(qw(ldap srch log_base)),
565 'scope: ', 'sub',
566 'filter: ', '(reqDN=' . $obj->dn . ')' ] );
567 $self->l->cc_ldap_err( mesg => $mesg );
568 } else {
569 if ( $mesg->count > 0 ) {
570 ### modified object has accesslog records when it was add/modify/delete
571 ### before, as well as ModRDN ... so, we need to be sure, there is no accesslog
572 ### object with reqNewRDN=<$obj->dn RDN> close to the processing time of this $obj
573  
574 ### NEED TO BE FINISHED
575  
576 } elsif ( $mesg->count == 0 ) {
577 ### modified object has no accesslog records when it was ModRDN-ed so, we search
578 ### for accesslog object with reqNewRDN=<$obj->dn RDN> to know old object RDN to use
579 ### it further for $out_file
580 $mesg = $self->o('ldap')->search( base => $self->cf->get(qw(ldap srch log_base)),
581 scope => 'sub',
582 sizelimit=> $self->cf->get(qw(ldap srch sizelimit)),
583 timelimit=> $self->cf->get(qw(ldap srch timelimit)),
584 filter => '(reqNewRDN=' . (split(/,/, $obj->dn))[0] . ')', );
585 if ( $mesg->code ) {
586 $self->l->cc( pr => 'err', nt => 1,
587 fm => "%s:%s: LDAP accesslog search on %s, error:\n% 13s%s\n% 13s%s\n% 13s%s\n\n",
588 ls => [ __FILE__,__LINE__, SYNST->[$st],
589 'base: ', $self->cf->get(qw(ldap srch log_base)),
590 'scope: ', 'sub',
591 'filter: ', '(reqNewRDN=' . (split(/,/, $obj->dn))[0] . ')' ] );
592 $self->l->cc_ldap_err( mesg => $mesg );
593 # exit $mesg->code; # !!! NEED TO DECIDE WHAT TO DO
594 } else {
595 ### here we pick last reqNewRDN entry up, to find the latest UUID for entries
596 ### with same DN if the object was added/deleted/ModRDN-ed several times
597 @entries = $mesg->sorted;
598 $entry = pop @entries;
599 if ( defined $entry ) {
600 $rdn_re = qr/^$rdn: .*$/;
601 ###### !!! NEED FIX
602 ### slapd.log-20210510-regather-fails-on-rdn
603 ###
604 ### here we're searching master db log, and the record is absent there, while
605 ### it is still present (have no idea why) in local db log ...
606 ###
607 ### Can't use an undefined value as an ARRAY reference at /usr/local/lib/perl5/site_perl/App/Regather.pm line 546.
608 ### BEGIN failed--compilation aborted at /usr/local/bin/regather line 10 (#1)
609 ### (F) A value used as either a hard reference or a symbolic reference must
610 ### be a defined value. This helps to delurk some insidious errors.
611 ### Uncaught exception from user code:
612 ### Can't use an undefined value as an ARRAY reference at /usr/local/lib/perl5/site_perl/App/Regather.pm line 546.
613 ### BEGIN failed--compilation aborted at /usr/local/bin/regather line 10.
614 ###### !!! NEED FIX
615 if ( $entry->exists('reqOld') ) {
616 foreach ( @{$entry->get_value('reqOld', asref => 1)} ) {
617 $rdn_old = (split(/: /, $_))[1] if /$rdn_re/;
618 }
619 }
620 ### now we reconstruct original object
621 $mesg = $self->o('ldap')->search( base => $self->cf->get(qw(ldap srch log_base)),
622 scope => 'sub',
623 sizelimit=> $self->cf->get(qw(ldap srch sizelimit)),
624 timelimit=> $self->cf->get(qw(ldap srch timelimit)),
625 filter => sprintf("(reqEntryUUID=%s)",
626 $entry->get_value('reqEntryUUID')) );
627 if ( $mesg->code ) {
628 $self->l->cc( pr => 'err', nt => 1,
629 fm => "%s:%s: LDAP accesslog search on %s, error:\n% 13s%s\n% 13s%s\n% 13s%s\n\n",
630 ls => [ __FILE__,__LINE__, SYNST->[$st],
631 'base: ', $self->cf->get(qw(ldap srch log_base)),
632 'scope: ', 'sub',
633 'filter: ', sprintf("(reqEntryUUID=%s)",
634 $entry->get_value('reqEntryUUID') ) ] );
635 $self->l->cc_ldap_err( mesg => $mesg );
636 # exit $mesg->code; # !!! NEED TO DECIDE WHAT TO DO
637 } else {
638 @entries = $mesg->sorted;
639 if ( $entries[0]->get_value('reqType') eq 'add' ) {
640 ### here we re-assemble $obj to have all attributes on its creation except RDN,
641 ### which we'll set from next to the last element and since after that it'll has
642 ### ctrl_attr but reqType=add, it'll go to $st == LDAP_SYNC_DELETE
643 $obj->add( map { /^(.*):\+ (.*)$/g } @{$entry->get_value('reqMod', asref => 1)} );
644 $obj->replace( $rdn => $entries[scalar(@entries) - 2]->get_value($rdn) );
645 } else {
646 $self->l->cc( pr => 'err', nt => 1,
647 fm => "%s:%s: %s object (before ModRDN) to delete not found! accesslog reqType=add object not found, object reqEntryUUID=%s should be processed manually",
648 ls => [ __FILE__,__LINE__, SYNST->[$st], $entry->get_value('reqEntryUUID') ] );
649 }
650 }
651 } else {
652 $self->l->cc( pr => 'err', nt => 1, ls => [ __FILE__,__LINE__, SYNST->[$st] ],
653 fm => "%s:%s: LDAP accesslog search on %s object returned no result\n\n" );
654 }
655 }
656 }
657 }
658 }
659  
660 ### picking up a service, the $obj relates to
661 my $is_ctrl_attr;
662 my $ctrl_srv_re;
663 my $s;
664 foreach ( @{$self->{_opt}{svc}} ) {
665 $is_ctrl_attr = 0;
666 foreach my $ctrl_attr ( @{$self->cf->get('service', $_, 'ctrl_attr')} ) {
667 if ( $obj->exists( $ctrl_attr ) ) {
668 $is_ctrl_attr++;
669 } else {
670 $is_ctrl_attr--;
671 }
672 }
673 $ctrl_srv_re = $self->cf->get('service', $_, 'ctrl_srv_re');
674 if ( $is_ctrl_attr > 0 && $obj->dn =~ qr/$ctrl_srv_re/ &&
675 $is_ctrl_attr == scalar( @{$self->cf->get('service', $_, 'ctrl_attr')} ) ) {
676 $s = $_;
677 }
678 }
679  
680 if ( ! defined $s ) {
681 $self->l->cc( pr => 'warning', ls => [ __FILE__,__LINE__, $obj->dn, SYNST->[$st] ],
682 fm => "%s:%s: dn: %s is not configured to be processed on control: %s" )
683 if $self->o('v') > 2;
684 return;
685 }
686  
687 #######################################################################
688 ####### --------------------------------------------------->>>>>>>>> 1
689 #######################################################################
690 if ( $st == LDAP_SYNC_ADD || $st == LDAP_SYNC_MODIFY ) {
691  
692 # App::Regather::Plugin->new( 'args', { log => $self->log,
693 # params => [ 1, 2, 3]} )->run;
694 foreach my $svc ( @{$self->cf->get('service', $s, 'plugin')} ) {
695 App::Regather::Plugin->new( $svc, {
696 cf => $self->cf,
697 force => $self->o('force'),
698 log => $self->l,
699 obj => $obj,
700 out_file_old => $out_file_old,
701 prog => sprintf("%s v.%s", $self->progname, $VERSION),
702 rdn => $rdn,
703 s => $s,
704 st => $st,
705 ts_fmt => $self->o('ts_fmt'),
706 v => $self->o('v'),
707 } )->ldap_sync_add_modify;
708 }
709  
710 #######################################################################
711 ####### --------------------------------------------------->>>>>>>>> 2
712 #######################################################################
713 } elsif ( $st == LDAP_SYNC_DELETE ) {
714  
715 foreach my $svc ( @{$self->cf->get('service', $s, 'plugin')} ) {
716 App::Regather::Plugin->new( $svc, {
717 cf => $self->cf,
718 force => $self->o('force'),
719 log => $self->l,
720 obj => $obj,
721 out_file_old => $out_file_old,
722 prog => sprintf("%s v.%s", $self->progname, $VERSION),
723 rdn => $rdn,
724 s => $s,
725 st => $st,
726 ts_fmt => $self->o('ts_fmt'),
727 v => $self->o('v'),
728 } )->ldap_sync_delete;
729 }
730  
731 }
732 } elsif ( defined $syncstate && $syncstate->isa('Net::LDAP::Control::SyncDone') ) {
733 $self->l->cc( pr => 'debug', fm => "%s: Received SYNC DONE CONTROL" ) if $self->o('v') > 1;
734 } elsif ( ! defined $syncstate ) {
735 $self->l->cc( pr => 'warning', fm => "%s: LDAP entry without Sync State control" ) if $self->o('v') > 1;
736 }
737  
738 $self->o('req')->cookie($syncstate->cookie) if $syncstate->cookie;
739  
740 } elsif ( defined $obj && $obj->isa('Net::LDAP::Intermediate') ) {
741 $self->l->cc( pr => 'debug', fm => "%s:%s: Received Net::LDAP::Intermediate\n%s", ls => [ __FILE__,__LINE__, $obj ] )
742 if $self->o('v') > 3;
743 $self->o('req')->cookie($obj->{'asn'}->{'refreshDelete'}->{'cookie'});
744 } elsif ( defined $obj && $obj->isa('Net::LDAP::Reference') ) {
745 $self->l->cc( pr => 'debug', fm => "%s:%s: Received Net::LDAP::Reference\n%s", ls => [ __FILE__,__LINE__, $obj ] )
746 if $self->o('v') > 3;
747 return;
748 } else {
749 return;
750 }
751 }
752  
753 1;