Unix-Login
view release on metacpan or search on metacpan
passreq => 1,
# If can't find homedir
nohomemesg => "No home directory! Setting HOME=/\n",
# Where to take input from
input => \*STDIN,
output => \*STDOUT,
# Set ENV variables?
setenv => 1,
clearenv => 0,
path => '/usr/bin:',
supath => '/usr/sbin:/usr/bin',
maildir => '/var/mail',
# Use TomC's User::pwent module?
pwent => 0,
# Exec the person's shell?
cdhome => 0,
sleep $conf{failsleep};
print OUTPUT $conf{failmesg};
}
unless ($success) {
sttyexit if $conf{failexit};
return;
}
# Do a few basic things
if ($conf{setenv}) {
undef %ENV if $conf{clearenv}; # clean slate
$ENV{LOGNAME} = $pwstruct[0];
$ENV{PATH} = ($pwstruct[2] == 0) ? $conf{supath} : $conf{path};
$ENV{HOME} = $pwstruct[7];
$ENV{SHELL} = $pwstruct[8];
$ENV{MAIL} = $conf{maildir} . '/' . $pwstruct[0];
}
# Fork a shell if, for some strange reason, we are asked to.
# We use the little-known indirect object form of exec()
banner Banner printed once up top ["Please Login\n"]
bannerfile File to print after banner (i.e. /etc/issue) []
login Prompt asking for username ["login: "]
password Prompt asking for password ["Password: "]
sameuser Take username from process? [0]
passreq Require a password for all users? [1]
nohomemesg Printed if no homedir ["No home directory! Setting HOME=/\n"]
stripspaces Strip spaces from username? [1]
setenv If true, setup HOME and other %ENV variables [1]
clearenv If true, first undef %ENV before setenv [0]
path If setenv, set PATH to this for non-root [/usr/bin:]
supath If setenv, set PATH to this for root [/usr/sbin:/usr/bin]
maildir If setenv, set MAIL to this dir/username [/var/mail]
input Where to read input from filehandle [STDIN]
output Where to write output to filehandle [STDOUT]
pwent Return a User::pwent struct in scalar context? [0]
cdhome Chdir to the person's homedir on success? [0]
execshell Execute the person's shell as login session? [0]
So, for example, you can create a fully-customized login screen like so:
If you really like OO-calling styles, this module also provides an
OO form, although I personally think it's rather silly.
The C<new()> function creates a new Unix::Login object. It accepts the
same parameters as listed above. Then, you call C<login()> as a member
function. So for example:
use Unix::Login;
my $ul = Unix::Login->new(setenv => 0, passreq => 0);
my @pw = $ul->login;
Personally, I always just use C<login()> as a function...
=head1 NOTES
This module automatically grabs control of the signals C<INT>, C<TERM>,
and C<QUIT>, just like C<DBI.pm>, to make sure that a C<^C> causes the
module to fail insted of accidentally succeed.
failsleep And sleep for this many seconds [3]
banner Banner printed once up top ["Please Login\n"]
bannerfile If set, printed after banner (i.e. /etc/issue) []
login Prompt asking for username ["login: "]
password Prompt asking for password ["Password: "]
passreq Require a password for all users? [1]
nohomemesg Printed if no homedir ["No home directory! Setting HOME=/\n"]
setenv If true, setup HOME and other %ENV variables [1]
clearenv If true, first undef %ENV before setenv [0]
path If setenv, set PATH to this for non-root [/usr/bin:]
supath If setenv, set PATH to this for root [/usr/sbin:/usr/bin]
maildir If setenv, set MAIL to this dir/username [/var/mail]
pwent Return a User::pwent struct in scalar context? [0]
cdhome Chdir to the person's homedir on success? [0]
execshell Execute the person's shell as login session? [0]
If the "pwent" option is set, then User::pwent is used to provide an
object in a scalar context. See the man page for User::pwent.
If the "execshell" option is set, then if login() is successful the
user's shell is forked and the current process is terminated, just like
a real Unix login session.
With these options, you could create a very Unix-like login with the
following:
use Unix::Login;
my $ul = Unix::Login->new(bannerfile => '/etc/issue',
banner => `uname -rs`,
setenv => 1,
clearenv => 1,
cdhome => 1,
execshell => 1);
my(@pwent) = $ul->login || exit 1;
This will validate our login, clear our environment and reset it, then
exec the shell as a login shell just like a real life Unix login.
login(option => value, option => value)
( run in 0.383 second using v1.01-cache-2.11-cpan-a1d94b6210f )