1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
#include <librc.h>
#include <pwd.h>
#include <security/pam_appl.h>
#include <security/pam_modules.h>
#include <stdbool.h>
#include <stdio.h>
#include <syslog.h>
#include <unistd.h>
#include "einfo.h"
static bool exec_openrc(pam_handle_t *pamh, const char *runlevel) {
char *cmd = NULL;
const char *username;
struct passwd *pw = NULL;
char **envlist;
char **env;
envlist = pam_getenvlist(pamh);
if (pam_get_user(pamh, &username, "username:") != PAM_SUCCESS)
return false;
pw = getpwnam(username);
if (!pw)
return false;
elog(LOG_INFO, "Executing %s runlevel for user %s", runlevel, username);
xasprintf(&cmd, "openrc --user %s", runlevel);
switch (fork()) {
case 0:
setgid(pw->pw_gid);
setuid(pw->pw_uid);
execle(pw->pw_shell, "-", "-c", cmd, NULL, envlist);
free(cmd);
return false;
break;
case -1:
free(cmd);
return false;
break;
}
wait(NULL);
for (env = envlist; *env; env++)
free(*env);
free(envlist);
free(cmd);
return true;
}
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";
(void)flags;
setenv("EINFO_LOG", "openrc-pam", 1);
elog(LOG_INFO, "Opening openrc session");
setenv("RC_PAM_STARTING", "YES", true);
if (exec_openrc(pamh, runlevel)) {
elog(LOG_INFO, "Openrc session opened");
unsetenv("RC_PAM_STARTING");
unsetenv("EINFO_LOG");
return PAM_SUCCESS;
} else {
elog(LOG_ERR, "Failed to open session");
unsetenv("RC_PAM_STARTING");
unsetenv("EINFO_LOG");
return PAM_SESSION_ERR;
}
}
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";
(void)flags;
setenv("EINFO_LOG", "openrc-pam", 1);
elog(LOG_INFO, "Closing openrc session");
setenv("RC_PAM_STOPPING", "YES", true);
if (exec_openrc(pamh, runlevel)) {
elog(LOG_INFO, "Openrc session closed");
unsetenv("RC_PAM_STOPPING");
unsetenv("EINFO_LOG");
return PAM_SUCCESS;
} else {
elog(LOG_ERR, "Failed to close session");
unsetenv("RC_PAM_STOPPING");
unsetenv("EINFO_LOG");
return PAM_SESSION_ERR;
}
}
|