(root)/
Linux-PAM-1.5.3/
modules/
pam_unix/
pam_unix_sess.c
       1  /*
       2   * pam_unix session management
       3   *
       4   * Copyright Alexander O. Yuriev, 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 <stdio.h>
      42  #include <stdlib.h>
      43  #include <stdarg.h>
      44  #include <unistd.h>
      45  #include <syslog.h>
      46  #include <fcntl.h>
      47  #include <sys/types.h>
      48  #include <sys/stat.h>
      49  
      50  #include <security/_pam_macros.h>
      51  #include <security/pam_modules.h>
      52  #include <security/pam_ext.h>
      53  #include <security/pam_modutil.h>
      54  
      55  #include "support.h"
      56  
      57  /*
      58   * PAM framework looks for these entry-points to pass control to the
      59   * session module.
      60   */
      61  
      62  int
      63  pam_sm_open_session(pam_handle_t *pamh, int flags, int argc, const char **argv)
      64  {
      65  	char *user_name, *service;
      66  	unsigned long long ctrl;
      67  	int retval;
      68  	const char *login_name;
      69  
      70  	D(("called."));
      71  
      72  	ctrl = _set_ctrl(pamh, flags, NULL, NULL, NULL, argc, argv);
      73  
      74  	retval = pam_get_item(pamh, PAM_USER, (void *) &user_name);
      75  	if (user_name == NULL || *user_name == '\0' || retval != PAM_SUCCESS) {
      76  		pam_syslog(pamh, LOG_ERR,
      77  			"open_session - error recovering username");
      78  		return PAM_SESSION_ERR;		/* How did we get authenticated with
      79  						   no username?! */
      80  	}
      81  	retval = pam_get_item(pamh, PAM_SERVICE, (void *) &service);
      82  	if (service == NULL || *service == '\0' || retval != PAM_SUCCESS) {
      83  		pam_syslog(pamh, LOG_CRIT,
      84  			"open_session - error recovering service");
      85  		return PAM_SESSION_ERR;
      86  	}
      87  	login_name = pam_modutil_getlogin(pamh);
      88  	if (login_name == NULL) {
      89  		login_name = "";
      90  	}
      91  	if (off (UNIX_QUIET, ctrl)) {
      92  		char uid[32];
      93  		struct passwd *pwd = pam_modutil_getpwnam (pamh, user_name);
      94  		if (pwd == NULL) {
      95  			snprintf (uid, 32, "getpwnam error");
      96  		}
      97  		else {
      98  			snprintf (uid, 32, "%u", pwd->pw_uid);
      99  		}
     100  		pam_syslog(pamh, LOG_INFO, "session opened for user %s(uid=%s) by %s(uid=%lu)", user_name, uid, login_name, (unsigned long)getuid());
     101  	}
     102  	return PAM_SUCCESS;
     103  }
     104  
     105  int
     106  pam_sm_close_session(pam_handle_t *pamh, int flags, int argc, const char **argv)
     107  {
     108  	char *user_name, *service;
     109  	unsigned long long ctrl;
     110  	int retval;
     111  
     112  	D(("called."));
     113  
     114  	ctrl = _set_ctrl(pamh, flags, NULL, NULL, NULL, argc, argv);
     115  
     116  	retval = pam_get_item(pamh, PAM_USER, (void *) &user_name);
     117  	if (user_name == NULL || *user_name == '\0' || retval != PAM_SUCCESS) {
     118  		pam_syslog(pamh, LOG_ERR,
     119  			"close_session - error recovering username");
     120  		return PAM_SESSION_ERR;		/* How did we get authenticated with
     121  						   no username?! */
     122  	}
     123  	retval = pam_get_item(pamh, PAM_SERVICE, (void *) &service);
     124  	if (service == NULL || *service == '\0' || retval != PAM_SUCCESS) {
     125  		pam_syslog(pamh, LOG_CRIT,
     126  			"close_session - error recovering service");
     127  		return PAM_SESSION_ERR;
     128  	}
     129  	if (off (UNIX_QUIET, ctrl))
     130  		pam_syslog(pamh, LOG_INFO, "session closed for user %s",
     131  			user_name);
     132  
     133  	return PAM_SUCCESS;
     134  }