APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed
RSS Feeds RSS Feeds











(OLDER) <- More Stuff -> (NEWER) (NEWEST)
Printer Friendly Version
->
-> Understanding PAM Authentication and Security


Understanding PAM





PAM is the Pluggable Authentication Module, invented by Sun. It's a beautiful concept, but it can be confusing and even intimidating at first. We're going to look at it on a RedHat system, but other Linuxes will be similar - some details may vary, but the basic ideas will be the same.

The first thing to understand is that PAM is NOT something like tcpd (tcp wrappers) or xinetd that encloses and restricts access to some service. An application needs to be "PAM aware"; it needs to have been written and compiled specifically to use PAM. There are tremendous advantages in doing so, and most applications with any interest in security will be PAM aware.

PAM is about security - checking to see that a service should be used or not. Most of us first learned about PAM when we were told that login was using it, but PAM can do much more than just validate passwords. A lot of applications now use PAM - even things like SAMBA can call on PAM for authentication.

The big advantage here is that security is no longer the application's concern: if PAM says its OK, its OK. That makes things easier for the application, and it makes things easier for the system administrator. PAM consults text configuration files to see what security actions to take for an application, and the administrator can add and subtract new rules at any time. PAM is also extensible: should someone invent a device that can read your brain waves and determine ill intent, all we need is a PAM module that can use that device. Change a few files, and login now reads your mind and grants or denies access appropriately. We're a bit away from that feature, but there are a tremendous number of available PAM modules that administrators can use.

Configuration Files

