[gdm/rstrode/wip/list-extension: 3/5] daemon: add ChoiceList PAM extension



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

    daemon: add ChoiceList PAM extension
    
    This commit adds one PAM extension, a "Choice List" using the
    new PAM_BINARY_PROMPT protocol added in the previous commit.  The
    PAM module sends a list of (key, row text) pairs, and GDM ferries
    the request to gnome-shell using a new user verifier sub-interface.
    
    gnome-shell should present the list to the user and pass back the
    corresponding key, which GDM ferries back to the PAM module.
    
    Note this commit is only the daemon side. A subsequent commit will
    add the libgdm API needed for gnome-shell to actually deal with
    this new PAM extension.

 daemon/gdm-pam-extensions.h |   43 +++++++++++++++++++++++++++
 daemon/gdm-session-worker.c |   60 +++++++++++++++++++++++++++++++++++++-
 daemon/gdm-session.c        |   67 +++++++++++++++++++++++++++++++++++++++++++
 daemon/gdm-session.xml      |   15 +++++++++
 4 files changed, 184 insertions(+), 1 deletions(-)
---
diff --git a/daemon/gdm-pam-extensions.h b/daemon/gdm-pam-extensions.h
index 06bd770..f236698 100644
--- a/daemon/gdm-pam-extensions.h
+++ b/daemon/gdm-pam-extensions.h
@@ -113,4 +113,47 @@ typedef struct {
 
 #define GDM_PAM_EXTENSION_SUPPORTED(name) GDM_PAM_EXTENSION_LOOK_UP_TYPE(name, (unsigned char *) NULL)
 
+typedef struct {
+        const char *key;
+        const char *text;
+} GdmChoiceListItems;
+
+typedef struct {
+        size_t number_of_items;
+        GdmChoiceListItems items[];
+} GdmChoiceList;
+
+typedef struct {
+        GdmPamExtensionMessage header;
+
+        GdmChoiceList list;
+} GdmPamExtensionChoiceListRequest;
+
+typedef struct {
+        GdmPamExtensionMessage header;
+
+        char *key;
+} GdmPamExtensionChoiceListResponse;
+
+#define GDM_PAM_EXTENSION_CHOICE_LIST "org.gnome.DisplayManager.ChoiceList"
+
+#define GDM_CHOICE_LIST_SIZE(num_items) (offsetof(GdmChoiceList, items) + (num_items) * sizeof 
(GdmChoiceListItems))
+#define GDM_PAM_EXTENSION_CHOICE_LIST_REQUEST_SIZE(num_items) (offsetof(GdmPamExtensionChoiceListRequest, 
list) + GDM_CHOICE_LIST_SIZE((num_items)))
+#define GDM_PAM_EXTENSION_CHOICE_LIST_REQUEST_INIT(request,num_items) \
+{ \
+        int _n = num_items; \
+        GDM_PAM_EXTENSION_LOOK_UP_TYPE (GDM_PAM_EXTENSION_CHOICE_LIST, &request->header.type); \
+        request->header.length = htobe32 (GDM_PAM_EXTENSION_CHOICE_LIST_REQUEST_SIZE(_n)); \
+        request->list.number_of_items = _n; \
+}
+
+#define GDM_PAM_EXTENSION_CHOICE_LIST_RESPONSE_SIZE sizeof (GdmPamExtensionChoiceListResponse)
+#define GDM_PAM_EXTENSION_CHOICE_LIST_RESPONSE_INIT(response) \
+{ \
+        GDM_PAM_EXTENSION_LOOK_UP_TYPE (GDM_PAM_EXTENSION_CHOICE_LIST, &response->header.type); \
+        response->header.length = htobe32 (GDM_PAM_EXTENSION_CHOICE_LIST_RESPONSE_SIZE); \
+        response->key = NULL; \
+}
+#define GDM_PAM_EXTENSION_REPLY_TO_CHOICE_LIST_RESPONSE(reply) ((GdmPamExtensionChoiceListResponse *) (void 
*) reply->resp)
+
 #endif
diff --git a/daemon/gdm-session-worker.c b/daemon/gdm-session-worker.c
index 7271c55..b130200 100644
--- a/daemon/gdm-session-worker.c
+++ b/daemon/gdm-session-worker.c
@@ -180,6 +180,7 @@ struct GdmSessionWorkerPrivate
 
 static char *
 gdm_supported_pam_extensions[] = {
+        GDM_PAM_EXTENSION_CHOICE_LIST,
         NULL
 };
 
