[krb5-auth-dialog] add list of current tickets dialog



commit 485b914708dfe900d83fe0082506a2cad2b35b02
Author: Guido Günther <agx sigxcpu org>
Date:   Tue Sep 15 14:24:28 2009 +0200

    add list of current tickets dialog

 src/Makefile.am          |    2 +
 src/krb5-auth-applet.c   |   18 +++++++-
 src/krb5-auth-dialog.c   |  112 +++++++++++++++++++++++++++++++++++++++++++---
 src/krb5-auth-dialog.h   |    1 +
 src/krb5-auth-dialog.xml |   62 +++++++++++++++++++++++++
 src/krb5-auth-tickets.c  |   93 ++++++++++++++++++++++++++++++++++++++
 src/krb5-auth-tickets.h  |   36 +++++++++++++++
 7 files changed, 316 insertions(+), 8 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index cd016d3..e0068fb 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -35,6 +35,8 @@ krb5_auth_dialog_SOURCES =	\
 	krb5-auth-dbus.h	\
 	krb5-auth-tools.c	\
 	krb5-auth-tools.h	\
+	krb5-auth-tickets.c	\
+	krb5-auth-tickets.h	\
 	dummy-strings.c		\
 	$(NULL)
 
diff --git a/src/krb5-auth-applet.c b/src/krb5-auth-applet.c
index a1c99fe..bfe99c2 100644
--- a/src/krb5-auth-applet.c
+++ b/src/krb5-auth-applet.c
@@ -27,6 +27,7 @@
 #include "krb5-auth-gconf-tools.h"
 #include "krb5-auth-gconf.h"
 #include "krb5-auth-tools.h"
+#include "krb5-auth-tickets.h"
 #ifdef HAVE_LIBNOTIFY
 #include <libnotify/notify.h>
 #endif
@@ -72,7 +73,7 @@ struct _KaAppletPrivate
 	const char* icons[3]; 		/* for invalid, expiring and valid tickts */
 	gboolean show_trayicon;		/* show the trayicon */
 
-	KaPwDialog* pwdialog;		/* the password dialog */
+	KaPwDialog *pwdialog;		/* the password dialog */
 	int	   pw_prompt_secs;	/* when to start prompting for a password */
 
 #ifdef HAVE_LIBNOTIFY
@@ -666,6 +667,13 @@ ka_applet_cb_destroy_ccache(GtkMenuItem* menuitem G_GNUC_UNUSED,
 	ka_destroy_ccache(applet);
 }
 
+static void
+ka_applet_cb_show_tickets(GtkMenuItem* menuitem G_GNUC_UNUSED,
+			  gpointer user_data G_GNUC_UNUSED)
+{
+	ka_tickets_dialog_run();
+}
+
 
 /* The tray icon's context menu */
 static gboolean
@@ -687,6 +695,12 @@ ka_applet_create_context_menu (KaApplet* applet)
 
 	ka_applet_menu_add_separator_item (menu);
 
+	/* Ticket dialog */
+	menu_item = gtk_image_menu_item_new_with_mnemonic("_List Tickets");
+	g_signal_connect (G_OBJECT (menu_item), "activate",
+			  G_CALLBACK (ka_applet_cb_show_tickets), applet);
+	gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
+
 	/* Preferences */
 	menu_item = gtk_image_menu_item_new_from_stock(GTK_STOCK_PREFERENCES, NULL);
 	g_signal_connect (G_OBJECT (menu_item), "activate",
@@ -853,6 +867,8 @@ ka_applet_create()
 	applet->priv->gconf = ka_gconf_init (applet);
 	g_return_val_if_fail (applet->priv->gconf != NULL, NULL);
 
+	ka_tickets_dialog_create(applet->priv->uixml);
+
 	return applet;
 }
 
diff --git a/src/krb5-auth-dialog.c b/src/krb5-auth-dialog.c
index 17373e5..3d998ac 100644
--- a/src/krb5-auth-dialog.c
+++ b/src/krb5-auth-dialog.c
@@ -41,6 +41,7 @@
 #include "krb5-auth-pwdialog.h"
 #include "krb5-auth-dbus.h"
 #include "krb5-auth-tools.h"
+#include "krb5-auth-tickets.h"
 
 #ifdef ENABLE_NETWORK_MANAGER
 #include <libnm_glib.h>
@@ -160,6 +161,7 @@ ka_get_error_message(krb5_context context, krb5_error_code err)
 	return msg;
 }
 
