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