RT 83251 - moved script
[freeside.git] / fs_passwd / fs_passwd
1 #!/usr/bin/perl -Tw
2 #
3 # fs_passwd
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-8
10 #
11 # password lengths 0,255 instead of 6,8 - we'll let the server process
12 # check the data ivan@sisd.com 98-jul-17
13 #
14 # updated for the exciting new world of self-service 2004-mar-10
15
16 use strict;
17 use Getopt::Std;
18 use FS::SelfService qw(passwd);
19 use vars qw($opt_f $opt_s);
20
21 my($freeside_uid)=scalar(getpwnam('freeside'));
22
23 $ENV{'PATH'} ='/usr/local/bin:/usr/bin:/usr/ucb:/bin';
24 $ENV{'SHELL'} = '/bin/sh';
25 $ENV{'IFS'} = " \t\n";
26 $ENV{'CDPATH'} = '';
27 $ENV{'ENV'} = '';
28 $ENV{'BASH_ENV'} = '';
29
30 $SIG{__DIE__}= sub { system '/bin/stty', 'echo'; };
31
32 die "passwd program isn't running setuid to freeside\n" if $> != $freeside_uid;
33
34 unshift @ARGV, "-f" if $0 =~ /chfn$/;
35 unshift @ARGV, "-s" if $0 =~ /chsh$/;
36
37 getopts('fs');
38
39 my($me)='';
40 if ( $_ = shift(@ARGV) ) {
41   /^(\w{2,8})$/;
42   $me = $1; 
43 }
44 die "You can't change the password for $me." if $me && $<;
45 $me = (getpwuid($<))[0] unless $me;
46
47 my($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell)=
48   getpwnam $me;
49
50 my($old_password,$new_password,$new_gecos,$new_shell);
51
52 if ( $opt_f || $opt_s ) {
53   system '/bin/stty', '-echo';
54   print "Password:";
55   $old_password=<STDIN>;
56   system '/bin/stty', 'echo'; 
57   chop($old_password);
58   #$old_password =~ /^(.{6,8})$/ or die "\nIllegal password.\n";
59   $old_password =~ /^(.{0,255})$/ or die "\nIllegal password.\n";
60   $old_password = $1;
61
62   $new_password = '';
63
64   if ( $opt_f ) {
65     print "\nChanging gecos for $me.\n";
66     print "Gecos [", $gcos, "]: ";
67     $new_gecos=<STDIN>;
68     chop($new_gecos);
69     $new_gecos ||= $gcos;
70     $new_gecos =~ /^(.{0,255})$/ or die "\nIllegal gecos.\n";
71   } else {
72     $new_gecos = '';
73   } 
74
75   if ( $opt_s ) {
76     print "\nChanging shell for $me.\n";
77     print "Shell [", $shell, "]: ";
78     $new_shell=<STDIN>;
79     chop($new_shell);
80     $new_shell ||= $shell;
81     $new_shell =~ /^(.{0,255})$/ or die "\nIllegal shell.\n";
82   } else {
83     $new_shell = '';
84   }
85
86 } else {
87
88   print "Changing password for $me.\n";
89   print "Old password:";
90   system '/bin/stty', '-echo';
91   $old_password=<STDIN>;
92   chop $old_password;
93   #$old_password =~ /^(.{6,8})$/ or die "\nIllegal password.\n";
94   $old_password =~ /^(.{0,255})$/ or die "\nIllegal password.\n";
95   $old_password = $1;
96   print "\nEnter the new password (minimum of 6, maximum of 8 characters)\n";
97   print "Please use a combination of upper and lowercase letters and numbers.\n";
98   print "New password:";
99   $new_password=<STDIN>;
100   chop($new_password);
101   #$new_password =~ /^(.{6,8})$/ or die "\nIllegal password.\n";
102   $new_password =~ /^(.{0,255})$/ or die "\nIllegal password.\n";
103   $new_password = $1;
104   print "\nRe-enter new password:";
105   my($check_new_password);
106   $check_new_password=<STDIN>;
107   chop($check_new_password);
108   die "\nThey don't match; try again.\n" unless $check_new_password eq $new_password;
109
110   $new_gecos='';
111   $new_shell='';
112 }
113 print "\n";
114
115 system '/bin/stty', 'echo'; 
116
117 my $rv = passwd(
118   'username'     => $me,
119   'old_password' => $old_password,
120   'new_password' => $new_password,
121   'new_gecos'    => $new_gecos,
122   'new_shell'    => $new_shell,
123 );
124
125 my $error = $rv->{error};
126
127 if ($error) {
128   print "\nUpdate error: $error\n";
129 } else {
130   print "\nUpdate sucessful.\n";
131 }