aboutsummaryrefslogtreecommitdiff
path: root/src/modules/m_account.cpp
blob: c596c6a1f75c959e12c1608cec331cfaf754670b (about) (plain) (blame)
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/*       +------------------------------------+
 *       | Inspire Internet Relay Chat Daemon |
 *       +------------------------------------+
 *
 *  InspIRCd: (C) 2002-2011 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 "protocol.h"
#include "account.h"
#include "hash.h"

/* $ModDesc: Store account information and allow logging in to them ircd-side */

static dynamic_reference<AccountProvider> account("account");

class AccountDBEntryImpl : public AccountDBEntry
{
 public:
	AccountDBEntryImpl(const irc::string& nameref, time_t ourTS, std::string h = "", std::string p = "", time_t h_p_ts = 0, std::string cc = "", time_t cc_ts = 0) : AccountDBEntry(nameref, ourTS, h, p, h_p_ts, cc, cc_ts)
	{
	}
	virtual CullResult cull()
	{
		return Extensible::cull();
	}
	virtual ~AccountDBEntryImpl()
	{
	}
};

class AccountDBProviderImpl : public AccountDBProvider
{
 public:
	AccountDB db;

	AccountDBProviderImpl(Module* parent) : AccountDBProvider(parent) {}

	AccountDBEntry* AddAccount(bool send, const irc::string& nameref, time_t ourTS, std::string h = "", std::string p = "", time_t h_p_ts = 0, std::string cc = "", time_t cc_ts = 0)
	{
		if(db.find(nameref) != db.end())
			return NULL;
		AccountDBEntry* entry = new AccountDBEntryImpl(nameref, ourTS, h, p, h_p_ts, cc, cc_ts);
		db.insert(std::make_pair(nameref, entry));
		if(send)
			SendAccount(entry);
		return entry;
	}

	AccountDBEntry* GetAccount(irc::string acctname, bool alias) const
	{
		if(alias)
		{
			GetAccountByAliasEvent e(creator, acctname);
			if(e.entry)
				return e.entry;
		}
		AccountDB::const_iterator res = db.find(acctname);
		return res != db.end() ? res->second : NULL;
	}

	void RemoveAccount(bool send, AccountDBEntry* entry)
	{
		db.erase(entry->name);
		if(account)
			for (user_hash::const_iterator i = ServerInstance->Users->clientlist->begin(); i != ServerInstance->Users->clientlist->end(); ++i)
			{
				if(!IS_LOCAL(i->second))
					continue;
				if(entry->name == account->GetAccountName(i->second))
				{
					account->DoLogin(i->second, "", "");
					i->second->WriteServ("NOTICE %s :Account %s has been dropped", i->second->nick.c_str(), entry->name.c_str());
				}
			}
		if(send)
			SendRemoval(entry->name, entry->ts);
		entry->cull();
		delete entry;
	}

	const AccountDB& GetDB() const
	{
		return db;
	}

	void SendAccount(const AccountDBEntry* entry) const
	{
		std::vector<std::string> params;
		params.push_back("*");
		params.push_back("SVSACCOUNT");
		params.push_back("ADD");
		params.push_back(entry->name);
		params.push_back(":" + ConvToStr(entry->ts));
		ServerInstance->PI->SendEncapsulatedData(params);
		params.clear();
		params.push_back("*");
		params.push_back("SVSACCOUNT");
		params.push_back("SET");
		params.push_back(entry->name);
		params.push_back(ConvToStr(entry->ts));
		params.push_back("hash_password");
		params.push_back(ConvToStr(entry->hash_password_ts));
		params.push_back(":" + entry->hash + " " + entry->password);
		ServerInstance->PI->SendEncapsulatedData(params);
		for(Extensible::ExtensibleStore::const_iterator it = entry->GetExtList().begin(); it != entry->GetExtList().end(); ++it)
		{
			ExtensionItem* item = it->first;
			std::string value = item->serialize(FORMAT_NETWORK, entry, it->second);
			if (!value.empty())
			{
				params.clear();
				params.push_back("*");
				params.push_back("SVSACCOUNT");
				params.push_back("SET");
				params.push_back(entry->name);
				params.push_back(ConvToStr(entry->ts));
				params.push_back(item->name);
				params.push_back(value);
				ServerInstance->PI->SendEncapsulatedData(params);
			}
		}
		AccountDBModifiedEvent(creator, entry->name, entry).Send();
	}

	void SendUpdate(const AccountDBEntry* entry, std::string field) const
	{
		std::vector<std::string> params;
		params.push_back("*");
		params.push_back("SVSACCOUNT");
		params.push_back("SET");
		params.push_back(entry->name);
		params.push_back(ConvToStr(entry->ts));
		params.push_back(field);
		if(field == "hash_password")
		{
			params.push_back(ConvToStr(entry->hash_password_ts));
			params.push_back(":" + entry->hash + " " + entry->password);
		}
		else
		{
			ExtensionItem* ext = ServerInstance->Extensions.GetItem(field);
			if(!ext)
				return;
			Extensible::ExtensibleStore::const_iterator it = entry->GetExtList().find(ext);
			if(it == entry->GetExtList().end())
				return;
			std::string value = ext->serialize(FORMAT_NETWORK, entry, it->second);
			if(value.empty())
				return;
			params.push_back(value);
		}
		ServerInstance->PI->SendEncapsulatedData(params);
		AccountDBModifiedEvent(creator, entry->name, entry).Send();
	}

