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