1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-server-socket.c Server implementation for sockets
4 * Copyright (C) 2002, 2003, 2004, 2006 Red Hat Inc.
6 * Licensed under the Academic Free License version 2.1
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.
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.
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
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"
34 * @defgroup DBusServerSocket DBusServer implementations for SOCKET
35 * @ingroup DBusInternals
36 * @brief Implementation details of DBusServer on SOCKET
42 * Opaque object representing a Socket server implementation.
44 typedef struct DBusServerSocket DBusServerSocket;
47 * Implementation details of DBusServerSocket. All members
50 struct DBusServerSocket
52 DBusServer base; /**< Parent class members. */
53 int n_fds; /**< Number of active file handles */
54 int *fds; /**< File descriptor or -1 if disconnected. */
55 DBusWatch **watch; /**< File descriptor watch. */
56 char *socket_name; /**< Name of domain socket, to unlink if appropriate */
57 DBusNonceFile *noncefile; /**< Nonce file used to authenticate clients */
61 socket_finalize (DBusServer *server)
63 DBusServerSocket *socket_server = (DBusServerSocket*) server;
66 _dbus_server_finalize_base (server);
68 for (i = 0 ; i < socket_server->n_fds ; i++)
69 if (socket_server->watch[i])
71 _dbus_watch_unref (socket_server->watch[i]);
72 socket_server->watch[i] = NULL;
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);
84 /* Return value is just for memory, not other failures. */
86 handle_new_client_fd_and_unlock (DBusServer *server,
89 DBusConnection *connection;
90 DBusTransport *transport;
91 DBusNewConnectionFunction new_connection_function;
92 void *new_connection_data;
94 _dbus_verbose ("Creating new client connection with fd %d\n", client_fd);
96 HAVE_LOCK_CHECK (server);
98 if (!_dbus_set_fd_nonblocking (client_fd, NULL))
100 SERVER_UNLOCK (server);
104 transport = _dbus_transport_new_for_socket (client_fd, &server->guid_hex, FALSE);
105 if (transport == NULL)
107 _dbus_close_socket (client_fd, NULL);
108 SERVER_UNLOCK (server);
112 if (!_dbus_transport_set_auth_mechanisms (transport,
113 (const char **) server->auth_mechanisms))
115 _dbus_transport_unref (transport);
116 SERVER_UNLOCK (server);
120 /* note that client_fd is now owned by the transport, and will be
121 * closed on transport disconnection/finalization
124 connection = _dbus_connection_new_for_transport (transport);
125 _dbus_transport_unref (transport);
126 transport = NULL; /* now under the connection lock */
128 if (connection == NULL)
130 SERVER_UNLOCK (server);
134 /* See if someone wants to handle this new connection, self-referencing
137 new_connection_function = server->new_connection_function;
138 new_connection_data = server->new_connection_data;
140 _dbus_server_ref_unlocked (server);
141 SERVER_UNLOCK (server);
143 if (new_connection_function)
145 (* new_connection_function) (server, connection,
146 new_connection_data);
148 dbus_server_unref (server);
150 /* If no one grabbed a reference, the connection will die. */
151 _dbus_connection_close_if_only_one_ref (connection);
152 dbus_connection_unref (connection);
158 socket_handle_watch (DBusWatch *watch,
162 DBusServer *server = data;
163 DBusServerSocket *socket_server = data;
165 #ifndef DBUS_DISABLE_ASSERT
167 dbus_bool_t found = FALSE;
170 SERVER_LOCK (server);
172 #ifndef DBUS_DISABLE_ASSERT
173 for (i = 0 ; i < socket_server->n_fds ; i++)
175 if (socket_server->watch[i] == watch)
178 _dbus_assert (found);
181 _dbus_verbose ("Handling client connection, flags 0x%x\n", flags);
183 if (flags & DBUS_WATCH_READABLE)
188 listen_fd = dbus_watch_get_socket (watch);
190 if (socket_server->noncefile)
191 client_fd = _dbus_accept_with_noncefile (listen_fd, socket_server->noncefile);
193 client_fd = _dbus_accept (listen_fd);
197 /* EINTR handled for us */
199 if (_dbus_get_is_errno_eagain_or_ewouldblock ())
200 _dbus_verbose ("No client available to accept after all\n");
202 _dbus_verbose ("Failed to accept a client connection: %s\n",
203 _dbus_strerror_from_errno ());
205 SERVER_UNLOCK (server);
209 if (!handle_new_client_fd_and_unlock (server, client_fd))
210 _dbus_verbose ("Rejected client connection due to lack of memory\n");
214 if (flags & DBUS_WATCH_ERROR)
215 _dbus_verbose ("Error on server listening socket\n");
217 if (flags & DBUS_WATCH_HANGUP)
218 _dbus_verbose ("Hangup on server listening socket\n");
224 socket_disconnect (DBusServer *server)
226 DBusServerSocket *socket_server = (DBusServerSocket*) server;
229 HAVE_LOCK_CHECK (server);
231 for (i = 0 ; i < socket_server->n_fds ; i++)
233 if (socket_server->watch[i])
235 _dbus_server_remove_watch (server,
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;
242 _dbus_close_socket (socket_server->fds[i], NULL);
243 socket_server->fds[i] = -1;
246 if (socket_server->socket_name != NULL)
249 _dbus_string_init_const (&tmp, socket_server->socket_name);
250 _dbus_delete_file (&tmp, NULL);
253 if (server->published_address)
254 _dbus_daemon_unpublish_session_bus_address();
256 HAVE_LOCK_CHECK (server);
259 static const DBusServerVTable socket_vtable = {
265 * Creates a new server listening on the given file descriptor. The
266 * file descriptor should be nonblocking (use
267 * _dbus_set_fd_nonblocking() to make it so). The file descriptor
268 * should be listening for connections, that is, listen() should have
269 * been successfully invoked on it. The server will use accept() to
270 * accept new client connections.
272 * @param fds list of file descriptors.
273 * @param n_fds number of file descriptors
274 * @param address the server's address
275 * @param noncefile to be used for authentication (NULL if not needed)
276 * @returns the new server, or #NULL if no memory.
280 _dbus_server_new_for_socket (int *fds,
282 const DBusString *address,
283 DBusNonceFile *noncefile)
285 DBusServerSocket *socket_server;
289 socket_server = dbus_new0 (DBusServerSocket, 1);
290 if (socket_server == NULL)
293 socket_server->noncefile = noncefile;
295 socket_server->fds = dbus_new (int, n_fds);
296 if (!socket_server->fds)
299 socket_server->watch = dbus_new0 (DBusWatch *, n_fds);
300 if (!socket_server->watch)
303 for (i = 0 ; i < n_fds ; i++)
307 watch = _dbus_watch_new (fds[i],
310 socket_handle_watch, socket_server,
315 socket_server->n_fds++;
316 socket_server->fds[i] = fds[i];
317 socket_server->watch[i] = watch;
320 if (!_dbus_server_init_base (&socket_server->base,
321 &socket_vtable, address))
324 server = (DBusServer*)socket_server;
326 SERVER_LOCK (server);
328 for (i = 0 ; i < n_fds ; i++)
330 if (!_dbus_server_add_watch (&socket_server->base,
331 socket_server->watch[i]))
334 for (j = 0 ; j < i ; j++)
335 _dbus_server_remove_watch (server,
336 socket_server->watch[j]);
338 SERVER_UNLOCK (server);
339 _dbus_server_finalize_base (&socket_server->base);
344 SERVER_UNLOCK (server);
346 return (DBusServer*) socket_server;
349 for (i = 0 ; i < n_fds ; i++)
351 if (socket_server->watch[i] != NULL)
353 _dbus_watch_unref (socket_server->watch[i]);
354 socket_server->watch[i] = NULL;
357 dbus_free (socket_server->watch);
360 dbus_free (socket_server->fds);
363 dbus_free (socket_server);
368 * Creates a new server listening on TCP.
369 * If host is NULL, it will default to localhost.
370 * If bind is NULL, it will default to the value for the host
371 * parameter, and if that is NULL, then localhost
372 * If bind is a hostname, it will be resolved and will listen
373 * on all returned addresses.
374 * If family is NULL, hostname resolution will try all address
375 * families, otherwise it can be ipv4 or ipv6 to restrict the
376 * addresses considered.
378 * @param host the hostname to report for the listen address
379 * @param bind the hostname to listen on
380 * @param port the port to listen on or 0 to let the OS choose
382 * @param error location to store reason for failure.
383 * @param use_nonce whether to use a nonce for low-level authentication (nonce-tcp transport) or not (tcp transport)
384 * @returns the new server, or #NULL on failure.
387 _dbus_server_new_for_tcp_socket (const char *host,
392 dbus_bool_t use_nonce)
395 int *listen_fds = NULL;
396 int nlisten_fds = 0, i;
400 DBusNonceFile *noncefile;
402 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
406 if (!_dbus_string_init (&address))
408 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
412 if (!_dbus_string_init (&port_str))
414 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
426 else if (strcmp (bind, "*") == 0)
429 nlisten_fds =_dbus_listen_tcp_socket (bind, port, family,
432 if (nlisten_fds <= 0)
434 _DBUS_ASSERT_ERROR_IS_SET(error);
438 _dbus_string_init_const (&host_str, host);
439 if (!_dbus_string_append (&address, use_nonce ? "nonce-tcp:host=" : "tcp:host=") ||
440 !_dbus_address_append_escaped (&address, &host_str) ||
441 !_dbus_string_append (&address, ",port=") ||
442 !_dbus_string_append (&address, _dbus_string_get_const_data(&port_str)))
444 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
448 (!_dbus_string_append (&address, ",family=") ||
449 !_dbus_string_append (&address, family)))
451 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
457 noncefile = dbus_new0 (DBusNonceFile, 1);
458 if (noncefile == NULL)
460 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
464 if (!_dbus_noncefile_create (noncefile, error))
467 if (!_dbus_string_append (&address, ",noncefile=") ||
468 !_dbus_address_append_escaped (&address, _dbus_noncefile_get_path (noncefile)))
470 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
476 server = _dbus_server_new_for_socket (listen_fds, nlisten_fds, &address, noncefile);
479 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
483 _dbus_string_free (&port_str);
484 _dbus_string_free (&address);
485 dbus_free(listen_fds);
490 _dbus_noncefile_delete (noncefile, NULL);
493 dbus_free (noncefile);
496 for (i = 0 ; i < nlisten_fds ; i++)
497 _dbus_close_socket (listen_fds[i], NULL);
498 dbus_free(listen_fds);
501 _dbus_string_free (&port_str);
504 _dbus_string_free (&address);
510 * Tries to interpret the address entry for various socket-related
511 * addresses (well, currently only tcp and nonce-tcp).
513 * Sets error if the result is not OK.
515 * @param entry an address entry
516 * @param server_p a new DBusServer, or #NULL on failure.
517 * @param error location to store rationale for failure on bad address
518 * @returns the outcome
521 DBusServerListenResult
522 _dbus_server_listen_socket (DBusAddressEntry *entry,
523 DBusServer **server_p,
530 method = dbus_address_entry_get_method (entry);
532 if (strcmp (method, "tcp") == 0 || strcmp (method, "nonce-tcp") == 0)
539 host = dbus_address_entry_get_value (entry, "host");
540 bind = dbus_address_entry_get_value (entry, "bind");
541 port = dbus_address_entry_get_value (entry, "port");
542 family = dbus_address_entry_get_value (entry, "family");
544 *server_p = _dbus_server_new_for_tcp_socket (host, bind, port,
545 family, error, strcmp (method, "nonce-tcp") == 0 ? TRUE : FALSE);
549 _DBUS_ASSERT_ERROR_IS_CLEAR(error);
550 return DBUS_SERVER_LISTEN_OK;
554 _DBUS_ASSERT_ERROR_IS_SET(error);
555 return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
560 _DBUS_ASSERT_ERROR_IS_CLEAR(error);
561 return DBUS_SERVER_LISTEN_NOT_HANDLED;
566 * This is a bad hack since it's really unix domain socket
567 * specific. Also, the function weirdly adopts ownership
568 * of the passed-in string.
570 * @param server a socket server
571 * @param filename socket filename to report/delete
575 _dbus_server_socket_own_filename (DBusServer *server,
578 DBusServerSocket *socket_server = (DBusServerSocket*) server;
580 socket_server->socket_name = filename;