	void SendRemoval(irc::string acctname, time_t ts) const
	{
		std::vector<std::string> params;
		params.push_back("*");
		params.push_back("SVSACCOUNT");
		params.push_back("DEL");
		params.push_back(acctname);
		params.push_back(ConvToStr(ts));
		ServerInstance->PI->SendEncapsulatedData(params);
		AccountDBModifiedEvent(creator, acctname, NULL).Send();
	}
};

/** Handle /SVSACCOUNT
 */
class CommandSvsaccount : public Command
{
 public:
	AccountDBProviderImpl prov;
	CommandSvsaccount(Module* Creator) : Command(Creator,"SVSACCOUNT", 3, 6), prov(Creator)
	{
		flags_needed = FLAG_SERVERONLY; syntax = "ADD|SET|DEL <account name> <account TS> [key] [value TS] [value]";
	}

	CmdResult Handle (const std::vector<std::string>& parameters, User *user)
	{
		GetAccountByAliasEvent e(creator, parameters[1]);
		AccountDB::iterator iter = prov.db.find(parameters[1]);
		time_t ts = atol(parameters[2].c_str());
		if(parameters[0] == "SET")
		{
			if(parameters.size() < 5)
				return CMD_INVALID; /* this form of the command needs at least 5 parameters */
			if(iter == prov.db.end())
				return CMD_FAILURE; /* if this ever happens, we're desynced */
			if(e.entry)
			{
				if(e.alias_ts < ts)
					return CMD_FAILURE;
				e.RemoveAlias();
			}
			if(iter->second->ts < ts)
				return CMD_FAILURE; /* we have an older account with the same name */
			if(iter->second->ts > ts)
			{
				/* Nuke the entry. */
				prov.RemoveAccount(false, iter->second);
				AccountDBEntry* entry = new AccountDBEntryImpl(parameters[1], ts);
				iter = prov.db.insert(std::make_pair(parameters[1], entry)).first;
			}
			ExtensionItem* ext = ServerInstance->Extensions.GetItem(parameters[3]);
			if (ext)
			{
				if(parameters.size() > 5)
					ext->unserialize(FORMAT_NETWORK, iter->second, parameters[4] + " " + parameters[5]);
				else
					ext->unserialize(FORMAT_NETWORK, iter->second, parameters[4]);
			}
			if(parameters[3] == "hash_password")
			{
				time_t theirTS = atol(parameters[4].c_str());
				if(iter->second->hash_password_ts > theirTS)
					return CMD_FAILURE;
				std::string theirhash, theirpassword;
				std::string::size_type delim;
				if(parameters.size() > 5 && (delim = parameters[5].find_first_of(' ')) != std::string::npos)
				{
					theirhash = parameters[5].substr(0, delim);
					theirpassword = parameters[5].substr(delim + 1);
				}
				else
					theirhash = theirpassword = "";

				if(iter->second->hash_password_ts < theirTS || iter->second->hash < theirhash || iter->second->password < theirpassword)
				{
					iter->second->hash = theirhash;
					iter->second->password = theirpassword;
					iter->second->hash_password_ts = theirTS;
				}
			}
			AccountDBModifiedEvent(creator, iter->second->name, iter->second).Send();
		}
		else if(parameters[0] == "ADD")
		{
			if(e.entry)
			{
				if(e.alias_ts < ts)
					return CMD_FAILURE;
				e.RemoveAlias();
			}
			if(iter == prov.db.end() || iter->second->ts > ts)
			{
				if(iter != prov.db.end())
					prov.RemoveAccount(false, iter->second);
				AccountDBEntry* entry = new AccountDBEntryImpl(parameters[1], ts);
				iter = prov.db.insert(std::make_pair(parameters[1], entry)).first;
			}
			else if(iter->second->ts < ts)
				return CMD_FAILURE;
			AccountDBModifiedEvent(creator, iter->second->name, iter->second).Send();
		}
		else if(parameters[0] == "DEL")
		{
			if(iter != prov.db.end())
			{
				if(iter->second->ts < ts)
					return CMD_FAILURE;
				prov.RemoveAccount(false, iter->second);
				AccountDBModifiedEvent(creator, parameters[1], NULL).Send();
			}
		}
		else
			return CMD_FAILURE;
		return CMD_SUCCESS;
	}
};

/** Handle /IDENTIFY
 */
class CommandIdentify : public Command
{
	AccountDB& db;
 public:
	CommandIdentify(Module* Creator, AccountDB& db_ref) : Command(Creator,"IDENTIFY", 1, 2), db(db_ref)
	{
		syntax = "[account name] <password>";
	}

