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