summaryrefslogtreecommitdiff
path: root/src/modules/m_authcache.cpp
blob: c87938f8579a591416ec3fb333d17f993d02b2fd (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
/*       +------------------------------------+
 *       | Inspire Internet Relay Chat Daemon |
 *       +------------------------------------+
 *
 *  InspIRCd: (C) 2002-2010 InspIRCd Development Team
 * See: http://wiki.inspircd.org/Credits
 *
 * This program is free but copyrighted software; see
 *            the file COPYING for details.
 *
 * ---------------------------------------------------
 */

#include "inspircd.h"
#include "account.h"
#include "ssl.h"

// Password description - method, authdata
typedef std::pair<std::string, std::string> passdesc;
typedef std::multimap<std::string,passdesc> AuthMap;

class ModuleAuthCache : public Module
{
	AuthMap usernames;

 public:
	ModuleAuthCache()
	{
	}

	void init()
	{
		Implementation eventlist[] = { I_OnCheckReady, I_OnDecodeMetaData, I_OnSyncNetwork };
		ServerInstance->Modules->Attach(eventlist, this, 3);
	}

	void OnSyncNetwork(Module* proto, void* opaque)
	{
		for(AuthMap::iterator it = usernames.begin(); it != usernames.end(); it++)
			proto->ProtoSendMetaData(opaque, NULL, "authcache", it->second.first +
				" " + it->first + " " + it->second.second);
	}

	void OnDecodeMetaData(Extensible* dest, const std::string& name, const std::string& value)
	{
		if (dest || name != "authcache")
			return;
		irc::spacesepstream splitter(value);
		std::string method, id, data;
		splitter.GetToken(method);
		splitter.GetToken(id);
		data = splitter.GetRemaining();

		std::pair<AuthMap::iterator, AuthMap::iterator> it = usernames.equal_range(id);
		while (it.first != it.second)
		{
			if (it.first->second.first == method)
			{
				usernames.erase(it.first);
				break;
			}
			it.first++;
		}

		if (!data.empty())
		{
			usernames.insert(std::make_pair(id, std::make_pair(method, data)));
		}
	}

	ModResult OnCheckReady(LocalUser* user)
	{
		std::string login;

		std::pair<AuthMap::iterator, AuthMap::iterator> it = usernames.equal_range(user->nick);
		while (it.first != it.second)
		{
			if (!ServerInstance->PassCompare(user, it.first->second.second, user->password, it.first->second.first))
				login = user->nick;
			it.first++;
		}

		SocketCertificateRequest req(&user->eh, this);
		req.Send();
		if (req.cert)
		{
			it = usernames.equal_range(req.cert->GetFingerprint());
			while (it.first != it.second)
			{
				if (it.first->second.first == "sslfp")
					login = it.first->second.second;
				it.first++;
			}
		}

		if (!login.empty())
		{
			AccountExtItem* ext = GetAccountExtItem();
			if (ext)
			{
				ext->set(user, login);
				AccountEvent(this, user, login).Send();
			}
		}
		return MOD_RES_PASSTHRU;
	}

	Version GetVersion()
	{
		return Version("Provides an authorization cache for user accounts", VF_OPTCOMMON | VF_VENDOR);
	}
};

MODULE_INIT(ModuleAuthCache)