D-Bus  1.10.24
dbus-server-socket.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-server-socket.c Server implementation for sockets
3  *
4  * Copyright (C) 2002, 2003, 2004, 2006 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-internals.h"
26 #include "dbus-server-socket.h"
27 #include "dbus-transport-socket.h"
28 #include "dbus-connection-internal.h"
29 #include "dbus-memory.h"
30 #include "dbus-nonce.h"
31 #include "dbus-string.h"
32 
45 
51 {
53  int n_fds;
56  char *socket_name;
58 };
59 
60 static void
61 socket_finalize (DBusServer *server)
62 {
63  DBusServerSocket *socket_server = (DBusServerSocket*) server;
64  int i;
65 
67 
68  for (i = 0 ; i < socket_server->n_fds ; i++)
69  if (socket_server->watch[i])
70  {
71  _dbus_watch_unref (socket_server->watch[i]);
72  socket_server->watch[i] = NULL;
73  }
74 
75  dbus_free (socket_server->fds);
76  dbus_free (socket_server->watch);
77  dbus_free (socket_server->socket_name);
78  if (socket_server->noncefile)
79  _dbus_noncefile_delete (socket_server->noncefile, NULL);
80  dbus_free (socket_server->noncefile);
81  dbus_free (server);
82 }
83 
84 /* Return value is just for memory, not other failures. */
85 static dbus_bool_t
86 handle_new_client_fd_and_unlock (DBusServer *server,
87  DBusSocket client_fd)
88 {
89  DBusConnection *connection;
90  DBusTransport *transport;
91  DBusNewConnectionFunction new_connection_function;
92  void *new_connection_data;
93 
94  _dbus_verbose ("Creating new client connection with fd %" DBUS_SOCKET_FORMAT "\n",
95  _dbus_socket_printable (client_fd));
96 
97  HAVE_LOCK_CHECK (server);
98 
99  if (!_dbus_set_socket_nonblocking (client_fd, NULL))
100  {
101  SERVER_UNLOCK (server);
102  return TRUE;
103  }
104 
105  transport = _dbus_transport_new_for_socket (client_fd, &server->guid_hex, NULL);
106  if (transport == NULL)
107  {
108  _dbus_close_socket (client_fd, NULL);
109  SERVER_UNLOCK (server);
110  return FALSE;
111  }
112 
113  if (!_dbus_transport_set_auth_mechanisms (transport,
114  (const char **) server->auth_mechanisms))
115  {
116  _dbus_transport_unref (transport);
117  SERVER_UNLOCK (server);
118  return FALSE;
119  }
120 
121  /* note that client_fd is now owned by the transport, and will be
122  * closed on transport disconnection/finalization
123  */
124 
125  connection = _dbus_connection_new_for_transport (transport);
126  _dbus_transport_unref (transport);
127  transport = NULL; /* now under the connection lock */
128 
129  if (connection == NULL)
130  {
131  SERVER_UNLOCK (server);
132  return FALSE;
133  }
134 
135  /* See if someone wants to handle this new connection, self-referencing
136  * for paranoia.
137  */
138  new_connection_function = server->new_connection_function;
139  new_connection_data = server->new_connection_data;
140 
141  _dbus_server_ref_unlocked (server);
142  SERVER_UNLOCK (server);
143 
144  if (new_connection_function)
145  {
146  (* new_connection_function) (server, connection,
147  new_connection_data);
148  }
149  dbus_server_unref (server);
150 
151  /* If no one grabbed a reference, the connection will die. */
153  dbus_connection_unref (connection);
154 
155  return TRUE;
156 }
157 
158 static dbus_bool_t
159 socket_handle_watch (DBusWatch *watch,
160  unsigned int flags,
161  void *data)
162 {
163  DBusServer *server = data;
164  DBusServerSocket *socket_server = data;
165 
166 #ifndef DBUS_DISABLE_ASSERT
167  int i;
168  dbus_bool_t found = FALSE;
169 #endif
170 
171  SERVER_LOCK (server);
172 
173 #ifndef DBUS_DISABLE_ASSERT
174  for (i = 0 ; i < socket_server->n_fds ; i++)
175  {
176  if (socket_server->watch[i] == watch)
177  found = TRUE;
178  }
179  _dbus_assert (found);
180 #endif
181 
182  _dbus_verbose ("Handling client connection, flags 0x%x\n", flags);
183 
184  if (flags & DBUS_WATCH_READABLE)
185  {
186  DBusSocket client_fd;
187  DBusSocket listen_fd;
188  int saved_errno;
189 
190  listen_fd = _dbus_watch_get_socket (watch);
191 
192  if (socket_server->noncefile)
193  client_fd = _dbus_accept_with_noncefile (listen_fd, socket_server->noncefile);
194  else
195  client_fd = _dbus_accept (listen_fd);
196 
197  saved_errno = _dbus_save_socket_errno ();
198 
199  if (!_dbus_socket_is_valid (client_fd))
200  {
201  /* EINTR handled for us */
202 
204  _dbus_verbose ("No client available to accept after all\n");
205  else
206  _dbus_verbose ("Failed to accept a client connection: %s\n",
207  _dbus_strerror (saved_errno));
208 
209  SERVER_UNLOCK (server);
210  }
211  else
212  {
213  if (!handle_new_client_fd_and_unlock (server, client_fd))
214  _dbus_verbose ("Rejected client connection due to lack of memory\n");
215  }
216  }
217 
218  if (flags & DBUS_WATCH_ERROR)
219  _dbus_verbose ("Error on server listening socket\n");
220 
221  if (flags & DBUS_WATCH_HANGUP)
222  _dbus_verbose ("Hangup on server listening socket\n");
223 
224  return TRUE;
225 }
226 
227 static void
228 socket_disconnect (DBusServer *server)
229 {
230  DBusServerSocket *socket_server = (DBusServerSocket*) server;
231  int i;
232 
233  HAVE_LOCK_CHECK (server);
234 
235  for (i = 0 ; i < socket_server->n_fds ; i++)
236  {
237  if (socket_server->watch[i])
238  {
240  socket_server->watch[i]);
241  _dbus_watch_invalidate (socket_server->watch[i]);
242  _dbus_watch_unref (socket_server->watch[i]);
243  socket_server->watch[i] = NULL;
244  }
245 
246  _dbus_close_socket (socket_server->fds[i], NULL);
247  _dbus_socket_invalidate (&socket_server->fds[i]);
248  }
249 
250  if (socket_server->socket_name != NULL)
251  {
252  DBusString tmp;
253  _dbus_string_init_const (&tmp, socket_server->socket_name);
254  _dbus_delete_file (&tmp, NULL);
255  }
256 
257  if (server->published_address)
258  _dbus_daemon_unpublish_session_bus_address();
259 
260  HAVE_LOCK_CHECK (server);
261 }
262 
263 static const DBusServerVTable socket_vtable = {
264  socket_finalize,
265  socket_disconnect
266 };
267 
284 DBusServer*
286  int n_fds,
287  const DBusString *address,
288  DBusNonceFile *noncefile,
289  DBusError *error)
290 {
291  DBusServerSocket *socket_server;
292  DBusServer *server;
293  int i;
294 
295  socket_server = dbus_new0 (DBusServerSocket, 1);
296  if (socket_server == NULL)
297  goto failed_0;
298 
299  socket_server->noncefile = noncefile;
300 
301  socket_server->fds = dbus_new (DBusSocket, n_fds);
302  if (!socket_server->fds)
303  goto failed_0;
304 
305  socket_server->watch = dbus_new0 (DBusWatch *, n_fds);
306  if (!socket_server->watch)
307  goto failed_1;
308 
309  for (i = 0 ; i < n_fds ; i++)
310  {
311  DBusWatch *watch;
312 
313  watch = _dbus_watch_new (_dbus_socket_get_pollable (fds[i]),
314  DBUS_WATCH_READABLE,
315  TRUE,
316  socket_handle_watch, socket_server,
317  NULL);
318  if (watch == NULL)
319  goto failed_2;
320 
321  socket_server->n_fds++;
322  socket_server->fds[i] = fds[i];
323  socket_server->watch[i] = watch;
324  }
325 
326  if (!_dbus_server_init_base (&socket_server->base,
327  &socket_vtable, address,
328  error))
329  goto failed_2;
330 
331  server = (DBusServer*)socket_server;
332 
333  SERVER_LOCK (server);
334 
335  for (i = 0 ; i < n_fds ; i++)
336  {
337  if (!_dbus_server_add_watch (&socket_server->base,
338  socket_server->watch[i]))
339  {
340  int j;
341  for (j = 0 ; j < i ; j++)
343  socket_server->watch[j]);
344 
345  SERVER_UNLOCK (server);
346  _dbus_server_finalize_base (&socket_server->base);
347  goto failed_2;
348  }
349  }
350 
351  SERVER_UNLOCK (server);
352 
353  _dbus_server_trace_ref (&socket_server->base, 0, 1, "new_for_socket");
354  return (DBusServer*) socket_server;
355 
356  failed_2:
357  for (i = 0 ; i < n_fds ; i++)
358  {
359  if (socket_server->watch[i] != NULL)
360  {
361  _dbus_watch_unref (socket_server->watch[i]);
362  socket_server->watch[i] = NULL;
363  }
364  }
365  dbus_free (socket_server->watch);
366 
367  failed_1:
368  dbus_free (socket_server->fds);
369 
370  failed_0:
371  dbus_free (socket_server);
372 
373  if (error != NULL && !dbus_error_is_set (error))
374  _DBUS_SET_OOM (error);
375 
376  return NULL;
377 }
378 
398 DBusServer*
400  const char *bind,
401  const char *port,
402  const char *family,
403  DBusError *error,
404  dbus_bool_t use_nonce)
405 {
406  DBusServer *server;
407  DBusSocket *listen_fds = NULL;
408  int nlisten_fds = 0, i;
409  DBusString address;
410  DBusString host_str;
411  DBusString port_str;
412  DBusNonceFile *noncefile;
413 
414  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
415 
416  noncefile = NULL;
417 
418  if (!_dbus_string_init (&address))
419  {
421  return NULL;
422  }
423 
424  if (!_dbus_string_init (&port_str))
425  {
427  goto failed_0;
428  }
429 
430  if (host == NULL)
431  host = "localhost";
432 
433  if (port == NULL)
434  port = "0";
435 
436  if (bind == NULL)
437  bind = host;
438  else if (strcmp (bind, "*") == 0)
439  bind = NULL;
440 
441  nlisten_fds =_dbus_listen_tcp_socket (bind, port, family,
442  &port_str,
443  &listen_fds, error);
444  if (nlisten_fds <= 0)
445  {
446  _DBUS_ASSERT_ERROR_IS_SET(error);
447  goto failed_1;
448  }
449 
450  _dbus_string_init_const (&host_str, host);
451  if (!_dbus_string_append (&address, use_nonce ? "nonce-tcp:host=" : "tcp:host=") ||
452  !_dbus_address_append_escaped (&address, &host_str) ||
453  !_dbus_string_append (&address, ",port=") ||
454  !_dbus_string_append (&address, _dbus_string_get_const_data(&port_str)))
455  {
457  goto failed_2;
458  }
459  if (family &&
460  (!_dbus_string_append (&address, ",family=") ||
461  !_dbus_string_append (&address, family)))
462  {
464  goto failed_2;
465  }
466 
467  if (use_nonce)
468  {
469  noncefile = dbus_new0 (DBusNonceFile, 1);
470  if (noncefile == NULL)
471  {
473  goto failed_2;
474  }
475 
476  if (!_dbus_noncefile_create (noncefile, error))
477  goto failed_3;
478 
479  if (!_dbus_string_append (&address, ",noncefile=") ||
480  !_dbus_address_append_escaped (&address, _dbus_noncefile_get_path (noncefile)))
481  {
483  goto failed_4;
484  }
485 
486  }
487 
488  server = _dbus_server_new_for_socket (listen_fds, nlisten_fds, &address, noncefile, error);
489  if (server == NULL)
490  {
491  if (noncefile != NULL)
492  goto failed_4;
493  else
494  goto failed_2;
495  }
496 
497  _dbus_string_free (&port_str);
498  _dbus_string_free (&address);
499  dbus_free(listen_fds);
500 
501  return server;
502 
503  failed_4:
504  _dbus_noncefile_delete (noncefile, NULL);
505 
506  failed_3:
507  dbus_free (noncefile);
508 
509  failed_2:
510  for (i = 0 ; i < nlisten_fds ; i++)
511  _dbus_close_socket (listen_fds[i], NULL);
512  dbus_free(listen_fds);
513 
514  failed_1:
515  _dbus_string_free (&port_str);
516 
517  failed_0:
518  _dbus_string_free (&address);
519 
520  return NULL;
521 }
522 
535 DBusServerListenResult
537  DBusServer **server_p,
538  DBusError *error)
539 {
540  const char *method;
541 
542  *server_p = NULL;
543 
544  method = dbus_address_entry_get_method (entry);
545 
546  if (strcmp (method, "tcp") == 0 || strcmp (method, "nonce-tcp") == 0)
547  {
548  const char *host;
549  const char *port;
550  const char *bind;
551  const char *family;
552 
553  host = dbus_address_entry_get_value (entry, "host");
554  bind = dbus_address_entry_get_value (entry, "bind");
555  port = dbus_address_entry_get_value (entry, "port");
556  family = dbus_address_entry_get_value (entry, "family");
557 
558  *server_p = _dbus_server_new_for_tcp_socket (host, bind, port,
559  family, error, strcmp (method, "nonce-tcp") == 0 ? TRUE : FALSE);
560 
561  if (*server_p)
562  {
563  _DBUS_ASSERT_ERROR_IS_CLEAR(error);
564  return DBUS_SERVER_LISTEN_OK;
565  }
566  else
567  {
568  _DBUS_ASSERT_ERROR_IS_SET(error);
569  return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
570  }
571  }
572  else
573  {
574  _DBUS_ASSERT_ERROR_IS_CLEAR(error);
575  return DBUS_SERVER_LISTEN_NOT_HANDLED;
576  }
577 }
578 
588 void
590  char *filename)
591 {
592  DBusServerSocket *socket_server = (DBusServerSocket*) server;
593 
594  socket_server->socket_name = filename;
595 }
596 
597 
dbus_bool_t _dbus_string_append(DBusString *str, const char *buffer)
Appends a nul-terminated C-style string to a DBusString.
Definition: dbus-string.c:935
dbus_bool_t _dbus_server_add_watch(DBusServer *server, DBusWatch *watch)
Adds a watch for this server, chaining out to application-provided watch handlers.
Definition: dbus-server.c:296
DBusWatch * _dbus_watch_new(DBusPollable fd, unsigned int flags, dbus_bool_t enabled, DBusWatchHandler handler, void *data, DBusFreeFunction free_data_function)
Creates a new DBusWatch.
Definition: dbus-watch.c:88
DBusNonceFile * noncefile
Nonce file used to authenticate clients.
Implementation of DBusWatch.
Definition: dbus-watch.c:40
#define NULL
A null pointer, defined appropriately for C or C++.
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition: dbus-memory.c:701
#define dbus_new(type, count)
Safe macro for using dbus_malloc().
Definition: dbus-memory.h:58
Internals of DBusServer object.
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
const char * dbus_address_entry_get_method(DBusAddressEntry *entry)
Returns the method string of an address entry.
Definition: dbus-address.c:227
void _dbus_connection_close_if_only_one_ref(DBusConnection *connection)
Used internally to handle the semantics of dbus_server_set_new_connection_function().
Implementation details of DBusConnection.
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
Definition: dbus-string.c:175
DBUS_PRIVATE_EXPORT void _dbus_server_ref_unlocked(DBusServer *server)
Like dbus_server_ref() but does not acquire the lock (must already be held)
Definition: dbus-server.c:456
DBusNewConnectionFunction new_connection_function
Callback to invoke when a new connection is created.
dbus_bool_t _dbus_close_socket(DBusSocket fd, DBusError *error)
Closes a socket.
DBusServer * _dbus_server_new_for_tcp_socket(const char *host, const char *bind, const char *port, const char *family, DBusError *error, dbus_bool_t use_nonce)
Creates a new server listening on TCP.
char ** auth_mechanisms
Array of allowed authentication mechanisms.
dbus_bool_t _dbus_delete_file(const DBusString *filename, DBusError *error)
Deletes the given file.
dbus_bool_t _dbus_transport_set_auth_mechanisms(DBusTransport *transport, const char **mechanisms)
Sets the SASL authentication mechanisms supported by this transport.
DBusServer * _dbus_server_new_for_socket(DBusSocket *fds, int n_fds, const DBusString *address, DBusNonceFile *noncefile, DBusError *error)
Creates a new server listening on the given file descriptor.
Socket interface.
Definition: dbus-sysdeps.h:148
const char * dbus_address_entry_get_value(DBusAddressEntry *entry, const char *key)
Returns a value from a key of an entry.
Definition: dbus-address.c:244
Implementation details of DBusServerSocket.
DBusTransport * _dbus_transport_new_for_socket(DBusSocket fd, const DBusString *server_guid, const DBusString *address)
Creates a new transport for the given socket file descriptor.
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
Definition: dbus-memory.h:59
dbus_bool_t _dbus_get_is_errno_eagain_or_ewouldblock(int e)
See if errno is EAGAIN or EWOULDBLOCK (this has to be done differently for Winsock so is abstracted) ...
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition: dbus-types.h:35
void _dbus_string_init_const(DBusString *str, const char *value)
Initializes a constant string.
Definition: dbus-string.c:190
void _dbus_server_socket_own_filename(DBusServer *server, char *filename)
This is a bad hack since it&#39;s really unix domain socket specific.
DBusSocket _dbus_accept(DBusSocket listen_fd)
Accepts a connection on a listening socket.
Internals of DBusAddressEntry.
Definition: dbus-address.c:43
void _dbus_server_remove_watch(DBusServer *server, DBusWatch *watch)
Removes a watch previously added with _dbus_server_remove_watch().
Definition: dbus-server.c:312
void _dbus_watch_invalidate(DBusWatch *watch)
Clears the file descriptor from a now-invalid watch object so that no one tries to use it...
Definition: dbus-watch.c:169
Object representing an exception.
Definition: dbus-errors.h:48
dbus_bool_t _dbus_address_append_escaped(DBusString *escaped, const DBusString *unescaped)
Appends an escaped version of one string to another string, using the D-Bus address escaping mechanis...
Definition: dbus-address.c:104
int _dbus_listen_tcp_socket(const char *host, const char *port, const char *family, DBusString *retport, DBusSocket **fds_p, DBusError *error)
Creates a socket and binds it to the given path, then listens on the socket.
DBusSocket * fds
File descriptor or DBUS_SOCKET_INVALID if disconnected.
void dbus_set_error(DBusError *error, const char *name, const char *format,...)
Assigns an error name and message to a DBusError.
Definition: dbus-errors.c:354
void _dbus_server_finalize_base(DBusServer *server)
Finalizes the members of the DBusServer base class.
Definition: dbus-server.c:201
Virtual table to be implemented by all server &quot;subclasses&quot;.
void dbus_server_unref(DBusServer *server)
Decrements the reference count of a DBusServer.
Definition: dbus-server.c:728
As in POLLERR (can&#39;t watch for this, but can be present in current state passed to dbus_watch_handle(...
DBusConnection * _dbus_connection_new_for_transport(DBusTransport *transport)
Creates a new connection for the given transport.
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init().
Definition: dbus-string.c:259
#define TRUE
Expands to &quot;1&quot;.
As in POLLHUP (can&#39;t watch for it, but can be present in current state passed to dbus_watch_handle())...
dbus_bool_t published_address
flag which indicates that server has published its bus address.
Object representing a transport such as a socket.
void * new_connection_data
Data for new connection callback.
DBusServerListenResult _dbus_server_listen_socket(DBusAddressEntry *entry, DBusServer **server_p, DBusError *error)
Tries to interpret the address entry for various socket-related addresses (well, currently only tcp a...
DBusServer base
Parent class members.
dbus_bool_t _dbus_server_init_base(DBusServer *server, const DBusServerVTable *vtable, const DBusString *address, DBusError *error)
Initializes the members of the DBusServer base class.
Definition: dbus-server.c:112
char * socket_name
Name of domain socket, to unlink if appropriate.
void _dbus_watch_unref(DBusWatch *watch)
Decrements the reference count of a DBusWatch object and finalizes the object if the count reaches ze...
Definition: dbus-watch.c:138
dbus_bool_t _dbus_set_socket_nonblocking(DBusSocket fd, DBusError *error)
Sets a file descriptor to be nonblocking.
DBusString guid_hex
Hex-encoded version of GUID.
#define DBUS_ERROR_NO_MEMORY
There was not enough memory to complete an operation.
#define FALSE
Expands to &quot;0&quot;.
int n_fds
Number of active file handles.
As in POLLIN.
void dbus_connection_unref(DBusConnection *connection)
Decrements the reference count of a DBusConnection, and finalizes it if the count reaches zero...
void(* DBusNewConnectionFunction)(DBusServer *server, DBusConnection *new_connection, void *data)
Called when a new connection to the server is available.
Definition: dbus-server.h:47
dbus_bool_t dbus_error_is_set(const DBusError *error)
Checks whether an error occurred (the error is set).
Definition: dbus-errors.c:329
void _dbus_transport_unref(DBusTransport *transport)
Decrements the reference count for the transport.
DBusWatch ** watch
File descriptor watch.