  The Linux-PAM Application Developers' Guide
  Andrew G. Morgan, morgan@parc.power.net
  DRAFT v0.54 1996/11/30

  This manual documents what an application developer needs to know
  about the LLiinnuuxx--PPAAMM library. It describes how an application might use
  the LLiinnuuxx--PPAAMM 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.  Authenticating the user

  2.1.7.  Setting user credentials

  2.1.8.  Account management

  2.1.9.  Updating authentication tokens

  2.1.10. Session initialization

  2.1.11. Terminating sessions

  2.1.12. Setting PAM environment variables

  2.1.13. Getting a PAM environment variable

  2.1.14. 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

  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.      An example application

  6.      Files

  7.      See also

  8.      Notes

  9.      Author/acknowledgments

  10.     Bugs/omissions
  ______________________________________________________________________

  11..  IInnttrroodduuccttiioonn

  11..11..  SSyynnooppssiiss

  For general applications that wish to use the services provided by
  LLiinnuuxx--PPAAMM the following is a summary of the relevant linking
  information:

       #include <security/pam_appl.h>

       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 <security/pam_misc.h>

       cc -o application .... -lpam -lpam_misc

  11..22..  DDeessccrriippttiioonn

  LLiinnuuxx--PPAAMM (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 LLiinnuuxx--PPAAMM
  library see the LLiinnuuxx--PPAAMM System Administrators' Guide.

  It is the purpose of the LLiinnuuxx--PPAAMM 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).

  22..  TThhee ppuubblliicc iinntteerrffaaccee ttoo LLiinnuuxx--PPAAMM

  Firstly, the relevant include file for the LLiinnuuxx--PPAAMM 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.

  22..11..  WWhhaatt ccaann bbee eexxppeecctteedd bbyy tthhee aapppplliiccaattiioonn

  Here we document those functions in the LLiinnuuxx--PPAAMM library that may be
  called from an application.

  22..11..11..  IInniittiiaalliizzaattiioonn ooff LLiinnuuxx--PPAAMM

       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 LLiinnuuxx--PPAAMM functions that must be called by an
  application. It initializes the interface and reads the system
  configuration file, /etc/pam.conf (see the LLiinnuuxx--PPAAMM System
  Administrators' Guide).  Following a successful return (PAM_SUCCESS)
  the contents of *pamh is a handle that provides continuity for
  successive calls to the LLiinnuuxx--PPAAMM 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
  LLiinnuuxx--PPAAMM library provides the functions pam_set_item and
  pam_get_item.  These functions are documented below.

  22..11..22..  TTeerrmmiinnaattiioonn ooff tthhee lliibbrraarryy

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

  This function is the last function an application should call in the
  LLiinnuuxx--PPAAMM 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 LLiinnuuxx--PPAAMM 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 LLiinnuuxx--PPAAMM Module Developers'
  Guide).

  22..11..33..  SSeettttiinngg PPAAMM iitteemmss

       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
  iitteemm__ttyyppees:

     PPAAMM__SSEERRVVIICCEE
        The service name

     PPAAMM__UUSSEERR
        The user name

     PPAAMM__TTTTYY
        The terminal name

     PPAAMM__RRHHOOSSTT
        The remote host name

     PPAAMM__CCOONNVV
        The conversation structure (see section ``below'')

     PPAAMM__RRUUSSEERR
        The remote user name

     PPAAMM__UUSSEERR__PPRROOMMPPTT
        The string used when prompting for a user's name.

  For all item_types, other than PAM_CONV, item is a pointer to a <NUL>
  terminated character string.  In the case of PAM_CONV, item points to
  an initialized pam_conv structure (see section ``below'').

  A successful call to this function returns PAM_SUCCESS.  However, the
  application should expect one of the following errors:

     PPAAMM__PPEERRMM__DDEENNIIEEDD
        An attempt was made to replace the conversation structure with a
        NULL value.

     PPAAMM__BBUUFF__EERRRR
        The function ran out of memory making a copy of the item.

     PPAAMM__BBAADD__IITTEEMM
        The application attempted to set an undefined item.

  22..11..44..  GGeettttiinngg PPAAMM iitteemmss

       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.

  22..11..55..  UUnnddeerrssttaannddiinngg eerrrroorrss

       extern const char *pam_strerror(int errnum);

  This function returns some text describing the LLiinnuuxx--PPAAMM error
  associated with the argument errnum.  If the error is not recognized
  ``Unknown Linux-PAM error'' is returned.

  22..11..66..  AAuutthheennttiiccaattiinngg tthhee uusseerr

       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,

     PPAAMM__DDIISSAALLLLOOWW__NNUULLLL__AAUUTTHHTTOOKK
        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:

     PPAAMM__AAUUTTHH__EERRRR
        The user was not authenticated

     PPAAMM__CCRREEDD__IINNSSUUFFFFIICCIIEENNTT
        For some reason the application does not have sufficient
        credentials to authenticate the user.

     PPAAMM__AAUUTTHHIINNFFOO__UUNNAAVVAAIILL
        The modules were not able to access the authentication
        information. This might be due to a network or hardware failure
        etc.

     PPAAMM__UUSSEERR__UUNNKKNNOOWWNN
        The supplied username is not known to the authentication service

     PPAAMM__MMAAXXTTRRIIEESS
        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.

  22..11..77..  SSeettttiinngg uusseerr ccrreeddeennttiiaallss

       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:

     PPAAMM__CCRREEDD__EESSTTAABBLLIISSHH
        Set the credentials for the authentication service,

     PPAAMM__CCRREEDD__DDEELLEETTEE
        Delete the credentials associated with the authentication
        service,

     PPAAMM__CCRREEDD__RREEIINNIITTIIAALLIIZZEE
        Reinitialize the user credentials, and

     PPAAMM__CCRREEDD__RREEFFRREESSHH
        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:

     PPAAMM__CCRREEDD__UUNNAAVVAAIILL
        A module cannot retrieve the user's credentials.

     PPAAMM__CCRREEDD__EEXXPPIIRREEDD
        The user's credentials have expired.

     PPAAMM__UUSSEERR__UUNNKKNNOOWWNN
        The user is not known to an authentication module.

     PPAAMM__CCRREEDD__EERRRR
        A module was unable to set the credentials of the user.

  22..11..88..  AAccccoouunntt mmaannaaggeemmeenntt

       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:

     PPAAMM__AAUUTTHHTTOOKKEENN__RREEQQDD
        The user iiss 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.

     PPAAMM__AACCCCTT__EEXXPPIIRREEDD
        The user is no longer permitted access to the system.

     PPAAMM__AAUUTTHH__EERRRR
        There was an authentication error.

     PPAAMM__PPEERRMM__DDEENNIIEEDD
        The user is not permitted to gain access at this time.

     PPAAMM__UUSSEERR__UUNNKKNNOOWWNN
        The user is not known to a module's account management
        component.

  22..11..99..  UUppddaattiinngg aauutthheennttiiccaattiioonn ttookkeennss

       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,

     PPAAMM__CCHHAANNGGEE__EEXXPPIIRREEDD__AAUUTTHHTTOOKK
        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:

     PPAAMM__AAUUTTHHTTOOKK__EERRRR
        A module was unable to obtain the new authentication token.

     PPAAMM__AAUUTTHHTTOOKK__RREECCOOVVEERRYY__EERRRR
        A module was unable to obtain the old authentication token.

     PPAAMM__AAUUTTHHTTOOKK__LLOOCCKK__BBUUSSYY
        One or more of the modules was unable to change the
        authentication token since it is currently locked.

     PPAAMM__AAUUTTHHTTOOKK__DDIISSAABBLLEE__AAGGIINNGG
        Authentication token aging has been disabled for at least one of
        the modules.

     PPAAMM__PPEERRMM__DDEENNIIEEDD
        Permission denied.

     PPAAMM__TTRRYY__AAGGAAIINN
        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.

     PPAAMM__UUSSEERR__UUNNKKNNOOWWNN
        The user is not known to the authentication token changing
        service.

  22..11..1100..  SSeessssiioonn iinniittiiaalliizzaattiioonn

       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 LLiinnuuxx--PPAAMM 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.

  22..11..1111..  TTeerrmmiinnaattiinngg sseessssiioonnss

       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 LLiinnuuxx--PPAAMM 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.

  22..11..1122..  SSeettttiinngg PPAAMM eennvviirroonnmmeenntt vvaarriiaabblleess

       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 LLiinnuuxx--PPAAMM 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 LLiinnuuxx--PPAAMM environment variable.
  The name_value argument is a single NUL terminated string of one of
  the following forms:

     ````NNAAMMEE==vvaalluuee ooff vvaarriiaabbllee''
        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 LLiinnuuxx--PPAAMM environment.

     ````NNAAMMEE==''
        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.

     ````NNAAMMEE''
        Without an `=' the pam_putenv() function will delete the
        correspoding variable from the LLiinnuuxx--PPAAMM environment.

  Success is indicated with a return value of PAM_SUCCESS. Failure is
  indicated by one of the following returns:

     PPAAMM__PPEERRMM__DDEENNIIEEDD
        name given is a NULL pointer

     PPAAMM__BBAADD__IITTEEMM
        variable requested (for deletion) is not currently set

     PPAAMM__AABBOORRTT
        the LLiinnuuxx--PPAAMM handle, pamh, is corrupt

     PPAAMM__BBUUFF__EERRRR
        failed to allocate memory when attempting update

  22..11..1133..  GGeettttiinngg aa PPAAMM eennvviirroonnmmeenntt vvaarriiaabbllee

       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 LLiinnuuxx--PPAAMM 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 LLiinnuuxx--PPAAMM environment variable. On
  error, internal failure or the unavailability of the given variable
  (unspecified), this function simply returns NULL.

  22..11..1144..  GGeettttiinngg tthhee PPAAMM eennvviirroonnmmeenntt

       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 LLiinnuuxx--PPAAMM 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.)

  22..22..  WWhhaatt iiss eexxppeecctteedd ooff aann aapppplliiccaattiioonn

  22..22..11..  TThhee ccoonnvveerrssaattiioonn ffuunnccttiioonn

  An application must provide a ``conversation function''. It is used
  for direct communication between a loaded module and the application
  and will typically provide a means for the module to prompt the user
  for a password etc. . The structure, pam_conv, is defined by including
  <security/pam_appl.h>; to be,

       struct pam_conv {
           int (*conv)(int num_msg,
               const struct pam_message **msg,
               struct pam_response **resp,
               void *appdata_ptr);
           void *appdata_ptr;
       };

  It is initialized by the application before it is passed to the
  library.  The _c_o_n_t_e_n_t_s of this structure are attached to the *pamh
  handle.  The point of this argument is to provide a mechanism for any
  loaded module to interact directly with the application program. This
  is why it is called a _c_o_n_v_e_r_s_a_t_i_o_n structure.

  When a module calls the referenced conv() function, the argument
  *appdata_ptr is set to the second element of this structure.

  The other arguments of a call to conv() concern the information
  exchanged by module and application. That is to say, num_msg holds the
  length of the array of pointers, msg. After a successful return, the
  pointer *resp points to an array of pam_response structures, holding
  the application supplied text.  Note, *resp is an struct pam_response
  array and _n_o_t an array of pointers.

  The message (from the module to the application) passing structure is
  defined by <security/pam_appl.h> as:

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

  Valid choices for msg_style are:

     PPAAMM__PPRROOMMPPTT__EECCHHOO__OOFFFF
        Obtain a string without echoing any text

     PPAAMM__PPRROOMMPPTT__EECCHHOO__OONN
        Obtain a string whilst echoing text

     PPAAMM__EERRRROORR__MMSSGG
        Display an error

     PPAAMM__TTEEXXTT__IINNFFOO
        Display some text.

  The point of having an array of messages is that it becomes possible
  to pass a number of things to the application in a single call from
  the module. It can also be convenient for the application that related
  things come at once: a windows based application can then present a
  single form with many messages/prompts on at once.

  The response (from the application to the module) passing structure is
  defined by including <security/pam_appl.h> as:

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

  Currently, there are no definitions for resp_retcode values; the
  normal value is 0. 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().

  22..33..  PPrrooggrraammmmiinngg nnootteess

  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.

  33..  SSeeccuurriittyy iissssuueess ooff LLiinnuuxx--PPAAMM

  A poorly (or maliciously) written application can defeat any LLiinnuuxx--PPAAMM
  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 LLiinnuuxx--PPAAMM 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 LLiinnuuxx--PPAAMM library
  functions.  A failure to do this will most certainly lead to a future
  security breach.

  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 any call to the LLiinnuuxx--PPAAMM library, or copy the
  structure contents to some safe area of memory before passing control
  to the LLiinnuuxx--PPAAMM library.

  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 avoid the
  temptation of choosing, argv[0]. Since a user can always establish the
  service that uses the weakest authentication service.  By symbolically
  linking the targeted application to a link of that name, the user
  assumes power to choose the authentication method for any application.

  Care should be taken to ensure that the conv() function is robust.
  Such a function is provided in the library libpam_misc (see
  ``below'').

  44..  AA lliibbrraarryy ooff mmiisscceellllaanneeoouuss hheellppeerr ffuunnccttiioonnss

  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.

  44..11..  RReeqquuiirreemmeennttss

  The functions, structures and macros made available by this library
  can be defined by including <security/pam_misc.h>. It should be noted
  that this library is specific to LLiinnuuxx--PPAAMM and is not referred to in
  the defining DCE-RFC (see ``the bibliography'') below.

  44..22..  FFuunnccttiioonnss ssuupppplliieedd

  44..22..11..  SSaaffee ssttrriinngg dduupplliiccaattiioonn

       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.

  44..22..22..  AA tteexxtt bbaasseedd ccoonnvveerrssaattiioonn ffuunnccttiioonn

       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.

  44..22..33..  TTrraannssccrriibbiinngg aann eennvviirroonnmmeenntt ttoo tthhaatt ooff LLiinnuuxx--PPAAMM

       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 LLiinnuuxx--PPAAMM environment. Success is
  indicated by PAM_SUCCESS.

  44..22..44..  SSaavviinngg tthhee LLiinnuuxx--PPAAMM eennvviirroonnmmeenntt ffoorr llaatteerr uussee

       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 LLiinnuuxx--PPAAMM 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().

  44..22..55..  LLiibbeerraattiinngg aa llooccaallllyy ssaavveedd eennvviirroonnmmeenntt

       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.

  44..22..66..  BBSSDD lliikkee LLiinnuuxx--PPAAMM eennvviirroonnmmeenntt vvaarriiaabbllee sseettttiinngg

       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 LLiinnuuxx--PPAAMM variable is already set,
  the replacement will only be applied if the last argument, readonly,
  is zero.

  55..  AAnn eexxaammppllee aapppplliiccaattiioonn

  To get a flavor of the way a Linux-PAM application is written we
  include the following example. It prompts the user for their password
  and indicates whether their account is valid on the standard output,
  its return code also indicates the success (0 for success; 1 for
  failure).

  /*
    This program was contributed by Shane Watts
    [modifications by AGM]

    You need to add the following (or equivalent) to the /etc/pam.conf file.
    # check authorization
    check_user   auth       required     /usr/lib/security/pam_unix_auth.so
    check_user   account    required     /usr/lib/security/pam_unix_acct.so
   */

  #include <security/pam_appl.h>
  #include <security/pam_misc.h>
  #include <stdio.h>

  static struct pam_conv conv = {
      misc_conv,
      NULL
  };

  int main(int argc, char *argv[])
  {
      pam_handle_t *pamh=NULL;
      int retval;
      const char *user="nobody";

      if(argc == 2) {
          user = argv[1];
      }

      if(argc > 2) {
          fprintf(stderr, "Usage: check_user [username]\n");
          exit(1);
      }

      retval = pam_start("check_user", user, &conv, &pamh);

      if (retval == PAM_SUCCESS)
          retval = pam_authenticate(pamh, 0);    /* is user really user? */

      if (retval == PAM_SUCCESS)
          retval = pam_acct_mgmt(pamh, 0);       /* permitted access? */

      /* This is where we have been authorized or not. */

      if (retval == PAM_SUCCESS) {
          fprintf(stdout, "Authenticated\n");
      } else {
          fprintf(stdout, "Not Authenticated\n");
      }

      if (pam_end(pamh,retval) != PAM_SUCCESS) {     /* close Linux-PAM */
          pamh = NULL;
          fprintf(stderr, "check_user: failed to release authenticator\n");
          exit(1);
      }

      return ( retval == PAM_SUCCESS ? 0:1 );       /* indicate success */
  }

  66..  FFiilleess

     //uussrr//iinncclluuddee//sseeccuurriittyy//ppaamm__aappppll..hh
        header file for LLiinnuuxx--PPAAMM applications interface

     //uussrr//iinncclluuddee//sseeccuurriittyy//ppaamm__mmiisscc..hh
        header file for useful library functions for making applications
        easier to write

     //uussrr//lliibb//lliibbppaamm..ssoo..**
        the shared library providing applications with access to LLiinnuuxx--
        PPAAMM.

     //eettcc//ppaamm..ccoonnff
        the LLiinnuuxx--PPAAMM configuration file.

     //uussrr//lliibb//sseeccuurriittyy//ppaamm__**..ssoo
        the primary location for LLiinnuuxx--PPAAMM dynamically loadable object
        files; the modules.

  77..  SSeeee aallssoo

  +o  The LLiinnuuxx--PPAAMM System Administrators' Guide.

  +o  The LLiinnuuxx--PPAAMM 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.

  88..  NNootteess

  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....

  99..  AAuutthhoorr//aacckknnoowwlleeddggmmeennttss

  This document was written by Andrew G. Morgan (morgan@parc.power.net)
  with many contributions from Marc Ewing, Michael K. Johnson, Al
  Longyear, Marek Michalkiewicz, Theodore Ts'o, Jeff Uphoff, 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
  LLiinnuuxx--PPAAMM, Sun graciously made the documentation for their
  implementation of PAM available. This act greatly accelerated the
  development of LLiinnuuxx--PPAAMM.

  1100..  BBuuggss//oommiissssiioonnss

  This manual is hopelessly unfinished. Only a partial list of people is
  credited for all the good work they have done.

