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
00370 DBusTransport *
00371 _dbus_transport_ref (DBusTransport *transport)
00372 {
00373 _dbus_assert (transport->refcount > 0);
00374
00375 transport->refcount += 1;
00376
00377 return transport;
00378 }
00379
00387 void
00388 _dbus_transport_unref (DBusTransport *transport)
00389 {
00390 _dbus_assert (transport != NULL);
00391 _dbus_assert (transport->refcount > 0);
00392
00393 transport->refcount -= 1;
00394 if (transport->refcount == 0)
00395 {
00396 _dbus_assert (transport->vtable->finalize != NULL);
00397
00398 (* transport->vtable->finalize) (transport);
00399 }
00400 }
00401
00410 void
00411 _dbus_transport_disconnect (DBusTransport *transport)
00412 {
00413 _dbus_assert (transport->vtable->disconnect != NULL);
00414
00415 if (transport->disconnected)
00416 return;
00417
00418 (* transport->vtable->disconnect) (transport);
00419
00420 transport->disconnected = TRUE;
00421 }
00422
00431 dbus_bool_t
00432 _dbus_transport_get_is_connected (DBusTransport *transport)
00433 {
00434 return !transport->disconnected;
00435 }
00436
00446 dbus_bool_t
00447 _dbus_transport_get_is_authenticated (DBusTransport *transport)
00448 {
00449 if (transport->authenticated)
00450 return TRUE;
00451 else
00452 {
00453 dbus_bool_t maybe_authenticated;
00454
00455 if (transport->disconnected)
00456 return FALSE;
00457
00458 maybe_authenticated =
00459 (!(transport->send_credentials_pending ||
00460 transport->receive_credentials_pending));
00461
00462 if (maybe_authenticated)
00463 {
00464 switch (_dbus_auth_do_work (transport->auth))
00465 {
00466 case DBUS_AUTH_STATE_AUTHENTICATED:
00467 case DBUS_AUTH_STATE_AUTHENTICATED_WITH_UNUSED_BYTES:
00468
00469 break;
00470 default:
00471 maybe_authenticated = FALSE;
00472 }
00473 }
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483 if (maybe_authenticated && transport->is_server)
00484 {
00485 DBusCredentials auth_identity;
00486
00487 _dbus_auth_get_identity (transport->auth, &auth_identity);
00488
00489 if (transport->unix_user_function != NULL)
00490 {
00491
00492 if (!(* transport->unix_user_function) (transport->connection,
00493 auth_identity.uid,
00494 transport->unix_user_data))
00495 {
00496 _dbus_verbose ("Client UID "DBUS_UID_FORMAT
00497 " was rejected, disconnecting\n",
00498 auth_identity.uid);
00499 _dbus_transport_disconnect (transport);
00500 return FALSE;
00501 }
00502 else
00503 {
00504 _dbus_verbose ("Client UID "DBUS_UID_FORMAT" authorized\n", auth_identity.uid);
00505 }
00506 }
00507 else
00508 {
00509 DBusCredentials our_identity;
00510
00511 _dbus_credentials_from_current_process (&our_identity);
00512
00513 if (!_dbus_credentials_match (&our_identity,
00514 &auth_identity))
00515 {
00516 _dbus_verbose ("Client authorized as UID "DBUS_UID_FORMAT
00517 " but our UID is "DBUS_UID_FORMAT", disconnecting\n",
00518 auth_identity.uid, our_identity.uid);
00519 _dbus_transport_disconnect (transport);
00520 return FALSE;
00521 }
00522 else
00523 {
00524 _dbus_verbose ("Client authorized as UID "DBUS_UID_FORMAT
00525 " matching our UID "DBUS_UID_FORMAT"\n",
00526 auth_identity.uid, our_identity.uid);
00527 }
00528 }
00529 }
00530
00531 transport->authenticated = maybe_authenticated;
00532
00533 return transport->authenticated;
00534 }
00535 }
00536
00544 const char*
00545 _dbus_transport_get_address (DBusTransport *transport)
00546 {
00547 return transport->address;
00548 }
00549
00559 dbus_bool_t
00560 _dbus_transport_handle_watch (DBusTransport *transport,
00561 DBusWatch *watch,
00562 unsigned int condition)
00563 {
00564 dbus_bool_t retval;
00565
00566 _dbus_assert (transport->vtable->handle_watch != NULL);
00567
00568 if (transport->disconnected)
00569 return TRUE;
00570
00571 if (dbus_watch_get_fd (watch) < 0)
00572 {
00573 _dbus_warn ("Tried to handle an invalidated watch; this watch should have been removed\n");
00574 return TRUE;
00575 }
00576
00577 _dbus_watch_sanitize_condition (watch, &condition);
00578
00579 _dbus_transport_ref (transport);
00580 _dbus_watch_ref (watch);
00581 retval = (* transport->vtable->handle_watch) (transport, watch, condition);
00582 _dbus_watch_unref (watch);
00583 _dbus_transport_unref (transport);
00584
00585 return retval;
00586 }
00587
00597 dbus_bool_t
00598 _dbus_transport_set_connection (DBusTransport *transport,
00599 DBusConnection *connection)
00600 {
00601 _dbus_assert (transport->vtable->connection_set != NULL);
00602 _dbus_assert (transport->connection == NULL);
00603
00604 transport->connection = connection;
00605
00606 _dbus_transport_ref (transport);
00607 if (!(* transport->vtable->connection_set) (transport))
00608 transport->connection = NULL;
00609 _dbus_transport_unref (transport);
00610
00611 return transport->connection != NULL;
00612 }
00613
00623 void
00624 _dbus_transport_messages_pending (DBusTransport *transport,
00625 int queue_length)
00626 {
00627 _dbus_assert (transport->vtable->messages_pending != NULL);
00628
00629 if (transport->disconnected)
00630 return;
00631
00632 transport->messages_need_sending = queue_length > 0;
00633
00634 _dbus_transport_ref (transport);
00635 (* transport->vtable->messages_pending) (transport,
00636 queue_length);
00637 _dbus_transport_unref (transport);
00638 }
00639
00651 void
00652 _dbus_transport_do_iteration (DBusTransport *transport,
00653 unsigned int flags,
00654 int timeout_milliseconds)
00655 {
00656 _dbus_assert (transport->vtable->do_iteration != NULL);
00657
00658 _dbus_verbose ("Transport iteration flags 0x%x timeout %d connected = %d\n",
00659 flags, timeout_milliseconds, !transport->disconnected);
00660
00661 if ((flags & (DBUS_ITERATION_DO_WRITING |
00662 DBUS_ITERATION_DO_READING)) == 0)
00663 return;
00664
00665 if (transport->disconnected)
00666 return;
00667
00668 _dbus_transport_ref (transport);
00669 (* transport->vtable->do_iteration) (transport, flags,
00670 timeout_milliseconds);
00671 _dbus_transport_unref (transport);
00672 }
00673
00674 static dbus_bool_t
00675 recover_unused_bytes (DBusTransport *transport)
00676 {
00677 if (_dbus_auth_do_work (transport->auth) != DBUS_AUTH_STATE_AUTHENTICATED_WITH_UNUSED_BYTES)
00678 return TRUE;
00679
00680 if (_dbus_auth_needs_decoding (transport->auth))
00681 {
00682 DBusString plaintext;
00683 const DBusString *encoded;
00684 DBusString *buffer;
00685 int orig_len;
00686
00687 if (!_dbus_string_init (&plaintext))
00688 goto nomem;
00689
00690 _dbus_auth_get_unused_bytes (transport->auth,
00691 &encoded);
00692
00693 if (!_dbus_auth_decode_data (transport->auth,
00694 encoded, &plaintext))
00695 {
00696 _dbus_string_free (&plaintext);
00697 goto nomem;
00698 }
00699
00700 _dbus_message_loader_get_buffer (transport->loader,
00701 &buffer);
00702
00703 orig_len = _dbus_string_get_length (buffer);
00704
00705 if (!_dbus_string_move (&plaintext, 0, buffer,
00706 orig_len))
00707 {
00708 _dbus_string_free (&plaintext);
00709 goto nomem;
00710 }
00711
00712 _dbus_verbose (" %d unused bytes sent to message loader\n",
00713 _dbus_string_get_length (buffer) -
00714 orig_len);
00715
00716 _dbus_message_loader_return_buffer (transport->loader,
00717 buffer,
00718 _dbus_string_get_length (buffer) -
00719 orig_len);
00720
00721 _dbus_auth_delete_unused_bytes (transport->auth);
00722
00723 _dbus_string_free (&plaintext);
00724 }
00725 else
00726 {
00727 const DBusString *bytes;
00728 DBusString *buffer;
00729 int orig_len;
00730 dbus_bool_t succeeded;
00731
00732 _dbus_message_loader_get_buffer (transport->loader,
00733 &buffer);
00734
00735 orig_len = _dbus_string_get_length (buffer);
00736
00737 _dbus_auth_get_unused_bytes (transport->auth,
00738 &bytes);
00739
00740 succeeded = TRUE;
00741 if (!_dbus_string_copy (bytes, 0, buffer, _dbus_string_get_length (buffer)))
00742 succeeded = FALSE;
00743
00744 _dbus_verbose (" %d unused bytes sent to message loader\n",
00745 _dbus_string_get_length (buffer) -
00746 orig_len);
00747
00748 _dbus_message_loader_return_buffer (transport->loader,
00749 buffer,
00750 _dbus_string_get_length (buffer) -
00751 orig_len);
00752
00753 if (succeeded)
00754 _dbus_auth_delete_unused_bytes (transport->auth);
00755 else
00756 goto nomem;
00757 }
00758
00759 return TRUE;
00760
00761 nomem:
00762 _dbus_verbose ("Not enough memory to transfer unused bytes from auth conversation\n");
00763 return FALSE;
00764 }
00765
00773 DBusDispatchStatus
00774 _dbus_transport_get_dispatch_status (DBusTransport *transport)
00775 {
00776 if (_dbus_counter_get_value (transport->live_messages_size) >= transport->max_live_messages_size)
00777 return DBUS_DISPATCH_COMPLETE;
00778
00779 if (!_dbus_transport_get_is_authenticated (transport))
00780 {
00781 if (_dbus_auth_do_work (transport->auth) ==
00782 DBUS_AUTH_STATE_WAITING_FOR_MEMORY)
00783 return DBUS_DISPATCH_NEED_MEMORY;
00784 else if (!_dbus_transport_get_is_authenticated (transport))
00785 return DBUS_DISPATCH_COMPLETE;
00786 }
00787
00788 if (!transport->unused_bytes_recovered &&
00789 !recover_unused_bytes (transport))
00790 return DBUS_DISPATCH_NEED_MEMORY;
00791
00792 transport->unused_bytes_recovered = TRUE;
00793
00794 if (!_dbus_message_loader_queue_messages (transport->loader))
00795 return DBUS_DISPATCH_NEED_MEMORY;
00796
00797 if (_dbus_message_loader_peek_message (transport->loader) != NULL)
00798 return DBUS_DISPATCH_DATA_REMAINS;
00799 else
00800 return DBUS_DISPATCH_COMPLETE;
00801 }
00802
00811 dbus_bool_t
00812 _dbus_transport_queue_messages (DBusTransport *transport)
00813 {
00814 DBusDispatchStatus status;
00815
00816 #if 0
00817 _dbus_verbose ("_dbus_transport_queue_messages()\n");
00818 #endif
00819
00820
00821 while ((status = _dbus_transport_get_dispatch_status (transport)) == DBUS_DISPATCH_DATA_REMAINS)
00822 {
00823 DBusMessage *message;
00824 DBusList *link;
00825
00826 link = _dbus_message_loader_pop_message_link (transport->loader);
00827 _dbus_assert (link != NULL);
00828
00829 message = link->data;
00830
00831 _dbus_verbose ("queueing received message %p\n", message);
00832
00833 if (!_dbus_message_add_size_counter (message, transport->live_messages_size))
00834 {
00835 _dbus_message_loader_putback_message_link (transport->loader,
00836 link);
00837 status = DBUS_DISPATCH_NEED_MEMORY;
00838 break;
00839 }
00840 else
00841 {
00842
00843 _dbus_connection_queue_received_message_link (transport->connection,
00844 link);
00845 }
00846 }
00847
00848 if (_dbus_message_loader_get_is_corrupted (transport->loader))
00849 {
00850 _dbus_verbose ("Corrupted message stream, disconnecting\n");
00851 _dbus_transport_disconnect (transport);
00852 }
00853
00854 return status != DBUS_DISPATCH_NEED_MEMORY;
00855 }
00856
00863 void
00864 _dbus_transport_set_max_message_size (DBusTransport *transport,
00865 long size)
00866 {
00867 _dbus_message_loader_set_max_message_size (transport->loader, size);
00868 }
00869
00876 long
00877 _dbus_transport_get_max_message_size (DBusTransport *transport)
00878 {
00879 return _dbus_message_loader_get_max_message_size (transport->loader);
00880 }
00881
00888 void
00889 _dbus_transport_set_max_received_size (DBusTransport *transport,
00890 long size)
00891 {
00892 transport->max_live_messages_size = size;
00893 _dbus_counter_set_notify (transport->live_messages_size,
00894 transport->max_live_messages_size,
00895 live_messages_size_notify,
00896 transport);
00897 }
00898
00899
00906 long
00907 _dbus_transport_get_max_received_size (DBusTransport *transport)
00908 {
00909 return transport->max_live_messages_size;
00910 }
00911
00919 dbus_bool_t
00920 _dbus_transport_get_unix_user (DBusTransport *transport,
00921 unsigned long *uid)
00922 {
00923 DBusCredentials auth_identity;
00924
00925 *uid = _DBUS_INT_MAX;
00926
00927
00928
00929
00930 if (!transport->authenticated)
00931 return FALSE;
00932
00933 _dbus_auth_get_identity (transport->auth, &auth_identity);
00934
00935 if (auth_identity.uid != DBUS_UID_UNSET)
00936 {
00937 *uid = auth_identity.uid;
00938 return TRUE;
00939 }
00940 else
00941 return FALSE;
00942 }
00943
00954 void
00955 _dbus_transport_set_unix_user_function (DBusTransport *transport,
00956 DBusAllowUnixUserFunction function,
00957 void *data,
00958 DBusFreeFunction free_data_function,
00959 void **old_data,
00960 DBusFreeFunction *old_free_data_function)
00961 {
00962 *old_data = transport->unix_user_data;
00963 *old_free_data_function = transport->free_unix_user_data;
00964
00965 transport->unix_user_function = function;
00966 transport->unix_user_data = data;
00967 transport->free_unix_user_data = free_data_function;
00968 }
00969
00978 dbus_bool_t
00979 _dbus_transport_set_auth_mechanisms (DBusTransport *transport,
00980 const char **mechanisms)
00981 {
00982 return _dbus_auth_set_mechanisms (transport->auth, mechanisms);
00983 }
00984
00985