pam-module.c revision 35d2e7ec19f8d3960a14dc04642060ccee3faa43
/*-*- Mode: C; c-basic-offset: 8 -*-*/
/***
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 <libcgroup.h>
#include "util.h"
#include "cgroup-util.h"
#include "macro.h"
#include "sd-daemon.h"
bool *create_session,
bool *kill_session,
bool *kill_user) {
unsigned i;
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;
} else {
return -EINVAL;
}
}
*kill_user = true;
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) {
*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. /var should be machine specific anyway, and
* 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,
const char *username;
int r;
return r;
}
return PAM_AUTH_ERR;
}
return PAM_USER_UNKNOWN;
}
*ret_username = username;
return PAM_SUCCESS;
}
static int create_user_group(pam_handle_t *handle, const char *group, struct passwd *pw, bool attach) {
int r;
if (attach)
else
if (r < 0) {
return PAM_SESSION_ERR;
}
return PAM_SESSION_ERR;
}
return PAM_SUCCESS;
}
int flags,
int r;
int lock_fd = -1;
bool create_session = true;
return PAM_SESSION_ERR;
/* Make this a NOP on non-systemd systems */
if (sd_booted() <= 0)
return PAM_SUCCESS;
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;
}
goto finish;
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;
}
int flags,
bool kill_session = false;
bool kill_user = false;
int lock_fd = -1, r;
const char *id;
return PAM_SESSION_ERR;
/* Make this a NOP on non-systemd systems */
if (sd_booted() <= 0)
return PAM_SUCCESS;
goto finish;
r = PAM_SYSTEM_ERR;
goto finish;
}
/* We are probably still in some session/no-session dir. Move ourselves out of the way as first step */
r = PAM_BUF_ERR;
goto finish;
}
r = PAM_BUF_ERR;
goto finish;
}
if (kill_session) {
/* Kill processes in session cgroup, and delete it */
} else {
/* Migrate processes from session to
* no-session cgroup. First, try to create the
* no-session 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)
}
}
/* GC user tree */
/* Kill user processes not attached to any session */
if (kill_user && r == 0) {
/* Kill no-session 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;
/* This will migrate us to the /user cgroup. */
if ((r = rm_rf(runtime_dir, false, true)) < 0)
}
r = PAM_SUCCESS;
if (lock_fd >= 0)
return r;
}