1 /* -*- mode: C; c-file-style: "gnu" -*- */
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 1.2
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-connection-internal.h"
27 #include "dbus-watch.h"
28 #include "dbus-auth.h"
29 #include "dbus-address.h"
30 #ifdef DBUS_BUILD_TESTS
31 #include "dbus-server-debug-pipe.h"
35 * @defgroup DBusTransport DBusTransport object
36 * @ingroup DBusInternals
37 * @brief "Backend" for a DBusConnection.
39 * Types and functions related to DBusTransport. A transport is an
40 * abstraction that can send and receive data via various kinds of
41 * network connections or other IPC mechanisms.
47 * @typedef DBusTransport
49 * Opaque object representing a way message stream.
50 * DBusTransport abstracts various kinds of actual
51 * transport mechanism, such as different network protocols,
52 * or encryption schemes.
56 live_messages_size_notify (DBusCounter *counter,
59 DBusTransport *transport = user_data;
61 _dbus_transport_ref (transport);
64 _dbus_verbose ("Counter value is now %d\n",
65 (int) _dbus_counter_get_value (counter));
68 /* disable or re-enable the read watch for the transport if
71 if (* transport->vtable->live_messages_changed)
72 (* transport->vtable->live_messages_changed) (transport);
74 _dbus_transport_unref (transport);
78 * Initializes the base class members of DBusTransport.
79 * Chained up to by subclasses in their constructor.
81 * @param transport the transport being created.
82 * @param vtable the subclass vtable.
83 * @param server #TRUE if this transport is on the server side of a connection
84 * @param address the address of the transport
85 * @returns #TRUE on success.
88 _dbus_transport_init_base (DBusTransport *transport,
89 const DBusTransportVTable *vtable,
91 const DBusString *address)
93 DBusMessageLoader *loader;
98 loader = _dbus_message_loader_new ();
103 auth = _dbus_auth_server_new ();
105 auth = _dbus_auth_client_new ();
108 _dbus_message_loader_unref (loader);
112 counter = _dbus_counter_new ();
115 _dbus_auth_unref (auth);
116 _dbus_message_loader_unref (loader);
122 _dbus_assert (address == NULL);
127 _dbus_assert (address != NULL);
129 if (!_dbus_string_copy_data (address, &address_copy))
131 _dbus_counter_unref (counter);
132 _dbus_auth_unref (auth);
133 _dbus_message_loader_unref (loader);
138 transport->refcount = 1;
139 transport->vtable = vtable;
140 transport->loader = loader;
141 transport->auth = auth;
142 transport->live_messages_size = counter;
143 transport->authenticated = FALSE;
144 transport->messages_need_sending = FALSE;
145 transport->disconnected = FALSE;
146 transport->send_credentials_pending = !server;
147 transport->receive_credentials_pending = server;
148 transport->is_server = server;
149 transport->address = address_copy;
151 transport->unix_user_function = NULL;
152 transport->unix_user_data = NULL;
153 transport->free_unix_user_data = NULL;
155 /* Try to default to something that won't totally hose the system,
156 * but doesn't impose too much of a limitation.
158 transport->max_live_messages_size = _DBUS_ONE_MEGABYTE * 63;
160 transport->credentials.pid = -1;
161 transport->credentials.uid = -1;
162 transport->credentials.gid = -1;
164 _dbus_counter_set_notify (transport->live_messages_size,
165 transport->max_live_messages_size,
166 live_messages_size_notify,
169 if (transport->address)
170 _dbus_verbose ("Initialized transport on address %s\n", transport->address);
176 * Finalizes base class members of DBusTransport.
177 * Chained up to from subclass finalizers.
179 * @param transport the transport.
182 _dbus_transport_finalize_base (DBusTransport *transport)
184 if (!transport->disconnected)
185 _dbus_transport_disconnect (transport);
187 if (transport->free_unix_user_data != NULL)
188 (* transport->free_unix_user_data) (transport->unix_user_data);
190 _dbus_message_loader_unref (transport->loader);
191 _dbus_auth_unref (transport->auth);
192 _dbus_counter_set_notify (transport->live_messages_size,
194 _dbus_counter_unref (transport->live_messages_size);
195 dbus_free (transport->address);
199 * Opens a new transport for the given address. (This opens a
200 * client-side-of-the-connection transport.)
202 * @todo error messages on bad address could really be better.
203 * DBusResultCode is a bit limiting here.
205 * @param address the address.
206 * @param error location to store reason for failure.
207 * @returns new transport of #NULL on failure.
210 _dbus_transport_open (const char *address,
213 DBusTransport *transport;
214 DBusAddressEntry **entries;
216 DBusError first_error;
218 const char *address_problem_type;
219 const char *address_problem_field;
220 const char *address_problem_other;
222 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
224 if (!dbus_parse_address (address, &entries, &len, error))
227 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
230 address_problem_type = NULL;
231 address_problem_field = NULL;
232 address_problem_other = NULL;
234 dbus_error_init (&tmp_error);
235 dbus_error_init (&first_error);
236 for (i = 0; i < len; i++)
240 method = dbus_address_entry_get_method (entries[i]);
242 if (strcmp (method, "unix") == 0)
244 const char *path = dbus_address_entry_get_value (entries[i], "path");
245 const char *tmpdir = dbus_address_entry_get_value (entries[i], "tmpdir");
246 const char *abstract = dbus_address_entry_get_value (entries[i], "abstract");
250 address_problem_other = "cannot use the \"tmpdir\" option for an address to connect to, only in an address to listen on";
254 if (path == NULL && abstract == NULL)
256 address_problem_type = "unix";
257 address_problem_field = "path or abstract";
261 if (path != NULL && abstract != NULL)
263 address_problem_other = "can't specify both \"path\" and \"abstract\" options in an address";
268 transport = _dbus_transport_new_for_domain_socket (path, FALSE,
271 transport = _dbus_transport_new_for_domain_socket (abstract, TRUE,
274 else if (strcmp (method, "tcp") == 0)
276 const char *host = dbus_address_entry_get_value (entries[i], "host");
277 const char *port = dbus_address_entry_get_value (entries[i], "port");
284 address_problem_type = "tcp";
285 address_problem_field = "port";
289 _dbus_string_init_const (&str, port);
290 sresult = _dbus_string_parse_int (&str, 0, &lport, NULL);
291 _dbus_string_free (&str);
293 if (sresult == FALSE || lport <= 0 || lport > 65535)
295 address_problem_other = "Port is not an integer between 0 and 65535";
299 transport = _dbus_transport_new_for_tcp_socket (host, lport, &tmp_error);
301 #ifdef DBUS_BUILD_TESTS
302 else if (strcmp (method, "debug-pipe") == 0)
304 const char *name = dbus_address_entry_get_value (entries[i], "name");
308 address_problem_type = "debug-pipe";
309 address_problem_field = "name";
313 transport = _dbus_transport_debug_pipe_new (name, &tmp_error);
318 address_problem_other = "Unknown address type (examples of valid types are \"unix\" and \"tcp\")";
325 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
328 dbus_move_error (&tmp_error, &first_error);
330 dbus_error_free (&tmp_error);
333 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
334 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
336 if (transport == NULL)
338 _DBUS_ASSERT_ERROR_IS_SET (&first_error);
339 dbus_move_error (&first_error, error);
343 dbus_error_free (&first_error);
346 dbus_address_entries_free (entries);
350 dbus_address_entries_free (entries);
352 if (address_problem_type != NULL)
353 dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
354 "Address of type %s was missing argument %s",
355 address_problem_type, address_problem_field);
357 dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
358 "Could not parse address: %s",
359 address_problem_other);
365 * Increments the reference count for the transport.
367 * @param transport the transport.
370 _dbus_transport_ref (DBusTransport *transport)
372 _dbus_assert (transport->refcount > 0);
374 transport->refcount += 1;
378 * Decrements the reference count for the transport.
379 * Disconnects and finalizes the transport if
380 * the reference count reaches zero.
382 * @param transport the transport.
385 _dbus_transport_unref (DBusTransport *transport)
387 _dbus_assert (transport != NULL);
388 _dbus_assert (transport->refcount > 0);
390 transport->refcount -= 1;
391 if (transport->refcount == 0)
393 _dbus_assert (transport->vtable->finalize != NULL);
395 (* transport->vtable->finalize) (transport);
400 * Closes our end of the connection to a remote application. Further
401 * attempts to use this transport will fail. Only the first call to
402 * _dbus_transport_disconnect() will have an effect.
404 * @param transport the transport.
408 _dbus_transport_disconnect (DBusTransport *transport)
410 _dbus_assert (transport->vtable->disconnect != NULL);
412 if (transport->disconnected)
415 (* transport->vtable->disconnect) (transport);
417 transport->disconnected = TRUE;
419 if (transport->connection)
420 _dbus_connection_notify_disconnected (transport->connection);
424 * Returns #TRUE if the transport has not been disconnected.
425 * Disconnection can result from _dbus_transport_disconnect()
426 * or because the server drops its end of the connection.
428 * @param transport the transport.
429 * @returns whether we're connected
432 _dbus_transport_get_is_connected (DBusTransport *transport)
434 return !transport->disconnected;
438 * Returns #TRUE if we have been authenticated. Will return #TRUE
439 * even if the transport is disconnected.
441 * @todo needs to drop connection->mutex when calling the unix_user_function
443 * @param transport the transport
444 * @returns whether we're authenticated
447 _dbus_transport_get_is_authenticated (DBusTransport *transport)
449 if (transport->authenticated)
453 dbus_bool_t maybe_authenticated;
455 if (transport->disconnected)
458 maybe_authenticated =
459 (!(transport->send_credentials_pending ||
460 transport->receive_credentials_pending));
462 if (maybe_authenticated)
464 switch (_dbus_auth_do_work (transport->auth))
466 case DBUS_AUTH_STATE_AUTHENTICATED:
467 case DBUS_AUTH_STATE_AUTHENTICATED_WITH_UNUSED_BYTES:
468 /* leave as maybe_authenticated */
471 maybe_authenticated = FALSE;
475 /* If we've authenticated as some identity, check that the auth
476 * identity is the same as our own identity. In the future, we
477 * may have API allowing applications to specify how this is
478 * done, for example they may allow connection as any identity,
479 * but then impose restrictions on certain identities.
480 * Or they may give certain identities extra privileges.
483 if (maybe_authenticated && transport->is_server)
485 DBusCredentials auth_identity;
487 _dbus_auth_get_identity (transport->auth, &auth_identity);
489 if (transport->unix_user_function != NULL)
491 /* FIXME we hold the connection lock here and should drop it */
492 if (!(* transport->unix_user_function) (transport->connection,
494 transport->unix_user_data))
496 _dbus_verbose ("Client UID "DBUS_UID_FORMAT
497 " was rejected, disconnecting\n",
499 _dbus_transport_disconnect (transport);
504 _dbus_verbose ("Client UID "DBUS_UID_FORMAT" authorized\n", auth_identity.uid);
509 DBusCredentials our_identity;
511 _dbus_credentials_from_current_process (&our_identity);
513 if (!_dbus_credentials_match (&our_identity,
516 _dbus_verbose ("Client authorized as UID "DBUS_UID_FORMAT
517 " but our UID is "DBUS_UID_FORMAT", disconnecting\n",
518 auth_identity.uid, our_identity.uid);
519 _dbus_transport_disconnect (transport);
524 _dbus_verbose ("Client authorized as UID "DBUS_UID_FORMAT
525 " matching our UID "DBUS_UID_FORMAT"\n",
526 auth_identity.uid, our_identity.uid);
531 transport->authenticated = maybe_authenticated;
533 return transport->authenticated;
538 * Gets the address of a transport. It will be
539 * #NULL for a server-side transport.
541 * @param transport the transport
542 * @returns transport's address
545 _dbus_transport_get_address (DBusTransport *transport)
547 return transport->address;
551 * Handles a watch by reading data, writing data, or disconnecting
552 * the transport, as appropriate for the given condition.
554 * @param transport the transport.
555 * @param watch the watch.
556 * @param condition the current state of the watched file descriptor.
557 * @returns #FALSE if not enough memory to fully handle the watch
560 _dbus_transport_handle_watch (DBusTransport *transport,
562 unsigned int condition)
566 _dbus_assert (transport->vtable->handle_watch != NULL);
568 if (transport->disconnected)
571 if (dbus_watch_get_fd (watch) < 0)
573 _dbus_warn ("Tried to handle an invalidated watch; this watch should have been removed\n");
577 _dbus_watch_sanitize_condition (watch, &condition);
579 _dbus_transport_ref (transport);
580 _dbus_watch_ref (watch);
581 retval = (* transport->vtable->handle_watch) (transport, watch, condition);
582 _dbus_watch_unref (watch);
583 _dbus_transport_unref (transport);
589 * Sets the connection using this transport. Allows the transport
590 * to add watches to the connection, queue incoming messages,
591 * and pull outgoing messages.
593 * @param transport the transport.
594 * @param connection the connection.
595 * @returns #FALSE if not enough memory
598 _dbus_transport_set_connection (DBusTransport *transport,
599 DBusConnection *connection)
601 _dbus_assert (transport->vtable->connection_set != NULL);
602 _dbus_assert (transport->connection == NULL);
604 transport->connection = connection;
606 _dbus_transport_ref (transport);
607 if (!(* transport->vtable->connection_set) (transport))
608 transport->connection = NULL;
609 _dbus_transport_unref (transport);
611 return transport->connection != NULL;
615 * Notifies the transport when the outgoing message queue goes from
616 * empty to non-empty or vice versa. Typically causes the transport to
617 * add or remove its DBUS_WATCH_WRITABLE watch.
619 * @param transport the transport.
620 * @param queue_length the length of the outgoing message queue.
624 _dbus_transport_messages_pending (DBusTransport *transport,
627 _dbus_assert (transport->vtable->messages_pending != NULL);
629 if (transport->disconnected)
632 transport->messages_need_sending = queue_length > 0;
634 _dbus_transport_ref (transport);
635 (* transport->vtable->messages_pending) (transport,
637 _dbus_transport_unref (transport);
641 * Performs a single poll()/select() on the transport's file
642 * descriptors and then reads/writes data as appropriate,
643 * queueing incoming messages and sending outgoing messages.
644 * This is the backend for _dbus_connection_do_iteration().
645 * See _dbus_connection_do_iteration() for full details.
647 * @param transport the transport.
648 * @param flags indicates whether to read or write, and whether to block.
649 * @param timeout_milliseconds if blocking, timeout or -1 for no timeout.
652 _dbus_transport_do_iteration (DBusTransport *transport,
654 int timeout_milliseconds)
656 _dbus_assert (transport->vtable->do_iteration != NULL);
658 _dbus_verbose ("Transport iteration flags 0x%x timeout %d connected = %d\n",
659 flags, timeout_milliseconds, !transport->disconnected);
661 if ((flags & (DBUS_ITERATION_DO_WRITING |
662 DBUS_ITERATION_DO_READING)) == 0)
663 return; /* Nothing to do */
665 if (transport->disconnected)
668 _dbus_transport_ref (transport);
669 (* transport->vtable->do_iteration) (transport, flags,
670 timeout_milliseconds);
671 _dbus_transport_unref (transport);
675 recover_unused_bytes (DBusTransport *transport)
677 if (_dbus_auth_do_work (transport->auth) != DBUS_AUTH_STATE_AUTHENTICATED_WITH_UNUSED_BYTES)
680 if (_dbus_auth_needs_decoding (transport->auth))
682 DBusString plaintext;
683 const DBusString *encoded;
687 if (!_dbus_string_init (&plaintext))
690 _dbus_auth_get_unused_bytes (transport->auth,
693 if (!_dbus_auth_decode_data (transport->auth,
694 encoded, &plaintext))
696 _dbus_string_free (&plaintext);
700 _dbus_message_loader_get_buffer (transport->loader,
703 orig_len = _dbus_string_get_length (buffer);
705 if (!_dbus_string_move (&plaintext, 0, buffer,
708 _dbus_string_free (&plaintext);
712 _dbus_verbose (" %d unused bytes sent to message loader\n",
713 _dbus_string_get_length (buffer) -
716 _dbus_message_loader_return_buffer (transport->loader,
718 _dbus_string_get_length (buffer) -
721 _dbus_auth_delete_unused_bytes (transport->auth);
723 _dbus_string_free (&plaintext);
727 const DBusString *bytes;
730 dbus_bool_t succeeded;
732 _dbus_message_loader_get_buffer (transport->loader,
735 orig_len = _dbus_string_get_length (buffer);
737 _dbus_auth_get_unused_bytes (transport->auth,
741 if (!_dbus_string_copy (bytes, 0, buffer, _dbus_string_get_length (buffer)))
744 _dbus_verbose (" %d unused bytes sent to message loader\n",
745 _dbus_string_get_length (buffer) -
748 _dbus_message_loader_return_buffer (transport->loader,
750 _dbus_string_get_length (buffer) -
754 _dbus_auth_delete_unused_bytes (transport->auth);
762 _dbus_verbose ("Not enough memory to transfer unused bytes from auth conversation\n");
767 * Reports our current dispatch status (whether there's buffered
768 * data to be queued as messages, or not, or we need memory).
770 * @param transport the transport
771 * @returns current status
774 _dbus_transport_get_dispatch_status (DBusTransport *transport)
776 if (_dbus_counter_get_value (transport->live_messages_size) >= transport->max_live_messages_size)
777 return DBUS_DISPATCH_COMPLETE; /* complete for now */
779 if (!_dbus_transport_get_is_authenticated (transport))
781 if (_dbus_auth_do_work (transport->auth) ==
782 DBUS_AUTH_STATE_WAITING_FOR_MEMORY)
783 return DBUS_DISPATCH_NEED_MEMORY;
784 else if (!_dbus_transport_get_is_authenticated (transport))
785 return DBUS_DISPATCH_COMPLETE;
788 if (!transport->unused_bytes_recovered &&
789 !recover_unused_bytes (transport))
790 return DBUS_DISPATCH_NEED_MEMORY;
792 transport->unused_bytes_recovered = TRUE;
794 if (!_dbus_message_loader_queue_messages (transport->loader))
795 return DBUS_DISPATCH_NEED_MEMORY;
797 if (_dbus_message_loader_peek_message (transport->loader) != NULL)
798 return DBUS_DISPATCH_DATA_REMAINS;
800 return DBUS_DISPATCH_COMPLETE;
804 * Processes data we've read while handling a watch, potentially
805 * converting some of it to messages and queueing those messages on
808 * @param transport the transport
809 * @returns #TRUE if we had enough memory to queue all messages
812 _dbus_transport_queue_messages (DBusTransport *transport)
814 DBusDispatchStatus status;
816 _dbus_verbose ("_dbus_transport_queue_messages()\n");
818 /* Queue any messages */
819 while ((status = _dbus_transport_get_dispatch_status (transport)) == DBUS_DISPATCH_DATA_REMAINS)
821 DBusMessage *message;
824 link = _dbus_message_loader_pop_message_link (transport->loader);
825 _dbus_assert (link != NULL);
827 message = link->data;
829 _dbus_verbose ("queueing received message %p\n", message);
831 if (!_dbus_message_add_size_counter (message, transport->live_messages_size))
833 _dbus_message_loader_putback_message_link (transport->loader,
835 status = DBUS_DISPATCH_NEED_MEMORY;
840 /* pass ownership of link and message ref to connection */
841 _dbus_connection_queue_received_message_link (transport->connection,
846 if (_dbus_message_loader_get_is_corrupted (transport->loader))
848 _dbus_verbose ("Corrupted message stream, disconnecting\n");
849 _dbus_transport_disconnect (transport);
852 return status != DBUS_DISPATCH_NEED_MEMORY;
856 * See dbus_connection_set_max_message_size().
858 * @param transport the transport
859 * @param size the max size of a single message
862 _dbus_transport_set_max_message_size (DBusTransport *transport,
865 _dbus_message_loader_set_max_message_size (transport->loader, size);
869 * See dbus_connection_get_max_message_size().
871 * @param transport the transport
872 * @returns max message size
875 _dbus_transport_get_max_message_size (DBusTransport *transport)
877 return _dbus_message_loader_get_max_message_size (transport->loader);
881 * See dbus_connection_set_max_received_size().
883 * @param transport the transport
884 * @param size the max size of all incoming messages
887 _dbus_transport_set_max_received_size (DBusTransport *transport,
890 transport->max_live_messages_size = size;
891 _dbus_counter_set_notify (transport->live_messages_size,
892 transport->max_live_messages_size,
893 live_messages_size_notify,
899 * See dbus_connection_get_max_received_size().
901 * @param transport the transport
902 * @returns max bytes for all live messages
905 _dbus_transport_get_max_received_size (DBusTransport *transport)
907 return transport->max_live_messages_size;
911 * See dbus_connection_get_unix_user().
913 * @param transport the transport
914 * @param uid return location for the user ID
915 * @returns #TRUE if uid is filled in with a valid user ID
918 _dbus_transport_get_unix_user (DBusTransport *transport,
921 DBusCredentials auth_identity;
923 *uid = _DBUS_INT_MAX; /* better than some root or system user in
924 * case of bugs in the caller. Caller should
925 * never use this value on purpose, however.
928 if (!transport->authenticated)
931 _dbus_auth_get_identity (transport->auth, &auth_identity);
933 if (auth_identity.uid != DBUS_UID_UNSET)
935 *uid = auth_identity.uid;
943 * See dbus_connection_set_unix_user_function().
945 * @param transport the transport
946 * @param function the predicate
947 * @param data data to pass to the predicate
948 * @param free_data_function function to free the data
949 * @param old_data the old user data to be freed
950 * @param old_free_data_function old free data function to free it with
953 _dbus_transport_set_unix_user_function (DBusTransport *transport,
954 DBusAllowUnixUserFunction function,
956 DBusFreeFunction free_data_function,
958 DBusFreeFunction *old_free_data_function)
960 *old_data = transport->unix_user_data;
961 *old_free_data_function = transport->free_unix_user_data;
963 transport->unix_user_function = function;
964 transport->unix_user_data = data;
965 transport->free_unix_user_data = free_data_function;
969 * Sets the SASL authentication mechanisms supported by this transport.
971 * @param transport the transport
972 * @param mechanisms the #NULL-terminated array of mechanisms
974 * @returns #FALSE if no memory
977 _dbus_transport_set_auth_mechanisms (DBusTransport *transport,
978 const char **mechanisms)
980 return _dbus_auth_set_mechanisms (transport->auth, mechanisms);