gdbusauthmechanism.c 11.8 KB
Newer Older
1 2
/* GDBus - GLib D-Bus Library
 *
3
 * Copyright (C) 2008-2010 Red Hat, Inc.
4 5 6 7
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
8
 * version 2.1 of the License, or (at your option) any later version.
9 10 11 12 13 14 15
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
16
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 18 19 20 21 22 23 24 25 26 27 28
 *
 * Author: David Zeuthen <davidz@redhat.com>
 */

#include "config.h"

#include "gdbusauthmechanism.h"
#include "gcredentials.h"
#include "gdbuserror.h"
#include "gioenumtypes.h"
#include "giostream.h"

David Zeuthen's avatar
David Zeuthen committed
29 30
#include "glibintl.h"

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
/* ---------------------------------------------------------------------------------------------------- */

struct _GDBusAuthMechanismPrivate
{
  GIOStream *stream;
  GCredentials *credentials;
};

enum
{
  PROP_0,
  PROP_STREAM,
  PROP_CREDENTIALS
};

46
G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GDBusAuthMechanism, _g_dbus_auth_mechanism, G_TYPE_OBJECT)
47 48 49 50 51 52 53 54 55 56 57 58 59

/* ---------------------------------------------------------------------------------------------------- */

static void
_g_dbus_auth_mechanism_finalize (GObject *object)
{
  GDBusAuthMechanism *mechanism = G_DBUS_AUTH_MECHANISM (object);

  if (mechanism->priv->stream != NULL)
    g_object_unref (mechanism->priv->stream);
  if (mechanism->priv->credentials != NULL)
    g_object_unref (mechanism->priv->credentials);

Matthias Clasen's avatar
Matthias Clasen committed
60
  G_OBJECT_CLASS (_g_dbus_auth_mechanism_parent_class)->finalize (object);
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
}

static void
_g_dbus_auth_mechanism_get_property (GObject    *object,
                                     guint       prop_id,
                                     GValue     *value,
                                     GParamSpec *pspec)
{
  GDBusAuthMechanism *mechanism = G_DBUS_AUTH_MECHANISM (object);

  switch (prop_id)
    {
    case PROP_STREAM:
      g_value_set_object (value, mechanism->priv->stream);
      break;

    case PROP_CREDENTIALS:
      g_value_set_object (value, mechanism->priv->credentials);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
_g_dbus_auth_mechanism_set_property (GObject      *object,
                                     guint         prop_id,
                                     const GValue *value,
                                     GParamSpec   *pspec)
{
  GDBusAuthMechanism *mechanism = G_DBUS_AUTH_MECHANISM (object);

  switch (prop_id)
    {
    case PROP_STREAM:
      mechanism->priv->stream = g_value_dup_object (value);
      break;

    case PROP_CREDENTIALS:
      mechanism->priv->credentials = g_value_dup_object (value);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
_g_dbus_auth_mechanism_class_init (GDBusAuthMechanismClass *klass)
{
  GObjectClass *gobject_class;

  gobject_class = G_OBJECT_CLASS (klass);
  gobject_class->get_property = _g_dbus_auth_mechanism_get_property;
  gobject_class->set_property = _g_dbus_auth_mechanism_set_property;
  gobject_class->finalize     = _g_dbus_auth_mechanism_finalize;

  g_object_class_install_property (gobject_class,
                                   PROP_STREAM,
                                   g_param_spec_object ("stream",
Matthias Clasen's avatar
Matthias Clasen committed
124 125
                                                        P_("IO Stream"),
                                                        P_("The underlying GIOStream used for I/O"),
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
                                                        G_TYPE_IO_STREAM,
                                                        G_PARAM_READABLE |
                                                        G_PARAM_WRITABLE |
                                                        G_PARAM_CONSTRUCT_ONLY |
                                                        G_PARAM_STATIC_NAME |
                                                        G_PARAM_STATIC_BLURB |
                                                        G_PARAM_STATIC_NICK));

  /**
   * GDBusAuthMechanism:credentials:
   *
   * If authenticating as a server, this property contains the
   * received credentials, if any.
   *
   * If authenticating as a client, the property contains the
   * credentials that were sent, if any.
   */
  g_object_class_install_property (gobject_class,
                                   PROP_CREDENTIALS,
                                   g_param_spec_object ("credentials",
Matthias Clasen's avatar
Matthias Clasen committed
146 147
                                                        P_("Credentials"),
                                                        P_("The credentials of the remote peer"),
148 149 150 151 152 153 154 155 156 157 158 159
                                                        G_TYPE_CREDENTIALS,
                                                        G_PARAM_READABLE |
                                                        G_PARAM_WRITABLE |
                                                        G_PARAM_CONSTRUCT_ONLY |
                                                        G_PARAM_STATIC_NAME |
                                                        G_PARAM_STATIC_BLURB |
                                                        G_PARAM_STATIC_NICK));
}

static void
_g_dbus_auth_mechanism_init (GDBusAuthMechanism *mechanism)
{
160
  mechanism->priv = _g_dbus_auth_mechanism_get_instance_private (mechanism);
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
}

/* ---------------------------------------------------------------------------------------------------- */

GIOStream *
_g_dbus_auth_mechanism_get_stream (GDBusAuthMechanism *mechanism)
{
  g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), NULL);
  return mechanism->priv->stream;
}

GCredentials *
_g_dbus_auth_mechanism_get_credentials (GDBusAuthMechanism *mechanism)
{
  g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), NULL);
  return mechanism->priv->credentials;
}

