3.1. What can be expected by the application

3.1.1. Initialization of PAM transaction

#include <security/pam_appl.h>
int pam_start(service_name,  
 user,  
 pam_conversation,  
 pamh); 
const char *service_name;
const char *user;
const struct pam_conv *pam_conversation;
pam_handle_t **pamh;
 

3.1.1.1. DESCRIPTION

The pam_start function creates the PAM context and initiates the PAM transaction. It is the first of the PAM functions that needs to be called by an application. The transaction state is contained entirely within the structure identified by this handle, so it is possible to have multiple transactions in parallel. But it is not possible to use the same handle for different transactions, a new one is needed for every new context.

The service_name argument specifies the name of the service to apply and will be stored as PAM_SERVICE item in the new context. The policy for the service will be read from the file /etc/pam.d/service_name or, if that file does not exist, from /etc/pam.conf.

The user argument can specify the name of the target user and will be stored as PAM_USER item. If the argument is NULL, the module has to ask for this item if necessary.

The pam_conversation argument points to a struct pam_conv describing the conversation function to use. An application must provide this for direct communication between a loaded module and the application.

Following a successful return (PAM_SUCCESS) the contents of pamh is a handle that contains the PAM context for successive calls to the PAM functions. In an error case is the content of pamh undefined.

The pam_handle_t is a blind structure and the application should not attempt to probe it directly for information. Instead the PAM library provides the functions pam_set_item(3) and pam_get_item(3). The PAM handle cannot be used for mulitiple authentications at the same time as long as pam_end was not called on it before.

3.1.1.2. RETURN VALUES

PAM_ABORT

General failure.

PAM_BUF_ERR

Memory buffer error.

PAM_SUCCESS

Transaction was successful created.

PAM_SYSTEM_ERR

System error, for example a NULL pointer was submitted instead of a pointer to data.

3.1.2. Termination of PAM transaction

#include <security/pam_appl.h>
int pam_end(pamh,  
 pam_status); 
pam_handle_t *pamh;
int pam_status;
 

3.1.2.1. DESCRIPTION

The pam_end function terminates the PAM transaction and is the last function an application should call in the PAM context. Upon return the handle pamh is no longer valid and all memory associated with it will be invalid.

The pam_status argument should be set to the value returned to the application by the last PAM library call.

The value taken by pam_status is used as an argument to the module specific callback function, cleanup() (See pam_set_data(3) and pam_get_data(3)). In this way the module can be given notification of the pass/fail nature of the tear-down process, and perform any last minute tasks that are appropriate to the module before it is unlinked. This argument can be logically OR'd with PAM_DATA_SILENT to indicate to indicate that the module should not treat the call too seriously. It is generally used to indicate that the current closing of the library is in a fork(2)ed process, and that the parent will take care of cleaning up things that exist outside of the current process space (files etc.).

This function free's all memory for items associated with the pam_set_item(3) and pam_get_item(3) functions. Pointers associated with such objects are not valid anymore after pam_end was called.

3.1.2.2. RETURN VALUES

PAM_SUCCESS

Transaction was successful terminated.

PAM_SYSTEM_ERR

System error, for example a NULL pointer was submitted as PAM handle or the function was called by a module.

3.1.3. Setting PAM items

#include <security/pam_modules.h>
int pam_set_item(pamh,  
 item_type,  
 item); 
pam_handle_t *pamh;
int item_type;
const void *item;
 

3.1.3.1. DESCRIPTION

The pam_set_item function allows applications and PAM service modules to access and to update PAM informations of item_type. For this a copy of the object pointed to by the item argument is created. The following item_types are supported:

PAM_SERVICE

The service name (which identifies that PAM stack that the PAM functions will use to authenticate the program).

PAM_USER

The username of the entity under whose identity service will be given. That is, following authentication, PAM_USER identifies the local entity that gets to use the service. Note, this value can be mapped from something (eg., "anonymous") to something else (eg. "guest119") by any module in the PAM stack. As such an application should consult the value of PAM_USER after each call to a PAM function.

PAM_USER_PROMPT

The string used when prompting for a user's name. The default value for this string is a localized version of "login: ".

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_RUSER

The requesting user name: local name for a locally requesting user or a remote user name for a remote requesting user.

