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