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
24 // major sections of this file are modified code from libassuan, (C) FSF
25 #include "dbus-nonce.h"
26 #include "dbus-internals.h"
27 #include "dbus-protocol.h"
28 #include "dbus-sysdeps.h"
37 # define ENOFILE ENOENT
41 _dbus_check_nonce (int fd, const DBusString *nonce)
51 _dbus_string_init (&buffer);
52 _dbus_string_init (&p);
53 //PENDING(kdab) replace errno by DBusError
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_string_free (&p);
64 _dbus_string_free (&buffer);
69 _dbus_string_free (&p);
70 _dbus_string_free (&buffer);
76 _dbus_string_append_len(&buffer, _dbus_string_get_const_data (&p), n);
81 result = _dbus_string_equal_len (&buffer, nonce, 16);
85 _dbus_string_free (&p);
86 _dbus_string_free (&buffer);
91 //PENDING(kdab) document
93 _dbus_read_nonce (const DBusString *fname, DBusString *nonce)
95 //PENDING(kdab) replace errno by DBusError
98 buffer[sizeof buffer - 1] = '\0';
100 _dbus_verbose ("reading nonce from file: %s\n", _dbus_string_get_const_data (fname));
103 fp = fopen (_dbus_string_get_const_data (fname), "rb");
106 nread = fread (buffer, 1, sizeof buffer - 1, fp);
114 if (!_dbus_string_append_len (nonce, buffer, sizeof buffer - 1 ))
123 _dbus_accept_with_nonce (int listen_fd, const DBusString *nonce)
125 _dbus_assert (nonce != NULL);
127 fd = _dbus_accept (listen_fd);
128 if (_dbus_socket_is_invalid (fd))
130 if (_dbus_check_nonce(fd, nonce) != TRUE) {
131 _dbus_verbose ("nonce check failed. Closing socket.\n");
132 _dbus_close_socket(fd, NULL);
140 _dbus_accept_with_noncefile (int listen_fd, const DBusString *noncefile)
142 _dbus_assert (noncefile != NULL);
144 _dbus_string_init (&nonce);
145 //PENDING(kdab): set better errors
146 if (_dbus_read_nonce (noncefile, &nonce) != TRUE)
148 return _dbus_accept_with_nonce (listen_fd, &nonce);
152 _dbus_generate_noncefilename (DBusString *buf, DBusError *error)
155 DBusString randomStr;
157 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
159 ret = _dbus_string_init (&randomStr);
162 ret = _dbus_generate_random_ascii (&randomStr, 8);
165 if (!_dbus_string_append (buf, _dbus_get_tmpdir())
166 || !_dbus_string_append (buf, DBUS_DIR_SEPARATOR "dbus_nonce-")
167 || !_dbus_string_append (buf, _dbus_string_get_const_data (&randomStr)) )
170 _dbus_string_free (&randomStr);
173 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
174 _dbus_string_free (&randomStr);
179 _dbus_generate_and_write_nonce (const DBusString *filename, DBusError *error)
184 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
186 _dbus_string_init (&nonce);
188 if (!_dbus_generate_random_bytes (&nonce, 16))
190 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
191 _dbus_string_free (&nonce);
195 ret = _dbus_string_save_to_file (filename, &nonce, error);
197 _dbus_string_free (&nonce);
203 _dbus_send_nonce(int fd, const DBusString *noncefile, DBusError *error)
205 dbus_bool_t read_result;
210 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
212 if (_dbus_string_get_length (noncefile) == 0)
215 if ( !_dbus_string_init (&nonce) )
217 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
221 read_result = _dbus_read_nonce (noncefile, &nonce);
225 dbus_set_error (error,
226 _dbus_error_from_errno (errno),
227 "Could not read nonce from file %s (%s)",
228 _dbus_string_get_const_data (noncefile), _dbus_strerror(errno));
229 _dbus_string_free (&nonce);
233 send_result = _dbus_write_socket (fd, &nonce, 0, _dbus_string_get_length (&nonce));
235 _dbus_string_free (&nonce);
237 if (send_result == -1)
239 dbus_set_error (error,
240 _dbus_error_from_errno (errno),
241 "Failed to send nonce (fd=%d): %s",
242 fd, _dbus_strerror(errno));
249 /** @} end of nonce */