~indicator-applet-developers/indicator-session/trunk.0.1

« back to all changes in this revision

Viewing changes to src/gtk-dialog/ck-pk-helper.c

  • Committer: Ted Gould
  • Date: 2009-07-27 04:47:27 UTC
  • mfrom: (9.1.94 fusa.libindicator)
  • Revision ID: ted@canonical.com-20090727044727-m04ul5d8sxqt2pd4
Merging in the branch to port everything to libindicator and fleshing thigns out.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
#include <unistd.h>
 
3
#include <glib.h>
 
4
#include <dbus/dbus-glib.h>
 
5
#include <polkit-gnome/polkit-gnome.h>
 
6
 
 
7
#include "logout-dialog.h"
 
8
#include "ck-pk-helper.h"
 
9
 
 
10
static gboolean
 
11
ck_multiple_users (void)
 
12
{
 
13
        DBusGConnection * sbus = dbus_g_bus_get(DBUS_BUS_SYSTEM, NULL);
 
14
        g_return_val_if_fail(sbus != NULL, TRUE); /* worst case */
 
15
        DBusGProxy * proxy = dbus_g_proxy_new_for_name(sbus, "org.freedesktop.ConsoleKit",
 
16
                                                             "/org/freedesktop/ConsoleKit/Manager",
 
17
                                                             "org.freedesktop.ConsoleKit.Manager");
 
18
 
 
19
        if (proxy == NULL) {
 
20
                return TRUE;
 
21
        }
 
22
 
 
23
        gboolean result;
 
24
        GPtrArray * seats = NULL;
 
25
 
 
26
        result = dbus_g_proxy_call(proxy, "GetSeats", NULL, G_TYPE_INVALID,
 
27
                                   dbus_g_type_get_collection("GPtrArray", DBUS_TYPE_G_OBJECT_PATH), &seats, G_TYPE_INVALID);
 
28
 
 
29
        if (!result) {
 
30
                g_warning("Unable to get the seats for ConsoleKit");
 
31
                g_object_unref(proxy);
 
32
                return TRUE;
 
33
        }
 
34
        
 
35
        gchar * this_session_id = NULL;
 
36
        
 
37
        result = dbus_g_proxy_call(proxy, "GetCurrentSession", NULL, G_TYPE_INVALID,
 
38
                                                        DBUS_TYPE_G_OBJECT_PATH, &this_session_id, G_TYPE_INVALID);
 
39
                                                        
 
40
        g_object_unref(proxy);
 
41
                                                        
 
42
        if (!result) {
 
43
                g_warning("Unable to get current session from ConsoleKit");
 
44
                return TRUE;
 
45
        }
 
46
        
 
47
        proxy = dbus_g_proxy_new_for_name(sbus, "org.freedesktop.ConsoleKit",
 
48
                                                                        this_session_id, "org.freedesktop.ConsoleKit.Session");
 
49
 
 
50
        if (proxy == NULL) {
 
51
                return TRUE;
 
52
        }
 
53
        
 
54
        guint this_session_uid;
 
55
        
 
56
        result = dbus_g_proxy_call(proxy, "GetUnixUser", NULL, G_TYPE_INVALID,
 
57
                                                        G_TYPE_UINT, &this_session_uid, G_TYPE_INVALID);                                                                                
 
58
 
 
59
        if (!result) {
 
60
                g_warning("Unable to get UID from ConsoleKit");
 
61
                return TRUE;
 
62
        }
 
63
        
 
64
        guint seat;
 
65
        gboolean multiple_users = FALSE;
 
66
        for (seat = 0; seat < seats->len; seat++) {
 
67
                gchar * seat_id = g_ptr_array_index(seats, seat);
 
68
                DBusGProxy * seat_proxy = dbus_g_proxy_new_for_name(sbus, "org.freedesktop.ConsoleKit",
 
69
                                                                    seat_id, "org.freedesktop.ConsoleKit.Seat");
 
70
                g_free(seat_id);
 
71
 
 
72
                if (seat_proxy == NULL) {
 
73
                        continue;
 
74
                }
 
75
 
 
76
                GPtrArray * sessions = NULL;
 
77
 
 
78
                gboolean result = dbus_g_proxy_call(seat_proxy,
 
79
                                                    "GetSessions", NULL, G_TYPE_INVALID,
 
80
                                                dbus_g_type_get_collection("GPtrArray", DBUS_TYPE_G_OBJECT_PATH), &sessions, G_TYPE_INVALID);
 
81
 
 
82
                g_object_unref(seat_proxy);
 
83
                if (!result) {
 
84
                        continue;
 
85
                }
 
86
 
 
87
                guint session;
 
88
                for (session = 0; session < sessions->len; session++) {
 
89
                        gchar * session_id = g_ptr_array_index(sessions, session);
 
90
                        if (g_strcmp0(this_session_id, session_id) == 0) {
 
91
                                continue;
 
92
                        }
 
93
                        DBusGProxy * session_proxy = dbus_g_proxy_new_for_name(sbus, "org.freedesktop.ConsoleKit",
 
94
                                                                        session_id, "org.freedesktop.ConsoleKit.Session");
 
95
                    g_free(session_id);
 
96
                    
 
97
                    if (session_proxy == NULL) {
 
98
                        continue;
 
99
                    }
 
100
                    
 
101
                    guint session_uid;
 
102
                    result =  dbus_g_proxy_call(session_proxy, "GetUnixUser", NULL, G_TYPE_INVALID,
 
103
                                                                        G_TYPE_UINT, &session_uid, G_TYPE_INVALID);     
 
104
                    g_object_unref(session_proxy);
 
105
                                                                        
 
106
                    if (!result) {
 
107
                        continue;
 
108
                    }
 
109
                    
 
110
                    if (session_uid != this_session_uid) {
 
111
                        multiple_users = TRUE;
 
112
                        break;
 
113
                    }
 
114
                }
 
115
                
 
116
                g_ptr_array_free(sessions, TRUE);
 
117
                
 
118
                if (multiple_users) {
 
119
                        break;
 
120
                }
 
121
        }
 
122
 
 
123
        g_ptr_array_free(seats, TRUE);
 
124
        g_object_unref(proxy);
 
125
        g_free(this_session_id);
 
126
 
 
127
        return multiple_users;
 
128
}
 
