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)
4
* Copyright (C) 2009 Klaralvdalens Datakonsult AB, a KDAB Group company, info@kdab.net
6
* Licensed under the Academic Free License version 2.1
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.
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.
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
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"
34
do_check_nonce (int fd, const DBusString *nonce, DBusError *error)
42
_DBUS_ASSERT_ERROR_IS_CLEAR (error);
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);
56
n = _dbus_read_socket (fd, &p, nleft);
57
if (n == -1 && _dbus_get_is_errno_eintr())
59
else if (n == -1 && _dbus_get_is_errno_eagain_or_ewouldblock())
60
_dbus_sleep_milliseconds (100);
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);
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 );
77
_dbus_string_append_len(&buffer, _dbus_string_get_const_data (&p), n);
82
result = _dbus_string_equal_len (&buffer, nonce, 16);
84
dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED, "Nonces do not match, access denied (fd=%d)", fd );
86
_dbus_string_free (&p);
87
_dbus_string_free (&buffer);
93
* reads the nonce from the nonce file and stores it in a string
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)
101
_dbus_read_nonce (const DBusString *fname, DBusString *nonce, DBusError* error)
107
buffer[sizeof buffer - 1] = '\0';
109
_DBUS_ASSERT_ERROR_IS_CLEAR (error);
111
_dbus_verbose ("reading nonce from file: %s\n", _dbus_string_get_const_data (fname));
114
fp = fopen (_dbus_string_get_const_data (fname), "rb");
117
nread = fread (buffer, 1, sizeof buffer - 1, fp);
121
dbus_set_error (error, DBUS_ERROR_FILE_NOT_FOUND, "Could not read nonce from file %s", _dbus_string_get_const_data (fname));
125
if (!_dbus_string_append_len (nonce, buffer, sizeof buffer - 1 ))
127
dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
134
_dbus_accept_with_noncefile (int listen_fd, const DBusNonceFile *noncefile)
139
_dbus_assert (noncefile != NULL);
140
_dbus_string_init (&nonce);
141
//PENDING(kdab): set better errors
142
if (_dbus_read_nonce (_dbus_noncefile_get_path(noncefile), &nonce, NULL) != TRUE)
144
fd = _dbus_accept (listen_fd);
145
if (_dbus_socket_is_invalid (fd))
147
if (do_check_nonce(fd, &nonce, NULL) != TRUE) {
148
_dbus_verbose ("nonce check failed. Closing socket.\n");
149
_dbus_close_socket(fd, NULL);
157
generate_and_write_nonce (const DBusString *filename, DBusError *error)
162
_DBUS_ASSERT_ERROR_IS_CLEAR (error);
164
_dbus_string_init (&nonce);
166
if (!_dbus_generate_random_bytes (&nonce, 16))
168
dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
169
_dbus_string_free (&nonce);
173
ret = _dbus_string_save_to_file (&nonce, filename, FALSE, error);
175
_dbus_string_free (&nonce);
181
* sends the nonce over a given socket. Blocks while doing so.
183
* @param fd the file descriptor to write the nonce data to (usually a socket)
184
* @param noncefile the noncefile location to read the nonce from
185
* @param error contains error details if FALSE is returned
186
* @return TRUE iff the nonce was successfully sent. Note that this does not
187
* indicate whether the server accepted the nonce.
190
_dbus_send_nonce (int fd, const DBusString *noncefile, DBusError *error)
192
dbus_bool_t read_result;
197
_DBUS_ASSERT_ERROR_IS_CLEAR (error);
199
if (_dbus_string_get_length (noncefile) == 0)
202
if (!_dbus_string_init (&nonce))
204
dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
208
read_result = _dbus_read_nonce (noncefile, &nonce, error);
211
_DBUS_ASSERT_ERROR_IS_SET (error);
212
_dbus_string_free (&nonce);
215
_DBUS_ASSERT_ERROR_IS_CLEAR (error);
217
send_result = _dbus_write_socket (fd, &nonce, 0, _dbus_string_get_length (&nonce));
219
_dbus_string_free (&nonce);
221
if (send_result == -1)
223
dbus_set_error (error,
224
_dbus_error_from_system_errno (),
225
"Failed to send nonce (fd=%d): %s",
226
fd, _dbus_strerror_from_errno ());
234
do_noncefile_create (DBusNonceFile *noncefile,
236
dbus_bool_t use_subdir)
239
DBusString randomStr;
241
_DBUS_ASSERT_ERROR_IS_CLEAR (error);
243
_dbus_assert (noncefile);
245
if (!_dbus_string_init (&randomStr))
247
dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
251
if (!_dbus_generate_random_ascii (&randomStr, 8))
253
dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
257
if (!_dbus_string_init (&noncefile->dir)
258
|| !_dbus_string_append (&noncefile->dir, _dbus_get_tmpdir()))
260
dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
265
if (!_dbus_string_append (&noncefile->dir, "/dbus_nonce-")
266
|| !_dbus_string_append (&noncefile->dir, _dbus_string_get_const_data (&randomStr)) )
268
dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
271
if (!_dbus_string_init (&noncefile->path)
272
|| !_dbus_string_copy (&noncefile->dir, 0, &noncefile->path, 0)
273
|| !_dbus_string_append (&noncefile->dir, "/nonce"))
275
dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
278
if (!_dbus_create_directory (&noncefile->dir, error))
280
_DBUS_ASSERT_ERROR_IS_SET (error);
283
_DBUS_ASSERT_ERROR_IS_CLEAR (error);
288
if (!_dbus_string_init (&noncefile->path)
289
|| !_dbus_string_copy (&noncefile->dir, 0, &noncefile->path, 0)
290
|| !_dbus_string_append (&noncefile->path, "/dbus_nonce-")
291
|| !_dbus_string_append (&noncefile->path, _dbus_string_get_const_data (&randomStr)))
293
dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
299
if (!generate_and_write_nonce (&noncefile->path, error))
301
_DBUS_ASSERT_ERROR_IS_SET (error);
303
_dbus_delete_directory (&noncefile->dir, NULL); //we ignore possible errors deleting the dir and return the write error instead
306
_DBUS_ASSERT_ERROR_IS_CLEAR (error);
308
_dbus_string_free (&randomStr);
313
_dbus_delete_directory (&noncefile->dir, NULL);
314
_dbus_string_free (&noncefile->dir);
315
_dbus_string_free (&noncefile->path);
316
_dbus_string_free (&randomStr);
322
* creates a nonce file in a user-readable location and writes a generated nonce to it
324
* @param noncefile returns the nonce file location
325
* @param error error details if creating the nonce file fails
326
* @return TRUE iff the nonce file was successfully created
329
_dbus_noncefile_create (DBusNonceFile *noncefile,
332
return do_noncefile_create (noncefile, error, /*use_subdir=*/FALSE);
336
* deletes the noncefile and frees the DBusNonceFile object.
338
* @param noncefile the nonce file to delete. Contents will be freed.
339
* @param error error details if the nonce file could not be deleted
343
_dbus_noncefile_delete (DBusNonceFile *noncefile,
346
_DBUS_ASSERT_ERROR_IS_CLEAR (error);
348
_dbus_delete_file (&noncefile->path, error);
349
_dbus_string_free (&noncefile->dir);
350
_dbus_string_free (&noncefile->path);
356
* creates a nonce file in a user-readable location and writes a generated nonce to it.
357
* Initializes the noncefile object.
359
* @param noncefile returns the nonce file location
360
* @param error error details if creating the nonce file fails
361
* @return TRUE iff the nonce file was successfully created
364
_dbus_noncefile_create (DBusNonceFile *noncefile,
367
return do_noncefile_create (noncefile, error, /*use_subdir=*/TRUE);
371
* deletes the noncefile and frees the DBusNonceFile object.
373
* @param noncefile the nonce file to delete. Contents will be freed.
374
* @param error error details if the nonce file could not be deleted
378
_dbus_noncefile_delete (DBusNonceFile *noncefile,
381
_DBUS_ASSERT_ERROR_IS_CLEAR (error);
383
_dbus_delete_directory (&noncefile->dir, error);
384
_dbus_string_free (&noncefile->dir);
385
_dbus_string_free (&noncefile->path);
392
* returns the absolute file path of the nonce file
394
* @param noncefile an initialized noncefile object
395
* @return the absolute path of the nonce file
398
_dbus_noncefile_get_path (const DBusNonceFile *noncefile)
400
_dbus_assert (noncefile);
401
return &noncefile->path;
405
* reads data from a file descriptor and checks if the received data matches
406
* the data in the given noncefile.
408
* @param fd the file descriptor to read the nonce from
409
* @param noncefile the nonce file to check the received data against
410
* @param error error details on fail
411
* @return TRUE iff a nonce could be successfully read from the file descriptor
412
* and matches the nonce from the given nonce file
415
_dbus_noncefile_check_nonce (int fd,
416
const DBusNonceFile *noncefile,
419
return do_check_nonce (fd, _dbus_noncefile_get_path (noncefile), error);
423
/** @} end of nonce */