aboutsummaryrefslogtreecommitdiff
path: root/src/openrc-pam/openrc-pam.c
blob: 40b9a6dcaaf7df5d777db6b1527dce5f30524e60 (plain)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include <librc.h>
#include <pwd.h>
#include <security/pam_appl.h>
#include <security/pam_ext.h>
#include <security/pam_modules.h>
#include <stdbool.h>
#include <stdio.h>
#include <syslog.h>
#include <unistd.h>
#include <string.h>

#include "einfo.h"
#include "queue.h"

static void load_envs_from_file(const char *path, RC_STRINGLIST *out) {
	FILE *fp = NULL;
	char *line = NULL;
	size_t n = 0;
	char *p = NULL;

	fp = fopen(path, "r");
	if (!fp) {
		return;
	}
	while (getline(&line, &n, fp) != -1) {
		if ((p = strchr(line, '\n'))) {
			*p = '\0';
		};
		rc_stringlist_addu(out, line);
	}
	fclose(fp);
}

static RC_STRINGLIST *load_dir(const char *dir) {
	RC_STRINGLIST *list = rc_stringlist_new();
	DIR *dp = NULL;
	struct dirent *d = NULL;
	char *path;

	if ((dp = opendir(dir)) != NULL) {
		while ((d = readdir(dp)) != NULL) {
			xasprintf(&path, "%s/%s", dir, d->d_name);
			load_envs_from_file(path, list);
		}
	}
	closedir(dp);

	return list;
}

static void set_user_env(pam_handle_t *pamh) {
	RC_STRINGLIST *allowed_env = NULL;
	RC_STRINGLIST *user_env = NULL;
	RC_STRING *env;
	RC_STRING *uenv;
	char *p;
	char *user_env_path;

	pam_syslog(pamh, LOG_INFO, "Loading allowed envs in %s", RC_USER_ENV_WHITELIST_D);
	allowed_env = load_dir(RC_USER_ENV_WHITELIST_D);

	pam_syslog(pamh, LOG_INFO, "Loading allowed envs in %s", RC_USER_ENV_WHITELIST);
	load_envs_from_file(RC_USER_ENV_WHITELIST, allowed_env);

	xasprintf(&user_env_path, "%s/openrc/env", pam_getenv(pamh, "XDG_RUNTIME_DIR"));

	pam_syslog(pamh, LOG_INFO, "Loading user envs in %s", user_env_path);
	user_env = load_dir(user_env_path);

	TAILQ_FOREACH(env, allowed_env, entries) {
		pam_syslog(pamh, LOG_INFO, "allowed env %s", env->value);
		TAILQ_FOREACH(uenv, user_env, entries) {
			p = strchr(uenv->value, '=');
			if (p) {
				*p = '\0';
				if (strcmp(env->value, uenv->value) == 0) {
					*p = '=';
					pam_syslog(pamh, LOG_INFO, "Exporting: %s", uenv->value);
					pam_putenv(pamh, uenv->value);
				} else {
					*p = '=';
				}
			}
		}
	}

	pam_syslog(pamh, LOG_INFO, "Finished loading user environment");

	rc_stringlist_free(allowed_env);
	rc_stringlist_free(user_env);
	free(user_env_path);
}

static int
exec_user_cmd(struct passwd *pw, char *cmd, char **envlist)
{
	int retval;
	const char *shellname = basename_c(pw->pw_shell);

	switch (fork()) {
		case 0:
			setgid(pw->pw_gid);
			setuid(pw->pw_uid);

			execle(pw->pw_shell, shellname, "-c", cmd, NULL, envlist);

			return -1;
			break;
		case -1:
			return -1;
			break;
	}
	wait(&retval);
	return retval;
}

static char *create_xdg_runtime_dir(struct passwd *pw) {
	char *path = NULL;
	char *openrc_path = NULL;

	if (mkdir("/run/user", 0755) != 0 && errno != EEXIST)
		return NULL;

	xasprintf(&path, "/run/user/%d/", pw->pw_uid);

	if (mkdir(path, 0700) != 0 && errno != EEXIST) {
		free(path);
		return NULL;
	}

	if (chown(path, pw->pw_uid, pw->pw_gid) != 0) {
		free(path);
		return NULL;
	}

	xasprintf(&openrc_path, "%s/%s", path, "openrc");

	if (mkdir(openrc_path, 0700) != 0 && errno != EEXIST) {
		free(openrc_path);
		free(path);
		return NULL;
	}

	if (chown(openrc_path, pw->pw_uid, pw->pw_gid) != 0) {
		free(openrc_path);
		free(path);
		return NULL;
	}

	return path;
}

static bool exec_openrc(pam_handle_t *pamh, const char *runlevel, bool lock) {
	char *cmd = NULL;
	const char *username;
	struct passwd *pw = NULL;
	char *xdg_runtime_dir;
	char *xdg_runtime_dir_env;
	char **envlist;
	char **env;

	if (pam_get_user(pamh, &username, "username:") != PAM_SUCCESS)
		return false;
	pw = getpwnam(username);
	if (!pw)
		return false;

	/* XDG_RUNTIME_DIR is needed in so many places, that if it's not defined
	 * we're defining it on the standard location. */
	if (pam_getenv(pamh, "XDG_RUNTIME_DIR") == NULL) {
		xdg_runtime_dir = create_xdg_runtime_dir(pw);
		if (!xdg_runtime_dir) {
			return false;
		}

		xasprintf(&xdg_runtime_dir_env, "XDG_RUNTIME_DIR=%s", xdg_runtime_dir);
		pam_putenv(pamh, xdg_runtime_dir_env);
		pam_syslog(pamh, LOG_INFO, "exporting: %s", xdg_runtime_dir_env);
		free(xdg_runtime_dir);
		free(xdg_runtime_dir_env);
	}

	envlist = pam_getenvlist(pamh);

	xasprintf(&cmd, "openrc --user %s %s", lock ? "--lock" : "--unlock", runlevel);
	pam_syslog(pamh, LOG_INFO, "Executing %s for user %s", cmd, username);
	exec_user_cmd(pw, cmd, envlist);

	set_user_env(pamh);

	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;

	pam_syslog(pamh, LOG_INFO, "Opening openrc session");

	if (exec_openrc(pamh, runlevel, true)) {
		pam_syslog(pamh, LOG_INFO, "Openrc session opened");
		return PAM_SUCCESS;
	} else {
		pam_syslog(pamh, LOG_ERR, "Failed to open session");
		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;

	pam_syslog(pamh, LOG_INFO, "Closing openrc session");

	if (exec_openrc(pamh, runlevel, false)) {
		pam_syslog(pamh, LOG_INFO, "Openrc session closed");
		return PAM_SUCCESS;
	} else {
		pam_syslog(pamh, LOG_ERR, "Failed to close session");
		return PAM_SESSION_ERR;
	}
}