The Linux-PAM Application Developers' Guide Andrew G. Morgan, morgan@parc.power.net DRAFT v0.57 1997/3/4 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. The public interface to 2.1. What can be expected by the application 2.1.1. Initialization of Linux-PAM 2.1.2. Termination of the library 2.1.3. Setting PAM items 2.1.4. Getting PAM items 2.1.5. Understanding errors 2.1.6. Planning for delays 2.1.7. Authenticating the user 2.1.8. Setting user credentials 2.1.9. Account management 2.1.10. Updating authentication tokens 2.1.11. Session initialization 2.1.12. Terminating sessions 2.1.13. Setting PAM environment variables 2.1.14. Getting a PAM environment variable 2.1.15. Getting the PAM environment 2.2. What is expected of an application 2.2.1. The conversation function 2.3. Programming notes 3. Security issues of 3.1. Care about standard library calls 3.2. Choice of a service name 3.3. The conversation function 3.4. The identity of the user 3.5. Sufficient resources 4. A library of miscellaneous helper functions 4.1. Requirements 4.2. Functions supplied 4.2.1. Safe string duplication 4.2.2. A text based conversation function 4.2.3. Transcribing an environment to that of Linux-PAM 4.2.4. Saving the Linux-PAM environment for later use 4.2.5. Liberating a locally saved environment 4.2.6. BSD like Linux-PAM environment variable setting 5. Glossary of PAM related terms 6. An example application 7. Files 8. See also 9. Notes 10. Author/acknowledgments 11. Bugs/omissions 12. 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 cc -o application .... -lpam 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): #include cc -o application .... -lpam -lpam_misc 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.. TThhee ppuubblliicc iinntteerrffaaccee ttoo LLiinnuuxx--PPAAMM Firstly, the relevant include file for the LLiinnuuxx--PPAAMM library is . It contains the definitions for a number of functions. After listing these functions, we collect some guiding remarks for programmers. 22..11.. WWhhaatt ccaann bbee eexxppeecctteedd bbyy tthhee aapppplliiccaattiioonn Here we document those functions in the LLiinnuuxx--PPAAMM library that may be called from an application. 22..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. 22..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). 22..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: PPAAMM__SSEERRVVIICCEE The service name PPAAMM__UUSSEERR The user name PPAAMM__TTTTYY 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. PPAAMM__RRHHOOSSTT The remote host name PPAAMM__CCOONNVV The conversation structure (see section ``below'') PPAAMM__RRUUSSEERR The remote user name PPAAMM__UUSSEERR__PPRROOMMPPTT 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 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: PPAAMM__PPEERRMM__DDEENNIIEEDD An attempt was made to replace the conversation structure with a NULL value. PPAAMM__BBUUFF__EERRRR The function ran out of memory making a copy of the item. PPAAMM__BBAADD__IITTEEMM The application attempted to set an undefined item. 22..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. 22..11..55.. UUnnddeerrssttaannddiinngg eerrrroorrss extern const char *pam_strerror(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. 22..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 calls each of the _s_t_a_c_k_e_d authentication modules listed in the /etc/pam.conf 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. 22..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, PPAAMM__DDIISSAALLLLOOWW__NNUULLLL__AAUUTTHHTTOOKK 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: PPAAMM__AAUUTTHH__EERRRR The user was not authenticated PPAAMM__CCRREEDD__IINNSSUUFFFFIICCIIEENNTT For some reason the application does not have sufficient credentials to authenticate the user. PPAAMM__AAUUTTHHIINNFFOO__UUNNAAVVAAIILL The modules were not able to access the authentication information. This might be due to a network or hardware failure etc. PPAAMM__UUSSEERR__UUNNKKNNOOWWNN The supplied username is not known to the authentication service PPAAMM__MMAAXXTTRRIIEESS 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. 22..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: PPAAMM__CCRREEDD__EESSTTAABBLLIISSHH Set the credentials for the authentication service, PPAAMM__CCRREEDD__DDEELLEETTEE Delete the credentials associated with the authentication service, PPAAMM__CCRREEDD__RREEIINNIITTIIAALLIIZZEE Reinitialize the user credentials, and PPAAMM__CCRREEDD__RREEFFRREESSHH 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: PPAAMM__CCRREEDD__UUNNAAVVAAIILL A module cannot retrieve the user's credentials. PPAAMM__CCRREEDD__EEXXPPIIRREEDD The user's credentials have expired. PPAAMM__UUSSEERR__UUNNKKNNOOWWNN The user is not known to an authentication module. PPAAMM__CCRREEDD__EERRRR A module was unable to set the credentials of the user. 22..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: PPAAMM__AAUUTTHHTTOOKKEENN__RREEQQDD 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. PPAAMM__AACCCCTT__EEXXPPIIRREEDD The user is no longer permitted access to the system. PPAAMM__AAUUTTHH__EERRRR There was an authentication error. PPAAMM__PPEERRMM__DDEENNIIEEDD The user is not permitted to gain access at this time. PPAAMM__UUSSEERR__UUNNKKNNOOWWNN The user is not known to a module's account management component. 22..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, PPAAMM__CCHHAANNGGEE__EEXXPPIIRREEDD__AAUUTTHHTTOOKK 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: PPAAMM__AAUUTTHHTTOOKK__EERRRR A module was unable to obtain the new authentication token. PPAAMM__AAUUTTHHTTOOKK__RREECCOOVVEERRYY__EERRRR A module was unable to obtain the old authentication token. PPAAMM__AAUUTTHHTTOOKK__LLOOCCKK__BBUUSSYY One or more of the modules was unable to change the authentication token since it is currently locked. PPAAMM__AAUUTTHHTTOOKK__DDIISSAABBLLEE__AAGGIINNGG Authentication token aging has been disabled for at least one of the modules. PPAAMM__PPEERRMM__DDEENNIIEEDD Permission denied. PPAAMM__TTRRYY__AAGGAAIINN 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. PPAAMM__UUSSEERR__UUNNKKNNOOWWNN The user is not known to the authentication token changing service. 22..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. 22..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. 22..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: ````NNAAMMEE==vvaalluuee ooff vvaarriiaabbllee'' 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. ````NNAAMMEE=='' 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. ````NNAAMMEE'' 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: PPAAMM__PPEERRMM__DDEENNIIEEDD name given is a NULL pointer PPAAMM__BBAADD__IITTEEMM variable requested (for deletion) is not currently set PPAAMM__AABBOORRTT the LLiinnuuxx--PPAAMM handle, pamh, is corrupt PPAAMM__BBUUFF__EERRRR failed to allocate memory when attempting update 22..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. 22..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.) 22..22.. WWhhaatt iiss eexxppeecctteedd ooff aann aapppplliiccaattiioonn 22..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 ; 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 as: struct pam_message { int msg_style; const char *msg; }; Valid choices for msg_style are: PPAAMM__PPRROOMMPPTT__EECCHHOO__OOFFFF Obtain a string without echoing any text PPAAMM__PPRROOMMPPTT__EECCHHOO__OONN Obtain a string whilst echoing text PPAAMM__EERRRROORR__MMSSGG Display an error PPAAMM__TTEEXXTT__IINNFFOO 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 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 _d_y_n_a_m_i_c_a_l_l_y allocated with one of the malloc() library of functions. _I_t _w_i_l_l _b_e _f_r_e_e_(_)'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 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(). 22..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. 33.. SSeeccuurriittyy iissssuueess ooff LLiinnuuxx--PPAAMM 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. 33..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. 33..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 /etc/pam.conf 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 -- 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, /etc/pam.conf, 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. 33..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''). 33..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. Basically, 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(2). In addition the identity of a remote user, requesting the service from a distant location, will be placed in the PAM_RUSER item. 33..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. 44.. 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. 44..11.. RReeqquuiirreemmeennttss The functions, structures and macros, made available by this library can be defined by including . 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. 44..22.. FFuunnccttiioonnss ssuupppplliieedd 44..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. 44..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: eexxtteerrnn ttiimmee__tt ppaamm__mmiisscc__ccoonnvv__wwaarrnn__ttiimmee;; 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. eexxtteerrnn ccoonnsstt cchhaarr **ppaamm__mmiisscc__ccoonnvv__wwaarrnn__lliinnee;; 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. eexxtteerrnn ttiimmee__tt ppaamm__mmiisscc__ccoonnvv__ddiiee__ttiimmee;; 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. eexxtteerrnn ccoonnsstt cchhaarr **ppaamm__mmiisscc__ccoonnvv__ddiiee__lliinnee;; 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. eexxtteerrnn iinntt ppaamm__mmiisscc__ccoonnvv__ddiieedd;; 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. 44..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. 44..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(). 44..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. 44..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. 55.. 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. 66.. 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 #include #include 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 */ } 77.. FFiilleess //uussrr//iinncclluuddee//sseeccuurriittyy//ppaamm__aappppll..hh header file for LLiinnuuxx--PPAAMM applications interface //uussrr//iinncclluuddee//sseeccuurriittyy//ppaamm__mmiisscc..hh header file for useful library functions for making applications easier to write //uussrr//lliibb//lliibbppaamm..ssoo..** the shared library providing applications with access to LLiinnuuxx-- PPAAMM. //eettcc//ppaamm..ccoonnff the LLiinnuuxx--PPAAMM configuration file. //uussrr//lliibb//sseeccuurriittyy//ppaamm__**..ssoo the primary location for LLiinnuuxx--PPAAMM dynamically loadable object files; the modules. 88.. 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. 99.. 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.... 1100.. AAuutthhoorr//aacckknnoowwlleeddggmmeennttss This document was written by Andrew G. Morgan (morgan@parc.power.net) with many contributions from Craig S. Bell, Derrick J. Brashear, Oliver Crow, Marc Ewing, Cristian Gafton, Eric Hester, Michael K. Johnson, Elliot Lee, Al Longyear, Marek Michalkiewicz, Aleph One, Sean Reifschneider, Eric Troan, Theodore Ts'o, Jeff Uphoff, Ronald Wahl, 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. 1111.. BBuuggss//oommiissssiioonnss This manual is hopelessly unfinished. Only a partial list of people is credited for all the good work they have done. 1122.. CCooppyyrriigghhtt iinnffoorrmmaattiioonn ffoorr tthhiiss ddooccuummeenntt Copyright (c) Andrew G. Morgan 1996. All rights reserved. Email: 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.16 1997/04/05 06:49:14 morgan Exp $