summaryrefslogtreecommitdiffstats
path: root/src/auth.cpp
blob: ff5c4bc8479d8087f2e9e0c47b07dce3d27dd2c6 (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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
    Copyright (C) 2005-2009  Michel de Boer <michel@twinklephone.com>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include <cassert>
#include "auth.h"
#include "log.h"
#include "protocol.h"
#include "user.h"
#include "userintf.h"
#include "util.h"

extern string		user_host;

t_cr_cache_entry:: t_cr_cache_entry(const t_url &_to, const t_credentials &_cr,
	const string &_passwd, bool _proxy) :
		to(_to)
{
	credentials = _cr;
	passwd = _passwd;
	proxy = _proxy;
}


list<t_cr_cache_entry>::iterator t_auth::find_cache_entry(
	const t_url &_to, const string &realm, bool proxy)
{
	for (list<t_cr_cache_entry>::iterator i = cache.begin();
	     i != cache.end(); i++)
	{
		// RFC 3261 22.1
		// Only the realm determines the protection space.
		// So the to-uri must not need to be compared as for HTTP
		// i.e. check i->to == _to must not be done.
		// As realm strings are globally unique there is no need
		// to check if the credentials are for a 407 or 401 response.
		// i.e. check i->proxy == proxy is not needed.
		if (i->credentials.digest_response.realm == realm)
		{
			return i;
		}
	}

	return cache.end();
}

void t_auth::update_cache(const t_url &to, const t_credentials &cr,
	const string &passwd, bool proxy)
{
	list<t_cr_cache_entry>::iterator i, j;

	i = find_cache_entry(to, cr.digest_response.realm, proxy);

	if (i == cache.end()) {
		if (cache.size() > AUTH_CACHE_SIZE) {
			cache.erase(cache.begin());
		}
		cache.push_back(t_cr_cache_entry(to, cr, passwd, proxy));
	} else {
		i->credentials = cr;
		i->passwd = passwd;

		// Move cache entry to end of the cache.
		// TODO: this can be more efficient by checking if the
		//       entry is already at the end.
		t_cr_cache_entry e = *i;
		cache.erase(i);
		cache.push_back(e);
	}
}

bool t_auth::auth_failed(t_request *r, const t_challenge &c,
	bool proxy) const
{
	if (c.digest_challenge.stale) {
		log_file->write_report("Stale nonce value.", "t_auth::auth_failed");
		return false;
	}

	if (proxy) {
		return r->hdr_proxy_authorization.contains(
			c.digest_challenge.realm, r->uri);
	} else {
		return r->hdr_authorization.contains(
			c.digest_challenge.realm, r->uri);
	}
}

void t_auth::remove_credentials(t_request *r, const t_challenge &c,
	bool proxy) const
{
	if (proxy) {
		r->hdr_proxy_authorization.remove_credentials(
			c.digest_challenge.realm, r->uri);
	} else {
		r->hdr_authorization.remove_credentials(
			c.digest_challenge.realm, r->uri);
	}
}

t_auth::t_auth() {
	re_register = false;
}

bool t_auth::authorize(t_user *user_config, t_request *r, t_response *resp) {
	string username;
	string passwd;
	list<t_cr_cache_entry>::iterator i;
	t_challenge c;
	bool proxy;

	assert(resp->must_authenticate());

	if (resp->code == R_401_UNAUTHORIZED) {
		c = resp->hdr_www_authenticate.challenge;
		proxy = false;
	} else {
		c = resp->hdr_proxy_authenticate.challenge;
		proxy = true;
	}

	// Only DIGEST is supported
	if (c.auth_scheme != AUTH_DIGEST) {
		log_file->write_header("t_auth::authorize");
		log_file->write_raw("Unsupported authentication scheme: ");
		log_file->write_raw(c.auth_scheme);
		log_file->write_endl();
		log_file->write_footer();
		return false;
	}

	const t_digest_challenge &dc = c.digest_challenge;
	i = find_cache_entry(r->uri, dc.realm, proxy);

	if (auth_failed(r, c, proxy)) {
		// The current credentials are wrong. Remove them and
		// ask the user for a username and password.
		remove_credentials(r, c, proxy);
	} else {
		// Determine user name and password
		if (i != cache.end()) {
			username = i->credentials.digest_response.username;
			passwd = i->passwd;
		} else if (dc.realm == user_config->get_auth_realm() ||
		           user_config->get_auth_realm() == "") {
			username = user_config->get_auth_name();
			passwd = user_config->get_auth_pass();
		}

		if (dc.stale) {
			// The current credentials are stale. Remove them.
			remove_credentials(r, c, proxy);
		}
	}

	// Ask user for username/password
	if ((username == "" || passwd == "") && !re_register) {
		if (!ui->cb_ask_credentials(user_config, dc.realm, username, passwd)) {
			log_file->write_report("Asking user name and password failed.",
						"t_auth::authorize");
			return false;
		}
	}

	// No valid username/passwd
	if (username == "" && passwd == "") {
		log_file->write_report("Incorrect user name and/or password.",
						"t_auth::authorize");
		return false;
	}
	
	bool auth_success;
	string fail_reason;
	if (!proxy) {
		t_credentials cr;
		auth_success = r->www_authorize(c, user_config,
			username, passwd, 1, NEW_CNONCE, cr, fail_reason);

		if (auth_success) {
			update_cache(r->uri, cr, passwd, proxy);
		}
	} else {
		t_credentials cr;
		auth_success = r->proxy_authorize(c, user_config,
			username, passwd, 1, NEW_CNONCE, cr, fail_reason);

		if (auth_success) {
			update_cache(r->uri, cr, passwd, proxy);
		}
	}

	if (!auth_success) {
		log_file->write_report(fail_reason, "t_auth::authorize");
		return false;
	}

	return true;
}

void t_auth::remove_from_cache(const string &realm) {
	if (realm.empty()) {
		cache.clear();
	} else {	
		list<t_cr_cache_entry>::iterator i = find_cache_entry(t_url(), realm);
		if (i != cache.end()) {
			cache.erase(i);
		}
	}
}

void t_auth::set_re_register(bool on) {
	re_register = on;
}

bool t_auth::get_re_register(void) const {
	return re_register;
}