The Linux-PAM Application Developers' Guide
Andrew G. Morgan, morgan@linux.kernel.org
DRAFT v0.71 1999/11/8
This manual documents what an application developer needs to know
about the LLiinnuuxx--PPAAMM library. It describes how an application might use
the LLiinnuuxx--PPAAMM library to authenticate users. In addition it contains a
description of the funtions to be found in libpam_misc library, that
can be used in general applications. Finally, it contains some com-
ments on PAM related security issues for the application developer.
______________________________________________________________________
Table of Contents
1. Introduction
1.1 Synopsis
1.2 Description
2. Overview
3. The public interface to Linux-PAM
3.1 What can be expected by the application
3.1.1 Initialization of Linux-PAM
3.1.2 Termination of the library
3.1.3 Setting PAM items
3.1.4 Getting PAM items
3.1.5 Understanding errors
3.1.6 Planning for delays
3.1.7 Authenticating the user
3.1.8 Setting user credentials
3.1.9 Account management
3.1.10 Updating authentication tokens
3.1.11 Session initialization
3.1.12 Terminating sessions
3.1.13 Setting PAM environment variables
3.1.14 Getting a PAM environment variable
3.1.15 Getting the PAM environment
3.2 What is expected of an application
3.2.1 The conversation function
3.3 Programming notes
4. Security issues of Linux-PAM
4.1 Care about standard library calls
4.2 Choice of a service name
4.3 The conversation function
4.4 The identity of the user
4.5 Sufficient resources
5. A library of miscellaneous helper functions
5.1 Requirements
5.2 Functions supplied
5.2.1 Safe string duplication
5.2.2 A text based conversation function
5.2.3 Transcribing an environment to that of Linux-PAM
5.2.4 Saving the Linux-PAM environment for later use
5.2.5 Liberating a locally saved environment
5.2.6 BSD like Linux-PAM environment variable setting
6. Porting legacy applications
7. Glossary of PAM related terms
8. An example application
9. Files
10. See also
11. Notes
12. Author/acknowledgments
13. Bugs/omissions
14. Copyright information for this document
______________________________________________________________________
11.. IInnttrroodduuccttiioonn
11..11.. SSyynnooppssiiss
For general applications that wish to use the services provided by
LLiinnuuxx--PPAAMM the following is a summary of the relevant linking
information:
#include <security/pam_appl.h>
cc -o application .... -lpam -ldl
In addition to libpam, there is a library of miscellaneous functions
that make the job of writing _P_A_M_-_a_w_a_r_e applications easier (this
library is not covered in the DCE-RFC for PAM and is specific to the
Linux-PAM distribution):
...
#include <security/pam_misc.h>
cc -o application .... -lpam -lpam_misc -ldl
11..22.. DDeessccrriippttiioonn
LLiinnuuxx--PPAAMM (Pluggable Authentication Modules for Linux) is a library
that enables the local system administrator to choose how individual
applications authenticate users. For an overview of the LLiinnuuxx--PPAAMM
library see the LLiinnuuxx--PPAAMM System Administrators' Guide.
It is the purpose of the LLiinnuuxx--PPAAMM 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 authentication tasks.
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 here as a ``black box'' that will deal with all
aspects of user authentication. ``All aspects'' includes user
verification, account management, session initialization/termination
and also the resetting of passwords (_a_u_t_h_e_n_t_i_c_a_t_i_o_n _t_o_k_e_n_s).
22.. OOvveerrvviieeww
Most service-giving applications are restricted. In other words,
their service is not available to all and every prospective client.
Instead, the applying client must jump through a number of hoops to
convince the serving application that they are authorized to obtain
service.
The process of _a_u_t_h_e_n_t_i_c_a_t_i_n_g a client is what PAM is designed to
manage. In addition to authentication, PAM provides account
management, credential management, session management and
authentication-token (password changing) management services. It is
important to realize when writing a PAM based application that these
services are provided in a manner that is ttrraannssppaarreenntt to the the
application. That is to say, when the application is written, no
assumptions can be made about _h_o_w the client will be authenticated.
The process of authentication is performed by the PAM library via a
call to pam_authenticate(). The return value of this function will
indicate whether a named client (the _u_s_e_r) has been authenticated. If
the PAM library needs to prompt the user for any information, such as
their _n_a_m_e or a _p_a_s_s_w_o_r_d then it will do so. If the PAM library is
configured to authenticate the user using some silent protocol, it
will do this too. (This latter case might be via some hardware
interface for example.)
It is important to note that the application must leave all decisions
about when to prompt the user at the discretion of the PAM library.
The PAM library, however, must work equally well for different styles
of application. Some applications, like the familiar login and passwd
are terminal based applications, exchanges of information with the
client in these cases is as plain text messages. Graphically based
applications, however, have a more sophisticated interface. They
generally interact with the user via specially constructed dialogue
boxes. Additionally, network based services require that text
messages exchanged with the client are specially formatted for
automated processing: one such example is ftpd which prefixes each
exchanged message with a numeric identifier.
The presentation of simple requests to a client is thus something very
dependent on the protocol that the serving application will use. In
spite of the fact that PAM demands that it drives the whole
authentication process, it is not possible to leave such protocol
subtleties up to the PAM library. To overcome this potential problem,
the application provides the PAM library with a _c_o_n_v_e_r_s_a_t_i_o_n function.
This function is called from wwiitthhiinn the PAM library and enables the
PAM to directly interact with the client. The sorts of things that
this conversation function must be able to do are prompt the user with
text and/or obtain textual input from the user for processing by the
PAM library. The details of this function are provided in a later
section.
For example, the conversation function may be called by the PAM
library with a request to prompt the user for a password. Its job is
to reformat the prompt request into a form that the client will
understand. In the case of ftpd, this might involve prefixing the
string with the number 331 and sending the request over the network to
a connected client. The conversation function will then obtain any
reply and, after extracting the typed password, will return this
string of text to the PAM library. Similar concerns need to be
addressed in the case of an X-based graphical server.
There are a number of issues that need to be addressed when one is
porting an existing application to become PAM compliant. A section
below has been devoted to this: Porting legacy applications.
Besides authentication, PAM provides other forms of management.
Session management is provided with calls to pam_open_session() and
pam_close_session(). What these functions actually do is up to the
local administrator. But typically, they could be used to log entry
and exit from the system or for mounting and unmounting the user's
home directory. If an application provides continuous service for a
period of time, it should probably call these functions, first open
after the user is authenticated and then close when the service is
terminated.
Account management is another area that an application developer
should include with a call to pam_acct_mgmt(). This call will perform
checks on the good health of the user's account (has it expired etc.).
One of the things this function may check is whether the user's
authentication token has expired - in such a case the application may
choose to attempt to update it with a call to pam_chauthtok(),
although some applications are not suited to this task (_f_t_p for
example) and in this case the application should deny access to the
user.
PAM is also capable of setting and deleting the users credentials with
the call pam_setcred(). This function should always be called after
the user is authenticated and before service is offered to the user.
By convention, this should be the last call to the PAM library before
service is given to the user. What exactly a credential is, is not
well defined. However, some examples are given in the glossary below.
33.. TThhee ppuubblliicc iinntteerrffaaccee ttoo LLiinnuuxx--PPAAMM
Firstly, the relevant include file for the LLiinnuuxx--PPAAMM library is
<security/pam_appl.h>. It contains the definitions for a number of
functions. After listing these functions, we collect some guiding
remarks for programmers.
33..11.. WWhhaatt ccaann bbee eexxppeecctteedd bbyy tthhee aapppplliiccaattiioonn
Below we document those functions in the LLiinnuuxx--PPAAMM library that may be
called from an application.
33..11..11.. IInniittiiaalliizzaattiioonn ooff LLiinnuuxx--PPAAMM
extern int pam_start(const char *service_name, const char *user,
const struct pam_conv *pam_conversation,
pam_handle_t **pamh);
This is the first of the LLiinnuuxx--PPAAMM functions that must be called by an
application. It initializes the interface and reads the system
configuration file, /etc/pam.conf (see the LLiinnuuxx--PPAAMM System
Administrators' Guide). Following a successful return (PAM_SUCCESS)
the contents of *pamh is a handle that provides continuity for
successive calls to the LLiinnuuxx--PPAAMM library. The arguments expected by
pam_start are as follows: the service_name of the program, the
username of the individual to be authenticated, a pointer to an
application-supplied pam_conv structure and a pointer to a
pam_handle_t _p_o_i_n_t_e_r.
The pam_conv structure is discussed more fully in the section
``below''. The pam_handle_t is a _b_l_i_n_d structure and the application
should not attempt to probe it directly for information. Instead the
LLiinnuuxx--PPAAMM library provides the functions pam_set_item and
pam_get_item. These functions are documented below.
33..11..22.. TTeerrmmiinnaattiioonn ooff tthhee lliibbrraarryy
extern int pam_end(pam_handle_t *pamh, int pam_status);
This function is the last function an application should call in the
LLiinnuuxx--PPAAMM library. Upon return the handle pamh is no longer valid and
all memory associated with it will be invalid (likely to cause a
segmentation fault if accessed).
Under normal conditions the argument pam_status has the value
PAM_SUCCESS, but in the event of an unsuccessful service application
the approprite LLiinnuuxx--PPAAMM error-return value should be used here.
attempt its purpose is to be passed as an argument to the module
specific function cleanup() (see the LLiinnuuxx--PPAAMM Module Developers'
Guide).
33..11..33.. SSeettttiinngg PPAAMM iitteemmss
extern int pam_set_item(pam_handle_t *pamh, int item_type,
const void *item);
This function is used to (re)set the value of one of the following
iitteemm__ttyyppees:
PAM_SERVICE
The service name
PAM_USER
The user name
PAM_TTY
The terminal name: prefixed by /dev/ if it is a device file; for
graphical, X-based, applications the value for this item should
be the $DISPLAY variable.
PAM_RHOST
The remote host name
PAM_CONV
The conversation structure (see section ``below'')
PAM_RUSER
The remote user name
PAM_USER_PROMPT
The string used when prompting for a user's name. The default
value for this string is ``Please enter username: ''.
For all item_types, other than PAM_CONV, item is a pointer to a <NUL>
terminated character string. In the case of PAM_CONV, item points to
an initialized pam_conv structure (see section ``below'').
A successful call to this function returns PAM_SUCCESS. However, the
application should expect one of the following errors:
PAM_PERM_DENIED
An attempt was made to replace the conversation structure with a
NULL value.
PAM_BUF_ERR
The function ran out of memory making a copy of the item.
PAM_BAD_ITEM
The application attempted to set an undefined item.
33..11..44.. GGeettttiinngg PPAAMM iitteemmss
extern int pam_get_item(const pam_handle_t *pamh, int item_type,
const void **item);
This function is used to obtain the value of the indicated item_type.
Upon successful return, *item contains a pointer to the value of the
corresponding item. Note, this is a pointer to the _a_c_t_u_a_l data and
should _n_o_t be free()'ed or over-written! A successful call is
signaled by a return value of PAM_SUCCESS. If an attempt is made to
get an undefined item, PAM_BAD_ITEM is returned.
33..11..55.. UUnnddeerrssttaannddiinngg eerrrroorrss
extern const char *pam_strerror(pam_handle_t *pamh, int errnum);
This function returns some text describing the LLiinnuuxx--PPAAMM error
associated with the argument errnum. If the error is not recognized
``Unknown Linux-PAM error'' is returned.
33..11..66.. PPllaannnniinngg ffoorr ddeellaayyss
extern int pam_fail_delay(pam_handle_t *pamh, unsigned int micro_sec);
This function is offered by LLiinnuuxx--PPAAMM to facilitate time delays
following a failed call to pam_authenticate() and before control is
returned to the application. When using this function the application
programmer should check if it is available with,
#ifdef HAVE_PAM_FAIL_DELAY
....
#endif /* HAVE_PAM_FAIL_DELAY */
Generally, an application requests that a user is authenticated by
LLiinnuuxx--PPAAMM through a call to pam_authenticate() or pam_chauthtok().
These functions call each of the _s_t_a_c_k_e_d authentication modules listed
in the relevant LLiinnuuxx--PPAAMM configuration file. As directed by this
file, one of more of the modules may fail causing the pam_...() call
to return an error. It is desirable for there to also be a pause
before the application continues. The principal reason for such a
delay is security: a delay acts to discourage _b_r_u_t_e _f_o_r_c_e dictionary
attacks primarily, but also helps hinder _t_i_m_e_d (covert channel)
attacks.
The pam_fail_delay() function provides the mechanism by which an
application or module can suggest a minimum delay (of micro_sec _m_i_c_r_o_-
_s_e_c_o_n_d_s). LLiinnuuxx--PPAAMM keeps a record of the longest time requested with
this function. Should pam_authenticate() fail, the failing return to
the application is delayed by an amount of time randomly distributed
(by up to 25%) about this longest value.
Independent of success, the delay time is reset to its zero default
value when LLiinnuuxx--PPAAMM returns control to the application.
For applications written with a single thread that are event driven in
nature, libpam generating this dalay may be undesirable. Instead, the
application may want to register the delay in some other way. For
example, in a single threaded server that serves multiple
authentication requests from a single event loop, the application
might want to simply mark a given connection as blocked until an
application timer expires. For this reason, LLiinnuuxx--PPAAMM supplies the
PAM_FAIL_DELAY item. It can be queried and set with pam_get_item() and
pam_set_item() respectively. The value used to set it should be a
function pointer of the following prototype:
void (*delay_fn)(int retval, unsigned usec_delay, void *appdata_ptr);
The arguments being the retval return code of the module stack, the
usec_delay micro-second delay that libpam is requesting and the
appdata_ptr that the application has associated with the current pamh
(/tt/pam_handle_t/). This last value was set by the application when
it called pam_start or explicitly with pam_set_item(... , PAM_CONV,
...). Note, if PAM_FAIL_DELAY is unset (or set to NULL), then libpam
will perform any delay.
33..11..77.. AAuutthheennttiiccaattiinngg tthhee uusseerr
extern int pam_authenticate(pam_handle_t *pamh, int flags);
This function serves as an interface to the authentication mechanisms
of the loaded modules. The single _o_p_t_i_o_n_a_l flag, which may be
logically OR'd with PAM_SILENT, takes the following value,
PAM_DISALLOW_NULL_AUTHTOK
Instruct the authentication modules to return PAM_AUTH_ERR if
the user does not have a registered authorization token---it is
set to NULL in the system database.
The value returned by this function is one of the following:
PAM_AUTH_ERR
The user was not authenticated
PAM_CRED_INSUFFICIENT
For some reason the application does not have sufficient
credentials to authenticate the user.
PAM_AUTHINFO_UNAVAIL
The modules were not able to access the authentication
information. This might be due to a network or hardware failure
etc.
PAM_USER_UNKNOWN
The supplied username is not known to the authentication service
PAM_MAXTRIES
One or more of the authentication modules has reached its limit
of tries authenticating the user. Do not try again.
If one or more of the authentication modules fails to load, for
whatever reason, this function will return PAM_ABORT.
33..11..88.. SSeettttiinngg uusseerr ccrreeddeennttiiaallss
extern int pam_setcred(pam_handle_t *pamh, int flags);
This function is used to set the module-specific credentials of the
user. It is usually called after the user has been authenticated,
after the account management function has been called and after a
session has been opened for the user.
A credential is something that the user possesses. It is some
property, such as a _K_e_r_b_e_r_o_s ticket, or a supplementary group
membership that make up the uniqueness of a given user. On a Linux
(or UN*X system) the user's UID and GID's are credentials too.
However, it has been decided that these properties (along with the
default supplementary groups of which the user is a member) are
credentials that should be set directly by the application and not by
PAM.
This function simply calls the pam_sm_setcred functions of each of the
loaded modules. Valid flags, any one of which, may be logically OR'd
with PAM_SILENT, are:
PAM_ESTABLISH_CRED
Set the credentials for the authentication service,
PAM_DELETE_CRED
Delete the credentials associated with the authentication
service,
PAM_REINITIALIZE_CRED
Reinitialize the user credentials, and
PAM_REFRESH_CRED
Extend the lifetime of the user credentials.
A successful return is signalled with PAM_SUCCESS. Errors that are
especially relevant to this function are the following:
PAM_CRED_UNAVAIL
A module cannot retrieve the user's credentials.
PAM_CRED_EXPIRED
The user's credentials have expired.
PAM_USER_UNKNOWN
The user is not known to an authentication module.
PAM_CRED_ERR
A module was unable to set the credentials of the user.
33..11..99.. AAccccoouunntt mmaannaaggeemmeenntt
extern int pam_acct_mgmt(pam_handle_t *pamh, int flags);
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 PAM_SILENT,
and are the same as those applicable to the flags argument of
pam_authenticate.
This function simply calls the corresponding functions of each of the
loaded modules, as instructed by the configuration file,
/etc/pam.conf.
The normal response from this function is PAM_SUCCESS, however,
specific failures are indicated by the following error returns:
PAM_AUTHTOKEN_REQD
The user iiss valid but their authentication token has _e_x_p_i_r_e_d.
The correct response to this return-value is to require that the
user satisfies the pam_chauthtok() function before obtaining
service. It may not be possible for some applications to do
this. In such cases, the user should be denied access until
such time as they can update their password.
PAM_ACCT_EXPIRED
The user is no longer permitted access to the system.
PAM_AUTH_ERR
There was an authentication error.
PAM_PERM_DENIED
The user is not permitted to gain access at this time.
PAM_USER_UNKNOWN
The user is not known to a module's account management
component.
33..11..1100.. UUppddaattiinngg aauutthheennttiiccaattiioonn ttookkeennss
extern int pam_chauthtok(pam_handle_t *pamh, const int flags);
This function is used to change the authentication token for a given
user (as indicated by the state associated with the handle, pamh). The
following is a valid but optional flag which may be logically OR'd
with PAM_SILENT,
PAM_CHANGE_EXPIRED_AUTHTOK
This argument indicates to the modules that the users
authentication token (password) should only be changed if it has
expired.
Note, if this argument is not passed, the application requires that
_a_l_l authentication tokens are to be changed.
PAM_SUCCESS is the only successful return value, valid error-returns
are:
PAM_AUTHTOK_ERR
A module was unable to obtain the new authentication token.
PAM_AUTHTOK_RECOVER_ERR
A module was unable to obtain the old authentication token.
PAM_AUTHTOK_LOCK_BUSY
One or more of the modules was unable to change the
authentication token since it is currently locked.
PAM_AUTHTOK_DISABLE_AGING
Authentication token aging has been disabled for at least one of
the modules.
PAM_PERM_DENIED
Permission denied.
PAM_TRY_AGAIN
Not all of the modules were in a position to update the
authentication token(s). In such a case none of the user's
authentication tokens are updated.
PAM_USER_UNKNOWN
The user is not known to the authentication token changing
service.
33..11..1111.. SSeessssiioonn iinniittiiaalliizzaattiioonn
extern int pam_open_session(pam_handle_t *pamh, int flags);
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 LLiinnuuxx--PPAAMM library to open a
session and close the same session (see section ``below'') from
different applications.
Currently, this function simply calls each of the corresponding
functions of the loaded modules. The only valid flag is PAM_SILENT and
this is, of course, _o_p_t_i_o_n_a_l.
If any of the _r_e_q_u_i_r_e_d loaded modules are unable to open a session for
the user, this function will return PAM_SESSION_ERR.
33..11..1122.. TTeerrmmiinnaattiinngg sseessssiioonnss
extern int pam_close_session(pam_handle_t *pamh, int flags);
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 LLiinnuuxx--PPAAMM library to open a
session and close the same session from different applications.
Currently, this function simply calls each of the corresponding
functions of the loaded modules. The only valid flag is PAM_SILENT
and this is, of course, _o_p_t_i_o_n_a_l.
If any of the _r_e_q_u_i_r_e_d loaded modules are unable to close a session
for the user, this function will return PAM_SESSION_ERR.
33..11..1133.. SSeettttiinngg PPAAMM eennvviirroonnmmeenntt vvaarriiaabblleess
extern int pam_putenv(pam_handle_t *pamh, const char *name_value);
_W_a_r_n_i_n_g_, _t_h_e _e_n_v_i_r_o_n_m_e_n_t _s_u_p_p_o_r_t _i_n LLiinnuuxx--PPAAMM is based solely on a six
line email from the developers at Sun. Its interface is likely to be
generally correct, however, the details are likely to be changed as
more information becomes available.
This function attempts to (re)set a LLiinnuuxx--PPAAMM environment variable.
The name_value argument is a single NUL terminated string of one of
the following forms:
````NAME=value of variable''
In this case the environment variable of the given NAME is set
to the indicated value: ``value of variable''. If this variable
is already known, it is overwritten. Otherwise it is added to
the LLiinnuuxx--PPAAMM environment.
````NAME=''
This function sets the variable to an empty value. It is listed
separately to indicate that this is the correct way to achieve
such a setting.
````NAME''
Without an `=' the pam_putenv() function will delete the
correspoding variable from the LLiinnuuxx--PPAAMM environment.
Success is indicated with a return value of PAM_SUCCESS. Failure is
indicated by one of the following returns:
PAM_PERM_DENIED
name given is a NULL pointer
PAM_BAD_ITEM
variable requested (for deletion) is not currently set
PAM_ABORT
the LLiinnuuxx--PPAAMM handle, pamh, is corrupt
PAM_BUF_ERR
failed to allocate memory when attempting update
33..11..1144.. GGeettttiinngg aa PPAAMM eennvviirroonnmmeenntt vvaarriiaabbllee
extern const char *pam_getenv(pam_handle_t *pamh, const char *name);
_W_a_r_n_i_n_g_, _t_h_e _e_n_v_i_r_o_n_m_e_n_t _s_u_p_p_o_r_t _i_n LLiinnuuxx--PPAAMM is based solely on a
six-line email from the developers at Sun. Its interface is likely to
be generally correct, however, the details are likely to be changed as
more information becomes available.
Obtain the value of the indicated LLiinnuuxx--PPAAMM environment variable. On
error, internal failure or the unavailability of the given variable
(unspecified), this function simply returns NULL.
33..11..1155.. GGeettttiinngg tthhee PPAAMM eennvviirroonnmmeenntt
extern const char * const *pam_getenvlist(pam_handle_t *pamh);
_W_a_r_n_i_n_g_, _t_h_e _e_n_v_i_r_o_n_m_e_n_t _s_u_p_p_o_r_t _i_n LLiinnuuxx--PPAAMM is based solely on a six
line email from the developers at Sun. Its interface is likely to be
generally correct, however, the details are likely to be changed as
more information becomes available.
This function returns a pointer to the complete Linux-PAM environment.
It is a pointer to a _r_e_a_d_-_o_n_l_y list of _r_e_a_d_-_o_n_l_y environment
variables. It should be noted that this memory will become invalid
after a call to pam_end() (see the section ``above''). If application
wishes to make use of this list after such a call, it should first
make a copy of all the set variables. (A function that performs such a
transcription is to be found in libpam_misc.)
33..22.. WWhhaatt iiss eexxppeecctteedd ooff aann aapppplliiccaattiioonn
33..22..11.. TThhee ccoonnvveerrssaattiioonn ffuunnccttiioonn
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, pam_conv, is defined by including
<security/pam_appl.h>; to be,
struct pam_conv {
int (*conv)(int num_msg,
const struct pam_message **msg,
struct pam_response **resp,
void *appdata_ptr);
void *appdata_ptr;
};
It is initialized by the application before it is passed to the
library. The _c_o_n_t_e_n_t_s of this structure are attached to the *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 _c_o_n_v_e_r_s_a_t_i_o_n structure.
When a module calls the referenced conv() function, the argument
*appdata_ptr is set to the second element of this structure.
The other arguments of a call to conv() concern the information
exchanged by module and application. That is to say, num_msg holds the
length of the array of pointers, msg. After a successful return, the
pointer *resp points to an array of pam_response structures, holding
the application supplied text. Note, *resp is an struct pam_response
array and _n_o_t an array of pointers.
The message (from the module to the application) passing structure is
defined by <security/pam_appl.h> as:
struct pam_message {
int msg_style;
const char *msg;
};
Valid choices for msg_style are:
PAM_PROMPT_ECHO_OFF
Obtain a string without echoing any text
PAM_PROMPT_ECHO_ON
Obtain a string whilst echoing text
PAM_ERROR_MSG
Display an error
PAM_TEXT_INFO
Display some text.
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.
The response (from the application to the module) passing structure is
defined by including <security/pam_appl.h> as:
struct pam_response {
char *resp;
int resp_retcode;
};
Currently, there are no definitions for resp_retcode values; the
normal value is 0.
Prior to the 0.59 release of Linux-PAM, the length of the returned
pam_response array was equal to the number of _p_r_o_m_p_t_s (types
PAM_PROMPT_ECHO_OFF and PAM_PROMPT_ECHO_ON) in the pam_message array
with which the conversation function was called. This meant that it
was not always necessary for the module to free(3) the responses if
the conversation function was only used to display some text.
Post Linux-PAM-0.59 (and in the interests of compatibility with
Sunsoft). The number of resposes is always equal to the num_msg
conversation function argument. This is slightly easier to program
but does require that the response array is free(3)'d after every call
to the conversation function. The index of the responses corresponds
directly to the prompt index in the pam_message array.
The maximum length of the pam_msg.msg and pam_response.resp character
strings is PAM_MAX_MSG_SIZE. (This is not enforced by Linux-PAM.)
PAM_SUCCESS is the expected return value of this function. However,
should an error occur the application should not set *resp but simply
return PAM_CONV_ERR.
Note, if an application wishes to use two conversation functions, it
should activate the second with a call to pam_set_item().
NNootteess:: New item types are being added to the conversation protocol.
Currently Linux-PAM supports: PAM_BINARY_PROMPT and PAM_BINARY_MSG.
These two are intended for server-client hidden information exchange
and may be used as an interface for maching-machine authentication.
33..33.. PPrrooggrraammmmiinngg nnootteess
Note, all of the authentication service function calls accept the
token 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.
PAM_SILENT does not override the prompting of the user for passwords
etc., it only stops informative messages from being generated.
44.. SSeeccuurriittyy iissssuueess ooff LLiinnuuxx--PPAAMM
PAM, from the perspective of an application, is a convenient API for
authenticating users. PAM modules generally have no increased
privilege over that posessed by the application that is making use of
it. For this reason, the application must take ultimate responsibility
for protecting the environment in which PAM operates.
A poorly (or maliciously) written application can defeat any LLiinnuuxx--PPAAMM
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 LLiinnuuxx--PPAAMM library simply
assumes the responsibility of _a_u_t_h_e_n_t_i_c_a_t_i_n_g the user; ascertaining
that the user _i_s who they say they are. Care should be taken to
anticipate all of the documented behavior of the LLiinnuuxx--PPAAMM library
functions. A failure to do this will most certainly lead to a future
security breach.
44..11.. CCaarree aabboouutt ssttaannddaarrdd lliibbrraarryy ccaallllss
In general, writers of authorization-granting applications should
assume that each module is likely to call any or _a_l_l `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 `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 a call to the LLiinnuuxx--PPAAMM library, or copy the structure
contents to some safe area of memory before passing control to the
LLiinnuuxx--PPAAMM library.
Two important function classes that fall into this category are
getpwnam(3) and syslog(3).
44..22.. CChhooiiccee ooff aa sseerrvviiccee nnaammee
When picking the _s_e_r_v_i_c_e_-_n_a_m_e that corresponds to the first entry in
the LLiinnuuxx--PPAAMM configuration file, the application programmer should
aavvooiidd the temptation of choosing something related to argv[0]. It is
a trivial matter for any user to invoke any application on a system
under a different name and this should not be permitted to cause a
security breach.
To invoke some target application by another name, the user may
symbolically link the target application with the desired name. To be
precise all the user need do is,
ln -s /target/application ./preferred_name
and then _r_u_n ./preferred_name
By studying the LLiinnuuxx--PPAAMM configuration file(s), an attacker can
choose the preferred_name to be that of a service enjoying minimal
protection; for example a game which uses LLiinnuuxx--PPAAMM to restrict access
to certain hours of the day. If the service-name were to be linked to
the filename under which the service was invoked, it is clear that the
user is effectively in the position of dictating which authentication
scheme the service uses. Needless to say, this is not a secure
situation.
The conclusion is that the application developer should carefully
define the service-name of an application. The safest thing is to make
it a single hard-wired name.
44..33.. TThhee ccoonnvveerrssaattiioonn ffuunnccttiioonn
Care should be taken to ensure that the conv() function is robust.
Such a function is provided in the library libpam_misc (see
``below'').
44..44.. TThhee iiddeennttiittyy ooff tthhee uusseerr
The LLiinnuuxx--PPAAMM modules will need to determine the identity of the user
who requests a service, and the identity of the user who grants the
service. These two users will seldom be the same. Indeed there is
generally a third user identity to be considered, the new (assumed)
identity of the user once the service is granted.
The need for keeping tabs on these identities is clearly an issue of
security. One convention that is actively used by some modules is
that the identity of the user requesting a service should be the
current uid (userid) of the running process; the identity of the
privilege granting user is the euid (effective userid) of the running
process; the identity of the user, under whose name the service will
be executed, is given by the contents of the PAM_USER pam_get_item(3).
For network-serving databases and other applications that provide
their own security model (independent of the OS kernel) the above
scheme is insufficient to identify the requesting user.
A more portable solution to storing the identity of the requesting
user is to use the PAM_RUSER pam_get_item(3). The application should
supply this value before attempting to authenticate the user with
pam_authenticate(). How well this name can be trusted will ultimately
be at the discretion of the local administrator (who configures PAM
for your application) and a selected module may attempt to override
the value where it can obtain more reliable data. If an application is
unable to determine the identity of the requesting entity/user, it
should not call pam_set_item(3) to set PAM_RUSER.
In addition to the PAM_RUSER item, the application should supply the
PAM_RHOST (_r_e_q_u_e_s_t_i_n_g _h_o_s_t) item. As a general rule, the following
convention for its value can be assumed: <unset> = unknown; localhost
= invoked directly from the local system; _o_t_h_e_r_._p_l_a_c_e_._x_y_z = some
component of the user's connection originates from this
remote/requesting host. At present, PAM has no established convention
for indicating whether the application supports a trusted path to
communication from this host.
44..55.. SSuuffffiicciieenntt rreessoouurrcceess
Care should be taken to ensure that the proper execution of an
application is not compromised by a lack of system resources. If an
application is unable to open sufficient files to perform its service,
it should fail gracefully, or request additional resources.
Specifically, the quantities manipulated by the setrlimit(2) family of
commands should be taken into consideration.
This is also true of conversation prompts. The application should not
accept prompts of arbitrary length with out checking for resource
allocation failure and dealing with such extreme conditions gracefully
and in a mannor that preserves the PAM API. Such tolerance may be
especially important when attempting to track a malicious adversary.
55.. AA lliibbrraarryy ooff mmiisscceellllaanneeoouuss hheellppeerr ffuunnccttiioonnss
To aid the work of the application developer a library of
miscellaneous functions is provided. It is called libpam_misc, and
contains functions for allocating memory (securely), a text based
conversation function, and routines for enhancing the standard PAM-
environment variable support.
55..11.. RReeqquuiirreemmeennttss
The functions, structures and macros, made available by this library
can be defined by including <security/pam_misc.h>. It should be noted
that this library is specific to LLiinnuuxx--PPAAMM and is not referred to in
the defining DCE-RFC (see ``the bibliography'') below.
55..22.. FFuunnccttiioonnss ssuupppplliieedd
55..22..11.. SSaaffee ssttrriinngg dduupplliiccaattiioonn
extern char *xstrdup(const char *s)
Return a duplicate copy of the NUL terminated string, s. NULL is
returned if there is insufficient memory available for the duplicate
or if s=NULL.
55..22..22.. AA tteexxtt bbaasseedd ccoonnvveerrssaattiioonn ffuunnccttiioonn
extern int misc_conv(int num_msg, const struct pam_message **msgm,
struct pam_response **response, void *appdata_ptr);
This is a function that will prompt the user with the appropriate
comments and obtain the appropriate inputs as directed by
authentication modules.
In addition to simply slotting into the appropriate struct pam_conv,
this function provides some time-out facilities. The function exports
five variables that can be used by an application programmer to limit
the amount of time this conversation function will spend waiting for
the user to type something.
The five variables are as follows:
extern time_t pam_misc_conv_warn_time;
This variable contains the _t_i_m_e (as returned by time()) that the
user should be first warned that the clock is ticking. By
default it has the value 0, which indicates that no such warning
will be given. The application may set its value to sometime in
the future, but this should be done prior to passing control to
the LLiinnuuxx--PPAAMM library.
extern const char *pam_misc_conv_warn_line;
Used in conjuction with pam_misc_conv_warn_time, this variable
is a pointer to the string that will be displayed when it
becomes time to warn the user that the timeout is approaching.
Its default value is ``..\a.Time is running out...\n'', but this
can be changed by the application prior to passing control to
LLiinnuuxx--PPAAMM.
extern time_t pam_misc_conv_die_time;
This variable contains the _t_i_m_e (as returned by time()) that the
conversation will time out. By default it has the value 0, which
indicates that the conversation function will not timeout. The
application may set its value to sometime in the future, this
should be done prior to passing control to the LLiinnuuxx--PPAAMM
library.
extern const char *pam_misc_conv_die_line;
Used in conjuction with pam_misc_conv_die_time, this variable is
a pointer to the string that will be displayed when the
conversation times out. Its default value is ``..\a.Sorry, your
time is up!\n'', but this can be changed by the application
prior to passing control to LLiinnuuxx--PPAAMM.
extern int pam_misc_conv_died;
Following a return from the LLiinnuuxx--PPAAMM libraray, the value of
this variable indicates whether the conversation has timed out.
A value of 1 indicates the time-out occurred.
extern int (*pam_binary_handler_fn)(const union pam_u_packet_p
send,
union pam_u_packet_p *receive);" This function pointer is
initialized to NULL but can be filled with a function that
provides machine-machine (hidden) message exchange. It is
intended for use with hidden authentication protocols such as
RSA or Diffie-Hellman key exchanges. (This is still under
development.)
55..22..33.. TTrraannssccrriibbiinngg aann eennvviirroonnmmeenntt ttoo tthhaatt ooff LLiinnuuxx--PPAAMM
extern int pam_misc_paste_env(pam_handle_t *pamh,
const char * const * user_env);
This function takes the supplied list of environment pointers and
_u_p_l_o_a_d_s its contents to the LLiinnuuxx--PPAAMM environment. Success is
indicated by PAM_SUCCESS.
55..22..44.. SSaavviinngg tthhee LLiinnuuxx--PPAAMM eennvviirroonnmmeenntt ffoorr llaatteerr uussee
extern char **pam_misc_copy_env(pam_handle_t *pamh);
This function returns a pointer to a list of environment variables
that are a direct copy of the LLiinnuuxx--PPAAMM environment. The memory
associated with these variables are the responsibility of the
application and should be liberated with a call to
pam_misc_drop_env().
55..22..55.. LLiibbeerraattiinngg aa llooccaallllyy ssaavveedd eennvviirroonnmmeenntt
extern char **pam_misc_drop_env(char **env);
This function is defined to complement the pam_misc_copy_env()
function. It liberates the memory associated with env, _o_v_e_r_w_r_i_t_i_n_g
with 0 all memory before free()ing it.
55..22..66.. BBSSDD lliikkee LLiinnuuxx--PPAAMM eennvviirroonnmmeenntt vvaarriiaabbllee sseettttiinngg
extern int pam_misc_setenv(pam_handle_t *pamh, const char *name,
const char *value, int readonly);
This function performs a task equivalent to pam_putenv(), its syntax
is, however, more like the BSD style function; setenv(). The name and
value are concatenated with an ``='' to form a name_value and passed
to pam_putenv(). If, however, the LLiinnuuxx--PPAAMM variable is already set,
the replacement will only be applied if the last argument, readonly,
is zero.
66.. PPoorrttiinngg lleeggaaccyy aapppplliiccaattiioonnss
The following is extracted from an email. I'll tidy it up later.
The point of PAM is that the application is not supposed to have any
idea how the attatched authentication modules will choose to
authenticate the user. So all they can do is provide a conversation
function that will talk directly to the user(client) on the modules'
behalf.
Consider the case that you plug a retinal scanner into the login
program. In this situation the user would be prompted: "please look
into the scanner". No username or password would be needed - all this
information could be deduced from the scan and a database lookup. The
point is that the retinal scanner is an ideal task for a "module".
While it is true that a pop-daemon program is designed with the POP
protocol in mind and no-one ever considered attatching a retinal
scanner to it, it is also the case that the "clean" PAM'ification of
such a daemon would allow for the possibility of a scanner module
being be attatched to it. The point being that the "standard" pop-
authentication protocol(s) [which will be needed to satisfy
inflexible/legacy clients] would be supported by inserting an
appropriate pam_qpopper module(s). However, having rewritten popd
once in this way any new protocols can be implemented in-situ.
One simple test of a ported application would be to insert the
pam_permit module and see if the application demands you type a
password... In such a case, xlock would fail to lock the terminal -
or would at best be a screen-saver, ftp would give password free
access to all etc.. Neither of these is a very secure thing to do,
but they do illustrate how much flexibility PAM puts in the hands of
the local admin.
The key issue, in doing things correctly, is identifying what is part
of the authentication procedure (how many passwords etc..) the
exchange protocol (prefixes to prompts etc., numbers like 331 in the
case of ftpd) and what is part of the service that the application
delivers. PAM really needs to have total control in the
authentication "proceedure", the conversation function should only
deal with reformatting user prompts and extracting responses from raw
input.
77.. GGlloossssaarryy ooff PPAAMM rreellaatteedd tteerrmmss
The following are a list of terms used within this document.
AAuutthheennttiiccaattiioonn ttookkeenn
Generally, this is a password. However, a user can authenticate
him/herself in a variety of ways. Updating the user's
authentication token thus corresponds to _r_e_f_r_e_s_h_i_n_g the object
they use to authenticate themself with the system. The word
password is avoided to keep open the possibility that the
authentication involves a retinal scan or other non-textual mode
of challenge/response.
CCrreeddeennttiiaallss
Having successfully authenticated the user, PAM is able to
establish certain characteristics/attributes of the user. These
are termed _c_r_e_d_e_n_t_i_a_l_s. Examples of which are group memberships
to perform privileged tasks with, and _t_i_c_k_e_t_s in the form of
environment variables etc. . Some user-credentials, such as the
user's UID and GID (plus default group memberships) are not
deemed to be PAM-credentials. It is the responsibility of the
application to grant these directly.
88.. AAnn eexxaammppllee aapppplliiccaattiioonn
To get a flavor of the way a Linux-PAM application is written we
include the following example. It prompts the user for their password
and indicates whether their account is valid on the standard output,
its return code also indicates the success (0 for success; 1 for
failure).
/*
This program was contributed by Shane Watts
[modifications by AGM]
You need to add the following (or equivalent) to the /etc/pam.conf file.
# check authorization
check_user auth required /usr/lib/security/pam_unix_auth.so
check_user account required /usr/lib/security/pam_unix_acct.so
*/
#include <security/pam_appl.h>
#include <security/pam_misc.h>
#include <stdio.h>
static struct pam_conv conv = {
misc_conv,
NULL
};
int main(int argc, char *argv[])
{
pam_handle_t *pamh=NULL;
int retval;
const char *user="nobody";
if(argc == 2) {
user = argv[1];
}
if(argc > 2) {
fprintf(stderr, "Usage: check_user [username]\n");
exit(1);
}
retval = pam_start("check_user", user, &conv, &pamh);
if (retval == PAM_SUCCESS)
retval = pam_authenticate(pamh, 0); /* is user really user? */
if (retval == PAM_SUCCESS)
retval = pam_acct_mgmt(pamh, 0); /* permitted access? */
/* This is where we have been authorized or not. */
if (retval == PAM_SUCCESS) {
fprintf(stdout, "Authenticated\n");
} else {
fprintf(stdout, "Not Authenticated\n");
}
if (pam_end(pamh,retval) != PAM_SUCCESS) { /* close Linux-PAM */
pamh = NULL;
fprintf(stderr, "check_user: failed to release authenticator\n");
exit(1);
}
return ( retval == PAM_SUCCESS ? 0:1 ); /* indicate success */
}
99.. FFiilleess
/usr/include/security/pam_appl.h
header file for LLiinnuuxx--PPAAMM applications interface
/usr/include/security/pam_misc.h
header file for useful library functions for making applications
easier to write
/usr/lib/libpam.so.*
the shared library providing applications with access to LLiinnuuxx--
PPAAMM.
/etc/pam.conf
the LLiinnuuxx--PPAAMM configuration file.
/usr/lib/security/pam_*.so
the primary location for LLiinnuuxx--PPAAMM dynamically loadable object
files; the modules.
1100.. SSeeee aallssoo
+o The LLiinnuuxx--PPAAMM System Administrators' Guide.
+o The LLiinnuuxx--PPAAMM Module Writers' Guide.
+o The V. Samar and R. Schemers (SunSoft), ``UNIFIED LOGIN WITH
PLUGGABLE AUTHENTICATION MODULES'', Open Software Foundation
Request For Comments 86.0, October 1995.
1111.. NNootteess
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! :)
+o pam_strerror() should be internationalized....
+o Note, the resp_retcode of struct pam_message, has no purpose at the
moment. Ideas/suggestions welcome!
+o more security issues are required....
1122.. AAuutthhoorr//aacckknnoowwlleeddggmmeennttss
This document was written by Andrew G. Morgan (morgan@transmeta.com)
with many contributions from Chris Adams, Peter Allgeyer, Tim
Baverstock, Tim Berger, Craig S. Bell, Derrick J. Brashear, Ben
Buxton, Seth Chaiklin, Oliver Crow, Chris Dent, Marc Ewing, Cristian
Gafton, Emmanuel Galanos, Brad M. Garcia, Eric Hester, Roger Hu, Eric
Jacksch, Michael K. Johnson, David Kinchlea, Olaf Kirch, Marcin
Korzonek, Stephen Langasek, Nicolai Langfeldt, Elliot Lee, Luke
Kenneth Casson Leighton, Al Longyear, Ingo Luetkebohle, Marek
Michalkiewicz, Robert Milkowski, Aleph One, Martin Pool, Sean
Reifschneider, Jan Rekorajski, Erik Troan, Theodore Ts'o, Jeff Uphoff,
Myles Uyema, Savochkin Andrey Vladimirovich, Ronald Wahl, David Wood,
John Wilmes, Joseph S. D. Yao and Alex O. Yuriev.
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
LLiinnuuxx--PPAAMM, Sun graciously made the documentation for their
implementation of PAM available. This act greatly accelerated the
development of LLiinnuuxx--PPAAMM.
1133.. BBuuggss//oommiissssiioonnss
This manual is hopelessly unfinished. Only a partial list of people is
credited for all the good work they have done.
1144.. CCooppyyrriigghhtt iinnffoorrmmaattiioonn ffoorr tthhiiss ddooccuummeenntt
Copyright (c) Andrew G. Morgan 1996-9. All rights reserved.
Email: <morgan@transmeta.com>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
+o 1. Redistributions of source code must retain the above copyright
notice, and the entire permission notice in its entirety, including
the disclaimer of warranties.
+o 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.
+o 3. The name of the author may not be used to endorse or promote
products derived from this software without specific prior written
permission.
AAlltteerrnnaattiivveellyy, this product may be distributed under the terms of the
GNU General Public License (GPL), in which case the provisions of the
GNU GPL are required iinnsstteeaadd ooff 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.
$Id: pam_appl.sgml,v 1.6 1999/11/08 05:09:17 morgan Exp $