nexmon – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 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 | |||
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 | |||
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 |
||
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 |
||
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 | return 0; |
||
100 | #endif |
||
101 | } |
||
102 | |||
103 | static GSocketControlMessage * |
||
104 | g_unix_credentials_message_deserialize (gint level, |
||
105 | gint type, |
||
106 | gsize size, |
||
107 | gpointer data) |
||
108 | { |
||
109 | #if G_CREDENTIALS_UNIX_CREDENTIALS_MESSAGE_SUPPORTED |
||
110 | GSocketControlMessage *message; |
||
111 | GCredentials *credentials; |
||
112 | |||
113 | if (level != SOL_SOCKET || type != g_unix_credentials_message_get_msg_type (NULL)) |
||
114 | return NULL; |
||
115 | |||
116 | if (size != G_CREDENTIALS_NATIVE_SIZE) |
||
117 | { |
||
118 | g_warning ("Expected a credentials struct of %" G_GSIZE_FORMAT " bytes but " |
||
119 | "got %" G_GSIZE_FORMAT " bytes of data", |
||
120 | G_CREDENTIALS_NATIVE_SIZE, size); |
||
121 | return NULL; |
||
122 | } |
||
123 | |||
124 | credentials = g_credentials_new (); |
||
125 | g_credentials_set_native (credentials, G_CREDENTIALS_NATIVE_TYPE, data); |
||
126 | |||
127 | if (g_credentials_get_unix_user (credentials, NULL) == (uid_t) -1) |
||
128 | { |
||
129 | /* This happens on Linux if the remote side didn't pass the credentials */ |
||
130 | g_object_unref (credentials); |
||
131 | return NULL; |
||
132 | } |
||
133 | |||
134 | message = g_unix_credentials_message_new_with_credentials (credentials); |
||
135 | g_object_unref (credentials); |
||
136 | |||
137 | return message; |
||
138 | |||
139 | #else /* !G_CREDENTIALS_UNIX_CREDENTIALS_MESSAGE_SUPPORTED */ |
||
140 | |||
141 | return NULL; |
||
142 | #endif |
||
143 | } |
||
144 | |||
145 | static void |
||
146 | g_unix_credentials_message_serialize (GSocketControlMessage *_message, |
||
147 | gpointer data) |
||
148 | { |
||
149 | #if G_CREDENTIALS_UNIX_CREDENTIALS_MESSAGE_SUPPORTED |
||
150 | GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (_message); |
||
151 | |||
152 | memcpy (data, |
||
153 | g_credentials_get_native (message->priv->credentials, |
||
154 | G_CREDENTIALS_NATIVE_TYPE), |
||
155 | G_CREDENTIALS_NATIVE_SIZE); |
||
156 | #endif |
||
157 | } |
||
158 | |||
159 | static void |
||
160 | g_unix_credentials_message_finalize (GObject *object) |
||
161 | { |
||
162 | GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (object); |
||
163 | |||
164 | if (message->priv->credentials != NULL) |
||
165 | g_object_unref (message->priv->credentials); |
||
166 | |||
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_unix_credentials_message_get_instance_private (message); |
||
174 | } |
||
175 | |||
176 | static void |
||
177 | g_unix_credentials_message_get_property (GObject *object, |
||
178 | guint prop_id, |
||
179 | GValue *value, |
||
180 | GParamSpec *pspec) |
||
181 | { |
||
182 | GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (object); |
||
183 | |||
184 | switch (prop_id) |
||
185 | { |
||
186 | case PROP_CREDENTIALS: |
||
187 | g_value_set_object (value, message->priv->credentials); |
||
188 | break; |
||
189 | |||
190 | default: |
||
191 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
||
192 | break; |
||
193 | } |
||
194 | } |
||
195 | |||
196 | static void |
||
197 | g_unix_credentials_message_set_property (GObject *object, |
||
198 | guint prop_id, |
||
199 | const GValue *value, |
||
200 | GParamSpec *pspec) |
||
201 | { |
||
202 | GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (object); |
||
203 | |||
204 | switch (prop_id) |
||
205 | { |
||
206 | case PROP_CREDENTIALS: |
||
207 | message->priv->credentials = g_value_dup_object (value); |
||
208 | break; |
||
209 | |||
210 | default: |
||
211 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
||
212 | break; |
||
213 | } |
||
214 | } |
||
215 | |||
216 | static void |
||
217 | g_unix_credentials_message_constructed (GObject *object) |
||
218 | { |
||
219 | GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (object); |
||
220 | |||
221 | if (message->priv->credentials == NULL) |
||
222 | message->priv->credentials = g_credentials_new (); |
||
223 | |||
224 | if (G_OBJECT_CLASS (g_unix_credentials_message_parent_class)->constructed != NULL) |
||
225 | G_OBJECT_CLASS (g_unix_credentials_message_parent_class)->constructed (object); |
||
226 | } |
||
227 | |||
228 | static void |
||
229 | g_unix_credentials_message_class_init (GUnixCredentialsMessageClass *class) |
||
230 | { |
||
231 | GSocketControlMessageClass *scm_class; |
||
232 | GObjectClass *gobject_class; |
||
233 | |||
234 | gobject_class = G_OBJECT_CLASS (class); |
||
235 | gobject_class->get_property = g_unix_credentials_message_get_property; |
||
236 | gobject_class->set_property = g_unix_credentials_message_set_property; |
||
237 | gobject_class->finalize = g_unix_credentials_message_finalize; |
||
238 | gobject_class->constructed = g_unix_credentials_message_constructed; |
||
239 | |||
240 | scm_class = G_SOCKET_CONTROL_MESSAGE_CLASS (class); |
||
241 | scm_class->get_size = g_unix_credentials_message_get_size; |
||
242 | scm_class->get_level = g_unix_credentials_message_get_level; |
||
243 | scm_class->get_type = g_unix_credentials_message_get_msg_type; |
||
244 | scm_class->serialize = g_unix_credentials_message_serialize; |
||
245 | scm_class->deserialize = g_unix_credentials_message_deserialize; |
||
246 | |||
247 | /** |
||
248 | * GUnixCredentialsMessage:credentials: |
||
249 | * |
||
250 | * The credentials stored in the message. |
||
251 | * |
||
252 | * Since: 2.26 |
||
253 | */ |
||
254 | g_object_class_install_property (gobject_class, |
||
255 | PROP_CREDENTIALS, |
||
256 | g_param_spec_object ("credentials", |
||
257 | P_("Credentials"), |
||
258 | P_("The credentials stored in the message"), |
||
259 | G_TYPE_CREDENTIALS, |
||
260 | G_PARAM_READABLE | |
||
261 | G_PARAM_WRITABLE | |
||
262 | G_PARAM_CONSTRUCT_ONLY | |
||
263 | G_PARAM_STATIC_NAME | |
||
264 | G_PARAM_STATIC_BLURB | |
||
265 | G_PARAM_STATIC_NICK)); |
||
266 | |||
267 | } |
||
268 | |||
269 | /* ---------------------------------------------------------------------------------------------------- */ |
||
270 | |||
271 | /** |
||
272 | * g_unix_credentials_message_is_supported: |
||
273 | * |
||
274 | * Checks if passing #GCredentials on a #GSocket is supported on this platform. |
||
275 | * |
||
276 | * Returns: %TRUE if supported, %FALSE otherwise |
||
277 | * |
||
278 | * Since: 2.26 |
||
279 | */ |
||
280 | gboolean |
||
281 | g_unix_credentials_message_is_supported (void) |
||
282 | { |
||
283 | #if G_CREDENTIALS_UNIX_CREDENTIALS_MESSAGE_SUPPORTED |
||
284 | return TRUE; |
||
285 | #else |
||
286 | return FALSE; |
||
287 | #endif |
||
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_with_credentials: |
||
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: (transfer none): 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 | } |