• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
33 /**
34  * @defgroup DBusServerSocket DBusServer implementations for SOCKET
35  * @ingroup  DBusInternals
36  * @brief Implementation details of DBusServer on SOCKET
37  *
38  * @{
39  */
40 /**
41  *
42  * Opaque object representing a Socket server implementation.
43  */
44 typedef struct DBusServerSocket DBusServerSocket;
45 
46 /**
47  * Implementation details of DBusServerSocket. All members
48  * are private.
49  */
50 struct DBusServerSocket
51 {
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 */
58 };
59 
60 static void
socket_finalize(DBusServer * server)61 socket_finalize (DBusServer *server)
62 {
63   DBusServerSocket *socket_server = (DBusServerSocket*) server;
64   int i;
65 
66   _dbus_server_finalize_base (server);
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
handle_new_client_fd_and_unlock(DBusServer * server,int client_fd)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   DBusServerSocket* socket_server;
93   void *new_connection_data;
94 
95   socket_server = (DBusServerSocket*)server;
96   _dbus_verbose ("Creating new client connection with fd %d\n", client_fd);
97 
98   HAVE_LOCK_CHECK (server);
99 
100   if (!_dbus_set_fd_nonblocking (client_fd, NULL))
101     {
102       SERVER_UNLOCK (server);
103       return TRUE;
104     }
105 
106   transport = _dbus_transport_new_for_socket (client_fd, &server->guid_hex, FALSE);
107   if (transport == NULL)
108     {
109       _dbus_close_socket (client_fd, NULL);
110       SERVER_UNLOCK (server);
111       return FALSE;
112     }
113 
114   if (!_dbus_transport_set_auth_mechanisms (transport,
115                                             (const char **) server->auth_mechanisms))
116     {
117       _dbus_transport_unref (transport);
118       SERVER_UNLOCK (server);
119       return FALSE;
120     }
121 
122   /* note that client_fd is now owned by the transport, and will be
123    * closed on transport disconnection/finalization
124    */
125 
126   connection = _dbus_connection_new_for_transport (transport);
127   _dbus_transport_unref (transport);
128   transport = NULL; /* now under the connection lock */
129 
130   if (connection == NULL)
131     {
132       SERVER_UNLOCK (server);
133       return FALSE;
134     }
135 
136   /* See if someone wants to handle this new connection, self-referencing
137    * for paranoia.
138    */
139   new_connection_function = server->new_connection_function;
140   new_connection_data = server->new_connection_data;
141 
142   _dbus_server_ref_unlocked (server);
143   SERVER_UNLOCK (server);
144 
145   if (new_connection_function)
146     {
147       (* new_connection_function) (server, connection,
148                                    new_connection_data);
149     }
150   dbus_server_unref (server);
151 
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);
155 
156   return TRUE;
157 }
158 
159 static dbus_bool_t
socket_handle_watch(DBusWatch * watch,unsigned int flags,void * data)160 socket_handle_watch (DBusWatch    *watch,
161                    unsigned int  flags,
162                    void         *data)
163 {
164   DBusServer *server = data;
165   DBusServerSocket *socket_server = data;
166 
167 #ifndef DBUS_DISABLE_ASSERT
168   int i;
169   dbus_bool_t found = FALSE;
170 #endif
171 
172   SERVER_LOCK (server);
173 
174 #ifndef DBUS_DISABLE_ASSERT
175   for (i = 0 ; i < socket_server->n_fds ; i++)
176     {
177       if (socket_server->watch[i] == watch)
178         found = TRUE;
179     }
180   _dbus_assert (found);
181 #endif
182 
183   _dbus_verbose ("Handling client connection, flags 0x%x\n", flags);
184 
185   if (flags & DBUS_WATCH_READABLE)
186     {
187       int client_fd;
188       int listen_fd;
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       if (client_fd < 0)
198         {
199           /* EINTR handled for us */
200 
201           if (_dbus_get_is_errno_eagain_or_ewouldblock ())
202             _dbus_verbose ("No client available to accept after all\n");
203           else
204             _dbus_verbose ("Failed to accept a client connection: %s\n",
205                            _dbus_strerror_from_errno ());
206 
207           SERVER_UNLOCK (server);
208         }
209       else
210         {
211           if (!handle_new_client_fd_and_unlock (server, client_fd))
212             _dbus_verbose ("Rejected client connection due to lack of memory\n");
213         }
214     }
215 
216   if (flags & DBUS_WATCH_ERROR)
217     _dbus_verbose ("Error on server listening socket\n");
218 
219   if (flags & DBUS_WATCH_HANGUP)
220     _dbus_verbose ("Hangup on server listening socket\n");
221 
222   return TRUE;
223 }
224 
225 static void
socket_disconnect(DBusServer * server)226 socket_disconnect (DBusServer *server)
227 {
228   DBusServerSocket *socket_server = (DBusServerSocket*) server;
229   int i;
230 
231   HAVE_LOCK_CHECK (server);
232 
233   for (i = 0 ; i < socket_server->n_fds ; i++)
234     {
235       if (socket_server->watch[i])
236         {
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;
241         }
242 
243       _dbus_close_socket (socket_server->fds[i], NULL);
244       socket_server->fds[i] = -1;
245     }
246 
247   if (socket_server->socket_name != NULL)
248     {
249       DBusString tmp;
250       _dbus_string_init_const (&tmp, socket_server->socket_name);
251       _dbus_delete_file (&tmp, NULL);
252     }
253 
254   HAVE_LOCK_CHECK (server);
255 }
256 
257 static const DBusServerVTable socket_vtable = {
258   socket_finalize,
259   socket_disconnect
260 };
261 
262 /**
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.
269  *
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.
275  *
276  */
277 DBusServer*
_dbus_server_new_for_socket(int * fds,int n_fds,const DBusString * address,DBusNonceFile * noncefile)278 _dbus_server_new_for_socket (int              *fds,
279                              int               n_fds,
280                              const DBusString *address,
281                              DBusNonceFile    *noncefile)
282 {
283   DBusServerSocket *socket_server;
284   DBusServer *server;
285   int i;
286 
287   socket_server = dbus_new0 (DBusServerSocket, 1);
288   if (socket_server == NULL)
289     return NULL;
290 
291   socket_server->noncefile = noncefile;
292 
293   socket_server->fds = dbus_new (int, n_fds);
294   if (!socket_server->fds)
295     goto failed_0;
296 
297   socket_server->watch = dbus_new0 (DBusWatch *, n_fds);
298   if (!socket_server->watch)
299     goto failed_1;
300 
301   for (i = 0 ; i < n_fds ; i++)
302     {
303       DBusWatch *watch;
304 
305       watch = _dbus_watch_new (fds[i],
306                                DBUS_WATCH_READABLE,
307                                TRUE,
308                                socket_handle_watch, socket_server,
309                                NULL);
310       if (watch == NULL)
311         goto failed_2;
312 
313       socket_server->n_fds++;
314       socket_server->fds[i] = fds[i];
315       socket_server->watch[i] = watch;
316     }
317 
318   if (!_dbus_server_init_base (&socket_server->base,
319                                &socket_vtable, address))
320     goto failed_2;
321 
322   server = (DBusServer*)socket_server;
323 
324   SERVER_LOCK (server);
325 
326   for (i = 0 ; i < n_fds ; i++)
327     {
328       if (!_dbus_server_add_watch (&socket_server->base,
329                                    socket_server->watch[i]))
330         {
331           int j;
332           for (j = 0 ; j < i ; j++)
333             _dbus_server_remove_watch (server,
334                                        socket_server->watch[j]);
335 
336           SERVER_UNLOCK (server);
337           _dbus_server_finalize_base (&socket_server->base);
338           goto failed_2;
339         }
340     }
341 
342   SERVER_UNLOCK (server);
343 
344   return (DBusServer*) socket_server;
345 
346  failed_3:
347   if (socket_server->noncefile)
348     {
349       _dbus_noncefile_delete (socket_server->noncefile, NULL);
350       dbus_free (socket_server->noncefile );
351     }
352  failed_2:
353   for (i = 0 ; i < n_fds ; i++)
354     {
355       if (socket_server->watch[i] != NULL)
356         {
357           _dbus_watch_unref (socket_server->watch[i]);
358           socket_server->watch[i] = NULL;
359         }
360     }
361   dbus_free (socket_server->watch);
362 
363  failed_1:
364   dbus_free (socket_server->fds);
365 
366  failed_0:
367   dbus_free (socket_server);
368   return NULL;
369 }
370 
371 /**
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.
381  *
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
385  * @param family
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.
389  */
390 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)391 _dbus_server_new_for_tcp_socket (const char     *host,
392                                  const char     *bind,
393                                  const char     *port,
394                                  const char     *family,
395                                  DBusError      *error,
396                                  dbus_bool_t    use_nonce)
397 {
398   DBusServer *server;
399   int *listen_fds = NULL;
400   int nlisten_fds = 0, i;
401   DBusString address;
402   DBusString host_str;
403   DBusString port_str;
404   DBusNonceFile *noncefile;
405 
406   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
407 
408   noncefile = NULL;
409 
410   if (!_dbus_string_init (&address))
411     {
412       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
413       return NULL;
414     }
415 
416   if (!_dbus_string_init (&port_str))
417     {
418       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
419       goto failed_0;
420     }
421 
422   if (host == NULL)
423     host = "localhost";
424 
425   if (port == NULL)
426     port = "0";
427 
428   if (bind == NULL)
429     bind = host;
430   else if (strcmp (bind, "*") == 0)
431     bind = NULL;
432 
433   nlisten_fds =_dbus_listen_tcp_socket (bind, port, family,
434                                         &port_str,
435                                         &listen_fds, error);
436   if (nlisten_fds <= 0)
437     {
438       _DBUS_ASSERT_ERROR_IS_SET(error);
439       goto failed_1;
440     }
441 
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)))
447     {
448       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
449       goto failed_2;
450     }
451   if (family &&
452       (!_dbus_string_append (&address, ",family=") ||
453        !_dbus_string_append (&address, family)))
454     {
455       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
456       goto failed_2;
457     }
458 
459   if (use_nonce)
460     {
461       noncefile = dbus_new0 (DBusNonceFile, 1);
462       if (noncefile == NULL)
463         {
464           goto failed_2;
465         }
466 
467       if (!_dbus_noncefile_create (noncefile, NULL))
468           goto failed_2;
469 
470       if (!_dbus_string_append (&address, ",noncefile=") ||
471           !_dbus_address_append_escaped (&address, _dbus_noncefile_get_path (noncefile)))
472         {
473           goto failed_2;
474         }
475 
476     }
477 
478   server = _dbus_server_new_for_socket (listen_fds, nlisten_fds, &address, noncefile);
479   if (server == NULL)
480     {
481       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
482       goto failed_2;
483     }
484 
485   _dbus_string_free (&port_str);
486   _dbus_string_free (&address);
487   dbus_free(listen_fds);
488 
489   return server;
490 
491  failed_2:
492   for (i = 0 ; i < nlisten_fds ; i++)
493     _dbus_close_socket (listen_fds[i], NULL);
494   dbus_free(listen_fds);
495 
496  failed_1:
497   _dbus_string_free (&port_str);
498 
499  failed_0:
500   _dbus_string_free (&address);
501 
502   return NULL;
503 }
504 
505 /**
506  * Tries to interpret the address entry for various socket-related
507  * addresses (well, currently only tcp and nonce-tcp).
508  *
509  * Sets error if the result is not OK.
510  *
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
515  *
516  */
517 DBusServerListenResult
_dbus_server_listen_socket(DBusAddressEntry * entry,DBusServer ** server_p,DBusError * error)518 _dbus_server_listen_socket (DBusAddressEntry *entry,
519                             DBusServer      **server_p,
520                             DBusError        *error)
521 {
522   const char *method;
523 
524   *server_p = NULL;
525 
526   method = dbus_address_entry_get_method (entry);
527 
528   if (strcmp (method, "tcp") == 0 || strcmp (method, "nonce-tcp") == 0)
529     {
530       const char *host;
531       const char *port;
532       const char *bind;
533       const char *family;
534 
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");
539 
540       *server_p = _dbus_server_new_for_tcp_socket (host, bind, port,
541                                                    family, error, strcmp (method, "nonce-tcp") == 0 ? TRUE : FALSE);
542 
543       if (*server_p)
544         {
545           _DBUS_ASSERT_ERROR_IS_CLEAR(error);
546           return DBUS_SERVER_LISTEN_OK;
547         }
548       else
549         {
550           _DBUS_ASSERT_ERROR_IS_SET(error);
551           return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
552         }
553     }
554   else
555     {
556       _DBUS_ASSERT_ERROR_IS_CLEAR(error);
557       return DBUS_SERVER_LISTEN_NOT_HANDLED;
558     }
559 }
560 
561 /**
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.
565  *
566  * @param server a socket server
567  * @param filename socket filename to report/delete
568  *
569  */
570 void
_dbus_server_socket_own_filename(DBusServer * server,char * filename)571 _dbus_server_socket_own_filename (DBusServer *server,
572                                   char       *filename)
573 {
574   DBusServerSocket *socket_server = (DBusServerSocket*) server;
575 
576   socket_server->socket_name = filename;
577 }
578 
579 
580 /** @} */
581 
582