예제 #1
0
/**
 * reads the nonce from the nonce file and stores it in a string
 *
 * @param fname the file to read the nonce from
 * @param nonce returns the nonce. Must be an initialized string, the nonce will be appended.
 * @param error error object to report possible errors
 * @return FALSE iff reading the nonce fails (error is set then)
 */
dbus_bool_t
_dbus_read_nonce (const DBusString *fname, DBusString *nonce, DBusError* error)
{
  FILE *fp;
  char buffer[17];
  size_t nread;

  buffer[sizeof buffer - 1] = '\0';

  _DBUS_ASSERT_ERROR_IS_CLEAR (error);

  _dbus_verbose ("reading nonce from file: %s\n", _dbus_string_get_const_data (fname));


  fp = fopen (_dbus_string_get_const_data (fname), "rb");
  if (!fp)
    return FALSE;
  nread = fread (buffer, 1, sizeof buffer - 1, fp);
  fclose (fp);
  if (!nread)
    {
      dbus_set_error (error, DBUS_ERROR_FILE_NOT_FOUND, "Could not read nonce from file %s", _dbus_string_get_const_data (fname));
      return FALSE;
    }

  if (!_dbus_string_append_len (nonce, buffer, sizeof buffer - 1 ))
    {
      dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
      return FALSE;
    }
  return TRUE;
}
static dbus_bool_t
write_args_for_direction (DBusString *xml,
			  const char *signature,
			  dbus_bool_t in)
{
  DBusTypeReader typereader;
  DBusString sigstr;
  int current_type;
  
  _dbus_string_init_const (&sigstr, signature);
  _dbus_type_reader_init_types_only (&typereader, &sigstr, 0);
      
  while ((current_type = _dbus_type_reader_get_current_type (&typereader)) != DBUS_TYPE_INVALID)
    {
      const DBusString *subsig;
      int start, len;

      _dbus_type_reader_get_signature (&typereader, &subsig, &start, &len);
      if (!_dbus_string_append_printf (xml, "      <arg direction=\"%s\" type=\"",
				       in ? "in" : "out"))
	goto oom;
      if (!_dbus_string_append_len (xml,
				    _dbus_string_get_const_data (subsig) + start,
				    len))
	goto oom;
      if (!_dbus_string_append (xml, "\"/>\n"))
	goto oom;

      _dbus_type_reader_next (&typereader);
    }
  return TRUE;
 oom:
  return FALSE;
}
예제 #3
0
static dbus_bool_t
do_check_nonce (int fd, const DBusString *nonce, DBusError *error)
{
  DBusString buffer;
  DBusString p;
  size_t nleft;
  dbus_bool_t result;
  int n;

  _DBUS_ASSERT_ERROR_IS_CLEAR (error);

  nleft = 16;

  if (   !_dbus_string_init (&buffer)
      || !_dbus_string_init (&p) ) {
        dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
        _dbus_string_free (&p);
        _dbus_string_free (&buffer);
        return FALSE;
      }

  while (nleft)
    {
      n = _dbus_read_socket (fd, &p, nleft);
      if (n == -1 && _dbus_get_is_errno_eintr())
        ;
      else if (n == -1 && _dbus_get_is_errno_eagain_or_ewouldblock())
        _dbus_sleep_milliseconds (100);
      else if (n==-1)
        {
          dbus_set_error (error, DBUS_ERROR_IO_ERROR, "Could not read nonce from socket (fd=%d)", fd );
          _dbus_string_free (&p);
          _dbus_string_free (&buffer);
          return FALSE;
        }
      else if (!n)
        {
          _dbus_string_free (&p);
          _dbus_string_free (&buffer);
          dbus_set_error (error, DBUS_ERROR_IO_ERROR, "Could not read nonce from socket (fd=%d)", fd );
          return FALSE;
        }
      else
        {
          _dbus_string_append_len(&buffer, _dbus_string_get_const_data (&p), n);
          nleft -= n;
        }
    }

  result =  _dbus_string_equal_len (&buffer, nonce, 16);
  if (!result)
    dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED, "Nonces do not match, access denied (fd=%d)", fd );

  _dbus_string_free (&p);
  _dbus_string_free (&buffer);

  return result;
}
예제 #4
0
static void
test_hex_roundtrip (const unsigned char *data,
                    int                  len)
{
  DBusString orig;
  DBusString encoded;
  DBusString decoded;
  int end;

  if (len < 0)
    len = strlen (data);
  
  if (!_dbus_string_init (&orig))
    _dbus_assert_not_reached ("could not init string");

  if (!_dbus_string_init (&encoded))
    _dbus_assert_not_reached ("could not init string");
  
  if (!_dbus_string_init (&decoded))
    _dbus_assert_not_reached ("could not init string");

  if (!_dbus_string_append_len (&orig, data, len))
    _dbus_assert_not_reached ("couldn't append orig data");

  if (!_dbus_string_hex_encode (&orig, 0, &encoded, 0))
    _dbus_assert_not_reached ("could not encode");

  if (!_dbus_string_hex_decode (&encoded, 0, &end, &decoded, 0))
    _dbus_assert_not_reached ("could not decode");
    
  _dbus_assert (_dbus_string_get_length (&encoded) == end);

  if (!_dbus_string_equal (&orig, &decoded))
    {
      const char *s;
      
      printf ("Original string %d bytes encoded %d bytes decoded %d bytes\n",
              _dbus_string_get_length (&orig),
              _dbus_string_get_length (&encoded),
              _dbus_string_get_length (&decoded));
      printf ("Original: %s\n", data);
      s = _dbus_string_get_const_data (&decoded);
      printf ("Decoded: %s\n", s);
      _dbus_assert_not_reached ("original string not the same as string decoded from hex");
    }
  
  _dbus_string_free (&orig);
  _dbus_string_free (&encoded);
  _dbus_string_free (&decoded);  
}
예제 #5
0
/**
 * Returns the signature of the single complete type starting at the
 * given iterator.
 *
 * For example, if the iterator is pointing at the start of "(ii)ii"
 * (which is "a struct of two ints, followed by an int, followed by an
 * int"), then "(ii)" would be returned. If the iterator is pointing at
 * one of the "i" then just that "i" would be returned.
 *
 * @param iter pointer to an iterator 
 * @returns current signature; or #NULL if no memory.  Should be freed with dbus_free()
 */
