ece892cfc142167e7f1716bef94fcadaf32234bd
[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
41 #define _GNU_SOURCE
42 #include <sys/types.h>
43 #include <sys/socket.h>
44 #include <sys/un.h>
45 #include <unistd.h>
46 #include <fcntl.h>
47 #define G_UNIX_CREDENTIALS_MESSAGE_SUPPORTED 1
48
49 #elif defined(__FreeBSD__)
50 #include <sys/types.h>
51 #include <sys/socket.h>
52 #include <sys/un.h>
53 #include <unistd.h>
54 #include <fcntl.h>
55 #define G_UNIX_CREDENTIALS_MESSAGE_SUPPORTED 1
56 #else
57 /* TODO: please add support for your UNIX flavor */
58 #define G_UNIX_CREDENTIALS_MESSAGE_SUPPORTED 0
59 #endif
60
61 /* ---------------------------------------------------------------------------------------------------- */
62
63 #include <string.h>
64 #include <errno.h>
65
66 #include "gunixcredentialsmessage.h"
67 #include "gcredentials.h"
68
69 #include "glibintl.h"
70
71 struct _GUnixCredentialsMessagePrivate
72 {
73   GCredentials *credentials;
74 };
75
76 enum
77 {
78   PROP_0,
79   PROP_CREDENTIALS
80 };
81
82 G_DEFINE_TYPE (GUnixCredentialsMessage, g_unix_credentials_message, G_TYPE_SOCKET_CONTROL_MESSAGE);
83
84 static gsize
85 g_unix_credentials_message_get_size (GSocketControlMessage *message)
86 {
87 #ifdef __linux__
88   return sizeof (struct ucred);
89 #elif defined(__FreeBSD__)
90   return sizeof (struct cmsgcred);
91 #else
92   return 0;
93 #endif
94 }
95
96 static int
97 g_unix_credentials_message_get_level (GSocketControlMessage *message)
98 {
99 #ifdef __linux__
100   return SOL_SOCKET;
101 #elif defined(__FreeBSD__)
102   return SOL_SOCKET;
103 #else
104   return 0;
105 #endif
106 }
107
108 static int
109 g_unix_credentials_message_get_msg_type (GSocketControlMessage *message)
110 {
111 #ifdef __linux__
112   return SCM_CREDENTIALS;
113 #elif defined(__FreeBSD__)
114   return SCM_CREDS;
115 #else
116   return 0;
117 #endif
118 }
119
120 static GSocketControlMessage *
121 g_unix_credentials_message_deserialize (gint     level,
122                                         gint     type,
123                                         gsize    size,
124                                         gpointer data)
125 {
126   GSocketControlMessage *message;
127
128   message = NULL;
129
130 #ifdef __linux__
131   {
132     GCredentials *credentials;
133     struct ucred *ucred;
134
135     if (level != SOL_SOCKET || type != SCM_CREDENTIALS)
136       goto out;
137
138     if (size != sizeof (struct ucred))
139       {
140         g_warning ("Expected a struct ucred (%" G_GSIZE_FORMAT " bytes) but "
141                    "got %" G_GSIZE_FORMAT " bytes of data",
142                    sizeof (struct ucred),
143                    size);
144         goto out;
145       }
146
147     ucred = data;
148
149     credentials = g_credentials_new ();
150     g_credentials_set_native (credentials, G_CREDENTIALS_TYPE_LINUX_UCRED, ucred);
151     message = g_unix_credentials_message_new_with_credentials (credentials);
152     g_object_unref (credentials);
153  out:
154     ;
155   }
156 #elif defined(__FreeBSD__)
157   {
158     GCredentials *credentials;
159     struct cmsgcred *cred;
160
161     if (level != SOL_SOCKET || type != SCM_CREDS)
162       {
163         goto out;
164       }
165     if (size < CMSG_LEN (sizeof *cred))
166       {
167         g_warning ("Expected a struct cmsgcred (%" G_GSIZE_FORMAT " bytes) but "
168                    "got %" G_GSIZE_FORMAT " bytes of data",
169                    CMSG_LEN (sizeof *cred),
170                    size);
171         goto out;
172       }
173
174     cred = data;
175
176     credentials = g_credentials_new ();
177     g_credentials_set_native (credentials, G_CREDENTIALS_TYPE_FREEBSD_CMSGCRED, cred);
178     message = g_unix_credentials_message_new_with_credentials (credentials);
179     g_object_unref (credentials);
180  out:
181     ;
182   }
183 #endif
184
185   return message;
186 }
187
188 static void
189 g_unix_credentials_message_serialize (GSocketControlMessage *_message,
190                                       gpointer               data)
191 {
192   GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (_message);
193 #ifdef __linux__
194   memcpy (data,
195           g_credentials_get_native (message->priv->credentials,
196                                     G_CREDENTIALS_TYPE_LINUX_UCRED),
197           sizeof (struct ucred));
198 #elif defined(__FreeBSD__)
199   memcpy (data,
200           g_credentials_get_native (message->priv->credentials,
201                                     G_CREDENTIALS_TYPE_FREEBSD_CMSGCRED),
202           sizeof (struct cmsgcred));
203
204 #endif
205 }
206
207 static void
208 g_unix_credentials_message_finalize (GObject *object)
209 {
210   GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (object);
211
212   if (message->priv->credentials != NULL)
213     g_object_unref (message->priv->credentials);
214
215   G_OBJECT_CLASS (g_unix_credentials_message_parent_class)->finalize (object);
216 }
217
218 static void
219 g_unix_credentials_message_init (GUnixCredentialsMessage *message)
220 {
221   message->priv = G_TYPE_INSTANCE_GET_PRIVATE (message,
222                                                G_TYPE_UNIX_CREDENTIALS_MESSAGE,
223                                                GUnixCredentialsMessagePrivate);
224 }
225
226 static void
227 g_unix_credentials_message_get_property (GObject    *object,
228                                          guint       prop_id,
229                                          GValue     *value,
230                                          GParamSpec *pspec)
231 {
232   GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (object);
233
234   switch (prop_id)
235     {
236     case PROP_CREDENTIALS:
237       g_value_set_object (value, message->priv->credentials);
238       break;
239
240     default:
241       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
242       break;
243     }
244 }
245
246 static void
247 g_unix_credentials_message_set_property (GObject      *object,
248                                          guint         prop_id,
249                                          const GValue *value,
250                                          GParamSpec   *pspec)
251 {
252   GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (object);
253
254   switch (prop_id)
255     {
256     case PROP_CREDENTIALS:
257       message->priv->credentials = g_value_dup_object (value);
258       break;
259
260     default:
261       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
262       break;
263     }
264 }
265
266 static void
267 g_unix_credentials_message_constructed (GObject *object)
268 {
269   GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (object);
270
271   if (message->priv->credentials == NULL)
272     message->priv->credentials = g_credentials_new ();
273
274   if (G_OBJECT_CLASS (g_unix_credentials_message_parent_class)->constructed != NULL)
275     G_OBJECT_CLASS (g_unix_credentials_message_parent_class)->constructed (object);
276 }
277
278 static void
279 g_unix_credentials_message_class_init (GUnixCredentialsMessageClass *class)
280 {
281   GSocketControlMessageClass *scm_class;
282   GObjectClass *gobject_class;
283
284   g_type_class_add_private (class, sizeof (GUnixCredentialsMessagePrivate));
285
286   gobject_class = G_OBJECT_CLASS (class);
287   gobject_class->get_property = g_unix_credentials_message_get_property;
288   gobject_class->set_property = g_unix_credentials_message_set_property;
289   gobject_class->finalize = g_unix_credentials_message_finalize;
290   gobject_class->constructed = g_unix_credentials_message_constructed;
291
292   scm_class = G_SOCKET_CONTROL_MESSAGE_CLASS (class);
293   scm_class->get_size = g_unix_credentials_message_get_size;
294   scm_class->get_level = g_unix_credentials_message_get_level;
295   scm_class->get_type = g_unix_credentials_message_get_msg_type;
296   scm_class->serialize = g_unix_credentials_message_serialize;
297   scm_class->deserialize = g_unix_credentials_message_deserialize;
298
299   /**
300    * GUnixCredentialsMessage:credentials:
301    *
302    * The credentials stored in the message.
303    *
304    * Since: 2.26
305    */
306   g_object_class_install_property (gobject_class,
307                                    PROP_CREDENTIALS,
308                                    g_param_spec_object ("credentials",
309                                                         P_("Credentials"),
310                                                         P_("The credentials stored in the message"),
311                                                         G_TYPE_CREDENTIALS,
312                                                         G_PARAM_READABLE |
313                                                         G_PARAM_WRITABLE |
314                                                         G_PARAM_CONSTRUCT_ONLY |
315                                                         G_PARAM_STATIC_NAME |
316                                                         G_PARAM_STATIC_BLURB |
317                                                         G_PARAM_STATIC_NICK));
318
319 }
320
321 /* ---------------------------------------------------------------------------------------------------- */
322
323 /**
324  * g_unix_credentials_message_is_supported:
325  *
326  * Checks if passing a #GCredential on a #GSocket is supported on this platform.
327  *
328  * Returns: %TRUE if supported, %FALSE otherwise
329  *
330  * Since: 2.26
331  */
332 gboolean
333 g_unix_credentials_message_is_supported (void)
334 {
335   return G_UNIX_CREDENTIALS_MESSAGE_SUPPORTED;
336 }
337
338 /* ---------------------------------------------------------------------------------------------------- */
339
340 /**
341  * g_unix_credentials_message_new:
342  *
343  * Creates a new #GUnixCredentialsMessage with credentials matching the current processes.
344  *
345  * Returns: a new #GUnixCredentialsMessage
346  *
347  * Since: 2.26
348  */
349 GSocketControlMessage *
350 g_unix_credentials_message_new (void)
351 {
352   g_return_val_if_fail (g_unix_credentials_message_is_supported (), NULL);
353   return g_object_new (G_TYPE_UNIX_CREDENTIALS_MESSAGE,
354                        NULL);
355 }
356
357 /**
358  * g_unix_credentials_message_new_with_credentials:
359  * @credentials: A #GCredentials object.
360  *
361  * Creates a new #GUnixCredentialsMessage holding @credentials.
362  *
363  * Returns: a new #GUnixCredentialsMessage
364  *
365  * Since: 2.26
366  */
367 GSocketControlMessage *
368 g_unix_credentials_message_new_with_credentials (GCredentials *credentials)
369 {
370   g_return_val_if_fail (G_IS_CREDENTIALS (credentials), NULL);
371   g_return_val_if_fail (g_unix_credentials_message_is_supported (), NULL);
372   return g_object_new (G_TYPE_UNIX_CREDENTIALS_MESSAGE,
373                        "credentials", credentials,
374                        NULL);
375 }
376
377 /**
378  * g_unix_credentials_message_get_credentials:
379  * @message: A #GUnixCredentialsMessage.
380  *
381  * Gets the credentials stored in @message.
382  *
383  * Returns: (transfer none): A #GCredentials instance. Do not free, it is owned by @message.
384  *
385  * Since: 2.26
386  */
387 GCredentials *
388 g_unix_credentials_message_get_credentials (GUnixCredentialsMessage *message)
389 {
390   g_return_val_if_fail (G_IS_UNIX_CREDENTIALS_MESSAGE (message), NULL);
391   return message->priv->credentials;
392 }