Generally an application or module will attempt to supply the value that is most strongly authenticated (a local account before a remote one. The level of trust in this value is embodied in the actual authentication stack associated with the application, so it is ultimately at the discretion of the system administrator.

PAM_RUSER@PAM_RHOST should always identify the requesting user. In some cases, PAM_RUSER may be NULL. In such situations, it is unclear who the requesting entity is.

PAM_RHOST

The requesting hostname (the hostname of the machine from which the PAM_RUSER entity is requesting service). That is PAM_RUSER@PAM_RHOST does identify the requesting user. In some applications, PAM_RHOST may be NULL. In such situations, it is unclear where the authentication request is originating from.

PAM_AUTHTOK

The authentication token (often a password). This token should be ignored by all module functions besides pam_sm_authenticate(3) and pam_sm_chauthtok(3). In the former function it is used to pass the most recent authentication token from one stacked module to another. In the latter function the token is used for another purpose. It contains the currently active authentication token.

PAM_OLDAUTHTOK

The old authentication token. This token should be ignored by all module functions except pam_sm_chauthtok(3).

PAM_CONV

The pam_conv structure. See pam_conv(3).

The following additional items are specific to Linux-PAM and should not be used in portable applications:

PAM_FAIL_DELAY

A function pointer to redirect centrally managed failure delays. See pam_fail_delay(3).

PAM_XDISPLAY

The name of the X display. For graphical, X-based applications the value for this item should be the $DISPLAY variable. This value may be used independently of PAM_TTY for passing the name of the display.

PAM_XAUTHDATA

A pointer to a structure containing the X authentication data required to make a connection to the display specified by PAM_XDISPLAY, if such information is necessary. See pam_xauth_data(3).

PAM_AUTHTOK_TYPE

The default action is for the module to use the following prompts when requesting passwords: "New UNIX password: " and "Retype UNIX password: ". The example word UNIX can be replaced with this item, by default it is empty. This item is used by pam_get_authtok(3).

For all item_types, other than PAM_CONV and PAM_FAIL_DELAY, item is a pointer to a <NUL> terminated character string. In the case of PAM_CONV, item points to an initialized pam_conv structure. In the case of PAM_FAIL_DELAY, item is a function pointer: void (*delay_fn)(int retval, unsigned usec_delay, void *appdata_ptr)

Both, PAM_AUTHTOK and PAM_OLDAUTHTOK, will be reseted before returning to the application. Which means an application is not able to access the authentication tokens.

3.1.3.2. RETURN VALUES

PAM_BAD_ITEM

The application attempted to set an undefined or inaccessible item.

PAM_BUF_ERR

Memory buffer error.

PAM_SUCCESS

Data was successful updated.

PAM_SYSTEM_ERR

The pam_handle_t passed as first argument was invalid.

3.1.4. Getting PAM items

#include <security/pam_modules.h>
int pam_get_item(pamh,  
 item_type,  
 item); 
const pam_handle_t *pamh;
int item_type;
const void **item;
 

3.1.4.1. DESCRIPTION

The pam_get_item function allows applications and PAM service modules to access and retrieve PAM informations of item_type. Upon successful return, item contains a pointer to the value of the corresponding item. Note, this is a pointer to the actual data and should not be free()'ed or over-written! The following values are supported for item_type:

PAM_SERVICE

The service name (which identifies that PAM stack that the PAM functions will use to authenticate the program).

PAM_USER

The username of the entity under whose identity service will be given. That is, following authentication, PAM_USER identifies the local entity that gets to use the service. Note, this value can be mapped from something (eg., "anonymous") to something else (eg. "guest119") by any module in the PAM stack. As such an application should consult the value of PAM_USER after each call to a PAM function.

PAM_USER_PROMPT

The string used when prompting for a user's name. The default value for this string is a localized version of "login: ".

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_RUSER

The requesting user name: local name for a locally requesting user or a remote user name for a remote requesting user.

Generally an application or module will attempt to supply the value that is most strongly authenticated (a local account before a remote one. The level of trust in this value is embodied in the actual authentication stack associated with the application, so it is ultimately at the discretion of the system administrator.

PAM_RUSER@PAM_RHOST should always identify the requesting user. In some cases, PAM_RUSER may be NULL. In such situations, it is unclear who the requesting entity is.

PAM_RHOST

The requesting hostname (the hostname of the machine from which the PAM_RUSER entity is requesting service). That is PAM_RUSER@PAM_RHOST does identify the requesting user. In some applications, PAM_RHOST may be NULL. In such situations, it is unclear where the authentication request is originating from.

PAM_AUTHTOK

The authentication token (often a password). This token should be ignored by all module functions besides pam_sm_authenticate(3) and pam_sm_chauthtok(3). In the former function it is used to pass the most recent authentication token from one stacked module to another. In the latter function the token is used for another purpose. It contains the currently active authentication token.

PAM_OLDAUTHTOK

The old authentication token. This token should be ignored by all module functions except pam_sm_chauthtok(3).

PAM_CONV

The pam_conv structure. See pam_conv(3).

The following additional items are specific to Linux-PAM and should not be used in portable applications:

PAM_FAIL_DELAY

A function pointer to redirect centrally managed failure delays. See pam_fail_delay(3).

PAM_XDISPLAY

The name of the X display. For graphical, X-based applications the value for this item should be the $DISPLAY variable. This value may be used independently of PAM_TTY for passing the name of the display.

PAM_XAUTHDATA

A pointer to a structure containing the X authentication data required to make a connection to the display specified by PAM_XDISPLAY, if such information is necessary. See pam_xauth_data(3).

PAM_AUTHTOK_TYPE

The default action is for the module to use the following prompts when requesting passwords: "New UNIX password: " and "Retype UNIX password: ". The example word UNIX can be replaced with this item, by default it is empty. This item is used by pam_get_authtok(3).

If a service module wishes to obtain the name of the user, it should not use this function, but instead perform a call to pam_get_user(3).

Only a service module is privileged to read the authentication tokens, PAM_AUTHTOK and PAM_OLDAUTHTOK.

3.1.4.2. RETURN VALUES

PAM_BAD_ITEM

The application attempted to set an undefined or inaccessible item.

PAM_BUF_ERR

Memory buffer error.

PAM_PERM_DENIED

The value of item was NULL.

PAM_SUCCESS

Data was successful updated.

PAM_SYSTEM_ERR

The pam_handle_t passed as first argument was invalid.

3.1.5. Strings describing PAM error codes

#include <security/pam_appl.h>
const char *pam_strerror(pamh,  
 errnum); 
pam_handle_t *pamh;
int errnum;
 

3.1.5.1. DESCRIPTION

The pam_strerror function returns a pointer to a string describing the error code passed in the argument errnum, possibly using the LC_MESSAGES part of the current locale to select the appropriate language. This string must not be modified by the application. No library function will modify this string.

3.1.5.2. RETURN VALUES

This function returns always a pointer to a string.

3.1.6. Request a delay on failure

#include <security/pam_appl.h>
int pam_fail_delay(pamh,  
 usec); 
pam_handle_t *pamh;
unsigned int usec;
 

3.1.6.1. DESCRIPTION

The pam_fail_delay function provides a mechanism by which an application or module can suggest a minimum delay of usec micro-seconds. The function keeps a record of the longest time requested with this function. Should pam_authenticate(3) fail, the failing return to the application is delayed by an amount of time randomly distributed (by up to 50%) about this longest value.

Independent of success, the delay time is reset to its zero default value when the PAM service module returns control to the application. The delay occurs after all authentication modules have been called, but before control is returned to the service application.

When using this function the programmer should check if it is available with:

#ifdef HAVE_PAM_FAIL_DELAY
    ....
#endif /* HAVE_PAM_FAIL_DELAY */
      

For applications written with a single thread that are event driven in nature, generating this delay 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 the delay function can be changed with the PAM_FAIL_DELAY item. It can be queried and set with pam_get_item(3) and pam_set_item (3) 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. This last value was set by the application when it called pam_start(3) or explicitly with pam_set_item(3). Note, if PAM_FAIL_DELAY item is unset (or set to NULL), then no delay will be performed.

3.1.6.2. RETURN VALUES

PAM_SUCCESS

Delay was successful adjusted.

PAM_SYSTEM_ERR

A NULL pointer was submitted as PAM handle.

3.1.7. Authenticating the user

#include <security/pam_appl.h>
int pam_authenticate(pamh,  
 flags); 
pam_handle_t *pamh;
int flags;
 

3.1.7.1. DESCRIPTION

The pam_authenticate function is used to authenticate the user. The user is required to provide an authentication token depending upon the authentication service, usually this is a password, but could also be a finger print.

The PAM service module may request that the user enter their username via the conversation mechanism (see pam_start(3) and pam_conv(3)). The name of the authenticated user will be present in the PAM item PAM_USER. This item may be recovered with a call to pam_get_item(3).

The pamh argument is an authentication handle obtained by a prior call to pam_start(). The flags argument is the binary or of zero or more of the following values:

PAM_SILENT

Do not emit any messages.

PAM_DISALLOW_NULL_AUTHTOK

The PAM module service should return PAM_AUTH_ERR if the user does not have a registered authentication token.

3.1.7.2. RETURN VALUES

PAM_ABORT

The application should exit immediately after calling pam_end(3) first.

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_MAXTRIES

One or more of the authentication modules has reached its limit of tries authenticating the user. Do not try again.

PAM_SUCCESS

The user was successfully authenticated.

PAM_USER_UNKNOWN

User unknown to authentication service.

3.1.8. Setting user credentials

#include <security/pam_appl.h>
int pam_setcred(pamh,  
 flags); 
pam_handle_t *pamh;
int flags;
 

3.1.8.1. DESCRIPTION

The pam_setcred function is used to establish, maintain and delete the credentials of a user. It should be called to set the credentials after a user has been authenticated and before a session is opened for the user (with pam_open_session(3)). The credentials should be deleted after the session has been closed (with pam_close_session(3)).

A credential is something that the user possesses. It is some property, such as a Kerberos ticket, or a supplementary group membership that make up the uniqueness of a given user. On a Linux 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. Such credentials should be established, by the application, prior to a call to this function. For example, initgroups(2) (or equivalent) should have been performed.

Valid flags, any one of which, may be logically OR'd with PAM_SILENT, are:

PAM_ESTABLISH_CRED

Initialize the credentials for the user.

PAM_DELETE_CRED

Delete the user's credentials.

PAM_REINITIALIZE_CRED

Fully reinitialize the user's credentials.

PAM_REFRESH_CRED

Extend the lifetime of the existing credentials.

3.1.8.2. RETURN VALUES

PAM_BUF_ERR

Memory buffer error.

PAM_CRED_ERR

Failed to set user credentials.

PAM_CRED_EXPIRED

User credentials are expired.

PAM_CRED_UNAVAIL

Failed to retrieve user credentials.

PAM_SUCCESS

Data was successful stored.

PAM_SYSTEM_ERR

A NULL pointer was submitted as PAM handle, the function was called by a module or another system error occured.

PAM_USER_UNKNOWN

User is not known to an authentication module.

3.1.9. Account validation management

#include <security/pam_appl.h>
int pam_acct_mgmt(pamh,  
 flags); 
pam_handle_t *pamh;
int flags;
 

3.1.9.1. DESCRIPTION

The pam_acct_mgmt function is used to determine if the user's account is valid. It checks for authentication token and account expiration and verifies access restrictions. It is typically called after the user has been authenticated.

The pamh argument is an authentication handle obtained by a prior call to pam_start(). The flags argument is the binary or of zero or more of the following values:

PAM_SILENT

Do not emit any messages.

PAM_DISALLOW_NULL_AUTHTOK

The PAM module service should return PAM_NEW_AUTHTOK_REQD if the user has a null authentication token.

3.1.9.2. RETURN VALUES

PAM_ACCT_EXPIRED

User account has expired.

PAM_AUTH_ERR

Authentication failure.

PAM_NEW_AUTHTOK_REQD

The user account is valid but their authentication token is expired. 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_PERM_DENIED

Permission denied.

PAM_SUCCESS

The authentication token was successfully updated.

PAM_USER_UNKNOWN

User unknown to password service.

3.1.10. Updating authentication tokens

#include <security/pam_appl.h>
int pam_chauthtok(pamh,  
 flags); 
pam_handle_t *pamh;
int flags;
 

3.1.10.1. DESCRIPTION

The pam_chauthtok function is used to change the authentication token for a given user (as indicated by the state associated with the handle pamh).

The pamh argument is an authentication handle obtained by a prior call to pam_start(). The flags argument is the binary or of zero or more of the following values:

PAM_SILENT

Do not emit any messages.

PAM_CHANGE_EXPIRED_AUTHTOK

This argument indicates to the modules that the user's authentication token (password) should only be changed if it has expired. If this argument is not passed, the application requires that all authentication tokens are to be changed.

3.1.10.2. RETURN VALUES

PAM_AUTHTOK_ERR

A module was unable to obtain the new authentication token.

PAM_AUTHTOK_RECOVERY_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_SUCCESS

The authentication token was successfully updated.

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

User unknown to password service.

3.1.11. Start PAM session management

#include <security/pam_appl.h>
int pam_open_session(pamh,  
 flags); 
pam_handle_t *pamh;
int flags;
 

3.1.11.1. DESCRIPTION

The pam_open_session function sets up a user session for a previously successful authenticated user. The session should later be terminated with a call to pam_close_session(3).

It should be noted that the effective uid, geteuid(2). of the application should be of sufficient privilege to perform such tasks as creating or mounting the user's home directory for example.

The flags argument is the binary or of zero or more of the following values:

PAM_SILENT

Do not emit any messages.

3.1.11.2. RETURN VALUES

PAM_ABORT

General failure.

PAM_BUF_ERR

Memory buffer error.

PAM_SESSION_ERR

Session failure.

PAM_SUCCESS

Session was successful created.

3.1.12. terminating PAM session management

#include <security/pam_appl.h>
int pam_close_session(pamh,  
 flags); 
pam_handle_t *pamh;
int flags;
 

3.1.12.1. DESCRIPTION

The pam_close_session function is used to indicate that an authenticated session has ended. The session should have been created with a call to pam_open_session(3).

It should be noted that the effective uid, geteuid(2). of the application should be of sufficient privilege to perform such tasks as unmounting the user's home directory for example.

The flags argument is the binary or of zero or more of the following values:

PAM_SILENT

Do not emit any messages.

3.1.12.2. RETURN VALUES

PAM_ABORT

General failure.

PAM_BUF_ERR

Memory buffer error.

PAM_SESSION_ERR

Session failure.

PAM_SUCCESS

Session was successful terminated.

3.1.13. Set or change PAM environment variable

#include <security/pam_appl.h>
int pam_putenv(pamh,  
 name_value); 
pam_handle_t *pamh;
const char *name_value;
 

3.1.13.1. DESCRIPTION

The pam_putenv function is used to add or change the value of PAM environment variables as associated with the pamh handle.

The pamh argument is an authentication handle obtained by a prior call to pam_start(). 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 PAM 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 corresponding variable from the PAM environment.

pam_putenv() operates on a copy of name_value, which means in contrast to putenv(3), the application is responsible to free the data.

3.1.13.2. RETURN VALUES

PAM_PERM_DENIED

Argument name_value given is a NULL pointer.

PAM_BAD_ITEM

Variable requested (for deletion) is not currently set.

PAM_ABORT

The pamh handle is corrupt.

PAM_BUF_ERR

Memory buffer error.

PAM_SUCCESS

The environment variable was successfully updated.

3.1.14. Get a PAM environment variable

#include <security/pam_appl.h>
const char *pam_getenv(pamh,  
 name); 
pam_handle_t *pamh;
const char *name;
 

3.1.14.1. DESCRIPTION

The pam_getenv function searches the PAM environment list as associated with the handle pamh for an item that matches the string pointed to by name and returns a pointer to the value of the environment variable. The application is not allowed to free the data.

3.1.14.2. RETURN VALUES

The pam_getenv function returns NULL on failure.

3.1.15. Getting the PAM environment

#include <security/pam_appl.h>
char **pam_getenvlist(pamh); 
pam_handle_t *pamh;
 

3.1.15.1. DESCRIPTION

The pam_getenvlist function returns a complete copy of the PAM environment as associated with the handle pamh. The PAM environment variables represent the contents of the regular environment variables of the authenticated user when service is granted.

The format of the memory is a malloc()'d array of char pointers, the last element of which is set to NULL. Each of the non-NULL entries in this array point to a NUL terminated and malloc()'d char string of the form: "name=value".

It should be noted that this memory will never be free()'d by libpam. Once obtained by a call to pam_getenvlist, it is the responsibility of the calling application to free() this memory.

It is by design, and not a coincidence, that the format and contents of the returned array matches that required for the third argument of the execle(3) function call.

3.1.15.2. RETURN VALUES

The pam_getenvlist function returns NULL on failure.