129
 
 
130
gboolean
 
131
pk_require_auth (LogoutDialogAction action) {
 
132
        if (action == LOGOUT_DIALOG_LOGOUT) {
 
133
                return FALSE;
 
134
        }
 
135
 
 
136
        gchar * pk_action;
 
137
        if (ck_multiple_users()) {
 
138
                if (action == LOGOUT_DIALOG_RESTART) {
 
139
                        pk_action = "org.freedesktop.consolekit.system.restart-multiple-users";
 
140
                } else {
 
141
                        pk_action = "org.freedesktop.consolekit.system.stop-multiple-users";
 
142
                }
 
143
        } else {
 
144
                if (action == LOGOUT_DIALOG_RESTART) {
 
145
                        pk_action = "org.freedesktop.consolekit.system.restart";
 
146
                } else {
 
147
                        pk_action = "org.freedesktop.consolekit.system.stop";
 
148
                }
 
149
        }
 
150
 
 
151
        PolKitResult polres;
 
152
        if (pk_can_do_action(pk_action, &polres)) {
 
153
                if (polres == POLKIT_RESULT_YES) {
 
154
                        return FALSE;
 
155
                }
 
156
                return TRUE;
 
157
        }
 
158
        return FALSE;
 
159
}
 
160
 
 
161
gboolean
 
162
pk_can_do_action (const gchar    *action_id, PolKitResult * pol_result)
 
163
{
 
164
        PolKitGnomeContext *gnome_context;
 
165
        PolKitAction *action;
 
166
        PolKitCaller *caller;
 
167
        DBusError dbus_error;
 
168
        PolKitError *error;
 
169
        PolKitResult result;
 
170
 
 
171
        gnome_context = polkit_gnome_context_get (NULL);
 
172
 
 
173
        if (gnome_context == NULL) {
 
174
                return FALSE;
 
175
        }
 
176
 
 
177
        if (gnome_context->pk_tracker == NULL) {
 
178
                return FALSE;
 
179
        }
 
180
 
 
181
        dbus_error_init (&dbus_error);
 
182
        caller = polkit_tracker_get_caller_from_pid (gnome_context->pk_tracker,
 
183
                                                     getpid (),
 
184
                                                     &dbus_error);
 
185
        dbus_error_free (&dbus_error);
 
186
 
 
187
        if (caller == NULL) {
 
188
                return FALSE;
 
189
        }
 
190
 
 
191
        action = polkit_action_new ();
 
192
        if (!polkit_action_set_action_id (action, action_id)) {
 
193
                polkit_action_unref (action);
 
194
                polkit_caller_unref (caller);
 
195
                return FALSE;
 
196
        }
 
197
 
 
198
        result = POLKIT_RESULT_UNKNOWN;
 
199
        error = NULL;
 
200
        result = polkit_context_is_caller_authorized (gnome_context->pk_context,
 
201
                                                      action, caller, FALSE,
 
202
                                                      &error);
 
203
        if (polkit_error_is_set (error)) {
 
204
                polkit_error_free (error);
 
205
        }
 
206
        polkit_action_unref (action);
 
207
                polkit_caller_unref (caller);
 
208
 
 
209
                if (pol_result != NULL) {
 
210
                        *pol_result = result;
 
211
                }
 
212
 
 
213
        return result != POLKIT_RESULT_NO && result != POLKIT_RESULT_UNKNOWN;
 
214
}