]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
8c6db833 LP |
2 | /*** |
3 | This file is part of systemd. | |
4 | ||
5 | Copyright 2010 Lennart Poettering | |
8c6db833 LP |
6 | ***/ |
7 | ||
00229fe4 | 8 | #include <endian.h> |
8c6db833 LP |
9 | #include <errno.h> |
10 | #include <fcntl.h> | |
8c6db833 | 11 | #include <pwd.h> |
8c6db833 | 12 | #include <security/_pam_macros.h> |
8c6db833 LP |
13 | #include <security/pam_ext.h> |
14 | #include <security/pam_misc.h> | |
00229fe4 LP |
15 | #include <security/pam_modules.h> |
16 | #include <security/pam_modutil.h> | |
17 | #include <sys/file.h> | |
8c6db833 | 18 | |
b5efdb8a | 19 | #include "alloc-util.h" |
430f0182 | 20 | #include "audit-util.h" |
00229fe4 LP |
21 | #include "bus-common-errors.h" |
22 | #include "bus-error.h" | |
ffcfcb6b | 23 | #include "bus-util.h" |
98a28fef | 24 | #include "def.h" |
3ffd4af2 | 25 | #include "fd-util.h" |
a5c32cff | 26 | #include "fileio.h" |
f97b34a6 | 27 | #include "format-util.h" |
958b66ea | 28 | #include "hostname-util.h" |
00229fe4 LP |
29 | #include "login-util.h" |
30 | #include "macro.h" | |
6bedfcbb | 31 | #include "parse-util.h" |
df0ff127 | 32 | #include "process-util.h" |
00229fe4 LP |
33 | #include "socket-util.h" |
34 | #include "strv.h" | |
35 | #include "terminal-util.h" | |
36 | #include "util.h" | |
c9ed61e7 | 37 | #include "path-util.h" |
22f93314 | 38 | #include "cgroup-util.h" |
8c6db833 | 39 | |
baae0358 LP |
40 | static int parse_argv( |
41 | pam_handle_t *handle, | |
42 | int argc, const char **argv, | |
43 | const char **class, | |
49ebd11f | 44 | const char **type, |
baae0358 | 45 | bool *debug) { |
8c6db833 LP |
46 | |
47 | unsigned i; | |
48 | ||
49 | assert(argc >= 0); | |
50 | assert(argc == 0 || argv); | |
51 | ||
49ebd11f | 52 | for (i = 0; i < (unsigned) argc; i++) { |
fb6becb4 | 53 | if (startswith(argv[i], "class=")) { |
485507b8 MM |
54 | if (class) |
55 | *class = argv[i] + 6; | |
56 | ||
49ebd11f LP |
57 | } else if (startswith(argv[i], "type=")) { |
58 | if (type) | |
59 | *type = argv[i] + 5; | |
60 | ||
05a049cc ZJS |
61 | } else if (streq(argv[i], "debug")) { |
62 | if (debug) | |
63 | *debug = true; | |
fb6becb4 | 64 | |
05a049cc ZJS |
65 | } else if (startswith(argv[i], "debug=")) { |
66 | int k; | |
0e318cad | 67 | |
05a049cc ZJS |
68 | k = parse_boolean(argv[i] + 6); |
69 | if (k < 0) | |
70 | pam_syslog(handle, LOG_WARNING, "Failed to parse debug= argument, ignoring."); | |
71 | else if (debug) | |
0e318cad MS |
72 | *debug = k; |
73 | ||
05a049cc | 74 | } else |
fb6becb4 | 75 | pam_syslog(handle, LOG_WARNING, "Unknown parameter '%s', ignoring", argv[i]); |
49ebd11f | 76 | } |
8c6db833 | 77 | |
8c6db833 LP |
78 | return 0; |
79 | } | |
80 | ||
8c6db833 LP |
81 | static int get_user_data( |
82 | pam_handle_t *handle, | |
83 | const char **ret_username, | |
84 | struct passwd **ret_pw) { | |
85 | ||
d90b9d27 LP |
86 | const char *username = NULL; |
87 | struct passwd *pw = NULL; | |
8c6db833 LP |
88 | int r; |
89 | ||
90 | assert(handle); | |
91 | assert(ret_username); | |
92 | assert(ret_pw); | |
93 | ||
baae0358 LP |
94 | r = pam_get_user(handle, &username, NULL); |
95 | if (r != PAM_SUCCESS) { | |
96 | pam_syslog(handle, LOG_ERR, "Failed to get user name."); | |
97 | return r; | |
98 | } | |
d90b9d27 | 99 | |
baae0358 LP |
100 | if (isempty(username)) { |
101 | pam_syslog(handle, LOG_ERR, "User name not valid."); | |
102 | return PAM_AUTH_ERR; | |
8c6db833 LP |
103 | } |
104 | ||
baae0358 | 105 | pw = pam_modutil_getpwnam(handle, username); |
d90b9d27 | 106 | if (!pw) { |
8c6db833 LP |
107 | pam_syslog(handle, LOG_ERR, "Failed to get user data."); |
108 | return PAM_USER_UNKNOWN; | |
109 | } | |
110 | ||
111 | *ret_pw = pw; | |
8e24a4f8 | 112 | *ret_username = username; |
8c6db833 LP |
113 | |
114 | return PAM_SUCCESS; | |
115 | } | |
116 | ||
4d6d6518 | 117 | static int get_seat_from_display(const char *display, const char **seat, uint32_t *vtnr) { |
b92bea5d ZJS |
118 | union sockaddr_union sa = { |
119 | .un.sun_family = AF_UNIX, | |
120 | }; | |
baae0358 LP |
121 | _cleanup_free_ char *p = NULL, *tty = NULL; |
122 | _cleanup_close_ int fd = -1; | |
4d6d6518 | 123 | struct ucred ucred; |
baae0358 | 124 | int v, r; |
4d6d6518 LP |
125 | |
126 | assert(display); | |
4d6d6518 LP |
127 | assert(vtnr); |
128 | ||
129 | /* We deduce the X11 socket from the display name, then use | |
130 | * SO_PEERCRED to determine the X11 server process, ask for | |
131 | * the controlling tty of that and if it's a VC then we know | |
132 | * the seat and the virtual terminal. Sounds ugly, is only | |
133 | * semi-ugly. */ | |
134 | ||
135 | r = socket_from_display(display, &p); | |
136 | if (r < 0) | |
137 | return r; | |
b92bea5d | 138 | strncpy(sa.un.sun_path, p, sizeof(sa.un.sun_path)-1); |
4d6d6518 LP |
139 | |
140 | fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0); | |
b92bea5d | 141 | if (fd < 0) |
4d6d6518 | 142 | return -errno; |
4d6d6518 | 143 | |
fc2fffe7 | 144 | if (connect(fd, &sa.sa, SOCKADDR_UN_LEN(sa.un)) < 0) |
4d6d6518 | 145 | return -errno; |
4d6d6518 | 146 | |
eff05270 | 147 | r = getpeercred(fd, &ucred); |
4d6d6518 | 148 | if (r < 0) |
eff05270 | 149 | return r; |
4d6d6518 LP |
150 | |
151 | r = get_ctty(ucred.pid, NULL, &tty); | |
152 | if (r < 0) | |
153 | return r; | |
154 | ||
155 | v = vtnr_from_tty(tty); | |
4d6d6518 LP |
156 | if (v < 0) |
157 | return v; | |
158 | else if (v == 0) | |
159 | return -ENOENT; | |
160 | ||
fc7985ed LP |
161 | if (seat) |
162 | *seat = "seat0"; | |
4d6d6518 LP |
163 | *vtnr = (uint32_t) v; |
164 | ||
165 | return 0; | |
166 | } | |
167 | ||
8b255ecd KS |
168 | static int export_legacy_dbus_address( |
169 | pam_handle_t *handle, | |
170 | uid_t uid, | |
171 | const char *runtime) { | |
172 | ||
8b255ecd | 173 | _cleanup_free_ char *s = NULL; |
3df49c28 DH |
174 | int r = PAM_BUF_ERR; |
175 | ||
222953e8 DR |
176 | /* FIXME: We *really* should move the access() check into the |
177 | * daemons that spawn dbus-daemon, instead of forcing | |
178 | * DBUS_SESSION_BUS_ADDRESS= here. */ | |
179 | ||
605405c6 | 180 | s = strjoin(runtime, "/bus"); |
222953e8 DR |
181 | if (!s) |
182 | goto error; | |
183 | ||
184 | if (access(s, F_OK) < 0) | |
185 | return PAM_SUCCESS; | |
186 | ||
187 | s = mfree(s); | |
15ca0a42 | 188 | if (asprintf(&s, DEFAULT_USER_BUS_ADDRESS_FMT, runtime) < 0) |
222953e8 | 189 | goto error; |
8b255ecd KS |
190 | |
191 | r = pam_misc_setenv(handle, "DBUS_SESSION_BUS_ADDRESS", s, 0); | |
3df49c28 DH |
192 | if (r != PAM_SUCCESS) |
193 | goto error; | |
1b09f548 | 194 | |
8b255ecd | 195 | return PAM_SUCCESS; |
3df49c28 DH |
196 | |
197 | error: | |
198 | pam_syslog(handle, LOG_ERR, "Failed to set bus variable."); | |
199 | return r; | |
8b255ecd KS |
200 | } |
201 | ||
22f93314 JS |
202 | static int append_session_memory_max(pam_handle_t *handle, sd_bus_message *m, const char *limit) { |
203 | uint64_t val; | |
204 | int r; | |
205 | ||
206 | if (isempty(limit)) | |
207 | return 0; | |
208 | ||
209 | if (streq(limit, "infinity")) { | |
210 | r = sd_bus_message_append(m, "(sv)", "MemoryMax", "t", (uint64_t)-1); | |
211 | if (r < 0) { | |
212 | pam_syslog(handle, LOG_ERR, "Failed to append to bus message: %s", strerror(-r)); | |
213 | return r; | |
214 | } | |
215 | } else { | |
216 | r = parse_percent(limit); | |
217 | if (r >= 0) { | |
218 | r = sd_bus_message_append(m, "(sv)", "MemoryMaxScale", "u", (uint32_t) (((uint64_t) UINT32_MAX * r) / 100U)); | |
219 | if (r < 0) { | |
220 | pam_syslog(handle, LOG_ERR, "Failed to append to bus message: %s", strerror(-r)); | |
221 | return r; | |
222 | } | |
223 | } else { | |
224 | r = parse_size(limit, 1024, &val); | |
225 | if (r >= 0) { | |
226 | r = sd_bus_message_append(m, "(sv)", "MemoryMax", "t", val); | |
227 | if (r < 0) { | |
228 | pam_syslog(handle, LOG_ERR, "Failed to append to bus message: %s", strerror(-r)); | |
229 | return r; | |
230 | } | |
231 | } else | |
232 | pam_syslog(handle, LOG_WARNING, "Failed to parse systemd.limit: %s, ignoring.", limit); | |
233 | } | |
234 | } | |
235 | ||
236 | return 0; | |
237 | } | |
238 | ||
239 | static int append_session_tasks_max(pam_handle_t *handle, sd_bus_message *m, const char *limit) | |
240 | { | |
241 | uint64_t val; | |
242 | int r; | |
243 | ||
244 | /* No need to parse "infinity" here, it will be set unconditionally later in manager_start_scope() */ | |
245 | if (isempty(limit) || streq(limit, "infinity")) | |
246 | return 0; | |
247 | ||
248 | r = safe_atou64(limit, &val); | |
249 | if (r >= 0) { | |
250 | r = sd_bus_message_append(m, "(sv)", "TasksMax", "t", val); | |
251 | if (r < 0) { | |
252 | pam_syslog(handle, LOG_ERR, "Failed to append to bus message: %s", strerror(-r)); | |
253 | return r; | |
254 | } | |
255 | } else | |
256 | pam_syslog(handle, LOG_WARNING, "Failed to parse systemd.limit: %s, ignoring.", limit); | |
257 | ||
258 | return 0; | |
259 | } | |
260 | ||
261 | static int append_session_cg_weight(pam_handle_t *handle, sd_bus_message *m, const char *limit, const char *field) { | |
262 | uint64_t val; | |
263 | int r; | |
264 | ||
265 | if (!isempty(limit)) { | |
266 | r = cg_weight_parse(limit, &val); | |
267 | if (r >= 0) { | |
268 | r = sd_bus_message_append(m, "(sv)", field, "t", val); | |
269 | if (r < 0) { | |
270 | pam_syslog(handle, LOG_ERR, "Failed to append to bus message: %s", strerror(-r)); | |
271 | return r; | |
272 | } | |
273 | } else if (streq(field, "CPUWeight")) | |
274 | pam_syslog(handle, LOG_WARNING, "Failed to parse systemd.cpu_weight: %s, ignoring.", limit); | |
275 | else | |
276 | pam_syslog(handle, LOG_WARNING, "Failed to parse systemd.io_weight: %s, ignoring.", limit); | |
277 | } | |
278 | ||
279 | return 0; | |
280 | } | |
281 | ||
98a28fef | 282 | _public_ PAM_EXTERN int pam_sm_open_session( |
8c6db833 LP |
283 | pam_handle_t *handle, |
284 | int flags, | |
285 | int argc, const char **argv) { | |
286 | ||
4afd3348 | 287 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
22f93314 | 288 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL; |
d1529c9e LP |
289 | const char |
290 | *username, *id, *object_path, *runtime_path, | |
291 | *service = NULL, | |
292 | *tty = NULL, *display = NULL, | |
293 | *remote_user = NULL, *remote_host = NULL, | |
294 | *seat = NULL, | |
295 | *type = NULL, *class = NULL, | |
22f93314 JS |
296 | *class_pam = NULL, *type_pam = NULL, *cvtnr = NULL, *desktop = NULL, |
297 | *memory_max = NULL, *tasks_max = NULL, *cpu_weight = NULL, *io_weight = NULL; | |
4afd3348 | 298 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
d1529c9e | 299 | int session_fd = -1, existing, r; |
d1529c9e LP |
300 | bool debug = false, remote; |
301 | struct passwd *pw; | |
baae0358 LP |
302 | uint32_t vtnr = 0; |
303 | uid_t original_uid; | |
8c6db833 | 304 | |
ffcfcb6b | 305 | assert(handle); |
98a28fef | 306 | |
79d860fe MP |
307 | /* Make this a NOP on non-logind systems */ |
308 | if (!logind_running()) | |
8c6db833 LP |
309 | return PAM_SUCCESS; |
310 | ||
e9fbc77c LP |
311 | if (parse_argv(handle, |
312 | argc, argv, | |
fb6becb4 | 313 | &class_pam, |
49ebd11f | 314 | &type_pam, |
5a330cda ZJS |
315 | &debug) < 0) |
316 | return PAM_SESSION_ERR; | |
74fe1fe3 | 317 | |
baae0358 | 318 | if (debug) |
3831838a | 319 | pam_syslog(handle, LOG_DEBUG, "pam-systemd initializing"); |
baae0358 | 320 | |
98a28fef | 321 | r = get_user_data(handle, &username, &pw); |
5a330cda ZJS |
322 | if (r != PAM_SUCCESS) { |
323 | pam_syslog(handle, LOG_ERR, "Failed to get user data."); | |
324 | return r; | |
325 | } | |
8c6db833 | 326 | |
30b2c336 LP |
327 | /* Make sure we don't enter a loop by talking to |
328 | * systemd-logind when it is actually waiting for the | |
329 | * background to finish start-up. If the service is | |
5c390a4a | 330 | * "systemd-user" we simply set XDG_RUNTIME_DIR and |
30b2c336 LP |
331 | * leave. */ |
332 | ||
333 | pam_get_item(handle, PAM_SERVICE, (const void**) &service); | |
5c390a4a | 334 | if (streq_ptr(service, "systemd-user")) { |
f9e4283d | 335 | _cleanup_free_ char *rt = NULL; |
30b2c336 | 336 | |
f9e4283d | 337 | if (asprintf(&rt, "/run/user/"UID_FMT, pw->pw_uid) < 0) |
ffcfcb6b | 338 | return PAM_BUF_ERR; |
30b2c336 | 339 | |
f9e4283d DH |
340 | r = pam_misc_setenv(handle, "XDG_RUNTIME_DIR", rt, 0); |
341 | if (r != PAM_SUCCESS) { | |
342 | pam_syslog(handle, LOG_ERR, "Failed to set runtime dir."); | |
343 | return r; | |
30b2c336 LP |
344 | } |
345 | ||
f9e4283d DH |
346 | r = export_legacy_dbus_address(handle, pw->pw_uid, rt); |
347 | if (r != PAM_SUCCESS) | |
348 | return r; | |
349 | ||
ffcfcb6b | 350 | return PAM_SUCCESS; |
8c6db833 LP |
351 | } |
352 | ||
ffcfcb6b | 353 | /* Otherwise, we ask logind to create a session for us */ |
8c6db833 | 354 | |
98a28fef LP |
355 | pam_get_item(handle, PAM_XDISPLAY, (const void**) &display); |
356 | pam_get_item(handle, PAM_TTY, (const void**) &tty); | |
357 | pam_get_item(handle, PAM_RUSER, (const void**) &remote_user); | |
358 | pam_get_item(handle, PAM_RHOST, (const void**) &remote_host); | |
a8573ccc | 359 | |
bbc73283 | 360 | seat = pam_getenv(handle, "XDG_SEAT"); |
a8573ccc LP |
361 | if (isempty(seat)) |
362 | seat = getenv("XDG_SEAT"); | |
363 | ||
bbc73283 | 364 | cvtnr = pam_getenv(handle, "XDG_VTNR"); |
a8573ccc LP |
365 | if (isempty(cvtnr)) |
366 | cvtnr = getenv("XDG_VTNR"); | |
98a28fef | 367 | |
49ebd11f LP |
368 | type = pam_getenv(handle, "XDG_SESSION_TYPE"); |
369 | if (isempty(type)) | |
370 | type = getenv("XDG_SESSION_TYPE"); | |
371 | if (isempty(type)) | |
372 | type = type_pam; | |
373 | ||
374 | class = pam_getenv(handle, "XDG_SESSION_CLASS"); | |
375 | if (isempty(class)) | |
376 | class = getenv("XDG_SESSION_CLASS"); | |
377 | if (isempty(class)) | |
378 | class = class_pam; | |
379 | ||
a4cd87e9 LP |
380 | desktop = pam_getenv(handle, "XDG_SESSION_DESKTOP"); |
381 | if (isempty(desktop)) | |
382 | desktop = getenv("XDG_SESSION_DESKTOP"); | |
383 | ||
ed18b08b | 384 | tty = strempty(tty); |
98a28fef | 385 | |
ee8545b0 | 386 | if (strchr(tty, ':')) { |
e2acb67b LP |
387 | /* A tty with a colon is usually an X11 display, |
388 | * placed there to show up in utmp. We rearrange | |
389 | * things and don't pretend that an X display was a | |
390 | * tty. */ | |
ee8545b0 LP |
391 | |
392 | if (isempty(display)) | |
393 | display = tty; | |
a4cd87e9 | 394 | tty = NULL; |
1a4459d6 | 395 | } else if (streq(tty, "cron")) { |
0ad1271f LP |
396 | /* cron has been setting PAM_TTY to "cron" for a very |
397 | * long time and it probably shouldn't stop doing that | |
398 | * for compatibility reasons. */ | |
0ad1271f | 399 | type = "unspecified"; |
49ebd11f | 400 | class = "background"; |
a4cd87e9 | 401 | tty = NULL; |
0ad1271f LP |
402 | } else if (streq(tty, "ssh")) { |
403 | /* ssh has been setting PAM_TTY to "ssh" for a very | |
404 | * long time and probably shouldn't stop doing that | |
405 | * for compatibility reasons. */ | |
0ad1271f | 406 | type ="tty"; |
49ebd11f | 407 | class = "user"; |
a4cd87e9 | 408 | tty = NULL; |
c9ed61e7 LP |
409 | } else |
410 | /* Chop off leading /dev prefix that some clients specify, but others do not. */ | |
411 | tty = skip_dev_prefix(tty); | |
ee8545b0 | 412 | |
8e7705e5 | 413 | /* If this fails vtnr will be 0, that's intended */ |
4d6d6518 | 414 | if (!isempty(cvtnr)) |
2ae4842b | 415 | (void) safe_atou32(cvtnr, &vtnr); |
4d6d6518 | 416 | |
92bd5ff3 | 417 | if (!isempty(display) && !vtnr) { |
fc7985ed | 418 | if (isempty(seat)) |
6ef25fb6 | 419 | get_seat_from_display(display, &seat, &vtnr); |
fc7985ed | 420 | else if (streq(seat, "seat0")) |
6ef25fb6 | 421 | get_seat_from_display(display, NULL, &vtnr); |
fc7985ed | 422 | } |
4d6d6518 | 423 | |
49ebd11f | 424 | if (seat && !streq(seat, "seat0") && vtnr != 0) { |
1fa2f38f | 425 | pam_syslog(handle, LOG_DEBUG, "Ignoring vtnr %"PRIu32" for %s which is not seat0", vtnr, seat); |
d7353ef6 MM |
426 | vtnr = 0; |
427 | } | |
428 | ||
49ebd11f | 429 | if (isempty(type)) |
0ad1271f | 430 | type = !isempty(display) ? "x11" : |
49ebd11f | 431 | !isempty(tty) ? "tty" : "unspecified"; |
98a28fef | 432 | |
55efac6c | 433 | if (isempty(class)) |
e2acb67b | 434 | class = streq(type, "unspecified") ? "background" : "user"; |
55efac6c | 435 | |
fecc80c1 | 436 | remote = !isempty(remote_host) && !is_localhost(remote_host); |
98a28fef | 437 | |
22f93314 JS |
438 | (void) pam_get_data(handle, "systemd.memory_max", (const void **)&memory_max); |
439 | (void) pam_get_data(handle, "systemd.tasks_max", (const void **)&tasks_max); | |
440 | (void) pam_get_data(handle, "systemd.cpu_weight", (const void **)&cpu_weight); | |
441 | (void) pam_get_data(handle, "systemd.io_weight", (const void **)&io_weight); | |
442 | ||
4d49b48c | 443 | /* Talk to logind over the message bus */ |
5a330cda | 444 | |
ffcfcb6b ZJS |
445 | r = sd_bus_open_system(&bus); |
446 | if (r < 0) { | |
447 | pam_syslog(handle, LOG_ERR, "Failed to connect to system bus: %s", strerror(-r)); | |
448 | return PAM_SESSION_ERR; | |
cc377381 LP |
449 | } |
450 | ||
22f93314 | 451 | if (debug) { |
ce959314 | 452 | pam_syslog(handle, LOG_DEBUG, "Asking logind to create session: " |
1fa2f38f | 453 | "uid="UID_FMT" pid="PID_FMT" service=%s type=%s class=%s desktop=%s seat=%s vtnr=%"PRIu32" tty=%s display=%s remote=%s remote_user=%s remote_host=%s", |
df0ff127 | 454 | pw->pw_uid, getpid_cached(), |
baae0358 | 455 | strempty(service), |
cda7ecb0 | 456 | type, class, strempty(desktop), |
a4cd87e9 | 457 | strempty(seat), vtnr, strempty(tty), strempty(display), |
baae0358 | 458 | yes_no(remote), strempty(remote_user), strempty(remote_host)); |
22f93314 JS |
459 | pam_syslog(handle, LOG_DEBUG, "Session limits: " |
460 | "memory_max=%s tasks_max=%s cpu_weight=%s io_weight=%s", | |
461 | strna(memory_max), strna(tasks_max), strna(cpu_weight), strna(io_weight)); | |
462 | } | |
463 | ||
464 | r = sd_bus_message_new_method_call( | |
465 | bus, | |
466 | &m, | |
467 | "org.freedesktop.login1", | |
468 | "/org/freedesktop/login1", | |
469 | "org.freedesktop.login1.Manager", | |
470 | "CreateSession"); | |
471 | if (r < 0) { | |
472 | pam_syslog(handle, LOG_ERR, "Failed to create CreateSession method call: %s", strerror(-r)); | |
473 | return PAM_SESSION_ERR; | |
474 | } | |
475 | ||
476 | r = sd_bus_message_append(m, "uusssssussbss", | |
477 | (uint32_t) pw->pw_uid, | |
478 | (uint32_t) getpid_cached(), | |
479 | service, | |
480 | type, | |
481 | class, | |
482 | desktop, | |
483 | seat, | |
484 | vtnr, | |
485 | tty, | |
486 | display, | |
487 | remote, | |
488 | remote_user, | |
489 | remote_host); | |
490 | if (r < 0) { | |
491 | pam_syslog(handle, LOG_ERR, "Failed to append to bus message: %s", strerror(-r)); | |
492 | return PAM_SESSION_ERR; | |
493 | } | |
494 | ||
495 | r = sd_bus_message_open_container(m, 'a', "(sv)"); | |
496 | if (r < 0) { | |
497 | pam_syslog(handle, LOG_ERR, "Failed to open message container: %s", strerror(-r)); | |
498 | return PAM_SYSTEM_ERR; | |
499 | } | |
500 | ||
501 | r = append_session_memory_max(handle, m, memory_max); | |
502 | if (r < 0) | |
503 | return PAM_SESSION_ERR; | |
504 | ||
505 | r = append_session_tasks_max(handle, m, tasks_max); | |
506 | if (r < 0) | |
507 | return PAM_SESSION_ERR; | |
508 | ||
509 | r = append_session_cg_weight(handle, m, cpu_weight, "CPUWeight"); | |
510 | if (r < 0) | |
511 | return PAM_SESSION_ERR; | |
512 | ||
513 | r = append_session_cg_weight(handle, m, io_weight, "IOWeight"); | |
514 | if (r < 0) | |
515 | return PAM_SESSION_ERR; | |
516 | ||
517 | r = sd_bus_message_close_container(m); | |
518 | if (r < 0) { | |
519 | pam_syslog(handle, LOG_ERR, "Failed to close message container: %s", strerror(-r)); | |
520 | return PAM_SYSTEM_ERR; | |
521 | } | |
ce959314 | 522 | |
22f93314 | 523 | r = sd_bus_call(bus, m, 0, &error, &reply); |
ffcfcb6b | 524 | if (r < 0) { |
b80120c4 DH |
525 | if (sd_bus_error_has_name(&error, BUS_ERROR_SESSION_BUSY)) { |
526 | pam_syslog(handle, LOG_DEBUG, "Cannot create session: %s", bus_error_message(&error, r)); | |
527 | return PAM_SUCCESS; | |
528 | } else { | |
529 | pam_syslog(handle, LOG_ERR, "Failed to create session: %s", bus_error_message(&error, r)); | |
530 | return PAM_SYSTEM_ERR; | |
531 | } | |
98a28fef | 532 | } |
74fe1fe3 | 533 | |
ffcfcb6b | 534 | r = sd_bus_message_read(reply, |
baae0358 | 535 | "soshusub", |
ffcfcb6b ZJS |
536 | &id, |
537 | &object_path, | |
538 | &runtime_path, | |
539 | &session_fd, | |
baae0358 | 540 | &original_uid, |
ffcfcb6b ZJS |
541 | &seat, |
542 | &vtnr, | |
543 | &existing); | |
544 | if (r < 0) { | |
545 | pam_syslog(handle, LOG_ERR, "Failed to parse message: %s", strerror(-r)); | |
5a330cda | 546 | return PAM_SESSION_ERR; |
98a28fef | 547 | } |
74fe1fe3 | 548 | |
ce959314 MS |
549 | if (debug) |
550 | pam_syslog(handle, LOG_DEBUG, "Reply from logind: " | |
baae0358 LP |
551 | "id=%s object_path=%s runtime_path=%s session_fd=%d seat=%s vtnr=%u original_uid=%u", |
552 | id, object_path, runtime_path, session_fd, seat, vtnr, original_uid); | |
ce959314 | 553 | |
98a28fef LP |
554 | r = pam_misc_setenv(handle, "XDG_SESSION_ID", id, 0); |
555 | if (r != PAM_SUCCESS) { | |
556 | pam_syslog(handle, LOG_ERR, "Failed to set session id."); | |
5a330cda | 557 | return r; |
8c6db833 LP |
558 | } |
559 | ||
baae0358 LP |
560 | if (original_uid == pw->pw_uid) { |
561 | /* Don't set $XDG_RUNTIME_DIR if the user we now | |
562 | * authenticated for does not match the original user | |
563 | * of the session. We do this in order not to result | |
564 | * in privileged apps clobbering the runtime directory | |
565 | * unnecessarily. */ | |
566 | ||
567 | r = pam_misc_setenv(handle, "XDG_RUNTIME_DIR", runtime_path, 0); | |
568 | if (r != PAM_SUCCESS) { | |
569 | pam_syslog(handle, LOG_ERR, "Failed to set runtime dir."); | |
570 | return r; | |
571 | } | |
8b255ecd KS |
572 | |
573 | r = export_legacy_dbus_address(handle, pw->pw_uid, runtime_path); | |
574 | if (r != PAM_SUCCESS) | |
575 | return r; | |
98a28fef | 576 | } |
8c6db833 | 577 | |
bbc73283 LP |
578 | if (!isempty(seat)) { |
579 | r = pam_misc_setenv(handle, "XDG_SEAT", seat, 0); | |
580 | if (r != PAM_SUCCESS) { | |
581 | pam_syslog(handle, LOG_ERR, "Failed to set seat."); | |
5a330cda | 582 | return r; |
bbc73283 LP |
583 | } |
584 | } | |
585 | ||
586 | if (vtnr > 0) { | |
29d230f6 | 587 | char buf[DECIMAL_STR_MAX(vtnr)]; |
baae0358 | 588 | sprintf(buf, "%u", vtnr); |
bbc73283 LP |
589 | |
590 | r = pam_misc_setenv(handle, "XDG_VTNR", buf, 0); | |
591 | if (r != PAM_SUCCESS) { | |
592 | pam_syslog(handle, LOG_ERR, "Failed to set virtual terminal number."); | |
5a330cda | 593 | return r; |
bbc73283 LP |
594 | } |
595 | } | |
596 | ||
77085881 LP |
597 | r = pam_set_data(handle, "systemd.existing", INT_TO_PTR(!!existing), NULL); |
598 | if (r != PAM_SUCCESS) { | |
599 | pam_syslog(handle, LOG_ERR, "Failed to install existing flag."); | |
5a330cda | 600 | return r; |
77085881 LP |
601 | } |
602 | ||
21c390cc | 603 | if (session_fd >= 0) { |
85c08dc0 | 604 | session_fd = fcntl(session_fd, F_DUPFD_CLOEXEC, 3); |
5a330cda ZJS |
605 | if (session_fd < 0) { |
606 | pam_syslog(handle, LOG_ERR, "Failed to dup session fd: %m"); | |
607 | return PAM_SESSION_ERR; | |
608 | } | |
609 | ||
23e096cc | 610 | r = pam_set_data(handle, "systemd.session-fd", FD_TO_PTR(session_fd), NULL); |
21c390cc LP |
611 | if (r != PAM_SUCCESS) { |
612 | pam_syslog(handle, LOG_ERR, "Failed to install session fd."); | |
03e334a1 | 613 | safe_close(session_fd); |
5a330cda | 614 | return r; |
21c390cc | 615 | } |
98a28fef | 616 | } |
8c6db833 | 617 | |
ffcfcb6b | 618 | return PAM_SUCCESS; |
98a28fef | 619 | } |
8c6db833 | 620 | |
98a28fef LP |
621 | _public_ PAM_EXTERN int pam_sm_close_session( |
622 | pam_handle_t *handle, | |
623 | int flags, | |
624 | int argc, const char **argv) { | |
8c6db833 | 625 | |
4afd3348 LP |
626 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
627 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; | |
5f41d1f1 | 628 | const void *existing = NULL; |
75c8e3cf | 629 | const char *id; |
75c8e3cf | 630 | int r; |
8c6db833 | 631 | |
ffcfcb6b | 632 | assert(handle); |
75c8e3cf | 633 | |
77085881 LP |
634 | /* Only release session if it wasn't pre-existing when we |
635 | * tried to create it */ | |
636 | pam_get_data(handle, "systemd.existing", &existing); | |
637 | ||
75c8e3cf | 638 | id = pam_getenv(handle, "XDG_SESSION_ID"); |
77085881 | 639 | if (id && !existing) { |
75c8e3cf LP |
640 | |
641 | /* Before we go and close the FIFO we need to tell | |
642 | * logind that this is a clean session shutdown, so | |
643 | * that it doesn't just go and slaughter us | |
644 | * immediately after closing the fd */ | |
645 | ||
ffcfcb6b ZJS |
646 | r = sd_bus_open_system(&bus); |
647 | if (r < 0) { | |
5f41d1f1 LP |
648 | pam_syslog(handle, LOG_ERR, "Failed to connect to system bus: %s", strerror(-r)); |
649 | return PAM_SESSION_ERR; | |
75c8e3cf LP |
650 | } |
651 | ||
ffcfcb6b ZJS |
652 | r = sd_bus_call_method(bus, |
653 | "org.freedesktop.login1", | |
654 | "/org/freedesktop/login1", | |
655 | "org.freedesktop.login1.Manager", | |
656 | "ReleaseSession", | |
657 | &error, | |
658 | NULL, | |
659 | "s", | |
660 | id); | |
661 | if (r < 0) { | |
5f41d1f1 LP |
662 | pam_syslog(handle, LOG_ERR, "Failed to release session: %s", bus_error_message(&error, r)); |
663 | return PAM_SESSION_ERR; | |
75c8e3cf LP |
664 | } |
665 | } | |
666 | ||
5f41d1f1 LP |
667 | /* Note that we are knowingly leaking the FIFO fd here. This |
668 | * way, logind can watch us die. If we closed it here it would | |
669 | * not have any clue when that is completed. Given that one | |
670 | * cannot really have multiple PAM sessions open from the same | |
671 | * process this means we will leak one FD at max. */ | |
75c8e3cf | 672 | |
5f41d1f1 | 673 | return PAM_SUCCESS; |
8c6db833 | 674 | } |