2be5c5d8db83b4f2fcd2224631707b6265920cfc
[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  * @see_also: #GUnixConnection, #GSocketControlMessage
21  *
22  * This #GSocketControlMessage contains a #GCredentials instance.  It
23  * may be sent using g_socket_send_message() and received using
24  * g_socket_receive_message() over UNIX sockets (ie: sockets in the
25  * %G_SOCKET_ADDRESS_UNIX family).
26  *
27  * For an easier way to send and receive credentials over
28  * stream-oriented UNIX sockets, see g_unix_connection_send_credentials() and
29  * g_unix_connection_receive_credentials().
30  **/
31
32 #include "config.h"
33
34 /* ---------------------------------------------------------------------------------------------------- */
35 #ifdef __linux__
36
37 #define _GNU_SOURCE
38 #define __USE_GNU
39 #include <sys/types.h>
40 #include <sys/socket.h>
41 #include <sys/un.h>
42 #include <unistd.h>
43 #include <fcntl.h>
44 #define G_UNIX_CREDENTIALS_MESSAGE_SUPPORTED 1
45
46 #else
47 /* TODO: please add support for your UNIX flavor */
48 #define G_UNIX_CREDENTIALS_MESSAGE_SUPPORTED 0
49 #endif
50
51 /* ---------------------------------------------------------------------------------------------------- */
52
53 #include <string.h>
54 #include <errno.h>
55
56 #include "gunixcredentialsmessage.h"
57 #include "gcredentials.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 #else
80   return 0;
81 #endif
82 }
83
84 static int
85 g_unix_credentials_message_get_level (GSocketControlMessage *message)
86 {
87   return SOL_SOCKET;
88 }
89
90 static int
91 g_unix_credentials_message_get_msg_type (GSocketControlMessage *message)
92 {
93 #ifdef __linux__
94   return SCM_CREDENTIALS;
95 #else
96   return 0;
97 #endif
98 }
99
100 static GSocketControlMessage *
101 g_unix_credentials_message_deserialize (gint     level,
102                                         gint     type,
103                                         gsize    size,
104                                         gpointer data)
105 {
106   GSocketControlMessage *message;
107
108   message = NULL;
109
110 #ifdef __linux__
111   {
112     GCredentials *credentials;
113     struct ucred *ucred;
114
115     if (level != SOL_SOCKET || type != SCM_CREDENTIALS)
116       goto out;
117
118     if (size != sizeof (struct ucred))
119       {
120         g_warning ("Expected a struct ucred (%" G_GSIZE_FORMAT " bytes) but "
121                    "got %" G_GSIZE_FORMAT " bytes of data",
122                    sizeof (struct ucred),
123                    size);
124         goto out;
125       }
126
127     ucred = data;
128
129     credentials = g_credentials_new ();
130     g_credentials_set_unix_user (credentials, ucred->uid);
131     g_credentials_set_unix_group (credentials, ucred->gid);
132     g_credentials_set_unix_process (credentials, ucred->pid);
133     message = g_unix_credentials_message_new_with_credentials (credentials);
134     g_object_unref (credentials);
135  out:
136     ;
137   }
138 #endif
139
140   return message;
141 }
142
143 static void
144 g_unix_credentials_message_serialize (GSocketControlMessage *_message,
145                                       gpointer               data)
146 {
147   GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (_message);
148 #ifdef __linux__
149   {
150     struct ucred *ucred = data;
151     ucred->uid = g_credentials_get_unix_user (message->priv->credentials);
152     ucred->gid = g_credentials_get_unix_group (message->priv->credentials);
153     ucred->pid = g_credentials_get_unix_process (message->priv->credentials);
154   }
155 #endif
156 }
157
158 static void
159 g_unix_credentials_message_finalize (GObject *object)
160 {
161   GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (object);
162
163   if (message->priv->credentials != NULL)
164     g_object_unref (message->priv->credentials);
165
166   if (G_OBJECT_CLASS (g_unix_credentials_message_parent_class)->finalize != NULL)
167     G_OBJECT_CLASS (g_unix_credentials_message_parent_class)->finalize (object);
168 }
169
170 static void
171 g_unix_credentials_message_init (GUnixCredentialsMessage *message)
172 {
173   message->priv = G_TYPE_INSTANCE_GET_PRIVATE (message,
174                                                G_TYPE_UNIX_CREDENTIALS_MESSAGE,
175                                                GUnixCredentialsMessagePrivate);
176 }
177
178 static void
179 g_unix_credentials_message_get_property (GObject    *object,
180                                          guint       prop_id,
181                                          GValue     *value,
182                                          GParamSpec *pspec)
183 {
184   GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (object);
185
186   switch (prop_id)
187     {
188     case PROP_CREDENTIALS:
189       g_value_set_object (value, message->priv->credentials);
190       break;
191
192     default:
193       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
194       break;
195     }
196 }
197
198 static void
199 g_unix_credentials_message_set_property (GObject      *object,
200                                          guint         prop_id,
201                                          const GValue *value,
202                                          GParamSpec   *pspec)
203 {
204   GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (object);
205
206   switch (prop_id)
207     {
208     case PROP_CREDENTIALS:
209       message->priv->credentials = g_value_dup_object (value);
210       break;
211
212     default:
213       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
214       break;
215     }
216 }
217
218 static void
219 g_unix_credentials_message_constructed (GObject *object)
220 {
221   GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (object);
222
223   if (message->priv->credentials == NULL)
224     message->priv->credentials = g_credentials_new_for_process ();
225
226   if (G_OBJECT_CLASS (g_unix_credentials_message_parent_class)->constructed != NULL)
227     G_OBJECT_CLASS (g_unix_credentials_message_parent_class)->constructed (object);
228 }
229
230 static void
231 g_unix_credentials_message_class_init (GUnixCredentialsMessageClass *class)
232 {
233   GSocketControlMessageClass *scm_class;
234   GObjectClass *gobject_class;
235
236   g_type_class_add_private (class, sizeof (GUnixCredentialsMessagePrivate));
237
238   gobject_class = G_OBJECT_CLASS (class);
239   gobject_class->get_property = g_unix_credentials_message_get_property;
240   gobject_class->set_property = g_unix_credentials_message_set_property;
241   gobject_class->finalize = g_unix_credentials_message_finalize;
242   gobject_class->constructed = g_unix_credentials_message_constructed;
243
244   scm_class = G_SOCKET_CONTROL_MESSAGE_CLASS (class);
245   scm_class->get_size = g_unix_credentials_message_get_size;
246   scm_class->get_level = g_unix_credentials_message_get_level;
247   scm_class->get_type = g_unix_credentials_message_get_msg_type;
248   scm_class->serialize = g_unix_credentials_message_serialize;
249   scm_class->deserialize = g_unix_credentials_message_deserialize;
250
251   /**
252    * GUnixCredentialsMessage:credentials:
253    *
254    * The credentials stored in the message.
255    *
256    * Since: 2.26
257    */
258   g_object_class_install_property (gobject_class,
259                                    PROP_CREDENTIALS,
260                                    g_param_spec_object ("credentials",
261                                                         _("Credentials"),
262                                                         _("The credentials stored in the message"),
263                                                         G_TYPE_CREDENTIALS,
264                                                         G_PARAM_READABLE |
265                                                         G_PARAM_WRITABLE |
266                                                         G_PARAM_CONSTRUCT_ONLY |
267                                                         G_PARAM_STATIC_NAME |
268                                                         G_PARAM_STATIC_BLURB |
269                                                         G_PARAM_STATIC_NICK));
270
271 }
272
273 /* ---------------------------------------------------------------------------------------------------- */
274
275 /**
276  * g_unix_credentials_message_is_supported:
277  *
278  * Checks if passing a #GCredential on a #GSocket is supported on this platform.
279  *
280  * Returns: %TRUE if supported, %FALSE otherwise
281  *
282  * Since: 2.26
283  */
284 gboolean
285 g_unix_credentials_message_is_supported (void)
286 {
287   return G_UNIX_CREDENTIALS_MESSAGE_SUPPORTED;
288 }
289
290 /* ---------------------------------------------------------------------------------------------------- */
291
292 /**
293  * g_unix_credentials_message_new:
294  *
295  * Creates a new #GUnixCredentialsMessage with credentials matching the current processes.
296  *
297  * Returns: a new #GUnixCredentialsMessage
298  *
299  * Since: 2.26
300  */
301 GSocketControlMessage *
302 g_unix_credentials_message_new (void)
303 {
304   g_return_val_if_fail (g_unix_credentials_message_is_supported (), NULL);
305   return g_object_new (G_TYPE_UNIX_CREDENTIALS_MESSAGE,
306                        NULL);
307 }
308
309 /**
310  * g_unix_credentials_message_new:
311  * @credentials: A #GCredentials object.
312  *
313  * Creates a new #GUnixCredentialsMessage holding @credentials.
314  *
315  * Returns: a new #GUnixCredentialsMessage
316  *
317  * Since: 2.26
318  */
319 GSocketControlMessage *
320 g_unix_credentials_message_new_with_credentials (GCredentials *credentials)
321 {
322   g_return_val_if_fail (G_IS_CREDENTIALS (credentials), NULL);
323   g_return_val_if_fail (g_unix_credentials_message_is_supported (), NULL);
324   return g_object_new (G_TYPE_UNIX_CREDENTIALS_MESSAGE,
325                        "credentials", credentials,
326                        NULL);
327 }
328
329 /**
330  * g_unix_credentials_message_get_credentials:
331  * @message: A #GUnixCredentialsMessage.
332  *
333  * Gets the credentials stored in @message.
334  *
335  * Returns: A #GCredentials instance. Do not free, it is owned by @message.
336  *
337  * Since: 2.26
338  */
339 GCredentials *
340 g_unix_credentials_message_get_credentials (GUnixCredentialsMessage *message)
341 {
342   g_return_val_if_fail (G_IS_UNIX_CREDENTIALS_MESSAGE (message), NULL);
343   return message->priv->credentials;
344 }
345