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 <gobject/gvaluecollector.h>
28 #include "gcredentials.h"
32 #include <sys/types.h>
40 * SECTION:gcredentials
41 * @short_description: An object containing credentials
44 * The #GCredentials type is used for storing information that can be
45 * used for identifying, authenticating and authorizing processes.
47 * Most UNIX and UNIX-like operating systems support a secure exchange
48 * of credentials over a Unix Domain Socket, see
49 * #GUnixCredentialsMessage, g_unix_connection_send_credentials() and
50 * g_unix_connection_receive_credentials() for details.
53 struct _GCredentialsPrivate
61 G_DEFINE_TYPE (GCredentials, g_credentials, G_TYPE_OBJECT);
64 g_credentials_finalize (GObject *object)
66 GCredentials *credentials = G_CREDENTIALS (object);
68 g_free (credentials->priv->windows_user);
70 if (G_OBJECT_CLASS (g_credentials_parent_class)->finalize != NULL)
71 G_OBJECT_CLASS (g_credentials_parent_class)->finalize (object);
76 g_credentials_class_init (GCredentialsClass *klass)
78 GObjectClass *gobject_class;
80 g_type_class_add_private (klass, sizeof (GCredentialsPrivate));
82 gobject_class = G_OBJECT_CLASS (klass);
83 gobject_class->finalize = g_credentials_finalize;
87 g_credentials_init (GCredentials *credentials)
89 credentials->priv = G_TYPE_INSTANCE_GET_PRIVATE (credentials, G_TYPE_CREDENTIALS, GCredentialsPrivate);
91 credentials->priv->unix_user = -1;
92 credentials->priv->unix_group = -1;
93 credentials->priv->unix_process = -1;
94 credentials->priv->windows_user = NULL;
97 /* ---------------------------------------------------------------------------------------------------- */
102 * Creates a new empty credentials object.
104 * Returns: A #GCredentials. Free with g_object_unref().
109 g_credentials_new (void)
111 return g_object_new (G_TYPE_CREDENTIALS, NULL);
114 /* ---------------------------------------------------------------------------------------------------- */
117 static GCredentials *
118 g_credentials_new_for_unix_process (void)
120 GCredentials *credentials;
121 credentials = g_credentials_new ();
122 credentials->priv->unix_user = getuid ();
123 credentials->priv->unix_group = getgid ();
124 credentials->priv->unix_process = getpid ();
129 /* ---------------------------------------------------------------------------------------------------- */
132 * g_credentials_new_for_process:
134 * Gets the credentials for the current process. Note that the exact
135 * set of credentials in the returned object vary according to
138 * Returns: A #GCredentials. Free with g_object_unref().
143 g_credentials_new_for_process (void)
146 return g_credentials_new_for_unix_process ();
148 return g_credentials_new_for_win32_process ();
150 #warning Please implement g_credentials_new_for_process() for your OS. For now g_credentials_new_for_process() will return empty credentials.
151 return g_credentials_new ();
155 /* ---------------------------------------------------------------------------------------------------- */
158 * g_credentials_new_for_string:
159 * @str: A string returned from g_credentials_to_string().
160 * @error: Return location for error.
162 * Constructs a #GCredentials instance from @str.
164 * Returns: A #GCredentials or %NULL if @error is set. The return
165 * object must be freed with g_object_unref().
170 g_credentials_new_for_string (const gchar *str,
173 GCredentials *credentials;
177 g_return_val_if_fail (str != NULL, NULL);
178 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
181 credentials = g_credentials_new ();
183 if (!g_str_has_prefix (str, "GCredentials:"))
186 tokens = g_strsplit (str + sizeof "GCredentials:" - 1, ",", 0);
187 for (n = 0; tokens[n] != NULL; n++)
189 const gchar *token = tokens[n];
190 if (g_str_has_prefix (token, "unix-user:"))
191 g_credentials_set_unix_user (credentials, atoi (token + sizeof ("unix-user:") - 1));
192 else if (g_str_has_prefix (token, "unix-group:"))
193 g_credentials_set_unix_group (credentials, atoi (token + sizeof ("unix-group:") - 1));
194 else if (g_str_has_prefix (token, "unix-process:"))
195 g_credentials_set_unix_process (credentials, atoi (token + sizeof ("unix-process:") - 1));
196 else if (g_str_has_prefix (token, "windows-user:"))
197 g_credentials_set_windows_user (credentials, token + sizeof ("windows-user:"));
208 _("The string `%s' is not a valid credentials string"),
210 g_object_unref (credentials);
216 * g_credentials_to_string:
217 * @credentials: A #GCredentials object.
219 * Serializes @credentials to a string that can be used with
220 * g_credentials_new_for_string().
222 * Returns: A string that should be freed with g_free().
227 g_credentials_to_string (GCredentials *credentials)
231 g_return_val_if_fail (G_IS_CREDENTIALS (credentials), NULL);
233 ret = g_string_new ("GCredentials:");
234 if (credentials->priv->unix_user != -1)
235 g_string_append_printf (ret, "unix-user=%" G_GINT64_FORMAT ",", credentials->priv->unix_user);
236 if (credentials->priv->unix_group != -1)
237 g_string_append_printf (ret, "unix-group=%" G_GINT64_FORMAT ",", credentials->priv->unix_group);
238 if (credentials->priv->unix_process != -1)
239 g_string_append_printf (ret, "unix-process=%" G_GINT64_FORMAT ",", credentials->priv->unix_process);
240 if (credentials->priv->windows_user != NULL)
241 g_string_append_printf (ret, "windows-user=%s,", credentials->priv->windows_user);
242 if (ret->str[ret->len - 1] == ',')
243 ret->str[ret->len - 1] = '\0';
245 return g_string_free (ret, FALSE);
248 /* ---------------------------------------------------------------------------------------------------- */
251 * g_credentials_has_unix_user:
252 * @credentials: A #GCredentials.
254 * Checks if @credentials has a UNIX user credential.
256 * Returns: %TRUE if @credentials has this type of credential, %FALSE otherwise.
261 g_credentials_has_unix_user (GCredentials *credentials)
263 g_return_val_if_fail (G_IS_CREDENTIALS (credentials), FALSE);
264 return credentials->priv->unix_user != -1;
268 * g_credentials_get_unix_user:
269 * @credentials: A #GCredentials.
271 * Gets the UNIX user identifier from @credentials.
273 * Returns: The identifier or -1 if unset.
278 g_credentials_get_unix_user (GCredentials *credentials)
280 g_return_val_if_fail (G_IS_CREDENTIALS (credentials), -1);
281 return credentials->priv->unix_user;
285 * g_credentials_set_unix_user:
286 * @credentials: A #GCredentials.
287 * @user_id: A UNIX user identifier (typically type #uid_t) or -1 to unset it.
289 * Sets the UNIX user identifier.
294 g_credentials_set_unix_user (GCredentials *credentials,
297 g_return_if_fail (G_IS_CREDENTIALS (credentials));
298 credentials->priv->unix_user = user_id;
301 /* ---------------------------------------------------------------------------------------------------- */
304 * g_credentials_has_unix_group:
305 * @credentials: A #GCredentials.
307 * Checks if @credentials has a UNIX group credential.
309 * Returns: %TRUE if @credentials has this type of credential, %FALSE otherwise.
314 g_credentials_has_unix_group (GCredentials *credentials)
316 g_return_val_if_fail (G_IS_CREDENTIALS (credentials), FALSE);
317 return credentials->priv->unix_group != -1;
321 * g_credentials_get_unix_group:
322 * @credentials: A #GCredentials.
324 * Gets the UNIX group identifier from @credentials.
326 * Returns: The identifier or -1 if unset.
331 g_credentials_get_unix_group (GCredentials *credentials)
333 g_return_val_if_fail (G_IS_CREDENTIALS (credentials), -1);
334 return credentials->priv->unix_group;
338 * g_credentials_set_unix_group:
339 * @credentials: A #GCredentials.
340 * @group_id: A UNIX group identifier (typically type #gid_t) or -1 to unset.
342 * Sets the UNIX group identifier.
347 g_credentials_set_unix_group (GCredentials *credentials,
350 g_return_if_fail (G_IS_CREDENTIALS (credentials));
351 credentials->priv->unix_group = group_id;
354 /* ---------------------------------------------------------------------------------------------------- */
357 * g_credentials_has_unix_process:
358 * @credentials: A #GCredentials.
360 * Checks if @credentials has a UNIX process credential.
362 * Returns: %TRUE if @credentials has this type of credential, %FALSE otherwise.
367 g_credentials_has_unix_process (GCredentials *credentials)
369 g_return_val_if_fail (G_IS_CREDENTIALS (credentials), FALSE);
370 return credentials->priv->unix_process != -1;
374 * g_credentials_get_unix_process:
375 * @credentials: A #GCredentials.
377 * Gets the UNIX process identifier from @credentials.
379 * Returns: The identifier or -1 if unset.
384 g_credentials_get_unix_process (GCredentials *credentials)
386 g_return_val_if_fail (G_IS_CREDENTIALS (credentials), -1);
387 return credentials->priv->unix_process;
391 * g_credentials_set_unix_process:
392 * @credentials: A #GCredentials.
393 * @process_id: A UNIX process identifier (typically type #pid_t/#GPid) or -1 to unset.
395 * Sets the UNIX process identifier.
400 g_credentials_set_unix_process (GCredentials *credentials,
403 g_return_if_fail (G_IS_CREDENTIALS (credentials));
404 credentials->priv->unix_process = process_id;
407 /* ---------------------------------------------------------------------------------------------------- */
410 * g_credentials_has_windows_user:
411 * @credentials: A #GCredentials.
413 * Checks if @credentials has a Windows user SID (Security Identifier).
415 * Returns: %TRUE if @credentials has this type of credential, %FALSE otherwise.
420 g_credentials_has_windows_user (GCredentials *credentials)
422 g_return_val_if_fail (G_IS_CREDENTIALS (credentials), FALSE);
423 return credentials->priv->windows_user != NULL;
427 * g_credentials_get_windows_user:
428 * @credentials: A #GCredentials.
430 * Gets the Windows User SID from @credentials.
432 * Returns: A string or %NULL if unset. Do not free, the string is owned by @credentials.
437 g_credentials_get_windows_user (GCredentials *credentials)
439 g_return_val_if_fail (G_IS_CREDENTIALS (credentials), NULL);
440 return credentials->priv->windows_user;
444 * g_credentials_set_windows_user:
445 * @credentials: A #GCredentials.
446 * @user_sid: The Windows User SID or %NULL to unset.
448 * Sets the Windows User SID.
453 g_credentials_set_windows_user (GCredentials *credentials,
454 const gchar *user_sid)
456 g_return_if_fail (G_IS_CREDENTIALS (credentials));
457 g_free (credentials->priv->windows_user);
458 credentials->priv->windows_user = g_strdup (user_sid);
461 /* ---------------------------------------------------------------------------------------------------- */
463 #define __G_CREDENTIALS_C__
464 #include "gioaliasdef.c"