RT# 83450 - fixed rateplan export
[freeside.git] / FS / FS / part_export / forward_shellcommands.pm
1 package FS::part_export::forward_shellcommands;
2
3 use strict;
4 use vars qw(@ISA %info);
5 use Tie::IxHash;
6 use FS::part_export;
7
8 @ISA = qw(FS::part_export);
9
10 tie my %options, 'Tie::IxHash',
11   'user' => { label=>'Remote username', default=>'root' },
12   'useradd' => { label=>'Insert command',
13                  default=>'',
14                },
15   'userdel'  => { label=>'Delete command',
16                   default=>'',
17                 },
18   'usermod'  => { label=>'Modify command',
19                   default=>'',
20                 },
21 ;
22
23 %info = (
24   'svc'     => 'svc_forward',
25   'desc'    => 'Run remote commands via SSH, for forwards',
26   'options' => \%options,
27   'notes'   => <<'END'
28 Run remote commands via SSH, for forwards.  You will need to
29 <a href="http://www.freeside.biz/mediawiki/index.php/Freeside:1.9:Documentation:Administration:SSH_Keys">setup SSH for unattended operation</a>.
30 <BR><BR>Use these buttons for some useful presets:
31 <UL>
32   <LI>
33     <INPUT TYPE="button" VALUE="text vpopmail maintenance" onClick='
34       this.form.useradd.value = "[ -d /home/vpopmail/domains/$domain/$username ] && { echo \"$destination\" > /home/vpopmail/domains/$domain/$username/.qmail; chown vpopmail:vchkpw /home/vpopmail/domains/$domain/$username/.qmail; }";
35       this.form.userdel.value = "rm /home/vpopmail/domains/$domain/$username/.qmail";
36       this.form.usermod.value = "mv /home/vpopmail/domains/$old_domain/$old_username/.qmail /home/vpopmail/domains/$new_domain/$new_username; [ \"$old_destination\" != \"$new_destination\" ] && { echo \"$new_destination\" > /home/vpopmail/domains/$new_domain/$new_username/.qmail; chown vpopmail:vchkpw /home/vpopmail/domains/$new_domain/$new_username/.qmail; }";
37     '>
38   <LI>
39     <INPUT TYPE="button" VALUE="ISPMan CLI" onClick='
40       this.form.useradd.value = "";
41       this.form.userdel.value = "";
42       this.form.usermod.value = "";
43     '>
44 </UL>
45 The following variables are available for interpolation (prefixed with
46 <code>new_</code> or <code>old_</code> for replace operations):
47 <UL>
48   <LI><code>$username</code> - username of forward source
49   <LI><code>$domain</code> - domain of forward source
50   <LI><code>$source</code> - forward source ($username@$domain)
51   <LI><code>$destination</code> - forward destination
52   <LI>All other fields in <a href="../docs/schema.html#svc_forward">svc_forward</a> are also available.
53 </UL>
54 END
55 );
56
57 sub rebless { shift; }
58
59 sub _export_insert {
60   my($self) = shift;
61   $self->_export_command('useradd', @_);
62 }
63
64 sub _export_delete {
65   my($self) = shift;
66   $self->_export_command('userdel', @_);
67 }
68
69 sub _export_command {
70   my ( $self, $action, $svc_forward ) = (shift, shift, shift);
71   my $command = $self->option($action);
72   return '' if $command =~ /^\s*$/;
73
74   #set variable for the command
75   no strict 'vars';
76   {
77     no strict 'refs';
78     ${$_} = $svc_forward->getfield($_) foreach $svc_forward->fields;
79   }
80
81   if ( $svc_forward->srcsvc ) {
82     my $srcsvc_acct = $svc_forward->srcsvc_acct;
83     $username = $srcsvc_acct->username;
84     $domain   = $srcsvc_acct->domain;
85     $source   = $srcsvc_acct->email;
86   } else {
87     $source = $svc_forward->src;
88     ( $username, $domain ) = split(/\@/, $source);
89   }
90
91   if ($svc_forward->dstsvc) {
92     $destination = $svc_forward->dstsvc_acct->email;
93   } else {
94     $destination = $svc_forward->dst;
95   }
96
97   #done setting variables for the command
98
99   $self->shellcommands_queue( $svc_forward->svcnum,
100     user         => $self->option('user')||'root',
101     host         => $self->machine,
102     command      => eval(qq("$command")),
103   );
104 }
105
106 sub _export_replace {
107   my( $self, $new, $old ) = (shift, shift, shift);
108   my $command = $self->option('usermod');
109   
110   #set variable for the command
111   no strict 'vars';
112   {
113     no strict 'refs';
114     ${"old_$_"} = $old->getfield($_) foreach $old->fields;
115     ${"new_$_"} = $new->getfield($_) foreach $new->fields;
116   }
117
118   if ( $old->srcsvc ) {
119     my $srcsvc_acct = $old->srcsvc_acct;
120     $old_username = $srcsvc_acct->username;
121     $old_domain   = $srcsvc_acct->domain;
122     $old_source   = $srcsvc_acct->email;
123   } else {
124     $old_source = $old->src;
125     ( $old_username, $old_domain ) = split(/\@/, $old_source);
126   }
127
128   if ( $old->dstsvc ) {
129     $old_destination = $old->dstsvc_acct->email;
130   } else {
131     $old_destination = $old->dst;
132   }
133
134   if ( $new->srcsvc ) {
135     my $srcsvc_acct = $new->srcsvc_acct;
136     $new_username = $srcsvc_acct->username;
137     $new_domain   = $srcsvc_acct->domain;
138     $new_source   = $srcsvc_acct->email;
139   } else {
140     $new_source = $new->src;
141     ( $new_username, $new_domain ) = split(/\@/, $new_source);
142   }
143
144   if ( $new->dstsvc ) {
145     $new_destination = $new->dstsvc_acct->email;
146   } else {
147     $new_destination = $new->dst;
148   }
149
150   #done setting variables for the command
151
152   $self->shellcommands_queue( $new->svcnum,
153     user         => $self->option('user')||'root',
154     host         => $self->machine,
155     command      => eval(qq("$command")),
156   );
157 }
158
159 #a good idea to queue anything that could fail or take any time
160 sub shellcommands_queue {
161   my( $self, $svcnum ) = (shift, shift);
162   my $queue = new FS::queue {
163     'svcnum' => $svcnum,
164     'job'    => "FS::part_export::forward_shellcommands::ssh_cmd",
165   };
166   $queue->insert( @_ );
167 }
168
169 sub ssh_cmd { #subroutine, not method
170   use Net::SSH '0.08';
171   &Net::SSH::ssh_cmd( { @_ } );
172 }
173
174 #sub shellcommands_insert { #subroutine, not method
175 #}
176 #sub shellcommands_replace { #subroutine, not method
177 #}
178 #sub shellcommands_delete { #subroutine, not method
179 #}
180
181 1;
182