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