enable shellcommands suspension/unsuspension hooks
[freeside.git] / FS / FS / part_export / shellcommands.pm
1 package FS::part_export::shellcommands;
2
3 use vars qw(@ISA @saltset);
4 use String::ShellQuote;
5 use FS::part_export;
6
7 @ISA = qw(FS::part_export);
8
9 @saltset = ( 'a'..'z' , 'A'..'Z' , '0'..'9' , '.' , '/' );
10
11 sub rebless { shift; }
12
13 sub _export_insert {
14   my($self) = shift;
15   $self->_export_command('useradd', @_);
16 }
17
18 sub _export_delete {
19   my($self) = shift;
20   $self->_export_command('userdel', @_);
21 }
22
23 sub _export_suspend {
24   my($self) = shift;
25   $self->_export_command('suspend', @_);
26 }
27
28 sub _export_unsuspend {
29   my($self) = shift;
30   $self->_export_command('unsuspend', @_);
31 }
32
33 sub _export_command {
34   my ( $self, $action, $svc_acct) = (shift, shift, shift);
35   my $command = $self->option($action);
36   my $stdin = $self->option($action."_stdin");
37   no strict 'vars';
38   {
39     no strict 'refs';
40     ${$_} = $svc_acct->getfield($_) foreach $svc_acct->fields;
41   }
42   $finger = shell_quote $finger;
43   $quoted_password = shell_quote $_password;
44   $domain = $svc_acct->domain;
45   $crypt_password = ''; #surpress "used only once" warnings
46   $crypt_password = crypt( $svc_acct->_password,
47                              $saltset[int(rand(64))].$saltset[int(rand(64))] );
48   $self->shellcommands_queue( $svc_acct->svcnum,
49     user         => $self->option('user')||'root',
50     host         => $self->machine,
51     command      => eval(qq("$command")),
52     stdin_string => eval(qq("$stdin")),
53   );
54 }
55
56 sub _export_replace {
57   my($self, $new, $old ) = (shift, shift, shift);
58   my $command = $self->option('usermod');
59   my $stdin = $self->option('usermod_stdin');
60   no strict 'vars';
61   {
62     no strict 'refs';
63     ${"old_$_"} = $old->getfield($_) foreach $old->fields;
64     ${"new_$_"} = $new->getfield($_) foreach $new->fields;
65   }
66   $new_finger = shell_quote $new_finger;
67   $quoted_new__password = shell_quote $new__password;
68   $new_domain = $new->domain;
69   $new_crypt_password = ''; #surpress "used only once" warnings
70   $new_crypt_password = crypt( $new->_password,
71                                $saltset[int(rand(64))].$saltset[int(rand(64))]);
72   $self->shellcommands_queue( $new->svcnum,
73     user         => $self->option('user')||'root',
74     host         => $self->machine,
75     command      => eval(qq("$command")),
76     stdin_string => eval(qq("$stdin")),
77   );
78 }
79
80 #a good idea to queue anything that could fail or take any time
81 sub shellcommands_queue {
82   my( $self, $svcnum ) = (shift, shift);
83   my $queue = new FS::queue {
84     'svcnum' => $svcnum,
85     'job'    => "FS::part_export::shellcommands::ssh_cmd",
86   };
87   $queue->insert( @_ );
88 }
89
90 sub ssh_cmd { #subroutine, not method
91   use Net::SSH '0.07';
92   &Net::SSH::ssh_cmd( { @_ } );
93 }
94
95 #sub shellcommands_insert { #subroutine, not method
96 #}
97 #sub shellcommands_replace { #subroutine, not method
98 #}
99 #sub shellcommands_delete { #subroutine, not method
100 #}
101