+
 static void
 ka_krb5_cc_clear_mcred(krb5_creds* mcred)
 {
@@ -174,6 +176,17 @@ ka_krb5_cc_clear_mcred(krb5_creds* mcred)
 /* ***************************************************************** */
 /* ***************************************************************** */
 
+/* log a kerberos error messge */
+static void
+ka_log_error_message(const char* prefix, krb5_context context, krb5_error_code err)
+{
+	char *errmsg = ka_get_error_message(context, err);
+
+	g_warning("%s: %s", prefix, errmsg);
+	g_free (errmsg);
+}
+
+
 static gboolean
 credentials_expiring_real (KaApplet* applet)
 {
@@ -237,6 +250,95 @@ ka_ccache_filename (void)
 }
 
 
+static void
+ka_format_time (time_t t, gchar *ts, size_t len)
+{
+	g_strlcpy(ts, ctime(&t)+ 4, len);
+	ts[15] = 0;
+}
+
+
+/* fill in service tickets data */
+gboolean
+ka_get_service_tickets (GtkListStore *tickets)
+{
+	krb5_cc_cursor cursor;
+	krb5_creds creds;
+	krb5_error_code ret;
+	GtkTreeIter iter;
+	krb5_ccache ccache;
+	char *name;
+	krb5_timestamp sec;
+	gchar start_time[128], end_time[128], end_time_markup[256];
+	gboolean retval = FALSE;
+
+	gtk_list_store_clear(tickets);
+
+	krb5_timeofday (kcontext, &sec);
+	ret = krb5_cc_default (kcontext, &ccache);
+	g_return_val_if_fail (!ret, FALSE);
+
+	ret = krb5_cc_start_seq_get (kcontext, ccache, &cursor);
+	if (ret) {
+		ka_log_error_message("krb5_cc_start_seq_get", kcontext, ret);
+
+		/* if the file doesn't exist, it's not an error if we can't
+		 * parse it */
+		if (!g_file_test(ka_ccache_filename (),
+				 G_FILE_TEST_EXISTS))
+			retval = TRUE;
+		goto out;
+	}
+
+	while ((ret = krb5_cc_next_cred (kcontext,
+					 ccache,
+					 &cursor,
+					 &creds)) == 0) {
+		if (creds.times.starttime)
+			ka_format_time(creds.times.starttime, start_time,
+				       sizeof(start_time));
+		else
+			ka_format_time(creds.times.authtime, start_time,
+				       sizeof(start_time));
+
+		ka_format_time(creds.times.endtime, end_time,
+			       sizeof(end_time));
+		if (creds.times.endtime > sec)
+			strcpy(end_time_markup, end_time);
+		else
+			g_snprintf(end_time_markup, sizeof(end_time_markup),
+				  "%s <span foreground=\"red\" style=\"italic\">(%s)</span>",
+				  end_time, _("Expired"));
+
+		ret = krb5_unparse_name (kcontext, creds.server, &name);
+		if (!ret) {
+			gtk_list_store_append(tickets, &iter);
+			gtk_list_store_set(tickets, &iter,
+					   PRINCIPAL_COLUMN, name,
+					   START_TIME_COLUMN, start_time,
+					   END_TIME_COLUMN, end_time_markup,
+					   -1);
+			free(name);
+		} else
+			ka_log_error_message("krb5_unparse_name", kcontext, ret);
+		krb5_free_cred_contents (kcontext, &creds);
+	}
+	if(ret != KRB5_CC_END)
+		ka_log_error_message("krb5_cc_get_next", kcontext, ret);
+
+	ret = krb5_cc_end_seq_get (kcontext, ccache, &cursor);
+	if (ret)
+		ka_log_error_message("krb5_cc_end_seq_get", kcontext, ret);
+
+	retval = TRUE;
+out:
+	ret = krb5_cc_close (kcontext, ccache);
+	g_return_val_if_fail (!ret, FALSE);
+
+	return retval;
+}
+
+
 /* Check for things we have to do while the password dialog is open */
 static gboolean
 krb5_auth_dialog_do_updates (gpointer data)
@@ -576,7 +678,7 @@ ccache_changed_cb (GFileMonitor *monitor G_GNUC_UNUSED,
 
 
 static gboolean
-monitor_ccache(KaApplet* applet)
+monitor_ccache(KaApplet *applet)
 {
 	const gchar *ccache_name;
 	GFile *ccache;
@@ -694,7 +796,6 @@ ka_renew_credentials (KaApplet* applet)
 	krb5_creds my_creds;
 	krb5_ccache ccache;
 	krb5_get_init_creds_opt opts;
-	gchar *errmsg = NULL;
 
 	if (kprincipal == NULL) {
 		retval = ka_parse_name(applet, kcontext, &kprincipal);
@@ -723,14 +824,12 @@ ka_renew_credentials (KaApplet* applet)
 
 		retval = krb5_cc_initialize(kcontext, ccache, kprincipal);
 		if(retval) {
-			errmsg = ka_get_error_message(kcontext, retval);
-			g_warning("krb5_cc_initialize: %s", errmsg);
+			ka_log_error_message("krb5_cc_initialize", kcontext, retval);
 			goto out;
 		}
 		retval = krb5_cc_store_cred(kcontext, ccache, &my_creds);
 		if (retval) {
-			errmsg = ka_get_error_message(kcontext, retval);
-			g_warning("krb5_cc_store_cred: %s", errmsg);
+			ka_log_error_message("krb5_cc_store_cred", kcontext, retval);
 			goto out;
 		}
 	}
@@ -738,7 +837,6 @@ out:
 	creds_expiry = my_creds.times.endtime;
 	krb5_free_cred_contents (kcontext, &my_creds);
 	krb5_cc_close (kcontext, ccache);
-	g_free(errmsg);
 	return retval;
 }
 
diff --git a/src/krb5-auth-dialog.h b/src/krb5-auth-dialog.h
index 814f617..ae13159 100644
--- a/src/krb5-auth-dialog.h
+++ b/src/krb5-auth-dialog.h
@@ -26,6 +26,7 @@
 gboolean ka_destroy_ccache (KaApplet* applet);
 gboolean ka_grab_credentials(KaApplet* applet);
 gboolean ka_check_credentials (KaApplet *applet, const char* principal);
+gboolean ka_get_service_tickets(GtkListStore *tickets);
 int ka_tgt_valid_seconds(void);
 #endif
 
diff --git a/src/krb5-auth-dialog.xml b/src/krb5-auth-dialog.xml
index 792b389..f1a9d11 100644
--- a/src/krb5-auth-dialog.xml
+++ b/src/krb5-auth-dialog.xml
@@ -152,4 +152,66 @@
       <action-widget response="-5">krb5_renew_button</action-widget>
     </action-widgets>
   </object>
+  <object class="GtkDialog" id="krb5_tickets_dialog">
+    <property name="border_width">5</property>
+    <property name="title" translatable="yes">Service Tickets</property>
+    <property name="type_hint">normal</property>
+    <property name="has_separator">False</property>
+    <child internal-child="vbox">
+      <object class="GtkVBox" id="tickets-vbox2">
+        <property name="visible">True</property>
+        <property name="orientation">vertical</property>
+        <property name="spacing">2</property>
+        <child>
+          <object class="GtkVBox" id="tickets-vbox1">
+            <property name="visible">True</property>
+            <property name="orientation">vertical</property>
+            <child>
+              <object class="GtkTreeView" id="krb5_tickets_treeview">
+                <property name="visible">True</property>
+                <property name="can_focus">True</property>
+              </object>
+              <packing>
+                <property name="position">0</property>
+              </packing>
+            </child>
+          </object>
+          <packing>
+            <property name="position">1</property>
+          </packing>
+        </child>
+        <child internal-child="action_area">
+          <object class="GtkHButtonBox" id="dialog-action_area">
+            <property name="visible">True</property>
+            <property name="layout_style">end</property>
+            <child>
+              <placeholder/>
+            </child>
+            <child>
+              <object class="GtkButton" id="krb5_tickets_ok">
+                <property name="label">gtk-close</property>
+                <property name="visible">True</property>
+                <property name="can_focus">True</property>
+                <property name="receives_default">True</property>
+                <property name="use_stock">True</property>
+              </object>
+              <packing>
+                <property name="expand">False</property>
+                <property name="fill">False</property>
+                <property name="position">1</property>
+              </packing>
+            </child>
+          </object>
+          <packing>
+            <property name="expand">False</property>
+            <property name="pack_type">end</property>
+            <property name="position">0</property>
+          </packing>
+        </child>
+      </object>
+    </child>
+    <action-widgets>
+      <action-widget response="0">krb5_tickets_ok</action-widget>
+    </action-widgets>
+  </object>
 </interface>
diff --git a/src/krb5-auth-tickets.c b/src/krb5-auth-tickets.c
new file mode 100644
index 0000000..e926324
--- /dev/null
+++ b/src/krb5-auth-tickets.c
@@ -0,0 +1,93 @@
+/* Krb5 Auth Applet -- Acquire and release kerberos tickets
+ *
+ * (C) 2009 Guido Guenther <agx sigxcpu org>
+ *
+ * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ */
+
+#include "config.h"
+
+#include <glib/gi18n.h>
+#include <gtk/gtk.h>
+
+#include "krb5-auth-tickets.h"
+#include "krb5-auth-dialog.h"
+
+static GtkListStore *tickets;
+static GtkWidget    *tickets_dialog;
+
+GtkWidget*
+ka_tickets_dialog_create(GtkBuilder *xml)
+{
+	GtkCellRenderer *renderer;
+	GtkTreeViewColumn *column;
+	GtkTreeView *tickets_view;
+
+	tickets = gtk_list_store_new (N_COLUMNS,
+				      G_TYPE_STRING,
+				      G_TYPE_STRING,
+				      G_TYPE_STRING);
+
+	tickets_dialog = GTK_WIDGET (gtk_builder_get_object (xml, "krb5_tickets_dialog"));
+	tickets_view = GTK_TREE_VIEW (gtk_builder_get_object (xml, "krb5_tickets_treeview"));
+	gtk_tree_view_set_model(GTK_TREE_VIEW(tickets_view), GTK_TREE_MODEL(tickets));
+
+	renderer = gtk_cell_renderer_text_new ();
+	column = gtk_tree_view_column_new_with_attributes("Principal",
+							  renderer,
+							  "text",
+							  PRINCIPAL_COLUMN,
+							  NULL);
+	gtk_tree_view_append_column (GTK_TREE_VIEW (tickets_view), column);
+	column = gtk_tree_view_column_new_with_attributes("Start Time",
+							  renderer,
+							  "text",
+							  START_TIME_COLUMN,
+							  NULL);
+	gtk_tree_view_append_column (GTK_TREE_VIEW (tickets_view), column);
+	column = gtk_tree_view_column_new_with_attributes("End Time",
+							  renderer,
+							  "markup",
+							  END_TIME_COLUMN,
+							  NULL);
+	gtk_tree_view_append_column (GTK_TREE_VIEW (tickets_view), column);
+	return tickets_dialog;
+}
+
+void
+ka_tickets_dialog_run()
+{
+	if (ka_get_service_tickets(tickets)) {
+		gtk_widget_show(tickets_dialog);
+		gtk_dialog_run(GTK_DIALOG(tickets_dialog));
+		gtk_widget_hide(tickets_dialog);
+	} else {
+		GtkWidget *message_dialog;
+
+		message_dialog = gtk_message_dialog_new (NULL,
+					GTK_DIALOG_DESTROY_WITH_PARENT,
+					GTK_MESSAGE_ERROR,
+					GTK_BUTTONS_CLOSE,
+					_("Error displaying service ticket information"));
+		gtk_window_set_resizable (GTK_WINDOW (message_dialog), FALSE);
+
+		g_signal_connect (message_dialog, "response",
+				  G_CALLBACK (gtk_widget_destroy),
+				  NULL);
+		gtk_widget_show (message_dialog);
+	}
+}
+
diff --git a/src/krb5-auth-tickets.h b/src/krb5-auth-tickets.h
new file mode 100644
index 0000000..a5fa869
--- /dev/null
+++ b/src/krb5-auth-tickets.h
@@ -0,0 +1,36 @@
+/* Krb5 Auth Applet -- Acquire and release kerberos tickets
+ *
+ * (C) 2009 Guido Guenther <agx sigxcpu org>
+ *
+ * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ */
+
+#ifndef KRB5_AUTH_TICKETS_H
+#define KRB5_AUTH_TICKETS_H
+
+enum ticket_columns {
+	PRINCIPAL_COLUMN,
+	START_TIME_COLUMN,
+	END_TIME_COLUMN,
+	N_COLUMNS
+};
+
+
+GtkWidget* ka_tickets_dialog_create(GtkBuilder *xml);
+void ka_tickets_dialog_run(void);
+
+
+#endif



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