[gdm/rstrode/wip/list-extension: 2/5] daemon: introduce pam extension mechanism



commit 53f5490a37ce3cf5c7edbd570df253b7380b6b4a
Author: Ray Strode <rstrode redhat com>
Date:   Fri Jul 14 16:05:46 2017 -0400

    daemon: introduce pam extension mechanism
    
    This abuses PAM_BINARY_PROMPT for our own nefarious purposes.
    The way it works is GDM advertises what "extensions" it supports
    with the environment variable, GDM_SUPPORTED_PAM_EXTENSIONS (a space
    separated list of reverse dns notation names). PAM services that
    support this protocol, will read the environment variable, and
    check for extension strings they support. They then know that sending
    PAM_BINARY_PROMPT won't blow up, and know what format to use for the
    binary data.  The type field of the structure is the index of the
    string from the environment variable.
    
    This commit is just foundation work. It doesn't actually add any
    extensions.

 daemon/gdm-pam-extensions.h  |  116 ++++++++++++++++++++++++++++++++++++++++++
 daemon/gdm-session-worker.c  |   39 ++++++++++++++
 daemon/gdm-session-worker.h  |    1 +
 daemon/session-worker-main.c |    2 +
 4 files changed, 158 insertions(+), 0 deletions(-)
---
diff --git a/daemon/gdm-pam-extensions.h b/daemon/gdm-pam-extensions.h
new file mode 100644
index 0000000..06bd770
--- /dev/null
+++ b/daemon/gdm-pam-extensions.h
@@ -0,0 +1,116 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2017 Red Hat, Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ */
+#ifndef GDM_PAM_EXTENSIONS_H
+#define GDM_PAM_EXTENSIONS_H
+
+#include <alloca.h>
+#include <endian.h>
+#include <stdbool.h>
+#include <stddef.h>
+#include <stdint.h>
+#include <limits.h>
+
+#include <security/pam_appl.h>
+
+typedef struct {
+        uint32_t length;
+
+        unsigned char type;
+        unsigned char data[];
+} GdmPamExtensionMessage;
+
+#define GDM_PAM_EXTENSION_MESSAGE_FROM_PAM_MESSAGE(query) (GdmPamExtensionMessage *) (void *) query->msg
+#define GDM_PAM_EXTENSION_MESSAGE_TO_PAM_REPLY(msg) (char *) (void *) msg
+#define GDM_PAM_EXTENSION_MESSAGE_TRUNCATED(msg) be32toh(msg->length) < sizeof (GdmPamExtensionMessage)
+#define GDM_PAM_EXTENSION_MESSAGE_INVALID_TYPE(msg) \
+({ \
+        bool _invalid = true; \
+        int _n = -1; \
+        const char *_supported_extensions; \
+        _supported_extensions = getenv ("GDM_SUPPORTED_PAM_EXTENSIONS"); \
+        if (_supported_extensions != NULL) { \
+                const char *_p = _supported_extensions; \
+                while (*_p != '\0' && _n < UCHAR_MAX) { \
+                        size_t _length; \
+                        _length = strcspn (_p, " "); \
+                        if (_length > 0) \
+                                _n++; \
+                        _p += _length; \
+                        _length = strspn (_p, " "); \
+                        _p += _length; \
+                } \
+                if (_n >= msg->type) \
+                        _invalid = false; \
+        } \
+        _invalid; \
+})
+
+#define GDM_PAM_EXTENSION_ADVERTISE_SUPPORTED_EXTENSIONS(supported_extensions) \
+{ \
+        size_t _size = 0; \
+        unsigned char _t; \
+        char *_extension_string, *_p; \
+        for (_t = 0; supported_extensions[_t] != NULL; _t++) \
+                _size += strlen (supported_extensions[_t]) + strlen (" "); \
+        if (_t != 0) { \
+                _extension_string = alloca (_size); \
+                _p = _extension_string; \
+                for (_t = 0; supported_extensions[_t] != NULL; _t++) { \
+                        if (_p != _extension_string) \
+                                _p = stpcpy (_p, " "); \
+                        _p = stpcpy (_p, supported_extensions[_t]); \
+                } \
+                *_p = '\0'; \
+                setenv ("GDM_SUPPORTED_PAM_EXTENSIONS", _extension_string, true); \
+        } \
+}
+
+#define GDM_PAM_EXTENSION_LOOK_UP_TYPE(name, extension_type) \
+({ \
+        bool _supported = false; \
+        unsigned char _t = 0; \
+        const char *_supported_extensions; \
+        _supported_extensions = getenv ("GDM_SUPPORTED_PAM_EXTENSIONS"); \
+        if (_supported_extensions != NULL) { \
+                const char *_p = _supported_extensions; \
+                while (*_p != '\0') { \
+                        size_t _length; \
+                        _length = strcspn (_p, " "); \
+                        if (strncmp (_p, name, _length) == 0) { \
+                                _supported = true; \
+                                break; \
+                        } \
+                        _p += _length; \
+                        _length = strspn (_p, " "); \
+                        _p += _length; \
+                        if (_t >= UCHAR_MAX) { \
+                                break; \
+                        } \
+                        _t++; \
+                } \
+                if (_supported && extension_type != NULL) \
+                        *extension_type = _t; \
+        } \
+        _supported; \
+})
+
+#define GDM_PAM_EXTENSION_SUPPORTED(name) GDM_PAM_EXTENSION_LOOK_UP_TYPE(name, (unsigned char *) NULL)
+
+#endif
diff --git a/daemon/gdm-session-worker.c b/daemon/gdm-session-worker.c
index 9342352..7271c55 100644
--- a/daemon/gdm-session-worker.c
+++ b/daemon/gdm-session-worker.c
@@ -61,6 +61,7 @@
 
 #include "gdm-common.h"
 #include "gdm-log.h"
