Debian Codegraph @

src/polkit/polkitunixuser.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 */
21
22 #include <string.h>
23 #include <pwd.h>
24 #include <errno.h>
25 #include "polkitunixuser.h"
26 #include "polkitidentity.h"
27 #include "polkiterror.h"
28 #include "polkitprivate.h"
29
30 /**
31 * SECTION:polkitunixuser
32 * @title: PolkitUnixUser
33 * @short_description: Unix users
34 *
35 * An object representing a user identity on a UNIX system.
36 */
37
38 /**
39 * PolkitUnixUser:
40 *
41 * The #PolkitUnixUser struct should not be accessed directly.
42 */
43 struct _PolkitUnixUser
44 {
45 GObject parent_instance;
46
47 gint uid;
48 gchar *name;
49 };
50
51 struct _PolkitUnixUserClass
52 {
53 GObjectClass parent_class;
54 };
55
56 enum
57 {
58 PROP_0,
59 PROP_UID,
60 };
61
62 static void identity_iface_init (PolkitIdentityIface *identity_iface);
63
64 G_DEFINE_TYPE_WITH_CODE (PolkitUnixUser, polkit_unix_user, G_TYPE_OBJECT,
65 G_IMPLEMENT_INTERFACE (POLKIT_TYPE_IDENTITY, identity_iface_init)
66 );
67
68 static void
69 polkit_unix_user_init (PolkitUnixUser *unix_user)
70 {
71 unix_user->uid = -1; /* (uid_t) -1 is not a valid UID under Linux */
72 unix_user->name = NULL;
73 }
74
75 static void
76 polkit_unix_user_finalize (GObject *object)
77 {
78 PolkitUnixUser *unix_user = POLKIT_UNIX_USER (object);
79
80 g_free(unix_user->name);
81
82 G_OBJECT_CLASS (polkit_unix_user_parent_class)->finalize (object);
83 }
84
85 static void
86 polkit_unix_user_get_property (GObject *object,
87 guint prop_id,
88 GValue *value,
89 GParamSpec *pspec)
90 {
91 PolkitUnixUser *unix_user = POLKIT_UNIX_USER (object);
92
93 switch (prop_id)
94 {
95 case PROP_UID:
96 g_value_set_int (value, unix_user->uid);
97 break;
98
99 default:
100 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
101 break;
102 }
103 }
104
105 static void
106 polkit_unix_user_set_property (GObject *object,
107 guint prop_id,
108 const GValue *value,
109 GParamSpec *pspec)
110 {
111 PolkitUnixUser *unix_user = POLKIT_UNIX_USER (object);
112 gint val;
113
114 switch (prop_id)
115 {
116 case PROP_UID:
117 val = g_value_get_int (value);
118 g_return_if_fail (val != -1);
119 unix_user->uid = val;
120 break;
121
122 default:
123 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
124 break;
125 }
126 }
127
128 static void
129 polkit_unix_user_class_init (PolkitUnixUserClass *klass)
130 {
131 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
132
133 gobject_class->finalize = polkit_unix_user_finalize;
134 gobject_class->get_property = polkit_unix_user_get_property;
135 gobject_class->set_property = polkit_unix_user_set_property;
136
137 /**
138 * PolkitUnixUser:uid:
139 *
140 * The UNIX user id.
141 */
142 g_object_class_install_property (gobject_class,
143 PROP_UID,
144 g_param_spec_int ("uid",
145 "User ID",
146 "The UNIX user ID",
147 G_MININT,
148 G_MAXINT,
149 -1,
150 G_PARAM_CONSTRUCT |
151 G_PARAM_READWRITE |
152 G_PARAM_STATIC_NAME |
153 G_PARAM_STATIC_BLURB |
154 G_PARAM_STATIC_NICK));
155
156 }
157
158 /**
159 * polkit_unix_user_get_uid:
160 * @user: A #PolkitUnixUser.
161 *
162 * Gets the UNIX user id for @user.
163 *
164 * Returns: A UNIX user id.
165 */
166 gint
167 polkit_unix_user_get_uid (PolkitUnixUser *user)
168 {
169 g_return_val_if_fail (POLKIT_IS_UNIX_USER (user), -1);
170 return user->uid;
171 }
172
173 /**
174 * polkit_unix_user_set_uid:
175 * @user: A #PolkitUnixUser.
176 * @uid: A UNIX user id.
177 *
178 * Sets @uid for @user.
179 */
180 void
181 polkit_unix_user_set_uid (PolkitUnixUser *user,
182 gint uid)
183 {
184 g_return_if_fail (POLKIT_IS_UNIX_USER (user));
185 g_return_if_fail (uid != -1);
186 user->uid = uid;
187 }
188
189 /**
190 * polkit_unix_user_new:
191 * @uid: A UNIX user id.
192 *
193 * Creates a new #PolkitUnixUser object for @uid.
194 *
195 * Returns: (transfer full): A #PolkitUnixUser object. Free with g_object_unref().
196 */
197 PolkitIdentity *
198 polkit_unix_user_new (gint uid)
199 {
200 g_return_val_if_fail (uid != -1, NULL);
201
202 return POLKIT_IDENTITY (g_object_new (POLKIT_TYPE_UNIX_USER,
203 "uid", uid,
204 NULL));
205 }
206
207 /**
208 * polkit_unix_user_new_for_name:
209 * @name: A UNIX user name.
210 * @error: Return location for error.
211 *
212 * Creates a new #PolkitUnixUser object for a user with the user name
213 * @name.
214 *
215 * Returns: (allow-none) (transfer full): A #PolkitUnixUser object or %NULL if @error is set.
216 */
217 PolkitIdentity *
218 polkit_unix_user_new_for_name (const gchar *name,
219 GError **error)
220 {
221 struct passwd *passwd;
222 PolkitIdentity *identity;
223
224 g_return_val_if_fail (name != NULL, NULL);
225 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
226
227 identity = NULL;
228
229 passwd = getpwnam (name);
230 if (passwd == NULL)
231 {
232 g_set_error (error,
233 POLKIT_ERROR,
234 POLKIT_ERROR_FAILED,
235 "No UNIX user with name %s: %s",
236 name,
237 g_strerror (errno));
238 goto out;
239 }
240
241 identity = polkit_unix_user_new (passwd->pw_uid);
242
243 out:
244 return identity;
245 }
246
247 /**
248 * polkit_unix_user_get_name:
249 * @user: A #PolkitUnixUser.
250 *
251 * Get the user's name.
252 *
253 * Returns: (allow-none) (transfer none): User name string or %NULL if user uid not found.
254 */
255 const gchar *
256 polkit_unix_user_get_name (PolkitUnixUser *user)
257 {
258 if (user->name == NULL)
259 {
260 struct passwd *passwd;
261 passwd = getpwuid (user->uid);
262
263 if (passwd != NULL)
264 user->name = g_strdup(passwd->pw_name);
265 }
266
267 return user->name;
268 }
269
270 static gboolean
271 polkit_unix_user_equal (PolkitIdentity *a,
272 PolkitIdentity *b)
273 {
274 PolkitUnixUser *user_a;
275 PolkitUnixUser *user_b;
276
277 user_a = POLKIT_UNIX_USER (a);
278 user_b = POLKIT_UNIX_USER (b);
279
280 return user_a->uid == user_b->uid;
281 }
282
283 static guint
284 polkit_unix_user_hash (PolkitIdentity *identity)
285 {
286 PolkitUnixUser *user;
287
288 user = POLKIT_UNIX_USER (identity);
289
290 return g_direct_hash (GINT_TO_POINTER (((gint) (user->uid)) * 2));
291 }
292
293 static gchar *
294 polkit_unix_user_to_string (PolkitIdentity *identity)
295 {
296 PolkitUnixUser *user = POLKIT_UNIX_USER (identity);
297 const gchar *user_name = polkit_unix_user_get_name(user);
298
299 if (user_name != NULL)
300 return g_strdup_printf ("unix-user:%s", user_name);
301 else
302 return g_strdup_printf ("unix-user:%d", user->uid);
303 }
304
305 static void
306 identity_iface_init (PolkitIdentityIface *identity_iface)
307 {
308 identity_iface->hash = polkit_unix_user_hash;
309 identity_iface->equal = polkit_unix_user_equal;
310 identity_iface->to_string = polkit_unix_user_to_string;
311 }