]>
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 | ||
f7b8b5c4 LP |
117 | static int socket_from_display(const char *display, char **path) { |
118 | size_t k; | |
119 | char *f, *c; | |
120 | ||
121 | assert(display); | |
122 | assert(path); | |
123 | ||
124 | if (!display_is_local(display)) | |
125 | return -EINVAL; | |
126 | ||
127 | k = strspn(display+1, "0123456789"); | |
128 | ||
129 | f = new(char, STRLEN("/tmp/.X11-unix/X") + k + 1); | |
130 | if (!f) | |
131 | return -ENOMEM; | |
132 | ||
133 | c = stpcpy(f, "/tmp/.X11-unix/X"); | |
134 | memcpy(c, display+1, k); | |
135 | c[k] = 0; | |
136 | ||
137 | *path = f; | |
138 | ||
139 | return 0; | |
140 | } | |
141 | ||
4d6d6518 | 142 | static int get_seat_from_display(const char *display, const char **seat, uint32_t *vtnr) { |
15a3e96f | 143 | union sockaddr_union sa = {}; |
baae0358 LP |
144 | _cleanup_free_ char *p = NULL, *tty = NULL; |
145 | _cleanup_close_ int fd = -1; | |
4d6d6518 | 146 | struct ucred ucred; |
15a3e96f | 147 | int v, r, salen; |
4d6d6518 LP |
148 | |
149 | assert(display); | |
4d6d6518 LP |
150 | assert(vtnr); |
151 | ||
152 | /* We deduce the X11 socket from the display name, then use | |
153 | * SO_PEERCRED to determine the X11 server process, ask for | |
154 | * the controlling tty of that and if it's a VC then we know | |
155 | * the seat and the virtual terminal. Sounds ugly, is only | |
156 | * semi-ugly. */ | |
157 | ||
158 | r = socket_from_display(display, &p); | |
159 | if (r < 0) | |
160 | return r; | |
15a3e96f LP |
161 | salen = sockaddr_un_set_path(&sa.un, p); |
162 | if (salen < 0) | |
163 | return salen; | |
4d6d6518 LP |
164 | |
165 | fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0); | |
b92bea5d | 166 | if (fd < 0) |
4d6d6518 | 167 | return -errno; |
4d6d6518 | 168 | |
15a3e96f | 169 | if (connect(fd, &sa.sa, salen) < 0) |
4d6d6518 | 170 | return -errno; |
4d6d6518 | 171 | |
eff05270 | 172 | r = getpeercred(fd, &ucred); |
4d6d6518 | 173 | if (r < 0) |
eff05270 | 174 | return r; |
4d6d6518 LP |
175 | |
176 | r = get_ctty(ucred.pid, NULL, &tty); | |
177 | if (r < 0) | |
178 | return r; | |
179 | ||
180 | v = vtnr_from_tty(tty); | |
4d6d6518 LP |
181 | if (v < 0) |
182 | return v; | |
183 | else if (v == 0) | |
184 | return -ENOENT; | |
185 | ||
fc7985ed LP |
186 | if (seat) |
187 | *seat = "seat0"; | |
4d6d6518 LP |
188 | *vtnr = (uint32_t) v; |
189 | ||
190 | return 0; | |
191 | } | |
192 | ||
00efd498 ZJS |
193 | static int export_legacy_dbus_address( |
194 | pam_handle_t *handle, | |
195 | uid_t uid, | |
196 | const char *runtime) { | |
197 | ||
198 | _cleanup_free_ char *s = NULL; | |
199 | int r = PAM_BUF_ERR; | |
200 | ||
201 | /* FIXME: We *really* should move the access() check into the | |
202 | * daemons that spawn dbus-daemon, instead of forcing | |
203 | * DBUS_SESSION_BUS_ADDRESS= here. */ | |
204 | ||
205 | s = strjoin(runtime, "/bus"); | |
206 | if (!s) | |
207 | goto error; | |
208 | ||
209 | if (access(s, F_OK) < 0) | |
210 | return PAM_SUCCESS; | |
211 | ||
212 | s = mfree(s); | |
213 | if (asprintf(&s, DEFAULT_USER_BUS_ADDRESS_FMT, runtime) < 0) | |
214 | goto error; | |
215 | ||
216 | r = pam_misc_setenv(handle, "DBUS_SESSION_BUS_ADDRESS", s, 0); | |
217 | if (r != PAM_SUCCESS) | |
218 | goto error; | |
219 | ||
220 | return PAM_SUCCESS; | |
221 | ||
222 | error: | |
223 | pam_syslog(handle, LOG_ERR, "Failed to set bus variable."); | |
224 | return r; | |
225 | } | |
226 | ||
22f93314 JS |
227 | static int append_session_memory_max(pam_handle_t *handle, sd_bus_message *m, const char *limit) { |
228 | uint64_t val; | |
229 | int r; | |
230 | ||
231 | if (isempty(limit)) | |
232 | return 0; | |
233 | ||
234 | if (streq(limit, "infinity")) { | |
235 | r = sd_bus_message_append(m, "(sv)", "MemoryMax", "t", (uint64_t)-1); | |
236 | if (r < 0) { | |
237 | pam_syslog(handle, LOG_ERR, "Failed to append to bus message: %s", strerror(-r)); | |
238 | return r; | |
239 | } | |
240 | } else { | |
f806dfd3 | 241 | r = parse_permille(limit); |
22f93314 | 242 | if (r >= 0) { |
f806dfd3 | 243 | r = sd_bus_message_append(m, "(sv)", "MemoryMaxScale", "u", (uint32_t) (((uint64_t) r * UINT32_MAX) / 1000U)); |
22f93314 JS |
244 | if (r < 0) { |
245 | pam_syslog(handle, LOG_ERR, "Failed to append to bus message: %s", strerror(-r)); | |
246 | return r; | |
247 | } | |
248 | } else { | |
249 | r = parse_size(limit, 1024, &val); | |
250 | if (r >= 0) { | |
251 | r = sd_bus_message_append(m, "(sv)", "MemoryMax", "t", val); | |
252 | if (r < 0) { | |
253 | pam_syslog(handle, LOG_ERR, "Failed to append to bus message: %s", strerror(-r)); | |
254 | return r; | |
255 | } | |
256 | } else | |
257 | pam_syslog(handle, LOG_WARNING, "Failed to parse systemd.limit: %s, ignoring.", limit); | |
258 | } | |
259 | } | |
260 | ||
261 | return 0; | |
262 | } | |
263 | ||
5fdfbbd5 | 264 | static int append_session_tasks_max(pam_handle_t *handle, sd_bus_message *m, const char *limit) { |
22f93314 JS |
265 | uint64_t val; |
266 | int r; | |
267 | ||
268 | /* No need to parse "infinity" here, it will be set unconditionally later in manager_start_scope() */ | |
269 | if (isempty(limit) || streq(limit, "infinity")) | |
270 | return 0; | |
271 | ||
272 | r = safe_atou64(limit, &val); | |
273 | if (r >= 0) { | |
274 | r = sd_bus_message_append(m, "(sv)", "TasksMax", "t", val); | |
275 | if (r < 0) { | |
276 | pam_syslog(handle, LOG_ERR, "Failed to append to bus message: %s", strerror(-r)); | |
277 | return r; | |
278 | } | |
279 | } else | |
280 | pam_syslog(handle, LOG_WARNING, "Failed to parse systemd.limit: %s, ignoring.", limit); | |
281 | ||
282 | return 0; | |
283 | } | |
284 | ||
285 | static int append_session_cg_weight(pam_handle_t *handle, sd_bus_message *m, const char *limit, const char *field) { | |
286 | uint64_t val; | |
287 | int r; | |
288 | ||
36a4dbae LP |
289 | if (isempty(limit)) |
290 | return 0; | |
291 | ||
292 | r = cg_weight_parse(limit, &val); | |
293 | if (r >= 0) { | |
294 | r = sd_bus_message_append(m, "(sv)", field, "t", val); | |
295 | if (r < 0) { | |
296 | pam_syslog(handle, LOG_ERR, "Failed to append to bus message: %s", strerror(-r)); | |
297 | return r; | |
298 | } | |
299 | } else if (streq(field, "CPUWeight")) | |
300 | pam_syslog(handle, LOG_WARNING, "Failed to parse systemd.cpu_weight: %s, ignoring.", limit); | |
301 | else | |
302 | pam_syslog(handle, LOG_WARNING, "Failed to parse systemd.io_weight: %s, ignoring.", limit); | |
22f93314 JS |
303 | |
304 | return 0; | |
305 | } | |
306 | ||
0ecc1c9d LP |
307 | static const char* getenv_harder(pam_handle_t *handle, const char *key, const char *fallback) { |
308 | const char *v; | |
309 | ||
310 | assert(handle); | |
311 | assert(key); | |
312 | ||
313 | /* Looks for an environment variable, preferrably in the environment block associated with the specified PAM | |
314 | * handle, falling back to the process' block instead. */ | |
315 | ||
316 | v = pam_getenv(handle, key); | |
317 | if (!isempty(v)) | |
318 | return v; | |
319 | ||
320 | v = getenv(key); | |
321 | if (!isempty(v)) | |
322 | return v; | |
323 | ||
324 | return fallback; | |
325 | } | |
326 | ||
d6baaa69 LP |
327 | static int update_environment(pam_handle_t *handle, const char *key, const char *value) { |
328 | int r; | |
329 | ||
330 | assert(handle); | |
331 | assert(key); | |
332 | ||
333 | /* Updates the environment, but only if there's actually a value set. Also, log about errors */ | |
334 | ||
335 | if (isempty(value)) | |
336 | return PAM_SUCCESS; | |
337 | ||
338 | r = pam_misc_setenv(handle, key, value, 0); | |
339 | if (r != PAM_SUCCESS) | |
340 | pam_syslog(handle, LOG_ERR, "Failed to set environment variable %s.", key); | |
341 | ||
342 | return r; | |
343 | } | |
344 | ||
b9217112 LP |
345 | static bool validate_runtime_directory(pam_handle_t *handle, const char *path, uid_t uid) { |
346 | struct stat st; | |
347 | ||
348 | assert(path); | |
349 | ||
350 | /* Just some extra paranoia: let's not set $XDG_RUNTIME_DIR if the directory we'd set it to isn't actually set | |
351 | * up properly for us. */ | |
352 | ||
353 | if (lstat(path, &st) < 0) { | |
354 | pam_syslog(handle, LOG_ERR, "Failed to stat() runtime directory '%s': %s", path, strerror(errno)); | |
355 | goto fail; | |
356 | } | |
357 | ||
358 | if (!S_ISDIR(st.st_mode)) { | |
359 | pam_syslog(handle, LOG_ERR, "Runtime directory '%s' is not actually a directory.", path); | |
360 | goto fail; | |
361 | } | |
362 | ||
363 | if (st.st_uid != uid) { | |
364 | pam_syslog(handle, LOG_ERR, "Runtime directory '%s' is not owned by UID " UID_FMT ", as it should.", path, uid); | |
365 | goto fail; | |
366 | } | |
367 | ||
368 | return true; | |
369 | ||
370 | fail: | |
371 | pam_syslog(handle, LOG_WARNING, "Not setting $XDG_RUNTIME_DIR, as the directory is not in order."); | |
372 | return false; | |
373 | } | |
374 | ||
98a28fef | 375 | _public_ PAM_EXTERN int pam_sm_open_session( |
8c6db833 LP |
376 | pam_handle_t *handle, |
377 | int flags, | |
378 | int argc, const char **argv) { | |
379 | ||
4afd3348 | 380 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
22f93314 | 381 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL; |
d1529c9e LP |
382 | const char |
383 | *username, *id, *object_path, *runtime_path, | |
384 | *service = NULL, | |
385 | *tty = NULL, *display = NULL, | |
386 | *remote_user = NULL, *remote_host = NULL, | |
387 | *seat = NULL, | |
388 | *type = NULL, *class = NULL, | |
f5cb2820 | 389 | *class_pam = NULL, *type_pam = NULL, *cvtnr = NULL, *desktop = NULL, *desktop_pam = NULL, |
22f93314 | 390 | *memory_max = NULL, *tasks_max = NULL, *cpu_weight = NULL, *io_weight = NULL; |
4afd3348 | 391 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
d1529c9e | 392 | int session_fd = -1, existing, r; |
d1529c9e LP |
393 | bool debug = false, remote; |
394 | struct passwd *pw; | |
baae0358 LP |
395 | uint32_t vtnr = 0; |
396 | uid_t original_uid; | |
8c6db833 | 397 | |
ffcfcb6b | 398 | assert(handle); |
98a28fef | 399 | |
79d860fe MP |
400 | /* Make this a NOP on non-logind systems */ |
401 | if (!logind_running()) | |
8c6db833 LP |
402 | return PAM_SUCCESS; |
403 | ||
e9fbc77c LP |
404 | if (parse_argv(handle, |
405 | argc, argv, | |
fb6becb4 | 406 | &class_pam, |
49ebd11f | 407 | &type_pam, |
f5cb2820 | 408 | &desktop_pam, |
5a330cda ZJS |
409 | &debug) < 0) |
410 | return PAM_SESSION_ERR; | |
74fe1fe3 | 411 | |
baae0358 | 412 | if (debug) |
3831838a | 413 | pam_syslog(handle, LOG_DEBUG, "pam-systemd initializing"); |
baae0358 | 414 | |
98a28fef | 415 | r = get_user_data(handle, &username, &pw); |
5a330cda ZJS |
416 | if (r != PAM_SUCCESS) { |
417 | pam_syslog(handle, LOG_ERR, "Failed to get user data."); | |
418 | return r; | |
419 | } | |
8c6db833 | 420 | |
30b2c336 LP |
421 | /* Make sure we don't enter a loop by talking to |
422 | * systemd-logind when it is actually waiting for the | |
423 | * background to finish start-up. If the service is | |
5c390a4a | 424 | * "systemd-user" we simply set XDG_RUNTIME_DIR and |
30b2c336 LP |
425 | * leave. */ |
426 | ||
427 | pam_get_item(handle, PAM_SERVICE, (const void**) &service); | |
5c390a4a | 428 | if (streq_ptr(service, "systemd-user")) { |
f9e4283d | 429 | _cleanup_free_ char *rt = NULL; |
30b2c336 | 430 | |
f9e4283d | 431 | if (asprintf(&rt, "/run/user/"UID_FMT, pw->pw_uid) < 0) |
ffcfcb6b | 432 | return PAM_BUF_ERR; |
30b2c336 | 433 | |
b9217112 LP |
434 | if (validate_runtime_directory(handle, rt, pw->pw_uid)) { |
435 | r = pam_misc_setenv(handle, "XDG_RUNTIME_DIR", rt, 0); | |
436 | if (r != PAM_SUCCESS) { | |
437 | pam_syslog(handle, LOG_ERR, "Failed to set runtime dir."); | |
438 | return r; | |
439 | } | |
30b2c336 LP |
440 | } |
441 | ||
00efd498 ZJS |
442 | r = export_legacy_dbus_address(handle, pw->pw_uid, rt); |
443 | if (r != PAM_SUCCESS) | |
444 | return r; | |
445 | ||
ffcfcb6b | 446 | return PAM_SUCCESS; |
8c6db833 LP |
447 | } |
448 | ||
ffcfcb6b | 449 | /* Otherwise, we ask logind to create a session for us */ |
8c6db833 | 450 | |
98a28fef LP |
451 | pam_get_item(handle, PAM_XDISPLAY, (const void**) &display); |
452 | pam_get_item(handle, PAM_TTY, (const void**) &tty); | |
453 | pam_get_item(handle, PAM_RUSER, (const void**) &remote_user); | |
454 | pam_get_item(handle, PAM_RHOST, (const void**) &remote_host); | |
a8573ccc | 455 | |
0ecc1c9d LP |
456 | seat = getenv_harder(handle, "XDG_SEAT", NULL); |
457 | cvtnr = getenv_harder(handle, "XDG_VTNR", NULL); | |
458 | type = getenv_harder(handle, "XDG_SESSION_TYPE", type_pam); | |
459 | class = getenv_harder(handle, "XDG_SESSION_CLASS", class_pam); | |
f5cb2820 | 460 | desktop = getenv_harder(handle, "XDG_SESSION_DESKTOP", desktop_pam); |
a4cd87e9 | 461 | |
ed18b08b | 462 | tty = strempty(tty); |
98a28fef | 463 | |
ee8545b0 | 464 | if (strchr(tty, ':')) { |
3a736949 LP |
465 | /* A tty with a colon is usually an X11 display, placed there to show up in utmp. We rearrange things |
466 | * and don't pretend that an X display was a tty. */ | |
ee8545b0 LP |
467 | if (isempty(display)) |
468 | display = tty; | |
a4cd87e9 | 469 | tty = NULL; |
3a736949 | 470 | |
1a4459d6 | 471 | } else if (streq(tty, "cron")) { |
3a736949 LP |
472 | /* cron is setting PAM_TTY to "cron" for some reason (the commit carries no information why, but |
473 | * probably because it wants to set it to something as pam_time/pam_access/… require PAM_TTY to be set | |
474 | * (as they otherwise even try to update it!) — but cron doesn't actually allocate a TTY for its forked | |
475 | * off processes.) */ | |
0ad1271f | 476 | type = "unspecified"; |
49ebd11f | 477 | class = "background"; |
a4cd87e9 | 478 | tty = NULL; |
3a736949 | 479 | |
0ad1271f | 480 | } else if (streq(tty, "ssh")) { |
3a736949 LP |
481 | /* ssh has been setting PAM_TTY to "ssh" (for the same reason as cron does this, see above. For further |
482 | * details look for "PAM_TTY_KLUDGE" in the openssh sources). */ | |
0ad1271f | 483 | type ="tty"; |
49ebd11f | 484 | class = "user"; |
3a736949 LP |
485 | tty = NULL; /* This one is particularly sad, as this means that ssh sessions — even though usually |
486 | * associated with a pty — won't be tracked by their tty in logind. This is because ssh | |
487 | * does the PAM session registration early for new connections, and registers a pty only | |
488 | * much later (this is because it doesn't know yet if it needs one at all, as whether to | |
489 | * register a pty or not is negotiated much later in the protocol). */ | |
490 | ||
c9ed61e7 LP |
491 | } else |
492 | /* Chop off leading /dev prefix that some clients specify, but others do not. */ | |
493 | tty = skip_dev_prefix(tty); | |
ee8545b0 | 494 | |
8e7705e5 | 495 | /* If this fails vtnr will be 0, that's intended */ |
4d6d6518 | 496 | if (!isempty(cvtnr)) |
2ae4842b | 497 | (void) safe_atou32(cvtnr, &vtnr); |
4d6d6518 | 498 | |
92bd5ff3 | 499 | if (!isempty(display) && !vtnr) { |
fc7985ed | 500 | if (isempty(seat)) |
d487e2d6 | 501 | (void) get_seat_from_display(display, &seat, &vtnr); |
fc7985ed | 502 | else if (streq(seat, "seat0")) |
d487e2d6 | 503 | (void) get_seat_from_display(display, NULL, &vtnr); |
fc7985ed | 504 | } |
4d6d6518 | 505 | |
49ebd11f | 506 | if (seat && !streq(seat, "seat0") && vtnr != 0) { |
2675747f LP |
507 | if (debug) |
508 | pam_syslog(handle, LOG_DEBUG, "Ignoring vtnr %"PRIu32" for %s which is not seat0", vtnr, seat); | |
d7353ef6 MM |
509 | vtnr = 0; |
510 | } | |
511 | ||
49ebd11f | 512 | if (isempty(type)) |
0ad1271f | 513 | type = !isempty(display) ? "x11" : |
49ebd11f | 514 | !isempty(tty) ? "tty" : "unspecified"; |
98a28fef | 515 | |
55efac6c | 516 | if (isempty(class)) |
e2acb67b | 517 | class = streq(type, "unspecified") ? "background" : "user"; |
55efac6c | 518 | |
fecc80c1 | 519 | remote = !isempty(remote_host) && !is_localhost(remote_host); |
98a28fef | 520 | |
22f93314 JS |
521 | (void) pam_get_data(handle, "systemd.memory_max", (const void **)&memory_max); |
522 | (void) pam_get_data(handle, "systemd.tasks_max", (const void **)&tasks_max); | |
523 | (void) pam_get_data(handle, "systemd.cpu_weight", (const void **)&cpu_weight); | |
524 | (void) pam_get_data(handle, "systemd.io_weight", (const void **)&io_weight); | |
525 | ||
4d49b48c | 526 | /* Talk to logind over the message bus */ |
5a330cda | 527 | |
ffcfcb6b ZJS |
528 | r = sd_bus_open_system(&bus); |
529 | if (r < 0) { | |
530 | pam_syslog(handle, LOG_ERR, "Failed to connect to system bus: %s", strerror(-r)); | |
531 | return PAM_SESSION_ERR; | |
cc377381 LP |
532 | } |
533 | ||
22f93314 | 534 | if (debug) { |
ce959314 | 535 | pam_syslog(handle, LOG_DEBUG, "Asking logind to create session: " |
1fa2f38f | 536 | "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 | 537 | pw->pw_uid, getpid_cached(), |
baae0358 | 538 | strempty(service), |
cda7ecb0 | 539 | type, class, strempty(desktop), |
a4cd87e9 | 540 | strempty(seat), vtnr, strempty(tty), strempty(display), |
baae0358 | 541 | yes_no(remote), strempty(remote_user), strempty(remote_host)); |
22f93314 JS |
542 | pam_syslog(handle, LOG_DEBUG, "Session limits: " |
543 | "memory_max=%s tasks_max=%s cpu_weight=%s io_weight=%s", | |
544 | strna(memory_max), strna(tasks_max), strna(cpu_weight), strna(io_weight)); | |
545 | } | |
546 | ||
547 | r = sd_bus_message_new_method_call( | |
548 | bus, | |
549 | &m, | |
550 | "org.freedesktop.login1", | |
551 | "/org/freedesktop/login1", | |
552 | "org.freedesktop.login1.Manager", | |
553 | "CreateSession"); | |
554 | if (r < 0) { | |
555 | pam_syslog(handle, LOG_ERR, "Failed to create CreateSession method call: %s", strerror(-r)); | |
556 | return PAM_SESSION_ERR; | |
557 | } | |
558 | ||
559 | r = sd_bus_message_append(m, "uusssssussbss", | |
560 | (uint32_t) pw->pw_uid, | |
da0da5ec | 561 | 0, |
22f93314 JS |
562 | service, |
563 | type, | |
564 | class, | |
565 | desktop, | |
566 | seat, | |
567 | vtnr, | |
568 | tty, | |
569 | display, | |
570 | remote, | |
571 | remote_user, | |
572 | remote_host); | |
573 | if (r < 0) { | |
574 | pam_syslog(handle, LOG_ERR, "Failed to append to bus message: %s", strerror(-r)); | |
575 | return PAM_SESSION_ERR; | |
576 | } | |
577 | ||
578 | r = sd_bus_message_open_container(m, 'a', "(sv)"); | |
579 | if (r < 0) { | |
580 | pam_syslog(handle, LOG_ERR, "Failed to open message container: %s", strerror(-r)); | |
581 | return PAM_SYSTEM_ERR; | |
582 | } | |
583 | ||
584 | r = append_session_memory_max(handle, m, memory_max); | |
585 | if (r < 0) | |
586 | return PAM_SESSION_ERR; | |
587 | ||
588 | r = append_session_tasks_max(handle, m, tasks_max); | |
589 | if (r < 0) | |
590 | return PAM_SESSION_ERR; | |
591 | ||
592 | r = append_session_cg_weight(handle, m, cpu_weight, "CPUWeight"); | |
593 | if (r < 0) | |
594 | return PAM_SESSION_ERR; | |
595 | ||
596 | r = append_session_cg_weight(handle, m, io_weight, "IOWeight"); | |
597 | if (r < 0) | |
598 | return PAM_SESSION_ERR; | |
599 | ||
600 | r = sd_bus_message_close_container(m); | |
601 | if (r < 0) { | |
602 | pam_syslog(handle, LOG_ERR, "Failed to close message container: %s", strerror(-r)); | |
603 | return PAM_SYSTEM_ERR; | |
604 | } | |
ce959314 | 605 | |
22f93314 | 606 | r = sd_bus_call(bus, m, 0, &error, &reply); |
ffcfcb6b | 607 | if (r < 0) { |
b80120c4 | 608 | if (sd_bus_error_has_name(&error, BUS_ERROR_SESSION_BUSY)) { |
2675747f LP |
609 | if (debug) |
610 | pam_syslog(handle, LOG_DEBUG, "Cannot create session: %s", bus_error_message(&error, r)); | |
b80120c4 DH |
611 | return PAM_SUCCESS; |
612 | } else { | |
613 | pam_syslog(handle, LOG_ERR, "Failed to create session: %s", bus_error_message(&error, r)); | |
614 | return PAM_SYSTEM_ERR; | |
615 | } | |
98a28fef | 616 | } |
74fe1fe3 | 617 | |
ffcfcb6b | 618 | r = sd_bus_message_read(reply, |
baae0358 | 619 | "soshusub", |
ffcfcb6b ZJS |
620 | &id, |
621 | &object_path, | |
622 | &runtime_path, | |
623 | &session_fd, | |
baae0358 | 624 | &original_uid, |
ffcfcb6b ZJS |
625 | &seat, |
626 | &vtnr, | |
627 | &existing); | |
628 | if (r < 0) { | |
629 | pam_syslog(handle, LOG_ERR, "Failed to parse message: %s", strerror(-r)); | |
5a330cda | 630 | return PAM_SESSION_ERR; |
98a28fef | 631 | } |
74fe1fe3 | 632 | |
ce959314 MS |
633 | if (debug) |
634 | pam_syslog(handle, LOG_DEBUG, "Reply from logind: " | |
baae0358 LP |
635 | "id=%s object_path=%s runtime_path=%s session_fd=%d seat=%s vtnr=%u original_uid=%u", |
636 | id, object_path, runtime_path, session_fd, seat, vtnr, original_uid); | |
ce959314 | 637 | |
d6baaa69 LP |
638 | r = update_environment(handle, "XDG_SESSION_ID", id); |
639 | if (r != PAM_SUCCESS) | |
5a330cda | 640 | return r; |
8c6db833 | 641 | |
baae0358 LP |
642 | if (original_uid == pw->pw_uid) { |
643 | /* Don't set $XDG_RUNTIME_DIR if the user we now | |
644 | * authenticated for does not match the original user | |
645 | * of the session. We do this in order not to result | |
646 | * in privileged apps clobbering the runtime directory | |
647 | * unnecessarily. */ | |
648 | ||
b9217112 LP |
649 | if (validate_runtime_directory(handle, runtime_path, pw->pw_uid)) { |
650 | r = update_environment(handle, "XDG_RUNTIME_DIR", runtime_path); | |
651 | if (r != PAM_SUCCESS) | |
652 | return r; | |
653 | } | |
00efd498 ZJS |
654 | |
655 | r = export_legacy_dbus_address(handle, pw->pw_uid, runtime_path); | |
656 | if (r != PAM_SUCCESS) | |
657 | return r; | |
98a28fef | 658 | } |
8c6db833 | 659 | |
b2f74f07 LP |
660 | /* Most likely we got the session/type/class from environment variables, but might have gotten the data |
661 | * somewhere else (for example PAM module parameters). Let's now update the environment variables, so that this | |
662 | * data is inherited into the session processes, and programs can rely on them to be initialized. */ | |
663 | ||
664 | r = update_environment(handle, "XDG_SESSION_TYPE", type); | |
665 | if (r != PAM_SUCCESS) | |
666 | return r; | |
667 | ||
668 | r = update_environment(handle, "XDG_SESSION_CLASS", class); | |
669 | if (r != PAM_SUCCESS) | |
670 | return r; | |
671 | ||
672 | r = update_environment(handle, "XDG_SESSION_DESKTOP", desktop); | |
673 | if (r != PAM_SUCCESS) | |
674 | return r; | |
675 | ||
d6baaa69 LP |
676 | r = update_environment(handle, "XDG_SEAT", seat); |
677 | if (r != PAM_SUCCESS) | |
678 | return r; | |
bbc73283 LP |
679 | |
680 | if (vtnr > 0) { | |
29d230f6 | 681 | char buf[DECIMAL_STR_MAX(vtnr)]; |
baae0358 | 682 | sprintf(buf, "%u", vtnr); |
bbc73283 | 683 | |
d6baaa69 LP |
684 | r = update_environment(handle, "XDG_VTNR", buf); |
685 | if (r != PAM_SUCCESS) | |
5a330cda | 686 | return r; |
bbc73283 LP |
687 | } |
688 | ||
77085881 LP |
689 | r = pam_set_data(handle, "systemd.existing", INT_TO_PTR(!!existing), NULL); |
690 | if (r != PAM_SUCCESS) { | |
691 | pam_syslog(handle, LOG_ERR, "Failed to install existing flag."); | |
5a330cda | 692 | return r; |
77085881 LP |
693 | } |
694 | ||
21c390cc | 695 | if (session_fd >= 0) { |
85c08dc0 | 696 | session_fd = fcntl(session_fd, F_DUPFD_CLOEXEC, 3); |
5a330cda ZJS |
697 | if (session_fd < 0) { |
698 | pam_syslog(handle, LOG_ERR, "Failed to dup session fd: %m"); | |
699 | return PAM_SESSION_ERR; | |
700 | } | |
701 | ||
23e096cc | 702 | r = pam_set_data(handle, "systemd.session-fd", FD_TO_PTR(session_fd), NULL); |
21c390cc LP |
703 | if (r != PAM_SUCCESS) { |
704 | pam_syslog(handle, LOG_ERR, "Failed to install session fd."); | |
03e334a1 | 705 | safe_close(session_fd); |
5a330cda | 706 | return r; |
21c390cc | 707 | } |
98a28fef | 708 | } |
8c6db833 | 709 | |
ffcfcb6b | 710 | return PAM_SUCCESS; |
98a28fef | 711 | } |
8c6db833 | 712 | |
98a28fef LP |
713 | _public_ PAM_EXTERN int pam_sm_close_session( |
714 | pam_handle_t *handle, | |
715 | int flags, | |
716 | int argc, const char **argv) { | |
8c6db833 | 717 | |
4afd3348 LP |
718 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
719 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; | |
5f41d1f1 | 720 | const void *existing = NULL; |
75c8e3cf | 721 | const char *id; |
75c8e3cf | 722 | int r; |
8c6db833 | 723 | |
ffcfcb6b | 724 | assert(handle); |
75c8e3cf | 725 | |
77085881 LP |
726 | /* Only release session if it wasn't pre-existing when we |
727 | * tried to create it */ | |
d487e2d6 | 728 | (void) pam_get_data(handle, "systemd.existing", &existing); |
77085881 | 729 | |
75c8e3cf | 730 | id = pam_getenv(handle, "XDG_SESSION_ID"); |
77085881 | 731 | if (id && !existing) { |
75c8e3cf LP |
732 | |
733 | /* Before we go and close the FIFO we need to tell | |
734 | * logind that this is a clean session shutdown, so | |
735 | * that it doesn't just go and slaughter us | |
736 | * immediately after closing the fd */ | |
737 | ||
ffcfcb6b ZJS |
738 | r = sd_bus_open_system(&bus); |
739 | if (r < 0) { | |
5f41d1f1 LP |
740 | pam_syslog(handle, LOG_ERR, "Failed to connect to system bus: %s", strerror(-r)); |
741 | return PAM_SESSION_ERR; | |
75c8e3cf LP |
742 | } |
743 | ||
ffcfcb6b ZJS |
744 | r = sd_bus_call_method(bus, |
745 | "org.freedesktop.login1", | |
746 | "/org/freedesktop/login1", | |
747 | "org.freedesktop.login1.Manager", | |
748 | "ReleaseSession", | |
749 | &error, | |
750 | NULL, | |
751 | "s", | |
752 | id); | |
753 | if (r < 0) { | |
5f41d1f1 LP |
754 | pam_syslog(handle, LOG_ERR, "Failed to release session: %s", bus_error_message(&error, r)); |
755 | return PAM_SESSION_ERR; | |
75c8e3cf LP |
756 | } |
757 | } | |
758 | ||
5f41d1f1 LP |
759 | /* Note that we are knowingly leaking the FIFO fd here. This |
760 | * way, logind can watch us die. If we closed it here it would | |
761 | * not have any clue when that is completed. Given that one | |
762 | * cannot really have multiple PAM sessions open from the same | |
763 | * process this means we will leak one FD at max. */ | |
75c8e3cf | 764 | |
5f41d1f1 | 765 | return PAM_SUCCESS; |
8c6db833 | 766 | } |