| 1 |
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- |
| 2 |
* |
| 3 |
* Copyright (C) 2010 Richard Hughes <[email protected]> |
| 4 |
* |
| 5 |
* Licensed under the GNU General Public License Version 2 |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 2 of the License, or |
| 10 |
* (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this program; if not, write to the Free Software |
| 19 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "config.h" |
| 23 |
|
| 24 |
#include <string.h> |
| 25 |
#include <polkit/polkit.h> |
| 26 |
|
| 27 |
#include "cd-common.h" |
| 28 |
|
| 29 |
#if !defined(POLKIT_HAS_AUTOPTR_MACROS) |
| 30 |
G_DEFINE_AUTOPTR_CLEANUP_FUNC(PolkitAuthorizationResult, g_object_unref) |
| 31 |
G_DEFINE_AUTOPTR_CLEANUP_FUNC(PolkitSubject, g_object_unref) |
| 32 |
G_DEFINE_AUTOPTR_CLEANUP_FUNC(PolkitAuthority, g_object_unref) |
| 33 |
#endif |
| 34 |
|
| 35 |
GQuark |
| 36 |
cd_client_error_quark (void) |
| 37 |
{ |
| 38 |
guint i; |
| 39 |
static GQuark quark = 0; |
| 40 |
if (!quark) { |
| 41 |
quark = g_quark_from_static_string ("colord"); |
| 42 |
for (i = 0; i < CD_CLIENT_ERROR_LAST; i++) { |
| 43 |
g_dbus_error_register_error (quark, |
| 44 |
i, |
| 45 |
cd_client_error_to_string (i)); |
| 46 |
} |
| 47 |
} |
| 48 |
return quark; |
| 49 |
} |
| 50 |
|
| 51 |
gchar * |
| 52 |
cd_main_ensure_dbus_path (const gchar *object_path) |
| 53 |
{ |
| 54 |
gchar *object_path_tmp; |
| 55 |
object_path_tmp = g_strdup (object_path); |
| 56 |
g_strcanon (object_path_tmp, |
| 57 |
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 58 |
"abcdefghijklmnopqrstuvwxyz" |
| 59 |
"1234567890_", |
| 60 |
'_'); |
| 61 |
return object_path_tmp; |
| 62 |
} |
| 63 |
|
| 64 |
guint |
| 65 |
cd_main_get_sender_uid (GDBusConnection *connection, |
| 66 |
const gchar *sender, |
| 67 |
GError **error) |
| 68 |
{ |
| 69 |
guint uid = G_MAXUINT; |
| 70 |
g_autoptr(GVariant) value = NULL; |
| 71 |
|
| 72 |
/* call into DBus to get the user ID that issued the request */ |
| 73 |
value = g_dbus_connection_call_sync (connection, |
| 74 |
"org.freedesktop.DBus", |
| 75 |
"/org/freedesktop/DBus", |
| 76 |
"org.freedesktop.DBus", |
| 77 |
"GetConnectionUnixUser", |
| 78 |
g_variant_new ("(s)", |
| 79 |
sender), |
| 80 |
NULL, |
| 81 |
G_DBUS_CALL_FLAGS_NONE, |
| 82 |
200, |
| 83 |
NULL, |
| 84 |
error); |
| 85 |
if (value != NULL) |
| 86 |
g_variant_get (value, "(u)", &uid); |
| 87 |
return uid; |
| 88 |
} |
| 89 |
|
| 90 |
guint |
| 91 |
cd_main_get_sender_pid (GDBusConnection *connection, |
| 92 |
const gchar *sender, |
| 93 |
GError **error) |
| 94 |
{ |
| 95 |
guint pid = G_MAXUINT; |
| 96 |
g_autoptr(GVariant) value = NULL; |
| 97 |
|
| 98 |
/* call into DBus to get the user ID that issued the request */ |
| 99 |
value = g_dbus_connection_call_sync (connection, |
| 100 |
"org.freedesktop.DBus", |
| 101 |
"/org/freedesktop/DBus", |
| 102 |
"org.freedesktop.DBus", |
| 103 |
"GetConnectionUnixProcessID", |
| 104 |
g_variant_new ("(s)", |
| 105 |
sender), |
| 106 |
NULL, |
| 107 |
G_DBUS_CALL_FLAGS_NONE, |
| 108 |
200, |
| 109 |
NULL, |
| 110 |
error); |
| 111 |
if (value != NULL) |
| 112 |
g_variant_get (value, "(u)", &pid); |
| 113 |
return pid; |
| 114 |
} |
| 115 |
|
| 116 |
gboolean |
| 117 |
cd_main_sender_authenticated (GDBusConnection *connection, |
| 118 |
const gchar *sender, |
| 119 |
const gchar *action_id, |
| 120 |
GError **error) |
| 121 |
{ |
| 122 |
guint uid; |
| 123 |
g_autoptr(GError) error_local = NULL; |
| 124 |
g_autoptr(PolkitAuthority) authority = NULL; |
| 125 |
g_autoptr(PolkitAuthorizationResult) result = NULL; |
| 126 |
g_autoptr(PolkitSubject) subject = NULL; |
| 127 |
|
| 128 |
/* uid 0 is allowed to do all actions */ |
| 129 |
uid = cd_main_get_sender_uid (connection, sender, &error_local); |
| 130 |
if (uid == G_MAXUINT) { |
| 131 |
g_set_error (error, |
| 132 |
CD_CLIENT_ERROR, |
| 133 |
CD_CLIENT_ERROR_FAILED_TO_AUTHENTICATE, |
| 134 |
"could not get uid to authenticate %s: %s", |
| 135 |
action_id, |
| 136 |
error_local->message); |
| 137 |
return FALSE; |
| 138 |
} |
| 139 |
|
| 140 |
/* the root user can always do all actions */ |
| 141 |
if (uid == 0) { |
| 142 |
g_debug ("CdCommon: not checking %s for %s as uid 0", |
| 143 |
action_id, sender); |
| 144 |
return TRUE; |
| 145 |
} |
| 146 |
|
| 147 |
#ifdef HAVE_GETUID |
| 148 |
/* a client running as the daemon user may also do all actions */ |
| 149 |
if (uid == getuid ()) { |
| 150 |
g_debug ("CdCommon: not checking %s for %s as running as daemon user", |
| 151 |
action_id, sender); |
| 152 |
return TRUE; |
| 153 |
} |
| 154 |
#endif |
| 155 |
|
| 156 |
/* get authority */ |
| 157 |
authority = polkit_authority_get_sync (NULL, &error_local); |
| 158 |
if (authority == NULL) { |
| 159 |
g_set_error (error, |
| 160 |
CD_CLIENT_ERROR, |
| 161 |
CD_CLIENT_ERROR_FAILED_TO_AUTHENTICATE, |
| 162 |
"failed to get polkit authorit: %s", |
| 163 |
error_local->message); |
| 164 |
return FALSE; |
| 165 |
} |
| 166 |
|
| 167 |
/* do authorization async */ |
| 168 |
subject = polkit_system_bus_name_new (sender); |
| 169 |
result = polkit_authority_check_authorization_sync (authority, subject, |
| 170 |
action_id, |
| 171 |
NULL, |
| 172 |
POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION, |
| 173 |
NULL, |
| 174 |
&error_local); |
| 175 |
if (result == NULL) { |
| 176 |
g_set_error (error, |
| 177 |
CD_CLIENT_ERROR, |
| 178 |
CD_CLIENT_ERROR_FAILED_TO_AUTHENTICATE, |
| 179 |
"could not check %s for auth: %s", |
| 180 |
action_id, |
| 181 |
error_local->message); |
| 182 |
return FALSE; |
| 183 |
} |
| 184 |
|
| 185 |
/* did not auth */ |
| 186 |
if (!polkit_authorization_result_get_is_authorized (result)) { |
| 187 |
g_set_error (error, |
| 188 |
CD_CLIENT_ERROR, |
| 189 |
CD_CLIENT_ERROR_FAILED_TO_AUTHENTICATE, |
| 190 |
"failed to obtain %s auth", |
| 191 |
action_id); |
| 192 |
return FALSE; |
| 193 |
} |
| 194 |
return TRUE; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
gboolean |
| 199 |
cd_main_mkdir_with_parents (const gchar *filename, GError **error) |
| 200 |
{ |
| 201 |
/* ensure destination exists */ |
| 202 |
if (!g_file_test (filename, G_FILE_TEST_EXISTS)) { |
| 203 |
g_autoptr(GFile) file = NULL; |
| 204 |
file = g_file_new_for_path (filename); |
| 205 |
return g_file_make_directory_with_parents (file, NULL, error); |
| 206 |
} |
| 207 |
return TRUE; |
| 208 |
} |