initial checkin of module files for proper perl installation
[freeside.git] / fs_signup / fs_signup_server
1 #!/usr/bin/perl -Tw
2 #
3 # fs_signup_server
4 #
5
6 use strict;
7 use IO::Handle;
8 use FS::SSH qw(sshopen2);
9 use FS::UID qw(adminsuidsetup);
10 use FS::Record qw(qsearchs);
11 use FS::cust_main_county;
12 use FS::cust_main;
13
14 use vars qw( $opt );
15
16 my $user = shift or die &usage;
17 adminsuidsetup $user; 
18
19 my $machine = shift or die &usage;
20
21 my $agentnum = shift or die &usage;
22 my $agent = qsearchs( 'agent', { 'agentnum' => $agentnum } ) or die &usage;
23 #my %part_pkg = %{ $agent->pkgpart_hashref };
24
25 my $refnum = shift or die &usage;
26
27 $SIG{CHLD} = sub { wait() };
28
29 my($fs_signupd)="/usr/local/sbin/fs_signupd";
30
31 while (1) {
32   my($reader,$writer)=(new IO::Handle, new IO::Handle);
33   $writer->autoflush(1);
34   sshopen2($shellmachine,$reader,$writer,$fs_signupd);
35
36   #send fs_signupd state/county/country
37   @cust_main_county = qsearch('cust_main_county', {} );
38   print $writer join("\n",
39     scalar(@cust_main_county),
40     map {
41       $_->taxnum,
42       $_->state,
43       $_->county,
44       $_->country,
45     } @cust_main_county
46   ),"\n";
47
48   #send fs_signupd package definitions
49
50   #send fs_signupd POPs
51
52
53
54   while (1) {
55     my( $first, $last, $ss, $company, $address1, $address2, $city, $county,
56         $state, $zip, $country, $daytime, $night, $fax, $payby, $payinfo,
57         $paydate, $payname, $username, $password, $popnum,
58     );
59
60
61
62     my($username,$old_password,$new_password,$new_gecos,$new_shell);
63     defined($username=<$reader>) or last;
64     defined($old_password=<$reader>) or last; 
65     defined($new_password=<$reader>) or last; 
66     defined($new_gecos=<$reader>) or last; 
67     defined($new_shell=<$reader>) or last; 
68     chop($username);
69     chop($old_password);
70     chop($new_password);
71     chop($new_gecos);
72     chop($new_shell);
73     my($svc_acct);
74
75     #need to try both $old_password and encrypted $old_password
76     #maybe the crypt function in svc_acct.export needs to be a library?
77     my $salt = substr($old_password,0,2);
78     my $cold_password = crypt($old_password,$salt);
79     $svc_acct=qsearchs('svc_acct',{'username'=>$username,
80                                    '_password'=>$old_password,
81     } )
82            || qsearchs('svc_acct',{'username'=>$username,
83                                    '_password'=>$cold_password,
84     } );
85     unless ( $svc_acct ) { print $writer "Incorrect password.\n"; next; }
86
87     my(%hash)=$svc_acct->hash;
88     my($new_svc_acct) = new FS::svc_acct ( \%hash );
89     $new_svc_acct->setfield('_password',$new_password) 
90       if $new_password && $new_password ne $old_password;
91     $new_svc_acct->setfield('finger',$new_gecos) if $new_gecos;
92     $new_svc_acct->setfield('shell',$new_shell) if $new_shell;
93     my($error)=$new_svc_acct->replace($svc_acct);
94     print $writer $error,"\n";
95   }
96   close $writer;
97   close $reader;
98   sleep 60;
99   warn "Connection to $shellmachine lost!  Reconnecting...\n";
100 }
101
102 sub usage {
103   die "Usage:\n\n  fs_signup_server user machine agentnum refnum\n";
104 }
105