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