4e943605f07f8af5c8f7d8e39c13611cf1219686
[freeside.git] / FS / FS / part_export / shellcommands.pm
1 package FS::part_export::shellcommands;
2
3 use vars qw(@ISA @saltset);
4 use FS::part_export;
5
6 @ISA = qw(FS::part_export);
7
8 @saltset = ( 'a'..'z' , 'A'..'Z' , '0'..'9' , '.' , '/' );
9
10 sub rebless { shift; }
11
12 sub _export_insert {
13   my($self) = shift;
14   $self->_export_command('useradd', @_);
15 }
16
17 sub _export_delete {
18   my($self) = shift;
19   $self->_export_command('userdel', @_);
20 }
21
22 sub _export_command {
23   my ( $self, $action, $svc_acct) = (shift, shift, shift);
24   my $command = $self->option($action);
25   my $stdin = $self->option($action."_stdin");
26   no strict 'refs';
27   ${$_} = $svc_acct->getfield($_) foreach $svc_acct->fields;
28   $crypt_password = crypt( $svc_acct->_password,
29                            $saltset[int(rand(64))].$saltset[int(rand(64))] );
30   $self->shellcommands_queue( $svc_acct->svcnum,
31     user         => $self->option('user')||'root',
32     host         => $self->machine,
33     command      => eval(qq("$command")),
34     stdin_string => eval(qq("$stdin")),
35   );
36 }
37
38 sub _export_replace {
39   my($self, $new, $old ) = (shift, shift, shift);
40   my $command = $self->option('usermod');
41   my $stdin = $self->option('usermod_stdin');
42   no strict 'refs';
43   ${"old_$_"} = $old->getfield($_) foreach $old->fields;
44   ${"new_$_"} = $new->getfield($_) foreach $new->fields;
45   $new_crypt_password = crypt( $svc_acct->_password,
46                                $saltset[int(rand(64))].$saltset[int(rand(64))]);
47   $self->shellcommands_queue( $new->svcnum,
48     user         => $self->option('user')||'root',
49     host         => $self->machine,
50     command      => eval(qq("$command")),
51     stdin_string => eval(qq("$stdin")),
52   );
53 }
54
55 #a good idea to queue anything that could fail or take any time
56 sub shellcommands_queue {
57   my( $self, $svcnum ) = (shift, shift);
58   my $queue = new FS::queue {
59     'svcnum' => $svcnum,
60     'job'    => "FS::part_export::shellcommands::ssh_cmd",
61   };
62   $queue->insert( @_ );
63 }
64
65 sub ssh_cmd { #subroutine, not method
66   use Net::SSH '0.06';
67   &Net::SSH::ssh_cmd( { @_ } );
68 }
69
70 #sub shellcommands_insert { #subroutine, not method
71 #}
72 #sub shellcommands_replace { #subroutine, not method
73 #}
74 #sub shellcommands_delete { #subroutine, not method
75 #}
76