@@ -532,11 +533,50 @@ gdm_advertise_supported_pam_extensions (void)
 }
 
 static gboolean
+gdm_session_worker_ask_list_of_choices (GdmSessionWorker *worker,
+                                        GdmChoiceList    *list,
+                                        char            **answerp)
+{
+        GVariantBuilder builder;
+        GVariant *choices_as_variant;
+        size_t i;
+
+        g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{ss}"));
+
+        for (i = 0; i < list->number_of_items; i++) {
+                if (list->items[i].key == NULL) {
+                        g_warning ("choice list contains item with NULL key");
+                        g_variant_builder_clear (&builder);
+                        return FALSE;
+                }
+                g_variant_builder_add (&builder, "{ss}", list->items[i].key, list->items[i].text);
+        }
+
+        choices_as_variant = g_variant_builder_end (&builder);
+
+        return gdm_dbus_worker_manager_call_choice_list_query_sync (worker->priv->manager,
+                                                                    worker->priv->service,
+                                                                    choices_as_variant,
+                                                                    answerp,
+                                                                    NULL,
+                                                                    NULL);
+}
+
+static gboolean
+gdm_session_worker_process_choice_list_request (GdmSessionWorker                   *worker,
+                                                GdmPamExtensionChoiceListRequest  *request,
+                                                GdmPamExtensionChoiceListResponse *response)
+{
+        return gdm_session_worker_ask_list_of_choices (worker, &request->list, &response->key);
+}
+
+static gboolean
 gdm_session_worker_process_extended_pam_message (GdmSessionWorker          *worker,
                                                  const struct pam_message  *query,
                                                  char                     **response)
 {
         GdmPamExtensionMessage *extended_message;
+        gboolean res;
 
         extended_message = GDM_PAM_EXTENSION_MESSAGE_FROM_PAM_MESSAGE (query);
 
@@ -550,7 +590,24 @@ gdm_session_worker_process_extended_pam_message (GdmSessionWorker          *work
                 return FALSE;
         }
 
-        return FALSE;
+        if (g_strcmp0 (gdm_supported_pam_extensions[extended_message->type], GDM_PAM_EXTENSION_CHOICE_LIST) 
== 0) {
+                GdmPamExtensionChoiceListRequest *list_request = (GdmPamExtensionChoiceListRequest *) 
extended_message;
+                GdmPamExtensionChoiceListResponse *list_response = malloc 
(GDM_PAM_EXTENSION_CHOICE_LIST_RESPONSE_SIZE);
+
+                GDM_PAM_EXTENSION_CHOICE_LIST_RESPONSE_INIT (list_response);
+
+                res = gdm_session_worker_process_choice_list_request (worker, list_request, list_response);
+
+                if (! res) {
+                        g_free (list_response);
+                        return FALSE;
+                }
+
+                *response = GDM_PAM_EXTENSION_MESSAGE_TO_PAM_REPLY (list_response);
+                return TRUE;
+        }
+
+        return TRUE;
 }
 
 static char *
@@ -3366,3 +3423,4 @@ gdm_session_worker_new (const char *address,
 
         return GDM_SESSION_WORKER (object);
 }
+
diff --git a/daemon/gdm-session.c b/daemon/gdm-session.c
index 0b83223..0530ee1 100644
--- a/daemon/gdm-session.c
+++ b/daemon/gdm-session.c
@@ -105,6 +105,8 @@ struct _GdmSessionPrivate
         GdmDBusRemoteGreeter  *remote_greeter_interface;
         GdmDBusChooser        *chooser_interface;
 
+        GdmDBusUserVerifierChoiceList   *user_verifier_choice_list_interface;
+
         GList               *pending_worker_connections;
         GList               *outside_connections;
 
@@ -687,6 +689,29 @@ set_pending_query (GdmSessionConversation *conversation,
 }
 
 static gboolean
