D-Bus  1.12.16
dbus-transport.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-transport.c DBusTransport object (internal to D-Bus implementation)
3  *
4  * Copyright (C) 2002, 2003 Red Hat Inc.
5  *
6  * Licensed under the Academic Free License version 2.1
7  *
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.
12  *
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.
17  *
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  *
22  */
23 
24 #include <config.h>
25 #include "dbus-transport-protected.h"
26 #include "dbus-transport-unix.h"
27 #include "dbus-transport-socket.h"
28 #include "dbus-connection-internal.h"
29 #include "dbus-watch.h"
30 #include "dbus-auth.h"
31 #include "dbus-address.h"
32 #include "dbus-credentials.h"
33 #include "dbus-mainloop.h"
34 #include "dbus-message.h"
35 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
36 #include "dbus-server-debug-pipe.h"
37 #endif
38 
60 static void
61 live_messages_notify (DBusCounter *counter,
62  void *user_data)
63 {
64  DBusTransport *transport = user_data;
65 
66  _dbus_connection_lock (transport->connection);
67  _dbus_transport_ref (transport);
68 
69 #if 0
70  _dbus_verbose ("Size counter value is now %d\n",
71  (int) _dbus_counter_get_size_value (counter));
72  _dbus_verbose ("Unix FD counter value is now %d\n",
73  (int) _dbus_counter_get_unix_fd_value (counter));
74 #endif
75 
76  /* disable or re-enable the read watch for the transport if
77  * required.
78  */
79  if (transport->vtable->live_messages_changed)
80  {
81  (* transport->vtable->live_messages_changed) (transport);
82  }
83 
84  _dbus_transport_unref (transport);
86 }
87 
103  const DBusTransportVTable *vtable,
104  const DBusString *server_guid,
105  const DBusString *address)
106 {
107  DBusMessageLoader *loader;
108  DBusAuth *auth;
109  DBusCounter *counter;
110  char *address_copy;
111  DBusCredentials *creds;
112 
113  loader = _dbus_message_loader_new ();
114  if (loader == NULL)
115  return FALSE;
116 
117  if (server_guid)
118  auth = _dbus_auth_server_new (server_guid);
119  else
120  auth = _dbus_auth_client_new ();
121  if (auth == NULL)
122  {
124  return FALSE;
125  }
126 
127  counter = _dbus_counter_new ();
128  if (counter == NULL)
129  {
130  _dbus_auth_unref (auth);
132  return FALSE;
133  }
134 
135  creds = _dbus_credentials_new ();
136  if (creds == NULL)
137  {
138  _dbus_counter_unref (counter);
139  _dbus_auth_unref (auth);
141  return FALSE;
142  }
143 
144  if (server_guid)
145  {
146  _dbus_assert (address == NULL);
147  address_copy = NULL;
148  }
149  else
150  {
151  _dbus_assert (address != NULL);
152 
153  if (!_dbus_string_copy_data (address, &address_copy))
154  {
155  _dbus_credentials_unref (creds);
156  _dbus_counter_unref (counter);
157  _dbus_auth_unref (auth);
159  return FALSE;
160  }
161  }
162 
163  transport->refcount = 1;
164  transport->vtable = vtable;
165  transport->loader = loader;
166  transport->auth = auth;
167  transport->live_messages = counter;
168  transport->authenticated = FALSE;
169  transport->disconnected = FALSE;
170  transport->is_server = (server_guid != NULL);
171  transport->send_credentials_pending = !transport->is_server;
172  transport->receive_credentials_pending = transport->is_server;
173  transport->address = address_copy;
174 
175  transport->unix_user_function = NULL;
176  transport->unix_user_data = NULL;
177  transport->free_unix_user_data = NULL;
178 
179  transport->windows_user_function = NULL;
180  transport->windows_user_data = NULL;
181  transport->free_windows_user_data = NULL;
182 
183  transport->expected_guid = NULL;
184 
185  /* Try to default to something that won't totally hose the system,
186  * but doesn't impose too much of a limitation.
187  */
188  transport->max_live_messages_size = _DBUS_ONE_MEGABYTE * 63;
189 
190  /* On Linux RLIMIT_NOFILE defaults to 1024, so allowing 4096 fds live
191  should be more than enough */
192  transport->max_live_messages_unix_fds = 4096;
193 
194  /* credentials read from socket if any */
195  transport->credentials = creds;
196 
198  transport->max_live_messages_size,
199  transport->max_live_messages_unix_fds,
200  live_messages_notify,
201  transport);
202 
203  if (transport->address)
204  _dbus_verbose ("Initialized transport on address %s\n", transport->address);
205 
206  return TRUE;
207 }
208 
215 void
217 {
218  if (!transport->disconnected)
219  _dbus_transport_disconnect (transport);
220 
221  if (transport->free_unix_user_data != NULL)
222  (* transport->free_unix_user_data) (transport->unix_user_data);
223 
224  if (transport->free_windows_user_data != NULL)
225  (* transport->free_windows_user_data) (transport->windows_user_data);
226 
227  _dbus_message_loader_unref (transport->loader);
228  _dbus_auth_unref (transport->auth);
230  0, 0, NULL, NULL);
231  _dbus_counter_unref (transport->live_messages);
232  dbus_free (transport->address);
233  dbus_free (transport->expected_guid);
234  if (transport->credentials)
236 }
237 
238 
249 static DBusTransport*
250 check_address (const char *address, DBusError *error)
251 {
252  DBusAddressEntry **entries;
253  DBusTransport *transport = NULL;
254  int len, i;
255 
256  _dbus_assert (address != NULL);
257  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
258 
259  if (!dbus_parse_address (address, &entries, &len, error))
260  return NULL; /* not a valid address */
261 
262  for (i = 0; i < len; i++)
263  {
264  dbus_error_free (error);
265  transport = _dbus_transport_open (entries[i], error);
266 
267  if (transport != NULL)
268  break;
269  }
270 
271  dbus_address_entries_free (entries);
272  return transport;
273 }
274 
283 static DBusTransport*
284 _dbus_transport_new_for_autolaunch (const char *scope, DBusError *error)
285 {
286  DBusString address;
287  DBusTransport *result = NULL;
288 
289  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
290 
291  if (!_dbus_string_init (&address))
292  {
294  return NULL;
295  }
296 
297  if (!_dbus_get_autolaunch_address (scope, &address, error))
298  {
299  _DBUS_ASSERT_ERROR_IS_SET (error);
300  goto out;
301  }
302 
303  result = check_address (_dbus_string_get_const_data (&address), error);
304  if (result == NULL)
305  _DBUS_ASSERT_ERROR_IS_SET (error);
306  else
307  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
308 
309  out:
310  _dbus_string_free (&address);
311  return result;
312 }
313 
314 static DBusTransportOpenResult
315 _dbus_transport_open_autolaunch (DBusAddressEntry *entry,
316  DBusTransport **transport_p,
317  DBusError *error)
318 {
319  const char *method;
320 
321  method = dbus_address_entry_get_method (entry);
322  _dbus_assert (method != NULL);
323 
324  if (strcmp (method, "autolaunch") == 0)
325  {
326  const char *scope = dbus_address_entry_get_value (entry, "scope");
327 
328  *transport_p = _dbus_transport_new_for_autolaunch (scope, error);
329 
330  if (*transport_p == NULL)
331  {
332  _DBUS_ASSERT_ERROR_IS_SET (error);
333  return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
334  }
335  else
336  {
337  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
338  return DBUS_TRANSPORT_OPEN_OK;
339  }
340  }
341  else
342  {
343  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
344  return DBUS_TRANSPORT_OPEN_NOT_HANDLED;
345  }
346 }
347 
348 static const struct {
349  DBusTransportOpenResult (* func) (DBusAddressEntry *entry,
350  DBusTransport **transport_p,
351  DBusError *error);
352 } open_funcs[] = {
355  { _dbus_transport_open_autolaunch }
356 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
357  , { _dbus_transport_open_debug_pipe }
358 #endif
359 };
360 
371  DBusError *error)
372 {
373  DBusTransport *transport;
374  const char *expected_guid_orig;
375  char *expected_guid;
376  int i;
377  DBusError tmp_error = DBUS_ERROR_INIT;
378 
379  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
380 
381  transport = NULL;
382  expected_guid_orig = dbus_address_entry_get_value (entry, "guid");
383  expected_guid = _dbus_strdup (expected_guid_orig);
384 
385  if (expected_guid_orig != NULL && expected_guid == NULL)
386  {
387  _DBUS_SET_OOM (error);
388  return NULL;
389  }
390 
391  for (i = 0; i < (int) _DBUS_N_ELEMENTS (open_funcs); ++i)
392  {
393  DBusTransportOpenResult result;
394 
395  _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
396  result = (* open_funcs[i].func) (entry, &transport, &tmp_error);
397 
398  switch (result)
399  {
400  case DBUS_TRANSPORT_OPEN_OK:
401  _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
402  goto out;
403  break;
404  case DBUS_TRANSPORT_OPEN_NOT_HANDLED:
405  _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
406  /* keep going through the loop of open funcs */
407  break;
408  case DBUS_TRANSPORT_OPEN_BAD_ADDRESS:
409  _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
410  goto out;
411  break;
412  case DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT:
413  _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
414  goto out;
415  break;
416  default:
417  _dbus_assert_not_reached ("invalid transport open result");
418  break;
419  }
420  }
421 
422  out:
423 
424  if (transport == NULL)
425  {
426  if (!dbus_error_is_set (&tmp_error))
427  _dbus_set_bad_address (&tmp_error,
428  NULL, NULL,
429  "Unknown address type (examples of valid types are \"tcp\" and on UNIX \"unix\")");
430 
431  _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
432  dbus_move_error(&tmp_error, error);
433  dbus_free (expected_guid);
434  }
435  else
436  {
437  _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
438 
439  /* In the case of autostart the initial guid is NULL
440  * and the autostart transport recursively calls
441  * _dbus_open_transport wich returns a transport
442  * with a guid. That guid is the definitive one.
443  *
444  * FIXME: if more transports are added they may have
445  * an effect on the expected_guid semantics (i.e.
446  * expected_guid and transport->expected_guid may
447  * both have values). This is very unlikely though
448  * we should either throw asserts here for those
449  * corner cases or refactor the code so it is
450  * clearer on what is expected and what is not
451  */
452  if(expected_guid)
453  transport->expected_guid = expected_guid;
454  }
455 
456  return transport;
457 }
458 
467 {
468  _dbus_assert (transport->refcount > 0);
469 
470  transport->refcount += 1;
471 
472  return transport;
473 }
474 
482 void
484 {
485  _dbus_assert (transport != NULL);
486  _dbus_assert (transport->refcount > 0);
487 
488  transport->refcount -= 1;
489  if (transport->refcount == 0)
490  {
491  _dbus_verbose ("finalizing\n");
492 
493  _dbus_assert (transport->vtable->finalize != NULL);
494 
495  (* transport->vtable->finalize) (transport);
496  }
497 }
498 
507 void
509 {
510  _dbus_verbose ("start\n");
511 
512  _dbus_assert (transport->vtable->disconnect != NULL);
513 
514  if (transport->disconnected)
515  return;
516 
517  (* transport->vtable->disconnect) (transport);
518 
519  transport->disconnected = TRUE;
520 
521  _dbus_verbose ("end\n");
522 }
523 
534 {
535  return !transport->disconnected;
536 }
537 
538 static dbus_bool_t
539 auth_via_unix_user_function (DBusTransport *transport)
540 {
541  DBusCredentials *auth_identity;
542  dbus_bool_t allow;
543  DBusConnection *connection;
544  DBusAllowUnixUserFunction unix_user_function;
545  void *unix_user_data;
546  dbus_uid_t uid;
547 
548  /* Dropping the lock here probably isn't that safe. */
549 
550  auth_identity = _dbus_auth_get_identity (transport->auth);
551  _dbus_assert (auth_identity != NULL);
552 
553  connection = transport->connection;
554  unix_user_function = transport->unix_user_function;
555  unix_user_data = transport->unix_user_data;
556  uid = _dbus_credentials_get_unix_uid (auth_identity);
557 
558  _dbus_verbose ("unlock\n");
559  _dbus_connection_unlock (connection);
560 
561  allow = (* unix_user_function) (connection,
562  uid,
563  unix_user_data);
564 
565  _dbus_verbose ("lock post unix user function\n");
566  _dbus_connection_lock (connection);
567 
568  if (allow)
569  {
570  _dbus_verbose ("Client UID "DBUS_UID_FORMAT" authorized\n", uid);
571  }
572  else
573  {
574  _dbus_verbose ("Client UID "DBUS_UID_FORMAT
575  " was rejected, disconnecting\n",
576  _dbus_credentials_get_unix_uid (auth_identity));
577  _dbus_transport_disconnect (transport);
578  }
579 
580  return allow;
581 }
582 
583 static dbus_bool_t
584 auth_via_windows_user_function (DBusTransport *transport)
585 {
586  DBusCredentials *auth_identity;
587  dbus_bool_t allow;
588  DBusConnection *connection;
589  DBusAllowWindowsUserFunction windows_user_function;
590  void *windows_user_data;
591  char *windows_sid;
592 
593  /* Dropping the lock here probably isn't that safe. */
594 
595  auth_identity = _dbus_auth_get_identity (transport->auth);
596  _dbus_assert (auth_identity != NULL);
597 
598  connection = transport->connection;
599  windows_user_function = transport->windows_user_function;
600  windows_user_data = transport->unix_user_data;
601  windows_sid = _dbus_strdup (_dbus_credentials_get_windows_sid (auth_identity));
602 
603  if (windows_sid == NULL)
604  {
605  /* OOM */
606  return FALSE;
607  }
608 
609  _dbus_verbose ("unlock\n");
610  _dbus_connection_unlock (connection);
611 
612  allow = (* windows_user_function) (connection,
613  windows_sid,
614  windows_user_data);
615 
616  _dbus_verbose ("lock post windows user function\n");
617  _dbus_connection_lock (connection);
618 
619  if (allow)
620  {
621  _dbus_verbose ("Client SID '%s' authorized\n", windows_sid);
622  }
623  else
624  {
625  _dbus_verbose ("Client SID '%s' was rejected, disconnecting\n",
626  _dbus_credentials_get_windows_sid (auth_identity));
627  _dbus_transport_disconnect (transport);
628  }
629 
630  return allow;
631 }
632 
633 static dbus_bool_t
634 auth_via_default_rules (DBusTransport *transport)
635 {
636  DBusCredentials *auth_identity;
637  DBusCredentials *our_identity;
638  dbus_bool_t allow;
639 
640  auth_identity = _dbus_auth_get_identity (transport->auth);
641  _dbus_assert (auth_identity != NULL);
642 
643  /* By default, connection is allowed if the client is 1) root or 2)
644  * has the same UID as us or 3) anonymous is allowed.
645  */
646 
648  if (our_identity == NULL)
649  {
650  /* OOM */
651  return FALSE;
652  }
653 
654  if (transport->allow_anonymous ||
655  _dbus_credentials_get_unix_uid (auth_identity) == 0 ||
656  _dbus_credentials_same_user (our_identity,
657  auth_identity))
658  {
659  if (_dbus_credentials_include(our_identity,DBUS_CREDENTIAL_WINDOWS_SID))
660  _dbus_verbose ("Client authorized as SID '%s'"
661  "matching our SID '%s'\n",
662  _dbus_credentials_get_windows_sid(auth_identity),
663  _dbus_credentials_get_windows_sid(our_identity));
664  else
665  _dbus_verbose ("Client authorized as UID "DBUS_UID_FORMAT
666  " matching our UID "DBUS_UID_FORMAT"\n",
667  _dbus_credentials_get_unix_uid(auth_identity),
668  _dbus_credentials_get_unix_uid(our_identity));
669  /* We have authenticated! */
670  allow = TRUE;
671  }
672  else
673  {
674  if (_dbus_credentials_include(our_identity,DBUS_CREDENTIAL_WINDOWS_SID))
675  _dbus_verbose ("Client authorized as SID '%s'"
676  " but our SID is '%s', disconnecting\n",
677  (_dbus_credentials_get_windows_sid(auth_identity) ?
678  _dbus_credentials_get_windows_sid(auth_identity) : "<null>"),
679  (_dbus_credentials_get_windows_sid(our_identity) ?
680  _dbus_credentials_get_windows_sid(our_identity) : "<null>"));
681  else
682  _dbus_verbose ("Client authorized as UID "DBUS_UID_FORMAT
683  " but our UID is "DBUS_UID_FORMAT", disconnecting\n",
684  _dbus_credentials_get_unix_uid(auth_identity),
685  _dbus_credentials_get_unix_uid(our_identity));
686  _dbus_transport_disconnect (transport);
687  allow = FALSE;
688  }
689 
690  _dbus_credentials_unref (our_identity);
691 
692  return allow;
693 }
694 
708 {
709  return transport->authenticated;
710 }
711 
731 {
732  if (transport->authenticated)
733  return TRUE;
734  else
735  {
736  dbus_bool_t maybe_authenticated;
737 
738  if (transport->disconnected)
739  return FALSE;
740 
741  /* paranoia ref since we call user callbacks sometimes */
743 
744  maybe_authenticated =
745  (!(transport->send_credentials_pending ||
746  transport->receive_credentials_pending));
747 
748  if (maybe_authenticated)
749  {
750  switch (_dbus_auth_do_work (transport->auth))
751  {
752  case DBUS_AUTH_STATE_AUTHENTICATED:
753  /* leave as maybe_authenticated */
754  break;
755 
756  case DBUS_AUTH_STATE_WAITING_FOR_INPUT:
757  case DBUS_AUTH_STATE_WAITING_FOR_MEMORY:
758  case DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND:
759  case DBUS_AUTH_STATE_NEED_DISCONNECT:
760  maybe_authenticated = FALSE;
761  break;
762 
763  case DBUS_AUTH_STATE_INVALID:
764  default:
765  _dbus_assert_not_reached ("invalid authentication state");
766  }
767  }
768 
769  /* If we're the client, verify the GUID
770  */
771  if (maybe_authenticated && !transport->is_server)
772  {
773  const char *server_guid;
774 
775  server_guid = _dbus_auth_get_guid_from_server (transport->auth);
776  _dbus_assert (server_guid != NULL);
777 
778  if (transport->expected_guid &&
779  strcmp (transport->expected_guid, server_guid) != 0)
780  {
781  _dbus_verbose ("Client expected GUID '%s' and we got '%s' from the server\n",
782  transport->expected_guid, server_guid);
783  _dbus_transport_disconnect (transport);
785  return FALSE;
786  }
787  }
788 
789  /* If we're the server, see if we want to allow this identity to proceed.
790  */
791  if (maybe_authenticated && transport->is_server)
792  {
793  dbus_bool_t allow;
794  DBusCredentials *auth_identity;
795 
796  auth_identity = _dbus_auth_get_identity (transport->auth);
797  _dbus_assert (auth_identity != NULL);
798 
799  /* If we have an auth'd user and a user function, delegate
800  * deciding whether auth credentials are good enough to the
801  * app; otherwise, use our default decision process.
802  */
803  if (transport->unix_user_function != NULL &&
804  _dbus_credentials_include (auth_identity, DBUS_CREDENTIAL_UNIX_USER_ID))
805  {
806  allow = auth_via_unix_user_function (transport);
807  }
808  else if (transport->windows_user_function != NULL &&
809  _dbus_credentials_include (auth_identity, DBUS_CREDENTIAL_WINDOWS_SID))
810  {
811  allow = auth_via_windows_user_function (transport);
812  }
813  else
814  {
815  allow = auth_via_default_rules (transport);
816  }
817 
818  if (!allow)
819  maybe_authenticated = FALSE;
820  }
821 
822  transport->authenticated = maybe_authenticated;
823 
825  return maybe_authenticated;
826  }
827 }
828 
837 {
838  DBusCredentials *auth_identity;
839 
840  if (!transport->authenticated)
841  return TRUE;
842 
843  auth_identity = _dbus_auth_get_identity (transport->auth);
844 
845  if (_dbus_credentials_are_anonymous (auth_identity))
846  return TRUE;
847  else
848  return FALSE;
849 }
850 
859 {
860  return DBUS_TRANSPORT_CAN_SEND_UNIX_FD(transport);
861 }
862 
870 const char*
872 {
873  return transport->address;
874 }
875 
883 const char*
885 {
886  if (transport->is_server)
887  return NULL;
888  else if (transport->authenticated)
889  return _dbus_auth_get_guid_from_server (transport->auth);
890  else
891  return transport->expected_guid;
892 }
893 
905  DBusWatch *watch,
906  unsigned int condition)
907 {
908  dbus_bool_t retval;
909 
910  _dbus_assert (transport->vtable->handle_watch != NULL);
911 
912  if (transport->disconnected)
913  return TRUE;
914 
915  if (dbus_watch_get_socket (watch) < 0)
916  {
917  _dbus_warn_check_failed ("Tried to handle an invalidated watch; this watch should have been removed");
918  return TRUE;
919  }
920 
921  _dbus_watch_sanitize_condition (watch, &condition);
922 
923  _dbus_transport_ref (transport);
924  _dbus_watch_ref (watch);
925  retval = (* transport->vtable->handle_watch) (transport, watch, condition);
926  _dbus_watch_unref (watch);
927  _dbus_transport_unref (transport);
928 
929  return retval;
930 }
931 
943  DBusConnection *connection)
944 {
945  _dbus_assert (transport->vtable->connection_set != NULL);
946  _dbus_assert (transport->connection == NULL);
947 
948  transport->connection = connection;
949 
950  _dbus_transport_ref (transport);
951  if (!(* transport->vtable->connection_set) (transport))
952  transport->connection = NULL;
953  _dbus_transport_unref (transport);
954 
955  return transport->connection != NULL;
956 }
957 
967  DBusSocket *fd_p)
968 {
969  dbus_bool_t retval;
970 
971  if (transport->vtable->get_socket_fd == NULL)
972  return FALSE;
973 
974  if (transport->disconnected)
975  return FALSE;
976 
977  _dbus_transport_ref (transport);
978 
979  retval = (* transport->vtable->get_socket_fd) (transport,
980  fd_p);
981 
982  _dbus_transport_unref (transport);
983 
984  return retval;
985 }
986 
998 void
1000  unsigned int flags,
1001  int timeout_milliseconds)
1002 {
1003  _dbus_assert (transport->vtable->do_iteration != NULL);
1004 
1005  _dbus_verbose ("Transport iteration flags 0x%x timeout %d connected = %d\n",
1006  flags, timeout_milliseconds, !transport->disconnected);
1007 
1008  if ((flags & (DBUS_ITERATION_DO_WRITING |
1009  DBUS_ITERATION_DO_READING)) == 0)
1010  return; /* Nothing to do */
1011 
1012  if (transport->disconnected)
1013  return;
1014 
1015  _dbus_transport_ref (transport);
1016  (* transport->vtable->do_iteration) (transport, flags,
1017  timeout_milliseconds);
1018  _dbus_transport_unref (transport);
1019 
1020  _dbus_verbose ("end\n");
1021 }
1022 
1023 static dbus_bool_t
1024 recover_unused_bytes (DBusTransport *transport)
1025 {
1026  if (_dbus_auth_needs_decoding (transport->auth))
1027  {
1028  DBusString plaintext;
1029  const DBusString *encoded;
1030  DBusString *buffer;
1031  int orig_len;
1032 
1033  if (!_dbus_string_init (&plaintext))
1034  goto nomem;
1035 
1036  _dbus_auth_get_unused_bytes (transport->auth,
1037  &encoded);
1038 
1039  if (!_dbus_auth_decode_data (transport->auth,
1040  encoded, &plaintext))
1041  {
1042  _dbus_string_free (&plaintext);
1043  goto nomem;
1044  }
1045 
1047  &buffer,
1048  NULL,
1049  NULL);
1050 
1051  orig_len = _dbus_string_get_length (buffer);
1052 
1053  if (!_dbus_string_move (&plaintext, 0, buffer,
1054  orig_len))
1055  {
1056  _dbus_string_free (&plaintext);
1057  goto nomem;
1058  }
1059 
1060  _dbus_verbose (" %d unused bytes sent to message loader\n",
1061  _dbus_string_get_length (buffer) -
1062  orig_len);
1063 
1065  buffer);
1066 
1067  _dbus_auth_delete_unused_bytes (transport->auth);
1068 
1069  _dbus_string_free (&plaintext);
1070  }
1071  else
1072  {
1073  const DBusString *bytes;
1074  DBusString *buffer;
1075 #ifdef DBUS_ENABLE_VERBOSE_MODE
1076  int orig_len;
1077 #endif
1078  dbus_bool_t succeeded;
1079 
1081  &buffer,
1082  NULL,
1083  NULL);
1084 
1085 #ifdef DBUS_ENABLE_VERBOSE_MODE
1086  orig_len = _dbus_string_get_length (buffer);
1087 #endif
1088 
1089  _dbus_auth_get_unused_bytes (transport->auth,
1090  &bytes);
1091 
1092  succeeded = TRUE;
1093  if (!_dbus_string_copy (bytes, 0, buffer, _dbus_string_get_length (buffer)))
1094  succeeded = FALSE;
1095 
1096  _dbus_verbose (" %d unused bytes sent to message loader\n",
1097  _dbus_string_get_length (buffer) -
1098  orig_len);
1099 
1101  buffer);
1102 
1103  if (succeeded)
1104  _dbus_auth_delete_unused_bytes (transport->auth);
1105  else
1106  goto nomem;
1107  }
1108 
1109  return TRUE;
1110 
1111  nomem:
1112  _dbus_verbose ("Not enough memory to transfer unused bytes from auth conversation\n");
1113  return FALSE;
1114 }
1115 
1125 {
1126  if (_dbus_counter_get_size_value (transport->live_messages) >= transport->max_live_messages_size ||
1128  return DBUS_DISPATCH_COMPLETE; /* complete for now */
1129 
1130  if (!_dbus_transport_try_to_authenticate (transport))
1131  {
1132  if (_dbus_auth_do_work (transport->auth) ==
1133  DBUS_AUTH_STATE_WAITING_FOR_MEMORY)
1135  else if (!_dbus_transport_try_to_authenticate (transport))
1136  return DBUS_DISPATCH_COMPLETE;
1137  }
1138 
1139  if (!transport->unused_bytes_recovered &&
1140  !recover_unused_bytes (transport))
1142 
1143  transport->unused_bytes_recovered = TRUE;
1144 
1145  if (!_dbus_message_loader_queue_messages (transport->loader))
1147 
1148  if (_dbus_message_loader_peek_message (transport->loader) != NULL)
1150  else
1151  return DBUS_DISPATCH_COMPLETE;
1152 }
1153 
1164 {
1165  DBusDispatchStatus status;
1166 
1167 #if 0
1168  _dbus_verbose ("enter\n");
1169 #endif
1170 
1171  /* Queue any messages */
1172  while ((status = _dbus_transport_get_dispatch_status (transport)) == DBUS_DISPATCH_DATA_REMAINS)
1173  {
1174  DBusMessage *message;
1175  DBusList *link;
1176 
1177  link = _dbus_message_loader_pop_message_link (transport->loader);
1178  _dbus_assert (link != NULL);
1179 
1180  message = link->data;
1181 
1182  _dbus_verbose ("queueing received message %p\n", message);
1183 
1184  if (!_dbus_message_add_counter (message, transport->live_messages))
1185  {
1187  link);
1188  status = DBUS_DISPATCH_NEED_MEMORY;
1189  break;
1190  }
1191  else
1192  {
1193  /* We didn't call the notify function when we added the counter, so
1194  * catch up now. Since we have the connection's lock, it's desirable
1195  * that we bypass the notify function and call this virtual method
1196  * directly. */
1197  if (transport->vtable->live_messages_changed)
1198  (* transport->vtable->live_messages_changed) (transport);
1199 
1200  /* pass ownership of link and message ref to connection */
1202  link);
1203  }
1204  }
1205 
1207  {
1208  _dbus_verbose ("Corrupted message stream, disconnecting\n");
1209  _dbus_transport_disconnect (transport);
1210  }
1211 
1212  return status != DBUS_DISPATCH_NEED_MEMORY;
1213 }
1214 
1221 void
1223  long size)
1224 {
1226 }
1227 
1234 void
1236  long n)
1237 {
1239 }
1240 
1247 long
1249 {
1251 }
1252 
1259 long
1261 {
1263 }
1264 
1271 void
1273  long size)
1274 {
1275  transport->max_live_messages_size = size;
1277  transport->max_live_messages_size,
1278  transport->max_live_messages_unix_fds,
1279  live_messages_notify,
1280  transport);
1281 }
1282 
1289 void
1291  long n)
1292 {
1293  transport->max_live_messages_unix_fds = n;
1295  transport->max_live_messages_size,
1296  transport->max_live_messages_unix_fds,
1297  live_messages_notify,
1298  transport);
1299 }
1300 
1307 long
1309 {
1310  return transport->max_live_messages_size;
1311 }
1312 
1319 long
1321 {
1322  return transport->max_live_messages_unix_fds;
1323 }
1324 
1334  unsigned long *uid)
1335 {
1336  DBusCredentials *auth_identity;
1337 
1338  *uid = _DBUS_INT32_MAX; /* better than some root or system user in
1339  * case of bugs in the caller. Caller should
1340  * never use this value on purpose, however.
1341  */
1342 
1343  if (!transport->authenticated)
1344  return FALSE;
1345 
1346  auth_identity = _dbus_auth_get_identity (transport->auth);
1347 
1348  if (_dbus_credentials_include (auth_identity,
1349  DBUS_CREDENTIAL_UNIX_USER_ID))
1350  {
1351  *uid = _dbus_credentials_get_unix_uid (auth_identity);
1352  return TRUE;
1353  }
1354  else
1355  return FALSE;
1356 }
1357 
1367  unsigned long *pid)
1368 {
1369  DBusCredentials *auth_identity;
1370 
1371  *pid = DBUS_PID_UNSET; /* Caller should never use this value on purpose,
1372  * but we set it to a safe number, INT_MAX,
1373  * just to root out possible bugs in bad callers.
1374  */
1375 
1376  if (!transport->authenticated)
1377  return FALSE;
1378 
1379  auth_identity = _dbus_auth_get_identity (transport->auth);
1380 
1381  if (_dbus_credentials_include (auth_identity,
1382  DBUS_CREDENTIAL_UNIX_PROCESS_ID))
1383  {
1384  *pid = _dbus_credentials_get_pid (auth_identity);
1385  return TRUE;
1386  }
1387  else
1388  return FALSE;
1389 }
1390 
1401  void **data,
1402  int *data_size)
1403 {
1404  DBusCredentials *auth_identity;
1405 
1406  *data = NULL;
1407  *data_size = 0;
1408 
1409  if (!transport->authenticated)
1410  return FALSE;
1411 
1412  auth_identity = _dbus_auth_get_identity (transport->auth);
1413 
1414  if (_dbus_credentials_include (auth_identity,
1415  DBUS_CREDENTIAL_ADT_AUDIT_DATA_ID))
1416  {
1417  *data = (void *) _dbus_credentials_get_adt_audit_data (auth_identity);
1418  *data_size = _dbus_credentials_get_adt_audit_data_size (auth_identity);
1419  return TRUE;
1420  }
1421  else
1422  return FALSE;
1423 }
1424 
1435 void
1437  DBusAllowUnixUserFunction function,
1438  void *data,
1439  DBusFreeFunction free_data_function,
1440  void **old_data,
1441  DBusFreeFunction *old_free_data_function)
1442 {
1443  *old_data = transport->unix_user_data;
1444  *old_free_data_function = transport->free_unix_user_data;
1445 
1446  transport->unix_user_function = function;
1447  transport->unix_user_data = data;
1448  transport->free_unix_user_data = free_data_function;
1449 }
1450 
1452 _dbus_transport_get_linux_security_label (DBusTransport *transport,
1453  char **label_p)
1454 {
1455  DBusCredentials *auth_identity;
1456 
1457  *label_p = NULL;
1458 
1459  if (!transport->authenticated)
1460  return FALSE;
1461 
1462  auth_identity = _dbus_auth_get_identity (transport->auth);
1463 
1464  if (_dbus_credentials_include (auth_identity,
1465  DBUS_CREDENTIAL_LINUX_SECURITY_LABEL))
1466  {
1467  /* If no memory, we are supposed to return TRUE and set NULL */
1468  *label_p = _dbus_strdup (_dbus_credentials_get_linux_security_label (auth_identity));
1469 
1470  return TRUE;
1471  }
1472  else
1473  {
1474  return FALSE;
1475  }
1476 }
1477 
1487  char **windows_sid_p)
1488 {
1489  DBusCredentials *auth_identity;
1490 
1491  *windows_sid_p = NULL;
1492 
1493  if (!transport->authenticated)
1494  return FALSE;
1495 
1496  auth_identity = _dbus_auth_get_identity (transport->auth);
1497 
1498  if (_dbus_credentials_include (auth_identity,
1499  DBUS_CREDENTIAL_WINDOWS_SID))
1500  {
1501  /* If no memory, we are supposed to return TRUE and set NULL */
1502  *windows_sid_p = _dbus_strdup (_dbus_credentials_get_windows_sid (auth_identity));
1503 
1504  return TRUE;
1505  }
1506  else
1507  return FALSE;
1508 }
1509 
1521 void
1524  void *data,
1525  DBusFreeFunction free_data_function,
1526  void **old_data,
1527  DBusFreeFunction *old_free_data_function)
1528 {
1529  *old_data = transport->windows_user_data;
1530  *old_free_data_function = transport->free_windows_user_data;
1531 
1532  transport->windows_user_function = function;
1533  transport->windows_user_data = data;
1534  transport->free_windows_user_data = free_data_function;
1535 }
1536 
1547  const char **mechanisms)
1548 {
1549  return _dbus_auth_set_mechanisms (transport->auth, mechanisms);
1550 }
1551 
1558 void
1560  dbus_bool_t value)
1561 {
1562  transport->allow_anonymous = value != FALSE;
1563 }
1564 
1570 int
1572 {
1574 }
1575 
1583 void
1585  void (* callback) (void *),
1586  void *data)
1587 {
1589  callback, data);
1590 }
1591 
1592 #ifdef DBUS_ENABLE_STATS
1593 void
1594 _dbus_transport_get_stats (DBusTransport *transport,
1595  dbus_uint32_t *queue_bytes,
1596  dbus_uint32_t *queue_fds,
1597  dbus_uint32_t *peak_queue_bytes,
1598  dbus_uint32_t *peak_queue_fds)
1599 {
1600  if (queue_bytes != NULL)
1601  *queue_bytes = _dbus_counter_get_size_value (transport->live_messages);
1602 
1603  if (queue_fds != NULL)
1604  *queue_fds = _dbus_counter_get_unix_fd_value (transport->live_messages);
1605 
1606  if (peak_queue_bytes != NULL)
1607  *peak_queue_bytes = _dbus_counter_get_peak_size_value (transport->live_messages);
1608 
1609  if (peak_queue_fds != NULL)
1610  *peak_queue_fds = _dbus_counter_get_peak_unix_fd_value (transport->live_messages);
1611 }
1612 #endif /* DBUS_ENABLE_STATS */
1613