	bool TryLogin (User* user, irc::string username, std::string password)
	{
		AccountDBEntry* entry;
		GetAccountByAliasEvent e(creator, username);
		if(e.entry)
			entry = e.entry;
		else
		{
			AccountDB::const_iterator iter = db.find(username);
			if(iter == db.end())
				return false;
			entry = iter->second;
		}
		if(entry->password.empty() || ServerInstance->PassCompare(user, entry->password, password, entry->hash))
			return false;
		if(account)
			account->DoLogin(user, entry->name, "");
		return true;
	}

	CmdResult Handle (const std::vector<std::string>& parameters, User *user)
	{
		bool result;
		if(parameters.size() == 1)
			result = TryLogin(user, user->nick, parameters[0]);
		else
			result = TryLogin(user, parameters[0], parameters[1]);

		if(!result)
		{
			user->WriteServ("NOTICE %s :Invalid username or password", user->nick.c_str());
			return CMD_FAILURE;
		}
		return CMD_SUCCESS;

	}
};

/** Handle /LOGOUT
 */
class CommandLogout : public Command
{
 public:
	CommandLogout(Module* Creator) : Command(Creator,"LOGOUT", 0, 0)
	{
	}

	CmdResult Handle (const std::vector<std::string>& parameters, User *user)
	{
		if(account)
		{
			if(account->IsRegistered(user))
			{
				account->DoLogin(user, "", "");
				user->WriteServ("NOTICE %s :Logout successful", user->nick.c_str());
			}
			else
				user->WriteServ("NOTICE %s :You are not logged in", user->nick.c_str());
		}
		return CMD_SUCCESS;
	}
};

class ModuleAccount : public Module
{
 private:
	CommandSvsaccount cmd;
	CommandIdentify cmd_identify;
	CommandLogout cmd_logout;

 public:
	ModuleAccount() : cmd(this), cmd_identify(this, cmd.prov.db), cmd_logout(this)
	{
	}

	void init()
	{
		ServerInstance->SNO->EnableSnomask('u', "ACCOUNT");
		ServerInstance->Modules->AddService(cmd);
		ServerInstance->Modules->AddService(cmd.prov);
		ServerInstance->Modules->AddService(cmd_identify);
		ServerInstance->Modules->AddService(cmd_logout);
		Implementation eventlist[] = { I_OnUserRegister, I_OnSyncNetwork, I_OnUnloadModule };
		ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
	}

	virtual ~ModuleAccount()
	{
		for(AccountDB::iterator iter = cmd.prov.db.begin(); iter != cmd.prov.db.end(); ++iter)
		{
			iter->second->cull();
			delete iter->second;
		}
		cmd.prov.db.clear();
	}

	void OnUserRegister(LocalUser* user)
	{
		if (account && account->IsRegistered(user))
			return;
		std::string::size_type sep = user->password.find_first_of(':');
		if(sep != std::string::npos &&
			cmd_identify.TryLogin(user, user->password.substr(0, sep), user->password.substr(sep + 1)))
			return;
		if(cmd_identify.TryLogin(user, user->nick, user->password))
			return;
		cmd_identify.TryLogin(user, user->ident, user->password);
	}

	void OnSyncNetwork(SyncTarget* target)
	{
		for (AccountDB::const_iterator i = cmd.prov.db.begin(); i != cmd.prov.db.end(); ++i)
		{
			std::string name = i->first, ts = ConvToStr(i->second->ts);
			target->SendCommand("ENCAP * SVSACCOUNT ADD " + name + " :" + ts);
			target->SendCommand("ENCAP * SVSACCOUNT SET " + name + " " + ts + " hash_password "
				+ ConvToStr(i->second->hash_password_ts) + " :" + i->second->hash + " " + i->second->password);
			for(Extensible::ExtensibleStore::const_iterator it = i->second->GetExtList().begin(); it != i->second->GetExtList().end(); ++it)
			{
				ExtensionItem* item = it->first;
				std::string value = item->serialize(FORMAT_NETWORK, i->second, it->second);
				if (!value.empty())
					target->SendCommand("ENCAP * SVSACCOUNT SET " + name + " " + ts + " " + item->name + " " + value);
			}
		}
	}

	void OnUnloadModule(Module* mod)
	{
		std::vector<reference<ExtensionItem> > acct_exts;
		ServerInstance->Extensions.BeginUnregister(mod, EXTENSIBLE_ACCOUNT, acct_exts);
		for(AccountDB::const_iterator iter = cmd.prov.db.begin(); iter != cmd.prov.db.end(); ++iter)
		{
			mod->OnCleanup(TYPE_OTHER, iter->second);
			iter->second->doUnhookExtensions(acct_exts);
			AccountDBModifiedEvent(this, iter->second->name, iter->second).Send();
		}
	}

	Version GetVersion()
	{
		return Version("Store account information and allow logging in to them ircd-side", VF_VENDOR|VF_OPTCOMMON);
	}
};

MODULE_INIT(ModuleAccount)