Simple pam_systemd clean-ups (#35919)

This commit is contained in:
Lennart Poettering
2025-01-08 15:03:36 +01:00
committed by GitHub
3 changed files with 61 additions and 39 deletions
+57 -35
View File
@@ -85,6 +85,8 @@ static int get_sender_session(
const char *name;
int r;
assert(m);
/* Acquire the sender's session. This first checks if the sending process is inside a session itself,
* and returns that. If not and 'consult_display' is true, this returns the display session of the
* owning user of the caller. */
@@ -827,9 +829,56 @@ static int method_list_inhibitors(sd_bus_message *message, void *userdata, sd_bu
return sd_bus_send(NULL, reply, NULL);
}
static int manager_choose_session_id(
Manager *m,
const PidRef *leader,
char **ret_id) {
int r;
assert(m);
assert(pidref_is_set(leader));
assert(ret_id);
/* Try to keep our session IDs and the audit session IDs in sync */
_cleanup_free_ char *id = NULL;
uint32_t audit_id = AUDIT_SESSION_INVALID;
r = audit_session_from_pid(leader, &audit_id);
if (r < 0) {
if (r != -ENODATA)
log_debug_errno(r, "Failed to read audit session ID of process " PID_FMT ", ignoring: %m", leader->pid);
} else {
if (asprintf(&id, "%"PRIu32, audit_id) < 0)
return -ENOMEM;
/* Wut? There's already a session by this name and we didn't find it above? Weird, then let's
* not trust the audit data and let's better register a new ID */
if (hashmap_contains(m->sessions, id)) {
log_warning("Existing logind session ID %s used by new audit session, ignoring.", id);
id = mfree(id);
}
}
if (!id)
do {
id = mfree(id);
if (asprintf(&id, "c%" PRIu64, ++m->session_counter) < 0)
return -ENOMEM;
} while (hashmap_contains(m->sessions, id));
/* The generated names should not clash with 'auto' or 'self' */
assert(!SESSION_IS_SELF(id));
assert(!SESSION_IS_AUTO(id));
*ret_id = TAKE_PTR(id);
return 0;
}
static int create_session(
Manager *m,
sd_bus_message *message,
void *userdata,
sd_bus_error *error,
uid_t uid,
pid_t leader_pid,
@@ -848,16 +897,15 @@ static int create_session(
uint64_t flags) {
_cleanup_(pidref_done) PidRef leader = PIDREF_NULL;
Manager *m = ASSERT_PTR(userdata);
_cleanup_free_ char *id = NULL;
Session *session = NULL;
uint32_t audit_id = 0;
User *user = NULL;
Seat *seat = NULL;
SessionType t;
SessionClass c;
int r;
assert(m);
assert(message);
if (!uid_is_valid(uid))
@@ -1006,35 +1054,9 @@ static int create_session(
"Maximum number of sessions (%" PRIu64 ") reached, refusing further sessions.",
m->sessions_max);
(void) audit_session_from_pid(&leader, &audit_id);
if (audit_session_is_valid(audit_id)) {
/* Keep our session IDs and the audit session IDs in sync */
if (asprintf(&id, "%"PRIu32, audit_id) < 0)
return -ENOMEM;
/* Wut? There's already a session by this name and we didn't find it above? Weird, then let's
* not trust the audit data and let's better register a new ID */
if (hashmap_contains(m->sessions, id)) {
log_warning("Existing logind session ID %s used by new audit session, ignoring.", id);
audit_id = AUDIT_SESSION_INVALID;
id = mfree(id);
}
}
if (!id) {
do {
id = mfree(id);
if (asprintf(&id, "c%" PRIu64, ++m->session_counter) < 0)
return -ENOMEM;
} while (hashmap_contains(m->sessions, id));
}
/* The generated names should not clash with 'auto' or 'self' */
assert(!SESSION_IS_SELF(id));
assert(!SESSION_IS_AUTO(id));
r = manager_choose_session_id(m, &leader, &id);
if (r < 0)
return r;
/* If we are not watching utmp already, try again */
manager_reconnect_utmp(m);
@@ -1130,7 +1152,7 @@ static int create_session(
* all is complete - or wait again. */
r = session_send_create_reply(session, /* error= */ NULL);
if (r < 0)
return r;
goto fail;
return 1;
@@ -1176,8 +1198,8 @@ static int method_create_session(sd_bus_message *message, void *userdata, sd_bus
return r;
return create_session(
message,
userdata,
message,
error,
uid,
leader_pid,
@@ -1224,8 +1246,8 @@ static int method_create_session_pidfd(sd_bus_message *message, void *userdata,
return r;
return create_session(
message,
userdata,
message,
error,
uid,
/* leader_pid = */ 0,
+2 -2
View File
@@ -912,7 +912,7 @@ static bool session_job_pending(Session *s) {
(SESSION_CLASS_WANTS_SERVICE_MANAGER(s->class) && s->user->service_manager_job);
}
int session_send_create_reply(Session *s, sd_bus_error *error) {
int session_send_create_reply(Session *s, const sd_bus_error *error) {
_cleanup_(sd_bus_message_unrefp) sd_bus_message *c = NULL;
_cleanup_close_ int fifo_fd = -EBADF;
_cleanup_free_ char *p = NULL;
@@ -968,7 +968,7 @@ int session_send_create_reply(Session *s, sd_bus_error *error) {
false);
}
int session_send_upgrade_reply(Session *s, sd_bus_error *error) {
int session_send_upgrade_reply(Session *s, const sd_bus_error *error) {
_cleanup_(sd_bus_message_unrefp) sd_bus_message *c = NULL;
assert(s);
+2 -2
View File
@@ -15,8 +15,8 @@ int session_send_changed(Session *s, const char *properties, ...) _sentinel_;
int session_send_lock(Session *s, bool lock);
int session_send_lock_all(Manager *m, bool lock);
int session_send_create_reply(Session *s, sd_bus_error *error);
int session_send_upgrade_reply(Session *s, sd_bus_error *error);
int session_send_create_reply(Session *s, const sd_bus_error *error);
int session_send_upgrade_reply(Session *s, const sd_bus_error *error);
int bus_session_method_activate(sd_bus_message *message, void *userdata, sd_bus_error *error);
int bus_session_method_lock(sd_bus_message *message, void *userdata, sd_bus_error *error);