Blame src/nm-session-monitor.c

Packit Service 87a54e
/* SPDX-License-Identifier: GPL-2.0-or-later */
Packit 5756e2
/*
Packit 5756e2
 * Copyright (C) 2008 - 2015 Red Hat, Inc.
Packit 5756e2
 * Author: David Zeuthen <davidz@redhat.com>
Packit 5756e2
 * Author: Dan Williams <dcbw@redhat.com>
Packit 5756e2
 * Author: Matthias Clasen
Packit 5756e2
 * Author: Pavel Šimerda <psimerda@redhat.com>
Packit 5756e2
 */
Packit 5756e2
#include "nm-default.h"
Packit 5756e2
Packit 5756e2
#include "nm-session-monitor.h"
Packit 5756e2
Packit 5756e2
#include <pwd.h>
Packit 5756e2
#include <sys/stat.h>
Packit 5756e2
Packit 5756e2
#if SESSION_TRACKING_SYSTEMD && SESSION_TRACKING_ELOGIND
Packit Service a1bd4f
    #error Cannot build both systemd-logind and elogind support
Packit 5756e2
#endif
Packit 5756e2
Packit 5756e2
#if SESSION_TRACKING_SYSTEMD
Packit Service a1bd4f
    #include <systemd/sd-login.h>
Packit Service a1bd4f
    #define LOGIND_NAME "systemd-logind"
Packit 5756e2
#endif
Packit 5756e2
Packit 5756e2
#if SESSION_TRACKING_ELOGIND
Packit Service a1bd4f
    #include <elogind/sd-login.h>
Packit Service a1bd4f
    #define LOGIND_NAME "elogind"
