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 <davidz@redhat.com>
20 * Author: Nikki VonHollen <vonhollen@google.com>
21 */
22
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
26
27 #include <string.h>
28 #include <errno.h>
29 #include "polkitunixnetgroup.h"
30 #include "polkitidentity.h"
31 #include "polkiterror.h"
32 #include "polkitprivate.h"
33
34 /**
35 * SECTION:polkitunixnetgroup
36 * @title: PolkitUnixNetgroup
37 * @short_description: Unix netgroups
38 *
39 * An object representing a netgroup identity on a UNIX system.
40 */
41
42 /**
43 * PolkitUnixNetgroup:
44 *
45 * The #PolkitUnixNetgroup struct should not be accessed directly.
46 */
47 struct _PolkitUnixNetgroup
48 {
49 GObject parent_instance;
50
51 gchar *name;
52 };
53
54 struct _PolkitUnixNetgroupClass
55 {
56 GObjectClass parent_class;
57 };
58
59 enum
60 {
61 PROP_0,
62 PROP_NAME,
63 };
64
65 static void identity_iface_init (PolkitIdentityIface *identity_iface);
66
67 G_DEFINE_TYPE_WITH_CODE (PolkitUnixNetgroup, polkit_unix_netgroup, G_TYPE_OBJECT,
68 G_IMPLEMENT_INTERFACE (POLKIT_TYPE_IDENTITY, identity_iface_init)
69 );
70
71 static void
polkit_unix_netgroup_init(PolkitUnixNetgroup * net_group)72 polkit_unix_netgroup_init (PolkitUnixNetgroup *net_group)
73 {
74 net_group->name = NULL;
75 }
76
77 static void
polkit_unix_netgroup_finalize(GObject * object)78 polkit_unix_netgroup_finalize (GObject *object)
79 {
80 PolkitUnixNetgroup *net_group = POLKIT_UNIX_NETGROUP (object);
81
82 g_free(net_group->name);
83
84 G_OBJECT_CLASS (polkit_unix_netgroup_parent_class)->finalize (object);
85 }
86
87 static void
polkit_unix_netgroup_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)88 polkit_unix_netgroup_get_property (GObject *object,
89 guint prop_id,
90 GValue *value,
91 GParamSpec *pspec)
92 {
93 PolkitUnixNetgroup *net_group = POLKIT_UNIX_NETGROUP (object);
94
95 switch (prop_id)
96 {
97 case PROP_NAME:
98 g_value_set_string (value, polkit_unix_netgroup_get_name (net_group));
99 break;
100
101 default:
102 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
103 break;
104 }
105 }
106
107 static void
polkit_unix_netgroup_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)108 polkit_unix_netgroup_set_property (GObject *object,
109 guint prop_id,
110 const GValue *value,
111 GParamSpec *pspec)
112 {
113 PolkitUnixNetgroup *net_group = POLKIT_UNIX_NETGROUP (object);
114
115 switch (prop_id)
116 {
117 case PROP_NAME:
118 polkit_unix_netgroup_set_name (net_group, g_value_get_string (value));
119 break;
120
121 default:
122 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
123 break;
124 }
125 }
126
127 static void
polkit_unix_netgroup_class_init(PolkitUnixNetgroupClass * klass)128 polkit_unix_netgroup_class_init (PolkitUnixNetgroupClass *klass)
129 {
130 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
131
132 gobject_class->finalize = polkit_unix_netgroup_finalize;
133 gobject_class->get_property = polkit_unix_netgroup_get_property;
134 gobject_class->set_property = polkit_unix_netgroup_set_property;
135
136 /**
137 * PolkitUnixNetgroup:name:
138 *
139 * The NIS netgroup name.
140 */
141 g_object_class_install_property (gobject_class,
142 PROP_NAME,
143 g_param_spec_string ("name",
144 "Group Name",
145 "The NIS netgroup name",
146 NULL,
147 G_PARAM_CONSTRUCT |
148 G_PARAM_READWRITE |
149 G_PARAM_STATIC_NAME |
150 G_PARAM_STATIC_BLURB |
151 G_PARAM_STATIC_NICK));
152
153 }
154
155 /**
156 * polkit_unix_netgroup_get_name:
157 * @group: A #PolkitUnixNetgroup.
158 *
159 * Gets the netgroup name for @group.
160 *
161 * Returns: A netgroup name string.
162 */
163 const gchar *
polkit_unix_netgroup_get_name(PolkitUnixNetgroup * group)164 polkit_unix_netgroup_get_name (PolkitUnixNetgroup *group)
165 {
166 g_return_val_if_fail (POLKIT_IS_UNIX_NETGROUP (group), NULL);
167 return group->name;
168 }
169
170 /**
171 * polkit_unix_netgroup_set_name:
172 * @group: A #PolkitUnixNetgroup.
173 * @name: A netgroup name.
174 *
175 * Sets @name for @group.
176 */
177 void
polkit_unix_netgroup_set_name(PolkitUnixNetgroup * group,const gchar * name)178 polkit_unix_netgroup_set_name (PolkitUnixNetgroup *group,
179 const gchar * name)
180 {
181 g_return_if_fail (POLKIT_IS_UNIX_NETGROUP (group));
182 g_free(group->name);
183 group->name = g_strdup(name);
184 }
185
186 /**
187 * polkit_unix_netgroup_new:
188 * @name: A netgroup name.
189 *
190 * Creates a new #PolkitUnixNetgroup object for @name.
191 *
192 * Returns: (transfer full): A #PolkitUnixNetgroup object. Free with g_object_unref().
193 */
194 PolkitIdentity *
polkit_unix_netgroup_new(const gchar * name)195 polkit_unix_netgroup_new (const gchar *name)
196 {
197 g_return_val_if_fail (name != NULL, NULL);
198 return POLKIT_IDENTITY (g_object_new (POLKIT_TYPE_UNIX_NETGROUP,
199 "name", name,
200 NULL));
201 }
202
203 static guint
polkit_unix_netgroup_hash(PolkitIdentity * identity)204 polkit_unix_netgroup_hash (PolkitIdentity *identity)
205 {
206 PolkitUnixNetgroup *group;
207
208 group = POLKIT_UNIX_NETGROUP (identity);
209
210 return g_str_hash(group->name);
211 }
212
213 static gboolean
polkit_unix_netgroup_equal(PolkitIdentity * a,PolkitIdentity * b)214 polkit_unix_netgroup_equal (PolkitIdentity *a,
215 PolkitIdentity *b)
216 {
217 PolkitUnixNetgroup *group_a;
218 PolkitUnixNetgroup *group_b;
219
220 group_a = POLKIT_UNIX_NETGROUP (a);
221 group_b = POLKIT_UNIX_NETGROUP (b);
222
223 if (g_strcmp0(group_a->name, group_b->name) == 0)
224 return TRUE;
225 else
226 return FALSE;
227 }
228
229 static gchar *
polkit_unix_netgroup_to_string(PolkitIdentity * identity)230 polkit_unix_netgroup_to_string (PolkitIdentity *identity)
231 {
232 PolkitUnixNetgroup *group = POLKIT_UNIX_NETGROUP (identity);
233 return g_strconcat("unix-netgroup:", group->name, NULL);
234 }
235
236 static void
identity_iface_init(PolkitIdentityIface * identity_iface)237 identity_iface_init (PolkitIdentityIface *identity_iface)
238 {
239 identity_iface->hash = polkit_unix_netgroup_hash;
240 identity_iface->equal = polkit_unix_netgroup_equal;
241 identity_iface->to_string = polkit_unix_netgroup_to_string;
242 }
243