diff options
author | Vincent Untz <vuntz@novell.com> | 2008-09-18 15:54:33 +0200 |
---|---|---|
committer | Vincent Untz <vuntz@novell.com> | 2008-09-18 15:54:33 +0200 |
commit | b4276a418cda637167be754a9dd5574505485fdf (patch) | |
tree | 0a9f14fe764476286709c362c79bb09e9a34ce1d | |
parent | c5ce506b85670d1a17214f74a8ccfd9b7cd6d49e (diff) |
Add base of code, without methods (based on clock applet code).
-rw-r--r-- | .gitignore | 38 | ||||
-rw-r--r-- | configure.ac | 4 | ||||
-rw-r--r-- | src/Makefile.am | 6 | ||||
-rw-r--r-- | src/cups-pk-helper-mechanism.c | 294 | ||||
-rw-r--r-- | src/cups-pk-helper-mechanism.h | 78 | ||||
-rw-r--r-- | src/cups-pk-helper-mechanism.xml | 5 | ||||
-rw-r--r-- | src/main.c | 147 | ||||
-rw-r--r-- | src/org.opensuse.cups-pk-helper.Mechanism.conf | 17 | ||||
-rw-r--r-- | src/org.opensuse.cups-pk-helper.Mechanism.service.in | 4 | ||||
-rw-r--r-- | src/org.opensuse.cups-pk-helper.mechanism.policy.in | 10 |
10 files changed, 597 insertions, 6 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c12d96e --- /dev/null +++ b/.gitignore @@ -0,0 +1,38 @@ +*.sw[nop] +*.o +INSTALL +Makefile +Makefile.in +aclocal.m4 +autom4te.cache/ +compile +config.guess +config.h +config.h.in +config.log +config.status +config.sub +configure +depcomp +install-sh +intltool-extract.in +intltool-merge.in +intltool-update.in +libtool +ltmain.sh +missing +mkinstalldirs +po/.intltool-merge-cache +po/Makefile +po/Makefile.in +po/Makefile.in.in +po/POTFILES +po/stamp-it +src/.deps/ +src/Makefile +src/Makefile.in +src/cups-pk-helper-mechanism +src/cups-pk-helper-mechanism-glue.h +src/org.opensuse.cups-pk-helper.Mechanism.service +src/org.opensuse.cups-pk-helper.mechanism.policy +stamp-h1 diff --git a/configure.ac b/configure.ac index 71ce081..4b42b54 100644 --- a/configure.ac +++ b/configure.ac @@ -43,7 +43,6 @@ GLIB_REQUIRED=2.14.0 DBUS_REQUIRED=1.1.2 DBUS_GLIB_REQUIRED=0.74 POLKIT_DBUS_REQUIRED=0.8 -POLKIT_GNOME_REQUIRED=0.8 # pkg-config dependency checks PKG_CHECK_MODULES(CUPS_PK, glib-2.0 >= $GLIB_REQUIRED \ @@ -52,8 +51,7 @@ PKG_CHECK_MODULES(CUPS_PK, glib-2.0 >= $GLIB_REQUIRED \ gthread-2.0 \ dbus-1 >= $DBUS_REQUIRED \ dbus-glib-1 >= $DBUS_GLIB_REQUIRED \ - polkit-dbus >= $POLKIT_DBUS_REQUIRED \ - polkit-gnome >= $POLKIT_GNOME_REQUIRED) + polkit-dbus >= $POLKIT_DBUS_REQUIRED) AC_SUBST(CUPS_PK_CFLAGS) AC_SUBST(CUPS_PK_LIBS) diff --git a/src/Makefile.am b/src/Makefile.am index 9ad394d..bd6e2d3 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -4,9 +4,9 @@ INCLUDES = \ libexec_PROGRAMS = cups-pk-helper-mechanism -cups-pk-helper-glue.h: $(srcdir)/cups-pk-helper-mechanism.xml - dbus-binding-tool --prefix=cups_pk_helper --mode=glib-server \ - --output=cups-pk-helper-glue.h \ +cups-pk-helper-mechanism-glue.h: $(srcdir)/cups-pk-helper-mechanism.xml + dbus-binding-tool --prefix=cph_mechanism --mode=glib-server \ + --output=cups-pk-helper-mechanism-glue.h \ $(srcdir)/cups-pk-helper-mechanism.xml cups_pk_helper_mechanism_SOURCES = \ diff --git a/src/cups-pk-helper-mechanism.c b/src/cups-pk-helper-mechanism.c new file mode 100644 index 0000000..64f1487 --- /dev/null +++ b/src/cups-pk-helper-mechanism.c @@ -0,0 +1,294 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- + * + * Copyright (C) 2008 Novell, Inc. + * + * Authors: Vincent Untz + * + * 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. + * + * The code is originally based on gnome-clock-applet-mechanism.c, which + * is under the same license and with the following copyright: + * + * Copyright (C) 2007 David Zeuthen <david@fubar.dk> + * + */ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include <stdlib.h> +#include <stdio.h> +#include <fcntl.h> +#include <unistd.h> +#include <string.h> +#include <sys/wait.h> +#include <errno.h> +#include <sys/time.h> + +#include <glib.h> +#include <glib-object.h> + +#include <dbus/dbus-glib.h> +#include <dbus/dbus-glib-lowlevel.h> + +#include <polkit-dbus/polkit-dbus.h> + +#include "cups-pk-helper-mechanism.h" +#include "cups-pk-helper-mechanism-glue.h" + +/* exit timer */ + +static gboolean +do_exit (gpointer user_data) +{ + exit (0); + + return FALSE; +} + +static void +reset_killtimer (void) +{ + static guint timer_id = 0; + + if (timer_id > 0) + g_source_remove (timer_id); + + timer_id = g_timeout_add_seconds (30, do_exit, NULL); +} + +/* error */ + +GQuark +cph_mechanism_error_quark (void) +{ + static GQuark ret = 0; + + if (ret == 0) + ret = g_quark_from_static_string ("cph_mechanism_error"); + + return ret; +} + +#define ENUM_ENTRY(NAME, DESC) { NAME, "" #NAME "", DESC } + +GType +cph_mechanism_error_get_type (void) +{ + static GType etype = 0; + + if (etype == 0) { + static const GEnumValue values[] = + { + ENUM_ENTRY (CPH_MECHANISM_ERROR_GENERAL, + "GeneralError"), + ENUM_ENTRY (CPH_MECHANISM_ERROR_NOT_PRIVILEGED, + "NotPrivileged"), + { 0, 0, 0 } + }; + + g_assert (CPH_MECHANISM_NUM_ERRORS == G_N_ELEMENTS (values) - 1); + + etype = g_enum_register_static ("CphMechanismError", values); + } + + return etype; +} + +/* mechanism object */ + +G_DEFINE_TYPE (CphMechanism, cph_mechanism, G_TYPE_OBJECT) + +#define CPH_MECHANISM_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), CPH_TYPE_MECHANISM, CphMechanismPrivate)) + +struct CphMechanismPrivate +{ + DBusGConnection *system_bus_connection; + PolKitContext *pol_ctx; +}; + +static void +cph_mechanism_class_init (CphMechanismClass *klass) +{ + g_type_class_add_private (klass, sizeof (CphMechanismPrivate)); + + dbus_g_object_type_install_info (CPH_TYPE_MECHANISM, + &dbus_glib_cph_mechanism_object_info); + + dbus_g_error_domain_register (CPH_MECHANISM_ERROR, NULL, + CPH_MECHANISM_TYPE_ERROR); +} + +static void +cph_mechanism_init (CphMechanism *mechanism) +{ + mechanism->priv = CPH_MECHANISM_GET_PRIVATE (mechanism); +} + +static gboolean +pk_io_watch_have_data (GIOChannel *channel, + GIOCondition condition, + gpointer user_data) +{ + int fd; + PolKitContext *pk_context; + + pk_context = user_data; + fd = g_io_channel_unix_get_fd (channel); + polkit_context_io_func (pk_context, fd); + + return TRUE; +} + +static int +pk_io_add_watch (PolKitContext *pk_context, + int fd) +{ + guint id; + GIOChannel *channel; + + channel = g_io_channel_unix_new (fd); + if (channel == NULL) + return 0; + + id = g_io_add_watch (channel, G_IO_IN, + pk_io_watch_have_data, pk_context); + + return id; +} + +static void +pk_io_remove_watch (PolKitContext *pk_context, + int watch_id) +{ + g_source_remove (watch_id); +} + +static gboolean +register_mechanism (CphMechanism *mechanism) +{ + GError *error; + + mechanism->priv->pol_ctx = polkit_context_new (); + + polkit_context_set_io_watch_functions (mechanism->priv->pol_ctx, + pk_io_add_watch, + pk_io_remove_watch); + + if (!polkit_context_init (mechanism->priv->pol_ctx, NULL)) { + g_critical ("cannot initialize libpolkit"); + return FALSE; + } + + error = NULL; + mechanism->priv->system_bus_connection = dbus_g_bus_get (DBUS_BUS_SYSTEM, + &error); + if (mechanism->priv->system_bus_connection == NULL) { + if (error != NULL) { + g_critical ("error getting system bus: %s", + error->message); + g_error_free (error); + } else { + g_critical ("error getting system bus"); + } + return FALSE; + } + + dbus_g_connection_register_g_object (mechanism->priv->system_bus_connection, "/", + G_OBJECT (mechanism)); + + reset_killtimer (); + + return TRUE; +} + + +CphMechanism * +cph_mechanism_new (void) +{ + GObject *object; + + object = g_object_new (CPH_TYPE_MECHANISM, NULL); + + if (!register_mechanism (CPH_MECHANISM (object))) { + g_object_unref (object); + return NULL; + } + + return CPH_MECHANISM (object); +} + +static gboolean +_check_polkit_for_action (CphMechanism *mechanism, + DBusGMethodInvocation *context, + const char *action) +{ + const char *sender; + GError *error; + DBusError dbus_error; + PolKitCaller *pk_caller; + PolKitAction *pk_action; + PolKitResult pk_result; + + error = NULL; + + /* Check that caller is privileged */ + sender = dbus_g_method_get_sender (context); + dbus_error_init (&dbus_error); + + pk_caller = polkit_caller_new_from_dbus_name ( + dbus_g_connection_get_connection (mechanism->priv->system_bus_connection), + sender, + &dbus_error); + + if (pk_caller == NULL) { + error = g_error_new (CPH_MECHANISM_ERROR, + CPH_MECHANISM_ERROR_GENERAL, + "Error getting information about " + "caller: %s: %s", + dbus_error.name, dbus_error.message); + dbus_error_free (&dbus_error); + dbus_g_method_return_error (context, error); + g_error_free (error); + + return FALSE; + } + + pk_action = polkit_action_new (); + polkit_action_set_action_id (pk_action, action); + pk_result = polkit_context_is_caller_authorized (mechanism->priv->pol_ctx, + pk_action, pk_caller, + FALSE, NULL); + polkit_caller_unref (pk_caller); + polkit_action_unref (pk_action); + + if (pk_result != POLKIT_RESULT_YES) { + error = g_error_new (CPH_MECHANISM_ERROR, + CPH_MECHANISM_ERROR_NOT_PRIVILEGED, + "%s %s <-- (action, result)", + action, + polkit_result_to_string_representation (pk_result)); + dbus_error_free (&dbus_error); + dbus_g_method_return_error (context, error); + g_error_free (error); + + return FALSE; + } + + return TRUE; +} + +/* exported methods */ diff --git a/src/cups-pk-helper-mechanism.h b/src/cups-pk-helper-mechanism.h new file mode 100644 index 0000000..ae14a52 --- /dev/null +++ b/src/cups-pk-helper-mechanism.h @@ -0,0 +1,78 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- + * + * Copyright (C) 2008 Novell, Inc. + * + * Authors: Vincent Untz + * + * 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. + * + * The code is originally based on gnome-clock-applet-mechanism.h, which + * is under the same license and with the following copyright: + * + * Copyright (C) 2007 David Zeuthen <david@fubar.dk> + * + */ + +#ifndef CPH_MECHANISM_H +#define CPH_MECHANISM_H + +#include <glib-object.h> +#include <dbus/dbus-glib.h> + +G_BEGIN_DECLS + +#define CPH_TYPE_MECHANISM (cph_mechanism_get_type ()) +#define CPH_MECHANISM(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), CPH_TYPE_MECHANISM, CphMechanism)) +#define CPH_MECHANISM_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), CPH_TYPE_MECHANISM, CphMechanismClass)) +#define CPH_IS_MECHANISM(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), CPH_TYPE_MECHANISM)) +#define CPH_IS_MECHANISM_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), CPH_TYPE_MECHANISM)) +#define CPH_MECHANISM_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), CPH_TYPE_MECHANISM, CphMechanismClass)) + +typedef struct CphMechanismPrivate CphMechanismPrivate; + +typedef struct +{ + GObject parent; + CphMechanismPrivate *priv; +} CphMechanism; + +typedef struct +{ + GObjectClass parent_class; +} CphMechanismClass; + +typedef enum +{ + CPH_MECHANISM_ERROR_GENERAL, + CPH_MECHANISM_ERROR_NOT_PRIVILEGED, + CPH_MECHANISM_NUM_ERRORS +} CphMechanismError; + +#define CPH_MECHANISM_ERROR cph_mechanism_error_quark () + +GType cph_mechanism_error_get_type (void); +#define CPH_MECHANISM_TYPE_ERROR (cph_mechanism_error_get_type ()) + + +GQuark cph_mechanism_error_quark (void); +GType cph_mechanism_get_type (void); + +CphMechanism *cph_mechanism_new (void); + +/* exported methods */ + +G_END_DECLS + +#endif /* CPH_MECHANISM_H */ diff --git a/src/cups-pk-helper-mechanism.xml b/src/cups-pk-helper-mechanism.xml new file mode 100644 index 0000000..63975de --- /dev/null +++ b/src/cups-pk-helper-mechanism.xml @@ -0,0 +1,5 @@ +<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd"> +<node name="/"> + <interface name="org.opensuse.cups-pk-helper.Mechanism"> + </interface> +</node> diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..c77ddbd --- /dev/null +++ b/src/main.c @@ -0,0 +1,147 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- + * + * Copyright (C) 2008 Novell, Inc. + * + * Authors: Vincent Untz + * + * 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. + * + * The code is originally based on gnome-clock-applet-mechanism-main.c, which + * is under the same license and with the following copyright: + * + * Copyright (C) 2007 David Zeuthen <david@fubar.dk> + * + */ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include <stdlib.h> +#include <stdio.h> +#include <unistd.h> +#include <signal.h> +#include <errno.h> +#include <string.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> + +#include <glib.h> +#include <glib-object.h> + +#include <dbus/dbus-glib.h> +#include <dbus/dbus-glib-lowlevel.h> + +#include "cups-pk-helper-mechanism.h" + +#define BUS_NAME "org.opensuse.cups-pk-helper.Mechanism" + +static DBusGProxy * +get_bus_proxy (DBusGConnection *connection) +{ + DBusGProxy *bus_proxy; + + bus_proxy = dbus_g_proxy_new_for_name (connection, + DBUS_SERVICE_DBUS, + DBUS_PATH_DBUS, + DBUS_INTERFACE_DBUS); + return bus_proxy; +} + +static gboolean +acquire_name_on_proxy (DBusGProxy *bus_proxy, + GError **error) +{ + guint result; + gboolean res; + + g_assert (bus_proxy != NULL); + + res = dbus_g_proxy_call (bus_proxy, + "RequestName", + error, + G_TYPE_STRING, BUS_NAME, + G_TYPE_UINT, 0, + G_TYPE_INVALID, + G_TYPE_UINT, &result, + G_TYPE_INVALID); + if (!res || + result != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) + return FALSE; + + return TRUE; +} + +int +main (int argc, char **argv) +{ + GError *error; + GMainLoop *loop; + CphMechanism *mechanism; + DBusGProxy *bus_proxy; + DBusGConnection *connection; + int ret; + + if (!g_thread_supported ()) + g_thread_init (NULL); + + dbus_g_thread_init (); + g_type_init (); + + error = NULL; + + connection = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error); + if (connection == NULL) { + g_warning ("Could not connect to system bus: %s", + error->message); + g_error_free (error); + return 1; + } + + bus_proxy = get_bus_proxy (connection); + if (bus_proxy == NULL) { + g_warning ("Could not construct bus_proxy objects"); + return 1; + } + + if (!acquire_name_on_proxy (bus_proxy, &error) ) { + if (error != NULL) { + g_warning ("Could not acquire name: %s", + error->message); + g_error_free (error); + } else { + g_warning ("Could not acquire name"); + } + + return 1; + } + + mechanism = cph_mechanism_new (); + + if (mechanism == NULL) { + g_warning ("Could not create mechanism object"); + return 1; + } + + loop = g_main_loop_new (NULL, FALSE); + + g_main_loop_run (loop); + + g_object_unref (mechanism); + g_main_loop_unref (loop); + + return 0; +} diff --git a/src/org.opensuse.cups-pk-helper.Mechanism.conf b/src/org.opensuse.cups-pk-helper.Mechanism.conf new file mode 100644 index 0000000..e519ff8 --- /dev/null +++ b/src/org.opensuse.cups-pk-helper.Mechanism.conf @@ -0,0 +1,17 @@ +<?xml version="1.0" encoding="UTF-8"?> <!-- -*- XML -*- --> + +<!DOCTYPE busconfig PUBLIC + "-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN" + "http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd"> +<busconfig> + + <!-- Only root can own the service --> + <policy user="root"> + <allow own="org.opensuse.cups-pk-helper.Mechanism"/> + </policy> + + <!-- Allow anyone to invoke methods on the interfaces --> + <policy context="default"> + </policy> + +</busconfig> diff --git a/src/org.opensuse.cups-pk-helper.Mechanism.service.in b/src/org.opensuse.cups-pk-helper.Mechanism.service.in new file mode 100644 index 0000000..6751364 --- /dev/null +++ b/src/org.opensuse.cups-pk-helper.Mechanism.service.in @@ -0,0 +1,4 @@ +[D-BUS Service] +Name=org.opensuse.cups-pk-helper.Mechanism +Exec=@LIBEXECDIR@/cups-pk-helper-mechanism +User=root diff --git a/src/org.opensuse.cups-pk-helper.mechanism.policy.in b/src/org.opensuse.cups-pk-helper.mechanism.policy.in new file mode 100644 index 0000000..27b89b7 --- /dev/null +++ b/src/org.opensuse.cups-pk-helper.mechanism.policy.in @@ -0,0 +1,10 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE policyconfig PUBLIC + "-//freedesktop//DTD PolicyKit Policy Configuration 1.0//EN" + "http://www.freedesktop.org/standards/PolicyKit/1.0/policyconfig.dtd"> + +<policyconfig> + <vendor>The openSUSE Project</vendor> + <vendor_url>http://www.opensuse.org/</vendor_url> + <icon_name>printer</icon_name> +</policyconfig> |