-
Notifications
You must be signed in to change notification settings - Fork 247
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
openrc-pam: Add pam module to autolaunch openrc --user
the module sets up XDG_RUNTIME_DIR if it's already not set, then uses utmpx to figure out if the user has any open sessions. If not, starts or stops openrc --user. Signed-off-by: Anna (navi) Figueiredo Gomes <[email protected]>
- Loading branch information
Showing
7 changed files
with
295 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
if get_option('pam') and pam_dep.found() | ||
shared_library('pam_openrc', | ||
['openrc-pam.c', misc_c, version_h], | ||
c_args : [cc_branding_flags], | ||
dependencies : [pam_dep], | ||
name_prefix : '', | ||
link_with : [libeinfo, librc], | ||
include_directories : [incdir, einfo_incdir, rc_incdir], | ||
install : true, | ||
install_dir : libdir / 'security') | ||
endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,231 @@ | ||
#include <librc.h> | ||
#include <pwd.h> | ||
#include <grp.h> | ||
#include <security/pam_modules.h> | ||
#include <stdbool.h> | ||
#include <stdio.h> | ||
#include <string.h> | ||
#include <sys/file.h> | ||
#include <syslog.h> | ||
#include <unistd.h> | ||
#include <utmpx.h> | ||
|
||
#include "einfo.h" | ||
#include "queue.h" | ||
|
||
static inline bool | ||
check_utmp(struct utmpx *utmpx, const char *user) | ||
{ | ||
return utmpx->ut_type == USER_PROCESS | ||
&& strncmp(utmpx->ut_user, user, sizeof(utmpx->ut_user)) == 0 | ||
&& kill(utmpx->ut_pid, 0) == 0; | ||
} | ||
|
||
static inline bool | ||
check_rundir(struct stat *sb, struct passwd *pw) | ||
{ | ||
return sb->st_uid == pw->pw_uid && sb->st_gid == pw->pw_gid; | ||
} | ||
|
||
static size_t | ||
get_logged_in_count(const char *user) | ||
{ | ||
struct utmpx *utmpx; | ||
size_t counter = 0; | ||
setutxent(); | ||
while ((utmpx = getutxent())) | ||
if (check_utmp(utmpx, user)) | ||
counter++; | ||
return counter; | ||
} | ||
|
||
static int | ||
exec_user_cmd(pam_handle_t *pamh, struct passwd *pw, char *cmd) | ||
{ | ||
int retval; | ||
const char *shellname = basename_c(pw->pw_shell); | ||
char **envron; | ||
|
||
elog(LOG_INFO, "Executing %s for user %s", cmd, pw->pw_name); | ||
|
||
switch (fork()) { | ||
case 0: | ||
initgroups(pw->pw_name, pw->pw_gid); | ||
setgid(pw->pw_gid); | ||
setuid(pw->pw_uid); | ||
|
||
envron = pam_getenvlist(pamh); | ||
execle(pw->pw_shell, shellname, "-c", cmd, NULL, envron); | ||
|
||
eerror("failed to exec \"%s, %s, -c, %s\": %s", | ||
pw->pw_shell, shellname, cmd, strerror(errno)); | ||
return -1; | ||
break; | ||
case -1: | ||
return -1; | ||
break; | ||
} | ||
wait(&retval); | ||
return retval; | ||
} | ||
|
||
static char * | ||
ensure_xdg_rundir(pam_handle_t *pamh, struct passwd *pw) | ||
{ | ||
char *rundir = xstrdup(pam_getenv(pamh, "XDG_RUNTIME_DIR")); | ||
char *tmp; | ||
struct stat sb; | ||
|
||
if (rundir) { | ||
if (stat(rundir, &sb) != 0 || !check_rundir(&sb, pw)) { | ||
free(rundir); | ||
return NULL; | ||
} | ||
return rundir; | ||
} | ||
|
||
if (mkdir("/run/user", 0755) != 0 && errno != EEXIST) | ||
return NULL; | ||
|
||
xasprintf(&rundir, "/run/user/%d", pw->pw_uid); | ||
|
||
if (stat(rundir, &sb) == 0 && !check_rundir(&sb, pw)) { | ||
free(rundir); | ||
return NULL; | ||
} | ||
|
||
elog(LOG_INFO, "Creating runtime directory %s for uid %d", rundir, pw->pw_uid); | ||
|
||
if ((mkdir(rundir, 0700) != 0 && errno != EEXIST) | ||
|| chown(rundir, pw->pw_uid, pw->pw_gid) != 0) { | ||
free(rundir); | ||
return NULL; | ||
} | ||
|
||
xasprintf(&tmp, "%s/.openrc-rundir", rundir); | ||
close(creat(tmp, 0500)); | ||
free(tmp); | ||
|
||
xasprintf(&tmp, "XDG_RUNTIME_DIR=%s", rundir); | ||
|
||
pam_putenv(pamh, tmp); | ||
free(tmp); | ||
|
||
return rundir; | ||
} | ||
|
||
static void | ||
clear_xdg_rundir(pam_handle_t *pamh, struct passwd *pw) | ||
{ | ||
char *rundir = xstrdup(pam_getenv(pamh, "XDG_RUNTIME_DIR")); | ||
char *file; | ||
if (!rundir) | ||
xasprintf(&rundir, "/run/user/%d", pw->pw_uid); | ||
|
||
xasprintf(&file, "%s/.openrc-rundir", rundir); | ||
if (exists(file)) { | ||
elog(LOG_INFO, "Removing runtime directory %s for uid %d", rundir, pw->pw_uid); | ||
rm_dir(rundir, true); | ||
} | ||
|
||
free(file); | ||
free(rundir); | ||
} | ||
|
||
static bool | ||
exec_openrc(pam_handle_t *pamh, struct passwd *pw, const char *runlevel, bool going_down) | ||
{ | ||
char *cmd = NULL, *dir, *lock; | ||
bool ret = true; | ||
char *rundir = ensure_xdg_rundir(pamh, pw); | ||
int lock_fd; | ||
|
||
if (!rundir) | ||
return false; | ||
|
||
xasprintf(&lock, "%s/openrc.lock", rundir); | ||
lock_fd = creat(lock, 0600); | ||
free(lock); | ||
|
||
if (lock_fd == -1) | ||
return false; | ||
if (flock(lock_fd, LOCK_EX) != 0) { | ||
close(lock_fd); | ||
return false; | ||
} | ||
|
||
xasprintf(&dir, "%s/openrc", rundir); | ||
free(rundir); | ||
|
||
/* | ||
* execute the command if the user doesn't have any open sessions, | ||
* or has one but svcdir doesn't exist | ||
*/ | ||
if (get_logged_in_count(pw->pw_name) == 0 || (!going_down && !exists(dir))) { | ||
xasprintf(&cmd, "openrc --user %s", runlevel); | ||
if (exec_user_cmd(pamh, pw, cmd) == -1) | ||
ret = false; | ||
free(cmd); | ||
if (going_down) | ||
clear_xdg_rundir(pamh, pw); | ||
} | ||
|
||
free(dir); | ||
close(lock_fd); | ||
return ret; | ||
} | ||
|
||
static struct passwd * | ||
get_pw(pam_handle_t *pamh) | ||
{ | ||
const char *username; | ||
if (pam_get_user(pamh, &username, "username:") != PAM_SUCCESS) | ||
return NULL; | ||
return getpwnam(username); | ||
} | ||
|
||
PAM_EXTERN int pam_sm_open_session(pam_handle_t *pamh, int flags, int argc, const char **argv) { | ||
const char *runlevel = argc > 0 ? runlevel = argv[0] : "default"; | ||
struct passwd *pw; | ||
int ret = PAM_SUCCESS; | ||
(void) flags; | ||
|
||
pw = get_pw(pamh); | ||
if (!pw) | ||
return PAM_SESSION_ERR; | ||
|
||
if (pw->pw_uid == 0) | ||
return PAM_SUCCESS; | ||
|
||
setenv("EINFO_LOG", "openrc-pam", 1); | ||
elog(LOG_INFO, "Opening openrc session for user %s", pw->pw_name); | ||
|
||
if (!exec_openrc(pamh, pw, runlevel, false)) | ||
ret = PAM_SESSION_ERR; | ||
|
||
unsetenv("EINFO_LOG"); | ||
return ret; | ||
} | ||
|
||
PAM_EXTERN int pam_sm_close_session(pam_handle_t *pamh, int flags, int argc, const char **argv) { | ||
const char *runlevel = argc > 1 ? argv[1] : "none"; | ||
struct passwd *pw; | ||
int ret = PAM_SUCCESS; | ||
(void) flags; | ||
|
||
pw = get_pw(pamh); | ||
if (!pw) | ||
return PAM_SESSION_ERR; | ||
|
||
if (pw->pw_uid == 0) | ||
return PAM_SUCCESS; | ||
|
||
setenv("EINFO_LOG", "openrc-pam", 1); | ||
elog(LOG_INFO, "Closing openrc session for user %s", pw->pw_name); | ||
|
||
if (!exec_openrc(pamh, pw, runlevel, true)) | ||
ret = PAM_SESSION_ERR; | ||
|
||
unsetenv("EINFO_LOG"); | ||
return ret; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters