(root)/
Linux-PAM-1.5.3/
modules/
pam_unix/
pam_unix_acct.c
       1  /*
       2   * pam_unix account management
       3   *
       4   * Copyright Elliot Lee, 1996.  All rights reserved.
       5   * Copyright Jan Rękorajski, 1999.  All rights reserved.
       6   *
       7   * Redistribution and use in source and binary forms, with or without
       8   * modification, are permitted provided that the following conditions
       9   * are met:
      10   * 1. Redistributions of source code must retain the above copyright
      11   *    notice, and the entire permission notice in its entirety,
      12   *    including the disclaimer of warranties.
      13   * 2. Redistributions in binary form must reproduce the above copyright
      14   *    notice, this list of conditions and the following disclaimer in the
      15   *    documentation and/or other materials provided with the distribution.
      16   * 3. The name of the author may not be used to endorse or promote
      17   *    products derived from this software without specific prior
      18   *    written permission.
      19   *
      20   * ALTERNATIVELY, this product may be distributed under the terms of
      21   * the GNU Public License, in which case the provisions of the GPL are
      22   * required INSTEAD OF the above restrictions.  (This clause is
      23   * necessary due to a potential bad interaction between the GPL and
      24   * the restrictions contained in a BSD-style copyright.)
      25   *
      26   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
      27   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
      28   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
      29   * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
      30   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
      31   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
      32   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
      33   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
      34   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
      35   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
      36   * OF THE POSSIBILITY OF SUCH DAMAGE.
      37   */
      38  
      39  #include "config.h"
      40  
      41  #include <stdlib.h>
      42  #include <stdio.h>
      43  #include <string.h>
      44  #include <unistd.h>
      45  #include <sys/types.h>
      46  #include <sys/resource.h>
      47  #include <syslog.h>
      48  #include <pwd.h>
      49  #include <shadow.h>
      50  #include <time.h>		/* for time() */
      51  #include <errno.h>
      52  #include <sys/wait.h>
      53  
      54  #include <security/_pam_macros.h>
      55  
      56  #include <security/pam_modules.h>
      57  #include <security/pam_ext.h>
      58  #include <security/pam_modutil.h>
      59  
      60  #include "pam_cc_compat.h"
      61  #include "support.h"
      62  #include "passverify.h"
      63  
      64  int _unix_run_verify_binary(pam_handle_t *pamh, unsigned long long ctrl,
      65  	const char *user, int *daysleft)
      66  {
      67    int retval=0, child, fds[2];
      68    struct sigaction newsa, oldsa;
      69    D(("running verify_binary"));
      70  
      71    /* create a pipe for the messages */
      72    if (pipe(fds) != 0) {
      73      D(("could not make pipe"));
      74      pam_syslog(pamh, LOG_ERR, "Could not make pipe: %m");
      75      return PAM_AUTH_ERR;
      76    }
      77    D(("called."));
      78  
      79    if (off(UNIX_NOREAP, ctrl)) {
      80      /*
      81       * This code arranges that the demise of the child does not cause
      82       * the application to receive a signal it is not expecting - which
      83       * may kill the application or worse.
      84       *
      85       * The "noreap" module argument is provided so that the admin can
      86       * override this behavior.
      87       */
      88       memset(&newsa, '\0', sizeof(newsa));
      89       newsa.sa_handler = SIG_DFL;
      90       sigaction(SIGCHLD, &newsa, &oldsa);
      91    }
      92  
      93    /* fork */
      94    child = fork();
      95    if (child == 0) {
      96      static char *envp[] = { NULL };
      97      const char *args[] = { NULL, NULL, NULL, NULL };
      98  
      99      /* XXX - should really tidy up PAM here too */
     100  
     101      /* reopen stdout as pipe */
     102      if (dup2(fds[1], STDOUT_FILENO) != STDOUT_FILENO) {
     103        pam_syslog(pamh, LOG_ERR, "dup2 of %s failed: %m", "stdout");
     104        _exit(PAM_AUTHINFO_UNAVAIL);
     105      }
     106  
     107      if (pam_modutil_sanitize_helper_fds(pamh, PAM_MODUTIL_PIPE_FD,
     108  					PAM_MODUTIL_IGNORE_FD,
     109  					PAM_MODUTIL_PIPE_FD) < 0) {
     110        _exit(PAM_AUTHINFO_UNAVAIL);
     111      }
     112  
     113      if (geteuid() == 0) {
     114        /* must set the real uid to 0 so the helper will not error
     115           out if pam is called from setuid binary (su, sudo...) */
     116        if (setuid(0) == -1) {
     117            pam_syslog(pamh, LOG_ERR, "setuid failed: %m");
     118            printf("-1\n");
     119            fflush(stdout);
     120            _exit(PAM_AUTHINFO_UNAVAIL);
     121        }
     122      }
     123  
     124      /* exec binary helper */
     125      args[0] = CHKPWD_HELPER;
     126      args[1] = user;
     127      args[2] = "chkexpiry";
     128  
     129      DIAG_PUSH_IGNORE_CAST_QUAL;
     130      execve(CHKPWD_HELPER, (char *const *) args, envp);
     131      DIAG_POP_IGNORE_CAST_QUAL;
     132  
     133      pam_syslog(pamh, LOG_ERR, "helper binary execve failed: %m");
     134      /* should not get here: exit with error */
     135      D(("helper binary is not available"));
     136      printf("-1\n");
     137      fflush(stdout);
     138      _exit(PAM_AUTHINFO_UNAVAIL);
     139    } else {
     140      close(fds[1]);
     141      if (child > 0) {
     142        char buf[32];
     143        int rc=0;
     144        /* wait for helper to complete: */
     145        while ((rc=waitpid(child, &retval, 0)) < 0 && errno == EINTR);
     146        if (rc<0) {
     147  	pam_syslog(pamh, LOG_ERR, "unix_chkpwd waitpid returned %d: %m", rc);
     148  	retval = PAM_AUTH_ERR;
     149        } else if (!WIFEXITED(retval)) {
     150          pam_syslog(pamh, LOG_ERR, "unix_chkpwd abnormal exit: %d", retval);
     151          retval = PAM_AUTH_ERR;
     152        } else {
     153  	retval = WEXITSTATUS(retval);
     154          rc = pam_modutil_read(fds[0], buf, sizeof(buf) - 1);
     155  	if(rc > 0) {
     156  	      buf[rc] = '\0';
     157  	      if (sscanf(buf,"%d", daysleft) != 1 )
     158  	        retval = PAM_AUTH_ERR;
     159  	    }
     160  	else {
     161  	    pam_syslog(pamh, LOG_ERR, "read unix_chkpwd output error %d: %m", rc);
     162  	    retval = PAM_AUTH_ERR;
     163  	  }
     164        }
     165      } else {
     166        pam_syslog(pamh, LOG_ERR, "Fork failed: %m");
     167        D(("fork failed"));
     168        retval = PAM_AUTH_ERR;
     169      }
     170      close(fds[0]);
     171    }
     172  
     173    if (off(UNIX_NOREAP, ctrl)) {
     174          sigaction(SIGCHLD, &oldsa, NULL);   /* restore old signal handler */
     175    }
     176  
     177    D(("Returning %d",retval));
     178    return retval;
     179  }
     180  
     181  /*
     182   * PAM framework looks for this entry-point to pass control to the
     183   * account management module.
     184   */
     185  
     186  int
     187  pam_sm_acct_mgmt(pam_handle_t *pamh, int flags, int argc, const char **argv)
     188  {
     189  	unsigned long long ctrl;
     190  	const void *void_uname;
     191  	const char *uname;
     192  	int retval, daysleft = -1;
     193  	char buf[256];
     194  
     195  	D(("called."));
     196  
     197  	ctrl = _set_ctrl(pamh, flags, NULL, NULL, NULL, argc, argv);
     198  
     199  	retval = pam_get_item(pamh, PAM_USER, &void_uname);
     200  	uname = void_uname;
     201  	D(("user = `%s'", uname));
     202  	if (retval != PAM_SUCCESS || uname == NULL) {
     203  		pam_syslog(pamh, LOG_ERR,
     204  			 "could not identify user (from uid=%lu)",
     205  			 (unsigned long int)getuid());
     206  		return PAM_USER_UNKNOWN;
     207  	}
     208  
     209  	retval = _unix_verify_user(pamh, ctrl, uname, &daysleft);
     210  
     211  	if (on(UNIX_NO_PASS_EXPIRY, ctrl)) {
     212  		const void *pretval = NULL;
     213  		int authrv = PAM_AUTHINFO_UNAVAIL; /* authentication not called */
     214  
     215  		if (pam_get_data(pamh, "unix_setcred_return", &pretval) == PAM_SUCCESS
     216  			&& pretval)
     217  			authrv = *(const int *)pretval;
     218  
     219  		if (authrv != PAM_SUCCESS
     220  			&& (retval == PAM_NEW_AUTHTOK_REQD || retval == PAM_AUTHTOK_EXPIRED))
     221  			retval = PAM_SUCCESS;
     222  	}
     223  
     224  	switch (retval) {
     225  	case PAM_ACCT_EXPIRED:
     226  		pam_syslog(pamh, LOG_NOTICE,
     227  			"account %s has expired (account expired)",
     228  			uname);
     229  		_make_remark(pamh, ctrl, PAM_ERROR_MSG,
     230  			_("Your account has expired; please contact your system administrator."));
     231  		break;
     232  	case PAM_NEW_AUTHTOK_REQD:
     233  		if (daysleft == 0) {
     234  			pam_syslog(pamh, LOG_NOTICE,
     235  				"expired password for user %s (root enforced)",
     236  				uname);
     237  			_make_remark(pamh, ctrl, PAM_ERROR_MSG,
     238  				_("You are required to change your password immediately (administrator enforced)."));
     239  		} else {
     240  			pam_syslog(pamh, LOG_DEBUG,
     241  				"expired password for user %s (password aged)",
     242  				uname);
     243  			_make_remark(pamh, ctrl, PAM_ERROR_MSG,
     244  				_("You are required to change your password immediately (password expired)."));
     245  		}
     246  		break;
     247  	case PAM_AUTHTOK_EXPIRED:
     248  		pam_syslog(pamh, LOG_NOTICE,
     249  			"account %s has expired (failed to change password)",
     250  			uname);
     251  		_make_remark(pamh, ctrl, PAM_ERROR_MSG,
     252  			_("Your account has expired; please contact your system administrator."));
     253  		break;
     254  	case PAM_AUTHTOK_ERR:
     255  		/*
     256  		 * We ignore "password changed too early" error
     257  		 * as it is relevant only for password change.
     258  		 */
     259  		retval = PAM_SUCCESS;
     260  		/* fallthrough */
     261  	case PAM_SUCCESS:
     262  		if (daysleft >= 0) {
     263  			pam_syslog(pamh, LOG_DEBUG,
     264  				"password for user %s will expire in %d days",
     265  				uname, daysleft);
     266  #if defined HAVE_DNGETTEXT && defined ENABLE_NLS
     267  			snprintf (buf, sizeof (buf),
     268  				dngettext(PACKAGE,
     269  				  "Warning: your password will expire in %d day.",
     270  				  "Warning: your password will expire in %d days.",
     271  				  daysleft),
     272  				daysleft);
     273  #else
     274  			if (daysleft == 1)
     275  			    snprintf(buf, sizeof (buf),
     276  				_("Warning: your password will expire in %d day."),
     277  				daysleft);
     278  			else
     279  			    snprintf(buf, sizeof (buf),
     280  			    /* TRANSLATORS: only used if dngettext is not supported */
     281  				_("Warning: your password will expire in %d days."),
     282  				daysleft);
     283  #endif
     284  			_make_remark(pamh, ctrl, PAM_TEXT_INFO, buf);
     285  		}
     286  	}
     287  
     288  	D(("all done"));
     289  
     290  	return retval;
     291  }