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