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.
6 * Licensed under the Academic Free License version 2.1
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "dbus-transport-protected.h"
25 #include "dbus-transport-unix.h"
26 #include "dbus-transport-socket.h"
27 #include "dbus-connection-internal.h"
28 #include "dbus-watch.h"
29 #include "dbus-auth.h"
30 #include "dbus-address.h"
31 #include "dbus-credentials.h"
32 #ifdef DBUS_BUILD_TESTS
33 #include "dbus-server-debug-pipe.h"
37 * @defgroup DBusTransport DBusTransport object
38 * @ingroup DBusInternals
39 * @brief "Backend" for a DBusConnection.
41 * Types and functions related to DBusTransport. A transport is an
42 * abstraction that can send and receive data via various kinds of
43 * network connections or other IPC mechanisms.
49 * @typedef DBusTransport
51 * Opaque object representing a way message stream.
52 * DBusTransport abstracts various kinds of actual
53 * transport mechanism, such as different network protocols,
54 * or encryption schemes.
58 live_messages_size_notify (DBusCounter *counter,
61 DBusTransport *transport = user_data;
63 _dbus_transport_ref (transport);
66 _dbus_verbose ("Counter value is now %d\n",
67 (int) _dbus_counter_get_value (counter));
70 /* disable or re-enable the read watch for the transport if
73 if (transport->vtable->live_messages_changed)
74 (* transport->vtable->live_messages_changed) (transport);
76 _dbus_transport_unref (transport);
80 * Initializes the base class members of DBusTransport. Chained up to
81 * by subclasses in their constructor. The server GUID is the
82 * globally unique ID for the server creating this connection
83 * and will be #NULL for the client side of a connection. The GUID
86 * @param transport the transport being created.
87 * @param vtable the subclass vtable.
88 * @param server_guid non-#NULL if this transport is on the server side of a connection
89 * @param address the address of the transport
90 * @returns #TRUE on success.
93 _dbus_transport_init_base (DBusTransport *transport,
94 const DBusTransportVTable *vtable,
95 const DBusString *server_guid,
96 const DBusString *address)
98 DBusMessageLoader *loader;
100 DBusCounter *counter;
102 DBusCredentials *creds;
104 loader = _dbus_message_loader_new ();
109 auth = _dbus_auth_server_new (server_guid);
111 auth = _dbus_auth_client_new ();
114 _dbus_message_loader_unref (loader);
118 counter = _dbus_counter_new ();
121 _dbus_auth_unref (auth);
122 _dbus_message_loader_unref (loader);
126 creds = _dbus_credentials_new ();
129 _dbus_counter_unref (counter);
130 _dbus_auth_unref (auth);
131 _dbus_message_loader_unref (loader);
137 _dbus_assert (address == NULL);
142 _dbus_assert (address != NULL);
144 if (!_dbus_string_copy_data (address, &address_copy))
146 _dbus_credentials_unref (creds);
147 _dbus_counter_unref (counter);
148 _dbus_auth_unref (auth);
149 _dbus_message_loader_unref (loader);
154 transport->refcount = 1;
155 transport->vtable = vtable;
156 transport->loader = loader;
157 transport->auth = auth;
158 transport->live_messages_size = counter;
159 transport->authenticated = FALSE;
160 transport->disconnected = FALSE;
161 transport->is_server = (server_guid != NULL);
162 transport->send_credentials_pending = !transport->is_server;
163 transport->receive_credentials_pending = transport->is_server;
164 transport->address = address_copy;
166 transport->unix_user_function = NULL;
167 transport->unix_user_data = NULL;
168 transport->free_unix_user_data = NULL;
170 transport->windows_user_function = NULL;
171 transport->windows_user_data = NULL;
172 transport->free_windows_user_data = NULL;
174 transport->expected_guid = NULL;
176 /* Try to default to something that won't totally hose the system,
177 * but doesn't impose too much of a limitation.
179 transport->max_live_messages_size = _DBUS_ONE_MEGABYTE * 63;
181 /* credentials read from socket if any */
182 transport->credentials = creds;
184 _dbus_counter_set_notify (transport->live_messages_size,
185 transport->max_live_messages_size,
186 live_messages_size_notify,
189 if (transport->address)
190 _dbus_verbose ("Initialized transport on address %s\n", transport->address);
196 * Finalizes base class members of DBusTransport.
197 * Chained up to from subclass finalizers.
199 * @param transport the transport.
202 _dbus_transport_finalize_base (DBusTransport *transport)
204 if (!transport->disconnected)
205 _dbus_transport_disconnect (transport);
207 if (transport->free_unix_user_data != NULL)
208 (* transport->free_unix_user_data) (transport->unix_user_data);
210 if (transport->free_windows_user_data != NULL)
211 (* transport->free_windows_user_data) (transport->windows_user_data);
213 _dbus_message_loader_unref (transport->loader);
214 _dbus_auth_unref (transport->auth);
215 _dbus_counter_set_notify (transport->live_messages_size,
217 _dbus_counter_unref (transport->live_messages_size);
218 dbus_free (transport->address);
219 dbus_free (transport->expected_guid);
220 if (transport->credentials)
221 _dbus_credentials_unref (transport->credentials);
226 * Verifies if a given D-Bus address is a valid address
227 * by attempting to connect to it. If it is, returns the
228 * opened DBusTransport object. If it isn't, returns #NULL
231 * @param error address where an error can be returned.
232 * @returns a new transport, or #NULL on failure.
234 static DBusTransport*
235 check_address (const char *address, DBusError *error)
237 DBusAddressEntry **entries;
238 DBusTransport *transport = NULL;
241 _dbus_assert (address != NULL);
242 _dbus_assert (*address != '\0');
244 if (!dbus_parse_address (address, &entries, &len, error))
245 return FALSE; /* not a valid address */
247 for (i = 0; i < len; i++)
249 transport = _dbus_transport_open (entries[i], error);
250 if (transport != NULL)
254 dbus_address_entries_free (entries);
259 * Creates a new transport for the "autostart" method.
260 * This creates a client-side of a transport.
262 * @param error address where an error can be returned.
263 * @returns a new transport, or #NULL on failure.
265 static DBusTransport*
266 _dbus_transport_new_for_autolaunch (DBusError *error)
269 DBusTransport *result = NULL;
271 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
273 if (!_dbus_string_init (&address))
275 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
279 if (!_dbus_get_autolaunch_address (&address, error))
281 _DBUS_ASSERT_ERROR_IS_SET (error);
285 result = check_address (_dbus_string_get_const_data (&address), error);
287 _DBUS_ASSERT_ERROR_IS_SET (error);
289 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
292 _dbus_string_free (&address);
296 static DBusTransportOpenResult
297 _dbus_transport_open_autolaunch (DBusAddressEntry *entry,
298 DBusTransport **transport_p,
303 method = dbus_address_entry_get_method (entry);
304 _dbus_assert (method != NULL);
306 if (strcmp (method, "autolaunch") == 0)
308 *transport_p = _dbus_transport_new_for_autolaunch (error);
310 if (*transport_p == NULL)
312 _DBUS_ASSERT_ERROR_IS_SET (error);
313 return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
317 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
318 return DBUS_TRANSPORT_OPEN_OK;
323 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
324 return DBUS_TRANSPORT_OPEN_NOT_HANDLED;
328 static const struct {
329 DBusTransportOpenResult (* func) (DBusAddressEntry *entry,
330 DBusTransport **transport_p,
333 { _dbus_transport_open_socket },
334 { _dbus_transport_open_platform_specific },
335 { _dbus_transport_open_autolaunch }
336 #ifdef DBUS_BUILD_TESTS
337 , { _dbus_transport_open_debug_pipe }
342 * Try to open a new transport for the given address entry. (This
343 * opens a client-side-of-the-connection transport.)
345 * @param entry the address entry
346 * @param error location to store reason for failure.
347 * @returns new transport of #NULL on failure.
350 _dbus_transport_open (DBusAddressEntry *entry,
353 DBusTransport *transport;
354 const char *expected_guid_orig;
357 DBusError tmp_error = DBUS_ERROR_INIT;
359 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
362 expected_guid_orig = dbus_address_entry_get_value (entry, "guid");
363 expected_guid = _dbus_strdup (expected_guid_orig);
365 if (expected_guid_orig != NULL && expected_guid == NULL)
367 _DBUS_SET_OOM (error);
371 for (i = 0; i < (int) _DBUS_N_ELEMENTS (open_funcs); ++i)
373 DBusTransportOpenResult result;
375 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
376 result = (* open_funcs[i].func) (entry, &transport, &tmp_error);
380 case DBUS_TRANSPORT_OPEN_OK:
381 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
384 case DBUS_TRANSPORT_OPEN_NOT_HANDLED:
385 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
386 /* keep going through the loop of open funcs */
388 case DBUS_TRANSPORT_OPEN_BAD_ADDRESS:
389 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
392 case DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT:
393 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
401 if (transport == NULL)
403 if (!dbus_error_is_set (&tmp_error))
404 _dbus_set_bad_address (&tmp_error,
406 "Unknown address type (examples of valid types are \"tcp\" and on UNIX \"unix\")");
408 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
409 dbus_move_error(&tmp_error, error);
410 dbus_free (expected_guid);
414 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
415 transport->expected_guid = expected_guid;
422 * Increments the reference count for the transport.
424 * @param transport the transport.
425 * @returns the transport.
428 _dbus_transport_ref (DBusTransport *transport)
430 _dbus_assert (transport->refcount > 0);
432 transport->refcount += 1;
438 * Decrements the reference count for the transport.
439 * Disconnects and finalizes the transport if
440 * the reference count reaches zero.
442 * @param transport the transport.
445 _dbus_transport_unref (DBusTransport *transport)
447 _dbus_assert (transport != NULL);
448 _dbus_assert (transport->refcount > 0);
450 transport->refcount -= 1;
451 if (transport->refcount == 0)
453 _dbus_verbose ("%s: finalizing\n", _DBUS_FUNCTION_NAME);
455 _dbus_assert (transport->vtable->finalize != NULL);
457 (* transport->vtable->finalize) (transport);
462 * Closes our end of the connection to a remote application. Further
463 * attempts to use this transport will fail. Only the first call to
464 * _dbus_transport_disconnect() will have an effect.
466 * @param transport the transport.
470 _dbus_transport_disconnect (DBusTransport *transport)
472 _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
474 _dbus_assert (transport->vtable->disconnect != NULL);
476 if (transport->disconnected)
479 (* transport->vtable->disconnect) (transport);
481 transport->disconnected = TRUE;
483 _dbus_verbose ("%s end\n", _DBUS_FUNCTION_NAME);
487 * Returns #TRUE if the transport has not been disconnected.
488 * Disconnection can result from _dbus_transport_disconnect()
489 * or because the server drops its end of the connection.
491 * @param transport the transport.
492 * @returns whether we're connected
495 _dbus_transport_get_is_connected (DBusTransport *transport)
497 return !transport->disconnected;
501 auth_via_unix_user_function (DBusTransport *transport)
503 DBusCredentials *auth_identity;
505 DBusConnection *connection;
506 DBusAllowUnixUserFunction unix_user_function;
507 void *unix_user_data;
510 /* Dropping the lock here probably isn't that safe. */
512 auth_identity = _dbus_auth_get_identity (transport->auth);
513 _dbus_assert (auth_identity != NULL);
515 connection = transport->connection;
516 unix_user_function = transport->unix_user_function;
517 unix_user_data = transport->unix_user_data;
518 uid = _dbus_credentials_get_unix_uid (auth_identity);
520 _dbus_verbose ("unlock %s\n", _DBUS_FUNCTION_NAME);
521 _dbus_connection_unlock (connection);
523 allow = (* unix_user_function) (connection,
527 _dbus_verbose ("lock %s post unix user function\n", _DBUS_FUNCTION_NAME);
528 _dbus_connection_lock (connection);
532 _dbus_verbose ("Client UID "DBUS_UID_FORMAT" authorized\n", uid);
536 _dbus_verbose ("Client UID "DBUS_UID_FORMAT
537 " was rejected, disconnecting\n",
538 _dbus_credentials_get_unix_uid (auth_identity));
539 _dbus_transport_disconnect (transport);
546 auth_via_windows_user_function (DBusTransport *transport)
548 DBusCredentials *auth_identity;
550 DBusConnection *connection;
551 DBusAllowWindowsUserFunction windows_user_function;
552 void *windows_user_data;
555 /* Dropping the lock here probably isn't that safe. */
557 auth_identity = _dbus_auth_get_identity (transport->auth);
558 _dbus_assert (auth_identity != NULL);
560 connection = transport->connection;
561 windows_user_function = transport->windows_user_function;
562 windows_user_data = transport->unix_user_data;
563 windows_sid = _dbus_strdup (_dbus_credentials_get_windows_sid (auth_identity));
565 if (windows_sid == NULL)
571 _dbus_verbose ("unlock %s\n", _DBUS_FUNCTION_NAME);
572 _dbus_connection_unlock (connection);
574 allow = (* windows_user_function) (connection,
578 _dbus_verbose ("lock %s post windows user function\n", _DBUS_FUNCTION_NAME);
579 _dbus_connection_lock (connection);
583 _dbus_verbose ("Client SID '%s' authorized\n", windows_sid);
587 _dbus_verbose ("Client SID '%s' was rejected, disconnecting\n",
588 _dbus_credentials_get_windows_sid (auth_identity));
589 _dbus_transport_disconnect (transport);
596 auth_via_default_rules (DBusTransport *transport)
598 DBusCredentials *auth_identity;
599 DBusCredentials *our_identity;
602 auth_identity = _dbus_auth_get_identity (transport->auth);
603 _dbus_assert (auth_identity != NULL);
605 /* By default, connection is allowed if the client is 1) root or 2)
606 * has the same UID as us or 3) anonymous is allowed.
609 our_identity = _dbus_credentials_new_from_current_process ();
610 if (our_identity == NULL)
616 if (transport->allow_anonymous ||
617 _dbus_credentials_get_unix_uid (auth_identity) == 0 ||
618 _dbus_credentials_same_user (our_identity,
621 if (_dbus_credentials_include(our_identity,DBUS_CREDENTIAL_WINDOWS_SID))
622 _dbus_verbose ("Client authorized as SID '%s'"
623 "matching our SID '%s'\n",
624 _dbus_credentials_get_windows_sid(auth_identity),
625 _dbus_credentials_get_windows_sid(our_identity));
627 _dbus_verbose ("Client authorized as UID "DBUS_UID_FORMAT
628 " matching our UID "DBUS_UID_FORMAT"\n",
629 _dbus_credentials_get_unix_uid(auth_identity),
630 _dbus_credentials_get_unix_uid(our_identity));
631 /* We have authenticated! */
636 if (_dbus_credentials_include(our_identity,DBUS_CREDENTIAL_WINDOWS_SID))
637 _dbus_verbose ("Client authorized as SID '%s'"
638 " but our SID is '%s', disconnecting\n",
639 _dbus_credentials_get_windows_sid(our_identity),
640 _dbus_credentials_get_windows_sid(our_identity));
642 _dbus_verbose ("Client authorized as UID "DBUS_UID_FORMAT
643 " but our UID is "DBUS_UID_FORMAT", disconnecting\n",
644 _dbus_credentials_get_unix_uid(our_identity),
645 _dbus_credentials_get_unix_uid(our_identity));
646 _dbus_transport_disconnect (transport);
650 _dbus_credentials_unref (our_identity);
657 * Returns #TRUE if we have been authenticated. Will return #TRUE
658 * even if the transport is disconnected.
660 * @todo we drop connection->mutex when calling the unix_user_function,
661 * and windows_user_function, which may not be safe really.
663 * @param transport the transport
664 * @returns whether we're authenticated
667 _dbus_transport_get_is_authenticated (DBusTransport *transport)
669 if (transport->authenticated)
673 dbus_bool_t maybe_authenticated;
675 if (transport->disconnected)
678 /* paranoia ref since we call user callbacks sometimes */
679 _dbus_connection_ref_unlocked (transport->connection);
681 maybe_authenticated =
682 (!(transport->send_credentials_pending ||
683 transport->receive_credentials_pending));
685 if (maybe_authenticated)
687 switch (_dbus_auth_do_work (transport->auth))
689 case DBUS_AUTH_STATE_AUTHENTICATED:
690 /* leave as maybe_authenticated */
693 maybe_authenticated = FALSE;
697 /* If we're the client, verify the GUID
699 if (maybe_authenticated && !transport->is_server)
701 const char *server_guid;
703 server_guid = _dbus_auth_get_guid_from_server (transport->auth);
704 _dbus_assert (server_guid != NULL);
706 if (transport->expected_guid &&
707 strcmp (transport->expected_guid, server_guid) != 0)
709 _dbus_verbose ("Client expected GUID '%s' and we got '%s' from the server\n",
710 transport->expected_guid, server_guid);
711 _dbus_transport_disconnect (transport);
712 _dbus_connection_unref_unlocked (transport->connection);
716 if (transport->expected_guid == NULL)
718 transport->expected_guid = _dbus_strdup (server_guid);
720 if (transport->expected_guid == NULL)
722 _dbus_verbose ("No memory to complete auth in %s\n", _DBUS_FUNCTION_NAME);
728 /* If we're the server, see if we want to allow this identity to proceed.
730 if (maybe_authenticated && transport->is_server)
733 DBusCredentials *auth_identity;
735 auth_identity = _dbus_auth_get_identity (transport->auth);
736 _dbus_assert (auth_identity != NULL);
738 /* If we have an auth'd user and a user function, delegate
739 * deciding whether auth credentials are good enough to the
740 * app; otherwise, use our default decision process.
742 if (transport->unix_user_function != NULL &&
743 _dbus_credentials_include (auth_identity, DBUS_CREDENTIAL_UNIX_USER_ID))
745 allow = auth_via_unix_user_function (transport);
747 else if (transport->windows_user_function != NULL &&
748 _dbus_credentials_include (auth_identity, DBUS_CREDENTIAL_WINDOWS_SID))
750 allow = auth_via_windows_user_function (transport);
754 allow = auth_via_default_rules (transport);
758 maybe_authenticated = FALSE;
761 transport->authenticated = maybe_authenticated;
763 _dbus_connection_unref_unlocked (transport->connection);
764 return maybe_authenticated;
769 * See dbus_connection_get_is_anonymous().
771 * @param transport the transport
772 * @returns #TRUE if not authenticated or authenticated as anonymous
775 _dbus_transport_get_is_anonymous (DBusTransport *transport)
777 DBusCredentials *auth_identity;
779 if (!transport->authenticated)
782 auth_identity = _dbus_auth_get_identity (transport->auth);
784 if (_dbus_credentials_are_anonymous (auth_identity))
791 * Gets the address of a transport. It will be
792 * #NULL for a server-side transport.
794 * @param transport the transport
795 * @returns transport's address
798 _dbus_transport_get_address (DBusTransport *transport)
800 return transport->address;
804 * Gets the id of the server we are connected to (see
805 * dbus_server_get_id()). Only works on client side.
807 * @param transport the transport
808 * @returns transport's server's id or #NULL if we are the server side
811 _dbus_transport_get_server_id (DBusTransport *transport)
813 if (transport->is_server)
816 return transport->expected_guid;
820 * Handles a watch by reading data, writing data, or disconnecting
821 * the transport, as appropriate for the given condition.
823 * @param transport the transport.
824 * @param watch the watch.
825 * @param condition the current state of the watched file descriptor.
826 * @returns #FALSE if not enough memory to fully handle the watch
829 _dbus_transport_handle_watch (DBusTransport *transport,
831 unsigned int condition)
835 _dbus_assert (transport->vtable->handle_watch != NULL);
837 if (transport->disconnected)
840 if (dbus_watch_get_socket (watch) < 0)
842 _dbus_warn_check_failed ("Tried to handle an invalidated watch; this watch should have been removed\n");
846 _dbus_watch_sanitize_condition (watch, &condition);
848 _dbus_transport_ref (transport);
849 _dbus_watch_ref (watch);
850 retval = (* transport->vtable->handle_watch) (transport, watch, condition);
851 _dbus_watch_unref (watch);
852 _dbus_transport_unref (transport);
858 * Sets the connection using this transport. Allows the transport
859 * to add watches to the connection, queue incoming messages,
860 * and pull outgoing messages.
862 * @param transport the transport.
863 * @param connection the connection.
864 * @returns #FALSE if not enough memory
867 _dbus_transport_set_connection (DBusTransport *transport,
868 DBusConnection *connection)
870 _dbus_assert (transport->vtable->connection_set != NULL);
871 _dbus_assert (transport->connection == NULL);
873 transport->connection = connection;
875 _dbus_transport_ref (transport);
876 if (!(* transport->vtable->connection_set) (transport))
877 transport->connection = NULL;
878 _dbus_transport_unref (transport);
880 return transport->connection != NULL;
884 * Get the socket file descriptor, if any.
886 * @param transport the transport
887 * @param fd_p pointer to fill in with the descriptor
888 * @returns #TRUE if a descriptor was available
891 _dbus_transport_get_socket_fd (DBusTransport *transport,
896 if (transport->vtable->get_socket_fd == NULL)
899 if (transport->disconnected)
902 _dbus_transport_ref (transport);
904 retval = (* transport->vtable->get_socket_fd) (transport,
907 _dbus_transport_unref (transport);
913 * Performs a single poll()/select() on the transport's file
914 * descriptors and then reads/writes data as appropriate,
915 * queueing incoming messages and sending outgoing messages.
916 * This is the backend for _dbus_connection_do_iteration().
917 * See _dbus_connection_do_iteration() for full details.
919 * @param transport the transport.
920 * @param flags indicates whether to read or write, and whether to block.
921 * @param timeout_milliseconds if blocking, timeout or -1 for no timeout.
924 _dbus_transport_do_iteration (DBusTransport *transport,
926 int timeout_milliseconds)
928 _dbus_assert (transport->vtable->do_iteration != NULL);
930 _dbus_verbose ("Transport iteration flags 0x%x timeout %d connected = %d\n",
931 flags, timeout_milliseconds, !transport->disconnected);
933 if ((flags & (DBUS_ITERATION_DO_WRITING |
934 DBUS_ITERATION_DO_READING)) == 0)
935 return; /* Nothing to do */
937 if (transport->disconnected)
940 _dbus_transport_ref (transport);
941 (* transport->vtable->do_iteration) (transport, flags,
942 timeout_milliseconds);
943 _dbus_transport_unref (transport);
945 _dbus_verbose ("%s end\n", _DBUS_FUNCTION_NAME);
949 recover_unused_bytes (DBusTransport *transport)
951 if (_dbus_auth_needs_decoding (transport->auth))
953 DBusString plaintext;
954 const DBusString *encoded;
958 if (!_dbus_string_init (&plaintext))
961 _dbus_auth_get_unused_bytes (transport->auth,
964 if (!_dbus_auth_decode_data (transport->auth,
965 encoded, &plaintext))
967 _dbus_string_free (&plaintext);
971 _dbus_message_loader_get_buffer (transport->loader,
974 orig_len = _dbus_string_get_length (buffer);
976 if (!_dbus_string_move (&plaintext, 0, buffer,
979 _dbus_string_free (&plaintext);
983 _dbus_verbose (" %d unused bytes sent to message loader\n",
984 _dbus_string_get_length (buffer) -
987 _dbus_message_loader_return_buffer (transport->loader,
989 _dbus_string_get_length (buffer) -
992 _dbus_auth_delete_unused_bytes (transport->auth);
994 _dbus_string_free (&plaintext);
998 const DBusString *bytes;
1001 dbus_bool_t succeeded;
1003 _dbus_message_loader_get_buffer (transport->loader,
1006 orig_len = _dbus_string_get_length (buffer);
1008 _dbus_auth_get_unused_bytes (transport->auth,
1012 if (!_dbus_string_copy (bytes, 0, buffer, _dbus_string_get_length (buffer)))
1015 _dbus_verbose (" %d unused bytes sent to message loader\n",
1016 _dbus_string_get_length (buffer) -
1019 _dbus_message_loader_return_buffer (transport->loader,
1021 _dbus_string_get_length (buffer) -
1025 _dbus_auth_delete_unused_bytes (transport->auth);
1033 _dbus_verbose ("Not enough memory to transfer unused bytes from auth conversation\n");
1038 * Reports our current dispatch status (whether there's buffered
1039 * data to be queued as messages, or not, or we need memory).
1041 * @param transport the transport
1042 * @returns current status
1045 _dbus_transport_get_dispatch_status (DBusTransport *transport)
1047 if (_dbus_counter_get_value (transport->live_messages_size) >= transport->max_live_messages_size)
1048 return DBUS_DISPATCH_COMPLETE; /* complete for now */
1050 if (!_dbus_transport_get_is_authenticated (transport))
1052 if (_dbus_auth_do_work (transport->auth) ==
1053 DBUS_AUTH_STATE_WAITING_FOR_MEMORY)
1054 return DBUS_DISPATCH_NEED_MEMORY;
1055 else if (!_dbus_transport_get_is_authenticated (transport))
1056 return DBUS_DISPATCH_COMPLETE;
1059 if (!transport->unused_bytes_recovered &&
1060 !recover_unused_bytes (transport))
1061 return DBUS_DISPATCH_NEED_MEMORY;
1063 transport->unused_bytes_recovered = TRUE;
1065 if (!_dbus_message_loader_queue_messages (transport->loader))
1066 return DBUS_DISPATCH_NEED_MEMORY;
1068 if (_dbus_message_loader_peek_message (transport->loader) != NULL)
1069 return DBUS_DISPATCH_DATA_REMAINS;
1071 return DBUS_DISPATCH_COMPLETE;
1075 * Processes data we've read while handling a watch, potentially
1076 * converting some of it to messages and queueing those messages on
1079 * @param transport the transport
1080 * @returns #TRUE if we had enough memory to queue all messages
1083 _dbus_transport_queue_messages (DBusTransport *transport)
1085 DBusDispatchStatus status;
1088 _dbus_verbose ("_dbus_transport_queue_messages()\n");
1091 /* Queue any messages */
1092 while ((status = _dbus_transport_get_dispatch_status (transport)) == DBUS_DISPATCH_DATA_REMAINS)
1094 DBusMessage *message;
1097 link = _dbus_message_loader_pop_message_link (transport->loader);
1098 _dbus_assert (link != NULL);
1100 message = link->data;
1102 _dbus_verbose ("queueing received message %p\n", message);
1104 if (!_dbus_message_add_size_counter (message, transport->live_messages_size))
1106 _dbus_message_loader_putback_message_link (transport->loader,
1108 status = DBUS_DISPATCH_NEED_MEMORY;
1113 /* pass ownership of link and message ref to connection */
1114 _dbus_connection_queue_received_message_link (transport->connection,
1119 if (_dbus_message_loader_get_is_corrupted (transport->loader))
1121 _dbus_verbose ("Corrupted message stream, disconnecting\n");
1122 _dbus_transport_disconnect (transport);
1125 return status != DBUS_DISPATCH_NEED_MEMORY;
1129 * See dbus_connection_set_max_message_size().
1131 * @param transport the transport
1132 * @param size the max size of a single message
1135 _dbus_transport_set_max_message_size (DBusTransport *transport,
1138 _dbus_message_loader_set_max_message_size (transport->loader, size);
1142 * See dbus_connection_get_max_message_size().
1144 * @param transport the transport
1145 * @returns max message size
1148 _dbus_transport_get_max_message_size (DBusTransport *transport)
1150 return _dbus_message_loader_get_max_message_size (transport->loader);
1154 * See dbus_connection_set_max_received_size().
1156 * @param transport the transport
1157 * @param size the max size of all incoming messages
1160 _dbus_transport_set_max_received_size (DBusTransport *transport,
1163 transport->max_live_messages_size = size;
1164 _dbus_counter_set_notify (transport->live_messages_size,
1165 transport->max_live_messages_size,
1166 live_messages_size_notify,
1172 * See dbus_connection_get_max_received_size().
1174 * @param transport the transport
1175 * @returns max bytes for all live messages
1178 _dbus_transport_get_max_received_size (DBusTransport *transport)
1180 return transport->max_live_messages_size;
1184 * See dbus_connection_get_unix_user().
1186 * @param transport the transport
1187 * @param uid return location for the user ID
1188 * @returns #TRUE if uid is filled in with a valid user ID
1191 _dbus_transport_get_unix_user (DBusTransport *transport,
1194 DBusCredentials *auth_identity;
1196 *uid = _DBUS_INT32_MAX; /* better than some root or system user in
1197 * case of bugs in the caller. Caller should
1198 * never use this value on purpose, however.
1201 if (!transport->authenticated)
1204 auth_identity = _dbus_auth_get_identity (transport->auth);
1206 if (_dbus_credentials_include (auth_identity,
1207 DBUS_CREDENTIAL_UNIX_USER_ID))
1209 *uid = _dbus_credentials_get_unix_uid (auth_identity);
1217 * See dbus_connection_get_unix_process_id().
1219 * @param transport the transport
1220 * @param pid return location for the process ID
1221 * @returns #TRUE if uid is filled in with a valid process ID
1224 _dbus_transport_get_unix_process_id (DBusTransport *transport,
1227 DBusCredentials *auth_identity;
1229 *pid = DBUS_PID_UNSET; /* Caller should never use this value on purpose,
1230 * but we set it to a safe number, INT_MAX,
1231 * just to root out possible bugs in bad callers.
1234 if (!transport->authenticated)
1237 auth_identity = _dbus_auth_get_identity (transport->auth);
1239 if (_dbus_credentials_include (auth_identity,
1240 DBUS_CREDENTIAL_UNIX_PROCESS_ID))
1242 *pid = _dbus_credentials_get_unix_pid (auth_identity);
1250 * See dbus_connection_set_unix_user_function().
1252 * @param transport the transport
1253 * @param function the predicate
1254 * @param data data to pass to the predicate
1255 * @param free_data_function function to free the data
1256 * @param old_data the old user data to be freed
1257 * @param old_free_data_function old free data function to free it with
1260 _dbus_transport_set_unix_user_function (DBusTransport *transport,
1261 DBusAllowUnixUserFunction function,
1263 DBusFreeFunction free_data_function,
1265 DBusFreeFunction *old_free_data_function)
1267 *old_data = transport->unix_user_data;
1268 *old_free_data_function = transport->free_unix_user_data;
1270 transport->unix_user_function = function;
1271 transport->unix_user_data = data;
1272 transport->free_unix_user_data = free_data_function;
1276 * See dbus_connection_get_windows_user().
1278 * @param transport the transport
1279 * @param windows_sid_p return location for the user ID
1280 * @returns #TRUE if user is available; the returned value may still be #NULL if no memory to copy it
1283 _dbus_transport_get_windows_user (DBusTransport *transport,
1284 char **windows_sid_p)
1286 DBusCredentials *auth_identity;
1288 *windows_sid_p = NULL;
1290 if (!transport->authenticated)
1293 auth_identity = _dbus_auth_get_identity (transport->auth);
1295 if (_dbus_credentials_include (auth_identity,
1296 DBUS_CREDENTIAL_WINDOWS_SID))
1298 /* If no memory, we are supposed to return TRUE and set NULL */
1299 *windows_sid_p = _dbus_strdup (_dbus_credentials_get_windows_sid (auth_identity));
1308 * See dbus_connection_set_windows_user_function().
1310 * @param transport the transport
1311 * @param function the predicate
1312 * @param data data to pass to the predicate
1313 * @param free_data_function function to free the data
1314 * @param old_data the old user data to be freed
1315 * @param old_free_data_function old free data function to free it with
1319 _dbus_transport_set_windows_user_function (DBusTransport *transport,
1320 DBusAllowWindowsUserFunction function,
1322 DBusFreeFunction free_data_function,
1324 DBusFreeFunction *old_free_data_function)
1326 *old_data = transport->windows_user_data;
1327 *old_free_data_function = transport->free_windows_user_data;
1329 transport->windows_user_function = function;
1330 transport->windows_user_data = data;
1331 transport->free_windows_user_data = free_data_function;
1335 * Sets the SASL authentication mechanisms supported by this transport.
1337 * @param transport the transport
1338 * @param mechanisms the #NULL-terminated array of mechanisms
1340 * @returns #FALSE if no memory
1343 _dbus_transport_set_auth_mechanisms (DBusTransport *transport,
1344 const char **mechanisms)
1346 return _dbus_auth_set_mechanisms (transport->auth, mechanisms);
1350 * See dbus_connection_set_allow_anonymous()
1352 * @param transport the transport
1353 * @param value #TRUE to allow anonymous connection
1356 _dbus_transport_set_allow_anonymous (DBusTransport *transport,
1359 transport->allow_anonymous = value != FALSE;