3d3e88ea4da781c2a19bcd536f1989b18659a608
[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   $email = ( grep { $_ ne 'POST' }
43              $svc_acct->cust_svc->cust_pkg->cust_main->invoicing_list
44              )[0];
45   $finger = shell_quote $finger;
46   $quoted_password = shell_quote $_password;
47   $domain = $svc_acct->domain;
48   $crypt_password = ''; #surpress "used only once" warnings
49   $crypt_password = crypt( $svc_acct->_password,
50                              $saltset[int(rand(64))].$saltset[int(rand(64))] );
51   $self->shellcommands_queue( $svc_acct->svcnum,
52     user         => $self->option('user')||'root',
53     host         => $self->machine,
54     command      => eval(qq("$command")),
55     stdin_string => eval(qq("$stdin")),
56   );
57 }
58
59 sub _export_replace {
60   my($self, $new, $old ) = (shift, shift, shift);
61   my $command = $self->option('usermod');
62   my $stdin = $self->option('usermod_stdin');
63   no strict 'vars';
64   {
65     no strict 'refs';
66     ${"old_$_"} = $old->getfield($_) foreach $old->fields;
67     ${"new_$_"} = $new->getfield($_) foreach $new->fields;
68   }
69   $new_finger = shell_quote $new_finger;
70   $quoted_new__password = shell_quote $new__password;
71   $old_domain = $old->domain;
72   $new_domain = $new->domain;
73   $new_crypt_password = ''; #surpress "used only once" warnings
74   $new_crypt_password = crypt( $new->_password,
75                                $saltset[int(rand(64))].$saltset[int(rand(64))]);
76   $self->shellcommands_queue( $new->svcnum,
77     user         => $self->option('user')||'root',
78     host         => $self->machine,
79     command      => eval(qq("$command")),
80     stdin_string => eval(qq("$stdin")),
81   );
82 }
83
84 #a good idea to queue anything that could fail or take any time
85 sub shellcommands_queue {
86   my( $self, $svcnum ) = (shift, shift);
87   my $queue = new FS::queue {
88     'svcnum' => $svcnum,
89     'job'    => "FS::part_export::shellcommands::ssh_cmd",
90   };
91   $queue->insert( @_ );
92 }
93
94 sub ssh_cmd { #subroutine, not method
95   use Net::SSH '0.07';
96   &Net::SSH::ssh_cmd( { @_ } );
97 }
98
99 #sub shellcommands_insert { #subroutine, not method
100 #}
101 #sub shellcommands_replace { #subroutine, not method
102 #}
103 #sub shellcommands_delete { #subroutine, not method
104 #}
105