NAME
pam_faq - Frequently-Asked Questions about Authen::PAM.
SYNOPSIS
perldoc pam_faq
VERSION
This document is currently at version 0.01, as of April 21, 2000
DESCRIPTION
1. Can I authenticate a user non interactively?
Yes, you can although not in a very clean way. The PAM library has a mechanism, in a form of a conversation function, to send and receive text data from the user. For details of the format of the conversation function consult the Authen::PAM manual. This function receives a list of code/string pairs. There are two codes (PAM_TEXT_INFO and PAM_ERROR_MSG) for displaying the associated string to the user and two codes (PAM_ECHO_ON and PAM_ECHO_OFF) for getting input from the user. As you can see the codes are rather general and you can not be completely sure when you are asked for a user name and when for a password. However, the common practice is that PAM_ECHO_ON is used for a user name and PAM_ECHO_OFF is used for a password. So, what you can do is to write your own conversation function which ignores the PAM_TEXT_INFO and PAM_ERROR_MSG codes and returns the user name for the code PAM_ECHO_ON and the password for the code PAM_ECHO_OFF. If you pass the user name in the initialization function then usually you will not be asked for it. Here is a simple example how to do this:
use Authen::PAM;
$service = "passwd";
$username = "foo";
$password = "bar";
sub my_conv_func {
my @res;
while ( @_ ) {
my $code = shift;
my $msg = shift;
my $ans = "";
$ans = $username if ($code == PAM_PROMPT_ECHO_ON() );
$ans = $password if ($code == PAM_PROMPT_ECHO_OFF() );
push @res, PAM_SUCCESS();
push @res, $ans;
}
push @res, PAM_SUCCESS();
return @res;
}
ref($pamh = new Authen::PAM($service, $username, \&my_conv_func)) ||
die "Error code $pamh during PAM init!";
$pamh->pam_authenticate;
The Authen::PAM module comes with a default conversation function which you can find in the file PAM.pm.
2. Can I change a password non interactively?
All the discussion of the previous question also applies here. There is however one serious complication. When changing a password it is quite possible that the PAM library will send you at lest two PAM_ECHO_OFF prompts - one for the old password and one or two for the new one. Therefore, the first thing you should do is to see what sequence of prompts is produced by your service. Then the conversation function should include some state variable to distinguish the different prompts. Here is an example:
use Authen::PAM;
$service = "passwd";
$username = "foo";
$oldpassword = "bar_old";
$newpassword = "bar_new";
sub my_conv_func {
my @res;
while ( @_ ) {
my $code = shift;
my $msg = shift;
my $ans = "";
$ans = $username if ($code == PAM_PROMPT_ECHO_ON() );
if ($code == PAM_PROMPT_ECHO_OFF() ) {
$ans = $oldpassword if ($state == 0);
$ans = $newpassword if ($state == 1);
$ans = $newpassword if ($state == 2);
$state++;
}
push @res, PAM_SUCCESS();
push @res, $ans;
}
push @res, PAM_SUCCESS();
return @res;
}
ref($pamh = new Authen::PAM($service, $username, \&my_conv_func)) ||
die "Error code $pamh during PAM init!";
$state = 0;
$pamh->pam_chauthtok;
SEE ALSO
Authen-PAM
AUTHOR
Nikolay Pelov nikip@iname.com
COPYRIGHT
Copyright (c) 1998-2000 Nikolay Pelov. All rights reserved. This file is part of the Authen::PAM library. This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.