summaryrefslogtreecommitdiff
path: root/fs_passwd/fs_passwd
blob: feddb462ce83369ffc8035af52d2ca595c417f5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/perl -Tw
#
# fs_passwd
#
# portions of this script are copied from the `passwd' script in the original
# (perl 4) camel book, now archived at 
# http://www.perl.com/CPAN/scripts/nutshell/ch6/passwd
#
# ivan@sisd.com 98-mar-8
#
# password lengths 0,255 instead of 6,8 - we'll let the server process
# check the data ivan@sisd.com 98-jul-17
#
# updated for the exciting new world of self-service 2004-mar-10

use strict;
use Getopt::Std;
use FS::SelfService qw(passwd);
use vars qw($opt_f $opt_s);

my($freeside_uid)=scalar(getpwnam('freeside'));

$ENV{'PATH'} ='/usr/local/bin:/usr/bin:/usr/ucb:/bin';
$ENV{'SHELL'} = '/bin/sh';
$ENV{'IFS'} = " \t\n";
$ENV{'CDPATH'} = '';
$ENV{'ENV'} = '';
$ENV{'BASH_ENV'} = '';

$SIG{__DIE__}= sub { system '/bin/stty', 'echo'; };

die "passwd program isn't running setuid to freeside\n" if $> != $freeside_uid;

unshift @ARGV, "-f" if $0 =~ /chfn$/;
unshift @ARGV, "-s" if $0 =~ /chsh$/;

getopts('fs');

my($me)='';
if ( $_ = shift(@ARGV) ) {
  /^(\w{2,8})$/;
  $me = $1; 
}
die "You can't change the password for $me." if $me && $<;
$me = (getpwuid($<))[0] unless $me;

my($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell)=
  getpwnam $me;

my($old_password,$new_password,$new_gecos,$new_shell);

if ( $opt_f || $opt_s ) {
  system '/bin/stty', '-echo';
  print "Password:";
  $old_password=<STDIN>;
  system '/bin/stty', 'echo'; 
  chop($old_password);
  #$old_password =~ /^(.{6,8})$/ or die "\nIllegal password.\n";
  $old_password =~ /^(.{0,255})$/ or die "\nIllegal password.\n";
  $old_password = $1;

  $new_password = '';

  if ( $opt_f ) {
    print "\nChanging gecos for $me.\n";
    print "Gecos [", $gcos, "]: ";
    $new_gecos=<STDIN>;
    chop($new_gecos);
    $new_gecos ||= $gcos;
    $new_gecos =~ /^(.{0,255})$/ or die "\nIllegal gecos.\n";
  } else {
    $new_gecos = '';
  } 

  if ( $opt_s ) {
    print "\nChanging shell for $me.\n";
    print "Shell [", $shell, "]: ";
    $new_shell=<STDIN>;
    chop($new_shell);
    $new_shell ||= $shell;
    $new_shell =~ /^(.{0,255})$/ or die "\nIllegal shell.\n";
  } else {
    $new_shell = '';
  }

} else {

  print "Changing password for $me.\n";
  print "Old password:";
  system '/bin/stty', '-echo';
  $old_password=<STDIN>;
  chop $old_password;
  #$old_password =~ /^(.{6,8})$/ or die "\nIllegal password.\n";
  $old_password =~ /^(.{0,255})$/ or die "\nIllegal password.\n";
  $old_password = $1;
  print "\nEnter the new password (minimum of 6, maximum of 8 characters)\n";
  print "Please use a combination of upper and lowercase letters and numbers.\n";
  print "New password:";
  $new_password=<STDIN>;
  chop($new_password);
  #$new_password =~ /^(.{6,8})$/ or die "\nIllegal password.\n";
  $new_password =~ /^(.{0,255})$/ or die "\nIllegal password.\n";
  $new_password = $1;
  print "\nRe-enter new password:";
  my($check_new_password);
  $check_new_password=<STDIN>;
  chop($check_new_password);
  die "\nThey don't match; try again.\n" unless $check_new_password eq $new_password;

  $new_gecos='';
  $new_shell='';
}
print "\n";

system '/bin/stty', 'echo'; 

my $rv = passwd(
  'username'     => $me,
  'old_password' => $old_password,
  'new_password' => $new_password,
  'new_gecos'    => $new_gecos,
  'new_shell'    => $new_shell,
);

my $error = $rv->{error};

if ($error) {
  print "\nUpdate error: $error\n";
} else {
  print "\nUpdate sucessful.\n";
}