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

#include "inspircd.h"
#include "m_cap.h"
#include "account.h"

/* $ModDesc: Provides support for atheme SASL via AUTHENTICATE. */

class CommandAuthenticate : public Command
{
	Module* Creator;
 public:
	CommandAuthenticate (InspIRCd* Instance, Module* creator) : Command(Instance,"AUTHENTICATE", 0, 1, true), Creator(creator)
	{
		this->source = "m_sasl.so";
	}

	CmdResult Handle (const char* const* parameters, int pcnt, User *user)
	{
		if (user->registered != REG_ALL)
		{
			/* Only act if theyve enabled CAP REQ sasl */
			if (user->GetExt("sasl"))
			{
				/* Only allow AUTHENTICATE on unregistered clients */
				std::deque<std::string> params;
				params.push_back("*");
				params.push_back("AUTHENTICATE");
				params.push_back(user->uuid);

				for (int i = 0; i < pcnt; ++i)
					params.push_back(parameters[i]);

				Event e((char*)&params, Creator, "send_encap");
				e.Send(ServerInstance);
			}
		}
		return CMD_FAILURE;
	}
};


class ModuleSASL : public Module
{
	CommandAuthenticate* sasl;
 public:
	
	ModuleSASL(InspIRCd* Me)
		: Module(Me)
	{
		Implementation eventlist[] = { I_OnEvent, I_OnUserRegister, I_OnPostConnect };
		ServerInstance->Modules->Attach(eventlist, this, 3);

		sasl = new CommandAuthenticate(ServerInstance, this);
		ServerInstance->AddCommand(sasl);

		if (!ServerInstance->Modules->Find("m_services_account.so") || !ServerInstance->Modules->Find("m_cap.so"))
			ServerInstance->Logs->Log("m_sasl", DEFAULT, "WARNING: m_services_account.so and m_cap.so are not loaded! m_sasl.so will NOT function correctly until these two modules are loaded!");
	}

	virtual int OnUserRegister(User *user)
	{
		if (user->GetExt("sasl"))
		{
			user->WriteServ("906 %s :SASL authentication aborted", user->nick);
			user->Shrink("sasl");
		}

		return 0;
	}

	virtual void OnPostConnect(User* user)
	{
		if (!IS_LOCAL(user))
			return;

		std::string* str = NULL;

		if (user->GetExt("acountname", str))
		{
			std::deque<std::string> params;
			params.push_back(user->uuid);
			params.push_back("accountname");
			params.push_back(*str);
			Event e((char*)&params, this, "send_metadata");
			e.Send(ServerInstance);
		}
		return;
	}

	virtual ~ModuleSASL()
	{
	}

	virtual Version GetVersion()
	{
		return Version(1,2,0,1,VF_VENDOR,API_VERSION);
	}

	virtual void OnEvent(Event *ev)
	{
		GenericCapHandler(ev, "sasl", "sasl");

		if (ev->GetEventID() == "encap_received")
		{
			/* Received encap reply, look for AUTHENTICATE */
			std::deque<std::string>* parameters = (std::deque<std::string>*)ev->GetData();

			User* target = ServerInstance->FindNick((*parameters)[0]);

			if (target)
			{
				/* Found a user */
				parameters->pop_front();
				std::string line = irc::stringjoiner(" ", *parameters, 0, parameters->size() - 1).GetJoined();
				target->WriteServ("AUTHENTICATE %s", line.c_str());
			}
		}
		else if (ev->GetEventID() == "account_login")
		{
			AccountData* ac = (AccountData*)ev->GetData();

			if (ac->user->GetExt("sasl"))
			{
				ac->user->WriteServ("903 %s :SASL authentication successful", ac->user->nick);
				ac->user->Shrink("sasl");
			}
		}
	}
};

MODULE_INIT(ModuleSASL)