1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 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 187 188 189 190
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* soup-connection-auth.c: Abstract base class for hacky Microsoft
* connection-based auth mechanisms (NTLM and Negotiate)
*
* Copyright (C) 2010 Red Hat, Inc.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <ctype.h>
#include <string.h>
#include "soup-connection-auth.h"
#include "soup.h"
#include "soup-connection.h"
#include "soup-message-private.h"
struct SoupConnectionAuthPrivate {
GHashTable *conns;
};
G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (SoupConnectionAuth, soup_connection_auth, SOUP_TYPE_AUTH)
static void
soup_connection_auth_init (SoupConnectionAuth *auth)
{
auth->priv = soup_connection_auth_get_instance_private (auth);
auth->priv->conns = g_hash_table_new (NULL, NULL);
}
static void connection_disconnected (SoupConnection *conn, gpointer user_data);
static void
soup_connection_auth_free_connection_state (SoupConnectionAuth *auth,
SoupConnection *conn,
gpointer state)
{
g_signal_handlers_disconnect_by_func (conn, G_CALLBACK (connection_disconnected), auth);
SOUP_CONNECTION_AUTH_GET_CLASS (auth)->free_connection_state (auth, state);
}
static void
connection_disconnected (SoupConnection *conn, gpointer user_data)
{
SoupConnectionAuth *auth = user_data;
gpointer state;
state = g_hash_table_lookup (auth->priv->conns, conn);
g_hash_table_remove (auth->priv->conns, conn);
soup_connection_auth_free_connection_state (auth, conn, state);
}
static void
soup_connection_auth_finalize (GObject *object)
{
SoupConnectionAuth *auth = SOUP_CONNECTION_AUTH (object);
GHashTableIter iter;
gpointer conn, state;
g_hash_table_iter_init (&iter, auth->priv->conns);
while (g_hash_table_iter_next (&iter, &conn, &state)) {
soup_connection_auth_free_connection_state (auth, conn, state);
g_hash_table_iter_remove (&iter);
}
g_hash_table_destroy (auth->priv->conns);
G_OBJECT_CLASS (soup_connection_auth_parent_class)->finalize (object);
}
/**
* soup_connection_auth_get_connection_state_for_message:
* @auth: a #SoupConnectionAuth
* @msg: a #SoupMessage
*
* Returns an associated connection state object for the given @auth and @msg.
*
* This function is only useful from within implementations of SoupConnectionAuth
* subclasses.
*
* Return value: (transfer none): the connection state
*
* Since: 2.58
**/
gpointer
soup_connection_auth_get_connection_state_for_message (SoupConnectionAuth *auth,
SoupMessage *msg)
{
SoupConnection *conn;
gpointer state;
g_return_val_if_fail (SOUP_IS_CONNECTION_AUTH (auth), NULL);
g_return_val_if_fail (SOUP_IS_MESSAGE (msg), NULL);
conn = soup_message_get_connection (msg);
state = g_hash_table_lookup (auth->priv->conns, conn);
if (state)
return state;
state = SOUP_CONNECTION_AUTH_GET_CLASS (auth)->create_connection_state (auth);
if (conn) {
g_signal_connect (conn, "disconnected",
G_CALLBACK (connection_disconnected), auth);
}
g_hash_table_insert (auth->priv->conns, conn, state);
return state;
}
static gboolean
soup_connection_auth_update (SoupAuth *auth,
SoupMessage *msg,
GHashTable *auth_params)
{
SoupConnectionAuth *cauth = SOUP_CONNECTION_AUTH (auth);
gpointer conn = soup_connection_auth_get_connection_state_for_message (cauth, msg);
GHashTableIter iter;
GString *auth_header;
gpointer key, value;
gboolean result;
/* Recreate @auth_header out of @auth_params. If the
* base64 data ended with 1 or more "="s, then it
* will have been parsed as key=value. Otherwise
* it will all have been parsed as key, and value
* will be %NULL.
*/
auth_header = g_string_new (soup_auth_get_scheme_name (auth));
g_hash_table_iter_init (&iter, auth_params);
if (g_hash_table_iter_next (&iter, &key, &value)) {
if (value) {
g_string_append_printf (auth_header, " %s=%s",
(char *)key,
(char *)value);
} else {
g_string_append_printf (auth_header, " %s",
(char *)key);
}
if (g_hash_table_iter_next (&iter, &key, &value)) {
g_string_free (auth_header, TRUE);
return FALSE;
}
}
result = SOUP_CONNECTION_AUTH_GET_CLASS (auth)->
update_connection (cauth, msg, auth_header->str, conn);
g_string_free (auth_header, TRUE);
return result;
}
static char *
soup_connection_auth_get_authorization (SoupAuth *auth,
SoupMessage *msg)
{
SoupConnectionAuth *cauth = SOUP_CONNECTION_AUTH (auth);
gpointer conn = soup_connection_auth_get_connection_state_for_message (cauth, msg);
return SOUP_CONNECTION_AUTH_GET_CLASS (auth)->
get_connection_authorization (cauth, msg, conn);
}
static gboolean
soup_connection_auth_is_ready (SoupAuth *auth,
SoupMessage *msg)
{
SoupConnectionAuth *cauth = SOUP_CONNECTION_AUTH (auth);
gpointer conn = soup_connection_auth_get_connection_state_for_message (cauth, msg);
return SOUP_CONNECTION_AUTH_GET_CLASS (auth)->
is_connection_ready (SOUP_CONNECTION_AUTH (auth), msg, conn);
}
static void
soup_connection_auth_class_init (SoupConnectionAuthClass *connauth_class)
{
SoupAuthClass *auth_class = SOUP_AUTH_CLASS (connauth_class);
GObjectClass *object_class = G_OBJECT_CLASS (connauth_class);
auth_class->update = soup_connection_auth_update;
auth_class->get_authorization = soup_connection_auth_get_authorization;
auth_class->is_ready = soup_connection_auth_is_ready;
object_class->finalize = soup_connection_auth_finalize;
}
|