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 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-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. Chained up to
79 * by subclasses in their constructor. The server GUID is the
80 * globally unique ID for the server creating this connection
81 * and will be #NULL for the client side of a connection. The GUID
84 * @param transport the transport being created.
85 * @param vtable the subclass vtable.
86 * @param server_guid non-#NULL if this transport is on the server side of a connection
87 * @param address the address of the transport
88 * @returns #TRUE on success.
91 _dbus_transport_init_base (DBusTransport *transport,
92 const DBusTransportVTable *vtable,
93 const DBusString *server_guid,
94 const DBusString *address)
96 DBusMessageLoader *loader;
101 loader = _dbus_message_loader_new ();
106 auth = _dbus_auth_server_new (server_guid);
108 auth = _dbus_auth_client_new ();
111 _dbus_message_loader_unref (loader);
115 counter = _dbus_counter_new ();
118 _dbus_auth_unref (auth);
119 _dbus_message_loader_unref (loader);
125 _dbus_assert (address == NULL);
130 _dbus_assert (address != NULL);
132 if (!_dbus_string_copy_data (address, &address_copy))
134 _dbus_counter_unref (counter);
135 _dbus_auth_unref (auth);
136 _dbus_message_loader_unref (loader);
141 transport->refcount = 1;
142 transport->vtable = vtable;
143 transport->loader = loader;
144 transport->auth = auth;
145 transport->live_messages_size = counter;
146 transport->authenticated = FALSE;
147 transport->disconnected = FALSE;
148 transport->is_server = (server_guid != NULL);
149 transport->send_credentials_pending = !transport->is_server;
150 transport->receive_credentials_pending = transport->is_server;
151 transport->address = address_copy;
153 transport->unix_user_function = NULL;
154 transport->unix_user_data = NULL;
155 transport->free_unix_user_data = NULL;
157 /* Try to default to something that won't totally hose the system,
158 * but doesn't impose too much of a limitation.
160 transport->max_live_messages_size = _DBUS_ONE_MEGABYTE * 63;
162 transport->credentials.pid = -1;
163 transport->credentials.uid = -1;
164 transport->credentials.gid = -1;
166 _dbus_counter_set_notify (transport->live_messages_size,
167 transport->max_live_messages_size,
168 live_messages_size_notify,
171 if (transport->address)
172 _dbus_verbose ("Initialized transport on address %s\n", transport->address);
178 * Finalizes base class members of DBusTransport.
179 * Chained up to from subclass finalizers.
181 * @param transport the transport.
184 _dbus_transport_finalize_base (DBusTransport *transport)
186 if (!transport->disconnected)
187 _dbus_transport_disconnect (transport);
189 if (transport->free_unix_user_data != NULL)
190 (* transport->free_unix_user_data) (transport->unix_user_data);
192 _dbus_message_loader_unref (transport->loader);
193 _dbus_auth_unref (transport->auth);
194 _dbus_counter_set_notify (transport->live_messages_size,
196 _dbus_counter_unref (transport->live_messages_size);
197 dbus_free (transport->address);
201 * Try to open a new transport for the given address entry. (This
202 * opens a client-side-of-the-connection transport.)
204 * @param entry the address entry
205 * @param error location to store reason for failure.
206 * @returns new transport of #NULL on failure.
209 _dbus_transport_open (DBusAddressEntry *entry,
212 DBusTransport *transport;
213 const char *address_problem_type;
214 const char *address_problem_field;
215 const char *address_problem_other;
218 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
221 address_problem_type = NULL;
222 address_problem_field = NULL;
223 address_problem_other = NULL;
225 method = dbus_address_entry_get_method (entry);
226 _dbus_assert (method != NULL);
228 if (strcmp (method, "unix") == 0)
230 const char *path = dbus_address_entry_get_value (entry, "path");
231 const char *tmpdir = dbus_address_entry_get_value (entry, "tmpdir");
232 const char *abstract = dbus_address_entry_get_value (entry, "abstract");
236 address_problem_other = "cannot use the \"tmpdir\" option for an address to connect to, only in an address to listen on";
240 if (path == NULL && abstract == NULL)
242 address_problem_type = "unix";
243 address_problem_field = "path or abstract";
247 if (path != NULL && abstract != NULL)
249 address_problem_other = "can't specify both \"path\" and \"abstract\" options in an address";
254 transport = _dbus_transport_new_for_domain_socket (path, FALSE,
257 transport = _dbus_transport_new_for_domain_socket (abstract, TRUE,
260 else if (strcmp (method, "tcp") == 0)
262 const char *host = dbus_address_entry_get_value (entry, "host");
263 const char *port = dbus_address_entry_get_value (entry, "port");
270 address_problem_type = "tcp";
271 address_problem_field = "port";
275 _dbus_string_init_const (&str, port);
276 sresult = _dbus_string_parse_int (&str, 0, &lport, NULL);
277 _dbus_string_free (&str);
279 if (sresult == FALSE || lport <= 0 || lport > 65535)
281 address_problem_other = "Port is not an integer between 0 and 65535";
285 transport = _dbus_transport_new_for_tcp_socket (host, lport, error);
287 #ifdef DBUS_BUILD_TESTS
288 else if (strcmp (method, "debug-pipe") == 0)
290 const char *name = dbus_address_entry_get_value (entry, "name");
294 address_problem_type = "debug-pipe";
295 address_problem_field = "name";
299 transport = _dbus_transport_debug_pipe_new (name, error);
304 address_problem_other = "Unknown address type (examples of valid types are \"unix\" and \"tcp\")";
308 if (transport == NULL)
309 _DBUS_ASSERT_ERROR_IS_SET (error);
314 if (address_problem_type != NULL)
315 dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
316 "Address of type %s was missing argument %s",
317 address_problem_type, address_problem_field);
319 dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
320 "Could not parse address: %s",
321 address_problem_other);
327 * Increments the reference count for the transport.
329 * @param transport the transport.
330 * @returns the transport.
333 _dbus_transport_ref (DBusTransport *transport)
335 _dbus_assert (transport->refcount > 0);
337 transport->refcount += 1;
343 * Decrements the reference count for the transport.
344 * Disconnects and finalizes the transport if
345 * the reference count reaches zero.
347 * @param transport the transport.
350 _dbus_transport_unref (DBusTransport *transport)
352 _dbus_assert (transport != NULL);
353 _dbus_assert (transport->refcount > 0);
355 transport->refcount -= 1;
356 if (transport->refcount == 0)
358 _dbus_verbose ("%s: finalizing\n", _DBUS_FUNCTION_NAME);
360 _dbus_assert (transport->vtable->finalize != NULL);
362 (* transport->vtable->finalize) (transport);
367 * Closes our end of the connection to a remote application. Further
368 * attempts to use this transport will fail. Only the first call to
369 * _dbus_transport_disconnect() will have an effect.
371 * @param transport the transport.
375 _dbus_transport_disconnect (DBusTransport *transport)
377 _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
379 _dbus_assert (transport->vtable->disconnect != NULL);
381 if (transport->disconnected)
384 (* transport->vtable->disconnect) (transport);
386 transport->disconnected = TRUE;
388 _dbus_verbose ("%s end\n", _DBUS_FUNCTION_NAME);
392 * Returns #TRUE if the transport has not been disconnected.
393 * Disconnection can result from _dbus_transport_disconnect()
394 * or because the server drops its end of the connection.
396 * @param transport the transport.
397 * @returns whether we're connected
400 _dbus_transport_get_is_connected (DBusTransport *transport)
402 return !transport->disconnected;
406 * Returns #TRUE if we have been authenticated. Will return #TRUE
407 * even if the transport is disconnected.
409 * @todo we drop connection->mutex when calling the unix_user_function,
410 * which may not be safe really.
412 * @param transport the transport
413 * @returns whether we're authenticated
416 _dbus_transport_get_is_authenticated (DBusTransport *transport)
418 if (transport->authenticated)
422 dbus_bool_t maybe_authenticated;
424 if (transport->disconnected)
427 /* paranoia ref since we call user callbacks sometimes */
428 _dbus_connection_ref_unlocked (transport->connection);
430 maybe_authenticated =
431 (!(transport->send_credentials_pending ||
432 transport->receive_credentials_pending));
434 if (maybe_authenticated)
436 switch (_dbus_auth_do_work (transport->auth))
438 case DBUS_AUTH_STATE_AUTHENTICATED:
439 /* leave as maybe_authenticated */
442 maybe_authenticated = FALSE;
446 /* If we've authenticated as some identity, check that the auth
447 * identity is the same as our own identity. In the future, we
448 * may have API allowing applications to specify how this is
449 * done, for example they may allow connection as any identity,
450 * but then impose restrictions on certain identities.
451 * Or they may give certain identities extra privileges.
454 if (maybe_authenticated && transport->is_server)
456 DBusCredentials auth_identity;
458 _dbus_auth_get_identity (transport->auth, &auth_identity);
460 if (transport->unix_user_function != NULL)
463 DBusConnection *connection;
464 DBusAllowUnixUserFunction unix_user_function;
465 void *unix_user_data;
467 /* Dropping the lock here probably isn't that safe. */
469 connection = transport->connection;
470 unix_user_function = transport->unix_user_function;
471 unix_user_data = transport->unix_user_data;
473 _dbus_verbose ("unlock %s\n", _DBUS_FUNCTION_NAME);
474 _dbus_connection_unlock (connection);
476 allow = (* unix_user_function) (connection,
480 _dbus_verbose ("lock %s post unix user function\n", _DBUS_FUNCTION_NAME);
481 _dbus_connection_lock (connection);
485 _dbus_verbose ("Client UID "DBUS_UID_FORMAT" authorized\n", auth_identity.uid);
489 _dbus_verbose ("Client UID "DBUS_UID_FORMAT
490 " was rejected, disconnecting\n",
492 _dbus_transport_disconnect (transport);
493 _dbus_connection_unref_unlocked (connection);
499 DBusCredentials our_identity;
501 _dbus_credentials_from_current_process (&our_identity);
503 if (!_dbus_credentials_match (&our_identity,
506 _dbus_verbose ("Client authorized as UID "DBUS_UID_FORMAT
507 " but our UID is "DBUS_UID_FORMAT", disconnecting\n",
508 auth_identity.uid, our_identity.uid);
509 _dbus_transport_disconnect (transport);
510 _dbus_connection_unref_unlocked (transport->connection);
515 _dbus_verbose ("Client authorized as UID "DBUS_UID_FORMAT
516 " matching our UID "DBUS_UID_FORMAT"\n",
517 auth_identity.uid, our_identity.uid);
522 transport->authenticated = maybe_authenticated;
524 _dbus_connection_unref_unlocked (transport->connection);
525 return maybe_authenticated;
530 * Gets the address of a transport. It will be
531 * #NULL for a server-side transport.
533 * @param transport the transport
534 * @returns transport's address
537 _dbus_transport_get_address (DBusTransport *transport)
539 return transport->address;
543 * Handles a watch by reading data, writing data, or disconnecting
544 * the transport, as appropriate for the given condition.
546 * @param transport the transport.
547 * @param watch the watch.
548 * @param condition the current state of the watched file descriptor.
549 * @returns #FALSE if not enough memory to fully handle the watch
552 _dbus_transport_handle_watch (DBusTransport *transport,
554 unsigned int condition)
558 _dbus_assert (transport->vtable->handle_watch != NULL);
560 if (transport->disconnected)
563 if (dbus_watch_get_fd (watch) < 0)
565 _dbus_warn ("Tried to handle an invalidated watch; this watch should have been removed\n");
569 _dbus_watch_sanitize_condition (watch, &condition);
571 _dbus_transport_ref (transport);
572 _dbus_watch_ref (watch);
573 retval = (* transport->vtable->handle_watch) (transport, watch, condition);
574 _dbus_watch_unref (watch);
575 _dbus_transport_unref (transport);
581 * Sets the connection using this transport. Allows the transport
582 * to add watches to the connection, queue incoming messages,
583 * and pull outgoing messages.
585 * @param transport the transport.
586 * @param connection the connection.
587 * @returns #FALSE if not enough memory
590 _dbus_transport_set_connection (DBusTransport *transport,
591 DBusConnection *connection)
593 _dbus_assert (transport->vtable->connection_set != NULL);
594 _dbus_assert (transport->connection == NULL);
596 transport->connection = connection;
598 _dbus_transport_ref (transport);
599 if (!(* transport->vtable->connection_set) (transport))
600 transport->connection = NULL;
601 _dbus_transport_unref (transport);
603 return transport->connection != NULL;
607 * Get the UNIX file descriptor, if any.
609 * @param transport the transport
610 * @param fd_p pointer to fill in with the descriptor
611 * @returns #TRUE if a descriptor was available
614 _dbus_transport_get_unix_fd (DBusTransport *transport,
619 if (transport->vtable->get_unix_fd == NULL)
622 if (transport->disconnected)
625 _dbus_transport_ref (transport);
627 retval = (* transport->vtable->get_unix_fd) (transport,
630 _dbus_transport_unref (transport);
636 * Performs a single poll()/select() on the transport's file
637 * descriptors and then reads/writes data as appropriate,
638 * queueing incoming messages and sending outgoing messages.
639 * This is the backend for _dbus_connection_do_iteration().
640 * See _dbus_connection_do_iteration() for full details.
642 * @param transport the transport.
643 * @param flags indicates whether to read or write, and whether to block.
644 * @param timeout_milliseconds if blocking, timeout or -1 for no timeout.
647 _dbus_transport_do_iteration (DBusTransport *transport,
649 int timeout_milliseconds)
651 _dbus_assert (transport->vtable->do_iteration != NULL);
653 _dbus_verbose ("Transport iteration flags 0x%x timeout %d connected = %d\n",
654 flags, timeout_milliseconds, !transport->disconnected);
656 if ((flags & (DBUS_ITERATION_DO_WRITING |
657 DBUS_ITERATION_DO_READING)) == 0)
658 return; /* Nothing to do */
660 if (transport->disconnected)
663 _dbus_transport_ref (transport);
664 (* transport->vtable->do_iteration) (transport, flags,
665 timeout_milliseconds);
666 _dbus_transport_unref (transport);
668 _dbus_verbose ("%s end\n", _DBUS_FUNCTION_NAME);
672 recover_unused_bytes (DBusTransport *transport)
674 if (_dbus_auth_needs_decoding (transport->auth))
676 DBusString plaintext;
677 const DBusString *encoded;
681 if (!_dbus_string_init (&plaintext))
684 _dbus_auth_get_unused_bytes (transport->auth,
687 if (!_dbus_auth_decode_data (transport->auth,
688 encoded, &plaintext))
690 _dbus_string_free (&plaintext);
694 _dbus_message_loader_get_buffer (transport->loader,
697 orig_len = _dbus_string_get_length (buffer);
699 if (!_dbus_string_move (&plaintext, 0, buffer,
702 _dbus_string_free (&plaintext);
706 _dbus_verbose (" %d unused bytes sent to message loader\n",
707 _dbus_string_get_length (buffer) -
710 _dbus_message_loader_return_buffer (transport->loader,
712 _dbus_string_get_length (buffer) -
715 _dbus_auth_delete_unused_bytes (transport->auth);
717 _dbus_string_free (&plaintext);
721 const DBusString *bytes;
724 dbus_bool_t succeeded;
726 _dbus_message_loader_get_buffer (transport->loader,
729 orig_len = _dbus_string_get_length (buffer);
731 _dbus_auth_get_unused_bytes (transport->auth,
735 if (!_dbus_string_copy (bytes, 0, buffer, _dbus_string_get_length (buffer)))
738 _dbus_verbose (" %d unused bytes sent to message loader\n",
739 _dbus_string_get_length (buffer) -
742 _dbus_message_loader_return_buffer (transport->loader,
744 _dbus_string_get_length (buffer) -
748 _dbus_auth_delete_unused_bytes (transport->auth);
756 _dbus_verbose ("Not enough memory to transfer unused bytes from auth conversation\n");
761 * Reports our current dispatch status (whether there's buffered
762 * data to be queued as messages, or not, or we need memory).
764 * @param transport the transport
765 * @returns current status
768 _dbus_transport_get_dispatch_status (DBusTransport *transport)
770 if (_dbus_counter_get_value (transport->live_messages_size) >= transport->max_live_messages_size)
771 return DBUS_DISPATCH_COMPLETE; /* complete for now */
773 if (!_dbus_transport_get_is_authenticated (transport))
775 if (_dbus_auth_do_work (transport->auth) ==
776 DBUS_AUTH_STATE_WAITING_FOR_MEMORY)
777 return DBUS_DISPATCH_NEED_MEMORY;
778 else if (!_dbus_transport_get_is_authenticated (transport))
779 return DBUS_DISPATCH_COMPLETE;
782 if (!transport->unused_bytes_recovered &&
783 !recover_unused_bytes (transport))
784 return DBUS_DISPATCH_NEED_MEMORY;
786 transport->unused_bytes_recovered = TRUE;
788 if (!_dbus_message_loader_queue_messages (transport->loader))
789 return DBUS_DISPATCH_NEED_MEMORY;
791 if (_dbus_message_loader_peek_message (transport->loader) != NULL)
792 return DBUS_DISPATCH_DATA_REMAINS;
794 return DBUS_DISPATCH_COMPLETE;
798 * Processes data we've read while handling a watch, potentially
799 * converting some of it to messages and queueing those messages on
802 * @param transport the transport
803 * @returns #TRUE if we had enough memory to queue all messages
806 _dbus_transport_queue_messages (DBusTransport *transport)
808 DBusDispatchStatus status;
811 _dbus_verbose ("_dbus_transport_queue_messages()\n");
814 /* Queue any messages */
815 while ((status = _dbus_transport_get_dispatch_status (transport)) == DBUS_DISPATCH_DATA_REMAINS)
817 DBusMessage *message;
820 link = _dbus_message_loader_pop_message_link (transport->loader);
821 _dbus_assert (link != NULL);
823 message = link->data;
825 _dbus_verbose ("queueing received message %p\n", message);
827 if (!_dbus_message_add_size_counter (message, transport->live_messages_size))
829 _dbus_message_loader_putback_message_link (transport->loader,
831 status = DBUS_DISPATCH_NEED_MEMORY;
836 /* pass ownership of link and message ref to connection */
837 _dbus_connection_queue_received_message_link (transport->connection,
842 if (_dbus_message_loader_get_is_corrupted (transport->loader))
844 _dbus_verbose ("Corrupted message stream, disconnecting\n");
845 _dbus_transport_disconnect (transport);
848 return status != DBUS_DISPATCH_NEED_MEMORY;
852 * See dbus_connection_set_max_message_size().
854 * @param transport the transport
855 * @param size the max size of a single message
858 _dbus_transport_set_max_message_size (DBusTransport *transport,
861 _dbus_message_loader_set_max_message_size (transport->loader, size);
865 * See dbus_connection_get_max_message_size().
867 * @param transport the transport
868 * @returns max message size
871 _dbus_transport_get_max_message_size (DBusTransport *transport)
873 return _dbus_message_loader_get_max_message_size (transport->loader);
877 * See dbus_connection_set_max_received_size().
879 * @param transport the transport
880 * @param size the max size of all incoming messages
883 _dbus_transport_set_max_received_size (DBusTransport *transport,
886 transport->max_live_messages_size = size;
887 _dbus_counter_set_notify (transport->live_messages_size,
888 transport->max_live_messages_size,
889 live_messages_size_notify,
895 * See dbus_connection_get_max_received_size().
897 * @param transport the transport
898 * @returns max bytes for all live messages
901 _dbus_transport_get_max_received_size (DBusTransport *transport)
903 return transport->max_live_messages_size;
907 * See dbus_connection_get_unix_user().
909 * @param transport the transport
910 * @param uid return location for the user ID
911 * @returns #TRUE if uid is filled in with a valid user ID
914 _dbus_transport_get_unix_user (DBusTransport *transport,
917 DBusCredentials auth_identity;
919 *uid = _DBUS_INT32_MAX; /* better than some root or system user in
920 * case of bugs in the caller. Caller should
921 * never use this value on purpose, however.
924 if (!transport->authenticated)
927 _dbus_auth_get_identity (transport->auth, &auth_identity);
929 if (auth_identity.uid != DBUS_UID_UNSET)
931 *uid = auth_identity.uid;
939 * See dbus_connection_get_unix_process_id().
941 * @param transport the transport
942 * @param pid return location for the process ID
943 * @returns #TRUE if uid is filled in with a valid process ID
946 _dbus_transport_get_unix_process_id (DBusTransport *transport,
949 DBusCredentials auth_identity;
951 *pid = DBUS_PID_UNSET; /* Caller should never use this value on purpose,
952 * but we set it to a safe number, INT_MAX,
953 * just to root out possible bugs in bad callers.
956 if (!transport->authenticated)
959 _dbus_auth_get_identity (transport->auth, &auth_identity);
961 if (auth_identity.pid != DBUS_PID_UNSET)
963 *pid = auth_identity.pid;
971 * See dbus_connection_set_unix_user_function().
973 * @param transport the transport
974 * @param function the predicate
975 * @param data data to pass to the predicate
976 * @param free_data_function function to free the data
977 * @param old_data the old user data to be freed
978 * @param old_free_data_function old free data function to free it with
981 _dbus_transport_set_unix_user_function (DBusTransport *transport,
982 DBusAllowUnixUserFunction function,
984 DBusFreeFunction free_data_function,
986 DBusFreeFunction *old_free_data_function)
988 *old_data = transport->unix_user_data;
989 *old_free_data_function = transport->free_unix_user_data;
991 transport->unix_user_function = function;
992 transport->unix_user_data = data;
993 transport->free_unix_user_data = free_data_function;
997 * Sets the SASL authentication mechanisms supported by this transport.
999 * @param transport the transport
1000 * @param mechanisms the #NULL-terminated array of mechanisms
1002 * @returns #FALSE if no memory
1005 _dbus_transport_set_auth_mechanisms (DBusTransport *transport,
1006 const char **mechanisms)
1008 return _dbus_auth_set_mechanisms (transport->auth, mechanisms);