1 package FS::part_export::forward_shellcommands;
4 use vars qw(@ISA %info);
8 @ISA = qw(FS::part_export);
10 tie my %options, 'Tie::IxHash',
11 'user' => { label=>'Remote username', default=>'root' },
12 'useradd' => { label=>'Insert command',
15 'userdel' => { label=>'Delete command',
18 'usermod' => { label=>'Modify command',
24 'svc' => 'svc_forward',
25 'desc' => 'Run remote commands via SSH, for forwards',
26 'options' => \%options,
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:
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; }";
39 The following variables are available for interpolation (prefixed with
40 <code>new_</code> or <code>old_</code> for replace operations):
42 <LI><code>$username</code>
43 <LI><code>$domain</code>
44 <LI><code>$destination</code> - forward destination
45 <LI>All other fields in <a href="../docs/schema.html#svc_forward">svc_forward</a> are also available.
50 sub rebless { shift; }
54 $self->_export_command('useradd', @_);
59 $self->_export_command('userdel', @_);
63 my ( $self, $action, $svc_forward ) = (shift, shift, shift);
64 my $command = $self->option($action);
66 #set variable for the command
70 ${$_} = $svc_forward->getfield($_) foreach $svc_forward->fields;
73 my $svc_acct = $svc_forward->srcsvc_acct;
74 $username = $svc_acct->username;
75 $domain = $svc_acct->domain;
76 if ($svc_forward->dstsvc_acct) {
77 $destination = $svc_forward->dstsvc_acct->email;
79 $destination = $svc_forward->dst;
82 #done setting variables for the command
84 $self->shellcommands_queue( $svc_forward->svcnum,
85 user => $self->option('user')||'root',
86 host => $self->machine,
87 command => eval(qq("$command")),
92 my( $self, $new, $old ) = (shift, shift, shift);
93 my $command = $self->option('usermod');
95 #set variable for the command
99 ${"old_$_"} = $old->getfield($_) foreach $old->fields;
100 ${"new_$_"} = $new->getfield($_) foreach $new->fields;
103 my $old_svc_acct = $old->srcsvc_acct;
104 $old_username = $old_svc_acct->username;
105 $old_domain = $old_svc_acct->domain;
106 if ($old->dstsvc_acct) {
107 $old_destination = $old->dstsvc_acct->email;
109 $old_destination = $old->dst;
112 my $new_svc_acct = $new->srcsvc_acct;
113 $new_username = $new_svc_acct->username;
114 $new_domain = $new_svc_acct->domain;
116 $new_destination = $new->dstsvc_acct->email;
118 $new_destination = $new->dst;
121 #done setting variables for the command
123 $self->shellcommands_queue( $new->svcnum,
124 user => $self->option('user')||'root',
125 host => $self->machine,
126 command => eval(qq("$command")),
130 #a good idea to queue anything that could fail or take any time
131 sub shellcommands_queue {
132 my( $self, $svcnum ) = (shift, shift);
133 my $queue = new FS::queue {
135 'job' => "FS::part_export::forward_shellcommands::ssh_cmd",
137 $queue->insert( @_ );
140 sub ssh_cmd { #subroutine, not method
142 &Net::SSH::ssh_cmd( { @_ } );
145 #sub shellcommands_insert { #subroutine, not method
147 #sub shellcommands_replace { #subroutine, not method
149 #sub shellcommands_delete { #subroutine, not method