LinuxDevCenter.com
oreilly.comSafari Books Online.Conferences.

advertisement


Writing PAM-Capable Applications, Part Two
Pages: 1, 2

Setting Credentials

After the user is authenticated and the account is verified as currently usable, many authentication methods require the authenticator to set credentials. Credentials might be group membership in a Unix system, or a Kerberos ticket.



The application should call this function before the session is started.

extern int pam_setcred(pam_handle_t *pamh, int flags);

Session

These functions open and close authenticated sessions. The close session function can be called from a different application, provided the application has the PAM handle.

extern int pam_open_session(pam_handle_t *pamh, int flags);
extern int pam_close_session(pam_handle_t *pamh, int flags);

Password

pam_chauthtok changes authentication tokens, using the conversation function to interact with the user.

Calling this function with the flag PAM_CHANGE_EXPIRED_AUTHTOK will instruct it to only change the token if the token has actually expired. I recommend doing this if the call is triggered by the account module returning PAM_NEW_AUTHTOK_REQD.

This module type is usually called after the session is started, but it can be called immediately following account management, to refresh expired authentication tokens.

extern int pam_chauthtok(pam_handle_t *pamh, const int flags);

pam_end()

This function closes the PAM application. Call it when you no longer need the PAM handle, to allow the modules to be called and perform their cleanups -- freeing memory, wiping data structures clean, and disabling tokens.

The pam_status parameter indicates whether PAM was successful or not, which helps modules determine what cleanup has to be done. It can be useful to use one variable to receive the return value of all PAM calls, and bracket most PAM calls with if (retval == PAM_SUCCESS). You can then call pam_end with the return value as the pam_status argument.

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

Failure Codes

All PAM functions return either PAM_SUCCESS or one of the following failure codes. It is worth noting that I have had PAM functions return failure codes not listed in the documentation.

The PAM function pam_strerror() displays the text string associated with each failure code.

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

Listed failure codes are:

PAM_ABORT
Usually means the PAM handle has been corrupted.



PAM_ACCT_EXPIRED
The user's account is out-of-date and he or she may not access the system.



PAM_AUTH_ERR
Authentication error.

PAM_AUTHINFO_UNAVAIL
One or more modules could not access the authentication information, perhaps due to hardware failure.

PAM_AUTHTOK_DISABLE_AGING
One or more modules has had token-aging disabled.

PAM_AUTHTOK_ERR
One or more modules could not read the new authentication token.

PAM_AUTHTOK_LOCK_BUSY
The authentication token could not be changed because of a lock.

PAM_AUTHTOK_RECOVERY_ERR
The old authentication token could not be retrieved.

PAM_BAD_ITEM
A variable could not be set/deleted because it was undefined, inaccessible, or not currently set.

PAM_BUF_ERR
Memory allocation failure.

PAM_CRED_ERR
One or more modules could not set the user's credentials.

PAM_CRED_EXPIRED
The user's credentials have expired.

PAM_CRED_INSUFFICIENT
The application is not permitted to authenticate the user, due to insufficient credentials.

PAM_CRED_UNAVAIL
One or more modules cannot read the user's credentials.

PAM_MAXTRIES
One or more authentication modules has reached its maximum number of retries. Do not continue to attempt to authenticate this user.

PAM_NEW_AUTHTOK_REQD
The authentication token has expired and should be renewed.

PAM_PERM_DENIED
This usually means permission denied, but sometimes means that a required parameter was a NULL pointer.

PAM_SYSTEM_ERR
Usually means an invalid PAM handle.

PAM_TRY_AGAIN
One or more modules could not update the authentication token, therefore none of the tokens were updated.

PAM_USER_UNKNOWN
The user is not known to one or more modules (of this type).

Additional Functions

This function is in <security/pam_misc.h>. Linux-PAM provides a function to securely duplicate strings. A copy of the parameter is returned. If there isn't enough memory to duplicate the string, a NULL is returned.

extern char *x_strdup(const char *s)

Default Configuration

In your application's installation script, check for a file with your service name in /etc/pam.d. If it doesn't exist, create one with an appropriate default PAM-authentication sequence for your program. You can find an acceptable default PAM configuration, with explanations, in /etc/pam.d/login in the default installation of many Linux systems. Another alternative is:

auth       requisite  pam_securetty.so
auth       required   pam_unix.so
account    required   pam_unix.so
session    required   pam_unix.so
password   required   pam_cracklib.so retry=3 minlen=6 difok=3
password   required   pam_unix.so use_authtok nullok md5

If a file with your service name exists, you may have a service-name clash with another application, or the file may have been installed by a previous version of your application.

Caveats and Gotchas

  • DO read the Linux-PAM Application Developer's Guide.
  • DO code securely.
  • xstrdup in the documentation is x_strdup in the code (confirmed with Andrew Morgan).
  • PAM_AUTHTOKEN_REQD in the documentation seems to be PAM_NEW_AUTHTOK_REQD in the code.

Final Words

This article has touched on the most critical aspects of writing PAM-capable applications. For additional details, please do see the Application Developer's Guide.

Further Reading

Jennifer Vesperman is the author of Essential CVS. She writes for the O'Reilly Network, the Linux Documentation Project, and occasionally Linux.Com.


Return to the Linux DevCenter.




Linux Online Certification

Linux/Unix System Administration Certificate Series
Linux/Unix System Administration Certificate Series — This course series targets both beginning and intermediate Linux/Unix users who want to acquire advanced system administration skills, and to back those skills up with a Certificate from the University of Illinois Office of Continuing Education.

Enroll today!


Linux Resources
  • Linux Online
  • The Linux FAQ
  • linux.java.net
  • Linux Kernel Archives
  • Kernel Traffic
  • DistroWatch.com


  • Sponsored by: