D-Bus  1.6.12
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;
54  int *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  int 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 %d\n", client_fd);
95 
96  HAVE_LOCK_CHECK (server);
97 
98  if (!_dbus_set_fd_nonblocking (client_fd, NULL))
99  {
100  SERVER_UNLOCK (server);
101  return TRUE;
102  }
103 
104  transport = _dbus_transport_new_for_socket (client_fd, &server->guid_hex, FALSE);
105  if (transport == NULL)
106  {
107  _dbus_close_socket (client_fd, NULL);
108  SERVER_UNLOCK (server);
109  return FALSE;
110  }
111 
112  if (!_dbus_transport_set_auth_mechanisms (transport,
113  (const char **) server->auth_mechanisms))
114  {
115  _dbus_transport_unref (transport);
116  SERVER_UNLOCK (server);
117  return FALSE;
118  }
119 
120  /* note that client_fd is now owned by the transport, and will be
121  * closed on transport disconnection/finalization
122  */
123 
124  connection = _dbus_connection_new_for_transport (transport);
125  _dbus_transport_unref (transport);
126  transport = NULL; /* now under the connection lock */
127 
128  if (connection == NULL)
129  {
130  SERVER_UNLOCK (server);
131  return FALSE;
132  }
133 
134  /* See if someone wants to handle this new connection, self-referencing
135  * for paranoia.
136  */
137  new_connection_function = server->new_connection_function;
138  new_connection_data = server->new_connection_data;
139 
140  _dbus_server_ref_unlocked (server);
141  SERVER_UNLOCK (server);
142 
143  if (new_connection_function)
144  {
145  (* new_connection_function) (server, connection,
146  new_connection_data);
147  }
148  dbus_server_unref (server);
149 
150  /* If no one grabbed a reference, the connection will die. */
152  dbus_connection_unref (connection);
153 
154  return TRUE;
155 }
156 
157 static dbus_bool_t
158 socket_handle_watch (DBusWatch *watch,
159  unsigned int flags,
160  void *data)
161 {
162  DBusServer *server = data;
163  DBusServerSocket *socket_server = data;
164 
165 #ifndef DBUS_DISABLE_ASSERT
166  int i;
167  dbus_bool_t found = FALSE;
168 #endif
169 
170  SERVER_LOCK (server);
171 
172 #ifndef DBUS_DISABLE_ASSERT
173  for (i = 0 ; i < socket_server->n_fds ; i++)
174  {
175  if (socket_server->watch[i] == watch)
176  found = TRUE;
177  }
178  _dbus_assert (found);
179 #endif
180 
181  _dbus_verbose ("Handling client connection, flags 0x%x\n", flags);
182 
183  if (flags & DBUS_WATCH_READABLE)
184  {
185  int client_fd;
186  int listen_fd;
187 
188  listen_fd = dbus_watch_get_socket (watch);
189 
190  if (socket_server->noncefile)
191  client_fd = _dbus_accept_with_noncefile (listen_fd, socket_server->noncefile);
192  else
193  client_fd = _dbus_accept (listen_fd);
194 
195  if (client_fd < 0)
196  {
197  /* EINTR handled for us */
198 
200  _dbus_verbose ("No client available to accept after all\n");
201  else
202  _dbus_verbose ("Failed to accept a client connection: %s\n",
204 
205  SERVER_UNLOCK (server);
206  }
207  else
208  {
209  if (!handle_new_client_fd_and_unlock (server, client_fd))
210  _dbus_verbose ("Rejected client connection due to lack of memory\n");
211  }
212  }
213 
214  if (flags & DBUS_WATCH_ERROR)
215  _dbus_verbose ("Error on server listening socket\n");
216 
217  if (flags & DBUS_WATCH_HANGUP)
218  _dbus_verbose ("Hangup on server listening socket\n");
219 
220  return TRUE;
221 }
222 
223 static void
224 socket_disconnect (DBusServer *server)
225 {
226  DBusServerSocket *socket_server = (DBusServerSocket*) server;
227  int i;
228 
229  HAVE_LOCK_CHECK (server);
230 
231  for (i = 0 ; i < socket_server->n_fds ; i++)
232  {
233  if (socket_server->watch[i])
234  {
236  socket_server->watch[i]);
237  _dbus_watch_invalidate (socket_server->watch[i]);
238  _dbus_watch_unref (socket_server->watch[i]);
239  socket_server->watch[i] = NULL;
240  }
241 
242  _dbus_close_socket (socket_server->fds[i], NULL);
243  socket_server->fds[i] = -1;
244  }
245 
246  if (socket_server->socket_name != NULL)
247  {
248  DBusString tmp;
249  _dbus_string_init_const (&tmp, socket_server->socket_name);
250  _dbus_delete_file (&tmp, NULL);
251  }
252 
253  if (server->published_address)
254  _dbus_daemon_unpublish_session_bus_address();
255 
256  HAVE_LOCK_CHECK (server);
257 }
258 
259 static const DBusServerVTable socket_vtable = {
260  socket_finalize,
261  socket_disconnect
262 };
263 
279 DBusServer*
281  int n_fds,
282  const DBusString *address,
283  DBusNonceFile *noncefile)
284 {
285  DBusServerSocket *socket_server;
286  DBusServer *server;
287  int i;
288 
289  socket_server = dbus_new0 (DBusServerSocket, 1);
290  if (socket_server == NULL)
291  return NULL;
292 
293  socket_server->noncefile = noncefile;
294 
295  socket_server->fds = dbus_new (int, n_fds);
296  if (!socket_server->fds)
297  goto failed_0;
298 
299  socket_server->watch = dbus_new0 (DBusWatch *, n_fds);
300  if (!socket_server->watch)
301  goto failed_1;
302 
303  for (i = 0 ; i < n_fds ; i++)
304  {
305  DBusWatch *watch;
306 
307  watch = _dbus_watch_new (fds[i],
308  DBUS_WATCH_READABLE,
309  TRUE,
310  socket_handle_watch, socket_server,
311  NULL);
312  if (watch == NULL)
313  goto failed_2;
314 
315  socket_server->n_fds++;
316  socket_server->fds[i] = fds[i];
317  socket_server->watch[i] = watch;
318  }
319 
320  if (!_dbus_server_init_base (&socket_server->base,
321  &socket_vtable, address))
322  goto failed_2;
323 
324  server = (DBusServer*)socket_server;
325 
326  SERVER_LOCK (server);
327 
328  for (i = 0 ; i < n_fds ; i++)
329  {
330  if (!_dbus_server_add_watch (&socket_server->base,
331  socket_server->watch[i]))
332  {
333  int j;
334  for (j = 0 ; j < i ; j++)
336  socket_server->watch[j]);
337 
338  SERVER_UNLOCK (server);
339  _dbus_server_finalize_base (&socket_server->base);
340  goto failed_2;
341  }
342  }
343 
344  SERVER_UNLOCK (server);
345 
346  _dbus_server_trace_ref (&socket_server->base, 0, 1, "new_for_socket");
347  return (DBusServer*) socket_server;
348 
349  failed_2:
350  for (i = 0 ; i < n_fds ; i++)
351  {
352  if (socket_server->watch[i] != NULL)
353  {
354  _dbus_watch_unref (socket_server->watch[i]);
355  socket_server->watch[i] = NULL;
356  }
357  }
358  dbus_free (socket_server->watch);
359 
360  failed_1:
361  dbus_free (socket_server->fds);
362 
363  failed_0:
364  dbus_free (socket_server);
365  return NULL;
366 }
367 
387 DBusServer*
389  const char *bind,
390  const char *port,
391  const char *family,
392  DBusError *error,
393  dbus_bool_t use_nonce)
394 {
395  DBusServer *server;
396  int *listen_fds = NULL;
397  int nlisten_fds = 0, i;
398  DBusString address;
399  DBusString host_str;
400  DBusString port_str;
401  DBusNonceFile *noncefile;
402 
403  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
404 
405  noncefile = NULL;
406 
407  if (!_dbus_string_init (&address))
408  {
410  return NULL;
411  }
412 
413  if (!_dbus_string_init (&port_str))
414  {
416  goto failed_0;
417  }
418 
419  if (host == NULL)
420  host = "localhost";
421 
422  if (port == NULL)
423  port = "0";
424 
425  if (bind == NULL)
426  bind = host;
427  else if (strcmp (bind, "*") == 0)
428  bind = NULL;
429 
430  nlisten_fds =_dbus_listen_tcp_socket (bind, port, family,
431  &port_str,
432  &listen_fds, error);
433  if (nlisten_fds <= 0)
434  {
435  _DBUS_ASSERT_ERROR_IS_SET(error);
436  goto failed_1;
437  }
438 
439  _dbus_string_init_const (&host_str, host);
440  if (!_dbus_string_append (&address, use_nonce ? "nonce-tcp:host=" : "tcp:host=") ||
441  !_dbus_address_append_escaped (&address, &host_str) ||
442  !_dbus_string_append (&address, ",port=") ||
443  !_dbus_string_append (&address, _dbus_string_get_const_data(&port_str)))
444  {
446  goto failed_2;
447  }
448  if (family &&
449  (!_dbus_string_append (&address, ",family=") ||
450  !_dbus_string_append (&address, family)))
451  {
453  goto failed_2;
454  }
455 
456  if (use_nonce)
457  {
458  noncefile = dbus_new0 (DBusNonceFile, 1);
459  if (noncefile == NULL)
460  {
462  goto failed_2;
463  }
464 
465  if (!_dbus_noncefile_create (noncefile, error))
466  goto failed_3;
467 
468  if (!_dbus_string_append (&address, ",noncefile=") ||
469  !_dbus_address_append_escaped (&address, _dbus_noncefile_get_path (noncefile)))
470  {
472  goto failed_4;
473  }
474 
475  }
476 
477  server = _dbus_server_new_for_socket (listen_fds, nlisten_fds, &address, noncefile);
478  if (server == NULL)
479  {
481  goto failed_4;
482  }
483 
484  _dbus_string_free (&port_str);
485  _dbus_string_free (&address);
486  dbus_free(listen_fds);
487 
488  return server;
489 
490  failed_4:
491  _dbus_noncefile_delete (noncefile, NULL);
492 
493  failed_3:
494  dbus_free (noncefile);
495 
496  failed_2:
497  for (i = 0 ; i < nlisten_fds ; i++)
498  _dbus_close_socket (listen_fds[i], NULL);
499  dbus_free(listen_fds);
500 
501  failed_1:
502  _dbus_string_free (&port_str);
503 
504  failed_0:
505  _dbus_string_free (&address);
506 
507  return NULL;
508 }
509 
522 DBusServerListenResult
524  DBusServer **server_p,
525  DBusError *error)
526 {
527  const char *method;
528 
529  *server_p = NULL;
530 
531  method = dbus_address_entry_get_method (entry);
532 
533  if (strcmp (method, "tcp") == 0 || strcmp (method, "nonce-tcp") == 0)
534  {
535  const char *host;
536  const char *port;
537  const char *bind;
538  const char *family;
539 
540  host = dbus_address_entry_get_value (entry, "host");
541  bind = dbus_address_entry_get_value (entry, "bind");
542  port = dbus_address_entry_get_value (entry, "port");
543  family = dbus_address_entry_get_value (entry, "family");
544 
545  *server_p = _dbus_server_new_for_tcp_socket (host, bind, port,
546  family, error, strcmp (method, "nonce-tcp") == 0 ? TRUE : FALSE);
547 
548  if (*server_p)
549  {
550  _DBUS_ASSERT_ERROR_IS_CLEAR(error);
551  return DBUS_SERVER_LISTEN_OK;
552  }
553  else
554  {
555  _DBUS_ASSERT_ERROR_IS_SET(error);
556  return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
557  }
558  }
559  else
560  {
561  _DBUS_ASSERT_ERROR_IS_CLEAR(error);
562  return DBUS_SERVER_LISTEN_NOT_HANDLED;
563  }
564 }
565 
575 void
577  char *filename)
578 {
579  DBusServerSocket *socket_server = (DBusServerSocket*) server;
580 
581  socket_server->socket_name = filename;
582 }
583 
584 
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:913
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:288
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:700
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
#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().
int _dbus_accept(int listen_fd)
Accepts a connection on a listening socket.
Implementation details of DBusConnection.
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
Definition: dbus-string.c:175
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:457
DBusTransport * _dbus_transport_new_for_socket(int fd, const DBusString *server_guid, const DBusString *address)
Creates a new transport for the given socket file descriptor.
int * fds
File descriptor or -1 if disconnected.
DBusNewConnectionFunction new_connection_function
Callback to invoke when a new connection is created.
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_transport_set_auth_mechanisms(DBusTransport *transport, const char **mechanisms)
Sets the SASL authentication mechanisms supported by this transport.
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.
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
Definition: dbus-memory.h:59
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
DBUS_EXPORT int dbus_watch_get_socket(DBusWatch *watch)
Returns a socket to be watched, on UNIX this will return -1 if our transport is not socket-based so d...
Definition: dbus-watch.c:564
void _dbus_server_socket_own_filename(DBusServer *server, char *filename)
This is a bad hack since it&#39;s really unix domain socket specific.
DBusServer * _dbus_server_new_for_socket(int *fds, int n_fds, const DBusString *address, DBusNonceFile *noncefile)
Creates a new server listening on the given file descriptor.
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:304
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
dbus_bool_t _dbus_server_init_base(DBusServer *server, const DBusServerVTable *vtable, const DBusString *address)
Initializes the members of the DBusServer base class.
Definition: dbus-server.c:111
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:193
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:731
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:242
#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.
const char * _dbus_strerror_from_errno(void)
Get error message from errno.
Definition: dbus-sysdeps.c:767
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.
DBusWatch * _dbus_watch_new(int 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
char * socket_name
Name of domain socket, to unlink if appropriate.
dbus_bool_t _dbus_get_is_errno_eagain_or_ewouldblock(void)
See if errno is EAGAIN or EWOULDBLOCK (this has to be done differently for Winsock so is abstracted) ...
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.
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
As in POLLIN.
void dbus_connection_unref(DBusConnection *connection)
Decrements the reference count of a DBusConnection, and finalizes it if the count reaches zero...
dbus_bool_t _dbus_close_socket(int fd, DBusError *error)
Closes a socket.
void(* DBusNewConnectionFunction)(DBusServer *server, DBusConnection *new_connection, void *data)
Called when a new connection to the server is available.
Definition: dbus-server.h:47
const char * _dbus_string_get_const_data(const DBusString *str)
Gets the raw character buffer from a const string.
Definition: dbus-string.c:446
dbus_bool_t _dbus_delete_file(const DBusString *filename, DBusError *error)
Deletes the given file.
void _dbus_transport_unref(DBusTransport *transport)
Decrements the reference count for the transport.
int _dbus_listen_tcp_socket(const char *host, const char *port, const char *family, DBusString *retport, int **fds_p, DBusError *error)
Creates a socket and binds it to the given path, then listens on the socket.
DBusWatch ** watch
File descriptor watch.