web-based password changer!
[freeside.git] / fs_passwd / fs_passwd_server
1 #!/usr/bin/perl -Tw
2 #
3 # fs_passwd_server
4 #
5 # portions of this script are copied from the `passwd' script in the original
6 # (perl 4) camel book, now archived at 
7 # http://www.perl.com/CPAN/scripts/nutshell/ch6/passwd
8 #
9 # ivan@sisd.com 98-mar-9
10 #
11 # crypt-aware, s/password/_password/; ivan@sisd.com 98-aug-23
12
13 use strict;
14 use IO::Handle;
15 use Net::SSH qw(sshopen2);
16 use FS::UID qw(adminsuidsetup);
17 use FS::Record qw(qsearchs);
18 use FS::svc_acct;
19
20 my $user = shift or die &usage;
21 adminsuidsetup $user; 
22
23 my($shellmachine)=shift or die &usage;
24
25 #causing trouble for some folks
26 #$SIG{CHLD} = sub { wait() };
27
28 my($fs_passwdd)="/usr/local/sbin/fs_passwdd";
29
30 while (1) {
31   my($reader,$writer)=(new IO::Handle, new IO::Handle);
32   $writer->autoflush(1);
33   sshopen2($shellmachine,$reader,$writer,$fs_passwdd);
34   while (1) {
35     my($username,$old_password,$new_password,$new_gecos,$new_shell);
36     defined($username=<$reader>) or last;
37     defined($old_password=<$reader>) or last; 
38     defined($new_password=<$reader>) or last; 
39     defined($new_gecos=<$reader>) or last; 
40     defined($new_shell=<$reader>) or last; 
41     chop($username);
42     chop($old_password);
43     chop($new_password);
44     chop($new_gecos);
45     chop($new_shell);
46     my($svc_acct);
47
48     #need to try both $old_password and encrypted $old_password
49     #maybe the crypt function in svc_acct.export needs to be a library?
50     my $salt = substr($old_password,0,2);
51     my $cold_password = crypt($old_password,$salt);
52     $svc_acct=qsearchs('svc_acct',{'username'=>$username,
53                                    '_password'=>$old_password,
54     } )
55            || qsearchs('svc_acct',{'username'=>$username,
56                                    '_password'=>$cold_password,
57     } );
58     unless ( $svc_acct ) { print $writer "Incorrect password.\n"; next; }
59
60     my(%hash)=$svc_acct->hash;
61     my($new_svc_acct) = new FS::svc_acct ( \%hash );
62     $new_svc_acct->setfield('_password',$new_password) 
63       if $new_password && $new_password ne $old_password;
64     $new_svc_acct->setfield('finger',$new_gecos) if $new_gecos;
65     $new_svc_acct->setfield('shell',$new_shell) if $new_shell;
66     my($error)=$new_svc_acct->replace($svc_acct);
67     print $writer $error,"\n";
68   }
69   close $writer;
70   close $reader;
71   sleep 60;
72   warn "Connection to $shellmachine lost!  Reconnecting...\n";
73 }
74
75 sub usage {
76   die "Usage:\n\n  fs_passwd_server user shellmachine\n";
77 }
78