summaryrefslogtreecommitdiff
path: root/FS/FS/part_export/router.pm
blob: eee7a4eb4cebca8b5a2a612397dc0fbbb12674e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package FS::part_export::router;

=head1 FS::part_export::router

This export connects to a router and transmits commands via telnet or SSH.
It requires the following custom router fields:

=head1 Required custom fields

=over 4

=item admin_address - IP address (or hostname) to connect.

=item admin_user - Username for the router.

=item admin_password - Password for the  router.

=item admin_protocol - Protocol to use for the router.  'telnet' or 'ssh'.  The ssh protocol only support password-less (ie. RSA key) authentication.  As such, the admin_password field isn't used if ssh is specified.

=item admin_timeout - Time in seconds to wait for a connection.

=item admin_prompt - A regular expression matching the router's prompt.  See Net::Telnet for details.  Only applies to the 'telnet' protocol.

=item admin_cmd_insert - Insert export command.

=item admin_cmd_insert_error - Insert export command error pattern.

=item admin_cmd_delete - Delete export command.

=item admin_cmd_delete_error - Delete export command error pattern.

=item admin_cmd_replace - Replace export command.

=item admin_cmd_replace_error - Replace export command error pattern.

=item admin_cmd_suspend - Suspend export command.

=item admin_cmd_suspend_error - Support export command error pattern.

=item admin_cmd_unsuspend - Unsuspend export command.

=item admin_cmd_unsuspend_error - Unsuspend export command error pattern.

The admin_cmd_* virtual fields, if set, will be processed in one of two ways.  After being expanded, they will be run on the router specified by admin_address using the protocol specified by admin_protocol.

=over 4

=item Text::Template

