[2014] | 1 | package LATMOS::Accounts::Task::Unusedhosts; |
---|
| 2 | |
---|
| 3 | use strict; |
---|
| 4 | use warnings; |
---|
| 5 | use base qw(LATMOS::Accounts::Task); |
---|
| 6 | use LATMOS::Accounts; |
---|
| 7 | use LATMOS::Accounts::Log; |
---|
| 8 | use LATMOS::Accounts::Utils; |
---|
| 9 | use DateTime; |
---|
| 10 | |
---|
| 11 | =head1 NAME |
---|
| 12 | |
---|
| 13 | LATMOS::Accounts::Task::Unusedhosts - Disable host owned by inactive users |
---|
| 14 | |
---|
| 15 | =head1 DESCRIPTION |
---|
| 16 | |
---|
| 17 | This module disable nethost when owned by an expired or disabled user account, removing it from network |
---|
| 18 | |
---|
| 19 | =head1 PARAMETER |
---|
| 20 | |
---|
| 21 | =head2 netzone |
---|
| 22 | |
---|
| 23 | The C<netzone> object containing concerned hosts |
---|
| 24 | |
---|
| 25 | =head2 filter |
---|
| 26 | |
---|
| 27 | A filter limiting host search. |
---|
| 28 | |
---|
| 29 | =head2 disableMode |
---|
| 30 | |
---|
| 31 | The rules to disable hosts: |
---|
| 32 | |
---|
| 33 | =over 4 |
---|
| 34 | |
---|
| 35 | =item C<owner>: C<owner> is set and is inactive |
---|
| 36 | |
---|
| 37 | =item C<user>: C<user> is set and is inactive |
---|
| 38 | |
---|
| 39 | =item C<both>: C<owner> or C<user> (if set) is inactive |
---|
| 40 | |
---|
| 41 | =item C<effective> (default): If C<user> is set and is inactive otherwise if |
---|
| 42 | C<owner> is inactive |
---|
| 43 | |
---|
| 44 | =back |
---|
| 45 | |
---|
| 46 | =head2 test |
---|
| 47 | |
---|
| 48 | If set only log are emitted but changes are not done and mail not really sent |
---|
| 49 | |
---|
| 50 | =cut |
---|
| 51 | |
---|
| 52 | # Wait 30 minutes |
---|
| 53 | sub waitDelay { 30 * 60 } |
---|
| 54 | |
---|
| 55 | sub init { |
---|
| 56 | my ($self) = @_; |
---|
[2439] | 57 | my $LA = $self->LA; |
---|
[2014] | 58 | my $labase = $self->{base} ? $LA->base($self->{base}) : $LA->base; |
---|
| 59 | $labase && $labase->load or die "Cannot load base"; |
---|
| 60 | |
---|
| 61 | $self->{_la} = $LA; |
---|
| 62 | $self->{_base} = $labase; |
---|
| 63 | |
---|
| 64 | 1; |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | sub run { |
---|
| 68 | my ($self) = @_; |
---|
| 69 | |
---|
| 70 | $self->_enable; |
---|
| 71 | |
---|
| 72 | $self->_disable; |
---|
| 73 | |
---|
| 74 | 1; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | sub _filters { |
---|
| 78 | my ($self) = @_; |
---|
| 79 | |
---|
| 80 | my $filteredHosts; |
---|
| 81 | |
---|
| 82 | if (my @cfilters = $self->{syncm}->ini->val($self->{name}, 'filter')) { |
---|
[2024] | 83 | $filteredHosts = {}; |
---|
[2014] | 84 | foreach ($self->{_base}->search_objects('nethost', @cfilters)) { |
---|
| 85 | $filteredHosts->{$_} = 1; |
---|
| 86 | } |
---|
| 87 | } |
---|
| 88 | |
---|
[2024] | 89 | my $hosts; |
---|
[2014] | 90 | |
---|
| 91 | if (my @zones = $self->{syncm}->ini->val($self->{name}, 'netzone')) { |
---|
| 92 | |
---|
[2024] | 93 | $hosts = {}; |
---|
| 94 | |
---|
[2014] | 95 | my @filters; |
---|
| 96 | |
---|
| 97 | foreach my $zone (@zones) { |
---|
| 98 | la_log(LA_DEBUG, "Limiting affected host to zone %s", $zone); |
---|
| 99 | my $oZone = $self->{_base}->get_object('netzone', $zone) or do { |
---|
| 100 | la_log(LA_ERR, "Cannot get zone $zone named in config"); |
---|
| 101 | return; |
---|
| 102 | }; |
---|
| 103 | |
---|
[2024] | 104 | my @zonehosts = $oZone->get_attributes('hosts'); |
---|
[2014] | 105 | |
---|
[2024] | 106 | push(@filters, [ 'name=' . join('||', @zonehosts) ]); |
---|
[2014] | 107 | |
---|
| 108 | if ($oZone->get_attributes('allow_dyn')) { |
---|
| 109 | la_log(LA_DEBUG, "Zone %s has allow_dyn set, searching w/o ip", $zone); |
---|
| 110 | push(@filters, [ 'ip=NULL' ]); |
---|
| 111 | } |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | foreach my $filter (@filters) { |
---|
| 115 | foreach ($self->{_base}->search_objects('nethost', @$filter)) { |
---|
| 116 | $filteredHosts && !$filteredHosts->{$_} and next; |
---|
[2024] | 117 | $hosts->{$_} = 1; |
---|
[2014] | 118 | } |
---|
| 119 | } |
---|
[2024] | 120 | } else { |
---|
| 121 | $hosts = $filteredHosts; |
---|
[2014] | 122 | } |
---|
| 123 | |
---|
[2024] | 124 | return $hosts; |
---|
[2014] | 125 | } |
---|
| 126 | |
---|
| 127 | sub _disable { |
---|
| 128 | my ($self) = @_; |
---|
| 129 | |
---|
| 130 | my $test = $self->{syncm}->ini->val($self->{name}, 'test'); |
---|
| 131 | my $mode = $self->{syncm}->ini->val($self->{name}, 'disableMode', 'effective'); |
---|
| 132 | |
---|
| 133 | la_log(LA_DEBUG, "Checking for host to disable using %s method", $mode); |
---|
| 134 | |
---|
[2024] | 135 | my $hosts = $self->_filters; |
---|
[2014] | 136 | |
---|
| 137 | my @HostToDisable; |
---|
| 138 | for ($mode) { |
---|
| 139 | /owner/i and do { |
---|
| 140 | my @HostNoOwner = $self->{_base}->search_objects('nethost', 'exported=1', 'owner.active=0'); |
---|
| 141 | foreach (@HostNoOwner) { |
---|
| 142 | push(@HostToDisable, $_); |
---|
| 143 | } |
---|
| 144 | last; |
---|
| 145 | }; |
---|
| 146 | /user/i and do { |
---|
| 147 | my @HostNoUser = $self->{_base}->search_objects('nethost', 'exported=1', 'user.active=0'); |
---|
| 148 | foreach (@HostNoUser) { |
---|
| 149 | push(@HostToDisable, $_); |
---|
| 150 | } |
---|
| 151 | last; |
---|
| 152 | }; |
---|
| 153 | /both/i and do { |
---|
| 154 | my %HostNoUser = map { $_ => } ( |
---|
| 155 | $self->{_base}->search_objects('nethost', 'exported=1', 'user.active=0'), |
---|
| 156 | $self->{_base}->search_objects('nethost', 'exported=1', 'owner.active=0'), |
---|
| 157 | ); |
---|
| 158 | foreach (keys %HostNoUser) { |
---|
| 159 | push(@HostToDisable, $_); |
---|
| 160 | } |
---|
| 161 | last; |
---|
| 162 | }; |
---|
| 163 | /effective/i and do { |
---|
| 164 | my %HostNoUser = map { $_ => } ( |
---|
| 165 | $self->{_base}->search_objects('nethost', 'exported=1', 'user.active=0'), |
---|
| 166 | $self->{_base}->search_objects('nethost', 'exported=1', 'user=NULL', 'owner.active=0'), |
---|
| 167 | ); |
---|
| 168 | foreach (keys %HostNoUser) { |
---|
| 169 | push(@HostToDisable, $_); |
---|
| 170 | } |
---|
| 171 | last; |
---|
| 172 | }; |
---|
| 173 | |
---|
| 174 | la_log(LA_ERR, "Unknown host check method `%s'", $mode); |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | foreach my $hostname (sort @HostToDisable) { |
---|
[2024] | 178 | if ($hosts) { |
---|
| 179 | $hosts->{$hostname} or next; |
---|
| 180 | } |
---|
[2014] | 181 | my $Host = $self->{_base}->get_object('nethost', $hostname) or next; |
---|
| 182 | $self->{_base}->log( |
---|
[2024] | 183 | LA_NOTICE, |
---|
[2014] | 184 | '%sHost %s disable because owner or user is inactive', |
---|
| 185 | ($test ? '(Test) ' : ''), |
---|
| 186 | $hostname, |
---|
| 187 | ); |
---|
| 188 | $Host->ReportChange('Update', "Disabling because user/owner is inactive"); |
---|
| 189 | $Host->_set_c_fields( unexported => 1 ); |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | $self->{_base}->commit() |
---|
| 193 | unless($test); |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | sub _enable { |
---|
| 197 | my ($self) = @_; |
---|
| 198 | |
---|
| 199 | my $test = $self->{syncm}->ini->val($self->{name}, 'test'); |
---|
| 200 | my $mode = $self->{syncm}->ini->val($self->{name}, 'disableMode', 'effective'); |
---|
| 201 | |
---|
| 202 | la_log(LA_DEBUG, "Checking for host to disable using %s method", $mode); |
---|
| 203 | |
---|
[2024] | 204 | my $hosts = $self->_filters; |
---|
[2014] | 205 | |
---|
| 206 | my @HostToEnable; |
---|
| 207 | for ($mode) { |
---|
| 208 | /owner/i and do { |
---|
| 209 | my @HostNoOwner = $self->{_base}->search_objects('nethost', 'exported=0', 'owner.active=1'); |
---|
| 210 | foreach (@HostNoOwner) { |
---|
| 211 | push(@HostToEnable, $_); |
---|
| 212 | } |
---|
| 213 | last; |
---|
| 214 | }; |
---|
| 215 | /user/i and do { |
---|
| 216 | my @HostNoUser = $self->{_base}->search_objects('nethost', 'exported=0', 'user.active=1'); |
---|
| 217 | foreach (@HostNoUser) { |
---|
| 218 | push(@HostToEnable, $_); |
---|
| 219 | } |
---|
| 220 | last; |
---|
| 221 | }; |
---|
| 222 | /both/i and do { |
---|
| 223 | my %HostNoUser = map { $_ => } ( |
---|
| 224 | $self->{_base}->search_objects('nethost', 'exported=0', 'user.active=1', 'owner.active=1'), |
---|
| 225 | $self->{_base}->search_objects('nethost', 'exported=0', 'user=NULL', 'owner.active=1'), |
---|
| 226 | ); |
---|
| 227 | foreach (keys %HostNoUser) { |
---|
| 228 | push(@HostToEnable, $_); |
---|
| 229 | } |
---|
| 230 | last; |
---|
| 231 | }; |
---|
| 232 | /effective/i and do { |
---|
| 233 | my %HostNoUser = map { $_ => } ( |
---|
| 234 | $self->{_base}->search_objects('nethost', 'exported=0', 'user.active=1'), |
---|
| 235 | $self->{_base}->search_objects('nethost', 'exported=0', 'user=NULL', 'owner.active=1'), |
---|
| 236 | ); |
---|
| 237 | foreach (keys %HostNoUser) { |
---|
| 238 | push(@HostToEnable, $_); |
---|
| 239 | } |
---|
| 240 | last; |
---|
| 241 | }; |
---|
| 242 | |
---|
| 243 | la_log(LA_ERR, "Unknown host check method `%s'", $mode); |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | foreach my $hostname (sort @HostToEnable) { |
---|
[2024] | 247 | if ($hosts) { |
---|
| 248 | $hosts->{$hostname} or next; |
---|
| 249 | } |
---|
[2014] | 250 | my $Host = $self->{_base}->get_object('nethost', $hostname) or next; |
---|
| 251 | $self->{_base}->log( |
---|
| 252 | LA_INFO, |
---|
| 253 | '%sHost %s enable because owner or user is active', |
---|
| 254 | ($test ? '(Test) ' : ''), |
---|
| 255 | $hostname, |
---|
| 256 | ); |
---|
| 257 | $Host->ReportChange('Update', "Enabling because user/owner is active"); |
---|
| 258 | $Host->_set_c_fields( unexported => 0 ); |
---|
| 259 | } |
---|
| 260 | |
---|
| 261 | $self->{_base}->commit() |
---|
| 262 | unless($test); |
---|
| 263 | } |
---|
| 264 | |
---|
| 265 | 1; |
---|
| 266 | |
---|
| 267 | __END__ |
---|
| 268 | |
---|
| 269 | =head1 SEE ALSO |
---|
| 270 | |
---|
| 271 | L<LATMOS::Accounts>, L<LATMOS::Accounts::Task> |
---|
| 272 | |
---|
| 273 | =head1 AUTHOR |
---|
| 274 | |
---|
| 275 | Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt> |
---|
| 276 | |
---|
| 277 | =head1 COPYRIGHT AND LICENSE |
---|
| 278 | |
---|
| 279 | Copyright (C) 2012 CNRS SA/CETP/LATMOS |
---|
| 280 | |
---|
| 281 | This library is free software; you can redistribute it and/or modify |
---|
| 282 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
| 283 | at your option, any later version of Perl 5 you may have available. |
---|
| 284 | |
---|
| 285 | =cut |
---|
| 286 | |
---|