<!doctype linuxdoc system>

<!--

 $Id: pam_appl.sgml,v 1.11 1996/06/24 05:43:19 morgan Exp $

    Copyright (C) Andrew G. Morgan 1996.  All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

1. Redistributions of source code must retain the above copyright
   notice, and the entire permission notice in its entirety,
   including the disclaimer of warranties.

2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.

3. The name of the author may not be used to endorse or promote
   products derived from this software without specific prior
   written permission.

ALTERNATIVELY, this product may be distributed under the terms of the
GNU General Public License, in which case the provisions of the GNU
GPL are required INSTEAD OF the above restrictions.  (This clause is
necessary due to a potential bad interaction between the GNU GPL and
the restrictions contained in a BSD-style copyright.)

THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.

 -->

<article>

<title>The <bf>Linux-PAM</bf> Application Developers' Guide
<author>Andrew G. Morgan, <tt>morgan@physics.ucla.edu</tt>
<date>DRAFT v0.43 1996/6/23
<abstract>
This manual documents what an application developer needs to know
about the <bf>Linux-PAM</bf> library. It describes how an application
might use the <bf>Linux-PAM</bf> library to authenticate users. It also
contains some comments on PAM related security issues for the
application developer.
</abstract>

<toc>

<sect>Introduction

<sect1> SYNOPSIS
<p>
<tscreen>
<verb>
#include <security/pam_appl.h>

cc -o application .... -lpam
</verb>
</tscreen>

<sect1> Description

<p>
<bf>Linux-PAM</bf> (Pluggable Authentication Modules) is a library that
enables the local system administrator to choose how individual
applications authenticate users.  For an overview of the <bf>Linux-PAM</bf>
library see the <bf/Linux-PAM/ System Administrators' Guide.

<p>
It is the purpose of the <bf>Linux-PAM</bf> project to liberate the
development of privilege granting software from the development of
secure and appropriate authentication schemes.  This is accomplished
by providing a documented library of functions that an application may
use for all forms of user authentication management. This library
dynamically loads locally configured authentication modules that
actually perform the task of user authentication.

<p>
From the perspective of an application developer the information
contained in the local configuration of the PAM library should not be
important. Indeed it is intended that an application treat the
functions documented in this file as a ``black box'' that will deal
with all aspects of user authentication. ``All aspects'' includes user
verification and also the resetting of passwords etc. .

<sect>The public interface to <bf>Linux-PAM</bf>
 
<p>
Firstly, the relevant include file for the <bf>Linux-PAM</bf> library
is <tt>&lt;security/pam_appl.h&gt;</tt>. It contains the definitions
for a number of functions. After listing these functions, we collect
some guiding remarks for programmers.

<sect1>What can be expected by the application

<p>
Here we document those functions in the <bf/Linux-PAM/ library that
may be called from an application.

<p>
<itemize>
<item>
<tt>extern int pam_start(const char *service_name, const char *user,
const struct pam_conv *pam_conversation, pam_handle_t **pamh);</tt>

