56cd569afb74e0ee471b83e52f103cc59ca01f57
[freeside.git] / FS / FS / part_export / shellcommands.pm
1 package FS::part_export::shellcommands;
2
3 use vars qw(@ISA @saltset);
4 use FS::part_export;
5
6 @ISA = qw(FS::part_export);
7
8 @saltset = ( 'a'..'z' , 'A'..'Z' , '0'..'9' , '.' , '/' );
9
10 sub rebless { shift; }
11
12 sub _export_insert {
13   my($self) = shift;
14   $self->_export_command('useradd', @_);
15 }
16
17 sub _export_delete {
18   my($self) = shift;
19   $self->_export_command('userdel', @_);
20 }
21
22 sub _export_command {
23   my ( $self, $action, $svc_acct) = (shift, shift, shift);
24   my $command = $self->option($action);
25   my $stdin = $self->option($action."_stdin");
26   {
27     no strict 'refs';
28     ${$_} = $svc_acct->getfield($_) foreach $svc_acct->fields;
29   }
30   $crypt_password = ''; #surpress "used only once" warnings
31   $crypt_password = crypt( $svc_acct->_password,
32                              $saltset[int(rand(64))].$saltset[int(rand(64))] );
33   $self->shellcommands_queue( $svc_acct->svcnum,
34     user         => $self->option('user')||'root',
35     host         => $self->machine,
36     command      => eval(qq("$command")),
37     stdin_string => eval(qq("$stdin")),
38   );
39 }
40
41 sub _export_replace {
42   my($self, $new, $old ) = (shift, shift, shift);
43   my $command = $self->option('usermod');
44   my $stdin = $self->option('usermod_stdin');
45   {
46     no strict 'refs';
47     ${"old_$_"} = $old->getfield($_) foreach $old->fields;
48     ${"new_$_"} = $new->getfield($_) foreach $new->fields;
49   }
50   $new_crypt_password = ''; #surpress "used only once" warnings
51   $new_crypt_password = crypt( $new->_password,
52                                $saltset[int(rand(64))].$saltset[int(rand(64))]);
53   $self->shellcommands_queue( $new->svcnum,
54     user         => $self->option('user')||'root',
55     host         => $self->machine,
56     command      => eval(qq("$command")),
57     stdin_string => eval(qq("$stdin")),
58   );
59 }
60
61 #a good idea to queue anything that could fail or take any time
62 sub shellcommands_queue {
63   my( $self, $svcnum ) = (shift, shift);
64   my $queue = new FS::queue {
65     'svcnum' => $svcnum,
66     'job'    => "FS::part_export::shellcommands::ssh_cmd",
67   };
68   $queue->insert( @_ );
69 }
70
71 sub ssh_cmd { #subroutine, not method
72   use Net::SSH '0.06';
73   &Net::SSH::ssh_cmd( { @_ } );
74 }
75
76 #sub shellcommands_insert { #subroutine, not method
77 #}
78 #sub shellcommands_replace { #subroutine, not method
79 #}
80 #sub shellcommands_delete { #subroutine, not method
81 #}
82