1eb0d8367440ce1d25ddbf6ababca927cc6eb9ec
[freeside.git] / FS / FS / part_export / shellcommands.pm
1 package FS::part_export::shellcommands;
2
3 use vars qw(@ISA @saltset);
4 use String::ShellQuote;
5 use FS::part_export;
6
7 @ISA = qw(FS::part_export);
8
9 @saltset = ( 'a'..'z' , 'A'..'Z' , '0'..'9' , '.' , '/' );
10
11 sub rebless { shift; }
12
13 sub _export_insert {
14   my($self) = shift;
15   $self->_export_command('useradd', @_);
16 }
17
18 sub _export_delete {
19   my($self) = shift;
20   $self->_export_command('userdel', @_);
21 }
22
23 sub _export_suspend {
24   my($self) = shift;
25   $self->_export_command('suspend', @_);
26 }
27
28 sub _export_unsuspend {
29   my($self) = shift;
30   $self->_export_command('unsuspend', @_);
31 }
32
33 sub _export_command {
34   my ( $self, $action, $svc_acct) = (shift, shift, shift);
35   my $command = $self->option($action);
36   return '' if $command =~ /^\s*$/;
37   my $stdin = $self->option($action."_stdin");
38
39   no strict 'vars';
40   {
41     no strict 'refs';
42     ${$_} = $svc_acct->getfield($_) foreach $svc_acct->fields;
43
44     my $count = 1;
45     foreach my $acct_snarf ( $svc_acct->acct_snarf ) {
46       ${"snarf_$_$count"} = shell_quote( $acct_snarf->get($_) )
47         foreach qw( machine username _password );
48       $count++;
49     }
50   }
51
52   my $cust_pkg = $svc_acct->cust_svc->cust_pkg;
53   if ( $cust_pkg ) {
54     $email = ( grep { $_ ne 'POST' } $cust_pkg->cust_main->invoicing_list )[0];
55   } else {
56     $email = '';
57   }
58
59   $finger = shell_quote $finger;
60   $quoted_password = shell_quote $_password;
61   $domain = $svc_acct->domain;
62
63   #eventually should check a "password-encoding" field
64   if ( length($svc_acct->_password) == 13
65        || $svc_acct->_password =~ /^\$(1|2a?)\$/ ) {
66     $crypt_password = $svc_acct->_password;
67   } else {
68     $crypt_password = crypt(
69       $svc_acct->_password,
70       $saltset[int(rand(64))].$saltset[int(rand(64))]
71     );
72   }
73
74   $self->shellcommands_queue( $svc_acct->svcnum,
75     user         => $self->option('user')||'root',
76     host         => $self->machine,
77     command      => eval(qq("$command")),
78     stdin_string => eval(qq("$stdin")),
79   );
80 }
81
82 sub _export_replace {
83   my($self, $new, $old ) = (shift, shift, shift);
84   my $command = $self->option('usermod');
85   my $stdin = $self->option('usermod_stdin');
86   no strict 'vars';
87   {
88     no strict 'refs';
89     ${"old_$_"} = $old->getfield($_) foreach $old->fields;
90     ${"new_$_"} = $new->getfield($_) foreach $new->fields;
91   }
92   $new_finger = shell_quote $new_finger;
93   $quoted_new__password = shell_quote $new__password; #old, wrong?
94   $new_quoted_password = shell_quote $new__password; #new, better?
95   $old_domain = $old->domain;
96   $new_domain = $new->domain;
97
98   #eventuall should check a "password-encoding" field
99   if ( length($new->_password) == 13
100        || $new->_password =~ /^\$(1|2a?)\$/ ) {
101     $new_crypt_password = $new->_password;
102   } else {
103     $new_crypt_password =
104       crypt( $new->_password, $saltset[int(rand(64))].$saltset[int(rand(64))]
105     );
106   }
107
108   if ( $self->option('usermod_pwonly') ) {
109     my $error = '';
110     if ( $old_username ne $new_username ) {
111       $error ||= "can't change username";
112     }
113     if ( $old_domain ne $new_domain ) {
114       $error ||= "can't change domain";
115     }
116     if ( $old_uid != $new_uid ) {
117       $error ||= "can't change uid";
118     }
119     if ( $old_dir ne $new_dir ) {
120       $error ||= "can't change dir";
121     }
122     return $error. ' ('. $self->exporttype. ' to '. $self->machine. ')'
123       if $error;
124   }
125   $self->shellcommands_queue( $new->svcnum,
126     user         => $self->option('user')||'root',
127     host         => $self->machine,
128     command      => eval(qq("$command")),
129     stdin_string => eval(qq("$stdin")),
130   );
131 }
132
133 #a good idea to queue anything that could fail or take any time
134 sub shellcommands_queue {
135   my( $self, $svcnum ) = (shift, shift);
136   my $queue = new FS::queue {
137     'svcnum' => $svcnum,
138     'job'    => "FS::part_export::shellcommands::ssh_cmd",
139   };
140   $queue->insert( @_ );
141 }
142
143 sub ssh_cmd { #subroutine, not method
144   use Net::SSH '0.08';
145   &Net::SSH::ssh_cmd( { @_ } );
146 }
147
148 #sub shellcommands_insert { #subroutine, not method
149 #}
150 #sub shellcommands_replace { #subroutine, not method
151 #}
152 #sub shellcommands_delete { #subroutine, not method
153 #}
154