+gdm_session_handle_choice_list_query (GdmDBusWorkerManager  *worker_manager_interface,
+                                      GDBusMethodInvocation *invocation,
+                                      const char            *service_name,
+                                      GVariant              *query,
+                                      GdmSession            *self)
+{
+        GdmSessionConversation *conversation;
+
+        g_return_val_if_fail (self->priv->user_verifier_choice_list_interface != NULL, FALSE);
+
+        conversation = find_conversation_by_name (self, service_name);
+        if (conversation != NULL) {
+                set_pending_query (conversation, invocation);
+
+                gdm_dbus_user_verifier_choice_list_emit_choice_query 
(self->priv->user_verifier_choice_list_interface,
+                                                                      service_name,
+                                                                      query);
+        }
+
+        return TRUE;
+}
+
+static gboolean
 gdm_session_handle_info_query (GdmDBusWorkerManager  *worker_manager_interface,
                                GDBusMethodInvocation *invocation,
                                const char            *service_name,
@@ -1144,6 +1169,10 @@ export_worker_manager_interface (GdmSession      *self,
                           "handle-problem",
                           G_CALLBACK (gdm_session_handle_problem),
                           self);
+        g_signal_connect (worker_manager_interface,
+                          "handle-choice-list-query",
+                          G_CALLBACK (gdm_session_handle_choice_list_query),
+                          self);
 
         g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (worker_manager_interface),
                                           connection,
@@ -1173,6 +1202,9 @@ unexport_worker_manager_interface (GdmSession           *self,
         g_signal_handlers_disconnect_by_func (worker_manager_interface,
                                               G_CALLBACK (gdm_session_handle_problem),
                                               self);
+        g_signal_handlers_disconnect_by_func (worker_manager_interface,
+                                              G_CALLBACK (gdm_session_handle_choice_list_query),
+                                              self);
 }
 
 static gboolean
@@ -1405,6 +1437,40 @@ export_user_verifier_interface (GdmSession      *self,
         self->priv->user_verifier_interface = user_verifier_interface;
 }
 
+static gboolean
+gdm_session_handle_client_select_choice (GdmDBusUserVerifier    *user_verifier_interface,
+                                         GDBusMethodInvocation  *invocation,
+                                         const char             *service_name,
+                                         const char             *answer,
+                                         GdmSession             *self)
+{
+        gdm_dbus_user_verifier_complete_answer_query (user_verifier_interface,
+                                                      invocation);
+        gdm_session_answer_query (self, service_name, answer);
+        return TRUE;
+}
+
+static void
+export_user_verifier_choice_list_interface (GdmSession      *self,
+                                            GDBusConnection *connection)
+{
+        GdmDBusUserVerifierChoiceList   *interface;
+
+        interface = GDM_DBUS_USER_VERIFIER_CHOICE_LIST (gdm_dbus_user_verifier_choice_list_skeleton_new ());
+
+        g_signal_connect (interface,
+                          "handle-select-choice",
+                          G_CALLBACK (gdm_session_handle_client_select_choice),
+                          self);
+
+        g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (interface),
+                                          connection,
+                                          GDM_SESSION_DBUS_OBJECT_PATH,
+                                          NULL);
+
+        self->priv->user_verifier_choice_list_interface = interface;
+}
+
 static void
 export_greeter_interface (GdmSession      *self,
                           GDBusConnection *connection)
@@ -1559,6 +1625,7 @@ handle_connection_from_outside (GDBusServer      *server,
                                  0);
 
         export_user_verifier_interface (self, connection);
+        export_user_verifier_choice_list_interface (self, connection);
 
         switch (self->priv->verification_mode) {
                 case GDM_SESSION_VERIFICATION_MODE_LOGIN:
diff --git a/daemon/gdm-session.xml b/daemon/gdm-session.xml
index 9d44005..2f7b1d4 100644
--- a/daemon/gdm-session.xml
+++ b/daemon/gdm-session.xml
@@ -22,6 +22,11 @@
       <arg name="service_name" direction="in" type="s"/>
       <arg name="problem" direction="in" type="s"/>
     </method>
+    <method name="ChoiceListQuery">
+      <arg name="service_name" direction="in" type="s"/>
+      <arg name="query" direction="in" type="a{ss}"/>
+      <arg name="answer" direction="out" type="s"/>
+    </method>
   </interface>
   <interface name="org.gnome.DisplayManager.UserVerifier">
     <method name="BeginVerification">
@@ -75,6 +80,16 @@
       <arg name="service_name" type="s"/>
     </signal>
   </interface>
+  <interface name="org.gnome.DisplayManager.UserVerifier.ChoiceList">
+    <method name="SelectChoice">
+      <arg name="service_name" direction="in" type="s"/>
+      <arg name="choice" direction="in" type="s"/>
+    </method>
+    <signal name="ChoiceQuery">
+      <arg name="service_name" type="s"/>
+      <arg name="list" type="a{ss}"/>
+    </signal>
+  </interface>
   <interface name="org.gnome.DisplayManager.Greeter">
     <method name="SelectSession">
       <arg name="session" direction="in" type="s"/>


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