Add a UNIX fd source
[platform/upstream/glib.git] / gio / gunixcredentialsmessage.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright (C) 2010 Red Hat, Inc.
4  * Copyright (C) 2009 Codethink Limited
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published
8  * by the Free Software Foundation; either version 2 of the licence or (at
9  * your option) any later version.
10  *
11  * See the included COPYING file for more information.
12  *
13  * Authors: David Zeuthen <davidz@redhat.com>
14  */
15
16 /**
17  * SECTION:gunixcredentialsmessage
18  * @title: GUnixCredentialsMessage
19  * @short_description: A GSocketControlMessage containing credentials
20  * @include: gio/gunixcredentialsmessage.h
21  * @see_also: #GUnixConnection, #GSocketControlMessage
22  *
23  * This #GSocketControlMessage contains a #GCredentials instance.  It
24  * may be sent using g_socket_send_message() and received using
25  * g_socket_receive_message() over UNIX sockets (ie: sockets in the
26  * %G_SOCKET_FAMILY_UNIX family).
27  *
28  * For an easier way to send and receive credentials over
29  * stream-oriented UNIX sockets, see
30  * g_unix_connection_send_credentials() and
31  * g_unix_connection_receive_credentials(). To receive credentials of
32  * a foreign process connected to a socket, use
33  * g_socket_get_credentials().
34  */
35
36 #include "config.h"
37
38 /* ---------------------------------------------------------------------------------------------------- */
39 #ifdef __linux__
40 #define G_UNIX_CREDENTIALS_MESSAGE_SUPPORTED 1
41 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
42 #define G_UNIX_CREDENTIALS_MESSAGE_SUPPORTED 1
43 #else
44 /* TODO: please add support for your UNIX flavor */
45 #define G_UNIX_CREDENTIALS_MESSAGE_SUPPORTED 0
46 #endif
47
48 /* ---------------------------------------------------------------------------------------------------- */
49
50 #include <fcntl.h>
51 #include <errno.h>
52 #include <string.h>
53 #include <unistd.h>
54
55 #include "gunixcredentialsmessage.h"
56 #include "gcredentials.h"
57 #include "gnetworking.h"
58
59 #include "glibintl.h"
60
61 struct _GUnixCredentialsMessagePrivate
62 {
63   GCredentials *credentials;
64 };
65
66 enum
67 {
68   PROP_0,
69   PROP_CREDENTIALS
70 };
71
72 G_DEFINE_TYPE (GUnixCredentialsMessage, g_unix_credentials_message, G_TYPE_SOCKET_CONTROL_MESSAGE);
73
74 static gsize
75 g_unix_credentials_message_get_size (GSocketControlMessage *message)
76 {
77 #ifdef __linux__
78   return sizeof (struct ucred);
79 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
80   return sizeof (struct cmsgcred);
81 #else
82   return 0;
83 #endif
84 }
85
86 static int
87 g_unix_credentials_message_get_level (GSocketControlMessage *message)
88 {
89 #ifdef __linux__
90   return SOL_SOCKET;
91 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
92   return SOL_SOCKET;
93 #else
94   return 0;
95 #endif
96 }
97
98 static int
99 g_unix_credentials_message_get_msg_type (GSocketControlMessage *message)
100 {
101 #ifdef __linux__
102   return SCM_CREDENTIALS;
103 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
104   return SCM_CREDS;
105 #else
106   return 0;
107 #endif
108 }
109
110 static GSocketControlMessage *
111 g_unix_credentials_message_deserialize (gint     level,
112                                         gint     type,
113                                         gsize    size,
114                                         gpointer data)
115 {
116   GSocketControlMessage *message;
117
118   message = NULL;
119
120 #ifdef __linux__
121   {
122     GCredentials *credentials;
123     struct ucred *ucred;
124
125     if (level != SOL_SOCKET || type != SCM_CREDENTIALS)
126       goto out;
127
128     if (size != sizeof (struct ucred))
129       {
130         g_warning ("Expected a struct ucred (%" G_GSIZE_FORMAT " bytes) but "
131                    "got %" G_GSIZE_FORMAT " bytes of data",
132                    sizeof (struct ucred),
133                    size);
134         goto out;
135       }
136
137     ucred = data;
138
139     if (ucred->uid == (uid_t)-1 &&
140         ucred->gid == (gid_t)-1)
141       {
142         /* This happens if the remote side didn't pass the credentials */
143         goto out;
144       }
145
146     credentials = g_credentials_new ();
147     g_credentials_set_native (credentials, G_CREDENTIALS_TYPE_LINUX_UCRED, ucred);
148     message = g_unix_credentials_message_new_with_credentials (credentials);
149     g_object_unref (credentials);
150  out:
151     ;
152   }
153 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
154   {
155     GCredentials *credentials;
156     struct cmsgcred *cred;
157
158     if (level != SOL_SOCKET || type != SCM_CREDS)
159       {
160         goto out;
161       }
162     if (size < sizeof *cred)
163       {
164         g_warning ("Expected a struct cmsgcred (%" G_GSIZE_FORMAT " bytes) but "
165                    "got %" G_GSIZE_FORMAT " bytes of data",
166                    CMSG_LEN (sizeof *cred),
167                    size);
168         goto out;
169       }
170
171     cred = data;
172
173     credentials = g_credentials_new ();
174     g_credentials_set_native (credentials, G_CREDENTIALS_TYPE_FREEBSD_CMSGCRED, cred);
175     message = g_unix_credentials_message_new_with_credentials (credentials);
176     g_object_unref (credentials);
177  out:
178     ;
179   }
180 #endif
181
182   return message;
183 }
184
185 static void
186 g_unix_credentials_message_serialize (GSocketControlMessage *_message,
187                                       gpointer               data)
188 {
189   GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (_message);
190 #ifdef __linux__
191   memcpy (data,
192           g_credentials_get_native (message->priv->credentials,
193                                     G_CREDENTIALS_TYPE_LINUX_UCRED),
194           sizeof (struct ucred));
195 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
196   memcpy (data,
197           g_credentials_get_native (message->priv->credentials,
198                                     G_CREDENTIALS_TYPE_FREEBSD_CMSGCRED),
199           sizeof (struct cmsgcred));
200
201 #endif
202 }
203
204 static void
205 g_unix_credentials_message_finalize (GObject *object)
206 {
207   GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (object);
208
209   if (message->priv->credentials != NULL)
210     g_object_unref (message->priv->credentials);
211
212   G_OBJECT_CLASS (g_unix_credentials_message_parent_class)->finalize (object);
213 }
214
215 static void
216 g_unix_credentials_message_init (GUnixCredentialsMessage *message)
217 {
218   message->priv = G_TYPE_INSTANCE_GET_PRIVATE (message,
219                                                G_TYPE_UNIX_CREDENTIALS_MESSAGE,
220                                                GUnixCredentialsMessagePrivate);
221 }
222
223 static void
224 g_unix_credentials_message_get_property (GObject    *object,
225                                          guint       prop_id,
226                                          GValue     *value,
227                                          GParamSpec *pspec)
228 {
229   GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (object);
230
231   switch (prop_id)
232     {
233     case PROP_CREDENTIALS:
234       g_value_set_object (value, message->priv->credentials);
235       break;
236
237     default:
238       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
239       break;
240     }
241 }
242
243 static void
244 g_unix_credentials_message_set_property (GObject      *object,
245                                          guint         prop_id,
246                                          const GValue *value,
247                                          GParamSpec   *pspec)
248 {
249   GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (object);
250
251   switch (prop_id)
252     {
253     case PROP_CREDENTIALS:
254       message->priv->credentials = g_value_dup_object (value);
255       break;
256
257     default:
258       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
259       break;
260     }
261 }
262
263 static void
264 g_unix_credentials_message_constructed (GObject *object)
265 {
266   GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (object);
267
268   if (message->priv->credentials == NULL)
269     message->priv->credentials = g_credentials_new ();
270
271   if (G_OBJECT_CLASS (g_unix_credentials_message_parent_class)->constructed != NULL)
272     G_OBJECT_CLASS (g_unix_credentials_message_parent_class)->constructed (object);
273 }
274
275 static void
276 g_unix_credentials_message_class_init (GUnixCredentialsMessageClass *class)
277 {
278   GSocketControlMessageClass *scm_class;
279   GObjectClass *gobject_class;
280
281   g_type_class_add_private (class, sizeof (GUnixCredentialsMessagePrivate));
282
283   gobject_class = G_OBJECT_CLASS (class);
284   gobject_class->get_property = g_unix_credentials_message_get_property;
285   gobject_class->set_property = g_unix_credentials_message_set_property;
286   gobject_class->finalize = g_unix_credentials_message_finalize;
287   gobject_class->constructed = g_unix_credentials_message_constructed;
288
289   scm_class = G_SOCKET_CONTROL_MESSAGE_CLASS (class);
290   scm_class->get_size = g_unix_credentials_message_get_size;
291   scm_class->get_level = g_unix_credentials_message_get_level;
292   scm_class->get_type = g_unix_credentials_message_get_msg_type;
293   scm_class->serialize = g_unix_credentials_message_serialize;
294   scm_class->deserialize = g_unix_credentials_message_deserialize;
295
296   /**
297    * GUnixCredentialsMessage:credentials:
298    *
299    * The credentials stored in the message.
300    *
301    * Since: 2.26
302    */
303   g_object_class_install_property (gobject_class,
304                                    PROP_CREDENTIALS,
305                                    g_param_spec_object ("credentials",
306                                                         P_("Credentials"),
307                                                         P_("The credentials stored in the message"),
308                                                         G_TYPE_CREDENTIALS,
309                                                         G_PARAM_READABLE |
310                                                         G_PARAM_WRITABLE |
311                                                         G_PARAM_CONSTRUCT_ONLY |
312                                                         G_PARAM_STATIC_NAME |
313                                                         G_PARAM_STATIC_BLURB |
314                                                         G_PARAM_STATIC_NICK));
315
316 }
317
318 /* ---------------------------------------------------------------------------------------------------- */
319
320 /**
321  * g_unix_credentials_message_is_supported:
322  *
323  * Checks if passing #GCredentials on a #GSocket is supported on this platform.
324  *
325  * Returns: %TRUE if supported, %FALSE otherwise
326  *
327  * Since: 2.26
328  */
329 gboolean
330 g_unix_credentials_message_is_supported (void)
331 {
332   return G_UNIX_CREDENTIALS_MESSAGE_SUPPORTED;
333 }
334
335 /* ---------------------------------------------------------------------------------------------------- */
336
337 /**
338  * g_unix_credentials_message_new:
339  *
340  * Creates a new #GUnixCredentialsMessage with credentials matching the current processes.
341  *
342  * Returns: a new #GUnixCredentialsMessage
343  *
344  * Since: 2.26
345  */
346 GSocketControlMessage *
347 g_unix_credentials_message_new (void)
348 {
349   g_return_val_if_fail (g_unix_credentials_message_is_supported (), NULL);
350   return g_object_new (G_TYPE_UNIX_CREDENTIALS_MESSAGE,
351                        NULL);
352 }
353
354 /**
355  * g_unix_credentials_message_new_with_credentials:
356  * @credentials: A #GCredentials object.
357  *
358  * Creates a new #GUnixCredentialsMessage holding @credentials.
359  *
360  * Returns: a new #GUnixCredentialsMessage
361  *
362  * Since: 2.26
363  */
364 GSocketControlMessage *
365 g_unix_credentials_message_new_with_credentials (GCredentials *credentials)
366 {
367   g_return_val_if_fail (G_IS_CREDENTIALS (credentials), NULL);
368   g_return_val_if_fail (g_unix_credentials_message_is_supported (), NULL);
369   return g_object_new (G_TYPE_UNIX_CREDENTIALS_MESSAGE,
370                        "credentials", credentials,
371                        NULL);
372 }
373
374 /**
375  * g_unix_credentials_message_get_credentials:
376  * @message: A #GUnixCredentialsMessage.
377  *
378  * Gets the credentials stored in @message.
379  *
380  * Returns: (transfer none): A #GCredentials instance. Do not free, it is owned by @message.
381  *
382  * Since: 2.26
383  */
384 GCredentials *
385 g_unix_credentials_message_get_credentials (GUnixCredentialsMessage *message)
386 {
387   g_return_val_if_fail (G_IS_UNIX_CREDENTIALS_MESSAGE (message), NULL);
388   return message->priv->credentials;
389 }