D-Bus  1.6.12
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  {
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 
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  {
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");
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  {
168  return FALSE;
169  }
170 
171  if (!_dbus_generate_random_bytes (&nonce, 16))
172  {
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 
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  {
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,
229  "Failed to send nonce (fd=%d): %s",
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 
244  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
245 
246  _dbus_assert (noncefile);
247 
248  if (!_dbus_string_init (&randomStr))
249  {
251  goto on_error;
252  }
253 
254  if (!_dbus_generate_random_ascii (&randomStr, 8))
255  {
257  goto on_error;
258  }
259 
260  if (!_dbus_string_init (&noncefile->dir)
261  || !_dbus_string_append (&noncefile->dir, _dbus_get_tmpdir()))
262  {
264  goto on_error;
265  }
266  if (use_subdir)
267  {
268  if (!_dbus_string_append (&noncefile->dir, "/dbus_nonce-")
269  || !_dbus_string_append (&noncefile->dir, _dbus_string_get_const_data (&randomStr)) )
270  {
272  goto on_error;
273  }
274  if (!_dbus_string_init (&noncefile->path)
275  || !_dbus_string_copy (&noncefile->dir, 0, &noncefile->path, 0)
276  || !_dbus_string_append (&noncefile->path, "/nonce"))
277  {
279  goto on_error;
280  }
281  if (!_dbus_create_directory (&noncefile->dir, error))
282  {
283  _DBUS_ASSERT_ERROR_IS_SET (error);
284  goto on_error;
285  }
286  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
287 
288  }
289  else
290  {
291  if (!_dbus_string_init (&noncefile->path)
292  || !_dbus_string_copy (&noncefile->dir, 0, &noncefile->path, 0)
293  || !_dbus_string_append (&noncefile->path, "/dbus_nonce-")
294  || !_dbus_string_append (&noncefile->path, _dbus_string_get_const_data (&randomStr)))
295  {
297  goto on_error;
298  }
299 
300  }
301 
302  if (!generate_and_write_nonce (&noncefile->path, error))
303  {
304  _DBUS_ASSERT_ERROR_IS_SET (error);
305  if (use_subdir)
306  _dbus_delete_directory (&noncefile->dir, NULL); //we ignore possible errors deleting the dir and return the write error instead
307  goto on_error;
308  }
309  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
310 
311  _dbus_string_free (&randomStr);
312 
313  return TRUE;
314  on_error:
315  if (use_subdir)
316  _dbus_delete_directory (&noncefile->dir, NULL);
317  _dbus_string_free (&noncefile->dir);
318  _dbus_string_free (&noncefile->path);
319  _dbus_string_free (&randomStr);
320  return FALSE;
321 }
322 
323 #ifdef DBUS_WIN
324 
332 _dbus_noncefile_create (DBusNonceFile *noncefile,
333  DBusError *error)
334 {
335  return do_noncefile_create (noncefile, error, /*use_subdir=*/FALSE);
336 }
337 
346 _dbus_noncefile_delete (DBusNonceFile *noncefile,
347  DBusError *error)
348 {
349  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
350 
351  _dbus_delete_file (&noncefile->path, error);
352  _dbus_string_free (&noncefile->dir);
353  _dbus_string_free (&noncefile->path);
354  return TRUE;
355 }
356 
357 #else
358 
367 _dbus_noncefile_create (DBusNonceFile *noncefile,
368  DBusError *error)
369 {
370  return do_noncefile_create (noncefile, error, /*use_subdir=*/TRUE);
371 }
372 
381 _dbus_noncefile_delete (DBusNonceFile *noncefile,
382  DBusError *error)
383 {
384  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
385 
386  _dbus_delete_directory (&noncefile->dir, error);
387  _dbus_string_free (&noncefile->dir);
388  _dbus_string_free (&noncefile->path);
389  return TRUE;
390 }
391 #endif
392 
393 
400 const DBusString*
401 _dbus_noncefile_get_path (const DBusNonceFile *noncefile)
402 {
403  _dbus_assert (noncefile);
404  return &noncefile->path;
405 }
406 
418 _dbus_noncefile_check_nonce (int fd,
419  const DBusNonceFile *noncefile,
420  DBusError* error)
421 {
422  return do_check_nonce (fd, _dbus_noncefile_get_path (noncefile), error);
423 }
424 
425 
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:913
#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&#39;s copied to the d...
Definition: dbus-string.c:1280
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:573
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition: dbus-types.h:35
int _dbus_string_get_length(const DBusString *str)
Gets the length of a string (not including nul termination).
Definition: dbus-string.c:717
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:2053
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 &quot;1&quot;.
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:767
const char * _dbus_error_from_system_errno(void)
Converts the current system errno value into a DBusError name.
Definition: dbus-sysdeps.c:704
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:747
#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:1134
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 &quot;0&quot;.
dbus_bool_t _dbus_close_socket(int fd, DBusError *error)
Closes a socket.
const char * _dbus_string_get_const_data(const DBusString *str)
Gets the raw character buffer from a const string.
Definition: dbus-string.c:446
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...