]>
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 | 13 | #include <sys/stat.h> |
e21d9060 | 14 | #include <sys/sysmacros.h> |
ca78ad1d ZJS |
15 | #include <sys/types.h> |
16 | #include <unistd.h> | |
8c6db833 | 17 | |
b5efdb8a | 18 | #include "alloc-util.h" |
430f0182 | 19 | #include "audit-util.h" |
00229fe4 LP |
20 | #include "bus-common-errors.h" |
21 | #include "bus-error.h" | |
47094ce0 | 22 | #include "bus-internal.h" |
ffcfcb6b | 23 | #include "bus-util.h" |
fdb3deca | 24 | #include "cgroup-setup.h" |
4bbccb02 | 25 | #include "errno-util.h" |
3ffd4af2 | 26 | #include "fd-util.h" |
a5c32cff | 27 | #include "fileio.h" |
f97b34a6 | 28 | #include "format-util.h" |
f9c1f4e1 | 29 | #include "fs-util.h" |
958b66ea | 30 | #include "hostname-util.h" |
f9c1f4e1 | 31 | #include "locale-util.h" |
00229fe4 LP |
32 | #include "login-util.h" |
33 | #include "macro.h" | |
d750dde2 | 34 | #include "pam-util.h" |
6bedfcbb | 35 | #include "parse-util.h" |
e37e5ed3 | 36 | #include "path-util.h" |
df0ff127 | 37 | #include "process-util.h" |
f9c1f4e1 | 38 | #include "rlimit-util.h" |
00229fe4 | 39 | #include "socket-util.h" |
055c08ef | 40 | #include "stdio-util.h" |
00229fe4 LP |
41 | #include "strv.h" |
42 | #include "terminal-util.h" | |
9ab0d3eb LP |
43 | #include "user-util.h" |
44 | #include "userdb.h" | |
8c6db833 | 45 | |
fbcb6300 LP |
46 | #define LOGIN_SLOW_BUS_CALL_TIMEOUT_USEC (2*USEC_PER_MINUTE) |
47 | ||
baae0358 LP |
48 | static int parse_argv( |
49 | pam_handle_t *handle, | |
50 | int argc, const char **argv, | |
51 | const char **class, | |
49ebd11f | 52 | const char **type, |
f5cb2820 | 53 | const char **desktop, |
baae0358 | 54 | bool *debug) { |
8c6db833 LP |
55 | |
56 | unsigned i; | |
57 | ||
58 | assert(argc >= 0); | |
59 | assert(argc == 0 || argv); | |
60 | ||
49ebd11f | 61 | for (i = 0; i < (unsigned) argc; i++) { |
d9608d40 LP |
62 | const char *p; |
63 | ||
64 | if ((p = startswith(argv[i], "class="))) { | |
485507b8 | 65 | if (class) |
d9608d40 | 66 | *class = p; |
485507b8 | 67 | |
d9608d40 | 68 | } else if ((p = startswith(argv[i], "type="))) { |
49ebd11f | 69 | if (type) |
d9608d40 | 70 | *type = p; |
49ebd11f | 71 | |
d9608d40 | 72 | } else if ((p = startswith(argv[i], "desktop="))) { |
f5cb2820 | 73 | if (desktop) |
d9608d40 | 74 | *desktop = p; |
f5cb2820 | 75 | |
05a049cc ZJS |
76 | } else if (streq(argv[i], "debug")) { |
77 | if (debug) | |
78 | *debug = true; | |
fb6becb4 | 79 | |
d9608d40 | 80 | } else if ((p = startswith(argv[i], "debug="))) { |
05a049cc | 81 | int k; |
0e318cad | 82 | |
d9608d40 | 83 | k = parse_boolean(p); |
05a049cc | 84 | if (k < 0) |
d9608d40 | 85 | pam_syslog(handle, LOG_WARNING, "Failed to parse debug= argument, ignoring: %s", p); |
05a049cc | 86 | else if (debug) |
0e318cad MS |
87 | *debug = k; |
88 | ||
05a049cc | 89 | } else |
fb6becb4 | 90 | pam_syslog(handle, LOG_WARNING, "Unknown parameter '%s', ignoring", argv[i]); |
49ebd11f | 91 | } |
8c6db833 | 92 | |
8c6db833 LP |
93 | return 0; |
94 | } | |
95 | ||
9ab0d3eb | 96 | static int acquire_user_record( |
8c6db833 | 97 | pam_handle_t *handle, |
9ab0d3eb | 98 | UserRecord **ret_record) { |
8c6db833 | 99 | |
9ab0d3eb LP |
100 | _cleanup_(user_record_unrefp) UserRecord *ur = NULL; |
101 | const char *username = NULL, *json = NULL; | |
8c6db833 LP |
102 | int r; |
103 | ||
104 | assert(handle); | |
8c6db833 | 105 | |
baae0358 LP |
106 | r = pam_get_user(handle, &username, NULL); |
107 | if (r != PAM_SUCCESS) { | |
d9608d40 | 108 | pam_syslog(handle, LOG_ERR, "Failed to get user name: %s", pam_strerror(handle, r)); |
baae0358 LP |
109 | return r; |
110 | } | |
d90b9d27 | 111 | |
baae0358 LP |
112 | if (isempty(username)) { |
113 | pam_syslog(handle, LOG_ERR, "User name not valid."); | |
9ab0d3eb | 114 | return PAM_SERVICE_ERR; |
8c6db833 LP |
115 | } |
116 | ||
9ab0d3eb LP |
117 | /* If pam_systemd_homed (or some other module) already acqired the user record we can reuse it |
118 | * here. */ | |
119 | r = pam_get_data(handle, "systemd-user-record", (const void**) &json); | |
120 | if (r != PAM_SUCCESS || !json) { | |
121 | _cleanup_free_ char *formatted = NULL; | |
122 | ||
123 | if (!IN_SET(r, PAM_SUCCESS, PAM_NO_MODULE_DATA)) { | |
124 | pam_syslog(handle, LOG_ERR, "Failed to get PAM user record data: %s", pam_strerror(handle, r)); | |
125 | return r; | |
126 | } | |
127 | ||
128 | /* Request the record ourselves */ | |
129 | r = userdb_by_name(username, 0, &ur); | |
130 | if (r < 0) { | |
131 | pam_syslog(handle, LOG_ERR, "Failed to get user record: %s", strerror_safe(r)); | |
132 | return PAM_USER_UNKNOWN; | |
133 | } | |
134 | ||
135 | r = json_variant_format(ur->json, 0, &formatted); | |
136 | if (r < 0) { | |
137 | pam_syslog(handle, LOG_ERR, "Failed to format user JSON: %s", strerror_safe(r)); | |
138 | return PAM_SERVICE_ERR; | |
139 | } | |
140 | ||
141 | /* And cache it for everyone else */ | |
142 | r = pam_set_data(handle, "systemd-user-record", formatted, pam_cleanup_free); | |
143 | if (r < 0) { | |
144 | pam_syslog(handle, LOG_ERR, "Failed to set PAM user record data: %s", pam_strerror(handle, r)); | |
145 | return r; | |
146 | } | |
147 | ||
148 | TAKE_PTR(formatted); | |
149 | } else { | |
150 | _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; | |
151 | ||
152 | /* Parse cached record */ | |
153 | r = json_parse(json, JSON_PARSE_SENSITIVE, &v, NULL, NULL); | |
154 | if (r < 0) { | |
155 | pam_syslog(handle, LOG_ERR, "Failed to parse JSON user record: %s", strerror_safe(r)); | |
156 | return PAM_SERVICE_ERR; | |
157 | } | |
158 | ||
159 | ur = user_record_new(); | |
160 | if (!ur) | |
161 | return pam_log_oom(handle); | |
162 | ||
163 | r = user_record_load(ur, v, USER_RECORD_LOAD_REFUSE_SECRET); | |
164 | if (r < 0) { | |
165 | pam_syslog(handle, LOG_ERR, "Failed to load user record: %s", strerror_safe(r)); | |
166 | return PAM_SERVICE_ERR; | |
167 | } | |
168 | ||
169 | /* Safety check if cached record actually matches what we are looking for */ | |
170 | if (!streq_ptr(username, ur->user_name)) { | |
171 | pam_syslog(handle, LOG_ERR, "Acquired user record does not match user name."); | |
172 | return PAM_SERVICE_ERR; | |
173 | } | |
174 | } | |
175 | ||
176 | if (!uid_is_valid(ur->uid)) { | |
177 | pam_syslog(handle, LOG_ERR, "Acquired user record does not have a UID."); | |
178 | return PAM_SERVICE_ERR; | |
8c6db833 LP |
179 | } |
180 | ||
9ab0d3eb LP |
181 | if (ret_record) |
182 | *ret_record = TAKE_PTR(ur); | |
8c6db833 LP |
183 | |
184 | return PAM_SUCCESS; | |
185 | } | |
186 | ||
ecd5f1a9 LP |
187 | static bool display_is_local(const char *display) { |
188 | assert(display); | |
189 | ||
190 | return | |
191 | display[0] == ':' && | |
192 | display[1] >= '0' && | |
193 | display[1] <= '9'; | |
194 | } | |
195 | ||
f7b8b5c4 LP |
196 | static int socket_from_display(const char *display, char **path) { |
197 | size_t k; | |
198 | char *f, *c; | |
199 | ||
200 | assert(display); | |
201 | assert(path); | |
202 | ||
203 | if (!display_is_local(display)) | |
204 | return -EINVAL; | |
205 | ||
206 | k = strspn(display+1, "0123456789"); | |
207 | ||
208 | f = new(char, STRLEN("/tmp/.X11-unix/X") + k + 1); | |
209 | if (!f) | |
210 | return -ENOMEM; | |
211 | ||
212 | c = stpcpy(f, "/tmp/.X11-unix/X"); | |
213 | memcpy(c, display+1, k); | |
214 | c[k] = 0; | |
215 | ||
216 | *path = f; | |
217 | ||
218 | return 0; | |
219 | } | |
220 | ||
4d6d6518 | 221 | static int get_seat_from_display(const char *display, const char **seat, uint32_t *vtnr) { |
f36a9d59 ZJS |
222 | union sockaddr_union sa; |
223 | socklen_t sa_len; | |
e21d9060 | 224 | _cleanup_free_ char *p = NULL, *sys_path = NULL, *tty = NULL; |
baae0358 | 225 | _cleanup_close_ int fd = -1; |
4d6d6518 | 226 | struct ucred ucred; |
f36a9d59 | 227 | int v, r; |
e21d9060 | 228 | dev_t display_ctty; |
4d6d6518 LP |
229 | |
230 | assert(display); | |
4d6d6518 LP |
231 | assert(vtnr); |
232 | ||
233 | /* We deduce the X11 socket from the display name, then use | |
234 | * SO_PEERCRED to determine the X11 server process, ask for | |
235 | * the controlling tty of that and if it's a VC then we know | |
236 | * the seat and the virtual terminal. Sounds ugly, is only | |
237 | * semi-ugly. */ | |
238 | ||
239 | r = socket_from_display(display, &p); | |
240 | if (r < 0) | |
241 | return r; | |
f36a9d59 ZJS |
242 | r = sockaddr_un_set_path(&sa.un, p); |
243 | if (r < 0) | |
244 | return r; | |
245 | sa_len = r; | |
4d6d6518 LP |
246 | |
247 | fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0); | |
b92bea5d | 248 | if (fd < 0) |
4d6d6518 | 249 | return -errno; |
4d6d6518 | 250 | |
f36a9d59 | 251 | if (connect(fd, &sa.sa, sa_len) < 0) |
4d6d6518 | 252 | return -errno; |
4d6d6518 | 253 | |
eff05270 | 254 | r = getpeercred(fd, &ucred); |
4d6d6518 | 255 | if (r < 0) |
eff05270 | 256 | return r; |
4d6d6518 | 257 | |
e21d9060 TM |
258 | r = get_ctty_devnr(ucred.pid, &display_ctty); |
259 | if (r < 0) | |
260 | return r; | |
261 | ||
262 | if (asprintf(&sys_path, "/sys/dev/char/%d:%d", major(display_ctty), minor(display_ctty)) < 0) | |
263 | return -ENOMEM; | |
264 | r = readlink_value(sys_path, &tty); | |
4d6d6518 LP |
265 | if (r < 0) |
266 | return r; | |
267 | ||
268 | v = vtnr_from_tty(tty); | |
4d6d6518 LP |
269 | if (v < 0) |
270 | return v; | |
271 | else if (v == 0) | |
272 | return -ENOENT; | |
273 | ||
fc7985ed LP |
274 | if (seat) |
275 | *seat = "seat0"; | |
4d6d6518 LP |
276 | *vtnr = (uint32_t) v; |
277 | ||
278 | return 0; | |
279 | } | |
280 | ||
00efd498 ZJS |
281 | static int export_legacy_dbus_address( |
282 | pam_handle_t *handle, | |
00efd498 ZJS |
283 | const char *runtime) { |
284 | ||
15ee6c20 ZJS |
285 | const char *s; |
286 | _cleanup_free_ char *t = NULL; | |
00efd498 ZJS |
287 | int r = PAM_BUF_ERR; |
288 | ||
15ee6c20 ZJS |
289 | /* We need to export $DBUS_SESSION_BUS_ADDRESS because various applications will not connect |
290 | * correctly to the bus without it. This setting matches what dbus.socket does for the user | |
291 | * session using 'systemctl --user set-environment'. We want to have the same configuration | |
292 | * in processes started from the PAM session. | |
293 | * | |
294 | * The setting of the address is guarded by the access() check because it is also possible to compile | |
295 | * dbus without --enable-user-session, in which case this socket is not used, and | |
296 | * $DBUS_SESSION_BUS_ADDRESS should not be set. An alternative approach would to not do the access() | |
297 | * check here, and let applications try on their own, by using "unix:path=%s/bus;autolaunch:". But we | |
298 | * expect the socket to be present by the time we do this check, so we can just as well check once | |
299 | * here. */ | |
300 | ||
301 | s = strjoina(runtime, "/bus"); | |
302 | if (access(s, F_OK) < 0) | |
303 | return PAM_SUCCESS; | |
304 | ||
305 | if (asprintf(&t, DEFAULT_USER_BUS_ADDRESS_FMT, runtime) < 0) | |
d750dde2 | 306 | return pam_log_oom(handle); |
00efd498 | 307 | |
15ee6c20 | 308 | r = pam_misc_setenv(handle, "DBUS_SESSION_BUS_ADDRESS", t, 0); |
d750dde2 LP |
309 | if (r != PAM_SUCCESS) { |
310 | pam_syslog(handle, LOG_ERR, "Failed to set bus variable: %s", pam_strerror(handle, r)); | |
311 | return r; | |
312 | } | |
00efd498 ZJS |
313 | |
314 | return PAM_SUCCESS; | |
00efd498 ZJS |
315 | } |
316 | ||
22f93314 JS |
317 | static int append_session_memory_max(pam_handle_t *handle, sd_bus_message *m, const char *limit) { |
318 | uint64_t val; | |
319 | int r; | |
320 | ||
321 | if (isempty(limit)) | |
7bfbf6cc | 322 | return PAM_SUCCESS; |
22f93314 JS |
323 | |
324 | if (streq(limit, "infinity")) { | |
325 | r = sd_bus_message_append(m, "(sv)", "MemoryMax", "t", (uint64_t)-1); | |
d750dde2 LP |
326 | if (r < 0) |
327 | return pam_bus_log_create_error(handle, r); | |
328 | ||
7bfbf6cc | 329 | return PAM_SUCCESS; |
d750dde2 LP |
330 | } |
331 | ||
332 | r = parse_permille(limit); | |
333 | if (r >= 0) { | |
334 | r = sd_bus_message_append(m, "(sv)", "MemoryMaxScale", "u", (uint32_t) (((uint64_t) r * UINT32_MAX) / 1000U)); | |
335 | if (r < 0) | |
336 | return pam_bus_log_create_error(handle, r); | |
337 | ||
7bfbf6cc | 338 | return PAM_SUCCESS; |
d750dde2 LP |
339 | } |
340 | ||
341 | r = parse_size(limit, 1024, &val); | |
342 | if (r >= 0) { | |
343 | r = sd_bus_message_append(m, "(sv)", "MemoryMax", "t", val); | |
344 | if (r < 0) | |
345 | return pam_bus_log_create_error(handle, r); | |
346 | ||
7bfbf6cc | 347 | return PAM_SUCCESS; |
22f93314 JS |
348 | } |
349 | ||
d750dde2 | 350 | pam_syslog(handle, LOG_WARNING, "Failed to parse systemd.memory_max, ignoring: %s", limit); |
7bfbf6cc | 351 | return PAM_SUCCESS; |
22f93314 JS |
352 | } |
353 | ||
adc09af2 PW |
354 | static int append_session_runtime_max_sec(pam_handle_t *handle, sd_bus_message *m, const char *limit) { |
355 | usec_t val; | |
356 | int r; | |
357 | ||
358 | /* No need to parse "infinity" here, it will be set by default later in scope_init() */ | |
359 | if (isempty(limit) || streq(limit, "infinity")) | |
7bfbf6cc | 360 | return PAM_SUCCESS; |
adc09af2 PW |
361 | |
362 | r = parse_sec(limit, &val); | |
363 | if (r >= 0) { | |
364 | r = sd_bus_message_append(m, "(sv)", "RuntimeMaxUSec", "t", (uint64_t) val); | |
7bfbf6cc LP |
365 | if (r < 0) |
366 | return pam_bus_log_create_error(handle, r); | |
adc09af2 PW |
367 | } else |
368 | pam_syslog(handle, LOG_WARNING, "Failed to parse systemd.runtime_max_sec: %s, ignoring.", limit); | |
369 | ||
7bfbf6cc | 370 | return PAM_SUCCESS; |
adc09af2 PW |
371 | } |
372 | ||
5fdfbbd5 | 373 | static int append_session_tasks_max(pam_handle_t *handle, sd_bus_message *m, const char *limit) { |
22f93314 JS |
374 | uint64_t val; |
375 | int r; | |
376 | ||
377 | /* No need to parse "infinity" here, it will be set unconditionally later in manager_start_scope() */ | |
378 | if (isempty(limit) || streq(limit, "infinity")) | |
7bfbf6cc | 379 | return PAM_SUCCESS; |
22f93314 JS |
380 | |
381 | r = safe_atou64(limit, &val); | |
382 | if (r >= 0) { | |
383 | r = sd_bus_message_append(m, "(sv)", "TasksMax", "t", val); | |
d750dde2 LP |
384 | if (r < 0) |
385 | return pam_bus_log_create_error(handle, r); | |
22f93314 | 386 | } else |
d750dde2 | 387 | pam_syslog(handle, LOG_WARNING, "Failed to parse systemd.tasks_max, ignoring: %s", limit); |
22f93314 | 388 | |
7bfbf6cc | 389 | return PAM_SUCCESS; |
22f93314 JS |
390 | } |
391 | ||
392 | static int append_session_cg_weight(pam_handle_t *handle, sd_bus_message *m, const char *limit, const char *field) { | |
393 | uint64_t val; | |
394 | int r; | |
395 | ||
36a4dbae | 396 | if (isempty(limit)) |
7bfbf6cc | 397 | return PAM_SUCCESS; |
36a4dbae LP |
398 | |
399 | r = cg_weight_parse(limit, &val); | |
400 | if (r >= 0) { | |
401 | r = sd_bus_message_append(m, "(sv)", field, "t", val); | |
d750dde2 LP |
402 | if (r < 0) |
403 | return pam_bus_log_create_error(handle, r); | |
36a4dbae | 404 | } else if (streq(field, "CPUWeight")) |
d750dde2 | 405 | pam_syslog(handle, LOG_WARNING, "Failed to parse systemd.cpu_weight, ignoring: %s", limit); |
36a4dbae | 406 | else |
d750dde2 | 407 | pam_syslog(handle, LOG_WARNING, "Failed to parse systemd.io_weight, ignoring: %s", limit); |
22f93314 | 408 | |
7bfbf6cc | 409 | return PAM_SUCCESS; |
22f93314 JS |
410 | } |
411 | ||
0ecc1c9d LP |
412 | static const char* getenv_harder(pam_handle_t *handle, const char *key, const char *fallback) { |
413 | const char *v; | |
414 | ||
415 | assert(handle); | |
416 | assert(key); | |
417 | ||
5238e957 | 418 | /* Looks for an environment variable, preferably in the environment block associated with the |
83d4ab55 LP |
419 | * specified PAM handle, falling back to the process' block instead. Why check both? Because we want |
420 | * to permit configuration of session properties from unit files that invoke PAM services, so that | |
421 | * PAM services don't have to be reworked to set systemd-specific properties, but these properties | |
422 | * can still be set from the unit file Environment= block. */ | |
0ecc1c9d LP |
423 | |
424 | v = pam_getenv(handle, key); | |
425 | if (!isempty(v)) | |
426 | return v; | |
427 | ||
83d4ab55 LP |
428 | /* We use secure_getenv() here, since we might get loaded into su/sudo, which are SUID. Ideally |
429 | * they'd clean up the environment before invoking foreign code (such as PAM modules), but alas they | |
430 | * currently don't (to be precise, they clean up the environment they pass to their children, but | |
431 | * not their own environ[]). */ | |
432 | v = secure_getenv(key); | |
0ecc1c9d LP |
433 | if (!isempty(v)) |
434 | return v; | |
435 | ||
436 | return fallback; | |
437 | } | |
438 | ||
d6baaa69 LP |
439 | static int update_environment(pam_handle_t *handle, const char *key, const char *value) { |
440 | int r; | |
441 | ||
442 | assert(handle); | |
443 | assert(key); | |
444 | ||
445 | /* Updates the environment, but only if there's actually a value set. Also, log about errors */ | |
446 | ||
447 | if (isempty(value)) | |
448 | return PAM_SUCCESS; | |
449 | ||
450 | r = pam_misc_setenv(handle, key, value, 0); | |
451 | if (r != PAM_SUCCESS) | |
d9608d40 | 452 | pam_syslog(handle, LOG_ERR, "Failed to set environment variable %s: %s", key, pam_strerror(handle, r)); |
d6baaa69 LP |
453 | |
454 | return r; | |
455 | } | |
456 | ||
b9217112 LP |
457 | static bool validate_runtime_directory(pam_handle_t *handle, const char *path, uid_t uid) { |
458 | struct stat st; | |
459 | ||
dca81e28 | 460 | assert(handle); |
b9217112 LP |
461 | assert(path); |
462 | ||
463 | /* Just some extra paranoia: let's not set $XDG_RUNTIME_DIR if the directory we'd set it to isn't actually set | |
464 | * up properly for us. */ | |
465 | ||
466 | if (lstat(path, &st) < 0) { | |
4bbccb02 | 467 | pam_syslog(handle, LOG_ERR, "Failed to stat() runtime directory '%s': %s", path, strerror_safe(errno)); |
b9217112 LP |
468 | goto fail; |
469 | } | |
470 | ||
471 | if (!S_ISDIR(st.st_mode)) { | |
472 | pam_syslog(handle, LOG_ERR, "Runtime directory '%s' is not actually a directory.", path); | |
473 | goto fail; | |
474 | } | |
475 | ||
476 | if (st.st_uid != uid) { | |
477 | pam_syslog(handle, LOG_ERR, "Runtime directory '%s' is not owned by UID " UID_FMT ", as it should.", path, uid); | |
478 | goto fail; | |
479 | } | |
480 | ||
481 | return true; | |
482 | ||
483 | fail: | |
484 | pam_syslog(handle, LOG_WARNING, "Not setting $XDG_RUNTIME_DIR, as the directory is not in order."); | |
485 | return false; | |
486 | } | |
487 | ||
f9c1f4e1 LP |
488 | static int pam_putenv_and_log(pam_handle_t *handle, const char *e, bool debug) { |
489 | int r; | |
490 | ||
491 | assert(handle); | |
492 | assert(e); | |
493 | ||
494 | r = pam_putenv(handle, e); | |
495 | if (r != PAM_SUCCESS) { | |
496 | pam_syslog(handle, LOG_ERR, "Failed to set PAM environment variable %s: %s", e, pam_strerror(handle, r)); | |
497 | return r; | |
498 | } | |
499 | ||
500 | if (debug) | |
501 | pam_syslog(handle, LOG_DEBUG, "PAM environment variable %s set based on user record.", e); | |
502 | ||
503 | return PAM_SUCCESS; | |
504 | } | |
505 | ||
506 | static int apply_user_record_settings(pam_handle_t *handle, UserRecord *ur, bool debug) { | |
507 | char **i; | |
508 | int r; | |
509 | ||
510 | assert(handle); | |
511 | assert(ur); | |
512 | ||
513 | if (ur->umask != MODE_INVALID) { | |
514 | umask(ur->umask); | |
515 | ||
516 | if (debug) | |
517 | pam_syslog(handle, LOG_DEBUG, "Set user umask to %04o based on user record.", ur->umask); | |
518 | } | |
519 | ||
520 | STRV_FOREACH(i, ur->environment) { | |
521 | _cleanup_free_ char *n = NULL; | |
522 | const char *e; | |
523 | ||
524 | assert_se(e = strchr(*i, '=')); /* environment was already validated while parsing JSON record, this thus must hold */ | |
525 | ||
526 | n = strndup(*i, e - *i); | |
527 | if (!n) | |
528 | return pam_log_oom(handle); | |
529 | ||
530 | if (pam_getenv(handle, n)) { | |
531 | if (debug) | |
532 | pam_syslog(handle, LOG_DEBUG, "PAM environment variable $%s already set, not changing based on record.", *i); | |
533 | continue; | |
534 | } | |
535 | ||
536 | r = pam_putenv_and_log(handle, *i, debug); | |
537 | if (r != PAM_SUCCESS) | |
538 | return r; | |
539 | } | |
540 | ||
541 | if (ur->email_address) { | |
542 | if (pam_getenv(handle, "EMAIL")) { | |
543 | if (debug) | |
544 | pam_syslog(handle, LOG_DEBUG, "PAM environment variable $EMAIL already set, not changing based on user record."); | |
545 | } else { | |
546 | _cleanup_free_ char *joined = NULL; | |
547 | ||
548 | joined = strjoin("EMAIL=", ur->email_address); | |
549 | if (!joined) | |
550 | return pam_log_oom(handle); | |
551 | ||
552 | r = pam_putenv_and_log(handle, joined, debug); | |
553 | if (r != PAM_SUCCESS) | |
554 | return r; | |
555 | } | |
556 | } | |
557 | ||
558 | if (ur->time_zone) { | |
559 | if (pam_getenv(handle, "TZ")) { | |
560 | if (debug) | |
561 | pam_syslog(handle, LOG_DEBUG, "PAM environment variable $TZ already set, not changing based on user record."); | |
562 | } else if (!timezone_is_valid(ur->time_zone, LOG_DEBUG)) { | |
563 | if (debug) | |
564 | pam_syslog(handle, LOG_DEBUG, "Time zone specified in user record is not valid locally, not setting $TZ."); | |
565 | } else { | |
566 | _cleanup_free_ char *joined = NULL; | |
567 | ||
568 | joined = strjoin("TZ=:", ur->time_zone); | |
569 | if (!joined) | |
570 | return pam_log_oom(handle); | |
571 | ||
572 | r = pam_putenv_and_log(handle, joined, debug); | |
573 | if (r != PAM_SUCCESS) | |
574 | return r; | |
575 | } | |
576 | } | |
577 | ||
578 | if (ur->preferred_language) { | |
579 | if (pam_getenv(handle, "LANG")) { | |
580 | if (debug) | |
581 | pam_syslog(handle, LOG_DEBUG, "PAM environment variable $LANG already set, not changing based on user record."); | |
a00a78b8 | 582 | } else if (locale_is_installed(ur->preferred_language) <= 0) { |
f9c1f4e1 | 583 | if (debug) |
a00a78b8 | 584 | pam_syslog(handle, LOG_DEBUG, "Preferred language specified in user record is not valid or not installed, not setting $LANG."); |
f9c1f4e1 LP |
585 | } else { |
586 | _cleanup_free_ char *joined = NULL; | |
587 | ||
588 | joined = strjoin("LANG=", ur->preferred_language); | |
589 | if (!joined) | |
590 | return pam_log_oom(handle); | |
591 | ||
592 | r = pam_putenv_and_log(handle, joined, debug); | |
593 | if (r != PAM_SUCCESS) | |
594 | return r; | |
595 | } | |
596 | } | |
597 | ||
598 | if (nice_is_valid(ur->nice_level)) { | |
599 | if (nice(ur->nice_level) < 0) | |
600 | pam_syslog(handle, LOG_ERR, "Failed to set nice level to %i, ignoring: %s", ur->nice_level, strerror_safe(errno)); | |
601 | else if (debug) | |
602 | pam_syslog(handle, LOG_DEBUG, "Nice level set, based on user record."); | |
603 | } | |
604 | ||
605 | for (int rl = 0; rl < _RLIMIT_MAX; rl++) { | |
606 | ||
607 | if (!ur->rlimits[rl]) | |
608 | continue; | |
609 | ||
610 | r = setrlimit_closest(rl, ur->rlimits[rl]); | |
611 | if (r < 0) | |
612 | pam_syslog(handle, LOG_ERR, "Failed to set resource limit %s, ignoring: %s", rlimit_to_string(rl), strerror_safe(r)); | |
613 | else if (debug) | |
614 | pam_syslog(handle, LOG_DEBUG, "Resource limit %s set, based on user record.", rlimit_to_string(rl)); | |
615 | } | |
616 | ||
617 | return PAM_SUCCESS; | |
618 | } | |
619 | ||
98a28fef | 620 | _public_ PAM_EXTERN int pam_sm_open_session( |
8c6db833 LP |
621 | pam_handle_t *handle, |
622 | int flags, | |
623 | int argc, const char **argv) { | |
624 | ||
4afd3348 | 625 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
22f93314 | 626 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL; |
d1529c9e | 627 | const char |
9ab0d3eb | 628 | *id, *object_path, *runtime_path, |
d1529c9e LP |
629 | *service = NULL, |
630 | *tty = NULL, *display = NULL, | |
631 | *remote_user = NULL, *remote_host = NULL, | |
632 | *seat = NULL, | |
633 | *type = NULL, *class = NULL, | |
f5cb2820 | 634 | *class_pam = NULL, *type_pam = NULL, *cvtnr = NULL, *desktop = NULL, *desktop_pam = NULL, |
adc09af2 | 635 | *memory_max = NULL, *tasks_max = NULL, *cpu_weight = NULL, *io_weight = NULL, *runtime_max_sec = NULL; |
4afd3348 | 636 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
9ab0d3eb | 637 | _cleanup_(user_record_unrefp) UserRecord *ur = NULL; |
d1529c9e | 638 | int session_fd = -1, existing, r; |
d1529c9e | 639 | bool debug = false, remote; |
baae0358 LP |
640 | uint32_t vtnr = 0; |
641 | uid_t original_uid; | |
8c6db833 | 642 | |
ffcfcb6b | 643 | assert(handle); |
98a28fef | 644 | |
79d860fe MP |
645 | /* Make this a NOP on non-logind systems */ |
646 | if (!logind_running()) | |
8c6db833 LP |
647 | return PAM_SUCCESS; |
648 | ||
e9fbc77c LP |
649 | if (parse_argv(handle, |
650 | argc, argv, | |
fb6becb4 | 651 | &class_pam, |
49ebd11f | 652 | &type_pam, |
f5cb2820 | 653 | &desktop_pam, |
5a330cda ZJS |
654 | &debug) < 0) |
655 | return PAM_SESSION_ERR; | |
74fe1fe3 | 656 | |
baae0358 | 657 | if (debug) |
3831838a | 658 | pam_syslog(handle, LOG_DEBUG, "pam-systemd initializing"); |
baae0358 | 659 | |
9ab0d3eb | 660 | r = acquire_user_record(handle, &ur); |
42e66809 | 661 | if (r != PAM_SUCCESS) |
5a330cda | 662 | return r; |
8c6db833 | 663 | |
30b2c336 LP |
664 | /* Make sure we don't enter a loop by talking to |
665 | * systemd-logind when it is actually waiting for the | |
666 | * background to finish start-up. If the service is | |
5c390a4a | 667 | * "systemd-user" we simply set XDG_RUNTIME_DIR and |
30b2c336 LP |
668 | * leave. */ |
669 | ||
1798f5af | 670 | (void) pam_get_item(handle, PAM_SERVICE, (const void**) &service); |
5c390a4a | 671 | if (streq_ptr(service, "systemd-user")) { |
055c08ef | 672 | char rt[STRLEN("/run/user/") + DECIMAL_STR_MAX(uid_t)]; |
30b2c336 | 673 | |
9ab0d3eb LP |
674 | xsprintf(rt, "/run/user/"UID_FMT, ur->uid); |
675 | if (validate_runtime_directory(handle, rt, ur->uid)) { | |
b9217112 LP |
676 | r = pam_misc_setenv(handle, "XDG_RUNTIME_DIR", rt, 0); |
677 | if (r != PAM_SUCCESS) { | |
d9608d40 | 678 | pam_syslog(handle, LOG_ERR, "Failed to set runtime dir: %s", pam_strerror(handle, r)); |
b9217112 LP |
679 | return r; |
680 | } | |
30b2c336 LP |
681 | } |
682 | ||
3400bc86 | 683 | r = export_legacy_dbus_address(handle, rt); |
00efd498 ZJS |
684 | if (r != PAM_SUCCESS) |
685 | return r; | |
686 | ||
f9c1f4e1 LP |
687 | r = apply_user_record_settings(handle, ur, debug); |
688 | if (r != PAM_SUCCESS) | |
689 | return r; | |
690 | ||
ffcfcb6b | 691 | return PAM_SUCCESS; |
8c6db833 LP |
692 | } |
693 | ||
ffcfcb6b | 694 | /* Otherwise, we ask logind to create a session for us */ |
8c6db833 | 695 | |
1798f5af LP |
696 | (void) pam_get_item(handle, PAM_XDISPLAY, (const void**) &display); |
697 | (void) pam_get_item(handle, PAM_TTY, (const void**) &tty); | |
698 | (void) pam_get_item(handle, PAM_RUSER, (const void**) &remote_user); | |
699 | (void) pam_get_item(handle, PAM_RHOST, (const void**) &remote_host); | |
a8573ccc | 700 | |
0ecc1c9d LP |
701 | seat = getenv_harder(handle, "XDG_SEAT", NULL); |
702 | cvtnr = getenv_harder(handle, "XDG_VTNR", NULL); | |
703 | type = getenv_harder(handle, "XDG_SESSION_TYPE", type_pam); | |
704 | class = getenv_harder(handle, "XDG_SESSION_CLASS", class_pam); | |
f5cb2820 | 705 | desktop = getenv_harder(handle, "XDG_SESSION_DESKTOP", desktop_pam); |
a4cd87e9 | 706 | |
ed18b08b | 707 | tty = strempty(tty); |
98a28fef | 708 | |
ee8545b0 | 709 | if (strchr(tty, ':')) { |
3a736949 LP |
710 | /* A tty with a colon is usually an X11 display, placed there to show up in utmp. We rearrange things |
711 | * and don't pretend that an X display was a tty. */ | |
ee8545b0 LP |
712 | if (isempty(display)) |
713 | display = tty; | |
a4cd87e9 | 714 | tty = NULL; |
3a736949 | 715 | |
1a4459d6 | 716 | } else if (streq(tty, "cron")) { |
3a736949 LP |
717 | /* cron is setting PAM_TTY to "cron" for some reason (the commit carries no information why, but |
718 | * probably because it wants to set it to something as pam_time/pam_access/… require PAM_TTY to be set | |
719 | * (as they otherwise even try to update it!) — but cron doesn't actually allocate a TTY for its forked | |
720 | * off processes.) */ | |
0ad1271f | 721 | type = "unspecified"; |
49ebd11f | 722 | class = "background"; |
a4cd87e9 | 723 | tty = NULL; |
3a736949 | 724 | |
0ad1271f | 725 | } else if (streq(tty, "ssh")) { |
3a736949 LP |
726 | /* ssh has been setting PAM_TTY to "ssh" (for the same reason as cron does this, see above. For further |
727 | * details look for "PAM_TTY_KLUDGE" in the openssh sources). */ | |
0ad1271f | 728 | type ="tty"; |
49ebd11f | 729 | class = "user"; |
3a736949 LP |
730 | tty = NULL; /* This one is particularly sad, as this means that ssh sessions — even though usually |
731 | * associated with a pty — won't be tracked by their tty in logind. This is because ssh | |
732 | * does the PAM session registration early for new connections, and registers a pty only | |
733 | * much later (this is because it doesn't know yet if it needs one at all, as whether to | |
734 | * register a pty or not is negotiated much later in the protocol). */ | |
735 | ||
c9ed61e7 LP |
736 | } else |
737 | /* Chop off leading /dev prefix that some clients specify, but others do not. */ | |
738 | tty = skip_dev_prefix(tty); | |
ee8545b0 | 739 | |
8e7705e5 | 740 | /* If this fails vtnr will be 0, that's intended */ |
4d6d6518 | 741 | if (!isempty(cvtnr)) |
2ae4842b | 742 | (void) safe_atou32(cvtnr, &vtnr); |
4d6d6518 | 743 | |
92bd5ff3 | 744 | if (!isempty(display) && !vtnr) { |
fc7985ed | 745 | if (isempty(seat)) |
d487e2d6 | 746 | (void) get_seat_from_display(display, &seat, &vtnr); |
fc7985ed | 747 | else if (streq(seat, "seat0")) |
d487e2d6 | 748 | (void) get_seat_from_display(display, NULL, &vtnr); |
fc7985ed | 749 | } |
4d6d6518 | 750 | |
49ebd11f | 751 | if (seat && !streq(seat, "seat0") && vtnr != 0) { |
2675747f LP |
752 | if (debug) |
753 | pam_syslog(handle, LOG_DEBUG, "Ignoring vtnr %"PRIu32" for %s which is not seat0", vtnr, seat); | |
d7353ef6 MM |
754 | vtnr = 0; |
755 | } | |
756 | ||
49ebd11f | 757 | if (isempty(type)) |
0ad1271f | 758 | type = !isempty(display) ? "x11" : |
49ebd11f | 759 | !isempty(tty) ? "tty" : "unspecified"; |
98a28fef | 760 | |
55efac6c | 761 | if (isempty(class)) |
e2acb67b | 762 | class = streq(type, "unspecified") ? "background" : "user"; |
55efac6c | 763 | |
fecc80c1 | 764 | remote = !isempty(remote_host) && !is_localhost(remote_host); |
98a28fef | 765 | |
22f93314 JS |
766 | (void) pam_get_data(handle, "systemd.memory_max", (const void **)&memory_max); |
767 | (void) pam_get_data(handle, "systemd.tasks_max", (const void **)&tasks_max); | |
768 | (void) pam_get_data(handle, "systemd.cpu_weight", (const void **)&cpu_weight); | |
769 | (void) pam_get_data(handle, "systemd.io_weight", (const void **)&io_weight); | |
adc09af2 | 770 | (void) pam_get_data(handle, "systemd.runtime_max_sec", (const void **)&runtime_max_sec); |
22f93314 | 771 | |
4d49b48c | 772 | /* Talk to logind over the message bus */ |
5a330cda | 773 | |
355c9966 LP |
774 | r = pam_acquire_bus_connection(handle, &bus); |
775 | if (r != PAM_SUCCESS) | |
776 | return r; | |
cc377381 | 777 | |
22f93314 | 778 | if (debug) { |
ce959314 | 779 | pam_syslog(handle, LOG_DEBUG, "Asking logind to create session: " |
1fa2f38f | 780 | "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", |
9ab0d3eb | 781 | ur->uid, getpid_cached(), |
baae0358 | 782 | strempty(service), |
cda7ecb0 | 783 | type, class, strempty(desktop), |
a4cd87e9 | 784 | strempty(seat), vtnr, strempty(tty), strempty(display), |
baae0358 | 785 | yes_no(remote), strempty(remote_user), strempty(remote_host)); |
22f93314 | 786 | pam_syslog(handle, LOG_DEBUG, "Session limits: " |
adc09af2 PW |
787 | "memory_max=%s tasks_max=%s cpu_weight=%s io_weight=%s runtime_max_sec=%s", |
788 | strna(memory_max), strna(tasks_max), strna(cpu_weight), strna(io_weight), strna(runtime_max_sec)); | |
22f93314 JS |
789 | } |
790 | ||
5d990cc5 | 791 | r = bus_message_new_method_call(bus, &m, bus_login_mgr, "CreateSession"); |
d750dde2 LP |
792 | if (r < 0) |
793 | return pam_bus_log_create_error(handle, r); | |
22f93314 JS |
794 | |
795 | r = sd_bus_message_append(m, "uusssssussbss", | |
9ab0d3eb | 796 | (uint32_t) ur->uid, |
da0da5ec | 797 | 0, |
22f93314 JS |
798 | service, |
799 | type, | |
800 | class, | |
801 | desktop, | |
802 | seat, | |
803 | vtnr, | |
804 | tty, | |
805 | display, | |
806 | remote, | |
807 | remote_user, | |
808 | remote_host); | |
d750dde2 LP |
809 | if (r < 0) |
810 | return pam_bus_log_create_error(handle, r); | |
22f93314 JS |
811 | |
812 | r = sd_bus_message_open_container(m, 'a', "(sv)"); | |
d750dde2 LP |
813 | if (r < 0) |
814 | return pam_bus_log_create_error(handle, r); | |
22f93314 JS |
815 | |
816 | r = append_session_memory_max(handle, m, memory_max); | |
7bfbf6cc LP |
817 | if (r != PAM_SUCCESS) |
818 | return r; | |
22f93314 | 819 | |
adc09af2 | 820 | r = append_session_runtime_max_sec(handle, m, runtime_max_sec); |
7bfbf6cc LP |
821 | if (r != PAM_SUCCESS) |
822 | return r; | |
adc09af2 | 823 | |
22f93314 | 824 | r = append_session_tasks_max(handle, m, tasks_max); |
7bfbf6cc LP |
825 | if (r != PAM_SUCCESS) |
826 | return r; | |
22f93314 JS |
827 | |
828 | r = append_session_cg_weight(handle, m, cpu_weight, "CPUWeight"); | |
7bfbf6cc LP |
829 | if (r != PAM_SUCCESS) |
830 | return r; | |
22f93314 JS |
831 | |
832 | r = append_session_cg_weight(handle, m, io_weight, "IOWeight"); | |
7bfbf6cc LP |
833 | if (r != PAM_SUCCESS) |
834 | return r; | |
22f93314 JS |
835 | |
836 | r = sd_bus_message_close_container(m); | |
d750dde2 LP |
837 | if (r < 0) |
838 | return pam_bus_log_create_error(handle, r); | |
ce959314 | 839 | |
fbcb6300 | 840 | r = sd_bus_call(bus, m, LOGIN_SLOW_BUS_CALL_TIMEOUT_USEC, &error, &reply); |
ffcfcb6b | 841 | if (r < 0) { |
b80120c4 | 842 | if (sd_bus_error_has_name(&error, BUS_ERROR_SESSION_BUSY)) { |
2675747f | 843 | if (debug) |
c7e93c4d | 844 | pam_syslog(handle, LOG_DEBUG, "Not creating session: %s", bus_error_message(&error, r)); |
b80120c4 DH |
845 | return PAM_SUCCESS; |
846 | } else { | |
847 | pam_syslog(handle, LOG_ERR, "Failed to create session: %s", bus_error_message(&error, r)); | |
8d46418e | 848 | return PAM_SESSION_ERR; |
b80120c4 | 849 | } |
98a28fef | 850 | } |
74fe1fe3 | 851 | |
ffcfcb6b | 852 | r = sd_bus_message_read(reply, |
baae0358 | 853 | "soshusub", |
ffcfcb6b ZJS |
854 | &id, |
855 | &object_path, | |
856 | &runtime_path, | |
857 | &session_fd, | |
baae0358 | 858 | &original_uid, |
ffcfcb6b ZJS |
859 | &seat, |
860 | &vtnr, | |
861 | &existing); | |
d750dde2 LP |
862 | if (r < 0) |
863 | return pam_bus_log_parse_error(handle, r); | |
74fe1fe3 | 864 | |
ce959314 MS |
865 | if (debug) |
866 | pam_syslog(handle, LOG_DEBUG, "Reply from logind: " | |
baae0358 LP |
867 | "id=%s object_path=%s runtime_path=%s session_fd=%d seat=%s vtnr=%u original_uid=%u", |
868 | id, object_path, runtime_path, session_fd, seat, vtnr, original_uid); | |
ce959314 | 869 | |
d6baaa69 LP |
870 | r = update_environment(handle, "XDG_SESSION_ID", id); |
871 | if (r != PAM_SUCCESS) | |
5a330cda | 872 | return r; |
8c6db833 | 873 | |
9ab0d3eb | 874 | if (original_uid == ur->uid) { |
baae0358 LP |
875 | /* Don't set $XDG_RUNTIME_DIR if the user we now |
876 | * authenticated for does not match the original user | |
877 | * of the session. We do this in order not to result | |
878 | * in privileged apps clobbering the runtime directory | |
879 | * unnecessarily. */ | |
880 | ||
9ab0d3eb | 881 | if (validate_runtime_directory(handle, runtime_path, ur->uid)) { |
b9217112 LP |
882 | r = update_environment(handle, "XDG_RUNTIME_DIR", runtime_path); |
883 | if (r != PAM_SUCCESS) | |
884 | return r; | |
885 | } | |
00efd498 | 886 | |
3400bc86 | 887 | r = export_legacy_dbus_address(handle, runtime_path); |
00efd498 ZJS |
888 | if (r != PAM_SUCCESS) |
889 | return r; | |
98a28fef | 890 | } |
8c6db833 | 891 | |
b2f74f07 LP |
892 | /* Most likely we got the session/type/class from environment variables, but might have gotten the data |
893 | * somewhere else (for example PAM module parameters). Let's now update the environment variables, so that this | |
894 | * data is inherited into the session processes, and programs can rely on them to be initialized. */ | |
895 | ||
896 | r = update_environment(handle, "XDG_SESSION_TYPE", type); | |
897 | if (r != PAM_SUCCESS) | |
898 | return r; | |
899 | ||
900 | r = update_environment(handle, "XDG_SESSION_CLASS", class); | |
901 | if (r != PAM_SUCCESS) | |
902 | return r; | |
903 | ||
904 | r = update_environment(handle, "XDG_SESSION_DESKTOP", desktop); | |
905 | if (r != PAM_SUCCESS) | |
906 | return r; | |
907 | ||
d6baaa69 LP |
908 | r = update_environment(handle, "XDG_SEAT", seat); |
909 | if (r != PAM_SUCCESS) | |
910 | return r; | |
bbc73283 LP |
911 | |
912 | if (vtnr > 0) { | |
29d230f6 | 913 | char buf[DECIMAL_STR_MAX(vtnr)]; |
baae0358 | 914 | sprintf(buf, "%u", vtnr); |
bbc73283 | 915 | |
d6baaa69 LP |
916 | r = update_environment(handle, "XDG_VTNR", buf); |
917 | if (r != PAM_SUCCESS) | |
5a330cda | 918 | return r; |
bbc73283 LP |
919 | } |
920 | ||
77085881 LP |
921 | r = pam_set_data(handle, "systemd.existing", INT_TO_PTR(!!existing), NULL); |
922 | if (r != PAM_SUCCESS) { | |
d9608d40 | 923 | pam_syslog(handle, LOG_ERR, "Failed to install existing flag: %s", pam_strerror(handle, r)); |
5a330cda | 924 | return r; |
77085881 LP |
925 | } |
926 | ||
21c390cc | 927 | if (session_fd >= 0) { |
85c08dc0 | 928 | session_fd = fcntl(session_fd, F_DUPFD_CLOEXEC, 3); |
5a330cda ZJS |
929 | if (session_fd < 0) { |
930 | pam_syslog(handle, LOG_ERR, "Failed to dup session fd: %m"); | |
931 | return PAM_SESSION_ERR; | |
932 | } | |
933 | ||
23e096cc | 934 | r = pam_set_data(handle, "systemd.session-fd", FD_TO_PTR(session_fd), NULL); |
21c390cc | 935 | if (r != PAM_SUCCESS) { |
d9608d40 | 936 | pam_syslog(handle, LOG_ERR, "Failed to install session fd: %s", pam_strerror(handle, r)); |
03e334a1 | 937 | safe_close(session_fd); |
5a330cda | 938 | return r; |
21c390cc | 939 | } |
98a28fef | 940 | } |
8c6db833 | 941 | |
f9c1f4e1 LP |
942 | r = apply_user_record_settings(handle, ur, debug); |
943 | if (r != PAM_SUCCESS) | |
944 | return r; | |
945 | ||
355c9966 | 946 | /* Let's release the D-Bus connection, after all the session might live quite a long time, and we are |
f9c1f4e1 LP |
947 | * not going to use the bus connection in that time, so let's better close before the daemon kicks us |
948 | * off because we are not processing anything. */ | |
355c9966 | 949 | (void) pam_release_bus_connection(handle); |
ffcfcb6b | 950 | return PAM_SUCCESS; |
98a28fef | 951 | } |
8c6db833 | 952 | |
98a28fef LP |
953 | _public_ PAM_EXTERN int pam_sm_close_session( |
954 | pam_handle_t *handle, | |
955 | int flags, | |
956 | int argc, const char **argv) { | |
8c6db833 | 957 | |
5f41d1f1 | 958 | const void *existing = NULL; |
45c5fa25 | 959 | bool debug = false; |
75c8e3cf | 960 | const char *id; |
75c8e3cf | 961 | int r; |
8c6db833 | 962 | |
ffcfcb6b | 963 | assert(handle); |
75c8e3cf | 964 | |
45c5fa25 LP |
965 | if (parse_argv(handle, |
966 | argc, argv, | |
967 | NULL, | |
968 | NULL, | |
969 | NULL, | |
970 | &debug) < 0) | |
971 | return PAM_SESSION_ERR; | |
972 | ||
973 | if (debug) | |
974 | pam_syslog(handle, LOG_DEBUG, "pam-systemd shutting down"); | |
975 | ||
77085881 LP |
976 | /* Only release session if it wasn't pre-existing when we |
977 | * tried to create it */ | |
d487e2d6 | 978 | (void) pam_get_data(handle, "systemd.existing", &existing); |
77085881 | 979 | |
75c8e3cf | 980 | id = pam_getenv(handle, "XDG_SESSION_ID"); |
77085881 | 981 | if (id && !existing) { |
355c9966 LP |
982 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
983 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; | |
75c8e3cf | 984 | |
355c9966 LP |
985 | /* Before we go and close the FIFO we need to tell logind that this is a clean session |
986 | * shutdown, so that it doesn't just go and slaughter us immediately after closing the fd */ | |
75c8e3cf | 987 | |
355c9966 LP |
988 | r = pam_acquire_bus_connection(handle, &bus); |
989 | if (r != PAM_SUCCESS) | |
990 | return r; | |
75c8e3cf | 991 | |
5d990cc5 | 992 | r = bus_call_method(bus, bus_login_mgr, "ReleaseSession", &error, NULL, "s", id); |
ffcfcb6b | 993 | if (r < 0) { |
5f41d1f1 LP |
994 | pam_syslog(handle, LOG_ERR, "Failed to release session: %s", bus_error_message(&error, r)); |
995 | return PAM_SESSION_ERR; | |
75c8e3cf LP |
996 | } |
997 | } | |
998 | ||
355c9966 LP |
999 | /* Note that we are knowingly leaking the FIFO fd here. This way, logind can watch us die. If we |
1000 | * closed it here it would not have any clue when that is completed. Given that one cannot really | |
1001 | * have multiple PAM sessions open from the same process this means we will leak one FD at max. */ | |
75c8e3cf | 1002 | |
5f41d1f1 | 1003 | return PAM_SUCCESS; |
8c6db833 | 1004 | } |