| 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 |
#include <grp.h> |
| 24 |
#include <errno.h> |
| 25 |
#include "polkitunixgroup.h" |
| 26 |
#include "polkitidentity.h" |
| 27 |
#include "polkiterror.h" |
| 28 |
#include "polkitprivate.h" |
| 29 |
|
| 30 |
/** |
| 31 |
* SECTION:polkitunixgroup |
| 32 |
* @title: PolkitUnixGroup |
| 33 |
* @short_description: Unix groups |
| 34 |
* |
| 35 |
* An object representing a group identity on a UNIX system. |
| 36 |
*/ |
| 37 |
|
| 38 |
/** |
| 39 |
* PolkitUnixGroup: |
| 40 |
* |
| 41 |
* The #PolkitUnixGroup struct should not be accessed directly. |
| 42 |
*/ |
| 43 |
struct _PolkitUnixGroup |
| 44 |
{ |
| 45 |
GObject parent_instance; |
| 46 |
|
| 47 |
gint gid; |
| 48 |
}; |
| 49 |
|
| 50 |
struct _PolkitUnixGroupClass |
| 51 |
{ |
| 52 |
GObjectClass parent_class; |
| 53 |
}; |
| 54 |
|
| 55 |
enum |
| 56 |
{ |
| 57 |
PROP_0, |
| 58 |
PROP_GID, |
| 59 |
}; |
| 60 |
|
| 61 |
static void identity_iface_init (PolkitIdentityIface *identity_iface); |
| 62 |
|
| 63 |
G_DEFINE_TYPE_WITH_CODE (PolkitUnixGroup, polkit_unix_group, G_TYPE_OBJECT, |
| 64 |
G_IMPLEMENT_INTERFACE (POLKIT_TYPE_IDENTITY, identity_iface_init) |
| 65 |
); |
| 66 |
|
| 67 |
static void |
| 68 |
polkit_unix_group_init (PolkitUnixGroup *unix_group) |
| 69 |
{ |
| 70 |
unix_group->gid = -1; /* (gid_t) -1 is not a valid GID under Linux */ |
| 71 |
} |
| 72 |
|
| 73 |
static void |
| 74 |
polkit_unix_group_get_property (GObject *object, |
| 75 |
guint prop_id, |
| 76 |
GValue *value, |
| 77 |
GParamSpec *pspec) |
| 78 |
{ |
| 79 |
PolkitUnixGroup *unix_group = POLKIT_UNIX_GROUP (object); |
| 80 |
|
| 81 |
switch (prop_id) |
| 82 |
{ |
| 83 |
case PROP_GID: |
| 84 |
g_value_set_int (value, unix_group->gid); |
| 85 |
break; |
| 86 |
|
| 87 |
default: |
| 88 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 89 |
break; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
static void |
| 94 |
polkit_unix_group_set_property (GObject *object, |
| 95 |
guint prop_id, |
| 96 |
const GValue *value, |
| 97 |
GParamSpec *pspec) |
| 98 |
{ |
| 99 |
PolkitUnixGroup *unix_group = POLKIT_UNIX_GROUP (object); |
| 100 |
gint val; |
| 101 |
|
| 102 |
switch (prop_id) |
| 103 |
{ |
| 104 |
case PROP_GID: |
| 105 |
val = g_value_get_int (value); |
| 106 |
g_return_if_fail (val != -1); |
| 107 |
unix_group->gid = val; |
| 108 |
break; |
| 109 |
|
| 110 |
default: |
| 111 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 112 |
break; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
static void |
| 117 |
polkit_unix_group_class_init (PolkitUnixGroupClass *klass) |
| 118 |
{ |
| 119 |
GObjectClass *gobject_class = G_OBJECT_CLASS (klass); |
| 120 |
|
| 121 |
gobject_class->get_property = polkit_unix_group_get_property; |
| 122 |
gobject_class->set_property = polkit_unix_group_set_property; |
| 123 |
|
| 124 |
/** |
| 125 |
* PolkitUnixGroup:gid: |
| 126 |
* |
| 127 |
* The UNIX group id. |
| 128 |
*/ |
| 129 |
g_object_class_install_property (gobject_class, |
| 130 |
PROP_GID, |
| 131 |
g_param_spec_int ("gid", |
| 132 |
"Group ID", |
| 133 |
"The UNIX group ID", |
| 134 |
G_MININT, |
| 135 |
G_MAXINT, |
| 136 |
-1, |
| 137 |
G_PARAM_CONSTRUCT | |
| 138 |
G_PARAM_READWRITE | |
| 139 |
G_PARAM_STATIC_NAME | |
| 140 |
G_PARAM_STATIC_BLURB | |
| 141 |
G_PARAM_STATIC_NICK)); |
| 142 |
|
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* polkit_unix_group_get_gid: |
| 147 |
* @group: A #PolkitUnixGroup. |
| 148 |
* |
| 149 |
* Gets the UNIX group id for @group. |
| 150 |
* |
| 151 |
* Returns: A UNIX group id. |
| 152 |
*/ |
| 153 |
gint |
| 154 |
polkit_unix_group_get_gid (PolkitUnixGroup *group) |
| 155 |
{ |
| 156 |
g_return_val_if_fail (POLKIT_IS_UNIX_GROUP (group), -1); |
| 157 |
return group->gid; |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* polkit_unix_group_set_gid: |
| 162 |
* @group: A #PolkitUnixGroup. |
| 163 |
* @gid: A UNIX group id. |
| 164 |
* |
| 165 |
* Sets @gid for @group. |
| 166 |
*/ |
| 167 |
void |
| 168 |
polkit_unix_group_set_gid (PolkitUnixGroup *group, |
| 169 |
gint gid) |
| 170 |
{ |
| 171 |
g_return_if_fail (POLKIT_IS_UNIX_GROUP (group)); |
| 172 |
g_return_if_fail (gid != -1); |
| 173 |
group->gid = gid; |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* polkit_unix_group_new: |
| 178 |
* @gid: A UNIX group id. |
| 179 |
* |
| 180 |
* Creates a new #PolkitUnixGroup object for @gid. |
| 181 |
* |
| 182 |
* Returns: (transfer full): A #PolkitUnixGroup object. Free with g_object_unref(). |
| 183 |
*/ |
| 184 |
PolkitIdentity * |
| 185 |
polkit_unix_group_new (gint gid) |
| 186 |
{ |
| 187 |
g_return_val_if_fail (gid != -1, NULL); |
| 188 |
|
| 189 |
return POLKIT_IDENTITY (g_object_new (POLKIT_TYPE_UNIX_GROUP, |
| 190 |
"gid", gid, |
| 191 |
NULL)); |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* polkit_unix_group_new_for_name: |
| 196 |
* @name: A UNIX group name. |
| 197 |
* @error: Return location for error. |
| 198 |
* |
| 199 |
* Creates a new #PolkitUnixGroup object for a group with the group name |
| 200 |
* @name. |
| 201 |
* |
| 202 |
* Returns: (transfer full) (allow-none): A #PolkitUnixGroup object or %NULL if @error |
| 203 |
* is set. |
| 204 |
*/ |
| 205 |
PolkitIdentity * |
| 206 |
polkit_unix_group_new_for_name (const gchar *name, |
| 207 |
GError **error) |
| 208 |
{ |
| 209 |
struct group *group; |
| 210 |
PolkitIdentity *identity; |
| 211 |
|
| 212 |
g_return_val_if_fail (name != NULL, NULL); |
| 213 |
g_return_val_if_fail (error == NULL || *error == NULL, NULL); |
| 214 |
|
| 215 |
identity = NULL; |
| 216 |
|
| 217 |
group = getgrnam (name); |
| 218 |
if (group == NULL) |
| 219 |
{ |
| 220 |
g_set_error (error, |
| 221 |
POLKIT_ERROR, |
| 222 |
POLKIT_ERROR_FAILED, |
| 223 |
"No UNIX group with name %s: %s", |
| 224 |
name, |
| 225 |
g_strerror (errno)); |
| 226 |
goto out; |
| 227 |
} |
| 228 |
|
| 229 |
identity = polkit_unix_group_new (group->gr_gid); |
| 230 |
|
| 231 |
out: |
| 232 |
return identity; |
| 233 |
} |
| 234 |
|
| 235 |
static guint |
| 236 |
polkit_unix_group_hash (PolkitIdentity *identity) |
| 237 |
{ |
| 238 |
PolkitUnixGroup *group; |
| 239 |
|
| 240 |
group = POLKIT_UNIX_GROUP (identity); |
| 241 |
|
| 242 |
return g_direct_hash (GINT_TO_POINTER (((gint) (group->gid)) * 2 + 1)); |
| 243 |
} |
| 244 |
|
| 245 |
static gboolean |
| 246 |
polkit_unix_group_equal (PolkitIdentity *a, |
| 247 |
PolkitIdentity *b) |
| 248 |
{ |
| 249 |
PolkitUnixGroup *group_a; |
| 250 |
PolkitUnixGroup *group_b; |
| 251 |
|
| 252 |
group_a = POLKIT_UNIX_GROUP (a); |
| 253 |
group_b = POLKIT_UNIX_GROUP (b); |
| 254 |
|
| 255 |
return group_a->gid == group_b->gid; |
| 256 |
} |
| 257 |
|
| 258 |
static gchar * |
| 259 |
polkit_unix_group_to_string (PolkitIdentity *identity) |
| 260 |
{ |
| 261 |
PolkitUnixGroup *group = POLKIT_UNIX_GROUP (identity); |
| 262 |
struct group *gr; |
| 263 |
|
| 264 |
gr = getgrgid (group->gid); |
| 265 |
|
| 266 |
if (gr == NULL) |
| 267 |
return g_strdup_printf ("unix-group:%d", group->gid); |
| 268 |
else |
| 269 |
return g_strdup_printf ("unix-group:%s", gr->gr_name); |
| 270 |
} |
| 271 |
|
| 272 |
static void |
| 273 |
identity_iface_init (PolkitIdentityIface *identity_iface) |
| 274 |
{ |
| 275 |
identity_iface->hash = polkit_unix_group_hash; |
| 276 |
identity_iface->equal = polkit_unix_group_equal; |
| 277 |
identity_iface->to_string = polkit_unix_group_to_string; |
| 278 |
} |