28326ba70cd441d3dcc1a9c392d705469b050050
[platform/upstream/dbus.git] / dbus / dbus-transport.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-transport.c DBusTransport object (internal to D-BUS implementation)
3  *
4  * Copyright (C) 2002  Red Hat Inc.
5  *
6  * Licensed under the Academic Free License version 1.2
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23
24 #include "dbus-transport-protected.h"
25 #include "dbus-transport-unix.h"
26 #include "dbus-connection-internal.h"
27 #include "dbus-watch.h"
28 #include "dbus-auth.h"
29
30 /**
31  * @defgroup DBusTransport DBusTransport object
32  * @ingroup  DBusInternals
33  * @brief "Backend" for a DBusConnection.
34  *
35  * Types and functions related to DBusTransport.  A transport is an
36  * abstraction that can send and receive data via various kinds of
37  * network connections or other IPC mechanisms.
38  * 
39  * @{
40  */
41
42 /**
43  * @typedef DBusTransport
44  *
45  * Opaque object representing a way message stream.
46  * DBusTransport abstracts various kinds of actual
47  * transport mechanism, such as different network protocols,
48  * or encryption schemes.
49  */
50
51 /**
52  * Refs a transport and associated connection for reentrancy.
53  *
54  * @todo this macro reflects a design mistake, which is that the
55  * transport has a pointer to its connection. Ownership should move in
56  * only one direction; the connection should push/pull from the
57  * transport, rather than vice versa. Then the connection would take
58  * care of referencing itself when needed.
59  */
60 #define DBUS_TRANSPORT_HOLD_REF(t) \
61   _dbus_transport_ref (t); if ((t)->connection) dbus_connection_ref ((t)->connection)
62
63 /**
64  * Inverse of DBUS_TRANSPORT_HOLD_REF().
65  */
66 #define DBUS_TRANSPORT_RELEASE_REF(t) \
67   if ((t)->connection) dbus_connection_unref ((t)->connection); _dbus_transport_unref (t)
68
69
70 /**
71  * Initializes the base class members of DBusTransport.
72  * Chained up to by subclasses in their constructor.
73  *
74  * @param transport the transport being created.
75  * @param vtable the subclass vtable.
76  * @param server #TRUE if this transport is on the server side of a connection
77  * @returns #TRUE on success.
78  */
79 dbus_bool_t
80 _dbus_transport_init_base (DBusTransport             *transport,
81                            const DBusTransportVTable *vtable,
82                            dbus_bool_t                server)
83 {
84   DBusMessageLoader *loader;
85   DBusAuth *auth;
86   
87   loader = _dbus_message_loader_new ();
88   if (loader == NULL)
89     return FALSE;
90
91   if (server)
92     auth = _dbus_auth_server_new ();
93   else
94     auth = _dbus_auth_client_new ();
95   if (auth == NULL)
96     {
97       _dbus_message_loader_unref (loader);
98       return FALSE;
99     }
100   
101   transport->refcount = 1;
102   transport->vtable = vtable;
103   transport->loader = loader;
104   transport->auth = auth;
105   transport->authenticated = FALSE;
106   transport->messages_need_sending = FALSE;
107   transport->disconnected = FALSE;
108   transport->send_credentials_pending = !server;
109   transport->receive_credentials_pending = server;
110   transport->is_server = server;
111   
112   transport->credentials.pid = -1;
113   transport->credentials.uid = -1;
114   transport->credentials.gid = -1;
115   
116   return TRUE;
117 }
118
119 /**
120  * Finalizes base class members of DBusTransport.
121  * Chained up to from subclass finalizers.
122  *
123  * @param transport the transport.
124  */
125 void
126 _dbus_transport_finalize_base (DBusTransport *transport)
127 {
128   if (!transport->disconnected)
129     _dbus_transport_disconnect (transport);
130
131   _dbus_message_loader_unref (transport->loader);
132   _dbus_auth_unref (transport->auth);
133 }
134
135 /**
136  * Opens a new transport for the given address.  (This opens a
137  * client-side-of-the-connection transport.)
138  *
139  * @todo right now the address is just a Unix domain socket path.
140  * 
141  * @param address the address.
142  * @param result location to store reason for failure.
143  * @returns new transport of #NULL on failure.
144  */
145 DBusTransport*
146 _dbus_transport_open (const char     *address,
147                       DBusResultCode *result)
148 {
149   DBusTransport *transport;
150   
151   /* FIXME parse the address - whatever format
152    * we decide addresses are in - and find the
153    * appropriate transport.
154    */
155
156   /* Pretend it's just a unix domain socket name for now */
157   transport = _dbus_transport_new_for_domain_socket (address,
158                                                      FALSE,
159                                                      result);
160   
161   return transport;
162 }
163
164 /**
165  * Increments the reference count for the transport.
166  *
167  * @param transport the transport.
168  */
169 void
170 _dbus_transport_ref (DBusTransport *transport)
171 {
172   transport->refcount += 1;
173 }
174
175 /**
176  * Decrements the reference count for the transport.
177  * Disconnects and finalizes the transport if
178  * the reference count reaches zero.
179  *
180  * @param transport the transport.
181  */
182 void
183 _dbus_transport_unref (DBusTransport *transport)
184 {
185   _dbus_assert (transport != NULL);
186   _dbus_assert (transport->refcount > 0);
187
188   transport->refcount -= 1;
189   if (transport->refcount == 0)
190     {
191       _dbus_assert (transport->vtable->finalize != NULL);
192       
193       (* transport->vtable->finalize) (transport);
194     }
195 }
196
197 /**
198  * Closes our end of the connection to a remote application. Further
199  * attempts to use this transport will fail. Only the first call to
200  * _dbus_transport_disconnect() will have an effect.
201  *
202  * @param transport the transport.
203  * 
204  */
205 void
206 _dbus_transport_disconnect (DBusTransport *transport)
207 {
208   _dbus_assert (transport->vtable->disconnect != NULL);
209
210   if (transport->disconnected)
211     return;
212
213   DBUS_TRANSPORT_HOLD_REF (transport);
214   (* transport->vtable->disconnect) (transport);
215   
216   transport->disconnected = TRUE;
217
218   _dbus_connection_notify_disconnected (transport->connection);
219   
220   DBUS_TRANSPORT_RELEASE_REF (transport);
221 }
222
223 /**
224  * Returns #TRUE if the transport has not been disconnected.
225  * Disconnection can result from _dbus_transport_disconnect()
226  * or because the server drops its end of the connection.
227  *
228  * @param transport the transport.
229  * @returns whether we're connected
230  */
231 dbus_bool_t
232 _dbus_transport_get_is_connected (DBusTransport *transport)
233 {
234   return !transport->disconnected;
235 }
236
237 /**
238  * Returns #TRUE if we have been authenticated.  Will return #TRUE
239  * even if the transport is disconnected.
240  *
241  * @param transport the transport
242  * @returns whether we're authenticated
243  */
244 dbus_bool_t
245 _dbus_transport_get_is_authenticated (DBusTransport *transport)
246 {  
247   if (transport->authenticated)
248     return TRUE;
249   else
250     {
251       if (transport->disconnected)
252         return FALSE;
253       
254       transport->authenticated =
255         (!(transport->send_credentials_pending ||
256            transport->receive_credentials_pending)) &&
257         _dbus_auth_do_work (transport->auth) == DBUS_AUTH_STATE_AUTHENTICATED;
258
259       /* If we've authenticated as some identity, check that the auth
260        * identity is the same as our own identity.  In the future, we
261        * may have API allowing applications to specify how this is
262        * done, for example they may allow connection as any identity,
263        * but then impose restrictions on certain identities.
264        * Or they may give certain identities extra privileges.
265        */
266       
267       if (transport->authenticated && transport->is_server)
268         {
269           DBusCredentials auth_identity;
270           DBusCredentials our_identity;
271
272           _dbus_credentials_from_current_process (&our_identity);
273           _dbus_auth_get_identity (transport->auth, &auth_identity);
274           
275           if (!_dbus_credentials_match (&our_identity,
276                                         &auth_identity))
277             {
278               _dbus_verbose ("Client authorized as UID %d but our UID is %d, disconnecting\n",
279                              auth_identity.uid, our_identity.uid);
280               _dbus_transport_disconnect (transport);
281               return FALSE;
282             }
283           else
284             {
285               _dbus_verbose ("Client authorized as UID %d matching our UID %d\n",
286                              auth_identity.uid, our_identity.uid);
287             }
288         }
289       
290       return transport->authenticated;
291     }
292 }
293
294 /**
295  * Handles a watch by reading data, writing data, or disconnecting
296  * the transport, as appropriate for the given condition.
297  *
298  * @param transport the transport.
299  * @param watch the watch.
300  * @param condition the current state of the watched file descriptor.
301  */
302 void
303 _dbus_transport_handle_watch (DBusTransport           *transport,
304                               DBusWatch               *watch,
305                               unsigned int             condition)
306 {
307   _dbus_assert (transport->vtable->handle_watch != NULL);
308
309   if (transport->disconnected)
310     return;
311
312   if (dbus_watch_get_fd (watch) < 0)
313     {
314       _dbus_warn ("Tried to handle an invalidated watch; this watch should have been removed\n");
315       return;
316     }
317   
318   _dbus_watch_sanitize_condition (watch, &condition);
319
320   DBUS_TRANSPORT_HOLD_REF (transport);
321   _dbus_watch_ref (watch);
322   (* transport->vtable->handle_watch) (transport, watch, condition);
323   _dbus_watch_unref (watch);
324   DBUS_TRANSPORT_RELEASE_REF (transport);
325 }
326
327 /**
328  * Sets the connection using this transport. Allows the transport
329  * to add watches to the connection, queue incoming messages,
330  * and pull outgoing messages.
331  *
332  * @param transport the transport.
333  * @param connection the connection.
334  */
335 void
336 _dbus_transport_set_connection (DBusTransport  *transport,
337                                 DBusConnection *connection)
338 {
339   _dbus_assert (transport->vtable->connection_set != NULL);
340   _dbus_assert (transport->connection == NULL);
341   
342   transport->connection = connection;
343
344   DBUS_TRANSPORT_HOLD_REF (transport);
345   (* transport->vtable->connection_set) (transport);
346   DBUS_TRANSPORT_RELEASE_REF (transport);
347 }
348
349 /**
350  * Notifies the transport when the outgoing message queue goes from
351  * empty to non-empty or vice versa. Typically causes the transport to
352  * add or remove its DBUS_WATCH_WRITABLE watch.
353  *
354  * @param transport the transport.
355  * @param queue_length the length of the outgoing message queue.
356  *
357  */
358 void
359 _dbus_transport_messages_pending (DBusTransport  *transport,
360                                   int             queue_length)
361 {
362   _dbus_assert (transport->vtable->messages_pending != NULL);
363
364   if (transport->disconnected)
365     return;
366
367   transport->messages_need_sending = queue_length > 0;
368
369   DBUS_TRANSPORT_HOLD_REF (transport);
370   (* transport->vtable->messages_pending) (transport,
371                                            queue_length);
372   DBUS_TRANSPORT_RELEASE_REF (transport);
373 }
374
375 /**
376  * Performs a single poll()/select() on the transport's file
377  * descriptors and then reads/writes data as appropriate,
378  * queueing incoming messages and sending outgoing messages.
379  * This is the backend for _dbus_connection_do_iteration().
380  * See _dbus_connection_do_iteration() for full details.
381  *
382  * @param transport the transport.
383  * @param flags indicates whether to read or write, and whether to block.
384  * @param timeout_milliseconds if blocking, timeout or -1 for no timeout.
385  */
386 void
387 _dbus_transport_do_iteration (DBusTransport  *transport,
388                               unsigned int    flags,
389                               int             timeout_milliseconds)
390 {
391   _dbus_assert (transport->vtable->do_iteration != NULL);
392
393   if ((flags & (DBUS_ITERATION_DO_WRITING |
394                 DBUS_ITERATION_DO_READING)) == 0)
395     return; /* Nothing to do */
396
397   if (transport->disconnected)
398     return;
399
400   DBUS_TRANSPORT_HOLD_REF (transport);
401   (* transport->vtable->do_iteration) (transport, flags,
402                                        timeout_milliseconds);
403   DBUS_TRANSPORT_RELEASE_REF (transport);
404 }
405
406 /** @} */