Firstly, the relevant include file for the Linux-PAM 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.
Here we document those functions in the Linux-PAM library that may be called from an application.
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_SUCCESS) the contents of *pamh is a handle that
provides continuity for successive calls to the Linux-PAM
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
pointer.
The pam_conv structure is discussed more fully below in the
section covering ``What is expected of an application''. The
pam_handle_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_set_item and pam_get_item. These
functions are documented below.
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. Under normal conditions the argument pam_status has
the value PAM_SUCCESS, but its purpose is to be passed as an argument
to the module specific function cleanup() (see the Linux-PAM
Module Developers' Guide).
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_types:
PAM_SERVICEThe service name
PAM_USERThe user name
PAM_TTYThe terminal name
PAM_RHOSTThe remote host name
PAM_CONVThe pam_conv structure (see below)
PAM_RUSERThe remote user name
PAM_USER_PROMPTThe string used when prompting for a user's name.
For all item_types, but 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 below).
A successful call to this function returns PAM_SUCCESS. However
the application can expect one of the following errors:
PAM_PERM_DENIEDAn attempt was made to replace the conversation structure with
a NULL value.
PAM_BUF_ERRThe function ran out of memory making a copy of the item.
PAM_BAD_ITEMThe application attempted to set an un-defined item.
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
actual data and should not 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.
extern const char *pam_strerror(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.
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_SILENT, takes the following value,
PAM_DISALLOW_NULL_AUTHTOKInstruct 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_ERRThe user was not authenticated
PAM_CRED_INSUFFICIENTFor some reason the application does not have sufficient credentials to authenticate the user.
PAM_AUTHINFO_UNAVAILThe modules were not able to access the authentication information. This might be due to a network or hardware failure etc.
PAM_USER_UNKNOWNThe supplied username is not known to the authentication service
PAM_MAXTRIESOne 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.
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. 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_CRED_ESTABLISHSet the credentials for the authentication service,
PAM_CRED_DELETEDelete the credentials associated with the authentication service,
PAM_CRED_REINITIALIZEReinitialize the user credentials, and
PAM_CRED_REFRESHExtend the lifetime of the user credentials.
Return values that the application should look out for are the following:
PAM_CRED_UNAVAILA module cannot retrieve the user's credentials.
PAM_CRED_EXPIREDThe user's credentials have expired.
PAM_USER_UNKNOWNThe user is not known to an authentication module.
PAM_CRED_ERRA module was unable to set the credentials of the user.
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.
Currently, 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_ACCT_EXPIREDThe user is no longer permitted access to the system.
PAM_AUTH_ERRThere was an authentication error.
PAM_AUTHTOKEN_REQDThe user's authentication token has expired. Before calling
this function again the application should arrange for a new one to be
given. This is accomplished via a call to pam_chauthtok().
PAM_PERM_DENIEDThe user is not permitted to gain access at this time.
PAM_USER_UNKNOWNThe user is not known to a module's account management component.
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_AUTHTOKThis 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_SUCCESS is the only successful return value, valid
error-returns are:
PAM_AUTHTOK_ERRA module was unable to obtain the new authentication token.
PAM_AUTHTOK_RECOVERY_ERRA module was unable to obtain the old authentication token.
PAM_AUTHTOK_LOCK_BUSYOne or more of the modules was unable to change the authentication token since it is currently locked.
PAM_AUTHTOK_DISABLE_AGINGAuthentication token aging has been disabled for at least one of the modules.
PAM_PERM_DENIEDPermission denied.
PAM_TRY_AGAINNot all of the modules were in a position to update the authentication token(s).
PAM_USER_UNKNOWNThe user is not known to the authentication token changing service.
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 Linux-PAM library to open a session and close the same session (see 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, optional.
If any of the required loaded modules are unable to open a
session for the user, this function will return PAM_SESSION_ERR.
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 Linux-PAM 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, optional.
If any of the required loaded modules are unable to close a
session for the user, this function will return PAM_SESSION_ERR.
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 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_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 not 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_OFFObtain a string without echoing any text
PAM_PROMPT_ECHO_ONObtain a string whilst echoing text
PAM_ERROR_MSGDisplay an error
PAM_TEXT_INFODisplay 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. The length of the *resp array is known in
advance by the module, since it is dependent on the number of prompts
sent in the msg array. The memory for this array and its string
elements should be dynamically allocated with one of the
malloc() library of functions. It will be free()'d by
the module.
The maximum length of the pam_msg.msg and pam_response.resp
character strings is PAM_MAX_MSG_SIZE.
PAM_SUCCESS is the expected return value of this
function. However, should an error occur the application should not
return set *resp and simply return PAM_CONV_ERR.
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.
Next Chapter, Previous Chapter
Table of contents of this chapter, General table of contents
Top of the document, Beginning of this Chapter