Next Previous Contents

3. The public interface to Linux-PAM

Firstly, the relevant include file for the Linux-PAM library is {[lt ]}security/pam{[lowbar]}appl.h{[gt ]}. It contains the definitions for a number of functions. After listing these functions, we collect some guiding remarks for programmers.

3.1 What can be expected by the application

Below we document those functions in the Linux-PAM library that may be called from an application.

Initialization of Linux-PAM

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 Linux-PAM functions that must be called by an application. It initializes the interface and reads the system configuration file, /etc/pam.conf (see the Linux-PAM System Administrators' Guide). Following a successful return (PAM{[lowbar]}SUCCESS) the contents of *pamh is a handle that provides continuity for successive calls to the Linux-PAM library. The arguments expected by pam{[lowbar]}start are as follows: the service{[lowbar]}name of the program, the username of the individual to be authenticated, a pointer to an application-supplied pam{[lowbar]}conv structure and a pointer to a pam{[lowbar]}handle{[lowbar]}t pointer.

The pam{[lowbar]}conv structure is discussed more fully in the section below. The pam{[lowbar]}handle{[lowbar]}t is a blind structure and the application should not attempt to probe it directly for information. Instead the Linux-PAM library provides the functions pam{[lowbar]}set{[lowbar]}item and pam{[lowbar]}get{[lowbar]}item. These functions are documented below.

Termination of the library

extern int pam_end(pam_handle_t *pamh, int pam_status);

This function is the last function an application should call in the Linux-PAM 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{[lowbar]}status has the value PAM{[lowbar]}SUCCESS, but in the event of an unsuccessful application for service the appropriate Linux-PAM error-return value should be used here. Note, pam{[lowbar]}end() unconditionally shuts down the authentication stack associated with the pamh handle. The value taken by pam{[lowbar]}status is used as an argument to the module specific callback functions, cleanup() (see the Linux-PAM Module Developers' Guide). 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.

Setting PAM items

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 item{[lowbar]}types:

PAM{[lowbar]}SERVICE

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

PAM{[lowbar]}USER

The username of the entity under who's identity service will be given. That is, following authentication, PAM{[lowbar]}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{[lowbar]}USER after each call to a pam{[lowbar]}*() function.

PAM{[lowbar]}USER{[lowbar]}PROMPT

The string used when prompting for a user's name. The default value for this string is ``Please enter username: ''.

PAM{[lowbar]}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 {[dollar]}DISPLAY variable.

PAM{[lowbar]}RUSER

The requesting entity: user's username for a locally requesting user or 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. It should generally match the current PAM{[lowbar]}RHOST value. That is, "PAM{[lowbar]}RUSER@PAM{[lowbar]}RHOST" should always identify the requesting user. In some cases, PAM{[lowbar]}RUSER may be NULL. In such situations, it is unclear who the requesting entity is.

PAM{[lowbar]}RHOST

The requesting hostname (the hostname of the machine from which the PAM{[lowbar]}RUSER entity is requesting service). That is "PAM{[lowbar]}RUSER@PAM{[lowbar]}RHOST" does identify the requesting user. "luser@localhost" or "evil@evilcom.com" are valid "PAM{[lowbar]}RUSER@PAM{[lowbar]}RHOST" examples. In some applications, PAM{[lowbar]}RHOST may be NULL. In such situations, it is unclear where the authentication request is originating from.

PAM{[lowbar]}CONV

The conversation structure (see section below).

PAM{[lowbar]}FAIL{[lowbar]}DELAY

A function pointer to redirect centrally managed failure delays (see section below).

For all item{[lowbar]}types, other than PAM{[lowbar]}CONV and PAM{[lowbar]}FAIL{[lowbar]}DELAY, item is a pointer to a {[lt ]}NUL{[gt ]} terminated character string. In the case of PAM{[lowbar]}CONV, item points to an initialized pam{[lowbar]}conv structure (see section below). In the case of PAM{[lowbar]}FAIL{[lowbar]}DELAY, item is a function pointer: void (*delay{[lowbar]}fn)(int retval, unsigned usec{[lowbar]}delay, void *appdata{[lowbar]}ptr) (see section below).

A successful call to this function returns PAM{[lowbar]}SUCCESS. However, the application should expect at least one the following errors:

PAM{[lowbar]}SYSTEM{[lowbar]}ERR

The pam{[lowbar]}handle{[lowbar]}t passed as a first argument to this function was invalid.

PAM{[lowbar]}PERM{[lowbar]}DENIED

An attempt was made to replace the conversation structure with a NULL value.

PAM{[lowbar]}BUF{[lowbar]}ERR

The function ran out of memory making a copy of the item.

PAM{[lowbar]}BAD{[lowbar]}ITEM

The application attempted to set an undefined or inaccessible item.

Getting PAM items

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{[lowbar]}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!

A successful call is signaled by a return value of PAM{[lowbar]}SUCCESS. However, the application should expect one of the following errors:

PAM{[lowbar]}SYSTEM{[lowbar]}ERR

The pam{[lowbar]}handle{[lowbar]}t passed as a first argument to this function was invalid.

PAM{[lowbar]}PERM{[lowbar]}DENIED

The value of item was NULL.

PAM{[lowbar]}BAD{[lowbar]}ITEM

The application attempted to set an undefined or inaccessible item.

In the case of an error, the contents of item is set to NULL.

Understanding errors

extern const char *pam_strerror(pam_handle_t *pamh, int errnum);

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

Planning for delays

extern int pam_fail_delay(pam_handle_t *pamh, unsigned int micro_sec);

This function is offered by Linux-PAM to facilitate time delays following a failed call to pam{[lowbar]}authenticate() and before control is returned to the application. When using this function the application programmer should check if it is available with,

#ifdef PAM_FAIL_DELAY
    ....
#endif /* PAM_FAIL_DELAY */

Generally, an application requests that a user is authenticated by Linux-PAM through a call to pam{[lowbar]}authenticate() or pam{[lowbar]}chauthtok(). These functions call each of the stacked authentication modules listed in the relevant Linux-PAM configuration file. As directed by this file, one of more of the modules may fail causing the pam{[lowbar]}...() 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 brute force dictionary attacks primarily, but also helps hinder timed (covert channel) attacks.

The pam{[lowbar]}fail{[lowbar]}delay() function provides the mechanism by which an application or module can suggest a minimum delay (of micro{[lowbar]}sec micro-seconds). Linux-PAM keeps a record of the longest time requested with this function. Should pam{[lowbar]}authenticate() fail, the failing return to the application is delayed by an amount of time randomly distributed (by up to 25{[percnt]}) about this longest value.

Independent of success, the delay time is reset to its zero default value when Linux-PAM returns control to the application.

For applications written with a single thread that are event driven in nature, libpam 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, Linux-PAM supplies the PAM{[lowbar]}FAIL{[lowbar]}DELAY item. It can be queried and set with pam{[lowbar]}get{[lowbar]}item() and pam{[lowbar]}set{[lowbar]}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{[lowbar]}delay micro-second delay that libpam is requesting and the appdata{[lowbar]}ptr that the application has associated with the current pamh (pam{[lowbar]}handle{[lowbar]}t). This last value was set by the application when it called pam{[lowbar]}start or explicitly with pam{[lowbar]}set{[lowbar]}item(... , PAM{[lowbar]}CONV, ...). Note, if PAM{[lowbar]}FAIL{[lowbar]}DELAY is unset (or set to NULL), then libpam will perform any delay.

Authenticating the user

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 optional flag, which may be logically OR'd with PAM{[lowbar]}SILENT, takes the following value,

PAM{[lowbar]}DISALLOW{[lowbar]}NULL{[lowbar]}AUTHTOK

Instruct the authentication modules to return PAM{[lowbar]}AUTH{[lowbar]}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{[lowbar]}AUTH{[lowbar]}ERR

The user was not authenticated

PAM{[lowbar]}CRED{[lowbar]}INSUFFICIENT

For some reason the application does not have sufficient credentials to authenticate the user.

PAM{[lowbar]}AUTHINFO{[lowbar]}UNAVAIL

The modules were not able to access the authentication information. This might be due to a network or hardware failure etc.

PAM{[lowbar]}USER{[lowbar]}UNKNOWN

The supplied username is not known to the authentication service

PAM{[lowbar]}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{[lowbar]}ABORT.

Setting user credentials

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 but before a session has been opened for the user.

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 (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{[lowbar]}sm{[lowbar]}setcred functions of each of the loaded modules. Valid flags, any one of which, may be logically OR'd with PAM{[lowbar]}SILENT, are:

PAM{[lowbar]}ESTABLISH{[lowbar]}CRED

Set the credentials for the authentication service,

PAM{[lowbar]}DELETE{[lowbar]}CRED

Delete the credentials associated with the authentication service,

PAM{[lowbar]}REINITIALIZE{[lowbar]}CRED

Reinitialize the user credentials, and

PAM{[lowbar]}REFRESH{[lowbar]}CRED

Extend the lifetime of the user credentials.

A successful return is signalled with PAM{[lowbar]}SUCCESS. Errors that are especially relevant to this function are the following:

PAM{[lowbar]}CRED{[lowbar]}UNAVAIL

A module cannot retrieve the user's credentials.

PAM{[lowbar]}CRED{[lowbar]}EXPIRED

The user's credentials have expired.

PAM{[lowbar]}USER{[lowbar]}UNKNOWN

The user is not known to an authentication module.

PAM{[lowbar]}CRED{[lowbar]}ERR

A module was unable to set the credentials of the user.

Account management

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{[lowbar]}SILENT, and are the same as those applicable to the flags argument of pam{[lowbar]}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{[lowbar]}SUCCESS, however, specific failures are indicated by the following error returns:

PAM{[lowbar]}AUTHTOKEN{[lowbar]}REQD

The user is valid but their authentication token has expired. The correct response to this return-value is to require that the user satisfies the pam{[lowbar]}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{[lowbar]}ACCT{[lowbar]}EXPIRED

The user is no longer permitted to access the system.

PAM{[lowbar]}AUTH{[lowbar]}ERR

There was an authentication error.

PAM{[lowbar]}PERM{[lowbar]}DENIED

The user is not permitted to gain access at this time.

PAM{[lowbar]}USER{[lowbar]}UNKNOWN

The user is not known to a module's account management component.

Updating authentication tokens

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{[lowbar]}SILENT,

PAM{[lowbar]}CHANGE{[lowbar]}EXPIRED{[lowbar]}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 all authentication tokens are to be changed.

PAM{[lowbar]}SUCCESS is the only successful return value, valid error-returns are:

PAM{[lowbar]}AUTHTOK{[lowbar]}ERR

A module was unable to obtain the new authentication token.

PAM{[lowbar]}AUTHTOK{[lowbar]}RECOVERY{[lowbar]}ERR

A module was unable to obtain the old authentication token.

PAM{[lowbar]}AUTHTOK{[lowbar]}LOCK{[lowbar]}BUSY

One or more of the modules was unable to change the authentication token since it is currently locked.

PAM{[lowbar]}AUTHTOK{[lowbar]}DISABLE{[lowbar]}AGING

Authentication token aging has been disabled for at least one of the modules.

PAM{[lowbar]}PERM{[lowbar]}DENIED

Permission denied.

PAM{[lowbar]}TRY{[lowbar]}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{[lowbar]}USER{[lowbar]}UNKNOWN

The user is not known to the authentication token changing service.

Session initialization

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 modules that the user is currently in a session. It should be possible for the Linux-PAM 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{[lowbar]}SILENT and this is, of course, optional.

If any of the required loaded modules are unable to open a session for the user, this function will return PAM{[lowbar]}SESSION{[lowbar]}ERR.

Terminating sessions

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 modules that the user is exiting a session. It should be possible for the Linux-PAM library to open a session and close the same session from different applications.

This function simply calls each of the corresponding functions of the loaded modules in the same order that they were invoked with pam{[lowbar]}open{[lowbar]}session(). The only valid flag is PAM{[lowbar]}SILENT and this is, of course, optional.

If any of the required loaded modules are unable to close a session for the user, this function will return PAM{[lowbar]}SESSION{[lowbar]}ERR.

Setting PAM environment variables

The libpam library associates with each PAM-handle (pamh), a set of PAM environment variables. These variables are intended to hold the session environment variables that the user will inherit when the session is granted and the authenticated user obtains access to the requested service. For example, when login has finally given the user a shell, the environment (as viewed with the command env) will be what libpam was maintaining as the PAM environment for that service application. Note, these variables are not the environment variables of the login application. This is principally for two reasons: login may want to have an environment that cannot be seen or manipulated by a user; and login (or whatever the serving application is) may be maintaining a number of parallel sessions, via different pamh values, at the same time and a single environment may not be appropriately shared between each of these. The PAM environment may contain variables seeded by the applicant user's client program, for example, and as such it is not appropriate for one applicant to interfere with the environment of another applicant.

extern int pam_putenv(pam_handle_t *pamh, const char *name_value);

This function attempts to (re)set a Linux-PAM environment variable. The name{[lowbar]}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 Linux-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{[lowbar]}putenv() function will delete the corresponding variable from the Linux-PAM environment.

Success is indicated with a return value of PAM{[lowbar]}SUCCESS. Failure is indicated by one of the following returns:

PAM{[lowbar]}PERM{[lowbar]}DENIED

name given is a NULL pointer

PAM{[lowbar]}BAD{[lowbar]}ITEM

variable requested (for deletion) is not currently set

PAM{[lowbar]}ABORT

the Linux-PAM handle, pamh, is corrupt

PAM{[lowbar]}BUF{[lowbar]}ERR

failed to allocate memory when attempting update

Getting a PAM environment variable

extern const char *pam_getenv(pam_handle_t *pamh, const char *name);

Obtain the value of the indicated Linux-PAM environment variable. On error, internal failure or the unavailability of the given variable (unspecified), this function simply returns NULL.

Getting the PAM environment

extern const char * const *pam_getenvlist(pam_handle_t *pamh);

The PAM environment variables (see section above) are a complete set of enviroment variables that are associated with a PAM-handle (pamh). They represent the contents of the regular environment variables of the authenticated user when service is granted.

Th function, pam{[lowbar]}getenvlist() returns a pointer to a complete, malloc()'d, copy of the PAM environment. It is a pointer to a duplicated list of environment variables. It should be noted that this memory will never be free()'d by libpam. Once obtained by a call to pam{[lowbar]}getenvlist(), it is the responsibility of the calling application to free() this memory.

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 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.2 What is expected of an application

The conversation function

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{[lowbar]}conv, is defined by including {[lt ]}security/pam{[lowbar]}appl.h{[gt ]}; 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 contents 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 conversation structure.

When a module calls the referenced conv() function, the argument *appdata{[lowbar]}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{[lowbar]}msg holds the length of the array of pointers, msg. After a successful return, the pointer *resp points to an array of pam{[lowbar]}response structures, holding the application supplied text. Note, *resp is an struct pam{[lowbar]}response array and not an array of pointers.

The message (from the module to the application) passing structure is defined by {[lt ]}security/pam{[lowbar]}appl.h{[gt ]} as:

struct pam_message {
    int msg_style;
    const char *msg;
};

Valid choices for msg{[lowbar]}style are:

PAM{[lowbar]}PROMPT{[lowbar]}ECHO{[lowbar]}OFF

Obtain a string without echoing any text

PAM{[lowbar]}PROMPT{[lowbar]}ECHO{[lowbar]}ON

Obtain a string whilst echoing text

PAM{[lowbar]}ERROR{[lowbar]}MSG

Display an error

PAM{[lowbar]}TEXT{[lowbar]}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.

In passing, it is worth noting that there is a descrepency between the way Linux-PAM handles the const struct pam{[lowbar]}message **msg conversation function argument from the way that Solaris' PAM (and derivitives, known to include HP/UX, are there others?) does. Linux-PAM interprets the msg argument as entirely equivalent to the following prototype const struct pam{[lowbar]}message *msg[] (which, in spirit, is consistent with the commonly used prototypes for argv argument to the familiar main() function: char **argv; and char *argv[]). Said another way Linux-PAM interprets the msg argument as a pointer to an array of num{[lowbar]}meg read only 'struct pam{[lowbar]}message' pointers. Solaris' PAM implementation interprets this argument as a pointer to a pointer to an array of num{[lowbar]}meg pam{[lowbar]}message structures. Fortunately, perhaps, for most module/application developers when num{[lowbar]}msg has a value of one these two definitions are entirely equivalent. Unfortunately, casually raising this number to two has led to unanticipated compatibility problems.

For what its worth the two known module writer work-arounds for trying to maintain source level compatibility with both PAM implementations are:

The response (from the application to the module) passing structure is defined by including {[lt ]}security/pam{[lowbar]}appl.h{[gt ]} as:

struct pam_response {
    char *resp;
    int resp_retcode;
};

Currently, there are no definitions for resp{[lowbar]}retcode values; the normal value is 0.

Prior to the 0.59 release of Linux-PAM, the length of the returned pam{[lowbar]}response array was equal to the number of prompts (types PAM{[lowbar]}PROMPT{[lowbar]}ECHO{[lowbar]}OFF and PAM{[lowbar]}PROMPT{[lowbar]}ECHO{[lowbar]}ON) in the pam{[lowbar]}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. The number of responses is always equal to the num{[lowbar]}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{[lowbar]}message array.

The maximum length of the pam{[lowbar]}msg.msg and pam{[lowbar]}response.resp character strings is PAM{[lowbar]}MAX{[lowbar]}MSG{[lowbar]}SIZE. (This is not enforced by Linux-PAM.)

PAM{[lowbar]}SUCCESS is the expected return value of this function. However, should an error occur the application should not set *resp but simply return PAM{[lowbar]}CONV{[lowbar]}ERR.

Note, if an application wishes to use two conversation functions, it should activate the second with a call to pam{[lowbar]}set{[lowbar]}item().

Notes: New item types are being added to the conversation protocol. Currently Linux-PAM supports: PAM{[lowbar]}BINARY{[lowbar]}PROMPT and PAM{[lowbar]}BINARY{[lowbar]}MSG. These two are intended for server-client hidden information exchange and may be used as an interface for maching-machine authentication.

3.3 Programming notes

Note, all of the authentication service function calls accept the token PAM{[lowbar]}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{[lowbar]}SILENT does not override the prompting of the user for passwords etc., it only stops informative messages from being generated.


Next Previous Contents