Ejemplo n.º 1
0
/**
 * Encode and send password on a socket
 */
bool synce_password_send(
		SynceSocket* socket,
		const char* asciiPassword,
		unsigned char key)
{
	bool success = false;
	unsigned char* encoded_password = NULL;
	size_t size = 0;
	uint16_t size_le = 0;
	
	if (!synce_password_encode(asciiPassword, key, &encoded_password, &size))
	{
		synce_error("failed to encode password");
	}

	size_le = htole16((uint16_t)size);

	if ( !synce_socket_write(socket, &size_le, sizeof(uint16_t)) )
	{
		synce_error("failed to write buffer size to socket");
		goto exit;
	}

	if ( !synce_socket_write(socket, encoded_password, size) )
	{
		synce_error("failed to write encoded password to socket");
		goto exit;
	}

	success = true;

exit:
	synce_password_free(encoded_password);
	return success;
}
Ejemplo n.º 2
0
static gboolean
synce_device_provide_password_impl (G_GNUC_UNUSED SynceDbusDevice *interface,
				    GDBusMethodInvocation *ctx,
				    const gchar *password,
				    gpointer userdata)
{
  SynceDevice *self = SYNCE_DEVICE (userdata);
#else /* USE_GDBUS */
static void
synce_device_provide_password_impl (SynceDevice *self,
				    const gchar *password,
				    DBusGMethodInvocation *ctx)
{
#endif

  SynceDevicePrivate *priv = SYNCE_DEVICE_GET_PRIVATE(self);

#if USE_GDBUS
  g_return_val_if_fail(priv->inited && !(priv->dispose_has_run), TRUE);
#else
  g_return_if_fail(priv->inited && !(priv->dispose_has_run));
#endif

  GError *error = NULL;
  guchar *buf;
  gsize buf_size;

  if (priv->state != CTRL_STATE_AUTH ||
      (priv->pw_flags & SYNCE_DEVICE_PASSWORD_FLAG_PROVIDE) == 0)
    {
      error = g_error_new (SYNCE_DCCM_ERROR, SYNCE_DCCM_ERROR_NOT_AVAILABLE,
			   "No password expected in the current state.");
      goto OUT;
    }
  else if (priv->pw_ctx != NULL)
    {
      error = g_error_new (SYNCE_DCCM_ERROR, SYNCE_DCCM_ERROR_NOT_AVAILABLE,
			   "An authentication attempt is still in progress.");
      goto OUT;
    }

  priv->pw_ctx = ctx;

  synce_password_encode(password, priv->pw_key, &buf, &buf_size);

  buf_size = GUINT16_TO_LE (buf_size);
  gsize written = 0;
  GOutputStream *out_stream = g_io_stream_get_output_stream(G_IO_STREAM(priv->conn));
  if (!(g_output_stream_write_all(out_stream, (gchar *) &buf_size, sizeof (buf_size), &written, NULL, &error))) {
    g_critical("%s: failed to send password to device: %s", G_STRFUNC, error->message);
    goto OUT;
  }
  if (!(g_output_stream_write_all(out_stream, buf, buf_size, &written, NULL, &error))) {
    g_critical("%s: failed to send password to device: %s", G_STRFUNC, error->message);
    goto OUT;
  }

  GInputStream *in_stream = g_io_stream_get_input_stream(G_IO_STREAM(priv->conn));
  priv->iobuf = g_malloc(sizeof (guint16));
  g_input_stream_read_async(in_stream, priv->iobuf, sizeof (guint16), G_PRIORITY_DEFAULT, NULL, synce_device_conn_event_cb, g_object_ref(self));

  synce_device_change_password_flags (self, SYNCE_DEVICE_PASSWORD_FLAG_CHECKING);

 OUT:
  if (buf) wstr_free_string(buf);
  if (error != NULL)
#if USE_GDBUS
    {
      g_dbus_method_invocation_return_gerror(ctx, error);
      g_error_free(error);
    }
  return TRUE;
#else
  {
    dbus_g_method_return_error (ctx, error);
  }
  return;
#endif
}