00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "dbus-transport-protected.h"
00025 #include "dbus-transport-unix.h"
00026 #include "dbus-connection-internal.h"
00027 #include "dbus-watch.h"
00028 #include "dbus-auth.h"
00029 #include "dbus-address.h"
00030 #ifdef DBUS_BUILD_TESTS
00031 #include "dbus-server-debug-pipe.h"
00032 #endif
00033
00055 static void
00056 live_messages_size_notify (DBusCounter *counter,
00057 void *user_data)
00058 {
00059 DBusTransport *transport = user_data;
00060
00061 _dbus_transport_ref (transport);
00062
00063 #if 0
00064 _dbus_verbose ("Counter value is now %d\n",
00065 (int) _dbus_counter_get_value (counter));
00066 #endif
00067
00068
00069
00070
00071 if (* transport->vtable->live_messages_changed)
00072 (* transport->vtable->live_messages_changed) (transport);
00073
00074 _dbus_transport_unref (transport);
00075 }
00076
00087 dbus_bool_t
00088 _dbus_transport_init_base (DBusTransport *transport,
00089 const DBusTransportVTable *vtable,
00090 dbus_bool_t server,
00091 const DBusString *address)
00092 {
00093 DBusMessageLoader *loader;
00094 DBusAuth *auth;
00095 DBusCounter *counter;
00096 char *address_copy;
00097
00098 loader = _dbus_message_loader_new ();
00099 if (loader == NULL)
00100 return FALSE;
00101
00102 if (server)
00103 auth = _dbus_auth_server_new ();
00104 else
00105 auth = _dbus_auth_client_new ();
00106 if (auth == NULL)
00107 {
00108 _dbus_message_loader_unref (loader);
00109 return FALSE;
00110 }
00111
00112 counter = _dbus_counter_new ();
00113 if (counter == NULL)
00114 {
00115 _dbus_auth_unref (auth);
00116 _dbus_message_loader_unref (loader);
00117 return FALSE;
00118 }
00119
00120 if (server)
00121 {
00122 _dbus_assert (address == NULL);
00123 address_copy = NULL;
00124 }
00125 else
00126 {
00127 _dbus_assert (address != NULL);
00128
00129 if (!_dbus_string_copy_data (address, &address_copy))
00130 {
00131 _dbus_counter_unref (counter);
00132 _dbus_auth_unref (auth);
00133 _dbus_message_loader_unref (loader);
00134 return FALSE;
00135 }
00136 }
00137
00138 transport->refcount = 1;
00139 transport->vtable = vtable;
00140 transport->loader = loader;
00141 transport->auth = auth;
00142 transport->live_messages_size = counter;
00143 transport->authenticated = FALSE;
00144 transport->messages_need_sending = FALSE;
00145 transport->disconnected = FALSE;
00146 transport->send_credentials_pending = !server;
00147 transport->receive_credentials_pending = server;
00148 transport->is_server = server;
00149 transport->address = address_copy;
00150
00151 transport->unix_user_function = NULL;
00152 transport->unix_user_data = NULL;
00153 transport->free_unix_user_data = NULL;
00154
00155
00156
00157
00158 transport->max_live_messages_size = _DBUS_ONE_MEGABYTE * 63;
00159
00160 transport->credentials.pid = -1;
00161 transport->credentials.uid = -1;
00162 transport->credentials.gid = -1;
00163
00164 _dbus_counter_set_notify (transport->live_messages_size,
00165 transport->max_live_messages_size,
00166 live_messages_size_notify,
00167 transport);
00168
00169 if (transport->address)
00170 _dbus_verbose ("Initialized transport on address %s\n", transport->address);
00171
00172 return TRUE;
00173 }
00174
00181 void
00182 _dbus_transport_finalize_base (DBusTransport *transport)
00183 {
00184 if (!transport->disconnected)
00185 _dbus_transport_disconnect (transport);
00186
00187 if (transport->free_unix_user_data != NULL)
00188 (* transport->free_unix_user_data) (transport->unix_user_data);
00189
00190 _dbus_message_loader_unref (transport->loader);
00191 _dbus_auth_unref (transport->auth);
00192 _dbus_counter_set_notify (transport->live_messages_size,
00193 0, NULL, NULL);
00194 _dbus_counter_unref (transport->live_messages_size);
00195 dbus_free (transport->address);
00196 }
00197
00209 DBusTransport*
00210 _dbus_transport_open (const char *address,
00211 DBusError *error)
00212 {
00213 DBusTransport *transport;
00214 DBusAddressEntry **entries;
00215 DBusError tmp_error;
00216 DBusError first_error;
00217 int len, i;
00218 const char *address_problem_type;
00219 const char *address_problem_field;
00220 const char *address_problem_other;
00221
00222 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
00223
00224 if (!dbus_parse_address (address, &entries, &len, error))
00225 return NULL;
00226
00227 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
00228
00229 transport = NULL;
00230 address_problem_type = NULL;
00231 address_problem_field = NULL;
00232 address_problem_other = NULL;
00233
00234 dbus_error_init (&tmp_error);
00235 dbus_error_init (&first_error);
00236 for (i = 0; i < len; i++)
00237 {
00238 const char *method;
00239
00240 method = dbus_address_entry_get_method (entries[i]);
00241
00242 if (strcmp (method, "unix") == 0)
00243 {
00244 const char *path = dbus_address_entry_get_value (entries[i], "path");
00245 const char *tmpdir = dbus_address_entry_get_value (entries[i], "tmpdir");
00246 const char *abstract = dbus_address_entry_get_value (entries[i], "abstract");
00247
00248 if (tmpdir != NULL)
00249 {
00250 address_problem_other = "cannot use the \"tmpdir\" option for an address to connect to, only in an address to listen on";
00251 goto bad_address;
00252 }
00253
00254 if (path == NULL && abstract == NULL)
00255 {
00256 address_problem_type = "unix";
00257 address_problem_field = "path or abstract";
00258 goto bad_address;
00259 }
00260
00261 if (path != NULL && abstract != NULL)
00262 {
00263 address_problem_other = "can't specify both \"path\" and \"abstract\" options in an address";
00264 goto bad_address;
00265 }
00266
00267 if (path)
00268 transport = _dbus_transport_new_for_domain_socket (path, FALSE,
00269 &tmp_error);
00270 else
00271 transport = _dbus_transport_new_for_domain_socket (abstract, TRUE,
00272 &tmp_error);
00273 }
00274 else if (strcmp (method, "tcp") == 0)
00275 {
00276 const char *host = dbus_address_entry_get_value (entries[i], "host");
00277 const char *port = dbus_address_entry_get_value (entries[i], "port");
00278 DBusString str;
00279 long lport;
00280 dbus_bool_t sresult;
00281
00282 if (port == NULL)
00283 {
00284 address_problem_type = "tcp";
00285 address_problem_field = "port";
00286 goto bad_address;
00287 }
00288
00289 _dbus_string_init_const (&str, port);
00290 sresult = _dbus_string_parse_int (&str, 0, &lport, NULL);
00291 _dbus_string_free (&str);
00292
00293 if (sresult == FALSE || lport <= 0 || lport > 65535)
00294 {
00295 address_problem_other = "Port is not an integer between 0 and 65535";
00296 goto bad_address;
00297 }
00298
00299 transport = _dbus_transport_new_for_tcp_socket (host, lport, &tmp_error);
00300 }
00301 #ifdef DBUS_BUILD_TESTS
00302 else if (strcmp (method, "debug-pipe") == 0)
00303 {
00304 const char *name = dbus_address_entry_get_value (entries[i], "name");
00305
00306 if (name == NULL)
00307 {
00308 address_problem_type = "debug-pipe";
00309 address_problem_field = "name";
00310 goto bad_address;
00311 }
00312
00313 transport = _dbus_transport_debug_pipe_new (name, &tmp_error);
00314 }
00315 #endif
00316 else
00317 {
00318 address_problem_other = "Unknown address type (examples of valid types are \"unix\" and \"tcp\")";
00319 goto bad_address;
00320 }
00321
00322 if (transport)
00323 break;
00324
00325 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
00326
00327 if (i == 0)
00328 dbus_move_error (&tmp_error, &first_error);
00329 else
00330 dbus_error_free (&tmp_error);
00331 }
00332
00333 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
00334 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
00335
00336 if (transport == NULL)
00337 {
00338 _DBUS_ASSERT_ERROR_IS_SET (&first_error);
00339 dbus_move_error (&first_error, error);
00340 }
00341 else
00342 {
00343 dbus_error_free (&first_error);
00344 }
00345
00346 dbus_address_entries_free (entries);
00347 return transport;
00348
00349 bad_address:
00350 dbus_address_entries_free (entries);
00351
00352 if (address_problem_type != NULL)
00353 dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
00354 "Address of type %s was missing argument %s",
00355 address_problem_type, address_problem_field);
00356 else
00357 dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
00358 "Could not parse address: %s",
00359 address_problem_other);
00360
00361 return NULL;
00362 }
00363
00369 void
00370 _dbus_transport_ref (DBusTransport *transport)
00371 {
00372 _dbus_assert (transport->refcount > 0);
00373
00374 transport->refcount += 1;
00375 }
00376
00384 void
00385 _dbus_transport_unref (DBusTransport *transport)
00386 {
00387 _dbus_assert (transport != NULL);
00388 _dbus_assert (transport->refcount > 0);
00389
00390 transport->refcount -= 1;
00391 if (transport->refcount == 0)
00392 {
00393 _dbus_assert (transport->vtable->finalize != NULL);
00394
00395 (* transport->vtable->finalize) (transport);
00396 }
00397 }
00398
00407 void
00408 _dbus_transport_disconnect (DBusTransport *transport)
00409 {
00410 _dbus_assert (transport->vtable->disconnect != NULL);
00411
00412 if (transport->disconnected)
00413 return;
00414
00415 (* transport->vtable->disconnect) (transport);
00416
00417 transport->disconnected = TRUE;
00418 }
00419
00428 dbus_bool_t
00429 _dbus_transport_get_is_connected (DBusTransport *transport)
00430 {
00431 return !transport->disconnected;
00432 }
00433
00443 dbus_bool_t
00444 _dbus_transport_get_is_authenticated (DBusTransport *transport)
00445 {
00446 if (transport->authenticated)
00447 return TRUE;
00448 else
00449 {
00450 dbus_bool_t maybe_authenticated;
00451
00452 if (transport->disconnected)
00453 return FALSE;
00454
00455 maybe_authenticated =
00456 (!(transport->send_credentials_pending ||
00457 transport->receive_credentials_pending));
00458
00459 if (maybe_authenticated)
00460 {
00461 switch (_dbus_auth_do_work (transport->auth))
00462 {
00463 case DBUS_AUTH_STATE_AUTHENTICATED:
00464 case DBUS_AUTH_STATE_AUTHENTICATED_WITH_UNUSED_BYTES:
00465
00466 break;
00467 default:
00468 maybe_authenticated = FALSE;
00469 }
00470 }
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480 if (maybe_authenticated && transport->is_server)
00481 {
00482 DBusCredentials auth_identity;
00483
00484 _dbus_auth_get_identity (transport->auth, &auth_identity);
00485
00486 if (transport->unix_user_function != NULL)
00487 {
00488
00489 if (!(* transport->unix_user_function) (transport->connection,
00490 auth_identity.uid,
00491 transport->unix_user_data))
00492 {
00493 _dbus_verbose ("Client UID "DBUS_UID_FORMAT
00494 " was rejected, disconnecting\n",
00495 auth_identity.uid);
00496 _dbus_transport_disconnect (transport);
00497 return FALSE;
00498 }
00499 else
00500 {
00501 _dbus_verbose ("Client UID "DBUS_UID_FORMAT" authorized\n", auth_identity.uid);
00502 }
00503 }
00504 else
00505 {
00506 DBusCredentials our_identity;
00507
00508 _dbus_credentials_from_current_process (&our_identity);
00509
00510 if (!_dbus_credentials_match (&our_identity,
00511 &auth_identity))
00512 {
00513 _dbus_verbose ("Client authorized as UID "DBUS_UID_FORMAT
00514 " but our UID is "DBUS_UID_FORMAT", disconnecting\n",
00515 auth_identity.uid, our_identity.uid);
00516 _dbus_transport_disconnect (transport);
00517 return FALSE;
00518 }
00519 else
00520 {
00521 _dbus_verbose ("Client authorized as UID "DBUS_UID_FORMAT
00522 " matching our UID "DBUS_UID_FORMAT"\n",
00523 auth_identity.uid, our_identity.uid);
00524 }
00525 }
00526 }
00527
00528 transport->authenticated = maybe_authenticated;
00529
00530 return transport->authenticated;
00531 }
00532 }
00533
00541 const char*
00542 _dbus_transport_get_address (DBusTransport *transport)
00543 {
00544 return transport->address;
00545 }
00546
00556 dbus_bool_t
00557 _dbus_transport_handle_watch (DBusTransport *transport,
00558 DBusWatch *watch,
00559 unsigned int condition)
00560 {
00561 dbus_bool_t retval;
00562
00563 _dbus_assert (transport->vtable->handle_watch != NULL);
00564
00565 if (transport->disconnected)
00566 return TRUE;
00567
00568 if (dbus_watch_get_fd (watch) < 0)
00569 {
00570 _dbus_warn ("Tried to handle an invalidated watch; this watch should have been removed\n");
00571 return TRUE;
00572 }
00573
00574 _dbus_watch_sanitize_condition (watch, &condition);
00575
00576 _dbus_transport_ref (transport);
00577 _dbus_watch_ref (watch);
00578 retval = (* transport->vtable->handle_watch) (transport, watch, condition);
00579 _dbus_watch_unref (watch);
00580 _dbus_transport_unref (transport);
00581
00582 return retval;
00583 }
00584
00594 dbus_bool_t
00595 _dbus_transport_set_connection (DBusTransport *transport,
00596 DBusConnection *connection)
00597 {
00598 _dbus_assert (transport->vtable->connection_set != NULL);
00599 _dbus_assert (transport->connection == NULL);
00600
00601 transport->connection = connection;
00602
00603 _dbus_transport_ref (transport);
00604 if (!(* transport->vtable->connection_set) (transport))
00605 transport->connection = NULL;
00606 _dbus_transport_unref (transport);
00607
00608 return transport->connection != NULL;
00609 }
00610
00620 void
00621 _dbus_transport_messages_pending (DBusTransport *transport,
00622 int queue_length)
00623 {
00624 _dbus_assert (transport->vtable->messages_pending != NULL);
00625
00626 if (transport->disconnected)
00627 return;
00628
00629 transport->messages_need_sending = queue_length > 0;
00630
00631 _dbus_transport_ref (transport);
00632 (* transport->vtable->messages_pending) (transport,
00633 queue_length);
00634 _dbus_transport_unref (transport);
00635 }
00636
00648 void
00649 _dbus_transport_do_iteration (DBusTransport *transport,
00650 unsigned int flags,
00651 int timeout_milliseconds)
00652 {
00653 _dbus_assert (transport->vtable->do_iteration != NULL);
00654
00655 _dbus_verbose ("Transport iteration flags 0x%x timeout %d connected = %d\n",
00656 flags, timeout_milliseconds, !transport->disconnected);
00657
00658 if ((flags & (DBUS_ITERATION_DO_WRITING |
00659 DBUS_ITERATION_DO_READING)) == 0)
00660 return;
00661
00662 if (transport->disconnected)
00663 return;
00664
00665 _dbus_transport_ref (transport);
00666 (* transport->vtable->do_iteration) (transport, flags,
00667 timeout_milliseconds);
00668 _dbus_transport_unref (transport);
00669 }
00670
00671 static dbus_bool_t
00672 recover_unused_bytes (DBusTransport *transport)
00673 {
00674 if (_dbus_auth_do_work (transport->auth) != DBUS_AUTH_STATE_AUTHENTICATED_WITH_UNUSED_BYTES)
00675 return TRUE;
00676
00677 if (_dbus_auth_needs_decoding (transport->auth))
00678 {
00679 DBusString plaintext;
00680 const DBusString *encoded;
00681 DBusString *buffer;
00682 int orig_len;
00683
00684 if (!_dbus_string_init (&plaintext))
00685 goto nomem;
00686
00687 _dbus_auth_get_unused_bytes (transport->auth,
00688 &encoded);
00689
00690 if (!_dbus_auth_decode_data (transport->auth,
00691 encoded, &plaintext))
00692 {
00693 _dbus_string_free (&plaintext);
00694 goto nomem;
00695 }
00696
00697 _dbus_message_loader_get_buffer (transport->loader,
00698 &buffer);
00699
00700 orig_len = _dbus_string_get_length (buffer);
00701
00702 if (!_dbus_string_move (&plaintext, 0, buffer,
00703 orig_len))
00704 {
00705 _dbus_string_free (&plaintext);
00706 goto nomem;
00707 }
00708
00709 _dbus_verbose (" %d unused bytes sent to message loader\n",
00710 _dbus_string_get_length (buffer) -
00711 orig_len);
00712
00713 _dbus_message_loader_return_buffer (transport->loader,
00714 buffer,
00715 _dbus_string_get_length (buffer) -
00716 orig_len);
00717
00718 _dbus_auth_delete_unused_bytes (transport->auth);
00719
00720 _dbus_string_free (&plaintext);
00721 }
00722 else
00723 {
00724 const DBusString *bytes;
00725 DBusString *buffer;
00726 int orig_len;
00727 dbus_bool_t succeeded;
00728
00729 _dbus_message_loader_get_buffer (transport->loader,
00730 &buffer);
00731
00732 orig_len = _dbus_string_get_length (buffer);
00733
00734 _dbus_auth_get_unused_bytes (transport->auth,
00735 &bytes);
00736
00737 succeeded = TRUE;
00738 if (!_dbus_string_copy (bytes, 0, buffer, _dbus_string_get_length (buffer)))
00739 succeeded = FALSE;
00740
00741 _dbus_verbose (" %d unused bytes sent to message loader\n",
00742 _dbus_string_get_length (buffer) -
00743 orig_len);
00744
00745 _dbus_message_loader_return_buffer (transport->loader,
00746 buffer,
00747 _dbus_string_get_length (buffer) -
00748 orig_len);
00749
00750 if (succeeded)
00751 _dbus_auth_delete_unused_bytes (transport->auth);
00752 else
00753 goto nomem;
00754 }
00755
00756 return TRUE;
00757
00758 nomem:
00759 _dbus_verbose ("Not enough memory to transfer unused bytes from auth conversation\n");
00760 return FALSE;
00761 }
00762
00770 DBusDispatchStatus
00771 _dbus_transport_get_dispatch_status (DBusTransport *transport)
00772 {
00773 if (_dbus_counter_get_value (transport->live_messages_size) >= transport->max_live_messages_size)
00774 return DBUS_DISPATCH_COMPLETE;
00775
00776 if (!_dbus_transport_get_is_authenticated (transport))
00777 {
00778 if (_dbus_auth_do_work (transport->auth) ==
00779 DBUS_AUTH_STATE_WAITING_FOR_MEMORY)
00780 return DBUS_DISPATCH_NEED_MEMORY;
00781 else if (!_dbus_transport_get_is_authenticated (transport))
00782 return DBUS_DISPATCH_COMPLETE;
00783 }
00784
00785 if (!transport->unused_bytes_recovered &&
00786 !recover_unused_bytes (transport))
00787 return DBUS_DISPATCH_NEED_MEMORY;
00788
00789 transport->unused_bytes_recovered = TRUE;
00790
00791 if (!_dbus_message_loader_queue_messages (transport->loader))
00792 return DBUS_DISPATCH_NEED_MEMORY;
00793
00794 if (_dbus_message_loader_peek_message (transport->loader) != NULL)
00795 return DBUS_DISPATCH_DATA_REMAINS;
00796 else
00797 return DBUS_DISPATCH_COMPLETE;
00798 }
00799
00808 dbus_bool_t
00809 _dbus_transport_queue_messages (DBusTransport *transport)
00810 {
00811 DBusDispatchStatus status;
00812
00813 #if 0
00814 _dbus_verbose ("_dbus_transport_queue_messages()\n");
00815 #endif
00816
00817
00818 while ((status = _dbus_transport_get_dispatch_status (transport)) == DBUS_DISPATCH_DATA_REMAINS)
00819 {
00820 DBusMessage *message;
00821 DBusList *link;
00822
00823 link = _dbus_message_loader_pop_message_link (transport->loader);
00824 _dbus_assert (link != NULL);
00825
00826 message = link->data;
00827
00828 _dbus_verbose ("queueing received message %p\n", message);
00829
00830 if (!_dbus_message_add_size_counter (message, transport->live_messages_size))
00831 {
00832 _dbus_message_loader_putback_message_link (transport->loader,
00833 link);
00834 status = DBUS_DISPATCH_NEED_MEMORY;
00835 break;
00836 }
00837 else
00838 {
00839
00840 _dbus_connection_queue_received_message_link (transport->connection,
00841 link);
00842 }
00843 }
00844
00845 if (_dbus_message_loader_get_is_corrupted (transport->loader))
00846 {
00847 _dbus_verbose ("Corrupted message stream, disconnecting\n");
00848 _dbus_transport_disconnect (transport);
00849 }
00850
00851 return status != DBUS_DISPATCH_NEED_MEMORY;
00852 }
00853
00860 void
00861 _dbus_transport_set_max_message_size (DBusTransport *transport,
00862 long size)
00863 {
00864 _dbus_message_loader_set_max_message_size (transport->loader, size);
00865 }
00866
00873 long
00874 _dbus_transport_get_max_message_size (DBusTransport *transport)
00875 {
00876 return _dbus_message_loader_get_max_message_size (transport->loader);
00877 }
00878
00885 void
00886 _dbus_transport_set_max_received_size (DBusTransport *transport,
00887 long size)
00888 {
00889 transport->max_live_messages_size = size;
00890 _dbus_counter_set_notify (transport->live_messages_size,
00891 transport->max_live_messages_size,
00892 live_messages_size_notify,
00893 transport);
00894 }
00895
00896
00903 long
00904 _dbus_transport_get_max_received_size (DBusTransport *transport)
00905 {
00906 return transport->max_live_messages_size;
00907 }
00908
00916 dbus_bool_t
00917 _dbus_transport_get_unix_user (DBusTransport *transport,
00918 unsigned long *uid)
00919 {
00920 DBusCredentials auth_identity;
00921
00922 *uid = _DBUS_INT_MAX;
00923
00924
00925
00926
00927 if (!transport->authenticated)
00928 return FALSE;
00929
00930 _dbus_auth_get_identity (transport->auth, &auth_identity);
00931
00932 if (auth_identity.uid != DBUS_UID_UNSET)
00933 {
00934 *uid = auth_identity.uid;
00935 return TRUE;
00936 }
00937 else
00938 return FALSE;
00939 }
00940
00951 void
00952 _dbus_transport_set_unix_user_function (DBusTransport *transport,
00953 DBusAllowUnixUserFunction function,
00954 void *data,
00955 DBusFreeFunction free_data_function,
00956 void **old_data,
00957 DBusFreeFunction *old_free_data_function)
00958 {
00959 *old_data = transport->unix_user_data;
00960 *old_free_data_function = transport->free_unix_user_data;
00961
00962 transport->unix_user_function = function;
00963 transport->unix_user_data = data;
00964 transport->free_unix_user_data = free_data_function;
00965 }
00966
00975 dbus_bool_t
00976 _dbus_transport_set_auth_mechanisms (DBusTransport *transport,
00977 const char **mechanisms)
00978 {
00979 return _dbus_auth_set_mechanisms (transport->auth, mechanisms);
00980 }
00981
00982