da24e5ca757c550649dd576f1797920c2b526f50
[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
45   my $cust_pkg = $svc_acct->cust_svc->cust_pkg;
46   if ( $cust_pkg ) {
47     $email = ( grep { $_ ne 'POST' } $cust_pkg->cust_main->invoicing_list )[0];
48   } else {
49     $email = '';
50   }
51
52   $finger = shell_quote $finger;
53   $quoted_password = shell_quote $_password;
54   $domain = $svc_acct->domain;
55   $crypt_password = ''; #surpress "used only once" warnings
56   $crypt_password = crypt( $svc_acct->_password,
57                              $saltset[int(rand(64))].$saltset[int(rand(64))] );
58
59   $self->shellcommands_queue( $svc_acct->svcnum,
60     user         => $self->option('user')||'root',
61     host         => $self->machine,
62     command      => eval(qq("$command")),
63     stdin_string => eval(qq("$stdin")),
64   );
65 }
66
67 sub _export_replace {
68   my($self, $new, $old ) = (shift, shift, shift);
69   my $command = $self->option('usermod');
70   my $stdin = $self->option('usermod_stdin');
71   no strict 'vars';
72   {
73     no strict 'refs';
74     ${"old_$_"} = $old->getfield($_) foreach $old->fields;
75     ${"new_$_"} = $new->getfield($_) foreach $new->fields;
76   }
77   $new_finger = shell_quote $new_finger;
78   $quoted_new__password = shell_quote $new__password; #old, wrong?
79   $new_quoted_password = shell_quote $new__password; #new, better?
80   $old_domain = $old->domain;
81   $new_domain = $new->domain;
82   $new_crypt_password = ''; #surpress "used only once" warnings
83   $new_crypt_password = crypt( $new->_password,
84                                $saltset[int(rand(64))].$saltset[int(rand(64))]);
85   if ( $self->option('usermod_pwonly') ) {
86     my $error = '';
87     if ( $old_username ne $new_username ) {
88       $error ||= "can't change username";
89     }
90     if ( $old_domain ne $new_domain ) {
91       $error ||= "can't change domain";
92     }
93     if ( $old_uid != $new_uid ) {
94       $error ||= "can't change uid";
95     }
96     if ( $old_dir ne $new_dir ) {
97       $error ||= "can't change dir";
98     }
99     return $error. ' ('. $self->exporttype. ' to '. $self->machine. ')'
100       if $error;
101   }
102   $self->shellcommands_queue( $new->svcnum,
103     user         => $self->option('user')||'root',
104     host         => $self->machine,
105     command      => eval(qq("$command")),
106     stdin_string => eval(qq("$stdin")),
107   );
108 }
109
110 #a good idea to queue anything that could fail or take any time
111 sub shellcommands_queue {
112   my( $self, $svcnum ) = (shift, shift);
113   my $queue = new FS::queue {
114     'svcnum' => $svcnum,
115     'job'    => "FS::part_export::shellcommands::ssh_cmd",
116   };
117   $queue->insert( @_ );
118 }
119
120 sub ssh_cmd { #subroutine, not method
121   use Net::SSH '0.07';
122   &Net::SSH::ssh_cmd( { @_ } );
123 }
124
125 #sub shellcommands_insert { #subroutine, not method
126 #}
127 #sub shellcommands_replace { #subroutine, not method
128 #}
129 #sub shellcommands_delete { #subroutine, not method
130 #}
131