Git Product home page Git Product logo

libpam-policycache's Introduction

Overview

Caches passwords from other PAM modules and bypasses those modules based on a configurable policy.

Why use libpam-policycache?

  • Allows users to login when network is down.
  • Reduces latency and load on remote login services.
  • Restricts cache usage to specific users and groups.
  • Enforces policies like cached password lifetime and rotation.

Module Configuration

Arguments:

  • action=check: Prompt the user for a password and check it against the cache.
  • action=update: Set the user's cached password to the last successful password in the stack.
  • try_first_pass: Try to use the password from a previous module (check only).
  • use_first_pass: Only use the password from a previous module (check only).
  • policy=<path-glob>: Default path-glob is /etc/libpam-policycache.d/*.policy.
  • storage=<path>: Default path is /var/cache/libpam-policycache.

Example

auth [success=2 new_authtok_reqd=ok default=ignore] pam_policycache.so action=check
auth [success=ok new_authtok_reqd=ok default=die] pam_krb5.so use_first_pass
auth [default=ignore] pam_policycache.so action=update

Policy Configuration

Each policy file is INI-style with one section for each potential policy. The first of the most specific matching sections is chosen.

The section's name describes who the policy applies to:

  • user:<name>
  • group:<name>
  • netgroup:<name>

Each section has a combination of attributes:

  • tries: Maximum number of failed attempts before entry is invalid.
  • refresh: Duration (1h, 5d, 3w) an entry stays valid between successful uses (action=check).
  • renew: Duration after the last update (action=update) when a successful use (action=check) will return new_authtok_req instead of success. Used when a password should be tried against another module opportunistically before the entry expires.
  • expire: Duration after the last update (action=update) when the cache entry is marked invalid.

TODO

  • Add a backoff timer to rate-limit renew attempts.
  • Add a reset-tries timer to reset the try count on an entry.
  • Choose between "best-match" and "first-match" for the preferred policy.

Example

In the following example policy, user "janedoe" may be the owner of the machine and her login experience is similar to having a password in /etc/shadow. Her password only needs to be checked by another module once a year.

Others in the "users" group may also use the cache, but only for a short time. Their entries are evicted from the cache after an hour without use or two days without being verified again using another module.

# /etc/libpam-policycache.d/foo.policy

[user:janedoe]
renew=1w
expire=52w

[group:users]
tries=3
refresh=1h
renew=1d
expire=2d

Cache Storage

Policy entries are stored in /var/cache/libpam-policycache by default. Each entry is stored in a file for the user it belongs to.

The cache entry format is human-readable for easier debugging:

{'version': <1>, 'tries': <0>, 'algorithm': <'SHA256'>, 'salt': <'0B8BAA809CDCA339910EE8F6F9FE22A5'>, 'hash': <'14BABCBC943B302EFDCC137419F7D3FB736602D77CF42975A6778A5B7F2D63CD'>, 'last_verified': <'2014-03-28T23:14:21Z'>, 'last_used': <'2014-03-28T23:14:21Z'>, 'last_tried': <'2014-03-28T23:14:21Z'>}

User Applications

Applications like screensavers that try to use the cache directly will be denied, since the cache directory is only usable by root. Those applications should use pam_escalate.so instead, which proxies the authentication prompt(s) through a pipe to a setuid-root helper called pam-escalate-helper. The helper calls pam_start() and pam_authenticate() just like an application would, and it always uses /etc/pam.d/escalate for the service configuration. It checks in advance that the user calling it is the same user as the one who should be authenticated so it doesn't work for cases like web oder mail servers who check system users.

The escalate module clears all environment variables, ignores some PAM items, and only implements pam_sm_authenticate() so it may not cover all use cases.

At some point before or during the 1.0 release, the escalate module will be moved to its own project.

Example

# /etc/pam.d/gnome-screensaver
auth requisite pam_escalate.so
# /etc/pam.d/escalate
# Include common-auth where cache check/update and other auth modules live.
@include common-auth

Building

Built with autotools and includes an autogen.sh script:

  1. ./autogen.sh (not needed when using source tarball)
  2. ./configure
  3. make
  4. make check
  5. sudo make install

Dependencies:

  • GLib
  • Linux-PAM
  • libscrypt
  • Autoconf, Automake, Libtool

libpam-policycache's People

Contributors

dwfreed avatar gerow avatar haraldj avatar mcclunge avatar tokkee avatar vonhollen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

libpam-policycache's Issues

add support for yescrypt

Dear Nikki VonHollen,

how do you think about adding yescrypt support as the default encryption method?

Kind regards
Harald Jenny

Add support for GLib 2.62

GTimeVal is deprecated in GLib 2.62, but this project currently uses this in util.c.

I have made some changes to the first 2 functions in util.c to make it work with newer versions of GLib, and thereby also make it compatible with Ubuntu 20.04, while maintaining backwards compatibility with macros:

/**
 * CacheUtilDatetimeToString:
 * @value: Date to create string from. Must be UTC.
 *
 * Returns: (allow-none): Date in "{yyyy}-{mm}-{dd}T{hh}:{mm}:{ss}Z" format
 * (ISO 8601) or #NULL if the date couldn't be serialized.
 */
