D-Bus  1.11.18
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 (DBusSocket 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) ) {
49  _dbus_string_free (&p);
50  _dbus_string_free (&buffer);
51  return FALSE;
52  }
53 
54  while (nleft)
55  {
56  int saved_errno;
57 
58  n = _dbus_read_socket (fd, &p, nleft);
59  saved_errno = _dbus_save_socket_errno ();
60 
61  if (n == -1 && _dbus_get_is_errno_eintr (saved_errno))
62  ;
63  else if (n == -1 && _dbus_get_is_errno_eagain_or_ewouldblock (saved_errno))
65  else if (n==-1)
66  {
67  dbus_set_error (error, DBUS_ERROR_IO_ERROR, "Could not read nonce from socket (fd=%" DBUS_SOCKET_FORMAT ")", _dbus_socket_printable (fd));
68  _dbus_string_free (&p);
69  _dbus_string_free (&buffer);
70  return FALSE;
71  }
72  else if (!n)
73  {
74  _dbus_string_free (&p);
75  _dbus_string_free (&buffer);
76  dbus_set_error (error, DBUS_ERROR_IO_ERROR, "Could not read nonce from socket (fd=%" DBUS_SOCKET_FORMAT ")", _dbus_socket_printable (fd));
77  return FALSE;
78  }
79  else
80  {
81  if (!_dbus_string_append_len (&buffer, _dbus_string_get_const_data (&p), n))
82  {
84  _dbus_string_free (&p);
85  _dbus_string_free (&buffer);
86  return FALSE;
87  }
88  nleft -= n;
89  }
90  }
91 
92  result = _dbus_string_equal_len (&buffer, nonce, 16);
93  if (!result)
94  dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED, "Nonces do not match, access denied (fd=%" DBUS_SOCKET_FORMAT ")", _dbus_socket_printable (fd));
95 
96  _dbus_string_free (&p);
97  _dbus_string_free (&buffer);
98 
99  return result;
100 }
101 
111 _dbus_read_nonce (const DBusString *fname, DBusString *nonce, DBusError* error)
112 {
113  FILE *fp;
114  char buffer[17];
115  size_t nread;
116 
117  buffer[sizeof buffer - 1] = '\0';
118 
119  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
120 
121  _dbus_verbose ("reading nonce from file: %s\n", _dbus_string_get_const_data (fname));
122 
123 
124  fp = fopen (_dbus_string_get_const_data (fname), "rb");
125  if (!fp)
126  {
127  dbus_set_error (error,
129  "Failed to open %s for read: %s",
130  _dbus_string_get_const_data (fname),
132  return FALSE;
133  }
134 
135  nread = fread (buffer, 1, sizeof buffer - 1, fp);
136  fclose (fp);
137  if (!nread)
138  {
139  dbus_set_error (error, DBUS_ERROR_FILE_NOT_FOUND, "Could not read nonce from file %s", _dbus_string_get_const_data (fname));
140  return FALSE;
141  }
142 
143  if (!_dbus_string_append_len (nonce, buffer, sizeof buffer - 1 ))
144  {
146  return FALSE;
147  }
148  return TRUE;
149 }
150 
152 _dbus_accept_with_noncefile (DBusSocket listen_fd, const DBusNonceFile *noncefile)
153 {
154  DBusSocket fd;
155  DBusString nonce;
156 
157  _dbus_assert (noncefile != NULL);
158  if (!_dbus_string_init (&nonce))
159  return _dbus_socket_get_invalid ();
160  //PENDING(kdab): set better errors
161  if (_dbus_read_nonce (_dbus_noncefile_get_path(noncefile), &nonce, NULL) != TRUE)
162  return _dbus_socket_get_invalid ();
163  fd = _dbus_accept (listen_fd);
164  if (!_dbus_socket_is_valid (fd))
165  return fd;
166  if (do_check_nonce(fd, &nonce, NULL) != TRUE) {
167  _dbus_verbose ("nonce check failed. Closing socket.\n");
169  return _dbus_socket_get_invalid ();
170  }
171 
172  return fd;
173 }
174 
175 static dbus_bool_t
176 generate_and_write_nonce (const DBusString *filename, DBusError *error)
177 {
178  DBusString nonce;
179  dbus_bool_t ret;
180 
181  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
182 
183  if (!_dbus_string_init (&nonce))
184  {
186  return FALSE;
187  }
188 
189  if (!_dbus_generate_random_bytes (&nonce, 16, error))
190  {
191  _dbus_string_free (&nonce);
192  return FALSE;
193  }
194 
195  ret = _dbus_string_save_to_file (&nonce, filename, FALSE, error);
196 
197  _dbus_string_free (&nonce);
198 
199  return ret;
200 }
201 
212 _dbus_send_nonce (DBusSocket fd,
213  const DBusString *noncefile,
214  DBusError *error)
215 {
216  dbus_bool_t read_result;
217  int send_result;
218  DBusString nonce;
219 
220  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
221 
222  if (_dbus_string_get_length (noncefile) == 0)
223  return FALSE;
224 
225  if (!_dbus_string_init (&nonce))
226  {
228  return FALSE;
229  }
230 
231  read_result = _dbus_read_nonce (noncefile, &nonce, error);
232  if (!read_result)
233  {
234  _DBUS_ASSERT_ERROR_IS_SET (error);
235  _dbus_string_free (&nonce);
236  return FALSE;
237  }
238  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
239 
240  send_result = _dbus_write_socket (fd, &nonce, 0, _dbus_string_get_length (&nonce));
241 
242  _dbus_string_free (&nonce);
243 
244  if (send_result == -1)
245  {
246  dbus_set_error (error,
248  "Failed to send nonce (fd=%" DBUS_SOCKET_FORMAT "): %s",
249  _dbus_socket_printable (fd),
251  return FALSE;
252  }
253 
254  return TRUE;
255 }
256 
257 static dbus_bool_t
258 do_noncefile_create (DBusNonceFile *noncefile,
259  DBusError *error,
260  dbus_bool_t use_subdir)
261 {
262  DBusString randomStr;
263  const char *tmp;
264 
265  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
266 
267  _dbus_assert (noncefile);
268 
269  if (!_dbus_string_init (&randomStr))
270  {
272  goto on_error;
273  }
274 
275  if (!_dbus_generate_random_ascii (&randomStr, 8, error))
276  {
277  goto on_error;
278  }
279 
280  tmp = _dbus_get_tmpdir ();
281 
282  if (!_dbus_string_init (&noncefile->dir)
283  || tmp == NULL
284  || !_dbus_string_append (&noncefile->dir, tmp))
285  {
287  goto on_error;
288  }
289  if (use_subdir)
290  {
291  if (!_dbus_string_append (&noncefile->dir, "/dbus_nonce-")
292  || !_dbus_string_append (&noncefile->dir, _dbus_string_get_const_data (&randomStr)) )
293  {
295  goto on_error;
296  }
297  if (!_dbus_string_init (&noncefile->path)
298  || !_dbus_string_copy (&noncefile->dir, 0, &noncefile->path, 0)
299  || !_dbus_string_append (&noncefile->path, "/nonce"))
300  {
302  goto on_error;
303  }
304  if (!_dbus_create_directory (&noncefile->dir, error))
305  {
306  _DBUS_ASSERT_ERROR_IS_SET (error);
307  goto on_error;
308  }
309  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
310 
311  }
312  else
313  {
314  if (!_dbus_string_init (&noncefile->path)
315  || !_dbus_string_copy (&noncefile->dir, 0, &noncefile->path, 0)
316  || !_dbus_string_append (&noncefile->path, "/dbus_nonce-")
317  || !_dbus_string_append (&noncefile->path, _dbus_string_get_const_data (&randomStr)))
318  {
320  goto on_error;
321  }
322 
323  }
324 
325  if (!generate_and_write_nonce (&noncefile->path, error))
326  {
327  _DBUS_ASSERT_ERROR_IS_SET (error);
328  if (use_subdir)
329  _dbus_delete_directory (&noncefile->dir, NULL); //we ignore possible errors deleting the dir and return the write error instead
330  goto on_error;
331  }
332  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
333 
334  _dbus_string_free (&randomStr);
335 
336  return TRUE;
337  on_error:
338  if (use_subdir)
339  _dbus_delete_directory (&noncefile->dir, NULL);
340  _dbus_string_free (&noncefile->dir);
341  _dbus_string_free (&noncefile->path);
342  _dbus_string_free (&randomStr);
343  return FALSE;
344 }
345 
346 #ifdef DBUS_WIN
347 
355 _dbus_noncefile_create (DBusNonceFile *noncefile,
356  DBusError *error)
357 {
358  return do_noncefile_create (noncefile, error, /*use_subdir=*/FALSE);
359 }
360 
369 _dbus_noncefile_delete (DBusNonceFile *noncefile,
370  DBusError *error)
371 {
372  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
373 
374  _dbus_delete_file (&noncefile->path, error);
375  _dbus_string_free (&noncefile->dir);
376  _dbus_string_free (&noncefile->path);
377  return TRUE;
378 }
379 
380 #else
381 
390 _dbus_noncefile_create (DBusNonceFile *noncefile,
391  DBusError *error)
392 {
393  return do_noncefile_create (noncefile, error, /*use_subdir=*/TRUE);
394 }
395 
404 _dbus_noncefile_delete (DBusNonceFile *noncefile,
405  DBusError *error)
406 {
407  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
408 
409  _dbus_delete_directory (&noncefile->dir, error);
410  _dbus_string_free (&noncefile->dir);
411  _dbus_string_free (&noncefile->path);
412  return TRUE;
413 }
414 #endif
415 
416 
423 const DBusString*
424 _dbus_noncefile_get_path (const DBusNonceFile *noncefile)
425 {
426  _dbus_assert (noncefile);
427  return &noncefile->path;
428 }
429 
441 _dbus_noncefile_check_nonce (DBusSocket fd,
442  const DBusNonceFile *noncefile,
443  DBusError* error)
444 {
445  return do_check_nonce (fd, _dbus_noncefile_get_path (noncefile), error);
446 }
447 
448 
dbus_bool_t _dbus_string_append(DBusString *str, const char *buffer)
Appends a nul-terminated C-style string to a DBusString.
Definition: dbus-string.c:935
#define DBUS_ERROR_FILE_NOT_FOUND
Missing file.
#define NULL
A null pointer, defined appropriately for C or C++.
dbus_bool_t _dbus_string_save_to_file(const DBusString *str, const DBusString *filename, dbus_bool_t world_readable, DBusError *error)
Writes a string out to a file.
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
int _dbus_read_socket(DBusSocket fd, DBusString *buffer, int count)
Like _dbus_read(), but only works on sockets so is available on Windows.
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
Definition: dbus-string.c:175
#define DBUS_ERROR_IO_ERROR
Something went wrong reading or writing to a socket, for example.
dbus_bool_t _dbus_string_copy(const DBusString *source, int start, DBusString *dest, int insert_at)
Like _dbus_string_move(), but does not delete the section of the source string that&#39;s copied to the d...
Definition: dbus-string.c:1283
dbus_bool_t _dbus_close_socket(DBusSocket fd, DBusError *error)
Closes a socket.
dbus_bool_t _dbus_delete_file(const DBusString *filename, DBusError *error)
Deletes the given file.
Socket interface.
Definition: dbus-sysdeps.h:149
dbus_bool_t _dbus_get_is_errno_eagain_or_ewouldblock(int e)
See if errno is EAGAIN or EWOULDBLOCK (this has to be done differently for Winsock so is abstracted) ...
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition: dbus-types.h:35
dbus_bool_t _dbus_generate_random_ascii(DBusString *str, int n_bytes, DBusError *error)
Generates the given number of random bytes, where the bytes are chosen from the alphanumeric ASCII su...
Definition: dbus-sysdeps.c:552
DBusSocket _dbus_accept(DBusSocket listen_fd)
Accepts a connection on a listening socket.
dbus_bool_t _dbus_create_directory(const DBusString *filename, DBusError *error)
Creates a directory.
dbus_bool_t _dbus_delete_directory(const DBusString *filename, DBusError *error)
Removes a directory; Directory must be empty.
dbus_bool_t _dbus_get_is_errno_eintr(int e)
See if errno is EINTR.
Definition: dbus-sysdeps.c:717
Object representing an exception.
Definition: dbus-errors.h:48
void dbus_set_error(DBusError *error, const char *name, const char *format,...)
Assigns an error name and message to a DBusError.
Definition: dbus-errors.c:354
dbus_bool_t _dbus_string_equal_len(const DBusString *a, const DBusString *b, int len)
Tests two DBusString for equality up to the given length.
Definition: dbus-string.c:2056
const char * _dbus_get_tmpdir(void)
Gets the temporary files directory by inspecting the environment variables TMPDIR, TMP, and TEMP in that order.
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init().
Definition: dbus-string.c:259
#define TRUE
Expands to "1".
const char * _dbus_strerror_from_errno(void)
Get error message from errno.
Definition: dbus-sysdeps.c:751
const char * _dbus_error_from_system_errno(void)
Converts the current system errno value into a DBusError name.
Definition: dbus-sysdeps.c:684
#define DBUS_ERROR_ACCESS_DENIED
Security restrictions don&#39;t allow doing what you&#39;re trying to do.
dbus_bool_t _dbus_string_append_len(DBusString *str, const char *buffer, int len)
Appends block of bytes with the given length to a DBusString.
Definition: dbus-string.c:1137
dbus_bool_t _dbus_generate_random_bytes(DBusString *str, int n_bytes, DBusError *error)
Generates the given number of securely random bytes, using the best mechanism we can come up with...
#define DBUS_ERROR_NO_MEMORY
There was not enough memory to complete an operation.
#define FALSE
Expands to "0".
int _dbus_write_socket(DBusSocket fd, const DBusString *buffer, int start, int len)
Like _dbus_write(), but only supports sockets and is thus available on Windows.
void _dbus_sleep_milliseconds(int milliseconds)
Sleeps the given number of milliseconds.