Merge branch 'master' of git.freeside.biz:/home/git/freeside
[freeside.git] / FS / FS / part_export / domain_shellcommands.pm
1 package FS::part_export::domain_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_domain',
25   'desc'    => 'Run remote commands via SSH, for domains (qmail, ISPMan).',
26   'options' => \%options,
27   'notes'   => <<'END'
28 Run remote commands via SSH, for domains.  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="qmail catchall .qmail-domain-default maintenance" onClick='
34       this.form.useradd.value = "[ \"$uid\" -a \"$gid\" -a \"$dir\" -a \"$qdomain\" ] && [ -e $dir/.qmail-$qdomain-default ] || { touch $dir/.qmail-$qdomain-default; chown $uid:$gid $dir/.qmail-$qdomain-default; }";
35       this.form.userdel.value = "";
36       this.form.usermod.value = "";
37     '>
38   <LI>
39     <INPUT TYPE="button" VALUE="ISPMan CLI" onClick='
40       this.form.useradd.value = "/usr/local/ispman/bin/ispman.addDomain -d $domain changeme";
41       this.form.userdel.value = "/usr/local/ispman/bin/ispman.deleteDomain -d $domain";
42       this.form.usermod.value = "";
43     '>
44 </UL>
45 The following variables are available for interpolation (prefixed with <code>new_</code> or <code>old_</code> for replace operations):
46 <UL>
47   <LI><code>$domain</code>
48   <LI><code>$qdomain</code> - domain with periods replaced by colons
49   <LI><code>$uid</code> - of catchall account
50   <LI><code>$gid</code> - of catchall account
51   <LI><code>$dir</code> - home directory of catchall account
52   <LI>All other fields in <b>svc_domain</b> 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_domain) = (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_domain->getfield($_) foreach $svc_domain->fields;
79   }
80   ( $qdomain = $domain ) =~ s/\./:/g; #see dot-qmail(5): EXTENSION ADDRESSES
81
82   if ( $svc_domain->catchall ) {
83     no strict 'refs';
84     my $svc_acct = $svc_domain->catchall_svc_acct;
85     ${$_} = $svc_acct->getfield($_) foreach qw(uid gid dir);
86   } else {
87     no strict 'refs';
88     ${$_} = '' foreach qw(uid gid dir);
89   }
90
91   #done setting variables for the command
92
93   $self->shellcommands_queue( $svc_domain->svcnum,
94     user         => $self->option('user')||'root',
95     host         => $self->machine,
96     command      => eval(qq("$command")),
97   );
98 }
99
100 sub _export_replace {
101   my($self, $new, $old ) = (shift, shift, shift);
102   my $command = $self->option('usermod');
103   
104   #set variable for the command
105   no strict 'vars';
106   {
107     no strict 'refs';
108     ${"old_$_"} = $old->getfield($_) foreach $old->fields;
109     ${"new_$_"} = $new->getfield($_) foreach $new->fields;
110   }
111   ( $old_qdomain = $old_domain ) =~ s/\./:/g; #see dot-qmail(5): EXTENSION ADDRESSES
112   ( $new_qdomain = $new_domain ) =~ s/\./:/g; #see dot-qmail(5): EXTENSION ADDRESSES
113
114   { 
115     no strict 'refs';
116
117     if ( $old->catchall ) {
118       my $svc_acct = $old->catchall_svc_acct;
119       ${"old_$_"} = $svc_acct->getfield($_) foreach qw(uid gid dir);
120     } else {
121       ${"old_$_"} = '' foreach qw(uid gid dir);
122     }
123     if ( $new->catchall ) {
124       my $svc_acct = $new->catchall_svc_acct;
125       ${"new_$_"} = $svc_acct->getfield($_) foreach qw(uid gid dir);
126     } else {
127       ${"new_$_"} = '' foreach qw(uid gid dir);
128     }
129
130   }
131
132   #done setting variables for the command
133
134   $self->shellcommands_queue( $new->svcnum,
135     user         => $self->option('user')||'root',
136     host         => $self->machine,
137     command      => eval(qq("$command")),
138   );
139 }
140
141 #a good idea to queue anything that could fail or take any time
142 sub shellcommands_queue {
143   my( $self, $svcnum ) = (shift, shift);
144   my $queue = new FS::queue {
145     'svcnum' => $svcnum,
146     'job'    => "FS::part_export::domain_shellcommands::ssh_cmd",
147   };
148   $queue->insert( @_ );
149 }
150
151 sub ssh_cmd { #subroutine, not method
152   use Net::SSH '0.08';
153   &Net::SSH::ssh_cmd( { @_ } );
154 }
155
156 #sub shellcommands_insert { #subroutine, not method
157 #}
158 #sub shellcommands_replace { #subroutine, not method
159 #}
160 #sub shellcommands_delete { #subroutine, not method
161 #}
162
163 1;
164