| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <endian.h> |
| 4 |
#include <fcntl.h> |
| 5 |
#include <pwd.h> |
| 6 |
#include <sys/file.h> |
| 7 |
#include <sys/stat.h> |
| 8 |
#include <sys/sysmacros.h> |
| 9 |
#include <unistd.h> |
| 10 |
|
| 11 |
#include "sd-bus.h" |
| 12 |
#include "sd-daemon.h" |
| 13 |
#include "sd-varlink.h" |
| 14 |
|
| 15 |
#include "alloc-util.h" |
| 16 |
#include "bus-common-errors.h" |
| 17 |
#include "bus-error.h" |
| 18 |
#include "bus-internal.h" |
| 19 |
#include "bus-locator.h" |
| 20 |
#include "capability-list.h" |
| 21 |
#include "capability-util.h" |
| 22 |
#include "cgroup-setup.h" |
| 23 |
#include "chase.h" |
| 24 |
#include "creds-util.h" |
| 25 |
#include "devnum-util.h" |
| 26 |
#include "errno-util.h" |
| 27 |
#include "extract-word.h" |
| 28 |
#include "fd-util.h" |
| 29 |
#include "format-util.h" |
| 30 |
#include "fs-util.h" |
| 31 |
#include "hostname-util.h" |
| 32 |
#include "io-util.h" |
| 33 |
#include "json-util.h" |
| 34 |
#include "locale-util.h" |
| 35 |
#include "login-util.h" |
| 36 |
#include "osc-context.h" |
| 37 |
#include "pam-util.h" |
| 38 |
#include "parse-util.h" |
| 39 |
#include "path-util.h" |
| 40 |
#include "percent-util.h" |
| 41 |
#include "pidfd-util.h" |
| 42 |
#include "pidref.h" |
| 43 |
#include "process-util.h" |
| 44 |
#include "rlimit-util.h" |
| 45 |
#include "socket-util.h" |
| 46 |
#include "stdio-util.h" |
| 47 |
#include "string-util.h" |
| 48 |
#include "strv.h" |
| 49 |
#include "terminal-util.h" |
| 50 |
#include "time-util.h" |
| 51 |
#include "tmpfile-util.h" |
| 52 |
#include "user-util.h" |
| 53 |
#include "userdb.h" |
| 54 |
|
| 55 |
#define LOGIN_SLOW_BUS_CALL_TIMEOUT_USEC (2*USEC_PER_MINUTE) |
| 56 |
|
| 57 |
static int parse_caps( |
| 58 |
pam_handle_t *pamh, |
| 59 |
const char *value, |
| 60 |
uint64_t *caps) { |
| 61 |
|
| 62 |
bool subtract; |
| 63 |
int r; |
| 64 |
|
| 65 |
assert(pamh); |
| 66 |
assert(value); |
| 67 |
|
| 68 |
if (value[0] == '~') { |
| 69 |
subtract = true; |
| 70 |
value++; |
| 71 |
} else |
| 72 |
subtract = false; |
| 73 |
|
| 74 |
for (;;) { |
| 75 |
_cleanup_free_ char *s = NULL; |
| 76 |
uint64_t b, m; |
| 77 |
int c; |
| 78 |
|
| 79 |
/* We can't use spaces as separators here, as PAM's simplistic argument parser doesn't allow |
| 80 |
* spaces inside of arguments. We use commas instead (which is similar to cap_from_text(), |
| 81 |
* which also uses commas). */ |
| 82 |
r = extract_first_word(&value, &s, ",", EXTRACT_DONT_COALESCE_SEPARATORS); |
| 83 |
if (r < 0) |
| 84 |
return r; |
| 85 |
if (r == 0) |
| 86 |
break; |
| 87 |
|
| 88 |
c = capability_from_name(s); |
| 89 |
if (c < 0) { |
| 90 |
sym_pam_syslog(pamh, LOG_WARNING, "Unknown capability, ignoring: %s", s); |
| 91 |
continue; |
| 92 |
} |
| 93 |
|
| 94 |
m = UINT64_C(1) << c; |
| 95 |
|
| 96 |
if (!caps) |
| 97 |
continue; |
| 98 |
|
| 99 |
if (*caps == CAP_MASK_UNSET) |
| 100 |
b = subtract ? all_capabilities() : 0; |
| 101 |
else |
| 102 |
b = *caps; |
| 103 |
|
| 104 |
if (subtract) |
| 105 |
*caps = b & ~m; |
| 106 |
else |
| 107 |
*caps = b | m; |
| 108 |
} |
| 109 |
|
| 110 |
return 0; |
| 111 |
} |
| 112 |
|
| 113 |
static int parse_argv( |
| 114 |
pam_handle_t *pamh, |
| 115 |
int argc, const char **argv, |
| 116 |
const char **class, |
| 117 |
const char **type, |
| 118 |
const char **desktop, |
| 119 |
const char **area, |
| 120 |
bool *debug, |
| 121 |
uint64_t *default_capability_bounding_set, |
| 122 |
uint64_t *default_capability_ambient_set) { |
| 123 |
|
| 124 |
int r; |
| 125 |
|
| 126 |
assert(pamh); |
| 127 |
assert(argc >= 0); |
| 128 |
assert(argc == 0 || argv); |
| 129 |
|
| 130 |
for (int i = 0; i < argc; i++) { |
| 131 |
const char *p; |
| 132 |
|
| 133 |
if ((p = startswith(argv[i], "class="))) { |
| 134 |
if (class) |
| 135 |
*class = p; |
| 136 |
|
| 137 |
} else if ((p = startswith(argv[i], "type="))) { |
| 138 |
if (type) |
| 139 |
*type = p; |
| 140 |
|
| 141 |
} else if ((p = startswith(argv[i], "desktop="))) { |
| 142 |
if (desktop) |
| 143 |
*desktop = p; |
| 144 |
|
| 145 |
} else if ((p = startswith(argv[i], "area="))) { |
| 146 |
|
| 147 |
if (!isempty(p) && !filename_is_valid(p)) |
| 148 |
sym_pam_syslog(pamh, LOG_WARNING, "Area name specified among PAM module parameters is not valid, ignoring: %s", p); |
| 149 |
else if (area) |
| 150 |
*area = p; |
| 151 |
|
| 152 |
} else if (streq(argv[i], "debug")) { |
| 153 |
if (debug) |
| 154 |
*debug = true; |
| 155 |
|
| 156 |
} else if ((p = startswith(argv[i], "debug="))) { |
| 157 |
r = parse_boolean(p); |
| 158 |
if (r < 0) |
| 159 |
sym_pam_syslog(pamh, LOG_WARNING, "Failed to parse debug= argument, ignoring: %s", p); |
| 160 |
else if (debug) |
| 161 |
*debug = r; |
| 162 |
|
| 163 |
} else if ((p = startswith(argv[i], "default-capability-bounding-set="))) { |
| 164 |
r = parse_caps(pamh, p, default_capability_bounding_set); |
| 165 |
if (r < 0) |
| 166 |
sym_pam_syslog(pamh, LOG_WARNING, "Failed to parse default-capability-bounding-set= argument, ignoring: %s", p); |
| 167 |
|
| 168 |
} else if ((p = startswith(argv[i], "default-capability-ambient-set="))) { |
| 169 |
r = parse_caps(pamh, p, default_capability_ambient_set); |
| 170 |
if (r < 0) |
| 171 |
sym_pam_syslog(pamh, LOG_WARNING, "Failed to parse default-capability-ambient-set= argument, ignoring: %s", p); |
| 172 |
|
| 173 |
} else |
| 174 |
sym_pam_syslog(pamh, LOG_WARNING, "Unknown parameter '%s', ignoring.", argv[i]); |
| 175 |
} |
| 176 |
|
| 177 |
return 0; |
| 178 |
} |
| 179 |
|
| 180 |
static int acquire_user_record(pam_handle_t *pamh, UserRecord **ret_record) { |
| 181 |
int r; |
| 182 |
|
| 183 |
assert(pamh); |
| 184 |
|
| 185 |
const char *username = NULL; |
| 186 |
r = sym_pam_get_user(pamh, &username, NULL); |
| 187 |
if (r != PAM_SUCCESS) |
| 188 |
return pam_syslog_pam_error(pamh, LOG_ERR, r, "Failed to get user name: @PAMERR@"); |
| 189 |
if (isempty(username)) |
| 190 |
return pam_syslog_pam_error(pamh, LOG_ERR, PAM_SERVICE_ERR, "User name not valid."); |
| 191 |
|
| 192 |
/* If pam_systemd_homed (or some other module) already acquired the user record we can reuse it |
| 193 |
* here. */ |
| 194 |
_cleanup_free_ char *field = strjoin("systemd-user-record-", username); |
| 195 |
if (!field) |
| 196 |
return pam_log_oom(pamh); |
| 197 |
|
| 198 |
_cleanup_(user_record_unrefp) UserRecord *ur = NULL; |
| 199 |
const char *json = NULL; |
| 200 |
r = sym_pam_get_data(pamh, field, (const void**) &json); |
| 201 |
if (!IN_SET(r, PAM_SUCCESS, PAM_NO_MODULE_DATA)) |
| 202 |
return pam_syslog_pam_error(pamh, LOG_ERR, r, "Failed to get PAM user record data: @PAMERR@"); |
| 203 |
if (r == PAM_SUCCESS && json) { |
| 204 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 205 |
|
| 206 |
/* Parse cached record */ |
| 207 |
r = sd_json_parse(json, SD_JSON_PARSE_MUST_BE_OBJECT|SD_JSON_PARSE_SENSITIVE, &v, /* reterr_line= */ NULL, /* reterr_column= */ NULL); |
| 208 |
if (r < 0) |
| 209 |
return pam_syslog_errno(pamh, LOG_ERR, r, "Failed to parse JSON user record: %m"); |
| 210 |
|
| 211 |
ur = user_record_new(); |
| 212 |
if (!ur) |
| 213 |
return pam_log_oom(pamh); |
| 214 |
|
| 215 |
r = user_record_load(ur, v, USER_RECORD_LOAD_REFUSE_SECRET|USER_RECORD_PERMISSIVE); |
| 216 |
if (r < 0) |
| 217 |
return pam_syslog_errno(pamh, LOG_ERR, r, "Failed to load user record: %m"); |
| 218 |
|
| 219 |
/* Safety check if cached record actually matches what we are looking for */ |
| 220 |
if (!user_record_matches_user_name(ur, username)) |
| 221 |
return pam_syslog_pam_error(pamh, LOG_ERR, PAM_SERVICE_ERR, |
| 222 |
"Acquired user record does not match user name."); |
| 223 |
} else { |
| 224 |
_cleanup_free_ char *formatted = NULL; |
| 225 |
|
| 226 |
/* Request the record ourselves */ |
| 227 |
r = userdb_by_name(username, /* match= */ NULL, /* flags= */ 0, &ur); |
| 228 |
if (r < 0) { |
| 229 |
pam_syslog_errno(pamh, LOG_ERR, r, "Failed to get user record: %m"); |
| 230 |
return PAM_USER_UNKNOWN; |
| 231 |
} |
| 232 |
|
| 233 |
if (!uid_is_valid(ur->uid)) |
| 234 |
return pam_syslog_pam_error(pamh, LOG_ERR, PAM_USER_UNKNOWN, |
| 235 |
"User record of user '%s' has no UID, refusing.", username); |
| 236 |
|
| 237 |
r = sd_json_variant_format(ur->json, 0, &formatted); |
| 238 |
if (r < 0) |
| 239 |
return pam_syslog_errno(pamh, LOG_ERR, r, "Failed to format user JSON: %m"); |
| 240 |
|
| 241 |
/* And cache it for everyone else */ |
| 242 |
r = sym_pam_set_data(pamh, field, formatted, pam_cleanup_free); |
| 243 |
if (r != PAM_SUCCESS) |
| 244 |
return pam_syslog_pam_error(pamh, LOG_ERR, r, |
| 245 |
"Failed to set PAM user record data '%s': @PAMERR@", field); |
| 246 |
TAKE_PTR(formatted); |
| 247 |
} |
| 248 |
|
| 249 |
if (!uid_is_valid(ur->uid)) |
| 250 |
return pam_syslog_pam_error(pamh, LOG_ERR, PAM_SERVICE_ERR, |
| 251 |
"Acquired user record does not have a UID."); |
| 252 |
|
| 253 |
if (ret_record) |
| 254 |
*ret_record = TAKE_PTR(ur); |
| 255 |
|
| 256 |
return PAM_SUCCESS; |
| 257 |
} |
| 258 |
|
| 259 |
static bool display_is_local(const char *display) { |
| 260 |
assert(display); |
| 261 |
|
| 262 |
return |
| 263 |
display[0] == ':' && |
| 264 |
ascii_isdigit(display[1]); |
| 265 |
} |
| 266 |
|
| 267 |
static int socket_from_display(const char *display) { |
| 268 |
_cleanup_free_ char *f = NULL; |
| 269 |
size_t k; |
| 270 |
char *c; |
| 271 |
union sockaddr_union sa; |
| 272 |
socklen_t sa_len; |
| 273 |
_cleanup_close_ int fd = -EBADF; |
| 274 |
int r; |
| 275 |
|
| 276 |
assert(display); |
| 277 |
|
| 278 |
if (!display_is_local(display)) |
| 279 |
return -EINVAL; |
| 280 |
|
| 281 |
k = strspn(display + 1, DIGITS); |
| 282 |
|
| 283 |
/* Try abstract socket first. */ |
| 284 |
f = new(char, STRLEN("@/tmp/.X11-unix/X") + k + 1); |
| 285 |
if (!f) |
| 286 |
return -ENOMEM; |
| 287 |
|
| 288 |
c = stpcpy(f, "@/tmp/.X11-unix/X"); |
| 289 |
memcpy(c, display+1, k); |
| 290 |
c[k] = 0; |
| 291 |
|
| 292 |
r = sockaddr_un_set_path(&sa.un, f); |
| 293 |
if (r < 0) |
| 294 |
return r; |
| 295 |
sa_len = r; |
| 296 |
|
| 297 |
fd = RET_NERRNO(socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0)); |
| 298 |
if (fd < 0) |
| 299 |
return fd; |
| 300 |
|
| 301 |
r = RET_NERRNO(connect(fd, &sa.sa, sa_len)); |
| 302 |
if (r >= 0) |
| 303 |
return TAKE_FD(fd); |
| 304 |
if (r != -ECONNREFUSED) |
| 305 |
return r; |
| 306 |
|
| 307 |
/* Try also non-abstract socket. */ |
| 308 |
r = sockaddr_un_set_path(&sa.un, f + 1); |
| 309 |
if (r < 0) |
| 310 |
return r; |
| 311 |
sa_len = r; |
| 312 |
|
| 313 |
r = RET_NERRNO(connect(fd, &sa.sa, sa_len)); |
| 314 |
if (r >= 0) |
| 315 |
return TAKE_FD(fd); |
| 316 |
return r; |
| 317 |
} |
| 318 |
|
| 319 |
static int get_seat_from_display(const char *display, const char **seat, uint32_t *vtnr) { |
| 320 |
_cleanup_free_ char *sys_path = NULL, *tty = NULL; |
| 321 |
_cleanup_close_ int fd = -EBADF; |
| 322 |
struct ucred ucred; |
| 323 |
int v, r; |
| 324 |
dev_t display_ctty; |
| 325 |
|
| 326 |
assert(display); |
| 327 |
assert(vtnr); |
| 328 |
|
| 329 |
/* We deduce the X11 socket from the display name, then use |
| 330 |
* SO_PEERCRED to determine the X11 server process, ask for |
| 331 |
* the controlling tty of that and if it's a VC then we know |
| 332 |
* the seat and the virtual terminal. Sounds ugly, is only |
| 333 |
* semi-ugly. */ |
| 334 |
|
| 335 |
fd = socket_from_display(display); |
| 336 |
if (fd < 0) |
| 337 |
return fd; |
| 338 |
|
| 339 |
r = getpeercred(fd, &ucred); |
| 340 |
if (r < 0) |
| 341 |
return r; |
| 342 |
|
| 343 |
r = get_ctty_devnr(ucred.pid, &display_ctty); |
| 344 |
if (r < 0) |
| 345 |
return r; |
| 346 |
|
| 347 |
if (asprintf(&sys_path, "/sys/dev/char/" DEVNUM_FORMAT_STR, DEVNUM_FORMAT_VAL(display_ctty)) < 0) |
| 348 |
return -ENOMEM; |
| 349 |
r = readlink_value(sys_path, &tty); |
| 350 |
if (r < 0) |
| 351 |
return r; |
| 352 |
|
| 353 |
v = vtnr_from_tty(tty); |
| 354 |
if (v < 0) |
| 355 |
return v; |
| 356 |
assert(v > 0); |
| 357 |
|
| 358 |
if (seat) |
| 359 |
*seat = "seat0"; |
| 360 |
*vtnr = (uint32_t) v; |
| 361 |
|
| 362 |
return 0; |
| 363 |
} |
| 364 |
|
| 365 |
static int append_session_memory_max(pam_handle_t *pamh, sd_bus_message *m, const char *limit) { |
| 366 |
int r; |
| 367 |
|
| 368 |
assert(pamh); |
| 369 |
assert(m); |
| 370 |
|
| 371 |
if (isempty(limit)) |
| 372 |
return 0; |
| 373 |
|
| 374 |
if (streq(limit, "infinity")) |
| 375 |
return sd_bus_message_append(m, "(sv)", "MemoryMax", "t", UINT64_MAX); |
| 376 |
|
| 377 |
r = parse_permyriad(limit); |
| 378 |
if (r >= 0) |
| 379 |
return sd_bus_message_append(m, "(sv)", "MemoryMaxScale", "u", UINT32_SCALE_FROM_PERMYRIAD(r)); |
| 380 |
|
| 381 |
uint64_t val; |
| 382 |
r = parse_size(limit, 1024, &val); |
| 383 |
if (r < 0) { |
| 384 |
sym_pam_syslog(pamh, LOG_WARNING, "Failed to parse systemd.memory_max, ignoring: %s", limit); |
| 385 |
return 0; |
| 386 |
} |
| 387 |
|
| 388 |
return sd_bus_message_append(m, "(sv)", "MemoryMax", "t", val); |
| 389 |
} |
| 390 |
|
| 391 |
static int append_session_runtime_max_sec(pam_handle_t *pamh, sd_bus_message *m, const char *limit) { |
| 392 |
int r; |
| 393 |
|
| 394 |
assert(pamh); |
| 395 |
assert(m); |
| 396 |
|
| 397 |
/* No need to parse "infinity" here, it will be set by default later in scope_init() */ |
| 398 |
if (isempty(limit) || streq(limit, "infinity")) |
| 399 |
return 0; |
| 400 |
|
| 401 |
usec_t val; |
| 402 |
r = parse_sec(limit, &val); |
| 403 |
if (r < 0) { |
| 404 |
sym_pam_syslog(pamh, LOG_WARNING, "Failed to parse systemd.runtime_max_sec: %s, ignoring.", limit); |
| 405 |
return 0; |
| 406 |
} |
| 407 |
|
| 408 |
return sd_bus_message_append(m, "(sv)", "RuntimeMaxUSec", "t", (uint64_t) val); |
| 409 |
} |
| 410 |
|
| 411 |
static int append_session_tasks_max(pam_handle_t *pamh, sd_bus_message *m, const char *limit) { |
| 412 |
int r; |
| 413 |
|
| 414 |
assert(pamh); |
| 415 |
assert(m); |
| 416 |
|
| 417 |
/* No need to parse "infinity" here, it will be set unconditionally later in manager_start_scope() */ |
| 418 |
if (isempty(limit) || streq(limit, "infinity")) |
| 419 |
return 0; |
| 420 |
|
| 421 |
uint64_t val; |
| 422 |
r = safe_atou64(limit, &val); |
| 423 |
if (r < 0) { |
| 424 |
sym_pam_syslog(pamh, LOG_WARNING, "Failed to parse systemd.tasks_max, ignoring: %s", limit); |
| 425 |
return 0; |
| 426 |
} |
| 427 |
|
| 428 |
return sd_bus_message_append(m, "(sv)", "TasksMax", "t", val); |
| 429 |
} |
| 430 |
|
| 431 |
static int append_session_cpu_weight(pam_handle_t *pamh, sd_bus_message *m, const char *limit) { |
| 432 |
int r; |
| 433 |
|
| 434 |
assert(pamh); |
| 435 |
assert(m); |
| 436 |
|
| 437 |
if (isempty(limit)) |
| 438 |
return 0; |
| 439 |
|
| 440 |
uint64_t val; |
| 441 |
r = cg_cpu_weight_parse(limit, &val); |
| 442 |
if (r < 0) { |
| 443 |
sym_pam_syslog(pamh, LOG_WARNING, "Failed to parse systemd.cpu_weight, ignoring: %s", limit); |
| 444 |
return 0; |
| 445 |
} |
| 446 |
|
| 447 |
return sd_bus_message_append(m, "(sv)", "CPUWeight", "t", val); |
| 448 |
} |
| 449 |
|
| 450 |
static int append_session_io_weight(pam_handle_t *pamh, sd_bus_message *m, const char *limit) { |
| 451 |
int r; |
| 452 |
|
| 453 |
assert(pamh); |
| 454 |
assert(m); |
| 455 |
|
| 456 |
if (isempty(limit)) |
| 457 |
return 0; |
| 458 |
|
| 459 |
uint64_t val; |
| 460 |
r = cg_weight_parse(limit, &val); |
| 461 |
if (r < 0) { |
| 462 |
sym_pam_syslog(pamh, LOG_WARNING, "Failed to parse systemd.io_weight, ignoring: %s", limit); |
| 463 |
return 0; |
| 464 |
} |
| 465 |
|
| 466 |
return sd_bus_message_append(m, "(sv)", "IOWeight", "t", val); |
| 467 |
} |
| 468 |
|
| 469 |
static const char* getenv_harder(pam_handle_t *pamh, const char *key, const char *fallback) { |
| 470 |
const char *v; |
| 471 |
|
| 472 |
assert(pamh); |
| 473 |
assert(key); |
| 474 |
|
| 475 |
/* Looks for an environment variable, preferably in the environment block associated with the |
| 476 |
* specified PAM handle, falling back to the process' block instead. Why check both? Because we want |
| 477 |
* to permit configuration of session properties from unit files that invoke PAM services, so that |
| 478 |
* PAM services don't have to be reworked to set systemd-specific properties, but these properties |
| 479 |
* can still be set from the unit file Environment= block. */ |
| 480 |
|
| 481 |
v = sym_pam_getenv(pamh, key); |
| 482 |
if (!isempty(v)) |
| 483 |
return v; |
| 484 |
|
| 485 |
/* We use secure_getenv() here, since we might get loaded into su/sudo, which are SUID. Ideally |
| 486 |
* they'd clean up the environment before invoking foreign code (such as PAM modules), but alas they |
| 487 |
* currently don't (to be precise, they clean up the environment they pass to their children, but |
| 488 |
* not their own environ[]). */ |
| 489 |
v = secure_getenv(key); |
| 490 |
if (!isempty(v)) |
| 491 |
return v; |
| 492 |
|
| 493 |
return fallback; |
| 494 |
} |
| 495 |
|
| 496 |
static bool getenv_harder_bool(pam_handle_t *pamh, const char *key, bool fallback) { |
| 497 |
const char *v; |
| 498 |
int r; |
| 499 |
|
| 500 |
assert(pamh); |
| 501 |
assert(key); |
| 502 |
|
| 503 |
v = getenv_harder(pamh, key, NULL); |
| 504 |
if (isempty(v)) |
| 505 |
return fallback; |
| 506 |
|
| 507 |
r = parse_boolean(v); |
| 508 |
if (r < 0) { |
| 509 |
sym_pam_syslog(pamh, LOG_WARNING, |
| 510 |
"Failed to parse environment variable value '%s' of '%s', falling back to using '%s'.", |
| 511 |
v, key, true_false(fallback)); |
| 512 |
return fallback; |
| 513 |
} |
| 514 |
|
| 515 |
return r; |
| 516 |
} |
| 517 |
|
| 518 |
static uint32_t getenv_harder_uint32(pam_handle_t *pamh, const char *key, uint32_t fallback) { |
| 519 |
int r; |
| 520 |
|
| 521 |
assert(pamh); |
| 522 |
assert(key); |
| 523 |
|
| 524 |
const char *v = getenv_harder(pamh, key, NULL); |
| 525 |
if (isempty(v)) |
| 526 |
return fallback; |
| 527 |
|
| 528 |
uint32_t u; |
| 529 |
r = safe_atou32(v, &u); |
| 530 |
if (r < 0) { |
| 531 |
sym_pam_syslog(pamh, LOG_WARNING, |
| 532 |
"Failed to parse environment variable value '%s' of '%s' as unsigned integer, falling back to using %" PRIu32 ".", |
| 533 |
v, key, fallback); |
| 534 |
return fallback; |
| 535 |
} |
| 536 |
|
| 537 |
return u; |
| 538 |
} |
| 539 |
|
| 540 |
static int update_environment(pam_handle_t *pamh, const char *key, const char *value) { |
| 541 |
int r; |
| 542 |
|
| 543 |
assert(pamh); |
| 544 |
assert(key); |
| 545 |
|
| 546 |
/* Updates the environment, and removes environment variables if value is NULL or empty. Also, log |
| 547 |
* about errors. */ |
| 548 |
|
| 549 |
if (isempty(value)) { |
| 550 |
/* Unset the variable if set. Note that sym_pam_putenv() would log nastily behind our back if we |
| 551 |
* call it without the variable actually being set. Hence we check explicitly if it's set |
| 552 |
* before. */ |
| 553 |
|
| 554 |
if (!sym_pam_getenv(pamh, key)) |
| 555 |
return PAM_SUCCESS; |
| 556 |
|
| 557 |
r = sym_pam_putenv(pamh, key); |
| 558 |
if (!IN_SET(r, PAM_SUCCESS, PAM_BAD_ITEM)) |
| 559 |
return pam_syslog_pam_error(pamh, LOG_WARNING, r, |
| 560 |
"Failed to unset %s environment variable: @PAMERR@", key); |
| 561 |
|
| 562 |
return PAM_SUCCESS; |
| 563 |
} |
| 564 |
|
| 565 |
r = pam_putenv_assign(pamh, key, value); |
| 566 |
if (r != PAM_SUCCESS) |
| 567 |
return pam_syslog_pam_error(pamh, LOG_ERR, r, |
| 568 |
"Failed to set environment variable %s: @PAMERR@", key); |
| 569 |
|
| 570 |
return PAM_SUCCESS; |
| 571 |
} |
| 572 |
|
| 573 |
static int propagate_credential_to_environment(pam_handle_t *pamh, bool debug, const char *credential, const char *varname) { |
| 574 |
int r; |
| 575 |
|
| 576 |
assert(pamh); |
| 577 |
assert(credential); |
| 578 |
assert(varname); |
| 579 |
|
| 580 |
_cleanup_free_ char *value = NULL; |
| 581 |
|
| 582 |
/* Read a service credential, and propagate it into an environment variable */ |
| 583 |
|
| 584 |
r = read_credential(credential, (void**) &value, /* ret_size= */ NULL); |
| 585 |
if (r < 0) { |
| 586 |
pam_debug_syslog_errno(pamh, debug, r, "Failed to read credential '%s', ignoring: %m", credential); |
| 587 |
return PAM_SUCCESS; |
| 588 |
} |
| 589 |
|
| 590 |
r = pam_putenv_assign(pamh, varname, value); |
| 591 |
if (r != PAM_SUCCESS) |
| 592 |
return pam_syslog_pam_error(pamh, LOG_ERR, r, |
| 593 |
"Failed to set environment variable %s: @PAMERR@", varname); |
| 594 |
|
| 595 |
return PAM_SUCCESS; |
| 596 |
} |
| 597 |
|
| 598 |
static bool validate_runtime_directory(pam_handle_t *pamh, const char *path, uid_t uid) { |
| 599 |
struct stat st; |
| 600 |
|
| 601 |
assert(pamh); |
| 602 |
assert(path); |
| 603 |
|
| 604 |
/* Some extra paranoia: let's not set $XDG_RUNTIME_DIR if the directory we'd set it to isn't actually |
| 605 |
* set up properly for us. This is supposed to provide a careful safety net for supporting su/sudo |
| 606 |
* type transitions: in that case the UID changes, but the session and thus the user owning it |
| 607 |
* doesn't change. Since the $XDG_RUNTIME_DIR lifecycle is bound to the session's user being logged |
| 608 |
* in at least once we should be particularly careful when setting the environment variable, since |
| 609 |
* otherwise we might end up setting $XDG_RUNTIME_DIR to some directory owned by the wrong user. */ |
| 610 |
|
| 611 |
if (!path_is_absolute(path)) { |
| 612 |
sym_pam_syslog(pamh, LOG_ERR, "Provided runtime directory '%s' is not absolute.", path); |
| 613 |
goto fail; |
| 614 |
} |
| 615 |
|
| 616 |
if (lstat(path, &st) < 0) { |
| 617 |
pam_syslog_errno(pamh, LOG_ERR, errno, "Failed to stat() runtime directory '%s': %m", path); |
| 618 |
goto fail; |
| 619 |
} |
| 620 |
|
| 621 |
if (!S_ISDIR(st.st_mode)) { |
| 622 |
sym_pam_syslog(pamh, LOG_ERR, "Runtime directory '%s' is not actually a directory.", path); |
| 623 |
goto fail; |
| 624 |
} |
| 625 |
|
| 626 |
if (st.st_uid != uid) { |
| 627 |
sym_pam_syslog(pamh, LOG_ERR, "Runtime directory '%s' is not owned by UID " UID_FMT ", as it should.", path, uid); |
| 628 |
goto fail; |
| 629 |
} |
| 630 |
|
| 631 |
return true; |
| 632 |
|
| 633 |
fail: |
| 634 |
sym_pam_syslog(pamh, LOG_WARNING, "Not setting $XDG_RUNTIME_DIR, as the directory is not in order."); |
| 635 |
return false; |
| 636 |
} |
| 637 |
|
| 638 |
static int pam_putenv_and_log(pam_handle_t *pamh, const char *e, bool debug) { |
| 639 |
int r; |
| 640 |
|
| 641 |
assert(pamh); |
| 642 |
assert(e); |
| 643 |
|
| 644 |
r = sym_pam_putenv(pamh, e); |
| 645 |
if (r != PAM_SUCCESS) |
| 646 |
return pam_syslog_pam_error(pamh, LOG_ERR, r, |
| 647 |
"Failed to set PAM environment variable %s: @PAMERR@", e); |
| 648 |
|
| 649 |
pam_debug_syslog(pamh, debug, "PAM environment variable %s set based on user record.", e); |
| 650 |
return PAM_SUCCESS; |
| 651 |
} |
| 652 |
|
| 653 |
static int apply_user_record_settings( |
| 654 |
pam_handle_t *pamh, |
| 655 |
UserRecord *ur, |
| 656 |
bool debug, |
| 657 |
uint64_t default_capability_bounding_set, |
| 658 |
uint64_t default_capability_ambient_set) { |
| 659 |
_cleanup_strv_free_ char **langs = NULL; |
| 660 |
int r; |
| 661 |
|
| 662 |
assert(pamh); |
| 663 |
assert(ur); |
| 664 |
|
| 665 |
if (ur->umask != MODE_INVALID) { |
| 666 |
umask(ur->umask); |
| 667 |
pam_debug_syslog(pamh, debug, "Set user umask to %04o based on user record.", ur->umask); |
| 668 |
} |
| 669 |
|
| 670 |
STRV_FOREACH(i, ur->environment) { |
| 671 |
r = pam_putenv_and_log(pamh, *i, debug); |
| 672 |
if (r != PAM_SUCCESS) |
| 673 |
return r; |
| 674 |
} |
| 675 |
|
| 676 |
if (ur->email_address) { |
| 677 |
_cleanup_free_ char *joined = NULL; |
| 678 |
|
| 679 |
joined = strjoin("EMAIL=", ur->email_address); |
| 680 |
if (!joined) |
| 681 |
return pam_log_oom(pamh); |
| 682 |
|
| 683 |
r = pam_putenv_and_log(pamh, joined, debug); |
| 684 |
if (r != PAM_SUCCESS) |
| 685 |
return r; |
| 686 |
} |
| 687 |
|
| 688 |
if (ur->time_zone) { |
| 689 |
if (!timezone_is_valid(ur->time_zone, LOG_DEBUG)) |
| 690 |
pam_debug_syslog(pamh, debug, |
| 691 |
"Time zone specified in user record is not valid locally, not setting $TZ."); |
| 692 |
else { |
| 693 |
_cleanup_free_ char *joined = NULL; |
| 694 |
|
| 695 |
joined = strjoin("TZ=:", ur->time_zone); |
| 696 |
if (!joined) |
| 697 |
return pam_log_oom(pamh); |
| 698 |
|
| 699 |
r = pam_putenv_and_log(pamh, joined, debug); |
| 700 |
if (r != PAM_SUCCESS) |
| 701 |
return r; |
| 702 |
} |
| 703 |
} |
| 704 |
|
| 705 |
r = user_record_languages(ur, &langs); |
| 706 |
if (r < 0) |
| 707 |
pam_syslog_errno(pamh, LOG_ERR, r, |
| 708 |
"Failed to acquire user's language preferences, ignoring: %m"); |
| 709 |
else if (strv_isempty(langs)) |
| 710 |
; /* User has no preference set so we do nothing */ |
| 711 |
else if (locale_is_installed(langs[0]) <= 0) |
| 712 |
pam_debug_syslog(pamh, debug, |
| 713 |
"Preferred languages specified in user record are not installed locally, not setting $LANG or $LANGUAGE."); |
| 714 |
else { |
| 715 |
_cleanup_free_ char *lang = NULL; |
| 716 |
|
| 717 |
lang = strjoin("LANG=", langs[0]); |
| 718 |
if (!lang) |
| 719 |
return pam_log_oom(pamh); |
| 720 |
|
| 721 |
r = pam_putenv_and_log(pamh, lang, debug); |
| 722 |
if (r != PAM_SUCCESS) |
| 723 |
return r; |
| 724 |
|
| 725 |
if (strv_length(langs) > 1) { |
| 726 |
_cleanup_free_ char *joined = NULL, *language = NULL; |
| 727 |
|
| 728 |
joined = strv_join(langs, ":"); |
| 729 |
if (!joined) |
| 730 |
return pam_log_oom(pamh); |
| 731 |
|
| 732 |
language = strjoin("LANGUAGE=", joined); |
| 733 |
if (!language) |
| 734 |
return pam_log_oom(pamh); |
| 735 |
|
| 736 |
r = pam_putenv_and_log(pamh, language, debug); |
| 737 |
if (r != PAM_SUCCESS) |
| 738 |
return r; |
| 739 |
} |
| 740 |
} |
| 741 |
|
| 742 |
if (nice_is_valid(ur->nice_level)) { |
| 743 |
if (nice(ur->nice_level) < 0) |
| 744 |
pam_syslog_errno(pamh, LOG_WARNING, errno, |
| 745 |
"Failed to set nice level to %i, ignoring: %m", ur->nice_level); |
| 746 |
else |
| 747 |
pam_debug_syslog(pamh, debug, |
| 748 |
"Nice level set to %i, based on user record.", ur->nice_level); |
| 749 |
} |
| 750 |
|
| 751 |
for (int rl = 0; rl < _RLIMIT_MAX; rl++) { |
| 752 |
if (!ur->rlimits[rl]) |
| 753 |
continue; |
| 754 |
|
| 755 |
r = setrlimit_closest(rl, ur->rlimits[rl]); |
| 756 |
if (r < 0) |
| 757 |
pam_syslog_errno(pamh, LOG_ERR, r, |
| 758 |
"Failed to set resource limit %s, ignoring: %m", rlimit_to_string(rl)); |
| 759 |
else |
| 760 |
pam_debug_syslog(pamh, debug, |
| 761 |
"Resource limit %s set, based on user record.", rlimit_to_string(rl)); |
| 762 |
} |
| 763 |
|
| 764 |
uint64_t a, b; |
| 765 |
a = user_record_capability_ambient_set(ur); |
| 766 |
if (a == CAP_MASK_UNSET) |
| 767 |
a = default_capability_ambient_set; |
| 768 |
|
| 769 |
b = user_record_capability_bounding_set(ur); |
| 770 |
if (b == CAP_MASK_UNSET) |
| 771 |
b = default_capability_bounding_set; |
| 772 |
|
| 773 |
if (a != CAP_MASK_UNSET && a != 0) { |
| 774 |
a &= b; |
| 775 |
|
| 776 |
r = capability_ambient_set_apply(a, /* also_inherit= */ true); |
| 777 |
if (r < 0) |
| 778 |
pam_syslog_errno(pamh, LOG_ERR, r, |
| 779 |
"Failed to set ambient capabilities, ignoring: %m"); |
| 780 |
} |
| 781 |
|
| 782 |
if (b != CAP_MASK_UNSET && !cap_test_all(b)) { |
| 783 |
r = capability_bounding_set_drop(b, /* right_now= */ false); |
| 784 |
if (r < 0) |
| 785 |
pam_syslog_errno(pamh, LOG_ERR, r, |
| 786 |
"Failed to set bounding capabilities, ignoring: %m"); |
| 787 |
} |
| 788 |
|
| 789 |
return PAM_SUCCESS; |
| 790 |
} |
| 791 |
|
| 792 |
static uint64_t pick_default_capability_ambient_set( |
| 793 |
UserRecord *ur, |
| 794 |
const char *service, |
| 795 |
const char *seat) { |
| 796 |
|
| 797 |
/* If not configured otherwise, let's enable CAP_WAKE_ALARM for regular users when logging in on a |
| 798 |
* seat (i.e. when they are present physically on the device), or when invoked for the systemd --user |
| 799 |
* instances. This allows desktops to install CAP_WAKE_ALARM to implement alarm clock apps without |
| 800 |
* much fuss. */ |
| 801 |
|
| 802 |
return ur && |
| 803 |
user_record_disposition(ur) == USER_REGULAR && |
| 804 |
(streq_ptr(service, "systemd-user") || !isempty(seat)) ? (UINT64_C(1) << CAP_WAKE_ALARM) : CAP_MASK_UNSET; |
| 805 |
} |
| 806 |
|
| 807 |
typedef struct SessionContext { |
| 808 |
const char *service; |
| 809 |
const char *type; |
| 810 |
const char *class; |
| 811 |
const char *desktop; |
| 812 |
const char *seat; |
| 813 |
uint32_t vtnr; |
| 814 |
const char *tty; |
| 815 |
const char *display; |
| 816 |
char **extra_device_access; |
| 817 |
bool remote; |
| 818 |
const char *remote_user; |
| 819 |
const char *remote_host; |
| 820 |
const char *memory_max; |
| 821 |
const char *tasks_max; |
| 822 |
const char *cpu_weight; |
| 823 |
const char *io_weight; |
| 824 |
const char *runtime_max_sec; |
| 825 |
const char *area; |
| 826 |
bool incomplete; |
| 827 |
} SessionContext; |
| 828 |
|
| 829 |
static void session_context_done(SessionContext *c) { |
| 830 |
strv_free(c->extra_device_access); |
| 831 |
} |
| 832 |
|
| 833 |
static int create_session_message( |
| 834 |
sd_bus *bus, |
| 835 |
pam_handle_t *pamh, |
| 836 |
UserRecord *ur, |
| 837 |
const SessionContext *context, |
| 838 |
bool avoid_pidfd, |
| 839 |
sd_bus_message **ret) { |
| 840 |
|
| 841 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
| 842 |
_cleanup_close_ int pidfd = -EBADF; |
| 843 |
int r; |
| 844 |
|
| 845 |
assert(bus); |
| 846 |
assert(pamh); |
| 847 |
assert(ur); |
| 848 |
assert(context); |
| 849 |
assert(ret); |
| 850 |
|
| 851 |
if (!avoid_pidfd) { |
| 852 |
pidfd = pidfd_open(getpid_cached(), 0); |
| 853 |
if (pidfd < 0) |
| 854 |
return -errno; |
| 855 |
} |
| 856 |
|
| 857 |
r = bus_message_new_method_call(bus, &m, bus_login_mgr, pidfd >= 0 ? "CreateSessionWithPIDFD" : "CreateSession"); |
| 858 |
if (r < 0) |
| 859 |
return r; |
| 860 |
|
| 861 |
r = sd_bus_message_append( |
| 862 |
m, |
| 863 |
pidfd >= 0 ? "uhsssssussbss" : "uusssssussbss", |
| 864 |
(uint32_t) ur->uid, |
| 865 |
pidfd >= 0 ? pidfd : 0, |
| 866 |
context->service, |
| 867 |
context->type, |
| 868 |
context->class, |
| 869 |
context->desktop, |
| 870 |
context->seat, |
| 871 |
context->vtnr, |
| 872 |
context->tty, |
| 873 |
context->display, |
| 874 |
context->remote, |
| 875 |
context->remote_user, |
| 876 |
context->remote_host); |
| 877 |
if (r < 0) |
| 878 |
return r; |
| 879 |
|
| 880 |
if (pidfd >= 0) { |
| 881 |
r = sd_bus_message_append(m, "t", UINT64_C(0)); |
| 882 |
if (r < 0) |
| 883 |
return r; |
| 884 |
} |
| 885 |
|
| 886 |
r = sd_bus_message_open_container(m, 'a', "(sv)"); |
| 887 |
if (r < 0) |
| 888 |
return r; |
| 889 |
|
| 890 |
r = append_session_memory_max(pamh, m, context->memory_max); |
| 891 |
if (r < 0) |
| 892 |
return r; |
| 893 |
|
| 894 |
r = append_session_runtime_max_sec(pamh, m, context->runtime_max_sec); |
| 895 |
if (r < 0) |
| 896 |
return r; |
| 897 |
|
| 898 |
r = append_session_tasks_max(pamh, m, context->tasks_max); |
| 899 |
if (r < 0) |
| 900 |
return r; |
| 901 |
|
| 902 |
r = append_session_cpu_weight(pamh, m, context->cpu_weight); |
| 903 |
if (r < 0) |
| 904 |
return r; |
| 905 |
|
| 906 |
r = append_session_io_weight(pamh, m, context->io_weight); |
| 907 |
if (r < 0) |
| 908 |
return r; |
| 909 |
|
| 910 |
r = sd_bus_message_close_container(m); |
| 911 |
if (r < 0) |
| 912 |
return r; |
| 913 |
|
| 914 |
*ret = TAKE_PTR(m); |
| 915 |
return 0; |
| 916 |
} |
| 917 |
|
| 918 |
static void session_context_mangle( |
| 919 |
pam_handle_t *pamh, |
| 920 |
SessionContext *c, |
| 921 |
UserRecord *ur, |
| 922 |
bool debug) { |
| 923 |
|
| 924 |
assert(pamh); |
| 925 |
assert(c); |
| 926 |
assert(ur); |
| 927 |
|
| 928 |
/* The session class can be overridden via the PAM environment, and we try to honor that selection. */ |
| 929 |
if (streq_ptr(c->service, "systemd-user")) { |
| 930 |
/* If we detect that we are running in the "systemd-user" PAM stack, then let's patch the class to |
| 931 |
* 'manager' if not set, simply for robustness reasons. */ |
| 932 |
c->type = "unspecified"; |
| 933 |
if (isempty(c->class)) |
| 934 |
c->class = IN_SET(user_record_disposition(ur), USER_INTRINSIC, USER_SYSTEM, USER_DYNAMIC) ? |
| 935 |
"manager-early" : "manager"; |
| 936 |
c->tty = NULL; |
| 937 |
|
| 938 |
} else if (c->tty && strchr(c->tty, ':')) { |
| 939 |
/* A tty with a colon is usually an X11 display, placed there to show up in utmp. We rearrange things |
| 940 |
* and don't pretend that an X display was a tty. */ |
| 941 |
if (isempty(c->display)) |
| 942 |
c->display = c->tty; |
| 943 |
c->tty = NULL; |
| 944 |
|
| 945 |
} else if (streq_ptr(c->tty, "cron")) { |
| 946 |
/* cron is setting PAM_TTY to "cron" for some reason (the commit carries no information why, but |
| 947 |
* probably because it wants to set it to something as pam_time/pam_access/… require PAM_TTY to be set |
| 948 |
* (as they otherwise even try to update it!) — but cron doesn't actually allocate a TTY for its forked |
| 949 |
* off processes.) */ |
| 950 |
c->type = "unspecified"; |
| 951 |
if (isempty(c->class)) |
| 952 |
c->class = "background"; |
| 953 |
c->tty = NULL; |
| 954 |
|
| 955 |
} else if (streq_ptr(c->tty, "ssh")) { |
| 956 |
/* ssh has been setting PAM_TTY to "ssh" (for the same reason as cron does this, see above. For further |
| 957 |
* details look for "PAM_TTY_KLUDGE" in the openssh sources). */ |
| 958 |
c->type = "tty"; |
| 959 |
if (isempty(c->class)) |
| 960 |
c->class = "user"; |
| 961 |
c->tty = NULL; /* This one is particularly sad, as this means that ssh sessions — even though |
| 962 |
* usually associated with a pty — won't be tracked by their tty in |
| 963 |
* logind. This is because ssh does the PAM session registration early for new |
| 964 |
* connections, and registers a pty only much later (this is because it doesn't |
| 965 |
* know yet if it needs one at all, as whether to register a pty or not is |
| 966 |
* negotiated much later in the protocol). */ |
| 967 |
|
| 968 |
} else if (c->tty) |
| 969 |
/* Chop off leading /dev prefix that some clients specify, but others do not. */ |
| 970 |
c->tty = skip_dev_prefix(c->tty); |
| 971 |
|
| 972 |
if (!isempty(c->display) && !c->vtnr) { |
| 973 |
if (isempty(c->seat)) |
| 974 |
(void) get_seat_from_display(c->display, &c->seat, &c->vtnr); |
| 975 |
else if (streq(c->seat, "seat0")) |
| 976 |
(void) get_seat_from_display(c->display, /* seat= */ NULL, &c->vtnr); |
| 977 |
} |
| 978 |
|
| 979 |
if (c->seat && !streq(c->seat, "seat0") && c->vtnr != 0) { |
| 980 |
pam_debug_syslog(pamh, debug, "Ignoring vtnr %"PRIu32" for %s which is not seat0.", c->vtnr, c->seat); |
| 981 |
c->vtnr = 0; |
| 982 |
} |
| 983 |
|
| 984 |
if (isempty(c->type)) { |
| 985 |
c->type = !isempty(c->display) ? "x11" : |
| 986 |
!isempty(c->tty) ? "tty" : "unspecified"; |
| 987 |
pam_debug_syslog(pamh, debug, "Automatically chose session type '%s'.", c->type); |
| 988 |
} |
| 989 |
|
| 990 |
if (!c->area) |
| 991 |
c->area = ur->default_area; |
| 992 |
|
| 993 |
if (!isempty(c->area) && !filename_is_valid(c->area)) { |
| 994 |
sym_pam_syslog(pamh, LOG_WARNING, "Specified area '%s' is not a valid filename, ignoring area request.", c->area); |
| 995 |
c->area = NULL; |
| 996 |
} |
| 997 |
|
| 998 |
if (isempty(c->class)) { |
| 999 |
c->class = streq(c->type, "unspecified") ? "background" : "user"; |
| 1000 |
|
| 1001 |
/* For non-regular users tweak the type a bit: |
| 1002 |
* |
| 1003 |
* - Allow root tty logins *before* systemd-user-sessions.service is run, to allow early boot |
| 1004 |
* logins to debug things. |
| 1005 |
* |
| 1006 |
* - Non-graphical sessions shall be invoked without service manager. |
| 1007 |
* |
| 1008 |
* (Note that this somewhat replicates the class mangling logic on systemd-logind.service's |
| 1009 |
* server side to some degree, in case clients allocate a session and don't specify a |
| 1010 |
* class. This is somewhat redundant, but we need the class set up properly below.) |
| 1011 |
* |
| 1012 |
* For regular users also tweak the type a bit: if an area is specified at login time, switch |
| 1013 |
* to light mode too. (Mostly because at the moment we do no support a per-area service |
| 1014 |
* manager. Once we do, we should change this.). |
| 1015 |
*/ |
| 1016 |
|
| 1017 |
switch (user_record_disposition(ur)) { |
| 1018 |
|
| 1019 |
case USER_INTRINSIC: |
| 1020 |
case USER_SYSTEM: |
| 1021 |
case USER_DYNAMIC: |
| 1022 |
if (streq(c->class, "user")) |
| 1023 |
c->class = user_record_is_root(ur) ? "user-early" : |
| 1024 |
(STR_IN_SET(c->type, "x11", "wayland", "mir") ? "user" : "user-light"); |
| 1025 |
else if (streq(c->class, "background")) |
| 1026 |
c->class = "background-light"; |
| 1027 |
break; |
| 1028 |
|
| 1029 |
case USER_REGULAR: |
| 1030 |
if (!isempty(c->area)) { |
| 1031 |
if (streq(c->class, "user")) |
| 1032 |
c->class = "user-light"; |
| 1033 |
else if (streq(c->class, "background")) |
| 1034 |
c->class = "background-light"; |
| 1035 |
} |
| 1036 |
|
| 1037 |
break; |
| 1038 |
|
| 1039 |
default: |
| 1040 |
; |
| 1041 |
} |
| 1042 |
|
| 1043 |
pam_debug_syslog(pamh, debug, "Automatically chose session class '%s'.", c->class); |
| 1044 |
} |
| 1045 |
|
| 1046 |
if (c->incomplete) { |
| 1047 |
if (streq(c->class, "user")) |
| 1048 |
c->class = "user-incomplete"; |
| 1049 |
else |
| 1050 |
sym_pam_syslog(pamh, LOG_WARNING, "PAM session of class '%s' is incomplete, which is not supported, ignoring.", c->class); |
| 1051 |
} |
| 1052 |
|
| 1053 |
c->remote = !isempty(c->remote_host) && !is_localhost(c->remote_host); |
| 1054 |
} |
| 1055 |
|
| 1056 |
static bool can_use_varlink(const SessionContext *c) { |
| 1057 |
/* Since PID 1 currently doesn't do Varlink right now, we cannot directly set properties for the |
| 1058 |
* scope, for now. */ |
| 1059 |
return !c->memory_max && |
| 1060 |
!c->runtime_max_sec && |
| 1061 |
!c->tasks_max && |
| 1062 |
!c->cpu_weight && |
| 1063 |
!c->io_weight; |
| 1064 |
} |
| 1065 |
|
| 1066 |
static int register_session( |
| 1067 |
pam_handle_t *pamh, |
| 1068 |
SessionContext *c, |
| 1069 |
UserRecord *ur, |
| 1070 |
bool debug, |
| 1071 |
char **ret_seat, |
| 1072 |
char **ret_type, |
| 1073 |
char **ret_runtime_dir) { |
| 1074 |
|
| 1075 |
int r; |
| 1076 |
|
| 1077 |
assert(pamh); |
| 1078 |
assert(c); |
| 1079 |
assert(ur); |
| 1080 |
assert(ret_seat); |
| 1081 |
assert(ret_type); |
| 1082 |
assert(ret_runtime_dir); |
| 1083 |
|
| 1084 |
/* We don't register session class none with logind */ |
| 1085 |
if (streq(c->class, "none")) { |
| 1086 |
pam_debug_syslog(pamh, debug, "Skipping logind registration for session class none."); |
| 1087 |
*ret_seat = *ret_type = *ret_runtime_dir = NULL; |
| 1088 |
return PAM_SUCCESS; |
| 1089 |
} |
| 1090 |
|
| 1091 |
/* Make most of this a NOP on non-logind systems */ |
| 1092 |
if (!logind_running()) { |
| 1093 |
pam_debug_syslog(pamh, debug, "Skipping logind registration as logind is not running."); |
| 1094 |
*ret_seat = *ret_type = *ret_runtime_dir = NULL; |
| 1095 |
return PAM_SUCCESS; |
| 1096 |
} |
| 1097 |
|
| 1098 |
pam_debug_syslog(pamh, debug, |
| 1099 |
"Asking logind to create session: " |
| 1100 |
"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", |
| 1101 |
ur->uid, getpid_cached(), |
| 1102 |
strempty(c->service), |
| 1103 |
c->type, c->class, strempty(c->desktop), |
| 1104 |
strempty(c->seat), c->vtnr, strempty(c->tty), strempty(c->display), |
| 1105 |
yes_no(c->remote), strempty(c->remote_user), strempty(c->remote_host)); |
| 1106 |
pam_debug_syslog(pamh, debug, |
| 1107 |
"Session limits: " |
| 1108 |
"memory_max=%s tasks_max=%s cpu_weight=%s io_weight=%s runtime_max_sec=%s", |
| 1109 |
strna(c->memory_max), strna(c->tasks_max), strna(c->cpu_weight), strna(c->io_weight), strna(c->runtime_max_sec)); |
| 1110 |
|
| 1111 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; /* the following variables point into this message, hence pin it for longer */ |
| 1112 |
_cleanup_(sd_varlink_unrefp) sd_varlink *vl = NULL; /* similar */ |
| 1113 |
const char *id = NULL, *object_path = NULL, *runtime_path = NULL, *real_seat = NULL; |
| 1114 |
uint32_t original_uid = UID_INVALID, real_vtnr = 0; |
| 1115 |
|
| 1116 |
bool done = false; |
| 1117 |
if (can_use_varlink(c)) { |
| 1118 |
|
| 1119 |
r = sd_varlink_connect_address(&vl, "/run/systemd/io.systemd.Login"); |
| 1120 |
if (r < 0) |
| 1121 |
pam_debug_syslog_errno(pamh, debug, r, "Failed to connect to logind via Varlink, falling back to D-Bus: %m"); |
| 1122 |
else { |
| 1123 |
r = sd_varlink_set_allow_fd_passing_output(vl, true); |
| 1124 |
if (r < 0) |
| 1125 |
return pam_syslog_errno(pamh, LOG_ERR, r, "Failed to enable output fd passing on Varlink socket: %m"); |
| 1126 |
|
| 1127 |
r = sd_varlink_set_relative_timeout(vl, LOGIN_SLOW_BUS_CALL_TIMEOUT_USEC); |
| 1128 |
if (r < 0) |
| 1129 |
return pam_syslog_errno(pamh, LOG_ERR, r, "Failed to set relative timeout on Varlink socket: %m"); |
| 1130 |
|
| 1131 |
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; |
| 1132 |
r = pidref_set_self(&pidref); |
| 1133 |
if (r < 0) |
| 1134 |
return pam_syslog_errno(pamh, LOG_ERR, r, "Failed to acquire PID reference on ourselves: %m"); |
| 1135 |
|
| 1136 |
sd_json_variant *vreply = NULL; |
| 1137 |
const char *error_id = NULL; |
| 1138 |
r = sd_varlink_callbo( |
| 1139 |
vl, |
| 1140 |
"io.systemd.Login.CreateSession", |
| 1141 |
&vreply, |
| 1142 |
&error_id, |
| 1143 |
SD_JSON_BUILD_PAIR_UNSIGNED("UID", ur->uid), |
| 1144 |
JSON_BUILD_PAIR_PIDREF("PID", &pidref), |
| 1145 |
JSON_BUILD_PAIR_STRING_NON_EMPTY("Service", c->service), |
| 1146 |
JSON_BUILD_PAIR_ENUM("Type", c->type), |
| 1147 |
JSON_BUILD_PAIR_ENUM("Class", c->class), |
| 1148 |
JSON_BUILD_PAIR_STRING_NON_EMPTY("Desktop", c->desktop), |
| 1149 |
JSON_BUILD_PAIR_STRING_NON_EMPTY("Seat", c->seat), |
| 1150 |
JSON_BUILD_PAIR_UNSIGNED_NON_ZERO("VTNr", c->vtnr), |
| 1151 |
JSON_BUILD_PAIR_STRING_NON_EMPTY("TTY", c->tty), |
| 1152 |
JSON_BUILD_PAIR_STRING_NON_EMPTY("Display", c->display), |
| 1153 |
SD_JSON_BUILD_PAIR_BOOLEAN("Remote", c->remote), |
| 1154 |
JSON_BUILD_PAIR_STRING_NON_EMPTY("RemoteUser", c->remote_user), |
| 1155 |
JSON_BUILD_PAIR_STRING_NON_EMPTY("RemoteHost", c->remote_host), |
| 1156 |
JSON_BUILD_PAIR_CONDITION_STRV(!strv_isempty(c->extra_device_access), "ExtraDeviceAccess", c->extra_device_access)); |
| 1157 |
if (r < 0) |
| 1158 |
return pam_syslog_errno(pamh, LOG_ERR, r, |
| 1159 |
"Failed to issue io.systemd.Login.CreateSession varlink call: %m"); |
| 1160 |
if (streq_ptr(error_id, "io.systemd.Login.AlreadySessionMember")) { |
| 1161 |
/* We are already in a session, don't do anything */ |
| 1162 |
pam_debug_syslog(pamh, debug, "Not creating session: %s", error_id); |
| 1163 |
*ret_seat = *ret_type= *ret_runtime_dir = NULL; |
| 1164 |
return PAM_SUCCESS; |
| 1165 |
} |
| 1166 |
if (error_id) |
| 1167 |
return pam_syslog_errno(pamh, LOG_ERR, sd_varlink_error_to_errno(error_id, vreply), |
| 1168 |
"Varlink call io.systemd.Login.CreateSession failed: %s", error_id); |
| 1169 |
|
| 1170 |
struct { |
| 1171 |
const char *id; |
| 1172 |
const char *runtime_path; |
| 1173 |
uid_t uid; |
| 1174 |
const char *seat; |
| 1175 |
unsigned vtnr; |
| 1176 |
} p = { |
| 1177 |
.uid = UID_INVALID, |
| 1178 |
}; |
| 1179 |
|
| 1180 |
static const sd_json_dispatch_field dispatch_table[] = { |
| 1181 |
{ "Id", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, voffsetof(p, id), SD_JSON_MANDATORY }, |
| 1182 |
{ "RuntimePath", SD_JSON_VARIANT_STRING, json_dispatch_const_path, voffsetof(p, runtime_path), SD_JSON_MANDATORY }, |
| 1183 |
{ "UID", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uid_gid, voffsetof(p, uid), SD_JSON_MANDATORY }, |
| 1184 |
{ "Seat", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, voffsetof(p, seat), 0 }, |
| 1185 |
{ "VTNr", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint, voffsetof(p, vtnr), 0 }, |
| 1186 |
{} |
| 1187 |
}; |
| 1188 |
|
| 1189 |
r = sd_json_dispatch(vreply, dispatch_table, SD_JSON_ALLOW_EXTENSIONS, &p); |
| 1190 |
if (r < 0) |
| 1191 |
return pam_syslog_errno(pamh, LOG_ERR, r, "Failed to parse CreateSession() reply: %m"); |
| 1192 |
|
| 1193 |
id = p.id; |
| 1194 |
runtime_path = p.runtime_path; |
| 1195 |
original_uid = p.uid; |
| 1196 |
real_seat = p.seat; |
| 1197 |
real_vtnr = p.vtnr; |
| 1198 |
|
| 1199 |
done = true; |
| 1200 |
} |
| 1201 |
} |
| 1202 |
|
| 1203 |
if (!done) { |
| 1204 |
/* Let's release the D-Bus connection once we are done here, after all the session might live |
| 1205 |
* quite a long time, and we are not going to process the bus connection in that time, so |
| 1206 |
* let's better close before the daemon kicks us off because we are not processing |
| 1207 |
* anything. */ |
| 1208 |
_cleanup_(pam_bus_data_disconnectp) PamBusData *d = NULL; |
| 1209 |
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
| 1210 |
|
| 1211 |
/* Talk to logind over the message bus */ |
| 1212 |
r = pam_acquire_bus_connection(pamh, "pam-systemd", debug, &bus, &d); |
| 1213 |
if (r != PAM_SUCCESS) |
| 1214 |
return r; |
| 1215 |
|
| 1216 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
| 1217 |
r = create_session_message( |
| 1218 |
bus, |
| 1219 |
pamh, |
| 1220 |
ur, |
| 1221 |
c, |
| 1222 |
/* avoid_pidfd= */ false, |
| 1223 |
&m); |
| 1224 |
if (r < 0) |
| 1225 |
return pam_bus_log_create_error(pamh, r); |
| 1226 |
|
| 1227 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 1228 |
r = sd_bus_call(bus, m, LOGIN_SLOW_BUS_CALL_TIMEOUT_USEC, &error, &reply); |
| 1229 |
if (r < 0 && sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_METHOD)) { |
| 1230 |
sd_bus_error_free(&error); |
| 1231 |
pam_debug_syslog(pamh, debug, |
| 1232 |
"CreateSessionWithPIDFD() API is not available, retrying with CreateSession()."); |
| 1233 |
|
| 1234 |
m = sd_bus_message_unref(m); |
| 1235 |
r = create_session_message(bus, |
| 1236 |
pamh, |
| 1237 |
ur, |
| 1238 |
c, |
| 1239 |
/* avoid_pidfd= */ true, |
| 1240 |
&m); |
| 1241 |
if (r < 0) |
| 1242 |
return pam_bus_log_create_error(pamh, r); |
| 1243 |
|
| 1244 |
r = sd_bus_call(bus, m, LOGIN_SLOW_BUS_CALL_TIMEOUT_USEC, &error, &reply); |
| 1245 |
} |
| 1246 |
if (r < 0) { |
| 1247 |
if (sd_bus_error_has_name(&error, BUS_ERROR_SESSION_BUSY)) { |
| 1248 |
/* We are already in a session, don't do anything */ |
| 1249 |
pam_debug_syslog(pamh, debug, |
| 1250 |
"Not creating session: %s", bus_error_message(&error, r)); |
| 1251 |
*ret_seat = *ret_type = *ret_runtime_dir = NULL; |
| 1252 |
return PAM_SUCCESS; |
| 1253 |
} |
| 1254 |
|
| 1255 |
sym_pam_syslog(pamh, LOG_ERR, "Failed to create session: %s", bus_error_message(&error, r)); |
| 1256 |
return PAM_SESSION_ERR; |
| 1257 |
} |
| 1258 |
|
| 1259 |
int session_fd; |
| 1260 |
r = sd_bus_message_read( |
| 1261 |
reply, |
| 1262 |
"soshusub", |
| 1263 |
&id, |
| 1264 |
&object_path, |
| 1265 |
&runtime_path, |
| 1266 |
&session_fd, |
| 1267 |
&original_uid, |
| 1268 |
&real_seat, |
| 1269 |
&real_vtnr, |
| 1270 |
/* existing = */ NULL); /* deprecated in b80120c4cba7d134b5437a58437a23fdf7ab2084 */ |
| 1271 |
if (r < 0) |
| 1272 |
return pam_bus_log_parse_error(pamh, r); |
| 1273 |
|
| 1274 |
/* Since v258, logind fully relies on pidfd to monitor the lifetime of the session leader |
| 1275 |
* process and returns a dummy session_fd (no longer a fifo). However because logind cannot |
| 1276 |
* be restarted (known long-standing issue), we must still be prepared to receive a fifo fd |
| 1277 |
* from a running logind older than v258. */ |
| 1278 |
if (sd_is_fifo(session_fd, NULL) > 0) { |
| 1279 |
_cleanup_close_ int fd = fcntl(session_fd, F_DUPFD_CLOEXEC, 3); |
| 1280 |
if (fd < 0) |
| 1281 |
return pam_syslog_errno(pamh, LOG_ERR, errno, "Failed to dup session fd: %m"); |
| 1282 |
|
| 1283 |
r = sym_pam_set_data(pamh, "systemd.session-fd", FD_TO_PTR(fd), NULL); |
| 1284 |
if (r != PAM_SUCCESS) |
| 1285 |
return pam_syslog_pam_error(pamh, LOG_ERR, r, "Failed to install session fd: @PAMERR@"); |
| 1286 |
TAKE_FD(fd); |
| 1287 |
} |
| 1288 |
} |
| 1289 |
|
| 1290 |
pam_debug_syslog(pamh, debug, |
| 1291 |
"Reply from logind: " |
| 1292 |
"id=%s object_path=%s runtime_path=%s seat=%s vtnr=%u original_uid=%u", |
| 1293 |
id, strna(object_path), runtime_path, real_seat, real_vtnr, original_uid); |
| 1294 |
|
| 1295 |
/* Please update manager_default_environment() in core/manager.c accordingly if more session envvars |
| 1296 |
* shall be added. */ |
| 1297 |
|
| 1298 |
r = update_environment(pamh, "XDG_SESSION_ID", id); |
| 1299 |
if (r != PAM_SUCCESS) |
| 1300 |
return r; |
| 1301 |
|
| 1302 |
/* Most likely we got the session/type/class from environment variables, but might have gotten the data |
| 1303 |
* somewhere else (for example PAM module parameters). Let's now update the environment variables, so that this |
| 1304 |
* data is inherited into the session processes, and programs can rely on them to be initialized. */ |
| 1305 |
|
| 1306 |
_cleanup_free_ char *real_type = strdup(c->type); /* make copy because this might point to env block, which we are going to update shortly */ |
| 1307 |
if (!real_type) |
| 1308 |
return pam_log_oom(pamh); |
| 1309 |
|
| 1310 |
r = update_environment(pamh, "XDG_SESSION_TYPE", c->type); |
| 1311 |
if (r != PAM_SUCCESS) |
| 1312 |
return r; |
| 1313 |
|
| 1314 |
r = update_environment(pamh, "XDG_SESSION_CLASS", c->class); |
| 1315 |
if (r != PAM_SUCCESS) |
| 1316 |
return r; |
| 1317 |
|
| 1318 |
r = update_environment(pamh, "XDG_SESSION_DESKTOP", c->desktop); |
| 1319 |
if (r != PAM_SUCCESS) |
| 1320 |
return r; |
| 1321 |
|
| 1322 |
_cleanup_free_ char *extra_devices = strv_join(c->extra_device_access, ":"); |
| 1323 |
if (!extra_devices) |
| 1324 |
return pam_log_oom(pamh); |
| 1325 |
|
| 1326 |
r = update_environment(pamh, "XDG_SESSION_EXTRA_DEVICE_ACCESS", extra_devices); |
| 1327 |
if (r != PAM_SUCCESS) |
| 1328 |
return r; |
| 1329 |
|
| 1330 |
r = update_environment(pamh, "XDG_SEAT", real_seat); |
| 1331 |
if (r != PAM_SUCCESS) |
| 1332 |
return r; |
| 1333 |
|
| 1334 |
if (real_vtnr > 0) { |
| 1335 |
char buf[DECIMAL_STR_MAX(real_vtnr)]; |
| 1336 |
xsprintf(buf, "%u", real_vtnr); |
| 1337 |
|
| 1338 |
r = update_environment(pamh, "XDG_VTNR", buf); |
| 1339 |
if (r != PAM_SUCCESS) |
| 1340 |
return r; |
| 1341 |
} |
| 1342 |
|
| 1343 |
/* Don't set $XDG_RUNTIME_DIR if the user we now authenticated for does not match the |
| 1344 |
* original user of the session. We do this in order not to result in privileged apps |
| 1345 |
* clobbering the runtime directory unnecessarily. */ |
| 1346 |
_cleanup_free_ char *rt = NULL; |
| 1347 |
if (original_uid == ur->uid && validate_runtime_directory(pamh, runtime_path, ur->uid)) |
| 1348 |
if (strdup_to(&rt, runtime_path) < 0) |
| 1349 |
return pam_log_oom(pamh); |
| 1350 |
|
| 1351 |
/* Everything worked, hence let's patch in the data we learned. Since 'real_set' points into the |
| 1352 |
* D-Bus message, let's copy it and return it as a buffer */ |
| 1353 |
_cleanup_free_ char *rs = NULL; |
| 1354 |
if (strdup_to(&rs, real_seat) < 0) |
| 1355 |
return pam_log_oom(pamh); |
| 1356 |
|
| 1357 |
c->vtnr = real_vtnr; |
| 1358 |
c->seat = *ret_seat = TAKE_PTR(rs); |
| 1359 |
c->type = *ret_type = TAKE_PTR(real_type); |
| 1360 |
*ret_runtime_dir = TAKE_PTR(rt); |
| 1361 |
|
| 1362 |
return PAM_SUCCESS; |
| 1363 |
} |
| 1364 |
|
| 1365 |
static int import_shell_credentials(pam_handle_t *pamh, bool debug) { |
| 1366 |
|
| 1367 |
static const char *const propagate[] = { |
| 1368 |
"shell.prompt.prefix", "SHELL_PROMPT_PREFIX", |
| 1369 |
"shell.prompt.suffix", "SHELL_PROMPT_SUFFIX", |
| 1370 |
"shell.welcome", "SHELL_WELCOME", |
| 1371 |
NULL |
| 1372 |
}; |
| 1373 |
int r; |
| 1374 |
|
| 1375 |
assert(pamh); |
| 1376 |
|
| 1377 |
STRV_FOREACH_PAIR(k, v, propagate) { |
| 1378 |
r = propagate_credential_to_environment(pamh, debug, *k, *v); |
| 1379 |
if (r != PAM_SUCCESS) |
| 1380 |
return r; |
| 1381 |
} |
| 1382 |
|
| 1383 |
return PAM_SUCCESS; |
| 1384 |
} |
| 1385 |
|
| 1386 |
static int mkdir_chown_open_directory( |
| 1387 |
int parent_fd, |
| 1388 |
const char *name, |
| 1389 |
uid_t uid, |
| 1390 |
gid_t gid, |
| 1391 |
mode_t mode) { |
| 1392 |
|
| 1393 |
_cleanup_free_ char *t = NULL; |
| 1394 |
int r; |
| 1395 |
|
| 1396 |
assert(parent_fd >= 0); |
| 1397 |
assert(name); |
| 1398 |
assert(uid_is_valid(uid)); |
| 1399 |
assert(gid_is_valid(gid)); |
| 1400 |
assert(mode != MODE_INVALID); |
| 1401 |
|
| 1402 |
for (unsigned attempt = 0;; attempt++) { |
| 1403 |
_cleanup_close_ int fd = openat(parent_fd, name, O_CLOEXEC|O_DIRECTORY|O_NOFOLLOW); |
| 1404 |
if (fd >= 0) |
| 1405 |
return TAKE_FD(fd); |
| 1406 |
if (errno != ENOENT) |
| 1407 |
return -errno; |
| 1408 |
|
| 1409 |
/* Let's create the directory under a temporary name first, since we want to make sure that |
| 1410 |
* once it appears under the right name it has the right ownership */ |
| 1411 |
r = tempfn_random(name, /* extra= */ NULL, &t); |
| 1412 |
if (r < 0) |
| 1413 |
return r; |
| 1414 |
|
| 1415 |
fd = open_mkdir_at(parent_fd, t, O_CLOEXEC|O_EXCL, 0700); /* Use restrictive mode until ownership is in order */ |
| 1416 |
if (fd < 0) |
| 1417 |
return fd; |
| 1418 |
|
| 1419 |
r = RET_NERRNO(fchown(fd, uid, gid)); |
| 1420 |
if (r < 0) |
| 1421 |
goto fail; |
| 1422 |
|
| 1423 |
r = RET_NERRNO(fchmod(fd, mode)); |
| 1424 |
if (r < 0) |
| 1425 |
goto fail; |
| 1426 |
|
| 1427 |
r = rename_noreplace(parent_fd, t, parent_fd, name); |
| 1428 |
if (r >= 0) |
| 1429 |
return TAKE_FD(fd); |
| 1430 |
if (r != -EEXIST || attempt >= 5) |
| 1431 |
goto fail; |
| 1432 |
|
| 1433 |
/* Maybe some other login attempt created the directory at the same time? Let's retry */ |
| 1434 |
(void) unlinkat(parent_fd, t, AT_REMOVEDIR); |
| 1435 |
t = mfree(t); |
| 1436 |
} |
| 1437 |
|
| 1438 |
fail: |
| 1439 |
(void) unlinkat(parent_fd, ASSERT_PTR(t), AT_REMOVEDIR); |
| 1440 |
return r; |
| 1441 |
} |
| 1442 |
|
| 1443 |
static int make_area_runtime_directory( |
| 1444 |
pam_handle_t *pamh, |
| 1445 |
UserRecord *ur, |
| 1446 |
const char *runtime_directory, |
| 1447 |
const char *area, |
| 1448 |
char **ret) { |
| 1449 |
|
| 1450 |
assert(pamh); |
| 1451 |
assert(ur); |
| 1452 |
assert(runtime_directory); |
| 1453 |
assert(area); |
| 1454 |
assert(ret); |
| 1455 |
|
| 1456 |
/* Let's be careful with creating these directories, the runtime directory is owned by the user after all, |
| 1457 |
* and they might play symlink games with us. */ |
| 1458 |
|
| 1459 |
_cleanup_close_ int fd = open(runtime_directory, O_CLOEXEC|O_PATH|O_DIRECTORY); |
| 1460 |
if (fd < 0) |
| 1461 |
return pam_syslog_errno(pamh, LOG_ERR, errno, "Unable to open runtime directory '%s': %m", runtime_directory); |
| 1462 |
|
| 1463 |
_cleanup_close_ int fd_areas = mkdir_chown_open_directory(fd, "Areas", ur->uid, user_record_gid(ur), 0755); |
| 1464 |
if (fd_areas < 0) |
| 1465 |
return pam_syslog_errno(pamh, LOG_ERR, fd_areas, "Unable to create 'Areas' directory below '%s': %m", runtime_directory); |
| 1466 |
|
| 1467 |
_cleanup_close_ int fd_area = mkdir_chown_open_directory(fd_areas, area, ur->uid, user_record_gid(ur), 0755); |
| 1468 |
if (fd_area < 0) |
| 1469 |
return pam_syslog_errno(pamh, LOG_ERR, fd_area, "Unable to create '%s' directory below '%s/Areas': %m", area, runtime_directory); |
| 1470 |
|
| 1471 |
char *j = path_join(runtime_directory, "Areas", area); |
| 1472 |
if (!j) |
| 1473 |
return pam_log_oom(pamh); |
| 1474 |
|
| 1475 |
*ret = j; |
| 1476 |
return 0; |
| 1477 |
} |
| 1478 |
|
| 1479 |
static int export_legacy_dbus_address(pam_handle_t *pamh, const char *runtime) { |
| 1480 |
|
| 1481 |
assert(pamh); |
| 1482 |
assert(runtime); |
| 1483 |
|
| 1484 |
/* We need to export $DBUS_SESSION_BUS_ADDRESS because various applications will not connect |
| 1485 |
* correctly to the bus without it. This setting matches what dbus.socket does for the user session |
| 1486 |
* using 'systemctl --user set-environment'. We want to have the same configuration in processes |
| 1487 |
* started from the PAM session. |
| 1488 |
* |
| 1489 |
* The setting of the address is guarded by the access() check because it is also possible to compile |
| 1490 |
* dbus without --enable-user-session, in which case this socket is not used, and |
| 1491 |
* $DBUS_SESSION_BUS_ADDRESS should not be set. An alternative approach would to not do the access() |
| 1492 |
* check here, and let applications try on their own, by using "unix:path=%s/bus;autolaunch:". But we |
| 1493 |
* expect the socket to be present by the time we do this check, so we can just as well check once |
| 1494 |
* here. */ |
| 1495 |
|
| 1496 |
const char *s = strjoina(runtime, "/bus"); |
| 1497 |
if (access(s, F_OK) < 0) { |
| 1498 |
if (errno != ENOENT) |
| 1499 |
pam_syslog_errno(pamh, LOG_WARNING, errno, "Failed to check if %s/bus exists, ignoring: %m", runtime); |
| 1500 |
|
| 1501 |
return PAM_SUCCESS; |
| 1502 |
} |
| 1503 |
|
| 1504 |
_cleanup_free_ char *t = NULL; |
| 1505 |
if (asprintf(&t, DEFAULT_USER_BUS_ADDRESS_FMT, runtime) < 0) |
| 1506 |
return pam_log_oom(pamh); |
| 1507 |
|
| 1508 |
return update_environment(pamh, "DBUS_SESSION_BUS_ADDRESS", t); |
| 1509 |
} |
| 1510 |
|
| 1511 |
static int setup_runtime_directory( |
| 1512 |
pam_handle_t *pamh, |
| 1513 |
UserRecord *ur, |
| 1514 |
const char *runtime_directory, |
| 1515 |
const char *area) { |
| 1516 |
|
| 1517 |
int r; |
| 1518 |
|
| 1519 |
assert(pamh); |
| 1520 |
assert(ur); |
| 1521 |
|
| 1522 |
if (!runtime_directory) { |
| 1523 |
/* If this is an area switch request, always reset $XDG_RUNTIME_DIR if we got nothing |
| 1524 |
* to ensure the main runtime dir won't be clobbered. */ |
| 1525 |
if (area) |
| 1526 |
return update_environment(pamh, "XDG_RUNTIME_DIR", NULL); |
| 1527 |
|
| 1528 |
return PAM_SUCCESS; |
| 1529 |
} |
| 1530 |
|
| 1531 |
/* Also create a per-area subdirectory for $XDG_RUNTIME_DIR, so that each area has their own |
| 1532 |
* set of runtime services. We follow the same directory structure as for $HOME. Note that we |
| 1533 |
* do not define any form of automatic clean-up for the per-area subdirs beyond the regular |
| 1534 |
* clean-up of the whole $XDG_RUNTIME_DIR hierarchy when the user finally logs out. */ |
| 1535 |
_cleanup_free_ char *per_area_runtime_directory = NULL; |
| 1536 |
if (area) { |
| 1537 |
r = make_area_runtime_directory(pamh, ur, runtime_directory, area, &per_area_runtime_directory); |
| 1538 |
if (r != PAM_SUCCESS) |
| 1539 |
return r; |
| 1540 |
|
| 1541 |
runtime_directory = per_area_runtime_directory; |
| 1542 |
} |
| 1543 |
|
| 1544 |
r = update_environment(pamh, "XDG_RUNTIME_DIR", runtime_directory); |
| 1545 |
if (r != PAM_SUCCESS) |
| 1546 |
return r; |
| 1547 |
|
| 1548 |
return export_legacy_dbus_address(pamh, runtime_directory); |
| 1549 |
} |
| 1550 |
|
| 1551 |
static int setup_environment( |
| 1552 |
pam_handle_t *pamh, |
| 1553 |
UserRecord *ur, |
| 1554 |
const char *runtime_directory, |
| 1555 |
const char *area, |
| 1556 |
bool debug) { |
| 1557 |
|
| 1558 |
int r; |
| 1559 |
|
| 1560 |
assert(pamh); |
| 1561 |
assert(ur); |
| 1562 |
|
| 1563 |
const char *h = ASSERT_PTR(user_record_home_directory(ur)); |
| 1564 |
|
| 1565 |
/* If an empty area string is specified, this means an explicit: do not use the area logic, normalize this here */ |
| 1566 |
area = empty_to_null(area); |
| 1567 |
|
| 1568 |
_cleanup_free_ char *ha = NULL, *area_copy = NULL; |
| 1569 |
if (area) { |
| 1570 |
_cleanup_free_ char *j = path_join(h, "Areas", area); |
| 1571 |
if (!j) |
| 1572 |
return pam_log_oom(pamh); |
| 1573 |
|
| 1574 |
_cleanup_close_ int fd = -EBADF; |
| 1575 |
r = chase(j, /* root= */ NULL, CHASE_MUST_BE_DIRECTORY, &ha, &fd); |
| 1576 |
if (r < 0) { |
| 1577 |
/* Log the precise error */ |
| 1578 |
pam_syslog_errno(pamh, LOG_WARNING, r, "Path '%s' of requested user area '%s' is not accessible, reverting to regular home directory: %m", j, area); |
| 1579 |
|
| 1580 |
/* Also tell the user directly at login, but a bit more vague */ |
| 1581 |
sym_pam_info(pamh, "Path '%s' of requested user area '%s' is not accessible, reverting to regular home directory.", j, area); |
| 1582 |
area = NULL; |
| 1583 |
} else { |
| 1584 |
/* Validate that the target is definitely owned by user */ |
| 1585 |
struct stat st; |
| 1586 |
if (fstat(fd, &st) < 0) |
| 1587 |
return pam_syslog_errno(pamh, LOG_ERR, errno, "Unable to fstat() target area directory '%s': %m", ha); |
| 1588 |
|
| 1589 |
if (st.st_uid != ur->uid) { |
| 1590 |
sym_pam_syslog(pamh, LOG_ERR, "Path '%s' of requested user area '%s' is not owned by user, reverting to regular home directory.", ha, area); |
| 1591 |
|
| 1592 |
/* Also tell the user directly at login. */ |
| 1593 |
sym_pam_info(pamh, "Path '%s' of requested user area '%s' is not owned by user, reverting to regular home directory.", ha, area); |
| 1594 |
area = NULL; |
| 1595 |
} else { |
| 1596 |
/* All good, now make a copy of the area string, since we quite likely are |
| 1597 |
* going to invalidate it (if it points into the environment block), via the |
| 1598 |
* update_environment() call below */ |
| 1599 |
area_copy = strdup(area); |
| 1600 |
if (!area_copy) |
| 1601 |
return pam_log_oom(pamh); |
| 1602 |
|
| 1603 |
pam_debug_syslog(pamh, debug, "Area '%s' selected, setting $HOME to '%s'.", area, ha); |
| 1604 |
h = ha; |
| 1605 |
area = area_copy; |
| 1606 |
} |
| 1607 |
} |
| 1608 |
} |
| 1609 |
|
| 1610 |
r = update_environment(pamh, "XDG_AREA", area); |
| 1611 |
if (r != PAM_SUCCESS) |
| 1612 |
return r; |
| 1613 |
|
| 1614 |
r = update_environment(pamh, "HOME", h); |
| 1615 |
if (r != PAM_SUCCESS) |
| 1616 |
return r; |
| 1617 |
|
| 1618 |
return setup_runtime_directory(pamh, ur, runtime_directory, area); |
| 1619 |
} |
| 1620 |
|
| 1621 |
static int open_osc_context(pam_handle_t *pamh, const char *session_type, UserRecord *ur, bool debug) { |
| 1622 |
int r; |
| 1623 |
|
| 1624 |
assert(pamh); |
| 1625 |
assert(ur); |
| 1626 |
|
| 1627 |
/* If this is a TTY session, then output the session start OSC sequence */ |
| 1628 |
|
| 1629 |
if (!streq_ptr(session_type, "tty")) |
| 1630 |
return PAM_SUCCESS; |
| 1631 |
|
| 1632 |
const char *e = sym_pam_getenv(pamh, "TERM"); |
| 1633 |
if (!e) |
| 1634 |
e = getenv("TERM"); |
| 1635 |
if (streq_ptr(e, "dumb")) |
| 1636 |
return PAM_SUCCESS; |
| 1637 |
|
| 1638 |
/* NB: we output directly to stdout, instead of going via sym_pam_info() or so, because that's too |
| 1639 |
* high-level for us, as it suffixes the output with a newline, expecting a full blown text message |
| 1640 |
* as prompt string, not just an ANSI sequence. Note that PAM's conv_misc() actually goes to stdout |
| 1641 |
* anyway, hence let's do so here too, but only after careful validation. */ |
| 1642 |
if (!isatty_safe(STDOUT_FILENO)) |
| 1643 |
return PAM_SUCCESS; |
| 1644 |
|
| 1645 |
/* Keep a reference to the TTY we are operating on, so that we can issue the OSC close sequence also |
| 1646 |
* if the TTY is already closed. We use an O_PATH reference here, rather than a properly opened fd, |
| 1647 |
* so that we don't delay tty hang-up. */ |
| 1648 |
_cleanup_close_ int tty_opath_fd = fd_reopen(STDOUT_FILENO, O_PATH|O_CLOEXEC); |
| 1649 |
if (tty_opath_fd < 0) |
| 1650 |
pam_debug_syslog_errno(pamh, debug, tty_opath_fd, "Failed to pin TTY, ignoring: %m"); |
| 1651 |
else |
| 1652 |
tty_opath_fd = fd_move_above_stdio(tty_opath_fd); |
| 1653 |
|
| 1654 |
_cleanup_free_ char *osc = NULL; |
| 1655 |
sd_id128_t osc_id; |
| 1656 |
r = osc_context_open_session( |
| 1657 |
ur->user_name, |
| 1658 |
sym_pam_getenv(pamh, "XDG_SESSION_ID"), |
| 1659 |
&osc, |
| 1660 |
&osc_id); |
| 1661 |
if (r < 0) |
| 1662 |
return pam_syslog_errno(pamh, LOG_ERR, r, "Failed to prepare OSC sequence: %m"); |
| 1663 |
|
| 1664 |
r = loop_write(STDOUT_FILENO, osc, SIZE_MAX); |
| 1665 |
if (r < 0) |
| 1666 |
return pam_syslog_errno(pamh, LOG_ERR, r, "Failed to write OSC sequence to TTY: %m"); |
| 1667 |
|
| 1668 |
/* Remember the OSC context id, so that we can close it cleanly later */ |
| 1669 |
_cleanup_free_ sd_id128_t *osc_id_copy = newdup(sd_id128_t, &osc_id, 1); |
| 1670 |
if (!osc_id_copy) |
| 1671 |
return pam_log_oom(pamh); |
| 1672 |
|
| 1673 |
r = sym_pam_set_data(pamh, "systemd.osc-context-id", osc_id_copy, pam_cleanup_free); |
| 1674 |
if (r != PAM_SUCCESS) |
| 1675 |
return pam_syslog_pam_error(pamh, LOG_ERR, r, |
| 1676 |
"Failed to set PAM OSC sequence ID data: @PAMERR@"); |
| 1677 |
|
| 1678 |
TAKE_PTR(osc_id_copy); |
| 1679 |
|
| 1680 |
if (tty_opath_fd >= 0) { |
| 1681 |
r = sym_pam_set_data(pamh, "systemd.osc-context-fd", FD_TO_PTR(tty_opath_fd), pam_cleanup_close); |
| 1682 |
if (r != PAM_SUCCESS) |
| 1683 |
return pam_syslog_pam_error(pamh, LOG_ERR, r, |
| 1684 |
"Failed to set PAM OSC sequence fd data: @PAMERR@"); |
| 1685 |
|
| 1686 |
TAKE_FD(tty_opath_fd); |
| 1687 |
} |
| 1688 |
|
| 1689 |
return PAM_SUCCESS; |
| 1690 |
} |
| 1691 |
|
| 1692 |
static int close_osc_context(pam_handle_t *pamh, bool debug) { |
| 1693 |
int r; |
| 1694 |
|
| 1695 |
assert(pamh); |
| 1696 |
|
| 1697 |
const void *p; |
| 1698 |
int tty_opath_fd = -EBADF; |
| 1699 |
r = sym_pam_get_data(pamh, "systemd.osc-context-fd", &p); |
| 1700 |
if (r == PAM_SUCCESS) |
| 1701 |
tty_opath_fd = PTR_TO_FD(p); |
| 1702 |
else if (r != PAM_NO_MODULE_DATA) |
| 1703 |
return pam_syslog_pam_error(pamh, LOG_ERR, r, "Failed to get PAM OSC context fd: @PAMERR@"); |
| 1704 |
if (tty_opath_fd < 0) |
| 1705 |
return PAM_SUCCESS; |
| 1706 |
|
| 1707 |
const sd_id128_t *osc_id = NULL; |
| 1708 |
r = sym_pam_get_data(pamh, "systemd.osc-context-id", (const void**) &osc_id); |
| 1709 |
if (!IN_SET(r, PAM_SUCCESS, PAM_NO_MODULE_DATA)) |
| 1710 |
return pam_syslog_pam_error(pamh, LOG_ERR, r, "Failed to get PAM OSC context id data: @PAMERR@"); |
| 1711 |
if (!osc_id) |
| 1712 |
return PAM_SUCCESS; |
| 1713 |
|
| 1714 |
/* Now open the original TTY again, so that we can write on it */ |
| 1715 |
_cleanup_close_ int fd = fd_reopen(tty_opath_fd, O_WRONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY); |
| 1716 |
if (fd < 0) { |
| 1717 |
pam_debug_syslog_errno(pamh, debug, fd, "Failed to reopen TTY, ignoring: %m"); |
| 1718 |
return PAM_SUCCESS; |
| 1719 |
} |
| 1720 |
|
| 1721 |
/* /bin/login calls us with fds 0, 1, 2 closed, which is just weird. Let's step outside of that |
| 1722 |
* range, just in case sym_pam_syslog() or so logs to stderr */ |
| 1723 |
fd = fd_move_above_stdio(fd); |
| 1724 |
|
| 1725 |
/* Safety check, let's verify this is a valid TTY we just opened */ |
| 1726 |
if (!isatty_safe(fd)) |
| 1727 |
return PAM_SUCCESS; |
| 1728 |
|
| 1729 |
_cleanup_free_ char *osc = NULL; |
| 1730 |
r = osc_context_close(*osc_id, &osc); |
| 1731 |
if (r < 0) |
| 1732 |
return pam_syslog_errno(pamh, LOG_ERR, r, "Failed to prepare OSC sequence: %m"); |
| 1733 |
|
| 1734 |
/* When we are closing things, the TTY might not take our writes anymore. Accept that gracefully. */ |
| 1735 |
r = loop_write(fd, osc, SIZE_MAX); |
| 1736 |
if (r < 0) |
| 1737 |
pam_debug_syslog_errno(pamh, debug, r, "Failed to write OSC sequence to TTY, ignoring: %m"); |
| 1738 |
|
| 1739 |
return PAM_SUCCESS; |
| 1740 |
} |
| 1741 |
|
| 1742 |
_public_ PAM_EXTERN int pam_sm_open_session( |
| 1743 |
pam_handle_t *pamh, |
| 1744 |
int flags, |
| 1745 |
int argc, const char **argv) { |
| 1746 |
|
| 1747 |
int r; |
| 1748 |
|
| 1749 |
assert(pamh); |
| 1750 |
|
| 1751 |
r = DLOPEN_LIBPAM(LOG_DEBUG, SD_ELF_NOTE_DLOPEN_PRIORITY_REQUIRED); |
| 1752 |
if (r < 0) |
| 1753 |
return PAM_SERVICE_ERR; |
| 1754 |
|
| 1755 |
pam_log_setup(); |
| 1756 |
|
| 1757 |
uint64_t default_capability_bounding_set = CAP_MASK_UNSET, default_capability_ambient_set = CAP_MASK_UNSET; |
| 1758 |
const char *class_pam = NULL, *type_pam = NULL, *desktop_pam = NULL, *area_pam = NULL; |
| 1759 |
bool debug = false; |
| 1760 |
if (parse_argv(pamh, |
| 1761 |
argc, argv, |
| 1762 |
&class_pam, |
| 1763 |
&type_pam, |
| 1764 |
&desktop_pam, |
| 1765 |
&area_pam, |
| 1766 |
&debug, |
| 1767 |
&default_capability_bounding_set, |
| 1768 |
&default_capability_ambient_set) < 0) |
| 1769 |
return PAM_SESSION_ERR; |
| 1770 |
|
| 1771 |
pam_debug_syslog(pamh, debug, "pam-systemd: initializing..."); |
| 1772 |
|
| 1773 |
_cleanup_(user_record_unrefp) UserRecord *ur = NULL; |
| 1774 |
r = acquire_user_record(pamh, &ur); |
| 1775 |
if (r != PAM_SUCCESS) |
| 1776 |
return r; |
| 1777 |
|
| 1778 |
_cleanup_(session_context_done) SessionContext c = {}; |
| 1779 |
r = pam_get_item_many( |
| 1780 |
pamh, |
| 1781 |
PAM_SERVICE, &c.service, |
| 1782 |
PAM_XDISPLAY, &c.display, |
| 1783 |
PAM_TTY, &c.tty, |
| 1784 |
PAM_RUSER, &c.remote_user, |
| 1785 |
PAM_RHOST, &c.remote_host); |
| 1786 |
if (r != PAM_SUCCESS) |
| 1787 |
return pam_syslog_pam_error(pamh, LOG_ERR, r, "Failed to get PAM items: @PAMERR@"); |
| 1788 |
|
| 1789 |
c.seat = getenv_harder(pamh, "XDG_SEAT", NULL); |
| 1790 |
c.vtnr = getenv_harder_uint32(pamh, "XDG_VTNR", 0); |
| 1791 |
c.type = getenv_harder(pamh, "XDG_SESSION_TYPE", type_pam); |
| 1792 |
c.class = getenv_harder(pamh, "XDG_SESSION_CLASS", class_pam); |
| 1793 |
c.desktop = getenv_harder(pamh, "XDG_SESSION_DESKTOP", desktop_pam); |
| 1794 |
c.area = getenv_harder(pamh, "XDG_AREA", area_pam); |
| 1795 |
c.incomplete = getenv_harder_bool(pamh, "XDG_SESSION_INCOMPLETE", false); |
| 1796 |
|
| 1797 |
const char *extra_device_access = getenv_harder(pamh, "XDG_SESSION_EXTRA_DEVICE_ACCESS", NULL); |
| 1798 |
if (extra_device_access) { |
| 1799 |
c.extra_device_access = strv_split(extra_device_access, ":"); |
| 1800 |
if (!c.extra_device_access) |
| 1801 |
return pam_log_oom(pamh); |
| 1802 |
} |
| 1803 |
|
| 1804 |
r = pam_get_data_many( |
| 1805 |
pamh, |
| 1806 |
"systemd.memory_max", &c.memory_max, |
| 1807 |
"systemd.tasks_max", &c.tasks_max, |
| 1808 |
"systemd.cpu_weight", &c.cpu_weight, |
| 1809 |
"systemd.io_weight", &c.io_weight, |
| 1810 |
"systemd.runtime_max_sec", &c.runtime_max_sec); |
| 1811 |
if (r != PAM_SUCCESS) |
| 1812 |
return pam_syslog_pam_error(pamh, LOG_ERR, r, "Failed to get PAM data: @PAMERR@"); |
| 1813 |
|
| 1814 |
session_context_mangle(pamh, &c, ur, debug); |
| 1815 |
|
| 1816 |
_cleanup_free_ char *seat_buffer = NULL, *type_buffer = NULL, *runtime_dir = NULL; |
| 1817 |
r = register_session(pamh, &c, ur, debug, &seat_buffer, &type_buffer, &runtime_dir); |
| 1818 |
if (r != PAM_SUCCESS) |
| 1819 |
return r; |
| 1820 |
|
| 1821 |
r = import_shell_credentials(pamh, debug); |
| 1822 |
if (r != PAM_SUCCESS) |
| 1823 |
return r; |
| 1824 |
|
| 1825 |
r = setup_environment(pamh, ur, runtime_dir, c.area, debug); |
| 1826 |
if (r != PAM_SUCCESS) |
| 1827 |
return r; |
| 1828 |
|
| 1829 |
if (default_capability_ambient_set == CAP_MASK_UNSET) |
| 1830 |
default_capability_ambient_set = pick_default_capability_ambient_set(ur, c.service, c.seat); |
| 1831 |
|
| 1832 |
r = apply_user_record_settings(pamh, ur, debug, default_capability_bounding_set, default_capability_ambient_set); |
| 1833 |
if (r != PAM_SUCCESS) |
| 1834 |
return r; |
| 1835 |
|
| 1836 |
return open_osc_context(pamh, c.type, ur, debug); |
| 1837 |
} |
| 1838 |
|
| 1839 |
_public_ PAM_EXTERN int pam_sm_close_session( |
| 1840 |
pam_handle_t *pamh, |
| 1841 |
int flags, |
| 1842 |
int argc, const char **argv) { |
| 1843 |
|
| 1844 |
bool debug = false; |
| 1845 |
const char *id; |
| 1846 |
int r; |
| 1847 |
|
| 1848 |
assert(pamh); |
| 1849 |
|
| 1850 |
r = DLOPEN_LIBPAM(LOG_DEBUG, SD_ELF_NOTE_DLOPEN_PRIORITY_REQUIRED); |
| 1851 |
if (r < 0) |
| 1852 |
return PAM_SERVICE_ERR; |
| 1853 |
|
| 1854 |
pam_log_setup(); |
| 1855 |
|
| 1856 |
if (parse_argv(pamh, |
| 1857 |
argc, argv, |
| 1858 |
/* class= */ NULL, |
| 1859 |
/* type= */ NULL, |
| 1860 |
/* desktop= */ NULL, |
| 1861 |
/* area= */ NULL, |
| 1862 |
&debug, |
| 1863 |
/* default_capability_bounding_set= */ NULL, |
| 1864 |
/* default_capability_ambient_set= */ NULL) < 0) |
| 1865 |
return PAM_SESSION_ERR; |
| 1866 |
|
| 1867 |
pam_debug_syslog(pamh, debug, "pam-systemd: shutting down..."); |
| 1868 |
|
| 1869 |
(void) close_osc_context(pamh, debug); |
| 1870 |
|
| 1871 |
id = sym_pam_getenv(pamh, "XDG_SESSION_ID"); |
| 1872 |
if (id) { |
| 1873 |
_cleanup_(sd_varlink_unrefp) sd_varlink *vl = NULL; |
| 1874 |
bool done = false; |
| 1875 |
|
| 1876 |
r = sd_varlink_connect_address(&vl, "/run/systemd/io.systemd.Login"); |
| 1877 |
if (r < 0) |
| 1878 |
pam_debug_syslog_errno(pamh, debug, r, "Failed to connect to logind via Varlink, falling back to D-Bus: %m"); |
| 1879 |
else { |
| 1880 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *vreply = NULL; |
| 1881 |
const char *error_id = NULL; |
| 1882 |
r = sd_varlink_callbo( |
| 1883 |
vl, |
| 1884 |
"io.systemd.Login.ReleaseSession", |
| 1885 |
/* ret_reply= */ NULL, |
| 1886 |
&error_id, |
| 1887 |
SD_JSON_BUILD_PAIR_STRING("Id", id)); |
| 1888 |
if (r < 0) |
| 1889 |
return pam_syslog_errno(pamh, LOG_ERR, r, |
| 1890 |
"Failed to issue io.systemd.Login.ReleaseSession varlink call: %m"); |
| 1891 |
if (error_id) |
| 1892 |
return pam_syslog_errno(pamh, LOG_ERR, sd_varlink_error_to_errno(error_id, vreply), |
| 1893 |
"Varlink call io.systemd.Login.ReleaseSession failed: %s", error_id); |
| 1894 |
|
| 1895 |
done = true; |
| 1896 |
} |
| 1897 |
|
| 1898 |
if (!done) { |
| 1899 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 1900 |
_cleanup_(pam_bus_data_disconnectp) PamBusData *d = NULL; |
| 1901 |
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
| 1902 |
|
| 1903 |
/* Before we go and close the FIFO we need to tell logind that this is a clean session |
| 1904 |
* shutdown, so that it doesn't just go and slaughter us immediately after closing the fd */ |
| 1905 |
|
| 1906 |
r = pam_acquire_bus_connection(pamh, "pam-systemd", debug, &bus, &d); |
| 1907 |
if (r != PAM_SUCCESS) |
| 1908 |
return r; |
| 1909 |
|
| 1910 |
r = bus_call_method(bus, bus_login_mgr, "ReleaseSession", &error, NULL, "s", id); |
| 1911 |
if (r < 0) |
| 1912 |
return pam_syslog_pam_error(pamh, LOG_ERR, PAM_SESSION_ERR, |
| 1913 |
"Failed to release session: %s", bus_error_message(&error, r)); |
| 1914 |
} |
| 1915 |
} |
| 1916 |
|
| 1917 |
return PAM_SUCCESS; |
| 1918 |
} |