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