+#include "gdm-pam-extensions.h"
 #include "gdm-session-worker.h"
 #include "gdm-session-glue.h"
 #include "gdm-session.h"
@@ -177,6 +178,11 @@ struct GdmSessionWorkerPrivate
         GDBusMethodInvocation *pending_invocation;
 };
 
+static char *
+gdm_supported_pam_extensions[] = {
+        NULL
+};
+
 enum {
         PROP_0,
         PROP_SERVER_ADDRESS,
@@ -519,6 +525,34 @@ gdm_session_worker_report_problem (GdmSessionWorker *worker,
                                                           NULL);
 }
 
+void
+gdm_advertise_supported_pam_extensions (void)
+{
+        GDM_PAM_EXTENSION_ADVERTISE_SUPPORTED_EXTENSIONS (gdm_supported_pam_extensions);
+}
+
+static gboolean
+gdm_session_worker_process_extended_pam_message (GdmSessionWorker          *worker,
+                                                 const struct pam_message  *query,
+                                                 char                     **response)
+{
+        GdmPamExtensionMessage *extended_message;
+
+        extended_message = GDM_PAM_EXTENSION_MESSAGE_FROM_PAM_MESSAGE (query);
+
+        if (GDM_PAM_EXTENSION_MESSAGE_TRUNCATED (extended_message)) {
+                g_warning ("PAM service requested binary response for truncated query");
+                return FALSE;
+        }
+
+        if (GDM_PAM_EXTENSION_MESSAGE_INVALID_TYPE (extended_message)) {
+                g_warning ("PAM service requested binary response for unadvertised query type");
+                return FALSE;
+        }
+
+        return FALSE;
+}
+
 static char *
 convert_to_utf8 (const char *str)
 {
@@ -563,6 +597,11 @@ gdm_session_worker_process_pam_message (GdmSessionWorker          *worker,
 
         gdm_session_worker_update_username (worker);
 
+#ifdef PAM_BINARY_PROMPT
+        if (query->msg_style == PAM_BINARY_PROMPT)
+                return gdm_session_worker_process_extended_pam_message (worker, query, response);
+#endif
+
         g_debug ("GdmSessionWorker: received pam message of type %u with payload '%s'",
                  query->msg_style, query->msg);
 
diff --git a/daemon/gdm-session-worker.h b/daemon/gdm-session-worker.h
index cba3a4e..2f3e145 100644
--- a/daemon/gdm-session-worker.h
+++ b/daemon/gdm-session-worker.h
@@ -53,6 +53,7 @@ GType              gdm_session_worker_get_type                 (void);
 GdmSessionWorker * gdm_session_worker_new                      (const char *server_address,
                                                                 gboolean    is_for_reauth) G_GNUC_MALLOC;
 
+void               gdm_advertise_supported_pam_extensions      (void);
 G_END_DECLS
 
 #endif /* GDM_SESSION_WORKER_H */
diff --git a/daemon/session-worker-main.c b/daemon/session-worker-main.c
index 051d7f9..49cc264 100644
--- a/daemon/session-worker-main.c
+++ b/daemon/session-worker-main.c
@@ -85,6 +85,8 @@ main (int    argc,
 
         signal (SIGTERM, on_sigterm_cb);
 
+        gdm_advertise_supported_pam_extensions ();
+
         bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
         textdomain (GETTEXT_PACKAGE);
         setlocale (LC_ALL, "");


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]