Packit 5756e2
#endif
Packit 5756e2
Packit 5756e2
#include "NetworkManagerUtils.h"
Packit 5756e2
Packit 5756e2
#define SESSION_TRACKING_XLOGIND (SESSION_TRACKING_SYSTEMD || SESSION_TRACKING_ELOGIND)
Packit 5756e2
Packit 5756e2
#define CKDB_PATH "/run/ConsoleKit/database"
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit 5756e2
enum {
Packit Service a1bd4f
    CHANGED,
Packit Service a1bd4f
    LAST_SIGNAL,
Packit 5756e2
};
Packit 5756e2
Packit Service a1bd4f
static guint signals[LAST_SIGNAL] = {0};
Packit 5756e2
Packit 5756e2
struct _NMSessionMonitor {
Packit Service a1bd4f
    GObject parent;
Packit 5756e2
Packit 5756e2
#if SESSION_TRACKING_XLOGIND
Packit Service a1bd4f
    struct {
Packit Service a1bd4f
        sd_login_monitor *monitor;
Packit Service a1bd4f
        GSource *         watch;
Packit Service a1bd4f
    } sd;
Packit 5756e2
#endif
Packit 5756e2
Packit 5756e2
#if SESSION_TRACKING_CONSOLEKIT
Packit Service a1bd4f
    struct {
Packit Service a1bd4f
        GFileMonitor *monitor;
Packit Service a1bd4f
        GHashTable *  cache;
Packit Service a1bd4f
        time_t        timestamp;
Packit Service a1bd4f
    } ck;
Packit 5756e2
#endif
Packit 5756e2
};
Packit 5756e2
Packit 5756e2
struct _NMSessionMonitorClass {
Packit Service a1bd4f
    GObjectClass parent;
Packit 5756e2
};
Packit 5756e2
Packit Service a1bd4f
G_DEFINE_TYPE(NMSessionMonitor, nm_session_monitor, G_TYPE_OBJECT);
Packit 5756e2
Packit 5756e2
#define _NMLOG_DOMAIN      LOGD_CORE
Packit Service a1bd4f
#define _NMLOG(level, ...) __NMLOG_DEFAULT(level, _NMLOG_DOMAIN, "session-monitor", __VA_ARGS__)
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit 5756e2
#if SESSION_TRACKING_XLOGIND
Packit 5756e2
static gboolean
Packit Service a1bd4f
st_sd_session_exists(NMSessionMonitor *monitor, uid_t uid, gboolean active)
Packit 5756e2
{
Packit Service a1bd4f
    int status;
Packit 5756e2
Packit Service a1bd4f
    if (!monitor->sd.monitor)
Packit Service a1bd4f
        return FALSE;
Packit 5756e2
Packit Service a1bd4f
    status = sd_uid_get_sessions(uid, active, NULL);
Packit 5756e2
Packit Service a1bd4f
    if (status < 0)
Packit Service a1bd4f
        _LOGE("failed to get " LOGIND_NAME " sessions for uid %d: %d", uid, status);
Packit 5756e2
Packit Service a1bd4f
    return status > 0;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static gboolean
Packit Service a1bd4f
st_sd_changed(int fd, GIOCondition condition, gpointer user_data)
Packit 5756e2
{
Packit Service a1bd4f
    NMSessionMonitor *monitor = user_data;
Packit 5756e2
Packit Service a1bd4f
    g_signal_emit(monitor, signals[CHANGED], 0);
Packit 5756e2
Packit Service a1bd4f
    sd_login_monitor_flush(monitor->sd.monitor);
Packit 5756e2
Packit Service a1bd4f
    return G_SOURCE_CONTINUE;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
st_sd_init(NMSessionMonitor *monitor)
Packit 5756e2
{
Packit Service a1bd4f
    int status;
Packit Service a1bd4f
Packit Service a1bd4f
    if (!g_file_test("/run/systemd/seats/", G_FILE_TEST_EXISTS))
Packit Service a1bd4f
        return;
Packit Service a1bd4f
Packit Service a1bd4f
    if ((status = sd_login_monitor_new(NULL, &monitor->sd.monitor)) < 0) {
Packit Service a1bd4f
        _LOGE("failed to create " LOGIND_NAME " monitor: %d", status);
Packit Service a1bd4f
        return;
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    monitor->sd.watch = nm_g_unix_fd_source_new(sd_login_monitor_get_fd(monitor->sd.monitor),
Packit Service a1bd4f
                                                G_IO_IN,
Packit Service a1bd4f
                                                G_PRIORITY_DEFAULT,
Packit Service a1bd4f
                                                st_sd_changed,
Packit Service a1bd4f
                                                monitor,
Packit Service a1bd4f
                                                NULL);
Packit Service a1bd4f
    g_source_attach(monitor->sd.watch, NULL);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
st_sd_finalize(NMSessionMonitor *monitor)
Packit 5756e2
{
Packit Service a1bd4f
    if (monitor->sd.monitor) {
Packit Service a1bd4f
        sd_login_monitor_unref(monitor->sd.monitor);
Packit Service a1bd4f
        monitor->sd.monitor = NULL;
Packit Service a1bd4f
    }
Packit Service a1bd4f
    nm_clear_g_source_inst(&monitor->sd.watch);
Packit 5756e2
}
Packit 5756e2
#endif /* SESSION_TRACKING_XLOGIND */
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit 5756e2
#if SESSION_TRACKING_CONSOLEKIT
Packit 5756e2
typedef struct {
Packit Service a1bd4f
    gboolean active;
Packit 5756e2
} CkSession;
Packit 5756e2
Packit 5756e2
static gboolean
Packit Service a1bd4f
ck_load_cache(GHashTable *cache)
Packit 5756e2
{
Packit Service a1bd4f
    GKeyFile *keyfile = g_key_file_new();
Packit Service a1bd4f
    char **   groups  = NULL;
Packit Service a1bd4f
    GError *  error   = NULL;
Packit Service a1bd4f
    gsize     i, len;
Packit Service a1bd4f
    gboolean  finished = FALSE;
Packit 5756e2
Packit Service a1bd4f
    if (!g_key_file_load_from_file(keyfile, CKDB_PATH, G_KEY_FILE_NONE, &error))
Packit Service a1bd4f
        goto out;
Packit 5756e2
Packit Service a1bd4f
    if (!(groups = g_key_file_get_groups(keyfile, &len))) {
Packit Service a1bd4f
        _LOGE("could not load groups from " CKDB_PATH);
Packit Service a1bd4f
        goto out;
Packit Service a1bd4f
    }
Packit 5756e2
Packit Service a1bd4f
    g_hash_table_remove_all(cache);
Packit 5756e2
Packit Service a1bd4f
    for (i = 0; i < len; i++) {
Packit Service a1bd4f
        guint     uid     = G_MAXUINT;
Packit Service a1bd4f
        CkSession session = {.active = FALSE};
Packit 5756e2
Packit Service a1bd4f
        if (!g_str_has_prefix(groups[i], "Session "))
Packit Service a1bd4f
            continue;
Packit 5756e2
Packit Service a1bd4f
        uid = g_key_file_get_integer(keyfile, groups[i], "uid", &error);
Packit Service a1bd4f
        if (error)
Packit Service a1bd4f
            goto out;
Packit 5756e2
Packit Service a1bd4f
        session.active = g_key_file_get_boolean(keyfile, groups[i], "is_active", &error);
Packit Service a1bd4f
        if (error)
Packit Service a1bd4f
            goto out;
Packit 5756e2
Packit Service a1bd4f
        g_hash_table_insert(cache, GUINT_TO_POINTER(uid), nm_memdup(&session, sizeof session));
Packit Service a1bd4f
    }
Packit 5756e2
Packit Service a1bd4f
    finished = TRUE;
Packit 5756e2
out:
Packit Service a1bd4f
    if (error)
Packit Service a1bd4f
        _LOGE("failed to load ConsoleKit database: %s", error->message);
Packit Service a1bd4f
    g_clear_error(&error);
Packit Service a1bd4f
    nm_clear_pointer(&groups, g_strfreev);
Packit Service a1bd4f
    nm_clear_pointer(&keyfile, g_key_file_free);
Packit 5756e2
Packit Service a1bd4f
    return finished;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static gboolean
Packit Service a1bd4f
ck_update_cache(NMSessionMonitor *monitor)
Packit 5756e2
{
Packit Service a1bd4f
    struct stat statbuf;
Packit Service a1bd4f
    int         errsv;
Packit 5756e2
Packit Service a1bd4f
    if (!monitor->ck.cache)
Packit Service a1bd4f
        return FALSE;
Packit 5756e2
Packit Service a1bd4f
    /* Check the database file */
Packit Service a1bd4f
    if (stat(CKDB_PATH, &statbuf) != 0) {
Packit Service a1bd4f
        errsv = errno;
Packit Service a1bd4f
        _LOGE("failed to check ConsoleKit timestamp: %s", nm_strerror_native(errsv));
Packit Service a1bd4f
        return FALSE;
Packit Service a1bd4f
    }
Packit Service a1bd4f
    if (statbuf.st_mtime == monitor->ck.timestamp)
Packit Service a1bd4f
        return TRUE;
Packit 5756e2
Packit Service a1bd4f
    /* Update the cache */
Packit Service a1bd4f
    if (!ck_load_cache(monitor->ck.cache))
Packit Service a1bd4f
        return FALSE;
Packit 5756e2
Packit Service a1bd4f
    monitor->ck.timestamp = statbuf.st_mtime;
Packit 5756e2
Packit Service a1bd4f
    return TRUE;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static gboolean
Packit Service a1bd4f
ck_session_exists(NMSessionMonitor *monitor, uid_t uid, gboolean active)
Packit 5756e2
{
Packit Service a1bd4f
    CkSession *session;
Packit 5756e2
Packit Service a1bd4f
    if (!ck_update_cache(monitor))
Packit Service a1bd4f
        return FALSE;
Packit 5756e2
Packit Service a1bd4f
    session = g_hash_table_lookup(monitor->ck.cache, GUINT_TO_POINTER(uid));
Packit 5756e2
Packit Service a1bd4f
    if (!session)
Packit Service a1bd4f
        return FALSE;
Packit Service a1bd4f
    if (active && !session->active)
Packit Service a1bd4f
        return FALSE;
Packit 5756e2
Packit Service a1bd4f
    return TRUE;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
ck_changed(GFileMonitor *    file_monitor,
Packit Service a1bd4f
           GFile *           file,
Packit Service a1bd4f
           GFile *           other_file,
Packit Service a1bd4f
           GFileMonitorEvent event_type,
Packit Service a1bd4f
           gpointer          user_data)
Packit 5756e2
{
Packit Service a1bd4f
    g_signal_emit(user_data, signals[CHANGED], 0);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
ck_init(NMSessionMonitor *monitor)
Packit 5756e2
{
Packit Service a1bd4f
    GFile * file  = g_file_new_for_path(CKDB_PATH);
Packit Service a1bd4f
    GError *error = NULL;
Packit Service a1bd4f
Packit Service a1bd4f
    if (g_file_query_exists(file, NULL)) {
Packit Service a1bd4f
        if ((monitor->ck.monitor = g_file_monitor_file(file, G_FILE_MONITOR_NONE, NULL, &error))) {
Packit Service a1bd4f
            monitor->ck.cache = g_hash_table_new_full(nm_direct_hash, NULL, NULL, g_free);
Packit Service a1bd4f
            g_signal_connect(monitor->ck.monitor, "changed", G_CALLBACK(ck_changed), monitor);
Packit Service a1bd4f
        } else {
Packit Service a1bd4f
            _LOGE("error monitoring " CKDB_PATH ": %s", error->message);
Packit Service a1bd4f
            g_clear_error(&error);
Packit Service a1bd4f
        }
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    g_object_unref(file);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
ck_finalize(NMSessionMonitor *monitor)
Packit 5756e2
{
Packit Service a1bd4f
    nm_clear_pointer(&monitor->ck.cache, g_hash_table_unref);
Packit Service a1bd4f
    g_clear_object(&monitor->ck.monitor);
Packit 5756e2
}
Packit 5756e2
#endif /* SESSION_TRACKING_CONSOLEKIT */
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit Service a1bd4f
NM_DEFINE_SINGLETON_GETTER(NMSessionMonitor, nm_session_monitor_get, NM_TYPE_SESSION_MONITOR);
Packit 5756e2
Packit 5756e2
/**
Packit 5756e2
 * nm_session_monitor_session_exists:
Packit 5756e2
 * @self: the session monitor
Packit 5756e2
 * @uid: A user ID.
Packit 5756e2
 * @active: Ignore inactive sessions.
Packit 5756e2
 *
Packit 5756e2
 * Checks whether the given @uid is logged into an active session. Don't
Packit 5756e2
 * use this feature for security purposes. It is there just to allow you
Packit 5756e2
 * to prefer an agent from an active session over an agent from an
Packit 5756e2
 * inactive one.
Packit 5756e2
 *
Packit 5756e2
 * Returns: %FALSE if @error is set otherwise %TRUE if the given @uid is
Packit 5756e2
 * logged into an active session.
Packit 5756e2
 */
Packit 5756e2
gboolean
Packit Service a1bd4f
nm_session_monitor_session_exists(NMSessionMonitor *self, uid_t uid, gboolean active)
Packit 5756e2
{
Packit Service a1bd4f
    g_return_val_if_fail(NM_IS_SESSION_MONITOR(self), FALSE);
Packit 5756e2
Packit 5756e2
#if SESSION_TRACKING_XLOGIND
Packit Service a1bd4f
    if (st_sd_session_exists(self, uid, active))
Packit Service a1bd4f
        return TRUE;
Packit 5756e2
#endif
Packit 5756e2
Packit 5756e2
#if SESSION_TRACKING_CONSOLEKIT
Packit Service a1bd4f
    if (ck_session_exists(self, uid, active))
Packit Service a1bd4f
        return TRUE;
Packit 5756e2
#endif
Packit 5756e2
Packit Service a1bd4f
    return FALSE;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
nm_session_monitor_init(NMSessionMonitor *monitor)
Packit 5756e2
{
Packit 5756e2
#if SESSION_TRACKING_XLOGIND
Packit Service a1bd4f
    st_sd_init(monitor);
Packit Service a1bd4f
    _LOGD("using " LOGIND_NAME " session tracking");
Packit 5756e2
#endif
Packit 5756e2
Packit 5756e2
#if SESSION_TRACKING_CONSOLEKIT
Packit Service a1bd4f
    ck_init(monitor);
Packit Service a1bd4f
    _LOGD("using ConsoleKit session tracking");
Packit 5756e2
#endif
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
finalize(GObject *object)
Packit 5756e2
{
Packit 5756e2
#if SESSION_TRACKING_XLOGIND
Packit Service a1bd4f
    st_sd_finalize(NM_SESSION_MONITOR(object));
Packit 5756e2
#endif
Packit 5756e2
Packit 5756e2
#if SESSION_TRACKING_CONSOLEKIT
Packit Service a1bd4f
    ck_finalize(NM_SESSION_MONITOR(object));
Packit 5756e2
#endif
Packit 5756e2
Packit Service a1bd4f
    G_OBJECT_CLASS(nm_session_monitor_parent_class)->finalize(object);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
nm_session_monitor_class_init(NMSessionMonitorClass *klass)
Packit 5756e2
{
Packit Service a1bd4f
    GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
Packit Service a1bd4f
Packit Service a1bd4f
    gobject_class->finalize = finalize;
Packit Service a1bd4f
Packit Service a1bd4f
    /**
Packit Service a1bd4f
     * NMSessionMonitor::changed:
Packit Service a1bd4f
     * @monitor: A #NMSessionMonitor
Packit Service a1bd4f
     *
Packit Service a1bd4f
     * Emitted when something changes.
Packit Service a1bd4f
     */
Packit Service a1bd4f
    signals[CHANGED] = g_signal_new(NM_SESSION_MONITOR_CHANGED,
Packit Service a1bd4f
                                    NM_TYPE_SESSION_MONITOR,
Packit Service a1bd4f
                                    G_SIGNAL_RUN_LAST,
Packit Service a1bd4f
                                    0,
Packit Service a1bd4f
                                    NULL,
Packit Service a1bd4f
                                    NULL,
Packit Service a1bd4f
                                    g_cclosure_marshal_VOID__VOID,
Packit Service a1bd4f
                                    G_TYPE_NONE,
Packit Service a1bd4f
                                    0);
Packit 5756e2
}