Merge branch 'dbus-1.10'
[platform/upstream/dbus.git] / dbus / 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
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   DBusSocket *fds;   /**< File descriptor or DBUS_SOCKET_INVALID 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
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
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. */
152   _dbus_connection_close_if_only_one_ref (connection);
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
203           if (_dbus_get_is_errno_eagain_or_ewouldblock (saved_errno))
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         {
239           _dbus_server_remove_watch (server,
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
268 /**
269  * Creates a new server listening on the given file descriptor.  The
270  * file descriptor should be nonblocking (use
271  * _dbus_set_fd_nonblocking() to make it so). The file descriptor
272  * should be listening for connections, that is, listen() should have
273  * been successfully invoked on it. The server will use accept() to
274  * accept new client connections.
275  *
276  * @param fds list of file descriptors.
277  * @param n_fds number of file descriptors
278  * @param address the server's address
279  * @param noncefile to be used for authentication (NULL if not needed)
280  * @param error location to store reason for failure
281  * @returns the new server, or #NULL on OOM or other error.
282  *
283  */
284 DBusServer*
285 _dbus_server_new_for_socket (DBusSocket       *fds,
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++)
342             _dbus_server_remove_watch (server,
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
379 /**
380  * Creates a new server listening on TCP.
381  * If host is NULL, it will default to localhost.
382  * If bind is NULL, it will default to the value for the host
383  * parameter, and if that is NULL, then localhost
384  * If bind is a hostname, it will be resolved and will listen
385  * on all returned addresses.
386  * If family is NULL, hostname resolution will try all address
387  * families, otherwise it can be ipv4 or ipv6 to restrict the
388  * addresses considered.
389  *
390  * @param host the hostname to report for the listen address
391  * @param bind the hostname to listen on
392  * @param port the port to listen on or 0 to let the OS choose
393  * @param family
394  * @param error location to store reason for failure.
395  * @param use_nonce whether to use a nonce for low-level authentication (nonce-tcp transport) or not (tcp transport)
396  * @returns the new server, or #NULL on failure.
397  */
398 DBusServer*
399 _dbus_server_new_for_tcp_socket (const char     *host,
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     {
420       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
421       return NULL;
422     }
423
424   if (!_dbus_string_init (&port_str))
425     {
426       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
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     {
456       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
457       goto failed_2;
458     }
459   if (family &&
460       (!_dbus_string_append (&address, ",family=") ||
461        !_dbus_string_append (&address, family)))
462     {
463       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
464       goto failed_2;
465     }
466
467   if (use_nonce)
468     {
469       noncefile = dbus_new0 (DBusNonceFile, 1);
470       if (noncefile == NULL)
471         {
472           dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
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         {
482           dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
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
523 /**
524  * Tries to interpret the address entry for various socket-related
525  * addresses (well, currently only tcp and nonce-tcp).
526  *
527  * Sets error if the result is not OK.
528  *
529  * @param entry an address entry
530  * @param server_p a new DBusServer, or #NULL on failure.
531  * @param error location to store rationale for failure on bad address
532  * @returns the outcome
533  *
534  */
535 DBusServerListenResult
536 _dbus_server_listen_socket (DBusAddressEntry *entry,
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
579 /**
580  * This is a bad hack since it's really unix domain socket
581  * specific. Also, the function weirdly adopts ownership
582  * of the passed-in string.
583  *
584  * @param server a socket server
585  * @param filename socket filename to report/delete
586  *
587  */
588 void
589 _dbus_server_socket_own_filename (DBusServer *server,
590                                   char       *filename)
591 {
592   DBusServerSocket *socket_server = (DBusServerSocket*) server;
593
594   socket_server->socket_name = filename;
595 }
596
597
598 /** @} */
599