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