If the export command contains the string [@--, then it will be processed with Text::Template using [@-- and --@] as delimeters.

=item eval

If the export command does not contain [@--, it will be double quoted and eval'd.

=back

The admin_cmd_*_error virtual fields, if set, define a regular expression that will be matched against the output of the command being run.  If the pattern matches, an error will be raised using the output as the error.

If any of the required router virtual fields are not defined, then the export silently declines.

=back

The export itself takes no options.

=cut

use strict;
use vars qw(@ISA %info $me $DEBUG);
use Tie::IxHash;
use Text::Template;

use FS::Record qw(qsearchs);
use FS::part_export;

@ISA = qw(FS::part_export);

tie my %options, 'Tie::IxHash',
  'protocol' => {
	  label=>'Protocol',
	  type =>'select',
	  options => [qw(telnet ssh)],
	  default => 'telnet'},
;

%info = (
  'svc'     => 'svc_broadband',
  'desc'    => 'Send a command to a router.',
  'options' => \%options,
  'no_machine' => 1,
  'notes'   => 'Installation of Net::Telnet from CPAN is required for telnet connections.  This export will execute if the following virtual fields are set on the router: admin_user, admin_password, admin_address, admin_timeout, admin_prompt.  Option virtual fields are: admin_cmd_insert, admin_cmd_replace, admin_cmd_delete, admin_cmd_suspend, admin_cmd_unsuspend.  See the module documentation for a full list of required/supported router virtual fields.',
);

$me = '[' . __PACKAGE__ . ']';
$DEBUG = 1;


sub rebless { shift; }

sub _field_prefix { 'cf_admin'; }

sub _req_router_fields {
  map {
    $_[0]->_field_prefix . '_' . $_
  } (qw(address prompt user));
}

sub _export_insert {
  my($self) = shift;
  warn "Running insert for " . ref($self);
  $self->_export_command('insert', @_);
}

sub _export_delete {
  my($self) = shift;
  $self->_export_command('delete', @_);
}

sub _export_suspend {
  my($self) = shift;
  $self->_export_command('suspend', @_);
}

sub _export_unsuspend {
  my($self) = shift;
  $self->_export_command('unsuspend', @_);
}

sub _export_replace {
  my($self) = shift;
  $self->_export_command('replace', @_);
}

sub _export_command {
  my ($self, $action, $svc_broadband) = (shift, shift, shift);
  my ($error, $old);
  
  if ($action eq 'replace') {
    $old = shift;
  }

 warn "[debug]$me Processing action '$action'" if $DEBUG;

  # fetch router info
  my $router = $self->_get_router($svc_broadband, @_);
  unless ($router) {
    return "Unable to lookup router for $action export";
  }

  unless ($self->_check_router_fields($router)) {
    # Virtual fields aren't defined.  Exit silently.
    warn "[debug]$me Required router virtual fields not defined.  Returning..."
      if $DEBUG;
    return '';
  }

  my $args;
  ($error, $args) = $self->_prepare_args(
    $action,
    $router,
    $svc_broadband,
    ($old ? $old : ()),
    @_
  );

  if ($error) {
    # Error occured while preparing args.
    return $error;
  } elsif (not defined $args) {
    # Silently decline.
    warn "[debug]$me Declining '$action' export" if $DEBUG;
    return '';
  } # else ... queue the export.

  warn "[debug]$me Queueing with args: " . join(', ', @$args) if $DEBUG;

  return(
    $self->_queue(
      $svc_broadband->svcnum,
      $self->_get_cmd_sub($svc_broadband, $router),
      @$args
    )
  );

}

sub _prepare_args {

  my ($self, $action, $router, $svc_broadband) = (shift, shift, shift, shift);
  my $old = shift if ($action eq 'replace');
  my $error = '';

  my $field_prefix = $self->_field_prefix;
  my $command = $router->getfield("${field_prefix}_cmd_${action}");
  unless ($command) {
    warn "[debug]$me router custom field '${field_prefix}_cmd_$action' "
      . "is not defined." if $DEBUG;
    return '';
  }

  if ($command =~ /\[\@--/) { # Use Text::Template

    my $template_data = {};

    if ($action eq 'replace') {
      $template_data->{"old_$_"} = $old->getfield($_) foreach $old->fields;
      $template_data->{"new_$_"} = $svc_broadband->getfield($_)
        foreach $svc_broadband->fields;
    } else {
      $template_data->{$_} = $svc_broadband->getfield($_)
        foreach $svc_broadband->fields;
    }

    my $template = new Text::Template (
      TYPE => 'STRING',
      SOURCE => $command,
      DELIMITERS => [ '[@--', '--@]' ],
    ) or return "Unable to construct template for router command: "
                . $Text::Template::ERROR;

    $command = $template->fill_in(
      HASH => $template_data,
      BROKEN_ARG => \$error,
      BROKEN => sub {
        my %bargs = @_;
        my $err = $bargs{'arg'};
        $$err = $bargs{'error'};
        return undef;
      },
    );

    if (not defined $command or $error) {
      $error ||= $Text::Template::ERROR;
      return "Unable to fill-in template for router command: $error";
    }

  } else { # Use eval
    no strict 'vars';
    no strict 'refs';

    if ($action eq 'replace') {
      ${"old_$_"} = $old->getfield($_) foreach $old->fields;
      ${"new_$_"} = $svc_broadband->getfield($_) foreach $svc_broadband->fields;
      $command = eval(qq("$command"));
    } else {
      ${$_} = $svc_broadband->getfield($_) foreach $svc_broadband->fields;
      $command = eval(qq("$command"));
    }
    return $@ if $@;
  }

  my $args = [
    'user' => $router->getfield($field_prefix . '_user'),
    'password' => $router->getfield($field_prefix . '_password'),
    'host' => $router->getfield($field_prefix . '_address'),
    'Timeout' => $router->getfield($field_prefix . '_timeout'),
    'Prompt' => $router->getfield($field_prefix . '_prompt'),
    'command' => $command,
  ];

  my $error_check = $router->getfield("${field_prefix}_cmd_${action}_error");
  push(@$args, ('error_check' => $error_check)) if ($error_check);

  return('', $args);

}

sub _get_cmd_sub {

  my ($self, $svc_broadband, $router) = (shift, shift, shift);

  my $protocol = (
    $router->getfield($self->_field_prefix . '_protocol') =~ /^(telnet|ssh)$/
  ) ? $1 : 'telnet';

  return(ref($self)."::".$protocol."_cmd");

}

sub _check_router_fields {

  my ($self, $router, $action) = (shift, shift, shift);
  my @check_fields = $self->_req_router_fields;

  foreach (@check_fields) {
    if ($router->getfield($_) eq '') {
      warn "[debug]$me Required field '$_' is unset" if $DEBUG;
      return 0;
    } else {
      return 1;
    }
  }

}

sub _queue {
  my( $self, $svcnum, $cmd_sub ) = (shift, shift, shift);
  my $queue = new FS::queue {
    'svcnum' => $svcnum,
  };
  $queue->job($cmd_sub);
  $queue->insert(@_);
}

sub _get_router {
  my ($self, $svc_broadband, %args) = (shift, shift, @_);

  my $router;
  if ($args{'routernum'}) {
    $router = qsearchs('router', { routernum => $args{'routernum'}});
  } else {
    $router = $svc_broadband->router;
  }

  return($router);

}


# Subroutines
sub ssh_cmd {
  my %arg = @_;

  eval 'use Net::SSH \'0.08\'';
  die $@ if $@;

  my @out = &Net::SSH::ssh_cmd( { @_ } );
  my $error = &_cmd_error_check(\%arg, \@out);

  die ("Error while processing ssh command: $error") if $error;

  return '';

}

sub telnet_cmd {
  my %arg = @_;

  eval 'use Net::Telnet';
  die $@ if $@;

  my $t = new Net::Telnet (Timeout => $arg{'Timeout'},
                           Prompt  => $arg{'Prompt'});
  $t->open($arg{'host'});
  $t->login($arg{'user'}, $arg{'password'});
  my @out  = $t->cmd($arg{'command'});
  my $error = &_cmd_error_check(\%arg, \@out);

  die ("Error while processing telnet command: $error") if $error;

  return '';

}

sub _cmd_error_check {
  my ($arg, $out) = (shift, shift);

  die "_cmd_error_check called without proper arguments"
    unless (ref($arg) eq 'HASH' and ref($out) eq 'ARRAY');

  unless (exists($arg->{'error_check'}) and $arg->{'error_check'} ne '') {
    #Preserve default behaviour and return output if a check isn't defined.
    warn "Output from router command: " . join('', @$out) if $DEBUG;
    return '';
  }

  my $error_check = $arg->{'error_check'};
  foreach (@$out) {
    return $_ if /$error_check/;
  }

  return '';

}

1;