make $old_domain available too
[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   $old_domain = $old->domain;
69   $new_domain = $new->domain;
70   $new_crypt_password = ''; #surpress "used only once" warnings
71   $new_crypt_password = crypt( $new->_password,
72                                $saltset[int(rand(64))].$saltset[int(rand(64))]);
73   $self->shellcommands_queue( $new->svcnum,
74     user         => $self->option('user')||'root',
75     host         => $self->machine,
76     command      => eval(qq("$command")),
77     stdin_string => eval(qq("$stdin")),
78   );
79 }
80
81 #a good idea to queue anything that could fail or take any time
82 sub shellcommands_queue {
83   my( $self, $svcnum ) = (shift, shift);
84   my $queue = new FS::queue {
85     'svcnum' => $svcnum,
86     'job'    => "FS::part_export::shellcommands::ssh_cmd",
87   };
88   $queue->insert( @_ );
89 }
90
91 sub ssh_cmd { #subroutine, not method
92   use Net::SSH '0.07';
93   &Net::SSH::ssh_cmd( { @_ } );
94 }
95
96 #sub shellcommands_insert { #subroutine, not method
97 #}
98 #sub shellcommands_replace { #subroutine, not method
99 #}
100 #sub shellcommands_delete { #subroutine, not method
101 #}
102