D-Bus: dbus-nonce.c Source File

D-Bus 1.10.18

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) ) {
48 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
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))
64 _dbus_sleep_milliseconds (100);
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 {
83 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
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
110 dbus_bool_t
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,
128 _dbus_error_from_system_errno (),
129 "Failed to open %s for read: %s",
130 _dbus_string_get_const_data (fname),
131 _dbus_strerror_from_errno ());
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 {
145 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
146 return FALSE;
147 }
148 return TRUE;
149 }
150
151 DBusSocket
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");
168 _dbus_close_socket(fd, NULL);
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 {
185 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
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
211 dbus_bool_t
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 {
227 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
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,
247 _dbus_error_from_system_errno (),
248 "Failed to send nonce (fd=%d): %s",
249 fd, _dbus_strerror_from_errno ());
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 {
270 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
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 {
285 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
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 {
293 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
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 {
300 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
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 {
318 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
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
353 dbus_bool_t
354 _dbus_noncefile_create (DBusNonceFile *noncefile,
355 DBusError *error)
356 {
357 return do_noncefile_create (noncefile, error, /*use_subdir=*/FALSE);
358 }
359
367 dbus_bool_t
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
388 dbus_bool_t
389 _dbus_noncefile_create (DBusNonceFile *noncefile,
390 DBusError *error)
391 {
392 return do_noncefile_create (noncefile, error, /*use_subdir=*/TRUE);
393 }
394
402 dbus_bool_t
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
439 dbus_bool_t
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_string_append
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
DBUS_ERROR_FILE_NOT_FOUND
#define DBUS_ERROR_FILE_NOT_FOUND
Missing file.
Definition: dbus-protocol.h:393
NULL
#define NULL
A null pointer, defined appropriately for C or C++.
_dbus_string_save_to_file
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.
Definition: dbus-file-unix.c:164
_dbus_assert
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
Definition: dbus-internals.h:148
_dbus_read_socket
int _dbus_read_socket(DBusSocket fd, DBusString *buffer, int count)
Like _dbus_read(), but only works on sockets so is available on Windows.
Definition: dbus-sysdeps-unix.c:299
_dbus_string_init
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
Definition: dbus-string.c:175
DBUS_ERROR_IO_ERROR
#define DBUS_ERROR_IO_ERROR
Something went wrong reading or writing to a socket, for example.
Definition: dbus-protocol.h:365
_dbus_string_copy
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_close_socket
dbus_bool_t _dbus_close_socket(DBusSocket fd, DBusError *error)
Closes a socket.
Definition: dbus-sysdeps-unix.c:283
_dbus_delete_file
dbus_bool_t _dbus_delete_file(const DBusString *filename, DBusError *error)
Deletes the given file.
Definition: dbus-file-unix.c:399
DBusSocket
Socket interface.
Definition: dbus-sysdeps.h:148
DBusNonceFile
Definition: dbus-nonce.h:36
DBusString
Definition: dbus-string.h:42
_dbus_get_is_errno_eagain_or_ewouldblock
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) ...
Definition: dbus-sysdeps-unix.c:4267
dbus_bool_t
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition: dbus-types.h:35
_dbus_generate_random_ascii
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
_dbus_accept
DBusSocket _dbus_accept(DBusSocket listen_fd)
Accepts a connection on a listening socket.
Definition: dbus-sysdeps-unix.c:2220
_dbus_create_directory
dbus_bool_t _dbus_create_directory(const DBusString *filename, DBusError *error)
Creates a directory.
Definition: dbus-sysdeps-unix.c:2988
_dbus_delete_directory
dbus_bool_t _dbus_delete_directory(const DBusString *filename, DBusError *error)
Removes a directory; Directory must be empty.
Definition: dbus-sysdeps-unix.c:4280
_dbus_get_is_errno_eintr
dbus_bool_t _dbus_get_is_errno_eintr(int e)
See if errno is EINTR.
Definition: dbus-sysdeps.c:715
DBusError
Object representing an exception.
Definition: dbus-errors.h:48
dbus_set_error
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_string_equal_len
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
_dbus_get_tmpdir
const char * _dbus_get_tmpdir(void)
Gets the temporary files directory by inspecting the environment variables TMPDIR, TMP, and TEMP in that order.
Definition: dbus-sysdeps-unix.c:3515
_dbus_string_free
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init().
Definition: dbus-string.c:259
TRUE
#define TRUE
Expands to "1".
_dbus_strerror_from_errno
const char * _dbus_strerror_from_errno(void)
Get error message from errno.
Definition: dbus-sysdeps.c:749
_dbus_error_from_system_errno
const char * _dbus_error_from_system_errno(void)
Converts the current system errno value into a DBusError name.
Definition: dbus-sysdeps.c:682
DBUS_ERROR_ACCESS_DENIED
#define DBUS_ERROR_ACCESS_DENIED
Security restrictions don&#39;t allow doing what you&#39;re trying to do.
Definition: dbus-protocol.h:373
_dbus_string_append_len
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_generate_random_bytes
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...
Definition: dbus-sysdeps-unix.c:3094
DBUS_ERROR_NO_MEMORY
#define DBUS_ERROR_NO_MEMORY
There was not enough memory to complete an operation.
Definition: dbus-protocol.h:357
FALSE
#define FALSE
Expands to "0".
_dbus_write_socket
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.
Definition: dbus-sysdeps-unix.c:317
_dbus_sleep_milliseconds
void _dbus_sleep_milliseconds(int milliseconds)
Sleeps the given number of milliseconds.
Definition: dbus-sysdeps-unix.c:3064