Consistently include <config.h> in all C source files and never in header files.
[platform/upstream/dbus.git] / dbus / 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 #ifdef HAVE_ERRNO_H
34 # include <errno.h>
35 #endif
36
37 static dbus_bool_t
38 do_check_nonce (int fd, const DBusString *nonce, DBusError *error)
39 {
40   DBusString buffer;
41   DBusString p;
42   size_t nleft;
43   dbus_bool_t result;
44   int n;
45
46   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
47
48   nleft = 16;
49
50   if (   !_dbus_string_init (&buffer)
51       || !_dbus_string_init (&p) ) {
52         dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
53         _dbus_string_free (&p);
54         _dbus_string_free (&buffer);
55         return FALSE;
56       }
57
58   while (nleft)
59     {
60       n = _dbus_read_socket (fd, &p, nleft);
61       if (n == -1 && _dbus_get_is_errno_eintr())
62         ;
63       else if (n == -1 && _dbus_get_is_errno_eagain_or_ewouldblock())
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           _dbus_string_append_len(&buffer, _dbus_string_get_const_data (&p), n);
82           nleft -= n;
83         }
84     }
85
86   result =  _dbus_string_equal_len (&buffer, nonce, 16);
87   if (!result)
88     dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED, "Nonces do not match, access denied (fd=%d)", fd );
89
90   _dbus_string_free (&p);
91   _dbus_string_free (&buffer);
92
93   return result;
94 }
95
96 /**
97  * reads the nonce from the nonce file and stores it in a string
98  *
99  * @param fname the file to read the nonce from
100  * @param nonce returns the nonce. Must be an initialized string, the nonce will be appended.
101  * @param error error object to report possible errors
102  * @return FALSE iff reading the nonce fails (error is set then)
103  */
104 dbus_bool_t
105 _dbus_read_nonce (const DBusString *fname, DBusString *nonce, DBusError* error)
106 {
107   FILE *fp;
108   char buffer[17];
109   size_t nread;
110
111   buffer[sizeof buffer - 1] = '\0';
112
113   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
114
115   _dbus_verbose ("reading nonce from file: %s\n", _dbus_string_get_const_data (fname));
116
117
118   fp = fopen (_dbus_string_get_const_data (fname), "rb");
119   if (!fp)
120     return FALSE;
121   nread = fread (buffer, 1, sizeof buffer - 1, fp);
122   fclose (fp);
123   if (!nread)
124     {
125       dbus_set_error (error, DBUS_ERROR_FILE_NOT_FOUND, "Could not read nonce from file %s", _dbus_string_get_const_data (fname));
126       return FALSE;
127     }
128
129   if (!_dbus_string_append_len (nonce, buffer, sizeof buffer - 1 ))
130     {
131       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
132       return FALSE;
133     }
134   return TRUE;
135 }
136
137 int
138 _dbus_accept_with_noncefile (int listen_fd, const DBusNonceFile *noncefile)
139 {
140   int fd;
141   DBusString nonce;
142
143   _dbus_assert (noncefile != NULL);
144   _dbus_string_init (&nonce);
145   //PENDING(kdab): set better errors
146   if (_dbus_read_nonce (_dbus_noncefile_get_path(noncefile), &nonce, NULL) != TRUE)
147     return -1;
148   fd = _dbus_accept (listen_fd);
149   if (_dbus_socket_is_invalid (fd))
150     return fd;
151   if (do_check_nonce(fd, &nonce, NULL) != TRUE) {
152     _dbus_verbose ("nonce check failed. Closing socket.\n");
153     _dbus_close_socket(fd, NULL);
154     return -1;
155   }
156
157   return fd;
158 }
159
160 static dbus_bool_t
161 generate_and_write_nonce (const DBusString *filename, DBusError *error)
162 {
163   DBusString nonce;
164   dbus_bool_t ret;
165
166   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
167
168   _dbus_string_init (&nonce);
169
170   if (!_dbus_generate_random_bytes (&nonce, 16))
171     {
172       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
173       _dbus_string_free (&nonce);
174       return FALSE;
175     }
176
177   ret = _dbus_string_save_to_file (&nonce, filename, error);
178
179   _dbus_string_free (&nonce);
180
181   return ret;
182 }
183
184 /**
185  * sends the nonce over a given socket. Blocks while doing so.
186  *
187  * @param fd the file descriptor to write the nonce data to (usually a socket)
188  * @param noncefile the noncefile location to read the nonce from
189  * @param error contains error details if FALSE is returned
190  * @return TRUE iff the nonce was successfully sent. Note that this does not
191  * indicate whether the server accepted the nonce.
192  */
193 dbus_bool_t
194 _dbus_send_nonce(int fd, const DBusString *noncefile, DBusError *error)
195 {
196   dbus_bool_t read_result;
197   int send_result;
198   size_t sendLen;
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     {
208       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
209       return FALSE;
210   }
211
212   read_result = _dbus_read_nonce (noncefile, &nonce, NULL);
213
214   if (!read_result)
215     {
216       dbus_set_error (error,
217                       _dbus_error_from_errno (errno),
218                       "Could not read nonce from file %s (%s)",
219                       _dbus_string_get_const_data (noncefile), _dbus_strerror(errno));
220       _dbus_string_free (&nonce);
221       return FALSE;
222     }
223
224   send_result = _dbus_write_socket (fd, &nonce, 0, _dbus_string_get_length (&nonce));
225
226   _dbus_string_free (&nonce);
227
228   if (send_result == -1)
229   {
230     dbus_set_error (error,
231                     _dbus_error_from_errno (errno),
232                     "Failed to send nonce (fd=%d): %s",
233                     fd, _dbus_strerror(errno));
234     return FALSE;
235   }
236
237   return TRUE;
238 }
239
240 static dbus_bool_t
241 do_noncefile_create (DBusNonceFile *noncefile,
242                      DBusError *error,
243                      dbus_bool_t use_subdir)
244 {
245     dbus_bool_t ret;
246     DBusString randomStr;
247
248     _DBUS_ASSERT_ERROR_IS_CLEAR (error);
249
250     _dbus_assert (noncefile);
251
252     if (!_dbus_string_init (&randomStr))
253       {
254         dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
255         goto on_error;
256       }
257
258     if (!_dbus_generate_random_ascii (&randomStr, 8))
259       {
260         dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
261         goto on_error;
262       }
263
264     if (!_dbus_string_init (&noncefile->dir)
265         || !_dbus_string_append (&noncefile->dir, _dbus_get_tmpdir()))
266       {
267         dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
268         goto on_error;
269       }
270     if (use_subdir)
271       {
272         if (!_dbus_string_append (&noncefile->dir, "/dbus_nonce-")
273             || !_dbus_string_append (&noncefile->dir, _dbus_string_get_const_data (&randomStr)) )
274           {
275             dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
276             goto on_error;
277           }
278         if (!_dbus_string_init (&noncefile->path)
279             || !_dbus_string_copy (&noncefile->dir, 0, &noncefile->path, 0)
280             || !_dbus_string_append (&noncefile->dir, "/nonce"))
281           {
282             dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
283             goto on_error;
284           }
285         if (!_dbus_create_directory (&noncefile->dir, error))
286           {
287             goto on_error;
288           }
289
290       }
291     else
292       {
293         if (!_dbus_string_init (&noncefile->path)
294             || !_dbus_string_copy (&noncefile->dir, 0, &noncefile->path, 0)
295             || !_dbus_string_append (&noncefile->path, "/dbus_nonce-")
296             || !_dbus_string_append (&noncefile->path, _dbus_string_get_const_data (&randomStr)))
297           {
298             dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
299             goto on_error;
300           }
301
302       }
303
304     if (!generate_and_write_nonce (&noncefile->path, error))
305       {
306         if (use_subdir)
307           _dbus_delete_directory (&noncefile->dir, NULL); //we ignore possible errors deleting the dir and return the write error instead
308         goto on_error;
309       }
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 /**
325  * creates a nonce file in a user-readable location and writes a generated nonce to it
326  *
327  * @param noncefile returns the nonce file location
328  * @param error error details if creating the nonce file fails
329  * @return TRUE iff the nonce file was successfully created
330  */
331 dbus_bool_t
332 _dbus_noncefile_create (DBusNonceFile *noncefile,
333                         DBusError *error)
334 {
335     return do_noncefile_create (noncefile, error, /*use_subdir=*/FALSE);
336 }
337
338 /**
339  * deletes the noncefile and frees the DBusNonceFile object.
340  *
341  * @param noncefile the nonce file to delete. Contents will be freed.
342  * @param error error details if the nonce file could not be deleted
343  * @return TRUE
344  */
345 dbus_bool_t
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 /**
359  * creates a nonce file in a user-readable location and writes a generated nonce to it.
360  * Initializes the noncefile object.
361  *
362  * @param noncefile returns the nonce file location
363  * @param error error details if creating the nonce file fails
364  * @return TRUE iff the nonce file was successfully created
365  */
366 dbus_bool_t
367 _dbus_noncefile_create (DBusNonceFile *noncefile,
368                         DBusError *error)
369 {
370     return do_noncefile_create (noncefile, error, /*use_subdir=*/TRUE);
371 }
372
373 /**
374  * deletes the noncefile and frees the DBusNonceFile object.
375  *
376  * @param noncefile the nonce file to delete. Contents will be freed.
377  * @param error error details if the nonce file could not be deleted
378  * @return TRUE
379  */
380 dbus_bool_t
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
394 /**
395  * returns the absolute file path of the nonce file
396  *
397  * @param noncefile an initialized noncefile object
398  * @return the absolute path of the nonce file
399  */
400 const DBusString*
401 _dbus_noncefile_get_path (const DBusNonceFile *noncefile)
402 {
403     _dbus_assert (noncefile);
404     return &noncefile->path;
405 }
406
407 /**
408  * reads data from a file descriptor and checks if the received data matches
409  * the data in the given noncefile.
410  *
411  * @param fd the file descriptor to read the nonce from
412  * @param noncefile the nonce file to check the received data against
413  * @param error error details on fail
414  * @return TRUE iff a nonce could be successfully read from the file descriptor
415  * and matches the nonce from the given nonce file
416  */
417 dbus_bool_t
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
426 /** @} end of nonce */