| 1 | # -*- mode: cperl; eval: (follow-mode); -*- | ||
| 2 | # | ||
| 3 | |||
| 4 | package App::Regather::Plugin::generic; | ||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | generic - generic plugin example | ||
| 9 | |||
| 10 | =cut | ||
| 11 | |||
| 12 | =head1 DESCRIPTION | ||
| 13 | |||
| 14 | generic plugin example | ||
| 15 | |||
| 16 | writes LDAP entry dump to the file set in config option | ||
| 17 | service.servicename.out_file | ||
| 18 | |||
| 19 | configuration: | ||
| 20 | |||
| 21 | =over | ||
| 22 | |||
| 23 | [service generic] | ||
| 24 | plugin = generic | ||
| 25 | ctrl_attr = uid | ||
| 26 | ctrl_srv_re = ^.*$ | ||
| 27 | out_path = /path/to/regather-output | ||
| 28 | |||
| 29 | [service generic map s] | ||
| 30 | uid = uid | ||
| 31 | |||
| 32 | =back | ||
| 33 | |||
| 34 | =cut | ||
| 35 | |||
| 36 | use strict; | ||
| 37 | use warnings; | ||
| 38 | use diagnostics; | ||
| 39 | |||
| 40 | use Net::LDAP; | ||
| 41 | use constant SYNST => [ qw( LDAP_SYNC_PRESENT LDAP_SYNC_ADD LDAP_SYNC_MODIFY LDAP_SYNC_DELETE ) ]; | ||
| 42 | |||
| 43 | use POSIX; | ||
| 44 | |||
| 45 | =head1 METHODS | ||
| 46 | |||
| 47 | =head2 new | ||
| 48 | |||
| 49 | Creates an instance of the class and saves a reference to its | ||
| 50 | arguments for further use. | ||
| 51 | |||
| 52 | =cut | ||
| 53 | |||
| 54 | sub new { | ||
| 55 | my $class = shift; | ||
| 56 | local %_ = %{$_[0]}; | ||
| 57 | |||
| 58 | $_{log}->cc( pr => 'debug', fm => "%s:%s: service %s; called for dn: %s", | ||
| 59 | ls => [ __FILE__,__LINE__, $_{s}, $_{obj}->dn, ] ) if $_{v} > 2; | ||
| 60 | |||
| 61 | my $example_dn = $_{obj}->dn; | ||
| 62 | my $example_out_file = $_{cf}->is_set('service', $_{s}, 'out_file') ? | ||
| 63 | $_{cf}->get('service', $_{s}, 'out_file') : | ||
| 64 | "regather-plugin-generic-example-output.txt"; | ||
| 65 | $_{log}->cc( pr => 'debug', fm => "%s:%s: service %s; out_file: %s", | ||
| 66 | ls => [ __FILE__,__LINE__, $_{s}, $example_out_file, ] ) if $_{v} > 2; | ||
| 67 | |||
| 68 | bless { | ||
| 69 | cf => delete $_{cf}, | ||
| 70 | force => delete $_{force}, | ||
| 71 | log => delete $_{log}, | ||
| 72 | obj => delete $_{obj}, | ||
| 73 | service => delete $_{s}, | ||
| 74 | st => delete $_{st}, | ||
| 75 | v => delete $_{v}, | ||
| 76 | |||
| 77 | example_dn => $example_dn, | ||
| 78 | example_out_file => $example_out_file, | ||
| 79 | |||
| 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 | |||
| 93 | sub example_dn {shift->{example_dn}} | ||
| 94 | sub example_out_file {shift->{example_out_file}} | ||
| 95 | |||
| 96 | =head2 ldap_sync_add_modify | ||
| 97 | |||
| 98 | writes LDAP entry dump to the file set in config option | ||
| 99 | service.servicename.out_file | ||
| 100 | |||
| 101 | =cut | ||
| 102 | |||
| 103 | sub ldap_sync_add_modify { | ||
| 104 | my $self = shift; | ||
| 105 | |||
| 106 | my $ts = POSIX::strftime( "%Y-%m-%d %H:%M:%S", localtime()); | ||
| 107 | |||
| 108 | $self->log->cc( pr => 'debug', fm => "%s:%s: dn: %s; example: LDAP sync event: %s processing start", | ||
| 109 | ls => [ __FILE__,__LINE__, $self->example_dn, SYNST->[$self->syncstate] ]) if $self->v > 0; | ||
| 110 | |||
| 111 | open(my $fh, ">>", '/tmp/' . $self->example_out_file) || do { | ||
| 112 | print "Can't open > /tmp/" . $self->example_out_file . " for writing: $!"; exit 1; }; | ||
| 113 | |||
| 114 | print $fh "\n\n$ts: generic plugin example: LDAP sync event: " . SYNST->[$self->syncstate] . "\n"; | ||
| 115 | $self->obj->dump($fh); | ||
| 116 | |||
| 117 | close($fh) || do { | ||
| 118 | print "close $self->example_out_file (opened for writing), failed: $!\n\n"; exit 1; }; | ||
| 119 | |||
| 120 | $self->log->cc( pr => 'debug', fm => "%s:%s: dn: %s; example: LDAP sync event: %s processing stop", | ||
| 121 | ls => [ __FILE__,__LINE__, $self->example_dn, SYNST->[$self->syncstate] ]) if $self->v > 0; | ||
| 122 | |||
| 123 | } | ||
| 124 | |||
| 125 | =head2 ldap_sync_delete | ||
| 126 | |||
| 127 | alias to ldap_sync_add_modify | ||
| 128 | |||
| 129 | =cut | ||
| 130 | |||
| 131 | sub ldap_sync_delete { goto &ldap_sync_add_modify } | ||
| 132 | |||
| 133 | ###################################################################### | ||
| 134 | |||
| 135 | 1; |