1 /* GDBus - GLib D-Bus Library
3 * Copyright (C) 2008-2009 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: David Zeuthen <davidz@redhat.com>
26 #include <glib/gi18n.h>
27 #include <gobject/gvaluecollector.h>
29 #include "gcredentials.h"
33 #include <sys/types.h>
38 * SECTION:gcredentials
39 * @short_description: An object containing credentials
42 * The #GCredentials type is used for storing information that can be
43 * used for identifying, authenticating and authorizing processes.
45 * Most UNIX and UNIX-like operating systems support a secure exchange
46 * of credentials over a Unix Domain Socket, see
47 * #GUnixCredentialsMessage, g_unix_connection_send_credentials() and
48 * g_unix_connection_receive_credentials() for details.
51 struct _GCredentialsPrivate
59 G_DEFINE_TYPE (GCredentials, g_credentials, G_TYPE_OBJECT);
62 g_credentials_finalize (GObject *object)
64 GCredentials *credentials = G_CREDENTIALS (object);
66 g_free (credentials->priv->windows_user);
68 if (G_OBJECT_CLASS (g_credentials_parent_class)->finalize != NULL)
69 G_OBJECT_CLASS (g_credentials_parent_class)->finalize (object);
74 g_credentials_class_init (GCredentialsClass *klass)
76 GObjectClass *gobject_class;
78 g_type_class_add_private (klass, sizeof (GCredentialsPrivate));
80 gobject_class = G_OBJECT_CLASS (klass);
81 gobject_class->finalize = g_credentials_finalize;
85 g_credentials_init (GCredentials *credentials)
87 credentials->priv = G_TYPE_INSTANCE_GET_PRIVATE (credentials, G_TYPE_CREDENTIALS, GCredentialsPrivate);
89 credentials->priv->unix_user = -1;
90 credentials->priv->unix_group = -1;
91 credentials->priv->unix_process = -1;
92 credentials->priv->windows_user = NULL;
95 /* ---------------------------------------------------------------------------------------------------- */
100 * Creates a new empty credentials object.
102 * Returns: A #GCredentials. Free with g_object_unref().
107 g_credentials_new (void)
109 return g_object_new (G_TYPE_CREDENTIALS, NULL);
112 /* ---------------------------------------------------------------------------------------------------- */
115 static GCredentials *
116 g_credentials_new_for_unix_process (void)
118 GCredentials *credentials;
119 credentials = g_credentials_new ();
120 credentials->priv->unix_user = getuid ();
121 credentials->priv->unix_group = getgid ();
122 credentials->priv->unix_process = getpid ();
127 /* ---------------------------------------------------------------------------------------------------- */
130 * g_credentials_new_for_process:
132 * Gets the credentials for the current process. Note that the exact
133 * set of credentials in the returned object vary according to
136 * Returns: A #GCredentials. Free with g_object_unref().
141 g_credentials_new_for_process (void)
144 return g_credentials_new_for_unix_process ();
146 return g_credentials_new_for_win32_process ();
148 #warning Please implement g_credentials_new_for_process() for your OS. For now g_credentials_new_for_process() will return empty credentials.
149 return g_credentials_new ();
153 /* ---------------------------------------------------------------------------------------------------- */
156 * g_credentials_new_for_string:
157 * @str: A string returned from g_credentials_to_string().
158 * @error: Return location for error.
160 * Constructs a #GCredentials instance from @str.
162 * Returns: A #GCredentials or %NULL if @error is set. The return
163 * object must be freed with g_object_unref().
168 g_credentials_new_for_string (const gchar *str,
171 GCredentials *credentials;
175 g_return_val_if_fail (str != NULL, NULL);
176 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
179 credentials = g_credentials_new ();
181 if (!g_str_has_prefix (str, "GCredentials:"))
184 tokens = g_strsplit (str + sizeof "GCredentials:" - 1, ",", 0);
185 for (n = 0; tokens[n] != NULL; n++)
187 const gchar *token = tokens[n];
188 if (g_str_has_prefix (token, "unix-user:"))
189 g_credentials_set_unix_user (credentials, atoi (token + sizeof ("unix-user:") - 1));
190 else if (g_str_has_prefix (token, "unix-group:"))
191 g_credentials_set_unix_group (credentials, atoi (token + sizeof ("unix-group:") - 1));
192 else if (g_str_has_prefix (token, "unix-process:"))
193 g_credentials_set_unix_process (credentials, atoi (token + sizeof ("unix-process:") - 1));
194 else if (g_str_has_prefix (token, "windows-user:"))
195 g_credentials_set_windows_user (credentials, token + sizeof ("windows-user:"));
206 _("The string `%s' is not a valid credentials string"),
208 g_object_unref (credentials);
214 * g_credentials_to_string:
215 * @credentials: A #GCredentials object.
217 * Serializes @credentials to a string that can be used with
218 * g_credentials_new_for_string().
220 * Returns: A string that should be freed with g_free().
225 g_credentials_to_string (GCredentials *credentials)
229 g_return_val_if_fail (G_IS_CREDENTIALS (credentials), NULL);
231 ret = g_string_new ("GCredentials:");
232 if (credentials->priv->unix_user != -1)
233 g_string_append_printf (ret, "unix-user=%" G_GINT64_FORMAT ",", credentials->priv->unix_user);
234 if (credentials->priv->unix_group != -1)
235 g_string_append_printf (ret, "unix-group=%" G_GINT64_FORMAT ",", credentials->priv->unix_group);
236 if (credentials->priv->unix_process != -1)
237 g_string_append_printf (ret, "unix-process=%" G_GINT64_FORMAT ",", credentials->priv->unix_process);
238 if (credentials->priv->windows_user != NULL)
239 g_string_append_printf (ret, "windows-user=%s,", credentials->priv->windows_user);
240 if (ret->str[ret->len - 1] == ',')
241 ret->str[ret->len - 1] = '\0';
243 return g_string_free (ret, FALSE);
246 /* ---------------------------------------------------------------------------------------------------- */
249 * g_credentials_has_unix_user:
250 * @credentials: A #GCredentials.
252 * Checks if @credentials has a UNIX user credential.
254 * Returns: %TRUE if @credentials has this type of credential, %FALSE otherwise.
259 g_credentials_has_unix_user (GCredentials *credentials)
261 g_return_val_if_fail (G_IS_CREDENTIALS (credentials), FALSE);
262 return credentials->priv->unix_user != -1;
266 * g_credentials_get_unix_user:
267 * @credentials: A #GCredentials.
269 * Gets the UNIX user identifier from @credentials.
271 * Returns: The identifier or -1 if unset.
276 g_credentials_get_unix_user (GCredentials *credentials)
278 g_return_val_if_fail (G_IS_CREDENTIALS (credentials), -1);
279 return credentials->priv->unix_user;
283 * g_credentials_set_unix_user:
284 * @credentials: A #GCredentials.
285 * @user_id: A UNIX user identifier (typically type #uid_t) or -1 to unset it.
287 * Sets the UNIX user identifier.
292 g_credentials_set_unix_user (GCredentials *credentials,
295 g_return_if_fail (G_IS_CREDENTIALS (credentials));
296 credentials->priv->unix_user = user_id;
299 /* ---------------------------------------------------------------------------------------------------- */
302 * g_credentials_has_unix_group:
303 * @credentials: A #GCredentials.
305 * Checks if @credentials has a UNIX group credential.
307 * Returns: %TRUE if @credentials has this type of credential, %FALSE otherwise.
312 g_credentials_has_unix_group (GCredentials *credentials)
314 g_return_val_if_fail (G_IS_CREDENTIALS (credentials), FALSE);
315 return credentials->priv->unix_group != -1;
319 * g_credentials_get_unix_group:
320 * @credentials: A #GCredentials.
322 * Gets the UNIX group identifier from @credentials.
324 * Returns: The identifier or -1 if unset.
329 g_credentials_get_unix_group (GCredentials *credentials)
331 g_return_val_if_fail (G_IS_CREDENTIALS (credentials), -1);
332 return credentials->priv->unix_group;
336 * g_credentials_set_unix_group:
337 * @credentials: A #GCredentials.
338 * @group_id: A UNIX group identifier (typically type #gid_t) or -1 to unset.
340 * Sets the UNIX group identifier.
345 g_credentials_set_unix_group (GCredentials *credentials,
348 g_return_if_fail (G_IS_CREDENTIALS (credentials));
349 credentials->priv->unix_group = group_id;
352 /* ---------------------------------------------------------------------------------------------------- */
355 * g_credentials_has_unix_process:
356 * @credentials: A #GCredentials.
358 * Checks if @credentials has a UNIX process credential.
360 * Returns: %TRUE if @credentials has this type of credential, %FALSE otherwise.
365 g_credentials_has_unix_process (GCredentials *credentials)
367 g_return_val_if_fail (G_IS_CREDENTIALS (credentials), FALSE);
368 return credentials->priv->unix_process != -1;
372 * g_credentials_get_unix_process:
373 * @credentials: A #GCredentials.
375 * Gets the UNIX process identifier from @credentials.
377 * Returns: The identifier or -1 if unset.
382 g_credentials_get_unix_process (GCredentials *credentials)
384 g_return_val_if_fail (G_IS_CREDENTIALS (credentials), -1);
385 return credentials->priv->unix_process;
389 * g_credentials_set_unix_process:
390 * @credentials: A #GCredentials.
391 * @process_id: A UNIX process identifier (typically type #pid_t/#GPid) or -1 to unset.
393 * Sets the UNIX process identifier.
398 g_credentials_set_unix_process (GCredentials *credentials,
401 g_return_if_fail (G_IS_CREDENTIALS (credentials));
402 credentials->priv->unix_process = process_id;
405 /* ---------------------------------------------------------------------------------------------------- */
408 * g_credentials_has_windows_user:
409 * @credentials: A #GCredentials.
411 * Checks if @credentials has a Windows user SID (Security Identifier).
413 * Returns: %TRUE if @credentials has this type of credential, %FALSE otherwise.
418 g_credentials_has_windows_user (GCredentials *credentials)
420 g_return_val_if_fail (G_IS_CREDENTIALS (credentials), FALSE);
421 return credentials->priv->windows_user != NULL;
425 * g_credentials_get_windows_user:
426 * @credentials: A #GCredentials.
428 * Gets the Windows User SID from @credentials.
430 * Returns: A string or %NULL if unset. Do not free, the string is owned by @credentials.
435 g_credentials_get_windows_user (GCredentials *credentials)
437 g_return_val_if_fail (G_IS_CREDENTIALS (credentials), NULL);
438 return credentials->priv->windows_user;
442 * g_credentials_set_windows_user:
443 * @credentials: A #GCredentials.
444 * @user_sid: The Windows User SID or %NULL to unset.
446 * Sets the Windows User SID.
451 g_credentials_set_windows_user (GCredentials *credentials,
452 const gchar *user_sid)
454 g_return_if_fail (G_IS_CREDENTIALS (credentials));
455 g_free (credentials->priv->windows_user);
456 credentials->priv->windows_user = g_strdup (user_sid);
459 /* ---------------------------------------------------------------------------------------------------- */