- depend on Net::SSH 0.07, for OpenSSH -T fix
[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_command {
24   my ( $self, $action, $svc_acct) = (shift, shift, shift);
25   my $command = $self->option($action);
26   my $stdin = $self->option($action."_stdin");
27   no strict 'vars';
28   {
29     no strict 'refs';
30     ${$_} = $svc_acct->getfield($_) foreach $svc_acct->fields;
31   }
32   $finger = shell_quote $finger;
33   $crypt_password = ''; #surpress "used only once" warnings
34   $crypt_password = crypt( $svc_acct->_password,
35                              $saltset[int(rand(64))].$saltset[int(rand(64))] );
36   $self->shellcommands_queue( $svc_acct->svcnum,
37     user         => $self->option('user')||'root',
38     host         => $self->machine,
39     command      => eval(qq("$command")),
40     stdin_string => eval(qq("$stdin")),
41   );
42 }
43
44 sub _export_replace {
45   my($self, $new, $old ) = (shift, shift, shift);
46   my $command = $self->option('usermod');
47   my $stdin = $self->option('usermod_stdin');
48   no strict 'vars';
49   {
50     no strict 'refs';
51     ${"old_$_"} = $old->getfield($_) foreach $old->fields;
52     ${"new_$_"} = $new->getfield($_) foreach $new->fields;
53   }
54   $new_finger = shell_quote $new_finger;
55   $new_crypt_password = ''; #surpress "used only once" warnings
56   $new_crypt_password = crypt( $new->_password,
57                                $saltset[int(rand(64))].$saltset[int(rand(64))]);
58   $self->shellcommands_queue( $new->svcnum,
59     user         => $self->option('user')||'root',
60     host         => $self->machine,
61     command      => eval(qq("$command")),
62     stdin_string => eval(qq("$stdin")),
63   );
64 }
65
66 #a good idea to queue anything that could fail or take any time
67 sub shellcommands_queue {
68   my( $self, $svcnum ) = (shift, shift);
69   my $queue = new FS::queue {
70     'svcnum' => $svcnum,
71     'job'    => "FS::part_export::shellcommands::ssh_cmd",
72   };
73   $queue->insert( @_ );
74 }
75
76 sub ssh_cmd { #subroutine, not method
77   use Net::SSH '0.07';
78   &Net::SSH::ssh_cmd( { @_ } );
79 }
80
81 #sub shellcommands_insert { #subroutine, not method
82 #}
83 #sub shellcommands_replace { #subroutine, not method
84 #}
85 #sub shellcommands_delete { #subroutine, not method
86 #}
87