On modern RedHat systems, the configuration files are found in /etc/pam.d, one file for each PAM aware application (plus a special "other" file we'll get to later). One word of warning: changes to these files take effect instantly. You aren't going to get logged out if you make a mistake here. but if you DO screw up and blithely log out, you may not be able to log back in. So test changes before you exit.

We're going to use a very simple example to get started here. In a number of articles here, we've talked about SSH Security. Most of those articles have been about changes to ssh's configuration files, but here we'll use PAM to add some additional restriction: the time of day you are allowed to use ssh. To do this, we need a PAM module called pam_time.so - it's probably in your /lib/security/ directory already. It uses a configuration file "/etc/security/time.conf". That file is pretty well commented, so I'm not going to go into detail about it and will just say that I added the line

sshd;*;*;!Al2200-0400
 

which says that sshd cannot be used between 10:00 PM and 4:00 AM. I'm usually rather soundly asleep between those times, so why let ssh be used? I could still login at the console if I woke up with an urgent need to see an ls of my /tmp directory, but I couldn't ssh in, period. Configuring the time.conf file by itself doesn't affect ssh; we need to add the pam module to /etc/pam.d/sshd. My file ends up looking like this:

#%PAM-1.0
account    required     pam_time.so
auth       required     pam_stack.so service=system-auth
auth       required     pam_nologin.so
account    required     pam_stack.so service=system-auth
password   required     pam_stack.so service=system-auth
session    required     pam_stack.so service=system-auth
session    required     pam_limits.so
session    optional     pam_console.so
 

I put the time.so module first so that it is the very first thing that is checked. If that module doesn't give sshd a green light, that's the end of it: no access. That's the meaning of "required": the module HAS to say that it is happy. The "account" type is specified here. That's a bit of a confusing thing: we have "account", "auth", "password" and "session". The man page isn't all that helpful:


       account - provide account verification types of service: has the user's
       password expired?; is this user permitted access to the requested  ser-
       vice?

       authentication  - establish the user is who they claim to be. Typically
       this is via some challenge-response request that the user must satisfy:
       if  you  are  who  you claim to be please enter your password.  Not all
       authentications are of this type, there exist hardware based  authenti-
       cation  schemes (such as the use of smart-cards and biometric devices),
       with suitable modules, these may be  substituted  seamlessly  for  more
       standard  approaches  to  authentication  -  such is the flexibility of
       Linux-PAM.

       password - this group's responsibility is the task of updating  authen-
       tication  mechanisms.  Typically, such services are strongly coupled to
       those of the auth group. Some authentication mechanisms lend themselves
       well  to  being  updated  with such a function. Standard UN*X password-
       based access is the obvious example: please enter a  replacement  pass-
       word.

       session - this group of tasks cover things that should be done prior to
       a service being given and after it is withdrawn. Such tasks include the
       maintenance  of audit trails and the mounting of the user's home direc-
       tory. The session management group is important as it provides both  an
       opening  and  closing hook for modules to affect the services available
       to a user.
 

I think that the distinction between account and session in that man page is a little confusing. I think it would be quite reasonable to think you should use "session" for this module. Now, sometimes you have a man page for the module that shows you what to use, but pam_time doesn't help us there. Technically, it's not up to the library: the application is the one that is checking with account or session, but keep this in mind: session happens AFTER authentication. I liked the older PAM manual better, which said:

      auth modules provide the actual authentication, perhaps asking
      for and checking a password, and they set "credentials" such
      as group membership or kerberos "tickets."

      account modules check to make sure that the authentication
      is allowed (the account has not expired, the user is allowed
      to log in at this time of day, and so on).

      password modules are used to set passwords.

      session modules are used once a user has been authenticated
      to allow them to use their account, perhaps mounting the
      user's home directory or making their mailbox available.
 

For me, that was more clear.

Stacking

In this case, I only wanted to apply this restriction to ssh. If I'm physically at the box, I want no time restrictions. If I DID want these same restrictions, I'd make the same change to /etc/pam.d/login. But what if there are a whole bunch of things I want to apply the same rules to? RedHat has a special module "pam_stack". It functions much like an "include" statement in any programming language. We saw it in my /etc/pamd/sshd file:

auth       required     pam_stack.so service=system-auth
 

That says to look in /etc/pam.d/system-auth for other modules to use. Both login and sshd have this line (as does just about every other file in /etc/pam.d/), so we can look in system-auth to see what gets called by them:

#%PAM-1.0
# This file is auto-generated.
# User changes will be destroyed the next time authconfig is run.
auth        required      /lib/security/$ISA/pam_env.so
auth        sufficient    /lib/security/$ISA/pam_unix.so likeauth nullok
auth        required      /lib/security/$ISA/pam_deny.so
auth required /lib/security/$ISA/pam_tally.so no_magic_root onerr=fail
account     required      /lib/security/$ISA/pam_unix.so
account    required      /lib/security/$ISA/pam_tally.so onerr=fail file=/var/log/faillog deny=1 no_magic_root even_deny_root_account

password    required      /lib/security/$ISA/pam_cracklib.so retry=3 type=
password    sufficient    /lib/security/$ISA/pam_unix.so nullok use_authtok md5 shadow
password    required      /lib/security/$ISA/pam_deny.so

session     required      /lib/security/$ISA/pam_limits.so
session     required      /lib/security/$ISA/pam_unix.so
 

Therefor, if we really wanted our time restrictions to apply to just about everything, we could add it to system-auth. Note the warning about authconfig though, and also consider that you will be making sudden sweeping changes to a LOT of applications and services.

Other

What if a PAM aware app doesn't have a file in /etc/pam.d? In that case, it uses the "other" file, which looks like this by default:

#%PAM-1.0
auth     required       /lib/security/$ISA/pam_deny.so
account  required       /lib/security/$ISA/pam_deny.so
password required       /lib/security/$ISA/pam_deny.so
session  required       /lib/security/$ISA/pam_deny.so
 

That "deny" module is a flat-out no access, red light, stop you dead right here module that is always going to say no. That's excellent from a security point of view, but can be a bit harsh should you accidentally delete something like "login". Login would now use the "other" file, and you couldn't login. That could be unpleasant.

There are many, many useful and clever PAM modules. While our brain wave interpreter doesn't exist yet, many other possibilities are available to you. There are modules to automatically black list hosts that have many failed logins, and much more. See http://www.kernel.org/pub/linux/libs/pam/modules.html.

Wouldn't it be nice if EVERY application were PAM aware? Imagine limiting vi access to certain files, or not allowing rm to remove certain files. Sure, you can do that other ways, but doing it with PAM could give you fine grained control. Maybe someday more apps will be..




If this page was useful to you, please help others find it:  





30 comments




More Articles by - Find me on Google+



Click here to add your comments
- no registration needed!




Sun Mar 13 19:11:34 2005: 170   anonymous


Nice writeup... good start of unraveling the mysteries of PAM.

Thanks!



Sat Mar 26 11:18:04 2005: 237   anonymous


Thanks!
Seems to have solved a login problem ...



Sun May 29 05:14:12 2005: 589   anonymous


The article helped to solve my time login problem! I followed the example for ssh and tried that for login service and most important of all, the order of the line added in sshd is crucial. Thanks again!



Thu Jul 21 09:18:09 2005: 827   solarix




Thanks, I finally understood the role of system-auth file



What is still unknown to me is the definition of the $ISA environment variable




Thu Jul 21 10:10:22 2005: 828   TonyLawrence

gravatar
I don't know. It seems to be unset on x86, so it's probably something to do with system architecture. It would drive pam into a subdirectory, obviously.







Fri Nov 11 06:24:58 2005: 1328   anonymous


when i added the time.so to login file ,even root could'nt login.where did i go wrong?



Fri Nov 11 11:31:07 2005: 1330   TonyLawrence

gravatar
You didn't go wrong; if you set valid login times with that module, not even root can bypass them.



Mon Jan 29 20:59:26 2007: 2844   anonymous


This article mentions what $ISA is for: http://www.enterprisenetworkingplanet.com/netsecur/article.php/3514511



Thu Sep 20 04:08:00 2007: 3138   anonymous


Great article, but when I went to cat the .so files, I was not able to read them. Are they complied files? Is there utility that I need to use in order to view them? Thanks for a great article!



Thu Sep 20 10:14:29 2007: 3139   TonyLawrence

gravatar
Yes, they are complied, but this is Linux so the source is available if you want.



Fri Jan 18 22:51:21 2008: 3494   anonymous


Thanks, nicely written; understandable and to the point.




Wed Apr 9 12:57:59 2008: 4016   Damien


Nice !
Thanks



Tue May 26 11:03:49 2009: 6406   Sri

gravatar
Thats Fantastic write-up...Thank you so much...



Tue May 26 12:46:19 2009: 6408   TonyLawrence

gravatar
Probably why it's been plagiarised so widely :-)

Seriously, a lot of websites and news postings have stolen this and published it as their own. Very annoying.. most recently I caught http://marspc.co.il lifting a lot of articles from here. I've asked them to stop, but so far they are ignoring me.. as they run ads, the next step is to notify Google of their violation.



Wed Jul 29 10:49:24 2009: 6708   adam

gravatar
red hat and other systems are using a newer version of pam which uses include control instead of pam stack.



Wed Jul 29 10:59:36 2009: 6709   TonyLawrence

gravatar
Thanks - that's a welcome addition.



Thu Sep 9 04:37:02 2010: 8964   AdamLogan

gravatar


Wow. Thanks for describing Pam =). Was reading up via "man sshd_conf" and was wondering what pam was all about. Now I got an idea. I'm kinda shocked at all the files already populated in /etc/pam.d/. I'm glad I came across this article. Will need to read more on the topic later. Even though this article was written in 2005! It still explains the concept clearly.



