pam-module.c revision bb29785e0df6a7cf07db0259a60bc1f3b4814cb4
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <errno.h>
#include <fcntl.h>
#include <pwd.h>
#include <endian.h>
#include <security/pam_modules.h>
#include <security/_pam_macros.h>
#include <security/pam_modutil.h>
#include <security/pam_misc.h>
#include "util.h"
#include "cgroup-util.h"
#include "macro.h"
#include "sd-daemon.h"
#include "strv.h"
bool *create_session,
bool *kill_session,
bool *kill_user,
char ***controllers,
char ***reset_controllers,
char ***kill_only_users,
char ***kill_exclude_users) {
unsigned i;
bool reset_controller_set = false;
bool kill_exclude_users_set = false;
for (i = 0; i < (unsigned) argc; i++) {
int k;
return k;
}
if (create_session)
*create_session = k;
return k;
}
if (kill_session)
*kill_session = k;
return k;
}
if (kill_user)
*kill_user = k;
if (controllers) {
char **l;
return -ENOMEM;
}
*controllers = l;
}
if (reset_controllers) {
char **l;
return -ENOMEM;
}
*reset_controllers = l;
}
reset_controller_set = true;
if (kill_only_users) {
char **l;
return -ENOMEM;
}
*kill_only_users = l;
}
if (kill_exclude_users) {
char **l;
return -ENOMEM;
}
*kill_exclude_users = l;
}
kill_exclude_users_set = true;
} else {
return -EINVAL;
}
}
if (!reset_controller_set && reset_controllers) {
char **l;
return -ENOMEM;
}
*reset_controllers = l;
}
if (controllers)
if (reset_controllers)
*kill_user = true;
if (!kill_exclude_users_set && kill_exclude_users) {
char **l;
return -ENOMEM;
}
*kill_exclude_users = l;
}
return 0;
}
static int open_file_and_lock(const char *fn) {
int fd;
return -errno;
/* The BSD socket semantics are a lot nicer than those of
* POSIX locks. Which is why we use flock() here. BSD locking
* does not work across NFS which however is not needed here
* as the filesystems in question should be local, and only
* locally accessible, and most likely even tmpfs. */
return -errno;
}
return fd;
}
enum {
SESSION_ID_AUDIT = 'a',
SESSION_ID_COUNTER = 'c',
SESSION_ID_RANDOM = 'r'
};
char *s;
int fd;
/* First attempt: let's use the session ID of the audit
* system, if it is available. */
if (read_one_line_file("/proc/self/sessionid", &s) >= 0) {
uint32_t u;
int r;
r = safe_atou32(s, &u);
free(s);
if (r >= 0 && u != (uint32_t) -1 && u > 0) {
*mode = SESSION_ID_AUDIT;
return (uint64_t) u;
}
}
/* Second attempt, use our own counter. */
ssize_t r;
/* We do a bit of endianess swapping here, just to be
* sure. /run should be machine specific anyway, and
* even mounted from tmpfs, so this byteswapping
* should really not be necessary. But then again, you
* never know, so let's avoid any risk. */
counter = 1;
else
if (r != sizeof(swapped))
r = -EIO;
} else
r = -errno;
if (r >= 0) {
return counter;
}
}
/* Last attempt, pick a random value */
return (uint64_t) random_ull();
}
static int get_user_data(
const char **ret_username,
int r;
bool have_loginuid = false;
char *s;
if (read_one_line_file("/proc/self/loginuid", &s) >= 0) {
uint32_t u;
r = safe_atou32(s, &u);
free(s);
if (r >= 0 && u != (uint32_t) -1 && u > 0) {
have_loginuid = true;
}
}
if (!have_loginuid) {
return r;
}
return PAM_AUTH_ERR;
}
}
if (!pw) {
return PAM_USER_UNKNOWN;
}
return PAM_SUCCESS;
}
static int create_user_group(
const char *controller,
const char *group,
bool attach,
bool remember) {
int r;
if (attach)
else
if (r < 0) {
return PAM_SESSION_ERR;
}
if (r > 0 && remember) {
/* Remember that it was us who created this group, and
* that hence we need to remove it too. This is a
* protection against removing the cgroup when run
* recursively. */
return r;
}
}
return PAM_SESSION_ERR;
}
return PAM_SUCCESS;
}
static int reset_group(
const char *controller) {
int r;
pam_syslog(handle, LOG_ERR, "Failed to reset cgroup for controller %s: %s", controller, strerror(-r));
return PAM_SESSION_ERR;
}
return PAM_SUCCESS;
}
int flags,
int r;
int lock_fd = -1;
bool create_session = true;
char *cgroup_user_tree = NULL;
/* pam_syslog(handle, LOG_DEBUG, "pam-systemd initializing"); */
/* Make this a NOP on non-systemd systems */
if (sd_booted() <= 0)
return PAM_SUCCESS;
if (parse_argv(handle,
return PAM_SESSION_ERR;
goto finish;
if ((r = cg_get_user_path(&cgroup_user_tree)) < 0) {
r = PAM_SYSTEM_ERR;
goto finish;
}
r = PAM_SYSTEM_ERR;
goto finish;
}
r = PAM_SYSTEM_ERR;
goto finish;
}
r = PAM_BUF_ERR;
goto finish;
}
r = PAM_SYSTEM_ERR;
goto finish;
goto finish;
}
if (create_session) {
const char *id;
/* Reuse or create XDG session ID */
int mode;
r = PAM_BUF_ERR;
goto finish;
}
/* To avoid id clashes we add the session id
* source to our session ids. Note that the
* session id source might change during
* runtime, because a filesystem became
* writable or the system reconfigured. */
goto finish;
}
r = PAM_SESSION_ERR;
goto finish;
}
}
} else
if (r < 0) {
r = PAM_BUF_ERR;
goto finish;
}
pam_syslog(handle, LOG_INFO, "Moving new user session for %s into control group %s.", username, buf);
goto finish;
/* The additional controllers don't really matter, so we
* ignore the return value */
reset_group(handle, *c);
r = PAM_SUCCESS;
if (lock_fd >= 0)
return r;
}
int r;
bool remains = false;
DIR *d;
char *subgroup;
return r;
while ((r = cg_read_subgroup(d, &subgroup)) > 0) {
if (remains)
break;
}
closedir(d);
if (r < 0)
return r;
return !!remains;
}
static bool check_user_lists(
char **kill_only_users,
char **kill_exclude_users) {
char **l;
if (uid == 0)
else {
}
if (safe_atou32(*l, &id) >= 0)
return false;
return false;
}
if (strv_isempty(kill_only_users))
return true;
STRV_FOREACH(l, kill_only_users) {
if (safe_atou32(*l, &id) >= 0)
return true;
return true;
}
return false;
}
int flags,
bool kill_session = false;
bool kill_user = false;
int lock_fd = -1, r;
const char *id;
char *cgroup_user_tree = NULL;
/* Make this a NOP on non-systemd systems */
if (sd_booted() <= 0)
return PAM_SUCCESS;
if (parse_argv(handle,
&controllers, NULL,
&kill_only_users, &kill_exclude_users) < 0)
return PAM_SESSION_ERR;
goto finish;
if ((r = cg_get_user_path(&cgroup_user_tree)) < 0) {
r = PAM_SYSTEM_ERR;
goto finish;
}
r = PAM_SYSTEM_ERR;
goto finish;
}
if ((r = cg_attach(*c, cgroup_user_tree, 0)) < 0)
r = PAM_BUF_ERR;
goto finish;
}
r = PAM_BUF_ERR;
goto finish;
}
pam_syslog(handle, LOG_INFO, "Killing remaining processes of user session %s of %s.", id, username);
/* Kill processes in session cgroup, and delete it */
} else {
pam_syslog(handle, LOG_INFO, "Moving remaining processes of user session %s of %s into control group %s.", id, username, nosession_path);
/* Migrate processes from session to user
* cgroup. First, try to create the user group
* in case it doesn't exist yet. Also, delete
* the session group. */
if ((r = cg_migrate_recursive(SYSTEMD_CGROUP_CONTROLLER, session_path, nosession_path, false, true)) < 0)
}
STRV_FOREACH(c, controllers) {
pam_syslog(handle, LOG_ERR, "Failed to migrate session cgroup in hierarchy %s: %s", *c, strerror(-r));
}
}
/* GC user tree */
/* Kill user processes not attached to any session */
if (kill_user && r == 0 && check_user_lists(handle, pw->pw_uid, kill_only_users, kill_exclude_users)) {
/* Kill user cgroup */
} else {
/* Remove user cgroup */
if (r > 0) {
/* If we managed to find somebody, don't cleanup the cgroup. */
} else if (r == 0)
r = -EBUSY;
}
if (r >= 0) {
const char *runtime_dir;
if ((r = rm_rf(runtime_dir, false, true)) < 0)
}
/* pam_syslog(handle, LOG_DEBUG, "pam-systemd done"); */
r = PAM_SUCCESS;
if (lock_fd >= 0)
return r;
}