This commit was generated by cvs2svn to compensate for changes in r3921,
[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
72   #set variable for the command
73   no strict 'vars';
74   {
75     no strict 'refs';
76     ${$_} = $svc_forward->getfield($_) foreach $svc_forward->fields;
77   }
78
79   my $svc_acct = $svc_forward->srcsvc_acct;
80   $username = $svc_acct->username;
81   $domain = $svc_acct->domain;
82   if ($svc_forward->dstsvc_acct) {
83     $destination = $svc_forward->dstsvc_acct->email;
84   } else {
85     $destination = $svc_forward->dst;
86   }
87
88   #done setting variables for the command
89
90   $self->shellcommands_queue( $svc_forward->svcnum,
91     user         => $self->option('user')||'root',
92     host         => $self->machine,
93     command      => eval(qq("$command")),
94   );
95 }
96
97 sub _export_replace {
98   my( $self, $new, $old ) = (shift, shift, shift);
99   my $command = $self->option('usermod');
100   
101   #set variable for the command
102   no strict 'vars';
103   {
104     no strict 'refs';
105     ${"old_$_"} = $old->getfield($_) foreach $old->fields;
106     ${"new_$_"} = $new->getfield($_) foreach $new->fields;
107   }
108
109   my $old_svc_acct = $old->srcsvc_acct;
110   $old_username = $old_svc_acct->username;
111   $old_domain = $old_svc_acct->domain;
112   if ($old->dstsvc_acct) {
113     $old_destination = $old->dstsvc_acct->email;
114   } else {
115     $old_destination = $old->dst;
116   }
117
118   my $new_svc_acct = $new->srcsvc_acct;
119   $new_username = $new_svc_acct->username;
120   $new_domain = $new_svc_acct->domain;
121   if ($new->dstsvc) {
122     $new_destination = $new->dstsvc_acct->email;
123   } else {
124     $new_destination = $new->dst;
125   }
126
127   #done setting variables for the command
128
129   $self->shellcommands_queue( $new->svcnum,
130     user         => $self->option('user')||'root',
131     host         => $self->machine,
132     command      => eval(qq("$command")),
133   );
134 }
135
136 #a good idea to queue anything that could fail or take any time
137 sub shellcommands_queue {
138   my( $self, $svcnum ) = (shift, shift);
139   my $queue = new FS::queue {
140     'svcnum' => $svcnum,
141     'job'    => "FS::part_export::forward_shellcommands::ssh_cmd",
142   };
143   $queue->insert( @_ );
144 }
145
146 sub ssh_cmd { #subroutine, not method
147   use Net::SSH '0.08';
148   &Net::SSH::ssh_cmd( { @_ } );
149 }
150
151 #sub shellcommands_insert { #subroutine, not method
152 #}
153 #sub shellcommands_replace { #subroutine, not method
154 #}
155 #sub shellcommands_delete { #subroutine, not method
156 #}
157
158 1;
159