Thu Nov 11 13:20:29 2010: 9109   anonymous

gravatar


I am not able to login as root from console in runlevel.
have checked from runlevel 1 in message , say pam_tally reset option unkown.

How do i fix this.



Thu Nov 11 14:11:09 2010: 9110   TonyLawrence

gravatar


Why would you be checking in single user mode?

Is the machine broken in some other way?



Thu Nov 11 14:17:12 2010: 9111   anonymous

gravatar


I am able to login remotely , but not from console. The remote users doesnt have su login enabled by default , so had to go into single user mode to grant trsusted access to a sysadmin user.

I checked the /var/log/messages and found following errors:
Nov 11 14:15:14 vmwas7 login[2995]: PAM unable to dlopen(/lib64/security/pam_laus.so)
Nov 11 14:15:14 vmwas7 login[2995]: PAM [error: /lib64/security/pam_laus.so: cannot open shared object file: No such file or directory]
Nov 11 14:15:14 vmwas7 login[2995]: PAM adding faulty module: /lib64/security/pam_laus.so
Nov 11 14:15:20 vmwas7 login[2997]: PAM unable to dlopen(/lib64/security/pam_laus.so)
Nov 11 14:15:20 vmwas7 login[2997]: PAM [error: /lib64/security/pam_laus.so: cannot open shared object file: No such file or directory]
Nov 11 14:15:20 vmwas7 login[2997]: PAM adding faulty module: /lib64/security/pam_laus.so
Nov 11 14:15:25 vmwas7 login[2997]: Module is unknown