char *
dbus_signature_iter_get_signature (const DBusSignatureIter *iter)
{
  DBusSignatureRealIter *real_iter = (DBusSignatureRealIter *) iter;
  DBusString str;
  char *ret;
  int pos;
  
  if (!_dbus_string_init (&str))
    return NULL;

  pos = 0;
  _dbus_type_signature_next (real_iter->pos, &pos);

  if (!_dbus_string_append_len (&str, real_iter->pos, pos))
    return NULL;
  if (!_dbus_string_steal_data (&str, &ret))
    ret = NULL;
  _dbus_string_free (&str);

  return ret; 
}
예제 #6
0
파일: dbus-nonce.c 프로젝트: halfline/dbus
static dbus_bool_t
do_check_nonce (DBusSocket fd, const DBusString *nonce, DBusError *error)
{
  DBusString buffer;
  DBusString p;
  size_t nleft;
  dbus_bool_t result;
  int n;

  _DBUS_ASSERT_ERROR_IS_CLEAR (error);

  nleft = 16;

  /* This is a trick to make it safe to call _dbus_string_free on these
   * strings during error unwinding, even if allocating memory for them
   * fails. A constant DBusString is considered to be valid to "free",
   * even though there is nothing to free (of course the free operation
   * is trivial, because it does not own its own buffer); but
   * unlike a mutable DBusString, initializing a constant DBusString
   * cannot fail.
   *
   * We must successfully re-initialize the strings to be mutable before
   * writing to them, of course.
   */
  _dbus_string_init_const (&buffer, "");
  _dbus_string_init_const (&p, "");

  if (   !_dbus_string_init (&buffer)
      || !_dbus_string_init (&p) ) {
        dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
        _dbus_string_free (&p);
        _dbus_string_free (&buffer);
        return FALSE;
      }

  while (nleft)
    {
      int saved_errno;

      n = _dbus_read_socket (fd, &p, nleft);
      saved_errno = _dbus_save_socket_errno ();

      if (n == -1 && _dbus_get_is_errno_eintr (saved_errno))
        ;
      else if (n == -1 && _dbus_get_is_errno_eagain_or_ewouldblock (saved_errno))
        _dbus_sleep_milliseconds (100);
      else if (n==-1)
        {
          dbus_set_error (error, DBUS_ERROR_IO_ERROR, "Could not read nonce from socket (fd=%" DBUS_SOCKET_FORMAT ")", _dbus_socket_printable (fd));
          _dbus_string_free (&p);
          _dbus_string_free (&buffer);
          return FALSE;
        }
      else if (!n)
        {
          _dbus_string_free (&p);
          _dbus_string_free (&buffer);
          dbus_set_error (error, DBUS_ERROR_IO_ERROR, "Could not read nonce from socket (fd=%" DBUS_SOCKET_FORMAT ")", _dbus_socket_printable (fd));
          return FALSE;
        }
      else
        {
          if (!_dbus_string_append_len (&buffer, _dbus_string_get_const_data (&p), n))
            {
              dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
              _dbus_string_free (&p);
              _dbus_string_free (&buffer);
              return FALSE;
            }
          nleft -= n;
        }
    }

  result =  _dbus_string_equal_len (&buffer, nonce, 16);
  if (!result)
    dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED, "Nonces do not match, access denied (fd=%" DBUS_SOCKET_FORMAT ")", _dbus_socket_printable (fd));

  _dbus_string_free (&p);
  _dbus_string_free (&buffer);

  return result;
}