dbus-transport.c

00001 /* -*- mode: C; c-file-style: "gnu" -*- */
00002 /* dbus-transport.c DBusTransport object (internal to D-Bus implementation)
00003  *
00004  * Copyright (C) 2002, 2003  Red Hat Inc.
00005  *
00006  * Licensed under the Academic Free License version 2.1
00007  * 
00008  * This program is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * This program is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  * 
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  *
00022  */
00023 
00024 #include "dbus-transport-protected.h"
00025 #include "dbus-transport-unix.h"
00026 #include "dbus-transport-socket.h"
00027 #include "dbus-connection-internal.h"
00028 #include "dbus-watch.h"
00029 #include "dbus-auth.h"
00030 #include "dbus-address.h"
00031 #ifdef DBUS_BUILD_TESTS
00032 #include "dbus-server-debug-pipe.h"
00033 #endif
00034 
00056 static void
00057 live_messages_size_notify (DBusCounter *counter,
00058                            void        *user_data)
00059 {
00060   DBusTransport *transport = user_data;
00061 
00062   _dbus_transport_ref (transport);
00063 
00064 #if 0
00065   _dbus_verbose ("Counter value is now %d\n",
00066                  (int) _dbus_counter_get_value (counter));
00067 #endif
00068   
00069   /* disable or re-enable the read watch for the transport if
00070    * required.
00071    */
00072   if (transport->vtable->live_messages_changed)
00073     (* transport->vtable->live_messages_changed) (transport);
00074 
00075   _dbus_transport_unref (transport);
00076 }
00077 
00091 dbus_bool_t
00092 _dbus_transport_init_base (DBusTransport             *transport,
00093                            const DBusTransportVTable *vtable,
00094                            const DBusString          *server_guid,
00095                            const DBusString          *address)
00096 {
00097   DBusMessageLoader *loader;
00098   DBusAuth *auth;
00099   DBusCounter *counter;
00100   char *address_copy;
00101   
00102   loader = _dbus_message_loader_new ();
00103   if (loader == NULL)
00104     return FALSE;
00105   
00106   if (server_guid)
00107     auth = _dbus_auth_server_new (server_guid);
00108   else
00109     auth = _dbus_auth_client_new ();
00110   if (auth == NULL)
00111     {
00112       _dbus_message_loader_unref (loader);
00113       return FALSE;
00114     }
00115 
00116   counter = _dbus_counter_new ();
00117   if (counter == NULL)
00118     {
00119       _dbus_auth_unref (auth);
00120       _dbus_message_loader_unref (loader);
00121       return FALSE;
00122     }  
00123   
00124   if (server_guid)
00125     {
00126       _dbus_assert (address == NULL);
00127       address_copy = NULL;
00128     }
00129   else
00130     {
00131       _dbus_assert (address != NULL);
00132 
00133       if (!_dbus_string_copy_data (address, &address_copy))
00134         {
00135           _dbus_counter_unref (counter);
00136           _dbus_auth_unref (auth);
00137           _dbus_message_loader_unref (loader);
00138           return FALSE;
00139         }
00140     }
00141   
00142   transport->refcount = 1;
00143   transport->vtable = vtable;
00144   transport->loader = loader;
00145   transport->auth = auth;
00146   transport->live_messages_size = counter;
00147   transport->authenticated = FALSE;
00148   transport->disconnected = FALSE;
00149   transport->is_server = (server_guid != NULL);
00150   transport->send_credentials_pending = !transport->is_server;
00151   transport->receive_credentials_pending = transport->is_server;
00152   transport->address = address_copy;
00153   
00154   transport->unix_user_function = NULL;
00155   transport->unix_user_data = NULL;
00156   transport->free_unix_user_data = NULL;
00157 
00158   transport->expected_guid = NULL;
00159   
00160   /* Try to default to something that won't totally hose the system,
00161    * but doesn't impose too much of a limitation.
00162    */
00163   transport->max_live_messages_size = _DBUS_ONE_MEGABYTE * 63;
00164   
00165   transport->credentials.pid = -1;
00166   transport->credentials.uid = -1;
00167   transport->credentials.gid = -1;
00168 
00169   _dbus_counter_set_notify (transport->live_messages_size,
00170                             transport->max_live_messages_size,
00171                             live_messages_size_notify,
00172                             transport);
00173 
00174   if (transport->address)
00175     _dbus_verbose ("Initialized transport on address %s\n", transport->address);
00176   
00177   return TRUE;
00178 }
00179 
00186 void
00187 _dbus_transport_finalize_base (DBusTransport *transport)
00188 {
00189   if (!transport->disconnected)
00190     _dbus_transport_disconnect (transport);
00191 
00192   if (transport->free_unix_user_data != NULL)
00193     (* transport->free_unix_user_data) (transport->unix_user_data);
00194   
00195   _dbus_message_loader_unref (transport->loader);
00196   _dbus_auth_unref (transport->auth);
00197   _dbus_counter_set_notify (transport->live_messages_size,
00198                             0, NULL, NULL);
00199   _dbus_counter_unref (transport->live_messages_size);
00200   dbus_free (transport->address);
00201   dbus_free (transport->expected_guid);
00202 }
00203 
00204 
00214 static DBusTransport*
00215 check_address (const char *address, DBusError *error)
00216 {
00217   DBusAddressEntry **entries;
00218   DBusTransport *transport = NULL;
00219   int len, i;
00220 
00221   _dbus_assert (address != NULL);
00222   _dbus_assert (*address != '\0');
00223 
00224   if (!dbus_parse_address (address, &entries, &len, error))
00225     return FALSE;              /* not a valid address */
00226 
00227   for (i = 0; i < len; i++)
00228     {
00229       transport = _dbus_transport_open (entries[i], error);
00230       if (transport != NULL)
00231         break;
00232     }
00233 
00234   dbus_address_entries_free (entries);
00235   return transport;
00236 }
00237 
00245 static DBusTransport*
00246 _dbus_transport_new_for_autolaunch (DBusError      *error)
00247 {
00248   DBusString address;
00249   DBusTransport *result = NULL;
00250 
00251   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
00252 
00253   if (!_dbus_string_init (&address))
00254     {
00255       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
00256       return NULL;
00257     }
00258 
00259   if (!_dbus_get_autolaunch_address (&address, error))
00260     {
00261       _DBUS_ASSERT_ERROR_IS_SET (error);
00262       goto out;
00263     }
00264 
00265   result = check_address (_dbus_string_get_const_data (&address), error);
00266   if (result == NULL)
00267     _DBUS_ASSERT_ERROR_IS_SET (error);
00268   else
00269     _DBUS_ASSERT_ERROR_IS_CLEAR (error);
00270 
00271  out:
00272   _dbus_string_free (&address);
00273   return result;
00274 }
00275 
00276 static DBusTransportOpenResult
00277 _dbus_transport_open_autolaunch (DBusAddressEntry  *entry,
00278                                  DBusTransport    **transport_p,
00279                                  DBusError         *error)
00280 {
00281   const char *method;
00282   
00283   method = dbus_address_entry_get_method (entry);
00284   _dbus_assert (method != NULL);
00285 
00286   if (strcmp (method, "autolaunch") == 0)
00287     {
00288       *transport_p = _dbus_transport_new_for_autolaunch (error);
00289 
00290       if (*transport_p == NULL)
00291         {
00292           _DBUS_ASSERT_ERROR_IS_SET (error);
00293           return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
00294         }
00295       else
00296         {
00297           _DBUS_ASSERT_ERROR_IS_CLEAR (error);
00298           return DBUS_TRANSPORT_OPEN_OK;
00299         }      
00300     }
00301   else
00302     {
00303       _DBUS_ASSERT_ERROR_IS_CLEAR (error);
00304       return DBUS_TRANSPORT_OPEN_NOT_HANDLED;
00305     }
00306 }
00307 
00308 static const struct {
00309   DBusTransportOpenResult (* func) (DBusAddressEntry *entry,
00310                                     DBusTransport   **transport_p,
00311                                     DBusError        *error);
00312 } open_funcs[] = {
00313   { _dbus_transport_open_socket },
00314   { _dbus_transport_open_platform_specific },
00315   { _dbus_transport_open_autolaunch }
00316 #ifdef DBUS_BUILD_TESTS
00317   , { _dbus_transport_open_debug_pipe }
00318 #endif
00319 };
00320 
00329 DBusTransport*
00330 _dbus_transport_open (DBusAddressEntry *entry,
00331                       DBusError        *error)
00332 {
00333   DBusTransport *transport;
00334   const char *expected_guid_orig;
00335   char *expected_guid;
00336   int i;
00337   DBusError tmp_error;
00338   
00339   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
00340   
00341   transport = NULL;
00342   expected_guid_orig = dbus_address_entry_get_value (entry, "guid");
00343   expected_guid = _dbus_strdup (expected_guid_orig);
00344 
00345   if (expected_guid_orig != NULL && expected_guid == NULL)
00346     {
00347       _DBUS_SET_OOM (error);
00348       return NULL;
00349     }
00350 
00351   dbus_error_init (&tmp_error);
00352   for (i = 0; i < (int) _DBUS_N_ELEMENTS (open_funcs); ++i)
00353     {
00354       DBusTransportOpenResult result;
00355 
00356       _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
00357       result = (* open_funcs[i].func) (entry, &transport, &tmp_error);
00358 
00359       switch (result)
00360         {
00361         case DBUS_TRANSPORT_OPEN_OK:
00362           _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
00363           goto out;
00364           break;
00365         case DBUS_TRANSPORT_OPEN_NOT_HANDLED:
00366           _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
00367           /* keep going through the loop of open funcs */
00368           break;
00369         case DBUS_TRANSPORT_OPEN_BAD_ADDRESS:
00370           _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
00371           goto out;
00372           break;
00373         case DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT:
00374           _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
00375           goto out;
00376           break;
00377         }
00378     }
00379 
00380  out:
00381   
00382   if (transport == NULL)
00383     {
00384       if (!dbus_error_is_set (&tmp_error))
00385         _dbus_set_bad_address (&tmp_error,
00386                                NULL, NULL,
00387                                "Unknown address type (examples of valid types are \"tcp\" and on UNIX \"unix\")");
00388       
00389       _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
00390       dbus_move_error(&tmp_error, error);
00391       dbus_free (expected_guid);
00392     }
00393   else
00394     {
00395       _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
00396       transport->expected_guid = expected_guid;
00397     }
00398 
00399   return transport;
00400 }
00401 
00408 DBusTransport *
00409 _dbus_transport_ref (DBusTransport *transport)
00410 {
00411   _dbus_assert (transport->refcount > 0);
00412   
00413   transport->refcount += 1;
00414 
00415   return transport;
00416 }
00417 
00425 void
00426 _dbus_transport_unref (DBusTransport *transport)
00427 {
00428   _dbus_assert (transport != NULL);
00429   _dbus_assert (transport->refcount > 0);
00430   
00431   transport->refcount -= 1;
00432   if (transport->refcount == 0)
00433     {
00434       _dbus_verbose ("%s: finalizing\n", _DBUS_FUNCTION_NAME);
00435       
00436       _dbus_assert (transport->vtable->finalize != NULL);
00437       
00438       (* transport->vtable->finalize) (transport);
00439     }
00440 }
00441 
00450 void
00451 _dbus_transport_disconnect (DBusTransport *transport)
00452 {
00453   _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
00454   
00455   _dbus_assert (transport->vtable->disconnect != NULL);
00456   
00457   if (transport->disconnected)
00458     return;
00459 
00460   (* transport->vtable->disconnect) (transport);
00461   
00462   transport->disconnected = TRUE;
00463 
00464   _dbus_verbose ("%s end\n", _DBUS_FUNCTION_NAME);
00465 }
00466 
00475 dbus_bool_t
00476 _dbus_transport_get_is_connected (DBusTransport *transport)
00477 {
00478   return !transport->disconnected;
00479 }
00480 
00491 dbus_bool_t
00492 _dbus_transport_get_is_authenticated (DBusTransport *transport)
00493 {
00494   /* We don't want to run unix_user_function on Windows, but it
00495    * can exist, which allows application code to just unconditionally
00496    * set it and have it only be invoked when appropriate.
00497    */
00498   dbus_bool_t on_windows = FALSE;
00499 #ifdef DBUS_WIN
00500   on_windows = TRUE;
00501 #endif
00502   
00503   if (transport->authenticated)
00504     return TRUE;
00505   else
00506     {
00507       dbus_bool_t maybe_authenticated;
00508       
00509       if (transport->disconnected)
00510         return FALSE;
00511 
00512       /* paranoia ref since we call user callbacks sometimes */
00513       _dbus_connection_ref_unlocked (transport->connection);
00514       
00515       maybe_authenticated =
00516         (!(transport->send_credentials_pending ||
00517            transport->receive_credentials_pending));
00518 
00519       if (maybe_authenticated)
00520         {
00521           switch (_dbus_auth_do_work (transport->auth))
00522             {
00523             case DBUS_AUTH_STATE_AUTHENTICATED:
00524               /* leave as maybe_authenticated */
00525               break;
00526             default:
00527               maybe_authenticated = FALSE;
00528             }
00529         }
00530 
00531       if (maybe_authenticated && !transport->is_server)
00532         {
00533           const char *server_guid;
00534 
00535           server_guid = _dbus_auth_get_guid_from_server (transport->auth);
00536           _dbus_assert (server_guid != NULL);
00537 
00538           if (transport->expected_guid &&
00539               strcmp (transport->expected_guid, server_guid) != 0)
00540             {
00541               _dbus_verbose ("Client expected GUID '%s' and we got '%s' from the server\n",
00542                              transport->expected_guid, server_guid);
00543               _dbus_transport_disconnect (transport);
00544               _dbus_connection_unref_unlocked (transport->connection);
00545               return FALSE;
00546             }
00547 
00548           if (transport->expected_guid == NULL)
00549             {
00550               transport->expected_guid = _dbus_strdup (server_guid);
00551 
00552               if (transport->expected_guid == NULL)
00553                 {
00554                   _dbus_verbose ("No memory to complete auth in %s\n", _DBUS_FUNCTION_NAME);
00555                   return FALSE;
00556                 }
00557             }
00558         }
00559       
00560       /* If we've authenticated as some identity, check that the auth
00561        * identity is the same as our own identity.  In the future, we
00562        * may have API allowing applications to specify how this is
00563        * done, for example they may allow connection as any identity,
00564        * but then impose restrictions on certain identities.
00565        * Or they may give certain identities extra privileges.
00566        */
00567       
00568       if (maybe_authenticated && transport->is_server)
00569         {
00570           DBusCredentials auth_identity;
00571 
00572           _dbus_auth_get_identity (transport->auth, &auth_identity);
00573 
00574           if (transport->unix_user_function != NULL && !on_windows)
00575             {
00576               dbus_bool_t allow;
00577               DBusConnection *connection;
00578               DBusAllowUnixUserFunction unix_user_function;
00579               void *unix_user_data;
00580               
00581               /* Dropping the lock here probably isn't that safe. */
00582 
00583               connection = transport->connection;
00584               unix_user_function = transport->unix_user_function;
00585               unix_user_data = transport->unix_user_data;
00586 
00587               _dbus_verbose ("unlock %s\n", _DBUS_FUNCTION_NAME);
00588               _dbus_connection_unlock (connection);
00589 
00590               allow = (* unix_user_function) (connection,
00591                                               auth_identity.uid,
00592                                               unix_user_data);
00593               
00594               _dbus_verbose ("lock %s post unix user function\n", _DBUS_FUNCTION_NAME);
00595               _dbus_connection_lock (connection);
00596 
00597               if (allow)
00598                 {
00599                   _dbus_verbose ("Client UID "DBUS_UID_FORMAT" authorized\n", auth_identity.uid);
00600                 }
00601               else
00602                 {
00603                   _dbus_verbose ("Client UID "DBUS_UID_FORMAT
00604                                  " was rejected, disconnecting\n",
00605                                  auth_identity.uid);
00606                   _dbus_transport_disconnect (transport);
00607                   _dbus_connection_unref_unlocked (connection);
00608                   return FALSE;
00609                 }
00610             }
00611           else
00612             {
00613               DBusCredentials our_identity;
00614               
00615               _dbus_credentials_from_current_process (&our_identity);
00616               
00617               if (!_dbus_credentials_match (&our_identity,
00618                                             &auth_identity))
00619                 {
00620                   _dbus_verbose ("Client authorized as UID "DBUS_UID_FORMAT
00621                                  " but our UID is "DBUS_UID_FORMAT", disconnecting\n",
00622                                  auth_identity.uid, our_identity.uid);
00623                   _dbus_transport_disconnect (transport);
00624                   _dbus_connection_unref_unlocked (transport->connection);
00625                   return FALSE;
00626                 }
00627               else
00628                 {
00629                   _dbus_verbose ("Client authorized as UID "DBUS_UID_FORMAT
00630                                  " matching our UID "DBUS_UID_FORMAT"\n",
00631                                  auth_identity.uid, our_identity.uid);
00632                 }
00633             }
00634         }
00635       
00636       transport->authenticated = maybe_authenticated;
00637 
00638       _dbus_connection_unref_unlocked (transport->connection);
00639       return maybe_authenticated;
00640     }
00641 }
00642 
00650 const char*
00651 _dbus_transport_get_address (DBusTransport *transport)
00652 {
00653   return transport->address;
00654 }
00655 
00665 dbus_bool_t
00666 _dbus_transport_handle_watch (DBusTransport           *transport,
00667                               DBusWatch               *watch,
00668                               unsigned int             condition)
00669 {
00670   dbus_bool_t retval;
00671   
00672   _dbus_assert (transport->vtable->handle_watch != NULL);
00673 
00674   if (transport->disconnected)
00675     return TRUE;
00676 
00677   if (dbus_watch_get_fd (watch) < 0)
00678     {
00679       _dbus_warn_check_failed ("Tried to handle an invalidated watch; this watch should have been removed\n");
00680       return TRUE;
00681     }
00682   
00683   _dbus_watch_sanitize_condition (watch, &condition);
00684 
00685   _dbus_transport_ref (transport);
00686   _dbus_watch_ref (watch);
00687   retval = (* transport->vtable->handle_watch) (transport, watch, condition);
00688   _dbus_watch_unref (watch);
00689   _dbus_transport_unref (transport);
00690 
00691   return retval;
00692 }
00693 
00703 dbus_bool_t
00704 _dbus_transport_set_connection (DBusTransport  *transport,
00705                                 DBusConnection *connection)
00706 {
00707   _dbus_assert (transport->vtable->connection_set != NULL);
00708   _dbus_assert (transport->connection == NULL);
00709   
00710   transport->connection = connection;
00711 
00712   _dbus_transport_ref (transport);
00713   if (!(* transport->vtable->connection_set) (transport))
00714     transport->connection = NULL;
00715   _dbus_transport_unref (transport);
00716 
00717   return transport->connection != NULL;
00718 }
00719 
00727 dbus_bool_t
00728 _dbus_transport_get_socket_fd (DBusTransport *transport,
00729                                int           *fd_p)
00730 {
00731   dbus_bool_t retval;
00732   
00733   if (transport->vtable->get_socket_fd == NULL)
00734     return FALSE;
00735 
00736   if (transport->disconnected)
00737     return FALSE;
00738 
00739   _dbus_transport_ref (transport);
00740 
00741   retval = (* transport->vtable->get_socket_fd) (transport,
00742                                                  fd_p);
00743   
00744   _dbus_transport_unref (transport);
00745 
00746   return retval;
00747 }
00748 
00760 void
00761 _dbus_transport_do_iteration (DBusTransport  *transport,
00762                               unsigned int    flags,
00763                               int             timeout_milliseconds)
00764 {
00765   _dbus_assert (transport->vtable->do_iteration != NULL);
00766 
00767   _dbus_verbose ("Transport iteration flags 0x%x timeout %d connected = %d\n",
00768                  flags, timeout_milliseconds, !transport->disconnected);
00769   
00770   if ((flags & (DBUS_ITERATION_DO_WRITING |
00771                 DBUS_ITERATION_DO_READING)) == 0)
00772     return; /* Nothing to do */
00773 
00774   if (transport->disconnected)
00775     return;
00776 
00777   _dbus_transport_ref (transport);
00778   (* transport->vtable->do_iteration) (transport, flags,
00779                                        timeout_milliseconds);
00780   _dbus_transport_unref (transport);
00781 
00782   _dbus_verbose ("%s end\n", _DBUS_FUNCTION_NAME);
00783 }
00784 
00785 static dbus_bool_t
00786 recover_unused_bytes (DBusTransport *transport)
00787 {
00788   if (_dbus_auth_needs_decoding (transport->auth))
00789     {
00790       DBusString plaintext;
00791       const DBusString *encoded;
00792       DBusString *buffer;
00793       int orig_len;
00794       
00795       if (!_dbus_string_init (&plaintext))
00796         goto nomem;
00797       
00798       _dbus_auth_get_unused_bytes (transport->auth,
00799                                    &encoded);
00800 
00801       if (!_dbus_auth_decode_data (transport->auth,
00802                                    encoded, &plaintext))
00803         {
00804           _dbus_string_free (&plaintext);
00805           goto nomem;
00806         }
00807       
00808       _dbus_message_loader_get_buffer (transport->loader,
00809                                        &buffer);
00810       
00811       orig_len = _dbus_string_get_length (buffer);
00812       
00813       if (!_dbus_string_move (&plaintext, 0, buffer,
00814                               orig_len))
00815         {
00816           _dbus_string_free (&plaintext);
00817           goto nomem;
00818         }
00819       
00820       _dbus_verbose (" %d unused bytes sent to message loader\n", 
00821                      _dbus_string_get_length (buffer) -
00822                      orig_len);
00823       
00824       _dbus_message_loader_return_buffer (transport->loader,
00825                                           buffer,
00826                                           _dbus_string_get_length (buffer) -
00827                                           orig_len);
00828 
00829       _dbus_auth_delete_unused_bytes (transport->auth);
00830       
00831       _dbus_string_free (&plaintext);
00832     }
00833   else
00834     {
00835       const DBusString *bytes;
00836       DBusString *buffer;
00837       int orig_len;
00838       dbus_bool_t succeeded;
00839 
00840       _dbus_message_loader_get_buffer (transport->loader,
00841                                        &buffer);
00842                 
00843       orig_len = _dbus_string_get_length (buffer);
00844                 
00845       _dbus_auth_get_unused_bytes (transport->auth,
00846                                    &bytes);
00847 
00848       succeeded = TRUE;
00849       if (!_dbus_string_copy (bytes, 0, buffer, _dbus_string_get_length (buffer)))
00850         succeeded = FALSE;
00851       
00852       _dbus_verbose (" %d unused bytes sent to message loader\n", 
00853                      _dbus_string_get_length (buffer) -
00854                      orig_len);
00855       
00856       _dbus_message_loader_return_buffer (transport->loader,
00857                                           buffer,
00858                                           _dbus_string_get_length (buffer) -
00859                                           orig_len);
00860 
00861       if (succeeded)
00862         _dbus_auth_delete_unused_bytes (transport->auth);
00863       else
00864         goto nomem;
00865     }
00866 
00867   return TRUE;
00868 
00869  nomem:
00870   _dbus_verbose ("Not enough memory to transfer unused bytes from auth conversation\n");
00871   return FALSE;
00872 }
00873 
00881 DBusDispatchStatus
00882 _dbus_transport_get_dispatch_status (DBusTransport *transport)
00883 {
00884   if (_dbus_counter_get_value (transport->live_messages_size) >= transport->max_live_messages_size)
00885     return DBUS_DISPATCH_COMPLETE; /* complete for now */
00886 
00887   if (!_dbus_transport_get_is_authenticated (transport))
00888     {
00889       if (_dbus_auth_do_work (transport->auth) ==
00890           DBUS_AUTH_STATE_WAITING_FOR_MEMORY)
00891         return DBUS_DISPATCH_NEED_MEMORY;
00892       else if (!_dbus_transport_get_is_authenticated (transport))
00893         return DBUS_DISPATCH_COMPLETE;
00894     }
00895 
00896   if (!transport->unused_bytes_recovered &&
00897       !recover_unused_bytes (transport))
00898     return DBUS_DISPATCH_NEED_MEMORY;
00899 
00900   transport->unused_bytes_recovered = TRUE;
00901   
00902   if (!_dbus_message_loader_queue_messages (transport->loader))
00903     return DBUS_DISPATCH_NEED_MEMORY;
00904 
00905   if (_dbus_message_loader_peek_message (transport->loader) != NULL)
00906     return DBUS_DISPATCH_DATA_REMAINS;
00907   else
00908     return DBUS_DISPATCH_COMPLETE;
00909 }
00910 
00919 dbus_bool_t
00920 _dbus_transport_queue_messages (DBusTransport *transport)
00921 {
00922   DBusDispatchStatus status;
00923 
00924 #if 0
00925   _dbus_verbose ("_dbus_transport_queue_messages()\n");
00926 #endif
00927   
00928   /* Queue any messages */
00929   while ((status = _dbus_transport_get_dispatch_status (transport)) == DBUS_DISPATCH_DATA_REMAINS)
00930     {
00931       DBusMessage *message;
00932       DBusList *link;
00933 
00934       link = _dbus_message_loader_pop_message_link (transport->loader);
00935       _dbus_assert (link != NULL);
00936       
00937       message = link->data;
00938       
00939       _dbus_verbose ("queueing received message %p\n", message);
00940 
00941       if (!_dbus_message_add_size_counter (message, transport->live_messages_size))
00942         {
00943           _dbus_message_loader_putback_message_link (transport->loader,
00944                                                      link);
00945           status = DBUS_DISPATCH_NEED_MEMORY;
00946           break;
00947         }
00948       else
00949         {
00950           /* pass ownership of link and message ref to connection */
00951           _dbus_connection_queue_received_message_link (transport->connection,
00952                                                         link);
00953         }
00954     }
00955 
00956   if (_dbus_message_loader_get_is_corrupted (transport->loader))
00957     {
00958       _dbus_verbose ("Corrupted message stream, disconnecting\n");
00959       _dbus_transport_disconnect (transport);
00960     }
00961 
00962   return status != DBUS_DISPATCH_NEED_MEMORY;
00963 }
00964 
00971 void
00972 _dbus_transport_set_max_message_size (DBusTransport  *transport,
00973                                       long            size)
00974 {
00975   _dbus_message_loader_set_max_message_size (transport->loader, size);
00976 }
00977 
00984 long
00985 _dbus_transport_get_max_message_size (DBusTransport  *transport)
00986 {
00987   return _dbus_message_loader_get_max_message_size (transport->loader);
00988 }
00989 
00996 void
00997 _dbus_transport_set_max_received_size (DBusTransport  *transport,
00998                                        long            size)
00999 {
01000   transport->max_live_messages_size = size;
01001   _dbus_counter_set_notify (transport->live_messages_size,
01002                             transport->max_live_messages_size,
01003                             live_messages_size_notify,
01004                             transport);
01005 }
01006 
01007 
01014 long
01015 _dbus_transport_get_max_received_size (DBusTransport  *transport)
01016 {
01017   return transport->max_live_messages_size;
01018 }
01019 
01027 dbus_bool_t
01028 _dbus_transport_get_unix_user (DBusTransport *transport,
01029                                unsigned long *uid)
01030 {
01031   DBusCredentials auth_identity;
01032 
01033   *uid = _DBUS_INT32_MAX; /* better than some root or system user in
01034                            * case of bugs in the caller. Caller should
01035                            * never use this value on purpose, however.
01036                            */
01037   
01038   if (!transport->authenticated)
01039     return FALSE;
01040   
01041   _dbus_auth_get_identity (transport->auth, &auth_identity);
01042 
01043   if (auth_identity.uid != DBUS_UID_UNSET)
01044     {
01045       *uid = auth_identity.uid;
01046       return TRUE;
01047     }
01048   else
01049     return FALSE;
01050 }
01051 
01059 dbus_bool_t
01060 _dbus_transport_get_unix_process_id (DBusTransport *transport,
01061                                      unsigned long *pid)
01062 {
01063   DBusCredentials auth_identity;
01064 
01065   *pid = DBUS_PID_UNSET; /* Caller should never use this value on purpose,
01066                           * but we set it to a safe number, INT_MAX,
01067                           * just to root out possible bugs in bad callers.
01068                           */
01069   
01070   if (!transport->authenticated)
01071     return FALSE;
01072   
01073   _dbus_auth_get_identity (transport->auth, &auth_identity);
01074 
01075   if (auth_identity.pid != DBUS_PID_UNSET)
01076     {
01077       *pid = auth_identity.pid;
01078       return TRUE;
01079     }
01080   else
01081     return FALSE;
01082 }
01083 
01094 void
01095 _dbus_transport_set_unix_user_function (DBusTransport             *transport,
01096                                         DBusAllowUnixUserFunction  function,
01097                                         void                      *data,
01098                                         DBusFreeFunction           free_data_function,
01099                                         void                     **old_data,
01100                                         DBusFreeFunction          *old_free_data_function)
01101 {  
01102   *old_data = transport->unix_user_data;
01103   *old_free_data_function = transport->free_unix_user_data;
01104 
01105   transport->unix_user_function = function;
01106   transport->unix_user_data = data;
01107   transport->free_unix_user_data = free_data_function;
01108 }
01109 
01118 dbus_bool_t
01119 _dbus_transport_set_auth_mechanisms (DBusTransport  *transport,
01120                                      const char    **mechanisms)
01121 {
01122   return _dbus_auth_set_mechanisms (transport->auth, mechanisms);
01123 }
01124 
01125 

Generated on Tue Apr 15 15:54:03 2008 for D-Bus by  doxygen 1.5.1