/* ---------------------------------------------------------------------------------------------------- */

const gchar *
_g_dbus_auth_mechanism_get_name (GType mechanism_type)
{
  const gchar *name;
  GDBusAuthMechanismClass *klass;

Matthias Clasen's avatar
Matthias Clasen committed
187
  g_return_val_if_fail (g_type_is_a (mechanism_type, G_TYPE_DBUS_AUTH_MECHANISM), NULL);
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202

  klass = g_type_class_ref (mechanism_type);
  g_assert (klass != NULL);
  name = klass->get_name ();
  //g_type_class_unref (klass);

  return name;
}

gint
_g_dbus_auth_mechanism_get_priority (GType mechanism_type)
{
  gint priority;
  GDBusAuthMechanismClass *klass;

Matthias Clasen's avatar
Matthias Clasen committed
203
  g_return_val_if_fail (g_type_is_a (mechanism_type, G_TYPE_DBUS_AUTH_MECHANISM), 0);
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222

  klass = g_type_class_ref (mechanism_type);
  g_assert (klass != NULL);
  priority = klass->get_priority ();
  //g_type_class_unref (klass);

  return priority;
}

/* ---------------------------------------------------------------------------------------------------- */

gboolean
_g_dbus_auth_mechanism_is_supported (GDBusAuthMechanism *mechanism)
{
  g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), FALSE);
  return G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->is_supported (mechanism);
}

gchar *
Matthias Clasen's avatar
Matthias Clasen committed
223 224 225 226
_g_dbus_auth_mechanism_encode_data (GDBusAuthMechanism *mechanism,
                                    const gchar        *data,
                                    gsize               data_len,
                                    gsize              *out_data_len)
227 228 229 230 231 232 233
{
  g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), NULL);
  return G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->encode_data (mechanism, data, data_len, out_data_len);
}


gchar *
Matthias Clasen's avatar
Matthias Clasen committed
234 235 236 237
_g_dbus_auth_mechanism_decode_data (GDBusAuthMechanism *mechanism,
                                    const gchar        *data,
                                    gsize               data_len,
                                    gsize              *out_data_len)
238 239 240 241 242 243 244 245
{
  g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), NULL);
  return G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->decode_data (mechanism, data, data_len, out_data_len);
}

