| 1 |
/* |
| 2 |
* Copyright (C) 2008 Red Hat, Inc. |
| 3 |
* |
| 4 |
* This library is free software; you can redistribute it and/or |
| 5 |
* modify it under the terms of the GNU Lesser General Public |
| 6 |
* License as published by the Free Software Foundation; either |
| 7 |
* version 2 of the License, or (at your option) any later version. |
| 8 |
* |
| 9 |
* This library is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 |
* Lesser General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU Lesser General |
| 15 |
* Public License along with this library; if not, write to the |
| 16 |
* Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
| 17 |
* Boston, MA 02111-1307, USA. |
| 18 |
* |
| 19 |
* Author: David Zeuthen <[email protected]> |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <string.h> |
| 23 |
|
| 24 |
#include "polkitidentity.h" |
| 25 |
#include "polkitunixuser.h" |
| 26 |
#include "polkitunixgroup.h" |
| 27 |
#include "polkitunixnetgroup.h" |
| 28 |
#include "polkiterror.h" |
| 29 |
#include "polkitprivate.h" |
| 30 |
|
| 31 |
/** |
| 32 |
* SECTION:polkitidentity |
| 33 |
* @title: PolkitIdentity |
| 34 |
* @short_description: Type for representing identities |
| 35 |
* |
| 36 |
* #PolkitIdentity is an abstract type for representing one or more |
| 37 |
* identities. |
| 38 |
*/ |
| 39 |
|
| 40 |
static void |
| 41 |
base_init (gpointer g_iface) |
| 42 |
{ |
| 43 |
} |
| 44 |
|
| 45 |
GType |
| 46 |
polkit_identity_get_type (void) |
| 47 |
{ |
| 48 |
static volatile gsize g_define_type_id__volatile = 0; |
| 49 |
|
| 50 |
if (g_once_init_enter ((gsize*) &g_define_type_id__volatile)) |
| 51 |
{ |
| 52 |
static const GTypeInfo info = |
| 53 |
{ |
| 54 |
sizeof (PolkitIdentityIface), |
| 55 |
base_init, /* base_init */ |
| 56 |
NULL, /* base_finalize */ |
| 57 |
NULL, /* class_init */ |
| 58 |
NULL, /* class_finalize */ |
| 59 |
NULL, /* class_data */ |
| 60 |
0, /* instance_size */ |
| 61 |
0, /* n_preallocs */ |
| 62 |
NULL, /* instance_init */ |
| 63 |
NULL /* value_table */ |
| 64 |
}; |
| 65 |
|
| 66 |
GType iface_type = |
| 67 |
g_type_register_static (G_TYPE_INTERFACE, "PolkitIdentity", &info, 0); |
| 68 |
|
| 69 |
g_type_interface_add_prerequisite (iface_type, G_TYPE_OBJECT); |
| 70 |
g_once_init_leave (&g_define_type_id__volatile, iface_type); |
| 71 |
} |
| 72 |
|
| 73 |
return g_define_type_id__volatile; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* polkit_identity_hash: |
| 78 |
* @identity: A #PolkitIdentity. |
| 79 |
* |
| 80 |
* Gets a hash code for @identity that can be used with e.g. g_hash_table_new(). |
| 81 |
* |
| 82 |
* Returns: A hash code. |
| 83 |
*/ |
| 84 |
guint |
| 85 |
polkit_identity_hash (PolkitIdentity *identity) |
| 86 |
{ |
| 87 |
g_return_val_if_fail (POLKIT_IS_IDENTITY (identity), 0); |
| 88 |
return POLKIT_IDENTITY_GET_IFACE (identity)->hash (identity); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* polkit_identity_equal: |
| 93 |
* @a: A #PolkitIdentity. |
| 94 |
* @b: A #PolkitIdentity. |
| 95 |
* |
| 96 |
* Checks if @a and @b are equal, ie. represent the same identity. |
| 97 |
* |
| 98 |
* This function can be used in e.g. g_hash_table_new(). |
| 99 |
* |
| 100 |
* Returns: %TRUE if @a and @b are equal, %FALSE otherwise. |
| 101 |
*/ |
| 102 |
gboolean |
| 103 |
polkit_identity_equal (PolkitIdentity *a, |
| 104 |
PolkitIdentity *b) |
| 105 |
{ |
| 106 |
g_return_val_if_fail (POLKIT_IS_IDENTITY (a), FALSE); |
| 107 |
g_return_val_if_fail (POLKIT_IS_IDENTITY (b), FALSE); |
| 108 |
|
| 109 |
if (!g_type_is_a (G_TYPE_FROM_INSTANCE (a), G_TYPE_FROM_INSTANCE (b))) |
| 110 |
return FALSE; |
| 111 |
|
| 112 |
return POLKIT_IDENTITY_GET_IFACE (a)->equal (a, b); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* polkit_identity_to_string: |
| 117 |
* @identity: A #PolkitIdentity. |
| 118 |
* |
| 119 |
* Serializes @identity to a string that can be used in |
| 120 |
* polkit_identity_from_string(). |
| 121 |
* |
| 122 |
* Returns: A string representing @identity. Free with g_free(). |
| 123 |
*/ |
| 124 |
gchar * |
| 125 |
polkit_identity_to_string (PolkitIdentity *identity) |
| 126 |
{ |
| 127 |
g_return_val_if_fail (POLKIT_IS_IDENTITY (identity), NULL); |
| 128 |
return POLKIT_IDENTITY_GET_IFACE (identity)->to_string (identity); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* polkit_identity_from_string: |
| 133 |
* @str: A string obtained from polkit_identity_to_string(). |
| 134 |
* @error: Return location for error. |
| 135 |
* |
| 136 |
* Creates an object from @str that implements the #PolkitIdentity |
| 137 |
* interface. |
| 138 |
* |
| 139 |
* Returns: (allow-none) (transfer full): A #PolkitIdentity or %NULL |
| 140 |
* if @error is set. Free with g_object_unref(). |
| 141 |
*/ |
| 142 |
PolkitIdentity * |
| 143 |
polkit_identity_from_string (const gchar *str, |
| 144 |
GError **error) |
| 145 |
{ |
| 146 |
PolkitIdentity *identity; |
| 147 |
guint64 val; |
| 148 |
gchar *endptr; |
| 149 |
|
| 150 |
g_return_val_if_fail (str != NULL, NULL); |
| 151 |
g_return_val_if_fail (error == NULL || *error == NULL, NULL); |
| 152 |
|
| 153 |
/* TODO: we could do something with VFuncs like in g_icon_from_string() */ |
| 154 |
|
| 155 |
identity = NULL; |
| 156 |
|
| 157 |
if (g_str_has_prefix (str, "unix-user:")) |
| 158 |
{ |
| 159 |
val = g_ascii_strtoull (str + sizeof "unix-user:" - 1, |
| 160 |
&endptr, |
| 161 |
10); |
| 162 |
if (*endptr == '\0') |
| 163 |
identity = polkit_unix_user_new ((gint) val); |
| 164 |
else |
| 165 |
identity = polkit_unix_user_new_for_name (str + sizeof "unix-user:" - 1, |
| 166 |
error); |
| 167 |
} |
| 168 |
else if (g_str_has_prefix (str, "unix-group:")) |
| 169 |
{ |
| 170 |
val = g_ascii_strtoull (str + sizeof "unix-group:" - 1, |
| 171 |
&endptr, |
| 172 |
10); |
| 173 |
if (*endptr == '\0') |
| 174 |
identity = polkit_unix_group_new ((gint) val); |
| 175 |
else |
| 176 |
identity = polkit_unix_group_new_for_name (str + sizeof "unix-group:" - 1, |
| 177 |
error); |
| 178 |
} |
| 179 |
else if (g_str_has_prefix (str, "unix-netgroup:")) |
| 180 |
{ |
| 181 |
#ifndef HAVE_SETNETGRENT |
| 182 |
g_set_error (error, |
| 183 |
POLKIT_ERROR, |
| 184 |
POLKIT_ERROR_FAILED, |
| 185 |
"Netgroups are not available on this machine ('%s')", |
| 186 |
str); |
| 187 |
#else |
| 188 |
identity = polkit_unix_netgroup_new (str + sizeof "unix-netgroup:" - 1); |
| 189 |
#endif |
| 190 |
} |
| 191 |
|
| 192 |
if (identity == NULL && (error != NULL && *error == NULL)) |
| 193 |
{ |
| 194 |
g_set_error (error, |
| 195 |
POLKIT_ERROR, |
| 196 |
POLKIT_ERROR_FAILED, |
| 197 |
"Malformed identity string '%s'", |
| 198 |
str); |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
return identity; |
| 203 |
} |
| 204 |
|
| 205 |
/* Note that this returns a floating value. */ |
| 206 |
GVariant * |
| 207 |
polkit_identity_to_gvariant (PolkitIdentity *identity) |
| 208 |
{ |
| 209 |
GVariantBuilder builder; |
| 210 |
GVariant *dict; |
| 211 |
const gchar *kind; |
| 212 |
|
| 213 |
kind = ""; |
| 214 |
|
| 215 |
g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}")); |
| 216 |
if (POLKIT_IS_UNIX_USER (identity)) |
| 217 |
{ |
| 218 |
kind = "unix-user"; |
| 219 |
g_variant_builder_add (&builder, "{sv}", "uid", |
| 220 |
g_variant_new_uint32 (polkit_unix_user_get_uid (POLKIT_UNIX_USER (identity)))); |
| 221 |
} |
| 222 |
else if (POLKIT_IS_UNIX_GROUP (identity)) |
| 223 |
{ |
| 224 |
kind = "unix-group"; |
| 225 |
g_variant_builder_add (&builder, "{sv}", "gid", |
| 226 |
g_variant_new_uint32 (polkit_unix_group_get_gid (POLKIT_UNIX_GROUP (identity)))); |
| 227 |
} |
| 228 |
else if (POLKIT_IS_UNIX_NETGROUP (identity)) |
| 229 |
{ |
| 230 |
kind = "unix-netgroup"; |
| 231 |
g_variant_builder_add (&builder, "{sv}", "name", |
| 232 |
g_variant_new_string (polkit_unix_netgroup_get_name (POLKIT_UNIX_NETGROUP (identity)))); |
| 233 |
} |
| 234 |
else |
| 235 |
{ |
| 236 |
g_warning ("Unknown class %s implementing PolkitIdentity", g_type_name (G_TYPE_FROM_INSTANCE (identity))); |
| 237 |
} |
| 238 |
|
| 239 |
dict = g_variant_builder_end (&builder); |
| 240 |
return g_variant_new ("(s@a{sv})", kind, dict); |
| 241 |
} |
| 242 |
|
| 243 |
static GVariant * |
| 244 |
lookup_asv (GVariant *dict, |
| 245 |
const gchar *given_key, |
| 246 |
const GVariantType *given_type, |
| 247 |
GError **error) |
| 248 |
{ |
| 249 |
GVariantIter iter; |
| 250 |
const gchar *key; |
| 251 |
GVariant *value; |
| 252 |
GVariant *ret; |
| 253 |
|
| 254 |
ret = NULL; |
| 255 |
|
| 256 |
g_variant_iter_init (&iter, dict); |
| 257 |
while (g_variant_iter_next (&iter, "{&sv}", &key, &value)) |
| 258 |
{ |
| 259 |
if (g_strcmp0 (key, given_key) == 0) |
| 260 |
{ |
| 261 |
if (!g_variant_is_of_type (value, given_type)) |
| 262 |
{ |
| 263 |
gchar *type_string; |
| 264 |
type_string = g_variant_type_dup_string (given_type); |
| 265 |
g_set_error (error, |
| 266 |
POLKIT_ERROR, |
| 267 |
POLKIT_ERROR_FAILED, |
| 268 |
"Value for key `%s' found but is of type %s and type %s was expected", |
| 269 |
given_key, |
| 270 |
g_variant_get_type_string (value), |
| 271 |
type_string); |
| 272 |
g_free (type_string); |
| 273 |
g_variant_unref (value); |
| 274 |
goto out; |
| 275 |
} |
| 276 |
ret = value; |
| 277 |
goto out; |
| 278 |
} |
| 279 |
g_variant_unref (value); |
| 280 |
} |
| 281 |
|
| 282 |
out: |
| 283 |
if (ret == NULL) |
| 284 |
{ |
| 285 |
gchar *type_string; |
| 286 |
type_string = g_variant_type_dup_string (given_type); |
| 287 |
g_set_error (error, |
| 288 |
POLKIT_ERROR, |
| 289 |
POLKIT_ERROR_FAILED, |
| 290 |
"Didn't find value for key `%s' of type %s", |
| 291 |
given_key, |
| 292 |
type_string); |
| 293 |
g_free (type_string); |
| 294 |
} |
| 295 |
|
| 296 |
return ret; |
| 297 |
} |
| 298 |
|
| 299 |
PolkitIdentity * |
| 300 |
polkit_identity_new_for_gvariant (GVariant *variant, |
| 301 |
GError **error) |
| 302 |
{ |
| 303 |
PolkitIdentity *ret; |
| 304 |
const gchar *kind; |
| 305 |
GVariant *details_gvariant; |
| 306 |
|
| 307 |
ret = NULL; |
| 308 |
|
| 309 |
g_variant_get (variant, |
| 310 |
"(&s@a{sv})", |
| 311 |
&kind, |
| 312 |
&details_gvariant); |
| 313 |
|
| 314 |
if (g_strcmp0 (kind, "unix-user") == 0) |
| 315 |
{ |
| 316 |
GVariant *v; |
| 317 |
guint32 uid; |
| 318 |
|
| 319 |
v = lookup_asv (details_gvariant, "uid", G_VARIANT_TYPE_UINT32, error); |
| 320 |
if (v == NULL) |
| 321 |
{ |
| 322 |
g_prefix_error (error, "Error parsing unix-user identity: "); |
| 323 |
goto out; |
| 324 |
} |
| 325 |
uid = g_variant_get_uint32 (v); |
| 326 |
g_variant_unref (v); |
| 327 |
|
| 328 |
ret = polkit_unix_user_new (uid); |
| 329 |
} |
| 330 |
else if (g_strcmp0 (kind, "unix-group") == 0) |
| 331 |
{ |
| 332 |
GVariant *v; |
| 333 |
guint32 gid; |
| 334 |
|
| 335 |
v = lookup_asv (details_gvariant, "gid", G_VARIANT_TYPE_UINT32, error); |
| 336 |
if (v == NULL) |
| 337 |
{ |
| 338 |
g_prefix_error (error, "Error parsing unix-user identity: "); |
| 339 |
goto out; |
| 340 |
} |
| 341 |
gid = g_variant_get_uint32 (v); |
| 342 |
g_variant_unref (v); |
| 343 |
|
| 344 |
ret = polkit_unix_group_new (gid); |
| 345 |
} |
| 346 |
else if (g_strcmp0 (kind, "unix-netgroup") == 0) |
| 347 |
{ |
| 348 |
#ifdef HAVE_SETNETGRENT |
| 349 |
GVariant *v; |
| 350 |
const char *name; |
| 351 |
|
| 352 |
v = lookup_asv (details_gvariant, "name", G_VARIANT_TYPE_STRING, error); |
| 353 |
if (v == NULL) |
| 354 |
{ |
| 355 |
g_prefix_error (error, "Error parsing net identity: "); |
| 356 |
goto out; |
| 357 |
} |
| 358 |
name = g_variant_get_string (v, NULL); |
| 359 |
ret = polkit_unix_netgroup_new (name); |
| 360 |
g_variant_unref (v); |
| 361 |
#else |
| 362 |
g_set_error (error, |
| 363 |
POLKIT_ERROR, |
| 364 |
POLKIT_ERROR_FAILED, |
| 365 |
"Netgroups are not available on this machine"); |
| 366 |
goto out; |
| 367 |
#endif |
| 368 |
} |
| 369 |
else |
| 370 |
{ |
| 371 |
g_set_error (error, |
| 372 |
POLKIT_ERROR, |
| 373 |
POLKIT_ERROR_FAILED, |
| 374 |
"Unknown identity of kind `%s'", |
| 375 |
kind); |
| 376 |
} |
| 377 |
|
| 378 |
out: |
| 379 |
g_variant_unref (details_gvariant); |
| 380 |
return ret; |
| 381 |
} |