[daemon-fix] Fixed sending daemon match rules for kdbus broadcasts
[platform/upstream/dbus.git] / dbus / dbus-nonce.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-nonce.c  Nonce handling functions used by nonce-tcp (internal to D-Bus implementation)
3  *
4  * Copyright (C) 2009 Klaralvdalens Datakonsult AB, a KDAB Group company, info@kdab.net
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23
24 #include <config.h>
25 // major sections of this file are modified code from libassuan, (C) FSF
26 #include "dbus-nonce.h"
27 #include "dbus-internals.h"
28 #include "dbus-protocol.h"
29 #include "dbus-sysdeps.h"
30
31 #include <stdio.h>
32
33 static dbus_bool_t
34 do_check_nonce (int fd, const DBusString *nonce, DBusError *error)
35 {
36   DBusString buffer;
37   DBusString p;
38   size_t nleft;
39   dbus_bool_t result;
40   int n;
41
42   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
43
44   nleft = 16;
45
46   if (   !_dbus_string_init (&buffer)
47       || !_dbus_string_init (&p) ) {
48         dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
49         _dbus_string_free (&p);
50         _dbus_string_free (&buffer);
51         return FALSE;
52       }
53
54   while (nleft)
55     {
56       n = _dbus_read_socket (fd, &p, nleft);
57       if (n == -1 && _dbus_get_is_errno_eintr())
58         ;
59       else if (n == -1 && _dbus_get_is_errno_eagain_or_ewouldblock())
60         _dbus_sleep_milliseconds (100);
61       else if (n==-1)
62         {
63           dbus_set_error (error, DBUS_ERROR_IO_ERROR, "Could not read nonce from socket (fd=%d)", fd );
64           _dbus_string_free (&p);
65           _dbus_string_free (&buffer);
66           return FALSE;
67         }
68       else if (!n)
69         {
70           _dbus_string_free (&p);
71           _dbus_string_free (&buffer);
72           dbus_set_error (error, DBUS_ERROR_IO_ERROR, "Could not read nonce from socket (fd=%d)", fd );
73           return FALSE;
74         }
75       else
76         {
77           _dbus_string_append_len(&buffer, _dbus_string_get_const_data (&p), n);
78           nleft -= n;
79         }
80     }
81
82   result =  _dbus_string_equal_len (&buffer, nonce, 16);
83   if (!result)
84     dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED, "Nonces do not match, access denied (fd=%d)", fd );
85
86   _dbus_string_free (&p);
87   _dbus_string_free (&buffer);
88
89   return result;
90 }
91
92 /**
93  * reads the nonce from the nonce file and stores it in a string
94  *
95  * @param fname the file to read the nonce from
96  * @param nonce returns the nonce. Must be an initialized string, the nonce will be appended.
97  * @param error error object to report possible errors
98  * @return FALSE iff reading the nonce fails (error is set then)
99  */
100 dbus_bool_t
101 _dbus_read_nonce (const DBusString *fname, DBusString *nonce, DBusError* error)
102 {
103   FILE *fp;
104   char buffer[17];
105   size_t nread;
106
107   buffer[sizeof buffer - 1] = '\0';
108
109   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
110
111   _dbus_verbose ("reading nonce from file: %s\n", _dbus_string_get_const_data (fname));
112
113
114   fp = fopen (_dbus_string_get_const_data (fname), "rb");
115   if (!fp)
116     return FALSE;
117   nread = fread (buffer, 1, sizeof buffer - 1, fp);
118   fclose (fp);
119   if (!nread)
120     {
121       dbus_set_error (error, DBUS_ERROR_FILE_NOT_FOUND, "Could not read nonce from file %s", _dbus_string_get_const_data (fname));
122       return FALSE;
123     }
124
125   if (!_dbus_string_append_len (nonce, buffer, sizeof buffer - 1 ))
126     {
127       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
128       return FALSE;
129     }
130   return TRUE;
131 }
132
133 int
134 _dbus_accept_with_noncefile (int listen_fd, const DBusNonceFile *noncefile)
135 {
136   int fd;
137   DBusString nonce;
138
139   _dbus_assert (noncefile != NULL);
140   if (!_dbus_string_init (&nonce))
141     return -1;
142   //PENDING(kdab): set better errors
143   if (_dbus_read_nonce (_dbus_noncefile_get_path(noncefile), &nonce, NULL) != TRUE)
144     return -1;
145   fd = _dbus_accept (listen_fd);
146   if (_dbus_socket_is_invalid (fd))
147     return fd;
148   if (do_check_nonce(fd, &nonce, NULL) != TRUE) {
149     _dbus_verbose ("nonce check failed. Closing socket.\n");
150     _dbus_close_socket(fd, NULL);
151     return -1;
152   }
153
154   return fd;
155 }
156
157 static dbus_bool_t
158 generate_and_write_nonce (const DBusString *filename, DBusError *error)
159 {
160   DBusString nonce;
161   dbus_bool_t ret;
162
163   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
164
165   if (!_dbus_string_init (&nonce))
166     {
167       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
168       return FALSE;
169     }
170
171   if (!_dbus_generate_random_bytes (&nonce, 16))
172     {
173       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
174       _dbus_string_free (&nonce);
175       return FALSE;
176     }
177
178   ret = _dbus_string_save_to_file (&nonce, filename, FALSE, error);
179
180   _dbus_string_free (&nonce);
181
182   return ret;
183 }
184
185 /**
186  * sends the nonce over a given socket. Blocks while doing so.
187  *
188  * @param fd the file descriptor to write the nonce data to (usually a socket)
189  * @param noncefile the noncefile location to read the nonce from
190  * @param error contains error details if FALSE is returned
191  * @return TRUE iff the nonce was successfully sent. Note that this does not
192  * indicate whether the server accepted the nonce.
193  */
194 dbus_bool_t
195 _dbus_send_nonce (int fd, const DBusString *noncefile, DBusError *error)
196 {
197   dbus_bool_t read_result;
198   int send_result;
199   DBusString nonce;
200
201   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
202
203   if (_dbus_string_get_length (noncefile) == 0)
204     return FALSE;
205
206   if (!_dbus_string_init (&nonce))
207     {
208       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
209       return FALSE;
210     }
211
212   read_result = _dbus_read_nonce (noncefile, &nonce, error);
213   if (!read_result)
214     {
215       _DBUS_ASSERT_ERROR_IS_SET (error);
216       _dbus_string_free (&nonce);
217       return FALSE;
218     }
219   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
220
221   send_result = _dbus_write_socket (fd, &nonce, 0, _dbus_string_get_length (&nonce));
222
223   _dbus_string_free (&nonce);
224
225   if (send_result == -1)
226     {
227       dbus_set_error (error,
228                       _dbus_error_from_system_errno (),
229                       "Failed to send nonce (fd=%d): %s",
230                       fd, _dbus_strerror_from_errno ());
231       return FALSE;
232     }
233
234   return TRUE;
235 }
236
237 static dbus_bool_t
238 do_noncefile_create (DBusNonceFile *noncefile,
239                      DBusError *error,
240                      dbus_bool_t use_subdir)
241 {
242     DBusString randomStr;
243     const char *tmp;
244
245     _DBUS_ASSERT_ERROR_IS_CLEAR (error);
246
247     _dbus_assert (noncefile);
248
249     if (!_dbus_string_init (&randomStr))
250       {
251         dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
252         goto on_error;
253       }
254
255     if (!_dbus_generate_random_ascii (&randomStr, 8))
256       {
257         dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
258         goto on_error;
259       }
260
261     tmp = _dbus_get_tmpdir ();
262
263     if (!_dbus_string_init (&noncefile->dir)
264         || tmp == NULL
265         || !_dbus_string_append (&noncefile->dir, tmp))
266       {
267         dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
268         goto on_error;
269       }
270     if (use_subdir)
271       {
272         if (!_dbus_string_append (&noncefile->dir, "/dbus_nonce-")
273             || !_dbus_string_append (&noncefile->dir, _dbus_string_get_const_data (&randomStr)) )
274           {
275             dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
276             goto on_error;
277           }
278         if (!_dbus_string_init (&noncefile->path)
279             || !_dbus_string_copy (&noncefile->dir, 0, &noncefile->path, 0)
280             || !_dbus_string_append (&noncefile->path, "/nonce"))
281           {
282             dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
283             goto on_error;
284           }
285         if (!_dbus_create_directory (&noncefile->dir, error))
286           {
287             _DBUS_ASSERT_ERROR_IS_SET (error);
288             goto on_error;
289           }
290         _DBUS_ASSERT_ERROR_IS_CLEAR (error);
291
292       }
293     else
294       {
295         if (!_dbus_string_init (&noncefile->path)
296             || !_dbus_string_copy (&noncefile->dir, 0, &noncefile->path, 0)
297             || !_dbus_string_append (&noncefile->path, "/dbus_nonce-")
298             || !_dbus_string_append (&noncefile->path, _dbus_string_get_const_data (&randomStr)))
299           {
300             dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
301             goto on_error;
302           }
303
304       }
305
306     if (!generate_and_write_nonce (&noncefile->path, error))
307       {
308         _DBUS_ASSERT_ERROR_IS_SET (error);
309         if (use_subdir)
310           _dbus_delete_directory (&noncefile->dir, NULL); //we ignore possible errors deleting the dir and return the write error instead
311         goto on_error;
312       }
313     _DBUS_ASSERT_ERROR_IS_CLEAR (error);
314
315     _dbus_string_free (&randomStr);
316
317     return TRUE;
318   on_error:
319     if (use_subdir)
320       _dbus_delete_directory (&noncefile->dir, NULL);
321     _dbus_string_free (&noncefile->dir);
322     _dbus_string_free (&noncefile->path);
323     _dbus_string_free (&randomStr);
324     return FALSE;
325 }
326
327 #ifdef DBUS_WIN
328 /**
329  * creates a nonce file in a user-readable location and writes a generated nonce to it
330  *
331  * @param noncefile returns the nonce file location
332  * @param error error details if creating the nonce file fails
333  * @return TRUE iff the nonce file was successfully created
334  */
335 dbus_bool_t
336 _dbus_noncefile_create (DBusNonceFile *noncefile,
337                         DBusError *error)
338 {
339     return do_noncefile_create (noncefile, error, /*use_subdir=*/FALSE);
340 }
341
342 /**
343  * deletes the noncefile and frees the DBusNonceFile object.
344  *
345  * @param noncefile the nonce file to delete. Contents will be freed.
346  * @param error error details if the nonce file could not be deleted
347  * @return TRUE
348  */
349 dbus_bool_t
350 _dbus_noncefile_delete (DBusNonceFile *noncefile,
351                         DBusError *error)
352 {
353     _DBUS_ASSERT_ERROR_IS_CLEAR (error);
354
355     _dbus_delete_file (&noncefile->path, error);
356     _dbus_string_free (&noncefile->dir);
357     _dbus_string_free (&noncefile->path);
358     return TRUE;
359 }
360
361 #else
362 /**
363  * creates a nonce file in a user-readable location and writes a generated nonce to it.
364  * Initializes the noncefile object.
365  *
366  * @param noncefile returns the nonce file location
367  * @param error error details if creating the nonce file fails
368  * @return TRUE iff the nonce file was successfully created
369  */
370 dbus_bool_t
371 _dbus_noncefile_create (DBusNonceFile *noncefile,
372                         DBusError *error)
373 {
374     return do_noncefile_create (noncefile, error, /*use_subdir=*/TRUE);
375 }
376
377 /**
378  * deletes the noncefile and frees the DBusNonceFile object.
379  *
380  * @param noncefile the nonce file to delete. Contents will be freed.
381  * @param error error details if the nonce file could not be deleted
382  * @return TRUE
383  */
384 dbus_bool_t
385 _dbus_noncefile_delete (DBusNonceFile *noncefile,
386                         DBusError *error)
387 {
388     _DBUS_ASSERT_ERROR_IS_CLEAR (error);
389
390     _dbus_delete_directory (&noncefile->dir, error);
391     _dbus_string_free (&noncefile->dir);
392     _dbus_string_free (&noncefile->path);
393     return TRUE;
394 }
395 #endif
396
397
398 /**
399  * returns the absolute file path of the nonce file
400  *
401  * @param noncefile an initialized noncefile object
402  * @return the absolute path of the nonce file
403  */
404 const DBusString*
405 _dbus_noncefile_get_path (const DBusNonceFile *noncefile)
406 {
407     _dbus_assert (noncefile);
408     return &noncefile->path;
409 }
410
411 /**
412  * reads data from a file descriptor and checks if the received data matches
413  * the data in the given noncefile.
414  *
415  * @param fd the file descriptor to read the nonce from
416  * @param noncefile the nonce file to check the received data against
417  * @param error error details on fail
418  * @return TRUE iff a nonce could be successfully read from the file descriptor
419  * and matches the nonce from the given nonce file
420  */
421 dbus_bool_t
422 _dbus_noncefile_check_nonce (int fd,
423                              const DBusNonceFile *noncefile,
424                              DBusError* error)
425 {
426     return do_check_nonce (fd, _dbus_noncefile_get_path (noncefile), error);
427 }
428
429
430 /** @} end of nonce */