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