-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathNiPasswd.pm
188 lines (137 loc) · 4.3 KB
/
NiPasswd.pm
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/perl
#
# NiPasswd.pm - perl interface to the "nipasswd" program.
#
# See perl POD documentation at end.
# To view, run: perldoc NiPasswd.pm
#
# Part of the "web-chpass" package.
# https://github.com/chip-rosenthal/web-chpass
#
# Chip Rosenthal
#
use strict;
use warnings;
use File::Temp qw(tempfile);
use IO::File;
package NiPasswd;
our $PATH_NIPASSWD = "/usr/local/lib/web-chpass/nipasswd";
our $DEBUG = 0;
require Exporter;
use vars qw(@ISA @EXPORT);
@ISA = qw(Exporter);
@EXPORT = qw($PATH_NIPASSWD $DEBUG);
sub _run_nipasswd
{
die 'usage: _run_nipasswd($input_file, [$options, ...])'
unless(@_ > 0);
my $input_file = shift;
if ($DEBUG) {
unshift(@_, "-D");
}
my $options = join(' ', @_);
#
# Startup "nipasswd".
#
open(FP, "-|", "$PATH_NIPASSWD $options <$input_file 2>&1")
or die("SYSTEM ERROR: pipe($PATH_NIPASSWD) failed: $!");
#
# Retrieve the results from "nipasswd."
#
my $resp = join("", <FP>);
close(FP);
my $rc = $?;
if (($rc & 0xFF) != 0) {
# Process did not exit normally.
$rc = -1;
$resp = sprintf("exit status 0x%04x", $rc);
} else {
$rc = ($rc & 0xFF00) >> 8;
}
return ($rc, $resp);
}
sub change_passwd
{
die 'usage: NiPasswd::change_passwd($username, $old_password, $new_password)'
unless(@_ == 3);
my($username, $old_password, $new_password) = @_;
my($fh, $fname) = main::tempfile(UNLINK => 1)
or die("SYSTEM ERROR: tempfile failed: $!");
$fh->print($username, "\n");
$fh->print($old_password, "\n");
$fh->print($new_password, "\n");
$fh->close();
my @ret = _run_nipasswd($fname);
unlink($fname);
return @ret;
}
sub auth_user
{
die 'usage: NiPasswd::auth_user($username, $password)'
unless(@_ == 2);
my($username, $password) = @_;
my($fh, $fname) = main::tempfile(UNLINK => 1)
or die("SYSTEM ERROR: tempfile failed: $!");
$fh->print($username, "\n");
$fh->print($password, "\n");
$fh->close();
my @ret = _run_nipasswd($fname, '-a');
unlink($fname);
return @ret;
}
1;
__END__
=head1 NAME
NiPasswd - non-interactive password change or verify
=head1 SYNOPSIS
use lib "/usr/local/lib/web-chpass";
use NiPasswd;
$NiPasswd::PATH_NIPASSWD = "/usr/local/lib/web-chpass/nipasswd";
($rc, $resp) = NiPasswd::auth_user($username, $password);
($rc, $resp) = NiPasswd::change_passwd($username, $old_password, $new_password);
=head1 DESCRIPTION
The "NiPasswd" module is a perl interface to the I<nipass(8)> utility.
It allows scripts to authenticate users and change passwords through
the I<pam(8)> subsystem. It provides these functions to non-privileged
programs in a fairly secure and trustworthy fashion.
The following functions are provided:
=over 4
=item B<NiPasswd::auth_user()>
Authenticate a user through the PAM subsystem, given the specified
I<username> and I<password>.
=item B<NiPasswd::change_password()>
First, authenticate a user through the PAM subsystem, given the specified
I<username> and I<old_password>. If authentication passes, then change
the account password to I<new_password>.
=back
Both of these routines return an list of two values. The first value
is one of the following numeric status codes:
-1 process did not exit normally
0 password successfully changed (EX_SUCCESS)
1 failed due to an error (EX_ERROR)
2 failed due to username/password auth (EX_DENIED)
3 failed due to bad password checks (EX_BADPW)
The second value will be a diagnostic message that may be displayed to
the user on a non-zero status. It contains the output produced by
I<nipasswd(8)>, and usually represents some diagnostic from the PAM
system.
The following global parameters are provided to configure the "NiPasswd"
system:
=over 4
=item B<$NiPasswd::PATH_NIPASSWD>
Pathname to the I<nipasswd(8)> command. This may be used if the
default value I</usr/local/lib/web-chpass/nipasswd> is not correct.
=item B<$NiPasswd::DEBUG>
If set non-zero, enables debugging output from I<nipasswd>.
=back
=head1 NOTES
This module is just a wrapper around I<nipasswd(8)>. See the manpage
for that utility. The limits and caveats apply here too.
=head1 AUTHOR
Chip Rosenthal
Unicom Systems Development
This module is part of the "web-chpass" package.
Visit http://www.unicom.com/sw/web-chpass/ for more info.
$Id: NiPasswd.pm,v 1.2 2002/07/21 21:29:23 chip Exp $