1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-transport.c DBusTransport object (internal to D-Bus implementation)
4 * Copyright (C) 2002, 2003 Red Hat Inc.
5 * Copyright (C) 2013 Samsung Electronics
7 * Licensed under the Academic Free License version 2.1
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "dbus-transport-protected.h"
27 #include "dbus-transport-unix.h"
28 #include "dbus-transport-socket.h"
29 #include "dbus-connection-internal.h"
30 #include "dbus-watch.h"
31 #include "dbus-auth.h"
32 #include "dbus-address.h"
33 #include "dbus-credentials.h"
34 #include "dbus-mainloop.h"
35 #include "dbus-message.h"
36 #include "dbus-message-internal.h"
37 #ifdef ENABLE_KDBUS_TRANSPORT
38 #include "dbus-transport-kdbus.h"
40 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
41 #include "dbus-server-debug-pipe.h"
45 * @defgroup DBusTransport DBusTransport object
46 * @ingroup DBusInternals
47 * @brief "Backend" for a DBusConnection.
49 * Types and functions related to DBusTransport. A transport is an
50 * abstraction that can send and receive data via various kinds of
51 * network connections or other IPC mechanisms.
57 * @typedef DBusTransport
59 * Opaque object representing a way message stream.
60 * DBusTransport abstracts various kinds of actual
61 * transport mechanism, such as different network protocols,
62 * or encryption schemes.
66 live_messages_notify (DBusCounter *counter,
69 DBusTransport *transport = user_data;
71 _dbus_connection_lock (transport->connection);
72 _dbus_transport_ref (transport);
75 _dbus_verbose ("Size counter value is now %d\n",
76 (int) _dbus_counter_get_size_value (counter));
77 _dbus_verbose ("Unix FD counter value is now %d\n",
78 (int) _dbus_counter_get_unix_fd_value (counter));
81 /* disable or re-enable the read watch for the transport if
84 if (transport->vtable->live_messages_changed)
86 (* transport->vtable->live_messages_changed) (transport);
89 _dbus_transport_unref (transport);
90 _dbus_connection_unlock (transport->connection);
94 _dbus_transport_default_get_unix_user (DBusTransport *transport,
97 DBusCredentials *auth_identity;
99 *uid = _DBUS_INT32_MAX; /* better than some root or system user in
100 * case of bugs in the caller. Caller should
101 * never use this value on purpose, however.
104 if (!transport->authenticated)
107 auth_identity = _dbus_auth_get_identity (transport->auth);
109 if (_dbus_credentials_include (auth_identity,
110 DBUS_CREDENTIAL_UNIX_USER_ID))
112 *uid = _dbus_credentials_get_unix_uid (auth_identity);
120 _dbus_transport_default_get_unix_process_id (DBusTransport *transport,
123 DBusCredentials *auth_identity;
125 *pid = DBUS_PID_UNSET; /* Caller should never use this value on purpose,
126 * but we set it to a safe number, INT_MAX,
127 * just to root out possible bugs in bad callers.
130 if (!transport->authenticated)
133 auth_identity = _dbus_auth_get_identity (transport->auth);
135 if (_dbus_credentials_include (auth_identity,
136 DBUS_CREDENTIAL_UNIX_PROCESS_ID))
138 *pid = _dbus_credentials_get_pid (auth_identity);
146 * Initializes the base class members of DBusTransport. Chained up to
147 * by subclasses in their constructor. The server GUID is the
148 * globally unique ID for the server creating this connection
149 * and will be #NULL for the client side of a connection. The GUID
152 * @param transport the transport being created.
153 * @param vtable the subclass vtable.
154 * @param server_guid non-#NULL if this transport is on the server side of a connection
155 * @param address the address of the transport
156 * @param with_auth TRUE if authentication should be used
157 * @returns #TRUE on success.
160 _dbus_transport_init_base_with_auth (DBusTransport *transport,
161 const DBusTransportVTable *vtable,
162 const DBusString *server_guid,
163 const DBusString *address,
164 dbus_bool_t with_auth)
166 DBusMessageLoader *loader;
168 DBusCounter *counter;
170 DBusCredentials *creds;
172 loader = _dbus_message_loader_new ();
177 auth = _dbus_auth_server_new (server_guid);
181 auth = _dbus_auth_client_new ();
183 auth = _dbus_auth_client_new_authenticated ();
188 _dbus_message_loader_unref (loader);
192 counter = _dbus_counter_new ();
195 _dbus_auth_unref (auth);
196 _dbus_message_loader_unref (loader);
200 creds = _dbus_credentials_new ();
203 _dbus_counter_unref (counter);
204 _dbus_auth_unref (auth);
205 _dbus_message_loader_unref (loader);
211 _dbus_assert (address == NULL);
216 _dbus_assert (address != NULL);
218 if (!_dbus_string_copy_data (address, &address_copy))
220 _dbus_credentials_unref (creds);
221 _dbus_counter_unref (counter);
222 _dbus_auth_unref (auth);
223 _dbus_message_loader_unref (loader);
228 transport->refcount = 1;
229 transport->vtable = vtable;
230 transport->loader = loader;
231 transport->auth = auth;
232 transport->live_messages = counter;
233 transport->authenticated = FALSE;
234 transport->disconnected = FALSE;
235 transport->is_server = (server_guid != NULL);
236 transport->send_credentials_pending = !transport->is_server;
237 transport->receive_credentials_pending = transport->is_server;
238 transport->address = address_copy;
240 transport->unix_user_function = NULL;
241 transport->unix_user_data = NULL;
242 transport->free_unix_user_data = NULL;
244 transport->windows_user_function = NULL;
245 transport->windows_user_data = NULL;
246 transport->free_windows_user_data = NULL;
248 transport->expected_guid = NULL;
250 /* Try to default to something that won't totally hose the system,
251 * but doesn't impose too much of a limitation.
253 transport->max_live_messages_size = _DBUS_ONE_MEGABYTE * 63;
255 /* On Linux RLIMIT_NOFILE defaults to 1024, so allowing 4096 fds live
256 should be more than enough */
257 transport->max_live_messages_unix_fds = 4096;
259 /* credentials read from socket if any */
260 transport->credentials = creds;
262 _dbus_counter_set_notify (transport->live_messages,
263 transport->max_live_messages_size,
264 transport->max_live_messages_unix_fds,
265 live_messages_notify,
268 if (transport->address)
269 _dbus_verbose ("Initialized transport on address %s\n", transport->address);
271 transport->get_unix_user_function = _dbus_transport_default_get_unix_user;
272 transport->get_unix_process_id_function = _dbus_transport_default_get_unix_process_id;
273 transport->assure_protocol_function = _dbus_message_assure_dbus1;
274 transport->send_sync_call_function = NULL;
275 transport->protocol = DBUS_MAJOR_PROTOCOL_VERSION;
276 transport->overflowed = FALSE;
282 _dbus_transport_assure_protocol_version (DBusTransport *transport,
283 DBusMessage **message)
285 return transport->assure_protocol_function (message);
289 _dbus_transport_send_sync_call (DBusTransport *transport,
290 DBusMessage *message)
292 return transport->send_sync_call_function (transport, message);
296 _dbus_transport_get_protocol (DBusTransport *transport)
298 return transport->protocol;
302 _dbus_transport_get_overflowed (DBusTransport *transport)
304 return transport->overflowed;
308 * Initializes the base class members of DBusTransport. Chained up to
309 * by subclasses in their constructor. The server GUID is the
310 * globally unique ID for the server creating this connection
311 * and will be #NULL for the client side of a connection. The GUID
314 * @param transport the transport being created.
315 * @param vtable the subclass vtable.
316 * @param server_guid non-#NULL if this transport is on the server side of a connection
317 * @param address the address of the transport
318 * @returns #TRUE on success.
321 _dbus_transport_init_base (DBusTransport *transport,
322 const DBusTransportVTable *vtable,
323 const DBusString *server_guid,
324 const DBusString *address)
326 return _dbus_transport_init_base_with_auth (transport, vtable, server_guid, address, TRUE);
330 * Initializes the base class members of DBusTransport. Chained up to
331 * by subclasses in their constructor. The server GUID is the
332 * globally unique ID for the server creating this connection
333 * and will be #NULL for the client side of a connection. The GUID
334 * is in hex format. Differs from _dbus_transport_init_base in that
335 * it sets auth as authenticated. This way auth negotiation is skipped.
337 * @param transport the transport being created.
338 * @param vtable the subclass vtable.
339 * @param server_guid non-#NULL if this transport is on the server side of a connection
340 * @param address the address of the transport
341 * @returns #TRUE on success.
344 _dbus_transport_init_base_authenticated (DBusTransport *transport,
345 const DBusTransportVTable *vtable,
346 const DBusString *server_guid,
347 const DBusString *address)
349 dbus_bool_t result = _dbus_transport_init_base_with_auth (transport, vtable, server_guid, address, FALSE);
351 transport->authenticated = TRUE;
356 * Finalizes base class members of DBusTransport.
357 * Chained up to from subclass finalizers.
359 * @param transport the transport.
362 _dbus_transport_finalize_base (DBusTransport *transport)
364 if (!transport->disconnected)
365 _dbus_transport_disconnect (transport);
367 if (transport->free_unix_user_data != NULL)
368 (* transport->free_unix_user_data) (transport->unix_user_data);
370 if (transport->free_windows_user_data != NULL)
371 (* transport->free_windows_user_data) (transport->windows_user_data);
373 _dbus_message_loader_unref (transport->loader);
374 _dbus_auth_unref (transport->auth);
375 _dbus_counter_set_notify (transport->live_messages,
377 _dbus_counter_unref (transport->live_messages);
378 dbus_free (transport->address);
379 dbus_free (transport->expected_guid);
380 if (transport->credentials)
381 _dbus_credentials_unref (transport->credentials);
386 * Verifies if a given D-Bus address is a valid address
387 * by attempting to connect to it. If it is, returns the
388 * opened DBusTransport object. If it isn't, returns #NULL
391 * @param address the address to be checked.
392 * @param error address where an error can be returned.
393 * @returns a new transport, or #NULL on failure.
395 static DBusTransport*
396 check_address (const char *address, DBusError *error)
398 DBusAddressEntry **entries;
399 DBusTransport *transport = NULL;
402 _dbus_assert (address != NULL);
403 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
405 if (!dbus_parse_address (address, &entries, &len, error))
406 return NULL; /* not a valid address */
408 for (i = 0; i < len; i++)
410 dbus_error_free (error);
411 transport = _dbus_transport_open (entries[i], error);
413 if (transport != NULL)
417 dbus_address_entries_free (entries);
422 * Creates a new transport for the "autostart" method.
423 * This creates a client-side of a transport.
425 * @param scope scope of autolaunch (Windows only)
426 * @param error address where an error can be returned.
427 * @returns a new transport, or #NULL on failure.
429 static DBusTransport*
430 _dbus_transport_new_for_autolaunch (const char *scope, DBusError *error)
433 DBusTransport *result = NULL;
435 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
437 if (!_dbus_string_init (&address))
439 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
443 if (!_dbus_get_autolaunch_address (scope, &address, error))
445 _DBUS_ASSERT_ERROR_IS_SET (error);
449 result = check_address (_dbus_string_get_const_data (&address), error);
451 _DBUS_ASSERT_ERROR_IS_SET (error);
453 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
456 _dbus_string_free (&address);
460 static DBusTransportOpenResult
461 _dbus_transport_open_autolaunch (DBusAddressEntry *entry,
462 DBusTransport **transport_p,
467 method = dbus_address_entry_get_method (entry);
468 _dbus_assert (method != NULL);
470 if (strcmp (method, "autolaunch") == 0)
472 const char *scope = dbus_address_entry_get_value (entry, "scope");
474 *transport_p = _dbus_transport_new_for_autolaunch (scope, error);
476 if (*transport_p == NULL)
478 _DBUS_ASSERT_ERROR_IS_SET (error);
479 return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
483 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
484 return DBUS_TRANSPORT_OPEN_OK;
489 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
490 return DBUS_TRANSPORT_OPEN_NOT_HANDLED;
494 static const struct {
495 DBusTransportOpenResult (* func) (DBusAddressEntry *entry,
496 DBusTransport **transport_p,
499 #ifdef ENABLE_KDBUS_TRANSPORT
500 { _dbus_transport_open_kdbus },
502 { _dbus_transport_open_socket },
503 { _dbus_transport_open_platform_specific },
504 { _dbus_transport_open_autolaunch }
505 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
506 , { _dbus_transport_open_debug_pipe }
511 * Try to open a new transport for the given address entry. (This
512 * opens a client-side-of-the-connection transport.)
514 * @param entry the address entry
515 * @param error location to store reason for failure.
516 * @returns new transport of #NULL on failure.
519 _dbus_transport_open (DBusAddressEntry *entry,
522 DBusTransport *transport;
523 const char *expected_guid_orig;
526 DBusError tmp_error = DBUS_ERROR_INIT;
528 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
531 expected_guid_orig = dbus_address_entry_get_value (entry, "guid");
532 expected_guid = _dbus_strdup (expected_guid_orig);
534 if (expected_guid_orig != NULL && expected_guid == NULL)
536 _DBUS_SET_OOM (error);
540 for (i = 0; i < (int) _DBUS_N_ELEMENTS (open_funcs); ++i)
542 DBusTransportOpenResult result;
544 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
545 result = (* open_funcs[i].func) (entry, &transport, &tmp_error);
549 case DBUS_TRANSPORT_OPEN_OK:
550 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
553 case DBUS_TRANSPORT_OPEN_NOT_HANDLED:
554 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
555 /* keep going through the loop of open funcs */
557 case DBUS_TRANSPORT_OPEN_BAD_ADDRESS:
558 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
561 case DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT:
562 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
566 _dbus_assert_not_reached ("invalid transport open result");
573 if (transport == NULL)
575 if (!dbus_error_is_set (&tmp_error))
576 _dbus_set_bad_address (&tmp_error,
578 "Unknown address type (examples of valid types are \"tcp\" and on UNIX \"unix\")");
580 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
581 dbus_move_error(&tmp_error, error);
582 dbus_free (expected_guid);
586 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
588 /* In the case of autostart the initial guid is NULL
589 * and the autostart transport recursively calls
590 * _dbus_open_transport wich returns a transport
591 * with a guid. That guid is the definitive one.
593 * FIXME: if more transports are added they may have
594 * an effect on the expected_guid semantics (i.e.
595 * expected_guid and transport->expected_guid may
596 * both have values). This is very unlikely though
597 * we should either throw asserts here for those
598 * corner cases or refactor the code so it is
599 * clearer on what is expected and what is not
602 transport->expected_guid = expected_guid;
603 _dbus_on_new_bus (_dbus_transport_get_protocol (transport));
610 * Increments the reference count for the transport.
612 * @param transport the transport.
613 * @returns the transport.
616 _dbus_transport_ref (DBusTransport *transport)
618 _dbus_assert (transport->refcount > 0);
620 transport->refcount += 1;
626 * Decrements the reference count for the transport.
627 * Disconnects and finalizes the transport if
628 * the reference count reaches zero.
630 * @param transport the transport.
633 _dbus_transport_unref (DBusTransport *transport)
635 _dbus_assert (transport != NULL);
636 _dbus_assert (transport->refcount > 0);
638 transport->refcount -= 1;
639 if (transport->refcount == 0)
641 _dbus_verbose ("finalizing\n");
643 _dbus_assert (transport->vtable->finalize != NULL);
645 (* transport->vtable->finalize) (transport);
650 * Closes our end of the connection to a remote application. Further
651 * attempts to use this transport will fail. Only the first call to
652 * _dbus_transport_disconnect() will have an effect.
654 * @param transport the transport.
658 _dbus_transport_disconnect (DBusTransport *transport)
660 _dbus_verbose ("start\n");
662 _dbus_assert (transport->vtable->disconnect != NULL);
664 if (transport->disconnected)
667 (* transport->vtable->disconnect) (transport);
669 transport->disconnected = TRUE;
671 _dbus_verbose ("end\n");
675 * Returns #TRUE if the transport has not been disconnected.
676 * Disconnection can result from _dbus_transport_disconnect()
677 * or because the server drops its end of the connection.
679 * @param transport the transport.
680 * @returns whether we're connected
683 _dbus_transport_get_is_connected (DBusTransport *transport)
685 return !transport->disconnected;
689 auth_via_unix_user_function (DBusTransport *transport)
691 DBusCredentials *auth_identity;
693 DBusConnection *connection;
694 DBusAllowUnixUserFunction unix_user_function;
695 void *unix_user_data;
698 /* Dropping the lock here probably isn't that safe. */
700 auth_identity = _dbus_auth_get_identity (transport->auth);
701 _dbus_assert (auth_identity != NULL);
703 connection = transport->connection;
704 unix_user_function = transport->unix_user_function;
705 unix_user_data = transport->unix_user_data;
706 uid = _dbus_credentials_get_unix_uid (auth_identity);
708 _dbus_verbose ("unlock\n");
709 _dbus_connection_unlock (connection);
711 allow = (* unix_user_function) (connection,
715 _dbus_verbose ("lock post unix user function\n");
716 _dbus_connection_lock (connection);
720 _dbus_verbose ("Client UID "DBUS_UID_FORMAT" authorized\n", uid);
724 _dbus_verbose ("Client UID "DBUS_UID_FORMAT
725 " was rejected, disconnecting\n",
726 _dbus_credentials_get_unix_uid (auth_identity));
727 _dbus_transport_disconnect (transport);
734 auth_via_windows_user_function (DBusTransport *transport)
736 DBusCredentials *auth_identity;
738 DBusConnection *connection;
739 DBusAllowWindowsUserFunction windows_user_function;
740 void *windows_user_data;
743 /* Dropping the lock here probably isn't that safe. */
745 auth_identity = _dbus_auth_get_identity (transport->auth);
746 _dbus_assert (auth_identity != NULL);
748 connection = transport->connection;
749 windows_user_function = transport->windows_user_function;
750 windows_user_data = transport->unix_user_data;
751 windows_sid = _dbus_strdup (_dbus_credentials_get_windows_sid (auth_identity));
753 if (windows_sid == NULL)
759 _dbus_verbose ("unlock\n");
760 _dbus_connection_unlock (connection);
762 allow = (* windows_user_function) (connection,
766 _dbus_verbose ("lock post windows user function\n");
767 _dbus_connection_lock (connection);
771 _dbus_verbose ("Client SID '%s' authorized\n", windows_sid);
775 _dbus_verbose ("Client SID '%s' was rejected, disconnecting\n",
776 _dbus_credentials_get_windows_sid (auth_identity));
777 _dbus_transport_disconnect (transport);
784 auth_via_default_rules (DBusTransport *transport)
786 DBusCredentials *auth_identity;
787 DBusCredentials *our_identity;
790 auth_identity = _dbus_auth_get_identity (transport->auth);
791 _dbus_assert (auth_identity != NULL);
793 /* By default, connection is allowed if the client is 1) root or 2)
794 * has the same UID as us or 3) anonymous is allowed.
797 our_identity = _dbus_credentials_new_from_current_process ();
798 if (our_identity == NULL)
804 if (transport->allow_anonymous ||
805 _dbus_credentials_get_unix_uid (auth_identity) == 0 ||
806 _dbus_credentials_same_user (our_identity,
809 if (_dbus_credentials_include(our_identity,DBUS_CREDENTIAL_WINDOWS_SID))
810 _dbus_verbose ("Client authorized as SID '%s'"
811 "matching our SID '%s'\n",
812 _dbus_credentials_get_windows_sid(auth_identity),
813 _dbus_credentials_get_windows_sid(our_identity));
815 _dbus_verbose ("Client authorized as UID "DBUS_UID_FORMAT
816 " matching our UID "DBUS_UID_FORMAT"\n",
817 _dbus_credentials_get_unix_uid(auth_identity),
818 _dbus_credentials_get_unix_uid(our_identity));
819 /* We have authenticated! */
824 if (_dbus_credentials_include(our_identity,DBUS_CREDENTIAL_WINDOWS_SID))
825 _dbus_verbose ("Client authorized as SID '%s'"
826 " but our SID is '%s', disconnecting\n",
827 (_dbus_credentials_get_windows_sid(auth_identity) ?
828 _dbus_credentials_get_windows_sid(auth_identity) : "<null>"),
829 (_dbus_credentials_get_windows_sid(our_identity) ?
830 _dbus_credentials_get_windows_sid(our_identity) : "<null>"));
832 _dbus_verbose ("Client authorized as UID "DBUS_UID_FORMAT
833 " but our UID is "DBUS_UID_FORMAT", disconnecting\n",
834 _dbus_credentials_get_unix_uid(auth_identity),
835 _dbus_credentials_get_unix_uid(our_identity));
836 _dbus_transport_disconnect (transport);
840 _dbus_credentials_unref (our_identity);
846 * Returns #TRUE if we have been authenticated. It will return #TRUE even if
847 * the transport is now disconnected, but was ever authenticated before
850 * This replaces the older _dbus_transport_get_is_authenticated() which
853 * @param transport the transport
854 * @returns whether we're authenticated
857 _dbus_transport_peek_is_authenticated (DBusTransport *transport)
859 return transport->authenticated;
863 * Returns #TRUE if we have been authenticated. It will return #TRUE even if
864 * the transport is now disconnected, but was ever authenticated before
867 * If we have not finished authenticating, but we have enough buffered input
868 * to finish the job, then this function will do so before it returns.
870 * This used to be called _dbus_transport_get_is_authenticated(), but that
871 * name seems inappropriate for a function with side-effects.
873 * @todo we drop connection->mutex when calling the unix_user_function,
874 * and windows_user_function, which may not be safe really.
876 * @param transport the transport
877 * @returns whether we're authenticated
880 _dbus_transport_try_to_authenticate (DBusTransport *transport)
882 if (transport->authenticated)
886 dbus_bool_t maybe_authenticated;
888 if (transport->disconnected)
891 /* paranoia ref since we call user callbacks sometimes */
892 _dbus_connection_ref_unlocked (transport->connection);
894 maybe_authenticated =
895 (!(transport->send_credentials_pending ||
896 transport->receive_credentials_pending));
898 if (maybe_authenticated)
900 switch (_dbus_auth_do_work (transport->auth))
902 case DBUS_AUTH_STATE_AUTHENTICATED:
903 /* leave as maybe_authenticated */
906 case DBUS_AUTH_STATE_WAITING_FOR_INPUT:
907 case DBUS_AUTH_STATE_WAITING_FOR_MEMORY:
908 case DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND:
909 case DBUS_AUTH_STATE_NEED_DISCONNECT:
910 maybe_authenticated = FALSE;
913 case DBUS_AUTH_STATE_INVALID:
915 _dbus_assert_not_reached ("invalid authentication state");
919 /* If we're the client, verify the GUID
921 if (maybe_authenticated && !transport->is_server)
923 const char *server_guid;
925 server_guid = _dbus_auth_get_guid_from_server (transport->auth);
926 _dbus_assert (server_guid != NULL);
928 if (transport->expected_guid &&
929 strcmp (transport->expected_guid, server_guid) != 0)
931 _dbus_verbose ("Client expected GUID '%s' and we got '%s' from the server\n",
932 transport->expected_guid, server_guid);
933 _dbus_transport_disconnect (transport);
934 _dbus_connection_unref_unlocked (transport->connection);
939 /* If we're the server, see if we want to allow this identity to proceed.
941 if (maybe_authenticated && transport->is_server)
944 DBusCredentials *auth_identity;
946 auth_identity = _dbus_auth_get_identity (transport->auth);
947 _dbus_assert (auth_identity != NULL);
949 /* If we have an auth'd user and a user function, delegate
950 * deciding whether auth credentials are good enough to the
951 * app; otherwise, use our default decision process.
953 if (transport->unix_user_function != NULL &&
954 _dbus_credentials_include (auth_identity, DBUS_CREDENTIAL_UNIX_USER_ID))
956 allow = auth_via_unix_user_function (transport);
958 else if (transport->windows_user_function != NULL &&
959 _dbus_credentials_include (auth_identity, DBUS_CREDENTIAL_WINDOWS_SID))
961 allow = auth_via_windows_user_function (transport);
965 allow = auth_via_default_rules (transport);
969 maybe_authenticated = FALSE;
972 transport->authenticated = maybe_authenticated;
974 _dbus_connection_unref_unlocked (transport->connection);
975 return maybe_authenticated;
980 * See dbus_connection_get_is_anonymous().
982 * @param transport the transport
983 * @returns #TRUE if not authenticated or authenticated as anonymous
986 _dbus_transport_get_is_anonymous (DBusTransport *transport)
988 DBusCredentials *auth_identity;
990 if (!transport->authenticated)
993 auth_identity = _dbus_auth_get_identity (transport->auth);
995 if (_dbus_credentials_are_anonymous (auth_identity))
1002 * Returns TRUE if the transport supports sending unix fds.
1004 * @param transport the transport
1005 * @returns #TRUE if TRUE it is possible to send unix fds across the transport.
1008 _dbus_transport_can_pass_unix_fd(DBusTransport *transport)
1010 return DBUS_TRANSPORT_CAN_SEND_UNIX_FD(transport);
1014 * Returns TRUE if the transport supports sending synchronous calls.
1016 * @param transport the transport
1017 * @returns #TRUE if TRUE it is possible to send sync call across the transport.
1020 _dbus_transport_can_send_sync_call (DBusTransport *transport)
1022 if (transport->send_sync_call_function)
1029 * Gets the address of a transport. It will be
1030 * #NULL for a server-side transport.
1032 * @param transport the transport
1033 * @returns transport's address
1036 _dbus_transport_get_address (DBusTransport *transport)
1038 return transport->address;
1042 * Gets the id of the server we are connected to (see
1043 * dbus_server_get_id()). Only works on client side.
1045 * @param transport the transport
1046 * @returns transport's server's id or #NULL if we are the server side
1049 _dbus_transport_get_server_id (DBusTransport *transport)
1051 if (transport->is_server)
1053 else if (transport->authenticated)
1054 return _dbus_auth_get_guid_from_server (transport->auth);
1056 return transport->expected_guid;
1060 * Handles a watch by reading data, writing data, or disconnecting
1061 * the transport, as appropriate for the given condition.
1063 * @param transport the transport.
1064 * @param watch the watch.
1065 * @param condition the current state of the watched file descriptor.
1066 * @returns #FALSE if not enough memory to fully handle the watch
1069 _dbus_transport_handle_watch (DBusTransport *transport,
1071 unsigned int condition)
1075 _dbus_assert (transport->vtable->handle_watch != NULL);
1077 if (transport->disconnected)
1080 if (dbus_watch_get_socket (watch) < 0)
1082 _dbus_warn_check_failed ("Tried to handle an invalidated watch; this watch should have been removed");
1086 _dbus_watch_sanitize_condition (watch, &condition);
1088 _dbus_transport_ref (transport);
1089 _dbus_watch_ref (watch);
1090 retval = (* transport->vtable->handle_watch) (transport, watch, condition);
1091 _dbus_watch_unref (watch);
1092 _dbus_transport_unref (transport);
1098 * Sets the connection using this transport. Allows the transport
1099 * to add watches to the connection, queue incoming messages,
1100 * and pull outgoing messages.
1102 * @param transport the transport.
1103 * @param connection the connection.
1104 * @returns #FALSE if not enough memory
1107 _dbus_transport_set_connection (DBusTransport *transport,
1108 DBusConnection *connection)
1110 _dbus_assert (transport->vtable->connection_set != NULL);
1111 _dbus_assert (transport->connection == NULL);
1113 transport->connection = connection;
1115 _dbus_transport_ref (transport);
1116 if (!(* transport->vtable->connection_set) (transport))
1117 transport->connection = NULL;
1118 _dbus_transport_unref (transport);
1120 return transport->connection != NULL;
1124 * Get the socket file descriptor, if any.
1126 * @param transport the transport
1127 * @param fd_p pointer to fill in with the descriptor
1128 * @returns #TRUE if a descriptor was available
1131 _dbus_transport_get_socket_fd (DBusTransport *transport,
1136 if (transport->vtable->get_socket_fd == NULL)
1139 if (transport->disconnected)
1142 _dbus_transport_ref (transport);
1144 retval = (* transport->vtable->get_socket_fd) (transport,
1147 _dbus_transport_unref (transport);
1153 * Performs a single poll()/select() on the transport's file
1154 * descriptors and then reads/writes data as appropriate,
1155 * queueing incoming messages and sending outgoing messages.
1156 * This is the backend for _dbus_connection_do_iteration().
1157 * See _dbus_connection_do_iteration() for full details.
1159 * @param transport the transport.
1160 * @param flags indicates whether to read or write, and whether to block.
1161 * @param timeout_milliseconds if blocking, timeout or -1 for no timeout.
1164 _dbus_transport_do_iteration (DBusTransport *transport,
1166 int timeout_milliseconds)
1168 _dbus_assert (transport->vtable->do_iteration != NULL);
1170 _dbus_verbose ("Transport iteration flags 0x%x timeout %d connected = %d\n",
1171 flags, timeout_milliseconds, !transport->disconnected);
1173 if ((flags & (DBUS_ITERATION_DO_WRITING |
1174 DBUS_ITERATION_DO_READING)) == 0)
1175 return; /* Nothing to do */
1177 if (transport->disconnected)
1180 _dbus_transport_ref (transport);
1181 (* transport->vtable->do_iteration) (transport, flags,
1182 timeout_milliseconds);
1183 _dbus_transport_unref (transport);
1185 _dbus_verbose ("end\n");
1189 recover_unused_bytes (DBusTransport *transport)
1191 if (_dbus_auth_needs_decoding (transport->auth))
1193 DBusString plaintext;
1194 const DBusString *encoded;
1198 if (!_dbus_string_init (&plaintext))
1201 _dbus_auth_get_unused_bytes (transport->auth,
1204 if (!_dbus_auth_decode_data (transport->auth,
1205 encoded, &plaintext))
1207 _dbus_string_free (&plaintext);
1211 _dbus_message_loader_get_buffer (transport->loader,
1216 orig_len = _dbus_string_get_length (buffer);
1218 if (!_dbus_string_move (&plaintext, 0, buffer,
1221 _dbus_string_free (&plaintext);
1225 _dbus_verbose (" %d unused bytes sent to message loader\n",
1226 _dbus_string_get_length (buffer) -
1229 _dbus_message_loader_return_buffer (transport->loader,
1232 _dbus_auth_delete_unused_bytes (transport->auth);
1234 _dbus_string_free (&plaintext);
1238 const DBusString *bytes;
1240 #ifdef DBUS_ENABLE_VERBOSE_MODE
1243 dbus_bool_t succeeded;
1245 _dbus_message_loader_get_buffer (transport->loader,
1250 #ifdef DBUS_ENABLE_VERBOSE_MODE
1251 orig_len = _dbus_string_get_length (buffer);
1254 _dbus_auth_get_unused_bytes (transport->auth,
1258 if (!_dbus_string_copy (bytes, 0, buffer, _dbus_string_get_length (buffer)))
1261 _dbus_verbose (" %d unused bytes sent to message loader\n",
1262 _dbus_string_get_length (buffer) -
1265 _dbus_message_loader_return_buffer (transport->loader,
1269 _dbus_auth_delete_unused_bytes (transport->auth);
1277 _dbus_verbose ("Not enough memory to transfer unused bytes from auth conversation\n");
1282 * Reports our current dispatch status (whether there's buffered
1283 * data to be queued as messages, or not, or we need memory).
1285 * @param transport the transport
1286 * @returns current status
1289 _dbus_transport_get_dispatch_status (DBusTransport *transport)
1291 if (_dbus_counter_get_size_value (transport->live_messages) >= transport->max_live_messages_size ||
1292 _dbus_counter_get_unix_fd_value (transport->live_messages) >= transport->max_live_messages_unix_fds)
1293 return DBUS_DISPATCH_COMPLETE; /* complete for now */
1295 if (!_dbus_transport_try_to_authenticate (transport))
1297 if (_dbus_auth_do_work (transport->auth) ==
1298 DBUS_AUTH_STATE_WAITING_FOR_MEMORY)
1299 return DBUS_DISPATCH_NEED_MEMORY;
1300 else if (!_dbus_transport_try_to_authenticate (transport))
1301 return DBUS_DISPATCH_COMPLETE;
1304 if (!transport->unused_bytes_recovered &&
1305 !recover_unused_bytes (transport))
1306 return DBUS_DISPATCH_NEED_MEMORY;
1308 transport->unused_bytes_recovered = TRUE;
1310 if (!_dbus_message_loader_queue_messages (transport->loader))
1311 return DBUS_DISPATCH_NEED_MEMORY;
1313 if (_dbus_message_loader_peek_message (transport->loader) != NULL)
1314 return DBUS_DISPATCH_DATA_REMAINS;
1316 return DBUS_DISPATCH_COMPLETE;
1320 * Processes data we've read while handling a watch, potentially
1321 * converting some of it to messages and queueing those messages on
1324 * @param transport the transport
1325 * @returns #TRUE if we had enough memory to queue all messages
1328 _dbus_transport_queue_messages (DBusTransport *transport)
1330 DBusDispatchStatus status;
1333 _dbus_verbose ("enter\n");
1336 /* Queue any messages */
1337 while ((status = _dbus_transport_get_dispatch_status (transport)) == DBUS_DISPATCH_DATA_REMAINS)
1339 DBusMessage *message;
1342 link = _dbus_message_loader_pop_message_link (transport->loader);
1343 _dbus_assert (link != NULL);
1345 message = link->data;
1347 _dbus_verbose ("queueing received message %p\n", message);
1349 if (!_dbus_message_add_counter (message, transport->live_messages))
1351 _dbus_message_loader_putback_message_link (transport->loader,
1353 status = DBUS_DISPATCH_NEED_MEMORY;
1358 /* We didn't call the notify function when we added the counter, so
1359 * catch up now. Since we have the connection's lock, it's desirable
1360 * that we bypass the notify function and call this virtual method
1362 if (transport->vtable->live_messages_changed)
1363 (* transport->vtable->live_messages_changed) (transport);
1365 /* pass ownership of link and message ref to connection */
1366 _dbus_connection_queue_received_message_link (transport->connection,
1371 if (_dbus_message_loader_get_is_corrupted (transport->loader))
1373 _dbus_verbose ("Corrupted message stream, disconnecting\n");
1374 _dbus_transport_disconnect (transport);
1377 return status != DBUS_DISPATCH_NEED_MEMORY;
1381 * See dbus_connection_set_max_message_size().
1383 * @param transport the transport
1384 * @param size the max size of a single message
1387 _dbus_transport_set_max_message_size (DBusTransport *transport,
1390 _dbus_message_loader_set_max_message_size (transport->loader, size);
1394 * See dbus_connection_set_max_message_unix_fds().
1396 * @param transport the transport
1397 * @param n the max number of unix fds of a single message
1400 _dbus_transport_set_max_message_unix_fds (DBusTransport *transport,
1403 _dbus_message_loader_set_max_message_unix_fds (transport->loader, n);
1407 * See dbus_connection_get_max_message_size().
1409 * @param transport the transport
1410 * @returns max message size
1413 _dbus_transport_get_max_message_size (DBusTransport *transport)
1415 return _dbus_message_loader_get_max_message_size (transport->loader);
1419 * See dbus_connection_get_max_message_unix_fds().
1421 * @param transport the transport
1422 * @returns max message unix fds
1425 _dbus_transport_get_max_message_unix_fds (DBusTransport *transport)
1427 return _dbus_message_loader_get_max_message_unix_fds (transport->loader);
1431 * See dbus_connection_set_max_received_size().
1433 * @param transport the transport
1434 * @param size the max size of all incoming messages
1437 _dbus_transport_set_max_received_size (DBusTransport *transport,
1440 transport->max_live_messages_size = size;
1441 _dbus_counter_set_notify (transport->live_messages,
1442 transport->max_live_messages_size,
1443 transport->max_live_messages_unix_fds,
1444 live_messages_notify,
1449 * See dbus_connection_set_max_received_unix_fds().
1451 * @param transport the transport
1452 * @param n the max unix fds of all incoming messages
1455 _dbus_transport_set_max_received_unix_fds (DBusTransport *transport,
1458 transport->max_live_messages_unix_fds = n;
1459 _dbus_counter_set_notify (transport->live_messages,
1460 transport->max_live_messages_size,
1461 transport->max_live_messages_unix_fds,
1462 live_messages_notify,
1467 * See dbus_connection_get_max_received_size().
1469 * @param transport the transport
1470 * @returns max bytes for all live messages
1473 _dbus_transport_get_max_received_size (DBusTransport *transport)
1475 return transport->max_live_messages_size;
1479 * See dbus_connection_set_max_received_unix_fds().
1481 * @param transport the transport
1482 * @returns max unix fds for all live messages
1485 _dbus_transport_get_max_received_unix_fds (DBusTransport *transport)
1487 return transport->max_live_messages_unix_fds;
1491 * Sets a function used to get UNIX user ID of the connection.
1492 * See dbus_connection_get_unix_user().
1494 * @param transport the transport
1495 * @param function the getter function
1498 _dbus_transport_set_get_unix_user_function (DBusTransport *transport,
1499 DBusTransportGetUnixUserFunction function)
1501 transport->get_unix_user_function = function;
1505 * Sets a function used to get process ID of the connection.
1506 * See dbus_connection_get_unix_process_id().
1508 * @param transport the transport
1509 * @param function the getter function
1512 _dbus_transport_set_get_unix_process_id_function (DBusTransport *transport,
1513 DBusTransportGetUnixPIDFunction function)
1515 transport->get_unix_process_id_function = function;
1519 * Sets a function used to send synchronous calls.
1521 * @param transport the transport
1522 * @param function the getter function
1525 _dbus_transport_set_send_sync_call_function (DBusTransport *transport,
1526 DBusTransportSendSyncCallFunction function)
1528 transport->send_sync_call_function = function;
1532 * Sets a function used to assure that messages have correct protocol version
1534 * @param transport the transport
1535 * @param function the getter function
1538 _dbus_transport_set_assure_protocol_function (DBusTransport *transport,
1539 DBusTransportAssureProtocolFunction function,
1542 transport->assure_protocol_function = function;
1543 transport->protocol = protocol;
1547 * See dbus_connection_get_unix_user().
1549 * @param transport the transport
1550 * @param uid return location for the user ID
1551 * @returns #TRUE if uid is filled in with a valid user ID
1554 _dbus_transport_get_unix_user (DBusTransport *transport,
1557 if (transport->get_unix_user_function == NULL)
1559 return (transport->get_unix_user_function) (transport, uid);
1563 * See dbus_connection_get_unix_process_id().
1565 * @param transport the transport
1566 * @param pid return location for the process ID
1567 * @returns #TRUE if uid is filled in with a valid process ID
1570 _dbus_transport_get_unix_process_id (DBusTransport *transport,
1573 if (transport->get_unix_process_id_function == NULL)
1575 return (transport->get_unix_process_id_function) (transport, pid);
1579 * See dbus_connection_get_adt_audit_session_data().
1581 * @param transport the transport
1582 * @param data return location for the ADT audit data
1583 * @param data_size return length of audit data
1584 * @returns #TRUE if audit data is filled in with a valid ucred
1587 _dbus_transport_get_adt_audit_session_data (DBusTransport *transport,
1591 DBusCredentials *auth_identity;
1596 if (!transport->authenticated)
1599 auth_identity = _dbus_auth_get_identity (transport->auth);
1601 if (_dbus_credentials_include (auth_identity,
1602 DBUS_CREDENTIAL_ADT_AUDIT_DATA_ID))
1604 *data = (void *) _dbus_credentials_get_adt_audit_data (auth_identity);
1605 *data_size = _dbus_credentials_get_adt_audit_data_size (auth_identity);
1613 * See dbus_connection_set_unix_user_function().
1615 * @param transport the transport
1616 * @param function the predicate
1617 * @param data data to pass to the predicate
1618 * @param free_data_function function to free the data
1619 * @param old_data the old user data to be freed
1620 * @param old_free_data_function old free data function to free it with
1623 _dbus_transport_set_unix_user_function (DBusTransport *transport,
1624 DBusAllowUnixUserFunction function,
1626 DBusFreeFunction free_data_function,
1628 DBusFreeFunction *old_free_data_function)
1630 *old_data = transport->unix_user_data;
1631 *old_free_data_function = transport->free_unix_user_data;
1633 transport->unix_user_function = function;
1634 transport->unix_user_data = data;
1635 transport->free_unix_user_data = free_data_function;
1639 _dbus_transport_get_linux_security_label (DBusTransport *transport,
1642 DBusCredentials *auth_identity;
1646 if (!transport->authenticated)
1649 auth_identity = _dbus_auth_get_identity (transport->auth);
1651 if (_dbus_credentials_include (auth_identity,
1652 DBUS_CREDENTIAL_LINUX_SECURITY_LABEL))
1654 /* If no memory, we are supposed to return TRUE and set NULL */
1655 *label_p = _dbus_strdup (_dbus_credentials_get_linux_security_label (auth_identity));
1666 * See dbus_connection_get_windows_user().
1668 * @param transport the transport
1669 * @param windows_sid_p return location for the user ID
1670 * @returns #TRUE if user is available; the returned value may still be #NULL if no memory to copy it
1673 _dbus_transport_get_windows_user (DBusTransport *transport,
1674 char **windows_sid_p)
1676 DBusCredentials *auth_identity;
1678 *windows_sid_p = NULL;
1680 if (!transport->authenticated)
1683 auth_identity = _dbus_auth_get_identity (transport->auth);
1685 if (_dbus_credentials_include (auth_identity,
1686 DBUS_CREDENTIAL_WINDOWS_SID))
1688 /* If no memory, we are supposed to return TRUE and set NULL */
1689 *windows_sid_p = _dbus_strdup (_dbus_credentials_get_windows_sid (auth_identity));
1698 * See dbus_connection_set_windows_user_function().
1700 * @param transport the transport
1701 * @param function the predicate
1702 * @param data data to pass to the predicate
1703 * @param free_data_function function to free the data
1704 * @param old_data the old user data to be freed
1705 * @param old_free_data_function old free data function to free it with
1709 _dbus_transport_set_windows_user_function (DBusTransport *transport,
1710 DBusAllowWindowsUserFunction function,
1712 DBusFreeFunction free_data_function,
1714 DBusFreeFunction *old_free_data_function)
1716 *old_data = transport->windows_user_data;
1717 *old_free_data_function = transport->free_windows_user_data;
1719 transport->windows_user_function = function;
1720 transport->windows_user_data = data;
1721 transport->free_windows_user_data = free_data_function;
1725 * Sets the SASL authentication mechanisms supported by this transport.
1727 * @param transport the transport
1728 * @param mechanisms the #NULL-terminated array of mechanisms
1730 * @returns #FALSE if no memory
1733 _dbus_transport_set_auth_mechanisms (DBusTransport *transport,
1734 const char **mechanisms)
1736 return _dbus_auth_set_mechanisms (transport->auth, mechanisms);
1740 * See dbus_connection_set_allow_anonymous()
1742 * @param transport the transport
1743 * @param value #TRUE to allow anonymous connection
1746 _dbus_transport_set_allow_anonymous (DBusTransport *transport,
1749 transport->allow_anonymous = value != FALSE;
1753 * Return how many file descriptors are pending in the loader
1755 * @param transport the transport
1758 _dbus_transport_get_pending_fds_count (DBusTransport *transport)
1760 return _dbus_message_loader_get_pending_fds_count (transport->loader);
1764 * Register a function to be called whenever the number of pending file
1765 * descriptors in the loader change.
1767 * @param transport the transport
1768 * @param callback the callback
1771 _dbus_transport_set_pending_fds_function (DBusTransport *transport,
1772 void (* callback) (void *),
1775 _dbus_message_loader_set_pending_fds_function (transport->loader,
1779 #ifdef DBUS_ENABLE_STATS
1781 _dbus_transport_get_stats (DBusTransport *transport,
1782 dbus_uint32_t *queue_bytes,
1783 dbus_uint32_t *queue_fds,
1784 dbus_uint32_t *peak_queue_bytes,
1785 dbus_uint32_t *peak_queue_fds)
1787 if (queue_bytes != NULL)
1788 *queue_bytes = _dbus_counter_get_size_value (transport->live_messages);
1790 if (queue_fds != NULL)
1791 *queue_fds = _dbus_counter_get_unix_fd_value (transport->live_messages);
1793 if (peak_queue_bytes != NULL)
1794 *peak_queue_bytes = _dbus_counter_get_peak_size_value (transport->live_messages);
1796 if (peak_queue_fds != NULL)
1797 *peak_queue_fds = _dbus_counter_get_peak_unix_fd_value (transport->live_messages);
1799 #endif /* DBUS_ENABLE_STATS */