~ubuntu-branches/ubuntu/quantal/colord/quantal-proposed

« back to all changes in this revision

Viewing changes to src/cd-common.c

  • Committer: Package Import Robot
  • Author(s): Sjoerd Simons
  • Date: 2011-10-25 16:21:20 UTC
  • mto: (2.1.1 sid) (1.1.2)
  • mto: This revision was merged to the branch mainline in revision 8.
  • Revision ID: package-import@ubuntu.com-20111025162120-0aypjqn1zx9n6vgf
Tags: upstream-0.1.13
ImportĀ upstreamĀ versionĀ 0.1.13

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
 
22
22
#include "config.h"
23
23
 
 
24
#ifdef USE_POLKIT
24
25
#include <polkit/polkit.h>
 
26
#endif
25
27
 
26
28
#include "cd-common.h"
27
29
 
37
39
                g_dbus_error_register_error (quark,
38
40
                                             CD_MAIN_ERROR_FAILED,
39
41
                                             COLORD_DBUS_SERVICE ".Failed");
 
42
                g_dbus_error_register_error (quark,
 
43
                                             CD_MAIN_ERROR_ALREADY_EXISTS,
 
44
                                             COLORD_DBUS_SERVICE ".AlreadyExists");
40
45
        }
41
46
        return quark;
42
47
}
58
63
}
59
64
 
60
65
/**
 
66
 * cd_main_get_sender_uid:
 
67
 **/
 
68
guint
 
69
cd_main_get_sender_uid (GDBusMethodInvocation *invocation, GError **error)
 
70
{
 
71
        const gchar *sender;
 
72
        GDBusConnection *connection;
 
73
        guint uid = G_MAXUINT;
 
74
        GVariant *value;
 
75
 
 
76
        /* call into DBus to get the user ID that issued the request */
 
77
        connection = g_dbus_method_invocation_get_connection (invocation);
 
78
        sender = g_dbus_method_invocation_get_sender (invocation);
 
79
        value = g_dbus_connection_call_sync (connection,
 
80
                                             "org.freedesktop.DBus",
 
81
                                             "/org/freedesktop/DBus",
 
82
                                             "org.freedesktop.DBus",
 
83
                                             "GetConnectionUnixUser",
 
84
                                             g_variant_new ("(s)",
 
85
                                                            sender),
 
86
                                             NULL,
 
87
                                             G_DBUS_CALL_FLAGS_NONE,
 
88
                                             200,
 
89
                                             NULL,
 
90
                                             error);
 
91
        if (value != NULL) {
 
92
                g_variant_get (value, "(u)", &uid);
 
93
                g_variant_unref (value);
 
94
        }
 
95
        return uid;
 
96
}
 
97
 
 
98
/**
61
99
 * cd_main_sender_authenticated:
62
100
 **/
63
101
gboolean
64
102
cd_main_sender_authenticated (GDBusMethodInvocation *invocation,
65
 
                              const gchar *sender,
66
103
                              const gchar *action_id)
67
104
{
68
 
#ifdef USE_POLKIT
 
105
        const gchar *sender;
69
106
        gboolean ret = FALSE;
70
107
        GError *error = NULL;
 
108
        guint uid;
 
109
#ifdef USE_POLKIT
71
110
        PolkitAuthorizationResult *result = NULL;
72
111
        PolkitSubject *subject = NULL;
73
112
        PolkitAuthority *authority = NULL;
74
 
 
 
113
#endif
 
114
 
 
115
        /* uid 0 is allowed to do all actions */
 
116
        sender = g_dbus_method_invocation_get_sender (invocation);
 
117
        uid = cd_main_get_sender_uid (invocation, &error);
 
118
        if (uid == G_MAXUINT) {
 
119
                g_dbus_method_invocation_return_error (invocation,
 
120
                                                       CD_MAIN_ERROR,
 
121
                                                       CD_MAIN_ERROR_FAILED,
 
122
                                                       "could not get uid to authenticate %s: %s",
 
123
                                                       action_id,
 
124
                                                       error->message);
 
125
                g_error_free (error);
 
126
                goto out;
 
127
        }
 
128
 
 
129
        /* the root user can always do all actions */
 
130
        if (uid == 0) {
 
131
                g_debug ("CdCommon: not checking %s for %s as uid 0",
 
132
                         action_id, sender);
 
133
                ret = TRUE;
 
134
                goto out;
 
135
        }
 
136
 
 
137
#ifdef USE_POLKIT
75
138
        /* get authority */
76
139
        authority = polkit_authority_get_sync (NULL, &error);
77
140
        if (authority == NULL) {
110
173
                                                       action_id);
111
174
                goto out;
112
175
        }
 
176
#else
 
177
        g_warning ("CdCommon: not checking %s for %s as no PolicyKit support",
 
178
                   action_id, sender);
 
179
#endif
113
180
 
114
181
        /* success */
115
182
        ret = TRUE;
116
183
out:
 
184
#ifdef USE_POLKIT
117
185
        if (authority != NULL)
118
186
                g_object_unref (authority);
119
187
        if (result != NULL)
120
188
                g_object_unref (result);
121
189
        if (subject != NULL)
122
190
                g_object_unref (subject);
 
191
#endif
123
192
        return ret;
124
 
#else
125
 
        g_warning ("CdCommon: not checking %s for %s as no PolicyKit support",
126
 
                   action_id, sender);
127
 
        return TRUE;
128
 
#endif
129
193
}
130
194
 
131
195