1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
2
/***************************************************************************
4
* polkit-gnome-auth.c : Show authentication dialogs to gain privileges
6
* Copyright (C) 2007 David Zeuthen, <david@fubar.dk>
8
* This library is free software; you can redistribute it and/or
9
* modify it under the terms of the GNU Lesser General Public License
10
* as published by the Free Software Foundation; either version 2 of
11
* the License, or (at your option) any later version.
13
* This library is distributed in the hope that it will be useful, but
14
* WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
* Lesser General Public License for more details.
18
* You should have received a copy of the GNU Lesser General Public
19
* License along with this library; if not, write to the Free Software
20
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23
**************************************************************************/
35
#include <sys/types.h>
41
#include <glib/gi18n-lib.h>
42
#include <glib-object.h>
47
#include <dbus/dbus-glib.h>
48
#include <dbus/dbus-glib-lowlevel.h>
50
#include "polkit-gnome-auth.h"
53
* SECTION:polkit-gnome-auth
54
* @title: Authentication Dialogs
55
* @short_description: Show authentication dialogs to gain privileges
57
* Show authentication dialogs to gain privileges.
64
PolKitGnomeAuthCB callback;
69
_notify_callback (DBusGProxy *proxy, DBusGProxyCall *call, void *user_data)
72
CallClosure *c = (CallClosure *) user_data;
73
gboolean gained_privilege;
76
if (!dbus_g_proxy_end_call (proxy, call, &error, G_TYPE_BOOLEAN, &gained_privilege, G_TYPE_INVALID)) {
77
gained_privilege = FALSE;
80
/* perform the callback */
81
c->callback (c->action, gained_privilege, error, c->user_data);
83
g_object_unref (proxy);
84
polkit_action_unref (c->action);
88
* polkit_gnome_auth_obtain:
89
* @action: The #PolKitAction to make the user authenticate for
90
* @xid: X11 window ID for the window that the dialog will be transient for. If there is no window, pass 0.
91
* @pid: Process ID of process to grant authorization to. Normally one wants to pass result of getpid().
92
* @callback: Function to call when authentication is done
93
* @user_data: Data to pass to the callback function
94
* @error: Return location for error
96
* Applications can use this function to show a dialog for the user
97
* asking her to authenticate in order to gain privileges to do the
98
* given action. The authentication, for security reasons, happens in
99
* a separate process; this function is merely a wrapper around a
100
* D-Bus call across the session message bus to the
101
* <literal>org.freedesktop.PolicyKit.AuthenticationAgent</literal>
102
* service. Depending on the setup, this may be the Authentication
103
* Agent shipped with PolicyKit-gnome or it may be another
104
* implementation. For example, if the user is in KDE it may be an
105
* Authentication Agent using the Qt toolkit.
107
* The Authentication Agent shipped with PolicyKit-gnome is described
108
* in <link linkend="ref-auth-daemon">this section</link>.
110
* This function is similar to the polkit_auth_obtain() function
111
* supplied in <literal>libpolkit-dbus</literal> except that this
112
* function is asynchronous.
114
* Returns: #TRUE if the authentication session was scheduled to
115
* start. #FALSE if error is set (and no callback will be made).
118
polkit_gnome_auth_obtain (PolKitAction *action,
121
PolKitGnomeAuthCB callback,
125
char *polkit_action_id;
128
DBusGConnection *session_bus;
129
DBusGProxy *polkit_gnome_proxy;
133
if ((session_bus = dbus_g_bus_get (DBUS_BUS_SESSION, error)) == NULL) {
137
/* TODO: this can fail.. */
138
polkit_action_get_action_id (action, &polkit_action_id);
140
polkit_gnome_proxy = dbus_g_proxy_new_for_name (session_bus,
141
"org.freedesktop.PolicyKit.AuthenticationAgent", /* bus name */
143
"org.freedesktop.PolicyKit.AuthenticationAgent");/* interface */
145
c = g_new0 (CallClosure, 1);
146
c->action = polkit_action_ref (action);
147
c->callback = callback;
148
c->user_data = user_data;
150
dbus_g_proxy_begin_call_with_timeout (polkit_gnome_proxy,
157
G_TYPE_STRING, polkit_action_id, /* action_id */
158
G_TYPE_UINT, xid, /* X11 window ID */