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