Regather/Plugin/script.pm untested

filter: Statementstmt 0.0% 0 / 57 Branchbran 0.0% 0 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 93 CC 5 SCAR 17.6
CC
5
Cov
0%
ldap_sync_add_modify
20.0
new
6.0
cf
2.0
CRAP 5.8
1 # -*- mode: cperl; eval: (follow-mode); -*-
2 #
3  
4 package App::Regather::Plugin::script;
5  
6 =head1 NAME
7  
8 script - plugin to run a script against LDAP object on LDAP_SYNC event
9  
10 =cut
11  
12 =head1 DESCRIPTION
13  
14 script - plugin to run script against LDAP object attributes, set as
15 environment variables on LDAP_SYNC event
16  
17 environment variables provided to the script:
18  
19 =over
20  
21 REGATHER_LDAP_OBJ_ATTR_<attributeX_name>
22 REGATHER_LDAP_OBJ_DN
23 REGATHER_LDAP_OBJ_LDIF
24 REGATHER_LDAP_SYNC_CONTROL_CODE
25 REGATHER_LDAP_SYNC_CONTROL_NAME
26  
27 =back
28  
29 configuration:
30  
31 =over
32  
33 [service name_of_my_script_service]
34 plugin = script
35 all_attr = 1
36 ctrl_attr = uid
37 ctrl_srv_re = ^.*$
38 post_process = /path/to/scrip1
39 post_process = /path/to/scrip2
40  
41 =back
42  
43 all attributes will be exposed if option B<all_attr> not null
44  
45 =cut
46  
47 use strict;
48 use warnings;
49 use diagnostics;
50  
51 use IPC::Open2;
52  
53 use Net::LDAP;
54 use constant SYNST => [ qw( LDAP_SYNC_PRESENT LDAP_SYNC_ADD LDAP_SYNC_MODIFY LDAP_SYNC_DELETE ) ];
55  
56 =head1 METHODS
57  
58 =head2 new
59  
60 Creates an instance of the class and saves a reference to its
61 arguments for further use.
62  
63 =cut
64  
65 sub new {
66 my $class = shift;
67 local %_ = %{$_[0]};
68  
69 $_{log}->cc( pr => 'debug', fm => "%s:%s: service %s; called for dn: %s",
70 ls => [ __FILE__,__LINE__, $_{s}, $_{obj}->dn, ] ) if $_{v} > 2;
71  
72 bless {
73 cf => delete $_{cf},
74 force => delete $_{force},
75 log => delete $_{log},
76 obj => delete $_{obj},
77 service => delete $_{s},
78 st => delete $_{st},
79 v => delete $_{v},
80 rest => \%_,
81 }, $class;
82  
83 }
84  
85 sub cf { shift->{cf} }
86 sub force { shift->{force} }
87 sub log { shift->{log} }
88 sub obj { shift->{obj} }
89 sub service { shift->{service} }
90 sub syncstate { shift->{st} }
91 sub v { shift->{v} }
92 sub rest { shift->{rest} };
93  
94 =head2 ldap_sync_add_modify
95  
96 set environmental variables according LDAP object attribute:value
97 pairs and runs a script
98  
99 =cut
100  
101 sub ldap_sync_add_modify {
102 my $self = shift;
103  
104 my ($pp, $chin, $chou, $chst, $cher);
105 if ( $self->cf->is_set('service', $self->service, 'post_process') ) {
106  
107 local %ENV;
108 $ENV{REGATHER_LDAP_OBJ_DN} = $self->obj->dn;
109 $ENV{REGATHER_LDAP_OBJ_LDIF} = $self->obj->ldif;
110 $ENV{REGATHER_LDAP_SYNC_CONTROL_CODE} = $self->syncstate;
111 $ENV{REGATHER_LDAP_SYNC_CONTROL_NAME} = SYNST->[$self->syncstate];
112 $ENV{"REGATHER_LDAP_OBJ_ATTR_" . $_} = $self->obj->get_value($_)
113 foreach ($self->obj->attributes);
114  
115 foreach $pp ( @{$self->cf->get('service', $self->service, 'post_process')} ) {
116 $self->log->cc( pr => 'debug', fm => "%s:%s: dn: %s; LDAP sync event %s processing script: %s",
117 ls => [ __FILE__,__LINE__, $self->obj->dn, SYNST->[$self->syncstate], $pp ]) if $self->v > 2;
118  
119 my $pid = open2( $chou, $chin, $pp );
120 waitpid( $pid, 0 );
121 $chst = $? >> 8;
122 $cher .= $_ while ( <$chou> );
123 if ( $chst ) {
124 $self->log->cc( pr => 'err', ls => [ __FILE__,__LINE__, $self->service, $pp, $cher ],
125 nt => 1, fm => "%s:%s: service %s: %s, error: %s", );
126 } else {
127 $self->log->cc( pr => 'info', fm => "%s:%s: service %s: %s",
128 ls => [ __FILE__,__LINE__, $self->service, $cher ], );
129 }
130 }
131  
132 }
133  
134 }
135  
136 =head2 ldap_sync_delete
137  
138 alias to ldap_sync_add_modify
139  
140 =cut
141  
142 sub ldap_sync_delete { goto &ldap_sync_add_modify }
143  
144 ######################################################################
145  
146 1;