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