D-Bus: dbus-nonce.c Source File

D-Bus 1.8.16

- Main Page
- Related Pages
- Modules
- Data Structures
- Files

- File List

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) ) {
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 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())
60 _dbus_sleep_milliseconds (100);
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 _dbus_string_append_len(&buffer, _dbus_string_get_const_data (&p), n);
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
100 dbus_bool_t
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 {
117 dbus_set_error (error,
118 _dbus_error_from_system_errno (),
119 "Failed to open %s for read: %s",
120 _dbus_string_get_const_data (fname),
121 _dbus_strerror_from_errno ());
122 return FALSE;
123 }
124
125 nread = fread (buffer, 1, sizeof buffer - 1, fp);
126 fclose (fp);
127 if (!nread)
128 {
129 dbus_set_error (error, DBUS_ERROR_FILE_NOT_FOUND, "Could not read nonce from file %s", _dbus_string_get_const_data (fname));
130 return FALSE;
131 }
132
133 if (!_dbus_string_append_len (nonce, buffer, sizeof buffer - 1 ))
134 {
135 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
136 return FALSE;
137 }
138 return TRUE;
139 }
140
141 int
142 _dbus_accept_with_noncefile (int listen_fd, const DBusNonceFile *noncefile)
143 {
144 int fd;
145 DBusString nonce;
146
147 _dbus_assert (noncefile != NULL);
148 if (!_dbus_string_init (&nonce))
149 return -1;
150 //PENDING(kdab): set better errors
151 if (_dbus_read_nonce (_dbus_noncefile_get_path(noncefile), &nonce, NULL) != TRUE)
152 return -1;
153 fd = _dbus_accept (listen_fd);
154 if (_dbus_socket_is_invalid (fd))
155 return fd;
156 if (do_check_nonce(fd, &nonce, NULL) != TRUE) {
157 _dbus_verbose ("nonce check failed. Closing socket.\n");
158 _dbus_close_socket(fd, NULL);
159 return -1;
160 }
161
162 return fd;
163 }
164
165 static dbus_bool_t
166 generate_and_write_nonce (const DBusString *filename, DBusError *error)
167 {
168 DBusString nonce;
169 dbus_bool_t ret;
170
171 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
172
173 if (!_dbus_string_init (&nonce))
174 {
175 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
176 return FALSE;
177 }
178
179 if (!_dbus_generate_random_bytes (&nonce, 16))
180 {
181 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
182 _dbus_string_free (&nonce);
183 return FALSE;
184 }
185
186 ret = _dbus_string_save_to_file (&nonce, filename, FALSE, error);
187
188 _dbus_string_free (&nonce);
189
190 return ret;
191 }
192
202 dbus_bool_t
203 _dbus_send_nonce (int fd, const DBusString *noncefile, DBusError *error)
204 {
205 dbus_bool_t read_result;
206 int send_result;
207 DBusString nonce;
208
209 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
210
211 if (_dbus_string_get_length (noncefile) == 0)
212 return FALSE;
213
214 if (!_dbus_string_init (&nonce))
215 {
216 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
217 return FALSE;
218 }
219
220 read_result = _dbus_read_nonce (noncefile, &nonce, error);
221 if (!read_result)
222 {
223 _DBUS_ASSERT_ERROR_IS_SET (error);
224 _dbus_string_free (&nonce);
225 return FALSE;
226 }
227 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
228
229 send_result = _dbus_write_socket (fd, &nonce, 0, _dbus_string_get_length (&nonce));
230
231 _dbus_string_free (&nonce);
232
233 if (send_result == -1)
234 {
235 dbus_set_error (error,
236 _dbus_error_from_system_errno (),
237 "Failed to send nonce (fd=%d): %s",
238 fd, _dbus_strerror_from_errno ());
239 return FALSE;
240 }
241
242 return TRUE;
243 }
244
245 static dbus_bool_t
246 do_noncefile_create (DBusNonceFile *noncefile,
247 DBusError *error,
248 dbus_bool_t use_subdir)
249 {
250 DBusString randomStr;
251 const char *tmp;
252
253 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
254
255 _dbus_assert (noncefile);
256
257 if (!_dbus_string_init (&randomStr))
258 {
259 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
260 goto on_error;
261 }
262
263 if (!_dbus_generate_random_ascii (&randomStr, 8))
264 {
265 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
266 goto on_error;
267 }
268
269 tmp = _dbus_get_tmpdir ();
270
271 if (!_dbus_string_init (&noncefile->dir)
272 || tmp == NULL
273 || !_dbus_string_append (&noncefile->dir, tmp))
274 {
275 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
276 goto on_error;
277 }
278 if (use_subdir)
279 {
280 if (!_dbus_string_append (&noncefile->dir, "/dbus_nonce-")
281 || !_dbus_string_append (&noncefile->dir, _dbus_string_get_const_data (&randomStr)) )
282 {
283 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
284 goto on_error;
285 }
286 if (!_dbus_string_init (&noncefile->path)
287 || !_dbus_string_copy (&noncefile->dir, 0, &noncefile->path, 0)
288 || !_dbus_string_append (&noncefile->path, "/nonce"))
289 {
290 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
291 goto on_error;
292 }
293 if (!_dbus_create_directory (&noncefile->dir, error))
294 {
295 _DBUS_ASSERT_ERROR_IS_SET (error);
296 goto on_error;
297 }
298 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
299
300 }
301 else
302 {
303 if (!_dbus_string_init (&noncefile->path)
304 || !_dbus_string_copy (&noncefile->dir, 0, &noncefile->path, 0)
305 || !_dbus_string_append (&noncefile->path, "/dbus_nonce-")
306 || !_dbus_string_append (&noncefile->path, _dbus_string_get_const_data (&randomStr)))
307 {
308 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
309 goto on_error;
310 }
311
312 }
313
314 if (!generate_and_write_nonce (&noncefile->path, error))
315 {
316 _DBUS_ASSERT_ERROR_IS_SET (error);
317 if (use_subdir)
318 _dbus_delete_directory (&noncefile->dir, NULL); //we ignore possible errors deleting the dir and return the write error instead
319 goto on_error;
320 }
321 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
322
323 _dbus_string_free (&randomStr);
324
325 return TRUE;
326 on_error:
327 if (use_subdir)
328 _dbus_delete_directory (&noncefile->dir, NULL);
329 _dbus_string_free (&noncefile->dir);
330 _dbus_string_free (&noncefile->path);
331 _dbus_string_free (&randomStr);
332 return FALSE;
333 }
334
335 #ifdef DBUS_WIN
336
343 dbus_bool_t
344 _dbus_noncefile_create (DBusNonceFile *noncefile,
345 DBusError *error)
346 {
347 return do_noncefile_create (noncefile, error, /*use_subdir=*/FALSE);
348 }
349
357 dbus_bool_t
358 _dbus_noncefile_delete (DBusNonceFile *noncefile,
359 DBusError *error)
360 {
361 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
362
363 _dbus_delete_file (&noncefile->path, error);
364 _dbus_string_free (&noncefile->dir);
365 _dbus_string_free (&noncefile->path);
366 return TRUE;
367 }
368
369 #else
370
378 dbus_bool_t
379 _dbus_noncefile_create (DBusNonceFile *noncefile,
380 DBusError *error)
381 {
382 return do_noncefile_create (noncefile, error, /*use_subdir=*/TRUE);
383 }
384
392 dbus_bool_t
393 _dbus_noncefile_delete (DBusNonceFile *noncefile,
394 DBusError *error)
395 {
396 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
397
398 _dbus_delete_directory (&noncefile->dir, error);
399 _dbus_string_free (&noncefile->dir);
400 _dbus_string_free (&noncefile->path);
401 return TRUE;
402 }
403 #endif
404
405
412 const DBusString*
413 _dbus_noncefile_get_path (const DBusNonceFile *noncefile)
414 {
415 _dbus_assert (noncefile);
416 return &noncefile->path;
417 }
418
429 dbus_bool_t
430 _dbus_noncefile_check_nonce (int fd,
431 const DBusNonceFile *noncefile,
432 DBusError* error)
433 {
434 return do_check_nonce (fd, _dbus_noncefile_get_path (noncefile), error);
435 }
436
437
_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:918
DBUS_ERROR_FILE_NOT_FOUND
#define DBUS_ERROR_FILE_NOT_FOUND
Missing file.
Definition: dbus-protocol.h:388
NULL
#define NULL
A null pointer, defined appropriately for C or C++.
_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:3926
_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:135
_dbus_accept
int _dbus_accept(int listen_fd)
Accepts a connection on a listening socket.
Definition: dbus-sysdeps-unix.c:1967
_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:360
_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's copied to the d...
Definition: dbus-string.c:1265
DBusNonceFile
Definition: dbus-nonce.h:35
_dbus_generate_random_ascii
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
DBusString
Definition: dbus-string.h:42
dbus_bool_t
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition: dbus-types.h:35
_dbus_read_socket
int _dbus_read_socket(int fd, DBusString *buffer, int count)
Like _dbus_read(), but only works on sockets so is available on Windows.
Definition: dbus-sysdeps-unix.c:217
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:2038
_dbus_create_directory
dbus_bool_t _dbus_create_directory(const DBusString *filename, DBusError *error)
Creates a directory; succeeds if the directory is created or already existed.
Definition: dbus-sysdeps-unix.c:2724
_dbus_string_free
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init().
Definition: dbus-string.c:242
TRUE
#define TRUE
Expands to "1".
_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:3253
_dbus_strerror_from_errno
const char * _dbus_strerror_from_errno(void)
Get error message from errno.
Definition: dbus-sysdeps.c:783
_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:706
_dbus_write_socket
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.
Definition: dbus-sysdeps-unix.c:235
_dbus_get_is_errno_eintr
dbus_bool_t _dbus_get_is_errno_eintr(void)
See if errno is EINTR.
Definition: dbus-sysdeps.c:749
DBUS_ERROR_ACCESS_DENIED
#define DBUS_ERROR_ACCESS_DENIED
Security restrictions don't allow doing what you're trying to do.
Definition: dbus-protocol.h:368
_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:1119
_dbus_get_is_errno_eagain_or_ewouldblock
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) ...
Definition: dbus-sysdeps-unix.c:3913
DBUS_ERROR_NO_MEMORY
#define DBUS_ERROR_NO_MEMORY
There was not enough memory to complete an operation.
Definition: dbus-protocol.h:352
FALSE
#define FALSE
Expands to "0".
_dbus_close_socket
dbus_bool_t _dbus_close_socket(int fd, DBusError *error)
Closes a socket.
Definition: dbus-sysdeps-unix.c:201
_dbus_sleep_milliseconds
void _dbus_sleep_milliseconds(int milliseconds)
Sleeps the given number of milliseconds.
Definition: dbus-sysdeps-unix.c:2803
_dbus_delete_file
dbus_bool_t _dbus_delete_file(const DBusString *filename, DBusError *error)
Deletes the given file.
Definition: dbus-file-unix.c:399
_dbus_generate_random_bytes
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...
Definition: dbus-sysdeps-unix.c:2851