]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
8c6db833 | 2 | |
00229fe4 | 3 | #include <endian.h> |
8c6db833 LP |
4 | #include <errno.h> |
5 | #include <fcntl.h> | |
8c6db833 | 6 | #include <pwd.h> |
8c6db833 | 7 | #include <security/_pam_macros.h> |
8c6db833 LP |
8 | #include <security/pam_ext.h> |
9 | #include <security/pam_misc.h> | |
00229fe4 LP |
10 | #include <security/pam_modules.h> |
11 | #include <security/pam_modutil.h> | |
12 | #include <sys/file.h> | |
8c6db833 | 13 | |
b5efdb8a | 14 | #include "alloc-util.h" |
430f0182 | 15 | #include "audit-util.h" |
00229fe4 LP |
16 | #include "bus-common-errors.h" |
17 | #include "bus-error.h" | |
ffcfcb6b | 18 | #include "bus-util.h" |
e37e5ed3 | 19 | #include "cgroup-util.h" |
98a28fef | 20 | #include "def.h" |
3ffd4af2 | 21 | #include "fd-util.h" |
a5c32cff | 22 | #include "fileio.h" |
f97b34a6 | 23 | #include "format-util.h" |
958b66ea | 24 | #include "hostname-util.h" |
00229fe4 LP |
25 | #include "login-util.h" |
26 | #include "macro.h" | |
6bedfcbb | 27 | #include "parse-util.h" |
e37e5ed3 | 28 | #include "path-util.h" |
df0ff127 | 29 | #include "process-util.h" |
00229fe4 LP |
30 | #include "socket-util.h" |
31 | #include "strv.h" | |
32 | #include "terminal-util.h" | |
33 | #include "util.h" | |
8c6db833 | 34 | |
baae0358 LP |
35 | static int parse_argv( |
36 | pam_handle_t *handle, | |
37 | int argc, const char **argv, | |
38 | const char **class, | |
49ebd11f | 39 | const char **type, |
f5cb2820 | 40 | const char **desktop, |
baae0358 | 41 | bool *debug) { |
8c6db833 LP |
42 | |
43 | unsigned i; | |
44 | ||
45 | assert(argc >= 0); | |
46 | assert(argc == 0 || argv); | |
47 | ||
49ebd11f | 48 | for (i = 0; i < (unsigned) argc; i++) { |
fb6becb4 | 49 | if (startswith(argv[i], "class=")) { |
485507b8 MM |
50 | if (class) |
51 | *class = argv[i] + 6; | |
52 | ||
49ebd11f LP |
53 | } else if (startswith(argv[i], "type=")) { |
54 | if (type) | |
55 | *type = argv[i] + 5; | |
56 | ||
f5cb2820 LP |
57 | } else if (startswith(argv[i], "desktop=")) { |
58 | if (desktop) | |
59 | *desktop = argv[i] + 8; | |
60 | ||
05a049cc ZJS |
61 | } else if (streq(argv[i], "debug")) { |
62 | if (debug) | |
63 | *debug = true; | |
fb6becb4 | 64 | |
05a049cc ZJS |
65 | } else if (startswith(argv[i], "debug=")) { |
66 | int k; | |
0e318cad | 67 | |
05a049cc ZJS |
68 | k = parse_boolean(argv[i] + 6); |
69 | if (k < 0) | |
70 | pam_syslog(handle, LOG_WARNING, "Failed to parse debug= argument, ignoring."); | |
71 | else if (debug) | |
0e318cad MS |
72 | *debug = k; |
73 | ||
05a049cc | 74 | } else |
fb6becb4 | 75 | pam_syslog(handle, LOG_WARNING, "Unknown parameter '%s', ignoring", argv[i]); |
49ebd11f | 76 | } |
8c6db833 | 77 | |
8c6db833 LP |
78 | return 0; |
79 | } | |
80 | ||
8c6db833 LP |
81 | static int get_user_data( |
82 | pam_handle_t *handle, | |
83 | const char **ret_username, | |
84 | struct passwd **ret_pw) { | |
85 | ||
d90b9d27 LP |
86 | const char *username = NULL; |
87 | struct passwd *pw = NULL; | |
8c6db833 LP |
88 | int r; |
89 | ||
90 | assert(handle); | |
91 | assert(ret_username); | |
92 | assert(ret_pw); | |
93 | ||
baae0358 LP |
94 | r = pam_get_user(handle, &username, NULL); |
95 | if (r != PAM_SUCCESS) { | |
96 | pam_syslog(handle, LOG_ERR, "Failed to get user name."); | |
97 | return r; | |
98 | } | |
d90b9d27 | 99 | |
baae0358 LP |
100 | if (isempty(username)) { |
101 | pam_syslog(handle, LOG_ERR, "User name not valid."); | |
102 | return PAM_AUTH_ERR; | |
8c6db833 LP |
103 | } |
104 | ||
baae0358 | 105 | pw = pam_modutil_getpwnam(handle, username); |
d90b9d27 | 106 | if (!pw) { |
8c6db833 LP |
107 | pam_syslog(handle, LOG_ERR, "Failed to get user data."); |
108 | return PAM_USER_UNKNOWN; | |
109 | } | |
110 | ||
111 | *ret_pw = pw; | |
8e24a4f8 | 112 | *ret_username = username; |
8c6db833 LP |
113 | |
114 | return PAM_SUCCESS; | |
115 | } | |
116 | ||
4d6d6518 | 117 | static int get_seat_from_display(const char *display, const char **seat, uint32_t *vtnr) { |
b92bea5d ZJS |
118 | union sockaddr_union sa = { |
119 | .un.sun_family = AF_UNIX, | |
120 | }; | |
baae0358 LP |
121 | _cleanup_free_ char *p = NULL, *tty = NULL; |
122 | _cleanup_close_ int fd = -1; | |
4d6d6518 | 123 | struct ucred ucred; |
baae0358 | 124 | int v, r; |
4d6d6518 LP |
125 | |
126 | assert(display); | |
4d6d6518 LP |
127 | assert(vtnr); |
128 | ||
129 | /* We deduce the X11 socket from the display name, then use | |
130 | * SO_PEERCRED to determine the X11 server process, ask for | |
131 | * the controlling tty of that and if it's a VC then we know | |
132 | * the seat and the virtual terminal. Sounds ugly, is only | |
133 | * semi-ugly. */ | |
134 | ||
135 | r = socket_from_display(display, &p); | |
136 | if (r < 0) | |
137 | return r; | |
b92bea5d | 138 | strncpy(sa.un.sun_path, p, sizeof(sa.un.sun_path)-1); |
4d6d6518 LP |
139 | |
140 | fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0); | |
b92bea5d | 141 | if (fd < 0) |
4d6d6518 | 142 | return -errno; |
4d6d6518 | 143 | |
fc2fffe7 | 144 | if (connect(fd, &sa.sa, SOCKADDR_UN_LEN(sa.un)) < 0) |
4d6d6518 | 145 | return -errno; |
4d6d6518 | 146 | |
eff05270 | 147 | r = getpeercred(fd, &ucred); |
4d6d6518 | 148 | if (r < 0) |
eff05270 | 149 | return r; |
4d6d6518 LP |
150 | |
151 | r = get_ctty(ucred.pid, NULL, &tty); | |
152 | if (r < 0) | |
153 | return r; | |
154 | ||
155 | v = vtnr_from_tty(tty); | |
4d6d6518 LP |
156 | if (v < 0) |
157 | return v; | |
158 | else if (v == 0) | |
159 | return -ENOENT; | |
160 | ||
fc7985ed LP |
161 | if (seat) |
162 | *seat = "seat0"; | |
4d6d6518 LP |
163 | *vtnr = (uint32_t) v; |
164 | ||
165 | return 0; | |
166 | } | |
167 | ||
22f93314 JS |
168 | static int append_session_memory_max(pam_handle_t *handle, sd_bus_message *m, const char *limit) { |
169 | uint64_t val; | |
170 | int r; | |
171 | ||
172 | if (isempty(limit)) | |
173 | return 0; | |
174 | ||
175 | if (streq(limit, "infinity")) { | |
176 | r = sd_bus_message_append(m, "(sv)", "MemoryMax", "t", (uint64_t)-1); | |
177 | if (r < 0) { | |
178 | pam_syslog(handle, LOG_ERR, "Failed to append to bus message: %s", strerror(-r)); | |
179 | return r; | |
180 | } | |
181 | } else { | |
182 | r = parse_percent(limit); | |
183 | if (r >= 0) { | |
184 | r = sd_bus_message_append(m, "(sv)", "MemoryMaxScale", "u", (uint32_t) (((uint64_t) UINT32_MAX * r) / 100U)); | |
185 | if (r < 0) { | |
186 | pam_syslog(handle, LOG_ERR, "Failed to append to bus message: %s", strerror(-r)); | |
187 | return r; | |
188 | } | |
189 | } else { | |
190 | r = parse_size(limit, 1024, &val); | |
191 | if (r >= 0) { | |
192 | r = sd_bus_message_append(m, "(sv)", "MemoryMax", "t", val); | |
193 | if (r < 0) { | |
194 | pam_syslog(handle, LOG_ERR, "Failed to append to bus message: %s", strerror(-r)); | |
195 | return r; | |
196 | } | |
197 | } else | |
198 | pam_syslog(handle, LOG_WARNING, "Failed to parse systemd.limit: %s, ignoring.", limit); | |
199 | } | |
200 | } | |
201 | ||
202 | return 0; | |
203 | } | |
204 | ||
5fdfbbd5 | 205 | static int append_session_tasks_max(pam_handle_t *handle, sd_bus_message *m, const char *limit) { |
22f93314 JS |
206 | uint64_t val; |
207 | int r; | |
208 | ||
209 | /* No need to parse "infinity" here, it will be set unconditionally later in manager_start_scope() */ | |
210 | if (isempty(limit) || streq(limit, "infinity")) | |
211 | return 0; | |
212 | ||
213 | r = safe_atou64(limit, &val); | |
214 | if (r >= 0) { | |
215 | r = sd_bus_message_append(m, "(sv)", "TasksMax", "t", val); | |
216 | if (r < 0) { | |
217 | pam_syslog(handle, LOG_ERR, "Failed to append to bus message: %s", strerror(-r)); | |
218 | return r; | |
219 | } | |
220 | } else | |
221 | pam_syslog(handle, LOG_WARNING, "Failed to parse systemd.limit: %s, ignoring.", limit); | |
222 | ||
223 | return 0; | |
224 | } | |
225 | ||
226 | static int append_session_cg_weight(pam_handle_t *handle, sd_bus_message *m, const char *limit, const char *field) { | |
227 | uint64_t val; | |
228 | int r; | |
229 | ||
230 | if (!isempty(limit)) { | |
231 | r = cg_weight_parse(limit, &val); | |
232 | if (r >= 0) { | |
233 | r = sd_bus_message_append(m, "(sv)", field, "t", val); | |
234 | if (r < 0) { | |
235 | pam_syslog(handle, LOG_ERR, "Failed to append to bus message: %s", strerror(-r)); | |
236 | return r; | |
237 | } | |
238 | } else if (streq(field, "CPUWeight")) | |
239 | pam_syslog(handle, LOG_WARNING, "Failed to parse systemd.cpu_weight: %s, ignoring.", limit); | |
240 | else | |
241 | pam_syslog(handle, LOG_WARNING, "Failed to parse systemd.io_weight: %s, ignoring.", limit); | |
242 | } | |
243 | ||
244 | return 0; | |
245 | } | |
246 | ||
0ecc1c9d LP |
247 | static const char* getenv_harder(pam_handle_t *handle, const char *key, const char *fallback) { |
248 | const char *v; | |
249 | ||
250 | assert(handle); | |
251 | assert(key); | |
252 | ||
253 | /* Looks for an environment variable, preferrably in the environment block associated with the specified PAM | |
254 | * handle, falling back to the process' block instead. */ | |
255 | ||
256 | v = pam_getenv(handle, key); | |
257 | if (!isempty(v)) | |
258 | return v; | |
259 | ||
260 | v = getenv(key); | |
261 | if (!isempty(v)) | |
262 | return v; | |
263 | ||
264 | return fallback; | |
265 | } | |
266 | ||
d6baaa69 LP |
267 | static int update_environment(pam_handle_t *handle, const char *key, const char *value) { |
268 | int r; | |
269 | ||
270 | assert(handle); | |
271 | assert(key); | |
272 | ||
273 | /* Updates the environment, but only if there's actually a value set. Also, log about errors */ | |
274 | ||
275 | if (isempty(value)) | |
276 | return PAM_SUCCESS; | |
277 | ||
278 | r = pam_misc_setenv(handle, key, value, 0); | |
279 | if (r != PAM_SUCCESS) | |
280 | pam_syslog(handle, LOG_ERR, "Failed to set environment variable %s.", key); | |
281 | ||
282 | return r; | |
283 | } | |
284 | ||
98a28fef | 285 | _public_ PAM_EXTERN int pam_sm_open_session( |
8c6db833 LP |
286 | pam_handle_t *handle, |
287 | int flags, | |
288 | int argc, const char **argv) { | |
289 | ||
4afd3348 | 290 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
22f93314 | 291 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL; |
d1529c9e LP |
292 | const char |
293 | *username, *id, *object_path, *runtime_path, | |
294 | *service = NULL, | |
295 | *tty = NULL, *display = NULL, | |
296 | *remote_user = NULL, *remote_host = NULL, | |
297 | *seat = NULL, | |
298 | *type = NULL, *class = NULL, | |
f5cb2820 | 299 | *class_pam = NULL, *type_pam = NULL, *cvtnr = NULL, *desktop = NULL, *desktop_pam = NULL, |
22f93314 | 300 | *memory_max = NULL, *tasks_max = NULL, *cpu_weight = NULL, *io_weight = NULL; |
4afd3348 | 301 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
d1529c9e | 302 | int session_fd = -1, existing, r; |
d1529c9e LP |
303 | bool debug = false, remote; |
304 | struct passwd *pw; | |
baae0358 LP |
305 | uint32_t vtnr = 0; |
306 | uid_t original_uid; | |
8c6db833 | 307 | |
ffcfcb6b | 308 | assert(handle); |
98a28fef | 309 | |
79d860fe MP |
310 | /* Make this a NOP on non-logind systems */ |
311 | if (!logind_running()) | |
8c6db833 LP |
312 | return PAM_SUCCESS; |
313 | ||
e9fbc77c LP |
314 | if (parse_argv(handle, |
315 | argc, argv, | |
fb6becb4 | 316 | &class_pam, |
49ebd11f | 317 | &type_pam, |
f5cb2820 | 318 | &desktop_pam, |
5a330cda ZJS |
319 | &debug) < 0) |
320 | return PAM_SESSION_ERR; | |
74fe1fe3 | 321 | |
baae0358 | 322 | if (debug) |
3831838a | 323 | pam_syslog(handle, LOG_DEBUG, "pam-systemd initializing"); |
baae0358 | 324 | |
98a28fef | 325 | r = get_user_data(handle, &username, &pw); |
5a330cda ZJS |
326 | if (r != PAM_SUCCESS) { |
327 | pam_syslog(handle, LOG_ERR, "Failed to get user data."); | |
328 | return r; | |
329 | } | |
8c6db833 | 330 | |
30b2c336 LP |
331 | /* Make sure we don't enter a loop by talking to |
332 | * systemd-logind when it is actually waiting for the | |
333 | * background to finish start-up. If the service is | |
5c390a4a | 334 | * "systemd-user" we simply set XDG_RUNTIME_DIR and |
30b2c336 LP |
335 | * leave. */ |
336 | ||
337 | pam_get_item(handle, PAM_SERVICE, (const void**) &service); | |
5c390a4a | 338 | if (streq_ptr(service, "systemd-user")) { |
f9e4283d | 339 | _cleanup_free_ char *rt = NULL; |
30b2c336 | 340 | |
f9e4283d | 341 | if (asprintf(&rt, "/run/user/"UID_FMT, pw->pw_uid) < 0) |
ffcfcb6b | 342 | return PAM_BUF_ERR; |
30b2c336 | 343 | |
f9e4283d DH |
344 | r = pam_misc_setenv(handle, "XDG_RUNTIME_DIR", rt, 0); |
345 | if (r != PAM_SUCCESS) { | |
346 | pam_syslog(handle, LOG_ERR, "Failed to set runtime dir."); | |
347 | return r; | |
30b2c336 LP |
348 | } |
349 | ||
ffcfcb6b | 350 | return PAM_SUCCESS; |
8c6db833 LP |
351 | } |
352 | ||
ffcfcb6b | 353 | /* Otherwise, we ask logind to create a session for us */ |
8c6db833 | 354 | |
98a28fef LP |
355 | pam_get_item(handle, PAM_XDISPLAY, (const void**) &display); |
356 | pam_get_item(handle, PAM_TTY, (const void**) &tty); | |
357 | pam_get_item(handle, PAM_RUSER, (const void**) &remote_user); | |
358 | pam_get_item(handle, PAM_RHOST, (const void**) &remote_host); | |
a8573ccc | 359 | |
0ecc1c9d LP |
360 | seat = getenv_harder(handle, "XDG_SEAT", NULL); |
361 | cvtnr = getenv_harder(handle, "XDG_VTNR", NULL); | |
362 | type = getenv_harder(handle, "XDG_SESSION_TYPE", type_pam); | |
363 | class = getenv_harder(handle, "XDG_SESSION_CLASS", class_pam); | |
f5cb2820 | 364 | desktop = getenv_harder(handle, "XDG_SESSION_DESKTOP", desktop_pam); |
a4cd87e9 | 365 | |
ed18b08b | 366 | tty = strempty(tty); |
98a28fef | 367 | |
ee8545b0 | 368 | if (strchr(tty, ':')) { |
3a736949 LP |
369 | /* A tty with a colon is usually an X11 display, placed there to show up in utmp. We rearrange things |
370 | * and don't pretend that an X display was a tty. */ | |
ee8545b0 LP |
371 | if (isempty(display)) |
372 | display = tty; | |
a4cd87e9 | 373 | tty = NULL; |
3a736949 | 374 | |
1a4459d6 | 375 | } else if (streq(tty, "cron")) { |
3a736949 LP |
376 | /* cron is setting PAM_TTY to "cron" for some reason (the commit carries no information why, but |
377 | * probably because it wants to set it to something as pam_time/pam_access/… require PAM_TTY to be set | |
378 | * (as they otherwise even try to update it!) — but cron doesn't actually allocate a TTY for its forked | |
379 | * off processes.) */ | |
0ad1271f | 380 | type = "unspecified"; |
49ebd11f | 381 | class = "background"; |
a4cd87e9 | 382 | tty = NULL; |
3a736949 | 383 | |
0ad1271f | 384 | } else if (streq(tty, "ssh")) { |
3a736949 LP |
385 | /* ssh has been setting PAM_TTY to "ssh" (for the same reason as cron does this, see above. For further |
386 | * details look for "PAM_TTY_KLUDGE" in the openssh sources). */ | |
0ad1271f | 387 | type ="tty"; |
49ebd11f | 388 | class = "user"; |
3a736949 LP |
389 | tty = NULL; /* This one is particularly sad, as this means that ssh sessions — even though usually |
390 | * associated with a pty — won't be tracked by their tty in logind. This is because ssh | |
391 | * does the PAM session registration early for new connections, and registers a pty only | |
392 | * much later (this is because it doesn't know yet if it needs one at all, as whether to | |
393 | * register a pty or not is negotiated much later in the protocol). */ | |
394 | ||
c9ed61e7 LP |
395 | } else |
396 | /* Chop off leading /dev prefix that some clients specify, but others do not. */ | |
397 | tty = skip_dev_prefix(tty); | |
ee8545b0 | 398 | |
8e7705e5 | 399 | /* If this fails vtnr will be 0, that's intended */ |
4d6d6518 | 400 | if (!isempty(cvtnr)) |
2ae4842b | 401 | (void) safe_atou32(cvtnr, &vtnr); |
4d6d6518 | 402 | |
92bd5ff3 | 403 | if (!isempty(display) && !vtnr) { |
fc7985ed | 404 | if (isempty(seat)) |
6ef25fb6 | 405 | get_seat_from_display(display, &seat, &vtnr); |
fc7985ed | 406 | else if (streq(seat, "seat0")) |
6ef25fb6 | 407 | get_seat_from_display(display, NULL, &vtnr); |
fc7985ed | 408 | } |
4d6d6518 | 409 | |
49ebd11f | 410 | if (seat && !streq(seat, "seat0") && vtnr != 0) { |
1fa2f38f | 411 | pam_syslog(handle, LOG_DEBUG, "Ignoring vtnr %"PRIu32" for %s which is not seat0", vtnr, seat); |
d7353ef6 MM |
412 | vtnr = 0; |
413 | } | |
414 | ||
49ebd11f | 415 | if (isempty(type)) |
0ad1271f | 416 | type = !isempty(display) ? "x11" : |
49ebd11f | 417 | !isempty(tty) ? "tty" : "unspecified"; |
98a28fef | 418 | |
55efac6c | 419 | if (isempty(class)) |
e2acb67b | 420 | class = streq(type, "unspecified") ? "background" : "user"; |
55efac6c | 421 | |
fecc80c1 | 422 | remote = !isempty(remote_host) && !is_localhost(remote_host); |
98a28fef | 423 | |
22f93314 JS |
424 | (void) pam_get_data(handle, "systemd.memory_max", (const void **)&memory_max); |
425 | (void) pam_get_data(handle, "systemd.tasks_max", (const void **)&tasks_max); | |
426 | (void) pam_get_data(handle, "systemd.cpu_weight", (const void **)&cpu_weight); | |
427 | (void) pam_get_data(handle, "systemd.io_weight", (const void **)&io_weight); | |
428 | ||
4d49b48c | 429 | /* Talk to logind over the message bus */ |
5a330cda | 430 | |
ffcfcb6b ZJS |
431 | r = sd_bus_open_system(&bus); |
432 | if (r < 0) { | |
433 | pam_syslog(handle, LOG_ERR, "Failed to connect to system bus: %s", strerror(-r)); | |
434 | return PAM_SESSION_ERR; | |
cc377381 LP |
435 | } |
436 | ||
22f93314 | 437 | if (debug) { |
ce959314 | 438 | pam_syslog(handle, LOG_DEBUG, "Asking logind to create session: " |
1fa2f38f | 439 | "uid="UID_FMT" pid="PID_FMT" service=%s type=%s class=%s desktop=%s seat=%s vtnr=%"PRIu32" tty=%s display=%s remote=%s remote_user=%s remote_host=%s", |
df0ff127 | 440 | pw->pw_uid, getpid_cached(), |
baae0358 | 441 | strempty(service), |
cda7ecb0 | 442 | type, class, strempty(desktop), |
a4cd87e9 | 443 | strempty(seat), vtnr, strempty(tty), strempty(display), |
baae0358 | 444 | yes_no(remote), strempty(remote_user), strempty(remote_host)); |
22f93314 JS |
445 | pam_syslog(handle, LOG_DEBUG, "Session limits: " |
446 | "memory_max=%s tasks_max=%s cpu_weight=%s io_weight=%s", | |
447 | strna(memory_max), strna(tasks_max), strna(cpu_weight), strna(io_weight)); | |
448 | } | |
449 | ||
450 | r = sd_bus_message_new_method_call( | |
451 | bus, | |
452 | &m, | |
453 | "org.freedesktop.login1", | |
454 | "/org/freedesktop/login1", | |
455 | "org.freedesktop.login1.Manager", | |
456 | "CreateSession"); | |
457 | if (r < 0) { | |
458 | pam_syslog(handle, LOG_ERR, "Failed to create CreateSession method call: %s", strerror(-r)); | |
459 | return PAM_SESSION_ERR; | |
460 | } | |
461 | ||
462 | r = sd_bus_message_append(m, "uusssssussbss", | |
463 | (uint32_t) pw->pw_uid, | |
464 | (uint32_t) getpid_cached(), | |
465 | service, | |
466 | type, | |
467 | class, | |
468 | desktop, | |
469 | seat, | |
470 | vtnr, | |
471 | tty, | |
472 | display, | |
473 | remote, | |
474 | remote_user, | |
475 | remote_host); | |
476 | if (r < 0) { | |
477 | pam_syslog(handle, LOG_ERR, "Failed to append to bus message: %s", strerror(-r)); | |
478 | return PAM_SESSION_ERR; | |
479 | } | |
480 | ||
481 | r = sd_bus_message_open_container(m, 'a', "(sv)"); | |
482 | if (r < 0) { | |
483 | pam_syslog(handle, LOG_ERR, "Failed to open message container: %s", strerror(-r)); | |
484 | return PAM_SYSTEM_ERR; | |
485 | } | |
486 | ||
487 | r = append_session_memory_max(handle, m, memory_max); | |
488 | if (r < 0) | |
489 | return PAM_SESSION_ERR; | |
490 | ||
491 | r = append_session_tasks_max(handle, m, tasks_max); | |
492 | if (r < 0) | |
493 | return PAM_SESSION_ERR; | |
494 | ||
495 | r = append_session_cg_weight(handle, m, cpu_weight, "CPUWeight"); | |
496 | if (r < 0) | |
497 | return PAM_SESSION_ERR; | |
498 | ||
499 | r = append_session_cg_weight(handle, m, io_weight, "IOWeight"); | |
500 | if (r < 0) | |
501 | return PAM_SESSION_ERR; | |
502 | ||
503 | r = sd_bus_message_close_container(m); | |
504 | if (r < 0) { | |
505 | pam_syslog(handle, LOG_ERR, "Failed to close message container: %s", strerror(-r)); | |
506 | return PAM_SYSTEM_ERR; | |
507 | } | |
ce959314 | 508 | |
22f93314 | 509 | r = sd_bus_call(bus, m, 0, &error, &reply); |
ffcfcb6b | 510 | if (r < 0) { |
b80120c4 DH |
511 | if (sd_bus_error_has_name(&error, BUS_ERROR_SESSION_BUSY)) { |
512 | pam_syslog(handle, LOG_DEBUG, "Cannot create session: %s", bus_error_message(&error, r)); | |
513 | return PAM_SUCCESS; | |
514 | } else { | |
515 | pam_syslog(handle, LOG_ERR, "Failed to create session: %s", bus_error_message(&error, r)); | |
516 | return PAM_SYSTEM_ERR; | |
517 | } | |
98a28fef | 518 | } |
74fe1fe3 | 519 | |
ffcfcb6b | 520 | r = sd_bus_message_read(reply, |
baae0358 | 521 | "soshusub", |
ffcfcb6b ZJS |
522 | &id, |
523 | &object_path, | |
524 | &runtime_path, | |
525 | &session_fd, | |
baae0358 | 526 | &original_uid, |
ffcfcb6b ZJS |
527 | &seat, |
528 | &vtnr, | |
529 | &existing); | |
530 | if (r < 0) { | |
531 | pam_syslog(handle, LOG_ERR, "Failed to parse message: %s", strerror(-r)); | |
5a330cda | 532 | return PAM_SESSION_ERR; |
98a28fef | 533 | } |
74fe1fe3 | 534 | |
ce959314 MS |
535 | if (debug) |
536 | pam_syslog(handle, LOG_DEBUG, "Reply from logind: " | |
baae0358 LP |
537 | "id=%s object_path=%s runtime_path=%s session_fd=%d seat=%s vtnr=%u original_uid=%u", |
538 | id, object_path, runtime_path, session_fd, seat, vtnr, original_uid); | |
ce959314 | 539 | |
d6baaa69 LP |
540 | r = update_environment(handle, "XDG_SESSION_ID", id); |
541 | if (r != PAM_SUCCESS) | |
5a330cda | 542 | return r; |
8c6db833 | 543 | |
baae0358 LP |
544 | if (original_uid == pw->pw_uid) { |
545 | /* Don't set $XDG_RUNTIME_DIR if the user we now | |
546 | * authenticated for does not match the original user | |
547 | * of the session. We do this in order not to result | |
548 | * in privileged apps clobbering the runtime directory | |
549 | * unnecessarily. */ | |
550 | ||
d6baaa69 LP |
551 | r = update_environment(handle, "XDG_RUNTIME_DIR", runtime_path); |
552 | if (r != PAM_SUCCESS) | |
baae0358 | 553 | return r; |
98a28fef | 554 | } |
8c6db833 | 555 | |
b2f74f07 LP |
556 | /* Most likely we got the session/type/class from environment variables, but might have gotten the data |
557 | * somewhere else (for example PAM module parameters). Let's now update the environment variables, so that this | |
558 | * data is inherited into the session processes, and programs can rely on them to be initialized. */ | |
559 | ||
560 | r = update_environment(handle, "XDG_SESSION_TYPE", type); | |
561 | if (r != PAM_SUCCESS) | |
562 | return r; | |
563 | ||
564 | r = update_environment(handle, "XDG_SESSION_CLASS", class); | |
565 | if (r != PAM_SUCCESS) | |
566 | return r; | |
567 | ||
568 | r = update_environment(handle, "XDG_SESSION_DESKTOP", desktop); | |
569 | if (r != PAM_SUCCESS) | |
570 | return r; | |
571 | ||
d6baaa69 LP |
572 | r = update_environment(handle, "XDG_SEAT", seat); |
573 | if (r != PAM_SUCCESS) | |
574 | return r; | |
bbc73283 LP |
575 | |
576 | if (vtnr > 0) { | |
29d230f6 | 577 | char buf[DECIMAL_STR_MAX(vtnr)]; |
baae0358 | 578 | sprintf(buf, "%u", vtnr); |
bbc73283 | 579 | |
d6baaa69 LP |
580 | r = update_environment(handle, "XDG_VTNR", buf); |
581 | if (r != PAM_SUCCESS) | |
5a330cda | 582 | return r; |
bbc73283 LP |
583 | } |
584 | ||
77085881 LP |
585 | r = pam_set_data(handle, "systemd.existing", INT_TO_PTR(!!existing), NULL); |
586 | if (r != PAM_SUCCESS) { | |
587 | pam_syslog(handle, LOG_ERR, "Failed to install existing flag."); | |
5a330cda | 588 | return r; |
77085881 LP |
589 | } |
590 | ||
21c390cc | 591 | if (session_fd >= 0) { |
85c08dc0 | 592 | session_fd = fcntl(session_fd, F_DUPFD_CLOEXEC, 3); |
5a330cda ZJS |
593 | if (session_fd < 0) { |
594 | pam_syslog(handle, LOG_ERR, "Failed to dup session fd: %m"); | |
595 | return PAM_SESSION_ERR; | |
596 | } | |
597 | ||
23e096cc | 598 | r = pam_set_data(handle, "systemd.session-fd", FD_TO_PTR(session_fd), NULL); |
21c390cc LP |
599 | if (r != PAM_SUCCESS) { |
600 | pam_syslog(handle, LOG_ERR, "Failed to install session fd."); | |
03e334a1 | 601 | safe_close(session_fd); |
5a330cda | 602 | return r; |
21c390cc | 603 | } |
98a28fef | 604 | } |
8c6db833 | 605 | |
ffcfcb6b | 606 | return PAM_SUCCESS; |
98a28fef | 607 | } |
8c6db833 | 608 | |
98a28fef LP |
609 | _public_ PAM_EXTERN int pam_sm_close_session( |
610 | pam_handle_t *handle, | |
611 | int flags, | |
612 | int argc, const char **argv) { | |
8c6db833 | 613 | |
4afd3348 LP |
614 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
615 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; | |
5f41d1f1 | 616 | const void *existing = NULL; |
75c8e3cf | 617 | const char *id; |
75c8e3cf | 618 | int r; |
8c6db833 | 619 | |
ffcfcb6b | 620 | assert(handle); |
75c8e3cf | 621 | |
77085881 LP |
622 | /* Only release session if it wasn't pre-existing when we |
623 | * tried to create it */ | |
624 | pam_get_data(handle, "systemd.existing", &existing); | |
625 | ||
75c8e3cf | 626 | id = pam_getenv(handle, "XDG_SESSION_ID"); |
77085881 | 627 | if (id && !existing) { |
75c8e3cf LP |
628 | |
629 | /* Before we go and close the FIFO we need to tell | |
630 | * logind that this is a clean session shutdown, so | |
631 | * that it doesn't just go and slaughter us | |
632 | * immediately after closing the fd */ | |
633 | ||
ffcfcb6b ZJS |
634 | r = sd_bus_open_system(&bus); |
635 | if (r < 0) { | |
5f41d1f1 LP |
636 | pam_syslog(handle, LOG_ERR, "Failed to connect to system bus: %s", strerror(-r)); |
637 | return PAM_SESSION_ERR; | |
75c8e3cf LP |
638 | } |
639 | ||
ffcfcb6b ZJS |
640 | r = sd_bus_call_method(bus, |
641 | "org.freedesktop.login1", | |
642 | "/org/freedesktop/login1", | |
643 | "org.freedesktop.login1.Manager", | |
644 | "ReleaseSession", | |
645 | &error, | |
646 | NULL, | |
647 | "s", | |
648 | id); | |
649 | if (r < 0) { | |
5f41d1f1 LP |
650 | pam_syslog(handle, LOG_ERR, "Failed to release session: %s", bus_error_message(&error, r)); |
651 | return PAM_SESSION_ERR; | |
75c8e3cf LP |
652 | } |
653 | } | |
654 | ||
5f41d1f1 LP |
655 | /* Note that we are knowingly leaking the FIFO fd here. This |
656 | * way, logind can watch us die. If we closed it here it would | |
657 | * not have any clue when that is completed. Given that one | |
658 | * cannot really have multiple PAM sessions open from the same | |
659 | * process this means we will leak one FD at max. */ | |
75c8e3cf | 660 | |
5f41d1f1 | 661 | return PAM_SUCCESS; |
8c6db833 | 662 | } |