<p>
This is the first of the <bf>Linux-PAM</bf> functions that must be
called by an application. It initializes the interface and reads the
system configuration file, <tt>/etc/pam.conf</tt> (see the
<bf/Linux-PAM/ System Administrators' Guide). Following a successful
return (<tt/PAM_SUCCESS/) the contents of <tt/*pamh/ is a handle that
provides continuity for successive calls to the <bf/Linux-PAM/
library. The arguments expected by <tt/pam_start/ are as follows: the
<tt/service_name/ of the program, the <tt/user/name of the individual
to be authenticated, a pointer to an application-supplied
<tt/pam_conv/ structure and a pointer to a <tt/pam_handle_t/
<em/pointer/.

<p>
The <tt>pam_conv</tt> structure is discussed more fully below in the
section covering ``What is expected of an application''. The
<tt>pam_handle_t</tt> is a <em>blind</em> structure and the
application should not attempt to probe it directly for
information. Instead the <bf>Linux-PAM</bf> library provides the
functions <tt>pam_set_item</tt> and <tt>pam_get_item</tt>. These
functions are documented below.

<p>
<item>
<tt>extern int pam_end(pam_handle_t *pamh, int pam_status);</tt>

<p>
This function is the last function an application should call in the
<bf>Linux-PAM</bf> library. Upon return the handle <tt/pamh/ is no
longer valid. Under normal conditions the argument <tt/pam_status/ has
the value PAM_SUCCESS, but its purpose is to be passed as an argument
to the module specific function <tt/cleanup()/ (see the <bf/Linux-PAM/
Module Developers' Guide).

<p>
<item>
<tt>extern int pam_set_item(pam_handle_t *pamh, int item_type, const
void *item);</tt>

<p>This function is used to (re)set the value of one of the following
<bf/item_type/s:

<p><descrip>
<tag><tt/PAM_SERVICE/</tag>
     	The service name
<tag><tt/PAM_USER/</tag>
	The user name
<tag><tt/PAM_TTY/</tag>
	The terminal name
<tag><tt/PAM_RHOST/</tag>
	The remote host name
<tag><tt/PAM_CONV/</tag>
	The pam_conv structure (see below)
<tag><tt/PAM_RUSER/</tag>
	The remote user name
<tag><tt/PAM_USER_PROMPT/</tag>
	The string used when prompting for a user's name.
</descrip>

<p>
For all <tt/item_type/s, but <tt/PAM_CONV/, <tt/item/ is a pointer to
a <tt>&lt;NUL&gt;</tt> terminated character string. In the case of
<tt/PAM_CONV/, <tt/item/ points to an initialized <tt/pam_conv/
structure (see below).

<p>
A successful call to this function returns <tt/PAM_SUCCESS/. However
the application can expect one of the following errors:

<p>
<descrip>
<tag><tt/PAM_PERM_DENIED/</tag>
	An attempt was made to replace the conversation structure with
a <tt/NULL/ value.
<tag><tt/PAM_BUF_ERR/</tag>
	The function ran out of memory making a copy of the item.
<tag><tt/PAM_BAD_ITEM/</tag>
	The application attempted to set an un-defined item.
</descrip>

<p>
<item>
<tt>extern int pam_get_item(const pam_handle_t *pamh, int item_type,
const void **item);</tt>

<p>
This function is used to obtain the value of the indicated
<tt/item_type/.  Upon successful return, <tt/*item/ contains a pointer
to the value of the corresponding item. Note, this is a pointer to the
<em/actual/ data and should <em/not/ be <tt/free()/'ed or
over-written! A successful call is signaled by a return value of
<tt/PAM_SUCCESS/.  If an attempt is made to get an undefined item,
<tt/PAM_BAD_ITEM/ is returned.

<p>
<item>
<tt>extern const char *pam_strerror(int errnum);</tt>

<p>This function returns some text describing the <bf>Linux-PAM</bf>
error associated with the argument <tt/errnum/. If the error is not
recognized <tt/``Unknown Linux-PAM error''/ is returned.

<p>
<item>
<tt>extern int pam_authenticate(pam_handle_t *pamh, int flags);</tt>

<p>
This function serves as an interface to the authentication mechanisms
of the loaded modules. The single <em/optional/ flag, which may be
logically OR'd with <tt/PAM_SILENT/, takes the following value,

<p><descrip>

<tag><tt/PAM_DISALLOW_NULL_AUTHTOK/</tag>
	Instruct the authentication modules to return
<tt/PAM_AUTH_ERR/ if the user does not have a registered
authorization token---it is set to <tt/NULL/ in the system database.
</descrip>

<p>
The value returned by this function is one of the following:

<p><descrip>

<tag><tt/PAM_AUTH_ERR/</tag>
	The user was not authenticated
<tag><tt/PAM_CRED_INSUFFICIENT/</tag>
	For some reason the application does not have sufficient
credentials to authenticate the user.
<tag><tt/PAM_AUTHINFO_UNAVAIL/</tag>
	The modules were not able to access the authentication
information. This might be due to a network or hardware failure etc.
<tag><tt/PAM_USER_UNKNOWN/</tag>
	The supplied username is not known to the authentication
service
<tag><tt/PAM_MAXTRIES/</tag>
	One or more of the authentication modules has reached its
limit of tries authenticating the user. Do not try again.

</descrip>

<p>
If one or more of the authentication modules fails to load, for
whatever reason, this function will return <tt/PAM_ABORT/.

<p>
<item>
<tt>extern int pam_setcred(pam_handle_t *pamh, int flags);</tt>

<p>
This function is used to set the module-specific credentials of the
user. It is usually called after the user has been authenticated. This
function simply calls the <tt/pam_sm_setcred/ functions of each of the
loaded modules.  Valid <tt/flags/, any one of which, may be logically
OR'd with <tt/PAM_SILENT/, are:

<p><descrip>
<tag><tt/PAM_CRED_ESTABLISH/</tag>
	Set the credentials for the authentication service,
<tag><tt/PAM_CRED_DELETE/</tag>
	Delete the credentials associated with the authentication service,
<tag><tt/PAM_CRED_REINITIALIZE/</tag>
	Reinitialize the user credentials, and
<tag><tt/PAM_CRED_REFRESH/</tag>
	Extend the lifetime of the user credentials.
</descrip>

<p>
Return values that the application should look out for are the
following:

<p><descrip>
<tag><tt/PAM_CRED_UNAVAIL/</tag>
	A module cannot retrieve the user's credentials.
<tag><tt/PAM_CRED_EXPIRED/</tag>
	The user's credentials have expired.
<tag><tt/PAM_USER_UNKNOWN/</tag>
	The user is not known to an authentication module.
<tag><tt/PAM_CRED_ERR/</tag>
	A module was unable to set the credentials of the user.
</descrip>

<p>
<item>
<tt>extern int pam_acct_mgmt(pam_handle_t *pamh, int flags);</tt>

<p>
This function is typically called after the user has been
authenticated. It establishes whether the user's account is
healthy. That is to say, whether the user's account is still active
and whether the user is permitted to gain access to the system at this
time. Valid flags, any one of which, may be logically OR'd with
<tt/PAM_SILENT/, and are the same as those applicable to the
<tt/flags/ argument of <tt/pam_authenticate/.

<p>
Currently, this function simply calls the corresponding functions of
each of the loaded modules, as instructed by the configuration file,
<tt>/etc/pam.conf</tt>.

<p>
The normal response from this function is <tt/PAM_SUCCESS/, however,
specific failures are indicated by the following error returns:

<descrip>
<tag><tt/PAM_ACCT_EXPIRED/</tag>
	The user is no longer permitted access to the system.
<tag><tt/PAM_AUTH_ERR/</tag>
	There was an authentication error.
<tag><tt/PAM_AUTHTOKEN_REQD/</tag>
	The user's authentication token has expired. Before calling
this function again the application should arrange for a new one to be
given. This is accomplished via a call to <tt/pam_chauthtok()/.
<tag><tt/PAM_PERM_DENIED/</tag>
	The user is not permitted to gain access at this time.
<tag><tt/PAM_USER_UNKNOWN/</tag>
	The user is not known to a module's account management
component.

</descrip>

<p>
<item>
<tt>extern int pam_chauthtok(pam_handle_t *pamh, const int flags);</tt>

<p>
This function is used to change the authentication token for a given
user (as indicated by the state associated with the handle,
<tt/pamh/). The following is a valid but optional flag which may be
logically OR'd with <tt/PAM_SILENT/,

<descrip>
<tag><tt/PAM_CHANGE_EXPIRED_AUTHTOK/</tag>
	This argument indicates to the modules that the users
authentication token (password) should only be changed if it has
expired.
</descrip>

<p>
Note, if this argument is not passed, the application requires that
<em/all/ authentication tokens are to be changed.

<p>
<tt/PAM_SUCCESS/ is the only successful return value, valid
error-returns are:

<descrip>
<tag><tt/PAM_AUTHTOK_ERR/</tag>
	A module was unable to obtain the new authentication token.
	
<tag><tt/PAM_AUTHTOK_RECOVERY_ERR/</tag>
	A module was unable to obtain the old authentication token.

<tag><tt/PAM_AUTHTOK_LOCK_BUSY/</tag>
	One or more of the modules was unable to change the
authentication token since it is currently locked.
	
<tag><tt/PAM_AUTHTOK_DISABLE_AGING/</tag>
	Authentication token aging has been disabled for at least one
of the modules.

<tag><tt/PAM_PERM_DENIED/</tag>
	Permission denied.

<tag><tt/PAM_TRY_AGAIN/</tag>
	Not all of the modules were in a position to update the
authentication token(s).

<tag><tt/PAM_USER_UNKNOWN/</tag>
	The user is not known to the authentication token changing
service.

</descrip>

<p>
<item>
<tt>extern int pam_open_session(pam_handle_t *pamh, int flags);</tt>

<p>
This function is used to indicate that an authenticated session has
begun. It is used to inform the module that the user is currently in a
session. It should be possible for the <bf>Linux-PAM</bf> library to
open a session and close the same session (see below) from different
applications.

<p>
Currently, this function simply calls each of the corresponding
functions of the loaded modules. The only valid flag is
<tt/PAM_SILENT/ and this is, of course, <em/optional/.

<p>
If any of the <em/required/ loaded modules are unable to open a
session for the user, this function will return <tt/PAM_SESSION_ERR/.

<p>
<item>
<tt>extern int pam_close_session(pam_handle_t *pamh, int flags);</tt>

<p>
This function is used to indicate that an authenticated session has
ended. It is used to inform the module that the user is exiting a
session. It should be possible for the <bf>Linux-PAM</bf> library to
open a session and close the same session from different applications.

<p>
Currently, this function simply calls each of the corresponding
functions of the loaded modules. The only valid flag is
<tt/PAM_SILENT/ and this is, of course, <em/optional/.

<p>
If any of the <em/required/ loaded modules are unable to close a
session for the user, this function will return <tt/PAM_SESSION_ERR/.

</itemize>

<sect1>What is expected of an application

<p>
An application must provide a ``conversation function''. It is used
for direct communication between a loaded module and the application
and will typically provide a means for the module to prompt the user
for a password etc. . The structure, <tt/pam_conv/, is defined by
including <tt>&lt;security/pam_appl.h&gt</tt>; to be,

<p>
<tscreen>
<verb>
struct pam_conv {
    int (*conv)(int num_msg,
        const struct pam_message **msg,
        struct pam_response **resp,
        void *appdata_ptr);
    void *appdata_ptr;
};
</verb>
</tscreen>

<p>
It is initialized by the application before it is passed to the
library. The contents of this structure are attached to the <tt/*pamh/
handle.  The point of this argument is to provide a mechanism for any
loaded module to interact directly with the application program. This
is why it is called a <em/conversation/ structure.

<p>
When a module calls the referenced <tt/conv()/ function, the argument
<tt/*appdata_ptr/ is set to the second element of this structure.

<p>
The other arguments of a call to <tt/conv()/ concern the information
exchanged by module and application. That is to say, <tt/num_msg/
holds the length of the array of pointers, <tt/msg/. After a
successful return, the pointer <tt/*resp/ points to an array of
<tt/pam_response/ structures, holding the application supplied text.
Note, <tt/*resp/ is an <tt/struct pam_response/ array and <em/not/ an
array of pointers.

<p>
The message (from the module to the application) passing structure is
defined by <tt>&lt;security/pam_appl.h&gt;</tt> as:

<p>
<tscreen>
<verb>
struct pam_message {
    int msg_style;
    const char *msg;
};
</verb>
</tscreen>

<p>
Valid choices for <tt/msg_style/ are:

<p><descrip>
<tag><tt/PAM_PROMPT_ECHO_OFF/</tag>
	Obtain a string without echoing any text
<tag><tt/PAM_PROMPT_ECHO_ON/</tag>
	Obtain a string whilst echoing text
<tag><tt/PAM_ERROR_MSG/</tag>
	Display an error
<tag><tt/PAM_TEXT_INFO/</tag>
	Display some text.
</descrip>

<p>
The point of having an array of messages is that it becomes possible
to pass a number of things to the application in a single call from
the module. It can also be convenient for the application that related
things come at once: a windows based application can then present a
single form with many messages/prompts on at once.

<p>
The response (from the application to the module) passing structure is
defined by including <tt>&lt;security/pam_appl.h&gt;</tt> as:

<p><tscreen><verb>
struct pam_response {
    char *resp;
    int resp_retcode;
};
</verb></tscreen>

<p>
Currently, there are no definitions for <tt/resp_retcode/ values; the
normal value is <tt/0/. The length of the <tt/*resp/ array is known in
advance by the module, since it is dependent on the number of prompts
sent in the <tt/msg/ array. The memory for this array and its string
elements should be <em/dynamically/ allocated with one of the
<tt/malloc()/ library of functions. <em>It will be <tt/free()/'d by
the module.</em>

<p>
The maximum length of the <tt/pam_msg.msg/ and <tt/pam_response.resp/
character strings is <tt/PAM_MAX_MSG_SIZE/.

<p>
<tt/PAM_SUCCESS/ is the expected return value of this
function. However, should an error occur the application should not
return set <tt/*resp/ and simply return <tt/PAM_CONV_ERR/.

<sect1>Programming notes

<p>
Note, all of the authentication service function calls accept the
token <tt/PAM_SILENT/, which instructs the modules to not send
messages to the application. This token can be logically OR'd with any
one of the permitted tokens specific to the individual function calls.
<tt/PAM_SILENT/ does not override the prompting of the user for
passwords etc., it only stops informative messages from being
generated.

<sect>Security issues of <bf>Linux-PAM</bf>

<p>
A poorly (or maliciously) written application can defeat any
<bf/Linux-PAM/ module's authentication mechanisms by simply ignoring
it's return values. It is the applications task and responsibility to
grant privileges and access to services. The <bf/Linux-PAM/ library
simply assumes the responsibility of <em/authenticating/ the user;
ascertaining that the user <em/is/ who they say they are. Care should
be taken to anticipate all of the documented behavior of the
<bf/Linux-PAM/ library functions. A failure to do this will most
certainly lead to a future security breach.

<p>
In general writers of authorization-granting applications should
assume that each module is likely to call any or <em/all/ `libc'
functions. For `libc' functions that return pointers to
static/dynamically allocated structures (ie. the library allocates the
memory and the user is not expected to `<tt/free()/' it) any module
call to this function is likely to corrupt a pointer previously
obtained by the application. The application programmer should either
re-call such a `libc' function after any call to the <bf/Linux-PAM/
library, or copy the structure contents to some safe area of memory
before passing control to the <bf/Linux-PAM/ library.

<p>
When picking the <em/service name/ that corresponds to the first entry
in the <tt>/etc/pam.conf</tt> file, the application programmer should
avoid the temptation of choosing, <tt/argv[0]/. Since a user can
always establish the service that uses the weakest authentication
service. By symbolically linking the targeted application to a link of
that name, the user assumes power to choose the authentication method
for any application.

<p>
Care should be taken to ensure that the <tt/conv()/ function is
robust.

<sect>An example application

<p><em>
It would be nice to give a simple template for a PAM-aware application
here. I guess it need not grant any privileges, but rather just print
``authentication succeeded/failed''. It would also be rather useful
for developing modules.
</em>

<sect>Files

<p><descrip>

<tag><tt>/usr/lib/libpam.so.*</tt></tag>

the shared library providing applications with access to
<bf/Linux-PAM/.

<tag><tt>/etc/pam.conf</tt></tag>

the <bf/Linux-PAM/ configuration file.

<tag><tt>/usr/lib/security/pam_*.so</tt></tag>

the primary location for <bf/Linux-PAM/ dynamically loadable object
files; the modules.

</descrip>

<sect>See also

<p><itemize>

<item>The <bf/Linux-PAM/ System Administrators' Guide.

<item>The <bf/Linux-PAM/ Module Writers' Guide.

<item>The V. Samar and R. Schemers (SunSoft), ``UNIFIED LOGIN WITH
PLUGGABLE AUTHENTICATION MODULES'', Open Software Foundation Request
For Comments 86.0, October 1995.

</itemize>

<sect>Notes

<p>
I intend to put development comments here... like ``at the moment
this isn't actually supported''. At release time what ever is in
this section will be placed in the Bugs section below! :)

<p>
<itemize>

<item>
the <tt/pam_status/ argument of <tt/pam_end()/ is passed to
<tt/cleanup()/? This seems dangerously close to making applications
module specific...? Perhaps someone can clarify this for me? Is
there going to be a recognized list of valid pam_status arguments? I
guess it should be in &lt;security/pam_appl.h&gt;?

<item> <tt/pam_strerror()/ should be internationalized....

<item> need a simple example application?

<item> <tt>pam_setcred</tt>, this function will get a better write up
once I appreciate it better!

<item> <tt/pam_conv/.. This seems to be a good way for the module to
set application environment variables etc. from the module . Might I
suggest that (see rfc86.0 pp26-27) we have a special action for the
conv function when the <tt/num_msg/ argument is <em/negative/. For
example, if <tt/num_mesg==PAM_EXPORT_ENV( = -1 )/, then the <tt/msg/
pointer points at an array of two <tt/pam_message/ structures holding
the name of the environment variable and its value. While we are at it
the <tt/*resp/ pointer can be directed at a pam_response holding the
<em/previous/ value of this variable (or <tt/NULL/). Thoughts?

<p> another thing that we might think about is multiple responses from
an application, I have inserted it into the code and documented it
here in the hope that no-one objects too strongly. My motivation is
simply that it seems desirable to get username and password with a
single call to <tt/conv()/. Wouldn't this be easier for windows based
applications to do logging in type things?

<item>
Note, the <tt/resp_retcode/ of struct <tt/pam_message/, has no
purpose at the moment. Ideas/suggestions welcome!

<item> more security issues are required....

</itemize>

<sect>Author/acknowledgments

<p>
This document was written by Andrew G. Morgan
(morgan@physics.ucla.edu) with many contributions from Marc Ewing,
Michael K. Johnson, Al Longyear, Marek Michalkiewicz, Theodore Ts'o,
Jeff Uphoff, Joseph S. D. Yao and Alex O.  Yuriev.

<p>
Thanks are also due to Sun Microsystems, especially to Vipin Samar and
Charlie Lai for their advice. At an early stage in the development of
<bf/Linux-PAM/, Sun graciously made the documentation for their
implementation of PAM available. This act greatly accelerated the
development of <bf/Linux-PAM/.

<sect>Bugs/omissions

<p>Few PAM modules currently exist. Few PAM-aware applications exist.
This manual is hopelessly unfinished. Only a partial list of people is
credited for all the good work they have done.

</article>
