don't try to run blank commands for non-svc_acct shellcommand exports too
[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="../docs/ssh.html">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>
49   <LI><code>$domain</code>
50   <LI><code>$destination</code> - forward destination
51   <LI>All other fields in <a href="../docs/schema.html#svc_forward">svc_forward</a> are also available.
52 </UL>
53 END
54 );
55
56 sub rebless { shift; }
57
58 sub _export_insert {
59   my($self) = shift;
60   $self->_export_command('useradd', @_);
61 }
62
63 sub _export_delete {
64   my($self) = shift;
65   $self->_export_command('userdel', @_);
66 }
67
68 sub _export_command {
69   my ( $self, $action, $svc_forward ) = (shift, shift, shift);
70   my $command = $self->option($action);
71   return '' if $command =~ /^\s*$/;
72
73   #set variable for the command
74   no strict 'vars';
75   {
76     no strict 'refs';
77     ${$_} = $svc_forward->getfield($_) foreach $svc_forward->fields;
78   }
79
80   my $svc_acct = $svc_forward->srcsvc_acct;
81   $username = $svc_acct->username;
82   $domain = $svc_acct->domain;
83   if ($svc_forward->dstsvc_acct) {
84     $destination = $svc_forward->dstsvc_acct->email;
85   } else {
86     $destination = $svc_forward->dst;
87   }
88
89   #done setting variables for the command
90
91   $self->shellcommands_queue( $svc_forward->svcnum,
92     user         => $self->option('user')||'root',
93     host         => $self->machine,
94     command      => eval(qq("$command")),
95   );
96 }
97
98 sub _export_replace {
99   my( $self, $new, $old ) = (shift, shift, shift);
100   my $command = $self->option('usermod');
101   
102   #set variable for the command
103   no strict 'vars';
104   {
105     no strict 'refs';
106     ${"old_$_"} = $old->getfield($_) foreach $old->fields;
107     ${"new_$_"} = $new->getfield($_) foreach $new->fields;
108   }
109
110   my $old_svc_acct = $old->srcsvc_acct;
111   $old_username = $old_svc_acct->username;
112   $old_domain = $old_svc_acct->domain;
113   if ($old->dstsvc_acct) {
114     $old_destination = $old->dstsvc_acct->email;
115   } else {
116     $old_destination = $old->dst;
117   }
118
119   my $new_svc_acct = $new->srcsvc_acct;
120   $new_username = $new_svc_acct->username;
121   $new_domain = $new_svc_acct->domain;
122   if ($new->dstsvc) {
123     $new_destination = $new->dstsvc_acct->email;
124   } else {
125     $new_destination = $new->dst;
126   }
127
128   #done setting variables for the command
129
130   $self->shellcommands_queue( $new->svcnum,
131     user         => $self->option('user')||'root',
132     host         => $self->machine,
133     command      => eval(qq("$command")),
134   );
135 }
136
137 #a good idea to queue anything that could fail or take any time
138 sub shellcommands_queue {
139   my( $self, $svcnum ) = (shift, shift);
140   my $queue = new FS::queue {
141     'svcnum' => $svcnum,
142     'job'    => "FS::part_export::forward_shellcommands::ssh_cmd",
143   };
144   $queue->insert( @_ );
145 }
146
147 sub ssh_cmd { #subroutine, not method
148   use Net::SSH '0.08';
149   &Net::SSH::ssh_cmd( { @_ } );
150 }
151
152 #sub shellcommands_insert { #subroutine, not method
153 #}
154 #sub shellcommands_replace { #subroutine, not method
155 #}
156 #sub shellcommands_delete { #subroutine, not method
157 #}
158
159 1;
160