Merge branch 'gdbus-merge'
[platform/upstream/glib.git] / gio / gcredentials.c
1 /* GDBus - GLib D-Bus Library
2  *
3  * Copyright (C) 2008-2010 Red Hat, Inc.
4  *
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.
9  *
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.
14  *
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.
19  *
20  * Author: David Zeuthen <davidz@redhat.com>
21  */
22
23 #include "config.h"
24
25 #include <stdlib.h>
26 #include <gobject/gvaluecollector.h>
27
28 #include "gcredentials.h"
29 #include "gioerror.h"
30
31 #ifdef __linux__
32 #define __USE_GNU
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <unistd.h>
36 #include <string.h>
37 #endif
38
39 #include "glibintl.h"
40 #include "gioalias.h"
41
42 /**
43  * SECTION:gcredentials
44  * @short_description: An object containing credentials
45  * @include: gio/gio.h
46  *
47  * The #GCredentials type is a reference-counted wrapper for the
48  * native credentials type. This information is typically used for
49  * identifying, authenticating and authorizing other processes.
50  *
51  * Some operating systems supports looking up the credentials of the
52  * remote peer of a communication endpoint - see e.g.
53  * g_socket_get_credentials().
54  *
55  * Some operating systems supports securely sending and receiving
56  * credentials over a Unix Domain Socket, see
57  * #GUnixCredentialsMessage, g_unix_connection_send_credentials() and
58  * g_unix_connection_receive_credentials() for details.
59  *
60  * On Linux, the native credential type is a <literal>struct ucred</literal> - see
61  * the <literal>unix(7)</literal> man page for details.
62  */
63
64 struct _GCredentialsPrivate
65 {
66 #ifdef __linux__
67   struct ucred native;
68 #else
69 #warning Please add GCredentials support for your OS
70   guint foo;
71 #endif
72 };
73
74 G_DEFINE_TYPE (GCredentials, g_credentials, G_TYPE_OBJECT);
75
76 static void
77 g_credentials_finalize (GObject *object)
78 {
79   G_GNUC_UNUSED GCredentials *credentials = G_CREDENTIALS (object);
80
81   if (G_OBJECT_CLASS (g_credentials_parent_class)->finalize != NULL)
82     G_OBJECT_CLASS (g_credentials_parent_class)->finalize (object);
83 }
84
85
86 static void
87 g_credentials_class_init (GCredentialsClass *klass)
88 {
89   GObjectClass *gobject_class;
90
91   g_type_class_add_private (klass, sizeof (GCredentialsPrivate));
92
93   gobject_class = G_OBJECT_CLASS (klass);
94   gobject_class->finalize = g_credentials_finalize;
95 }
96
97 static void
98 g_credentials_init (GCredentials *credentials)
99 {
100   credentials->priv = G_TYPE_INSTANCE_GET_PRIVATE (credentials, G_TYPE_CREDENTIALS, GCredentialsPrivate);
101 #ifdef __linux__
102   credentials->priv->native.pid = getpid ();
103   credentials->priv->native.uid = getuid ();
104   credentials->priv->native.gid = getgid ();
105 #endif
106 }
107
108 /* ---------------------------------------------------------------------------------------------------- */
109
110 /**
111  * g_credentials_new:
112  *
113  * Creates a new #GCredentials object with credentials matching the
114  * the current process.
115  *
116  * Returns: A #GCredentials. Free with g_object_unref().
117  *
118  * Since: 2.26
119  */
120 GCredentials *
121 g_credentials_new (void)
122 {
123   return g_object_new (G_TYPE_CREDENTIALS, NULL);
124 }
125
126 /* ---------------------------------------------------------------------------------------------------- */
127
128 /**
129  * g_credentials_to_string:
130  * @credentials: A #GCredentials object.
131  *
132  * Creates a human-readable textual representation of @credentials
133  * that can be used in logging and debug messages. The format of the
134  * returned string may change in future GLib release.
135  *
136  * Returns: A string that should be freed with g_free().
137  *
138  * Since: 2.26
139  */
140 gchar *
141 g_credentials_to_string (GCredentials *credentials)
142 {
143   GString *ret;
144
145   g_return_val_if_fail (G_IS_CREDENTIALS (credentials), NULL);
146
147   ret = g_string_new ("GCredentials:");
148 #ifdef __linux__
149   g_string_append (ret, "linux:");
150   if (credentials->priv->native.pid != -1)
151     g_string_append_printf (ret, "pid=%" G_GINT64_FORMAT ",", (gint64) credentials->priv->native.pid);
152   if (credentials->priv->native.uid != -1)
153     g_string_append_printf (ret, "uid=%" G_GINT64_FORMAT ",", (gint64) credentials->priv->native.uid);
154   if (credentials->priv->native.gid != -1)
155     g_string_append_printf (ret, "gid=%" G_GINT64_FORMAT ",", (gint64) credentials->priv->native.gid);
156   if (ret->str[ret->len - 1] == ',')
157     ret->str[ret->len - 1] = '\0';
158 #else
159   g_string_append (ret, "unknown");
160 #endif
161
162   return g_string_free (ret, FALSE);
163 }
164
165 /* ---------------------------------------------------------------------------------------------------- */
166
167 /**
168  * g_credentials_is_same_user:
169  * @credentials: A #GCredentials.
170  * @other_credentials: A #GCredentials.
171  * @error: Return location for error or %NULL.
172  *
173  * Checks if @credentials and @other_credentials is the same user.
174  *
175  * This operation can fail if #GCredentials is not supported on the
176  * the OS.
177  *
178  * Returns: %TRUE if @credentials and @other_credentials has the same
179  * user, %FALSE otherwise or if @error is set.
180  *
181  * Since: 2.26
182  */
183 gboolean
184 g_credentials_is_same_user (GCredentials  *credentials,
185                             GCredentials  *other_credentials,
186                             GError       **error)
187 {
188   gboolean ret;
189
190   g_return_val_if_fail (G_IS_CREDENTIALS (credentials), FALSE);
191   g_return_val_if_fail (G_IS_CREDENTIALS (other_credentials), FALSE);
192   g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
193
194   ret = FALSE;
195 #ifdef __linux__
196   if (credentials->priv->native.uid == other_credentials->priv->native.uid)
197     ret = TRUE;
198 #else
199   g_set_error_literal (error,
200                        G_IO_ERROR,
201                        G_IO_ERROR_NOT_SUPPORTED,
202                        _("GCredentials is not implemented on this OS"));
203 #endif
204
205   return ret;
206 }
207
208 /**
209  * g_credentials_get_native:
210  * @credentials: A #GCredentials.
211  *
212  * Gets a pointer to the native credentials structure.
213  *
214  * Returns: The pointer or %NULL if there is no #GCredentials support
215  * for the OS. Do not free the returned data, it is owned by
216  * @credentials.
217  *
218  * Since: 2.26
219  */
220 gpointer
221 g_credentials_get_native (GCredentials *credentials)
222 {
223   gpointer ret;
224   g_return_val_if_fail (G_IS_CREDENTIALS (credentials), NULL);
225
226 #ifdef __linux__
227   ret = &credentials->priv->native;
228 #else
229   ret = NULL;
230 #endif
231
232   return ret;
233 }
234
235 /**
236  * g_credentials_set_native:
237  * @credentials: A #GCredentials.
238  * @native: A pointer to native credentials.
239  *
240  * Copies the native credentials from @native into @credentials.
241  *
242  * It is a programming error (which will cause an warning to be
243  * logged) to use this method if there is no #GCredentials support for
244  * the OS.
245  *
246  * Since: 2.26
247  */
248 void
249 g_credentials_set_native (GCredentials    *credentials,
250                           gpointer         native)
251 {
252 #ifdef __linux__
253   memcpy (&credentials->priv->native, native, sizeof (struct ucred));
254 #else
255   g_warning ("g_credentials_set_native: Trying to set credentials but GLib has no support "
256              "for the native credentials type. Please add support.");
257 #endif
258 }
259
260 /* ---------------------------------------------------------------------------------------------------- */
261
262 #ifdef G_OS_UNIX
263 /**
264  * g_credentials_get_unix_user:
265  * @credentials: A #GCredentials
266  * @error: Return location for error or %NULL.
267  *
268  * Tries to get the UNIX user identifier from @credentials. This
269  * method is only available on UNIX platforms.
270  *
271  * This operation can fail if #GCredentials is not supported on the
272  * OS or if the native credentials type does not contain information
273  * about the UNIX user.
274  *
275  * Returns: The UNIX user identifier or -1 if @error is set.
276  *
277  * Since: 2.26
278  */
279 uid_t
280 g_credentials_get_unix_user (GCredentials    *credentials,
281                              GError         **error)
282 {
283   uid_t ret;
284
285   g_return_val_if_fail (G_IS_CREDENTIALS (credentials), -1);
286   g_return_val_if_fail (error == NULL || *error == NULL, -1);
287
288 #ifdef __linux__
289   ret = credentials->priv->native.uid;
290 #else
291   ret = -1;
292   g_set_error_literal (error,
293                        G_IO_ERROR,
294                        G_IO_ERROR_NOT_SUPPORTED,
295                        _("There no GCredentials support for your your platform"));
296 #endif
297
298   return ret;
299 }
300
301 /**
302  * g_credentials_set_unix_user:
303  * @credentials: A #GCredentials.
304  * @uid: The UNIX user identifier to set.
305  * @error: Return location for error or %NULL.
306  *
307  * Tries to set the UNIX user identifier on @credentials. This method
308  * is only available on UNIX platforms.
309  *
310  * This operation can fail if #GCredentials is not supported on the
311  * OS or if the native credentials type does not contain information
312  * about the UNIX user.
313  *
314  * Returns: %TRUE if @uid was set, %FALSE if error is set.
315  *
316  * Since: 2.26
317  */
318 gboolean
319 g_credentials_set_unix_user (GCredentials    *credentials,
320                              uid_t            uid,
321                              GError         **error)
322 {
323   gboolean ret;
324
325   g_return_val_if_fail (G_IS_CREDENTIALS (credentials), FALSE);
326   g_return_val_if_fail (uid != -1, FALSE);
327   g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
328
329   ret = FALSE;
330 #ifdef __linux__
331   credentials->priv->native.uid = uid;
332   ret = TRUE;
333 #else
334   g_set_error_literal (error,
335                        G_IO_ERROR,
336                        G_IO_ERROR_NOT_SUPPORTED,
337                        _("GCredentials is not implemented on this OS"));
338 #endif
339
340   return ret;
341 }
342 #endif /* G_OS_UNIX */
343
344 #define __G_CREDENTIALS_C__
345 #include "gioaliasdef.c"