D-Bus  1.10.24
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=%d)", 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=%d)", 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=%d)", 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=%d): %s",
250  return FALSE;
251  }
252 
253  return TRUE;
254 }
255 
256 static dbus_bool_t
257 do_noncefile_create (DBusNonceFile *noncefile,
258  DBusError *error,
259  dbus_bool_t use_subdir)
260 {
261  DBusString randomStr;
262  const char *tmp;
263 
264  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
265 
266  _dbus_assert (noncefile);
267 
268  if (!_dbus_string_init (&randomStr))
269  {
271  goto on_error;
272  }
273 
274  if (!_dbus_generate_random_ascii (&randomStr, 8, error))
275  {
276  goto on_error;
277  }
278 
279  tmp = _dbus_get_tmpdir ();
280 
281  if (!_dbus_string_init (&noncefile->dir)
282  || tmp == NULL
283  || !_dbus_string_append (&noncefile->dir, tmp))
284  {
286  goto on_error;
287  }
288  if (use_subdir)
289  {
290  if (!_dbus_string_append (&noncefile->dir, "/dbus_nonce-")
291  || !_dbus_string_append (&noncefile->dir, _dbus_string_get_const_data (&randomStr)) )
292  {
294  goto on_error;
295  }
296  if (!_dbus_string_init (&noncefile->path)
297  || !_dbus_string_copy (&noncefile->dir, 0, &noncefile->path, 0)
298  || !_dbus_string_append (&noncefile->path, "/nonce"))
299  {
301  goto on_error;
302  }
303  if (!_dbus_create_directory (&noncefile->dir, error))
304  {
305  _DBUS_ASSERT_ERROR_IS_SET (error);
306  goto on_error;
307  }
308  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
309 
310  }
311  else
312  {
313  if (!_dbus_string_init (&noncefile->path)
314  || !_dbus_string_copy (&noncefile->dir, 0, &noncefile->path, 0)
315  || !_dbus_string_append (&noncefile->path, "/dbus_nonce-")
316  || !_dbus_string_append (&noncefile->path, _dbus_string_get_const_data (&randomStr)))
317  {
319  goto on_error;
320  }
321 
322  }
323 
324  if (!generate_and_write_nonce (&noncefile->path, error))
325  {
326  _DBUS_ASSERT_ERROR_IS_SET (error);
327  if (use_subdir)
328  _dbus_delete_directory (&noncefile->dir, NULL); //we ignore possible errors deleting the dir and return the write error instead
329  goto on_error;
330  }
331  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
332 
333  _dbus_string_free (&randomStr);
334 
335  return TRUE;
336  on_error:
337  if (use_subdir)
338  _dbus_delete_directory (&noncefile->dir, NULL);
339  _dbus_string_free (&noncefile->dir);
340  _dbus_string_free (&noncefile->path);
341  _dbus_string_free (&randomStr);
342  return FALSE;
343 }
344 
345 #ifdef DBUS_WIN
346 
354 _dbus_noncefile_create (DBusNonceFile *noncefile,
355  DBusError *error)
356 {
357  return do_noncefile_create (noncefile, error, /*use_subdir=*/FALSE);
358 }
359 
368 _dbus_noncefile_delete (DBusNonceFile *noncefile,
369  DBusError *error)
370 {
371  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
372 
373  _dbus_delete_file (&noncefile->path, error);
374  _dbus_string_free (&noncefile->dir);
375  _dbus_string_free (&noncefile->path);
376  return TRUE;
377 }
378 
379 #else
380 
389 _dbus_noncefile_create (DBusNonceFile *noncefile,
390  DBusError *error)
391 {
392  return do_noncefile_create (noncefile, error, /*use_subdir=*/TRUE);
393 }
394 
403 _dbus_noncefile_delete (DBusNonceFile *noncefile,
404  DBusError *error)
405 {
406  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
407 
408  _dbus_delete_directory (&noncefile->dir, error);
409  _dbus_string_free (&noncefile->dir);
410  _dbus_string_free (&noncefile->path);
411  return TRUE;
412 }
413 #endif
414 
415 
422 const DBusString*
423 _dbus_noncefile_get_path (const DBusNonceFile *noncefile)
424 {
425  _dbus_assert (noncefile);
426  return &noncefile->path;
427 }
428 
440 _dbus_noncefile_check_nonce (DBusSocket fd,
441  const DBusNonceFile *noncefile,
442  DBusError* error)
443 {
444  return do_check_nonce (fd, _dbus_noncefile_get_path (noncefile), error);
445 }
446 
447 
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:148
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:550
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:715
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 &quot;1&quot;.
const char * _dbus_strerror_from_errno(void)
Get error message from errno.
Definition: dbus-sysdeps.c:749
const char * _dbus_error_from_system_errno(void)
Converts the current system errno value into a DBusError name.
Definition: dbus-sysdeps.c:682
#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 &quot;0&quot;.
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.