Debian Codegraph @

src/polkit/polkitunixnetgroup.c

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