Not sure what is wrong , no joy for long time.



Sun Nov 6 17:52:18 2011: 10133   anonymous

gravatar


Nice to find some good info on the net.
tired of the crap people post.

Thank you so much!



Tue May 22 17:07:49 2012: 10998   anonymous

gravatar


it'd been nice to include how to add an entry to a pam.d module using authconfig






Tue May 22 18:01:40 2012: 11000   TonyLawrence

gravatar


I dunno. I tend to avoid tools like that if I have any choice - I don't trust them.



Thu Jun 7 05:59:48 2012: 11069   anonymous

gravatar


Hi,
pam_tally tool shows number of bad attempts by a user by using /var/log/faillog database. And after lockout time expires, with a correct login attempt count gets cleared. can someone tell me is there any way I can clear tally account automatically after the lockout time expires for a user.
I want to clear tally automatically once lockout time expires, don't want to wait for user to login again with correct credentials.
Any help is highly appreciated.
Thanks



Thu Jun 7 12:04:08 2012: 11070   TonyLawrence

gravatar


I'm not sure I understand.

faillog -r doesn't do what you want?






Tue Jun 19 18:34:42 2012: 11117   Andy

gravatar


I've created a Radius Client modified for my needs which is pamified. My entries in sshd are as follows:
#RADIUS AUTHENTICATION
auth sufficient /lib/libPAMradius.so debug
account sufficient /lib/libPAMradius.so debug

When I attempt to log in using ssh, I'm getting authentication failures always. Do I need to include a different PAM module in order to receive the username/password from the ssh application? I've implemented a conversation function, but don't think I'm getting the password from the right location. I"ve put print statements into my code and find that the pam_sm_authenticate function is never called.

Any help you can give would be much appreciated. If I'm in the incorrect forum, let me know.



Tue Jun 19 18:50:29 2012: 11118   TonyLawrence

gravatar


Well, this isn't a "forum" :)

But I can't help you with that anyway, sorry. Out of my pay grade.



Thu Apr 18 09:15:40 2013: 12037   anonymous

gravatar


thanks very much indeed, actually I was studding PAM for SUSE SLES 11 and I found this article very well explained.



Wed Jun 4 12:45:53 2014: Website: 12476   DavidSpector

gravatar


I added a link to your article to the Wikipedia article on PAM. Your article explains it much better than the existing WP article, in my opinion.



Wed Jun 4 12:49:27 2014: Website: 12477   TonyLawrence

gravatar


Thanks, though they'll probably delete it if past experience is any guide. I've had people add links before to other things and they get booted quickly. Wikipedia seems to prefer "big name" sites.

Don't miss responses! Subscribe to Comments by RSS or by Email

Click here to add your comments


If you want a picture to show with your comment, go get a Gravatar

Kerio Connect Mailserver

Kerio Samepage

Kerio Control Firewall

Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more. We appreciate comments and article submissions.

Publishing your articles here

Jump to Comments



Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.

I am a Kerio reseller. Articles here related to Kerio products reflect my honest opinion, but I do have an obvious interest in selling those products also.

Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.

We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.

pavatar.jpg

This post tagged:

       - Administration
       - Basics
       - Popular
       - Security



















My Troubleshooting E-Book will show you how to solve tough problems on Linux and Unix systems!


book graphic unix and linux troubleshooting guide



Buy Kerio from a dealer
who knows tech:
I sell and support

Kerio Connect Mail server, Control, Workspace and Operator licenses and subscription renewals



Click and enter your name and phone number to call me about Kerio® products right now (Flash required)