/* ---------------------------------------------------------------------------------------------------- */

GDBusAuthMechanismState
Matthias Clasen's avatar
Matthias Clasen committed
246
_g_dbus_auth_mechanism_server_get_state (GDBusAuthMechanism *mechanism)
247 248 249 250 251 252
{
  g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), G_DBUS_AUTH_MECHANISM_STATE_INVALID);
  return G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->server_get_state (mechanism);
}

void
Matthias Clasen's avatar
Matthias Clasen committed
253 254 255
_g_dbus_auth_mechanism_server_initiate (GDBusAuthMechanism *mechanism,
                                        const gchar        *initial_response,
                                        gsize               initial_response_len)
256 257 258 259 260 261
{
  g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism));
  G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->server_initiate (mechanism, initial_response, initial_response_len);
}

void
Matthias Clasen's avatar
Matthias Clasen committed
262 263 264
_g_dbus_auth_mechanism_server_data_receive (GDBusAuthMechanism *mechanism,
                                            const gchar        *data,
                                            gsize               data_len)
265 266 267 268 269 270
{
  g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism));
  G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->server_data_receive (mechanism, data, data_len);
}

gchar *
Matthias Clasen's avatar
Matthias Clasen committed
271 272
_g_dbus_auth_mechanism_server_data_send (GDBusAuthMechanism *mechanism,
                                         gsize              *out_data_len)
273 274 275 276 277 278
{
  g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), NULL);
  return G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->server_data_send (mechanism, out_data_len);
}

gchar *
Matthias Clasen's avatar
Matthias Clasen committed
279
_g_dbus_auth_mechanism_server_get_reject_reason (GDBusAuthMechanism *mechanism)
280 281 282 283 284 285
{
  g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), NULL);
  return G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->server_get_reject_reason (mechanism);
}

void
Matthias Clasen's avatar
Matthias Clasen committed
286
_g_dbus_auth_mechanism_server_shutdown (GDBusAuthMechanism *mechanism)
287 288 289 290 291 292 293 294
{
  g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism));
  G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->server_shutdown (mechanism);
}

/* ---------------------------------------------------------------------------------------------------- */

GDBusAuthMechanismState
Matthias Clasen's avatar
Matthias Clasen committed
295
_g_dbus_auth_mechanism_client_get_state (GDBusAuthMechanism *mechanism)
296 297 298 299 300 301
{
  g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), G_DBUS_AUTH_MECHANISM_STATE_INVALID);
  return G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->client_get_state (mechanism);
}

gchar *
Matthias Clasen's avatar
Matthias Clasen committed
302 303
_g_dbus_auth_mechanism_client_initiate (GDBusAuthMechanism *mechanism,
                                        gsize              *out_initial_response_len)
304 305 306 307 308 309 310
{
  g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), NULL);
  return G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->client_initiate (mechanism,
                                                                       out_initial_response_len);
}

void
Matthias Clasen's avatar
Matthias Clasen committed
311 312 313
_g_dbus_auth_mechanism_client_data_receive (GDBusAuthMechanism *mechanism,
                                            const gchar        *data,
                                            gsize               data_len)
314 315 316 317 318 319
{
  g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism));
  G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->client_data_receive (mechanism, data, data_len);
}

gchar *
Matthias Clasen's avatar
Matthias Clasen committed
320 321
_g_dbus_auth_mechanism_client_data_send (GDBusAuthMechanism *mechanism,
                                         gsize              *out_data_len)
322 323 324 325 326 327
{
  g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), NULL);
  return G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->client_data_send (mechanism, out_data_len);
}

void
Matthias Clasen's avatar
Matthias Clasen committed
328
_g_dbus_auth_mechanism_client_shutdown (GDBusAuthMechanism *mechanism)
329 330 331 332 333 334
{
  g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism));
  G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->client_shutdown (mechanism);
}

/* ---------------------------------------------------------------------------------------------------- */