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="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:
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 <INPUT TYPE="button" VALUE="ISPMan CLI" onClick='
40 this.form.useradd.value = "";
41 this.form.userdel.value = "";
42 this.form.usermod.value = "";
45 The following variables are available for interpolation (prefixed with
46 <code>new_</code> or <code>old_</code> for replace operations):
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.
57 sub rebless { shift; }
61 $self->_export_command('useradd', @_);
66 $self->_export_command('userdel', @_);
70 my ( $self, $action, $svc_forward ) = (shift, shift, shift);
71 my $command = $self->option($action);
72 return '' if $command =~ /^\s*$/;
74 #set variable for the command
78 ${$_} = $svc_forward->getfield($_) foreach $svc_forward->fields;
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;
87 $source = $svc_forward->src;
88 ( $username, $domain ) = split(/\@/, $source);
91 if ($svc_forward->dstsvc) {
92 $destination = $svc_forward->dstsvc_acct->email;
94 $destination = $svc_forward->dst;
97 #done setting variables for the command
99 $self->shellcommands_queue( $svc_forward->svcnum,
100 user => $self->option('user')||'root',
101 host => $self->machine,
102 command => eval(qq("$command")),
106 sub _export_replace {
107 my( $self, $new, $old ) = (shift, shift, shift);
108 my $command = $self->option('usermod');
110 #set variable for the command
114 ${"old_$_"} = $old->getfield($_) foreach $old->fields;
115 ${"new_$_"} = $new->getfield($_) foreach $new->fields;
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;
124 $old_source = $old->src;
125 ( $old_username, $old_domain ) = split(/\@/, $old_source);
128 if ( $old->dstsvc ) {
129 $old_destination = $old->dstsvc_acct->email;
131 $old_destination = $old->dst;
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;
140 $new_source = $new->src;
141 ( $new_username, $new_domain ) = split(/\@/, $new_source);
144 if ( $new->dstsvc ) {
145 $new_destination = $new->dstsvc_acct->email;
147 $new_destination = $new->dst;
150 #done setting variables for the command
152 $self->shellcommands_queue( $new->svcnum,
153 user => $self->option('user')||'root',
154 host => $self->machine,
155 command => eval(qq("$command")),
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 {
164 'job' => "FS::part_export::forward_shellcommands::ssh_cmd",
166 $queue->insert( @_ );
169 sub ssh_cmd { #subroutine, not method
171 &Net::SSH::ssh_cmd( { @_ } );
174 #sub shellcommands_insert { #subroutine, not method
176 #sub shellcommands_replace { #subroutine, not method
178 #sub shellcommands_delete { #subroutine, not method