• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at 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 
40 #include <fcntl.h>
41 #include <errno.h>
42 #include <string.h>
43 #include <unistd.h>
44 
45 #include "gunixcredentialsmessage.h"
46 #include "gcredentials.h"
47 #include "gcredentialsprivate.h"
48 #include "gnetworking.h"
49 
50 #include "glibintl.h"
51 
52 struct _GUnixCredentialsMessagePrivate
53 {
54   GCredentials *credentials;
55 };
56 
57 enum
58 {
59   PROP_0,
60   PROP_CREDENTIALS
61 };
62 
G_DEFINE_TYPE_WITH_PRIVATE(GUnixCredentialsMessage,g_unix_credentials_message,G_TYPE_SOCKET_CONTROL_MESSAGE)63 G_DEFINE_TYPE_WITH_PRIVATE (GUnixCredentialsMessage, g_unix_credentials_message, G_TYPE_SOCKET_CONTROL_MESSAGE)
64 
65 static gsize
66 g_unix_credentials_message_get_size (GSocketControlMessage *message)
67 {
68 #if G_CREDENTIALS_UNIX_CREDENTIALS_MESSAGE_SUPPORTED
69   return G_CREDENTIALS_NATIVE_SIZE;
70 #else
71   return 0;
72 #endif
73 }
74 
75 static int
g_unix_credentials_message_get_level(GSocketControlMessage * message)76 g_unix_credentials_message_get_level (GSocketControlMessage *message)
77 {
78 #if G_CREDENTIALS_UNIX_CREDENTIALS_MESSAGE_SUPPORTED
79   return SOL_SOCKET;
80 #else
81   return 0;
82 #endif
83 }
84 
85 static int
g_unix_credentials_message_get_msg_type(GSocketControlMessage * message)86 g_unix_credentials_message_get_msg_type (GSocketControlMessage *message)
87 {
88 #if G_CREDENTIALS_USE_LINUX_UCRED
89   return SCM_CREDENTIALS;
90 #elif G_CREDENTIALS_USE_FREEBSD_CMSGCRED
91   return SCM_CREDS;
92 #elif G_CREDENTIALS_USE_NETBSD_UNPCBID
93   return SCM_CREDS;
94 #elif G_CREDENTIALS_USE_SOLARIS_UCRED
95   return SCM_UCRED;
96 #elif G_CREDENTIALS_UNIX_CREDENTIALS_MESSAGE_SUPPORTED
97   #error "G_CREDENTIALS_UNIX_CREDENTIALS_MESSAGE_SUPPORTED is set but there is no msg_type defined for this platform"
98 #else
99   /* includes G_CREDENTIALS_USE_APPLE_XUCRED */
100   return 0;
101 #endif
102 }
103 
104 static GSocketControlMessage *
g_unix_credentials_message_deserialize(gint level,gint type,gsize size,gpointer data)105 g_unix_credentials_message_deserialize (gint     level,
106                                         gint     type,
107                                         gsize    size,
108                                         gpointer data)
109 {
110 #if G_CREDENTIALS_UNIX_CREDENTIALS_MESSAGE_SUPPORTED
111   GSocketControlMessage *message;
112   GCredentials *credentials;
113 
114   if (level != SOL_SOCKET || type != g_unix_credentials_message_get_msg_type (NULL))
115     return NULL;
116 
117   if (size != G_CREDENTIALS_NATIVE_SIZE)
118     {
119       g_warning ("Expected a credentials struct of %" G_GSIZE_FORMAT " bytes but "
120                  "got %" G_GSIZE_FORMAT " bytes of data",
121                  G_CREDENTIALS_NATIVE_SIZE, size);
122       return NULL;
123     }
124 
125   credentials = g_credentials_new ();
126   g_credentials_set_native (credentials, G_CREDENTIALS_NATIVE_TYPE, data);
127 
128   if (g_credentials_get_unix_user (credentials, NULL) == (uid_t) -1)
129     {
130       /* This happens on Linux if the remote side didn't pass the credentials */
131       g_object_unref (credentials);
132       return NULL;
133     }
134 
135   message = g_unix_credentials_message_new_with_credentials (credentials);
136   g_object_unref (credentials);
137 
138   return message;
139 
140 #else /* !G_CREDENTIALS_UNIX_CREDENTIALS_MESSAGE_SUPPORTED */
141 
142   return NULL;
143 #endif
144 }
145 
146 static void
g_unix_credentials_message_serialize(GSocketControlMessage * _message,gpointer data)147 g_unix_credentials_message_serialize (GSocketControlMessage *_message,
148                                       gpointer               data)
149 {
150 #if G_CREDENTIALS_UNIX_CREDENTIALS_MESSAGE_SUPPORTED
151   GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (_message);
152 
153   memcpy (data,
154           g_credentials_get_native (message->priv->credentials,
155                                     G_CREDENTIALS_NATIVE_TYPE),
156           G_CREDENTIALS_NATIVE_SIZE);
157 #endif
158 }
159 
160 static void
g_unix_credentials_message_finalize(GObject * object)161 g_unix_credentials_message_finalize (GObject *object)
162 {
163   GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (object);
164 
165   if (message->priv->credentials != NULL)
166     g_object_unref (message->priv->credentials);
167 
168   G_OBJECT_CLASS (g_unix_credentials_message_parent_class)->finalize (object);
169 }
170 
171 static void
g_unix_credentials_message_init(GUnixCredentialsMessage * message)172 g_unix_credentials_message_init (GUnixCredentialsMessage *message)
173 {
174   message->priv = g_unix_credentials_message_get_instance_private (message);
175 }
176 
177 static void
g_unix_credentials_message_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)178 g_unix_credentials_message_get_property (GObject    *object,
179                                          guint       prop_id,
180                                          GValue     *value,
181                                          GParamSpec *pspec)
182 {
183   GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (object);
184 
185   switch (prop_id)
186     {
187     case PROP_CREDENTIALS:
188       g_value_set_object (value, message->priv->credentials);
189       break;
190 
191     default:
192       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
193       break;
194     }
195 }
196 
197 static void
g_unix_credentials_message_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)198 g_unix_credentials_message_set_property (GObject      *object,
199                                          guint         prop_id,
200                                          const GValue *value,
201                                          GParamSpec   *pspec)
202 {
203   GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (object);
204 
205   switch (prop_id)
206     {
207     case PROP_CREDENTIALS:
208       message->priv->credentials = g_value_dup_object (value);
209       break;
210 
211     default:
212       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
213       break;
214     }
215 }
216 
217 static void
g_unix_credentials_message_constructed(GObject * object)218 g_unix_credentials_message_constructed (GObject *object)
219 {
220   GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (object);
221 
222   if (message->priv->credentials == NULL)
223     message->priv->credentials = g_credentials_new ();
224 
225   if (G_OBJECT_CLASS (g_unix_credentials_message_parent_class)->constructed != NULL)
226     G_OBJECT_CLASS (g_unix_credentials_message_parent_class)->constructed (object);
227 }
228 
229 static void
g_unix_credentials_message_class_init(GUnixCredentialsMessageClass * class)230 g_unix_credentials_message_class_init (GUnixCredentialsMessageClass *class)
231 {
232   GSocketControlMessageClass *scm_class;
233   GObjectClass *gobject_class;
234 
235   gobject_class = G_OBJECT_CLASS (class);
236   gobject_class->get_property = g_unix_credentials_message_get_property;
237   gobject_class->set_property = g_unix_credentials_message_set_property;
238   gobject_class->finalize = g_unix_credentials_message_finalize;
239   gobject_class->constructed = g_unix_credentials_message_constructed;
240 
241   scm_class = G_SOCKET_CONTROL_MESSAGE_CLASS (class);
242   scm_class->get_size = g_unix_credentials_message_get_size;
243   scm_class->get_level = g_unix_credentials_message_get_level;
244   scm_class->get_type = g_unix_credentials_message_get_msg_type;
245   scm_class->serialize = g_unix_credentials_message_serialize;
246   scm_class->deserialize = g_unix_credentials_message_deserialize;
247 
248   /**
249    * GUnixCredentialsMessage:credentials:
250    *
251    * The credentials stored in the message.
252    *
253    * Since: 2.26
254    */
255   g_object_class_install_property (gobject_class,
256                                    PROP_CREDENTIALS,
257                                    g_param_spec_object ("credentials",
258                                                         P_("Credentials"),
259                                                         P_("The credentials stored in the message"),
260                                                         G_TYPE_CREDENTIALS,
261                                                         G_PARAM_READABLE |
262                                                         G_PARAM_WRITABLE |
263                                                         G_PARAM_CONSTRUCT_ONLY |
264                                                         G_PARAM_STATIC_NAME |
265                                                         G_PARAM_STATIC_BLURB |
266                                                         G_PARAM_STATIC_NICK));
267 
268 }
269 
270 /* ---------------------------------------------------------------------------------------------------- */
271 
272 /**
273  * g_unix_credentials_message_is_supported:
274  *
275  * Checks if passing #GCredentials on a #GSocket is supported on this platform.
276  *
277  * Returns: %TRUE if supported, %FALSE otherwise
278  *
279  * Since: 2.26
280  */
281 gboolean
g_unix_credentials_message_is_supported(void)282 g_unix_credentials_message_is_supported (void)
283 {
284 #if G_CREDENTIALS_UNIX_CREDENTIALS_MESSAGE_SUPPORTED
285   return TRUE;
286 #else
287   return FALSE;
288 #endif
289 }
290 
291 /* ---------------------------------------------------------------------------------------------------- */
292 
293 /**
294  * g_unix_credentials_message_new:
295  *
296  * Creates a new #GUnixCredentialsMessage with credentials matching the current processes.
297  *
298  * Returns: a new #GUnixCredentialsMessage
299  *
300  * Since: 2.26
301  */
302 GSocketControlMessage *
g_unix_credentials_message_new(void)303 g_unix_credentials_message_new (void)
304 {
305   g_return_val_if_fail (g_unix_credentials_message_is_supported (), NULL);
306   return g_object_new (G_TYPE_UNIX_CREDENTIALS_MESSAGE,
307                        NULL);
308 }
309 
310 /**
311  * g_unix_credentials_message_new_with_credentials:
312  * @credentials: A #GCredentials object.
313  *
314  * Creates a new #GUnixCredentialsMessage holding @credentials.
315  *
316  * Returns: a new #GUnixCredentialsMessage
317  *
318  * Since: 2.26
319  */
320 GSocketControlMessage *
g_unix_credentials_message_new_with_credentials(GCredentials * credentials)321 g_unix_credentials_message_new_with_credentials (GCredentials *credentials)
322 {
323   g_return_val_if_fail (G_IS_CREDENTIALS (credentials), NULL);
324   g_return_val_if_fail (g_unix_credentials_message_is_supported (), NULL);
325   return g_object_new (G_TYPE_UNIX_CREDENTIALS_MESSAGE,
326                        "credentials", credentials,
327                        NULL);
328 }
329 
330 /**
331  * g_unix_credentials_message_get_credentials:
332  * @message: A #GUnixCredentialsMessage.
333  *
334  * Gets the credentials stored in @message.
335  *
336  * Returns: (transfer none): A #GCredentials instance. Do not free, it is owned by @message.
337  *
338  * Since: 2.26
339  */
340 GCredentials *
g_unix_credentials_message_get_credentials(GUnixCredentialsMessage * message)341 g_unix_credentials_message_get_credentials (GUnixCredentialsMessage *message)
342 {
343   g_return_val_if_fail (G_IS_UNIX_CREDENTIALS_MESSAGE (message), NULL);
344   return message->priv->credentials;
345 }
346