gchar *CacheUtilDatetimeToString(GDateTime *value) {
  if (!value)
    return NULL;

#if GLIB_CHECK_VERSION(2, 62, 0)
  return g_date_time_format_iso8601(value);
#else
  GTimeVal tv = {0, 0};

  if (g_date_time_to_timeval(value, &tv)) {
    return g_time_val_to_iso8601(&tv);
  } else {
    return NULL;
  }
#endif
}


/**
 * CacheUtilDatetimeFromString:
 * @value: String containing the date, in the "{yyyy}-{mm}-{dd}T{hh}:{mm}:{ss}Z"
 * format (ISO 8601). Dates are always UTC.
 * @result: (out): GDateTime respresenting the given @value.
 *
 * Returns: #TRUE if @result was modified or #FALSE if the date couldn't be
 * parsed.
 */
gboolean CacheUtilDatetimeFromString(const gchar *value, GDateTime **result) {
  g_assert(result);

#if GLIB_CHECK_VERSION(2, 56, 0)
  GDateTime *dt = g_date_time_new_from_iso8601(value, g_time_zone_new_utc());

  if (dt) {
    *result = dt;
#else
  GTimeVal tv = {0, 0};

  if (g_time_val_from_iso8601(value, &tv)) {
    *result = g_date_time_new_from_timeval_utc(&tv);
#endif
    return TRUE;
  } else {
    return FALSE;
  }
}

I hope this can be of help to other users of libpam-policycache.

Kind regards,
Rune Lejbølle

Make cache work for non-root applications

Modules like pam_unix.so have setuid-root helpers like unix_chkpwd so they work from applications like screensavers that run as the user.

The cache will need a similar helper, but it's not so straight-forward. The "check" action is safe to run in a helper, but the "update" action isn't. It's probably not wise to allow a user to set their own password arbitrarily using the helper without providing their old password.

The helper also can't accept arbitrary storage and policy paths, so only the defaults will be allowed for non-root users.

Until some clever solution is found for handling the update action, I'll just make "check" work when run as a normal user for that specific user only. That means the last_verified time and cached password will only be updated during login, sudo, etc. Users who have long-lived graphical sessions should only rely on policies that use a long "refresh" time.

Readme incorrect

It took me a little bit of time to find out one issue with this module is the ini-file style config - not renew: 1w but renew=1w. Could you please correct the Readme accordingly?

Choose between first-match and best-match in CachePolicyNewForUser

The current CachePolicyNewForUser in src/policy.c has to pick one section out of multiple possible matching sections. The current logic uses the first section it finds. Policy filenames are sorted before they are read.

@tokkee proposed an alternate way, picking the best match out of all possible matches. A section like [user:janedoe] would be picked before [group:allusers]. There's still the problem of ordering when multiple sections on the same level are chosen, but we can fallback to first-match between equal sections.

Why to choose first-match:

  • Policy files can be more advanced.
  • Figuring out what policy matches is more simple.
  • The "disable=true" setting can blacklist certain users/groups before any other sections are tried.
  • Netgroups can be preferred over groups.
  • It's still required as a fallback in the best-match implementation.

Why to choose best-match:

  • It's more intuitive. A [user:janedoe] section will always be picked for that user.
  • It's more efficient. Querying large groups/netgroups can be very expensive, so it's nice to only do it when we know it's necessary.

What other pros/cons exist?

Restrict permissions on cache files

We rely on the cache storage directory's permissions to ensure that the cache entries are only readable by root. Instead, we should explicitly chmod the entry files to 0600 when they are written.

pam_escalate not working?

Dear Nikki VonHollen,

I'm currently using your pam module but I face the problem that pam_escalete doesn't seem to work (anymore), could you do a recompile in a Debian unstable environment and tell me if you can get any output from pam_escalete?

Kind regards
Harald Jenny

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.