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 DBusServerSocket* socket_server;
93 void *new_connection_data;
95 socket_server = (DBusServerSocket*)server;
96 _dbus_verbose ("Creating new client connection with fd %d\n", client_fd);
98 HAVE_LOCK_CHECK (server);
100 if (!_dbus_set_fd_nonblocking (client_fd, NULL))
102 SERVER_UNLOCK (server);
106 transport = _dbus_transport_new_for_socket (client_fd, &server->guid_hex, FALSE);
107 if (transport == NULL)
109 _dbus_close_socket (client_fd, NULL);
110 SERVER_UNLOCK (server);
114 if (!_dbus_transport_set_auth_mechanisms (transport,
115 (const char **) server->auth_mechanisms))
117 _dbus_transport_unref (transport);
118 SERVER_UNLOCK (server);
122 /* note that client_fd is now owned by the transport, and will be
123 * closed on transport disconnection/finalization
126 connection = _dbus_connection_new_for_transport (transport);
127 _dbus_transport_unref (transport);
128 transport = NULL; /* now under the connection lock */
130 if (connection == NULL)
132 SERVER_UNLOCK (server);
136 /* See if someone wants to handle this new connection, self-referencing
139 new_connection_function = server->new_connection_function;
140 new_connection_data = server->new_connection_data;
142 _dbus_server_ref_unlocked (server);
143 SERVER_UNLOCK (server);
145 if (new_connection_function)
147 (* new_connection_function) (server, connection,
148 new_connection_data);
150 dbus_server_unref (server);
152 /* If no one grabbed a reference, the connection will die. */
153 _dbus_connection_close_if_only_one_ref (connection);
154 dbus_connection_unref (connection);
160 socket_handle_watch (DBusWatch *watch,
164 DBusServer *server = data;
165 DBusServerSocket *socket_server = data;
167 #ifndef DBUS_DISABLE_ASSERT
169 dbus_bool_t found = FALSE;
172 SERVER_LOCK (server);
174 #ifndef DBUS_DISABLE_ASSERT
175 for (i = 0 ; i < socket_server->n_fds ; i++)
177 if (socket_server->watch[i] == watch)
180 _dbus_assert (found);
183 _dbus_verbose ("Handling client connection, flags 0x%x\n", flags);
185 if (flags & DBUS_WATCH_READABLE)
190 listen_fd = dbus_watch_get_socket (watch);
192 if (socket_server->noncefile)
193 client_fd = _dbus_accept_with_noncefile (listen_fd, socket_server->noncefile);
195 client_fd = _dbus_accept (listen_fd);
199 /* EINTR handled for us */
201 if (_dbus_get_is_errno_eagain_or_ewouldblock ())
202 _dbus_verbose ("No client available to accept after all\n");
204 _dbus_verbose ("Failed to accept a client connection: %s\n",
205 _dbus_strerror_from_errno ());
207 SERVER_UNLOCK (server);
211 if (!handle_new_client_fd_and_unlock (server, client_fd))
212 _dbus_verbose ("Rejected client connection due to lack of memory\n");
216 if (flags & DBUS_WATCH_ERROR)
217 _dbus_verbose ("Error on server listening socket\n");
219 if (flags & DBUS_WATCH_HANGUP)
220 _dbus_verbose ("Hangup on server listening socket\n");
226 socket_disconnect (DBusServer *server)
228 DBusServerSocket *socket_server = (DBusServerSocket*) server;
231 HAVE_LOCK_CHECK (server);
233 for (i = 0 ; i < socket_server->n_fds ; i++)
235 if (socket_server->watch[i])
237 _dbus_server_remove_watch (server,
238 socket_server->watch[i]);
239 _dbus_watch_unref (socket_server->watch[i]);
240 socket_server->watch[i] = NULL;
243 _dbus_close_socket (socket_server->fds[i], NULL);
244 socket_server->fds[i] = -1;
247 if (socket_server->socket_name != NULL)
250 _dbus_string_init_const (&tmp, socket_server->socket_name);
251 _dbus_delete_file (&tmp, NULL);
254 HAVE_LOCK_CHECK (server);
257 static const DBusServerVTable socket_vtable = {
263 * Creates a new server listening on the given file descriptor. The
264 * file descriptor should be nonblocking (use
265 * _dbus_set_fd_nonblocking() to make it so). The file descriptor
266 * should be listening for connections, that is, listen() should have
267 * been successfully invoked on it. The server will use accept() to
268 * accept new client connections.
270 * @param fds list of file descriptors.
271 * @param n_fds number of file descriptors
272 * @param address the server's address
273 * @param use_nonce whether to create and use a nonce for authentication
274 * @returns the new server, or #NULL if no memory.
278 _dbus_server_new_for_socket (int *fds,
280 const DBusString *address,
281 DBusNonceFile *noncefile)
283 DBusServerSocket *socket_server;
287 socket_server = dbus_new0 (DBusServerSocket, 1);
288 if (socket_server == NULL)
291 socket_server->noncefile = noncefile;
293 socket_server->fds = dbus_new (int, n_fds);
294 if (!socket_server->fds)
297 socket_server->watch = dbus_new0 (DBusWatch *, n_fds);
298 if (!socket_server->watch)
301 for (i = 0 ; i < n_fds ; i++)
305 watch = _dbus_watch_new (fds[i],
308 socket_handle_watch, socket_server,
313 socket_server->n_fds++;
314 socket_server->fds[i] = fds[i];
315 socket_server->watch[i] = watch;
318 if (!_dbus_server_init_base (&socket_server->base,
319 &socket_vtable, address))
322 server = (DBusServer*)socket_server;
324 SERVER_LOCK (server);
326 for (i = 0 ; i < n_fds ; i++)
328 if (!_dbus_server_add_watch (&socket_server->base,
329 socket_server->watch[i]))
332 for (j = 0 ; j < i ; j++)
333 _dbus_server_remove_watch (server,
334 socket_server->watch[j]);
336 SERVER_UNLOCK (server);
337 _dbus_server_finalize_base (&socket_server->base);
342 SERVER_UNLOCK (server);
344 return (DBusServer*) socket_server;
347 if (socket_server->noncefile)
349 _dbus_noncefile_delete (socket_server->noncefile, NULL);
350 dbus_free (socket_server->noncefile );
353 for (i = 0 ; i < n_fds ; i++)
355 if (socket_server->watch[i] != NULL)
357 _dbus_watch_unref (socket_server->watch[i]);
358 socket_server->watch[i] = NULL;
361 dbus_free (socket_server->watch);
364 dbus_free (socket_server->fds);
367 dbus_free (socket_server);
372 * Creates a new server listening on TCP.
373 * If host is NULL, it will default to localhost.
374 * If bind is NULL, it will default to the value for the host
375 * parameter, and if that is NULL, then localhost
376 * If bind is a hostname, it will be resolved and will listen
377 * on all returned addresses.
378 * If family is NULL, hostname resolution will try all address
379 * families, otherwise it can be ipv4 or ipv6 to restrict the
380 * addresses considered.
382 * @param host the hostname to report for the listen address
383 * @param bind the hostname to listen on
384 * @param port the port to listen on or 0 to let the OS choose
386 * @param error location to store reason for failure.
387 * @param use_nonce whether to use a nonce for low-level authentication (nonce-tcp transport) or not (tcp transport)
388 * @returns the new server, or #NULL on failure.
391 _dbus_server_new_for_tcp_socket (const char *host,
396 dbus_bool_t use_nonce)
399 int *listen_fds = NULL;
400 int nlisten_fds = 0, i;
404 DBusNonceFile *noncefile;
406 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
410 if (!_dbus_string_init (&address))
412 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
416 if (!_dbus_string_init (&port_str))
418 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
430 else if (strcmp (bind, "*") == 0)
433 nlisten_fds =_dbus_listen_tcp_socket (bind, port, family,
436 if (nlisten_fds <= 0)
438 _DBUS_ASSERT_ERROR_IS_SET(error);
442 _dbus_string_init_const (&host_str, host);
443 if (!_dbus_string_append (&address, use_nonce ? "nonce-tcp:host=" : "tcp:host=") ||
444 !_dbus_address_append_escaped (&address, &host_str) ||
445 !_dbus_string_append (&address, ",port=") ||
446 !_dbus_string_append (&address, _dbus_string_get_const_data(&port_str)))
448 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
452 (!_dbus_string_append (&address, ",family=") ||
453 !_dbus_string_append (&address, family)))
455 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
461 noncefile = dbus_new0 (DBusNonceFile, 1);
462 if (noncefile == NULL)
467 if (!_dbus_noncefile_create (noncefile, NULL))
470 if (!_dbus_string_append (&address, ",noncefile=") ||
471 !_dbus_address_append_escaped (&address, _dbus_noncefile_get_path (noncefile)))
478 server = _dbus_server_new_for_socket (listen_fds, nlisten_fds, &address, noncefile);
481 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
485 _dbus_string_free (&port_str);
486 _dbus_string_free (&address);
487 dbus_free(listen_fds);
492 for (i = 0 ; i < nlisten_fds ; i++)
493 _dbus_close_socket (listen_fds[i], NULL);
494 dbus_free(listen_fds);
497 _dbus_string_free (&port_str);
500 _dbus_string_free (&address);
506 * Tries to interpret the address entry for various socket-related
507 * addresses (well, currently only tcp and nonce-tcp).
509 * Sets error if the result is not OK.
511 * @param entry an address entry
512 * @param server_p a new DBusServer, or #NULL on failure.
513 * @param error location to store rationale for failure on bad address
514 * @returns the outcome
517 DBusServerListenResult
518 _dbus_server_listen_socket (DBusAddressEntry *entry,
519 DBusServer **server_p,
526 method = dbus_address_entry_get_method (entry);
528 if (strcmp (method, "tcp") == 0 || strcmp (method, "nonce-tcp") == 0)
535 host = dbus_address_entry_get_value (entry, "host");
536 bind = dbus_address_entry_get_value (entry, "bind");
537 port = dbus_address_entry_get_value (entry, "port");
538 family = dbus_address_entry_get_value (entry, "family");
540 *server_p = _dbus_server_new_for_tcp_socket (host, bind, port,
541 family, error, strcmp (method, "nonce-tcp") == 0 ? TRUE : FALSE);
545 _DBUS_ASSERT_ERROR_IS_CLEAR(error);
546 return DBUS_SERVER_LISTEN_OK;
550 _DBUS_ASSERT_ERROR_IS_SET(error);
551 return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
556 _DBUS_ASSERT_ERROR_IS_CLEAR(error);
557 return DBUS_SERVER_LISTEN_NOT_HANDLED;
562 * This is a bad hack since it's really unix domain socket
563 * specific. Also, the function weirdly adopts ownership
564 * of the passed-in string.
566 * @param server a socket server
567 * @param filename socket filename to report/delete
571 _dbus_server_socket_own_filename (DBusServer *server,
574 DBusServerSocket *socket_server = (DBusServerSocket*) server;
576 socket_server->socket_name = filename;