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