D-Bus  1.8.20
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) ) {
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())
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  if (!_dbus_string_append_len (&buffer, _dbus_string_get_const_data (&p), n))
78  {
80  _dbus_string_free (&p);
81  _dbus_string_free (&buffer);
82  return FALSE;
83  }
84  nleft -= n;
85  }
86  }
87 
88  result = _dbus_string_equal_len (&buffer, nonce, 16);
89  if (!result)
90  dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED, "Nonces do not match, access denied (fd=%d)", fd );
91 
92  _dbus_string_free (&p);
93  _dbus_string_free (&buffer);
94 
95  return result;
96 }
97 
107 _dbus_read_nonce (const DBusString *fname, DBusString *nonce, DBusError* error)
108 {
109  FILE *fp;
110  char buffer[17];
111  size_t nread;
112 
113  buffer[sizeof buffer - 1] = '\0';
114 
115  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
116 
117  _dbus_verbose ("reading nonce from file: %s\n", _dbus_string_get_const_data (fname));
118 
119 
120  fp = fopen (_dbus_string_get_const_data (fname), "rb");
121  if (!fp)
122  {
123  dbus_set_error (error,
125  "Failed to open %s for read: %s",
126  _dbus_string_get_const_data (fname),
128  return FALSE;
129  }
130 
131  nread = fread (buffer, 1, sizeof buffer - 1, fp);
132  fclose (fp);
133  if (!nread)
134  {
135  dbus_set_error (error, DBUS_ERROR_FILE_NOT_FOUND, "Could not read nonce from file %s", _dbus_string_get_const_data (fname));
136  return FALSE;
137  }
138 
139  if (!_dbus_string_append_len (nonce, buffer, sizeof buffer - 1 ))
140  {
142  return FALSE;
143  }
144  return TRUE;
145 }
146 
147 int
148 _dbus_accept_with_noncefile (int listen_fd, const DBusNonceFile *noncefile)
149 {
150  int fd;
151  DBusString nonce;
152 
153  _dbus_assert (noncefile != NULL);
154  if (!_dbus_string_init (&nonce))
155  return -1;
156  //PENDING(kdab): set better errors
157  if (_dbus_read_nonce (_dbus_noncefile_get_path(noncefile), &nonce, NULL) != TRUE)
158  return -1;
159  fd = _dbus_accept (listen_fd);
160  if (_dbus_socket_is_invalid (fd))
161  return fd;
162  if (do_check_nonce(fd, &nonce, NULL) != TRUE) {
163  _dbus_verbose ("nonce check failed. Closing socket.\n");
165  return -1;
166  }
167 
168  return fd;
169 }
170 
171 static dbus_bool_t
172 generate_and_write_nonce (const DBusString *filename, DBusError *error)
173 {
174  DBusString nonce;
175  dbus_bool_t ret;
176 
177  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
178 
179  if (!_dbus_string_init (&nonce))
180  {
182  return FALSE;
183  }
184 
185  if (!_dbus_generate_random_bytes (&nonce, 16))
186  {
188  _dbus_string_free (&nonce);
189  return FALSE;
190  }
191 
192  ret = _dbus_string_save_to_file (&nonce, filename, FALSE, error);
193 
194  _dbus_string_free (&nonce);
195 
196  return ret;
197 }
198 
209 _dbus_send_nonce (int fd, const DBusString *noncefile, DBusError *error)
210 {
211  dbus_bool_t read_result;
212  int send_result;
213  DBusString nonce;
214 
215  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
216 
217  if (_dbus_string_get_length (noncefile) == 0)
218  return FALSE;
219 
220  if (!_dbus_string_init (&nonce))
221  {
223  return FALSE;
224  }
225 
226  read_result = _dbus_read_nonce (noncefile, &nonce, error);
227  if (!read_result)
228  {
229  _DBUS_ASSERT_ERROR_IS_SET (error);
230  _dbus_string_free (&nonce);
231  return FALSE;
232  }
233  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
234 
235  send_result = _dbus_write_socket (fd, &nonce, 0, _dbus_string_get_length (&nonce));
236 
237  _dbus_string_free (&nonce);
238 
239  if (send_result == -1)
240  {
241  dbus_set_error (error,
243  "Failed to send nonce (fd=%d): %s",
245  return FALSE;
246  }
247 
248  return TRUE;
249 }
250 
251 static dbus_bool_t
252 do_noncefile_create (DBusNonceFile *noncefile,
253  DBusError *error,
254  dbus_bool_t use_subdir)
255 {
256  DBusString randomStr;
257  const char *tmp;
258 
259  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
260 
261  _dbus_assert (noncefile);
262 
263  if (!_dbus_string_init (&randomStr))
264  {
266  goto on_error;
267  }
268 
269  if (!_dbus_generate_random_ascii (&randomStr, 8))
270  {
272  goto on_error;
273  }
274 
275  tmp = _dbus_get_tmpdir ();
276 
277  if (!_dbus_string_init (&noncefile->dir)
278  || tmp == NULL
279  || !_dbus_string_append (&noncefile->dir, tmp))
280  {
282  goto on_error;
283  }
284  if (use_subdir)
285  {
286  if (!_dbus_string_append (&noncefile->dir, "/dbus_nonce-")
287  || !_dbus_string_append (&noncefile->dir, _dbus_string_get_const_data (&randomStr)) )
288  {
290  goto on_error;
291  }
292  if (!_dbus_string_init (&noncefile->path)
293  || !_dbus_string_copy (&noncefile->dir, 0, &noncefile->path, 0)
294  || !_dbus_string_append (&noncefile->path, "/nonce"))
295  {
297  goto on_error;
298  }
299  if (!_dbus_create_directory (&noncefile->dir, error))
300  {
301  _DBUS_ASSERT_ERROR_IS_SET (error);
302  goto on_error;
303  }
304  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
305 
306  }
307  else
308  {
309  if (!_dbus_string_init (&noncefile->path)
310  || !_dbus_string_copy (&noncefile->dir, 0, &noncefile->path, 0)
311  || !_dbus_string_append (&noncefile->path, "/dbus_nonce-")
312  || !_dbus_string_append (&noncefile->path, _dbus_string_get_const_data (&randomStr)))
313  {
315  goto on_error;
316  }
317 
318  }
319 
320  if (!generate_and_write_nonce (&noncefile->path, error))
321  {
322  _DBUS_ASSERT_ERROR_IS_SET (error);
323  if (use_subdir)
324  _dbus_delete_directory (&noncefile->dir, NULL); //we ignore possible errors deleting the dir and return the write error instead
325  goto on_error;
326  }
327  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
328 
329  _dbus_string_free (&randomStr);
330 
331  return TRUE;
332  on_error:
333  if (use_subdir)
334  _dbus_delete_directory (&noncefile->dir, NULL);
335  _dbus_string_free (&noncefile->dir);
336  _dbus_string_free (&noncefile->path);
337  _dbus_string_free (&randomStr);
338  return FALSE;
339 }
340 
341 #ifdef DBUS_WIN
342 
350 _dbus_noncefile_create (DBusNonceFile *noncefile,
351  DBusError *error)
352 {
353  return do_noncefile_create (noncefile, error, /*use_subdir=*/FALSE);
354 }
355 
364 _dbus_noncefile_delete (DBusNonceFile *noncefile,
365  DBusError *error)
366 {
367  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
368 
369  _dbus_delete_file (&noncefile->path, error);
370  _dbus_string_free (&noncefile->dir);
371  _dbus_string_free (&noncefile->path);
372  return TRUE;
373 }
374 
375 #else
376 
385 _dbus_noncefile_create (DBusNonceFile *noncefile,
386  DBusError *error)
387 {
388  return do_noncefile_create (noncefile, error, /*use_subdir=*/TRUE);
389 }
390 
399 _dbus_noncefile_delete (DBusNonceFile *noncefile,
400  DBusError *error)
401 {
402  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
403 
404  _dbus_delete_directory (&noncefile->dir, error);
405  _dbus_string_free (&noncefile->dir);
406  _dbus_string_free (&noncefile->path);
407  return TRUE;
408 }
409 #endif
410 
411 
418 const DBusString*
419 _dbus_noncefile_get_path (const DBusNonceFile *noncefile)
420 {
421  _dbus_assert (noncefile);
422  return &noncefile->path;
423 }
424 
436 _dbus_noncefile_check_nonce (int fd,
437  const DBusNonceFile *noncefile,
438  DBusError* error)
439 {
440  return do_check_nonce (fd, _dbus_noncefile_get_path (noncefile), error);
441 }
442 
443 
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:918
#define DBUS_ERROR_FILE_NOT_FOUND
Missing file.
#define NULL
A null pointer, defined appropriately for C or C++.
dbus_bool_t _dbus_delete_directory(const DBusString *filename, DBusError *error)
Removes a directory; Directory must be empty.
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_accept(int listen_fd)
Accepts a connection on a listening socket.
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's copied to the d...
Definition: dbus-string.c:1265
dbus_bool_t _dbus_generate_random_ascii(DBusString *str, int n_bytes)
Generates the given number of random bytes, where the bytes are chosen from the alphanumeric ASCII su...
Definition: dbus-sysdeps.c:575
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition: dbus-types.h:35
int _dbus_read_socket(int fd, DBusString *buffer, int count)
Like _dbus_read(), but only works on sockets so is available on Windows.
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:2038
dbus_bool_t _dbus_create_directory(const DBusString *filename, DBusError *error)
Creates a directory; succeeds if the directory is created or already existed.
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init().
Definition: dbus-string.c:242
#define TRUE
Expands to "1".
const char * _dbus_get_tmpdir(void)
Gets the temporary files directory by inspecting the environment variables TMPDIR, TMP, and TEMP in that order.
const char * _dbus_strerror_from_errno(void)
Get error message from errno.
Definition: dbus-sysdeps.c:783
const char * _dbus_error_from_system_errno(void)
Converts the current system errno value into a DBusError name.
Definition: dbus-sysdeps.c:706
int _dbus_write_socket(int fd, const DBusString *buffer, int start, int len)
Like _dbus_write(), but only supports sockets and is thus available on Windows.
dbus_bool_t _dbus_get_is_errno_eintr(void)
See if errno is EINTR.
Definition: dbus-sysdeps.c:749
#define DBUS_ERROR_ACCESS_DENIED
Security restrictions don't allow doing what you'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:1119
dbus_bool_t _dbus_get_is_errno_eagain_or_ewouldblock(void)
See if errno is EAGAIN or EWOULDBLOCK (this has to be done differently for Winsock so is abstracted) ...
#define DBUS_ERROR_NO_MEMORY
There was not enough memory to complete an operation.
#define FALSE
Expands to "0".
dbus_bool_t _dbus_close_socket(int fd, DBusError *error)
Closes a socket.
void _dbus_sleep_milliseconds(int milliseconds)
Sleeps the given number of milliseconds.
dbus_bool_t _dbus_delete_file(const DBusString *filename, DBusError *error)
Deletes the given file.
dbus_bool_t _dbus_generate_random_bytes(DBusString *str, int n_bytes)
Generates the given number of random bytes, using the best mechanism we can come up with...