summaryrefslogtreecommitdiff
path: root/libsoup/soup-connection-auth.c
blob: fc327335acef81b56acd91326b0cfc26790704d7 (plain)
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
/* -*- 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"

G_DEFINE_ABSTRACT_TYPE (SoupConnectionAuth, soup_connection_auth, SOUP_TYPE_AUTH)

struct SoupConnectionAuthPrivate {
	GHashTable *conns;
};

static void
soup_connection_auth_init (SoupConnectionAuth *auth)
{
	auth->priv = G_TYPE_INSTANCE_GET_PRIVATE (auth, SOUP_TYPE_CONNECTION_AUTH, SoupConnectionAuthPrivate);

	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);
}

static gpointer
get_connection_state_for_message (SoupConnectionAuth *auth, SoupMessage *msg)
{
	SoupConnection *conn;
	gpointer state;

	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 = 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 = 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 = 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);

	g_type_class_add_private (connauth_class, sizeof (SoupConnectionAuthPrivate));

	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;
}